mmc: rk_sdmmc: local reversion and code regulation
[firefly-linux-kernel-4.4.55.git] / drivers / mmc / host / rk_sdmmc.c
1 /*
2  * Synopsys DesignWare Multimedia Card Interface driver
3  *  (Based on NXP driver for lpc 31xx)
4  *
5  * Copyright (C) 2009 NXP Semiconductors
6  * Copyright (C) 2009, 2010 Imagination Technologies Ltd.
7  *
8  * Copyright (C) 2014 - 2015 Fuzhou Rockchip Electronics Co.Ltd.
9  *
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.
14  */
15
16 #include <linux/blkdev.h>
17 #include <linux/clk.h>
18 #include <linux/debugfs.h>
19 #include <linux/device.h>
20 #include <linux/dma-mapping.h>
21 #include <linux/dmaengine.h>
22 #include <linux/err.h>
23 #include <linux/init.h>
24 #include <linux/interrupt.h>
25 #include <linux/ioport.h>
26 #include <linux/module.h>
27 #include <linux/platform_device.h>
28 #include <linux/seq_file.h>
29 #include <linux/slab.h>
30 #include <linux/stat.h>
31 #include <linux/delay.h>
32 #include <linux/irq.h>
33 #include <linux/suspend.h>
34 #include <linux/mmc/host.h>
35 #include <linux/mmc/mmc.h>
36 #include <linux/mmc/sd.h>
37 #include <linux/mmc/card.h>
38 #include <linux/mmc/sdio.h>
39 #include <linux/mmc/rk_mmc.h>
40 #include <linux/bitops.h>
41 #include <linux/regulator/consumer.h>
42 #include <linux/workqueue.h>
43 #include <linux/of.h>
44 #include <linux/of_gpio.h>
45 #include <linux/mmc/slot-gpio.h>
46 #include <linux/clk-private.h>
47 #include <linux/rockchip/cpu.h>
48 #include <linux/rfkill-wlan.h>
49 #include <linux/mfd/syscon.h>
50 #include <linux/regmap.h>
51 #include <linux/log2.h>
52 #include "rk_sdmmc.h"
53 #include "rk_sdmmc_dbg.h"
54 #include <linux/regulator/rockchip_io_vol_domain.h>
55 #include "../../clk/rockchip/clk-ops.h"
56
57 #define RK_SDMMC_DRIVER_VERSION "Ver 2.00 2015-06-10"
58
59 /* Common flag combinations */
60 #define DW_MCI_DATA_ERROR_FLAGS (SDMMC_INT_DRTO | SDMMC_INT_DCRC | \
61                                  SDMMC_INT_SBE  | \
62                                  SDMMC_INT_EBE)
63 #define DW_MCI_CMD_ERROR_FLAGS  (SDMMC_INT_RTO | SDMMC_INT_RCRC | \
64                                  SDMMC_INT_RESP_ERR)
65 #define DW_MCI_ERROR_FLAGS      (DW_MCI_DATA_ERROR_FLAGS | \
66                                  DW_MCI_CMD_ERROR_FLAGS  | SDMMC_INT_HLE)
67 #define DW_MCI_SEND_STATUS      1
68 #define DW_MCI_RECV_STATUS      2
69 #define DW_MCI_DMA_THRESHOLD    16
70 #define DW_MCI_FREQ_MAX         50000000        /* unit: HZ */
71 #define DW_MCI_FREQ_MIN         300000          /* unit: HZ */
72
73 /* Max is 250ms showed in Spec */
74 #define SDMMC_DATA_TIMEOUT_SD   500
75 #define SDMMC_DATA_TIMEOUT_SDIO 250
76 #define SDMMC_DATA_TIMEOUT_EMMC 2500
77 #define SDMMC_CMD_RTO_MAX_HOLD  200
78 #define SDMMC_WAIT_FOR_UNBUSY   2500
79
80 #define DW_REGS_SIZE    (0x0098 + 4)
81 #define DW_REGS_NUM     (0x0098 / 4)
82
83 #ifdef CONFIG_MMC_DW_IDMAC
84 #define IDMAC_INT_CLR           (SDMMC_IDMAC_INT_AI | SDMMC_IDMAC_INT_NI | \
85                                  SDMMC_IDMAC_INT_CES | SDMMC_IDMAC_INT_DU | \
86                                  SDMMC_IDMAC_INT_FBE | SDMMC_IDMAC_INT_RI | \
87                                  SDMMC_IDMAC_INT_TI)
88
89 struct idmac_desc {
90         u32             des0;   /* Control Descriptor */
91 #define IDMAC_DES0_DIC  BIT(1)
92 #define IDMAC_DES0_LD   BIT(2)
93 #define IDMAC_DES0_FD   BIT(3)
94 #define IDMAC_DES0_CH   BIT(4)
95 #define IDMAC_DES0_ER   BIT(5)
96 #define IDMAC_DES0_CES  BIT(30)
97 #define IDMAC_DES0_OWN  BIT(31)
98
99         u32             des1;   /* Buffer sizes */
100 #define IDMAC_SET_BUFFER1_SIZE(d, s) \
101         ((d)->des1 = ((d)->des1 & 0x03ffe000) | ((s) & 0x1fff))
102
103         u32             des2;   /* buffer 1 physical address */
104
105         u32             des3;   /* buffer 2 physical address */
106 };
107 #endif /* CONFIG_MMC_DW_IDMAC */
108
109
110 static inline bool dw_mci_fifo_reset(struct dw_mci *host);
111 static inline bool dw_mci_ctrl_all_reset(struct dw_mci *host);
112 static bool dw_mci_ctrl_reset(struct dw_mci *host, u32 reset);
113 static void dw_mci_disable_low_power(struct dw_mci_slot *slot);
114 extern void rk_send_wakeup_key(void);
115
116 #if defined(CONFIG_DEBUG_FS)
117 static int dw_mci_req_show(struct seq_file *s, void *v)
118 {
119         struct dw_mci_slot *slot = s->private;
120         struct mmc_request *mrq;
121         struct mmc_command *cmd;
122         struct mmc_command *stop;
123         struct mmc_data *data;
124
125         /* Make sure we get a consistent snapshot */
126         spin_lock_bh(&slot->host->lock);
127         mrq = slot->mrq;
128
129         if (mrq) {
130                 cmd = mrq->cmd;
131                 data = mrq->data;
132                 stop = mrq->stop;
133
134                 if (cmd)
135                         seq_printf(s,
136                                    "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n",
137                                    cmd->opcode, cmd->arg, cmd->flags,
138                                    cmd->resp[0], cmd->resp[1], cmd->resp[2],
139                                    cmd->resp[2], cmd->error);
140                 if (data)
141                         seq_printf(s, "DATA %u / %u * %u flg %x err %d\n",
142                                    data->bytes_xfered, data->blocks,
143                                    data->blksz, data->flags, data->error);
144                 if (stop)
145                         seq_printf(s,
146                                    "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n",
147                                    stop->opcode, stop->arg, stop->flags,
148                                    stop->resp[0], stop->resp[1], stop->resp[2],
149                                    stop->resp[2], stop->error);
150         }
151
152         spin_unlock_bh(&slot->host->lock);
153
154         return 0;
155 }
156
157 static int dw_mci_req_open(struct inode *inode, struct file *file)
158 {
159         return single_open(file, dw_mci_req_show, inode->i_private);
160 }
161
162 static const struct file_operations dw_mci_req_fops = {
163         .owner          = THIS_MODULE,
164         .open           = dw_mci_req_open,
165         .read           = seq_read,
166         .llseek         = seq_lseek,
167         .release        = single_release,
168 };
169
170 static int dw_mci_regs_show(struct seq_file *s, void *v)
171 {
172         seq_printf(s, "STATUS:\t0x%08x\n", SDMMC_STATUS);
173         seq_printf(s, "RINTSTS:\t0x%08x\n", SDMMC_RINTSTS);
174         seq_printf(s, "CMD:\t0x%08x\n", SDMMC_CMD);
175         seq_printf(s, "CTRL:\t0x%08x\n", SDMMC_CTRL);
176         seq_printf(s, "INTMASK:\t0x%08x\n", SDMMC_INTMASK);
177         seq_printf(s, "CLKENA:\t0x%08x\n", SDMMC_CLKENA);
178
179         return 0;
180 }
181
182 static int dw_mci_regs_open(struct inode *inode, struct file *file)
183 {
184         return single_open(file, dw_mci_regs_show, inode->i_private);
185 }
186
187 static const struct file_operations dw_mci_regs_fops = {
188         .owner          = THIS_MODULE,
189         .open           = dw_mci_regs_open,
190         .read           = seq_read,
191         .llseek         = seq_lseek,
192         .release        = single_release,
193 };
194
195 static void dw_mci_init_debugfs(struct dw_mci_slot *slot)
196 {
197         struct mmc_host *mmc = slot->mmc;
198         struct dw_mci *host = slot->host;
199         struct dentry *root;
200         struct dentry *node;
201
202         root = mmc->debugfs_root;
203         if (!root)
204                 return;
205
206         node = debugfs_create_file("regs", S_IRUSR, root, host,
207                                    &dw_mci_regs_fops);
208         if (!node)
209                 goto err;
210
211         node = debugfs_create_file("req", S_IRUSR, root, slot,
212                                    &dw_mci_req_fops);
213         if (!node)
214                 goto err;
215
216         node = debugfs_create_u32("state", S_IRUSR, root, (u32 *)&host->state);
217         if (!node)
218                 goto err;
219
220         node = debugfs_create_x32("pending_events", S_IRUSR, root,
221                                   (u32 *)&host->pending_events);
222         if (!node)
223                 goto err;
224
225         node = debugfs_create_x32("completed_events", S_IRUSR, root,
226                                   (u32 *)&host->completed_events);
227         if (!node)
228                 goto err;
229
230         return;
231
232 err:
233         dev_err(&mmc->class_dev, "failed to initialize debugfs for slot\n");
234 }
235 #endif /* defined(CONFIG_DEBUG_FS) */
236
237 static void dw_mci_set_timeout(struct dw_mci *host)
238 {
239         /* timeout (maximum) */
240         mci_writel(host, TMOUT, 0xffffffff);
241 }
242
243 static u32 dw_mci_prepare_command(struct mmc_host *mmc, struct mmc_command *cmd)
244 {
245         struct mmc_data *data;
246         struct dw_mci_slot *slot = mmc_priv(mmc);
247         const struct dw_mci_drv_data *drv_data = slot->host->drv_data;
248         u32 cmdr;
249         cmd->error = -EINPROGRESS;
250
251         cmdr = cmd->opcode;
252
253         if (cmdr == MMC_STOP_TRANSMISSION)
254                 cmdr |= SDMMC_CMD_STOP;
255         else
256                 cmdr |= SDMMC_CMD_PRV_DAT_WAIT;
257
258         if (cmd->flags & MMC_RSP_PRESENT) {
259                 /* We expect a response, so set this bit */
260                 cmdr |= SDMMC_CMD_RESP_EXP;
261                 if (cmd->flags & MMC_RSP_136)
262                         cmdr |= SDMMC_CMD_RESP_LONG;
263         }
264
265         if (cmd->flags & MMC_RSP_CRC)
266                 cmdr |= SDMMC_CMD_RESP_CRC;
267
268         data = cmd->data;
269         if (data) {
270                 cmdr |= SDMMC_CMD_DAT_EXP;
271                 if (data->flags & MMC_DATA_STREAM)
272                         cmdr |= SDMMC_CMD_STRM_MODE;
273                 if (data->flags & MMC_DATA_WRITE)
274                         cmdr |= SDMMC_CMD_DAT_WR;
275         }
276
277         if (drv_data && drv_data->prepare_command)
278                 drv_data->prepare_command(slot->host, &cmdr);
279
280         return cmdr;
281 }
282
283 static void dw_mci_start_command(struct dw_mci *host,
284                                  struct mmc_command *cmd, u32 cmd_flags)
285 {
286         struct dw_mci_slot *slot = host->slot[0];
287
288         host->pre_cmd = host->cmd;
289         host->cmd = cmd;
290         dev_vdbg(host->dev,
291                 "start command: ARGR=0x%08x CMDR=0x%08x\n",
292                 cmd->arg, cmd_flags);
293
294         if(SD_SWITCH_VOLTAGE == cmd->opcode){
295                 /*confirm non-low-power mode*/
296                 mci_writel(host, CMDARG, 0);
297                 dw_mci_disable_low_power(slot);
298                 
299                 MMC_DBG_INFO_FUNC(host->mmc,"Line%d..%s before start cmd=11,[%s]",
300                         __LINE__, __FUNCTION__,mmc_hostname(host->mmc));
301
302                 cmd_flags |= SDMMC_CMD_VOLT_SWITCH;
303         }
304
305         mci_writel(host, CMDARG, cmd->arg);
306         wmb();
307         
308         /* Fix the value to 1 in current soc */
309         if(host->mmc->hold_reg_flag)
310                 cmd_flags |= SDMMC_CMD_USE_HOLD_REG;
311
312         mci_writel(host, CMD, cmd_flags | SDMMC_CMD_START);
313         wmb();
314 }
315
316 static void send_stop_cmd(struct dw_mci *host, struct mmc_data *data)
317 {
318         dw_mci_start_command(host, data->stop, host->stop_cmdr);
319 }
320
321 /* DMA interface functions */
322 static void dw_mci_stop_dma(struct dw_mci *host)
323 {
324         if (host->using_dma) {
325                 /* Fixme: No need to terminate edma, may cause flush op */
326                 if(!(cpu_is_rk3036() || cpu_is_rk312x()))
327                         host->dma_ops->stop(host);
328                 host->dma_ops->cleanup(host);
329         }
330
331         /* Data transfer was stopped by the interrupt handler */
332         set_bit(EVENT_XFER_COMPLETE, &host->pending_events);
333 }
334
335 static int dw_mci_get_dma_dir(struct mmc_data *data)
336 {
337         if (data->flags & MMC_DATA_WRITE)
338                 return DMA_TO_DEVICE;
339         else
340                 return DMA_FROM_DEVICE;
341 }
342
343 #ifdef CONFIG_MMC_DW_IDMAC
344 static void dw_mci_dma_cleanup(struct dw_mci *host)
345 {
346         struct mmc_data *data = host->data;
347
348         if (data)
349                 if (!data->host_cookie)
350                         dma_unmap_sg(host->dev,
351                                      data->sg,
352                                      data->sg_len,
353                                      dw_mci_get_dma_dir(data));
354 }
355
356 static void dw_mci_idmac_reset(struct dw_mci *host)
357 {
358         u32 bmod = mci_readl(host, BMOD);
359         /* Software reset of DMA */
360         bmod |= SDMMC_IDMAC_SWRESET;
361         mci_writel(host, BMOD, bmod);
362 }
363
364 static void dw_mci_idmac_stop_dma(struct dw_mci *host)
365 {
366         u32 temp;
367
368         /* Disable and reset the IDMAC interface */
369         temp = mci_readl(host, CTRL);
370         temp &= ~SDMMC_CTRL_USE_IDMAC;
371         temp |= SDMMC_CTRL_DMA_RESET;
372         mci_writel(host, CTRL, temp);
373
374         /* Stop the IDMAC running */
375         temp = mci_readl(host, BMOD);
376         temp &= ~(SDMMC_IDMAC_ENABLE | SDMMC_IDMAC_FB);
377         temp |= SDMMC_IDMAC_SWRESET;
378         mci_writel(host, BMOD, temp);
379 }
380
381 static void dw_mci_idmac_complete_dma(void *arg)
382 {
383         struct dw_mci *host = arg;
384         struct mmc_data *data = host->data;
385
386         dev_vdbg(host->dev, "DMA complete\n");
387
388         host->dma_ops->cleanup(host);
389
390         /*
391          * If the card was removed, data will be NULL. No point in trying to
392          * send the stop command or waiting for NBUSY in this case.
393          */
394         if(data){
395                 set_bit(EVENT_XFER_COMPLETE, &host->pending_events);
396                 tasklet_schedule(&host->tasklet);
397         }
398 }
399
400 static void dw_mci_translate_sglist(struct dw_mci *host, struct mmc_data *data,
401                                     unsigned int sg_len)
402 {
403         int i;
404         struct idmac_desc *desc = host->sg_cpu;
405
406         for (i = 0; i < sg_len; i++, desc++) {
407                 unsigned int length = sg_dma_len(&data->sg[i]);
408                 u32 mem_addr = sg_dma_address(&data->sg[i]);
409
410                 /* Set the OWN bit and disable interrupts for this descriptor */
411                 desc->des0 = IDMAC_DES0_OWN | IDMAC_DES0_DIC | IDMAC_DES0_CH;
412
413                 /* Buffer length */
414                 IDMAC_SET_BUFFER1_SIZE(desc, length);
415
416                 /* Physical address to DMA to/from */
417                 desc->des2 = mem_addr;
418         }
419
420         /* Set first descriptor */
421         desc = host->sg_cpu;
422         desc->des0 |= IDMAC_DES0_FD;
423
424         /* Set last descriptor */
425         desc = host->sg_cpu + (i - 1) * sizeof(struct idmac_desc);
426         desc->des0 &= ~(IDMAC_DES0_CH | IDMAC_DES0_DIC);
427         desc->des0 |= IDMAC_DES0_LD;
428
429         wmb();
430 }
431
432 static void dw_mci_idmac_start_dma(struct dw_mci *host, unsigned int sg_len)
433 {
434         u32 temp;
435
436         dw_mci_translate_sglist(host, host->data, sg_len);
437
438         /* Select IDMAC interface */
439         temp = mci_readl(host, CTRL);
440         temp |= SDMMC_CTRL_USE_IDMAC;
441         mci_writel(host, CTRL, temp);
442
443         wmb();
444
445         /* Enable the IDMAC */
446         temp = mci_readl(host, BMOD);
447         temp |= SDMMC_IDMAC_ENABLE | SDMMC_IDMAC_FB;
448         mci_writel(host, BMOD, temp);
449
450         /* Start it running */
451         mci_writel(host, PLDMND, 1);
452 }
453
454 static int dw_mci_idmac_init(struct dw_mci *host)
455 {
456         struct idmac_desc *p;
457         int i;
458
459         /* Number of descriptors in the ring buffer */
460         host->ring_size = PAGE_SIZE / sizeof(struct idmac_desc);
461
462         /* Forward link the descriptor list */
463         for (i = 0, p = host->sg_cpu; i < host->ring_size - 1; i++, p++) {
464                 p->des3 = host->sg_dma + (sizeof(struct idmac_desc) * (i + 1));
465                 p->des1 = 0;
466         }
467         /* Set the last descriptor as the end-of-ring descriptor */
468         p->des3 = host->sg_dma;
469         p->des0 = IDMAC_DES0_ER;
470
471         dw_mci_idmac_reset(host);
472
473         /* Mask out interrupts - get Tx & Rx complete only */
474         mci_writel(host, IDSTS, IDMAC_INT_CLR);
475         mci_writel(host, IDINTEN, SDMMC_IDMAC_INT_NI | SDMMC_IDMAC_INT_RI |
476                    SDMMC_IDMAC_INT_TI);
477
478         /* Set the descriptor base address */
479         mci_writel(host, DBADDR, host->sg_dma);
480         return 0;
481 }
482
483 static const struct dw_mci_dma_ops dw_mci_idmac_ops = {
484         .init = dw_mci_idmac_init,
485         .start = dw_mci_idmac_start_dma,
486         .stop = dw_mci_idmac_stop_dma,
487         .complete = dw_mci_idmac_complete_dma,
488         .cleanup = dw_mci_dma_cleanup,
489 };
490
491
492 static void dw_mci_edma_cleanup(struct dw_mci *host)
493 {
494         struct mmc_data *data = host->data;
495
496         if (data)
497                 if (!data->host_cookie)
498                         dma_unmap_sg(host->dev,
499                                         data->sg, data->sg_len,
500                                         dw_mci_get_dma_dir(data));
501 }
502
503 static void dw_mci_edmac_stop_dma(struct dw_mci *host)
504 {
505         dmaengine_terminate_all(host->dms->ch);
506 }
507
508 static void dw_mci_edmac_complete_dma(void *arg)
509 {
510         struct dw_mci *host = arg;
511         struct mmc_data *data = host->data;
512
513         dev_vdbg(host->dev, "DMA complete\n");
514
515         if(data)
516                 if(data->flags & MMC_DATA_READ)
517                         /* Invalidate cache after read */
518                         dma_sync_sg_for_cpu(mmc_dev(host->mmc), data->sg,
519                                 data->sg_len, DMA_FROM_DEVICE);
520
521         host->dma_ops->cleanup(host);
522
523         /*
524          * If the card was removed, data will be NULL. No point in trying to
525          * send the stop command or waiting for NBUSY in this case.
526          */
527         if (data) {
528                 set_bit(EVENT_XFER_COMPLETE, &host->pending_events);
529                 tasklet_schedule(&host->tasklet);
530         }
531 }
532
533 static void dw_mci_edmac_start_dma(struct dw_mci *host, unsigned int sg_len)
534 {
535         struct dma_slave_config slave_config;
536         struct dma_async_tx_descriptor *desc = NULL;
537         struct scatterlist *sgl = host->data->sg;
538         const u32 mszs[] = {1, 4, 8, 16, 32, 64, 128, 256};
539         u32 sg_elems = host->data->sg_len;
540         u32 fifoth_val, mburst;
541         u32 burst_limit = 0;
542         u32 idx, rx_wmark, tx_wmark;
543         int ret = 0;
544
545         /* Set external dma config: burst size, burst width*/
546         slave_config.dst_addr = (dma_addr_t)(host->phy_regs + host->data_offset);
547         slave_config.src_addr = slave_config.dst_addr;
548         slave_config.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
549         slave_config.src_addr_width = slave_config.dst_addr_width;
550
551         /* Match FIFO dma burst MSIZE with external dma config*/
552         fifoth_val = mci_readl(host, FIFOTH);
553         mburst = mszs[(fifoth_val >> 28) & 0x7];
554
555         /* Edmac limit burst to 16, but work around for rk3036 to 8 */
556         if (unlikely(cpu_is_rk3036()))
557                 burst_limit = 8;
558         else
559                 burst_limit = 16;
560
561         if (mburst > burst_limit) {
562                 mburst = burst_limit;
563                 idx = (ilog2(mburst) > 0) ? (ilog2(mburst) - 1) : 0;
564
565                 rx_wmark = mszs[idx] - 1;
566                 tx_wmark = (host->fifo_depth) / 2;
567                 fifoth_val = SDMMC_SET_FIFOTH(idx, rx_wmark, tx_wmark);
568
569                 mci_writel(host, FIFOTH, fifoth_val);
570         }
571
572         slave_config.dst_maxburst = mburst;
573         slave_config.src_maxburst = slave_config.dst_maxburst;
574
575         if(host->data->flags & MMC_DATA_WRITE){
576                 slave_config.direction = DMA_MEM_TO_DEV;
577                 ret = dmaengine_slave_config(host->dms->ch, &slave_config);
578                 if(ret){
579                         dev_err(host->dev,
580                                 "Error in dw_mci edmac write configuration.\n");
581                         return;
582                 }
583
584                 desc = dmaengine_prep_slave_sg(host->dms->ch, sgl, sg_len,
585                                         DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
586                 if (!desc) {
587                         dev_err(host->dev,
588                                 "Cannot prepare slave write sg the dw_mci edmac!\n");
589                         return;
590                 }
591
592                 /* Set dw_mci_edmac_complete_dma as callback */
593                 desc->callback = dw_mci_edmac_complete_dma;
594                 desc->callback_param = (void *)host;
595                 dmaengine_submit(desc);
596
597                 /* Flush cache before write */
598                 dma_sync_sg_for_device(mmc_dev(host->mmc), sgl,
599                                 sg_elems, DMA_TO_DEVICE);
600                 dma_async_issue_pending(host->dms->ch);
601         } else {
602                 /* MMC_DATA_READ*/
603                 slave_config.direction = DMA_DEV_TO_MEM;
604                 ret = dmaengine_slave_config(host->dms->ch, &slave_config);
605                 if (ret) {
606                         dev_err(host->dev,
607                                 "Error in dw_mci edmac read configuration.\n");
608                         return;
609                 }
610                 desc = dmaengine_prep_slave_sg(host->dms->ch, sgl, sg_len,
611                                         DMA_DEV_TO_MEM, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
612                 if (!desc) {
613                         dev_err(host->dev,
614                                 "Cannot prepare slave read sg for the dw_mci edmac!\n");
615                         return;
616                 }
617                 /* set dw_mci_edmac_complete_dma as callback */
618                 desc->callback = dw_mci_edmac_complete_dma;
619                 desc->callback_param = (void *)host;
620                 dmaengine_submit(desc);
621                 dma_async_issue_pending(host->dms->ch);
622         }
623 }
624
625 static int dw_mci_edmac_init(struct dw_mci *host)
626 {
627         int ret = 0;
628
629         /* Request external dma channel, SHOULD decide chn in dts */
630         host->dms = NULL;
631         host->dms = (struct dw_mci_dma_slave *)kzalloc
632                                 (sizeof(struct dw_mci_dma_slave), GFP_KERNEL);
633         if (NULL == host->dms) {
634                 dev_err(host->dev, "No enough memory to alloc dms.\n");
635                 return -ENOMEM;
636         }
637
638         host->dms->ch = dma_request_slave_channel(host->dev, "dw_mci");
639         if (!host->dms->ch) {
640                 dev_err(host->dev,
641                         "Failed to get external DMA channel %d\n",
642                         host->dms->ch->chan_id);
643                 kfree(host->dms);
644                 host->dms = NULL;
645                 return -ENXIO;
646         }
647
648         return ret;
649 }
650
651 static void dw_mci_edmac_exit(struct dw_mci *host)
652 {
653         if (NULL != host->dms) {
654                 if (NULL != host->dms->ch) {
655                         dma_release_channel(host->dms->ch);
656                         host->dms->ch = NULL;
657                 }
658                 kfree(host->dms);
659                 host->dms = NULL;
660         }
661 }
662
663 static const struct dw_mci_dma_ops dw_mci_edmac_ops = {
664         .init = dw_mci_edmac_init,
665         .exit = dw_mci_edmac_exit,
666         .start = dw_mci_edmac_start_dma,
667         .stop = dw_mci_edmac_stop_dma,
668         .complete = dw_mci_edmac_complete_dma,
669         .cleanup = dw_mci_edma_cleanup,
670 };
671 #endif /* CONFIG_MMC_DW_IDMAC */
672
673 static int dw_mci_pre_dma_transfer(struct dw_mci *host,
674                                    struct mmc_data *data,
675                                    bool next)
676 {
677         struct scatterlist *sg;
678         unsigned int i, sg_len;
679
680         if (!next && data->host_cookie)
681                 return data->host_cookie;
682
683         /*
684          * We don't do DMA on "complex" transfers, i.e. with
685          * non-word-aligned buffers or lengths. Also, we don't bother
686          * with all the DMA setup overhead for short transfers.
687          */
688         if (data->blocks * data->blksz < DW_MCI_DMA_THRESHOLD)
689                 return -EINVAL;
690
691         if (data->blksz & 3)
692                 return -EINVAL;
693
694         for_each_sg(data->sg, sg, data->sg_len, i) {
695                 if (sg->offset & 3 || sg->length & 3)
696                         return -EINVAL;
697         }
698
699         sg_len = dma_map_sg(host->dev,
700                             data->sg,
701                             data->sg_len,
702                             dw_mci_get_dma_dir(data));
703         if (sg_len == 0)
704                 return -EINVAL;
705
706         if (next)
707                 data->host_cookie = sg_len;
708
709         return sg_len;
710 }
711
712 static void dw_mci_pre_req(struct mmc_host *mmc,
713                            struct mmc_request *mrq,
714                            bool is_first_req)
715 {
716         struct dw_mci_slot *slot = mmc_priv(mmc);
717         struct mmc_data *data = mrq->data;
718
719         if (!slot->host->use_dma || !data)
720                 return;
721
722         if (data->host_cookie) {
723                 data->host_cookie = 0;
724                 return;
725         }
726
727         if (dw_mci_pre_dma_transfer(slot->host, mrq->data, 1) < 0)
728                 data->host_cookie = 0;
729 }
730
731 static void dw_mci_post_req(struct mmc_host *mmc,
732                             struct mmc_request *mrq,
733                             int err)
734 {
735         struct dw_mci_slot *slot = mmc_priv(mmc);
736         struct mmc_data *data = mrq->data;
737
738         if (!slot->host->use_dma || !data)
739                 return;
740
741         if (data->host_cookie)
742                 dma_unmap_sg(slot->host->dev,
743                              data->sg,
744                              data->sg_len,
745                              dw_mci_get_dma_dir(data));
746         data->host_cookie = 0;
747 }
748
749 static void dw_mci_adjust_fifoth(struct dw_mci *host, struct mmc_data *data)
750 {
751 #ifdef CONFIG_MMC_DW_IDMAC
752         unsigned int blksz = data->blksz;
753         const u32 mszs[] = {1, 4, 8, 16, 32, 64, 128, 256};
754         u32 fifo_width = 1 << host->data_shift;
755         u32 blksz_depth = blksz / fifo_width, fifoth_val;
756         u32 msize = 0, rx_wmark = 1, tx_wmark, tx_wmark_invers;
757         int idx = (sizeof(mszs) / sizeof(mszs[0])) - 1;
758
759         tx_wmark = (host->fifo_depth) / 2;
760         tx_wmark_invers = host->fifo_depth - tx_wmark;
761
762         /*
763          * MSIZE is '1',
764          * if blksz is not a multiple of the FIFO width
765          */
766         if (blksz % fifo_width) {
767                 msize = 0;
768                 rx_wmark = 1;
769                 goto done;
770         }
771
772         do {
773                 if (!((blksz_depth % mszs[idx]) ||
774                      (tx_wmark_invers % mszs[idx]))) {
775                         msize = idx;
776                         rx_wmark = mszs[idx] - 1;
777                         break;
778                 }
779         } while (--idx > 0);
780         /*
781          * If idx is '0', it won't be tried
782          * Thus, initial values are uesed
783          */
784 done:
785         fifoth_val = SDMMC_SET_FIFOTH(msize, rx_wmark, tx_wmark);
786         mci_writel(host, FIFOTH, fifoth_val);
787
788 #endif
789 }
790
791 static void dw_mci_ctrl_rd_thld(struct dw_mci *host, struct mmc_data *data)
792 {
793         unsigned int blksz = data->blksz;
794         u32 blksz_depth, fifo_depth;
795         u16 thld_size;
796
797         WARN_ON(!(data->flags & MMC_DATA_READ));
798
799         if (host->timing != MMC_TIMING_MMC_HS200 &&
800             host->timing != MMC_TIMING_UHS_SDR104)
801                 goto disable;
802
803         blksz_depth = blksz / (1 << host->data_shift);
804         fifo_depth = host->fifo_depth;
805
806         if (blksz_depth > fifo_depth)
807                 goto disable;
808
809         /*
810          * If (blksz_depth) >= (fifo_depth >> 1), should be 'thld_size <= blksz'
811          * If (blksz_depth) <  (fifo_depth >> 1), should be thld_size = blksz
812          * Currently just choose blksz.
813          */
814         thld_size = blksz;
815         mci_writel(host, CDTHRCTL, SDMMC_SET_RD_THLD(thld_size, 1));
816         return;
817
818 disable:
819         mci_writel(host, CDTHRCTL, SDMMC_SET_RD_THLD(0, 0));
820 }
821
822 static int dw_mci_submit_data_dma(struct dw_mci *host, struct mmc_data *data)
823 {
824         int sg_len;
825         unsigned long flags;
826         u32 temp;
827
828         host->using_dma = 0;
829
830         /* If we don't have a channel, we can't do DMA */
831         if (!host->use_dma)
832                 return -ENODEV;
833
834         sg_len = dw_mci_pre_dma_transfer(host, data, 0);
835         if (sg_len < 0) {
836                 /* Fixme: No need terminate edma, may cause flush op */
837                 if(!(cpu_is_rk3036() || cpu_is_rk312x()))
838                         host->dma_ops->stop(host);
839                 return sg_len;
840         }
841
842         host->using_dma = 1;
843
844         dev_vdbg(host->dev,
845                  "sd sg_cpu: %#lx sg_dma: %#lx sg_len: %d\n",
846                  (unsigned long)host->sg_cpu, (unsigned long)host->sg_dma,
847                  sg_len);
848
849         /*
850          * Decide the MSIZE and RX/TX Watermark.
851          * If current block size is same with previous size,
852          * no need to update fifoth.
853          */
854         if (host->prev_blksz != data->blksz)
855                 dw_mci_adjust_fifoth(host, data);
856
857         /* Reset DMA FIFO*/
858         dw_mci_ctrl_reset(host, SDMMC_CTRL_DMA_RESET);
859
860         /* Enable the DMA interface */
861         temp = mci_readl(host, CTRL);
862         temp |= SDMMC_CTRL_DMA_ENABLE;
863         mci_writel(host, CTRL, temp);
864
865         /* Disable RX/TX IRQs, let DMA handle it */
866         spin_lock_irqsave(&host->slock, flags);
867         temp = mci_readl(host, INTMASK);
868         temp  &= ~(SDMMC_INT_RXDR | SDMMC_INT_TXDR);
869         mci_writel(host, INTMASK, temp);
870         spin_unlock_irqrestore(&host->slock, flags);
871
872         host->dma_ops->start(host, sg_len);
873
874         return 0;
875 }
876
877 static void dw_mci_submit_data(struct dw_mci *host, struct mmc_data *data)
878 {
879         u32 temp;
880         unsigned long flag;
881
882         data->error = -EINPROGRESS;
883
884         //WARN_ON(host->data);
885         host->sg = NULL;
886         host->data = data;
887
888         /* Reset FIFO*/
889         dw_mci_ctrl_reset(host, SDMMC_CTRL_DMA_RESET);
890
891         if (data->flags & MMC_DATA_READ) {
892                 host->dir_status = DW_MCI_RECV_STATUS;
893                 dw_mci_ctrl_rd_thld(host, data);
894         } else {
895                 host->dir_status = DW_MCI_SEND_STATUS;
896         }
897         
898         MMC_DBG_INFO_FUNC(host->mmc,
899                         "Dw_mci_submit_data, blocks = %d, blksz = %d [%s]",
900                         data->blocks, data->blksz, mmc_hostname(host->mmc));
901
902         if (dw_mci_submit_data_dma(host, data)) {
903                 int flags = SG_MITER_ATOMIC;
904                 if (host->data->flags & MMC_DATA_READ)
905                         flags |= SG_MITER_TO_SG;
906                 else
907                         flags |= SG_MITER_FROM_SG;
908
909                 sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
910                 host->sg = data->sg;
911                 host->part_buf_start = 0;
912                 host->part_buf_count = 0;
913
914                 spin_lock_irqsave(&host->slock, flag);
915                 mci_writel(host, RINTSTS, SDMMC_INT_TXDR | SDMMC_INT_RXDR);
916                 temp = mci_readl(host, INTMASK);
917                 temp |= SDMMC_INT_TXDR | SDMMC_INT_RXDR;
918                 mci_writel(host, INTMASK, temp);
919                 spin_unlock_irqrestore(&host->slock, flag);
920
921                 temp = mci_readl(host, CTRL);
922                 temp &= ~SDMMC_CTRL_DMA_ENABLE;
923                 mci_writel(host, CTRL, temp);
924
925                 /*
926                  * Use the initial fifoth_val for PIO mode.
927                  * If next issued data may be transfered by DMA mode,
928                  * prev_blksz should be invalidated.
929                  */
930                 mci_writel(host, FIFOTH, host->fifoth_val);
931                 host->prev_blksz = 0;
932         } else {
933                 /*
934                  * Keep the current block size.
935                  * It will be used to decide whether to update
936                  * fifoth register next time.
937                  */
938                 host->prev_blksz = data->blksz;
939         }
940 }
941
942 static void mci_send_cmd(struct dw_mci_slot *slot, u32 cmd, u32 arg)
943 {
944         struct dw_mci *host = slot->host;       
945         unsigned long timeout = jiffies + msecs_to_jiffies(500);
946         unsigned int cmd_status = 0;
947
948 #ifdef SDMMC_WAIT_FOR_UNBUSY
949         bool ret = true;
950         timeout = jiffies + msecs_to_jiffies(SDMMC_WAIT_FOR_UNBUSY);
951         
952         if (test_bit(DW_MMC_CARD_PRESENT, &slot->flags)) {
953                 while (ret) {
954                         ret =  time_before(jiffies, timeout);
955                         cmd_status = mci_readl(host, STATUS);
956                         if (!(cmd_status &
957                                 (SDMMC_STAUTS_DATA_BUSY |
958                                 SDMMC_STAUTS_MC_BUSY)))
959                                 break;
960                 };
961
962                 if(false == ret)
963                         MMC_DBG_ERR_FUNC(host->mmc,
964                                 "mci_send_cmd: wait for unbusy timeout! [%s]",
965                                 mmc_hostname(host->mmc));
966         }
967 #endif
968  
969         mci_writel(host, CMDARG, arg);
970         wmb();
971         mci_writel(host, CMD, SDMMC_CMD_START | cmd);
972         if(cmd & SDMMC_CMD_UPD_CLK)
973                 timeout = jiffies + msecs_to_jiffies(50);
974         else
975                 timeout = jiffies + msecs_to_jiffies(500);
976
977         while (time_before(jiffies, timeout)) {
978                 cmd_status = mci_readl(host, CMD);
979                 if (!(cmd_status & SDMMC_CMD_START))
980                         return;
981         }
982
983         dev_err(&slot->mmc->class_dev,
984                 "Timeout sending command (cmd %#x arg %#x status %#x)\n",
985                 cmd, arg, cmd_status);
986 }
987
988 static void dw_mci_setup_bus(struct dw_mci_slot *slot, bool force_clkinit)
989 {
990         struct dw_mci *host = slot->host;
991         unsigned int tempck,clock = slot->clock;
992         u32 div;
993         u32 clk_en_a;
994         u32 sdio_int;
995
996         MMC_DBG_INFO_FUNC(host->mmc,
997                         "%s: clock=%d, current_speed=%d, bus_hz=%d, forc=%d[%s]\n",
998                         __FUNCTION__, clock, host->current_speed, host->bus_hz,
999                         force_clkinit, mmc_hostname(host->mmc));
1000
1001         if (!clock) {
1002                 mci_writel(host, CLKENA, 0);
1003                 #ifdef CONFIG_MMC_DW_ROCKCHIP_SWITCH_VOLTAGE
1004                 if(host->svi_flags == 0)
1005                         mci_send_cmd(slot, SDMMC_CMD_UPD_CLK | SDMMC_CMD_PRV_DAT_WAIT, 0);
1006                 #else
1007                 mci_send_cmd(slot, SDMMC_CMD_UPD_CLK | SDMMC_CMD_PRV_DAT_WAIT, 0);
1008                 #endif
1009         } else if (clock != host->current_speed || force_clkinit) {
1010                 div = host->bus_hz / clock;
1011                 if (host->bus_hz % clock && host->bus_hz > clock)
1012                         /*
1013                          * move the + 1 after the divide to prevent
1014                          * over-clocking the card.
1015                          */
1016                         div += 1;
1017
1018                 div = (host->bus_hz != clock) ? DIV_ROUND_UP(div, 2) : 0;
1019
1020                 if ((clock << div) != slot->__clk_old || force_clkinit) {
1021                     tempck = div ? ((host->bus_hz / div) >> 1) :host->bus_hz;
1022                         dev_info(&slot->mmc->class_dev,
1023                                  "Bus speed (slot %d) = %dHz (slot req %dHz, actual %dHZ div = %d)\n",
1024                                  slot->id, host->bus_hz, clock,
1025                                  tempck, div);
1026
1027                         host->set_speed = tempck;
1028                         host->set_div = div;
1029                 }
1030
1031                 /* disable clock */
1032                 mci_writel(host, CLKENA, 0);
1033                 mci_writel(host, CLKSRC, 0);
1034
1035                 /* inform CIU */
1036                 mci_send_cmd(slot,
1037                              SDMMC_CMD_UPD_CLK | SDMMC_CMD_PRV_DAT_WAIT, 0);
1038                         
1039                 if(clock <= 400*1000){
1040                         MMC_DBG_BOOT_FUNC(host->mmc,
1041                                 "dw_mci_setup_bus: argue clk_mmc workaround out %dHz for init[%s]",
1042                                 clock * 2, mmc_hostname(host->mmc)); 
1043                         /* clk_mmc will change parents to 24MHz xtal*/
1044                         clk_set_rate(host->clk_mmc, clock * 2);                
1045
1046                         div = 0;
1047                         host->set_div = div;
1048                 }
1049                 else
1050                 {
1051                         MMC_DBG_BOOT_FUNC(host->mmc,
1052                                 "dw_mci_setup_bus: argue clk_mmc workaround out normal clock [%s]",
1053                                 mmc_hostname(host->mmc)); 
1054                         if(div > 1)
1055                         {
1056                                 MMC_DBG_ERR_FUNC(host->mmc,
1057                                         "dw_mci_setup_bus: div SHOULD NOT LARGER THAN ONE! [%s]",
1058                                         mmc_hostname(host->mmc)); 
1059                                  div = 1;
1060                                  host->set_div = div;
1061                                  host->bus_hz = host->set_speed * 2;
1062                                  MMC_DBG_BOOT_FUNC(host->mmc,
1063                                         "dw_mci_setup_bus: workaround div = %d, host->bus_hz = %d [%s]",
1064                                         div, host->bus_hz, mmc_hostname(host->mmc));                                 
1065                         }
1066                         /* BUG may be here, come on,  Linux BSP engineer looks!
1067                            FIXME:  HS-DDR eMMC, div SHOULD be ONE, but we here cannot fetch eMMC bus mode!!!!!!!! 
1068                            WRONG dts set clk = 50M, and calc div be zero. Controller denied this setting!
1069                            some oops happened like that:
1070                            mmc_host mmc0: Bus speed (slot 0) = 50000000Hz (slot req 50000000Hz, actual 50000000HZ div = 0)
1071                            rk_sdmmc: BOOT dw_mci_setup_bus: argue clk_mmc workaround out normal clock [mmc0]
1072                            rk_sdmmc: BOOT Bus speed=50000000Hz,Bus width=8bits.[mmc0]
1073                            mmc0: new high speed DDR MMC card at address 0001
1074                            mmcblk0: mmc0:0001 M8G1GC 7.28 GiB 
1075                            ....
1076                            mmcblk0: error -84 transferring data, sector 606208, nr 32, cmd response 0x900, card status 0xb00
1077                            mmcblk0: retrying using single block read
1078                            mmcblk0: error -110 sending status command, retrying
1079
1080                            We assume all eMMC in RK platform with 3.10 kernel, at least version 4.5
1081                          */
1082                         if ((div == 0) &&
1083                                 (host->mmc->caps & (MMC_CAP_1_8V_DDR | MMC_CAP_1_2V_DDR)) &&
1084                                 !(host->mmc->caps2 & MMC_CAP2_HS200)) {
1085                                 /*  Fixup DDR MMC */
1086                                 div = 1;
1087                                 host->set_div = div;
1088                                 host->bus_hz = host->set_speed * 2;
1089                                 MMC_DBG_BOOT_FUNC(host->mmc,
1090                                         "dw_mci_setup_bus: workaround div = %d, host->bus_hz = %d [%s]",
1091                                         div, host->bus_hz, mmc_hostname(host->mmc));
1092                         }
1093
1094                         if (host->verid < DW_MMC_240A)
1095                                 clk_set_rate(host->clk_mmc,(host->bus_hz));
1096                         else
1097                                 clk_set_rate(host->clk_mmc,(host->bus_hz) * 2);
1098
1099
1100                                 
1101                 }
1102                                
1103                 /* set clock to desired speed */
1104                 mci_writel(host, CLKDIV, div);
1105
1106                 /* inform CIU */
1107                 mci_send_cmd(slot,
1108                              SDMMC_CMD_UPD_CLK | SDMMC_CMD_PRV_DAT_WAIT, 0);
1109
1110                 /* enable clock; only low power if no SDIO */
1111                 clk_en_a = SDMMC_CLKEN_ENABLE << slot->id;
1112
1113                 if (host->verid < DW_MMC_240A)
1114                     sdio_int = SDMMC_INT_SDIO(slot->id);
1115                 else
1116                     sdio_int = SDMMC_INT_SDIO((slot->id) + 8);
1117
1118                 if (!(mci_readl(host, INTMASK) & sdio_int))
1119                         clk_en_a |= SDMMC_CLKEN_LOW_PWR << slot->id;
1120                 mci_writel(host, CLKENA, clk_en_a);
1121
1122                 /* inform CIU */
1123                 mci_send_cmd(slot,
1124                              SDMMC_CMD_UPD_CLK | SDMMC_CMD_PRV_DAT_WAIT, 0);
1125                 /* keep the clock with reflecting clock dividor */
1126                 slot->__clk_old = clock << div;
1127         }
1128
1129         host->current_speed = clock;
1130
1131         if(slot->ctype != slot->pre_ctype)
1132                 MMC_DBG_BOOT_FUNC(host->mmc,
1133                                 "Bus speed=%dHz,Bus width=%s.[%s]",
1134                                 host->set_speed,
1135                                 (slot->ctype == SDMMC_CTYPE_4BIT) ? "4bits" : "8bits",
1136                                 mmc_hostname(host->mmc));
1137
1138         slot->pre_ctype = slot->ctype;
1139
1140         /* Set the current slot bus width */
1141         mci_writel(host, CTYPE, (slot->ctype << slot->id));
1142 }
1143
1144 extern struct mmc_card *this_card;
1145 static void dw_mci_wait_unbusy(struct dw_mci *host)
1146 {
1147         unsigned int timeout = SDMMC_DATA_TIMEOUT_SDIO;
1148         unsigned long time_loop;
1149         unsigned int status;
1150         unsigned int tmo = 300000;
1151         /* Secure erase flag */
1152         u32 se_flag = 0;
1153
1154         MMC_DBG_INFO_FUNC(host->mmc,
1155                 "dw_mci_wait_unbusy, status=0x%x ", mci_readl(host, STATUS));
1156     
1157         if (host->mmc->restrict_caps & RESTRICT_CARD_TYPE_EMMC) {
1158                 if (host->cmd && (host->cmd->opcode == MMC_ERASE) && this_card) {
1159                 /* Special care for (secure)erase timeout calculation */
1160                         if ((host->cmd->arg & (0x1 << 31)) == 1)
1161                                 se_flag = 0x1;
1162
1163                         if (((this_card->ext_csd.erase_group_def) & 0x1) == 1)
1164                                 se_flag ?
1165                                 (timeout = (this_card->ext_csd.hc_erase_timeout) *
1166                                 tmo * (this_card->ext_csd.sec_erase_mult)) :
1167                                 (timeout = (this_card->ext_csd.hc_erase_timeout) * tmo);
1168                 }
1169         
1170                 if(timeout < SDMMC_DATA_TIMEOUT_EMMC)
1171                         timeout = SDMMC_DATA_TIMEOUT_EMMC;
1172         } else if (host->mmc->restrict_caps & RESTRICT_CARD_TYPE_SD) {
1173                 timeout = SDMMC_DATA_TIMEOUT_SD;
1174         }
1175
1176         time_loop = jiffies + msecs_to_jiffies(timeout);
1177         do {
1178                 status = mci_readl(host, STATUS);
1179                 if (!(status & (SDMMC_STAUTS_DATA_BUSY | SDMMC_STAUTS_MC_BUSY)))
1180                         break;
1181         } while (time_before(jiffies, time_loop));
1182 }
1183
1184 #ifdef CONFIG_MMC_DW_ROCKCHIP_SWITCH_VOLTAGE
1185 /*
1186 *   result: 
1187 *   0--status is busy. 
1188 *   1--status is unbusy.
1189 */
1190 int dw_mci_card_busy(struct mmc_host *mmc)
1191 {
1192         struct dw_mci_slot *slot = mmc_priv(mmc);
1193         struct dw_mci *host = slot->host;
1194
1195         MMC_DBG_INFO_FUNC(host->mmc, "dw_mci_card_busy: svi_flags = %d [%s]", \
1196                                 host->svi_flags, mmc_hostname(host->mmc));      
1197     
1198         /* svi toggle*/
1199         if(host->svi_flags == 0){
1200                 /*first svi*/
1201                 host->svi_flags = 1;
1202                 return host->svi_flags;           
1203     
1204         }else{
1205                 host->svi_flags = 0;
1206                 return host->svi_flags;   
1207         }
1208         
1209
1210 }
1211 #endif
1212 static void __dw_mci_start_request(struct dw_mci *host,
1213                                    struct dw_mci_slot *slot,
1214                                    struct mmc_command *cmd)
1215 {
1216         struct mmc_request *mrq;
1217         struct mmc_data *data;
1218         u32 cmdflags;
1219
1220         mrq = slot->mrq;
1221         if (host->pdata->select_slot)
1222                 host->pdata->select_slot(slot->id);
1223
1224         host->cur_slot = slot;
1225         host->mrq = mrq;
1226
1227         dw_mci_wait_unbusy(host);
1228     
1229         host->pending_events = 0;
1230         host->completed_events = 0;
1231         host->data_status = 0;
1232
1233         data = cmd->data;
1234         if (data) {
1235                 dw_mci_set_timeout(host);
1236                 mci_writel(host, BYTCNT, data->blksz*data->blocks);
1237                 mci_writel(host, BLKSIZ, data->blksz);
1238         }
1239
1240         cmdflags = dw_mci_prepare_command(slot->mmc, cmd);
1241
1242         /* this is the first command, send the initialization clock */
1243         if (test_and_clear_bit(DW_MMC_CARD_NEED_INIT, &slot->flags))
1244                 cmdflags |= SDMMC_CMD_INIT;
1245
1246         if (data) {
1247                 dw_mci_submit_data(host, data);
1248                 wmb();
1249         }
1250
1251         dw_mci_start_command(host, cmd, cmdflags);
1252
1253         if (mrq->stop)
1254                 host->stop_cmdr = dw_mci_prepare_command(slot->mmc, mrq->stop);
1255 }
1256
1257 static void dw_mci_start_request(struct dw_mci *host,
1258                                  struct dw_mci_slot *slot)
1259 {
1260         struct mmc_request *mrq = slot->mrq;
1261         struct mmc_command *cmd;
1262
1263         MMC_DBG_INFO_FUNC(host->mmc,
1264                 " Begin to start the new request. cmd=%d(arg=0x%x)[%s]",
1265                 mrq->cmd->opcode, mrq->cmd->arg, mmc_hostname(host->mmc));
1266         
1267         cmd = mrq->sbc ? mrq->sbc : mrq->cmd;
1268         __dw_mci_start_request(host, slot, cmd);
1269 }
1270
1271 /* must be called with host->lock held */
1272 static void dw_mci_queue_request(struct dw_mci *host, struct dw_mci_slot *slot,
1273                                  struct mmc_request *mrq)
1274 {
1275         dev_vdbg(&slot->mmc->class_dev, "queue request: state=%d\n",
1276                  host->state);
1277
1278         slot->mrq = mrq;
1279
1280         if (host->state == STATE_IDLE) {
1281                 host->state = STATE_SENDING_CMD;
1282                 dw_mci_start_request(host, slot);
1283         } else {
1284                 list_add_tail(&slot->queue_node, &host->queue);
1285         }
1286 }
1287
1288 static void dw_mci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1289 {
1290         struct dw_mci_slot *slot = mmc_priv(mmc);
1291         struct dw_mci *host = slot->host;
1292
1293         WARN_ON(slot->mrq);
1294
1295         /*
1296          * The check for card presence and queueing of the request must be
1297          * atomic, otherwise the card could be removed in between and the
1298          * request wouldn't fail until another card was inserted.
1299          */
1300         spin_lock_bh(&host->lock);
1301
1302         if (!test_bit(DW_MMC_CARD_PRESENT, &slot->flags)) {
1303                 spin_unlock_bh(&host->lock);
1304                 mrq->cmd->error = -ENOMEDIUM;           
1305                 MMC_DBG_CMD_FUNC(host->mmc, "%s: no card,so reqeuest done, cmd=%d [%s]",
1306                                 __FUNCTION__, mrq->cmd->opcode, mmc_hostname(host->mmc));
1307             
1308                 mmc_request_done(mmc, mrq);
1309                 return;
1310         }
1311
1312         MMC_DBG_CMD_FUNC(host->mmc,
1313                 "======>\n    pull a new request from MMC-frame to dw_mci_queue. cmd=%d(arg=0x%x)[%s]",
1314                 mrq->cmd->opcode, mrq->cmd->arg, mmc_hostname(host->mmc));
1315
1316         dw_mci_queue_request(host, slot, mrq);
1317
1318         spin_unlock_bh(&host->lock);
1319 }
1320
1321 static void dw_mci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1322 {
1323         struct dw_mci_slot *slot = mmc_priv(mmc);
1324         #ifdef CONFIG_MMC_DW_ROCKCHIP_SWITCH_VOLTAGE
1325         struct dw_mci *host = slot->host;
1326         #endif
1327         const struct dw_mci_drv_data *drv_data = slot->host->drv_data;
1328         u32 regs;
1329         
1330         #ifdef SDMMC_WAIT_FOR_UNBUSY
1331         unsigned long   time_loop;
1332         bool ret = true;
1333
1334         #ifdef CONFIG_MMC_DW_ROCKCHIP_SWITCH_VOLTAGE
1335         if(host->svi_flags == 1)
1336                 time_loop = jiffies + msecs_to_jiffies(SDMMC_DATA_TIMEOUT_SD);
1337         else
1338                 time_loop = jiffies + msecs_to_jiffies(SDMMC_WAIT_FOR_UNBUSY);
1339         #else
1340                 time_loop = jiffies + msecs_to_jiffies(SDMMC_WAIT_FOR_UNBUSY);
1341         #endif
1342         
1343         if(!test_bit(DW_MMC_CARD_PRESENT, &slot->flags)){
1344                 printk("%d..%s:  no card. [%s]\n", \
1345                         __LINE__, __FUNCTION__, mmc_hostname(mmc));
1346                 goto EXIT_POWER;
1347         }
1348     
1349         while (ret) {
1350                 ret = time_before(jiffies, time_loop);
1351                 regs = mci_readl(slot->host, STATUS);
1352                 if (!(regs & (SDMMC_STAUTS_DATA_BUSY |
1353                         SDMMC_STAUTS_MC_BUSY)))
1354                         break;
1355         };
1356         
1357         if(false == ret)
1358         {
1359                 printk("slot->flags = %lu ", slot->flags);
1360                 #ifdef CONFIG_MMC_DW_ROCKCHIP_SWITCH_VOLTAGE
1361                 if(host->svi_flags != 1)
1362                 #endif
1363                         dump_stack();
1364                 printk("%s:  wait for unbusy timeout....... STATUS = 0x%x [%s]\n",
1365                          __FUNCTION__, regs, mmc_hostname(mmc));
1366         }
1367         #endif
1368         
1369         switch (ios->bus_width) {
1370         case MMC_BUS_WIDTH_4:
1371                 slot->ctype = SDMMC_CTYPE_4BIT;
1372                 break;                  
1373         case MMC_BUS_WIDTH_8: 
1374                 slot->ctype = SDMMC_CTYPE_8BIT;
1375                 break;  
1376         default:
1377                 /* set default 1 bit mode */
1378                 slot->ctype = SDMMC_CTYPE_1BIT;
1379                 slot->pre_ctype = SDMMC_CTYPE_1BIT;
1380         }
1381
1382         regs = mci_readl(slot->host, UHS_REG);
1383
1384         /* DDR mode set */
1385         if (ios->timing == MMC_TIMING_UHS_DDR50)
1386                 regs |= ((0x1 << slot->id) << 16);
1387         else
1388                 regs &= ~((0x1 << slot->id) << 16);
1389
1390         mci_writel(slot->host, UHS_REG, regs);
1391         slot->host->timing = ios->timing;
1392
1393         /*
1394          * Use mirror of ios->clock to prevent race with mmc
1395          * core ios update when finding the minimum.
1396          */
1397         slot->clock = ios->clock;
1398
1399         if (drv_data && drv_data->set_ios)
1400                 drv_data->set_ios(slot->host, ios);
1401
1402         /* Slot specific timing and width adjustment */
1403         dw_mci_setup_bus(slot, false);
1404                 //return -EAGAIN;
1405
1406 EXIT_POWER:
1407         switch (ios->power_mode) {
1408         case MMC_POWER_UP:
1409         /* Power up slot */
1410                 set_bit(DW_MMC_CARD_NEED_INIT, &slot->flags);
1411                 if (slot->host->pdata->setpower)
1412                         slot->host->pdata->setpower(slot->id, mmc->ocr_avail);
1413                 regs = mci_readl(slot->host, PWREN);
1414                 regs |= (1 << slot->id);
1415                 mci_writel(slot->host, PWREN, regs);
1416                 break;
1417         case MMC_POWER_OFF:
1418         /* Power down slot */
1419                 if(slot->host->pdata->setpower)
1420                         slot->host->pdata->setpower(slot->id, 0);
1421                 regs = mci_readl(slot->host, PWREN);
1422                 regs &= ~(1 << slot->id);
1423                 mci_writel(slot->host, PWREN, regs);
1424                 break;
1425         default:
1426                 break;
1427         }
1428 }
1429
1430 static int dw_mci_get_ro(struct mmc_host *mmc)
1431 {
1432         int read_only;
1433         struct dw_mci_slot *slot = mmc_priv(mmc);
1434         struct dw_mci_board *brd = slot->host->pdata;
1435
1436         /* Use platform get_ro function, else try on board write protect */
1437         if(slot->quirks & DW_MCI_SLOT_QUIRK_NO_WRITE_PROTECT)
1438                 read_only = 0;
1439         else if(brd->get_ro)
1440                 read_only = brd->get_ro(slot->id);
1441         else if(gpio_is_valid(slot->wp_gpio))
1442                 read_only = gpio_get_value(slot->wp_gpio);
1443         else
1444                 read_only =
1445                         mci_readl(slot->host, WRTPRT) & (1 << slot->id) ? 1 : 0;
1446
1447         dev_dbg(&mmc->class_dev, "card is %s\n",
1448                 read_only ? "read-only" : "read-write");
1449
1450         return read_only;
1451 }
1452
1453 static int dw_mci_set_sdio_status(struct mmc_host *mmc, int val)
1454 {
1455         struct dw_mci_slot *slot = mmc_priv(mmc);
1456         struct dw_mci *host = slot->host;
1457
1458         if (!(mmc->restrict_caps & RESTRICT_CARD_TYPE_SDIO))
1459                 return 0;
1460                 
1461         spin_lock_bh(&host->lock);
1462
1463         if(val)
1464                 set_bit(DW_MMC_CARD_PRESENT, &slot->flags);
1465         else
1466                 clear_bit(DW_MMC_CARD_PRESENT, &slot->flags);
1467
1468         spin_unlock_bh(&host->lock);
1469
1470         if (test_bit(DW_MMC_CARD_PRESENT, &slot->flags)) {
1471                 if (__clk_is_enabled(host->hclk_mmc) == false)
1472                         clk_prepare_enable(host->hclk_mmc);
1473                 if (__clk_is_enabled(host->clk_mmc) == false)
1474                         clk_prepare_enable(host->clk_mmc);
1475         } else {
1476                 if (__clk_is_enabled(host->clk_mmc) == true)
1477                         clk_disable_unprepare(slot->host->clk_mmc);
1478                 if (__clk_is_enabled(host->hclk_mmc) == true)
1479                         clk_disable_unprepare(slot->host->hclk_mmc);
1480         }
1481
1482         mmc_detect_change(slot->mmc, 20);
1483         return 0;
1484 }
1485
1486
1487
1488 static int dw_mci_get_cd(struct mmc_host *mmc)
1489 {
1490         int present;
1491         struct dw_mci_slot *slot = mmc_priv(mmc);
1492         struct dw_mci_board *brd = slot->host->pdata;
1493         struct dw_mci *host = slot->host;
1494         int gpio_cd = mmc_gpio_get_cd(mmc);
1495         int force_jtag_bit, force_jtag_reg;
1496         int gpio_val;
1497         int irq;
1498
1499         if ((soc_is_rk3126() || soc_is_rk3126b() || soc_is_rk3036()) &&
1500                 (mmc->restrict_caps & RESTRICT_CARD_TYPE_SD)) {
1501                 gpio_cd = slot->cd_gpio;
1502                 irq = gpio_to_irq(gpio_cd);
1503                 if (gpio_is_valid(gpio_cd)) {
1504                         gpio_val = gpio_get_value(gpio_cd);
1505                         if (soc_is_rk3036()) {
1506                                 force_jtag_bit = 11;
1507                                 force_jtag_reg = RK312X_GRF_SOC_CON0;
1508                         } else if (soc_is_rk3126() || soc_is_rk3126b()) {
1509                                 force_jtag_reg = RK312X_GRF_SOC_CON0;
1510                                 force_jtag_bit = 8;
1511                         }
1512                         msleep(10);
1513                         if (gpio_val == gpio_get_value(gpio_cd)) {
1514                                 gpio_cd = (gpio_val == 0 ? 1 : 0);
1515                                 if (gpio_cd == 0) {
1516                                         irq_set_irq_type(irq, IRQF_TRIGGER_LOW | IRQF_ONESHOT);
1517                                         /* Enable force_jtag wihtout card in slot, ONLY for NCD-package */
1518                                         grf_writel((0x1 << (force_jtag_bit + 16)) | (1 << force_jtag_bit), 
1519                                                         force_jtag_reg);
1520                                         dw_mci_ctrl_all_reset(host);
1521                                 } else {
1522                                         irq_set_irq_type(irq, IRQF_TRIGGER_HIGH | IRQF_ONESHOT);
1523                                         /* Really card detected: SHOULD disable force_jtag */
1524                                         grf_writel((0x1 << (force_jtag_bit + 16)) | (0 << force_jtag_bit),
1525                                                         force_jtag_reg);
1526                                 }
1527                         } else {
1528                                 /* Jitter */
1529                                 gpio_val = gpio_get_value(gpio_cd);
1530                                 (gpio_val == 0) ? 
1531                                         irq_set_irq_type(irq, IRQF_TRIGGER_HIGH  | IRQF_ONESHOT) :
1532                                         irq_set_irq_type(irq, IRQF_TRIGGER_LOW  | IRQF_ONESHOT);
1533                                 return slot->last_detect_state;
1534                         }
1535                 } else {
1536                         dev_err(host->dev, "dw_mci_get_cd: invalid gpio_cd!\n");
1537                 }
1538         }
1539
1540         if (mmc->restrict_caps & RESTRICT_CARD_TYPE_SDIO)
1541                 return test_bit(DW_MMC_CARD_PRESENT, &slot->flags);
1542
1543         /* Use platform get_cd function, else try onboard card detect */
1544         if (brd->quirks & DW_MCI_QUIRK_BROKEN_CARD_DETECTION)
1545                 present = 1;
1546         else if (brd->get_cd)
1547                 present = !brd->get_cd(slot->id);
1548         else if (!IS_ERR_VALUE(gpio_cd))
1549                 present = gpio_cd;
1550         else
1551                 present = ((mci_readl(slot->host, CDETECT) & (1 << slot->id))
1552                         == 0) ? 1 : 0;
1553
1554         spin_lock_bh(&host->lock);
1555         if (present) {
1556                 set_bit(DW_MMC_CARD_PRESENT, &slot->flags);
1557                 dev_dbg(&mmc->class_dev, "card is present\n");
1558         } else {
1559                 clear_bit(DW_MMC_CARD_PRESENT, &slot->flags);
1560                 dev_dbg(&mmc->class_dev, "card is not present\n");
1561         }
1562         spin_unlock_bh(&host->lock);
1563
1564         return present;
1565 }
1566
1567
1568 /*
1569  * Dts Should caps emmc controller with poll-hw-reset
1570  */
1571 static void dw_mci_hw_reset(struct mmc_host *mmc)
1572 {
1573         struct dw_mci_slot *slot = mmc_priv(mmc);
1574         struct dw_mci *host = slot->host;
1575         u32 regs;
1576
1577         #if 0
1578         u32 cmd_flags;
1579         unsigned long timeout;
1580         bool ret = true;
1581
1582         /* (1) CMD12 to end any transfer in process */
1583         cmd_flags = SDMMC_CMD_STOP | SDMMC_CMD_RESP_CRC
1584                         | SDMMC_CMD_RESP_EXP | MMC_STOP_TRANSMISSION;
1585
1586         if(host->mmc->hold_reg_flag)
1587                 cmd_flags |= SDMMC_CMD_USE_HOLD_REG;
1588         mci_writel(host, CMDARG, 0);
1589         wmb();
1590         mci_writel(host, CMD, cmd_flags | SDMMC_CMD_START);
1591         wmb();
1592         timeout = jiffies + msecs_to_jiffies(500);
1593         while(ret){
1594                 ret = time_before(jiffies, timeout);
1595                 if(!(mci_readl(host, CMD) & SDMMC_CMD_START))
1596                         break;
1597         }
1598         
1599         if(false == ret)
1600                 MMC_DBG_ERR_FUNC(host->mmc,
1601                         "%s dw_mci_hw_reset: STOP_TRANSMISSION failed!!! [%s]\n",
1602                         __func__, mmc_hostname(host->mmc));
1603         
1604         /* (2) wait DTO, even if no response is sent back by card */
1605         ret = true;
1606         timeout = jiffies + msecs_to_jiffies(5);
1607         while(ret){
1608                 ret = time_before(jiffies, timeout);
1609                 if(!(mci_readl(host, MINTSTS) & SDMMC_INT_DATA_OVER)){
1610                         mci_writel(host, RINTSTS, SDMMC_INT_DATA_OVER);
1611                         break;
1612                 }
1613         }
1614         #endif
1615
1616         /* (3) Reset following: DONNOT CHANGE RESET ORDER!*/
1617
1618         /* Software reset - BMOD[0] for IDMA only */
1619         regs = mci_readl(host, BMOD);
1620         regs |= SDMMC_IDMAC_SWRESET;
1621         mci_writel(host, BMOD, regs);
1622         udelay(1); /* Auto cleared after 1 cycle, 1us is enough for hclk_mmc */
1623         regs = mci_readl(host, BMOD);
1624         if(regs & SDMMC_IDMAC_SWRESET)
1625                 MMC_DBG_WARN_FUNC(host->mmc,
1626                         "%s dw_mci_hw_reset: SDMMC_IDMAC_SWRESET failed!!! [%s]\n",
1627                         __func__, mmc_hostname(host->mmc));
1628
1629         /* DMA reset - CTRL[2] */
1630         regs = mci_readl(host, CTRL);
1631         regs |= SDMMC_CTRL_DMA_RESET;
1632         mci_writel(host, CTRL, regs);
1633         udelay(1); /* Auto cleared after 2 AHB clocks, 1us is enough plus mci_readl access */
1634         regs = mci_readl(host, CTRL);
1635         if(regs & SDMMC_CTRL_DMA_RESET)
1636                 MMC_DBG_WARN_FUNC(host->mmc,
1637                         "%s dw_mci_hw_reset: SDMMC_CTRL_DMA_RESET failed!!! [%s]\n",
1638                         __func__, mmc_hostname(host->mmc));
1639
1640         /* FIFO reset - CTRL[1] */
1641         regs = mci_readl(host, CTRL);
1642         regs |= SDMMC_CTRL_FIFO_RESET;
1643         mci_writel(host, CTRL, regs);
1644         mdelay(1); /* no timing limited, 1ms is random value */
1645         regs = mci_readl(host, CTRL);
1646         if(regs & SDMMC_CTRL_FIFO_RESET)
1647                 MMC_DBG_WARN_FUNC(host->mmc,
1648                         "%s dw_mci_hw_reset: SDMMC_CTRL_DMA_RESET failed!!! [%s]\n",
1649                         __func__, mmc_hostname(host->mmc));
1650
1651         /* (4) CARD_RESET
1652         According to eMMC spec
1653         tRstW >= 1us ;   RST_n pulse width
1654         tRSCA >= 200us ; RST_n to Command time
1655         tRSTH >= 1us ;   RST_n high period
1656         */
1657         mci_writel(slot->host, PWREN, 0x0);
1658         mci_writel(slot->host, RST_N, 0x0);
1659         dsb(sy);
1660         udelay(10); /* 10us for bad quality eMMc. */
1661
1662         mci_writel(slot->host, PWREN, 0x1);
1663         mci_writel(slot->host, RST_N, 0x1);
1664         dsb(sy);
1665         usleep_range(500, 1000); /* at least 500(> 200us) */
1666 }
1667
1668 /*
1669  * Disable lower power mode.
1670  *
1671  * Low power mode will stop the card clock when idle.  According to the
1672  * description of the CLKENA register we should disable low power mode
1673  * for SDIO cards if we need SDIO interrupts to work.
1674  *
1675  * This function is fast if low power mode is already disabled.
1676  */
1677 static void dw_mci_disable_low_power(struct dw_mci_slot *slot)
1678 {
1679         struct dw_mci *host = slot->host;
1680         u32 clk_en_a;
1681         const u32 clken_low_pwr = SDMMC_CLKEN_LOW_PWR << slot->id;
1682
1683         clk_en_a = mci_readl(host, CLKENA);
1684
1685         if (clk_en_a & clken_low_pwr) {
1686                 mci_writel(host, CLKENA, clk_en_a & ~clken_low_pwr);
1687                 mci_send_cmd(slot, SDMMC_CMD_UPD_CLK |
1688                              SDMMC_CMD_PRV_DAT_WAIT, 0);
1689         }
1690 }
1691
1692 static void dw_mci_enable_sdio_irq(struct mmc_host *mmc, int enb)
1693 {
1694         struct dw_mci_slot *slot = mmc_priv(mmc);
1695         struct dw_mci *host = slot->host;
1696         unsigned long flags;
1697         u32 int_mask;
1698         u32 sdio_int;
1699
1700         spin_lock_irqsave(&host->slock, flags);
1701
1702         /* Enable/disable Slot Specific SDIO interrupt */
1703         int_mask = mci_readl(host, INTMASK);
1704
1705         if (host->verid < DW_MMC_240A)
1706                 sdio_int = SDMMC_INT_SDIO(slot->id);
1707         else
1708                 sdio_int = SDMMC_INT_SDIO((slot->id) + 8);
1709         
1710         if (enb) {
1711                 /*
1712                  * Turn off low power mode if it was enabled.  This is a bit of
1713                  * a heavy operation and we disable / enable IRQs a lot, so
1714                  * we'll leave low power mode disabled and it will get
1715                  * re-enabled again in dw_mci_setup_bus().
1716                  */
1717                 dw_mci_disable_low_power(slot);
1718
1719                 mci_writel(host, INTMASK,
1720                            (int_mask | sdio_int));
1721         } else {
1722                 mci_writel(host, INTMASK,
1723                            (int_mask & ~sdio_int));
1724         }
1725
1726         spin_unlock_irqrestore(&host->slock, flags);
1727 }
1728
1729 #ifdef CONFIG_MMC_DW_ROCKCHIP_SWITCH_VOLTAGE
1730 enum{
1731         IO_DOMAIN_12 = 1200,
1732         IO_DOMAIN_18 = 1800,
1733         IO_DOMAIN_33 = 3300,
1734 };
1735 static void dw_mci_do_grf_io_domain_switch(struct dw_mci *host, u32 voltage)
1736 {
1737         switch(voltage){
1738         case IO_DOMAIN_33:
1739                 voltage = 0;
1740                 break;
1741         case IO_DOMAIN_18:
1742                 voltage = 1;
1743                 break;
1744         case IO_DOMAIN_12:
1745                 MMC_DBG_ERR_FUNC(host->mmc,
1746                         "%s : Not support io domain voltage [%s]\n",
1747                         __FUNCTION__, mmc_hostname(host->mmc));
1748                 break;
1749         default:
1750                 MMC_DBG_ERR_FUNC(host->mmc,
1751                         "%s : Err io domain voltage [%s]\n",
1752                         __FUNCTION__, mmc_hostname(host->mmc));
1753                 break;
1754         }
1755
1756         if (cpu_is_rk3288()) {
1757                 if(host->mmc->restrict_caps & RESTRICT_CARD_TYPE_SD)
1758                         grf_writel((voltage << 7) | (1 << 23), RK3288_GRF_IO_VSEL);
1759                 else
1760                         return ;
1761         } else if (host->cid == DW_MCI_TYPE_RK3368) {
1762                 if(host->mmc->restrict_caps & RESTRICT_CARD_TYPE_SD)
1763                          regmap_write(host->grf, 0x900, (voltage << 6) | (1 << 22));    
1764                 else
1765                         return;
1766         } else {
1767                 MMC_DBG_ERR_FUNC(host->mmc,
1768                         "%s : unknown chip [%s]\n",
1769                         __FUNCTION__, mmc_hostname(host->mmc));
1770         }
1771 }
1772
1773 static int dw_mci_do_start_signal_voltage_switch(struct dw_mci *host,
1774                                                 struct mmc_ios *ios)
1775 {
1776         int ret;
1777         unsigned int value,uhs_reg;
1778
1779         /*
1780          * Signal Voltage Switching is only applicable for Host Controllers
1781          * v3.00 and above.
1782          */
1783         if (host->verid < DW_MMC_240A)
1784                 return 0;
1785
1786         uhs_reg = mci_readl(host, UHS_REG);
1787         MMC_DBG_SW_VOL_FUNC(host->mmc, "%s: vol=%d.[%s]\n",
1788                  __FUNCTION__, ios->signal_voltage, mmc_hostname(host->mmc));
1789
1790         switch (ios->signal_voltage) {
1791         case MMC_SIGNAL_VOLTAGE_330:
1792         /* Set 1.8V Signal Enable in the Host Control2 register to 0 */
1793                 if (host->vmmc) {
1794                         if (cpu_is_rk3288())
1795                                 ret = io_domain_regulator_set_voltage(
1796                                                 host->vmmc, 3300000, 3300000);
1797                         else
1798                                 ret = regulator_set_voltage(host->vmmc,
1799                                                         3300000, 3300000);
1800
1801                         /* regulator_put(host->vmmc); */
1802                         MMC_DBG_SW_VOL_FUNC(host->mmc,"%s =%dmV set 3.3 end, ret = %d\n",
1803                                 __func__, regulator_get_voltage(host->vmmc), ret);
1804                         if (ret) {
1805                                 MMC_DBG_SW_VOL_FUNC(host->mmc,
1806                                         "%s: Switching to 3.3V signalling voltage "
1807                                         " failed\n", mmc_hostname(host->mmc));
1808                                 return -EIO;
1809                         }
1810                         dw_mci_do_grf_io_domain_switch(host, IO_DOMAIN_33);
1811                 }
1812
1813                 MMC_DBG_SW_VOL_FUNC(host->mmc, "%s: [%s]\n",
1814                                 __FUNCTION__, mmc_hostname(host->mmc));
1815
1816                 /* set High-power mode */
1817                 value = mci_readl(host, CLKENA);
1818                 value &= ~SDMMC_CLKEN_LOW_PWR;
1819                 mci_writel(host,CLKENA , value);
1820                 /* SDMMC_UHS_REG */
1821                 uhs_reg &= ~SDMMC_UHS_VOLT_REG_18;
1822                 mci_writel(host,UHS_REG , uhs_reg);
1823
1824                 /* Wait for 5ms */
1825                 usleep_range(5000, 5500);
1826
1827                 /* 3.3V regulator output should be stable within 5 ms */
1828                 uhs_reg = mci_readl(host, UHS_REG);
1829                 if( !(uhs_reg & SDMMC_UHS_VOLT_REG_18))
1830                         return 0;
1831
1832                 MMC_DBG_SW_VOL_FUNC(host->mmc,
1833                         "%s: 3.3V regulator output did not became stable\n",
1834                         mmc_hostname(host->mmc));
1835
1836                 return -EAGAIN;
1837         case MMC_SIGNAL_VOLTAGE_180:
1838                 if (host->vmmc) {
1839                         if (cpu_is_rk3288())
1840                                 ret = io_domain_regulator_set_voltage(
1841                                         host->vmmc, 1800000, 1800000);
1842                         else
1843                                 ret = regulator_set_voltage(
1844                                                 host->vmmc, 1800000, 1800000);
1845                         /* regulator_put(host->vmmc); */
1846                         MMC_DBG_SW_VOL_FUNC(host->mmc,
1847                                         "%s   =%dmV  set 1.8end, ret=%d . \n",
1848                                         __func__, regulator_get_voltage(host->vmmc), ret);
1849                         if (ret) {
1850                                 MMC_DBG_SW_VOL_FUNC(host->mmc,
1851                                         "%s: Switching to 1.8V signalling voltage "
1852                                         " failed\n", mmc_hostname(host->mmc));
1853                                 return -EIO;
1854                         }
1855                         dw_mci_do_grf_io_domain_switch(host, IO_DOMAIN_18);
1856                 }
1857
1858                 /*
1859                 * Enable 1.8V Signal Enable in the Host Control2
1860                 * register
1861                 */
1862                 mci_writel(host,UHS_REG , uhs_reg | SDMMC_UHS_VOLT_REG_18);
1863
1864                 /* Wait for 5ms */
1865                 usleep_range(5000, 5500);
1866                 MMC_DBG_SW_VOL_FUNC(host->mmc, "%d..%s: .[%s]\n",__LINE__,
1867                                 __FUNCTION__, mmc_hostname(host->mmc));
1868
1869                 /* 1.8V regulator output should be stable within 5 ms */
1870                 uhs_reg = mci_readl(host, UHS_REG);
1871                 if (uhs_reg & SDMMC_UHS_VOLT_REG_18)
1872                         return 0;
1873
1874                 MMC_DBG_SW_VOL_FUNC(host->mmc,
1875                         "%s: 1.8V regulator output did not became stable\n",
1876                         mmc_hostname(host->mmc));
1877
1878                 return -EAGAIN;
1879         case MMC_SIGNAL_VOLTAGE_120:
1880                 if (host->vmmc) {
1881                         if (cpu_is_rk3288())
1882                                 ret = io_domain_regulator_set_voltage(
1883                                         host->vmmc, 1200000, 1200000);
1884                         else
1885                                 ret = regulator_set_voltage(host->vmmc,
1886                                         1200000, 1200000);
1887                         if (ret) {
1888                                 MMC_DBG_SW_VOL_FUNC(host->mmc,
1889                                         "%s: Switching to 1.2V signalling voltage "
1890                                         " failed\n", mmc_hostname(host->mmc));
1891                                 return -EIO;
1892                         }
1893                 }
1894                 return 0;
1895         default:
1896                 /* No signal voltage switch required */
1897                 return 0;
1898         }
1899 }
1900
1901
1902 static int dw_mci_start_signal_voltage_switch(struct mmc_host *mmc,
1903         struct mmc_ios *ios)
1904 {
1905         struct dw_mci_slot *slot = mmc_priv(mmc);
1906         struct dw_mci *host = slot->host;
1907         int err;
1908
1909         if (host->verid < DW_MMC_240A)
1910                 return 0;
1911         
1912         err = dw_mci_do_start_signal_voltage_switch(host, ios);
1913         return err;
1914 }
1915
1916 #endif
1917
1918 static int dw_mci_execute_tuning(struct mmc_host *mmc, u32 opcode)
1919 {
1920         struct dw_mci_slot *slot = mmc_priv(mmc);
1921         struct dw_mci *host = slot->host;
1922         const struct dw_mci_drv_data *drv_data = host->drv_data;
1923         struct dw_mci_tuning_data tuning_data;
1924         int err = -ENOSYS;
1925
1926         /* Fixme: 3036/3126 doesn't support 1.8 io domain, no sense exe tuning */
1927         if(cpu_is_rk3036() || cpu_is_rk312x())
1928                 return err;
1929
1930         if (opcode == MMC_SEND_TUNING_BLOCK_HS200) {
1931                 if (mmc->ios.bus_width == MMC_BUS_WIDTH_8) {
1932                         tuning_data.blk_pattern = tuning_blk_pattern_8bit;
1933                         tuning_data.blksz = sizeof(tuning_blk_pattern_8bit);
1934                 } else if (mmc->ios.bus_width == MMC_BUS_WIDTH_4) {
1935                         tuning_data.blk_pattern = tuning_blk_pattern_4bit;
1936                         tuning_data.blksz = sizeof(tuning_blk_pattern_4bit);
1937                 } else {
1938                         return -EINVAL;
1939                 }
1940         } else if (opcode == MMC_SEND_TUNING_BLOCK) {
1941                 tuning_data.blk_pattern = tuning_blk_pattern_4bit;
1942                 tuning_data.blksz = sizeof(tuning_blk_pattern_4bit);
1943         } else {
1944                 dev_err(host->dev,
1945                         "Undefined command(%d) for tuning\n", opcode);
1946                 return -EINVAL;
1947         }
1948
1949     
1950         /* Recommend sample phase and delayline
1951            Fixme: Mix-use these three controllers will cause
1952            con_id mismatch.
1953          */
1954         if (mmc->restrict_caps & RESTRICT_CARD_TYPE_EMMC)
1955             tuning_data.con_id = 3;
1956         else if(mmc->restrict_caps & RESTRICT_CARD_TYPE_SDIO)
1957             tuning_data.con_id = 1;
1958         else
1959             tuning_data.con_id = 0;     
1960
1961         /* 0: driver, from host->devices
1962            1: sample, from devices->host
1963          */    
1964         tuning_data.tuning_type = 1; 
1965    
1966         if (drv_data && drv_data->execute_tuning)
1967                 err = drv_data->execute_tuning(slot, opcode, &tuning_data);
1968                 
1969         return err;
1970 }
1971
1972 static void dw_mci_post_tmo(struct mmc_host *mmc)
1973 {
1974         struct dw_mci_slot *slot = mmc_priv(mmc);
1975         struct dw_mci *host = slot->host;
1976         struct mmc_data *data;
1977         u32 ret, i, regs, cmd_flags;
1978         u32 sdio_int;
1979         unsigned long timeout = 0;
1980         bool ret_timeout = true;
1981
1982         host->cur_slot->mrq = NULL;
1983         host->mrq = NULL;
1984         host->state = STATE_IDLE;
1985
1986         data = host->data;
1987
1988         printk("[%s] -- Timeout recovery procedure start --\n",
1989                 mmc_hostname(host->mmc));
1990
1991         if (data && (data->stop)) {
1992                 send_stop_cmd(host, data);
1993         } else {
1994                 mci_writel(host, CMDARG, 0);
1995                 wmb();
1996                 cmd_flags = SDMMC_CMD_STOP | SDMMC_CMD_RESP_CRC |
1997                         SDMMC_CMD_RESP_EXP | MMC_STOP_TRANSMISSION;
1998
1999                 if (host->mmc->hold_reg_flag)
2000                         cmd_flags |= SDMMC_CMD_USE_HOLD_REG;
2001
2002                 mci_writel(host, CMD, cmd_flags | SDMMC_CMD_START);
2003                 wmb();
2004                 timeout = jiffies + msecs_to_jiffies(500);
2005
2006                 while(ret_timeout) {
2007                         ret_timeout = time_before(jiffies, timeout);
2008                         if(!(mci_readl(host, CMD) & SDMMC_CMD_START))
2009                                 break;
2010                 }
2011
2012                 if (false == ret_timeout)
2013                         MMC_DBG_ERR_FUNC(host->mmc, "stop recovery failed![%s]",
2014                                         mmc_hostname(host->mmc));
2015         }
2016
2017         if (!dw_mci_ctrl_all_reset(host)) {
2018                 ret = -ENODEV;
2019                 return ;
2020         }
2021
2022 #ifdef CONFIG_MMC_DW_IDMAC
2023         if (host->use_dma && host->dma_ops->init)
2024                 host->dma_ops->init(host);
2025 #endif
2026
2027         /*
2028         * Restore the initial value at FIFOTH register
2029         * And Invalidate the prev_blksz with zero
2030         */
2031         mci_writel(host, FIFOTH, host->fifoth_val);
2032         host->prev_blksz = 0;
2033         mci_writel(host, TMOUT, 0xFFFFFFFF);
2034         mci_writel(host, RINTSTS, 0xFFFFFFFF);
2035         regs = SDMMC_INT_CMD_DONE | SDMMC_INT_DATA_OVER | SDMMC_INT_TXDR
2036                         | SDMMC_INT_RXDR | SDMMC_INT_VSI | DW_MCI_ERROR_FLAGS;
2037         if (!(host->mmc->restrict_caps & RESTRICT_CARD_TYPE_SDIO))
2038                 regs |= SDMMC_INT_CD;
2039
2040         if ((host->mmc->restrict_caps & RESTRICT_CARD_TYPE_SDIO)) {
2041                 if (host->verid < DW_MMC_240A)
2042                         sdio_int = SDMMC_INT_SDIO(0);
2043                 else
2044                         sdio_int = SDMMC_INT_SDIO(8);
2045
2046                 if (mci_readl(host, INTMASK) & sdio_int)
2047                         regs |= sdio_int;
2048         }
2049
2050         mci_writel(host, INTMASK, regs);
2051         mci_writel(host, CTRL, SDMMC_CTRL_INT_ENABLE);
2052         for (i = 0; i < host->num_slots; i++) {
2053                 struct dw_mci_slot *slot = host->slot[i];
2054                 if (!slot)
2055                         continue;
2056                 if (slot->mmc->pm_flags & MMC_PM_KEEP_POWER) {
2057                         dw_mci_set_ios(slot->mmc, &slot->mmc->ios);
2058                         dw_mci_setup_bus(slot, true);
2059                 }
2060         }
2061         mci_writel(host, RINTSTS, 0xFFFFFFFF);
2062
2063         printk("[%s] -- Timeout recovery procedure finished --\n",
2064                 mmc_hostname(host->mmc));
2065 }
2066
2067 static const struct mmc_host_ops dw_mci_ops = {
2068         .request                = dw_mci_request,
2069         .pre_req                = dw_mci_pre_req,
2070         .post_req               = dw_mci_post_req,
2071         .set_ios                = dw_mci_set_ios,
2072         .get_ro                 = dw_mci_get_ro,
2073         .get_cd                 = dw_mci_get_cd,
2074         .set_sdio_status        = dw_mci_set_sdio_status,
2075         .hw_reset               = dw_mci_hw_reset,
2076         .enable_sdio_irq        = dw_mci_enable_sdio_irq,
2077         .execute_tuning         = dw_mci_execute_tuning,
2078         .post_tmo               = dw_mci_post_tmo,
2079         #ifdef CONFIG_MMC_DW_ROCKCHIP_SWITCH_VOLTAGE
2080         .start_signal_voltage_switch
2081                                 = dw_mci_start_signal_voltage_switch,
2082         .card_busy              = dw_mci_card_busy,
2083         #endif
2084 };
2085
2086 static void dw_mci_deal_data_end(struct dw_mci *host, struct mmc_request *mrq)
2087         __releases(&host->lock)
2088         __acquires(&host->lock)
2089 {
2090         if (DW_MCI_SEND_STATUS == host->dir_status){
2091                 dw_mci_wait_unbusy(host);
2092         }
2093 }
2094
2095 static void dw_mci_request_end(struct dw_mci *host, struct mmc_request *mrq)
2096         __releases(&host->lock)
2097         __acquires(&host->lock)
2098 {
2099         struct dw_mci_slot *slot;
2100         struct mmc_host *prev_mmc = host->cur_slot->mmc;
2101
2102         //WARN_ON(host->cmd || host->data);
2103
2104         dw_mci_deal_data_end(host, mrq);
2105
2106         if(mrq->cmd)
2107                 MMC_DBG_CMD_FUNC(host->mmc,
2108                         " reqeust end--reqeuest done, cmd=%d, "
2109                         "cmderr=%d, host->state=%d [%s]",
2110                         mrq->cmd->opcode, mrq->cmd->error,
2111                         host->state,mmc_hostname(host->mmc));
2112         if(mrq->data)
2113                 MMC_DBG_CMD_FUNC(host->mmc,
2114                         " reqeust end--reqeuest done, cmd=%d, "
2115                         "dataerr=%d, host->state=%d [%s]",
2116                         mrq->cmd->opcode,mrq->data->error,
2117                         host->state, mmc_hostname(host->mmc));
2118
2119         host->cur_slot->mrq = NULL;
2120         host->mrq = NULL;
2121         if (!list_empty(&host->queue)) {
2122                 slot = list_entry(host->queue.next,
2123                                   struct dw_mci_slot, queue_node);
2124                 list_del(&slot->queue_node);
2125                 dev_vdbg(host->dev, "list not empty: %s is next\n",
2126                          mmc_hostname(slot->mmc));
2127                 host->state = STATE_SENDING_CMD;
2128                 MMC_DBG_CMD_FUNC(host->mmc,
2129                         " list is not empty. run the request in list. [%s]",
2130                         mmc_hostname(host->mmc));
2131                 dw_mci_start_request(host, slot);
2132         } else {
2133                 dev_vdbg(host->dev, "list empty\n");
2134                 host->state = STATE_IDLE;
2135         }
2136
2137         spin_unlock(&host->lock);
2138         mmc_request_done(prev_mmc, mrq);
2139         spin_lock(&host->lock);
2140 }
2141
2142 static void dw_mci_command_complete(struct dw_mci *host, struct mmc_command *cmd)
2143 {
2144         u32 status = host->cmd_status;
2145
2146         host->cmd_status = 0;
2147
2148         /* Read the response from the card (up to 16 bytes) */
2149         if (cmd->flags & MMC_RSP_PRESENT) {
2150                 if (cmd->flags & MMC_RSP_136) {
2151                         cmd->resp[3] = mci_readl(host, RESP0);
2152                         cmd->resp[2] = mci_readl(host, RESP1);
2153                         cmd->resp[1] = mci_readl(host, RESP2);
2154                         cmd->resp[0] = mci_readl(host, RESP3);
2155                         
2156             MMC_DBG_INFO_FUNC(host->mmc,
2157                 "Command complete cmd=%d, "
2158                 "resp[3]=0x%x, resp[2]=0x%x,resp[1]=0x%x,resp[0]=0x%x.[%s]",
2159                 cmd->opcode,cmd->resp[3], cmd->resp[2], cmd->resp[1], cmd->resp[0],
2160                 mmc_hostname(host->mmc));
2161         } else {
2162                 cmd->resp[0] = mci_readl(host, RESP0);
2163                 cmd->resp[1] = 0;
2164                 cmd->resp[2] = 0;
2165                 cmd->resp[3] = 0;
2166                 MMC_DBG_INFO_FUNC(host->mmc,
2167                         "Command complete cmd=%d,resp[0]=0x%x. [%s]",
2168                         cmd->opcode, cmd->resp[0], mmc_hostname(host->mmc));
2169                 }
2170         }
2171
2172         if (status & SDMMC_INT_RTO)
2173         {
2174                 if(host->mmc->restrict_caps & RESTRICT_CARD_TYPE_SDIO)
2175                         host->cmd_rto += 1;
2176
2177                 cmd->error = -ETIMEDOUT;
2178         }else if ((cmd->flags & MMC_RSP_CRC) && (status & SDMMC_INT_RCRC)){
2179                 cmd->error = -EILSEQ;
2180         }else if (status & SDMMC_INT_RESP_ERR){
2181                 cmd->error = -EIO;
2182         }else{
2183                 cmd->error = 0;
2184         }
2185
2186         MMC_DBG_CMD_FUNC(host->mmc,
2187                         "Command complete, cmd=%d,cmdError=%d [%s]",
2188                         cmd->opcode, cmd->error, mmc_hostname(host->mmc));
2189
2190         if (cmd->error) {
2191                 if(MMC_SEND_STATUS != cmd->opcode)
2192                         if(host->cmd_rto >= SDMMC_CMD_RTO_MAX_HOLD){
2193                                 MMC_DBG_CMD_FUNC(host->mmc,
2194                                         "Command complete, cmd=%d,cmdError=%d [%s]",
2195                                         cmd->opcode, cmd->error, mmc_hostname(host->mmc));
2196                                 host->cmd_rto = 0;
2197                         }
2198
2199                 /* newer ip versions need a delay between retries */
2200                 if (host->quirks & DW_MCI_QUIRK_RETRY_DELAY)
2201                         mdelay(20);
2202         }
2203 }
2204
2205 static void dw_mci_tasklet_func(unsigned long priv)
2206 {
2207         struct dw_mci *host = (struct dw_mci *)priv;
2208         struct dw_mci_slot *slot = mmc_priv(host->mmc);
2209         struct mmc_data *data;
2210         struct mmc_command *cmd;
2211         enum dw_mci_state state;
2212         enum dw_mci_state prev_state;
2213         u32 status, cmd_flags;
2214         unsigned long timeout = 0;
2215         bool ret = true;
2216
2217         spin_lock(&host->lock);
2218
2219         state = host->state;
2220         data = host->data;
2221
2222         do {
2223                 prev_state = state;
2224
2225                 switch (state) {
2226                 case STATE_IDLE:
2227                         break;
2228
2229                 case STATE_SENDING_CMD:
2230                         if (!test_and_clear_bit(EVENT_CMD_COMPLETE,
2231                                                 &host->pending_events))
2232                                 break;
2233
2234                         cmd = host->cmd;
2235                         host->cmd = NULL;
2236                         set_bit(EVENT_CMD_COMPLETE, &host->completed_events);
2237                         dw_mci_command_complete(host, cmd);
2238                         if (cmd == host->mrq->sbc && !cmd->error) {
2239                                 prev_state = state = STATE_SENDING_CMD;
2240                                 __dw_mci_start_request(host, host->cur_slot,
2241                                                        host->mrq->cmd);
2242                                 goto unlock;
2243                         }
2244                         
2245                         if (cmd->data && cmd->error) {
2246                                 dw_mci_stop_dma(host);
2247                                 #if 1
2248                                 if (data->stop) {
2249                                         send_stop_cmd(host, data);
2250                                         state = STATE_SENDING_STOP;
2251                                         break;
2252                                 }else{
2253                                        /*  host->data = NULL; */
2254                                 }
2255                                 #else
2256                                 send_stop_abort(host, data);
2257                                 state = STATE_SENDING_STOP;
2258                                 break;
2259                                 #endif
2260                                 set_bit(EVENT_DATA_COMPLETE, &host->completed_events);
2261                         }
2262
2263                         if (!host->mrq->data || cmd->error) {
2264                                 dw_mci_request_end(host, host->mrq);
2265                                 goto unlock;
2266                         }
2267
2268                         prev_state = state = STATE_SENDING_DATA;
2269                         /* fall through */
2270
2271                 case STATE_SENDING_DATA:
2272                         if (test_and_clear_bit(EVENT_DATA_ERROR,
2273                                 &host->pending_events)) {
2274                                 dw_mci_stop_dma(host);
2275                                 #if 1
2276                                 if (data->stop){
2277                                         send_stop_cmd(host, data);
2278                                 }else{
2279                                         /*single block read/write, send stop cmd
2280                                         manually to prevent host controller halt*/
2281                                         MMC_DBG_INFO_FUNC(host->mmc,
2282                                                 "%s status 1 0x%08x [%s]\n",
2283                                                 __func__, mci_readl(host, STATUS),
2284                                                 mmc_hostname(host->mmc));
2285                         
2286                                         mci_writel(host, CMDARG, 0);
2287                                         wmb();
2288                                         cmd_flags = SDMMC_CMD_STOP | SDMMC_CMD_RESP_CRC |
2289                                                 SDMMC_CMD_RESP_EXP | MMC_STOP_TRANSMISSION;
2290
2291                                         if(host->mmc->hold_reg_flag)
2292                                                 cmd_flags |= SDMMC_CMD_USE_HOLD_REG;
2293
2294                                         mci_writel(host, CMD, cmd_flags | SDMMC_CMD_START);
2295                                         wmb();
2296                                         timeout = jiffies + msecs_to_jiffies(500);
2297
2298                                         while(ret) {
2299                                                 ret = time_before(jiffies, timeout);
2300                                                 if (!(mci_readl(host, CMD) & SDMMC_CMD_START))
2301                                                         break;
2302                                         }
2303                                         if(false == ret)
2304                                                 MMC_DBG_ERR_FUNC(host->mmc,
2305                                                         "%s EVENT_DATA_ERROR recovery failed!!! [%s]\n",
2306                                                         __func__, mmc_hostname(host->mmc));
2307                                 }
2308                                 #else
2309                                 send_stop_abort(host, data);
2310                                 #endif
2311                                 state = STATE_DATA_ERROR;
2312                                 break;
2313                         }
2314
2315                         MMC_DBG_CMD_FUNC(host->mmc, 
2316                                 "Pre-state[%d]-->NowState[%d]: STATE_SENDING_DATA, "
2317                                 "wait for EVENT_XFER_COMPLETE.[%s]",
2318                                 prev_state, state, mmc_hostname(host->mmc));
2319
2320                         if (!test_and_clear_bit(EVENT_XFER_COMPLETE,
2321                                 &host->pending_events))
2322                                 break;
2323                         MMC_DBG_INFO_FUNC(host->mmc,
2324                                 "Pre-state[%d]-->NowState[%d]: STATE_SENDING_DATA, "
2325                                 "wait for EVENT_DATA_COMPLETE. [%s]",
2326                                 prev_state, state, mmc_hostname(host->mmc));
2327
2328                         set_bit(EVENT_XFER_COMPLETE, &host->completed_events);
2329                         prev_state = state = STATE_DATA_BUSY;
2330                         /* fall through */
2331
2332                 case STATE_DATA_BUSY:
2333                         if (!test_and_clear_bit(EVENT_DATA_COMPLETE,
2334                                                 &host->pending_events))
2335                                 break;
2336
2337                         dw_mci_deal_data_end(host, host->mrq);                  
2338                         MMC_DBG_INFO_FUNC(host->mmc, 
2339                                 "Pre-state[%d]-->NowState[%d]: STATE_DATA_BUSY, "
2340                                 "after EVENT_DATA_COMPLETE. [%s]",
2341                                 prev_state, state, mmc_hostname(host->mmc));
2342
2343                         /* host->data = NULL; */
2344                         set_bit(EVENT_DATA_COMPLETE, &host->completed_events);
2345                         status = host->data_status;
2346
2347                         if (status & DW_MCI_DATA_ERROR_FLAGS) {
2348                                 if((SDMMC_CTYPE_1BIT != slot->ctype) &&
2349                                         (MMC_SEND_EXT_CSD == host->mrq->cmd->opcode))
2350                                         MMC_DBG_ERR_FUNC(host->mmc,
2351                                         "Pre-state[%d]-->NowState[%d]: DW_MCI_DATA_ERROR_FLAGS,"
2352                                         "datastatus=0x%x [%s]",
2353                                         prev_state, state, status, mmc_hostname(host->mmc));
2354
2355                         if (status & SDMMC_INT_DRTO) {
2356                                         data->error = -ETIMEDOUT;
2357                                 } else if (status & SDMMC_INT_DCRC) {
2358                                         data->error = -EILSEQ;
2359                                 } else if (status & SDMMC_INT_EBE &&
2360                                 host->dir_status == DW_MCI_SEND_STATUS){
2361                                         /*
2362                                          * No data CRC status was returned.
2363                                          * The number of bytes transferred will
2364                                          * be exaggerated in PIO mode.
2365                                          */
2366                                         data->bytes_xfered = 0;
2367                                         data->error = -ETIMEDOUT;
2368                                 } else {
2369                                         dev_err(host->dev,
2370                                                 "data FIFO error "
2371                                                 "(status=%08x)\n",
2372                                                 status);
2373                                         data->error = -EIO;
2374                                 }
2375                                 /*
2376                                  * After an error, there may be data lingering
2377                                  * in the FIFO, so reset it - doing so
2378                                  * generates a block interrupt, hence setting
2379                                  * the scatter-gather pointer to NULL.
2380                                  */
2381                                 dw_mci_fifo_reset(host);
2382                         } else {
2383                                 data->bytes_xfered = data->blocks * data->blksz;
2384                                 data->error = 0;
2385                         }
2386
2387                         if (!data->stop) {
2388                                 MMC_DBG_CMD_FUNC(host->mmc,
2389                                         "Pre-state[%d]-->NowState[%d]: "
2390                                         "no stop and no dataerr, exit [%s]",
2391                                         prev_state, state, mmc_hostname(host->mmc));
2392                                 dw_mci_request_end(host, host->mrq);
2393                                 goto unlock;
2394                         }
2395                         MMC_DBG_CMD_FUNC(host->mmc,
2396                                 "Pre-state[%d]-->NowState[%d]: begin to stop [%s]",
2397                                 prev_state, state, mmc_hostname(host->mmc));
2398
2399                         if (host->mrq->sbc && !data->error) {
2400                                 data->stop->error = 0;
2401
2402                                 MMC_DBG_CMD_FUNC(host->mmc,
2403                                         "Pre-state[%d]-->NowState[%d]: have stop and sbc, exit [%s]",
2404                                         prev_state,state,mmc_hostname(host->mmc));
2405
2406                                 dw_mci_request_end(host, host->mrq);
2407                                 goto unlock;
2408                         }
2409
2410                         prev_state = state = STATE_SENDING_STOP;
2411                         if (!data->error)
2412                                 send_stop_cmd(host, data);
2413                         /* fall through */
2414                         MMC_DBG_CMD_FUNC(host->mmc,
2415                                 "Pre-state[%d]-->NowState[%d]: begin to STATE_SENDING_STOP [%s]",
2416                                 prev_state, state, mmc_hostname(host->mmc));
2417
2418                 case STATE_SENDING_STOP:
2419                         if (!test_and_clear_bit(EVENT_CMD_COMPLETE,
2420                                 &host->pending_events))
2421                                 break;
2422
2423                         MMC_DBG_CMD_FUNC(host->mmc,
2424                                 "Pre-state[%d]-->NowState[%d]: begin to send cmd12 [%s]",
2425                                 prev_state, state, mmc_hostname(host->mmc));
2426
2427                         /* CMD error in data command */
2428                         if (host->mrq->cmd->error && host->mrq->data) {
2429                                 dw_mci_fifo_reset(host);
2430                         }
2431
2432                         /*
2433                         host->cmd = NULL;
2434                         host->data = NULL;
2435                         */
2436                         #if 1
2437                         dw_mci_command_complete(host, host->mrq->stop);
2438                         #else
2439                         if (host->mrq->stop)
2440                                 dw_mci_command_complete(host, host->mrq->stop);
2441                         else
2442                                 host->cmd_status = 0;
2443                         #endif
2444             
2445                         dw_mci_request_end(host, host->mrq);
2446                         goto unlock;
2447
2448                 case STATE_DATA_ERROR:
2449                         if (!test_and_clear_bit(EVENT_XFER_COMPLETE,
2450                                                 &host->pending_events))
2451                                 break;
2452
2453                         state = STATE_DATA_BUSY;
2454                         break;
2455                 }
2456         } while (state != prev_state);
2457
2458         host->state = state;
2459 unlock:
2460         spin_unlock(&host->lock);
2461 }
2462
2463 /* push final bytes to part_buf, only use during push */
2464 static void dw_mci_set_part_bytes(struct dw_mci *host, void *buf, int cnt)
2465 {
2466         memcpy((void *)&host->part_buf, buf, cnt);
2467         host->part_buf_count = cnt;
2468 }
2469
2470 /* append bytes to part_buf, only use during push */
2471 static int dw_mci_push_part_bytes(struct dw_mci *host, void *buf, int cnt)
2472 {
2473         cnt = min(cnt, (1 << host->data_shift) - host->part_buf_count);
2474         memcpy((void *)&host->part_buf + host->part_buf_count, buf, cnt);
2475         host->part_buf_count += cnt;
2476         return cnt;
2477 }
2478
2479 /* pull first bytes from part_buf, only use during pull */
2480 static int dw_mci_pull_part_bytes(struct dw_mci *host, void *buf, int cnt)
2481 {
2482         cnt = min(cnt, (int)host->part_buf_count);
2483         if (cnt) {
2484                 memcpy(buf, (void *)&host->part_buf + host->part_buf_start,
2485                        cnt);
2486                 host->part_buf_count -= cnt;
2487                 host->part_buf_start += cnt;
2488         }
2489         return cnt;
2490 }
2491
2492 /* pull final bytes from the part_buf, assuming it's just been filled */
2493 static void dw_mci_pull_final_bytes(struct dw_mci *host, void *buf, int cnt)
2494 {
2495         memcpy(buf, &host->part_buf, cnt);
2496         host->part_buf_start = cnt;
2497         host->part_buf_count = (1 << host->data_shift) - cnt;
2498 }
2499
2500 static void dw_mci_push_data16(struct dw_mci *host, void *buf, int cnt)
2501 {
2502         struct mmc_data *data = host->data;
2503         int init_cnt = cnt;
2504
2505         /* try and push anything in the part_buf */
2506         if (unlikely(host->part_buf_count)) {
2507                 int len = dw_mci_push_part_bytes(host, buf, cnt);
2508                 buf += len;
2509                 cnt -= len;
2510                 if (host->part_buf_count == 2) {
2511                         mci_writew(host, DATA(host->data_offset),
2512                                         host->part_buf16);
2513                         host->part_buf_count = 0;
2514                 }
2515         }
2516 #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
2517         if (unlikely((unsigned long)buf & 0x1)) {
2518                 while (cnt >= 2) {
2519                         u16 aligned_buf[64];
2520                         int len = min(cnt & -2, (int)sizeof(aligned_buf));
2521                         int items = len >> 1;
2522                         int i;
2523                         /* memcpy from input buffer into aligned buffer */
2524                         memcpy(aligned_buf, buf, len);
2525                         buf += len;
2526                         cnt -= len;
2527                         /* push data from aligned buffer into fifo */
2528                         for (i = 0; i < items; ++i)
2529                                 mci_writew(host, DATA(host->data_offset),
2530                                                 aligned_buf[i]);
2531                 }
2532         } else
2533 #endif
2534         {
2535                 u16 *pdata = buf;
2536                 for (; cnt >= 2; cnt -= 2)
2537                         mci_writew(host, DATA(host->data_offset), *pdata++);
2538                 buf = pdata;
2539         }
2540         /* put anything remaining in the part_buf */
2541         if (cnt) {
2542                 dw_mci_set_part_bytes(host, buf, cnt);
2543                  /* Push data if we have reached the expected data length */
2544                 if ((data->bytes_xfered + init_cnt) ==
2545                     (data->blksz * data->blocks))
2546                         mci_writew(host, DATA(host->data_offset),
2547                                    host->part_buf16);
2548         }
2549 }
2550
2551 static void dw_mci_pull_data16(struct dw_mci *host, void *buf, int cnt)
2552 {
2553 #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
2554         if (unlikely((unsigned long)buf & 0x1)) {
2555                 while (cnt >= 2) {
2556                         /* pull data from fifo into aligned buffer */
2557                         u16 aligned_buf[64];
2558                         int len = min(cnt & -2, (int)sizeof(aligned_buf));
2559                         int items = len >> 1;
2560                         int i;
2561                         for (i = 0; i < items; ++i)
2562                                 aligned_buf[i] = mci_readw(host,
2563                                                 DATA(host->data_offset));
2564                         /* memcpy from aligned buffer into output buffer */
2565                         memcpy(buf, aligned_buf, len);
2566                         buf += len;
2567                         cnt -= len;
2568                 }
2569         } else
2570 #endif
2571         {
2572                 u16 *pdata = buf;
2573                 for (; cnt >= 2; cnt -= 2)
2574                         *pdata++ = mci_readw(host, DATA(host->data_offset));
2575                 buf = pdata;
2576         }
2577         if (cnt) {
2578                 host->part_buf16 = mci_readw(host, DATA(host->data_offset));
2579                 dw_mci_pull_final_bytes(host, buf, cnt);
2580         }
2581 }
2582
2583 static void dw_mci_push_data32(struct dw_mci *host, void *buf, int cnt)
2584 {
2585         struct mmc_data *data = host->data;
2586         int init_cnt = cnt;
2587
2588         /* try and push anything in the part_buf */
2589         if (unlikely(host->part_buf_count)) {
2590                 int len = dw_mci_push_part_bytes(host, buf, cnt);
2591                 buf += len;
2592                 cnt -= len;
2593                 if (host->part_buf_count == 4) {
2594                         mci_writel(host, DATA(host->data_offset),
2595                                         host->part_buf32);
2596                         host->part_buf_count = 0;
2597                 }
2598         }
2599 #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
2600         if (unlikely((unsigned long)buf & 0x3)) {
2601                 while (cnt >= 4) {
2602                         u32 aligned_buf[32];
2603                         int len = min(cnt & -4, (int)sizeof(aligned_buf));
2604                         int items = len >> 2;
2605                         int i;
2606                         /* memcpy from input buffer into aligned buffer */
2607                         memcpy(aligned_buf, buf, len);
2608                         buf += len;
2609                         cnt -= len;
2610                         /* push data from aligned buffer into fifo */
2611                         for (i = 0; i < items; ++i)
2612                                 mci_writel(host, DATA(host->data_offset),
2613                                                 aligned_buf[i]);
2614                 }
2615         } else
2616 #endif
2617         {
2618                 u32 *pdata = buf;
2619                 for (; cnt >= 4; cnt -= 4)
2620                         mci_writel(host, DATA(host->data_offset), *pdata++);
2621                 buf = pdata;
2622         }
2623         /* put anything remaining in the part_buf */
2624         if (cnt) {
2625                 dw_mci_set_part_bytes(host, buf, cnt);
2626                  /* Push data if we have reached the expected data length */
2627                 if ((data->bytes_xfered + init_cnt) ==
2628                     (data->blksz * data->blocks))
2629                         mci_writel(host, DATA(host->data_offset),
2630                                    host->part_buf32);
2631         }
2632 }
2633
2634 static void dw_mci_pull_data32(struct dw_mci *host, void *buf, int cnt)
2635 {
2636 #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
2637         if (unlikely((unsigned long)buf & 0x3)) {
2638                 while (cnt >= 4) {
2639                         /* pull data from fifo into aligned buffer */
2640                         u32 aligned_buf[32];
2641                         int len = min(cnt & -4, (int)sizeof(aligned_buf));
2642                         int items = len >> 2;
2643                         int i;
2644                         for (i = 0; i < items; ++i)
2645                                 aligned_buf[i] = mci_readl(host,
2646                                                 DATA(host->data_offset));
2647                         /* memcpy from aligned buffer into output buffer */
2648                         memcpy(buf, aligned_buf, len);
2649                         buf += len;
2650                         cnt -= len;
2651                 }
2652         } else
2653 #endif
2654         {
2655                 u32 *pdata = buf;
2656                 for (; cnt >= 4; cnt -= 4)
2657                         *pdata++ = mci_readl(host, DATA(host->data_offset));
2658                 buf = pdata;
2659         }
2660         if (cnt) {
2661                 host->part_buf32 = mci_readl(host, DATA(host->data_offset));
2662                 dw_mci_pull_final_bytes(host, buf, cnt);
2663         }
2664 }
2665
2666 static void dw_mci_push_data64(struct dw_mci *host, void *buf, int cnt)
2667 {
2668         struct mmc_data *data = host->data;
2669         int init_cnt = cnt;
2670
2671         /* try and push anything in the part_buf */
2672         if (unlikely(host->part_buf_count)) {
2673                 int len = dw_mci_push_part_bytes(host, buf, cnt);
2674                 buf += len;
2675                 cnt -= len;
2676
2677                 if (host->part_buf_count == 8) {
2678                         mci_writeq(host, DATA(host->data_offset),
2679                                         host->part_buf);
2680                         host->part_buf_count = 0;
2681                 }
2682         }
2683 #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
2684         if (unlikely((unsigned long)buf & 0x7)) {
2685                 while (cnt >= 8) {
2686                         u64 aligned_buf[16];
2687                         int len = min(cnt & -8, (int)sizeof(aligned_buf));
2688                         int items = len >> 3;
2689                         int i;
2690                         /* memcpy from input buffer into aligned buffer */
2691                         memcpy(aligned_buf, buf, len);
2692                         buf += len;
2693                         cnt -= len;
2694                         /* push data from aligned buffer into fifo */
2695                         for (i = 0; i < items; ++i)
2696                                 mci_writeq(host, DATA(host->data_offset),
2697                                                 aligned_buf[i]);
2698                 }
2699         } else
2700 #endif
2701         {
2702                 u64 *pdata = buf;
2703                 for (; cnt >= 8; cnt -= 8)
2704                         mci_writeq(host, DATA(host->data_offset), *pdata++);
2705                 buf = pdata;
2706         }
2707         /* put anything remaining in the part_buf */
2708         if (cnt) {
2709                 dw_mci_set_part_bytes(host, buf, cnt);
2710                 /* Push data if we have reached the expected data length */
2711                 if ((data->bytes_xfered + init_cnt) ==
2712                     (data->blksz * data->blocks))
2713                         mci_writeq(host, DATA(host->data_offset),
2714                                    host->part_buf);
2715         }
2716 }
2717
2718 static void dw_mci_pull_data64(struct dw_mci *host, void *buf, int cnt)
2719 {
2720 #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
2721         if (unlikely((unsigned long)buf & 0x7)) {
2722                 while (cnt >= 8) {
2723                         /* pull data from fifo into aligned buffer */
2724                         u64 aligned_buf[16];
2725                         int len = min(cnt & -8, (int)sizeof(aligned_buf));
2726                         int items = len >> 3;
2727                         int i;
2728                         for (i = 0; i < items; ++i)
2729                                 aligned_buf[i] = mci_readq(host,
2730                                                 DATA(host->data_offset));
2731                         /* memcpy from aligned buffer into output buffer */
2732                         memcpy(buf, aligned_buf, len);
2733                         buf += len;
2734                         cnt -= len;
2735                 }
2736         } else
2737 #endif
2738         {
2739                 u64 *pdata = buf;
2740                 for (; cnt >= 8; cnt -= 8)
2741                         *pdata++ = mci_readq(host, DATA(host->data_offset));
2742                 buf = pdata;
2743         }
2744         if (cnt) {
2745                 host->part_buf = mci_readq(host, DATA(host->data_offset));
2746                 dw_mci_pull_final_bytes(host, buf, cnt);
2747         }
2748 }
2749
2750 static void dw_mci_pull_data(struct dw_mci *host, void *buf, int cnt)
2751 {
2752         int len;
2753
2754         /* get remaining partial bytes */
2755         len = dw_mci_pull_part_bytes(host, buf, cnt);
2756         if (unlikely(len == cnt))
2757                 return;
2758         buf += len;
2759         cnt -= len;
2760
2761         /* get the rest of the data */
2762         host->pull_data(host, buf, cnt);
2763 }
2764
2765 static void dw_mci_read_data_pio(struct dw_mci *host, bool dto)
2766 {
2767         struct sg_mapping_iter *sg_miter = &host->sg_miter;
2768         void *buf;
2769         unsigned int offset;
2770         struct mmc_data *data = host->data;
2771         int shift = host->data_shift;
2772         u32 status;
2773         unsigned int len;
2774         unsigned int remain, fcnt;
2775
2776         if(!host->mmc->bus_refs){
2777                 printk("Note: %s host->mmc->bus_refs is 0!!!\n", __func__);
2778                 goto host_put;
2779         }
2780         do {
2781                 if (!sg_miter_next(sg_miter))
2782                         goto done;
2783
2784                 host->sg = sg_miter->piter.sg;
2785                 buf = sg_miter->addr;
2786                 remain = sg_miter->length;
2787                 offset = 0;
2788
2789                 do {
2790                         fcnt = (SDMMC_GET_FCNT(mci_readl(host, STATUS))
2791                                         << shift) + host->part_buf_count;
2792                         len = min(remain, fcnt);
2793                         if (!len)
2794                                 break;
2795                         dw_mci_pull_data(host, (void *)(buf + offset), len);
2796                         data->bytes_xfered += len;
2797                         offset += len;
2798                         remain -= len;
2799                 } while (remain);
2800
2801                 sg_miter->consumed = offset;
2802                 status = mci_readl(host, MINTSTS);
2803                 mci_writel(host, RINTSTS, SDMMC_INT_RXDR);
2804         /* if the RXDR is ready read again */
2805         } while ((status & SDMMC_INT_RXDR) ||
2806                  (dto && SDMMC_GET_FCNT(mci_readl(host, STATUS))));
2807
2808         if (!remain) {
2809                 if (!sg_miter_next(sg_miter))
2810                         goto done;
2811                 sg_miter->consumed = 0;
2812         }
2813         sg_miter_stop(sg_miter);
2814         return;
2815
2816 done:
2817         sg_miter_stop(sg_miter);
2818 host_put:       
2819         host->sg = NULL;
2820         smp_wmb();
2821         set_bit(EVENT_XFER_COMPLETE, &host->pending_events);
2822 }
2823
2824 static void dw_mci_write_data_pio(struct dw_mci *host)
2825 {
2826         struct sg_mapping_iter *sg_miter = &host->sg_miter;
2827         void *buf;
2828         unsigned int offset;
2829         struct mmc_data *data = host->data;
2830         int shift = host->data_shift;
2831         u32 status;
2832         unsigned int len;
2833         unsigned int fifo_depth = host->fifo_depth;
2834         unsigned int remain, fcnt;
2835         
2836         if (!host->mmc->bus_refs){
2837                 printk("Note: %s host->mmc->bus_refs is 0!!!\n", __func__);
2838                 goto host_put;
2839         }
2840
2841         do {
2842                 if (!sg_miter_next(sg_miter))
2843                         goto done;
2844
2845                 host->sg = sg_miter->piter.sg;
2846                 buf = sg_miter->addr;
2847                 remain = sg_miter->length;
2848                 offset = 0;
2849
2850                 do {
2851                         fcnt = ((fifo_depth -
2852                                  SDMMC_GET_FCNT(mci_readl(host, STATUS)))
2853                                         << shift) - host->part_buf_count;
2854                         len = min(remain, fcnt);
2855                         if (!len)
2856                                 break;
2857                         host->push_data(host, (void *)(buf + offset), len);
2858                         data->bytes_xfered += len;
2859                         offset += len;
2860                         remain -= len;
2861                 } while (remain);
2862
2863                 sg_miter->consumed = offset;
2864                 status = mci_readl(host, MINTSTS);
2865                 mci_writel(host, RINTSTS, SDMMC_INT_TXDR);
2866         } while (status & SDMMC_INT_TXDR); /* if TXDR write again */
2867
2868         if (!remain) {
2869                 if (!sg_miter_next(sg_miter))
2870                         goto done;
2871                 sg_miter->consumed = 0;
2872         }
2873         sg_miter_stop(sg_miter);
2874         return;
2875
2876 done:
2877         sg_miter_stop(sg_miter);
2878 host_put:       
2879         host->sg = NULL;
2880         smp_wmb();
2881         set_bit(EVENT_XFER_COMPLETE, &host->pending_events);
2882 }
2883
2884 static void dw_mci_cmd_interrupt(struct dw_mci *host, u32 status)
2885 {
2886         if (!host->cmd_status)
2887             host->cmd_status = status;
2888             
2889         if (!host->cmd)
2890                 goto cmd_exit;
2891
2892 cmd_exit:
2893         smp_wmb();
2894         set_bit(EVENT_CMD_COMPLETE, &host->pending_events);
2895         tasklet_schedule(&host->tasklet);
2896 }
2897
2898 static irqreturn_t dw_mci_interrupt(int irq, void *dev_id)
2899 {
2900         struct dw_mci *host = dev_id;
2901         u32 pending, sdio_int;
2902         int i;
2903
2904         pending = mci_readl(host, MINTSTS); /* read-only mask reg */
2905
2906         /*
2907         * DTO fix - version 2.10a and below, and only if internal DMA
2908         * is configured.
2909         */
2910         if (host->quirks & DW_MCI_QUIRK_IDMAC_DTO) {
2911                         if (!pending &&
2912                             ((mci_readl(host, STATUS) >> 17) & 0x1fff))
2913                                 pending |= SDMMC_INT_DATA_OVER;
2914         }
2915
2916         if (pending) {
2917                 if (pending & DW_MCI_CMD_ERROR_FLAGS) {
2918                         mci_writel(host, RINTSTS, DW_MCI_CMD_ERROR_FLAGS);
2919                         host->cmd_status = pending;
2920                         smp_wmb();
2921                         MMC_DBG_INFO_FUNC(host->mmc,
2922                                 "%s cmd_status INT=0x%x,[%s]",
2923                                 __FUNCTION__, host->cmd_status,
2924                                 mmc_hostname(host->mmc));
2925             
2926                         set_bit(EVENT_CMD_COMPLETE, &host->pending_events);
2927                 }
2928
2929                 if (pending & DW_MCI_DATA_ERROR_FLAGS) {
2930                         /* if there is an error report DATA_ERROR */
2931                         mci_writel(host, RINTSTS, DW_MCI_DATA_ERROR_FLAGS);
2932                         host->data_status = pending;
2933                         smp_wmb();
2934                         set_bit(EVENT_DATA_ERROR, &host->pending_events);
2935
2936                         MMC_DBG_INFO_FUNC(host->mmc,
2937                                 "%s data_status INT=0x%x,[%s]",
2938                                  __FUNCTION__, host->data_status,
2939                                  mmc_hostname(host->mmc));
2940                         tasklet_schedule(&host->tasklet);
2941                 }
2942
2943                 if (pending & SDMMC_INT_DATA_OVER) {
2944                         mci_writel(host, RINTSTS, SDMMC_INT_DATA_OVER);
2945                         MMC_DBG_CMD_FUNC(host->mmc,
2946                                 "SDMMC_INT_DATA_OVER, INT-pending=0x%x [%s]",
2947                                 pending, mmc_hostname(host->mmc));
2948                         if (!host->data_status)
2949                                 host->data_status = pending;
2950                         smp_wmb();
2951                         if (host->dir_status == DW_MCI_RECV_STATUS) {
2952                                 if (host->sg != NULL)
2953                                         dw_mci_read_data_pio(host, true);
2954                         }
2955                         set_bit(EVENT_DATA_COMPLETE, &host->pending_events);
2956                         tasklet_schedule(&host->tasklet);
2957                 }
2958
2959                 if (pending & SDMMC_INT_RXDR) {
2960                         mci_writel(host, RINTSTS, SDMMC_INT_RXDR);
2961                         if (host->dir_status == DW_MCI_RECV_STATUS && host->sg)
2962                                 dw_mci_read_data_pio(host, false);
2963                 }
2964
2965                 if (pending & SDMMC_INT_TXDR) {
2966                         mci_writel(host, RINTSTS, SDMMC_INT_TXDR);
2967                         if (host->dir_status == DW_MCI_SEND_STATUS && host->sg)
2968                                 dw_mci_write_data_pio(host);
2969                 }
2970
2971                 if (pending & SDMMC_INT_VSI) {
2972                         MMC_DBG_SW_VOL_FUNC(host->mmc,
2973                                 "SDMMC_INT_VSI, INT-pending=0x%x. [%s]",
2974                                 pending, mmc_hostname(host->mmc));
2975                         mci_writel(host, RINTSTS, SDMMC_INT_VSI);
2976                         dw_mci_cmd_interrupt(host, pending);
2977                 }
2978
2979                 if (pending & SDMMC_INT_CMD_DONE) {
2980                         MMC_DBG_CMD_FUNC(host->mmc,
2981                                 "SDMMC_INT_CMD_DONE, CMD = 0x%x, INT-pending=0x%x. [%s]",
2982                                 mci_readl(host, CMD), pending, mmc_hostname(host->mmc));
2983                         mci_writel(host, RINTSTS, SDMMC_INT_CMD_DONE);
2984                         dw_mci_cmd_interrupt(host, pending);
2985                 }
2986
2987                 if (pending & SDMMC_INT_CD) {
2988                         mci_writel(host, RINTSTS, SDMMC_INT_CD);
2989                         MMC_DBG_INFO_FUNC(host->mmc,
2990                                 "SDMMC_INT_CD, INT-pending=0x%x. [%s]",
2991                                 pending, mmc_hostname(host->mmc));
2992                         wake_lock_timeout(&host->mmc->detect_wake_lock, 5 * HZ);
2993                         queue_work(host->card_workqueue, &host->card_work);
2994                 }
2995                 
2996                 if (pending & SDMMC_INT_HLE) {
2997                         mci_writel(host, RINTSTS, SDMMC_INT_HLE);
2998                         MMC_DBG_CMD_FUNC(host->mmc,
2999                                 "SDMMC_INT_HLE INT-pending=0x%x. [%s]\n",
3000                                 pending, mmc_hostname(host->mmc));
3001                         
3002                 }
3003
3004                 /* Handle SDIO Interrupts */
3005                 for (i = 0; i < host->num_slots; i++) {
3006                         struct dw_mci_slot *slot = host->slot[i];
3007
3008                         if (host->verid < DW_MMC_240A)
3009                                 sdio_int = SDMMC_INT_SDIO(i);
3010                         else
3011                                 sdio_int = SDMMC_INT_SDIO(i + 8);
3012                         
3013                         if (pending & sdio_int) {
3014                                 mci_writel(host, RINTSTS, sdio_int);
3015                                 mmc_signal_sdio_irq(slot->mmc);
3016                         }
3017                 }
3018
3019         }
3020
3021 #ifdef CONFIG_MMC_DW_IDMAC
3022         /* External DMA Soc platform NOT need to ack interrupt IDSTS */
3023         if (!(cpu_is_rk3036() || cpu_is_rk312x())){
3024                 /* Handle DMA interrupts */
3025                 pending = mci_readl(host, IDSTS);
3026                 if (pending & (SDMMC_IDMAC_INT_TI | SDMMC_IDMAC_INT_RI)) {
3027                         mci_writel(host, IDSTS, SDMMC_IDMAC_INT_TI | SDMMC_IDMAC_INT_RI);
3028                         mci_writel(host, IDSTS, SDMMC_IDMAC_INT_NI);
3029                         host->dma_ops->complete((void *)host);
3030                 }
3031         }
3032 #endif
3033
3034         return IRQ_HANDLED;
3035 }
3036
3037 static void dw_mci_work_routine_card(struct work_struct *work)
3038 {
3039         struct dw_mci *host = container_of(work, struct dw_mci, card_work);
3040         int i;
3041         
3042         for (i = 0; i < host->num_slots; i++) {
3043                 struct dw_mci_slot *slot = host->slot[i];
3044                 struct mmc_host *mmc = slot->mmc;
3045                 struct mmc_request *mrq;
3046                 int present;
3047
3048                 present = dw_mci_get_cd(mmc);
3049
3050                 /* Card insert, switch data line to uart function, and vice verse.
3051                 ONLY audi chip need switched by software, using udbg tag in dts!
3052                 */
3053                 if (!(IS_ERR(host->pins_udbg)) &&
3054                         !(IS_ERR(host->pins_default))) {
3055                         if (present) {
3056                                 if (pinctrl_select_state(host->pinctrl,
3057                                         host->pins_default) < 0)
3058                                         dev_err(host->dev,
3059                                                 "%s: Default pinctrl setting failed!\n",
3060                                                 mmc_hostname(host->mmc));
3061                         } else {
3062                                 if (pinctrl_select_state(host->pinctrl,
3063                                         host->pins_udbg) < 0)
3064                                         dev_err(host->dev,
3065                                                 "%s: Udbg pinctrl setting failed!\n",
3066                                                 mmc_hostname(host->mmc));
3067                         }
3068                 }
3069
3070                 while (present != slot->last_detect_state) {
3071                         dev_dbg(&slot->mmc->class_dev, "card %s\n",
3072                                 present ? "inserted" : "removed");
3073                         MMC_DBG_BOOT_FUNC(mmc, "  The card is %s.  ===!!!!!!==[%s]\n",
3074                                 present ? "inserted" : "removed.", mmc_hostname(mmc));
3075         
3076                         dw_mci_ctrl_all_reset(host);
3077                         /* Stop edma when rountine card triggered */
3078                         if(cpu_is_rk3036() || cpu_is_rk312x())
3079                                 if(host->dma_ops && host->dma_ops->stop)
3080                                         host->dma_ops->stop(host);
3081                         rk_send_wakeup_key();//wake up system
3082                         spin_lock_bh(&host->lock);
3083
3084                         /* Card change detected */
3085                         slot->last_detect_state = present;
3086
3087                         /* Clean up queue if present */
3088                         mrq = slot->mrq;
3089                         if (mrq) {
3090                                 if (mrq == host->mrq) {
3091                                         host->data = NULL;
3092                                         host->cmd = NULL;
3093
3094                                         switch (host->state) {
3095                                         case STATE_IDLE:
3096                                                 break;
3097                                         case STATE_SENDING_CMD:
3098                                                 mrq->cmd->error = -ENOMEDIUM;
3099                                                 if (!mrq->data)
3100                                                         break;
3101                                                 /* fall through */
3102                                         case STATE_SENDING_DATA:
3103                                                 mrq->data->error = -ENOMEDIUM;
3104                                                 dw_mci_stop_dma(host);
3105                                                 break;
3106                                         case STATE_DATA_BUSY:
3107                                         case STATE_DATA_ERROR:
3108                                                 if (mrq->data->error == -EINPROGRESS)
3109                                                         mrq->data->error = -ENOMEDIUM;
3110                                                 if (!mrq->stop)
3111                                                         break;
3112                                                 /* fall through */
3113                                         case STATE_SENDING_STOP:
3114                                                 mrq->stop->error = -ENOMEDIUM;
3115                                                 break;
3116                                         }
3117
3118                                         dw_mci_request_end(host, mrq);
3119                                 } else {
3120                                         list_del(&slot->queue_node);
3121                                         mrq->cmd->error = -ENOMEDIUM;
3122                                         if (mrq->data)
3123                                                 mrq->data->error = -ENOMEDIUM;
3124                                         if (mrq->stop)
3125                                                 mrq->stop->error = -ENOMEDIUM;
3126
3127                                         MMC_DBG_CMD_FUNC(host->mmc,
3128                                                 "dw_mci_work--reqeuest done, cmd=%d [%s]",
3129                                                 mrq->cmd->opcode, mmc_hostname(mmc));
3130
3131                                         spin_unlock(&host->lock);
3132                                         mmc_request_done(slot->mmc, mrq);
3133                                         spin_lock(&host->lock);
3134                                 }
3135                         }
3136
3137                         /* Power down slot */
3138                         if (present == 0) {
3139                                 /* Clear down the FIFO */
3140                                 dw_mci_fifo_reset(host);
3141 #ifdef CONFIG_MMC_DW_IDMAC
3142                                 if (!(cpu_is_rk3036() || cpu_is_rk312x()))
3143                                         dw_mci_idmac_reset(host);
3144 #endif
3145                         }
3146
3147                         spin_unlock_bh(&host->lock);
3148
3149                         present = dw_mci_get_cd(mmc);
3150                 }
3151
3152                 mmc_detect_change(slot->mmc,
3153                         msecs_to_jiffies(host->pdata->detect_delay_ms));
3154         }
3155 }
3156
3157 #ifdef CONFIG_OF
3158 /* given a slot id, find out the device node representing that slot */
3159 static struct device_node *dw_mci_of_find_slot_node(struct device *dev, u8 slot)
3160 {
3161         struct device_node *np;
3162         const __be32 *addr;
3163         int len;
3164
3165         if (!dev || !dev->of_node)
3166                 return NULL;
3167
3168         for_each_child_of_node(dev->of_node, np) {
3169                 addr = of_get_property(np, "reg", &len);
3170                 if (!addr || (len < sizeof(int)))
3171                         continue;
3172                 if (be32_to_cpup(addr) == slot)
3173                         return np;
3174         }
3175         return NULL;
3176 }
3177
3178 static struct dw_mci_of_slot_quirks {
3179         char *quirk;
3180         int id;
3181 } of_slot_quirks[] = {
3182         {
3183                 .quirk  = "disable-wp",
3184                 .id     = DW_MCI_SLOT_QUIRK_NO_WRITE_PROTECT,
3185         },
3186 };
3187
3188 static int dw_mci_of_get_slot_quirks(struct device *dev, u8 slot)
3189 {
3190         struct device_node *np = dw_mci_of_find_slot_node(dev, slot);
3191         int quirks = 0;
3192         int idx;
3193
3194         /* get quirks */
3195         for (idx = 0; idx < ARRAY_SIZE(of_slot_quirks); idx++)
3196                 if (of_get_property(np, of_slot_quirks[idx].quirk, NULL))
3197                         quirks |= of_slot_quirks[idx].id;
3198
3199         return quirks;
3200 }
3201
3202 /* find out bus-width for a given slot */
3203 static u32 dw_mci_of_get_bus_wd(struct device *dev, u8 slot)
3204 {
3205         struct device_node *np = dev->of_node;//dw_mci_of_find_slot_node(dev, slot);
3206         u32 bus_wd = 1;
3207
3208         if (!np)
3209                 return 1;
3210
3211         if (of_property_read_u32(np, "bus-width", &bus_wd))
3212                 dev_err(dev, "bus-width property not found, assuming width"
3213                                " as 1\n");
3214         return bus_wd;
3215 }
3216
3217
3218 /* find the pwr-en gpio for a given slot; or -1 if none specified */
3219 static int dw_mci_of_get_pwr_en_gpio(struct device *dev, u8 slot)
3220 {
3221         struct device_node *np = dev->of_node;//dw_mci_of_find_slot_node(dev, slot);
3222         int gpio;
3223
3224         if (!np)
3225                 return -EINVAL;
3226
3227         gpio = of_get_named_gpio(np, "pwr-gpios", 0);
3228
3229         /* Having a missing entry is valid; return silently */
3230         if (!gpio_is_valid(gpio))
3231                 return -EINVAL;
3232
3233         if (devm_gpio_request(dev, gpio, "dw-mci-pwr_en")) {
3234                 dev_warn(dev, "gpio [%d] request failed\n", gpio);
3235                 return -EINVAL;
3236         }
3237
3238     gpio_direction_output(gpio, 0);//set 0 to pwr-en
3239
3240         return gpio;
3241 }
3242
3243
3244 /* find the write protect gpio for a given slot; or -1 if none specified */
3245 static int dw_mci_of_get_wp_gpio(struct device *dev, u8 slot)
3246 {
3247         struct device_node *np = dw_mci_of_find_slot_node(dev, slot);
3248         int gpio;
3249
3250         if (!np)
3251                 return -EINVAL;
3252
3253         gpio = of_get_named_gpio(np, "wp-gpios", 0);
3254
3255         /* Having a missing entry is valid; return silently */
3256         if (!gpio_is_valid(gpio))
3257                 return -EINVAL;
3258
3259         if (devm_gpio_request(dev, gpio, "dw-mci-wp")) {
3260                 dev_warn(dev, "gpio [%d] request failed\n", gpio);
3261                 return -EINVAL;
3262         }
3263
3264         return gpio;
3265 }
3266
3267 /* find the cd gpio for a given slot */
3268 static void dw_mci_of_get_cd_gpio(struct device *dev, u8 slot,
3269                                         struct mmc_host *mmc)
3270 {
3271         struct device_node *np = dev->of_node;//dw_mci_of_find_slot_node(dev, slot);
3272         int gpio;
3273
3274         if (!np)
3275                 return;
3276
3277         gpio = of_get_named_gpio(np, "cd-gpios", 0);
3278
3279         /* Having a missing entry is valid; return silently */
3280         if (!gpio_is_valid(gpio))
3281                 return;
3282
3283         if (mmc_gpio_request_cd(mmc, gpio, 0))
3284                 dev_warn(dev, "gpio [%d] request failed\n", gpio);
3285 }
3286
3287 static irqreturn_t dw_mci_gpio_cd_irqt(int irq, void *dev_id)
3288 {
3289         struct mmc_host *mmc = dev_id;
3290         struct dw_mci_slot *slot = mmc_priv(mmc);
3291         struct dw_mci *host = slot->host;
3292         int gpio_cd = slot->cd_gpio;
3293
3294         (gpio_get_value(gpio_cd)  == 0) ? 
3295                 irq_set_irq_type(irq, IRQF_TRIGGER_HIGH  | IRQF_ONESHOT) : 
3296                 irq_set_irq_type(irq, IRQF_TRIGGER_LOW | IRQF_ONESHOT);
3297
3298         /* wakeup system whether gpio debounce or not */
3299         rk_send_wakeup_key();
3300
3301         /* no need to trigger detect flow when rescan is disabled.
3302            This case happended in dpm, that we just wakeup system and
3303            let suspend_post notify callback handle it.
3304          */
3305         if(mmc->rescan_disable == 0)
3306                 queue_work(host->card_workqueue, &host->card_work);
3307         else
3308                 printk("%s: rescan been disabled!\n", __FUNCTION__);
3309
3310         return IRQ_HANDLED;
3311 }
3312
3313 static void dw_mci_of_set_cd_gpio_irq(struct device *dev, u32 gpio,
3314                                         struct mmc_host *mmc)
3315 {
3316         struct dw_mci_slot *slot = mmc_priv(mmc);
3317         struct dw_mci *host = slot->host;
3318         int irq;
3319         int ret;
3320
3321         /* Having a missing entry is valid; return silently */
3322         if (!gpio_is_valid(gpio))
3323                 return;
3324
3325         irq = gpio_to_irq(gpio);
3326         if (irq >= 0) {
3327                 ret = devm_request_threaded_irq(
3328                                         &mmc->class_dev, irq,
3329                                         NULL, dw_mci_gpio_cd_irqt,
3330                                         IRQF_TRIGGER_LOW | IRQF_ONESHOT,
3331                                         "dw_mci_cd", mmc);
3332                 if (ret < 0) {
3333                         irq = ret;
3334                         dev_err(host->dev,
3335                                 "Request cd-gpio %d interrupt error!\n", gpio);
3336                 } else{
3337                         /* enable wakeup event for gpio-cd in idle or deep suspend*/
3338                         enable_irq_wake(irq);
3339                 }
3340         } else {
3341                 dev_err(host->dev, "Cannot convert gpio %d to irq!\n", gpio);
3342         }
3343 }
3344
3345 static void dw_mci_of_free_cd_gpio_irq(struct device *dev, u32 gpio,
3346                                         struct mmc_host *mmc)
3347 {
3348         if (!gpio_is_valid(gpio))
3349                 return;
3350
3351         if (gpio_to_irq(gpio) >= 0) {
3352                 devm_free_irq(&mmc->class_dev, gpio_to_irq(gpio), mmc);
3353                 devm_gpio_free(&mmc->class_dev, gpio);
3354         }
3355 }
3356 #else /* CONFIG_OF */
3357 static int dw_mci_of_get_slot_quirks(struct device *dev, u8 slot)
3358 {
3359         return 0;
3360 }
3361 static u32 dw_mci_of_get_bus_wd(struct device *dev, u8 slot)
3362 {
3363         return 1;
3364 }
3365 static struct device_node *dw_mci_of_find_slot_node(struct device *dev, u8 slot)
3366 {
3367         return NULL;
3368 }
3369 static int dw_mci_of_get_wp_gpio(struct device *dev, u8 slot)
3370 {
3371         return -EINVAL;
3372 }
3373 static void dw_mci_of_get_cd_gpio(struct device *dev, u8 slot,
3374                                         struct mmc_host *mmc)
3375 {
3376         return;
3377 }
3378 #endif /* CONFIG_OF */
3379
3380 /* @host: dw_mci host prvdata
3381  * Init pinctrl for each platform. Usually we assign
3382  * "defalut" tag for functional usage, "idle" tag for gpio
3383  * state and "udbg" tag for uart_dbg if any.
3384  */
3385 static void dw_mci_init_pinctrl(struct dw_mci *host)
3386 {
3387         /* Fixme: DON'T TOUCH EMMC SETTING! */
3388         if (host->mmc->restrict_caps & RESTRICT_CARD_TYPE_EMMC)
3389                 return;
3390
3391         /* Get pinctrl for DTS */
3392         host->pinctrl = devm_pinctrl_get(host->dev);
3393         if (IS_ERR(host->pinctrl)) {
3394                 dev_err(host->dev, "%s: No pinctrl used!\n",
3395                                 mmc_hostname(host->mmc));
3396                 return;
3397         }
3398
3399         /* Lookup idle state */
3400         host->pins_idle =
3401                 pinctrl_lookup_state(host->pinctrl, PINCTRL_STATE_IDLE);
3402         if (IS_ERR(host->pins_idle)) {
3403                 dev_err(host->dev, "%s: No idle tag found!\n",
3404                                 mmc_hostname(host->mmc));
3405         } else {
3406                 if (pinctrl_select_state(host->pinctrl, host->pins_idle) < 0)
3407                         dev_err(host->dev,
3408                                 "%s: Idle pinctrl setting failed!\n",
3409                                 mmc_hostname(host->mmc));
3410         }
3411
3412         /* Lookup default state */
3413         host->pins_default =
3414                 pinctrl_lookup_state(host->pinctrl, PINCTRL_STATE_DEFAULT);
3415         if (IS_ERR(host->pins_default)) {
3416                 dev_err(host->dev, "%s: No default pinctrl found!\n",
3417                 mmc_hostname(host->mmc));
3418         } else {
3419                 if (pinctrl_select_state(host->pinctrl, host->pins_default) < 0)
3420                         dev_err(host->dev,
3421                                 "%s:  Default pinctrl setting failed!\n",
3422                                 mmc_hostname(host->mmc));
3423         }
3424
3425         /* Sd card data0/1 may be used for uart_dbg, so were data2/3 for Jtag */
3426         if ((host->mmc->restrict_caps & RESTRICT_CARD_TYPE_SD)) {
3427                 host->pins_udbg = pinctrl_lookup_state(host->pinctrl, "udbg");
3428                 if (IS_ERR(host->pins_udbg)) {
3429                         dev_warn(host->dev, "%s: No udbg pinctrl found!\n",
3430                                 mmc_hostname(host->mmc));
3431                 } else {
3432                         if (!dw_mci_get_cd(host->mmc))
3433                                 if (pinctrl_select_state(host->pinctrl, host->pins_udbg) < 0)
3434                                         dev_err(host->dev, "%s: Udbg pinctrl setting failed!\n",
3435                                                 mmc_hostname(host->mmc));
3436                 }
3437         }
3438 }
3439
3440 static int dw_mci_pm_notify(struct notifier_block *notify_block,
3441                                         unsigned long mode, void *unused)
3442 {
3443         struct mmc_host *host = container_of(
3444                 notify_block, struct mmc_host, pm_notify);
3445         unsigned long flags;
3446
3447         switch (mode) {
3448         case PM_HIBERNATION_PREPARE:
3449         case PM_SUSPEND_PREPARE:
3450                 dev_err(host->parent,
3451                         "dw_mci_pm_notify:  suspend prepare\n");
3452                 spin_lock_irqsave(&host->lock, flags);
3453                 host->rescan_disable = 1;
3454                 spin_unlock_irqrestore(&host->lock, flags);
3455                 if (cancel_delayed_work(&host->detect))
3456                         wake_unlock(&host->detect_wake_lock);
3457                 break;
3458
3459         case PM_POST_SUSPEND:
3460         case PM_POST_HIBERNATION:
3461         case PM_POST_RESTORE:
3462                 dev_err(host->parent,
3463                         "dw_mci_pm_notify:  post suspend\n");
3464                 spin_lock_irqsave(&host->lock, flags);
3465                 host->rescan_disable = 0;
3466                 spin_unlock_irqrestore(&host->lock, flags);
3467                 mmc_detect_change(host, 10);
3468         }
3469
3470         return 0;
3471 }
3472
3473 static int dw_mci_init_slot(struct dw_mci *host, unsigned int id)
3474 {
3475         struct mmc_host *mmc;
3476         struct dw_mci_slot *slot;
3477         const struct dw_mci_drv_data *drv_data = host->drv_data;
3478         int ctrl_id, ret;
3479         u32 freq[2];
3480         u8 bus_width;
3481
3482         mmc = mmc_alloc_host(sizeof(struct dw_mci_slot), host->dev);
3483         if (!mmc)
3484                 return -ENOMEM;
3485
3486         slot = mmc_priv(mmc);
3487         slot->id = id;
3488         slot->mmc = mmc;
3489         slot->host = host;
3490         host->slot[id] = slot;
3491         host->mmc = mmc;
3492
3493         slot->quirks = dw_mci_of_get_slot_quirks(host->dev, slot->id);
3494
3495         mmc->ops = &dw_mci_ops;
3496
3497         if (of_property_read_u32_array(host->dev->of_node,
3498                                        "clock-freq-min-max", freq, 2)) {
3499                 mmc->f_min = DW_MCI_FREQ_MIN;
3500                 mmc->f_max = DW_MCI_FREQ_MAX;
3501                 
3502                 printk("%s: fmin=%d, fmax=%d [%s]\n",
3503                          __FUNCTION__, mmc->f_min,
3504                          mmc->f_max, mmc_hostname(mmc));
3505         } else {
3506                 mmc->f_min = freq[0];
3507                 mmc->f_max = freq[1];
3508                 
3509                 printk("%s: fmin=%d, fmax=%d [%s]\n",
3510                          __FUNCTION__, mmc->f_min,
3511                          mmc->f_max, mmc_hostname(mmc));
3512         }
3513
3514         if (of_find_property(host->dev->of_node, "supports-sd", NULL))
3515                 mmc->restrict_caps |= RESTRICT_CARD_TYPE_SD;    
3516         if (of_find_property(host->dev->of_node, "supports-sdio", NULL))
3517                 mmc->restrict_caps |= RESTRICT_CARD_TYPE_SDIO;  
3518         if (of_find_property(host->dev->of_node, "supports-emmc", NULL))
3519                 mmc->restrict_caps |= RESTRICT_CARD_TYPE_EMMC;
3520
3521         if (mmc->restrict_caps & RESTRICT_CARD_TYPE_SD) {
3522                 mmc->pm_notify.notifier_call = dw_mci_pm_notify;
3523                 if (register_pm_notifier(&mmc->pm_notify)) {
3524                         pr_err("dw_mci: register_pm_notifier failed\n");
3525                         goto err_pm_notifier;
3526                 }
3527         }
3528
3529         if (host->cid == DW_MCI_TYPE_RK3368) {
3530                 if (IS_ERR(host->grf))
3531                         pr_err("rk_sdmmc: dts couldn't find grf regmap for 3368\n");
3532                 else
3533                         /* Disable force_jtag */
3534                         regmap_write(host->grf, 0x43c, (1<<13)<<16 | (0 << 13));
3535         } else if (cpu_is_rk3288()) {
3536                 grf_writel(((1 << 12) << 16) | (0 << 12), RK3288_GRF_SOC_CON0);
3537         }
3538
3539         /* We assume only low-level chip use gpio_cd */
3540         if ((soc_is_rk3126() || soc_is_rk3126b() || soc_is_rk3036()) &&
3541                 (host->mmc->restrict_caps & RESTRICT_CARD_TYPE_SD)) {
3542                 slot->cd_gpio = of_get_named_gpio(host->dev->of_node, "cd-gpios", 0);
3543                 if (gpio_is_valid(slot->cd_gpio)) {
3544                         /* Request gpio int for card detection */
3545                         dw_mci_of_set_cd_gpio_irq(host->dev, slot->cd_gpio,host->mmc);
3546                 } else {
3547                         slot->cd_gpio = -ENODEV;
3548                         dev_err(host->dev, "failed to get your cd-gpios!\n");
3549                 }
3550         }
3551
3552         if (host->pdata->get_ocr)
3553                 mmc->ocr_avail = host->pdata->get_ocr(id);
3554         else
3555         {
3556                 mmc->ocr_avail = MMC_VDD_27_28 | MMC_VDD_28_29 | MMC_VDD_29_30 |
3557                                 MMC_VDD_30_31 | MMC_VDD_31_32 | MMC_VDD_32_33|
3558                                 MMC_VDD_33_34 | MMC_VDD_34_35 | MMC_VDD_35_36 |
3559                                 MMC_VDD_26_27 | MMC_VDD_25_26 | MMC_VDD_24_25 |
3560                                 MMC_VDD_23_24 | MMC_VDD_22_23 | MMC_VDD_21_22 |
3561                                 MMC_VDD_20_21 | MMC_VDD_165_195;
3562         }
3563
3564         /*
3565          * Start with slot power disabled, it will be enabled when a card
3566          * is detected.
3567          */
3568         if (host->pdata->setpower)
3569                 host->pdata->setpower(id, 0);
3570
3571         if (host->pdata->caps)
3572                 mmc->caps = host->pdata->caps;
3573
3574         if (host->pdata->pm_caps)
3575                 mmc->pm_caps = host->pdata->pm_caps;
3576
3577         if (host->dev->of_node) {
3578                 ctrl_id = of_alias_get_id(host->dev->of_node, "mshc");
3579                 if (ctrl_id < 0)
3580                         ctrl_id = 0;
3581         } else {
3582                 ctrl_id = to_platform_device(host->dev)->id;
3583         }
3584         if (drv_data && drv_data->caps)
3585                 mmc->caps |= drv_data->caps[ctrl_id];
3586         if (drv_data && drv_data->hold_reg_flag)
3587                 mmc->hold_reg_flag |= drv_data->hold_reg_flag[ctrl_id];         
3588
3589         /* set the compatibility of driver. */
3590         mmc->caps |= MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 | MMC_CAP_UHS_SDR50 
3591                         | MMC_CAP_UHS_SDR104 | MMC_CAP_ERASE ;
3592
3593         if (host->pdata->caps2)
3594                 mmc->caps2 = host->pdata->caps2;
3595
3596         if (host->pdata->get_bus_wd)
3597                 bus_width = host->pdata->get_bus_wd(slot->id);
3598         else if (host->dev->of_node)
3599                 bus_width = dw_mci_of_get_bus_wd(host->dev, slot->id);
3600         else
3601                 bus_width = 1;
3602
3603         switch (bus_width) {
3604                 case 8:
3605                         mmc->caps |= MMC_CAP_8_BIT_DATA;
3606                 case 4:
3607                         mmc->caps |= MMC_CAP_4_BIT_DATA;
3608         }
3609         
3610         if (of_find_property(host->dev->of_node, "cap-power-off-card", NULL))
3611                 mmc->caps |= MMC_CAP_POWER_OFF_CARD;
3612         if (of_find_property(host->dev->of_node, "cap-sdio-irq", NULL))
3613                 mmc->caps |= MMC_CAP_SDIO_IRQ;
3614         if (of_find_property(host->dev->of_node, "poll-hw-reset", NULL))
3615                 mmc->caps |= MMC_CAP_HW_RESET;
3616         if (of_find_property(host->dev->of_node, "full-pwr-cycle", NULL))
3617                 mmc->caps2 |= MMC_CAP2_FULL_PWR_CYCLE;
3618         if (of_find_property(host->dev->of_node, "keep-power-in-suspend", NULL))
3619                 mmc->pm_caps |= MMC_PM_KEEP_POWER;
3620         if (of_find_property(host->dev->of_node, "ignore-pm-notify", NULL))
3621                 mmc->pm_caps |= MMC_PM_IGNORE_PM_NOTIFY;
3622         if (of_find_property(host->dev->of_node, "enable-sdio-wakeup", NULL))
3623                 mmc->pm_caps |= MMC_PM_WAKE_SDIO_IRQ;
3624
3625         /*Assign pm_caps pass to pm_flags*/
3626         mmc->pm_flags = mmc->pm_caps;
3627
3628         if (host->pdata->blk_settings) {
3629                 mmc->max_segs = host->pdata->blk_settings->max_segs;
3630                 mmc->max_blk_size = host->pdata->blk_settings->max_blk_size;
3631                 mmc->max_blk_count = host->pdata->blk_settings->max_blk_count;
3632                 mmc->max_req_size = host->pdata->blk_settings->max_req_size;
3633                 mmc->max_seg_size = host->pdata->blk_settings->max_seg_size;
3634         } else {
3635                 /* Useful defaults if platform data is unset. */
3636 #ifdef CONFIG_MMC_DW_IDMAC
3637                 mmc->max_segs = host->ring_size;
3638                 mmc->max_blk_size = 65536;
3639                 mmc->max_blk_count = host->ring_size;
3640                 mmc->max_seg_size = 0x1000;
3641                 mmc->max_req_size = mmc->max_seg_size * mmc->max_blk_count;
3642                 if(cpu_is_rk3036() || cpu_is_rk312x()){
3643                         /* fixup for external dmac setting */
3644                         mmc->max_segs = 64;
3645                         mmc->max_blk_size = 65536; /* BLKSIZ is 16 bits */
3646                         mmc->max_blk_count = 65535;
3647                         mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
3648                         mmc->max_seg_size = mmc->max_req_size; 
3649                 }
3650 #else
3651                 mmc->max_segs = 64;
3652                 mmc->max_blk_size = 65536; /* BLKSIZ is 16 bits */
3653                 mmc->max_blk_count = 512;
3654                 mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
3655                 mmc->max_seg_size = mmc->max_req_size;
3656 #endif /* CONFIG_MMC_DW_IDMAC */
3657                 
3658         }
3659         /* pwr_en */   
3660         slot->pwr_en_gpio = dw_mci_of_get_pwr_en_gpio(host->dev, slot->id);
3661
3662         if (!(mmc->restrict_caps & RESTRICT_CARD_TYPE_SD))
3663         {
3664                 host->vmmc = NULL;
3665         }else{
3666
3667                 if(mmc->restrict_caps & RESTRICT_CARD_TYPE_SD)
3668                         host->vmmc = devm_regulator_get(mmc_dev(mmc), "vmmc");
3669                 else
3670                         host->vmmc = NULL;
3671          
3672                 if (IS_ERR(host->vmmc)) {
3673                         pr_info("%s: no vmmc regulator found\n", mmc_hostname(mmc));
3674                         host->vmmc = NULL;
3675                 }else{
3676                         ret = regulator_enable(host->vmmc);
3677                         if (ret) {
3678                                 dev_err(host->dev,
3679                                         "failed to enable regulator: %d\n", ret);
3680                                 host->vmmc = NULL;
3681                                 goto err_setup_bus;
3682                         }
3683                 }
3684         }
3685     
3686         slot->wp_gpio = dw_mci_of_get_wp_gpio(host->dev, slot->id);
3687         
3688         if (mmc->restrict_caps & RESTRICT_CARD_TYPE_SDIO)
3689                 clear_bit(DW_MMC_CARD_PRESENT, &slot->flags);
3690
3691         dw_mci_init_pinctrl(host);
3692         ret = mmc_add_host(mmc);
3693         if (ret)
3694                 goto err_setup_bus;
3695
3696 #if defined(CONFIG_DEBUG_FS)
3697         dw_mci_init_debugfs(slot);
3698 #endif
3699
3700         /* Card initially undetected */
3701         slot->last_detect_state = 1;
3702
3703         return 0;
3704 err_pm_notifier:
3705         unregister_pm_notifier(&mmc->pm_notify);
3706
3707 err_setup_bus:
3708         if (gpio_is_valid(slot->cd_gpio))
3709                 dw_mci_of_free_cd_gpio_irq(host->dev, slot->cd_gpio,host->mmc);
3710         mmc_free_host(mmc);
3711         return -EINVAL;
3712 }
3713
3714 static void dw_mci_cleanup_slot(struct dw_mci_slot *slot, unsigned int id)
3715 {
3716         /* Shutdown detect IRQ */
3717         if (slot->host->pdata->exit)
3718                 slot->host->pdata->exit(id);
3719
3720         /* Debugfs stuff is cleaned up by mmc core */
3721         mmc_remove_host(slot->mmc);
3722         slot->host->slot[id] = NULL;
3723         mmc_free_host(slot->mmc);
3724 }
3725
3726 static void dw_mci_init_dma(struct dw_mci *host)
3727 {
3728         /* Alloc memory for sg translation */
3729         host->sg_cpu = dmam_alloc_coherent(host->dev, PAGE_SIZE,
3730                                           &host->sg_dma, GFP_KERNEL);
3731         if (!host->sg_cpu) {
3732                 dev_err(host->dev, "%s: could not alloc DMA memory\n",
3733                         __func__);
3734                 goto no_dma;
3735         }
3736
3737         /* Determine which DMA interface to use */
3738 #if defined(CONFIG_MMC_DW_IDMAC)
3739         if(cpu_is_rk3036() || cpu_is_rk312x()){
3740                 host->dma_ops = &dw_mci_edmac_ops;
3741                 dev_info(host->dev, "Using external DMA controller.\n");
3742         }else{
3743                 host->dma_ops = &dw_mci_idmac_ops;
3744                 dev_info(host->dev, "Using internal DMA controller.\n");
3745         }
3746 #endif
3747
3748         if (!host->dma_ops)
3749                 goto no_dma;
3750
3751         if (host->dma_ops->init && host->dma_ops->start &&
3752             host->dma_ops->stop && host->dma_ops->cleanup) {
3753                 if (host->dma_ops->init(host)) {
3754                         dev_err(host->dev, "%s: Unable to initialize "
3755                                 "DMA Controller.\n", __func__);
3756                         goto no_dma;
3757                 }
3758         } else {
3759                 dev_err(host->dev, "DMA initialization not found.\n");
3760                 goto no_dma;
3761         }
3762
3763         host->use_dma = 1;
3764         return;
3765
3766 no_dma:
3767         dev_info(host->dev, "Using PIO mode.\n");
3768         host->use_dma = 0;
3769         return;
3770 }
3771
3772 static bool dw_mci_ctrl_reset(struct dw_mci *host, u32 reset)
3773 {
3774         unsigned long timeout = jiffies + msecs_to_jiffies(500);
3775         u32 ctrl;
3776
3777         ctrl = mci_readl(host, CTRL);
3778         ctrl |= reset;
3779         mci_writel(host, CTRL, ctrl);
3780
3781         /* wait till resets clear */
3782         do {
3783                 ctrl = mci_readl(host, CTRL);
3784                 if (!(ctrl & reset))
3785                         return true;
3786         } while (time_before(jiffies, timeout));
3787
3788         dev_err(host->dev,
3789                 "Timeout resetting block (ctrl reset %#x)\n",
3790                 ctrl & reset);
3791                 
3792         return false;
3793 }
3794
3795 static inline bool dw_mci_fifo_reset(struct dw_mci *host)
3796 {
3797         /*
3798          * Reseting generates a block interrupt, hence setting
3799          * the scatter-gather pointer to NULL.
3800          */
3801         if (host->sg) {
3802                 sg_miter_stop(&host->sg_miter);
3803                 host->sg = NULL;
3804         }
3805
3806         return dw_mci_ctrl_reset(host, SDMMC_CTRL_FIFO_RESET);
3807 }
3808
3809 static inline bool dw_mci_ctrl_all_reset(struct dw_mci *host)
3810 {
3811         return dw_mci_ctrl_reset(host,
3812                                  SDMMC_CTRL_FIFO_RESET |
3813                                  SDMMC_CTRL_RESET |
3814                                  SDMMC_CTRL_DMA_RESET);
3815 }
3816
3817 static void dw_mci_rst_pre_suspend(struct dw_mci *host)
3818 {
3819         u32 index;
3820         u32 *buffer;
3821
3822         buffer = host->regs_buffer;
3823
3824         for (index = 0; index < DW_REGS_NUM ; index++){
3825                 *buffer = mci_readreg(host, index*4);
3826                 MMC_DBG_INFO_FUNC(host->mmc, "[%s] :0x%08x.\n",
3827                         dw_mci_regs[index].name, *buffer);
3828                 buffer++;
3829         }
3830
3831         *buffer = mci_readl(host,CDTHRCTL);
3832         MMC_DBG_INFO_FUNC(host->mmc, "[%s] :0x%08x.\n", "CARDTHRCTL", *buffer);
3833 }
3834
3835 static void dw_mci_rst_post_resume(struct dw_mci *host)
3836 {
3837         u32 index;
3838         u32 *buffer;
3839
3840         buffer = host->regs_buffer;
3841
3842         for (index = 0; index < DW_REGS_NUM; index++){
3843                 mci_writereg(host, index*4, *buffer);
3844                 buffer++;
3845         }
3846         mci_writel(host, CDTHRCTL, *buffer);
3847 }
3848
3849 static const struct dw_mci_rst_ops dw_mci_pdrst_ops = {
3850         .pre_suspend = dw_mci_rst_pre_suspend,
3851         .post_resume = dw_mci_rst_post_resume,
3852 };
3853
3854 #ifdef CONFIG_OF
3855 /*
3856 static struct dw_mci_of_quirks {
3857         char *quirk;
3858         int id;
3859 } of_quirks[] = {
3860         {
3861                 .quirk  = "broken-cd",
3862                 .id     = DW_MCI_QUIRK_BROKEN_CARD_DETECTION,
3863         },
3864 };
3865 */
3866 static struct dw_mci_board *dw_mci_parse_dt(struct dw_mci *host)
3867 {
3868         struct dw_mci_board *pdata;
3869         struct device *dev = host->dev;
3870         struct device_node *np = dev->of_node;
3871         const struct dw_mci_drv_data *drv_data = host->drv_data;
3872         int  ret;
3873         u32 clock_frequency;
3874
3875         pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
3876         if (!pdata) {
3877                 dev_err(dev, "could not allocate memory for pdata\n");
3878                 return ERR_PTR(-ENOMEM);
3879         }
3880
3881         /* find out number of slots supported */
3882         if (of_property_read_u32(dev->of_node, "num-slots",
3883                                 &pdata->num_slots)) {
3884                 dev_info(dev, "num-slots property not found, "
3885                                 "assuming 1 slot is available\n");
3886                 pdata->num_slots = 1;
3887         }
3888
3889         if (of_property_read_u32(np, "fifo-depth", &pdata->fifo_depth))
3890                 dev_info(dev, "fifo-depth property not found, using "
3891                                 "value of FIFOTH register as default\n");
3892
3893         of_property_read_u32(np, "card-detect-delay", &pdata->detect_delay_ms);
3894
3895         if (!of_property_read_u32(np, "clock-frequency", &clock_frequency))
3896                 pdata->bus_hz = clock_frequency;
3897
3898         if (drv_data && drv_data->parse_dt) {
3899                 ret = drv_data->parse_dt(host);
3900                 if (ret)
3901                         return ERR_PTR(ret);
3902         }
3903
3904         if (of_find_property(np, "keep-power-in-suspend", NULL))
3905                 pdata->pm_caps |= MMC_PM_KEEP_POWER;
3906                 
3907         if (of_find_property(np, "enable-sdio-wakeup", NULL))
3908                 pdata->pm_caps |= MMC_PM_WAKE_SDIO_IRQ;
3909
3910         if (of_find_property(np, "supports-highspeed", NULL))
3911                 pdata->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED;
3912
3913         if (of_find_property(np, "supports-UHS_SDR104", NULL))
3914                 pdata->caps |= MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_SDR50;
3915
3916         if (of_find_property(np, "supports-DDR_MODE", NULL))
3917                 pdata->caps |= MMC_CAP_1_8V_DDR | MMC_CAP_1_2V_DDR;
3918
3919         if (of_find_property(np, "caps2-mmc-hs200", NULL))
3920                 pdata->caps2 |= MMC_CAP2_HS200;
3921
3922         if (of_find_property(np, "caps2-mmc-hs200-1_8v", NULL))
3923                 pdata->caps2 |= MMC_CAP2_HS200_1_8V_SDR;
3924
3925         if (of_find_property(np, "caps2-mmc-hs200-1_2v", NULL))
3926                 pdata->caps2 |= MMC_CAP2_HS200_1_2V_SDR;
3927
3928         if (of_get_property(np, "cd-inverted", NULL))
3929                 pdata->caps2 |= MMC_CAP2_CD_ACTIVE_HIGH;
3930         if (of_get_property(np, "bootpart-no-access", NULL))
3931                 pdata->caps2 |= MMC_CAP2_BOOTPART_NOACC;
3932
3933         if (of_get_property(np, "controller-power-down", NULL)) {
3934                 host->regs_buffer = (u32 *)devm_kzalloc(host->dev,
3935                                                 DW_REGS_SIZE, GFP_KERNEL);
3936                 if (!host->regs_buffer) {
3937                         dev_err(host->dev,
3938                                 "could not allocate memory for regs_buffer\n");
3939                         return ERR_PTR(-ENOMEM);
3940                 }
3941
3942                 host->rst_ops = &dw_mci_pdrst_ops;
3943                 mmc_assume_removable = 0;
3944         }
3945
3946         return pdata;
3947 }
3948
3949 #else /* CONFIG_OF */
3950 static struct dw_mci_board *dw_mci_parse_dt(struct dw_mci *host)
3951 {
3952         return ERR_PTR(-EINVAL);
3953 }
3954 #endif /* CONFIG_OF */
3955
3956 int dw_mci_probe(struct dw_mci *host)
3957 {
3958         const struct dw_mci_drv_data *drv_data = host->drv_data;
3959         int width, i, ret = 0;
3960         u32 fifo_size;
3961         int init_slots = 0;
3962         u32 regs;
3963
3964         if (!host->pdata) {
3965                 host->pdata = dw_mci_parse_dt(host);
3966                 if (IS_ERR(host->pdata)) {
3967                         dev_err(host->dev, "platform data not available\n");
3968                         return -EINVAL;
3969                 }
3970         }
3971
3972         if (!host->pdata->select_slot && host->pdata->num_slots > 1) {
3973                 dev_err(host->dev,
3974                         "Platform data must supply select_slot function\n");
3975                 return -ENODEV;
3976         }
3977
3978         /*
3979          * In 2.40a spec, Data offset is changed.
3980          * Need to check the version-id and set data-offset for DATA register.
3981          */
3982         host->verid = SDMMC_GET_VERID(mci_readl(host, VERID));
3983         dev_info(host->dev, "Version ID is %04x\n", host->verid);
3984
3985         if (host->verid < DW_MMC_240A)
3986                 host->data_offset = DATA_OFFSET;
3987         else
3988                 host->data_offset = DATA_240A_OFFSET;
3989
3990         //hpclk enable
3991         host->hpclk_mmc= devm_clk_get(host->dev, "hpclk_mmc");
3992         if (IS_ERR(host->hpclk_mmc)) {
3993                 dev_err(host->dev, "failed to get hpclk_mmc\n");
3994         } else {
3995                 clk_prepare_enable(host->hpclk_mmc);
3996         }
3997
3998         //hclk enable
3999         host->hclk_mmc= devm_clk_get(host->dev, "hclk_mmc");
4000         if (IS_ERR(host->hclk_mmc)) {
4001                 dev_err(host->dev, "failed to get hclk_mmc\n");
4002                 ret = PTR_ERR(host->hclk_mmc);
4003                 goto err_hclk_mmc;
4004         }
4005
4006         clk_prepare_enable(host->hclk_mmc);
4007
4008         //mmc clk enable
4009         host->clk_mmc = devm_clk_get(host->dev, "clk_mmc");
4010         if (IS_ERR(host->clk_mmc)) {
4011                 dev_err(host->dev, "failed to get clk mmc_per\n");
4012                 ret = PTR_ERR(host->clk_mmc);
4013                 goto err_clk_mmc;
4014         }
4015
4016         host->bus_hz = host->pdata->bus_hz;
4017         if (!host->bus_hz) {
4018                 dev_err(host->dev, "Platform data must supply bus speed\n");
4019                 ret = -ENODEV;
4020                 goto err_clk_mmc;
4021         }
4022
4023         if (host->verid < DW_MMC_240A)
4024                 ret = clk_set_rate(host->clk_mmc, host->bus_hz);
4025         else
4026                 //rockchip: fix divider 2 in clksum before controlller
4027                 ret = clk_set_rate(host->clk_mmc, host->bus_hz * 2);
4028                 
4029         if(ret < 0) {
4030                 dev_err(host->dev, "failed to set clk mmc\n");
4031                 goto err_clk_mmc;
4032         }
4033         clk_prepare_enable(host->clk_mmc);
4034
4035         if (drv_data && drv_data->setup_clock) {
4036                 ret = drv_data->setup_clock(host);
4037                 if (ret) {
4038                         dev_err(host->dev,
4039                         "implementation specific clock setup failed\n");
4040                         goto err_clk_mmc;
4041                 }
4042         }
4043
4044         host->quirks = host->pdata->quirks;
4045         host->irq_state = true;
4046         host->set_speed = 0;
4047         host->set_div = 0;
4048         host->svi_flags = 0;
4049
4050         spin_lock_init(&host->lock);
4051         spin_lock_init(&host->slock);
4052
4053         INIT_LIST_HEAD(&host->queue);
4054         /*
4055          * Get the host data width - this assumes that HCON has been set with
4056          * the correct values.
4057          */
4058         i = (mci_readl(host, HCON) >> 7) & 0x7;
4059         if (!i) {
4060                 host->push_data = dw_mci_push_data16;
4061                 host->pull_data = dw_mci_pull_data16;
4062                 width = 16;
4063                 host->data_shift = 1;
4064         } else if (i == 2) {
4065                 host->push_data = dw_mci_push_data64;
4066                 host->pull_data = dw_mci_pull_data64;
4067                 width = 64;
4068                 host->data_shift = 3;
4069         } else {
4070                 /* Check for a reserved value, and warn if it is */
4071                 WARN((i != 1),
4072                      "HCON reports a reserved host data width!\n"
4073                      "Defaulting to 32-bit access.\n");
4074                 host->push_data = dw_mci_push_data32;
4075                 host->pull_data = dw_mci_pull_data32;
4076                 width = 32;
4077                 host->data_shift = 2;
4078         }
4079
4080         /* Reset all blocks */
4081         if (!dw_mci_ctrl_all_reset(host))
4082                 return -ENODEV;
4083
4084         host->dma_ops = host->pdata->dma_ops;
4085         dw_mci_init_dma(host);
4086
4087         /* Clear the interrupts for the host controller */
4088         mci_writel(host, RINTSTS, 0xFFFFFFFF);
4089         mci_writel(host, INTMASK, 0); /* disable all mmc interrupt first */
4090
4091         /* Put in max timeout */
4092         mci_writel(host, TMOUT, 0xFFFFFFFF);
4093
4094         /*
4095          * FIFO threshold settings  RxMark  = fifo_size / 2 - 1,
4096          *                          Tx Mark = fifo_size / 2 DMA Size = 8
4097          */
4098         if (!host->pdata->fifo_depth) {
4099                 /*
4100                  * Power-on value of RX_WMark is FIFO_DEPTH-1, but this may
4101                  * have been overwritten by the bootloader, just like we're
4102                  * about to do, so if you know the value for your hardware, you
4103                  * should put it in the platform data.
4104                  */
4105                 fifo_size = mci_readl(host, FIFOTH);
4106                 fifo_size = 1 + ((fifo_size >> 16) & 0xfff);
4107         } else {
4108                 fifo_size = host->pdata->fifo_depth;
4109         }
4110         host->fifo_depth = fifo_size;
4111         host->fifoth_val =
4112                 SDMMC_SET_FIFOTH(0x2, fifo_size / 2 - 1, fifo_size / 2);
4113         mci_writel(host, FIFOTH, host->fifoth_val);
4114
4115         /* disable clock to CIU */
4116         mci_writel(host, CLKENA, 0);
4117         mci_writel(host, CLKSRC, 0);
4118
4119         tasklet_init(&host->tasklet, dw_mci_tasklet_func, (unsigned long)host);
4120         host->card_workqueue = alloc_workqueue("dw-mci-card",
4121                         WQ_MEM_RECLAIM | WQ_NON_REENTRANT, 1);
4122         if (!host->card_workqueue) {
4123                 ret = -ENOMEM;
4124                 goto err_dmaunmap;
4125         }
4126         INIT_WORK(&host->card_work, dw_mci_work_routine_card);
4127         ret = devm_request_irq(host->dev, host->irq, dw_mci_interrupt,
4128                                host->irq_flags, "dw-mci", host);
4129         if (ret)
4130                 goto err_workqueue;
4131
4132         if (host->pdata->num_slots)
4133                 host->num_slots = host->pdata->num_slots;
4134         else
4135                 host->num_slots = ((mci_readl(host, HCON) >> 1) & 0x1F) + 1;
4136
4137         /* We need at least one slot to succeed */
4138         for (i = 0; i < host->num_slots; i++) {
4139                 ret = dw_mci_init_slot(host, i);
4140                 if (ret)
4141                         dev_dbg(host->dev, "slot %d init failed\n", i);
4142                 else
4143                         init_slots++;
4144         }
4145         
4146         /*
4147          * Enable interrupts for command done, data over, data empty, card det,
4148          * receive ready and error such as transmit, receive timeout, crc error
4149          */
4150         mci_writel(host, RINTSTS, 0xFFFFFFFF);
4151         regs = SDMMC_INT_CMD_DONE | SDMMC_INT_DATA_OVER | SDMMC_INT_TXDR |
4152                 SDMMC_INT_VSI | SDMMC_INT_RXDR | DW_MCI_ERROR_FLAGS;
4153         if (!(host->mmc->restrict_caps & RESTRICT_CARD_TYPE_SDIO) &&
4154                 !(host->mmc->restrict_caps & RESTRICT_CARD_TYPE_EMMC))
4155                 regs |= SDMMC_INT_CD;
4156
4157         mci_writel(host, INTMASK, regs);
4158
4159         mci_writel(host, CTRL, SDMMC_CTRL_INT_ENABLE); /* Enable mci interrupt */
4160         
4161         dev_info(host->dev, "DW MMC controller at irq %d, "
4162                  "%d bit host data width, "
4163                  "%u deep fifo\n",
4164                  host->irq, width, fifo_size);
4165
4166         if (init_slots) {
4167                 dev_info(host->dev, "%d slots initialized\n", init_slots);
4168         } else {
4169                 dev_dbg(host->dev, "attempted to initialize %d slots, "
4170                                         "but failed on all\n", host->num_slots);
4171                 goto err_workqueue;
4172         }
4173
4174
4175         if (host->quirks & DW_MCI_QUIRK_IDMAC_DTO)
4176                 dev_info(host->dev, "Internal DMAC interrupt fix enabled.\n");
4177
4178         return 0;
4179
4180 err_workqueue:
4181         destroy_workqueue(host->card_workqueue);
4182
4183 err_dmaunmap:
4184         if (host->use_dma && host->dma_ops->exit)
4185                 host->dma_ops->exit(host);
4186
4187         if (host->vmmc){
4188                 regulator_disable(host->vmmc);
4189                 regulator_put(host->vmmc);
4190         }
4191
4192 err_clk_mmc:
4193         if (!IS_ERR(host->clk_mmc))
4194                 clk_disable_unprepare(host->clk_mmc);
4195 err_hclk_mmc:
4196         if (!IS_ERR(host->hclk_mmc))
4197                 clk_disable_unprepare(host->hclk_mmc);
4198         return ret;
4199 }
4200 EXPORT_SYMBOL(dw_mci_probe);
4201
4202 void dw_mci_remove(struct dw_mci *host)
4203 {
4204         struct mmc_host *mmc = host->mmc;
4205         struct dw_mci_slot *slot = mmc_priv(mmc);
4206         int i;
4207
4208         mci_writel(host, RINTSTS, 0xFFFFFFFF);
4209         mci_writel(host, INTMASK, 0); /* disable all mmc interrupt first */
4210
4211         for(i = 0; i < host->num_slots; i++){
4212                 dev_dbg(host->dev, "remove slot %d\n", i);
4213                 if(host->slot[i])
4214                         dw_mci_cleanup_slot(host->slot[i], i);
4215         }
4216
4217         /* disable clock to CIU */
4218         mci_writel(host, CLKENA, 0);
4219         mci_writel(host, CLKSRC, 0);
4220
4221         destroy_workqueue(host->card_workqueue);
4222         if (host->mmc->restrict_caps & RESTRICT_CARD_TYPE_SD)
4223                 unregister_pm_notifier(&host->mmc->pm_notify);
4224
4225         if (host->use_dma && host->dma_ops->exit)
4226                 host->dma_ops->exit(host);
4227
4228         if (gpio_is_valid(slot->cd_gpio))
4229                 dw_mci_of_free_cd_gpio_irq(host->dev, slot->cd_gpio, host->mmc);
4230
4231         if (host->vmmc){
4232                 regulator_disable(host->vmmc);
4233                 regulator_put(host->vmmc);
4234         }
4235         if (!IS_ERR(host->clk_mmc))
4236                 clk_disable_unprepare(host->clk_mmc);
4237
4238         if (!IS_ERR(host->hclk_mmc))
4239                 clk_disable_unprepare(host->hclk_mmc);
4240         if (!IS_ERR(host->hpclk_mmc))
4241                 clk_disable_unprepare(host->hpclk_mmc);
4242 }
4243 EXPORT_SYMBOL(dw_mci_remove);
4244
4245
4246
4247 #ifdef CONFIG_PM_SLEEP
4248 /*
4249  * TODO: we should probably disable the clock to the card in the suspend path.
4250  */
4251 extern int get_wifi_chip_type(void);
4252 int dw_mci_suspend(struct dw_mci *host)
4253 {
4254         int present = dw_mci_get_cd(host->mmc);
4255
4256         if((host->mmc->restrict_caps &
4257                 RESTRICT_CARD_TYPE_SDIO) &&
4258                 (get_wifi_chip_type() == WIFI_ESP8089 ||
4259                 get_wifi_chip_type() > WIFI_AP6XXX_SERIES))
4260                 return 0;
4261
4262         if(host->vmmc)
4263                 regulator_disable(host->vmmc);
4264
4265         /* Only for sdmmc controller */
4266         if (host->mmc->restrict_caps & RESTRICT_CARD_TYPE_SD) {
4267                 disable_irq(host->irq);
4268                 if (present) {
4269                         if (pinctrl_select_state(host->pinctrl, host->pins_idle) < 0)
4270                                 MMC_DBG_ERR_FUNC(host->mmc,
4271                                         "Idle pinctrl setting failed! [%s]",
4272                                         mmc_hostname(host->mmc));
4273                 }
4274
4275                 /* Soc rk3126/3036 already in gpio_cd mode */
4276                 if (!soc_is_rk3126() && !soc_is_rk3126b() && !soc_is_rk3036()) {
4277                         dw_mci_of_get_cd_gpio(host->dev, 0, host->mmc);
4278                         enable_irq_wake(host->mmc->slot.cd_irq);
4279                 }
4280         }
4281
4282         mci_writel(host, RINTSTS, 0xFFFFFFFF);
4283         mci_writel(host, INTMASK, 0x00);
4284         mci_writel(host, CTRL, 0x00);
4285
4286         if (host->rst_ops &&
4287                 host->rst_ops->pre_suspend)
4288                 host->rst_ops->pre_suspend(host);
4289
4290         return 0;
4291 }
4292 EXPORT_SYMBOL(dw_mci_suspend);
4293
4294 int dw_mci_resume(struct dw_mci *host)
4295 {
4296         int i, ret;
4297         u32 regs;
4298         struct dw_mci_slot *slot;
4299         int present = dw_mci_get_cd(host->mmc);
4300
4301         if (host->rst_ops &&
4302                 host->rst_ops->post_resume)
4303                 host->rst_ops->post_resume(host);
4304
4305
4306         if ((host->mmc->restrict_caps & RESTRICT_CARD_TYPE_SDIO) &&
4307                 (get_wifi_chip_type() == WIFI_ESP8089 ||
4308                         get_wifi_chip_type() > WIFI_AP6XXX_SERIES))
4309                 return 0;
4310
4311         if (host->mmc->restrict_caps & RESTRICT_CARD_TYPE_SDIO) {
4312                 slot = mmc_priv(host->mmc);
4313                 if (!test_bit(DW_MMC_CARD_PRESENT, &slot->flags))
4314                         return 0;
4315         }
4316
4317         /*only for sdmmc controller*/
4318         if (host->mmc->restrict_caps & RESTRICT_CARD_TYPE_SD) {
4319                 /* Soc rk3126/3036 already in gpio_cd mode */
4320                 if (!soc_is_rk3126() && !soc_is_rk3126b() && !soc_is_rk3036()) {
4321                         disable_irq_wake(host->mmc->slot.cd_irq);
4322                         mmc_gpio_free_cd(host->mmc);
4323                 }
4324
4325                 if (!present) {
4326                         if (!IS_ERR(host->pins_udbg)) {
4327                                 if (pinctrl_select_state(host->pinctrl, host->pins_idle) < 0)
4328                                         MMC_DBG_ERR_FUNC(host->mmc,
4329                                                 "Idle pinctrl setting failed! [%s]",
4330                                                 mmc_hostname(host->mmc));
4331                                 if (pinctrl_select_state(host->pinctrl, host->pins_udbg) < 0)
4332                                         MMC_DBG_ERR_FUNC(host->mmc,
4333                                                 "Udbg pinctrl setting failed! [%s]",
4334                                                 mmc_hostname(host->mmc));
4335                         } else {
4336                                 if (pinctrl_select_state(host->pinctrl, host->pins_default) < 0)
4337                                         MMC_DBG_ERR_FUNC(host->mmc,
4338                                                 "Default pinctrl setting failed! [%s]",
4339                                                 mmc_hostname(host->mmc));
4340                         }
4341                 } else {
4342                         if(pinctrl_select_state(host->pinctrl, host->pins_default) < 0)
4343                                 MMC_DBG_ERR_FUNC(host->mmc,
4344                                         "Default pinctrl setting failed! [%s]",
4345                                         mmc_hostname(host->mmc));
4346                 }
4347
4348                 /* Disable jtag*/
4349                 if(cpu_is_rk3288())
4350                         grf_writel(((1 << 12) << 16) | (0 << 12), RK3288_GRF_SOC_CON0);
4351                 else if(cpu_is_rk3036())
4352                         grf_writel(((1 << 11) << 16) | (0 << 11), RK3036_GRF_SOC_CON0);
4353                 else if(cpu_is_rk312x())
4354                         /* RK3036_GRF_SOC_CON0 is compatible with rk312x, tmp setting */
4355                         grf_writel(((1 << 8) << 16) | (0 << 8), RK3036_GRF_SOC_CON0);
4356         }
4357         if(host->vmmc){
4358                 ret = regulator_enable(host->vmmc);
4359                 if (ret){
4360                         dev_err(host->dev,
4361                                 "failed to enable regulator: %d\n", ret);
4362                         return ret;
4363                 }
4364         }
4365         
4366         if(!dw_mci_ctrl_all_reset(host)){
4367                 ret = -ENODEV;
4368                 return ret;
4369         }
4370
4371         if(!(cpu_is_rk3036() || cpu_is_rk312x()))
4372                 if(host->use_dma && host->dma_ops->init)
4373                         host->dma_ops->init(host);
4374
4375         /*
4376          * Restore the initial value at FIFOTH register
4377          * And Invalidate the prev_blksz with zero
4378          */
4379         mci_writel(host, FIFOTH, host->fifoth_val);
4380         host->prev_blksz = 0;
4381         /* Put in max timeout */
4382         mci_writel(host, TMOUT, 0xFFFFFFFF);
4383
4384         mci_writel(host, RINTSTS, 0xFFFFFFFF);
4385         regs = SDMMC_INT_CMD_DONE | SDMMC_INT_DATA_OVER | SDMMC_INT_TXDR |
4386                 SDMMC_INT_RXDR | SDMMC_INT_VSI | DW_MCI_ERROR_FLAGS;
4387
4388         if(!(host->mmc->restrict_caps & RESTRICT_CARD_TYPE_SDIO))
4389             regs |= SDMMC_INT_CD;
4390
4391         mci_writel(host, INTMASK, regs);
4392         mci_writel(host, CTRL, SDMMC_CTRL_INT_ENABLE);
4393
4394         /*only for sdmmc controller*/
4395         if((host->mmc->restrict_caps & RESTRICT_CARD_TYPE_SD)){
4396                 enable_irq(host->irq);  
4397         }   
4398
4399         for(i = 0; i < host->num_slots; i++){
4400                 struct dw_mci_slot *slot = host->slot[i];
4401                 if(!slot)
4402                         continue;
4403                 if(slot->mmc->pm_flags & MMC_PM_KEEP_POWER){
4404                         dw_mci_set_ios(slot->mmc, &slot->mmc->ios);
4405                         dw_mci_setup_bus(slot, true);
4406                 }
4407         }
4408
4409         return 0;
4410 }
4411 EXPORT_SYMBOL(dw_mci_resume);
4412 #endif /* CONFIG_PM_SLEEP */
4413
4414 static int __init dw_mci_init(void)
4415 {
4416         pr_info("Synopsys Designware Multimedia Card Interface Driver\n");
4417         pr_info("MHSC version = %s\n", RK_SDMMC_DRIVER_VERSION);
4418         return 0;
4419 }
4420
4421 static void __exit dw_mci_exit(void)
4422 {
4423 }
4424
4425 module_init(dw_mci_init);
4426 module_exit(dw_mci_exit);
4427
4428 MODULE_DESCRIPTION("Rockchip specific DW Multimedia Card Interface driver");
4429 MODULE_AUTHOR("Shawn Lin <lintao@rock-chips.com>");
4430 MODULE_LICENSE("GPL v2");