2 * linux/drivers/mmc/host/mxcmmc.c - Freescale i.MX MMCI driver
4 * This is a driver for the SDHC controller found in Freescale MX2/MX3
5 * SoCs. It is basically the same hardware as found on MX1 (imxmmc.c).
6 * Unlike the hardware found on MX1, this hardware just works and does
7 * not need all the quirks found in imxmmc.c, hence the separate driver.
9 * Copyright (C) 2008 Sascha Hauer, Pengutronix <s.hauer@pengutronix.de>
10 * Copyright (C) 2006 Pavel Pisa, PiKRON <ppisa@pikron.com>
12 * derived from pxamci.c by Russell King
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License version 2 as
16 * published by the Free Software Foundation.
20 #include <linux/module.h>
21 #include <linux/init.h>
22 #include <linux/ioport.h>
23 #include <linux/platform_device.h>
24 #include <linux/interrupt.h>
25 #include <linux/irq.h>
26 #include <linux/blkdev.h>
27 #include <linux/dma-mapping.h>
28 #include <linux/mmc/host.h>
29 #include <linux/mmc/card.h>
30 #include <linux/delay.h>
31 #include <linux/clk.h>
33 #include <linux/gpio.h>
34 #include <linux/regulator/consumer.h>
35 #include <linux/dmaengine.h>
36 #include <linux/types.h>
40 #include <asm/sizes.h>
41 #include <linux/platform_data/mmc-mxcmmc.h>
43 #include <linux/platform_data/dma-imx.h>
44 #include <mach/hardware.h>
46 #define DRIVER_NAME "mxc-mmc"
47 #define MXCMCI_TIMEOUT_MS 10000
49 #define MMC_REG_STR_STP_CLK 0x00
50 #define MMC_REG_STATUS 0x04
51 #define MMC_REG_CLK_RATE 0x08
52 #define MMC_REG_CMD_DAT_CONT 0x0C
53 #define MMC_REG_RES_TO 0x10
54 #define MMC_REG_READ_TO 0x14
55 #define MMC_REG_BLK_LEN 0x18
56 #define MMC_REG_NOB 0x1C
57 #define MMC_REG_REV_NO 0x20
58 #define MMC_REG_INT_CNTR 0x24
59 #define MMC_REG_CMD 0x28
60 #define MMC_REG_ARG 0x2C
61 #define MMC_REG_RES_FIFO 0x34
62 #define MMC_REG_BUFFER_ACCESS 0x38
64 #define STR_STP_CLK_RESET (1 << 3)
65 #define STR_STP_CLK_START_CLK (1 << 1)
66 #define STR_STP_CLK_STOP_CLK (1 << 0)
68 #define STATUS_CARD_INSERTION (1 << 31)
69 #define STATUS_CARD_REMOVAL (1 << 30)
70 #define STATUS_YBUF_EMPTY (1 << 29)
71 #define STATUS_XBUF_EMPTY (1 << 28)
72 #define STATUS_YBUF_FULL (1 << 27)
73 #define STATUS_XBUF_FULL (1 << 26)
74 #define STATUS_BUF_UND_RUN (1 << 25)
75 #define STATUS_BUF_OVFL (1 << 24)
76 #define STATUS_SDIO_INT_ACTIVE (1 << 14)
77 #define STATUS_END_CMD_RESP (1 << 13)
78 #define STATUS_WRITE_OP_DONE (1 << 12)
79 #define STATUS_DATA_TRANS_DONE (1 << 11)
80 #define STATUS_READ_OP_DONE (1 << 11)
81 #define STATUS_WR_CRC_ERROR_CODE_MASK (3 << 10)
82 #define STATUS_CARD_BUS_CLK_RUN (1 << 8)
83 #define STATUS_BUF_READ_RDY (1 << 7)
84 #define STATUS_BUF_WRITE_RDY (1 << 6)
85 #define STATUS_RESP_CRC_ERR (1 << 5)
86 #define STATUS_CRC_READ_ERR (1 << 3)
87 #define STATUS_CRC_WRITE_ERR (1 << 2)
88 #define STATUS_TIME_OUT_RESP (1 << 1)
89 #define STATUS_TIME_OUT_READ (1 << 0)
90 #define STATUS_ERR_MASK 0x2f
92 #define CMD_DAT_CONT_CMD_RESP_LONG_OFF (1 << 12)
93 #define CMD_DAT_CONT_STOP_READWAIT (1 << 11)
94 #define CMD_DAT_CONT_START_READWAIT (1 << 10)
95 #define CMD_DAT_CONT_BUS_WIDTH_4 (2 << 8)
96 #define CMD_DAT_CONT_INIT (1 << 7)
97 #define CMD_DAT_CONT_WRITE (1 << 4)
98 #define CMD_DAT_CONT_DATA_ENABLE (1 << 3)
99 #define CMD_DAT_CONT_RESPONSE_48BIT_CRC (1 << 0)
100 #define CMD_DAT_CONT_RESPONSE_136BIT (2 << 0)
101 #define CMD_DAT_CONT_RESPONSE_48BIT (3 << 0)
103 #define INT_SDIO_INT_WKP_EN (1 << 18)
104 #define INT_CARD_INSERTION_WKP_EN (1 << 17)
105 #define INT_CARD_REMOVAL_WKP_EN (1 << 16)
106 #define INT_CARD_INSERTION_EN (1 << 15)
107 #define INT_CARD_REMOVAL_EN (1 << 14)
108 #define INT_SDIO_IRQ_EN (1 << 13)
109 #define INT_DAT0_EN (1 << 12)
110 #define INT_BUF_READ_EN (1 << 4)
111 #define INT_BUF_WRITE_EN (1 << 3)
112 #define INT_END_CMD_RES_EN (1 << 2)
113 #define INT_WRITE_OP_DONE_EN (1 << 1)
114 #define INT_READ_OP_EN (1 << 0)
117 struct mmc_host
*mmc
;
118 struct resource
*res
;
122 struct dma_chan
*dma
;
123 struct dma_async_tx_descriptor
*desc
;
125 int default_irq_mask
;
127 unsigned int power_mode
;
128 struct imxmmc_platform_data
*pdata
;
130 struct mmc_request
*req
;
131 struct mmc_command
*cmd
;
132 struct mmc_data
*data
;
134 unsigned int datasize
;
135 unsigned int dma_dir
;
145 struct work_struct datawork
;
148 struct regulator
*vcc
;
152 struct dma_slave_config dma_slave_config
;
153 struct imx_dma_data dma_data
;
155 struct timer_list watchdog
;
158 static void mxcmci_set_clk_rate(struct mxcmci_host
*host
, unsigned int clk_ios
);
160 static inline void mxcmci_init_ocr(struct mxcmci_host
*host
)
162 host
->vcc
= regulator_get(mmc_dev(host
->mmc
), "vmmc");
164 if (IS_ERR(host
->vcc
)) {
167 host
->mmc
->ocr_avail
= mmc_regulator_get_ocrmask(host
->vcc
);
168 if (host
->pdata
&& host
->pdata
->ocr_avail
)
169 dev_warn(mmc_dev(host
->mmc
),
170 "pdata->ocr_avail will not be used\n");
173 if (host
->vcc
== NULL
) {
174 /* fall-back to platform data */
175 if (host
->pdata
&& host
->pdata
->ocr_avail
)
176 host
->mmc
->ocr_avail
= host
->pdata
->ocr_avail
;
178 host
->mmc
->ocr_avail
= MMC_VDD_32_33
| MMC_VDD_33_34
;
182 static inline void mxcmci_set_power(struct mxcmci_host
*host
,
183 unsigned char power_mode
,
187 if (power_mode
== MMC_POWER_UP
)
188 mmc_regulator_set_ocr(host
->mmc
, host
->vcc
, vdd
);
189 else if (power_mode
== MMC_POWER_OFF
)
190 mmc_regulator_set_ocr(host
->mmc
, host
->vcc
, 0);
193 if (host
->pdata
&& host
->pdata
->setpower
)
194 host
->pdata
->setpower(mmc_dev(host
->mmc
), vdd
);
197 static inline int mxcmci_use_dma(struct mxcmci_host
*host
)
202 static void mxcmci_softreset(struct mxcmci_host
*host
)
206 dev_dbg(mmc_dev(host
->mmc
), "mxcmci_softreset\n");
209 writew(STR_STP_CLK_RESET
, host
->base
+ MMC_REG_STR_STP_CLK
);
210 writew(STR_STP_CLK_RESET
| STR_STP_CLK_START_CLK
,
211 host
->base
+ MMC_REG_STR_STP_CLK
);
213 for (i
= 0; i
< 8; i
++)
214 writew(STR_STP_CLK_START_CLK
, host
->base
+ MMC_REG_STR_STP_CLK
);
216 writew(0xff, host
->base
+ MMC_REG_RES_TO
);
218 static int mxcmci_setup_dma(struct mmc_host
*mmc
);
220 static int mxcmci_setup_data(struct mxcmci_host
*host
, struct mmc_data
*data
)
222 unsigned int nob
= data
->blocks
;
223 unsigned int blksz
= data
->blksz
;
224 unsigned int datasize
= nob
* blksz
;
225 struct scatterlist
*sg
;
226 enum dma_transfer_direction slave_dirn
;
229 if (data
->flags
& MMC_DATA_STREAM
)
233 data
->bytes_xfered
= 0;
235 writew(nob
, host
->base
+ MMC_REG_NOB
);
236 writew(blksz
, host
->base
+ MMC_REG_BLK_LEN
);
237 host
->datasize
= datasize
;
239 if (!mxcmci_use_dma(host
))
242 for_each_sg(data
->sg
, sg
, data
->sg_len
, i
) {
243 if (sg
->offset
& 3 || sg
->length
& 3) {
249 if (data
->flags
& MMC_DATA_READ
) {
250 host
->dma_dir
= DMA_FROM_DEVICE
;
251 slave_dirn
= DMA_DEV_TO_MEM
;
253 host
->dma_dir
= DMA_TO_DEVICE
;
254 slave_dirn
= DMA_MEM_TO_DEV
;
257 nents
= dma_map_sg(host
->dma
->device
->dev
, data
->sg
,
258 data
->sg_len
, host
->dma_dir
);
259 if (nents
!= data
->sg_len
)
262 host
->desc
= dmaengine_prep_slave_sg(host
->dma
,
263 data
->sg
, data
->sg_len
, slave_dirn
,
264 DMA_PREP_INTERRUPT
| DMA_CTRL_ACK
);
267 dma_unmap_sg(host
->dma
->device
->dev
, data
->sg
, data
->sg_len
,
270 return 0; /* Fall back to PIO */
274 dmaengine_submit(host
->desc
);
275 dma_async_issue_pending(host
->dma
);
277 mod_timer(&host
->watchdog
, jiffies
+ msecs_to_jiffies(MXCMCI_TIMEOUT_MS
));
282 static void mxcmci_cmd_done(struct mxcmci_host
*host
, unsigned int stat
);
283 static void mxcmci_data_done(struct mxcmci_host
*host
, unsigned int stat
);
285 static void mxcmci_dma_callback(void *data
)
287 struct mxcmci_host
*host
= data
;
290 del_timer(&host
->watchdog
);
292 stat
= readl(host
->base
+ MMC_REG_STATUS
);
293 writel(stat
& ~STATUS_DATA_TRANS_DONE
, host
->base
+ MMC_REG_STATUS
);
295 dev_dbg(mmc_dev(host
->mmc
), "%s: 0x%08x\n", __func__
, stat
);
297 if (stat
& STATUS_READ_OP_DONE
)
298 writel(STATUS_READ_OP_DONE
, host
->base
+ MMC_REG_STATUS
);
300 mxcmci_data_done(host
, stat
);
303 static int mxcmci_start_cmd(struct mxcmci_host
*host
, struct mmc_command
*cmd
,
306 u32 int_cntr
= host
->default_irq_mask
;
309 WARN_ON(host
->cmd
!= NULL
);
312 switch (mmc_resp_type(cmd
)) {
313 case MMC_RSP_R1
: /* short CRC, OPCODE */
314 case MMC_RSP_R1B
:/* short CRC, OPCODE, BUSY */
315 cmdat
|= CMD_DAT_CONT_RESPONSE_48BIT_CRC
;
317 case MMC_RSP_R2
: /* long 136 bit + CRC */
318 cmdat
|= CMD_DAT_CONT_RESPONSE_136BIT
;
320 case MMC_RSP_R3
: /* short */
321 cmdat
|= CMD_DAT_CONT_RESPONSE_48BIT
;
326 dev_err(mmc_dev(host
->mmc
), "unhandled response type 0x%x\n",
328 cmd
->error
= -EINVAL
;
332 int_cntr
= INT_END_CMD_RES_EN
;
334 if (mxcmci_use_dma(host
)) {
335 if (host
->dma_dir
== DMA_FROM_DEVICE
) {
336 host
->desc
->callback
= mxcmci_dma_callback
;
337 host
->desc
->callback_param
= host
;
339 int_cntr
|= INT_WRITE_OP_DONE_EN
;
343 spin_lock_irqsave(&host
->lock
, flags
);
345 int_cntr
|= INT_SDIO_IRQ_EN
;
346 writel(int_cntr
, host
->base
+ MMC_REG_INT_CNTR
);
347 spin_unlock_irqrestore(&host
->lock
, flags
);
349 writew(cmd
->opcode
, host
->base
+ MMC_REG_CMD
);
350 writel(cmd
->arg
, host
->base
+ MMC_REG_ARG
);
351 writew(cmdat
, host
->base
+ MMC_REG_CMD_DAT_CONT
);
356 static void mxcmci_finish_request(struct mxcmci_host
*host
,
357 struct mmc_request
*req
)
359 u32 int_cntr
= host
->default_irq_mask
;
362 spin_lock_irqsave(&host
->lock
, flags
);
364 int_cntr
|= INT_SDIO_IRQ_EN
;
365 writel(int_cntr
, host
->base
+ MMC_REG_INT_CNTR
);
366 spin_unlock_irqrestore(&host
->lock
, flags
);
372 mmc_request_done(host
->mmc
, req
);
375 static int mxcmci_finish_data(struct mxcmci_host
*host
, unsigned int stat
)
377 struct mmc_data
*data
= host
->data
;
380 if (mxcmci_use_dma(host
))
381 dma_unmap_sg(host
->dma
->device
->dev
, data
->sg
, data
->sg_len
,
384 if (stat
& STATUS_ERR_MASK
) {
385 dev_dbg(mmc_dev(host
->mmc
), "request failed. status: 0x%08x\n",
387 if (stat
& STATUS_CRC_READ_ERR
) {
388 dev_err(mmc_dev(host
->mmc
), "%s: -EILSEQ\n", __func__
);
389 data
->error
= -EILSEQ
;
390 } else if (stat
& STATUS_CRC_WRITE_ERR
) {
391 u32 err_code
= (stat
>> 9) & 0x3;
392 if (err_code
== 2) { /* No CRC response */
393 dev_err(mmc_dev(host
->mmc
),
394 "%s: No CRC -ETIMEDOUT\n", __func__
);
395 data
->error
= -ETIMEDOUT
;
397 dev_err(mmc_dev(host
->mmc
),
398 "%s: -EILSEQ\n", __func__
);
399 data
->error
= -EILSEQ
;
401 } else if (stat
& STATUS_TIME_OUT_READ
) {
402 dev_err(mmc_dev(host
->mmc
),
403 "%s: read -ETIMEDOUT\n", __func__
);
404 data
->error
= -ETIMEDOUT
;
406 dev_err(mmc_dev(host
->mmc
), "%s: -EIO\n", __func__
);
410 data
->bytes_xfered
= host
->datasize
;
413 data_error
= data
->error
;
420 static void mxcmci_read_response(struct mxcmci_host
*host
, unsigned int stat
)
422 struct mmc_command
*cmd
= host
->cmd
;
429 if (stat
& STATUS_TIME_OUT_RESP
) {
430 dev_dbg(mmc_dev(host
->mmc
), "CMD TIMEOUT\n");
431 cmd
->error
= -ETIMEDOUT
;
432 } else if (stat
& STATUS_RESP_CRC_ERR
&& cmd
->flags
& MMC_RSP_CRC
) {
433 dev_dbg(mmc_dev(host
->mmc
), "cmd crc error\n");
434 cmd
->error
= -EILSEQ
;
437 if (cmd
->flags
& MMC_RSP_PRESENT
) {
438 if (cmd
->flags
& MMC_RSP_136
) {
439 for (i
= 0; i
< 4; i
++) {
440 a
= readw(host
->base
+ MMC_REG_RES_FIFO
);
441 b
= readw(host
->base
+ MMC_REG_RES_FIFO
);
442 cmd
->resp
[i
] = a
<< 16 | b
;
445 a
= readw(host
->base
+ MMC_REG_RES_FIFO
);
446 b
= readw(host
->base
+ MMC_REG_RES_FIFO
);
447 c
= readw(host
->base
+ MMC_REG_RES_FIFO
);
448 cmd
->resp
[0] = a
<< 24 | b
<< 8 | c
>> 8;
453 static int mxcmci_poll_status(struct mxcmci_host
*host
, u32 mask
)
456 unsigned long timeout
= jiffies
+ HZ
;
459 stat
= readl(host
->base
+ MMC_REG_STATUS
);
460 if (stat
& STATUS_ERR_MASK
)
462 if (time_after(jiffies
, timeout
)) {
463 mxcmci_softreset(host
);
464 mxcmci_set_clk_rate(host
, host
->clock
);
465 return STATUS_TIME_OUT_READ
;
473 static int mxcmci_pull(struct mxcmci_host
*host
, void *_buf
, int bytes
)
479 stat
= mxcmci_poll_status(host
,
480 STATUS_BUF_READ_RDY
| STATUS_READ_OP_DONE
);
483 *buf
++ = readl(host
->base
+ MMC_REG_BUFFER_ACCESS
);
491 stat
= mxcmci_poll_status(host
,
492 STATUS_BUF_READ_RDY
| STATUS_READ_OP_DONE
);
495 tmp
= readl(host
->base
+ MMC_REG_BUFFER_ACCESS
);
496 memcpy(b
, &tmp
, bytes
);
502 static int mxcmci_push(struct mxcmci_host
*host
, void *_buf
, int bytes
)
508 stat
= mxcmci_poll_status(host
, STATUS_BUF_WRITE_RDY
);
511 writel(*buf
++, host
->base
+ MMC_REG_BUFFER_ACCESS
);
519 stat
= mxcmci_poll_status(host
, STATUS_BUF_WRITE_RDY
);
523 memcpy(&tmp
, b
, bytes
);
524 writel(tmp
, host
->base
+ MMC_REG_BUFFER_ACCESS
);
527 stat
= mxcmci_poll_status(host
, STATUS_BUF_WRITE_RDY
);
534 static int mxcmci_transfer_data(struct mxcmci_host
*host
)
536 struct mmc_data
*data
= host
->req
->data
;
537 struct scatterlist
*sg
;
543 if (data
->flags
& MMC_DATA_READ
) {
544 for_each_sg(data
->sg
, sg
, data
->sg_len
, i
) {
545 stat
= mxcmci_pull(host
, sg_virt(sg
), sg
->length
);
548 host
->datasize
+= sg
->length
;
551 for_each_sg(data
->sg
, sg
, data
->sg_len
, i
) {
552 stat
= mxcmci_push(host
, sg_virt(sg
), sg
->length
);
555 host
->datasize
+= sg
->length
;
557 stat
= mxcmci_poll_status(host
, STATUS_WRITE_OP_DONE
);
564 static void mxcmci_datawork(struct work_struct
*work
)
566 struct mxcmci_host
*host
= container_of(work
, struct mxcmci_host
,
568 int datastat
= mxcmci_transfer_data(host
);
570 writel(STATUS_READ_OP_DONE
| STATUS_WRITE_OP_DONE
,
571 host
->base
+ MMC_REG_STATUS
);
572 mxcmci_finish_data(host
, datastat
);
574 if (host
->req
->stop
) {
575 if (mxcmci_start_cmd(host
, host
->req
->stop
, 0)) {
576 mxcmci_finish_request(host
, host
->req
);
580 mxcmci_finish_request(host
, host
->req
);
584 static void mxcmci_data_done(struct mxcmci_host
*host
, unsigned int stat
)
586 struct mmc_data
*data
= host
->data
;
592 data_error
= mxcmci_finish_data(host
, stat
);
594 mxcmci_read_response(host
, stat
);
597 if (host
->req
->stop
) {
598 if (mxcmci_start_cmd(host
, host
->req
->stop
, 0)) {
599 mxcmci_finish_request(host
, host
->req
);
603 mxcmci_finish_request(host
, host
->req
);
607 static void mxcmci_cmd_done(struct mxcmci_host
*host
, unsigned int stat
)
609 mxcmci_read_response(host
, stat
);
612 if (!host
->data
&& host
->req
) {
613 mxcmci_finish_request(host
, host
->req
);
617 /* For the DMA case the DMA engine handles the data transfer
618 * automatically. For non DMA we have to do it ourselves.
619 * Don't do it in interrupt context though.
621 if (!mxcmci_use_dma(host
) && host
->data
)
622 schedule_work(&host
->datawork
);
626 static irqreturn_t
mxcmci_irq(int irq
, void *devid
)
628 struct mxcmci_host
*host
= devid
;
633 stat
= readl(host
->base
+ MMC_REG_STATUS
);
634 writel(stat
& ~(STATUS_SDIO_INT_ACTIVE
| STATUS_DATA_TRANS_DONE
|
635 STATUS_WRITE_OP_DONE
), host
->base
+ MMC_REG_STATUS
);
637 dev_dbg(mmc_dev(host
->mmc
), "%s: 0x%08x\n", __func__
, stat
);
639 spin_lock_irqsave(&host
->lock
, flags
);
640 sdio_irq
= (stat
& STATUS_SDIO_INT_ACTIVE
) && host
->use_sdio
;
641 spin_unlock_irqrestore(&host
->lock
, flags
);
643 if (mxcmci_use_dma(host
) &&
644 (stat
& (STATUS_READ_OP_DONE
| STATUS_WRITE_OP_DONE
)))
645 writel(STATUS_READ_OP_DONE
| STATUS_WRITE_OP_DONE
,
646 host
->base
+ MMC_REG_STATUS
);
649 writel(STATUS_SDIO_INT_ACTIVE
, host
->base
+ MMC_REG_STATUS
);
650 mmc_signal_sdio_irq(host
->mmc
);
653 if (stat
& STATUS_END_CMD_RESP
)
654 mxcmci_cmd_done(host
, stat
);
656 if (mxcmci_use_dma(host
) &&
657 (stat
& (STATUS_DATA_TRANS_DONE
| STATUS_WRITE_OP_DONE
))) {
658 del_timer(&host
->watchdog
);
659 mxcmci_data_done(host
, stat
);
662 if (host
->default_irq_mask
&&
663 (stat
& (STATUS_CARD_INSERTION
| STATUS_CARD_REMOVAL
)))
664 mmc_detect_change(host
->mmc
, msecs_to_jiffies(200));
669 static void mxcmci_request(struct mmc_host
*mmc
, struct mmc_request
*req
)
671 struct mxcmci_host
*host
= mmc_priv(mmc
);
672 unsigned int cmdat
= host
->cmdat
;
675 WARN_ON(host
->req
!= NULL
);
678 host
->cmdat
&= ~CMD_DAT_CONT_INIT
;
684 error
= mxcmci_setup_data(host
, req
->data
);
686 req
->cmd
->error
= error
;
691 cmdat
|= CMD_DAT_CONT_DATA_ENABLE
;
693 if (req
->data
->flags
& MMC_DATA_WRITE
)
694 cmdat
|= CMD_DAT_CONT_WRITE
;
697 error
= mxcmci_start_cmd(host
, req
->cmd
, cmdat
);
701 mxcmci_finish_request(host
, req
);
704 static void mxcmci_set_clk_rate(struct mxcmci_host
*host
, unsigned int clk_ios
)
706 unsigned int divider
;
708 unsigned int clk_in
= clk_get_rate(host
->clk_per
);
710 while (prescaler
<= 0x800) {
711 for (divider
= 1; divider
<= 0xF; divider
++) {
714 x
= (clk_in
/ (divider
+ 1));
717 x
/= (prescaler
* 2);
731 writew((prescaler
<< 4) | divider
, host
->base
+ MMC_REG_CLK_RATE
);
733 dev_dbg(mmc_dev(host
->mmc
), "scaler: %d divider: %d in: %d out: %d\n",
734 prescaler
, divider
, clk_in
, clk_ios
);
737 static int mxcmci_setup_dma(struct mmc_host
*mmc
)
739 struct mxcmci_host
*host
= mmc_priv(mmc
);
740 struct dma_slave_config
*config
= &host
->dma_slave_config
;
742 config
->dst_addr
= host
->res
->start
+ MMC_REG_BUFFER_ACCESS
;
743 config
->src_addr
= host
->res
->start
+ MMC_REG_BUFFER_ACCESS
;
744 config
->dst_addr_width
= 4;
745 config
->src_addr_width
= 4;
746 config
->dst_maxburst
= host
->burstlen
;
747 config
->src_maxburst
= host
->burstlen
;
748 config
->device_fc
= false;
750 return dmaengine_slave_config(host
->dma
, config
);
753 static void mxcmci_set_ios(struct mmc_host
*mmc
, struct mmc_ios
*ios
)
755 struct mxcmci_host
*host
= mmc_priv(mmc
);
759 * use burstlen of 64 (16 words) in 4 bit mode (--> reg value 0)
760 * use burstlen of 16 (4 words) in 1 bit mode (--> reg value 16)
762 if (ios
->bus_width
== MMC_BUS_WIDTH_4
)
767 if (mxcmci_use_dma(host
) && burstlen
!= host
->burstlen
) {
768 host
->burstlen
= burstlen
;
769 ret
= mxcmci_setup_dma(mmc
);
771 dev_err(mmc_dev(host
->mmc
),
772 "failed to config DMA channel. Falling back to PIO\n");
773 dma_release_channel(host
->dma
);
779 if (ios
->bus_width
== MMC_BUS_WIDTH_4
)
780 host
->cmdat
|= CMD_DAT_CONT_BUS_WIDTH_4
;
782 host
->cmdat
&= ~CMD_DAT_CONT_BUS_WIDTH_4
;
784 if (host
->power_mode
!= ios
->power_mode
) {
785 mxcmci_set_power(host
, ios
->power_mode
, ios
->vdd
);
786 host
->power_mode
= ios
->power_mode
;
788 if (ios
->power_mode
== MMC_POWER_ON
)
789 host
->cmdat
|= CMD_DAT_CONT_INIT
;
793 mxcmci_set_clk_rate(host
, ios
->clock
);
794 writew(STR_STP_CLK_START_CLK
, host
->base
+ MMC_REG_STR_STP_CLK
);
796 writew(STR_STP_CLK_STOP_CLK
, host
->base
+ MMC_REG_STR_STP_CLK
);
799 host
->clock
= ios
->clock
;
802 static irqreturn_t
mxcmci_detect_irq(int irq
, void *data
)
804 struct mmc_host
*mmc
= data
;
806 dev_dbg(mmc_dev(mmc
), "%s\n", __func__
);
808 mmc_detect_change(mmc
, msecs_to_jiffies(250));
812 static int mxcmci_get_ro(struct mmc_host
*mmc
)
814 struct mxcmci_host
*host
= mmc_priv(mmc
);
816 if (host
->pdata
&& host
->pdata
->get_ro
)
817 return !!host
->pdata
->get_ro(mmc_dev(mmc
));
819 * Board doesn't support read only detection; let the mmc core
825 static void mxcmci_enable_sdio_irq(struct mmc_host
*mmc
, int enable
)
827 struct mxcmci_host
*host
= mmc_priv(mmc
);
831 spin_lock_irqsave(&host
->lock
, flags
);
832 host
->use_sdio
= enable
;
833 int_cntr
= readl(host
->base
+ MMC_REG_INT_CNTR
);
836 int_cntr
|= INT_SDIO_IRQ_EN
;
838 int_cntr
&= ~INT_SDIO_IRQ_EN
;
840 writel(int_cntr
, host
->base
+ MMC_REG_INT_CNTR
);
841 spin_unlock_irqrestore(&host
->lock
, flags
);
844 static void mxcmci_init_card(struct mmc_host
*host
, struct mmc_card
*card
)
847 * MX3 SoCs have a silicon bug which corrupts CRC calculation of
848 * multi-block transfers when connected SDIO peripheral doesn't
849 * drive the BUSY line as required by the specs.
850 * One way to prevent this is to only allow 1-bit transfers.
853 if (cpu_is_mx3() && card
->type
== MMC_TYPE_SDIO
)
854 host
->caps
&= ~MMC_CAP_4_BIT_DATA
;
856 host
->caps
|= MMC_CAP_4_BIT_DATA
;
859 static bool filter(struct dma_chan
*chan
, void *param
)
861 struct mxcmci_host
*host
= param
;
863 if (!imx_dma_is_general_purpose(chan
))
866 chan
->private = &host
->dma_data
;
871 static void mxcmci_watchdog(unsigned long data
)
873 struct mmc_host
*mmc
= (struct mmc_host
*)data
;
874 struct mxcmci_host
*host
= mmc_priv(mmc
);
875 struct mmc_request
*req
= host
->req
;
876 unsigned int stat
= readl(host
->base
+ MMC_REG_STATUS
);
878 if (host
->dma_dir
== DMA_FROM_DEVICE
) {
879 dmaengine_terminate_all(host
->dma
);
880 dev_err(mmc_dev(host
->mmc
),
881 "%s: read time out (status = 0x%08x)\n",
884 dev_err(mmc_dev(host
->mmc
),
885 "%s: write time out (status = 0x%08x)\n",
887 mxcmci_softreset(host
);
890 /* Mark transfer as erroneus and inform the upper layers */
892 host
->data
->error
= -ETIMEDOUT
;
896 mmc_request_done(host
->mmc
, req
);
899 static const struct mmc_host_ops mxcmci_ops
= {
900 .request
= mxcmci_request
,
901 .set_ios
= mxcmci_set_ios
,
902 .get_ro
= mxcmci_get_ro
,
903 .enable_sdio_irq
= mxcmci_enable_sdio_irq
,
904 .init_card
= mxcmci_init_card
,
907 static int mxcmci_probe(struct platform_device
*pdev
)
909 struct mmc_host
*mmc
;
910 struct mxcmci_host
*host
= NULL
;
911 struct resource
*iores
, *r
;
915 pr_info("i.MX SDHC driver\n");
917 iores
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
918 irq
= platform_get_irq(pdev
, 0);
919 if (!iores
|| irq
< 0)
922 r
= request_mem_region(iores
->start
, resource_size(iores
), pdev
->name
);
926 mmc
= mmc_alloc_host(sizeof(struct mxcmci_host
), &pdev
->dev
);
929 goto out_release_mem
;
932 mmc
->ops
= &mxcmci_ops
;
933 mmc
->caps
= MMC_CAP_4_BIT_DATA
| MMC_CAP_SDIO_IRQ
;
935 /* MMC core transfer sizes tunable parameters */
937 mmc
->max_blk_size
= 2048;
938 mmc
->max_blk_count
= 65535;
939 mmc
->max_req_size
= mmc
->max_blk_size
* mmc
->max_blk_count
;
940 mmc
->max_seg_size
= mmc
->max_req_size
;
942 host
= mmc_priv(mmc
);
943 host
->base
= ioremap(r
->start
, resource_size(r
));
950 host
->pdata
= pdev
->dev
.platform_data
;
951 spin_lock_init(&host
->lock
);
953 mxcmci_init_ocr(host
);
955 if (host
->pdata
&& host
->pdata
->dat3_card_detect
)
956 host
->default_irq_mask
=
957 INT_CARD_INSERTION_EN
| INT_CARD_REMOVAL_EN
;
959 host
->default_irq_mask
= 0;
964 host
->clk_ipg
= devm_clk_get(&pdev
->dev
, "ipg");
965 if (IS_ERR(host
->clk_ipg
)) {
966 ret
= PTR_ERR(host
->clk_ipg
);
970 host
->clk_per
= devm_clk_get(&pdev
->dev
, "per");
971 if (IS_ERR(host
->clk_per
)) {
972 ret
= PTR_ERR(host
->clk_per
);
976 clk_prepare_enable(host
->clk_per
);
977 clk_prepare_enable(host
->clk_ipg
);
979 mxcmci_softreset(host
);
981 host
->rev_no
= readw(host
->base
+ MMC_REG_REV_NO
);
982 if (host
->rev_no
!= 0x400) {
984 dev_err(mmc_dev(host
->mmc
), "wrong rev.no. 0x%08x. aborting.\n",
989 mmc
->f_min
= clk_get_rate(host
->clk_per
) >> 16;
990 mmc
->f_max
= clk_get_rate(host
->clk_per
) >> 1;
992 /* recommended in data sheet */
993 writew(0x2db4, host
->base
+ MMC_REG_READ_TO
);
995 writel(host
->default_irq_mask
, host
->base
+ MMC_REG_INT_CNTR
);
997 r
= platform_get_resource(pdev
, IORESOURCE_DMA
, 0);
999 host
->dmareq
= r
->start
;
1000 host
->dma_data
.peripheral_type
= IMX_DMATYPE_SDHC
;
1001 host
->dma_data
.priority
= DMA_PRIO_LOW
;
1002 host
->dma_data
.dma_request
= host
->dmareq
;
1004 dma_cap_set(DMA_SLAVE
, mask
);
1005 host
->dma
= dma_request_channel(mask
, filter
, host
);
1007 mmc
->max_seg_size
= dma_get_max_seg_size(
1008 host
->dma
->device
->dev
);
1012 dev_info(mmc_dev(host
->mmc
), "dma not available. Using PIO\n");
1014 INIT_WORK(&host
->datawork
, mxcmci_datawork
);
1016 ret
= request_irq(host
->irq
, mxcmci_irq
, 0, DRIVER_NAME
, host
);
1020 platform_set_drvdata(pdev
, mmc
);
1022 if (host
->pdata
&& host
->pdata
->init
) {
1023 ret
= host
->pdata
->init(&pdev
->dev
, mxcmci_detect_irq
,
1031 init_timer(&host
->watchdog
);
1032 host
->watchdog
.function
= &mxcmci_watchdog
;
1033 host
->watchdog
.data
= (unsigned long)mmc
;
1038 free_irq(host
->irq
, host
);
1041 dma_release_channel(host
->dma
);
1043 clk_disable_unprepare(host
->clk_per
);
1044 clk_disable_unprepare(host
->clk_ipg
);
1046 iounmap(host
->base
);
1050 release_mem_region(iores
->start
, resource_size(iores
));
1054 static int mxcmci_remove(struct platform_device
*pdev
)
1056 struct mmc_host
*mmc
= platform_get_drvdata(pdev
);
1057 struct mxcmci_host
*host
= mmc_priv(mmc
);
1059 platform_set_drvdata(pdev
, NULL
);
1061 mmc_remove_host(mmc
);
1064 regulator_put(host
->vcc
);
1066 if (host
->pdata
&& host
->pdata
->exit
)
1067 host
->pdata
->exit(&pdev
->dev
, mmc
);
1069 free_irq(host
->irq
, host
);
1070 iounmap(host
->base
);
1073 dma_release_channel(host
->dma
);
1075 clk_disable_unprepare(host
->clk_per
);
1076 clk_disable_unprepare(host
->clk_ipg
);
1078 release_mem_region(host
->res
->start
, resource_size(host
->res
));
1086 static int mxcmci_suspend(struct device
*dev
)
1088 struct mmc_host
*mmc
= dev_get_drvdata(dev
);
1089 struct mxcmci_host
*host
= mmc_priv(mmc
);
1093 ret
= mmc_suspend_host(mmc
);
1094 clk_disable_unprepare(host
->clk_per
);
1095 clk_disable_unprepare(host
->clk_ipg
);
1100 static int mxcmci_resume(struct device
*dev
)
1102 struct mmc_host
*mmc
= dev_get_drvdata(dev
);
1103 struct mxcmci_host
*host
= mmc_priv(mmc
);
1106 clk_prepare_enable(host
->clk_per
);
1107 clk_prepare_enable(host
->clk_ipg
);
1109 ret
= mmc_resume_host(mmc
);
1114 static const struct dev_pm_ops mxcmci_pm_ops
= {
1115 .suspend
= mxcmci_suspend
,
1116 .resume
= mxcmci_resume
,
1120 static struct platform_driver mxcmci_driver
= {
1121 .probe
= mxcmci_probe
,
1122 .remove
= mxcmci_remove
,
1124 .name
= DRIVER_NAME
,
1125 .owner
= THIS_MODULE
,
1127 .pm
= &mxcmci_pm_ops
,
1132 module_platform_driver(mxcmci_driver
);
1134 MODULE_DESCRIPTION("i.MX Multimedia Card Interface Driver");
1135 MODULE_AUTHOR("Sascha Hauer, Pengutronix");
1136 MODULE_LICENSE("GPL");
1137 MODULE_ALIAS("platform:mxc-mmc");