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 * We normally treat cards as removed during suspend if they are not
54 * known to be on a non-removable bus, to avoid the risk of writing
55 * back data to a different card after resume. Allow this to be
56 * overridden if necessary.
58 #ifdef CONFIG_MMC_UNSAFE_RESUME
59 int mmc_assume_removable;
61 int mmc_assume_removable = 1;
63 module_param_named(removable, mmc_assume_removable, bool, 0644);
66 "MMC/SD cards are removable and may be removed during suspend");
69 * Internal function. Schedule delayed work in the MMC work queue.
71 static int mmc_schedule_delayed_work(struct delayed_work *work,
74 wake_lock(&mmc_delayed_work_wake_lock);
75 return queue_delayed_work(workqueue, work, delay);
79 * Internal function. Flush all scheduled work from the MMC work queue.
81 static void mmc_flush_scheduled_work(void)
83 flush_workqueue(workqueue);
87 * mmc_request_done - finish processing an MMC request
88 * @host: MMC host which completed request
89 * @mrq: MMC request which request
91 * MMC drivers should call this function when they have completed
92 * their processing of a request.
94 void mmc_request_done(struct mmc_host *host, struct mmc_request *mrq)
96 struct mmc_command *cmd = mrq->cmd;
99 if (err && cmd->retries && mmc_host_is_spi(host)) {
100 if (cmd->resp[0] & R1_SPI_ILLEGAL_COMMAND)
104 if (err && cmd->retries) {
105 pr_debug("%s: req failed (CMD%u): %d, retrying...\n",
106 mmc_hostname(host), cmd->opcode, err);
110 host->ops->request(host, mrq);
112 led_trigger_event(host->led, LED_OFF);
114 pr_debug("%s: req done (CMD%u): %d: %08x %08x %08x %08x\n",
115 mmc_hostname(host), cmd->opcode, err,
116 cmd->resp[0], cmd->resp[1],
117 cmd->resp[2], cmd->resp[3]);
120 pr_debug("%s: %d bytes transferred: %d\n",
122 mrq->data->bytes_xfered, mrq->data->error);
126 pr_debug("%s: (CMD%u): %d: %08x %08x %08x %08x\n",
127 mmc_hostname(host), mrq->stop->opcode,
129 mrq->stop->resp[0], mrq->stop->resp[1],
130 mrq->stop->resp[2], mrq->stop->resp[3]);
138 EXPORT_SYMBOL(mmc_request_done);
141 mmc_start_request(struct mmc_host *host, struct mmc_request *mrq)
143 #ifdef CONFIG_MMC_DEBUG
145 struct scatterlist *sg;
148 pr_debug("%s: starting CMD%u arg %08x flags %08x\n",
149 mmc_hostname(host), mrq->cmd->opcode,
150 mrq->cmd->arg, mrq->cmd->flags);
153 pr_debug("%s: blksz %d blocks %d flags %08x "
154 "tsac %d ms nsac %d\n",
155 mmc_hostname(host), mrq->data->blksz,
156 mrq->data->blocks, mrq->data->flags,
157 mrq->data->timeout_ns / 1000000,
158 mrq->data->timeout_clks);
162 pr_debug("%s: CMD%u arg %08x flags %08x\n",
163 mmc_hostname(host), mrq->stop->opcode,
164 mrq->stop->arg, mrq->stop->flags);
167 WARN_ON(!host->claimed);
169 led_trigger_event(host->led, LED_FULL);
174 BUG_ON(mrq->data->blksz > host->max_blk_size);
175 BUG_ON(mrq->data->blocks > host->max_blk_count);
176 BUG_ON(mrq->data->blocks * mrq->data->blksz >
179 #ifdef CONFIG_MMC_DEBUG
181 for_each_sg(mrq->data->sg, sg, mrq->data->sg_len, i)
183 BUG_ON(sz != mrq->data->blocks * mrq->data->blksz);
186 mrq->cmd->data = mrq->data;
187 mrq->data->error = 0;
188 mrq->data->mrq = mrq;
190 mrq->data->stop = mrq->stop;
191 mrq->stop->error = 0;
192 mrq->stop->mrq = mrq;
195 host->ops->request(host, mrq);
198 static void mmc_wait_done(struct mmc_request *mrq)
200 complete(mrq->done_data);
204 * mmc_wait_for_req - start a request and wait for completion
205 * @host: MMC host to start command
206 * @mrq: MMC request to start
208 * Start a new MMC custom command request for a host, and wait
209 * for the command to complete. Does not attempt to parse the
212 void mmc_wait_for_req(struct mmc_host *host, struct mmc_request *mrq)
214 DECLARE_COMPLETION_ONSTACK(complete);
216 mrq->done_data = &complete;
217 mrq->done = mmc_wait_done;
219 mmc_start_request(host, mrq);
221 wait_for_completion(&complete);
224 EXPORT_SYMBOL(mmc_wait_for_req);
227 * mmc_wait_for_cmd - start a command and wait for completion
228 * @host: MMC host to start command
229 * @cmd: MMC command to start
230 * @retries: maximum number of retries
232 * Start a new MMC command for a host, and wait for the command
233 * to complete. Return any error that occurred while the command
234 * was executing. Do not attempt to parse the response.
236 int mmc_wait_for_cmd(struct mmc_host *host, struct mmc_command *cmd, int retries)
238 struct mmc_request mrq;
240 WARN_ON(!host->claimed);
242 memset(&mrq, 0, sizeof(struct mmc_request));
244 memset(cmd->resp, 0, sizeof(cmd->resp));
245 cmd->retries = retries;
250 mmc_wait_for_req(host, &mrq);
255 EXPORT_SYMBOL(mmc_wait_for_cmd);
258 * mmc_set_data_timeout - set the timeout for a data command
259 * @data: data phase for command
260 * @card: the MMC card associated with the data transfer
262 * Computes the data timeout parameters according to the
263 * correct algorithm given the card type.
265 void mmc_set_data_timeout(struct mmc_data *data, const struct mmc_card *card)
270 * SDIO cards only define an upper 1 s limit on access.
272 if (mmc_card_sdio(card)) {
273 data->timeout_ns = 1000000000;
274 data->timeout_clks = 0;
279 * SD cards use a 100 multiplier rather than 10
281 mult = mmc_card_sd(card) ? 100 : 10;
284 * Scale up the multiplier (and therefore the timeout) by
285 * the r2w factor for writes.
287 if (data->flags & MMC_DATA_WRITE)
288 mult <<= card->csd.r2w_factor;
290 data->timeout_ns = card->csd.tacc_ns * mult;
291 data->timeout_clks = card->csd.tacc_clks * mult;
294 * SD cards also have an upper limit on the timeout.
296 if (mmc_card_sd(card)) {
297 unsigned int timeout_us, limit_us;
299 timeout_us = data->timeout_ns / 1000;
300 timeout_us += data->timeout_clks * 1000 /
301 (card->host->ios.clock / 1000);
303 if (data->flags & MMC_DATA_WRITE)
305 * The limit is really 250 ms, but that is
306 * insufficient for some crappy cards.
313 * SDHC cards always use these fixed values.
315 if (timeout_us > limit_us || mmc_card_blockaddr(card)) {
316 data->timeout_ns = limit_us * 1000;
317 data->timeout_clks = 0;
321 * Some cards need very high timeouts if driven in SPI mode.
322 * The worst observed timeout was 900ms after writing a
323 * continuous stream of data until the internal logic
326 if (mmc_host_is_spi(card->host)) {
327 if (data->flags & MMC_DATA_WRITE) {
328 if (data->timeout_ns < 1000000000)
329 data->timeout_ns = 1000000000; /* 1s */
331 if (data->timeout_ns < 100000000)
332 data->timeout_ns = 100000000; /* 100ms */
336 EXPORT_SYMBOL(mmc_set_data_timeout);
339 * mmc_align_data_size - pads a transfer size to a more optimal value
340 * @card: the MMC card associated with the data transfer
341 * @sz: original transfer size
343 * Pads the original data size with a number of extra bytes in
344 * order to avoid controller bugs and/or performance hits
345 * (e.g. some controllers revert to PIO for certain sizes).
347 * Returns the improved size, which might be unmodified.
349 * Note that this function is only relevant when issuing a
350 * single scatter gather entry.
352 unsigned int mmc_align_data_size(struct mmc_card *card, unsigned int sz)
355 * FIXME: We don't have a system for the controller to tell
356 * the core about its problems yet, so for now we just 32-bit
359 sz = ((sz + 3) / 4) * 4;
363 EXPORT_SYMBOL(mmc_align_data_size);
366 * mmc_host_enable - enable a host.
367 * @host: mmc host to enable
369 * Hosts that support power saving can use the 'enable' and 'disable'
370 * methods to exit and enter power saving states. For more information
371 * see comments for struct mmc_host_ops.
373 int mmc_host_enable(struct mmc_host *host)
375 if (!(host->caps & MMC_CAP_DISABLE))
378 if (host->en_dis_recurs)
381 if (host->nesting_cnt++)
384 cancel_delayed_work_sync(&host->disable);
389 if (host->ops->enable) {
392 host->en_dis_recurs = 1;
393 err = host->ops->enable(host);
394 host->en_dis_recurs = 0;
397 pr_debug("%s: enable error %d\n",
398 mmc_hostname(host), err);
405 EXPORT_SYMBOL(mmc_host_enable);
407 static int mmc_host_do_disable(struct mmc_host *host, int lazy)
409 if (host->ops->disable) {
412 host->en_dis_recurs = 1;
413 err = host->ops->disable(host, lazy);
414 host->en_dis_recurs = 0;
417 pr_debug("%s: disable error %d\n",
418 mmc_hostname(host), err);
422 unsigned long delay = msecs_to_jiffies(err);
424 mmc_schedule_delayed_work(&host->disable, delay);
432 * mmc_host_disable - disable a host.
433 * @host: mmc host to disable
435 * Hosts that support power saving can use the 'enable' and 'disable'
436 * methods to exit and enter power saving states. For more information
437 * see comments for struct mmc_host_ops.
439 int mmc_host_disable(struct mmc_host *host)
443 if (!(host->caps & MMC_CAP_DISABLE))
446 if (host->en_dis_recurs)
449 if (--host->nesting_cnt)
455 err = mmc_host_do_disable(host, 0);
458 EXPORT_SYMBOL(mmc_host_disable);
461 * __mmc_claim_host - exclusively claim a host
462 * @host: mmc host to claim
463 * @abort: whether or not the operation should be aborted
465 * Claim a host for a set of operations. If @abort is non null and
466 * dereference a non-zero value then this will return prematurely with
467 * that non-zero value without acquiring the lock. Returns zero
468 * with the lock held otherwise.
470 int __mmc_claim_host(struct mmc_host *host, atomic_t *abort)
472 DECLARE_WAITQUEUE(wait, current);
478 add_wait_queue(&host->wq, &wait);
479 spin_lock_irqsave(&host->lock, flags);
481 set_current_state(TASK_UNINTERRUPTIBLE);
482 stop = abort ? atomic_read(abort) : 0;
483 if (stop || !host->claimed || host->claimer == current)
485 spin_unlock_irqrestore(&host->lock, flags);
487 spin_lock_irqsave(&host->lock, flags);
489 set_current_state(TASK_RUNNING);
492 host->claimer = current;
493 host->claim_cnt += 1;
496 spin_unlock_irqrestore(&host->lock, flags);
497 remove_wait_queue(&host->wq, &wait);
499 mmc_host_enable(host);
503 EXPORT_SYMBOL(__mmc_claim_host);
506 * mmc_try_claim_host - try exclusively to claim a host
507 * @host: mmc host to claim
509 * Returns %1 if the host is claimed, %0 otherwise.
511 int mmc_try_claim_host(struct mmc_host *host)
513 int claimed_host = 0;
516 spin_lock_irqsave(&host->lock, flags);
517 if (!host->claimed || host->claimer == current) {
519 host->claimer = current;
520 host->claim_cnt += 1;
523 spin_unlock_irqrestore(&host->lock, flags);
526 EXPORT_SYMBOL(mmc_try_claim_host);
528 static void mmc_do_release_host(struct mmc_host *host)
532 spin_lock_irqsave(&host->lock, flags);
533 if (--host->claim_cnt) {
534 /* Release for nested claim */
535 spin_unlock_irqrestore(&host->lock, flags);
538 host->claimer = NULL;
539 spin_unlock_irqrestore(&host->lock, flags);
544 void mmc_host_deeper_disable(struct work_struct *work)
546 struct mmc_host *host =
547 container_of(work, struct mmc_host, disable.work);
549 /* If the host is claimed then we do not want to disable it anymore */
550 if (!mmc_try_claim_host(host))
552 mmc_host_do_disable(host, 1);
553 mmc_do_release_host(host);
556 wake_unlock(&mmc_delayed_work_wake_lock);
560 * mmc_host_lazy_disable - lazily disable a host.
561 * @host: mmc host to disable
563 * Hosts that support power saving can use the 'enable' and 'disable'
564 * methods to exit and enter power saving states. For more information
565 * see comments for struct mmc_host_ops.
567 int mmc_host_lazy_disable(struct mmc_host *host)
569 if (!(host->caps & MMC_CAP_DISABLE))
572 if (host->en_dis_recurs)
575 if (--host->nesting_cnt)
581 if (host->disable_delay) {
582 mmc_schedule_delayed_work(&host->disable,
583 msecs_to_jiffies(host->disable_delay));
586 return mmc_host_do_disable(host, 1);
588 EXPORT_SYMBOL(mmc_host_lazy_disable);
591 * mmc_release_host - release a host
592 * @host: mmc host to release
594 * Release a MMC host, allowing others to claim the host
595 * for their operations.
597 void mmc_release_host(struct mmc_host *host)
599 WARN_ON(!host->claimed);
601 mmc_host_lazy_disable(host);
603 mmc_do_release_host(host);
606 EXPORT_SYMBOL(mmc_release_host);
609 * Internal function that does the actual ios call to the host driver,
610 * optionally printing some debug output.
612 static inline void mmc_set_ios(struct mmc_host *host)
614 struct mmc_ios *ios = &host->ios;
616 pr_debug("%s: clock %uHz busmode %u powermode %u cs %u Vdd %u "
617 "width %u timing %u\n",
618 mmc_hostname(host), ios->clock, ios->bus_mode,
619 ios->power_mode, ios->chip_select, ios->vdd,
620 ios->bus_width, ios->timing);
622 host->ops->set_ios(host, ios);
626 * Control chip select pin on a host.
628 void mmc_set_chip_select(struct mmc_host *host, int mode)
630 host->ios.chip_select = mode;
635 * Sets the host clock to the highest possible frequency that
638 void mmc_set_clock(struct mmc_host *host, unsigned int hz)
640 WARN_ON(hz < host->f_min);
642 if (hz > host->f_max)
645 host->ios.clock = hz;
650 * Change the bus mode (open drain/push-pull) of a host.
652 void mmc_set_bus_mode(struct mmc_host *host, unsigned int mode)
654 host->ios.bus_mode = mode;
659 * Change data bus width of a host.
661 void mmc_set_bus_width(struct mmc_host *host, unsigned int width)
663 host->ios.bus_width = width;
668 * mmc_vdd_to_ocrbitnum - Convert a voltage to the OCR bit number
670 * @low_bits: prefer low bits in boundary cases
672 * This function returns the OCR bit number according to the provided @vdd
673 * value. If conversion is not possible a negative errno value returned.
675 * Depending on the @low_bits flag the function prefers low or high OCR bits
676 * on boundary voltages. For example,
677 * with @low_bits = true, 3300 mV translates to ilog2(MMC_VDD_32_33);
678 * with @low_bits = false, 3300 mV translates to ilog2(MMC_VDD_33_34);
680 * Any value in the [1951:1999] range translates to the ilog2(MMC_VDD_20_21).
682 static int mmc_vdd_to_ocrbitnum(int vdd, bool low_bits)
684 const int max_bit = ilog2(MMC_VDD_35_36);
687 if (vdd < 1650 || vdd > 3600)
690 if (vdd >= 1650 && vdd <= 1950)
691 return ilog2(MMC_VDD_165_195);
696 /* Base 2000 mV, step 100 mV, bit's base 8. */
697 bit = (vdd - 2000) / 100 + 8;
704 * mmc_vddrange_to_ocrmask - Convert a voltage range to the OCR mask
705 * @vdd_min: minimum voltage value (mV)
706 * @vdd_max: maximum voltage value (mV)
708 * This function returns the OCR mask bits according to the provided @vdd_min
709 * and @vdd_max values. If conversion is not possible the function returns 0.
711 * Notes wrt boundary cases:
712 * This function sets the OCR bits for all boundary voltages, for example
713 * [3300:3400] range is translated to MMC_VDD_32_33 | MMC_VDD_33_34 |
714 * MMC_VDD_34_35 mask.
716 u32 mmc_vddrange_to_ocrmask(int vdd_min, int vdd_max)
720 if (vdd_max < vdd_min)
723 /* Prefer high bits for the boundary vdd_max values. */
724 vdd_max = mmc_vdd_to_ocrbitnum(vdd_max, false);
728 /* Prefer low bits for the boundary vdd_min values. */
729 vdd_min = mmc_vdd_to_ocrbitnum(vdd_min, true);
733 /* Fill the mask, from max bit to min bit. */
734 while (vdd_max >= vdd_min)
735 mask |= 1 << vdd_max--;
739 EXPORT_SYMBOL(mmc_vddrange_to_ocrmask);
741 #ifdef CONFIG_REGULATOR
744 * mmc_regulator_get_ocrmask - return mask of supported voltages
745 * @supply: regulator to use
747 * This returns either a negative errno, or a mask of voltages that
748 * can be provided to MMC/SD/SDIO devices using the specified voltage
749 * regulator. This would normally be called before registering the
752 int mmc_regulator_get_ocrmask(struct regulator *supply)
758 count = regulator_count_voltages(supply);
762 for (i = 0; i < count; i++) {
766 vdd_uV = regulator_list_voltage(supply, i);
770 vdd_mV = vdd_uV / 1000;
771 result |= mmc_vddrange_to_ocrmask(vdd_mV, vdd_mV);
776 EXPORT_SYMBOL(mmc_regulator_get_ocrmask);
779 * mmc_regulator_set_ocr - set regulator to match host->ios voltage
780 * @vdd_bit: zero for power off, else a bit number (host->ios.vdd)
781 * @supply: regulator to use
783 * Returns zero on success, else negative errno.
785 * MMC host drivers may use this to enable or disable a regulator using
786 * a particular supply voltage. This would normally be called from the
789 int mmc_regulator_set_ocr(struct regulator *supply, unsigned short vdd_bit)
795 enabled = regulator_is_enabled(supply);
803 /* REVISIT mmc_vddrange_to_ocrmask() may have set some
804 * bits this regulator doesn't quite support ... don't
805 * be too picky, most cards and regulators are OK with
806 * a 0.1V range goof (it's a small error percentage).
808 tmp = vdd_bit - ilog2(MMC_VDD_165_195);
810 min_uV = 1650 * 1000;
811 max_uV = 1950 * 1000;
813 min_uV = 1900 * 1000 + tmp * 100 * 1000;
814 max_uV = min_uV + 100 * 1000;
817 /* avoid needless changes to this voltage; the regulator
818 * might not allow this operation
820 voltage = regulator_get_voltage(supply);
823 else if (voltage < min_uV || voltage > max_uV)
824 result = regulator_set_voltage(supply, min_uV, max_uV);
828 if (result == 0 && !enabled)
829 result = regulator_enable(supply);
830 } else if (enabled) {
831 result = regulator_disable(supply);
836 EXPORT_SYMBOL(mmc_regulator_set_ocr);
841 * Mask off any voltages we don't support and select
844 u32 mmc_select_voltage(struct mmc_host *host, u32 ocr)
848 ocr &= host->ocr_avail;
859 pr_warning("%s: host doesn't support card's voltages\n",
868 * Select timing parameters for host.
870 void mmc_set_timing(struct mmc_host *host, unsigned int timing)
872 host->ios.timing = timing;
877 * Apply power to the MMC stack. This is a two-stage process.
878 * First, we enable power to the card without the clock running.
879 * We then wait a bit for the power to stabilise. Finally,
880 * enable the bus drivers and clock to the card.
882 * We must _NOT_ enable the clock prior to power stablising.
884 * If a host does all the power sequencing itself, ignore the
885 * initial MMC_POWER_UP stage.
887 static void mmc_power_up(struct mmc_host *host)
891 /* If ocr is set, we use it */
893 bit = ffs(host->ocr) - 1;
895 bit = fls(host->ocr_avail) - 1;
898 if (mmc_host_is_spi(host)) {
899 host->ios.chip_select = MMC_CS_HIGH;
900 host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
902 host->ios.chip_select = MMC_CS_DONTCARE;
903 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
905 host->ios.power_mode = MMC_POWER_UP;
906 host->ios.bus_width = MMC_BUS_WIDTH_1;
907 host->ios.timing = MMC_TIMING_LEGACY;
911 * This delay should be sufficient to allow the power supply
912 * to reach the minimum voltage.
916 host->ios.clock = host->f_min;
918 host->ios.power_mode = MMC_POWER_ON;
922 * This delay must be at least 74 clock sizes, or 1 ms, or the
923 * time required to reach a stable voltage.
928 static void mmc_power_off(struct mmc_host *host)
932 if (!mmc_host_is_spi(host)) {
933 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
934 host->ios.chip_select = MMC_CS_DONTCARE;
936 host->ios.power_mode = MMC_POWER_OFF;
937 host->ios.bus_width = MMC_BUS_WIDTH_1;
938 host->ios.timing = MMC_TIMING_LEGACY;
943 * Cleanup when the last reference to the bus operator is dropped.
945 static void __mmc_release_bus(struct mmc_host *host)
948 BUG_ON(host->bus_refs);
949 BUG_ON(!host->bus_dead);
951 host->bus_ops = NULL;
955 * Increase reference count of bus operator
957 static inline void mmc_bus_get(struct mmc_host *host)
961 spin_lock_irqsave(&host->lock, flags);
963 spin_unlock_irqrestore(&host->lock, flags);
967 * Decrease reference count of bus operator and free it if
968 * it is the last reference.
970 static inline void mmc_bus_put(struct mmc_host *host)
974 spin_lock_irqsave(&host->lock, flags);
976 if ((host->bus_refs == 0) && host->bus_ops)
977 __mmc_release_bus(host);
978 spin_unlock_irqrestore(&host->lock, flags);
981 int mmc_resume_bus(struct mmc_host *host)
985 if (!mmc_bus_needs_resume(host))
988 printk("%s: Starting deferred resume\n", mmc_hostname(host));
989 spin_lock_irqsave(&host->lock, flags);
990 host->bus_resume_flags &= ~MMC_BUSRESUME_NEEDS_RESUME;
991 host->rescan_disable = 0;
992 spin_unlock_irqrestore(&host->lock, flags);
995 if (host->bus_ops && !host->bus_dead) {
997 BUG_ON(!host->bus_ops->resume);
998 host->bus_ops->resume(host);
1001 if (host->bus_ops->detect && !host->bus_dead)
1002 host->bus_ops->detect(host);
1005 printk("%s: Deferred resume completed\n", mmc_hostname(host));
1009 EXPORT_SYMBOL(mmc_resume_bus);
1012 * Assign a mmc bus handler to a host. Only one bus handler may control a
1013 * host at any given time.
1015 void mmc_attach_bus(struct mmc_host *host, const struct mmc_bus_ops *ops)
1017 unsigned long flags;
1022 WARN_ON(!host->claimed);
1024 spin_lock_irqsave(&host->lock, flags);
1026 BUG_ON(host->bus_ops);
1027 BUG_ON(host->bus_refs);
1029 host->bus_ops = ops;
1033 spin_unlock_irqrestore(&host->lock, flags);
1037 * Remove the current bus handler from a host. Assumes that there are
1038 * no interesting cards left, so the bus is powered down.
1040 void mmc_detach_bus(struct mmc_host *host)
1042 unsigned long flags;
1046 WARN_ON(!host->claimed);
1047 WARN_ON(!host->bus_ops);
1049 spin_lock_irqsave(&host->lock, flags);
1053 spin_unlock_irqrestore(&host->lock, flags);
1055 mmc_power_off(host);
1061 * mmc_detect_change - process change of state on a MMC socket
1062 * @host: host which changed state.
1063 * @delay: optional delay to wait before detection (jiffies)
1065 * MMC drivers should call this when they detect a card has been
1066 * inserted or removed. The MMC layer will confirm that any
1067 * present card is still functional, and initialize any newly
1070 void mmc_detect_change(struct mmc_host *host, unsigned long delay)
1072 #ifdef CONFIG_MMC_DEBUG
1073 unsigned long flags;
1074 spin_lock_irqsave(&host->lock, flags);
1075 WARN_ON(host->removed);
1076 spin_unlock_irqrestore(&host->lock, flags);
1079 mmc_schedule_delayed_work(&host->detect, delay);
1082 EXPORT_SYMBOL(mmc_detect_change);
1084 void mmc_init_erase(struct mmc_card *card)
1088 if (is_power_of_2(card->erase_size))
1089 card->erase_shift = ffs(card->erase_size) - 1;
1091 card->erase_shift = 0;
1094 * It is possible to erase an arbitrarily large area of an SD or MMC
1095 * card. That is not desirable because it can take a long time
1096 * (minutes) potentially delaying more important I/O, and also the
1097 * timeout calculations become increasingly hugely over-estimated.
1098 * Consequently, 'pref_erase' is defined as a guide to limit erases
1099 * to that size and alignment.
1101 * For SD cards that define Allocation Unit size, limit erases to one
1102 * Allocation Unit at a time. For MMC cards that define High Capacity
1103 * Erase Size, whether it is switched on or not, limit to that size.
1104 * Otherwise just have a stab at a good value. For modern cards it
1105 * will end up being 4MiB. Note that if the value is too small, it
1106 * can end up taking longer to erase.
1108 if (mmc_card_sd(card) && card->ssr.au) {
1109 card->pref_erase = card->ssr.au;
1110 card->erase_shift = ffs(card->ssr.au) - 1;
1111 } else if (card->ext_csd.hc_erase_size) {
1112 card->pref_erase = card->ext_csd.hc_erase_size;
1114 sz = (card->csd.capacity << (card->csd.read_blkbits - 9)) >> 11;
1116 card->pref_erase = 512 * 1024 / 512;
1118 card->pref_erase = 1024 * 1024 / 512;
1120 card->pref_erase = 2 * 1024 * 1024 / 512;
1122 card->pref_erase = 4 * 1024 * 1024 / 512;
1123 if (card->pref_erase < card->erase_size)
1124 card->pref_erase = card->erase_size;
1126 sz = card->pref_erase % card->erase_size;
1128 card->pref_erase += card->erase_size - sz;
1133 static void mmc_set_mmc_erase_timeout(struct mmc_card *card,
1134 struct mmc_command *cmd,
1135 unsigned int arg, unsigned int qty)
1137 unsigned int erase_timeout;
1139 if (card->ext_csd.erase_group_def & 1) {
1140 /* High Capacity Erase Group Size uses HC timeouts */
1141 if (arg == MMC_TRIM_ARG)
1142 erase_timeout = card->ext_csd.trim_timeout;
1144 erase_timeout = card->ext_csd.hc_erase_timeout;
1146 /* CSD Erase Group Size uses write timeout */
1147 unsigned int mult = (10 << card->csd.r2w_factor);
1148 unsigned int timeout_clks = card->csd.tacc_clks * mult;
1149 unsigned int timeout_us;
1151 /* Avoid overflow: e.g. tacc_ns=80000000 mult=1280 */
1152 if (card->csd.tacc_ns < 1000000)
1153 timeout_us = (card->csd.tacc_ns * mult) / 1000;
1155 timeout_us = (card->csd.tacc_ns / 1000) * mult;
1158 * ios.clock is only a target. The real clock rate might be
1159 * less but not that much less, so fudge it by multiplying by 2.
1162 timeout_us += (timeout_clks * 1000) /
1163 (card->host->ios.clock / 1000);
1165 erase_timeout = timeout_us / 1000;
1168 * Theoretically, the calculation could underflow so round up
1169 * to 1ms in that case.
1175 /* Multiplier for secure operations */
1176 if (arg & MMC_SECURE_ARGS) {
1177 if (arg == MMC_SECURE_ERASE_ARG)
1178 erase_timeout *= card->ext_csd.sec_erase_mult;
1180 erase_timeout *= card->ext_csd.sec_trim_mult;
1183 erase_timeout *= qty;
1186 * Ensure at least a 1 second timeout for SPI as per
1187 * 'mmc_set_data_timeout()'
1189 if (mmc_host_is_spi(card->host) && erase_timeout < 1000)
1190 erase_timeout = 1000;
1192 cmd->erase_timeout = erase_timeout;
1195 static void mmc_set_sd_erase_timeout(struct mmc_card *card,
1196 struct mmc_command *cmd, unsigned int arg,
1199 if (card->ssr.erase_timeout) {
1200 /* Erase timeout specified in SD Status Register (SSR) */
1201 cmd->erase_timeout = card->ssr.erase_timeout * qty +
1202 card->ssr.erase_offset;
1205 * Erase timeout not specified in SD Status Register (SSR) so
1206 * use 250ms per write block.
1208 cmd->erase_timeout = 250 * qty;
1211 /* Must not be less than 1 second */
1212 if (cmd->erase_timeout < 1000)
1213 cmd->erase_timeout = 1000;
1216 static void mmc_set_erase_timeout(struct mmc_card *card,
1217 struct mmc_command *cmd, unsigned int arg,
1220 if (mmc_card_sd(card))
1221 mmc_set_sd_erase_timeout(card, cmd, arg, qty);
1223 mmc_set_mmc_erase_timeout(card, cmd, arg, qty);
1226 static int mmc_do_erase(struct mmc_card *card, unsigned int from,
1227 unsigned int to, unsigned int arg)
1229 struct mmc_command cmd;
1230 unsigned int qty = 0;
1234 * qty is used to calculate the erase timeout which depends on how many
1235 * erase groups (or allocation units in SD terminology) are affected.
1236 * We count erasing part of an erase group as one erase group.
1237 * For SD, the allocation units are always a power of 2. For MMC, the
1238 * erase group size is almost certainly also power of 2, but it does not
1239 * seem to insist on that in the JEDEC standard, so we fall back to
1240 * division in that case. SD may not specify an allocation unit size,
1241 * in which case the timeout is based on the number of write blocks.
1243 * Note that the timeout for secure trim 2 will only be correct if the
1244 * number of erase groups specified is the same as the total of all
1245 * preceding secure trim 1 commands. Since the power may have been
1246 * lost since the secure trim 1 commands occurred, it is generally
1247 * impossible to calculate the secure trim 2 timeout correctly.
1249 if (card->erase_shift)
1250 qty += ((to >> card->erase_shift) -
1251 (from >> card->erase_shift)) + 1;
1252 else if (mmc_card_sd(card))
1253 qty += to - from + 1;
1255 qty += ((to / card->erase_size) -
1256 (from / card->erase_size)) + 1;
1258 if (!mmc_card_blockaddr(card)) {
1263 memset(&cmd, 0, sizeof(struct mmc_command));
1264 if (mmc_card_sd(card))
1265 cmd.opcode = SD_ERASE_WR_BLK_START;
1267 cmd.opcode = MMC_ERASE_GROUP_START;
1269 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
1270 err = mmc_wait_for_cmd(card->host, &cmd, 0);
1272 printk(KERN_ERR "mmc_erase: group start error %d, "
1273 "status %#x\n", err, cmd.resp[0]);
1278 memset(&cmd, 0, sizeof(struct mmc_command));
1279 if (mmc_card_sd(card))
1280 cmd.opcode = SD_ERASE_WR_BLK_END;
1282 cmd.opcode = MMC_ERASE_GROUP_END;
1284 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
1285 err = mmc_wait_for_cmd(card->host, &cmd, 0);
1287 printk(KERN_ERR "mmc_erase: group end error %d, status %#x\n",
1293 memset(&cmd, 0, sizeof(struct mmc_command));
1294 cmd.opcode = MMC_ERASE;
1296 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1297 mmc_set_erase_timeout(card, &cmd, arg, qty);
1298 err = mmc_wait_for_cmd(card->host, &cmd, 0);
1300 printk(KERN_ERR "mmc_erase: erase error %d, status %#x\n",
1306 if (mmc_host_is_spi(card->host))
1310 memset(&cmd, 0, sizeof(struct mmc_command));
1311 cmd.opcode = MMC_SEND_STATUS;
1312 cmd.arg = card->rca << 16;
1313 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
1314 /* Do not retry else we can't see errors */
1315 err = mmc_wait_for_cmd(card->host, &cmd, 0);
1316 if (err || (cmd.resp[0] & 0xFDF92000)) {
1317 printk(KERN_ERR "error %d requesting status %#x\n",
1322 } while (!(cmd.resp[0] & R1_READY_FOR_DATA) ||
1323 R1_CURRENT_STATE(cmd.resp[0]) == 7);
1329 * mmc_erase - erase sectors.
1330 * @card: card to erase
1331 * @from: first sector to erase
1332 * @nr: number of sectors to erase
1333 * @arg: erase command argument (SD supports only %MMC_ERASE_ARG)
1335 * Caller must claim host before calling this function.
1337 int mmc_erase(struct mmc_card *card, unsigned int from, unsigned int nr,
1340 unsigned int rem, to = from + nr;
1342 if (!(card->host->caps & MMC_CAP_ERASE) ||
1343 !(card->csd.cmdclass & CCC_ERASE))
1346 if (!card->erase_size)
1349 if (mmc_card_sd(card) && arg != MMC_ERASE_ARG)
1352 if ((arg & MMC_SECURE_ARGS) &&
1353 !(card->ext_csd.sec_feature_support & EXT_CSD_SEC_ER_EN))
1356 if ((arg & MMC_TRIM_ARGS) &&
1357 !(card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN))
1360 if (arg == MMC_SECURE_ERASE_ARG) {
1361 if (from % card->erase_size || nr % card->erase_size)
1365 if (arg == MMC_ERASE_ARG) {
1366 rem = from % card->erase_size;
1368 rem = card->erase_size - rem;
1375 rem = nr % card->erase_size;
1388 /* 'from' and 'to' are inclusive */
1391 return mmc_do_erase(card, from, to, arg);
1393 EXPORT_SYMBOL(mmc_erase);
1395 int mmc_can_erase(struct mmc_card *card)
1397 if ((card->host->caps & MMC_CAP_ERASE) &&
1398 (card->csd.cmdclass & CCC_ERASE) && card->erase_size)
1402 EXPORT_SYMBOL(mmc_can_erase);
1404 int mmc_can_trim(struct mmc_card *card)
1406 if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN)
1410 EXPORT_SYMBOL(mmc_can_trim);
1412 int mmc_can_secure_erase_trim(struct mmc_card *card)
1414 if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_ER_EN)
1418 EXPORT_SYMBOL(mmc_can_secure_erase_trim);
1420 int mmc_erase_group_aligned(struct mmc_card *card, unsigned int from,
1423 if (!card->erase_size)
1425 if (from % card->erase_size || nr % card->erase_size)
1429 EXPORT_SYMBOL(mmc_erase_group_aligned);
1431 void mmc_rescan(struct work_struct *work)
1433 struct mmc_host *host =
1434 container_of(work, struct mmc_host, detect.work);
1437 unsigned long flags;
1438 int extend_wakelock = 0;
1440 spin_lock_irqsave(&host->lock, flags);
1442 if (host->rescan_disable) {
1443 spin_unlock_irqrestore(&host->lock, flags);
1447 spin_unlock_irqrestore(&host->lock, flags);
1452 /* if there is a card registered, check whether it is still present */
1453 if ((host->bus_ops != NULL) && host->bus_ops->detect && !host->bus_dead)
1454 host->bus_ops->detect(host);
1456 /* If the card was removed the bus will be marked
1457 * as dead - extend the wakelock so userspace
1460 extend_wakelock = 1;
1467 /* if there still is a card present, stop here */
1468 if (host->bus_ops != NULL) {
1473 /* detect a newly inserted card */
1476 * Only we can add a new handler, so it's safe to
1477 * release the lock here.
1481 if (host->ops->get_cd && host->ops->get_cd(host) == 0)
1484 mmc_claim_host(host);
1490 mmc_send_if_cond(host, host->ocr_avail);
1493 * First we search for SDIO...
1495 err = mmc_send_io_op_cond(host, 0, &ocr);
1497 if (mmc_attach_sdio(host, ocr)) {
1498 mmc_claim_host(host);
1499 /* try SDMEM (but not MMC) even if SDIO is broken */
1500 if (mmc_send_app_op_cond(host, 0, &ocr))
1503 if (mmc_attach_sd(host, ocr))
1504 mmc_power_off(host);
1505 extend_wakelock = 1;
1511 * ...then normal SD...
1513 err = mmc_send_app_op_cond(host, 0, &ocr);
1515 if (mmc_attach_sd(host, ocr))
1516 mmc_power_off(host);
1517 extend_wakelock = 1;
1522 * ...and finally MMC.
1524 err = mmc_send_op_cond(host, 0, &ocr);
1526 if (mmc_attach_mmc(host, ocr))
1527 mmc_power_off(host);
1528 extend_wakelock = 1;
1533 mmc_release_host(host);
1534 mmc_power_off(host);
1537 if (extend_wakelock)
1538 wake_lock_timeout(&mmc_delayed_work_wake_lock, HZ / 2);
1540 wake_unlock(&mmc_delayed_work_wake_lock);
1542 if (host->caps & MMC_CAP_NEEDS_POLL)
1543 mmc_schedule_delayed_work(&host->detect, HZ);
1546 void mmc_start_host(struct mmc_host *host)
1548 mmc_power_off(host);
1549 mmc_detect_change(host, 0);
1552 void mmc_stop_host(struct mmc_host *host)
1554 #ifdef CONFIG_MMC_DEBUG
1555 unsigned long flags;
1556 spin_lock_irqsave(&host->lock, flags);
1558 spin_unlock_irqrestore(&host->lock, flags);
1561 if (host->caps & MMC_CAP_DISABLE)
1562 cancel_delayed_work(&host->disable);
1563 cancel_delayed_work(&host->detect);
1564 mmc_flush_scheduled_work();
1566 /* clear pm flags now and let card drivers set them as needed */
1570 if (host->bus_ops && !host->bus_dead) {
1571 if (host->bus_ops->remove)
1572 host->bus_ops->remove(host);
1574 mmc_claim_host(host);
1575 mmc_detach_bus(host);
1576 mmc_release_host(host);
1584 mmc_power_off(host);
1587 void mmc_power_save_host(struct mmc_host *host)
1591 if (!host->bus_ops || host->bus_dead || !host->bus_ops->power_restore) {
1596 if (host->bus_ops->power_save)
1597 host->bus_ops->power_save(host);
1601 mmc_power_off(host);
1603 EXPORT_SYMBOL(mmc_power_save_host);
1605 void mmc_power_restore_host(struct mmc_host *host)
1609 if (!host->bus_ops || host->bus_dead || !host->bus_ops->power_restore) {
1615 host->bus_ops->power_restore(host);
1619 EXPORT_SYMBOL(mmc_power_restore_host);
1621 int mmc_card_awake(struct mmc_host *host)
1627 if (host->bus_ops && !host->bus_dead && host->bus_ops->awake)
1628 err = host->bus_ops->awake(host);
1634 EXPORT_SYMBOL(mmc_card_awake);
1636 int mmc_card_sleep(struct mmc_host *host)
1642 if (host->bus_ops && !host->bus_dead && host->bus_ops->awake)
1643 err = host->bus_ops->sleep(host);
1649 EXPORT_SYMBOL(mmc_card_sleep);
1651 int mmc_card_can_sleep(struct mmc_host *host)
1653 struct mmc_card *card = host->card;
1655 if (card && mmc_card_mmc(card) && card->ext_csd.rev >= 3)
1659 EXPORT_SYMBOL(mmc_card_can_sleep);
1664 * mmc_suspend_host - suspend a host
1667 int mmc_suspend_host(struct mmc_host *host)
1671 if (mmc_bus_needs_resume(host))
1674 if (host->caps & MMC_CAP_DISABLE)
1675 cancel_delayed_work(&host->disable);
1676 cancel_delayed_work(&host->detect);
1677 mmc_flush_scheduled_work();
1680 if (host->bus_ops && !host->bus_dead) {
1681 if (host->bus_ops->suspend)
1682 err = host->bus_ops->suspend(host);
1683 if (err == -ENOSYS || !host->bus_ops->resume) {
1685 * We simply "remove" the card in this case.
1686 * It will be redetected on resume.
1688 if (host->bus_ops->remove)
1689 host->bus_ops->remove(host);
1690 mmc_claim_host(host);
1691 mmc_detach_bus(host);
1692 mmc_release_host(host);
1699 if (!err && !(host->pm_flags & MMC_PM_KEEP_POWER))
1700 mmc_power_off(host);
1705 EXPORT_SYMBOL(mmc_suspend_host);
1708 * mmc_resume_host - resume a previously suspended host
1711 int mmc_resume_host(struct mmc_host *host)
1716 if (mmc_bus_manual_resume(host)) {
1717 host->bus_resume_flags |= MMC_BUSRESUME_NEEDS_RESUME;
1722 if (host->bus_ops && !host->bus_dead) {
1723 if (!(host->pm_flags & MMC_PM_KEEP_POWER)) {
1725 mmc_select_voltage(host, host->ocr);
1727 BUG_ON(!host->bus_ops->resume);
1728 err = host->bus_ops->resume(host);
1730 printk(KERN_WARNING "%s: error %d during resume "
1731 "(card was removed?)\n",
1732 mmc_hostname(host), err);
1740 EXPORT_SYMBOL(mmc_resume_host);
1742 /* Do the card removal on suspend if card is assumed removeable
1743 * Do that in pm notifier while userspace isn't yet frozen, so we will be able
1746 int mmc_pm_notify(struct notifier_block *notify_block,
1747 unsigned long mode, void *unused)
1749 struct mmc_host *host = container_of(
1750 notify_block, struct mmc_host, pm_notify);
1751 unsigned long flags;
1755 case PM_HIBERNATION_PREPARE:
1756 case PM_SUSPEND_PREPARE:
1758 spin_lock_irqsave(&host->lock, flags);
1759 if (mmc_bus_needs_resume(host)) {
1760 spin_unlock_irqrestore(&host->lock, flags);
1763 host->rescan_disable = 1;
1764 spin_unlock_irqrestore(&host->lock, flags);
1765 cancel_delayed_work_sync(&host->detect);
1767 if (!host->bus_ops || host->bus_ops->suspend)
1770 mmc_claim_host(host);
1772 if (host->bus_ops->remove)
1773 host->bus_ops->remove(host);
1775 mmc_detach_bus(host);
1776 mmc_release_host(host);
1780 case PM_POST_SUSPEND:
1781 case PM_POST_HIBERNATION:
1783 spin_lock_irqsave(&host->lock, flags);
1784 if (mmc_bus_manual_resume(host)) {
1785 spin_unlock_irqrestore(&host->lock, flags);
1788 host->rescan_disable = 0;
1789 spin_unlock_irqrestore(&host->lock, flags);
1790 mmc_detect_change(host, 0);
1798 #ifdef CONFIG_MMC_EMBEDDED_SDIO
1799 void mmc_set_embedded_sdio_data(struct mmc_host *host,
1800 struct sdio_cis *cis,
1801 struct sdio_cccr *cccr,
1802 struct sdio_embedded_func *funcs,
1805 host->embedded_sdio_data.cis = cis;
1806 host->embedded_sdio_data.cccr = cccr;
1807 host->embedded_sdio_data.funcs = funcs;
1808 host->embedded_sdio_data.num_funcs = num_funcs;
1811 EXPORT_SYMBOL(mmc_set_embedded_sdio_data);
1814 static int __init mmc_init(void)
1818 wake_lock_init(&mmc_delayed_work_wake_lock, WAKE_LOCK_SUSPEND, "mmc_delayed_work");
1820 workqueue = create_singlethread_workqueue("kmmcd");
1824 ret = mmc_register_bus();
1826 goto destroy_workqueue;
1828 ret = mmc_register_host_class();
1830 goto unregister_bus;
1832 ret = sdio_register_bus();
1834 goto unregister_host_class;
1838 unregister_host_class:
1839 mmc_unregister_host_class();
1841 mmc_unregister_bus();
1843 destroy_workqueue(workqueue);
1848 static void __exit mmc_exit(void)
1850 sdio_unregister_bus();
1851 mmc_unregister_host_class();
1852 mmc_unregister_bus();
1853 destroy_workqueue(workqueue);
1854 wake_lock_destroy(&mmc_delayed_work_wake_lock);
1857 subsys_initcall(mmc_init);
1858 module_exit(mmc_exit);
1860 MODULE_LICENSE("GPL");