modify the failure of SD-sdio in some case,due to print too much to invalid
[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("%s..%d.. !!!!! wait for CMD%d timeout ===xbw[%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 static 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. Assumes that there are
1231  * no interesting cards left, so the bus is powered down.
1232  */
1233 void mmc_detach_bus(struct mmc_host *host)
1234 {
1235         unsigned long flags;
1236
1237         BUG_ON(!host);
1238
1239         WARN_ON(!host->claimed);
1240         WARN_ON(!host->bus_ops);
1241
1242         spin_lock_irqsave(&host->lock, flags);
1243
1244         host->bus_dead = 1;
1245
1246         spin_unlock_irqrestore(&host->lock, flags);
1247
1248         mmc_power_off(host);
1249
1250         mmc_bus_put(host);
1251 }
1252
1253 /**
1254  *      mmc_detect_change - process change of state on a MMC socket
1255  *      @host: host which changed state.
1256  *      @delay: optional delay to wait before detection (jiffies)
1257  *
1258  *      MMC drivers should call this when they detect a card has been
1259  *      inserted or removed. The MMC layer will confirm that any
1260  *      present card is still functional, and initialize any newly
1261  *      inserted.
1262  */
1263 void mmc_detect_change(struct mmc_host *host, unsigned long delay)
1264 {
1265 #ifdef CONFIG_MMC_DEBUG
1266         unsigned long flags;
1267         spin_lock_irqsave(&host->lock, flags);
1268         WARN_ON(host->removed);
1269         spin_unlock_irqrestore(&host->lock, flags);
1270 #endif
1271
1272         wake_lock(&host->detect_wake_lock);
1273         mmc_schedule_delayed_work(&host->detect, delay);
1274 }
1275
1276 EXPORT_SYMBOL(mmc_detect_change);
1277
1278 void mmc_init_erase(struct mmc_card *card)
1279 {
1280         unsigned int sz;
1281
1282         if (is_power_of_2(card->erase_size))
1283                 card->erase_shift = ffs(card->erase_size) - 1;
1284         else
1285                 card->erase_shift = 0;
1286
1287         /*
1288          * It is possible to erase an arbitrarily large area of an SD or MMC
1289          * card.  That is not desirable because it can take a long time
1290          * (minutes) potentially delaying more important I/O, and also the
1291          * timeout calculations become increasingly hugely over-estimated.
1292          * Consequently, 'pref_erase' is defined as a guide to limit erases
1293          * to that size and alignment.
1294          *
1295          * For SD cards that define Allocation Unit size, limit erases to one
1296          * Allocation Unit at a time.  For MMC cards that define High Capacity
1297          * Erase Size, whether it is switched on or not, limit to that size.
1298          * Otherwise just have a stab at a good value.  For modern cards it
1299          * will end up being 4MiB.  Note that if the value is too small, it
1300          * can end up taking longer to erase.
1301          */
1302         if (mmc_card_sd(card) && card->ssr.au) {
1303                 card->pref_erase = card->ssr.au;
1304                 card->erase_shift = ffs(card->ssr.au) - 1;
1305         } else if (card->ext_csd.hc_erase_size) {
1306                 card->pref_erase = card->ext_csd.hc_erase_size;
1307         } else {
1308                 sz = (card->csd.capacity << (card->csd.read_blkbits - 9)) >> 11;
1309                 if (sz < 128)
1310                         card->pref_erase = 512 * 1024 / 512;
1311                 else if (sz < 512)
1312                         card->pref_erase = 1024 * 1024 / 512;
1313                 else if (sz < 1024)
1314                         card->pref_erase = 2 * 1024 * 1024 / 512;
1315                 else
1316                         card->pref_erase = 4 * 1024 * 1024 / 512;
1317                 if (card->pref_erase < card->erase_size)
1318                         card->pref_erase = card->erase_size;
1319                 else {
1320                         sz = card->pref_erase % card->erase_size;
1321                         if (sz)
1322                                 card->pref_erase += card->erase_size - sz;
1323                 }
1324         }
1325 }
1326
1327 static unsigned int mmc_mmc_erase_timeout(struct mmc_card *card,
1328                                           unsigned int arg, unsigned int qty)
1329 {
1330         unsigned int erase_timeout;
1331
1332         if (card->ext_csd.erase_group_def & 1) {
1333                 /* High Capacity Erase Group Size uses HC timeouts */
1334                 if (arg == MMC_TRIM_ARG)
1335                         erase_timeout = card->ext_csd.trim_timeout;
1336                 else
1337                         erase_timeout = card->ext_csd.hc_erase_timeout;
1338         } else {
1339                 /* CSD Erase Group Size uses write timeout */
1340                 unsigned int mult = (10 << card->csd.r2w_factor);
1341                 unsigned int timeout_clks = card->csd.tacc_clks * mult;
1342                 unsigned int timeout_us;
1343
1344                 /* Avoid overflow: e.g. tacc_ns=80000000 mult=1280 */
1345                 if (card->csd.tacc_ns < 1000000)
1346                         timeout_us = (card->csd.tacc_ns * mult) / 1000;
1347                 else
1348                         timeout_us = (card->csd.tacc_ns / 1000) * mult;
1349
1350                 /*
1351                  * ios.clock is only a target.  The real clock rate might be
1352                  * less but not that much less, so fudge it by multiplying by 2.
1353                  */
1354                 timeout_clks <<= 1;
1355                 timeout_us += (timeout_clks * 1000) /
1356                               (mmc_host_clk_rate(card->host) / 1000);
1357
1358                 erase_timeout = timeout_us / 1000;
1359
1360                 /*
1361                  * Theoretically, the calculation could underflow so round up
1362                  * to 1ms in that case.
1363                  */
1364                 if (!erase_timeout)
1365                         erase_timeout = 1;
1366         }
1367
1368         /* Multiplier for secure operations */
1369         if (arg & MMC_SECURE_ARGS) {
1370                 if (arg == MMC_SECURE_ERASE_ARG)
1371                         erase_timeout *= card->ext_csd.sec_erase_mult;
1372                 else
1373                         erase_timeout *= card->ext_csd.sec_trim_mult;
1374         }
1375
1376         erase_timeout *= qty;
1377
1378         /*
1379          * Ensure at least a 1 second timeout for SPI as per
1380          * 'mmc_set_data_timeout()'
1381          */
1382         if (mmc_host_is_spi(card->host) && erase_timeout < 1000)
1383                 erase_timeout = 1000;
1384
1385         return erase_timeout;
1386 }
1387
1388 static unsigned int mmc_sd_erase_timeout(struct mmc_card *card,
1389                                          unsigned int arg,
1390                                          unsigned int qty)
1391 {
1392         unsigned int erase_timeout;
1393
1394         if (card->ssr.erase_timeout) {
1395                 /* Erase timeout specified in SD Status Register (SSR) */
1396                 erase_timeout = card->ssr.erase_timeout * qty +
1397                                 card->ssr.erase_offset;
1398         } else {
1399                 /*
1400                  * Erase timeout not specified in SD Status Register (SSR) so
1401                  * use 250ms per write block.
1402                  */
1403                 erase_timeout = 250 * qty;
1404         }
1405
1406         /* Must not be less than 1 second */
1407         if (erase_timeout < 1000)
1408                 erase_timeout = 1000;
1409
1410         return erase_timeout;
1411 }
1412
1413 static unsigned int mmc_erase_timeout(struct mmc_card *card,
1414                                       unsigned int arg,
1415                                       unsigned int qty)
1416 {
1417         if (mmc_card_sd(card))
1418                 return mmc_sd_erase_timeout(card, arg, qty);
1419         else
1420                 return mmc_mmc_erase_timeout(card, arg, qty);
1421 }
1422
1423 static int mmc_do_erase(struct mmc_card *card, unsigned int from,
1424                         unsigned int to, unsigned int arg)
1425 {
1426         struct mmc_command cmd = {0};
1427         unsigned int qty = 0;
1428         int err;
1429
1430         /*
1431          * qty is used to calculate the erase timeout which depends on how many
1432          * erase groups (or allocation units in SD terminology) are affected.
1433          * We count erasing part of an erase group as one erase group.
1434          * For SD, the allocation units are always a power of 2.  For MMC, the
1435          * erase group size is almost certainly also power of 2, but it does not
1436          * seem to insist on that in the JEDEC standard, so we fall back to
1437          * division in that case.  SD may not specify an allocation unit size,
1438          * in which case the timeout is based on the number of write blocks.
1439          *
1440          * Note that the timeout for secure trim 2 will only be correct if the
1441          * number of erase groups specified is the same as the total of all
1442          * preceding secure trim 1 commands.  Since the power may have been
1443          * lost since the secure trim 1 commands occurred, it is generally
1444          * impossible to calculate the secure trim 2 timeout correctly.
1445          */
1446         if (card->erase_shift)
1447                 qty += ((to >> card->erase_shift) -
1448                         (from >> card->erase_shift)) + 1;
1449         else if (mmc_card_sd(card))
1450                 qty += to - from + 1;
1451         else
1452                 qty += ((to / card->erase_size) -
1453                         (from / card->erase_size)) + 1;
1454
1455         if (!mmc_card_blockaddr(card)) {
1456                 from <<= 9;
1457                 to <<= 9;
1458         }
1459
1460         if (mmc_card_sd(card))
1461                 cmd.opcode = SD_ERASE_WR_BLK_START;
1462         else
1463                 cmd.opcode = MMC_ERASE_GROUP_START;
1464         cmd.arg = from;
1465         cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
1466         err = mmc_wait_for_cmd(card->host, &cmd, 0);
1467         if (err) {
1468                 printk(KERN_ERR "mmc_erase: group start error %d, "
1469                        "status %#x\n", err, cmd.resp[0]);
1470                 err = -EINVAL;
1471                 goto out;
1472         }
1473
1474         memset(&cmd, 0, sizeof(struct mmc_command));
1475         if (mmc_card_sd(card))
1476                 cmd.opcode = SD_ERASE_WR_BLK_END;
1477         else
1478                 cmd.opcode = MMC_ERASE_GROUP_END;
1479         cmd.arg = to;
1480         cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
1481         err = mmc_wait_for_cmd(card->host, &cmd, 0);
1482         if (err) {
1483                 printk(KERN_ERR "mmc_erase: group end error %d, status %#x\n",
1484                        err, cmd.resp[0]);
1485                 err = -EINVAL;
1486                 goto out;
1487         }
1488
1489         memset(&cmd, 0, sizeof(struct mmc_command));
1490         cmd.opcode = MMC_ERASE;
1491         cmd.arg = arg;
1492         cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1493         cmd.cmd_timeout_ms = mmc_erase_timeout(card, arg, qty);
1494         err = mmc_wait_for_cmd(card->host, &cmd, 0);
1495         if (err) {
1496                 printk(KERN_ERR "mmc_erase: erase error %d, status %#x\n",
1497                        err, cmd.resp[0]);
1498                 err = -EIO;
1499                 goto out;
1500         }
1501
1502         if (mmc_host_is_spi(card->host))
1503                 goto out;
1504
1505         do {
1506                 memset(&cmd, 0, sizeof(struct mmc_command));
1507                 cmd.opcode = MMC_SEND_STATUS;
1508                 cmd.arg = card->rca << 16;
1509                 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
1510                 /* Do not retry else we can't see errors */
1511                 err = mmc_wait_for_cmd(card->host, &cmd, 0);
1512                 if (err || (cmd.resp[0] & 0xFDF92000)) {
1513                         printk(KERN_ERR "error %d requesting status %#x\n",
1514                                 err, cmd.resp[0]);
1515                         err = -EIO;
1516                         goto out;
1517                 }
1518         } while (!(cmd.resp[0] & R1_READY_FOR_DATA) ||
1519                  R1_CURRENT_STATE(cmd.resp[0]) == 7);
1520 out:
1521         return err;
1522 }
1523
1524 /**
1525  * mmc_erase - erase sectors.
1526  * @card: card to erase
1527  * @from: first sector to erase
1528  * @nr: number of sectors to erase
1529  * @arg: erase command argument (SD supports only %MMC_ERASE_ARG)
1530  *
1531  * Caller must claim host before calling this function.
1532  */
1533 int mmc_erase(struct mmc_card *card, unsigned int from, unsigned int nr,
1534               unsigned int arg)
1535 {
1536         unsigned int rem, to = from + nr;
1537
1538         if (!(card->host->caps & MMC_CAP_ERASE) ||
1539             !(card->csd.cmdclass & CCC_ERASE))
1540                 return -EOPNOTSUPP;
1541
1542         if (!card->erase_size)
1543                 return -EOPNOTSUPP;
1544
1545         if (mmc_card_sd(card) && arg != MMC_ERASE_ARG)
1546                 return -EOPNOTSUPP;
1547
1548         if ((arg & MMC_SECURE_ARGS) &&
1549             !(card->ext_csd.sec_feature_support & EXT_CSD_SEC_ER_EN))
1550                 return -EOPNOTSUPP;
1551
1552         if ((arg & MMC_TRIM_ARGS) &&
1553             !(card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN))
1554                 return -EOPNOTSUPP;
1555
1556         if (arg == MMC_SECURE_ERASE_ARG) {
1557                 if (from % card->erase_size || nr % card->erase_size)
1558                         return -EINVAL;
1559         }
1560
1561         if (arg == MMC_ERASE_ARG) {
1562                 rem = from % card->erase_size;
1563                 if (rem) {
1564                         rem = card->erase_size - rem;
1565                         from += rem;
1566                         if (nr > rem)
1567                                 nr -= rem;
1568                         else
1569                                 return 0;
1570                 }
1571                 rem = nr % card->erase_size;
1572                 if (rem)
1573                         nr -= rem;
1574         }
1575
1576         if (nr == 0)
1577                 return 0;
1578
1579         to = from + nr;
1580
1581         if (to <= from)
1582                 return -EINVAL;
1583
1584         /* 'from' and 'to' are inclusive */
1585         to -= 1;
1586
1587         return mmc_do_erase(card, from, to, arg);
1588 }
1589 EXPORT_SYMBOL(mmc_erase);
1590
1591 int mmc_can_erase(struct mmc_card *card)
1592 {
1593         if ((card->host->caps & MMC_CAP_ERASE) &&
1594             (card->csd.cmdclass & CCC_ERASE) && card->erase_size)
1595                 return 1;
1596         return 0;
1597 }
1598 EXPORT_SYMBOL(mmc_can_erase);
1599
1600 int mmc_can_trim(struct mmc_card *card)
1601 {
1602         if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN)
1603                 return 1;
1604         return 0;
1605 }
1606 EXPORT_SYMBOL(mmc_can_trim);
1607
1608 int mmc_can_secure_erase_trim(struct mmc_card *card)
1609 {
1610         if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_ER_EN)
1611                 return 1;
1612         return 0;
1613 }
1614 EXPORT_SYMBOL(mmc_can_secure_erase_trim);
1615
1616 int mmc_erase_group_aligned(struct mmc_card *card, unsigned int from,
1617                             unsigned int nr)
1618 {
1619         if (!card->erase_size)
1620                 return 0;
1621         if (from % card->erase_size || nr % card->erase_size)
1622                 return 0;
1623         return 1;
1624 }
1625 EXPORT_SYMBOL(mmc_erase_group_aligned);
1626
1627 int mmc_set_blocklen(struct mmc_card *card, unsigned int blocklen)
1628 {
1629         struct mmc_command cmd = {0};
1630
1631         if (mmc_card_blockaddr(card) || mmc_card_ddr_mode(card))
1632                 return 0;
1633
1634         cmd.opcode = MMC_SET_BLOCKLEN;
1635         cmd.arg = blocklen;
1636         cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
1637         return mmc_wait_for_cmd(card->host, &cmd, 5);
1638 }
1639 EXPORT_SYMBOL(mmc_set_blocklen);
1640
1641 static int mmc_rescan_try_freq(struct mmc_host *host, unsigned freq)
1642 {
1643         host->f_init = freq;
1644
1645 #if defined(CONFIG_SDMMC_RK29) || !defined(CONFIG_SDMMC_RK29_OLD)   //Modifyed by xbw at 2011-11-17             
1646         int init_ret=0;
1647 #endif
1648
1649 #ifdef CONFIG_MMC_DEBUG
1650         pr_info("%s: %s: trying to init card at %u Hz\n",
1651                 mmc_hostname(host), __func__, host->f_init);
1652 #endif
1653         mmc_power_up(host);
1654
1655         /*
1656          * sdio_reset sends CMD52 to reset card.  Since we do not know
1657          * if the card is being re-initialized, just send it.  CMD52
1658          * should be ignored by SD/eMMC cards.
1659          */
1660 #if defined(CONFIG_SDMMC_RK29) || !defined(CONFIG_SDMMC_RK29_OLD)   //Modifyed by xbw at 2011-11-17     
1661 //the process is default for rockchip SDK. noted by xbw at 2011-11-17
1662
1663 /* Order's important: probe SDIO, then SD, then MMC */
1664
1665 #if !defined(CONFIG_USE_SDMMC0_FOR_WIFI_DEVELOP_BOARD)
1666     if( strncmp( mmc_hostname(host) ,"mmc0" , strlen("mmc0")) )
1667     {
1668             //sdio_reset(host);//make no sense; noteed by xbw at 2011-12-14
1669         mmc_go_idle(host);
1670
1671         if (!(init_ret=mmc_attach_sdio(host)))
1672         {
1673             printk("%s..%d..  ===== Initialize SDIO successfully. ===xbw[%s]===\n",\
1674                 __FUNCTION__,  __LINE__, mmc_hostname(host));
1675                 return 0;
1676         }
1677         else
1678         {
1679             if(0xFF!=init_ret)
1680             {
1681                  printk("\n=====\n %s..%d..  ===== Initialize SDIO-card unsuccessfully!!! ===xbw[%s]===\n=====\n",\
1682                         __FUNCTION__,  __LINE__, mmc_hostname(host));
1683
1684                      goto freq_out;   
1685             }
1686         }
1687     }
1688     else
1689     {
1690         mmc_go_idle(host);
1691     }
1692 #else
1693     //sdio_reset(host); //make no sense; noteed by xbw at 2011-12-14
1694         mmc_go_idle(host);
1695
1696         if (!(init_ret=mmc_attach_sdio(host)))
1697         {
1698             printk("%s..%d..  ===== Initialize SDIO successfully. ===xbw[%s]===\n",\
1699                 __FUNCTION__,  __LINE__, mmc_hostname(host));
1700                 return 0;
1701         }
1702         else
1703         {
1704             if(0xFF!=init_ret)
1705             {
1706                  printk("\n=====\n %s..%d..  ===== Initialize SDIO-card unsuccessfully!!! ===xbw[%s]===\n=====\n",\
1707                         __FUNCTION__,  __LINE__, mmc_hostname(host));
1708
1709                      goto freq_out;   
1710             }
1711         }
1712 #endif // #end--#if !defined(CONFIG_USE_SDMMC0_FOR_WIFI_DEVELOP_BOARD)
1713
1714     if (!(init_ret=mmc_attach_sd(host)))
1715     {
1716         printk("%s..%d..  ===== Initialize SD-card successfully. ===xbw[%s]===\n",\
1717             __FUNCTION__,  __LINE__, mmc_hostname(host));
1718             
1719             return 0;
1720         }
1721         else
1722         {
1723             if(0xFF!=init_ret)
1724             {
1725                   printk("\n=====\n%s..%d..  ===== Initialize SD-card unsuccessfully!!! ===xbw[%s]===\n====\n",\
1726                         __FUNCTION__,  __LINE__, mmc_hostname(host));
1727
1728                      goto freq_out;   
1729             }
1730         }
1731
1732
1733         if (!(init_ret=mmc_attach_mmc(host)))
1734         {
1735             printk("%s...%d..  ===== Initialize MMC-card successfully. ===xbw[%s]===\n",\
1736                 __FUNCTION__,  __LINE__, mmc_hostname(host));
1737
1738             return 0;
1739         }
1740         else
1741         {
1742             if(0xFF!=init_ret)
1743             {
1744                  printk("\n =====\n%s..%d..  ===== Initialize MMC-card unsuccessfully!!! ===xbw[%s]===\n======\n",\
1745                     __FUNCTION__,  __LINE__, mmc_hostname(host));
1746                     
1747                      goto freq_out;   
1748             }
1749         }               
1750         
1751 freq_out:
1752         mmc_power_off(host);
1753         return -EIO;
1754
1755 #else  // the default process in ICS.
1756
1757     sdio_reset(host);
1758         mmc_go_idle(host);
1759
1760         mmc_send_if_cond(host, host->ocr_avail);
1761
1762         /* Order's important: probe SDIO, then SD, then MMC */
1763         if (!mmc_attach_sdio(host))
1764                 return 0;
1765         if (!mmc_attach_sd(host))
1766                 return 0;
1767         if (!mmc_attach_mmc(host))
1768                 return 0;
1769
1770         mmc_power_off(host);
1771         return -EIO;
1772 #endif 
1773
1774 }
1775
1776 void mmc_rescan(struct work_struct *work)
1777 {
1778         static const unsigned freqs[] = { 400000, 300000, 200000, 100000 };
1779         struct mmc_host *host =
1780                 container_of(work, struct mmc_host, detect.work);
1781         int i;
1782         bool extend_wakelock = false;
1783
1784         if (host->rescan_disable)
1785                 return;
1786
1787         mmc_bus_get(host);
1788
1789         /*
1790          * if there is a _removable_ card registered, check whether it is
1791          * still present
1792          */
1793         if (host->bus_ops && host->bus_ops->detect && !host->bus_dead
1794             && !(host->caps & MMC_CAP_NONREMOVABLE))
1795                 host->bus_ops->detect(host);
1796
1797         /* If the card was removed the bus will be marked
1798          * as dead - extend the wakelock so userspace
1799          * can respond */
1800         if (host->bus_dead)
1801                 extend_wakelock = 1;
1802
1803         /*
1804          * Let mmc_bus_put() free the bus/bus_ops if we've found that
1805          * the card is no longer present.
1806          */
1807         mmc_bus_put(host);
1808         mmc_bus_get(host);
1809
1810         /* if there still is a card present, stop here */
1811         if (host->bus_ops != NULL) {
1812                 mmc_bus_put(host);
1813                 goto out;
1814         }
1815
1816         /*
1817          * Only we can add a new handler, so it's safe to
1818          * release the lock here.
1819          */
1820         mmc_bus_put(host);
1821
1822 #if defined(CONFIG_SDMMC_RK29) || !defined(CONFIG_SDMMC_RK29_OLD)   //Modifyed by xbw at 2011-11-17
1823     printk("\n%s...%d..  ===== mmc_rescan Begin....======xbw[%s]=====\n",__FILE__, __LINE__, mmc_hostname(host));
1824 #endif
1825
1826         if (host->ops->get_cd && host->ops->get_cd(host) == 0)
1827         {
1828 #if defined(CONFIG_SDMMC_RK29) || !defined(CONFIG_SDMMC_RK29_OLD)   //Modifyed by xbw at 2011-11-17
1829          printk("\n=================\n%s..%d..  ====find no SDMMC host.====xbw[%s]=====\n", \
1830                 __FUNCTION__, __LINE__, mmc_hostname(host));
1831 #endif
1832
1833                 goto out;
1834         }
1835
1836         mmc_claim_host(host);
1837
1838 #if defined(CONFIG_SDMMC_RK29) || !defined(CONFIG_SDMMC_RK29_OLD)   //Modifyed by xbw at 2011-11-17
1839     if (!mmc_rescan_try_freq(host, host->f_min)) 
1840         extend_wakelock = true;
1841
1842 #else   
1843         for (i = 0; i < ARRAY_SIZE(freqs); i++) {
1844                 if (!mmc_rescan_try_freq(host, max(freqs[i], host->f_min))) {
1845                         extend_wakelock = true;
1846                         break;
1847                 }
1848                 if (freqs[i] <= host->f_min)
1849                         break;
1850         }
1851 #endif
1852
1853         mmc_release_host(host);
1854
1855  out:
1856         if (extend_wakelock)
1857                 wake_lock_timeout(&host->detect_wake_lock, HZ / 2);
1858         else
1859                 wake_unlock(&host->detect_wake_lock);
1860         if (host->caps & MMC_CAP_NEEDS_POLL) {
1861                 wake_lock(&host->detect_wake_lock);
1862                 mmc_schedule_delayed_work(&host->detect, HZ);
1863         }
1864 }
1865
1866 void mmc_start_host(struct mmc_host *host)
1867 {
1868         mmc_power_off(host);
1869         mmc_detect_change(host, 0);
1870 }
1871
1872 void mmc_stop_host(struct mmc_host *host)
1873 {
1874 #ifdef CONFIG_MMC_DEBUG
1875         unsigned long flags;
1876         spin_lock_irqsave(&host->lock, flags);
1877         host->removed = 1;
1878         spin_unlock_irqrestore(&host->lock, flags);
1879 #endif
1880
1881         if (host->caps & MMC_CAP_DISABLE)
1882                 cancel_delayed_work(&host->disable);
1883         if (cancel_delayed_work_sync(&host->detect))
1884                 wake_unlock(&host->detect_wake_lock);
1885         mmc_flush_scheduled_work();
1886
1887         /* clear pm flags now and let card drivers set them as needed */
1888         host->pm_flags = 0;
1889
1890         mmc_bus_get(host);
1891         if (host->bus_ops && !host->bus_dead) {
1892                 if (host->bus_ops->remove)
1893                         host->bus_ops->remove(host);
1894
1895                 mmc_claim_host(host);
1896                 mmc_detach_bus(host);
1897                 mmc_release_host(host);
1898                 mmc_bus_put(host);
1899                 return;
1900         }
1901         mmc_bus_put(host);
1902
1903         BUG_ON(host->card);
1904
1905         mmc_power_off(host);
1906 }
1907
1908 int mmc_power_save_host(struct mmc_host *host)
1909 {
1910         int ret = 0;
1911
1912         mmc_bus_get(host);
1913
1914         if (!host->bus_ops || host->bus_dead || !host->bus_ops->power_restore) {
1915                 mmc_bus_put(host);
1916                 return -EINVAL;
1917         }
1918
1919         if (host->bus_ops->power_save)
1920                 ret = host->bus_ops->power_save(host);
1921
1922         mmc_bus_put(host);
1923
1924         mmc_power_off(host);
1925
1926         return ret;
1927 }
1928 EXPORT_SYMBOL(mmc_power_save_host);
1929
1930 int mmc_power_restore_host(struct mmc_host *host)
1931 {
1932         int ret;
1933
1934         mmc_bus_get(host);
1935
1936         if (!host->bus_ops || host->bus_dead || !host->bus_ops->power_restore) {
1937                 mmc_bus_put(host);
1938                 return -EINVAL;
1939         }
1940
1941         mmc_power_up(host);
1942         ret = host->bus_ops->power_restore(host);
1943
1944         mmc_bus_put(host);
1945
1946         return ret;
1947 }
1948 EXPORT_SYMBOL(mmc_power_restore_host);
1949
1950 int mmc_card_awake(struct mmc_host *host)
1951 {
1952         int err = -ENOSYS;
1953
1954         mmc_bus_get(host);
1955
1956         if (host->bus_ops && !host->bus_dead && host->bus_ops->awake)
1957                 err = host->bus_ops->awake(host);
1958
1959         mmc_bus_put(host);
1960
1961         return err;
1962 }
1963 EXPORT_SYMBOL(mmc_card_awake);
1964
1965 int mmc_card_sleep(struct mmc_host *host)
1966 {
1967         int err = -ENOSYS;
1968
1969         mmc_bus_get(host);
1970
1971         if (host->bus_ops && !host->bus_dead && host->bus_ops->awake)
1972                 err = host->bus_ops->sleep(host);
1973
1974         mmc_bus_put(host);
1975
1976         return err;
1977 }
1978 EXPORT_SYMBOL(mmc_card_sleep);
1979
1980 int mmc_card_can_sleep(struct mmc_host *host)
1981 {
1982         struct mmc_card *card = host->card;
1983
1984         if (card && mmc_card_mmc(card) && card->ext_csd.rev >= 3)
1985                 return 1;
1986         return 0;
1987 }
1988 EXPORT_SYMBOL(mmc_card_can_sleep);
1989
1990 #ifdef CONFIG_PM
1991
1992 /**
1993  *      mmc_suspend_host - suspend a host
1994  *      @host: mmc host
1995  */
1996 int mmc_suspend_host(struct mmc_host *host)
1997 {
1998         int err = 0;
1999
2000         if (mmc_bus_needs_resume(host))
2001                 return 0;
2002
2003         if (host->caps & MMC_CAP_DISABLE)
2004                 cancel_delayed_work(&host->disable);
2005         if (cancel_delayed_work(&host->detect))
2006                 wake_unlock(&host->detect_wake_lock);
2007         mmc_flush_scheduled_work();
2008
2009         mmc_bus_get(host);
2010         if (host->bus_ops && !host->bus_dead) {
2011                 if (host->bus_ops->suspend)
2012                         err = host->bus_ops->suspend(host);
2013                 if (err == -ENOSYS || !host->bus_ops->resume) {
2014                         /*
2015                          * We simply "remove" the card in this case.
2016                          * It will be redetected on resume.
2017                          */
2018                         if (host->bus_ops->remove)
2019                                 host->bus_ops->remove(host);
2020                         mmc_claim_host(host);
2021                         mmc_detach_bus(host);
2022                         mmc_release_host(host);
2023                         host->pm_flags = 0;
2024                         err = 0;
2025                 }
2026         }
2027         mmc_bus_put(host);
2028
2029         if (!err && !mmc_card_keep_power(host))
2030                 mmc_power_off(host);
2031
2032         return err;
2033 }
2034
2035 EXPORT_SYMBOL(mmc_suspend_host);
2036
2037 /**
2038  *      mmc_resume_host - resume a previously suspended host
2039  *      @host: mmc host
2040  */
2041 int mmc_resume_host(struct mmc_host *host)
2042 {
2043         int err = 0;
2044
2045         mmc_bus_get(host);
2046         if (mmc_bus_manual_resume(host)) {
2047                 host->bus_resume_flags |= MMC_BUSRESUME_NEEDS_RESUME;
2048                 mmc_bus_put(host);
2049                 return 0;
2050         }
2051
2052         if (host->bus_ops && !host->bus_dead) {
2053                 if (!mmc_card_keep_power(host)) {
2054                         mmc_power_up(host);
2055                         mmc_select_voltage(host, host->ocr);
2056                         /*
2057                          * Tell runtime PM core we just powered up the card,
2058                          * since it still believes the card is powered off.
2059                          * Note that currently runtime PM is only enabled
2060                          * for SDIO cards that are MMC_CAP_POWER_OFF_CARD
2061                          */
2062                         if (mmc_card_sdio(host->card) &&
2063                             (host->caps & MMC_CAP_POWER_OFF_CARD)) {
2064                                 pm_runtime_disable(&host->card->dev);
2065                                 pm_runtime_set_active(&host->card->dev);
2066                                 pm_runtime_enable(&host->card->dev);
2067                         }
2068                 }
2069                 BUG_ON(!host->bus_ops->resume);
2070 #if defined(CONFIG_SDMMC_RK29) && !defined(CONFIG_SDMMC_RK29_OLD)
2071         //panic if the card is being removed during the resume, deleted by xbw at 2011-06-20
2072                 host->bus_ops->resume(host);
2073
2074 #else
2075                 err = host->bus_ops->resume(host);
2076                 if (err) {
2077                         printk(KERN_WARNING "%s: error %d during resume "
2078                                             "(card was removed?)\n",
2079                                             mmc_hostname(host), err);
2080                         err = 0;
2081                 }
2082 #endif
2083         }
2084         host->pm_flags &= ~MMC_PM_KEEP_POWER;
2085         mmc_bus_put(host);
2086
2087         return err;
2088 }
2089 EXPORT_SYMBOL(mmc_resume_host);
2090
2091 /* Do the card removal on suspend if card is assumed removeable
2092  * Do that in pm notifier while userspace isn't yet frozen, so we will be able
2093    to sync the card.
2094 */
2095 int mmc_pm_notify(struct notifier_block *notify_block,
2096                                         unsigned long mode, void *unused)
2097 {
2098         struct mmc_host *host = container_of(
2099                 notify_block, struct mmc_host, pm_notify);
2100         unsigned long flags;
2101
2102
2103         switch (mode) {
2104         case PM_HIBERNATION_PREPARE:
2105         case PM_SUSPEND_PREPARE:
2106
2107                 spin_lock_irqsave(&host->lock, flags);
2108                 if (mmc_bus_needs_resume(host)) {
2109                         spin_unlock_irqrestore(&host->lock, flags);
2110                         break;
2111                 }
2112                 host->rescan_disable = 1;
2113                 spin_unlock_irqrestore(&host->lock, flags);
2114                 if (cancel_delayed_work_sync(&host->detect))
2115                         wake_unlock(&host->detect_wake_lock);
2116
2117                 if (!host->bus_ops || host->bus_ops->suspend)
2118                         break;
2119
2120                 mmc_claim_host(host);
2121
2122                 if (host->bus_ops->remove)
2123                         host->bus_ops->remove(host);
2124
2125                 mmc_detach_bus(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");