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