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/pm_runtime.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;
44 * Enabling software CRCs on the data blocks can be a significant (30%)
45 * performance cost, and for other reasons may not always be desired.
46 * So we allow it it to be disabled.
49 module_param(use_spi_crc, bool, 0);
52 * We normally treat cards as removed during suspend if they are not
53 * known to be on a non-removable bus, to avoid the risk of writing
54 * back data to a different card after resume. Allow this to be
55 * overridden if necessary.
57 #ifdef CONFIG_MMC_UNSAFE_RESUME
58 int mmc_assume_removable;
60 int mmc_assume_removable = 1;
62 EXPORT_SYMBOL(mmc_assume_removable);
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 return queue_delayed_work(workqueue, work, delay);
78 * Internal function. Flush all scheduled work from the MMC work queue.
80 static void mmc_flush_scheduled_work(void)
82 flush_workqueue(workqueue);
86 * mmc_request_done - finish processing an MMC request
87 * @host: MMC host which completed request
88 * @mrq: MMC request which request
90 * MMC drivers should call this function when they have completed
91 * their processing of a request.
93 void mmc_request_done(struct mmc_host *host, struct mmc_request *mrq)
95 struct mmc_command *cmd = mrq->cmd;
98 if (err && cmd->retries && mmc_host_is_spi(host)) {
99 if (cmd->resp[0] & R1_SPI_ILLEGAL_COMMAND)
103 if (err && cmd->retries) {
104 pr_debug("%s: req failed (CMD%u): %d, retrying...\n",
105 mmc_hostname(host), cmd->opcode, err);
109 host->ops->request(host, mrq);
111 led_trigger_event(host->led, LED_OFF);
113 pr_debug("%s: req done (CMD%u): %d: %08x %08x %08x %08x\n",
114 mmc_hostname(host), cmd->opcode, err,
115 cmd->resp[0], cmd->resp[1],
116 cmd->resp[2], cmd->resp[3]);
119 pr_debug("%s: %d bytes transferred: %d\n",
121 mrq->data->bytes_xfered, mrq->data->error);
125 pr_debug("%s: (CMD%u): %d: %08x %08x %08x %08x\n",
126 mmc_hostname(host), mrq->stop->opcode,
128 mrq->stop->resp[0], mrq->stop->resp[1],
129 mrq->stop->resp[2], mrq->stop->resp[3]);
135 mmc_host_clk_gate(host);
139 EXPORT_SYMBOL(mmc_request_done);
142 mmc_start_request(struct mmc_host *host, struct mmc_request *mrq)
144 #ifdef CONFIG_MMC_DEBUG
146 struct scatterlist *sg;
149 pr_debug("%s: starting CMD%u arg %08x flags %08x\n",
150 mmc_hostname(host), mrq->cmd->opcode,
151 mrq->cmd->arg, mrq->cmd->flags);
154 pr_debug("%s: blksz %d blocks %d flags %08x "
155 "tsac %d ms nsac %d\n",
156 mmc_hostname(host), mrq->data->blksz,
157 mrq->data->blocks, mrq->data->flags,
158 mrq->data->timeout_ns / 1000000,
159 mrq->data->timeout_clks);
163 pr_debug("%s: CMD%u arg %08x flags %08x\n",
164 mmc_hostname(host), mrq->stop->opcode,
165 mrq->stop->arg, mrq->stop->flags);
168 WARN_ON(!host->claimed);
173 BUG_ON(mrq->data->blksz > host->max_blk_size);
174 BUG_ON(mrq->data->blocks > host->max_blk_count);
175 BUG_ON(mrq->data->blocks * mrq->data->blksz >
178 #ifdef CONFIG_MMC_DEBUG
180 for_each_sg(mrq->data->sg, sg, mrq->data->sg_len, i)
182 BUG_ON(sz != mrq->data->blocks * mrq->data->blksz);
185 mrq->cmd->data = mrq->data;
186 mrq->data->error = 0;
187 mrq->data->mrq = mrq;
189 mrq->data->stop = mrq->stop;
190 mrq->stop->error = 0;
191 mrq->stop->mrq = mrq;
194 mmc_host_clk_ungate(host);
195 led_trigger_event(host->led, LED_FULL);
196 host->ops->request(host, mrq);
199 static void mmc_wait_done(struct mmc_request *mrq)
201 complete(mrq->done_data);
205 * mmc_wait_for_req - start a request and wait for completion
206 * @host: MMC host to start command
207 * @mrq: MMC request to start
209 * Start a new MMC custom command request for a host, and wait
210 * for the command to complete. Does not attempt to parse the
213 void mmc_wait_for_req(struct mmc_host *host, struct mmc_request *mrq)
215 DECLARE_COMPLETION_ONSTACK(complete);
217 mrq->done_data = &complete;
218 mrq->done = mmc_wait_done;
220 mmc_start_request(host, mrq);
222 wait_for_completion(&complete);
225 EXPORT_SYMBOL(mmc_wait_for_req);
228 * mmc_wait_for_cmd - start a command and wait for completion
229 * @host: MMC host to start command
230 * @cmd: MMC command to start
231 * @retries: maximum number of retries
233 * Start a new MMC command for a host, and wait for the command
234 * to complete. Return any error that occurred while the command
235 * was executing. Do not attempt to parse the response.
237 int mmc_wait_for_cmd(struct mmc_host *host, struct mmc_command *cmd, int retries)
239 struct mmc_request mrq;
241 WARN_ON(!host->claimed);
243 memset(&mrq, 0, sizeof(struct mmc_request));
245 memset(cmd->resp, 0, sizeof(cmd->resp));
246 cmd->retries = retries;
251 mmc_wait_for_req(host, &mrq);
256 EXPORT_SYMBOL(mmc_wait_for_cmd);
259 * mmc_set_data_timeout - set the timeout for a data command
260 * @data: data phase for command
261 * @card: the MMC card associated with the data transfer
263 * Computes the data timeout parameters according to the
264 * correct algorithm given the card type.
266 void mmc_set_data_timeout(struct mmc_data *data, const struct mmc_card *card)
271 * SDIO cards only define an upper 1 s limit on access.
273 if (mmc_card_sdio(card)) {
274 data->timeout_ns = 1000000000;
275 data->timeout_clks = 0;
280 * SD cards use a 100 multiplier rather than 10
282 mult = mmc_card_sd(card) ? 100 : 10;
285 * Scale up the multiplier (and therefore the timeout) by
286 * the r2w factor for writes.
288 if (data->flags & MMC_DATA_WRITE)
289 mult <<= card->csd.r2w_factor;
291 data->timeout_ns = card->csd.tacc_ns * mult;
292 data->timeout_clks = card->csd.tacc_clks * mult;
295 * SD cards also have an upper limit on the timeout.
297 if (mmc_card_sd(card)) {
298 unsigned int timeout_us, limit_us;
300 timeout_us = data->timeout_ns / 1000;
301 if (mmc_host_clk_rate(card->host))
302 timeout_us += data->timeout_clks * 1000 /
303 (mmc_host_clk_rate(card->host) / 1000);
305 if (data->flags & MMC_DATA_WRITE)
307 * The limit is really 250 ms, but that is
308 * insufficient for some crappy cards.
315 * SDHC cards always use these fixed values.
317 if (timeout_us > limit_us || mmc_card_blockaddr(card)) {
318 data->timeout_ns = limit_us * 1000;
319 data->timeout_clks = 0;
323 * Some cards need very high timeouts if driven in SPI mode.
324 * The worst observed timeout was 900ms after writing a
325 * continuous stream of data until the internal logic
328 if (mmc_host_is_spi(card->host)) {
329 if (data->flags & MMC_DATA_WRITE) {
330 if (data->timeout_ns < 1000000000)
331 data->timeout_ns = 1000000000; /* 1s */
333 if (data->timeout_ns < 100000000)
334 data->timeout_ns = 100000000; /* 100ms */
338 EXPORT_SYMBOL(mmc_set_data_timeout);
341 * mmc_align_data_size - pads a transfer size to a more optimal value
342 * @card: the MMC card associated with the data transfer
343 * @sz: original transfer size
345 * Pads the original data size with a number of extra bytes in
346 * order to avoid controller bugs and/or performance hits
347 * (e.g. some controllers revert to PIO for certain sizes).
349 * Returns the improved size, which might be unmodified.
351 * Note that this function is only relevant when issuing a
352 * single scatter gather entry.
354 unsigned int mmc_align_data_size(struct mmc_card *card, unsigned int sz)
357 * FIXME: We don't have a system for the controller to tell
358 * the core about its problems yet, so for now we just 32-bit
361 sz = ((sz + 3) / 4) * 4;
365 EXPORT_SYMBOL(mmc_align_data_size);
368 * mmc_host_enable - enable a host.
369 * @host: mmc host to enable
371 * Hosts that support power saving can use the 'enable' and 'disable'
372 * methods to exit and enter power saving states. For more information
373 * see comments for struct mmc_host_ops.
375 int mmc_host_enable(struct mmc_host *host)
377 if (!(host->caps & MMC_CAP_DISABLE))
380 if (host->en_dis_recurs)
383 if (host->nesting_cnt++)
386 cancel_delayed_work_sync(&host->disable);
391 if (host->ops->enable) {
394 host->en_dis_recurs = 1;
395 err = host->ops->enable(host);
396 host->en_dis_recurs = 0;
399 pr_debug("%s: enable error %d\n",
400 mmc_hostname(host), err);
407 EXPORT_SYMBOL(mmc_host_enable);
409 static int mmc_host_do_disable(struct mmc_host *host, int lazy)
411 if (host->ops->disable) {
414 host->en_dis_recurs = 1;
415 err = host->ops->disable(host, lazy);
416 host->en_dis_recurs = 0;
419 pr_debug("%s: disable error %d\n",
420 mmc_hostname(host), err);
424 unsigned long delay = msecs_to_jiffies(err);
426 mmc_schedule_delayed_work(&host->disable, delay);
434 * mmc_host_disable - disable a host.
435 * @host: mmc host to disable
437 * Hosts that support power saving can use the 'enable' and 'disable'
438 * methods to exit and enter power saving states. For more information
439 * see comments for struct mmc_host_ops.
441 int mmc_host_disable(struct mmc_host *host)
445 if (!(host->caps & MMC_CAP_DISABLE))
448 if (host->en_dis_recurs)
451 if (--host->nesting_cnt)
457 err = mmc_host_do_disable(host, 0);
460 EXPORT_SYMBOL(mmc_host_disable);
463 * __mmc_claim_host - exclusively claim a host
464 * @host: mmc host to claim
465 * @abort: whether or not the operation should be aborted
467 * Claim a host for a set of operations. If @abort is non null and
468 * dereference a non-zero value then this will return prematurely with
469 * that non-zero value without acquiring the lock. Returns zero
470 * with the lock held otherwise.
472 int __mmc_claim_host(struct mmc_host *host, atomic_t *abort)
474 DECLARE_WAITQUEUE(wait, current);
480 add_wait_queue(&host->wq, &wait);
481 spin_lock_irqsave(&host->lock, flags);
483 set_current_state(TASK_UNINTERRUPTIBLE);
484 stop = abort ? atomic_read(abort) : 0;
485 if (stop || !host->claimed || host->claimer == current)
487 spin_unlock_irqrestore(&host->lock, flags);
489 spin_lock_irqsave(&host->lock, flags);
491 set_current_state(TASK_RUNNING);
494 host->claimer = current;
495 host->claim_cnt += 1;
498 spin_unlock_irqrestore(&host->lock, flags);
499 remove_wait_queue(&host->wq, &wait);
501 mmc_host_enable(host);
505 EXPORT_SYMBOL(__mmc_claim_host);
508 * mmc_try_claim_host - try exclusively to claim a host
509 * @host: mmc host to claim
511 * Returns %1 if the host is claimed, %0 otherwise.
513 int mmc_try_claim_host(struct mmc_host *host)
515 int claimed_host = 0;
518 spin_lock_irqsave(&host->lock, flags);
519 if (!host->claimed || host->claimer == current) {
521 host->claimer = current;
522 host->claim_cnt += 1;
525 spin_unlock_irqrestore(&host->lock, flags);
528 EXPORT_SYMBOL(mmc_try_claim_host);
531 * mmc_do_release_host - release a claimed host
532 * @host: mmc host to release
534 * If you successfully claimed a host, this function will
537 void mmc_do_release_host(struct mmc_host *host)
541 spin_lock_irqsave(&host->lock, flags);
542 if (--host->claim_cnt) {
543 /* Release for nested claim */
544 spin_unlock_irqrestore(&host->lock, flags);
547 host->claimer = NULL;
548 spin_unlock_irqrestore(&host->lock, flags);
552 EXPORT_SYMBOL(mmc_do_release_host);
554 void mmc_host_deeper_disable(struct work_struct *work)
556 struct mmc_host *host =
557 container_of(work, struct mmc_host, disable.work);
559 /* If the host is claimed then we do not want to disable it anymore */
560 if (!mmc_try_claim_host(host))
562 mmc_host_do_disable(host, 1);
563 mmc_do_release_host(host);
567 * mmc_host_lazy_disable - lazily disable a host.
568 * @host: mmc host to disable
570 * Hosts that support power saving can use the 'enable' and 'disable'
571 * methods to exit and enter power saving states. For more information
572 * see comments for struct mmc_host_ops.
574 int mmc_host_lazy_disable(struct mmc_host *host)
576 if (!(host->caps & MMC_CAP_DISABLE))
579 if (host->en_dis_recurs)
582 if (--host->nesting_cnt)
588 if (host->disable_delay) {
589 mmc_schedule_delayed_work(&host->disable,
590 msecs_to_jiffies(host->disable_delay));
593 return mmc_host_do_disable(host, 1);
595 EXPORT_SYMBOL(mmc_host_lazy_disable);
598 * mmc_release_host - release a host
599 * @host: mmc host to release
601 * Release a MMC host, allowing others to claim the host
602 * for their operations.
604 void mmc_release_host(struct mmc_host *host)
606 WARN_ON(!host->claimed);
608 mmc_host_lazy_disable(host);
610 mmc_do_release_host(host);
613 EXPORT_SYMBOL(mmc_release_host);
616 * Internal function that does the actual ios call to the host driver,
617 * optionally printing some debug output.
619 static inline void mmc_set_ios(struct mmc_host *host)
621 struct mmc_ios *ios = &host->ios;
623 pr_debug("%s: clock %uHz busmode %u powermode %u cs %u Vdd %u "
624 "width %u timing %u\n",
625 mmc_hostname(host), ios->clock, ios->bus_mode,
626 ios->power_mode, ios->chip_select, ios->vdd,
627 ios->bus_width, ios->timing);
630 mmc_set_ungated(host);
631 host->ops->set_ios(host, ios);
635 * Control chip select pin on a host.
637 void mmc_set_chip_select(struct mmc_host *host, int mode)
639 host->ios.chip_select = mode;
644 * Sets the host clock to the highest possible frequency that
647 void mmc_set_clock(struct mmc_host *host, unsigned int hz)
649 WARN_ON(hz < host->f_min);
651 if (hz > host->f_max)
654 host->ios.clock = hz;
658 #ifdef CONFIG_MMC_CLKGATE
660 * This gates the clock by setting it to 0 Hz.
662 void mmc_gate_clock(struct mmc_host *host)
666 spin_lock_irqsave(&host->clk_lock, flags);
667 host->clk_old = host->ios.clock;
669 host->clk_gated = true;
670 spin_unlock_irqrestore(&host->clk_lock, flags);
675 * This restores the clock from gating by using the cached
678 void mmc_ungate_clock(struct mmc_host *host)
681 * We should previously have gated the clock, so the clock shall
682 * be 0 here! The clock may however be 0 during initialization,
683 * when some request operations are performed before setting
684 * the frequency. When ungate is requested in that situation
685 * we just ignore the call.
688 BUG_ON(host->ios.clock);
689 /* This call will also set host->clk_gated to false */
690 mmc_set_clock(host, host->clk_old);
694 void mmc_set_ungated(struct mmc_host *host)
699 * We've been given a new frequency while the clock is gated,
700 * so make sure we regard this as ungating it.
702 spin_lock_irqsave(&host->clk_lock, flags);
703 host->clk_gated = false;
704 spin_unlock_irqrestore(&host->clk_lock, flags);
708 void mmc_set_ungated(struct mmc_host *host)
714 * Change the bus mode (open drain/push-pull) of a host.
716 void mmc_set_bus_mode(struct mmc_host *host, unsigned int mode)
718 host->ios.bus_mode = mode;
723 * Change data bus width and DDR mode of a host.
725 void mmc_set_bus_width_ddr(struct mmc_host *host, unsigned int width,
728 host->ios.bus_width = width;
734 * Change data bus width of a host.
736 void mmc_set_bus_width(struct mmc_host *host, unsigned int width)
738 mmc_set_bus_width_ddr(host, width, MMC_SDR_MODE);
742 * mmc_vdd_to_ocrbitnum - Convert a voltage to the OCR bit number
744 * @low_bits: prefer low bits in boundary cases
746 * This function returns the OCR bit number according to the provided @vdd
747 * value. If conversion is not possible a negative errno value returned.
749 * Depending on the @low_bits flag the function prefers low or high OCR bits
750 * on boundary voltages. For example,
751 * with @low_bits = true, 3300 mV translates to ilog2(MMC_VDD_32_33);
752 * with @low_bits = false, 3300 mV translates to ilog2(MMC_VDD_33_34);
754 * Any value in the [1951:1999] range translates to the ilog2(MMC_VDD_20_21).
756 static int mmc_vdd_to_ocrbitnum(int vdd, bool low_bits)
758 const int max_bit = ilog2(MMC_VDD_35_36);
761 if (vdd < 1650 || vdd > 3600)
764 if (vdd >= 1650 && vdd <= 1950)
765 return ilog2(MMC_VDD_165_195);
770 /* Base 2000 mV, step 100 mV, bit's base 8. */
771 bit = (vdd - 2000) / 100 + 8;
778 * mmc_vddrange_to_ocrmask - Convert a voltage range to the OCR mask
779 * @vdd_min: minimum voltage value (mV)
780 * @vdd_max: maximum voltage value (mV)
782 * This function returns the OCR mask bits according to the provided @vdd_min
783 * and @vdd_max values. If conversion is not possible the function returns 0.
785 * Notes wrt boundary cases:
786 * This function sets the OCR bits for all boundary voltages, for example
787 * [3300:3400] range is translated to MMC_VDD_32_33 | MMC_VDD_33_34 |
788 * MMC_VDD_34_35 mask.
790 u32 mmc_vddrange_to_ocrmask(int vdd_min, int vdd_max)
794 if (vdd_max < vdd_min)
797 /* Prefer high bits for the boundary vdd_max values. */
798 vdd_max = mmc_vdd_to_ocrbitnum(vdd_max, false);
802 /* Prefer low bits for the boundary vdd_min values. */
803 vdd_min = mmc_vdd_to_ocrbitnum(vdd_min, true);
807 /* Fill the mask, from max bit to min bit. */
808 while (vdd_max >= vdd_min)
809 mask |= 1 << vdd_max--;
813 EXPORT_SYMBOL(mmc_vddrange_to_ocrmask);
815 #ifdef CONFIG_REGULATOR
818 * mmc_regulator_get_ocrmask - return mask of supported voltages
819 * @supply: regulator to use
821 * This returns either a negative errno, or a mask of voltages that
822 * can be provided to MMC/SD/SDIO devices using the specified voltage
823 * regulator. This would normally be called before registering the
826 int mmc_regulator_get_ocrmask(struct regulator *supply)
832 count = regulator_count_voltages(supply);
836 for (i = 0; i < count; i++) {
840 vdd_uV = regulator_list_voltage(supply, i);
844 vdd_mV = vdd_uV / 1000;
845 result |= mmc_vddrange_to_ocrmask(vdd_mV, vdd_mV);
850 EXPORT_SYMBOL(mmc_regulator_get_ocrmask);
853 * mmc_regulator_set_ocr - set regulator to match host->ios voltage
854 * @mmc: the host to regulate
855 * @supply: regulator to use
856 * @vdd_bit: zero for power off, else a bit number (host->ios.vdd)
858 * Returns zero on success, else negative errno.
860 * MMC host drivers may use this to enable or disable a regulator using
861 * a particular supply voltage. This would normally be called from the
864 int mmc_regulator_set_ocr(struct mmc_host *mmc,
865 struct regulator *supply,
866 unsigned short vdd_bit)
875 /* REVISIT mmc_vddrange_to_ocrmask() may have set some
876 * bits this regulator doesn't quite support ... don't
877 * be too picky, most cards and regulators are OK with
878 * a 0.1V range goof (it's a small error percentage).
880 tmp = vdd_bit - ilog2(MMC_VDD_165_195);
882 min_uV = 1650 * 1000;
883 max_uV = 1950 * 1000;
885 min_uV = 1900 * 1000 + tmp * 100 * 1000;
886 max_uV = min_uV + 100 * 1000;
889 /* avoid needless changes to this voltage; the regulator
890 * might not allow this operation
892 voltage = regulator_get_voltage(supply);
895 else if (voltage < min_uV || voltage > max_uV)
896 result = regulator_set_voltage(supply, min_uV, max_uV);
900 if (result == 0 && !mmc->regulator_enabled) {
901 result = regulator_enable(supply);
903 mmc->regulator_enabled = true;
905 } else if (mmc->regulator_enabled) {
906 result = regulator_disable(supply);
908 mmc->regulator_enabled = false;
912 dev_err(mmc_dev(mmc),
913 "could not set regulator OCR (%d)\n", result);
916 EXPORT_SYMBOL(mmc_regulator_set_ocr);
918 #endif /* CONFIG_REGULATOR */
921 * Mask off any voltages we don't support and select
924 u32 mmc_select_voltage(struct mmc_host *host, u32 ocr)
928 ocr &= host->ocr_avail;
939 pr_warning("%s: host doesn't support card's voltages\n",
948 * Select timing parameters for host.
950 void mmc_set_timing(struct mmc_host *host, unsigned int timing)
952 host->ios.timing = timing;
957 * Apply power to the MMC stack. This is a two-stage process.
958 * First, we enable power to the card without the clock running.
959 * We then wait a bit for the power to stabilise. Finally,
960 * enable the bus drivers and clock to the card.
962 * We must _NOT_ enable the clock prior to power stablising.
964 * If a host does all the power sequencing itself, ignore the
965 * initial MMC_POWER_UP stage.
967 static void mmc_power_up(struct mmc_host *host)
971 /* If ocr is set, we use it */
973 bit = ffs(host->ocr) - 1;
975 bit = fls(host->ocr_avail) - 1;
978 if (mmc_host_is_spi(host)) {
979 host->ios.chip_select = MMC_CS_HIGH;
980 host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
982 host->ios.chip_select = MMC_CS_DONTCARE;
983 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
985 host->ios.power_mode = MMC_POWER_UP;
986 host->ios.bus_width = MMC_BUS_WIDTH_1;
987 host->ios.timing = MMC_TIMING_LEGACY;
991 * This delay should be sufficient to allow the power supply
992 * to reach the minimum voltage.
996 host->ios.clock = host->f_init;
998 host->ios.power_mode = MMC_POWER_ON;
1002 * This delay must be at least 74 clock sizes, or 1 ms, or the
1003 * time required to reach a stable voltage.
1008 static void mmc_power_off(struct mmc_host *host)
1010 host->ios.clock = 0;
1014 * Reset ocr mask to be the highest possible voltage supported for
1015 * this mmc host. This value will be used at next power up.
1017 host->ocr = 1 << (fls(host->ocr_avail) - 1);
1019 if (!mmc_host_is_spi(host)) {
1020 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
1021 host->ios.chip_select = MMC_CS_DONTCARE;
1023 host->ios.power_mode = MMC_POWER_OFF;
1024 host->ios.bus_width = MMC_BUS_WIDTH_1;
1025 host->ios.timing = MMC_TIMING_LEGACY;
1030 * Cleanup when the last reference to the bus operator is dropped.
1032 static void __mmc_release_bus(struct mmc_host *host)
1035 BUG_ON(host->bus_refs);
1036 BUG_ON(!host->bus_dead);
1038 host->bus_ops = NULL;
1042 * Increase reference count of bus operator
1044 static inline void mmc_bus_get(struct mmc_host *host)
1046 unsigned long flags;
1048 spin_lock_irqsave(&host->lock, flags);
1050 spin_unlock_irqrestore(&host->lock, flags);
1054 * Decrease reference count of bus operator and free it if
1055 * it is the last reference.
1057 static inline void mmc_bus_put(struct mmc_host *host)
1059 unsigned long flags;
1061 spin_lock_irqsave(&host->lock, flags);
1063 if ((host->bus_refs == 0) && host->bus_ops)
1064 __mmc_release_bus(host);
1065 spin_unlock_irqrestore(&host->lock, flags);
1069 * Assign a mmc bus handler to a host. Only one bus handler may control a
1070 * host at any given time.
1072 void mmc_attach_bus(struct mmc_host *host, const struct mmc_bus_ops *ops)
1074 unsigned long flags;
1079 WARN_ON(!host->claimed);
1081 spin_lock_irqsave(&host->lock, flags);
1083 BUG_ON(host->bus_ops);
1084 BUG_ON(host->bus_refs);
1086 host->bus_ops = ops;
1090 spin_unlock_irqrestore(&host->lock, flags);
1094 * Remove the current bus handler from a host. Assumes that there are
1095 * no interesting cards left, so the bus is powered down.
1097 void mmc_detach_bus(struct mmc_host *host)
1099 unsigned long flags;
1103 WARN_ON(!host->claimed);
1104 WARN_ON(!host->bus_ops);
1106 spin_lock_irqsave(&host->lock, flags);
1110 spin_unlock_irqrestore(&host->lock, flags);
1112 mmc_power_off(host);
1118 * mmc_detect_change - process change of state on a MMC socket
1119 * @host: host which changed state.
1120 * @delay: optional delay to wait before detection (jiffies)
1122 * MMC drivers should call this when they detect a card has been
1123 * inserted or removed. The MMC layer will confirm that any
1124 * present card is still functional, and initialize any newly
1127 void mmc_detect_change(struct mmc_host *host, unsigned long delay)
1129 #ifdef CONFIG_MMC_DEBUG
1130 unsigned long flags;
1131 spin_lock_irqsave(&host->lock, flags);
1132 WARN_ON(host->removed);
1133 spin_unlock_irqrestore(&host->lock, flags);
1136 mmc_schedule_delayed_work(&host->detect, delay);
1139 EXPORT_SYMBOL(mmc_detect_change);
1141 void mmc_init_erase(struct mmc_card *card)
1145 if (is_power_of_2(card->erase_size))
1146 card->erase_shift = ffs(card->erase_size) - 1;
1148 card->erase_shift = 0;
1151 * It is possible to erase an arbitrarily large area of an SD or MMC
1152 * card. That is not desirable because it can take a long time
1153 * (minutes) potentially delaying more important I/O, and also the
1154 * timeout calculations become increasingly hugely over-estimated.
1155 * Consequently, 'pref_erase' is defined as a guide to limit erases
1156 * to that size and alignment.
1158 * For SD cards that define Allocation Unit size, limit erases to one
1159 * Allocation Unit at a time. For MMC cards that define High Capacity
1160 * Erase Size, whether it is switched on or not, limit to that size.
1161 * Otherwise just have a stab at a good value. For modern cards it
1162 * will end up being 4MiB. Note that if the value is too small, it
1163 * can end up taking longer to erase.
1165 if (mmc_card_sd(card) && card->ssr.au) {
1166 card->pref_erase = card->ssr.au;
1167 card->erase_shift = ffs(card->ssr.au) - 1;
1168 } else if (card->ext_csd.hc_erase_size) {
1169 card->pref_erase = card->ext_csd.hc_erase_size;
1171 sz = (card->csd.capacity << (card->csd.read_blkbits - 9)) >> 11;
1173 card->pref_erase = 512 * 1024 / 512;
1175 card->pref_erase = 1024 * 1024 / 512;
1177 card->pref_erase = 2 * 1024 * 1024 / 512;
1179 card->pref_erase = 4 * 1024 * 1024 / 512;
1180 if (card->pref_erase < card->erase_size)
1181 card->pref_erase = card->erase_size;
1183 sz = card->pref_erase % card->erase_size;
1185 card->pref_erase += card->erase_size - sz;
1190 static unsigned int mmc_mmc_erase_timeout(struct mmc_card *card,
1191 unsigned int arg, unsigned int qty)
1193 unsigned int erase_timeout;
1195 if (card->ext_csd.erase_group_def & 1) {
1196 /* High Capacity Erase Group Size uses HC timeouts */
1197 if (arg == MMC_TRIM_ARG)
1198 erase_timeout = card->ext_csd.trim_timeout;
1200 erase_timeout = card->ext_csd.hc_erase_timeout;
1202 /* CSD Erase Group Size uses write timeout */
1203 unsigned int mult = (10 << card->csd.r2w_factor);
1204 unsigned int timeout_clks = card->csd.tacc_clks * mult;
1205 unsigned int timeout_us;
1207 /* Avoid overflow: e.g. tacc_ns=80000000 mult=1280 */
1208 if (card->csd.tacc_ns < 1000000)
1209 timeout_us = (card->csd.tacc_ns * mult) / 1000;
1211 timeout_us = (card->csd.tacc_ns / 1000) * mult;
1214 * ios.clock is only a target. The real clock rate might be
1215 * less but not that much less, so fudge it by multiplying by 2.
1218 timeout_us += (timeout_clks * 1000) /
1219 (card->host->ios.clock / 1000);
1221 erase_timeout = timeout_us / 1000;
1224 * Theoretically, the calculation could underflow so round up
1225 * to 1ms in that case.
1231 /* Multiplier for secure operations */
1232 if (arg & MMC_SECURE_ARGS) {
1233 if (arg == MMC_SECURE_ERASE_ARG)
1234 erase_timeout *= card->ext_csd.sec_erase_mult;
1236 erase_timeout *= card->ext_csd.sec_trim_mult;
1239 erase_timeout *= qty;
1242 * Ensure at least a 1 second timeout for SPI as per
1243 * 'mmc_set_data_timeout()'
1245 if (mmc_host_is_spi(card->host) && erase_timeout < 1000)
1246 erase_timeout = 1000;
1248 return erase_timeout;
1251 static unsigned int mmc_sd_erase_timeout(struct mmc_card *card,
1255 unsigned int erase_timeout;
1257 if (card->ssr.erase_timeout) {
1258 /* Erase timeout specified in SD Status Register (SSR) */
1259 erase_timeout = card->ssr.erase_timeout * qty +
1260 card->ssr.erase_offset;
1263 * Erase timeout not specified in SD Status Register (SSR) so
1264 * use 250ms per write block.
1266 erase_timeout = 250 * qty;
1269 /* Must not be less than 1 second */
1270 if (erase_timeout < 1000)
1271 erase_timeout = 1000;
1273 return erase_timeout;
1276 static unsigned int mmc_erase_timeout(struct mmc_card *card,
1280 if (mmc_card_sd(card))
1281 return mmc_sd_erase_timeout(card, arg, qty);
1283 return mmc_mmc_erase_timeout(card, arg, qty);
1286 static int mmc_do_erase(struct mmc_card *card, unsigned int from,
1287 unsigned int to, unsigned int arg)
1289 struct mmc_command cmd;
1290 unsigned int qty = 0;
1294 * qty is used to calculate the erase timeout which depends on how many
1295 * erase groups (or allocation units in SD terminology) are affected.
1296 * We count erasing part of an erase group as one erase group.
1297 * For SD, the allocation units are always a power of 2. For MMC, the
1298 * erase group size is almost certainly also power of 2, but it does not
1299 * seem to insist on that in the JEDEC standard, so we fall back to
1300 * division in that case. SD may not specify an allocation unit size,
1301 * in which case the timeout is based on the number of write blocks.
1303 * Note that the timeout for secure trim 2 will only be correct if the
1304 * number of erase groups specified is the same as the total of all
1305 * preceding secure trim 1 commands. Since the power may have been
1306 * lost since the secure trim 1 commands occurred, it is generally
1307 * impossible to calculate the secure trim 2 timeout correctly.
1309 if (card->erase_shift)
1310 qty += ((to >> card->erase_shift) -
1311 (from >> card->erase_shift)) + 1;
1312 else if (mmc_card_sd(card))
1313 qty += to - from + 1;
1315 qty += ((to / card->erase_size) -
1316 (from / card->erase_size)) + 1;
1318 if (!mmc_card_blockaddr(card)) {
1323 memset(&cmd, 0, sizeof(struct mmc_command));
1324 if (mmc_card_sd(card))
1325 cmd.opcode = SD_ERASE_WR_BLK_START;
1327 cmd.opcode = MMC_ERASE_GROUP_START;
1329 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
1330 err = mmc_wait_for_cmd(card->host, &cmd, 0);
1332 printk(KERN_ERR "mmc_erase: group start error %d, "
1333 "status %#x\n", err, cmd.resp[0]);
1338 memset(&cmd, 0, sizeof(struct mmc_command));
1339 if (mmc_card_sd(card))
1340 cmd.opcode = SD_ERASE_WR_BLK_END;
1342 cmd.opcode = MMC_ERASE_GROUP_END;
1344 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
1345 err = mmc_wait_for_cmd(card->host, &cmd, 0);
1347 printk(KERN_ERR "mmc_erase: group end error %d, status %#x\n",
1353 memset(&cmd, 0, sizeof(struct mmc_command));
1354 cmd.opcode = MMC_ERASE;
1356 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1357 cmd.cmd_timeout_ms = mmc_erase_timeout(card, arg, qty);
1358 err = mmc_wait_for_cmd(card->host, &cmd, 0);
1360 printk(KERN_ERR "mmc_erase: erase error %d, status %#x\n",
1366 if (mmc_host_is_spi(card->host))
1370 memset(&cmd, 0, sizeof(struct mmc_command));
1371 cmd.opcode = MMC_SEND_STATUS;
1372 cmd.arg = card->rca << 16;
1373 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
1374 /* Do not retry else we can't see errors */
1375 err = mmc_wait_for_cmd(card->host, &cmd, 0);
1376 if (err || (cmd.resp[0] & 0xFDF92000)) {
1377 printk(KERN_ERR "error %d requesting status %#x\n",
1382 } while (!(cmd.resp[0] & R1_READY_FOR_DATA) ||
1383 R1_CURRENT_STATE(cmd.resp[0]) == 7);
1389 * mmc_erase - erase sectors.
1390 * @card: card to erase
1391 * @from: first sector to erase
1392 * @nr: number of sectors to erase
1393 * @arg: erase command argument (SD supports only %MMC_ERASE_ARG)
1395 * Caller must claim host before calling this function.
1397 int mmc_erase(struct mmc_card *card, unsigned int from, unsigned int nr,
1400 unsigned int rem, to = from + nr;
1402 if (!(card->host->caps & MMC_CAP_ERASE) ||
1403 !(card->csd.cmdclass & CCC_ERASE))
1406 if (!card->erase_size)
1409 if (mmc_card_sd(card) && arg != MMC_ERASE_ARG)
1412 if ((arg & MMC_SECURE_ARGS) &&
1413 !(card->ext_csd.sec_feature_support & EXT_CSD_SEC_ER_EN))
1416 if ((arg & MMC_TRIM_ARGS) &&
1417 !(card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN))
1420 if (arg == MMC_SECURE_ERASE_ARG) {
1421 if (from % card->erase_size || nr % card->erase_size)
1425 if (arg == MMC_ERASE_ARG) {
1426 rem = from % card->erase_size;
1428 rem = card->erase_size - rem;
1435 rem = nr % card->erase_size;
1448 /* 'from' and 'to' are inclusive */
1451 return mmc_do_erase(card, from, to, arg);
1453 EXPORT_SYMBOL(mmc_erase);
1455 int mmc_can_erase(struct mmc_card *card)
1457 if ((card->host->caps & MMC_CAP_ERASE) &&
1458 (card->csd.cmdclass & CCC_ERASE) && card->erase_size)
1462 EXPORT_SYMBOL(mmc_can_erase);
1464 int mmc_can_trim(struct mmc_card *card)
1466 if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN)
1470 EXPORT_SYMBOL(mmc_can_trim);
1472 int mmc_can_secure_erase_trim(struct mmc_card *card)
1474 if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_ER_EN)
1478 EXPORT_SYMBOL(mmc_can_secure_erase_trim);
1480 int mmc_erase_group_aligned(struct mmc_card *card, unsigned int from,
1483 if (!card->erase_size)
1485 if (from % card->erase_size || nr % card->erase_size)
1489 EXPORT_SYMBOL(mmc_erase_group_aligned);
1491 int mmc_set_blocklen(struct mmc_card *card, unsigned int blocklen)
1493 struct mmc_command cmd;
1495 if (mmc_card_blockaddr(card) || mmc_card_ddr_mode(card))
1498 memset(&cmd, 0, sizeof(struct mmc_command));
1499 cmd.opcode = MMC_SET_BLOCKLEN;
1501 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
1502 return mmc_wait_for_cmd(card->host, &cmd, 5);
1504 EXPORT_SYMBOL(mmc_set_blocklen);
1506 static int mmc_rescan_try_freq(struct mmc_host *host, unsigned freq)
1508 host->f_init = freq;
1510 #ifdef CONFIG_MMC_DEBUG
1511 pr_info("%s: %s: trying to init card at %u Hz\n",
1512 mmc_hostname(host), __func__, host->f_init);
1517 * sdio_reset sends CMD52 to reset card. Since we do not know
1518 * if the card is being re-initialized, just send it. CMD52
1519 * should be ignored by SD/eMMC cards.
1524 mmc_send_if_cond(host, host->ocr_avail);
1526 /* Order's important: probe SDIO, then SD, then MMC */
1527 if (!mmc_attach_sdio(host))
1529 if (!mmc_attach_sd(host))
1531 if (!mmc_attach_mmc(host))
1534 mmc_power_off(host);
1538 void mmc_rescan(struct work_struct *work)
1540 static const unsigned freqs[] = { 400000, 300000, 200000, 100000 };
1541 struct mmc_host *host =
1542 container_of(work, struct mmc_host, detect.work);
1545 if (host->rescan_disable)
1551 * if there is a _removable_ card registered, check whether it is
1554 if (host->bus_ops && host->bus_ops->detect && !host->bus_dead
1555 && !(host->caps & MMC_CAP_NONREMOVABLE))
1556 host->bus_ops->detect(host);
1559 * Let mmc_bus_put() free the bus/bus_ops if we've found that
1560 * the card is no longer present.
1565 /* if there still is a card present, stop here */
1566 if (host->bus_ops != NULL) {
1572 * Only we can add a new handler, so it's safe to
1573 * release the lock here.
1577 if (host->ops->get_cd && host->ops->get_cd(host) == 0)
1580 mmc_claim_host(host);
1581 for (i = 0; i < ARRAY_SIZE(freqs); i++) {
1582 if (!mmc_rescan_try_freq(host, max(freqs[i], host->f_min)))
1584 if (freqs[i] < host->f_min)
1587 mmc_release_host(host);
1590 if (host->caps & MMC_CAP_NEEDS_POLL)
1591 mmc_schedule_delayed_work(&host->detect, HZ);
1594 void mmc_start_host(struct mmc_host *host)
1596 mmc_power_off(host);
1597 mmc_detect_change(host, 0);
1600 void mmc_stop_host(struct mmc_host *host)
1602 #ifdef CONFIG_MMC_DEBUG
1603 unsigned long flags;
1604 spin_lock_irqsave(&host->lock, flags);
1606 spin_unlock_irqrestore(&host->lock, flags);
1609 if (host->caps & MMC_CAP_DISABLE)
1610 cancel_delayed_work(&host->disable);
1611 cancel_delayed_work_sync(&host->detect);
1612 mmc_flush_scheduled_work();
1614 /* clear pm flags now and let card drivers set them as needed */
1618 if (host->bus_ops && !host->bus_dead) {
1619 if (host->bus_ops->remove)
1620 host->bus_ops->remove(host);
1622 mmc_claim_host(host);
1623 mmc_detach_bus(host);
1624 mmc_release_host(host);
1632 mmc_power_off(host);
1635 int mmc_power_save_host(struct mmc_host *host)
1641 if (!host->bus_ops || host->bus_dead || !host->bus_ops->power_restore) {
1646 if (host->bus_ops->power_save)
1647 ret = host->bus_ops->power_save(host);
1651 mmc_power_off(host);
1655 EXPORT_SYMBOL(mmc_power_save_host);
1657 int mmc_power_restore_host(struct mmc_host *host)
1663 if (!host->bus_ops || host->bus_dead || !host->bus_ops->power_restore) {
1669 ret = host->bus_ops->power_restore(host);
1675 EXPORT_SYMBOL(mmc_power_restore_host);
1677 int mmc_card_awake(struct mmc_host *host)
1683 if (host->bus_ops && !host->bus_dead && host->bus_ops->awake)
1684 err = host->bus_ops->awake(host);
1690 EXPORT_SYMBOL(mmc_card_awake);
1692 int mmc_card_sleep(struct mmc_host *host)
1698 if (host->bus_ops && !host->bus_dead && host->bus_ops->awake)
1699 err = host->bus_ops->sleep(host);
1705 EXPORT_SYMBOL(mmc_card_sleep);
1707 int mmc_card_can_sleep(struct mmc_host *host)
1709 struct mmc_card *card = host->card;
1711 if (card && mmc_card_mmc(card) && card->ext_csd.rev >= 3)
1715 EXPORT_SYMBOL(mmc_card_can_sleep);
1720 * mmc_suspend_host - suspend a host
1723 int mmc_suspend_host(struct mmc_host *host)
1727 if (host->caps & MMC_CAP_DISABLE)
1728 cancel_delayed_work(&host->disable);
1729 cancel_delayed_work(&host->detect);
1730 mmc_flush_scheduled_work();
1733 if (host->bus_ops && !host->bus_dead) {
1734 if (host->bus_ops->suspend)
1735 err = host->bus_ops->suspend(host);
1736 if (err == -ENOSYS || !host->bus_ops->resume) {
1738 * We simply "remove" the card in this case.
1739 * It will be redetected on resume.
1741 if (host->bus_ops->remove)
1742 host->bus_ops->remove(host);
1743 mmc_claim_host(host);
1744 mmc_detach_bus(host);
1745 mmc_release_host(host);
1752 if (!err && !mmc_card_keep_power(host))
1753 mmc_power_off(host);
1758 EXPORT_SYMBOL(mmc_suspend_host);
1761 * mmc_resume_host - resume a previously suspended host
1764 int mmc_resume_host(struct mmc_host *host)
1769 if (host->bus_ops && !host->bus_dead) {
1770 if (!mmc_card_keep_power(host)) {
1772 mmc_select_voltage(host, host->ocr);
1774 * Tell runtime PM core we just powered up the card,
1775 * since it still believes the card is powered off.
1776 * Note that currently runtime PM is only enabled
1777 * for SDIO cards that are MMC_CAP_POWER_OFF_CARD
1779 if (mmc_card_sdio(host->card) &&
1780 (host->caps & MMC_CAP_POWER_OFF_CARD)) {
1781 pm_runtime_disable(&host->card->dev);
1782 pm_runtime_set_active(&host->card->dev);
1783 pm_runtime_enable(&host->card->dev);
1786 BUG_ON(!host->bus_ops->resume);
1787 err = host->bus_ops->resume(host);
1789 printk(KERN_WARNING "%s: error %d during resume "
1790 "(card was removed?)\n",
1791 mmc_hostname(host), err);
1799 EXPORT_SYMBOL(mmc_resume_host);
1801 /* Do the card removal on suspend if card is assumed removeable
1802 * Do that in pm notifier while userspace isn't yet frozen, so we will be able
1805 int mmc_pm_notify(struct notifier_block *notify_block,
1806 unsigned long mode, void *unused)
1808 struct mmc_host *host = container_of(
1809 notify_block, struct mmc_host, pm_notify);
1810 unsigned long flags;
1814 case PM_HIBERNATION_PREPARE:
1815 case PM_SUSPEND_PREPARE:
1817 spin_lock_irqsave(&host->lock, flags);
1818 host->rescan_disable = 1;
1819 spin_unlock_irqrestore(&host->lock, flags);
1820 cancel_delayed_work_sync(&host->detect);
1822 if (!host->bus_ops || host->bus_ops->suspend)
1825 mmc_claim_host(host);
1827 if (host->bus_ops->remove)
1828 host->bus_ops->remove(host);
1830 mmc_detach_bus(host);
1831 mmc_release_host(host);
1835 case PM_POST_SUSPEND:
1836 case PM_POST_HIBERNATION:
1837 case PM_POST_RESTORE:
1839 spin_lock_irqsave(&host->lock, flags);
1840 host->rescan_disable = 0;
1841 spin_unlock_irqrestore(&host->lock, flags);
1842 mmc_detect_change(host, 0);
1850 static int __init mmc_init(void)
1854 workqueue = alloc_ordered_workqueue("kmmcd", 0);
1858 ret = mmc_register_bus();
1860 goto destroy_workqueue;
1862 ret = mmc_register_host_class();
1864 goto unregister_bus;
1866 ret = sdio_register_bus();
1868 goto unregister_host_class;
1872 unregister_host_class:
1873 mmc_unregister_host_class();
1875 mmc_unregister_bus();
1877 destroy_workqueue(workqueue);
1882 static void __exit mmc_exit(void)
1884 sdio_unregister_bus();
1885 mmc_unregister_host_class();
1886 mmc_unregister_bus();
1887 destroy_workqueue(workqueue);
1890 subsys_initcall(mmc_init);
1891 module_exit(mmc_exit);
1893 MODULE_LICENSE("GPL");