2 * Synopsys DesignWare Multimedia Card Interface driver
3 * (Based on NXP driver for lpc 31xx)
5 * Copyright (C) 2009 NXP Semiconductors
6 * Copyright (C) 2009, 2010 Imagination Technologies Ltd.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
14 #include <linux/blkdev.h>
15 #include <linux/clk.h>
16 #include <linux/debugfs.h>
17 #include <linux/device.h>
18 #include <linux/dma-mapping.h>
19 #include <linux/err.h>
20 #include <linux/init.h>
21 #include <linux/interrupt.h>
22 #include <linux/ioport.h>
23 #include <linux/module.h>
24 #include <linux/platform_device.h>
25 #include <linux/seq_file.h>
26 #include <linux/slab.h>
27 #include <linux/stat.h>
28 #include <linux/delay.h>
29 #include <linux/irq.h>
30 #include <linux/mmc/card.h>
31 #include <linux/mmc/host.h>
32 #include <linux/mmc/mmc.h>
33 #include <linux/mmc/sd.h>
34 #include <linux/mmc/sdio.h>
35 #include <linux/mmc/dw_mmc.h>
36 #include <linux/bitops.h>
37 #include <linux/regulator/consumer.h>
39 #include <linux/of_gpio.h>
40 #include <linux/mmc/slot-gpio.h>
44 /* Common flag combinations */
45 #define DW_MCI_DATA_ERROR_FLAGS (SDMMC_INT_DRTO | SDMMC_INT_DCRC | \
46 SDMMC_INT_HTO | SDMMC_INT_SBE | \
48 #define DW_MCI_CMD_ERROR_FLAGS (SDMMC_INT_RTO | SDMMC_INT_RCRC | \
50 #define DW_MCI_ERROR_FLAGS (DW_MCI_DATA_ERROR_FLAGS | \
51 DW_MCI_CMD_ERROR_FLAGS | SDMMC_INT_HLE)
52 #define DW_MCI_SEND_STATUS 1
53 #define DW_MCI_RECV_STATUS 2
54 #define DW_MCI_DMA_THRESHOLD 16
56 #define DW_MCI_FREQ_MAX 200000000 /* unit: HZ */
57 #define DW_MCI_FREQ_MIN 400000 /* unit: HZ */
59 #ifdef CONFIG_MMC_DW_IDMAC
60 #define IDMAC_INT_CLR (SDMMC_IDMAC_INT_AI | SDMMC_IDMAC_INT_NI | \
61 SDMMC_IDMAC_INT_CES | SDMMC_IDMAC_INT_DU | \
62 SDMMC_IDMAC_INT_FBE | SDMMC_IDMAC_INT_RI | \
65 struct idmac_desc_64addr {
66 u32 des0; /* Control Descriptor */
68 u32 des1; /* Reserved */
70 u32 des2; /*Buffer sizes */
71 #define IDMAC_64ADDR_SET_BUFFER1_SIZE(d, s) \
72 ((d)->des2 = ((d)->des2 & 0x03ffe000) | ((s) & 0x1fff))
74 u32 des3; /* Reserved */
76 u32 des4; /* Lower 32-bits of Buffer Address Pointer 1*/
77 u32 des5; /* Upper 32-bits of Buffer Address Pointer 1*/
79 u32 des6; /* Lower 32-bits of Next Descriptor Address */
80 u32 des7; /* Upper 32-bits of Next Descriptor Address */
84 u32 des0; /* Control Descriptor */
85 #define IDMAC_DES0_DIC BIT(1)
86 #define IDMAC_DES0_LD BIT(2)
87 #define IDMAC_DES0_FD BIT(3)
88 #define IDMAC_DES0_CH BIT(4)
89 #define IDMAC_DES0_ER BIT(5)
90 #define IDMAC_DES0_CES BIT(30)
91 #define IDMAC_DES0_OWN BIT(31)
93 u32 des1; /* Buffer sizes */
94 #define IDMAC_SET_BUFFER1_SIZE(d, s) \
95 ((d)->des1 = ((d)->des1 & 0x03ffe000) | ((s) & 0x1fff))
97 u32 des2; /* buffer 1 physical address */
99 u32 des3; /* buffer 2 physical address */
101 #endif /* CONFIG_MMC_DW_IDMAC */
103 static bool dw_mci_reset(struct dw_mci *host);
104 static bool dw_mci_ctrl_reset(struct dw_mci *host, u32 reset);
106 #if defined(CONFIG_DEBUG_FS)
107 static int dw_mci_req_show(struct seq_file *s, void *v)
109 struct dw_mci_slot *slot = s->private;
110 struct mmc_request *mrq;
111 struct mmc_command *cmd;
112 struct mmc_command *stop;
113 struct mmc_data *data;
115 /* Make sure we get a consistent snapshot */
116 spin_lock_bh(&slot->host->lock);
126 "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n",
127 cmd->opcode, cmd->arg, cmd->flags,
128 cmd->resp[0], cmd->resp[1], cmd->resp[2],
129 cmd->resp[2], cmd->error);
131 seq_printf(s, "DATA %u / %u * %u flg %x err %d\n",
132 data->bytes_xfered, data->blocks,
133 data->blksz, data->flags, data->error);
136 "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n",
137 stop->opcode, stop->arg, stop->flags,
138 stop->resp[0], stop->resp[1], stop->resp[2],
139 stop->resp[2], stop->error);
142 spin_unlock_bh(&slot->host->lock);
147 static int dw_mci_req_open(struct inode *inode, struct file *file)
149 return single_open(file, dw_mci_req_show, inode->i_private);
152 static const struct file_operations dw_mci_req_fops = {
153 .owner = THIS_MODULE,
154 .open = dw_mci_req_open,
157 .release = single_release,
160 static int dw_mci_regs_show(struct seq_file *s, void *v)
162 seq_printf(s, "STATUS:\t0x%08x\n", SDMMC_STATUS);
163 seq_printf(s, "RINTSTS:\t0x%08x\n", SDMMC_RINTSTS);
164 seq_printf(s, "CMD:\t0x%08x\n", SDMMC_CMD);
165 seq_printf(s, "CTRL:\t0x%08x\n", SDMMC_CTRL);
166 seq_printf(s, "INTMASK:\t0x%08x\n", SDMMC_INTMASK);
167 seq_printf(s, "CLKENA:\t0x%08x\n", SDMMC_CLKENA);
172 static int dw_mci_regs_open(struct inode *inode, struct file *file)
174 return single_open(file, dw_mci_regs_show, inode->i_private);
177 static const struct file_operations dw_mci_regs_fops = {
178 .owner = THIS_MODULE,
179 .open = dw_mci_regs_open,
182 .release = single_release,
185 static void dw_mci_init_debugfs(struct dw_mci_slot *slot)
187 struct mmc_host *mmc = slot->mmc;
188 struct dw_mci *host = slot->host;
192 root = mmc->debugfs_root;
196 node = debugfs_create_file("regs", S_IRUSR, root, host,
201 node = debugfs_create_file("req", S_IRUSR, root, slot,
206 node = debugfs_create_u32("state", S_IRUSR, root, (u32 *)&host->state);
210 node = debugfs_create_x32("pending_events", S_IRUSR, root,
211 (u32 *)&host->pending_events);
215 node = debugfs_create_x32("completed_events", S_IRUSR, root,
216 (u32 *)&host->completed_events);
223 dev_err(&mmc->class_dev, "failed to initialize debugfs for slot\n");
225 #endif /* defined(CONFIG_DEBUG_FS) */
227 static void mci_send_cmd(struct dw_mci_slot *slot, u32 cmd, u32 arg);
229 static u32 dw_mci_prepare_command(struct mmc_host *mmc, struct mmc_command *cmd)
231 struct mmc_data *data;
232 struct dw_mci_slot *slot = mmc_priv(mmc);
233 struct dw_mci *host = slot->host;
234 const struct dw_mci_drv_data *drv_data = slot->host->drv_data;
236 cmd->error = -EINPROGRESS;
240 if (cmd->opcode == MMC_STOP_TRANSMISSION ||
241 cmd->opcode == MMC_GO_IDLE_STATE ||
242 cmd->opcode == MMC_GO_INACTIVE_STATE ||
243 (cmd->opcode == SD_IO_RW_DIRECT &&
244 ((cmd->arg >> 9) & 0x1FFFF) == SDIO_CCCR_ABORT))
245 cmdr |= SDMMC_CMD_STOP;
246 else if (cmd->opcode != MMC_SEND_STATUS && cmd->data)
247 cmdr |= SDMMC_CMD_PRV_DAT_WAIT;
249 if (cmd->opcode == SD_SWITCH_VOLTAGE) {
252 /* Special bit makes CMD11 not die */
253 cmdr |= SDMMC_CMD_VOLT_SWITCH;
255 /* Change state to continue to handle CMD11 weirdness */
256 WARN_ON(slot->host->state != STATE_SENDING_CMD);
257 slot->host->state = STATE_SENDING_CMD11;
260 * We need to disable low power mode (automatic clock stop)
261 * while doing voltage switch so we don't confuse the card,
262 * since stopping the clock is a specific part of the UHS
263 * voltage change dance.
265 * Note that low power mode (SDMMC_CLKEN_LOW_PWR) will be
266 * unconditionally turned back on in dw_mci_setup_bus() if it's
267 * ever called with a non-zero clock. That shouldn't happen
268 * until the voltage change is all done.
270 clk_en_a = mci_readl(host, CLKENA);
271 clk_en_a &= ~(SDMMC_CLKEN_LOW_PWR << slot->id);
272 mci_writel(host, CLKENA, clk_en_a);
273 mci_send_cmd(slot, SDMMC_CMD_UPD_CLK |
274 SDMMC_CMD_PRV_DAT_WAIT, 0);
277 if (cmd->flags & MMC_RSP_PRESENT) {
278 /* We expect a response, so set this bit */
279 cmdr |= SDMMC_CMD_RESP_EXP;
280 if (cmd->flags & MMC_RSP_136)
281 cmdr |= SDMMC_CMD_RESP_LONG;
284 if (cmd->flags & MMC_RSP_CRC)
285 cmdr |= SDMMC_CMD_RESP_CRC;
289 cmdr |= SDMMC_CMD_DAT_EXP;
290 if (data->flags & MMC_DATA_STREAM)
291 cmdr |= SDMMC_CMD_STRM_MODE;
292 if (data->flags & MMC_DATA_WRITE)
293 cmdr |= SDMMC_CMD_DAT_WR;
296 if (drv_data && drv_data->prepare_command)
297 drv_data->prepare_command(slot->host, &cmdr);
302 static u32 dw_mci_prep_stop_abort(struct dw_mci *host, struct mmc_command *cmd)
304 struct mmc_command *stop;
310 stop = &host->stop_abort;
312 memset(stop, 0, sizeof(struct mmc_command));
314 if (cmdr == MMC_READ_SINGLE_BLOCK ||
315 cmdr == MMC_READ_MULTIPLE_BLOCK ||
316 cmdr == MMC_WRITE_BLOCK ||
317 cmdr == MMC_WRITE_MULTIPLE_BLOCK ||
318 cmdr == MMC_SEND_TUNING_BLOCK ||
319 cmdr == MMC_SEND_TUNING_BLOCK_HS200) {
320 stop->opcode = MMC_STOP_TRANSMISSION;
322 stop->flags = MMC_RSP_R1B | MMC_CMD_AC;
323 } else if (cmdr == SD_IO_RW_EXTENDED) {
324 stop->opcode = SD_IO_RW_DIRECT;
325 stop->arg |= (1 << 31) | (0 << 28) | (SDIO_CCCR_ABORT << 9) |
326 ((cmd->arg >> 28) & 0x7);
327 stop->flags = MMC_RSP_SPI_R5 | MMC_RSP_R5 | MMC_CMD_AC;
332 cmdr = stop->opcode | SDMMC_CMD_STOP |
333 SDMMC_CMD_RESP_CRC | SDMMC_CMD_RESP_EXP;
338 static void dw_mci_start_command(struct dw_mci *host,
339 struct mmc_command *cmd, u32 cmd_flags)
343 "start command: ARGR=0x%08x CMDR=0x%08x\n",
344 cmd->arg, cmd_flags);
346 mci_writel(host, CMDARG, cmd->arg);
349 mci_writel(host, CMD, cmd_flags | SDMMC_CMD_START);
352 static inline void send_stop_abort(struct dw_mci *host, struct mmc_data *data)
354 struct mmc_command *stop = data->stop ? data->stop : &host->stop_abort;
355 dw_mci_start_command(host, stop, host->stop_cmdr);
358 /* DMA interface functions */
359 static void dw_mci_stop_dma(struct dw_mci *host)
361 if (host->using_dma) {
362 host->dma_ops->stop(host);
363 host->dma_ops->cleanup(host);
366 /* Data transfer was stopped by the interrupt handler */
367 set_bit(EVENT_XFER_COMPLETE, &host->pending_events);
370 static int dw_mci_get_dma_dir(struct mmc_data *data)
372 if (data->flags & MMC_DATA_WRITE)
373 return DMA_TO_DEVICE;
375 return DMA_FROM_DEVICE;
378 #ifdef CONFIG_MMC_DW_IDMAC
379 static void dw_mci_dma_cleanup(struct dw_mci *host)
381 struct mmc_data *data = host->data;
384 if (!data->host_cookie)
385 dma_unmap_sg(host->dev,
388 dw_mci_get_dma_dir(data));
391 static void dw_mci_idmac_reset(struct dw_mci *host)
393 u32 bmod = mci_readl(host, BMOD);
394 /* Software reset of DMA */
395 bmod |= SDMMC_IDMAC_SWRESET;
396 mci_writel(host, BMOD, bmod);
399 static void dw_mci_idmac_stop_dma(struct dw_mci *host)
403 /* Disable and reset the IDMAC interface */
404 temp = mci_readl(host, CTRL);
405 temp &= ~SDMMC_CTRL_USE_IDMAC;
406 temp |= SDMMC_CTRL_DMA_RESET;
407 mci_writel(host, CTRL, temp);
409 /* Stop the IDMAC running */
410 temp = mci_readl(host, BMOD);
411 temp &= ~(SDMMC_IDMAC_ENABLE | SDMMC_IDMAC_FB);
412 temp |= SDMMC_IDMAC_SWRESET;
413 mci_writel(host, BMOD, temp);
416 static void dw_mci_idmac_complete_dma(struct dw_mci *host)
418 struct mmc_data *data = host->data;
420 dev_vdbg(host->dev, "DMA complete\n");
422 host->dma_ops->cleanup(host);
425 * If the card was removed, data will be NULL. No point in trying to
426 * send the stop command or waiting for NBUSY in this case.
429 set_bit(EVENT_XFER_COMPLETE, &host->pending_events);
430 tasklet_schedule(&host->tasklet);
434 static void dw_mci_translate_sglist(struct dw_mci *host, struct mmc_data *data,
438 if (host->dma_64bit_address == 1) {
439 struct idmac_desc_64addr *desc = host->sg_cpu;
441 for (i = 0; i < sg_len; i++, desc++) {
442 unsigned int length = sg_dma_len(&data->sg[i]);
443 u64 mem_addr = sg_dma_address(&data->sg[i]);
446 * Set the OWN bit and disable interrupts for this
449 desc->des0 = IDMAC_DES0_OWN | IDMAC_DES0_DIC |
452 IDMAC_64ADDR_SET_BUFFER1_SIZE(desc, length);
454 /* Physical address to DMA to/from */
455 desc->des4 = mem_addr & 0xffffffff;
456 desc->des5 = mem_addr >> 32;
459 /* Set first descriptor */
461 desc->des0 |= IDMAC_DES0_FD;
463 /* Set last descriptor */
464 desc = host->sg_cpu + (i - 1) *
465 sizeof(struct idmac_desc_64addr);
466 desc->des0 &= ~(IDMAC_DES0_CH | IDMAC_DES0_DIC);
467 desc->des0 |= IDMAC_DES0_LD;
470 struct idmac_desc *desc = host->sg_cpu;
472 for (i = 0; i < sg_len; i++, desc++) {
473 unsigned int length = sg_dma_len(&data->sg[i]);
474 u32 mem_addr = sg_dma_address(&data->sg[i]);
477 * Set the OWN bit and disable interrupts for this
480 desc->des0 = IDMAC_DES0_OWN | IDMAC_DES0_DIC |
483 IDMAC_SET_BUFFER1_SIZE(desc, length);
485 /* Physical address to DMA to/from */
486 desc->des2 = mem_addr;
489 /* Set first descriptor */
491 desc->des0 |= IDMAC_DES0_FD;
493 /* Set last descriptor */
494 desc = host->sg_cpu + (i - 1) * sizeof(struct idmac_desc);
495 desc->des0 &= ~(IDMAC_DES0_CH | IDMAC_DES0_DIC);
496 desc->des0 |= IDMAC_DES0_LD;
502 static void dw_mci_idmac_start_dma(struct dw_mci *host, unsigned int sg_len)
506 dw_mci_translate_sglist(host, host->data, sg_len);
508 /* Make sure to reset DMA in case we did PIO before this */
509 dw_mci_ctrl_reset(host, SDMMC_CTRL_DMA_RESET);
510 dw_mci_idmac_reset(host);
512 /* Select IDMAC interface */
513 temp = mci_readl(host, CTRL);
514 temp |= SDMMC_CTRL_USE_IDMAC;
515 mci_writel(host, CTRL, temp);
519 /* Enable the IDMAC */
520 temp = mci_readl(host, BMOD);
521 temp |= SDMMC_IDMAC_ENABLE | SDMMC_IDMAC_FB;
522 mci_writel(host, BMOD, temp);
524 /* Start it running */
525 mci_writel(host, PLDMND, 1);
528 static int dw_mci_idmac_init(struct dw_mci *host)
532 if (host->dma_64bit_address == 1) {
533 struct idmac_desc_64addr *p;
534 /* Number of descriptors in the ring buffer */
535 host->ring_size = PAGE_SIZE / sizeof(struct idmac_desc_64addr);
537 /* Forward link the descriptor list */
538 for (i = 0, p = host->sg_cpu; i < host->ring_size - 1;
540 p->des6 = (host->sg_dma +
541 (sizeof(struct idmac_desc_64addr) *
542 (i + 1))) & 0xffffffff;
544 p->des7 = (u64)(host->sg_dma +
545 (sizeof(struct idmac_desc_64addr) *
547 /* Initialize reserved and buffer size fields to "0" */
553 /* Set the last descriptor as the end-of-ring descriptor */
554 p->des6 = host->sg_dma & 0xffffffff;
555 p->des7 = (u64)host->sg_dma >> 32;
556 p->des0 = IDMAC_DES0_ER;
559 struct idmac_desc *p;
560 /* Number of descriptors in the ring buffer */
561 host->ring_size = PAGE_SIZE / sizeof(struct idmac_desc);
563 /* Forward link the descriptor list */
564 for (i = 0, p = host->sg_cpu; i < host->ring_size - 1; i++, p++)
565 p->des3 = host->sg_dma + (sizeof(struct idmac_desc) *
568 /* Set the last descriptor as the end-of-ring descriptor */
569 p->des3 = host->sg_dma;
570 p->des0 = IDMAC_DES0_ER;
573 dw_mci_idmac_reset(host);
575 if (host->dma_64bit_address == 1) {
576 /* Mask out interrupts - get Tx & Rx complete only */
577 mci_writel(host, IDSTS64, IDMAC_INT_CLR);
578 mci_writel(host, IDINTEN64, SDMMC_IDMAC_INT_NI |
579 SDMMC_IDMAC_INT_RI | SDMMC_IDMAC_INT_TI);
581 /* Set the descriptor base address */
582 mci_writel(host, DBADDRL, host->sg_dma & 0xffffffff);
583 mci_writel(host, DBADDRU, (u64)host->sg_dma >> 32);
586 /* Mask out interrupts - get Tx & Rx complete only */
587 mci_writel(host, IDSTS, IDMAC_INT_CLR);
588 mci_writel(host, IDINTEN, SDMMC_IDMAC_INT_NI |
589 SDMMC_IDMAC_INT_RI | SDMMC_IDMAC_INT_TI);
591 /* Set the descriptor base address */
592 mci_writel(host, DBADDR, host->sg_dma);
598 static const struct dw_mci_dma_ops dw_mci_idmac_ops = {
599 .init = dw_mci_idmac_init,
600 .start = dw_mci_idmac_start_dma,
601 .stop = dw_mci_idmac_stop_dma,
602 .complete = dw_mci_idmac_complete_dma,
603 .cleanup = dw_mci_dma_cleanup,
605 #endif /* CONFIG_MMC_DW_IDMAC */
607 static int dw_mci_pre_dma_transfer(struct dw_mci *host,
608 struct mmc_data *data,
611 struct scatterlist *sg;
612 unsigned int i, sg_len;
614 if (!next && data->host_cookie)
615 return data->host_cookie;
618 * We don't do DMA on "complex" transfers, i.e. with
619 * non-word-aligned buffers or lengths. Also, we don't bother
620 * with all the DMA setup overhead for short transfers.
622 if (data->blocks * data->blksz < DW_MCI_DMA_THRESHOLD)
628 for_each_sg(data->sg, sg, data->sg_len, i) {
629 if (sg->offset & 3 || sg->length & 3)
633 sg_len = dma_map_sg(host->dev,
636 dw_mci_get_dma_dir(data));
641 data->host_cookie = sg_len;
646 static void dw_mci_pre_req(struct mmc_host *mmc,
647 struct mmc_request *mrq,
650 struct dw_mci_slot *slot = mmc_priv(mmc);
651 struct mmc_data *data = mrq->data;
653 if (!slot->host->use_dma || !data)
656 if (data->host_cookie) {
657 data->host_cookie = 0;
661 if (dw_mci_pre_dma_transfer(slot->host, mrq->data, 1) < 0)
662 data->host_cookie = 0;
665 static void dw_mci_post_req(struct mmc_host *mmc,
666 struct mmc_request *mrq,
669 struct dw_mci_slot *slot = mmc_priv(mmc);
670 struct mmc_data *data = mrq->data;
672 if (!slot->host->use_dma || !data)
675 if (data->host_cookie)
676 dma_unmap_sg(slot->host->dev,
679 dw_mci_get_dma_dir(data));
680 data->host_cookie = 0;
683 static void dw_mci_adjust_fifoth(struct dw_mci *host, struct mmc_data *data)
685 #ifdef CONFIG_MMC_DW_IDMAC
686 unsigned int blksz = data->blksz;
687 const u32 mszs[] = {1, 4, 8, 16, 32, 64, 128, 256};
688 u32 fifo_width = 1 << host->data_shift;
689 u32 blksz_depth = blksz / fifo_width, fifoth_val;
690 u32 msize = 0, rx_wmark = 1, tx_wmark, tx_wmark_invers;
691 int idx = (sizeof(mszs) / sizeof(mszs[0])) - 1;
693 tx_wmark = (host->fifo_depth) / 2;
694 tx_wmark_invers = host->fifo_depth - tx_wmark;
698 * if blksz is not a multiple of the FIFO width
700 if (blksz % fifo_width) {
707 if (!((blksz_depth % mszs[idx]) ||
708 (tx_wmark_invers % mszs[idx]))) {
710 rx_wmark = mszs[idx] - 1;
715 * If idx is '0', it won't be tried
716 * Thus, initial values are uesed
719 fifoth_val = SDMMC_SET_FIFOTH(msize, rx_wmark, tx_wmark);
720 mci_writel(host, FIFOTH, fifoth_val);
724 static void dw_mci_ctrl_rd_thld(struct dw_mci *host, struct mmc_data *data)
726 unsigned int blksz = data->blksz;
727 u32 blksz_depth, fifo_depth;
730 WARN_ON(!(data->flags & MMC_DATA_READ));
733 * CDTHRCTL doesn't exist prior to 240A (in fact that register offset is
734 * in the FIFO region, so we really shouldn't access it).
736 if (host->verid < DW_MMC_240A)
739 if (host->timing != MMC_TIMING_MMC_HS200 &&
740 host->timing != MMC_TIMING_UHS_SDR104)
743 blksz_depth = blksz / (1 << host->data_shift);
744 fifo_depth = host->fifo_depth;
746 if (blksz_depth > fifo_depth)
750 * If (blksz_depth) >= (fifo_depth >> 1), should be 'thld_size <= blksz'
751 * If (blksz_depth) < (fifo_depth >> 1), should be thld_size = blksz
752 * Currently just choose blksz.
755 mci_writel(host, CDTHRCTL, SDMMC_SET_RD_THLD(thld_size, 1));
759 mci_writel(host, CDTHRCTL, SDMMC_SET_RD_THLD(0, 0));
762 static int dw_mci_submit_data_dma(struct dw_mci *host, struct mmc_data *data)
764 unsigned long irqflags;
770 /* If we don't have a channel, we can't do DMA */
774 sg_len = dw_mci_pre_dma_transfer(host, data, 0);
776 host->dma_ops->stop(host);
783 "sd sg_cpu: %#lx sg_dma: %#lx sg_len: %d\n",
784 (unsigned long)host->sg_cpu, (unsigned long)host->sg_dma,
788 * Decide the MSIZE and RX/TX Watermark.
789 * If current block size is same with previous size,
790 * no need to update fifoth.
792 if (host->prev_blksz != data->blksz)
793 dw_mci_adjust_fifoth(host, data);
795 /* Enable the DMA interface */
796 temp = mci_readl(host, CTRL);
797 temp |= SDMMC_CTRL_DMA_ENABLE;
798 mci_writel(host, CTRL, temp);
800 /* Disable RX/TX IRQs, let DMA handle it */
801 spin_lock_irqsave(&host->irq_lock, irqflags);
802 temp = mci_readl(host, INTMASK);
803 temp &= ~(SDMMC_INT_RXDR | SDMMC_INT_TXDR);
804 mci_writel(host, INTMASK, temp);
805 spin_unlock_irqrestore(&host->irq_lock, irqflags);
807 host->dma_ops->start(host, sg_len);
812 static void dw_mci_submit_data(struct dw_mci *host, struct mmc_data *data)
814 unsigned long irqflags;
817 data->error = -EINPROGRESS;
823 if (data->flags & MMC_DATA_READ) {
824 host->dir_status = DW_MCI_RECV_STATUS;
825 dw_mci_ctrl_rd_thld(host, data);
827 host->dir_status = DW_MCI_SEND_STATUS;
830 if (dw_mci_submit_data_dma(host, data)) {
831 int flags = SG_MITER_ATOMIC;
832 if (host->data->flags & MMC_DATA_READ)
833 flags |= SG_MITER_TO_SG;
835 flags |= SG_MITER_FROM_SG;
837 sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
839 host->part_buf_start = 0;
840 host->part_buf_count = 0;
842 mci_writel(host, RINTSTS, SDMMC_INT_TXDR | SDMMC_INT_RXDR);
844 spin_lock_irqsave(&host->irq_lock, irqflags);
845 temp = mci_readl(host, INTMASK);
846 temp |= SDMMC_INT_TXDR | SDMMC_INT_RXDR;
847 mci_writel(host, INTMASK, temp);
848 spin_unlock_irqrestore(&host->irq_lock, irqflags);
850 temp = mci_readl(host, CTRL);
851 temp &= ~SDMMC_CTRL_DMA_ENABLE;
852 mci_writel(host, CTRL, temp);
855 * Use the initial fifoth_val for PIO mode.
856 * If next issued data may be transfered by DMA mode,
857 * prev_blksz should be invalidated.
859 mci_writel(host, FIFOTH, host->fifoth_val);
860 host->prev_blksz = 0;
863 * Keep the current block size.
864 * It will be used to decide whether to update
865 * fifoth register next time.
867 host->prev_blksz = data->blksz;
871 static void mci_send_cmd(struct dw_mci_slot *slot, u32 cmd, u32 arg)
873 struct dw_mci *host = slot->host;
874 unsigned long timeout = jiffies + msecs_to_jiffies(500);
875 unsigned int cmd_status = 0;
877 mci_writel(host, CMDARG, arg);
879 mci_writel(host, CMD, SDMMC_CMD_START | cmd);
881 while (time_before(jiffies, timeout)) {
882 cmd_status = mci_readl(host, CMD);
883 if (!(cmd_status & SDMMC_CMD_START))
886 dev_err(&slot->mmc->class_dev,
887 "Timeout sending command (cmd %#x arg %#x status %#x)\n",
888 cmd, arg, cmd_status);
891 static void dw_mci_setup_bus(struct dw_mci_slot *slot, bool force_clkinit)
893 struct dw_mci *host = slot->host;
894 unsigned int clock = slot->clock;
897 u32 sdmmc_cmd_bits = SDMMC_CMD_UPD_CLK | SDMMC_CMD_PRV_DAT_WAIT;
899 /* We must continue to set bit 28 in CMD until the change is complete */
900 if (host->state == STATE_WAITING_CMD11_DONE)
901 sdmmc_cmd_bits |= SDMMC_CMD_VOLT_SWITCH;
904 mci_writel(host, CLKENA, 0);
905 mci_send_cmd(slot, sdmmc_cmd_bits, 0);
906 } else if (clock != host->current_speed || force_clkinit) {
907 div = host->bus_hz / clock;
908 if (host->bus_hz % clock && host->bus_hz > clock)
910 * move the + 1 after the divide to prevent
911 * over-clocking the card.
915 div = (host->bus_hz != clock) ? DIV_ROUND_UP(div, 2) : 0;
917 if ((clock << div) != slot->__clk_old || force_clkinit)
918 dev_info(&slot->mmc->class_dev,
919 "Bus speed (slot %d) = %dHz (slot req %dHz, actual %dHZ div = %d)\n",
920 slot->id, host->bus_hz, clock,
921 div ? ((host->bus_hz / div) >> 1) :
925 mci_writel(host, CLKENA, 0);
926 mci_writel(host, CLKSRC, 0);
929 mci_send_cmd(slot, sdmmc_cmd_bits, 0);
931 /* set clock to desired speed */
932 mci_writel(host, CLKDIV, div);
935 mci_send_cmd(slot, sdmmc_cmd_bits, 0);
937 /* enable clock; only low power if no SDIO */
938 clk_en_a = SDMMC_CLKEN_ENABLE << slot->id;
939 if (!test_bit(DW_MMC_CARD_NO_LOW_PWR, &slot->flags))
940 clk_en_a |= SDMMC_CLKEN_LOW_PWR << slot->id;
941 mci_writel(host, CLKENA, clk_en_a);
944 mci_send_cmd(slot, sdmmc_cmd_bits, 0);
946 /* keep the clock with reflecting clock dividor */
947 slot->__clk_old = clock << div;
950 host->current_speed = clock;
952 /* Set the current slot bus width */
953 mci_writel(host, CTYPE, (slot->ctype << slot->id));
956 static void __dw_mci_start_request(struct dw_mci *host,
957 struct dw_mci_slot *slot,
958 struct mmc_command *cmd)
960 struct mmc_request *mrq;
961 struct mmc_data *data;
966 host->cur_slot = slot;
969 host->pending_events = 0;
970 host->completed_events = 0;
971 host->cmd_status = 0;
972 host->data_status = 0;
973 host->dir_status = 0;
977 mci_writel(host, TMOUT, 0xFFFFFFFF);
978 mci_writel(host, BYTCNT, data->blksz*data->blocks);
979 mci_writel(host, BLKSIZ, data->blksz);
982 cmdflags = dw_mci_prepare_command(slot->mmc, cmd);
984 /* this is the first command, send the initialization clock */
985 if (test_and_clear_bit(DW_MMC_CARD_NEED_INIT, &slot->flags))
986 cmdflags |= SDMMC_CMD_INIT;
989 dw_mci_submit_data(host, data);
993 dw_mci_start_command(host, cmd, cmdflags);
996 host->stop_cmdr = dw_mci_prepare_command(slot->mmc, mrq->stop);
998 host->stop_cmdr = dw_mci_prep_stop_abort(host, cmd);
1001 static void dw_mci_start_request(struct dw_mci *host,
1002 struct dw_mci_slot *slot)
1004 struct mmc_request *mrq = slot->mrq;
1005 struct mmc_command *cmd;
1007 cmd = mrq->sbc ? mrq->sbc : mrq->cmd;
1008 __dw_mci_start_request(host, slot, cmd);
1011 /* must be called with host->lock held */
1012 static void dw_mci_queue_request(struct dw_mci *host, struct dw_mci_slot *slot,
1013 struct mmc_request *mrq)
1015 dev_vdbg(&slot->mmc->class_dev, "queue request: state=%d\n",
1020 if (host->state == STATE_WAITING_CMD11_DONE) {
1021 dev_warn(&slot->mmc->class_dev,
1022 "Voltage change didn't complete\n");
1024 * this case isn't expected to happen, so we can
1025 * either crash here or just try to continue on
1026 * in the closest possible state
1028 host->state = STATE_IDLE;
1031 if (host->state == STATE_IDLE) {
1032 host->state = STATE_SENDING_CMD;
1033 dw_mci_start_request(host, slot);
1035 list_add_tail(&slot->queue_node, &host->queue);
1039 static void dw_mci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1041 struct dw_mci_slot *slot = mmc_priv(mmc);
1042 struct dw_mci *host = slot->host;
1047 * The check for card presence and queueing of the request must be
1048 * atomic, otherwise the card could be removed in between and the
1049 * request wouldn't fail until another card was inserted.
1051 spin_lock_bh(&host->lock);
1053 if (!test_bit(DW_MMC_CARD_PRESENT, &slot->flags)) {
1054 spin_unlock_bh(&host->lock);
1055 mrq->cmd->error = -ENOMEDIUM;
1056 mmc_request_done(mmc, mrq);
1060 dw_mci_queue_request(host, slot, mrq);
1062 spin_unlock_bh(&host->lock);
1065 static void dw_mci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1067 struct dw_mci_slot *slot = mmc_priv(mmc);
1068 const struct dw_mci_drv_data *drv_data = slot->host->drv_data;
1072 switch (ios->bus_width) {
1073 case MMC_BUS_WIDTH_4:
1074 slot->ctype = SDMMC_CTYPE_4BIT;
1076 case MMC_BUS_WIDTH_8:
1077 slot->ctype = SDMMC_CTYPE_8BIT;
1080 /* set default 1 bit mode */
1081 slot->ctype = SDMMC_CTYPE_1BIT;
1084 regs = mci_readl(slot->host, UHS_REG);
1087 if (ios->timing == MMC_TIMING_MMC_DDR52 ||
1088 ios->timing == MMC_TIMING_MMC_HS400)
1089 regs |= ((0x1 << slot->id) << 16);
1091 regs &= ~((0x1 << slot->id) << 16);
1093 mci_writel(slot->host, UHS_REG, regs);
1094 slot->host->timing = ios->timing;
1097 * Use mirror of ios->clock to prevent race with mmc
1098 * core ios update when finding the minimum.
1100 slot->clock = ios->clock;
1102 if (drv_data && drv_data->set_ios)
1103 drv_data->set_ios(slot->host, ios);
1105 /* Slot specific timing and width adjustment */
1106 dw_mci_setup_bus(slot, false);
1108 if (slot->host->state == STATE_WAITING_CMD11_DONE && ios->clock != 0)
1109 slot->host->state = STATE_IDLE;
1111 switch (ios->power_mode) {
1113 if (!IS_ERR(mmc->supply.vmmc)) {
1114 ret = mmc_regulator_set_ocr(mmc, mmc->supply.vmmc,
1117 dev_err(slot->host->dev,
1118 "failed to enable vmmc regulator\n");
1119 /*return, if failed turn on vmmc*/
1123 set_bit(DW_MMC_CARD_NEED_INIT, &slot->flags);
1124 regs = mci_readl(slot->host, PWREN);
1125 regs |= (1 << slot->id);
1126 mci_writel(slot->host, PWREN, regs);
1129 if (!IS_ERR(mmc->supply.vqmmc) && !slot->host->vqmmc_enabled) {
1130 ret = regulator_enable(mmc->supply.vqmmc);
1132 dev_err(slot->host->dev,
1133 "failed to enable vqmmc regulator\n");
1135 slot->host->vqmmc_enabled = true;
1139 if (!IS_ERR(mmc->supply.vmmc))
1140 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0);
1142 if (!IS_ERR(mmc->supply.vqmmc) && slot->host->vqmmc_enabled) {
1143 regulator_disable(mmc->supply.vqmmc);
1144 slot->host->vqmmc_enabled = false;
1147 regs = mci_readl(slot->host, PWREN);
1148 regs &= ~(1 << slot->id);
1149 mci_writel(slot->host, PWREN, regs);
1156 static int dw_mci_card_busy(struct mmc_host *mmc)
1158 struct dw_mci_slot *slot = mmc_priv(mmc);
1162 * Check the busy bit which is low when DAT[3:0]
1163 * (the data lines) are 0000
1165 status = mci_readl(slot->host, STATUS);
1167 return !!(status & SDMMC_STATUS_BUSY);
1170 static int dw_mci_switch_voltage(struct mmc_host *mmc, struct mmc_ios *ios)
1172 struct dw_mci_slot *slot = mmc_priv(mmc);
1173 struct dw_mci *host = slot->host;
1175 u32 v18 = SDMMC_UHS_18V << slot->id;
1180 * Program the voltage. Note that some instances of dw_mmc may use
1181 * the UHS_REG for this. For other instances (like exynos) the UHS_REG
1182 * does no harm but you need to set the regulator directly. Try both.
1184 uhs = mci_readl(host, UHS_REG);
1185 if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_330) {
1194 if (!IS_ERR(mmc->supply.vqmmc)) {
1195 ret = regulator_set_voltage(mmc->supply.vqmmc, min_uv, max_uv);
1198 dev_dbg(&mmc->class_dev,
1199 "Regulator set error %d: %d - %d\n",
1200 ret, min_uv, max_uv);
1204 mci_writel(host, UHS_REG, uhs);
1209 static int dw_mci_get_ro(struct mmc_host *mmc)
1212 struct dw_mci_slot *slot = mmc_priv(mmc);
1213 int gpio_ro = mmc_gpio_get_ro(mmc);
1215 /* Use platform get_ro function, else try on board write protect */
1216 if ((slot->quirks & DW_MCI_SLOT_QUIRK_NO_WRITE_PROTECT) ||
1217 (slot->host->quirks & DW_MCI_QUIRK_NO_WRITE_PROTECT))
1219 else if (!IS_ERR_VALUE(gpio_ro))
1220 read_only = gpio_ro;
1223 mci_readl(slot->host, WRTPRT) & (1 << slot->id) ? 1 : 0;
1225 dev_dbg(&mmc->class_dev, "card is %s\n",
1226 read_only ? "read-only" : "read-write");
1231 static int dw_mci_get_cd(struct mmc_host *mmc)
1234 struct dw_mci_slot *slot = mmc_priv(mmc);
1235 struct dw_mci_board *brd = slot->host->pdata;
1236 struct dw_mci *host = slot->host;
1237 int gpio_cd = mmc_gpio_get_cd(mmc);
1239 /* Use platform get_cd function, else try onboard card detect */
1240 if (brd->quirks & DW_MCI_QUIRK_BROKEN_CARD_DETECTION)
1242 else if (!IS_ERR_VALUE(gpio_cd))
1245 present = (mci_readl(slot->host, CDETECT) & (1 << slot->id))
1248 spin_lock_bh(&host->lock);
1250 set_bit(DW_MMC_CARD_PRESENT, &slot->flags);
1251 dev_dbg(&mmc->class_dev, "card is present\n");
1253 clear_bit(DW_MMC_CARD_PRESENT, &slot->flags);
1254 dev_dbg(&mmc->class_dev, "card is not present\n");
1256 spin_unlock_bh(&host->lock);
1261 static void dw_mci_init_card(struct mmc_host *mmc, struct mmc_card *card)
1263 struct dw_mci_slot *slot = mmc_priv(mmc);
1264 struct dw_mci *host = slot->host;
1267 * Low power mode will stop the card clock when idle. According to the
1268 * description of the CLKENA register we should disable low power mode
1269 * for SDIO cards if we need SDIO interrupts to work.
1271 if (mmc->caps & MMC_CAP_SDIO_IRQ) {
1272 const u32 clken_low_pwr = SDMMC_CLKEN_LOW_PWR << slot->id;
1276 clk_en_a_old = mci_readl(host, CLKENA);
1278 if (card->type == MMC_TYPE_SDIO ||
1279 card->type == MMC_TYPE_SD_COMBO) {
1280 set_bit(DW_MMC_CARD_NO_LOW_PWR, &slot->flags);
1281 clk_en_a = clk_en_a_old & ~clken_low_pwr;
1283 clear_bit(DW_MMC_CARD_NO_LOW_PWR, &slot->flags);
1284 clk_en_a = clk_en_a_old | clken_low_pwr;
1287 if (clk_en_a != clk_en_a_old) {
1288 mci_writel(host, CLKENA, clk_en_a);
1289 mci_send_cmd(slot, SDMMC_CMD_UPD_CLK |
1290 SDMMC_CMD_PRV_DAT_WAIT, 0);
1295 static void dw_mci_enable_sdio_irq(struct mmc_host *mmc, int enb)
1297 struct dw_mci_slot *slot = mmc_priv(mmc);
1298 struct dw_mci *host = slot->host;
1299 unsigned long irqflags;
1302 spin_lock_irqsave(&host->irq_lock, irqflags);
1304 /* Enable/disable Slot Specific SDIO interrupt */
1305 int_mask = mci_readl(host, INTMASK);
1307 int_mask |= SDMMC_INT_SDIO(slot->sdio_id);
1309 int_mask &= ~SDMMC_INT_SDIO(slot->sdio_id);
1310 mci_writel(host, INTMASK, int_mask);
1312 spin_unlock_irqrestore(&host->irq_lock, irqflags);
1315 static int dw_mci_execute_tuning(struct mmc_host *mmc, u32 opcode)
1317 struct dw_mci_slot *slot = mmc_priv(mmc);
1318 struct dw_mci *host = slot->host;
1319 const struct dw_mci_drv_data *drv_data = host->drv_data;
1322 if (drv_data && drv_data->execute_tuning)
1323 err = drv_data->execute_tuning(slot);
1327 int dw_mci_prepare_hs400_tuning(struct mmc_host *mmc, struct mmc_ios *ios)
1329 struct dw_mci_slot *slot = mmc_priv(mmc);
1330 struct dw_mci *host = slot->host;
1331 const struct dw_mci_drv_data *drv_data = host->drv_data;
1333 if (drv_data && drv_data->prepare_hs400_tuning)
1334 return drv_data->prepare_hs400_tuning(host, ios);
1339 static const struct mmc_host_ops dw_mci_ops = {
1340 .request = dw_mci_request,
1341 .pre_req = dw_mci_pre_req,
1342 .post_req = dw_mci_post_req,
1343 .set_ios = dw_mci_set_ios,
1344 .get_ro = dw_mci_get_ro,
1345 .get_cd = dw_mci_get_cd,
1346 .enable_sdio_irq = dw_mci_enable_sdio_irq,
1347 .execute_tuning = dw_mci_execute_tuning,
1348 .card_busy = dw_mci_card_busy,
1349 .start_signal_voltage_switch = dw_mci_switch_voltage,
1350 .init_card = dw_mci_init_card,
1351 .prepare_hs400_tuning = dw_mci_prepare_hs400_tuning,
1354 static void dw_mci_request_end(struct dw_mci *host, struct mmc_request *mrq)
1355 __releases(&host->lock)
1356 __acquires(&host->lock)
1358 struct dw_mci_slot *slot;
1359 struct mmc_host *prev_mmc = host->cur_slot->mmc;
1361 WARN_ON(host->cmd || host->data);
1363 host->cur_slot->mrq = NULL;
1365 if (!list_empty(&host->queue)) {
1366 slot = list_entry(host->queue.next,
1367 struct dw_mci_slot, queue_node);
1368 list_del(&slot->queue_node);
1369 dev_vdbg(host->dev, "list not empty: %s is next\n",
1370 mmc_hostname(slot->mmc));
1371 host->state = STATE_SENDING_CMD;
1372 dw_mci_start_request(host, slot);
1374 dev_vdbg(host->dev, "list empty\n");
1376 if (host->state == STATE_SENDING_CMD11)
1377 host->state = STATE_WAITING_CMD11_DONE;
1379 host->state = STATE_IDLE;
1382 spin_unlock(&host->lock);
1383 mmc_request_done(prev_mmc, mrq);
1384 spin_lock(&host->lock);
1387 static int dw_mci_command_complete(struct dw_mci *host, struct mmc_command *cmd)
1389 u32 status = host->cmd_status;
1391 host->cmd_status = 0;
1393 /* Read the response from the card (up to 16 bytes) */
1394 if (cmd->flags & MMC_RSP_PRESENT) {
1395 if (cmd->flags & MMC_RSP_136) {
1396 cmd->resp[3] = mci_readl(host, RESP0);
1397 cmd->resp[2] = mci_readl(host, RESP1);
1398 cmd->resp[1] = mci_readl(host, RESP2);
1399 cmd->resp[0] = mci_readl(host, RESP3);
1401 cmd->resp[0] = mci_readl(host, RESP0);
1408 if (status & SDMMC_INT_RTO)
1409 cmd->error = -ETIMEDOUT;
1410 else if ((cmd->flags & MMC_RSP_CRC) && (status & SDMMC_INT_RCRC))
1411 cmd->error = -EILSEQ;
1412 else if (status & SDMMC_INT_RESP_ERR)
1418 /* newer ip versions need a delay between retries */
1419 if (host->quirks & DW_MCI_QUIRK_RETRY_DELAY)
1426 static int dw_mci_data_complete(struct dw_mci *host, struct mmc_data *data)
1428 u32 status = host->data_status;
1430 if (status & DW_MCI_DATA_ERROR_FLAGS) {
1431 if (status & SDMMC_INT_DRTO) {
1432 data->error = -ETIMEDOUT;
1433 } else if (status & SDMMC_INT_DCRC) {
1434 data->error = -EILSEQ;
1435 } else if (status & SDMMC_INT_EBE) {
1436 if (host->dir_status ==
1437 DW_MCI_SEND_STATUS) {
1439 * No data CRC status was returned.
1440 * The number of bytes transferred
1441 * will be exaggerated in PIO mode.
1443 data->bytes_xfered = 0;
1444 data->error = -ETIMEDOUT;
1445 } else if (host->dir_status ==
1446 DW_MCI_RECV_STATUS) {
1450 /* SDMMC_INT_SBE is included */
1454 dev_dbg(host->dev, "data error, status 0x%08x\n", status);
1457 * After an error, there may be data lingering
1462 data->bytes_xfered = data->blocks * data->blksz;
1469 static void dw_mci_tasklet_func(unsigned long priv)
1471 struct dw_mci *host = (struct dw_mci *)priv;
1472 struct mmc_data *data;
1473 struct mmc_command *cmd;
1474 struct mmc_request *mrq;
1475 enum dw_mci_state state;
1476 enum dw_mci_state prev_state;
1479 spin_lock(&host->lock);
1481 state = host->state;
1490 case STATE_WAITING_CMD11_DONE:
1493 case STATE_SENDING_CMD11:
1494 case STATE_SENDING_CMD:
1495 if (!test_and_clear_bit(EVENT_CMD_COMPLETE,
1496 &host->pending_events))
1501 set_bit(EVENT_CMD_COMPLETE, &host->completed_events);
1502 err = dw_mci_command_complete(host, cmd);
1503 if (cmd == mrq->sbc && !err) {
1504 prev_state = state = STATE_SENDING_CMD;
1505 __dw_mci_start_request(host, host->cur_slot,
1510 if (cmd->data && err) {
1511 dw_mci_stop_dma(host);
1512 send_stop_abort(host, data);
1513 state = STATE_SENDING_STOP;
1517 if (!cmd->data || err) {
1518 dw_mci_request_end(host, mrq);
1522 prev_state = state = STATE_SENDING_DATA;
1525 case STATE_SENDING_DATA:
1527 * We could get a data error and never a transfer
1528 * complete so we'd better check for it here.
1530 * Note that we don't really care if we also got a
1531 * transfer complete; stopping the DMA and sending an
1534 if (test_and_clear_bit(EVENT_DATA_ERROR,
1535 &host->pending_events)) {
1536 dw_mci_stop_dma(host);
1538 !(host->data_status & (SDMMC_INT_DRTO |
1540 send_stop_abort(host, data);
1541 state = STATE_DATA_ERROR;
1545 if (!test_and_clear_bit(EVENT_XFER_COMPLETE,
1546 &host->pending_events))
1549 set_bit(EVENT_XFER_COMPLETE, &host->completed_events);
1552 * Handle an EVENT_DATA_ERROR that might have shown up
1553 * before the transfer completed. This might not have
1554 * been caught by the check above because the interrupt
1555 * could have gone off between the previous check and
1556 * the check for transfer complete.
1558 * Technically this ought not be needed assuming we
1559 * get a DATA_COMPLETE eventually (we'll notice the
1560 * error and end the request), but it shouldn't hurt.
1562 * This has the advantage of sending the stop command.
1564 if (test_and_clear_bit(EVENT_DATA_ERROR,
1565 &host->pending_events)) {
1566 dw_mci_stop_dma(host);
1568 !(host->data_status & (SDMMC_INT_DRTO |
1570 send_stop_abort(host, data);
1571 state = STATE_DATA_ERROR;
1574 prev_state = state = STATE_DATA_BUSY;
1578 case STATE_DATA_BUSY:
1579 if (!test_and_clear_bit(EVENT_DATA_COMPLETE,
1580 &host->pending_events))
1584 set_bit(EVENT_DATA_COMPLETE, &host->completed_events);
1585 err = dw_mci_data_complete(host, data);
1588 if (!data->stop || mrq->sbc) {
1589 if (mrq->sbc && data->stop)
1590 data->stop->error = 0;
1591 dw_mci_request_end(host, mrq);
1595 /* stop command for open-ended transfer*/
1597 send_stop_abort(host, data);
1600 * If we don't have a command complete now we'll
1601 * never get one since we just reset everything;
1602 * better end the request.
1604 * If we do have a command complete we'll fall
1605 * through to the SENDING_STOP command and
1606 * everything will be peachy keen.
1608 if (!test_bit(EVENT_CMD_COMPLETE,
1609 &host->pending_events)) {
1611 dw_mci_request_end(host, mrq);
1617 * If err has non-zero,
1618 * stop-abort command has been already issued.
1620 prev_state = state = STATE_SENDING_STOP;
1624 case STATE_SENDING_STOP:
1625 if (!test_and_clear_bit(EVENT_CMD_COMPLETE,
1626 &host->pending_events))
1629 /* CMD error in data command */
1630 if (mrq->cmd->error && mrq->data)
1637 dw_mci_command_complete(host, mrq->stop);
1639 host->cmd_status = 0;
1641 dw_mci_request_end(host, mrq);
1644 case STATE_DATA_ERROR:
1645 if (!test_and_clear_bit(EVENT_XFER_COMPLETE,
1646 &host->pending_events))
1649 state = STATE_DATA_BUSY;
1652 } while (state != prev_state);
1654 host->state = state;
1656 spin_unlock(&host->lock);
1660 /* push final bytes to part_buf, only use during push */
1661 static void dw_mci_set_part_bytes(struct dw_mci *host, void *buf, int cnt)
1663 memcpy((void *)&host->part_buf, buf, cnt);
1664 host->part_buf_count = cnt;
1667 /* append bytes to part_buf, only use during push */
1668 static int dw_mci_push_part_bytes(struct dw_mci *host, void *buf, int cnt)
1670 cnt = min(cnt, (1 << host->data_shift) - host->part_buf_count);
1671 memcpy((void *)&host->part_buf + host->part_buf_count, buf, cnt);
1672 host->part_buf_count += cnt;
1676 /* pull first bytes from part_buf, only use during pull */
1677 static int dw_mci_pull_part_bytes(struct dw_mci *host, void *buf, int cnt)
1679 cnt = min(cnt, (int)host->part_buf_count);
1681 memcpy(buf, (void *)&host->part_buf + host->part_buf_start,
1683 host->part_buf_count -= cnt;
1684 host->part_buf_start += cnt;
1689 /* pull final bytes from the part_buf, assuming it's just been filled */
1690 static void dw_mci_pull_final_bytes(struct dw_mci *host, void *buf, int cnt)
1692 memcpy(buf, &host->part_buf, cnt);
1693 host->part_buf_start = cnt;
1694 host->part_buf_count = (1 << host->data_shift) - cnt;
1697 static void dw_mci_push_data16(struct dw_mci *host, void *buf, int cnt)
1699 struct mmc_data *data = host->data;
1702 /* try and push anything in the part_buf */
1703 if (unlikely(host->part_buf_count)) {
1704 int len = dw_mci_push_part_bytes(host, buf, cnt);
1707 if (host->part_buf_count == 2) {
1708 mci_writew(host, DATA(host->data_offset),
1710 host->part_buf_count = 0;
1713 #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
1714 if (unlikely((unsigned long)buf & 0x1)) {
1716 u16 aligned_buf[64];
1717 int len = min(cnt & -2, (int)sizeof(aligned_buf));
1718 int items = len >> 1;
1720 /* memcpy from input buffer into aligned buffer */
1721 memcpy(aligned_buf, buf, len);
1724 /* push data from aligned buffer into fifo */
1725 for (i = 0; i < items; ++i)
1726 mci_writew(host, DATA(host->data_offset),
1733 for (; cnt >= 2; cnt -= 2)
1734 mci_writew(host, DATA(host->data_offset), *pdata++);
1737 /* put anything remaining in the part_buf */
1739 dw_mci_set_part_bytes(host, buf, cnt);
1740 /* Push data if we have reached the expected data length */
1741 if ((data->bytes_xfered + init_cnt) ==
1742 (data->blksz * data->blocks))
1743 mci_writew(host, DATA(host->data_offset),
1748 static void dw_mci_pull_data16(struct dw_mci *host, void *buf, int cnt)
1750 #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
1751 if (unlikely((unsigned long)buf & 0x1)) {
1753 /* pull data from fifo into aligned buffer */
1754 u16 aligned_buf[64];
1755 int len = min(cnt & -2, (int)sizeof(aligned_buf));
1756 int items = len >> 1;
1758 for (i = 0; i < items; ++i)
1759 aligned_buf[i] = mci_readw(host,
1760 DATA(host->data_offset));
1761 /* memcpy from aligned buffer into output buffer */
1762 memcpy(buf, aligned_buf, len);
1770 for (; cnt >= 2; cnt -= 2)
1771 *pdata++ = mci_readw(host, DATA(host->data_offset));
1775 host->part_buf16 = mci_readw(host, DATA(host->data_offset));
1776 dw_mci_pull_final_bytes(host, buf, cnt);
1780 static void dw_mci_push_data32(struct dw_mci *host, void *buf, int cnt)
1782 struct mmc_data *data = host->data;
1785 /* try and push anything in the part_buf */
1786 if (unlikely(host->part_buf_count)) {
1787 int len = dw_mci_push_part_bytes(host, buf, cnt);
1790 if (host->part_buf_count == 4) {
1791 mci_writel(host, DATA(host->data_offset),
1793 host->part_buf_count = 0;
1796 #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
1797 if (unlikely((unsigned long)buf & 0x3)) {
1799 u32 aligned_buf[32];
1800 int len = min(cnt & -4, (int)sizeof(aligned_buf));
1801 int items = len >> 2;
1803 /* memcpy from input buffer into aligned buffer */
1804 memcpy(aligned_buf, buf, len);
1807 /* push data from aligned buffer into fifo */
1808 for (i = 0; i < items; ++i)
1809 mci_writel(host, DATA(host->data_offset),
1816 for (; cnt >= 4; cnt -= 4)
1817 mci_writel(host, DATA(host->data_offset), *pdata++);
1820 /* put anything remaining in the part_buf */
1822 dw_mci_set_part_bytes(host, buf, cnt);
1823 /* Push data if we have reached the expected data length */
1824 if ((data->bytes_xfered + init_cnt) ==
1825 (data->blksz * data->blocks))
1826 mci_writel(host, DATA(host->data_offset),
1831 static void dw_mci_pull_data32(struct dw_mci *host, void *buf, int cnt)
1833 #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
1834 if (unlikely((unsigned long)buf & 0x3)) {
1836 /* pull data from fifo into aligned buffer */
1837 u32 aligned_buf[32];
1838 int len = min(cnt & -4, (int)sizeof(aligned_buf));
1839 int items = len >> 2;
1841 for (i = 0; i < items; ++i)
1842 aligned_buf[i] = mci_readl(host,
1843 DATA(host->data_offset));
1844 /* memcpy from aligned buffer into output buffer */
1845 memcpy(buf, aligned_buf, len);
1853 for (; cnt >= 4; cnt -= 4)
1854 *pdata++ = mci_readl(host, DATA(host->data_offset));
1858 host->part_buf32 = mci_readl(host, DATA(host->data_offset));
1859 dw_mci_pull_final_bytes(host, buf, cnt);
1863 static void dw_mci_push_data64(struct dw_mci *host, void *buf, int cnt)
1865 struct mmc_data *data = host->data;
1868 /* try and push anything in the part_buf */
1869 if (unlikely(host->part_buf_count)) {
1870 int len = dw_mci_push_part_bytes(host, buf, cnt);
1874 if (host->part_buf_count == 8) {
1875 mci_writeq(host, DATA(host->data_offset),
1877 host->part_buf_count = 0;
1880 #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
1881 if (unlikely((unsigned long)buf & 0x7)) {
1883 u64 aligned_buf[16];
1884 int len = min(cnt & -8, (int)sizeof(aligned_buf));
1885 int items = len >> 3;
1887 /* memcpy from input buffer into aligned buffer */
1888 memcpy(aligned_buf, buf, len);
1891 /* push data from aligned buffer into fifo */
1892 for (i = 0; i < items; ++i)
1893 mci_writeq(host, DATA(host->data_offset),
1900 for (; cnt >= 8; cnt -= 8)
1901 mci_writeq(host, DATA(host->data_offset), *pdata++);
1904 /* put anything remaining in the part_buf */
1906 dw_mci_set_part_bytes(host, buf, cnt);
1907 /* Push data if we have reached the expected data length */
1908 if ((data->bytes_xfered + init_cnt) ==
1909 (data->blksz * data->blocks))
1910 mci_writeq(host, DATA(host->data_offset),
1915 static void dw_mci_pull_data64(struct dw_mci *host, void *buf, int cnt)
1917 #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
1918 if (unlikely((unsigned long)buf & 0x7)) {
1920 /* pull data from fifo into aligned buffer */
1921 u64 aligned_buf[16];
1922 int len = min(cnt & -8, (int)sizeof(aligned_buf));
1923 int items = len >> 3;
1925 for (i = 0; i < items; ++i)
1926 aligned_buf[i] = mci_readq(host,
1927 DATA(host->data_offset));
1928 /* memcpy from aligned buffer into output buffer */
1929 memcpy(buf, aligned_buf, len);
1937 for (; cnt >= 8; cnt -= 8)
1938 *pdata++ = mci_readq(host, DATA(host->data_offset));
1942 host->part_buf = mci_readq(host, DATA(host->data_offset));
1943 dw_mci_pull_final_bytes(host, buf, cnt);
1947 static void dw_mci_pull_data(struct dw_mci *host, void *buf, int cnt)
1951 /* get remaining partial bytes */
1952 len = dw_mci_pull_part_bytes(host, buf, cnt);
1953 if (unlikely(len == cnt))
1958 /* get the rest of the data */
1959 host->pull_data(host, buf, cnt);
1962 static void dw_mci_read_data_pio(struct dw_mci *host, bool dto)
1964 struct sg_mapping_iter *sg_miter = &host->sg_miter;
1966 unsigned int offset;
1967 struct mmc_data *data = host->data;
1968 int shift = host->data_shift;
1971 unsigned int remain, fcnt;
1974 if (!sg_miter_next(sg_miter))
1977 host->sg = sg_miter->piter.sg;
1978 buf = sg_miter->addr;
1979 remain = sg_miter->length;
1983 fcnt = (SDMMC_GET_FCNT(mci_readl(host, STATUS))
1984 << shift) + host->part_buf_count;
1985 len = min(remain, fcnt);
1988 dw_mci_pull_data(host, (void *)(buf + offset), len);
1989 data->bytes_xfered += len;
1994 sg_miter->consumed = offset;
1995 status = mci_readl(host, MINTSTS);
1996 mci_writel(host, RINTSTS, SDMMC_INT_RXDR);
1997 /* if the RXDR is ready read again */
1998 } while ((status & SDMMC_INT_RXDR) ||
1999 (dto && SDMMC_GET_FCNT(mci_readl(host, STATUS))));
2002 if (!sg_miter_next(sg_miter))
2004 sg_miter->consumed = 0;
2006 sg_miter_stop(sg_miter);
2010 sg_miter_stop(sg_miter);
2013 set_bit(EVENT_XFER_COMPLETE, &host->pending_events);
2016 static void dw_mci_write_data_pio(struct dw_mci *host)
2018 struct sg_mapping_iter *sg_miter = &host->sg_miter;
2020 unsigned int offset;
2021 struct mmc_data *data = host->data;
2022 int shift = host->data_shift;
2025 unsigned int fifo_depth = host->fifo_depth;
2026 unsigned int remain, fcnt;
2029 if (!sg_miter_next(sg_miter))
2032 host->sg = sg_miter->piter.sg;
2033 buf = sg_miter->addr;
2034 remain = sg_miter->length;
2038 fcnt = ((fifo_depth -
2039 SDMMC_GET_FCNT(mci_readl(host, STATUS)))
2040 << shift) - host->part_buf_count;
2041 len = min(remain, fcnt);
2044 host->push_data(host, (void *)(buf + offset), len);
2045 data->bytes_xfered += len;
2050 sg_miter->consumed = offset;
2051 status = mci_readl(host, MINTSTS);
2052 mci_writel(host, RINTSTS, SDMMC_INT_TXDR);
2053 } while (status & SDMMC_INT_TXDR); /* if TXDR write again */
2056 if (!sg_miter_next(sg_miter))
2058 sg_miter->consumed = 0;
2060 sg_miter_stop(sg_miter);
2064 sg_miter_stop(sg_miter);
2067 set_bit(EVENT_XFER_COMPLETE, &host->pending_events);
2070 static void dw_mci_cmd_interrupt(struct dw_mci *host, u32 status)
2072 if (!host->cmd_status)
2073 host->cmd_status = status;
2077 set_bit(EVENT_CMD_COMPLETE, &host->pending_events);
2078 tasklet_schedule(&host->tasklet);
2081 static void dw_mci_handle_cd(struct dw_mci *host)
2085 for (i = 0; i < host->num_slots; i++) {
2086 struct dw_mci_slot *slot = host->slot[i];
2091 if (slot->mmc->ops->card_event)
2092 slot->mmc->ops->card_event(slot->mmc);
2093 mmc_detect_change(slot->mmc,
2094 msecs_to_jiffies(host->pdata->detect_delay_ms));
2098 static irqreturn_t dw_mci_interrupt(int irq, void *dev_id)
2100 struct dw_mci *host = dev_id;
2104 pending = mci_readl(host, MINTSTS); /* read-only mask reg */
2107 * DTO fix - version 2.10a and below, and only if internal DMA
2110 if (host->quirks & DW_MCI_QUIRK_IDMAC_DTO) {
2112 ((mci_readl(host, STATUS) >> 17) & 0x1fff))
2113 pending |= SDMMC_INT_DATA_OVER;
2117 /* Check volt switch first, since it can look like an error */
2118 if ((host->state == STATE_SENDING_CMD11) &&
2119 (pending & SDMMC_INT_VOLT_SWITCH)) {
2120 mci_writel(host, RINTSTS, SDMMC_INT_VOLT_SWITCH);
2121 pending &= ~SDMMC_INT_VOLT_SWITCH;
2122 dw_mci_cmd_interrupt(host, pending);
2125 if (pending & DW_MCI_CMD_ERROR_FLAGS) {
2126 mci_writel(host, RINTSTS, DW_MCI_CMD_ERROR_FLAGS);
2127 host->cmd_status = pending;
2129 set_bit(EVENT_CMD_COMPLETE, &host->pending_events);
2132 if (pending & DW_MCI_DATA_ERROR_FLAGS) {
2133 /* if there is an error report DATA_ERROR */
2134 mci_writel(host, RINTSTS, DW_MCI_DATA_ERROR_FLAGS);
2135 host->data_status = pending;
2137 set_bit(EVENT_DATA_ERROR, &host->pending_events);
2138 tasklet_schedule(&host->tasklet);
2141 if (pending & SDMMC_INT_DATA_OVER) {
2142 mci_writel(host, RINTSTS, SDMMC_INT_DATA_OVER);
2143 if (!host->data_status)
2144 host->data_status = pending;
2146 if (host->dir_status == DW_MCI_RECV_STATUS) {
2147 if (host->sg != NULL)
2148 dw_mci_read_data_pio(host, true);
2150 set_bit(EVENT_DATA_COMPLETE, &host->pending_events);
2151 tasklet_schedule(&host->tasklet);
2154 if (pending & SDMMC_INT_RXDR) {
2155 mci_writel(host, RINTSTS, SDMMC_INT_RXDR);
2156 if (host->dir_status == DW_MCI_RECV_STATUS && host->sg)
2157 dw_mci_read_data_pio(host, false);
2160 if (pending & SDMMC_INT_TXDR) {
2161 mci_writel(host, RINTSTS, SDMMC_INT_TXDR);
2162 if (host->dir_status == DW_MCI_SEND_STATUS && host->sg)
2163 dw_mci_write_data_pio(host);
2166 if (pending & SDMMC_INT_CMD_DONE) {
2167 mci_writel(host, RINTSTS, SDMMC_INT_CMD_DONE);
2168 dw_mci_cmd_interrupt(host, pending);
2171 if (pending & SDMMC_INT_CD) {
2172 mci_writel(host, RINTSTS, SDMMC_INT_CD);
2173 dw_mci_handle_cd(host);
2176 /* Handle SDIO Interrupts */
2177 for (i = 0; i < host->num_slots; i++) {
2178 struct dw_mci_slot *slot = host->slot[i];
2179 if (pending & SDMMC_INT_SDIO(slot->sdio_id)) {
2180 mci_writel(host, RINTSTS,
2181 SDMMC_INT_SDIO(slot->sdio_id));
2182 mmc_signal_sdio_irq(slot->mmc);
2188 #ifdef CONFIG_MMC_DW_IDMAC
2189 /* Handle DMA interrupts */
2190 if (host->dma_64bit_address == 1) {
2191 pending = mci_readl(host, IDSTS64);
2192 if (pending & (SDMMC_IDMAC_INT_TI | SDMMC_IDMAC_INT_RI)) {
2193 mci_writel(host, IDSTS64, SDMMC_IDMAC_INT_TI |
2194 SDMMC_IDMAC_INT_RI);
2195 mci_writel(host, IDSTS64, SDMMC_IDMAC_INT_NI);
2196 host->dma_ops->complete(host);
2199 pending = mci_readl(host, IDSTS);
2200 if (pending & (SDMMC_IDMAC_INT_TI | SDMMC_IDMAC_INT_RI)) {
2201 mci_writel(host, IDSTS, SDMMC_IDMAC_INT_TI |
2202 SDMMC_IDMAC_INT_RI);
2203 mci_writel(host, IDSTS, SDMMC_IDMAC_INT_NI);
2204 host->dma_ops->complete(host);
2213 /* given a slot id, find out the device node representing that slot */
2214 static struct device_node *dw_mci_of_find_slot_node(struct device *dev, u8 slot)
2216 struct device_node *np;
2220 if (!dev || !dev->of_node)
2223 for_each_child_of_node(dev->of_node, np) {
2224 addr = of_get_property(np, "reg", &len);
2225 if (!addr || (len < sizeof(int)))
2227 if (be32_to_cpup(addr) == slot)
2233 static struct dw_mci_of_slot_quirks {
2236 } of_slot_quirks[] = {
2238 .quirk = "disable-wp",
2239 .id = DW_MCI_SLOT_QUIRK_NO_WRITE_PROTECT,
2243 static int dw_mci_of_get_slot_quirks(struct device *dev, u8 slot)
2245 struct device_node *np = dw_mci_of_find_slot_node(dev, slot);
2250 for (idx = 0; idx < ARRAY_SIZE(of_slot_quirks); idx++)
2251 if (of_get_property(np, of_slot_quirks[idx].quirk, NULL)) {
2252 dev_warn(dev, "Slot quirk %s is deprecated\n",
2253 of_slot_quirks[idx].quirk);
2254 quirks |= of_slot_quirks[idx].id;
2259 #else /* CONFIG_OF */
2260 static int dw_mci_of_get_slot_quirks(struct device *dev, u8 slot)
2264 #endif /* CONFIG_OF */
2266 static int dw_mci_init_slot(struct dw_mci *host, unsigned int id)
2268 struct mmc_host *mmc;
2269 struct dw_mci_slot *slot;
2270 const struct dw_mci_drv_data *drv_data = host->drv_data;
2274 mmc = mmc_alloc_host(sizeof(struct dw_mci_slot), host->dev);
2278 slot = mmc_priv(mmc);
2280 slot->sdio_id = host->sdio_id0 + id;
2283 host->slot[id] = slot;
2285 slot->quirks = dw_mci_of_get_slot_quirks(host->dev, slot->id);
2287 mmc->ops = &dw_mci_ops;
2288 if (of_property_read_u32_array(host->dev->of_node,
2289 "clock-freq-min-max", freq, 2)) {
2290 mmc->f_min = DW_MCI_FREQ_MIN;
2291 mmc->f_max = DW_MCI_FREQ_MAX;
2293 mmc->f_min = freq[0];
2294 mmc->f_max = freq[1];
2297 /*if there are external regulators, get them*/
2298 ret = mmc_regulator_get_supply(mmc);
2299 if (ret == -EPROBE_DEFER)
2300 goto err_host_allocated;
2302 if (!mmc->ocr_avail)
2303 mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
2305 if (host->pdata->caps)
2306 mmc->caps = host->pdata->caps;
2308 if (host->pdata->pm_caps)
2309 mmc->pm_caps = host->pdata->pm_caps;
2311 if (host->dev->of_node) {
2312 ctrl_id = of_alias_get_id(host->dev->of_node, "mshc");
2316 ctrl_id = to_platform_device(host->dev)->id;
2318 if (drv_data && drv_data->caps)
2319 mmc->caps |= drv_data->caps[ctrl_id];
2321 if (host->pdata->caps2)
2322 mmc->caps2 = host->pdata->caps2;
2324 ret = mmc_of_parse(mmc);
2326 goto err_host_allocated;
2328 if (host->pdata->blk_settings) {
2329 mmc->max_segs = host->pdata->blk_settings->max_segs;
2330 mmc->max_blk_size = host->pdata->blk_settings->max_blk_size;
2331 mmc->max_blk_count = host->pdata->blk_settings->max_blk_count;
2332 mmc->max_req_size = host->pdata->blk_settings->max_req_size;
2333 mmc->max_seg_size = host->pdata->blk_settings->max_seg_size;
2335 /* Useful defaults if platform data is unset. */
2336 #ifdef CONFIG_MMC_DW_IDMAC
2337 mmc->max_segs = host->ring_size;
2338 mmc->max_blk_size = 65536;
2339 mmc->max_seg_size = 0x1000;
2340 mmc->max_req_size = mmc->max_seg_size * host->ring_size;
2341 mmc->max_blk_count = mmc->max_req_size / 512;
2344 mmc->max_blk_size = 65536; /* BLKSIZ is 16 bits */
2345 mmc->max_blk_count = 512;
2346 mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
2347 mmc->max_seg_size = mmc->max_req_size;
2348 #endif /* CONFIG_MMC_DW_IDMAC */
2351 if (dw_mci_get_cd(mmc))
2352 set_bit(DW_MMC_CARD_PRESENT, &slot->flags);
2354 clear_bit(DW_MMC_CARD_PRESENT, &slot->flags);
2356 ret = mmc_add_host(mmc);
2358 goto err_host_allocated;
2360 #if defined(CONFIG_DEBUG_FS)
2361 dw_mci_init_debugfs(slot);
2371 static void dw_mci_cleanup_slot(struct dw_mci_slot *slot, unsigned int id)
2373 /* Debugfs stuff is cleaned up by mmc core */
2374 mmc_remove_host(slot->mmc);
2375 slot->host->slot[id] = NULL;
2376 mmc_free_host(slot->mmc);
2379 static void dw_mci_init_dma(struct dw_mci *host)
2382 /* Check ADDR_CONFIG bit in HCON to find IDMAC address bus width */
2383 addr_config = (mci_readl(host, HCON) >> 27) & 0x01;
2385 if (addr_config == 1) {
2386 /* host supports IDMAC in 64-bit address mode */
2387 host->dma_64bit_address = 1;
2388 dev_info(host->dev, "IDMAC supports 64-bit address mode.\n");
2389 if (!dma_set_mask(host->dev, DMA_BIT_MASK(64)))
2390 dma_set_coherent_mask(host->dev, DMA_BIT_MASK(64));
2392 /* host supports IDMAC in 32-bit address mode */
2393 host->dma_64bit_address = 0;
2394 dev_info(host->dev, "IDMAC supports 32-bit address mode.\n");
2397 /* Alloc memory for sg translation */
2398 host->sg_cpu = dmam_alloc_coherent(host->dev, PAGE_SIZE,
2399 &host->sg_dma, GFP_KERNEL);
2400 if (!host->sg_cpu) {
2401 dev_err(host->dev, "%s: could not alloc DMA memory\n",
2406 /* Determine which DMA interface to use */
2407 #ifdef CONFIG_MMC_DW_IDMAC
2408 host->dma_ops = &dw_mci_idmac_ops;
2409 dev_info(host->dev, "Using internal DMA controller.\n");
2415 if (host->dma_ops->init && host->dma_ops->start &&
2416 host->dma_ops->stop && host->dma_ops->cleanup) {
2417 if (host->dma_ops->init(host)) {
2418 dev_err(host->dev, "%s: Unable to initialize "
2419 "DMA Controller.\n", __func__);
2423 dev_err(host->dev, "DMA initialization not found.\n");
2431 dev_info(host->dev, "Using PIO mode.\n");
2436 static bool dw_mci_ctrl_reset(struct dw_mci *host, u32 reset)
2438 unsigned long timeout = jiffies + msecs_to_jiffies(500);
2441 ctrl = mci_readl(host, CTRL);
2443 mci_writel(host, CTRL, ctrl);
2445 /* wait till resets clear */
2447 ctrl = mci_readl(host, CTRL);
2448 if (!(ctrl & reset))
2450 } while (time_before(jiffies, timeout));
2453 "Timeout resetting block (ctrl reset %#x)\n",
2459 static bool dw_mci_reset(struct dw_mci *host)
2461 u32 flags = SDMMC_CTRL_RESET | SDMMC_CTRL_FIFO_RESET;
2465 * Reseting generates a block interrupt, hence setting
2466 * the scatter-gather pointer to NULL.
2469 sg_miter_stop(&host->sg_miter);
2474 flags |= SDMMC_CTRL_DMA_RESET;
2476 if (dw_mci_ctrl_reset(host, flags)) {
2478 * In all cases we clear the RAWINTS register to clear any
2481 mci_writel(host, RINTSTS, 0xFFFFFFFF);
2483 /* if using dma we wait for dma_req to clear */
2484 if (host->use_dma) {
2485 unsigned long timeout = jiffies + msecs_to_jiffies(500);
2488 status = mci_readl(host, STATUS);
2489 if (!(status & SDMMC_STATUS_DMA_REQ))
2492 } while (time_before(jiffies, timeout));
2494 if (status & SDMMC_STATUS_DMA_REQ) {
2496 "%s: Timeout waiting for dma_req to "
2497 "clear during reset\n", __func__);
2501 /* when using DMA next we reset the fifo again */
2502 if (!dw_mci_ctrl_reset(host, SDMMC_CTRL_FIFO_RESET))
2506 /* if the controller reset bit did clear, then set clock regs */
2507 if (!(mci_readl(host, CTRL) & SDMMC_CTRL_RESET)) {
2508 dev_err(host->dev, "%s: fifo/dma reset bits didn't "
2509 "clear but ciu was reset, doing clock update\n",
2515 #if IS_ENABLED(CONFIG_MMC_DW_IDMAC)
2516 /* It is also recommended that we reset and reprogram idmac */
2517 dw_mci_idmac_reset(host);
2523 /* After a CTRL reset we need to have CIU set clock registers */
2524 mci_send_cmd(host->cur_slot, SDMMC_CMD_UPD_CLK, 0);
2530 static struct dw_mci_of_quirks {
2535 .quirk = "broken-cd",
2536 .id = DW_MCI_QUIRK_BROKEN_CARD_DETECTION,
2538 .quirk = "disable-wp",
2539 .id = DW_MCI_QUIRK_NO_WRITE_PROTECT,
2543 static struct dw_mci_board *dw_mci_parse_dt(struct dw_mci *host)
2545 struct dw_mci_board *pdata;
2546 struct device *dev = host->dev;
2547 struct device_node *np = dev->of_node;
2548 const struct dw_mci_drv_data *drv_data = host->drv_data;
2550 u32 clock_frequency;
2552 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
2554 return ERR_PTR(-ENOMEM);
2556 /* find out number of slots supported */
2557 if (of_property_read_u32(dev->of_node, "num-slots",
2558 &pdata->num_slots)) {
2559 dev_info(dev, "num-slots property not found, "
2560 "assuming 1 slot is available\n");
2561 pdata->num_slots = 1;
2565 for (idx = 0; idx < ARRAY_SIZE(of_quirks); idx++)
2566 if (of_get_property(np, of_quirks[idx].quirk, NULL))
2567 pdata->quirks |= of_quirks[idx].id;
2569 if (of_property_read_u32(np, "fifo-depth", &pdata->fifo_depth))
2570 dev_info(dev, "fifo-depth property not found, using "
2571 "value of FIFOTH register as default\n");
2573 of_property_read_u32(np, "card-detect-delay", &pdata->detect_delay_ms);
2575 if (!of_property_read_u32(np, "clock-frequency", &clock_frequency))
2576 pdata->bus_hz = clock_frequency;
2578 if (drv_data && drv_data->parse_dt) {
2579 ret = drv_data->parse_dt(host);
2581 return ERR_PTR(ret);
2584 if (of_find_property(np, "supports-highspeed", NULL))
2585 pdata->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED;
2590 #else /* CONFIG_OF */
2591 static struct dw_mci_board *dw_mci_parse_dt(struct dw_mci *host)
2593 return ERR_PTR(-EINVAL);
2595 #endif /* CONFIG_OF */
2597 int dw_mci_probe(struct dw_mci *host)
2599 const struct dw_mci_drv_data *drv_data = host->drv_data;
2600 int width, i, ret = 0;
2605 host->pdata = dw_mci_parse_dt(host);
2606 if (IS_ERR(host->pdata)) {
2607 dev_err(host->dev, "platform data not available\n");
2612 if (host->pdata->num_slots > 1) {
2614 "Platform data must supply num_slots.\n");
2618 host->biu_clk = devm_clk_get(host->dev, "biu");
2619 if (IS_ERR(host->biu_clk)) {
2620 dev_dbg(host->dev, "biu clock not available\n");
2622 ret = clk_prepare_enable(host->biu_clk);
2624 dev_err(host->dev, "failed to enable biu clock\n");
2629 host->ciu_clk = devm_clk_get(host->dev, "ciu");
2630 if (IS_ERR(host->ciu_clk)) {
2631 dev_dbg(host->dev, "ciu clock not available\n");
2632 host->bus_hz = host->pdata->bus_hz;
2634 ret = clk_prepare_enable(host->ciu_clk);
2636 dev_err(host->dev, "failed to enable ciu clock\n");
2640 if (host->pdata->bus_hz) {
2641 ret = clk_set_rate(host->ciu_clk, host->pdata->bus_hz);
2644 "Unable to set bus rate to %uHz\n",
2645 host->pdata->bus_hz);
2647 host->bus_hz = clk_get_rate(host->ciu_clk);
2650 if (!host->bus_hz) {
2652 "Platform data must supply bus speed\n");
2657 if (drv_data && drv_data->init) {
2658 ret = drv_data->init(host);
2661 "implementation specific init failed\n");
2666 if (drv_data && drv_data->setup_clock) {
2667 ret = drv_data->setup_clock(host);
2670 "implementation specific clock setup failed\n");
2675 host->quirks = host->pdata->quirks;
2677 spin_lock_init(&host->lock);
2678 spin_lock_init(&host->irq_lock);
2679 INIT_LIST_HEAD(&host->queue);
2682 * Get the host data width - this assumes that HCON has been set with
2683 * the correct values.
2685 i = (mci_readl(host, HCON) >> 7) & 0x7;
2687 host->push_data = dw_mci_push_data16;
2688 host->pull_data = dw_mci_pull_data16;
2690 host->data_shift = 1;
2691 } else if (i == 2) {
2692 host->push_data = dw_mci_push_data64;
2693 host->pull_data = dw_mci_pull_data64;
2695 host->data_shift = 3;
2697 /* Check for a reserved value, and warn if it is */
2699 "HCON reports a reserved host data width!\n"
2700 "Defaulting to 32-bit access.\n");
2701 host->push_data = dw_mci_push_data32;
2702 host->pull_data = dw_mci_pull_data32;
2704 host->data_shift = 2;
2707 /* Reset all blocks */
2708 if (!dw_mci_ctrl_reset(host, SDMMC_CTRL_ALL_RESET_FLAGS))
2711 host->dma_ops = host->pdata->dma_ops;
2712 dw_mci_init_dma(host);
2714 /* Clear the interrupts for the host controller */
2715 mci_writel(host, RINTSTS, 0xFFFFFFFF);
2716 mci_writel(host, INTMASK, 0); /* disable all mmc interrupt first */
2718 /* Put in max timeout */
2719 mci_writel(host, TMOUT, 0xFFFFFFFF);
2722 * FIFO threshold settings RxMark = fifo_size / 2 - 1,
2723 * Tx Mark = fifo_size / 2 DMA Size = 8
2725 if (!host->pdata->fifo_depth) {
2727 * Power-on value of RX_WMark is FIFO_DEPTH-1, but this may
2728 * have been overwritten by the bootloader, just like we're
2729 * about to do, so if you know the value for your hardware, you
2730 * should put it in the platform data.
2732 fifo_size = mci_readl(host, FIFOTH);
2733 fifo_size = 1 + ((fifo_size >> 16) & 0xfff);
2735 fifo_size = host->pdata->fifo_depth;
2737 host->fifo_depth = fifo_size;
2739 SDMMC_SET_FIFOTH(0x2, fifo_size / 2 - 1, fifo_size / 2);
2740 mci_writel(host, FIFOTH, host->fifoth_val);
2742 /* disable clock to CIU */
2743 mci_writel(host, CLKENA, 0);
2744 mci_writel(host, CLKSRC, 0);
2747 * In 2.40a spec, Data offset is changed.
2748 * Need to check the version-id and set data-offset for DATA register.
2750 host->verid = SDMMC_GET_VERID(mci_readl(host, VERID));
2751 dev_info(host->dev, "Version ID is %04x\n", host->verid);
2753 if (host->verid < DW_MMC_240A)
2754 host->data_offset = DATA_OFFSET;
2756 host->data_offset = DATA_240A_OFFSET;
2758 tasklet_init(&host->tasklet, dw_mci_tasklet_func, (unsigned long)host);
2759 ret = devm_request_irq(host->dev, host->irq, dw_mci_interrupt,
2760 host->irq_flags, "dw-mci", host);
2764 if (host->pdata->num_slots)
2765 host->num_slots = host->pdata->num_slots;
2767 host->num_slots = ((mci_readl(host, HCON) >> 1) & 0x1F) + 1;
2770 * Enable interrupts for command done, data over, data empty, card det,
2771 * receive ready and error such as transmit, receive timeout, crc error
2773 mci_writel(host, RINTSTS, 0xFFFFFFFF);
2774 mci_writel(host, INTMASK, SDMMC_INT_CMD_DONE | SDMMC_INT_DATA_OVER |
2775 SDMMC_INT_TXDR | SDMMC_INT_RXDR |
2776 DW_MCI_ERROR_FLAGS | SDMMC_INT_CD);
2777 mci_writel(host, CTRL, SDMMC_CTRL_INT_ENABLE); /* Enable mci interrupt */
2779 dev_info(host->dev, "DW MMC controller at irq %d, "
2780 "%d bit host data width, "
2782 host->irq, width, fifo_size);
2784 /* We need at least one slot to succeed */
2785 for (i = 0; i < host->num_slots; i++) {
2786 ret = dw_mci_init_slot(host, i);
2788 dev_dbg(host->dev, "slot %d init failed\n", i);
2794 dev_info(host->dev, "%d slots initialized\n", init_slots);
2796 dev_dbg(host->dev, "attempted to initialize %d slots, "
2797 "but failed on all\n", host->num_slots);
2801 if (host->quirks & DW_MCI_QUIRK_IDMAC_DTO)
2802 dev_info(host->dev, "Internal DMAC interrupt fix enabled.\n");
2807 if (host->use_dma && host->dma_ops->exit)
2808 host->dma_ops->exit(host);
2811 if (!IS_ERR(host->ciu_clk))
2812 clk_disable_unprepare(host->ciu_clk);
2815 if (!IS_ERR(host->biu_clk))
2816 clk_disable_unprepare(host->biu_clk);
2820 EXPORT_SYMBOL(dw_mci_probe);
2822 void dw_mci_remove(struct dw_mci *host)
2826 mci_writel(host, RINTSTS, 0xFFFFFFFF);
2827 mci_writel(host, INTMASK, 0); /* disable all mmc interrupt first */
2829 for (i = 0; i < host->num_slots; i++) {
2830 dev_dbg(host->dev, "remove slot %d\n", i);
2832 dw_mci_cleanup_slot(host->slot[i], i);
2835 /* disable clock to CIU */
2836 mci_writel(host, CLKENA, 0);
2837 mci_writel(host, CLKSRC, 0);
2839 if (host->use_dma && host->dma_ops->exit)
2840 host->dma_ops->exit(host);
2842 if (!IS_ERR(host->ciu_clk))
2843 clk_disable_unprepare(host->ciu_clk);
2845 if (!IS_ERR(host->biu_clk))
2846 clk_disable_unprepare(host->biu_clk);
2848 EXPORT_SYMBOL(dw_mci_remove);
2852 #ifdef CONFIG_PM_SLEEP
2854 * TODO: we should probably disable the clock to the card in the suspend path.
2856 int dw_mci_suspend(struct dw_mci *host)
2860 EXPORT_SYMBOL(dw_mci_suspend);
2862 int dw_mci_resume(struct dw_mci *host)
2866 if (!dw_mci_ctrl_reset(host, SDMMC_CTRL_ALL_RESET_FLAGS)) {
2871 if (host->use_dma && host->dma_ops->init)
2872 host->dma_ops->init(host);
2875 * Restore the initial value at FIFOTH register
2876 * And Invalidate the prev_blksz with zero
2878 mci_writel(host, FIFOTH, host->fifoth_val);
2879 host->prev_blksz = 0;
2881 /* Put in max timeout */
2882 mci_writel(host, TMOUT, 0xFFFFFFFF);
2884 mci_writel(host, RINTSTS, 0xFFFFFFFF);
2885 mci_writel(host, INTMASK, SDMMC_INT_CMD_DONE | SDMMC_INT_DATA_OVER |
2886 SDMMC_INT_TXDR | SDMMC_INT_RXDR |
2887 DW_MCI_ERROR_FLAGS | SDMMC_INT_CD);
2888 mci_writel(host, CTRL, SDMMC_CTRL_INT_ENABLE);
2890 for (i = 0; i < host->num_slots; i++) {
2891 struct dw_mci_slot *slot = host->slot[i];
2894 if (slot->mmc->pm_flags & MMC_PM_KEEP_POWER) {
2895 dw_mci_set_ios(slot->mmc, &slot->mmc->ios);
2896 dw_mci_setup_bus(slot, true);
2901 EXPORT_SYMBOL(dw_mci_resume);
2902 #endif /* CONFIG_PM_SLEEP */
2904 static int __init dw_mci_init(void)
2906 pr_info("Synopsys Designware Multimedia Card Interface Driver\n");
2910 static void __exit dw_mci_exit(void)
2914 module_init(dw_mci_init);
2915 module_exit(dw_mci_exit);
2917 MODULE_DESCRIPTION("DW Multimedia Card Interface driver");
2918 MODULE_AUTHOR("NXP Semiconductor VietNam");
2919 MODULE_AUTHOR("Imagination Technologies Ltd");
2920 MODULE_LICENSE("GPL v2");