omap.c 33.4 KB
Newer Older
C
Carlos Aguiar 已提交
1
/*
P
Pierre Ossman 已提交
2
 *  linux/drivers/mmc/host/omap.c
C
Carlos Aguiar 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
 *
 *  Copyright (C) 2004 Nokia Corporation
 *  Written by Tuukka Tikkanen and Juha Yrjl<juha.yrjola@nokia.com>
 *  Misc hacks here and there by Tony Lindgren <tony@atomide.com>
 *  Other hacks (DMA, SD, etc) by David Brownell
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/init.h>
#include <linux/ioport.h>
#include <linux/platform_device.h>
#include <linux/interrupt.h>
#include <linux/dma-mapping.h>
#include <linux/delay.h>
#include <linux/spinlock.h>
#include <linux/timer.h>
#include <linux/mmc/host.h>
#include <linux/mmc/card.h>
#include <linux/clk.h>
J
Jens Axboe 已提交
27
#include <linux/scatterlist.h>
28
#include <linux/i2c/tps65010.h>
C
Carlos Aguiar 已提交
29 30 31 32 33 34

#include <asm/io.h>
#include <asm/irq.h>
#include <asm/mach-types.h>

#include <asm/arch/board.h>
35
#include <asm/arch/mmc.h>
C
Carlos Aguiar 已提交
36 37 38 39 40
#include <asm/arch/gpio.h>
#include <asm/arch/dma.h>
#include <asm/arch/mux.h>
#include <asm/arch/fpga.h>

41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
#define	OMAP_MMC_REG_CMD	0x00
#define	OMAP_MMC_REG_ARGL	0x04
#define	OMAP_MMC_REG_ARGH	0x08
#define	OMAP_MMC_REG_CON	0x0c
#define	OMAP_MMC_REG_STAT	0x10
#define	OMAP_MMC_REG_IE		0x14
#define	OMAP_MMC_REG_CTO	0x18
#define	OMAP_MMC_REG_DTO	0x1c
#define	OMAP_MMC_REG_DATA	0x20
#define	OMAP_MMC_REG_BLEN	0x24
#define	OMAP_MMC_REG_NBLK	0x28
#define	OMAP_MMC_REG_BUF	0x2c
#define OMAP_MMC_REG_SDIO	0x34
#define	OMAP_MMC_REG_REV	0x3c
#define	OMAP_MMC_REG_RSP0	0x40
#define	OMAP_MMC_REG_RSP1	0x44
#define	OMAP_MMC_REG_RSP2	0x48
#define	OMAP_MMC_REG_RSP3	0x4c
#define	OMAP_MMC_REG_RSP4	0x50
#define	OMAP_MMC_REG_RSP5	0x54
#define	OMAP_MMC_REG_RSP6	0x58
#define	OMAP_MMC_REG_RSP7	0x5c
#define	OMAP_MMC_REG_IOSR	0x60
#define	OMAP_MMC_REG_SYSC	0x64
#define	OMAP_MMC_REG_SYSS	0x68

#define	OMAP_MMC_STAT_CARD_ERR		(1 << 14)
#define	OMAP_MMC_STAT_CARD_IRQ		(1 << 13)
#define	OMAP_MMC_STAT_OCR_BUSY		(1 << 12)
#define	OMAP_MMC_STAT_A_EMPTY		(1 << 11)
#define	OMAP_MMC_STAT_A_FULL		(1 << 10)
#define	OMAP_MMC_STAT_CMD_CRC		(1 <<  8)
#define	OMAP_MMC_STAT_CMD_TOUT		(1 <<  7)
#define	OMAP_MMC_STAT_DATA_CRC		(1 <<  6)
#define	OMAP_MMC_STAT_DATA_TOUT		(1 <<  5)
#define	OMAP_MMC_STAT_END_BUSY		(1 <<  4)
#define	OMAP_MMC_STAT_END_OF_DATA	(1 <<  3)
#define	OMAP_MMC_STAT_CARD_BUSY		(1 <<  2)
#define	OMAP_MMC_STAT_END_OF_CMD	(1 <<  0)

#define OMAP_MMC_READ(host, reg)	__raw_readw((host)->virt_base + OMAP_MMC_REG_##reg)
#define OMAP_MMC_WRITE(host, reg, val)	__raw_writew((val), (host)->virt_base + OMAP_MMC_REG_##reg)

/*
 * Command types
 */
#define OMAP_MMC_CMDTYPE_BC	0
#define OMAP_MMC_CMDTYPE_BCR	1
#define OMAP_MMC_CMDTYPE_AC	2
#define OMAP_MMC_CMDTYPE_ADTC	3

C
Carlos Aguiar 已提交
92 93 94 95 96 97 98

#define DRIVER_NAME "mmci-omap"

/* Specifies how often in millisecs to poll for card status changes
 * when the cover switch is open */
#define OMAP_MMC_SWITCH_POLL_DELAY	500

99 100 101 102 103 104 105 106 107 108
struct mmc_omap_host;

struct mmc_omap_slot {
	int			id;
	unsigned int		vdd;
	u16			saved_con;
	u16			bus_mode;
	unsigned int		fclk_freq;
	unsigned		powered:1;

109 110 111 112
	struct work_struct      switch_work;
	struct timer_list       switch_timer;
	unsigned		cover_open;

113 114 115 116 117 118
	struct mmc_request      *mrq;
	struct mmc_omap_host    *host;
	struct mmc_host		*mmc;
	struct omap_mmc_slot_data *pdata;
};

C
Carlos Aguiar 已提交
119 120 121 122 123 124 125 126 127 128 129
struct mmc_omap_host {
	int			initialized;
	int			suspended;
	struct mmc_request *	mrq;
	struct mmc_command *	cmd;
	struct mmc_data *	data;
	struct mmc_host *	mmc;
	struct device *		dev;
	unsigned char		id; /* 16xx chips have 2 MMC blocks */
	struct clk *		iclk;
	struct clk *		fclk;
130 131 132
	struct resource		*mem_res;
	void __iomem		*virt_base;
	unsigned int		phys_base;
C
Carlos Aguiar 已提交
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
	int			irq;
	unsigned char		bus_mode;
	unsigned char		hw_bus_mode;

	unsigned int		sg_len;
	int			sg_idx;
	u16 *			buffer;
	u32			buffer_bytes_left;
	u32			total_bytes_left;

	unsigned		use_dma:1;
	unsigned		brs_received:1, dma_done:1;
	unsigned		dma_is_read:1;
	unsigned		dma_in_use:1;
	int			dma_ch;
	spinlock_t		dma_lock;
	struct timer_list	dma_timer;
	unsigned		dma_len;

	short			power_pin;

154 155 156 157 158 159 160
	struct mmc_omap_slot    *slots[OMAP_MMC_MAX_SLOTS];
	struct mmc_omap_slot    *current_slot;
	spinlock_t              slot_lock;
	wait_queue_head_t       slot_wq;
	int                     nr_slots;

	struct omap_mmc_platform_data *pdata;
C
Carlos Aguiar 已提交
161 162
};

163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
static void mmc_omap_select_slot(struct mmc_omap_slot *slot, int claimed)
{
	struct mmc_omap_host *host = slot->host;
	unsigned long flags;

	if (claimed)
		goto no_claim;
	spin_lock_irqsave(&host->slot_lock, flags);
	while (host->mmc != NULL) {
		spin_unlock_irqrestore(&host->slot_lock, flags);
		wait_event(host->slot_wq, host->mmc == NULL);
		spin_lock_irqsave(&host->slot_lock, flags);
	}
	host->mmc = slot->mmc;
	spin_unlock_irqrestore(&host->slot_lock, flags);
no_claim:
	clk_enable(host->fclk);
	if (host->current_slot != slot) {
		if (host->pdata->switch_slot != NULL)
			host->pdata->switch_slot(mmc_dev(slot->mmc), slot->id);
		host->current_slot = slot;
	}

	/* Doing the dummy read here seems to work around some bug
	 * at least in OMAP24xx silicon where the command would not
	 * start after writing the CMD register. Sigh. */
	OMAP_MMC_READ(host, CON);

	OMAP_MMC_WRITE(host, CON, slot->saved_con);
}

static void mmc_omap_start_request(struct mmc_omap_host *host,
				   struct mmc_request *req);

static void mmc_omap_release_slot(struct mmc_omap_slot *slot)
{
	struct mmc_omap_host *host = slot->host;
	unsigned long flags;
	int i;

	BUG_ON(slot == NULL || host->mmc == NULL);
	clk_disable(host->fclk);

	spin_lock_irqsave(&host->slot_lock, flags);
	/* Check for any pending requests */
	for (i = 0; i < host->nr_slots; i++) {
		struct mmc_omap_slot *new_slot;
		struct mmc_request *rq;

		if (host->slots[i] == NULL || host->slots[i]->mrq == NULL)
			continue;

		new_slot = host->slots[i];
		/* The current slot should not have a request in queue */
		BUG_ON(new_slot == host->current_slot);

		host->mmc = new_slot->mmc;
		spin_unlock_irqrestore(&host->slot_lock, flags);
		mmc_omap_select_slot(new_slot, 1);
		rq = new_slot->mrq;
		new_slot->mrq = NULL;
		mmc_omap_start_request(host, rq);
		return;
	}

	host->mmc = NULL;
	wake_up(&host->slot_wq);
	spin_unlock_irqrestore(&host->slot_lock, flags);
}

233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
static inline
int mmc_omap_cover_is_open(struct mmc_omap_slot *slot)
{
	return slot->pdata->get_cover_state(mmc_dev(slot->mmc), slot->id);
}

static ssize_t
mmc_omap_show_cover_switch(struct device *dev, struct device_attribute *attr,
			   char *buf)
{
	struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
	struct mmc_omap_slot *slot = mmc_priv(mmc);

	return sprintf(buf, "%s\n", mmc_omap_cover_is_open(slot) ? "open" :
		       "closed");
}

static DEVICE_ATTR(cover_switch, S_IRUGO, mmc_omap_show_cover_switch, NULL);

252 253 254 255 256 257 258 259 260 261 262 263
static ssize_t
mmc_omap_show_slot_name(struct device *dev, struct device_attribute *attr,
			char *buf)
{
	struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
	struct mmc_omap_slot *slot = mmc_priv(mmc);

	return sprintf(buf, "%s\n", slot->pdata->name);
}

static DEVICE_ATTR(slot_name, S_IRUGO, mmc_omap_show_slot_name, NULL);

C
Carlos Aguiar 已提交
264 265 266 267 268 269 270 271 272 273 274 275 276
static void
mmc_omap_start_command(struct mmc_omap_host *host, struct mmc_command *cmd)
{
	u32 cmdreg;
	u32 resptype;
	u32 cmdtype;

	host->cmd = cmd;

	resptype = 0;
	cmdtype = 0;

	/* Our hardware needs to know exact type */
277 278 279 280 281
	switch (mmc_resp_type(cmd)) {
	case MMC_RSP_NONE:
		break;
	case MMC_RSP_R1:
	case MMC_RSP_R1B:
P
Philip Langdale 已提交
282
		/* resp 1, 1b, 6, 7 */
C
Carlos Aguiar 已提交
283 284
		resptype = 1;
		break;
285
	case MMC_RSP_R2:
C
Carlos Aguiar 已提交
286 287
		resptype = 2;
		break;
288
	case MMC_RSP_R3:
C
Carlos Aguiar 已提交
289 290 291
		resptype = 3;
		break;
	default:
292
		dev_err(mmc_dev(host->mmc), "Invalid response type: %04x\n", mmc_resp_type(cmd));
C
Carlos Aguiar 已提交
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
		break;
	}

	if (mmc_cmd_type(cmd) == MMC_CMD_ADTC) {
		cmdtype = OMAP_MMC_CMDTYPE_ADTC;
	} else if (mmc_cmd_type(cmd) == MMC_CMD_BC) {
		cmdtype = OMAP_MMC_CMDTYPE_BC;
	} else if (mmc_cmd_type(cmd) == MMC_CMD_BCR) {
		cmdtype = OMAP_MMC_CMDTYPE_BCR;
	} else {
		cmdtype = OMAP_MMC_CMDTYPE_AC;
	}

	cmdreg = cmd->opcode | (resptype << 8) | (cmdtype << 12);

308
	if (host->current_slot->bus_mode == MMC_BUSMODE_OPENDRAIN)
C
Carlos Aguiar 已提交
309 310 311 312 313 314 315 316
		cmdreg |= 1 << 6;

	if (cmd->flags & MMC_RSP_BUSY)
		cmdreg |= 1 << 11;

	if (host->data && !(host->data->flags & MMC_DATA_WRITE))
		cmdreg |= 1 << 15;

317 318 319 320
	OMAP_MMC_WRITE(host, CTO, 200);
	OMAP_MMC_WRITE(host, ARGL, cmd->arg & 0xffff);
	OMAP_MMC_WRITE(host, ARGH, cmd->arg >> 16);
	OMAP_MMC_WRITE(host, IE,
C
Carlos Aguiar 已提交
321 322 323 324 325
		       OMAP_MMC_STAT_A_EMPTY    | OMAP_MMC_STAT_A_FULL    |
		       OMAP_MMC_STAT_CMD_CRC    | OMAP_MMC_STAT_CMD_TOUT  |
		       OMAP_MMC_STAT_DATA_CRC   | OMAP_MMC_STAT_DATA_TOUT |
		       OMAP_MMC_STAT_END_OF_CMD | OMAP_MMC_STAT_CARD_ERR  |
		       OMAP_MMC_STAT_END_OF_DATA);
326
	OMAP_MMC_WRITE(host, CMD, cmdreg);
C
Carlos Aguiar 已提交
327 328 329 330 331 332 333 334 335
}

static void
mmc_omap_xfer_done(struct mmc_omap_host *host, struct mmc_data *data)
{
	if (host->dma_in_use) {
		enum dma_data_direction dma_data_dir;

		BUG_ON(host->dma_ch < 0);
P
Pierre Ossman 已提交
336
		if (data->error)
C
Carlos Aguiar 已提交
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
			omap_stop_dma(host->dma_ch);
		/* Release DMA channel lazily */
		mod_timer(&host->dma_timer, jiffies + HZ);
		if (data->flags & MMC_DATA_WRITE)
			dma_data_dir = DMA_TO_DEVICE;
		else
			dma_data_dir = DMA_FROM_DEVICE;
		dma_unmap_sg(mmc_dev(host->mmc), data->sg, host->sg_len,
			     dma_data_dir);
	}
	host->data = NULL;
	host->sg_len = 0;
	clk_disable(host->fclk);

	/* NOTE:  MMC layer will sometimes poll-wait CMD13 next, issuing
	 * dozens of requests until the card finishes writing data.
	 * It'd be cheaper to just wait till an EOFB interrupt arrives...
	 */

	if (!data->stop) {
		host->mrq = NULL;
		mmc_request_done(host->mmc, data->mrq);
		return;
	}

	mmc_omap_start_command(host, data->stop);
}

static void
mmc_omap_end_of_data(struct mmc_omap_host *host, struct mmc_data *data)
{
	unsigned long flags;
	int done;

	if (!host->dma_in_use) {
		mmc_omap_xfer_done(host, data);
		return;
	}
	done = 0;
	spin_lock_irqsave(&host->dma_lock, flags);
	if (host->dma_done)
		done = 1;
	else
		host->brs_received = 1;
	spin_unlock_irqrestore(&host->dma_lock, flags);
	if (done)
		mmc_omap_xfer_done(host, data);
}

static void
mmc_omap_dma_timer(unsigned long data)
{
	struct mmc_omap_host *host = (struct mmc_omap_host *) data;

	BUG_ON(host->dma_ch < 0);
	omap_free_dma(host->dma_ch);
	host->dma_ch = -1;
}

static void
mmc_omap_dma_done(struct mmc_omap_host *host, struct mmc_data *data)
{
	unsigned long flags;
	int done;

	done = 0;
	spin_lock_irqsave(&host->dma_lock, flags);
	if (host->brs_received)
		done = 1;
	else
		host->dma_done = 1;
	spin_unlock_irqrestore(&host->dma_lock, flags);
	if (done)
		mmc_omap_xfer_done(host, data);
}

static void
mmc_omap_cmd_done(struct mmc_omap_host *host, struct mmc_command *cmd)
{
	host->cmd = NULL;

	if (cmd->flags & MMC_RSP_PRESENT) {
		if (cmd->flags & MMC_RSP_136) {
			/* response type 2 */
			cmd->resp[3] =
422 423
				OMAP_MMC_READ(host, RSP0) |
				(OMAP_MMC_READ(host, RSP1) << 16);
C
Carlos Aguiar 已提交
424
			cmd->resp[2] =
425 426
				OMAP_MMC_READ(host, RSP2) |
				(OMAP_MMC_READ(host, RSP3) << 16);
C
Carlos Aguiar 已提交
427
			cmd->resp[1] =
428 429
				OMAP_MMC_READ(host, RSP4) |
				(OMAP_MMC_READ(host, RSP5) << 16);
C
Carlos Aguiar 已提交
430
			cmd->resp[0] =
431 432
				OMAP_MMC_READ(host, RSP6) |
				(OMAP_MMC_READ(host, RSP7) << 16);
C
Carlos Aguiar 已提交
433 434 435
		} else {
			/* response types 1, 1b, 3, 4, 5, 6 */
			cmd->resp[0] =
436 437
				OMAP_MMC_READ(host, RSP6) |
				(OMAP_MMC_READ(host, RSP7) << 16);
C
Carlos Aguiar 已提交
438 439 440
		}
	}

P
Pierre Ossman 已提交
441
	if (host->data == NULL || cmd->error) {
C
Carlos Aguiar 已提交
442 443 444 445 446 447 448 449 450 451 452 453 454 455
		host->mrq = NULL;
		clk_disable(host->fclk);
		mmc_request_done(host->mmc, cmd->mrq);
	}
}

/* PIO only */
static void
mmc_omap_sg_to_buf(struct mmc_omap_host *host)
{
	struct scatterlist *sg;

	sg = host->data->sg + host->sg_idx;
	host->buffer_bytes_left = sg->length;
J
Jens Axboe 已提交
456
	host->buffer = sg_virt(sg);
C
Carlos Aguiar 已提交
457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
	if (host->buffer_bytes_left > host->total_bytes_left)
		host->buffer_bytes_left = host->total_bytes_left;
}

/* PIO only */
static void
mmc_omap_xfer_data(struct mmc_omap_host *host, int write)
{
	int n;

	if (host->buffer_bytes_left == 0) {
		host->sg_idx++;
		BUG_ON(host->sg_idx == host->sg_len);
		mmc_omap_sg_to_buf(host);
	}
	n = 64;
	if (n > host->buffer_bytes_left)
		n = host->buffer_bytes_left;
	host->buffer_bytes_left -= n;
	host->total_bytes_left -= n;
	host->data->bytes_xfered += n;

	if (write) {
480
		__raw_writesw(host->virt_base + OMAP_MMC_REG_DATA, host->buffer, n);
C
Carlos Aguiar 已提交
481
	} else {
482
		__raw_readsw(host->virt_base + OMAP_MMC_REG_DATA, host->buffer, n);
C
Carlos Aguiar 已提交
483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502
	}
}

static inline void mmc_omap_report_irq(u16 status)
{
	static const char *mmc_omap_status_bits[] = {
		"EOC", "CD", "CB", "BRS", "EOFB", "DTO", "DCRC", "CTO",
		"CCRC", "CRW", "AF", "AE", "OCRB", "CIRQ", "CERR"
	};
	int i, c = 0;

	for (i = 0; i < ARRAY_SIZE(mmc_omap_status_bits); i++)
		if (status & (1 << i)) {
			if (c)
				printk(" ");
			printk("%s", mmc_omap_status_bits[i]);
			c++;
		}
}

503
static irqreturn_t mmc_omap_irq(int irq, void *dev_id)
C
Carlos Aguiar 已提交
504 505 506 507 508 509 510 511
{
	struct mmc_omap_host * host = (struct mmc_omap_host *)dev_id;
	u16 status;
	int end_command;
	int end_transfer;
	int transfer_error;

	if (host->cmd == NULL && host->data == NULL) {
512
		status = OMAP_MMC_READ(host, STAT);
C
Carlos Aguiar 已提交
513 514
		dev_info(mmc_dev(host->mmc),"spurious irq 0x%04x\n", status);
		if (status != 0) {
515 516
			OMAP_MMC_WRITE(host, STAT, status);
			OMAP_MMC_WRITE(host, IE, 0);
C
Carlos Aguiar 已提交
517 518 519 520 521 522 523 524
		}
		return IRQ_HANDLED;
	}

	end_command = 0;
	end_transfer = 0;
	transfer_error = 0;

525 526
	while ((status = OMAP_MMC_READ(host, STAT)) != 0) {
		OMAP_MMC_WRITE(host, STAT, status);
C
Carlos Aguiar 已提交
527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547
#ifdef CONFIG_MMC_DEBUG
		dev_dbg(mmc_dev(host->mmc), "MMC IRQ %04x (CMD %d): ",
			status, host->cmd != NULL ? host->cmd->opcode : -1);
		mmc_omap_report_irq(status);
		printk("\n");
#endif
		if (host->total_bytes_left) {
			if ((status & OMAP_MMC_STAT_A_FULL) ||
			    (status & OMAP_MMC_STAT_END_OF_DATA))
				mmc_omap_xfer_data(host, 0);
			if (status & OMAP_MMC_STAT_A_EMPTY)
				mmc_omap_xfer_data(host, 1);
		}

		if (status & OMAP_MMC_STAT_END_OF_DATA) {
			end_transfer = 1;
		}

		if (status & OMAP_MMC_STAT_DATA_TOUT) {
			dev_dbg(mmc_dev(host->mmc), "data timeout\n");
			if (host->data) {
P
Pierre Ossman 已提交
548
				host->data->error = -ETIMEDOUT;
C
Carlos Aguiar 已提交
549 550 551 552 553 554
				transfer_error = 1;
			}
		}

		if (status & OMAP_MMC_STAT_DATA_CRC) {
			if (host->data) {
P
Pierre Ossman 已提交
555
				host->data->error = -EILSEQ;
C
Carlos Aguiar 已提交
556 557 558 559 560 561 562 563 564 565 566 567
				dev_dbg(mmc_dev(host->mmc),
					 "data CRC error, bytes left %d\n",
					host->total_bytes_left);
				transfer_error = 1;
			} else {
				dev_dbg(mmc_dev(host->mmc), "data CRC error\n");
			}
		}

		if (status & OMAP_MMC_STAT_CMD_TOUT) {
			/* Timeouts are routine with some commands */
			if (host->cmd) {
568 569
				struct mmc_omap_slot *slot =
					host->current_slot;
570 571 572 573
				if (!mmc_omap_cover_is_open(slot))
					dev_err(mmc_dev(host->mmc),
						"command timeout, CMD %d\n",
						host->cmd->opcode);
P
Pierre Ossman 已提交
574
				host->cmd->error = -ETIMEDOUT;
C
Carlos Aguiar 已提交
575 576 577 578 579 580 581 582 583
				end_command = 1;
			}
		}

		if (status & OMAP_MMC_STAT_CMD_CRC) {
			if (host->cmd) {
				dev_err(mmc_dev(host->mmc),
					"command CRC error (CMD%d, arg 0x%08x)\n",
					host->cmd->opcode, host->cmd->arg);
P
Pierre Ossman 已提交
584
				host->cmd->error = -EILSEQ;
C
Carlos Aguiar 已提交
585 586 587 588 589 590 591
				end_command = 1;
			} else
				dev_err(mmc_dev(host->mmc),
					"command CRC error without cmd?\n");
		}

		if (status & OMAP_MMC_STAT_CARD_ERR) {
592 593
			dev_dbg(mmc_dev(host->mmc),
				"ignoring card status error (CMD%d)\n",
C
Carlos Aguiar 已提交
594
				host->cmd->opcode);
595
			end_command = 1;
C
Carlos Aguiar 已提交
596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618
		}

		/*
		 * NOTE: On 1610 the END_OF_CMD may come too early when
		 * starting a write 
		 */
		if ((status & OMAP_MMC_STAT_END_OF_CMD) &&
		    (!(status & OMAP_MMC_STAT_A_EMPTY))) {
			end_command = 1;
		}
	}

	if (end_command) {
		mmc_omap_cmd_done(host, host->cmd);
	}
	if (transfer_error)
		mmc_omap_xfer_done(host, host->data);
	else if (end_transfer)
		mmc_omap_end_of_data(host, host->data);

	return IRQ_HANDLED;
}

619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654
void omap_mmc_notify_cover_event(struct device *dev, int slot, int is_closed)
{
	struct mmc_omap_host *host = dev_get_drvdata(dev);

	BUG_ON(slot >= host->nr_slots);

	/* Other subsystems can call in here before we're initialised. */
	if (host->nr_slots == 0 || !host->slots[slot])
		return;

	schedule_work(&host->slots[slot]->switch_work);
}

static void mmc_omap_switch_timer(unsigned long arg)
{
	struct mmc_omap_slot *slot = (struct mmc_omap_slot *) arg;

	schedule_work(&slot->switch_work);
}

static void mmc_omap_cover_handler(struct work_struct *work)
{
	struct mmc_omap_slot *slot = container_of(work, struct mmc_omap_slot,
						  switch_work);
	int cover_open;

	cover_open = mmc_omap_cover_is_open(slot);
	if (cover_open != slot->cover_open) {
		sysfs_notify(&slot->mmc->class_dev.kobj, NULL, "cover_switch");
		slot->cover_open = cover_open;
		dev_info(mmc_dev(slot->mmc), "cover is now %s\n",
			 cover_open ? "open" : "closed");
	}
	mmc_detect_change(slot->mmc, slot->id);
}

C
Carlos Aguiar 已提交
655 656 657 658 659 660 661 662 663 664 665 666 667
/* Prepare to transfer the next segment of a scatterlist */
static void
mmc_omap_prepare_dma(struct mmc_omap_host *host, struct mmc_data *data)
{
	int dma_ch = host->dma_ch;
	unsigned long data_addr;
	u16 buf, frame;
	u32 count;
	struct scatterlist *sg = &data->sg[host->sg_idx];
	int src_port = 0;
	int dst_port = 0;
	int sync_dev = 0;

668
	data_addr = host->phys_base + OMAP_MMC_REG_DATA;
669
	frame = data->blksz;
C
Carlos Aguiar 已提交
670 671
	count = sg_dma_len(sg);

672
	if ((data->blocks == 1) && (count > data->blksz))
C
Carlos Aguiar 已提交
673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726
		count = frame;

	host->dma_len = count;

	/* FIFO is 16x2 bytes on 15xx, and 32x2 bytes on 16xx and 24xx.
	 * Use 16 or 32 word frames when the blocksize is at least that large.
	 * Blocksize is usually 512 bytes; but not for some SD reads.
	 */
	if (cpu_is_omap15xx() && frame > 32)
		frame = 32;
	else if (frame > 64)
		frame = 64;
	count /= frame;
	frame >>= 1;

	if (!(data->flags & MMC_DATA_WRITE)) {
		buf = 0x800f | ((frame - 1) << 8);

		if (cpu_class_is_omap1()) {
			src_port = OMAP_DMA_PORT_TIPB;
			dst_port = OMAP_DMA_PORT_EMIFF;
		}
		if (cpu_is_omap24xx())
			sync_dev = OMAP24XX_DMA_MMC1_RX;

		omap_set_dma_src_params(dma_ch, src_port,
					OMAP_DMA_AMODE_CONSTANT,
					data_addr, 0, 0);
		omap_set_dma_dest_params(dma_ch, dst_port,
					 OMAP_DMA_AMODE_POST_INC,
					 sg_dma_address(sg), 0, 0);
		omap_set_dma_dest_data_pack(dma_ch, 1);
		omap_set_dma_dest_burst_mode(dma_ch, OMAP_DMA_DATA_BURST_4);
	} else {
		buf = 0x0f80 | ((frame - 1) << 0);

		if (cpu_class_is_omap1()) {
			src_port = OMAP_DMA_PORT_EMIFF;
			dst_port = OMAP_DMA_PORT_TIPB;
		}
		if (cpu_is_omap24xx())
			sync_dev = OMAP24XX_DMA_MMC1_TX;

		omap_set_dma_dest_params(dma_ch, dst_port,
					 OMAP_DMA_AMODE_CONSTANT,
					 data_addr, 0, 0);
		omap_set_dma_src_params(dma_ch, src_port,
					OMAP_DMA_AMODE_POST_INC,
					sg_dma_address(sg), 0, 0);
		omap_set_dma_src_data_pack(dma_ch, 1);
		omap_set_dma_src_burst_mode(dma_ch, OMAP_DMA_DATA_BURST_4);
	}

	/* Max limit for DMA frame count is 0xffff */
727
	BUG_ON(count > 0xffff);
C
Carlos Aguiar 已提交
728

729
	OMAP_MMC_WRITE(host, BUF, buf);
C
Carlos Aguiar 已提交
730 731 732 733 734 735 736 737 738 739 740 741
	omap_set_dma_transfer_params(dma_ch, OMAP_DMA_DATA_TYPE_S16,
				     frame, count, OMAP_DMA_SYNC_FRAME,
				     sync_dev, 0);
}

/* A scatterlist segment completed */
static void mmc_omap_dma_cb(int lch, u16 ch_status, void *data)
{
	struct mmc_omap_host *host = (struct mmc_omap_host *) data;
	struct mmc_data *mmcdat = host->data;

	if (unlikely(host->dma_ch < 0)) {
742 743
		dev_err(mmc_dev(host->mmc),
			"DMA callback while DMA not enabled\n");
C
Carlos Aguiar 已提交
744 745 746
		return;
	}
	/* FIXME: We really should do something to _handle_ the errors */
747
	if (ch_status & OMAP1_DMA_TOUT_IRQ) {
C
Carlos Aguiar 已提交
748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813
		dev_err(mmc_dev(host->mmc),"DMA timeout\n");
		return;
	}
	if (ch_status & OMAP_DMA_DROP_IRQ) {
		dev_err(mmc_dev(host->mmc), "DMA sync error\n");
		return;
	}
	if (!(ch_status & OMAP_DMA_BLOCK_IRQ)) {
		return;
	}
	mmcdat->bytes_xfered += host->dma_len;
	host->sg_idx++;
	if (host->sg_idx < host->sg_len) {
		mmc_omap_prepare_dma(host, host->data);
		omap_start_dma(host->dma_ch);
	} else
		mmc_omap_dma_done(host, host->data);
}

static int mmc_omap_get_dma_channel(struct mmc_omap_host *host, struct mmc_data *data)
{
	const char *dev_name;
	int sync_dev, dma_ch, is_read, r;

	is_read = !(data->flags & MMC_DATA_WRITE);
	del_timer_sync(&host->dma_timer);
	if (host->dma_ch >= 0) {
		if (is_read == host->dma_is_read)
			return 0;
		omap_free_dma(host->dma_ch);
		host->dma_ch = -1;
	}

	if (is_read) {
		if (host->id == 1) {
			sync_dev = OMAP_DMA_MMC_RX;
			dev_name = "MMC1 read";
		} else {
			sync_dev = OMAP_DMA_MMC2_RX;
			dev_name = "MMC2 read";
		}
	} else {
		if (host->id == 1) {
			sync_dev = OMAP_DMA_MMC_TX;
			dev_name = "MMC1 write";
		} else {
			sync_dev = OMAP_DMA_MMC2_TX;
			dev_name = "MMC2 write";
		}
	}
	r = omap_request_dma(sync_dev, dev_name, mmc_omap_dma_cb,
			     host, &dma_ch);
	if (r != 0) {
		dev_dbg(mmc_dev(host->mmc), "omap_request_dma() failed with %d\n", r);
		return r;
	}
	host->dma_ch = dma_ch;
	host->dma_is_read = is_read;

	return 0;
}

static inline void set_cmd_timeout(struct mmc_omap_host *host, struct mmc_request *req)
{
	u16 reg;

814
	reg = OMAP_MMC_READ(host, SDIO);
C
Carlos Aguiar 已提交
815
	reg &= ~(1 << 5);
816
	OMAP_MMC_WRITE(host, SDIO, reg);
C
Carlos Aguiar 已提交
817
	/* Set maximum timeout */
818
	OMAP_MMC_WRITE(host, CTO, 0xff);
C
Carlos Aguiar 已提交
819 820 821 822 823 824 825 826 827 828 829 830 831
}

static inline void set_data_timeout(struct mmc_omap_host *host, struct mmc_request *req)
{
	int timeout;
	u16 reg;

	/* Convert ns to clock cycles by assuming 20MHz frequency
	 * 1 cycle at 20MHz = 500 ns
	 */
	timeout = req->data->timeout_clks + req->data->timeout_ns / 500;

	/* Check if we need to use timeout multiplier register */
832
	reg = OMAP_MMC_READ(host, SDIO);
C
Carlos Aguiar 已提交
833 834 835 836 837
	if (timeout > 0xffff) {
		reg |= (1 << 5);
		timeout /= 1024;
	} else
		reg &= ~(1 << 5);
838 839
	OMAP_MMC_WRITE(host, SDIO, reg);
	OMAP_MMC_WRITE(host, DTO, timeout);
C
Carlos Aguiar 已提交
840 841 842 843 844 845 846 847 848 849 850
}

static void
mmc_omap_prepare_data(struct mmc_omap_host *host, struct mmc_request *req)
{
	struct mmc_data *data = req->data;
	int i, use_dma, block_size;
	unsigned sg_len;

	host->data = data;
	if (data == NULL) {
851 852 853
		OMAP_MMC_WRITE(host, BLEN, 0);
		OMAP_MMC_WRITE(host, NBLK, 0);
		OMAP_MMC_WRITE(host, BUF, 0);
C
Carlos Aguiar 已提交
854 855 856 857 858
		host->dma_in_use = 0;
		set_cmd_timeout(host, req);
		return;
	}

859
	block_size = data->blksz;
C
Carlos Aguiar 已提交
860

861 862
	OMAP_MMC_WRITE(host, NBLK, data->blocks - 1);
	OMAP_MMC_WRITE(host, BLEN, block_size - 1);
C
Carlos Aguiar 已提交
863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903
	set_data_timeout(host, req);

	/* cope with calling layer confusion; it issues "single
	 * block" writes using multi-block scatterlists.
	 */
	sg_len = (data->blocks == 1) ? 1 : data->sg_len;

	/* Only do DMA for entire blocks */
	use_dma = host->use_dma;
	if (use_dma) {
		for (i = 0; i < sg_len; i++) {
			if ((data->sg[i].length % block_size) != 0) {
				use_dma = 0;
				break;
			}
		}
	}

	host->sg_idx = 0;
	if (use_dma) {
		if (mmc_omap_get_dma_channel(host, data) == 0) {
			enum dma_data_direction dma_data_dir;

			if (data->flags & MMC_DATA_WRITE)
				dma_data_dir = DMA_TO_DEVICE;
			else
				dma_data_dir = DMA_FROM_DEVICE;

			host->sg_len = dma_map_sg(mmc_dev(host->mmc), data->sg,
						sg_len, dma_data_dir);
			host->total_bytes_left = 0;
			mmc_omap_prepare_dma(host, req->data);
			host->brs_received = 0;
			host->dma_done = 0;
			host->dma_in_use = 1;
		} else
			use_dma = 0;
	}

	/* Revert to PIO? */
	if (!use_dma) {
904
		OMAP_MMC_WRITE(host, BUF, 0x1f1f);
C
Carlos Aguiar 已提交
905 906 907 908 909 910 911
		host->total_bytes_left = data->blocks * block_size;
		host->sg_len = sg_len;
		mmc_omap_sg_to_buf(host);
		host->dma_in_use = 0;
	}
}

912 913
static void mmc_omap_start_request(struct mmc_omap_host *host,
				   struct mmc_request *req)
C
Carlos Aguiar 已提交
914
{
915
	BUG_ON(host->mrq != NULL);
C
Carlos Aguiar 已提交
916 917 918 919 920 921 922 923

	host->mrq = req;

	/* only touch fifo AFTER the controller readies it */
	mmc_omap_prepare_data(host, req);
	mmc_omap_start_command(host, req->cmd);
	if (host->dma_in_use)
		omap_start_dma(host->dma_ch);
924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943
	BUG_ON(irqs_disabled());
}

static void mmc_omap_request(struct mmc_host *mmc, struct mmc_request *req)
{
	struct mmc_omap_slot *slot = mmc_priv(mmc);
	struct mmc_omap_host *host = slot->host;
	unsigned long flags;

	spin_lock_irqsave(&host->slot_lock, flags);
	if (host->mmc != NULL) {
		BUG_ON(slot->mrq != NULL);
		slot->mrq = req;
		spin_unlock_irqrestore(&host->slot_lock, flags);
		return;
	} else
		host->mmc = mmc;
	spin_unlock_irqrestore(&host->slot_lock, flags);
	mmc_omap_select_slot(slot, 1);
	mmc_omap_start_request(host, req);
C
Carlos Aguiar 已提交
944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973
}

static void innovator_fpga_socket_power(int on)
{
#if defined(CONFIG_MACH_OMAP_INNOVATOR) && defined(CONFIG_ARCH_OMAP15XX)
	if (on) {
		fpga_write(fpga_read(OMAP1510_FPGA_POWER) | (1 << 3),
		     OMAP1510_FPGA_POWER);
	} else {
		fpga_write(fpga_read(OMAP1510_FPGA_POWER) & ~(1 << 3),
		     OMAP1510_FPGA_POWER);
	}
#endif
}

/*
 * Turn the socket power on/off. Innovator uses FPGA, most boards
 * probably use GPIO.
 */
static void mmc_omap_power(struct mmc_omap_host *host, int on)
{
	if (on) {
		if (machine_is_omap_innovator())
			innovator_fpga_socket_power(1);
		else if (machine_is_omap_h2())
			tps65010_set_gpio_out_value(GPIO3, HIGH);
		else if (machine_is_omap_h3())
			/* GPIO 4 of TPS65010 sends SD_EN signal */
			tps65010_set_gpio_out_value(GPIO4, HIGH);
		else if (cpu_is_omap24xx()) {
974 975
			u16 reg = OMAP_MMC_READ(host, CON);
			OMAP_MMC_WRITE(host, CON, reg | (1 << 11));
C
Carlos Aguiar 已提交
976 977 978 979 980 981 982 983 984 985 986
		} else
			if (host->power_pin >= 0)
				omap_set_gpio_dataout(host->power_pin, 1);
	} else {
		if (machine_is_omap_innovator())
			innovator_fpga_socket_power(0);
		else if (machine_is_omap_h2())
			tps65010_set_gpio_out_value(GPIO3, LOW);
		else if (machine_is_omap_h3())
			tps65010_set_gpio_out_value(GPIO4, LOW);
		else if (cpu_is_omap24xx()) {
987 988
			u16 reg = OMAP_MMC_READ(host, CON);
			OMAP_MMC_WRITE(host, CON, reg & ~(1 << 11));
C
Carlos Aguiar 已提交
989 990 991 992 993 994
		} else
			if (host->power_pin >= 0)
				omap_set_gpio_dataout(host->power_pin, 0);
	}
}

995
static int mmc_omap_calc_divisor(struct mmc_host *mmc, struct mmc_ios *ios)
C
Carlos Aguiar 已提交
996
{
997 998
	struct mmc_omap_slot *slot = mmc_priv(mmc);
	struct mmc_omap_host *host = slot->host;
999
	int func_clk_rate = clk_get_rate(host->fclk);
C
Carlos Aguiar 已提交
1000 1001 1002
	int dsor;

	if (ios->clock == 0)
1003
		return 0;
C
Carlos Aguiar 已提交
1004

1005 1006 1007
	dsor = func_clk_rate / ios->clock;
	if (dsor < 1)
		dsor = 1;
C
Carlos Aguiar 已提交
1008

1009
	if (func_clk_rate / dsor > ios->clock)
C
Carlos Aguiar 已提交
1010 1011
		dsor++;

1012 1013 1014
	if (dsor > 250)
		dsor = 250;

1015 1016
	slot->fclk_freq = func_clk_rate / dsor;

1017 1018 1019 1020 1021 1022 1023 1024
	if (ios->bus_width == MMC_BUS_WIDTH_4)
		dsor |= 1 << 15;

	return dsor;
}

static void mmc_omap_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
{
1025 1026 1027
	struct mmc_omap_slot *slot = mmc_priv(mmc);
	struct mmc_omap_host *host = slot->host;
	int i, dsor;
1028 1029 1030 1031

	dsor = mmc_omap_calc_divisor(mmc, ios);
	host->bus_mode = ios->bus_mode;
	host->hw_bus_mode = host->bus_mode;
C
Carlos Aguiar 已提交
1032 1033 1034 1035 1036 1037

	switch (ios->power_mode) {
	case MMC_POWER_OFF:
		mmc_omap_power(host, 0);
		break;
	case MMC_POWER_UP:
1038
		/* Cannot touch dsor yet, just power up MMC */
C
Carlos Aguiar 已提交
1039
		mmc_omap_power(host, 1);
1040 1041
		return;
	case MMC_POWER_ON:
1042
		dsor |= 1 << 11;
C
Carlos Aguiar 已提交
1043 1044 1045 1046 1047 1048 1049 1050 1051 1052
		break;
	}

	clk_enable(host->fclk);

	/* On insanely high arm_per frequencies something sometimes
	 * goes somehow out of sync, and the POW bit is not being set,
	 * which results in the while loop below getting stuck.
	 * Writing to the CON register twice seems to do the trick. */
	for (i = 0; i < 2; i++)
1053
		OMAP_MMC_WRITE(host, CON, dsor);
1054
	if (ios->power_mode == MMC_POWER_ON) {
C
Carlos Aguiar 已提交
1055
		/* Send clock cycles, poll completion */
1056 1057
		OMAP_MMC_WRITE(host, IE, 0);
		OMAP_MMC_WRITE(host, STAT, 0xffff);
1058 1059
		OMAP_MMC_WRITE(host, CMD, 1 << 7);
		while ((OMAP_MMC_READ(host, STAT) & 1) == 0);
1060
		OMAP_MMC_WRITE(host, STAT, 1);
C
Carlos Aguiar 已提交
1061 1062 1063 1064
	}
	clk_disable(host->fclk);
}

1065
static const struct mmc_host_ops mmc_omap_ops = {
C
Carlos Aguiar 已提交
1066 1067 1068 1069
	.request	= mmc_omap_request,
	.set_ios	= mmc_omap_set_ios,
};

1070
static int __init mmc_omap_new_slot(struct mmc_omap_host *host, int id)
C
Carlos Aguiar 已提交
1071
{
1072
	struct mmc_omap_slot *slot = NULL;
C
Carlos Aguiar 已提交
1073
	struct mmc_host *mmc;
1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124
	int r;

	mmc = mmc_alloc_host(sizeof(struct mmc_omap_slot), host->dev);
	if (mmc == NULL)
		return -ENOMEM;

	slot = mmc_priv(mmc);
	slot->host = host;
	slot->mmc = mmc;
	slot->id = id;
	slot->pdata = &host->pdata->slots[id];

	host->slots[id] = slot;

	mmc->caps = MMC_CAP_MULTIWRITE;
	if (host->pdata->conf.wire4)
		mmc->caps |= MMC_CAP_4_BIT_DATA;

	mmc->ops = &mmc_omap_ops;
	mmc->f_min = 400000;

	if (cpu_class_is_omap2())
		mmc->f_max = 48000000;
	else
		mmc->f_max = 24000000;
	if (host->pdata->max_freq)
		mmc->f_max = min(host->pdata->max_freq, mmc->f_max);
	mmc->ocr_avail = slot->pdata->ocr_mask;

	/* Use scatterlist DMA to reduce per-transfer costs.
	 * NOTE max_seg_size assumption that small blocks aren't
	 * normally used (except e.g. for reading SD registers).
	 */
	mmc->max_phys_segs = 32;
	mmc->max_hw_segs = 32;
	mmc->max_blk_size = 2048;	/* BLEN is 11 bits (+1) */
	mmc->max_blk_count = 2048;	/* NBLK is 11 bits (+1) */
	mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
	mmc->max_seg_size = mmc->max_req_size;

	r = mmc_add_host(mmc);
	if (r < 0)
		goto err_remove_host;

	if (slot->pdata->name != NULL) {
		r = device_create_file(&mmc->class_dev,
					&dev_attr_slot_name);
		if (r < 0)
			goto err_remove_host;
	}

1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137
	if (slot->pdata->get_cover_state != NULL) {
		r = device_create_file(&mmc->class_dev,
					&dev_attr_cover_switch);
		if (r < 0)
			goto err_remove_slot_name;

		INIT_WORK(&slot->switch_work, mmc_omap_cover_handler);
		init_timer(&slot->switch_timer);
		slot->switch_timer.function = mmc_omap_switch_timer;
		slot->switch_timer.data = (unsigned long) slot;
		schedule_work(&slot->switch_work);
	}

1138 1139
	return 0;

1140 1141 1142
err_remove_slot_name:
	if (slot->pdata->name != NULL)
		device_remove_file(&mmc->class_dev, &dev_attr_slot_name);
1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154
err_remove_host:
	mmc_remove_host(mmc);
	mmc_free_host(mmc);
	return r;
}

static void mmc_omap_remove_slot(struct mmc_omap_slot *slot)
{
	struct mmc_host *mmc = slot->mmc;

	if (slot->pdata->name != NULL)
		device_remove_file(&mmc->class_dev, &dev_attr_slot_name);
1155 1156 1157 1158 1159
	if (slot->pdata->get_cover_state != NULL)
		device_remove_file(&mmc->class_dev, &dev_attr_cover_switch);

	del_timer_sync(&slot->switch_timer);
	flush_scheduled_work();
1160 1161 1162 1163 1164 1165 1166 1167

	mmc_remove_host(mmc);
	mmc_free_host(mmc);
}

static int __init mmc_omap_probe(struct platform_device *pdev)
{
	struct omap_mmc_platform_data *pdata = pdev->dev.platform_data;
C
Carlos Aguiar 已提交
1168
	struct mmc_omap_host *host = NULL;
1169
	struct resource *res;
1170
	int i, ret = 0;
1171
	int irq;
1172

1173
	if (pdata == NULL) {
1174 1175 1176
		dev_err(&pdev->dev, "platform data missing\n");
		return -ENXIO;
	}
1177 1178 1179 1180
	if (pdata->nr_slots == 0) {
		dev_err(&pdev->dev, "no slots\n");
		return -ENXIO;
	}
1181 1182

	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1183
	irq = platform_get_irq(pdev, 0);
1184
	if (res == NULL || irq < 0)
1185
		return -ENXIO;
C
Carlos Aguiar 已提交
1186

1187
	res = request_mem_region(res->start, res->end - res->start + 1,
1188
				 pdev->name);
1189
	if (res == NULL)
C
Carlos Aguiar 已提交
1190 1191
		return -EBUSY;

1192 1193
	host = kzalloc(sizeof(struct mmc_omap_host), GFP_KERNEL);
	if (host == NULL) {
C
Carlos Aguiar 已提交
1194
		ret = -ENOMEM;
1195
		goto err_free_mem_region;
C
Carlos Aguiar 已提交
1196 1197 1198 1199
	}

	spin_lock_init(&host->dma_lock);
	init_timer(&host->dma_timer);
1200 1201 1202
	spin_lock_init(&host->slot_lock);
	init_waitqueue_head(&host->slot_wq);

C
Carlos Aguiar 已提交
1203 1204 1205
	host->dma_timer.function = mmc_omap_dma_timer;
	host->dma_timer.data = (unsigned long) host;

1206 1207 1208 1209
	host->pdata = pdata;
	host->dev = &pdev->dev;
	platform_set_drvdata(pdev, host);

C
Carlos Aguiar 已提交
1210
	host->id = pdev->id;
1211
	host->mem_res = res;
1212
	host->irq = irq;
C
Carlos Aguiar 已提交
1213

1214 1215 1216 1217 1218 1219 1220
	host->use_dma = 1;
	host->dma_ch = -1;

	host->irq = irq;
	host->phys_base = host->mem_res->start;
	host->virt_base = (void __iomem *) IO_ADDRESS(host->phys_base);

C
Carlos Aguiar 已提交
1221 1222 1223
	if (cpu_is_omap24xx()) {
		host->iclk = clk_get(&pdev->dev, "mmc_ick");
		if (IS_ERR(host->iclk))
1224
			goto err_free_mmc_host;
C
Carlos Aguiar 已提交
1225 1226 1227 1228 1229 1230 1231 1232 1233 1234
		clk_enable(host->iclk);
	}

	if (!cpu_is_omap24xx())
		host->fclk = clk_get(&pdev->dev, "mmc_ck");
	else
		host->fclk = clk_get(&pdev->dev, "mmc_fck");

	if (IS_ERR(host->fclk)) {
		ret = PTR_ERR(host->fclk);
1235
		goto err_free_iclk;
C
Carlos Aguiar 已提交
1236 1237
	}

1238 1239 1240
	ret = request_irq(host->irq, mmc_omap_irq, 0, DRIVER_NAME, host);
	if (ret)
		goto err_free_fclk;
1241

1242 1243 1244 1245 1246
	if (pdata->init != NULL) {
		ret = pdata->init(&pdev->dev);
		if (ret < 0)
			goto err_free_irq;
	}
C
Carlos Aguiar 已提交
1247

1248 1249 1250 1251 1252 1253
	host->nr_slots = pdata->nr_slots;
	for (i = 0; i < pdata->nr_slots; i++) {
		ret = mmc_omap_new_slot(host, i);
		if (ret < 0) {
			while (--i >= 0)
				mmc_omap_remove_slot(host->slots[i]);
C
Carlos Aguiar 已提交
1254

1255
			goto err_plat_cleanup;
C
Carlos Aguiar 已提交
1256 1257 1258 1259 1260
		}
	}

	return 0;

1261 1262 1263 1264 1265
err_plat_cleanup:
	if (pdata->cleanup)
		pdata->cleanup(&pdev->dev);
err_free_irq:
	free_irq(host->irq, host);
1266 1267 1268 1269 1270 1271 1272 1273
err_free_fclk:
	clk_put(host->fclk);
err_free_iclk:
	if (host->iclk != NULL) {
		clk_disable(host->iclk);
		clk_put(host->iclk);
	}
err_free_mmc_host:
1274
	kfree(host);
1275 1276
err_free_mem_region:
	release_mem_region(res->start, res->end - res->start + 1);
C
Carlos Aguiar 已提交
1277 1278 1279 1280 1281 1282
	return ret;
}

static int mmc_omap_remove(struct platform_device *pdev)
{
	struct mmc_omap_host *host = platform_get_drvdata(pdev);
1283
	int i;
C
Carlos Aguiar 已提交
1284 1285 1286

	platform_set_drvdata(pdev, NULL);

1287 1288
	BUG_ON(host == NULL);

1289 1290 1291 1292 1293
	for (i = 0; i < host->nr_slots; i++)
		mmc_omap_remove_slot(host->slots[i]);

	if (host->pdata->cleanup)
		host->pdata->cleanup(&pdev->dev);
1294 1295 1296 1297 1298

	if (host->iclk && !IS_ERR(host->iclk))
		clk_put(host->iclk);
	if (host->fclk && !IS_ERR(host->fclk))
		clk_put(host->fclk);
C
Carlos Aguiar 已提交
1299 1300

	release_mem_region(pdev->resource[0].start,
1301 1302
			   pdev->resource[0].end - pdev->resource[0].start + 1);

1303
	kfree(host);
C
Carlos Aguiar 已提交
1304 1305 1306 1307 1308 1309 1310

	return 0;
}

#ifdef CONFIG_PM
static int mmc_omap_suspend(struct platform_device *pdev, pm_message_t mesg)
{
1311
	int i, ret = 0;
C
Carlos Aguiar 已提交
1312 1313
	struct mmc_omap_host *host = platform_get_drvdata(pdev);

1314
	if (host == NULL || host->suspended)
C
Carlos Aguiar 已提交
1315 1316
		return 0;

1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328
	for (i = 0; i < host->nr_slots; i++) {
		struct mmc_omap_slot *slot;

		slot = host->slots[i];
		ret = mmc_suspend_host(slot->mmc, mesg);
		if (ret < 0) {
			while (--i >= 0) {
				slot = host->slots[i];
				mmc_resume_host(slot->mmc);
			}
			return ret;
		}
C
Carlos Aguiar 已提交
1329
	}
1330 1331
	host->suspended = 1;
	return 0;
C
Carlos Aguiar 已提交
1332 1333 1334 1335
}

static int mmc_omap_resume(struct platform_device *pdev)
{
1336
	int i, ret = 0;
C
Carlos Aguiar 已提交
1337 1338
	struct mmc_omap_host *host = platform_get_drvdata(pdev);

1339
	if (host == NULL || !host->suspended)
C
Carlos Aguiar 已提交
1340 1341
		return 0;

1342 1343 1344 1345 1346 1347
	for (i = 0; i < host->nr_slots; i++) {
		struct mmc_omap_slot *slot;
		slot = host->slots[i];
		ret = mmc_resume_host(slot->mmc);
		if (ret < 0)
			return ret;
C
Carlos Aguiar 已提交
1348

1349 1350 1351
		host->suspended = 0;
	}
	return 0;
C
Carlos Aguiar 已提交
1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364
}
#else
#define mmc_omap_suspend	NULL
#define mmc_omap_resume		NULL
#endif

static struct platform_driver mmc_omap_driver = {
	.probe		= mmc_omap_probe,
	.remove		= mmc_omap_remove,
	.suspend	= mmc_omap_suspend,
	.resume		= mmc_omap_resume,
	.driver		= {
		.name	= DRIVER_NAME,
1365
		.owner	= THIS_MODULE,
C
Carlos Aguiar 已提交
1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383
	},
};

static int __init mmc_omap_init(void)
{
	return platform_driver_register(&mmc_omap_driver);
}

static void __exit mmc_omap_exit(void)
{
	platform_driver_unregister(&mmc_omap_driver);
}

module_init(mmc_omap_init);
module_exit(mmc_omap_exit);

MODULE_DESCRIPTION("OMAP Multimedia Card driver");
MODULE_LICENSE("GPL");
1384
MODULE_ALIAS("platform:" DRIVER_NAME);
C
Carlos Aguiar 已提交
1385
MODULE_AUTHOR("Juha Yrjl");