1 /* drivers/mmc/host/rk29_sdmmc.c
3 * Copyright (C) 2010 ROCKCHIP, Inc.
5 * This software is licensed under the terms of the GNU General Public
6 * License version 2, as published by the Free Software Foundation, and
7 * may be copied, distributed, and modified under those terms.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * mount -t debugfs debugfs /data/debugfs;cat /data/debugfs/mmc0/status
15 * echo 't' >/proc/sysrq-trigger
16 * echo 19 >/sys/module/wakelock/parameters/debug_mask
17 * vdc volume uevent on
20 #include <linux/blkdev.h>
21 #include <linux/clk.h>
22 #include <linux/debugfs.h>
23 #include <linux/device.h>
24 #include <linux/dma-mapping.h>
25 #include <linux/err.h>
26 #include <linux/init.h>
27 #include <linux/interrupt.h>
28 #include <linux/ioport.h>
29 #include <linux/module.h>
30 #include <linux/platform_device.h>
31 #include <linux/scatterlist.h>
32 #include <linux/seq_file.h>
33 #include <linux/stat.h>
34 #include <linux/delay.h>
35 #include <linux/irq.h>
36 #include <linux/slab.h>
37 #include <linux/version.h>
38 #include <linux/mmc/host.h>
39 #include <linux/mmc/mmc.h>
40 #include <linux/mmc/card.h>
42 #include <mach/board.h>
44 #include <mach/gpio.h>
45 #include <mach/iomux.h>
46 #include <asm/unaligned.h>
49 #include <mach/dma-pl330.h>
50 #include <asm/scatterlist.h>
52 #include "rk29_sdmmc.h"
55 #define RK29_SDMMC_xbw_Debug 0
57 #if RK29_SDMMC_xbw_Debug
59 #define xbwprintk(n, format, arg...) \
60 if (n <= debug_level) { \
61 printk(format,##arg); \
64 #define xbwprintk(n, arg...)
67 #if defined(CONFIG_ARCH_RK29)
68 #define SDMMC_USE_INT_UNBUSY 0
70 #define SDMMC_USE_INT_UNBUSY 0///1
74 ** You can set the macro to true, if some module wants to use this feature, which is about SDIO suspend-resume.
75 ** As the following example.
76 ** added by xbw at 2013-05-08
78 #if defined(CONFIG_MTK_COMBO_DRIVER_VERSION_JB2)
79 #define RK_SDMMC_USE_SDIO_SUSPEND_RESUME 1
81 #define RK_SDMMC_USE_SDIO_SUSPEND_RESUME 0
84 #define RK29_SDMMC_ERROR_FLAGS (SDMMC_INT_FRUN | SDMMC_INT_HLE )
86 #if defined(CONFIG_SDMMC0_RK29_SDCARD_DET_FROM_GPIO)
87 #if SDMMC_USE_INT_UNBUSY
88 #define RK29_SDMMC_INTMASK_USEDMA (SDMMC_INT_CMD_DONE | SDMMC_INT_DTO | SDMMC_INT_UNBUSY |RK29_SDMMC_ERROR_FLAGS )
89 #define RK29_SDMMC_INTMASK_USEIO (SDMMC_INT_CMD_DONE | SDMMC_INT_DTO | SDMMC_INT_UNBUSY |RK29_SDMMC_ERROR_FLAGS | SDMMC_INT_TXDR | SDMMC_INT_RXDR )
91 #define RK29_SDMMC_INTMASK_USEDMA (SDMMC_INT_CMD_DONE | SDMMC_INT_DTO | RK29_SDMMC_ERROR_FLAGS )
92 #define RK29_SDMMC_INTMASK_USEIO (SDMMC_INT_CMD_DONE | SDMMC_INT_DTO | RK29_SDMMC_ERROR_FLAGS | SDMMC_INT_TXDR | SDMMC_INT_RXDR )
95 #if SDMMC_USE_INT_UNBUSY
96 #define RK29_SDMMC_INTMASK_USEDMA (SDMMC_INT_CMD_DONE | SDMMC_INT_DTO | SDMMC_INT_UNBUSY |RK29_SDMMC_ERROR_FLAGS | SDMMC_INT_CD)
97 #define RK29_SDMMC_INTMASK_USEIO (SDMMC_INT_CMD_DONE | SDMMC_INT_DTO | SDMMC_INT_UNBUSY |RK29_SDMMC_ERROR_FLAGS | SDMMC_INT_CD| SDMMC_INT_TXDR | SDMMC_INT_RXDR )
99 #define RK29_SDMMC_INTMASK_USEDMA (SDMMC_INT_CMD_DONE | SDMMC_INT_DTO | RK29_SDMMC_ERROR_FLAGS | SDMMC_INT_CD)
100 #define RK29_SDMMC_INTMASK_USEIO (SDMMC_INT_CMD_DONE | SDMMC_INT_DTO | RK29_SDMMC_ERROR_FLAGS | SDMMC_INT_CD| SDMMC_INT_TXDR | SDMMC_INT_RXDR )
104 #define RK29_SDMMC_SEND_START_TIMEOUT 3000 //The time interval from the time SEND_CMD to START_CMD_BIT cleared.
105 #define RK29_ERROR_PRINTK_INTERVAL 200 //The time interval between the two printk for the same error.
106 #define RK29_SDMMC_WAIT_DTO_INTERNVAL 4500 //The time interval from the CMD_DONE_INT to DTO_INT
107 #define RK29_SDMMC_REMOVAL_DELAY 2000 //The time interval from the CD_INT to detect_timer react.
109 #define RK29_SDMMC_VERSION "Ver.5.05 The last modify date is 2013-05-08"
111 #if !defined(CONFIG_USE_SDMMC0_FOR_WIFI_DEVELOP_BOARD)
112 #define RK29_CTRL_SDMMC_ID 0 //mainly used by SDMMC
113 #define RK29_CTRL_SDIO1_ID 1 //mainly used by sdio-wifi
114 #define RK29_CTRL_SDIO2_ID 2 //mainly used by sdio-card
116 #define RK29_CTRL_SDMMC_ID 5
117 #define RK29_CTRL_SDIO1_ID 1
118 #define RK29_CTRL_SDIO2_ID 2
121 #define SDMMC_CLOCK_TEST 0
123 #define RK29_SDMMC_NOTIFY_REMOVE_INSERTION /* use sysfs to notify the removal or insertion of sd-card*/
124 //#define RK29_SDMMC_LIST_QUEUE /* use list-queue for multi-card*/
126 #define RK29_SDMMC_DEFAULT_SDIO_FREQ 0 // 1--run in default frequency(50Mhz); 0---run in 25Mhz,
127 #if defined(CONFIG_MT6620)
128 #define RK29_MAX_SDIO_FREQ 45000000 //set max-sdio-frequency 45Mhz in MTK module.
130 #define RK29_MAX_SDIO_FREQ 25000000 //set max-sdio-frequency 25Mhz at the present time
133 //use the new iomux-API
134 #if defined(CONFIG_ARCH_RK3066B)||defined(CONFIG_ARCH_RK3168)||defined(CONFIG_ARCH_RK3188)
135 #define DRIVER_SDMMC_USE_NEW_IOMUX_API 1
137 #define DRIVER_SDMMC_USE_NEW_IOMUX_API 0
141 EVENT_CMD_COMPLETE = 0,
148 enum rk29_sdmmc_state {
157 struct rk29_sdmmc_dma_info {
160 struct rk29_dma_client client;
163 static struct rk29_sdmmc_dma_info rk29_sdmmc_dma_infos[]= {
167 .name = "rk29-dma-sdmmc0",
173 .name = "rk29-dma-sdio1",
180 .name = "rk29-dma-sdio2",
186 /* Interrupt Information */
187 typedef struct TagSDC_INT_INFO
189 u32 transLen; //the length of data sent.
190 u32 desLen; //the total length of the all data.
191 u32 *pBuf; //the data buffer for interrupt read or write.
200 struct mmc_request *mrq;
201 struct mmc_request *new_mrq;
202 struct mmc_command *cmd;
203 struct mmc_data *data;
204 struct scatterlist *sg;
206 dma_addr_t dma_addr;;
207 unsigned int use_dma:1;
214 u32 cmdr; //the value setted into command-register
215 u32 dodma; //sign the DMA used for transfer.
216 u32 errorstep;//record the error point.
217 int timeout_times; //use to force close the sdmmc0 when the timeout_times exceeds the limit.
219 SDC_INT_INFO_T intInfo;
220 struct rk29_sdmmc_dma_info dma_info;
225 struct tasklet_struct tasklet;
226 unsigned long pending_events;
227 unsigned long completed_events;
228 enum rk29_sdmmc_state state;
230 #ifdef RK29_SDMMC_LIST_QUEUE
231 struct list_head queue;
232 struct list_head queue_node;
236 struct platform_device *pdev;
237 struct mmc_host *mmc;
242 #define RK29_SDMMC_CARD_PRESENT 0
246 struct timer_list detect_timer;
247 struct timer_list request_timer; //the timer for INT_CMD_DONE
248 struct timer_list DTO_timer; //the timer for INT_DTO
249 struct mmc_command stopcmd;
250 struct rksdmmc_gpio det_pin;
252 /* flag for current bus settings */
255 unsigned int oldstatus;
256 unsigned int complete_done;
257 unsigned int retryfunc;
261 int gpio_power_en_level;
262 struct delayed_work work;
264 #ifdef CONFIG_RK29_SDIO_IRQ_FROM_GPIO
265 unsigned int sdio_INT_gpio;
266 unsigned int sdio_irq;
267 unsigned long trigger_level;
270 #if defined(CONFIG_SDMMC0_RK29_WRITE_PROTECT) || defined(CONFIG_SDMMC1_RK29_WRITE_PROTECT)
276 void (*set_iomux)(int device_id, unsigned int bus_width);
281 #ifdef RK29_SDMMC_NOTIFY_REMOVE_INSERTION
282 static struct rk29_sdmmc *globalSDhost[3];
285 #define rk29_sdmmc_test_and_clear_pending(host, event) \
286 test_and_clear_bit(event, &host->pending_events)
287 #define rk29_sdmmc_test_pending(host, event) \
288 test_bit(event, &host->pending_events)
289 #define rk29_sdmmc_test_completed(host, event) \
290 test_bit(event, &host->completed_events)
291 #define rk29_sdmmc_set_completed(host, event) \
292 set_bit(event, &host->completed_events)
293 #define rk29_sdmmc_set_pending(host, event) \
294 set_bit(event, &host->pending_events)
296 static void rk29_sdmmc_start_error(struct rk29_sdmmc *host);
297 static int rk29_sdmmc_clear_fifo(struct rk29_sdmmc *host);
298 int rk29_sdmmc_hw_init(void *data);
300 static void rk29_sdmmc_write(unsigned char __iomem *regbase, unsigned int regOff,unsigned int val)
302 __raw_writel(val,regbase + regOff);
305 static unsigned int rk29_sdmmc_read(unsigned char __iomem *regbase, unsigned int regOff)
307 return __raw_readl(regbase + regOff);
310 static int rk29_sdmmc_regs_printk(struct rk29_sdmmc *host)
312 printk("SDMMC_CTRL: \t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_CTRL));
313 printk("SDMMC_PWREN: \t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_PWREN));
314 printk("SDMMC_CLKDIV: \t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_CLKDIV));
315 printk("SDMMC_CLKSRC: \t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_CLKSRC));
316 printk("SDMMC_CLKENA: \t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_CLKENA));
317 printk("SDMMC_TMOUT: \t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_TMOUT));
318 printk("SDMMC_CTYPE: \t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_CTYPE));
319 printk("SDMMC_BLKSIZ: \t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_BLKSIZ));
320 printk("SDMMC_BYTCNT: \t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_BYTCNT));
321 printk("SDMMC_INTMASK:\t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_INTMASK));
322 printk("SDMMC_CMDARG: \t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_CMDARG));
323 printk("SDMMC_CMD: \t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_CMD));
324 printk("SDMMC_RESP0: \t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_RESP0));
325 printk("SDMMC_RESP1: \t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_RESP1));
326 printk("SDMMC_RESP2: \t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_RESP2));
327 printk("SDMMC_RESP3: \t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_RESP3));
328 printk("SDMMC_MINTSTS:\t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_MINTSTS));
329 printk("SDMMC_RINTSTS:\t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_RINTSTS));
330 printk("SDMMC_STATUS: \t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_STATUS));
331 printk("SDMMC_FIFOTH: \t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_FIFOTH));
332 printk("SDMMC_CDETECT:\t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_CDETECT));
333 printk("SDMMC_WRTPRT: \t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_WRTPRT));
334 printk("SDMMC_TCBCNT: \t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_TCBCNT));
335 printk("SDMMC_TBBCNT: \t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_TBBCNT));
336 printk("SDMMC_DEBNCE: \t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_DEBNCE));
337 printk("SDMMC_USRID: \t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_USRID));
339 #if !defined(CONFIG_ARCH_RK29)
340 printk("SDMMC_VERID: \t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_VERID));
341 printk("SDMMC_UHS_REG:\t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_UHS_REG));
342 printk("SDMMC_RST_n: \t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_RST_n));
343 printk("SDMMC_CARDTHRCTL: \t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_CARDTHRCTL));
344 printk("SDMMC_BACK_END_POWER: \t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_BACK_END_POWER));
346 printk("=======printk %s-register end =========\n", host->dma_name);
350 static void rk29_sdmmc_enable_irq(struct rk29_sdmmc *host, bool irqflag)
359 local_irq_save(flags);
360 if(host->irq_state != irqflag)
362 host->irq_state = irqflag;
365 enable_irq(host->irq);
369 disable_irq(host->irq);
372 local_irq_restore(flags);
376 #ifdef RK29_SDMMC_NOTIFY_REMOVE_INSERTION
377 ssize_t rk29_sdmmc_progress_store(struct kobject *kobj, struct kobj_attribute *attr,
378 const char *buf, size_t count)
380 struct rk29_sdmmc *host = NULL;
381 static u32 unmounting_times = 0;
382 static char oldbuf[64];
384 if( !strncmp(buf,"version" , strlen("version")))
386 printk(KERN_INFO "\n The driver SDMMC named 'rk29_sdmmc.c' is %s.\n", RK29_SDMMC_VERSION);
390 //envalue the address of host base on input-parameter.
391 if( !strncmp(buf,"sd-" , strlen("sd-")) )
393 host = (struct rk29_sdmmc *)globalSDhost[0];
396 printk(KERN_WARNING "%s..%d.. fail to call progress_store because the host is null. \n",__FUNCTION__,__LINE__);
400 else if( !strncmp(buf,"sdio1-" , strlen("sdio1-")) )
402 host = (struct rk29_sdmmc *)globalSDhost[RK29_CTRL_SDIO1_ID];
405 printk(KERN_WARNING "%s..%d.. fail to call progress_store because the host-sdio1 is null.\n",__FUNCTION__,__LINE__);
409 else if( !strncmp(buf,"sdio2-" , strlen("sdio2-")) )
411 host = (struct rk29_sdmmc *)globalSDhost[RK29_CTRL_SDIO2_ID];
414 printk(KERN_WARNING "%s..%d.. fail to call progress_store because the host-sdio2 is null.\n",__FUNCTION__,__LINE__);
420 printk(KERN_WARNING "%s..%d.. You want to use sysfs for SDMMC but input-parameter is wrong.\n",__FUNCTION__,__LINE__);
423 rk29_sdmmc_enable_irq(host, false);
425 //spin_lock(&host->lock);
426 if(strncmp(buf,oldbuf , strlen(buf)))
428 printk(KERN_INFO ".%d.. MMC0 receive the message %s from VOLD.[%s]\n", __LINE__, buf, host->dma_name);
433 * //deal with the message
434 * insert card state-change: No-Media ==> Pending ==> Idle-Unmounted ==> Checking ==>Mounted
435 * remove card state-change: Unmounting ==> Idle-Unmounted ==> No-Media
437 #if !defined(CONFIG_USE_SDMMC0_FOR_WIFI_DEVELOP_BOARD)
438 if(RK29_CTRL_SDMMC_ID == host->pdev->id)
440 if(!strncmp(buf, "sd-Unmounting", strlen("sd-Unmounting")))
442 if(unmounting_times++%10 == 0)
444 printk(KERN_INFO ".%d.. MMC0 receive the message Unmounting(waitTimes=%d) from VOLD.[%s]\n", \
445 __LINE__, unmounting_times, host->dma_name);
448 if(0 == host->mmc->re_initialized_flags)
449 mod_timer(&host->detect_timer, jiffies + msecs_to_jiffies(RK29_SDMMC_REMOVAL_DELAY*2));
451 else if(!strncmp(buf, "sd-Idle-Unmounted", strlen("sd-Idle-Unmounted")))
453 if(0 == host->mmc->re_initialized_flags)
454 mod_timer(&host->detect_timer, jiffies + msecs_to_jiffies(RK29_SDMMC_REMOVAL_DELAY*2));
456 else if( !strncmp(buf, "sd-No-Media", strlen("sd-No-Media")))
458 printk(KERN_INFO ".%d.. MMC0 receive the message No-Media from VOLD. waitTimes=%d [%s]\n" ,\
459 __LINE__,unmounting_times, host->dma_name);
461 del_timer_sync(&host->detect_timer);
462 host->mmc->re_initialized_flags = 1;
463 unmounting_times = 0;
465 if(test_bit(RK29_SDMMC_CARD_PRESENT, &host->flags))
467 mmc_detect_change(host->mmc, 0);
470 else if( !strncmp(buf, "sd-Ready", strlen("sd-Ready")))
472 printk(KERN_INFO ".%d.. MMC0 receive the message Ready(ReInitFlag=%d) from VOLD. waitTimes=%d [%s]\n" ,\
473 __LINE__, host->mmc->re_initialized_flags, unmounting_times, host->dma_name);
475 unmounting_times = 0;
476 host->mmc->re_initialized_flags = 1;
478 else if( !strncmp(buf,"sd-reset" , strlen("sd-reset")) )
480 printk(KERN_INFO ".%d.. Now manual reset for SDMMC0. [%s]\n",__LINE__, host->dma_name);
481 rk29_sdmmc_hw_init(host);
482 mmc_detect_change(host->mmc, 0);
484 else if( !strncmp(buf, "sd-regs", strlen("sd-regs")))
486 printk(KERN_INFO ".%d.. Now printk the register of SDMMC0. [%s]\n",__LINE__, host->dma_name);
487 rk29_sdmmc_regs_printk(host);
492 if(0 == host->pdev->id)
494 if( !strncmp(buf,"sd-reset" , strlen("sd-reset")) )
496 printk(KERN_INFO ".%d.. Now manual reset for SDMMC0. [%s]\n",__LINE__, host->dma_name);
497 rk29_sdmmc_hw_init(host);
498 mmc_detect_change(host->mmc, 0);
500 else if( !strncmp(buf, "sd-regs", strlen("sd-regs")))
502 printk(KERN_INFO ".%d.. Now printk the register of SDMMC0. [%s]\n",__LINE__, host->dma_name);
503 rk29_sdmmc_regs_printk(host);
507 else if(RK29_CTRL_SDIO1_ID == host->pdev->id)
509 if( !strncmp(buf, "sdio1-regs", strlen("sdio1-regs")))
511 printk(KERN_INFO ".%d.. Now printk the register of SDMMC1. [%s]\n",__LINE__, host->dma_name);
512 rk29_sdmmc_regs_printk(host);
514 else if( !strncmp(buf,"sdio1-reset" , strlen("sdio1-reset")) )
516 printk(KERN_INFO ".%d.. Now manual reset for SDMMC1. [%s]\n",__LINE__, host->dma_name);
517 rk29_sdmmc_hw_init(host);
518 mmc_detect_change(host->mmc, 0);
521 else if(RK29_CTRL_SDIO2_ID == host->pdev->id)
523 if( !strncmp(buf, "sdio2-regs", strlen("sdio2-regs")))
525 printk(KERN_INFO ".%d.. Now printk the register of SDMMC2. [%s]\n",__LINE__, host->dma_name);
526 rk29_sdmmc_regs_printk(host);
528 else if( !strncmp(buf,"sdio2-reset" , strlen("sdio2-reset")) )
530 printk(KERN_INFO ".%d.. Now manual reset for SDMMC2. [%s]\n",__LINE__, host->dma_name);
531 rk29_sdmmc_hw_init(host);
532 mmc_detect_change(host->mmc, 0);
536 // spin_unlock(&host->lock);
538 rk29_sdmmc_enable_irq(host, true);
544 struct kobj_attribute mmc_reset_attrs =
550 .store = rk29_sdmmc_progress_store,
552 struct attribute *mmc_attrs[] =
554 &mmc_reset_attrs.attr,
558 static struct kobj_type mmc_kset_ktype = {
559 .sysfs_ops = &kobj_sysfs_ops,
560 .default_attrs = &mmc_attrs[0],
563 static int rk29_sdmmc_progress_add_attr( struct platform_device *pdev )
566 struct kobject *parentkobject;
567 struct kobject * me = kmalloc(sizeof(struct kobject) , GFP_KERNEL );
572 memset(me ,0,sizeof(struct kobject));
573 kobject_init( me , &mmc_kset_ktype );
575 parentkobject = &pdev->dev.kobj ;
576 result = kobject_add( me , parentkobject->parent->parent->parent,"%s", "sd-sdio" );
582 #if defined (CONFIG_DEBUG_FS)
583 static int rk29_sdmmc_regs_show(struct seq_file *s, void *v)
585 struct rk29_sdmmc *host = s->private;
587 seq_printf(s, "SDMMC_CTRL: \t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_CTRL));
588 seq_printf(s, "SDMMC_PWREN: \t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_PWREN));
589 seq_printf(s, "SDMMC_CLKDIV: \t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_CLKDIV));
590 seq_printf(s, "SDMMC_CLKSRC: \t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_CLKSRC));
591 seq_printf(s, "SDMMC_CLKENA: \t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_CLKENA));
592 seq_printf(s, "SDMMC_TMOUT: \t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_TMOUT));
593 seq_printf(s, "SDMMC_CTYPE: \t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_CTYPE));
594 seq_printf(s, "SDMMC_BLKSIZ: \t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_BLKSIZ));
595 seq_printf(s, "SDMMC_BYTCNT: \t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_BYTCNT));
596 seq_printf(s, "SDMMC_INTMASK:\t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_INTMASK));
597 seq_printf(s, "SDMMC_CMDARG: \t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_CMDARG));
598 seq_printf(s, "SDMMC_CMD: \t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_CMD));
599 seq_printf(s, "SDMMC_RESP0: \t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_RESP0));
600 seq_printf(s, "SDMMC_RESP1: \t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_RESP1));
601 seq_printf(s, "SDMMC_RESP2: \t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_RESP2));
602 seq_printf(s, "SDMMC_RESP3: \t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_RESP3));
603 seq_printf(s, "SDMMC_MINTSTS:\t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_MINTSTS));
604 seq_printf(s, "SDMMC_RINTSTS:\t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_RINTSTS));
605 seq_printf(s, "SDMMC_STATUS: \t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_STATUS));
606 seq_printf(s, "SDMMC_FIFOTH: \t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_FIFOTH));
607 seq_printf(s, "SDMMC_CDETECT:\t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_CDETECT));
608 seq_printf(s, "SDMMC_WRTPRT: \t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_WRTPRT));
609 seq_printf(s, "SDMMC_TCBCNT: \t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_TCBCNT));
610 seq_printf(s, "SDMMC_TBBCNT: \t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_TBBCNT));
611 seq_printf(s, "SDMMC_DEBNCE: \t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_DEBNCE));
618 * The debugfs stuff below is mostly optimized away when
619 * CONFIG_DEBUG_FS is not set.
621 static int rk29_sdmmc_req_show(struct seq_file *s, void *v)
623 struct rk29_sdmmc *host = s->private;
624 struct mmc_request *mrq;
625 struct mmc_command *cmd;
626 struct mmc_command *stop;
627 struct mmc_data *data;
629 /* Make sure we get a consistent snapshot */
630 spin_lock(&host->lock);
641 "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n",
642 cmd->opcode, cmd->arg, cmd->flags,
643 cmd->resp[0], cmd->resp[1], cmd->resp[2],
644 cmd->resp[2], cmd->error);
646 seq_printf(s, "DATA %u / %u * %u flg %x err %d\n",
647 data->bytes_xfered, data->blocks,
648 data->blksz, data->flags, data->error);
651 "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n",
652 stop->opcode, stop->arg, stop->flags,
653 stop->resp[0], stop->resp[1], stop->resp[2],
654 stop->resp[2], stop->error);
657 spin_unlock(&host->lock);
662 static int rk29_sdmmc_req_open(struct inode *inode, struct file *file)
664 return single_open(file, rk29_sdmmc_req_show, inode->i_private);
667 static const struct file_operations rk29_sdmmc_req_fops = {
668 .owner = THIS_MODULE,
669 .open = rk29_sdmmc_req_open,
672 .release = single_release,
676 static int rk29_sdmmc_regs_open(struct inode *inode, struct file *file)
678 return single_open(file, rk29_sdmmc_regs_show, inode->i_private);
681 static const struct file_operations rk29_sdmmc_regs_fops = {
682 .owner = THIS_MODULE,
683 .open = rk29_sdmmc_regs_open,
686 .release = single_release,
689 static void rk29_sdmmc_init_debugfs(struct rk29_sdmmc *host)
691 struct mmc_host *mmc = host->mmc;
695 root = mmc->debugfs_root;
699 node = debugfs_create_file("regs", S_IRUSR, root, host,
700 &rk29_sdmmc_regs_fops);
706 node = debugfs_create_file("req", S_IRUSR, root, host, &rk29_sdmmc_req_fops);
710 node = debugfs_create_u32("state", S_IRUSR, root, (u32 *)&host->state);
714 node = debugfs_create_x32("pending_events", S_IRUSR, root,
715 (u32 *)&host->pending_events);
719 node = debugfs_create_x32("completed_events", S_IRUSR, root,
720 (u32 *)&host->completed_events);
727 dev_err(&mmc->class_dev, "failed to initialize debugfs for host\n");
731 static u32 rk29_sdmmc_prepare_command(struct mmc_command *cmd)
733 u32 cmdr = cmd->opcode;
737 case MMC_GO_IDLE_STATE:
738 cmdr |= (SDMMC_CMD_INIT | SDMMC_CMD_PRV_DAT_NO_WAIT);
741 case MMC_STOP_TRANSMISSION:
742 cmdr |= (SDMMC_CMD_STOP | SDMMC_CMD_PRV_DAT_NO_WAIT);
744 case MMC_SEND_STATUS:
745 case MMC_GO_INACTIVE_STATE:
746 cmdr |= SDMMC_CMD_PRV_DAT_NO_WAIT;
750 cmdr |= SDMMC_CMD_PRV_DAT_WAIT;
755 switch(mmc_resp_type(cmd))
759 // case MMC_RSP_R5: //R5,R6,R7 is same with the R1
763 cmdr |= (SDMMC_CMD_RESP_CRC | SDMMC_CMD_RESP_SHORT | SDMMC_CMD_RESP_EXP);
767 /* these response not contain crc7, so don't care crc error and response error */
768 cmdr |= (SDMMC_CMD_RESP_NO_CRC | SDMMC_CMD_RESP_SHORT | SDMMC_CMD_RESP_EXP);
771 cmdr |= (SDMMC_CMD_RESP_CRC | SDMMC_CMD_RESP_LONG | SDMMC_CMD_RESP_EXP);
774 cmdr |= (SDMMC_CMD_RESP_CRC_NOCARE | SDMMC_CMD_RESP_NOCARE | SDMMC_CMD_RESP_NO_EXP);
777 cmdr |= (SDMMC_CMD_RESP_CRC_NOCARE | SDMMC_CMD_RESP_NOCARE | SDMMC_CMD_RESP_NO_EXP);
784 void rk29_sdmmc_set_frq(struct rk29_sdmmc *host)
786 struct mmc_host *mmchost = platform_get_drvdata(host->pdev);
787 struct mmc_card *card;
789 unsigned int max_dtr;
791 extern void mmc_set_clock(struct mmc_host *host, unsigned int hz);
796 card = (struct mmc_card *)mmchost->card;
797 ios = ( struct mmc_ios *)&mmchost->ios;
802 if(MMC_POWER_ON == ios->power_mode)
805 max_dtr = (unsigned int)-1;
807 if (mmc_card_highspeed(card))
809 if (max_dtr > card->ext_csd.hs_max_dtr)
810 max_dtr = card->ext_csd.hs_max_dtr;
813 else if (max_dtr > card->csd.max_dtr)
815 if(MMC_TYPE_SD == card->type)
817 max_dtr = (card->csd.max_dtr > SD_FPP_FREQ) ? SD_FPP_FREQ : (card->csd.max_dtr);
821 max_dtr = (card->csd.max_dtr > MMC_FPP_FREQ) ? MMC_FPP_FREQ : (card->csd.max_dtr);
825 xbwprintk(7, "%s..%d... call mmc_set_clock() set clk=%d [%s]\n", \
826 __FUNCTION__, __LINE__, max_dtr, host->dma_name);
829 mmc_set_clock(mmchost, max_dtr);
834 static int rk29_sdmmc_start_command(struct rk29_sdmmc *host, struct mmc_command *cmd, u32 cmd_flags)
836 int tmo = RK29_SDMMC_SEND_START_TIMEOUT*10;//wait 60ms cycle.
839 host->old_cmd = cmd->opcode;
841 host->pending_events = 0;
842 host->completed_events = 0;
844 host->cmd_status = 0;
846 if(MMC_STOP_TRANSMISSION != cmd->opcode)
848 host->data_status = 0;
851 if(RK29_CTRL_SDMMC_ID == host->pdev->id)
853 //adjust the frequency division control of SDMMC0 every time.
854 rk29_sdmmc_set_frq(host);
857 rk29_sdmmc_write(host->regs, SDMMC_CMDARG, cmd->arg); // write to SDMMC_CMDARG register
858 #if defined(CONFIG_ARCH_RK29)
859 rk29_sdmmc_write(host->regs, SDMMC_CMD, cmd_flags | SDMMC_CMD_START); // write to SDMMC_CMD register
861 rk29_sdmmc_write(host->regs, SDMMC_CMD, cmd_flags | SDMMC_CMD_USE_HOLD_REG |SDMMC_CMD_START); // write to SDMMC_CMD register
864 xbwprintk(1,"\n%s..%d..************.start cmd=%d, arg=0x%x ******** [%s]\n", \
865 __FUNCTION__, __LINE__, cmd->opcode, cmd->arg, host->dma_name);
868 /* wait until CIU accepts the command */
869 while (--tmo && (rk29_sdmmc_read(host->regs, SDMMC_CMD) & SDMMC_CMD_START))
871 udelay(2);//cpu_relax();
874 host->complete_done = 0;
875 host->mmc->doneflag = 1;
881 printk(KERN_WARNING "%s..%d.. CMD_START timeout! CMD%d(arg=0x%x, retries=%d) [%s]\n",\
882 __FUNCTION__,__LINE__, cmd->opcode, cmd->arg, cmd->retries,host->dma_name);
885 cmd->error = -ETIMEDOUT;
886 host->mrq->cmd->error = -ETIMEDOUT;
887 del_timer_sync(&host->request_timer);
889 host->errorstep = 0x1;
890 return SDM_WAIT_FOR_CMDSTART_TIMEOUT;
892 host->errorstep = 0xfe;
897 static int rk29_sdmmc_reset_fifo(struct rk29_sdmmc *host)
901 int ret = SDM_SUCCESS;
903 value = rk29_sdmmc_read(host->regs, SDMMC_STATUS);
904 if (!(value & SDMMC_STAUTS_FIFO_EMPTY))
906 value = rk29_sdmmc_read(host->regs, SDMMC_CTRL);
907 value |= SDMMC_CTRL_FIFO_RESET;
908 rk29_sdmmc_write(host->regs, SDMMC_CTRL, value);
911 while (((value = rk29_sdmmc_read(host->regs, SDMMC_CTRL)) & (SDMMC_CTRL_FIFO_RESET)) && (timeout > 0))
918 host->errorstep = 0x2;
919 ret = SDM_WAIT_FOR_FIFORESET_TIMEOUT;
927 static int rk29_sdmmc_wait_unbusy(struct rk29_sdmmc *host)
929 int time_out = 500000;//250000; //max is 250ms; //adapt the value to the sick card. modify at 2011-10-08
931 #if SDMMC_USE_INT_UNBUSY
932 if((24==host->cmd->opcode)||(25==host->cmd->opcode))
935 while (rk29_sdmmc_read(host->regs, SDMMC_STATUS) & (SDMMC_STAUTS_DATA_BUSY|SDMMC_STAUTS_MC_BUSY))
942 host->errorstep = 0x3;
943 return SDM_BUSY_TIMEOUT;
950 static void rk29_sdmmc_dma_cleanup(struct rk29_sdmmc *host)
954 dma_unmap_sg(&host->pdev->dev, host->data->sg, host->data->sg_len,
955 ((host->data->flags & MMC_DATA_WRITE)
956 ? DMA_TO_DEVICE : DMA_FROM_DEVICE));
960 static void rk29_sdmmc_stop_dma(struct rk29_sdmmc *host)
964 if(host->use_dma == 0)
967 if (host->dma_info.chn> 0)
969 rk29_sdmmc_dma_cleanup(host);
971 ret = rk29_dma_ctrl(host->dma_info.chn,RK29_DMAOP_STOP);
974 printk(KERN_WARNING "%s..%d...rk29_dma_ctrl STOP error! [%s]\n", __FUNCTION__, __LINE__, host->dma_name);
975 host->errorstep = 0x95;
979 ret = rk29_dma_ctrl(host->dma_info.chn,RK29_DMAOP_FLUSH);
982 printk(KERN_WARNING "%s..%d...rk29_dma_ctrl FLUSH error! [%s]\n", __FUNCTION__, __LINE__, host->dma_name);
983 host->errorstep = 0x96;
990 /* Data transfer was stopped by the interrupt handler */
991 rk29_sdmmc_set_pending(host, EVENT_DATA_COMPLETE);
995 static void rk29_sdmmc_control_host_dma(struct rk29_sdmmc *host, bool enable)
997 u32 value = rk29_sdmmc_read(host->regs, SDMMC_CTRL);
1001 value |= SDMMC_CTRL_DMA_ENABLE;
1005 value &= ~(SDMMC_CTRL_DMA_ENABLE);
1008 rk29_sdmmc_write(host->regs, SDMMC_CTRL, value);
1011 static void send_stop_cmd(struct rk29_sdmmc *host)
1014 // int timeout = 250;
1015 // unsigned int value;
1018 if(host->mrq->cmd->error)
1023 rk29_sdmmc_stop_dma(host);
1024 rk29_sdmmc_control_host_dma(host, FALSE);
1029 ret= rk29_sdmmc_clear_fifo(host);
1030 if(SDM_SUCCESS != ret)
1032 xbwprintk(3, "%s..%d.. clear fifo error before call CMD_STOP [%s]\n", \
1033 __FUNCTION__, __LINE__, host->dma_name);
1040 ret = rk29_sdmmc_wait_unbusy(host);
1041 if(SDM_SUCCESS == ret)
1044 mod_timer(&host->request_timer, jiffies + msecs_to_jiffies(RK29_SDMMC_SEND_START_TIMEOUT+2500));
1045 xbwprintk(7, "%d..%s: retry times=%d before send_stop_cmd [%s].\n", __LINE__, __FUNCTION__, i, host->dma_name);
1050 host->errorstep = 0xe1;
1051 mod_timer(&host->request_timer, jiffies + msecs_to_jiffies(RK29_SDMMC_SEND_START_TIMEOUT+2500));
1053 host->stopcmd.opcode = MMC_STOP_TRANSMISSION;
1054 host->stopcmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;;
1055 host->stopcmd.arg = 0;
1056 host->stopcmd.data = NULL;
1057 host->stopcmd.mrq = NULL;
1058 host->stopcmd.retries = 0;
1059 host->stopcmd.error = 0;
1060 if(host->mrq && host->mrq->stop)
1062 host->mrq->stop->error = 0;
1065 host->cmdr = rk29_sdmmc_prepare_command(&host->stopcmd);
1067 ret = rk29_sdmmc_start_command(host, &host->stopcmd, host->cmdr);
1068 if(SDM_SUCCESS != ret)
1070 rk29_sdmmc_start_error(host);
1072 host->state = STATE_IDLE;
1073 host->complete_done = 4;
1075 host->errorstep = 0xe2;
1079 /* This function is called by the DMA driver from tasklet context. */
1080 static void rk29_sdmmc_dma_complete(void *arg, int size, enum rk29_dma_buffresult result)
1082 struct rk29_sdmmc *host = arg;
1084 if(host->use_dma == 0)
1087 host->intInfo.transLen = host->intInfo.desLen;
1090 static int rk29_sdmmc_read_data_pio(struct rk29_sdmmc *host)
1092 struct scatterlist *sg;
1094 unsigned int offset = 0;
1095 struct mmc_data *data;
1097 unsigned int nbytes = 0;
1100 value = rk29_sdmmc_read(host->regs, SDMMC_STATUS);
1101 if ( value& SDMMC_STAUTS_FIFO_EMPTY)
1104 if(NULL == host->data)
1107 if((NULL == host)&&(NULL == host->data))
1112 buf = (u32 *)sg_virt(sg);
1114 while ( (host->intInfo.transLen < host->intInfo.desLen) && (!(value & SDMMC_STAUTS_FIFO_EMPTY)) )
1116 if( ((offset + (host->intInfo.desLen - host->intInfo.transLen))<<2) <= sg->length )
1118 buf[offset] = rk29_sdmmc_read(host->regs, SDMMC_DATA);
1121 host->intInfo.transLen++;
1123 if ((offset<<2) == sg->length)
1125 flush_dcache_page(sg_page(sg));
1126 host->sg = sg = sg_next(sg);
1131 buf = (u32 *)sg_virt(sg);
1137 remaining = (sg->length>>2) - offset;
1140 buf[offset] = rk29_sdmmc_read(host->regs, SDMMC_DATA);
1144 host->intInfo.transLen++;
1147 flush_dcache_page(sg_page(sg));
1149 host->sg = sg = sg_next(sg);
1154 buf = (u32 *)sg_virt(sg);
1157 data->bytes_xfered += nbytes;
1159 value = rk29_sdmmc_read(host->regs, SDMMC_STATUS);
1167 static int rk29_sdmmc_write_data_pio(struct rk29_sdmmc *host)
1169 struct scatterlist *sg;
1171 unsigned int offset = 0;
1172 struct mmc_data *data;
1174 unsigned int nbytes = 0;
1177 value = rk29_sdmmc_read(host->regs, SDMMC_STATUS);
1178 if ( value& SDMMC_STAUTS_FIFO_EMPTY)
1181 if(NULL == host->data)
1184 if((NULL == host)&&(NULL == host->data))
1189 buf = (u32 *)sg_virt(sg);
1191 while ( (host->intInfo.transLen < host->intInfo.desLen) && (!(value & SDMMC_STAUTS_FIFO_EMPTY)) )
1193 if( ((offset + (host->intInfo.desLen - host->intInfo.transLen))<<2) <= sg->length )
1195 rk29_sdmmc_write(host->regs, SDMMC_DATA, buf[offset]);
1198 host->intInfo.transLen++;
1200 if ((offset<<2) == sg->length)
1202 host->sg = sg = sg_next(sg);
1207 buf = (u32 *)sg_virt(sg);
1213 remaining = (sg->length>>2) - offset;
1216 rk29_sdmmc_write(host->regs, SDMMC_DATA, buf[offset]);
1220 host->intInfo.transLen++;
1223 host->sg = sg = sg_next(sg);
1228 buf = (u32 *)sg_virt(sg);
1231 data->bytes_xfered += nbytes;
1233 value = rk29_sdmmc_read(host->regs, SDMMC_STATUS);
1240 static int rk29_sdmmc_submit_data_dma(struct rk29_sdmmc *host, struct mmc_data *data)
1242 unsigned int i,direction, sgDirection;
1245 if(host->use_dma == 0)
1247 printk(KERN_WARNING "%s..%d...setup DMA fail!!!!!!. host->use_dma=0 [%s]\n", __FUNCTION__, __LINE__, host->dma_name);
1248 host->errorstep = 0x4;
1251 /* If we don't have a channel, we can't do DMA */
1252 if (host->dma_info.chn < 0)
1254 printk(KERN_WARNING "%s..%d...setup DMA fail!!!!!!!. dma_info.chn < 0 [%s]\n", __FUNCTION__, __LINE__, host->dma_name);
1255 host->errorstep = 0x5;
1259 if (data->blksz & 3)
1261 printk(KERN_ERR "%s..%d...data_len not aligned to 4bytes. [%s]\n", \
1262 __FUNCTION__, __LINE__, host->dma_name);
1267 if (data->flags & MMC_DATA_READ)
1269 direction = RK29_DMASRC_HW;
1270 sgDirection = DMA_FROM_DEVICE;
1274 direction = RK29_DMASRC_MEM;
1275 sgDirection = DMA_TO_DEVICE;
1278 ret = rk29_dma_ctrl(host->dma_info.chn,RK29_DMAOP_STOP);
1281 printk(KERN_WARNING "%s..%d...rk29_dma_ctrl stop error![%s]\n", __FUNCTION__, __LINE__, host->dma_name);
1282 host->errorstep = 0x91;
1286 ret = rk29_dma_ctrl(host->dma_info.chn,RK29_DMAOP_FLUSH);
1289 printk(KERN_WARNING "%s..%d...rk29_dma_ctrl flush error![%s]\n", __FUNCTION__, __LINE__, host->dma_name);
1290 host->errorstep = 0x91;
1295 ret = rk29_dma_devconfig(host->dma_info.chn, direction, (unsigned long )(host->dma_addr));
1298 printk(KERN_WARNING "%s..%d...call rk29_dma_devconfig() fail ![%s]\n", __FUNCTION__, __LINE__, host->dma_name);
1299 host->errorstep = 0x8;
1303 dma_len = dma_map_sg(&host->pdev->dev, data->sg, data->sg_len, sgDirection);
1304 for (i = 0; i < dma_len; i++)
1306 ret = rk29_dma_enqueue(host->dma_info.chn, host, sg_dma_address(&data->sg[i]),sg_dma_len(&data->sg[i]));
1309 printk(KERN_WARNING "%s..%d...call rk29_dma_devconfig() fail ![%s]\n", __FUNCTION__, __LINE__, host->dma_name);
1310 host->errorstep = 0x93;
1315 rk29_sdmmc_control_host_dma(host, TRUE);// enable dma
1316 ret = rk29_dma_ctrl(host->dma_info.chn, RK29_DMAOP_START);
1319 printk(KERN_WARNING "%s..%d...rk29_dma_ctrl start error![%s]\n", __FUNCTION__, __LINE__, host->dma_name);
1320 host->errorstep = 0x94;
1328 static int rk29_sdmmc_prepare_write_data(struct rk29_sdmmc *host, struct mmc_data *data)
1333 u32 count, *pBuf = (u32 *)host->pbuf;
1335 output = SDM_SUCCESS;
1336 dataLen = data->blocks*data->blksz;
1338 host->dodma = 0; //DMA still no request;
1340 //SDMMC controller request the data is multiple of 4.
1341 count = (dataLen >> 2) + ((dataLen & 0x3) ? 1:0);
1343 if(count <= FIFO_DEPTH)
1345 #if defined(CONFIG_ARCH_RK29)
1346 if( (count <= 0x20) && (RK29_CTRL_SDMMC_ID != host->pdev->id))
1348 if( (count <= 0x80) && (RK29_CTRL_SDMMC_ID != host->pdev->id))
1354 for (i=0; i<count; i++)
1356 rk29_sdmmc_write(host->regs, SDMMC_DATA, pBuf[i]);
1359 rk29_sdmmc_write_data_pio(host);
1364 host->intInfo.desLen = count;
1365 host->intInfo.transLen = 0;
1366 host->intInfo.pBuf = (u32 *)pBuf;
1368 if(0)//(host->intInfo.desLen <= 512 )
1375 xbwprintk(7, "%s..%d... trace data, [%s]\n", __FUNCTION__, __LINE__, host->dma_name);
1376 output = rk29_sdmmc_submit_data_dma(host, data);
1381 printk(KERN_WARNING "%s..%d... CMD%d setupDMA failure!!!!! pre_cmd=%d [%s]\n", \
1382 __FUNCTION__, __LINE__, host->cmd->opcode,host->old_cmd, host->dma_name);
1384 host->errorstep = 0x81;
1386 rk29_sdmmc_control_host_dma(host, FALSE);
1402 static int rk29_sdmmc_prepare_read_data(struct rk29_sdmmc *host, struct mmc_data *data)
1408 output = SDM_SUCCESS;
1409 dataLen = data->blocks*data->blksz;
1411 host->dodma = 0;//DMA still no request;
1413 //SDMMC controller request the data is multiple of 4.
1414 count = (dataLen >> 2) ;//+ ((dataLen & 0x3) ? 1:0);
1416 host->intInfo.desLen = (dataLen >> 2);
1417 host->intInfo.transLen = 0;
1418 host->intInfo.pBuf = (u32 *)host->pbuf;
1420 if(count > (RX_WMARK+1)) //datasheet error.actually, it can nont waken the interrupt when less and equal than RX_WMARK+1
1422 if(0) //(host->intInfo.desLen <= 512 )
1429 output = rk29_sdmmc_submit_data_dma(host, data);
1434 printk(KERN_WARNING "%s..%d... CMD%d setupDMA failure!!! [%s]\n", \
1435 __FUNCTION__, __LINE__, host->cmd->opcode, host->dma_name);
1437 host->errorstep = 0x82;
1439 rk29_sdmmc_control_host_dma(host, FALSE);
1453 static int rk29_sdmmc_read_remain_data(struct rk29_sdmmc *host, u32 originalLen, void *pDataBuf)
1455 struct mmc_data *data;
1456 struct scatterlist *sg;
1459 if(1 == host->dodma)
1463 buf = (u32 *)sg_virt(sg);
1465 for_each_sg(data->sg, sg, data->sg_len, i)
1473 flush_dcache_page(sg_page(sg));
1477 rk29_sdmmc_read_data_pio(host);
1482 static void rk29_sdmmc_submit_data(struct rk29_sdmmc *host, struct mmc_data *data)
1485 struct scatterlist *sg;
1491 host->cmd->data = data;
1492 host->sg = data->sg;
1494 for_each_sg(data->sg, sg, data->sg_len, i)
1496 if (sg->offset & 3 || sg->length & 3)
1498 data->error = -EILSEQ;
1499 printk("%s..%d..CMD%d(arg=0x%x), data->blksz=%d, data->blocks=%d [%s]\n", \
1500 __FUNCTION__, __LINE__, host->cmd->opcode,\
1501 host->cmd->arg,data->blksz, data->blocks, host->dma_name);
1506 data->bytes_xfered = 0;
1507 host->pbuf = (u32*)sg_virt(data->sg);
1509 if (data->flags & MMC_DATA_STREAM)
1511 host->cmdr |= SDMMC_CMD_STRM_MODE; //set stream mode
1515 host->cmdr |= SDMMC_CMD_BLOCK_MODE; //set block mode
1518 //set the blocks and blocksize
1519 rk29_sdmmc_write(host->regs, SDMMC_BYTCNT,data->blksz*data->blocks);
1520 rk29_sdmmc_write(host->regs, SDMMC_BLKSIZ,data->blksz);
1522 xbwprintk(1, "%s..%d..CMD%d(arg=0x%x), data->blksz=%d, data->blocks=%d [%s]\n", \
1523 __FUNCTION__, __LINE__, host->cmd->opcode,host->cmd->arg,data->blksz, data->blocks, host->dma_name);
1525 if (data->flags & MMC_DATA_WRITE)
1527 host->cmdr |= (SDMMC_CMD_DAT_WRITE | SDMMC_CMD_DAT_EXP);
1529 xbwprintk(7,"%s..%d..CMD%d(arg=0x%x), data->blksz=%d, data->blocks=%d [%s]\n", \
1530 __FUNCTION__, __LINE__, host->cmd->opcode,host->cmd->arg,\
1531 data->blksz, data->blocks, host->dma_name);
1532 ret = rk29_sdmmc_prepare_write_data(host, data);
1536 host->cmdr |= (SDMMC_CMD_DAT_READ | SDMMC_CMD_DAT_EXP);
1537 xbwprintk(7, "%s..%d... read data len=%d [%s]\n", \
1538 __FUNCTION__, __LINE__, data->blksz*data->blocks, host->dma_name);
1540 ret = rk29_sdmmc_prepare_read_data(host, data);
1546 rk29_sdmmc_write(host->regs, SDMMC_BLKSIZ, 0);
1547 rk29_sdmmc_write(host->regs, SDMMC_BYTCNT, 0);
1552 static int sdmmc_send_cmd_start(struct rk29_sdmmc *host, unsigned int cmd)
1554 int tmo = RK29_SDMMC_SEND_START_TIMEOUT*10;//wait 60ms cycle.
1556 #if defined(CONFIG_ARCH_RK29)
1557 rk29_sdmmc_write(host->regs, SDMMC_CMD, SDMMC_CMD_START | cmd);
1559 rk29_sdmmc_write(host->regs, SDMMC_CMD, SDMMC_CMD_USE_HOLD_REG |SDMMC_CMD_START | cmd);
1561 while (--tmo && (rk29_sdmmc_read(host->regs, SDMMC_CMD) & SDMMC_CMD_START))
1566 if(!tmo && test_bit(RK29_SDMMC_CARD_PRESENT, &host->flags))
1568 printk(KERN_WARNING "%s.. %d set cmd(value=0x%x) register timeout error ! [%s]\n",\
1569 __FUNCTION__,__LINE__, cmd, host->dma_name);
1571 host->errorstep = 0x9;
1572 return SDM_START_CMD_FAIL;
1578 static int rk29_sdmmc_get_cd(struct mmc_host *mmc)
1580 struct rk29_sdmmc *host = mmc_priv(mmc);
1582 #if defined(CONFIG_SDMMC0_RK29_SDCARD_DET_FROM_GPIO)
1583 int i=0,cdetect1=0, cdetect2=0;
1585 switch(host->pdev->id)
1589 #if defined(CONFIG_SDMMC0_RK29_SDCARD_DET_FROM_GPIO)
1590 if(host->det_pin.io == INVALID_GPIO)
1596 cdetect1 = gpio_get_value(host->det_pin.io);
1598 cdetect2 = gpio_get_value(host->det_pin.io);
1599 if(cdetect1 == cdetect2)
1603 if(host->det_pin.enable)
1604 cdetect = cdetect?1:0;
1606 cdetect = cdetect?0:1;
1609 if(host->det_pin.io == INVALID_GPIO)
1612 cdetect = rk29_sdmmc_read(host->regs, SDMMC_CDETECT);
1614 cdetect = (cdetect & SDMMC_CARD_DETECT_N)?0:1;
1622 #if defined(CONFIG_USE_SDMMC1_FOR_WIFI_DEVELOP_BOARD)
1625 if(host->det_pin.io == INVALID_GPIO)
1628 cdetect = test_bit(RK29_SDMMC_CARD_PRESENT, &host->flags)?1:0;
1638 #if defined(CONFIG_USE_SDMMC0_FOR_WIFI_DEVELOP_BOARD)
1639 set_bit(RK29_SDMMC_CARD_PRESENT, &host->flags);
1647 /****************************************************************/
1648 //reset the SDMMC controller of the current host
1649 /****************************************************************/
1650 int rk29_sdmmc_reset_controller(struct rk29_sdmmc *host)
1655 rk29_sdmmc_write(host->regs, SDMMC_PWREN, POWER_ENABLE);
1658 for(timeOut=0; timeOut<FIFO_DEPTH; timeOut++)
1660 if(rk29_sdmmc_read(host->regs, SDMMC_STATUS) & SDMMC_STAUTS_FIFO_EMPTY)
1663 value = rk29_sdmmc_read(host->regs, SDMMC_DATA);
1667 #if defined(CONFIG_ARCH_RK29)
1668 rk29_sdmmc_write(host->regs, SDMMC_CTRL,(SDMMC_CTRL_RESET | SDMMC_CTRL_FIFO_RESET ));
1670 rk29_sdmmc_write(host->regs, SDMMC_CTRL,(SDMMC_CTRL_RESET | SDMMC_CTRL_FIFO_RESET | SDMMC_CTRL_DMA_RESET));
1673 value = rk29_sdmmc_read(host->regs, SDMMC_CTRL);
1674 while (( value & (SDMMC_CTRL_FIFO_RESET | SDMMC_CTRL_RESET)) && (timeOut > 0))
1678 value = rk29_sdmmc_read(host->regs, SDMMC_CTRL);
1683 printk(KERN_WARNING "%s..%s..%d.. reset controller fail! [%s]=\n",\
1684 __FILE__, __FUNCTION__,__LINE__, host->dma_name);
1686 host->errorstep = 0x0A;
1687 return SDM_WAIT_FOR_FIFORESET_TIMEOUT;
1690 /* FIFO threshold settings */
1691 rk29_sdmmc_write(host->regs, SDMMC_FIFOTH, (SD_MSIZE_16 | (RX_WMARK << RX_WMARK_SHIFT) | (TX_WMARK << TX_WMARK_SHIFT)));
1693 rk29_sdmmc_write(host->regs, SDMMC_CTYPE, SDMMC_CTYPE_1BIT);
1694 rk29_sdmmc_write(host->regs, SDMMC_CLKSRC, CLK_DIV_SRC_0);
1695 /* config debounce */
1696 host->bus_hz = clk_get_rate(host->clk);
1697 if((host->bus_hz > 52000000) || (host->bus_hz <= 0))
1699 printk(KERN_WARNING "%s..%s..%d..****Error!!!!!! Bus clock %d hz is beyond the prescribed limits. [%s]\n",\
1700 __FILE__, __FUNCTION__,__LINE__,host->bus_hz, host->dma_name);
1702 host->errorstep = 0x0B;
1703 return SDM_PARAM_ERROR;
1706 rk29_sdmmc_write(host->regs, SDMMC_DEBNCE, (DEBOUNCE_TIME*host->bus_hz)&0xFFFFFF);
1708 /* config interrupt */
1709 rk29_sdmmc_write(host->regs, SDMMC_RINTSTS, 0xFFFFFFFF);
1713 if(RK29_CTRL_SDMMC_ID == host->pdev->id)
1715 rk29_sdmmc_write(host->regs, SDMMC_INTMASK,RK29_SDMMC_INTMASK_USEDMA);
1719 if(0== host->pdev->id)
1721 #if !defined(CONFIG_USE_SDMMC0_FOR_WIFI_DEVELOP_BOARD)
1722 #if defined(CONFIG_RK29_SDIO_IRQ_FROM_GPIO)
1723 rk29_sdmmc_write(host->regs, SDMMC_INTMASK,RK29_SDMMC_INTMASK_USEDMA);
1725 rk29_sdmmc_write(host->regs, SDMMC_INTMASK,RK29_SDMMC_INTMASK_USEDMA | SDMMC_INT_SDIO);
1728 rk29_sdmmc_write(host->regs, SDMMC_INTMASK,RK29_SDMMC_INTMASK_USEDMA);
1731 else if(1== host->pdev->id)
1733 #if !defined(CONFIG_USE_SDMMC1_FOR_WIFI_DEVELOP_BOARD)
1734 #if defined(CONFIG_RK29_SDIO_IRQ_FROM_GPIO)
1735 rk29_sdmmc_write(host->regs, SDMMC_INTMASK,RK29_SDMMC_INTMASK_USEDMA);
1737 rk29_sdmmc_write(host->regs, SDMMC_INTMASK,RK29_SDMMC_INTMASK_USEDMA | SDMMC_INT_SDIO);
1740 rk29_sdmmc_write(host->regs, SDMMC_INTMASK,RK29_SDMMC_INTMASK_USEDMA);
1745 #if defined(CONFIG_RK29_SDIO_IRQ_FROM_GPIO)
1746 rk29_sdmmc_write(host->regs, SDMMC_INTMASK,RK29_SDMMC_INTMASK_USEDMA);
1748 rk29_sdmmc_write(host->regs, SDMMC_INTMASK,RK29_SDMMC_INTMASK_USEDMA | SDMMC_INT_SDIO);
1755 if(RK29_CTRL_SDMMC_ID == host->pdev->id)
1757 rk29_sdmmc_write(host->regs, SDMMC_INTMASK,RK29_SDMMC_INTMASK_USEIO);
1761 if(0== host->pdev->id)
1763 #if !defined(CONFIG_USE_SDMMC0_FOR_WIFI_DEVELOP_BOARD)
1764 #if defined(CONFIG_RK29_SDIO_IRQ_FROM_GPIO)
1765 rk29_sdmmc_write(host->regs, SDMMC_INTMASK,RK29_SDMMC_INTMASK_USEIO);
1767 rk29_sdmmc_write(host->regs, SDMMC_INTMASK,RK29_SDMMC_INTMASK_USEIO | SDMMC_INT_SDIO);
1770 rk29_sdmmc_write(host->regs, SDMMC_INTMASK,RK29_SDMMC_INTMASK_USEIO);
1773 else if(1== host->pdev->id)
1775 #if !defined(CONFIG_USE_SDMMC1_FOR_WIFI_DEVELOP_BOARD)
1776 #if defined(CONFIG_RK29_SDIO_IRQ_FROM_GPIO)
1777 rk29_sdmmc_write(host->regs, SDMMC_INTMASK,RK29_SDMMC_INTMASK_USEIO);
1779 rk29_sdmmc_write(host->regs, SDMMC_INTMASK,RK29_SDMMC_INTMASK_USEIO | SDMMC_INT_SDIO);
1782 rk29_sdmmc_write(host->regs, SDMMC_INTMASK,RK29_SDMMC_INTMASK_USEIO);
1787 #if defined(CONFIG_RK29_SDIO_IRQ_FROM_GPIO)
1788 rk29_sdmmc_write(host->regs, SDMMC_INTMASK,RK29_SDMMC_INTMASK_USEIO);
1790 rk29_sdmmc_write(host->regs, SDMMC_INTMASK,RK29_SDMMC_INTMASK_USEIO | SDMMC_INT_SDIO);
1796 rk29_sdmmc_write(host->regs, SDMMC_PWREN, POWER_ENABLE);
1798 rk29_sdmmc_write(host->regs, SDMMC_CTRL,SDMMC_CTRL_INT_ENABLE); // enable mci interrupt
1806 //enable/disnable the clk.
1807 static int rk29_sdmmc_control_clock(struct rk29_sdmmc *host, bool enable)
1811 int ret = SDM_SUCCESS;
1813 //wait previous start to clear
1815 while (--tmo && (rk29_sdmmc_read(host->regs, SDMMC_CMD) & SDMMC_CMD_START))
1817 udelay(1);//cpu_relax();
1821 host->errorstep = 0x0C;
1822 ret = SDM_START_CMD_FAIL;
1826 if(RK29_CTRL_SDMMC_ID == host->pdev->id)
1828 //SDMMC use low-power mode
1829 #if SDMMC_CLOCK_TEST
1832 value = (SDMMC_CLKEN_ENABLE);
1836 value = (SDMMC_CLKEN_DISABLE);
1843 value = (SDMMC_CLKEN_LOW_PWR | SDMMC_CLKEN_ENABLE);
1847 value = (SDMMC_CLKEN_LOW_PWR | SDMMC_CLKEN_DISABLE);
1854 //SDIO-card use non-low-power mode
1857 value = (SDMMC_CLKEN_ENABLE);
1861 value = (SDMMC_CLKEN_DISABLE);
1865 rk29_sdmmc_write(host->regs, SDMMC_CLKENA, value);
1868 ret = sdmmc_send_cmd_start(host, SDMMC_CMD_UPD_CLK | SDMMC_CMD_PRV_DAT_WAIT);
1869 if(ret != SDM_SUCCESS)
1877 printk(KERN_WARNING "\n%s....%d.. control clock fail!!! Enable=%d, ret=0x%x [%s]\n",\
1878 __FILE__,__LINE__,enable,ret, host->dma_name);
1885 //adjust the frequency.ie, to set the frequency division control
1886 int rk29_sdmmc_change_clk_div(struct rk29_sdmmc *host, u32 freqHz)
1890 int ret = SDM_SUCCESS;
1894 ret = SDM_PARAM_ERROR;
1898 ret = rk29_sdmmc_control_clock(host, FALSE);
1899 if (ret != SDM_SUCCESS)
1905 host->bus_hz = clk_get_rate(host->clk);
1906 if((host->bus_hz > 52000000) || (host->bus_hz <= 0))
1908 printk(KERN_WARNING "%s..%s..%d..****Error!!!!!! Bus clock %d hz is beyond the prescribed limits [%s]\n",\
1909 __FILE__, __FUNCTION__,__LINE__,host->bus_hz, host->dma_name);
1911 host->errorstep = 0x0D;
1912 ret = SDM_PARAM_ERROR;
1916 //calculate the divider
1917 div = host->bus_hz/freqHz + ((( host->bus_hz%freqHz ) > 0) ? 1:0 );
1918 if( (div & 0x01) && (1 != div) )
1920 //It is sure that the value of div is even.
1926 host->clock = host->bus_hz/div;
1930 host->clock = host->bus_hz;
1934 //wait previous start to clear
1936 while (--tmo && (rk29_sdmmc_read(host->regs, SDMMC_CMD) & SDMMC_CMD_START))
1938 udelay(1);//cpu_relax();
1942 host->errorstep = 0x0E;
1943 ret = SDM_START_CMD_FAIL;
1947 /* set clock to desired speed */
1948 rk29_sdmmc_write(host->regs, SDMMC_CLKDIV, div);
1951 ret = sdmmc_send_cmd_start(host, SDMMC_CMD_UPD_CLK | SDMMC_CMD_PRV_DAT_WAIT);
1952 if(ret != SDM_SUCCESS)
1954 host->errorstep = 0x0E1;
1958 if(host->old_div != div)
1960 printk(KERN_INFO "%s..%d.. newDiv=%u, newCLK=%uKhz [%s]\n", \
1961 __FUNCTION__, __LINE__,div, host->clock/1000, host->dma_name);
1964 ret = rk29_sdmmc_control_clock(host, TRUE);
1965 if(ret != SDM_SUCCESS)
1969 host->old_div = div;
1975 printk(KERN_WARNING "%s..%d.. change division fail, errorStep=0x%x,ret=%d [%s]\n",\
1976 __FILE__, __LINE__,host->errorstep,ret, host->dma_name);
1982 int rk29_sdmmc_hw_init(void *data)
1984 struct rk29_sdmmc *host = (struct rk29_sdmmc *)data;
1985 struct rk29_sdmmc_platform_data *pdata = host->pdev->dev.platform_data;
1988 host->ctype = SDMMC_CTYPE_1BIT;
1989 host->set_iomux(host->pdev->id, host->ctype);
1991 if( pdata && pdata->sd_vcc_reset )
1993 int cdetect = gpio_get_value(host->det_pin.io) ;
1994 if(host->det_pin.enable)
1996 cdetect = cdetect?1:0;
2000 cdetect = cdetect?0:1;
2005 pdata->sd_vcc_reset();
2009 /* reset controller */
2010 rk29_sdmmc_reset_controller(host);
2012 rk29_sdmmc_change_clk_div(host, FOD_FREQ);
2019 int rk29_sdmmc_set_buswidth(struct rk29_sdmmc *host)
2022 switch (host->ctype)
2024 case SDMMC_CTYPE_1BIT:
2025 case SDMMC_CTYPE_4BIT:
2027 case SDMMC_CTYPE_8BIT:
2028 return SDM_PARAM_ERROR; //Now, not support 8 bit width
2030 return SDM_PARAM_ERROR;
2033 host->set_iomux(host->pdev->id, host->ctype);
2035 /* Set the current bus width */
2036 rk29_sdmmc_write(host->regs, SDMMC_CTYPE, host->ctype);
2042 static void rk29_sdmmc_dealwith_timeout(struct rk29_sdmmc *host)
2044 if(0 == host->mmc->doneflag)
2045 return; //not to generate error flag if the command has been over.
2051 case STATE_SENDING_CMD:
2052 host->cmd_status |= SDMMC_INT_RTO;
2053 host->cmd->error = -ETIME;
2054 rk29_sdmmc_write(host->regs, SDMMC_RINTSTS,(SDMMC_INT_CMD_DONE | SDMMC_INT_RTO)); // clear interrupt
2055 rk29_sdmmc_set_pending(host, EVENT_CMD_COMPLETE);
2056 tasklet_schedule(&host->tasklet);
2058 case STATE_DATA_BUSY:
2059 host->data_status |= (SDMMC_INT_DCRC|SDMMC_INT_EBE);
2060 host->cmd->data->error = -EILSEQ;
2061 rk29_sdmmc_write(host->regs, SDMMC_RINTSTS,SDMMC_INT_DTO); // clear interrupt
2062 rk29_sdmmc_set_pending(host, EVENT_DATA_COMPLETE);
2063 #if SDMMC_USE_INT_UNBUSY
2064 rk29_sdmmc_set_pending(host, EVENT_DATA_UNBUSY);
2066 tasklet_schedule(&host->tasklet);
2068 #if SDMMC_USE_INT_UNBUSY
2069 case STATE_DATA_UNBUSY:
2070 host->data_status |= (SDMMC_INT_DCRC|SDMMC_INT_EBE);
2071 host->cmd->data->error = -EILSEQ;
2072 rk29_sdmmc_write(host->regs, SDMMC_RINTSTS,SDMMC_INT_UNBUSY); // clear interrupt
2073 rk29_sdmmc_set_pending(host, EVENT_DATA_UNBUSY);
2074 tasklet_schedule(&host->tasklet);
2077 case STATE_SENDING_STOP:
2078 host->cmd_status |= SDMMC_INT_RTO;
2079 host->cmd->error = -ETIME;
2080 rk29_sdmmc_write(host->regs, SDMMC_RINTSTS,(SDMMC_INT_CMD_DONE | SDMMC_INT_RTO)); // clear interrupt
2081 rk29_sdmmc_set_pending(host, EVENT_CMD_COMPLETE);
2082 tasklet_schedule(&host->tasklet);
2084 case STATE_DATA_END:
2092 static void rk29_sdmmc_INT_CMD_DONE_timeout(unsigned long host_data)
2094 struct rk29_sdmmc *host = (struct rk29_sdmmc *) host_data;
2096 rk29_sdmmc_enable_irq(host, false);
2098 if(STATE_SENDING_CMD == host->state)
2100 if((0==host->cmd->retries)&&(12 != host->cmd->opcode))
2102 printk(KERN_WARNING "%d... cmd=%d(arg=0x%x), INT_CMD_DONE timeout, errorStep=0x%x, host->state=%x [%s]\n",\
2103 __LINE__,host->cmd->opcode, host->cmd->arg,host->errorstep,host->state,host->dma_name);
2105 if(++host->timeout_times >= 3)
2107 printk(KERN_WARNING "I am very sorry to tell you,in order to make the machine correctly,you must remove-insert card again.");
2108 host->mmc->re_initialized_flags = 0;
2109 host->timeout_times = 0;
2112 rk29_sdmmc_write(host->regs, SDMMC_RINTSTS, 0xFFFFFFFE); // clear INT,except for SDMMC_INT_CD
2113 rk29_sdmmc_dealwith_timeout(host);
2116 rk29_sdmmc_enable_irq(host, true);
2120 static void rk29_sdmmc_INT_DTO_timeout(unsigned long host_data)
2122 struct rk29_sdmmc *host = (struct rk29_sdmmc *) host_data;
2124 rk29_sdmmc_enable_irq(host, false);
2126 #if SDMMC_USE_INT_UNBUSY
2127 if( (host->cmdr & SDMMC_CMD_DAT_EXP) &&((STATE_DATA_BUSY == host->state)||(STATE_DATA_UNBUSY == host->state) ))
2129 if( (host->cmdr & SDMMC_CMD_DAT_EXP) && (STATE_DATA_BUSY == host->state))
2132 if(0==host->cmd->retries)
2134 printk(KERN_WARNING "%s..%d...cmd=%d DTO_timeout,cmdr=0x%x, errorStep=0x%x, Hoststate=%x [%s]\n", \
2135 __FUNCTION__, __LINE__,host->cmd->opcode,host->cmdr ,host->errorstep,host->state,host->dma_name);
2137 if(++host->timeout_times >= 3)
2139 printk(KERN_WARNING "I am very sorry to tell you,in order to make the machine correctly,you must remove-insert card again.");
2140 host->mmc->re_initialized_flags = 0;
2141 host->timeout_times = 0;
2144 rk29_sdmmc_write(host->regs, SDMMC_RINTSTS, 0xFFFFFFFE); // clear INT,except for SDMMC_INT_CD
2145 rk29_sdmmc_dealwith_timeout(host);
2147 rk29_sdmmc_enable_irq(host, true);
2152 //to excute a request
2153 static int rk29_sdmmc_start_request(struct mmc_host *mmc )
2155 struct rk29_sdmmc *host = mmc_priv(mmc);
2156 struct mmc_request *mrq;
2157 struct mmc_command *cmd;
2160 unsigned long iflags;
2162 spin_lock_irqsave(&host->lock, iflags);
2164 mrq = host->new_mrq;
2168 cmdr = rk29_sdmmc_prepare_command(cmd);
2172 /*clean FIFO if it is a new request*/
2173 if((RK29_CTRL_SDMMC_ID == host->pdev->id) && ( !(cmdr & SDMMC_CMD_STOP)))
2175 ret = rk29_sdmmc_reset_fifo(host);
2176 if(SDM_SUCCESS != ret)
2178 host->mrq = host->new_mrq;///
2179 cmd->error = -ENOMEDIUM;
2180 host->errorstep = 0x0F;
2182 goto start_request_Err;
2186 //check data-busy if the current command has the bit13 in command register.
2187 if( cmdr & SDMMC_CMD_PRV_DAT_WAIT )
2189 if(SDM_SUCCESS != rk29_sdmmc_wait_unbusy(host)) //if(rk29_sdmmc_read(host->regs, SDMMC_STATUS) & SDMMC_STAUTS_DATA_BUSY)
2191 host->mrq = host->new_mrq;///
2192 cmd->error = -ETIMEDOUT;
2193 ret = SDM_BUSY_TIMEOUT;
2194 host->errorstep = 0x10;
2195 if(0 == cmd->retries)
2197 printk(KERN_WARNING "%s..Error happen in CMD_PRV_DAT_WAIT. STATUS-reg=0x%x [%s]\n", \
2198 __FUNCTION__, rk29_sdmmc_read(host->regs, SDMMC_STATUS),host->dma_name);
2200 rk29_sdmmc_clear_fifo(host);
2201 goto start_request_Err;
2205 host->state = STATE_SENDING_CMD;
2206 host->mrq = host->new_mrq;
2214 host->data_status = 0;
2217 host->errorstep = 0;
2222 //setting for the data
2223 rk29_sdmmc_submit_data(host, mrq->data);
2224 host->errorstep = 0xff;
2226 xbwprintk(7, "%s..%d... CMD%d begin to call rk29_sdmmc_start_command(). [%s]\n", \
2227 __FUNCTION__, __LINE__ , cmd->opcode,host->dma_name);
2229 if(RK29_CTRL_SDMMC_ID == host->pdev->id)
2231 mod_timer(&host->request_timer, jiffies + msecs_to_jiffies(RK29_SDMMC_SEND_START_TIMEOUT+700));
2235 mod_timer(&host->request_timer, jiffies + msecs_to_jiffies(RK29_SDMMC_SEND_START_TIMEOUT+500));
2239 ret = rk29_sdmmc_start_command(host, cmd, host->cmdr);
2240 if(SDM_SUCCESS != ret)
2242 cmd->error = -ETIMEDOUT;
2245 printk(KERN_WARNING "%s..%d... start_command(CMD%d, arg=%x, retries=%d) fail! ret=%d . [%s]\n",\
2246 __FUNCTION__, __LINE__ , cmd->opcode,cmd->arg, cmd->retries,ret, host->dma_name);
2248 host->errorstep = 0x11;
2249 del_timer_sync(&host->request_timer);
2251 goto start_request_Err;
2253 host->errorstep = 0xfd;
2255 xbwprintk(7, "%s..%d... CMD=%d, wait for INT_CMD_DONE, ret=%d , \n \
2256 host->state=0x%x, cmdINT=0x%x \n host->pendingEvent=0x%lu, host->completeEvents=0x%lu [%s]\n\n",\
2257 __FUNCTION__, __LINE__, host->cmd->opcode,ret, \
2258 host->state,host->cmd_status, host->pending_events,host->completed_events,host->dma_name);
2260 spin_unlock_irqrestore(&host->lock, iflags);
2265 rk29_sdmmc_start_error(host);
2267 if(0 == cmd->retries)
2269 printk(KERN_WARNING "%s: CMD%d(arg=%x) fail to start request. err=%d, Errorstep=0x%x [%s]\n",\
2270 __FUNCTION__, cmd->opcode, cmd->arg,ret,host->errorstep,host->dma_name);
2273 host->state = STATE_IDLE; //modifyed by xbw at 2011-08-15
2275 if(host->mrq && host->mmc->doneflag && host->complete_done)
2277 host->mmc->doneflag = 0;
2278 host->complete_done = 0;
2279 spin_unlock_irqrestore(&host->lock, iflags);
2281 mmc_request_done(host->mmc, host->mrq);
2285 spin_unlock_irqrestore(&host->lock, iflags);
2293 static void rk29_sdmmc_request(struct mmc_host *mmc, struct mmc_request *mrq)
2295 unsigned long iflags;
2297 struct rk29_sdmmc *host = mmc_priv(mmc);
2299 spin_lock_irqsave(&host->lock, iflags);
2302 //set 1 to close the controller for Debug.
2303 if(RK29_CTRL_SDIO1_ID==host->pdev->id)//if(RK29_CTRL_SDMMC_ID==host->pdev->id)//
2305 mrq->cmd->error = -ENOMEDIUM;
2306 printk(KERN_WARNING "%s..%d.. ==== The %s had been closed by myself for the experiment. [%s]\n",\
2307 __FUNCTION__, __LINE__, host->dma_name, host->dma_name);
2309 host->state = STATE_IDLE;
2310 rk29_sdmmc_write(host->regs, SDMMC_RINTSTS, 0xFFFFFFFF);
2311 spin_unlock_irqrestore(&host->lock, iflags);
2312 mmc_request_done(mmc, mrq);
2320 ret = rk29_sdmmc_wait_unbusy(host);
2321 if(SDM_SUCCESS == ret)
2325 xbwprintk(6, "\n%s..%d..New cmd=%2d(arg=0x%8x)=== cardPresent=0x%lu, state=0x%x [%s]\n", \
2326 __FUNCTION__, __LINE__,mrq->cmd->opcode, mrq->cmd->arg,host->flags,host->state, host->dma_name);
2328 if(RK29_CTRL_SDMMC_ID == host->pdev->id)
2330 if(!rk29_sdmmc_get_cd(mmc) || ((0==mmc->re_initialized_flags)&&(MMC_GO_IDLE_STATE != mrq->cmd->opcode)))
2332 mrq->cmd->error = -ENOMEDIUM;
2334 if((RK29_CTRL_SDMMC_ID == host->pdev->id)&&(0==mrq->cmd->retries))
2336 if(host->old_cmd != mrq->cmd->opcode)
2338 if( ((17==host->old_cmd)&&(18==mrq->cmd->opcode)) || ((18==host->old_cmd)&&(17==mrq->cmd->opcode)) ||\
2339 ((24==host->old_cmd)&&(25==mrq->cmd->opcode)) || ((25==host->old_cmd)&&(24==mrq->cmd->opcode)))
2341 host->old_cmd = mrq->cmd->opcode;
2342 if(host->error_times++ % (RK29_ERROR_PRINTK_INTERVAL*100) ==0)
2344 printk(KERN_INFO "%s: Refuse to run CMD%2d(arg=0x%8x) due to the removal of card. 1==[%s]==\n", \
2345 __FUNCTION__, mrq->cmd->opcode, mrq->cmd->arg, host->dma_name);
2350 host->old_cmd = mrq->cmd->opcode;
2351 host->error_times = 0;
2352 printk(KERN_INFO "%s: Refuse to run CMD%2d(arg=0x%8x) due to the removal of card. 2==[%s]==\n", \
2353 __FUNCTION__, mrq->cmd->opcode, mrq->cmd->arg, host->dma_name);
2358 if(host->error_times++ % (RK29_ERROR_PRINTK_INTERVAL*100) ==0)
2360 printk(KERN_INFO "%s: Refuse to run CMD%2d(arg=0x%8x) due to the removal of card. 3==[%s]==\n", \
2361 __FUNCTION__, mrq->cmd->opcode, mrq->cmd->arg, host->dma_name);
2363 host->old_cmd = mrq->cmd->opcode;
2366 host->state = STATE_IDLE;
2367 spin_unlock_irqrestore(&host->lock, iflags);
2369 mmc_request_done(mmc, mrq);
2374 if(host->old_cmd != mrq->cmd->opcode)
2376 host->old_cmd = mrq->cmd->opcode;
2377 host->error_times = 0;
2383 host->old_cmd = mrq->cmd->opcode;
2384 host->error_times = 0;
2386 if(!test_bit(RK29_SDMMC_CARD_PRESENT, &host->flags))
2388 host->state = STATE_IDLE;
2389 mrq->cmd->error = -ENOMEDIUM;
2390 spin_unlock_irqrestore(&host->lock, iflags);
2392 mmc_request_done(mmc, mrq);
2398 host->new_mrq = mrq;
2400 spin_unlock_irqrestore(&host->lock, iflags);
2402 rk29_sdmmc_start_request(mmc);
2406 extern void rk29_sdmmc_gpio_open(int device_id, int on);
2407 static void rk29_sdmmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
2411 struct rk29_sdmmc *host = mmc_priv(mmc);
2413 rk29_sdmmc_enable_irq(host, false);
2415 //if(host->bus_mode != ios->power_mode)
2417 switch (ios->power_mode)
2420 rk29_sdmmc_write(host->regs, SDMMC_PWREN, POWER_ENABLE);
2422 //reset the controller if it is SDMMC0
2423 if(RK29_CTRL_SDMMC_ID == host->pdev->id)
2425 xbwprintk(7, "%s..%d..POWER_UP, call reset_controller, initialized_flags=%d [%s]\n",\
2426 __FUNCTION__, __LINE__, host->mmc->re_initialized_flags,host->dma_name);
2429 gpio_direction_output(host->gpio_power_en, host->gpio_power_en_level);
2433 rk29_sdmmc_hw_init(host);
2439 if(RK29_CTRL_SDMMC_ID == host->pdev->id)
2442 rk29_sdmmc_control_clock(host, FALSE);
2443 rk29_sdmmc_write(host->regs, SDMMC_PWREN, POWER_DISABLE);
2445 if(5 == host->bus_mode)
2448 xbwprintk(7, "%s..%d..Fisrt powerOFF, call reset_controller [%s]\n", \
2449 __FUNCTION__, __LINE__,host->dma_name);
2451 rk29_sdmmc_reset_controller(host);
2455 rk29_sdmmc_gpio_open(0, 0);
2457 gpio_direction_output(host->gpio_power_en, !(host->gpio_power_en_level));
2466 host->bus_mode = ios->power_mode;
2470 if(test_bit(RK29_SDMMC_CARD_PRESENT, &host->flags) || (RK29_CTRL_SDMMC_ID == host->pdev->id))
2473 * Waiting SDIO controller to be IDLE.
2475 while (timeout-- > 0)
2477 value = rk29_sdmmc_read(host->regs, SDMMC_STATUS);
2478 if ((value & SDMMC_STAUTS_DATA_BUSY) == 0 &&(value & SDMMC_CMD_FSM_MASK) == SDMMC_CMD_FSM_IDLE)
2487 printk(KERN_WARNING "%s..%d...Waiting for SDMMC%d controller to be IDLE timeout.[%s]\n", \
2488 __FUNCTION__, __LINE__, host->pdev->id, host->dma_name);
2495 if((!(test_bit(RK29_SDMMC_CARD_PRESENT, &host->flags))|| !rk29_sdmmc_get_cd(host->mmc))
2496 &&(RK29_CTRL_SDIO1_ID != host->pdev->id))
2497 goto out; //exit the set_ios directly if the SDIO is not present.
2499 if(host->ctype != ios->bus_width)
2501 switch (ios->bus_width)
2503 case MMC_BUS_WIDTH_1:
2504 host->ctype = SDMMC_CTYPE_1BIT;
2506 case MMC_BUS_WIDTH_4:
2507 host->ctype = SDMMC_CTYPE_4BIT;
2509 case MMC_BUS_WIDTH_8:
2510 host->ctype = SDMMC_CTYPE_8BIT;
2517 rk29_sdmmc_set_buswidth(host);
2521 if (ios->clock && (ios->clock != host->clock))
2524 * Use mirror of ios->clock to prevent race with mmc
2525 * core ios update when finding the minimum.
2527 //host->clock = ios->clock;
2528 rk29_sdmmc_change_clk_div(host, ios->clock);
2531 rk29_sdmmc_enable_irq(host, true);
2535 static int rk29_sdmmc_get_ro(struct mmc_host *mmc)
2537 struct rk29_sdmmc *host = mmc_priv(mmc);
2540 switch(host->pdev->id)
2544 #if defined(CONFIG_SDMMC0_RK29_WRITE_PROTECT) || defined(CONFIG_SDMMC1_RK29_WRITE_PROTECT)
2545 if(INVALID_GPIO == host->write_protect)
2546 ret = 0;//no write-protect
2548 ret = (host->protect_level == gpio_get_value(host->write_protect)?1:0;
2550 xbwprintk(7,"%s..%d.. write_prt_pin=%d, get_ro=%d. [%s]\n",\
2551 __FUNCTION__, __LINE__,host->write_protect, ret, host->dma_name);
2554 u32 wrtprt = rk29_sdmmc_read(host->regs, SDMMC_WRTPRT);
2556 ret = (wrtprt & SDMMC_WRITE_PROTECT)?1:0;
2563 ret = 0;//no write-protect
2575 #if defined(CONFIG_RK29_SDIO_IRQ_FROM_GPIO)
2576 static irqreturn_t rk29_sdmmc_sdio_irq_cb(int irq, void *dev_id)
2578 struct rk29_sdmmc *host = dev_id;
2580 if(host && host->mmc)
2581 mmc_signal_sdio_irq(host->mmc);
2588 static void rk29_sdmmc_enable_sdio_irq(struct mmc_host *mmc, int enable)
2590 #if !defined(CONFIG_RK29_SDIO_IRQ_FROM_GPIO)
2592 unsigned long flags;
2594 struct rk29_sdmmc *host = mmc_priv(mmc);
2596 #if defined(CONFIG_RK29_SDIO_IRQ_FROM_GPIO)
2599 enable_irq(host->sdio_irq);
2601 #if !defined(CONFIG_MTK_COMBO_DRIVER_VERSION_JB2)
2602 enable_irq_wake(host->sdio_irq);
2607 disable_irq_nosync(host->sdio_irq);
2609 #if !defined(CONFIG_MTK_COMBO_DRIVER_VERSION_JB2)
2610 disable_irq_wake(host->sdio_irq);
2615 spin_lock_irqsave(&host->lock, flags);
2617 intmask = rk29_sdmmc_read(host->regs, SDMMC_INTMASK);
2619 rk29_sdmmc_write(host->regs, SDMMC_INTMASK, intmask | SDMMC_INT_SDIO);
2621 rk29_sdmmc_write(host->regs, SDMMC_INTMASK, intmask & ~SDMMC_INT_SDIO);
2623 spin_unlock_irqrestore(&host->lock, flags);
2629 static void rk29_sdmmc_init_card(struct mmc_host *mmc, struct mmc_card *card)
2631 card->quirks = MMC_QUIRK_BLKSZ_FOR_BYTE_MODE;
2635 static int rk29_sdmmc_clear_fifo(struct rk29_sdmmc *host)
2637 unsigned int timeout, value;
2638 int ret = SDM_SUCCESS;
2640 if(RK29_CTRL_SDMMC_ID == host->pdev->id)
2642 rk29_sdmmc_write(host->regs, SDMMC_RINTSTS, 0xFFFFFFFF);
2645 rk29_sdmmc_stop_dma(host);
2646 rk29_sdmmc_control_host_dma(host, FALSE);
2650 for(timeout=0; timeout<FIFO_DEPTH; timeout++)
2652 if(rk29_sdmmc_read(host->regs, SDMMC_STATUS) & SDMMC_STAUTS_FIFO_EMPTY)
2655 value = rk29_sdmmc_read(host->regs, SDMMC_DATA);
2660 value = rk29_sdmmc_read(host->regs, SDMMC_CTRL);
2661 value |= (SDMMC_CTRL_RESET | SDMMC_CTRL_FIFO_RESET | SDMMC_CTRL_DMA_RESET);
2662 rk29_sdmmc_write(host->regs, SDMMC_CTRL, value);
2664 value = rk29_sdmmc_read(host->regs, SDMMC_CTRL);
2666 while( (value & (SDMMC_CTRL_FIFO_RESET | SDMMC_CTRL_RESET | SDMMC_CTRL_DMA_RESET)) && (timeout > 0))
2670 value = rk29_sdmmc_read(host->regs, SDMMC_CTRL);
2675 host->errorstep = 0x0A;
2676 ret = SDM_WAIT_FOR_FIFORESET_TIMEOUT;
2684 static const struct mmc_host_ops rk29_sdmmc_ops[] = {
2686 .request = rk29_sdmmc_request,
2687 .set_ios = rk29_sdmmc_set_ios,
2688 .get_ro = rk29_sdmmc_get_ro,
2689 .get_cd = rk29_sdmmc_get_cd,
2692 .request = rk29_sdmmc_request,
2693 .set_ios = rk29_sdmmc_set_ios,
2694 .get_ro = rk29_sdmmc_get_ro,
2695 .get_cd = rk29_sdmmc_get_cd,
2696 .enable_sdio_irq = rk29_sdmmc_enable_sdio_irq,
2697 .init_card = rk29_sdmmc_init_card,
2701 static void rk29_sdmmc_request_end(struct rk29_sdmmc *host, struct mmc_command *cmd)
2703 u32 status = host->data_status;
2704 int output=SDM_SUCCESS;
2706 xbwprintk(7, "%s..%d... cmd=%d, host->state=0x%x,\n pendingEvent=0x%lu, completeEvents=0x%lu [%s]\n",\
2707 __FUNCTION__, __LINE__,cmd->opcode,host->state, host->pending_events,host->completed_events,host->dma_name);
2709 del_timer_sync(&host->DTO_timer);
2711 if(RK29_CTRL_SDMMC_ID == host->pdev->id)
2713 rk29_sdmmc_write(host->regs, SDMMC_RINTSTS, 0xFFFFFFFF); //added by xbw at 2011-08-15
2719 rk29_sdmmc_stop_dma(host);
2720 rk29_sdmmc_control_host_dma(host, FALSE);
2726 goto exit;//It need not to wait-for-busy if the CMD-ERROR happen.
2728 host->errorstep = 0xf7;
2731 if(host->cmdr & SDMMC_CMD_DAT_WRITE)
2733 if(status & (SDMMC_INT_DCRC | SDMMC_INT_EBE))
2735 cmd->data->error = -EILSEQ;
2736 output = SDM_DATA_CRC_ERROR;
2737 host->errorstep = 0x16;
2741 output = rk29_sdmmc_wait_unbusy(host);
2742 if(SDM_SUCCESS != output)
2744 host->errorstep = 0x17;
2745 cmd->data->error = -ETIMEDOUT;
2748 host->data->bytes_xfered = host->data->blocks * host->data->blksz;
2753 if( status & SDMMC_INT_SBE)
2755 cmd->data->error = -EIO;
2756 host->errorstep = 0x18;
2757 output = SDM_START_BIT_ERROR;
2759 else if((status & SDMMC_INT_EBE) && (cmd->opcode != 14)) //MMC4.0, BUSTEST_R, A host read the reserved bus testing data parttern from a card.
2761 cmd->data->error = -EILSEQ;
2762 host->errorstep = 0x19;
2763 output = SDM_END_BIT_ERROR;
2765 else if(status & SDMMC_INT_DRTO)
2767 cmd->data->error = -ETIMEDOUT;
2768 host->errorstep = 0x1A;
2769 output = SDM_DATA_READ_TIMEOUT;
2771 else if(status & SDMMC_INT_DCRC)
2773 host->errorstep = 0x1B;
2774 cmd->data->error = -EILSEQ;
2775 output = SDM_DATA_CRC_ERROR;
2779 output = rk29_sdmmc_read_remain_data(host, (host->data->blocks * host->data->blksz), host->pbuf);
2780 if(SDM_SUCCESS == output)
2782 host->data->bytes_xfered = host->data->blocks * host->data->blksz;
2788 if(SDM_SUCCESS == output)
2790 if ((mmc_resp_type(cmd) == MMC_RSP_R1B) || (MMC_STOP_TRANSMISSION == cmd->opcode))
2792 output = rk29_sdmmc_wait_unbusy(host);
2793 if((SDM_SUCCESS != output) && (!host->mrq->cmd->error))
2795 printk(KERN_WARNING "%s..%d... CMD12 wait busy timeout!!!!! errorStep=0x%x [%s]\n", \
2796 __FUNCTION__, __LINE__, host->errorstep, host->dma_name);
2797 rk29_sdmmc_clear_fifo(host);
2798 cmd->error = -ETIMEDOUT;
2799 host->mrq->cmd->error = -ETIMEDOUT;
2800 host->errorstep = 0x1C;
2804 host->errorstep = 0xf6;
2807 if(cmd->data && cmd->data->error)
2809 if( (!cmd->error) && (0==cmd->retries))
2811 printk(KERN_WARNING "%s..%d......CMD=%d error!!!(arg=0x%x,cmdretry=%d,blksize=%d, blocks=%d), \n \
2812 statusReg=0x%x, ctrlReg=0x%x, nerrorTimes=%d, errorStep=0x%x. [%s]\n",\
2813 __FUNCTION__, __LINE__, cmd->opcode, cmd->arg, cmd->retries,cmd->data->blksz, cmd->data->blocks,
2814 rk29_sdmmc_read(host->regs, SDMMC_STATUS),
2815 rk29_sdmmc_read(host->regs, SDMMC_CTRL),
2816 host->error_times,host->errorstep, host->dma_name);
2818 cmd->error = -ENODATA;
2820 host->errorstep = 0xf5;
2824 #ifdef RK29_SDMMC_LIST_QUEUE
2825 if (!list_empty(&host->queue))
2827 printk(KERN_WARNING "%s..%d.. Danger!Danger!. continue the next request in the queue. [%s]\n",\
2828 __FUNCTION__, __LINE__, host->dma_name);
2830 host = list_entry(host->queue.next,
2831 struct rk29_sdmmc, queue_node);
2832 list_del(&host->queue_node);
2833 host->state = STATE_SENDING_CMD;
2834 rk29_sdmmc_start_request(host->mmc);
2838 dev_vdbg(&host->pdev->dev, "list empty\n");
2839 host->state = STATE_IDLE;
2842 dev_vdbg(&host->pdev->dev, "list empty\n");
2843 host->state = STATE_IDLE;
2848 static int rk29_sdmmc_command_complete(struct rk29_sdmmc *host,
2849 struct mmc_command *cmd)
2851 u32 value, status = host->cmd_status;
2852 int timeout, output= SDM_SUCCESS;
2854 xbwprintk(7, "%s..%d. cmd=%d, host->state=0x%x, cmdINT=0x%x\n,pendingEvent=0x%lu,completeEvents=0x%lu. [%s]\n",\
2855 __FUNCTION__, __LINE__,cmd->opcode,host->state,status, host->pending_events,host->completed_events,host->dma_name);
2858 del_timer_sync(&host->request_timer);
2860 host->cmd_status = 0;
2862 if((RK29_CTRL_SDMMC_ID == host->pdev->id) && (host->cmdr & SDMMC_CMD_STOP))
2864 output = rk29_sdmmc_reset_fifo(host);
2865 if (SDM_SUCCESS != output)
2867 printk(KERN_WARNING "%s..%d......reset fifo fail! CMD%d(arg=0x%x, Retries=%d) [%s]\n",__FUNCTION__, __LINE__, \
2868 cmd->opcode, cmd->arg, cmd->retries,host->dma_name);
2870 cmd->error = -ETIMEDOUT;
2871 host->mrq->cmd->error = cmd->error;
2873 host->errorstep = 0x1C;
2878 if(status & SDMMC_INT_RTO)
2880 cmd->error = -ENOMEM;
2881 host->mrq->cmd->error = cmd->error;
2882 output = SDM_BUSY_TIMEOUT;
2883 host->errorstep = 0x1E;
2885 //rk29_sdmmc_write(host->regs, SDMMC_RINTSTS,SDMMC_INT_RTO);
2886 rk29_sdmmc_write(host->regs, SDMMC_RINTSTS,0xFFFFFFFF); //modifyed by xbw at 2011-08-15
2888 if(host->use_dma)//if(host->dodma)
2892 rk29_sdmmc_stop_dma(host);
2893 rk29_sdmmc_control_host_dma(host, FALSE);
2897 value = rk29_sdmmc_read(host->regs, SDMMC_CTRL);
2898 value |= SDMMC_CTRL_FIFO_RESET;
2899 rk29_sdmmc_write(host->regs, SDMMC_CTRL, value);
2902 while (((value = rk29_sdmmc_read(host->regs, SDMMC_CTRL)) & (SDMMC_CTRL_FIFO_RESET)) && (timeout > 0))
2910 host->errorstep = 0x1D;
2911 printk(KERN_WARNING "%s..%d......reset CTRL fail! CMD%d(arg=0x%x, Retries=%d).[%s]\n",\
2912 __FUNCTION__, __LINE__, cmd->opcode, cmd->arg, cmd->retries,host->dma_name);
2920 if(cmd->flags & MMC_RSP_PRESENT)
2922 if(cmd->flags & MMC_RSP_136)
2924 cmd->resp[3] = rk29_sdmmc_read(host->regs, SDMMC_RESP0);
2925 cmd->resp[2] = rk29_sdmmc_read(host->regs, SDMMC_RESP1);
2926 cmd->resp[1] = rk29_sdmmc_read(host->regs, SDMMC_RESP2);
2927 cmd->resp[0] = rk29_sdmmc_read(host->regs, SDMMC_RESP3);
2931 cmd->resp[0] = rk29_sdmmc_read(host->regs, SDMMC_RESP0);
2938 del_timer_sync(&host->DTO_timer);
2941 if((0==cmd->retries) && (host->error_times++%(RK29_ERROR_PRINTK_INTERVAL*3) == 0) && (12 != cmd->opcode))
2943 if( ((RK29_CTRL_SDMMC_ID==host->pdev->id)&&(MMC_SLEEP_AWAKE!=cmd->opcode)) ||
2944 ((RK29_CTRL_SDMMC_ID!=host->pdev->id)&&(MMC_SEND_EXT_CSD!=cmd->opcode)) )
2946 printk(KERN_WARNING "%s..%d...CMD%d(arg=0x%x), hoststate=%d, errorTimes=%d, errorStep=0x%x ! [%s]\n",\
2947 __FUNCTION__, __LINE__, cmd->opcode, cmd->arg, host->state,host->error_times,host->errorstep, host->dma_name);
2954 del_timer_sync(&host->request_timer);
2960 del_timer_sync(&host->request_timer);
2961 del_timer_sync(&host->DTO_timer);
2963 if((0==cmd->retries) && (host->error_times++%RK29_ERROR_PRINTK_INTERVAL == 0))
2965 printk(KERN_WARNING "%s..%d....command_complete(CMD=%d, arg=%x) error=%d. [%s]\n",\
2966 __FUNCTION__, __LINE__, host->cmd->opcode,host->cmd->arg, output, host->dma_name);
2974 static void rk29_sdmmc_start_error(struct rk29_sdmmc *host)
2976 host->cmd->error = -EIO;
2977 host->mrq->cmd->error = -EIO;
2978 host->cmd_status |= SDMMC_INT_RTO;
2980 del_timer_sync(&host->request_timer);
2982 rk29_sdmmc_command_complete(host, host->mrq->cmd);
2983 rk29_sdmmc_request_end(host, host->mrq->cmd);
2986 static void rk29_sdmmc_tasklet_func(unsigned long priv)
2988 struct rk29_sdmmc *host = (struct rk29_sdmmc *)priv;
2989 struct mmc_data *data = host->cmd->data;
2990 enum rk29_sdmmc_state state = host->state;
2991 int pending_flag, stopflag;
2993 rk29_sdmmc_enable_irq(host, false);
2994 spin_lock(&host->lock);//spin_lock_irqsave(&host->lock, iflags);
2996 state = host->state;
3006 xbwprintk(7, "%s..%d.. prev_state= STATE_IDLE [%s]\n", \
3007 __FUNCTION__, __LINE__, host->dma_name);
3011 case STATE_SENDING_CMD:
3013 xbwprintk(7, "%s..%d.. prev_state= STATE_SENDING_CMD, pendingEvernt=0x%lu [%s]\n",\
3014 __FUNCTION__, __LINE__,host->completed_events, host->dma_name);
3015 if(host->cmd->error)
3017 del_timer_sync(&host->request_timer);
3020 if (!rk29_sdmmc_test_and_clear_pending(host, EVENT_CMD_COMPLETE))
3022 host->errorstep = 0xfb;
3024 del_timer_sync(&host->request_timer); //delete the timer for INT_COME_DONE
3026 rk29_sdmmc_set_completed(host, EVENT_CMD_COMPLETE);
3027 rk29_sdmmc_command_complete(host, host->cmd);
3032 rk29_sdmmc_request_end(host, host->cmd);
3034 xbwprintk(7, "%s..%d.. CMD%d call mmc_request_done() . [%s]\n", \
3035 __FUNCTION__, __LINE__,host->cmd->opcode,host->dma_name);
3037 host->complete_done = 1;
3040 host->errorstep = 0xfa;
3041 if(host->cmd->error)
3043 del_timer_sync(&host->DTO_timer); //delete the timer for INT_DTO
3045 if((data->stop) && (MMC_STOP_TRANSMISSION != host->cmd->opcode))
3047 xbwprintk(7, "%s..%d.. cmderr, so call send_stop_cmd() [%s]\n", \
3048 __FUNCTION__, __LINE__, host->dma_name);
3050 stopflag = 1; //Moidfyed by xbw at 2011-09-08
3055 rk29_sdmmc_set_pending(host, EVENT_DATA_COMPLETE);
3058 host->errorstep = 0xf9;
3059 state = STATE_DATA_BUSY;
3063 case STATE_DATA_BUSY:
3065 xbwprintk(7, "%s..%d.. prev_state= STATE_DATA_BUSY, pendingEvernt=0x%lu [%s]\n", \
3066 __FUNCTION__, __LINE__,host->pending_events, host->dma_name);
3068 if (!rk29_sdmmc_test_and_clear_pending(host, EVENT_DATA_COMPLETE))
3070 host->errorstep = 0xf8;
3071 rk29_sdmmc_set_completed(host, EVENT_DATA_COMPLETE);
3073 #if SDMMC_USE_INT_UNBUSY
3074 if((MMC_WRITE_BLOCK==host->cmd->opcode)||(MMC_WRITE_MULTIPLE_BLOCK==host->cmd->opcode))
3077 ** use DTO_timer for waiting for INT_UNBUSY.
3078 ** max 250ms in specification, but adapt 500 for the compatibility of all kinds of sick sdcard.
3080 mod_timer(&host->DTO_timer, jiffies + msecs_to_jiffies(5000));
3084 del_timer_sync(&host->DTO_timer); //delete the timer for INT_DTO
3087 state = STATE_DATA_UNBUSY;
3090 del_timer_sync(&host->DTO_timer); //delete the timer for INT_DTO
3094 case STATE_DATA_UNBUSY:
3096 #if SDMMC_USE_INT_UNBUSY
3097 if((MMC_WRITE_BLOCK==host->cmd->opcode)||(MMC_WRITE_MULTIPLE_BLOCK==host->cmd->opcode))
3099 if (!rk29_sdmmc_test_and_clear_pending(host, EVENT_DATA_UNBUSY))
3102 del_timer_sync(&host->DTO_timer);
3104 rk29_sdmmc_set_completed(host, EVENT_DATA_UNBUSY);
3105 state = STATE_DATA_END;
3107 rk29_sdmmc_request_end(host, host->cmd);
3109 if (data && !data->stop)
3111 xbwprintk(7, "%s..%d.. CMD%d call mmc_request_done(). [%s]\n", \
3112 __FUNCTION__, __LINE__,host->cmd->opcode,host->dma_name);
3114 if(!( (MMC_READ_SINGLE_BLOCK == host->cmd->opcode)&&( -EIO == data->error))) //deal with START_BIT_ERROR
3116 host->complete_done = 2;
3121 host->errorstep = 0xf4;
3122 xbwprintk(7, "%s..%d.. after DATA_COMPLETE, so call send_stop_cmd() [%s]\n", \
3123 __FUNCTION__, __LINE__, host->dma_name);
3125 stopflag = 2; //Moidfyed by xbw at 2011-09-08
3130 case STATE_SENDING_STOP:
3132 xbwprintk(7, "%s..%d.. prev_state= STATE_SENDING_STOP, pendingEvernt=0x%lu [%s]\n", \
3133 __FUNCTION__, __LINE__, host->pending_events, host->dma_name);
3135 if (!rk29_sdmmc_test_and_clear_pending(host, EVENT_CMD_COMPLETE))
3138 rk29_sdmmc_command_complete(host, host->cmd);
3139 del_timer_sync(&host->request_timer); //delete the timer for INT_CMD_DONE int CMD12
3140 rk29_sdmmc_request_end(host, host->cmd);
3142 host->complete_done = 3;
3146 case STATE_DATA_END:
3152 pending_flag = (host->complete_done > 0) && (host->retryfunc<50) \
3153 && (rk29_sdmmc_test_pending(host, EVENT_CMD_COMPLETE)|| rk29_sdmmc_test_pending(host, EVENT_DATA_COMPLETE) ) \
3154 && test_bit(RK29_SDMMC_CARD_PRESENT, &host->flags);
3158 xbwprintk(7, "%s..%d... cmd=%d(arg=0x%x),completedone=%d, retrycount=%d, doneflag=%d, \n \
3159 host->state=0x%x, switchstate=%x, \n \
3160 pendingEvent=0x%lu, completeEvents=0x%lu, \n \
3161 mrqCMD=%d, arg=0x%x [%s]\n",\
3163 __FUNCTION__, __LINE__,host->cmd->opcode, host->cmd->arg, host->complete_done,\
3164 host->retryfunc, host->mmc->doneflag,host->state, state, \
3165 host->pending_events,host->completed_events,\
3166 host->mrq->cmd->opcode, host->mrq->cmd->arg, host->dma_name);
3171 } while(pending_flag && ++host->retryfunc); //while(0);
3175 if(host->cmd->error)
3176 xbwprintk(3,"%d: call send_stop_cmd== %d, completedone=%d, doneflag=%d, hoststate=%x, statusReg=0x%x \n", \
3177 __LINE__,stopflag, host->complete_done, host->mmc->doneflag, state, rk29_sdmmc_read(host->regs, SDMMC_STATUS));
3179 host->errorstep = 0xe0;
3181 if(test_bit(RK29_SDMMC_CARD_PRESENT, &host->flags))
3183 state = STATE_SENDING_CMD;
3184 send_stop_cmd(host); //Moidfyed by xbw at 2011-09-08
3188 host->complete_done = 5;
3192 host->state = state;
3194 if((0==host->complete_done)&& host->mmc->doneflag && test_bit(RK29_SDMMC_CARD_PRESENT, &host->flags))
3196 host->errorstep = 0xf2;
3200 if(12==host->cmd->opcode)
3202 printk(KERN_ERR "%d... cmd=%d(arg=0x%x),blksz=%d,blocks=%d,errorStep=0x%x,\n host->state=%x, statusReg=0x%x [%s]\n",\
3203 __LINE__,host->mrq->cmd->opcode, host->mrq->cmd->arg, host->mrq->cmd->data->blksz, host->mrq->cmd->data->blocks,\
3204 host->errorstep,host->state,rk29_sdmmc_read(host->regs, SDMMC_STATUS),host->dma_name);
3208 spin_unlock(&host->lock);//spin_unlock_irqrestore(&host->lock, iflags);
3209 rk29_sdmmc_enable_irq(host, true);
3212 host->errorstep = 0xf3;
3213 host->state = STATE_IDLE;
3215 if(host->mrq && host->mmc->doneflag && host->complete_done)
3217 host->mmc->doneflag = 0;
3218 host->complete_done = 0;
3219 spin_unlock(&host->lock);//spin_unlock_irqrestore(&host->lock, iflags);
3220 rk29_sdmmc_enable_irq(host, true);
3221 mmc_request_done(host->mmc, host->mrq);
3225 spin_unlock(&host->lock);//spin_unlock_irqrestore(&host->lock, iflags);
3226 rk29_sdmmc_enable_irq(host, true);
3231 static inline void rk29_sdmmc_cmd_interrupt(struct rk29_sdmmc *host, u32 status)
3235 host->cmd_status |= status;
3236 host->errorstep = 0xfc;
3237 if((MMC_STOP_TRANSMISSION != host->cmd->opcode) && (host->cmdr & SDMMC_CMD_DAT_EXP))
3240 multi = rk29_sdmmc_read(host->regs, SDMMC_BYTCNT)/unit;
3241 multi += ((rk29_sdmmc_read(host->regs, SDMMC_BYTCNT)%unit) ? 1 :0 );
3242 multi = (multi>0) ? multi : 1;
3243 multi += (host->cmd->retries>2)?2:host->cmd->retries;
3244 mod_timer(&host->DTO_timer, jiffies + msecs_to_jiffies(RK29_SDMMC_WAIT_DTO_INTERNVAL*multi));//max wait 8s larger
3248 rk29_sdmmc_set_pending(host, EVENT_CMD_COMPLETE);
3249 tasklet_schedule(&host->tasklet);
3252 static irqreturn_t rk29_sdmmc_interrupt(int irq, void *dev_id)
3254 struct rk29_sdmmc *host = dev_id;
3255 u32 status, pending;
3260 spin_lock(&host->lock);
3262 status = rk29_sdmmc_read(host->regs, SDMMC_RINTSTS);
3263 pending = rk29_sdmmc_read(host->regs, SDMMC_MINTSTS);// read only mask reg
3270 if(pending & SDMMC_INT_CD)
3272 //disable_irq_nosync(host->irq);
3273 rk29_sdmmc_write(host->regs, SDMMC_RINTSTS, SDMMC_INT_CD); // clear sd detect int
3274 present = rk29_sdmmc_get_cd(host->mmc);
3275 present_old = test_bit(RK29_SDMMC_CARD_PRESENT, &host->flags);
3277 if(present != present_old)
3279 printk(KERN_INFO "\n******************\n%s:INT_CD=0x%x,INT-En=%d,hostState=%d, present Old=%d ==> New=%d . [%s]\n",\
3280 __FUNCTION__, pending, host->mmc->re_initialized_flags, host->state, present_old, present, host->dma_name);
3282 rk28_send_wakeup_key(); //wake up backlight
3283 host->error_times = 0;
3286 del_timer(&host->request_timer);
3287 del_timer(&host->DTO_timer);
3288 rk29_sdmmc_dealwith_timeout(host);
3293 set_bit(RK29_SDMMC_CARD_PRESENT, &host->flags);
3295 if(host->mmc->re_initialized_flags)
3297 mod_timer(&host->detect_timer, jiffies + msecs_to_jiffies(RK29_SDMMC_REMOVAL_DELAY));
3301 mod_timer(&host->detect_timer, jiffies + msecs_to_jiffies(RK29_SDMMC_REMOVAL_DELAY*2));
3306 clear_bit(RK29_SDMMC_CARD_PRESENT, &host->flags);
3307 host->mmc->re_initialized_flags = 0;
3309 mmc_detect_change(host->mmc, 200);
3318 if (pending & SDMMC_INT_CMD_DONE) {
3320 xbwprintk(6, "%s..%d.. CMD%d INT_CMD_DONE INT=0x%x [%s]\n", \
3321 __FUNCTION__, __LINE__, host->cmd->opcode,pending, host->dma_name);
3323 rk29_sdmmc_write(host->regs, SDMMC_RINTSTS,SDMMC_INT_CMD_DONE); // clear interrupt
3324 rk29_sdmmc_cmd_interrupt(host, status);
3329 #if !defined(CONFIG_RK29_SDIO_IRQ_FROM_GPIO)
3330 if(pending & SDMMC_INT_SDIO)
3332 xbwprintk(7, "%s..%d.. INT_SDIO INT=0x%x [%s]\n", \
3333 __FUNCTION__, __LINE__, pending, host->dma_name);
3335 rk29_sdmmc_write(host->regs, SDMMC_RINTSTS,SDMMC_INT_SDIO);
3342 if(pending & SDMMC_INT_RTO)
3344 xbwprintk(7, "%s..%d.. CMD%d CMD_ERROR_FLAGS INT=0x%x [%s]\n", \
3345 __FUNCTION__, __LINE__, host->cmd->opcode,pending, host->dma_name);
3347 //rk29_sdmmc_write(host->regs, SDMMC_RINTSTS,SDMMC_INT_RTO);
3348 rk29_sdmmc_write(host->regs, SDMMC_RINTSTS,0xFFFFFFFF); //Modifyed by xbw at 2011-08-15
3349 host->cmd_status = status;
3351 rk29_sdmmc_set_pending(host, EVENT_CMD_COMPLETE);
3353 if(!(pending & SDMMC_INT_CMD_DONE))
3355 tasklet_schedule(&host->tasklet);
3362 if(pending & SDMMC_INT_HLE)
3364 printk(KERN_WARNING "%s: Error due to hardware locked. Please check your hardware. INT=0x%x, CMD%d(arg=0x%x, retries=%d). [%s]\n",\
3365 __FUNCTION__, pending,host->cmd->opcode, host->cmd->arg, host->cmd->retries, host->dma_name);
3367 rk29_sdmmc_write(host->regs, SDMMC_RINTSTS,SDMMC_INT_HLE);
3372 if(pending & SDMMC_INT_DTO)
3374 xbwprintk(1,"%d..%s: INT=0x%x ,RINTSTS=0x%x, CMD%d(arg=0x%x, retries=%d),host->state=0x%x. [%s]\n", \
3375 __LINE__,__FUNCTION__, pending,status, host->cmd->opcode, host->cmd->arg, host->cmd->retries, host->state,host->dma_name);
3377 rk29_sdmmc_write(host->regs, SDMMC_RINTSTS,SDMMC_INT_DTO);
3378 del_timer(&host->DTO_timer); //delete the timer for INT_DTO
3380 host->data_status |= status;
3384 rk29_sdmmc_set_pending(host, EVENT_DATA_COMPLETE);
3385 tasklet_schedule(&host->tasklet);
3390 if (pending & SDMMC_INT_FRUN)
3392 printk(KERN_WARNING "%s: INT=0x%x Oh!My God,let me see!What happened?Why?Where? CMD%d(arg=0x%x, retries=%d). [%s]\n", \
3393 __FUNCTION__, pending, host->cmd->opcode, host->cmd->arg, host->cmd->retries,host->dma_name);
3395 //rk29_sdmmc_write(host->regs, SDMMC_RINTSTS,SDMMC_INT_FRUN);
3396 rk29_sdmmc_write(host->regs, SDMMC_RINTSTS,0xFFFFFFFE);
3400 if (pending & SDMMC_INT_RXDR)
3402 xbwprintk(6, "%s..%d.. SDMMC_INT_RXDR INT=0x%x [%s]\n", \
3403 __FUNCTION__, __LINE__, pending, host->dma_name);
3405 rk29_sdmmc_write(host->regs, SDMMC_RINTSTS,SDMMC_INT_RXDR); // clear interrupt
3406 rk29_sdmmc_read_data_pio(host);
3409 if (pending & SDMMC_INT_TXDR)
3411 xbwprintk(6, "%s..%d.. SDMMC_INT_TXDR INT=0x%x [%s]\n", \
3412 __FUNCTION__, __LINE__, pending, host->dma_name);
3414 rk29_sdmmc_write(host->regs, SDMMC_RINTSTS,SDMMC_INT_TXDR); // clear interrupt
3415 rk29_sdmmc_write_data_pio(host);
3418 #if SDMMC_USE_INT_UNBUSY
3419 if(pending & SDMMC_INT_UNBUSY)
3421 xbwprintk(1, "%d..%s: INT=0x%x ,RINTSTS=0x%x, CMD%d(arg=0x%x, retries=%d),host->state=0x%x. [%s]\n", \
3422 __LINE__,__FUNCTION__, pending,status, host->cmd->opcode, host->cmd->arg, host->cmd->retries, \
3423 host->state,host->dma_name);
3425 rk29_sdmmc_write(host->regs, SDMMC_RINTSTS,SDMMC_INT_UNBUSY);
3427 host->data_status = status;
3429 rk29_sdmmc_set_pending(host, EVENT_DATA_UNBUSY);
3430 tasklet_schedule(&host->tasklet);
3438 spin_unlock(&host->lock);
3442 mmc_signal_sdio_irq(host->mmc);
3450 * MMC card detect thread, kicked off from detect interrupt, 1 timer
3453 static void rk29_sdmmc_detect_change(unsigned long data)
3455 struct rk29_sdmmc *host = (struct rk29_sdmmc *)data;
3462 if((RK29_CTRL_SDMMC_ID == host->pdev->id) && rk29_sdmmc_get_cd(host->mmc))
3464 host->mmc->re_initialized_flags =1;
3467 mmc_detect_change(host->mmc, 0);
3471 static void rk29_sdmmc1_check_status(unsigned long data)
3473 struct rk29_sdmmc *host = (struct rk29_sdmmc *)data;
3474 struct rk29_sdmmc_platform_data *pdata = host->pdev->dev.platform_data;
3475 unsigned int status;
3477 status = pdata->status(mmc_dev(host->mmc));
3479 pr_info("%s: slot status change detected(%d-%d)\n",mmc_hostname(host->mmc), host->oldstatus, status);
3481 if (status ^ host->oldstatus)
3485 #if RK_SDMMC_USE_SDIO_SUSPEND_RESUME
3486 if(host->pdev->id == RK29_CTRL_SDIO1_ID)
3487 host->mmc->pm_caps |= (MMC_PM_KEEP_POWER|MMC_PM_WAKE_SDIO_IRQ);
3490 rk29_sdmmc_hw_init(host);
3491 set_bit(RK29_SDMMC_CARD_PRESENT, &host->flags);
3492 mod_timer(&host->detect_timer, jiffies + msecs_to_jiffies(200));
3496 clear_bit(RK29_SDMMC_CARD_PRESENT, &host->flags);
3497 rk29_sdmmc_detect_change((unsigned long)host);
3501 host->oldstatus = status;
3504 static void rk29_sdmmc1_status_notify_cb(int card_present, void *dev_id)
3506 struct rk29_sdmmc *host = dev_id;
3508 rk29_sdmmc1_check_status((unsigned long)host);
3512 #if defined(CONFIG_SDMMC0_RK29_SDCARD_DET_FROM_GPIO)
3513 static irqreturn_t det_keys_isr(int irq, void *dev_id);
3514 static void rk29_sdmmc_detect_change_work(struct work_struct *work)
3517 struct rk29_sdmmc *host = container_of(work, struct rk29_sdmmc, work.work);
3519 rk28_send_wakeup_key();
3520 rk29_sdmmc_detect_change(host);
3524 static irqreturn_t det_keys_isr(int irq, void *dev_id)
3526 struct rk29_sdmmc *host = dev_id;
3528 #if defined(CONFIG_SDMMC0_RK29_SDCARD_DET_FROM_GPIO)
3533 present = rk29_sdmmc_get_cd(host->mmc);
3534 present_old = test_bit(RK29_SDMMC_CARD_PRESENT, &host->flags);
3536 if(present != present_old)
3538 printk(KERN_INFO "\n******************\n%s: present Old=%d ==> New=%d . [%s]\n",\
3539 __FUNCTION__, present_old, present, host->dma_name);
3542 value = gpio_get_value(host->det_pin.io) ?IRQ_TYPE_EDGE_FALLING : IRQ_TYPE_EDGE_RISING;
3544 irq_set_irq_type(host->gpio_irq, value);
3548 del_timer(&host->request_timer);
3549 del_timer(&host->DTO_timer);
3550 rk29_sdmmc_dealwith_timeout(host);
3552 enable_irq_wake(host->gpio_irq);
3556 set_bit(RK29_SDMMC_CARD_PRESENT, &host->flags);
3557 schedule_delayed_work(&host->work, msecs_to_jiffies(500));
3561 clear_bit(RK29_SDMMC_CARD_PRESENT, &host->flags);
3562 host->mmc->re_initialized_flags = 0;
3563 schedule_delayed_work(&host->work, 0);
3567 dev_info(&host->pdev->dev, "sd det_gpio changed(%s), send wakeup key!\n",
3568 gpio_get_value(host->det_pin.io)?"removed":"insert");
3569 rk29_sdmmc_detect_change((unsigned long)dev_id);
3574 static int rk29_sdmmc_probe(struct platform_device *pdev)
3576 struct mmc_host *mmc;
3577 struct rk29_sdmmc *host;
3578 struct resource *regs;
3579 struct rk29_sdmmc_platform_data *pdata;
3583 #if defined(CONFIG_RK29_SDIO_IRQ_FROM_GPIO)
3584 unsigned long trigger_flags;
3587 /* must have platform data */
3588 pdata = pdev->dev.platform_data;
3590 dev_err(&pdev->dev, "Platform data missing\n");
3595 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3601 mmc = mmc_alloc_host(sizeof(struct rk29_sdmmc), &pdev->dev);
3608 host = mmc_priv(mmc);
3612 host->ctype = 0; // set default 1 bit mode
3613 host->errorstep = 0;
3615 host->old_cmd = 100;
3617 host->old_div = 0xFF;
3618 host->error_times = 0;
3619 host->state = STATE_IDLE;
3620 host->complete_done = 0;
3621 host->retryfunc = 0;
3623 host->new_mrq = NULL;
3624 host->irq_state = true;
3625 host->timeout_times = 0;
3628 host->det_pin.io = pdata->det_pin_info.io;
3629 host->det_pin.enable = pdata->det_pin_info.enable;
3630 host->det_pin.iomux.name = pdata->det_pin_info.iomux.name;
3631 host->det_pin.iomux.fgpio = pdata->det_pin_info.iomux.fgpio;
3632 host->det_pin.iomux.fmux = pdata->det_pin_info.iomux.fmux;
3634 host->gpio_power_en = pdata->power_en;
3635 host->gpio_power_en_level = pdata->power_en_level;
3637 host->set_iomux = pdata->set_iomux;
3639 #if defined(CONFIG_RK29_SDIO_IRQ_FROM_GPIO)
3640 if(RK29_CTRL_SDIO1_ID == host->pdev->id)
3642 host->sdio_INT_gpio = pdata->sdio_INT_gpio;
3643 #ifdef USE_SDIO_INT_LEVEL
3644 host->trigger_level = pdata->sdio_INT_level;
3652 spin_lock_init(&host->lock);
3654 #ifdef RK29_SDMMC_LIST_QUEUE
3655 INIT_LIST_HEAD(&host->queue);
3658 host->clk = clk_get(&pdev->dev, "mmc");
3660 #if RK29_SDMMC_DEFAULT_SDIO_FREQ
3661 clk_set_rate(host->clk,SDHC_FPP_FREQ);
3663 if(RK29_CTRL_SDMMC_ID== host->pdev->id)
3664 clk_set_rate(host->clk,SDHC_FPP_FREQ);
3666 clk_set_rate(host->clk,RK29_MAX_SDIO_FREQ);
3670 clk_enable(host->clk);
3671 clk_enable(clk_get(&pdev->dev, "hclk_mmc"));
3674 host->regs = ioremap(regs->start, regs->end - regs->start + 1);
3677 host->errorstep = 0x8A;
3681 mmc->ops = &rk29_sdmmc_ops[pdev->id];
3682 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 35))
3683 mmc->pm_flags |= MMC_PM_IGNORE_PM_NOTIFY;
3685 mmc->f_min = FOD_FREQ;
3687 #if RK29_SDMMC_DEFAULT_SDIO_FREQ
3688 mmc->f_max = SDHC_FPP_FREQ;
3690 if(RK29_CTRL_SDMMC_ID== host->pdev->id)
3692 mmc->f_max = SDHC_FPP_FREQ;
3696 mmc->f_max = RK29_MAX_SDIO_FREQ;
3700 //mmc->ocr_avail = pdata->host_ocr_avail;
3701 mmc->ocr_avail = MMC_VDD_27_28|MMC_VDD_28_29|MMC_VDD_29_30|MMC_VDD_30_31
3702 | MMC_VDD_31_32|MMC_VDD_32_33 | MMC_VDD_33_34 | MMC_VDD_34_35| MMC_VDD_35_36; ///set valid volage 2.7---3.6v
3703 mmc->caps = pdata->host_caps;
3704 mmc->re_initialized_flags = 1;
3706 mmc->sdmmc_host_hw_init = rk29_sdmmc_hw_init;
3711 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 37))
3714 mmc->max_phys_segs = 64;
3715 mmc->max_hw_segs = 64;
3719 * Block size can be up to 2048 bytes, but must be a power of two.
3721 mmc->max_blk_size = 4095;
3724 * No limit on the number of blocks transferred.
3726 mmc->max_blk_count = 4096;
3729 * Since we only have a 16-bit data length register, we must
3730 * ensure that we don't exceed 2^16-1 bytes in a single request.
3732 mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count; //8M bytes(2K*4K)
3735 * Set the maximum segment size. Since we aren't doing DMA
3736 * (yet) we are only limited by the data length register.
3738 mmc->max_seg_size = mmc->max_req_size;
3740 tasklet_init(&host->tasklet, rk29_sdmmc_tasklet_func, (unsigned long)host);
3742 /* Create card detect handler thread */
3743 setup_timer(&host->detect_timer, rk29_sdmmc_detect_change,(unsigned long)host);
3744 setup_timer(&host->request_timer,rk29_sdmmc_INT_CMD_DONE_timeout,(unsigned long)host);
3745 setup_timer(&host->DTO_timer,rk29_sdmmc_INT_DTO_timeout,(unsigned long)host);
3747 host->irq = platform_get_irq(pdev, 0);
3750 host->errorstep = 0x8B;
3755 memcpy(host->dma_name, pdata->dma_name, 8);
3756 host->use_dma = pdata->use_dma;
3758 xbwprintk(7,"%s..%s..%d..*********** Bus clock= %d Khz **** [%s]\n",\
3759 __FILE__, __FUNCTION__,__LINE__,clk_get_rate(host->clk)/1000, host->dma_name);
3764 host->dma_info = rk29_sdmmc_dma_infos[host->pdev->id];
3765 ret = rk29_dma_request(host->dma_info.chn, &(host->dma_info.client), NULL);
3768 printk(KERN_WARNING "%s..%d...rk29_dma_request error=%d. [%s]\n", \
3769 __FUNCTION__, __LINE__,ret, host->dma_name);
3770 host->errorstep = 0x97;
3774 #if 0 //deal with the old API of DMA-module
3775 ret = rk29_dma_config(host->dma_info.chn, 4);
3776 #else //deal with the new API of DMA-module
3777 if(RK29_CTRL_SDMMC_ID== host->pdev->id)
3779 ret = rk29_dma_config(host->dma_info.chn, 4, 16);
3783 #if defined(CONFIG_ARCH_RK29)
3784 // to maintain set this value to 1 in RK29,noted at 2012-07-16
3785 ret = rk29_dma_config(host->dma_info.chn, 4, 1);
3787 // a unified set the burst value to 16 in RK30,noted at 2012-07-16
3788 ret = rk29_dma_config(host->dma_info.chn, 4, 16);
3794 printk(KERN_WARNING "%s..%d.. rk29_dma_config error=%d. [%s]\n", \
3795 __FUNCTION__, __LINE__, ret, host->dma_name);
3796 host->errorstep = 0x98;
3800 ret = rk29_dma_set_buffdone_fn(host->dma_info.chn, rk29_sdmmc_dma_complete);
3803 printk(KERN_WARNING "%s..%d.. dma_set_buffdone_fn error=%d. [%s]\n", \
3804 __FUNCTION__, __LINE__, ret, host->dma_name);
3805 host->errorstep = 0x99;
3809 host->dma_addr = regs->start + SDMMC_DATA;
3812 #if defined(CONFIG_SDMMC0_RK29_WRITE_PROTECT) || defined(CONFIG_SDMMC1_RK29_WRITE_PROTECT)
3813 host->write_protect = pdata->write_prt;
3814 host->protect_level = pdata->write_prt_enalbe_level;
3817 #if defined(CONFIG_ARCH_RK29)
3818 if(RK29_CTRL_SDMMC_ID == host->pdev->id)
3820 rk29_sdmmc_hw_init(host);
3824 ret = request_irq(host->irq, rk29_sdmmc_interrupt, 0, dev_name(&pdev->dev), host);
3828 printk(KERN_WARNING "%s..%d.. request_irq error=%d. [%s]\n", \
3829 __FUNCTION__, __LINE__, ret, host->dma_name);
3830 host->errorstep = 0x8C;
3834 #if defined(CONFIG_SDMMC0_RK29_SDCARD_DET_FROM_GPIO)
3835 if((RK29_CTRL_SDMMC_ID == host->pdev->id) && (INVALID_GPIO != host->det_pin.io))
3837 INIT_DELAYED_WORK(&host->work, rk29_sdmmc_detect_change_work);
3838 ret = gpio_request(host->det_pin.io, "sd_detect");
3840 dev_err(&pdev->dev, "gpio_request error\n");
3843 gpio_direction_input(host->det_pin.io);
3845 level_value = gpio_get_value(host->det_pin.io);
3847 host->gpio_irq = gpio_to_irq(host->det_pin.io);
3848 ret = request_irq(host->gpio_irq, det_keys_isr,
3849 level_value?IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING,
3853 dev_err(&pdev->dev, "gpio request_irq error\n");
3857 enable_irq_wake(host->gpio_irq);
3859 #elif DRIVER_SDMMC_USE_NEW_IOMUX_API
3860 if(RK29_CTRL_SDMMC_ID == host->pdev->id)
3862 iomux_set(MMC0_DETN);
3867 #if defined(CONFIG_RK29_SDIO_IRQ_FROM_GPIO)
3868 if(RK29_CTRL_SDIO1_ID == host->pdev->id)
3870 gpio_request(host->sdio_INT_gpio, "sdio_interrupt");
3872 // intput + pull_Up,
3873 gpio_direction_input(host->sdio_INT_gpio);
3874 //gpio_direction_output(host->sdio_INT_gpio, GPIO_HIGH);
3875 gpio_pull_updown(host->sdio_INT_gpio, 0); //disable default internal pull-down
3877 host->sdio_irq = gpio_to_irq(host->sdio_INT_gpio);
3878 #ifdef USE_SDIO_INT_LEVEL
3879 trigger_flags = (host->trigger_level==GPIO_HIGH)?IRQF_TRIGGER_HIGH:IRQF_TRIGGER_LOW;
3881 trigger_flags = IRQF_TRIGGER_LOW;
3883 //printk("%d..%s sdio interrupt gpio level=%lu ====[%s]====\n", __LINE__, __FUNCTION__, trigger_flags, host->dma_name);
3884 ret = request_irq(host->sdio_irq, rk29_sdmmc_sdio_irq_cb,
3891 printk("%s..%d.. sdio_request_INT_irq error=%d ====xbw[%s]====\n", \
3892 __FUNCTION__, __LINE__, ret, host->dma_name);
3893 host->errorstep = 0x8D;
3897 disable_irq_nosync(host->sdio_irq);
3898 enable_irq_wake(host->sdio_irq);
3903 /* setup sdmmc1 wifi card detect change */
3904 if (pdata->register_status_notify) {
3905 pdata->register_status_notify(rk29_sdmmc1_status_notify_cb, host);
3908 if(RK29_CTRL_SDMMC_ID== host->pdev->id)
3910 if(rk29_sdmmc_get_cd(host->mmc))
3912 set_bit(RK29_SDMMC_CARD_PRESENT, &host->flags);
3916 clear_bit(RK29_SDMMC_CARD_PRESENT, &host->flags);
3921 #if defined(CONFIG_USE_SDMMC0_FOR_WIFI_DEVELOP_BOARD)
3922 if(0== host->pdev->id)
3924 set_bit(RK29_SDMMC_CARD_PRESENT, &host->flags);
3928 #if defined(CONFIG_USE_SDMMC1_FOR_WIFI_DEVELOP_BOARD)
3929 if(1== host->pdev->id)
3931 set_bit(RK29_SDMMC_CARD_PRESENT, &host->flags);
3937 /* sdmmc1 wifi card slot status initially */
3938 if (pdata->status) {
3939 host->oldstatus = pdata->status(mmc_dev(host->mmc));
3940 if (host->oldstatus) {
3941 set_bit(RK29_SDMMC_CARD_PRESENT, &host->flags);
3943 clear_bit(RK29_SDMMC_CARD_PRESENT, &host->flags);
3948 platform_set_drvdata(pdev, mmc);
3952 #ifdef RK29_SDMMC_NOTIFY_REMOVE_INSERTION
3954 globalSDhost[pdev->id] = (struct rk29_sdmmc *)host;
3955 if(0== host->pdev->id)
3957 rk29_sdmmc_progress_add_attr(pdev);
3961 #if defined (CONFIG_DEBUG_FS)
3962 rk29_sdmmc_init_debugfs(host);
3965 printk(KERN_INFO ".Line%d..The End of SDMMC-probe %s. [%s]\n", __LINE__, RK29_SDMMC_VERSION,host->dma_name);
3972 rk29_dma_free(host->dma_info.chn, &host->dma_info.client);
3976 iounmap(host->regs);
3988 static int __exit rk29_sdmmc_remove(struct platform_device *pdev)
3991 struct mmc_host *mmc = platform_get_drvdata(pdev);
3992 struct rk29_sdmmc *host;
3993 struct resource *regs;
3998 host = mmc_priv(mmc);
4001 rk29_sdmmc_control_clock(host, 0);
4003 /* Shutdown detect IRQ and kill detect thread */
4004 del_timer_sync(&host->detect_timer);
4005 del_timer_sync(&host->request_timer);
4006 del_timer_sync(&host->DTO_timer);
4008 tasklet_disable(&host->tasklet);
4009 free_irq(platform_get_irq(pdev, 0), host);
4012 rk29_dma_free(host->dma_info.chn, &host->dma_info.client);
4015 mmc_remove_host(mmc);
4017 iounmap(host->regs);
4019 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
4020 release_mem_region(regs->start,resource_size(regs));
4023 platform_set_drvdata(pdev, NULL);
4031 static int rk29_sdmmc_sdcard_suspend(struct rk29_sdmmc *host)
4034 #if !defined(CONFIG_SDMMC0_RK29_SDCARD_DET_FROM_GPIO)
4035 rk29_sdmmc_enable_irq(host,false);
4036 #if DRIVER_SDMMC_USE_NEW_IOMUX_API
4037 //need not to change mode to gpio.
4039 rk29_mux_api_set(host->det_pin.iomux.name, host->det_pin.iomux.fgpio);
4041 gpio_request(host->det_pin.io, "sd_detect");
4042 gpio_direction_output(host->det_pin.io, GPIO_HIGH);
4043 gpio_direction_input(host->det_pin.io);
4045 host->gpio_irq = gpio_to_irq(host->det_pin.io);
4046 ret = request_irq(host->gpio_irq, det_keys_isr,
4047 (gpio_get_value(host->det_pin.io))?IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING,
4051 enable_irq_wake(host->gpio_irq);
4057 static void rk29_sdmmc_sdcard_resume(struct rk29_sdmmc *host)
4059 #if !defined(CONFIG_SDMMC0_RK29_SDCARD_DET_FROM_GPIO)
4060 disable_irq_wake(host->gpio_irq);
4061 free_irq(host->gpio_irq,host);
4062 gpio_free(host->det_pin.io);
4063 #if DRIVER_SDMMC_USE_NEW_IOMUX_API
4064 iomux_set(MMC0_DETN);
4066 rk29_mux_api_set(host->det_pin.iomux.name, host->det_pin.iomux.fmux);
4068 rk29_sdmmc_enable_irq(host, true);
4072 static int rk29_sdmmc_suspend(struct platform_device *pdev, pm_message_t state)
4074 struct mmc_host *mmc = platform_get_drvdata(pdev);
4075 struct rk29_sdmmc *host = mmc_priv(mmc);
4078 if(host && host->pdev && (RK29_CTRL_SDMMC_ID == host->pdev->id)) //only the SDMMC0 have suspend-resume; noted by xbw
4081 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 35))
4082 ret = mmc_suspend_host(mmc);
4084 ret = mmc_suspend_host(mmc, state);
4087 #if !defined(CONFIG_SDMMC0_RK29_SDCARD_DET_FROM_GPIO)
4088 if(rk29_sdmmc_sdcard_suspend(host) < 0)
4089 dev_info(&host->pdev->dev, "rk29_sdmmc_sdcard_suspend error\n");
4092 #if RK_SDMMC_USE_SDIO_SUSPEND_RESUME
4093 else if(host && host->pdev && (RK29_CTRL_SDIO1_ID == host->pdev->id))
4096 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 35))
4097 ret = mmc_suspend_host(mmc);
4099 ret = mmc_suspend_host(mmc, state);
4103 clk_disable(host->clk);
4104 clk_disable(clk_get(&pdev->dev, "hclk_mmc"));
4107 #endif // --#if RK_SDMMC_USE_SDIO_SUSPEND_RESUME
4112 static int rk29_sdmmc_resume(struct platform_device *pdev)
4114 struct mmc_host *mmc = platform_get_drvdata(pdev);
4115 struct rk29_sdmmc *host = mmc_priv(mmc);
4118 if(host && host->pdev && (RK29_CTRL_SDMMC_ID == host->pdev->id)) //only the SDMMC0 have suspend-resume; noted by xbw
4123 #if !defined(CONFIG_SDMMC0_RK29_SDCARD_DET_FROM_GPIO)
4124 rk29_sdmmc_sdcard_resume(host);
4127 ret = mmc_resume_host(mmc);
4130 #if RK_SDMMC_USE_SDIO_SUSPEND_RESUME
4131 else if(host && host->pdev && (RK29_CTRL_SDIO1_ID == host->pdev->id))
4135 clk_enable(host->clk);
4136 clk_enable(clk_get(&pdev->dev, "hclk_mmc"));
4138 ret = mmc_resume_host(mmc);
4141 #endif // --#if RK_SDMMC_USE_SDIO_SUSPEND_RESUME
4146 #define rk29_sdmmc_suspend NULL
4147 #define rk29_sdmmc_resume NULL
4150 static struct platform_driver rk29_sdmmc_driver = {
4151 .suspend = rk29_sdmmc_suspend,
4152 .resume = rk29_sdmmc_resume,
4153 .remove = __exit_p(rk29_sdmmc_remove),
4155 .name = "rk29_sdmmc",
4159 static int __init rk29_sdmmc_init(void)
4161 return platform_driver_probe(&rk29_sdmmc_driver, rk29_sdmmc_probe);
4164 static void __exit rk29_sdmmc_exit(void)
4166 platform_driver_unregister(&rk29_sdmmc_driver);
4169 module_init(rk29_sdmmc_init);
4170 module_exit(rk29_sdmmc_exit);
4172 MODULE_DESCRIPTION("Rk29 Multimedia Card Interface driver");
4173 MODULE_AUTHOR("xbw@rock-chips.com");
4174 MODULE_LICENSE("GPL v2");