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