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