2 * linux/drivers/mmc/core/core.c
4 * Copyright (C) 2003-2004 Russell King, All Rights Reserved.
5 * SD support Copyright (C) 2004 Ian Molton, All Rights Reserved.
6 * Copyright (C) 2005-2008 Pierre Ossman, All Rights Reserved.
7 * MMCv4 support Copyright (C) 2006 Philip Langdale, All Rights Reserved.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
13 #include <linux/module.h>
14 #include <linux/init.h>
15 #include <linux/interrupt.h>
16 #include <linux/completion.h>
17 #include <linux/device.h>
18 #include <linux/delay.h>
19 #include <linux/pagemap.h>
20 #include <linux/err.h>
21 #include <linux/leds.h>
22 #include <linux/scatterlist.h>
23 #include <linux/log2.h>
24 #include <linux/regulator/consumer.h>
26 #include <linux/mmc/card.h>
27 #include <linux/mmc/host.h>
28 #include <linux/mmc/mmc.h>
29 #include <linux/mmc/sd.h>
40 static struct workqueue_struct *workqueue;
43 * Enabling software CRCs on the data blocks can be a significant (30%)
44 * performance cost, and for other reasons may not always be desired.
45 * So we allow it it to be disabled.
48 module_param(use_spi_crc, bool, 0);
51 * We normally treat cards as removed during suspend if they are not
52 * known to be on a non-removable bus, to avoid the risk of writing
53 * back data to a different card after resume. Allow this to be
54 * overridden if necessary.
56 #ifdef CONFIG_MMC_UNSAFE_RESUME
57 int mmc_assume_removable;
59 int mmc_assume_removable = 1;
61 EXPORT_SYMBOL(mmc_assume_removable);
62 module_param_named(removable, mmc_assume_removable, bool, 0644);
65 "MMC/SD cards are removable and may be removed during suspend");
68 * Internal function. Schedule delayed work in the MMC work queue.
70 static int mmc_schedule_delayed_work(struct delayed_work *work,
73 return queue_delayed_work(workqueue, work, delay);
77 * Internal function. Flush all scheduled work from the MMC work queue.
79 static void mmc_flush_scheduled_work(void)
81 flush_workqueue(workqueue);
85 * mmc_request_done - finish processing an MMC request
86 * @host: MMC host which completed request
87 * @mrq: MMC request which request
89 * MMC drivers should call this function when they have completed
90 * their processing of a request.
92 void mmc_request_done(struct mmc_host *host, struct mmc_request *mrq)
94 struct mmc_command *cmd = mrq->cmd;
97 if (err && cmd->retries && mmc_host_is_spi(host)) {
98 if (cmd->resp[0] & R1_SPI_ILLEGAL_COMMAND)
102 if (err && cmd->retries) {
103 pr_debug("%s: req failed (CMD%u): %d, retrying...\n",
104 mmc_hostname(host), cmd->opcode, err);
108 host->ops->request(host, mrq);
110 led_trigger_event(host->led, LED_OFF);
112 pr_debug("%s: req done (CMD%u): %d: %08x %08x %08x %08x\n",
113 mmc_hostname(host), cmd->opcode, err,
114 cmd->resp[0], cmd->resp[1],
115 cmd->resp[2], cmd->resp[3]);
118 pr_debug("%s: %d bytes transferred: %d\n",
120 mrq->data->bytes_xfered, mrq->data->error);
124 pr_debug("%s: (CMD%u): %d: %08x %08x %08x %08x\n",
125 mmc_hostname(host), mrq->stop->opcode,
127 mrq->stop->resp[0], mrq->stop->resp[1],
128 mrq->stop->resp[2], mrq->stop->resp[3]);
134 mmc_host_clk_gate(host);
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 mmc_host_clk_ungate(host);
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 timeout_us += data->timeout_clks * 1000 /
302 (mmc_host_clk_rate(card->host) / 1000);
304 if (data->flags & MMC_DATA_WRITE)
306 * The limit is really 250 ms, but that is
307 * insufficient for some crappy cards.
314 * SDHC cards always use these fixed values.
316 if (timeout_us > limit_us || mmc_card_blockaddr(card)) {
317 data->timeout_ns = limit_us * 1000;
318 data->timeout_clks = 0;
322 * Some cards need very high timeouts if driven in SPI mode.
323 * The worst observed timeout was 900ms after writing a
324 * continuous stream of data until the internal logic
327 if (mmc_host_is_spi(card->host)) {
328 if (data->flags & MMC_DATA_WRITE) {
329 if (data->timeout_ns < 1000000000)
330 data->timeout_ns = 1000000000; /* 1s */
332 if (data->timeout_ns < 100000000)
333 data->timeout_ns = 100000000; /* 100ms */
337 EXPORT_SYMBOL(mmc_set_data_timeout);
340 * mmc_align_data_size - pads a transfer size to a more optimal value
341 * @card: the MMC card associated with the data transfer
342 * @sz: original transfer size
344 * Pads the original data size with a number of extra bytes in
345 * order to avoid controller bugs and/or performance hits
346 * (e.g. some controllers revert to PIO for certain sizes).
348 * Returns the improved size, which might be unmodified.
350 * Note that this function is only relevant when issuing a
351 * single scatter gather entry.
353 unsigned int mmc_align_data_size(struct mmc_card *card, unsigned int sz)
356 * FIXME: We don't have a system for the controller to tell
357 * the core about its problems yet, so for now we just 32-bit
360 sz = ((sz + 3) / 4) * 4;
364 EXPORT_SYMBOL(mmc_align_data_size);
367 * mmc_host_enable - enable a host.
368 * @host: mmc host to enable
370 * Hosts that support power saving can use the 'enable' and 'disable'
371 * methods to exit and enter power saving states. For more information
372 * see comments for struct mmc_host_ops.
374 int mmc_host_enable(struct mmc_host *host)
376 if (!(host->caps & MMC_CAP_DISABLE))
379 if (host->en_dis_recurs)
382 if (host->nesting_cnt++)
385 cancel_delayed_work_sync(&host->disable);
390 if (host->ops->enable) {
393 host->en_dis_recurs = 1;
394 err = host->ops->enable(host);
395 host->en_dis_recurs = 0;
398 pr_debug("%s: enable error %d\n",
399 mmc_hostname(host), err);
406 EXPORT_SYMBOL(mmc_host_enable);
408 static int mmc_host_do_disable(struct mmc_host *host, int lazy)
410 if (host->ops->disable) {
413 host->en_dis_recurs = 1;
414 err = host->ops->disable(host, lazy);
415 host->en_dis_recurs = 0;
418 pr_debug("%s: disable error %d\n",
419 mmc_hostname(host), err);
423 unsigned long delay = msecs_to_jiffies(err);
425 mmc_schedule_delayed_work(&host->disable, delay);
433 * mmc_host_disable - disable a host.
434 * @host: mmc host to disable
436 * Hosts that support power saving can use the 'enable' and 'disable'
437 * methods to exit and enter power saving states. For more information
438 * see comments for struct mmc_host_ops.
440 int mmc_host_disable(struct mmc_host *host)
444 if (!(host->caps & MMC_CAP_DISABLE))
447 if (host->en_dis_recurs)
450 if (--host->nesting_cnt)
456 err = mmc_host_do_disable(host, 0);
459 EXPORT_SYMBOL(mmc_host_disable);
462 * __mmc_claim_host - exclusively claim a host
463 * @host: mmc host to claim
464 * @abort: whether or not the operation should be aborted
466 * Claim a host for a set of operations. If @abort is non null and
467 * dereference a non-zero value then this will return prematurely with
468 * that non-zero value without acquiring the lock. Returns zero
469 * with the lock held otherwise.
471 int __mmc_claim_host(struct mmc_host *host, atomic_t *abort)
473 DECLARE_WAITQUEUE(wait, current);
479 add_wait_queue(&host->wq, &wait);
480 spin_lock_irqsave(&host->lock, flags);
482 set_current_state(TASK_UNINTERRUPTIBLE);
483 stop = abort ? atomic_read(abort) : 0;
484 if (stop || !host->claimed || host->claimer == current)
486 spin_unlock_irqrestore(&host->lock, flags);
488 spin_lock_irqsave(&host->lock, flags);
490 set_current_state(TASK_RUNNING);
493 host->claimer = current;
494 host->claim_cnt += 1;
497 spin_unlock_irqrestore(&host->lock, flags);
498 remove_wait_queue(&host->wq, &wait);
500 mmc_host_enable(host);
504 EXPORT_SYMBOL(__mmc_claim_host);
507 * mmc_try_claim_host - try exclusively to claim a host
508 * @host: mmc host to claim
510 * Returns %1 if the host is claimed, %0 otherwise.
512 int mmc_try_claim_host(struct mmc_host *host)
514 int claimed_host = 0;
517 spin_lock_irqsave(&host->lock, flags);
518 if (!host->claimed || host->claimer == current) {
520 host->claimer = current;
521 host->claim_cnt += 1;
524 spin_unlock_irqrestore(&host->lock, flags);
527 EXPORT_SYMBOL(mmc_try_claim_host);
529 static void mmc_do_release_host(struct mmc_host *host)
533 spin_lock_irqsave(&host->lock, flags);
534 if (--host->claim_cnt) {
535 /* Release for nested claim */
536 spin_unlock_irqrestore(&host->lock, flags);
539 host->claimer = NULL;
540 spin_unlock_irqrestore(&host->lock, flags);
545 void mmc_host_deeper_disable(struct work_struct *work)
547 struct mmc_host *host =
548 container_of(work, struct mmc_host, disable.work);
550 /* If the host is claimed then we do not want to disable it anymore */
551 if (!mmc_try_claim_host(host))
553 mmc_host_do_disable(host, 1);
554 mmc_do_release_host(host);
558 * mmc_host_lazy_disable - lazily disable a host.
559 * @host: mmc host to disable
561 * Hosts that support power saving can use the 'enable' and 'disable'
562 * methods to exit and enter power saving states. For more information
563 * see comments for struct mmc_host_ops.
565 int mmc_host_lazy_disable(struct mmc_host *host)
567 if (!(host->caps & MMC_CAP_DISABLE))
570 if (host->en_dis_recurs)
573 if (--host->nesting_cnt)
579 if (host->disable_delay) {
580 mmc_schedule_delayed_work(&host->disable,
581 msecs_to_jiffies(host->disable_delay));
584 return mmc_host_do_disable(host, 1);
586 EXPORT_SYMBOL(mmc_host_lazy_disable);
589 * mmc_release_host - release a host
590 * @host: mmc host to release
592 * Release a MMC host, allowing others to claim the host
593 * for their operations.
595 void mmc_release_host(struct mmc_host *host)
597 WARN_ON(!host->claimed);
599 mmc_host_lazy_disable(host);
601 mmc_do_release_host(host);
604 EXPORT_SYMBOL(mmc_release_host);
607 * Internal function that does the actual ios call to the host driver,
608 * optionally printing some debug output.
610 static inline void mmc_set_ios(struct mmc_host *host)
612 struct mmc_ios *ios = &host->ios;
614 pr_debug("%s: clock %uHz busmode %u powermode %u cs %u Vdd %u "
615 "width %u timing %u\n",
616 mmc_hostname(host), ios->clock, ios->bus_mode,
617 ios->power_mode, ios->chip_select, ios->vdd,
618 ios->bus_width, ios->timing);
621 mmc_set_ungated(host);
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;
649 #ifdef CONFIG_MMC_CLKGATE
651 * This gates the clock by setting it to 0 Hz.
653 void mmc_gate_clock(struct mmc_host *host)
657 spin_lock_irqsave(&host->clk_lock, flags);
658 host->clk_old = host->ios.clock;
660 host->clk_gated = true;
661 spin_unlock_irqrestore(&host->clk_lock, flags);
666 * This restores the clock from gating by using the cached
669 void mmc_ungate_clock(struct mmc_host *host)
672 * We should previously have gated the clock, so the clock shall
673 * be 0 here! The clock may however be 0 during initialization,
674 * when some request operations are performed before setting
675 * the frequency. When ungate is requested in that situation
676 * we just ignore the call.
679 BUG_ON(host->ios.clock);
680 /* This call will also set host->clk_gated to false */
681 mmc_set_clock(host, host->clk_old);
685 void mmc_set_ungated(struct mmc_host *host)
690 * We've been given a new frequency while the clock is gated,
691 * so make sure we regard this as ungating it.
693 spin_lock_irqsave(&host->clk_lock, flags);
694 host->clk_gated = false;
695 spin_unlock_irqrestore(&host->clk_lock, flags);
699 void mmc_set_ungated(struct mmc_host *host)
705 * Change the bus mode (open drain/push-pull) of a host.
707 void mmc_set_bus_mode(struct mmc_host *host, unsigned int mode)
709 host->ios.bus_mode = mode;
714 * Change data bus width and DDR mode of a host.
716 void mmc_set_bus_width_ddr(struct mmc_host *host, unsigned int width,
719 host->ios.bus_width = width;
725 * Change data bus width of a host.
727 void mmc_set_bus_width(struct mmc_host *host, unsigned int width)
729 mmc_set_bus_width_ddr(host, width, MMC_SDR_MODE);
733 * mmc_vdd_to_ocrbitnum - Convert a voltage to the OCR bit number
735 * @low_bits: prefer low bits in boundary cases
737 * This function returns the OCR bit number according to the provided @vdd
738 * value. If conversion is not possible a negative errno value returned.
740 * Depending on the @low_bits flag the function prefers low or high OCR bits
741 * on boundary voltages. For example,
742 * with @low_bits = true, 3300 mV translates to ilog2(MMC_VDD_32_33);
743 * with @low_bits = false, 3300 mV translates to ilog2(MMC_VDD_33_34);
745 * Any value in the [1951:1999] range translates to the ilog2(MMC_VDD_20_21).
747 static int mmc_vdd_to_ocrbitnum(int vdd, bool low_bits)
749 const int max_bit = ilog2(MMC_VDD_35_36);
752 if (vdd < 1650 || vdd > 3600)
755 if (vdd >= 1650 && vdd <= 1950)
756 return ilog2(MMC_VDD_165_195);
761 /* Base 2000 mV, step 100 mV, bit's base 8. */
762 bit = (vdd - 2000) / 100 + 8;
769 * mmc_vddrange_to_ocrmask - Convert a voltage range to the OCR mask
770 * @vdd_min: minimum voltage value (mV)
771 * @vdd_max: maximum voltage value (mV)
773 * This function returns the OCR mask bits according to the provided @vdd_min
774 * and @vdd_max values. If conversion is not possible the function returns 0.
776 * Notes wrt boundary cases:
777 * This function sets the OCR bits for all boundary voltages, for example
778 * [3300:3400] range is translated to MMC_VDD_32_33 | MMC_VDD_33_34 |
779 * MMC_VDD_34_35 mask.
781 u32 mmc_vddrange_to_ocrmask(int vdd_min, int vdd_max)
785 if (vdd_max < vdd_min)
788 /* Prefer high bits for the boundary vdd_max values. */
789 vdd_max = mmc_vdd_to_ocrbitnum(vdd_max, false);
793 /* Prefer low bits for the boundary vdd_min values. */
794 vdd_min = mmc_vdd_to_ocrbitnum(vdd_min, true);
798 /* Fill the mask, from max bit to min bit. */
799 while (vdd_max >= vdd_min)
800 mask |= 1 << vdd_max--;
804 EXPORT_SYMBOL(mmc_vddrange_to_ocrmask);
806 #ifdef CONFIG_REGULATOR
809 * mmc_regulator_get_ocrmask - return mask of supported voltages
810 * @supply: regulator to use
812 * This returns either a negative errno, or a mask of voltages that
813 * can be provided to MMC/SD/SDIO devices using the specified voltage
814 * regulator. This would normally be called before registering the
817 int mmc_regulator_get_ocrmask(struct regulator *supply)
823 count = regulator_count_voltages(supply);
827 for (i = 0; i < count; i++) {
831 vdd_uV = regulator_list_voltage(supply, i);
835 vdd_mV = vdd_uV / 1000;
836 result |= mmc_vddrange_to_ocrmask(vdd_mV, vdd_mV);
841 EXPORT_SYMBOL(mmc_regulator_get_ocrmask);
844 * mmc_regulator_set_ocr - set regulator to match host->ios voltage
845 * @mmc: the host to regulate
846 * @supply: regulator to use
847 * @vdd_bit: zero for power off, else a bit number (host->ios.vdd)
849 * Returns zero on success, else negative errno.
851 * MMC host drivers may use this to enable or disable a regulator using
852 * a particular supply voltage. This would normally be called from the
855 int mmc_regulator_set_ocr(struct mmc_host *mmc,
856 struct regulator *supply,
857 unsigned short vdd_bit)
866 /* REVISIT mmc_vddrange_to_ocrmask() may have set some
867 * bits this regulator doesn't quite support ... don't
868 * be too picky, most cards and regulators are OK with
869 * a 0.1V range goof (it's a small error percentage).
871 tmp = vdd_bit - ilog2(MMC_VDD_165_195);
873 min_uV = 1650 * 1000;
874 max_uV = 1950 * 1000;
876 min_uV = 1900 * 1000 + tmp * 100 * 1000;
877 max_uV = min_uV + 100 * 1000;
880 /* avoid needless changes to this voltage; the regulator
881 * might not allow this operation
883 voltage = regulator_get_voltage(supply);
886 else if (voltage < min_uV || voltage > max_uV)
887 result = regulator_set_voltage(supply, min_uV, max_uV);
891 if (result == 0 && !mmc->regulator_enabled) {
892 result = regulator_enable(supply);
894 mmc->regulator_enabled = true;
896 } else if (mmc->regulator_enabled) {
897 result = regulator_disable(supply);
899 mmc->regulator_enabled = false;
903 dev_err(mmc_dev(mmc),
904 "could not set regulator OCR (%d)\n", result);
907 EXPORT_SYMBOL(mmc_regulator_set_ocr);
909 #endif /* CONFIG_REGULATOR */
912 * Mask off any voltages we don't support and select
915 u32 mmc_select_voltage(struct mmc_host *host, u32 ocr)
919 ocr &= host->ocr_avail;
930 pr_warning("%s: host doesn't support card's voltages\n",
939 * Select timing parameters for host.
941 void mmc_set_timing(struct mmc_host *host, unsigned int timing)
943 host->ios.timing = timing;
948 * Apply power to the MMC stack. This is a two-stage process.
949 * First, we enable power to the card without the clock running.
950 * We then wait a bit for the power to stabilise. Finally,
951 * enable the bus drivers and clock to the card.
953 * We must _NOT_ enable the clock prior to power stablising.
955 * If a host does all the power sequencing itself, ignore the
956 * initial MMC_POWER_UP stage.
958 static void mmc_power_up(struct mmc_host *host)
962 /* If ocr is set, we use it */
964 bit = ffs(host->ocr) - 1;
966 bit = fls(host->ocr_avail) - 1;
969 if (mmc_host_is_spi(host)) {
970 host->ios.chip_select = MMC_CS_HIGH;
971 host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
973 host->ios.chip_select = MMC_CS_DONTCARE;
974 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
976 host->ios.power_mode = MMC_POWER_UP;
977 host->ios.bus_width = MMC_BUS_WIDTH_1;
978 host->ios.timing = MMC_TIMING_LEGACY;
982 * This delay should be sufficient to allow the power supply
983 * to reach the minimum voltage.
987 host->ios.clock = host->f_init;
989 host->ios.power_mode = MMC_POWER_ON;
993 * This delay must be at least 74 clock sizes, or 1 ms, or the
994 * time required to reach a stable voltage.
999 static void mmc_power_off(struct mmc_host *host)
1001 host->ios.clock = 0;
1003 if (!mmc_host_is_spi(host)) {
1004 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
1005 host->ios.chip_select = MMC_CS_DONTCARE;
1007 host->ios.power_mode = MMC_POWER_OFF;
1008 host->ios.bus_width = MMC_BUS_WIDTH_1;
1009 host->ios.timing = MMC_TIMING_LEGACY;
1014 * Cleanup when the last reference to the bus operator is dropped.
1016 static void __mmc_release_bus(struct mmc_host *host)
1019 BUG_ON(host->bus_refs);
1020 BUG_ON(!host->bus_dead);
1022 host->bus_ops = NULL;
1026 * Increase reference count of bus operator
1028 static inline void mmc_bus_get(struct mmc_host *host)
1030 unsigned long flags;
1032 spin_lock_irqsave(&host->lock, flags);
1034 spin_unlock_irqrestore(&host->lock, flags);
1038 * Decrease reference count of bus operator and free it if
1039 * it is the last reference.
1041 static inline void mmc_bus_put(struct mmc_host *host)
1043 unsigned long flags;
1045 spin_lock_irqsave(&host->lock, flags);
1047 if ((host->bus_refs == 0) && host->bus_ops)
1048 __mmc_release_bus(host);
1049 spin_unlock_irqrestore(&host->lock, flags);
1053 * Assign a mmc bus handler to a host. Only one bus handler may control a
1054 * host at any given time.
1056 void mmc_attach_bus(struct mmc_host *host, const struct mmc_bus_ops *ops)
1058 unsigned long flags;
1063 WARN_ON(!host->claimed);
1065 spin_lock_irqsave(&host->lock, flags);
1067 BUG_ON(host->bus_ops);
1068 BUG_ON(host->bus_refs);
1070 host->bus_ops = ops;
1074 spin_unlock_irqrestore(&host->lock, flags);
1078 * Remove the current bus handler from a host. Assumes that there are
1079 * no interesting cards left, so the bus is powered down.
1081 void mmc_detach_bus(struct mmc_host *host)
1083 unsigned long flags;
1087 WARN_ON(!host->claimed);
1088 WARN_ON(!host->bus_ops);
1090 spin_lock_irqsave(&host->lock, flags);
1094 spin_unlock_irqrestore(&host->lock, flags);
1096 mmc_power_off(host);
1102 * mmc_detect_change - process change of state on a MMC socket
1103 * @host: host which changed state.
1104 * @delay: optional delay to wait before detection (jiffies)
1106 * MMC drivers should call this when they detect a card has been
1107 * inserted or removed. The MMC layer will confirm that any
1108 * present card is still functional, and initialize any newly
1111 void mmc_detect_change(struct mmc_host *host, unsigned long delay)
1113 #ifdef CONFIG_MMC_DEBUG
1114 unsigned long flags;
1115 spin_lock_irqsave(&host->lock, flags);
1116 WARN_ON(host->removed);
1117 spin_unlock_irqrestore(&host->lock, flags);
1120 mmc_schedule_delayed_work(&host->detect, delay);
1123 EXPORT_SYMBOL(mmc_detect_change);
1125 void mmc_init_erase(struct mmc_card *card)
1129 if (is_power_of_2(card->erase_size))
1130 card->erase_shift = ffs(card->erase_size) - 1;
1132 card->erase_shift = 0;
1135 * It is possible to erase an arbitrarily large area of an SD or MMC
1136 * card. That is not desirable because it can take a long time
1137 * (minutes) potentially delaying more important I/O, and also the
1138 * timeout calculations become increasingly hugely over-estimated.
1139 * Consequently, 'pref_erase' is defined as a guide to limit erases
1140 * to that size and alignment.
1142 * For SD cards that define Allocation Unit size, limit erases to one
1143 * Allocation Unit at a time. For MMC cards that define High Capacity
1144 * Erase Size, whether it is switched on or not, limit to that size.
1145 * Otherwise just have a stab at a good value. For modern cards it
1146 * will end up being 4MiB. Note that if the value is too small, it
1147 * can end up taking longer to erase.
1149 if (mmc_card_sd(card) && card->ssr.au) {
1150 card->pref_erase = card->ssr.au;
1151 card->erase_shift = ffs(card->ssr.au) - 1;
1152 } else if (card->ext_csd.hc_erase_size) {
1153 card->pref_erase = card->ext_csd.hc_erase_size;
1155 sz = (card->csd.capacity << (card->csd.read_blkbits - 9)) >> 11;
1157 card->pref_erase = 512 * 1024 / 512;
1159 card->pref_erase = 1024 * 1024 / 512;
1161 card->pref_erase = 2 * 1024 * 1024 / 512;
1163 card->pref_erase = 4 * 1024 * 1024 / 512;
1164 if (card->pref_erase < card->erase_size)
1165 card->pref_erase = card->erase_size;
1167 sz = card->pref_erase % card->erase_size;
1169 card->pref_erase += card->erase_size - sz;
1174 static void mmc_set_mmc_erase_timeout(struct mmc_card *card,
1175 struct mmc_command *cmd,
1176 unsigned int arg, unsigned int qty)
1178 unsigned int erase_timeout;
1180 if (card->ext_csd.erase_group_def & 1) {
1181 /* High Capacity Erase Group Size uses HC timeouts */
1182 if (arg == MMC_TRIM_ARG)
1183 erase_timeout = card->ext_csd.trim_timeout;
1185 erase_timeout = card->ext_csd.hc_erase_timeout;
1187 /* CSD Erase Group Size uses write timeout */
1188 unsigned int mult = (10 << card->csd.r2w_factor);
1189 unsigned int timeout_clks = card->csd.tacc_clks * mult;
1190 unsigned int timeout_us;
1192 /* Avoid overflow: e.g. tacc_ns=80000000 mult=1280 */
1193 if (card->csd.tacc_ns < 1000000)
1194 timeout_us = (card->csd.tacc_ns * mult) / 1000;
1196 timeout_us = (card->csd.tacc_ns / 1000) * mult;
1199 * ios.clock is only a target. The real clock rate might be
1200 * less but not that much less, so fudge it by multiplying by 2.
1203 timeout_us += (timeout_clks * 1000) /
1204 (card->host->ios.clock / 1000);
1206 erase_timeout = timeout_us / 1000;
1209 * Theoretically, the calculation could underflow so round up
1210 * to 1ms in that case.
1216 /* Multiplier for secure operations */
1217 if (arg & MMC_SECURE_ARGS) {
1218 if (arg == MMC_SECURE_ERASE_ARG)
1219 erase_timeout *= card->ext_csd.sec_erase_mult;
1221 erase_timeout *= card->ext_csd.sec_trim_mult;
1224 erase_timeout *= qty;
1227 * Ensure at least a 1 second timeout for SPI as per
1228 * 'mmc_set_data_timeout()'
1230 if (mmc_host_is_spi(card->host) && erase_timeout < 1000)
1231 erase_timeout = 1000;
1233 cmd->erase_timeout = erase_timeout;
1236 static void mmc_set_sd_erase_timeout(struct mmc_card *card,
1237 struct mmc_command *cmd, unsigned int arg,
1240 if (card->ssr.erase_timeout) {
1241 /* Erase timeout specified in SD Status Register (SSR) */
1242 cmd->erase_timeout = card->ssr.erase_timeout * qty +
1243 card->ssr.erase_offset;
1246 * Erase timeout not specified in SD Status Register (SSR) so
1247 * use 250ms per write block.
1249 cmd->erase_timeout = 250 * qty;
1252 /* Must not be less than 1 second */
1253 if (cmd->erase_timeout < 1000)
1254 cmd->erase_timeout = 1000;
1257 static void mmc_set_erase_timeout(struct mmc_card *card,
1258 struct mmc_command *cmd, unsigned int arg,
1261 if (mmc_card_sd(card))
1262 mmc_set_sd_erase_timeout(card, cmd, arg, qty);
1264 mmc_set_mmc_erase_timeout(card, cmd, arg, qty);
1267 static int mmc_do_erase(struct mmc_card *card, unsigned int from,
1268 unsigned int to, unsigned int arg)
1270 struct mmc_command cmd;
1271 unsigned int qty = 0;
1275 * qty is used to calculate the erase timeout which depends on how many
1276 * erase groups (or allocation units in SD terminology) are affected.
1277 * We count erasing part of an erase group as one erase group.
1278 * For SD, the allocation units are always a power of 2. For MMC, the
1279 * erase group size is almost certainly also power of 2, but it does not
1280 * seem to insist on that in the JEDEC standard, so we fall back to
1281 * division in that case. SD may not specify an allocation unit size,
1282 * in which case the timeout is based on the number of write blocks.
1284 * Note that the timeout for secure trim 2 will only be correct if the
1285 * number of erase groups specified is the same as the total of all
1286 * preceding secure trim 1 commands. Since the power may have been
1287 * lost since the secure trim 1 commands occurred, it is generally
1288 * impossible to calculate the secure trim 2 timeout correctly.
1290 if (card->erase_shift)
1291 qty += ((to >> card->erase_shift) -
1292 (from >> card->erase_shift)) + 1;
1293 else if (mmc_card_sd(card))
1294 qty += to - from + 1;
1296 qty += ((to / card->erase_size) -
1297 (from / card->erase_size)) + 1;
1299 if (!mmc_card_blockaddr(card)) {
1304 memset(&cmd, 0, sizeof(struct mmc_command));
1305 if (mmc_card_sd(card))
1306 cmd.opcode = SD_ERASE_WR_BLK_START;
1308 cmd.opcode = MMC_ERASE_GROUP_START;
1310 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
1311 err = mmc_wait_for_cmd(card->host, &cmd, 0);
1313 printk(KERN_ERR "mmc_erase: group start error %d, "
1314 "status %#x\n", err, cmd.resp[0]);
1319 memset(&cmd, 0, sizeof(struct mmc_command));
1320 if (mmc_card_sd(card))
1321 cmd.opcode = SD_ERASE_WR_BLK_END;
1323 cmd.opcode = MMC_ERASE_GROUP_END;
1325 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
1326 err = mmc_wait_for_cmd(card->host, &cmd, 0);
1328 printk(KERN_ERR "mmc_erase: group end error %d, status %#x\n",
1334 memset(&cmd, 0, sizeof(struct mmc_command));
1335 cmd.opcode = MMC_ERASE;
1337 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1338 mmc_set_erase_timeout(card, &cmd, arg, qty);
1339 err = mmc_wait_for_cmd(card->host, &cmd, 0);
1341 printk(KERN_ERR "mmc_erase: erase error %d, status %#x\n",
1347 if (mmc_host_is_spi(card->host))
1351 memset(&cmd, 0, sizeof(struct mmc_command));
1352 cmd.opcode = MMC_SEND_STATUS;
1353 cmd.arg = card->rca << 16;
1354 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
1355 /* Do not retry else we can't see errors */
1356 err = mmc_wait_for_cmd(card->host, &cmd, 0);
1357 if (err || (cmd.resp[0] & 0xFDF92000)) {
1358 printk(KERN_ERR "error %d requesting status %#x\n",
1363 } while (!(cmd.resp[0] & R1_READY_FOR_DATA) ||
1364 R1_CURRENT_STATE(cmd.resp[0]) == 7);
1370 * mmc_erase - erase sectors.
1371 * @card: card to erase
1372 * @from: first sector to erase
1373 * @nr: number of sectors to erase
1374 * @arg: erase command argument (SD supports only %MMC_ERASE_ARG)
1376 * Caller must claim host before calling this function.
1378 int mmc_erase(struct mmc_card *card, unsigned int from, unsigned int nr,
1381 unsigned int rem, to = from + nr;
1383 if (!(card->host->caps & MMC_CAP_ERASE) ||
1384 !(card->csd.cmdclass & CCC_ERASE))
1387 if (!card->erase_size)
1390 if (mmc_card_sd(card) && arg != MMC_ERASE_ARG)
1393 if ((arg & MMC_SECURE_ARGS) &&
1394 !(card->ext_csd.sec_feature_support & EXT_CSD_SEC_ER_EN))
1397 if ((arg & MMC_TRIM_ARGS) &&
1398 !(card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN))
1401 if (arg == MMC_SECURE_ERASE_ARG) {
1402 if (from % card->erase_size || nr % card->erase_size)
1406 if (arg == MMC_ERASE_ARG) {
1407 rem = from % card->erase_size;
1409 rem = card->erase_size - rem;
1416 rem = nr % card->erase_size;
1429 /* 'from' and 'to' are inclusive */
1432 return mmc_do_erase(card, from, to, arg);
1434 EXPORT_SYMBOL(mmc_erase);
1436 int mmc_can_erase(struct mmc_card *card)
1438 if ((card->host->caps & MMC_CAP_ERASE) &&
1439 (card->csd.cmdclass & CCC_ERASE) && card->erase_size)
1443 EXPORT_SYMBOL(mmc_can_erase);
1445 int mmc_can_trim(struct mmc_card *card)
1447 if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN)
1451 EXPORT_SYMBOL(mmc_can_trim);
1453 int mmc_can_secure_erase_trim(struct mmc_card *card)
1455 if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_ER_EN)
1459 EXPORT_SYMBOL(mmc_can_secure_erase_trim);
1461 int mmc_erase_group_aligned(struct mmc_card *card, unsigned int from,
1464 if (!card->erase_size)
1466 if (from % card->erase_size || nr % card->erase_size)
1470 EXPORT_SYMBOL(mmc_erase_group_aligned);
1472 int mmc_set_blocklen(struct mmc_card *card, unsigned int blocklen)
1474 struct mmc_command cmd;
1476 if (mmc_card_blockaddr(card) || mmc_card_ddr_mode(card))
1479 memset(&cmd, 0, sizeof(struct mmc_command));
1480 cmd.opcode = MMC_SET_BLOCKLEN;
1482 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
1483 return mmc_wait_for_cmd(card->host, &cmd, 5);
1485 EXPORT_SYMBOL(mmc_set_blocklen);
1487 void mmc_rescan(struct work_struct *work)
1489 struct mmc_host *host =
1490 container_of(work, struct mmc_host, detect.work);
1493 unsigned long flags;
1495 const unsigned freqs[] = { 400000, 300000, 200000, 100000 };
1497 spin_lock_irqsave(&host->lock, flags);
1499 if (host->rescan_disable) {
1500 spin_unlock_irqrestore(&host->lock, flags);
1504 spin_unlock_irqrestore(&host->lock, flags);
1509 /* if there is a card registered, check whether it is still present */
1510 if ((host->bus_ops != NULL) && host->bus_ops->detect && !host->bus_dead)
1511 host->bus_ops->detect(host);
1518 /* if there still is a card present, stop here */
1519 if (host->bus_ops != NULL) {
1524 /* detect a newly inserted card */
1527 * Only we can add a new handler, so it's safe to
1528 * release the lock here.
1532 if (host->ops->get_cd && host->ops->get_cd(host) == 0)
1535 for (i = 0; i < ARRAY_SIZE(freqs); i++) {
1536 mmc_claim_host(host);
1538 if (freqs[i] >= host->f_min)
1539 host->f_init = freqs[i];
1540 else if (!i || freqs[i-1] > host->f_min)
1541 host->f_init = host->f_min;
1543 mmc_release_host(host);
1546 #ifdef CONFIG_MMC_DEBUG
1547 pr_info("%s: %s: trying to init card at %u Hz\n",
1548 mmc_hostname(host), __func__, host->f_init);
1554 mmc_send_if_cond(host, host->ocr_avail);
1557 * First we search for SDIO...
1559 err = mmc_send_io_op_cond(host, 0, &ocr);
1561 if (mmc_attach_sdio(host, ocr)) {
1562 mmc_claim_host(host);
1564 * Try SDMEM (but not MMC) even if SDIO
1567 if (mmc_send_app_op_cond(host, 0, &ocr))
1570 if (mmc_attach_sd(host, ocr))
1571 mmc_power_off(host);
1577 * ...then normal SD...
1579 err = mmc_send_app_op_cond(host, 0, &ocr);
1581 if (mmc_attach_sd(host, ocr))
1582 mmc_power_off(host);
1587 * ...and finally MMC.
1589 err = mmc_send_op_cond(host, 0, &ocr);
1591 if (mmc_attach_mmc(host, ocr))
1592 mmc_power_off(host);
1597 mmc_release_host(host);
1598 mmc_power_off(host);
1601 if (host->caps & MMC_CAP_NEEDS_POLL)
1602 mmc_schedule_delayed_work(&host->detect, HZ);
1605 void mmc_start_host(struct mmc_host *host)
1607 mmc_power_off(host);
1608 mmc_detect_change(host, 0);
1611 void mmc_stop_host(struct mmc_host *host)
1613 #ifdef CONFIG_MMC_DEBUG
1614 unsigned long flags;
1615 spin_lock_irqsave(&host->lock, flags);
1617 spin_unlock_irqrestore(&host->lock, flags);
1620 if (host->caps & MMC_CAP_DISABLE)
1621 cancel_delayed_work(&host->disable);
1622 cancel_delayed_work_sync(&host->detect);
1623 mmc_flush_scheduled_work();
1625 /* clear pm flags now and let card drivers set them as needed */
1629 if (host->bus_ops && !host->bus_dead) {
1630 if (host->bus_ops->remove)
1631 host->bus_ops->remove(host);
1633 mmc_claim_host(host);
1634 mmc_detach_bus(host);
1635 mmc_release_host(host);
1643 mmc_power_off(host);
1646 int mmc_power_save_host(struct mmc_host *host)
1652 if (!host->bus_ops || host->bus_dead || !host->bus_ops->power_restore) {
1657 if (host->bus_ops->power_save)
1658 ret = host->bus_ops->power_save(host);
1662 mmc_power_off(host);
1666 EXPORT_SYMBOL(mmc_power_save_host);
1668 int mmc_power_restore_host(struct mmc_host *host)
1674 if (!host->bus_ops || host->bus_dead || !host->bus_ops->power_restore) {
1680 ret = host->bus_ops->power_restore(host);
1686 EXPORT_SYMBOL(mmc_power_restore_host);
1688 int mmc_card_awake(struct mmc_host *host)
1694 if (host->bus_ops && !host->bus_dead && host->bus_ops->awake)
1695 err = host->bus_ops->awake(host);
1701 EXPORT_SYMBOL(mmc_card_awake);
1703 int mmc_card_sleep(struct mmc_host *host)
1709 if (host->bus_ops && !host->bus_dead && host->bus_ops->awake)
1710 err = host->bus_ops->sleep(host);
1716 EXPORT_SYMBOL(mmc_card_sleep);
1718 int mmc_card_can_sleep(struct mmc_host *host)
1720 struct mmc_card *card = host->card;
1722 if (card && mmc_card_mmc(card) && card->ext_csd.rev >= 3)
1726 EXPORT_SYMBOL(mmc_card_can_sleep);
1731 * mmc_suspend_host - suspend a host
1734 int mmc_suspend_host(struct mmc_host *host)
1738 if (host->caps & MMC_CAP_DISABLE)
1739 cancel_delayed_work(&host->disable);
1740 cancel_delayed_work(&host->detect);
1741 mmc_flush_scheduled_work();
1744 if (host->bus_ops && !host->bus_dead) {
1745 if (host->bus_ops->suspend)
1746 err = host->bus_ops->suspend(host);
1747 if (err == -ENOSYS || !host->bus_ops->resume) {
1749 * We simply "remove" the card in this case.
1750 * It will be redetected on resume.
1752 if (host->bus_ops->remove)
1753 host->bus_ops->remove(host);
1754 mmc_claim_host(host);
1755 mmc_detach_bus(host);
1756 mmc_release_host(host);
1763 if (!err && !(host->pm_flags & MMC_PM_KEEP_POWER))
1764 mmc_power_off(host);
1769 EXPORT_SYMBOL(mmc_suspend_host);
1772 * mmc_resume_host - resume a previously suspended host
1775 int mmc_resume_host(struct mmc_host *host)
1780 if (host->bus_ops && !host->bus_dead) {
1781 if (!(host->pm_flags & MMC_PM_KEEP_POWER)) {
1783 mmc_select_voltage(host, host->ocr);
1785 BUG_ON(!host->bus_ops->resume);
1786 err = host->bus_ops->resume(host);
1788 printk(KERN_WARNING "%s: error %d during resume "
1789 "(card was removed?)\n",
1790 mmc_hostname(host), err);
1798 EXPORT_SYMBOL(mmc_resume_host);
1800 /* Do the card removal on suspend if card is assumed removeable
1801 * Do that in pm notifier while userspace isn't yet frozen, so we will be able
1804 int mmc_pm_notify(struct notifier_block *notify_block,
1805 unsigned long mode, void *unused)
1807 struct mmc_host *host = container_of(
1808 notify_block, struct mmc_host, pm_notify);
1809 unsigned long flags;
1813 case PM_HIBERNATION_PREPARE:
1814 case PM_SUSPEND_PREPARE:
1816 spin_lock_irqsave(&host->lock, flags);
1817 host->rescan_disable = 1;
1818 spin_unlock_irqrestore(&host->lock, flags);
1819 cancel_delayed_work_sync(&host->detect);
1821 if (!host->bus_ops || host->bus_ops->suspend)
1824 mmc_claim_host(host);
1826 if (host->bus_ops->remove)
1827 host->bus_ops->remove(host);
1829 mmc_detach_bus(host);
1830 mmc_release_host(host);
1834 case PM_POST_SUSPEND:
1835 case PM_POST_HIBERNATION:
1836 case PM_POST_RESTORE:
1838 spin_lock_irqsave(&host->lock, flags);
1839 host->rescan_disable = 0;
1840 spin_unlock_irqrestore(&host->lock, flags);
1841 mmc_detect_change(host, 0);
1849 static int __init mmc_init(void)
1853 workqueue = alloc_ordered_workqueue("kmmcd", 0);
1857 ret = mmc_register_bus();
1859 goto destroy_workqueue;
1861 ret = mmc_register_host_class();
1863 goto unregister_bus;
1865 ret = sdio_register_bus();
1867 goto unregister_host_class;
1871 unregister_host_class:
1872 mmc_unregister_host_class();
1874 mmc_unregister_bus();
1876 destroy_workqueue(workqueue);
1881 static void __exit mmc_exit(void)
1883 sdio_unregister_bus();
1884 mmc_unregister_host_class();
1885 mmc_unregister_bus();
1886 destroy_workqueue(workqueue);
1889 subsys_initcall(mmc_init);
1890 module_exit(mmc_exit);
1892 MODULE_LICENSE("GPL");