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>
25 #include <linux/wakelock.h>
27 #include <linux/mmc/card.h>
28 #include <linux/mmc/host.h>
29 #include <linux/mmc/mmc.h>
30 #include <linux/mmc/sd.h>
41 static struct workqueue_struct *workqueue;
42 static struct wake_lock mmc_delayed_work_wake_lock;
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.
50 module_param(use_spi_crc, bool, 0);
53 * Internal function. Schedule delayed work in the MMC work queue.
55 static int mmc_schedule_delayed_work(struct delayed_work *work,
58 wake_lock(&mmc_delayed_work_wake_lock);
59 return queue_delayed_work(workqueue, work, delay);
63 * Internal function. Flush all scheduled work from the MMC work queue.
65 static void mmc_flush_scheduled_work(void)
67 flush_workqueue(workqueue);
71 * mmc_request_done - finish processing an MMC request
72 * @host: MMC host which completed request
73 * @mrq: MMC request which request
75 * MMC drivers should call this function when they have completed
76 * their processing of a request.
78 void mmc_request_done(struct mmc_host *host, struct mmc_request *mrq)
80 struct mmc_command *cmd = mrq->cmd;
83 if (err && cmd->retries && mmc_host_is_spi(host)) {
84 if (cmd->resp[0] & R1_SPI_ILLEGAL_COMMAND)
88 if (err && cmd->retries) {
89 pr_debug("%s: req failed (CMD%u): %d, retrying...\n",
90 mmc_hostname(host), cmd->opcode, err);
94 host->ops->request(host, mrq);
96 led_trigger_event(host->led, LED_OFF);
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]);
104 pr_debug("%s: %d bytes transferred: %d\n",
106 mrq->data->bytes_xfered, mrq->data->error);
110 pr_debug("%s: (CMD%u): %d: %08x %08x %08x %08x\n",
111 mmc_hostname(host), mrq->stop->opcode,
113 mrq->stop->resp[0], mrq->stop->resp[1],
114 mrq->stop->resp[2], mrq->stop->resp[3]);
122 EXPORT_SYMBOL(mmc_request_done);
125 mmc_start_request(struct mmc_host *host, struct mmc_request *mrq)
127 #ifdef CONFIG_MMC_DEBUG
129 struct scatterlist *sg;
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);
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);
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);
151 WARN_ON(!host->claimed);
153 led_trigger_event(host->led, LED_FULL);
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 >
163 #ifdef CONFIG_MMC_DEBUG
165 for_each_sg(mrq->data->sg, sg, mrq->data->sg_len, i)
167 BUG_ON(sz != mrq->data->blocks * mrq->data->blksz);
170 mrq->cmd->data = mrq->data;
171 mrq->data->error = 0;
172 mrq->data->mrq = mrq;
174 mrq->data->stop = mrq->stop;
175 mrq->stop->error = 0;
176 mrq->stop->mrq = mrq;
179 host->ops->request(host, mrq);
182 static void mmc_wait_done(struct mmc_request *mrq)
184 complete(mrq->done_data);
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
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
196 void mmc_wait_for_req(struct mmc_host *host, struct mmc_request *mrq)
198 DECLARE_COMPLETION_ONSTACK(complete);
200 mrq->done_data = &complete;
201 mrq->done = mmc_wait_done;
203 mmc_start_request(host, mrq);
205 wait_for_completion(&complete);
208 EXPORT_SYMBOL(mmc_wait_for_req);
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
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.
220 int mmc_wait_for_cmd(struct mmc_host *host, struct mmc_command *cmd, int retries)
222 struct mmc_request mrq;
224 WARN_ON(!host->claimed);
226 memset(&mrq, 0, sizeof(struct mmc_request));
228 memset(cmd->resp, 0, sizeof(cmd->resp));
229 cmd->retries = retries;
234 mmc_wait_for_req(host, &mrq);
239 EXPORT_SYMBOL(mmc_wait_for_cmd);
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
246 * Computes the data timeout parameters according to the
247 * correct algorithm given the card type.
249 void mmc_set_data_timeout(struct mmc_data *data, const struct mmc_card *card)
254 * SDIO cards only define an upper 1 s limit on access.
256 if (mmc_card_sdio(card)) {
257 data->timeout_ns = 1000000000;
258 data->timeout_clks = 0;
263 * SD cards use a 100 multiplier rather than 10
265 mult = mmc_card_sd(card) ? 100 : 10;
268 * Scale up the multiplier (and therefore the timeout) by
269 * the r2w factor for writes.
271 if (data->flags & MMC_DATA_WRITE)
272 mult <<= card->csd.r2w_factor;
274 data->timeout_ns = card->csd.tacc_ns * mult;
275 data->timeout_clks = card->csd.tacc_clks * mult;
278 * SD cards also have an upper limit on the timeout.
280 if (mmc_card_sd(card)) {
281 unsigned int timeout_us, limit_us;
283 timeout_us = data->timeout_ns / 1000;
284 timeout_us += data->timeout_clks * 1000 /
285 (card->host->ios.clock / 1000);
287 if (data->flags & MMC_DATA_WRITE)
289 * The limit is really 250 ms, but that is
290 * insufficient for some crappy cards.
297 * SDHC cards always use these fixed values.
299 if (timeout_us > limit_us || mmc_card_blockaddr(card)) {
300 data->timeout_ns = limit_us * 1000;
301 data->timeout_clks = 0;
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
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 */
315 if (data->timeout_ns < 100000000)
316 data->timeout_ns = 100000000; /* 100ms */
320 EXPORT_SYMBOL(mmc_set_data_timeout);
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
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).
331 * Returns the improved size, which might be unmodified.
333 * Note that this function is only relevant when issuing a
334 * single scatter gather entry.
336 unsigned int mmc_align_data_size(struct mmc_card *card, unsigned int sz)
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
343 sz = ((sz + 3) / 4) * 4;
347 EXPORT_SYMBOL(mmc_align_data_size);
350 * mmc_host_enable - enable a host.
351 * @host: mmc host to enable
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.
357 int mmc_host_enable(struct mmc_host *host)
359 if (!(host->caps & MMC_CAP_DISABLE))
362 if (host->en_dis_recurs)
365 if (host->nesting_cnt++)
368 cancel_delayed_work_sync(&host->disable);
373 if (host->ops->enable) {
376 host->en_dis_recurs = 1;
377 err = host->ops->enable(host);
378 host->en_dis_recurs = 0;
381 pr_debug("%s: enable error %d\n",
382 mmc_hostname(host), err);
389 EXPORT_SYMBOL(mmc_host_enable);
391 static int mmc_host_do_disable(struct mmc_host *host, int lazy)
393 if (host->ops->disable) {
396 host->en_dis_recurs = 1;
397 err = host->ops->disable(host, lazy);
398 host->en_dis_recurs = 0;
401 pr_debug("%s: disable error %d\n",
402 mmc_hostname(host), err);
406 unsigned long delay = msecs_to_jiffies(err);
408 mmc_schedule_delayed_work(&host->disable, delay);
416 * mmc_host_disable - disable a host.
417 * @host: mmc host to disable
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.
423 int mmc_host_disable(struct mmc_host *host)
427 if (!(host->caps & MMC_CAP_DISABLE))
430 if (host->en_dis_recurs)
433 if (--host->nesting_cnt)
439 err = mmc_host_do_disable(host, 0);
442 EXPORT_SYMBOL(mmc_host_disable);
445 * __mmc_claim_host - exclusively claim a host
446 * @host: mmc host to claim
447 * @abort: whether or not the operation should be aborted
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.
454 int __mmc_claim_host(struct mmc_host *host, atomic_t *abort)
456 DECLARE_WAITQUEUE(wait, current);
462 add_wait_queue(&host->wq, &wait);
463 spin_lock_irqsave(&host->lock, flags);
465 set_current_state(TASK_UNINTERRUPTIBLE);
466 stop = abort ? atomic_read(abort) : 0;
467 if (stop || !host->claimed || host->claimer == current)
469 spin_unlock_irqrestore(&host->lock, flags);
471 spin_lock_irqsave(&host->lock, flags);
473 set_current_state(TASK_RUNNING);
476 host->claimer = current;
477 host->claim_cnt += 1;
480 spin_unlock_irqrestore(&host->lock, flags);
481 remove_wait_queue(&host->wq, &wait);
483 mmc_host_enable(host);
487 EXPORT_SYMBOL(__mmc_claim_host);
490 * mmc_try_claim_host - try exclusively to claim a host
491 * @host: mmc host to claim
493 * Returns %1 if the host is claimed, %0 otherwise.
495 int mmc_try_claim_host(struct mmc_host *host)
497 int claimed_host = 0;
500 spin_lock_irqsave(&host->lock, flags);
501 if (!host->claimed || host->claimer == current) {
503 host->claimer = current;
504 host->claim_cnt += 1;
507 spin_unlock_irqrestore(&host->lock, flags);
510 EXPORT_SYMBOL(mmc_try_claim_host);
512 static void mmc_do_release_host(struct mmc_host *host)
516 spin_lock_irqsave(&host->lock, flags);
517 if (--host->claim_cnt) {
518 /* Release for nested claim */
519 spin_unlock_irqrestore(&host->lock, flags);
522 host->claimer = NULL;
523 spin_unlock_irqrestore(&host->lock, flags);
528 void mmc_host_deeper_disable(struct work_struct *work)
530 struct mmc_host *host =
531 container_of(work, struct mmc_host, disable.work);
533 /* If the host is claimed then we do not want to disable it anymore */
534 if (!mmc_try_claim_host(host))
536 mmc_host_do_disable(host, 1);
537 mmc_do_release_host(host);
540 wake_unlock(&mmc_delayed_work_wake_lock);
544 * mmc_host_lazy_disable - lazily disable a host.
545 * @host: mmc host to disable
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.
551 int mmc_host_lazy_disable(struct mmc_host *host)
553 if (!(host->caps & MMC_CAP_DISABLE))
556 if (host->en_dis_recurs)
559 if (--host->nesting_cnt)
565 if (host->disable_delay) {
566 mmc_schedule_delayed_work(&host->disable,
567 msecs_to_jiffies(host->disable_delay));
570 return mmc_host_do_disable(host, 1);
572 EXPORT_SYMBOL(mmc_host_lazy_disable);
575 * mmc_release_host - release a host
576 * @host: mmc host to release
578 * Release a MMC host, allowing others to claim the host
579 * for their operations.
581 void mmc_release_host(struct mmc_host *host)
583 WARN_ON(!host->claimed);
585 mmc_host_lazy_disable(host);
587 mmc_do_release_host(host);
590 EXPORT_SYMBOL(mmc_release_host);
593 * Internal function that does the actual ios call to the host driver,
594 * optionally printing some debug output.
596 static inline void mmc_set_ios(struct mmc_host *host)
598 struct mmc_ios *ios = &host->ios;
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);
606 host->ops->set_ios(host, ios);
610 * Control chip select pin on a host.
612 void mmc_set_chip_select(struct mmc_host *host, int mode)
614 host->ios.chip_select = mode;
619 * Sets the host clock to the highest possible frequency that
622 void mmc_set_clock(struct mmc_host *host, unsigned int hz)
624 WARN_ON(hz < host->f_min);
626 if (hz > host->f_max)
629 host->ios.clock = hz;
634 * Change the bus mode (open drain/push-pull) of a host.
636 void mmc_set_bus_mode(struct mmc_host *host, unsigned int mode)
638 host->ios.bus_mode = mode;
643 * Change data bus width of a host.
645 void mmc_set_bus_width(struct mmc_host *host, unsigned int width)
647 host->ios.bus_width = width;
652 * mmc_vdd_to_ocrbitnum - Convert a voltage to the OCR bit number
654 * @low_bits: prefer low bits in boundary cases
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.
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);
664 * Any value in the [1951:1999] range translates to the ilog2(MMC_VDD_20_21).
666 static int mmc_vdd_to_ocrbitnum(int vdd, bool low_bits)
668 const int max_bit = ilog2(MMC_VDD_35_36);
671 if (vdd < 1650 || vdd > 3600)
674 if (vdd >= 1650 && vdd <= 1950)
675 return ilog2(MMC_VDD_165_195);
680 /* Base 2000 mV, step 100 mV, bit's base 8. */
681 bit = (vdd - 2000) / 100 + 8;
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)
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.
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.
700 u32 mmc_vddrange_to_ocrmask(int vdd_min, int vdd_max)
704 if (vdd_max < vdd_min)
707 /* Prefer high bits for the boundary vdd_max values. */
708 vdd_max = mmc_vdd_to_ocrbitnum(vdd_max, false);
712 /* Prefer low bits for the boundary vdd_min values. */
713 vdd_min = mmc_vdd_to_ocrbitnum(vdd_min, true);
717 /* Fill the mask, from max bit to min bit. */
718 while (vdd_max >= vdd_min)
719 mask |= 1 << vdd_max--;
723 EXPORT_SYMBOL(mmc_vddrange_to_ocrmask);
725 #ifdef CONFIG_REGULATOR
728 * mmc_regulator_get_ocrmask - return mask of supported voltages
729 * @supply: regulator to use
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
736 int mmc_regulator_get_ocrmask(struct regulator *supply)
742 count = regulator_count_voltages(supply);
746 for (i = 0; i < count; i++) {
750 vdd_uV = regulator_list_voltage(supply, i);
754 vdd_mV = vdd_uV / 1000;
755 result |= mmc_vddrange_to_ocrmask(vdd_mV, vdd_mV);
760 EXPORT_SYMBOL(mmc_regulator_get_ocrmask);
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
767 * Returns zero on success, else negative errno.
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
773 int mmc_regulator_set_ocr(struct regulator *supply, unsigned short vdd_bit)
779 enabled = regulator_is_enabled(supply);
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).
792 tmp = vdd_bit - ilog2(MMC_VDD_165_195);
794 min_uV = 1650 * 1000;
795 max_uV = 1950 * 1000;
797 min_uV = 1900 * 1000 + tmp * 100 * 1000;
798 max_uV = min_uV + 100 * 1000;
801 /* avoid needless changes to this voltage; the regulator
802 * might not allow this operation
804 voltage = regulator_get_voltage(supply);
807 else if (voltage < min_uV || voltage > max_uV)
808 result = regulator_set_voltage(supply, min_uV, max_uV);
812 if (result == 0 && !enabled)
813 result = regulator_enable(supply);
814 } else if (enabled) {
815 result = regulator_disable(supply);
820 EXPORT_SYMBOL(mmc_regulator_set_ocr);
825 * Mask off any voltages we don't support and select
828 u32 mmc_select_voltage(struct mmc_host *host, u32 ocr)
832 ocr &= host->ocr_avail;
843 pr_warning("%s: host doesn't support card's voltages\n",
852 * Select timing parameters for host.
854 void mmc_set_timing(struct mmc_host *host, unsigned int timing)
856 host->ios.timing = timing;
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.
866 * We must _NOT_ enable the clock prior to power stablising.
868 * If a host does all the power sequencing itself, ignore the
869 * initial MMC_POWER_UP stage.
871 static void mmc_power_up(struct mmc_host *host)
875 /* If ocr is set, we use it */
877 bit = ffs(host->ocr) - 1;
879 bit = fls(host->ocr_avail) - 1;
882 if (mmc_host_is_spi(host)) {
883 host->ios.chip_select = MMC_CS_HIGH;
884 host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
886 host->ios.chip_select = MMC_CS_DONTCARE;
887 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
889 host->ios.power_mode = MMC_POWER_UP;
890 host->ios.bus_width = MMC_BUS_WIDTH_1;
891 host->ios.timing = MMC_TIMING_LEGACY;
895 * This delay should be sufficient to allow the power supply
896 * to reach the minimum voltage.
900 host->ios.clock = host->f_min;
902 host->ios.power_mode = MMC_POWER_ON;
906 * This delay must be at least 74 clock sizes, or 1 ms, or the
907 * time required to reach a stable voltage.
912 static void mmc_power_off(struct mmc_host *host)
916 if (!mmc_host_is_spi(host)) {
917 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
918 host->ios.chip_select = MMC_CS_DONTCARE;
920 host->ios.power_mode = MMC_POWER_OFF;
921 host->ios.bus_width = MMC_BUS_WIDTH_1;
922 host->ios.timing = MMC_TIMING_LEGACY;
927 * Cleanup when the last reference to the bus operator is dropped.
929 static void __mmc_release_bus(struct mmc_host *host)
932 BUG_ON(host->bus_refs);
933 BUG_ON(!host->bus_dead);
935 host->bus_ops = NULL;
939 * Increase reference count of bus operator
941 static inline void mmc_bus_get(struct mmc_host *host)
945 spin_lock_irqsave(&host->lock, flags);
947 spin_unlock_irqrestore(&host->lock, flags);
951 * Decrease reference count of bus operator and free it if
952 * it is the last reference.
954 static inline void mmc_bus_put(struct mmc_host *host)
958 spin_lock_irqsave(&host->lock, flags);
960 if ((host->bus_refs == 0) && host->bus_ops)
961 __mmc_release_bus(host);
962 spin_unlock_irqrestore(&host->lock, flags);
965 int mmc_resume_bus(struct mmc_host *host)
967 if (!mmc_bus_needs_resume(host))
970 printk("%s: Starting deferred resume\n", mmc_hostname(host));
971 host->bus_resume_flags &= ~MMC_BUSRESUME_NEEDS_RESUME;
973 if (host->bus_ops && !host->bus_dead) {
975 BUG_ON(!host->bus_ops->resume);
976 host->bus_ops->resume(host);
979 if (host->bus_ops->detect && !host->bus_dead)
980 host->bus_ops->detect(host);
983 printk("%s: Deferred resume completed\n", mmc_hostname(host));
987 EXPORT_SYMBOL(mmc_resume_bus);
990 * Assign a mmc bus handler to a host. Only one bus handler may control a
991 * host at any given time.
993 void mmc_attach_bus(struct mmc_host *host, const struct mmc_bus_ops *ops)
1000 WARN_ON(!host->claimed);
1002 spin_lock_irqsave(&host->lock, flags);
1004 BUG_ON(host->bus_ops);
1005 BUG_ON(host->bus_refs);
1007 host->bus_ops = ops;
1011 spin_unlock_irqrestore(&host->lock, flags);
1015 * Remove the current bus handler from a host. Assumes that there are
1016 * no interesting cards left, so the bus is powered down.
1018 void mmc_detach_bus(struct mmc_host *host)
1020 unsigned long flags;
1024 WARN_ON(!host->claimed);
1025 WARN_ON(!host->bus_ops);
1027 spin_lock_irqsave(&host->lock, flags);
1031 spin_unlock_irqrestore(&host->lock, flags);
1033 mmc_power_off(host);
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)
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
1048 void mmc_detect_change(struct mmc_host *host, unsigned long delay)
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);
1057 mmc_schedule_delayed_work(&host->detect, delay);
1060 EXPORT_SYMBOL(mmc_detect_change);
1063 void mmc_rescan(struct work_struct *work)
1065 struct mmc_host *host =
1066 container_of(work, struct mmc_host, detect.work);
1069 int extend_wakelock = 0;
1070 unsigned long flags;
1072 spin_lock_irqsave(&host->lock, flags);
1074 if (host->rescan_disable) {
1075 spin_unlock_irqrestore(&host->lock, flags);
1079 spin_unlock_irqrestore(&host->lock, flags);
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);
1088 /* If the card was removed the bus will be marked
1089 * as dead - extend the wakelock so userspace
1092 extend_wakelock = 1;
1099 /* if there still is a card present, stop here */
1100 if (host->bus_ops != NULL) {
1105 /* detect a newly inserted card */
1108 * Only we can add a new handler, so it's safe to
1109 * release the lock here.
1113 if (host->ops->get_cd && host->ops->get_cd(host) == 0)
1116 mmc_claim_host(host);
1121 mmc_send_if_cond(host, host->ocr_avail);
1124 * First we search for SDIO...
1126 err = mmc_send_io_op_cond(host, 0, &ocr);
1128 if (mmc_attach_sdio(host, ocr))
1129 mmc_power_off(host);
1130 extend_wakelock = 1;
1135 * ...then normal SD...
1137 err = mmc_send_app_op_cond(host, 0, &ocr);
1139 if (mmc_attach_sd(host, ocr))
1140 mmc_power_off(host);
1141 extend_wakelock = 1;
1146 * ...and finally MMC.
1148 err = mmc_send_op_cond(host, 0, &ocr);
1150 if (mmc_attach_mmc(host, ocr))
1151 mmc_power_off(host);
1152 extend_wakelock = 1;
1156 mmc_release_host(host);
1157 mmc_power_off(host);
1160 if (extend_wakelock)
1161 wake_lock_timeout(&mmc_delayed_work_wake_lock, HZ / 2);
1163 wake_unlock(&mmc_delayed_work_wake_lock);
1165 if (host->caps & MMC_CAP_NEEDS_POLL)
1166 mmc_schedule_delayed_work(&host->detect, HZ);
1169 void mmc_start_host(struct mmc_host *host)
1171 mmc_power_off(host);
1172 mmc_detect_change(host, 0);
1175 void mmc_stop_host(struct mmc_host *host)
1177 #ifdef CONFIG_MMC_DEBUG
1178 unsigned long flags;
1179 spin_lock_irqsave(&host->lock, flags);
1181 spin_unlock_irqrestore(&host->lock, flags);
1184 if (host->caps & MMC_CAP_DISABLE)
1185 cancel_delayed_work(&host->disable);
1186 cancel_delayed_work(&host->detect);
1187 mmc_flush_scheduled_work();
1190 if (host->bus_ops && !host->bus_dead) {
1191 if (host->bus_ops->remove)
1192 host->bus_ops->remove(host);
1194 mmc_claim_host(host);
1195 mmc_detach_bus(host);
1196 mmc_release_host(host);
1204 mmc_power_off(host);
1207 void mmc_power_save_host(struct mmc_host *host)
1211 if (!host->bus_ops || host->bus_dead || !host->bus_ops->power_restore) {
1216 if (host->bus_ops->power_save)
1217 host->bus_ops->power_save(host);
1221 mmc_power_off(host);
1223 EXPORT_SYMBOL(mmc_power_save_host);
1225 void mmc_power_restore_host(struct mmc_host *host)
1229 if (!host->bus_ops || host->bus_dead || !host->bus_ops->power_restore) {
1235 host->bus_ops->power_restore(host);
1239 EXPORT_SYMBOL(mmc_power_restore_host);
1241 int mmc_card_awake(struct mmc_host *host)
1247 if (host->bus_ops && !host->bus_dead && host->bus_ops->awake)
1248 err = host->bus_ops->awake(host);
1254 EXPORT_SYMBOL(mmc_card_awake);
1256 int mmc_card_sleep(struct mmc_host *host)
1262 if (host->bus_ops && !host->bus_dead && host->bus_ops->awake)
1263 err = host->bus_ops->sleep(host);
1269 EXPORT_SYMBOL(mmc_card_sleep);
1271 int mmc_card_can_sleep(struct mmc_host *host)
1273 struct mmc_card *card = host->card;
1275 if (card && mmc_card_mmc(card) && card->ext_csd.rev >= 3)
1279 EXPORT_SYMBOL(mmc_card_can_sleep);
1284 * mmc_suspend_host - suspend a host
1286 * @state: suspend mode (PM_SUSPEND_xxx)
1288 int mmc_suspend_host(struct mmc_host *host, pm_message_t state)
1292 if (mmc_bus_needs_resume(host))
1295 if (host->caps & MMC_CAP_DISABLE)
1296 cancel_delayed_work(&host->disable);
1297 cancel_delayed_work(&host->detect);
1298 mmc_flush_scheduled_work();
1301 if (host->bus_ops && !host->bus_dead) {
1302 if (host->bus_ops->suspend)
1303 err = host->bus_ops->suspend(host);
1308 mmc_power_off(host);
1313 EXPORT_SYMBOL(mmc_suspend_host);
1316 * mmc_resume_host - resume a previously suspended host
1319 int mmc_resume_host(struct mmc_host *host)
1324 if (host->bus_resume_flags & MMC_BUSRESUME_MANUAL_RESUME) {
1325 host->bus_resume_flags |= MMC_BUSRESUME_NEEDS_RESUME;
1330 if (host->bus_ops && !host->bus_dead) {
1332 mmc_select_voltage(host, host->ocr);
1333 BUG_ON(!host->bus_ops->resume);
1334 err = host->bus_ops->resume(host);
1336 printk(KERN_WARNING "%s: error %d during resume "
1337 "(card was removed?)\n",
1338 mmc_hostname(host), err);
1346 EXPORT_SYMBOL(mmc_resume_host);
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
1352 int mmc_pm_notify(struct notifier_block *notify_block,
1353 unsigned long mode, void *unused)
1355 struct mmc_host *host = container_of(
1356 notify_block, struct mmc_host, pm_notify);
1357 unsigned long flags;
1361 case PM_HIBERNATION_PREPARE:
1362 case PM_SUSPEND_PREPARE:
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);
1369 if (!host->bus_ops || host->bus_ops->suspend)
1372 mmc_claim_host(host);
1374 if (host->bus_ops->remove)
1375 host->bus_ops->remove(host);
1377 mmc_detach_bus(host);
1378 mmc_release_host(host);
1381 case PM_POST_SUSPEND:
1382 case PM_POST_HIBERNATION:
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);
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,
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;
1408 EXPORT_SYMBOL(mmc_set_embedded_sdio_data);
1411 static int __init mmc_init(void)
1415 wake_lock_init(&mmc_delayed_work_wake_lock, WAKE_LOCK_SUSPEND, "mmc_delayed_work");
1417 workqueue = create_singlethread_workqueue("kmmcd");
1421 ret = mmc_register_bus();
1423 goto destroy_workqueue;
1425 ret = mmc_register_host_class();
1427 goto unregister_bus;
1429 ret = sdio_register_bus();
1431 goto unregister_host_class;
1435 unregister_host_class:
1436 mmc_unregister_host_class();
1438 mmc_unregister_bus();
1440 destroy_workqueue(workqueue);
1445 static void __exit mmc_exit(void)
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);
1454 subsys_initcall(mmc_init);
1455 module_exit(mmc_exit);
1457 MODULE_LICENSE("GPL");