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 & cpu_to_le32(0x03ffe000)) | \
73 ((cpu_to_le32(s)) & cpu_to_le32(0x1fff)))
75 u32 des3; /* Reserved */
77 u32 des4; /* Lower 32-bits of Buffer Address Pointer 1*/
78 u32 des5; /* Upper 32-bits of Buffer Address Pointer 1*/
80 u32 des6; /* Lower 32-bits of Next Descriptor Address */
81 u32 des7; /* Upper 32-bits of Next Descriptor Address */
85 __le32 des0; /* Control Descriptor */
86 #define IDMAC_DES0_DIC BIT(1)
87 #define IDMAC_DES0_LD BIT(2)
88 #define IDMAC_DES0_FD BIT(3)
89 #define IDMAC_DES0_CH BIT(4)
90 #define IDMAC_DES0_ER BIT(5)
91 #define IDMAC_DES0_CES BIT(30)
92 #define IDMAC_DES0_OWN BIT(31)
94 __le32 des1; /* Buffer sizes */
95 #define IDMAC_SET_BUFFER1_SIZE(d, s) \
96 ((d)->des1 = ((d)->des1 & 0x03ffe000) | ((s) & 0x1fff))
98 __le32 des2; /* buffer 1 physical address */
100 __le32 des3; /* buffer 2 physical address */
103 /* Each descriptor can transfer up to 4KB of data in chained mode */
104 #define DW_MCI_DESC_DATA_LENGTH 0x1000
105 #endif /* CONFIG_MMC_DW_IDMAC */
107 static bool dw_mci_reset(struct dw_mci *host);
108 static bool dw_mci_ctrl_reset(struct dw_mci *host, u32 reset);
109 static int dw_mci_card_busy(struct mmc_host *mmc);
111 #if defined(CONFIG_DEBUG_FS)
112 static int dw_mci_req_show(struct seq_file *s, void *v)
114 struct dw_mci_slot *slot = s->private;
115 struct mmc_request *mrq;
116 struct mmc_command *cmd;
117 struct mmc_command *stop;
118 struct mmc_data *data;
120 /* Make sure we get a consistent snapshot */
121 spin_lock_bh(&slot->host->lock);
131 "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n",
132 cmd->opcode, cmd->arg, cmd->flags,
133 cmd->resp[0], cmd->resp[1], cmd->resp[2],
134 cmd->resp[2], cmd->error);
136 seq_printf(s, "DATA %u / %u * %u flg %x err %d\n",
137 data->bytes_xfered, data->blocks,
138 data->blksz, data->flags, data->error);
141 "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n",
142 stop->opcode, stop->arg, stop->flags,
143 stop->resp[0], stop->resp[1], stop->resp[2],
144 stop->resp[2], stop->error);
147 spin_unlock_bh(&slot->host->lock);
152 static int dw_mci_req_open(struct inode *inode, struct file *file)
154 return single_open(file, dw_mci_req_show, inode->i_private);
157 static const struct file_operations dw_mci_req_fops = {
158 .owner = THIS_MODULE,
159 .open = dw_mci_req_open,
162 .release = single_release,
165 static int dw_mci_regs_show(struct seq_file *s, void *v)
167 seq_printf(s, "STATUS:\t0x%08x\n", SDMMC_STATUS);
168 seq_printf(s, "RINTSTS:\t0x%08x\n", SDMMC_RINTSTS);
169 seq_printf(s, "CMD:\t0x%08x\n", SDMMC_CMD);
170 seq_printf(s, "CTRL:\t0x%08x\n", SDMMC_CTRL);
171 seq_printf(s, "INTMASK:\t0x%08x\n", SDMMC_INTMASK);
172 seq_printf(s, "CLKENA:\t0x%08x\n", SDMMC_CLKENA);
177 static int dw_mci_regs_open(struct inode *inode, struct file *file)
179 return single_open(file, dw_mci_regs_show, inode->i_private);
182 static const struct file_operations dw_mci_regs_fops = {
183 .owner = THIS_MODULE,
184 .open = dw_mci_regs_open,
187 .release = single_release,
190 static void dw_mci_init_debugfs(struct dw_mci_slot *slot)
192 struct mmc_host *mmc = slot->mmc;
193 struct dw_mci *host = slot->host;
197 root = mmc->debugfs_root;
201 node = debugfs_create_file("regs", S_IRUSR, root, host,
206 node = debugfs_create_file("req", S_IRUSR, root, slot,
211 node = debugfs_create_u32("state", S_IRUSR, root, (u32 *)&host->state);
215 node = debugfs_create_x32("pending_events", S_IRUSR, root,
216 (u32 *)&host->pending_events);
220 node = debugfs_create_x32("completed_events", S_IRUSR, root,
221 (u32 *)&host->completed_events);
228 dev_err(&mmc->class_dev, "failed to initialize debugfs for slot\n");
230 #endif /* defined(CONFIG_DEBUG_FS) */
232 static void mci_send_cmd(struct dw_mci_slot *slot, u32 cmd, u32 arg);
234 static u32 dw_mci_prepare_command(struct mmc_host *mmc, struct mmc_command *cmd)
236 struct mmc_data *data;
237 struct dw_mci_slot *slot = mmc_priv(mmc);
238 struct dw_mci *host = slot->host;
239 const struct dw_mci_drv_data *drv_data = slot->host->drv_data;
242 cmd->error = -EINPROGRESS;
245 if (cmd->opcode == MMC_STOP_TRANSMISSION ||
246 cmd->opcode == MMC_GO_IDLE_STATE ||
247 cmd->opcode == MMC_GO_INACTIVE_STATE ||
248 (cmd->opcode == SD_IO_RW_DIRECT &&
249 ((cmd->arg >> 9) & 0x1FFFF) == SDIO_CCCR_ABORT))
250 cmdr |= SDMMC_CMD_STOP;
251 else if (cmd->opcode != MMC_SEND_STATUS && cmd->data)
252 cmdr |= SDMMC_CMD_PRV_DAT_WAIT;
254 if (cmd->opcode == SD_SWITCH_VOLTAGE) {
257 /* Special bit makes CMD11 not die */
258 cmdr |= SDMMC_CMD_VOLT_SWITCH;
260 /* Change state to continue to handle CMD11 weirdness */
261 WARN_ON(slot->host->state != STATE_SENDING_CMD);
262 slot->host->state = STATE_SENDING_CMD11;
265 * We need to disable low power mode (automatic clock stop)
266 * while doing voltage switch so we don't confuse the card,
267 * since stopping the clock is a specific part of the UHS
268 * voltage change dance.
270 * Note that low power mode (SDMMC_CLKEN_LOW_PWR) will be
271 * unconditionally turned back on in dw_mci_setup_bus() if it's
272 * ever called with a non-zero clock. That shouldn't happen
273 * until the voltage change is all done.
275 clk_en_a = mci_readl(host, CLKENA);
276 clk_en_a &= ~(SDMMC_CLKEN_LOW_PWR << slot->id);
277 mci_writel(host, CLKENA, clk_en_a);
278 mci_send_cmd(slot, SDMMC_CMD_UPD_CLK |
279 SDMMC_CMD_PRV_DAT_WAIT, 0);
282 if (cmd->flags & MMC_RSP_PRESENT) {
283 /* We expect a response, so set this bit */
284 cmdr |= SDMMC_CMD_RESP_EXP;
285 if (cmd->flags & MMC_RSP_136)
286 cmdr |= SDMMC_CMD_RESP_LONG;
289 if (cmd->flags & MMC_RSP_CRC)
290 cmdr |= SDMMC_CMD_RESP_CRC;
294 cmdr |= SDMMC_CMD_DAT_EXP;
295 if (data->flags & MMC_DATA_STREAM)
296 cmdr |= SDMMC_CMD_STRM_MODE;
297 if (data->flags & MMC_DATA_WRITE)
298 cmdr |= SDMMC_CMD_DAT_WR;
301 if (drv_data && drv_data->prepare_command)
302 drv_data->prepare_command(slot->host, &cmdr);
307 static u32 dw_mci_prep_stop_abort(struct dw_mci *host, struct mmc_command *cmd)
309 struct mmc_command *stop;
315 stop = &host->stop_abort;
317 memset(stop, 0, sizeof(struct mmc_command));
319 if (cmdr == MMC_READ_SINGLE_BLOCK ||
320 cmdr == MMC_READ_MULTIPLE_BLOCK ||
321 cmdr == MMC_WRITE_BLOCK ||
322 cmdr == MMC_WRITE_MULTIPLE_BLOCK ||
323 cmdr == MMC_SEND_TUNING_BLOCK ||
324 cmdr == MMC_SEND_TUNING_BLOCK_HS200) {
325 stop->opcode = MMC_STOP_TRANSMISSION;
327 stop->flags = MMC_RSP_R1B | MMC_CMD_AC;
328 } else if (cmdr == SD_IO_RW_EXTENDED) {
329 stop->opcode = SD_IO_RW_DIRECT;
330 stop->arg |= (1 << 31) | (0 << 28) | (SDIO_CCCR_ABORT << 9) |
331 ((cmd->arg >> 28) & 0x7);
332 stop->flags = MMC_RSP_SPI_R5 | MMC_RSP_R5 | MMC_CMD_AC;
337 cmdr = stop->opcode | SDMMC_CMD_STOP |
338 SDMMC_CMD_RESP_CRC | SDMMC_CMD_RESP_EXP;
343 static void dw_mci_wait_while_busy(struct dw_mci *host, u32 cmd_flags)
345 unsigned long timeout = jiffies + msecs_to_jiffies(500);
348 * Databook says that before issuing a new data transfer command
349 * we need to check to see if the card is busy. Data transfer commands
350 * all have SDMMC_CMD_PRV_DAT_WAIT set, so we'll key off that.
352 * ...also allow sending for SDMMC_CMD_VOLT_SWITCH where busy is
355 if ((cmd_flags & SDMMC_CMD_PRV_DAT_WAIT) &&
356 !(cmd_flags & SDMMC_CMD_VOLT_SWITCH)) {
357 while (mci_readl(host, STATUS) & SDMMC_STATUS_BUSY) {
358 if (time_after(jiffies, timeout)) {
359 /* Command will fail; we'll pass error then */
360 dev_err(host->dev, "Busy; trying anyway\n");
368 static void dw_mci_start_command(struct dw_mci *host,
369 struct mmc_command *cmd, u32 cmd_flags)
373 "start command: ARGR=0x%08x CMDR=0x%08x\n",
374 cmd->arg, cmd_flags);
376 mci_writel(host, CMDARG, cmd->arg);
377 wmb(); /* drain writebuffer */
378 dw_mci_wait_while_busy(host, cmd_flags);
380 mci_writel(host, CMD, cmd_flags | SDMMC_CMD_START);
383 static inline void send_stop_abort(struct dw_mci *host, struct mmc_data *data)
385 struct mmc_command *stop = data->stop ? data->stop : &host->stop_abort;
387 dw_mci_start_command(host, stop, host->stop_cmdr);
390 /* DMA interface functions */
391 static void dw_mci_stop_dma(struct dw_mci *host)
393 if (host->using_dma) {
394 host->dma_ops->stop(host);
395 host->dma_ops->cleanup(host);
398 /* Data transfer was stopped by the interrupt handler */
399 set_bit(EVENT_XFER_COMPLETE, &host->pending_events);
402 static int dw_mci_get_dma_dir(struct mmc_data *data)
404 if (data->flags & MMC_DATA_WRITE)
405 return DMA_TO_DEVICE;
407 return DMA_FROM_DEVICE;
410 #ifdef CONFIG_MMC_DW_IDMAC
411 static void dw_mci_dma_cleanup(struct dw_mci *host)
413 struct mmc_data *data = host->data;
416 if (!data->host_cookie)
417 dma_unmap_sg(host->dev,
420 dw_mci_get_dma_dir(data));
423 static void dw_mci_idmac_reset(struct dw_mci *host)
425 u32 bmod = mci_readl(host, BMOD);
426 /* Software reset of DMA */
427 bmod |= SDMMC_IDMAC_SWRESET;
428 mci_writel(host, BMOD, bmod);
431 static void dw_mci_idmac_stop_dma(struct dw_mci *host)
435 /* Disable and reset the IDMAC interface */
436 temp = mci_readl(host, CTRL);
437 temp &= ~SDMMC_CTRL_USE_IDMAC;
438 temp |= SDMMC_CTRL_DMA_RESET;
439 mci_writel(host, CTRL, temp);
441 /* Stop the IDMAC running */
442 temp = mci_readl(host, BMOD);
443 temp &= ~(SDMMC_IDMAC_ENABLE | SDMMC_IDMAC_FB);
444 temp |= SDMMC_IDMAC_SWRESET;
445 mci_writel(host, BMOD, temp);
448 static void dw_mci_idmac_complete_dma(struct dw_mci *host)
450 struct mmc_data *data = host->data;
452 dev_vdbg(host->dev, "DMA complete\n");
454 host->dma_ops->cleanup(host);
457 * If the card was removed, data will be NULL. No point in trying to
458 * send the stop command or waiting for NBUSY in this case.
461 set_bit(EVENT_XFER_COMPLETE, &host->pending_events);
462 tasklet_schedule(&host->tasklet);
466 static void dw_mci_translate_sglist(struct dw_mci *host, struct mmc_data *data,
469 unsigned int desc_len;
472 if (host->dma_64bit_address == 1) {
473 struct idmac_desc_64addr *desc_first, *desc_last, *desc;
475 desc_first = desc_last = desc = host->sg_cpu;
477 for (i = 0; i < sg_len; i++) {
478 unsigned int length = sg_dma_len(&data->sg[i]);
480 u64 mem_addr = sg_dma_address(&data->sg[i]);
482 for ( ; length ; desc++) {
483 desc_len = (length <= DW_MCI_DESC_DATA_LENGTH) ?
484 length : DW_MCI_DESC_DATA_LENGTH;
489 * Set the OWN bit and disable interrupts
490 * for this descriptor
492 desc->des0 = IDMAC_DES0_OWN | IDMAC_DES0_DIC |
496 IDMAC_64ADDR_SET_BUFFER1_SIZE(desc, desc_len);
498 /* Physical address to DMA to/from */
499 desc->des4 = mem_addr & 0xffffffff;
500 desc->des5 = mem_addr >> 32;
502 /* Update physical address for the next desc */
503 mem_addr += desc_len;
505 /* Save pointer to the last descriptor */
510 /* Set first descriptor */
511 desc_first->des0 |= IDMAC_DES0_FD;
513 /* Set last descriptor */
514 desc_last->des0 &= ~(IDMAC_DES0_CH | IDMAC_DES0_DIC);
515 desc_last->des0 |= IDMAC_DES0_LD;
518 struct idmac_desc *desc_first, *desc_last, *desc;
520 desc_first = desc_last = desc = host->sg_cpu;
522 for (i = 0; i < sg_len; i++) {
523 unsigned int length = sg_dma_len(&data->sg[i]);
525 u32 mem_addr = sg_dma_address(&data->sg[i]);
527 for ( ; length ; desc++) {
528 desc_len = (length <= DW_MCI_DESC_DATA_LENGTH) ?
529 length : DW_MCI_DESC_DATA_LENGTH;
534 * Set the OWN bit and disable interrupts
535 * for this descriptor
537 desc->des0 = cpu_to_le32(IDMAC_DES0_OWN |
542 IDMAC_SET_BUFFER1_SIZE(desc, desc_len);
544 /* Physical address to DMA to/from */
545 desc->des2 = cpu_to_le32(mem_addr);
547 /* Update physical address for the next desc */
548 mem_addr += desc_len;
550 /* Save pointer to the last descriptor */
555 /* Set first descriptor */
556 desc_first->des0 |= cpu_to_le32(IDMAC_DES0_FD);
558 /* Set last descriptor */
559 desc_last->des0 &= cpu_to_le32(~(IDMAC_DES0_CH |
561 desc_last->des0 |= cpu_to_le32(IDMAC_DES0_LD);
564 wmb(); /* drain writebuffer */
567 static void dw_mci_idmac_start_dma(struct dw_mci *host, unsigned int sg_len)
571 dw_mci_translate_sglist(host, host->data, sg_len);
573 /* Make sure to reset DMA in case we did PIO before this */
574 dw_mci_ctrl_reset(host, SDMMC_CTRL_DMA_RESET);
575 dw_mci_idmac_reset(host);
577 /* Select IDMAC interface */
578 temp = mci_readl(host, CTRL);
579 temp |= SDMMC_CTRL_USE_IDMAC;
580 mci_writel(host, CTRL, temp);
582 /* drain writebuffer */
585 /* Enable the IDMAC */
586 temp = mci_readl(host, BMOD);
587 temp |= SDMMC_IDMAC_ENABLE | SDMMC_IDMAC_FB;
588 mci_writel(host, BMOD, temp);
590 /* Start it running */
591 mci_writel(host, PLDMND, 1);
594 static int dw_mci_idmac_init(struct dw_mci *host)
598 if (host->dma_64bit_address == 1) {
599 struct idmac_desc_64addr *p;
600 /* Number of descriptors in the ring buffer */
601 host->ring_size = PAGE_SIZE / sizeof(struct idmac_desc_64addr);
603 /* Forward link the descriptor list */
604 for (i = 0, p = host->sg_cpu; i < host->ring_size - 1;
606 p->des6 = (host->sg_dma +
607 (sizeof(struct idmac_desc_64addr) *
608 (i + 1))) & 0xffffffff;
610 p->des7 = (u64)(host->sg_dma +
611 (sizeof(struct idmac_desc_64addr) *
613 /* Initialize reserved and buffer size fields to "0" */
619 /* Set the last descriptor as the end-of-ring descriptor */
620 p->des6 = host->sg_dma & 0xffffffff;
621 p->des7 = (u64)host->sg_dma >> 32;
622 p->des0 = IDMAC_DES0_ER;
625 struct idmac_desc *p;
626 /* Number of descriptors in the ring buffer */
627 host->ring_size = PAGE_SIZE / sizeof(struct idmac_desc);
629 /* Forward link the descriptor list */
630 for (i = 0, p = host->sg_cpu;
631 i < host->ring_size - 1;
633 p->des3 = cpu_to_le32(host->sg_dma +
634 (sizeof(struct idmac_desc) * (i + 1)));
638 /* Set the last descriptor as the end-of-ring descriptor */
639 p->des3 = cpu_to_le32(host->sg_dma);
640 p->des0 = cpu_to_le32(IDMAC_DES0_ER);
643 dw_mci_idmac_reset(host);
645 if (host->dma_64bit_address == 1) {
646 /* Mask out interrupts - get Tx & Rx complete only */
647 mci_writel(host, IDSTS64, IDMAC_INT_CLR);
648 mci_writel(host, IDINTEN64, SDMMC_IDMAC_INT_NI |
649 SDMMC_IDMAC_INT_RI | SDMMC_IDMAC_INT_TI);
651 /* Set the descriptor base address */
652 mci_writel(host, DBADDRL, host->sg_dma & 0xffffffff);
653 mci_writel(host, DBADDRU, (u64)host->sg_dma >> 32);
656 /* Mask out interrupts - get Tx & Rx complete only */
657 mci_writel(host, IDSTS, IDMAC_INT_CLR);
658 mci_writel(host, IDINTEN, SDMMC_IDMAC_INT_NI |
659 SDMMC_IDMAC_INT_RI | SDMMC_IDMAC_INT_TI);
661 /* Set the descriptor base address */
662 mci_writel(host, DBADDR, host->sg_dma);
668 static const struct dw_mci_dma_ops dw_mci_idmac_ops = {
669 .init = dw_mci_idmac_init,
670 .start = dw_mci_idmac_start_dma,
671 .stop = dw_mci_idmac_stop_dma,
672 .complete = dw_mci_idmac_complete_dma,
673 .cleanup = dw_mci_dma_cleanup,
675 #endif /* CONFIG_MMC_DW_IDMAC */
677 static int dw_mci_pre_dma_transfer(struct dw_mci *host,
678 struct mmc_data *data,
681 struct scatterlist *sg;
682 unsigned int i, sg_len;
684 if (!next && data->host_cookie)
685 return data->host_cookie;
688 * We don't do DMA on "complex" transfers, i.e. with
689 * non-word-aligned buffers or lengths. Also, we don't bother
690 * with all the DMA setup overhead for short transfers.
692 if (data->blocks * data->blksz < DW_MCI_DMA_THRESHOLD)
698 for_each_sg(data->sg, sg, data->sg_len, i) {
699 if (sg->offset & 3 || sg->length & 3)
703 sg_len = dma_map_sg(host->dev,
706 dw_mci_get_dma_dir(data));
711 data->host_cookie = sg_len;
716 static void dw_mci_pre_req(struct mmc_host *mmc,
717 struct mmc_request *mrq,
720 struct dw_mci_slot *slot = mmc_priv(mmc);
721 struct mmc_data *data = mrq->data;
723 if (!slot->host->use_dma || !data)
726 if (data->host_cookie) {
727 data->host_cookie = 0;
731 if (dw_mci_pre_dma_transfer(slot->host, mrq->data, 1) < 0)
732 data->host_cookie = 0;
735 static void dw_mci_post_req(struct mmc_host *mmc,
736 struct mmc_request *mrq,
739 struct dw_mci_slot *slot = mmc_priv(mmc);
740 struct mmc_data *data = mrq->data;
742 if (!slot->host->use_dma || !data)
745 if (data->host_cookie)
746 dma_unmap_sg(slot->host->dev,
749 dw_mci_get_dma_dir(data));
750 data->host_cookie = 0;
753 static void dw_mci_adjust_fifoth(struct dw_mci *host, struct mmc_data *data)
755 #ifdef CONFIG_MMC_DW_IDMAC
756 unsigned int blksz = data->blksz;
757 const u32 mszs[] = {1, 4, 8, 16, 32, 64, 128, 256};
758 u32 fifo_width = 1 << host->data_shift;
759 u32 blksz_depth = blksz / fifo_width, fifoth_val;
760 u32 msize = 0, rx_wmark = 1, tx_wmark, tx_wmark_invers;
761 int idx = ARRAY_SIZE(mszs) - 1;
763 tx_wmark = (host->fifo_depth) / 2;
764 tx_wmark_invers = host->fifo_depth - tx_wmark;
768 * if blksz is not a multiple of the FIFO width
770 if (blksz % fifo_width) {
777 if (!((blksz_depth % mszs[idx]) ||
778 (tx_wmark_invers % mszs[idx]))) {
780 rx_wmark = mszs[idx] - 1;
785 * If idx is '0', it won't be tried
786 * Thus, initial values are uesed
789 fifoth_val = SDMMC_SET_FIFOTH(msize, rx_wmark, tx_wmark);
790 mci_writel(host, FIFOTH, fifoth_val);
794 static void dw_mci_ctrl_rd_thld(struct dw_mci *host, struct mmc_data *data)
796 unsigned int blksz = data->blksz;
797 u32 blksz_depth, fifo_depth;
800 WARN_ON(!(data->flags & MMC_DATA_READ));
803 * CDTHRCTL doesn't exist prior to 240A (in fact that register offset is
804 * in the FIFO region, so we really shouldn't access it).
806 if (host->verid < DW_MMC_240A)
809 if (host->timing != MMC_TIMING_MMC_HS200 &&
810 host->timing != MMC_TIMING_MMC_HS400 &&
811 host->timing != MMC_TIMING_UHS_SDR104)
814 blksz_depth = blksz / (1 << host->data_shift);
815 fifo_depth = host->fifo_depth;
817 if (blksz_depth > fifo_depth)
821 * If (blksz_depth) >= (fifo_depth >> 1), should be 'thld_size <= blksz'
822 * If (blksz_depth) < (fifo_depth >> 1), should be thld_size = blksz
823 * Currently just choose blksz.
826 mci_writel(host, CDTHRCTL, SDMMC_SET_RD_THLD(thld_size, 1));
830 mci_writel(host, CDTHRCTL, SDMMC_SET_RD_THLD(0, 0));
833 static int dw_mci_submit_data_dma(struct dw_mci *host, struct mmc_data *data)
835 unsigned long irqflags;
841 /* If we don't have a channel, we can't do DMA */
845 sg_len = dw_mci_pre_dma_transfer(host, data, 0);
847 host->dma_ops->stop(host);
854 "sd sg_cpu: %#lx sg_dma: %#lx sg_len: %d\n",
855 (unsigned long)host->sg_cpu, (unsigned long)host->sg_dma,
859 * Decide the MSIZE and RX/TX Watermark.
860 * If current block size is same with previous size,
861 * no need to update fifoth.
863 if (host->prev_blksz != data->blksz)
864 dw_mci_adjust_fifoth(host, data);
866 /* Enable the DMA interface */
867 temp = mci_readl(host, CTRL);
868 temp |= SDMMC_CTRL_DMA_ENABLE;
869 mci_writel(host, CTRL, temp);
871 /* Disable RX/TX IRQs, let DMA handle it */
872 spin_lock_irqsave(&host->irq_lock, irqflags);
873 temp = mci_readl(host, INTMASK);
874 temp &= ~(SDMMC_INT_RXDR | SDMMC_INT_TXDR);
875 mci_writel(host, INTMASK, temp);
876 spin_unlock_irqrestore(&host->irq_lock, irqflags);
878 host->dma_ops->start(host, sg_len);
883 static void dw_mci_submit_data(struct dw_mci *host, struct mmc_data *data)
885 unsigned long irqflags;
886 int flags = SG_MITER_ATOMIC;
889 data->error = -EINPROGRESS;
895 if (data->flags & MMC_DATA_READ) {
896 host->dir_status = DW_MCI_RECV_STATUS;
897 dw_mci_ctrl_rd_thld(host, data);
899 host->dir_status = DW_MCI_SEND_STATUS;
902 if (dw_mci_submit_data_dma(host, data)) {
903 if (host->data->flags & MMC_DATA_READ)
904 flags |= SG_MITER_TO_SG;
906 flags |= SG_MITER_FROM_SG;
908 sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
910 host->part_buf_start = 0;
911 host->part_buf_count = 0;
913 mci_writel(host, RINTSTS, SDMMC_INT_TXDR | SDMMC_INT_RXDR);
915 spin_lock_irqsave(&host->irq_lock, irqflags);
916 temp = mci_readl(host, INTMASK);
917 temp |= SDMMC_INT_TXDR | SDMMC_INT_RXDR;
918 mci_writel(host, INTMASK, temp);
919 spin_unlock_irqrestore(&host->irq_lock, irqflags);
921 temp = mci_readl(host, CTRL);
922 temp &= ~SDMMC_CTRL_DMA_ENABLE;
923 mci_writel(host, CTRL, temp);
926 * Use the initial fifoth_val for PIO mode.
927 * If next issued data may be transfered by DMA mode,
928 * prev_blksz should be invalidated.
930 mci_writel(host, FIFOTH, host->fifoth_val);
931 host->prev_blksz = 0;
934 * Keep the current block size.
935 * It will be used to decide whether to update
936 * fifoth register next time.
938 host->prev_blksz = data->blksz;
942 static void mci_send_cmd(struct dw_mci_slot *slot, u32 cmd, u32 arg)
944 struct dw_mci *host = slot->host;
945 unsigned long timeout = jiffies + msecs_to_jiffies(500);
946 unsigned int cmd_status = 0;
948 mci_writel(host, CMDARG, arg);
949 wmb(); /* drain writebuffer */
950 dw_mci_wait_while_busy(host, cmd);
951 mci_writel(host, CMD, SDMMC_CMD_START | cmd);
953 while (time_before(jiffies, timeout)) {
954 cmd_status = mci_readl(host, CMD);
955 if (!(cmd_status & SDMMC_CMD_START))
958 dev_err(&slot->mmc->class_dev,
959 "Timeout sending command (cmd %#x arg %#x status %#x)\n",
960 cmd, arg, cmd_status);
963 static void dw_mci_setup_bus(struct dw_mci_slot *slot, bool force_clkinit)
965 struct dw_mci *host = slot->host;
966 unsigned int clock = slot->clock;
969 u32 sdmmc_cmd_bits = SDMMC_CMD_UPD_CLK | SDMMC_CMD_PRV_DAT_WAIT;
971 /* We must continue to set bit 28 in CMD until the change is complete */
972 if (host->state == STATE_WAITING_CMD11_DONE)
973 sdmmc_cmd_bits |= SDMMC_CMD_VOLT_SWITCH;
976 mci_writel(host, CLKENA, 0);
977 mci_send_cmd(slot, sdmmc_cmd_bits, 0);
978 } else if (clock != host->current_speed || force_clkinit) {
979 div = host->bus_hz / clock;
980 if (host->bus_hz % clock && host->bus_hz > clock)
982 * move the + 1 after the divide to prevent
983 * over-clocking the card.
987 div = (host->bus_hz != clock) ? DIV_ROUND_UP(div, 2) : 0;
989 if ((clock << div) != slot->__clk_old || force_clkinit)
990 dev_info(&slot->mmc->class_dev,
991 "Bus speed (slot %d) = %dHz (slot req %dHz, actual %dHZ div = %d)\n",
992 slot->id, host->bus_hz, clock,
993 div ? ((host->bus_hz / div) >> 1) :
997 mci_writel(host, CLKENA, 0);
998 mci_writel(host, CLKSRC, 0);
1001 mci_send_cmd(slot, sdmmc_cmd_bits, 0);
1003 /* set clock to desired speed */
1004 mci_writel(host, CLKDIV, div);
1007 mci_send_cmd(slot, sdmmc_cmd_bits, 0);
1009 /* enable clock; only low power if no SDIO */
1010 clk_en_a = SDMMC_CLKEN_ENABLE << slot->id;
1011 if (!test_bit(DW_MMC_CARD_NO_LOW_PWR, &slot->flags))
1012 clk_en_a |= SDMMC_CLKEN_LOW_PWR << slot->id;
1013 mci_writel(host, CLKENA, clk_en_a);
1016 mci_send_cmd(slot, sdmmc_cmd_bits, 0);
1018 /* keep the clock with reflecting clock dividor */
1019 slot->__clk_old = clock << div;
1022 host->current_speed = clock;
1024 /* Set the current slot bus width */
1025 mci_writel(host, CTYPE, (slot->ctype << slot->id));
1028 static void __dw_mci_start_request(struct dw_mci *host,
1029 struct dw_mci_slot *slot,
1030 struct mmc_command *cmd)
1032 struct mmc_request *mrq;
1033 struct mmc_data *data;
1038 host->cur_slot = slot;
1041 host->pending_events = 0;
1042 host->completed_events = 0;
1043 host->cmd_status = 0;
1044 host->data_status = 0;
1045 host->dir_status = 0;
1049 mci_writel(host, TMOUT, 0xFFFFFFFF);
1050 mci_writel(host, BYTCNT, data->blksz*data->blocks);
1051 mci_writel(host, BLKSIZ, data->blksz);
1054 cmdflags = dw_mci_prepare_command(slot->mmc, cmd);
1056 /* this is the first command, send the initialization clock */
1057 if (test_and_clear_bit(DW_MMC_CARD_NEED_INIT, &slot->flags))
1058 cmdflags |= SDMMC_CMD_INIT;
1061 dw_mci_submit_data(host, data);
1062 wmb(); /* drain writebuffer */
1065 dw_mci_start_command(host, cmd, cmdflags);
1067 if (cmd->opcode == SD_SWITCH_VOLTAGE) {
1068 unsigned long irqflags;
1071 * Databook says to fail after 2ms w/ no response, but evidence
1072 * shows that sometimes the cmd11 interrupt takes over 130ms.
1073 * We'll set to 500ms, plus an extra jiffy just in case jiffies
1074 * is just about to roll over.
1076 * We do this whole thing under spinlock and only if the
1077 * command hasn't already completed (indicating the the irq
1078 * already ran so we don't want the timeout).
1080 spin_lock_irqsave(&host->irq_lock, irqflags);
1081 if (!test_bit(EVENT_CMD_COMPLETE, &host->pending_events))
1082 mod_timer(&host->cmd11_timer,
1083 jiffies + msecs_to_jiffies(500) + 1);
1084 spin_unlock_irqrestore(&host->irq_lock, irqflags);
1088 host->stop_cmdr = dw_mci_prepare_command(slot->mmc, mrq->stop);
1090 host->stop_cmdr = dw_mci_prep_stop_abort(host, cmd);
1093 static void dw_mci_start_request(struct dw_mci *host,
1094 struct dw_mci_slot *slot)
1096 struct mmc_request *mrq = slot->mrq;
1097 struct mmc_command *cmd;
1099 cmd = mrq->sbc ? mrq->sbc : mrq->cmd;
1100 __dw_mci_start_request(host, slot, cmd);
1103 /* must be called with host->lock held */
1104 static void dw_mci_queue_request(struct dw_mci *host, struct dw_mci_slot *slot,
1105 struct mmc_request *mrq)
1107 dev_vdbg(&slot->mmc->class_dev, "queue request: state=%d\n",
1112 if (host->state == STATE_WAITING_CMD11_DONE) {
1113 dev_warn(&slot->mmc->class_dev,
1114 "Voltage change didn't complete\n");
1116 * this case isn't expected to happen, so we can
1117 * either crash here or just try to continue on
1118 * in the closest possible state
1120 host->state = STATE_IDLE;
1123 if (host->state == STATE_IDLE) {
1124 host->state = STATE_SENDING_CMD;
1125 dw_mci_start_request(host, slot);
1127 list_add_tail(&slot->queue_node, &host->queue);
1131 static void dw_mci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1133 struct dw_mci_slot *slot = mmc_priv(mmc);
1134 struct dw_mci *host = slot->host;
1139 * The check for card presence and queueing of the request must be
1140 * atomic, otherwise the card could be removed in between and the
1141 * request wouldn't fail until another card was inserted.
1143 spin_lock_bh(&host->lock);
1145 if (!test_bit(DW_MMC_CARD_PRESENT, &slot->flags)) {
1146 spin_unlock_bh(&host->lock);
1147 mrq->cmd->error = -ENOMEDIUM;
1148 mmc_request_done(mmc, mrq);
1152 dw_mci_queue_request(host, slot, mrq);
1154 spin_unlock_bh(&host->lock);
1157 static void dw_mci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1159 struct dw_mci_slot *slot = mmc_priv(mmc);
1160 const struct dw_mci_drv_data *drv_data = slot->host->drv_data;
1164 switch (ios->bus_width) {
1165 case MMC_BUS_WIDTH_4:
1166 slot->ctype = SDMMC_CTYPE_4BIT;
1168 case MMC_BUS_WIDTH_8:
1169 slot->ctype = SDMMC_CTYPE_8BIT;
1172 /* set default 1 bit mode */
1173 slot->ctype = SDMMC_CTYPE_1BIT;
1176 regs = mci_readl(slot->host, UHS_REG);
1179 if (ios->timing == MMC_TIMING_MMC_DDR52 ||
1180 ios->timing == MMC_TIMING_MMC_HS400)
1181 regs |= ((0x1 << slot->id) << 16);
1183 regs &= ~((0x1 << slot->id) << 16);
1185 mci_writel(slot->host, UHS_REG, regs);
1186 slot->host->timing = ios->timing;
1189 * Use mirror of ios->clock to prevent race with mmc
1190 * core ios update when finding the minimum.
1192 slot->clock = ios->clock;
1194 if (drv_data && drv_data->set_ios)
1195 drv_data->set_ios(slot->host, ios);
1197 switch (ios->power_mode) {
1199 if (!IS_ERR(mmc->supply.vmmc)) {
1200 ret = mmc_regulator_set_ocr(mmc, mmc->supply.vmmc,
1203 dev_err(slot->host->dev,
1204 "failed to enable vmmc regulator\n");
1205 /*return, if failed turn on vmmc*/
1209 set_bit(DW_MMC_CARD_NEED_INIT, &slot->flags);
1210 regs = mci_readl(slot->host, PWREN);
1211 regs |= (1 << slot->id);
1212 mci_writel(slot->host, PWREN, regs);
1215 if (!slot->host->vqmmc_enabled) {
1216 if (!IS_ERR(mmc->supply.vqmmc)) {
1217 ret = regulator_enable(mmc->supply.vqmmc);
1219 dev_err(slot->host->dev,
1220 "failed to enable vqmmc\n");
1222 slot->host->vqmmc_enabled = true;
1225 /* Keep track so we don't reset again */
1226 slot->host->vqmmc_enabled = true;
1229 /* Reset our state machine after powering on */
1230 dw_mci_ctrl_reset(slot->host,
1231 SDMMC_CTRL_ALL_RESET_FLAGS);
1234 /* Adjust clock / bus width after power is up */
1235 dw_mci_setup_bus(slot, false);
1239 /* Turn clock off before power goes down */
1240 dw_mci_setup_bus(slot, false);
1242 if (!IS_ERR(mmc->supply.vmmc))
1243 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0);
1245 if (!IS_ERR(mmc->supply.vqmmc) && slot->host->vqmmc_enabled)
1246 regulator_disable(mmc->supply.vqmmc);
1247 slot->host->vqmmc_enabled = false;
1249 regs = mci_readl(slot->host, PWREN);
1250 regs &= ~(1 << slot->id);
1251 mci_writel(slot->host, PWREN, regs);
1257 if (slot->host->state == STATE_WAITING_CMD11_DONE && ios->clock != 0)
1258 slot->host->state = STATE_IDLE;
1261 static int dw_mci_card_busy(struct mmc_host *mmc)
1263 struct dw_mci_slot *slot = mmc_priv(mmc);
1267 * Check the busy bit which is low when DAT[3:0]
1268 * (the data lines) are 0000
1270 status = mci_readl(slot->host, STATUS);
1272 return !!(status & SDMMC_STATUS_BUSY);
1275 static int dw_mci_switch_voltage(struct mmc_host *mmc, struct mmc_ios *ios)
1277 struct dw_mci_slot *slot = mmc_priv(mmc);
1278 struct dw_mci *host = slot->host;
1279 const struct dw_mci_drv_data *drv_data = host->drv_data;
1281 u32 v18 = SDMMC_UHS_18V << slot->id;
1285 if (drv_data && drv_data->switch_voltage)
1286 return drv_data->switch_voltage(mmc, ios);
1289 * Program the voltage. Note that some instances of dw_mmc may use
1290 * the UHS_REG for this. For other instances (like exynos) the UHS_REG
1291 * does no harm but you need to set the regulator directly. Try both.
1293 uhs = mci_readl(host, UHS_REG);
1294 if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_330) {
1303 if (!IS_ERR(mmc->supply.vqmmc)) {
1304 ret = regulator_set_voltage(mmc->supply.vqmmc, min_uv, max_uv);
1307 dev_dbg(&mmc->class_dev,
1308 "Regulator set error %d: %d - %d\n",
1309 ret, min_uv, max_uv);
1313 mci_writel(host, UHS_REG, uhs);
1318 static int dw_mci_get_ro(struct mmc_host *mmc)
1321 struct dw_mci_slot *slot = mmc_priv(mmc);
1322 int gpio_ro = mmc_gpio_get_ro(mmc);
1324 /* Use platform get_ro function, else try on board write protect */
1325 if (!IS_ERR_VALUE(gpio_ro))
1326 read_only = gpio_ro;
1329 mci_readl(slot->host, WRTPRT) & (1 << slot->id) ? 1 : 0;
1331 dev_dbg(&mmc->class_dev, "card is %s\n",
1332 read_only ? "read-only" : "read-write");
1337 static int dw_mci_get_cd(struct mmc_host *mmc)
1340 struct dw_mci_slot *slot = mmc_priv(mmc);
1341 struct dw_mci_board *brd = slot->host->pdata;
1342 struct dw_mci *host = slot->host;
1343 int gpio_cd = mmc_gpio_get_cd(mmc);
1345 /* Use platform get_cd function, else try onboard card detect */
1346 if ((brd->quirks & DW_MCI_QUIRK_BROKEN_CARD_DETECTION) ||
1347 (mmc->caps & MMC_CAP_NONREMOVABLE))
1349 else if (!IS_ERR_VALUE(gpio_cd))
1352 present = (mci_readl(slot->host, CDETECT) & (1 << slot->id))
1355 spin_lock_bh(&host->lock);
1357 set_bit(DW_MMC_CARD_PRESENT, &slot->flags);
1358 dev_dbg(&mmc->class_dev, "card is present\n");
1360 clear_bit(DW_MMC_CARD_PRESENT, &slot->flags);
1361 dev_dbg(&mmc->class_dev, "card is not present\n");
1363 spin_unlock_bh(&host->lock);
1368 static void dw_mci_init_card(struct mmc_host *mmc, struct mmc_card *card)
1370 struct dw_mci_slot *slot = mmc_priv(mmc);
1371 struct dw_mci *host = slot->host;
1374 * Low power mode will stop the card clock when idle. According to the
1375 * description of the CLKENA register we should disable low power mode
1376 * for SDIO cards if we need SDIO interrupts to work.
1378 if (mmc->caps & MMC_CAP_SDIO_IRQ) {
1379 const u32 clken_low_pwr = SDMMC_CLKEN_LOW_PWR << slot->id;
1383 clk_en_a_old = mci_readl(host, CLKENA);
1385 if (card->type == MMC_TYPE_SDIO ||
1386 card->type == MMC_TYPE_SD_COMBO) {
1387 set_bit(DW_MMC_CARD_NO_LOW_PWR, &slot->flags);
1388 clk_en_a = clk_en_a_old & ~clken_low_pwr;
1390 clear_bit(DW_MMC_CARD_NO_LOW_PWR, &slot->flags);
1391 clk_en_a = clk_en_a_old | clken_low_pwr;
1394 if (clk_en_a != clk_en_a_old) {
1395 mci_writel(host, CLKENA, clk_en_a);
1396 mci_send_cmd(slot, SDMMC_CMD_UPD_CLK |
1397 SDMMC_CMD_PRV_DAT_WAIT, 0);
1402 static void dw_mci_enable_sdio_irq(struct mmc_host *mmc, int enb)
1404 struct dw_mci_slot *slot = mmc_priv(mmc);
1405 struct dw_mci *host = slot->host;
1406 unsigned long irqflags;
1409 spin_lock_irqsave(&host->irq_lock, irqflags);
1411 /* Enable/disable Slot Specific SDIO interrupt */
1412 int_mask = mci_readl(host, INTMASK);
1414 int_mask |= SDMMC_INT_SDIO(slot->sdio_id);
1416 int_mask &= ~SDMMC_INT_SDIO(slot->sdio_id);
1417 mci_writel(host, INTMASK, int_mask);
1419 spin_unlock_irqrestore(&host->irq_lock, irqflags);
1422 static int dw_mci_execute_tuning(struct mmc_host *mmc, u32 opcode)
1424 struct dw_mci_slot *slot = mmc_priv(mmc);
1425 struct dw_mci *host = slot->host;
1426 const struct dw_mci_drv_data *drv_data = host->drv_data;
1429 if (drv_data && drv_data->execute_tuning)
1430 err = drv_data->execute_tuning(slot);
1434 static int dw_mci_prepare_hs400_tuning(struct mmc_host *mmc,
1435 struct mmc_ios *ios)
1437 struct dw_mci_slot *slot = mmc_priv(mmc);
1438 struct dw_mci *host = slot->host;
1439 const struct dw_mci_drv_data *drv_data = host->drv_data;
1441 if (drv_data && drv_data->prepare_hs400_tuning)
1442 return drv_data->prepare_hs400_tuning(host, ios);
1447 static const struct mmc_host_ops dw_mci_ops = {
1448 .request = dw_mci_request,
1449 .pre_req = dw_mci_pre_req,
1450 .post_req = dw_mci_post_req,
1451 .set_ios = dw_mci_set_ios,
1452 .get_ro = dw_mci_get_ro,
1453 .get_cd = dw_mci_get_cd,
1454 .enable_sdio_irq = dw_mci_enable_sdio_irq,
1455 .execute_tuning = dw_mci_execute_tuning,
1456 .card_busy = dw_mci_card_busy,
1457 .start_signal_voltage_switch = dw_mci_switch_voltage,
1458 .init_card = dw_mci_init_card,
1459 .prepare_hs400_tuning = dw_mci_prepare_hs400_tuning,
1462 static void dw_mci_request_end(struct dw_mci *host, struct mmc_request *mrq)
1463 __releases(&host->lock)
1464 __acquires(&host->lock)
1466 struct dw_mci_slot *slot;
1467 struct mmc_host *prev_mmc = host->cur_slot->mmc;
1469 WARN_ON(host->cmd || host->data);
1471 host->cur_slot->mrq = NULL;
1473 if (!list_empty(&host->queue)) {
1474 slot = list_entry(host->queue.next,
1475 struct dw_mci_slot, queue_node);
1476 list_del(&slot->queue_node);
1477 dev_vdbg(host->dev, "list not empty: %s is next\n",
1478 mmc_hostname(slot->mmc));
1479 host->state = STATE_SENDING_CMD;
1480 dw_mci_start_request(host, slot);
1482 dev_vdbg(host->dev, "list empty\n");
1484 if (host->state == STATE_SENDING_CMD11)
1485 host->state = STATE_WAITING_CMD11_DONE;
1487 host->state = STATE_IDLE;
1490 spin_unlock(&host->lock);
1491 mmc_request_done(prev_mmc, mrq);
1492 spin_lock(&host->lock);
1495 static int dw_mci_command_complete(struct dw_mci *host, struct mmc_command *cmd)
1497 u32 status = host->cmd_status;
1499 host->cmd_status = 0;
1501 /* Read the response from the card (up to 16 bytes) */
1502 if (cmd->flags & MMC_RSP_PRESENT) {
1503 if (cmd->flags & MMC_RSP_136) {
1504 cmd->resp[3] = mci_readl(host, RESP0);
1505 cmd->resp[2] = mci_readl(host, RESP1);
1506 cmd->resp[1] = mci_readl(host, RESP2);
1507 cmd->resp[0] = mci_readl(host, RESP3);
1509 cmd->resp[0] = mci_readl(host, RESP0);
1516 if (status & SDMMC_INT_RTO)
1517 cmd->error = -ETIMEDOUT;
1518 else if ((cmd->flags & MMC_RSP_CRC) && (status & SDMMC_INT_RCRC))
1519 cmd->error = -EILSEQ;
1520 else if (status & SDMMC_INT_RESP_ERR)
1526 /* newer ip versions need a delay between retries */
1527 if (host->quirks & DW_MCI_QUIRK_RETRY_DELAY)
1534 static int dw_mci_data_complete(struct dw_mci *host, struct mmc_data *data)
1536 u32 status = host->data_status;
1538 if (status & DW_MCI_DATA_ERROR_FLAGS) {
1539 if (status & SDMMC_INT_DRTO) {
1540 data->error = -ETIMEDOUT;
1541 } else if (status & SDMMC_INT_DCRC) {
1542 data->error = -EILSEQ;
1543 } else if (status & SDMMC_INT_EBE) {
1544 if (host->dir_status ==
1545 DW_MCI_SEND_STATUS) {
1547 * No data CRC status was returned.
1548 * The number of bytes transferred
1549 * will be exaggerated in PIO mode.
1551 data->bytes_xfered = 0;
1552 data->error = -ETIMEDOUT;
1553 } else if (host->dir_status ==
1554 DW_MCI_RECV_STATUS) {
1558 /* SDMMC_INT_SBE is included */
1562 dev_dbg(host->dev, "data error, status 0x%08x\n", status);
1565 * After an error, there may be data lingering
1570 data->bytes_xfered = data->blocks * data->blksz;
1577 static void dw_mci_set_drto(struct dw_mci *host)
1579 unsigned int drto_clks;
1580 unsigned int drto_ms;
1582 drto_clks = mci_readl(host, TMOUT) >> 8;
1583 drto_ms = DIV_ROUND_UP(drto_clks, host->bus_hz / 1000);
1585 /* add a bit spare time */
1588 mod_timer(&host->dto_timer, jiffies + msecs_to_jiffies(drto_ms));
1591 static void dw_mci_tasklet_func(unsigned long priv)
1593 struct dw_mci *host = (struct dw_mci *)priv;
1594 struct mmc_data *data;
1595 struct mmc_command *cmd;
1596 struct mmc_request *mrq;
1597 enum dw_mci_state state;
1598 enum dw_mci_state prev_state;
1601 spin_lock(&host->lock);
1603 state = host->state;
1612 case STATE_WAITING_CMD11_DONE:
1615 case STATE_SENDING_CMD11:
1616 case STATE_SENDING_CMD:
1617 if (!test_and_clear_bit(EVENT_CMD_COMPLETE,
1618 &host->pending_events))
1623 set_bit(EVENT_CMD_COMPLETE, &host->completed_events);
1624 err = dw_mci_command_complete(host, cmd);
1625 if (cmd == mrq->sbc && !err) {
1626 prev_state = state = STATE_SENDING_CMD;
1627 __dw_mci_start_request(host, host->cur_slot,
1632 if (cmd->data && err) {
1633 dw_mci_stop_dma(host);
1634 send_stop_abort(host, data);
1635 state = STATE_SENDING_STOP;
1639 if (!cmd->data || err) {
1640 dw_mci_request_end(host, mrq);
1644 prev_state = state = STATE_SENDING_DATA;
1647 case STATE_SENDING_DATA:
1649 * We could get a data error and never a transfer
1650 * complete so we'd better check for it here.
1652 * Note that we don't really care if we also got a
1653 * transfer complete; stopping the DMA and sending an
1656 if (test_and_clear_bit(EVENT_DATA_ERROR,
1657 &host->pending_events)) {
1658 dw_mci_stop_dma(host);
1660 !(host->data_status & (SDMMC_INT_DRTO |
1662 send_stop_abort(host, data);
1663 state = STATE_DATA_ERROR;
1667 if (!test_and_clear_bit(EVENT_XFER_COMPLETE,
1668 &host->pending_events)) {
1670 * If all data-related interrupts don't come
1671 * within the given time in reading data state.
1673 if ((host->quirks & DW_MCI_QUIRK_BROKEN_DTO) &&
1674 (host->dir_status == DW_MCI_RECV_STATUS))
1675 dw_mci_set_drto(host);
1679 set_bit(EVENT_XFER_COMPLETE, &host->completed_events);
1682 * Handle an EVENT_DATA_ERROR that might have shown up
1683 * before the transfer completed. This might not have
1684 * been caught by the check above because the interrupt
1685 * could have gone off between the previous check and
1686 * the check for transfer complete.
1688 * Technically this ought not be needed assuming we
1689 * get a DATA_COMPLETE eventually (we'll notice the
1690 * error and end the request), but it shouldn't hurt.
1692 * This has the advantage of sending the stop command.
1694 if (test_and_clear_bit(EVENT_DATA_ERROR,
1695 &host->pending_events)) {
1696 dw_mci_stop_dma(host);
1698 !(host->data_status & (SDMMC_INT_DRTO |
1700 send_stop_abort(host, data);
1701 state = STATE_DATA_ERROR;
1704 prev_state = state = STATE_DATA_BUSY;
1708 case STATE_DATA_BUSY:
1709 if (!test_and_clear_bit(EVENT_DATA_COMPLETE,
1710 &host->pending_events)) {
1712 * If data error interrupt comes but data over
1713 * interrupt doesn't come within the given time.
1714 * in reading data state.
1716 if ((host->quirks & DW_MCI_QUIRK_BROKEN_DTO) &&
1717 (host->dir_status == DW_MCI_RECV_STATUS))
1718 dw_mci_set_drto(host);
1723 set_bit(EVENT_DATA_COMPLETE, &host->completed_events);
1724 err = dw_mci_data_complete(host, data);
1727 if (!data->stop || mrq->sbc) {
1728 if (mrq->sbc && data->stop)
1729 data->stop->error = 0;
1730 dw_mci_request_end(host, mrq);
1734 /* stop command for open-ended transfer*/
1736 send_stop_abort(host, data);
1739 * If we don't have a command complete now we'll
1740 * never get one since we just reset everything;
1741 * better end the request.
1743 * If we do have a command complete we'll fall
1744 * through to the SENDING_STOP command and
1745 * everything will be peachy keen.
1747 if (!test_bit(EVENT_CMD_COMPLETE,
1748 &host->pending_events)) {
1750 dw_mci_request_end(host, mrq);
1756 * If err has non-zero,
1757 * stop-abort command has been already issued.
1759 prev_state = state = STATE_SENDING_STOP;
1763 case STATE_SENDING_STOP:
1764 if (!test_and_clear_bit(EVENT_CMD_COMPLETE,
1765 &host->pending_events))
1768 /* CMD error in data command */
1769 if (mrq->cmd->error && mrq->data)
1776 dw_mci_command_complete(host, mrq->stop);
1778 host->cmd_status = 0;
1780 dw_mci_request_end(host, mrq);
1783 case STATE_DATA_ERROR:
1784 if (!test_and_clear_bit(EVENT_XFER_COMPLETE,
1785 &host->pending_events))
1788 state = STATE_DATA_BUSY;
1791 } while (state != prev_state);
1793 host->state = state;
1795 spin_unlock(&host->lock);
1799 /* push final bytes to part_buf, only use during push */
1800 static void dw_mci_set_part_bytes(struct dw_mci *host, void *buf, int cnt)
1802 memcpy((void *)&host->part_buf, buf, cnt);
1803 host->part_buf_count = cnt;
1806 /* append bytes to part_buf, only use during push */
1807 static int dw_mci_push_part_bytes(struct dw_mci *host, void *buf, int cnt)
1809 cnt = min(cnt, (1 << host->data_shift) - host->part_buf_count);
1810 memcpy((void *)&host->part_buf + host->part_buf_count, buf, cnt);
1811 host->part_buf_count += cnt;
1815 /* pull first bytes from part_buf, only use during pull */
1816 static int dw_mci_pull_part_bytes(struct dw_mci *host, void *buf, int cnt)
1818 cnt = min_t(int, cnt, host->part_buf_count);
1820 memcpy(buf, (void *)&host->part_buf + host->part_buf_start,
1822 host->part_buf_count -= cnt;
1823 host->part_buf_start += cnt;
1828 /* pull final bytes from the part_buf, assuming it's just been filled */
1829 static void dw_mci_pull_final_bytes(struct dw_mci *host, void *buf, int cnt)
1831 memcpy(buf, &host->part_buf, cnt);
1832 host->part_buf_start = cnt;
1833 host->part_buf_count = (1 << host->data_shift) - cnt;
1836 static void dw_mci_push_data16(struct dw_mci *host, void *buf, int cnt)
1838 struct mmc_data *data = host->data;
1841 /* try and push anything in the part_buf */
1842 if (unlikely(host->part_buf_count)) {
1843 int len = dw_mci_push_part_bytes(host, buf, cnt);
1847 if (host->part_buf_count == 2) {
1848 mci_fifo_writew(host->fifo_reg, host->part_buf16);
1849 host->part_buf_count = 0;
1852 #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
1853 if (unlikely((unsigned long)buf & 0x1)) {
1855 u16 aligned_buf[64];
1856 int len = min(cnt & -2, (int)sizeof(aligned_buf));
1857 int items = len >> 1;
1859 /* memcpy from input buffer into aligned buffer */
1860 memcpy(aligned_buf, buf, len);
1863 /* push data from aligned buffer into fifo */
1864 for (i = 0; i < items; ++i)
1865 mci_fifo_writew(host->fifo_reg, aligned_buf[i]);
1872 for (; cnt >= 2; cnt -= 2)
1873 mci_fifo_writew(host->fifo_reg, *pdata++);
1876 /* put anything remaining in the part_buf */
1878 dw_mci_set_part_bytes(host, buf, cnt);
1879 /* Push data if we have reached the expected data length */
1880 if ((data->bytes_xfered + init_cnt) ==
1881 (data->blksz * data->blocks))
1882 mci_fifo_writew(host->fifo_reg, host->part_buf16);
1886 static void dw_mci_pull_data16(struct dw_mci *host, void *buf, int cnt)
1888 #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
1889 if (unlikely((unsigned long)buf & 0x1)) {
1891 /* pull data from fifo into aligned buffer */
1892 u16 aligned_buf[64];
1893 int len = min(cnt & -2, (int)sizeof(aligned_buf));
1894 int items = len >> 1;
1897 for (i = 0; i < items; ++i)
1898 aligned_buf[i] = mci_fifo_readw(host->fifo_reg);
1899 /* memcpy from aligned buffer into output buffer */
1900 memcpy(buf, aligned_buf, len);
1909 for (; cnt >= 2; cnt -= 2)
1910 *pdata++ = mci_fifo_readw(host->fifo_reg);
1914 host->part_buf16 = mci_fifo_readw(host->fifo_reg);
1915 dw_mci_pull_final_bytes(host, buf, cnt);
1919 static void dw_mci_push_data32(struct dw_mci *host, void *buf, int cnt)
1921 struct mmc_data *data = host->data;
1924 /* try and push anything in the part_buf */
1925 if (unlikely(host->part_buf_count)) {
1926 int len = dw_mci_push_part_bytes(host, buf, cnt);
1930 if (host->part_buf_count == 4) {
1931 mci_fifo_writel(host->fifo_reg, host->part_buf32);
1932 host->part_buf_count = 0;
1935 #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
1936 if (unlikely((unsigned long)buf & 0x3)) {
1938 u32 aligned_buf[32];
1939 int len = min(cnt & -4, (int)sizeof(aligned_buf));
1940 int items = len >> 2;
1942 /* memcpy from input buffer into aligned buffer */
1943 memcpy(aligned_buf, buf, len);
1946 /* push data from aligned buffer into fifo */
1947 for (i = 0; i < items; ++i)
1948 mci_fifo_writel(host->fifo_reg, aligned_buf[i]);
1955 for (; cnt >= 4; cnt -= 4)
1956 mci_fifo_writel(host->fifo_reg, *pdata++);
1959 /* put anything remaining in the part_buf */
1961 dw_mci_set_part_bytes(host, buf, cnt);
1962 /* Push data if we have reached the expected data length */
1963 if ((data->bytes_xfered + init_cnt) ==
1964 (data->blksz * data->blocks))
1965 mci_fifo_writel(host->fifo_reg, host->part_buf32);
1969 static void dw_mci_pull_data32(struct dw_mci *host, void *buf, int cnt)
1971 #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
1972 if (unlikely((unsigned long)buf & 0x3)) {
1974 /* pull data from fifo into aligned buffer */
1975 u32 aligned_buf[32];
1976 int len = min(cnt & -4, (int)sizeof(aligned_buf));
1977 int items = len >> 2;
1980 for (i = 0; i < items; ++i)
1981 aligned_buf[i] = mci_fifo_readl(host->fifo_reg);
1982 /* memcpy from aligned buffer into output buffer */
1983 memcpy(buf, aligned_buf, len);
1992 for (; cnt >= 4; cnt -= 4)
1993 *pdata++ = mci_fifo_readl(host->fifo_reg);
1997 host->part_buf32 = mci_fifo_readl(host->fifo_reg);
1998 dw_mci_pull_final_bytes(host, buf, cnt);
2002 static void dw_mci_push_data64(struct dw_mci *host, void *buf, int cnt)
2004 struct mmc_data *data = host->data;
2007 /* try and push anything in the part_buf */
2008 if (unlikely(host->part_buf_count)) {
2009 int len = dw_mci_push_part_bytes(host, buf, cnt);
2014 if (host->part_buf_count == 8) {
2015 mci_fifo_writeq(host->fifo_reg, host->part_buf);
2016 host->part_buf_count = 0;
2019 #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
2020 if (unlikely((unsigned long)buf & 0x7)) {
2022 u64 aligned_buf[16];
2023 int len = min(cnt & -8, (int)sizeof(aligned_buf));
2024 int items = len >> 3;
2026 /* memcpy from input buffer into aligned buffer */
2027 memcpy(aligned_buf, buf, len);
2030 /* push data from aligned buffer into fifo */
2031 for (i = 0; i < items; ++i)
2032 mci_fifo_writeq(host->fifo_reg, aligned_buf[i]);
2039 for (; cnt >= 8; cnt -= 8)
2040 mci_fifo_writeq(host->fifo_reg, *pdata++);
2043 /* put anything remaining in the part_buf */
2045 dw_mci_set_part_bytes(host, buf, cnt);
2046 /* Push data if we have reached the expected data length */
2047 if ((data->bytes_xfered + init_cnt) ==
2048 (data->blksz * data->blocks))
2049 mci_fifo_writeq(host->fifo_reg, host->part_buf);
2053 static void dw_mci_pull_data64(struct dw_mci *host, void *buf, int cnt)
2055 #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
2056 if (unlikely((unsigned long)buf & 0x7)) {
2058 /* pull data from fifo into aligned buffer */
2059 u64 aligned_buf[16];
2060 int len = min(cnt & -8, (int)sizeof(aligned_buf));
2061 int items = len >> 3;
2064 for (i = 0; i < items; ++i)
2065 aligned_buf[i] = mci_fifo_readq(host->fifo_reg);
2067 /* memcpy from aligned buffer into output buffer */
2068 memcpy(buf, aligned_buf, len);
2077 for (; cnt >= 8; cnt -= 8)
2078 *pdata++ = mci_fifo_readq(host->fifo_reg);
2082 host->part_buf = mci_fifo_readq(host->fifo_reg);
2083 dw_mci_pull_final_bytes(host, buf, cnt);
2087 static void dw_mci_pull_data(struct dw_mci *host, void *buf, int cnt)
2091 /* get remaining partial bytes */
2092 len = dw_mci_pull_part_bytes(host, buf, cnt);
2093 if (unlikely(len == cnt))
2098 /* get the rest of the data */
2099 host->pull_data(host, buf, cnt);
2102 static void dw_mci_read_data_pio(struct dw_mci *host, bool dto)
2104 struct sg_mapping_iter *sg_miter = &host->sg_miter;
2106 unsigned int offset;
2107 struct mmc_data *data = host->data;
2108 int shift = host->data_shift;
2111 unsigned int remain, fcnt;
2114 if (!sg_miter_next(sg_miter))
2117 host->sg = sg_miter->piter.sg;
2118 buf = sg_miter->addr;
2119 remain = sg_miter->length;
2123 fcnt = (SDMMC_GET_FCNT(mci_readl(host, STATUS))
2124 << shift) + host->part_buf_count;
2125 len = min(remain, fcnt);
2128 dw_mci_pull_data(host, (void *)(buf + offset), len);
2129 data->bytes_xfered += len;
2134 sg_miter->consumed = offset;
2135 status = mci_readl(host, MINTSTS);
2136 mci_writel(host, RINTSTS, SDMMC_INT_RXDR);
2137 /* if the RXDR is ready read again */
2138 } while ((status & SDMMC_INT_RXDR) ||
2139 (dto && SDMMC_GET_FCNT(mci_readl(host, STATUS))));
2142 if (!sg_miter_next(sg_miter))
2144 sg_miter->consumed = 0;
2146 sg_miter_stop(sg_miter);
2150 sg_miter_stop(sg_miter);
2152 smp_wmb(); /* drain writebuffer */
2153 set_bit(EVENT_XFER_COMPLETE, &host->pending_events);
2156 static void dw_mci_write_data_pio(struct dw_mci *host)
2158 struct sg_mapping_iter *sg_miter = &host->sg_miter;
2160 unsigned int offset;
2161 struct mmc_data *data = host->data;
2162 int shift = host->data_shift;
2165 unsigned int fifo_depth = host->fifo_depth;
2166 unsigned int remain, fcnt;
2169 if (!sg_miter_next(sg_miter))
2172 host->sg = sg_miter->piter.sg;
2173 buf = sg_miter->addr;
2174 remain = sg_miter->length;
2178 fcnt = ((fifo_depth -
2179 SDMMC_GET_FCNT(mci_readl(host, STATUS)))
2180 << shift) - host->part_buf_count;
2181 len = min(remain, fcnt);
2184 host->push_data(host, (void *)(buf + offset), len);
2185 data->bytes_xfered += len;
2190 sg_miter->consumed = offset;
2191 status = mci_readl(host, MINTSTS);
2192 mci_writel(host, RINTSTS, SDMMC_INT_TXDR);
2193 } while (status & SDMMC_INT_TXDR); /* if TXDR write again */
2196 if (!sg_miter_next(sg_miter))
2198 sg_miter->consumed = 0;
2200 sg_miter_stop(sg_miter);
2204 sg_miter_stop(sg_miter);
2206 smp_wmb(); /* drain writebuffer */
2207 set_bit(EVENT_XFER_COMPLETE, &host->pending_events);
2210 static void dw_mci_cmd_interrupt(struct dw_mci *host, u32 status)
2212 if (!host->cmd_status)
2213 host->cmd_status = status;
2215 smp_wmb(); /* drain writebuffer */
2217 set_bit(EVENT_CMD_COMPLETE, &host->pending_events);
2218 tasklet_schedule(&host->tasklet);
2221 static void dw_mci_handle_cd(struct dw_mci *host)
2225 for (i = 0; i < host->num_slots; i++) {
2226 struct dw_mci_slot *slot = host->slot[i];
2231 if (slot->mmc->ops->card_event)
2232 slot->mmc->ops->card_event(slot->mmc);
2233 mmc_detect_change(slot->mmc,
2234 msecs_to_jiffies(host->pdata->detect_delay_ms));
2238 static irqreturn_t dw_mci_interrupt(int irq, void *dev_id)
2240 struct dw_mci *host = dev_id;
2244 pending = mci_readl(host, MINTSTS); /* read-only mask reg */
2247 * DTO fix - version 2.10a and below, and only if internal DMA
2250 if (host->quirks & DW_MCI_QUIRK_IDMAC_DTO) {
2252 ((mci_readl(host, STATUS) >> 17) & 0x1fff))
2253 pending |= SDMMC_INT_DATA_OVER;
2257 /* Check volt switch first, since it can look like an error */
2258 if ((host->state == STATE_SENDING_CMD11) &&
2259 (pending & SDMMC_INT_VOLT_SWITCH)) {
2260 unsigned long irqflags;
2262 mci_writel(host, RINTSTS, SDMMC_INT_VOLT_SWITCH);
2263 pending &= ~SDMMC_INT_VOLT_SWITCH;
2266 * Hold the lock; we know cmd11_timer can't be kicked
2267 * off after the lock is released, so safe to delete.
2269 spin_lock_irqsave(&host->irq_lock, irqflags);
2270 dw_mci_cmd_interrupt(host, pending);
2271 spin_unlock_irqrestore(&host->irq_lock, irqflags);
2273 del_timer(&host->cmd11_timer);
2276 if (pending & DW_MCI_CMD_ERROR_FLAGS) {
2277 mci_writel(host, RINTSTS, DW_MCI_CMD_ERROR_FLAGS);
2278 host->cmd_status = pending;
2279 smp_wmb(); /* drain writebuffer */
2280 set_bit(EVENT_CMD_COMPLETE, &host->pending_events);
2283 if (pending & DW_MCI_DATA_ERROR_FLAGS) {
2284 /* if there is an error report DATA_ERROR */
2285 mci_writel(host, RINTSTS, DW_MCI_DATA_ERROR_FLAGS);
2286 host->data_status = pending;
2287 smp_wmb(); /* drain writebuffer */
2288 set_bit(EVENT_DATA_ERROR, &host->pending_events);
2289 tasklet_schedule(&host->tasklet);
2292 if (pending & SDMMC_INT_DATA_OVER) {
2293 if (host->quirks & DW_MCI_QUIRK_BROKEN_DTO)
2294 del_timer(&host->dto_timer);
2296 mci_writel(host, RINTSTS, SDMMC_INT_DATA_OVER);
2297 if (!host->data_status)
2298 host->data_status = pending;
2299 smp_wmb(); /* drain writebuffer */
2300 if (host->dir_status == DW_MCI_RECV_STATUS) {
2301 if (host->sg != NULL)
2302 dw_mci_read_data_pio(host, true);
2304 set_bit(EVENT_DATA_COMPLETE, &host->pending_events);
2305 tasklet_schedule(&host->tasklet);
2308 if (pending & SDMMC_INT_RXDR) {
2309 mci_writel(host, RINTSTS, SDMMC_INT_RXDR);
2310 if (host->dir_status == DW_MCI_RECV_STATUS && host->sg)
2311 dw_mci_read_data_pio(host, false);
2314 if (pending & SDMMC_INT_TXDR) {
2315 mci_writel(host, RINTSTS, SDMMC_INT_TXDR);
2316 if (host->dir_status == DW_MCI_SEND_STATUS && host->sg)
2317 dw_mci_write_data_pio(host);
2320 if (pending & SDMMC_INT_CMD_DONE) {
2321 mci_writel(host, RINTSTS, SDMMC_INT_CMD_DONE);
2322 dw_mci_cmd_interrupt(host, pending);
2325 if (pending & SDMMC_INT_CD) {
2326 mci_writel(host, RINTSTS, SDMMC_INT_CD);
2327 dw_mci_handle_cd(host);
2330 /* Handle SDIO Interrupts */
2331 for (i = 0; i < host->num_slots; i++) {
2332 struct dw_mci_slot *slot = host->slot[i];
2337 if (pending & SDMMC_INT_SDIO(slot->sdio_id)) {
2338 mci_writel(host, RINTSTS,
2339 SDMMC_INT_SDIO(slot->sdio_id));
2340 mmc_signal_sdio_irq(slot->mmc);
2346 #ifdef CONFIG_MMC_DW_IDMAC
2347 /* Handle DMA interrupts */
2348 if (host->dma_64bit_address == 1) {
2349 pending = mci_readl(host, IDSTS64);
2350 if (pending & (SDMMC_IDMAC_INT_TI | SDMMC_IDMAC_INT_RI)) {
2351 mci_writel(host, IDSTS64, SDMMC_IDMAC_INT_TI |
2352 SDMMC_IDMAC_INT_RI);
2353 mci_writel(host, IDSTS64, SDMMC_IDMAC_INT_NI);
2354 host->dma_ops->complete(host);
2357 pending = mci_readl(host, IDSTS);
2358 if (pending & (SDMMC_IDMAC_INT_TI | SDMMC_IDMAC_INT_RI)) {
2359 mci_writel(host, IDSTS, SDMMC_IDMAC_INT_TI |
2360 SDMMC_IDMAC_INT_RI);
2361 mci_writel(host, IDSTS, SDMMC_IDMAC_INT_NI);
2362 host->dma_ops->complete(host);
2371 /* given a slot, find out the device node representing that slot */
2372 static struct device_node *dw_mci_of_find_slot_node(struct dw_mci_slot *slot)
2374 struct device *dev = slot->mmc->parent;
2375 struct device_node *np;
2379 if (!dev || !dev->of_node)
2382 for_each_child_of_node(dev->of_node, np) {
2383 addr = of_get_property(np, "reg", &len);
2384 if (!addr || (len < sizeof(int)))
2386 if (be32_to_cpup(addr) == slot->id)
2392 static void dw_mci_slot_of_parse(struct dw_mci_slot *slot)
2394 struct device_node *np = dw_mci_of_find_slot_node(slot);
2399 if (of_property_read_bool(np, "disable-wp")) {
2400 slot->mmc->caps2 |= MMC_CAP2_NO_WRITE_PROTECT;
2401 dev_warn(slot->mmc->parent,
2402 "Slot quirk 'disable-wp' is deprecated\n");
2405 #else /* CONFIG_OF */
2406 static void dw_mci_slot_of_parse(struct dw_mci_slot *slot)
2409 #endif /* CONFIG_OF */
2411 static int dw_mci_init_slot(struct dw_mci *host, unsigned int id)
2413 struct mmc_host *mmc;
2414 struct dw_mci_slot *slot;
2415 const struct dw_mci_drv_data *drv_data = host->drv_data;
2419 mmc = mmc_alloc_host(sizeof(struct dw_mci_slot), host->dev);
2423 slot = mmc_priv(mmc);
2425 slot->sdio_id = host->sdio_id0 + id;
2428 host->slot[id] = slot;
2430 mmc->ops = &dw_mci_ops;
2431 if (of_property_read_u32_array(host->dev->of_node,
2432 "clock-freq-min-max", freq, 2)) {
2433 mmc->f_min = DW_MCI_FREQ_MIN;
2434 mmc->f_max = DW_MCI_FREQ_MAX;
2436 mmc->f_min = freq[0];
2437 mmc->f_max = freq[1];
2440 /*if there are external regulators, get them*/
2441 ret = mmc_regulator_get_supply(mmc);
2442 if (ret == -EPROBE_DEFER)
2443 goto err_host_allocated;
2445 if (!mmc->ocr_avail)
2446 mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
2448 if (host->pdata->caps)
2449 mmc->caps = host->pdata->caps;
2451 if (host->pdata->pm_caps)
2452 mmc->pm_caps = host->pdata->pm_caps;
2454 if (host->dev->of_node) {
2455 ctrl_id = of_alias_get_id(host->dev->of_node, "mshc");
2459 ctrl_id = to_platform_device(host->dev)->id;
2461 if (drv_data && drv_data->caps)
2462 mmc->caps |= drv_data->caps[ctrl_id];
2464 if (host->pdata->caps2)
2465 mmc->caps2 = host->pdata->caps2;
2467 dw_mci_slot_of_parse(slot);
2469 ret = mmc_of_parse(mmc);
2471 goto err_host_allocated;
2473 /* Useful defaults if platform data is unset. */
2474 if (host->use_dma) {
2475 mmc->max_segs = host->ring_size;
2476 mmc->max_blk_size = 65536;
2477 mmc->max_seg_size = 0x1000;
2478 mmc->max_req_size = mmc->max_seg_size * host->ring_size;
2479 mmc->max_blk_count = mmc->max_req_size / 512;
2482 mmc->max_blk_size = 65536; /* BLKSIZ is 16 bits */
2483 mmc->max_blk_count = 512;
2484 mmc->max_req_size = mmc->max_blk_size *
2486 mmc->max_seg_size = mmc->max_req_size;
2489 if (dw_mci_get_cd(mmc))
2490 set_bit(DW_MMC_CARD_PRESENT, &slot->flags);
2492 clear_bit(DW_MMC_CARD_PRESENT, &slot->flags);
2494 ret = mmc_add_host(mmc);
2496 goto err_host_allocated;
2498 #if defined(CONFIG_DEBUG_FS)
2499 dw_mci_init_debugfs(slot);
2509 static void dw_mci_cleanup_slot(struct dw_mci_slot *slot, unsigned int id)
2511 /* Debugfs stuff is cleaned up by mmc core */
2512 mmc_remove_host(slot->mmc);
2513 slot->host->slot[id] = NULL;
2514 mmc_free_host(slot->mmc);
2517 static void dw_mci_init_dma(struct dw_mci *host)
2520 /* Check ADDR_CONFIG bit in HCON to find IDMAC address bus width */
2521 addr_config = (mci_readl(host, HCON) >> 27) & 0x01;
2523 if (addr_config == 1) {
2524 /* host supports IDMAC in 64-bit address mode */
2525 host->dma_64bit_address = 1;
2526 dev_info(host->dev, "IDMAC supports 64-bit address mode.\n");
2527 if (!dma_set_mask(host->dev, DMA_BIT_MASK(64)))
2528 dma_set_coherent_mask(host->dev, DMA_BIT_MASK(64));
2530 /* host supports IDMAC in 32-bit address mode */
2531 host->dma_64bit_address = 0;
2532 dev_info(host->dev, "IDMAC supports 32-bit address mode.\n");
2535 /* Alloc memory for sg translation */
2536 host->sg_cpu = dmam_alloc_coherent(host->dev, PAGE_SIZE,
2537 &host->sg_dma, GFP_KERNEL);
2538 if (!host->sg_cpu) {
2539 dev_err(host->dev, "%s: could not alloc DMA memory\n",
2544 /* Determine which DMA interface to use */
2545 #ifdef CONFIG_MMC_DW_IDMAC
2546 host->dma_ops = &dw_mci_idmac_ops;
2547 dev_info(host->dev, "Using internal DMA controller.\n");
2553 if (host->dma_ops->init && host->dma_ops->start &&
2554 host->dma_ops->stop && host->dma_ops->cleanup) {
2555 if (host->dma_ops->init(host)) {
2556 dev_err(host->dev, "%s: Unable to initialize DMA Controller.\n",
2561 dev_err(host->dev, "DMA initialization not found.\n");
2569 dev_info(host->dev, "Using PIO mode.\n");
2573 static bool dw_mci_ctrl_reset(struct dw_mci *host, u32 reset)
2575 unsigned long timeout = jiffies + msecs_to_jiffies(500);
2578 ctrl = mci_readl(host, CTRL);
2580 mci_writel(host, CTRL, ctrl);
2582 /* wait till resets clear */
2584 ctrl = mci_readl(host, CTRL);
2585 if (!(ctrl & reset))
2587 } while (time_before(jiffies, timeout));
2590 "Timeout resetting block (ctrl reset %#x)\n",
2596 static bool dw_mci_reset(struct dw_mci *host)
2598 u32 flags = SDMMC_CTRL_RESET | SDMMC_CTRL_FIFO_RESET;
2602 * Reseting generates a block interrupt, hence setting
2603 * the scatter-gather pointer to NULL.
2606 sg_miter_stop(&host->sg_miter);
2611 flags |= SDMMC_CTRL_DMA_RESET;
2613 if (dw_mci_ctrl_reset(host, flags)) {
2615 * In all cases we clear the RAWINTS register to clear any
2618 mci_writel(host, RINTSTS, 0xFFFFFFFF);
2620 /* if using dma we wait for dma_req to clear */
2621 if (host->use_dma) {
2622 unsigned long timeout = jiffies + msecs_to_jiffies(500);
2626 status = mci_readl(host, STATUS);
2627 if (!(status & SDMMC_STATUS_DMA_REQ))
2630 } while (time_before(jiffies, timeout));
2632 if (status & SDMMC_STATUS_DMA_REQ) {
2634 "%s: Timeout waiting for dma_req to clear during reset\n",
2639 /* when using DMA next we reset the fifo again */
2640 if (!dw_mci_ctrl_reset(host, SDMMC_CTRL_FIFO_RESET))
2644 /* if the controller reset bit did clear, then set clock regs */
2645 if (!(mci_readl(host, CTRL) & SDMMC_CTRL_RESET)) {
2647 "%s: fifo/dma reset bits didn't clear but ciu was reset, doing clock update\n",
2653 #if IS_ENABLED(CONFIG_MMC_DW_IDMAC)
2654 /* It is also recommended that we reset and reprogram idmac */
2655 dw_mci_idmac_reset(host);
2661 /* After a CTRL reset we need to have CIU set clock registers */
2662 mci_send_cmd(host->cur_slot, SDMMC_CMD_UPD_CLK, 0);
2667 static void dw_mci_cmd11_timer(unsigned long arg)
2669 struct dw_mci *host = (struct dw_mci *)arg;
2671 if (host->state != STATE_SENDING_CMD11) {
2672 dev_warn(host->dev, "Unexpected CMD11 timeout\n");
2676 host->cmd_status = SDMMC_INT_RTO;
2677 set_bit(EVENT_CMD_COMPLETE, &host->pending_events);
2678 tasklet_schedule(&host->tasklet);
2681 static void dw_mci_dto_timer(unsigned long arg)
2683 struct dw_mci *host = (struct dw_mci *)arg;
2685 switch (host->state) {
2686 case STATE_SENDING_DATA:
2687 case STATE_DATA_BUSY:
2689 * If DTO interrupt does NOT come in sending data state,
2690 * we should notify the driver to terminate current transfer
2691 * and report a data timeout to the core.
2693 host->data_status = SDMMC_INT_DRTO;
2694 set_bit(EVENT_DATA_ERROR, &host->pending_events);
2695 set_bit(EVENT_DATA_COMPLETE, &host->pending_events);
2696 tasklet_schedule(&host->tasklet);
2704 static struct dw_mci_of_quirks {
2709 .quirk = "broken-cd",
2710 .id = DW_MCI_QUIRK_BROKEN_CARD_DETECTION,
2714 static struct dw_mci_board *dw_mci_parse_dt(struct dw_mci *host)
2716 struct dw_mci_board *pdata;
2717 struct device *dev = host->dev;
2718 struct device_node *np = dev->of_node;
2719 const struct dw_mci_drv_data *drv_data = host->drv_data;
2721 u32 clock_frequency;
2723 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
2725 return ERR_PTR(-ENOMEM);
2727 /* find out number of slots supported */
2728 if (of_property_read_u32(dev->of_node, "num-slots",
2729 &pdata->num_slots)) {
2731 "num-slots property not found, assuming 1 slot is available\n");
2732 pdata->num_slots = 1;
2736 for (idx = 0; idx < ARRAY_SIZE(of_quirks); idx++)
2737 if (of_get_property(np, of_quirks[idx].quirk, NULL))
2738 pdata->quirks |= of_quirks[idx].id;
2740 if (of_property_read_u32(np, "fifo-depth", &pdata->fifo_depth))
2742 "fifo-depth property not found, using value of FIFOTH register as default\n");
2744 of_property_read_u32(np, "card-detect-delay", &pdata->detect_delay_ms);
2746 if (!of_property_read_u32(np, "clock-frequency", &clock_frequency))
2747 pdata->bus_hz = clock_frequency;
2749 if (drv_data && drv_data->parse_dt) {
2750 ret = drv_data->parse_dt(host);
2752 return ERR_PTR(ret);
2755 if (of_find_property(np, "supports-highspeed", NULL)) {
2756 dev_info(dev, "supports-highspeed property is deprecated.\n");
2757 pdata->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED;
2763 #else /* CONFIG_OF */
2764 static struct dw_mci_board *dw_mci_parse_dt(struct dw_mci *host)
2766 return ERR_PTR(-EINVAL);
2768 #endif /* CONFIG_OF */
2770 static void dw_mci_enable_cd(struct dw_mci *host)
2772 struct dw_mci_board *brd = host->pdata;
2773 unsigned long irqflags;
2777 /* No need for CD if broken card detection */
2778 if (brd->quirks & DW_MCI_QUIRK_BROKEN_CARD_DETECTION)
2781 /* No need for CD if all slots have a non-error GPIO */
2782 for (i = 0; i < host->num_slots; i++) {
2783 struct dw_mci_slot *slot = host->slot[i];
2785 if (IS_ERR_VALUE(mmc_gpio_get_cd(slot->mmc)))
2788 if (i == host->num_slots)
2791 spin_lock_irqsave(&host->irq_lock, irqflags);
2792 temp = mci_readl(host, INTMASK);
2793 temp |= SDMMC_INT_CD;
2794 mci_writel(host, INTMASK, temp);
2795 spin_unlock_irqrestore(&host->irq_lock, irqflags);
2798 int dw_mci_probe(struct dw_mci *host)
2800 const struct dw_mci_drv_data *drv_data = host->drv_data;
2801 int width, i, ret = 0;
2806 host->pdata = dw_mci_parse_dt(host);
2807 if (IS_ERR(host->pdata)) {
2808 dev_err(host->dev, "platform data not available\n");
2813 if (host->pdata->num_slots < 1) {
2815 "Platform data must supply num_slots.\n");
2819 host->biu_clk = devm_clk_get(host->dev, "biu");
2820 if (IS_ERR(host->biu_clk)) {
2821 dev_dbg(host->dev, "biu clock not available\n");
2823 ret = clk_prepare_enable(host->biu_clk);
2825 dev_err(host->dev, "failed to enable biu clock\n");
2830 host->ciu_clk = devm_clk_get(host->dev, "ciu");
2831 if (IS_ERR(host->ciu_clk)) {
2832 dev_dbg(host->dev, "ciu clock not available\n");
2833 host->bus_hz = host->pdata->bus_hz;
2835 ret = clk_prepare_enable(host->ciu_clk);
2837 dev_err(host->dev, "failed to enable ciu clock\n");
2841 if (host->pdata->bus_hz) {
2842 ret = clk_set_rate(host->ciu_clk, host->pdata->bus_hz);
2845 "Unable to set bus rate to %uHz\n",
2846 host->pdata->bus_hz);
2848 host->bus_hz = clk_get_rate(host->ciu_clk);
2851 if (!host->bus_hz) {
2853 "Platform data must supply bus speed\n");
2858 if (drv_data && drv_data->init) {
2859 ret = drv_data->init(host);
2862 "implementation specific init failed\n");
2867 if (drv_data && drv_data->setup_clock) {
2868 ret = drv_data->setup_clock(host);
2871 "implementation specific clock setup failed\n");
2876 setup_timer(&host->cmd11_timer,
2877 dw_mci_cmd11_timer, (unsigned long)host);
2879 host->quirks = host->pdata->quirks;
2881 if (host->quirks & DW_MCI_QUIRK_BROKEN_DTO)
2882 setup_timer(&host->dto_timer,
2883 dw_mci_dto_timer, (unsigned long)host);
2885 spin_lock_init(&host->lock);
2886 spin_lock_init(&host->irq_lock);
2887 INIT_LIST_HEAD(&host->queue);
2890 * Get the host data width - this assumes that HCON has been set with
2891 * the correct values.
2893 i = (mci_readl(host, HCON) >> 7) & 0x7;
2895 host->push_data = dw_mci_push_data16;
2896 host->pull_data = dw_mci_pull_data16;
2898 host->data_shift = 1;
2899 } else if (i == 2) {
2900 host->push_data = dw_mci_push_data64;
2901 host->pull_data = dw_mci_pull_data64;
2903 host->data_shift = 3;
2905 /* Check for a reserved value, and warn if it is */
2907 "HCON reports a reserved host data width!\n"
2908 "Defaulting to 32-bit access.\n");
2909 host->push_data = dw_mci_push_data32;
2910 host->pull_data = dw_mci_pull_data32;
2912 host->data_shift = 2;
2915 /* Reset all blocks */
2916 if (!dw_mci_ctrl_reset(host, SDMMC_CTRL_ALL_RESET_FLAGS))
2919 host->dma_ops = host->pdata->dma_ops;
2920 dw_mci_init_dma(host);
2922 /* Clear the interrupts for the host controller */
2923 mci_writel(host, RINTSTS, 0xFFFFFFFF);
2924 mci_writel(host, INTMASK, 0); /* disable all mmc interrupt first */
2926 /* Put in max timeout */
2927 mci_writel(host, TMOUT, 0xFFFFFFFF);
2930 * FIFO threshold settings RxMark = fifo_size / 2 - 1,
2931 * Tx Mark = fifo_size / 2 DMA Size = 8
2933 if (!host->pdata->fifo_depth) {
2935 * Power-on value of RX_WMark is FIFO_DEPTH-1, but this may
2936 * have been overwritten by the bootloader, just like we're
2937 * about to do, so if you know the value for your hardware, you
2938 * should put it in the platform data.
2940 fifo_size = mci_readl(host, FIFOTH);
2941 fifo_size = 1 + ((fifo_size >> 16) & 0xfff);
2943 fifo_size = host->pdata->fifo_depth;
2945 host->fifo_depth = fifo_size;
2947 SDMMC_SET_FIFOTH(0x2, fifo_size / 2 - 1, fifo_size / 2);
2948 mci_writel(host, FIFOTH, host->fifoth_val);
2950 /* disable clock to CIU */
2951 mci_writel(host, CLKENA, 0);
2952 mci_writel(host, CLKSRC, 0);
2955 * In 2.40a spec, Data offset is changed.
2956 * Need to check the version-id and set data-offset for DATA register.
2958 host->verid = SDMMC_GET_VERID(mci_readl(host, VERID));
2959 dev_info(host->dev, "Version ID is %04x\n", host->verid);
2961 if (host->verid < DW_MMC_240A)
2962 host->fifo_reg = host->regs + DATA_OFFSET;
2964 host->fifo_reg = host->regs + DATA_240A_OFFSET;
2966 tasklet_init(&host->tasklet, dw_mci_tasklet_func, (unsigned long)host);
2967 ret = devm_request_irq(host->dev, host->irq, dw_mci_interrupt,
2968 host->irq_flags, "dw-mci", host);
2972 if (host->pdata->num_slots)
2973 host->num_slots = host->pdata->num_slots;
2975 host->num_slots = ((mci_readl(host, HCON) >> 1) & 0x1F) + 1;
2978 * Enable interrupts for command done, data over, data empty,
2979 * receive ready and error such as transmit, receive timeout, crc error
2981 mci_writel(host, RINTSTS, 0xFFFFFFFF);
2982 mci_writel(host, INTMASK, SDMMC_INT_CMD_DONE | SDMMC_INT_DATA_OVER |
2983 SDMMC_INT_TXDR | SDMMC_INT_RXDR |
2984 DW_MCI_ERROR_FLAGS);
2985 /* Enable mci interrupt */
2986 mci_writel(host, CTRL, SDMMC_CTRL_INT_ENABLE);
2989 "DW MMC controller at irq %d,%d bit host data width,%u deep fifo\n",
2990 host->irq, width, fifo_size);
2992 /* We need at least one slot to succeed */
2993 for (i = 0; i < host->num_slots; i++) {
2994 ret = dw_mci_init_slot(host, i);
2996 dev_dbg(host->dev, "slot %d init failed\n", i);
3002 dev_info(host->dev, "%d slots initialized\n", init_slots);
3005 "attempted to initialize %d slots, but failed on all\n",
3010 /* Now that slots are all setup, we can enable card detect */
3011 dw_mci_enable_cd(host);
3013 if (host->quirks & DW_MCI_QUIRK_IDMAC_DTO)
3014 dev_info(host->dev, "Internal DMAC interrupt fix enabled.\n");
3019 if (host->use_dma && host->dma_ops->exit)
3020 host->dma_ops->exit(host);
3023 if (!IS_ERR(host->ciu_clk))
3024 clk_disable_unprepare(host->ciu_clk);
3027 if (!IS_ERR(host->biu_clk))
3028 clk_disable_unprepare(host->biu_clk);
3032 EXPORT_SYMBOL(dw_mci_probe);
3034 void dw_mci_remove(struct dw_mci *host)
3038 for (i = 0; i < host->num_slots; i++) {
3039 dev_dbg(host->dev, "remove slot %d\n", i);
3041 dw_mci_cleanup_slot(host->slot[i], i);
3044 mci_writel(host, RINTSTS, 0xFFFFFFFF);
3045 mci_writel(host, INTMASK, 0); /* disable all mmc interrupt first */
3047 /* disable clock to CIU */
3048 mci_writel(host, CLKENA, 0);
3049 mci_writel(host, CLKSRC, 0);
3051 if (host->use_dma && host->dma_ops->exit)
3052 host->dma_ops->exit(host);
3054 if (!IS_ERR(host->ciu_clk))
3055 clk_disable_unprepare(host->ciu_clk);
3057 if (!IS_ERR(host->biu_clk))
3058 clk_disable_unprepare(host->biu_clk);
3060 EXPORT_SYMBOL(dw_mci_remove);
3064 #ifdef CONFIG_PM_SLEEP
3066 * TODO: we should probably disable the clock to the card in the suspend path.
3068 int dw_mci_suspend(struct dw_mci *host)
3072 EXPORT_SYMBOL(dw_mci_suspend);
3074 int dw_mci_resume(struct dw_mci *host)
3078 if (!dw_mci_ctrl_reset(host, SDMMC_CTRL_ALL_RESET_FLAGS)) {
3083 if (host->use_dma && host->dma_ops->init)
3084 host->dma_ops->init(host);
3087 * Restore the initial value at FIFOTH register
3088 * And Invalidate the prev_blksz with zero
3090 mci_writel(host, FIFOTH, host->fifoth_val);
3091 host->prev_blksz = 0;
3093 /* Put in max timeout */
3094 mci_writel(host, TMOUT, 0xFFFFFFFF);
3096 mci_writel(host, RINTSTS, 0xFFFFFFFF);
3097 mci_writel(host, INTMASK, SDMMC_INT_CMD_DONE | SDMMC_INT_DATA_OVER |
3098 SDMMC_INT_TXDR | SDMMC_INT_RXDR |
3099 DW_MCI_ERROR_FLAGS);
3100 mci_writel(host, CTRL, SDMMC_CTRL_INT_ENABLE);
3102 for (i = 0; i < host->num_slots; i++) {
3103 struct dw_mci_slot *slot = host->slot[i];
3107 if (slot->mmc->pm_flags & MMC_PM_KEEP_POWER) {
3108 dw_mci_set_ios(slot->mmc, &slot->mmc->ios);
3109 dw_mci_setup_bus(slot, true);
3113 /* Now that slots are all setup, we can enable card detect */
3114 dw_mci_enable_cd(host);
3118 EXPORT_SYMBOL(dw_mci_resume);
3119 #endif /* CONFIG_PM_SLEEP */
3121 static int __init dw_mci_init(void)
3123 pr_info("Synopsys Designware Multimedia Card Interface Driver\n");
3127 static void __exit dw_mci_exit(void)
3131 module_init(dw_mci_init);
3132 module_exit(dw_mci_exit);
3134 MODULE_DESCRIPTION("DW Multimedia Card Interface driver");
3135 MODULE_AUTHOR("NXP Semiconductor VietNam");
3136 MODULE_AUTHOR("Imagination Technologies Ltd");
3137 MODULE_LICENSE("GPL v2");