OSDN Git Service

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