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/iopoll.h>
23 #include <linux/ioport.h>
24 #include <linux/module.h>
25 #include <linux/platform_device.h>
26 #include <linux/pm_runtime.h>
27 #include <linux/seq_file.h>
28 #include <linux/slab.h>
29 #include <linux/stat.h>
30 #include <linux/delay.h>
31 #include <linux/irq.h>
32 #include <linux/mmc/card.h>
33 #include <linux/mmc/host.h>
34 #include <linux/mmc/mmc.h>
35 #include <linux/mmc/sd.h>
36 #include <linux/mmc/sdio.h>
37 #include <linux/bitops.h>
38 #include <linux/regulator/consumer.h>
40 #include <linux/of_gpio.h>
41 #include <linux/mmc/slot-gpio.h>
45 /* Common flag combinations */
46 #define DW_MCI_DATA_ERROR_FLAGS (SDMMC_INT_DRTO | SDMMC_INT_DCRC | \
47 SDMMC_INT_HTO | SDMMC_INT_SBE | \
48 SDMMC_INT_EBE | SDMMC_INT_HLE)
49 #define DW_MCI_CMD_ERROR_FLAGS (SDMMC_INT_RTO | SDMMC_INT_RCRC | \
50 SDMMC_INT_RESP_ERR | SDMMC_INT_HLE)
51 #define DW_MCI_ERROR_FLAGS (DW_MCI_DATA_ERROR_FLAGS | \
52 DW_MCI_CMD_ERROR_FLAGS)
53 #define DW_MCI_SEND_STATUS 1
54 #define DW_MCI_RECV_STATUS 2
55 #define DW_MCI_DMA_THRESHOLD 16
57 #define DW_MCI_FREQ_MAX 200000000 /* unit: HZ */
58 #define DW_MCI_FREQ_MIN 100000 /* unit: HZ */
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 #define DESC_RING_BUF_SZ PAGE_SIZE
67 struct idmac_desc_64addr {
68 u32 des0; /* Control Descriptor */
69 #define IDMAC_OWN_CLR64(x) \
70 !((x) & cpu_to_le32(IDMAC_DES0_OWN))
72 u32 des1; /* Reserved */
74 u32 des2; /*Buffer sizes */
75 #define IDMAC_64ADDR_SET_BUFFER1_SIZE(d, s) \
76 ((d)->des2 = ((d)->des2 & cpu_to_le32(0x03ffe000)) | \
77 ((cpu_to_le32(s)) & cpu_to_le32(0x1fff)))
79 u32 des3; /* Reserved */
81 u32 des4; /* Lower 32-bits of Buffer Address Pointer 1*/
82 u32 des5; /* Upper 32-bits of Buffer Address Pointer 1*/
84 u32 des6; /* Lower 32-bits of Next Descriptor Address */
85 u32 des7; /* Upper 32-bits of Next Descriptor Address */
89 __le32 des0; /* Control Descriptor */
90 #define IDMAC_DES0_DIC BIT(1)
91 #define IDMAC_DES0_LD BIT(2)
92 #define IDMAC_DES0_FD BIT(3)
93 #define IDMAC_DES0_CH BIT(4)
94 #define IDMAC_DES0_ER BIT(5)
95 #define IDMAC_DES0_CES BIT(30)
96 #define IDMAC_DES0_OWN BIT(31)
98 __le32 des1; /* Buffer sizes */
99 #define IDMAC_SET_BUFFER1_SIZE(d, s) \
100 ((d)->des1 = ((d)->des1 & cpu_to_le32(0x03ffe000)) | (cpu_to_le32((s) & 0x1fff)))
102 __le32 des2; /* buffer 1 physical address */
104 __le32 des3; /* buffer 2 physical address */
107 /* Each descriptor can transfer up to 4KB of data in chained mode */
108 #define DW_MCI_DESC_DATA_LENGTH 0x1000
110 static int dw_mci_card_busy(struct mmc_host *mmc);
111 static int dw_mci_get_cd(struct mmc_host *mmc);
113 #if defined(CONFIG_DEBUG_FS)
114 static int dw_mci_req_show(struct seq_file *s, void *v)
116 struct dw_mci_slot *slot = s->private;
117 struct mmc_request *mrq;
118 struct mmc_command *cmd;
119 struct mmc_command *stop;
120 struct mmc_data *data;
122 /* Make sure we get a consistent snapshot */
123 spin_lock_bh(&slot->host->lock);
133 "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n",
134 cmd->opcode, cmd->arg, cmd->flags,
135 cmd->resp[0], cmd->resp[1], cmd->resp[2],
136 cmd->resp[2], cmd->error);
138 seq_printf(s, "DATA %u / %u * %u flg %x err %d\n",
139 data->bytes_xfered, data->blocks,
140 data->blksz, data->flags, data->error);
143 "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n",
144 stop->opcode, stop->arg, stop->flags,
145 stop->resp[0], stop->resp[1], stop->resp[2],
146 stop->resp[2], stop->error);
149 spin_unlock_bh(&slot->host->lock);
154 static int dw_mci_req_open(struct inode *inode, struct file *file)
156 return single_open(file, dw_mci_req_show, inode->i_private);
159 static const struct file_operations dw_mci_req_fops = {
160 .owner = THIS_MODULE,
161 .open = dw_mci_req_open,
164 .release = single_release,
167 static int dw_mci_regs_show(struct seq_file *s, void *v)
169 struct dw_mci *host = s->private;
171 seq_printf(s, "STATUS:\t0x%08x\n", mci_readl(host, STATUS));
172 seq_printf(s, "RINTSTS:\t0x%08x\n", mci_readl(host, RINTSTS));
173 seq_printf(s, "CMD:\t0x%08x\n", mci_readl(host, CMD));
174 seq_printf(s, "CTRL:\t0x%08x\n", mci_readl(host, CTRL));
175 seq_printf(s, "INTMASK:\t0x%08x\n", mci_readl(host, INTMASK));
176 seq_printf(s, "CLKENA:\t0x%08x\n", mci_readl(host, CLKENA));
181 static int dw_mci_regs_open(struct inode *inode, struct file *file)
183 return single_open(file, dw_mci_regs_show, inode->i_private);
186 static const struct file_operations dw_mci_regs_fops = {
187 .owner = THIS_MODULE,
188 .open = dw_mci_regs_open,
191 .release = single_release,
194 static void dw_mci_init_debugfs(struct dw_mci_slot *slot)
196 struct mmc_host *mmc = slot->mmc;
197 struct dw_mci *host = slot->host;
201 root = mmc->debugfs_root;
205 node = debugfs_create_file("regs", S_IRUSR, root, host,
210 node = debugfs_create_file("req", S_IRUSR, root, slot,
215 node = debugfs_create_u32("state", S_IRUSR, root, (u32 *)&host->state);
219 node = debugfs_create_x32("pending_events", S_IRUSR, root,
220 (u32 *)&host->pending_events);
224 node = debugfs_create_x32("completed_events", S_IRUSR, root,
225 (u32 *)&host->completed_events);
232 dev_err(&mmc->class_dev, "failed to initialize debugfs for slot\n");
234 #endif /* defined(CONFIG_DEBUG_FS) */
236 static void mci_send_cmd(struct dw_mci_slot *slot, u32 cmd, u32 arg);
237 static bool dw_mci_ctrl_reset(struct dw_mci *host, u32 reset)
241 ctrl = mci_readl(host, CTRL);
243 mci_writel(host, CTRL, ctrl);
245 /* wait till resets clear */
246 if (readl_poll_timeout_atomic(host->regs + SDMMC_CTRL, ctrl,
248 1, 500 * USEC_PER_MSEC)) {
250 "Timeout resetting block (ctrl reset %#x)\n",
258 static u32 dw_mci_prepare_command(struct mmc_host *mmc, struct mmc_command *cmd)
260 struct dw_mci_slot *slot = mmc_priv(mmc);
261 struct dw_mci *host = slot->host;
264 cmd->error = -EINPROGRESS;
267 if (cmd->opcode == MMC_STOP_TRANSMISSION ||
268 cmd->opcode == MMC_GO_IDLE_STATE ||
269 cmd->opcode == MMC_GO_INACTIVE_STATE ||
270 (cmd->opcode == SD_IO_RW_DIRECT &&
271 ((cmd->arg >> 9) & 0x1FFFF) == SDIO_CCCR_ABORT))
272 cmdr |= SDMMC_CMD_STOP;
273 else if (cmd->opcode != MMC_SEND_STATUS && cmd->data)
274 cmdr |= SDMMC_CMD_PRV_DAT_WAIT;
276 if (cmd->opcode == SD_SWITCH_VOLTAGE) {
279 /* Special bit makes CMD11 not die */
280 cmdr |= SDMMC_CMD_VOLT_SWITCH;
282 /* Change state to continue to handle CMD11 weirdness */
283 WARN_ON(slot->host->state != STATE_SENDING_CMD);
284 slot->host->state = STATE_SENDING_CMD11;
287 * We need to disable low power mode (automatic clock stop)
288 * while doing voltage switch so we don't confuse the card,
289 * since stopping the clock is a specific part of the UHS
290 * voltage change dance.
292 * Note that low power mode (SDMMC_CLKEN_LOW_PWR) will be
293 * unconditionally turned back on in dw_mci_setup_bus() if it's
294 * ever called with a non-zero clock. That shouldn't happen
295 * until the voltage change is all done.
297 clk_en_a = mci_readl(host, CLKENA);
298 clk_en_a &= ~(SDMMC_CLKEN_LOW_PWR << slot->id);
299 mci_writel(host, CLKENA, clk_en_a);
300 mci_send_cmd(slot, SDMMC_CMD_UPD_CLK |
301 SDMMC_CMD_PRV_DAT_WAIT, 0);
304 if (cmd->flags & MMC_RSP_PRESENT) {
305 /* We expect a response, so set this bit */
306 cmdr |= SDMMC_CMD_RESP_EXP;
307 if (cmd->flags & MMC_RSP_136)
308 cmdr |= SDMMC_CMD_RESP_LONG;
311 if (cmd->flags & MMC_RSP_CRC)
312 cmdr |= SDMMC_CMD_RESP_CRC;
315 cmdr |= SDMMC_CMD_DAT_EXP;
316 if (cmd->data->flags & MMC_DATA_WRITE)
317 cmdr |= SDMMC_CMD_DAT_WR;
320 if (!test_bit(DW_MMC_CARD_NO_USE_HOLD, &slot->flags))
321 cmdr |= SDMMC_CMD_USE_HOLD_REG;
326 static u32 dw_mci_prep_stop_abort(struct dw_mci *host, struct mmc_command *cmd)
328 struct mmc_command *stop;
334 stop = &host->stop_abort;
336 memset(stop, 0, sizeof(struct mmc_command));
338 if (cmdr == MMC_READ_SINGLE_BLOCK ||
339 cmdr == MMC_READ_MULTIPLE_BLOCK ||
340 cmdr == MMC_WRITE_BLOCK ||
341 cmdr == MMC_WRITE_MULTIPLE_BLOCK ||
342 cmdr == MMC_SEND_TUNING_BLOCK ||
343 cmdr == MMC_SEND_TUNING_BLOCK_HS200) {
344 stop->opcode = MMC_STOP_TRANSMISSION;
346 stop->flags = MMC_RSP_R1B | MMC_CMD_AC;
347 } else if (cmdr == SD_IO_RW_EXTENDED) {
348 stop->opcode = SD_IO_RW_DIRECT;
349 stop->arg |= (1 << 31) | (0 << 28) | (SDIO_CCCR_ABORT << 9) |
350 ((cmd->arg >> 28) & 0x7);
351 stop->flags = MMC_RSP_SPI_R5 | MMC_RSP_R5 | MMC_CMD_AC;
356 cmdr = stop->opcode | SDMMC_CMD_STOP |
357 SDMMC_CMD_RESP_CRC | SDMMC_CMD_RESP_EXP;
359 if (!test_bit(DW_MMC_CARD_NO_USE_HOLD, &host->cur_slot->flags))
360 cmdr |= SDMMC_CMD_USE_HOLD_REG;
365 static void dw_mci_wait_while_busy(struct dw_mci *host, u32 cmd_flags)
370 * Databook says that before issuing a new data transfer command
371 * we need to check to see if the card is busy. Data transfer commands
372 * all have SDMMC_CMD_PRV_DAT_WAIT set, so we'll key off that.
374 * ...also allow sending for SDMMC_CMD_VOLT_SWITCH where busy is
377 if ((cmd_flags & SDMMC_CMD_PRV_DAT_WAIT) &&
378 !(cmd_flags & SDMMC_CMD_VOLT_SWITCH)) {
379 if (readl_poll_timeout_atomic(host->regs + SDMMC_STATUS,
381 !(status & SDMMC_STATUS_BUSY),
382 10, 500 * USEC_PER_MSEC))
383 dev_err(host->dev, "Busy; trying anyway\n");
387 static void dw_mci_start_command(struct dw_mci *host,
388 struct mmc_command *cmd, u32 cmd_flags)
392 "start command: ARGR=0x%08x CMDR=0x%08x\n",
393 cmd->arg, cmd_flags);
395 mci_writel(host, CMDARG, cmd->arg);
396 wmb(); /* drain writebuffer */
397 dw_mci_wait_while_busy(host, cmd_flags);
399 mci_writel(host, CMD, cmd_flags | SDMMC_CMD_START);
402 static inline void send_stop_abort(struct dw_mci *host, struct mmc_data *data)
404 struct mmc_command *stop = &host->stop_abort;
406 dw_mci_start_command(host, stop, host->stop_cmdr);
409 /* DMA interface functions */
410 static void dw_mci_stop_dma(struct dw_mci *host)
412 if (host->using_dma) {
413 host->dma_ops->stop(host);
414 host->dma_ops->cleanup(host);
417 /* Data transfer was stopped by the interrupt handler */
418 set_bit(EVENT_XFER_COMPLETE, &host->pending_events);
421 static int dw_mci_get_dma_dir(struct mmc_data *data)
423 if (data->flags & MMC_DATA_WRITE)
424 return DMA_TO_DEVICE;
426 return DMA_FROM_DEVICE;
429 static void dw_mci_dma_cleanup(struct dw_mci *host)
431 struct mmc_data *data = host->data;
433 if (data && data->host_cookie == COOKIE_MAPPED) {
434 dma_unmap_sg(host->dev,
437 dw_mci_get_dma_dir(data));
438 data->host_cookie = COOKIE_UNMAPPED;
442 static void dw_mci_idmac_reset(struct dw_mci *host)
444 u32 bmod = mci_readl(host, BMOD);
445 /* Software reset of DMA */
446 bmod |= SDMMC_IDMAC_SWRESET;
447 mci_writel(host, BMOD, bmod);
450 static void dw_mci_idmac_stop_dma(struct dw_mci *host)
454 /* Disable and reset the IDMAC interface */
455 temp = mci_readl(host, CTRL);
456 temp &= ~SDMMC_CTRL_USE_IDMAC;
457 temp |= SDMMC_CTRL_DMA_RESET;
458 mci_writel(host, CTRL, temp);
460 /* Stop the IDMAC running */
461 temp = mci_readl(host, BMOD);
462 temp &= ~(SDMMC_IDMAC_ENABLE | SDMMC_IDMAC_FB);
463 temp |= SDMMC_IDMAC_SWRESET;
464 mci_writel(host, BMOD, temp);
467 static void dw_mci_dmac_complete_dma(void *arg)
469 struct dw_mci *host = arg;
470 struct mmc_data *data = host->data;
472 dev_vdbg(host->dev, "DMA complete\n");
474 if ((host->use_dma == TRANS_MODE_EDMAC) &&
475 data && (data->flags & MMC_DATA_READ))
476 /* Invalidate cache after read */
477 dma_sync_sg_for_cpu(mmc_dev(host->cur_slot->mmc),
482 host->dma_ops->cleanup(host);
485 * If the card was removed, data will be NULL. No point in trying to
486 * send the stop command or waiting for NBUSY in this case.
489 set_bit(EVENT_XFER_COMPLETE, &host->pending_events);
490 tasklet_schedule(&host->tasklet);
494 static int dw_mci_idmac_init(struct dw_mci *host)
498 if (host->dma_64bit_address == 1) {
499 struct idmac_desc_64addr *p;
500 /* Number of descriptors in the ring buffer */
502 DESC_RING_BUF_SZ / sizeof(struct idmac_desc_64addr);
504 /* Forward link the descriptor list */
505 for (i = 0, p = host->sg_cpu; i < host->ring_size - 1;
507 p->des6 = (host->sg_dma +
508 (sizeof(struct idmac_desc_64addr) *
509 (i + 1))) & 0xffffffff;
511 p->des7 = (u64)(host->sg_dma +
512 (sizeof(struct idmac_desc_64addr) *
514 /* Initialize reserved and buffer size fields to "0" */
520 /* Set the last descriptor as the end-of-ring descriptor */
521 p->des6 = host->sg_dma & 0xffffffff;
522 p->des7 = (u64)host->sg_dma >> 32;
523 p->des0 = IDMAC_DES0_ER;
526 struct idmac_desc *p;
527 /* Number of descriptors in the ring buffer */
529 DESC_RING_BUF_SZ / sizeof(struct idmac_desc);
531 /* Forward link the descriptor list */
532 for (i = 0, p = host->sg_cpu;
533 i < host->ring_size - 1;
535 p->des3 = cpu_to_le32(host->sg_dma +
536 (sizeof(struct idmac_desc) * (i + 1)));
540 /* Set the last descriptor as the end-of-ring descriptor */
541 p->des3 = cpu_to_le32(host->sg_dma);
542 p->des0 = cpu_to_le32(IDMAC_DES0_ER);
545 dw_mci_idmac_reset(host);
547 if (host->dma_64bit_address == 1) {
548 /* Mask out interrupts - get Tx & Rx complete only */
549 mci_writel(host, IDSTS64, IDMAC_INT_CLR);
550 mci_writel(host, IDINTEN64, SDMMC_IDMAC_INT_NI |
551 SDMMC_IDMAC_INT_RI | SDMMC_IDMAC_INT_TI);
553 /* Set the descriptor base address */
554 mci_writel(host, DBADDRL, host->sg_dma & 0xffffffff);
555 mci_writel(host, DBADDRU, (u64)host->sg_dma >> 32);
558 /* Mask out interrupts - get Tx & Rx complete only */
559 mci_writel(host, IDSTS, IDMAC_INT_CLR);
560 mci_writel(host, IDINTEN, SDMMC_IDMAC_INT_NI |
561 SDMMC_IDMAC_INT_RI | SDMMC_IDMAC_INT_TI);
563 /* Set the descriptor base address */
564 mci_writel(host, DBADDR, host->sg_dma);
570 static inline int dw_mci_prepare_desc64(struct dw_mci *host,
571 struct mmc_data *data,
574 unsigned int desc_len;
575 struct idmac_desc_64addr *desc_first, *desc_last, *desc;
579 desc_first = desc_last = desc = host->sg_cpu;
581 for (i = 0; i < sg_len; i++) {
582 unsigned int length = sg_dma_len(&data->sg[i]);
584 u64 mem_addr = sg_dma_address(&data->sg[i]);
586 for ( ; length ; desc++) {
587 desc_len = (length <= DW_MCI_DESC_DATA_LENGTH) ?
588 length : DW_MCI_DESC_DATA_LENGTH;
593 * Wait for the former clear OWN bit operation
594 * of IDMAC to make sure that this descriptor
595 * isn't still owned by IDMAC as IDMAC's write
596 * ops and CPU's read ops are asynchronous.
598 if (readl_poll_timeout_atomic(&desc->des0, val,
599 !(val & IDMAC_DES0_OWN),
600 10, 100 * USEC_PER_MSEC))
604 * Set the OWN bit and disable interrupts
605 * for this descriptor
607 desc->des0 = IDMAC_DES0_OWN | IDMAC_DES0_DIC |
611 IDMAC_64ADDR_SET_BUFFER1_SIZE(desc, desc_len);
613 /* Physical address to DMA to/from */
614 desc->des4 = mem_addr & 0xffffffff;
615 desc->des5 = mem_addr >> 32;
617 /* Update physical address for the next desc */
618 mem_addr += desc_len;
620 /* Save pointer to the last descriptor */
625 /* Set first descriptor */
626 desc_first->des0 |= IDMAC_DES0_FD;
628 /* Set last descriptor */
629 desc_last->des0 &= ~(IDMAC_DES0_CH | IDMAC_DES0_DIC);
630 desc_last->des0 |= IDMAC_DES0_LD;
634 /* restore the descriptor chain as it's polluted */
635 dev_dbg(host->dev, "descriptor is still owned by IDMAC.\n");
636 memset(host->sg_cpu, 0, DESC_RING_BUF_SZ);
637 dw_mci_idmac_init(host);
642 static inline int dw_mci_prepare_desc32(struct dw_mci *host,
643 struct mmc_data *data,
646 unsigned int desc_len;
647 struct idmac_desc *desc_first, *desc_last, *desc;
651 desc_first = desc_last = desc = host->sg_cpu;
653 for (i = 0; i < sg_len; i++) {
654 unsigned int length = sg_dma_len(&data->sg[i]);
656 u32 mem_addr = sg_dma_address(&data->sg[i]);
658 for ( ; length ; desc++) {
659 desc_len = (length <= DW_MCI_DESC_DATA_LENGTH) ?
660 length : DW_MCI_DESC_DATA_LENGTH;
665 * Wait for the former clear OWN bit operation
666 * of IDMAC to make sure that this descriptor
667 * isn't still owned by IDMAC as IDMAC's write
668 * ops and CPU's read ops are asynchronous.
670 if (readl_poll_timeout_atomic(&desc->des0, val,
671 IDMAC_OWN_CLR64(val),
673 100 * USEC_PER_MSEC))
677 * Set the OWN bit and disable interrupts
678 * for this descriptor
680 desc->des0 = cpu_to_le32(IDMAC_DES0_OWN |
685 IDMAC_SET_BUFFER1_SIZE(desc, desc_len);
687 /* Physical address to DMA to/from */
688 desc->des2 = cpu_to_le32(mem_addr);
690 /* Update physical address for the next desc */
691 mem_addr += desc_len;
693 /* Save pointer to the last descriptor */
698 /* Set first descriptor */
699 desc_first->des0 |= cpu_to_le32(IDMAC_DES0_FD);
701 /* Set last descriptor */
702 desc_last->des0 &= cpu_to_le32(~(IDMAC_DES0_CH |
704 desc_last->des0 |= cpu_to_le32(IDMAC_DES0_LD);
708 /* restore the descriptor chain as it's polluted */
709 dev_dbg(host->dev, "descriptor is still owned by IDMAC.\n");
710 memset(host->sg_cpu, 0, DESC_RING_BUF_SZ);
711 dw_mci_idmac_init(host);
715 static int dw_mci_idmac_start_dma(struct dw_mci *host, unsigned int sg_len)
720 if (host->dma_64bit_address == 1)
721 ret = dw_mci_prepare_desc64(host, host->data, sg_len);
723 ret = dw_mci_prepare_desc32(host, host->data, sg_len);
728 /* drain writebuffer */
731 /* Make sure to reset DMA in case we did PIO before this */
732 dw_mci_ctrl_reset(host, SDMMC_CTRL_DMA_RESET);
733 dw_mci_idmac_reset(host);
735 /* Select IDMAC interface */
736 temp = mci_readl(host, CTRL);
737 temp |= SDMMC_CTRL_USE_IDMAC;
738 mci_writel(host, CTRL, temp);
740 /* drain writebuffer */
743 /* Enable the IDMAC */
744 temp = mci_readl(host, BMOD);
745 temp |= SDMMC_IDMAC_ENABLE | SDMMC_IDMAC_FB;
746 mci_writel(host, BMOD, temp);
748 /* Start it running */
749 mci_writel(host, PLDMND, 1);
755 static const struct dw_mci_dma_ops dw_mci_idmac_ops = {
756 .init = dw_mci_idmac_init,
757 .start = dw_mci_idmac_start_dma,
758 .stop = dw_mci_idmac_stop_dma,
759 .complete = dw_mci_dmac_complete_dma,
760 .cleanup = dw_mci_dma_cleanup,
763 static void dw_mci_edmac_stop_dma(struct dw_mci *host)
765 dmaengine_terminate_async(host->dms->ch);
768 static int dw_mci_edmac_start_dma(struct dw_mci *host,
771 struct dma_slave_config cfg;
772 struct dma_async_tx_descriptor *desc = NULL;
773 struct scatterlist *sgl = host->data->sg;
774 const u32 mszs[] = {1, 4, 8, 16, 32, 64, 128, 256};
775 u32 sg_elems = host->data->sg_len;
777 u32 fifo_offset = host->fifo_reg - host->regs;
780 /* Set external dma config: burst size, burst width */
781 cfg.dst_addr = host->phy_regs + fifo_offset;
782 cfg.src_addr = cfg.dst_addr;
783 cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
784 cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
786 /* Match burst msize with external dma config */
787 fifoth_val = mci_readl(host, FIFOTH);
788 cfg.dst_maxburst = mszs[(fifoth_val >> 28) & 0x7];
789 cfg.src_maxburst = cfg.dst_maxburst;
791 if (host->data->flags & MMC_DATA_WRITE)
792 cfg.direction = DMA_MEM_TO_DEV;
794 cfg.direction = DMA_DEV_TO_MEM;
796 ret = dmaengine_slave_config(host->dms->ch, &cfg);
798 dev_err(host->dev, "Failed to config edmac.\n");
802 desc = dmaengine_prep_slave_sg(host->dms->ch, sgl,
803 sg_len, cfg.direction,
804 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
806 dev_err(host->dev, "Can't prepare slave sg.\n");
810 /* Set dw_mci_dmac_complete_dma as callback */
811 desc->callback = dw_mci_dmac_complete_dma;
812 desc->callback_param = (void *)host;
813 dmaengine_submit(desc);
815 /* Flush cache before write */
816 if (host->data->flags & MMC_DATA_WRITE)
817 dma_sync_sg_for_device(mmc_dev(host->cur_slot->mmc), sgl,
818 sg_elems, DMA_TO_DEVICE);
820 dma_async_issue_pending(host->dms->ch);
825 static int dw_mci_edmac_init(struct dw_mci *host)
827 /* Request external dma channel */
828 host->dms = kzalloc(sizeof(struct dw_mci_dma_slave), GFP_KERNEL);
832 host->dms->ch = dma_request_slave_channel(host->dev, "rx-tx");
833 if (!host->dms->ch) {
834 dev_err(host->dev, "Failed to get external DMA channel.\n");
843 static void dw_mci_edmac_exit(struct dw_mci *host)
847 dma_release_channel(host->dms->ch);
848 host->dms->ch = NULL;
855 static const struct dw_mci_dma_ops dw_mci_edmac_ops = {
856 .init = dw_mci_edmac_init,
857 .exit = dw_mci_edmac_exit,
858 .start = dw_mci_edmac_start_dma,
859 .stop = dw_mci_edmac_stop_dma,
860 .complete = dw_mci_dmac_complete_dma,
861 .cleanup = dw_mci_dma_cleanup,
864 static int dw_mci_pre_dma_transfer(struct dw_mci *host,
865 struct mmc_data *data,
868 struct scatterlist *sg;
869 unsigned int i, sg_len;
871 if (data->host_cookie == COOKIE_PRE_MAPPED)
875 * We don't do DMA on "complex" transfers, i.e. with
876 * non-word-aligned buffers or lengths. Also, we don't bother
877 * with all the DMA setup overhead for short transfers.
879 if (data->blocks * data->blksz < DW_MCI_DMA_THRESHOLD)
885 for_each_sg(data->sg, sg, data->sg_len, i) {
886 if (sg->offset & 3 || sg->length & 3)
890 sg_len = dma_map_sg(host->dev,
893 dw_mci_get_dma_dir(data));
897 data->host_cookie = cookie;
902 static void dw_mci_pre_req(struct mmc_host *mmc,
903 struct mmc_request *mrq)
905 struct dw_mci_slot *slot = mmc_priv(mmc);
906 struct mmc_data *data = mrq->data;
908 if (!slot->host->use_dma || !data)
911 /* This data might be unmapped at this time */
912 data->host_cookie = COOKIE_UNMAPPED;
914 if (dw_mci_pre_dma_transfer(slot->host, mrq->data,
915 COOKIE_PRE_MAPPED) < 0)
916 data->host_cookie = COOKIE_UNMAPPED;
919 static void dw_mci_post_req(struct mmc_host *mmc,
920 struct mmc_request *mrq,
923 struct dw_mci_slot *slot = mmc_priv(mmc);
924 struct mmc_data *data = mrq->data;
926 if (!slot->host->use_dma || !data)
929 if (data->host_cookie != COOKIE_UNMAPPED)
930 dma_unmap_sg(slot->host->dev,
933 dw_mci_get_dma_dir(data));
934 data->host_cookie = COOKIE_UNMAPPED;
937 static void dw_mci_adjust_fifoth(struct dw_mci *host, struct mmc_data *data)
939 unsigned int blksz = data->blksz;
940 const u32 mszs[] = {1, 4, 8, 16, 32, 64, 128, 256};
941 u32 fifo_width = 1 << host->data_shift;
942 u32 blksz_depth = blksz / fifo_width, fifoth_val;
943 u32 msize = 0, rx_wmark = 1, tx_wmark, tx_wmark_invers;
944 int idx = ARRAY_SIZE(mszs) - 1;
946 /* pio should ship this scenario */
950 tx_wmark = (host->fifo_depth) / 2;
951 tx_wmark_invers = host->fifo_depth - tx_wmark;
955 * if blksz is not a multiple of the FIFO width
957 if (blksz % fifo_width)
961 if (!((blksz_depth % mszs[idx]) ||
962 (tx_wmark_invers % mszs[idx]))) {
964 rx_wmark = mszs[idx] - 1;
969 * If idx is '0', it won't be tried
970 * Thus, initial values are uesed
973 fifoth_val = SDMMC_SET_FIFOTH(msize, rx_wmark, tx_wmark);
974 mci_writel(host, FIFOTH, fifoth_val);
977 static void dw_mci_ctrl_thld(struct dw_mci *host, struct mmc_data *data)
979 unsigned int blksz = data->blksz;
980 u32 blksz_depth, fifo_depth;
985 * CDTHRCTL doesn't exist prior to 240A (in fact that register offset is
986 * in the FIFO region, so we really shouldn't access it).
988 if (host->verid < DW_MMC_240A ||
989 (host->verid < DW_MMC_280A && data->flags & MMC_DATA_WRITE))
993 * Card write Threshold is introduced since 2.80a
994 * It's used when HS400 mode is enabled.
996 if (data->flags & MMC_DATA_WRITE &&
997 !(host->timing != MMC_TIMING_MMC_HS400))
1000 if (data->flags & MMC_DATA_WRITE)
1001 enable = SDMMC_CARD_WR_THR_EN;
1003 enable = SDMMC_CARD_RD_THR_EN;
1005 if (host->timing != MMC_TIMING_MMC_HS200 &&
1006 host->timing != MMC_TIMING_UHS_SDR104)
1009 blksz_depth = blksz / (1 << host->data_shift);
1010 fifo_depth = host->fifo_depth;
1012 if (blksz_depth > fifo_depth)
1016 * If (blksz_depth) >= (fifo_depth >> 1), should be 'thld_size <= blksz'
1017 * If (blksz_depth) < (fifo_depth >> 1), should be thld_size = blksz
1018 * Currently just choose blksz.
1021 mci_writel(host, CDTHRCTL, SDMMC_SET_THLD(thld_size, enable));
1025 mci_writel(host, CDTHRCTL, 0);
1028 static int dw_mci_submit_data_dma(struct dw_mci *host, struct mmc_data *data)
1030 unsigned long irqflags;
1034 host->using_dma = 0;
1036 /* If we don't have a channel, we can't do DMA */
1040 sg_len = dw_mci_pre_dma_transfer(host, data, COOKIE_MAPPED);
1042 host->dma_ops->stop(host);
1046 host->using_dma = 1;
1048 if (host->use_dma == TRANS_MODE_IDMAC)
1050 "sd sg_cpu: %#lx sg_dma: %#lx sg_len: %d\n",
1051 (unsigned long)host->sg_cpu,
1052 (unsigned long)host->sg_dma,
1056 * Decide the MSIZE and RX/TX Watermark.
1057 * If current block size is same with previous size,
1058 * no need to update fifoth.
1060 if (host->prev_blksz != data->blksz)
1061 dw_mci_adjust_fifoth(host, data);
1063 /* Enable the DMA interface */
1064 temp = mci_readl(host, CTRL);
1065 temp |= SDMMC_CTRL_DMA_ENABLE;
1066 mci_writel(host, CTRL, temp);
1068 /* Disable RX/TX IRQs, let DMA handle it */
1069 spin_lock_irqsave(&host->irq_lock, irqflags);
1070 temp = mci_readl(host, INTMASK);
1071 temp &= ~(SDMMC_INT_RXDR | SDMMC_INT_TXDR);
1072 mci_writel(host, INTMASK, temp);
1073 spin_unlock_irqrestore(&host->irq_lock, irqflags);
1075 if (host->dma_ops->start(host, sg_len)) {
1076 host->dma_ops->stop(host);
1077 /* We can't do DMA, try PIO for this one */
1079 "%s: fall back to PIO mode for current transfer\n",
1087 static void dw_mci_submit_data(struct dw_mci *host, struct mmc_data *data)
1089 unsigned long irqflags;
1090 int flags = SG_MITER_ATOMIC;
1093 data->error = -EINPROGRESS;
1095 WARN_ON(host->data);
1099 if (data->flags & MMC_DATA_READ)
1100 host->dir_status = DW_MCI_RECV_STATUS;
1102 host->dir_status = DW_MCI_SEND_STATUS;
1104 dw_mci_ctrl_thld(host, data);
1106 if (dw_mci_submit_data_dma(host, data)) {
1107 if (host->data->flags & MMC_DATA_READ)
1108 flags |= SG_MITER_TO_SG;
1110 flags |= SG_MITER_FROM_SG;
1112 sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
1113 host->sg = data->sg;
1114 host->part_buf_start = 0;
1115 host->part_buf_count = 0;
1117 mci_writel(host, RINTSTS, SDMMC_INT_TXDR | SDMMC_INT_RXDR);
1119 spin_lock_irqsave(&host->irq_lock, irqflags);
1120 temp = mci_readl(host, INTMASK);
1121 temp |= SDMMC_INT_TXDR | SDMMC_INT_RXDR;
1122 mci_writel(host, INTMASK, temp);
1123 spin_unlock_irqrestore(&host->irq_lock, irqflags);
1125 temp = mci_readl(host, CTRL);
1126 temp &= ~SDMMC_CTRL_DMA_ENABLE;
1127 mci_writel(host, CTRL, temp);
1130 * Use the initial fifoth_val for PIO mode. If wm_algined
1131 * is set, we set watermark same as data size.
1132 * If next issued data may be transfered by DMA mode,
1133 * prev_blksz should be invalidated.
1135 if (host->wm_aligned)
1136 dw_mci_adjust_fifoth(host, data);
1138 mci_writel(host, FIFOTH, host->fifoth_val);
1139 host->prev_blksz = 0;
1142 * Keep the current block size.
1143 * It will be used to decide whether to update
1144 * fifoth register next time.
1146 host->prev_blksz = data->blksz;
1150 static void mci_send_cmd(struct dw_mci_slot *slot, u32 cmd, u32 arg)
1152 struct dw_mci *host = slot->host;
1153 unsigned int cmd_status = 0;
1155 mci_writel(host, CMDARG, arg);
1156 wmb(); /* drain writebuffer */
1157 dw_mci_wait_while_busy(host, cmd);
1158 mci_writel(host, CMD, SDMMC_CMD_START | cmd);
1160 if (readl_poll_timeout_atomic(host->regs + SDMMC_CMD, cmd_status,
1161 !(cmd_status & SDMMC_CMD_START),
1162 1, 500 * USEC_PER_MSEC))
1163 dev_err(&slot->mmc->class_dev,
1164 "Timeout sending command (cmd %#x arg %#x status %#x)\n",
1165 cmd, arg, cmd_status);
1168 static void dw_mci_setup_bus(struct dw_mci_slot *slot, bool force_clkinit)
1170 struct dw_mci *host = slot->host;
1171 unsigned int clock = slot->clock;
1174 u32 sdmmc_cmd_bits = SDMMC_CMD_UPD_CLK | SDMMC_CMD_PRV_DAT_WAIT;
1176 /* We must continue to set bit 28 in CMD until the change is complete */
1177 if (host->state == STATE_WAITING_CMD11_DONE)
1178 sdmmc_cmd_bits |= SDMMC_CMD_VOLT_SWITCH;
1181 mci_writel(host, CLKENA, 0);
1182 mci_send_cmd(slot, sdmmc_cmd_bits, 0);
1183 } else if (clock != host->current_speed || force_clkinit) {
1184 div = host->bus_hz / clock;
1185 if (host->bus_hz % clock && host->bus_hz > clock)
1187 * move the + 1 after the divide to prevent
1188 * over-clocking the card.
1192 div = (host->bus_hz != clock) ? DIV_ROUND_UP(div, 2) : 0;
1194 if ((clock != slot->__clk_old &&
1195 !test_bit(DW_MMC_CARD_NEEDS_POLL, &slot->flags)) ||
1197 /* Silent the verbose log if calling from PM context */
1199 dev_info(&slot->mmc->class_dev,
1200 "Bus speed (slot %d) = %dHz (slot req %dHz, actual %dHZ div = %d)\n",
1201 slot->id, host->bus_hz, clock,
1202 div ? ((host->bus_hz / div) >> 1) :
1206 * If card is polling, display the message only
1207 * one time at boot time.
1209 if (slot->mmc->caps & MMC_CAP_NEEDS_POLL &&
1210 slot->mmc->f_min == clock)
1211 set_bit(DW_MMC_CARD_NEEDS_POLL, &slot->flags);
1215 mci_writel(host, CLKENA, 0);
1216 mci_writel(host, CLKSRC, 0);
1219 mci_send_cmd(slot, sdmmc_cmd_bits, 0);
1221 /* set clock to desired speed */
1222 mci_writel(host, CLKDIV, div);
1225 mci_send_cmd(slot, sdmmc_cmd_bits, 0);
1227 /* enable clock; only low power if no SDIO */
1228 clk_en_a = SDMMC_CLKEN_ENABLE << slot->id;
1229 if (!test_bit(DW_MMC_CARD_NO_LOW_PWR, &slot->flags))
1230 clk_en_a |= SDMMC_CLKEN_LOW_PWR << slot->id;
1231 mci_writel(host, CLKENA, clk_en_a);
1234 mci_send_cmd(slot, sdmmc_cmd_bits, 0);
1236 /* keep the last clock value that was requested from core */
1237 slot->__clk_old = clock;
1240 host->current_speed = clock;
1242 /* Set the current slot bus width */
1243 mci_writel(host, CTYPE, (slot->ctype << slot->id));
1246 static void __dw_mci_start_request(struct dw_mci *host,
1247 struct dw_mci_slot *slot,
1248 struct mmc_command *cmd)
1250 struct mmc_request *mrq;
1251 struct mmc_data *data;
1256 host->cur_slot = slot;
1259 host->pending_events = 0;
1260 host->completed_events = 0;
1261 host->cmd_status = 0;
1262 host->data_status = 0;
1263 host->dir_status = 0;
1267 mci_writel(host, TMOUT, 0xFFFFFFFF);
1268 mci_writel(host, BYTCNT, data->blksz*data->blocks);
1269 mci_writel(host, BLKSIZ, data->blksz);
1272 cmdflags = dw_mci_prepare_command(slot->mmc, cmd);
1274 /* this is the first command, send the initialization clock */
1275 if (test_and_clear_bit(DW_MMC_CARD_NEED_INIT, &slot->flags))
1276 cmdflags |= SDMMC_CMD_INIT;
1279 dw_mci_submit_data(host, data);
1280 wmb(); /* drain writebuffer */
1283 dw_mci_start_command(host, cmd, cmdflags);
1285 if (cmd->opcode == SD_SWITCH_VOLTAGE) {
1286 unsigned long irqflags;
1289 * Databook says to fail after 2ms w/ no response, but evidence
1290 * shows that sometimes the cmd11 interrupt takes over 130ms.
1291 * We'll set to 500ms, plus an extra jiffy just in case jiffies
1292 * is just about to roll over.
1294 * We do this whole thing under spinlock and only if the
1295 * command hasn't already completed (indicating the the irq
1296 * already ran so we don't want the timeout).
1298 spin_lock_irqsave(&host->irq_lock, irqflags);
1299 if (!test_bit(EVENT_CMD_COMPLETE, &host->pending_events))
1300 mod_timer(&host->cmd11_timer,
1301 jiffies + msecs_to_jiffies(500) + 1);
1302 spin_unlock_irqrestore(&host->irq_lock, irqflags);
1305 host->stop_cmdr = dw_mci_prep_stop_abort(host, cmd);
1308 static void dw_mci_start_request(struct dw_mci *host,
1309 struct dw_mci_slot *slot)
1311 struct mmc_request *mrq = slot->mrq;
1312 struct mmc_command *cmd;
1314 cmd = mrq->sbc ? mrq->sbc : mrq->cmd;
1315 __dw_mci_start_request(host, slot, cmd);
1318 /* must be called with host->lock held */
1319 static void dw_mci_queue_request(struct dw_mci *host, struct dw_mci_slot *slot,
1320 struct mmc_request *mrq)
1322 dev_vdbg(&slot->mmc->class_dev, "queue request: state=%d\n",
1327 if (host->state == STATE_WAITING_CMD11_DONE) {
1328 dev_warn(&slot->mmc->class_dev,
1329 "Voltage change didn't complete\n");
1331 * this case isn't expected to happen, so we can
1332 * either crash here or just try to continue on
1333 * in the closest possible state
1335 host->state = STATE_IDLE;
1338 if (host->state == STATE_IDLE) {
1339 host->state = STATE_SENDING_CMD;
1340 dw_mci_start_request(host, slot);
1342 list_add_tail(&slot->queue_node, &host->queue);
1346 static void dw_mci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1348 struct dw_mci_slot *slot = mmc_priv(mmc);
1349 struct dw_mci *host = slot->host;
1354 * The check for card presence and queueing of the request must be
1355 * atomic, otherwise the card could be removed in between and the
1356 * request wouldn't fail until another card was inserted.
1359 if (!dw_mci_get_cd(mmc)) {
1360 mrq->cmd->error = -ENOMEDIUM;
1361 mmc_request_done(mmc, mrq);
1365 spin_lock_bh(&host->lock);
1367 dw_mci_queue_request(host, slot, mrq);
1369 spin_unlock_bh(&host->lock);
1372 static void dw_mci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1374 struct dw_mci_slot *slot = mmc_priv(mmc);
1375 const struct dw_mci_drv_data *drv_data = slot->host->drv_data;
1379 switch (ios->bus_width) {
1380 case MMC_BUS_WIDTH_4:
1381 slot->ctype = SDMMC_CTYPE_4BIT;
1383 case MMC_BUS_WIDTH_8:
1384 slot->ctype = SDMMC_CTYPE_8BIT;
1387 /* set default 1 bit mode */
1388 slot->ctype = SDMMC_CTYPE_1BIT;
1391 regs = mci_readl(slot->host, UHS_REG);
1394 if (ios->timing == MMC_TIMING_MMC_DDR52 ||
1395 ios->timing == MMC_TIMING_UHS_DDR50 ||
1396 ios->timing == MMC_TIMING_MMC_HS400)
1397 regs |= ((0x1 << slot->id) << 16);
1399 regs &= ~((0x1 << slot->id) << 16);
1401 mci_writel(slot->host, UHS_REG, regs);
1402 slot->host->timing = ios->timing;
1405 * Use mirror of ios->clock to prevent race with mmc
1406 * core ios update when finding the minimum.
1408 slot->clock = ios->clock;
1410 if (drv_data && drv_data->set_ios)
1411 drv_data->set_ios(slot->host, ios);
1413 switch (ios->power_mode) {
1415 if (!IS_ERR(mmc->supply.vmmc)) {
1416 ret = mmc_regulator_set_ocr(mmc, mmc->supply.vmmc,
1419 dev_err(slot->host->dev,
1420 "failed to enable vmmc regulator\n");
1421 /*return, if failed turn on vmmc*/
1425 set_bit(DW_MMC_CARD_NEED_INIT, &slot->flags);
1426 regs = mci_readl(slot->host, PWREN);
1427 regs |= (1 << slot->id);
1428 mci_writel(slot->host, PWREN, regs);
1431 if (!slot->host->vqmmc_enabled) {
1432 if (!IS_ERR(mmc->supply.vqmmc)) {
1433 ret = regulator_enable(mmc->supply.vqmmc);
1435 dev_err(slot->host->dev,
1436 "failed to enable vqmmc\n");
1438 slot->host->vqmmc_enabled = true;
1441 /* Keep track so we don't reset again */
1442 slot->host->vqmmc_enabled = true;
1445 /* Reset our state machine after powering on */
1446 dw_mci_ctrl_reset(slot->host,
1447 SDMMC_CTRL_ALL_RESET_FLAGS);
1450 /* Adjust clock / bus width after power is up */
1451 dw_mci_setup_bus(slot, false);
1455 /* Turn clock off before power goes down */
1456 dw_mci_setup_bus(slot, false);
1458 if (!IS_ERR(mmc->supply.vmmc))
1459 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0);
1461 if (!IS_ERR(mmc->supply.vqmmc) && slot->host->vqmmc_enabled)
1462 regulator_disable(mmc->supply.vqmmc);
1463 slot->host->vqmmc_enabled = false;
1465 regs = mci_readl(slot->host, PWREN);
1466 regs &= ~(1 << slot->id);
1467 mci_writel(slot->host, PWREN, regs);
1473 if (slot->host->state == STATE_WAITING_CMD11_DONE && ios->clock != 0)
1474 slot->host->state = STATE_IDLE;
1477 static int dw_mci_card_busy(struct mmc_host *mmc)
1479 struct dw_mci_slot *slot = mmc_priv(mmc);
1483 * Check the busy bit which is low when DAT[3:0]
1484 * (the data lines) are 0000
1486 status = mci_readl(slot->host, STATUS);
1488 return !!(status & SDMMC_STATUS_BUSY);
1491 static int dw_mci_switch_voltage(struct mmc_host *mmc, struct mmc_ios *ios)
1493 struct dw_mci_slot *slot = mmc_priv(mmc);
1494 struct dw_mci *host = slot->host;
1495 const struct dw_mci_drv_data *drv_data = host->drv_data;
1497 u32 v18 = SDMMC_UHS_18V << slot->id;
1500 if (drv_data && drv_data->switch_voltage)
1501 return drv_data->switch_voltage(mmc, ios);
1504 * Program the voltage. Note that some instances of dw_mmc may use
1505 * the UHS_REG for this. For other instances (like exynos) the UHS_REG
1506 * does no harm but you need to set the regulator directly. Try both.
1508 uhs = mci_readl(host, UHS_REG);
1509 if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_330)
1514 if (!IS_ERR(mmc->supply.vqmmc)) {
1515 ret = mmc_regulator_set_vqmmc(mmc, ios);
1518 dev_dbg(&mmc->class_dev,
1519 "Regulator set error %d - %s V\n",
1520 ret, uhs & v18 ? "1.8" : "3.3");
1524 mci_writel(host, UHS_REG, uhs);
1529 static int dw_mci_get_ro(struct mmc_host *mmc)
1532 struct dw_mci_slot *slot = mmc_priv(mmc);
1533 int gpio_ro = mmc_gpio_get_ro(mmc);
1535 /* Use platform get_ro function, else try on board write protect */
1537 read_only = gpio_ro;
1540 mci_readl(slot->host, WRTPRT) & (1 << slot->id) ? 1 : 0;
1542 dev_dbg(&mmc->class_dev, "card is %s\n",
1543 read_only ? "read-only" : "read-write");
1548 static int dw_mci_get_cd(struct mmc_host *mmc)
1551 struct dw_mci_slot *slot = mmc_priv(mmc);
1552 struct dw_mci *host = slot->host;
1553 int gpio_cd = mmc_gpio_get_cd(mmc);
1555 /* Use platform get_cd function, else try onboard card detect */
1556 if (((mmc->caps & MMC_CAP_NEEDS_POLL)
1557 || !mmc_card_is_removable(mmc))) {
1560 if (!test_bit(DW_MMC_CARD_PRESENT, &slot->flags)) {
1561 if (mmc->caps & MMC_CAP_NEEDS_POLL) {
1562 dev_info(&mmc->class_dev,
1563 "card is polling.\n");
1565 dev_info(&mmc->class_dev,
1566 "card is non-removable.\n");
1568 set_bit(DW_MMC_CARD_PRESENT, &slot->flags);
1572 } else if (gpio_cd >= 0)
1575 present = (mci_readl(slot->host, CDETECT) & (1 << slot->id))
1578 spin_lock_bh(&host->lock);
1579 if (present && !test_and_set_bit(DW_MMC_CARD_PRESENT, &slot->flags))
1580 dev_dbg(&mmc->class_dev, "card is present\n");
1581 else if (!present &&
1582 !test_and_clear_bit(DW_MMC_CARD_PRESENT, &slot->flags))
1583 dev_dbg(&mmc->class_dev, "card is not present\n");
1584 spin_unlock_bh(&host->lock);
1589 static void dw_mci_hw_reset(struct mmc_host *mmc)
1591 struct dw_mci_slot *slot = mmc_priv(mmc);
1592 struct dw_mci *host = slot->host;
1595 if (host->use_dma == TRANS_MODE_IDMAC)
1596 dw_mci_idmac_reset(host);
1598 if (!dw_mci_ctrl_reset(host, SDMMC_CTRL_DMA_RESET |
1599 SDMMC_CTRL_FIFO_RESET))
1603 * According to eMMC spec, card reset procedure:
1604 * tRstW >= 1us: RST_n pulse width
1605 * tRSCA >= 200us: RST_n to Command time
1606 * tRSTH >= 1us: RST_n high period
1608 reset = mci_readl(host, RST_N);
1609 reset &= ~(SDMMC_RST_HWACTIVE << slot->id);
1610 mci_writel(host, RST_N, reset);
1612 reset |= SDMMC_RST_HWACTIVE << slot->id;
1613 mci_writel(host, RST_N, reset);
1614 usleep_range(200, 300);
1617 static void dw_mci_init_card(struct mmc_host *mmc, struct mmc_card *card)
1619 struct dw_mci_slot *slot = mmc_priv(mmc);
1620 struct dw_mci *host = slot->host;
1623 * Low power mode will stop the card clock when idle. According to the
1624 * description of the CLKENA register we should disable low power mode
1625 * for SDIO cards if we need SDIO interrupts to work.
1627 if (mmc->caps & MMC_CAP_SDIO_IRQ) {
1628 const u32 clken_low_pwr = SDMMC_CLKEN_LOW_PWR << slot->id;
1632 clk_en_a_old = mci_readl(host, CLKENA);
1634 if (card->type == MMC_TYPE_SDIO ||
1635 card->type == MMC_TYPE_SD_COMBO) {
1636 if (!test_bit(DW_MMC_CARD_NO_LOW_PWR, &slot->flags)) {
1637 pm_runtime_get_noresume(mmc->parent);
1638 set_bit(DW_MMC_CARD_NO_LOW_PWR, &slot->flags);
1640 clk_en_a = clk_en_a_old & ~clken_low_pwr;
1642 if (test_bit(DW_MMC_CARD_NO_LOW_PWR, &slot->flags)) {
1643 pm_runtime_put_noidle(mmc->parent);
1644 clear_bit(DW_MMC_CARD_NO_LOW_PWR, &slot->flags);
1646 clk_en_a = clk_en_a_old | clken_low_pwr;
1649 if (clk_en_a != clk_en_a_old) {
1650 mci_writel(host, CLKENA, clk_en_a);
1651 mci_send_cmd(slot, SDMMC_CMD_UPD_CLK |
1652 SDMMC_CMD_PRV_DAT_WAIT, 0);
1657 static void dw_mci_enable_sdio_irq(struct mmc_host *mmc, int enb)
1659 struct dw_mci_slot *slot = mmc_priv(mmc);
1660 struct dw_mci *host = slot->host;
1661 unsigned long irqflags;
1664 spin_lock_irqsave(&host->irq_lock, irqflags);
1666 /* Enable/disable Slot Specific SDIO interrupt */
1667 int_mask = mci_readl(host, INTMASK);
1669 int_mask |= SDMMC_INT_SDIO(slot->sdio_id);
1671 int_mask &= ~SDMMC_INT_SDIO(slot->sdio_id);
1672 mci_writel(host, INTMASK, int_mask);
1674 spin_unlock_irqrestore(&host->irq_lock, irqflags);
1677 static int dw_mci_execute_tuning(struct mmc_host *mmc, u32 opcode)
1679 struct dw_mci_slot *slot = mmc_priv(mmc);
1680 struct dw_mci *host = slot->host;
1681 const struct dw_mci_drv_data *drv_data = host->drv_data;
1684 if (drv_data && drv_data->execute_tuning)
1685 err = drv_data->execute_tuning(slot, opcode);
1689 static int dw_mci_prepare_hs400_tuning(struct mmc_host *mmc,
1690 struct mmc_ios *ios)
1692 struct dw_mci_slot *slot = mmc_priv(mmc);
1693 struct dw_mci *host = slot->host;
1694 const struct dw_mci_drv_data *drv_data = host->drv_data;
1696 if (drv_data && drv_data->prepare_hs400_tuning)
1697 return drv_data->prepare_hs400_tuning(host, ios);
1702 static bool dw_mci_reset(struct dw_mci *host)
1704 u32 flags = SDMMC_CTRL_RESET | SDMMC_CTRL_FIFO_RESET;
1708 * Resetting generates a block interrupt, hence setting
1709 * the scatter-gather pointer to NULL.
1712 sg_miter_stop(&host->sg_miter);
1717 flags |= SDMMC_CTRL_DMA_RESET;
1719 if (dw_mci_ctrl_reset(host, flags)) {
1721 * In all cases we clear the RAWINTS register to clear any
1724 mci_writel(host, RINTSTS, 0xFFFFFFFF);
1726 /* if using dma we wait for dma_req to clear */
1727 if (host->use_dma) {
1730 if (readl_poll_timeout_atomic(host->regs + SDMMC_STATUS,
1732 !(status & SDMMC_STATUS_DMA_REQ),
1733 1, 500 * USEC_PER_MSEC)) {
1735 "%s: Timeout waiting for dma_req to clear during reset\n",
1740 /* when using DMA next we reset the fifo again */
1741 if (!dw_mci_ctrl_reset(host, SDMMC_CTRL_FIFO_RESET))
1745 /* if the controller reset bit did clear, then set clock regs */
1746 if (!(mci_readl(host, CTRL) & SDMMC_CTRL_RESET)) {
1748 "%s: fifo/dma reset bits didn't clear but ciu was reset, doing clock update\n",
1754 if (host->use_dma == TRANS_MODE_IDMAC)
1755 /* It is also recommended that we reset and reprogram idmac */
1756 dw_mci_idmac_reset(host);
1761 /* After a CTRL reset we need to have CIU set clock registers */
1762 mci_send_cmd(host->cur_slot, SDMMC_CMD_UPD_CLK, 0);
1767 static const struct mmc_host_ops dw_mci_ops = {
1768 .request = dw_mci_request,
1769 .pre_req = dw_mci_pre_req,
1770 .post_req = dw_mci_post_req,
1771 .set_ios = dw_mci_set_ios,
1772 .get_ro = dw_mci_get_ro,
1773 .get_cd = dw_mci_get_cd,
1774 .hw_reset = dw_mci_hw_reset,
1775 .enable_sdio_irq = dw_mci_enable_sdio_irq,
1776 .execute_tuning = dw_mci_execute_tuning,
1777 .card_busy = dw_mci_card_busy,
1778 .start_signal_voltage_switch = dw_mci_switch_voltage,
1779 .init_card = dw_mci_init_card,
1780 .prepare_hs400_tuning = dw_mci_prepare_hs400_tuning,
1783 static void dw_mci_request_end(struct dw_mci *host, struct mmc_request *mrq)
1784 __releases(&host->lock)
1785 __acquires(&host->lock)
1787 struct dw_mci_slot *slot;
1788 struct mmc_host *prev_mmc = host->cur_slot->mmc;
1790 WARN_ON(host->cmd || host->data);
1792 host->cur_slot->mrq = NULL;
1794 if (!list_empty(&host->queue)) {
1795 slot = list_entry(host->queue.next,
1796 struct dw_mci_slot, queue_node);
1797 list_del(&slot->queue_node);
1798 dev_vdbg(host->dev, "list not empty: %s is next\n",
1799 mmc_hostname(slot->mmc));
1800 host->state = STATE_SENDING_CMD;
1801 dw_mci_start_request(host, slot);
1803 dev_vdbg(host->dev, "list empty\n");
1805 if (host->state == STATE_SENDING_CMD11)
1806 host->state = STATE_WAITING_CMD11_DONE;
1808 host->state = STATE_IDLE;
1811 spin_unlock(&host->lock);
1812 mmc_request_done(prev_mmc, mrq);
1813 spin_lock(&host->lock);
1816 static int dw_mci_command_complete(struct dw_mci *host, struct mmc_command *cmd)
1818 u32 status = host->cmd_status;
1820 host->cmd_status = 0;
1822 /* Read the response from the card (up to 16 bytes) */
1823 if (cmd->flags & MMC_RSP_PRESENT) {
1824 if (cmd->flags & MMC_RSP_136) {
1825 cmd->resp[3] = mci_readl(host, RESP0);
1826 cmd->resp[2] = mci_readl(host, RESP1);
1827 cmd->resp[1] = mci_readl(host, RESP2);
1828 cmd->resp[0] = mci_readl(host, RESP3);
1830 cmd->resp[0] = mci_readl(host, RESP0);
1837 if (status & SDMMC_INT_RTO)
1838 cmd->error = -ETIMEDOUT;
1839 else if ((cmd->flags & MMC_RSP_CRC) && (status & SDMMC_INT_RCRC))
1840 cmd->error = -EILSEQ;
1841 else if (status & SDMMC_INT_RESP_ERR)
1849 static int dw_mci_data_complete(struct dw_mci *host, struct mmc_data *data)
1851 u32 status = host->data_status;
1853 if (status & DW_MCI_DATA_ERROR_FLAGS) {
1854 if (status & SDMMC_INT_DRTO) {
1855 data->error = -ETIMEDOUT;
1856 } else if (status & SDMMC_INT_DCRC) {
1857 data->error = -EILSEQ;
1858 } else if (status & SDMMC_INT_EBE) {
1859 if (host->dir_status ==
1860 DW_MCI_SEND_STATUS) {
1862 * No data CRC status was returned.
1863 * The number of bytes transferred
1864 * will be exaggerated in PIO mode.
1866 data->bytes_xfered = 0;
1867 data->error = -ETIMEDOUT;
1868 } else if (host->dir_status ==
1869 DW_MCI_RECV_STATUS) {
1870 data->error = -EILSEQ;
1873 /* SDMMC_INT_SBE is included */
1874 data->error = -EILSEQ;
1877 dev_dbg(host->dev, "data error, status 0x%08x\n", status);
1880 * After an error, there may be data lingering
1885 data->bytes_xfered = data->blocks * data->blksz;
1892 static void dw_mci_set_drto(struct dw_mci *host)
1894 unsigned int drto_clks;
1895 unsigned int drto_ms;
1897 drto_clks = mci_readl(host, TMOUT) >> 8;
1898 drto_ms = DIV_ROUND_UP(drto_clks, host->bus_hz / 1000);
1900 /* add a bit spare time */
1903 mod_timer(&host->dto_timer, jiffies + msecs_to_jiffies(drto_ms));
1906 static void dw_mci_tasklet_func(unsigned long priv)
1908 struct dw_mci *host = (struct dw_mci *)priv;
1909 struct mmc_data *data;
1910 struct mmc_command *cmd;
1911 struct mmc_request *mrq;
1912 enum dw_mci_state state;
1913 enum dw_mci_state prev_state;
1916 spin_lock(&host->lock);
1918 state = host->state;
1927 case STATE_WAITING_CMD11_DONE:
1930 case STATE_SENDING_CMD11:
1931 case STATE_SENDING_CMD:
1932 if (!test_and_clear_bit(EVENT_CMD_COMPLETE,
1933 &host->pending_events))
1938 set_bit(EVENT_CMD_COMPLETE, &host->completed_events);
1939 err = dw_mci_command_complete(host, cmd);
1940 if (cmd == mrq->sbc && !err) {
1941 prev_state = state = STATE_SENDING_CMD;
1942 __dw_mci_start_request(host, host->cur_slot,
1947 if (cmd->data && err) {
1949 * During UHS tuning sequence, sending the stop
1950 * command after the response CRC error would
1951 * throw the system into a confused state
1952 * causing all future tuning phases to report
1955 * In such case controller will move into a data
1956 * transfer state after a response error or
1957 * response CRC error. Let's let that finish
1958 * before trying to send a stop, so we'll go to
1959 * STATE_SENDING_DATA.
1961 * Although letting the data transfer take place
1962 * will waste a bit of time (we already know
1963 * the command was bad), it can't cause any
1964 * errors since it's possible it would have
1965 * taken place anyway if this tasklet got
1966 * delayed. Allowing the transfer to take place
1967 * avoids races and keeps things simple.
1969 if ((err != -ETIMEDOUT) &&
1970 (cmd->opcode == MMC_SEND_TUNING_BLOCK)) {
1971 state = STATE_SENDING_DATA;
1975 dw_mci_stop_dma(host);
1976 send_stop_abort(host, data);
1977 state = STATE_SENDING_STOP;
1981 if (!cmd->data || err) {
1982 dw_mci_request_end(host, mrq);
1986 prev_state = state = STATE_SENDING_DATA;
1989 case STATE_SENDING_DATA:
1991 * We could get a data error and never a transfer
1992 * complete so we'd better check for it here.
1994 * Note that we don't really care if we also got a
1995 * transfer complete; stopping the DMA and sending an
1998 if (test_and_clear_bit(EVENT_DATA_ERROR,
1999 &host->pending_events)) {
2000 dw_mci_stop_dma(host);
2001 if (!(host->data_status & (SDMMC_INT_DRTO |
2003 send_stop_abort(host, data);
2004 state = STATE_DATA_ERROR;
2008 if (!test_and_clear_bit(EVENT_XFER_COMPLETE,
2009 &host->pending_events)) {
2011 * If all data-related interrupts don't come
2012 * within the given time in reading data state.
2014 if (host->dir_status == DW_MCI_RECV_STATUS)
2015 dw_mci_set_drto(host);
2019 set_bit(EVENT_XFER_COMPLETE, &host->completed_events);
2022 * Handle an EVENT_DATA_ERROR that might have shown up
2023 * before the transfer completed. This might not have
2024 * been caught by the check above because the interrupt
2025 * could have gone off between the previous check and
2026 * the check for transfer complete.
2028 * Technically this ought not be needed assuming we
2029 * get a DATA_COMPLETE eventually (we'll notice the
2030 * error and end the request), but it shouldn't hurt.
2032 * This has the advantage of sending the stop command.
2034 if (test_and_clear_bit(EVENT_DATA_ERROR,
2035 &host->pending_events)) {
2036 dw_mci_stop_dma(host);
2037 if (!(host->data_status & (SDMMC_INT_DRTO |
2039 send_stop_abort(host, data);
2040 state = STATE_DATA_ERROR;
2043 prev_state = state = STATE_DATA_BUSY;
2047 case STATE_DATA_BUSY:
2048 if (!test_and_clear_bit(EVENT_DATA_COMPLETE,
2049 &host->pending_events)) {
2051 * If data error interrupt comes but data over
2052 * interrupt doesn't come within the given time.
2053 * in reading data state.
2055 if (host->dir_status == DW_MCI_RECV_STATUS)
2056 dw_mci_set_drto(host);
2061 set_bit(EVENT_DATA_COMPLETE, &host->completed_events);
2062 err = dw_mci_data_complete(host, data);
2065 if (!data->stop || mrq->sbc) {
2066 if (mrq->sbc && data->stop)
2067 data->stop->error = 0;
2068 dw_mci_request_end(host, mrq);
2072 /* stop command for open-ended transfer*/
2074 send_stop_abort(host, data);
2077 * If we don't have a command complete now we'll
2078 * never get one since we just reset everything;
2079 * better end the request.
2081 * If we do have a command complete we'll fall
2082 * through to the SENDING_STOP command and
2083 * everything will be peachy keen.
2085 if (!test_bit(EVENT_CMD_COMPLETE,
2086 &host->pending_events)) {
2088 dw_mci_request_end(host, mrq);
2094 * If err has non-zero,
2095 * stop-abort command has been already issued.
2097 prev_state = state = STATE_SENDING_STOP;
2101 case STATE_SENDING_STOP:
2102 if (!test_and_clear_bit(EVENT_CMD_COMPLETE,
2103 &host->pending_events))
2106 /* CMD error in data command */
2107 if (mrq->cmd->error && mrq->data)
2113 if (!mrq->sbc && mrq->stop)
2114 dw_mci_command_complete(host, mrq->stop);
2116 host->cmd_status = 0;
2118 dw_mci_request_end(host, mrq);
2121 case STATE_DATA_ERROR:
2122 if (!test_and_clear_bit(EVENT_XFER_COMPLETE,
2123 &host->pending_events))
2126 state = STATE_DATA_BUSY;
2129 } while (state != prev_state);
2131 host->state = state;
2133 spin_unlock(&host->lock);
2137 /* push final bytes to part_buf, only use during push */
2138 static void dw_mci_set_part_bytes(struct dw_mci *host, void *buf, int cnt)
2140 memcpy((void *)&host->part_buf, buf, cnt);
2141 host->part_buf_count = cnt;
2144 /* append bytes to part_buf, only use during push */
2145 static int dw_mci_push_part_bytes(struct dw_mci *host, void *buf, int cnt)
2147 cnt = min(cnt, (1 << host->data_shift) - host->part_buf_count);
2148 memcpy((void *)&host->part_buf + host->part_buf_count, buf, cnt);
2149 host->part_buf_count += cnt;
2153 /* pull first bytes from part_buf, only use during pull */
2154 static int dw_mci_pull_part_bytes(struct dw_mci *host, void *buf, int cnt)
2156 cnt = min_t(int, cnt, host->part_buf_count);
2158 memcpy(buf, (void *)&host->part_buf + host->part_buf_start,
2160 host->part_buf_count -= cnt;
2161 host->part_buf_start += cnt;
2166 /* pull final bytes from the part_buf, assuming it's just been filled */
2167 static void dw_mci_pull_final_bytes(struct dw_mci *host, void *buf, int cnt)
2169 memcpy(buf, &host->part_buf, cnt);
2170 host->part_buf_start = cnt;
2171 host->part_buf_count = (1 << host->data_shift) - cnt;
2174 static void dw_mci_push_data16(struct dw_mci *host, void *buf, int cnt)
2176 struct mmc_data *data = host->data;
2179 /* try and push anything in the part_buf */
2180 if (unlikely(host->part_buf_count)) {
2181 int len = dw_mci_push_part_bytes(host, buf, cnt);
2185 if (host->part_buf_count == 2) {
2186 mci_fifo_writew(host->fifo_reg, host->part_buf16);
2187 host->part_buf_count = 0;
2190 #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
2191 if (unlikely((unsigned long)buf & 0x1)) {
2193 u16 aligned_buf[64];
2194 int len = min(cnt & -2, (int)sizeof(aligned_buf));
2195 int items = len >> 1;
2197 /* memcpy from input buffer into aligned buffer */
2198 memcpy(aligned_buf, buf, len);
2201 /* push data from aligned buffer into fifo */
2202 for (i = 0; i < items; ++i)
2203 mci_fifo_writew(host->fifo_reg, aligned_buf[i]);
2210 for (; cnt >= 2; cnt -= 2)
2211 mci_fifo_writew(host->fifo_reg, *pdata++);
2214 /* put anything remaining in the part_buf */
2216 dw_mci_set_part_bytes(host, buf, cnt);
2217 /* Push data if we have reached the expected data length */
2218 if ((data->bytes_xfered + init_cnt) ==
2219 (data->blksz * data->blocks))
2220 mci_fifo_writew(host->fifo_reg, host->part_buf16);
2224 static void dw_mci_pull_data16(struct dw_mci *host, void *buf, int cnt)
2226 #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
2227 if (unlikely((unsigned long)buf & 0x1)) {
2229 /* pull data from fifo into aligned buffer */
2230 u16 aligned_buf[64];
2231 int len = min(cnt & -2, (int)sizeof(aligned_buf));
2232 int items = len >> 1;
2235 for (i = 0; i < items; ++i)
2236 aligned_buf[i] = mci_fifo_readw(host->fifo_reg);
2237 /* memcpy from aligned buffer into output buffer */
2238 memcpy(buf, aligned_buf, len);
2247 for (; cnt >= 2; cnt -= 2)
2248 *pdata++ = mci_fifo_readw(host->fifo_reg);
2252 host->part_buf16 = mci_fifo_readw(host->fifo_reg);
2253 dw_mci_pull_final_bytes(host, buf, cnt);
2257 static void dw_mci_push_data32(struct dw_mci *host, void *buf, int cnt)
2259 struct mmc_data *data = host->data;
2262 /* try and push anything in the part_buf */
2263 if (unlikely(host->part_buf_count)) {
2264 int len = dw_mci_push_part_bytes(host, buf, cnt);
2268 if (host->part_buf_count == 4) {
2269 mci_fifo_writel(host->fifo_reg, host->part_buf32);
2270 host->part_buf_count = 0;
2273 #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
2274 if (unlikely((unsigned long)buf & 0x3)) {
2276 u32 aligned_buf[32];
2277 int len = min(cnt & -4, (int)sizeof(aligned_buf));
2278 int items = len >> 2;
2280 /* memcpy from input buffer into aligned buffer */
2281 memcpy(aligned_buf, buf, len);
2284 /* push data from aligned buffer into fifo */
2285 for (i = 0; i < items; ++i)
2286 mci_fifo_writel(host->fifo_reg, aligned_buf[i]);
2293 for (; cnt >= 4; cnt -= 4)
2294 mci_fifo_writel(host->fifo_reg, *pdata++);
2297 /* put anything remaining in the part_buf */
2299 dw_mci_set_part_bytes(host, buf, cnt);
2300 /* Push data if we have reached the expected data length */
2301 if ((data->bytes_xfered + init_cnt) ==
2302 (data->blksz * data->blocks))
2303 mci_fifo_writel(host->fifo_reg, host->part_buf32);
2307 static void dw_mci_pull_data32(struct dw_mci *host, void *buf, int cnt)
2309 #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
2310 if (unlikely((unsigned long)buf & 0x3)) {
2312 /* pull data from fifo into aligned buffer */
2313 u32 aligned_buf[32];
2314 int len = min(cnt & -4, (int)sizeof(aligned_buf));
2315 int items = len >> 2;
2318 for (i = 0; i < items; ++i)
2319 aligned_buf[i] = mci_fifo_readl(host->fifo_reg);
2320 /* memcpy from aligned buffer into output buffer */
2321 memcpy(buf, aligned_buf, len);
2330 for (; cnt >= 4; cnt -= 4)
2331 *pdata++ = mci_fifo_readl(host->fifo_reg);
2335 host->part_buf32 = mci_fifo_readl(host->fifo_reg);
2336 dw_mci_pull_final_bytes(host, buf, cnt);
2340 static void dw_mci_push_data64(struct dw_mci *host, void *buf, int cnt)
2342 struct mmc_data *data = host->data;
2345 /* try and push anything in the part_buf */
2346 if (unlikely(host->part_buf_count)) {
2347 int len = dw_mci_push_part_bytes(host, buf, cnt);
2352 if (host->part_buf_count == 8) {
2353 mci_fifo_writeq(host->fifo_reg, host->part_buf);
2354 host->part_buf_count = 0;
2357 #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
2358 if (unlikely((unsigned long)buf & 0x7)) {
2360 u64 aligned_buf[16];
2361 int len = min(cnt & -8, (int)sizeof(aligned_buf));
2362 int items = len >> 3;
2364 /* memcpy from input buffer into aligned buffer */
2365 memcpy(aligned_buf, buf, len);
2368 /* push data from aligned buffer into fifo */
2369 for (i = 0; i < items; ++i)
2370 mci_fifo_writeq(host->fifo_reg, aligned_buf[i]);
2377 for (; cnt >= 8; cnt -= 8)
2378 mci_fifo_writeq(host->fifo_reg, *pdata++);
2381 /* put anything remaining in the part_buf */
2383 dw_mci_set_part_bytes(host, buf, cnt);
2384 /* Push data if we have reached the expected data length */
2385 if ((data->bytes_xfered + init_cnt) ==
2386 (data->blksz * data->blocks))
2387 mci_fifo_writeq(host->fifo_reg, host->part_buf);
2391 static void dw_mci_pull_data64(struct dw_mci *host, void *buf, int cnt)
2393 #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
2394 if (unlikely((unsigned long)buf & 0x7)) {
2396 /* pull data from fifo into aligned buffer */
2397 u64 aligned_buf[16];
2398 int len = min(cnt & -8, (int)sizeof(aligned_buf));
2399 int items = len >> 3;
2402 for (i = 0; i < items; ++i)
2403 aligned_buf[i] = mci_fifo_readq(host->fifo_reg);
2405 /* memcpy from aligned buffer into output buffer */
2406 memcpy(buf, aligned_buf, len);
2415 for (; cnt >= 8; cnt -= 8)
2416 *pdata++ = mci_fifo_readq(host->fifo_reg);
2420 host->part_buf = mci_fifo_readq(host->fifo_reg);
2421 dw_mci_pull_final_bytes(host, buf, cnt);
2425 static void dw_mci_pull_data(struct dw_mci *host, void *buf, int cnt)
2429 /* get remaining partial bytes */
2430 len = dw_mci_pull_part_bytes(host, buf, cnt);
2431 if (unlikely(len == cnt))
2436 /* get the rest of the data */
2437 host->pull_data(host, buf, cnt);
2440 static void dw_mci_read_data_pio(struct dw_mci *host, bool dto)
2442 struct sg_mapping_iter *sg_miter = &host->sg_miter;
2444 unsigned int offset;
2445 struct mmc_data *data = host->data;
2446 int shift = host->data_shift;
2449 unsigned int remain, fcnt;
2452 if (!sg_miter_next(sg_miter))
2455 host->sg = sg_miter->piter.sg;
2456 buf = sg_miter->addr;
2457 remain = sg_miter->length;
2461 fcnt = (SDMMC_GET_FCNT(mci_readl(host, STATUS))
2462 << shift) + host->part_buf_count;
2463 len = min(remain, fcnt);
2466 dw_mci_pull_data(host, (void *)(buf + offset), len);
2467 data->bytes_xfered += len;
2472 sg_miter->consumed = offset;
2473 status = mci_readl(host, MINTSTS);
2474 mci_writel(host, RINTSTS, SDMMC_INT_RXDR);
2475 /* if the RXDR is ready read again */
2476 } while ((status & SDMMC_INT_RXDR) ||
2477 (dto && SDMMC_GET_FCNT(mci_readl(host, STATUS))));
2480 if (!sg_miter_next(sg_miter))
2482 sg_miter->consumed = 0;
2484 sg_miter_stop(sg_miter);
2488 sg_miter_stop(sg_miter);
2490 smp_wmb(); /* drain writebuffer */
2491 set_bit(EVENT_XFER_COMPLETE, &host->pending_events);
2494 static void dw_mci_write_data_pio(struct dw_mci *host)
2496 struct sg_mapping_iter *sg_miter = &host->sg_miter;
2498 unsigned int offset;
2499 struct mmc_data *data = host->data;
2500 int shift = host->data_shift;
2503 unsigned int fifo_depth = host->fifo_depth;
2504 unsigned int remain, fcnt;
2507 if (!sg_miter_next(sg_miter))
2510 host->sg = sg_miter->piter.sg;
2511 buf = sg_miter->addr;
2512 remain = sg_miter->length;
2516 fcnt = ((fifo_depth -
2517 SDMMC_GET_FCNT(mci_readl(host, STATUS)))
2518 << shift) - host->part_buf_count;
2519 len = min(remain, fcnt);
2522 host->push_data(host, (void *)(buf + offset), len);
2523 data->bytes_xfered += len;
2528 sg_miter->consumed = offset;
2529 status = mci_readl(host, MINTSTS);
2530 mci_writel(host, RINTSTS, SDMMC_INT_TXDR);
2531 } while (status & SDMMC_INT_TXDR); /* if TXDR write again */
2534 if (!sg_miter_next(sg_miter))
2536 sg_miter->consumed = 0;
2538 sg_miter_stop(sg_miter);
2542 sg_miter_stop(sg_miter);
2544 smp_wmb(); /* drain writebuffer */
2545 set_bit(EVENT_XFER_COMPLETE, &host->pending_events);
2548 static void dw_mci_cmd_interrupt(struct dw_mci *host, u32 status)
2550 if (!host->cmd_status)
2551 host->cmd_status = status;
2553 smp_wmb(); /* drain writebuffer */
2555 set_bit(EVENT_CMD_COMPLETE, &host->pending_events);
2556 tasklet_schedule(&host->tasklet);
2559 static void dw_mci_handle_cd(struct dw_mci *host)
2563 for (i = 0; i < host->num_slots; i++) {
2564 struct dw_mci_slot *slot = host->slot[i];
2569 if (slot->mmc->ops->card_event)
2570 slot->mmc->ops->card_event(slot->mmc);
2571 mmc_detect_change(slot->mmc,
2572 msecs_to_jiffies(host->pdata->detect_delay_ms));
2576 static irqreturn_t dw_mci_interrupt(int irq, void *dev_id)
2578 struct dw_mci *host = dev_id;
2582 pending = mci_readl(host, MINTSTS); /* read-only mask reg */
2585 /* Check volt switch first, since it can look like an error */
2586 if ((host->state == STATE_SENDING_CMD11) &&
2587 (pending & SDMMC_INT_VOLT_SWITCH)) {
2588 unsigned long irqflags;
2590 mci_writel(host, RINTSTS, SDMMC_INT_VOLT_SWITCH);
2591 pending &= ~SDMMC_INT_VOLT_SWITCH;
2594 * Hold the lock; we know cmd11_timer can't be kicked
2595 * off after the lock is released, so safe to delete.
2597 spin_lock_irqsave(&host->irq_lock, irqflags);
2598 dw_mci_cmd_interrupt(host, pending);
2599 spin_unlock_irqrestore(&host->irq_lock, irqflags);
2601 del_timer(&host->cmd11_timer);
2604 if (pending & DW_MCI_CMD_ERROR_FLAGS) {
2605 mci_writel(host, RINTSTS, DW_MCI_CMD_ERROR_FLAGS);
2606 host->cmd_status = pending;
2607 smp_wmb(); /* drain writebuffer */
2608 set_bit(EVENT_CMD_COMPLETE, &host->pending_events);
2611 if (pending & DW_MCI_DATA_ERROR_FLAGS) {
2612 /* if there is an error report DATA_ERROR */
2613 mci_writel(host, RINTSTS, DW_MCI_DATA_ERROR_FLAGS);
2614 host->data_status = pending;
2615 smp_wmb(); /* drain writebuffer */
2616 set_bit(EVENT_DATA_ERROR, &host->pending_events);
2617 tasklet_schedule(&host->tasklet);
2620 if (pending & SDMMC_INT_DATA_OVER) {
2621 del_timer(&host->dto_timer);
2623 mci_writel(host, RINTSTS, SDMMC_INT_DATA_OVER);
2624 if (!host->data_status)
2625 host->data_status = pending;
2626 smp_wmb(); /* drain writebuffer */
2627 if (host->dir_status == DW_MCI_RECV_STATUS) {
2628 if (host->sg != NULL)
2629 dw_mci_read_data_pio(host, true);
2631 set_bit(EVENT_DATA_COMPLETE, &host->pending_events);
2632 tasklet_schedule(&host->tasklet);
2635 if (pending & SDMMC_INT_RXDR) {
2636 mci_writel(host, RINTSTS, SDMMC_INT_RXDR);
2637 if (host->dir_status == DW_MCI_RECV_STATUS && host->sg)
2638 dw_mci_read_data_pio(host, false);
2641 if (pending & SDMMC_INT_TXDR) {
2642 mci_writel(host, RINTSTS, SDMMC_INT_TXDR);
2643 if (host->dir_status == DW_MCI_SEND_STATUS && host->sg)
2644 dw_mci_write_data_pio(host);
2647 if (pending & SDMMC_INT_CMD_DONE) {
2648 mci_writel(host, RINTSTS, SDMMC_INT_CMD_DONE);
2649 dw_mci_cmd_interrupt(host, pending);
2652 if (pending & SDMMC_INT_CD) {
2653 mci_writel(host, RINTSTS, SDMMC_INT_CD);
2654 dw_mci_handle_cd(host);
2657 /* Handle SDIO Interrupts */
2658 for (i = 0; i < host->num_slots; i++) {
2659 struct dw_mci_slot *slot = host->slot[i];
2664 if (pending & SDMMC_INT_SDIO(slot->sdio_id)) {
2665 mci_writel(host, RINTSTS,
2666 SDMMC_INT_SDIO(slot->sdio_id));
2667 mmc_signal_sdio_irq(slot->mmc);
2673 if (host->use_dma != TRANS_MODE_IDMAC)
2676 /* Handle IDMA interrupts */
2677 if (host->dma_64bit_address == 1) {
2678 pending = mci_readl(host, IDSTS64);
2679 if (pending & (SDMMC_IDMAC_INT_TI | SDMMC_IDMAC_INT_RI)) {
2680 mci_writel(host, IDSTS64, SDMMC_IDMAC_INT_TI |
2681 SDMMC_IDMAC_INT_RI);
2682 mci_writel(host, IDSTS64, SDMMC_IDMAC_INT_NI);
2683 if (!test_bit(EVENT_DATA_ERROR, &host->pending_events))
2684 host->dma_ops->complete((void *)host);
2687 pending = mci_readl(host, IDSTS);
2688 if (pending & (SDMMC_IDMAC_INT_TI | SDMMC_IDMAC_INT_RI)) {
2689 mci_writel(host, IDSTS, SDMMC_IDMAC_INT_TI |
2690 SDMMC_IDMAC_INT_RI);
2691 mci_writel(host, IDSTS, SDMMC_IDMAC_INT_NI);
2692 if (!test_bit(EVENT_DATA_ERROR, &host->pending_events))
2693 host->dma_ops->complete((void *)host);
2700 static int dw_mci_init_slot(struct dw_mci *host, unsigned int id)
2702 struct mmc_host *mmc;
2703 struct dw_mci_slot *slot;
2704 const struct dw_mci_drv_data *drv_data = host->drv_data;
2708 mmc = mmc_alloc_host(sizeof(struct dw_mci_slot), host->dev);
2712 slot = mmc_priv(mmc);
2714 slot->sdio_id = host->sdio_id0 + id;
2717 host->slot[id] = slot;
2719 mmc->ops = &dw_mci_ops;
2720 if (of_property_read_u32_array(host->dev->of_node,
2721 "clock-freq-min-max", freq, 2)) {
2722 mmc->f_min = DW_MCI_FREQ_MIN;
2723 mmc->f_max = DW_MCI_FREQ_MAX;
2726 "'clock-freq-min-max' property was deprecated.\n");
2727 mmc->f_min = freq[0];
2728 mmc->f_max = freq[1];
2731 /*if there are external regulators, get them*/
2732 ret = mmc_regulator_get_supply(mmc);
2733 if (ret == -EPROBE_DEFER)
2734 goto err_host_allocated;
2736 if (!mmc->ocr_avail)
2737 mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
2739 if (host->pdata->caps)
2740 mmc->caps = host->pdata->caps;
2743 * Support MMC_CAP_ERASE by default.
2744 * It needs to use trim/discard/erase commands.
2746 mmc->caps |= MMC_CAP_ERASE;
2748 if (host->pdata->pm_caps)
2749 mmc->pm_caps = host->pdata->pm_caps;
2751 if (host->dev->of_node) {
2752 ctrl_id = of_alias_get_id(host->dev->of_node, "mshc");
2756 ctrl_id = to_platform_device(host->dev)->id;
2758 if (drv_data && drv_data->caps)
2759 mmc->caps |= drv_data->caps[ctrl_id];
2761 if (host->pdata->caps2)
2762 mmc->caps2 = host->pdata->caps2;
2764 ret = mmc_of_parse(mmc);
2766 goto err_host_allocated;
2768 /* Useful defaults if platform data is unset. */
2769 if (host->use_dma == TRANS_MODE_IDMAC) {
2770 mmc->max_segs = host->ring_size;
2771 mmc->max_blk_size = 65535;
2772 mmc->max_seg_size = 0x1000;
2773 mmc->max_req_size = mmc->max_seg_size * host->ring_size;
2774 mmc->max_blk_count = mmc->max_req_size / 512;
2775 } else if (host->use_dma == TRANS_MODE_EDMAC) {
2777 mmc->max_blk_size = 65535;
2778 mmc->max_blk_count = 65535;
2780 mmc->max_blk_size * mmc->max_blk_count;
2781 mmc->max_seg_size = mmc->max_req_size;
2783 /* TRANS_MODE_PIO */
2785 mmc->max_blk_size = 65535; /* BLKSIZ is 16 bits */
2786 mmc->max_blk_count = 512;
2787 mmc->max_req_size = mmc->max_blk_size *
2789 mmc->max_seg_size = mmc->max_req_size;
2794 ret = mmc_add_host(mmc);
2796 goto err_host_allocated;
2798 #if defined(CONFIG_DEBUG_FS)
2799 dw_mci_init_debugfs(slot);
2809 static void dw_mci_cleanup_slot(struct dw_mci_slot *slot, unsigned int id)
2811 /* Debugfs stuff is cleaned up by mmc core */
2812 mmc_remove_host(slot->mmc);
2813 slot->host->slot[id] = NULL;
2814 mmc_free_host(slot->mmc);
2817 static void dw_mci_init_dma(struct dw_mci *host)
2820 struct device *dev = host->dev;
2821 struct device_node *np = dev->of_node;
2824 * Check tansfer mode from HCON[17:16]
2825 * Clear the ambiguous description of dw_mmc databook:
2826 * 2b'00: No DMA Interface -> Actually means using Internal DMA block
2827 * 2b'01: DesignWare DMA Interface -> Synopsys DW-DMA block
2828 * 2b'10: Generic DMA Interface -> non-Synopsys generic DMA block
2829 * 2b'11: Non DW DMA Interface -> pio only
2830 * Compared to DesignWare DMA Interface, Generic DMA Interface has a
2831 * simpler request/acknowledge handshake mechanism and both of them
2832 * are regarded as external dma master for dw_mmc.
2834 host->use_dma = SDMMC_GET_TRANS_MODE(mci_readl(host, HCON));
2835 if (host->use_dma == DMA_INTERFACE_IDMA) {
2836 host->use_dma = TRANS_MODE_IDMAC;
2837 } else if (host->use_dma == DMA_INTERFACE_DWDMA ||
2838 host->use_dma == DMA_INTERFACE_GDMA) {
2839 host->use_dma = TRANS_MODE_EDMAC;
2844 /* Determine which DMA interface to use */
2845 if (host->use_dma == TRANS_MODE_IDMAC) {
2847 * Check ADDR_CONFIG bit in HCON to find
2848 * IDMAC address bus width
2850 addr_config = SDMMC_GET_ADDR_CONFIG(mci_readl(host, HCON));
2852 if (addr_config == 1) {
2853 /* host supports IDMAC in 64-bit address mode */
2854 host->dma_64bit_address = 1;
2856 "IDMAC supports 64-bit address mode.\n");
2857 if (!dma_set_mask(host->dev, DMA_BIT_MASK(64)))
2858 dma_set_coherent_mask(host->dev,
2861 /* host supports IDMAC in 32-bit address mode */
2862 host->dma_64bit_address = 0;
2864 "IDMAC supports 32-bit address mode.\n");
2867 /* Alloc memory for sg translation */
2868 host->sg_cpu = dmam_alloc_coherent(host->dev,
2870 &host->sg_dma, GFP_KERNEL);
2871 if (!host->sg_cpu) {
2873 "%s: could not alloc DMA memory\n",
2878 host->dma_ops = &dw_mci_idmac_ops;
2879 dev_info(host->dev, "Using internal DMA controller.\n");
2881 /* TRANS_MODE_EDMAC: check dma bindings again */
2882 if ((of_property_count_strings(np, "dma-names") < 0) ||
2883 (!of_find_property(np, "dmas", NULL))) {
2886 host->dma_ops = &dw_mci_edmac_ops;
2887 dev_info(host->dev, "Using external DMA controller.\n");
2890 if (host->dma_ops->init && host->dma_ops->start &&
2891 host->dma_ops->stop && host->dma_ops->cleanup) {
2892 if (host->dma_ops->init(host)) {
2893 dev_err(host->dev, "%s: Unable to initialize DMA Controller.\n",
2898 dev_err(host->dev, "DMA initialization not found.\n");
2905 dev_info(host->dev, "Using PIO mode.\n");
2906 host->use_dma = TRANS_MODE_PIO;
2909 static void dw_mci_cmd11_timer(unsigned long arg)
2911 struct dw_mci *host = (struct dw_mci *)arg;
2913 if (host->state != STATE_SENDING_CMD11) {
2914 dev_warn(host->dev, "Unexpected CMD11 timeout\n");
2918 host->cmd_status = SDMMC_INT_RTO;
2919 set_bit(EVENT_CMD_COMPLETE, &host->pending_events);
2920 tasklet_schedule(&host->tasklet);
2923 static void dw_mci_dto_timer(unsigned long arg)
2925 struct dw_mci *host = (struct dw_mci *)arg;
2927 switch (host->state) {
2928 case STATE_SENDING_DATA:
2929 case STATE_DATA_BUSY:
2931 * If DTO interrupt does NOT come in sending data state,
2932 * we should notify the driver to terminate current transfer
2933 * and report a data timeout to the core.
2935 host->data_status = SDMMC_INT_DRTO;
2936 set_bit(EVENT_DATA_ERROR, &host->pending_events);
2937 set_bit(EVENT_DATA_COMPLETE, &host->pending_events);
2938 tasklet_schedule(&host->tasklet);
2946 static struct dw_mci_board *dw_mci_parse_dt(struct dw_mci *host)
2948 struct dw_mci_board *pdata;
2949 struct device *dev = host->dev;
2950 struct device_node *np = dev->of_node;
2951 const struct dw_mci_drv_data *drv_data = host->drv_data;
2953 u32 clock_frequency;
2955 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
2957 return ERR_PTR(-ENOMEM);
2959 /* find reset controller when exist */
2960 pdata->rstc = devm_reset_control_get_optional(dev, "reset");
2961 if (IS_ERR(pdata->rstc)) {
2962 if (PTR_ERR(pdata->rstc) == -EPROBE_DEFER)
2963 return ERR_PTR(-EPROBE_DEFER);
2966 /* find out number of slots supported */
2967 of_property_read_u32(np, "num-slots", &pdata->num_slots);
2969 if (of_property_read_u32(np, "fifo-depth", &pdata->fifo_depth))
2971 "fifo-depth property not found, using value of FIFOTH register as default\n");
2973 of_property_read_u32(np, "card-detect-delay", &pdata->detect_delay_ms);
2975 of_property_read_u32(np, "data-addr", &host->data_addr_override);
2977 if (of_get_property(np, "fifo-watermark-aligned", NULL))
2978 host->wm_aligned = true;
2980 if (!of_property_read_u32(np, "clock-frequency", &clock_frequency))
2981 pdata->bus_hz = clock_frequency;
2983 if (drv_data && drv_data->parse_dt) {
2984 ret = drv_data->parse_dt(host);
2986 return ERR_PTR(ret);
2992 #else /* CONFIG_OF */
2993 static struct dw_mci_board *dw_mci_parse_dt(struct dw_mci *host)
2995 return ERR_PTR(-EINVAL);
2997 #endif /* CONFIG_OF */
2999 static void dw_mci_enable_cd(struct dw_mci *host)
3001 unsigned long irqflags;
3004 struct dw_mci_slot *slot;
3007 * No need for CD if all slots have a non-error GPIO
3008 * as well as broken card detection is found.
3010 for (i = 0; i < host->num_slots; i++) {
3011 slot = host->slot[i];
3012 if (slot->mmc->caps & MMC_CAP_NEEDS_POLL)
3015 if (mmc_gpio_get_cd(slot->mmc) < 0)
3018 if (i == host->num_slots)
3021 spin_lock_irqsave(&host->irq_lock, irqflags);
3022 temp = mci_readl(host, INTMASK);
3023 temp |= SDMMC_INT_CD;
3024 mci_writel(host, INTMASK, temp);
3025 spin_unlock_irqrestore(&host->irq_lock, irqflags);
3028 int dw_mci_probe(struct dw_mci *host)
3030 const struct dw_mci_drv_data *drv_data = host->drv_data;
3031 int width, i, ret = 0;
3036 host->pdata = dw_mci_parse_dt(host);
3037 if (PTR_ERR(host->pdata) == -EPROBE_DEFER) {
3038 return -EPROBE_DEFER;
3039 } else if (IS_ERR(host->pdata)) {
3040 dev_err(host->dev, "platform data not available\n");
3045 host->biu_clk = devm_clk_get(host->dev, "biu");
3046 if (IS_ERR(host->biu_clk)) {
3047 dev_dbg(host->dev, "biu clock not available\n");
3049 ret = clk_prepare_enable(host->biu_clk);
3051 dev_err(host->dev, "failed to enable biu clock\n");
3056 host->ciu_clk = devm_clk_get(host->dev, "ciu");
3057 if (IS_ERR(host->ciu_clk)) {
3058 dev_dbg(host->dev, "ciu clock not available\n");
3059 host->bus_hz = host->pdata->bus_hz;
3061 ret = clk_prepare_enable(host->ciu_clk);
3063 dev_err(host->dev, "failed to enable ciu clock\n");
3067 if (host->pdata->bus_hz) {
3068 ret = clk_set_rate(host->ciu_clk, host->pdata->bus_hz);
3071 "Unable to set bus rate to %uHz\n",
3072 host->pdata->bus_hz);
3074 host->bus_hz = clk_get_rate(host->ciu_clk);
3077 if (!host->bus_hz) {
3079 "Platform data must supply bus speed\n");
3084 if (drv_data && drv_data->init) {
3085 ret = drv_data->init(host);
3088 "implementation specific init failed\n");
3093 if (!IS_ERR(host->pdata->rstc)) {
3094 reset_control_assert(host->pdata->rstc);
3095 usleep_range(10, 50);
3096 reset_control_deassert(host->pdata->rstc);
3099 setup_timer(&host->cmd11_timer,
3100 dw_mci_cmd11_timer, (unsigned long)host);
3102 setup_timer(&host->dto_timer,
3103 dw_mci_dto_timer, (unsigned long)host);
3105 spin_lock_init(&host->lock);
3106 spin_lock_init(&host->irq_lock);
3107 INIT_LIST_HEAD(&host->queue);
3110 * Get the host data width - this assumes that HCON has been set with
3111 * the correct values.
3113 i = SDMMC_GET_HDATA_WIDTH(mci_readl(host, HCON));
3115 host->push_data = dw_mci_push_data16;
3116 host->pull_data = dw_mci_pull_data16;
3118 host->data_shift = 1;
3119 } else if (i == 2) {
3120 host->push_data = dw_mci_push_data64;
3121 host->pull_data = dw_mci_pull_data64;
3123 host->data_shift = 3;
3125 /* Check for a reserved value, and warn if it is */
3127 "HCON reports a reserved host data width!\n"
3128 "Defaulting to 32-bit access.\n");
3129 host->push_data = dw_mci_push_data32;
3130 host->pull_data = dw_mci_pull_data32;
3132 host->data_shift = 2;
3135 /* Reset all blocks */
3136 if (!dw_mci_ctrl_reset(host, SDMMC_CTRL_ALL_RESET_FLAGS)) {
3141 host->dma_ops = host->pdata->dma_ops;
3142 dw_mci_init_dma(host);
3144 /* Clear the interrupts for the host controller */
3145 mci_writel(host, RINTSTS, 0xFFFFFFFF);
3146 mci_writel(host, INTMASK, 0); /* disable all mmc interrupt first */
3148 /* Put in max timeout */
3149 mci_writel(host, TMOUT, 0xFFFFFFFF);
3152 * FIFO threshold settings RxMark = fifo_size / 2 - 1,
3153 * Tx Mark = fifo_size / 2 DMA Size = 8
3155 if (!host->pdata->fifo_depth) {
3157 * Power-on value of RX_WMark is FIFO_DEPTH-1, but this may
3158 * have been overwritten by the bootloader, just like we're
3159 * about to do, so if you know the value for your hardware, you
3160 * should put it in the platform data.
3162 fifo_size = mci_readl(host, FIFOTH);
3163 fifo_size = 1 + ((fifo_size >> 16) & 0xfff);
3165 fifo_size = host->pdata->fifo_depth;
3167 host->fifo_depth = fifo_size;
3169 SDMMC_SET_FIFOTH(0x2, fifo_size / 2 - 1, fifo_size / 2);
3170 mci_writel(host, FIFOTH, host->fifoth_val);
3172 /* disable clock to CIU */
3173 mci_writel(host, CLKENA, 0);
3174 mci_writel(host, CLKSRC, 0);
3177 * In 2.40a spec, Data offset is changed.
3178 * Need to check the version-id and set data-offset for DATA register.
3180 host->verid = SDMMC_GET_VERID(mci_readl(host, VERID));
3181 dev_info(host->dev, "Version ID is %04x\n", host->verid);
3183 if (host->data_addr_override)
3184 host->fifo_reg = host->regs + host->data_addr_override;
3185 else if (host->verid < DW_MMC_240A)
3186 host->fifo_reg = host->regs + DATA_OFFSET;
3188 host->fifo_reg = host->regs + DATA_240A_OFFSET;
3190 tasklet_init(&host->tasklet, dw_mci_tasklet_func, (unsigned long)host);
3191 ret = devm_request_irq(host->dev, host->irq, dw_mci_interrupt,
3192 host->irq_flags, "dw-mci", host);
3196 if (host->pdata->num_slots)
3197 host->num_slots = host->pdata->num_slots;
3199 host->num_slots = 1;
3201 if (host->num_slots < 1 ||
3202 host->num_slots > SDMMC_GET_SLOT_NUM(mci_readl(host, HCON))) {
3204 "Platform data must supply correct num_slots.\n");
3210 * Enable interrupts for command done, data over, data empty,
3211 * receive ready and error such as transmit, receive timeout, crc error
3213 mci_writel(host, INTMASK, SDMMC_INT_CMD_DONE | SDMMC_INT_DATA_OVER |
3214 SDMMC_INT_TXDR | SDMMC_INT_RXDR |
3215 DW_MCI_ERROR_FLAGS);
3216 /* Enable mci interrupt */
3217 mci_writel(host, CTRL, SDMMC_CTRL_INT_ENABLE);
3220 "DW MMC controller at irq %d,%d bit host data width,%u deep fifo\n",
3221 host->irq, width, fifo_size);
3223 /* We need at least one slot to succeed */
3224 for (i = 0; i < host->num_slots; i++) {
3225 ret = dw_mci_init_slot(host, i);
3227 dev_dbg(host->dev, "slot %d init failed\n", i);
3233 dev_info(host->dev, "%d slots initialized\n", init_slots);
3236 "attempted to initialize %d slots, but failed on all\n",
3241 /* Now that slots are all setup, we can enable card detect */
3242 dw_mci_enable_cd(host);
3247 if (host->use_dma && host->dma_ops->exit)
3248 host->dma_ops->exit(host);
3250 if (!IS_ERR(host->pdata->rstc))
3251 reset_control_assert(host->pdata->rstc);
3254 clk_disable_unprepare(host->ciu_clk);
3257 clk_disable_unprepare(host->biu_clk);
3261 EXPORT_SYMBOL(dw_mci_probe);
3263 void dw_mci_remove(struct dw_mci *host)
3267 for (i = 0; i < host->num_slots; i++) {
3268 dev_dbg(host->dev, "remove slot %d\n", i);
3270 dw_mci_cleanup_slot(host->slot[i], i);
3273 mci_writel(host, RINTSTS, 0xFFFFFFFF);
3274 mci_writel(host, INTMASK, 0); /* disable all mmc interrupt first */
3276 /* disable clock to CIU */
3277 mci_writel(host, CLKENA, 0);
3278 mci_writel(host, CLKSRC, 0);
3280 if (host->use_dma && host->dma_ops->exit)
3281 host->dma_ops->exit(host);
3283 if (!IS_ERR(host->pdata->rstc))
3284 reset_control_assert(host->pdata->rstc);
3286 clk_disable_unprepare(host->ciu_clk);
3287 clk_disable_unprepare(host->biu_clk);
3289 EXPORT_SYMBOL(dw_mci_remove);
3294 int dw_mci_runtime_suspend(struct device *dev)
3296 struct dw_mci *host = dev_get_drvdata(dev);
3298 if (host->use_dma && host->dma_ops->exit)
3299 host->dma_ops->exit(host);
3301 clk_disable_unprepare(host->ciu_clk);
3303 if (host->cur_slot &&
3304 (mmc_can_gpio_cd(host->cur_slot->mmc) ||
3305 !mmc_card_is_removable(host->cur_slot->mmc)))
3306 clk_disable_unprepare(host->biu_clk);
3310 EXPORT_SYMBOL(dw_mci_runtime_suspend);
3312 int dw_mci_runtime_resume(struct device *dev)
3315 struct dw_mci *host = dev_get_drvdata(dev);
3317 if (host->cur_slot &&
3318 (mmc_can_gpio_cd(host->cur_slot->mmc) ||
3319 !mmc_card_is_removable(host->cur_slot->mmc))) {
3320 ret = clk_prepare_enable(host->biu_clk);
3325 ret = clk_prepare_enable(host->ciu_clk);
3329 if (!dw_mci_ctrl_reset(host, SDMMC_CTRL_ALL_RESET_FLAGS)) {
3330 clk_disable_unprepare(host->ciu_clk);
3335 if (host->use_dma && host->dma_ops->init)
3336 host->dma_ops->init(host);
3339 * Restore the initial value at FIFOTH register
3340 * And Invalidate the prev_blksz with zero
3342 mci_writel(host, FIFOTH, host->fifoth_val);
3343 host->prev_blksz = 0;
3345 /* Put in max timeout */
3346 mci_writel(host, TMOUT, 0xFFFFFFFF);
3348 mci_writel(host, RINTSTS, 0xFFFFFFFF);
3349 mci_writel(host, INTMASK, SDMMC_INT_CMD_DONE | SDMMC_INT_DATA_OVER |
3350 SDMMC_INT_TXDR | SDMMC_INT_RXDR |
3351 DW_MCI_ERROR_FLAGS);
3352 mci_writel(host, CTRL, SDMMC_CTRL_INT_ENABLE);
3354 for (i = 0; i < host->num_slots; i++) {
3355 struct dw_mci_slot *slot = host->slot[i];
3359 if (slot->mmc->pm_flags & MMC_PM_KEEP_POWER)
3360 dw_mci_set_ios(slot->mmc, &slot->mmc->ios);
3362 /* Force setup bus to guarantee available clock output */
3363 dw_mci_setup_bus(slot, true);
3366 /* Now that slots are all setup, we can enable card detect */
3367 dw_mci_enable_cd(host);
3372 if (host->cur_slot &&
3373 (mmc_can_gpio_cd(host->cur_slot->mmc) ||
3374 !mmc_card_is_removable(host->cur_slot->mmc)))
3375 clk_disable_unprepare(host->biu_clk);
3379 EXPORT_SYMBOL(dw_mci_runtime_resume);
3380 #endif /* CONFIG_PM */
3382 static int __init dw_mci_init(void)
3384 pr_info("Synopsys Designware Multimedia Card Interface Driver\n");
3388 static void __exit dw_mci_exit(void)
3392 module_init(dw_mci_init);
3393 module_exit(dw_mci_exit);
3395 MODULE_DESCRIPTION("DW Multimedia Card Interface driver");
3396 MODULE_AUTHOR("NXP Semiconductor VietNam");
3397 MODULE_AUTHOR("Imagination Technologies Ltd");
3398 MODULE_LICENSE("GPL v2");