2 * linux/drivers/mmc/host/imxmmc.c - Motorola i.MX MMCI driver
4 * Copyright (C) 2004 Sascha Hauer, Pengutronix <sascha@saschahauer.de>
5 * Copyright (C) 2006 Pavel Pisa, PiKRON <ppisa@pikron.com>
7 * derived from pxamci.c by Russell King
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
15 #include <linux/module.h>
16 #include <linux/init.h>
17 #include <linux/ioport.h>
18 #include <linux/platform_device.h>
19 #include <linux/interrupt.h>
20 #include <linux/blkdev.h>
21 #include <linux/dma-mapping.h>
22 #include <linux/mmc/host.h>
23 #include <linux/mmc/card.h>
24 #include <linux/delay.h>
25 #include <linux/clk.h>
30 #include <asm/sizes.h>
32 #include <mach/imx-dma.h>
36 #define DRIVER_NAME "imx-mmc"
38 #define IMXMCI_INT_MASK_DEFAULT (INT_MASK_BUF_READY | INT_MASK_DATA_TRAN | \
39 INT_MASK_WRITE_OP_DONE | INT_MASK_END_CMD_RES | \
40 INT_MASK_AUTO_CARD_DETECT | INT_MASK_DAT0_EN | INT_MASK_SDIO)
49 volatile unsigned int imask;
50 unsigned int power_mode;
52 struct imxmmc_platform_data *pdata;
54 struct mmc_request *req;
55 struct mmc_command *cmd;
56 struct mmc_data *data;
58 struct timer_list timer;
59 struct tasklet_struct tasklet;
60 unsigned int status_reg;
61 unsigned long pending_events;
62 /* Next two fields are there for CPU driven transfers to overcome SDHC deficiencies */
64 unsigned int data_cnt;
65 atomic_t stuck_timeout;
67 unsigned int dma_nents;
68 unsigned int dma_size;
72 unsigned char actual_bus_width;
79 #define IMXMCI_PEND_IRQ_b 0
80 #define IMXMCI_PEND_DMA_END_b 1
81 #define IMXMCI_PEND_DMA_ERR_b 2
82 #define IMXMCI_PEND_WAIT_RESP_b 3
83 #define IMXMCI_PEND_DMA_DATA_b 4
84 #define IMXMCI_PEND_CPU_DATA_b 5
85 #define IMXMCI_PEND_CARD_XCHG_b 6
86 #define IMXMCI_PEND_SET_INIT_b 7
87 #define IMXMCI_PEND_STARTED_b 8
89 #define IMXMCI_PEND_IRQ_m (1 << IMXMCI_PEND_IRQ_b)
90 #define IMXMCI_PEND_DMA_END_m (1 << IMXMCI_PEND_DMA_END_b)
91 #define IMXMCI_PEND_DMA_ERR_m (1 << IMXMCI_PEND_DMA_ERR_b)
92 #define IMXMCI_PEND_WAIT_RESP_m (1 << IMXMCI_PEND_WAIT_RESP_b)
93 #define IMXMCI_PEND_DMA_DATA_m (1 << IMXMCI_PEND_DMA_DATA_b)
94 #define IMXMCI_PEND_CPU_DATA_m (1 << IMXMCI_PEND_CPU_DATA_b)
95 #define IMXMCI_PEND_CARD_XCHG_m (1 << IMXMCI_PEND_CARD_XCHG_b)
96 #define IMXMCI_PEND_SET_INIT_m (1 << IMXMCI_PEND_SET_INIT_b)
97 #define IMXMCI_PEND_STARTED_m (1 << IMXMCI_PEND_STARTED_b)
99 static void imxmci_stop_clock(struct imxmci_host *host)
104 reg = readw(host->base + MMC_REG_STR_STP_CLK);
105 writew(reg & ~STR_STP_CLK_START_CLK, host->base + MMC_REG_STR_STP_CLK);
108 reg = readw(host->base + MMC_REG_STR_STP_CLK);
109 writew(reg | STR_STP_CLK_STOP_CLK,
110 host->base + MMC_REG_STR_STP_CLK);
113 reg = readw(host->base + MMC_REG_STATUS);
114 if (!(reg & STATUS_CARD_BUS_CLK_RUN)) {
115 /* Check twice before cut */
116 reg = readw(host->base + MMC_REG_STATUS);
117 if (!(reg & STATUS_CARD_BUS_CLK_RUN))
123 dev_dbg(mmc_dev(host->mmc), "imxmci_stop_clock blocked, no luck\n");
126 static int imxmci_start_clock(struct imxmci_host *host)
128 unsigned int trials = 0;
129 unsigned int delay_limit = 128;
133 reg = readw(host->base + MMC_REG_STR_STP_CLK);
134 writew(reg & ~STR_STP_CLK_STOP_CLK, host->base + MMC_REG_STR_STP_CLK);
136 clear_bit(IMXMCI_PEND_STARTED_b, &host->pending_events);
139 * Command start of the clock, this usually succeeds in less
140 * then 6 delay loops, but during card detection (low clockrate)
141 * it takes up to 5000 delay loops and sometimes fails for the first time
143 reg = readw(host->base + MMC_REG_STR_STP_CLK);
144 writew(reg | STR_STP_CLK_START_CLK, host->base + MMC_REG_STR_STP_CLK);
147 unsigned int delay = delay_limit;
150 reg = readw(host->base + MMC_REG_STATUS);
151 if (reg & STATUS_CARD_BUS_CLK_RUN)
152 /* Check twice before cut */
153 reg = readw(host->base + MMC_REG_STATUS);
154 if (reg & STATUS_CARD_BUS_CLK_RUN)
157 if (test_bit(IMXMCI_PEND_STARTED_b, &host->pending_events))
161 local_irq_save(flags);
163 * Ensure, that request is not doubled under all possible circumstances.
164 * It is possible, that cock running state is missed, because some other
165 * IRQ or schedule delays this function execution and the clocks has
166 * been already stopped by other means (response processing, SDHC HW)
168 if (!test_bit(IMXMCI_PEND_STARTED_b, &host->pending_events)) {
169 reg = readw(host->base + MMC_REG_STR_STP_CLK);
170 writew(reg | STR_STP_CLK_START_CLK,
171 host->base + MMC_REG_STR_STP_CLK);
173 local_irq_restore(flags);
175 } while (++trials < 256);
177 dev_err(mmc_dev(host->mmc), "imxmci_start_clock blocked, no luck\n");
182 static void imxmci_softreset(struct imxmci_host *host)
187 writew(0x08, host->base + MMC_REG_STR_STP_CLK);
188 writew(0x0D, host->base + MMC_REG_STR_STP_CLK);
190 for (i = 0; i < 8; i++)
191 writew(0x05, host->base + MMC_REG_STR_STP_CLK);
193 writew(0xff, host->base + MMC_REG_RES_TO);
194 writew(512, host->base + MMC_REG_BLK_LEN);
195 writew(1, host->base + MMC_REG_NOB);
198 static int imxmci_busy_wait_for_status(struct imxmci_host *host,
199 unsigned int *pstat, unsigned int stat_mask,
200 int timeout, const char *where)
204 while (!(*pstat & stat_mask)) {
206 if (loops >= timeout) {
207 dev_dbg(mmc_dev(host->mmc), "busy wait timeout in %s, STATUS = 0x%x (0x%x)\n",
208 where, *pstat, stat_mask);
212 *pstat |= readw(host->base + MMC_REG_STATUS);
217 /* The busy-wait is expected there for clock <8MHz due to SDHC hardware flaws */
218 if (!(stat_mask & STATUS_END_CMD_RESP) || (host->mmc->ios.clock >= 8000000))
219 dev_info(mmc_dev(host->mmc), "busy wait for %d usec in %s, STATUS = 0x%x (0x%x)\n",
220 loops, where, *pstat, stat_mask);
224 static void imxmci_setup_data(struct imxmci_host *host, struct mmc_data *data)
226 unsigned int nob = data->blocks;
227 unsigned int blksz = data->blksz;
228 unsigned int datasz = nob * blksz;
231 if (data->flags & MMC_DATA_STREAM)
235 data->bytes_xfered = 0;
237 writew(nob, host->base + MMC_REG_NOB);
238 writew(blksz, host->base + MMC_REG_BLK_LEN);
241 * DMA cannot be used for small block sizes, we have to use CPU driven transfers otherwise.
242 * We are in big troubles for non-512 byte transfers according to note in the paragraph
243 * 20.6.7 of User Manual anyway, but we need to be able to transfer SCR at least.
244 * The situation is even more complex in reality. The SDHC in not able to handle wll
245 * partial FIFO fills and reads. The length has to be rounded up to burst size multiple.
246 * This is required for SCR read at least.
249 host->dma_size = datasz;
250 if (data->flags & MMC_DATA_READ) {
251 host->dma_dir = DMA_FROM_DEVICE;
253 /* Hack to enable read SCR */
254 writew(1, host->base + MMC_REG_NOB);
255 writew(512, host->base + MMC_REG_BLK_LEN);
257 host->dma_dir = DMA_TO_DEVICE;
260 /* Convert back to virtual address */
261 host->data_ptr = (u16 *)sg_virt(data->sg);
264 clear_bit(IMXMCI_PEND_DMA_DATA_b, &host->pending_events);
265 set_bit(IMXMCI_PEND_CPU_DATA_b, &host->pending_events);
270 if (data->flags & MMC_DATA_READ) {
271 host->dma_dir = DMA_FROM_DEVICE;
272 host->dma_nents = dma_map_sg(mmc_dev(host->mmc), data->sg,
273 data->sg_len, host->dma_dir);
275 imx_dma_setup_sg(host->dma, data->sg, data->sg_len, datasz,
276 host->res->start + MMC_REG_BUFFER_ACCESS,
279 /*imx_dma_setup_mem2dev_ccr(host->dma, DMA_MODE_READ, IMX_DMA_WIDTH_16, CCR_REN);*/
280 CCR(host->dma) = CCR_DMOD_LINEAR | CCR_DSIZ_32 | CCR_SMOD_FIFO | CCR_SSIZ_16 | CCR_REN;
282 host->dma_dir = DMA_TO_DEVICE;
284 host->dma_nents = dma_map_sg(mmc_dev(host->mmc), data->sg,
285 data->sg_len, host->dma_dir);
287 imx_dma_setup_sg(host->dma, data->sg, data->sg_len, datasz,
288 host->res->start + MMC_REG_BUFFER_ACCESS,
291 /*imx_dma_setup_mem2dev_ccr(host->dma, DMA_MODE_WRITE, IMX_DMA_WIDTH_16, CCR_REN);*/
292 CCR(host->dma) = CCR_SMOD_LINEAR | CCR_SSIZ_32 | CCR_DMOD_FIFO | CCR_DSIZ_16 | CCR_REN;
295 #if 1 /* This code is there only for consistency checking and can be disabled in future */
297 for (i = 0; i < host->dma_nents; i++)
298 host->dma_size += data->sg[i].length;
300 if (datasz > host->dma_size) {
301 dev_err(mmc_dev(host->mmc), "imxmci_setup_data datasz 0x%x > 0x%x dm_size\n",
302 datasz, host->dma_size);
306 host->dma_size = datasz;
310 if (host->actual_bus_width == MMC_BUS_WIDTH_4)
311 BLR(host->dma) = 0; /* burst 64 byte read / 64 bytes write */
313 BLR(host->dma) = 16; /* burst 16 byte read / 16 bytes write */
315 RSSR(host->dma) = DMA_REQ_SDHC;
317 set_bit(IMXMCI_PEND_DMA_DATA_b, &host->pending_events);
318 clear_bit(IMXMCI_PEND_CPU_DATA_b, &host->pending_events);
320 /* start DMA engine for read, write is delayed after initial response */
321 if (host->dma_dir == DMA_FROM_DEVICE)
322 imx_dma_enable(host->dma);
325 static void imxmci_start_cmd(struct imxmci_host *host, struct mmc_command *cmd, unsigned int cmdat)
330 WARN_ON(host->cmd != NULL);
333 /* Ensure, that clock are stopped else command programming and start fails */
334 imxmci_stop_clock(host);
336 if (cmd->flags & MMC_RSP_BUSY)
337 cmdat |= CMD_DAT_CONT_BUSY;
339 switch (mmc_resp_type(cmd)) {
340 case MMC_RSP_R1: /* short CRC, OPCODE */
341 case MMC_RSP_R1B:/* short CRC, OPCODE, BUSY */
342 cmdat |= CMD_DAT_CONT_RESPONSE_FORMAT_R1;
344 case MMC_RSP_R2: /* long 136 bit + CRC */
345 cmdat |= CMD_DAT_CONT_RESPONSE_FORMAT_R2;
347 case MMC_RSP_R3: /* short */
348 cmdat |= CMD_DAT_CONT_RESPONSE_FORMAT_R3;
354 if (test_and_clear_bit(IMXMCI_PEND_SET_INIT_b, &host->pending_events))
355 cmdat |= CMD_DAT_CONT_INIT; /* This command needs init */
357 if (host->actual_bus_width == MMC_BUS_WIDTH_4)
358 cmdat |= CMD_DAT_CONT_BUS_WIDTH_4;
360 writew(cmd->opcode, host->base + MMC_REG_CMD);
361 writew(cmd->arg >> 16, host->base + MMC_REG_ARGH);
362 writew(cmd->arg & 0xffff, host->base + MMC_REG_ARGL);
363 writew(cmdat, host->base + MMC_REG_CMD_DAT_CONT);
365 atomic_set(&host->stuck_timeout, 0);
366 set_bit(IMXMCI_PEND_WAIT_RESP_b, &host->pending_events);
369 imask = IMXMCI_INT_MASK_DEFAULT;
370 imask &= ~INT_MASK_END_CMD_RES;
371 if (cmdat & CMD_DAT_CONT_DATA_ENABLE) {
372 /* imask &= ~INT_MASK_BUF_READY; */
373 imask &= ~INT_MASK_DATA_TRAN;
374 if (cmdat & CMD_DAT_CONT_WRITE)
375 imask &= ~INT_MASK_WRITE_OP_DONE;
376 if (test_bit(IMXMCI_PEND_CPU_DATA_b, &host->pending_events))
377 imask &= ~INT_MASK_BUF_READY;
380 spin_lock_irqsave(&host->lock, flags);
382 writew(host->imask, host->base + MMC_REG_INT_MASK);
383 spin_unlock_irqrestore(&host->lock, flags);
385 dev_dbg(mmc_dev(host->mmc), "CMD%02d (0x%02x) mask set to 0x%04x\n",
386 cmd->opcode, cmd->opcode, imask);
388 imxmci_start_clock(host);
391 static void imxmci_finish_request(struct imxmci_host *host, struct mmc_request *req)
395 spin_lock_irqsave(&host->lock, flags);
397 host->pending_events &= ~(IMXMCI_PEND_WAIT_RESP_m | IMXMCI_PEND_DMA_END_m |
398 IMXMCI_PEND_DMA_DATA_m | IMXMCI_PEND_CPU_DATA_m);
400 host->imask = IMXMCI_INT_MASK_DEFAULT;
401 writew(host->imask, host->base + MMC_REG_INT_MASK);
403 spin_unlock_irqrestore(&host->lock, flags);
406 host->prev_cmd_code = req->cmd->opcode;
411 mmc_request_done(host->mmc, req);
414 static int imxmci_finish_data(struct imxmci_host *host, unsigned int stat)
416 struct mmc_data *data = host->data;
419 if (test_and_clear_bit(IMXMCI_PEND_DMA_DATA_b, &host->pending_events)) {
420 imx_dma_disable(host->dma);
421 dma_unmap_sg(mmc_dev(host->mmc), data->sg, host->dma_nents,
425 if (stat & STATUS_ERR_MASK) {
426 dev_dbg(mmc_dev(host->mmc), "request failed. status: 0x%08x\n", stat);
427 if (stat & (STATUS_CRC_READ_ERR | STATUS_CRC_WRITE_ERR))
428 data->error = -EILSEQ;
429 else if (stat & STATUS_TIME_OUT_READ)
430 data->error = -ETIMEDOUT;
434 data->bytes_xfered = host->dma_size;
437 data_error = data->error;
444 static int imxmci_cmd_done(struct imxmci_host *host, unsigned int stat)
446 struct mmc_command *cmd = host->cmd;
449 struct mmc_data *data = host->data;
456 if (stat & STATUS_TIME_OUT_RESP) {
457 dev_dbg(mmc_dev(host->mmc), "CMD TIMEOUT\n");
458 cmd->error = -ETIMEDOUT;
459 } else if (stat & STATUS_RESP_CRC_ERR && cmd->flags & MMC_RSP_CRC) {
460 dev_dbg(mmc_dev(host->mmc), "cmd crc error\n");
461 cmd->error = -EILSEQ;
464 if (cmd->flags & MMC_RSP_PRESENT) {
465 if (cmd->flags & MMC_RSP_136) {
466 for (i = 0; i < 4; i++) {
467 a = readw(host->base + MMC_REG_RES_FIFO);
468 b = readw(host->base + MMC_REG_RES_FIFO);
469 cmd->resp[i] = a << 16 | b;
472 a = readw(host->base + MMC_REG_RES_FIFO);
473 b = readw(host->base + MMC_REG_RES_FIFO);
474 c = readw(host->base + MMC_REG_RES_FIFO);
475 cmd->resp[0] = a << 24 | b << 8 | c >> 8;
479 dev_dbg(mmc_dev(host->mmc), "RESP 0x%08x, 0x%08x, 0x%08x, 0x%08x, error %d\n",
480 cmd->resp[0], cmd->resp[1], cmd->resp[2], cmd->resp[3], cmd->error);
482 if (data && !cmd->error && !(stat & STATUS_ERR_MASK)) {
483 if (host->req->data->flags & MMC_DATA_WRITE) {
485 /* Wait for FIFO to be empty before starting DMA write */
487 stat = readw(host->base + MMC_REG_STATUS);
488 if (imxmci_busy_wait_for_status(host, &stat,
490 40, "imxmci_cmd_done DMA WR") < 0) {
492 imxmci_finish_data(host, stat);
494 imxmci_finish_request(host, host->req);
495 dev_warn(mmc_dev(host->mmc), "STATUS = 0x%04x\n",
500 if (test_bit(IMXMCI_PEND_DMA_DATA_b, &host->pending_events))
501 imx_dma_enable(host->dma);
504 struct mmc_request *req;
505 imxmci_stop_clock(host);
509 imxmci_finish_data(host, stat);
512 imxmci_finish_request(host, req);
514 dev_warn(mmc_dev(host->mmc), "imxmci_cmd_done: no request to finish\n");
520 static int imxmci_data_done(struct imxmci_host *host, unsigned int stat)
522 struct mmc_data *data = host->data;
528 data_error = imxmci_finish_data(host, stat);
530 if (host->req->stop) {
531 imxmci_stop_clock(host);
532 imxmci_start_cmd(host, host->req->stop, 0);
534 struct mmc_request *req;
537 imxmci_finish_request(host, req);
539 dev_warn(mmc_dev(host->mmc), "imxmci_data_done: no request to finish\n");
545 static int imxmci_cpu_driven_data(struct imxmci_host *host, unsigned int *pstat)
550 unsigned int stat = *pstat;
552 if (host->actual_bus_width != MMC_BUS_WIDTH_4)
557 /* This is unfortunately required */
558 dev_dbg(mmc_dev(host->mmc), "imxmci_cpu_driven_data running STATUS = 0x%x\n",
561 udelay(20); /* required for clocks < 8MHz*/
563 if (host->dma_dir == DMA_FROM_DEVICE) {
564 imxmci_busy_wait_for_status(host, &stat,
565 STATUS_APPL_BUFF_FF | STATUS_DATA_TRANS_DONE |
566 STATUS_TIME_OUT_READ,
567 50, "imxmci_cpu_driven_data read");
569 while ((stat & (STATUS_APPL_BUFF_FF | STATUS_DATA_TRANS_DONE)) &&
570 !(stat & STATUS_TIME_OUT_READ) &&
571 (host->data_cnt < 512)) {
573 udelay(20); /* required for clocks < 8MHz*/
575 for (i = burst_len; i >= 2 ; i -= 2) {
577 data = readw(host->base + MMC_REG_BUFFER_ACCESS);
578 udelay(10); /* required for clocks < 8MHz*/
579 if (host->data_cnt+2 <= host->dma_size) {
580 *(host->data_ptr++) = data;
582 if (host->data_cnt < host->dma_size)
583 *(u8 *)(host->data_ptr) = data;
588 stat = readw(host->base + MMC_REG_STATUS);
590 dev_dbg(mmc_dev(host->mmc), "imxmci_cpu_driven_data read %d burst %d STATUS = 0x%x\n",
591 host->data_cnt, burst_len, stat);
594 if ((stat & STATUS_DATA_TRANS_DONE) && (host->data_cnt >= 512))
597 if (host->dma_size & 0x1ff)
598 stat &= ~STATUS_CRC_READ_ERR;
600 if (stat & STATUS_TIME_OUT_READ) {
601 dev_dbg(mmc_dev(host->mmc), "imxmci_cpu_driven_data read timeout STATUS = 0x%x\n",
607 imxmci_busy_wait_for_status(host, &stat,
609 20, "imxmci_cpu_driven_data write");
611 while ((stat & STATUS_APPL_BUFF_FE) &&
612 (host->data_cnt < host->dma_size)) {
613 if (burst_len >= host->dma_size - host->data_cnt) {
614 burst_len = host->dma_size - host->data_cnt;
615 host->data_cnt = host->dma_size;
618 host->data_cnt += burst_len;
621 for (i = burst_len; i > 0 ; i -= 2)
622 writew(*(host->data_ptr++), host->base + MMC_REG_BUFFER_ACCESS);
624 stat = readw(host->base + MMC_REG_STATUS);
626 dev_dbg(mmc_dev(host->mmc), "imxmci_cpu_driven_data write burst %d STATUS = 0x%x\n",
636 static void imxmci_dma_irq(int dma, void *devid)
638 struct imxmci_host *host = devid;
639 u32 stat = readw(host->base + MMC_REG_STATUS);
641 atomic_set(&host->stuck_timeout, 0);
642 host->status_reg = stat;
643 set_bit(IMXMCI_PEND_DMA_END_b, &host->pending_events);
644 tasklet_schedule(&host->tasklet);
647 static irqreturn_t imxmci_irq(int irq, void *devid)
649 struct imxmci_host *host = devid;
650 u32 stat = readw(host->base + MMC_REG_STATUS);
653 writew(host->imask | INT_MASK_SDIO | INT_MASK_AUTO_CARD_DETECT,
654 host->base + MMC_REG_INT_MASK);
656 atomic_set(&host->stuck_timeout, 0);
657 host->status_reg = stat;
658 set_bit(IMXMCI_PEND_IRQ_b, &host->pending_events);
659 set_bit(IMXMCI_PEND_STARTED_b, &host->pending_events);
660 tasklet_schedule(&host->tasklet);
662 return IRQ_RETVAL(handled);;
665 static void imxmci_tasklet_fnc(unsigned long data)
667 struct imxmci_host *host = (struct imxmci_host *)data;
669 unsigned int data_dir_mask = 0; /* STATUS_WR_CRC_ERROR_CODE_MASK */
672 if (atomic_read(&host->stuck_timeout) > 4) {
675 stat = readw(host->base + MMC_REG_STATUS);
676 host->status_reg = stat;
677 if (test_bit(IMXMCI_PEND_WAIT_RESP_b, &host->pending_events))
678 if (test_bit(IMXMCI_PEND_DMA_DATA_b, &host->pending_events))
683 if (test_bit(IMXMCI_PEND_DMA_DATA_b, &host->pending_events))
684 if (test_bit(IMXMCI_PEND_DMA_END_b, &host->pending_events))
691 dev_err(mmc_dev(host->mmc),
692 "%s TIMEOUT, hardware stucked STATUS = 0x%04x IMASK = 0x%04x\n",
694 readw(host->base + MMC_REG_INT_MASK));
695 dev_err(mmc_dev(host->mmc),
696 "CMD_DAT_CONT = 0x%04x, MMC_BLK_LEN = 0x%04x, MMC_NOB = 0x%04x, DMA_CCR = 0x%08x\n",
697 readw(host->base + MMC_REG_CMD_DAT_CONT),
698 readw(host->base + MMC_REG_BLK_LEN),
699 readw(host->base + MMC_REG_NOB),
701 dev_err(mmc_dev(host->mmc), "CMD%d, prevCMD%d, bus %d-bit, dma_size = 0x%x\n",
702 host->cmd ? host->cmd->opcode : 0,
704 1 << host->actual_bus_width, host->dma_size);
707 if (!host->present || timeout)
708 host->status_reg = STATUS_TIME_OUT_RESP | STATUS_TIME_OUT_READ |
709 STATUS_CRC_READ_ERR | STATUS_CRC_WRITE_ERR;
711 if (test_bit(IMXMCI_PEND_IRQ_b, &host->pending_events) || timeout) {
712 clear_bit(IMXMCI_PEND_IRQ_b, &host->pending_events);
714 stat = readw(host->base + MMC_REG_STATUS);
716 * This is not required in theory, but there is chance to miss some flag
717 * which clears automatically by mask write, FreeScale original code keeps
718 * stat from IRQ time so do I
720 stat |= host->status_reg;
722 if (test_bit(IMXMCI_PEND_CPU_DATA_b, &host->pending_events))
723 stat &= ~STATUS_CRC_READ_ERR;
725 if (test_bit(IMXMCI_PEND_WAIT_RESP_b, &host->pending_events)) {
726 imxmci_busy_wait_for_status(host, &stat,
727 STATUS_END_CMD_RESP | STATUS_ERR_MASK,
728 20, "imxmci_tasklet_fnc resp (ERRATUM #4)");
731 if (stat & (STATUS_END_CMD_RESP | STATUS_ERR_MASK)) {
732 if (test_and_clear_bit(IMXMCI_PEND_WAIT_RESP_b, &host->pending_events))
733 imxmci_cmd_done(host, stat);
734 if (host->data && (stat & STATUS_ERR_MASK))
735 imxmci_data_done(host, stat);
738 if (test_bit(IMXMCI_PEND_CPU_DATA_b, &host->pending_events)) {
739 stat |= readw(host->base + MMC_REG_STATUS);
740 if (imxmci_cpu_driven_data(host, &stat)) {
741 if (test_and_clear_bit(IMXMCI_PEND_WAIT_RESP_b, &host->pending_events))
742 imxmci_cmd_done(host, stat);
743 atomic_clear_mask(IMXMCI_PEND_IRQ_m|IMXMCI_PEND_CPU_DATA_m,
744 &host->pending_events);
745 imxmci_data_done(host, stat);
750 if (test_bit(IMXMCI_PEND_DMA_END_b, &host->pending_events) &&
751 !test_bit(IMXMCI_PEND_WAIT_RESP_b, &host->pending_events)) {
753 stat = readw(host->base + MMC_REG_STATUS);
755 stat |= host->status_reg;
757 if (host->dma_dir == DMA_TO_DEVICE)
758 data_dir_mask = STATUS_WRITE_OP_DONE;
760 data_dir_mask = STATUS_DATA_TRANS_DONE;
762 if (stat & data_dir_mask) {
763 clear_bit(IMXMCI_PEND_DMA_END_b, &host->pending_events);
764 imxmci_data_done(host, stat);
768 if (test_and_clear_bit(IMXMCI_PEND_CARD_XCHG_b, &host->pending_events)) {
771 imxmci_cmd_done(host, STATUS_TIME_OUT_RESP);
774 imxmci_data_done(host, STATUS_TIME_OUT_READ |
775 STATUS_CRC_READ_ERR | STATUS_CRC_WRITE_ERR);
778 imxmci_finish_request(host, host->req);
780 mmc_detect_change(host->mmc, msecs_to_jiffies(100));
785 static void imxmci_request(struct mmc_host *mmc, struct mmc_request *req)
787 struct imxmci_host *host = mmc_priv(mmc);
790 WARN_ON(host->req != NULL);
797 imxmci_setup_data(host, req->data);
799 cmdat |= CMD_DAT_CONT_DATA_ENABLE;
801 if (req->data->flags & MMC_DATA_WRITE)
802 cmdat |= CMD_DAT_CONT_WRITE;
804 if (req->data->flags & MMC_DATA_STREAM)
805 cmdat |= CMD_DAT_CONT_STREAM_BLOCK;
808 imxmci_start_cmd(host, req->cmd, cmdat);
811 #define CLK_RATE 19200000
813 static void imxmci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
815 struct imxmci_host *host = mmc_priv(mmc);
818 if (ios->bus_width == MMC_BUS_WIDTH_4) {
819 host->actual_bus_width = MMC_BUS_WIDTH_4;
820 imx_gpio_mode(PB11_PF_SD_DAT3);
822 host->actual_bus_width = MMC_BUS_WIDTH_1;
823 imx_gpio_mode(GPIO_PORTB | GPIO_IN | GPIO_PUEN | 11);
826 if (host->power_mode != ios->power_mode) {
827 switch (ios->power_mode) {
831 set_bit(IMXMCI_PEND_SET_INIT_b, &host->pending_events);
836 host->power_mode = ios->power_mode;
843 /* The prescaler is 5 for PERCLK2 equal to 96MHz
844 * then 96MHz / 5 = 19.2 MHz
846 clk = clk_get_rate(host->clk);
847 prescaler = (clk + (CLK_RATE * 7) / 8) / CLK_RATE;
850 case 1: prescaler = 0;
852 case 2: prescaler = 1;
854 case 3: prescaler = 2;
856 case 4: prescaler = 4;
859 case 5: prescaler = 5;
863 dev_dbg(mmc_dev(host->mmc), "PERCLK2 %d MHz -> prescaler %d\n",
866 for (clk = 0; clk < 8; clk++) {
868 x = CLK_RATE / (1 << clk);
873 /* enable controller */
874 reg = readw(host->base + MMC_REG_STR_STP_CLK);
875 writew(reg | STR_STP_CLK_ENABLE,
876 host->base + MMC_REG_STR_STP_CLK);
878 imxmci_stop_clock(host);
879 writew((prescaler << 3) | clk, host->base + MMC_REG_CLK_RATE);
881 * Under my understanding, clock should not be started there, because it would
882 * initiate SDHC sequencer and send last or random command into card
884 /* imxmci_start_clock(host); */
886 dev_dbg(mmc_dev(host->mmc),
887 "MMC_CLK_RATE: 0x%08x\n",
888 readw(host->base + MMC_REG_CLK_RATE));
890 imxmci_stop_clock(host);
894 static int imxmci_get_ro(struct mmc_host *mmc)
896 struct imxmci_host *host = mmc_priv(mmc);
898 if (host->pdata && host->pdata->get_ro)
899 return !!host->pdata->get_ro(mmc_dev(mmc));
901 * Board doesn't support read only detection; let the mmc core
908 static const struct mmc_host_ops imxmci_ops = {
909 .request = imxmci_request,
910 .set_ios = imxmci_set_ios,
911 .get_ro = imxmci_get_ro,
914 static void imxmci_check_status(unsigned long data)
916 struct imxmci_host *host = (struct imxmci_host *)data;
918 if (host->pdata && host->pdata->card_present &&
919 host->pdata->card_present(mmc_dev(host->mmc)) != host->present) {
921 dev_info(mmc_dev(host->mmc), "card %s\n",
922 host->present ? "inserted" : "removed");
924 set_bit(IMXMCI_PEND_CARD_XCHG_b, &host->pending_events);
925 tasklet_schedule(&host->tasklet);
928 if (test_bit(IMXMCI_PEND_WAIT_RESP_b, &host->pending_events) ||
929 test_bit(IMXMCI_PEND_DMA_DATA_b, &host->pending_events)) {
930 atomic_inc(&host->stuck_timeout);
931 if (atomic_read(&host->stuck_timeout) > 4)
932 tasklet_schedule(&host->tasklet);
934 atomic_set(&host->stuck_timeout, 0);
938 mod_timer(&host->timer, jiffies + (HZ>>1));
941 static int imxmci_probe(struct platform_device *pdev)
943 struct mmc_host *mmc;
944 struct imxmci_host *host = NULL;
949 printk(KERN_INFO "i.MX mmc driver\n");
951 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
952 irq = platform_get_irq(pdev, 0);
956 r = request_mem_region(r->start, resource_size(r), pdev->name);
960 mmc = mmc_alloc_host(sizeof(struct imxmci_host), &pdev->dev);
966 mmc->ops = &imxmci_ops;
968 mmc->f_max = CLK_RATE/2;
969 mmc->ocr_avail = MMC_VDD_32_33;
970 mmc->caps = MMC_CAP_4_BIT_DATA;
972 /* MMC core transfer sizes tunable parameters */
973 mmc->max_hw_segs = 64;
974 mmc->max_phys_segs = 64;
975 mmc->max_seg_size = 64*512; /* default PAGE_CACHE_SIZE */
976 mmc->max_req_size = 64*512; /* default PAGE_CACHE_SIZE */
977 mmc->max_blk_size = 2048;
978 mmc->max_blk_count = 65535;
980 host = mmc_priv(mmc);
981 host->base = ioremap(r->start, resource_size(r));
988 host->dma_allocated = 0;
989 host->pdata = pdev->dev.platform_data;
991 dev_warn(&pdev->dev, "No platform data provided!\n");
993 spin_lock_init(&host->lock);
997 host->clk = clk_get(&pdev->dev, "perclk2");
998 if (IS_ERR(host->clk)) {
999 ret = PTR_ERR(host->clk);
1002 clk_enable(host->clk);
1004 imx_gpio_mode(PB8_PF_SD_DAT0);
1005 imx_gpio_mode(PB9_PF_SD_DAT1);
1006 imx_gpio_mode(PB10_PF_SD_DAT2);
1007 /* Configured as GPIO with pull-up to ensure right MCC card mode */
1008 /* Switched to PB11_PF_SD_DAT3 if 4 bit bus is configured */
1009 imx_gpio_mode(GPIO_PORTB | GPIO_IN | GPIO_PUEN | 11);
1010 /* imx_gpio_mode(PB11_PF_SD_DAT3); */
1011 imx_gpio_mode(PB12_PF_SD_CLK);
1012 imx_gpio_mode(PB13_PF_SD_CMD);
1014 imxmci_softreset(host);
1016 rev_no = readw(host->base + MMC_REG_REV_NO);
1017 if (rev_no != 0x390) {
1018 dev_err(mmc_dev(host->mmc), "wrong rev.no. 0x%08x. aborting.\n",
1019 readw(host->base + MMC_REG_REV_NO));
1023 /* recommended in data sheet */
1024 writew(0x2db4, host->base + MMC_REG_READ_TO);
1026 host->imask = IMXMCI_INT_MASK_DEFAULT;
1027 writew(host->imask, host->base + MMC_REG_INT_MASK);
1029 host->dma = imx_dma_request_by_prio(DRIVER_NAME, DMA_PRIO_LOW);
1031 dev_err(mmc_dev(host->mmc), "imx_dma_request_by_prio failed\n");
1035 host->dma_allocated = 1;
1036 imx_dma_setup_handlers(host->dma, imxmci_dma_irq, NULL, host);
1038 tasklet_init(&host->tasklet, imxmci_tasklet_fnc, (unsigned long)host);
1040 host->pending_events=0;
1042 ret = request_irq(host->irq, imxmci_irq, 0, DRIVER_NAME, host);
1046 if (host->pdata && host->pdata->card_present)
1047 host->present = host->pdata->card_present(mmc_dev(mmc));
1048 else /* if there is no way to detect assume that card is present */
1051 init_timer(&host->timer);
1052 host->timer.data = (unsigned long)host;
1053 host->timer.function = imxmci_check_status;
1054 add_timer(&host->timer);
1055 mod_timer(&host->timer, jiffies + (HZ >> 1));
1057 platform_set_drvdata(pdev, mmc);
1065 if (host->dma_allocated) {
1066 imx_dma_free(host->dma);
1067 host->dma_allocated = 0;
1070 clk_disable(host->clk);
1074 iounmap(host->base);
1078 release_mem_region(r->start, resource_size(r));
1082 static int imxmci_remove(struct platform_device *pdev)
1084 struct mmc_host *mmc = platform_get_drvdata(pdev);
1086 platform_set_drvdata(pdev, NULL);
1089 struct imxmci_host *host = mmc_priv(mmc);
1091 tasklet_disable(&host->tasklet);
1093 del_timer_sync(&host->timer);
1094 mmc_remove_host(mmc);
1096 free_irq(host->irq, host);
1097 iounmap(host->base);
1098 if (host->dma_allocated) {
1099 imx_dma_free(host->dma);
1100 host->dma_allocated = 0;
1103 tasklet_kill(&host->tasklet);
1105 clk_disable(host->clk);
1108 release_mem_region(host->res->start, resource_size(host->res));
1116 static int imxmci_suspend(struct platform_device *dev, pm_message_t state)
1118 struct mmc_host *mmc = platform_get_drvdata(dev);
1122 ret = mmc_suspend_host(mmc, state);
1127 static int imxmci_resume(struct platform_device *dev)
1129 struct mmc_host *mmc = platform_get_drvdata(dev);
1130 struct imxmci_host *host;
1134 host = mmc_priv(mmc);
1136 set_bit(IMXMCI_PEND_SET_INIT_b, &host->pending_events);
1137 ret = mmc_resume_host(mmc);
1143 #define imxmci_suspend NULL
1144 #define imxmci_resume NULL
1145 #endif /* CONFIG_PM */
1147 static struct platform_driver imxmci_driver = {
1148 .probe = imxmci_probe,
1149 .remove = imxmci_remove,
1150 .suspend = imxmci_suspend,
1151 .resume = imxmci_resume,
1153 .name = DRIVER_NAME,
1154 .owner = THIS_MODULE,
1158 static int __init imxmci_init(void)
1160 return platform_driver_register(&imxmci_driver);
1163 static void __exit imxmci_exit(void)
1165 platform_driver_unregister(&imxmci_driver);
1168 module_init(imxmci_init);
1169 module_exit(imxmci_exit);
1171 MODULE_DESCRIPTION("i.MX Multimedia Card Interface Driver");
1172 MODULE_AUTHOR("Sascha Hauer, Pengutronix");
1173 MODULE_LICENSE("GPL");
1174 MODULE_ALIAS("platform:imx-mmc");