1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * davinci_mmc.c - TI DaVinci MMC/SD/SDIO driver
5 * Copyright (C) 2006 Texas Instruments.
6 * Original author: Purushotam Kumar
7 * Copyright (C) 2009 David Brownell
10 #include <linux/module.h>
11 #include <linux/ioport.h>
12 #include <linux/platform_device.h>
13 #include <linux/clk.h>
14 #include <linux/err.h>
15 #include <linux/cpufreq.h>
16 #include <linux/mmc/host.h>
18 #include <linux/irq.h>
19 #include <linux/delay.h>
20 #include <linux/dmaengine.h>
21 #include <linux/dma-mapping.h>
22 #include <linux/mmc/mmc.h>
24 #include <linux/mmc/slot-gpio.h>
25 #include <linux/interrupt.h>
27 #include <linux/platform_data/mmc-davinci.h>
30 * Register Definitions
32 #define DAVINCI_MMCCTL 0x00 /* Control Register */
33 #define DAVINCI_MMCCLK 0x04 /* Memory Clock Control Register */
34 #define DAVINCI_MMCST0 0x08 /* Status Register 0 */
35 #define DAVINCI_MMCST1 0x0C /* Status Register 1 */
36 #define DAVINCI_MMCIM 0x10 /* Interrupt Mask Register */
37 #define DAVINCI_MMCTOR 0x14 /* Response Time-Out Register */
38 #define DAVINCI_MMCTOD 0x18 /* Data Read Time-Out Register */
39 #define DAVINCI_MMCBLEN 0x1C /* Block Length Register */
40 #define DAVINCI_MMCNBLK 0x20 /* Number of Blocks Register */
41 #define DAVINCI_MMCNBLC 0x24 /* Number of Blocks Counter Register */
42 #define DAVINCI_MMCDRR 0x28 /* Data Receive Register */
43 #define DAVINCI_MMCDXR 0x2C /* Data Transmit Register */
44 #define DAVINCI_MMCCMD 0x30 /* Command Register */
45 #define DAVINCI_MMCARGHL 0x34 /* Argument Register */
46 #define DAVINCI_MMCRSP01 0x38 /* Response Register 0 and 1 */
47 #define DAVINCI_MMCRSP23 0x3C /* Response Register 0 and 1 */
48 #define DAVINCI_MMCRSP45 0x40 /* Response Register 0 and 1 */
49 #define DAVINCI_MMCRSP67 0x44 /* Response Register 0 and 1 */
50 #define DAVINCI_MMCDRSP 0x48 /* Data Response Register */
51 #define DAVINCI_MMCETOK 0x4C
52 #define DAVINCI_MMCCIDX 0x50 /* Command Index Register */
53 #define DAVINCI_MMCCKC 0x54
54 #define DAVINCI_MMCTORC 0x58
55 #define DAVINCI_MMCTODC 0x5C
56 #define DAVINCI_MMCBLNC 0x60
57 #define DAVINCI_SDIOCTL 0x64
58 #define DAVINCI_SDIOST0 0x68
59 #define DAVINCI_SDIOIEN 0x6C
60 #define DAVINCI_SDIOIST 0x70
61 #define DAVINCI_MMCFIFOCTL 0x74 /* FIFO Control Register */
63 /* DAVINCI_MMCCTL definitions */
64 #define MMCCTL_DATRST (1 << 0)
65 #define MMCCTL_CMDRST (1 << 1)
66 #define MMCCTL_WIDTH_8_BIT (1 << 8)
67 #define MMCCTL_WIDTH_4_BIT (1 << 2)
68 #define MMCCTL_DATEG_DISABLED (0 << 6)
69 #define MMCCTL_DATEG_RISING (1 << 6)
70 #define MMCCTL_DATEG_FALLING (2 << 6)
71 #define MMCCTL_DATEG_BOTH (3 << 6)
72 #define MMCCTL_PERMDR_LE (0 << 9)
73 #define MMCCTL_PERMDR_BE (1 << 9)
74 #define MMCCTL_PERMDX_LE (0 << 10)
75 #define MMCCTL_PERMDX_BE (1 << 10)
77 /* DAVINCI_MMCCLK definitions */
78 #define MMCCLK_CLKEN (1 << 8)
79 #define MMCCLK_CLKRT_MASK (0xFF << 0)
81 /* IRQ bit definitions, for DAVINCI_MMCST0 and DAVINCI_MMCIM */
82 #define MMCST0_DATDNE BIT(0) /* data done */
83 #define MMCST0_BSYDNE BIT(1) /* busy done */
84 #define MMCST0_RSPDNE BIT(2) /* command done */
85 #define MMCST0_TOUTRD BIT(3) /* data read timeout */
86 #define MMCST0_TOUTRS BIT(4) /* command response timeout */
87 #define MMCST0_CRCWR BIT(5) /* data write CRC error */
88 #define MMCST0_CRCRD BIT(6) /* data read CRC error */
89 #define MMCST0_CRCRS BIT(7) /* command response CRC error */
90 #define MMCST0_DXRDY BIT(9) /* data transmit ready (fifo empty) */
91 #define MMCST0_DRRDY BIT(10) /* data receive ready (data in fifo)*/
92 #define MMCST0_DATED BIT(11) /* DAT3 edge detect */
93 #define MMCST0_TRNDNE BIT(12) /* transfer done */
95 /* DAVINCI_MMCST1 definitions */
96 #define MMCST1_BUSY (1 << 0)
98 /* DAVINCI_MMCCMD definitions */
99 #define MMCCMD_CMD_MASK (0x3F << 0)
100 #define MMCCMD_PPLEN (1 << 7)
101 #define MMCCMD_BSYEXP (1 << 8)
102 #define MMCCMD_RSPFMT_MASK (3 << 9)
103 #define MMCCMD_RSPFMT_NONE (0 << 9)
104 #define MMCCMD_RSPFMT_R1456 (1 << 9)
105 #define MMCCMD_RSPFMT_R2 (2 << 9)
106 #define MMCCMD_RSPFMT_R3 (3 << 9)
107 #define MMCCMD_DTRW (1 << 11)
108 #define MMCCMD_STRMTP (1 << 12)
109 #define MMCCMD_WDATX (1 << 13)
110 #define MMCCMD_INITCK (1 << 14)
111 #define MMCCMD_DCLR (1 << 15)
112 #define MMCCMD_DMATRIG (1 << 16)
114 /* DAVINCI_MMCFIFOCTL definitions */
115 #define MMCFIFOCTL_FIFORST (1 << 0)
116 #define MMCFIFOCTL_FIFODIR_WR (1 << 1)
117 #define MMCFIFOCTL_FIFODIR_RD (0 << 1)
118 #define MMCFIFOCTL_FIFOLEV (1 << 2) /* 0 = 128 bits, 1 = 256 bits */
119 #define MMCFIFOCTL_ACCWD_4 (0 << 3) /* access width of 4 bytes */
120 #define MMCFIFOCTL_ACCWD_3 (1 << 3) /* access width of 3 bytes */
121 #define MMCFIFOCTL_ACCWD_2 (2 << 3) /* access width of 2 bytes */
122 #define MMCFIFOCTL_ACCWD_1 (3 << 3) /* access width of 1 byte */
124 /* DAVINCI_SDIOST0 definitions */
125 #define SDIOST0_DAT1_HI BIT(0)
127 /* DAVINCI_SDIOIEN definitions */
128 #define SDIOIEN_IOINTEN BIT(0)
130 /* DAVINCI_SDIOIST definitions */
131 #define SDIOIST_IOINT BIT(0)
133 /* MMCSD Init clock in Hz in opendrain mode */
134 #define MMCSD_INIT_CLOCK 200000
137 * One scatterlist dma "segment" is at most MAX_CCNT rw_threshold units,
138 * and we handle up to MAX_NR_SG segments. MMC_BLOCK_BOUNCE kicks in only
139 * for drivers with max_segs == 1, making the segments bigger (64KB)
140 * than the page or two that's otherwise typical. nr_sg (passed from
141 * platform data) == 16 gives at least the same throughput boost, using
142 * EDMA transfer linkage instead of spending CPU time copying pages.
144 #define MAX_CCNT ((1 << 16) - 1)
148 static unsigned rw_threshold = 32;
149 module_param(rw_threshold, uint, S_IRUGO);
150 MODULE_PARM_DESC(rw_threshold,
151 "Read/Write threshold. Default = 32");
153 static unsigned poll_threshold = 128;
154 module_param(poll_threshold, uint, S_IRUGO);
155 MODULE_PARM_DESC(poll_threshold,
156 "Polling transaction size threshold. Default = 128");
158 static unsigned poll_loopcount = 32;
159 module_param(poll_loopcount, uint, S_IRUGO);
160 MODULE_PARM_DESC(poll_loopcount,
161 "Maximum polling loop count. Default = 32");
163 static unsigned use_dma = 1;
164 module_param(use_dma, uint, 0);
165 MODULE_PARM_DESC(use_dma, "Whether to use DMA or not. Default = 1");
167 struct mmc_davinci_host {
168 struct mmc_command *cmd;
169 struct mmc_data *data;
170 struct mmc_host *mmc;
172 unsigned int mmc_input_clk;
174 struct resource *mem_res;
175 int mmc_irq, sdio_irq;
176 unsigned char bus_mode;
178 #define DAVINCI_MMC_DATADIR_NONE 0
179 #define DAVINCI_MMC_DATADIR_READ 1
180 #define DAVINCI_MMC_DATADIR_WRITE 2
181 unsigned char data_dir;
185 struct dma_chan *dma_tx;
186 struct dma_chan *dma_rx;
192 /* For PIO we walk scatterlists one segment at a time. */
193 struct sg_mapping_iter sg_miter;
196 /* Version of the MMC/SD controller */
198 /* for ns in one cycle calculation */
199 unsigned ns_in_one_cycle;
200 /* Number of sg segments */
202 #ifdef CONFIG_CPU_FREQ
203 struct notifier_block freq_transition;
207 static irqreturn_t mmc_davinci_irq(int irq, void *dev_id);
210 static void davinci_fifo_data_trans(struct mmc_davinci_host *host,
213 struct sg_mapping_iter *sgm = &host->sg_miter;
218 * By adjusting sgm->consumed this will give a pointer to the
219 * current index into the sgm.
221 if (!sg_miter_next(sgm)) {
222 dev_err(mmc_dev(host->mmc), "ran out of sglist prematurely\n");
227 /* NOTE: we never transfer more than rw_threshold bytes
228 * to/from the fifo here; there's no I/O overlap.
229 * This also assumes that access width( i.e. ACCWD) is 4 bytes
231 if (host->data_dir == DAVINCI_MMC_DATADIR_WRITE) {
232 for (i = 0; i < (n >> 2); i++) {
233 writel(*((u32 *)p), host->base + DAVINCI_MMCDXR);
237 iowrite8_rep(host->base + DAVINCI_MMCDXR, p, (n & 3));
241 for (i = 0; i < (n >> 2); i++) {
242 *((u32 *)p) = readl(host->base + DAVINCI_MMCDRR);
246 ioread8_rep(host->base + DAVINCI_MMCDRR, p, (n & 3));
252 host->bytes_left -= n;
255 static void mmc_davinci_start_command(struct mmc_davinci_host *host,
256 struct mmc_command *cmd)
261 dev_dbg(mmc_dev(host->mmc), "CMD%d, arg 0x%08x%s\n",
262 cmd->opcode, cmd->arg,
264 switch (mmc_resp_type(cmd)) {
266 s = ", R1/R5/R6/R7 response";
269 s = ", R1b response";
275 s = ", R3/R4 response";
278 s = ", (R? response)";
283 switch (mmc_resp_type(cmd)) {
285 /* There's some spec confusion about when R1B is
286 * allowed, but if the card doesn't issue a BUSY
287 * then it's harmless for us to allow it.
289 cmd_reg |= MMCCMD_BSYEXP;
291 case MMC_RSP_R1: /* 48 bits, CRC */
292 cmd_reg |= MMCCMD_RSPFMT_R1456;
294 case MMC_RSP_R2: /* 136 bits, CRC */
295 cmd_reg |= MMCCMD_RSPFMT_R2;
297 case MMC_RSP_R3: /* 48 bits, no CRC */
298 cmd_reg |= MMCCMD_RSPFMT_R3;
301 cmd_reg |= MMCCMD_RSPFMT_NONE;
302 dev_dbg(mmc_dev(host->mmc), "unknown resp_type %04x\n",
307 /* Set command index */
308 cmd_reg |= cmd->opcode;
310 /* Enable EDMA transfer triggers */
312 cmd_reg |= MMCCMD_DMATRIG;
314 if (host->version == MMC_CTLR_VERSION_2 && host->data != NULL &&
315 host->data_dir == DAVINCI_MMC_DATADIR_READ)
316 cmd_reg |= MMCCMD_DMATRIG;
318 /* Setting whether command involves data transfer or not */
320 cmd_reg |= MMCCMD_WDATX;
322 /* Setting whether data read or write */
323 if (host->data_dir == DAVINCI_MMC_DATADIR_WRITE)
324 cmd_reg |= MMCCMD_DTRW;
326 if (host->bus_mode == MMC_BUSMODE_PUSHPULL)
327 cmd_reg |= MMCCMD_PPLEN;
329 /* set Command timeout */
330 writel(0x1FFF, host->base + DAVINCI_MMCTOR);
332 /* Enable interrupt (calculate here, defer until FIFO is stuffed). */
333 im_val = MMCST0_RSPDNE | MMCST0_CRCRS | MMCST0_TOUTRS;
334 if (host->data_dir == DAVINCI_MMC_DATADIR_WRITE) {
335 im_val |= MMCST0_DATDNE | MMCST0_CRCWR;
338 im_val |= MMCST0_DXRDY;
339 } else if (host->data_dir == DAVINCI_MMC_DATADIR_READ) {
340 im_val |= MMCST0_DATDNE | MMCST0_CRCRD | MMCST0_TOUTRD;
343 im_val |= MMCST0_DRRDY;
347 * Before non-DMA WRITE commands the controller needs priming:
348 * FIFO should be populated with 32 bytes i.e. whatever is the FIFO size
350 if (!host->do_dma && (host->data_dir == DAVINCI_MMC_DATADIR_WRITE))
351 davinci_fifo_data_trans(host, rw_threshold);
353 writel(cmd->arg, host->base + DAVINCI_MMCARGHL);
354 writel(cmd_reg, host->base + DAVINCI_MMCCMD);
356 host->active_request = true;
358 if (!host->do_dma && host->bytes_left <= poll_threshold) {
359 u32 count = poll_loopcount;
361 while (host->active_request && count--) {
362 mmc_davinci_irq(0, host);
367 if (host->active_request)
368 writel(im_val, host->base + DAVINCI_MMCIM);
371 /*----------------------------------------------------------------------*/
373 /* DMA infrastructure */
375 static void davinci_abort_dma(struct mmc_davinci_host *host)
377 struct dma_chan *sync_dev;
379 if (host->data_dir == DAVINCI_MMC_DATADIR_READ)
380 sync_dev = host->dma_rx;
382 sync_dev = host->dma_tx;
384 dmaengine_terminate_all(sync_dev);
387 static int mmc_davinci_send_dma_request(struct mmc_davinci_host *host,
388 struct mmc_data *data)
390 struct dma_chan *chan;
391 struct dma_async_tx_descriptor *desc;
394 if (host->data_dir == DAVINCI_MMC_DATADIR_WRITE) {
395 struct dma_slave_config dma_tx_conf = {
396 .direction = DMA_MEM_TO_DEV,
397 .dst_addr = host->mem_res->start + DAVINCI_MMCDXR,
398 .dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES,
400 rw_threshold / DMA_SLAVE_BUSWIDTH_4_BYTES,
403 dmaengine_slave_config(host->dma_tx, &dma_tx_conf);
405 desc = dmaengine_prep_slave_sg(host->dma_tx,
409 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
411 dev_dbg(mmc_dev(host->mmc),
412 "failed to allocate DMA TX descriptor");
417 struct dma_slave_config dma_rx_conf = {
418 .direction = DMA_DEV_TO_MEM,
419 .src_addr = host->mem_res->start + DAVINCI_MMCDRR,
420 .src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES,
422 rw_threshold / DMA_SLAVE_BUSWIDTH_4_BYTES,
425 dmaengine_slave_config(host->dma_rx, &dma_rx_conf);
427 desc = dmaengine_prep_slave_sg(host->dma_rx,
431 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
433 dev_dbg(mmc_dev(host->mmc),
434 "failed to allocate DMA RX descriptor");
440 dmaengine_submit(desc);
441 dma_async_issue_pending(chan);
447 static int mmc_davinci_start_dma_transfer(struct mmc_davinci_host *host,
448 struct mmc_data *data)
451 int mask = rw_threshold - 1;
454 host->sg_len = dma_map_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
455 mmc_get_dma_dir(data));
457 /* no individual DMA segment should need a partial FIFO */
458 for (i = 0; i < host->sg_len; i++) {
459 if (sg_dma_len(data->sg + i) & mask) {
460 dma_unmap_sg(mmc_dev(host->mmc),
461 data->sg, data->sg_len,
462 mmc_get_dma_dir(data));
468 ret = mmc_davinci_send_dma_request(host, data);
473 static void davinci_release_dma_channels(struct mmc_davinci_host *host)
478 dma_release_channel(host->dma_tx);
479 dma_release_channel(host->dma_rx);
482 static int davinci_acquire_dma_channels(struct mmc_davinci_host *host)
484 host->dma_tx = dma_request_chan(mmc_dev(host->mmc), "tx");
485 if (IS_ERR(host->dma_tx)) {
486 dev_err(mmc_dev(host->mmc), "Can't get dma_tx channel\n");
487 return PTR_ERR(host->dma_tx);
490 host->dma_rx = dma_request_chan(mmc_dev(host->mmc), "rx");
491 if (IS_ERR(host->dma_rx)) {
492 dev_err(mmc_dev(host->mmc), "Can't get dma_rx channel\n");
493 dma_release_channel(host->dma_tx);
494 return PTR_ERR(host->dma_rx);
500 /*----------------------------------------------------------------------*/
503 mmc_davinci_prepare_data(struct mmc_davinci_host *host, struct mmc_request *req)
505 int fifo_lev = (rw_threshold == 32) ? MMCFIFOCTL_FIFOLEV : 0;
507 struct mmc_data *data = req->data;
508 unsigned int flags = SG_MITER_ATOMIC; /* Used from IRQ */
510 if (host->version == MMC_CTLR_VERSION_2)
511 fifo_lev = (rw_threshold == 64) ? MMCFIFOCTL_FIFOLEV : 0;
515 host->data_dir = DAVINCI_MMC_DATADIR_NONE;
516 writel(0, host->base + DAVINCI_MMCBLEN);
517 writel(0, host->base + DAVINCI_MMCNBLK);
521 dev_dbg(mmc_dev(host->mmc), "%s, %d blocks of %d bytes\n",
522 (data->flags & MMC_DATA_WRITE) ? "write" : "read",
523 data->blocks, data->blksz);
524 dev_dbg(mmc_dev(host->mmc), " DTO %d cycles + %d ns\n",
525 data->timeout_clks, data->timeout_ns);
526 timeout = data->timeout_clks +
527 (data->timeout_ns / host->ns_in_one_cycle);
528 if (timeout > 0xffff)
531 writel(timeout, host->base + DAVINCI_MMCTOD);
532 writel(data->blocks, host->base + DAVINCI_MMCNBLK);
533 writel(data->blksz, host->base + DAVINCI_MMCBLEN);
535 /* Configure the FIFO */
536 if (data->flags & MMC_DATA_WRITE) {
537 flags |= SG_MITER_FROM_SG;
538 host->data_dir = DAVINCI_MMC_DATADIR_WRITE;
539 writel(fifo_lev | MMCFIFOCTL_FIFODIR_WR | MMCFIFOCTL_FIFORST,
540 host->base + DAVINCI_MMCFIFOCTL);
541 writel(fifo_lev | MMCFIFOCTL_FIFODIR_WR,
542 host->base + DAVINCI_MMCFIFOCTL);
544 flags |= SG_MITER_TO_SG;
545 host->data_dir = DAVINCI_MMC_DATADIR_READ;
546 writel(fifo_lev | MMCFIFOCTL_FIFODIR_RD | MMCFIFOCTL_FIFORST,
547 host->base + DAVINCI_MMCFIFOCTL);
548 writel(fifo_lev | MMCFIFOCTL_FIFODIR_RD,
549 host->base + DAVINCI_MMCFIFOCTL);
552 host->bytes_left = data->blocks * data->blksz;
554 /* For now we try to use DMA whenever we won't need partial FIFO
555 * reads or writes, either for the whole transfer (as tested here)
556 * or for any individual scatterlist segment (tested when we call
557 * start_dma_transfer).
559 * While we *could* change that, unusual block sizes are rarely
560 * used. The occasional fallback to PIO should't hurt.
562 if (host->use_dma && (host->bytes_left & (rw_threshold - 1)) == 0
563 && mmc_davinci_start_dma_transfer(host, data) == 0) {
564 /* zero this to ensure we take no PIO paths */
565 host->bytes_left = 0;
567 /* Revert to CPU Copy */
568 host->sg_len = data->sg_len;
569 sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
573 static void mmc_davinci_request(struct mmc_host *mmc, struct mmc_request *req)
575 struct mmc_davinci_host *host = mmc_priv(mmc);
576 unsigned long timeout = jiffies + msecs_to_jiffies(900);
579 /* Card may still be sending BUSY after a previous operation,
580 * typically some kind of write. If so, we can't proceed yet.
582 while (time_before(jiffies, timeout)) {
583 mmcst1 = readl(host->base + DAVINCI_MMCST1);
584 if (!(mmcst1 & MMCST1_BUSY))
588 if (mmcst1 & MMCST1_BUSY) {
589 dev_err(mmc_dev(host->mmc), "still BUSY? bad ... \n");
590 req->cmd->error = -ETIMEDOUT;
591 mmc_request_done(mmc, req);
596 mmc_davinci_prepare_data(host, req);
597 mmc_davinci_start_command(host, req->cmd);
600 static unsigned int calculate_freq_for_card(struct mmc_davinci_host *host,
601 unsigned int mmc_req_freq)
603 unsigned int mmc_freq = 0, mmc_pclk = 0, mmc_push_pull_divisor = 0;
605 mmc_pclk = host->mmc_input_clk;
606 if (mmc_req_freq && mmc_pclk > (2 * mmc_req_freq))
607 mmc_push_pull_divisor = ((unsigned int)mmc_pclk
608 / (2 * mmc_req_freq)) - 1;
610 mmc_push_pull_divisor = 0;
612 mmc_freq = (unsigned int)mmc_pclk
613 / (2 * (mmc_push_pull_divisor + 1));
615 if (mmc_freq > mmc_req_freq)
616 mmc_push_pull_divisor = mmc_push_pull_divisor + 1;
617 /* Convert ns to clock cycles */
618 if (mmc_req_freq <= 400000)
619 host->ns_in_one_cycle = (1000000) / (((mmc_pclk
620 / (2 * (mmc_push_pull_divisor + 1)))/1000));
622 host->ns_in_one_cycle = (1000000) / (((mmc_pclk
623 / (2 * (mmc_push_pull_divisor + 1)))/1000000));
625 return mmc_push_pull_divisor;
628 static void calculate_clk_divider(struct mmc_host *mmc, struct mmc_ios *ios)
630 unsigned int open_drain_freq = 0, mmc_pclk = 0;
631 unsigned int mmc_push_pull_freq = 0;
632 struct mmc_davinci_host *host = mmc_priv(mmc);
634 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN) {
637 /* Ignoring the init clock value passed for fixing the inter
638 * operability with different cards.
640 open_drain_freq = ((unsigned int)mmc_pclk
641 / (2 * MMCSD_INIT_CLOCK)) - 1;
643 if (open_drain_freq > 0xFF)
644 open_drain_freq = 0xFF;
646 temp = readl(host->base + DAVINCI_MMCCLK) & ~MMCCLK_CLKRT_MASK;
647 temp |= open_drain_freq;
648 writel(temp, host->base + DAVINCI_MMCCLK);
650 /* Convert ns to clock cycles */
651 host->ns_in_one_cycle = (1000000) / (MMCSD_INIT_CLOCK/1000);
654 mmc_push_pull_freq = calculate_freq_for_card(host, ios->clock);
656 if (mmc_push_pull_freq > 0xFF)
657 mmc_push_pull_freq = 0xFF;
659 temp = readl(host->base + DAVINCI_MMCCLK) & ~MMCCLK_CLKEN;
660 writel(temp, host->base + DAVINCI_MMCCLK);
664 temp = readl(host->base + DAVINCI_MMCCLK) & ~MMCCLK_CLKRT_MASK;
665 temp |= mmc_push_pull_freq;
666 writel(temp, host->base + DAVINCI_MMCCLK);
668 writel(temp | MMCCLK_CLKEN, host->base + DAVINCI_MMCCLK);
674 static void mmc_davinci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
676 struct mmc_davinci_host *host = mmc_priv(mmc);
677 struct platform_device *pdev = to_platform_device(mmc->parent);
678 struct davinci_mmc_config *config = pdev->dev.platform_data;
680 dev_dbg(mmc_dev(host->mmc),
681 "clock %dHz busmode %d powermode %d Vdd %04x\n",
682 ios->clock, ios->bus_mode, ios->power_mode,
685 switch (ios->power_mode) {
687 if (config && config->set_power)
688 config->set_power(pdev->id, false);
691 if (config && config->set_power)
692 config->set_power(pdev->id, true);
696 switch (ios->bus_width) {
697 case MMC_BUS_WIDTH_8:
698 dev_dbg(mmc_dev(host->mmc), "Enabling 8 bit mode\n");
699 writel((readl(host->base + DAVINCI_MMCCTL) &
700 ~MMCCTL_WIDTH_4_BIT) | MMCCTL_WIDTH_8_BIT,
701 host->base + DAVINCI_MMCCTL);
703 case MMC_BUS_WIDTH_4:
704 dev_dbg(mmc_dev(host->mmc), "Enabling 4 bit mode\n");
705 if (host->version == MMC_CTLR_VERSION_2)
706 writel((readl(host->base + DAVINCI_MMCCTL) &
707 ~MMCCTL_WIDTH_8_BIT) | MMCCTL_WIDTH_4_BIT,
708 host->base + DAVINCI_MMCCTL);
710 writel(readl(host->base + DAVINCI_MMCCTL) |
712 host->base + DAVINCI_MMCCTL);
714 case MMC_BUS_WIDTH_1:
715 dev_dbg(mmc_dev(host->mmc), "Enabling 1 bit mode\n");
716 if (host->version == MMC_CTLR_VERSION_2)
717 writel(readl(host->base + DAVINCI_MMCCTL) &
718 ~(MMCCTL_WIDTH_8_BIT | MMCCTL_WIDTH_4_BIT),
719 host->base + DAVINCI_MMCCTL);
721 writel(readl(host->base + DAVINCI_MMCCTL) &
723 host->base + DAVINCI_MMCCTL);
727 calculate_clk_divider(mmc, ios);
729 host->bus_mode = ios->bus_mode;
730 if (ios->power_mode == MMC_POWER_UP) {
731 unsigned long timeout = jiffies + msecs_to_jiffies(50);
734 /* Send clock cycles, poll completion */
735 writel(0, host->base + DAVINCI_MMCARGHL);
736 writel(MMCCMD_INITCK, host->base + DAVINCI_MMCCMD);
737 while (time_before(jiffies, timeout)) {
738 u32 tmp = readl(host->base + DAVINCI_MMCST0);
740 if (tmp & MMCST0_RSPDNE) {
747 dev_warn(mmc_dev(host->mmc), "powerup timeout\n");
750 /* FIXME on power OFF, reset things ... */
754 mmc_davinci_xfer_done(struct mmc_davinci_host *host, struct mmc_data *data)
758 if (host->mmc->caps & MMC_CAP_SDIO_IRQ) {
760 * SDIO Interrupt Detection work-around as suggested by
761 * Davinci Errata (TMS320DM355 Silicon Revision 1.1 Errata
762 * 2.1.6): Signal SDIO interrupt only if it is enabled by core
764 if (host->sdio_int && !(readl(host->base + DAVINCI_SDIOST0) &
766 writel(SDIOIST_IOINT, host->base + DAVINCI_SDIOIST);
767 mmc_signal_sdio_irq(host->mmc);
772 davinci_abort_dma(host);
774 dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
775 mmc_get_dma_dir(data));
776 host->do_dma = false;
778 host->data_dir = DAVINCI_MMC_DATADIR_NONE;
780 if (!data->stop || (host->cmd && host->cmd->error)) {
781 mmc_request_done(host->mmc, data->mrq);
782 writel(0, host->base + DAVINCI_MMCIM);
783 host->active_request = false;
785 mmc_davinci_start_command(host, data->stop);
788 static void mmc_davinci_cmd_done(struct mmc_davinci_host *host,
789 struct mmc_command *cmd)
793 if (cmd->flags & MMC_RSP_PRESENT) {
794 if (cmd->flags & MMC_RSP_136) {
795 /* response type 2 */
796 cmd->resp[3] = readl(host->base + DAVINCI_MMCRSP01);
797 cmd->resp[2] = readl(host->base + DAVINCI_MMCRSP23);
798 cmd->resp[1] = readl(host->base + DAVINCI_MMCRSP45);
799 cmd->resp[0] = readl(host->base + DAVINCI_MMCRSP67);
801 /* response types 1, 1b, 3, 4, 5, 6 */
802 cmd->resp[0] = readl(host->base + DAVINCI_MMCRSP67);
806 if (host->data == NULL || cmd->error) {
807 if (cmd->error == -ETIMEDOUT)
808 cmd->mrq->cmd->retries = 0;
809 mmc_request_done(host->mmc, cmd->mrq);
810 writel(0, host->base + DAVINCI_MMCIM);
811 host->active_request = false;
815 static inline void mmc_davinci_reset_ctrl(struct mmc_davinci_host *host,
820 temp = readl(host->base + DAVINCI_MMCCTL);
822 temp |= MMCCTL_CMDRST | MMCCTL_DATRST;
824 temp &= ~(MMCCTL_CMDRST | MMCCTL_DATRST);
826 writel(temp, host->base + DAVINCI_MMCCTL);
831 davinci_abort_data(struct mmc_davinci_host *host, struct mmc_data *data)
833 mmc_davinci_reset_ctrl(host, 1);
834 mmc_davinci_reset_ctrl(host, 0);
836 sg_miter_stop(&host->sg_miter);
839 static irqreturn_t mmc_davinci_sdio_irq(int irq, void *dev_id)
841 struct mmc_davinci_host *host = dev_id;
844 status = readl(host->base + DAVINCI_SDIOIST);
845 if (status & SDIOIST_IOINT) {
846 dev_dbg(mmc_dev(host->mmc),
847 "SDIO interrupt status %x\n", status);
848 writel(status | SDIOIST_IOINT, host->base + DAVINCI_SDIOIST);
849 mmc_signal_sdio_irq(host->mmc);
854 static irqreturn_t mmc_davinci_irq(int irq, void *dev_id)
856 struct mmc_davinci_host *host = (struct mmc_davinci_host *)dev_id;
857 unsigned int status, qstatus;
859 int end_transfer = 0;
860 struct mmc_data *data = host->data;
862 if (host->cmd == NULL && host->data == NULL) {
863 status = readl(host->base + DAVINCI_MMCST0);
864 dev_dbg(mmc_dev(host->mmc),
865 "Spurious interrupt 0x%04x\n", status);
866 /* Disable the interrupt from mmcsd */
867 writel(0, host->base + DAVINCI_MMCIM);
871 status = readl(host->base + DAVINCI_MMCST0);
874 /* handle FIFO first when using PIO for data.
875 * bytes_left will decrease to zero as I/O progress and status will
876 * read zero over iteration because this controller status
877 * register(MMCST0) reports any status only once and it is cleared
878 * by read. So, it is not unbouned loop even in the case of
881 if (host->bytes_left && (status & (MMCST0_DXRDY | MMCST0_DRRDY))) {
882 unsigned long im_val;
885 * If interrupts fire during the following loop, they will be
886 * handled by the handler, but the PIC will still buffer these.
887 * As a result, the handler will be called again to serve these
888 * needlessly. In order to avoid these spurious interrupts,
889 * keep interrupts masked during the loop.
891 im_val = readl(host->base + DAVINCI_MMCIM);
892 writel(0, host->base + DAVINCI_MMCIM);
895 davinci_fifo_data_trans(host, rw_threshold);
896 status = readl(host->base + DAVINCI_MMCST0);
898 } while (host->bytes_left &&
899 (status & (MMCST0_DXRDY | MMCST0_DRRDY)));
902 * If an interrupt is pending, it is assumed it will fire when
903 * it is unmasked. This assumption is also taken when the MMCIM
904 * is first set. Otherwise, writing to MMCIM after reading the
905 * status is race-prone.
907 writel(im_val, host->base + DAVINCI_MMCIM);
910 if (qstatus & MMCST0_DATDNE) {
911 /* All blocks sent/received, and CRC checks passed */
914 if (host->bytes_left > 0)
915 /* if datasize < rw_threshold
916 * no RX ints are generated
918 davinci_fifo_data_trans(host, host->bytes_left);
919 sg_miter_stop(&host->sg_miter);
922 data->bytes_xfered = data->blocks * data->blksz;
924 dev_err(mmc_dev(host->mmc),
925 "DATDNE with no host->data\n");
929 if (qstatus & MMCST0_TOUTRD) {
930 /* Read data timeout */
931 data->error = -ETIMEDOUT;
934 dev_dbg(mmc_dev(host->mmc),
935 "read data timeout, status %x\n",
938 davinci_abort_data(host, data);
941 if (qstatus & (MMCST0_CRCWR | MMCST0_CRCRD)) {
943 data->error = -EILSEQ;
946 /* NOTE: this controller uses CRCWR to report both CRC
947 * errors and timeouts (on writes). MMCDRSP values are
948 * only weakly documented, but 0x9f was clearly a timeout
949 * case and the two three-bit patterns in various SD specs
950 * (101, 010) aren't part of it ...
952 if (qstatus & MMCST0_CRCWR) {
953 u32 temp = readb(host->base + DAVINCI_MMCDRSP);
956 data->error = -ETIMEDOUT;
958 dev_dbg(mmc_dev(host->mmc), "data %s %s error\n",
959 (qstatus & MMCST0_CRCWR) ? "write" : "read",
960 (data->error == -ETIMEDOUT) ? "timeout" : "CRC");
962 davinci_abort_data(host, data);
965 if (qstatus & MMCST0_TOUTRS) {
966 /* Command timeout */
968 dev_dbg(mmc_dev(host->mmc),
969 "CMD%d timeout, status %x\n",
970 host->cmd->opcode, qstatus);
971 host->cmd->error = -ETIMEDOUT;
974 davinci_abort_data(host, data);
980 if (qstatus & MMCST0_CRCRS) {
981 /* Command CRC error */
982 dev_dbg(mmc_dev(host->mmc), "Command CRC error\n");
984 host->cmd->error = -EILSEQ;
989 if (qstatus & MMCST0_RSPDNE) {
990 /* End of command phase */
991 end_command = host->cmd ? 1 : 0;
995 mmc_davinci_cmd_done(host, host->cmd);
997 mmc_davinci_xfer_done(host, data);
1001 static int mmc_davinci_get_cd(struct mmc_host *mmc)
1003 struct platform_device *pdev = to_platform_device(mmc->parent);
1004 struct davinci_mmc_config *config = pdev->dev.platform_data;
1006 if (config && config->get_cd)
1007 return config->get_cd(pdev->id);
1009 return mmc_gpio_get_cd(mmc);
1012 static int mmc_davinci_get_ro(struct mmc_host *mmc)
1014 struct platform_device *pdev = to_platform_device(mmc->parent);
1015 struct davinci_mmc_config *config = pdev->dev.platform_data;
1017 if (config && config->get_ro)
1018 return config->get_ro(pdev->id);
1020 return mmc_gpio_get_ro(mmc);
1023 static void mmc_davinci_enable_sdio_irq(struct mmc_host *mmc, int enable)
1025 struct mmc_davinci_host *host = mmc_priv(mmc);
1028 if (!(readl(host->base + DAVINCI_SDIOST0) & SDIOST0_DAT1_HI)) {
1029 writel(SDIOIST_IOINT, host->base + DAVINCI_SDIOIST);
1030 mmc_signal_sdio_irq(host->mmc);
1032 host->sdio_int = true;
1033 writel(readl(host->base + DAVINCI_SDIOIEN) |
1034 SDIOIEN_IOINTEN, host->base + DAVINCI_SDIOIEN);
1037 host->sdio_int = false;
1038 writel(readl(host->base + DAVINCI_SDIOIEN) & ~SDIOIEN_IOINTEN,
1039 host->base + DAVINCI_SDIOIEN);
1043 static const struct mmc_host_ops mmc_davinci_ops = {
1044 .request = mmc_davinci_request,
1045 .set_ios = mmc_davinci_set_ios,
1046 .get_cd = mmc_davinci_get_cd,
1047 .get_ro = mmc_davinci_get_ro,
1048 .enable_sdio_irq = mmc_davinci_enable_sdio_irq,
1051 /*----------------------------------------------------------------------*/
1053 #ifdef CONFIG_CPU_FREQ
1054 static int mmc_davinci_cpufreq_transition(struct notifier_block *nb,
1055 unsigned long val, void *data)
1057 struct mmc_davinci_host *host;
1058 unsigned int mmc_pclk;
1059 struct mmc_host *mmc;
1060 unsigned long flags;
1062 host = container_of(nb, struct mmc_davinci_host, freq_transition);
1064 mmc_pclk = clk_get_rate(host->clk);
1066 if (val == CPUFREQ_POSTCHANGE) {
1067 spin_lock_irqsave(&mmc->lock, flags);
1068 host->mmc_input_clk = mmc_pclk;
1069 calculate_clk_divider(mmc, &mmc->ios);
1070 spin_unlock_irqrestore(&mmc->lock, flags);
1076 static inline int mmc_davinci_cpufreq_register(struct mmc_davinci_host *host)
1078 host->freq_transition.notifier_call = mmc_davinci_cpufreq_transition;
1080 return cpufreq_register_notifier(&host->freq_transition,
1081 CPUFREQ_TRANSITION_NOTIFIER);
1084 static inline void mmc_davinci_cpufreq_deregister(struct mmc_davinci_host *host)
1086 cpufreq_unregister_notifier(&host->freq_transition,
1087 CPUFREQ_TRANSITION_NOTIFIER);
1090 static inline int mmc_davinci_cpufreq_register(struct mmc_davinci_host *host)
1095 static inline void mmc_davinci_cpufreq_deregister(struct mmc_davinci_host *host)
1099 static void init_mmcsd_host(struct mmc_davinci_host *host)
1102 mmc_davinci_reset_ctrl(host, 1);
1104 writel(0, host->base + DAVINCI_MMCCLK);
1105 writel(MMCCLK_CLKEN, host->base + DAVINCI_MMCCLK);
1107 writel(0x1FFF, host->base + DAVINCI_MMCTOR);
1108 writel(0xFFFF, host->base + DAVINCI_MMCTOD);
1110 mmc_davinci_reset_ctrl(host, 0);
1113 static const struct platform_device_id davinci_mmc_devtype[] = {
1115 .name = "dm6441-mmc",
1116 .driver_data = MMC_CTLR_VERSION_1,
1118 .name = "da830-mmc",
1119 .driver_data = MMC_CTLR_VERSION_2,
1123 MODULE_DEVICE_TABLE(platform, davinci_mmc_devtype);
1125 static const struct of_device_id davinci_mmc_dt_ids[] = {
1127 .compatible = "ti,dm6441-mmc",
1128 .data = &davinci_mmc_devtype[MMC_CTLR_VERSION_1],
1131 .compatible = "ti,da830-mmc",
1132 .data = &davinci_mmc_devtype[MMC_CTLR_VERSION_2],
1136 MODULE_DEVICE_TABLE(of, davinci_mmc_dt_ids);
1138 static int mmc_davinci_parse_pdata(struct mmc_host *mmc)
1140 struct platform_device *pdev = to_platform_device(mmc->parent);
1141 struct davinci_mmc_config *pdata = pdev->dev.platform_data;
1142 struct mmc_davinci_host *host;
1148 host = mmc_priv(mmc);
1152 if (pdata && pdata->nr_sg)
1153 host->nr_sg = pdata->nr_sg - 1;
1155 if (pdata && (pdata->wires == 4 || pdata->wires == 0))
1156 mmc->caps |= MMC_CAP_4_BIT_DATA;
1158 if (pdata && (pdata->wires == 8))
1159 mmc->caps |= (MMC_CAP_4_BIT_DATA | MMC_CAP_8_BIT_DATA);
1161 mmc->f_min = 312500;
1162 mmc->f_max = 25000000;
1163 if (pdata && pdata->max_freq)
1164 mmc->f_max = pdata->max_freq;
1165 if (pdata && pdata->caps)
1166 mmc->caps |= pdata->caps;
1168 /* Register a cd gpio, if there is not one, enable polling */
1169 ret = mmc_gpiod_request_cd(mmc, "cd", 0, false, 0);
1170 if (ret == -EPROBE_DEFER)
1173 mmc->caps |= MMC_CAP_NEEDS_POLL;
1175 ret = mmc_gpiod_request_ro(mmc, "wp", 0, 0);
1176 if (ret == -EPROBE_DEFER)
1182 static int davinci_mmcsd_probe(struct platform_device *pdev)
1184 struct mmc_davinci_host *host = NULL;
1185 struct mmc_host *mmc = NULL;
1186 struct resource *r, *mem = NULL;
1189 const struct platform_device_id *id_entry;
1191 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1194 irq = platform_get_irq(pdev, 0);
1198 mem_size = resource_size(r);
1199 mem = devm_request_mem_region(&pdev->dev, r->start, mem_size,
1204 mmc = mmc_alloc_host(sizeof(struct mmc_davinci_host), &pdev->dev);
1208 host = mmc_priv(mmc);
1209 host->mmc = mmc; /* Important */
1211 host->mem_res = mem;
1212 host->base = devm_ioremap(&pdev->dev, mem->start, mem_size);
1218 host->clk = devm_clk_get(&pdev->dev, NULL);
1219 if (IS_ERR(host->clk)) {
1220 ret = PTR_ERR(host->clk);
1223 ret = clk_prepare_enable(host->clk);
1225 goto clk_prepare_enable_fail;
1227 host->mmc_input_clk = clk_get_rate(host->clk);
1229 pdev->id_entry = of_device_get_match_data(&pdev->dev);
1230 if (pdev->id_entry) {
1231 ret = mmc_of_parse(mmc);
1233 dev_err_probe(&pdev->dev, ret,
1234 "could not parse of data\n");
1238 ret = mmc_davinci_parse_pdata(mmc);
1241 "could not parse platform data: %d\n", ret);
1245 if (host->nr_sg > MAX_NR_SG || !host->nr_sg)
1246 host->nr_sg = MAX_NR_SG;
1248 init_mmcsd_host(host);
1250 host->use_dma = use_dma;
1251 host->mmc_irq = irq;
1252 host->sdio_irq = platform_get_irq_optional(pdev, 1);
1254 if (host->use_dma) {
1255 ret = davinci_acquire_dma_channels(host);
1256 if (ret == -EPROBE_DEFER)
1257 goto dma_probe_defer;
1262 mmc->caps |= MMC_CAP_WAIT_WHILE_BUSY;
1264 id_entry = platform_get_device_id(pdev);
1266 host->version = id_entry->driver_data;
1268 mmc->ops = &mmc_davinci_ops;
1269 mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
1271 /* With no iommu coalescing pages, each phys_seg is a hw_seg.
1272 * Each hw_seg uses one EDMA parameter RAM slot, always one
1273 * channel and then usually some linked slots.
1275 mmc->max_segs = MAX_NR_SG;
1277 /* EDMA limit per hw segment (one or two MBytes) */
1278 mmc->max_seg_size = MAX_CCNT * rw_threshold;
1280 /* MMC/SD controller limits for multiblock requests */
1281 mmc->max_blk_size = 4095; /* BLEN is 12 bits */
1282 mmc->max_blk_count = 65535; /* NBLK is 16 bits */
1283 mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
1285 dev_dbg(mmc_dev(host->mmc), "max_segs=%d\n", mmc->max_segs);
1286 dev_dbg(mmc_dev(host->mmc), "max_blk_size=%d\n", mmc->max_blk_size);
1287 dev_dbg(mmc_dev(host->mmc), "max_req_size=%d\n", mmc->max_req_size);
1288 dev_dbg(mmc_dev(host->mmc), "max_seg_size=%d\n", mmc->max_seg_size);
1290 platform_set_drvdata(pdev, host);
1292 ret = mmc_davinci_cpufreq_register(host);
1294 dev_err(&pdev->dev, "failed to register cpufreq\n");
1298 ret = mmc_add_host(mmc);
1300 goto mmc_add_host_fail;
1302 ret = devm_request_irq(&pdev->dev, irq, mmc_davinci_irq, 0,
1303 mmc_hostname(mmc), host);
1305 goto request_irq_fail;
1307 if (host->sdio_irq >= 0) {
1308 ret = devm_request_irq(&pdev->dev, host->sdio_irq,
1309 mmc_davinci_sdio_irq, 0,
1310 mmc_hostname(mmc), host);
1312 mmc->caps |= MMC_CAP_SDIO_IRQ;
1315 rename_region(mem, mmc_hostname(mmc));
1317 dev_info(mmc_dev(host->mmc), "Using %s, %d-bit mode\n",
1318 host->use_dma ? "DMA" : "PIO",
1319 (mmc->caps & MMC_CAP_4_BIT_DATA) ? 4 : 1);
1324 mmc_remove_host(mmc);
1326 mmc_davinci_cpufreq_deregister(host);
1328 davinci_release_dma_channels(host);
1331 clk_disable_unprepare(host->clk);
1332 clk_prepare_enable_fail:
1340 static void __exit davinci_mmcsd_remove(struct platform_device *pdev)
1342 struct mmc_davinci_host *host = platform_get_drvdata(pdev);
1344 mmc_remove_host(host->mmc);
1345 mmc_davinci_cpufreq_deregister(host);
1346 davinci_release_dma_channels(host);
1347 clk_disable_unprepare(host->clk);
1348 mmc_free_host(host->mmc);
1352 static int davinci_mmcsd_suspend(struct device *dev)
1354 struct mmc_davinci_host *host = dev_get_drvdata(dev);
1356 writel(0, host->base + DAVINCI_MMCIM);
1357 mmc_davinci_reset_ctrl(host, 1);
1358 clk_disable(host->clk);
1363 static int davinci_mmcsd_resume(struct device *dev)
1365 struct mmc_davinci_host *host = dev_get_drvdata(dev);
1368 ret = clk_enable(host->clk);
1372 mmc_davinci_reset_ctrl(host, 0);
1377 static const struct dev_pm_ops davinci_mmcsd_pm = {
1378 .suspend = davinci_mmcsd_suspend,
1379 .resume = davinci_mmcsd_resume,
1382 #define davinci_mmcsd_pm_ops (&davinci_mmcsd_pm)
1384 #define davinci_mmcsd_pm_ops NULL
1387 static struct platform_driver davinci_mmcsd_driver = {
1389 .name = "davinci_mmc",
1390 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
1391 .pm = davinci_mmcsd_pm_ops,
1392 .of_match_table = davinci_mmc_dt_ids,
1394 .probe = davinci_mmcsd_probe,
1395 .remove_new = __exit_p(davinci_mmcsd_remove),
1396 .id_table = davinci_mmc_devtype,
1399 module_platform_driver(davinci_mmcsd_driver);
1401 MODULE_AUTHOR("Texas Instruments India");
1402 MODULE_LICENSE("GPL");
1403 MODULE_DESCRIPTION("MMC/SD driver for Davinci MMC controller");
1404 MODULE_ALIAS("platform:davinci_mmc");