Merge remote-tracking branch 'linux-2.6.32.y/master' into develop
[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  * Internal function. Schedule delayed work in the MMC work queue.
54  */
55 static int mmc_schedule_delayed_work(struct delayed_work *work,
56                                      unsigned long delay)
57 {
58         wake_lock(&mmc_delayed_work_wake_lock);
59         return queue_delayed_work(workqueue, work, delay);
60 }
61
62 /*
63  * Internal function. Flush all scheduled work from the MMC work queue.
64  */
65 static void mmc_flush_scheduled_work(void)
66 {
67         flush_workqueue(workqueue);
68 }
69
70 /**
71  *      mmc_request_done - finish processing an MMC request
72  *      @host: MMC host which completed request
73  *      @mrq: MMC request which request
74  *
75  *      MMC drivers should call this function when they have completed
76  *      their processing of a request.
77  */
78 void mmc_request_done(struct mmc_host *host, struct mmc_request *mrq)
79 {
80         struct mmc_command *cmd = mrq->cmd;
81         int err = cmd->error;
82
83         if (err && cmd->retries && mmc_host_is_spi(host)) {
84                 if (cmd->resp[0] & R1_SPI_ILLEGAL_COMMAND)
85                         cmd->retries = 0;
86         }
87
88         if (err && cmd->retries) {
89                 pr_debug("%s: req failed (CMD%u): %d, retrying...\n",
90                         mmc_hostname(host), cmd->opcode, err);
91
92                 cmd->retries--;
93                 cmd->error = 0;
94                 host->ops->request(host, mrq);
95         } else {
96                 led_trigger_event(host->led, LED_OFF);
97
98                 pr_debug("%s: req done (CMD%u): %d: %08x %08x %08x %08x\n",
99                         mmc_hostname(host), cmd->opcode, err,
100                         cmd->resp[0], cmd->resp[1],
101                         cmd->resp[2], cmd->resp[3]);
102
103                 if (mrq->data) {
104                         pr_debug("%s:     %d bytes transferred: %d\n",
105                                 mmc_hostname(host),
106                                 mrq->data->bytes_xfered, mrq->data->error);
107                 }
108
109                 if (mrq->stop) {
110                         pr_debug("%s:     (CMD%u): %d: %08x %08x %08x %08x\n",
111                                 mmc_hostname(host), mrq->stop->opcode,
112                                 mrq->stop->error,
113                                 mrq->stop->resp[0], mrq->stop->resp[1],
114                                 mrq->stop->resp[2], mrq->stop->resp[3]);
115                 }
116
117                 if (mrq->done)
118                         mrq->done(mrq);
119         }
120 }
121
122 EXPORT_SYMBOL(mmc_request_done);
123
124 static void
125 mmc_start_request(struct mmc_host *host, struct mmc_request *mrq)
126 {
127 #ifdef CONFIG_MMC_DEBUG
128         unsigned int i, sz;
129         struct scatterlist *sg;
130 #endif
131
132         pr_debug("%s: starting CMD%u arg %08x flags %08x\n",
133                  mmc_hostname(host), mrq->cmd->opcode,
134                  mrq->cmd->arg, mrq->cmd->flags);
135
136         if (mrq->data) {
137                 pr_debug("%s:     blksz %d blocks %d flags %08x "
138                         "tsac %d ms nsac %d\n",
139                         mmc_hostname(host), mrq->data->blksz,
140                         mrq->data->blocks, mrq->data->flags,
141                         mrq->data->timeout_ns / 1000000,
142                         mrq->data->timeout_clks);
143         }
144
145         if (mrq->stop) {
146                 pr_debug("%s:     CMD%u arg %08x flags %08x\n",
147                          mmc_hostname(host), mrq->stop->opcode,
148                          mrq->stop->arg, mrq->stop->flags);
149         }
150
151         WARN_ON(!host->claimed);
152
153         led_trigger_event(host->led, LED_FULL);
154
155         mrq->cmd->error = 0;
156         mrq->cmd->mrq = mrq;
157         if (mrq->data) {
158                 BUG_ON(mrq->data->blksz > host->max_blk_size);
159                 BUG_ON(mrq->data->blocks > host->max_blk_count);
160                 BUG_ON(mrq->data->blocks * mrq->data->blksz >
161                         host->max_req_size);
162
163 #ifdef CONFIG_MMC_DEBUG
164                 sz = 0;
165                 for_each_sg(mrq->data->sg, sg, mrq->data->sg_len, i)
166                         sz += sg->length;
167                 BUG_ON(sz != mrq->data->blocks * mrq->data->blksz);
168 #endif
169
170                 mrq->cmd->data = mrq->data;
171                 mrq->data->error = 0;
172                 mrq->data->mrq = mrq;
173                 if (mrq->stop) {
174                         mrq->data->stop = mrq->stop;
175                         mrq->stop->error = 0;
176                         mrq->stop->mrq = mrq;
177                 }
178         }
179         host->ops->request(host, mrq);
180 }
181
182 static void mmc_wait_done(struct mmc_request *mrq)
183 {
184         complete(mrq->done_data);
185 }
186
187 /**
188  *      mmc_wait_for_req - start a request and wait for completion
189  *      @host: MMC host to start command
190  *      @mrq: MMC request to start
191  *
192  *      Start a new MMC custom command request for a host, and wait
193  *      for the command to complete. Does not attempt to parse the
194  *      response.
195  */
196 void mmc_wait_for_req(struct mmc_host *host, struct mmc_request *mrq)
197 {
198         DECLARE_COMPLETION_ONSTACK(complete);
199
200         mrq->done_data = &complete;
201         mrq->done = mmc_wait_done;
202
203         mmc_start_request(host, mrq);
204
205         wait_for_completion(&complete);
206 }
207
208 EXPORT_SYMBOL(mmc_wait_for_req);
209
210 /**
211  *      mmc_wait_for_cmd - start a command and wait for completion
212  *      @host: MMC host to start command
213  *      @cmd: MMC command to start
214  *      @retries: maximum number of retries
215  *
216  *      Start a new MMC command for a host, and wait for the command
217  *      to complete.  Return any error that occurred while the command
218  *      was executing.  Do not attempt to parse the response.
219  */
220 int mmc_wait_for_cmd(struct mmc_host *host, struct mmc_command *cmd, int retries)
221 {
222         struct mmc_request mrq;
223
224         WARN_ON(!host->claimed);
225
226         memset(&mrq, 0, sizeof(struct mmc_request));
227
228         memset(cmd->resp, 0, sizeof(cmd->resp));
229         cmd->retries = retries;
230
231         mrq.cmd = cmd;
232         cmd->data = NULL;
233
234         mmc_wait_for_req(host, &mrq);
235
236         return cmd->error;
237 }
238
239 EXPORT_SYMBOL(mmc_wait_for_cmd);
240
241 /**
242  *      mmc_set_data_timeout - set the timeout for a data command
243  *      @data: data phase for command
244  *      @card: the MMC card associated with the data transfer
245  *
246  *      Computes the data timeout parameters according to the
247  *      correct algorithm given the card type.
248  */
249 void mmc_set_data_timeout(struct mmc_data *data, const struct mmc_card *card)
250 {
251         unsigned int mult;
252
253         /*
254          * SDIO cards only define an upper 1 s limit on access.
255          */
256         if (mmc_card_sdio(card)) {
257                 data->timeout_ns = 1000000000;
258                 data->timeout_clks = 0;
259                 return;
260         }
261
262         /*
263          * SD cards use a 100 multiplier rather than 10
264          */
265         mult = mmc_card_sd(card) ? 100 : 10;
266
267         /*
268          * Scale up the multiplier (and therefore the timeout) by
269          * the r2w factor for writes.
270          */
271         if (data->flags & MMC_DATA_WRITE)
272                 mult <<= card->csd.r2w_factor;
273
274         data->timeout_ns = card->csd.tacc_ns * mult;
275         data->timeout_clks = card->csd.tacc_clks * mult;
276
277         /*
278          * SD cards also have an upper limit on the timeout.
279          */
280         if (mmc_card_sd(card)) {
281                 unsigned int timeout_us, limit_us;
282
283                 timeout_us = data->timeout_ns / 1000;
284                 timeout_us += data->timeout_clks * 1000 /
285                         (card->host->ios.clock / 1000);
286
287                 if (data->flags & MMC_DATA_WRITE)
288                         /*
289                          * The limit is really 250 ms, but that is
290                          * insufficient for some crappy cards.
291                          */
292                         limit_us = 300000;
293                 else
294                         limit_us = 100000;
295
296                 /*
297                  * SDHC cards always use these fixed values.
298                  */
299                 if (timeout_us > limit_us || mmc_card_blockaddr(card)) {
300                         data->timeout_ns = limit_us * 1000;
301                         data->timeout_clks = 0;
302                 }
303         }
304         /*
305          * Some cards need very high timeouts if driven in SPI mode.
306          * The worst observed timeout was 900ms after writing a
307          * continuous stream of data until the internal logic
308          * overflowed.
309          */
310         if (mmc_host_is_spi(card->host)) {
311                 if (data->flags & MMC_DATA_WRITE) {
312                         if (data->timeout_ns < 1000000000)
313                                 data->timeout_ns = 1000000000;  /* 1s */
314                 } else {
315                         if (data->timeout_ns < 100000000)
316                                 data->timeout_ns =  100000000;  /* 100ms */
317                 }
318         }
319 }
320 EXPORT_SYMBOL(mmc_set_data_timeout);
321
322 /**
323  *      mmc_align_data_size - pads a transfer size to a more optimal value
324  *      @card: the MMC card associated with the data transfer
325  *      @sz: original transfer size
326  *
327  *      Pads the original data size with a number of extra bytes in
328  *      order to avoid controller bugs and/or performance hits
329  *      (e.g. some controllers revert to PIO for certain sizes).
330  *
331  *      Returns the improved size, which might be unmodified.
332  *
333  *      Note that this function is only relevant when issuing a
334  *      single scatter gather entry.
335  */
336 unsigned int mmc_align_data_size(struct mmc_card *card, unsigned int sz)
337 {
338         /*
339          * FIXME: We don't have a system for the controller to tell
340          * the core about its problems yet, so for now we just 32-bit
341          * align the size.
342          */
343         sz = ((sz + 3) / 4) * 4;
344
345         return sz;
346 }
347 EXPORT_SYMBOL(mmc_align_data_size);
348
349 /**
350  *      mmc_host_enable - enable a host.
351  *      @host: mmc host to enable
352  *
353  *      Hosts that support power saving can use the 'enable' and 'disable'
354  *      methods to exit and enter power saving states. For more information
355  *      see comments for struct mmc_host_ops.
356  */
357 int mmc_host_enable(struct mmc_host *host)
358 {
359         if (!(host->caps & MMC_CAP_DISABLE))
360                 return 0;
361
362         if (host->en_dis_recurs)
363                 return 0;
364
365         if (host->nesting_cnt++)
366                 return 0;
367
368         cancel_delayed_work_sync(&host->disable);
369
370         if (host->enabled)
371                 return 0;
372
373         if (host->ops->enable) {
374                 int err;
375
376                 host->en_dis_recurs = 1;
377                 err = host->ops->enable(host);
378                 host->en_dis_recurs = 0;
379
380                 if (err) {
381                         pr_debug("%s: enable error %d\n",
382                                  mmc_hostname(host), err);
383                         return err;
384                 }
385         }
386         host->enabled = 1;
387         return 0;
388 }
389 EXPORT_SYMBOL(mmc_host_enable);
390
391 static int mmc_host_do_disable(struct mmc_host *host, int lazy)
392 {
393         if (host->ops->disable) {
394                 int err;
395
396                 host->en_dis_recurs = 1;
397                 err = host->ops->disable(host, lazy);
398                 host->en_dis_recurs = 0;
399
400                 if (err < 0) {
401                         pr_debug("%s: disable error %d\n",
402                                  mmc_hostname(host), err);
403                         return err;
404                 }
405                 if (err > 0) {
406                         unsigned long delay = msecs_to_jiffies(err);
407
408                         mmc_schedule_delayed_work(&host->disable, delay);
409                 }
410         }
411         host->enabled = 0;
412         return 0;
413 }
414
415 /**
416  *      mmc_host_disable - disable a host.
417  *      @host: mmc host to disable
418  *
419  *      Hosts that support power saving can use the 'enable' and 'disable'
420  *      methods to exit and enter power saving states. For more information
421  *      see comments for struct mmc_host_ops.
422  */
423 int mmc_host_disable(struct mmc_host *host)
424 {
425         int err;
426
427         if (!(host->caps & MMC_CAP_DISABLE))
428                 return 0;
429
430         if (host->en_dis_recurs)
431                 return 0;
432
433         if (--host->nesting_cnt)
434                 return 0;
435
436         if (!host->enabled)
437                 return 0;
438
439         err = mmc_host_do_disable(host, 0);
440         return err;
441 }
442 EXPORT_SYMBOL(mmc_host_disable);
443
444 /**
445  *      __mmc_claim_host - exclusively claim a host
446  *      @host: mmc host to claim
447  *      @abort: whether or not the operation should be aborted
448  *
449  *      Claim a host for a set of operations.  If @abort is non null and
450  *      dereference a non-zero value then this will return prematurely with
451  *      that non-zero value without acquiring the lock.  Returns zero
452  *      with the lock held otherwise.
453  */
454 int __mmc_claim_host(struct mmc_host *host, atomic_t *abort)
455 {
456         DECLARE_WAITQUEUE(wait, current);
457         unsigned long flags;
458         int stop;
459
460         might_sleep();
461
462         add_wait_queue(&host->wq, &wait);
463         spin_lock_irqsave(&host->lock, flags);
464         while (1) {
465                 set_current_state(TASK_UNINTERRUPTIBLE);
466                 stop = abort ? atomic_read(abort) : 0;
467                 if (stop || !host->claimed || host->claimer == current)
468                         break;
469                 spin_unlock_irqrestore(&host->lock, flags);
470                 schedule();
471                 spin_lock_irqsave(&host->lock, flags);
472         }
473         set_current_state(TASK_RUNNING);
474         if (!stop) {
475                 host->claimed = 1;
476                 host->claimer = current;
477                 host->claim_cnt += 1;
478         } else
479                 wake_up(&host->wq);
480         spin_unlock_irqrestore(&host->lock, flags);
481         remove_wait_queue(&host->wq, &wait);
482         if (!stop)
483                 mmc_host_enable(host);
484         return stop;
485 }
486
487 EXPORT_SYMBOL(__mmc_claim_host);
488
489 /**
490  *      mmc_try_claim_host - try exclusively to claim a host
491  *      @host: mmc host to claim
492  *
493  *      Returns %1 if the host is claimed, %0 otherwise.
494  */
495 int mmc_try_claim_host(struct mmc_host *host)
496 {
497         int claimed_host = 0;
498         unsigned long flags;
499
500         spin_lock_irqsave(&host->lock, flags);
501         if (!host->claimed || host->claimer == current) {
502                 host->claimed = 1;
503                 host->claimer = current;
504                 host->claim_cnt += 1;
505                 claimed_host = 1;
506         }
507         spin_unlock_irqrestore(&host->lock, flags);
508         return claimed_host;
509 }
510 EXPORT_SYMBOL(mmc_try_claim_host);
511
512 static void mmc_do_release_host(struct mmc_host *host)
513 {
514         unsigned long flags;
515
516         spin_lock_irqsave(&host->lock, flags);
517         if (--host->claim_cnt) {
518                 /* Release for nested claim */
519                 spin_unlock_irqrestore(&host->lock, flags);
520         } else {
521                 host->claimed = 0;
522                 host->claimer = NULL;
523                 spin_unlock_irqrestore(&host->lock, flags);
524                 wake_up(&host->wq);
525         }
526 }
527
528 void mmc_host_deeper_disable(struct work_struct *work)
529 {
530         struct mmc_host *host =
531                 container_of(work, struct mmc_host, disable.work);
532
533         /* If the host is claimed then we do not want to disable it anymore */
534         if (!mmc_try_claim_host(host))
535                 goto out;
536         mmc_host_do_disable(host, 1);
537         mmc_do_release_host(host);
538
539 out:
540         wake_unlock(&mmc_delayed_work_wake_lock);
541 }
542
543 /**
544  *      mmc_host_lazy_disable - lazily disable a host.
545  *      @host: mmc host to disable
546  *
547  *      Hosts that support power saving can use the 'enable' and 'disable'
548  *      methods to exit and enter power saving states. For more information
549  *      see comments for struct mmc_host_ops.
550  */
551 int mmc_host_lazy_disable(struct mmc_host *host)
552 {
553         if (!(host->caps & MMC_CAP_DISABLE))
554                 return 0;
555
556         if (host->en_dis_recurs)
557                 return 0;
558
559         if (--host->nesting_cnt)
560                 return 0;
561
562         if (!host->enabled)
563                 return 0;
564
565         if (host->disable_delay) {
566                 mmc_schedule_delayed_work(&host->disable,
567                                 msecs_to_jiffies(host->disable_delay));
568                 return 0;
569         } else
570                 return mmc_host_do_disable(host, 1);
571 }
572 EXPORT_SYMBOL(mmc_host_lazy_disable);
573
574 /**
575  *      mmc_release_host - release a host
576  *      @host: mmc host to release
577  *
578  *      Release a MMC host, allowing others to claim the host
579  *      for their operations.
580  */
581 void mmc_release_host(struct mmc_host *host)
582 {
583         WARN_ON(!host->claimed);
584
585         mmc_host_lazy_disable(host);
586
587         mmc_do_release_host(host);
588 }
589
590 EXPORT_SYMBOL(mmc_release_host);
591
592 /*
593  * Internal function that does the actual ios call to the host driver,
594  * optionally printing some debug output.
595  */
596 static inline void mmc_set_ios(struct mmc_host *host)
597 {
598         struct mmc_ios *ios = &host->ios;
599
600         pr_debug("%s: clock %uHz busmode %u powermode %u cs %u Vdd %u "
601                 "width %u timing %u\n",
602                  mmc_hostname(host), ios->clock, ios->bus_mode,
603                  ios->power_mode, ios->chip_select, ios->vdd,
604                  ios->bus_width, ios->timing);
605
606         host->ops->set_ios(host, ios);
607 }
608
609 /*
610  * Control chip select pin on a host.
611  */
612 void mmc_set_chip_select(struct mmc_host *host, int mode)
613 {
614         host->ios.chip_select = mode;
615         mmc_set_ios(host);
616 }
617
618 /*
619  * Sets the host clock to the highest possible frequency that
620  * is below "hz".
621  */
622 void mmc_set_clock(struct mmc_host *host, unsigned int hz)
623 {
624         WARN_ON(hz < host->f_min);
625
626         if (hz > host->f_max)
627                 hz = host->f_max;
628
629         host->ios.clock = hz;
630         mmc_set_ios(host);
631 }
632
633 /*
634  * Change the bus mode (open drain/push-pull) of a host.
635  */
636 void mmc_set_bus_mode(struct mmc_host *host, unsigned int mode)
637 {
638         host->ios.bus_mode = mode;
639         mmc_set_ios(host);
640 }
641
642 /*
643  * Change data bus width of a host.
644  */
645 void mmc_set_bus_width(struct mmc_host *host, unsigned int width)
646 {
647         host->ios.bus_width = width;
648         mmc_set_ios(host);
649 }
650
651 /**
652  * mmc_vdd_to_ocrbitnum - Convert a voltage to the OCR bit number
653  * @vdd:        voltage (mV)
654  * @low_bits:   prefer low bits in boundary cases
655  *
656  * This function returns the OCR bit number according to the provided @vdd
657  * value. If conversion is not possible a negative errno value returned.
658  *
659  * Depending on the @low_bits flag the function prefers low or high OCR bits
660  * on boundary voltages. For example,
661  * with @low_bits = true, 3300 mV translates to ilog2(MMC_VDD_32_33);
662  * with @low_bits = false, 3300 mV translates to ilog2(MMC_VDD_33_34);
663  *
664  * Any value in the [1951:1999] range translates to the ilog2(MMC_VDD_20_21).
665  */
666 static int mmc_vdd_to_ocrbitnum(int vdd, bool low_bits)
667 {
668         const int max_bit = ilog2(MMC_VDD_35_36);
669         int bit;
670
671         if (vdd < 1650 || vdd > 3600)
672                 return -EINVAL;
673
674         if (vdd >= 1650 && vdd <= 1950)
675                 return ilog2(MMC_VDD_165_195);
676
677         if (low_bits)
678                 vdd -= 1;
679
680         /* Base 2000 mV, step 100 mV, bit's base 8. */
681         bit = (vdd - 2000) / 100 + 8;
682         if (bit > max_bit)
683                 return max_bit;
684         return bit;
685 }
686
687 /**
688  * mmc_vddrange_to_ocrmask - Convert a voltage range to the OCR mask
689  * @vdd_min:    minimum voltage value (mV)
690  * @vdd_max:    maximum voltage value (mV)
691  *
692  * This function returns the OCR mask bits according to the provided @vdd_min
693  * and @vdd_max values. If conversion is not possible the function returns 0.
694  *
695  * Notes wrt boundary cases:
696  * This function sets the OCR bits for all boundary voltages, for example
697  * [3300:3400] range is translated to MMC_VDD_32_33 | MMC_VDD_33_34 |
698  * MMC_VDD_34_35 mask.
699  */
700 u32 mmc_vddrange_to_ocrmask(int vdd_min, int vdd_max)
701 {
702         u32 mask = 0;
703
704         if (vdd_max < vdd_min)
705                 return 0;
706
707         /* Prefer high bits for the boundary vdd_max values. */
708         vdd_max = mmc_vdd_to_ocrbitnum(vdd_max, false);
709         if (vdd_max < 0)
710                 return 0;
711
712         /* Prefer low bits for the boundary vdd_min values. */
713         vdd_min = mmc_vdd_to_ocrbitnum(vdd_min, true);
714         if (vdd_min < 0)
715                 return 0;
716
717         /* Fill the mask, from max bit to min bit. */
718         while (vdd_max >= vdd_min)
719                 mask |= 1 << vdd_max--;
720
721         return mask;
722 }
723 EXPORT_SYMBOL(mmc_vddrange_to_ocrmask);
724
725 #ifdef CONFIG_REGULATOR
726
727 /**
728  * mmc_regulator_get_ocrmask - return mask of supported voltages
729  * @supply: regulator to use
730  *
731  * This returns either a negative errno, or a mask of voltages that
732  * can be provided to MMC/SD/SDIO devices using the specified voltage
733  * regulator.  This would normally be called before registering the
734  * MMC host adapter.
735  */
736 int mmc_regulator_get_ocrmask(struct regulator *supply)
737 {
738         int                     result = 0;
739         int                     count;
740         int                     i;
741
742         count = regulator_count_voltages(supply);
743         if (count < 0)
744                 return count;
745
746         for (i = 0; i < count; i++) {
747                 int             vdd_uV;
748                 int             vdd_mV;
749
750                 vdd_uV = regulator_list_voltage(supply, i);
751                 if (vdd_uV <= 0)
752                         continue;
753
754                 vdd_mV = vdd_uV / 1000;
755                 result |= mmc_vddrange_to_ocrmask(vdd_mV, vdd_mV);
756         }
757
758         return result;
759 }
760 EXPORT_SYMBOL(mmc_regulator_get_ocrmask);
761
762 /**
763  * mmc_regulator_set_ocr - set regulator to match host->ios voltage
764  * @vdd_bit: zero for power off, else a bit number (host->ios.vdd)
765  * @supply: regulator to use
766  *
767  * Returns zero on success, else negative errno.
768  *
769  * MMC host drivers may use this to enable or disable a regulator using
770  * a particular supply voltage.  This would normally be called from the
771  * set_ios() method.
772  */
773 int mmc_regulator_set_ocr(struct regulator *supply, unsigned short vdd_bit)
774 {
775         int                     result = 0;
776         int                     min_uV, max_uV;
777         int                     enabled;
778
779         enabled = regulator_is_enabled(supply);
780         if (enabled < 0)
781                 return enabled;
782
783         if (vdd_bit) {
784                 int             tmp;
785                 int             voltage;
786
787                 /* REVISIT mmc_vddrange_to_ocrmask() may have set some
788                  * bits this regulator doesn't quite support ... don't
789                  * be too picky, most cards and regulators are OK with
790                  * a 0.1V range goof (it's a small error percentage).
791                  */
792                 tmp = vdd_bit - ilog2(MMC_VDD_165_195);
793                 if (tmp == 0) {
794                         min_uV = 1650 * 1000;
795                         max_uV = 1950 * 1000;
796                 } else {
797                         min_uV = 1900 * 1000 + tmp * 100 * 1000;
798                         max_uV = min_uV + 100 * 1000;
799                 }
800
801                 /* avoid needless changes to this voltage; the regulator
802                  * might not allow this operation
803                  */
804                 voltage = regulator_get_voltage(supply);
805                 if (voltage < 0)
806                         result = voltage;
807                 else if (voltage < min_uV || voltage > max_uV)
808                         result = regulator_set_voltage(supply, min_uV, max_uV);
809                 else
810                         result = 0;
811
812                 if (result == 0 && !enabled)
813                         result = regulator_enable(supply);
814         } else if (enabled) {
815                 result = regulator_disable(supply);
816         }
817
818         return result;
819 }
820 EXPORT_SYMBOL(mmc_regulator_set_ocr);
821
822 #endif
823
824 /*
825  * Mask off any voltages we don't support and select
826  * the lowest voltage
827  */
828 u32 mmc_select_voltage(struct mmc_host *host, u32 ocr)
829 {
830         int bit;
831
832         ocr &= host->ocr_avail;
833
834         bit = ffs(ocr);
835         if (bit) {
836                 bit -= 1;
837
838                 ocr &= 3 << bit;
839
840                 host->ios.vdd = bit;
841                 mmc_set_ios(host);
842         } else {
843                 pr_warning("%s: host doesn't support card's voltages\n",
844                                 mmc_hostname(host));
845                 ocr = 0;
846         }
847
848         return ocr;
849 }
850
851 /*
852  * Select timing parameters for host.
853  */
854 void mmc_set_timing(struct mmc_host *host, unsigned int timing)
855 {
856         host->ios.timing = timing;
857         mmc_set_ios(host);
858 }
859
860 /*
861  * Apply power to the MMC stack.  This is a two-stage process.
862  * First, we enable power to the card without the clock running.
863  * We then wait a bit for the power to stabilise.  Finally,
864  * enable the bus drivers and clock to the card.
865  *
866  * We must _NOT_ enable the clock prior to power stablising.
867  *
868  * If a host does all the power sequencing itself, ignore the
869  * initial MMC_POWER_UP stage.
870  */
871 static void mmc_power_up(struct mmc_host *host)
872 {
873         int bit;
874
875         /* If ocr is set, we use it */
876         if (host->ocr)
877                 bit = ffs(host->ocr) - 1;
878         else
879                 bit = fls(host->ocr_avail) - 1;
880
881         host->ios.vdd = bit;
882         if (mmc_host_is_spi(host)) {
883                 host->ios.chip_select = MMC_CS_HIGH;
884                 host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
885         } else {
886                 host->ios.chip_select = MMC_CS_DONTCARE;
887                 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
888         }
889         host->ios.power_mode = MMC_POWER_UP;
890         host->ios.bus_width = MMC_BUS_WIDTH_1;
891         host->ios.timing = MMC_TIMING_LEGACY;
892         mmc_set_ios(host);
893
894         /*
895          * This delay should be sufficient to allow the power supply
896          * to reach the minimum voltage.
897          */
898         mmc_delay(10);
899
900         host->ios.clock = host->f_min;
901
902         host->ios.power_mode = MMC_POWER_ON;
903         mmc_set_ios(host);
904
905         /*
906          * This delay must be at least 74 clock sizes, or 1 ms, or the
907          * time required to reach a stable voltage.
908          */
909         mmc_delay(10);
910 }
911
912 static void mmc_power_off(struct mmc_host *host)
913 {
914         host->ios.clock = 0;
915         host->ios.vdd = 0;
916         if (!mmc_host_is_spi(host)) {
917                 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
918                 host->ios.chip_select = MMC_CS_DONTCARE;
919         }
920         host->ios.power_mode = MMC_POWER_OFF;
921         host->ios.bus_width = MMC_BUS_WIDTH_1;
922         host->ios.timing = MMC_TIMING_LEGACY;
923         mmc_set_ios(host);
924 }
925
926 /*
927  * Cleanup when the last reference to the bus operator is dropped.
928  */
929 static void __mmc_release_bus(struct mmc_host *host)
930 {
931         BUG_ON(!host);
932         BUG_ON(host->bus_refs);
933         BUG_ON(!host->bus_dead);
934
935         host->bus_ops = NULL;
936 }
937
938 /*
939  * Increase reference count of bus operator
940  */
941 static inline void mmc_bus_get(struct mmc_host *host)
942 {
943         unsigned long flags;
944
945         spin_lock_irqsave(&host->lock, flags);
946         host->bus_refs++;
947         spin_unlock_irqrestore(&host->lock, flags);
948 }
949
950 /*
951  * Decrease reference count of bus operator and free it if
952  * it is the last reference.
953  */
954 static inline void mmc_bus_put(struct mmc_host *host)
955 {
956         unsigned long flags;
957
958         spin_lock_irqsave(&host->lock, flags);
959         host->bus_refs--;
960         if ((host->bus_refs == 0) && host->bus_ops)
961                 __mmc_release_bus(host);
962         spin_unlock_irqrestore(&host->lock, flags);
963 }
964
965 int mmc_resume_bus(struct mmc_host *host)
966 {
967         if (!mmc_bus_needs_resume(host))
968                 return -EINVAL;
969
970         printk("%s: Starting deferred resume\n", mmc_hostname(host));
971         host->bus_resume_flags &= ~MMC_BUSRESUME_NEEDS_RESUME;
972         mmc_bus_get(host);
973         if (host->bus_ops && !host->bus_dead) {
974                 mmc_power_up(host);
975                 BUG_ON(!host->bus_ops->resume);
976                 host->bus_ops->resume(host);
977         }
978
979         if (host->bus_ops->detect && !host->bus_dead)
980                 host->bus_ops->detect(host);
981
982         mmc_bus_put(host);
983         printk("%s: Deferred resume completed\n", mmc_hostname(host));
984         return 0;
985 }
986
987 EXPORT_SYMBOL(mmc_resume_bus);
988
989 /*
990  * Assign a mmc bus handler to a host. Only one bus handler may control a
991  * host at any given time.
992  */
993 void mmc_attach_bus(struct mmc_host *host, const struct mmc_bus_ops *ops)
994 {
995         unsigned long flags;
996
997         BUG_ON(!host);
998         BUG_ON(!ops);
999
1000         WARN_ON(!host->claimed);
1001
1002         spin_lock_irqsave(&host->lock, flags);
1003
1004         BUG_ON(host->bus_ops);
1005         BUG_ON(host->bus_refs);
1006
1007         host->bus_ops = ops;
1008         host->bus_refs = 1;
1009         host->bus_dead = 0;
1010
1011         spin_unlock_irqrestore(&host->lock, flags);
1012 }
1013
1014 /*
1015  * Remove the current bus handler from a host. Assumes that there are
1016  * no interesting cards left, so the bus is powered down.
1017  */
1018 void mmc_detach_bus(struct mmc_host *host)
1019 {
1020         unsigned long flags;
1021
1022         BUG_ON(!host);
1023
1024         WARN_ON(!host->claimed);
1025         WARN_ON(!host->bus_ops);
1026
1027         spin_lock_irqsave(&host->lock, flags);
1028
1029         host->bus_dead = 1;
1030
1031         spin_unlock_irqrestore(&host->lock, flags);
1032
1033         mmc_power_off(host);
1034
1035         mmc_bus_put(host);
1036 }
1037
1038 /**
1039  *      mmc_detect_change - process change of state on a MMC socket
1040  *      @host: host which changed state.
1041  *      @delay: optional delay to wait before detection (jiffies)
1042  *
1043  *      MMC drivers should call this when they detect a card has been
1044  *      inserted or removed. The MMC layer will confirm that any
1045  *      present card is still functional, and initialize any newly
1046  *      inserted.
1047  */
1048 void mmc_detect_change(struct mmc_host *host, unsigned long delay)
1049 {
1050 #ifdef CONFIG_MMC_DEBUG
1051         unsigned long flags;
1052         spin_lock_irqsave(&host->lock, flags);
1053         WARN_ON(host->removed);
1054         spin_unlock_irqrestore(&host->lock, flags);
1055 #endif
1056
1057         mmc_schedule_delayed_work(&host->detect, delay);
1058 }
1059
1060 EXPORT_SYMBOL(mmc_detect_change);
1061
1062
1063 void mmc_rescan(struct work_struct *work)
1064 {
1065         struct mmc_host *host =
1066                 container_of(work, struct mmc_host, detect.work);
1067         u32 ocr;
1068         int err;
1069         int extend_wakelock = 0;
1070         unsigned long flags;
1071
1072         spin_lock_irqsave(&host->lock, flags);
1073
1074         if (host->rescan_disable) {
1075                 spin_unlock_irqrestore(&host->lock, flags);
1076                 return;
1077         }
1078
1079         spin_unlock_irqrestore(&host->lock, flags);
1080
1081
1082         mmc_bus_get(host);
1083
1084         /* if there is a card registered, check whether it is still present */
1085         if ((host->bus_ops != NULL) && host->bus_ops->detect && !host->bus_dead)
1086                 host->bus_ops->detect(host);
1087
1088         /* If the card was removed the bus will be marked
1089          * as dead - extend the wakelock so userspace
1090          * can respond */
1091         if (host->bus_dead)
1092                 extend_wakelock = 1;
1093
1094         mmc_bus_put(host);
1095
1096
1097         mmc_bus_get(host);
1098
1099         /* if there still is a card present, stop here */
1100         if (host->bus_ops != NULL) {
1101                 mmc_bus_put(host);
1102                 goto out;
1103         }
1104
1105         /* detect a newly inserted card */
1106
1107         /*
1108          * Only we can add a new handler, so it's safe to
1109          * release the lock here.
1110          */
1111         mmc_bus_put(host);
1112
1113         if (host->ops->get_cd && host->ops->get_cd(host) == 0)
1114                 goto out;
1115
1116         mmc_claim_host(host);
1117
1118         mmc_power_up(host);
1119         mmc_go_idle(host);
1120
1121         mmc_send_if_cond(host, host->ocr_avail);
1122
1123         /*
1124          * First we search for SDIO...
1125          */
1126         err = mmc_send_io_op_cond(host, 0, &ocr);
1127         if (!err) {
1128                 if (mmc_attach_sdio(host, ocr))
1129                         mmc_power_off(host);
1130                 extend_wakelock = 1;
1131                 goto out;
1132         }
1133
1134         /*
1135          * ...then normal SD...
1136          */
1137         err = mmc_send_app_op_cond(host, 0, &ocr);
1138         if (!err) {
1139                 if (mmc_attach_sd(host, ocr))
1140                         mmc_power_off(host);
1141                 extend_wakelock = 1;
1142                 goto out;
1143         }
1144
1145         /*
1146          * ...and finally MMC.
1147          */
1148         err = mmc_send_op_cond(host, 0, &ocr);
1149         if (!err) {
1150                 if (mmc_attach_mmc(host, ocr))
1151                         mmc_power_off(host);
1152                 extend_wakelock = 1;
1153                 goto out;
1154         }
1155
1156         mmc_release_host(host);
1157         mmc_power_off(host);
1158
1159 out:
1160         if (extend_wakelock)
1161                 wake_lock_timeout(&mmc_delayed_work_wake_lock, HZ / 2);
1162         else
1163                 wake_unlock(&mmc_delayed_work_wake_lock);
1164
1165         if (host->caps & MMC_CAP_NEEDS_POLL)
1166                 mmc_schedule_delayed_work(&host->detect, HZ);
1167 }
1168
1169 void mmc_start_host(struct mmc_host *host)
1170 {
1171         mmc_power_off(host);
1172         mmc_detect_change(host, 0);
1173 }
1174
1175 void mmc_stop_host(struct mmc_host *host)
1176 {
1177 #ifdef CONFIG_MMC_DEBUG
1178         unsigned long flags;
1179         spin_lock_irqsave(&host->lock, flags);
1180         host->removed = 1;
1181         spin_unlock_irqrestore(&host->lock, flags);
1182 #endif
1183
1184         if (host->caps & MMC_CAP_DISABLE)
1185                 cancel_delayed_work(&host->disable);
1186         cancel_delayed_work(&host->detect);
1187         mmc_flush_scheduled_work();
1188
1189         mmc_bus_get(host);
1190         if (host->bus_ops && !host->bus_dead) {
1191                 if (host->bus_ops->remove)
1192                         host->bus_ops->remove(host);
1193
1194                 mmc_claim_host(host);
1195                 mmc_detach_bus(host);
1196                 mmc_release_host(host);
1197                 mmc_bus_put(host);
1198                 return;
1199         }
1200         mmc_bus_put(host);
1201
1202         BUG_ON(host->card);
1203
1204         mmc_power_off(host);
1205 }
1206
1207 void mmc_power_save_host(struct mmc_host *host)
1208 {
1209         mmc_bus_get(host);
1210
1211         if (!host->bus_ops || host->bus_dead || !host->bus_ops->power_restore) {
1212                 mmc_bus_put(host);
1213                 return;
1214         }
1215
1216         if (host->bus_ops->power_save)
1217                 host->bus_ops->power_save(host);
1218
1219         mmc_bus_put(host);
1220
1221         mmc_power_off(host);
1222 }
1223 EXPORT_SYMBOL(mmc_power_save_host);
1224
1225 void mmc_power_restore_host(struct mmc_host *host)
1226 {
1227         mmc_bus_get(host);
1228
1229         if (!host->bus_ops || host->bus_dead || !host->bus_ops->power_restore) {
1230                 mmc_bus_put(host);
1231                 return;
1232         }
1233
1234         mmc_power_up(host);
1235         host->bus_ops->power_restore(host);
1236
1237         mmc_bus_put(host);
1238 }
1239 EXPORT_SYMBOL(mmc_power_restore_host);
1240
1241 int mmc_card_awake(struct mmc_host *host)
1242 {
1243         int err = -ENOSYS;
1244
1245         mmc_bus_get(host);
1246
1247         if (host->bus_ops && !host->bus_dead && host->bus_ops->awake)
1248                 err = host->bus_ops->awake(host);
1249
1250         mmc_bus_put(host);
1251
1252         return err;
1253 }
1254 EXPORT_SYMBOL(mmc_card_awake);
1255
1256 int mmc_card_sleep(struct mmc_host *host)
1257 {
1258         int err = -ENOSYS;
1259
1260         mmc_bus_get(host);
1261
1262         if (host->bus_ops && !host->bus_dead && host->bus_ops->awake)
1263                 err = host->bus_ops->sleep(host);
1264
1265         mmc_bus_put(host);
1266
1267         return err;
1268 }
1269 EXPORT_SYMBOL(mmc_card_sleep);
1270
1271 int mmc_card_can_sleep(struct mmc_host *host)
1272 {
1273         struct mmc_card *card = host->card;
1274
1275         if (card && mmc_card_mmc(card) && card->ext_csd.rev >= 3)
1276                 return 1;
1277         return 0;
1278 }
1279 EXPORT_SYMBOL(mmc_card_can_sleep);
1280
1281 #ifdef CONFIG_PM
1282
1283 /**
1284  *      mmc_suspend_host - suspend a host
1285  *      @host: mmc host
1286  *      @state: suspend mode (PM_SUSPEND_xxx)
1287  */
1288 int mmc_suspend_host(struct mmc_host *host, pm_message_t state)
1289 {
1290         int err = 0;
1291
1292         if (mmc_bus_needs_resume(host))
1293                 return 0;
1294
1295         if (host->caps & MMC_CAP_DISABLE)
1296                 cancel_delayed_work(&host->disable);
1297         cancel_delayed_work(&host->detect);
1298         mmc_flush_scheduled_work();
1299
1300         mmc_bus_get(host);
1301         if (host->bus_ops && !host->bus_dead) {
1302                 if (host->bus_ops->suspend)
1303                         err = host->bus_ops->suspend(host);
1304         }
1305         mmc_bus_put(host);
1306
1307         if (!err)
1308                 mmc_power_off(host);
1309
1310         return err;
1311 }
1312
1313 EXPORT_SYMBOL(mmc_suspend_host);
1314
1315 /**
1316  *      mmc_resume_host - resume a previously suspended host
1317  *      @host: mmc host
1318  */
1319 int mmc_resume_host(struct mmc_host *host)
1320 {
1321         int err = 0;
1322
1323         mmc_bus_get(host);
1324         if (host->bus_resume_flags & MMC_BUSRESUME_MANUAL_RESUME) {
1325                 host->bus_resume_flags |= MMC_BUSRESUME_NEEDS_RESUME;
1326                 mmc_bus_put(host);
1327                 return 0;
1328         }
1329
1330         if (host->bus_ops && !host->bus_dead) {
1331                 mmc_power_up(host);
1332                 mmc_select_voltage(host, host->ocr);
1333                 BUG_ON(!host->bus_ops->resume);
1334                 err = host->bus_ops->resume(host);
1335                 if (err) {
1336                         printk(KERN_WARNING "%s: error %d during resume "
1337                                             "(card was removed?)\n",
1338                                             mmc_hostname(host), err);
1339                         err = 0;
1340                 }
1341         }
1342         mmc_bus_put(host);
1343
1344         return err;
1345 }
1346 EXPORT_SYMBOL(mmc_resume_host);
1347
1348 /* Do the card removal on suspend if card is assumed removeable
1349  * Do that in pm notifier while userspace isn't yet frozen, so we will be able
1350    to sync the card.
1351 */
1352 int mmc_pm_notify(struct notifier_block *notify_block,
1353                                         unsigned long mode, void *unused)
1354 {
1355         struct mmc_host *host = container_of(
1356                 notify_block, struct mmc_host, pm_notify);
1357         unsigned long flags;
1358
1359
1360         switch (mode) {
1361         case PM_HIBERNATION_PREPARE:
1362         case PM_SUSPEND_PREPARE:
1363
1364                 spin_lock_irqsave(&host->lock, flags);
1365                 host->rescan_disable = 1;
1366                 spin_unlock_irqrestore(&host->lock, flags);
1367                 cancel_delayed_work_sync(&host->detect);
1368
1369                 if (!host->bus_ops || host->bus_ops->suspend)
1370                         break;
1371
1372                 mmc_claim_host(host);
1373
1374                 if (host->bus_ops->remove)
1375                         host->bus_ops->remove(host);
1376
1377                 mmc_detach_bus(host);
1378                 mmc_release_host(host);
1379                 break;
1380
1381         case PM_POST_SUSPEND:
1382         case PM_POST_HIBERNATION:
1383
1384                 spin_lock_irqsave(&host->lock, flags);
1385                 host->rescan_disable = 0;
1386                 spin_unlock_irqrestore(&host->lock, flags);
1387                 mmc_detect_change(host, 0);
1388
1389         }
1390
1391         return 0;
1392 }
1393 #endif
1394
1395 #ifdef CONFIG_MMC_EMBEDDED_SDIO
1396 void mmc_set_embedded_sdio_data(struct mmc_host *host,
1397                                 struct sdio_cis *cis,
1398                                 struct sdio_cccr *cccr,
1399                                 struct sdio_embedded_func *funcs,
1400                                 int num_funcs)
1401 {
1402         host->embedded_sdio_data.cis = cis;
1403         host->embedded_sdio_data.cccr = cccr;
1404         host->embedded_sdio_data.funcs = funcs;
1405         host->embedded_sdio_data.num_funcs = num_funcs;
1406 }
1407
1408 EXPORT_SYMBOL(mmc_set_embedded_sdio_data);
1409 #endif
1410
1411 static int __init mmc_init(void)
1412 {
1413         int ret;
1414
1415         wake_lock_init(&mmc_delayed_work_wake_lock, WAKE_LOCK_SUSPEND, "mmc_delayed_work");
1416
1417         workqueue = create_singlethread_workqueue("kmmcd");
1418         if (!workqueue)
1419                 return -ENOMEM;
1420
1421         ret = mmc_register_bus();
1422         if (ret)
1423                 goto destroy_workqueue;
1424
1425         ret = mmc_register_host_class();
1426         if (ret)
1427                 goto unregister_bus;
1428
1429         ret = sdio_register_bus();
1430         if (ret)
1431                 goto unregister_host_class;
1432
1433         return 0;
1434
1435 unregister_host_class:
1436         mmc_unregister_host_class();
1437 unregister_bus:
1438         mmc_unregister_bus();
1439 destroy_workqueue:
1440         destroy_workqueue(workqueue);
1441
1442         return ret;
1443 }
1444
1445 static void __exit mmc_exit(void)
1446 {
1447         sdio_unregister_bus();
1448         mmc_unregister_host_class();
1449         mmc_unregister_bus();
1450         destroy_workqueue(workqueue);
1451         wake_lock_destroy(&mmc_delayed_work_wake_lock);
1452 }
1453
1454 subsys_initcall(mmc_init);
1455 module_exit(mmc_exit);
1456
1457 MODULE_LICENSE("GPL");