mmc: Did not change the actual module funciton, only to standardized printed information
[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
47 #include <asm/dma.h>
48 #include <mach/dma-pl330.h>
49 #include <asm/scatterlist.h>
50
51 #include "rk29_sdmmc.h"
52
53
54 #define RK29_SDMMC_xbw_Debug 0
55
56 #if RK29_SDMMC_xbw_Debug 
57 int debug_level = 5;
58 #define xbwprintk(n, format, arg...) \
59         if (n <= debug_level) {  \
60                 printk(format,##arg); \
61         }
62 #else
63 #define xbwprintk(n, arg...)
64 #endif
65
66 #if defined(CONFIG_ARCH_RK29)
67 #define SDMMC_USE_INT_UNBUSY     0
68 #else
69 #define SDMMC_USE_INT_UNBUSY     1
70 #endif
71
72 #define RK29_SDMMC_ERROR_FLAGS          (SDMMC_INT_FRUN | SDMMC_INT_HLE )
73
74 #if SDMMC_USE_INT_UNBUSY
75 #define RK29_SDMMC_INTMASK_USEDMA   (SDMMC_INT_CMD_DONE | SDMMC_INT_DTO | SDMMC_INT_UNBUSY |RK29_SDMMC_ERROR_FLAGS | SDMMC_INT_CD)
76 #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 )
77 #else
78 #define RK29_SDMMC_INTMASK_USEDMA   (SDMMC_INT_CMD_DONE | SDMMC_INT_DTO | RK29_SDMMC_ERROR_FLAGS | SDMMC_INT_CD)
79 #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 )
80 #endif
81
82 #define RK29_SDMMC_SEND_START_TIMEOUT   3000  //The time interval from the time SEND_CMD to START_CMD_BIT cleared.
83 #define RK29_ERROR_PRINTK_INTERVAL      200   //The time interval between the two printk for the same error. 
84 #define RK29_SDMMC_WAIT_DTO_INTERNVAL   4500  //The time interval from the CMD_DONE_INT to DTO_INT
85 #define RK29_SDMMC_REMOVAL_DELAY        2000  //The time interval from the CD_INT to detect_timer react.
86
87 #define RK29_SDMMC_VERSION "Ver.3.06 The last modify date is 2012-04-05"
88
89 #if !defined(CONFIG_USE_SDMMC0_FOR_WIFI_DEVELOP_BOARD)  
90 #define RK29_CTRL_SDMMC_ID   0  //mainly used by SDMMC
91 #define RK29_CTRL_SDIO1_ID   1  //mainly used by sdio-wifi
92 #define RK29_CTRL_SDIO2_ID   2  //mainly used by sdio-card
93 #else
94 #define RK29_CTRL_SDMMC_ID   5  
95 #define RK29_CTRL_SDIO1_ID   1  
96 #define RK29_CTRL_SDIO2_ID   2  
97 #endif
98
99 #define SDMMC_CLOCK_TEST     0
100
101 #define RK29_SDMMC_NOTIFY_REMOVE_INSERTION /* use sysfs to notify the removal or insertion of sd-card*/
102 //#define RK29_SDMMC_LIST_QUEUE            /* use list-queue for multi-card*/
103
104 #define RK29_SDMMC_DEFAULT_SDIO_FREQ   0 // 1--run in default frequency(50Mhz); 0---run in 25Mhz, 
105 #define RK29_MAX_SDIO_FREQ   25000000    //set max-sdio-frequency 25Mhz at the present time¡£
106
107 enum {
108         EVENT_CMD_COMPLETE = 0,
109         EVENT_DATA_COMPLETE,
110         EVENT_DATA_UNBUSY,
111         EVENT_DATA_ERROR,
112         EVENT_XFER_ERROR
113 };
114
115 enum rk29_sdmmc_state {
116         STATE_IDLE = 0,
117         STATE_SENDING_CMD,
118         STATE_DATA_BUSY,
119         STATE_DATA_UNBUSY,
120         STATE_DATA_END,
121         STATE_SENDING_STOP,
122 };
123
124 struct rk29_sdmmc_dma_info {
125         enum dma_ch chn;
126         char *name;
127         struct rk29_dma_client client;
128 };
129
130 static struct rk29_sdmmc_dma_info rk29_sdmmc_dma_infos[]= {
131         {
132                 .chn = DMACH_SDMMC,
133                 .client = {
134                         .name = "rk29-dma-sdmmc0",
135                 }
136         },
137         {
138                 .chn = DMACH_SDIO,
139                 .client = {
140                         .name = "rk29-dma-sdio1",
141                 }
142         },
143
144         {
145                 .chn = DMACH_EMMC,
146                 .client = {
147                         .name = "rk29-dma-sdio2",
148                 }
149         },
150 };
151
152
153 /* Interrupt Information */
154 typedef struct TagSDC_INT_INFO
155 {
156     u32     transLen;               //the length of data sent.
157     u32     desLen;                 //the total length of the all data.
158     u32    *pBuf;                   //the data buffer for interrupt read or write.
159 }SDC_INT_INFO_T;
160
161
162 struct rk29_sdmmc {
163         spinlock_t              lock;
164         void __iomem    *regs;
165         struct clk              *clk;
166
167         struct mmc_request      *mrq;
168         struct mmc_request      *new_mrq;
169         struct mmc_command      *cmd;
170         struct mmc_data         *data;
171
172         dma_addr_t              dma_addr;;
173         unsigned int    use_dma:1;
174         char                    dma_name[8];
175         u32                     cmd_status;
176         u32                     data_status;
177         u32                     stop_cmdr;
178
179     u32         old_div;
180         u32                     cmdr;   //the value setted into command-register
181         u32                     dodma;  //sign the DMA used for transfer.
182         u32         errorstep;//record the error point.
183         u32         *pbuf;
184         SDC_INT_INFO_T    intInfo; 
185     struct rk29_sdmmc_dma_info  dma_info;
186     int         irq;
187         int error_times;
188         u32 old_cmd;
189         
190         struct tasklet_struct   tasklet;
191         unsigned long           pending_events;
192         unsigned long           completed_events;
193         enum rk29_sdmmc_state   state;
194
195 #ifdef RK29_SDMMC_LIST_QUEUE
196         struct list_head        queue;
197         struct list_head        queue_node;
198 #endif
199
200         u32                     bus_hz;
201         struct platform_device  *pdev;
202         struct mmc_host         *mmc;
203         u32                     ctype;
204         unsigned int            clock;
205         unsigned long           flags;
206         
207 #define RK29_SDMMC_CARD_PRESENT 0
208
209         int                     id;
210
211         struct timer_list       detect_timer; 
212         struct timer_list       request_timer; //the timer for INT_CMD_DONE
213         struct timer_list       DTO_timer;     //the timer for INT_DTO
214         struct mmc_command      stopcmd;
215
216         /* flag for current bus settings */
217     u32 bus_mode;
218
219     unsigned int            oldstatus;
220     unsigned int            complete_done;
221     unsigned int            retryfunc;
222     
223 #ifdef CONFIG_PM
224     int gpio_irq;
225         int gpio_det;
226 #endif
227
228 #if defined(CONFIG_SDMMC0_RK29_WRITE_PROTECT) || defined(CONFIG_SDMMC1_RK29_WRITE_PROTECT)
229     int write_protect;
230 #endif
231
232     void (*set_iomux)(int device_id, unsigned int bus_width);
233
234 };
235
236
237 #ifdef RK29_SDMMC_NOTIFY_REMOVE_INSERTION
238 static struct rk29_sdmmc    *globalSDhost[3];
239 #endif
240
241 #define rk29_sdmmc_test_and_clear_pending(host, event)          \
242         test_and_clear_bit(event, &host->pending_events)
243 #define rk29_sdmmc_test_pending(host, event)            \
244         test_bit(event, &host->pending_events)
245 #define rk29_sdmmc_test_completed(host, event)           \
246         test_bit(event, &host->completed_events)       
247 #define rk29_sdmmc_set_completed(host, event)                   \
248         set_bit(event, &host->completed_events)
249 #define rk29_sdmmc_set_pending(host, event)                             \
250         set_bit(event, &host->pending_events)
251
252 static void rk29_sdmmc_start_error(struct rk29_sdmmc *host);
253 static int rk29_sdmmc_clear_fifo(struct rk29_sdmmc *host);
254 int rk29_sdmmc_hw_init(void *data);
255
256 static void rk29_sdmmc_write(unsigned char  __iomem     *regbase, unsigned int regOff,unsigned int val)
257 {
258         __raw_writel(val,regbase + regOff);
259 }
260
261 static unsigned int rk29_sdmmc_read(unsigned char  __iomem      *regbase, unsigned int regOff)
262 {
263         return __raw_readl(regbase + regOff);
264 }
265
266 static int rk29_sdmmc_regs_printk(struct rk29_sdmmc *host)
267 {
268         printk("SDMMC_CTRL:   \t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_CTRL));
269         printk("SDMMC_PWREN:  \t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_PWREN));
270         printk("SDMMC_CLKDIV: \t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_CLKDIV));
271         printk("SDMMC_CLKSRC: \t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_CLKSRC));
272         printk("SDMMC_CLKENA: \t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_CLKENA));
273         printk("SDMMC_TMOUT:  \t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_TMOUT));
274         printk("SDMMC_CTYPE:  \t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_CTYPE));
275         printk("SDMMC_BLKSIZ: \t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_BLKSIZ));
276         printk("SDMMC_BYTCNT: \t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_BYTCNT));
277         printk("SDMMC_INTMASK:\t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_INTMASK));
278         printk("SDMMC_CMDARG: \t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_CMDARG));
279         printk("SDMMC_CMD:    \t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_CMD));
280         printk("SDMMC_RESP0:  \t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_RESP0));
281         printk("SDMMC_RESP1:  \t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_RESP1));
282         printk("SDMMC_RESP2:  \t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_RESP2));
283         printk("SDMMC_RESP3:  \t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_RESP3));
284         printk("SDMMC_MINTSTS:\t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_MINTSTS));
285         printk("SDMMC_RINTSTS:\t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_RINTSTS));
286         printk("SDMMC_STATUS: \t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_STATUS));
287         printk("SDMMC_FIFOTH: \t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_FIFOTH));
288         printk("SDMMC_CDETECT:\t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_CDETECT));
289         printk("SDMMC_WRTPRT: \t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_WRTPRT));
290         printk("SDMMC_TCBCNT: \t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_TCBCNT));
291         printk("SDMMC_TBBCNT: \t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_TBBCNT));
292         printk("SDMMC_DEBNCE: \t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_DEBNCE));
293     printk("SDMMC_USRID: \t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_USRID));
294
295 #if !defined(CONFIG_ARCH_RK29)          
296     printk("SDMMC_VERID: \t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_VERID));
297         printk("SDMMC_UHS_REG:\t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_UHS_REG));
298         printk("SDMMC_RST_n: \t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_RST_n));
299         printk("SDMMC_CARDTHRCTL: \t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_CARDTHRCTL));
300         printk("SDMMC_BACK_END_POWER: \t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_BACK_END_POWER));
301 #endif  
302         printk("=======printk %s-register end =========\n", host->dma_name);
303         return 0;
304 }
305
306
307 #ifdef RK29_SDMMC_NOTIFY_REMOVE_INSERTION
308 ssize_t rk29_sdmmc_progress_store(struct kobject *kobj, struct kobj_attribute *attr,
309                          const char *buf, size_t count)
310 {
311     struct rk29_sdmmc   *host = NULL;
312     static u32 unmounting_times = 0;
313     static char oldbuf[64];
314     
315     if( !strncmp(buf,"version" , strlen("version")))
316     {
317         printk(KERN_INFO "\n The driver SDMMC named 'rk29_sdmmc.c' is %s.\n", RK29_SDMMC_VERSION);
318         return count;
319     }
320     
321     //envalue the address of host base on input-parameter.
322     if( !strncmp(buf,"sd-" , strlen("sd-")) )
323     {
324         host = (struct rk29_sdmmc       *)globalSDhost[0];
325         if(!host)
326         {
327             printk(KERN_ERR "%s..%d.. fail to call progress_store because the host is null. \n",__FUNCTION__,__LINE__);
328             return count;
329         }
330     }    
331     else if(  !strncmp(buf,"sdio1-" , strlen("sdio1-")) )
332     {
333         host = (struct rk29_sdmmc       *)globalSDhost[RK29_CTRL_SDIO1_ID];
334         if(!host)
335         {
336             printk(KERN_ERR "%s..%d.. fail to call progress_store because the host-sdio1 is null.\n",__FUNCTION__,__LINE__);
337             return count;
338         }
339     }
340     else if(  !strncmp(buf,"sdio2-" , strlen("sdio2-")) )
341     {
342         host = (struct rk29_sdmmc       *)globalSDhost[RK29_CTRL_SDIO2_ID];
343         if(!host)
344         {
345             printk(KERN_ERR "%s..%d.. fail to call progress_store because the host-sdio2 is null.\n",__FUNCTION__,__LINE__);
346             return count;
347         }
348     }
349     else
350     {
351         printk(KERN_ERR "%s..%d.. You want to use sysfs for SDMMC but input-parameter is wrong.\n",__FUNCTION__,__LINE__);
352         return count;
353     }
354
355     spin_lock(&host->lock);
356     if(strncmp(buf,oldbuf , strlen(buf)))
357     {
358             printk(KERN_INFO ".%d.. MMC0 receive the message %s from VOLD.[%s]\n", __LINE__, buf, host->dma_name);
359             strcpy(oldbuf, buf);
360         }
361
362         /*
363      *  //deal with the message
364      *  insert card state-change:  No-Media ==> Pending ==> Idle-Unmounted ==> Checking ==>Mounted
365      *  remove card state-change:  Unmounting ==> Idle-Unmounted ==> No-Media
366     */
367     #if !defined(CONFIG_USE_SDMMC0_FOR_WIFI_DEVELOP_BOARD)
368     if(RK29_CTRL_SDMMC_ID == host->pdev->id)
369     {
370         if(!strncmp(buf, "sd-Unmounting", strlen("sd-Unmounting")))
371         {
372             if(unmounting_times++%10 == 0)
373             {
374                 printk(KERN_INFO ".%d.. MMC0 receive the message Unmounting(waitTimes=%d) from VOLD.[%s]\n", \
375                     __LINE__, unmounting_times, host->dma_name);
376             }
377
378             if(0 == host->mmc->re_initialized_flags)
379                 mod_timer(&host->detect_timer, jiffies + msecs_to_jiffies(RK29_SDMMC_REMOVAL_DELAY*2));
380         }
381         else if(!strncmp(buf, "sd-Idle-Unmounted", strlen("sd-Idle-Unmounted")))
382         {
383             if(0 == host->mmc->re_initialized_flags)
384                 mod_timer(&host->detect_timer, jiffies + msecs_to_jiffies(RK29_SDMMC_REMOVAL_DELAY*2));
385         }
386         else if( !strncmp(buf, "sd-No-Media", strlen("sd-No-Media")))
387         {
388             printk(KERN_INFO ".%d.. MMC0 receive the message No-Media from VOLD. waitTimes=%d [%s]\n" ,\
389                 __LINE__,unmounting_times, host->dma_name);
390                 
391             del_timer_sync(&host->detect_timer);
392             host->mmc->re_initialized_flags = 1;
393             unmounting_times = 0;
394             
395             if(test_bit(RK29_SDMMC_CARD_PRESENT, &host->flags))
396             {
397                 mmc_detect_change(host->mmc, 0);
398             }
399         }
400         else if( !strncmp(buf, "sd-Ready", strlen("sd-Ready")))
401         {
402             printk(KERN_INFO ".%d.. MMC0 receive the message Ready(ReInitFlag=%d) from VOLD. waitTimes=%d [%s]\n" ,\
403                 __LINE__, host->mmc->re_initialized_flags, unmounting_times, host->dma_name);
404                                                                 
405             unmounting_times = 0;
406                         host->mmc->re_initialized_flags = 1;            
407         }
408         else if( !strncmp(buf,"sd-reset" , strlen("sd-reset")) ) 
409         {
410             printk(KERN_INFO ".%d.. Now manual reset for SDMMC0. [%s]\n",__LINE__, host->dma_name);
411             rk29_sdmmc_hw_init(host);
412             mmc_detect_change(host->mmc, 0);           
413         }
414         else if( !strncmp(buf, "sd-regs", strlen("sd-regs")))
415         {
416             printk(KERN_INFO ".%d.. Now printk the register of SDMMC0. [%s]\n",__LINE__, host->dma_name); 
417             rk29_sdmmc_regs_printk(host);
418         }
419
420     }
421     #else
422     if(0 == host->pdev->id)
423     {
424         if( !strncmp(buf,"sd-reset" , strlen("sd-reset")) ) 
425         {
426             printk(KERN_INFO ".%d.. Now manual reset for SDMMC0. [%s]\n",__LINE__, host->dma_name);
427             rk29_sdmmc_hw_init(host);
428             mmc_detect_change(host->mmc, 0);           
429         }
430         else if( !strncmp(buf, "sd-regs", strlen("sd-regs")))
431         {
432             printk(KERN_INFO ".%d.. Now printk the register of SDMMC0. [%s]\n",__LINE__, host->dma_name); 
433             rk29_sdmmc_regs_printk(host);
434         }
435     }
436     #endif
437     else if(RK29_CTRL_SDIO1_ID == host->pdev->id)
438     {
439         if( !strncmp(buf, "sdio1-regs", strlen("sdio1-regs")))
440         {
441             printk(KERN_INFO ".%d.. Now printk the register of SDMMC1. [%s]\n",__LINE__, host->dma_name); 
442             rk29_sdmmc_regs_printk(host);
443         }
444         else if( !strncmp(buf,"sdio1-reset" , strlen("sdio1-reset")) ) 
445         {
446             printk(KERN_INFO ".%d.. Now manual reset for SDMMC1. [%s]\n",__LINE__, host->dma_name);
447             rk29_sdmmc_hw_init(host);
448             mmc_detect_change(host->mmc, 0);           
449         }
450     }
451     else if(RK29_CTRL_SDIO2_ID == host->pdev->id)
452     {
453         if( !strncmp(buf, "sdio2-regs", strlen("sdio2-regs")))
454         {
455             printk(KERN_INFO ".%d.. Now printk the register of SDMMC2. [%s]\n",__LINE__, host->dma_name); 
456             rk29_sdmmc_regs_printk(host);
457         }
458         else if( !strncmp(buf,"sdio2-reset" , strlen("sdio2-reset")) ) 
459         {
460             printk(KERN_INFO ".%d.. Now manual reset for SDMMC2. [%s]\n",__LINE__, host->dma_name);
461             rk29_sdmmc_hw_init(host);
462             mmc_detect_change(host->mmc, 0);           
463         }
464     }
465     
466     spin_unlock(&host->lock);
467     
468     return count;
469 }
470
471
472
473 struct kobj_attribute mmc_reset_attrs = 
474 {
475         .attr = {
476                 .name = "rescan",
477                 .mode = 0764},
478         .show = NULL,
479         .store = rk29_sdmmc_progress_store,
480 };
481 struct attribute *mmc_attrs[] = 
482 {
483         &mmc_reset_attrs.attr,
484         NULL
485 };
486
487 static struct kobj_type mmc_kset_ktype = {
488         .sysfs_ops      = &kobj_sysfs_ops,
489         .default_attrs = &mmc_attrs[0],
490 };
491
492 static int rk29_sdmmc_progress_add_attr( struct platform_device *pdev )
493 {
494         int result;
495                  struct kobject *parentkobject; 
496         struct kobject * me = kmalloc(sizeof(struct kobject) , GFP_KERNEL );
497         if(!me)
498         {
499             return -ENOMEM;
500         }
501         memset(me ,0,sizeof(struct kobject));
502         kobject_init( me , &mmc_kset_ktype );
503         
504         parentkobject = &pdev->dev.kobj ;
505                 result = kobject_add( me , parentkobject->parent->parent->parent,"%s", "sd-sdio" );     
506
507         return result;
508 }
509 #endif
510
511 #if defined (CONFIG_DEBUG_FS)
512 static int rk29_sdmmc_regs_show(struct seq_file *s, void *v)
513 {
514         struct rk29_sdmmc       *host = s->private;
515
516         seq_printf(s, "SDMMC_CTRL:   \t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_CTRL));
517         seq_printf(s, "SDMMC_PWREN:  \t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_PWREN));
518         seq_printf(s, "SDMMC_CLKDIV: \t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_CLKDIV));
519         seq_printf(s, "SDMMC_CLKSRC: \t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_CLKSRC));
520         seq_printf(s, "SDMMC_CLKENA: \t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_CLKENA));
521         seq_printf(s, "SDMMC_TMOUT:  \t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_TMOUT));
522         seq_printf(s, "SDMMC_CTYPE:  \t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_CTYPE));
523         seq_printf(s, "SDMMC_BLKSIZ: \t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_BLKSIZ));
524         seq_printf(s, "SDMMC_BYTCNT: \t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_BYTCNT));
525         seq_printf(s, "SDMMC_INTMASK:\t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_INTMASK));
526         seq_printf(s, "SDMMC_CMDARG: \t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_CMDARG));
527         seq_printf(s, "SDMMC_CMD:    \t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_CMD));
528         seq_printf(s, "SDMMC_RESP0:  \t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_RESP0));
529         seq_printf(s, "SDMMC_RESP1:  \t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_RESP1));
530         seq_printf(s, "SDMMC_RESP2:  \t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_RESP2));
531         seq_printf(s, "SDMMC_RESP3:  \t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_RESP3));
532         seq_printf(s, "SDMMC_MINTSTS:\t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_MINTSTS));
533         seq_printf(s, "SDMMC_RINTSTS:\t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_RINTSTS));
534         seq_printf(s, "SDMMC_STATUS: \t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_STATUS));
535         seq_printf(s, "SDMMC_FIFOTH: \t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_FIFOTH));
536         seq_printf(s, "SDMMC_CDETECT:\t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_CDETECT));
537         seq_printf(s, "SDMMC_WRTPRT: \t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_WRTPRT));
538         seq_printf(s, "SDMMC_TCBCNT: \t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_TCBCNT));
539         seq_printf(s, "SDMMC_TBBCNT: \t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_TBBCNT));
540         seq_printf(s, "SDMMC_DEBNCE: \t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_DEBNCE));
541
542         return 0;
543 }
544
545
546 /*
547  * The debugfs stuff below is mostly optimized away when
548  * CONFIG_DEBUG_FS is not set.
549  */
550 static int rk29_sdmmc_req_show(struct seq_file *s, void *v)
551 {
552         struct rk29_sdmmc       *host = s->private;
553         struct mmc_request      *mrq;
554         struct mmc_command      *cmd;
555         struct mmc_command      *stop;
556         struct mmc_data         *data;
557
558         /* Make sure we get a consistent snapshot */
559         spin_lock(&host->lock);
560         
561         mrq = host->mrq;
562
563         if (mrq) {
564                 cmd = mrq->cmd;
565                 data = mrq->data;
566                 stop = mrq->stop;
567
568                 if (cmd)
569                         seq_printf(s,
570                                 "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n",
571                                 cmd->opcode, cmd->arg, cmd->flags,
572                                 cmd->resp[0], cmd->resp[1], cmd->resp[2],
573                                 cmd->resp[2], cmd->error);
574                 if (data)
575                         seq_printf(s, "DATA %u / %u * %u flg %x err %d\n",
576                                 data->bytes_xfered, data->blocks,
577                                 data->blksz, data->flags, data->error);
578                 if (stop)
579                         seq_printf(s,
580                                 "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n",
581                                 stop->opcode, stop->arg, stop->flags,
582                                 stop->resp[0], stop->resp[1], stop->resp[2],
583                                 stop->resp[2], stop->error);
584         }
585
586         spin_unlock(&host->lock);
587
588         return 0;
589 }
590
591 static int rk29_sdmmc_req_open(struct inode *inode, struct file *file)
592 {
593         return single_open(file, rk29_sdmmc_req_show, inode->i_private);
594 }
595
596 static const struct file_operations rk29_sdmmc_req_fops = {
597         .owner          = THIS_MODULE,
598         .open           = rk29_sdmmc_req_open,
599         .read           = seq_read,
600         .llseek         = seq_lseek,
601         .release        = single_release,
602 };
603
604
605 static int rk29_sdmmc_regs_open(struct inode *inode, struct file *file)
606 {
607         return single_open(file, rk29_sdmmc_regs_show, inode->i_private);
608 }
609
610 static const struct file_operations rk29_sdmmc_regs_fops = {
611         .owner          = THIS_MODULE,
612         .open           = rk29_sdmmc_regs_open,
613         .read           = seq_read,
614         .llseek         = seq_lseek,
615         .release        = single_release,
616 };
617
618 static void rk29_sdmmc_init_debugfs(struct rk29_sdmmc *host)
619 {
620         struct mmc_host         *mmc = host->mmc;
621         struct dentry           *root;
622         struct dentry           *node;
623
624         root = mmc->debugfs_root;
625         if (!root)
626                 return;
627
628         node = debugfs_create_file("regs", S_IRUSR, root, host,
629                         &rk29_sdmmc_regs_fops);
630         if (IS_ERR(node))
631                 return;
632         if (!node)
633                 goto err;
634
635         node = debugfs_create_file("req", S_IRUSR, root, host, &rk29_sdmmc_req_fops);
636         if (!node)
637                 goto err;
638
639         node = debugfs_create_u32("state", S_IRUSR, root, (u32 *)&host->state);
640         if (!node)
641                 goto err;
642
643         node = debugfs_create_x32("pending_events", S_IRUSR, root,
644                                      (u32 *)&host->pending_events);
645         if (!node)
646                 goto err;
647
648         node = debugfs_create_x32("completed_events", S_IRUSR, root,
649                                      (u32 *)&host->completed_events);
650         if (!node)
651                 goto err;
652
653         return;
654
655 err:
656         dev_err(&mmc->class_dev, "failed to initialize debugfs for host\n");
657 }
658 #endif
659
660 static u32 rk29_sdmmc_prepare_command(struct mmc_command *cmd)
661 {
662         u32             cmdr = cmd->opcode;
663
664         switch(cmdr)
665         {
666             case MMC_GO_IDLE_STATE: 
667             cmdr |= (SDMMC_CMD_INIT | SDMMC_CMD_PRV_DAT_NO_WAIT);
668             break;
669
670         case MMC_STOP_TRANSMISSION:
671             cmdr |= (SDMMC_CMD_STOP | SDMMC_CMD_PRV_DAT_NO_WAIT);
672             break;
673         case MMC_SEND_STATUS:
674         case MMC_GO_INACTIVE_STATE:   
675             cmdr |= SDMMC_CMD_PRV_DAT_NO_WAIT;
676             break;
677
678         default:
679             cmdr |= SDMMC_CMD_PRV_DAT_WAIT;
680             break;
681         }
682
683     /* response type */
684         switch(mmc_resp_type(cmd))
685         {
686             case MMC_RSP_R1:
687         case MMC_RSP_R1B:
688             // case MMC_RSP_R5:  //R5,R6,R7 is same with the R1
689             //case MMC_RSP_R6:
690             // case R6m_TYPE:
691             // case MMC_RSP_R7:
692             cmdr |= (SDMMC_CMD_RESP_CRC | SDMMC_CMD_RESP_SHORT | SDMMC_CMD_RESP_EXP);
693             break;
694         case MMC_RSP_R3:
695             //case MMC_RSP_R4:
696             /* these response not contain crc7, so don't care crc error and response error */
697             cmdr |= (SDMMC_CMD_RESP_NO_CRC | SDMMC_CMD_RESP_SHORT | SDMMC_CMD_RESP_EXP); 
698             break;
699         case MMC_RSP_R2:
700             cmdr |= (SDMMC_CMD_RESP_CRC | SDMMC_CMD_RESP_LONG | SDMMC_CMD_RESP_EXP);
701             break;
702         case MMC_RSP_NONE:
703             cmdr |= (SDMMC_CMD_RESP_CRC_NOCARE | SDMMC_CMD_RESP_NOCARE | SDMMC_CMD_RESP_NO_EXP);  
704             break;
705         default:
706             cmdr |= (SDMMC_CMD_RESP_CRC_NOCARE | SDMMC_CMD_RESP_NOCARE | SDMMC_CMD_RESP_NO_EXP); 
707             break;
708         }
709
710         return cmdr;
711 }
712
713 void  rk29_sdmmc_set_frq(struct rk29_sdmmc *host)
714 {
715     struct mmc_host *mmchost = platform_get_drvdata(host->pdev);
716     struct mmc_card     *card;
717     struct mmc_ios *ios;
718         unsigned int max_dtr;
719     
720     extern void mmc_set_clock(struct mmc_host *host, unsigned int hz);
721
722     if(!mmchost)
723         return;
724
725     card = (struct mmc_card     *)mmchost->card;
726     ios  = ( struct mmc_ios *)&mmchost->ios;
727
728     if(!card || !ios)
729         return;
730
731     if(MMC_POWER_ON == ios->power_mode)
732         return;
733
734     max_dtr = (unsigned int)-1;
735     
736     if (mmc_card_highspeed(card)) 
737     {
738         if (max_dtr > card->ext_csd.hs_max_dtr)
739             max_dtr = card->ext_csd.hs_max_dtr;
740             
741     }
742     else if (max_dtr > card->csd.max_dtr) 
743     {
744         if(MMC_TYPE_SD == card->type)
745         {
746                 max_dtr = (card->csd.max_dtr > SD_FPP_FREQ) ? SD_FPP_FREQ : (card->csd.max_dtr);
747             }
748             else
749             {   
750             max_dtr = (card->csd.max_dtr > MMC_FPP_FREQ) ? MMC_FPP_FREQ : (card->csd.max_dtr);
751             }       
752     }
753
754     xbwprintk(7, "%s..%d...  call mmc_set_clock() set clk=%d [%s]\n", \
755                         __FUNCTION__, __LINE__, max_dtr, host->dma_name);
756
757   
758     mmc_set_clock(mmchost, max_dtr);
759
760 }
761
762
763 static int rk29_sdmmc_start_command(struct rk29_sdmmc *host, struct mmc_command *cmd, u32 cmd_flags)
764 {
765         int tmo = RK29_SDMMC_SEND_START_TIMEOUT*10;//wait 60ms cycle.
766         
767         host->cmd = cmd;
768         host->old_cmd = cmd->opcode;
769         host->errorstep = 0;
770         host->pending_events = 0;
771         host->completed_events = 0;     
772         host->complete_done = 0;
773     host->retryfunc = 0;
774     host->cmd_status = 0;
775
776     if(MMC_STOP_TRANSMISSION != cmd->opcode)
777     {
778         host->data_status = 0;
779     }
780     
781     if(RK29_CTRL_SDMMC_ID == host->pdev->id)
782     {
783         //adjust the frequency division control of SDMMC0 every time.
784         rk29_sdmmc_set_frq(host);
785     }
786                         
787         rk29_sdmmc_write(host->regs, SDMMC_CMDARG, cmd->arg); // write to SDMMC_CMDARG register
788         rk29_sdmmc_write(host->regs, SDMMC_CMD, cmd_flags | SDMMC_CMD_START); // write to SDMMC_CMD register
789
790
791     xbwprintk(1,"\n%s..%d..************.start cmd=%d, arg=0x%x ********  [%s]\n", \
792                         __FUNCTION__, __LINE__, cmd->opcode, cmd->arg, host->dma_name);
793
794         host->mmc->doneflag = 1;        
795
796         /* wait until CIU accepts the command */
797         while (--tmo && (rk29_sdmmc_read(host->regs, SDMMC_CMD) & SDMMC_CMD_START))
798         {
799                 udelay(2);//cpu_relax();
800         }
801         
802         if(!tmo)
803         {
804             if(0==cmd->retries)
805             {
806                 printk(KERN_ERR "%s..%d..  CMD_START timeout! CMD%d(arg=0x%x, retries=%d) [%s]\n",\
807                                 __FUNCTION__,__LINE__, cmd->opcode, cmd->arg, cmd->retries,host->dma_name);
808                 }
809
810                 cmd->error = -ETIMEDOUT;
811                 host->mrq->cmd->error = -ETIMEDOUT;
812                 del_timer_sync(&host->request_timer);
813                 
814                 host->errorstep = 0x1;
815                 return SDM_WAIT_FOR_CMDSTART_TIMEOUT;
816         }
817     host->errorstep = 0xfe;
818  
819         return SDM_SUCCESS;
820 }
821
822 static int rk29_sdmmc_reset_fifo(struct rk29_sdmmc *host)
823 {
824     u32     value;
825         int     timeout;
826         int     ret = SDM_SUCCESS;
827         
828     value = rk29_sdmmc_read(host->regs, SDMMC_STATUS);
829     if (!(value & SDMMC_STAUTS_FIFO_EMPTY))
830     {
831         value = rk29_sdmmc_read(host->regs, SDMMC_CTRL);
832         value |= SDMMC_CTRL_FIFO_RESET;
833         rk29_sdmmc_write(host->regs, SDMMC_CTRL, value);
834
835         timeout = 1000;
836         while (((value = rk29_sdmmc_read(host->regs, SDMMC_CTRL)) & (SDMMC_CTRL_FIFO_RESET)) && (timeout > 0))
837         {
838             udelay(1);
839             timeout--;
840         }
841         if (timeout == 0)
842         {
843             host->errorstep = 0x2;
844             ret = SDM_WAIT_FOR_FIFORESET_TIMEOUT;
845         }
846     }
847     
848         return ret;
849         
850 }
851
852 static int rk29_sdmmc_wait_unbusy(struct rk29_sdmmc *host)
853 {
854     int time_out = 500000;//250000; //max is 250ms; //adapt the value to the sick card.  modify at 2011-10-08
855
856 #if SDMMC_USE_INT_UNBUSY
857     if((24==host->cmd->opcode)||(25==host->cmd->opcode))
858         return SDM_SUCCESS;
859 #endif  
860         while (rk29_sdmmc_read(host->regs, SDMMC_STATUS) & (SDMMC_STAUTS_DATA_BUSY|SDMMC_STAUTS_MC_BUSY)) 
861         {
862                 udelay(1);
863                 time_out--;
864
865                 if(time_out == 0)
866                 {
867                     host->errorstep = 0x3;
868                     return SDM_BUSY_TIMEOUT;
869                 }
870         }
871
872         return SDM_SUCCESS;
873 }
874
875 static void rk29_sdmmc_dma_cleanup(struct rk29_sdmmc *host)
876 {
877         if (host->data) 
878         {
879                 dma_unmap_sg(&host->pdev->dev, host->data->sg, host->data->sg_len,
880                      ((host->data->flags & MMC_DATA_WRITE)
881                       ? DMA_TO_DEVICE : DMA_FROM_DEVICE));              
882     }
883 }
884
885 static void rk29_sdmmc_stop_dma(struct rk29_sdmmc *host)
886 {
887     int ret = 0;
888     
889         if(host->use_dma == 0)
890                 return;
891                 
892         if (host->dma_info.chn> 0) 
893         {
894                 rk29_sdmmc_dma_cleanup(host); 
895                 
896                 ret = rk29_dma_ctrl(host->dma_info.chn,RK29_DMAOP_STOP);
897                 if(ret < 0)
898                 {
899             printk(KERN_ERR "%s..%d...rk29_dma_ctrl STOP error! [%s]\n", __FUNCTION__, __LINE__, host->dma_name);
900             host->errorstep = 0x95;
901             return;
902                 }
903
904                 ret = rk29_dma_ctrl(host->dma_info.chn,RK29_DMAOP_FLUSH);
905                 if(ret < 0)
906                 {
907             printk(KERN_ERR "%s..%d...rk29_dma_ctrl FLUSH error! [%s]\n", __FUNCTION__, __LINE__, host->dma_name);
908             host->errorstep = 0x96;
909             return;
910                 }
911                 
912         } 
913         else 
914         {
915                 /* Data transfer was stopped by the interrupt handler */
916                 rk29_sdmmc_set_pending(host, EVENT_DATA_COMPLETE);
917         }
918 }
919
920 static void rk29_sdmmc_control_host_dma(struct rk29_sdmmc *host, bool enable)
921 {
922     u32 value = rk29_sdmmc_read(host->regs, SDMMC_CTRL);
923
924     if (enable)
925     {
926         value |= SDMMC_CTRL_DMA_ENABLE;
927     }
928     else
929     {
930         value &= ~(SDMMC_CTRL_DMA_ENABLE);
931     }
932
933     rk29_sdmmc_write(host->regs, SDMMC_CTRL, value);
934 }
935
936 static void send_stop_cmd(struct rk29_sdmmc *host)
937 {
938     int ret;
939
940     if(host->mrq->cmd->error)
941     {
942         //stop DMA
943         if(host->dodma)
944         {
945             rk29_sdmmc_stop_dma(host);
946             rk29_sdmmc_control_host_dma(host, FALSE);
947
948             host->dodma = 0;
949         }
950         
951         ret= rk29_sdmmc_clear_fifo(host);
952         if(SDM_SUCCESS != ret)
953         {
954             xbwprintk(3, "%s..%d..  clear fifo error before call CMD_STOP [%s]\n", \
955                                                         __FUNCTION__, __LINE__, host->dma_name);
956         }
957     }
958     
959     mod_timer(&host->request_timer, jiffies + msecs_to_jiffies(RK29_SDMMC_SEND_START_TIMEOUT+1500));
960                 
961     host->stopcmd.opcode = MMC_STOP_TRANSMISSION;
962     host->stopcmd.flags  = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;;
963     host->stopcmd.arg = 0;
964     host->stopcmd.data = NULL;
965     host->stopcmd.mrq = NULL;
966     host->stopcmd.retries = 0;
967     host->stopcmd.error = 0;
968     if(host->mrq && host->mrq->stop)
969     {
970         host->mrq->stop->error = 0;
971     }
972     
973     host->cmdr = rk29_sdmmc_prepare_command(&host->stopcmd);
974     
975     ret = rk29_sdmmc_start_command(host, &host->stopcmd, host->cmdr); 
976     if(SDM_SUCCESS != ret)
977     {
978         rk29_sdmmc_start_error(host);
979
980         host->state = STATE_IDLE;
981         host->complete_done = 4;
982     }
983 }
984
985
986 /* This function is called by the DMA driver from tasklet context. */
987 static void rk29_sdmmc_dma_complete(void *arg, int size, enum rk29_dma_buffresult result) 
988 {
989         struct rk29_sdmmc       *host = arg;
990
991         if(host->use_dma == 0)
992                 return;
993
994         host->intInfo.transLen = host->intInfo.desLen;  
995 }
996
997 static int rk29_sdmmc_submit_data_dma(struct rk29_sdmmc *host, struct mmc_data *data)
998 {
999         struct scatterlist              *sg;
1000         unsigned int                    i,direction, sgDirection;
1001         int ret, dma_len=0;
1002         
1003         if(host->use_dma == 0)
1004         {
1005             printk(KERN_ERR "%s..%d...setup DMA fail!!!!!!. host->use_dma=0 [%s]\n", __FUNCTION__, __LINE__, host->dma_name);
1006             host->errorstep = 0x4;
1007                 return -ENOSYS;
1008         }
1009         /* If we don't have a channel, we can't do DMA */
1010         if (host->dma_info.chn < 0)
1011         {
1012             printk(KERN_ERR "%s..%d...setup DMA fail!!!!!!!. dma_info.chn < 0  [%s]\n", __FUNCTION__, __LINE__, host->dma_name);
1013             host->errorstep = 0x5;
1014                 return -ENODEV;
1015         }
1016
1017         if (data->blksz & 3)
1018                 return -EINVAL;
1019         for_each_sg(data->sg, sg, data->sg_len, i) 
1020         {
1021                 if (sg->offset & 3 || sg->length & 3)
1022                 {
1023                     printk(KERN_ERR "%s..%d...call for_each_sg() fail !![%s]\n", __FUNCTION__, __LINE__, host->dma_name);
1024                     host->errorstep = 0x7;
1025                         return -EINVAL;
1026                 }
1027         }
1028         if (data->flags & MMC_DATA_READ)
1029         {
1030                 direction = RK29_DMASRC_HW;  
1031                 sgDirection = DMA_FROM_DEVICE; 
1032         }
1033         else
1034         {
1035                 direction = RK29_DMASRC_MEM;
1036                 sgDirection = DMA_TO_DEVICE;
1037         }
1038
1039         ret = rk29_dma_ctrl(host->dma_info.chn,RK29_DMAOP_STOP);
1040         if(ret < 0)
1041         {
1042             printk(KERN_ERR "%s..%d...rk29_dma_ctrl stop error![%s]\n", __FUNCTION__, __LINE__, host->dma_name);
1043             host->errorstep = 0x91;
1044                 return -ENOSYS;
1045         }
1046         
1047         ret = rk29_dma_ctrl(host->dma_info.chn,RK29_DMAOP_FLUSH);       
1048         if(ret < 0)
1049         {
1050         printk(KERN_ERR "%s..%d...rk29_dma_ctrl flush error![%s]\n", __FUNCTION__, __LINE__, host->dma_name);
1051         host->errorstep = 0x91;
1052         return -ENOSYS;
1053         }
1054
1055         
1056     ret = rk29_dma_devconfig(host->dma_info.chn, direction, (unsigned long )(host->dma_addr));
1057     if(0 != ret)
1058     {
1059         printk(KERN_ERR "%s..%d...call rk29_dma_devconfig() fail ![%s]\n", __FUNCTION__, __LINE__, host->dma_name);
1060         host->errorstep = 0x8;
1061         return -ENOSYS;
1062     }
1063     
1064         dma_len = dma_map_sg(&host->pdev->dev, data->sg, data->sg_len, sgDirection);                                                                       
1065         for (i = 0; i < dma_len; i++)
1066         {
1067         ret = rk29_dma_enqueue(host->dma_info.chn, host, sg_dma_address(&data->sg[i]),sg_dma_len(&data->sg[i]));
1068         if(ret < 0)
1069         {
1070             printk(KERN_ERR "%s..%d...call rk29_dma_devconfig() fail ![%s]\n", __FUNCTION__, __LINE__, host->dma_name);
1071             host->errorstep = 0x93;
1072             return -ENOSYS;
1073         }
1074     }
1075         
1076         rk29_sdmmc_control_host_dma(host, TRUE);// enable dma
1077         ret = rk29_dma_ctrl(host->dma_info.chn, RK29_DMAOP_START);
1078         if(ret < 0)
1079         {
1080         printk(KERN_ERR "%s..%d...rk29_dma_ctrl start error![%s]\n", __FUNCTION__, __LINE__, host->dma_name);
1081         host->errorstep = 0x94;
1082         return -ENOSYS;
1083         }
1084         
1085         return 0;
1086 }
1087
1088
1089 static int rk29_sdmmc_prepare_write_data(struct rk29_sdmmc *host, struct mmc_data *data)
1090 {
1091     int     output;
1092     u32    i = 0;
1093     u32     dataLen;
1094     u32     count, *pBuf = (u32 *)host->pbuf;
1095
1096     output = SDM_SUCCESS;
1097     dataLen = data->blocks*data->blksz;
1098     
1099     host->dodma = 0; //DMA still no request;
1100  
1101     //SDMMC controller request the data is multiple of 4.
1102     count = (dataLen >> 2) + ((dataLen & 0x3) ? 1:0);
1103
1104     if (count <= FIFO_DEPTH)
1105     {
1106         for (i=0; i<count; i++)
1107         {
1108             rk29_sdmmc_write(host->regs, SDMMC_DATA, pBuf[i]);
1109         }
1110     }
1111     else
1112     {
1113         host->intInfo.desLen = count;
1114         host->intInfo.transLen = 0;
1115         host->intInfo.pBuf = (u32 *)pBuf;
1116         
1117         if(0)//(host->intInfo.desLen <= 512 ) 
1118         {  
1119             //use pio-mode          
1120             return SDM_SUCCESS;
1121         } 
1122         else 
1123         {
1124             xbwprintk(7, "%s..%d...   trace data,   [%s]\n", __FUNCTION__, __LINE__,  host->dma_name);
1125             output = rk29_sdmmc_submit_data_dma(host, data);
1126             if(output)
1127             {
1128                         host->dodma = 0;
1129                                 
1130                     printk(KERN_ERR "%s..%d... CMD%d setupDMA failure!!!!! pre_cmd=%d  [%s]\n", \
1131                                                 __FUNCTION__, __LINE__, host->cmd->opcode,host->old_cmd, host->dma_name);
1132                     
1133                                 host->errorstep = 0x81;
1134
1135                         rk29_sdmmc_control_host_dma(host, FALSE); 
1136                 }
1137                 else
1138                 {
1139                     host->dodma = 1;
1140                 }
1141         }
1142        
1143     }
1144
1145     return output;
1146 }
1147
1148
1149
1150
1151 static int rk29_sdmmc_prepare_read_data(struct rk29_sdmmc *host, struct mmc_data *data)
1152 {
1153     u32  count = 0;
1154     u32  dataLen;
1155     int   output;
1156
1157     output = SDM_SUCCESS;
1158     dataLen = data->blocks*data->blksz;
1159     
1160     host->dodma = 0;//DMA still no request;
1161
1162     //SDMMC controller request the data is multiple of 4.
1163     count = (dataLen >> 2) ;//+ ((dataLen & 0x3) ? 1:0);
1164
1165     host->intInfo.desLen = (dataLen >> 2);
1166     host->intInfo.transLen = 0;
1167     host->intInfo.pBuf = (u32 *)host->pbuf;
1168        
1169     if (count > (RX_WMARK+1))  //datasheet error.actually, it can nont waken the interrupt when less and equal than RX_WMARK+1
1170     {
1171         if(0) //(host->intInfo.desLen <= 512 )
1172         {
1173             //use pio-mode
1174             return SDM_SUCCESS;
1175         }        
1176         else 
1177         {
1178             output = rk29_sdmmc_submit_data_dma(host, data);
1179             if(output)
1180             {
1181                         host->dodma = 0;
1182                                 
1183                     printk(KERN_ERR "%s..%d... CMD%d setupDMA  failure!!!  [%s]\n", \
1184                                                 __FUNCTION__, __LINE__, host->cmd->opcode, host->dma_name);
1185
1186                     host->errorstep = 0x82;
1187
1188                         rk29_sdmmc_control_host_dma(host, FALSE); 
1189                 }
1190                 else
1191                 {
1192                     host->dodma = 1;
1193                 }
1194         }
1195     }
1196
1197     return output;
1198 }
1199
1200
1201
1202 static int rk29_sdmmc_read_remain_data(struct rk29_sdmmc *host, u32 originalLen, void *pDataBuf)
1203 {
1204     u32  value = 0;
1205
1206     u32     i = 0;
1207     u32     *pBuf = (u32 *)pDataBuf;
1208     u8      *pByteBuf = (u8 *)pDataBuf;
1209     u32     lastData = 0;
1210
1211     //SDMMC controller must be multiple of 32. so if transfer 13, then actuall we should write or read 16 byte.
1212     u32  count = (originalLen >> 2) + ((originalLen & 0x3) ? 1:0);
1213
1214     if(1 == host->dodma)
1215     {
1216         //when use DMA, there are remain data only when datalen/4 less than  RX_WMARK+1 same as equaltion. or not multiple of 4
1217         if (!((value = rk29_sdmmc_read(host->regs, SDMMC_STATUS)) & SDMMC_STAUTS_FIFO_EMPTY))
1218         {
1219             if (count <= (RX_WMARK+1))
1220             {
1221                 i = 0;
1222                 while ((i<(originalLen >> 2))&&(!(value & SDMMC_STAUTS_FIFO_EMPTY)))
1223                 {
1224                     pBuf[i++] = rk29_sdmmc_read(host->regs, SDMMC_DATA);
1225                     value = rk29_sdmmc_read(host->regs, SDMMC_STATUS);
1226                 }
1227             }
1228
1229             if (count > (originalLen >> 2))
1230             {
1231                 lastData = rk29_sdmmc_read(host->regs, SDMMC_DATA);
1232
1233                 //fill the 1 to 3 byte.
1234                 for (i=0; i<(originalLen & 0x3); i++)
1235                 {
1236                     pByteBuf[(originalLen & 0xFFFFFFFC) + i] = (u8)((lastData >> (i << 3)) & 0xFF); //default little-endian
1237                 }
1238             }
1239         }
1240     }
1241     else
1242     {
1243         if (!((value = rk29_sdmmc_read(host->regs, SDMMC_STATUS)) & SDMMC_STAUTS_FIFO_EMPTY))
1244         {
1245              while ( (host->intInfo.transLen < host->intInfo.desLen)  && (!(value & SDMMC_STAUTS_FIFO_EMPTY)) )
1246             {
1247                 pBuf[host->intInfo.transLen++] = rk29_sdmmc_read(host->regs, SDMMC_DATA);  
1248                 value = rk29_sdmmc_read(host->regs, SDMMC_STATUS);  
1249             }
1250
1251             if (count > (originalLen >> 2))
1252             {
1253                 lastData = rk29_sdmmc_read(host->regs, SDMMC_DATA); 
1254
1255                 //fill the 1 to 3 byte.
1256                 for (i=0; i<(originalLen & 0x3); i++)
1257                 {
1258                     pByteBuf[(originalLen & 0xFFFFFFFC) + i] = (u8)((lastData >> (i << 3)) & 0xFF);  //default little-endian
1259                 }
1260             }
1261         }
1262     }
1263     
1264     return SDM_SUCCESS;
1265 }
1266
1267
1268 static void rk29_sdmmc_do_pio_read(struct rk29_sdmmc *host)
1269 {
1270     int i;
1271     for (i=0; i<(RX_WMARK+1); i++)
1272     {
1273         host->intInfo.pBuf[host->intInfo.transLen + i] = rk29_sdmmc_read(host->regs, SDMMC_DATA);
1274     }
1275     host->intInfo.transLen += (RX_WMARK+1);
1276 }
1277
1278 static void rk29_sdmmc_do_pio_write(struct rk29_sdmmc *host)
1279 {
1280     int i;
1281     if ( (host->intInfo.desLen - host->intInfo.transLen) > (FIFO_DEPTH - TX_WMARK) )
1282     {
1283         for (i=0; i<(FIFO_DEPTH - TX_WMARK); i++)
1284         {
1285             rk29_sdmmc_write(host->regs, SDMMC_DATA, host->intInfo.pBuf[host->intInfo.transLen + i]);
1286         }
1287         host->intInfo.transLen += (FIFO_DEPTH - TX_WMARK);
1288     }
1289     else
1290     {
1291         for (i=0; i<(host->intInfo.desLen - host->intInfo.transLen); i++)
1292         {
1293             rk29_sdmmc_write(host->regs, SDMMC_DATA, host->intInfo.pBuf[host->intInfo.transLen + i]);
1294         }
1295         host->intInfo.transLen =  host->intInfo.desLen;
1296     }
1297       
1298 }
1299
1300
1301 static void rk29_sdmmc_submit_data(struct rk29_sdmmc *host, struct mmc_data *data)
1302 {
1303     int ret;
1304     
1305     if(data)
1306     {
1307         host->data = data;
1308         data->error = 0;
1309         host->cmd->data = data;
1310
1311         data->bytes_xfered = 0;
1312         host->pbuf = (u32*)sg_virt(data->sg);
1313
1314         if (data->flags & MMC_DATA_STREAM)
1315                 {
1316                         host->cmdr |= SDMMC_CMD_STRM_MODE;    //set stream mode
1317                 }
1318                 else
1319                 {
1320                     host->cmdr |= SDMMC_CMD_BLOCK_MODE;   //set block mode
1321                 }
1322                 
1323         //set the blocks and blocksize
1324                 rk29_sdmmc_write(host->regs, SDMMC_BYTCNT,data->blksz*data->blocks);
1325                 rk29_sdmmc_write(host->regs, SDMMC_BLKSIZ,data->blksz);
1326
1327         xbwprintk(1, "%s..%d..CMD%d(arg=0x%x), data->blksz=%d, data->blocks=%d   [%s]\n", \
1328             __FUNCTION__, __LINE__, host->cmd->opcode,host->cmd->arg,data->blksz, data->blocks,  host->dma_name);
1329             
1330                 if (data->flags & MMC_DATA_WRITE)
1331                 {
1332                     host->cmdr |= (SDMMC_CMD_DAT_WRITE | SDMMC_CMD_DAT_EXP);
1333             xbwprintk(7, "%s..%d...   write data, len=%d     [%s]\n", \
1334                                         __FUNCTION__, __LINE__, data->blksz*data->blocks, host->dma_name);
1335                     
1336                         ret = rk29_sdmmc_prepare_write_data(host, data);
1337             }
1338             else
1339             {
1340                 host->cmdr |= (SDMMC_CMD_DAT_READ | SDMMC_CMD_DAT_EXP);
1341             xbwprintk(7, "%s..%d...   read data  len=%d   [%s]\n", \
1342                                         __FUNCTION__, __LINE__, data->blksz*data->blocks, host->dma_name);
1343                 
1344                         ret = rk29_sdmmc_prepare_read_data(host, data);
1345             }
1346
1347     }
1348     else
1349     {
1350         rk29_sdmmc_write(host->regs, SDMMC_BLKSIZ, 0);
1351         rk29_sdmmc_write(host->regs, SDMMC_BYTCNT, 0);
1352     }
1353 }
1354
1355
1356 static int sdmmc_send_cmd_start(struct rk29_sdmmc *host, unsigned int cmd)
1357 {
1358         int tmo = RK29_SDMMC_SEND_START_TIMEOUT*10;//wait 60ms cycle.
1359         
1360         rk29_sdmmc_write(host->regs, SDMMC_CMD, SDMMC_CMD_START | cmd);         
1361         while (--tmo && (rk29_sdmmc_read(host->regs, SDMMC_CMD) & SDMMC_CMD_START))
1362         {
1363             udelay(2);
1364         }
1365         
1366         if(!tmo) 
1367         {
1368                 printk(KERN_ERR "%s.. %d   set cmd(value=0x%x) register timeout error !   [%s]\n",\
1369                                 __FUNCTION__,__LINE__, cmd, host->dma_name);
1370
1371                 host->errorstep = 0x9;
1372                 return SDM_START_CMD_FAIL;
1373         }
1374
1375         return SDM_SUCCESS;
1376 }
1377
1378 static int rk29_sdmmc_get_cd(struct mmc_host *mmc)
1379 {
1380         struct rk29_sdmmc *host = mmc_priv(mmc);
1381         u32 cdetect=1;
1382
1383     switch(host->pdev->id)
1384     {
1385         case 0:
1386         {
1387             #ifdef CONFIG_PM
1388                 if(host->gpio_det == INVALID_GPIO)
1389                         return 1;
1390             #endif
1391
1392                 cdetect = rk29_sdmmc_read(host->regs, SDMMC_CDETECT);
1393
1394             cdetect = (cdetect & SDMMC_CARD_DETECT_N)?0:1;
1395
1396             break;
1397         }        
1398
1399         case 1:
1400         {
1401             #if defined(CONFIG_USE_SDMMC1_FOR_WIFI_DEVELOP_BOARD)
1402             cdetect = 1;
1403             #else
1404             cdetect = test_bit(RK29_SDMMC_CARD_PRESENT, &host->flags)?1:0;
1405             #endif
1406             break;
1407         }
1408         
1409         default:
1410             cdetect = 1;
1411             break;
1412     
1413         }
1414
1415          return cdetect;
1416 }
1417
1418
1419 /****************************************************************/
1420 //reset the SDMMC controller of the current host
1421 /****************************************************************/
1422 int rk29_sdmmc_reset_controller(struct rk29_sdmmc *host)
1423 {
1424     u32  value = 0;
1425     int  timeOut = 0;
1426
1427     rk29_sdmmc_write(host->regs, SDMMC_PWREN, POWER_ENABLE);
1428
1429     /* reset SDMMC IP */
1430     //SDPAM_SDCClkEnable(host, TRUE);
1431
1432     //Clean the fifo.
1433     for(timeOut=0; timeOut<FIFO_DEPTH; timeOut++)
1434     {
1435         if(rk29_sdmmc_read(host->regs, SDMMC_STATUS) & SDMMC_STAUTS_FIFO_EMPTY)
1436             break;
1437             
1438         value = rk29_sdmmc_read(host->regs, SDMMC_DATA);
1439     }
1440    
1441     /* reset */
1442 #if defined(CONFIG_ARCH_RK29)     
1443     rk29_sdmmc_write(host->regs, SDMMC_CTRL,(SDMMC_CTRL_RESET | SDMMC_CTRL_FIFO_RESET ));
1444 #elif defined(CONFIG_ARCH_RK30)
1445     rk29_sdmmc_write(host->regs, SDMMC_CTRL,(SDMMC_CTRL_RESET | SDMMC_CTRL_FIFO_RESET | SDMMC_CTRL_DMA_RESET));
1446 #endif
1447     timeOut = 1000;
1448     value = rk29_sdmmc_read(host->regs, SDMMC_CTRL);
1449     while (( value & (SDMMC_CTRL_FIFO_RESET | SDMMC_CTRL_RESET)) && (timeOut > 0))
1450     {
1451         udelay(1);
1452         timeOut--;
1453         value = rk29_sdmmc_read(host->regs, SDMMC_CTRL);
1454     }
1455
1456     if (timeOut == 0)
1457     {
1458         printk(KERN_ERR "%s..%s..%d..  reset controller fail! [%s]=\n",\
1459                                 __FILE__, __FUNCTION__,__LINE__, host->dma_name);
1460
1461         host->errorstep = 0x0A;
1462         return SDM_WAIT_FOR_FIFORESET_TIMEOUT;
1463     }
1464
1465      /* FIFO threshold settings  */
1466         rk29_sdmmc_write(host->regs, SDMMC_FIFOTH, (SD_MSIZE_16 | (RX_WMARK << RX_WMARK_SHIFT) | (TX_WMARK << TX_WMARK_SHIFT)));
1467         
1468     rk29_sdmmc_write(host->regs, SDMMC_CTYPE, SDMMC_CTYPE_1BIT);
1469     rk29_sdmmc_write(host->regs, SDMMC_CLKSRC, CLK_DIV_SRC_0);
1470     /* config debounce */
1471     host->bus_hz = clk_get_rate(host->clk);
1472     if((host->bus_hz > 52000000) || (host->bus_hz <= 0))
1473     {
1474         printk(KERN_ERR "%s..%s..%d..****Error!!!!!!  Bus clock %d hz is beyond the prescribed limits. [%s]\n",\
1475             __FILE__, __FUNCTION__,__LINE__,host->bus_hz, host->dma_name);
1476         
1477                 host->errorstep = 0x0B;            
1478         return SDM_PARAM_ERROR;            
1479     }
1480
1481     rk29_sdmmc_write(host->regs, SDMMC_DEBNCE, (DEBOUNCE_TIME*host->bus_hz)&0xFFFFFF);
1482
1483     /* config interrupt */
1484     rk29_sdmmc_write(host->regs, SDMMC_RINTSTS, 0xFFFFFFFF);
1485
1486     if(host->use_dma)
1487     {
1488         if(RK29_CTRL_SDMMC_ID == host->pdev->id)
1489         {
1490                     rk29_sdmmc_write(host->regs, SDMMC_INTMASK,RK29_SDMMC_INTMASK_USEDMA);
1491                 }
1492                 else
1493                 {
1494                     if(0== host->pdev->id)
1495                     {
1496                     #if !defined(CONFIG_USE_SDMMC0_FOR_WIFI_DEVELOP_BOARD)
1497                     rk29_sdmmc_write(host->regs, SDMMC_INTMASK,RK29_SDMMC_INTMASK_USEDMA | SDMMC_INT_SDIO);
1498                     #else
1499                     rk29_sdmmc_write(host->regs, SDMMC_INTMASK,RK29_SDMMC_INTMASK_USEDMA);
1500                     #endif
1501                     }
1502                     else if(1== host->pdev->id)
1503                     {
1504                        #if !defined(CONFIG_USE_SDMMC1_FOR_WIFI_DEVELOP_BOARD)
1505                     rk29_sdmmc_write(host->regs, SDMMC_INTMASK,RK29_SDMMC_INTMASK_USEDMA | SDMMC_INT_SDIO);
1506                    #else
1507                     rk29_sdmmc_write(host->regs, SDMMC_INTMASK,RK29_SDMMC_INTMASK_USEDMA);
1508                    #endif 
1509                     }
1510                     else
1511                     {
1512                         rk29_sdmmc_write(host->regs, SDMMC_INTMASK,RK29_SDMMC_INTMASK_USEDMA | SDMMC_INT_SDIO);
1513                     }
1514                 }
1515         }
1516         else
1517         {
1518                 if(RK29_CTRL_SDMMC_ID == host->pdev->id)
1519         {
1520                     rk29_sdmmc_write(host->regs, SDMMC_INTMASK,RK29_SDMMC_INTMASK_USEIO);
1521                 }
1522                 else
1523                 {
1524                     if(0== host->pdev->id)
1525                     {
1526                     #if !defined(CONFIG_USE_SDMMC0_FOR_WIFI_DEVELOP_BOARD)
1527                     rk29_sdmmc_write(host->regs, SDMMC_INTMASK,RK29_SDMMC_INTMASK_USEIO | SDMMC_INT_SDIO);
1528                     #else
1529                     rk29_sdmmc_write(host->regs, SDMMC_INTMASK,RK29_SDMMC_INTMASK_USEIO);
1530                     #endif
1531                     }
1532                     else if(1== host->pdev->id)
1533                     {
1534                         #if !defined(CONFIG_USE_SDMMC1_FOR_WIFI_DEVELOP_BOARD)
1535                     rk29_sdmmc_write(host->regs, SDMMC_INTMASK,RK29_SDMMC_INTMASK_USEIO | SDMMC_INT_SDIO);
1536                     #else
1537                     rk29_sdmmc_write(host->regs, SDMMC_INTMASK,RK29_SDMMC_INTMASK_USEIO);
1538                     #endif
1539                     }
1540                     else
1541                     {
1542                         rk29_sdmmc_write(host->regs, SDMMC_INTMASK,RK29_SDMMC_INTMASK_USEDMA | SDMMC_INT_SDIO);
1543                     }
1544                 }               
1545     }
1546
1547         rk29_sdmmc_write(host->regs, SDMMC_PWREN, POWER_ENABLE);
1548         
1549         rk29_sdmmc_write(host->regs, SDMMC_CTRL,SDMMC_CTRL_INT_ENABLE); // enable mci interrupt
1550
1551     return SDM_SUCCESS;
1552 }
1553
1554
1555
1556
1557 //enable/disnable the clk.
1558 static int rk29_sdmmc_control_clock(struct rk29_sdmmc *host, bool enable)
1559 {
1560     u32           value = 0;
1561     int           tmo = 0;
1562     int           ret = SDM_SUCCESS;
1563
1564     //wait previous start to clear
1565     tmo = 1000;
1566         while (--tmo && (rk29_sdmmc_read(host->regs, SDMMC_CMD) & SDMMC_CMD_START))
1567         {
1568                 udelay(1);//cpu_relax();
1569         }
1570         if(!tmo)
1571         {
1572             host->errorstep = 0x0C;
1573             ret = SDM_START_CMD_FAIL;
1574                 goto Error_exit;        
1575     }
1576
1577     if(RK29_CTRL_SDMMC_ID == host->pdev->id)
1578     { 
1579         //SDMMC use low-power mode
1580         #if SDMMC_CLOCK_TEST
1581         if (enable)
1582         {
1583             value = (SDMMC_CLKEN_ENABLE);
1584         }
1585         else
1586         {
1587             value = (SDMMC_CLKEN_DISABLE);
1588         }
1589         
1590         #else
1591         {
1592             if (enable)
1593             {
1594                 value = (SDMMC_CLKEN_LOW_PWR | SDMMC_CLKEN_ENABLE);
1595             }
1596             else
1597             {
1598                 value = (SDMMC_CLKEN_LOW_PWR | SDMMC_CLKEN_DISABLE);
1599             }
1600         }
1601         #endif
1602     }
1603     else
1604     {
1605         //SDIO-card use non-low-power mode
1606         if (enable)
1607         {
1608             value = (SDMMC_CLKEN_ENABLE);
1609         }
1610         else
1611         {
1612             value = (SDMMC_CLKEN_DISABLE);
1613         }
1614     }
1615   
1616     rk29_sdmmc_write(host->regs, SDMMC_CLKENA, value);
1617
1618         /* inform CIU */
1619         ret = sdmmc_send_cmd_start(host, SDMMC_CMD_UPD_CLK | SDMMC_CMD_PRV_DAT_WAIT);
1620     if(ret != SDM_SUCCESS)
1621     {
1622         goto Error_exit;
1623     }
1624
1625     return SDM_SUCCESS;
1626
1627 Error_exit:
1628     printk(KERN_ERR "\n%s....%d..  control clock fail!!! Enable=%d, ret=0x%x [%s]\n",\
1629                         __FILE__,__LINE__,enable,ret, host->dma_name);
1630
1631     return ret;
1632     
1633 }
1634
1635
1636 //adjust the frequency.ie, to set the frequency division control
1637 int rk29_sdmmc_change_clk_div(struct rk29_sdmmc *host, u32 freqHz)
1638 {
1639     u32 div;
1640     u32 tmo;
1641     int ret = SDM_SUCCESS;
1642
1643     if(0 == freqHz)
1644     {
1645         ret =  SDM_PARAM_ERROR;
1646         goto  SetFreq_error;
1647     }
1648
1649     ret = rk29_sdmmc_control_clock(host, FALSE);
1650     if (ret != SDM_SUCCESS)
1651     {
1652         goto SetFreq_error;
1653     }
1654
1655      
1656     host->bus_hz = clk_get_rate(host->clk);
1657     if((host->bus_hz > 52000000) || (host->bus_hz <= 0))
1658     {
1659         printk(KERN_ERR "%s..%s..%d..****Error!!!!!!  Bus clock %d hz is beyond the prescribed limits [%s]\n",\
1660             __FILE__, __FUNCTION__,__LINE__,host->bus_hz, host->dma_name);
1661             
1662         host->errorstep = 0x0D;    
1663         ret = SDM_PARAM_ERROR;   
1664         goto SetFreq_error;
1665     }
1666
1667     //calculate the divider
1668     div = host->bus_hz/freqHz + ((( host->bus_hz%freqHz ) > 0) ? 1:0 );
1669     if( (div & 0x01) && (1 != div) )
1670     {
1671         //It is sure that the value of div is even. 
1672         ++div;
1673     }
1674
1675     if(div > 1)
1676     {
1677         host->clock = host->bus_hz/div;
1678     }
1679     else
1680     {
1681         host->clock = host->bus_hz;
1682     }
1683     div = (div >> 1);
1684
1685     //wait previous start to clear
1686     tmo = 1000;
1687         while (--tmo && (rk29_sdmmc_read(host->regs, SDMMC_CMD) & SDMMC_CMD_START))
1688         {
1689                 udelay(1);//cpu_relax();
1690         }
1691         if(!tmo)
1692         {
1693             host->errorstep = 0x0E; 
1694             ret = SDM_START_CMD_FAIL;
1695                 goto SetFreq_error;
1696     }
1697            
1698     /* set clock to desired speed */
1699     rk29_sdmmc_write(host->regs, SDMMC_CLKDIV, div);
1700
1701     /* inform CIU */
1702     ret = sdmmc_send_cmd_start(host, SDMMC_CMD_UPD_CLK | SDMMC_CMD_PRV_DAT_WAIT);
1703     if(ret != SDM_SUCCESS)
1704     {
1705         host->errorstep = 0x0E1; 
1706         goto SetFreq_error;
1707     }
1708     
1709     if(host->old_div != div)
1710     {
1711         printk(KERN_INFO "%s..%d..  newDiv=%u, newCLK=%uKhz [%s]\n", \
1712             __FUNCTION__, __LINE__,div, host->clock/1000, host->dma_name);
1713     }
1714
1715     ret = rk29_sdmmc_control_clock(host, TRUE);
1716     if(ret != SDM_SUCCESS)
1717     {
1718         goto SetFreq_error;
1719     }
1720     host->old_div = div;
1721
1722     return SDM_SUCCESS;
1723     
1724 SetFreq_error:
1725
1726     printk(KERN_ERR "%s..%d..  change division fail, errorStep=0x%x,ret=%d  [%s]\n",\
1727         __FILE__, __LINE__,host->errorstep,ret, host->dma_name);
1728         
1729     return ret;
1730     
1731 }
1732
1733 int rk29_sdmmc_hw_init(void *data)
1734 {
1735     struct rk29_sdmmc *host = (struct rk29_sdmmc *)data;
1736
1737     //set the iomux
1738     host->ctype = SDMMC_CTYPE_1BIT;
1739     host->set_iomux(host->pdev->id, host->ctype);
1740     
1741     /* reset controller */
1742     rk29_sdmmc_reset_controller(host);
1743
1744     rk29_sdmmc_change_clk_div(host, FOD_FREQ);
1745     
1746     return SDM_SUCCESS;    
1747 }
1748
1749
1750
1751 int rk29_sdmmc_set_buswidth(struct rk29_sdmmc *host)
1752 {
1753     //int ret;
1754     switch (host->ctype)
1755     {
1756         case SDMMC_CTYPE_1BIT:
1757         case SDMMC_CTYPE_4BIT:
1758             break;
1759         case SDMMC_CTYPE_8BIT:
1760             return SDM_PARAM_ERROR; //Now, not support 8 bit width
1761         default:
1762             return SDM_PARAM_ERROR;
1763     }
1764
1765     host->set_iomux(host->pdev->id, host->ctype);
1766
1767     /* Set the current  bus width */
1768         rk29_sdmmc_write(host->regs, SDMMC_CTYPE, host->ctype);
1769
1770     return SDM_SUCCESS;
1771 }
1772
1773
1774 static void rk29_sdmmc_dealwith_timeout(struct rk29_sdmmc *host)
1775
1776     if(0 == host->mmc->doneflag)
1777         return; //not to generate error flag if the command has been over.
1778         
1779     switch(host->state)
1780     {
1781         case STATE_IDLE:
1782             break;          
1783         case STATE_SENDING_CMD:
1784             host->cmd_status |= SDMMC_INT_RTO;
1785             host->cmd->error = -ETIME;
1786             rk29_sdmmc_write(host->regs, SDMMC_RINTSTS,(SDMMC_INT_CMD_DONE | SDMMC_INT_RTO));  //  clear interrupt
1787             rk29_sdmmc_set_pending(host, EVENT_CMD_COMPLETE);
1788             tasklet_schedule(&host->tasklet);
1789             break;
1790          case STATE_DATA_BUSY:
1791             host->data_status |= (SDMMC_INT_DCRC|SDMMC_INT_EBE);
1792             host->cmd->data->error = -EILSEQ;
1793             rk29_sdmmc_write(host->regs, SDMMC_RINTSTS,SDMMC_INT_DTO);  // clear interrupt
1794             rk29_sdmmc_set_pending(host, EVENT_DATA_COMPLETE);
1795             tasklet_schedule(&host->tasklet);
1796             break;
1797 #if SDMMC_USE_INT_UNBUSY
1798          case STATE_DATA_UNBUSY:
1799             host->data_status |= (SDMMC_INT_DCRC|SDMMC_INT_EBE);
1800             host->cmd->data->error = -EILSEQ;
1801             rk29_sdmmc_write(host->regs, SDMMC_RINTSTS,SDMMC_INT_UNBUSY);  // clear interrupt
1802             rk29_sdmmc_set_pending(host, EVENT_DATA_UNBUSY);
1803             tasklet_schedule(&host->tasklet);
1804             break;
1805 #endif              
1806          case STATE_SENDING_STOP: 
1807             host->cmd_status |= SDMMC_INT_RTO;
1808             host->cmd->error = -ETIME;
1809             rk29_sdmmc_write(host->regs, SDMMC_RINTSTS,(SDMMC_INT_CMD_DONE | SDMMC_INT_RTO));  //  clear interrupt
1810             rk29_sdmmc_set_pending(host, EVENT_CMD_COMPLETE);
1811             tasklet_schedule(&host->tasklet);
1812             break;
1813         case STATE_DATA_END:
1814             break;
1815         default:
1816             break; 
1817     }
1818 }
1819
1820
1821 static void rk29_sdmmc_INT_CMD_DONE_timeout(unsigned long host_data)
1822 {
1823         struct rk29_sdmmc *host = (struct rk29_sdmmc *) host_data;
1824
1825         spin_lock(&host->lock);
1826         
1827         if(STATE_SENDING_CMD == host->state)
1828         {
1829             if(0==host->cmd->retries)
1830             {
1831             printk(KERN_ERR "%s..%d... cmd=%d, INT_CMD_DONE timeout, errorStep=0x%x, host->state=%x [%s]\n",\
1832                 __FUNCTION__, __LINE__,host->cmd->opcode, host->errorstep,host->state,host->dma_name);
1833         }
1834             
1835         rk29_sdmmc_dealwith_timeout(host);        
1836         }
1837         spin_unlock(&host->lock);
1838         
1839 }
1840
1841
1842 static void rk29_sdmmc_INT_DTO_timeout(unsigned long host_data)
1843 {
1844         struct rk29_sdmmc *host = (struct rk29_sdmmc *) host_data;
1845
1846         spin_lock(&host->lock);
1847
1848 #if SDMMC_USE_INT_UNBUSY
1849     if( (host->cmdr & SDMMC_CMD_DAT_EXP) &&((STATE_DATA_BUSY == host->state)||(STATE_DATA_UNBUSY == host->state) ))
1850 #else
1851     if( (host->cmdr & SDMMC_CMD_DAT_EXP) && (STATE_DATA_BUSY == host->state))
1852 #endif  
1853         {
1854             if(0==host->cmd->retries)
1855             {
1856            printk(KERN_ERR "%s..%d...cmd=%d DTO_timeout,cmdr=0x%x, errorStep=0x%x, Hoststate=%x [%s]\n", \
1857                 __FUNCTION__, __LINE__,host->cmd->opcode,host->cmdr ,host->errorstep,host->state,host->dma_name);
1858             }
1859
1860             rk29_sdmmc_dealwith_timeout(host);  
1861         }
1862         spin_unlock(&host->lock);
1863  
1864  
1865 }
1866
1867
1868 //to excute a  request 
1869 static int rk29_sdmmc_start_request(struct mmc_host *mmc )
1870 {
1871     struct rk29_sdmmc *host = mmc_priv(mmc);
1872         struct mmc_request      *mrq;
1873         struct mmc_command      *cmd;
1874         
1875         u32             cmdr, ret;
1876         unsigned long iflags;
1877
1878         spin_lock_irqsave(&host->lock, iflags);
1879         
1880         mrq = host->new_mrq;
1881         cmd = mrq->cmd;
1882         cmd->error = 0;
1883         
1884         cmdr = rk29_sdmmc_prepare_command(cmd);
1885         ret = SDM_SUCCESS;
1886         
1887
1888         /*clean FIFO if it is a new request*/
1889     if((RK29_CTRL_SDMMC_ID == host->pdev->id) && ( !(cmdr & SDMMC_CMD_STOP)))
1890     {
1891         ret = rk29_sdmmc_reset_fifo(host);
1892         if(SDM_SUCCESS != ret)
1893         {
1894                         host->mrq = host->new_mrq;///
1895             cmd->error = -ENOMEDIUM;
1896             host->errorstep = 0x0F; 
1897             ret = SDM_FALSE;
1898             goto start_request_Err; 
1899         }
1900     }
1901
1902     //check data-busy if the current command has the bit13 in command register.
1903     if( cmdr & SDMMC_CMD_PRV_DAT_WAIT )
1904     {
1905         if(rk29_sdmmc_read(host->regs, SDMMC_STATUS) & SDMMC_STAUTS_DATA_BUSY)
1906         {
1907                 host->mrq = host->new_mrq;///
1908             cmd->error = -ETIMEDOUT;
1909             ret = SDM_BUSY_TIMEOUT;
1910             host->errorstep = 0x10;
1911             if(0 == cmd->retries)
1912             {
1913                 printk(KERN_ERR "%s..Error happen in CMD_PRV_DAT_WAIT. STATUS-reg=0x%x [%s]\n", \
1914                     __FUNCTION__, rk29_sdmmc_read(host->regs, SDMMC_STATUS),host->dma_name);
1915             }
1916             rk29_sdmmc_clear_fifo(host);
1917             goto start_request_Err; 
1918         }
1919     }
1920     
1921     host->state = STATE_SENDING_CMD;
1922     host->mrq = host->new_mrq;
1923         mrq = host->mrq;
1924         cmd = mrq->cmd;
1925         cmd->error = 0;
1926         cmd->data = NULL;
1927
1928     host->cmdr = cmdr;
1929     host->cmd = cmd;
1930         host->data_status = 0;
1931         host->data = NULL;
1932         
1933         host->errorstep = 0;
1934         host->dodma = 0;
1935
1936
1937
1938     //setting for the data
1939         rk29_sdmmc_submit_data(host, mrq->data);
1940     host->errorstep = 0xff;
1941
1942         xbwprintk(7, "%s..%d...    CMD%d  begin to call rk29_sdmmc_start_command(). [%s]\n", \
1943                         __FUNCTION__, __LINE__ , cmd->opcode,host->dma_name);
1944
1945         if(RK29_CTRL_SDMMC_ID == host->pdev->id)
1946         {
1947             mod_timer(&host->request_timer, jiffies + msecs_to_jiffies(RK29_SDMMC_SEND_START_TIMEOUT+700));
1948         }
1949         else
1950         {
1951             mod_timer(&host->request_timer, jiffies + msecs_to_jiffies(RK29_SDMMC_SEND_START_TIMEOUT+500));
1952         }
1953
1954     
1955         ret = rk29_sdmmc_start_command(host, cmd, host->cmdr);
1956         if(SDM_SUCCESS != ret)
1957         {
1958         cmd->error = -ETIMEDOUT;
1959         if(0==cmd->retries)
1960         {
1961             printk(KERN_ERR "%s..%d...   start_command(CMD%d, arg=%x, retries=%d)  fail! ret=%d . [%s]\n",\
1962                 __FUNCTION__, __LINE__ , cmd->opcode,cmd->arg, cmd->retries,ret, host->dma_name);
1963         }
1964         host->errorstep = 0x11; 
1965         del_timer_sync(&host->request_timer);
1966         
1967         goto start_request_Err; 
1968         }
1969         host->errorstep = 0xfd;
1970
1971     xbwprintk(7, "%s..%d...  CMD=%d, wait for INT_CMD_DONE, ret=%d , \n  \
1972         host->state=0x%x, cmdINT=0x%x \n    host->pendingEvent=0x%lu, host->completeEvents=0x%lu [%s]\n\n",\
1973         __FUNCTION__, __LINE__, host->cmd->opcode,ret, \
1974         host->state,host->cmd_status, host->pending_events,host->completed_events,host->dma_name);
1975
1976     spin_unlock_irqrestore(&host->lock, iflags);
1977     
1978         return SDM_SUCCESS;
1979         
1980 start_request_Err:
1981     rk29_sdmmc_start_error(host);
1982
1983     if(0 == cmd->retries) 
1984     {
1985         printk(KERN_ERR "%s: CMD%d(arg=%x)  fail to start request.  err=%d, Errorstep=0x%x [%s]\n",\
1986             __FUNCTION__,  cmd->opcode, cmd->arg,ret,host->errorstep,host->dma_name);
1987     }
1988
1989     host->state = STATE_IDLE;  //modifyed by xbw  at 2011-08-15
1990     
1991     if(host->mrq && host->mmc->doneflag)
1992     {
1993         host->mmc->doneflag = 0;
1994         spin_unlock_irqrestore(&host->lock, iflags);
1995         
1996         mmc_request_done(host->mmc, host->mrq);
1997     }
1998     else
1999     {
2000         spin_unlock_irqrestore(&host->lock, iflags);        
2001     }
2002     
2003     return ret; 
2004         
2005 }
2006
2007  
2008 static void rk29_sdmmc_request(struct mmc_host *mmc, struct mmc_request *mrq)
2009 {
2010     unsigned long iflags;
2011         struct rk29_sdmmc *host = mmc_priv(mmc); 
2012         
2013     spin_lock_irqsave(&host->lock, iflags);
2014     
2015         #if 0
2016         //set 1 to close the controller for Debug.
2017         if(RK29_CTRL_SDIO1_ID==host->pdev->id)//if(RK29_CTRL_SDMMC_ID==host->pdev->id)//
2018         {
2019             mrq->cmd->error = -ENOMEDIUM;
2020             printk(KERN_ERR "%s..%d..  ==== The %s had been closed by myself for the experiment. [%s]\n",\
2021                                 __FUNCTION__, __LINE__, host->dma_name, host->dma_name);
2022
2023         host->state = STATE_IDLE;
2024         rk29_sdmmc_write(host->regs, SDMMC_RINTSTS, 0xFFFFFFFF);
2025         spin_unlock_irqrestore(&host->lock, iflags);
2026             mmc_request_done(mmc, mrq);
2027                 return;
2028         }
2029         #endif
2030
2031     xbwprintk(6, "\n%s..%d..New cmd=%2d(arg=0x%8x)=== cardPresent=0x%lu, state=0x%x [%s]\n", \
2032         __FUNCTION__, __LINE__,mrq->cmd->opcode, mrq->cmd->arg,host->flags,host->state, host->dma_name);
2033
2034     if(RK29_CTRL_SDMMC_ID == host->pdev->id)
2035     {
2036         if(!rk29_sdmmc_get_cd(mmc) || ((0==mmc->re_initialized_flags)&&(MMC_GO_IDLE_STATE != mrq->cmd->opcode)))
2037         {
2038                 mrq->cmd->error = -ENOMEDIUM;
2039
2040                 if((RK29_CTRL_SDMMC_ID == host->pdev->id)&&(0==mrq->cmd->retries))
2041                 {
2042                 if(host->old_cmd != mrq->cmd->opcode)
2043                 {
2044                     if( ((17==host->old_cmd)&&(18==mrq->cmd->opcode)) || ((18==host->old_cmd)&&(17==mrq->cmd->opcode)) ||\
2045                          ((24==host->old_cmd)&&(25==mrq->cmd->opcode)) || ((25==host->old_cmd)&&(24==mrq->cmd->opcode)))
2046                     {
2047                         host->old_cmd = mrq->cmd->opcode;
2048                         if(host->error_times++ %RK29_ERROR_PRINTK_INTERVAL ==0)
2049                         {
2050                                 printk(KERN_INFO "%s: Refuse to run CMD%2d(arg=0x%8x) due to the removal of card.  1==[%s]==\n", \
2051                                     __FUNCTION__, mrq->cmd->opcode, mrq->cmd->arg, host->dma_name);
2052                                 }
2053                     }
2054                     else
2055                     {
2056                         host->old_cmd = mrq->cmd->opcode;
2057                         host->error_times = 0;
2058                         printk(KERN_INFO "%s: Refuse to run CMD%2d(arg=0x%8x) due to the removal of card.  2==[%s]==\n", \
2059                                     __FUNCTION__, mrq->cmd->opcode, mrq->cmd->arg, host->dma_name); 
2060                         }
2061                     }
2062                     else
2063                     {
2064                         if(host->error_times++ % (RK29_ERROR_PRINTK_INTERVAL*3) ==0)
2065                         {
2066                                 printk(KERN_INFO "%s: Refuse to run CMD%2d(arg=0x%8x) due to the removal of card.  3==[%s]==\n", \
2067                                     __FUNCTION__, mrq->cmd->opcode, mrq->cmd->arg, host->dma_name);
2068                         }
2069                         host->old_cmd = mrq->cmd->opcode;
2070                     }       
2071                 }
2072             host->state = STATE_IDLE;
2073             spin_unlock_irqrestore(&host->lock, iflags);
2074             
2075                 mmc_request_done(mmc, mrq);
2076                 return;
2077         }
2078         else
2079         {
2080                 if(host->old_cmd != mrq->cmd->opcode)
2081                 {       
2082                         host->old_cmd = mrq->cmd->opcode;
2083                                 host->error_times = 0;
2084                         }                       
2085         }
2086         }
2087         else
2088         {
2089         host->old_cmd = mrq->cmd->opcode;
2090         host->error_times = 0;
2091
2092         if(!test_bit(RK29_SDMMC_CARD_PRESENT, &host->flags))
2093                 {
2094                     host->state = STATE_IDLE;
2095                     mrq->cmd->error = -ENOMEDIUM;
2096             spin_unlock_irqrestore(&host->lock, iflags);
2097             
2098                 mmc_request_done(mmc, mrq);
2099                 return;
2100                 }
2101
2102         }
2103         
2104         #if 1  
2105     host->new_mrq = mrq;        
2106
2107         spin_unlock_irqrestore(&host->lock, iflags);
2108                 
2109     rk29_sdmmc_start_request(mmc);
2110         
2111         #else
2112         if (host->state == STATE_IDLE) 
2113         {
2114         spin_unlock_irqrestore(&host->lock, iflags);
2115         
2116         host->new_mrq = mrq;        
2117         rk29_sdmmc_start_request(mmc);
2118         } 
2119         else 
2120         {
2121         #ifdef RK29_SDMMC_LIST_QUEUE    
2122         
2123         printk(KERN_INFO "%s..%d...Danger! Danger! New request was added to queue. [%s]\n", \
2124                                 __FUNCTION__, __LINE__,host->dma_name);
2125         list_add_tail(&host->queue_node, &host->queue);
2126         
2127         #else
2128
2129         printk(KERN_ERR "%s..%d..state Error! ,old_state=%d, OldCMD=%d ,NewCMD%2d,arg=0x%x [%s]\n", \
2130                                 __FUNCTION__, __LINE__, host->state, host->cmd->opcode,mrq->cmd->opcode,mrq->cmd->arg, host->dma_name);
2131                                 
2132                 mrq->cmd->error = -ENOMEDIUM;
2133
2134                 spin_unlock_irqrestore(&host->lock, iflags);            
2135                 mmc_request_done(mmc, mrq);
2136                 
2137                 return;
2138                                 
2139         #endif  
2140         }       
2141         #endif
2142
2143 }
2144
2145
2146
2147 static void rk29_sdmmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
2148 {
2149     int timeout = 250;
2150     unsigned int value;
2151         struct rk29_sdmmc *host = mmc_priv(mmc);
2152
2153     spin_lock(&host->lock);
2154
2155     if(test_bit(RK29_SDMMC_CARD_PRESENT, &host->flags) || (RK29_CTRL_SDMMC_ID == host->pdev->id))
2156     {
2157         /*
2158          * Waiting SDIO controller to be IDLE.
2159         */
2160         while (timeout-- > 0)
2161         {
2162                 value = rk29_sdmmc_read(host->regs, SDMMC_STATUS);
2163                 if ((value & SDMMC_STAUTS_DATA_BUSY) == 0 &&(value & SDMMC_CMD_FSM_MASK) == SDMMC_CMD_FSM_IDLE)
2164                 {
2165                         break;
2166                 }
2167                 
2168                 mdelay(1);
2169         }
2170         if (timeout <= 0)
2171         {
2172                 printk(KERN_ERR "%s..%d...Waiting for SDMMC%d controller to be IDLE timeout.[%s]\n", \
2173                                 __FUNCTION__, __LINE__, host->pdev->id, host->dma_name);
2174
2175                 goto out;
2176         }
2177         }
2178
2179     //if(host->bus_mode != ios->power_mode)
2180     {
2181         switch (ios->power_mode) 
2182         {
2183             case MMC_POWER_UP:
2184                 rk29_sdmmc_write(host->regs, SDMMC_PWREN, POWER_ENABLE);
2185                 
2186                 //reset the controller if it is SDMMC0
2187                 if(RK29_CTRL_SDMMC_ID == host->pdev->id)
2188                 {
2189                     xbwprintk(7, "%s..%d..POWER_UP, call reset_controller, initialized_flags=%d [%s]\n",\
2190                         __FUNCTION__, __LINE__, host->mmc->re_initialized_flags,host->dma_name);
2191                         
2192                     mdelay(5);
2193                         
2194                     rk29_sdmmc_hw_init(host);
2195                 }
2196                 
2197                 break;
2198             case MMC_POWER_OFF:
2199               
2200                 if(RK29_CTRL_SDMMC_ID == host->pdev->id)
2201                 {
2202                         rk29_sdmmc_control_clock(host, FALSE);
2203                         rk29_sdmmc_write(host->regs, SDMMC_PWREN, POWER_DISABLE);
2204                 
2205                         if(5 == host->bus_mode)
2206                         {
2207                         mdelay(5);
2208                         xbwprintk(7, "%s..%d..Fisrt powerOFF, call reset_controller [%s]\n", \
2209                             __FUNCTION__, __LINE__,host->dma_name);
2210                             
2211                         rk29_sdmmc_reset_controller(host);
2212                         }
2213               
2214                 }
2215
2216                 break;          
2217             default:
2218                 break;
2219         }
2220         
2221         host->bus_mode = ios->power_mode;
2222         
2223         }
2224
2225     if(!(test_bit(RK29_SDMMC_CARD_PRESENT, &host->flags) || (RK29_CTRL_SDIO1_ID != host->pdev->id)))
2226         goto out; //exit the set_ios directly if the SDIO is not present. 
2227         
2228         if(host->ctype != ios->bus_width)
2229         {
2230         switch (ios->bus_width) 
2231         {
2232             case MMC_BUS_WIDTH_1:
2233                 host->ctype = SDMMC_CTYPE_1BIT;
2234                 break;
2235             case MMC_BUS_WIDTH_4:
2236                 host->ctype = SDMMC_CTYPE_4BIT;
2237                 break;
2238             case MMC_BUS_WIDTH_8:
2239                 host->ctype = SDMMC_CTYPE_8BIT;
2240                 break;
2241             default:
2242                 host->ctype = 0;
2243                 break;
2244         }
2245
2246             rk29_sdmmc_set_buswidth(host);
2247             
2248         }
2249         
2250         if (ios->clock && (ios->clock != host->clock)) 
2251         {       
2252                 /*
2253                  * Use mirror of ios->clock to prevent race with mmc
2254                  * core ios update when finding the minimum.
2255                  */
2256                 //host->clock = ios->clock;     
2257                 rk29_sdmmc_change_clk_div(host, ios->clock);
2258         }
2259 out:    
2260
2261     spin_unlock(&host->lock);
2262     
2263 }
2264
2265 static int rk29_sdmmc_get_ro(struct mmc_host *mmc)
2266 {
2267     struct rk29_sdmmc *host = mmc_priv(mmc);
2268     int ret=0;
2269
2270     switch(host->pdev->id)
2271     {
2272         case 0:
2273         {
2274             #if defined(CONFIG_SDMMC0_RK29_WRITE_PROTECT) || defined(CONFIG_SDMMC1_RK29_WRITE_PROTECT)            
2275                 if(INVALID_GPIO == host->write_protect)
2276                     ret = 0;//no write-protect
2277                 else
2278                 ret = gpio_get_value(host->write_protect)?1:0;
2279            
2280             xbwprintk(7,"%s..%d.. write_prt_pin=%d, get_ro=%d. [%s]\n",\
2281                 __FUNCTION__, __LINE__,host->write_protect, ret, host->dma_name);
2282                             
2283             #else
2284                 u32 wrtprt = rk29_sdmmc_read(host->regs, SDMMC_WRTPRT);
2285                 
2286                 ret = (wrtprt & SDMMC_WRITE_PROTECT)?1:0;
2287             #endif
2288
2289             break;
2290         }
2291         
2292         case 1:
2293             ret = 0;//no write-protect
2294             break;
2295         
2296         default:
2297             ret = 0;
2298         break;   
2299     }
2300
2301     return ret;
2302
2303 }
2304
2305
2306 static void rk29_sdmmc_enable_sdio_irq(struct mmc_host *mmc, int enable)
2307 {
2308         u32 intmask;
2309         unsigned long flags;
2310         struct rk29_sdmmc *host = mmc_priv(mmc);
2311         
2312     spin_lock_irqsave(&host->lock, flags);
2313     
2314         intmask = rk29_sdmmc_read(host->regs, SDMMC_INTMASK);
2315         
2316         if(enable)
2317                 rk29_sdmmc_write(host->regs, SDMMC_INTMASK, intmask | SDMMC_INT_SDIO);
2318         else
2319                 rk29_sdmmc_write(host->regs, SDMMC_INTMASK, intmask & ~SDMMC_INT_SDIO);
2320     spin_unlock_irqrestore(&host->lock, flags);
2321     
2322 }
2323
2324 static void  rk29_sdmmc_init_card(struct mmc_host *mmc, struct mmc_card *card)
2325 {
2326         card->quirks = MMC_QUIRK_BLKSZ_FOR_BYTE_MODE;
2327
2328 }
2329
2330 static int rk29_sdmmc_clear_fifo(struct rk29_sdmmc *host)
2331 {
2332     unsigned int timeout, value;
2333     int ret = SDM_SUCCESS;
2334
2335     if(RK29_CTRL_SDMMC_ID == host->pdev->id)
2336     {
2337         rk29_sdmmc_write(host->regs, SDMMC_RINTSTS, 0xFFFFFFFF);
2338     }
2339
2340     rk29_sdmmc_stop_dma(host);
2341     rk29_sdmmc_control_host_dma(host, FALSE);
2342     host->dodma = 0;
2343    
2344     //Clean the fifo.
2345     for(timeout=0; timeout<FIFO_DEPTH; timeout++)
2346     {
2347         if(rk29_sdmmc_read(host->regs, SDMMC_STATUS) & SDMMC_STAUTS_FIFO_EMPTY)
2348             break;
2349             
2350         value = rk29_sdmmc_read(host->regs, SDMMC_DATA);
2351     }
2352
2353      /* reset */
2354     timeout = 1000;
2355     value = rk29_sdmmc_read(host->regs, SDMMC_CTRL);
2356     value |= (SDMMC_CTRL_RESET | SDMMC_CTRL_FIFO_RESET | SDMMC_CTRL_DMA_RESET);
2357     rk29_sdmmc_write(host->regs, SDMMC_CTRL, value);
2358
2359     value = rk29_sdmmc_read(host->regs, SDMMC_CTRL);
2360     
2361     while( (value & (SDMMC_CTRL_FIFO_RESET | SDMMC_CTRL_RESET | SDMMC_CTRL_DMA_RESET)) && (timeout > 0))
2362     {
2363         udelay(1);
2364         timeout--;
2365         value = rk29_sdmmc_read(host->regs, SDMMC_CTRL);
2366     }
2367
2368     if (timeout == 0)
2369     {
2370         host->errorstep = 0x0A;
2371         ret = SDM_WAIT_FOR_FIFORESET_TIMEOUT;
2372     }
2373
2374     return ret;
2375 }
2376
2377
2378
2379 static const struct mmc_host_ops rk29_sdmmc_ops[] = {
2380         {
2381                 .request        = rk29_sdmmc_request,
2382                 .set_ios        = rk29_sdmmc_set_ios,
2383                 .get_ro         = rk29_sdmmc_get_ro,
2384                 .get_cd         = rk29_sdmmc_get_cd,
2385         },
2386         {
2387                 .request        = rk29_sdmmc_request,
2388                 .set_ios        = rk29_sdmmc_set_ios,
2389                 .get_ro         = rk29_sdmmc_get_ro,
2390                 .get_cd         = rk29_sdmmc_get_cd,
2391                 .enable_sdio_irq = rk29_sdmmc_enable_sdio_irq,
2392                 .init_card       = rk29_sdmmc_init_card,
2393         },
2394 };
2395
2396 static void rk29_sdmmc_request_end(struct rk29_sdmmc *host, struct mmc_command *cmd)
2397 {
2398         u32 status = host->data_status;
2399         int output=SDM_SUCCESS;
2400
2401         xbwprintk(7, "%s..%d...  cmd=%d, host->state=0x%x,\n   pendingEvent=0x%lu, completeEvents=0x%lu [%s]\n",\
2402         __FUNCTION__, __LINE__,cmd->opcode,host->state, host->pending_events,host->completed_events,host->dma_name);
2403
2404     del_timer_sync(&host->DTO_timer);
2405
2406     if(RK29_CTRL_SDMMC_ID == host->pdev->id)
2407     {
2408         rk29_sdmmc_write(host->regs, SDMMC_RINTSTS, 0xFFFFFFFF); //added by xbw at 2011-08-15
2409     }
2410
2411     //stop DMA
2412     if(host->dodma)
2413     {
2414         rk29_sdmmc_stop_dma(host);
2415         rk29_sdmmc_control_host_dma(host, FALSE);
2416         host->dodma = 0;
2417     }
2418
2419     if(cmd->error)
2420     {
2421         goto exit;//It need not to wait-for-busy if the CMD-ERROR happen.
2422     }
2423     host->errorstep = 0xf7;
2424     if(cmd->data)
2425     {        
2426         if(host->cmdr & SDMMC_CMD_DAT_WRITE)
2427         {
2428             if(status & (SDMMC_INT_DCRC | SDMMC_INT_EBE))
2429             {
2430                 cmd->data->error = -EILSEQ;               
2431                 output = SDM_DATA_CRC_ERROR;
2432                 host->errorstep = 0x16; 
2433             }
2434             else
2435             {
2436                 output = rk29_sdmmc_wait_unbusy(host);
2437                 if(SDM_SUCCESS != output)
2438                 {
2439                     host->errorstep = 0x17;
2440                     cmd->data->error = -ETIMEDOUT;
2441                 }
2442
2443                 host->data->bytes_xfered = host->data->blocks * host->data->blksz;
2444             }
2445         }
2446         else
2447         {
2448             if( status  & SDMMC_INT_SBE)
2449             {
2450                 cmd->data->error = -EIO;
2451                 host->errorstep = 0x18;
2452                 output = SDM_START_BIT_ERROR;
2453             }
2454             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.
2455             {
2456                 cmd->data->error = -EILSEQ;
2457                 host->errorstep = 0x19;
2458                 output = SDM_END_BIT_ERROR;
2459             }
2460             else if(status  & SDMMC_INT_DRTO)
2461             {
2462                 cmd->data->error = -ETIMEDOUT;
2463                 host->errorstep = 0x1A;
2464                 output = SDM_DATA_READ_TIMEOUT;
2465             }
2466             else if(status  & SDMMC_INT_DCRC)
2467             {
2468                 host->errorstep = 0x1B;
2469                 cmd->data->error = -EILSEQ;
2470                 output = SDM_DATA_CRC_ERROR;
2471             }
2472             else
2473             {
2474                 output = rk29_sdmmc_read_remain_data(host, (host->data->blocks * host->data->blksz), host->pbuf);
2475                 if(SDM_SUCCESS == output)
2476                 {
2477                     host->data->bytes_xfered = host->data->blocks * host->data->blksz;
2478                 }
2479             }       
2480         }
2481     }
2482
2483     if(SDM_SUCCESS == output)
2484     {
2485         if ((mmc_resp_type(cmd) == MMC_RSP_R1B) || (MMC_STOP_TRANSMISSION == cmd->opcode))
2486         {
2487             output = rk29_sdmmc_wait_unbusy(host);
2488             if((SDM_SUCCESS != output) && (!host->mrq->cmd->error))
2489             {
2490                 printk(KERN_ERR "%s..%d...   CMD12 wait busy timeout!!!!! errorStep=0x%x   [%s]\n", \
2491                                                 __FUNCTION__, __LINE__, host->errorstep, host->dma_name);
2492                 rk29_sdmmc_clear_fifo(host);
2493                 cmd->error = -ETIMEDOUT;
2494                 host->mrq->cmd->error = -ETIMEDOUT;
2495                 host->errorstep = 0x1C;
2496             }
2497         }
2498     }
2499     host->errorstep = 0xf6;
2500     
2501     //trace error
2502     if(cmd->data && cmd->data->error)
2503     { 
2504         if( (!cmd->error) && (0==cmd->retries))
2505         {         
2506             printk(KERN_ERR "%s..%d......CMD=%d error!!!(arg=0x%x,cmdretry=%d,blksize=%d, blocks=%d), \n \
2507                 statusReg=0x%x, ctrlReg=0x%x, nerrorTimes=%d, errorStep=0x%x. [%s]\n",\
2508                 __FUNCTION__, __LINE__, cmd->opcode, cmd->arg, cmd->retries,cmd->data->blksz, cmd->data->blocks,
2509                 rk29_sdmmc_read(host->regs, SDMMC_STATUS),
2510                 rk29_sdmmc_read(host->regs, SDMMC_CTRL),
2511                 host->error_times,host->errorstep, host->dma_name);
2512         }
2513         cmd->error = -ENODATA;
2514     }
2515     host->errorstep = 0xf5;
2516
2517 exit:
2518
2519 #ifdef RK29_SDMMC_LIST_QUEUE
2520         if (!list_empty(&host->queue)) 
2521         {
2522                 printk(KERN_ERR "%s..%d..  Danger!Danger!. continue the next request in the queue.  [%s]\n",\
2523                         __FUNCTION__, __LINE__, host->dma_name);
2524
2525                 host = list_entry(host->queue.next,
2526                                 struct rk29_sdmmc, queue_node);
2527                 list_del(&host->queue_node);
2528                 host->state = STATE_SENDING_CMD;
2529                 rk29_sdmmc_start_request(host->mmc);
2530         } 
2531         else 
2532         {       
2533                 dev_vdbg(&host->pdev->dev, "list empty\n");
2534                 host->state = STATE_IDLE;
2535         }
2536 #else
2537     dev_vdbg(&host->pdev->dev, "list empty\n");
2538         host->state = STATE_IDLE;
2539 #endif
2540         
2541 }
2542
2543 static int rk29_sdmmc_command_complete(struct rk29_sdmmc *host,
2544                         struct mmc_command *cmd)
2545 {
2546         u32      value, status = host->cmd_status;
2547         int  timeout, output= SDM_SUCCESS;
2548
2549     xbwprintk(7, "%s..%d.  cmd=%d, host->state=0x%x, cmdINT=0x%x\n,pendingEvent=0x%lu,completeEvents=0x%lu. [%s]\n",\
2550         __FUNCTION__, __LINE__,cmd->opcode,host->state,status, host->pending_events,host->completed_events,host->dma_name);
2551
2552
2553     del_timer_sync(&host->request_timer);
2554     
2555     host->cmd_status = 0;
2556
2557         if((RK29_CTRL_SDMMC_ID == host->pdev->id) && (host->cmdr & SDMMC_CMD_STOP))
2558     {
2559         output = rk29_sdmmc_reset_fifo(host);
2560         if (SDM_SUCCESS != output)
2561         {
2562             printk(KERN_ERR "%s..%d......reset fifo fail! CMD%d(arg=0x%x, Retries=%d) [%s]\n",__FUNCTION__, __LINE__, \
2563                 cmd->opcode, cmd->arg, cmd->retries,host->dma_name);
2564                 
2565             cmd->error = -ETIMEDOUT;
2566             host->mrq->cmd->error = cmd->error;
2567             output = SDM_ERROR;
2568             host->errorstep = 0x1C;
2569             goto CMD_Errror;
2570         }
2571     }
2572
2573     if(status & SDMMC_INT_RTO)
2574         {
2575             cmd->error = -ENOMEM;
2576             host->mrq->cmd->error = cmd->error;
2577         output = SDM_BUSY_TIMEOUT;
2578
2579         //rk29_sdmmc_write(host->regs, SDMMC_RINTSTS,SDMMC_INT_RTO);
2580         rk29_sdmmc_write(host->regs, SDMMC_RINTSTS,0xFFFFFFFF);  //modifyed by xbw at 2011-08-15
2581         
2582         if(host->use_dma)//if(host->dodma)
2583         {
2584            if(host->dodma) 
2585            {
2586                 rk29_sdmmc_stop_dma(host);
2587                 rk29_sdmmc_control_host_dma(host, FALSE);
2588                 host->dodma = 0;
2589            }
2590             
2591             value = rk29_sdmmc_read(host->regs, SDMMC_CTRL);
2592             value |= SDMMC_CTRL_FIFO_RESET;
2593             rk29_sdmmc_write(host->regs, SDMMC_CTRL, value);
2594
2595             timeout = 1000;
2596             while (((value = rk29_sdmmc_read(host->regs, SDMMC_CTRL)) & (SDMMC_CTRL_FIFO_RESET)) && (timeout > 0))
2597             {
2598                 udelay(1);
2599                 timeout--;
2600             }
2601             if (timeout == 0)
2602             {   
2603                 output = SDM_FALSE;
2604                 host->errorstep = 0x1D;
2605                 printk(KERN_ERR "%s..%d......reset CTRL fail! CMD%d(arg=0x%x, Retries=%d).[%s]\n",\
2606                     __FUNCTION__, __LINE__, cmd->opcode, cmd->arg, cmd->retries,host->dma_name);
2607                 
2608                goto CMD_Errror;
2609             }
2610         }
2611
2612         }       
2613
2614         if(cmd->flags & MMC_RSP_PRESENT) 
2615         {
2616             if(cmd->flags & MMC_RSP_136) 
2617             {
2618             cmd->resp[3] = rk29_sdmmc_read(host->regs, SDMMC_RESP0);
2619             cmd->resp[2] = rk29_sdmmc_read(host->regs, SDMMC_RESP1);
2620             cmd->resp[1] = rk29_sdmmc_read(host->regs, SDMMC_RESP2);
2621             cmd->resp[0] = rk29_sdmmc_read(host->regs, SDMMC_RESP3);
2622             } 
2623             else 
2624             {
2625                 cmd->resp[0] = rk29_sdmmc_read(host->regs, SDMMC_RESP0);
2626             }
2627         }
2628         
2629         if(cmd->error)
2630         {
2631             del_timer_sync(&host->DTO_timer);
2632
2633         //trace error
2634             if((0==cmd->retries) && (host->error_times++%RK29_ERROR_PRINTK_INTERVAL == 0) && (12 != cmd->opcode))
2635             {
2636                 if( ((RK29_CTRL_SDMMC_ID==host->pdev->id)&&(MMC_SLEEP_AWAKE!=cmd->opcode)) || 
2637                      ((RK29_CTRL_SDMMC_ID!=host->pdev->id)&&(MMC_SEND_EXT_CSD!=cmd->opcode))  )
2638                 {
2639                     printk(KERN_ERR "%s..%d...CMD%d(arg=0x%x), hoststate=%d, errorTimes=%d, errorStep=0x%x ! [%s]\n",\
2640                     __FUNCTION__, __LINE__, cmd->opcode, cmd->arg, host->state,host->error_times,host->errorstep, host->dma_name);
2641                 }
2642             }
2643
2644         }
2645     del_timer_sync(&host->request_timer);
2646
2647
2648         return SDM_SUCCESS;
2649    
2650 CMD_Errror:
2651     del_timer_sync(&host->request_timer);
2652         del_timer_sync(&host->DTO_timer);
2653
2654         if((0==cmd->retries) && (host->error_times++%RK29_ERROR_PRINTK_INTERVAL == 0))
2655     {
2656         printk(KERN_ERR "%s..%d....command_complete(CMD=%d, arg=%x) error=%d. [%s]\n",\
2657             __FUNCTION__, __LINE__, host->cmd->opcode,host->cmd->arg, output, host->dma_name);
2658     }
2659         
2660     return output;
2661     
2662 }
2663
2664
2665 static void rk29_sdmmc_start_error(struct rk29_sdmmc *host)
2666 {
2667     host->cmd->error = -EIO;
2668     host->mrq->cmd->error = -EIO;
2669     host->cmd_status |= SDMMC_INT_RTO;
2670
2671     del_timer_sync(&host->request_timer);
2672
2673     rk29_sdmmc_command_complete(host, host->mrq->cmd);    
2674     rk29_sdmmc_request_end(host, host->mrq->cmd);
2675 }
2676
2677 static void rk29_sdmmc_tasklet_func(unsigned long priv)
2678 {
2679         struct rk29_sdmmc       *host = (struct rk29_sdmmc *)priv;
2680         struct mmc_data         *data = host->cmd->data;
2681         enum rk29_sdmmc_state   state = host->state;
2682         int pending_flag, stopflag;
2683         unsigned long iflags;
2684         
2685         spin_lock_irqsave(&host->lock, iflags); 
2686         
2687         state = host->state;
2688         pending_flag = 0;
2689         stopflag = 0;
2690         
2691         do 
2692         {
2693         switch (state) 
2694         {
2695             case STATE_IDLE:
2696             {
2697                 xbwprintk(7, "%s..%d..   prev_state=  STATE_IDLE  [%s]\n", \
2698                                                 __FUNCTION__, __LINE__, host->dma_name);
2699                 break;
2700             }
2701
2702             case STATE_SENDING_CMD:
2703             {
2704                 xbwprintk(7, "%s..%d..   prev_state=  STATE_SENDING_CMD, pendingEvernt=0x%lu  [%s]\n",\
2705                     __FUNCTION__, __LINE__,host->completed_events, host->dma_name);
2706
2707                 if (!rk29_sdmmc_test_and_clear_pending(host, EVENT_CMD_COMPLETE))
2708                         break;
2709                  host->errorstep = 0xfb;
2710
2711                 del_timer_sync(&host->request_timer); //delete the timer for INT_COME_DONE
2712
2713                 rk29_sdmmc_set_completed(host, EVENT_CMD_COMPLETE);
2714                 rk29_sdmmc_command_complete(host, host->cmd);
2715
2716                 
2717                 if (!data) 
2718                 {
2719                     rk29_sdmmc_request_end(host, host->cmd);
2720
2721                     xbwprintk(7, "%s..%d..  CMD%d call mmc_request_done() . [%s]\n", \
2722                                                         __FUNCTION__, __LINE__,host->cmd->opcode,host->dma_name);
2723                     
2724                     host->complete_done = 1;
2725                     break;
2726                 }
2727                 host->errorstep = 0xfa;
2728                 if(host->cmd->error)
2729                 {
2730                     del_timer_sync(&host->DTO_timer); //delete the timer for INT_DTO
2731                     
2732                     if(data->stop)
2733                     {
2734                         xbwprintk(7, "%s..%d..  cmderr, so call send_stop_cmd() [%s]\n", \
2735                                                                 __FUNCTION__, __LINE__, host->dma_name);
2736
2737                         #if 0
2738                         state = STATE_SENDING_CMD;//STATE_SENDING_STOP; 
2739                         send_stop_cmd(host);
2740                         #else
2741                         stopflag = 1;  //Moidfyed by xbw at 2011-09-08
2742                         #endif
2743                         break;
2744                     }
2745
2746                     rk29_sdmmc_set_pending(host, EVENT_DATA_COMPLETE);
2747                 }
2748
2749                 host->errorstep = 0xf9;
2750                 state = STATE_DATA_BUSY;
2751                 /* fall through */
2752             }
2753
2754             case STATE_DATA_BUSY:
2755             {
2756                 xbwprintk(7, "%s..%d..   prev_state= STATE_DATA_BUSY, pendingEvernt=0x%lu [%s]\n", \
2757                                                 __FUNCTION__, __LINE__,host->pending_events, host->dma_name);
2758
2759                 if (!rk29_sdmmc_test_and_clear_pending(host, EVENT_DATA_COMPLETE))
2760                         break;  
2761                 host->errorstep = 0xf8;
2762                 rk29_sdmmc_set_completed(host, EVENT_DATA_COMPLETE);
2763
2764              #if SDMMC_USE_INT_UNBUSY
2765                 if((24==host->cmd->opcode)||(25==host->cmd->opcode))
2766                 {
2767                     /*
2768                     ** use DTO_timer for waiting for INT_UNBUSY.
2769                     ** max 250ms in specification, but adapt 500 for the compatibility of all kinds of sick sdcard. 
2770                     */                    
2771                     mod_timer(&host->DTO_timer, jiffies + msecs_to_jiffies(500));
2772                 }
2773                 else
2774                 {
2775                     del_timer_sync(&host->DTO_timer); //delete the timer for INT_DTO
2776                 }
2777   
2778                 state = STATE_DATA_UNBUSY;
2779
2780              #else
2781                 del_timer_sync(&host->DTO_timer); //delete the timer for INT_DTO
2782              #endif
2783              }
2784
2785              case STATE_DATA_UNBUSY:
2786              {
2787              #if SDMMC_USE_INT_UNBUSY
2788                 if((24==host->cmd->opcode)||(25==host->cmd->opcode))
2789                 {
2790                     if (!rk29_sdmmc_test_and_clear_pending(host, EVENT_DATA_UNBUSY))
2791                         break;
2792
2793                     del_timer_sync(&host->DTO_timer);
2794                 }
2795                 rk29_sdmmc_set_completed(host, EVENT_DATA_UNBUSY);
2796                 state = STATE_DATA_END;
2797              #endif 
2798                 rk29_sdmmc_request_end(host, host->cmd);
2799
2800                 if (data && !data->stop) 
2801                 {
2802                     xbwprintk(7, "%s..%d..  CMD%d call mmc_request_done(). [%s]\n", \
2803                                                         __FUNCTION__, __LINE__,host->cmd->opcode,host->dma_name);
2804
2805                     if(!( (MMC_READ_SINGLE_BLOCK == host->cmd->opcode)&&( -EIO == data->error))) //deal with START_BIT_ERROR
2806                     {
2807                         host->complete_done = 2;
2808                         break;
2809                     }
2810
2811                 }
2812                 host->errorstep = 0xf4;
2813                 xbwprintk(7, "%s..%d..  after DATA_COMPLETE, so call send_stop_cmd() [%s]\n", \
2814                                                 __FUNCTION__, __LINE__, host->dma_name);
2815
2816                 #if 0
2817                 state = STATE_SENDING_CMD;
2818                 send_stop_cmd(host);
2819                 #else
2820                 stopflag = 2; //Moidfyed by xbw at 2011-09-08
2821                 #endif
2822                 
2823                 break;
2824             }
2825
2826             case STATE_SENDING_STOP:
2827             {
2828                 xbwprintk(7, "%s..%d..   prev_state=  STATE_SENDING_STOP, pendingEvernt=0x%lu  [%s]\n", \
2829                                                 __FUNCTION__, __LINE__, host->pending_events, host->dma_name);
2830
2831                 if (!rk29_sdmmc_test_and_clear_pending(host, EVENT_CMD_COMPLETE))
2832                         break;
2833
2834                 rk29_sdmmc_command_complete(host, host->cmd);
2835                 del_timer_sync(&host->request_timer); //delete the timer for INT_CMD_DONE int CMD12
2836                 rk29_sdmmc_request_end(host, host->cmd);
2837                 
2838                 host->complete_done = 3;
2839                 break;
2840             }
2841             
2842             case STATE_DATA_END:
2843                 break;
2844             default:
2845                 break;          
2846         }
2847
2848         pending_flag = (host->complete_done > 0) && (host->retryfunc<50) \
2849                        && (rk29_sdmmc_test_pending(host, EVENT_CMD_COMPLETE)|| rk29_sdmmc_test_pending(host, EVENT_DATA_COMPLETE) );
2850
2851         if(pending_flag)
2852         {
2853             xbwprintk(7, "%s..%d...  cmd=%d(arg=0x%x),completedone=%d, retrycount=%d, doneflag=%d, \n \
2854                 host->state=0x%x, switchstate=%x, \n \
2855                 pendingEvent=0x%lu, completeEvents=0x%lu, \n \
2856                 mrqCMD=%d, arg=0x%x [%s]\n",\
2857                 
2858                 __FUNCTION__, __LINE__,host->cmd->opcode, host->cmd->arg, host->complete_done,\
2859                 host->retryfunc, host->mmc->doneflag,host->state, state, \
2860                 host->pending_events,host->completed_events,\
2861                 host->mrq->cmd->opcode, host->mrq->cmd->arg, host->dma_name);
2862                 
2863             cpu_relax();
2864         }
2865                         
2866         } while(pending_flag && ++host->retryfunc); //while(0);
2867
2868          if(0!=stopflag)
2869     {
2870         if(host->cmd->error)
2871         xbwprintk(3,"%d:  call send_stop_cmd== %d,  completedone=%d, doneflag=%d, hoststate=%x, statusReg=0x%x \n", \
2872             __LINE__,stopflag, host->complete_done, host->mmc->doneflag, state, rk29_sdmmc_read(host->regs, SDMMC_STATUS));
2873             
2874         state = STATE_SENDING_CMD;
2875         send_stop_cmd(host);   //Moidfyed by xbw at 2011-09-08
2876     }
2877
2878         host->state = state;
2879                  
2880     if(0==host->complete_done)
2881     {
2882         host->errorstep = 0xf2;
2883         
2884         spin_unlock_irqrestore(&host->lock, iflags);
2885         return;
2886     }
2887     host->errorstep = 0xf3; 
2888         host->state = STATE_IDLE;
2889          
2890          if(host->mrq && host->mmc->doneflag)
2891          {
2892             host->mmc->doneflag = 0;
2893             spin_unlock_irqrestore(&host->lock, iflags);            
2894             mmc_request_done(host->mmc, host->mrq);
2895          }
2896          else
2897          {
2898             spin_unlock_irqrestore(&host->lock, iflags);            
2899          }
2900 }
2901
2902
2903 static inline void rk29_sdmmc_cmd_interrupt(struct rk29_sdmmc *host, u32 status)
2904 {
2905     u32 multi, unit;
2906     
2907         host->cmd_status |= status;
2908     host->errorstep = 0xfc;
2909     if((MMC_STOP_TRANSMISSION != host->cmd->opcode) && (host->cmdr & SDMMC_CMD_DAT_EXP))
2910     {
2911         unit = 3*1024*1024;
2912         multi = rk29_sdmmc_read(host->regs, SDMMC_BYTCNT)/unit;
2913         multi += ((rk29_sdmmc_read(host->regs, SDMMC_BYTCNT)%unit) ? 1 :0 );
2914         multi = (multi>0) ? multi : 1;
2915         multi += (host->cmd->retries>2)?2:host->cmd->retries;
2916             mod_timer(&host->DTO_timer, jiffies + msecs_to_jiffies(RK29_SDMMC_WAIT_DTO_INTERNVAL*multi));//max wait 8s larger  
2917         }
2918         
2919         smp_wmb();
2920         rk29_sdmmc_set_pending(host, EVENT_CMD_COMPLETE);
2921         tasklet_schedule(&host->tasklet);
2922 }
2923
2924 static irqreturn_t rk29_sdmmc_interrupt(int irq, void *dev_id)
2925 {
2926         struct rk29_sdmmc       *host = dev_id;
2927         u32                     status,  pending;
2928         bool present;
2929         bool present_old;
2930         int sdio_irq=0;
2931
2932         spin_lock(&host->lock);
2933
2934     status = rk29_sdmmc_read(host->regs, SDMMC_RINTSTS);
2935     pending = rk29_sdmmc_read(host->regs, SDMMC_MINTSTS);// read only mask reg
2936     if (!pending)
2937     {
2938         goto Exit_INT;
2939     }
2940
2941
2942     if(pending & SDMMC_INT_CD) 
2943     {
2944         //disable_irq_nosync(host->irq);
2945         rk29_sdmmc_write(host->regs, SDMMC_RINTSTS, SDMMC_INT_CD); // clear sd detect int
2946         present = rk29_sdmmc_get_cd(host->mmc);
2947         present_old = test_bit(RK29_SDMMC_CARD_PRESENT, &host->flags);
2948         
2949         if(present != present_old)
2950         {           
2951                 printk(KERN_INFO "\n******************\n%s:INT_CD=0x%x,INT-En=%d,hostState=%d,  present Old=%d ==> New=%d . [%s]\n",\
2952                     __FUNCTION__, pending, host->mmc->re_initialized_flags, host->state, present_old, present,  host->dma_name);
2953
2954             rk28_send_wakeup_key(); //wake up backlight
2955             host->error_times = 0;
2956
2957             #if 1
2958             del_timer(&host->request_timer);
2959                 del_timer(&host->DTO_timer);
2960             rk29_sdmmc_dealwith_timeout(host);              
2961             #endif
2962                             
2963             if(present)
2964             {
2965                 set_bit(RK29_SDMMC_CARD_PRESENT, &host->flags);
2966
2967                 if(host->mmc->re_initialized_flags)
2968                     {
2969                         mod_timer(&host->detect_timer, jiffies + msecs_to_jiffies(RK29_SDMMC_REMOVAL_DELAY));
2970                     }
2971                     else
2972                     {
2973                         mod_timer(&host->detect_timer, jiffies + msecs_to_jiffies(RK29_SDMMC_REMOVAL_DELAY*2));
2974                     }
2975             }
2976             else
2977             {
2978                 clear_bit(RK29_SDMMC_CARD_PRESENT, &host->flags);
2979                 host->mmc->re_initialized_flags = 0;
2980
2981                 mmc_detect_change(host->mmc, 200);
2982             }
2983
2984         }
2985
2986         goto Exit_INT;
2987
2988     }
2989     
2990     xbwprintk(1,"\n*******test***********\n%s:INT_CD=0x%x,INT-En=%d,hostState=%d, cmd=%d,present Old=%d ==> New=%d [%s]\n",\
2991             __FUNCTION__, pending, host->mmc->re_initialized_flags, host->state, host->cmd->opcode,present_old, present,  host->dma_name);
2992
2993     if (pending & SDMMC_INT_CMD_DONE) {
2994
2995         xbwprintk(6, "%s..%d..  CMD%d INT_CMD_DONE  INT=0x%x   [%s]\n", \
2996                                 __FUNCTION__, __LINE__, host->cmd->opcode,pending, host->dma_name);
2997
2998         rk29_sdmmc_write(host->regs, SDMMC_RINTSTS,SDMMC_INT_CMD_DONE);  //  clear interrupt
2999         rk29_sdmmc_cmd_interrupt(host, status);
3000
3001         goto Exit_INT;
3002     }
3003
3004     if(pending & SDMMC_INT_SDIO) 
3005     {   
3006         xbwprintk(7, "%s..%d..  INT_SDIO  INT=0x%x   [%s]\n", \
3007                                 __FUNCTION__, __LINE__, pending, host->dma_name);
3008
3009         rk29_sdmmc_write(host->regs, SDMMC_RINTSTS,SDMMC_INT_SDIO);
3010         sdio_irq = 1;
3011
3012         goto Exit_INT;
3013     }
3014
3015
3016     if(pending & SDMMC_INT_RTO) 
3017     {
3018         xbwprintk(7, "%s..%d..  CMD%d CMD_ERROR_FLAGS  INT=0x%x   [%s]\n", \
3019                                 __FUNCTION__, __LINE__, host->cmd->opcode,pending, host->dma_name);
3020
3021         //rk29_sdmmc_write(host->regs, SDMMC_RINTSTS,SDMMC_INT_RTO);
3022         rk29_sdmmc_write(host->regs, SDMMC_RINTSTS,0xFFFFFFFF); //Modifyed by xbw at 2011-08-15
3023         host->cmd_status = status;
3024         smp_wmb();
3025         rk29_sdmmc_set_pending(host, EVENT_CMD_COMPLETE);
3026
3027         if(!(pending & SDMMC_INT_CMD_DONE))
3028         {
3029             tasklet_schedule(&host->tasklet);
3030         }
3031
3032         goto Exit_INT;
3033     }
3034
3035
3036     if(pending & SDMMC_INT_HLE)
3037     {
3038         printk(KERN_ERR "%s: Error due to hardware locked. Please check your hardware. INT=0x%x, CMD%d(arg=0x%x, retries=%d). [%s]\n",\
3039                                 __FUNCTION__, pending,host->cmd->opcode, host->cmd->arg, host->cmd->retries, host->dma_name);         
3040     
3041         rk29_sdmmc_write(host->regs, SDMMC_RINTSTS,SDMMC_INT_HLE); 
3042         goto Exit_INT;
3043     }
3044
3045
3046     if(pending & SDMMC_INT_DTO) 
3047     {   
3048         xbwprintk(1,"%d..%s: INT=0x%x ,RINTSTS=0x%x, CMD%d(arg=0x%x, retries=%d),host->state=0x%x.  [%s]\n", \
3049                                 __LINE__,__FUNCTION__, pending,status, host->cmd->opcode, host->cmd->arg, host->cmd->retries, host->state,host->dma_name);
3050
3051         rk29_sdmmc_write(host->regs, SDMMC_RINTSTS,SDMMC_INT_DTO); 
3052         del_timer(&host->DTO_timer); //delete the timer for INT_DTO
3053
3054         host->data_status |= status;
3055
3056         smp_wmb();
3057
3058         rk29_sdmmc_set_pending(host, EVENT_DATA_COMPLETE);
3059         tasklet_schedule(&host->tasklet);
3060         goto Exit_INT;
3061     }
3062
3063
3064     if (pending & SDMMC_INT_FRUN) 
3065     { 
3066         printk(KERN_ERR "%s: INT=0x%x Oh!My God,let me see!What happened?Why?Where? CMD%d(arg=0x%x, retries=%d). [%s]\n", \
3067                                 __FUNCTION__, pending, host->cmd->opcode, host->cmd->arg, host->cmd->retries,host->dma_name);
3068         
3069         rk29_sdmmc_write(host->regs, SDMMC_RINTSTS,SDMMC_INT_FRUN); 
3070         goto Exit_INT;
3071     }
3072
3073     if (pending & SDMMC_INT_RXDR) 
3074     {   
3075         xbwprintk(6, "%s..%d..  SDMMC_INT_RXDR  INT=0x%x   [%s]\n", \
3076                                 __FUNCTION__, __LINE__, pending, host->dma_name);
3077
3078         rk29_sdmmc_write(host->regs, SDMMC_RINTSTS,SDMMC_INT_RXDR);  //  clear interrupt
3079         rk29_sdmmc_do_pio_read(host);
3080     }
3081
3082     if (pending & SDMMC_INT_TXDR) 
3083     {
3084         xbwprintk(6, "%s..%d..  SDMMC_INT_TXDR  INT=0x%x   [%s]\n", \
3085                                 __FUNCTION__, __LINE__, pending, host->dma_name);
3086                                 
3087         rk29_sdmmc_write(host->regs, SDMMC_RINTSTS,SDMMC_INT_TXDR);  //  clear interrupt
3088         rk29_sdmmc_do_pio_write(host); 
3089     }
3090
3091 #if SDMMC_USE_INT_UNBUSY
3092         if(pending & SDMMC_INT_UNBUSY) 
3093         {
3094             xbwprintk(1, "%d..%s: INT=0x%x ,RINTSTS=0x%x, CMD%d(arg=0x%x, retries=%d),host->state=0x%x. [%s]\n", \
3095                     __LINE__,__FUNCTION__, pending,status, host->cmd->opcode, host->cmd->arg, host->cmd->retries, \
3096                     host->state,host->dma_name);
3097     
3098             rk29_sdmmc_write(host->regs, SDMMC_RINTSTS,SDMMC_INT_UNBUSY); 
3099             
3100             host->data_status = status;
3101             smp_wmb();
3102             rk29_sdmmc_set_pending(host, EVENT_DATA_UNBUSY);
3103             tasklet_schedule(&host->tasklet);  
3104             
3105             goto Exit_INT;
3106         }
3107 #endif
3108
3109 Exit_INT:
3110
3111         spin_unlock(&host->lock);
3112
3113     if(1 == sdio_irq)
3114     {
3115         mmc_signal_sdio_irq(host->mmc);
3116     }
3117         
3118         return IRQ_HANDLED;
3119 }
3120
3121 /*
3122  *
3123  * MMC card detect thread, kicked off from detect interrupt, 1 timer 
3124  *
3125  */
3126 static void rk29_sdmmc_detect_change(unsigned long data)
3127 {
3128         struct rk29_sdmmc *host = (struct rk29_sdmmc *)data;
3129
3130         if(!host->mmc)
3131             return;
3132    
3133         smp_rmb();
3134
3135     if((RK29_CTRL_SDMMC_ID == host->pdev->id) && rk29_sdmmc_get_cd(host->mmc))
3136     {
3137         host->mmc->re_initialized_flags =1;
3138     }
3139     
3140         mmc_detect_change(host->mmc, 0);        
3141
3142 }
3143
3144 static void rk29_sdmmc1_check_status(unsigned long data)
3145 {
3146         struct rk29_sdmmc *host = (struct rk29_sdmmc *)data;
3147         struct rk29_sdmmc_platform_data *pdata = host->pdev->dev.platform_data;
3148         unsigned int status;
3149
3150     status = pdata->status(mmc_dev(host->mmc));
3151     
3152     pr_info("%s: slot status change detected(%d-%d)\n",mmc_hostname(host->mmc), host->oldstatus, status);
3153     
3154     if (status ^ host->oldstatus)
3155     {        
3156         if (status) 
3157         {
3158             rk29_sdmmc_hw_init(host);
3159             set_bit(RK29_SDMMC_CARD_PRESENT, &host->flags);
3160             mod_timer(&host->detect_timer, jiffies + msecs_to_jiffies(200));
3161         }
3162         else 
3163         {
3164             clear_bit(RK29_SDMMC_CARD_PRESENT, &host->flags);
3165             rk29_sdmmc_detect_change((unsigned long)host);
3166         }
3167     }
3168
3169     host->oldstatus = status;
3170 }
3171
3172 static void rk29_sdmmc1_status_notify_cb(int card_present, void *dev_id)
3173 {
3174         struct rk29_sdmmc *host = dev_id;
3175
3176         rk29_sdmmc1_check_status((unsigned long)host);
3177 }
3178
3179
3180 static int rk29_sdmmc_probe(struct platform_device *pdev)
3181 {
3182         struct mmc_host                 *mmc;
3183         struct rk29_sdmmc               *host;
3184         struct resource                 *regs;
3185         struct rk29_sdmmc_platform_data *pdata;
3186         //int                           irq;
3187         int                             ret = 0;
3188
3189     /* must have platform data */
3190         pdata = pdev->dev.platform_data;
3191         if (!pdata) {
3192                 dev_err(&pdev->dev, "Platform data missing\n");
3193                 ret = -ENODEV;
3194                 host->errorstep = 0x87;
3195                 goto out;
3196         }
3197
3198         regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3199         if (!regs)
3200         {
3201             host->errorstep = 0x88;
3202                 return -ENXIO;
3203         }
3204
3205         mmc = mmc_alloc_host(sizeof(struct rk29_sdmmc), &pdev->dev);
3206         if (!mmc)
3207         {
3208             host->errorstep = 0x89;
3209                 ret = -ENOMEM;
3210                 goto rel_regions;
3211         }       
3212
3213         host = mmc_priv(mmc);
3214         host->mmc = mmc;
3215         host->pdev = pdev;      
3216
3217         host->ctype = 0; // set default 1 bit mode
3218         host->errorstep = 0;
3219         host->bus_mode = 5;
3220         host->old_cmd = 100;
3221         host->clock =0;
3222         host->old_div = 0xFF;
3223         host->error_times = 0;
3224         host->state = STATE_IDLE;
3225         host->complete_done = 0;
3226         host->retryfunc = 0;
3227         host->mrq = NULL;
3228         host->new_mrq = NULL;
3229         
3230 #ifdef CONFIG_PM
3231     host->gpio_det = pdata->detect_irq;
3232 #endif
3233     host->set_iomux = pdata->set_iomux;
3234
3235         if(pdata->io_init)
3236                 pdata->io_init();
3237                 
3238         spin_lock_init(&host->lock);
3239     
3240 #ifdef RK29_SDMMC_LIST_QUEUE    
3241         INIT_LIST_HEAD(&host->queue);
3242 #endif  
3243
3244         host->clk = clk_get(&pdev->dev, "mmc");
3245
3246 #if RK29_SDMMC_DEFAULT_SDIO_FREQ
3247     clk_set_rate(host->clk,SDHC_FPP_FREQ);
3248 #else    
3249     if(RK29_CTRL_SDMMC_ID== host->pdev->id)
3250             clk_set_rate(host->clk,SDHC_FPP_FREQ);
3251         else
3252             clk_set_rate(host->clk,RK29_MAX_SDIO_FREQ); 
3253
3254 #endif
3255
3256         clk_enable(host->clk);
3257         clk_enable(clk_get(&pdev->dev, "hclk_mmc"));
3258
3259         ret = -ENOMEM;
3260         host->regs = ioremap(regs->start, regs->end - regs->start + 1);
3261         if (!host->regs)
3262         {
3263             host->errorstep = 0x8A;
3264             goto err_freemap; 
3265         }
3266
3267     mmc->ops = &rk29_sdmmc_ops[pdev->id];
3268 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 35))    
3269         mmc->pm_flags |= MMC_PM_IGNORE_PM_NOTIFY;
3270 #endif  
3271         mmc->f_min = FOD_FREQ;
3272         
3273 #if RK29_SDMMC_DEFAULT_SDIO_FREQ
3274     mmc->f_max = SDHC_FPP_FREQ;
3275 #else
3276     if(RK29_CTRL_SDMMC_ID== host->pdev->id)
3277     {
3278         mmc->f_max = SDHC_FPP_FREQ;
3279     }
3280     else
3281     {
3282         mmc->f_max = RK29_MAX_SDIO_FREQ;
3283     }
3284
3285 #endif 
3286         //mmc->ocr_avail = pdata->host_ocr_avail;
3287         mmc->ocr_avail = MMC_VDD_27_28|MMC_VDD_28_29|MMC_VDD_29_30|MMC_VDD_30_31
3288                      | 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
3289         mmc->caps = pdata->host_caps;
3290         mmc->re_initialized_flags = 1;
3291         mmc->doneflag = 1;
3292         mmc->sdmmc_host_hw_init = rk29_sdmmc_hw_init;
3293
3294     /*
3295          * We can do SGIO
3296         */
3297 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 37))
3298         mmc->max_segs = 64;
3299 #else
3300         mmc->max_phys_segs = 64;
3301         mmc->max_hw_segs = 64; 
3302 #endif
3303
3304         /*
3305          * Block size can be up to 2048 bytes, but must be a power of two.
3306         */
3307         mmc->max_blk_size = 4095;
3308
3309         /*
3310          * No limit on the number of blocks transferred.
3311         */
3312         mmc->max_blk_count = 4096; 
3313
3314         /*
3315          * Since we only have a 16-bit data length register, we must
3316          * ensure that we don't exceed 2^16-1 bytes in a single request.
3317         */
3318         mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count; //8M bytes(2K*4K)
3319
3320     /*
3321          * Set the maximum segment size.  Since we aren't doing DMA
3322          * (yet) we are only limited by the data length register.
3323         */
3324         mmc->max_seg_size = mmc->max_req_size;
3325
3326         tasklet_init(&host->tasklet, rk29_sdmmc_tasklet_func, (unsigned long)host);
3327
3328     /* Create card detect handler thread  */
3329         setup_timer(&host->detect_timer, rk29_sdmmc_detect_change,(unsigned long)host);
3330         setup_timer(&host->request_timer,rk29_sdmmc_INT_CMD_DONE_timeout,(unsigned long)host);
3331         setup_timer(&host->DTO_timer,rk29_sdmmc_INT_DTO_timeout,(unsigned long)host);
3332
3333         host->irq = platform_get_irq(pdev, 0);
3334         if (host->irq < 0)
3335         {
3336             host->errorstep = 0x8B;
3337                 ret = -EINVAL;
3338                 goto err_freemap;
3339         }
3340
3341     memcpy(host->dma_name, pdata->dma_name, 8);    
3342         host->use_dma = pdata->use_dma;
3343
3344     xbwprintk(7,"%s..%s..%d..***********  Bus clock= %d Khz  **** [%s]\n",\
3345         __FILE__, __FUNCTION__,__LINE__,clk_get_rate(host->clk)/1000, host->dma_name);
3346
3347         /*DMA init*/
3348         if(host->use_dma)
3349         {
3350         host->dma_info = rk29_sdmmc_dma_infos[host->pdev->id];
3351         ret = rk29_dma_request(host->dma_info.chn, &(host->dma_info.client), NULL); 
3352         if (ret < 0)
3353         {
3354                 printk(KERN_ERR "%s..%d...rk29_dma_request error=%d. [%s]\n", \
3355                                         __FUNCTION__, __LINE__,ret, host->dma_name);
3356                 host->errorstep = 0x97;
3357             goto err_freemap; 
3358         }
3359         
3360 #if 0  //deal with the old API of DMA-module 
3361                 ret = rk29_dma_config(host->dma_info.chn, 4);
3362 #else  //deal with the new API of DMA-module 
3363         if(RK29_CTRL_SDMMC_ID== host->pdev->id)
3364         {
3365             ret = rk29_dma_config(host->dma_info.chn, 4, 16);
3366         }
3367         else
3368         {
3369             ret = rk29_dma_config(host->dma_info.chn, 4, 1);
3370         }
3371 #endif
3372         if(ret < 0)
3373                 {
3374             printk(KERN_ERR "%s..%d..  rk29_dma_config error=%d. [%s]\n", \
3375                                         __FUNCTION__, __LINE__, ret, host->dma_name);
3376             host->errorstep = 0x98;
3377             goto err_dmaunmap;
3378                 }
3379
3380         ret = rk29_dma_set_buffdone_fn(host->dma_info.chn, rk29_sdmmc_dma_complete);    
3381                 if(ret < 0)
3382                 {
3383             printk(KERN_ERR "%s..%d..  dma_set_buffdone_fn error=%d. [%s]\n", \
3384                                         __FUNCTION__, __LINE__, ret, host->dma_name);
3385             host->errorstep = 0x99;
3386             goto err_dmaunmap;
3387                 }
3388                 
3389                 host->dma_addr = regs->start + SDMMC_DATA;
3390         }
3391
3392 #if defined(CONFIG_SDMMC0_RK29_WRITE_PROTECT) || defined(CONFIG_SDMMC1_RK29_WRITE_PROTECT)
3393         host->write_protect = pdata->write_prt; 
3394 #endif  
3395
3396     rk29_sdmmc_hw_init(host);
3397
3398     ret = request_irq(host->irq, rk29_sdmmc_interrupt, 0, dev_name(&pdev->dev), host);
3399         if (ret)
3400         {       
3401
3402             printk(KERN_ERR "%s..%d..  request_irq error=%d. [%s]\n", \
3403                                 __FUNCTION__, __LINE__, ret, host->dma_name);
3404             host->errorstep = 0x8C;
3405             goto err_dmaunmap;
3406         }
3407
3408     /* setup sdmmc1 wifi card detect change */
3409     if (pdata->register_status_notify) {
3410         pdata->register_status_notify(rk29_sdmmc1_status_notify_cb, host);
3411     }
3412
3413     if(RK29_CTRL_SDMMC_ID== host->pdev->id)
3414     {
3415         if(rk29_sdmmc_get_cd(host->mmc))
3416         {
3417             set_bit(RK29_SDMMC_CARD_PRESENT, &host->flags);
3418         }
3419         else
3420         {
3421             clear_bit(RK29_SDMMC_CARD_PRESENT, &host->flags);
3422         }
3423     }
3424     else
3425     {
3426         #if defined(CONFIG_USE_SDMMC0_FOR_WIFI_DEVELOP_BOARD)
3427         if(0== host->pdev->id)
3428         {
3429             set_bit(RK29_SDMMC_CARD_PRESENT, &host->flags);
3430         }
3431         #endif
3432
3433         #if defined(CONFIG_USE_SDMMC1_FOR_WIFI_DEVELOP_BOARD)
3434         if(1== host->pdev->id)
3435         {
3436             set_bit(RK29_SDMMC_CARD_PRESENT, &host->flags);
3437         }
3438         #endif
3439     }
3440
3441
3442     /* sdmmc1 wifi card slot status initially */
3443     if (pdata->status) {
3444         host->oldstatus = pdata->status(mmc_dev(host->mmc));
3445         if (host->oldstatus)  {
3446             set_bit(RK29_SDMMC_CARD_PRESENT, &host->flags);
3447         }else {
3448             clear_bit(RK29_SDMMC_CARD_PRESENT, &host->flags);
3449         }
3450     }
3451
3452
3453         platform_set_drvdata(pdev, mmc);        
3454
3455         mmc_add_host(mmc);
3456
3457 #ifdef RK29_SDMMC_NOTIFY_REMOVE_INSERTION
3458     
3459     globalSDhost[pdev->id] = (struct rk29_sdmmc *)host;
3460     if(0== host->pdev->id)
3461     {
3462         rk29_sdmmc_progress_add_attr(pdev);
3463     }
3464 #endif  
3465         
3466 #if defined (CONFIG_DEBUG_FS)
3467         rk29_sdmmc_init_debugfs(host);
3468 #endif
3469
3470     printk(KERN_INFO ".Line%d..The End of SDMMC-probe %s.  [%s]\n", __LINE__, RK29_SDMMC_VERSION,host->dma_name);
3471         return 0;
3472
3473
3474 err_dmaunmap:
3475         if(host->use_dma)
3476         {
3477             rk29_dma_free(host->dma_info.chn, &host->dma_info.client);
3478         }
3479
3480 err_freemap:
3481         iounmap(host->regs);
3482
3483 rel_regions:
3484     mmc_free_host(mmc);
3485
3486 out:
3487         
3488         return ret;
3489 }
3490
3491
3492
3493 static int __exit rk29_sdmmc_remove(struct platform_device *pdev)
3494 {
3495
3496     struct mmc_host *mmc = platform_get_drvdata(pdev);
3497     struct rk29_sdmmc *host;
3498     struct resource             *regs;
3499
3500     if (!mmc)
3501         return -1;
3502
3503     host = mmc_priv(mmc); 
3504     
3505         smp_wmb();
3506     rk29_sdmmc_control_clock(host, 0);
3507
3508     /* Shutdown detect IRQ and kill detect thread */
3509         del_timer_sync(&host->detect_timer);
3510         del_timer_sync(&host->request_timer);
3511         del_timer_sync(&host->DTO_timer);
3512
3513         tasklet_disable(&host->tasklet);
3514         free_irq(platform_get_irq(pdev, 0), host);
3515         if(host->use_dma)
3516         {
3517                 rk29_dma_free(host->dma_info.chn, &host->dma_info.client);
3518         }
3519
3520         mmc_remove_host(mmc);
3521
3522         iounmap(host->regs);
3523         
3524         regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3525         release_mem_region(regs->start,resource_size(regs));  
3526
3527     mmc_free_host(mmc);
3528     platform_set_drvdata(pdev, NULL);
3529
3530         return 0;
3531 }
3532
3533
3534 #ifdef CONFIG_PM
3535
3536 #if defined(CONFIG_ARCH_RK29)
3537 static irqreturn_t det_keys_isr(int irq, void *dev_id)
3538 {
3539         struct rk29_sdmmc *host = dev_id;
3540         dev_info(&host->pdev->dev, "sd det_gpio changed(%s), send wakeup key!\n",
3541                 gpio_get_value(RK29_PIN2_PA2)?"removed":"insert");
3542         rk29_sdmmc_detect_change((unsigned long)dev_id);
3543
3544         return IRQ_HANDLED;
3545 }
3546
3547 static int rk29_sdmmc_sdcard_suspend(struct rk29_sdmmc *host)
3548 {
3549         int ret = 0;
3550         rk29_mux_api_set(GPIO2A2_SDMMC0DETECTN_NAME, GPIO2L_GPIO2A2);
3551         gpio_request(RK29_PIN2_PA2, "sd_detect");
3552         gpio_direction_input(RK29_PIN2_PA2);
3553
3554         host->gpio_irq = gpio_to_irq(RK29_PIN2_PA2);
3555         ret = request_irq(host->gpio_irq, det_keys_isr,
3556                                             (gpio_get_value(RK29_PIN2_PA2))?IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING,
3557                                             "sd_detect",
3558                                             host);
3559         
3560         enable_irq_wake(host->gpio_irq);
3561
3562         return ret;
3563 }
3564 static void rk29_sdmmc_sdcard_resume(struct rk29_sdmmc *host)
3565 {
3566         disable_irq_wake(host->gpio_irq);
3567         free_irq(host->gpio_irq,host);
3568         gpio_free(RK29_PIN2_PA2);
3569         rk29_mux_api_set(GPIO2A2_SDMMC0DETECTN_NAME, GPIO2L_SDMMC0_DETECT_N);
3570 }
3571
3572 #elif defined(CONFIG_ARCH_RK30)
3573 static irqreturn_t det_keys_isr(int irq, void *dev_id)
3574 {
3575         struct rk29_sdmmc *host = dev_id;
3576         dev_info(&host->pdev->dev, "sd det_gpio changed(%s), send wakeup key!\n",
3577                 gpio_get_value(RK30_PIN3_PB6)?"removed":"insert");
3578         rk29_sdmmc_detect_change((unsigned long)dev_id);
3579
3580         return IRQ_HANDLED;
3581 }
3582
3583 static int rk29_sdmmc_sdcard_suspend(struct rk29_sdmmc *host)
3584 {
3585         int ret = 0;
3586         rk29_mux_api_set(GPIO3B6_SDMMC0DETECTN_NAME, GPIO3B_GPIO3B6);
3587         gpio_request(RK30_PIN3_PB6, "sd_detect");
3588         gpio_direction_input(RK30_PIN3_PB6);
3589
3590         host->gpio_irq = gpio_to_irq(RK30_PIN3_PB6);
3591         ret = request_irq(host->gpio_irq, det_keys_isr,
3592                                             (gpio_get_value(RK30_PIN3_PB6))?IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING,
3593                                             "sd_detect",
3594                                             host);
3595         
3596         enable_irq_wake(host->gpio_irq);
3597
3598         return ret;
3599 }
3600 static void rk29_sdmmc_sdcard_resume(struct rk29_sdmmc *host)
3601 {
3602         disable_irq_wake(host->gpio_irq);
3603         free_irq(host->gpio_irq,host);
3604         gpio_free(RK30_PIN3_PB6);
3605         rk29_mux_api_set(GPIO3B6_SDMMC0DETECTN_NAME, GPIO3B_SDMMC0_DETECT_N);
3606 }
3607
3608 #endif
3609
3610
3611 static int rk29_sdmmc_suspend(struct platform_device *pdev, pm_message_t state)
3612 {
3613     struct mmc_host *mmc = platform_get_drvdata(pdev);
3614     struct rk29_sdmmc *host = mmc_priv(mmc);
3615     int ret = 0;
3616
3617     if(host && host->pdev && (RK29_CTRL_SDMMC_ID == host->pdev->id)) //only the SDMMC0 have suspend-resume; noted by xbw
3618     {
3619         if (mmc)
3620 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 35))
3621             ret = mmc_suspend_host(mmc);
3622 #else
3623             ret = mmc_suspend_host(mmc, state);
3624 #endif
3625
3626         if(rk29_sdmmc_sdcard_suspend(host) < 0)
3627                         dev_info(&host->pdev->dev, "rk29_sdmmc_sdcard_suspend error\n");
3628     }
3629
3630     return ret;
3631 }
3632
3633 static int rk29_sdmmc_resume(struct platform_device *pdev)
3634 {
3635     struct mmc_host *mmc = platform_get_drvdata(pdev);
3636     struct rk29_sdmmc *host = mmc_priv(mmc);
3637     int ret = 0;
3638
3639     if(host && host->pdev && (RK29_CTRL_SDMMC_ID == host->pdev->id)) //only the SDMMC0 have suspend-resume; noted by xbw
3640     {
3641         if (mmc)
3642         {
3643             rk29_sdmmc_sdcard_resume(host);     
3644                 ret = mmc_resume_host(mmc);
3645         }
3646         }
3647
3648         return ret;
3649 }
3650 #else
3651 #define rk29_sdmmc_suspend      NULL
3652 #define rk29_sdmmc_resume       NULL
3653 #endif
3654
3655 static struct platform_driver rk29_sdmmc_driver = {
3656         .suspend    = rk29_sdmmc_suspend,
3657         .resume     = rk29_sdmmc_resume,
3658         .remove         = __exit_p(rk29_sdmmc_remove),
3659         .driver         = {
3660                 .name           = "rk29_sdmmc",
3661         },
3662 };
3663
3664 static int __init rk29_sdmmc_init(void)
3665 {
3666         return platform_driver_probe(&rk29_sdmmc_driver, rk29_sdmmc_probe);
3667 }
3668
3669 static void __exit rk29_sdmmc_exit(void)
3670 {
3671         platform_driver_unregister(&rk29_sdmmc_driver);
3672 }
3673
3674 module_init(rk29_sdmmc_init);
3675 module_exit(rk29_sdmmc_exit);
3676
3677 MODULE_DESCRIPTION("Rk29 Multimedia Card Interface driver");
3678 MODULE_AUTHOR("xbw@rock-chips.com");
3679 MODULE_LICENSE("GPL v2");
3680