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 * Copyright (C) 2014 - 2015 Fuzhou Rockchip Electronics Co.Ltd.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
16 #include <linux/blkdev.h>
17 #include <linux/clk.h>
18 #include <linux/debugfs.h>
19 #include <linux/device.h>
20 #include <linux/dma-mapping.h>
21 #include <linux/dmaengine.h>
22 #include <linux/err.h>
23 #include <linux/init.h>
24 #include <linux/interrupt.h>
25 #include <linux/ioport.h>
26 #include <linux/module.h>
27 #include <linux/platform_device.h>
28 #include <linux/seq_file.h>
29 #include <linux/slab.h>
30 #include <linux/stat.h>
31 #include <linux/delay.h>
32 #include <linux/irq.h>
33 #include <linux/suspend.h>
34 #include <linux/mmc/host.h>
35 #include <linux/mmc/mmc.h>
36 #include <linux/mmc/sd.h>
37 #include <linux/mmc/card.h>
38 #include <linux/mmc/sdio.h>
39 #include <linux/mmc/rk_mmc.h>
40 #include <linux/bitops.h>
41 #include <linux/regulator/consumer.h>
42 #include <linux/workqueue.h>
44 #include <linux/of_gpio.h>
45 #include <linux/mmc/slot-gpio.h>
46 #include <linux/clk-private.h>
47 #include <linux/rockchip/cpu.h>
48 #include <linux/rfkill-wlan.h>
49 #include <linux/mfd/syscon.h>
50 #include <linux/regmap.h>
51 #include <linux/log2.h>
53 #include "rk_sdmmc_dbg.h"
54 #include <linux/regulator/rockchip_io_vol_domain.h>
55 #include "../../clk/rockchip/clk-ops.h"
57 #define RK_SDMMC_DRIVER_VERSION "Ver 2.00 2015-06-10"
59 /* Common flag combinations */
60 #define DW_MCI_DATA_ERROR_FLAGS (SDMMC_INT_DRTO | SDMMC_INT_DCRC | \
63 #define DW_MCI_CMD_ERROR_FLAGS (SDMMC_INT_RTO | SDMMC_INT_RCRC | \
65 #define DW_MCI_ERROR_FLAGS (DW_MCI_DATA_ERROR_FLAGS | \
66 DW_MCI_CMD_ERROR_FLAGS | SDMMC_INT_HLE)
67 #define DW_MCI_SEND_STATUS 1
68 #define DW_MCI_RECV_STATUS 2
69 #define DW_MCI_DMA_THRESHOLD 16
70 #define DW_MCI_FREQ_MAX 50000000 /* unit: HZ */
71 #define DW_MCI_FREQ_MIN 300000 /* unit: HZ */
73 /* Max is 250ms showed in Spec */
74 #define SDMMC_DATA_TIMEOUT_SD 500
75 #define SDMMC_DATA_TIMEOUT_SDIO 250
76 #define SDMMC_DATA_TIMEOUT_EMMC 2500
77 #define SDMMC_CMD_RTO_MAX_HOLD 200
78 #define SDMMC_WAIT_FOR_UNBUSY 2500
80 #define DW_REGS_SIZE (0x0098 + 4)
81 #define DW_REGS_NUM (0x0098 / 4)
83 #ifdef CONFIG_MMC_DW_IDMAC
84 #define IDMAC_INT_CLR (SDMMC_IDMAC_INT_AI | SDMMC_IDMAC_INT_NI | \
85 SDMMC_IDMAC_INT_CES | SDMMC_IDMAC_INT_DU | \
86 SDMMC_IDMAC_INT_FBE | SDMMC_IDMAC_INT_RI | \
90 u32 des0; /* Control Descriptor */
91 #define IDMAC_DES0_DIC BIT(1)
92 #define IDMAC_DES0_LD BIT(2)
93 #define IDMAC_DES0_FD BIT(3)
94 #define IDMAC_DES0_CH BIT(4)
95 #define IDMAC_DES0_ER BIT(5)
96 #define IDMAC_DES0_CES BIT(30)
97 #define IDMAC_DES0_OWN BIT(31)
99 u32 des1; /* Buffer sizes */
100 #define IDMAC_SET_BUFFER1_SIZE(d, s) \
101 ((d)->des1 = ((d)->des1 & 0x03ffe000) | ((s) & 0x1fff))
103 u32 des2; /* buffer 1 physical address */
105 u32 des3; /* buffer 2 physical address */
107 #endif /* CONFIG_MMC_DW_IDMAC */
110 static inline bool dw_mci_fifo_reset(struct dw_mci *host);
111 static inline bool dw_mci_ctrl_all_reset(struct dw_mci *host);
112 static bool dw_mci_ctrl_reset(struct dw_mci *host, u32 reset);
113 static void dw_mci_disable_low_power(struct dw_mci_slot *slot);
114 extern void rk_send_wakeup_key(void);
116 #if defined(CONFIG_DEBUG_FS)
117 static int dw_mci_req_show(struct seq_file *s, void *v)
119 struct dw_mci_slot *slot = s->private;
120 struct mmc_request *mrq;
121 struct mmc_command *cmd;
122 struct mmc_command *stop;
123 struct mmc_data *data;
125 /* Make sure we get a consistent snapshot */
126 spin_lock_bh(&slot->host->lock);
136 "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n",
137 cmd->opcode, cmd->arg, cmd->flags,
138 cmd->resp[0], cmd->resp[1], cmd->resp[2],
139 cmd->resp[2], cmd->error);
141 seq_printf(s, "DATA %u / %u * %u flg %x err %d\n",
142 data->bytes_xfered, data->blocks,
143 data->blksz, data->flags, data->error);
146 "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n",
147 stop->opcode, stop->arg, stop->flags,
148 stop->resp[0], stop->resp[1], stop->resp[2],
149 stop->resp[2], stop->error);
152 spin_unlock_bh(&slot->host->lock);
157 static int dw_mci_req_open(struct inode *inode, struct file *file)
159 return single_open(file, dw_mci_req_show, inode->i_private);
162 static const struct file_operations dw_mci_req_fops = {
163 .owner = THIS_MODULE,
164 .open = dw_mci_req_open,
167 .release = single_release,
170 static int dw_mci_regs_show(struct seq_file *s, void *v)
172 seq_printf(s, "STATUS:\t0x%08x\n", SDMMC_STATUS);
173 seq_printf(s, "RINTSTS:\t0x%08x\n", SDMMC_RINTSTS);
174 seq_printf(s, "CMD:\t0x%08x\n", SDMMC_CMD);
175 seq_printf(s, "CTRL:\t0x%08x\n", SDMMC_CTRL);
176 seq_printf(s, "INTMASK:\t0x%08x\n", SDMMC_INTMASK);
177 seq_printf(s, "CLKENA:\t0x%08x\n", SDMMC_CLKENA);
182 static int dw_mci_regs_open(struct inode *inode, struct file *file)
184 return single_open(file, dw_mci_regs_show, inode->i_private);
187 static const struct file_operations dw_mci_regs_fops = {
188 .owner = THIS_MODULE,
189 .open = dw_mci_regs_open,
192 .release = single_release,
195 static void dw_mci_init_debugfs(struct dw_mci_slot *slot)
197 struct mmc_host *mmc = slot->mmc;
198 struct dw_mci *host = slot->host;
202 root = mmc->debugfs_root;
206 node = debugfs_create_file("regs", S_IRUSR, root, host,
211 node = debugfs_create_file("req", S_IRUSR, root, slot,
216 node = debugfs_create_u32("state", S_IRUSR, root, (u32 *)&host->state);
220 node = debugfs_create_x32("pending_events", S_IRUSR, root,
221 (u32 *)&host->pending_events);
225 node = debugfs_create_x32("completed_events", S_IRUSR, root,
226 (u32 *)&host->completed_events);
233 dev_err(&mmc->class_dev, "failed to initialize debugfs for slot\n");
235 #endif /* defined(CONFIG_DEBUG_FS) */
237 static void dw_mci_set_timeout(struct dw_mci *host)
239 /* timeout (maximum) */
240 mci_writel(host, TMOUT, 0xffffffff);
243 static u32 dw_mci_prepare_command(struct mmc_host *mmc, struct mmc_command *cmd)
245 struct mmc_data *data;
246 struct dw_mci_slot *slot = mmc_priv(mmc);
247 const struct dw_mci_drv_data *drv_data = slot->host->drv_data;
249 cmd->error = -EINPROGRESS;
253 if (cmdr == MMC_STOP_TRANSMISSION)
254 cmdr |= SDMMC_CMD_STOP;
256 cmdr |= SDMMC_CMD_PRV_DAT_WAIT;
258 if (cmd->flags & MMC_RSP_PRESENT) {
259 /* We expect a response, so set this bit */
260 cmdr |= SDMMC_CMD_RESP_EXP;
261 if (cmd->flags & MMC_RSP_136)
262 cmdr |= SDMMC_CMD_RESP_LONG;
265 if (cmd->flags & MMC_RSP_CRC)
266 cmdr |= SDMMC_CMD_RESP_CRC;
270 cmdr |= SDMMC_CMD_DAT_EXP;
271 if (data->flags & MMC_DATA_STREAM)
272 cmdr |= SDMMC_CMD_STRM_MODE;
273 if (data->flags & MMC_DATA_WRITE)
274 cmdr |= SDMMC_CMD_DAT_WR;
277 if (drv_data && drv_data->prepare_command)
278 drv_data->prepare_command(slot->host, &cmdr);
283 static void dw_mci_start_command(struct dw_mci *host,
284 struct mmc_command *cmd, u32 cmd_flags)
286 struct dw_mci_slot *slot = host->slot[0];
288 host->pre_cmd = host->cmd;
291 "start command: ARGR=0x%08x CMDR=0x%08x\n",
292 cmd->arg, cmd_flags);
294 if(SD_SWITCH_VOLTAGE == cmd->opcode){
295 /*confirm non-low-power mode*/
296 mci_writel(host, CMDARG, 0);
297 dw_mci_disable_low_power(slot);
299 MMC_DBG_INFO_FUNC(host->mmc,"Line%d..%s before start cmd=11,[%s]",
300 __LINE__, __FUNCTION__,mmc_hostname(host->mmc));
302 cmd_flags |= SDMMC_CMD_VOLT_SWITCH;
305 mci_writel(host, CMDARG, cmd->arg);
308 /* Fix the value to 1 in current soc */
309 if(host->mmc->hold_reg_flag)
310 cmd_flags |= SDMMC_CMD_USE_HOLD_REG;
312 mci_writel(host, CMD, cmd_flags | SDMMC_CMD_START);
316 static void send_stop_cmd(struct dw_mci *host, struct mmc_data *data)
318 dw_mci_start_command(host, data->stop, host->stop_cmdr);
321 /* DMA interface functions */
322 static void dw_mci_stop_dma(struct dw_mci *host)
324 if (host->using_dma) {
325 /* Fixme: No need to terminate edma, may cause flush op */
326 if(!(cpu_is_rk3036() || cpu_is_rk312x()))
327 host->dma_ops->stop(host);
328 host->dma_ops->cleanup(host);
331 /* Data transfer was stopped by the interrupt handler */
332 set_bit(EVENT_XFER_COMPLETE, &host->pending_events);
335 static int dw_mci_get_dma_dir(struct mmc_data *data)
337 if (data->flags & MMC_DATA_WRITE)
338 return DMA_TO_DEVICE;
340 return DMA_FROM_DEVICE;
343 #ifdef CONFIG_MMC_DW_IDMAC
344 static void dw_mci_dma_cleanup(struct dw_mci *host)
346 struct mmc_data *data = host->data;
349 if (!data->host_cookie)
350 dma_unmap_sg(host->dev,
353 dw_mci_get_dma_dir(data));
356 static void dw_mci_idmac_reset(struct dw_mci *host)
358 u32 bmod = mci_readl(host, BMOD);
359 /* Software reset of DMA */
360 bmod |= SDMMC_IDMAC_SWRESET;
361 mci_writel(host, BMOD, bmod);
364 static void dw_mci_idmac_stop_dma(struct dw_mci *host)
368 /* Disable and reset the IDMAC interface */
369 temp = mci_readl(host, CTRL);
370 temp &= ~SDMMC_CTRL_USE_IDMAC;
371 temp |= SDMMC_CTRL_DMA_RESET;
372 mci_writel(host, CTRL, temp);
374 /* Stop the IDMAC running */
375 temp = mci_readl(host, BMOD);
376 temp &= ~(SDMMC_IDMAC_ENABLE | SDMMC_IDMAC_FB);
377 temp |= SDMMC_IDMAC_SWRESET;
378 mci_writel(host, BMOD, temp);
381 static void dw_mci_idmac_complete_dma(void *arg)
383 struct dw_mci *host = arg;
384 struct mmc_data *data = host->data;
386 dev_vdbg(host->dev, "DMA complete\n");
388 host->dma_ops->cleanup(host);
391 * If the card was removed, data will be NULL. No point in trying to
392 * send the stop command or waiting for NBUSY in this case.
395 set_bit(EVENT_XFER_COMPLETE, &host->pending_events);
396 tasklet_schedule(&host->tasklet);
400 static void dw_mci_translate_sglist(struct dw_mci *host, struct mmc_data *data,
404 struct idmac_desc *desc = host->sg_cpu;
406 for (i = 0; i < sg_len; i++, desc++) {
407 unsigned int length = sg_dma_len(&data->sg[i]);
408 u32 mem_addr = sg_dma_address(&data->sg[i]);
410 /* Set the OWN bit and disable interrupts for this descriptor */
411 desc->des0 = IDMAC_DES0_OWN | IDMAC_DES0_DIC | IDMAC_DES0_CH;
414 IDMAC_SET_BUFFER1_SIZE(desc, length);
416 /* Physical address to DMA to/from */
417 desc->des2 = mem_addr;
420 /* Set first descriptor */
422 desc->des0 |= IDMAC_DES0_FD;
424 /* Set last descriptor */
425 desc = host->sg_cpu + (i - 1) * sizeof(struct idmac_desc);
426 desc->des0 &= ~(IDMAC_DES0_CH | IDMAC_DES0_DIC);
427 desc->des0 |= IDMAC_DES0_LD;
432 static void dw_mci_idmac_start_dma(struct dw_mci *host, unsigned int sg_len)
436 dw_mci_translate_sglist(host, host->data, sg_len);
438 /* Select IDMAC interface */
439 temp = mci_readl(host, CTRL);
440 temp |= SDMMC_CTRL_USE_IDMAC;
441 mci_writel(host, CTRL, temp);
445 /* Enable the IDMAC */
446 temp = mci_readl(host, BMOD);
447 temp |= SDMMC_IDMAC_ENABLE | SDMMC_IDMAC_FB;
448 mci_writel(host, BMOD, temp);
450 /* Start it running */
451 mci_writel(host, PLDMND, 1);
454 static int dw_mci_idmac_init(struct dw_mci *host)
456 struct idmac_desc *p;
459 /* Number of descriptors in the ring buffer */
460 host->ring_size = PAGE_SIZE / sizeof(struct idmac_desc);
462 /* Forward link the descriptor list */
463 for (i = 0, p = host->sg_cpu; i < host->ring_size - 1; i++, p++) {
464 p->des3 = host->sg_dma + (sizeof(struct idmac_desc) * (i + 1));
467 /* Set the last descriptor as the end-of-ring descriptor */
468 p->des3 = host->sg_dma;
469 p->des0 = IDMAC_DES0_ER;
471 dw_mci_idmac_reset(host);
473 /* Mask out interrupts - get Tx & Rx complete only */
474 mci_writel(host, IDSTS, IDMAC_INT_CLR);
475 mci_writel(host, IDINTEN, SDMMC_IDMAC_INT_NI | SDMMC_IDMAC_INT_RI |
478 /* Set the descriptor base address */
479 mci_writel(host, DBADDR, host->sg_dma);
483 static const struct dw_mci_dma_ops dw_mci_idmac_ops = {
484 .init = dw_mci_idmac_init,
485 .start = dw_mci_idmac_start_dma,
486 .stop = dw_mci_idmac_stop_dma,
487 .complete = dw_mci_idmac_complete_dma,
488 .cleanup = dw_mci_dma_cleanup,
492 static void dw_mci_edma_cleanup(struct dw_mci *host)
494 struct mmc_data *data = host->data;
497 if (!data->host_cookie)
498 dma_unmap_sg(host->dev,
499 data->sg, data->sg_len,
500 dw_mci_get_dma_dir(data));
503 static void dw_mci_edmac_stop_dma(struct dw_mci *host)
505 dmaengine_terminate_all(host->dms->ch);
508 static void dw_mci_edmac_complete_dma(void *arg)
510 struct dw_mci *host = arg;
511 struct mmc_data *data = host->data;
513 dev_vdbg(host->dev, "DMA complete\n");
516 if(data->flags & MMC_DATA_READ)
517 /* Invalidate cache after read */
518 dma_sync_sg_for_cpu(mmc_dev(host->mmc), data->sg,
519 data->sg_len, DMA_FROM_DEVICE);
521 host->dma_ops->cleanup(host);
524 * If the card was removed, data will be NULL. No point in trying to
525 * send the stop command or waiting for NBUSY in this case.
528 set_bit(EVENT_XFER_COMPLETE, &host->pending_events);
529 tasklet_schedule(&host->tasklet);
533 static void dw_mci_edmac_start_dma(struct dw_mci *host, unsigned int sg_len)
535 struct dma_slave_config slave_config;
536 struct dma_async_tx_descriptor *desc = NULL;
537 struct scatterlist *sgl = host->data->sg;
538 const u32 mszs[] = {1, 4, 8, 16, 32, 64, 128, 256};
539 u32 sg_elems = host->data->sg_len;
540 u32 fifoth_val, mburst;
542 u32 idx, rx_wmark, tx_wmark;
545 /* Set external dma config: burst size, burst width*/
546 slave_config.dst_addr = (dma_addr_t)(host->phy_regs + host->data_offset);
547 slave_config.src_addr = slave_config.dst_addr;
548 slave_config.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
549 slave_config.src_addr_width = slave_config.dst_addr_width;
551 /* Match FIFO dma burst MSIZE with external dma config*/
552 fifoth_val = mci_readl(host, FIFOTH);
553 mburst = mszs[(fifoth_val >> 28) & 0x7];
555 /* Edmac limit burst to 16, but work around for rk3036 to 8 */
556 if (unlikely(cpu_is_rk3036()))
561 if (mburst > burst_limit) {
562 mburst = burst_limit;
563 idx = (ilog2(mburst) > 0) ? (ilog2(mburst) - 1) : 0;
565 rx_wmark = mszs[idx] - 1;
566 tx_wmark = (host->fifo_depth) / 2;
567 fifoth_val = SDMMC_SET_FIFOTH(idx, rx_wmark, tx_wmark);
569 mci_writel(host, FIFOTH, fifoth_val);
572 slave_config.dst_maxburst = mburst;
573 slave_config.src_maxburst = slave_config.dst_maxburst;
575 if(host->data->flags & MMC_DATA_WRITE){
576 slave_config.direction = DMA_MEM_TO_DEV;
577 ret = dmaengine_slave_config(host->dms->ch, &slave_config);
580 "Error in dw_mci edmac write configuration.\n");
584 desc = dmaengine_prep_slave_sg(host->dms->ch, sgl, sg_len,
585 DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
588 "Cannot prepare slave write sg the dw_mci edmac!\n");
592 /* Set dw_mci_edmac_complete_dma as callback */
593 desc->callback = dw_mci_edmac_complete_dma;
594 desc->callback_param = (void *)host;
595 dmaengine_submit(desc);
597 /* Flush cache before write */
598 dma_sync_sg_for_device(mmc_dev(host->mmc), sgl,
599 sg_elems, DMA_TO_DEVICE);
600 dma_async_issue_pending(host->dms->ch);
603 slave_config.direction = DMA_DEV_TO_MEM;
604 ret = dmaengine_slave_config(host->dms->ch, &slave_config);
607 "Error in dw_mci edmac read configuration.\n");
610 desc = dmaengine_prep_slave_sg(host->dms->ch, sgl, sg_len,
611 DMA_DEV_TO_MEM, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
614 "Cannot prepare slave read sg for the dw_mci edmac!\n");
617 /* set dw_mci_edmac_complete_dma as callback */
618 desc->callback = dw_mci_edmac_complete_dma;
619 desc->callback_param = (void *)host;
620 dmaengine_submit(desc);
621 dma_async_issue_pending(host->dms->ch);
625 static int dw_mci_edmac_init(struct dw_mci *host)
629 /* Request external dma channel, SHOULD decide chn in dts */
631 host->dms = (struct dw_mci_dma_slave *)kzalloc
632 (sizeof(struct dw_mci_dma_slave), GFP_KERNEL);
633 if (NULL == host->dms) {
634 dev_err(host->dev, "No enough memory to alloc dms.\n");
638 host->dms->ch = dma_request_slave_channel(host->dev, "dw_mci");
639 if (!host->dms->ch) {
641 "Failed to get external DMA channel %d\n",
642 host->dms->ch->chan_id);
651 static void dw_mci_edmac_exit(struct dw_mci *host)
653 if (NULL != host->dms) {
654 if (NULL != host->dms->ch) {
655 dma_release_channel(host->dms->ch);
656 host->dms->ch = NULL;
663 static const struct dw_mci_dma_ops dw_mci_edmac_ops = {
664 .init = dw_mci_edmac_init,
665 .exit = dw_mci_edmac_exit,
666 .start = dw_mci_edmac_start_dma,
667 .stop = dw_mci_edmac_stop_dma,
668 .complete = dw_mci_edmac_complete_dma,
669 .cleanup = dw_mci_edma_cleanup,
671 #endif /* CONFIG_MMC_DW_IDMAC */
673 static int dw_mci_pre_dma_transfer(struct dw_mci *host,
674 struct mmc_data *data,
677 struct scatterlist *sg;
678 unsigned int i, sg_len;
680 if (!next && data->host_cookie)
681 return data->host_cookie;
684 * We don't do DMA on "complex" transfers, i.e. with
685 * non-word-aligned buffers or lengths. Also, we don't bother
686 * with all the DMA setup overhead for short transfers.
688 if (data->blocks * data->blksz < DW_MCI_DMA_THRESHOLD)
694 for_each_sg(data->sg, sg, data->sg_len, i) {
695 if (sg->offset & 3 || sg->length & 3)
699 sg_len = dma_map_sg(host->dev,
702 dw_mci_get_dma_dir(data));
707 data->host_cookie = sg_len;
712 static void dw_mci_pre_req(struct mmc_host *mmc,
713 struct mmc_request *mrq,
716 struct dw_mci_slot *slot = mmc_priv(mmc);
717 struct mmc_data *data = mrq->data;
719 if (!slot->host->use_dma || !data)
722 if (data->host_cookie) {
723 data->host_cookie = 0;
727 if (dw_mci_pre_dma_transfer(slot->host, mrq->data, 1) < 0)
728 data->host_cookie = 0;
731 static void dw_mci_post_req(struct mmc_host *mmc,
732 struct mmc_request *mrq,
735 struct dw_mci_slot *slot = mmc_priv(mmc);
736 struct mmc_data *data = mrq->data;
738 if (!slot->host->use_dma || !data)
741 if (data->host_cookie)
742 dma_unmap_sg(slot->host->dev,
745 dw_mci_get_dma_dir(data));
746 data->host_cookie = 0;
749 static void dw_mci_adjust_fifoth(struct dw_mci *host, struct mmc_data *data)
751 #ifdef CONFIG_MMC_DW_IDMAC
752 unsigned int blksz = data->blksz;
753 const u32 mszs[] = {1, 4, 8, 16, 32, 64, 128, 256};
754 u32 fifo_width = 1 << host->data_shift;
755 u32 blksz_depth = blksz / fifo_width, fifoth_val;
756 u32 msize = 0, rx_wmark = 1, tx_wmark, tx_wmark_invers;
757 int idx = (sizeof(mszs) / sizeof(mszs[0])) - 1;
759 tx_wmark = (host->fifo_depth) / 2;
760 tx_wmark_invers = host->fifo_depth - tx_wmark;
764 * if blksz is not a multiple of the FIFO width
766 if (blksz % fifo_width) {
773 if (!((blksz_depth % mszs[idx]) ||
774 (tx_wmark_invers % mszs[idx]))) {
776 rx_wmark = mszs[idx] - 1;
781 * If idx is '0', it won't be tried
782 * Thus, initial values are uesed
785 fifoth_val = SDMMC_SET_FIFOTH(msize, rx_wmark, tx_wmark);
786 mci_writel(host, FIFOTH, fifoth_val);
791 static void dw_mci_ctrl_rd_thld(struct dw_mci *host, struct mmc_data *data)
793 unsigned int blksz = data->blksz;
794 u32 blksz_depth, fifo_depth;
797 WARN_ON(!(data->flags & MMC_DATA_READ));
799 if (host->timing != MMC_TIMING_MMC_HS200 &&
800 host->timing != MMC_TIMING_UHS_SDR104)
803 blksz_depth = blksz / (1 << host->data_shift);
804 fifo_depth = host->fifo_depth;
806 if (blksz_depth > fifo_depth)
810 * If (blksz_depth) >= (fifo_depth >> 1), should be 'thld_size <= blksz'
811 * If (blksz_depth) < (fifo_depth >> 1), should be thld_size = blksz
812 * Currently just choose blksz.
815 mci_writel(host, CDTHRCTL, SDMMC_SET_RD_THLD(thld_size, 1));
819 mci_writel(host, CDTHRCTL, SDMMC_SET_RD_THLD(0, 0));
822 static int dw_mci_submit_data_dma(struct dw_mci *host, struct mmc_data *data)
830 /* If we don't have a channel, we can't do DMA */
834 sg_len = dw_mci_pre_dma_transfer(host, data, 0);
836 /* Fixme: No need terminate edma, may cause flush op */
837 if(!(cpu_is_rk3036() || cpu_is_rk312x()))
838 host->dma_ops->stop(host);
845 "sd sg_cpu: %#lx sg_dma: %#lx sg_len: %d\n",
846 (unsigned long)host->sg_cpu, (unsigned long)host->sg_dma,
850 * Decide the MSIZE and RX/TX Watermark.
851 * If current block size is same with previous size,
852 * no need to update fifoth.
854 if (host->prev_blksz != data->blksz)
855 dw_mci_adjust_fifoth(host, data);
858 dw_mci_ctrl_reset(host, SDMMC_CTRL_DMA_RESET);
860 /* Enable the DMA interface */
861 temp = mci_readl(host, CTRL);
862 temp |= SDMMC_CTRL_DMA_ENABLE;
863 mci_writel(host, CTRL, temp);
865 /* Disable RX/TX IRQs, let DMA handle it */
866 spin_lock_irqsave(&host->slock, flags);
867 temp = mci_readl(host, INTMASK);
868 temp &= ~(SDMMC_INT_RXDR | SDMMC_INT_TXDR);
869 mci_writel(host, INTMASK, temp);
870 spin_unlock_irqrestore(&host->slock, flags);
872 host->dma_ops->start(host, sg_len);
877 static void dw_mci_submit_data(struct dw_mci *host, struct mmc_data *data)
882 data->error = -EINPROGRESS;
884 //WARN_ON(host->data);
889 dw_mci_ctrl_reset(host, SDMMC_CTRL_DMA_RESET);
891 if (data->flags & MMC_DATA_READ) {
892 host->dir_status = DW_MCI_RECV_STATUS;
893 dw_mci_ctrl_rd_thld(host, data);
895 host->dir_status = DW_MCI_SEND_STATUS;
898 MMC_DBG_INFO_FUNC(host->mmc,
899 "Dw_mci_submit_data, blocks = %d, blksz = %d [%s]",
900 data->blocks, data->blksz, mmc_hostname(host->mmc));
902 if (dw_mci_submit_data_dma(host, data)) {
903 int flags = SG_MITER_ATOMIC;
904 if (host->data->flags & MMC_DATA_READ)
905 flags |= SG_MITER_TO_SG;
907 flags |= SG_MITER_FROM_SG;
909 sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
911 host->part_buf_start = 0;
912 host->part_buf_count = 0;
914 spin_lock_irqsave(&host->slock, flag);
915 mci_writel(host, RINTSTS, SDMMC_INT_TXDR | SDMMC_INT_RXDR);
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->slock, flag);
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 #ifdef SDMMC_WAIT_FOR_UNBUSY
950 timeout = jiffies + msecs_to_jiffies(SDMMC_WAIT_FOR_UNBUSY);
952 if (test_bit(DW_MMC_CARD_PRESENT, &slot->flags)) {
954 ret = time_before(jiffies, timeout);
955 cmd_status = mci_readl(host, STATUS);
957 (SDMMC_STAUTS_DATA_BUSY |
958 SDMMC_STAUTS_MC_BUSY)))
963 MMC_DBG_ERR_FUNC(host->mmc,
964 "mci_send_cmd: wait for unbusy timeout! [%s]",
965 mmc_hostname(host->mmc));
969 mci_writel(host, CMDARG, arg);
971 mci_writel(host, CMD, SDMMC_CMD_START | cmd);
972 if(cmd & SDMMC_CMD_UPD_CLK)
973 timeout = jiffies + msecs_to_jiffies(50);
975 timeout = jiffies + msecs_to_jiffies(500);
977 while (time_before(jiffies, timeout)) {
978 cmd_status = mci_readl(host, CMD);
979 if (!(cmd_status & SDMMC_CMD_START))
983 dev_err(&slot->mmc->class_dev,
984 "Timeout sending command (cmd %#x arg %#x status %#x)\n",
985 cmd, arg, cmd_status);
988 static void dw_mci_setup_bus(struct dw_mci_slot *slot, bool force_clkinit)
990 struct dw_mci *host = slot->host;
991 unsigned int tempck,clock = slot->clock;
996 MMC_DBG_INFO_FUNC(host->mmc,
997 "%s: clock=%d, current_speed=%d, bus_hz=%d, forc=%d[%s]\n",
998 __FUNCTION__, clock, host->current_speed, host->bus_hz,
999 force_clkinit, mmc_hostname(host->mmc));
1002 mci_writel(host, CLKENA, 0);
1003 #ifdef CONFIG_MMC_DW_ROCKCHIP_SWITCH_VOLTAGE
1004 if(host->svi_flags == 0)
1005 mci_send_cmd(slot, SDMMC_CMD_UPD_CLK | SDMMC_CMD_PRV_DAT_WAIT, 0);
1007 mci_send_cmd(slot, SDMMC_CMD_UPD_CLK | SDMMC_CMD_PRV_DAT_WAIT, 0);
1009 } else if (clock != host->current_speed || force_clkinit) {
1010 div = host->bus_hz / clock;
1011 if (host->bus_hz % clock && host->bus_hz > clock)
1013 * move the + 1 after the divide to prevent
1014 * over-clocking the card.
1018 div = (host->bus_hz != clock) ? DIV_ROUND_UP(div, 2) : 0;
1020 if ((clock << div) != slot->__clk_old || force_clkinit) {
1021 tempck = div ? ((host->bus_hz / div) >> 1) :host->bus_hz;
1022 dev_info(&slot->mmc->class_dev,
1023 "Bus speed (slot %d) = %dHz (slot req %dHz, actual %dHZ div = %d)\n",
1024 slot->id, host->bus_hz, clock,
1027 host->set_speed = tempck;
1028 host->set_div = div;
1032 mci_writel(host, CLKENA, 0);
1033 mci_writel(host, CLKSRC, 0);
1037 SDMMC_CMD_UPD_CLK | SDMMC_CMD_PRV_DAT_WAIT, 0);
1039 if(clock <= 400*1000){
1040 MMC_DBG_BOOT_FUNC(host->mmc,
1041 "dw_mci_setup_bus: argue clk_mmc workaround out %dHz for init[%s]",
1042 clock * 2, mmc_hostname(host->mmc));
1043 /* clk_mmc will change parents to 24MHz xtal*/
1044 clk_set_rate(host->clk_mmc, clock * 2);
1047 host->set_div = div;
1051 MMC_DBG_BOOT_FUNC(host->mmc,
1052 "dw_mci_setup_bus: argue clk_mmc workaround out normal clock [%s]",
1053 mmc_hostname(host->mmc));
1056 MMC_DBG_ERR_FUNC(host->mmc,
1057 "dw_mci_setup_bus: div SHOULD NOT LARGER THAN ONE! [%s]",
1058 mmc_hostname(host->mmc));
1060 host->set_div = div;
1061 host->bus_hz = host->set_speed * 2;
1062 MMC_DBG_BOOT_FUNC(host->mmc,
1063 "dw_mci_setup_bus: workaround div = %d, host->bus_hz = %d [%s]",
1064 div, host->bus_hz, mmc_hostname(host->mmc));
1066 /* BUG may be here, come on, Linux BSP engineer looks!
1067 FIXME: HS-DDR eMMC, div SHOULD be ONE, but we here cannot fetch eMMC bus mode!!!!!!!!
1068 WRONG dts set clk = 50M, and calc div be zero. Controller denied this setting!
1069 some oops happened like that:
1070 mmc_host mmc0: Bus speed (slot 0) = 50000000Hz (slot req 50000000Hz, actual 50000000HZ div = 0)
1071 rk_sdmmc: BOOT dw_mci_setup_bus: argue clk_mmc workaround out normal clock [mmc0]
1072 rk_sdmmc: BOOT Bus speed=50000000Hz,Bus width=8bits.[mmc0]
1073 mmc0: new high speed DDR MMC card at address 0001
1074 mmcblk0: mmc0:0001 M8G1GC 7.28 GiB
1076 mmcblk0: error -84 transferring data, sector 606208, nr 32, cmd response 0x900, card status 0xb00
1077 mmcblk0: retrying using single block read
1078 mmcblk0: error -110 sending status command, retrying
1080 We assume all eMMC in RK platform with 3.10 kernel, at least version 4.5
1083 (host->mmc->caps & (MMC_CAP_1_8V_DDR | MMC_CAP_1_2V_DDR)) &&
1084 !(host->mmc->caps2 & MMC_CAP2_HS200)) {
1087 host->set_div = div;
1088 host->bus_hz = host->set_speed * 2;
1089 MMC_DBG_BOOT_FUNC(host->mmc,
1090 "dw_mci_setup_bus: workaround div = %d, host->bus_hz = %d [%s]",
1091 div, host->bus_hz, mmc_hostname(host->mmc));
1094 if (host->verid < DW_MMC_240A)
1095 clk_set_rate(host->clk_mmc,(host->bus_hz));
1097 clk_set_rate(host->clk_mmc,(host->bus_hz) * 2);
1103 /* set clock to desired speed */
1104 mci_writel(host, CLKDIV, div);
1108 SDMMC_CMD_UPD_CLK | SDMMC_CMD_PRV_DAT_WAIT, 0);
1110 /* enable clock; only low power if no SDIO */
1111 clk_en_a = SDMMC_CLKEN_ENABLE << slot->id;
1113 if (host->verid < DW_MMC_240A)
1114 sdio_int = SDMMC_INT_SDIO(slot->id);
1116 sdio_int = SDMMC_INT_SDIO((slot->id) + 8);
1118 if (!(mci_readl(host, INTMASK) & sdio_int))
1119 clk_en_a |= SDMMC_CLKEN_LOW_PWR << slot->id;
1120 mci_writel(host, CLKENA, clk_en_a);
1124 SDMMC_CMD_UPD_CLK | SDMMC_CMD_PRV_DAT_WAIT, 0);
1125 /* keep the clock with reflecting clock dividor */
1126 slot->__clk_old = clock << div;
1129 host->current_speed = clock;
1131 if(slot->ctype != slot->pre_ctype)
1132 MMC_DBG_BOOT_FUNC(host->mmc,
1133 "Bus speed=%dHz,Bus width=%s.[%s]",
1135 (slot->ctype == SDMMC_CTYPE_4BIT) ? "4bits" : "8bits",
1136 mmc_hostname(host->mmc));
1138 slot->pre_ctype = slot->ctype;
1140 /* Set the current slot bus width */
1141 mci_writel(host, CTYPE, (slot->ctype << slot->id));
1144 extern struct mmc_card *this_card;
1145 static void dw_mci_wait_unbusy(struct dw_mci *host)
1147 unsigned int timeout = SDMMC_DATA_TIMEOUT_SDIO;
1148 unsigned long time_loop;
1149 unsigned int status;
1150 unsigned int tmo = 300000;
1151 /* Secure erase flag */
1154 MMC_DBG_INFO_FUNC(host->mmc,
1155 "dw_mci_wait_unbusy, status=0x%x ", mci_readl(host, STATUS));
1157 if (host->mmc->restrict_caps & RESTRICT_CARD_TYPE_EMMC) {
1158 if (host->cmd && (host->cmd->opcode == MMC_ERASE) && this_card) {
1159 /* Special care for (secure)erase timeout calculation */
1160 if ((host->cmd->arg & (0x1 << 31)) == 1)
1163 if (((this_card->ext_csd.erase_group_def) & 0x1) == 1)
1165 (timeout = (this_card->ext_csd.hc_erase_timeout) *
1166 tmo * (this_card->ext_csd.sec_erase_mult)) :
1167 (timeout = (this_card->ext_csd.hc_erase_timeout) * tmo);
1170 if(timeout < SDMMC_DATA_TIMEOUT_EMMC)
1171 timeout = SDMMC_DATA_TIMEOUT_EMMC;
1172 } else if (host->mmc->restrict_caps & RESTRICT_CARD_TYPE_SD) {
1173 timeout = SDMMC_DATA_TIMEOUT_SD;
1176 time_loop = jiffies + msecs_to_jiffies(timeout);
1178 status = mci_readl(host, STATUS);
1179 if (!(status & (SDMMC_STAUTS_DATA_BUSY | SDMMC_STAUTS_MC_BUSY)))
1181 } while (time_before(jiffies, time_loop));
1184 #ifdef CONFIG_MMC_DW_ROCKCHIP_SWITCH_VOLTAGE
1187 * 0--status is busy.
1188 * 1--status is unbusy.
1190 int dw_mci_card_busy(struct mmc_host *mmc)
1192 struct dw_mci_slot *slot = mmc_priv(mmc);
1193 struct dw_mci *host = slot->host;
1195 MMC_DBG_INFO_FUNC(host->mmc, "dw_mci_card_busy: svi_flags = %d [%s]", \
1196 host->svi_flags, mmc_hostname(host->mmc));
1199 if(host->svi_flags == 0){
1201 host->svi_flags = 1;
1202 return host->svi_flags;
1205 host->svi_flags = 0;
1206 return host->svi_flags;
1212 static void __dw_mci_start_request(struct dw_mci *host,
1213 struct dw_mci_slot *slot,
1214 struct mmc_command *cmd)
1216 struct mmc_request *mrq;
1217 struct mmc_data *data;
1221 if (host->pdata->select_slot)
1222 host->pdata->select_slot(slot->id);
1224 host->cur_slot = slot;
1227 dw_mci_wait_unbusy(host);
1229 host->pending_events = 0;
1230 host->completed_events = 0;
1231 host->data_status = 0;
1235 dw_mci_set_timeout(host);
1236 mci_writel(host, BYTCNT, data->blksz*data->blocks);
1237 mci_writel(host, BLKSIZ, data->blksz);
1240 cmdflags = dw_mci_prepare_command(slot->mmc, cmd);
1242 /* this is the first command, send the initialization clock */
1243 if (test_and_clear_bit(DW_MMC_CARD_NEED_INIT, &slot->flags))
1244 cmdflags |= SDMMC_CMD_INIT;
1247 dw_mci_submit_data(host, data);
1251 dw_mci_start_command(host, cmd, cmdflags);
1254 host->stop_cmdr = dw_mci_prepare_command(slot->mmc, mrq->stop);
1257 static void dw_mci_start_request(struct dw_mci *host,
1258 struct dw_mci_slot *slot)
1260 struct mmc_request *mrq = slot->mrq;
1261 struct mmc_command *cmd;
1263 MMC_DBG_INFO_FUNC(host->mmc,
1264 " Begin to start the new request. cmd=%d(arg=0x%x)[%s]",
1265 mrq->cmd->opcode, mrq->cmd->arg, mmc_hostname(host->mmc));
1267 cmd = mrq->sbc ? mrq->sbc : mrq->cmd;
1268 __dw_mci_start_request(host, slot, cmd);
1271 /* must be called with host->lock held */
1272 static void dw_mci_queue_request(struct dw_mci *host, struct dw_mci_slot *slot,
1273 struct mmc_request *mrq)
1275 dev_vdbg(&slot->mmc->class_dev, "queue request: state=%d\n",
1280 if (host->state == STATE_IDLE) {
1281 host->state = STATE_SENDING_CMD;
1282 dw_mci_start_request(host, slot);
1284 list_add_tail(&slot->queue_node, &host->queue);
1288 static void dw_mci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1290 struct dw_mci_slot *slot = mmc_priv(mmc);
1291 struct dw_mci *host = slot->host;
1296 * The check for card presence and queueing of the request must be
1297 * atomic, otherwise the card could be removed in between and the
1298 * request wouldn't fail until another card was inserted.
1300 spin_lock_bh(&host->lock);
1302 if (!test_bit(DW_MMC_CARD_PRESENT, &slot->flags)) {
1303 spin_unlock_bh(&host->lock);
1304 mrq->cmd->error = -ENOMEDIUM;
1305 MMC_DBG_CMD_FUNC(host->mmc, "%s: no card,so reqeuest done, cmd=%d [%s]",
1306 __FUNCTION__, mrq->cmd->opcode, mmc_hostname(host->mmc));
1308 mmc_request_done(mmc, mrq);
1312 MMC_DBG_CMD_FUNC(host->mmc,
1313 "======>\n pull a new request from MMC-frame to dw_mci_queue. cmd=%d(arg=0x%x)[%s]",
1314 mrq->cmd->opcode, mrq->cmd->arg, mmc_hostname(host->mmc));
1316 dw_mci_queue_request(host, slot, mrq);
1318 spin_unlock_bh(&host->lock);
1321 static void dw_mci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1323 struct dw_mci_slot *slot = mmc_priv(mmc);
1324 #ifdef CONFIG_MMC_DW_ROCKCHIP_SWITCH_VOLTAGE
1325 struct dw_mci *host = slot->host;
1327 const struct dw_mci_drv_data *drv_data = slot->host->drv_data;
1330 #ifdef SDMMC_WAIT_FOR_UNBUSY
1331 unsigned long time_loop;
1334 #ifdef CONFIG_MMC_DW_ROCKCHIP_SWITCH_VOLTAGE
1335 if(host->svi_flags == 1)
1336 time_loop = jiffies + msecs_to_jiffies(SDMMC_DATA_TIMEOUT_SD);
1338 time_loop = jiffies + msecs_to_jiffies(SDMMC_WAIT_FOR_UNBUSY);
1340 time_loop = jiffies + msecs_to_jiffies(SDMMC_WAIT_FOR_UNBUSY);
1343 if(!test_bit(DW_MMC_CARD_PRESENT, &slot->flags)){
1344 printk("%d..%s: no card. [%s]\n", \
1345 __LINE__, __FUNCTION__, mmc_hostname(mmc));
1350 ret = time_before(jiffies, time_loop);
1351 regs = mci_readl(slot->host, STATUS);
1352 if (!(regs & (SDMMC_STAUTS_DATA_BUSY |
1353 SDMMC_STAUTS_MC_BUSY)))
1359 printk("slot->flags = %lu ", slot->flags);
1360 #ifdef CONFIG_MMC_DW_ROCKCHIP_SWITCH_VOLTAGE
1361 if(host->svi_flags != 1)
1364 printk("%s: wait for unbusy timeout....... STATUS = 0x%x [%s]\n",
1365 __FUNCTION__, regs, mmc_hostname(mmc));
1369 switch (ios->bus_width) {
1370 case MMC_BUS_WIDTH_4:
1371 slot->ctype = SDMMC_CTYPE_4BIT;
1373 case MMC_BUS_WIDTH_8:
1374 slot->ctype = SDMMC_CTYPE_8BIT;
1377 /* set default 1 bit mode */
1378 slot->ctype = SDMMC_CTYPE_1BIT;
1379 slot->pre_ctype = SDMMC_CTYPE_1BIT;
1382 regs = mci_readl(slot->host, UHS_REG);
1385 if (ios->timing == MMC_TIMING_UHS_DDR50)
1386 regs |= ((0x1 << slot->id) << 16);
1388 regs &= ~((0x1 << slot->id) << 16);
1390 mci_writel(slot->host, UHS_REG, regs);
1391 slot->host->timing = ios->timing;
1394 * Use mirror of ios->clock to prevent race with mmc
1395 * core ios update when finding the minimum.
1397 slot->clock = ios->clock;
1399 if (drv_data && drv_data->set_ios)
1400 drv_data->set_ios(slot->host, ios);
1402 /* Slot specific timing and width adjustment */
1403 dw_mci_setup_bus(slot, false);
1407 switch (ios->power_mode) {
1410 set_bit(DW_MMC_CARD_NEED_INIT, &slot->flags);
1411 if (slot->host->pdata->setpower)
1412 slot->host->pdata->setpower(slot->id, mmc->ocr_avail);
1413 regs = mci_readl(slot->host, PWREN);
1414 regs |= (1 << slot->id);
1415 mci_writel(slot->host, PWREN, regs);
1418 /* Power down slot */
1419 if(slot->host->pdata->setpower)
1420 slot->host->pdata->setpower(slot->id, 0);
1421 regs = mci_readl(slot->host, PWREN);
1422 regs &= ~(1 << slot->id);
1423 mci_writel(slot->host, PWREN, regs);
1430 static int dw_mci_get_ro(struct mmc_host *mmc)
1433 struct dw_mci_slot *slot = mmc_priv(mmc);
1434 struct dw_mci_board *brd = slot->host->pdata;
1436 /* Use platform get_ro function, else try on board write protect */
1437 if(slot->quirks & DW_MCI_SLOT_QUIRK_NO_WRITE_PROTECT)
1439 else if(brd->get_ro)
1440 read_only = brd->get_ro(slot->id);
1441 else if(gpio_is_valid(slot->wp_gpio))
1442 read_only = gpio_get_value(slot->wp_gpio);
1445 mci_readl(slot->host, WRTPRT) & (1 << slot->id) ? 1 : 0;
1447 dev_dbg(&mmc->class_dev, "card is %s\n",
1448 read_only ? "read-only" : "read-write");
1453 static int dw_mci_set_sdio_status(struct mmc_host *mmc, int val)
1455 struct dw_mci_slot *slot = mmc_priv(mmc);
1456 struct dw_mci *host = slot->host;
1458 if (!(mmc->restrict_caps & RESTRICT_CARD_TYPE_SDIO))
1461 spin_lock_bh(&host->lock);
1464 set_bit(DW_MMC_CARD_PRESENT, &slot->flags);
1466 clear_bit(DW_MMC_CARD_PRESENT, &slot->flags);
1468 spin_unlock_bh(&host->lock);
1470 if (test_bit(DW_MMC_CARD_PRESENT, &slot->flags)) {
1471 if (__clk_is_enabled(host->hclk_mmc) == false)
1472 clk_prepare_enable(host->hclk_mmc);
1473 if (__clk_is_enabled(host->clk_mmc) == false)
1474 clk_prepare_enable(host->clk_mmc);
1476 if (__clk_is_enabled(host->clk_mmc) == true)
1477 clk_disable_unprepare(slot->host->clk_mmc);
1478 if (__clk_is_enabled(host->hclk_mmc) == true)
1479 clk_disable_unprepare(slot->host->hclk_mmc);
1482 mmc_detect_change(slot->mmc, 20);
1488 static int dw_mci_get_cd(struct mmc_host *mmc)
1491 struct dw_mci_slot *slot = mmc_priv(mmc);
1492 struct dw_mci_board *brd = slot->host->pdata;
1493 struct dw_mci *host = slot->host;
1494 int gpio_cd = mmc_gpio_get_cd(mmc);
1495 int force_jtag_bit, force_jtag_reg;
1499 if ((soc_is_rk3126() || soc_is_rk3126b() || soc_is_rk3036()) &&
1500 (mmc->restrict_caps & RESTRICT_CARD_TYPE_SD)) {
1501 gpio_cd = slot->cd_gpio;
1502 irq = gpio_to_irq(gpio_cd);
1503 if (gpio_is_valid(gpio_cd)) {
1504 gpio_val = gpio_get_value(gpio_cd);
1505 if (soc_is_rk3036()) {
1506 force_jtag_bit = 11;
1507 force_jtag_reg = RK312X_GRF_SOC_CON0;
1508 } else if (soc_is_rk3126() || soc_is_rk3126b()) {
1509 force_jtag_reg = RK312X_GRF_SOC_CON0;
1513 if (gpio_val == gpio_get_value(gpio_cd)) {
1514 gpio_cd = (gpio_val == 0 ? 1 : 0);
1516 irq_set_irq_type(irq, IRQF_TRIGGER_LOW | IRQF_ONESHOT);
1517 /* Enable force_jtag wihtout card in slot, ONLY for NCD-package */
1518 grf_writel((0x1 << (force_jtag_bit + 16)) | (1 << force_jtag_bit),
1520 dw_mci_ctrl_all_reset(host);
1522 irq_set_irq_type(irq, IRQF_TRIGGER_HIGH | IRQF_ONESHOT);
1523 /* Really card detected: SHOULD disable force_jtag */
1524 grf_writel((0x1 << (force_jtag_bit + 16)) | (0 << force_jtag_bit),
1529 gpio_val = gpio_get_value(gpio_cd);
1531 irq_set_irq_type(irq, IRQF_TRIGGER_HIGH | IRQF_ONESHOT) :
1532 irq_set_irq_type(irq, IRQF_TRIGGER_LOW | IRQF_ONESHOT);
1533 return slot->last_detect_state;
1536 dev_err(host->dev, "dw_mci_get_cd: invalid gpio_cd!\n");
1540 if (mmc->restrict_caps & RESTRICT_CARD_TYPE_SDIO)
1541 return test_bit(DW_MMC_CARD_PRESENT, &slot->flags);
1543 /* Use platform get_cd function, else try onboard card detect */
1544 if (brd->quirks & DW_MCI_QUIRK_BROKEN_CARD_DETECTION)
1546 else if (brd->get_cd)
1547 present = !brd->get_cd(slot->id);
1548 else if (!IS_ERR_VALUE(gpio_cd))
1551 present = ((mci_readl(slot->host, CDETECT) & (1 << slot->id))
1554 spin_lock_bh(&host->lock);
1556 set_bit(DW_MMC_CARD_PRESENT, &slot->flags);
1557 dev_dbg(&mmc->class_dev, "card is present\n");
1559 clear_bit(DW_MMC_CARD_PRESENT, &slot->flags);
1560 dev_dbg(&mmc->class_dev, "card is not present\n");
1562 spin_unlock_bh(&host->lock);
1569 * Dts Should caps emmc controller with poll-hw-reset
1571 static void dw_mci_hw_reset(struct mmc_host *mmc)
1573 struct dw_mci_slot *slot = mmc_priv(mmc);
1574 struct dw_mci *host = slot->host;
1579 unsigned long timeout;
1582 /* (1) CMD12 to end any transfer in process */
1583 cmd_flags = SDMMC_CMD_STOP | SDMMC_CMD_RESP_CRC
1584 | SDMMC_CMD_RESP_EXP | MMC_STOP_TRANSMISSION;
1586 if(host->mmc->hold_reg_flag)
1587 cmd_flags |= SDMMC_CMD_USE_HOLD_REG;
1588 mci_writel(host, CMDARG, 0);
1590 mci_writel(host, CMD, cmd_flags | SDMMC_CMD_START);
1592 timeout = jiffies + msecs_to_jiffies(500);
1594 ret = time_before(jiffies, timeout);
1595 if(!(mci_readl(host, CMD) & SDMMC_CMD_START))
1600 MMC_DBG_ERR_FUNC(host->mmc,
1601 "%s dw_mci_hw_reset: STOP_TRANSMISSION failed!!! [%s]\n",
1602 __func__, mmc_hostname(host->mmc));
1604 /* (2) wait DTO, even if no response is sent back by card */
1606 timeout = jiffies + msecs_to_jiffies(5);
1608 ret = time_before(jiffies, timeout);
1609 if(!(mci_readl(host, MINTSTS) & SDMMC_INT_DATA_OVER)){
1610 mci_writel(host, RINTSTS, SDMMC_INT_DATA_OVER);
1616 /* (3) Reset following: DONNOT CHANGE RESET ORDER!*/
1618 /* Software reset - BMOD[0] for IDMA only */
1619 regs = mci_readl(host, BMOD);
1620 regs |= SDMMC_IDMAC_SWRESET;
1621 mci_writel(host, BMOD, regs);
1622 udelay(1); /* Auto cleared after 1 cycle, 1us is enough for hclk_mmc */
1623 regs = mci_readl(host, BMOD);
1624 if(regs & SDMMC_IDMAC_SWRESET)
1625 MMC_DBG_WARN_FUNC(host->mmc,
1626 "%s dw_mci_hw_reset: SDMMC_IDMAC_SWRESET failed!!! [%s]\n",
1627 __func__, mmc_hostname(host->mmc));
1629 /* DMA reset - CTRL[2] */
1630 regs = mci_readl(host, CTRL);
1631 regs |= SDMMC_CTRL_DMA_RESET;
1632 mci_writel(host, CTRL, regs);
1633 udelay(1); /* Auto cleared after 2 AHB clocks, 1us is enough plus mci_readl access */
1634 regs = mci_readl(host, CTRL);
1635 if(regs & SDMMC_CTRL_DMA_RESET)
1636 MMC_DBG_WARN_FUNC(host->mmc,
1637 "%s dw_mci_hw_reset: SDMMC_CTRL_DMA_RESET failed!!! [%s]\n",
1638 __func__, mmc_hostname(host->mmc));
1640 /* FIFO reset - CTRL[1] */
1641 regs = mci_readl(host, CTRL);
1642 regs |= SDMMC_CTRL_FIFO_RESET;
1643 mci_writel(host, CTRL, regs);
1644 mdelay(1); /* no timing limited, 1ms is random value */
1645 regs = mci_readl(host, CTRL);
1646 if(regs & SDMMC_CTRL_FIFO_RESET)
1647 MMC_DBG_WARN_FUNC(host->mmc,
1648 "%s dw_mci_hw_reset: SDMMC_CTRL_DMA_RESET failed!!! [%s]\n",
1649 __func__, mmc_hostname(host->mmc));
1652 According to eMMC spec
1653 tRstW >= 1us ; RST_n pulse width
1654 tRSCA >= 200us ; RST_n to Command time
1655 tRSTH >= 1us ; RST_n high period
1657 mci_writel(slot->host, PWREN, 0x0);
1658 mci_writel(slot->host, RST_N, 0x0);
1660 udelay(10); /* 10us for bad quality eMMc. */
1662 mci_writel(slot->host, PWREN, 0x1);
1663 mci_writel(slot->host, RST_N, 0x1);
1665 usleep_range(500, 1000); /* at least 500(> 200us) */
1669 * Disable lower power mode.
1671 * Low power mode will stop the card clock when idle. According to the
1672 * description of the CLKENA register we should disable low power mode
1673 * for SDIO cards if we need SDIO interrupts to work.
1675 * This function is fast if low power mode is already disabled.
1677 static void dw_mci_disable_low_power(struct dw_mci_slot *slot)
1679 struct dw_mci *host = slot->host;
1681 const u32 clken_low_pwr = SDMMC_CLKEN_LOW_PWR << slot->id;
1683 clk_en_a = mci_readl(host, CLKENA);
1685 if (clk_en_a & clken_low_pwr) {
1686 mci_writel(host, CLKENA, clk_en_a & ~clken_low_pwr);
1687 mci_send_cmd(slot, SDMMC_CMD_UPD_CLK |
1688 SDMMC_CMD_PRV_DAT_WAIT, 0);
1692 static void dw_mci_enable_sdio_irq(struct mmc_host *mmc, int enb)
1694 struct dw_mci_slot *slot = mmc_priv(mmc);
1695 struct dw_mci *host = slot->host;
1696 unsigned long flags;
1700 spin_lock_irqsave(&host->slock, flags);
1702 /* Enable/disable Slot Specific SDIO interrupt */
1703 int_mask = mci_readl(host, INTMASK);
1705 if (host->verid < DW_MMC_240A)
1706 sdio_int = SDMMC_INT_SDIO(slot->id);
1708 sdio_int = SDMMC_INT_SDIO((slot->id) + 8);
1712 * Turn off low power mode if it was enabled. This is a bit of
1713 * a heavy operation and we disable / enable IRQs a lot, so
1714 * we'll leave low power mode disabled and it will get
1715 * re-enabled again in dw_mci_setup_bus().
1717 dw_mci_disable_low_power(slot);
1719 mci_writel(host, INTMASK,
1720 (int_mask | sdio_int));
1722 mci_writel(host, INTMASK,
1723 (int_mask & ~sdio_int));
1726 spin_unlock_irqrestore(&host->slock, flags);
1729 #ifdef CONFIG_MMC_DW_ROCKCHIP_SWITCH_VOLTAGE
1731 IO_DOMAIN_12 = 1200,
1732 IO_DOMAIN_18 = 1800,
1733 IO_DOMAIN_33 = 3300,
1735 static void dw_mci_do_grf_io_domain_switch(struct dw_mci *host, u32 voltage)
1745 MMC_DBG_ERR_FUNC(host->mmc,
1746 "%s : Not support io domain voltage [%s]\n",
1747 __FUNCTION__, mmc_hostname(host->mmc));
1750 MMC_DBG_ERR_FUNC(host->mmc,
1751 "%s : Err io domain voltage [%s]\n",
1752 __FUNCTION__, mmc_hostname(host->mmc));
1756 if (cpu_is_rk3288()) {
1757 if(host->mmc->restrict_caps & RESTRICT_CARD_TYPE_SD)
1758 grf_writel((voltage << 7) | (1 << 23), RK3288_GRF_IO_VSEL);
1761 } else if (host->cid == DW_MCI_TYPE_RK3368) {
1762 if(host->mmc->restrict_caps & RESTRICT_CARD_TYPE_SD)
1763 regmap_write(host->grf, 0x900, (voltage << 6) | (1 << 22));
1767 MMC_DBG_ERR_FUNC(host->mmc,
1768 "%s : unknown chip [%s]\n",
1769 __FUNCTION__, mmc_hostname(host->mmc));
1773 static int dw_mci_do_start_signal_voltage_switch(struct dw_mci *host,
1774 struct mmc_ios *ios)
1777 unsigned int value,uhs_reg;
1780 * Signal Voltage Switching is only applicable for Host Controllers
1783 if (host->verid < DW_MMC_240A)
1786 uhs_reg = mci_readl(host, UHS_REG);
1787 MMC_DBG_SW_VOL_FUNC(host->mmc, "%s: vol=%d.[%s]\n",
1788 __FUNCTION__, ios->signal_voltage, mmc_hostname(host->mmc));
1790 switch (ios->signal_voltage) {
1791 case MMC_SIGNAL_VOLTAGE_330:
1792 /* Set 1.8V Signal Enable in the Host Control2 register to 0 */
1794 if (cpu_is_rk3288())
1795 ret = io_domain_regulator_set_voltage(
1796 host->vmmc, 3300000, 3300000);
1798 ret = regulator_set_voltage(host->vmmc,
1801 /* regulator_put(host->vmmc); */
1802 MMC_DBG_SW_VOL_FUNC(host->mmc,"%s =%dmV set 3.3 end, ret = %d\n",
1803 __func__, regulator_get_voltage(host->vmmc), ret);
1805 MMC_DBG_SW_VOL_FUNC(host->mmc,
1806 "%s: Switching to 3.3V signalling voltage "
1807 " failed\n", mmc_hostname(host->mmc));
1810 dw_mci_do_grf_io_domain_switch(host, IO_DOMAIN_33);
1813 MMC_DBG_SW_VOL_FUNC(host->mmc, "%s: [%s]\n",
1814 __FUNCTION__, mmc_hostname(host->mmc));
1816 /* set High-power mode */
1817 value = mci_readl(host, CLKENA);
1818 value &= ~SDMMC_CLKEN_LOW_PWR;
1819 mci_writel(host,CLKENA , value);
1821 uhs_reg &= ~SDMMC_UHS_VOLT_REG_18;
1822 mci_writel(host,UHS_REG , uhs_reg);
1825 usleep_range(5000, 5500);
1827 /* 3.3V regulator output should be stable within 5 ms */
1828 uhs_reg = mci_readl(host, UHS_REG);
1829 if( !(uhs_reg & SDMMC_UHS_VOLT_REG_18))
1832 MMC_DBG_SW_VOL_FUNC(host->mmc,
1833 "%s: 3.3V regulator output did not became stable\n",
1834 mmc_hostname(host->mmc));
1837 case MMC_SIGNAL_VOLTAGE_180:
1839 if (cpu_is_rk3288())
1840 ret = io_domain_regulator_set_voltage(
1841 host->vmmc, 1800000, 1800000);
1843 ret = regulator_set_voltage(
1844 host->vmmc, 1800000, 1800000);
1845 /* regulator_put(host->vmmc); */
1846 MMC_DBG_SW_VOL_FUNC(host->mmc,
1847 "%s =%dmV set 1.8end, ret=%d . \n",
1848 __func__, regulator_get_voltage(host->vmmc), ret);
1850 MMC_DBG_SW_VOL_FUNC(host->mmc,
1851 "%s: Switching to 1.8V signalling voltage "
1852 " failed\n", mmc_hostname(host->mmc));
1855 dw_mci_do_grf_io_domain_switch(host, IO_DOMAIN_18);
1859 * Enable 1.8V Signal Enable in the Host Control2
1862 mci_writel(host,UHS_REG , uhs_reg | SDMMC_UHS_VOLT_REG_18);
1865 usleep_range(5000, 5500);
1866 MMC_DBG_SW_VOL_FUNC(host->mmc, "%d..%s: .[%s]\n",__LINE__,
1867 __FUNCTION__, mmc_hostname(host->mmc));
1869 /* 1.8V regulator output should be stable within 5 ms */
1870 uhs_reg = mci_readl(host, UHS_REG);
1871 if (uhs_reg & SDMMC_UHS_VOLT_REG_18)
1874 MMC_DBG_SW_VOL_FUNC(host->mmc,
1875 "%s: 1.8V regulator output did not became stable\n",
1876 mmc_hostname(host->mmc));
1879 case MMC_SIGNAL_VOLTAGE_120:
1881 if (cpu_is_rk3288())
1882 ret = io_domain_regulator_set_voltage(
1883 host->vmmc, 1200000, 1200000);
1885 ret = regulator_set_voltage(host->vmmc,
1888 MMC_DBG_SW_VOL_FUNC(host->mmc,
1889 "%s: Switching to 1.2V signalling voltage "
1890 " failed\n", mmc_hostname(host->mmc));
1896 /* No signal voltage switch required */
1902 static int dw_mci_start_signal_voltage_switch(struct mmc_host *mmc,
1903 struct mmc_ios *ios)
1905 struct dw_mci_slot *slot = mmc_priv(mmc);
1906 struct dw_mci *host = slot->host;
1909 if (host->verid < DW_MMC_240A)
1912 err = dw_mci_do_start_signal_voltage_switch(host, ios);
1918 static int dw_mci_execute_tuning(struct mmc_host *mmc, u32 opcode)
1920 struct dw_mci_slot *slot = mmc_priv(mmc);
1921 struct dw_mci *host = slot->host;
1922 const struct dw_mci_drv_data *drv_data = host->drv_data;
1923 struct dw_mci_tuning_data tuning_data;
1926 /* Fixme: 3036/3126 doesn't support 1.8 io domain, no sense exe tuning */
1927 if(cpu_is_rk3036() || cpu_is_rk312x())
1930 if (opcode == MMC_SEND_TUNING_BLOCK_HS200) {
1931 if (mmc->ios.bus_width == MMC_BUS_WIDTH_8) {
1932 tuning_data.blk_pattern = tuning_blk_pattern_8bit;
1933 tuning_data.blksz = sizeof(tuning_blk_pattern_8bit);
1934 } else if (mmc->ios.bus_width == MMC_BUS_WIDTH_4) {
1935 tuning_data.blk_pattern = tuning_blk_pattern_4bit;
1936 tuning_data.blksz = sizeof(tuning_blk_pattern_4bit);
1940 } else if (opcode == MMC_SEND_TUNING_BLOCK) {
1941 tuning_data.blk_pattern = tuning_blk_pattern_4bit;
1942 tuning_data.blksz = sizeof(tuning_blk_pattern_4bit);
1945 "Undefined command(%d) for tuning\n", opcode);
1950 /* Recommend sample phase and delayline
1951 Fixme: Mix-use these three controllers will cause
1954 if (mmc->restrict_caps & RESTRICT_CARD_TYPE_EMMC)
1955 tuning_data.con_id = 3;
1956 else if(mmc->restrict_caps & RESTRICT_CARD_TYPE_SDIO)
1957 tuning_data.con_id = 1;
1959 tuning_data.con_id = 0;
1961 /* 0: driver, from host->devices
1962 1: sample, from devices->host
1964 tuning_data.tuning_type = 1;
1966 if (drv_data && drv_data->execute_tuning)
1967 err = drv_data->execute_tuning(slot, opcode, &tuning_data);
1972 static void dw_mci_post_tmo(struct mmc_host *mmc)
1974 struct dw_mci_slot *slot = mmc_priv(mmc);
1975 struct dw_mci *host = slot->host;
1976 struct mmc_data *data;
1977 u32 ret, i, regs, cmd_flags;
1979 unsigned long timeout = 0;
1980 bool ret_timeout = true;
1982 host->cur_slot->mrq = NULL;
1984 host->state = STATE_IDLE;
1988 printk("[%s] -- Timeout recovery procedure start --\n",
1989 mmc_hostname(host->mmc));
1991 if (data && (data->stop)) {
1992 send_stop_cmd(host, data);
1994 mci_writel(host, CMDARG, 0);
1996 cmd_flags = SDMMC_CMD_STOP | SDMMC_CMD_RESP_CRC |
1997 SDMMC_CMD_RESP_EXP | MMC_STOP_TRANSMISSION;
1999 if (host->mmc->hold_reg_flag)
2000 cmd_flags |= SDMMC_CMD_USE_HOLD_REG;
2002 mci_writel(host, CMD, cmd_flags | SDMMC_CMD_START);
2004 timeout = jiffies + msecs_to_jiffies(500);
2006 while(ret_timeout) {
2007 ret_timeout = time_before(jiffies, timeout);
2008 if(!(mci_readl(host, CMD) & SDMMC_CMD_START))
2012 if (false == ret_timeout)
2013 MMC_DBG_ERR_FUNC(host->mmc, "stop recovery failed![%s]",
2014 mmc_hostname(host->mmc));
2017 if (!dw_mci_ctrl_all_reset(host)) {
2022 #ifdef CONFIG_MMC_DW_IDMAC
2023 if (host->use_dma && host->dma_ops->init)
2024 host->dma_ops->init(host);
2028 * Restore the initial value at FIFOTH register
2029 * And Invalidate the prev_blksz with zero
2031 mci_writel(host, FIFOTH, host->fifoth_val);
2032 host->prev_blksz = 0;
2033 mci_writel(host, TMOUT, 0xFFFFFFFF);
2034 mci_writel(host, RINTSTS, 0xFFFFFFFF);
2035 regs = SDMMC_INT_CMD_DONE | SDMMC_INT_DATA_OVER | SDMMC_INT_TXDR
2036 | SDMMC_INT_RXDR | SDMMC_INT_VSI | DW_MCI_ERROR_FLAGS;
2037 if (!(host->mmc->restrict_caps & RESTRICT_CARD_TYPE_SDIO))
2038 regs |= SDMMC_INT_CD;
2040 if ((host->mmc->restrict_caps & RESTRICT_CARD_TYPE_SDIO)) {
2041 if (host->verid < DW_MMC_240A)
2042 sdio_int = SDMMC_INT_SDIO(0);
2044 sdio_int = SDMMC_INT_SDIO(8);
2046 if (mci_readl(host, INTMASK) & sdio_int)
2050 mci_writel(host, INTMASK, regs);
2051 mci_writel(host, CTRL, SDMMC_CTRL_INT_ENABLE);
2052 for (i = 0; i < host->num_slots; i++) {
2053 struct dw_mci_slot *slot = host->slot[i];
2056 if (slot->mmc->pm_flags & MMC_PM_KEEP_POWER) {
2057 dw_mci_set_ios(slot->mmc, &slot->mmc->ios);
2058 dw_mci_setup_bus(slot, true);
2061 mci_writel(host, RINTSTS, 0xFFFFFFFF);
2063 printk("[%s] -- Timeout recovery procedure finished --\n",
2064 mmc_hostname(host->mmc));
2067 static const struct mmc_host_ops dw_mci_ops = {
2068 .request = dw_mci_request,
2069 .pre_req = dw_mci_pre_req,
2070 .post_req = dw_mci_post_req,
2071 .set_ios = dw_mci_set_ios,
2072 .get_ro = dw_mci_get_ro,
2073 .get_cd = dw_mci_get_cd,
2074 .set_sdio_status = dw_mci_set_sdio_status,
2075 .hw_reset = dw_mci_hw_reset,
2076 .enable_sdio_irq = dw_mci_enable_sdio_irq,
2077 .execute_tuning = dw_mci_execute_tuning,
2078 .post_tmo = dw_mci_post_tmo,
2079 #ifdef CONFIG_MMC_DW_ROCKCHIP_SWITCH_VOLTAGE
2080 .start_signal_voltage_switch
2081 = dw_mci_start_signal_voltage_switch,
2082 .card_busy = dw_mci_card_busy,
2086 static void dw_mci_deal_data_end(struct dw_mci *host, struct mmc_request *mrq)
2087 __releases(&host->lock)
2088 __acquires(&host->lock)
2090 if (DW_MCI_SEND_STATUS == host->dir_status){
2091 dw_mci_wait_unbusy(host);
2095 static void dw_mci_request_end(struct dw_mci *host, struct mmc_request *mrq)
2096 __releases(&host->lock)
2097 __acquires(&host->lock)
2099 struct dw_mci_slot *slot;
2100 struct mmc_host *prev_mmc = host->cur_slot->mmc;
2102 //WARN_ON(host->cmd || host->data);
2104 dw_mci_deal_data_end(host, mrq);
2107 MMC_DBG_CMD_FUNC(host->mmc,
2108 " reqeust end--reqeuest done, cmd=%d, "
2109 "cmderr=%d, host->state=%d [%s]",
2110 mrq->cmd->opcode, mrq->cmd->error,
2111 host->state,mmc_hostname(host->mmc));
2113 MMC_DBG_CMD_FUNC(host->mmc,
2114 " reqeust end--reqeuest done, cmd=%d, "
2115 "dataerr=%d, host->state=%d [%s]",
2116 mrq->cmd->opcode,mrq->data->error,
2117 host->state, mmc_hostname(host->mmc));
2119 host->cur_slot->mrq = NULL;
2121 if (!list_empty(&host->queue)) {
2122 slot = list_entry(host->queue.next,
2123 struct dw_mci_slot, queue_node);
2124 list_del(&slot->queue_node);
2125 dev_vdbg(host->dev, "list not empty: %s is next\n",
2126 mmc_hostname(slot->mmc));
2127 host->state = STATE_SENDING_CMD;
2128 MMC_DBG_CMD_FUNC(host->mmc,
2129 " list is not empty. run the request in list. [%s]",
2130 mmc_hostname(host->mmc));
2131 dw_mci_start_request(host, slot);
2133 dev_vdbg(host->dev, "list empty\n");
2134 host->state = STATE_IDLE;
2137 spin_unlock(&host->lock);
2138 mmc_request_done(prev_mmc, mrq);
2139 spin_lock(&host->lock);
2142 static void dw_mci_command_complete(struct dw_mci *host, struct mmc_command *cmd)
2144 u32 status = host->cmd_status;
2146 host->cmd_status = 0;
2148 /* Read the response from the card (up to 16 bytes) */
2149 if (cmd->flags & MMC_RSP_PRESENT) {
2150 if (cmd->flags & MMC_RSP_136) {
2151 cmd->resp[3] = mci_readl(host, RESP0);
2152 cmd->resp[2] = mci_readl(host, RESP1);
2153 cmd->resp[1] = mci_readl(host, RESP2);
2154 cmd->resp[0] = mci_readl(host, RESP3);
2156 MMC_DBG_INFO_FUNC(host->mmc,
2157 "Command complete cmd=%d, "
2158 "resp[3]=0x%x, resp[2]=0x%x,resp[1]=0x%x,resp[0]=0x%x.[%s]",
2159 cmd->opcode,cmd->resp[3], cmd->resp[2], cmd->resp[1], cmd->resp[0],
2160 mmc_hostname(host->mmc));
2162 cmd->resp[0] = mci_readl(host, RESP0);
2166 MMC_DBG_INFO_FUNC(host->mmc,
2167 "Command complete cmd=%d,resp[0]=0x%x. [%s]",
2168 cmd->opcode, cmd->resp[0], mmc_hostname(host->mmc));
2172 if (status & SDMMC_INT_RTO)
2174 if(host->mmc->restrict_caps & RESTRICT_CARD_TYPE_SDIO)
2177 cmd->error = -ETIMEDOUT;
2178 }else if ((cmd->flags & MMC_RSP_CRC) && (status & SDMMC_INT_RCRC)){
2179 cmd->error = -EILSEQ;
2180 }else if (status & SDMMC_INT_RESP_ERR){
2186 MMC_DBG_CMD_FUNC(host->mmc,
2187 "Command complete, cmd=%d,cmdError=%d [%s]",
2188 cmd->opcode, cmd->error, mmc_hostname(host->mmc));
2191 if(MMC_SEND_STATUS != cmd->opcode)
2192 if(host->cmd_rto >= SDMMC_CMD_RTO_MAX_HOLD){
2193 MMC_DBG_CMD_FUNC(host->mmc,
2194 "Command complete, cmd=%d,cmdError=%d [%s]",
2195 cmd->opcode, cmd->error, mmc_hostname(host->mmc));
2199 /* newer ip versions need a delay between retries */
2200 if (host->quirks & DW_MCI_QUIRK_RETRY_DELAY)
2205 static void dw_mci_tasklet_func(unsigned long priv)
2207 struct dw_mci *host = (struct dw_mci *)priv;
2208 struct dw_mci_slot *slot = mmc_priv(host->mmc);
2209 struct mmc_data *data;
2210 struct mmc_command *cmd;
2211 enum dw_mci_state state;
2212 enum dw_mci_state prev_state;
2213 u32 status, cmd_flags;
2214 unsigned long timeout = 0;
2217 spin_lock(&host->lock);
2219 state = host->state;
2229 case STATE_SENDING_CMD:
2230 if (!test_and_clear_bit(EVENT_CMD_COMPLETE,
2231 &host->pending_events))
2236 set_bit(EVENT_CMD_COMPLETE, &host->completed_events);
2237 dw_mci_command_complete(host, cmd);
2238 if (cmd == host->mrq->sbc && !cmd->error) {
2239 prev_state = state = STATE_SENDING_CMD;
2240 __dw_mci_start_request(host, host->cur_slot,
2245 if (cmd->data && cmd->error) {
2246 dw_mci_stop_dma(host);
2249 send_stop_cmd(host, data);
2250 state = STATE_SENDING_STOP;
2253 /* host->data = NULL; */
2256 send_stop_abort(host, data);
2257 state = STATE_SENDING_STOP;
2260 set_bit(EVENT_DATA_COMPLETE, &host->completed_events);
2263 if (!host->mrq->data || cmd->error) {
2264 dw_mci_request_end(host, host->mrq);
2268 prev_state = state = STATE_SENDING_DATA;
2271 case STATE_SENDING_DATA:
2272 if (test_and_clear_bit(EVENT_DATA_ERROR,
2273 &host->pending_events)) {
2274 dw_mci_stop_dma(host);
2277 send_stop_cmd(host, data);
2279 /*single block read/write, send stop cmd
2280 manually to prevent host controller halt*/
2281 MMC_DBG_INFO_FUNC(host->mmc,
2282 "%s status 1 0x%08x [%s]\n",
2283 __func__, mci_readl(host, STATUS),
2284 mmc_hostname(host->mmc));
2286 mci_writel(host, CMDARG, 0);
2288 cmd_flags = SDMMC_CMD_STOP | SDMMC_CMD_RESP_CRC |
2289 SDMMC_CMD_RESP_EXP | MMC_STOP_TRANSMISSION;
2291 if(host->mmc->hold_reg_flag)
2292 cmd_flags |= SDMMC_CMD_USE_HOLD_REG;
2294 mci_writel(host, CMD, cmd_flags | SDMMC_CMD_START);
2296 timeout = jiffies + msecs_to_jiffies(500);
2299 ret = time_before(jiffies, timeout);
2300 if (!(mci_readl(host, CMD) & SDMMC_CMD_START))
2304 MMC_DBG_ERR_FUNC(host->mmc,
2305 "%s EVENT_DATA_ERROR recovery failed!!! [%s]\n",
2306 __func__, mmc_hostname(host->mmc));
2309 send_stop_abort(host, data);
2311 state = STATE_DATA_ERROR;
2315 MMC_DBG_CMD_FUNC(host->mmc,
2316 "Pre-state[%d]-->NowState[%d]: STATE_SENDING_DATA, "
2317 "wait for EVENT_XFER_COMPLETE.[%s]",
2318 prev_state, state, mmc_hostname(host->mmc));
2320 if (!test_and_clear_bit(EVENT_XFER_COMPLETE,
2321 &host->pending_events))
2323 MMC_DBG_INFO_FUNC(host->mmc,
2324 "Pre-state[%d]-->NowState[%d]: STATE_SENDING_DATA, "
2325 "wait for EVENT_DATA_COMPLETE. [%s]",
2326 prev_state, state, mmc_hostname(host->mmc));
2328 set_bit(EVENT_XFER_COMPLETE, &host->completed_events);
2329 prev_state = state = STATE_DATA_BUSY;
2332 case STATE_DATA_BUSY:
2333 if (!test_and_clear_bit(EVENT_DATA_COMPLETE,
2334 &host->pending_events))
2337 dw_mci_deal_data_end(host, host->mrq);
2338 MMC_DBG_INFO_FUNC(host->mmc,
2339 "Pre-state[%d]-->NowState[%d]: STATE_DATA_BUSY, "
2340 "after EVENT_DATA_COMPLETE. [%s]",
2341 prev_state, state, mmc_hostname(host->mmc));
2343 /* host->data = NULL; */
2344 set_bit(EVENT_DATA_COMPLETE, &host->completed_events);
2345 status = host->data_status;
2347 if (status & DW_MCI_DATA_ERROR_FLAGS) {
2348 if((SDMMC_CTYPE_1BIT != slot->ctype) &&
2349 (MMC_SEND_EXT_CSD == host->mrq->cmd->opcode))
2350 MMC_DBG_ERR_FUNC(host->mmc,
2351 "Pre-state[%d]-->NowState[%d]: DW_MCI_DATA_ERROR_FLAGS,"
2352 "datastatus=0x%x [%s]",
2353 prev_state, state, status, mmc_hostname(host->mmc));
2355 if (status & SDMMC_INT_DRTO) {
2356 data->error = -ETIMEDOUT;
2357 } else if (status & SDMMC_INT_DCRC) {
2358 data->error = -EILSEQ;
2359 } else if (status & SDMMC_INT_EBE &&
2360 host->dir_status == DW_MCI_SEND_STATUS){
2362 * No data CRC status was returned.
2363 * The number of bytes transferred will
2364 * be exaggerated in PIO mode.
2366 data->bytes_xfered = 0;
2367 data->error = -ETIMEDOUT;
2376 * After an error, there may be data lingering
2377 * in the FIFO, so reset it - doing so
2378 * generates a block interrupt, hence setting
2379 * the scatter-gather pointer to NULL.
2381 dw_mci_fifo_reset(host);
2383 data->bytes_xfered = data->blocks * data->blksz;
2388 MMC_DBG_CMD_FUNC(host->mmc,
2389 "Pre-state[%d]-->NowState[%d]: "
2390 "no stop and no dataerr, exit [%s]",
2391 prev_state, state, mmc_hostname(host->mmc));
2392 dw_mci_request_end(host, host->mrq);
2395 MMC_DBG_CMD_FUNC(host->mmc,
2396 "Pre-state[%d]-->NowState[%d]: begin to stop [%s]",
2397 prev_state, state, mmc_hostname(host->mmc));
2399 if (host->mrq->sbc && !data->error) {
2400 data->stop->error = 0;
2402 MMC_DBG_CMD_FUNC(host->mmc,
2403 "Pre-state[%d]-->NowState[%d]: have stop and sbc, exit [%s]",
2404 prev_state,state,mmc_hostname(host->mmc));
2406 dw_mci_request_end(host, host->mrq);
2410 prev_state = state = STATE_SENDING_STOP;
2412 send_stop_cmd(host, data);
2414 MMC_DBG_CMD_FUNC(host->mmc,
2415 "Pre-state[%d]-->NowState[%d]: begin to STATE_SENDING_STOP [%s]",
2416 prev_state, state, mmc_hostname(host->mmc));
2418 case STATE_SENDING_STOP:
2419 if (!test_and_clear_bit(EVENT_CMD_COMPLETE,
2420 &host->pending_events))
2423 MMC_DBG_CMD_FUNC(host->mmc,
2424 "Pre-state[%d]-->NowState[%d]: begin to send cmd12 [%s]",
2425 prev_state, state, mmc_hostname(host->mmc));
2427 /* CMD error in data command */
2428 if (host->mrq->cmd->error && host->mrq->data) {
2429 dw_mci_fifo_reset(host);
2437 dw_mci_command_complete(host, host->mrq->stop);
2439 if (host->mrq->stop)
2440 dw_mci_command_complete(host, host->mrq->stop);
2442 host->cmd_status = 0;
2445 dw_mci_request_end(host, host->mrq);
2448 case STATE_DATA_ERROR:
2449 if (!test_and_clear_bit(EVENT_XFER_COMPLETE,
2450 &host->pending_events))
2453 state = STATE_DATA_BUSY;
2456 } while (state != prev_state);
2458 host->state = state;
2460 spin_unlock(&host->lock);
2463 /* push final bytes to part_buf, only use during push */
2464 static void dw_mci_set_part_bytes(struct dw_mci *host, void *buf, int cnt)
2466 memcpy((void *)&host->part_buf, buf, cnt);
2467 host->part_buf_count = cnt;
2470 /* append bytes to part_buf, only use during push */
2471 static int dw_mci_push_part_bytes(struct dw_mci *host, void *buf, int cnt)
2473 cnt = min(cnt, (1 << host->data_shift) - host->part_buf_count);
2474 memcpy((void *)&host->part_buf + host->part_buf_count, buf, cnt);
2475 host->part_buf_count += cnt;
2479 /* pull first bytes from part_buf, only use during pull */
2480 static int dw_mci_pull_part_bytes(struct dw_mci *host, void *buf, int cnt)
2482 cnt = min(cnt, (int)host->part_buf_count);
2484 memcpy(buf, (void *)&host->part_buf + host->part_buf_start,
2486 host->part_buf_count -= cnt;
2487 host->part_buf_start += cnt;
2492 /* pull final bytes from the part_buf, assuming it's just been filled */
2493 static void dw_mci_pull_final_bytes(struct dw_mci *host, void *buf, int cnt)
2495 memcpy(buf, &host->part_buf, cnt);
2496 host->part_buf_start = cnt;
2497 host->part_buf_count = (1 << host->data_shift) - cnt;
2500 static void dw_mci_push_data16(struct dw_mci *host, void *buf, int cnt)
2502 struct mmc_data *data = host->data;
2505 /* try and push anything in the part_buf */
2506 if (unlikely(host->part_buf_count)) {
2507 int len = dw_mci_push_part_bytes(host, buf, cnt);
2510 if (host->part_buf_count == 2) {
2511 mci_writew(host, DATA(host->data_offset),
2513 host->part_buf_count = 0;
2516 #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
2517 if (unlikely((unsigned long)buf & 0x1)) {
2519 u16 aligned_buf[64];
2520 int len = min(cnt & -2, (int)sizeof(aligned_buf));
2521 int items = len >> 1;
2523 /* memcpy from input buffer into aligned buffer */
2524 memcpy(aligned_buf, buf, len);
2527 /* push data from aligned buffer into fifo */
2528 for (i = 0; i < items; ++i)
2529 mci_writew(host, DATA(host->data_offset),
2536 for (; cnt >= 2; cnt -= 2)
2537 mci_writew(host, DATA(host->data_offset), *pdata++);
2540 /* put anything remaining in the part_buf */
2542 dw_mci_set_part_bytes(host, buf, cnt);
2543 /* Push data if we have reached the expected data length */
2544 if ((data->bytes_xfered + init_cnt) ==
2545 (data->blksz * data->blocks))
2546 mci_writew(host, DATA(host->data_offset),
2551 static void dw_mci_pull_data16(struct dw_mci *host, void *buf, int cnt)
2553 #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
2554 if (unlikely((unsigned long)buf & 0x1)) {
2556 /* pull data from fifo into aligned buffer */
2557 u16 aligned_buf[64];
2558 int len = min(cnt & -2, (int)sizeof(aligned_buf));
2559 int items = len >> 1;
2561 for (i = 0; i < items; ++i)
2562 aligned_buf[i] = mci_readw(host,
2563 DATA(host->data_offset));
2564 /* memcpy from aligned buffer into output buffer */
2565 memcpy(buf, aligned_buf, len);
2573 for (; cnt >= 2; cnt -= 2)
2574 *pdata++ = mci_readw(host, DATA(host->data_offset));
2578 host->part_buf16 = mci_readw(host, DATA(host->data_offset));
2579 dw_mci_pull_final_bytes(host, buf, cnt);
2583 static void dw_mci_push_data32(struct dw_mci *host, void *buf, int cnt)
2585 struct mmc_data *data = host->data;
2588 /* try and push anything in the part_buf */
2589 if (unlikely(host->part_buf_count)) {
2590 int len = dw_mci_push_part_bytes(host, buf, cnt);
2593 if (host->part_buf_count == 4) {
2594 mci_writel(host, DATA(host->data_offset),
2596 host->part_buf_count = 0;
2599 #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
2600 if (unlikely((unsigned long)buf & 0x3)) {
2602 u32 aligned_buf[32];
2603 int len = min(cnt & -4, (int)sizeof(aligned_buf));
2604 int items = len >> 2;
2606 /* memcpy from input buffer into aligned buffer */
2607 memcpy(aligned_buf, buf, len);
2610 /* push data from aligned buffer into fifo */
2611 for (i = 0; i < items; ++i)
2612 mci_writel(host, DATA(host->data_offset),
2619 for (; cnt >= 4; cnt -= 4)
2620 mci_writel(host, DATA(host->data_offset), *pdata++);
2623 /* put anything remaining in the part_buf */
2625 dw_mci_set_part_bytes(host, buf, cnt);
2626 /* Push data if we have reached the expected data length */
2627 if ((data->bytes_xfered + init_cnt) ==
2628 (data->blksz * data->blocks))
2629 mci_writel(host, DATA(host->data_offset),
2634 static void dw_mci_pull_data32(struct dw_mci *host, void *buf, int cnt)
2636 #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
2637 if (unlikely((unsigned long)buf & 0x3)) {
2639 /* pull data from fifo into aligned buffer */
2640 u32 aligned_buf[32];
2641 int len = min(cnt & -4, (int)sizeof(aligned_buf));
2642 int items = len >> 2;
2644 for (i = 0; i < items; ++i)
2645 aligned_buf[i] = mci_readl(host,
2646 DATA(host->data_offset));
2647 /* memcpy from aligned buffer into output buffer */
2648 memcpy(buf, aligned_buf, len);
2656 for (; cnt >= 4; cnt -= 4)
2657 *pdata++ = mci_readl(host, DATA(host->data_offset));
2661 host->part_buf32 = mci_readl(host, DATA(host->data_offset));
2662 dw_mci_pull_final_bytes(host, buf, cnt);
2666 static void dw_mci_push_data64(struct dw_mci *host, void *buf, int cnt)
2668 struct mmc_data *data = host->data;
2671 /* try and push anything in the part_buf */
2672 if (unlikely(host->part_buf_count)) {
2673 int len = dw_mci_push_part_bytes(host, buf, cnt);
2677 if (host->part_buf_count == 8) {
2678 mci_writeq(host, DATA(host->data_offset),
2680 host->part_buf_count = 0;
2683 #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
2684 if (unlikely((unsigned long)buf & 0x7)) {
2686 u64 aligned_buf[16];
2687 int len = min(cnt & -8, (int)sizeof(aligned_buf));
2688 int items = len >> 3;
2690 /* memcpy from input buffer into aligned buffer */
2691 memcpy(aligned_buf, buf, len);
2694 /* push data from aligned buffer into fifo */
2695 for (i = 0; i < items; ++i)
2696 mci_writeq(host, DATA(host->data_offset),
2703 for (; cnt >= 8; cnt -= 8)
2704 mci_writeq(host, DATA(host->data_offset), *pdata++);
2707 /* put anything remaining in the part_buf */
2709 dw_mci_set_part_bytes(host, buf, cnt);
2710 /* Push data if we have reached the expected data length */
2711 if ((data->bytes_xfered + init_cnt) ==
2712 (data->blksz * data->blocks))
2713 mci_writeq(host, DATA(host->data_offset),
2718 static void dw_mci_pull_data64(struct dw_mci *host, void *buf, int cnt)
2720 #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
2721 if (unlikely((unsigned long)buf & 0x7)) {
2723 /* pull data from fifo into aligned buffer */
2724 u64 aligned_buf[16];
2725 int len = min(cnt & -8, (int)sizeof(aligned_buf));
2726 int items = len >> 3;
2728 for (i = 0; i < items; ++i)
2729 aligned_buf[i] = mci_readq(host,
2730 DATA(host->data_offset));
2731 /* memcpy from aligned buffer into output buffer */
2732 memcpy(buf, aligned_buf, len);
2740 for (; cnt >= 8; cnt -= 8)
2741 *pdata++ = mci_readq(host, DATA(host->data_offset));
2745 host->part_buf = mci_readq(host, DATA(host->data_offset));
2746 dw_mci_pull_final_bytes(host, buf, cnt);
2750 static void dw_mci_pull_data(struct dw_mci *host, void *buf, int cnt)
2754 /* get remaining partial bytes */
2755 len = dw_mci_pull_part_bytes(host, buf, cnt);
2756 if (unlikely(len == cnt))
2761 /* get the rest of the data */
2762 host->pull_data(host, buf, cnt);
2765 static void dw_mci_read_data_pio(struct dw_mci *host, bool dto)
2767 struct sg_mapping_iter *sg_miter = &host->sg_miter;
2769 unsigned int offset;
2770 struct mmc_data *data = host->data;
2771 int shift = host->data_shift;
2774 unsigned int remain, fcnt;
2776 if(!host->mmc->bus_refs){
2777 printk("Note: %s host->mmc->bus_refs is 0!!!\n", __func__);
2781 if (!sg_miter_next(sg_miter))
2784 host->sg = sg_miter->piter.sg;
2785 buf = sg_miter->addr;
2786 remain = sg_miter->length;
2790 fcnt = (SDMMC_GET_FCNT(mci_readl(host, STATUS))
2791 << shift) + host->part_buf_count;
2792 len = min(remain, fcnt);
2795 dw_mci_pull_data(host, (void *)(buf + offset), len);
2796 data->bytes_xfered += len;
2801 sg_miter->consumed = offset;
2802 status = mci_readl(host, MINTSTS);
2803 mci_writel(host, RINTSTS, SDMMC_INT_RXDR);
2804 /* if the RXDR is ready read again */
2805 } while ((status & SDMMC_INT_RXDR) ||
2806 (dto && SDMMC_GET_FCNT(mci_readl(host, STATUS))));
2809 if (!sg_miter_next(sg_miter))
2811 sg_miter->consumed = 0;
2813 sg_miter_stop(sg_miter);
2817 sg_miter_stop(sg_miter);
2821 set_bit(EVENT_XFER_COMPLETE, &host->pending_events);
2824 static void dw_mci_write_data_pio(struct dw_mci *host)
2826 struct sg_mapping_iter *sg_miter = &host->sg_miter;
2828 unsigned int offset;
2829 struct mmc_data *data = host->data;
2830 int shift = host->data_shift;
2833 unsigned int fifo_depth = host->fifo_depth;
2834 unsigned int remain, fcnt;
2836 if (!host->mmc->bus_refs){
2837 printk("Note: %s host->mmc->bus_refs is 0!!!\n", __func__);
2842 if (!sg_miter_next(sg_miter))
2845 host->sg = sg_miter->piter.sg;
2846 buf = sg_miter->addr;
2847 remain = sg_miter->length;
2851 fcnt = ((fifo_depth -
2852 SDMMC_GET_FCNT(mci_readl(host, STATUS)))
2853 << shift) - host->part_buf_count;
2854 len = min(remain, fcnt);
2857 host->push_data(host, (void *)(buf + offset), len);
2858 data->bytes_xfered += len;
2863 sg_miter->consumed = offset;
2864 status = mci_readl(host, MINTSTS);
2865 mci_writel(host, RINTSTS, SDMMC_INT_TXDR);
2866 } while (status & SDMMC_INT_TXDR); /* if TXDR write again */
2869 if (!sg_miter_next(sg_miter))
2871 sg_miter->consumed = 0;
2873 sg_miter_stop(sg_miter);
2877 sg_miter_stop(sg_miter);
2881 set_bit(EVENT_XFER_COMPLETE, &host->pending_events);
2884 static void dw_mci_cmd_interrupt(struct dw_mci *host, u32 status)
2886 if (!host->cmd_status)
2887 host->cmd_status = status;
2894 set_bit(EVENT_CMD_COMPLETE, &host->pending_events);
2895 tasklet_schedule(&host->tasklet);
2898 static irqreturn_t dw_mci_interrupt(int irq, void *dev_id)
2900 struct dw_mci *host = dev_id;
2901 u32 pending, sdio_int;
2904 pending = mci_readl(host, MINTSTS); /* read-only mask reg */
2907 * DTO fix - version 2.10a and below, and only if internal DMA
2910 if (host->quirks & DW_MCI_QUIRK_IDMAC_DTO) {
2912 ((mci_readl(host, STATUS) >> 17) & 0x1fff))
2913 pending |= SDMMC_INT_DATA_OVER;
2917 if (pending & DW_MCI_CMD_ERROR_FLAGS) {
2918 mci_writel(host, RINTSTS, DW_MCI_CMD_ERROR_FLAGS);
2919 host->cmd_status = pending;
2921 MMC_DBG_INFO_FUNC(host->mmc,
2922 "%s cmd_status INT=0x%x,[%s]",
2923 __FUNCTION__, host->cmd_status,
2924 mmc_hostname(host->mmc));
2926 set_bit(EVENT_CMD_COMPLETE, &host->pending_events);
2929 if (pending & DW_MCI_DATA_ERROR_FLAGS) {
2930 /* if there is an error report DATA_ERROR */
2931 mci_writel(host, RINTSTS, DW_MCI_DATA_ERROR_FLAGS);
2932 host->data_status = pending;
2934 set_bit(EVENT_DATA_ERROR, &host->pending_events);
2936 MMC_DBG_INFO_FUNC(host->mmc,
2937 "%s data_status INT=0x%x,[%s]",
2938 __FUNCTION__, host->data_status,
2939 mmc_hostname(host->mmc));
2940 tasklet_schedule(&host->tasklet);
2943 if (pending & SDMMC_INT_DATA_OVER) {
2944 mci_writel(host, RINTSTS, SDMMC_INT_DATA_OVER);
2945 MMC_DBG_CMD_FUNC(host->mmc,
2946 "SDMMC_INT_DATA_OVER, INT-pending=0x%x [%s]",
2947 pending, mmc_hostname(host->mmc));
2948 if (!host->data_status)
2949 host->data_status = pending;
2951 if (host->dir_status == DW_MCI_RECV_STATUS) {
2952 if (host->sg != NULL)
2953 dw_mci_read_data_pio(host, true);
2955 set_bit(EVENT_DATA_COMPLETE, &host->pending_events);
2956 tasklet_schedule(&host->tasklet);
2959 if (pending & SDMMC_INT_RXDR) {
2960 mci_writel(host, RINTSTS, SDMMC_INT_RXDR);
2961 if (host->dir_status == DW_MCI_RECV_STATUS && host->sg)
2962 dw_mci_read_data_pio(host, false);
2965 if (pending & SDMMC_INT_TXDR) {
2966 mci_writel(host, RINTSTS, SDMMC_INT_TXDR);
2967 if (host->dir_status == DW_MCI_SEND_STATUS && host->sg)
2968 dw_mci_write_data_pio(host);
2971 if (pending & SDMMC_INT_VSI) {
2972 MMC_DBG_SW_VOL_FUNC(host->mmc,
2973 "SDMMC_INT_VSI, INT-pending=0x%x. [%s]",
2974 pending, mmc_hostname(host->mmc));
2975 mci_writel(host, RINTSTS, SDMMC_INT_VSI);
2976 dw_mci_cmd_interrupt(host, pending);
2979 if (pending & SDMMC_INT_CMD_DONE) {
2980 MMC_DBG_CMD_FUNC(host->mmc,
2981 "SDMMC_INT_CMD_DONE, CMD = 0x%x, INT-pending=0x%x. [%s]",
2982 mci_readl(host, CMD), pending, mmc_hostname(host->mmc));
2983 mci_writel(host, RINTSTS, SDMMC_INT_CMD_DONE);
2984 dw_mci_cmd_interrupt(host, pending);
2987 if (pending & SDMMC_INT_CD) {
2988 mci_writel(host, RINTSTS, SDMMC_INT_CD);
2989 MMC_DBG_INFO_FUNC(host->mmc,
2990 "SDMMC_INT_CD, INT-pending=0x%x. [%s]",
2991 pending, mmc_hostname(host->mmc));
2992 wake_lock_timeout(&host->mmc->detect_wake_lock, 5 * HZ);
2993 queue_work(host->card_workqueue, &host->card_work);
2996 if (pending & SDMMC_INT_HLE) {
2997 mci_writel(host, RINTSTS, SDMMC_INT_HLE);
2998 MMC_DBG_CMD_FUNC(host->mmc,
2999 "SDMMC_INT_HLE INT-pending=0x%x. [%s]\n",
3000 pending, mmc_hostname(host->mmc));
3004 /* Handle SDIO Interrupts */
3005 for (i = 0; i < host->num_slots; i++) {
3006 struct dw_mci_slot *slot = host->slot[i];
3008 if (host->verid < DW_MMC_240A)
3009 sdio_int = SDMMC_INT_SDIO(i);
3011 sdio_int = SDMMC_INT_SDIO(i + 8);
3013 if (pending & sdio_int) {
3014 mci_writel(host, RINTSTS, sdio_int);
3015 mmc_signal_sdio_irq(slot->mmc);
3021 #ifdef CONFIG_MMC_DW_IDMAC
3022 /* External DMA Soc platform NOT need to ack interrupt IDSTS */
3023 if (!(cpu_is_rk3036() || cpu_is_rk312x())){
3024 /* Handle DMA interrupts */
3025 pending = mci_readl(host, IDSTS);
3026 if (pending & (SDMMC_IDMAC_INT_TI | SDMMC_IDMAC_INT_RI)) {
3027 mci_writel(host, IDSTS, SDMMC_IDMAC_INT_TI | SDMMC_IDMAC_INT_RI);
3028 mci_writel(host, IDSTS, SDMMC_IDMAC_INT_NI);
3029 host->dma_ops->complete((void *)host);
3037 static void dw_mci_work_routine_card(struct work_struct *work)
3039 struct dw_mci *host = container_of(work, struct dw_mci, card_work);
3042 for (i = 0; i < host->num_slots; i++) {
3043 struct dw_mci_slot *slot = host->slot[i];
3044 struct mmc_host *mmc = slot->mmc;
3045 struct mmc_request *mrq;
3048 present = dw_mci_get_cd(mmc);
3050 /* Card insert, switch data line to uart function, and vice verse.
3051 ONLY audi chip need switched by software, using udbg tag in dts!
3053 if (!(IS_ERR(host->pins_udbg)) &&
3054 !(IS_ERR(host->pins_default))) {
3056 if (pinctrl_select_state(host->pinctrl,
3057 host->pins_default) < 0)
3059 "%s: Default pinctrl setting failed!\n",
3060 mmc_hostname(host->mmc));
3062 if (pinctrl_select_state(host->pinctrl,
3063 host->pins_udbg) < 0)
3065 "%s: Udbg pinctrl setting failed!\n",
3066 mmc_hostname(host->mmc));
3070 while (present != slot->last_detect_state) {
3071 dev_dbg(&slot->mmc->class_dev, "card %s\n",
3072 present ? "inserted" : "removed");
3073 MMC_DBG_BOOT_FUNC(mmc, " The card is %s. ===!!!!!!==[%s]\n",
3074 present ? "inserted" : "removed.", mmc_hostname(mmc));
3076 dw_mci_ctrl_all_reset(host);
3077 /* Stop edma when rountine card triggered */
3078 if(cpu_is_rk3036() || cpu_is_rk312x())
3079 if(host->dma_ops && host->dma_ops->stop)
3080 host->dma_ops->stop(host);
3081 rk_send_wakeup_key();//wake up system
3082 spin_lock_bh(&host->lock);
3084 /* Card change detected */
3085 slot->last_detect_state = present;
3087 /* Clean up queue if present */
3090 if (mrq == host->mrq) {
3094 switch (host->state) {
3097 case STATE_SENDING_CMD:
3098 mrq->cmd->error = -ENOMEDIUM;
3102 case STATE_SENDING_DATA:
3103 mrq->data->error = -ENOMEDIUM;
3104 dw_mci_stop_dma(host);
3106 case STATE_DATA_BUSY:
3107 case STATE_DATA_ERROR:
3108 if (mrq->data->error == -EINPROGRESS)
3109 mrq->data->error = -ENOMEDIUM;
3113 case STATE_SENDING_STOP:
3114 mrq->stop->error = -ENOMEDIUM;
3118 dw_mci_request_end(host, mrq);
3120 list_del(&slot->queue_node);
3121 mrq->cmd->error = -ENOMEDIUM;
3123 mrq->data->error = -ENOMEDIUM;
3125 mrq->stop->error = -ENOMEDIUM;
3127 MMC_DBG_CMD_FUNC(host->mmc,
3128 "dw_mci_work--reqeuest done, cmd=%d [%s]",
3129 mrq->cmd->opcode, mmc_hostname(mmc));
3131 spin_unlock(&host->lock);
3132 mmc_request_done(slot->mmc, mrq);
3133 spin_lock(&host->lock);
3137 /* Power down slot */
3139 /* Clear down the FIFO */
3140 dw_mci_fifo_reset(host);
3141 #ifdef CONFIG_MMC_DW_IDMAC
3142 if (!(cpu_is_rk3036() || cpu_is_rk312x()))
3143 dw_mci_idmac_reset(host);
3147 spin_unlock_bh(&host->lock);
3149 present = dw_mci_get_cd(mmc);
3152 mmc_detect_change(slot->mmc,
3153 msecs_to_jiffies(host->pdata->detect_delay_ms));
3158 /* given a slot id, find out the device node representing that slot */
3159 static struct device_node *dw_mci_of_find_slot_node(struct device *dev, u8 slot)
3161 struct device_node *np;
3165 if (!dev || !dev->of_node)
3168 for_each_child_of_node(dev->of_node, np) {
3169 addr = of_get_property(np, "reg", &len);
3170 if (!addr || (len < sizeof(int)))
3172 if (be32_to_cpup(addr) == slot)
3178 static struct dw_mci_of_slot_quirks {
3181 } of_slot_quirks[] = {
3183 .quirk = "disable-wp",
3184 .id = DW_MCI_SLOT_QUIRK_NO_WRITE_PROTECT,
3188 static int dw_mci_of_get_slot_quirks(struct device *dev, u8 slot)
3190 struct device_node *np = dw_mci_of_find_slot_node(dev, slot);
3195 for (idx = 0; idx < ARRAY_SIZE(of_slot_quirks); idx++)
3196 if (of_get_property(np, of_slot_quirks[idx].quirk, NULL))
3197 quirks |= of_slot_quirks[idx].id;
3202 /* find out bus-width for a given slot */
3203 static u32 dw_mci_of_get_bus_wd(struct device *dev, u8 slot)
3205 struct device_node *np = dev->of_node;//dw_mci_of_find_slot_node(dev, slot);
3211 if (of_property_read_u32(np, "bus-width", &bus_wd))
3212 dev_err(dev, "bus-width property not found, assuming width"
3218 /* find the pwr-en gpio for a given slot; or -1 if none specified */
3219 static int dw_mci_of_get_pwr_en_gpio(struct device *dev, u8 slot)
3221 struct device_node *np = dev->of_node;//dw_mci_of_find_slot_node(dev, slot);
3227 gpio = of_get_named_gpio(np, "pwr-gpios", 0);
3229 /* Having a missing entry is valid; return silently */
3230 if (!gpio_is_valid(gpio))
3233 if (devm_gpio_request(dev, gpio, "dw-mci-pwr_en")) {
3234 dev_warn(dev, "gpio [%d] request failed\n", gpio);
3238 gpio_direction_output(gpio, 0);//set 0 to pwr-en
3244 /* find the write protect gpio for a given slot; or -1 if none specified */
3245 static int dw_mci_of_get_wp_gpio(struct device *dev, u8 slot)
3247 struct device_node *np = dw_mci_of_find_slot_node(dev, slot);
3253 gpio = of_get_named_gpio(np, "wp-gpios", 0);
3255 /* Having a missing entry is valid; return silently */
3256 if (!gpio_is_valid(gpio))
3259 if (devm_gpio_request(dev, gpio, "dw-mci-wp")) {
3260 dev_warn(dev, "gpio [%d] request failed\n", gpio);
3267 /* find the cd gpio for a given slot */
3268 static void dw_mci_of_get_cd_gpio(struct device *dev, u8 slot,
3269 struct mmc_host *mmc)
3271 struct device_node *np = dev->of_node;//dw_mci_of_find_slot_node(dev, slot);
3277 gpio = of_get_named_gpio(np, "cd-gpios", 0);
3279 /* Having a missing entry is valid; return silently */
3280 if (!gpio_is_valid(gpio))
3283 if (mmc_gpio_request_cd(mmc, gpio, 0))
3284 dev_warn(dev, "gpio [%d] request failed\n", gpio);
3287 static irqreturn_t dw_mci_gpio_cd_irqt(int irq, void *dev_id)
3289 struct mmc_host *mmc = dev_id;
3290 struct dw_mci_slot *slot = mmc_priv(mmc);
3291 struct dw_mci *host = slot->host;
3292 int gpio_cd = slot->cd_gpio;
3294 (gpio_get_value(gpio_cd) == 0) ?
3295 irq_set_irq_type(irq, IRQF_TRIGGER_HIGH | IRQF_ONESHOT) :
3296 irq_set_irq_type(irq, IRQF_TRIGGER_LOW | IRQF_ONESHOT);
3298 /* wakeup system whether gpio debounce or not */
3299 rk_send_wakeup_key();
3301 /* no need to trigger detect flow when rescan is disabled.
3302 This case happended in dpm, that we just wakeup system and
3303 let suspend_post notify callback handle it.
3305 if(mmc->rescan_disable == 0)
3306 queue_work(host->card_workqueue, &host->card_work);
3308 printk("%s: rescan been disabled!\n", __FUNCTION__);
3313 static void dw_mci_of_set_cd_gpio_irq(struct device *dev, u32 gpio,
3314 struct mmc_host *mmc)
3316 struct dw_mci_slot *slot = mmc_priv(mmc);
3317 struct dw_mci *host = slot->host;
3321 /* Having a missing entry is valid; return silently */
3322 if (!gpio_is_valid(gpio))
3325 irq = gpio_to_irq(gpio);
3327 ret = devm_request_threaded_irq(
3328 &mmc->class_dev, irq,
3329 NULL, dw_mci_gpio_cd_irqt,
3330 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
3335 "Request cd-gpio %d interrupt error!\n", gpio);
3337 /* enable wakeup event for gpio-cd in idle or deep suspend*/
3338 enable_irq_wake(irq);
3341 dev_err(host->dev, "Cannot convert gpio %d to irq!\n", gpio);
3345 static void dw_mci_of_free_cd_gpio_irq(struct device *dev, u32 gpio,
3346 struct mmc_host *mmc)
3348 if (!gpio_is_valid(gpio))
3351 if (gpio_to_irq(gpio) >= 0) {
3352 devm_free_irq(&mmc->class_dev, gpio_to_irq(gpio), mmc);
3353 devm_gpio_free(&mmc->class_dev, gpio);
3356 #else /* CONFIG_OF */
3357 static int dw_mci_of_get_slot_quirks(struct device *dev, u8 slot)
3361 static u32 dw_mci_of_get_bus_wd(struct device *dev, u8 slot)
3365 static struct device_node *dw_mci_of_find_slot_node(struct device *dev, u8 slot)
3369 static int dw_mci_of_get_wp_gpio(struct device *dev, u8 slot)
3373 static void dw_mci_of_get_cd_gpio(struct device *dev, u8 slot,
3374 struct mmc_host *mmc)
3378 #endif /* CONFIG_OF */
3380 /* @host: dw_mci host prvdata
3381 * Init pinctrl for each platform. Usually we assign
3382 * "defalut" tag for functional usage, "idle" tag for gpio
3383 * state and "udbg" tag for uart_dbg if any.
3385 static void dw_mci_init_pinctrl(struct dw_mci *host)
3387 /* Fixme: DON'T TOUCH EMMC SETTING! */
3388 if (host->mmc->restrict_caps & RESTRICT_CARD_TYPE_EMMC)
3391 /* Get pinctrl for DTS */
3392 host->pinctrl = devm_pinctrl_get(host->dev);
3393 if (IS_ERR(host->pinctrl)) {
3394 dev_err(host->dev, "%s: No pinctrl used!\n",
3395 mmc_hostname(host->mmc));
3399 /* Lookup idle state */
3401 pinctrl_lookup_state(host->pinctrl, PINCTRL_STATE_IDLE);
3402 if (IS_ERR(host->pins_idle)) {
3403 dev_err(host->dev, "%s: No idle tag found!\n",
3404 mmc_hostname(host->mmc));
3406 if (pinctrl_select_state(host->pinctrl, host->pins_idle) < 0)
3408 "%s: Idle pinctrl setting failed!\n",
3409 mmc_hostname(host->mmc));
3412 /* Lookup default state */
3413 host->pins_default =
3414 pinctrl_lookup_state(host->pinctrl, PINCTRL_STATE_DEFAULT);
3415 if (IS_ERR(host->pins_default)) {
3416 dev_err(host->dev, "%s: No default pinctrl found!\n",
3417 mmc_hostname(host->mmc));
3419 if (pinctrl_select_state(host->pinctrl, host->pins_default) < 0)
3421 "%s: Default pinctrl setting failed!\n",
3422 mmc_hostname(host->mmc));
3425 /* Sd card data0/1 may be used for uart_dbg, so were data2/3 for Jtag */
3426 if ((host->mmc->restrict_caps & RESTRICT_CARD_TYPE_SD)) {
3427 host->pins_udbg = pinctrl_lookup_state(host->pinctrl, "udbg");
3428 if (IS_ERR(host->pins_udbg)) {
3429 dev_warn(host->dev, "%s: No udbg pinctrl found!\n",
3430 mmc_hostname(host->mmc));
3432 if (!dw_mci_get_cd(host->mmc))
3433 if (pinctrl_select_state(host->pinctrl, host->pins_udbg) < 0)
3434 dev_err(host->dev, "%s: Udbg pinctrl setting failed!\n",
3435 mmc_hostname(host->mmc));
3440 static int dw_mci_pm_notify(struct notifier_block *notify_block,
3441 unsigned long mode, void *unused)
3443 struct mmc_host *host = container_of(
3444 notify_block, struct mmc_host, pm_notify);
3445 unsigned long flags;
3448 case PM_HIBERNATION_PREPARE:
3449 case PM_SUSPEND_PREPARE:
3450 dev_err(host->parent,
3451 "dw_mci_pm_notify: suspend prepare\n");
3452 spin_lock_irqsave(&host->lock, flags);
3453 host->rescan_disable = 1;
3454 spin_unlock_irqrestore(&host->lock, flags);
3455 if (cancel_delayed_work(&host->detect))
3456 wake_unlock(&host->detect_wake_lock);
3459 case PM_POST_SUSPEND:
3460 case PM_POST_HIBERNATION:
3461 case PM_POST_RESTORE:
3462 dev_err(host->parent,
3463 "dw_mci_pm_notify: post suspend\n");
3464 spin_lock_irqsave(&host->lock, flags);
3465 host->rescan_disable = 0;
3466 spin_unlock_irqrestore(&host->lock, flags);
3467 mmc_detect_change(host, 10);
3473 static int dw_mci_init_slot(struct dw_mci *host, unsigned int id)
3475 struct mmc_host *mmc;
3476 struct dw_mci_slot *slot;
3477 const struct dw_mci_drv_data *drv_data = host->drv_data;
3482 mmc = mmc_alloc_host(sizeof(struct dw_mci_slot), host->dev);
3486 slot = mmc_priv(mmc);
3490 host->slot[id] = slot;
3493 slot->quirks = dw_mci_of_get_slot_quirks(host->dev, slot->id);
3495 mmc->ops = &dw_mci_ops;
3497 if (of_property_read_u32_array(host->dev->of_node,
3498 "clock-freq-min-max", freq, 2)) {
3499 mmc->f_min = DW_MCI_FREQ_MIN;
3500 mmc->f_max = DW_MCI_FREQ_MAX;
3502 printk("%s: fmin=%d, fmax=%d [%s]\n",
3503 __FUNCTION__, mmc->f_min,
3504 mmc->f_max, mmc_hostname(mmc));
3506 mmc->f_min = freq[0];
3507 mmc->f_max = freq[1];
3509 printk("%s: fmin=%d, fmax=%d [%s]\n",
3510 __FUNCTION__, mmc->f_min,
3511 mmc->f_max, mmc_hostname(mmc));
3514 if (of_find_property(host->dev->of_node, "supports-sd", NULL))
3515 mmc->restrict_caps |= RESTRICT_CARD_TYPE_SD;
3516 if (of_find_property(host->dev->of_node, "supports-sdio", NULL))
3517 mmc->restrict_caps |= RESTRICT_CARD_TYPE_SDIO;
3518 if (of_find_property(host->dev->of_node, "supports-emmc", NULL))
3519 mmc->restrict_caps |= RESTRICT_CARD_TYPE_EMMC;
3521 if (mmc->restrict_caps & RESTRICT_CARD_TYPE_SD) {
3522 mmc->pm_notify.notifier_call = dw_mci_pm_notify;
3523 if (register_pm_notifier(&mmc->pm_notify)) {
3524 pr_err("dw_mci: register_pm_notifier failed\n");
3525 goto err_pm_notifier;
3529 if (host->cid == DW_MCI_TYPE_RK3368) {
3530 if (IS_ERR(host->grf))
3531 pr_err("rk_sdmmc: dts couldn't find grf regmap for 3368\n");
3533 /* Disable force_jtag */
3534 regmap_write(host->grf, 0x43c, (1<<13)<<16 | (0 << 13));
3535 } else if (cpu_is_rk3288()) {
3536 grf_writel(((1 << 12) << 16) | (0 << 12), RK3288_GRF_SOC_CON0);
3539 /* We assume only low-level chip use gpio_cd */
3540 if ((soc_is_rk3126() || soc_is_rk3126b() || soc_is_rk3036()) &&
3541 (host->mmc->restrict_caps & RESTRICT_CARD_TYPE_SD)) {
3542 slot->cd_gpio = of_get_named_gpio(host->dev->of_node, "cd-gpios", 0);
3543 if (gpio_is_valid(slot->cd_gpio)) {
3544 /* Request gpio int for card detection */
3545 dw_mci_of_set_cd_gpio_irq(host->dev, slot->cd_gpio,host->mmc);
3547 slot->cd_gpio = -ENODEV;
3548 dev_err(host->dev, "failed to get your cd-gpios!\n");
3552 if (host->pdata->get_ocr)
3553 mmc->ocr_avail = host->pdata->get_ocr(id);
3556 mmc->ocr_avail = MMC_VDD_27_28 | MMC_VDD_28_29 | MMC_VDD_29_30 |
3557 MMC_VDD_30_31 | MMC_VDD_31_32 | MMC_VDD_32_33|
3558 MMC_VDD_33_34 | MMC_VDD_34_35 | MMC_VDD_35_36 |
3559 MMC_VDD_26_27 | MMC_VDD_25_26 | MMC_VDD_24_25 |
3560 MMC_VDD_23_24 | MMC_VDD_22_23 | MMC_VDD_21_22 |
3561 MMC_VDD_20_21 | MMC_VDD_165_195;
3565 * Start with slot power disabled, it will be enabled when a card
3568 if (host->pdata->setpower)
3569 host->pdata->setpower(id, 0);
3571 if (host->pdata->caps)
3572 mmc->caps = host->pdata->caps;
3574 if (host->pdata->pm_caps)
3575 mmc->pm_caps = host->pdata->pm_caps;
3577 if (host->dev->of_node) {
3578 ctrl_id = of_alias_get_id(host->dev->of_node, "mshc");
3582 ctrl_id = to_platform_device(host->dev)->id;
3584 if (drv_data && drv_data->caps)
3585 mmc->caps |= drv_data->caps[ctrl_id];
3586 if (drv_data && drv_data->hold_reg_flag)
3587 mmc->hold_reg_flag |= drv_data->hold_reg_flag[ctrl_id];
3589 /* set the compatibility of driver. */
3590 mmc->caps |= MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 | MMC_CAP_UHS_SDR50
3591 | MMC_CAP_UHS_SDR104 | MMC_CAP_ERASE ;
3593 if (host->pdata->caps2)
3594 mmc->caps2 = host->pdata->caps2;
3596 if (host->pdata->get_bus_wd)
3597 bus_width = host->pdata->get_bus_wd(slot->id);
3598 else if (host->dev->of_node)
3599 bus_width = dw_mci_of_get_bus_wd(host->dev, slot->id);
3603 switch (bus_width) {
3605 mmc->caps |= MMC_CAP_8_BIT_DATA;
3607 mmc->caps |= MMC_CAP_4_BIT_DATA;
3610 if (of_find_property(host->dev->of_node, "cap-power-off-card", NULL))
3611 mmc->caps |= MMC_CAP_POWER_OFF_CARD;
3612 if (of_find_property(host->dev->of_node, "cap-sdio-irq", NULL))
3613 mmc->caps |= MMC_CAP_SDIO_IRQ;
3614 if (of_find_property(host->dev->of_node, "poll-hw-reset", NULL))
3615 mmc->caps |= MMC_CAP_HW_RESET;
3616 if (of_find_property(host->dev->of_node, "full-pwr-cycle", NULL))
3617 mmc->caps2 |= MMC_CAP2_FULL_PWR_CYCLE;
3618 if (of_find_property(host->dev->of_node, "keep-power-in-suspend", NULL))
3619 mmc->pm_caps |= MMC_PM_KEEP_POWER;
3620 if (of_find_property(host->dev->of_node, "ignore-pm-notify", NULL))
3621 mmc->pm_caps |= MMC_PM_IGNORE_PM_NOTIFY;
3622 if (of_find_property(host->dev->of_node, "enable-sdio-wakeup", NULL))
3623 mmc->pm_caps |= MMC_PM_WAKE_SDIO_IRQ;
3625 /*Assign pm_caps pass to pm_flags*/
3626 mmc->pm_flags = mmc->pm_caps;
3628 if (host->pdata->blk_settings) {
3629 mmc->max_segs = host->pdata->blk_settings->max_segs;
3630 mmc->max_blk_size = host->pdata->blk_settings->max_blk_size;
3631 mmc->max_blk_count = host->pdata->blk_settings->max_blk_count;
3632 mmc->max_req_size = host->pdata->blk_settings->max_req_size;
3633 mmc->max_seg_size = host->pdata->blk_settings->max_seg_size;
3635 /* Useful defaults if platform data is unset. */
3636 #ifdef CONFIG_MMC_DW_IDMAC
3637 mmc->max_segs = host->ring_size;
3638 mmc->max_blk_size = 65536;
3639 mmc->max_blk_count = host->ring_size;
3640 mmc->max_seg_size = 0x1000;
3641 mmc->max_req_size = mmc->max_seg_size * mmc->max_blk_count;
3642 if(cpu_is_rk3036() || cpu_is_rk312x()){
3643 /* fixup for external dmac setting */
3645 mmc->max_blk_size = 65536; /* BLKSIZ is 16 bits */
3646 mmc->max_blk_count = 65535;
3647 mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
3648 mmc->max_seg_size = mmc->max_req_size;
3652 mmc->max_blk_size = 65536; /* BLKSIZ is 16 bits */
3653 mmc->max_blk_count = 512;
3654 mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
3655 mmc->max_seg_size = mmc->max_req_size;
3656 #endif /* CONFIG_MMC_DW_IDMAC */
3660 slot->pwr_en_gpio = dw_mci_of_get_pwr_en_gpio(host->dev, slot->id);
3662 if (!(mmc->restrict_caps & RESTRICT_CARD_TYPE_SD))
3667 if(mmc->restrict_caps & RESTRICT_CARD_TYPE_SD)
3668 host->vmmc = devm_regulator_get(mmc_dev(mmc), "vmmc");
3672 if (IS_ERR(host->vmmc)) {
3673 pr_info("%s: no vmmc regulator found\n", mmc_hostname(mmc));
3676 ret = regulator_enable(host->vmmc);
3679 "failed to enable regulator: %d\n", ret);
3686 slot->wp_gpio = dw_mci_of_get_wp_gpio(host->dev, slot->id);
3688 if (mmc->restrict_caps & RESTRICT_CARD_TYPE_SDIO)
3689 clear_bit(DW_MMC_CARD_PRESENT, &slot->flags);
3691 dw_mci_init_pinctrl(host);
3692 ret = mmc_add_host(mmc);
3696 #if defined(CONFIG_DEBUG_FS)
3697 dw_mci_init_debugfs(slot);
3700 /* Card initially undetected */
3701 slot->last_detect_state = 1;
3705 unregister_pm_notifier(&mmc->pm_notify);
3708 if (gpio_is_valid(slot->cd_gpio))
3709 dw_mci_of_free_cd_gpio_irq(host->dev, slot->cd_gpio,host->mmc);
3714 static void dw_mci_cleanup_slot(struct dw_mci_slot *slot, unsigned int id)
3716 /* Shutdown detect IRQ */
3717 if (slot->host->pdata->exit)
3718 slot->host->pdata->exit(id);
3720 /* Debugfs stuff is cleaned up by mmc core */
3721 mmc_remove_host(slot->mmc);
3722 slot->host->slot[id] = NULL;
3723 mmc_free_host(slot->mmc);
3726 static void dw_mci_init_dma(struct dw_mci *host)
3728 /* Alloc memory for sg translation */
3729 host->sg_cpu = dmam_alloc_coherent(host->dev, PAGE_SIZE,
3730 &host->sg_dma, GFP_KERNEL);
3731 if (!host->sg_cpu) {
3732 dev_err(host->dev, "%s: could not alloc DMA memory\n",
3737 /* Determine which DMA interface to use */
3738 #if defined(CONFIG_MMC_DW_IDMAC)
3739 if(cpu_is_rk3036() || cpu_is_rk312x()){
3740 host->dma_ops = &dw_mci_edmac_ops;
3741 dev_info(host->dev, "Using external DMA controller.\n");
3743 host->dma_ops = &dw_mci_idmac_ops;
3744 dev_info(host->dev, "Using internal DMA controller.\n");
3751 if (host->dma_ops->init && host->dma_ops->start &&
3752 host->dma_ops->stop && host->dma_ops->cleanup) {
3753 if (host->dma_ops->init(host)) {
3754 dev_err(host->dev, "%s: Unable to initialize "
3755 "DMA Controller.\n", __func__);
3759 dev_err(host->dev, "DMA initialization not found.\n");
3767 dev_info(host->dev, "Using PIO mode.\n");
3772 static bool dw_mci_ctrl_reset(struct dw_mci *host, u32 reset)
3774 unsigned long timeout = jiffies + msecs_to_jiffies(500);
3777 ctrl = mci_readl(host, CTRL);
3779 mci_writel(host, CTRL, ctrl);
3781 /* wait till resets clear */
3783 ctrl = mci_readl(host, CTRL);
3784 if (!(ctrl & reset))
3786 } while (time_before(jiffies, timeout));
3789 "Timeout resetting block (ctrl reset %#x)\n",
3795 static inline bool dw_mci_fifo_reset(struct dw_mci *host)
3798 * Reseting generates a block interrupt, hence setting
3799 * the scatter-gather pointer to NULL.
3802 sg_miter_stop(&host->sg_miter);
3806 return dw_mci_ctrl_reset(host, SDMMC_CTRL_FIFO_RESET);
3809 static inline bool dw_mci_ctrl_all_reset(struct dw_mci *host)
3811 return dw_mci_ctrl_reset(host,
3812 SDMMC_CTRL_FIFO_RESET |
3814 SDMMC_CTRL_DMA_RESET);
3817 static void dw_mci_rst_pre_suspend(struct dw_mci *host)
3822 buffer = host->regs_buffer;
3824 for (index = 0; index < DW_REGS_NUM ; index++){
3825 *buffer = mci_readreg(host, index*4);
3826 MMC_DBG_INFO_FUNC(host->mmc, "[%s] :0x%08x.\n",
3827 dw_mci_regs[index].name, *buffer);
3831 *buffer = mci_readl(host,CDTHRCTL);
3832 MMC_DBG_INFO_FUNC(host->mmc, "[%s] :0x%08x.\n", "CARDTHRCTL", *buffer);
3835 static void dw_mci_rst_post_resume(struct dw_mci *host)
3840 buffer = host->regs_buffer;
3842 for (index = 0; index < DW_REGS_NUM; index++){
3843 mci_writereg(host, index*4, *buffer);
3846 mci_writel(host, CDTHRCTL, *buffer);
3849 static const struct dw_mci_rst_ops dw_mci_pdrst_ops = {
3850 .pre_suspend = dw_mci_rst_pre_suspend,
3851 .post_resume = dw_mci_rst_post_resume,
3856 static struct dw_mci_of_quirks {
3861 .quirk = "broken-cd",
3862 .id = DW_MCI_QUIRK_BROKEN_CARD_DETECTION,
3866 static struct dw_mci_board *dw_mci_parse_dt(struct dw_mci *host)
3868 struct dw_mci_board *pdata;
3869 struct device *dev = host->dev;
3870 struct device_node *np = dev->of_node;
3871 const struct dw_mci_drv_data *drv_data = host->drv_data;
3873 u32 clock_frequency;
3875 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
3877 dev_err(dev, "could not allocate memory for pdata\n");
3878 return ERR_PTR(-ENOMEM);
3881 /* find out number of slots supported */
3882 if (of_property_read_u32(dev->of_node, "num-slots",
3883 &pdata->num_slots)) {
3884 dev_info(dev, "num-slots property not found, "
3885 "assuming 1 slot is available\n");
3886 pdata->num_slots = 1;
3889 if (of_property_read_u32(np, "fifo-depth", &pdata->fifo_depth))
3890 dev_info(dev, "fifo-depth property not found, using "
3891 "value of FIFOTH register as default\n");
3893 of_property_read_u32(np, "card-detect-delay", &pdata->detect_delay_ms);
3895 if (!of_property_read_u32(np, "clock-frequency", &clock_frequency))
3896 pdata->bus_hz = clock_frequency;
3898 if (drv_data && drv_data->parse_dt) {
3899 ret = drv_data->parse_dt(host);
3901 return ERR_PTR(ret);
3904 if (of_find_property(np, "keep-power-in-suspend", NULL))
3905 pdata->pm_caps |= MMC_PM_KEEP_POWER;
3907 if (of_find_property(np, "enable-sdio-wakeup", NULL))
3908 pdata->pm_caps |= MMC_PM_WAKE_SDIO_IRQ;
3910 if (of_find_property(np, "supports-highspeed", NULL))
3911 pdata->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED;
3913 if (of_find_property(np, "supports-UHS_SDR104", NULL))
3914 pdata->caps |= MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_SDR50;
3916 if (of_find_property(np, "supports-DDR_MODE", NULL))
3917 pdata->caps |= MMC_CAP_1_8V_DDR | MMC_CAP_1_2V_DDR;
3919 if (of_find_property(np, "caps2-mmc-hs200", NULL))
3920 pdata->caps2 |= MMC_CAP2_HS200;
3922 if (of_find_property(np, "caps2-mmc-hs200-1_8v", NULL))
3923 pdata->caps2 |= MMC_CAP2_HS200_1_8V_SDR;
3925 if (of_find_property(np, "caps2-mmc-hs200-1_2v", NULL))
3926 pdata->caps2 |= MMC_CAP2_HS200_1_2V_SDR;
3928 if (of_get_property(np, "cd-inverted", NULL))
3929 pdata->caps2 |= MMC_CAP2_CD_ACTIVE_HIGH;
3930 if (of_get_property(np, "bootpart-no-access", NULL))
3931 pdata->caps2 |= MMC_CAP2_BOOTPART_NOACC;
3933 if (of_get_property(np, "controller-power-down", NULL)) {
3934 host->regs_buffer = (u32 *)devm_kzalloc(host->dev,
3935 DW_REGS_SIZE, GFP_KERNEL);
3936 if (!host->regs_buffer) {
3938 "could not allocate memory for regs_buffer\n");
3939 return ERR_PTR(-ENOMEM);
3942 host->rst_ops = &dw_mci_pdrst_ops;
3943 mmc_assume_removable = 0;
3949 #else /* CONFIG_OF */
3950 static struct dw_mci_board *dw_mci_parse_dt(struct dw_mci *host)
3952 return ERR_PTR(-EINVAL);
3954 #endif /* CONFIG_OF */
3956 int dw_mci_probe(struct dw_mci *host)
3958 const struct dw_mci_drv_data *drv_data = host->drv_data;
3959 int width, i, ret = 0;
3965 host->pdata = dw_mci_parse_dt(host);
3966 if (IS_ERR(host->pdata)) {
3967 dev_err(host->dev, "platform data not available\n");
3972 if (!host->pdata->select_slot && host->pdata->num_slots > 1) {
3974 "Platform data must supply select_slot function\n");
3979 * In 2.40a spec, Data offset is changed.
3980 * Need to check the version-id and set data-offset for DATA register.
3982 host->verid = SDMMC_GET_VERID(mci_readl(host, VERID));
3983 dev_info(host->dev, "Version ID is %04x\n", host->verid);
3985 if (host->verid < DW_MMC_240A)
3986 host->data_offset = DATA_OFFSET;
3988 host->data_offset = DATA_240A_OFFSET;
3991 host->hpclk_mmc= devm_clk_get(host->dev, "hpclk_mmc");
3992 if (IS_ERR(host->hpclk_mmc)) {
3993 dev_err(host->dev, "failed to get hpclk_mmc\n");
3995 clk_prepare_enable(host->hpclk_mmc);
3999 host->hclk_mmc= devm_clk_get(host->dev, "hclk_mmc");
4000 if (IS_ERR(host->hclk_mmc)) {
4001 dev_err(host->dev, "failed to get hclk_mmc\n");
4002 ret = PTR_ERR(host->hclk_mmc);
4006 clk_prepare_enable(host->hclk_mmc);
4009 host->clk_mmc = devm_clk_get(host->dev, "clk_mmc");
4010 if (IS_ERR(host->clk_mmc)) {
4011 dev_err(host->dev, "failed to get clk mmc_per\n");
4012 ret = PTR_ERR(host->clk_mmc);
4016 host->bus_hz = host->pdata->bus_hz;
4017 if (!host->bus_hz) {
4018 dev_err(host->dev, "Platform data must supply bus speed\n");
4023 if (host->verid < DW_MMC_240A)
4024 ret = clk_set_rate(host->clk_mmc, host->bus_hz);
4026 //rockchip: fix divider 2 in clksum before controlller
4027 ret = clk_set_rate(host->clk_mmc, host->bus_hz * 2);
4030 dev_err(host->dev, "failed to set clk mmc\n");
4033 clk_prepare_enable(host->clk_mmc);
4035 if (drv_data && drv_data->setup_clock) {
4036 ret = drv_data->setup_clock(host);
4039 "implementation specific clock setup failed\n");
4044 host->quirks = host->pdata->quirks;
4045 host->irq_state = true;
4046 host->set_speed = 0;
4048 host->svi_flags = 0;
4050 spin_lock_init(&host->lock);
4051 spin_lock_init(&host->slock);
4053 INIT_LIST_HEAD(&host->queue);
4055 * Get the host data width - this assumes that HCON has been set with
4056 * the correct values.
4058 i = (mci_readl(host, HCON) >> 7) & 0x7;
4060 host->push_data = dw_mci_push_data16;
4061 host->pull_data = dw_mci_pull_data16;
4063 host->data_shift = 1;
4064 } else if (i == 2) {
4065 host->push_data = dw_mci_push_data64;
4066 host->pull_data = dw_mci_pull_data64;
4068 host->data_shift = 3;
4070 /* Check for a reserved value, and warn if it is */
4072 "HCON reports a reserved host data width!\n"
4073 "Defaulting to 32-bit access.\n");
4074 host->push_data = dw_mci_push_data32;
4075 host->pull_data = dw_mci_pull_data32;
4077 host->data_shift = 2;
4080 /* Reset all blocks */
4081 if (!dw_mci_ctrl_all_reset(host))
4084 host->dma_ops = host->pdata->dma_ops;
4085 dw_mci_init_dma(host);
4087 /* Clear the interrupts for the host controller */
4088 mci_writel(host, RINTSTS, 0xFFFFFFFF);
4089 mci_writel(host, INTMASK, 0); /* disable all mmc interrupt first */
4091 /* Put in max timeout */
4092 mci_writel(host, TMOUT, 0xFFFFFFFF);
4095 * FIFO threshold settings RxMark = fifo_size / 2 - 1,
4096 * Tx Mark = fifo_size / 2 DMA Size = 8
4098 if (!host->pdata->fifo_depth) {
4100 * Power-on value of RX_WMark is FIFO_DEPTH-1, but this may
4101 * have been overwritten by the bootloader, just like we're
4102 * about to do, so if you know the value for your hardware, you
4103 * should put it in the platform data.
4105 fifo_size = mci_readl(host, FIFOTH);
4106 fifo_size = 1 + ((fifo_size >> 16) & 0xfff);
4108 fifo_size = host->pdata->fifo_depth;
4110 host->fifo_depth = fifo_size;
4112 SDMMC_SET_FIFOTH(0x2, fifo_size / 2 - 1, fifo_size / 2);
4113 mci_writel(host, FIFOTH, host->fifoth_val);
4115 /* disable clock to CIU */
4116 mci_writel(host, CLKENA, 0);
4117 mci_writel(host, CLKSRC, 0);
4119 tasklet_init(&host->tasklet, dw_mci_tasklet_func, (unsigned long)host);
4120 host->card_workqueue = alloc_workqueue("dw-mci-card",
4121 WQ_MEM_RECLAIM | WQ_NON_REENTRANT, 1);
4122 if (!host->card_workqueue) {
4126 INIT_WORK(&host->card_work, dw_mci_work_routine_card);
4127 ret = devm_request_irq(host->dev, host->irq, dw_mci_interrupt,
4128 host->irq_flags, "dw-mci", host);
4132 if (host->pdata->num_slots)
4133 host->num_slots = host->pdata->num_slots;
4135 host->num_slots = ((mci_readl(host, HCON) >> 1) & 0x1F) + 1;
4137 /* We need at least one slot to succeed */
4138 for (i = 0; i < host->num_slots; i++) {
4139 ret = dw_mci_init_slot(host, i);
4141 dev_dbg(host->dev, "slot %d init failed\n", i);
4147 * Enable interrupts for command done, data over, data empty, card det,
4148 * receive ready and error such as transmit, receive timeout, crc error
4150 mci_writel(host, RINTSTS, 0xFFFFFFFF);
4151 regs = SDMMC_INT_CMD_DONE | SDMMC_INT_DATA_OVER | SDMMC_INT_TXDR |
4152 SDMMC_INT_VSI | SDMMC_INT_RXDR | DW_MCI_ERROR_FLAGS;
4153 if (!(host->mmc->restrict_caps & RESTRICT_CARD_TYPE_SDIO) &&
4154 !(host->mmc->restrict_caps & RESTRICT_CARD_TYPE_EMMC))
4155 regs |= SDMMC_INT_CD;
4157 mci_writel(host, INTMASK, regs);
4159 mci_writel(host, CTRL, SDMMC_CTRL_INT_ENABLE); /* Enable mci interrupt */
4161 dev_info(host->dev, "DW MMC controller at irq %d, "
4162 "%d bit host data width, "
4164 host->irq, width, fifo_size);
4167 dev_info(host->dev, "%d slots initialized\n", init_slots);
4169 dev_dbg(host->dev, "attempted to initialize %d slots, "
4170 "but failed on all\n", host->num_slots);
4175 if (host->quirks & DW_MCI_QUIRK_IDMAC_DTO)
4176 dev_info(host->dev, "Internal DMAC interrupt fix enabled.\n");
4181 destroy_workqueue(host->card_workqueue);
4184 if (host->use_dma && host->dma_ops->exit)
4185 host->dma_ops->exit(host);
4188 regulator_disable(host->vmmc);
4189 regulator_put(host->vmmc);
4193 if (!IS_ERR(host->clk_mmc))
4194 clk_disable_unprepare(host->clk_mmc);
4196 if (!IS_ERR(host->hclk_mmc))
4197 clk_disable_unprepare(host->hclk_mmc);
4200 EXPORT_SYMBOL(dw_mci_probe);
4202 void dw_mci_remove(struct dw_mci *host)
4204 struct mmc_host *mmc = host->mmc;
4205 struct dw_mci_slot *slot = mmc_priv(mmc);
4208 mci_writel(host, RINTSTS, 0xFFFFFFFF);
4209 mci_writel(host, INTMASK, 0); /* disable all mmc interrupt first */
4211 for(i = 0; i < host->num_slots; i++){
4212 dev_dbg(host->dev, "remove slot %d\n", i);
4214 dw_mci_cleanup_slot(host->slot[i], i);
4217 /* disable clock to CIU */
4218 mci_writel(host, CLKENA, 0);
4219 mci_writel(host, CLKSRC, 0);
4221 destroy_workqueue(host->card_workqueue);
4222 if (host->mmc->restrict_caps & RESTRICT_CARD_TYPE_SD)
4223 unregister_pm_notifier(&host->mmc->pm_notify);
4225 if (host->use_dma && host->dma_ops->exit)
4226 host->dma_ops->exit(host);
4228 if (gpio_is_valid(slot->cd_gpio))
4229 dw_mci_of_free_cd_gpio_irq(host->dev, slot->cd_gpio, host->mmc);
4232 regulator_disable(host->vmmc);
4233 regulator_put(host->vmmc);
4235 if (!IS_ERR(host->clk_mmc))
4236 clk_disable_unprepare(host->clk_mmc);
4238 if (!IS_ERR(host->hclk_mmc))
4239 clk_disable_unprepare(host->hclk_mmc);
4240 if (!IS_ERR(host->hpclk_mmc))
4241 clk_disable_unprepare(host->hpclk_mmc);
4243 EXPORT_SYMBOL(dw_mci_remove);
4247 #ifdef CONFIG_PM_SLEEP
4249 * TODO: we should probably disable the clock to the card in the suspend path.
4251 extern int get_wifi_chip_type(void);
4252 int dw_mci_suspend(struct dw_mci *host)
4254 int present = dw_mci_get_cd(host->mmc);
4256 if((host->mmc->restrict_caps &
4257 RESTRICT_CARD_TYPE_SDIO) &&
4258 (get_wifi_chip_type() == WIFI_ESP8089 ||
4259 get_wifi_chip_type() > WIFI_AP6XXX_SERIES))
4263 regulator_disable(host->vmmc);
4265 /* Only for sdmmc controller */
4266 if (host->mmc->restrict_caps & RESTRICT_CARD_TYPE_SD) {
4267 disable_irq(host->irq);
4269 if (pinctrl_select_state(host->pinctrl, host->pins_idle) < 0)
4270 MMC_DBG_ERR_FUNC(host->mmc,
4271 "Idle pinctrl setting failed! [%s]",
4272 mmc_hostname(host->mmc));
4275 /* Soc rk3126/3036 already in gpio_cd mode */
4276 if (!soc_is_rk3126() && !soc_is_rk3126b() && !soc_is_rk3036()) {
4277 dw_mci_of_get_cd_gpio(host->dev, 0, host->mmc);
4278 enable_irq_wake(host->mmc->slot.cd_irq);
4282 mci_writel(host, RINTSTS, 0xFFFFFFFF);
4283 mci_writel(host, INTMASK, 0x00);
4284 mci_writel(host, CTRL, 0x00);
4286 if (host->rst_ops &&
4287 host->rst_ops->pre_suspend)
4288 host->rst_ops->pre_suspend(host);
4292 EXPORT_SYMBOL(dw_mci_suspend);
4294 int dw_mci_resume(struct dw_mci *host)
4298 struct dw_mci_slot *slot;
4299 int present = dw_mci_get_cd(host->mmc);
4301 if (host->rst_ops &&
4302 host->rst_ops->post_resume)
4303 host->rst_ops->post_resume(host);
4306 if ((host->mmc->restrict_caps & RESTRICT_CARD_TYPE_SDIO) &&
4307 (get_wifi_chip_type() == WIFI_ESP8089 ||
4308 get_wifi_chip_type() > WIFI_AP6XXX_SERIES))
4311 if (host->mmc->restrict_caps & RESTRICT_CARD_TYPE_SDIO) {
4312 slot = mmc_priv(host->mmc);
4313 if (!test_bit(DW_MMC_CARD_PRESENT, &slot->flags))
4317 /*only for sdmmc controller*/
4318 if (host->mmc->restrict_caps & RESTRICT_CARD_TYPE_SD) {
4319 /* Soc rk3126/3036 already in gpio_cd mode */
4320 if (!soc_is_rk3126() && !soc_is_rk3126b() && !soc_is_rk3036()) {
4321 disable_irq_wake(host->mmc->slot.cd_irq);
4322 mmc_gpio_free_cd(host->mmc);
4326 if (!IS_ERR(host->pins_udbg)) {
4327 if (pinctrl_select_state(host->pinctrl, host->pins_idle) < 0)
4328 MMC_DBG_ERR_FUNC(host->mmc,
4329 "Idle pinctrl setting failed! [%s]",
4330 mmc_hostname(host->mmc));
4331 if (pinctrl_select_state(host->pinctrl, host->pins_udbg) < 0)
4332 MMC_DBG_ERR_FUNC(host->mmc,
4333 "Udbg pinctrl setting failed! [%s]",
4334 mmc_hostname(host->mmc));
4336 if (pinctrl_select_state(host->pinctrl, host->pins_default) < 0)
4337 MMC_DBG_ERR_FUNC(host->mmc,
4338 "Default pinctrl setting failed! [%s]",
4339 mmc_hostname(host->mmc));
4342 if(pinctrl_select_state(host->pinctrl, host->pins_default) < 0)
4343 MMC_DBG_ERR_FUNC(host->mmc,
4344 "Default pinctrl setting failed! [%s]",
4345 mmc_hostname(host->mmc));
4350 grf_writel(((1 << 12) << 16) | (0 << 12), RK3288_GRF_SOC_CON0);
4351 else if(cpu_is_rk3036())
4352 grf_writel(((1 << 11) << 16) | (0 << 11), RK3036_GRF_SOC_CON0);
4353 else if(cpu_is_rk312x())
4354 /* RK3036_GRF_SOC_CON0 is compatible with rk312x, tmp setting */
4355 grf_writel(((1 << 8) << 16) | (0 << 8), RK3036_GRF_SOC_CON0);
4358 ret = regulator_enable(host->vmmc);
4361 "failed to enable regulator: %d\n", ret);
4366 if(!dw_mci_ctrl_all_reset(host)){
4371 if(!(cpu_is_rk3036() || cpu_is_rk312x()))
4372 if(host->use_dma && host->dma_ops->init)
4373 host->dma_ops->init(host);
4376 * Restore the initial value at FIFOTH register
4377 * And Invalidate the prev_blksz with zero
4379 mci_writel(host, FIFOTH, host->fifoth_val);
4380 host->prev_blksz = 0;
4381 /* Put in max timeout */
4382 mci_writel(host, TMOUT, 0xFFFFFFFF);
4384 mci_writel(host, RINTSTS, 0xFFFFFFFF);
4385 regs = SDMMC_INT_CMD_DONE | SDMMC_INT_DATA_OVER | SDMMC_INT_TXDR |
4386 SDMMC_INT_RXDR | SDMMC_INT_VSI | DW_MCI_ERROR_FLAGS;
4388 if(!(host->mmc->restrict_caps & RESTRICT_CARD_TYPE_SDIO))
4389 regs |= SDMMC_INT_CD;
4391 mci_writel(host, INTMASK, regs);
4392 mci_writel(host, CTRL, SDMMC_CTRL_INT_ENABLE);
4394 /*only for sdmmc controller*/
4395 if((host->mmc->restrict_caps & RESTRICT_CARD_TYPE_SD)){
4396 enable_irq(host->irq);
4399 for(i = 0; i < host->num_slots; i++){
4400 struct dw_mci_slot *slot = host->slot[i];
4403 if(slot->mmc->pm_flags & MMC_PM_KEEP_POWER){
4404 dw_mci_set_ios(slot->mmc, &slot->mmc->ios);
4405 dw_mci_setup_bus(slot, true);
4411 EXPORT_SYMBOL(dw_mci_resume);
4412 #endif /* CONFIG_PM_SLEEP */
4414 static int __init dw_mci_init(void)
4416 pr_info("Synopsys Designware Multimedia Card Interface Driver\n");
4417 pr_info("MHSC version = %s\n", RK_SDMMC_DRIVER_VERSION);
4421 static void __exit dw_mci_exit(void)
4425 module_init(dw_mci_init);
4426 module_exit(dw_mci_exit);
4428 MODULE_DESCRIPTION("Rockchip specific DW Multimedia Card Interface driver");
4429 MODULE_AUTHOR("Shawn Lin <lintao@rock-chips.com>");
4430 MODULE_LICENSE("GPL v2");