de01a899d01f83124064a794fd6e964579bb5796
[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                 return;
536         mmc_host_do_disable(host, 1);
537         mmc_do_release_host(host);
538 }
539
540 /**
541  *      mmc_host_lazy_disable - lazily disable a host.
542  *      @host: mmc host to disable
543  *
544  *      Hosts that support power saving can use the 'enable' and 'disable'
545  *      methods to exit and enter power saving states. For more information
546  *      see comments for struct mmc_host_ops.
547  */
548 int mmc_host_lazy_disable(struct mmc_host *host)
549 {
550         if (!(host->caps & MMC_CAP_DISABLE))
551                 return 0;
552
553         if (host->en_dis_recurs)
554                 return 0;
555
556         if (--host->nesting_cnt)
557                 return 0;
558
559         if (!host->enabled)
560                 return 0;
561
562         if (host->disable_delay) {
563                 mmc_schedule_delayed_work(&host->disable,
564                                 msecs_to_jiffies(host->disable_delay));
565                 return 0;
566         } else
567                 return mmc_host_do_disable(host, 1);
568 }
569 EXPORT_SYMBOL(mmc_host_lazy_disable);
570
571 /**
572  *      mmc_release_host - release a host
573  *      @host: mmc host to release
574  *
575  *      Release a MMC host, allowing others to claim the host
576  *      for their operations.
577  */
578 void mmc_release_host(struct mmc_host *host)
579 {
580         WARN_ON(!host->claimed);
581
582         mmc_host_lazy_disable(host);
583
584         mmc_do_release_host(host);
585 }
586
587 EXPORT_SYMBOL(mmc_release_host);
588
589 /*
590  * Internal function that does the actual ios call to the host driver,
591  * optionally printing some debug output.
592  */
593 static inline void mmc_set_ios(struct mmc_host *host)
594 {
595         struct mmc_ios *ios = &host->ios;
596
597         pr_debug("%s: clock %uHz busmode %u powermode %u cs %u Vdd %u "
598                 "width %u timing %u\n",
599                  mmc_hostname(host), ios->clock, ios->bus_mode,
600                  ios->power_mode, ios->chip_select, ios->vdd,
601                  ios->bus_width, ios->timing);
602
603         host->ops->set_ios(host, ios);
604 }
605
606 /*
607  * Control chip select pin on a host.
608  */
609 void mmc_set_chip_select(struct mmc_host *host, int mode)
610 {
611         host->ios.chip_select = mode;
612         mmc_set_ios(host);
613 }
614
615 /*
616  * Sets the host clock to the highest possible frequency that
617  * is below "hz".
618  */
619 void mmc_set_clock(struct mmc_host *host, unsigned int hz)
620 {
621         WARN_ON(hz < host->f_min);
622
623         if (hz > host->f_max)
624                 hz = host->f_max;
625
626         host->ios.clock = hz;
627         mmc_set_ios(host);
628 }
629
630 /*
631  * Change the bus mode (open drain/push-pull) of a host.
632  */
633 void mmc_set_bus_mode(struct mmc_host *host, unsigned int mode)
634 {
635         host->ios.bus_mode = mode;
636         mmc_set_ios(host);
637 }
638
639 /*
640  * Change data bus width of a host.
641  */
642 void mmc_set_bus_width(struct mmc_host *host, unsigned int width)
643 {
644         host->ios.bus_width = width;
645         mmc_set_ios(host);
646 }
647
648 /**
649  * mmc_vdd_to_ocrbitnum - Convert a voltage to the OCR bit number
650  * @vdd:        voltage (mV)
651  * @low_bits:   prefer low bits in boundary cases
652  *
653  * This function returns the OCR bit number according to the provided @vdd
654  * value. If conversion is not possible a negative errno value returned.
655  *
656  * Depending on the @low_bits flag the function prefers low or high OCR bits
657  * on boundary voltages. For example,
658  * with @low_bits = true, 3300 mV translates to ilog2(MMC_VDD_32_33);
659  * with @low_bits = false, 3300 mV translates to ilog2(MMC_VDD_33_34);
660  *
661  * Any value in the [1951:1999] range translates to the ilog2(MMC_VDD_20_21).
662  */
663 static int mmc_vdd_to_ocrbitnum(int vdd, bool low_bits)
664 {
665         const int max_bit = ilog2(MMC_VDD_35_36);
666         int bit;
667
668         if (vdd < 1650 || vdd > 3600)
669                 return -EINVAL;
670
671         if (vdd >= 1650 && vdd <= 1950)
672                 return ilog2(MMC_VDD_165_195);
673
674         if (low_bits)
675                 vdd -= 1;
676
677         /* Base 2000 mV, step 100 mV, bit's base 8. */
678         bit = (vdd - 2000) / 100 + 8;
679         if (bit > max_bit)
680                 return max_bit;
681         return bit;
682 }
683
684 /**
685  * mmc_vddrange_to_ocrmask - Convert a voltage range to the OCR mask
686  * @vdd_min:    minimum voltage value (mV)
687  * @vdd_max:    maximum voltage value (mV)
688  *
689  * This function returns the OCR mask bits according to the provided @vdd_min
690  * and @vdd_max values. If conversion is not possible the function returns 0.
691  *
692  * Notes wrt boundary cases:
693  * This function sets the OCR bits for all boundary voltages, for example
694  * [3300:3400] range is translated to MMC_VDD_32_33 | MMC_VDD_33_34 |
695  * MMC_VDD_34_35 mask.
696  */
697 u32 mmc_vddrange_to_ocrmask(int vdd_min, int vdd_max)
698 {
699         u32 mask = 0;
700
701         if (vdd_max < vdd_min)
702                 return 0;
703
704         /* Prefer high bits for the boundary vdd_max values. */
705         vdd_max = mmc_vdd_to_ocrbitnum(vdd_max, false);
706         if (vdd_max < 0)
707                 return 0;
708
709         /* Prefer low bits for the boundary vdd_min values. */
710         vdd_min = mmc_vdd_to_ocrbitnum(vdd_min, true);
711         if (vdd_min < 0)
712                 return 0;
713
714         /* Fill the mask, from max bit to min bit. */
715         while (vdd_max >= vdd_min)
716                 mask |= 1 << vdd_max--;
717
718         return mask;
719 }
720 EXPORT_SYMBOL(mmc_vddrange_to_ocrmask);
721
722 #ifdef CONFIG_REGULATOR
723
724 /**
725  * mmc_regulator_get_ocrmask - return mask of supported voltages
726  * @supply: regulator to use
727  *
728  * This returns either a negative errno, or a mask of voltages that
729  * can be provided to MMC/SD/SDIO devices using the specified voltage
730  * regulator.  This would normally be called before registering the
731  * MMC host adapter.
732  */
733 int mmc_regulator_get_ocrmask(struct regulator *supply)
734 {
735         int                     result = 0;
736         int                     count;
737         int                     i;
738
739         count = regulator_count_voltages(supply);
740         if (count < 0)
741                 return count;
742
743         for (i = 0; i < count; i++) {
744                 int             vdd_uV;
745                 int             vdd_mV;
746
747                 vdd_uV = regulator_list_voltage(supply, i);
748                 if (vdd_uV <= 0)
749                         continue;
750
751                 vdd_mV = vdd_uV / 1000;
752                 result |= mmc_vddrange_to_ocrmask(vdd_mV, vdd_mV);
753         }
754
755         return result;
756 }
757 EXPORT_SYMBOL(mmc_regulator_get_ocrmask);
758
759 /**
760  * mmc_regulator_set_ocr - set regulator to match host->ios voltage
761  * @vdd_bit: zero for power off, else a bit number (host->ios.vdd)
762  * @supply: regulator to use
763  *
764  * Returns zero on success, else negative errno.
765  *
766  * MMC host drivers may use this to enable or disable a regulator using
767  * a particular supply voltage.  This would normally be called from the
768  * set_ios() method.
769  */
770 int mmc_regulator_set_ocr(struct regulator *supply, unsigned short vdd_bit)
771 {
772         int                     result = 0;
773         int                     min_uV, max_uV;
774         int                     enabled;
775
776         enabled = regulator_is_enabled(supply);
777         if (enabled < 0)
778                 return enabled;
779
780         if (vdd_bit) {
781                 int             tmp;
782                 int             voltage;
783
784                 /* REVISIT mmc_vddrange_to_ocrmask() may have set some
785                  * bits this regulator doesn't quite support ... don't
786                  * be too picky, most cards and regulators are OK with
787                  * a 0.1V range goof (it's a small error percentage).
788                  */
789                 tmp = vdd_bit - ilog2(MMC_VDD_165_195);
790                 if (tmp == 0) {
791                         min_uV = 1650 * 1000;
792                         max_uV = 1950 * 1000;
793                 } else {
794                         min_uV = 1900 * 1000 + tmp * 100 * 1000;
795                         max_uV = min_uV + 100 * 1000;
796                 }
797
798                 /* avoid needless changes to this voltage; the regulator
799                  * might not allow this operation
800                  */
801                 voltage = regulator_get_voltage(supply);
802                 if (voltage < 0)
803                         result = voltage;
804                 else if (voltage < min_uV || voltage > max_uV)
805                         result = regulator_set_voltage(supply, min_uV, max_uV);
806                 else
807                         result = 0;
808
809                 if (result == 0 && !enabled)
810                         result = regulator_enable(supply);
811         } else if (enabled) {
812                 result = regulator_disable(supply);
813         }
814
815         return result;
816 }
817 EXPORT_SYMBOL(mmc_regulator_set_ocr);
818
819 #endif
820
821 /*
822  * Mask off any voltages we don't support and select
823  * the lowest voltage
824  */
825 u32 mmc_select_voltage(struct mmc_host *host, u32 ocr)
826 {
827         int bit;
828
829         ocr &= host->ocr_avail;
830
831         bit = ffs(ocr);
832         if (bit) {
833                 bit -= 1;
834
835                 ocr &= 3 << bit;
836
837                 host->ios.vdd = bit;
838                 mmc_set_ios(host);
839         } else {
840                 pr_warning("%s: host doesn't support card's voltages\n",
841                                 mmc_hostname(host));
842                 ocr = 0;
843         }
844
845         return ocr;
846 }
847
848 /*
849  * Select timing parameters for host.
850  */
851 void mmc_set_timing(struct mmc_host *host, unsigned int timing)
852 {
853         host->ios.timing = timing;
854         mmc_set_ios(host);
855 }
856
857 /*
858  * Apply power to the MMC stack.  This is a two-stage process.
859  * First, we enable power to the card without the clock running.
860  * We then wait a bit for the power to stabilise.  Finally,
861  * enable the bus drivers and clock to the card.
862  *
863  * We must _NOT_ enable the clock prior to power stablising.
864  *
865  * If a host does all the power sequencing itself, ignore the
866  * initial MMC_POWER_UP stage.
867  */
868 static void mmc_power_up(struct mmc_host *host)
869 {
870         int bit;
871
872         /* If ocr is set, we use it */
873         if (host->ocr)
874                 bit = ffs(host->ocr) - 1;
875         else
876                 bit = fls(host->ocr_avail) - 1;
877
878         host->ios.vdd = bit;
879         if (mmc_host_is_spi(host)) {
880                 host->ios.chip_select = MMC_CS_HIGH;
881                 host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
882         } else {
883                 host->ios.chip_select = MMC_CS_DONTCARE;
884                 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
885         }
886         host->ios.power_mode = MMC_POWER_UP;
887         host->ios.bus_width = MMC_BUS_WIDTH_1;
888         host->ios.timing = MMC_TIMING_LEGACY;
889         mmc_set_ios(host);
890
891         /*
892          * This delay should be sufficient to allow the power supply
893          * to reach the minimum voltage.
894          */
895         mmc_delay(10);
896
897         if (host->f_min > 400000) {
898                 pr_warning("%s: Minimum clock frequency too high for "
899                                 "identification mode\n", mmc_hostname(host));
900                 host->ios.clock = host->f_min;
901         } else
902                 host->ios.clock = 400000;
903
904         host->ios.power_mode = MMC_POWER_ON;
905         mmc_set_ios(host);
906
907         /*
908          * This delay must be at least 74 clock sizes, or 1 ms, or the
909          * time required to reach a stable voltage.
910          */
911         mmc_delay(10);
912 }
913
914 static void mmc_power_off(struct mmc_host *host)
915 {
916         host->ios.clock = 0;
917         host->ios.vdd = 0;
918         if (!mmc_host_is_spi(host)) {
919                 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
920                 host->ios.chip_select = MMC_CS_DONTCARE;
921         }
922         host->ios.power_mode = MMC_POWER_OFF;
923         host->ios.bus_width = MMC_BUS_WIDTH_1;
924         host->ios.timing = MMC_TIMING_LEGACY;
925         mmc_set_ios(host);
926 }
927
928 /*
929  * Cleanup when the last reference to the bus operator is dropped.
930  */
931 static void __mmc_release_bus(struct mmc_host *host)
932 {
933         BUG_ON(!host);
934         BUG_ON(host->bus_refs);
935         BUG_ON(!host->bus_dead);
936
937         host->bus_ops = NULL;
938 }
939
940 /*
941  * Increase reference count of bus operator
942  */
943 static inline void mmc_bus_get(struct mmc_host *host)
944 {
945         unsigned long flags;
946
947         spin_lock_irqsave(&host->lock, flags);
948         host->bus_refs++;
949         spin_unlock_irqrestore(&host->lock, flags);
950 }
951
952 /*
953  * Decrease reference count of bus operator and free it if
954  * it is the last reference.
955  */
956 static inline void mmc_bus_put(struct mmc_host *host)
957 {
958         unsigned long flags;
959
960         spin_lock_irqsave(&host->lock, flags);
961         host->bus_refs--;
962         if ((host->bus_refs == 0) && host->bus_ops)
963                 __mmc_release_bus(host);
964         spin_unlock_irqrestore(&host->lock, flags);
965 }
966
967 int mmc_resume_bus(struct mmc_host *host)
968 {
969         if (!mmc_bus_needs_resume(host))
970                 return -EINVAL;
971
972         printk("%s: Starting deferred resume\n", mmc_hostname(host));
973         host->bus_resume_flags &= ~MMC_BUSRESUME_NEEDS_RESUME;
974         mmc_bus_get(host);
975         if (host->bus_ops && !host->bus_dead) {
976                 mmc_power_up(host);
977                 BUG_ON(!host->bus_ops->resume);
978                 host->bus_ops->resume(host);
979         }
980
981         if (host->bus_ops->detect && !host->bus_dead)
982                 host->bus_ops->detect(host);
983
984         mmc_bus_put(host);
985         printk("%s: Deferred resume completed\n", mmc_hostname(host));
986         return 0;
987 }
988
989 EXPORT_SYMBOL(mmc_resume_bus);
990
991 /*
992  * Assign a mmc bus handler to a host. Only one bus handler may control a
993  * host at any given time.
994  */
995 void mmc_attach_bus(struct mmc_host *host, const struct mmc_bus_ops *ops)
996 {
997         unsigned long flags;
998
999         BUG_ON(!host);
1000         BUG_ON(!ops);
1001
1002         WARN_ON(!host->claimed);
1003
1004         spin_lock_irqsave(&host->lock, flags);
1005
1006         BUG_ON(host->bus_ops);
1007         BUG_ON(host->bus_refs);
1008
1009         host->bus_ops = ops;
1010         host->bus_refs = 1;
1011         host->bus_dead = 0;
1012
1013         spin_unlock_irqrestore(&host->lock, flags);
1014 }
1015
1016 /*
1017  * Remove the current bus handler from a host. Assumes that there are
1018  * no interesting cards left, so the bus is powered down.
1019  */
1020 void mmc_detach_bus(struct mmc_host *host)
1021 {
1022         unsigned long flags;
1023
1024         BUG_ON(!host);
1025
1026         WARN_ON(!host->claimed);
1027         WARN_ON(!host->bus_ops);
1028
1029         spin_lock_irqsave(&host->lock, flags);
1030
1031         host->bus_dead = 1;
1032
1033         spin_unlock_irqrestore(&host->lock, flags);
1034
1035         mmc_power_off(host);
1036
1037         mmc_bus_put(host);
1038 }
1039
1040 /**
1041  *      mmc_detect_change - process change of state on a MMC socket
1042  *      @host: host which changed state.
1043  *      @delay: optional delay to wait before detection (jiffies)
1044  *
1045  *      MMC drivers should call this when they detect a card has been
1046  *      inserted or removed. The MMC layer will confirm that any
1047  *      present card is still functional, and initialize any newly
1048  *      inserted.
1049  */
1050 void mmc_detect_change(struct mmc_host *host, unsigned long delay)
1051 {
1052 #ifdef CONFIG_MMC_DEBUG
1053         unsigned long flags;
1054         spin_lock_irqsave(&host->lock, flags);
1055         WARN_ON(host->removed);
1056         spin_unlock_irqrestore(&host->lock, flags);
1057 #endif
1058
1059         mmc_schedule_delayed_work(&host->detect, delay);
1060 }
1061
1062 EXPORT_SYMBOL(mmc_detect_change);
1063
1064
1065 void mmc_rescan(struct work_struct *work)
1066 {
1067         struct mmc_host *host =
1068                 container_of(work, struct mmc_host, detect.work);
1069         u32 ocr;
1070         int err;
1071         int extend_wakelock = 0;
1072
1073         mmc_bus_get(host);
1074
1075         /* if there is a card registered, check whether it is still present */
1076         if ((host->bus_ops != NULL) && host->bus_ops->detect && !host->bus_dead)
1077                 host->bus_ops->detect(host);
1078
1079         /* If the card was removed the bus will be marked
1080          * as dead - extend the wakelock so userspace
1081          * can respond */
1082         if (host->bus_dead)
1083                 extend_wakelock = 1;
1084
1085         mmc_bus_put(host);
1086
1087
1088         mmc_bus_get(host);
1089
1090         /* if there still is a card present, stop here */
1091         if (host->bus_ops != NULL) {
1092                 mmc_bus_put(host);
1093                 goto out;
1094         }
1095
1096         /* detect a newly inserted card */
1097
1098         /*
1099          * Only we can add a new handler, so it's safe to
1100          * release the lock here.
1101          */
1102         mmc_bus_put(host);
1103
1104         if (host->ops->get_cd && host->ops->get_cd(host) == 0)
1105                 goto out;
1106
1107         mmc_claim_host(host);
1108
1109         mmc_power_up(host);
1110         mmc_go_idle(host);
1111
1112         mmc_send_if_cond(host, host->ocr_avail);
1113
1114         /*
1115          * First we search for SDIO...
1116          */
1117         err = mmc_send_io_op_cond(host, 0, &ocr);
1118         if (!err) {
1119                 if (mmc_attach_sdio(host, ocr))
1120                         mmc_power_off(host);
1121                 extend_wakelock = 1;
1122                 goto out;
1123         }
1124
1125         /*
1126          * ...then normal SD...
1127          */
1128         err = mmc_send_app_op_cond(host, 0, &ocr);
1129         if (!err) {
1130                 if (mmc_attach_sd(host, ocr))
1131                         mmc_power_off(host);
1132                 extend_wakelock = 1;
1133                 goto out;
1134         }
1135
1136         /*
1137          * ...and finally MMC.
1138          */
1139         err = mmc_send_op_cond(host, 0, &ocr);
1140         if (!err) {
1141                 if (mmc_attach_mmc(host, ocr))
1142                         mmc_power_off(host);
1143                 extend_wakelock = 1;
1144                 goto out;
1145         }
1146
1147         mmc_release_host(host);
1148         mmc_power_off(host);
1149
1150 out:
1151         if (extend_wakelock)
1152                 wake_lock_timeout(&mmc_delayed_work_wake_lock, HZ / 2);
1153         else
1154                 wake_unlock(&mmc_delayed_work_wake_lock);
1155
1156         if (host->caps & MMC_CAP_NEEDS_POLL)
1157                 mmc_schedule_delayed_work(&host->detect, HZ);
1158 }
1159
1160 void mmc_start_host(struct mmc_host *host)
1161 {
1162         mmc_power_off(host);
1163         mmc_detect_change(host, 0);
1164 }
1165
1166 void mmc_stop_host(struct mmc_host *host)
1167 {
1168 #ifdef CONFIG_MMC_DEBUG
1169         unsigned long flags;
1170         spin_lock_irqsave(&host->lock, flags);
1171         host->removed = 1;
1172         spin_unlock_irqrestore(&host->lock, flags);
1173 #endif
1174
1175         if (host->caps & MMC_CAP_DISABLE)
1176                 cancel_delayed_work(&host->disable);
1177         cancel_delayed_work(&host->detect);
1178         mmc_flush_scheduled_work();
1179
1180         mmc_bus_get(host);
1181         if (host->bus_ops && !host->bus_dead) {
1182                 if (host->bus_ops->remove)
1183                         host->bus_ops->remove(host);
1184
1185                 mmc_claim_host(host);
1186                 mmc_detach_bus(host);
1187                 mmc_release_host(host);
1188                 mmc_bus_put(host);
1189                 return;
1190         }
1191         mmc_bus_put(host);
1192
1193         BUG_ON(host->card);
1194
1195         mmc_power_off(host);
1196 }
1197
1198 void mmc_power_save_host(struct mmc_host *host)
1199 {
1200         mmc_bus_get(host);
1201
1202         if (!host->bus_ops || host->bus_dead || !host->bus_ops->power_restore) {
1203                 mmc_bus_put(host);
1204                 return;
1205         }
1206
1207         if (host->bus_ops->power_save)
1208                 host->bus_ops->power_save(host);
1209
1210         mmc_bus_put(host);
1211
1212         mmc_power_off(host);
1213 }
1214 EXPORT_SYMBOL(mmc_power_save_host);
1215
1216 void mmc_power_restore_host(struct mmc_host *host)
1217 {
1218         mmc_bus_get(host);
1219
1220         if (!host->bus_ops || host->bus_dead || !host->bus_ops->power_restore) {
1221                 mmc_bus_put(host);
1222                 return;
1223         }
1224
1225         mmc_power_up(host);
1226         host->bus_ops->power_restore(host);
1227
1228         mmc_bus_put(host);
1229 }
1230 EXPORT_SYMBOL(mmc_power_restore_host);
1231
1232 int mmc_card_awake(struct mmc_host *host)
1233 {
1234         int err = -ENOSYS;
1235
1236         mmc_bus_get(host);
1237
1238         if (host->bus_ops && !host->bus_dead && host->bus_ops->awake)
1239                 err = host->bus_ops->awake(host);
1240
1241         mmc_bus_put(host);
1242
1243         return err;
1244 }
1245 EXPORT_SYMBOL(mmc_card_awake);
1246
1247 int mmc_card_sleep(struct mmc_host *host)
1248 {
1249         int err = -ENOSYS;
1250
1251         mmc_bus_get(host);
1252
1253         if (host->bus_ops && !host->bus_dead && host->bus_ops->awake)
1254                 err = host->bus_ops->sleep(host);
1255
1256         mmc_bus_put(host);
1257
1258         return err;
1259 }
1260 EXPORT_SYMBOL(mmc_card_sleep);
1261
1262 int mmc_card_can_sleep(struct mmc_host *host)
1263 {
1264         struct mmc_card *card = host->card;
1265
1266         if (card && mmc_card_mmc(card) && card->ext_csd.rev >= 3)
1267                 return 1;
1268         return 0;
1269 }
1270 EXPORT_SYMBOL(mmc_card_can_sleep);
1271
1272 #ifdef CONFIG_PM
1273
1274 /**
1275  *      mmc_suspend_host - suspend a host
1276  *      @host: mmc host
1277  *      @state: suspend mode (PM_SUSPEND_xxx)
1278  */
1279 int mmc_suspend_host(struct mmc_host *host, pm_message_t state)
1280 {
1281         int err = 0;
1282
1283         if (mmc_bus_needs_resume(host))
1284                 return 0;
1285
1286         if (host->caps & MMC_CAP_DISABLE)
1287                 cancel_delayed_work(&host->disable);
1288         cancel_delayed_work(&host->detect);
1289         mmc_flush_scheduled_work();
1290
1291         mmc_bus_get(host);
1292         if (host->bus_ops && !host->bus_dead) {
1293                 if (host->bus_ops->suspend)
1294                         err = host->bus_ops->suspend(host);
1295                 if (err == -ENOSYS || !host->bus_ops->resume) {
1296                         /*
1297                          * We simply "remove" the card in this case.
1298                          * It will be redetected on resume.
1299                          */
1300                         if (host->bus_ops->remove)
1301                                 host->bus_ops->remove(host);
1302                         mmc_claim_host(host);
1303                         mmc_detach_bus(host);
1304                         mmc_release_host(host);
1305                         err = 0;
1306                 }
1307         }
1308         mmc_bus_put(host);
1309
1310         if (!err)
1311                 mmc_power_off(host);
1312
1313         return err;
1314 }
1315
1316 EXPORT_SYMBOL(mmc_suspend_host);
1317
1318 /**
1319  *      mmc_resume_host - resume a previously suspended host
1320  *      @host: mmc host
1321  */
1322 int mmc_resume_host(struct mmc_host *host)
1323 {
1324         int err = 0;
1325
1326         mmc_bus_get(host);
1327         if (host->bus_resume_flags & MMC_BUSRESUME_MANUAL_RESUME) {
1328                 host->bus_resume_flags |= MMC_BUSRESUME_NEEDS_RESUME;
1329                 mmc_bus_put(host);
1330                 return 0;
1331         }
1332
1333         if (host->bus_ops && !host->bus_dead) {
1334                 mmc_power_up(host);
1335                 mmc_select_voltage(host, host->ocr);
1336                 BUG_ON(!host->bus_ops->resume);
1337                 err = host->bus_ops->resume(host);
1338                 if (err) {
1339                         printk(KERN_WARNING "%s: error %d during resume "
1340                                             "(card was removed?)\n",
1341                                             mmc_hostname(host), err);
1342                         if (host->bus_ops->remove)
1343                                 host->bus_ops->remove(host);
1344                         mmc_claim_host(host);
1345                         mmc_detach_bus(host);
1346                         mmc_release_host(host);
1347                         /* no need to bother upper layers */
1348                         err = 0;
1349                 }
1350         }
1351         mmc_bus_put(host);
1352
1353         /*
1354          * We add a slight delay here so that resume can progress
1355          * in parallel.
1356          */
1357         mmc_detect_change(host, 1);
1358
1359         return err;
1360 }
1361
1362 EXPORT_SYMBOL(mmc_resume_host);
1363
1364 #endif
1365
1366 #ifdef CONFIG_MMC_EMBEDDED_SDIO
1367 void mmc_set_embedded_sdio_data(struct mmc_host *host,
1368                                 struct sdio_cis *cis,
1369                                 struct sdio_cccr *cccr,
1370                                 struct sdio_embedded_func *funcs,
1371                                 int num_funcs)
1372 {
1373         host->embedded_sdio_data.cis = cis;
1374         host->embedded_sdio_data.cccr = cccr;
1375         host->embedded_sdio_data.funcs = funcs;
1376         host->embedded_sdio_data.num_funcs = num_funcs;
1377 }
1378
1379 EXPORT_SYMBOL(mmc_set_embedded_sdio_data);
1380 #endif
1381
1382 static int __init mmc_init(void)
1383 {
1384         int ret;
1385
1386         wake_lock_init(&mmc_delayed_work_wake_lock, WAKE_LOCK_SUSPEND, "mmc_delayed_work");
1387
1388         workqueue = create_singlethread_workqueue("kmmcd");
1389         if (!workqueue)
1390                 return -ENOMEM;
1391
1392         ret = mmc_register_bus();
1393         if (ret)
1394                 goto destroy_workqueue;
1395
1396         ret = mmc_register_host_class();
1397         if (ret)
1398                 goto unregister_bus;
1399
1400         ret = sdio_register_bus();
1401         if (ret)
1402                 goto unregister_host_class;
1403
1404         return 0;
1405
1406 unregister_host_class:
1407         mmc_unregister_host_class();
1408 unregister_bus:
1409         mmc_unregister_bus();
1410 destroy_workqueue:
1411         destroy_workqueue(workqueue);
1412
1413         return ret;
1414 }
1415
1416 static void __exit mmc_exit(void)
1417 {
1418         sdio_unregister_bus();
1419         mmc_unregister_host_class();
1420         mmc_unregister_bus();
1421         destroy_workqueue(workqueue);
1422         wake_lock_destroy(&mmc_delayed_work_wake_lock);
1423 }
1424
1425 subsys_initcall(mmc_init);
1426 module_exit(mmc_exit);
1427
1428 MODULE_LICENSE("GPL");