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);
541 * mmc_host_lazy_disable - lazily disable a host.
542 * @host: mmc host to disable
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.
548 int mmc_host_lazy_disable(struct mmc_host *host)
550 if (!(host->caps & MMC_CAP_DISABLE))
553 if (host->en_dis_recurs)
556 if (--host->nesting_cnt)
562 if (host->disable_delay) {
563 mmc_schedule_delayed_work(&host->disable,
564 msecs_to_jiffies(host->disable_delay));
567 return mmc_host_do_disable(host, 1);
569 EXPORT_SYMBOL(mmc_host_lazy_disable);
572 * mmc_release_host - release a host
573 * @host: mmc host to release
575 * Release a MMC host, allowing others to claim the host
576 * for their operations.
578 void mmc_release_host(struct mmc_host *host)
580 WARN_ON(!host->claimed);
582 mmc_host_lazy_disable(host);
584 mmc_do_release_host(host);
587 EXPORT_SYMBOL(mmc_release_host);
590 * Internal function that does the actual ios call to the host driver,
591 * optionally printing some debug output.
593 static inline void mmc_set_ios(struct mmc_host *host)
595 struct mmc_ios *ios = &host->ios;
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);
603 host->ops->set_ios(host, ios);
607 * Control chip select pin on a host.
609 void mmc_set_chip_select(struct mmc_host *host, int mode)
611 host->ios.chip_select = mode;
616 * Sets the host clock to the highest possible frequency that
619 void mmc_set_clock(struct mmc_host *host, unsigned int hz)
621 WARN_ON(hz < host->f_min);
623 if (hz > host->f_max)
626 host->ios.clock = hz;
631 * Change the bus mode (open drain/push-pull) of a host.
633 void mmc_set_bus_mode(struct mmc_host *host, unsigned int mode)
635 host->ios.bus_mode = mode;
640 * Change data bus width of a host.
642 void mmc_set_bus_width(struct mmc_host *host, unsigned int width)
644 host->ios.bus_width = width;
649 * mmc_vdd_to_ocrbitnum - Convert a voltage to the OCR bit number
651 * @low_bits: prefer low bits in boundary cases
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.
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);
661 * Any value in the [1951:1999] range translates to the ilog2(MMC_VDD_20_21).
663 static int mmc_vdd_to_ocrbitnum(int vdd, bool low_bits)
665 const int max_bit = ilog2(MMC_VDD_35_36);
668 if (vdd < 1650 || vdd > 3600)
671 if (vdd >= 1650 && vdd <= 1950)
672 return ilog2(MMC_VDD_165_195);
677 /* Base 2000 mV, step 100 mV, bit's base 8. */
678 bit = (vdd - 2000) / 100 + 8;
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)
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.
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.
697 u32 mmc_vddrange_to_ocrmask(int vdd_min, int vdd_max)
701 if (vdd_max < vdd_min)
704 /* Prefer high bits for the boundary vdd_max values. */
705 vdd_max = mmc_vdd_to_ocrbitnum(vdd_max, false);
709 /* Prefer low bits for the boundary vdd_min values. */
710 vdd_min = mmc_vdd_to_ocrbitnum(vdd_min, true);
714 /* Fill the mask, from max bit to min bit. */
715 while (vdd_max >= vdd_min)
716 mask |= 1 << vdd_max--;
720 EXPORT_SYMBOL(mmc_vddrange_to_ocrmask);
722 #ifdef CONFIG_REGULATOR
725 * mmc_regulator_get_ocrmask - return mask of supported voltages
726 * @supply: regulator to use
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
733 int mmc_regulator_get_ocrmask(struct regulator *supply)
739 count = regulator_count_voltages(supply);
743 for (i = 0; i < count; i++) {
747 vdd_uV = regulator_list_voltage(supply, i);
751 vdd_mV = vdd_uV / 1000;
752 result |= mmc_vddrange_to_ocrmask(vdd_mV, vdd_mV);
757 EXPORT_SYMBOL(mmc_regulator_get_ocrmask);
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
764 * Returns zero on success, else negative errno.
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
770 int mmc_regulator_set_ocr(struct regulator *supply, unsigned short vdd_bit)
776 enabled = regulator_is_enabled(supply);
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).
789 tmp = vdd_bit - ilog2(MMC_VDD_165_195);
791 min_uV = 1650 * 1000;
792 max_uV = 1950 * 1000;
794 min_uV = 1900 * 1000 + tmp * 100 * 1000;
795 max_uV = min_uV + 100 * 1000;
798 /* avoid needless changes to this voltage; the regulator
799 * might not allow this operation
801 voltage = regulator_get_voltage(supply);
804 else if (voltage < min_uV || voltage > max_uV)
805 result = regulator_set_voltage(supply, min_uV, max_uV);
809 if (result == 0 && !enabled)
810 result = regulator_enable(supply);
811 } else if (enabled) {
812 result = regulator_disable(supply);
817 EXPORT_SYMBOL(mmc_regulator_set_ocr);
822 * Mask off any voltages we don't support and select
825 u32 mmc_select_voltage(struct mmc_host *host, u32 ocr)
829 ocr &= host->ocr_avail;
840 pr_warning("%s: host doesn't support card's voltages\n",
849 * Select timing parameters for host.
851 void mmc_set_timing(struct mmc_host *host, unsigned int timing)
853 host->ios.timing = timing;
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.
863 * We must _NOT_ enable the clock prior to power stablising.
865 * If a host does all the power sequencing itself, ignore the
866 * initial MMC_POWER_UP stage.
868 static void mmc_power_up(struct mmc_host *host)
872 /* If ocr is set, we use it */
874 bit = ffs(host->ocr) - 1;
876 bit = fls(host->ocr_avail) - 1;
879 if (mmc_host_is_spi(host)) {
880 host->ios.chip_select = MMC_CS_HIGH;
881 host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
883 host->ios.chip_select = MMC_CS_DONTCARE;
884 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
886 host->ios.power_mode = MMC_POWER_UP;
887 host->ios.bus_width = MMC_BUS_WIDTH_1;
888 host->ios.timing = MMC_TIMING_LEGACY;
892 * This delay should be sufficient to allow the power supply
893 * to reach the minimum voltage.
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;
902 host->ios.clock = 400000;
904 host->ios.power_mode = MMC_POWER_ON;
908 * This delay must be at least 74 clock sizes, or 1 ms, or the
909 * time required to reach a stable voltage.
914 static void mmc_power_off(struct mmc_host *host)
918 if (!mmc_host_is_spi(host)) {
919 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
920 host->ios.chip_select = MMC_CS_DONTCARE;
922 host->ios.power_mode = MMC_POWER_OFF;
923 host->ios.bus_width = MMC_BUS_WIDTH_1;
924 host->ios.timing = MMC_TIMING_LEGACY;
929 * Cleanup when the last reference to the bus operator is dropped.
931 static void __mmc_release_bus(struct mmc_host *host)
934 BUG_ON(host->bus_refs);
935 BUG_ON(!host->bus_dead);
937 host->bus_ops = NULL;
941 * Increase reference count of bus operator
943 static inline void mmc_bus_get(struct mmc_host *host)
947 spin_lock_irqsave(&host->lock, flags);
949 spin_unlock_irqrestore(&host->lock, flags);
953 * Decrease reference count of bus operator and free it if
954 * it is the last reference.
956 static inline void mmc_bus_put(struct mmc_host *host)
960 spin_lock_irqsave(&host->lock, flags);
962 if ((host->bus_refs == 0) && host->bus_ops)
963 __mmc_release_bus(host);
964 spin_unlock_irqrestore(&host->lock, flags);
967 int mmc_resume_bus(struct mmc_host *host)
969 if (!mmc_bus_needs_resume(host))
972 printk("%s: Starting deferred resume\n", mmc_hostname(host));
973 host->bus_resume_flags &= ~MMC_BUSRESUME_NEEDS_RESUME;
975 if (host->bus_ops && !host->bus_dead) {
977 BUG_ON(!host->bus_ops->resume);
978 host->bus_ops->resume(host);
981 if (host->bus_ops->detect && !host->bus_dead)
982 host->bus_ops->detect(host);
985 printk("%s: Deferred resume completed\n", mmc_hostname(host));
989 EXPORT_SYMBOL(mmc_resume_bus);
992 * Assign a mmc bus handler to a host. Only one bus handler may control a
993 * host at any given time.
995 void mmc_attach_bus(struct mmc_host *host, const struct mmc_bus_ops *ops)
1002 WARN_ON(!host->claimed);
1004 spin_lock_irqsave(&host->lock, flags);
1006 BUG_ON(host->bus_ops);
1007 BUG_ON(host->bus_refs);
1009 host->bus_ops = ops;
1013 spin_unlock_irqrestore(&host->lock, flags);
1017 * Remove the current bus handler from a host. Assumes that there are
1018 * no interesting cards left, so the bus is powered down.
1020 void mmc_detach_bus(struct mmc_host *host)
1022 unsigned long flags;
1026 WARN_ON(!host->claimed);
1027 WARN_ON(!host->bus_ops);
1029 spin_lock_irqsave(&host->lock, flags);
1033 spin_unlock_irqrestore(&host->lock, flags);
1035 mmc_power_off(host);
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)
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
1050 void mmc_detect_change(struct mmc_host *host, unsigned long delay)
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);
1059 mmc_schedule_delayed_work(&host->detect, delay);
1062 EXPORT_SYMBOL(mmc_detect_change);
1065 void mmc_rescan(struct work_struct *work)
1067 struct mmc_host *host =
1068 container_of(work, struct mmc_host, detect.work);
1071 int extend_wakelock = 0;
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);
1079 /* If the card was removed the bus will be marked
1080 * as dead - extend the wakelock so userspace
1083 extend_wakelock = 1;
1090 /* if there still is a card present, stop here */
1091 if (host->bus_ops != NULL) {
1096 /* detect a newly inserted card */
1099 * Only we can add a new handler, so it's safe to
1100 * release the lock here.
1104 if (host->ops->get_cd && host->ops->get_cd(host) == 0)
1107 mmc_claim_host(host);
1112 mmc_send_if_cond(host, host->ocr_avail);
1115 * First we search for SDIO...
1117 err = mmc_send_io_op_cond(host, 0, &ocr);
1119 if (mmc_attach_sdio(host, ocr))
1120 mmc_power_off(host);
1121 extend_wakelock = 1;
1126 * ...then normal SD...
1128 err = mmc_send_app_op_cond(host, 0, &ocr);
1130 if (mmc_attach_sd(host, ocr))
1131 mmc_power_off(host);
1132 extend_wakelock = 1;
1137 * ...and finally MMC.
1139 err = mmc_send_op_cond(host, 0, &ocr);
1141 if (mmc_attach_mmc(host, ocr))
1142 mmc_power_off(host);
1143 extend_wakelock = 1;
1147 mmc_release_host(host);
1148 mmc_power_off(host);
1151 if (extend_wakelock)
1152 wake_lock_timeout(&mmc_delayed_work_wake_lock, HZ / 2);
1154 wake_unlock(&mmc_delayed_work_wake_lock);
1156 if (host->caps & MMC_CAP_NEEDS_POLL)
1157 mmc_schedule_delayed_work(&host->detect, HZ);
1160 void mmc_start_host(struct mmc_host *host)
1162 mmc_power_off(host);
1163 mmc_detect_change(host, 0);
1166 void mmc_stop_host(struct mmc_host *host)
1168 #ifdef CONFIG_MMC_DEBUG
1169 unsigned long flags;
1170 spin_lock_irqsave(&host->lock, flags);
1172 spin_unlock_irqrestore(&host->lock, flags);
1175 if (host->caps & MMC_CAP_DISABLE)
1176 cancel_delayed_work(&host->disable);
1177 cancel_delayed_work(&host->detect);
1178 mmc_flush_scheduled_work();
1181 if (host->bus_ops && !host->bus_dead) {
1182 if (host->bus_ops->remove)
1183 host->bus_ops->remove(host);
1185 mmc_claim_host(host);
1186 mmc_detach_bus(host);
1187 mmc_release_host(host);
1195 mmc_power_off(host);
1198 void mmc_power_save_host(struct mmc_host *host)
1202 if (!host->bus_ops || host->bus_dead || !host->bus_ops->power_restore) {
1207 if (host->bus_ops->power_save)
1208 host->bus_ops->power_save(host);
1212 mmc_power_off(host);
1214 EXPORT_SYMBOL(mmc_power_save_host);
1216 void mmc_power_restore_host(struct mmc_host *host)
1220 if (!host->bus_ops || host->bus_dead || !host->bus_ops->power_restore) {
1226 host->bus_ops->power_restore(host);
1230 EXPORT_SYMBOL(mmc_power_restore_host);
1232 int mmc_card_awake(struct mmc_host *host)
1238 if (host->bus_ops && !host->bus_dead && host->bus_ops->awake)
1239 err = host->bus_ops->awake(host);
1245 EXPORT_SYMBOL(mmc_card_awake);
1247 int mmc_card_sleep(struct mmc_host *host)
1253 if (host->bus_ops && !host->bus_dead && host->bus_ops->awake)
1254 err = host->bus_ops->sleep(host);
1260 EXPORT_SYMBOL(mmc_card_sleep);
1262 int mmc_card_can_sleep(struct mmc_host *host)
1264 struct mmc_card *card = host->card;
1266 if (card && mmc_card_mmc(card) && card->ext_csd.rev >= 3)
1270 EXPORT_SYMBOL(mmc_card_can_sleep);
1275 * mmc_suspend_host - suspend a host
1277 * @state: suspend mode (PM_SUSPEND_xxx)
1279 int mmc_suspend_host(struct mmc_host *host, pm_message_t state)
1283 if (mmc_bus_needs_resume(host))
1286 if (host->caps & MMC_CAP_DISABLE)
1287 cancel_delayed_work(&host->disable);
1288 cancel_delayed_work(&host->detect);
1289 mmc_flush_scheduled_work();
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) {
1297 * We simply "remove" the card in this case.
1298 * It will be redetected on resume.
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);
1311 mmc_power_off(host);
1316 EXPORT_SYMBOL(mmc_suspend_host);
1319 * mmc_resume_host - resume a previously suspended host
1322 int mmc_resume_host(struct mmc_host *host)
1327 if (host->bus_resume_flags & MMC_BUSRESUME_MANUAL_RESUME) {
1328 host->bus_resume_flags |= MMC_BUSRESUME_NEEDS_RESUME;
1333 if (host->bus_ops && !host->bus_dead) {
1335 mmc_select_voltage(host, host->ocr);
1336 BUG_ON(!host->bus_ops->resume);
1337 err = host->bus_ops->resume(host);
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 */
1354 * We add a slight delay here so that resume can progress
1357 mmc_detect_change(host, 1);
1362 EXPORT_SYMBOL(mmc_resume_host);
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,
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;
1379 EXPORT_SYMBOL(mmc_set_embedded_sdio_data);
1382 static int __init mmc_init(void)
1386 wake_lock_init(&mmc_delayed_work_wake_lock, WAKE_LOCK_SUSPEND, "mmc_delayed_work");
1388 workqueue = create_singlethread_workqueue("kmmcd");
1392 ret = mmc_register_bus();
1394 goto destroy_workqueue;
1396 ret = mmc_register_host_class();
1398 goto unregister_bus;
1400 ret = sdio_register_bus();
1402 goto unregister_host_class;
1406 unregister_host_class:
1407 mmc_unregister_host_class();
1409 mmc_unregister_bus();
1411 destroy_workqueue(workqueue);
1416 static void __exit mmc_exit(void)
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);
1425 subsys_initcall(mmc_init);
1426 module_exit(mmc_exit);
1428 MODULE_LICENSE("GPL");