update sdmmc driver: disable 'card removed wakeup'
[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  */
15  
16 #include <linux/blkdev.h>
17 #include <linux/clk.h>
18 #include <linux/debugfs.h>
19 #include <linux/device.h>
20 #include <linux/dma-mapping.h>
21 #include <linux/err.h>
22 #include <linux/init.h>
23 #include <linux/interrupt.h>
24 #include <linux/ioport.h>
25 #include <linux/module.h>
26 #include <linux/platform_device.h>
27 #include <linux/scatterlist.h>
28 #include <linux/seq_file.h>
29 #include <linux/stat.h>
30 #include <linux/delay.h>
31 #include <linux/irq.h>
32 #include <linux/mmc/host.h>
33 #include <linux/mmc/mmc.h>
34 #include <linux/mmc/card.h>
35
36 #include <mach/board.h>
37 #include <mach/rk29_iomap.h>
38 #include <mach/gpio.h>
39 #include <mach/iomux.h>
40
41 #include <asm/dma.h>
42 #include <mach/rk29-dma-pl330.h>
43 #include <asm/scatterlist.h>
44
45 #include "rk2818-sdmmc.h"
46
47 #define RK29_SDMMC_DATA_ERROR_FLAGS     (SDMMC_INT_DRTO | SDMMC_INT_DCRC | SDMMC_INT_HTO | SDMMC_INT_SBE | SDMMC_INT_EBE | SDMMC_INT_FRUN)
48 #define RK29_SDMMC_CMD_ERROR_FLAGS      (SDMMC_INT_RTO | SDMMC_INT_RCRC | SDMMC_INT_RE | SDMMC_INT_HLE)
49 #define RK29_SDMMC_ERROR_FLAGS          (RK29_SDMMC_DATA_ERROR_FLAGS | RK29_SDMMC_CMD_ERROR_FLAGS | SDMMC_INT_HLE)
50 #define RK29_SDMMC_SEND_STATUS          1
51 #define RK29_SDMMC_RECV_STATUS          2
52 #define RK29_SDMMC_DMA_THRESHOLD        1
53
54 enum {
55         EVENT_CMD_COMPLETE = 0,
56         EVENT_XFER_COMPLETE,
57         EVENT_DATA_COMPLETE,
58         EVENT_DATA_ERROR,
59         EVENT_XFER_ERROR
60 };
61
62 enum rk29_sdmmc_state {
63         STATE_IDLE = 0,
64         STATE_SENDING_CMD,
65         STATE_SENDING_DATA,
66         STATE_DATA_BUSY,
67         STATE_SENDING_STOP,
68         STATE_DATA_ERROR,
69 };
70
71 static struct rk29_dma_client rk29_dma_sdmmc0_client = {
72         .name = "rk29-dma-sdmmc0",
73 };
74
75 static struct rk29_dma_client rk29_dma_sdio1_client = {
76         .name = "rk29-dma-sdio1",
77 };
78
79 struct rk29_sdmmc {
80         spinlock_t              lock;
81         void __iomem            *regs;
82         struct clk              *clk;
83         struct scatterlist      *sg;
84         unsigned int            pio_offset;
85         struct mmc_request      *mrq;
86         struct mmc_request      *curr_mrq;
87         struct mmc_command      *cmd;
88         struct mmc_data         *data;
89         int                     dma_chn;
90         dma_addr_t              dma_addr;;
91         //dma_sg_ll_t           *sg_cpu;
92         unsigned int    use_dma:1;
93         char                    dma_name[8];
94         u32                     cmd_status;
95         u32                     data_status;
96         u32                     stop_cmdr;
97         u32                     dir_status;
98         struct tasklet_struct   tasklet;
99         unsigned long           pending_events;
100         unsigned long           completed_events;
101         enum rk29_sdmmc_state   state;
102         struct list_head        queue;
103         u32                     bus_hz;
104         u32                     current_speed;
105         struct platform_device  *pdev;
106         struct mmc_host         *mmc;
107         u32                     ctype;
108         struct list_head        queue_node;
109         unsigned int            clock;
110         unsigned long           flags;
111 #define RK29_SDMMC_CARD_PRESENT 0
112 #define RK29_SDMMC_CARD_NEED_INIT       1
113 #define RK29_SDMMC_SHUTDOWN             2
114         int                     id;
115         int                     irq;
116         struct timer_list       detect_timer;
117         unsigned int            oldstatus;
118
119         int gpio_irq;
120         int gpio_det;
121 };
122
123 #define rk29_sdmmc_test_and_clear_pending(host, event)          \
124         test_and_clear_bit(event, &host->pending_events)
125 #define rk29_sdmmc_set_completed(host, event)                   \
126         set_bit(event, &host->completed_events)
127
128 #define rk29_sdmmc_set_pending(host, event)                             \
129         set_bit(event, &host->pending_events)
130
131 static void rk29_sdmmc_write(unsigned char  __iomem     *regbase, unsigned int regOff,unsigned int val)
132 {
133         __raw_writel(val,regbase + regOff);
134 }
135
136 static unsigned int rk29_sdmmc_read(unsigned char  __iomem      *regbase, unsigned int regOff)
137 {
138         return __raw_readl(regbase + regOff);
139 }
140
141
142 #if defined (CONFIG_DEBUG_FS)
143 /*
144  * The debugfs stuff below is mostly optimized away when
145  * CONFIG_DEBUG_FS is not set.
146  */
147 static int rk29_sdmmc_req_show(struct seq_file *s, void *v)
148 {
149         struct rk29_sdmmc       *host = s->private;
150         struct mmc_request      *mrq;
151         struct mmc_command      *cmd;
152         struct mmc_command      *stop;
153         struct mmc_data         *data;
154
155         /* Make sure we get a consistent snapshot */
156         spin_lock(&host->lock);
157         mrq = host->mrq;
158
159         if (mrq) {
160                 cmd = mrq->cmd;
161                 data = mrq->data;
162                 stop = mrq->stop;
163
164                 if (cmd)
165                         seq_printf(s,
166                                 "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n",
167                                 cmd->opcode, cmd->arg, cmd->flags,
168                                 cmd->resp[0], cmd->resp[1], cmd->resp[2],
169                                 cmd->resp[2], cmd->error);
170                 if (data)
171                         seq_printf(s, "DATA %u / %u * %u flg %x err %d\n",
172                                 data->bytes_xfered, data->blocks,
173                                 data->blksz, data->flags, data->error);
174                 if (stop)
175                         seq_printf(s,
176                                 "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n",
177                                 stop->opcode, stop->arg, stop->flags,
178                                 stop->resp[0], stop->resp[1], stop->resp[2],
179                                 stop->resp[2], stop->error);
180         }
181
182         spin_unlock(&host->lock);
183
184         return 0;
185 }
186
187 static int rk29_sdmmc_req_open(struct inode *inode, struct file *file)
188 {
189         return single_open(file, rk29_sdmmc_req_show, inode->i_private);
190 }
191
192 static const struct file_operations rk29_sdmmc_req_fops = {
193         .owner          = THIS_MODULE,
194         .open           = rk29_sdmmc_req_open,
195         .read           = seq_read,
196         .llseek         = seq_lseek,
197         .release        = single_release,
198 };
199
200 static int rk29_sdmmc_regs_show(struct seq_file *s, void *v)
201 {
202         struct rk29_sdmmc       *host = s->private;
203
204         seq_printf(s, "SDMMC_CTRL:   \t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_CTRL));
205         seq_printf(s, "SDMMC_PWREN:  \t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_PWREN));
206         seq_printf(s, "SDMMC_CLKDIV: \t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_CLKDIV));
207         seq_printf(s, "SDMMC_CLKSRC: \t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_CLKSRC));
208         seq_printf(s, "SDMMC_CLKENA: \t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_CLKENA));
209         seq_printf(s, "SDMMC_TMOUT:  \t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_TMOUT));
210         seq_printf(s, "SDMMC_CTYPE:  \t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_CTYPE));
211         seq_printf(s, "SDMMC_BLKSIZ: \t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_BLKSIZ));
212         seq_printf(s, "SDMMC_BYTCNT: \t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_BYTCNT));
213         seq_printf(s, "SDMMC_INTMASK:\t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_INTMASK));
214         seq_printf(s, "SDMMC_CMDARG: \t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_CMDARG));
215         seq_printf(s, "SDMMC_CMD:    \t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_CMD));
216         seq_printf(s, "SDMMC_RESP0:  \t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_RESP0));
217         seq_printf(s, "SDMMC_RESP1:  \t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_RESP1));
218         seq_printf(s, "SDMMC_RESP2:  \t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_RESP2));
219         seq_printf(s, "SDMMC_RESP3:  \t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_RESP3));
220         seq_printf(s, "SDMMC_MINTSTS:\t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_MINTSTS));
221         seq_printf(s, "SDMMC_RINTSTS:\t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_RINTSTS));
222         seq_printf(s, "SDMMC_STATUS: \t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_STATUS));
223         seq_printf(s, "SDMMC_FIFOTH: \t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_FIFOTH));
224         seq_printf(s, "SDMMC_CDETECT:\t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_CDETECT));
225         seq_printf(s, "SDMMC_WRTPRT: \t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_WRTPRT));
226         seq_printf(s, "SDMMC_TCBCNT: \t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_TCBCNT));
227         seq_printf(s, "SDMMC_TBBCNT: \t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_TBBCNT));
228         seq_printf(s, "SDMMC_DEBNCE: \t0x%08x\n", rk29_sdmmc_read(host->regs, SDMMC_DEBNCE));
229
230         return 0;
231 }
232
233 static int rk29_sdmmc_regs_open(struct inode *inode, struct file *file)
234 {
235         return single_open(file, rk29_sdmmc_regs_show, inode->i_private);
236 }
237
238 static const struct file_operations rk29_sdmmc_regs_fops = {
239         .owner          = THIS_MODULE,
240         .open           = rk29_sdmmc_regs_open,
241         .read           = seq_read,
242         .llseek         = seq_lseek,
243         .release        = single_release,
244 };
245
246 static void rk29_sdmmc_init_debugfs(struct rk29_sdmmc *host)
247 {
248         struct mmc_host         *mmc = host->mmc;
249         struct dentry           *root;
250         struct dentry           *node;
251
252         root = mmc->debugfs_root;
253         if (!root)
254                 return;
255
256         node = debugfs_create_file("regs", S_IRUSR, root, host,
257                         &rk29_sdmmc_regs_fops);
258         if (IS_ERR(node))
259                 return;
260         if (!node)
261                 goto err;
262
263         node = debugfs_create_file("req", S_IRUSR, root, host, &rk29_sdmmc_req_fops);
264         if (!node)
265                 goto err;
266
267         node = debugfs_create_u32("state", S_IRUSR, root, (u32 *)&host->state);
268         if (!node)
269                 goto err;
270
271         node = debugfs_create_x32("pending_events", S_IRUSR, root,
272                                      (u32 *)&host->pending_events);
273         if (!node)
274                 goto err;
275
276         node = debugfs_create_x32("completed_events", S_IRUSR, root,
277                                      (u32 *)&host->completed_events);
278         if (!node)
279                 goto err;
280
281         return;
282
283 err:
284         dev_err(&mmc->class_dev, "failed to initialize debugfs for host\n");
285 }
286 #endif
287
288 static inline unsigned ns_to_clocks(unsigned clkrate, unsigned ns)
289 {
290         u32 clks;
291         if (clkrate > 1000000)
292                 clks =  (ns * (clkrate / 1000000) + 999) / 1000;
293         else
294                 clks =  ((ns/1000) * (clkrate / 1000) + 999) / 1000;
295
296         return clks;
297 }
298
299 static void rk29_sdmmc_set_timeout(struct rk29_sdmmc *host,struct mmc_data *data)
300 {
301         unsigned timeout;
302
303         timeout = ns_to_clocks(host->clock, data->timeout_ns) + data->timeout_clks;
304         rk29_sdmmc_write(host->regs, SDMMC_TMOUT, 0xffffffff);
305         ///rk29_sdmmc_write(host->regs, SDMMC_TMOUT, (timeout << 8) | (70));
306 }
307
308 static u32 rk29_sdmmc_prepare_command(struct mmc_host *mmc,
309                                  struct mmc_command *cmd)
310 {
311         struct mmc_data *data;
312         u32             cmdr;
313         
314         cmd->error = -EINPROGRESS;
315         cmdr = cmd->opcode;
316
317         if(cmdr == 12) 
318                 cmdr |= SDMMC_CMD_STOP;
319         else if(cmdr == 13) 
320                 cmdr &= ~SDMMC_CMD_PRV_DAT_WAIT;
321         else 
322                 cmdr |= SDMMC_CMD_PRV_DAT_WAIT;
323
324         if (cmd->flags & MMC_RSP_PRESENT) {
325                 cmdr |= SDMMC_CMD_RESP_EXP; // expect the respond, need to set this bit
326                 if (cmd->flags & MMC_RSP_136) 
327                         cmdr |= SDMMC_CMD_RESP_LONG; // expect long respond
328                 
329                 if(cmd->flags & MMC_RSP_CRC) 
330                         cmdr |= SDMMC_CMD_RESP_CRC;
331         }
332
333         data = cmd->data;
334         if (data) {
335                 cmdr |= SDMMC_CMD_DAT_EXP;
336                 if (data->flags & MMC_DATA_STREAM) 
337                         cmdr |= SDMMC_CMD_STRM_MODE; //  set stream mode
338                 if (data->flags & MMC_DATA_WRITE) 
339                     cmdr |= SDMMC_CMD_DAT_WR;
340         }
341         return cmdr;
342 }
343
344
345 static void rk29_sdmmc_start_command(struct rk29_sdmmc *host,
346                 struct mmc_command *cmd, u32 cmd_flags)
347 {
348         int tmo = 5000;
349         unsigned long flags;
350         host->cmd = cmd;
351         dev_vdbg(&host->pdev->dev,
352                         "start cmd:%d ARGR=0x%08x CMDR=0x%08x\n",
353                         cmd->opcode, cmd->arg, cmd_flags);
354         local_irq_save(flags);
355         rk29_sdmmc_write(host->regs, SDMMC_CMDARG, cmd->arg); // write to SDMMC_CMDARG register
356         rk29_sdmmc_write(host->regs, SDMMC_CMD, cmd_flags | SDMMC_CMD_START); // write to SDMMC_CMD register
357         local_irq_restore(flags);
358
359         /* wait until CIU accepts the command */
360         while (--tmo && (rk29_sdmmc_read(host->regs, SDMMC_CMD) & SDMMC_CMD_START)) 
361                 cpu_relax();
362         if(!tmo){
363                 dev_info(&host->pdev->dev, "Enter:%s %d start tmo err!!\n",__FUNCTION__,__LINE__);      
364         }
365 }
366
367 static void rk29_sdmmc_reset_fifo(struct rk29_sdmmc *host)
368 {
369         unsigned long flags;
370         //return;
371         dev_info(&host->pdev->dev, "reset fifo\n");
372         local_irq_save(flags);
373         rk29_sdmmc_write(host->regs, SDMMC_CTRL, rk29_sdmmc_read(host->regs, SDMMC_CTRL) | SDMMC_CTRL_FIFO_RESET);
374         /* wait till resets clear */
375         while (rk29_sdmmc_read(host->regs, SDMMC_CTRL) & SDMMC_CTRL_FIFO_RESET);
376         local_irq_restore(flags);
377 }
378
379 static int rk29_sdmmc_wait_unbusy(struct rk29_sdmmc *host)
380 {
381         const int time_out_us = 500000;
382         int time_out = time_out_us, time_out2 = 3;
383         //return 0;
384         while (rk29_sdmmc_read(host->regs, SDMMC_STATUS) & SDMMC_STAUTS_DATA_BUSY) {
385                 udelay(1);
386                 time_out--;
387                 if (!time_out) {
388                         time_out = time_out_us;
389                         rk29_sdmmc_reset_fifo(host);                    
390                         if (!time_out2)
391                                 break;
392                         time_out2--;
393                 }
394         }
395
396         return time_out_us - time_out;
397 }
398
399 static void send_stop_cmd(struct rk29_sdmmc *host, struct mmc_data *data)
400 {
401         rk29_sdmmc_wait_unbusy(host);
402
403         if(!(rk29_sdmmc_read(host->regs, SDMMC_STATUS) & SDMMC_STAUTS_FIFO_EMPTY)) {
404                 rk29_sdmmc_reset_fifo(host);
405         }
406         rk29_sdmmc_start_command(host, data->stop, host->stop_cmdr);
407 }
408
409 static void rk29_sdmmc_dma_cleanup(struct rk29_sdmmc *host)
410 {
411         struct mmc_data                 *data = host->data;
412         if (data) 
413                 dma_unmap_sg(&host->pdev->dev, data->sg, data->sg_len,
414                      ((data->flags & MMC_DATA_WRITE)
415                       ? DMA_TO_DEVICE : DMA_FROM_DEVICE));                      
416 }
417
418 static void rk29_sdmmc_stop_dma(struct rk29_sdmmc *host)
419 {
420         if(host->use_dma == 0)
421                 return;
422 #if 0
423         if (host->dma_chn > 0) {
424                 //dma_stop_channel(host->dma_chn);
425                 rk29_dma_ctrl(host->dma_chn,RK29_DMAOP_STOP);
426                 rk29_sdmmc_dma_cleanup(host);           
427         } else {
428                 /* Data transfer was stopped by the interrupt handler */
429                 rk29_sdmmc_set_pending(host, EVENT_XFER_COMPLETE);
430         }
431         #endif
432         rk29_sdmmc_set_pending(host, EVENT_XFER_COMPLETE);
433 }
434
435 /* This function is called by the DMA driver from tasklet context. */
436 static void rk29_sdmmc_dma_complete(void *arg, int size, enum rk29_dma_buffresult result)  ///(int chn, dma_irq_type_t type, void *arg)
437 {
438         struct rk29_sdmmc       *host = arg;
439         struct mmc_data         *data = host->data;
440
441         if(host->use_dma == 0)
442                 return;
443         dev_vdbg(&host->pdev->dev, "DMA complete\n");   
444         spin_lock(&host->lock);
445         rk29_sdmmc_dma_cleanup(host);
446         /*
447          * If the card was removed, data will be NULL. No point trying
448          * to send the stop command or waiting for NBUSY in this case.
449          */
450         if (data) {
451                 rk29_sdmmc_set_pending(host, EVENT_XFER_COMPLETE);
452                 tasklet_schedule(&host->tasklet);
453         }
454         spin_unlock(&host->lock);
455         if(result != RK29_RES_OK){
456                 rk29_dma_ctrl(host->dma_chn,RK29_DMAOP_STOP);
457                 rk29_dma_ctrl(host->dma_chn,RK29_DMAOP_FLUSH);
458                 rk29_sdmmc_write(host->regs, SDMMC_CTRL, (rk29_sdmmc_read(host->regs, SDMMC_CTRL))&(~SDMMC_CTRL_DMA_ENABLE));
459                 printk(KERN_DEBUG "%s: sdio dma complete err\n",__FUNCTION__);
460         }
461 }
462
463 static int rk29_sdmmc_submit_data_dma(struct rk29_sdmmc *host, struct mmc_data *data)
464 {
465         struct scatterlist              *sg;
466         unsigned int                    i,direction;
467         int dma_len=0;
468         
469         if(host->use_dma == 0)
470                 return -ENOSYS;
471         /* If we don't have a channel, we can't do DMA */
472         if (host->dma_chn < 0)
473                 return -ENODEV;
474
475         /*
476          * We don't do DMA on "complex" transfers, i.e. with
477          * non-word-aligned buffers or lengths. Also, we don't bother
478          * with all the DMA setup overhead for short transfers.
479          */
480         if (data->blocks * data->blksz < RK29_SDMMC_DMA_THRESHOLD)
481         {
482                 rk29_sdmmc_write(host->regs, SDMMC_INTMASK,rk29_sdmmc_read(host->regs,SDMMC_INTMASK) | SDMMC_INT_TXDR | SDMMC_INT_RXDR );       
483                 return -EINVAL;
484         }
485         else
486         {
487                 rk29_sdmmc_write(host->regs, SDMMC_INTMASK,rk29_sdmmc_read(host->regs,SDMMC_INTMASK) & (~( SDMMC_INT_TXDR | SDMMC_INT_RXDR)));
488         }
489         if (data->blksz & 3)
490                 return -EINVAL;
491         for_each_sg(data->sg, sg, data->sg_len, i) {
492                 if (sg->offset & 3 || sg->length & 3)
493                         return -EINVAL;
494         }
495         if (data->flags & MMC_DATA_READ)
496                 direction = RK29_DMASRC_HW;  
497         else
498                 direction = RK29_DMASRC_MEM;  
499         if(rk29_sdmmc_read(host->regs, SDMMC_STATUS) & SDMMC_STAUTS_FIFO_FULL ) {
500                 rk29_sdmmc_reset_fifo(host);
501                 dev_info(&host->pdev->dev, "%s %d fifo full reset\n",__FUNCTION__,__LINE__);
502         }       
503         rk29_dma_ctrl(host->dma_chn,RK29_DMAOP_STOP);
504         rk29_dma_ctrl(host->dma_chn,RK29_DMAOP_FLUSH);                                          
505     rk29_dma_devconfig(host->dma_chn, direction, (unsigned long )(host->dma_addr));
506         dma_len = dma_map_sg(&host->pdev->dev, data->sg, data->sg_len, 
507                         (data->flags & MMC_DATA_READ)? DMA_FROM_DEVICE : DMA_TO_DEVICE);                                                                           
508         for (i = 0; i < dma_len; i++)                              
509         rk29_dma_enqueue(host->dma_chn, host, sg_dma_address(&data->sg[i]),sg_dma_len(&data->sg[i]));  // data->sg->dma_address, data->sg->length);             
510         rk29_sdmmc_write(host->regs, SDMMC_CTRL, (rk29_sdmmc_read(host->regs, SDMMC_CTRL))|SDMMC_CTRL_DMA_ENABLE);// enable dma
511         rk29_dma_ctrl(host->dma_chn, RK29_DMAOP_START); 
512         return 0;
513 }
514
515 static void rk29_sdmmc_submit_data(struct rk29_sdmmc *host, struct mmc_data *data)
516 {
517         data->error = -EINPROGRESS;
518
519         WARN_ON(host->data);
520         host->sg = NULL;
521         host->data = data;
522
523         if (rk29_sdmmc_submit_data_dma(host, data)) {
524                 host->sg = data->sg;
525                 host->pio_offset = 0;
526                 if (data->flags & MMC_DATA_READ)
527                         host->dir_status = RK29_SDMMC_RECV_STATUS;
528                 else 
529                         host->dir_status = RK29_SDMMC_SEND_STATUS;
530
531                 rk29_sdmmc_write(host->regs, SDMMC_CTRL, (rk29_sdmmc_read(host->regs, SDMMC_CTRL))&(~SDMMC_CTRL_DMA_ENABLE));
532         }
533
534 }
535
536 static void sdmmc_send_cmd(struct rk29_sdmmc *host, unsigned int cmd, int arg)
537 {
538         int tmo = 10000;
539         
540         rk29_sdmmc_write(host->regs, SDMMC_CMDARG, arg);
541         rk29_sdmmc_write(host->regs, SDMMC_CMD, SDMMC_CMD_START | cmd);         
542         while (readl(host->regs + SDMMC_CMD) & SDMMC_CMD_START){
543                 tmo--;
544                 if(!tmo) {
545                         tmo = 10000;
546                         dev_info(&host->pdev->dev, "set cmd register timeout error,status = 0x%08x!!!\n",
547                                 rk29_sdmmc_read(host->regs, SDMMC_STATUS));
548                 }
549         }
550 }
551
552 void rk29_sdmmc_setup_bus(struct rk29_sdmmc *host)
553 {
554         u32 div;
555
556         if (host->clock != host->current_speed && host->clock != 0) {
557
558
559                 while(rk29_sdmmc_read(host->regs, SDMMC_STATUS) & (SDMMC_STAUTS_MC_BUSY|SDMMC_STAUTS_DATA_BUSY)){
560                 /* reset all blocks */
561                         rk29_sdmmc_write(host->regs, SDMMC_CTRL, (SDMMC_CTRL_RESET | SDMMC_CTRL_FIFO_RESET | SDMMC_CTRL_DMA_RESET));
562                         /* wait till resets clear */
563                         dev_info(&host->pdev->dev, "sdmmc_send cmd mci busy, ctrl reset ");
564                         while (rk29_sdmmc_read(host->regs, SDMMC_CTRL) & (SDMMC_CTRL_RESET | SDMMC_CTRL_FIFO_RESET | SDMMC_CTRL_DMA_RESET));
565                         printk("done\n");
566                         rk29_sdmmc_write(host->regs, SDMMC_CTRL, rk29_sdmmc_read(host->regs, SDMMC_CTRL) | SDMMC_CTRL_INT_ENABLE);
567                         if(rk29_sdmmc_read(host->regs, SDMMC_STATUS) & SDMMC_STAUTS_FIFO_FULL){
568                                 dev_info(&host->pdev->dev, "fifo is full\n");
569                                 rk29_sdmmc_read(host->regs, SDMMC_DATA);
570                                 rk29_sdmmc_read(host->regs, SDMMC_DATA);
571                         }
572                 }
573                 //rk29_sdmmc_write(host->regs, SDMMC_INTMASK,0);
574                 div  = (((host->bus_hz + (host->bus_hz / 5)) / host->clock)) >> 1;
575                 if(!div)
576                         div = 1;
577                 /* store the actual clock for calculations */
578                 host->clock = (host->bus_hz / div) >> 1;
579                 /* disable clock */
580                 rk29_sdmmc_write(host->regs, SDMMC_CLKENA, 0);
581                 rk29_sdmmc_write(host->regs, SDMMC_CLKSRC,0);
582                 /* inform CIU */
583                 sdmmc_send_cmd(host, SDMMC_CMD_UPD_CLK | SDMMC_CMD_PRV_DAT_WAIT, 0);
584                 /* set clock to desired speed */
585                 rk29_sdmmc_write(host->regs, SDMMC_CLKDIV, div);
586                 /* inform CIU */
587                 sdmmc_send_cmd(host, SDMMC_CMD_UPD_CLK | SDMMC_CMD_PRV_DAT_WAIT, 0);
588                 /* enable clock */
589                 rk29_sdmmc_write(host->regs, SDMMC_CLKENA, SDMMC_CLKEN_ENABLE);
590                 /* inform CIU */
591                 sdmmc_send_cmd(host, SDMMC_CMD_UPD_CLK | SDMMC_CMD_PRV_DAT_WAIT, 0);
592
593                 host->current_speed = host->clock;
594         }
595
596         /* Set the current  bus width */
597         rk29_sdmmc_write(host->regs, SDMMC_CTYPE, host->ctype);
598 }
599
600 static void rk29_sdmmc_start_request(struct rk29_sdmmc *host)
601 {
602         struct mmc_request      *mrq;
603         struct mmc_command      *cmd;
604         struct mmc_data         *data;
605         u32                     cmdflags;
606         
607         mrq = host->mrq;
608
609         rk29_sdmmc_wait_unbusy(host);
610
611         if(!(rk29_sdmmc_read(host->regs, SDMMC_STATUS) & SDMMC_STAUTS_FIFO_EMPTY)) {
612                 rk29_sdmmc_reset_fifo(host);
613         }
614         /* Slot specific timing and width adjustment */
615         rk29_sdmmc_setup_bus(host);
616         rk29_sdmmc_write(host->regs, SDMMC_RINTSTS, ~SDMMC_INT_SDIO);
617         if(rk29_sdmmc_read(host->regs, SDMMC_INTMASK) & SDMMC_INT_SDIO)
618                 rk29_sdmmc_write(host->regs, SDMMC_INTMASK,SDMMC_INT_SDIO |SDMMC_INT_CMD_DONE | SDMMC_INT_DTO | RK29_SDMMC_ERROR_FLAGS | SDMMC_INT_CD);
619         else
620                 rk29_sdmmc_write(host->regs, SDMMC_INTMASK,SDMMC_INT_CMD_DONE | SDMMC_INT_DTO | RK29_SDMMC_ERROR_FLAGS | SDMMC_INT_CD);
621         host->curr_mrq = mrq;
622         host->pending_events = 0;
623         host->completed_events = 0;
624         host->data_status = 0;
625         data = mrq->data;
626         if (data) {
627                 rk29_sdmmc_set_timeout(host,data);
628                 rk29_sdmmc_write(host->regs, SDMMC_BYTCNT,data->blksz*data->blocks);
629                 rk29_sdmmc_write(host->regs, SDMMC_BLKSIZ,data->blksz);
630         }
631         cmd = mrq->cmd;
632         cmdflags = rk29_sdmmc_prepare_command(host->mmc, cmd);
633         if (unlikely(test_and_clear_bit(RK29_SDMMC_CARD_NEED_INIT, &host->flags))) 
634             cmdflags |= SDMMC_CMD_INIT; //this is the first command, let set send the initializtion clock
635         
636         if (data) //we may need to move this code to mci_start_command
637                 rk29_sdmmc_submit_data(host, data);
638         
639         rk29_sdmmc_start_command(host, cmd, cmdflags);
640
641         if (mrq->stop) 
642                 host->stop_cmdr = rk29_sdmmc_prepare_command(host->mmc, mrq->stop);
643         
644 }
645
646 static void rk29_sdmmc_queue_request(struct rk29_sdmmc *host,struct mmc_request *mrq)
647 {
648         spin_lock(&host->lock);
649         host->mrq = mrq;
650         if (host->state == STATE_IDLE) {
651                 host->state = STATE_SENDING_CMD;
652                 rk29_sdmmc_start_request(host);
653         } else {
654                 dev_info(&host->pdev->dev, "list add tail\n");
655                 list_add_tail(&host->queue_node, &host->queue);
656         }
657         spin_unlock(&host->lock);
658 }
659  
660 static void rk29_sdmmc_request(struct mmc_host *mmc, struct mmc_request *mrq)
661 {
662         struct rk29_sdmmc *host = mmc_priv(mmc);
663
664         WARN_ON(host->mrq);
665         
666         if (!test_bit(RK29_SDMMC_CARD_PRESENT, &host->flags)) {
667                 mrq->cmd->error = -ENOMEDIUM;
668                 mmc_request_done(mmc, mrq);
669                 return;
670         }
671         clk_enable(host->clk);
672     rk29_sdmmc_write(host->regs, SDMMC_CLKENA, 1);
673         rk29_sdmmc_queue_request(host,mrq);
674 }
675
676 static void rk29_sdmmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
677 {
678         struct rk29_sdmmc *host = mmc_priv(mmc);;
679
680         host->ctype = 0; // set default 1 bit mode
681
682         switch (ios->bus_width) {
683         case MMC_BUS_WIDTH_1:
684                 host->ctype = 0;
685                 break;
686         case MMC_BUS_WIDTH_4:
687                 host->ctype = SDMMC_CTYPE_4BIT;
688                 break;
689         }
690         if (ios->clock) {
691                 spin_lock(&host->lock);
692                 /*
693                  * Use mirror of ios->clock to prevent race with mmc
694                  * core ios update when finding the minimum.
695                  */
696                 host->clock = ios->clock;
697
698                 spin_unlock(&host->lock);
699         } else {
700                 spin_lock(&host->lock);
701                 host->clock = 0;
702                 spin_unlock(&host->lock);
703         }
704         switch (ios->power_mode) {
705         case MMC_POWER_UP:
706                 set_bit(RK29_SDMMC_CARD_NEED_INIT, &host->flags);
707                 rk29_sdmmc_write(host->regs, SDMMC_PWREN, 1);
708                 break;
709         default:
710                 break;
711         }
712 }
713
714 static int rk29_sdmmc_get_ro(struct mmc_host *mmc)
715 {
716         struct rk29_sdmmc *host = mmc_priv(mmc);
717         u32 wrtprt = rk29_sdmmc_read(host->regs, SDMMC_WRTPRT);
718
719         return (wrtprt & SDMMC_WRITE_PROTECT)?1:0;
720 }
721
722
723 static int rk29_sdmmc_get_cd(struct mmc_host *mmc)
724 {
725         struct rk29_sdmmc *host = mmc_priv(mmc);
726         u32 cdetect;
727
728         if(host->gpio_det == INVALID_GPIO)
729                 return 1;
730         
731         cdetect = rk29_sdmmc_read(host->regs, SDMMC_CDETECT);
732         return (cdetect & SDMMC_CARD_DETECT_N)?0:1;
733 }
734
735 static void rk29_sdmmc_enable_sdio_irq(struct mmc_host *mmc, int enable)
736 {
737         u32 intmask;
738         unsigned long flags;
739         struct rk29_sdmmc *host = mmc_priv(mmc);
740         
741         spin_lock_irqsave(&host->lock, flags);
742         intmask = rk29_sdmmc_read(host->regs, SDMMC_INTMASK);
743         if(enable)
744                 rk29_sdmmc_write(host->regs, SDMMC_INTMASK, intmask | SDMMC_INT_SDIO);
745         else
746                 rk29_sdmmc_write(host->regs, SDMMC_INTMASK, intmask & ~SDMMC_INT_SDIO);
747         spin_unlock_irqrestore(&host->lock, flags);
748 }
749
750 static void  rk29_sdmmc_init_card(struct mmc_host *mmc, struct mmc_card *card)
751 {
752         card->quirks = MMC_QUIRK_BLKSZ_FOR_BYTE_MODE;
753
754 }
755 static const struct mmc_host_ops rk29_sdmmc_ops[] = {
756         {
757                 .request        = rk29_sdmmc_request,
758                 .set_ios        = rk29_sdmmc_set_ios,
759                 .get_ro         = rk29_sdmmc_get_ro,
760                 .get_cd         = rk29_sdmmc_get_cd,
761         },
762         {
763                 .request        = rk29_sdmmc_request,
764                 .set_ios        = rk29_sdmmc_set_ios,
765                 .enable_sdio_irq = rk29_sdmmc_enable_sdio_irq,
766                 .init_card       = rk29_sdmmc_init_card,
767         },
768 };
769
770 static void rk29_sdmmc_request_end(struct rk29_sdmmc *host, struct mmc_request *mrq)
771         __releases(&host->lock)
772         __acquires(&host->lock)
773 {
774         struct mmc_host         *prev_mmc = host->mmc;
775         
776         //WARN_ON(host->cmd || host->data);
777         host->curr_mrq = NULL;
778         host->mrq = NULL;
779
780         rk29_sdmmc_wait_unbusy(host);
781
782         if(!(rk29_sdmmc_read(host->regs, SDMMC_STATUS) & SDMMC_STAUTS_FIFO_EMPTY)) {
783                 rk29_sdmmc_reset_fifo(host);
784         }
785         if (!list_empty(&host->queue)) {
786                 dev_info(&host->pdev->dev, "queque list is not empty\n");
787                 host = list_entry(host->queue.next,
788                                 struct rk29_sdmmc, queue_node);
789                 list_del(&host->queue_node);
790                 host->state = STATE_SENDING_CMD;
791                 rk29_sdmmc_start_request(host);
792         } else {
793                 dev_vdbg(&host->pdev->dev, "list empty\n");
794                 host->state = STATE_IDLE;
795         }
796
797         spin_unlock(&host->lock);
798         if(rk29_sdmmc_read(host->regs, SDMMC_INTMASK) & SDMMC_INT_SDIO)
799                 rk29_sdmmc_write(host->regs, SDMMC_INTMASK,SDMMC_INT_CD|SDMMC_INT_SDIO);
800         else
801                 rk29_sdmmc_write(host->regs, SDMMC_INTMASK,SDMMC_INT_CD);
802         if(mrq && mrq->data && mrq->data->error) {
803                 //mrq->data->bytes_xfered = 0;
804                 //rk29_sdmmc_write(host->regs, SDMMC_CMD, host->stop_cmdr | SDMMC_CMD_START); 
805                 dev_info(&host->pdev->dev, "data error, request done!\n");
806         }
807         rk29_sdmmc_write(host->regs, SDMMC_CLKENA, 0);
808         clk_disable(host->clk);
809         mmc_request_done(prev_mmc, mrq);
810
811         spin_lock(&host->lock);
812 }
813
814 static void rk29_sdmmc_command_complete(struct rk29_sdmmc *host,
815                         struct mmc_command *cmd)
816 {
817         u32             status = host->cmd_status;
818
819         host->cmd_status = 0;
820
821         if(cmd->flags & MMC_RSP_PRESENT) {
822
823             if(cmd->flags & MMC_RSP_136) {
824
825                 /* Read the response from the card (up to 16 bytes).
826                  * RK29 SDMMC controller saves bits 127-96 in SDMMC_RESP3
827                  * for easy parsing. But the UNSTUFF_BITS macro in core/mmc.c
828                  * core/sd.c expect those bits be in resp[0]. Hence
829                  * reverse the response word order.
830                  */
831                 cmd->resp[3] = rk29_sdmmc_read(host->regs, SDMMC_RESP0);
832                 cmd->resp[2] = rk29_sdmmc_read(host->regs, SDMMC_RESP1);
833                 cmd->resp[1] = rk29_sdmmc_read(host->regs, SDMMC_RESP2);
834                 cmd->resp[0] = rk29_sdmmc_read(host->regs, SDMMC_RESP3);
835             } else {
836                 cmd->resp[0] = rk29_sdmmc_read(host->regs, SDMMC_RESP0);
837                         cmd->resp[1] = 0;
838                         cmd->resp[2] = 0;
839                         cmd->resp[3] = 0;
840             }
841         }
842
843         if (status & SDMMC_INT_RTO)
844                 cmd->error = -ETIMEDOUT;
845         else if ((cmd->flags & MMC_RSP_CRC) && (status & SDMMC_INT_RCRC))
846                 cmd->error = -EILSEQ;
847         else if (status & SDMMC_INT_RE)
848                 cmd->error = -EIO;
849         else if(status & SDMMC_INT_HLE)
850                 cmd->error = -EIO;
851         else
852                 cmd->error = 0;
853
854         if (cmd->error) {
855                 dev_vdbg(&host->pdev->dev,
856                         "command error: status=0x%08x resp=0x%08x\n"
857                         "cmd=0x%08x arg=0x%08x flg=0x%08x err=%d\n", 
858                         status, cmd->resp[0], 
859                         cmd->opcode, cmd->arg, cmd->flags, cmd->error);
860
861                 if (cmd->data) {
862                         host->data = NULL;
863                         rk29_sdmmc_stop_dma(host);
864                 }
865         } 
866 }
867
868 static void rk29_sdmmc_tasklet_func(unsigned long priv)
869 {
870         struct rk29_sdmmc       *host = (struct rk29_sdmmc *)priv;
871         struct mmc_request      *mrq = host->curr_mrq;
872         struct mmc_data         *data = host->data;
873         struct mmc_command      *cmd = host->cmd;
874         enum rk29_sdmmc_state   state = host->state;
875         enum rk29_sdmmc_state   prev_state;
876         u32                     status;
877
878         spin_lock(&host->lock);
879
880         state = host->state;
881
882         do {
883                 prev_state = state;
884
885                 switch (state) {
886                 case STATE_IDLE:
887                         break;
888
889                 case STATE_SENDING_CMD:                 
890                         if (!rk29_sdmmc_test_and_clear_pending(host,
891                                                 EVENT_CMD_COMPLETE))
892                                 break;
893
894                         host->cmd = NULL;
895                         rk29_sdmmc_set_completed(host, EVENT_CMD_COMPLETE);
896                         if(mrq) 
897                         {
898                                 rk29_sdmmc_command_complete(host, mrq->cmd);
899                                 if (!mrq->data || (cmd && cmd->error)) {
900                                         rk29_sdmmc_request_end(host, host->curr_mrq);
901                                         goto unlock;
902                                 }
903                         }
904                         prev_state = state = STATE_SENDING_DATA;
905                         /* fall through */
906
907                 case STATE_SENDING_DATA:
908                         if (rk29_sdmmc_test_and_clear_pending(host,
909                                                 EVENT_DATA_ERROR)) {
910                                 rk29_sdmmc_stop_dma(host);
911                                 if (data && data->stop)
912                                         send_stop_cmd(host, data);
913                                 state = STATE_DATA_ERROR;
914                                 break;
915                         }
916
917                         if (!rk29_sdmmc_test_and_clear_pending(host,
918                                                 EVENT_XFER_COMPLETE))
919                                 break;
920
921                         rk29_sdmmc_set_completed(host, EVENT_XFER_COMPLETE);
922                         prev_state = state = STATE_DATA_BUSY;
923                         /* fall through */
924
925                 case STATE_DATA_BUSY:
926                         if (!rk29_sdmmc_test_and_clear_pending(host,
927                                                 EVENT_DATA_COMPLETE))
928                                 break;  
929                         host->data = NULL;
930                         rk29_sdmmc_write(host->regs, SDMMC_CTRL, (rk29_sdmmc_read(host->regs, SDMMC_CTRL))&(~SDMMC_CTRL_DMA_ENABLE));
931                         
932                         rk29_sdmmc_set_completed(host, EVENT_DATA_COMPLETE);
933                         status = host->data_status;
934                         if(data) {
935                         if (unlikely(status & RK29_SDMMC_DATA_ERROR_FLAGS)) {
936                                 if (status & SDMMC_INT_DRTO) {
937                                         dev_err(&host->pdev->dev,
938                                                         "data timeout error\n");
939                                         data->error = -ETIMEDOUT;
940                                 } else if (status & SDMMC_INT_DCRC) {
941                                         dev_err(&host->pdev->dev,
942                                                         "data CRC error\n");
943                                         data->error = -EILSEQ;
944                                 } else {
945                                         dev_err(&host->pdev->dev,
946                                                 "data FIFO error (status=%08x)\n",
947                                                 status);
948                                         data->error = -EIO;
949                                 }
950                         }else {
951                                 data->bytes_xfered = data->blocks * data->blksz;
952                                 data->error = 0;
953                         }
954                         }
955                         if (!data->stop && host->curr_mrq) {
956                                 rk29_sdmmc_request_end(host, host->curr_mrq);
957                                 goto unlock;
958                         }
959
960                         prev_state = state = STATE_SENDING_STOP;
961                         if (data && !data->error)
962                                 send_stop_cmd(host, data);
963                         /* fall through */
964
965                 case STATE_SENDING_STOP:
966                         if (!rk29_sdmmc_test_and_clear_pending(host,
967                                                 EVENT_CMD_COMPLETE))
968                                 break;
969
970                         host->cmd = NULL;
971                         if(mrq)
972                                 rk29_sdmmc_command_complete(host, mrq->stop);
973                         if(host->curr_mrq)
974                                 rk29_sdmmc_request_end(host, host->curr_mrq);
975                         goto unlock;
976                 case STATE_DATA_ERROR:
977                         if (!rk29_sdmmc_test_and_clear_pending(host,
978                                                 EVENT_XFER_COMPLETE))
979                                 break;
980
981                         state = STATE_DATA_BUSY;
982                         break;
983                 }
984         } while (state != prev_state);
985
986         host->state = state;
987
988 unlock:
989         spin_unlock(&host->lock);
990
991 }
992
993
994
995 inline static void rk29_sdmmc_push_data(struct rk29_sdmmc *host, void *buf,int cnt)
996 {
997     u32* pData = (u32*)buf;
998
999     if (cnt % 4 != 0) 
1000                 cnt = (cnt>>2) +1;
1001         else
1002         cnt = cnt >> 2;
1003     while (cnt > 0) {
1004         rk29_sdmmc_write(host->regs, SDMMC_DATA,*pData++);
1005         cnt--;
1006     }
1007 }
1008
1009 inline static void rk29_sdmmc_pull_data(struct rk29_sdmmc *host,void *buf,int cnt)
1010 {
1011     u32* pData = (u32*)buf;
1012     
1013     if (cnt % 4 != 0) 
1014                 cnt = (cnt>>2) +1;
1015         else
1016         cnt = cnt >> 2;
1017     while (cnt > 0) {       
1018         *pData++ = rk29_sdmmc_read(host->regs, SDMMC_DATA);
1019         cnt--;
1020     }
1021 }
1022
1023 static void rk29_sdmmc_read_data_pio(struct rk29_sdmmc *host)
1024 {
1025         struct scatterlist      *sg = host->sg;
1026         void                    *buf = sg_virt(sg);
1027         unsigned int            offset = host->pio_offset;
1028         struct mmc_data         *data = host->data;
1029         u32                     status;
1030         unsigned int            nbytes = 0,len,old_len,count =0;
1031
1032         do {
1033                 len = SDMMC_GET_FCNT(rk29_sdmmc_read(host->regs, SDMMC_STATUS)) << 2;
1034                 if(count == 0) 
1035                         old_len = len;
1036                 if (likely(offset + len <= sg->length)) {
1037                         rk29_sdmmc_pull_data(host, (void *)(buf + offset),len);
1038                         offset += len;
1039                         nbytes += len;
1040                         if (offset == sg->length) {
1041                                 flush_dcache_page(sg_page(sg));
1042                                 host->sg = sg = sg_next(sg);
1043                                 if (!sg)
1044                                         goto done;
1045                                 offset = 0;
1046                                 buf = sg_virt(sg);
1047                         }
1048                 } else {
1049                         unsigned int remaining = sg->length - offset;
1050                         rk29_sdmmc_pull_data(host, (void *)(buf + offset),remaining);
1051                         nbytes += remaining;
1052
1053                         flush_dcache_page(sg_page(sg));
1054                         host->sg = sg = sg_next(sg);
1055                         if (!sg)
1056                                 goto done;
1057                         offset = len - remaining;
1058                         buf = sg_virt(sg);
1059                         rk29_sdmmc_pull_data(host, buf,offset);
1060                         nbytes += offset;
1061                 }
1062
1063                 status = rk29_sdmmc_read(host->regs, SDMMC_MINTSTS);
1064                 rk29_sdmmc_write(host->regs, SDMMC_RINTSTS,SDMMC_INT_RXDR); // clear RXDR interrupt
1065                 if (status & RK29_SDMMC_DATA_ERROR_FLAGS) {
1066                         host->data_status = status;
1067                         if(data)
1068                                 data->bytes_xfered += nbytes;
1069                         else
1070                                 dev_info(&host->pdev->dev, "%s %d host data NULL err",__FUNCTION__,__LINE__);
1071                         smp_wmb();
1072                         rk29_sdmmc_set_pending(host, EVENT_DATA_ERROR);
1073                         tasklet_schedule(&host->tasklet);
1074                         return;
1075                 }
1076                 count ++;
1077         } while (status & SDMMC_INT_RXDR); // if the RXDR is ready let read again
1078         len = SDMMC_GET_FCNT(rk29_sdmmc_read(host->regs, SDMMC_STATUS));
1079         host->pio_offset = offset;
1080         if(data)
1081                 data->bytes_xfered += nbytes;
1082         else
1083                 dev_info(&host->pdev->dev, "%s %d host data NULL err",__FUNCTION__,__LINE__);   
1084         return;
1085
1086 done:
1087         if(data)
1088                 data->bytes_xfered += nbytes;
1089         else
1090                 dev_info(&host->pdev->dev, "%s %d host data NULL err",__FUNCTION__,__LINE__);   
1091         smp_wmb();
1092         rk29_sdmmc_set_pending(host, EVENT_XFER_COMPLETE);
1093 }
1094
1095 static void rk29_sdmmc_write_data_pio(struct rk29_sdmmc *host)
1096 {
1097         struct scatterlist      *sg = host->sg;
1098         void                    *buf = sg_virt(sg);
1099         unsigned int            offset = host->pio_offset;
1100         struct mmc_data         *data = host->data;
1101         u32                     status;
1102         unsigned int            nbytes = 0,len;
1103
1104         do {
1105
1106                 len = SDMMC_FIFO_SZ - (SDMMC_GET_FCNT(rk29_sdmmc_read(host->regs, SDMMC_STATUS)) << 2);
1107                 if (likely(offset + len <= sg->length)) {
1108                         rk29_sdmmc_push_data(host, (void *)(buf + offset),len);
1109
1110                         offset += len;
1111                         nbytes += len;
1112                         if (offset == sg->length) {
1113                                 host->sg = sg = sg_next(sg);
1114                                 if (!sg)
1115                                         goto done;
1116
1117                                 offset = 0;
1118                                 buf = sg_virt(sg);
1119                         }
1120                 } else {
1121                         unsigned int remaining = sg->length - offset;
1122                         rk29_sdmmc_push_data(host, (void *)(buf + offset), remaining);
1123                         nbytes += remaining;
1124
1125                         host->sg = sg = sg_next(sg);
1126                         if (!sg) {
1127                                 goto done;
1128                         }
1129
1130                         offset = len - remaining;
1131                         buf = sg_virt(sg);
1132                         rk29_sdmmc_push_data(host, (void *)buf, offset);
1133                         nbytes += offset;
1134                 }
1135
1136                 status = rk29_sdmmc_read(host->regs, SDMMC_MINTSTS);
1137                 rk29_sdmmc_write(host->regs, SDMMC_RINTSTS,SDMMC_INT_TXDR); // clear RXDR interrupt
1138                 if (status & RK29_SDMMC_DATA_ERROR_FLAGS) {
1139                         host->data_status = status;
1140                         if(data)
1141                                 data->bytes_xfered += nbytes;
1142                         else
1143                                 dev_info(&host->pdev->dev, "%s %d host data NULL err",__FUNCTION__,__LINE__);   
1144                         smp_wmb();
1145                         rk29_sdmmc_set_pending(host, EVENT_DATA_ERROR);
1146                         tasklet_schedule(&host->tasklet);
1147                         return;
1148                 }
1149         } while (status & SDMMC_INT_TXDR); // if TXDR, let write again
1150
1151         host->pio_offset = offset;
1152         if(data)
1153                 data->bytes_xfered += nbytes;
1154         else
1155                 dev_info(&host->pdev->dev, "%s %d host data NULL err",__FUNCTION__,__LINE__);   
1156         return;
1157
1158 done:
1159         if(data)
1160                 data->bytes_xfered += nbytes;
1161         else
1162                 dev_info(&host->pdev->dev, "%s %d host data NULL err",__FUNCTION__,__LINE__);   
1163         smp_wmb();
1164         rk29_sdmmc_set_pending(host, EVENT_XFER_COMPLETE);
1165 }
1166
1167 static void rk29_sdmmc_cmd_interrupt(struct rk29_sdmmc *host, u32 status)
1168 {
1169         if(!host->cmd_status) 
1170                 host->cmd_status = status;
1171
1172         smp_wmb();
1173         rk29_sdmmc_set_pending(host, EVENT_CMD_COMPLETE);
1174         tasklet_schedule(&host->tasklet);
1175 }
1176
1177 #if 0
1178 static int rk29_sdmmc1_card_get_cd(struct mmc_host *mmc)
1179 {
1180         struct rk29_sdmmc *host = mmc_priv(mmc);
1181         struct rk29_sdmmc_platform_data *pdata = host->pdev->dev.platform_data;
1182         return gpio_get_value(pdata->detect_irq);
1183 }
1184
1185 static int rk29_sdmmc1_card_change_cd_trigger_type(struct mmc_host *mmc, unsigned int type)
1186 {
1187        struct rk29_sdmmc *host = mmc_priv(mmc);
1188        struct rk29_sdmmc_platform_data *pdata = host->pdev->dev.platform_data;
1189        return set_irq_type(gpio_to_irq(pdata->detect_irq), type);
1190 }
1191
1192
1193 static irqreturn_t rk29_sdmmc1_card_detect_interrupt(int irq, void *dev_id)
1194 {
1195        struct rk29_sdmmc *host = dev_id;
1196        bool present, present_old; 
1197        
1198        present = rk29_sdmmc1_card_get_cd(host->mmc);
1199        present_old = test_bit(RK29_SDMMC_CARD_PRESENT, &host->flags);
1200        if (present != present_old) {
1201              if (present != 0) {
1202                   set_bit(RK29_SDMMC_CARD_PRESENT, &host->flags);
1203              } else {
1204                   clear_bit(RK29_SDMMC_CARD_PRESENT, &host->flags);
1205              }
1206              mod_timer(&host->detect_timer, jiffies + msecs_to_jiffies(200));
1207        }
1208        rk29_sdmmc1_card_change_cd_trigger_type(host->mmc, (present ? IRQF_TRIGGER_FALLING: IRQF_TRIGGER_RISING)); 
1209    
1210        return IRQ_HANDLED;
1211
1212 }
1213 #endif
1214
1215 static irqreturn_t rk29_sdmmc_interrupt(int irq, void *dev_id)
1216 {
1217         struct rk29_sdmmc       *host = dev_id;
1218         u32                     status,  pending, mask;
1219 //      unsigned int            pass_count = 0;
1220         bool present;
1221         bool present_old;
1222
1223         spin_lock(&host->lock);
1224 //      do {
1225                 status = rk29_sdmmc_read(host->regs, SDMMC_RINTSTS);
1226                 mask = rk29_sdmmc_read(host->regs, SDMMC_MINTSTS);// read only mask reg
1227                 pending = status & mask;
1228                 
1229 //              if (!pending)
1230 //                      break;  
1231                 if(pending & SDMMC_INT_CD) {
1232                     rk29_sdmmc_write(host->regs, SDMMC_RINTSTS, SDMMC_INT_CD); // clear sd detect int
1233                         present = rk29_sdmmc_get_cd(host->mmc);
1234                         present_old = test_bit(RK29_SDMMC_CARD_PRESENT, &host->flags);
1235                         if (present != 0) {
1236                                 dev_info(&host->pdev->dev, "card detect interrupt,card present \n");
1237                                 set_bit(RK29_SDMMC_CARD_PRESENT, &host->flags);
1238                                 if(present == present_old)      
1239                                         mod_timer(&host->detect_timer, jiffies + msecs_to_jiffies(2000));
1240                                 else
1241                                         mod_timer(&host->detect_timer, jiffies + msecs_to_jiffies(200));
1242                         } else {
1243                                 dev_info(&host->pdev->dev, "card detect interrupt,card not present \n");
1244                                 clear_bit(RK29_SDMMC_CARD_PRESENT, &host->flags);
1245                                 mod_timer(&host->detect_timer, jiffies + msecs_to_jiffies(10));                                 
1246                         }                                               
1247                 }       
1248                 if(pending & RK29_SDMMC_CMD_ERROR_FLAGS) {
1249                     rk29_sdmmc_write(host->regs, SDMMC_RINTSTS,RK29_SDMMC_CMD_ERROR_FLAGS);  //  clear interrupt
1250                     host->cmd_status = status;
1251                     smp_wmb();
1252                     rk29_sdmmc_set_pending(host, EVENT_CMD_COMPLETE);
1253                         if(host->cmd == NULL) {
1254                                 dev_info(&host->pdev->dev, "host->cmd == NULL\n");
1255                         }
1256                         else
1257                                 dev_info(&host->pdev->dev, "cmd transfer error(int status 0x%x cmd %d host->state %d pending_events %ld)\n", 
1258                                                 status,host->cmd->opcode,host->state,host->pending_events);
1259                         tasklet_schedule(&host->tasklet);
1260                 }
1261
1262                 if (pending & RK29_SDMMC_DATA_ERROR_FLAGS) { // if there is an error, let report DATA_ERROR
1263                         rk29_sdmmc_write(host->regs, SDMMC_RINTSTS,RK29_SDMMC_DATA_ERROR_FLAGS);  // clear interrupt
1264                         host->data_status = status;
1265                         smp_wmb();
1266                         rk29_sdmmc_set_pending(host, EVENT_DATA_ERROR);
1267                         tasklet_schedule(&host->tasklet);
1268                         dev_info(&host->pdev->dev, "data transfer error(int status 0x%x host->state %d pending_events %ld pending=0x%x)\n", 
1269                                         status,host->state,host->pending_events,pending);
1270                 }
1271
1272                 if(pending & SDMMC_INT_DTO) {
1273                     rk29_sdmmc_write(host->regs, SDMMC_RINTSTS,SDMMC_INT_DTO);  // clear interrupt
1274                     if (!host->data_status)
1275                                 host->data_status = status;
1276                     smp_wmb();
1277                     if(host->dir_status == RK29_SDMMC_RECV_STATUS) {
1278                         if(host->sg != NULL) 
1279                                 rk29_sdmmc_read_data_pio(host);
1280                     }
1281                     rk29_sdmmc_set_pending(host, EVENT_DATA_COMPLETE);
1282                     tasklet_schedule(&host->tasklet);
1283                 }
1284
1285                 if (pending & SDMMC_INT_RXDR) {
1286                     rk29_sdmmc_write(host->regs, SDMMC_RINTSTS,SDMMC_INT_RXDR);  //  clear interrupt
1287                     if(host->sg) 
1288                             rk29_sdmmc_read_data_pio(host);
1289                 }
1290
1291                 if (pending & SDMMC_INT_TXDR) {
1292                     rk29_sdmmc_write(host->regs, SDMMC_RINTSTS,SDMMC_INT_TXDR);  //  clear interrupt
1293                     if(host->sg) 
1294                                 rk29_sdmmc_write_data_pio(host);
1295                     
1296                 }
1297
1298                 if (pending & SDMMC_INT_CMD_DONE) {
1299                     rk29_sdmmc_write(host->regs, SDMMC_RINTSTS,SDMMC_INT_CMD_DONE);  //  clear interrupt
1300                     rk29_sdmmc_cmd_interrupt(host, status);
1301                 }
1302                 if(pending & SDMMC_INT_SDIO) {
1303                                 rk29_sdmmc_write(host->regs, SDMMC_RINTSTS,SDMMC_INT_SDIO);
1304                                 mmc_signal_sdio_irq(host->mmc);
1305                 }
1306 //      } while (pass_count++ < 5);
1307         spin_unlock(&host->lock);
1308         return IRQ_HANDLED;
1309 //      return pass_count ? IRQ_HANDLED : IRQ_NONE;
1310 }
1311
1312 /*
1313  *
1314  * MMC card detect thread, kicked off from detect interrupt, 1 timer 
1315  *
1316  */
1317 static void rk29_sdmmc_detect_change(unsigned long data)
1318 {
1319         struct mmc_request *mrq;
1320         struct rk29_sdmmc *host = (struct rk29_sdmmc *)data;
1321    
1322         smp_rmb();
1323         if (test_bit(RK29_SDMMC_SHUTDOWN, &host->flags))
1324                 return; 
1325         if(rk29_sdmmc_get_cd(host->mmc))
1326                 rk28_send_wakeup_key();
1327         
1328         spin_lock(&host->lock);         
1329         /* Clean up queue if present */
1330         mrq = host->mrq;
1331         if (mrq) {
1332                 if (mrq == host->curr_mrq) {
1333 #if 0 //TODO ,ºóÃæÒª°ÑÕâ¿é´¦ÀíÒƵ½tasklet,ʹÓÃ״̬»úÀ´×ö
1334                         /* reset all blocks */
1335                         rk29_sdmmc_write(host->regs, SDMMC_CTRL, (SDMMC_CTRL_RESET | SDMMC_CTRL_FIFO_RESET | SDMMC_CTRL_DMA_RESET));
1336                         /* wait till resets clear */
1337                         while (rk29_sdmmc_read(host->regs, SDMMC_CTRL) & (SDMMC_CTRL_RESET | SDMMC_CTRL_FIFO_RESET | SDMMC_CTRL_DMA_RESET));
1338                         /* FIFO threshold settings  */
1339                         rk29_sdmmc_write(host->regs, SDMMC_CTRL, rk29_sdmmc_read(host->regs, SDMMC_CTRL) | SDMMC_CTRL_INT_ENABLE);
1340 #endif
1341                         host->data = NULL;
1342                         host->cmd = NULL;
1343                         dev_info(&host->pdev->dev, "%s >> %s >> %d state=%d\n", __FILE__, __FUNCTION__,__LINE__,host->state);
1344
1345                         switch (host->state) {
1346                         case STATE_IDLE:
1347                                 break;
1348                         case STATE_SENDING_CMD:
1349                                 mrq->cmd->error = -ENOMEDIUM;
1350                                 if (!mrq->data)
1351                                         break;
1352                                 /* fall through */
1353                         case STATE_SENDING_DATA:
1354                                 if(mrq->data)
1355                                         mrq->data->error = -ENOMEDIUM;
1356                                 rk29_sdmmc_stop_dma(host);
1357                                 break;
1358                         case STATE_DATA_BUSY:
1359                         case STATE_DATA_ERROR:
1360                                 if (mrq->data && mrq->data->error == -EINPROGRESS)
1361                                         mrq->data->error = -ENOMEDIUM;
1362                                 if (!mrq->stop)
1363                                         break;
1364                                 /* fall through */
1365                         case STATE_SENDING_STOP:
1366                                 mrq->stop->error = -ENOMEDIUM;
1367                                 break;
1368                         }
1369
1370                                 rk29_sdmmc_request_end(host, mrq);
1371                         } else {
1372                                 dev_info(&host->pdev->dev, "mrq != host->curr_mrq");
1373                                 if (host->queue_node.next && host->queue_node.prev)
1374                                         list_del(&host->queue_node);
1375                                 mrq->cmd->error = -ENOMEDIUM;
1376                                 if (mrq->data) {
1377                                         mrq->data->bytes_xfered = 0;
1378                                         mrq->data->error = -ENOMEDIUM;
1379                                 }
1380                                 if (mrq->stop)
1381                                         mrq->stop->error = -ENOMEDIUM;
1382                                 spin_unlock(&host->lock);
1383                                 mmc_request_done(host->mmc, mrq);
1384                                 spin_lock(&host->lock);
1385                         }
1386                 
1387         
1388                 /* reset all blocks */
1389                 rk29_sdmmc_write(host->regs, SDMMC_CTRL, (SDMMC_CTRL_RESET | SDMMC_CTRL_FIFO_RESET | SDMMC_CTRL_DMA_RESET));
1390                 /* wait till resets clear */
1391                 dev_info(&host->pdev->dev, "mmc_detect_change: ctrl reset ");
1392                 while (rk29_sdmmc_read(host->regs, SDMMC_CTRL) & (SDMMC_CTRL_RESET | SDMMC_CTRL_FIFO_RESET | SDMMC_CTRL_DMA_RESET));
1393                 printk("done\n");
1394                 rk29_sdmmc_write(host->regs, SDMMC_CTRL, rk29_sdmmc_read(host->regs, SDMMC_CTRL) | SDMMC_CTRL_INT_ENABLE);
1395         }
1396         spin_unlock(&host->lock);
1397         
1398         mmc_detect_change(host->mmc, 0);        
1399 }
1400
1401 static void rk29_sdmmc1_check_status(unsigned long data)
1402 {
1403         struct rk29_sdmmc *host = (struct rk29_sdmmc *)data;
1404         struct rk29_sdmmc_platform_data *pdata = host->pdev->dev.platform_data;
1405         unsigned int status;
1406
1407         status = pdata->status(mmc_dev(host->mmc));
1408
1409         if (status ^ host->oldstatus)
1410         {
1411                 pr_info("%s: slot status change detected(%d-%d)\n",mmc_hostname(host->mmc), host->oldstatus, status);
1412                 if (status) {
1413                     set_bit(RK29_SDMMC_CARD_PRESENT, &host->flags);
1414                     mod_timer(&host->detect_timer, jiffies + msecs_to_jiffies(200));
1415                 }
1416                 else {
1417                     clear_bit(RK29_SDMMC_CARD_PRESENT, &host->flags);
1418                     rk29_sdmmc_detect_change((unsigned long)host);
1419                 }
1420         }
1421
1422         host->oldstatus = status;
1423 }
1424
1425 static void rk29_sdmmc1_status_notify_cb(int card_present, void *dev_id)
1426 {
1427         struct rk29_sdmmc *host = dev_id;
1428         printk(KERN_INFO "%s, card_present %d\n", mmc_hostname(host->mmc), card_present);
1429         rk29_sdmmc1_check_status((unsigned long)host);
1430 }
1431
1432 static int rk29_sdmmc_probe(struct platform_device *pdev)
1433 {
1434         struct mmc_host                 *mmc;
1435         struct rk29_sdmmc               *host;
1436         struct resource                 *regs;
1437         struct rk29_sdmmc_platform_data *pdata;
1438         int                             ret = 0;
1439
1440         regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1441         if (!regs)
1442                 return -ENXIO;
1443         mmc = mmc_alloc_host(sizeof(struct rk29_sdmmc), &pdev->dev);
1444         if (!mmc)
1445                 return -ENOMEM; 
1446         host = mmc_priv(mmc);
1447         host->mmc = mmc;
1448         host->pdev = pdev;
1449         pdata = pdev->dev.platform_data;
1450         if (!pdata) {
1451                 dev_err(&pdev->dev, "Platform data missing\n");
1452                 ret = -ENODEV;
1453                 goto err_freehost;
1454         }
1455         host->irq = platform_get_irq(pdev, 0);
1456         if (host->irq < 0)
1457                 return host->irq;
1458         host->gpio_det = pdata->detect_irq;
1459         if(pdata->io_init)
1460                 pdata->io_init();
1461         spin_lock_init(&host->lock);
1462         INIT_LIST_HEAD(&host->queue);
1463         ret = -ENOMEM;
1464         host->regs = ioremap(regs->start, regs->end - regs->start);
1465         if (!host->regs)
1466             goto err_freemap;   
1467         memcpy(host->dma_name, pdata->dma_name, 8);    
1468         host->use_dma = pdata->use_dma;     
1469         if(host->use_dma){
1470                 if(strncmp(host->dma_name, "sdio", strlen("sdio")) == 0){
1471                         rk29_dma_request(DMACH_SDIO, &rk29_dma_sdio1_client, NULL);
1472                         host->dma_chn = DMACH_SDIO;
1473                 }
1474                 if(strncmp(host->dma_name, "sd_mmc", strlen("sd_mmc")) == 0){   
1475                         rk29_dma_request(DMACH_SDMMC, &rk29_dma_sdmmc0_client, NULL);
1476                         host->dma_chn = DMACH_SDMMC;
1477                 }       
1478                 rk29_dma_config(host->dma_chn, 16);
1479                 rk29_dma_set_buffdone_fn(host->dma_chn, rk29_sdmmc_dma_complete);       
1480                 host->dma_addr = regs->start + SDMMC_DATA;
1481         }               
1482         host->clk = clk_get(&pdev->dev, "mmc");
1483         clk_set_rate(host->clk,52000000);
1484         clk_enable(host->clk);
1485         clk_enable(clk_get(&pdev->dev, "hclk_mmc"));
1486         host->bus_hz = clk_get_rate(host->clk);  ///40000000;  ////cgu_get_clk_freq(CGU_SB_SD_MMC_CCLK_IN_ID); 
1487
1488         /* reset all blocks */
1489         rk29_sdmmc_write(host->regs, SDMMC_CTRL,(SDMMC_CTRL_RESET | SDMMC_CTRL_FIFO_RESET | SDMMC_CTRL_DMA_RESET));
1490         /* wait till resets clear */
1491         while (rk29_sdmmc_read(host->regs, SDMMC_CTRL) & (SDMMC_CTRL_RESET | SDMMC_CTRL_FIFO_RESET | SDMMC_CTRL_DMA_RESET));
1492          /* Clear the interrupts for the host controller */
1493         rk29_sdmmc_write(host->regs, SDMMC_RINTSTS, 0xFFFFFFFF);
1494         rk29_sdmmc_write(host->regs, SDMMC_INTMASK, 0); // disable all mmc interrupt first
1495         /* Put in max timeout */
1496         rk29_sdmmc_write(host->regs, SDMMC_TMOUT, 0xFFFFFFFF);
1497
1498         /* FIFO threshold settings  */
1499         rk29_sdmmc_write(host->regs, SDMMC_FIFOTH, ((0x3 << 28) | (0x0f << 16) | (0x10 << 0))); // RXMark = 15, TXMark = 16, DMA Size = 16
1500         /* disable clock to CIU */
1501         rk29_sdmmc_write(host->regs, SDMMC_CLKENA,0);
1502         rk29_sdmmc_write(host->regs, SDMMC_CLKSRC,0);
1503         rk29_sdmmc_write(host->regs, SDMMC_PWREN, 1);
1504         tasklet_init(&host->tasklet, rk29_sdmmc_tasklet_func, (unsigned long)host);
1505         ret = request_irq(host->irq, rk29_sdmmc_interrupt, 0, dev_name(&pdev->dev), host);
1506         if (ret)
1507             goto err_dmaunmap;
1508        
1509 #if 0 
1510         /* register sdmmc1 card detect interrupt route */ 
1511         if ((pdev->id == 1) && (pdata->detect_irq != 0)) {
1512                 irq = gpio_to_irq(pdata->detect_irq);
1513                 if (irq < 0)  {
1514                         dev_info(&host->pdev->dev, "%s: request gpio irq failed\n", __FUNCTION__);
1515                         goto err_dmaunmap;
1516                 }
1517         
1518                 ret = request_irq(irq, rk29_sdmmc1_card_detect_interrupt, IRQF_TRIGGER_RISING, dev_name(&pdev->dev), host);
1519                 if (ret) {
1520                         dev_info(&host->pdev->dev, "%s: sdmmc1 request detect interrupt failed\n", __FUNCTION__);
1521                         goto err_dmaunmap;
1522                 }
1523          
1524         }
1525 #endif
1526         /* setup sdmmc1 wifi card detect change */
1527         if (pdata->register_status_notify) {
1528             pdata->register_status_notify(rk29_sdmmc1_status_notify_cb, host);
1529         }
1530
1531         /* Assume card is present initially */
1532         if(rk29_sdmmc_get_cd(host->mmc))
1533                 set_bit(RK29_SDMMC_CARD_PRESENT, &host->flags);
1534         else
1535                 clear_bit(RK29_SDMMC_CARD_PRESENT, &host->flags);
1536
1537         /* sdmmc1 wifi card slot status initially */
1538         if (pdata->status) {
1539             host->oldstatus = pdata->status(mmc_dev(host->mmc));
1540             if (host->oldstatus)  {
1541                 set_bit(RK29_SDMMC_CARD_PRESENT, &host->flags);
1542             }else {
1543                 clear_bit(RK29_SDMMC_CARD_PRESENT, &host->flags);
1544             }
1545         }
1546
1547         platform_set_drvdata(pdev, host);       
1548         mmc->ops = &rk29_sdmmc_ops[pdev->id];
1549         mmc->f_min = host->bus_hz/510;
1550         mmc->f_max = host->bus_hz/2;  //2;  ///20; //max f is clock to mmc_clk/2
1551         mmc->ocr_avail = pdata->host_ocr_avail;
1552         mmc->caps = pdata->host_caps;
1553         mmc->max_phys_segs = 64;
1554         mmc->max_hw_segs = 64;
1555         mmc->max_blk_size = 4095;  //65536; /* SDMMC_BLKSIZ is 16 bits*/
1556         mmc->max_blk_count = 65535;  ///512;
1557         mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
1558         mmc->max_seg_size = mmc->max_req_size;  
1559         
1560         mmc_add_host(mmc);
1561 #if defined (CONFIG_DEBUG_FS)
1562         rk29_sdmmc_init_debugfs(host);
1563 #endif
1564         /* Create card detect handler thread  */
1565         setup_timer(&host->detect_timer, rk29_sdmmc_detect_change,(unsigned long)host);
1566         // enable interrupt for command done, data over, data empty, receive ready and error such as transmit, receive timeout, crc error
1567         rk29_sdmmc_write(host->regs, SDMMC_RINTSTS, 0xFFFFFFFF);
1568         if(host->use_dma)
1569                 rk29_sdmmc_write(host->regs, SDMMC_INTMASK,SDMMC_INT_CMD_DONE | SDMMC_INT_DTO | RK29_SDMMC_ERROR_FLAGS | SDMMC_INT_CD);
1570         else
1571                 rk29_sdmmc_write(host->regs, SDMMC_INTMASK,SDMMC_INT_CMD_DONE | SDMMC_INT_DTO | SDMMC_INT_TXDR | SDMMC_INT_RXDR | RK29_SDMMC_ERROR_FLAGS | SDMMC_INT_CD);
1572         rk29_sdmmc_write(host->regs, SDMMC_CTRL,SDMMC_CTRL_INT_ENABLE); // enable mci interrupt
1573         rk29_sdmmc_write(host->regs, SDMMC_CLKENA,1);
1574         dev_info(&pdev->dev, "RK29 SDMMC controller at irq %d\n", host->irq);
1575         return 0;
1576 err_dmaunmap:
1577         if(host->use_dma){
1578                 if(strncmp(host->dma_name, "sdio", strlen("sdio")) == 0)
1579                         rk29_dma_free(DMACH_SDIO, &rk29_dma_sdio1_client);
1580                 if(strncmp(host->dma_name, "sd_mmc", strlen("sd_mmc")) == 0)    
1581                         rk29_dma_free(DMACH_SDMMC, &rk29_dma_sdmmc0_client);
1582         }
1583 err_freemap:
1584         iounmap(host->regs);
1585 err_freehost:
1586         kfree(host);
1587         return ret;
1588 }
1589
1590
1591
1592 static int __exit rk29_sdmmc_remove(struct platform_device *pdev)
1593 {
1594         struct rk29_sdmmc *host = platform_get_drvdata(pdev);
1595
1596         rk29_sdmmc_write(host->regs, SDMMC_RINTSTS, 0xFFFFFFFF);
1597         rk29_sdmmc_write(host->regs, SDMMC_INTMASK, 0); // disable all mmc interrupt first
1598         
1599         /* Shutdown detect IRQ and kill detect thread */
1600         del_timer_sync(&host->detect_timer);
1601
1602         /* Debugfs stuff is cleaned up by mmc core */
1603         set_bit(RK29_SDMMC_SHUTDOWN, &host->flags);
1604         smp_wmb();
1605         mmc_remove_host(host->mmc);
1606         mmc_free_host(host->mmc);
1607
1608         /* disable clock to CIU */
1609         rk29_sdmmc_write(host->regs, SDMMC_CLKENA,0);
1610         rk29_sdmmc_write(host->regs, SDMMC_CLKSRC,0);
1611         
1612         free_irq(platform_get_irq(pdev, 0), host);
1613         if(host->use_dma){
1614                 if(strncmp(host->dma_name, "sdio", strlen("sdio")) == 0)
1615                         rk29_dma_free(DMACH_SDIO, &rk29_dma_sdio1_client);
1616                 if(strncmp(host->dma_name, "sd_mmc", strlen("sd_mmc")) == 0)    
1617                         rk29_dma_free(DMACH_SDMMC, &rk29_dma_sdmmc0_client);
1618         }
1619         iounmap(host->regs);
1620
1621         kfree(host);
1622         return 0;
1623 }
1624 static irqreturn_t det_keys_isr(int irq, void *dev_id)
1625 {
1626         struct rk29_sdmmc *host = dev_id;
1627         dev_info(&host->pdev->dev, "sd det_gpio changed(%s), send wakeup key!\n",
1628                 gpio_get_value(RK29_PIN2_PA2)?"removed":"insert");
1629         rk29_sdmmc_detect_change((unsigned long)dev_id);
1630
1631         return IRQ_HANDLED;
1632 }
1633
1634 static int rk29_sdmmc_sdcard_suspend(struct rk29_sdmmc *host)
1635 {
1636         int ret = 0;
1637         disable_irq_nosync(host->irq);
1638         rk29_mux_api_set(GPIO2A2_SDMMC0DETECTN_NAME, GPIO2L_GPIO2A2);
1639         gpio_request(RK29_PIN2_PA2, "sd_detect");
1640         gpio_direction_input(RK29_PIN2_PA2);
1641
1642         host->gpio_irq = gpio_to_irq(RK29_PIN2_PA2);
1643         ret = request_irq(host->gpio_irq, det_keys_isr,
1644                                             (gpio_get_value(RK29_PIN2_PA2))?IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING,
1645                                             "sd_detect",
1646                                             host);
1647         
1648         enable_irq_wake(host->gpio_irq);
1649
1650         return ret;
1651 }
1652 static void rk29_sdmmc_sdcard_resume(struct rk29_sdmmc *host)
1653 {
1654         disable_irq_wake(host->gpio_irq);
1655         free_irq(host->gpio_irq,host);
1656         gpio_free(RK29_PIN2_PA2);
1657         rk29_mux_api_set(GPIO2A2_SDMMC0DETECTN_NAME, GPIO2L_SDMMC0_DETECT_N);
1658         enable_irq(host->irq);
1659 }
1660
1661 static int rk29_sdmmc_suspend(struct platform_device *pdev, pm_message_t state)
1662 {
1663         int ret = 0;
1664 #ifdef CONFIG_PM
1665         struct rk29_sdmmc *host = platform_get_drvdata(pdev);
1666
1667         dev_info(&host->pdev->dev, "Enter rk29_sdmmc_suspend\n");
1668         if(host->mmc && (strncmp(host->dma_name, "sdio", strlen("sdio")) != 0)){
1669                 ret = mmc_suspend_host(host->mmc, state);
1670                 if(rk29_sdmmc_sdcard_suspend(host) < 0)
1671                         dev_info(&host->pdev->dev, "rk29_sdmmc_sdcard_suspend error\n");
1672         }
1673         rk29_sdmmc_write(host->regs, SDMMC_CLKENA, 0);
1674         clk_disable(host->clk);
1675 #endif
1676         return ret;
1677 }
1678
1679 static int rk29_sdmmc_resume(struct platform_device *pdev)
1680 {
1681         int ret = 0;
1682 #ifdef CONFIG_PM
1683         struct rk29_sdmmc *host = platform_get_drvdata(pdev);
1684
1685         dev_info(&host->pdev->dev, "Exit rk29_sdmmc_suspend\n");
1686         clk_enable(host->clk);
1687     rk29_sdmmc_write(host->regs, SDMMC_CLKENA, 1);
1688         if(host->mmc && (strncmp(host->dma_name, "sdio", strlen("sdio")) != 0)){
1689                 rk29_sdmmc_sdcard_resume(host); 
1690                 ret = mmc_resume_host(host->mmc);
1691         }
1692 #endif
1693         return ret;
1694 }
1695 static struct platform_driver rk29_sdmmc_driver = {
1696         .suspend    = rk29_sdmmc_suspend,
1697         .resume     = rk29_sdmmc_resume,
1698         .remove         = __exit_p(rk29_sdmmc_remove),
1699         .driver         = {
1700                 .name           = "rk29_sdmmc",
1701         },
1702 };
1703
1704 static int __init rk29_sdmmc_init(void)
1705 {
1706         return platform_driver_probe(&rk29_sdmmc_driver, rk29_sdmmc_probe);
1707 }
1708
1709 static void __exit rk29_sdmmc_exit(void)
1710 {
1711         platform_driver_unregister(&rk29_sdmmc_driver);
1712 }
1713
1714 module_init(rk29_sdmmc_init);
1715 module_exit(rk29_sdmmc_exit);
1716
1717 MODULE_DESCRIPTION("Rk29 Multimedia Card Interface driver");
1718 MODULE_AUTHOR("Rockchips");
1719 MODULE_LICENSE("GPL v2");
1720