Close some printk sentences.
[firefly-linux-kernel-4.4.55.git] / drivers / mmc / core / core.c
1 /*
2  *  linux/drivers/mmc/core/core.c
3  *
4  *  Copyright (C) 2003-2004 Russell King, All Rights Reserved.
5  *  SD support Copyright (C) 2004 Ian Molton, All Rights Reserved.
6  *  Copyright (C) 2005-2008 Pierre Ossman, All Rights Reserved.
7  *  MMCv4 support Copyright (C) 2006 Philip Langdale, All Rights Reserved.
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License version 2 as
11  * published by the Free Software Foundation.
12  */
13 #include <linux/module.h>
14 #include <linux/init.h>
15 #include <linux/interrupt.h>
16 #include <linux/completion.h>
17 #include <linux/device.h>
18 #include <linux/delay.h>
19 #include <linux/pagemap.h>
20 #include <linux/err.h>
21 #include <linux/leds.h>
22 #include <linux/scatterlist.h>
23 #include <linux/log2.h>
24 #include <linux/regulator/consumer.h>
25 #include <linux/wakelock.h>
26
27 #include <linux/mmc/card.h>
28 #include <linux/mmc/host.h>
29 #include <linux/mmc/mmc.h>
30 #include <linux/mmc/sd.h>
31
32 #include "core.h"
33 #include "bus.h"
34 #include "host.h"
35 #include "sdio_bus.h"
36
37 #include "mmc_ops.h"
38 #include "sd_ops.h"
39 #include "sdio_ops.h"
40
41 static struct workqueue_struct *workqueue;
42 static struct wake_lock mmc_delayed_work_wake_lock;
43
44 /*
45  * Enabling software CRCs on the data blocks can be a significant (30%)
46  * performance cost, and for other reasons may not always be desired.
47  * So we allow it it to be disabled.
48  */
49 int use_spi_crc = 1;
50 module_param(use_spi_crc, bool, 0);
51
52 /*
53  * Internal function. Schedule delayed work in the MMC work queue.
54  */
55 static int mmc_schedule_delayed_work(struct delayed_work *work,
56                                      unsigned long delay)
57 {
58         wake_lock(&mmc_delayed_work_wake_lock);
59         return queue_delayed_work(workqueue, work, delay);
60 }
61
62 /*
63  * Internal function. Flush all scheduled work from the MMC work queue.
64  */
65 static void mmc_flush_scheduled_work(void)
66 {
67         flush_workqueue(workqueue);
68 }
69
70 /**
71  *      mmc_request_done - finish processing an MMC request
72  *      @host: MMC host which completed request
73  *      @mrq: MMC request which request
74  *
75  *      MMC drivers should call this function when they have completed
76  *      their processing of a request.
77  */
78 void mmc_request_done(struct mmc_host *host, struct mmc_request *mrq)
79 {
80         struct mmc_command *cmd = mrq->cmd;
81         int err = cmd->error;
82
83         if (err && cmd->retries && mmc_host_is_spi(host)) {
84                 if (cmd->resp[0] & R1_SPI_ILLEGAL_COMMAND)
85                         cmd->retries = 0;
86         }
87
88         if (err && cmd->retries) {
89                 pr_debug("%s: req failed (CMD%u): %d, retrying...\n",
90                         mmc_hostname(host), cmd->opcode, err);
91
92                 cmd->retries--;
93                 cmd->error = 0;
94                 host->ops->request(host, mrq);
95         } else {
96                 led_trigger_event(host->led, LED_OFF);
97
98                 pr_debug("%s: req done (CMD%u): %d: %08x %08x %08x %08x\n",
99                         mmc_hostname(host), cmd->opcode, err,
100                         cmd->resp[0], cmd->resp[1],
101                         cmd->resp[2], cmd->resp[3]);
102
103                 if (mrq->data) {
104                         pr_debug("%s:     %d bytes transferred: %d\n",
105                                 mmc_hostname(host),
106                                 mrq->data->bytes_xfered, mrq->data->error);
107                 }
108
109                 if (mrq->stop) {
110                         pr_debug("%s:     (CMD%u): %d: %08x %08x %08x %08x\n",
111                                 mmc_hostname(host), mrq->stop->opcode,
112                                 mrq->stop->error,
113                                 mrq->stop->resp[0], mrq->stop->resp[1],
114                                 mrq->stop->resp[2], mrq->stop->resp[3]);
115                 }
116
117                 if (mrq->done)
118                         mrq->done(mrq);
119         }
120 }
121
122 EXPORT_SYMBOL(mmc_request_done);
123
124 static void
125 mmc_start_request(struct mmc_host *host, struct mmc_request *mrq)
126 {
127 #ifdef CONFIG_MMC_DEBUG
128         unsigned int i, sz;
129         struct scatterlist *sg;
130 #endif
131
132         pr_debug("%s: starting CMD%u arg %08x flags %08x\n",
133                  mmc_hostname(host), mrq->cmd->opcode,
134                  mrq->cmd->arg, mrq->cmd->flags);
135
136         if (mrq->data) {
137                 pr_debug("%s:     blksz %d blocks %d flags %08x "
138                         "tsac %d ms nsac %d\n",
139                         mmc_hostname(host), mrq->data->blksz,
140                         mrq->data->blocks, mrq->data->flags,
141                         mrq->data->timeout_ns / 1000000,
142                         mrq->data->timeout_clks);
143         }
144
145         if (mrq->stop) {
146                 pr_debug("%s:     CMD%u arg %08x flags %08x\n",
147                          mmc_hostname(host), mrq->stop->opcode,
148                          mrq->stop->arg, mrq->stop->flags);
149         }
150
151         WARN_ON(!host->claimed);
152
153         led_trigger_event(host->led, LED_FULL);
154
155         mrq->cmd->error = 0;
156         mrq->cmd->mrq = mrq;
157         if (mrq->data) {
158                 BUG_ON(mrq->data->blksz > host->max_blk_size);
159                 BUG_ON(mrq->data->blocks > host->max_blk_count);
160                 BUG_ON(mrq->data->blocks * mrq->data->blksz >
161                         host->max_req_size);
162
163 #ifdef CONFIG_MMC_DEBUG
164                 sz = 0;
165                 for_each_sg(mrq->data->sg, sg, mrq->data->sg_len, i)
166                         sz += sg->length;
167                 BUG_ON(sz != mrq->data->blocks * mrq->data->blksz);
168 #endif
169
170                 mrq->cmd->data = mrq->data;
171                 mrq->data->error = 0;
172                 mrq->data->mrq = mrq;
173                 if (mrq->stop) {
174                         mrq->data->stop = mrq->stop;
175                         mrq->stop->error = 0;
176                         mrq->stop->mrq = mrq;
177                 }
178         }
179         host->ops->request(host, mrq);
180 }
181
182 static void mmc_wait_done(struct mmc_request *mrq)
183 {
184         complete(mrq->done_data);
185 }
186
187 /**
188  *      mmc_wait_for_req - start a request and wait for completion
189  *      @host: MMC host to start command
190  *      @mrq: MMC request to start
191  *
192  *      Start a new MMC custom command request for a host, and wait
193  *      for the command to complete. Does not attempt to parse the
194  *      response.
195  */
196 void mmc_wait_for_req(struct mmc_host *host, struct mmc_request *mrq)
197 {
198     unsigned long datasize, waittime=0xFFFF;
199     u32 multi, unit;
200     
201         DECLARE_COMPLETION_ONSTACK(complete);
202
203         mrq->done_data = &complete;
204         mrq->done = mmc_wait_done;
205
206         mmc_start_request(host, mrq);
207
208 #if defined(CONFIG_SDMMC_RK29) && !defined(CONFIG_SDMMC_RK29_OLD)
209     if( strncmp( mmc_hostname(host) ,"mmc0" , strlen("mmc0")) ) 
210     {
211         multi = (mrq->cmd->retries>0)?mrq->cmd->retries:1;
212         waittime = wait_for_completion_timeout(&complete,HZ*7*multi); //sdio; for cmd dead. Modifyed by xbw at 2011-06-02
213     }
214     else
215     {   
216         //calculate the timeout value for SDMMC; added by xbw at 2011-09-27
217         if(mrq->data)
218         {
219             unit = 3*(1<<20);// unit=3MB
220             datasize = mrq->data->blksz*mrq->data->blocks;
221             multi = datasize/unit;
222             multi += (datasize%unit)?1:0;
223             multi = (multi>0) ? multi : 1;
224             multi += (mrq->cmd->retries>0)?1:0;
225             waittime = wait_for_completion_timeout(&complete,HZ*7*multi); //It should be longer than bottom driver's time,due to the sum of two cmd time.
226                                                                           //modifyed by xbw at 2011-10-08
227                                                                           //
228                                                                           //example:
229                                                                           //rk29_sdmmc_request_end..2336...   CMD12 wait busy timeout!!!!! ====xbw=[sd_mmc]====
230                                                                           //mmc_wait_for_req..236.. !!!!! wait for CMD25 timeout ===xbw[mmc0]===
231         }
232         else
233         {
234             multi = (mrq->cmd->retries>0)?mrq->cmd->retries:1;
235             waittime = wait_for_completion_timeout(&complete,HZ*7*multi);
236         }
237     }
238     
239     if(waittime <= 1)
240     {
241         host->doneflag = 0;
242         mrq->cmd->error = -EIO;
243         printk("%s..%d.. !!!!! wait for CMD%d timeout ===xbw[%s]===\n",\
244             __FUNCTION__, __LINE__, mrq->cmd->opcode, mmc_hostname(host));
245     }
246 #else    
247         wait_for_completion(&complete);
248 #endif
249 }
250
251 EXPORT_SYMBOL(mmc_wait_for_req);
252
253 /**
254  *      mmc_wait_for_cmd - start a command and wait for completion
255  *      @host: MMC host to start command
256  *      @cmd: MMC command to start
257  *      @retries: maximum number of retries
258  *
259  *      Start a new MMC command for a host, and wait for the command
260  *      to complete.  Return any error that occurred while the command
261  *      was executing.  Do not attempt to parse the response.
262  */
263 int mmc_wait_for_cmd(struct mmc_host *host, struct mmc_command *cmd, int retries)
264 {
265         struct mmc_request mrq;
266
267         WARN_ON(!host->claimed);
268
269         memset(&mrq, 0, sizeof(struct mmc_request));
270
271         memset(cmd->resp, 0, sizeof(cmd->resp));
272         cmd->retries = retries;
273
274         mrq.cmd = cmd;
275         cmd->data = NULL;
276
277         mmc_wait_for_req(host, &mrq);
278
279         return cmd->error;
280 }
281
282 EXPORT_SYMBOL(mmc_wait_for_cmd);
283
284 /**
285  *      mmc_set_data_timeout - set the timeout for a data command
286  *      @data: data phase for command
287  *      @card: the MMC card associated with the data transfer
288  *
289  *      Computes the data timeout parameters according to the
290  *      correct algorithm given the card type.
291  */
292 void mmc_set_data_timeout(struct mmc_data *data, const struct mmc_card *card)
293 {
294         unsigned int mult;
295
296         /*
297          * SDIO cards only define an upper 1 s limit on access.
298          */
299         if (mmc_card_sdio(card)) {
300                 data->timeout_ns = 1000000000;
301                 data->timeout_clks = 0;
302                 return;
303         }
304
305         /*
306          * SD cards use a 100 multiplier rather than 10
307          */
308         mult = mmc_card_sd(card) ? 100 : 10;
309
310         /*
311          * Scale up the multiplier (and therefore the timeout) by
312          * the r2w factor for writes.
313          */
314         if (data->flags & MMC_DATA_WRITE)
315                 mult <<= card->csd.r2w_factor;
316
317         data->timeout_ns = card->csd.tacc_ns * mult;
318         data->timeout_clks = card->csd.tacc_clks * mult;
319
320         /*
321          * SD cards also have an upper limit on the timeout.
322          */
323         if (mmc_card_sd(card)) {
324                 unsigned int timeout_us, limit_us;
325
326                 timeout_us = data->timeout_ns / 1000;
327                 timeout_us += data->timeout_clks * 1000 /
328                         (card->host->ios.clock / 1000);
329
330                 if (data->flags & MMC_DATA_WRITE)
331                         /*
332                          * The limit is really 250 ms, but that is
333                          * insufficient for some crappy cards.
334                          */
335                         limit_us = 300000;
336                 else
337                         limit_us = 100000;
338
339                 /*
340                  * SDHC cards always use these fixed values.
341                  */
342                 if (timeout_us > limit_us || mmc_card_blockaddr(card)) {
343                         data->timeout_ns = limit_us * 1000;
344                         data->timeout_clks = 0;
345                 }
346         }
347         /*
348          * Some cards need very high timeouts if driven in SPI mode.
349          * The worst observed timeout was 900ms after writing a
350          * continuous stream of data until the internal logic
351          * overflowed.
352          */
353         if (mmc_host_is_spi(card->host)) {
354                 if (data->flags & MMC_DATA_WRITE) {
355                         if (data->timeout_ns < 1000000000)
356                                 data->timeout_ns = 1000000000;  /* 1s */
357                 } else {
358                         if (data->timeout_ns < 100000000)
359                                 data->timeout_ns =  100000000;  /* 100ms */
360                 }
361         }
362 }
363 EXPORT_SYMBOL(mmc_set_data_timeout);
364
365 /**
366  *      mmc_align_data_size - pads a transfer size to a more optimal value
367  *      @card: the MMC card associated with the data transfer
368  *      @sz: original transfer size
369  *
370  *      Pads the original data size with a number of extra bytes in
371  *      order to avoid controller bugs and/or performance hits
372  *      (e.g. some controllers revert to PIO for certain sizes).
373  *
374  *      Returns the improved size, which might be unmodified.
375  *
376  *      Note that this function is only relevant when issuing a
377  *      single scatter gather entry.
378  */
379 unsigned int mmc_align_data_size(struct mmc_card *card, unsigned int sz)
380 {
381         /*
382          * FIXME: We don't have a system for the controller to tell
383          * the core about its problems yet, so for now we just 32-bit
384          * align the size.
385          */
386         sz = ((sz + 3) / 4) * 4;
387
388         return sz;
389 }
390 EXPORT_SYMBOL(mmc_align_data_size);
391
392 /**
393  *      mmc_host_enable - enable a host.
394  *      @host: mmc host to enable
395  *
396  *      Hosts that support power saving can use the 'enable' and 'disable'
397  *      methods to exit and enter power saving states. For more information
398  *      see comments for struct mmc_host_ops.
399  */
400 int mmc_host_enable(struct mmc_host *host)
401 {
402         if (!(host->caps & MMC_CAP_DISABLE))
403                 return 0;
404
405         if (host->en_dis_recurs)
406                 return 0;
407
408         if (host->nesting_cnt++)
409                 return 0;
410
411         cancel_delayed_work_sync(&host->disable);
412
413         if (host->enabled)
414                 return 0;
415
416         if (host->ops->enable) {
417                 int err;
418
419                 host->en_dis_recurs = 1;
420                 err = host->ops->enable(host);
421                 host->en_dis_recurs = 0;
422
423                 if (err) {
424                         pr_debug("%s: enable error %d\n",
425                                  mmc_hostname(host), err);
426                         return err;
427                 }
428         }
429         host->enabled = 1;
430         return 0;
431 }
432 EXPORT_SYMBOL(mmc_host_enable);
433
434 static int mmc_host_do_disable(struct mmc_host *host, int lazy)
435 {
436         if (host->ops->disable) {
437                 int err;
438
439                 host->en_dis_recurs = 1;
440                 err = host->ops->disable(host, lazy);
441                 host->en_dis_recurs = 0;
442
443                 if (err < 0) {
444                         pr_debug("%s: disable error %d\n",
445                                  mmc_hostname(host), err);
446                         return err;
447                 }
448                 if (err > 0) {
449                         unsigned long delay = msecs_to_jiffies(err);
450
451                         mmc_schedule_delayed_work(&host->disable, delay);
452                 }
453         }
454         host->enabled = 0;
455         return 0;
456 }
457
458 /**
459  *      mmc_host_disable - disable a host.
460  *      @host: mmc host to disable
461  *
462  *      Hosts that support power saving can use the 'enable' and 'disable'
463  *      methods to exit and enter power saving states. For more information
464  *      see comments for struct mmc_host_ops.
465  */
466 int mmc_host_disable(struct mmc_host *host)
467 {
468         int err;
469
470         if (!(host->caps & MMC_CAP_DISABLE))
471                 return 0;
472
473         if (host->en_dis_recurs)
474                 return 0;
475
476         if (--host->nesting_cnt)
477                 return 0;
478
479         if (!host->enabled)
480                 return 0;
481
482         err = mmc_host_do_disable(host, 0);
483         return err;
484 }
485 EXPORT_SYMBOL(mmc_host_disable);
486
487 /**
488  *      __mmc_claim_host - exclusively claim a host
489  *      @host: mmc host to claim
490  *      @abort: whether or not the operation should be aborted
491  *
492  *      Claim a host for a set of operations.  If @abort is non null and
493  *      dereference a non-zero value then this will return prematurely with
494  *      that non-zero value without acquiring the lock.  Returns zero
495  *      with the lock held otherwise.
496  */
497 int __mmc_claim_host(struct mmc_host *host, atomic_t *abort)
498 {
499         DECLARE_WAITQUEUE(wait, current);
500         unsigned long flags;
501         int stop;
502
503         might_sleep();
504
505         add_wait_queue(&host->wq, &wait);
506         spin_lock_irqsave(&host->lock, flags);
507         while (1) {
508                 set_current_state(TASK_UNINTERRUPTIBLE);
509                 stop = abort ? atomic_read(abort) : 0;
510                 if (stop || !host->claimed || host->claimer == current)
511                         break;
512                 spin_unlock_irqrestore(&host->lock, flags);
513                 schedule();
514                 spin_lock_irqsave(&host->lock, flags);
515         }
516         set_current_state(TASK_RUNNING);
517         if (!stop) {
518                 host->claimed = 1;
519                 host->claimer = current;
520                 host->claim_cnt += 1;
521         } else
522                 wake_up(&host->wq);
523         spin_unlock_irqrestore(&host->lock, flags);
524         remove_wait_queue(&host->wq, &wait);
525         if (!stop)
526                 mmc_host_enable(host);
527         return stop;
528 }
529
530 EXPORT_SYMBOL(__mmc_claim_host);
531
532 /**
533  *      mmc_try_claim_host - try exclusively to claim a host
534  *      @host: mmc host to claim
535  *
536  *      Returns %1 if the host is claimed, %0 otherwise.
537  */
538 int mmc_try_claim_host(struct mmc_host *host)
539 {
540         int claimed_host = 0;
541         unsigned long flags;
542
543         spin_lock_irqsave(&host->lock, flags);
544         if (!host->claimed || host->claimer == current) {
545                 host->claimed = 1;
546                 host->claimer = current;
547                 host->claim_cnt += 1;
548                 claimed_host = 1;
549         }
550         spin_unlock_irqrestore(&host->lock, flags);
551         return claimed_host;
552 }
553 EXPORT_SYMBOL(mmc_try_claim_host);
554
555 static void mmc_do_release_host(struct mmc_host *host)
556 {
557         unsigned long flags;
558
559         spin_lock_irqsave(&host->lock, flags);
560         if (--host->claim_cnt) {
561                 /* Release for nested claim */
562                 spin_unlock_irqrestore(&host->lock, flags);
563         } else {
564                 host->claimed = 0;
565                 host->claimer = NULL;
566                 spin_unlock_irqrestore(&host->lock, flags);
567                 wake_up(&host->wq);
568         }
569 }
570
571 void mmc_host_deeper_disable(struct work_struct *work)
572 {
573         struct mmc_host *host =
574                 container_of(work, struct mmc_host, disable.work);
575
576         /* If the host is claimed then we do not want to disable it anymore */
577         if (!mmc_try_claim_host(host))
578                 goto out;
579         mmc_host_do_disable(host, 1);
580         mmc_do_release_host(host);
581
582 out:
583         wake_unlock(&mmc_delayed_work_wake_lock);
584 }
585
586 /**
587  *      mmc_host_lazy_disable - lazily disable a host.
588  *      @host: mmc host to disable
589  *
590  *      Hosts that support power saving can use the 'enable' and 'disable'
591  *      methods to exit and enter power saving states. For more information
592  *      see comments for struct mmc_host_ops.
593  */
594 int mmc_host_lazy_disable(struct mmc_host *host)
595 {
596         if (!(host->caps & MMC_CAP_DISABLE))
597                 return 0;
598
599         if (host->en_dis_recurs)
600                 return 0;
601
602         if (--host->nesting_cnt)
603                 return 0;
604
605         if (!host->enabled)
606                 return 0;
607
608         if (host->disable_delay) {
609                 mmc_schedule_delayed_work(&host->disable,
610                                 msecs_to_jiffies(host->disable_delay));
611                 return 0;
612         } else
613                 return mmc_host_do_disable(host, 1);
614 }
615 EXPORT_SYMBOL(mmc_host_lazy_disable);
616
617 /**
618  *      mmc_release_host - release a host
619  *      @host: mmc host to release
620  *
621  *      Release a MMC host, allowing others to claim the host
622  *      for their operations.
623  */
624 void mmc_release_host(struct mmc_host *host)
625 {
626         WARN_ON(!host->claimed);
627
628         mmc_host_lazy_disable(host);
629
630         mmc_do_release_host(host);
631 }
632
633 EXPORT_SYMBOL(mmc_release_host);
634
635 /*
636  * Internal function that does the actual ios call to the host driver,
637  * optionally printing some debug output.
638  */
639 static inline void mmc_set_ios(struct mmc_host *host)
640 {
641         struct mmc_ios *ios = &host->ios;
642
643         pr_debug("%s: clock %uHz busmode %u powermode %u cs %u Vdd %u "
644                 "width %u timing %u\n",
645                  mmc_hostname(host), ios->clock, ios->bus_mode,
646                  ios->power_mode, ios->chip_select, ios->vdd,
647                  ios->bus_width, ios->timing);
648
649         host->ops->set_ios(host, ios);
650 }
651
652 /*
653  * Control chip select pin on a host.
654  */
655 void mmc_set_chip_select(struct mmc_host *host, int mode)
656 {
657         host->ios.chip_select = mode;
658         mmc_set_ios(host);
659 }
660
661 /*
662  * Sets the host clock to the highest possible frequency that
663  * is below "hz".
664  */
665 void mmc_set_clock(struct mmc_host *host, unsigned int hz)
666 {
667         WARN_ON(hz < host->f_min);
668
669         if (hz > host->f_max)
670                 hz = host->f_max;
671
672         host->ios.clock = hz;
673         mmc_set_ios(host);
674 }
675
676 /*
677  * Change the bus mode (open drain/push-pull) of a host.
678  */
679 void mmc_set_bus_mode(struct mmc_host *host, unsigned int mode)
680 {
681         host->ios.bus_mode = mode;
682         mmc_set_ios(host);
683 }
684
685 /*
686  * Change data bus width of a host.
687  */
688 void mmc_set_bus_width(struct mmc_host *host, unsigned int width)
689 {
690         host->ios.bus_width = width;
691         mmc_set_ios(host);
692 }
693
694 /**
695  * mmc_vdd_to_ocrbitnum - Convert a voltage to the OCR bit number
696  * @vdd:        voltage (mV)
697  * @low_bits:   prefer low bits in boundary cases
698  *
699  * This function returns the OCR bit number according to the provided @vdd
700  * value. If conversion is not possible a negative errno value returned.
701  *
702  * Depending on the @low_bits flag the function prefers low or high OCR bits
703  * on boundary voltages. For example,
704  * with @low_bits = true, 3300 mV translates to ilog2(MMC_VDD_32_33);
705  * with @low_bits = false, 3300 mV translates to ilog2(MMC_VDD_33_34);
706  *
707  * Any value in the [1951:1999] range translates to the ilog2(MMC_VDD_20_21).
708  */
709 static int mmc_vdd_to_ocrbitnum(int vdd, bool low_bits)
710 {
711         const int max_bit = ilog2(MMC_VDD_35_36);
712         int bit;
713
714         if (vdd < 1650 || vdd > 3600)
715                 return -EINVAL;
716
717         if (vdd >= 1650 && vdd <= 1950)
718                 return ilog2(MMC_VDD_165_195);
719
720         if (low_bits)
721                 vdd -= 1;
722
723         /* Base 2000 mV, step 100 mV, bit's base 8. */
724         bit = (vdd - 2000) / 100 + 8;
725         if (bit > max_bit)
726                 return max_bit;
727         return bit;
728 }
729
730 /**
731  * mmc_vddrange_to_ocrmask - Convert a voltage range to the OCR mask
732  * @vdd_min:    minimum voltage value (mV)
733  * @vdd_max:    maximum voltage value (mV)
734  *
735  * This function returns the OCR mask bits according to the provided @vdd_min
736  * and @vdd_max values. If conversion is not possible the function returns 0.
737  *
738  * Notes wrt boundary cases:
739  * This function sets the OCR bits for all boundary voltages, for example
740  * [3300:3400] range is translated to MMC_VDD_32_33 | MMC_VDD_33_34 |
741  * MMC_VDD_34_35 mask.
742  */
743 u32 mmc_vddrange_to_ocrmask(int vdd_min, int vdd_max)
744 {
745         u32 mask = 0;
746
747         if (vdd_max < vdd_min)
748                 return 0;
749
750         /* Prefer high bits for the boundary vdd_max values. */
751         vdd_max = mmc_vdd_to_ocrbitnum(vdd_max, false);
752         if (vdd_max < 0)
753                 return 0;
754
755         /* Prefer low bits for the boundary vdd_min values. */
756         vdd_min = mmc_vdd_to_ocrbitnum(vdd_min, true);
757         if (vdd_min < 0)
758                 return 0;
759
760         /* Fill the mask, from max bit to min bit. */
761         while (vdd_max >= vdd_min)
762                 mask |= 1 << vdd_max--;
763
764         return mask;
765 }
766 EXPORT_SYMBOL(mmc_vddrange_to_ocrmask);
767
768 #ifdef CONFIG_REGULATOR
769
770 /**
771  * mmc_regulator_get_ocrmask - return mask of supported voltages
772  * @supply: regulator to use
773  *
774  * This returns either a negative errno, or a mask of voltages that
775  * can be provided to MMC/SD/SDIO devices using the specified voltage
776  * regulator.  This would normally be called before registering the
777  * MMC host adapter.
778  */
779 int mmc_regulator_get_ocrmask(struct regulator *supply)
780 {
781         int                     result = 0;
782         int                     count;
783         int                     i;
784
785         count = regulator_count_voltages(supply);
786         if (count < 0)
787                 return count;
788
789         for (i = 0; i < count; i++) {
790                 int             vdd_uV;
791                 int             vdd_mV;
792
793                 vdd_uV = regulator_list_voltage(supply, i);
794                 if (vdd_uV <= 0)
795                         continue;
796
797                 vdd_mV = vdd_uV / 1000;
798                 result |= mmc_vddrange_to_ocrmask(vdd_mV, vdd_mV);
799         }
800
801         return result;
802 }
803 EXPORT_SYMBOL(mmc_regulator_get_ocrmask);
804
805 /**
806  * mmc_regulator_set_ocr - set regulator to match host->ios voltage
807  * @vdd_bit: zero for power off, else a bit number (host->ios.vdd)
808  * @supply: regulator to use
809  *
810  * Returns zero on success, else negative errno.
811  *
812  * MMC host drivers may use this to enable or disable a regulator using
813  * a particular supply voltage.  This would normally be called from the
814  * set_ios() method.
815  */
816 int mmc_regulator_set_ocr(struct regulator *supply, unsigned short vdd_bit)
817 {
818         int                     result = 0;
819         int                     min_uV, max_uV;
820         int                     enabled;
821
822         enabled = regulator_is_enabled(supply);
823         if (enabled < 0)
824                 return enabled;
825
826         if (vdd_bit) {
827                 int             tmp;
828                 int             voltage;
829
830                 /* REVISIT mmc_vddrange_to_ocrmask() may have set some
831                  * bits this regulator doesn't quite support ... don't
832                  * be too picky, most cards and regulators are OK with
833                  * a 0.1V range goof (it's a small error percentage).
834                  */
835                 tmp = vdd_bit - ilog2(MMC_VDD_165_195);
836                 if (tmp == 0) {
837                         min_uV = 1650 * 1000;
838                         max_uV = 1950 * 1000;
839                 } else {
840                         min_uV = 1900 * 1000 + tmp * 100 * 1000;
841                         max_uV = min_uV + 100 * 1000;
842                 }
843
844                 /* avoid needless changes to this voltage; the regulator
845                  * might not allow this operation
846                  */
847                 voltage = regulator_get_voltage(supply);
848                 if (voltage < 0)
849                         result = voltage;
850                 else if (voltage < min_uV || voltage > max_uV)
851                         result = regulator_set_voltage(supply, min_uV, max_uV);
852                 else
853                         result = 0;
854
855                 if (result == 0 && !enabled)
856                         result = regulator_enable(supply);
857         } else if (enabled) {
858                 result = regulator_disable(supply);
859         }
860
861         return result;
862 }
863 EXPORT_SYMBOL(mmc_regulator_set_ocr);
864
865 #endif
866
867 /*
868  * Mask off any voltages we don't support and select
869  * the lowest voltage
870  */
871 u32 mmc_select_voltage(struct mmc_host *host, u32 ocr)
872 {
873         int bit;
874
875         ocr &= host->ocr_avail;
876
877         bit = ffs(ocr);
878         if (bit) {
879                 bit -= 1;
880
881                 ocr &= 3 << bit;
882
883                 host->ios.vdd = bit;
884                 mmc_set_ios(host);
885         } else {
886                 pr_warning("%s: host doesn't support card's voltages\n",
887                                 mmc_hostname(host));
888                 ocr = 0;
889         }
890
891         return ocr;
892 }
893
894 /*
895  * Select timing parameters for host.
896  */
897 void mmc_set_timing(struct mmc_host *host, unsigned int timing)
898 {
899         host->ios.timing = timing;
900         mmc_set_ios(host);
901 }
902
903 /*
904  * Apply power to the MMC stack.  This is a two-stage process.
905  * First, we enable power to the card without the clock running.
906  * We then wait a bit for the power to stabilise.  Finally,
907  * enable the bus drivers and clock to the card.
908  *
909  * We must _NOT_ enable the clock prior to power stablising.
910  *
911  * If a host does all the power sequencing itself, ignore the
912  * initial MMC_POWER_UP stage.
913  */
914 static void mmc_power_up(struct mmc_host *host)
915 {
916         int bit;
917
918         /* If ocr is set, we use it */
919         if (host->ocr)
920                 bit = ffs(host->ocr) - 1;
921         else
922                 bit = fls(host->ocr_avail) - 1;
923
924         host->ios.vdd = bit;
925         if (mmc_host_is_spi(host)) {
926                 host->ios.chip_select = MMC_CS_HIGH;
927                 host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
928         } else {
929                 host->ios.chip_select = MMC_CS_DONTCARE;
930                 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
931         }
932         host->ios.power_mode = MMC_POWER_UP;
933         host->ios.bus_width = MMC_BUS_WIDTH_1;
934         host->ios.timing = MMC_TIMING_LEGACY;
935         mmc_set_ios(host);
936
937         /*
938          * This delay should be sufficient to allow the power supply
939          * to reach the minimum voltage.
940          */
941         mmc_delay(10);
942
943         host->ios.clock = host->f_min;
944
945         host->ios.power_mode = MMC_POWER_ON;
946         mmc_set_ios(host);
947
948         /*
949          * This delay must be at least 74 clock sizes, or 1 ms, or the
950          * time required to reach a stable voltage.
951          */
952         mmc_delay(10);
953 }
954
955 static void mmc_power_off(struct mmc_host *host)
956 {
957         host->ios.clock = 0;
958         host->ios.vdd = 0;
959         if (!mmc_host_is_spi(host)) {
960                 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
961                 host->ios.chip_select = MMC_CS_DONTCARE;
962         }
963         host->ios.power_mode = MMC_POWER_OFF;
964         host->ios.bus_width = MMC_BUS_WIDTH_1;
965         host->ios.timing = MMC_TIMING_LEGACY;
966         mmc_set_ios(host);
967 }
968
969 /*
970  * Cleanup when the last reference to the bus operator is dropped.
971  */
972 static void __mmc_release_bus(struct mmc_host *host)
973 {
974         BUG_ON(!host);
975         BUG_ON(host->bus_refs);
976         BUG_ON(!host->bus_dead);
977
978         host->bus_ops = NULL;
979 }
980
981 /*
982  * Increase reference count of bus operator
983  */
984 static inline void mmc_bus_get(struct mmc_host *host)
985 {
986         unsigned long flags;
987
988         spin_lock_irqsave(&host->lock, flags);
989         host->bus_refs++;
990         spin_unlock_irqrestore(&host->lock, flags);
991 }
992
993 /*
994  * Decrease reference count of bus operator and free it if
995  * it is the last reference.
996  */
997 static inline void mmc_bus_put(struct mmc_host *host)
998 {
999         unsigned long flags;
1000
1001         spin_lock_irqsave(&host->lock, flags);
1002         host->bus_refs--;
1003         if ((host->bus_refs == 0) && host->bus_ops)
1004                 __mmc_release_bus(host);
1005         spin_unlock_irqrestore(&host->lock, flags);
1006 }
1007
1008 int mmc_resume_bus(struct mmc_host *host)
1009 {
1010         if (!mmc_bus_needs_resume(host))
1011                 return -EINVAL;
1012
1013         printk("%s: Starting deferred resume\n", mmc_hostname(host));
1014         host->bus_resume_flags &= ~MMC_BUSRESUME_NEEDS_RESUME;
1015         mmc_bus_get(host);
1016         if (host->bus_ops && !host->bus_dead) {
1017                 mmc_power_up(host);
1018                 BUG_ON(!host->bus_ops->resume);
1019                 host->bus_ops->resume(host);
1020         }
1021
1022         if (host->bus_ops->detect && !host->bus_dead)
1023                 host->bus_ops->detect(host);
1024
1025         mmc_bus_put(host);
1026         printk("%s: Deferred resume completed\n", mmc_hostname(host));
1027         return 0;
1028 }
1029
1030 EXPORT_SYMBOL(mmc_resume_bus);
1031
1032 /*
1033  * Assign a mmc bus handler to a host. Only one bus handler may control a
1034  * host at any given time.
1035  */
1036 void mmc_attach_bus(struct mmc_host *host, const struct mmc_bus_ops *ops)
1037 {
1038         unsigned long flags;
1039
1040         BUG_ON(!host);
1041         BUG_ON(!ops);
1042
1043         WARN_ON(!host->claimed);
1044
1045         spin_lock_irqsave(&host->lock, flags);
1046
1047         BUG_ON(host->bus_ops);
1048         BUG_ON(host->bus_refs);
1049
1050         host->bus_ops = ops;
1051         host->bus_refs = 1;
1052         host->bus_dead = 0;
1053
1054         spin_unlock_irqrestore(&host->lock, flags);
1055 }
1056
1057 /*
1058  * Remove the current bus handler from a host. Assumes that there are
1059  * no interesting cards left, so the bus is powered down.
1060  */
1061 void mmc_detach_bus(struct mmc_host *host)
1062 {
1063         unsigned long flags;
1064
1065         BUG_ON(!host);
1066
1067         WARN_ON(!host->claimed);
1068         WARN_ON(!host->bus_ops);
1069
1070         spin_lock_irqsave(&host->lock, flags);
1071
1072         host->bus_dead = 1;
1073
1074         spin_unlock_irqrestore(&host->lock, flags);
1075
1076         mmc_power_off(host);
1077
1078         mmc_bus_put(host);
1079 }
1080
1081 /**
1082  *      mmc_detect_change - process change of state on a MMC socket
1083  *      @host: host which changed state.
1084  *      @delay: optional delay to wait before detection (jiffies)
1085  *
1086  *      MMC drivers should call this when they detect a card has been
1087  *      inserted or removed. The MMC layer will confirm that any
1088  *      present card is still functional, and initialize any newly
1089  *      inserted.
1090  */
1091 void mmc_detect_change(struct mmc_host *host, unsigned long delay)
1092 {
1093 #ifdef CONFIG_MMC_DEBUG
1094         unsigned long flags;
1095         spin_lock_irqsave(&host->lock, flags);
1096         WARN_ON(host->removed);
1097         spin_unlock_irqrestore(&host->lock, flags);
1098 #endif
1099
1100         mmc_schedule_delayed_work(&host->detect, delay);
1101 }
1102
1103 EXPORT_SYMBOL(mmc_detect_change);
1104
1105 #if defined(CONFIG_SDMMC_RK29) && !defined(CONFIG_SDMMC_RK29_OLD)  
1106 void mmc_rescan(struct work_struct *work)
1107 {
1108         struct mmc_host *host =
1109                 container_of(work, struct mmc_host, detect.work);
1110         u32 ocr;
1111         int err;
1112         int extend_wakelock = 0;
1113
1114     
1115         mmc_bus_get(host);
1116
1117         /* if there is a card registered, check whether it is still present */
1118         if ((host->bus_ops != NULL) && host->bus_ops->detect && !host->bus_dead)
1119                 host->bus_ops->detect(host);
1120
1121         /* If the card was removed the bus will be marked
1122          * as dead - extend the wakelock so userspace
1123          * can respond */
1124         if (host->bus_dead)
1125                 extend_wakelock = 1;
1126
1127         mmc_bus_put(host);
1128
1129
1130         mmc_bus_get(host);
1131
1132         /* if there still is a card present, stop here */
1133         if (host->bus_ops != NULL) {
1134                 mmc_bus_put(host);
1135                 goto out;
1136         }
1137
1138         /* detect a newly inserted card */
1139
1140         /*
1141          * Only we can add a new handler, so it's safe to
1142          * release the lock here.
1143          */
1144         mmc_bus_put(host);
1145     printk("\n%s...%d..  ===== mmc_rescan Begin....======xbw[%s]=====\n",__FILE__, __LINE__, mmc_hostname(host));
1146
1147         if (host->ops->get_cd && host->ops->get_cd(host) == 0)
1148         {
1149             printk("\n=================\n%s..%d..  ====find no SDMMC host.====xbw[%s]=====\n", \
1150                 __FUNCTION__, __LINE__, mmc_hostname(host));
1151                 
1152                 goto out;
1153         }
1154
1155         mmc_claim_host(host);
1156         
1157         mmc_power_up(host);
1158         
1159         mmc_go_idle(host);
1160
1161     /*
1162         In oder to improve the initialization process in rockchip IC, I modify the following code about the the initialization process of SDIO-SD-MMC.
1163         So I deleted the CMD8 and add a conditional to distinguish between the two card type,i.e.SDMMC process and SDIO process.
1164         For detail,please refer to "RK29XX Technical Reference Manual" and "SD-MMC-SDIO Specifications".
1165         Noted by xbw@2011-04-09
1166     */
1167
1168     //mmc_send_if_cond(host, host->ocr_avail); //deleted by xbw@2011-04-09
1169 #if !defined(CONFIG_USE_SDMMC0_FOR_WIFI_DEVELOP_BOARD)  
1170      if( strncmp( mmc_hostname(host) ,"mmc0" , strlen("mmc0")) ){
1171 #endif     
1172         /*
1173          * First we search for SDIO...
1174          */
1175         err = mmc_send_io_op_cond(host, 0, &ocr);
1176         if (!err) {
1177                 printk("\n%s..%d..  ===== Begin to identify card as SDIO-card===xbw[%s]===\n",__FUNCTION__, __LINE__, mmc_hostname(host));
1178
1179                 if (mmc_attach_sdio(host, ocr))
1180                 {
1181                     printk("\n=====\n %s..%d..  ===== Initialize SDIO-card unsuccessfully!!! ===xbw[%s]===\n=====\n",\
1182                         __FUNCTION__,  __LINE__, mmc_hostname(host));
1183                         
1184                         mmc_power_off(host);
1185                 }
1186                 else
1187                 {
1188                     printk("%s..%d..  ===== Initialize SDIO successfully. ===xbw[%s]===\n",__FUNCTION__,  __LINE__, mmc_hostname(host));
1189                 }
1190                 extend_wakelock = 1;
1191                 goto out;
1192         }
1193 #if !defined(CONFIG_USE_SDMMC0_FOR_WIFI_DEVELOP_BOARD)          
1194     }
1195 #endif
1196
1197     /*
1198      * ...then normal SD...
1199      */
1200     err = mmc_send_app_op_cond(host, 0, &ocr);
1201     if (!err) {
1202         printk("\n%s..%d..  ===== Begin to identify card as SD-card ===xbw[%s]===\n",__FUNCTION__, __LINE__, mmc_hostname(host));
1203
1204         if (mmc_attach_sd(host, ocr))
1205         {
1206             printk("\n=====\n%s..%d..  ===== Initialize SD-card unsuccessfully!!! ===xbw[%s]===\n====\n",\
1207                 __FUNCTION__,  __LINE__, mmc_hostname(host));
1208                 
1209                 mmc_power_off(host);
1210         }
1211         else
1212         {
1213             printk("%s..%d..  ===== Initialize SD-card successfully. ===xbw[%s]===\n",__FUNCTION__,  __LINE__, mmc_hostname(host));
1214         }
1215         extend_wakelock = 1;
1216         goto out;
1217     }
1218
1219     /*
1220      * ...and finally MMC.
1221      */
1222     err = mmc_send_op_cond(host, 0, &ocr);
1223     if (!err) {
1224         printk("\n%s..%d..  ===== Begin to identify card as MMC-card ===xbw[%s]===\n", __FUNCTION__, __LINE__, mmc_hostname(host));
1225
1226         if (mmc_attach_mmc(host, ocr))
1227         {
1228             printk("\n =====\n%s..%d..  ===== Initialize MMC-card unsuccessfully!!! ===xbw[%s]===\n======\n",\
1229                 __FUNCTION__,  __LINE__, mmc_hostname(host));
1230                 
1231                 mmc_power_off(host);
1232         }
1233         else
1234         {
1235             printk("%s...%d..  ===== Initialize MMC-card successfully. ===xbw[%s]===\n",__FUNCTION__,  __LINE__, mmc_hostname(host));
1236         }
1237         extend_wakelock = 1;
1238         goto out;
1239     }
1240
1241         mmc_release_host(host);
1242         mmc_power_off(host);
1243
1244 out:
1245
1246         if (extend_wakelock)
1247                 wake_lock_timeout(&mmc_delayed_work_wake_lock, HZ / 2);
1248         else
1249                 wake_unlock(&mmc_delayed_work_wake_lock);
1250
1251         if (host->caps & MMC_CAP_NEEDS_POLL)
1252                 mmc_schedule_delayed_work(&host->detect, HZ);
1253 }
1254
1255 #else
1256 void mmc_rescan(struct work_struct *work)
1257 {
1258         struct mmc_host *host =
1259                 container_of(work, struct mmc_host, detect.work);
1260         u32 ocr;
1261         int err;
1262         int extend_wakelock = 0;
1263         
1264 #if defined(CONFIG_SDMMC_RK29) && defined(CONFIG_SDMMC_RK29_OLD)        
1265         unsigned long flags;
1266
1267         spin_lock_irqsave(&host->lock, flags);
1268
1269         if (host->rescan_disable) {
1270                 spin_unlock_irqrestore(&host->lock, flags);
1271                 return;
1272         }
1273
1274         spin_unlock_irqrestore(&host->lock, flags);
1275 #endif
1276
1277
1278         mmc_bus_get(host);
1279
1280         /* if there is a card registered, check whether it is still present */
1281         if ((host->bus_ops != NULL) && host->bus_ops->detect && !host->bus_dead)
1282                 host->bus_ops->detect(host);
1283
1284         /* If the card was removed the bus will be marked
1285          * as dead - extend the wakelock so userspace
1286          * can respond */
1287         if (host->bus_dead)
1288                 extend_wakelock = 1;
1289
1290         mmc_bus_put(host);
1291
1292
1293         mmc_bus_get(host);
1294
1295         /* if there still is a card present, stop here */
1296         if (host->bus_ops != NULL) {
1297                 mmc_bus_put(host);
1298                 goto out;
1299         }
1300
1301         /* detect a newly inserted card */
1302
1303         /*
1304          * Only we can add a new handler, so it's safe to
1305          * release the lock here.
1306          */
1307         mmc_bus_put(host);
1308
1309         if (host->ops->get_cd && host->ops->get_cd(host) == 0)
1310                 goto out;
1311
1312         mmc_claim_host(host);
1313
1314         mmc_power_up(host);
1315         mmc_go_idle(host);
1316
1317         mmc_send_if_cond(host, host->ocr_avail);
1318
1319         /*
1320          * First we search for SDIO...
1321          */
1322         err = mmc_send_io_op_cond(host, 0, &ocr);
1323         if (!err) {
1324                 if (mmc_attach_sdio(host, ocr))
1325                         mmc_power_off(host);
1326                 extend_wakelock = 1;
1327                 goto out;
1328         }
1329
1330         /*
1331          * ...then normal SD...
1332          */
1333         err = mmc_send_app_op_cond(host, 0, &ocr);
1334         if (!err) {
1335                 if (mmc_attach_sd(host, ocr))
1336                         mmc_power_off(host);
1337                 extend_wakelock = 1;
1338                 goto out;
1339         }
1340
1341         /*
1342          * ...and finally MMC.
1343          */
1344         err = mmc_send_op_cond(host, 0, &ocr);
1345         if (!err) {
1346                 if (mmc_attach_mmc(host, ocr))
1347                         mmc_power_off(host);
1348                 extend_wakelock = 1;
1349                 goto out;
1350         }
1351
1352         mmc_release_host(host);
1353         mmc_power_off(host);
1354
1355 out:
1356         if (extend_wakelock)
1357                 wake_lock_timeout(&mmc_delayed_work_wake_lock, HZ / 2);
1358         else
1359                 wake_unlock(&mmc_delayed_work_wake_lock);
1360
1361         if (host->caps & MMC_CAP_NEEDS_POLL)
1362                 mmc_schedule_delayed_work(&host->detect, HZ);
1363 }
1364 #endif
1365
1366 void mmc_start_host(struct mmc_host *host)
1367 {
1368         mmc_power_off(host);
1369         mmc_detect_change(host, 0);
1370 }
1371
1372 void mmc_stop_host(struct mmc_host *host)
1373 {
1374 #ifdef CONFIG_MMC_DEBUG
1375         unsigned long flags;
1376         spin_lock_irqsave(&host->lock, flags);
1377         host->removed = 1;
1378         spin_unlock_irqrestore(&host->lock, flags);
1379 #endif
1380
1381         if (host->caps & MMC_CAP_DISABLE)
1382                 cancel_delayed_work(&host->disable);
1383         cancel_delayed_work(&host->detect);
1384         mmc_flush_scheduled_work();
1385
1386         mmc_bus_get(host);
1387         if (host->bus_ops && !host->bus_dead) {
1388                 if (host->bus_ops->remove)
1389                         host->bus_ops->remove(host);
1390
1391                 mmc_claim_host(host);
1392                 mmc_detach_bus(host);
1393                 mmc_release_host(host);
1394                 mmc_bus_put(host);
1395                 return;
1396         }
1397         mmc_bus_put(host);
1398
1399         BUG_ON(host->card);
1400
1401         mmc_power_off(host);
1402 }
1403
1404 void mmc_power_save_host(struct mmc_host *host)
1405 {
1406         mmc_bus_get(host);
1407
1408         if (!host->bus_ops || host->bus_dead || !host->bus_ops->power_restore) {
1409                 mmc_bus_put(host);
1410                 return;
1411         }
1412
1413         if (host->bus_ops->power_save)
1414                 host->bus_ops->power_save(host);
1415
1416         mmc_bus_put(host);
1417
1418         mmc_power_off(host);
1419 }
1420 EXPORT_SYMBOL(mmc_power_save_host);
1421
1422 void mmc_power_restore_host(struct mmc_host *host)
1423 {
1424         mmc_bus_get(host);
1425
1426         if (!host->bus_ops || host->bus_dead || !host->bus_ops->power_restore) {
1427                 mmc_bus_put(host);
1428                 return;
1429         }
1430
1431         mmc_power_up(host);
1432         host->bus_ops->power_restore(host);
1433
1434         mmc_bus_put(host);
1435 }
1436 EXPORT_SYMBOL(mmc_power_restore_host);
1437
1438 int mmc_card_awake(struct mmc_host *host)
1439 {
1440         int err = -ENOSYS;
1441
1442         mmc_bus_get(host);
1443
1444         if (host->bus_ops && !host->bus_dead && host->bus_ops->awake)
1445                 err = host->bus_ops->awake(host);
1446
1447         mmc_bus_put(host);
1448
1449         return err;
1450 }
1451 EXPORT_SYMBOL(mmc_card_awake);
1452
1453 int mmc_card_sleep(struct mmc_host *host)
1454 {
1455         int err = -ENOSYS;
1456
1457         mmc_bus_get(host);
1458
1459         if (host->bus_ops && !host->bus_dead && host->bus_ops->awake)
1460                 err = host->bus_ops->sleep(host);
1461
1462         mmc_bus_put(host);
1463
1464         return err;
1465 }
1466 EXPORT_SYMBOL(mmc_card_sleep);
1467
1468 int mmc_card_can_sleep(struct mmc_host *host)
1469 {
1470         struct mmc_card *card = host->card;
1471
1472         if (card && mmc_card_mmc(card) && card->ext_csd.rev >= 3)
1473                 return 1;
1474         return 0;
1475 }
1476 EXPORT_SYMBOL(mmc_card_can_sleep);
1477
1478 #ifdef CONFIG_PM
1479
1480 /**
1481  *      mmc_suspend_host - suspend a host
1482  *      @host: mmc host
1483  *      @state: suspend mode (PM_SUSPEND_xxx)
1484  */
1485 int mmc_suspend_host(struct mmc_host *host, pm_message_t state)
1486 {
1487         int err = 0;
1488
1489         if (mmc_bus_needs_resume(host))
1490                 return 0;
1491
1492         if (host->caps & MMC_CAP_DISABLE)
1493                 cancel_delayed_work(&host->disable);
1494         cancel_delayed_work(&host->detect);
1495         mmc_flush_scheduled_work();
1496
1497         mmc_bus_get(host);
1498         if (host->bus_ops && !host->bus_dead) {
1499                 if (host->bus_ops->suspend)
1500                         err = host->bus_ops->suspend(host);
1501                         
1502 #if defined(CONFIG_SDMMC_RK29) && defined(CONFIG_SDMMC_RK29_OLD)
1503         //deleted all detail code.
1504 #else
1505                 if (err == -ENOSYS || !host->bus_ops->resume) {
1506                         /*
1507                          * We simply "remove" the card in this case.
1508                          * It will be redetected on resume.
1509                          */
1510                         if (host->bus_ops->remove)
1511                                 host->bus_ops->remove(host);
1512                         mmc_claim_host(host);
1513                         mmc_detach_bus(host);
1514                         mmc_release_host(host);
1515                         err = 0;
1516                 }
1517 #endif          
1518         }
1519         mmc_bus_put(host);
1520
1521         if (!err)
1522                 mmc_power_off(host);
1523
1524         return err;
1525 }
1526
1527 EXPORT_SYMBOL(mmc_suspend_host);
1528
1529 /**
1530  *      mmc_resume_host - resume a previously suspended host
1531  *      @host: mmc host
1532  */
1533 int mmc_resume_host(struct mmc_host *host)
1534 {
1535         int err = 0;
1536
1537         mmc_bus_get(host);
1538         if (host->bus_resume_flags & MMC_BUSRESUME_MANUAL_RESUME) {
1539                 host->bus_resume_flags |= MMC_BUSRESUME_NEEDS_RESUME;
1540                 mmc_bus_put(host);
1541                 return 0;
1542         }
1543
1544         if (host->bus_ops && !host->bus_dead) {
1545                 mmc_power_up(host);
1546                 mmc_select_voltage(host, host->ocr);
1547                 BUG_ON(!host->bus_ops->resume);
1548                 
1549 #if defined(CONFIG_SDMMC_RK29) && defined(CONFIG_SDMMC_RK29_OLD)
1550                 err = host->bus_ops->resume(host);
1551                 if (err) {
1552                         printk(KERN_WARNING "%s: error %d during resume "
1553                                             "(card was removed?)\n",
1554                                             mmc_hostname(host), err);
1555                         err = 0;
1556                 }
1557
1558 #elif defined(CONFIG_SDMMC_RK29) && !defined(CONFIG_SDMMC_RK29_OLD)  
1559         //panic if the card is being removed during the resume, deleted by xbw at 2011-06-20
1560                 host->bus_ops->resume(host);
1561
1562 #else
1563                 err = host->bus_ops->resume(host);
1564                 if (err) {
1565                         printk(KERN_WARNING "%s: error %d during resume "
1566                                             "(card was removed?)\n",
1567                                             mmc_hostname(host), err);
1568                         if (host->bus_ops->remove)
1569                                 host->bus_ops->remove(host);
1570                         mmc_claim_host(host);
1571                         mmc_detach_bus(host);
1572                         mmc_release_host(host);
1573                         /* no need to bother upper layers */
1574                         err = 0;
1575                 }               
1576 #endif
1577         }
1578         mmc_bus_put(host);
1579
1580 #if defined(CONFIG_SDMMC_RK29) && defined(CONFIG_SDMMC_RK29_OLD)
1581     //delete the detect_change.
1582 #else
1583         /*
1584          * We add a slight delay here so that resume can progress
1585          * in parallel.
1586          */
1587         mmc_detect_change(host, 1);
1588 #endif
1589         return err;
1590 }
1591 EXPORT_SYMBOL(mmc_resume_host);
1592
1593 ///old driver add the function reforming to kernel2.6.38
1594 #if defined(CONFIG_SDMMC_RK29) && defined(CONFIG_SDMMC_RK29_OLD) 
1595 /* Do the card removal on suspend if card is assumed removeable
1596  * Do that in pm notifier while userspace isn't yet frozen, so we will be able
1597    to sync the card.
1598 */
1599 int mmc_pm_notify(struct notifier_block *notify_block,
1600                                         unsigned long mode, void *unused)
1601 {
1602         struct mmc_host *host = container_of(
1603                 notify_block, struct mmc_host, pm_notify);
1604         unsigned long flags;
1605
1606
1607         switch (mode) {
1608         case PM_HIBERNATION_PREPARE:
1609         case PM_SUSPEND_PREPARE:
1610
1611                 spin_lock_irqsave(&host->lock, flags);
1612                 host->rescan_disable = 1;
1613                 spin_unlock_irqrestore(&host->lock, flags);
1614                 cancel_delayed_work_sync(&host->detect);
1615
1616                 if (!host->bus_ops || host->bus_ops->suspend)
1617                         break;
1618
1619                 mmc_claim_host(host);
1620
1621                 if (host->bus_ops->remove)
1622                         host->bus_ops->remove(host);
1623
1624                 mmc_detach_bus(host);
1625                 mmc_release_host(host);
1626                 break;
1627
1628         case PM_POST_SUSPEND:
1629         case PM_POST_HIBERNATION:
1630
1631                 spin_lock_irqsave(&host->lock, flags);
1632                 host->rescan_disable = 0;
1633                 spin_unlock_irqrestore(&host->lock, flags);
1634                 mmc_detect_change(host, 0);
1635
1636         }
1637
1638         return 0;
1639 }
1640 #endif// endif---defined(CONFIG_SDMMC_RK29) && defined(CONFIG_SDMMC_RK29_OLD) 
1641
1642 #endif
1643
1644 #ifdef CONFIG_MMC_EMBEDDED_SDIO
1645 void mmc_set_embedded_sdio_data(struct mmc_host *host,
1646                                 struct sdio_cis *cis,
1647                                 struct sdio_cccr *cccr,
1648                                 struct sdio_embedded_func *funcs,
1649                                 int num_funcs)
1650 {
1651         host->embedded_sdio_data.cis = cis;
1652         host->embedded_sdio_data.cccr = cccr;
1653         host->embedded_sdio_data.funcs = funcs;
1654         host->embedded_sdio_data.num_funcs = num_funcs;
1655 }
1656
1657 EXPORT_SYMBOL(mmc_set_embedded_sdio_data);
1658 #endif
1659
1660 static int __init mmc_init(void)
1661 {
1662         int ret;
1663
1664         wake_lock_init(&mmc_delayed_work_wake_lock, WAKE_LOCK_SUSPEND, "mmc_delayed_work");
1665
1666         workqueue = create_singlethread_workqueue("kmmcd");
1667         if (!workqueue)
1668                 return -ENOMEM;
1669
1670         ret = mmc_register_bus();
1671         if (ret)
1672                 goto destroy_workqueue;
1673
1674         ret = mmc_register_host_class();
1675         if (ret)
1676                 goto unregister_bus;
1677
1678         ret = sdio_register_bus();
1679         if (ret)
1680                 goto unregister_host_class;
1681
1682         return 0;
1683
1684 unregister_host_class:
1685         mmc_unregister_host_class();
1686 unregister_bus:
1687         mmc_unregister_bus();
1688 destroy_workqueue:
1689         destroy_workqueue(workqueue);
1690
1691         return ret;
1692 }
1693
1694 static void __exit mmc_exit(void)
1695 {
1696         sdio_unregister_bus();
1697         mmc_unregister_host_class();
1698         mmc_unregister_bus();
1699         destroy_workqueue(workqueue);
1700         wake_lock_destroy(&mmc_delayed_work_wake_lock);
1701 }
1702
1703 subsys_initcall(mmc_init);
1704 module_exit(mmc_exit);
1705
1706 MODULE_LICENSE("GPL");