1. The accuracy enhanced gpio_detect_card, such as RK2926.
[firefly-linux-kernel-4.4.55.git] / drivers / mmc / host / rk29_sdmmc.c
1 /* drivers/mmc/host/rk29_sdmmc.c
2  *
3  * Copyright (C) 2010 ROCKCHIP, Inc.
4  *
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.
8  *
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.
13  *
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
18  */
19  
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>
41
42 #include <mach/board.h>
43 #include <mach/io.h>
44 #include <mach/gpio.h>
45 #include <mach/iomux.h>
46 #include <asm/unaligned.h>
47
48 #include <asm/dma.h>
49 #include <mach/dma-pl330.h>
50 #include <asm/scatterlist.h>
51
52 #include "rk29_sdmmc.h"
53
54
55 #define RK29_SDMMC_xbw_Debug 0
56
57 #if RK29_SDMMC_xbw_Debug 
58 int debug_level = 5;
59 #define xbwprintk(n, format, arg...) \
60         if (n <= debug_level) {  \
61                 printk(format,##arg); \
62         }
63 #else
64 #define xbwprintk(n, arg...)
65 #endif
66
67 #if defined(CONFIG_ARCH_RK29)
68 #define SDMMC_USE_INT_UNBUSY     0
69 #else
70 #define SDMMC_USE_INT_UNBUSY     0///1 
71 #endif
72
73 /*
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
77 */
78 #if defined(CONFIG_MTK_COMBO_DRIVER_VERSION_JB2)
79 #define RK_SDMMC_USE_SDIO_SUSPEND_RESUME    1
80 #else
81 #define RK_SDMMC_USE_SDIO_SUSPEND_RESUME    0
82 #endif
83
84 #define RK29_SDMMC_ERROR_FLAGS          (SDMMC_INT_FRUN | SDMMC_INT_HLE )
85
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 )
90     #else
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 )
93     #endif
94 #else
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 )
98     #else
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 )
101     #endif
102 #endif
103
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.
108
109 #define RK29_SDMMC_VERSION "Ver.5.05 The last modify date is 2013-05-08"
110
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
115 #else
116 #define RK29_CTRL_SDMMC_ID   5  
117 #define RK29_CTRL_SDIO1_ID   1  
118 #define RK29_CTRL_SDIO2_ID   2  
119 #endif
120
121 #define SDMMC_CLOCK_TEST     0
122
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*/
125
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.
129 #else
130 #define RK29_MAX_SDIO_FREQ   25000000    //set max-sdio-frequency 25Mhz at the present time
131 #endif
132
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
136 #else
137 #define DRIVER_SDMMC_USE_NEW_IOMUX_API 0
138 #endif
139
140 enum {
141         EVENT_CMD_COMPLETE = 0,
142         EVENT_DATA_COMPLETE,
143         EVENT_DATA_UNBUSY,
144         EVENT_DATA_ERROR,
145         EVENT_XFER_ERROR
146 };
147
148 enum rk29_sdmmc_state {
149         STATE_IDLE = 0,
150         STATE_SENDING_CMD,
151         STATE_DATA_BUSY,
152         STATE_DATA_UNBUSY,
153         STATE_DATA_END,
154         STATE_SENDING_STOP,
155 };
156
157 struct rk29_sdmmc_dma_info {
158         enum dma_ch chn;
159         char *name;
160         struct rk29_dma_client client;
161 };
162
163 static struct rk29_sdmmc_dma_info rk29_sdmmc_dma_infos[]= {
164         {
165                 .chn = DMACH_SDMMC,
166                 .client = {
167                         .name = "rk29-dma-sdmmc0",
168                 }
169         },
170         {
171                 .chn = DMACH_SDIO,
172                 .client = {
173                         .name = "rk29-dma-sdio1",
174                 }
175         },
176
177         {
178                 .chn = DMACH_EMMC,
179                 .client = {
180                         .name = "rk29-dma-sdio2",
181                 }
182         },
183 };
184
185
186 /* Interrupt Information */
187 typedef struct TagSDC_INT_INFO
188 {
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.
192 }SDC_INT_INFO_T;
193
194
195 struct rk29_sdmmc {
196         spinlock_t              lock;
197         void __iomem    *regs;
198         struct clk              *clk;
199
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;
205
206         dma_addr_t              dma_addr;;
207         unsigned int    use_dma:1;
208         char                    dma_name[8];
209         u32                     cmd_status;
210         u32                     data_status;
211         u32                     stop_cmdr;
212
213     u32         old_div;
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.
218         u32         *pbuf;
219         SDC_INT_INFO_T    intInfo; 
220     struct rk29_sdmmc_dma_info  dma_info;
221     int         irq;
222         int error_times;
223         u32 old_cmd;
224         
225         struct tasklet_struct   tasklet;
226         unsigned long           pending_events;
227         unsigned long           completed_events;
228         enum rk29_sdmmc_state   state;
229
230 #ifdef RK29_SDMMC_LIST_QUEUE
231         struct list_head        queue;
232         struct list_head        queue_node;
233 #endif
234
235         u32                     bus_hz;
236         struct platform_device  *pdev;
237         struct mmc_host         *mmc;
238         u32                     ctype;
239         unsigned int            clock;
240         unsigned long           flags;
241         
242 #define RK29_SDMMC_CARD_PRESENT 0
243
244         int                     id;
245
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;
251
252         /* flag for current bus settings */
253     u32 bus_mode;
254
255     unsigned int            oldstatus;
256     unsigned int            complete_done;
257     unsigned int            retryfunc;
258     
259     int gpio_irq;
260         int gpio_power_en;
261         int gpio_power_en_level;
262         struct delayed_work             work;
263
264 #ifdef CONFIG_RK29_SDIO_IRQ_FROM_GPIO
265     unsigned int sdio_INT_gpio;
266     unsigned int sdio_irq;
267     unsigned long trigger_level;
268 #endif
269
270 #if defined(CONFIG_SDMMC0_RK29_WRITE_PROTECT) || defined(CONFIG_SDMMC1_RK29_WRITE_PROTECT)
271     int write_protect;
272     int protect_level;
273 #endif
274
275         bool                    irq_state;
276     void (*set_iomux)(int device_id, unsigned int bus_width);
277
278 };
279
280
281 #ifdef RK29_SDMMC_NOTIFY_REMOVE_INSERTION
282 static struct rk29_sdmmc    *globalSDhost[3];
283 #endif
284
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)
295
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);
299
300 static void rk29_sdmmc_write(unsigned char  __iomem     *regbase, unsigned int regOff,unsigned int val)
301 {
302         __raw_writel(val,regbase + regOff);
303 }
304
305 static unsigned int rk29_sdmmc_read(unsigned char  __iomem      *regbase, unsigned int regOff)
306 {
307         return __raw_readl(regbase + regOff);
308 }
309
310 static int rk29_sdmmc_regs_printk(struct rk29_sdmmc *host)
311 {
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));
338
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));
345 #endif  
346         printk("=======printk %s-register end =========\n", host->dma_name);
347         return 0;
348 }
349
350 static void rk29_sdmmc_enable_irq(struct rk29_sdmmc *host, bool irqflag)
351 {
352         unsigned long flags;
353
354     if(!host)
355     {
356         return;
357     }
358     
359         local_irq_save(flags);
360         if(host->irq_state != irqflag)
361         {
362             host->irq_state = irqflag;
363             if(irqflag)
364             {
365                 enable_irq(host->irq);
366             }
367             else
368             {
369                 disable_irq(host->irq);
370             }
371         }
372         local_irq_restore(flags);
373 }
374
375
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)
379 {
380     struct rk29_sdmmc   *host = NULL;
381     static u32 unmounting_times = 0;
382     static char oldbuf[64];
383     
384     if( !strncmp(buf,"version" , strlen("version")))
385     {
386         printk(KERN_INFO "\n The driver SDMMC named 'rk29_sdmmc.c' is %s.\n", RK29_SDMMC_VERSION);
387         return count;
388     }
389     
390     //envalue the address of host base on input-parameter.
391     if( !strncmp(buf,"sd-" , strlen("sd-")) )
392     {
393         host = (struct rk29_sdmmc       *)globalSDhost[0];
394         if(!host)
395         {
396             printk(KERN_WARNING "%s..%d.. fail to call progress_store because the host is null. \n",__FUNCTION__,__LINE__);
397             return count;
398         }
399     }    
400     else if(  !strncmp(buf,"sdio1-" , strlen("sdio1-")) )
401     {
402         host = (struct rk29_sdmmc       *)globalSDhost[RK29_CTRL_SDIO1_ID];
403         if(!host)
404         {
405             printk(KERN_WARNING "%s..%d.. fail to call progress_store because the host-sdio1 is null.\n",__FUNCTION__,__LINE__);
406             return count;
407         }
408     }
409     else if(  !strncmp(buf,"sdio2-" , strlen("sdio2-")) )
410     {
411         host = (struct rk29_sdmmc       *)globalSDhost[RK29_CTRL_SDIO2_ID];
412         if(!host)
413         {
414             printk(KERN_WARNING "%s..%d.. fail to call progress_store because the host-sdio2 is null.\n",__FUNCTION__,__LINE__);
415             return count;
416         }
417     }
418     else
419     {
420         printk(KERN_WARNING "%s..%d.. You want to use sysfs for SDMMC but input-parameter is wrong.\n",__FUNCTION__,__LINE__);
421         return count;
422     }
423     rk29_sdmmc_enable_irq(host, false);
424
425     //spin_lock(&host->lock);
426     if(strncmp(buf,oldbuf , strlen(buf)))
427     {
428             printk(KERN_INFO ".%d.. MMC0 receive the message %s from VOLD.[%s]\n", __LINE__, buf, host->dma_name);
429             strcpy(oldbuf, buf);
430         }
431
432         /*
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
436     */
437     #if !defined(CONFIG_USE_SDMMC0_FOR_WIFI_DEVELOP_BOARD)
438     if(RK29_CTRL_SDMMC_ID == host->pdev->id)
439     {
440         if(!strncmp(buf, "sd-Unmounting", strlen("sd-Unmounting")))
441         {
442             if(unmounting_times++%10 == 0)
443             {
444                 printk(KERN_INFO ".%d.. MMC0 receive the message Unmounting(waitTimes=%d) from VOLD.[%s]\n", \
445                     __LINE__, unmounting_times, host->dma_name);
446             }
447
448             if(0 == host->mmc->re_initialized_flags)
449                 mod_timer(&host->detect_timer, jiffies + msecs_to_jiffies(RK29_SDMMC_REMOVAL_DELAY*2));
450         }
451         else if(!strncmp(buf, "sd-Idle-Unmounted", strlen("sd-Idle-Unmounted")))
452         {
453             if(0 == host->mmc->re_initialized_flags)
454                 mod_timer(&host->detect_timer, jiffies + msecs_to_jiffies(RK29_SDMMC_REMOVAL_DELAY*2));
455         }
456         else if( !strncmp(buf, "sd-No-Media", strlen("sd-No-Media")))
457         {
458             printk(KERN_INFO ".%d.. MMC0 receive the message No-Media from VOLD. waitTimes=%d [%s]\n" ,\
459                 __LINE__,unmounting_times, host->dma_name);
460                 
461             del_timer_sync(&host->detect_timer);
462             host->mmc->re_initialized_flags = 1;
463             unmounting_times = 0;
464             
465             if(test_bit(RK29_SDMMC_CARD_PRESENT, &host->flags))
466             {
467                 mmc_detect_change(host->mmc, 0);
468             }
469         }
470         else if( !strncmp(buf, "sd-Ready", strlen("sd-Ready")))
471         {
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);
474                                                                 
475             unmounting_times = 0;
476                         host->mmc->re_initialized_flags = 1;            
477         }
478         else if( !strncmp(buf,"sd-reset" , strlen("sd-reset")) ) 
479         {
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);           
483         }
484         else if( !strncmp(buf, "sd-regs", strlen("sd-regs")))
485         {
486             printk(KERN_INFO ".%d.. Now printk the register of SDMMC0. [%s]\n",__LINE__, host->dma_name); 
487             rk29_sdmmc_regs_printk(host);
488         }
489
490     }
491     #else
492     if(0 == host->pdev->id)
493     {
494         if( !strncmp(buf,"sd-reset" , strlen("sd-reset")) ) 
495         {
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);           
499         }
500         else if( !strncmp(buf, "sd-regs", strlen("sd-regs")))
501         {
502             printk(KERN_INFO ".%d.. Now printk the register of SDMMC0. [%s]\n",__LINE__, host->dma_name); 
503             rk29_sdmmc_regs_printk(host);
504         }
505     }
506     #endif
507     else if(RK29_CTRL_SDIO1_ID == host->pdev->id)
508     {
509         if( !strncmp(buf, "sdio1-regs", strlen("sdio1-regs")))
510         {
511             printk(KERN_INFO ".%d.. Now printk the register of SDMMC1. [%s]\n",__LINE__, host->dma_name); 
512             rk29_sdmmc_regs_printk(host);
513         }
514         else if( !strncmp(buf,"sdio1-reset" , strlen("sdio1-reset")) ) 
515         {
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);           
519         }
520     }
521     else if(RK29_CTRL_SDIO2_ID == host->pdev->id)
522     {
523         if( !strncmp(buf, "sdio2-regs", strlen("sdio2-regs")))
524         {
525             printk(KERN_INFO ".%d.. Now printk the register of SDMMC2. [%s]\n",__LINE__, host->dma_name); 
526             rk29_sdmmc_regs_printk(host);
527         }
528         else if( !strncmp(buf,"sdio2-reset" , strlen("sdio2-reset")) ) 
529         {
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);           
533         }
534     }
535     
536    // spin_unlock(&host->lock);
537     
538     rk29_sdmmc_enable_irq(host, true);    
539     return count;
540 }
541
542
543
544 struct kobj_attribute mmc_reset_attrs = 
545 {
546         .attr = {
547                 .name = "rescan",
548                 .mode = 0764},
549         .show = NULL,
550         .store = rk29_sdmmc_progress_store,
551 };
552 struct attribute *mmc_attrs[] = 
553 {
554         &mmc_reset_attrs.attr,
555         NULL
556 };
557
558 static struct kobj_type mmc_kset_ktype = {
559         .sysfs_ops      = &kobj_sysfs_ops,
560         .default_attrs = &mmc_attrs[0],
561 };
562
563 static int rk29_sdmmc_progress_add_attr( struct platform_device *pdev )
564 {
565         int result;
566                  struct kobject *parentkobject; 
567         struct kobject * me = kmalloc(sizeof(struct kobject) , GFP_KERNEL );
568         if(!me)
569         {
570             return -ENOMEM;
571         }
572         memset(me ,0,sizeof(struct kobject));
573         kobject_init( me , &mmc_kset_ktype );
574         
575         parentkobject = &pdev->dev.kobj ;
576                 result = kobject_add( me , parentkobject->parent->parent->parent,"%s", "sd-sdio" );     
577
578         return result;
579 }
580 #endif
581
582 #if defined (CONFIG_DEBUG_FS)
583 static int rk29_sdmmc_regs_show(struct seq_file *s, void *v)
584 {
585         struct rk29_sdmmc       *host = s->private;
586
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));
612
613         return 0;
614 }
615
616
617 /*
618  * The debugfs stuff below is mostly optimized away when
619  * CONFIG_DEBUG_FS is not set.
620  */
621 static int rk29_sdmmc_req_show(struct seq_file *s, void *v)
622 {
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;
628
629         /* Make sure we get a consistent snapshot */
630         spin_lock(&host->lock);
631         
632         mrq = host->mrq;
633
634         if (mrq) {
635                 cmd = mrq->cmd;
636                 data = mrq->data;
637                 stop = mrq->stop;
638
639                 if (cmd)
640                         seq_printf(s,
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);
645                 if (data)
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);
649                 if (stop)
650                         seq_printf(s,
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);
655         }
656
657         spin_unlock(&host->lock);
658
659         return 0;
660 }
661
662 static int rk29_sdmmc_req_open(struct inode *inode, struct file *file)
663 {
664         return single_open(file, rk29_sdmmc_req_show, inode->i_private);
665 }
666
667 static const struct file_operations rk29_sdmmc_req_fops = {
668         .owner          = THIS_MODULE,
669         .open           = rk29_sdmmc_req_open,
670         .read           = seq_read,
671         .llseek         = seq_lseek,
672         .release        = single_release,
673 };
674
675
676 static int rk29_sdmmc_regs_open(struct inode *inode, struct file *file)
677 {
678         return single_open(file, rk29_sdmmc_regs_show, inode->i_private);
679 }
680
681 static const struct file_operations rk29_sdmmc_regs_fops = {
682         .owner          = THIS_MODULE,
683         .open           = rk29_sdmmc_regs_open,
684         .read           = seq_read,
685         .llseek         = seq_lseek,
686         .release        = single_release,
687 };
688
689 static void rk29_sdmmc_init_debugfs(struct rk29_sdmmc *host)
690 {
691         struct mmc_host         *mmc = host->mmc;
692         struct dentry           *root;
693         struct dentry           *node;
694
695         root = mmc->debugfs_root;
696         if (!root)
697                 return;
698
699         node = debugfs_create_file("regs", S_IRUSR, root, host,
700                         &rk29_sdmmc_regs_fops);
701         if (IS_ERR(node))
702                 return;
703         if (!node)
704                 goto err;
705
706         node = debugfs_create_file("req", S_IRUSR, root, host, &rk29_sdmmc_req_fops);
707         if (!node)
708                 goto err;
709
710         node = debugfs_create_u32("state", S_IRUSR, root, (u32 *)&host->state);
711         if (!node)
712                 goto err;
713
714         node = debugfs_create_x32("pending_events", S_IRUSR, root,
715                                      (u32 *)&host->pending_events);
716         if (!node)
717                 goto err;
718
719         node = debugfs_create_x32("completed_events", S_IRUSR, root,
720                                      (u32 *)&host->completed_events);
721         if (!node)
722                 goto err;
723
724         return;
725
726 err:
727         dev_err(&mmc->class_dev, "failed to initialize debugfs for host\n");
728 }
729 #endif
730
731 static u32 rk29_sdmmc_prepare_command(struct mmc_command *cmd)
732 {
733         u32             cmdr = cmd->opcode;
734
735         switch(cmdr)
736         {
737             case MMC_GO_IDLE_STATE: 
738             cmdr |= (SDMMC_CMD_INIT | SDMMC_CMD_PRV_DAT_NO_WAIT);
739             break;
740
741         case MMC_STOP_TRANSMISSION:
742             cmdr |= (SDMMC_CMD_STOP | SDMMC_CMD_PRV_DAT_NO_WAIT);
743             break;
744         case MMC_SEND_STATUS:
745         case MMC_GO_INACTIVE_STATE:   
746             cmdr |= SDMMC_CMD_PRV_DAT_NO_WAIT;
747             break;
748
749         default:
750             cmdr |= SDMMC_CMD_PRV_DAT_WAIT;
751             break;
752         }
753
754     /* response type */
755         switch(mmc_resp_type(cmd))
756         {
757             case MMC_RSP_R1:
758         case MMC_RSP_R1B:
759             // case MMC_RSP_R5:  //R5,R6,R7 is same with the R1
760             //case MMC_RSP_R6:
761             // case R6m_TYPE:
762             // case MMC_RSP_R7:
763             cmdr |= (SDMMC_CMD_RESP_CRC | SDMMC_CMD_RESP_SHORT | SDMMC_CMD_RESP_EXP);
764             break;
765         case MMC_RSP_R3:
766             //case MMC_RSP_R4:
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); 
769             break;
770         case MMC_RSP_R2:
771             cmdr |= (SDMMC_CMD_RESP_CRC | SDMMC_CMD_RESP_LONG | SDMMC_CMD_RESP_EXP);
772             break;
773         case MMC_RSP_NONE:
774             cmdr |= (SDMMC_CMD_RESP_CRC_NOCARE | SDMMC_CMD_RESP_NOCARE | SDMMC_CMD_RESP_NO_EXP);  
775             break;
776         default:
777             cmdr |= (SDMMC_CMD_RESP_CRC_NOCARE | SDMMC_CMD_RESP_NOCARE | SDMMC_CMD_RESP_NO_EXP); 
778             break;
779         }
780
781         return cmdr;
782 }
783
784 void  rk29_sdmmc_set_frq(struct rk29_sdmmc *host)
785 {
786     struct mmc_host *mmchost = platform_get_drvdata(host->pdev);
787     struct mmc_card     *card;
788     struct mmc_ios *ios;
789         unsigned int max_dtr;
790     
791     extern void mmc_set_clock(struct mmc_host *host, unsigned int hz);
792
793     if(!mmchost)
794         return;
795
796     card = (struct mmc_card     *)mmchost->card;
797     ios  = ( struct mmc_ios *)&mmchost->ios;
798
799     if(!card || !ios)
800         return;
801
802     if(MMC_POWER_ON == ios->power_mode)
803         return;
804
805     max_dtr = (unsigned int)-1;
806     
807     if (mmc_card_highspeed(card)) 
808     {
809         if (max_dtr > card->ext_csd.hs_max_dtr)
810             max_dtr = card->ext_csd.hs_max_dtr;
811             
812     }
813     else if (max_dtr > card->csd.max_dtr) 
814     {
815         if(MMC_TYPE_SD == card->type)
816         {
817                 max_dtr = (card->csd.max_dtr > SD_FPP_FREQ) ? SD_FPP_FREQ : (card->csd.max_dtr);
818             }
819             else
820             {   
821             max_dtr = (card->csd.max_dtr > MMC_FPP_FREQ) ? MMC_FPP_FREQ : (card->csd.max_dtr);
822             }       
823     }
824
825     xbwprintk(7, "%s..%d...  call mmc_set_clock() set clk=%d [%s]\n", \
826                         __FUNCTION__, __LINE__, max_dtr, host->dma_name);
827
828   
829     mmc_set_clock(mmchost, max_dtr);
830
831 }
832
833
834 static int rk29_sdmmc_start_command(struct rk29_sdmmc *host, struct mmc_command *cmd, u32 cmd_flags)
835 {
836         int tmo = RK29_SDMMC_SEND_START_TIMEOUT*10;//wait 60ms cycle.
837         
838         host->cmd = cmd;
839         host->old_cmd = cmd->opcode;
840         host->errorstep = 0;
841         host->pending_events = 0;
842         host->completed_events = 0;             
843     host->retryfunc = 0;
844     host->cmd_status = 0;
845         
846     if(MMC_STOP_TRANSMISSION != cmd->opcode)
847     {
848         host->data_status = 0;
849     }
850     
851     if(RK29_CTRL_SDMMC_ID == host->pdev->id)
852     {
853         //adjust the frequency division control of SDMMC0 every time.
854         rk29_sdmmc_set_frq(host);
855     }
856                         
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
860 #else
861     rk29_sdmmc_write(host->regs, SDMMC_CMD, cmd_flags | SDMMC_CMD_USE_HOLD_REG |SDMMC_CMD_START); // write to SDMMC_CMD register
862 #endif
863
864     xbwprintk(1,"\n%s..%d..************.start cmd=%d, arg=0x%x ********  [%s]\n", \
865                         __FUNCTION__, __LINE__, cmd->opcode, cmd->arg, host->dma_name);
866
867
868         /* wait until CIU accepts the command */
869         while (--tmo && (rk29_sdmmc_read(host->regs, SDMMC_CMD) & SDMMC_CMD_START))
870         {
871                 udelay(2);//cpu_relax();
872         }
873
874         host->complete_done = 0;
875         host->mmc->doneflag = 1;        
876         
877         if(!tmo)
878         {
879             if(0==cmd->retries)
880             {
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);
883                 }
884
885                 cmd->error = -ETIMEDOUT;
886                 host->mrq->cmd->error = -ETIMEDOUT;
887                 del_timer_sync(&host->request_timer);
888                 
889                 host->errorstep = 0x1;
890                 return SDM_WAIT_FOR_CMDSTART_TIMEOUT;
891         }
892     host->errorstep = 0xfe;
893  
894         return SDM_SUCCESS;
895 }
896
897 static int rk29_sdmmc_reset_fifo(struct rk29_sdmmc *host)
898 {
899     u32     value;
900         int     timeout;
901         int     ret = SDM_SUCCESS;
902         
903     value = rk29_sdmmc_read(host->regs, SDMMC_STATUS);
904     if (!(value & SDMMC_STAUTS_FIFO_EMPTY))
905     {
906         value = rk29_sdmmc_read(host->regs, SDMMC_CTRL);
907         value |= SDMMC_CTRL_FIFO_RESET;
908         rk29_sdmmc_write(host->regs, SDMMC_CTRL, value);
909
910         timeout = 1000;
911         while (((value = rk29_sdmmc_read(host->regs, SDMMC_CTRL)) & (SDMMC_CTRL_FIFO_RESET)) && (timeout > 0))
912         {
913             udelay(1);
914             timeout--;
915         }
916         if (timeout == 0)
917         {
918             host->errorstep = 0x2;
919             ret = SDM_WAIT_FOR_FIFORESET_TIMEOUT;
920         }
921     }
922     
923         return ret;
924         
925 }
926
927 static int rk29_sdmmc_wait_unbusy(struct rk29_sdmmc *host)
928 {
929     int time_out = 500000;//250000; //max is 250ms; //adapt the value to the sick card.  modify at 2011-10-08
930
931 #if SDMMC_USE_INT_UNBUSY
932     if((24==host->cmd->opcode)||(25==host->cmd->opcode))
933         return SDM_SUCCESS;
934 #endif  
935         while (rk29_sdmmc_read(host->regs, SDMMC_STATUS) & (SDMMC_STAUTS_DATA_BUSY|SDMMC_STAUTS_MC_BUSY)) 
936         {
937                 udelay(1);
938                 time_out--;
939
940                 if(time_out == 0)
941                 {
942                     host->errorstep = 0x3;
943                     return SDM_BUSY_TIMEOUT;
944                 }
945         }
946
947         return SDM_SUCCESS;
948 }
949
950 static void rk29_sdmmc_dma_cleanup(struct rk29_sdmmc *host)
951 {
952         if (host->data) 
953         {
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));              
957     }
958 }
959
960 static void rk29_sdmmc_stop_dma(struct rk29_sdmmc *host)
961 {
962     int ret = 0;
963     
964         if(host->use_dma == 0)
965                 return;
966                 
967         if (host->dma_info.chn> 0) 
968         {
969                 rk29_sdmmc_dma_cleanup(host); 
970                 
971                 ret = rk29_dma_ctrl(host->dma_info.chn,RK29_DMAOP_STOP);
972                 if(ret < 0)
973                 {
974             printk(KERN_WARNING "%s..%d...rk29_dma_ctrl STOP error! [%s]\n", __FUNCTION__, __LINE__, host->dma_name);
975             host->errorstep = 0x95;
976             return;
977                 }
978
979                 ret = rk29_dma_ctrl(host->dma_info.chn,RK29_DMAOP_FLUSH);
980                 if(ret < 0)
981                 {
982             printk(KERN_WARNING "%s..%d...rk29_dma_ctrl FLUSH error! [%s]\n", __FUNCTION__, __LINE__, host->dma_name);
983             host->errorstep = 0x96;
984             return;
985                 }
986                 
987         } 
988         else 
989         {
990                 /* Data transfer was stopped by the interrupt handler */
991                 rk29_sdmmc_set_pending(host, EVENT_DATA_COMPLETE);
992         }
993 }
994
995 static void rk29_sdmmc_control_host_dma(struct rk29_sdmmc *host, bool enable)
996 {
997     u32 value = rk29_sdmmc_read(host->regs, SDMMC_CTRL);
998
999     if (enable)
1000     {
1001         value |= SDMMC_CTRL_DMA_ENABLE;
1002     }
1003     else
1004     {
1005         value &= ~(SDMMC_CTRL_DMA_ENABLE);
1006     }
1007
1008     rk29_sdmmc_write(host->regs, SDMMC_CTRL, value);
1009 }
1010
1011 static void send_stop_cmd(struct rk29_sdmmc *host)
1012 {
1013     int ret, i;
1014    // int timeout = 250;
1015    // unsigned int value;
1016
1017
1018     if(host->mrq->cmd->error)
1019     {
1020         //stop DMA
1021         if(host->dodma)
1022         {
1023             rk29_sdmmc_stop_dma(host);
1024             rk29_sdmmc_control_host_dma(host, FALSE);
1025
1026             host->dodma = 0;
1027         }
1028         
1029         ret= rk29_sdmmc_clear_fifo(host);
1030         if(SDM_SUCCESS != ret)
1031         {
1032             xbwprintk(3, "%s..%d..  clear fifo error before call CMD_STOP [%s]\n", \
1033                                                         __FUNCTION__, __LINE__, host->dma_name);
1034         }
1035     }
1036
1037     i = 0;
1038     while(++i>2)
1039     {
1040         ret = rk29_sdmmc_wait_unbusy(host);
1041         if(SDM_SUCCESS == ret)
1042             break;
1043        
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);
1046
1047     }
1048
1049
1050     host->errorstep = 0xe1;     
1051     mod_timer(&host->request_timer, jiffies + msecs_to_jiffies(RK29_SDMMC_SEND_START_TIMEOUT+2500));
1052                 
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)
1061     {
1062         host->mrq->stop->error = 0;
1063     }
1064     
1065     host->cmdr = rk29_sdmmc_prepare_command(&host->stopcmd);
1066     
1067     ret = rk29_sdmmc_start_command(host, &host->stopcmd, host->cmdr); 
1068     if(SDM_SUCCESS != ret)
1069     {
1070         rk29_sdmmc_start_error(host);
1071
1072         host->state = STATE_IDLE;
1073         host->complete_done = 4;
1074     }
1075     host->errorstep = 0xe2;
1076 }
1077
1078
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) 
1081 {
1082         struct rk29_sdmmc       *host = arg;
1083
1084         if(host->use_dma == 0)
1085                 return;
1086
1087         host->intInfo.transLen = host->intInfo.desLen;  
1088 }
1089
1090 static int rk29_sdmmc_read_data_pio(struct rk29_sdmmc *host)
1091 {
1092         struct scatterlist      *sg;
1093         u32                     *buf;
1094         unsigned int            offset = 0;
1095         struct mmc_data         *data;
1096         u32                     value;
1097         unsigned int            nbytes = 0;
1098     int remaining;
1099
1100     value = rk29_sdmmc_read(host->regs, SDMMC_STATUS);
1101         if ( value& SDMMC_STAUTS_FIFO_EMPTY)
1102             goto done;
1103
1104     if(NULL == host->data)
1105        goto done; 
1106
1107     if((NULL == host)&&(NULL == host->data))
1108         goto done;
1109
1110     data = host->data;
1111         sg = host->sg;
1112         buf = (u32 *)sg_virt(sg);
1113         
1114     while ( (host->intInfo.transLen < host->intInfo.desLen)  && (!(value & SDMMC_STAUTS_FIFO_EMPTY)) )
1115     {
1116         if( ((offset + (host->intInfo.desLen - host->intInfo.transLen))<<2) <= sg->length )
1117         {
1118             buf[offset] = rk29_sdmmc_read(host->regs, SDMMC_DATA);
1119             offset ++;
1120                 nbytes += 4;
1121                 host->intInfo.transLen++;
1122
1123                 if ((offset<<2) == sg->length) 
1124                 {
1125                                 flush_dcache_page(sg_page(sg));
1126                                 host->sg = sg = sg_next(sg);
1127                                 if (!sg)
1128                                         goto done;
1129
1130                                 offset = 0;
1131                                 buf = (u32 *)sg_virt(sg);
1132                         }
1133                 
1134         }
1135         else
1136         {
1137             remaining = (sg->length>>2) - offset;
1138             while( remaining>0)
1139             {
1140                 buf[offset] = rk29_sdmmc_read(host->regs, SDMMC_DATA);
1141                 offset ++;
1142                     nbytes += 4;
1143                     remaining --;
1144                     host->intInfo.transLen++;
1145             }
1146
1147             flush_dcache_page(sg_page(sg));
1148
1149                 host->sg = sg = sg_next(sg);
1150                 if (!sg)
1151                         goto done;
1152
1153             offset = 0;
1154                 buf = (u32 *)sg_virt(sg);                   
1155         }
1156
1157         data->bytes_xfered += nbytes;
1158
1159         value = rk29_sdmmc_read(host->regs, SDMMC_STATUS);
1160     }
1161
1162 done:
1163     return 0;
1164 }
1165
1166
1167 static int rk29_sdmmc_write_data_pio(struct rk29_sdmmc *host)
1168 {
1169         struct scatterlist      *sg;
1170         u32                     *buf;
1171         unsigned int            offset = 0;
1172         struct mmc_data         *data;
1173         u32                     value;
1174         unsigned int            nbytes = 0;
1175     int remaining;
1176
1177     value = rk29_sdmmc_read(host->regs, SDMMC_STATUS);
1178         if ( value& SDMMC_STAUTS_FIFO_EMPTY)
1179             goto done;
1180
1181     if(NULL == host->data)
1182        goto done; 
1183
1184     if((NULL == host)&&(NULL == host->data))
1185         goto done;
1186
1187     data = host->data;
1188         sg = host->sg;
1189         buf = (u32 *)sg_virt(sg);
1190         
1191     while ( (host->intInfo.transLen < host->intInfo.desLen)  && (!(value & SDMMC_STAUTS_FIFO_EMPTY)) )
1192     {
1193         if( ((offset + (host->intInfo.desLen - host->intInfo.transLen))<<2) <= sg->length )
1194         {
1195             rk29_sdmmc_write(host->regs, SDMMC_DATA, buf[offset]);
1196             offset ++;
1197                 nbytes += 4;
1198                 host->intInfo.transLen++;
1199
1200                 if ((offset<<2) == sg->length) 
1201                 {
1202                                 host->sg = sg = sg_next(sg);
1203                                 if (!sg)
1204                                         goto done;
1205
1206                                 offset = 0;
1207                                 buf = (u32 *)sg_virt(sg);
1208                         }
1209                 
1210         }
1211         else
1212         {
1213             remaining = (sg->length>>2) - offset;
1214             while( remaining>0)
1215             {
1216                 rk29_sdmmc_write(host->regs, SDMMC_DATA, buf[offset]);
1217                 offset ++;
1218                     nbytes += 4;
1219                     remaining --;
1220                     host->intInfo.transLen++;
1221             }
1222
1223                 host->sg = sg = sg_next(sg);
1224                 if (!sg)
1225                         goto done;
1226
1227             offset = 0;
1228                 buf = (u32 *)sg_virt(sg);                   
1229         }
1230
1231         data->bytes_xfered += nbytes;
1232
1233         value = rk29_sdmmc_read(host->regs, SDMMC_STATUS);
1234     }
1235     
1236 done:
1237     return 0;
1238 }
1239
1240 static int rk29_sdmmc_submit_data_dma(struct rk29_sdmmc *host, struct mmc_data *data)
1241 {
1242         unsigned int                    i,direction, sgDirection;
1243         int ret, dma_len=0;
1244         
1245         if(host->use_dma == 0)
1246         {
1247             printk(KERN_WARNING "%s..%d...setup DMA fail!!!!!!. host->use_dma=0 [%s]\n", __FUNCTION__, __LINE__, host->dma_name);
1248             host->errorstep = 0x4;
1249                 return -ENOSYS;
1250         }
1251         /* If we don't have a channel, we can't do DMA */
1252         if (host->dma_info.chn < 0)
1253         {
1254             printk(KERN_WARNING "%s..%d...setup DMA fail!!!!!!!. dma_info.chn < 0  [%s]\n", __FUNCTION__, __LINE__, host->dma_name);
1255             host->errorstep = 0x5;
1256                 return -ENODEV;
1257         }
1258
1259         if (data->blksz & 3)
1260         {
1261             printk(KERN_ERR "%s..%d...data_len not aligned to 4bytes.  [%s]\n", \
1262                 __FUNCTION__, __LINE__, host->dma_name);
1263                 
1264                 return -EINVAL;
1265     }
1266
1267         if (data->flags & MMC_DATA_READ)
1268         {
1269                 direction = RK29_DMASRC_HW;  
1270                 sgDirection = DMA_FROM_DEVICE; 
1271         }
1272         else
1273         {
1274                 direction = RK29_DMASRC_MEM;
1275                 sgDirection = DMA_TO_DEVICE;
1276         }
1277
1278         ret = rk29_dma_ctrl(host->dma_info.chn,RK29_DMAOP_STOP);
1279         if(ret < 0)
1280         {
1281             printk(KERN_WARNING "%s..%d...rk29_dma_ctrl stop error![%s]\n", __FUNCTION__, __LINE__, host->dma_name);
1282             host->errorstep = 0x91;
1283                 return -ENOSYS;
1284         }
1285         
1286         ret = rk29_dma_ctrl(host->dma_info.chn,RK29_DMAOP_FLUSH);       
1287         if(ret < 0)
1288         {
1289         printk(KERN_WARNING "%s..%d...rk29_dma_ctrl flush error![%s]\n", __FUNCTION__, __LINE__, host->dma_name);
1290         host->errorstep = 0x91;
1291         return -ENOSYS;
1292         }
1293
1294         
1295     ret = rk29_dma_devconfig(host->dma_info.chn, direction, (unsigned long )(host->dma_addr));
1296     if(0 != ret)
1297     {
1298         printk(KERN_WARNING "%s..%d...call rk29_dma_devconfig() fail ![%s]\n", __FUNCTION__, __LINE__, host->dma_name);
1299         host->errorstep = 0x8;
1300         return -ENOSYS;
1301     }
1302     
1303         dma_len = dma_map_sg(&host->pdev->dev, data->sg, data->sg_len, sgDirection);                                                                       
1304         for (i = 0; i < dma_len; i++)
1305         {
1306         ret = rk29_dma_enqueue(host->dma_info.chn, host, sg_dma_address(&data->sg[i]),sg_dma_len(&data->sg[i]));
1307         if(ret < 0)
1308         {
1309             printk(KERN_WARNING "%s..%d...call rk29_dma_devconfig() fail ![%s]\n", __FUNCTION__, __LINE__, host->dma_name);
1310             host->errorstep = 0x93;
1311             return -ENOSYS;
1312         }
1313     }
1314         
1315         rk29_sdmmc_control_host_dma(host, TRUE);// enable dma
1316         ret = rk29_dma_ctrl(host->dma_info.chn, RK29_DMAOP_START);
1317         if(ret < 0)
1318         {
1319         printk(KERN_WARNING "%s..%d...rk29_dma_ctrl start error![%s]\n", __FUNCTION__, __LINE__, host->dma_name);
1320         host->errorstep = 0x94;
1321         return -ENOSYS;
1322         }
1323         
1324         return 0;
1325 }
1326
1327
1328 static int rk29_sdmmc_prepare_write_data(struct rk29_sdmmc *host, struct mmc_data *data)
1329 {
1330     int     output;
1331     u32    i = 0;
1332     u32     dataLen;
1333     u32     count, *pBuf = (u32 *)host->pbuf;
1334
1335     output = SDM_SUCCESS;
1336     dataLen = data->blocks*data->blksz;
1337     
1338     host->dodma = 0; //DMA still no request;
1339  
1340     //SDMMC controller request the data is multiple of 4.
1341     count = (dataLen >> 2) + ((dataLen & 0x3) ? 1:0);
1342 #if 0
1343     if(count <= FIFO_DEPTH)    
1344 #else
1345     #if defined(CONFIG_ARCH_RK29)
1346     if( (count <= 0x20) && (RK29_CTRL_SDMMC_ID != host->pdev->id))
1347     #else
1348     if( (count <= 0x80) && (RK29_CTRL_SDMMC_ID != host->pdev->id))
1349     #endif
1350 #endif
1351     {
1352            
1353         #if 1
1354         for (i=0; i<count; i++)
1355         {
1356             rk29_sdmmc_write(host->regs, SDMMC_DATA, pBuf[i]);
1357         }
1358         #else
1359         rk29_sdmmc_write_data_pio(host);
1360         #endif
1361     }
1362     else
1363     {
1364         host->intInfo.desLen = count;
1365         host->intInfo.transLen = 0;
1366         host->intInfo.pBuf = (u32 *)pBuf;
1367         
1368         if(0)//(host->intInfo.desLen <= 512 ) 
1369         {  
1370             //use pio-mode          
1371             return SDM_SUCCESS;
1372         } 
1373         else 
1374         {
1375             xbwprintk(7, "%s..%d...   trace data,   [%s]\n", __FUNCTION__, __LINE__,  host->dma_name);
1376             output = rk29_sdmmc_submit_data_dma(host, data);
1377             if(output)
1378             {
1379                         host->dodma = 0;
1380                                 
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);
1383                     
1384                                 host->errorstep = 0x81;
1385
1386                         rk29_sdmmc_control_host_dma(host, FALSE); 
1387                 }
1388                 else
1389                 {
1390                     host->dodma = 1;
1391                 }
1392         }
1393        
1394     }
1395
1396     return output;
1397 }
1398
1399
1400
1401
1402 static int rk29_sdmmc_prepare_read_data(struct rk29_sdmmc *host, struct mmc_data *data)
1403 {
1404     u32  count = 0;
1405     u32  dataLen;
1406     int   output;
1407
1408     output = SDM_SUCCESS;
1409     dataLen = data->blocks*data->blksz;
1410     
1411     host->dodma = 0;//DMA still no request;
1412
1413     //SDMMC controller request the data is multiple of 4.
1414     count = (dataLen >> 2) ;//+ ((dataLen & 0x3) ? 1:0);
1415
1416     host->intInfo.desLen = (dataLen >> 2);
1417     host->intInfo.transLen = 0;
1418     host->intInfo.pBuf = (u32 *)host->pbuf;
1419        
1420     if(count > (RX_WMARK+1))  //datasheet error.actually, it can nont waken the interrupt when less and equal than RX_WMARK+1
1421     {
1422         if(0) //(host->intInfo.desLen <= 512 )
1423         {
1424             //use pio-mode
1425             return SDM_SUCCESS;
1426         }        
1427         else 
1428         {
1429             output = rk29_sdmmc_submit_data_dma(host, data);
1430             if(output)
1431             {
1432                         host->dodma = 0;
1433                                 
1434                     printk(KERN_WARNING "%s..%d... CMD%d setupDMA  failure!!!  [%s]\n", \
1435                                                 __FUNCTION__, __LINE__, host->cmd->opcode, host->dma_name);
1436
1437                     host->errorstep = 0x82;
1438
1439                         rk29_sdmmc_control_host_dma(host, FALSE); 
1440                 }
1441                 else
1442                 {
1443                     host->dodma = 1;
1444                 }
1445         }
1446     }
1447
1448     return output;
1449 }
1450
1451
1452
1453 static int rk29_sdmmc_read_remain_data(struct rk29_sdmmc *host, u32 originalLen, void *pDataBuf)
1454 {
1455         struct mmc_data         *data;  
1456     struct scatterlist  *sg;
1457     u32   *buf,  i = 0;
1458
1459     if(1 == host->dodma)
1460     {
1461         data = host->data;
1462         sg = host->sg;
1463         buf = (u32 *)sg_virt(sg);
1464
1465         for_each_sg(data->sg, sg, data->sg_len, i) 
1466         {
1467             if (!sg)
1468                 return -1;
1469             if (sg_is_last(sg))
1470                 break;
1471         }
1472
1473         flush_dcache_page(sg_page(sg));
1474         host->sg = sg;
1475     }
1476
1477     rk29_sdmmc_read_data_pio(host);
1478     
1479     return SDM_SUCCESS;
1480 }
1481
1482 static void rk29_sdmmc_submit_data(struct rk29_sdmmc *host, struct mmc_data *data)
1483 {
1484     int ret,i;
1485     struct scatterlist *sg;
1486     
1487     if(data)
1488     {
1489         host->data = data;
1490         data->error = 0;
1491         host->cmd->data = data;
1492         host->sg = data->sg;
1493         
1494         for_each_sg(data->sg, sg, data->sg_len, i) 
1495         {
1496                 if (sg->offset & 3 || sg->length & 3) 
1497                 {
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);
1502                         return ;
1503                 }
1504             }
1505
1506         data->bytes_xfered = 0;
1507         host->pbuf = (u32*)sg_virt(data->sg);
1508
1509         if (data->flags & MMC_DATA_STREAM)
1510                 {
1511                         host->cmdr |= SDMMC_CMD_STRM_MODE;    //set stream mode
1512                 }
1513                 else
1514                 {
1515                     host->cmdr |= SDMMC_CMD_BLOCK_MODE;   //set block mode
1516                 }
1517                 
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);
1521
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);
1524             
1525                 if (data->flags & MMC_DATA_WRITE)
1526                 {
1527                     host->cmdr |= (SDMMC_CMD_DAT_WRITE | SDMMC_CMD_DAT_EXP);
1528                     
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);
1533             }
1534             else
1535             {
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);
1539                 
1540                         ret = rk29_sdmmc_prepare_read_data(host, data);
1541             }
1542
1543     }
1544     else
1545     {
1546         rk29_sdmmc_write(host->regs, SDMMC_BLKSIZ, 0);
1547         rk29_sdmmc_write(host->regs, SDMMC_BYTCNT, 0);
1548     }
1549 }
1550
1551
1552 static int sdmmc_send_cmd_start(struct rk29_sdmmc *host, unsigned int cmd)
1553 {
1554         int tmo = RK29_SDMMC_SEND_START_TIMEOUT*10;//wait 60ms cycle.
1555
1556 #if defined(CONFIG_ARCH_RK29)           
1557         rk29_sdmmc_write(host->regs, SDMMC_CMD, SDMMC_CMD_START | cmd); 
1558 #else   
1559     rk29_sdmmc_write(host->regs, SDMMC_CMD, SDMMC_CMD_USE_HOLD_REG |SDMMC_CMD_START | cmd);
1560 #endif    
1561         while (--tmo && (rk29_sdmmc_read(host->regs, SDMMC_CMD) & SDMMC_CMD_START))
1562         {
1563             udelay(2);
1564         }
1565         
1566         if(!tmo && test_bit(RK29_SDMMC_CARD_PRESENT, &host->flags)) 
1567         {
1568                 printk(KERN_WARNING "%s.. %d   set cmd(value=0x%x) register timeout error !   [%s]\n",\
1569                                 __FUNCTION__,__LINE__, cmd, host->dma_name);
1570
1571                 host->errorstep = 0x9;
1572                 return SDM_START_CMD_FAIL;
1573         }
1574
1575         return SDM_SUCCESS;
1576 }
1577
1578 static int rk29_sdmmc_get_cd(struct mmc_host *mmc)
1579 {
1580         struct rk29_sdmmc *host = mmc_priv(mmc);
1581         u32 cdetect=1;
1582 #if defined(CONFIG_SDMMC0_RK29_SDCARD_DET_FROM_GPIO)    
1583     int i=0,cdetect1=0, cdetect2=0;
1584 #endif    
1585     switch(host->pdev->id)
1586     {
1587         case 0:
1588         {            
1589          #if defined(CONFIG_SDMMC0_RK29_SDCARD_DET_FROM_GPIO)
1590             if(host->det_pin.io == INVALID_GPIO)
1591                 return 1;
1592
1593             for(i=0;i<5;i++)
1594             {
1595                 udelay(10);
1596                 cdetect1 = gpio_get_value(host->det_pin.io);  
1597                 udelay(10);
1598                 cdetect2 = gpio_get_value(host->det_pin.io); 
1599                 if(cdetect1 == cdetect2)
1600                     break;
1601             }
1602             cdetect = cdetect2;          
1603             if(host->det_pin.enable)
1604                 cdetect = cdetect?1:0;
1605             else
1606                 cdetect = cdetect?0:1;
1607                 
1608          #else
1609                 if(host->det_pin.io == INVALID_GPIO)
1610                         return 1;
1611
1612                 cdetect = rk29_sdmmc_read(host->regs, SDMMC_CDETECT);
1613
1614             cdetect = (cdetect & SDMMC_CARD_DETECT_N)?0:1;
1615          #endif
1616          
1617             break;
1618         }        
1619
1620         case 1:
1621         {
1622             #if defined(CONFIG_USE_SDMMC1_FOR_WIFI_DEVELOP_BOARD)
1623             cdetect = 1;
1624             #else
1625             if(host->det_pin.io == INVALID_GPIO)
1626                         return 1;
1627                         
1628             cdetect = test_bit(RK29_SDMMC_CARD_PRESENT, &host->flags)?1:0;
1629             #endif
1630             break;
1631         }
1632         
1633         default:
1634             cdetect = 1;
1635             break;
1636     
1637         }
1638 #if defined(CONFIG_USE_SDMMC0_FOR_WIFI_DEVELOP_BOARD)
1639     set_bit(RK29_SDMMC_CARD_PRESENT, &host->flags);
1640     return 1;
1641 #endif
1642
1643          return cdetect;
1644 }
1645
1646
1647 /****************************************************************/
1648 //reset the SDMMC controller of the current host
1649 /****************************************************************/
1650 int rk29_sdmmc_reset_controller(struct rk29_sdmmc *host)
1651 {
1652     u32  value = 0;
1653     int  timeOut = 0;
1654
1655     rk29_sdmmc_write(host->regs, SDMMC_PWREN, POWER_ENABLE);
1656     
1657     //Clean the fifo.
1658     for(timeOut=0; timeOut<FIFO_DEPTH; timeOut++)
1659     {
1660         if(rk29_sdmmc_read(host->regs, SDMMC_STATUS) & SDMMC_STAUTS_FIFO_EMPTY)
1661             break;
1662             
1663         value = rk29_sdmmc_read(host->regs, SDMMC_DATA);
1664     }
1665    
1666     /* reset */
1667 #if defined(CONFIG_ARCH_RK29)     
1668     rk29_sdmmc_write(host->regs, SDMMC_CTRL,(SDMMC_CTRL_RESET | SDMMC_CTRL_FIFO_RESET ));
1669 #else
1670     rk29_sdmmc_write(host->regs, SDMMC_CTRL,(SDMMC_CTRL_RESET | SDMMC_CTRL_FIFO_RESET | SDMMC_CTRL_DMA_RESET));
1671 #endif
1672     timeOut = 1000;
1673     value = rk29_sdmmc_read(host->regs, SDMMC_CTRL);
1674     while (( value & (SDMMC_CTRL_FIFO_RESET | SDMMC_CTRL_RESET)) && (timeOut > 0))
1675     {
1676         udelay(1);
1677         timeOut--;
1678         value = rk29_sdmmc_read(host->regs, SDMMC_CTRL);
1679     }
1680
1681     if (timeOut == 0)
1682     {
1683         printk(KERN_WARNING "%s..%s..%d..  reset controller fail! [%s]=\n",\
1684                                 __FILE__, __FUNCTION__,__LINE__, host->dma_name);
1685
1686         host->errorstep = 0x0A;
1687         return SDM_WAIT_FOR_FIFORESET_TIMEOUT;
1688     }
1689
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)));
1692         
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))
1698     {
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);
1701         
1702                 host->errorstep = 0x0B;            
1703         return SDM_PARAM_ERROR;            
1704     }
1705
1706     rk29_sdmmc_write(host->regs, SDMMC_DEBNCE, (DEBOUNCE_TIME*host->bus_hz)&0xFFFFFF);
1707
1708     /* config interrupt */
1709     rk29_sdmmc_write(host->regs, SDMMC_RINTSTS, 0xFFFFFFFF);
1710
1711     if(host->use_dma)
1712     {
1713         if(RK29_CTRL_SDMMC_ID == host->pdev->id)
1714         {
1715                     rk29_sdmmc_write(host->regs, SDMMC_INTMASK,RK29_SDMMC_INTMASK_USEDMA);
1716                 }
1717                 else
1718                 {
1719                     if(0== host->pdev->id)
1720                     {
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);
1724                     #else
1725                     rk29_sdmmc_write(host->regs, SDMMC_INTMASK,RK29_SDMMC_INTMASK_USEDMA | SDMMC_INT_SDIO);
1726                         #endif                          
1727                     #else
1728                     rk29_sdmmc_write(host->regs, SDMMC_INTMASK,RK29_SDMMC_INTMASK_USEDMA);
1729                     #endif
1730                     }
1731                     else if(1== host->pdev->id)
1732                     {
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);
1736                     #else
1737                     rk29_sdmmc_write(host->regs, SDMMC_INTMASK,RK29_SDMMC_INTMASK_USEDMA | SDMMC_INT_SDIO);
1738                         #endif
1739                    #else
1740                     rk29_sdmmc_write(host->regs, SDMMC_INTMASK,RK29_SDMMC_INTMASK_USEDMA);
1741                    #endif 
1742                     }
1743                     else
1744                     {
1745                         #if defined(CONFIG_RK29_SDIO_IRQ_FROM_GPIO)
1746                 rk29_sdmmc_write(host->regs, SDMMC_INTMASK,RK29_SDMMC_INTMASK_USEDMA);
1747                 #else
1748                         rk29_sdmmc_write(host->regs, SDMMC_INTMASK,RK29_SDMMC_INTMASK_USEDMA | SDMMC_INT_SDIO);
1749                         #endif
1750                     }
1751                 }
1752         }
1753         else
1754         {
1755                 if(RK29_CTRL_SDMMC_ID == host->pdev->id)
1756         {
1757                     rk29_sdmmc_write(host->regs, SDMMC_INTMASK,RK29_SDMMC_INTMASK_USEIO);
1758                 }
1759                 else
1760                 {
1761                     if(0== host->pdev->id)
1762                     {
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);
1766                     #else
1767                     rk29_sdmmc_write(host->regs, SDMMC_INTMASK,RK29_SDMMC_INTMASK_USEIO | SDMMC_INT_SDIO);
1768                     #endif
1769                     #else
1770                     rk29_sdmmc_write(host->regs, SDMMC_INTMASK,RK29_SDMMC_INTMASK_USEIO);
1771                     #endif
1772                     }
1773                     else if(1== host->pdev->id)
1774                     {
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);
1778                     #else
1779                     rk29_sdmmc_write(host->regs, SDMMC_INTMASK,RK29_SDMMC_INTMASK_USEIO | SDMMC_INT_SDIO);
1780                     #endif
1781                     #else
1782                     rk29_sdmmc_write(host->regs, SDMMC_INTMASK,RK29_SDMMC_INTMASK_USEIO);
1783                     #endif
1784                     }
1785                     else
1786                     {
1787                 #if defined(CONFIG_RK29_SDIO_IRQ_FROM_GPIO)
1788                     rk29_sdmmc_write(host->regs, SDMMC_INTMASK,RK29_SDMMC_INTMASK_USEIO);
1789                 #else
1790                             rk29_sdmmc_write(host->regs, SDMMC_INTMASK,RK29_SDMMC_INTMASK_USEIO | SDMMC_INT_SDIO);
1791                 #endif
1792                     }
1793                 }               
1794     }
1795
1796         rk29_sdmmc_write(host->regs, SDMMC_PWREN, POWER_ENABLE);
1797         
1798         rk29_sdmmc_write(host->regs, SDMMC_CTRL,SDMMC_CTRL_INT_ENABLE); // enable mci interrupt
1799
1800     return SDM_SUCCESS;
1801 }
1802
1803
1804
1805
1806 //enable/disnable the clk.
1807 static int rk29_sdmmc_control_clock(struct rk29_sdmmc *host, bool enable)
1808 {
1809     u32           value = 0;
1810     int           tmo = 0;
1811     int           ret = SDM_SUCCESS;
1812
1813     //wait previous start to clear
1814     tmo = 1000;
1815         while (--tmo && (rk29_sdmmc_read(host->regs, SDMMC_CMD) & SDMMC_CMD_START))
1816         {
1817                 udelay(1);//cpu_relax();
1818         }
1819         if(!tmo)
1820         {
1821             host->errorstep = 0x0C;
1822             ret = SDM_START_CMD_FAIL;
1823                 goto Error_exit;        
1824     }
1825
1826     if(RK29_CTRL_SDMMC_ID == host->pdev->id)
1827     { 
1828         //SDMMC use low-power mode
1829         #if SDMMC_CLOCK_TEST
1830         if (enable)
1831         {
1832             value = (SDMMC_CLKEN_ENABLE);
1833         }
1834         else
1835         {
1836             value = (SDMMC_CLKEN_DISABLE);
1837         }
1838         
1839         #else
1840         {
1841             if (enable)
1842             {
1843                 value = (SDMMC_CLKEN_LOW_PWR | SDMMC_CLKEN_ENABLE);
1844             }
1845             else
1846             {
1847                 value = (SDMMC_CLKEN_LOW_PWR | SDMMC_CLKEN_DISABLE);
1848             }
1849         }
1850         #endif
1851     }
1852     else
1853     {
1854         //SDIO-card use non-low-power mode
1855         if (enable)
1856         {
1857             value = (SDMMC_CLKEN_ENABLE);
1858         }
1859         else
1860         {
1861             value = (SDMMC_CLKEN_DISABLE);
1862         }
1863     }
1864   
1865     rk29_sdmmc_write(host->regs, SDMMC_CLKENA, value);
1866
1867         /* inform CIU */
1868         ret = sdmmc_send_cmd_start(host, SDMMC_CMD_UPD_CLK | SDMMC_CMD_PRV_DAT_WAIT);
1869     if(ret != SDM_SUCCESS)
1870     {
1871         goto Error_exit;
1872     }
1873
1874     return SDM_SUCCESS;
1875
1876 Error_exit:
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);
1879
1880     return ret;
1881     
1882 }
1883
1884
1885 //adjust the frequency.ie, to set the frequency division control
1886 int rk29_sdmmc_change_clk_div(struct rk29_sdmmc *host, u32 freqHz)
1887 {
1888     u32 div;
1889     u32 tmo;
1890     int ret = SDM_SUCCESS;
1891
1892     if(0 == freqHz)
1893     {
1894         ret =  SDM_PARAM_ERROR;
1895         goto  SetFreq_error;
1896     }
1897
1898     ret = rk29_sdmmc_control_clock(host, FALSE);
1899     if (ret != SDM_SUCCESS)
1900     {
1901         goto SetFreq_error;
1902     }
1903
1904      
1905     host->bus_hz = clk_get_rate(host->clk);
1906     if((host->bus_hz > 52000000) || (host->bus_hz <= 0))
1907     {
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);
1910             
1911         host->errorstep = 0x0D;    
1912         ret = SDM_PARAM_ERROR;   
1913         goto SetFreq_error;
1914     }
1915
1916     //calculate the divider
1917     div = host->bus_hz/freqHz + ((( host->bus_hz%freqHz ) > 0) ? 1:0 );
1918     if( (div & 0x01) && (1 != div) )
1919     {
1920         //It is sure that the value of div is even. 
1921         ++div;
1922     }
1923
1924     if(div > 1)
1925     {
1926         host->clock = host->bus_hz/div;
1927     }
1928     else
1929     {
1930         host->clock = host->bus_hz;
1931     }
1932     div = (div >> 1);
1933
1934     //wait previous start to clear
1935     tmo = 1000;
1936         while (--tmo && (rk29_sdmmc_read(host->regs, SDMMC_CMD) & SDMMC_CMD_START))
1937         {
1938                 udelay(1);//cpu_relax();
1939         }
1940         if(!tmo)
1941         {
1942             host->errorstep = 0x0E; 
1943             ret = SDM_START_CMD_FAIL;
1944                 goto SetFreq_error;
1945     }
1946            
1947     /* set clock to desired speed */
1948     rk29_sdmmc_write(host->regs, SDMMC_CLKDIV, div);
1949
1950     /* inform CIU */
1951     ret = sdmmc_send_cmd_start(host, SDMMC_CMD_UPD_CLK | SDMMC_CMD_PRV_DAT_WAIT);
1952     if(ret != SDM_SUCCESS)
1953     {
1954         host->errorstep = 0x0E1; 
1955         goto SetFreq_error;
1956     }
1957     
1958     if(host->old_div != div)
1959     {
1960         printk(KERN_INFO "%s..%d..  newDiv=%u, newCLK=%uKhz [%s]\n", \
1961             __FUNCTION__, __LINE__,div, host->clock/1000, host->dma_name);
1962     }
1963
1964     ret = rk29_sdmmc_control_clock(host, TRUE);
1965     if(ret != SDM_SUCCESS)
1966     {
1967         goto SetFreq_error;
1968     }
1969     host->old_div = div;
1970
1971     return SDM_SUCCESS;
1972     
1973 SetFreq_error:
1974
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);
1977         
1978     return ret;
1979     
1980 }
1981
1982 int rk29_sdmmc_hw_init(void *data)
1983 {
1984     struct rk29_sdmmc *host = (struct rk29_sdmmc *)data;
1985     struct rk29_sdmmc_platform_data *pdata = host->pdev->dev.platform_data;
1986
1987     //set the iomux
1988     host->ctype = SDMMC_CTYPE_1BIT;
1989     host->set_iomux(host->pdev->id, host->ctype);
1990     
1991     if( pdata && pdata->sd_vcc_reset )
1992     {
1993             int cdetect = gpio_get_value(host->det_pin.io) ;
1994             if(host->det_pin.enable)
1995             {
1996                 cdetect = cdetect?1:0;
1997         }
1998         else
1999         {
2000                 cdetect = cdetect?0:1;
2001         }
2002        
2003             if( cdetect )
2004             {
2005                     pdata->sd_vcc_reset();
2006             }
2007     }
2008   
2009     /* reset controller */
2010     rk29_sdmmc_reset_controller(host);
2011
2012     rk29_sdmmc_change_clk_div(host, FOD_FREQ);
2013     
2014     return SDM_SUCCESS;    
2015 }
2016
2017
2018
2019 int rk29_sdmmc_set_buswidth(struct rk29_sdmmc *host)
2020 {
2021     //int ret;
2022     switch (host->ctype)
2023     {
2024         case SDMMC_CTYPE_1BIT:
2025         case SDMMC_CTYPE_4BIT:
2026             break;
2027         case SDMMC_CTYPE_8BIT:
2028             return SDM_PARAM_ERROR; //Now, not support 8 bit width
2029         default:
2030             return SDM_PARAM_ERROR;
2031     }
2032
2033     host->set_iomux(host->pdev->id, host->ctype);
2034
2035     /* Set the current  bus width */
2036         rk29_sdmmc_write(host->regs, SDMMC_CTYPE, host->ctype);
2037
2038     return SDM_SUCCESS;
2039 }
2040
2041
2042 static void rk29_sdmmc_dealwith_timeout(struct rk29_sdmmc *host)
2043
2044     if(0 == host->mmc->doneflag)
2045         return; //not to generate error flag if the command has been over.
2046         
2047     switch(host->state)
2048     {
2049         case STATE_IDLE:
2050             break;          
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);
2057             break;
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);
2065 #endif              
2066             tasklet_schedule(&host->tasklet);
2067             break;
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);
2075             break;
2076 #endif              
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);
2083             break;
2084         case STATE_DATA_END:
2085             break;
2086         default:
2087             break; 
2088     }
2089 }
2090
2091
2092 static void rk29_sdmmc_INT_CMD_DONE_timeout(unsigned long host_data)
2093 {
2094         struct rk29_sdmmc *host = (struct rk29_sdmmc *) host_data;
2095     
2096         rk29_sdmmc_enable_irq(host, false);
2097         
2098         if(STATE_SENDING_CMD == host->state)
2099         {
2100             if((0==host->cmd->retries)&&(12 != host->cmd->opcode))
2101             {
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);
2104
2105             if(++host->timeout_times >= 3)
2106             {
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;
2110             }
2111         }
2112         rk29_sdmmc_write(host->regs, SDMMC_RINTSTS, 0xFFFFFFFE); // clear INT,except for SDMMC_INT_CD
2113         rk29_sdmmc_dealwith_timeout(host);  
2114         }
2115         
2116     rk29_sdmmc_enable_irq(host, true);
2117 }
2118
2119
2120 static void rk29_sdmmc_INT_DTO_timeout(unsigned long host_data)
2121 {
2122         struct rk29_sdmmc *host = (struct rk29_sdmmc *) host_data;
2123   
2124         rk29_sdmmc_enable_irq(host, false);
2125
2126 #if SDMMC_USE_INT_UNBUSY
2127     if( (host->cmdr & SDMMC_CMD_DAT_EXP) &&((STATE_DATA_BUSY == host->state)||(STATE_DATA_UNBUSY == host->state) ))
2128 #else
2129     if( (host->cmdr & SDMMC_CMD_DAT_EXP) && (STATE_DATA_BUSY == host->state))
2130 #endif  
2131         {
2132             if(0==host->cmd->retries)
2133             {
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);
2136
2137             if(++host->timeout_times >= 3)
2138             {
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;
2142             }     
2143             }
2144         rk29_sdmmc_write(host->regs, SDMMC_RINTSTS, 0xFFFFFFFE); // clear INT,except for SDMMC_INT_CD
2145             rk29_sdmmc_dealwith_timeout(host);  
2146         }
2147         rk29_sdmmc_enable_irq(host, true);
2148  
2149 }
2150
2151
2152 //to excute a  request 
2153 static int rk29_sdmmc_start_request(struct mmc_host *mmc )
2154 {
2155     struct rk29_sdmmc *host = mmc_priv(mmc);
2156         struct mmc_request      *mrq;
2157         struct mmc_command      *cmd;
2158         
2159         u32             cmdr, ret;
2160         unsigned long iflags;
2161
2162         spin_lock_irqsave(&host->lock, iflags);
2163         
2164         mrq = host->new_mrq;
2165         cmd = mrq->cmd;
2166         cmd->error = 0;
2167         
2168         cmdr = rk29_sdmmc_prepare_command(cmd);
2169         ret = SDM_SUCCESS;
2170         
2171
2172         /*clean FIFO if it is a new request*/
2173     if((RK29_CTRL_SDMMC_ID == host->pdev->id) && ( !(cmdr & SDMMC_CMD_STOP)))
2174     {
2175         ret = rk29_sdmmc_reset_fifo(host);
2176         if(SDM_SUCCESS != ret)
2177         {
2178                         host->mrq = host->new_mrq;///
2179             cmd->error = -ENOMEDIUM;
2180             host->errorstep = 0x0F; 
2181             ret = SDM_FALSE;
2182             goto start_request_Err; 
2183         }
2184     }
2185
2186     //check data-busy if the current command has the bit13 in command register.
2187     if( cmdr & SDMMC_CMD_PRV_DAT_WAIT )
2188     {        
2189         if(SDM_SUCCESS != rk29_sdmmc_wait_unbusy(host))   //if(rk29_sdmmc_read(host->regs, SDMMC_STATUS) & SDMMC_STAUTS_DATA_BUSY)
2190         {
2191                 host->mrq = host->new_mrq;///
2192             cmd->error = -ETIMEDOUT;
2193             ret = SDM_BUSY_TIMEOUT;
2194             host->errorstep = 0x10;
2195             if(0 == cmd->retries)
2196             {
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);
2199             }
2200             rk29_sdmmc_clear_fifo(host);
2201             goto start_request_Err; 
2202         }
2203     }
2204     
2205     host->state = STATE_SENDING_CMD;
2206     host->mrq = host->new_mrq;
2207         mrq = host->mrq;
2208         cmd = mrq->cmd;
2209         cmd->error = 0;
2210         cmd->data = NULL;
2211
2212     host->cmdr = cmdr;
2213     host->cmd = cmd;
2214         host->data_status = 0;
2215         host->data = NULL;
2216         
2217         host->errorstep = 0;
2218         host->dodma = 0;
2219
2220
2221
2222     //setting for the data
2223         rk29_sdmmc_submit_data(host, mrq->data);
2224     host->errorstep = 0xff;
2225
2226         xbwprintk(7, "%s..%d...    CMD%d  begin to call rk29_sdmmc_start_command(). [%s]\n", \
2227                         __FUNCTION__, __LINE__ , cmd->opcode,host->dma_name);
2228
2229         if(RK29_CTRL_SDMMC_ID == host->pdev->id)
2230         {
2231             mod_timer(&host->request_timer, jiffies + msecs_to_jiffies(RK29_SDMMC_SEND_START_TIMEOUT+700));
2232         }
2233         else
2234         {
2235             mod_timer(&host->request_timer, jiffies + msecs_to_jiffies(RK29_SDMMC_SEND_START_TIMEOUT+500));
2236         }
2237
2238     
2239         ret = rk29_sdmmc_start_command(host, cmd, host->cmdr);
2240         if(SDM_SUCCESS != ret)
2241         {
2242         cmd->error = -ETIMEDOUT;
2243         if(0==cmd->retries)
2244         {
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);
2247         }
2248         host->errorstep = 0x11; 
2249         del_timer_sync(&host->request_timer);
2250         
2251         goto start_request_Err; 
2252         }
2253         host->errorstep = 0xfd;
2254
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);
2259
2260     spin_unlock_irqrestore(&host->lock, iflags);
2261     
2262         return SDM_SUCCESS;
2263         
2264 start_request_Err:
2265     rk29_sdmmc_start_error(host);
2266
2267     if(0 == cmd->retries) 
2268     {
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);
2271     }
2272
2273     host->state = STATE_IDLE;  //modifyed by xbw  at 2011-08-15
2274     
2275     if(host->mrq && host->mmc->doneflag && host->complete_done)
2276     {
2277         host->mmc->doneflag = 0;
2278         host->complete_done = 0;
2279         spin_unlock_irqrestore(&host->lock, iflags);
2280         
2281         mmc_request_done(host->mmc, host->mrq);
2282     }
2283     else
2284     {
2285         spin_unlock_irqrestore(&host->lock, iflags);        
2286     }
2287     
2288     return ret; 
2289         
2290 }
2291
2292  
2293 static void rk29_sdmmc_request(struct mmc_host *mmc, struct mmc_request *mrq)
2294 {
2295     unsigned long iflags;
2296     int i,ret;
2297         struct rk29_sdmmc *host = mmc_priv(mmc); 
2298         
2299     spin_lock_irqsave(&host->lock, iflags);
2300     
2301         #if 0
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)//
2304         {
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);
2308
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);
2313                 return;
2314         }
2315         #endif
2316
2317     i=0;
2318         while(++i>2)
2319     {
2320         ret = rk29_sdmmc_wait_unbusy(host);
2321         if(SDM_SUCCESS == ret)
2322             break;
2323     }
2324
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);
2327
2328     if(RK29_CTRL_SDMMC_ID == host->pdev->id)
2329     {
2330         if(!rk29_sdmmc_get_cd(mmc) || ((0==mmc->re_initialized_flags)&&(MMC_GO_IDLE_STATE != mrq->cmd->opcode)))
2331         {
2332                 mrq->cmd->error = -ENOMEDIUM;
2333
2334                 if((RK29_CTRL_SDMMC_ID == host->pdev->id)&&(0==mrq->cmd->retries))
2335                 {
2336                 if(host->old_cmd != mrq->cmd->opcode)
2337                 {
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)))
2340                     {
2341                         host->old_cmd = mrq->cmd->opcode;
2342                         if(host->error_times++ % (RK29_ERROR_PRINTK_INTERVAL*100) ==0)
2343                         {
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);
2346                                 }
2347                     }
2348                     else
2349                     {
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); 
2354                         }
2355                     }
2356                     else
2357                     {
2358                         if(host->error_times++ % (RK29_ERROR_PRINTK_INTERVAL*100) ==0)
2359                         {
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);
2362                         }
2363                         host->old_cmd = mrq->cmd->opcode;
2364                     }       
2365                 }
2366             host->state = STATE_IDLE;
2367             spin_unlock_irqrestore(&host->lock, iflags);
2368             
2369                 mmc_request_done(mmc, mrq);
2370                 return;
2371         }
2372         else
2373         {
2374                 if(host->old_cmd != mrq->cmd->opcode)
2375                 {       
2376                         host->old_cmd = mrq->cmd->opcode;
2377                                 host->error_times = 0;
2378                         }                       
2379         }
2380         }
2381         else
2382         {
2383         host->old_cmd = mrq->cmd->opcode;
2384         host->error_times = 0;
2385
2386         if(!test_bit(RK29_SDMMC_CARD_PRESENT, &host->flags))
2387                 {
2388                     host->state = STATE_IDLE;
2389                     mrq->cmd->error = -ENOMEDIUM;
2390             spin_unlock_irqrestore(&host->lock, iflags);
2391             
2392                 mmc_request_done(mmc, mrq);
2393                 return;
2394                 }
2395
2396         }
2397  
2398     host->new_mrq = mrq;        
2399
2400         spin_unlock_irqrestore(&host->lock, iflags);
2401                 
2402     rk29_sdmmc_start_request(mmc);
2403 }
2404
2405
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)
2408 {
2409     int timeout = 250;
2410     unsigned int value;
2411         struct rk29_sdmmc *host = mmc_priv(mmc);
2412
2413     rk29_sdmmc_enable_irq(host, false);
2414
2415     //if(host->bus_mode != ios->power_mode)
2416     {
2417         switch (ios->power_mode) 
2418         {
2419             case MMC_POWER_UP:
2420                 rk29_sdmmc_write(host->regs, SDMMC_PWREN, POWER_ENABLE);
2421                                 
2422                 //reset the controller if it is SDMMC0
2423                 if(RK29_CTRL_SDMMC_ID == host->pdev->id)
2424                 {
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);
2427                         
2428                     //power-on; 
2429                     gpio_direction_output(host->gpio_power_en, host->gpio_power_en_level);
2430                     
2431                     mdelay(5);
2432                         
2433                     rk29_sdmmc_hw_init(host);
2434                 }
2435                 
2436                 break;
2437             case MMC_POWER_OFF:
2438               
2439                 if(RK29_CTRL_SDMMC_ID == host->pdev->id)
2440                 {
2441                     mdelay(5);
2442                         rk29_sdmmc_control_clock(host, FALSE);
2443                         rk29_sdmmc_write(host->regs, SDMMC_PWREN, POWER_DISABLE);
2444                         mdelay(5);                
2445                         if(5 == host->bus_mode)
2446                         {
2447                         mdelay(5);
2448                         xbwprintk(7, "%s..%d..Fisrt powerOFF, call reset_controller [%s]\n", \
2449                             __FUNCTION__, __LINE__,host->dma_name);
2450                             
2451                         rk29_sdmmc_reset_controller(host);
2452                         }
2453
2454                   
2455                                 rk29_sdmmc_gpio_open(0, 0);                             
2456                                 //power-off 
2457                     gpio_direction_output(host->gpio_power_en, !(host->gpio_power_en_level));  
2458                                 goto out;
2459                 }
2460
2461                 break;          
2462             default:
2463                 break;
2464         }
2465         
2466         host->bus_mode = ios->power_mode;
2467         
2468         }
2469
2470     if(test_bit(RK29_SDMMC_CARD_PRESENT, &host->flags) || (RK29_CTRL_SDMMC_ID == host->pdev->id))
2471     {
2472         /*
2473          * Waiting SDIO controller to be IDLE.
2474         */
2475         while (timeout-- > 0)
2476         {
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)
2479                 {
2480                         break;
2481                 }
2482                 
2483                 mdelay(1);
2484         }
2485         if (timeout <= 0)
2486         {
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);
2489
2490                 goto out;
2491         }
2492         }
2493
2494
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. 
2498         
2499         if(host->ctype != ios->bus_width)
2500         {
2501         switch (ios->bus_width) 
2502         {
2503             case MMC_BUS_WIDTH_1:
2504                 host->ctype = SDMMC_CTYPE_1BIT;
2505                 break;
2506             case MMC_BUS_WIDTH_4:
2507                 host->ctype = SDMMC_CTYPE_4BIT;
2508                 break;
2509             case MMC_BUS_WIDTH_8:
2510                 host->ctype = SDMMC_CTYPE_8BIT;
2511                 break;
2512             default:
2513                 host->ctype = 0;
2514                 break;
2515         }
2516
2517             rk29_sdmmc_set_buswidth(host);
2518             
2519         }
2520         
2521         if (ios->clock && (ios->clock != host->clock)) 
2522         {       
2523                 /*
2524                  * Use mirror of ios->clock to prevent race with mmc
2525                  * core ios update when finding the minimum.
2526                  */
2527                 //host->clock = ios->clock;     
2528                 rk29_sdmmc_change_clk_div(host, ios->clock);
2529         }
2530 out:       
2531     rk29_sdmmc_enable_irq(host, true);
2532     
2533 }
2534
2535 static int rk29_sdmmc_get_ro(struct mmc_host *mmc)
2536 {
2537     struct rk29_sdmmc *host = mmc_priv(mmc);
2538     int ret=0;
2539
2540     switch(host->pdev->id)
2541     {
2542         case 0:
2543         {
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
2547                 else
2548                 ret = (host->protect_level == gpio_get_value(host->write_protect)?1:0;
2549            
2550                 xbwprintk(7,"%s..%d.. write_prt_pin=%d, get_ro=%d. [%s]\n",\
2551                     __FUNCTION__, __LINE__,host->write_protect, ret, host->dma_name);
2552                             
2553             #else
2554                 u32 wrtprt = rk29_sdmmc_read(host->regs, SDMMC_WRTPRT);
2555                 
2556                 ret = (wrtprt & SDMMC_WRITE_PROTECT)?1:0;
2557             #endif
2558
2559             break;
2560         }
2561         
2562         case 1:
2563             ret = 0;//no write-protect
2564             break;
2565         
2566         default:
2567             ret = 0;
2568         break;   
2569     }
2570
2571     return ret;
2572
2573 }
2574
2575 #if defined(CONFIG_RK29_SDIO_IRQ_FROM_GPIO)
2576 static irqreturn_t rk29_sdmmc_sdio_irq_cb(int irq, void *dev_id)
2577 {
2578         struct rk29_sdmmc *host = dev_id;
2579         
2580     if(host && host->mmc)
2581         mmc_signal_sdio_irq(host->mmc);
2582
2583         return IRQ_HANDLED;
2584 }
2585 #endif
2586
2587
2588 static void rk29_sdmmc_enable_sdio_irq(struct mmc_host *mmc, int enable)
2589 {
2590 #if !defined(CONFIG_RK29_SDIO_IRQ_FROM_GPIO)    
2591         u32 intmask;    
2592         unsigned long flags;
2593 #endif  
2594         struct rk29_sdmmc *host = mmc_priv(mmc);
2595                 
2596 #if defined(CONFIG_RK29_SDIO_IRQ_FROM_GPIO)     
2597     if(enable)
2598     {
2599         enable_irq(host->sdio_irq);
2600         
2601         #if !defined(CONFIG_MTK_COMBO_DRIVER_VERSION_JB2)
2602         enable_irq_wake(host->sdio_irq);
2603         #endif
2604     }
2605     else
2606     {
2607         disable_irq_nosync(host->sdio_irq);
2608         
2609         #if !defined(CONFIG_MTK_COMBO_DRIVER_VERSION_JB2)
2610         disable_irq_wake(host->sdio_irq);
2611         #endif
2612     }
2613
2614 #else
2615     spin_lock_irqsave(&host->lock, flags);
2616
2617         intmask = rk29_sdmmc_read(host->regs, SDMMC_INTMASK);   
2618         if(enable)
2619                 rk29_sdmmc_write(host->regs, SDMMC_INTMASK, intmask | SDMMC_INT_SDIO);
2620         else
2621                 rk29_sdmmc_write(host->regs, SDMMC_INTMASK, intmask & ~SDMMC_INT_SDIO);
2622
2623         spin_unlock_irqrestore(&host->lock, flags);     
2624 #endif          
2625     
2626     
2627 }
2628
2629 static void  rk29_sdmmc_init_card(struct mmc_host *mmc, struct mmc_card *card)
2630 {
2631         card->quirks = MMC_QUIRK_BLKSZ_FOR_BYTE_MODE;
2632
2633 }
2634
2635 static int rk29_sdmmc_clear_fifo(struct rk29_sdmmc *host)
2636 {
2637     unsigned int timeout, value;
2638     int ret = SDM_SUCCESS;
2639
2640     if(RK29_CTRL_SDMMC_ID == host->pdev->id)
2641     {
2642         rk29_sdmmc_write(host->regs, SDMMC_RINTSTS, 0xFFFFFFFF);
2643     }
2644
2645     rk29_sdmmc_stop_dma(host);
2646     rk29_sdmmc_control_host_dma(host, FALSE);
2647     host->dodma = 0;
2648    
2649     //Clean the fifo.
2650     for(timeout=0; timeout<FIFO_DEPTH; timeout++)
2651     {
2652         if(rk29_sdmmc_read(host->regs, SDMMC_STATUS) & SDMMC_STAUTS_FIFO_EMPTY)
2653             break;
2654             
2655         value = rk29_sdmmc_read(host->regs, SDMMC_DATA);
2656     }
2657
2658      /* reset */
2659     timeout = 1000;
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);
2663
2664     value = rk29_sdmmc_read(host->regs, SDMMC_CTRL);
2665     
2666     while( (value & (SDMMC_CTRL_FIFO_RESET | SDMMC_CTRL_RESET | SDMMC_CTRL_DMA_RESET)) && (timeout > 0))
2667     {
2668         udelay(1);
2669         timeout--;
2670         value = rk29_sdmmc_read(host->regs, SDMMC_CTRL);
2671     }
2672
2673     if (timeout == 0)
2674     {
2675         host->errorstep = 0x0A;
2676         ret = SDM_WAIT_FOR_FIFORESET_TIMEOUT;
2677     }
2678
2679     return ret;
2680 }
2681
2682
2683
2684 static const struct mmc_host_ops rk29_sdmmc_ops[] = {
2685         {
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,
2690         },
2691         {
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,
2698         },
2699 };
2700
2701 static void rk29_sdmmc_request_end(struct rk29_sdmmc *host, struct mmc_command *cmd)
2702 {
2703         u32 status = host->data_status;
2704         int output=SDM_SUCCESS;
2705
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);
2708
2709     del_timer_sync(&host->DTO_timer);
2710
2711     if(RK29_CTRL_SDMMC_ID == host->pdev->id)
2712     {
2713         rk29_sdmmc_write(host->regs, SDMMC_RINTSTS, 0xFFFFFFFF); //added by xbw at 2011-08-15
2714     }
2715
2716     //stop DMA
2717     if(host->dodma)
2718     {
2719         rk29_sdmmc_stop_dma(host);
2720         rk29_sdmmc_control_host_dma(host, FALSE);
2721         host->dodma = 0;
2722     }
2723
2724     if(cmd->error)
2725     {
2726         goto exit;//It need not to wait-for-busy if the CMD-ERROR happen.
2727     }
2728     host->errorstep = 0xf7;
2729     if(cmd->data)
2730     {        
2731         if(host->cmdr & SDMMC_CMD_DAT_WRITE)
2732         {
2733             if(status & (SDMMC_INT_DCRC | SDMMC_INT_EBE))
2734             {
2735                 cmd->data->error = -EILSEQ;               
2736                 output = SDM_DATA_CRC_ERROR;
2737                 host->errorstep = 0x16; 
2738             }
2739             else
2740             {
2741                 output = rk29_sdmmc_wait_unbusy(host);
2742                 if(SDM_SUCCESS != output)
2743                 {
2744                     host->errorstep = 0x17;
2745                     cmd->data->error = -ETIMEDOUT;
2746                 }
2747
2748                 host->data->bytes_xfered = host->data->blocks * host->data->blksz;
2749             }
2750         }
2751         else
2752         {
2753             if( status  & SDMMC_INT_SBE)
2754             {
2755                 cmd->data->error = -EIO;
2756                 host->errorstep = 0x18;
2757                 output = SDM_START_BIT_ERROR;
2758             }
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.
2760             {
2761                 cmd->data->error = -EILSEQ;
2762                 host->errorstep = 0x19;
2763                 output = SDM_END_BIT_ERROR;
2764             }
2765             else if(status  & SDMMC_INT_DRTO)
2766             {
2767                 cmd->data->error = -ETIMEDOUT;
2768                 host->errorstep = 0x1A;
2769                 output = SDM_DATA_READ_TIMEOUT;
2770             }
2771             else if(status  & SDMMC_INT_DCRC)
2772             {
2773                 host->errorstep = 0x1B;
2774                 cmd->data->error = -EILSEQ;
2775                 output = SDM_DATA_CRC_ERROR;
2776             }
2777             else
2778             {
2779                 output = rk29_sdmmc_read_remain_data(host, (host->data->blocks * host->data->blksz), host->pbuf);
2780                 if(SDM_SUCCESS == output)
2781                 {
2782                     host->data->bytes_xfered = host->data->blocks * host->data->blksz;
2783                 }
2784             }       
2785         }
2786     }
2787
2788     if(SDM_SUCCESS == output)
2789     {
2790         if ((mmc_resp_type(cmd) == MMC_RSP_R1B) || (MMC_STOP_TRANSMISSION == cmd->opcode))
2791         {
2792             output = rk29_sdmmc_wait_unbusy(host);
2793             if((SDM_SUCCESS != output) && (!host->mrq->cmd->error))
2794             {
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;
2801             }
2802         }
2803     }
2804     host->errorstep = 0xf6;
2805     
2806     //trace error
2807     if(cmd->data && cmd->data->error)
2808     { 
2809         if( (!cmd->error) && (0==cmd->retries))
2810         {         
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);
2817         }
2818         cmd->error = -ENODATA;
2819     }
2820     host->errorstep = 0xf5;
2821
2822 exit:
2823
2824 #ifdef RK29_SDMMC_LIST_QUEUE
2825         if (!list_empty(&host->queue)) 
2826         {
2827                 printk(KERN_WARNING "%s..%d..  Danger!Danger!. continue the next request in the queue.  [%s]\n",\
2828                         __FUNCTION__, __LINE__, host->dma_name);
2829
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);
2835         } 
2836         else 
2837         {       
2838                 dev_vdbg(&host->pdev->dev, "list empty\n");
2839                 host->state = STATE_IDLE;
2840         }
2841 #else
2842     dev_vdbg(&host->pdev->dev, "list empty\n");
2843         host->state = STATE_IDLE;
2844 #endif
2845         
2846 }
2847
2848 static int rk29_sdmmc_command_complete(struct rk29_sdmmc *host,
2849                         struct mmc_command *cmd)
2850 {
2851         u32      value, status = host->cmd_status;
2852         int  timeout, output= SDM_SUCCESS;
2853
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);
2856
2857
2858     del_timer_sync(&host->request_timer);
2859     
2860     host->cmd_status = 0;
2861
2862         if((RK29_CTRL_SDMMC_ID == host->pdev->id) && (host->cmdr & SDMMC_CMD_STOP))
2863     {
2864         output = rk29_sdmmc_reset_fifo(host);
2865         if (SDM_SUCCESS != output)
2866         {
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);
2869                 
2870             cmd->error = -ETIMEDOUT;
2871             host->mrq->cmd->error = cmd->error;
2872             output = SDM_ERROR;
2873             host->errorstep = 0x1C;
2874             goto CMD_Errror;
2875         }
2876     }
2877
2878     if(status & SDMMC_INT_RTO)
2879         {
2880             cmd->error = -ENOMEM;
2881             host->mrq->cmd->error = cmd->error;
2882         output = SDM_BUSY_TIMEOUT;
2883         host->errorstep = 0x1E;
2884
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
2887         
2888         if(host->use_dma)//if(host->dodma)
2889         {
2890            if(host->dodma) 
2891            {
2892                 rk29_sdmmc_stop_dma(host);
2893                 rk29_sdmmc_control_host_dma(host, FALSE);
2894                 host->dodma = 0;
2895            }
2896             
2897             value = rk29_sdmmc_read(host->regs, SDMMC_CTRL);
2898             value |= SDMMC_CTRL_FIFO_RESET;
2899             rk29_sdmmc_write(host->regs, SDMMC_CTRL, value);
2900
2901             timeout = 1000;
2902             while (((value = rk29_sdmmc_read(host->regs, SDMMC_CTRL)) & (SDMMC_CTRL_FIFO_RESET)) && (timeout > 0))
2903             {
2904                 udelay(1);
2905                 timeout--;
2906             }
2907             if (timeout == 0)
2908             {   
2909                 output = SDM_FALSE;
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);
2913                 
2914                goto CMD_Errror;
2915             }
2916         }
2917
2918         }       
2919
2920         if(cmd->flags & MMC_RSP_PRESENT) 
2921         {
2922             if(cmd->flags & MMC_RSP_136) 
2923             {
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);
2928             } 
2929             else 
2930             {
2931                 cmd->resp[0] = rk29_sdmmc_read(host->regs, SDMMC_RESP0);
2932             }
2933         }
2934
2935      #if 1      
2936         if(cmd->error)
2937         {
2938             del_timer_sync(&host->DTO_timer);
2939
2940         //trace error
2941             if((0==cmd->retries) && (host->error_times++%(RK29_ERROR_PRINTK_INTERVAL*3) == 0) && (12 != cmd->opcode))
2942             {
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))  )
2945                 {
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);
2948                 }
2949             }
2950
2951         }
2952     #endif
2953
2954     del_timer_sync(&host->request_timer);
2955
2956
2957         return SDM_SUCCESS;
2958    
2959 CMD_Errror:
2960     del_timer_sync(&host->request_timer);
2961         del_timer_sync(&host->DTO_timer);
2962
2963         if((0==cmd->retries) && (host->error_times++%RK29_ERROR_PRINTK_INTERVAL == 0))
2964     {
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);
2967     }
2968         
2969     return output;
2970     
2971 }
2972
2973
2974 static void rk29_sdmmc_start_error(struct rk29_sdmmc *host)
2975 {
2976     host->cmd->error = -EIO;
2977     host->mrq->cmd->error = -EIO;
2978     host->cmd_status |= SDMMC_INT_RTO;
2979
2980     del_timer_sync(&host->request_timer);
2981
2982     rk29_sdmmc_command_complete(host, host->mrq->cmd);    
2983     rk29_sdmmc_request_end(host, host->mrq->cmd);
2984 }
2985
2986 static void rk29_sdmmc_tasklet_func(unsigned long priv)
2987 {
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;
2992
2993         rk29_sdmmc_enable_irq(host, false);
2994         spin_lock(&host->lock);//spin_lock_irqsave(&host->lock, iflags); 
2995         
2996         state = host->state;
2997         pending_flag = 0;
2998         stopflag = 0;
2999         
3000         do 
3001         {
3002         switch (state) 
3003         {
3004             case STATE_IDLE:
3005             {
3006                 xbwprintk(7, "%s..%d..   prev_state=  STATE_IDLE  [%s]\n", \
3007                                                 __FUNCTION__, __LINE__, host->dma_name);
3008                 break;
3009             }
3010
3011             case STATE_SENDING_CMD:
3012             {
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)
3016                 {
3017                     del_timer_sync(&host->request_timer);
3018                 }
3019                 
3020                 if (!rk29_sdmmc_test_and_clear_pending(host, EVENT_CMD_COMPLETE))
3021                         break;
3022                  host->errorstep = 0xfb;
3023
3024                 del_timer_sync(&host->request_timer); //delete the timer for INT_COME_DONE
3025
3026                 rk29_sdmmc_set_completed(host, EVENT_CMD_COMPLETE);
3027                 rk29_sdmmc_command_complete(host, host->cmd);
3028
3029                 
3030                 if (!data) 
3031                 {
3032                     rk29_sdmmc_request_end(host, host->cmd);
3033
3034                     xbwprintk(7, "%s..%d..  CMD%d call mmc_request_done() . [%s]\n", \
3035                                                         __FUNCTION__, __LINE__,host->cmd->opcode,host->dma_name);
3036                     
3037                     host->complete_done = 1;
3038                     break;
3039                 }
3040                 host->errorstep = 0xfa;
3041                 if(host->cmd->error)
3042                 {
3043                     del_timer_sync(&host->DTO_timer); //delete the timer for INT_DTO
3044                     
3045                     if((data->stop) && (MMC_STOP_TRANSMISSION != host->cmd->opcode)) 
3046                     {
3047                         xbwprintk(7, "%s..%d..  cmderr, so call send_stop_cmd() [%s]\n", \
3048                                                                 __FUNCTION__, __LINE__, host->dma_name);
3049
3050                         stopflag = 1;  //Moidfyed by xbw at 2011-09-08
3051
3052                         break;
3053                     }
3054
3055                     rk29_sdmmc_set_pending(host, EVENT_DATA_COMPLETE);
3056                 }
3057
3058                 host->errorstep = 0xf9;
3059                 state = STATE_DATA_BUSY;
3060                 /* fall through */
3061             }
3062
3063             case STATE_DATA_BUSY:
3064             {
3065                 xbwprintk(7, "%s..%d..   prev_state= STATE_DATA_BUSY, pendingEvernt=0x%lu [%s]\n", \
3066                                                 __FUNCTION__, __LINE__,host->pending_events, host->dma_name);
3067
3068                 if (!rk29_sdmmc_test_and_clear_pending(host, EVENT_DATA_COMPLETE))
3069                         break;  
3070                 host->errorstep = 0xf8;
3071                 rk29_sdmmc_set_completed(host, EVENT_DATA_COMPLETE);
3072
3073              #if SDMMC_USE_INT_UNBUSY
3074                 if((MMC_WRITE_BLOCK==host->cmd->opcode)||(MMC_WRITE_MULTIPLE_BLOCK==host->cmd->opcode))
3075                 {
3076                     /*
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. 
3079                     */                    
3080                     mod_timer(&host->DTO_timer, jiffies + msecs_to_jiffies(5000));
3081                 }
3082                 else
3083                 {
3084                     del_timer_sync(&host->DTO_timer); //delete the timer for INT_DTO
3085                 }
3086   
3087                 state = STATE_DATA_UNBUSY;
3088
3089              #else
3090                 del_timer_sync(&host->DTO_timer); //delete the timer for INT_DTO
3091              #endif
3092              }
3093
3094              case STATE_DATA_UNBUSY:
3095              {
3096              #if SDMMC_USE_INT_UNBUSY
3097                 if((MMC_WRITE_BLOCK==host->cmd->opcode)||(MMC_WRITE_MULTIPLE_BLOCK==host->cmd->opcode))
3098                 {
3099                     if (!rk29_sdmmc_test_and_clear_pending(host, EVENT_DATA_UNBUSY))
3100                         break;
3101
3102                     del_timer_sync(&host->DTO_timer);
3103                 }
3104                 rk29_sdmmc_set_completed(host, EVENT_DATA_UNBUSY);
3105                 state = STATE_DATA_END;
3106              #endif 
3107                 rk29_sdmmc_request_end(host, host->cmd);
3108
3109                 if (data && !data->stop) 
3110                 {
3111                     xbwprintk(7, "%s..%d..  CMD%d call mmc_request_done(). [%s]\n", \
3112                                                         __FUNCTION__, __LINE__,host->cmd->opcode,host->dma_name);
3113
3114                     if(!( (MMC_READ_SINGLE_BLOCK == host->cmd->opcode)&&( -EIO == data->error))) //deal with START_BIT_ERROR
3115                     {
3116                         host->complete_done = 2;
3117                         break;
3118                     }
3119
3120                 }
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);
3124
3125                 stopflag = 2; //Moidfyed by xbw at 2011-09-08
3126                 
3127                 break;
3128             }
3129
3130             case STATE_SENDING_STOP:
3131             {
3132                 xbwprintk(7, "%s..%d..   prev_state=  STATE_SENDING_STOP, pendingEvernt=0x%lu  [%s]\n", \
3133                                                 __FUNCTION__, __LINE__, host->pending_events, host->dma_name);
3134
3135                 if (!rk29_sdmmc_test_and_clear_pending(host, EVENT_CMD_COMPLETE))
3136                         break;
3137
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);
3141                 
3142                 host->complete_done = 3;
3143                 break;
3144             }
3145             
3146             case STATE_DATA_END:
3147                 break;
3148             default:
3149                 break;          
3150         }
3151
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);
3155
3156         if(pending_flag)
3157         {
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",\
3162                 
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);
3167                 
3168             cpu_relax();
3169         }
3170                         
3171         } while(pending_flag && ++host->retryfunc); //while(0);
3172
3173         if(0!=stopflag)
3174     {
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));
3178             
3179         host->errorstep = 0xe0;  
3180         
3181         if(test_bit(RK29_SDMMC_CARD_PRESENT, &host->flags))
3182         {
3183             state = STATE_SENDING_CMD;
3184             send_stop_cmd(host);   //Moidfyed by xbw at 2011-09-08
3185         }
3186         else
3187         {
3188             host->complete_done = 5;
3189         }
3190     }
3191
3192         host->state = state;
3193                  
3194     if((0==host->complete_done)&& host->mmc->doneflag && test_bit(RK29_SDMMC_CARD_PRESENT, &host->flags))
3195     {
3196         host->errorstep = 0xf2;
3197
3198      #if 0
3199         //debug
3200         if(12==host->cmd->opcode)
3201         {
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);
3205         }
3206       #endif  
3207         
3208         spin_unlock(&host->lock);//spin_unlock_irqrestore(&host->lock, iflags);
3209         rk29_sdmmc_enable_irq(host, true);
3210         return;
3211     }
3212     host->errorstep = 0xf3; 
3213         host->state = STATE_IDLE;
3214          
3215          if(host->mrq && host->mmc->doneflag && host->complete_done)
3216          {
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);
3222          }
3223          else
3224          {
3225             spin_unlock(&host->lock);//spin_unlock_irqrestore(&host->lock, iflags);
3226             rk29_sdmmc_enable_irq(host, true);
3227          }
3228 }
3229
3230
3231 static inline void rk29_sdmmc_cmd_interrupt(struct rk29_sdmmc *host, u32 status)
3232 {
3233     u32 multi, unit;
3234     
3235         host->cmd_status |= status;
3236     host->errorstep = 0xfc;
3237     if((MMC_STOP_TRANSMISSION != host->cmd->opcode) && (host->cmdr & SDMMC_CMD_DAT_EXP))
3238     {
3239         unit = 2*1024*1024;
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  
3245         }
3246         
3247         smp_wmb();
3248         rk29_sdmmc_set_pending(host, EVENT_CMD_COMPLETE);
3249         tasklet_schedule(&host->tasklet);
3250 }
3251
3252 static irqreturn_t rk29_sdmmc_interrupt(int irq, void *dev_id)
3253 {
3254         struct rk29_sdmmc       *host = dev_id;
3255         u32                     status,  pending;
3256         bool present;
3257         bool present_old;
3258         int sdio_irq=0;
3259
3260         spin_lock(&host->lock);
3261
3262     status = rk29_sdmmc_read(host->regs, SDMMC_RINTSTS);
3263     pending = rk29_sdmmc_read(host->regs, SDMMC_MINTSTS);// read only mask reg
3264     if (!pending)
3265     {
3266         goto Exit_INT;
3267     }
3268
3269
3270     if(pending & SDMMC_INT_CD) 
3271     {
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);
3276         
3277         if(present != present_old)
3278         {           
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);
3281
3282             rk28_send_wakeup_key(); //wake up backlight
3283             host->error_times = 0;
3284
3285             #if 1
3286             del_timer(&host->request_timer);
3287                 del_timer(&host->DTO_timer);
3288             rk29_sdmmc_dealwith_timeout(host);              
3289             #endif
3290                             
3291             if(present)
3292             {
3293                 set_bit(RK29_SDMMC_CARD_PRESENT, &host->flags);
3294
3295                 if(host->mmc->re_initialized_flags)
3296                     {
3297                         mod_timer(&host->detect_timer, jiffies + msecs_to_jiffies(RK29_SDMMC_REMOVAL_DELAY));
3298                     }
3299                     else
3300                     {
3301                         mod_timer(&host->detect_timer, jiffies + msecs_to_jiffies(RK29_SDMMC_REMOVAL_DELAY*2));
3302                     }
3303             }
3304             else
3305             {
3306                 clear_bit(RK29_SDMMC_CARD_PRESENT, &host->flags);
3307                 host->mmc->re_initialized_flags = 0;
3308
3309                 mmc_detect_change(host->mmc, 200);
3310             }
3311
3312         }
3313
3314         goto Exit_INT;
3315
3316     }
3317     
3318     if (pending & SDMMC_INT_CMD_DONE) {
3319
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);
3322
3323         rk29_sdmmc_write(host->regs, SDMMC_RINTSTS,SDMMC_INT_CMD_DONE);  //  clear interrupt
3324         rk29_sdmmc_cmd_interrupt(host, status);
3325
3326         goto Exit_INT;
3327     }
3328
3329 #if !defined(CONFIG_RK29_SDIO_IRQ_FROM_GPIO)
3330     if(pending & SDMMC_INT_SDIO) 
3331     {   
3332         xbwprintk(7, "%s..%d..  INT_SDIO  INT=0x%x   [%s]\n", \
3333                                 __FUNCTION__, __LINE__, pending, host->dma_name);
3334
3335         rk29_sdmmc_write(host->regs, SDMMC_RINTSTS,SDMMC_INT_SDIO);
3336         sdio_irq = 1;
3337
3338         goto Exit_INT;
3339     }
3340 #endif
3341
3342     if(pending & SDMMC_INT_RTO) 
3343     {
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);
3346
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;
3350         smp_wmb();
3351         rk29_sdmmc_set_pending(host, EVENT_CMD_COMPLETE);
3352
3353         if(!(pending & SDMMC_INT_CMD_DONE))
3354         {
3355             tasklet_schedule(&host->tasklet);
3356         }
3357
3358         goto Exit_INT;
3359     }
3360
3361
3362     if(pending & SDMMC_INT_HLE)
3363     {
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);         
3366     
3367         rk29_sdmmc_write(host->regs, SDMMC_RINTSTS,SDMMC_INT_HLE); 
3368         goto Exit_INT;
3369     }
3370
3371
3372     if(pending & SDMMC_INT_DTO) 
3373     {   
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);
3376
3377         rk29_sdmmc_write(host->regs, SDMMC_RINTSTS,SDMMC_INT_DTO); 
3378         del_timer(&host->DTO_timer); //delete the timer for INT_DTO
3379
3380         host->data_status |= status;
3381
3382         smp_wmb();
3383
3384         rk29_sdmmc_set_pending(host, EVENT_DATA_COMPLETE);
3385         tasklet_schedule(&host->tasklet);
3386         goto Exit_INT;
3387     }
3388
3389
3390     if (pending & SDMMC_INT_FRUN) 
3391     { 
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);
3394         
3395         //rk29_sdmmc_write(host->regs, SDMMC_RINTSTS,SDMMC_INT_FRUN);
3396         rk29_sdmmc_write(host->regs, SDMMC_RINTSTS,0xFFFFFFFE); 
3397         goto Exit_INT;
3398     }
3399
3400     if (pending & SDMMC_INT_RXDR) 
3401     {   
3402         xbwprintk(6, "%s..%d..  SDMMC_INT_RXDR  INT=0x%x   [%s]\n", \
3403                                 __FUNCTION__, __LINE__, pending, host->dma_name);
3404
3405         rk29_sdmmc_write(host->regs, SDMMC_RINTSTS,SDMMC_INT_RXDR);  //  clear interrupt
3406         rk29_sdmmc_read_data_pio(host);
3407     }
3408
3409     if (pending & SDMMC_INT_TXDR) 
3410     {
3411         xbwprintk(6, "%s..%d..  SDMMC_INT_TXDR  INT=0x%x   [%s]\n", \
3412                                 __FUNCTION__, __LINE__, pending, host->dma_name);
3413                                 
3414         rk29_sdmmc_write(host->regs, SDMMC_RINTSTS,SDMMC_INT_TXDR);  //  clear interrupt
3415         rk29_sdmmc_write_data_pio(host); 
3416     }
3417
3418 #if SDMMC_USE_INT_UNBUSY
3419         if(pending & SDMMC_INT_UNBUSY) 
3420         {
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);
3424     
3425             rk29_sdmmc_write(host->regs, SDMMC_RINTSTS,SDMMC_INT_UNBUSY); 
3426             
3427             host->data_status = status;
3428             smp_wmb();
3429             rk29_sdmmc_set_pending(host, EVENT_DATA_UNBUSY);
3430             tasklet_schedule(&host->tasklet);  
3431             
3432             goto Exit_INT;
3433         }
3434 #endif
3435
3436 Exit_INT:
3437
3438         spin_unlock(&host->lock);
3439
3440     if(1 == sdio_irq)
3441     {
3442         mmc_signal_sdio_irq(host->mmc);
3443     }
3444         
3445         return IRQ_HANDLED;
3446 }
3447
3448 /*
3449  *
3450  * MMC card detect thread, kicked off from detect interrupt, 1 timer 
3451  *
3452  */
3453 static void rk29_sdmmc_detect_change(unsigned long data)
3454 {
3455         struct rk29_sdmmc *host = (struct rk29_sdmmc *)data;
3456
3457         if(!host->mmc)
3458             return;
3459    
3460         smp_rmb();
3461
3462     if((RK29_CTRL_SDMMC_ID == host->pdev->id) && rk29_sdmmc_get_cd(host->mmc))
3463     {
3464         host->mmc->re_initialized_flags =1;
3465     }
3466     
3467         mmc_detect_change(host->mmc, 0);        
3468
3469 }
3470
3471 static void rk29_sdmmc1_check_status(unsigned long data)
3472 {
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;
3476
3477     status = pdata->status(mmc_dev(host->mmc));
3478     
3479     pr_info("%s: slot status change detected(%d-%d)\n",mmc_hostname(host->mmc), host->oldstatus, status);
3480     
3481     if (status ^ host->oldstatus)
3482     {        
3483         if (status) 
3484         {
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);
3488                     #endif
3489                     
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));
3493         }
3494         else 
3495         {
3496             clear_bit(RK29_SDMMC_CARD_PRESENT, &host->flags);
3497             rk29_sdmmc_detect_change((unsigned long)host);
3498         }
3499     }
3500
3501     host->oldstatus = status;
3502 }
3503
3504 static void rk29_sdmmc1_status_notify_cb(int card_present, void *dev_id)
3505 {
3506         struct rk29_sdmmc *host = dev_id;
3507
3508         rk29_sdmmc1_check_status((unsigned long)host);
3509 }
3510
3511
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)
3515 {
3516         int ret;
3517     struct rk29_sdmmc *host =  container_of(work, struct rk29_sdmmc, work.work);
3518
3519     rk28_send_wakeup_key();
3520         rk29_sdmmc_detect_change(host);                  
3521 }
3522 #endif
3523
3524 static irqreturn_t det_keys_isr(int irq, void *dev_id)
3525 {
3526         struct rk29_sdmmc *host = dev_id;
3527
3528 #if defined(CONFIG_SDMMC0_RK29_SDCARD_DET_FROM_GPIO)
3529         bool present;
3530         bool present_old;
3531         int value;
3532
3533     present = rk29_sdmmc_get_cd(host->mmc);
3534     present_old = test_bit(RK29_SDMMC_CARD_PRESENT, &host->flags);
3535
3536     if(present != present_old)
3537     {
3538         printk(KERN_INFO "\n******************\n%s: present Old=%d ==> New=%d . [%s]\n",\
3539                 __FUNCTION__,  present_old, present,  host->dma_name);
3540
3541     #if 1
3542         value = gpio_get_value(host->det_pin.io) ?IRQ_TYPE_EDGE_FALLING : IRQ_TYPE_EDGE_RISING;
3543     
3544             irq_set_irq_type(host->gpio_irq, value);
3545     #endif
3546     
3547         #if 1
3548         del_timer(&host->request_timer);
3549         del_timer(&host->DTO_timer);
3550         rk29_sdmmc_dealwith_timeout(host);              
3551         #endif
3552         enable_irq_wake(host->gpio_irq);
3553                     
3554         if(present)
3555         {
3556             set_bit(RK29_SDMMC_CARD_PRESENT, &host->flags);
3557                 schedule_delayed_work(&host->work, msecs_to_jiffies(500));
3558         }
3559         else
3560         {
3561             clear_bit(RK29_SDMMC_CARD_PRESENT, &host->flags);
3562             host->mmc->re_initialized_flags = 0;
3563                 schedule_delayed_work(&host->work, 0);
3564         }
3565     }
3566 #else
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);
3570 #endif  
3571         return IRQ_HANDLED;
3572 }
3573
3574 static int rk29_sdmmc_probe(struct platform_device *pdev)
3575 {
3576         struct mmc_host                 *mmc;
3577         struct rk29_sdmmc               *host;
3578         struct resource                 *regs;
3579         struct rk29_sdmmc_platform_data *pdata;
3580     int level_value;
3581         int                             ret = 0;
3582
3583 #if defined(CONFIG_RK29_SDIO_IRQ_FROM_GPIO)     
3584         unsigned long trigger_flags;
3585 #endif
3586
3587     /* must have platform data */
3588         pdata = pdev->dev.platform_data;
3589         if (!pdata) {
3590                 dev_err(&pdev->dev, "Platform data missing\n");
3591                 ret = -ENODEV;
3592                 goto out;
3593         }
3594
3595         regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3596         if (!regs)
3597         {
3598                 return -ENXIO;
3599         }
3600
3601         mmc = mmc_alloc_host(sizeof(struct rk29_sdmmc), &pdev->dev);
3602         if (!mmc)
3603         {
3604                 ret = -ENOMEM;
3605                 goto rel_regions;
3606         }       
3607     
3608         host = mmc_priv(mmc);
3609     host->mmc = mmc;
3610     host->pdev = pdev;
3611
3612         host->ctype = 0; // set default 1 bit mode
3613         host->errorstep = 0;
3614         host->bus_mode = 5;
3615         host->old_cmd = 100;
3616         host->clock =0;
3617         host->old_div = 0xFF;
3618         host->error_times = 0;
3619         host->state = STATE_IDLE;
3620         host->complete_done = 0;
3621         host->retryfunc = 0;
3622         host->mrq = NULL;
3623         host->new_mrq = NULL;
3624         host->irq_state = true;
3625         host->timeout_times = 0;
3626
3627         //detect pin info
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;
3633     //power pin info
3634     host->gpio_power_en = pdata->power_en;
3635     host->gpio_power_en_level = pdata->power_en_level;
3636
3637     host->set_iomux = pdata->set_iomux;
3638
3639 #if defined(CONFIG_RK29_SDIO_IRQ_FROM_GPIO)
3640     if(RK29_CTRL_SDIO1_ID == host->pdev->id)
3641     {
3642         host->sdio_INT_gpio = pdata->sdio_INT_gpio;
3643         #ifdef USE_SDIO_INT_LEVEL
3644         host->trigger_level = pdata->sdio_INT_level;
3645         #endif
3646     }
3647 #endif
3648
3649         if(pdata->io_init)
3650                 pdata->io_init();
3651                 
3652         spin_lock_init(&host->lock);
3653     
3654 #ifdef RK29_SDMMC_LIST_QUEUE    
3655         INIT_LIST_HEAD(&host->queue);
3656 #endif  
3657
3658         host->clk = clk_get(&pdev->dev, "mmc");
3659
3660 #if RK29_SDMMC_DEFAULT_SDIO_FREQ
3661     clk_set_rate(host->clk,SDHC_FPP_FREQ);
3662 #else    
3663     if(RK29_CTRL_SDMMC_ID== host->pdev->id)
3664             clk_set_rate(host->clk,SDHC_FPP_FREQ);
3665         else
3666             clk_set_rate(host->clk,RK29_MAX_SDIO_FREQ); 
3667
3668 #endif
3669
3670         clk_enable(host->clk);
3671         clk_enable(clk_get(&pdev->dev, "hclk_mmc"));
3672
3673         ret = -ENOMEM;
3674         host->regs = ioremap(regs->start, regs->end - regs->start + 1);
3675         if (!host->regs)
3676         {
3677             host->errorstep = 0x8A;
3678             goto err_freemap; 
3679         }
3680
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;
3684 #endif  
3685         mmc->f_min = FOD_FREQ;
3686         
3687 #if RK29_SDMMC_DEFAULT_SDIO_FREQ
3688     mmc->f_max = SDHC_FPP_FREQ;
3689 #else
3690     if(RK29_CTRL_SDMMC_ID== host->pdev->id)
3691     {
3692         mmc->f_max = SDHC_FPP_FREQ;
3693     }
3694     else
3695     {
3696         mmc->f_max = RK29_MAX_SDIO_FREQ;
3697     }
3698
3699 #endif 
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;
3705         mmc->doneflag = 1;
3706         mmc->sdmmc_host_hw_init = rk29_sdmmc_hw_init;
3707
3708     /*
3709          * We can do SGIO
3710         */
3711 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 37))
3712         mmc->max_segs = 64;
3713 #else
3714         mmc->max_phys_segs = 64;
3715         mmc->max_hw_segs = 64; 
3716 #endif
3717
3718         /*
3719          * Block size can be up to 2048 bytes, but must be a power of two.
3720         */
3721         mmc->max_blk_size = 4095;
3722
3723         /*
3724          * No limit on the number of blocks transferred.
3725         */
3726         mmc->max_blk_count = 4096; 
3727
3728         /*
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.
3731         */
3732         mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count; //8M bytes(2K*4K)
3733
3734     /*
3735          * Set the maximum segment size.  Since we aren't doing DMA
3736          * (yet) we are only limited by the data length register.
3737         */
3738         mmc->max_seg_size = mmc->max_req_size;
3739
3740         tasklet_init(&host->tasklet, rk29_sdmmc_tasklet_func, (unsigned long)host);
3741
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);
3746
3747         host->irq = platform_get_irq(pdev, 0);
3748         if (host->irq < 0)
3749         {
3750             host->errorstep = 0x8B;
3751                 ret = -EINVAL;
3752                 goto err_freemap;
3753         }
3754
3755     memcpy(host->dma_name, pdata->dma_name, 8);    
3756         host->use_dma = pdata->use_dma;
3757
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);
3760
3761         /*DMA init*/
3762         if(host->use_dma)
3763         {
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); 
3766         if (ret < 0)
3767         {
3768                 printk(KERN_WARNING "%s..%d...rk29_dma_request error=%d. [%s]\n", \
3769                                         __FUNCTION__, __LINE__,ret, host->dma_name);
3770                 host->errorstep = 0x97;
3771             goto err_freemap; 
3772         }
3773         
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)
3778         {
3779             ret = rk29_dma_config(host->dma_info.chn, 4, 16);
3780         }
3781         else
3782         {
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);  
3786             #else
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); 
3789             #endif 
3790         }
3791 #endif
3792         if(ret < 0)
3793                 {
3794             printk(KERN_WARNING "%s..%d..  rk29_dma_config error=%d. [%s]\n", \
3795                                         __FUNCTION__, __LINE__, ret, host->dma_name);
3796             host->errorstep = 0x98;
3797             goto err_dmaunmap;
3798                 }
3799
3800         ret = rk29_dma_set_buffdone_fn(host->dma_info.chn, rk29_sdmmc_dma_complete);    
3801                 if(ret < 0)
3802                 {
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;
3806             goto err_dmaunmap;
3807                 }
3808                 
3809                 host->dma_addr = regs->start + SDMMC_DATA;
3810         }
3811
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;
3815 #endif  
3816
3817 #if defined(CONFIG_ARCH_RK29)
3818    if(RK29_CTRL_SDMMC_ID == host->pdev->id)   
3819     {
3820        rk29_sdmmc_hw_init(host);
3821     }
3822 #endif
3823
3824     ret = request_irq(host->irq, rk29_sdmmc_interrupt, 0, dev_name(&pdev->dev), host);
3825         if (ret)
3826         {       
3827
3828             printk(KERN_WARNING "%s..%d..  request_irq error=%d. [%s]\n", \
3829                                 __FUNCTION__, __LINE__, ret, host->dma_name);
3830             host->errorstep = 0x8C;
3831             goto err_dmaunmap;
3832         }
3833
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))
3836     {
3837         INIT_DELAYED_WORK(&host->work, rk29_sdmmc_detect_change_work);
3838         ret = gpio_request(host->det_pin.io, "sd_detect");
3839                 if(ret < 0) {
3840                         dev_err(&pdev->dev, "gpio_request error\n");
3841                         goto err_dmaunmap;
3842                 }
3843                 gpio_direction_input(host->det_pin.io);
3844
3845         level_value = gpio_get_value(host->det_pin.io);       
3846         
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,
3850                                             "sd_detect",
3851                                             host);
3852                 if(ret < 0) {
3853                         dev_err(&pdev->dev, "gpio request_irq error\n");
3854                         goto err_dmaunmap;
3855                 }
3856
3857                 enable_irq_wake(host->gpio_irq);
3858     }
3859 #elif DRIVER_SDMMC_USE_NEW_IOMUX_API
3860     if(RK29_CTRL_SDMMC_ID == host->pdev->id)
3861     {
3862         iomux_set(MMC0_DETN);
3863     }
3864
3865 #endif
3866         
3867 #if defined(CONFIG_RK29_SDIO_IRQ_FROM_GPIO)
3868     if(RK29_CTRL_SDIO1_ID == host->pdev->id)
3869     {
3870         gpio_request(host->sdio_INT_gpio, "sdio_interrupt");
3871
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
3876
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;
3880         #else
3881         trigger_flags = IRQF_TRIGGER_LOW;
3882         #endif
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,
3885                     trigger_flags,
3886                     "sdio_interrupt",
3887                     host);                    
3888         if (ret)
3889         {       
3890
3891             printk("%s..%d..  sdio_request_INT_irq error=%d ====xbw[%s]====\n", \
3892                                 __FUNCTION__, __LINE__, ret, host->dma_name);
3893             host->errorstep = 0x8D;
3894             goto err_dmaunmap;
3895         }
3896         
3897         disable_irq_nosync(host->sdio_irq);
3898         enable_irq_wake(host->sdio_irq);
3899     }
3900
3901 #endif
3902     
3903     /* setup sdmmc1 wifi card detect change */
3904     if (pdata->register_status_notify) {
3905         pdata->register_status_notify(rk29_sdmmc1_status_notify_cb, host);
3906     }
3907
3908     if(RK29_CTRL_SDMMC_ID== host->pdev->id)
3909     {
3910         if(rk29_sdmmc_get_cd(host->mmc))
3911         {
3912             set_bit(RK29_SDMMC_CARD_PRESENT, &host->flags);
3913         }
3914         else
3915         {
3916             clear_bit(RK29_SDMMC_CARD_PRESENT, &host->flags);
3917         }
3918     }
3919     else
3920     {
3921         #if defined(CONFIG_USE_SDMMC0_FOR_WIFI_DEVELOP_BOARD)
3922         if(0== host->pdev->id)
3923         {
3924             set_bit(RK29_SDMMC_CARD_PRESENT, &host->flags);
3925         }
3926         #endif
3927
3928         #if defined(CONFIG_USE_SDMMC1_FOR_WIFI_DEVELOP_BOARD)
3929         if(1== host->pdev->id)
3930         {
3931             set_bit(RK29_SDMMC_CARD_PRESENT, &host->flags);
3932         }
3933         #endif
3934     }
3935
3936
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);
3942         }else {
3943             clear_bit(RK29_SDMMC_CARD_PRESENT, &host->flags);
3944         }
3945     }
3946
3947
3948         platform_set_drvdata(pdev, mmc);        
3949
3950         mmc_add_host(mmc);
3951
3952 #ifdef RK29_SDMMC_NOTIFY_REMOVE_INSERTION
3953     
3954     globalSDhost[pdev->id] = (struct rk29_sdmmc *)host;
3955     if(0== host->pdev->id)
3956     {
3957         rk29_sdmmc_progress_add_attr(pdev);
3958     }
3959 #endif  
3960         
3961 #if defined (CONFIG_DEBUG_FS)
3962         rk29_sdmmc_init_debugfs(host);
3963 #endif
3964
3965     printk(KERN_INFO ".Line%d..The End of SDMMC-probe %s.  [%s]\n", __LINE__, RK29_SDMMC_VERSION,host->dma_name);
3966         return 0;
3967
3968
3969 err_dmaunmap:
3970         if(host->use_dma)
3971         {
3972             rk29_dma_free(host->dma_info.chn, &host->dma_info.client);
3973         }
3974
3975 err_freemap:
3976         iounmap(host->regs);
3977
3978 rel_regions:
3979     mmc_free_host(mmc);
3980
3981 out:
3982         
3983         return ret;
3984 }
3985
3986
3987
3988 static int __exit rk29_sdmmc_remove(struct platform_device *pdev)
3989 {
3990
3991     struct mmc_host *mmc = platform_get_drvdata(pdev);
3992     struct rk29_sdmmc *host;
3993     struct resource             *regs;
3994
3995     if (!mmc)
3996         return -1;
3997
3998     host = mmc_priv(mmc); 
3999     
4000         smp_wmb();
4001     rk29_sdmmc_control_clock(host, 0);
4002
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);
4007
4008         tasklet_disable(&host->tasklet);
4009         free_irq(platform_get_irq(pdev, 0), host);
4010         if(host->use_dma)
4011         {
4012                 rk29_dma_free(host->dma_info.chn, &host->dma_info.client);
4013         }
4014
4015         mmc_remove_host(mmc);
4016
4017         iounmap(host->regs);
4018         
4019         regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
4020         release_mem_region(regs->start,resource_size(regs));  
4021
4022     mmc_free_host(mmc);
4023     platform_set_drvdata(pdev, NULL);
4024
4025         return 0;
4026 }
4027
4028
4029 #ifdef CONFIG_PM
4030
4031 static int rk29_sdmmc_sdcard_suspend(struct rk29_sdmmc *host)
4032 {
4033         int ret = 0;
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.
4038     #else
4039     rk29_mux_api_set(host->det_pin.iomux.name, host->det_pin.iomux.fgpio);
4040     #endif
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);
4044
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,
4048                                             "sd_detect",
4049                                             host);
4050         
4051         enable_irq_wake(host->gpio_irq);
4052         
4053 #endif
4054         return ret;
4055 }
4056
4057 static void rk29_sdmmc_sdcard_resume(struct rk29_sdmmc *host)
4058 {
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);
4065         #else
4066     rk29_mux_api_set(host->det_pin.iomux.name, host->det_pin.iomux.fmux);
4067     #endif
4068     rk29_sdmmc_enable_irq(host, true);
4069 #endif
4070 }
4071
4072 static int rk29_sdmmc_suspend(struct platform_device *pdev, pm_message_t state)
4073 {
4074     struct mmc_host *mmc = platform_get_drvdata(pdev);
4075     struct rk29_sdmmc *host = mmc_priv(mmc);
4076     int ret = 0;
4077
4078     if(host && host->pdev && (RK29_CTRL_SDMMC_ID == host->pdev->id)) //only the SDMMC0 have suspend-resume; noted by xbw
4079     {
4080         if (mmc)
4081 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 35))
4082             ret = mmc_suspend_host(mmc);
4083 #else
4084             ret = mmc_suspend_host(mmc, state);
4085 #endif
4086
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");
4090 #endif    
4091     }
4092 #if RK_SDMMC_USE_SDIO_SUSPEND_RESUME    
4093     else if(host && host->pdev && (RK29_CTRL_SDIO1_ID == host->pdev->id))
4094     {
4095         if (mmc)
4096 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 35))
4097             ret = mmc_suspend_host(mmc);
4098 #else
4099             ret = mmc_suspend_host(mmc, state);
4100 #endif
4101         if(!ret)
4102         {
4103             clk_disable(host->clk);
4104             clk_disable(clk_get(&pdev->dev, "hclk_mmc"));
4105         }
4106     }
4107 #endif // --#if RK_SDMMC_USE_SDIO_SUSPEND_RESUME
4108
4109     return ret;
4110 }
4111
4112 static int rk29_sdmmc_resume(struct platform_device *pdev)
4113 {
4114     struct mmc_host *mmc = platform_get_drvdata(pdev);
4115     struct rk29_sdmmc *host = mmc_priv(mmc);
4116     int ret = 0;
4117
4118     if(host && host->pdev && (RK29_CTRL_SDMMC_ID == host->pdev->id)) //only the SDMMC0 have suspend-resume; noted by xbw
4119     {
4120         if (mmc)
4121         {
4122             
4123             #if !defined(CONFIG_SDMMC0_RK29_SDCARD_DET_FROM_GPIO)
4124             rk29_sdmmc_sdcard_resume(host);     
4125             #endif
4126             
4127                 ret = mmc_resume_host(mmc);
4128         }
4129     }
4130 #if RK_SDMMC_USE_SDIO_SUSPEND_RESUME        
4131     else if(host && host->pdev && (RK29_CTRL_SDIO1_ID == host->pdev->id))
4132     {
4133         if (mmc)
4134         {
4135                 clk_enable(host->clk);
4136                clk_enable(clk_get(&pdev->dev, "hclk_mmc"));
4137                 mdelay(20);
4138                 ret = mmc_resume_host(mmc);
4139         }
4140     } 
4141 #endif // --#if RK_SDMMC_USE_SDIO_SUSPEND_RESUME
4142
4143         return ret;
4144 }
4145 #else
4146 #define rk29_sdmmc_suspend      NULL
4147 #define rk29_sdmmc_resume       NULL
4148 #endif
4149
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),
4154         .driver         = {
4155                 .name           = "rk29_sdmmc",
4156         },
4157 };
4158
4159 static int __init rk29_sdmmc_init(void)
4160 {
4161         return platform_driver_probe(&rk29_sdmmc_driver, rk29_sdmmc_probe);
4162 }
4163
4164 static void __exit rk29_sdmmc_exit(void)
4165 {
4166         platform_driver_unregister(&rk29_sdmmc_driver);
4167 }
4168
4169 module_init(rk29_sdmmc_init);
4170 module_exit(rk29_sdmmc_exit);
4171
4172 MODULE_DESCRIPTION("Rk29 Multimedia Card Interface driver");
4173 MODULE_AUTHOR("xbw@rock-chips.com");
4174 MODULE_LICENSE("GPL v2");
4175