20 #include <linux/module.h>
36 #include <linux/types.h>
40 #include <asm/sizes.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)
158 static void mxcmci_set_clk_rate(
struct mxcmci_host *
host,
unsigned int clk_ios);
164 if (IS_ERR(host->
vcc)) {
167 host->
mmc->ocr_avail = mmc_regulator_get_ocrmask(host->
vcc);
170 "pdata->ocr_avail will not be used\n");
176 host->
mmc->ocr_avail = host->
pdata->ocr_avail;
182 static inline void mxcmci_set_power(
struct mxcmci_host *host,
183 unsigned char power_mode,
188 mmc_regulator_set_ocr(host->
mmc, host->
vcc, vdd);
190 mmc_regulator_set_ocr(host->
mmc, host->
vcc, 0);
197 static inline int mxcmci_use_dma(
struct mxcmci_host *host)
202 static void mxcmci_softreset(
struct mxcmci_host *host)
213 for (i = 0; i < 8; i++)
218 static int mxcmci_setup_dma(
struct mmc_host *mmc);
222 unsigned int nob = data->
blocks;
224 unsigned int datasize = nob *
blksz;
239 if (!mxcmci_use_dma(host))
259 if (nents != data->
sg_len)
262 host->
desc = dmaengine_prep_slave_sg(host->
dma,
274 dmaengine_submit(host->
desc);
275 dma_async_issue_pending(host->
dma);
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)
300 mxcmci_data_done(host, stat);
334 if (mxcmci_use_dma(host)) {
336 host->
desc->callback = mxcmci_dma_callback;
337 host->
desc->callback_param = host;
347 spin_unlock_irqrestore(&host->
lock, flags);
356 static void mxcmci_finish_request(
struct mxcmci_host *host,
366 spin_unlock_irqrestore(&host->
lock, flags);
375 static int mxcmci_finish_data(
struct mxcmci_host *host,
unsigned int stat)
380 if (mxcmci_use_dma(host))
394 "%s: No CRC -ETIMEDOUT\n", __func__);
398 "%s: -EILSEQ\n", __func__);
403 "%s: read -ETIMEDOUT\n", __func__);
413 data_error = data->
error;
420 static void mxcmci_read_response(
struct mxcmci_host *host,
unsigned int stat)
439 for (i = 0; i < 4; i++) {
442 cmd->
resp[
i] = a << 16 |
b;
448 cmd->
resp[0] = a << 24 | b << 8 | c >> 8;
460 if (stat & STATUS_ERR_MASK)
463 mxcmci_softreset(host);
464 mxcmci_set_clk_rate(host, host->
clock);
479 stat = mxcmci_poll_status(host,
491 stat = mxcmci_poll_status(host,
502 static int mxcmci_push(
struct mxcmci_host *host,
void *_buf,
int bytes)
534 static int mxcmci_transfer_data(
struct mxcmci_host *host)
545 stat = mxcmci_pull(host, sg_virt(sg), sg->
length);
552 stat = mxcmci_push(host, sg_virt(sg), sg->
length);
568 int datastat = mxcmci_transfer_data(host);
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)
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);
621 if (!mxcmci_use_dma(host) && host->
data)
641 spin_unlock_irqrestore(&host->
lock, flags);
643 if (mxcmci_use_dma(host) &&
650 mmc_signal_sdio_irq(host->
mmc);
654 mxcmci_cmd_done(host, stat);
656 if (mxcmci_use_dma(host) &&
659 mxcmci_data_done(host, stat);
672 unsigned int cmdat = host->
cmdat;
684 error = mxcmci_setup_data(host, req->
data);
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;
710 while (prescaler <= 0x800) {
711 for (divider = 1; divider <= 0xF; divider++) {
714 x = (clk_in / (divider + 1));
717 x /= (prescaler * 2);
734 prescaler, divider, clk_in, clk_ios);
737 static int mxcmci_setup_dma(
struct mmc_host *mmc)
750 return dmaengine_slave_config(host->
dma, config);
767 if (mxcmci_use_dma(host) && burstlen != host->
burstlen) {
769 ret = mxcmci_setup_dma(mmc);
772 "failed to config DMA channel. Falling back to PIO\n");
793 mxcmci_set_clk_rate(host, ios->
clock);
802 static irqreturn_t mxcmci_detect_irq(
int irq,
void *data)
812 static int mxcmci_get_ro(
struct mmc_host *mmc)
825 static void mxcmci_enable_sdio_irq(
struct mmc_host *mmc,
int enable)
841 spin_unlock_irqrestore(&host->
lock, flags);
863 if (!imx_dma_is_general_purpose(chan))
871 static void mxcmci_watchdog(
unsigned long data)
879 dmaengine_terminate_all(host->
dma);
881 "%s: read time out (status = 0x%08x)\n",
885 "%s: write time out (status = 0x%08x)\n",
887 mxcmci_softreset(host);
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,
919 if (!iores || irq < 0)
929 goto out_release_mem;
932 mmc->
ops = &mxcmci_ops;
942 host = mmc_priv(mmc);
950 host->
pdata = pdev->
dev.platform_data;
953 mxcmci_init_ocr(host);
955 if (host->
pdata && host->
pdata->dat3_card_detect)
976 clk_prepare_enable(host->
clk_per);
977 clk_prepare_enable(host->
clk_ipg);
979 mxcmci_softreset(host);
982 if (host->
rev_no != 0x400) {
1008 host->
dma->device->dev);
1020 platform_set_drvdata(pdev, mmc);
1023 ret = host->
pdata->init(&pdev->
dev, mxcmci_detect_irq,
1032 host->
watchdog.function = &mxcmci_watchdog;
1043 clk_disable_unprepare(host->
clk_per);
1044 clk_disable_unprepare(host->
clk_ipg);
1056 struct mmc_host *mmc = platform_get_drvdata(pdev);
1059 platform_set_drvdata(pdev,
NULL);
1067 host->
pdata->exit(&pdev->
dev, mmc);
1075 clk_disable_unprepare(host->
clk_per);
1076 clk_disable_unprepare(host->
clk_ipg);
1086 static int mxcmci_suspend(
struct device *
dev)
1094 clk_disable_unprepare(host->
clk_per);
1095 clk_disable_unprepare(host->
clk_ipg);
1100 static int mxcmci_resume(
struct device *dev)
1106 clk_prepare_enable(host->
clk_per);
1107 clk_prepare_enable(host->
clk_ipg);
1114 static const struct dev_pm_ops mxcmci_pm_ops = {
1116 .resume = mxcmci_resume,
1121 .probe = mxcmci_probe,
1122 .remove = mxcmci_remove,
1127 .pm = &mxcmci_pm_ops,