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