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 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/err.h>
22 #include <linux/init.h>
23 #include <linux/interrupt.h>
24 #include <linux/ioport.h>
25 #include <linux/module.h>
26 #include <linux/platform_device.h>
27 #include <linux/seq_file.h>
28 #include <linux/slab.h>
29 #include <linux/stat.h>
30 #include <linux/delay.h>
31 #include <linux/irq.h>
32 #include <linux/mmc/host.h>
33 #include <linux/mmc/mmc.h>
34 #include <linux/mmc/sd.h>
35 #include <linux/mmc/sdio.h>
36 #include <linux/mmc/rk_mmc.h>
37 #include <linux/bitops.h>
38 #include <linux/regulator/consumer.h>
39 #include <linux/workqueue.h>
41 #include <linux/of_gpio.h>
42 #include <linux/mmc/slot-gpio.h>
43 #include <linux/clk-private.h>
44 #include <linux/rockchip/cpu.h>
47 #include "rk_sdmmc_of.h"
48 #include <linux/regulator/rockchip_io_vol_domain.h>
49 #include "../../clk/rockchip/clk-ops.h"
51 #define grf_writel(v, offset) do { writel_relaxed(v, RK_GRF_VIRT + offset); dsb(); } while (0)
53 #define RK_SDMMC_DRIVER_VERSION "Ver 1.11 2014-06-05"
55 /* Common flag combinations */
56 #define DW_MCI_DATA_ERROR_FLAGS (SDMMC_INT_DRTO | SDMMC_INT_DCRC | \
57 /*SDMMC_INT_HTO | */SDMMC_INT_SBE | \
59 #define DW_MCI_CMD_ERROR_FLAGS (SDMMC_INT_RTO | SDMMC_INT_RCRC | \
61 #define DW_MCI_ERROR_FLAGS (DW_MCI_DATA_ERROR_FLAGS | \
62 DW_MCI_CMD_ERROR_FLAGS | SDMMC_INT_HLE)
63 #define DW_MCI_SEND_STATUS 1
64 #define DW_MCI_RECV_STATUS 2
65 #define DW_MCI_DMA_THRESHOLD 16
67 #define DW_MCI_FREQ_MAX 50000000//200000000 /* unit: HZ */
68 #define DW_MCI_FREQ_MIN 300000//400000 /* unit: HZ */
70 #define SDMMC_DATA_TIMEOUT_SD 500; /*max is 250ms showed in Spec; Maybe adapt the value for the sick card.*/
71 #define SDMMC_DATA_TIMEOUT_SDIO 250
72 #define SDMMC_DATA_TIMEOUT_EMMC 2500
74 #define SDMMC_CMD_RTO_MAX_HOLD 200
75 #define SDMMC_WAIT_FOR_UNBUSY 2500
77 #ifdef CONFIG_MMC_DW_IDMAC
78 #define IDMAC_INT_CLR (SDMMC_IDMAC_INT_AI | SDMMC_IDMAC_INT_NI | \
79 SDMMC_IDMAC_INT_CES | SDMMC_IDMAC_INT_DU | \
80 SDMMC_IDMAC_INT_FBE | SDMMC_IDMAC_INT_RI | \
84 u32 des0; /* Control Descriptor */
85 #define IDMAC_DES0_DIC BIT(1)
86 #define IDMAC_DES0_LD BIT(2)
87 #define IDMAC_DES0_FD BIT(3)
88 #define IDMAC_DES0_CH BIT(4)
89 #define IDMAC_DES0_ER BIT(5)
90 #define IDMAC_DES0_CES BIT(30)
91 #define IDMAC_DES0_OWN BIT(31)
93 u32 des1; /* Buffer sizes */
94 #define IDMAC_SET_BUFFER1_SIZE(d, s) \
95 ((d)->des1 = ((d)->des1 & 0x03ffe000) | ((s) & 0x1fff))
97 u32 des2; /* buffer 1 physical address */
99 u32 des3; /* buffer 2 physical address */
101 #endif /* CONFIG_MMC_DW_IDMAC */
103 static const u8 tuning_blk_pattern_4bit[] = {
104 0xff, 0x0f, 0xff, 0x00, 0xff, 0xcc, 0xc3, 0xcc,
105 0xc3, 0x3c, 0xcc, 0xff, 0xfe, 0xff, 0xfe, 0xef,
106 0xff, 0xdf, 0xff, 0xdd, 0xff, 0xfb, 0xff, 0xfb,
107 0xbf, 0xff, 0x7f, 0xff, 0x77, 0xf7, 0xbd, 0xef,
108 0xff, 0xf0, 0xff, 0xf0, 0x0f, 0xfc, 0xcc, 0x3c,
109 0xcc, 0x33, 0xcc, 0xcf, 0xff, 0xef, 0xff, 0xee,
110 0xff, 0xfd, 0xff, 0xfd, 0xdf, 0xff, 0xbf, 0xff,
111 0xbb, 0xff, 0xf7, 0xff, 0xf7, 0x7f, 0x7b, 0xde,
114 static const u8 tuning_blk_pattern_8bit[] = {
115 0xff, 0xff, 0x00, 0xff, 0xff, 0xff, 0x00, 0x00,
116 0xff, 0xff, 0xcc, 0xcc, 0xcc, 0x33, 0xcc, 0xcc,
117 0xcc, 0x33, 0x33, 0xcc, 0xcc, 0xcc, 0xff, 0xff,
118 0xff, 0xee, 0xff, 0xff, 0xff, 0xee, 0xee, 0xff,
119 0xff, 0xff, 0xdd, 0xff, 0xff, 0xff, 0xdd, 0xdd,
120 0xff, 0xff, 0xff, 0xbb, 0xff, 0xff, 0xff, 0xbb,
121 0xbb, 0xff, 0xff, 0xff, 0x77, 0xff, 0xff, 0xff,
122 0x77, 0x77, 0xff, 0x77, 0xbb, 0xdd, 0xee, 0xff,
123 0xff, 0xff, 0xff, 0x00, 0xff, 0xff, 0xff, 0x00,
124 0x00, 0xff, 0xff, 0xcc, 0xcc, 0xcc, 0x33, 0xcc,
125 0xcc, 0xcc, 0x33, 0x33, 0xcc, 0xcc, 0xcc, 0xff,
126 0xff, 0xff, 0xee, 0xff, 0xff, 0xff, 0xee, 0xee,
127 0xff, 0xff, 0xff, 0xdd, 0xff, 0xff, 0xff, 0xdd,
128 0xdd, 0xff, 0xff, 0xff, 0xbb, 0xff, 0xff, 0xff,
129 0xbb, 0xbb, 0xff, 0xff, 0xff, 0x77, 0xff, 0xff,
130 0xff, 0x77, 0x77, 0xff, 0x77, 0xbb, 0xdd, 0xee,
133 static inline bool dw_mci_fifo_reset(struct dw_mci *host);
134 static inline bool dw_mci_ctrl_all_reset(struct dw_mci *host);
135 static bool dw_mci_ctrl_reset(struct dw_mci *host, u32 reset);
136 static void dw_mci_disable_low_power(struct dw_mci_slot *slot);
138 /*printk the all register of current host*/
140 static int dw_mci_regs_printk(struct dw_mci *host)
142 struct sdmmc_reg *regs = dw_mci_regs;
144 while( regs->name != 0 ){
145 printk("%s: (0x%04x) = 0x%08x\n", regs->name, regs->addr, mci_readreg(host,regs->addr));
148 printk("=======printk %s-register end =========\n", mmc_hostname(host->mmc));
153 #if defined(CONFIG_DEBUG_FS)
154 static int dw_mci_req_show(struct seq_file *s, void *v)
156 struct dw_mci_slot *slot = s->private;
157 struct mmc_request *mrq;
158 struct mmc_command *cmd;
159 struct mmc_command *stop;
160 struct mmc_data *data;
162 /* Make sure we get a consistent snapshot */
163 spin_lock_bh(&slot->host->lock);
173 "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n",
174 cmd->opcode, cmd->arg, cmd->flags,
175 cmd->resp[0], cmd->resp[1], cmd->resp[2],
176 cmd->resp[2], cmd->error);
178 seq_printf(s, "DATA %u / %u * %u flg %x err %d\n",
179 data->bytes_xfered, data->blocks,
180 data->blksz, data->flags, data->error);
183 "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n",
184 stop->opcode, stop->arg, stop->flags,
185 stop->resp[0], stop->resp[1], stop->resp[2],
186 stop->resp[2], stop->error);
189 spin_unlock_bh(&slot->host->lock);
194 static int dw_mci_req_open(struct inode *inode, struct file *file)
196 return single_open(file, dw_mci_req_show, inode->i_private);
199 static const struct file_operations dw_mci_req_fops = {
200 .owner = THIS_MODULE,
201 .open = dw_mci_req_open,
204 .release = single_release,
207 static int dw_mci_regs_show(struct seq_file *s, void *v)
209 seq_printf(s, "STATUS:\t0x%08x\n", SDMMC_STATUS);
210 seq_printf(s, "RINTSTS:\t0x%08x\n", SDMMC_RINTSTS);
211 seq_printf(s, "CMD:\t0x%08x\n", SDMMC_CMD);
212 seq_printf(s, "CTRL:\t0x%08x\n", SDMMC_CTRL);
213 seq_printf(s, "INTMASK:\t0x%08x\n", SDMMC_INTMASK);
214 seq_printf(s, "CLKENA:\t0x%08x\n", SDMMC_CLKENA);
219 static int dw_mci_regs_open(struct inode *inode, struct file *file)
221 return single_open(file, dw_mci_regs_show, inode->i_private);
224 static const struct file_operations dw_mci_regs_fops = {
225 .owner = THIS_MODULE,
226 .open = dw_mci_regs_open,
229 .release = single_release,
232 static void dw_mci_init_debugfs(struct dw_mci_slot *slot)
234 struct mmc_host *mmc = slot->mmc;
235 struct dw_mci *host = slot->host;
239 root = mmc->debugfs_root;
243 node = debugfs_create_file("regs", S_IRUSR, root, host,
248 node = debugfs_create_file("req", S_IRUSR, root, slot,
253 node = debugfs_create_u32("state", S_IRUSR, root, (u32 *)&host->state);
257 node = debugfs_create_x32("pending_events", S_IRUSR, root,
258 (u32 *)&host->pending_events);
262 node = debugfs_create_x32("completed_events", S_IRUSR, root,
263 (u32 *)&host->completed_events);
270 dev_err(&mmc->class_dev, "failed to initialize debugfs for slot\n");
272 #endif /* defined(CONFIG_DEBUG_FS) */
274 static void dw_mci_set_timeout(struct dw_mci *host)
276 /* timeout (maximum) */
277 mci_writel(host, TMOUT, 0xffffffff);
280 static u32 dw_mci_prepare_command(struct mmc_host *mmc, struct mmc_command *cmd)
282 struct mmc_data *data;
283 struct dw_mci_slot *slot = mmc_priv(mmc);
284 const struct dw_mci_drv_data *drv_data = slot->host->drv_data;
286 cmd->error = -EINPROGRESS;
290 if (cmdr == MMC_STOP_TRANSMISSION)
291 cmdr |= SDMMC_CMD_STOP;
293 cmdr |= SDMMC_CMD_PRV_DAT_WAIT;
295 if (cmd->flags & MMC_RSP_PRESENT) {
296 /* We expect a response, so set this bit */
297 cmdr |= SDMMC_CMD_RESP_EXP;
298 if (cmd->flags & MMC_RSP_136)
299 cmdr |= SDMMC_CMD_RESP_LONG;
302 if (cmd->flags & MMC_RSP_CRC)
303 cmdr |= SDMMC_CMD_RESP_CRC;
307 cmdr |= SDMMC_CMD_DAT_EXP;
308 if (data->flags & MMC_DATA_STREAM)
309 cmdr |= SDMMC_CMD_STRM_MODE;
310 if (data->flags & MMC_DATA_WRITE)
311 cmdr |= SDMMC_CMD_DAT_WR;
314 if (drv_data && drv_data->prepare_command)
315 drv_data->prepare_command(slot->host, &cmdr);
321 static u32 dw_mci_prep_stop_abort(struct dw_mci *host, struct mmc_command *cmd)
323 struct mmc_command *stop;
329 stop = &host->stop_abort;
331 memset(stop, 0, sizeof(struct mmc_command));
333 if (cmdr == MMC_READ_SINGLE_BLOCK ||
334 cmdr == MMC_READ_MULTIPLE_BLOCK ||
335 cmdr == MMC_WRITE_BLOCK ||
336 cmdr == MMC_WRITE_MULTIPLE_BLOCK) {
337 stop->opcode = MMC_STOP_TRANSMISSION;
339 stop->flags = MMC_RSP_R1B | MMC_CMD_AC;
340 } else if (cmdr == SD_IO_RW_EXTENDED) {
341 stop->opcode = SD_IO_RW_DIRECT;
342 stop->arg |= (1 << 31) | (0 << 28) | (SDIO_CCCR_ABORT << 9) |
343 ((cmd->arg >> 28) & 0x7);
344 stop->flags = MMC_RSP_SPI_R5 | MMC_RSP_R5 | MMC_CMD_AC;
349 cmdr = stop->opcode | SDMMC_CMD_STOP |
350 SDMMC_CMD_RESP_CRC | SDMMC_CMD_RESP_EXP;
355 static void dw_mci_start_command(struct dw_mci *host,
356 struct mmc_command *cmd, u32 cmd_flags)
358 struct dw_mci_slot *slot = host->slot[0];
359 /*temporality fix slot[0] due to host->num_slots equal to 1*/
361 host->pre_cmd = host->cmd;
364 "start command: ARGR=0x%08x CMDR=0x%08x\n",
365 cmd->arg, cmd_flags);
367 if(SD_SWITCH_VOLTAGE == cmd->opcode){
368 /*confirm non-low-power mode*/
369 mci_writel(host, CMDARG, 0);
370 dw_mci_disable_low_power(slot);
372 MMC_DBG_INFO_FUNC(host->mmc,"Line%d..%s before start cmd=11,[%s]",
373 __LINE__, __FUNCTION__,mmc_hostname(host->mmc));
375 cmd_flags |= SDMMC_CMD_VOLT_SWITCH;
378 mci_writel(host, CMDARG, cmd->arg);
381 /* fix the value to 1 in some Soc,for example RK3188. */
382 if(host->mmc->hold_reg_flag)
383 cmd_flags |= SDMMC_CMD_USE_HOLD_REG;
385 mci_writel(host, CMD, cmd_flags | SDMMC_CMD_START);
389 static void send_stop_cmd(struct dw_mci *host, struct mmc_data *data)
391 dw_mci_start_command(host, data->stop, host->stop_cmdr);
394 /* DMA interface functions */
395 static void dw_mci_stop_dma(struct dw_mci *host)
397 if (host->using_dma) {
398 host->dma_ops->stop(host);
399 host->dma_ops->cleanup(host);
402 /* Data transfer was stopped by the interrupt handler */
403 set_bit(EVENT_XFER_COMPLETE, &host->pending_events);
406 static int dw_mci_get_dma_dir(struct mmc_data *data)
408 if (data->flags & MMC_DATA_WRITE)
409 return DMA_TO_DEVICE;
411 return DMA_FROM_DEVICE;
414 #ifdef CONFIG_MMC_DW_IDMAC
415 static void dw_mci_dma_cleanup(struct dw_mci *host)
417 struct mmc_data *data = host->data;
420 if (!data->host_cookie)
421 dma_unmap_sg(host->dev,
424 dw_mci_get_dma_dir(data));
427 static void dw_mci_idmac_reset(struct dw_mci *host)
429 u32 bmod = mci_readl(host, BMOD);
430 /* Software reset of DMA */
431 bmod |= SDMMC_IDMAC_SWRESET;
432 mci_writel(host, BMOD, bmod);
435 static void dw_mci_idmac_stop_dma(struct dw_mci *host)
439 /* Disable and reset the IDMAC interface */
440 temp = mci_readl(host, CTRL);
441 temp &= ~SDMMC_CTRL_USE_IDMAC;
442 temp |= SDMMC_CTRL_DMA_RESET;
443 mci_writel(host, CTRL, temp);
445 /* Stop the IDMAC running */
446 temp = mci_readl(host, BMOD);
447 temp &= ~(SDMMC_IDMAC_ENABLE | SDMMC_IDMAC_FB);
448 temp |= SDMMC_IDMAC_SWRESET;
449 mci_writel(host, BMOD, temp);
452 static void dw_mci_idmac_complete_dma(struct dw_mci *host)
454 struct mmc_data *data = host->data;
456 dev_vdbg(host->dev, "DMA complete\n");
459 MMC_DBG_CMD_FUNC(host->mmc," DMA complete cmd=%d(arg=0x%x), blocks=%d,blksz=%d[%s]", \
460 host->mrq->cmd->opcode,host->mrq->cmd->arg,data->blocks,data->blksz,mmc_hostname(host->mmc));
463 host->dma_ops->cleanup(host);
466 * If the card was removed, data will be NULL. No point in trying to
467 * send the stop command or waiting for NBUSY in this case.
470 set_bit(EVENT_XFER_COMPLETE, &host->pending_events);
471 tasklet_schedule(&host->tasklet);
475 static void dw_mci_translate_sglist(struct dw_mci *host, struct mmc_data *data,
479 struct idmac_desc *desc = host->sg_cpu;
481 for (i = 0; i < sg_len; i++, desc++) {
482 unsigned int length = sg_dma_len(&data->sg[i]);
483 u32 mem_addr = sg_dma_address(&data->sg[i]);
485 /* Set the OWN bit and disable interrupts for this descriptor */
486 desc->des0 = IDMAC_DES0_OWN | IDMAC_DES0_DIC | IDMAC_DES0_CH;
489 IDMAC_SET_BUFFER1_SIZE(desc, length);
491 /* Physical address to DMA to/from */
492 desc->des2 = mem_addr;
495 /* Set first descriptor */
497 desc->des0 |= IDMAC_DES0_FD;
499 /* Set last descriptor */
500 desc = host->sg_cpu + (i - 1) * sizeof(struct idmac_desc);
501 desc->des0 &= ~(IDMAC_DES0_CH | IDMAC_DES0_DIC);
502 desc->des0 |= IDMAC_DES0_LD;
507 static void dw_mci_idmac_start_dma(struct dw_mci *host, unsigned int sg_len)
511 dw_mci_translate_sglist(host, host->data, sg_len);
513 /* Select IDMAC interface */
514 temp = mci_readl(host, CTRL);
515 temp |= SDMMC_CTRL_USE_IDMAC;
516 mci_writel(host, CTRL, temp);
520 /* Enable the IDMAC */
521 temp = mci_readl(host, BMOD);
522 temp |= SDMMC_IDMAC_ENABLE | SDMMC_IDMAC_FB;
523 mci_writel(host, BMOD, temp);
525 /* Start it running */
526 mci_writel(host, PLDMND, 1);
529 static int dw_mci_idmac_init(struct dw_mci *host)
531 struct idmac_desc *p;
534 /* Number of descriptors in the ring buffer */
535 host->ring_size = PAGE_SIZE / sizeof(struct idmac_desc);
537 /* Forward link the descriptor list */
538 for (i = 0, p = host->sg_cpu; i < host->ring_size - 1; i++, p++)
539 p->des3 = host->sg_dma + (sizeof(struct idmac_desc) * (i + 1));
541 /* Set the last descriptor as the end-of-ring descriptor */
542 p->des3 = host->sg_dma;
543 p->des0 = IDMAC_DES0_ER;
545 dw_mci_idmac_reset(host);
547 /* Mask out interrupts - get Tx & Rx complete only */
548 mci_writel(host, IDSTS, IDMAC_INT_CLR);
549 mci_writel(host, IDINTEN, SDMMC_IDMAC_INT_NI | SDMMC_IDMAC_INT_RI |
552 /* Set the descriptor base address */
553 mci_writel(host, DBADDR, host->sg_dma);
557 static const struct dw_mci_dma_ops dw_mci_idmac_ops = {
558 .init = dw_mci_idmac_init,
559 .start = dw_mci_idmac_start_dma,
560 .stop = dw_mci_idmac_stop_dma,
561 .complete = dw_mci_idmac_complete_dma,
562 .cleanup = dw_mci_dma_cleanup,
564 #endif /* CONFIG_MMC_DW_IDMAC */
566 static int dw_mci_pre_dma_transfer(struct dw_mci *host,
567 struct mmc_data *data,
570 struct scatterlist *sg;
571 unsigned int i, sg_len;
573 if (!next && data->host_cookie)
574 return data->host_cookie;
577 * We don't do DMA on "complex" transfers, i.e. with
578 * non-word-aligned buffers or lengths. Also, we don't bother
579 * with all the DMA setup overhead for short transfers.
581 if (data->blocks * data->blksz < DW_MCI_DMA_THRESHOLD)
587 for_each_sg(data->sg, sg, data->sg_len, i) {
588 if (sg->offset & 3 || sg->length & 3)
592 sg_len = dma_map_sg(host->dev,
595 dw_mci_get_dma_dir(data));
600 data->host_cookie = sg_len;
605 static void dw_mci_pre_req(struct mmc_host *mmc,
606 struct mmc_request *mrq,
609 struct dw_mci_slot *slot = mmc_priv(mmc);
610 struct mmc_data *data = mrq->data;
612 if (!slot->host->use_dma || !data)
615 if (data->host_cookie) {
616 data->host_cookie = 0;
620 if (dw_mci_pre_dma_transfer(slot->host, mrq->data, 1) < 0)
621 data->host_cookie = 0;
624 static void dw_mci_post_req(struct mmc_host *mmc,
625 struct mmc_request *mrq,
628 struct dw_mci_slot *slot = mmc_priv(mmc);
629 struct mmc_data *data = mrq->data;
631 if (!slot->host->use_dma || !data)
634 if (data->host_cookie)
635 dma_unmap_sg(slot->host->dev,
638 dw_mci_get_dma_dir(data));
639 data->host_cookie = 0;
642 static void dw_mci_adjust_fifoth(struct dw_mci *host, struct mmc_data *data)
644 #ifdef CONFIG_MMC_DW_IDMAC
645 unsigned int blksz = data->blksz;
646 const u32 mszs[] = {1, 4, 8, 16, 32, 64, 128, 256};
647 u32 fifo_width = 1 << host->data_shift;
648 u32 blksz_depth = blksz / fifo_width, fifoth_val;
649 u32 msize = 0, rx_wmark = 1, tx_wmark, tx_wmark_invers;
650 int idx = (sizeof(mszs) / sizeof(mszs[0])) - 1;
652 tx_wmark = (host->fifo_depth) / 2;
653 tx_wmark_invers = host->fifo_depth - tx_wmark;
657 * if blksz is not a multiple of the FIFO width
659 if (blksz % fifo_width) {
666 if (!((blksz_depth % mszs[idx]) ||
667 (tx_wmark_invers % mszs[idx]))) {
669 rx_wmark = mszs[idx] - 1;
674 * If idx is '0', it won't be tried
675 * Thus, initial values are uesed
678 fifoth_val = SDMMC_SET_FIFOTH(msize, rx_wmark, tx_wmark);
679 mci_writel(host, FIFOTH, fifoth_val);
683 static void dw_mci_ctrl_rd_thld(struct dw_mci *host, struct mmc_data *data)
685 unsigned int blksz = data->blksz;
686 u32 blksz_depth, fifo_depth;
689 WARN_ON(!(data->flags & MMC_DATA_READ));
691 if (host->timing != MMC_TIMING_MMC_HS200 &&
692 host->timing != MMC_TIMING_UHS_SDR104)
695 blksz_depth = blksz / (1 << host->data_shift);
696 fifo_depth = host->fifo_depth;
698 if (blksz_depth > fifo_depth)
702 * If (blksz_depth) >= (fifo_depth >> 1), should be 'thld_size <= blksz'
703 * If (blksz_depth) < (fifo_depth >> 1), should be thld_size = blksz
704 * Currently just choose blksz.
707 mci_writel(host, CDTHRCTL, SDMMC_SET_RD_THLD(thld_size, 1));
711 mci_writel(host, CDTHRCTL, SDMMC_SET_RD_THLD(0, 0));
714 static int dw_mci_submit_data_dma(struct dw_mci *host, struct mmc_data *data)
721 /* If we don't have a channel, we can't do DMA */
725 sg_len = dw_mci_pre_dma_transfer(host, data, 0);
727 host->dma_ops->stop(host);
734 "sd sg_cpu: %#lx sg_dma: %#lx sg_len: %d\n",
735 (unsigned long)host->sg_cpu, (unsigned long)host->sg_dma,
739 * Decide the MSIZE and RX/TX Watermark.
740 * If current block size is same with previous size,
741 * no need to update fifoth.
743 if (host->prev_blksz != data->blksz)
744 dw_mci_adjust_fifoth(host, data);
747 dw_mci_ctrl_reset(host, SDMMC_CTRL_DMA_RESET);// | SDMMC_CTRL_FIFO_RESET); //dange to fifo-reset; noted by xbw,at 2014-03-28
749 /* Enable the DMA interface */
750 temp = mci_readl(host, CTRL);
751 temp |= SDMMC_CTRL_DMA_ENABLE;
752 mci_writel(host, CTRL, temp);
754 /* Disable RX/TX IRQs, let DMA handle it */
755 temp = mci_readl(host, INTMASK);
756 temp &= ~(SDMMC_INT_RXDR | SDMMC_INT_TXDR);
757 mci_writel(host, INTMASK, temp);
759 host->dma_ops->start(host, sg_len);
764 static void dw_mci_submit_data(struct dw_mci *host, struct mmc_data *data)
768 data->error = -EINPROGRESS;
775 dw_mci_ctrl_reset(host, SDMMC_CTRL_DMA_RESET);// | SDMMC_CTRL_FIFO_RESET);//dange to fifo-reset; noted by xbw,at 2014-03-28
777 if (data->flags & MMC_DATA_READ) {
778 host->dir_status = DW_MCI_RECV_STATUS;
779 dw_mci_ctrl_rd_thld(host, data);
781 host->dir_status = DW_MCI_SEND_STATUS;
784 MMC_DBG_INFO_FUNC(host->mmc," dw_mci_submit_data,blocks=%d,blksz=%d [%s]",\
785 data->blocks, data->blksz, mmc_hostname(host->mmc));
787 if (dw_mci_submit_data_dma(host, data)) {
788 int flags = SG_MITER_ATOMIC;
789 if (host->data->flags & MMC_DATA_READ)
790 flags |= SG_MITER_TO_SG;
792 flags |= SG_MITER_FROM_SG;
794 sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
796 host->part_buf_start = 0;
797 host->part_buf_count = 0;
799 mci_writel(host, RINTSTS, SDMMC_INT_TXDR | SDMMC_INT_RXDR);
800 temp = mci_readl(host, INTMASK);
801 temp |= SDMMC_INT_TXDR | SDMMC_INT_RXDR;
802 mci_writel(host, INTMASK, temp);
804 temp = mci_readl(host, CTRL);
805 temp &= ~SDMMC_CTRL_DMA_ENABLE;
806 mci_writel(host, CTRL, temp);
809 * Use the initial fifoth_val for PIO mode.
810 * If next issued data may be transfered by DMA mode,
811 * prev_blksz should be invalidated.
813 mci_writel(host, FIFOTH, host->fifoth_val);
814 host->prev_blksz = 0;
817 * Keep the current block size.
818 * It will be used to decide whether to update
819 * fifoth register next time.
821 host->prev_blksz = data->blksz;
825 static void mci_send_cmd(struct dw_mci_slot *slot, u32 cmd, u32 arg)
827 struct dw_mci *host = slot->host;
828 unsigned long timeout = jiffies + msecs_to_jiffies(500);//msecs_to_jiffies(5000);
829 unsigned int cmd_status = 0;
830 #ifdef SDMMC_WAIT_FOR_UNBUSY
832 timeout = jiffies + msecs_to_jiffies(SDMMC_WAIT_FOR_UNBUSY);
834 if(test_bit(DW_MMC_CARD_PRESENT, &slot->flags)) {
836 ret = time_before(jiffies, timeout);
837 cmd_status = mci_readl(host, STATUS);
838 if (!(cmd_status & (SDMMC_STAUTS_DATA_BUSY|SDMMC_STAUTS_MC_BUSY)))
842 printk("%d..%s: wait for unbusy timeout.......[%s]\n", \
843 __LINE__, __FUNCTION__, mmc_hostname(host->mmc));
846 mci_writel(host, CMDARG, arg);
848 mci_writel(host, CMD, SDMMC_CMD_START | cmd);
849 if(cmd & SDMMC_CMD_UPD_CLK)
850 timeout = jiffies + msecs_to_jiffies(50);
852 timeout = jiffies + msecs_to_jiffies(500);
853 while (time_before(jiffies, timeout)) {
854 cmd_status = mci_readl(host, CMD);
855 if (!(cmd_status & SDMMC_CMD_START))
858 dev_err(&slot->mmc->class_dev,
859 "Timeout sending command (cmd %#x arg %#x status %#x)\n",
860 cmd, arg, cmd_status);
863 static void dw_mci_setup_bus(struct dw_mci_slot *slot, bool force_clkinit)
865 struct dw_mci *host = slot->host;
866 unsigned int tempck,clock = slot->clock;
871 MMC_DBG_INFO_FUNC(host->mmc,"%d..%s: clock=%d, current_speed=%d, bus_hz=%d,forc=%d[%s]\n",
872 __LINE__, __FUNCTION__, clock, host->current_speed,host->bus_hz,force_clkinit,mmc_hostname(host->mmc));
875 mci_writel(host, CLKENA, 0);
877 SDMMC_CMD_UPD_CLK | SDMMC_CMD_PRV_DAT_WAIT, 0);
879 } else if (clock != host->current_speed || force_clkinit) {
880 div = host->bus_hz / clock;
881 if (host->bus_hz % clock && host->bus_hz > clock)
883 * move the + 1 after the divide to prevent
884 * over-clocking the card.
888 div = (host->bus_hz != clock) ? DIV_ROUND_UP(div, 2) : 0;
890 if ((clock << div) != slot->__clk_old || force_clkinit) {
891 tempck = div ? ((host->bus_hz / div) >> 1) :host->bus_hz;
892 dev_info(&slot->mmc->class_dev,
893 "Bus speed (slot %d) = %dHz (slot req %dHz, actual %dHZ div = %d)\n",
894 slot->id, host->bus_hz, clock,
897 host->set_speed = tempck;
902 mci_writel(host, CLKENA, 0);
903 mci_writel(host, CLKSRC, 0);
907 SDMMC_CMD_UPD_CLK | SDMMC_CMD_PRV_DAT_WAIT, 0);
909 if(clock <= 400*1000){
910 MMC_DBG_BOOT_FUNC(host->mmc,
911 "dw_mci_setup_bus: argue clk_mmc workaround out %dHz for init[%s]",
912 clock * 2, mmc_hostname(host->mmc));
913 /* RK3288 clk_mmc will change parents to 24MHz xtal*/
914 clk_set_rate(host->clk_mmc, clock * 2);
921 MMC_DBG_BOOT_FUNC(host->mmc,
922 "dw_mci_setup_bus: argue clk_mmc workaround out normal clock [%s]",
923 mmc_hostname(host->mmc));
926 MMC_DBG_ERR_FUNC(host->mmc,
927 "dw_mci_setup_bus: div SHOULD NOT LARGER THAN ONE! [%s]",
928 mmc_hostname(host->mmc));
931 host->bus_hz = host->set_speed * 2;
932 MMC_DBG_BOOT_FUNC(host->mmc,
933 "dw_mci_setup_bus: workaround div = %d, host->bus_hz = %d [%s]",
934 div, host->bus_hz, mmc_hostname(host->mmc));
936 /* BUG may be here, come on, Linux BSP engineer looks!
937 FIXME: HS-DDR eMMC, div SHOULD be ONE, but we here cannot fetch eMMC bus mode!!!!!!!!
938 WRONG dts set clk = 50M, and calc div be zero. Controller denied this setting!
939 some oops happened like that:
940 mmc_host mmc0: Bus speed (slot 0) = 50000000Hz (slot req 50000000Hz, actual 50000000HZ div = 0)
941 rk_sdmmc: BOOT dw_mci_setup_bus: argue clk_mmc workaround out normal clock [mmc0]
942 rk_sdmmc: BOOT Bus speed=50000000Hz,Bus width=8bits.[mmc0]
943 mmc0: new high speed DDR MMC card at address 0001
944 mmcblk0: mmc0:0001 M8G1GC 7.28 GiB
946 mmcblk0: error -84 transferring data, sector 606208, nr 32, cmd response 0x900, card status 0xb00
947 mmcblk0: retrying using single block read
948 mmcblk0: error -110 sending status command, retrying
950 How to: If eMMC HW version < 4.51, or > 4.51 but no caps2-mmc-hs200 support in dts
951 Please set dts emmc clk to 100M or 150M, I will workaround it!
954 if (host->verid < DW_MMC_240A)
955 clk_set_rate(host->clk_mmc,(host->bus_hz));
957 clk_set_rate(host->clk_mmc,(host->bus_hz) * 2);
963 /* set clock to desired speed */
964 mci_writel(host, CLKDIV, div);
968 SDMMC_CMD_UPD_CLK | SDMMC_CMD_PRV_DAT_WAIT, 0);
970 /* enable clock; only low power if no SDIO */
971 clk_en_a = SDMMC_CLKEN_ENABLE << slot->id;
973 if (host->verid < DW_MMC_240A)
974 sdio_int = SDMMC_INT_SDIO(slot->id);
976 sdio_int = SDMMC_INT_SDIO((slot->id) + 8);
978 if (!(mci_readl(host, INTMASK) & sdio_int))
979 clk_en_a |= SDMMC_CLKEN_LOW_PWR << slot->id;
980 mci_writel(host, CLKENA, clk_en_a);
984 SDMMC_CMD_UPD_CLK | SDMMC_CMD_PRV_DAT_WAIT, 0);
985 /* keep the clock with reflecting clock dividor */
986 slot->__clk_old = clock << div;
989 host->current_speed = clock;
991 if(slot->ctype != slot->pre_ctype)
992 MMC_DBG_BOOT_FUNC(host->mmc, "Bus speed=%dHz,Bus width=%s.[%s]",
994 (slot->ctype == SDMMC_CTYPE_4BIT)?"4bits":"8bits",
995 mmc_hostname(host->mmc));
996 slot->pre_ctype = slot->ctype;
998 /* Set the current slot bus width */
999 mci_writel(host, CTYPE, (slot->ctype << slot->id));
1002 static void dw_mci_wait_unbusy(struct dw_mci *host)
1005 unsigned int timeout= SDMMC_DATA_TIMEOUT_SDIO;
1006 unsigned long time_loop;
1007 unsigned int status;
1009 MMC_DBG_INFO_FUNC(host->mmc, "dw_mci_wait_unbusy, status=0x%x ", mci_readl(host, STATUS));
1011 if(host->mmc->restrict_caps & RESTRICT_CARD_TYPE_EMMC)
1012 timeout = SDMMC_DATA_TIMEOUT_EMMC;
1013 else if(host->mmc->restrict_caps & RESTRICT_CARD_TYPE_SD)
1014 timeout = SDMMC_DATA_TIMEOUT_SD;
1016 time_loop = jiffies + msecs_to_jiffies(timeout);
1018 status = mci_readl(host, STATUS);
1019 if (!(status & (SDMMC_STAUTS_DATA_BUSY|SDMMC_STAUTS_MC_BUSY)))
1021 //MMC_DBG_INFO_FUNC("dw_mci_wait_unbusy, waiting for......");
1022 } while (time_before(jiffies, time_loop));
1027 * 0--status is busy.
1028 * 1--status is unbusy.
1030 int dw_mci_card_busy(struct mmc_host *mmc)
1032 struct dw_mci_slot *slot = mmc_priv(mmc);
1033 struct dw_mci *host = slot->host;
1034 unsigned int timeout= SDMMC_DATA_TIMEOUT_SDIO;
1035 unsigned long time_loop;
1036 unsigned int status;
1039 if(host->mmc->restrict_caps & RESTRICT_CARD_TYPE_EMMC)
1040 timeout = SDMMC_DATA_TIMEOUT_EMMC;
1041 else if(host->mmc->restrict_caps & RESTRICT_CARD_TYPE_SD)
1042 timeout = SDMMC_DATA_TIMEOUT_SD;
1043 timeout = 250*1000;//test
1044 time_loop = jiffies + msecs_to_jiffies(timeout);
1046 MMC_DBG_INFO_FUNC(host->mmc, "line%d: dw_mci_wait_unbusy,timeloop=%lu, status=0x%x ",
1047 __LINE__, time_loop, mci_readl(host, STATUS));
1049 status = mci_readl(host, STATUS);
1050 if (!(status & (SDMMC_STAUTS_DATA_BUSY|SDMMC_STAUTS_MC_BUSY))){
1051 ret = 1;//card is unbusy.
1054 //MMC_DBG_INFO_FUNC("dw_mci_wait_unbusy, waiting for......");
1055 } while (time_before(jiffies, time_loop));
1056 MMC_DBG_INFO_FUNC(host->mmc, "line%d: dw_mci_wait_unbusy,ret=%d, status=0x%x ",
1057 __LINE__,ret,mci_readl(host, STATUS));
1062 static void __dw_mci_start_request(struct dw_mci *host,
1063 struct dw_mci_slot *slot,
1064 struct mmc_command *cmd)
1066 struct mmc_request *mrq;
1067 struct mmc_data *data;
1071 if (host->pdata->select_slot)
1072 host->pdata->select_slot(slot->id);
1074 host->cur_slot = slot;
1076 #if 0 //add by xbw,at 2014-03-12
1077 /*clean FIFO if it is a new request*/
1078 if(!(mrq->cmd->opcode & SDMMC_CMD_STOP)) {
1079 MMC_DBG_INFO_FUNC("%d..%s: reset the ctrl.", __LINE__, __FUNCTION__);
1080 mci_writel(host, CTRL, (SDMMC_CTRL_RESET | SDMMC_CTRL_FIFO_RESET |
1081 SDMMC_CTRL_DMA_RESET));
1084 dw_mci_wait_unbusy(host);
1086 host->pending_events = 0;
1087 host->completed_events = 0;
1088 host->data_status = 0;
1092 dw_mci_set_timeout(host);
1093 mci_writel(host, BYTCNT, data->blksz*data->blocks);
1094 mci_writel(host, BLKSIZ, data->blksz);
1097 cmdflags = dw_mci_prepare_command(slot->mmc, cmd);
1099 /* this is the first command, send the initialization clock */
1100 if (test_and_clear_bit(DW_MMC_CARD_NEED_INIT, &slot->flags))
1101 cmdflags |= SDMMC_CMD_INIT;
1104 dw_mci_submit_data(host, data);
1108 dw_mci_start_command(host, cmd, cmdflags);
1111 host->stop_cmdr = dw_mci_prepare_command(slot->mmc, mrq->stop);
1114 static void dw_mci_start_request(struct dw_mci *host,
1115 struct dw_mci_slot *slot)
1117 struct mmc_request *mrq = slot->mrq;
1118 struct mmc_command *cmd;
1120 MMC_DBG_INFO_FUNC(host->mmc, " Begin to start the new request. cmd=%d(arg=0x%x)[%s]", \
1121 mrq->cmd->opcode, mrq->cmd->arg, mmc_hostname(host->mmc));
1123 cmd = mrq->sbc ? mrq->sbc : mrq->cmd;
1124 __dw_mci_start_request(host, slot, cmd);
1127 /* must be called with host->lock held */
1128 static void dw_mci_queue_request(struct dw_mci *host, struct dw_mci_slot *slot,
1129 struct mmc_request *mrq)
1131 dev_vdbg(&slot->mmc->class_dev, "queue request: state=%d\n",
1136 if (host->state == STATE_IDLE) {
1137 host->state = STATE_SENDING_CMD;
1138 dw_mci_start_request(host, slot);
1140 list_add_tail(&slot->queue_node, &host->queue);
1144 static void dw_mci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1146 struct dw_mci_slot *slot = mmc_priv(mmc);
1147 struct dw_mci *host = slot->host;
1152 * The check for card presence and queueing of the request must be
1153 * atomic, otherwise the card could be removed in between and the
1154 * request wouldn't fail until another card was inserted.
1156 spin_lock_bh(&host->lock);
1158 if (!test_bit(DW_MMC_CARD_PRESENT, &slot->flags)) {
1159 spin_unlock_bh(&host->lock);
1160 mrq->cmd->error = -ENOMEDIUM;
1161 MMC_DBG_CMD_FUNC(host->mmc, "%d..%s: no card,so reqeuest done, cmd=%d [%s]",\
1162 __LINE__, __FUNCTION__, mrq->cmd->opcode, mmc_hostname(host->mmc));
1164 mmc_request_done(mmc, mrq);
1167 MMC_DBG_CMD_FUNC(host->mmc, "======>\n pull a new request from MMC-frame to dw_mci_queue. cmd=%d(arg=0x%x)[%s]", \
1168 mrq->cmd->opcode, mrq->cmd->arg, mmc_hostname(host->mmc));
1170 dw_mci_queue_request(host, slot, mrq);
1172 spin_unlock_bh(&host->lock);
1175 static void dw_mci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1177 struct dw_mci_slot *slot = mmc_priv(mmc);
1178 const struct dw_mci_drv_data *drv_data = slot->host->drv_data;
1180 #ifdef SDMMC_WAIT_FOR_UNBUSY
1181 unsigned long time_loop = jiffies + msecs_to_jiffies(SDMMC_WAIT_FOR_UNBUSY);
1184 if(!test_bit(DW_MMC_CARD_PRESENT, &slot->flags)){
1185 printk("%d..%s: no card. [%s]\n", \
1186 __LINE__, __FUNCTION__, mmc_hostname(mmc));
1191 ret = time_before(jiffies, time_loop);
1192 regs = mci_readl(slot->host, STATUS);
1193 if (!(regs & (SDMMC_STAUTS_DATA_BUSY|SDMMC_STAUTS_MC_BUSY)))
1199 printk("slot->flags = %lu ", slot->flags);
1201 printk("%d..%s: wait for unbusy timeout....... STATUS = 0x%x [%s]\n", \
1202 __LINE__, __FUNCTION__, regs, mmc_hostname(mmc));
1205 switch (ios->bus_width) {
1206 case MMC_BUS_WIDTH_4:
1207 slot->ctype = SDMMC_CTYPE_4BIT;
1209 case MMC_BUS_WIDTH_8:
1210 slot->ctype = SDMMC_CTYPE_8BIT;
1213 /* set default 1 bit mode */
1214 slot->ctype = SDMMC_CTYPE_1BIT;
1215 slot->pre_ctype = SDMMC_CTYPE_1BIT;
1218 regs = mci_readl(slot->host, UHS_REG);
1221 if (ios->timing == MMC_TIMING_UHS_DDR50)
1222 regs |= ((0x1 << slot->id) << 16);
1224 regs &= ~((0x1 << slot->id) << 16);
1226 mci_writel(slot->host, UHS_REG, regs);
1227 slot->host->timing = ios->timing;
1230 * Use mirror of ios->clock to prevent race with mmc
1231 * core ios update when finding the minimum.
1233 slot->clock = ios->clock;
1235 if (drv_data && drv_data->set_ios)
1236 drv_data->set_ios(slot->host, ios);
1238 /* Slot specific timing and width adjustment */
1239 dw_mci_setup_bus(slot, false);
1243 switch (ios->power_mode) {
1245 set_bit(DW_MMC_CARD_NEED_INIT, &slot->flags);
1247 if (slot->host->pdata->setpower)
1248 slot->host->pdata->setpower(slot->id, mmc->ocr_avail);
1249 regs = mci_readl(slot->host, PWREN);
1250 regs |= (1 << slot->id);
1251 mci_writel(slot->host, PWREN, regs);
1254 /* Power down slot */
1255 if (slot->host->pdata->setpower)
1256 slot->host->pdata->setpower(slot->id, 0);
1257 regs = mci_readl(slot->host, PWREN);
1258 regs &= ~(1 << slot->id);
1259 mci_writel(slot->host, PWREN, regs);
1266 static int dw_mci_get_ro(struct mmc_host *mmc)
1269 struct dw_mci_slot *slot = mmc_priv(mmc);
1270 struct dw_mci_board *brd = slot->host->pdata;
1272 /* Use platform get_ro function, else try on board write protect */
1273 if (slot->quirks & DW_MCI_SLOT_QUIRK_NO_WRITE_PROTECT)
1275 else if (brd->get_ro)
1276 read_only = brd->get_ro(slot->id);
1277 else if (gpio_is_valid(slot->wp_gpio))
1278 read_only = gpio_get_value(slot->wp_gpio);
1281 mci_readl(slot->host, WRTPRT) & (1 << slot->id) ? 1 : 0;
1283 dev_dbg(&mmc->class_dev, "card is %s\n",
1284 read_only ? "read-only" : "read-write");
1289 static int dw_mci_set_sdio_status(struct mmc_host *mmc, int val)
1291 struct dw_mci_slot *slot = mmc_priv(mmc);
1292 /*struct dw_mci_board *brd = slot->host->pdata;*/
1293 struct dw_mci *host = slot->host;
1294 if (!(mmc->restrict_caps & RESTRICT_CARD_TYPE_SDIO))
1297 spin_lock_bh(&host->lock);
1299 set_bit(DW_MMC_CARD_PRESENT, &slot->flags);
1300 if(__clk_is_enabled(host->hclk_mmc) == false)
1301 clk_prepare_enable(host->hclk_mmc);
1302 if(__clk_is_enabled(host->clk_mmc) == false)
1303 clk_prepare_enable(host->clk_mmc);
1305 clear_bit(DW_MMC_CARD_PRESENT, &slot->flags);
1306 if(__clk_is_enabled(host->clk_mmc) == true)
1307 clk_disable_unprepare(slot->host->clk_mmc);
1308 if(__clk_is_enabled(host->hclk_mmc) == true)
1309 clk_disable_unprepare(slot->host->hclk_mmc);
1311 spin_unlock_bh(&host->lock);
1312 mmc_detect_change(slot->mmc, 20);
1319 static int dw_mci_get_cd(struct mmc_host *mmc)
1322 struct dw_mci_slot *slot = mmc_priv(mmc);
1323 struct dw_mci_board *brd = slot->host->pdata;
1324 struct dw_mci *host = slot->host;
1325 int gpio_cd = mmc_gpio_get_cd(mmc);
1327 if (mmc->restrict_caps & RESTRICT_CARD_TYPE_SDIO)
1328 return test_bit(DW_MMC_CARD_PRESENT, &slot->flags);
1330 /* Use platform get_cd function, else try onboard card detect */
1331 if (brd->quirks & DW_MCI_QUIRK_BROKEN_CARD_DETECTION)
1333 else if (brd->get_cd)
1334 present = !brd->get_cd(slot->id);
1335 else if (!IS_ERR_VALUE(gpio_cd))
1338 present = (mci_readl(slot->host, CDETECT) & (1 << slot->id))
1341 spin_lock_bh(&host->lock);
1343 set_bit(DW_MMC_CARD_PRESENT, &slot->flags);
1344 dev_dbg(&mmc->class_dev, "card is present\n");
1346 clear_bit(DW_MMC_CARD_PRESENT, &slot->flags);
1347 dev_dbg(&mmc->class_dev, "card is not present\n");
1349 spin_unlock_bh(&host->lock);
1354 static void dw_mci_hw_reset(struct mmc_host *mmc)
1356 struct dw_mci_slot *slot = mmc_priv(mmc);
1357 struct dw_mci *host = slot->host;
1358 u32 cmd_flags, regs;
1359 unsigned long timeout;
1362 /* (1) CMD12 to end any transfer in process */
1363 cmd_flags = SDMMC_CMD_STOP | SDMMC_CMD_RESP_CRC
1364 | SDMMC_CMD_RESP_EXP | MMC_STOP_TRANSMISSION;
1366 if(host->mmc->hold_reg_flag)
1367 cmd_flags |= SDMMC_CMD_USE_HOLD_REG;
1368 mci_writel(host, CMDARG, 0);
1370 mci_writel(host, CMD, cmd_flags | SDMMC_CMD_START);
1372 timeout = jiffies + msecs_to_jiffies(500);
1374 ret = time_before(jiffies, timeout);
1375 if(!(mci_readl(host, CMD) & SDMMC_CMD_START))
1380 MMC_DBG_ERR_FUNC(host->mmc,
1381 "%s dw_mci_hw_reset: STOP_TRANSMISSION failed!!! [%s]\n",
1382 __func__, mmc_hostname(host->mmc));
1384 /* (2) wait DTO, even if no response is sent back by card */
1386 timeout = jiffies + msecs_to_jiffies(5);
1388 ret = time_before(jiffies, timeout);
1389 if(!(mci_readl(host, MINTSTS) & SDMMC_INT_DATA_OVER)){
1390 mci_writel(host, RINTSTS, SDMMC_INT_DATA_OVER);
1395 /* (3) Reset following: DONNOT CHANGE RESET ORDER!*/
1397 /* Software reset - BMOD[0] for IDMA only */
1398 regs = mci_readl(host, BMOD);
1399 regs |= SDMMC_IDMAC_SWRESET;
1400 mci_writel(host, BMOD, regs);
1401 udelay(1); /* Auto cleared after 1 cycle, 1us is enough for hclk_mmc */
1402 regs = mci_readl(host, BMOD);
1403 if(regs & SDMMC_IDMAC_SWRESET)
1404 MMC_DBG_WARN_FUNC(host->mmc,
1405 "%s dw_mci_hw_reset: SDMMC_IDMAC_SWRESET failed!!! [%s]\n",
1406 __func__, mmc_hostname(host->mmc));
1408 /* DMA reset - CTRL[2] */
1409 regs = mci_readl(host, CTRL);
1410 regs |= SDMMC_CTRL_DMA_RESET;
1411 mci_writel(host, CTRL, regs);
1412 udelay(1); /* Auto cleared after 2 AHB clocks, 1us is enough plus mci_readl access */
1413 regs = mci_readl(host, CTRL);
1414 if(regs & SDMMC_CTRL_DMA_RESET)
1415 MMC_DBG_WARN_FUNC(host->mmc,
1416 "%s dw_mci_hw_reset: SDMMC_CTRL_DMA_RESET failed!!! [%s]\n",
1417 __func__, mmc_hostname(host->mmc));
1419 /* FIFO reset - CTRL[1] */
1420 regs = mci_readl(host, CTRL);
1421 regs |= SDMMC_CTRL_FIFO_RESET;
1422 mci_writel(host, CTRL, regs);
1423 mdelay(1); /* no timing limited, 1ms is random value */
1424 regs = mci_readl(host, CTRL);
1425 if(regs & SDMMC_CTRL_FIFO_RESET)
1426 MMC_DBG_WARN_FUNC(host->mmc,
1427 "%s dw_mci_hw_reset: SDMMC_CTRL_DMA_RESET failed!!! [%s]\n",
1428 __func__, mmc_hostname(host->mmc));
1431 According to eMMC spec
1432 tRstW >= 1us ; RST_n pulse width
1433 tRSCA >= 200us ; RST_n to Command time
1434 tRSTH >= 1us ; RST_n high period
1437 mci_writel(slot->host, RST_n, 0x1);
1439 udelay(10); /* 10us for bad quality eMMc. */
1441 mci_writel(slot->host, RST_n, 0x0);
1443 usleep_range(500, 1000); /* at least 500(> 200us) */
1447 * Disable lower power mode.
1449 * Low power mode will stop the card clock when idle. According to the
1450 * description of the CLKENA register we should disable low power mode
1451 * for SDIO cards if we need SDIO interrupts to work.
1453 * This function is fast if low power mode is already disabled.
1455 static void dw_mci_disable_low_power(struct dw_mci_slot *slot)
1457 struct dw_mci *host = slot->host;
1459 const u32 clken_low_pwr = SDMMC_CLKEN_LOW_PWR << slot->id;
1461 clk_en_a = mci_readl(host, CLKENA);
1463 if (clk_en_a & clken_low_pwr) {
1464 mci_writel(host, CLKENA, clk_en_a & ~clken_low_pwr);
1465 mci_send_cmd(slot, SDMMC_CMD_UPD_CLK |
1466 SDMMC_CMD_PRV_DAT_WAIT, 0);
1470 static void dw_mci_enable_sdio_irq(struct mmc_host *mmc, int enb)
1472 struct dw_mci_slot *slot = mmc_priv(mmc);
1473 struct dw_mci *host = slot->host;
1477 /* Enable/disable Slot Specific SDIO interrupt */
1478 int_mask = mci_readl(host, INTMASK);
1480 if (host->verid < DW_MMC_240A)
1481 sdio_int = SDMMC_INT_SDIO(slot->id);
1483 sdio_int = SDMMC_INT_SDIO((slot->id) + 8);
1487 * Turn off low power mode if it was enabled. This is a bit of
1488 * a heavy operation and we disable / enable IRQs a lot, so
1489 * we'll leave low power mode disabled and it will get
1490 * re-enabled again in dw_mci_setup_bus().
1492 dw_mci_disable_low_power(slot);
1494 mci_writel(host, INTMASK,
1495 (int_mask | sdio_int));
1497 mci_writel(host, INTMASK,
1498 (int_mask & ~sdio_int));
1503 static int dw_mci_do_start_signal_voltage_switch(struct dw_mci *host,
1504 struct mmc_ios *ios)
1507 unsigned int value,uhs_reg;
1510 * Signal Voltage Switching is only applicable for Host Controllers
1513 if (host->verid < DW_MMC_240A)
1516 uhs_reg = mci_readl(host, UHS_REG);
1517 MMC_DBG_SW_VOL_FUNC(host->mmc,"%d..%s: vol=%d.[%s]\n",
1518 __LINE__, __FUNCTION__,ios->signal_voltage, mmc_hostname(host->mmc));
1520 switch (ios->signal_voltage) {
1521 case MMC_SIGNAL_VOLTAGE_330:
1522 /* Set 1.8V Signal Enable in the Host Control2 register to 0 */
1524 ret = io_domain_regulator_set_voltage(host->vmmc, 3300000, 3300000);
1525 //regulator_put(host->vmmc); //to be done in remove function.
1527 MMC_DBG_SW_VOL_FUNC(host->mmc,"%s =%dmV set 3.3end, ret=%d \n",
1528 __func__, regulator_get_voltage(host->vmmc), ret);
1530 pr_warning("%s: Switching to 3.3V signalling voltage "
1531 " failed\n", mmc_hostname(host->mmc));
1535 MMC_DBG_SW_VOL_FUNC(host->mmc,"%d..%s: [%s]\n",__LINE__, __FUNCTION__, mmc_hostname(host->mmc));
1537 //set High-power mode
1538 value = mci_readl(host, CLKENA);
1539 mci_writel(host,CLKENA , value& ~SDMMC_CLKEN_LOW_PWR);
1541 mci_writel(host,UHS_REG , uhs_reg & ~SDMMC_UHS_VOLT_REG_18);
1544 usleep_range(5000, 5500);
1546 /* 3.3V regulator output should be stable within 5 ms */
1547 uhs_reg = mci_readl(host, UHS_REG);
1548 if( !(uhs_reg & SDMMC_UHS_VOLT_REG_18))
1551 pr_warning("%s: 3.3V regulator output did not became stable\n",
1552 mmc_hostname(host->mmc));
1555 case MMC_SIGNAL_VOLTAGE_180:
1557 ret = io_domain_regulator_set_voltage(host->vmmc,1800000, 1800000);
1558 // regulator_put(host->vmmc);//to be done in remove function.
1560 MMC_DBG_SW_VOL_FUNC(host->mmc,"%d..%s =%dmV set 1.8end, ret=%d . \n",
1561 __LINE__, __func__, regulator_get_voltage(host->vmmc), ret);
1563 pr_warning("%s: Switching to 1.8V signalling voltage "
1564 " failed\n", mmc_hostname(host->mmc));
1570 * Enable 1.8V Signal Enable in the Host Control2
1573 mci_writel(host,UHS_REG , uhs_reg | SDMMC_UHS_VOLT_REG_18);
1576 usleep_range(5000, 5500);
1577 MMC_DBG_SW_VOL_FUNC(host->mmc,"%d..%s: .[%s]\n",__LINE__, __FUNCTION__,mmc_hostname(host->mmc));
1579 /* 1.8V regulator output should be stable within 5 ms */
1580 uhs_reg = mci_readl(host, UHS_REG);
1581 if( uhs_reg & SDMMC_UHS_VOLT_REG_18){
1586 pr_warning("%s: 1.8V regulator output did not became stable\n",
1587 mmc_hostname(host->mmc));
1590 case MMC_SIGNAL_VOLTAGE_120:
1592 ret = io_domain_regulator_set_voltage(host->vmmc, 1200000, 1200000);
1594 pr_warning("%s: Switching to 1.2V signalling voltage "
1595 " failed\n", mmc_hostname(host->mmc));
1601 /* No signal voltage switch required */
1607 static int dw_mci_start_signal_voltage_switch(struct mmc_host *mmc,
1608 struct mmc_ios *ios)
1610 struct dw_mci_slot *slot = mmc_priv(mmc);
1611 struct dw_mci *host = slot->host;
1614 if (host->verid < DW_MMC_240A)
1616 //sdhci_runtime_pm_get(host);
1617 err = dw_mci_do_start_signal_voltage_switch(host, ios);
1618 //sdhci_runtime_pm_put(host);
1622 static int dw_mci_execute_tuning(struct mmc_host *mmc, u32 opcode)
1624 struct dw_mci_slot *slot = mmc_priv(mmc);
1625 struct dw_mci *host = slot->host;
1626 const struct dw_mci_drv_data *drv_data = host->drv_data;
1627 struct dw_mci_tuning_data tuning_data;
1630 if (opcode == MMC_SEND_TUNING_BLOCK_HS200) {
1631 if (mmc->ios.bus_width == MMC_BUS_WIDTH_8) {
1632 tuning_data.blk_pattern = tuning_blk_pattern_8bit;
1633 tuning_data.blksz = sizeof(tuning_blk_pattern_8bit);
1634 } else if (mmc->ios.bus_width == MMC_BUS_WIDTH_4) {
1635 tuning_data.blk_pattern = tuning_blk_pattern_4bit;
1636 tuning_data.blksz = sizeof(tuning_blk_pattern_4bit);
1640 } else if (opcode == MMC_SEND_TUNING_BLOCK) {
1641 tuning_data.blk_pattern = tuning_blk_pattern_4bit;
1642 tuning_data.blksz = sizeof(tuning_blk_pattern_4bit);
1645 "Undefined command(%d) for tuning\n", opcode);
1650 /* Recommend sample phase and delayline
1651 Fixme: Mix-use these three controllers will cause
1654 if (mmc->restrict_caps & RESTRICT_CARD_TYPE_EMMC)
1655 tuning_data.con_id = 3;
1656 else if(mmc->restrict_caps & RESTRICT_CARD_TYPE_SDIO)
1657 tuning_data.con_id = 1;
1659 tuning_data.con_id = 0;
1661 /* 0: driver, from host->devices
1662 1: sample, from devices->host
1664 tuning_data.tuning_type = 1;
1666 if (drv_data && drv_data->execute_tuning)
1667 err = drv_data->execute_tuning(slot, opcode, &tuning_data);
1672 static const struct mmc_host_ops dw_mci_ops = {
1673 .request = dw_mci_request,
1674 .pre_req = dw_mci_pre_req,
1675 .post_req = dw_mci_post_req,
1676 .set_ios = dw_mci_set_ios,
1677 .get_ro = dw_mci_get_ro,
1678 .get_cd = dw_mci_get_cd,
1679 .set_sdio_status = dw_mci_set_sdio_status,
1680 .hw_reset = dw_mci_hw_reset,
1681 .enable_sdio_irq = dw_mci_enable_sdio_irq,
1682 .execute_tuning = dw_mci_execute_tuning,
1683 //.start_signal_voltage_switch = dw_mci_start_signal_voltage_switch,
1684 //.card_busy = dw_mci_card_busy,
1687 static void dw_mci_enable_irq(struct dw_mci *host, bool irqflag)
1689 unsigned long flags;
1694 local_irq_save(flags);
1695 if(host->irq_state != irqflag)
1697 host->irq_state = irqflag;
1700 enable_irq(host->irq);
1704 disable_irq(host->irq);
1707 local_irq_restore(flags);
1710 static void dw_mci_deal_data_end(struct dw_mci *host, struct mmc_request *mrq)
1711 __releases(&host->lock)
1712 __acquires(&host->lock)
1714 if(DW_MCI_SEND_STATUS == host->dir_status){
1716 if( MMC_BUS_TEST_W != host->cmd->opcode){
1717 if(host->data_status & SDMMC_INT_DCRC)
1718 host->data->error = -EILSEQ;
1719 else if(host->data_status & SDMMC_INT_EBE)
1720 host->data->error = -ETIMEDOUT;
1722 dw_mci_wait_unbusy(host);
1725 dw_mci_wait_unbusy(host);
1731 static void dw_mci_request_end(struct dw_mci *host, struct mmc_request *mrq)
1732 __releases(&host->lock)
1733 __acquires(&host->lock)
1735 struct dw_mci_slot *slot;
1736 struct mmc_host *prev_mmc = host->cur_slot->mmc;
1738 WARN_ON(host->cmd || host->data);
1740 dw_mci_deal_data_end(host, mrq);
1743 MMC_DBG_CMD_FUNC(host->mmc, " reqeust end--reqeuest done, cmd=%d, cmderr=%d, host->state=%d [%s]",\
1744 mrq->cmd->opcode,mrq->cmd->error, host->state,mmc_hostname(host->mmc));
1746 MMC_DBG_CMD_FUNC(host->mmc, " reqeust end--reqeuest done, cmd=%d, dataerr=%d, host->state=%d [%s]",\
1747 mrq->cmd->opcode,mrq->data->error, host->state, mmc_hostname(host->mmc));
1749 host->cur_slot->mrq = NULL;
1751 if (!list_empty(&host->queue)) {
1752 slot = list_entry(host->queue.next,
1753 struct dw_mci_slot, queue_node);
1754 list_del(&slot->queue_node);
1755 dev_vdbg(host->dev, "list not empty: %s is next\n",
1756 mmc_hostname(slot->mmc));
1757 host->state = STATE_SENDING_CMD;
1758 MMC_DBG_CMD_FUNC(host->mmc, " list is not empty. run the request in list. [%s]", mmc_hostname(host->mmc));
1759 dw_mci_start_request(host, slot);
1761 dev_vdbg(host->dev, "list empty\n");
1762 host->state = STATE_IDLE;
1765 spin_unlock(&host->lock);
1766 mmc_request_done(prev_mmc, mrq);
1767 spin_lock(&host->lock);
1770 static void dw_mci_command_complete(struct dw_mci *host, struct mmc_command *cmd)
1772 u32 status = host->cmd_status;
1774 host->cmd_status = 0;
1776 /* Read the response from the card (up to 16 bytes) */
1777 if (cmd->flags & MMC_RSP_PRESENT) {
1778 if (cmd->flags & MMC_RSP_136) {
1779 cmd->resp[3] = mci_readl(host, RESP0);
1780 cmd->resp[2] = mci_readl(host, RESP1);
1781 cmd->resp[1] = mci_readl(host, RESP2);
1782 cmd->resp[0] = mci_readl(host, RESP3);
1784 MMC_DBG_INFO_FUNC(host->mmc,"Line%d: command complete cmd=%d,resp[3]=0x%x, resp[2]=0x%x,resp[1]=0x%x,resp[0]=0x%x.[%s]", \
1785 __LINE__,cmd->opcode,cmd->resp[3], cmd->resp[2], cmd->resp[1], cmd->resp[0], mmc_hostname(host->mmc));
1787 cmd->resp[0] = mci_readl(host, RESP0);
1791 MMC_DBG_INFO_FUNC(host->mmc, "Line%d: command complete cmd=%d,resp[0]=0x%x. [%s]",\
1792 __LINE__,cmd->opcode, cmd->resp[0], mmc_hostname(host->mmc));
1796 if (status & SDMMC_INT_RTO)
1798 if(host->mmc->restrict_caps & RESTRICT_CARD_TYPE_SDIO)
1801 cmd->error = -ETIMEDOUT;
1803 else if ((cmd->flags & MMC_RSP_CRC) && (status & SDMMC_INT_RCRC))
1804 cmd->error = -EILSEQ;
1805 else if (status & SDMMC_INT_RESP_ERR)
1809 MMC_DBG_CMD_FUNC(host->mmc, " command complete, cmd=%d,cmdError=%d [%s]",cmd->opcode, cmd->error,mmc_hostname(host->mmc));
1812 if(MMC_SEND_STATUS != cmd->opcode)
1813 if(host->cmd_rto >= SDMMC_CMD_RTO_MAX_HOLD){
1814 MMC_DBG_ERR_FUNC(host->mmc, " command complete, cmd=%d,cmdError=%d [%s]",\
1815 cmd->opcode, cmd->error,mmc_hostname(host->mmc));
1819 /* newer ip versions need a delay between retries */
1820 if (host->quirks & DW_MCI_QUIRK_RETRY_DELAY)
1826 static void dw_mci_tasklet_func(unsigned long priv)
1828 struct dw_mci *host = (struct dw_mci *)priv;
1829 struct dw_mci_slot *slot = mmc_priv(host->mmc);
1830 struct mmc_data *data;
1831 struct mmc_command *cmd;
1832 enum dw_mci_state state;
1833 enum dw_mci_state prev_state;
1834 u32 status, cmd_flags;
1835 unsigned long timeout = 0;
1838 spin_lock(&host->lock);
1840 state = host->state;
1850 case STATE_SENDING_CMD:
1851 if (!test_and_clear_bit(EVENT_CMD_COMPLETE,
1852 &host->pending_events))
1857 set_bit(EVENT_CMD_COMPLETE, &host->completed_events);
1858 dw_mci_command_complete(host, cmd);
1859 if (cmd == host->mrq->sbc && !cmd->error) {
1860 prev_state = state = STATE_SENDING_CMD;
1861 __dw_mci_start_request(host, host->cur_slot,
1866 if (cmd->data && cmd->error) {
1867 dw_mci_stop_dma(host);
1870 send_stop_cmd(host, data);
1871 state = STATE_SENDING_STOP;
1877 send_stop_abort(host, data);
1878 state = STATE_SENDING_STOP;
1884 if (!host->mrq->data || cmd->error) {
1885 dw_mci_request_end(host, host->mrq);
1889 prev_state = state = STATE_SENDING_DATA;
1892 case STATE_SENDING_DATA:
1893 if (test_and_clear_bit(EVENT_DATA_ERROR, &host->pending_events)) {
1894 dw_mci_stop_dma(host);
1897 send_stop_cmd(host, data);
1899 /*single block read/write, send stop cmd manually to prevent host controller halt*/
1900 MMC_DBG_INFO_FUNC(host->mmc, "%s status 1 0x%08x [%s]\n",
1901 __func__, mci_readl(host, STATUS), mmc_hostname(host->mmc));
1903 mci_writel(host, CMDARG, 0);
1905 cmd_flags = SDMMC_CMD_STOP |SDMMC_CMD_RESP_CRC|SDMMC_CMD_RESP_EXP|MMC_STOP_TRANSMISSION;
1907 if(host->mmc->hold_reg_flag)
1908 cmd_flags |= SDMMC_CMD_USE_HOLD_REG;
1910 mci_writel(host, CMD, cmd_flags | SDMMC_CMD_START);
1912 timeout = jiffies + msecs_to_jiffies(500);
1915 ret = time_before(jiffies, timeout);
1916 if(!(mci_readl(host, CMD) & SDMMC_CMD_START))
1921 MMC_DBG_ERR_FUNC(host->mmc,
1922 "%s EVENT_DATA_ERROR recovery failed!!! [%s]\n",
1923 __func__, mmc_hostname(host->mmc));
1926 send_stop_abort(host, data);
1928 state = STATE_DATA_ERROR;
1932 MMC_DBG_CMD_FUNC(host->mmc,
1933 "Pre-state[%d]-->NowState[%d]: STATE_SENDING_DATA, wait for EVENT_XFER_COMPLETE.[%s]",\
1934 prev_state,state, mmc_hostname(host->mmc));
1936 if (!test_and_clear_bit(EVENT_XFER_COMPLETE,
1937 &host->pending_events))
1939 MMC_DBG_INFO_FUNC(host->mmc,
1940 "Pre-state[%d]-->NowState[%d]: STATE_SENDING_DATA, wait for EVENT_DATA_COMPLETE. [%s]",\
1941 prev_state,state,mmc_hostname(host->mmc));
1943 set_bit(EVENT_XFER_COMPLETE, &host->completed_events);
1944 prev_state = state = STATE_DATA_BUSY;
1947 case STATE_DATA_BUSY:
1948 if (!test_and_clear_bit(EVENT_DATA_COMPLETE,
1949 &host->pending_events))
1952 dw_mci_deal_data_end(host, host->mrq);
1953 MMC_DBG_INFO_FUNC(host->mmc,
1954 "Pre-state[%d]-->NowState[%d]: STATE_DATA_BUSY, after EVENT_DATA_COMPLETE. [%s]", \
1955 prev_state,state,mmc_hostname(host->mmc));
1958 set_bit(EVENT_DATA_COMPLETE, &host->completed_events);
1959 status = host->data_status;
1961 if (status & DW_MCI_DATA_ERROR_FLAGS) {
1962 if((SDMMC_CTYPE_1BIT != slot->ctype)&&(MMC_SEND_EXT_CSD == host->mrq->cmd->opcode))
1963 MMC_DBG_ERR_FUNC(host->mmc,
1964 "Pre-state[%d]-->NowState[%d]: DW_MCI_DATA_ERROR_FLAGS,datastatus=0x%x [%s]",\
1965 prev_state,state, status, mmc_hostname(host->mmc));
1967 if (status & SDMMC_INT_DRTO) {
1968 data->error = -ETIMEDOUT;
1969 } else if (status & SDMMC_INT_DCRC) {
1970 data->error = -EILSEQ;
1971 } else if (status & SDMMC_INT_EBE &&
1972 host->dir_status == DW_MCI_SEND_STATUS){
1974 * No data CRC status was returned.
1975 * The number of bytes transferred will
1976 * be exaggerated in PIO mode.
1978 data->bytes_xfered = 0;
1979 data->error = -ETIMEDOUT;
1988 * After an error, there may be data lingering
1989 * in the FIFO, so reset it - doing so
1990 * generates a block interrupt, hence setting
1991 * the scatter-gather pointer to NULL.
1993 dw_mci_fifo_reset(host);
1995 data->bytes_xfered = data->blocks * data->blksz;
2000 MMC_DBG_CMD_FUNC(host->mmc, "Pre-state[%d]-->NowState[%d]: no stop and no dataerr, exit. [%s]", \
2001 prev_state,state,mmc_hostname(host->mmc));
2002 dw_mci_request_end(host, host->mrq);
2005 MMC_DBG_CMD_FUNC(host->mmc, "Pre-state[%d]-->NowState[%d]: begin to stop . [%s]", \
2006 prev_state,state,mmc_hostname(host->mmc));
2008 if (host->mrq->sbc && !data->error) {
2009 data->stop->error = 0;
2011 MMC_DBG_CMD_FUNC(host->mmc, "Pre-state[%d]-->NowState[%d]: have stop and sbc, exit. [%s]", \
2012 prev_state,state,mmc_hostname(host->mmc));
2014 dw_mci_request_end(host, host->mrq);
2018 prev_state = state = STATE_SENDING_STOP;
2020 send_stop_cmd(host, data);
2022 if (data->stop && !data->error) {
2023 /* stop command for open-ended transfer*/
2025 send_stop_abort(host, data);
2029 MMC_DBG_CMD_FUNC(host->mmc, "Pre-state[%d]-->NowState[%d]: begin to STATE_SENDING_STOP . [%s]", \
2030 prev_state,state,mmc_hostname(host->mmc));
2032 case STATE_SENDING_STOP:
2033 if (!test_and_clear_bit(EVENT_CMD_COMPLETE,
2034 &host->pending_events))
2036 MMC_DBG_CMD_FUNC(host->mmc, "Pre-state[%d]-->NowState[%d]: begin to send cmd12 . [%s]", \
2037 prev_state,state,mmc_hostname(host->mmc));
2039 /* CMD error in data command */
2040 if (host->mrq->cmd->error && host->mrq->data) {
2041 dw_mci_fifo_reset(host);
2047 dw_mci_command_complete(host, host->mrq->stop);
2049 if (host->mrq->stop)
2050 dw_mci_command_complete(host, host->mrq->stop);
2052 host->cmd_status = 0;
2055 dw_mci_request_end(host, host->mrq);
2058 case STATE_DATA_ERROR:
2059 if (!test_and_clear_bit(EVENT_XFER_COMPLETE,
2060 &host->pending_events))
2063 state = STATE_DATA_BUSY;
2066 } while (state != prev_state);
2068 host->state = state;
2070 spin_unlock(&host->lock);
2074 /* push final bytes to part_buf, only use during push */
2075 static void dw_mci_set_part_bytes(struct dw_mci *host, void *buf, int cnt)
2077 memcpy((void *)&host->part_buf, buf, cnt);
2078 host->part_buf_count = cnt;
2081 /* append bytes to part_buf, only use during push */
2082 static int dw_mci_push_part_bytes(struct dw_mci *host, void *buf, int cnt)
2084 cnt = min(cnt, (1 << host->data_shift) - host->part_buf_count);
2085 memcpy((void *)&host->part_buf + host->part_buf_count, buf, cnt);
2086 host->part_buf_count += cnt;
2090 /* pull first bytes from part_buf, only use during pull */
2091 static int dw_mci_pull_part_bytes(struct dw_mci *host, void *buf, int cnt)
2093 cnt = min(cnt, (int)host->part_buf_count);
2095 memcpy(buf, (void *)&host->part_buf + host->part_buf_start,
2097 host->part_buf_count -= cnt;
2098 host->part_buf_start += cnt;
2103 /* pull final bytes from the part_buf, assuming it's just been filled */
2104 static void dw_mci_pull_final_bytes(struct dw_mci *host, void *buf, int cnt)
2106 memcpy(buf, &host->part_buf, cnt);
2107 host->part_buf_start = cnt;
2108 host->part_buf_count = (1 << host->data_shift) - cnt;
2111 static void dw_mci_push_data16(struct dw_mci *host, void *buf, int cnt)
2113 struct mmc_data *data = host->data;
2116 /* try and push anything in the part_buf */
2117 if (unlikely(host->part_buf_count)) {
2118 int len = dw_mci_push_part_bytes(host, buf, cnt);
2121 if (host->part_buf_count == 2) {
2122 mci_writew(host, DATA(host->data_offset),
2124 host->part_buf_count = 0;
2127 #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
2128 if (unlikely((unsigned long)buf & 0x1)) {
2130 u16 aligned_buf[64];
2131 int len = min(cnt & -2, (int)sizeof(aligned_buf));
2132 int items = len >> 1;
2134 /* memcpy from input buffer into aligned buffer */
2135 memcpy(aligned_buf, buf, len);
2138 /* push data from aligned buffer into fifo */
2139 for (i = 0; i < items; ++i)
2140 mci_writew(host, DATA(host->data_offset),
2147 for (; cnt >= 2; cnt -= 2)
2148 mci_writew(host, DATA(host->data_offset), *pdata++);
2151 /* put anything remaining in the part_buf */
2153 dw_mci_set_part_bytes(host, buf, cnt);
2154 /* Push data if we have reached the expected data length */
2155 if ((data->bytes_xfered + init_cnt) ==
2156 (data->blksz * data->blocks))
2157 mci_writew(host, DATA(host->data_offset),
2162 static void dw_mci_pull_data16(struct dw_mci *host, void *buf, int cnt)
2164 #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
2165 if (unlikely((unsigned long)buf & 0x1)) {
2167 /* pull data from fifo into aligned buffer */
2168 u16 aligned_buf[64];
2169 int len = min(cnt & -2, (int)sizeof(aligned_buf));
2170 int items = len >> 1;
2172 for (i = 0; i < items; ++i)
2173 aligned_buf[i] = mci_readw(host,
2174 DATA(host->data_offset));
2175 /* memcpy from aligned buffer into output buffer */
2176 memcpy(buf, aligned_buf, len);
2184 for (; cnt >= 2; cnt -= 2)
2185 *pdata++ = mci_readw(host, DATA(host->data_offset));
2189 host->part_buf16 = mci_readw(host, DATA(host->data_offset));
2190 dw_mci_pull_final_bytes(host, buf, cnt);
2194 static void dw_mci_push_data32(struct dw_mci *host, void *buf, int cnt)
2196 struct mmc_data *data = host->data;
2199 /* try and push anything in the part_buf */
2200 if (unlikely(host->part_buf_count)) {
2201 int len = dw_mci_push_part_bytes(host, buf, cnt);
2204 if (host->part_buf_count == 4) {
2205 mci_writel(host, DATA(host->data_offset),
2207 host->part_buf_count = 0;
2210 #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
2211 if (unlikely((unsigned long)buf & 0x3)) {
2213 u32 aligned_buf[32];
2214 int len = min(cnt & -4, (int)sizeof(aligned_buf));
2215 int items = len >> 2;
2217 /* memcpy from input buffer into aligned buffer */
2218 memcpy(aligned_buf, buf, len);
2221 /* push data from aligned buffer into fifo */
2222 for (i = 0; i < items; ++i)
2223 mci_writel(host, DATA(host->data_offset),
2230 for (; cnt >= 4; cnt -= 4)
2231 mci_writel(host, DATA(host->data_offset), *pdata++);
2234 /* put anything remaining in the part_buf */
2236 dw_mci_set_part_bytes(host, buf, cnt);
2237 /* Push data if we have reached the expected data length */
2238 if ((data->bytes_xfered + init_cnt) ==
2239 (data->blksz * data->blocks))
2240 mci_writel(host, DATA(host->data_offset),
2245 static void dw_mci_pull_data32(struct dw_mci *host, void *buf, int cnt)
2247 #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
2248 if (unlikely((unsigned long)buf & 0x3)) {
2250 /* pull data from fifo into aligned buffer */
2251 u32 aligned_buf[32];
2252 int len = min(cnt & -4, (int)sizeof(aligned_buf));
2253 int items = len >> 2;
2255 for (i = 0; i < items; ++i)
2256 aligned_buf[i] = mci_readl(host,
2257 DATA(host->data_offset));
2258 /* memcpy from aligned buffer into output buffer */
2259 memcpy(buf, aligned_buf, len);
2267 for (; cnt >= 4; cnt -= 4)
2268 *pdata++ = mci_readl(host, DATA(host->data_offset));
2272 host->part_buf32 = mci_readl(host, DATA(host->data_offset));
2273 dw_mci_pull_final_bytes(host, buf, cnt);
2277 static void dw_mci_push_data64(struct dw_mci *host, void *buf, int cnt)
2279 struct mmc_data *data = host->data;
2282 /* try and push anything in the part_buf */
2283 if (unlikely(host->part_buf_count)) {
2284 int len = dw_mci_push_part_bytes(host, buf, cnt);
2288 if (host->part_buf_count == 8) {
2289 mci_writeq(host, DATA(host->data_offset),
2291 host->part_buf_count = 0;
2294 #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
2295 if (unlikely((unsigned long)buf & 0x7)) {
2297 u64 aligned_buf[16];
2298 int len = min(cnt & -8, (int)sizeof(aligned_buf));
2299 int items = len >> 3;
2301 /* memcpy from input buffer into aligned buffer */
2302 memcpy(aligned_buf, buf, len);
2305 /* push data from aligned buffer into fifo */
2306 for (i = 0; i < items; ++i)
2307 mci_writeq(host, DATA(host->data_offset),
2314 for (; cnt >= 8; cnt -= 8)
2315 mci_writeq(host, DATA(host->data_offset), *pdata++);
2318 /* put anything remaining in the part_buf */
2320 dw_mci_set_part_bytes(host, buf, cnt);
2321 /* Push data if we have reached the expected data length */
2322 if ((data->bytes_xfered + init_cnt) ==
2323 (data->blksz * data->blocks))
2324 mci_writeq(host, DATA(host->data_offset),
2329 static void dw_mci_pull_data64(struct dw_mci *host, void *buf, int cnt)
2331 #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
2332 if (unlikely((unsigned long)buf & 0x7)) {
2334 /* pull data from fifo into aligned buffer */
2335 u64 aligned_buf[16];
2336 int len = min(cnt & -8, (int)sizeof(aligned_buf));
2337 int items = len >> 3;
2339 for (i = 0; i < items; ++i)
2340 aligned_buf[i] = mci_readq(host,
2341 DATA(host->data_offset));
2342 /* memcpy from aligned buffer into output buffer */
2343 memcpy(buf, aligned_buf, len);
2351 for (; cnt >= 8; cnt -= 8)
2352 *pdata++ = mci_readq(host, DATA(host->data_offset));
2356 host->part_buf = mci_readq(host, DATA(host->data_offset));
2357 dw_mci_pull_final_bytes(host, buf, cnt);
2361 static void dw_mci_pull_data(struct dw_mci *host, void *buf, int cnt)
2365 /* get remaining partial bytes */
2366 len = dw_mci_pull_part_bytes(host, buf, cnt);
2367 if (unlikely(len == cnt))
2372 /* get the rest of the data */
2373 host->pull_data(host, buf, cnt);
2376 static void dw_mci_read_data_pio(struct dw_mci *host, bool dto)
2378 struct sg_mapping_iter *sg_miter = &host->sg_miter;
2380 unsigned int offset;
2381 struct mmc_data *data = host->data;
2382 int shift = host->data_shift;
2385 unsigned int remain, fcnt;
2387 if(!host->mmc->bus_refs){
2388 printk("Note: %s host->mmc->bus_refs is 0!!!\n", __func__);
2392 if (!sg_miter_next(sg_miter))
2395 host->sg = sg_miter->piter.sg;
2396 buf = sg_miter->addr;
2397 remain = sg_miter->length;
2401 fcnt = (SDMMC_GET_FCNT(mci_readl(host, STATUS))
2402 << shift) + host->part_buf_count;
2403 len = min(remain, fcnt);
2406 dw_mci_pull_data(host, (void *)(buf + offset), len);
2407 data->bytes_xfered += len;
2412 sg_miter->consumed = offset;
2413 status = mci_readl(host, MINTSTS);
2414 mci_writel(host, RINTSTS, SDMMC_INT_RXDR);
2415 /* if the RXDR is ready read again */
2416 } while ((status & SDMMC_INT_RXDR) ||
2417 (dto && SDMMC_GET_FCNT(mci_readl(host, STATUS))));
2420 if (!sg_miter_next(sg_miter))
2422 sg_miter->consumed = 0;
2424 sg_miter_stop(sg_miter);
2428 sg_miter_stop(sg_miter);
2432 set_bit(EVENT_XFER_COMPLETE, &host->pending_events);
2435 static void dw_mci_write_data_pio(struct dw_mci *host)
2437 struct sg_mapping_iter *sg_miter = &host->sg_miter;
2439 unsigned int offset;
2440 struct mmc_data *data = host->data;
2441 int shift = host->data_shift;
2444 unsigned int fifo_depth = host->fifo_depth;
2445 unsigned int remain, fcnt;
2447 if(!host->mmc->bus_refs){
2448 printk("Note: %s host->mmc->bus_refs is 0!!!\n", __func__);
2453 if (!sg_miter_next(sg_miter))
2456 host->sg = sg_miter->piter.sg;
2457 buf = sg_miter->addr;
2458 remain = sg_miter->length;
2462 fcnt = ((fifo_depth -
2463 SDMMC_GET_FCNT(mci_readl(host, STATUS)))
2464 << shift) - host->part_buf_count;
2465 len = min(remain, fcnt);
2468 host->push_data(host, (void *)(buf + offset), len);
2469 data->bytes_xfered += len;
2474 sg_miter->consumed = offset;
2475 status = mci_readl(host, MINTSTS);
2476 mci_writel(host, RINTSTS, SDMMC_INT_TXDR);
2477 } while (status & SDMMC_INT_TXDR); /* if TXDR write again */
2480 if (!sg_miter_next(sg_miter))
2482 sg_miter->consumed = 0;
2484 sg_miter_stop(sg_miter);
2488 sg_miter_stop(sg_miter);
2492 set_bit(EVENT_XFER_COMPLETE, &host->pending_events);
2495 static void dw_mci_cmd_interrupt(struct dw_mci *host, u32 status)
2497 if (!host->cmd_status)
2498 host->cmd_status = status;
2502 set_bit(EVENT_CMD_COMPLETE, &host->pending_events);
2503 tasklet_schedule(&host->tasklet);
2506 static irqreturn_t dw_mci_interrupt(int irq, void *dev_id)
2508 struct dw_mci *host = dev_id;
2509 u32 pending, sdio_int;
2512 pending = mci_readl(host, MINTSTS); /* read-only mask reg */
2513 //if(host->mmc->restrict_caps & RESTRICT_CARD_TYPE_SD)
2514 // printk("%s pending: 0x%08x\n",__func__,pending);
2516 * DTO fix - version 2.10a and below, and only if internal DMA
2519 if (host->quirks & DW_MCI_QUIRK_IDMAC_DTO) {
2521 ((mci_readl(host, STATUS) >> 17) & 0x1fff))
2522 pending |= SDMMC_INT_DATA_OVER;
2526 if (pending & DW_MCI_CMD_ERROR_FLAGS) {
2527 mci_writel(host, RINTSTS, DW_MCI_CMD_ERROR_FLAGS);
2528 host->cmd_status = pending;
2530 MMC_DBG_INFO_FUNC(host->mmc,"Line%d..%s cmd_status INT=0x%x,[%s]",
2531 __LINE__, __FUNCTION__,host->cmd_status,mmc_hostname(host->mmc));
2533 set_bit(EVENT_CMD_COMPLETE, &host->pending_events);
2536 if (pending & DW_MCI_DATA_ERROR_FLAGS) {
2537 /* if there is an error report DATA_ERROR */
2538 mci_writel(host, RINTSTS, DW_MCI_DATA_ERROR_FLAGS);
2539 host->data_status = pending;
2541 set_bit(EVENT_DATA_ERROR, &host->pending_events);
2543 MMC_DBG_INFO_FUNC(host->mmc,"Line%d..%s data_status INT=0x%x,[%s]",
2544 __LINE__, __FUNCTION__,host->data_status,mmc_hostname(host->mmc));
2545 tasklet_schedule(&host->tasklet);
2548 if (pending & SDMMC_INT_DATA_OVER) {
2549 mci_writel(host, RINTSTS, SDMMC_INT_DATA_OVER);
2550 MMC_DBG_CMD_FUNC(host->mmc, "SDMMC_INT_DATA_OVER, INT-pending=0x%x. [%s]",pending,mmc_hostname(host->mmc));
2551 if (!host->data_status)
2552 host->data_status = pending;
2554 if (host->dir_status == DW_MCI_RECV_STATUS) {
2555 if (host->sg != NULL)
2556 dw_mci_read_data_pio(host, true);
2558 set_bit(EVENT_DATA_COMPLETE, &host->pending_events);
2559 tasklet_schedule(&host->tasklet);
2562 if (pending & SDMMC_INT_RXDR) {
2563 mci_writel(host, RINTSTS, SDMMC_INT_RXDR);
2564 if (host->dir_status == DW_MCI_RECV_STATUS && host->sg)
2565 dw_mci_read_data_pio(host, false);
2568 if (pending & SDMMC_INT_TXDR) {
2569 mci_writel(host, RINTSTS, SDMMC_INT_TXDR);
2570 if (host->dir_status == DW_MCI_SEND_STATUS && host->sg)
2571 dw_mci_write_data_pio(host);
2574 if (pending & SDMMC_INT_VSI) {
2575 MMC_DBG_SW_VOL_FUNC(host->mmc, "SDMMC_INT_VSI, INT-pending=0x%x. [%s]",pending,mmc_hostname(host->mmc));
2576 mci_writel(host, RINTSTS, SDMMC_INT_VSI);
2577 dw_mci_cmd_interrupt(host, pending);
2580 if (pending & SDMMC_INT_CMD_DONE) {
2581 MMC_DBG_CMD_FUNC(host->mmc, "SDMMC_INT_CMD_DONE, CMD = 0x%x, INT-pending=0x%x. [%s]",mci_readl(host, CMD),pending,mmc_hostname(host->mmc));
2582 mci_writel(host, RINTSTS, SDMMC_INT_CMD_DONE);
2583 dw_mci_cmd_interrupt(host, pending);
2586 if (pending & SDMMC_INT_CD) {
2587 mci_writel(host, RINTSTS, SDMMC_INT_CD);
2588 MMC_DBG_INFO_FUNC(host->mmc, "SDMMC_INT_CD, INT-pending=0x%x. [%s]", pending, mmc_hostname(host->mmc));
2589 wake_lock_timeout(&host->mmc->detect_wake_lock, 5 * HZ);
2590 queue_work(host->card_workqueue, &host->card_work);
2593 if (pending & SDMMC_INT_HLE) {
2594 mci_writel(host, RINTSTS, SDMMC_INT_HLE);
2595 MMC_DBG_CMD_FUNC(host->mmc, "SDMMC_INT_HLE INT-pending=0x%x. [%s]\n",pending,mmc_hostname(host->mmc));
2599 /* Handle SDIO Interrupts */
2600 for (i = 0; i < host->num_slots; i++) {
2601 struct dw_mci_slot *slot = host->slot[i];
2603 if (host->verid < DW_MMC_240A)
2604 sdio_int = SDMMC_INT_SDIO(i);
2606 sdio_int = SDMMC_INT_SDIO(i + 8);
2608 if (pending & sdio_int) {
2609 mci_writel(host, RINTSTS, sdio_int);
2610 mmc_signal_sdio_irq(slot->mmc);
2616 #ifdef CONFIG_MMC_DW_IDMAC
2617 /* Handle DMA interrupts */
2618 pending = mci_readl(host, IDSTS);
2619 if (pending & (SDMMC_IDMAC_INT_TI | SDMMC_IDMAC_INT_RI)) {
2620 mci_writel(host, IDSTS, SDMMC_IDMAC_INT_TI | SDMMC_IDMAC_INT_RI);
2621 mci_writel(host, IDSTS, SDMMC_IDMAC_INT_NI);
2622 host->dma_ops->complete(host);
2629 static void dw_mci_work_routine_card(struct work_struct *work)
2631 struct dw_mci *host = container_of(work, struct dw_mci, card_work);
2634 for (i = 0; i < host->num_slots; i++) {
2635 struct dw_mci_slot *slot = host->slot[i];
2636 struct mmc_host *mmc = slot->mmc;
2637 struct mmc_request *mrq;
2640 present = dw_mci_get_cd(mmc);
2641 while (present != slot->last_detect_state) {
2642 dev_dbg(&slot->mmc->class_dev, "card %s\n",
2643 present ? "inserted" : "removed");
2644 MMC_DBG_BOOT_FUNC(mmc, " The card is %s. ===!!!!!!==[%s]\n",
2645 present ? "inserted" : "removed.", mmc_hostname(mmc));
2647 rk_send_wakeup_key();//wake up system
2648 spin_lock_bh(&host->lock);
2650 /* Card change detected */
2651 slot->last_detect_state = present;
2653 /* Clean up queue if present */
2656 if (mrq == host->mrq) {
2660 switch (host->state) {
2663 case STATE_SENDING_CMD:
2664 mrq->cmd->error = -ENOMEDIUM;
2668 case STATE_SENDING_DATA:
2669 mrq->data->error = -ENOMEDIUM;
2670 dw_mci_stop_dma(host);
2672 case STATE_DATA_BUSY:
2673 case STATE_DATA_ERROR:
2674 if (mrq->data->error == -EINPROGRESS)
2675 mrq->data->error = -ENOMEDIUM;
2679 case STATE_SENDING_STOP:
2680 mrq->stop->error = -ENOMEDIUM;
2684 dw_mci_request_end(host, mrq);
2686 list_del(&slot->queue_node);
2687 mrq->cmd->error = -ENOMEDIUM;
2689 mrq->data->error = -ENOMEDIUM;
2691 mrq->stop->error = -ENOMEDIUM;
2693 MMC_DBG_CMD_FUNC(host->mmc, "dw_mci_work--reqeuest done, cmd=%d [%s]",mrq->cmd->opcode, mmc_hostname(mmc));
2695 spin_unlock(&host->lock);
2696 mmc_request_done(slot->mmc, mrq);
2697 spin_lock(&host->lock);
2701 /* Power down slot */
2703 /* Clear down the FIFO */
2704 dw_mci_fifo_reset(host);
2705 #ifdef CONFIG_MMC_DW_IDMAC
2706 dw_mci_idmac_reset(host);
2711 spin_unlock_bh(&host->lock);
2713 present = dw_mci_get_cd(mmc);
2716 mmc_detect_change(slot->mmc,
2717 msecs_to_jiffies(host->pdata->detect_delay_ms));
2722 /* given a slot id, find out the device node representing that slot */
2723 static struct device_node *dw_mci_of_find_slot_node(struct device *dev, u8 slot)
2725 struct device_node *np;
2729 if (!dev || !dev->of_node)
2732 for_each_child_of_node(dev->of_node, np) {
2733 addr = of_get_property(np, "reg", &len);
2734 if (!addr || (len < sizeof(int)))
2736 if (be32_to_cpup(addr) == slot)
2742 static struct dw_mci_of_slot_quirks {
2745 } of_slot_quirks[] = {
2747 .quirk = "disable-wp",
2748 .id = DW_MCI_SLOT_QUIRK_NO_WRITE_PROTECT,
2752 static int dw_mci_of_get_slot_quirks(struct device *dev, u8 slot)
2754 struct device_node *np = dw_mci_of_find_slot_node(dev, slot);
2759 for (idx = 0; idx < ARRAY_SIZE(of_slot_quirks); idx++)
2760 if (of_get_property(np, of_slot_quirks[idx].quirk, NULL))
2761 quirks |= of_slot_quirks[idx].id;
2766 /* find out bus-width for a given slot */
2767 static u32 dw_mci_of_get_bus_wd(struct device *dev, u8 slot)
2769 struct device_node *np = dev->of_node;//dw_mci_of_find_slot_node(dev, slot);
2775 if (of_property_read_u32(np, "bus-width", &bus_wd))
2776 dev_err(dev, "bus-width property not found, assuming width"
2782 /* find the pwr-en gpio for a given slot; or -1 if none specified */
2783 static int dw_mci_of_get_pwr_en_gpio(struct device *dev, u8 slot)
2785 struct device_node *np = dev->of_node;//dw_mci_of_find_slot_node(dev, slot);
2791 gpio = of_get_named_gpio(np, "pwr-gpios", 0);
2793 /* Having a missing entry is valid; return silently */
2794 if (!gpio_is_valid(gpio))
2797 if (devm_gpio_request(dev, gpio, "dw-mci-pwr_en")) {
2798 dev_warn(dev, "gpio [%d] request failed\n", gpio);
2802 gpio_direction_output(gpio, 0);//set 0 to pwr-en
2808 /* find the write protect gpio for a given slot; or -1 if none specified */
2809 static int dw_mci_of_get_wp_gpio(struct device *dev, u8 slot)
2811 struct device_node *np = dw_mci_of_find_slot_node(dev, slot);
2817 gpio = of_get_named_gpio(np, "wp-gpios", 0);
2819 /* Having a missing entry is valid; return silently */
2820 if (!gpio_is_valid(gpio))
2823 if (devm_gpio_request(dev, gpio, "dw-mci-wp")) {
2824 dev_warn(dev, "gpio [%d] request failed\n", gpio);
2831 /* find the cd gpio for a given slot */
2832 static void dw_mci_of_get_cd_gpio(struct device *dev, u8 slot,
2833 struct mmc_host *mmc)
2835 struct device_node *np = dev->of_node;//dw_mci_of_find_slot_node(dev, slot);
2841 gpio = of_get_named_gpio(np, "cd-gpios", 0);
2843 /* Having a missing entry is valid; return silently */
2844 if (!gpio_is_valid(gpio))
2847 if (mmc_gpio_request_cd(mmc, gpio, 0))
2848 dev_warn(dev, "gpio [%d] request failed\n", gpio);
2850 #else /* CONFIG_OF */
2851 static int dw_mci_of_get_slot_quirks(struct device *dev, u8 slot)
2855 static u32 dw_mci_of_get_bus_wd(struct device *dev, u8 slot)
2859 static struct device_node *dw_mci_of_find_slot_node(struct device *dev, u8 slot)
2863 static int dw_mci_of_get_wp_gpio(struct device *dev, u8 slot)
2867 static void dw_mci_of_get_cd_gpio(struct device *dev, u8 slot,
2868 struct mmc_host *mmc)
2872 #endif /* CONFIG_OF */
2874 static int dw_mci_init_slot(struct dw_mci *host, unsigned int id)
2876 struct mmc_host *mmc;
2877 struct dw_mci_slot *slot;
2878 const struct dw_mci_drv_data *drv_data = host->drv_data;
2883 mmc = mmc_alloc_host(sizeof(struct dw_mci_slot), host->dev);
2887 slot = mmc_priv(mmc);
2891 host->slot[id] = slot;
2894 slot->quirks = dw_mci_of_get_slot_quirks(host->dev, slot->id);
2896 mmc->ops = &dw_mci_ops;
2898 if (of_property_read_u32_array(host->dev->of_node,
2899 "clock-freq-min-max", freq, 2)) {
2900 mmc->f_min = DW_MCI_FREQ_MIN;
2901 mmc->f_max = DW_MCI_FREQ_MAX;
2903 printk("%d..%s: fmin=%d, fmax=%d [%s]\n", __LINE__,__FUNCTION__,
2904 mmc->f_min, mmc->f_max, mmc_hostname(mmc));
2906 mmc->f_min = freq[0];
2907 mmc->f_max = freq[1];
2909 printk("%d..%s: fmin=%d, fmax=%d [%s]\n", __LINE__,__FUNCTION__,
2910 mmc->f_min, mmc->f_max, mmc_hostname(mmc));
2913 if(strstr("mmc0",mmc_hostname(mmc)))
2914 printk("Line%d..%s: The rk_sdmmc %s",__LINE__, __FUNCTION__,RK_SDMMC_DRIVER_VERSION);
2916 if (of_find_property(host->dev->of_node, "supports-sd", NULL))
2917 mmc->restrict_caps |= RESTRICT_CARD_TYPE_SD;
2918 if (of_find_property(host->dev->of_node, "supports-sdio", NULL))
2919 mmc->restrict_caps |= RESTRICT_CARD_TYPE_SDIO;
2920 if (of_find_property(host->dev->of_node, "supports-emmc", NULL))
2921 mmc->restrict_caps |= RESTRICT_CARD_TYPE_EMMC;
2923 if (host->pdata->get_ocr)
2924 mmc->ocr_avail = host->pdata->get_ocr(id);
2927 mmc->ocr_avail = MMC_VDD_27_28| MMC_VDD_28_29| MMC_VDD_29_30| MMC_VDD_30_31
2928 | MMC_VDD_31_32| MMC_VDD_32_33| MMC_VDD_33_34| MMC_VDD_34_35| MMC_VDD_35_36
2929 | MMC_VDD_26_27| MMC_VDD_25_26| MMC_VDD_24_25| MMC_VDD_23_24
2930 | MMC_VDD_22_23| MMC_VDD_21_22| MMC_VDD_20_21| MMC_VDD_165_195;
2934 * Start with slot power disabled, it will be enabled when a card
2937 if (host->pdata->setpower)
2938 host->pdata->setpower(id, 0);
2940 if (host->pdata->caps)
2941 mmc->caps = host->pdata->caps;
2943 if (host->pdata->pm_caps)
2944 mmc->pm_caps = host->pdata->pm_caps;
2946 if (host->dev->of_node) {
2947 ctrl_id = of_alias_get_id(host->dev->of_node, "mshc");
2951 ctrl_id = to_platform_device(host->dev)->id;
2953 if (drv_data && drv_data->caps)
2954 mmc->caps |= drv_data->caps[ctrl_id];
2955 if (drv_data && drv_data->hold_reg_flag)
2956 mmc->hold_reg_flag |= drv_data->hold_reg_flag[ctrl_id];
2958 //set the compatibility of driver.
2959 mmc->caps |= MMC_CAP_UHS_SDR12|MMC_CAP_UHS_SDR25|MMC_CAP_UHS_SDR50|MMC_CAP_UHS_SDR104|MMC_CAP_ERASE ;
2961 if (host->pdata->caps2)
2962 mmc->caps2 = host->pdata->caps2;
2964 if (host->pdata->get_bus_wd)
2965 bus_width = host->pdata->get_bus_wd(slot->id);
2966 else if (host->dev->of_node)
2967 bus_width = dw_mci_of_get_bus_wd(host->dev, slot->id);
2971 switch (bus_width) {
2973 mmc->caps |= MMC_CAP_8_BIT_DATA;
2975 mmc->caps |= MMC_CAP_4_BIT_DATA;
2977 if (of_find_property(host->dev->of_node, "cap-power-off-card", NULL))
2978 mmc->caps |= MMC_CAP_POWER_OFF_CARD;
2979 if (of_find_property(host->dev->of_node, "cap-sdio-irq", NULL))
2980 mmc->caps |= MMC_CAP_SDIO_IRQ;
2981 if (of_find_property(host->dev->of_node, "poll-hw-reset", NULL))
2982 mmc->caps |= MMC_CAP_HW_RESET;
2983 if (of_find_property(host->dev->of_node, "full-pwr-cycle", NULL))
2984 mmc->caps2 |= MMC_CAP2_FULL_PWR_CYCLE;
2985 if (of_find_property(host->dev->of_node, "keep-power-in-suspend", NULL))
2986 mmc->pm_caps |= MMC_PM_KEEP_POWER;
2987 if (of_find_property(host->dev->of_node, "ignore-pm-notify", NULL))
2988 mmc->pm_caps |= MMC_PM_IGNORE_PM_NOTIFY;
2989 if (of_find_property(host->dev->of_node, "enable-sdio-wakeup", NULL))
2990 mmc->pm_caps |= MMC_PM_WAKE_SDIO_IRQ;
2992 /*Assign pm_caps pass to pm_flags*/
2993 mmc->pm_flags = mmc->pm_caps;
2995 if (host->pdata->blk_settings) {
2996 mmc->max_segs = host->pdata->blk_settings->max_segs;
2997 mmc->max_blk_size = host->pdata->blk_settings->max_blk_size;
2998 mmc->max_blk_count = host->pdata->blk_settings->max_blk_count;
2999 mmc->max_req_size = host->pdata->blk_settings->max_req_size;
3000 mmc->max_seg_size = host->pdata->blk_settings->max_seg_size;
3002 /* Useful defaults if platform data is unset. */
3003 #ifdef CONFIG_MMC_DW_IDMAC
3004 mmc->max_segs = host->ring_size;
3005 mmc->max_blk_size = 65536;
3006 mmc->max_blk_count = host->ring_size;
3007 mmc->max_seg_size = 0x1000;
3008 mmc->max_req_size = mmc->max_seg_size * mmc->max_blk_count;
3011 mmc->max_blk_size = 65536; /* BLKSIZ is 16 bits */
3012 mmc->max_blk_count = 512;
3013 mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
3014 mmc->max_seg_size = mmc->max_req_size;
3015 #endif /* CONFIG_MMC_DW_IDMAC */
3018 slot->pwr_en_gpio = dw_mci_of_get_pwr_en_gpio(host->dev, slot->id);
3020 if (!(mmc->restrict_caps & RESTRICT_CARD_TYPE_SD))//(gpio_is_valid(slot->pwr_en_gpio))
3025 if(mmc->restrict_caps & RESTRICT_CARD_TYPE_SD)
3027 host->vmmc = devm_regulator_get(mmc_dev(mmc), "vmmc");
3034 if (IS_ERR(host->vmmc)) {
3035 pr_info("%s: no vmmc regulator found\n", mmc_hostname(mmc));
3038 ret = regulator_enable(host->vmmc);
3041 "failed to enable regulator: %d\n", ret);
3047 slot->wp_gpio = dw_mci_of_get_wp_gpio(host->dev, slot->id);
3049 if (mmc->restrict_caps & RESTRICT_CARD_TYPE_SDIO)
3050 clear_bit(DW_MMC_CARD_PRESENT, &slot->flags);
3052 ret = mmc_add_host(mmc);
3056 /* Pinctrl set default iomux state to fucntion port.
3057 * Fixme: DON'T TOUCH EMMC SETTING!
3059 if(!(host->mmc->restrict_caps & RESTRICT_CARD_TYPE_EMMC))
3061 host->pinctrl = devm_pinctrl_get(host->dev);
3062 if(IS_ERR(host->pinctrl))
3063 printk("%s: Warning : No pinctrl used!\n",mmc_hostname(host->mmc));
3066 host->pins_idle= pinctrl_lookup_state(host->pinctrl,PINCTRL_STATE_IDLE);
3067 if(IS_ERR(host->pins_default))
3068 printk("%s: Warning : No IDLE pinctrl matched!\n", mmc_hostname(host->mmc));
3071 if(pinctrl_select_state(host->pinctrl, host->pins_idle) < 0)
3072 printk("%s: Warning : Idle pinctrl setting failed!\n", mmc_hostname(host->mmc));
3075 host->pins_default = pinctrl_lookup_state(host->pinctrl,PINCTRL_STATE_DEFAULT);
3076 if(IS_ERR(host->pins_default))
3077 printk("%s: Warning : No default pinctrl matched!\n", mmc_hostname(host->mmc));
3080 if(pinctrl_select_state(host->pinctrl, host->pins_default) < 0)
3081 printk("%s: Warning : Default pinctrl setting failed!\n", mmc_hostname(host->mmc));
3087 #if defined(CONFIG_DEBUG_FS)
3088 dw_mci_init_debugfs(slot);
3091 /* Card initially undetected */
3092 slot->last_detect_state = 1;
3101 static void dw_mci_cleanup_slot(struct dw_mci_slot *slot, unsigned int id)
3103 /* Shutdown detect IRQ */
3104 if (slot->host->pdata->exit)
3105 slot->host->pdata->exit(id);
3107 /* Debugfs stuff is cleaned up by mmc core */
3108 mmc_remove_host(slot->mmc);
3109 slot->host->slot[id] = NULL;
3110 mmc_free_host(slot->mmc);
3113 static void dw_mci_init_dma(struct dw_mci *host)
3115 /* Alloc memory for sg translation */
3116 host->sg_cpu = dmam_alloc_coherent(host->dev, PAGE_SIZE,
3117 &host->sg_dma, GFP_KERNEL);
3118 if (!host->sg_cpu) {
3119 dev_err(host->dev, "%s: could not alloc DMA memory\n",
3124 /* Determine which DMA interface to use */
3125 #ifdef CONFIG_MMC_DW_IDMAC
3126 host->dma_ops = &dw_mci_idmac_ops;
3127 dev_info(host->dev, "Using internal DMA controller.\n");
3133 if (host->dma_ops->init && host->dma_ops->start &&
3134 host->dma_ops->stop && host->dma_ops->cleanup) {
3135 if (host->dma_ops->init(host)) {
3136 dev_err(host->dev, "%s: Unable to initialize "
3137 "DMA Controller.\n", __func__);
3141 dev_err(host->dev, "DMA initialization not found.\n");
3149 dev_info(host->dev, "Using PIO mode.\n");
3154 static bool dw_mci_ctrl_reset(struct dw_mci *host, u32 reset)
3156 unsigned long timeout = jiffies + msecs_to_jiffies(500);
3159 ctrl = mci_readl(host, CTRL);
3161 mci_writel(host, CTRL, ctrl);
3163 /* wait till resets clear */
3165 ctrl = mci_readl(host, CTRL);
3166 if (!(ctrl & reset))
3168 } while (time_before(jiffies, timeout));
3171 "Timeout resetting block (ctrl reset %#x)\n",
3177 static inline bool dw_mci_fifo_reset(struct dw_mci *host)
3180 * Reseting generates a block interrupt, hence setting
3181 * the scatter-gather pointer to NULL.
3184 sg_miter_stop(&host->sg_miter);
3188 return dw_mci_ctrl_reset(host, SDMMC_CTRL_FIFO_RESET);
3191 static inline bool dw_mci_ctrl_all_reset(struct dw_mci *host)
3193 return dw_mci_ctrl_reset(host,
3194 SDMMC_CTRL_FIFO_RESET |
3196 SDMMC_CTRL_DMA_RESET);
3201 static struct dw_mci_of_quirks {
3206 .quirk = "broken-cd",
3207 .id = DW_MCI_QUIRK_BROKEN_CARD_DETECTION,
3211 static struct dw_mci_board *dw_mci_parse_dt(struct dw_mci *host)
3213 struct dw_mci_board *pdata;
3214 struct device *dev = host->dev;
3215 struct device_node *np = dev->of_node;
3216 const struct dw_mci_drv_data *drv_data = host->drv_data;
3218 u32 clock_frequency;
3220 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
3222 dev_err(dev, "could not allocate memory for pdata\n");
3223 return ERR_PTR(-ENOMEM);
3226 /* find out number of slots supported */
3227 if (of_property_read_u32(dev->of_node, "num-slots",
3228 &pdata->num_slots)) {
3229 dev_info(dev, "num-slots property not found, "
3230 "assuming 1 slot is available\n");
3231 pdata->num_slots = 1;
3235 for (idx = 0; idx < ARRAY_SIZE(of_quirks); idx++)
3236 if (of_get_property(np, of_quirks[idx].quirk, NULL))
3237 pdata->quirks |= of_quirks[idx].id;
3240 if (of_property_read_u32(np, "fifo-depth", &pdata->fifo_depth))
3241 dev_info(dev, "fifo-depth property not found, using "
3242 "value of FIFOTH register as default\n");
3244 of_property_read_u32(np, "card-detect-delay", &pdata->detect_delay_ms);
3246 if (!of_property_read_u32(np, "clock-frequency", &clock_frequency))
3247 pdata->bus_hz = clock_frequency;
3249 if (drv_data && drv_data->parse_dt) {
3250 ret = drv_data->parse_dt(host);
3252 return ERR_PTR(ret);
3255 if (of_find_property(np, "keep-power-in-suspend", NULL))
3256 pdata->pm_caps |= MMC_PM_KEEP_POWER;
3258 if (of_find_property(np, "enable-sdio-wakeup", NULL))
3259 pdata->pm_caps |= MMC_PM_WAKE_SDIO_IRQ;
3261 if (of_find_property(np, "supports-highspeed", NULL))
3262 pdata->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED;
3264 if (of_find_property(np, "supports-UHS_SDR104", NULL))
3265 pdata->caps |= MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_SDR50;
3267 if (of_find_property(np, "supports-DDR_MODE", NULL))
3268 pdata->caps |= MMC_CAP_1_8V_DDR | MMC_CAP_1_2V_DDR;
3270 if (of_find_property(np, "caps2-mmc-hs200", NULL))
3271 pdata->caps2 |= MMC_CAP2_HS200;
3273 if (of_find_property(np, "caps2-mmc-hs200-1_8v", NULL))
3274 pdata->caps2 |= MMC_CAP2_HS200_1_8V_SDR;
3276 if (of_find_property(np, "caps2-mmc-hs200-1_2v", NULL))
3277 pdata->caps2 |= MMC_CAP2_HS200_1_2V_SDR;
3279 if (of_get_property(np, "cd-inverted", NULL))
3280 pdata->caps2 |= MMC_CAP2_CD_ACTIVE_HIGH;
3281 if (of_get_property(np, "bootpart-no-access", NULL))
3282 pdata->caps2 |= MMC_CAP2_BOOTPART_NOACC;
3287 #else /* CONFIG_OF */
3288 static struct dw_mci_board *dw_mci_parse_dt(struct dw_mci *host)
3290 return ERR_PTR(-EINVAL);
3292 #endif /* CONFIG_OF */
3294 int dw_mci_probe(struct dw_mci *host)
3296 const struct dw_mci_drv_data *drv_data = host->drv_data;
3297 int width, i, ret = 0;
3303 host->pdata = dw_mci_parse_dt(host);
3304 if (IS_ERR(host->pdata)) {
3305 dev_err(host->dev, "platform data not available\n");
3310 if (!host->pdata->select_slot && host->pdata->num_slots > 1) {
3312 "Platform data must supply select_slot function\n");
3317 * In 2.40a spec, Data offset is changed.
3318 * Need to check the version-id and set data-offset for DATA register.
3320 host->verid = SDMMC_GET_VERID(mci_readl(host, VERID));
3321 dev_info(host->dev, "Version ID is %04x\n", host->verid);
3323 if (host->verid < DW_MMC_240A)
3324 host->data_offset = DATA_OFFSET;
3326 host->data_offset = DATA_240A_OFFSET;
3329 host->hclk_mmc= devm_clk_get(host->dev, "hclk_mmc");
3330 if (IS_ERR(host->hclk_mmc)) {
3331 dev_err(host->dev, "failed to get hclk_mmc\n");
3332 ret = PTR_ERR(host->hclk_mmc);
3336 clk_prepare_enable(host->hclk_mmc);
3339 host->clk_mmc = devm_clk_get(host->dev, "clk_mmc");
3340 if (IS_ERR(host->clk_mmc)) {
3341 dev_err(host->dev, "failed to get clk mmc_per\n");
3342 ret = PTR_ERR(host->clk_mmc);
3346 host->bus_hz = host->pdata->bus_hz;
3347 if (!host->bus_hz) {
3348 dev_err(host->dev,"Platform data must supply bus speed\n");
3353 if (host->verid < DW_MMC_240A)
3354 ret = clk_set_rate(host->clk_mmc, host->bus_hz);
3356 //rockchip: fix divider 2 in clksum before controlller
3357 ret = clk_set_rate(host->clk_mmc, host->bus_hz * 2);
3360 dev_err(host->dev, "failed to set clk mmc\n");
3363 clk_prepare_enable(host->clk_mmc);
3365 if (drv_data && drv_data->setup_clock) {
3366 ret = drv_data->setup_clock(host);
3369 "implementation specific clock setup failed\n");
3374 host->quirks = host->pdata->quirks;
3375 host->irq_state = true;
3376 host->set_speed = 0;
3379 spin_lock_init(&host->lock);
3380 INIT_LIST_HEAD(&host->queue);
3383 * Get the host data width - this assumes that HCON has been set with
3384 * the correct values.
3386 i = (mci_readl(host, HCON) >> 7) & 0x7;
3388 host->push_data = dw_mci_push_data16;
3389 host->pull_data = dw_mci_pull_data16;
3391 host->data_shift = 1;
3392 } else if (i == 2) {
3393 host->push_data = dw_mci_push_data64;
3394 host->pull_data = dw_mci_pull_data64;
3396 host->data_shift = 3;
3398 /* Check for a reserved value, and warn if it is */
3400 "HCON reports a reserved host data width!\n"
3401 "Defaulting to 32-bit access.\n");
3402 host->push_data = dw_mci_push_data32;
3403 host->pull_data = dw_mci_pull_data32;
3405 host->data_shift = 2;
3408 /* Reset all blocks */
3409 if (!dw_mci_ctrl_all_reset(host))
3412 host->dma_ops = host->pdata->dma_ops;
3413 dw_mci_init_dma(host);
3415 /* Clear the interrupts for the host controller */
3416 mci_writel(host, RINTSTS, 0xFFFFFFFF);
3417 mci_writel(host, INTMASK, 0); /* disable all mmc interrupt first */
3419 /* Put in max timeout */
3420 mci_writel(host, TMOUT, 0xFFFFFFFF);
3423 * FIFO threshold settings RxMark = fifo_size / 2 - 1,
3424 * Tx Mark = fifo_size / 2 DMA Size = 8
3426 if (!host->pdata->fifo_depth) {
3428 * Power-on value of RX_WMark is FIFO_DEPTH-1, but this may
3429 * have been overwritten by the bootloader, just like we're
3430 * about to do, so if you know the value for your hardware, you
3431 * should put it in the platform data.
3433 fifo_size = mci_readl(host, FIFOTH);
3434 fifo_size = 1 + ((fifo_size >> 16) & 0xfff);
3436 fifo_size = host->pdata->fifo_depth;
3438 host->fifo_depth = fifo_size;
3440 SDMMC_SET_FIFOTH(0x2, fifo_size / 2 - 1, fifo_size / 2);
3441 mci_writel(host, FIFOTH, host->fifoth_val);
3443 /* disable clock to CIU */
3444 mci_writel(host, CLKENA, 0);
3445 mci_writel(host, CLKSRC, 0);
3447 tasklet_init(&host->tasklet, dw_mci_tasklet_func, (unsigned long)host);
3448 host->card_workqueue = alloc_workqueue("dw-mci-card",
3449 WQ_MEM_RECLAIM | WQ_NON_REENTRANT, 1);
3450 if (!host->card_workqueue) {
3454 INIT_WORK(&host->card_work, dw_mci_work_routine_card);
3455 ret = devm_request_irq(host->dev, host->irq, dw_mci_interrupt,
3456 host->irq_flags, "dw-mci", host);
3460 if (host->pdata->num_slots)
3461 host->num_slots = host->pdata->num_slots;
3463 host->num_slots = ((mci_readl(host, HCON) >> 1) & 0x1F) + 1;
3465 /* We need at least one slot to succeed */
3466 for (i = 0; i < host->num_slots; i++) {
3467 ret = dw_mci_init_slot(host, i);
3469 dev_dbg(host->dev, "slot %d init failed\n", i);
3475 * Enable interrupts for command done, data over, data empty, card det,
3476 * receive ready and error such as transmit, receive timeout, crc error
3478 mci_writel(host, RINTSTS, 0xFFFFFFFF);
3479 regs = SDMMC_INT_CMD_DONE | SDMMC_INT_DATA_OVER | SDMMC_INT_TXDR | SDMMC_INT_VSI |
3480 SDMMC_INT_RXDR | DW_MCI_ERROR_FLAGS;
3481 if(!(host->mmc->restrict_caps & RESTRICT_CARD_TYPE_SDIO)
3482 && !(host->mmc->restrict_caps & RESTRICT_CARD_TYPE_EMMC))
3483 regs |= SDMMC_INT_CD;
3485 mci_writel(host, INTMASK, regs);
3487 mci_writel(host, CTRL, SDMMC_CTRL_INT_ENABLE); /* Enable mci interrupt */
3489 dev_info(host->dev, "DW MMC controller at irq %d, "
3490 "%d bit host data width, "
3492 host->irq, width, fifo_size);
3495 dev_info(host->dev, "%d slots initialized\n", init_slots);
3497 dev_dbg(host->dev, "attempted to initialize %d slots, "
3498 "but failed on all\n", host->num_slots);
3503 if (host->quirks & DW_MCI_QUIRK_IDMAC_DTO)
3504 dev_info(host->dev, "Internal DMAC interrupt fix enabled.\n");
3509 destroy_workqueue(host->card_workqueue);
3512 if (host->use_dma && host->dma_ops->exit)
3513 host->dma_ops->exit(host);
3516 regulator_disable(host->vmmc);
3517 regulator_put(host->vmmc);
3521 if (!IS_ERR(host->clk_mmc))
3522 clk_disable_unprepare(host->clk_mmc);
3524 if (!IS_ERR(host->hclk_mmc))
3525 clk_disable_unprepare(host->hclk_mmc);
3529 EXPORT_SYMBOL(dw_mci_probe);
3531 void dw_mci_remove(struct dw_mci *host)
3535 mci_writel(host, RINTSTS, 0xFFFFFFFF);
3536 mci_writel(host, INTMASK, 0); /* disable all mmc interrupt first */
3538 for (i = 0; i < host->num_slots; i++) {
3539 dev_dbg(host->dev, "remove slot %d\n", i);
3541 dw_mci_cleanup_slot(host->slot[i], i);
3544 /* disable clock to CIU */
3545 mci_writel(host, CLKENA, 0);
3546 mci_writel(host, CLKSRC, 0);
3548 destroy_workqueue(host->card_workqueue);
3550 if (host->use_dma && host->dma_ops->exit)
3551 host->dma_ops->exit(host);
3554 regulator_disable(host->vmmc);
3555 regulator_put(host->vmmc);
3558 if (!IS_ERR(host->clk_mmc))
3559 clk_disable_unprepare(host->clk_mmc);
3561 if (!IS_ERR(host->hclk_mmc))
3562 clk_disable_unprepare(host->hclk_mmc);
3564 EXPORT_SYMBOL(dw_mci_remove);
3568 #ifdef CONFIG_PM_SLEEP
3570 * TODO: we should probably disable the clock to the card in the suspend path.
3572 int dw_mci_suspend(struct dw_mci *host)
3576 regulator_disable(host->vmmc);
3578 /*only for sdmmc controller*/
3579 if(host->mmc->restrict_caps & RESTRICT_CARD_TYPE_SD) {
3580 host->mmc->rescan_disable = 1;
3581 if (cancel_delayed_work_sync(&host->mmc->detect)){
3582 wake_unlock(&host->mmc->detect_wake_lock);
3584 disable_irq(host->irq);
3585 if(pinctrl_select_state(host->pinctrl, host->pins_idle) < 0)
3586 printk("%s: Warning : Idle pinctrl setting failed!\n", mmc_hostname(host->mmc));
3587 dw_mci_of_get_cd_gpio(host->dev,0,host->mmc);
3588 mci_writel(host, RINTSTS, 0xFFFFFFFF);
3589 mci_writel(host, INTMASK, 0x00);
3590 mci_writel(host, CTRL, 0x00);
3591 enable_irq_wake(host->mmc->slot.cd_irq);
3595 EXPORT_SYMBOL(dw_mci_suspend);
3597 int dw_mci_resume(struct dw_mci *host)
3599 int i, ret, retry_cnt = 0;
3601 struct dw_mci_slot *slot;
3603 if (host->mmc->restrict_caps & RESTRICT_CARD_TYPE_SDIO){
3604 slot = mmc_priv(host->mmc);
3606 if(!test_bit(DW_MMC_CARD_PRESENT, &slot->flags))
3609 /*only for sdmmc controller*/
3610 if(host->mmc->restrict_caps & RESTRICT_CARD_TYPE_SD) {
3611 disable_irq_wake(host->mmc->slot.cd_irq);
3612 mmc_gpio_free_cd(host->mmc);
3613 if(pinctrl_select_state(host->pinctrl, host->pins_default) < 0)
3614 printk("%s: Warning : Default pinctrl setting failed!\n", mmc_hostname(host->mmc));
3615 host->mmc->rescan_disable = 0;
3616 if(cpu_is_rk3288()) {
3617 grf_writel(((1<<12)<<16)|(0<<12), RK3288_GRF_SOC_CON0);//disable jtag
3621 ret = regulator_enable(host->vmmc);
3624 "failed to enable regulator: %d\n", ret);
3629 if (!dw_mci_ctrl_all_reset(host)) {
3634 if (host->use_dma && host->dma_ops->init)
3635 host->dma_ops->init(host);
3638 * Restore the initial value at FIFOTH register
3639 * And Invalidate the prev_blksz with zero
3641 mci_writel(host, FIFOTH, host->fifoth_val);
3642 host->prev_blksz = 0;
3643 /* Put in max timeout */
3644 mci_writel(host, TMOUT, 0xFFFFFFFF);
3646 mci_writel(host, RINTSTS, 0xFFFFFFFF);
3647 regs = SDMMC_INT_CMD_DONE | SDMMC_INT_DATA_OVER | SDMMC_INT_TXDR | SDMMC_INT_RXDR | SDMMC_INT_VSI |
3649 if(!(host->mmc->restrict_caps & RESTRICT_CARD_TYPE_SDIO))
3650 regs |= SDMMC_INT_CD;
3651 mci_writel(host, INTMASK, regs);
3652 mci_writel(host, CTRL, SDMMC_CTRL_INT_ENABLE);
3653 /*only for sdmmc controller*/
3654 if((host->mmc->restrict_caps & RESTRICT_CARD_TYPE_SD)&& (!retry_cnt)) {
3655 enable_irq(host->irq);
3659 for (i = 0; i < host->num_slots; i++) {
3660 struct dw_mci_slot *slot = host->slot[i];
3663 if (slot->mmc->pm_flags & MMC_PM_KEEP_POWER) {
3664 dw_mci_set_ios(slot->mmc, &slot->mmc->ios);
3665 dw_mci_setup_bus(slot, true);
3671 EXPORT_SYMBOL(dw_mci_resume);
3672 #endif /* CONFIG_PM_SLEEP */
3674 static int __init dw_mci_init(void)
3676 pr_info("Synopsys Designware Multimedia Card Interface Driver\n");
3680 static void __exit dw_mci_exit(void)
3684 module_init(dw_mci_init);
3685 module_exit(dw_mci_exit);
3687 MODULE_DESCRIPTION("DW Multimedia Card Interface driver");
3689 MODULE_AUTHOR("NXP Semiconductor VietNam");
3690 MODULE_AUTHOR("Imagination Technologies Ltd");
3691 MODULE_AUTHOR("Rockchip Electronics£¬Bangwang Xie < xbw@rock-chips.com> ");
3693 MODULE_LICENSE("GPL v2");