f18ba0aecca70c445661578d98dcfe3350727cc9
[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/pm_runtime.h>
26 #include <linux/wakelock.h>
27
28 #include <linux/mmc/card.h>
29 #include <linux/mmc/host.h>
30 #include <linux/mmc/mmc.h>
31 #include <linux/mmc/sd.h>
32
33 #include "core.h"
34 #include "bus.h"
35 #include "host.h"
36 #include "sdio_bus.h"
37
38 #include "mmc_ops.h"
39 #include "sd_ops.h"
40 #include "sdio_ops.h"
41
42 static struct workqueue_struct *workqueue;
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  * We normally treat cards as removed during suspend if they are not
54  * known to be on a non-removable bus, to avoid the risk of writing
55  * back data to a different card after resume.  Allow this to be
56  * overridden if necessary.
57  */
58 #ifdef CONFIG_MMC_UNSAFE_RESUME
59 int mmc_assume_removable;
60 #else
61 int mmc_assume_removable = 1;
62 #endif
63 EXPORT_SYMBOL(mmc_assume_removable);
64 module_param_named(removable, mmc_assume_removable, bool, 0644);
65 MODULE_PARM_DESC(
66         removable,
67         "MMC/SD cards are removable and may be removed during suspend");
68
69 /*
70  * Internal function. Schedule delayed work in the MMC work queue.
71  */
72 static int mmc_schedule_delayed_work(struct delayed_work *work,
73                                      unsigned long delay)
74 {
75         return queue_delayed_work(workqueue, work, delay);
76 }
77
78 /*
79  * Internal function. Flush all scheduled work from the MMC work queue.
80  */
81 static void mmc_flush_scheduled_work(void)
82 {
83         flush_workqueue(workqueue);
84 }
85
86 /**
87  *      mmc_request_done - finish processing an MMC request
88  *      @host: MMC host which completed request
89  *      @mrq: MMC request which request
90  *
91  *      MMC drivers should call this function when they have completed
92  *      their processing of a request.
93  */
94 void mmc_request_done(struct mmc_host *host, struct mmc_request *mrq)
95 {
96         struct mmc_command *cmd = mrq->cmd;
97         int err = cmd->error;
98
99         if (err && cmd->retries && mmc_host_is_spi(host)) {
100                 if (cmd->resp[0] & R1_SPI_ILLEGAL_COMMAND)
101                         cmd->retries = 0;
102         }
103
104         if (err && cmd->retries) {
105                 pr_debug("%s: req failed (CMD%u): %d, retrying...\n",
106                         mmc_hostname(host), cmd->opcode, err);
107
108                 cmd->retries--;
109                 cmd->error = 0;
110                 host->ops->request(host, mrq);
111         } else {
112                 led_trigger_event(host->led, LED_OFF);
113
114                 pr_debug("%s: req done (CMD%u): %d: %08x %08x %08x %08x\n",
115                         mmc_hostname(host), cmd->opcode, err,
116                         cmd->resp[0], cmd->resp[1],
117                         cmd->resp[2], cmd->resp[3]);
118
119                 if (mrq->data) {
120                         pr_debug("%s:     %d bytes transferred: %d\n",
121                                 mmc_hostname(host),
122                                 mrq->data->bytes_xfered, mrq->data->error);
123                 }
124
125                 if (mrq->stop) {
126                         pr_debug("%s:     (CMD%u): %d: %08x %08x %08x %08x\n",
127                                 mmc_hostname(host), mrq->stop->opcode,
128                                 mrq->stop->error,
129                                 mrq->stop->resp[0], mrq->stop->resp[1],
130                                 mrq->stop->resp[2], mrq->stop->resp[3]);
131                 }
132
133                 if (mrq->done)
134                         mrq->done(mrq);
135
136                 mmc_host_clk_release(host);
137         }
138 }
139
140 EXPORT_SYMBOL(mmc_request_done);
141
142 static void
143 mmc_start_request(struct mmc_host *host, struct mmc_request *mrq)
144 {
145 #ifdef CONFIG_MMC_DEBUG
146         unsigned int i, sz;
147         struct scatterlist *sg;
148 #endif
149
150         pr_debug("%s: starting CMD%u arg %08x flags %08x\n",
151                  mmc_hostname(host), mrq->cmd->opcode,
152                  mrq->cmd->arg, mrq->cmd->flags);
153
154         if (mrq->data) {
155                 pr_debug("%s:     blksz %d blocks %d flags %08x "
156                         "tsac %d ms nsac %d\n",
157                         mmc_hostname(host), mrq->data->blksz,
158                         mrq->data->blocks, mrq->data->flags,
159                         mrq->data->timeout_ns / 1000000,
160                         mrq->data->timeout_clks);
161         }
162
163         if (mrq->stop) {
164                 pr_debug("%s:     CMD%u arg %08x flags %08x\n",
165                          mmc_hostname(host), mrq->stop->opcode,
166                          mrq->stop->arg, mrq->stop->flags);
167         }
168
169         WARN_ON(!host->claimed);
170
171         mrq->cmd->error = 0;
172         mrq->cmd->mrq = mrq;
173         if (mrq->data) {
174                 BUG_ON(mrq->data->blksz > host->max_blk_size);
175                 BUG_ON(mrq->data->blocks > host->max_blk_count);
176                 BUG_ON(mrq->data->blocks * mrq->data->blksz >
177                         host->max_req_size);
178
179 #ifdef CONFIG_MMC_DEBUG
180                 sz = 0;
181                 for_each_sg(mrq->data->sg, sg, mrq->data->sg_len, i)
182                         sz += sg->length;
183                 BUG_ON(sz != mrq->data->blocks * mrq->data->blksz);
184 #endif
185
186                 mrq->cmd->data = mrq->data;
187                 mrq->data->error = 0;
188                 mrq->data->mrq = mrq;
189                 if (mrq->stop) {
190                         mrq->data->stop = mrq->stop;
191                         mrq->stop->error = 0;
192                         mrq->stop->mrq = mrq;
193                 }
194         }
195         mmc_host_clk_hold(host);
196         led_trigger_event(host->led, LED_FULL);
197         host->ops->request(host, mrq);
198 }
199
200 static void mmc_wait_done(struct mmc_request *mrq)
201 {
202         complete(mrq->done_data);
203 }
204
205 /**
206  *      mmc_wait_for_req - start a request and wait for completion
207  *      @host: MMC host to start command
208  *      @mrq: MMC request to start
209  *
210  *      Start a new MMC custom command request for a host, and wait
211  *      for the command to complete. Does not attempt to parse the
212  *      response.
213  */
214 void mmc_wait_for_req(struct mmc_host *host, struct mmc_request *mrq)
215 {
216 #if defined(CONFIG_SDMMC_RK29) && !defined(CONFIG_SDMMC_RK29_OLD)
217         unsigned long datasize, waittime = 0xFFFF;
218         u32 multi, unit;
219 #endif
220
221         DECLARE_COMPLETION_ONSTACK(complete);
222
223         mrq->done_data = &complete;
224         mrq->done = mmc_wait_done;
225
226         mmc_start_request(host, mrq);
227
228 #if defined(CONFIG_SDMMC_RK29) && !defined(CONFIG_SDMMC_RK29_OLD)
229     if( strncmp( mmc_hostname(host) ,"mmc0" , strlen("mmc0")) ) 
230     {
231         multi = (mrq->cmd->retries>0)?mrq->cmd->retries:1;
232         waittime = wait_for_completion_timeout(&complete,HZ*7*multi); //sdio; for cmd dead. Modifyed by xbw at 2011-06-02
233     }
234     else
235     {   
236         //calculate the timeout value for SDMMC; added by xbw at 2011-09-27
237         if(mrq->data)
238         {
239             unit = 3*(1<<20);// unit=3MB
240             datasize = mrq->data->blksz*mrq->data->blocks;
241             multi = datasize/unit;
242             multi += (datasize%unit)?1:0;
243             multi = (multi>0) ? multi : 1;
244             multi += (mrq->cmd->retries>0)?1:0;
245             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.
246                                                                           //modifyed by xbw at 2011-10-08
247                                                                           //
248                                                                           //example:
249                                                                           //rk29_sdmmc_request_end..2336...   CMD12 wait busy timeout!!!!! ====xbw=[sd_mmc]====
250                                                                           //mmc_wait_for_req..236.. !!!!! wait for CMD25 timeout ===xbw[mmc0]===
251         }
252         else
253         {
254             multi = (mrq->cmd->retries>0)?mrq->cmd->retries:1;
255             waittime = wait_for_completion_timeout(&complete,HZ*7*multi);
256         }
257     }
258     
259     if(waittime <= 1)
260     {
261         host->doneflag = 0;
262         mrq->cmd->error = -EIO;
263         printk(KERN_WARNING "%s..%d.. !!!!! wait for CMD%d timeout [%s]\n",\
264             __FUNCTION__, __LINE__, mrq->cmd->opcode, mmc_hostname(host));
265     }
266 #else
267         wait_for_completion(&complete);
268 #endif
269 }
270
271 EXPORT_SYMBOL(mmc_wait_for_req);
272
273 /**
274  *      mmc_wait_for_cmd - start a command and wait for completion
275  *      @host: MMC host to start command
276  *      @cmd: MMC command to start
277  *      @retries: maximum number of retries
278  *
279  *      Start a new MMC command for a host, and wait for the command
280  *      to complete.  Return any error that occurred while the command
281  *      was executing.  Do not attempt to parse the response.
282  */
283 int mmc_wait_for_cmd(struct mmc_host *host, struct mmc_command *cmd, int retries)
284 {
285         struct mmc_request mrq = {0};
286
287         WARN_ON(!host->claimed);
288
289         memset(cmd->resp, 0, sizeof(cmd->resp));
290         cmd->retries = retries;
291
292         mrq.cmd = cmd;
293         cmd->data = NULL;
294
295         mmc_wait_for_req(host, &mrq);
296
297         return cmd->error;
298 }
299
300 EXPORT_SYMBOL(mmc_wait_for_cmd);
301
302 /**
303  *      mmc_set_data_timeout - set the timeout for a data command
304  *      @data: data phase for command
305  *      @card: the MMC card associated with the data transfer
306  *
307  *      Computes the data timeout parameters according to the
308  *      correct algorithm given the card type.
309  */
310 void mmc_set_data_timeout(struct mmc_data *data, const struct mmc_card *card)
311 {
312         unsigned int mult;
313
314         /*
315          * SDIO cards only define an upper 1 s limit on access.
316          */
317         if (mmc_card_sdio(card)) {
318                 data->timeout_ns = 1000000000;
319                 data->timeout_clks = 0;
320                 return;
321         }
322
323         /*
324          * SD cards use a 100 multiplier rather than 10
325          */
326         mult = mmc_card_sd(card) ? 100 : 10;
327
328         /*
329          * Scale up the multiplier (and therefore the timeout) by
330          * the r2w factor for writes.
331          */
332         if (data->flags & MMC_DATA_WRITE)
333                 mult <<= card->csd.r2w_factor;
334
335         data->timeout_ns = card->csd.tacc_ns * mult;
336         data->timeout_clks = card->csd.tacc_clks * mult;
337
338         /*
339          * SD cards also have an upper limit on the timeout.
340          */
341         if (mmc_card_sd(card)) {
342                 unsigned int timeout_us, limit_us;
343
344                 timeout_us = data->timeout_ns / 1000;
345                 if (mmc_host_clk_rate(card->host))
346                         timeout_us += data->timeout_clks * 1000 /
347                                 (mmc_host_clk_rate(card->host) / 1000);
348
349                 if (data->flags & MMC_DATA_WRITE)
350                         /*
351                          * The limit is really 250 ms, but that is
352                          * insufficient for some crappy cards.
353                          */
354                         limit_us = 300000;
355                 else
356                         limit_us = 100000;
357
358                 /*
359                  * SDHC cards always use these fixed values.
360                  */
361                 if (timeout_us > limit_us || mmc_card_blockaddr(card)) {
362                         data->timeout_ns = limit_us * 1000;
363                         data->timeout_clks = 0;
364                 }
365         }
366         /*
367          * Some cards need very high timeouts if driven in SPI mode.
368          * The worst observed timeout was 900ms after writing a
369          * continuous stream of data until the internal logic
370          * overflowed.
371          */
372         if (mmc_host_is_spi(card->host)) {
373                 if (data->flags & MMC_DATA_WRITE) {
374                         if (data->timeout_ns < 1000000000)
375                                 data->timeout_ns = 1000000000;  /* 1s */
376                 } else {
377                         if (data->timeout_ns < 100000000)
378                                 data->timeout_ns =  100000000;  /* 100ms */
379                 }
380         }
381 }
382 EXPORT_SYMBOL(mmc_set_data_timeout);
383
384 /**
385  *      mmc_align_data_size - pads a transfer size to a more optimal value
386  *      @card: the MMC card associated with the data transfer
387  *      @sz: original transfer size
388  *
389  *      Pads the original data size with a number of extra bytes in
390  *      order to avoid controller bugs and/or performance hits
391  *      (e.g. some controllers revert to PIO for certain sizes).
392  *
393  *      Returns the improved size, which might be unmodified.
394  *
395  *      Note that this function is only relevant when issuing a
396  *      single scatter gather entry.
397  */
398 unsigned int mmc_align_data_size(struct mmc_card *card, unsigned int sz)
399 {
400         /*
401          * FIXME: We don't have a system for the controller to tell
402          * the core about its problems yet, so for now we just 32-bit
403          * align the size.
404          */
405         sz = ((sz + 3) / 4) * 4;
406
407         return sz;
408 }
409 EXPORT_SYMBOL(mmc_align_data_size);
410
411 /**
412  *      mmc_host_enable - enable a host.
413  *      @host: mmc host to enable
414  *
415  *      Hosts that support power saving can use the 'enable' and 'disable'
416  *      methods to exit and enter power saving states. For more information
417  *      see comments for struct mmc_host_ops.
418  */
419 int mmc_host_enable(struct mmc_host *host)
420 {
421         if (!(host->caps & MMC_CAP_DISABLE))
422                 return 0;
423
424         if (host->en_dis_recurs)
425                 return 0;
426
427         if (host->nesting_cnt++)
428                 return 0;
429
430         cancel_delayed_work_sync(&host->disable);
431
432         if (host->enabled)
433                 return 0;
434
435         if (host->ops->enable) {
436                 int err;
437
438                 host->en_dis_recurs = 1;
439                 err = host->ops->enable(host);
440                 host->en_dis_recurs = 0;
441
442                 if (err) {
443                         pr_debug("%s: enable error %d\n",
444                                  mmc_hostname(host), err);
445                         return err;
446                 }
447         }
448         host->enabled = 1;
449         return 0;
450 }
451 EXPORT_SYMBOL(mmc_host_enable);
452
453 static int mmc_host_do_disable(struct mmc_host *host, int lazy)
454 {
455         if (host->ops->disable) {
456                 int err;
457
458                 host->en_dis_recurs = 1;
459                 err = host->ops->disable(host, lazy);
460                 host->en_dis_recurs = 0;
461
462                 if (err < 0) {
463                         pr_debug("%s: disable error %d\n",
464                                  mmc_hostname(host), err);
465                         return err;
466                 }
467                 if (err > 0) {
468                         unsigned long delay = msecs_to_jiffies(err);
469
470                         mmc_schedule_delayed_work(&host->disable, delay);
471                 }
472         }
473         host->enabled = 0;
474         return 0;
475 }
476
477 /**
478  *      mmc_host_disable - disable a host.
479  *      @host: mmc host to disable
480  *
481  *      Hosts that support power saving can use the 'enable' and 'disable'
482  *      methods to exit and enter power saving states. For more information
483  *      see comments for struct mmc_host_ops.
484  */
485 int mmc_host_disable(struct mmc_host *host)
486 {
487         int err;
488
489         if (!(host->caps & MMC_CAP_DISABLE))
490                 return 0;
491
492         if (host->en_dis_recurs)
493                 return 0;
494
495         if (--host->nesting_cnt)
496                 return 0;
497
498         if (!host->enabled)
499                 return 0;
500
501         err = mmc_host_do_disable(host, 0);
502         return err;
503 }
504 EXPORT_SYMBOL(mmc_host_disable);
505
506 /**
507  *      __mmc_claim_host - exclusively claim a host
508  *      @host: mmc host to claim
509  *      @abort: whether or not the operation should be aborted
510  *
511  *      Claim a host for a set of operations.  If @abort is non null and
512  *      dereference a non-zero value then this will return prematurely with
513  *      that non-zero value without acquiring the lock.  Returns zero
514  *      with the lock held otherwise.
515  */
516 int __mmc_claim_host(struct mmc_host *host, atomic_t *abort)
517 {
518         DECLARE_WAITQUEUE(wait, current);
519         unsigned long flags;
520         int stop;
521
522         might_sleep();
523
524         add_wait_queue(&host->wq, &wait);
525         spin_lock_irqsave(&host->lock, flags);
526         while (1) {
527                 set_current_state(TASK_UNINTERRUPTIBLE);
528                 stop = abort ? atomic_read(abort) : 0;
529                 if (stop || !host->claimed || host->claimer == current)
530                         break;
531                 spin_unlock_irqrestore(&host->lock, flags);
532                 schedule();
533                 spin_lock_irqsave(&host->lock, flags);
534         }
535         set_current_state(TASK_RUNNING);
536         if (!stop) {
537                 host->claimed = 1;
538                 host->claimer = current;
539                 host->claim_cnt += 1;
540         } else
541                 wake_up(&host->wq);
542         spin_unlock_irqrestore(&host->lock, flags);
543         remove_wait_queue(&host->wq, &wait);
544         if (!stop)
545                 mmc_host_enable(host);
546         return stop;
547 }
548
549 EXPORT_SYMBOL(__mmc_claim_host);
550
551 /**
552  *      mmc_try_claim_host - try exclusively to claim a host
553  *      @host: mmc host to claim
554  *
555  *      Returns %1 if the host is claimed, %0 otherwise.
556  */
557 int mmc_try_claim_host(struct mmc_host *host)
558 {
559         int claimed_host = 0;
560         unsigned long flags;
561
562         spin_lock_irqsave(&host->lock, flags);
563         if (!host->claimed || host->claimer == current) {
564                 host->claimed = 1;
565                 host->claimer = current;
566                 host->claim_cnt += 1;
567                 claimed_host = 1;
568         }
569         spin_unlock_irqrestore(&host->lock, flags);
570         return claimed_host;
571 }
572 EXPORT_SYMBOL(mmc_try_claim_host);
573
574 /**
575  *      mmc_do_release_host - release a claimed host
576  *      @host: mmc host to release
577  *
578  *      If you successfully claimed a host, this function will
579  *      release it again.
580  */
581 void mmc_do_release_host(struct mmc_host *host)
582 {
583         unsigned long flags;
584
585         spin_lock_irqsave(&host->lock, flags);
586         if (--host->claim_cnt) {
587                 /* Release for nested claim */
588                 spin_unlock_irqrestore(&host->lock, flags);
589         } else {
590                 host->claimed = 0;
591                 host->claimer = NULL;
592                 spin_unlock_irqrestore(&host->lock, flags);
593                 wake_up(&host->wq);
594         }
595 }
596 EXPORT_SYMBOL(mmc_do_release_host);
597
598 void mmc_host_deeper_disable(struct work_struct *work)
599 {
600         struct mmc_host *host =
601                 container_of(work, struct mmc_host, disable.work);
602
603         /* If the host is claimed then we do not want to disable it anymore */
604         if (!mmc_try_claim_host(host))
605                 return;
606         mmc_host_do_disable(host, 1);
607         mmc_do_release_host(host);
608 }
609
610 /**
611  *      mmc_host_lazy_disable - lazily disable a host.
612  *      @host: mmc host to disable
613  *
614  *      Hosts that support power saving can use the 'enable' and 'disable'
615  *      methods to exit and enter power saving states. For more information
616  *      see comments for struct mmc_host_ops.
617  */
618 int mmc_host_lazy_disable(struct mmc_host *host)
619 {
620         if (!(host->caps & MMC_CAP_DISABLE))
621                 return 0;
622
623         if (host->en_dis_recurs)
624                 return 0;
625
626         if (--host->nesting_cnt)
627                 return 0;
628
629         if (!host->enabled)
630                 return 0;
631
632         if (host->disable_delay) {
633                 mmc_schedule_delayed_work(&host->disable,
634                                 msecs_to_jiffies(host->disable_delay));
635                 return 0;
636         } else
637                 return mmc_host_do_disable(host, 1);
638 }
639 EXPORT_SYMBOL(mmc_host_lazy_disable);
640
641 /**
642  *      mmc_release_host - release a host
643  *      @host: mmc host to release
644  *
645  *      Release a MMC host, allowing others to claim the host
646  *      for their operations.
647  */
648 void mmc_release_host(struct mmc_host *host)
649 {
650         WARN_ON(!host->claimed);
651
652         mmc_host_lazy_disable(host);
653
654         mmc_do_release_host(host);
655 }
656
657 EXPORT_SYMBOL(mmc_release_host);
658
659 /*
660  * Internal function that does the actual ios call to the host driver,
661  * optionally printing some debug output.
662  */
663 static inline void mmc_set_ios(struct mmc_host *host)
664 {
665         struct mmc_ios *ios = &host->ios;
666
667         pr_debug("%s: clock %uHz busmode %u powermode %u cs %u Vdd %u "
668                 "width %u timing %u\n",
669                  mmc_hostname(host), ios->clock, ios->bus_mode,
670                  ios->power_mode, ios->chip_select, ios->vdd,
671                  ios->bus_width, ios->timing);
672
673         if (ios->clock > 0)
674                 mmc_set_ungated(host);
675         host->ops->set_ios(host, ios);
676 }
677
678 /*
679  * Control chip select pin on a host.
680  */
681 void mmc_set_chip_select(struct mmc_host *host, int mode)
682 {
683         mmc_host_clk_hold(host);
684         host->ios.chip_select = mode;
685         mmc_set_ios(host);
686         mmc_host_clk_release(host);
687 }
688
689 /*
690  * Sets the host clock to the highest possible frequency that
691  * is below "hz".
692  */
693 static void __mmc_set_clock(struct mmc_host *host, unsigned int hz)
694 {
695         WARN_ON(hz < host->f_min);
696
697         if (hz > host->f_max)
698                 hz = host->f_max;
699
700         host->ios.clock = hz;
701         mmc_set_ios(host);
702 }
703
704 void mmc_set_clock(struct mmc_host *host, unsigned int hz)
705 {
706         mmc_host_clk_hold(host);
707         __mmc_set_clock(host, hz);
708         mmc_host_clk_release(host);
709 }
710
711 #ifdef CONFIG_MMC_CLKGATE
712 /*
713  * This gates the clock by setting it to 0 Hz.
714  */
715 void mmc_gate_clock(struct mmc_host *host)
716 {
717         unsigned long flags;
718
719         spin_lock_irqsave(&host->clk_lock, flags);
720         host->clk_old = host->ios.clock;
721         host->ios.clock = 0;
722         host->clk_gated = true;
723         spin_unlock_irqrestore(&host->clk_lock, flags);
724         mmc_set_ios(host);
725 }
726
727 /*
728  * This restores the clock from gating by using the cached
729  * clock value.
730  */
731 void mmc_ungate_clock(struct mmc_host *host)
732 {
733         /*
734          * We should previously have gated the clock, so the clock shall
735          * be 0 here! The clock may however be 0 during initialization,
736          * when some request operations are performed before setting
737          * the frequency. When ungate is requested in that situation
738          * we just ignore the call.
739          */
740         if (host->clk_old) {
741                 BUG_ON(host->ios.clock);
742                 /* This call will also set host->clk_gated to false */
743                 __mmc_set_clock(host, host->clk_old);
744         }
745 }
746
747 void mmc_set_ungated(struct mmc_host *host)
748 {
749         unsigned long flags;
750
751         /*
752          * We've been given a new frequency while the clock is gated,
753          * so make sure we regard this as ungating it.
754          */
755         spin_lock_irqsave(&host->clk_lock, flags);
756         host->clk_gated = false;
757         spin_unlock_irqrestore(&host->clk_lock, flags);
758 }
759
760 #else
761 void mmc_set_ungated(struct mmc_host *host)
762 {
763 }
764 #endif
765
766 /*
767  * Change the bus mode (open drain/push-pull) of a host.
768  */
769 void mmc_set_bus_mode(struct mmc_host *host, unsigned int mode)
770 {
771         mmc_host_clk_hold(host);
772         host->ios.bus_mode = mode;
773         mmc_set_ios(host);
774         mmc_host_clk_release(host);
775 }
776
777 /*
778  * Change data bus width of a host.
779  */
780 void mmc_set_bus_width(struct mmc_host *host, unsigned int width)
781 {
782         mmc_host_clk_hold(host);
783         host->ios.bus_width = width;
784         mmc_set_ios(host);
785         mmc_host_clk_release(host);
786 }
787
788 /**
789  * mmc_vdd_to_ocrbitnum - Convert a voltage to the OCR bit number
790  * @vdd:        voltage (mV)
791  * @low_bits:   prefer low bits in boundary cases
792  *
793  * This function returns the OCR bit number according to the provided @vdd
794  * value. If conversion is not possible a negative errno value returned.
795  *
796  * Depending on the @low_bits flag the function prefers low or high OCR bits
797  * on boundary voltages. For example,
798  * with @low_bits = true, 3300 mV translates to ilog2(MMC_VDD_32_33);
799  * with @low_bits = false, 3300 mV translates to ilog2(MMC_VDD_33_34);
800  *
801  * Any value in the [1951:1999] range translates to the ilog2(MMC_VDD_20_21).
802  */
803 static int mmc_vdd_to_ocrbitnum(int vdd, bool low_bits)
804 {
805         const int max_bit = ilog2(MMC_VDD_35_36);
806         int bit;
807
808         if (vdd < 1650 || vdd > 3600)
809                 return -EINVAL;
810
811         if (vdd >= 1650 && vdd <= 1950)
812                 return ilog2(MMC_VDD_165_195);
813
814         if (low_bits)
815                 vdd -= 1;
816
817         /* Base 2000 mV, step 100 mV, bit's base 8. */
818         bit = (vdd - 2000) / 100 + 8;
819         if (bit > max_bit)
820                 return max_bit;
821         return bit;
822 }
823
824 /**
825  * mmc_vddrange_to_ocrmask - Convert a voltage range to the OCR mask
826  * @vdd_min:    minimum voltage value (mV)
827  * @vdd_max:    maximum voltage value (mV)
828  *
829  * This function returns the OCR mask bits according to the provided @vdd_min
830  * and @vdd_max values. If conversion is not possible the function returns 0.
831  *
832  * Notes wrt boundary cases:
833  * This function sets the OCR bits for all boundary voltages, for example
834  * [3300:3400] range is translated to MMC_VDD_32_33 | MMC_VDD_33_34 |
835  * MMC_VDD_34_35 mask.
836  */
837 u32 mmc_vddrange_to_ocrmask(int vdd_min, int vdd_max)
838 {
839         u32 mask = 0;
840
841         if (vdd_max < vdd_min)
842                 return 0;
843
844         /* Prefer high bits for the boundary vdd_max values. */
845         vdd_max = mmc_vdd_to_ocrbitnum(vdd_max, false);
846         if (vdd_max < 0)
847                 return 0;
848
849         /* Prefer low bits for the boundary vdd_min values. */
850         vdd_min = mmc_vdd_to_ocrbitnum(vdd_min, true);
851         if (vdd_min < 0)
852                 return 0;
853
854         /* Fill the mask, from max bit to min bit. */
855         while (vdd_max >= vdd_min)
856                 mask |= 1 << vdd_max--;
857
858         return mask;
859 }
860 EXPORT_SYMBOL(mmc_vddrange_to_ocrmask);
861
862 #ifdef CONFIG_REGULATOR
863
864 /**
865  * mmc_regulator_get_ocrmask - return mask of supported voltages
866  * @supply: regulator to use
867  *
868  * This returns either a negative errno, or a mask of voltages that
869  * can be provided to MMC/SD/SDIO devices using the specified voltage
870  * regulator.  This would normally be called before registering the
871  * MMC host adapter.
872  */
873 int mmc_regulator_get_ocrmask(struct regulator *supply)
874 {
875         int                     result = 0;
876         int                     count;
877         int                     i;
878
879         count = regulator_count_voltages(supply);
880         if (count < 0)
881                 return count;
882
883         for (i = 0; i < count; i++) {
884                 int             vdd_uV;
885                 int             vdd_mV;
886
887                 vdd_uV = regulator_list_voltage(supply, i);
888                 if (vdd_uV <= 0)
889                         continue;
890
891                 vdd_mV = vdd_uV / 1000;
892                 result |= mmc_vddrange_to_ocrmask(vdd_mV, vdd_mV);
893         }
894
895         return result;
896 }
897 EXPORT_SYMBOL(mmc_regulator_get_ocrmask);
898
899 /**
900  * mmc_regulator_set_ocr - set regulator to match host->ios voltage
901  * @mmc: the host to regulate
902  * @supply: regulator to use
903  * @vdd_bit: zero for power off, else a bit number (host->ios.vdd)
904  *
905  * Returns zero on success, else negative errno.
906  *
907  * MMC host drivers may use this to enable or disable a regulator using
908  * a particular supply voltage.  This would normally be called from the
909  * set_ios() method.
910  */
911 int mmc_regulator_set_ocr(struct mmc_host *mmc,
912                         struct regulator *supply,
913                         unsigned short vdd_bit)
914 {
915         int                     result = 0;
916         int                     min_uV, max_uV;
917
918         if (vdd_bit) {
919                 int             tmp;
920                 int             voltage;
921
922                 /* REVISIT mmc_vddrange_to_ocrmask() may have set some
923                  * bits this regulator doesn't quite support ... don't
924                  * be too picky, most cards and regulators are OK with
925                  * a 0.1V range goof (it's a small error percentage).
926                  */
927                 tmp = vdd_bit - ilog2(MMC_VDD_165_195);
928                 if (tmp == 0) {
929                         min_uV = 1650 * 1000;
930                         max_uV = 1950 * 1000;
931                 } else {
932                         min_uV = 1900 * 1000 + tmp * 100 * 1000;
933                         max_uV = min_uV + 100 * 1000;
934                 }
935
936                 /* avoid needless changes to this voltage; the regulator
937                  * might not allow this operation
938                  */
939                 voltage = regulator_get_voltage(supply);
940                 if (voltage < 0)
941                         result = voltage;
942                 else if (voltage < min_uV || voltage > max_uV)
943                         result = regulator_set_voltage(supply, min_uV, max_uV);
944                 else
945                         result = 0;
946
947                 if (result == 0 && !mmc->regulator_enabled) {
948                         result = regulator_enable(supply);
949                         if (!result)
950                                 mmc->regulator_enabled = true;
951                 }
952         } else if (mmc->regulator_enabled) {
953                 result = regulator_disable(supply);
954                 if (result == 0)
955                         mmc->regulator_enabled = false;
956         }
957
958         if (result)
959                 dev_err(mmc_dev(mmc),
960                         "could not set regulator OCR (%d)\n", result);
961         return result;
962 }
963 EXPORT_SYMBOL(mmc_regulator_set_ocr);
964
965 #endif /* CONFIG_REGULATOR */
966
967 /*
968  * Mask off any voltages we don't support and select
969  * the lowest voltage
970  */
971 u32 mmc_select_voltage(struct mmc_host *host, u32 ocr)
972 {
973         int bit;
974
975         ocr &= host->ocr_avail;
976
977         bit = ffs(ocr);
978         if (bit) {
979                 bit -= 1;
980
981                 ocr &= 3 << bit;
982
983                 mmc_host_clk_hold(host);
984                 host->ios.vdd = bit;
985                 mmc_set_ios(host);
986                 mmc_host_clk_release(host);
987         } else {
988                 pr_warning("%s: host doesn't support card's voltages\n",
989                                 mmc_hostname(host));
990                 ocr = 0;
991         }
992
993         return ocr;
994 }
995
996 int mmc_set_signal_voltage(struct mmc_host *host, int signal_voltage, bool cmd11)
997 {
998         struct mmc_command cmd = {0};
999         int err = 0;
1000
1001         BUG_ON(!host);
1002
1003         /*
1004          * Send CMD11 only if the request is to switch the card to
1005          * 1.8V signalling.
1006          */
1007         if ((signal_voltage != MMC_SIGNAL_VOLTAGE_330) && cmd11) {
1008                 cmd.opcode = SD_SWITCH_VOLTAGE;
1009                 cmd.arg = 0;
1010                 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
1011
1012                 err = mmc_wait_for_cmd(host, &cmd, 0);
1013                 if (err)
1014                         return err;
1015
1016                 if (!mmc_host_is_spi(host) && (cmd.resp[0] & R1_ERROR))
1017                         return -EIO;
1018         }
1019
1020         host->ios.signal_voltage = signal_voltage;
1021
1022         if (host->ops->start_signal_voltage_switch)
1023                 err = host->ops->start_signal_voltage_switch(host, &host->ios);
1024
1025         return err;
1026 }
1027
1028 /*
1029  * Select timing parameters for host.
1030  */
1031 void mmc_set_timing(struct mmc_host *host, unsigned int timing)
1032 {
1033         mmc_host_clk_hold(host);
1034         host->ios.timing = timing;
1035         mmc_set_ios(host);
1036         mmc_host_clk_release(host);
1037 }
1038
1039 /*
1040  * Select appropriate driver type for host.
1041  */
1042 void mmc_set_driver_type(struct mmc_host *host, unsigned int drv_type)
1043 {
1044         mmc_host_clk_hold(host);
1045         host->ios.drv_type = drv_type;
1046         mmc_set_ios(host);
1047         mmc_host_clk_release(host);
1048 }
1049
1050 /*
1051  * Apply power to the MMC stack.  This is a two-stage process.
1052  * First, we enable power to the card without the clock running.
1053  * We then wait a bit for the power to stabilise.  Finally,
1054  * enable the bus drivers and clock to the card.
1055  *
1056  * We must _NOT_ enable the clock prior to power stablising.
1057  *
1058  * If a host does all the power sequencing itself, ignore the
1059  * initial MMC_POWER_UP stage.
1060  */
1061 static void mmc_power_up(struct mmc_host *host)
1062 {
1063         int bit;
1064
1065         mmc_host_clk_hold(host);
1066
1067         /* If ocr is set, we use it */
1068         if (host->ocr)
1069                 bit = ffs(host->ocr) - 1;
1070         else
1071                 bit = fls(host->ocr_avail) - 1;
1072
1073         host->ios.vdd = bit;
1074         if (mmc_host_is_spi(host)) {
1075                 host->ios.chip_select = MMC_CS_HIGH;
1076                 host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
1077         } else {
1078                 host->ios.chip_select = MMC_CS_DONTCARE;
1079                 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
1080         }
1081         host->ios.power_mode = MMC_POWER_UP;
1082         host->ios.bus_width = MMC_BUS_WIDTH_1;
1083         host->ios.timing = MMC_TIMING_LEGACY;
1084         mmc_set_ios(host);
1085
1086         /*
1087          * This delay should be sufficient to allow the power supply
1088          * to reach the minimum voltage.
1089          */
1090         mmc_delay(10);
1091         
1092 #if defined(CONFIG_SDMMC_RK29) || !defined(CONFIG_SDMMC_RK29_OLD)   //Modifyed by xbw at 2011-11-17
1093     host->ios.clock = host->f_min;
1094 #else
1095         host->ios.clock = host->f_init;
1096 #endif
1097
1098         host->ios.power_mode = MMC_POWER_ON;
1099         mmc_set_ios(host);
1100
1101         /*
1102          * This delay must be at least 74 clock sizes, or 1 ms, or the
1103          * time required to reach a stable voltage.
1104          */
1105         mmc_delay(10);
1106
1107         mmc_host_clk_release(host);
1108 }
1109
1110 void mmc_power_off(struct mmc_host *host)
1111 {
1112         mmc_host_clk_hold(host);
1113
1114         host->ios.clock = 0;
1115         host->ios.vdd = 0;
1116
1117         /*
1118          * Reset ocr mask to be the highest possible voltage supported for
1119          * this mmc host. This value will be used at next power up.
1120          */
1121         host->ocr = 1 << (fls(host->ocr_avail) - 1);
1122
1123         if (!mmc_host_is_spi(host)) {
1124                 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
1125                 host->ios.chip_select = MMC_CS_DONTCARE;
1126         }
1127         host->ios.power_mode = MMC_POWER_OFF;
1128         host->ios.bus_width = MMC_BUS_WIDTH_1;
1129         host->ios.timing = MMC_TIMING_LEGACY;
1130         mmc_set_ios(host);
1131
1132         mmc_host_clk_release(host);
1133 }
1134
1135 /*
1136  * Cleanup when the last reference to the bus operator is dropped.
1137  */
1138 static void __mmc_release_bus(struct mmc_host *host)
1139 {
1140         BUG_ON(!host);
1141         BUG_ON(host->bus_refs);
1142         BUG_ON(!host->bus_dead);
1143
1144         host->bus_ops = NULL;
1145 }
1146
1147 /*
1148  * Increase reference count of bus operator
1149  */
1150 static inline void mmc_bus_get(struct mmc_host *host)
1151 {
1152         unsigned long flags;
1153
1154         spin_lock_irqsave(&host->lock, flags);
1155         host->bus_refs++;
1156         spin_unlock_irqrestore(&host->lock, flags);
1157 }
1158
1159 /*
1160  * Decrease reference count of bus operator and free it if
1161  * it is the last reference.
1162  */
1163 static inline void mmc_bus_put(struct mmc_host *host)
1164 {
1165         unsigned long flags;
1166
1167         spin_lock_irqsave(&host->lock, flags);
1168         host->bus_refs--;
1169         if ((host->bus_refs == 0) && host->bus_ops)
1170                 __mmc_release_bus(host);
1171         spin_unlock_irqrestore(&host->lock, flags);
1172 }
1173
1174 int mmc_resume_bus(struct mmc_host *host)
1175 {
1176         unsigned long flags;
1177
1178         if (!mmc_bus_needs_resume(host))
1179                 return -EINVAL;
1180
1181         printk("%s: Starting deferred resume\n", mmc_hostname(host));
1182         spin_lock_irqsave(&host->lock, flags);
1183         host->bus_resume_flags &= ~MMC_BUSRESUME_NEEDS_RESUME;
1184         host->rescan_disable = 0;
1185         spin_unlock_irqrestore(&host->lock, flags);
1186
1187         mmc_bus_get(host);
1188         if (host->bus_ops && !host->bus_dead) {
1189                 mmc_power_up(host);
1190                 BUG_ON(!host->bus_ops->resume);
1191                 host->bus_ops->resume(host);
1192         }
1193
1194         if (host->bus_ops->detect && !host->bus_dead)
1195                 host->bus_ops->detect(host);
1196
1197         mmc_bus_put(host);
1198         printk("%s: Deferred resume completed\n", mmc_hostname(host));
1199         return 0;
1200 }
1201
1202 EXPORT_SYMBOL(mmc_resume_bus);
1203
1204 /*
1205  * Assign a mmc bus handler to a host. Only one bus handler may control a
1206  * host at any given time.
1207  */
1208 void mmc_attach_bus(struct mmc_host *host, const struct mmc_bus_ops *ops)
1209 {
1210         unsigned long flags;
1211
1212         BUG_ON(!host);
1213         BUG_ON(!ops);
1214
1215         WARN_ON(!host->claimed);
1216
1217         spin_lock_irqsave(&host->lock, flags);
1218
1219         BUG_ON(host->bus_ops);
1220         BUG_ON(host->bus_refs);
1221
1222         host->bus_ops = ops;
1223         host->bus_refs = 1;
1224         host->bus_dead = 0;
1225
1226         spin_unlock_irqrestore(&host->lock, flags);
1227 }
1228
1229 /*
1230  * Remove the current bus handler from a host.
1231  */
1232 void mmc_detach_bus(struct mmc_host *host)
1233 {
1234         unsigned long flags;
1235
1236         BUG_ON(!host);
1237
1238         WARN_ON(!host->claimed);
1239         WARN_ON(!host->bus_ops);
1240
1241         spin_lock_irqsave(&host->lock, flags);
1242
1243         host->bus_dead = 1;
1244
1245         spin_unlock_irqrestore(&host->lock, flags);
1246
1247         mmc_bus_put(host);
1248 }
1249
1250 /**
1251  *      mmc_detect_change - process change of state on a MMC socket
1252  *      @host: host which changed state.
1253  *      @delay: optional delay to wait before detection (jiffies)
1254  *
1255  *      MMC drivers should call this when they detect a card has been
1256  *      inserted or removed. The MMC layer will confirm that any
1257  *      present card is still functional, and initialize any newly
1258  *      inserted.
1259  */
1260 void mmc_detect_change(struct mmc_host *host, unsigned long delay)
1261 {
1262 #ifdef CONFIG_MMC_DEBUG
1263         unsigned long flags;
1264         spin_lock_irqsave(&host->lock, flags);
1265         WARN_ON(host->removed);
1266         spin_unlock_irqrestore(&host->lock, flags);
1267 #endif
1268
1269         wake_lock(&host->detect_wake_lock);
1270         mmc_schedule_delayed_work(&host->detect, delay);
1271 }
1272
1273 EXPORT_SYMBOL(mmc_detect_change);
1274
1275 void mmc_init_erase(struct mmc_card *card)
1276 {
1277         unsigned int sz;
1278
1279         if (is_power_of_2(card->erase_size))
1280                 card->erase_shift = ffs(card->erase_size) - 1;
1281         else
1282                 card->erase_shift = 0;
1283
1284         /*
1285          * It is possible to erase an arbitrarily large area of an SD or MMC
1286          * card.  That is not desirable because it can take a long time
1287          * (minutes) potentially delaying more important I/O, and also the
1288          * timeout calculations become increasingly hugely over-estimated.
1289          * Consequently, 'pref_erase' is defined as a guide to limit erases
1290          * to that size and alignment.
1291          *
1292          * For SD cards that define Allocation Unit size, limit erases to one
1293          * Allocation Unit at a time.  For MMC cards that define High Capacity
1294          * Erase Size, whether it is switched on or not, limit to that size.
1295          * Otherwise just have a stab at a good value.  For modern cards it
1296          * will end up being 4MiB.  Note that if the value is too small, it
1297          * can end up taking longer to erase.
1298          */
1299         if (mmc_card_sd(card) && card->ssr.au) {
1300                 card->pref_erase = card->ssr.au;
1301                 card->erase_shift = ffs(card->ssr.au) - 1;
1302         } else if (card->ext_csd.hc_erase_size) {
1303                 card->pref_erase = card->ext_csd.hc_erase_size;
1304         } else {
1305                 sz = (card->csd.capacity << (card->csd.read_blkbits - 9)) >> 11;
1306                 if (sz < 128)
1307                         card->pref_erase = 512 * 1024 / 512;
1308                 else if (sz < 512)
1309                         card->pref_erase = 1024 * 1024 / 512;
1310                 else if (sz < 1024)
1311                         card->pref_erase = 2 * 1024 * 1024 / 512;
1312                 else
1313                         card->pref_erase = 4 * 1024 * 1024 / 512;
1314                 if (card->pref_erase < card->erase_size)
1315                         card->pref_erase = card->erase_size;
1316                 else {
1317                         sz = card->pref_erase % card->erase_size;
1318                         if (sz)
1319                                 card->pref_erase += card->erase_size - sz;
1320                 }
1321         }
1322 }
1323
1324 static unsigned int mmc_mmc_erase_timeout(struct mmc_card *card,
1325                                           unsigned int arg, unsigned int qty)
1326 {
1327         unsigned int erase_timeout;
1328
1329         if (card->ext_csd.erase_group_def & 1) {
1330                 /* High Capacity Erase Group Size uses HC timeouts */
1331                 if (arg == MMC_TRIM_ARG)
1332                         erase_timeout = card->ext_csd.trim_timeout;
1333                 else
1334                         erase_timeout = card->ext_csd.hc_erase_timeout;
1335         } else {
1336                 /* CSD Erase Group Size uses write timeout */
1337                 unsigned int mult = (10 << card->csd.r2w_factor);
1338                 unsigned int timeout_clks = card->csd.tacc_clks * mult;
1339                 unsigned int timeout_us;
1340
1341                 /* Avoid overflow: e.g. tacc_ns=80000000 mult=1280 */
1342                 if (card->csd.tacc_ns < 1000000)
1343                         timeout_us = (card->csd.tacc_ns * mult) / 1000;
1344                 else
1345                         timeout_us = (card->csd.tacc_ns / 1000) * mult;
1346
1347                 /*
1348                  * ios.clock is only a target.  The real clock rate might be
1349                  * less but not that much less, so fudge it by multiplying by 2.
1350                  */
1351                 timeout_clks <<= 1;
1352                 timeout_us += (timeout_clks * 1000) /
1353                               (mmc_host_clk_rate(card->host) / 1000);
1354
1355                 erase_timeout = timeout_us / 1000;
1356
1357                 /*
1358                  * Theoretically, the calculation could underflow so round up
1359                  * to 1ms in that case.
1360                  */
1361                 if (!erase_timeout)
1362                         erase_timeout = 1;
1363         }
1364
1365         /* Multiplier for secure operations */
1366         if (arg & MMC_SECURE_ARGS) {
1367                 if (arg == MMC_SECURE_ERASE_ARG)
1368                         erase_timeout *= card->ext_csd.sec_erase_mult;
1369                 else
1370                         erase_timeout *= card->ext_csd.sec_trim_mult;
1371         }
1372
1373         erase_timeout *= qty;
1374
1375         /*
1376          * Ensure at least a 1 second timeout for SPI as per
1377          * 'mmc_set_data_timeout()'
1378          */
1379         if (mmc_host_is_spi(card->host) && erase_timeout < 1000)
1380                 erase_timeout = 1000;
1381
1382         return erase_timeout;
1383 }
1384
1385 static unsigned int mmc_sd_erase_timeout(struct mmc_card *card,
1386                                          unsigned int arg,
1387                                          unsigned int qty)
1388 {
1389         unsigned int erase_timeout;
1390
1391         if (card->ssr.erase_timeout) {
1392                 /* Erase timeout specified in SD Status Register (SSR) */
1393                 erase_timeout = card->ssr.erase_timeout * qty +
1394                                 card->ssr.erase_offset;
1395         } else {
1396                 /*
1397                  * Erase timeout not specified in SD Status Register (SSR) so
1398                  * use 250ms per write block.
1399                  */
1400                 erase_timeout = 250 * qty;
1401         }
1402
1403         /* Must not be less than 1 second */
1404         if (erase_timeout < 1000)
1405                 erase_timeout = 1000;
1406
1407         return erase_timeout;
1408 }
1409
1410 static unsigned int mmc_erase_timeout(struct mmc_card *card,
1411                                       unsigned int arg,
1412                                       unsigned int qty)
1413 {
1414         if (mmc_card_sd(card))
1415                 return mmc_sd_erase_timeout(card, arg, qty);
1416         else
1417                 return mmc_mmc_erase_timeout(card, arg, qty);
1418 }
1419
1420 static int mmc_do_erase(struct mmc_card *card, unsigned int from,
1421                         unsigned int to, unsigned int arg)
1422 {
1423         struct mmc_command cmd = {0};
1424         unsigned int qty = 0;
1425         int err;
1426
1427         /*
1428          * qty is used to calculate the erase timeout which depends on how many
1429          * erase groups (or allocation units in SD terminology) are affected.
1430          * We count erasing part of an erase group as one erase group.
1431          * For SD, the allocation units are always a power of 2.  For MMC, the
1432          * erase group size is almost certainly also power of 2, but it does not
1433          * seem to insist on that in the JEDEC standard, so we fall back to
1434          * division in that case.  SD may not specify an allocation unit size,
1435          * in which case the timeout is based on the number of write blocks.
1436          *
1437          * Note that the timeout for secure trim 2 will only be correct if the
1438          * number of erase groups specified is the same as the total of all
1439          * preceding secure trim 1 commands.  Since the power may have been
1440          * lost since the secure trim 1 commands occurred, it is generally
1441          * impossible to calculate the secure trim 2 timeout correctly.
1442          */
1443         if (card->erase_shift)
1444                 qty += ((to >> card->erase_shift) -
1445                         (from >> card->erase_shift)) + 1;
1446         else if (mmc_card_sd(card))
1447                 qty += to - from + 1;
1448         else
1449                 qty += ((to / card->erase_size) -
1450                         (from / card->erase_size)) + 1;
1451
1452         if (!mmc_card_blockaddr(card)) {
1453                 from <<= 9;
1454                 to <<= 9;
1455         }
1456
1457         if (mmc_card_sd(card))
1458                 cmd.opcode = SD_ERASE_WR_BLK_START;
1459         else
1460                 cmd.opcode = MMC_ERASE_GROUP_START;
1461         cmd.arg = from;
1462         cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
1463         err = mmc_wait_for_cmd(card->host, &cmd, 0);
1464         if (err) {
1465                 printk(KERN_ERR "mmc_erase: group start error %d, "
1466                        "status %#x\n", err, cmd.resp[0]);
1467                 err = -EINVAL;
1468                 goto out;
1469         }
1470
1471         memset(&cmd, 0, sizeof(struct mmc_command));
1472         if (mmc_card_sd(card))
1473                 cmd.opcode = SD_ERASE_WR_BLK_END;
1474         else
1475                 cmd.opcode = MMC_ERASE_GROUP_END;
1476         cmd.arg = to;
1477         cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
1478         err = mmc_wait_for_cmd(card->host, &cmd, 0);
1479         if (err) {
1480                 printk(KERN_ERR "mmc_erase: group end error %d, status %#x\n",
1481                        err, cmd.resp[0]);
1482                 err = -EINVAL;
1483                 goto out;
1484         }
1485
1486         memset(&cmd, 0, sizeof(struct mmc_command));
1487         cmd.opcode = MMC_ERASE;
1488         cmd.arg = arg;
1489         cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1490         cmd.cmd_timeout_ms = mmc_erase_timeout(card, arg, qty);
1491         err = mmc_wait_for_cmd(card->host, &cmd, 0);
1492         if (err) {
1493                 printk(KERN_ERR "mmc_erase: erase error %d, status %#x\n",
1494                        err, cmd.resp[0]);
1495                 err = -EIO;
1496                 goto out;
1497         }
1498
1499         if (mmc_host_is_spi(card->host))
1500                 goto out;
1501
1502         do {
1503                 memset(&cmd, 0, sizeof(struct mmc_command));
1504                 cmd.opcode = MMC_SEND_STATUS;
1505                 cmd.arg = card->rca << 16;
1506                 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
1507                 /* Do not retry else we can't see errors */
1508                 err = mmc_wait_for_cmd(card->host, &cmd, 0);
1509                 if (err || (cmd.resp[0] & 0xFDF92000)) {
1510                         printk(KERN_ERR "error %d requesting status %#x\n",
1511                                 err, cmd.resp[0]);
1512                         err = -EIO;
1513                         goto out;
1514                 }
1515         } while (!(cmd.resp[0] & R1_READY_FOR_DATA) ||
1516                  R1_CURRENT_STATE(cmd.resp[0]) == 7);
1517 out:
1518         return err;
1519 }
1520
1521 /**
1522  * mmc_erase - erase sectors.
1523  * @card: card to erase
1524  * @from: first sector to erase
1525  * @nr: number of sectors to erase
1526  * @arg: erase command argument (SD supports only %MMC_ERASE_ARG)
1527  *
1528  * Caller must claim host before calling this function.
1529  */
1530 int mmc_erase(struct mmc_card *card, unsigned int from, unsigned int nr,
1531               unsigned int arg)
1532 {
1533         unsigned int rem, to = from + nr;
1534
1535         if (!(card->host->caps & MMC_CAP_ERASE) ||
1536             !(card->csd.cmdclass & CCC_ERASE))
1537                 return -EOPNOTSUPP;
1538
1539         if (!card->erase_size)
1540                 return -EOPNOTSUPP;
1541
1542         if (mmc_card_sd(card) && arg != MMC_ERASE_ARG)
1543                 return -EOPNOTSUPP;
1544
1545         if ((arg & MMC_SECURE_ARGS) &&
1546             !(card->ext_csd.sec_feature_support & EXT_CSD_SEC_ER_EN))
1547                 return -EOPNOTSUPP;
1548
1549         if ((arg & MMC_TRIM_ARGS) &&
1550             !(card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN))
1551                 return -EOPNOTSUPP;
1552
1553         if (arg == MMC_SECURE_ERASE_ARG) {
1554                 if (from % card->erase_size || nr % card->erase_size)
1555                         return -EINVAL;
1556         }
1557
1558         if (arg == MMC_ERASE_ARG) {
1559                 rem = from % card->erase_size;
1560                 if (rem) {
1561                         rem = card->erase_size - rem;
1562                         from += rem;
1563                         if (nr > rem)
1564                                 nr -= rem;
1565                         else
1566                                 return 0;
1567                 }
1568                 rem = nr % card->erase_size;
1569                 if (rem)
1570                         nr -= rem;
1571         }
1572
1573         if (nr == 0)
1574                 return 0;
1575
1576         to = from + nr;
1577
1578         if (to <= from)
1579                 return -EINVAL;
1580
1581         /* 'from' and 'to' are inclusive */
1582         to -= 1;
1583
1584         return mmc_do_erase(card, from, to, arg);
1585 }
1586 EXPORT_SYMBOL(mmc_erase);
1587
1588 int mmc_can_erase(struct mmc_card *card)
1589 {
1590         if ((card->host->caps & MMC_CAP_ERASE) &&
1591             (card->csd.cmdclass & CCC_ERASE) && card->erase_size)
1592                 return 1;
1593         return 0;
1594 }
1595 EXPORT_SYMBOL(mmc_can_erase);
1596
1597 int mmc_can_trim(struct mmc_card *card)
1598 {
1599         if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN)
1600                 return 1;
1601         return 0;
1602 }
1603 EXPORT_SYMBOL(mmc_can_trim);
1604
1605 int mmc_can_secure_erase_trim(struct mmc_card *card)
1606 {
1607         if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_ER_EN)
1608                 return 1;
1609         return 0;
1610 }
1611 EXPORT_SYMBOL(mmc_can_secure_erase_trim);
1612
1613 int mmc_erase_group_aligned(struct mmc_card *card, unsigned int from,
1614                             unsigned int nr)
1615 {
1616         if (!card->erase_size)
1617                 return 0;
1618         if (from % card->erase_size || nr % card->erase_size)
1619                 return 0;
1620         return 1;
1621 }
1622 EXPORT_SYMBOL(mmc_erase_group_aligned);
1623
1624 int mmc_set_blocklen(struct mmc_card *card, unsigned int blocklen)
1625 {
1626         struct mmc_command cmd = {0};
1627
1628         if (mmc_card_blockaddr(card) || mmc_card_ddr_mode(card))
1629                 return 0;
1630
1631         cmd.opcode = MMC_SET_BLOCKLEN;
1632         cmd.arg = blocklen;
1633         cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
1634         return mmc_wait_for_cmd(card->host, &cmd, 5);
1635 }
1636 EXPORT_SYMBOL(mmc_set_blocklen);
1637
1638 static int mmc_rescan_try_freq(struct mmc_host *host, unsigned freq)
1639 {
1640         host->f_init = freq;
1641
1642 #if defined(CONFIG_SDMMC_RK29) || !defined(CONFIG_SDMMC_RK29_OLD)   //Modifyed by xbw at 2011-11-17             
1643         int init_ret=0;
1644 #endif
1645
1646 #ifdef CONFIG_MMC_DEBUG
1647         pr_info("%s: %s: trying to init card at %u Hz\n",
1648                 mmc_hostname(host), __func__, host->f_init);
1649 #endif
1650         mmc_power_up(host);
1651
1652         /*
1653          * sdio_reset sends CMD52 to reset card.  Since we do not know
1654          * if the card is being re-initialized, just send it.  CMD52
1655          * should be ignored by SD/eMMC cards.
1656          */
1657 #if defined(CONFIG_SDMMC_RK29) || !defined(CONFIG_SDMMC_RK29_OLD)   //Modifyed by xbw at 2011-11-17     
1658 //the process is default for rockchip SDK. noted by xbw at 2011-11-17
1659
1660 /* Order's important: probe SDIO, then SD, then MMC */
1661
1662 #if !defined(CONFIG_USE_SDMMC0_FOR_WIFI_DEVELOP_BOARD)
1663     if( strncmp( mmc_hostname(host) ,"mmc0" , strlen("mmc0")) )
1664     {
1665             //sdio_reset(host);//make no sense; noteed by xbw at 2011-12-14
1666         mmc_go_idle(host);
1667
1668         if (!(init_ret=mmc_attach_sdio(host)))
1669         {
1670             printk(KERN_INFO "%s..%d..  ===== Initialize SDIO successfully. [%s]\n",\
1671                 __FUNCTION__,  __LINE__, mmc_hostname(host));
1672                 return 0;
1673         }
1674         else
1675         {
1676             if(0xFF!=init_ret)
1677             {
1678                  printk(KERN_WARNING "\n=====\n %s..%d..  ===== Initialize SDIO-card unsuccessfully!!! [%s]\n=====\n",\
1679                         __FUNCTION__,  __LINE__, mmc_hostname(host));
1680
1681                      goto freq_out;   
1682             }
1683         }
1684     }
1685     else
1686     {
1687         mmc_go_idle(host);
1688     }
1689 #else
1690     //sdio_reset(host); //make no sense; noteed by xbw at 2011-12-14
1691         mmc_go_idle(host);
1692
1693         if (!(init_ret=mmc_attach_sdio(host)))
1694         {
1695             printk(KERN_INFO "%s..%d..  ===== Initialize SDIO successfully. [%s]\n",\
1696                 __FUNCTION__,  __LINE__, mmc_hostname(host));
1697                 return 0;
1698         }
1699         else
1700         {
1701             if(0xFF!=init_ret)
1702             {
1703                  printk(KERN_WARNING "\n=====\n %s..%d..  ===== Initialize SDIO-card unsuccessfully!!! [%s]\n=====\n",\
1704                         __FUNCTION__,  __LINE__, mmc_hostname(host));
1705
1706                      goto freq_out;   
1707             }
1708         }
1709 #endif // #end--#if !defined(CONFIG_USE_SDMMC0_FOR_WIFI_DEVELOP_BOARD)
1710
1711     if (!(init_ret=mmc_attach_sd(host)))
1712     {
1713         printk(KERN_INFO "%s..%d..  ===== Initialize SD-card successfully. [%s]\n",\
1714             __FUNCTION__,  __LINE__, mmc_hostname(host));
1715             
1716             return 0;
1717         }
1718         else
1719         {
1720             if(0xFF!=init_ret)
1721             {
1722                   printk(KERN_WARNING "\n=====\n%s..%d..  ===== Initialize SD-card unsuccessfully! [%s]\n====\n",\
1723                         __FUNCTION__,  __LINE__, mmc_hostname(host));
1724
1725                      goto freq_out;   
1726             }
1727         }
1728
1729
1730         if (!(init_ret=mmc_attach_mmc(host)))
1731         {
1732             printk(KERN_INFO "%s...%d..  ===== Initialize MMC-card successfully. [%s]\n",\
1733                 __FUNCTION__,  __LINE__, mmc_hostname(host));
1734
1735             return 0;
1736         }
1737         else
1738         {
1739             if(0xFF!=init_ret)
1740             {
1741                  printk(KERN_WARNING "\n =====\n%s..%d..  ===== Initialize MMC-card unsuccessfully!!! [%s]\n======\n",\
1742                     __FUNCTION__,  __LINE__, mmc_hostname(host));
1743                     
1744                      goto freq_out;   
1745             }
1746         }               
1747         
1748 freq_out:
1749         mmc_power_off(host);
1750         return -EIO;
1751
1752 #else  // the default process in ICS.
1753
1754     sdio_reset(host);
1755         mmc_go_idle(host);
1756
1757         mmc_send_if_cond(host, host->ocr_avail);
1758
1759         /* Order's important: probe SDIO, then SD, then MMC */
1760         if (!mmc_attach_sdio(host))
1761                 return 0;
1762         if (!mmc_attach_sd(host))
1763                 return 0;
1764         if (!mmc_attach_mmc(host))
1765                 return 0;
1766
1767         mmc_power_off(host);
1768         return -EIO;
1769 #endif 
1770
1771 }
1772
1773 void mmc_rescan(struct work_struct *work)
1774 {
1775         static const unsigned freqs[] = { 400000, 300000, 200000, 100000 };
1776         struct mmc_host *host =
1777                 container_of(work, struct mmc_host, detect.work);
1778         int i;
1779         bool extend_wakelock = false;
1780
1781         if (host->rescan_disable)
1782                 return;
1783
1784         mmc_bus_get(host);
1785
1786         /*
1787          * if there is a _removable_ card registered, check whether it is
1788          * still present
1789          */
1790         if (host->bus_ops && host->bus_ops->detect && !host->bus_dead
1791             && !(host->caps & MMC_CAP_NONREMOVABLE))
1792                 host->bus_ops->detect(host);
1793
1794         /* If the card was removed the bus will be marked
1795          * as dead - extend the wakelock so userspace
1796          * can respond */
1797         if (host->bus_dead)
1798                 extend_wakelock = 1;
1799
1800         /*
1801          * Let mmc_bus_put() free the bus/bus_ops if we've found that
1802          * the card is no longer present.
1803          */
1804         mmc_bus_put(host);
1805         mmc_bus_get(host);
1806
1807         /* if there still is a card present, stop here */
1808         if (host->bus_ops != NULL) {
1809                 mmc_bus_put(host);
1810                 goto out;
1811         }
1812
1813         /*
1814          * Only we can add a new handler, so it's safe to
1815          * release the lock here.
1816          */
1817         mmc_bus_put(host);
1818
1819 #if defined(CONFIG_SDMMC_RK29) || !defined(CONFIG_SDMMC_RK29_OLD)   //Modifyed by xbw at 2011-11-17
1820     printk(KERN_INFO "\n%s...%d..  ===== mmc_rescan Begin....[%s]\n",__FILE__, __LINE__, mmc_hostname(host));
1821 #endif
1822
1823         if (host->ops->get_cd && host->ops->get_cd(host) == 0)
1824         {
1825 #if defined(CONFIG_SDMMC_RK29) || !defined(CONFIG_SDMMC_RK29_OLD)   //Modifyed by xbw at 2011-11-17
1826          printk(KERN_WARNING "\n=================\n%s..%d..  ====find no SDMMC host. [%s]\n", \
1827                 __FUNCTION__, __LINE__, mmc_hostname(host));
1828 #endif
1829
1830                 goto out;
1831         }
1832
1833         mmc_claim_host(host);
1834
1835 #if defined(CONFIG_SDMMC_RK29) || !defined(CONFIG_SDMMC_RK29_OLD)   //Modifyed by xbw at 2011-11-17
1836     if (!mmc_rescan_try_freq(host, host->f_min)) 
1837         extend_wakelock = true;
1838
1839 #else   
1840         for (i = 0; i < ARRAY_SIZE(freqs); i++) {
1841                 if (!mmc_rescan_try_freq(host, max(freqs[i], host->f_min))) {
1842                         extend_wakelock = true;
1843                         break;
1844                 }
1845                 if (freqs[i] <= host->f_min)
1846                         break;
1847         }
1848 #endif
1849
1850         mmc_release_host(host);
1851
1852  out:
1853         if (extend_wakelock)
1854                 wake_lock_timeout(&host->detect_wake_lock, HZ / 2);
1855         else
1856                 wake_unlock(&host->detect_wake_lock);
1857         if (host->caps & MMC_CAP_NEEDS_POLL) {
1858                 wake_lock(&host->detect_wake_lock);
1859                 mmc_schedule_delayed_work(&host->detect, HZ);
1860         }
1861 }
1862
1863 void mmc_start_host(struct mmc_host *host)
1864 {
1865         mmc_power_off(host);
1866         mmc_detect_change(host, 0);
1867 }
1868
1869 void mmc_stop_host(struct mmc_host *host)
1870 {
1871 #ifdef CONFIG_MMC_DEBUG
1872         unsigned long flags;
1873         spin_lock_irqsave(&host->lock, flags);
1874         host->removed = 1;
1875         spin_unlock_irqrestore(&host->lock, flags);
1876 #endif
1877
1878         if (host->caps & MMC_CAP_DISABLE)
1879                 cancel_delayed_work(&host->disable);
1880         if (cancel_delayed_work_sync(&host->detect))
1881                 wake_unlock(&host->detect_wake_lock);
1882         mmc_flush_scheduled_work();
1883
1884         /* clear pm flags now and let card drivers set them as needed */
1885         host->pm_flags = 0;
1886
1887         mmc_bus_get(host);
1888         if (host->bus_ops && !host->bus_dead) {
1889                 if (host->bus_ops->remove)
1890                         host->bus_ops->remove(host);
1891
1892                 mmc_claim_host(host);
1893                 mmc_detach_bus(host);
1894                 mmc_power_off(host);
1895                 mmc_release_host(host);
1896                 mmc_bus_put(host);
1897                 return;
1898         }
1899         mmc_bus_put(host);
1900
1901         BUG_ON(host->card);
1902
1903         mmc_power_off(host);
1904 }
1905
1906 int mmc_power_save_host(struct mmc_host *host)
1907 {
1908         int ret = 0;
1909
1910         mmc_bus_get(host);
1911
1912         if (!host->bus_ops || host->bus_dead || !host->bus_ops->power_restore) {
1913                 mmc_bus_put(host);
1914                 return -EINVAL;
1915         }
1916
1917         if (host->bus_ops->power_save)
1918                 ret = host->bus_ops->power_save(host);
1919
1920         mmc_bus_put(host);
1921
1922         mmc_power_off(host);
1923
1924         return ret;
1925 }
1926 EXPORT_SYMBOL(mmc_power_save_host);
1927
1928 int mmc_power_restore_host(struct mmc_host *host)
1929 {
1930         int ret;
1931
1932         mmc_bus_get(host);
1933
1934         if (!host->bus_ops || host->bus_dead || !host->bus_ops->power_restore) {
1935                 mmc_bus_put(host);
1936                 return -EINVAL;
1937         }
1938
1939         mmc_power_up(host);
1940         ret = host->bus_ops->power_restore(host);
1941
1942         mmc_bus_put(host);
1943
1944         return ret;
1945 }
1946 EXPORT_SYMBOL(mmc_power_restore_host);
1947
1948 int mmc_card_awake(struct mmc_host *host)
1949 {
1950         int err = -ENOSYS;
1951
1952         mmc_bus_get(host);
1953
1954         if (host->bus_ops && !host->bus_dead && host->bus_ops->awake)
1955                 err = host->bus_ops->awake(host);
1956
1957         mmc_bus_put(host);
1958
1959         return err;
1960 }
1961 EXPORT_SYMBOL(mmc_card_awake);
1962
1963 int mmc_card_sleep(struct mmc_host *host)
1964 {
1965         int err = -ENOSYS;
1966
1967         mmc_bus_get(host);
1968
1969         if (host->bus_ops && !host->bus_dead && host->bus_ops->awake)
1970                 err = host->bus_ops->sleep(host);
1971
1972         mmc_bus_put(host);
1973
1974         return err;
1975 }
1976 EXPORT_SYMBOL(mmc_card_sleep);
1977
1978 int mmc_card_can_sleep(struct mmc_host *host)
1979 {
1980         struct mmc_card *card = host->card;
1981
1982         if (card && mmc_card_mmc(card) && card->ext_csd.rev >= 3)
1983                 return 1;
1984         return 0;
1985 }
1986 EXPORT_SYMBOL(mmc_card_can_sleep);
1987
1988 #ifdef CONFIG_PM
1989
1990 /**
1991  *      mmc_suspend_host - suspend a host
1992  *      @host: mmc host
1993  */
1994 int mmc_suspend_host(struct mmc_host *host)
1995 {
1996         int err = 0;
1997
1998         if (mmc_bus_needs_resume(host))
1999                 return 0;
2000
2001         if (host->caps & MMC_CAP_DISABLE)
2002                 cancel_delayed_work(&host->disable);
2003         if (cancel_delayed_work(&host->detect))
2004                 wake_unlock(&host->detect_wake_lock);
2005         mmc_flush_scheduled_work();
2006
2007         mmc_bus_get(host);
2008         if (host->bus_ops && !host->bus_dead) {
2009                 if (host->bus_ops->suspend)
2010                         err = host->bus_ops->suspend(host);
2011                 if (err == -ENOSYS || !host->bus_ops->resume) {
2012                         /*
2013                          * We simply "remove" the card in this case.
2014                          * It will be redetected on resume.
2015                          */
2016                         if (host->bus_ops->remove)
2017                                 host->bus_ops->remove(host);
2018                         mmc_claim_host(host);
2019                         mmc_detach_bus(host);
2020                         mmc_power_off(host);
2021                         mmc_release_host(host);
2022                         host->pm_flags = 0;
2023                         err = 0;
2024                 }
2025         }
2026         mmc_bus_put(host);
2027
2028         if (!err && !mmc_card_keep_power(host))
2029                 mmc_power_off(host);
2030
2031         return err;
2032 }
2033
2034 EXPORT_SYMBOL(mmc_suspend_host);
2035
2036 /**
2037  *      mmc_resume_host - resume a previously suspended host
2038  *      @host: mmc host
2039  */
2040 int mmc_resume_host(struct mmc_host *host)
2041 {
2042         int err = 0;
2043
2044         mmc_bus_get(host);
2045         if (mmc_bus_manual_resume(host)) {
2046                 host->bus_resume_flags |= MMC_BUSRESUME_NEEDS_RESUME;
2047                 mmc_bus_put(host);
2048                 return 0;
2049         }
2050
2051         if (host->bus_ops && !host->bus_dead) {
2052                 if (!mmc_card_keep_power(host)) {
2053                         mmc_power_up(host);
2054                         mmc_select_voltage(host, host->ocr);
2055                         /*
2056                          * Tell runtime PM core we just powered up the card,
2057                          * since it still believes the card is powered off.
2058                          * Note that currently runtime PM is only enabled
2059                          * for SDIO cards that are MMC_CAP_POWER_OFF_CARD
2060                          */
2061                         if (mmc_card_sdio(host->card) &&
2062                             (host->caps & MMC_CAP_POWER_OFF_CARD)) {
2063                                 pm_runtime_disable(&host->card->dev);
2064                                 pm_runtime_set_active(&host->card->dev);
2065                                 pm_runtime_enable(&host->card->dev);
2066                         }
2067                 }
2068                 BUG_ON(!host->bus_ops->resume);
2069 #if defined(CONFIG_SDMMC_RK29) && !defined(CONFIG_SDMMC_RK29_OLD)
2070         //panic if the card is being removed during the resume, deleted by xbw at 2011-06-20
2071                 host->bus_ops->resume(host);
2072
2073 #else
2074                 err = host->bus_ops->resume(host);
2075                 if (err) {
2076                         printk(KERN_WARNING "%s: error %d during resume "
2077                                             "(card was removed?)\n",
2078                                             mmc_hostname(host), err);
2079                         err = 0;
2080                 }
2081 #endif
2082         }
2083         host->pm_flags &= ~MMC_PM_KEEP_POWER;
2084         mmc_bus_put(host);
2085
2086         return err;
2087 }
2088 EXPORT_SYMBOL(mmc_resume_host);
2089
2090 /* Do the card removal on suspend if card is assumed removeable
2091  * Do that in pm notifier while userspace isn't yet frozen, so we will be able
2092    to sync the card.
2093 */
2094 int mmc_pm_notify(struct notifier_block *notify_block,
2095                                         unsigned long mode, void *unused)
2096 {
2097         struct mmc_host *host = container_of(
2098                 notify_block, struct mmc_host, pm_notify);
2099         unsigned long flags;
2100
2101
2102         switch (mode) {
2103         case PM_HIBERNATION_PREPARE:
2104         case PM_SUSPEND_PREPARE:
2105
2106                 spin_lock_irqsave(&host->lock, flags);
2107                 if (mmc_bus_needs_resume(host)) {
2108                         spin_unlock_irqrestore(&host->lock, flags);
2109                         break;
2110                 }
2111                 host->rescan_disable = 1;
2112                 spin_unlock_irqrestore(&host->lock, flags);
2113                 if (cancel_delayed_work_sync(&host->detect))
2114                         wake_unlock(&host->detect_wake_lock);
2115
2116                 if (!host->bus_ops || host->bus_ops->suspend)
2117                         break;
2118
2119                 mmc_claim_host(host);
2120
2121                 if (host->bus_ops->remove)
2122                         host->bus_ops->remove(host);
2123
2124                 mmc_detach_bus(host);
2125                 mmc_power_off(host);
2126                 mmc_release_host(host);
2127                 host->pm_flags = 0;
2128                 break;
2129
2130         case PM_POST_SUSPEND:
2131         case PM_POST_HIBERNATION:
2132         case PM_POST_RESTORE:
2133
2134                 spin_lock_irqsave(&host->lock, flags);
2135                 if (mmc_bus_manual_resume(host)) {
2136                         spin_unlock_irqrestore(&host->lock, flags);
2137                         break;
2138                 }
2139                 host->rescan_disable = 0;
2140                 spin_unlock_irqrestore(&host->lock, flags);
2141                 mmc_detect_change(host, 0);
2142
2143         }
2144
2145         return 0;
2146 }
2147 #endif
2148
2149 #ifdef CONFIG_MMC_EMBEDDED_SDIO
2150 void mmc_set_embedded_sdio_data(struct mmc_host *host,
2151                                 struct sdio_cis *cis,
2152                                 struct sdio_cccr *cccr,
2153                                 struct sdio_embedded_func *funcs,
2154                                 int num_funcs)
2155 {
2156         host->embedded_sdio_data.cis = cis;
2157         host->embedded_sdio_data.cccr = cccr;
2158         host->embedded_sdio_data.funcs = funcs;
2159         host->embedded_sdio_data.num_funcs = num_funcs;
2160 }
2161
2162 EXPORT_SYMBOL(mmc_set_embedded_sdio_data);
2163 #endif
2164
2165 static int __init mmc_init(void)
2166 {
2167         int ret;
2168
2169         workqueue = alloc_ordered_workqueue("kmmcd", 0);
2170         if (!workqueue)
2171                 return -ENOMEM;
2172
2173         ret = mmc_register_bus();
2174         if (ret)
2175                 goto destroy_workqueue;
2176
2177         ret = mmc_register_host_class();
2178         if (ret)
2179                 goto unregister_bus;
2180
2181         ret = sdio_register_bus();
2182         if (ret)
2183                 goto unregister_host_class;
2184
2185         return 0;
2186
2187 unregister_host_class:
2188         mmc_unregister_host_class();
2189 unregister_bus:
2190         mmc_unregister_bus();
2191 destroy_workqueue:
2192         destroy_workqueue(workqueue);
2193
2194         return ret;
2195 }
2196
2197 static void __exit mmc_exit(void)
2198 {
2199         sdio_unregister_bus();
2200         mmc_unregister_host_class();
2201         mmc_unregister_bus();
2202         destroy_workqueue(workqueue);
2203 }
2204
2205 subsys_initcall(mmc_init);
2206 module_exit(mmc_exit);
2207
2208 MODULE_LICENSE("GPL");