mmc: fix all hangs related to mmc/sd card insert/removal during suspend/resume
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / mmc / core / core.c
blobf3320f20f19fe0631a1c11001f4b268aac2a0093
1 /*
2 * linux/drivers/mmc/core/core.c
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.
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.
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>
26 #include <linux/mmc/card.h>
27 #include <linux/mmc/host.h>
28 #include <linux/mmc/mmc.h>
29 #include <linux/mmc/sd.h>
31 #include "core.h"
32 #include "bus.h"
33 #include "host.h"
34 #include "sdio_bus.h"
36 #include "mmc_ops.h"
37 #include "sd_ops.h"
38 #include "sdio_ops.h"
40 static struct workqueue_struct *workqueue;
43 * Enabling software CRCs on the data blocks can be a significant (30%)
44 * performance cost, and for other reasons may not always be desired.
45 * So we allow it it to be disabled.
47 int use_spi_crc = 1;
48 module_param(use_spi_crc, bool, 0);
51 * Internal function. Schedule delayed work in the MMC work queue.
53 static int mmc_schedule_delayed_work(struct delayed_work *work,
54 unsigned long delay)
56 return queue_delayed_work(workqueue, work, delay);
60 * Internal function. Flush all scheduled work from the MMC work queue.
62 static void mmc_flush_scheduled_work(void)
64 flush_workqueue(workqueue);
67 /**
68 * mmc_request_done - finish processing an MMC request
69 * @host: MMC host which completed request
70 * @mrq: MMC request which request
72 * MMC drivers should call this function when they have completed
73 * their processing of a request.
75 void mmc_request_done(struct mmc_host *host, struct mmc_request *mrq)
77 struct mmc_command *cmd = mrq->cmd;
78 int err = cmd->error;
80 if (err && cmd->retries && mmc_host_is_spi(host)) {
81 if (cmd->resp[0] & R1_SPI_ILLEGAL_COMMAND)
82 cmd->retries = 0;
85 if (err && cmd->retries) {
86 pr_debug("%s: req failed (CMD%u): %d, retrying...\n",
87 mmc_hostname(host), cmd->opcode, err);
89 cmd->retries--;
90 cmd->error = 0;
91 host->ops->request(host, mrq);
92 } else {
93 led_trigger_event(host->led, LED_OFF);
95 pr_debug("%s: req done (CMD%u): %d: %08x %08x %08x %08x\n",
96 mmc_hostname(host), cmd->opcode, err,
97 cmd->resp[0], cmd->resp[1],
98 cmd->resp[2], cmd->resp[3]);
100 if (mrq->data) {
101 pr_debug("%s: %d bytes transferred: %d\n",
102 mmc_hostname(host),
103 mrq->data->bytes_xfered, mrq->data->error);
106 if (mrq->stop) {
107 pr_debug("%s: (CMD%u): %d: %08x %08x %08x %08x\n",
108 mmc_hostname(host), mrq->stop->opcode,
109 mrq->stop->error,
110 mrq->stop->resp[0], mrq->stop->resp[1],
111 mrq->stop->resp[2], mrq->stop->resp[3]);
114 if (mrq->done)
115 mrq->done(mrq);
119 EXPORT_SYMBOL(mmc_request_done);
121 static void
122 mmc_start_request(struct mmc_host *host, struct mmc_request *mrq)
124 #ifdef CONFIG_MMC_DEBUG
125 unsigned int i, sz;
126 struct scatterlist *sg;
127 #endif
129 pr_debug("%s: starting CMD%u arg %08x flags %08x\n",
130 mmc_hostname(host), mrq->cmd->opcode,
131 mrq->cmd->arg, mrq->cmd->flags);
133 if (mrq->data) {
134 pr_debug("%s: blksz %d blocks %d flags %08x "
135 "tsac %d ms nsac %d\n",
136 mmc_hostname(host), mrq->data->blksz,
137 mrq->data->blocks, mrq->data->flags,
138 mrq->data->timeout_ns / 1000000,
139 mrq->data->timeout_clks);
142 if (mrq->stop) {
143 pr_debug("%s: CMD%u arg %08x flags %08x\n",
144 mmc_hostname(host), mrq->stop->opcode,
145 mrq->stop->arg, mrq->stop->flags);
148 WARN_ON(!host->claimed);
150 led_trigger_event(host->led, LED_FULL);
152 mrq->cmd->error = 0;
153 mrq->cmd->mrq = mrq;
154 if (mrq->data) {
155 BUG_ON(mrq->data->blksz > host->max_blk_size);
156 BUG_ON(mrq->data->blocks > host->max_blk_count);
157 BUG_ON(mrq->data->blocks * mrq->data->blksz >
158 host->max_req_size);
160 #ifdef CONFIG_MMC_DEBUG
161 sz = 0;
162 for_each_sg(mrq->data->sg, sg, mrq->data->sg_len, i)
163 sz += sg->length;
164 BUG_ON(sz != mrq->data->blocks * mrq->data->blksz);
165 #endif
167 mrq->cmd->data = mrq->data;
168 mrq->data->error = 0;
169 mrq->data->mrq = mrq;
170 if (mrq->stop) {
171 mrq->data->stop = mrq->stop;
172 mrq->stop->error = 0;
173 mrq->stop->mrq = mrq;
176 host->ops->request(host, mrq);
179 static void mmc_wait_done(struct mmc_request *mrq)
181 complete(mrq->done_data);
185 * mmc_wait_for_req - start a request and wait for completion
186 * @host: MMC host to start command
187 * @mrq: MMC request to start
189 * Start a new MMC custom command request for a host, and wait
190 * for the command to complete. Does not attempt to parse the
191 * response.
193 void mmc_wait_for_req(struct mmc_host *host, struct mmc_request *mrq)
195 DECLARE_COMPLETION_ONSTACK(complete);
197 mrq->done_data = &complete;
198 mrq->done = mmc_wait_done;
200 mmc_start_request(host, mrq);
202 wait_for_completion(&complete);
205 EXPORT_SYMBOL(mmc_wait_for_req);
208 * mmc_wait_for_cmd - start a command and wait for completion
209 * @host: MMC host to start command
210 * @cmd: MMC command to start
211 * @retries: maximum number of retries
213 * Start a new MMC command for a host, and wait for the command
214 * to complete. Return any error that occurred while the command
215 * was executing. Do not attempt to parse the response.
217 int mmc_wait_for_cmd(struct mmc_host *host, struct mmc_command *cmd, int retries)
219 struct mmc_request mrq;
221 WARN_ON(!host->claimed);
223 memset(&mrq, 0, sizeof(struct mmc_request));
225 memset(cmd->resp, 0, sizeof(cmd->resp));
226 cmd->retries = retries;
228 mrq.cmd = cmd;
229 cmd->data = NULL;
231 mmc_wait_for_req(host, &mrq);
233 return cmd->error;
236 EXPORT_SYMBOL(mmc_wait_for_cmd);
239 * mmc_set_data_timeout - set the timeout for a data command
240 * @data: data phase for command
241 * @card: the MMC card associated with the data transfer
243 * Computes the data timeout parameters according to the
244 * correct algorithm given the card type.
246 void mmc_set_data_timeout(struct mmc_data *data, const struct mmc_card *card)
248 unsigned int mult;
251 * SDIO cards only define an upper 1 s limit on access.
253 if (mmc_card_sdio(card)) {
254 data->timeout_ns = 1000000000;
255 data->timeout_clks = 0;
256 return;
260 * SD cards use a 100 multiplier rather than 10
262 mult = mmc_card_sd(card) ? 100 : 10;
265 * Scale up the multiplier (and therefore the timeout) by
266 * the r2w factor for writes.
268 if (data->flags & MMC_DATA_WRITE)
269 mult <<= card->csd.r2w_factor;
271 data->timeout_ns = card->csd.tacc_ns * mult;
272 data->timeout_clks = card->csd.tacc_clks * mult;
275 * SD cards also have an upper limit on the timeout.
277 if (mmc_card_sd(card)) {
278 unsigned int timeout_us, limit_us;
280 timeout_us = data->timeout_ns / 1000;
281 timeout_us += data->timeout_clks * 1000 /
282 (card->host->ios.clock / 1000);
284 if (data->flags & MMC_DATA_WRITE)
286 * The limit is really 250 ms, but that is
287 * insufficient for some crappy cards.
289 limit_us = 300000;
290 else
291 limit_us = 100000;
294 * SDHC cards always use these fixed values.
296 if (timeout_us > limit_us || mmc_card_blockaddr(card)) {
297 data->timeout_ns = limit_us * 1000;
298 data->timeout_clks = 0;
302 * Some cards need very high timeouts if driven in SPI mode.
303 * The worst observed timeout was 900ms after writing a
304 * continuous stream of data until the internal logic
305 * overflowed.
307 if (mmc_host_is_spi(card->host)) {
308 if (data->flags & MMC_DATA_WRITE) {
309 if (data->timeout_ns < 1000000000)
310 data->timeout_ns = 1000000000; /* 1s */
311 } else {
312 if (data->timeout_ns < 100000000)
313 data->timeout_ns = 100000000; /* 100ms */
317 EXPORT_SYMBOL(mmc_set_data_timeout);
320 * mmc_align_data_size - pads a transfer size to a more optimal value
321 * @card: the MMC card associated with the data transfer
322 * @sz: original transfer size
324 * Pads the original data size with a number of extra bytes in
325 * order to avoid controller bugs and/or performance hits
326 * (e.g. some controllers revert to PIO for certain sizes).
328 * Returns the improved size, which might be unmodified.
330 * Note that this function is only relevant when issuing a
331 * single scatter gather entry.
333 unsigned int mmc_align_data_size(struct mmc_card *card, unsigned int sz)
336 * FIXME: We don't have a system for the controller to tell
337 * the core about its problems yet, so for now we just 32-bit
338 * align the size.
340 sz = ((sz + 3) / 4) * 4;
342 return sz;
344 EXPORT_SYMBOL(mmc_align_data_size);
347 * mmc_host_enable - enable a host.
348 * @host: mmc host to enable
350 * Hosts that support power saving can use the 'enable' and 'disable'
351 * methods to exit and enter power saving states. For more information
352 * see comments for struct mmc_host_ops.
354 int mmc_host_enable(struct mmc_host *host)
356 if (!(host->caps & MMC_CAP_DISABLE))
357 return 0;
359 if (host->en_dis_recurs)
360 return 0;
362 if (host->nesting_cnt++)
363 return 0;
365 cancel_delayed_work_sync(&host->disable);
367 if (host->enabled)
368 return 0;
370 if (host->ops->enable) {
371 int err;
373 host->en_dis_recurs = 1;
374 err = host->ops->enable(host);
375 host->en_dis_recurs = 0;
377 if (err) {
378 pr_debug("%s: enable error %d\n",
379 mmc_hostname(host), err);
380 return err;
383 host->enabled = 1;
384 return 0;
386 EXPORT_SYMBOL(mmc_host_enable);
388 static int mmc_host_do_disable(struct mmc_host *host, int lazy)
390 if (host->ops->disable) {
391 int err;
393 host->en_dis_recurs = 1;
394 err = host->ops->disable(host, lazy);
395 host->en_dis_recurs = 0;
397 if (err < 0) {
398 pr_debug("%s: disable error %d\n",
399 mmc_hostname(host), err);
400 return err;
402 if (err > 0) {
403 unsigned long delay = msecs_to_jiffies(err);
405 mmc_schedule_delayed_work(&host->disable, delay);
408 host->enabled = 0;
409 return 0;
413 * mmc_host_disable - disable a host.
414 * @host: mmc host to disable
416 * Hosts that support power saving can use the 'enable' and 'disable'
417 * methods to exit and enter power saving states. For more information
418 * see comments for struct mmc_host_ops.
420 int mmc_host_disable(struct mmc_host *host)
422 int err;
424 if (!(host->caps & MMC_CAP_DISABLE))
425 return 0;
427 if (host->en_dis_recurs)
428 return 0;
430 if (--host->nesting_cnt)
431 return 0;
433 if (!host->enabled)
434 return 0;
436 err = mmc_host_do_disable(host, 0);
437 return err;
439 EXPORT_SYMBOL(mmc_host_disable);
442 * __mmc_claim_host - exclusively claim a host
443 * @host: mmc host to claim
444 * @abort: whether or not the operation should be aborted
446 * Claim a host for a set of operations. If @abort is non null and
447 * dereference a non-zero value then this will return prematurely with
448 * that non-zero value without acquiring the lock. Returns zero
449 * with the lock held otherwise.
451 int __mmc_claim_host(struct mmc_host *host, atomic_t *abort)
453 DECLARE_WAITQUEUE(wait, current);
454 unsigned long flags;
455 int stop;
457 might_sleep();
459 add_wait_queue(&host->wq, &wait);
460 spin_lock_irqsave(&host->lock, flags);
461 while (1) {
462 set_current_state(TASK_UNINTERRUPTIBLE);
463 stop = abort ? atomic_read(abort) : 0;
464 if (stop || !host->claimed || host->claimer == current)
465 break;
466 spin_unlock_irqrestore(&host->lock, flags);
467 schedule();
468 spin_lock_irqsave(&host->lock, flags);
470 set_current_state(TASK_RUNNING);
471 if (!stop) {
472 host->claimed = 1;
473 host->claimer = current;
474 host->claim_cnt += 1;
475 } else
476 wake_up(&host->wq);
477 spin_unlock_irqrestore(&host->lock, flags);
478 remove_wait_queue(&host->wq, &wait);
479 if (!stop)
480 mmc_host_enable(host);
481 return stop;
484 EXPORT_SYMBOL(__mmc_claim_host);
487 * mmc_try_claim_host - try exclusively to claim a host
488 * @host: mmc host to claim
490 * Returns %1 if the host is claimed, %0 otherwise.
492 int mmc_try_claim_host(struct mmc_host *host)
494 int claimed_host = 0;
495 unsigned long flags;
497 spin_lock_irqsave(&host->lock, flags);
498 if (!host->claimed || host->claimer == current) {
499 host->claimed = 1;
500 host->claimer = current;
501 host->claim_cnt += 1;
502 claimed_host = 1;
504 spin_unlock_irqrestore(&host->lock, flags);
505 return claimed_host;
507 EXPORT_SYMBOL(mmc_try_claim_host);
509 static void mmc_do_release_host(struct mmc_host *host)
511 unsigned long flags;
513 spin_lock_irqsave(&host->lock, flags);
514 if (--host->claim_cnt) {
515 /* Release for nested claim */
516 spin_unlock_irqrestore(&host->lock, flags);
517 } else {
518 host->claimed = 0;
519 host->claimer = NULL;
520 spin_unlock_irqrestore(&host->lock, flags);
521 wake_up(&host->wq);
525 void mmc_host_deeper_disable(struct work_struct *work)
527 struct mmc_host *host =
528 container_of(work, struct mmc_host, disable.work);
530 /* If the host is claimed then we do not want to disable it anymore */
531 if (!mmc_try_claim_host(host))
532 return;
533 mmc_host_do_disable(host, 1);
534 mmc_do_release_host(host);
538 * mmc_host_lazy_disable - lazily disable a host.
539 * @host: mmc host to disable
541 * Hosts that support power saving can use the 'enable' and 'disable'
542 * methods to exit and enter power saving states. For more information
543 * see comments for struct mmc_host_ops.
545 int mmc_host_lazy_disable(struct mmc_host *host)
547 if (!(host->caps & MMC_CAP_DISABLE))
548 return 0;
550 if (host->en_dis_recurs)
551 return 0;
553 if (--host->nesting_cnt)
554 return 0;
556 if (!host->enabled)
557 return 0;
559 if (host->disable_delay) {
560 mmc_schedule_delayed_work(&host->disable,
561 msecs_to_jiffies(host->disable_delay));
562 return 0;
563 } else
564 return mmc_host_do_disable(host, 1);
566 EXPORT_SYMBOL(mmc_host_lazy_disable);
569 * mmc_release_host - release a host
570 * @host: mmc host to release
572 * Release a MMC host, allowing others to claim the host
573 * for their operations.
575 void mmc_release_host(struct mmc_host *host)
577 WARN_ON(!host->claimed);
579 mmc_host_lazy_disable(host);
581 mmc_do_release_host(host);
584 EXPORT_SYMBOL(mmc_release_host);
587 * Internal function that does the actual ios call to the host driver,
588 * optionally printing some debug output.
590 static inline void mmc_set_ios(struct mmc_host *host)
592 struct mmc_ios *ios = &host->ios;
594 pr_debug("%s: clock %uHz busmode %u powermode %u cs %u Vdd %u "
595 "width %u timing %u\n",
596 mmc_hostname(host), ios->clock, ios->bus_mode,
597 ios->power_mode, ios->chip_select, ios->vdd,
598 ios->bus_width, ios->timing);
600 host->ops->set_ios(host, ios);
604 * Control chip select pin on a host.
606 void mmc_set_chip_select(struct mmc_host *host, int mode)
608 host->ios.chip_select = mode;
609 mmc_set_ios(host);
613 * Sets the host clock to the highest possible frequency that
614 * is below "hz".
616 void mmc_set_clock(struct mmc_host *host, unsigned int hz)
618 WARN_ON(hz < host->f_min);
620 if (hz > host->f_max)
621 hz = host->f_max;
623 host->ios.clock = hz;
624 mmc_set_ios(host);
628 * Change the bus mode (open drain/push-pull) of a host.
630 void mmc_set_bus_mode(struct mmc_host *host, unsigned int mode)
632 host->ios.bus_mode = mode;
633 mmc_set_ios(host);
637 * Change data bus width of a host.
639 void mmc_set_bus_width(struct mmc_host *host, unsigned int width)
641 host->ios.bus_width = width;
642 mmc_set_ios(host);
646 * mmc_vdd_to_ocrbitnum - Convert a voltage to the OCR bit number
647 * @vdd: voltage (mV)
648 * @low_bits: prefer low bits in boundary cases
650 * This function returns the OCR bit number according to the provided @vdd
651 * value. If conversion is not possible a negative errno value returned.
653 * Depending on the @low_bits flag the function prefers low or high OCR bits
654 * on boundary voltages. For example,
655 * with @low_bits = true, 3300 mV translates to ilog2(MMC_VDD_32_33);
656 * with @low_bits = false, 3300 mV translates to ilog2(MMC_VDD_33_34);
658 * Any value in the [1951:1999] range translates to the ilog2(MMC_VDD_20_21).
660 static int mmc_vdd_to_ocrbitnum(int vdd, bool low_bits)
662 const int max_bit = ilog2(MMC_VDD_35_36);
663 int bit;
665 if (vdd < 1650 || vdd > 3600)
666 return -EINVAL;
668 if (vdd >= 1650 && vdd <= 1950)
669 return ilog2(MMC_VDD_165_195);
671 if (low_bits)
672 vdd -= 1;
674 /* Base 2000 mV, step 100 mV, bit's base 8. */
675 bit = (vdd - 2000) / 100 + 8;
676 if (bit > max_bit)
677 return max_bit;
678 return bit;
682 * mmc_vddrange_to_ocrmask - Convert a voltage range to the OCR mask
683 * @vdd_min: minimum voltage value (mV)
684 * @vdd_max: maximum voltage value (mV)
686 * This function returns the OCR mask bits according to the provided @vdd_min
687 * and @vdd_max values. If conversion is not possible the function returns 0.
689 * Notes wrt boundary cases:
690 * This function sets the OCR bits for all boundary voltages, for example
691 * [3300:3400] range is translated to MMC_VDD_32_33 | MMC_VDD_33_34 |
692 * MMC_VDD_34_35 mask.
694 u32 mmc_vddrange_to_ocrmask(int vdd_min, int vdd_max)
696 u32 mask = 0;
698 if (vdd_max < vdd_min)
699 return 0;
701 /* Prefer high bits for the boundary vdd_max values. */
702 vdd_max = mmc_vdd_to_ocrbitnum(vdd_max, false);
703 if (vdd_max < 0)
704 return 0;
706 /* Prefer low bits for the boundary vdd_min values. */
707 vdd_min = mmc_vdd_to_ocrbitnum(vdd_min, true);
708 if (vdd_min < 0)
709 return 0;
711 /* Fill the mask, from max bit to min bit. */
712 while (vdd_max >= vdd_min)
713 mask |= 1 << vdd_max--;
715 return mask;
717 EXPORT_SYMBOL(mmc_vddrange_to_ocrmask);
719 #ifdef CONFIG_REGULATOR
722 * mmc_regulator_get_ocrmask - return mask of supported voltages
723 * @supply: regulator to use
725 * This returns either a negative errno, or a mask of voltages that
726 * can be provided to MMC/SD/SDIO devices using the specified voltage
727 * regulator. This would normally be called before registering the
728 * MMC host adapter.
730 int mmc_regulator_get_ocrmask(struct regulator *supply)
732 int result = 0;
733 int count;
734 int i;
736 count = regulator_count_voltages(supply);
737 if (count < 0)
738 return count;
740 for (i = 0; i < count; i++) {
741 int vdd_uV;
742 int vdd_mV;
744 vdd_uV = regulator_list_voltage(supply, i);
745 if (vdd_uV <= 0)
746 continue;
748 vdd_mV = vdd_uV / 1000;
749 result |= mmc_vddrange_to_ocrmask(vdd_mV, vdd_mV);
752 return result;
754 EXPORT_SYMBOL(mmc_regulator_get_ocrmask);
757 * mmc_regulator_set_ocr - set regulator to match host->ios voltage
758 * @vdd_bit: zero for power off, else a bit number (host->ios.vdd)
759 * @supply: regulator to use
761 * Returns zero on success, else negative errno.
763 * MMC host drivers may use this to enable or disable a regulator using
764 * a particular supply voltage. This would normally be called from the
765 * set_ios() method.
767 int mmc_regulator_set_ocr(struct regulator *supply, unsigned short vdd_bit)
769 int result = 0;
770 int min_uV, max_uV;
771 int enabled;
773 enabled = regulator_is_enabled(supply);
774 if (enabled < 0)
775 return enabled;
777 if (vdd_bit) {
778 int tmp;
779 int voltage;
781 /* REVISIT mmc_vddrange_to_ocrmask() may have set some
782 * bits this regulator doesn't quite support ... don't
783 * be too picky, most cards and regulators are OK with
784 * a 0.1V range goof (it's a small error percentage).
786 tmp = vdd_bit - ilog2(MMC_VDD_165_195);
787 if (tmp == 0) {
788 min_uV = 1650 * 1000;
789 max_uV = 1950 * 1000;
790 } else {
791 min_uV = 1900 * 1000 + tmp * 100 * 1000;
792 max_uV = min_uV + 100 * 1000;
795 /* avoid needless changes to this voltage; the regulator
796 * might not allow this operation
798 voltage = regulator_get_voltage(supply);
799 if (voltage < 0)
800 result = voltage;
801 else if (voltage < min_uV || voltage > max_uV)
802 result = regulator_set_voltage(supply, min_uV, max_uV);
803 else
804 result = 0;
806 if (result == 0 && !enabled)
807 result = regulator_enable(supply);
808 } else if (enabled) {
809 result = regulator_disable(supply);
812 return result;
814 EXPORT_SYMBOL(mmc_regulator_set_ocr);
816 #endif
819 * Mask off any voltages we don't support and select
820 * the lowest voltage
822 u32 mmc_select_voltage(struct mmc_host *host, u32 ocr)
824 int bit;
826 ocr &= host->ocr_avail;
828 bit = ffs(ocr);
829 if (bit) {
830 bit -= 1;
832 ocr &= 3 << bit;
834 host->ios.vdd = bit;
835 mmc_set_ios(host);
836 } else {
837 pr_warning("%s: host doesn't support card's voltages\n",
838 mmc_hostname(host));
839 ocr = 0;
842 return ocr;
846 * Select timing parameters for host.
848 void mmc_set_timing(struct mmc_host *host, unsigned int timing)
850 host->ios.timing = timing;
851 mmc_set_ios(host);
855 * Apply power to the MMC stack. This is a two-stage process.
856 * First, we enable power to the card without the clock running.
857 * We then wait a bit for the power to stabilise. Finally,
858 * enable the bus drivers and clock to the card.
860 * We must _NOT_ enable the clock prior to power stablising.
862 * If a host does all the power sequencing itself, ignore the
863 * initial MMC_POWER_UP stage.
865 static void mmc_power_up(struct mmc_host *host)
867 int bit;
869 /* If ocr is set, we use it */
870 if (host->ocr)
871 bit = ffs(host->ocr) - 1;
872 else
873 bit = fls(host->ocr_avail) - 1;
875 host->ios.vdd = bit;
876 if (mmc_host_is_spi(host)) {
877 host->ios.chip_select = MMC_CS_HIGH;
878 host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
879 } else {
880 host->ios.chip_select = MMC_CS_DONTCARE;
881 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
883 host->ios.power_mode = MMC_POWER_UP;
884 host->ios.bus_width = MMC_BUS_WIDTH_1;
885 host->ios.timing = MMC_TIMING_LEGACY;
886 mmc_set_ios(host);
889 * This delay should be sufficient to allow the power supply
890 * to reach the minimum voltage.
892 mmc_delay(10);
894 if (host->f_min > 400000) {
895 pr_warning("%s: Minimum clock frequency too high for "
896 "identification mode\n", mmc_hostname(host));
897 host->ios.clock = host->f_min;
898 } else
899 host->ios.clock = 400000;
901 host->ios.power_mode = MMC_POWER_ON;
902 mmc_set_ios(host);
905 * This delay must be at least 74 clock sizes, or 1 ms, or the
906 * time required to reach a stable voltage.
908 mmc_delay(10);
911 static void mmc_power_off(struct mmc_host *host)
913 host->ios.clock = 0;
914 host->ios.vdd = 0;
915 if (!mmc_host_is_spi(host)) {
916 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
917 host->ios.chip_select = MMC_CS_DONTCARE;
919 host->ios.power_mode = MMC_POWER_OFF;
920 host->ios.bus_width = MMC_BUS_WIDTH_1;
921 host->ios.timing = MMC_TIMING_LEGACY;
922 mmc_set_ios(host);
926 * Cleanup when the last reference to the bus operator is dropped.
928 static void __mmc_release_bus(struct mmc_host *host)
930 BUG_ON(!host);
931 BUG_ON(host->bus_refs);
932 BUG_ON(!host->bus_dead);
934 host->bus_ops = NULL;
938 * Increase reference count of bus operator
940 static inline void mmc_bus_get(struct mmc_host *host)
942 unsigned long flags;
944 spin_lock_irqsave(&host->lock, flags);
945 host->bus_refs++;
946 spin_unlock_irqrestore(&host->lock, flags);
950 * Decrease reference count of bus operator and free it if
951 * it is the last reference.
953 static inline void mmc_bus_put(struct mmc_host *host)
955 unsigned long flags;
957 spin_lock_irqsave(&host->lock, flags);
958 host->bus_refs--;
959 if ((host->bus_refs == 0) && host->bus_ops)
960 __mmc_release_bus(host);
961 spin_unlock_irqrestore(&host->lock, flags);
965 * Assign a mmc bus handler to a host. Only one bus handler may control a
966 * host at any given time.
968 void mmc_attach_bus(struct mmc_host *host, const struct mmc_bus_ops *ops)
970 unsigned long flags;
972 BUG_ON(!host);
973 BUG_ON(!ops);
975 WARN_ON(!host->claimed);
977 spin_lock_irqsave(&host->lock, flags);
979 BUG_ON(host->bus_ops);
980 BUG_ON(host->bus_refs);
982 host->bus_ops = ops;
983 host->bus_refs = 1;
984 host->bus_dead = 0;
986 spin_unlock_irqrestore(&host->lock, flags);
990 * Remove the current bus handler from a host. Assumes that there are
991 * no interesting cards left, so the bus is powered down.
993 void mmc_detach_bus(struct mmc_host *host)
995 unsigned long flags;
997 BUG_ON(!host);
999 WARN_ON(!host->claimed);
1000 WARN_ON(!host->bus_ops);
1002 spin_lock_irqsave(&host->lock, flags);
1004 host->bus_dead = 1;
1006 spin_unlock_irqrestore(&host->lock, flags);
1008 mmc_power_off(host);
1010 mmc_bus_put(host);
1014 * mmc_detect_change - process change of state on a MMC socket
1015 * @host: host which changed state.
1016 * @delay: optional delay to wait before detection (jiffies)
1018 * MMC drivers should call this when they detect a card has been
1019 * inserted or removed. The MMC layer will confirm that any
1020 * present card is still functional, and initialize any newly
1021 * inserted.
1023 void mmc_detect_change(struct mmc_host *host, unsigned long delay)
1025 #ifdef CONFIG_MMC_DEBUG
1026 unsigned long flags;
1027 spin_lock_irqsave(&host->lock, flags);
1028 WARN_ON(host->removed);
1029 spin_unlock_irqrestore(&host->lock, flags);
1030 #endif
1032 mmc_schedule_delayed_work(&host->detect, delay);
1035 EXPORT_SYMBOL(mmc_detect_change);
1038 void mmc_rescan(struct work_struct *work)
1040 struct mmc_host *host =
1041 container_of(work, struct mmc_host, detect.work);
1042 u32 ocr;
1043 int err;
1044 unsigned long flags;
1046 spin_lock_irqsave(&host->lock, flags);
1048 if (host->rescan_disable) {
1049 spin_unlock_irqrestore(&host->lock, flags);
1050 return;
1053 spin_unlock_irqrestore(&host->lock, flags);
1056 mmc_bus_get(host);
1058 /* if there is a card registered, check whether it is still present */
1059 if ((host->bus_ops != NULL) && host->bus_ops->detect && !host->bus_dead)
1060 host->bus_ops->detect(host);
1062 mmc_bus_put(host);
1065 mmc_bus_get(host);
1067 /* if there still is a card present, stop here */
1068 if (host->bus_ops != NULL) {
1069 mmc_bus_put(host);
1070 goto out;
1073 /* detect a newly inserted card */
1076 * Only we can add a new handler, so it's safe to
1077 * release the lock here.
1079 mmc_bus_put(host);
1081 if (host->ops->get_cd && host->ops->get_cd(host) == 0)
1082 goto out;
1084 mmc_claim_host(host);
1086 mmc_power_up(host);
1087 mmc_go_idle(host);
1089 mmc_send_if_cond(host, host->ocr_avail);
1092 * First we search for SDIO...
1094 err = mmc_send_io_op_cond(host, 0, &ocr);
1095 if (!err) {
1096 if (mmc_attach_sdio(host, ocr))
1097 mmc_power_off(host);
1098 goto out;
1102 * ...then normal SD...
1104 err = mmc_send_app_op_cond(host, 0, &ocr);
1105 if (!err) {
1106 if (mmc_attach_sd(host, ocr))
1107 mmc_power_off(host);
1108 goto out;
1112 * ...and finally MMC.
1114 err = mmc_send_op_cond(host, 0, &ocr);
1115 if (!err) {
1116 if (mmc_attach_mmc(host, ocr))
1117 mmc_power_off(host);
1118 goto out;
1121 mmc_release_host(host);
1122 mmc_power_off(host);
1124 out:
1125 if (host->caps & MMC_CAP_NEEDS_POLL)
1126 mmc_schedule_delayed_work(&host->detect, HZ);
1129 void mmc_start_host(struct mmc_host *host)
1131 mmc_power_off(host);
1132 mmc_detect_change(host, 0);
1135 void mmc_stop_host(struct mmc_host *host)
1137 #ifdef CONFIG_MMC_DEBUG
1138 unsigned long flags;
1139 spin_lock_irqsave(&host->lock, flags);
1140 host->removed = 1;
1141 spin_unlock_irqrestore(&host->lock, flags);
1142 #endif
1144 if (host->caps & MMC_CAP_DISABLE)
1145 cancel_delayed_work(&host->disable);
1146 cancel_delayed_work(&host->detect);
1147 mmc_flush_scheduled_work();
1149 mmc_bus_get(host);
1150 if (host->bus_ops && !host->bus_dead) {
1151 if (host->bus_ops->remove)
1152 host->bus_ops->remove(host);
1154 mmc_claim_host(host);
1155 mmc_detach_bus(host);
1156 mmc_release_host(host);
1157 mmc_bus_put(host);
1158 return;
1160 mmc_bus_put(host);
1162 BUG_ON(host->card);
1164 mmc_power_off(host);
1167 void mmc_power_save_host(struct mmc_host *host)
1169 mmc_bus_get(host);
1171 if (!host->bus_ops || host->bus_dead || !host->bus_ops->power_restore) {
1172 mmc_bus_put(host);
1173 return;
1176 if (host->bus_ops->power_save)
1177 host->bus_ops->power_save(host);
1179 mmc_bus_put(host);
1181 mmc_power_off(host);
1183 EXPORT_SYMBOL(mmc_power_save_host);
1185 void mmc_power_restore_host(struct mmc_host *host)
1187 mmc_bus_get(host);
1189 if (!host->bus_ops || host->bus_dead || !host->bus_ops->power_restore) {
1190 mmc_bus_put(host);
1191 return;
1194 mmc_power_up(host);
1195 host->bus_ops->power_restore(host);
1197 mmc_bus_put(host);
1199 EXPORT_SYMBOL(mmc_power_restore_host);
1201 int mmc_card_awake(struct mmc_host *host)
1203 int err = -ENOSYS;
1205 mmc_bus_get(host);
1207 if (host->bus_ops && !host->bus_dead && host->bus_ops->awake)
1208 err = host->bus_ops->awake(host);
1210 mmc_bus_put(host);
1212 return err;
1214 EXPORT_SYMBOL(mmc_card_awake);
1216 int mmc_card_sleep(struct mmc_host *host)
1218 int err = -ENOSYS;
1220 mmc_bus_get(host);
1222 if (host->bus_ops && !host->bus_dead && host->bus_ops->awake)
1223 err = host->bus_ops->sleep(host);
1225 mmc_bus_put(host);
1227 return err;
1229 EXPORT_SYMBOL(mmc_card_sleep);
1231 int mmc_card_can_sleep(struct mmc_host *host)
1233 struct mmc_card *card = host->card;
1235 if (card && mmc_card_mmc(card) && card->ext_csd.rev >= 3)
1236 return 1;
1237 return 0;
1239 EXPORT_SYMBOL(mmc_card_can_sleep);
1241 #ifdef CONFIG_PM
1244 * mmc_suspend_host - suspend a host
1245 * @host: mmc host
1246 * @state: suspend mode (PM_SUSPEND_xxx)
1248 int mmc_suspend_host(struct mmc_host *host, pm_message_t state)
1250 int err = 0;
1252 if (host->caps & MMC_CAP_DISABLE)
1253 cancel_delayed_work(&host->disable);
1254 cancel_delayed_work(&host->detect);
1255 mmc_flush_scheduled_work();
1257 mmc_bus_get(host);
1258 if (host->bus_ops && !host->bus_dead) {
1259 if (host->bus_ops->suspend)
1260 err = host->bus_ops->suspend(host);
1262 mmc_bus_put(host);
1264 if (!err)
1265 mmc_power_off(host);
1267 return err;
1270 EXPORT_SYMBOL(mmc_suspend_host);
1273 * mmc_resume_host - resume a previously suspended host
1274 * @host: mmc host
1276 int mmc_resume_host(struct mmc_host *host)
1278 int err = 0;
1280 mmc_bus_get(host);
1281 if (host->bus_ops && !host->bus_dead) {
1282 mmc_power_up(host);
1283 mmc_select_voltage(host, host->ocr);
1284 BUG_ON(!host->bus_ops->resume);
1285 err = host->bus_ops->resume(host);
1286 if (err) {
1287 printk(KERN_WARNING "%s: error %d during resume "
1288 "(card was removed?)\n",
1289 mmc_hostname(host), err);
1290 err = 0;
1293 mmc_bus_put(host);
1295 return err;
1297 EXPORT_SYMBOL(mmc_resume_host);
1299 /* Do the card removal on suspend if card is assumed removeable
1300 * Do that in pm notifier while userspace isn't yet frozen, so we will be able
1301 to sync the card.
1303 int mmc_pm_notify(struct notifier_block *notify_block,
1304 unsigned long mode, void *unused)
1306 struct mmc_host *host = container_of(
1307 notify_block, struct mmc_host, pm_notify);
1308 unsigned long flags;
1311 switch (mode) {
1312 case PM_HIBERNATION_PREPARE:
1313 case PM_SUSPEND_PREPARE:
1315 spin_lock_irqsave(&host->lock, flags);
1316 host->rescan_disable = 1;
1317 spin_unlock_irqrestore(&host->lock, flags);
1318 cancel_delayed_work_sync(&host->detect);
1320 if (!host->bus_ops || host->bus_ops->suspend)
1321 break;
1323 mmc_claim_host(host);
1325 if (host->bus_ops->remove)
1326 host->bus_ops->remove(host);
1328 mmc_detach_bus(host);
1329 mmc_release_host(host);
1330 break;
1332 case PM_POST_SUSPEND:
1333 case PM_POST_HIBERNATION:
1335 spin_lock_irqsave(&host->lock, flags);
1336 host->rescan_disable = 0;
1337 spin_unlock_irqrestore(&host->lock, flags);
1338 mmc_detect_change(host, 0);
1342 return 0;
1344 #endif
1346 static int __init mmc_init(void)
1348 int ret;
1350 workqueue = create_singlethread_workqueue("kmmcd");
1351 if (!workqueue)
1352 return -ENOMEM;
1354 ret = mmc_register_bus();
1355 if (ret)
1356 goto destroy_workqueue;
1358 ret = mmc_register_host_class();
1359 if (ret)
1360 goto unregister_bus;
1362 ret = sdio_register_bus();
1363 if (ret)
1364 goto unregister_host_class;
1366 return 0;
1368 unregister_host_class:
1369 mmc_unregister_host_class();
1370 unregister_bus:
1371 mmc_unregister_bus();
1372 destroy_workqueue:
1373 destroy_workqueue(workqueue);
1375 return ret;
1378 static void __exit mmc_exit(void)
1380 sdio_unregister_bus();
1381 mmc_unregister_host_class();
1382 mmc_unregister_bus();
1383 destroy_workqueue(workqueue);
1386 subsys_initcall(mmc_init);
1387 module_exit(mmc_exit);
1389 MODULE_LICENSE("GPL");