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