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>
26 #include <linux/wakelock.h>
28 #include <linux/mmc/card.h>
29 #include <linux/mmc/host.h>
30 #include <linux/mmc/mmc.h>
31 #include <linux/mmc/sd.h>
42 static struct workqueue_struct *workqueue;
43 static struct wake_lock mmc_delayed_work_wake_lock;
46 * Enabling software CRCs on the data blocks can be a significant (30%)
47 * performance cost, and for other reasons may not always be desired.
48 * So we allow it it to be disabled.
51 module_param(use_spi_crc, bool, 0);
54 * We normally treat cards as removed during suspend if they are not
55 * known to be on a non-removable bus, to avoid the risk of writing
56 * back data to a different card after resume. Allow this to be
57 * overridden if necessary.
59 #ifdef CONFIG_MMC_UNSAFE_RESUME
60 int mmc_assume_removable;
62 int mmc_assume_removable = 1;
64 EXPORT_SYMBOL(mmc_assume_removable);
65 module_param_named(removable, mmc_assume_removable, bool, 0644);
68 "MMC/SD cards are removable and may be removed during suspend");
71 * Internal function. Schedule delayed work in the MMC work queue.
73 static int mmc_schedule_delayed_work(struct delayed_work *work,
76 wake_lock(&mmc_delayed_work_wake_lock);
77 return queue_delayed_work(workqueue, work, delay);
81 * Internal function. Flush all scheduled work from the MMC work queue.
83 static void mmc_flush_scheduled_work(void)
85 flush_workqueue(workqueue);
89 * mmc_request_done - finish processing an MMC request
90 * @host: MMC host which completed request
91 * @mrq: MMC request which request
93 * MMC drivers should call this function when they have completed
94 * their processing of a request.
96 void mmc_request_done(struct mmc_host *host, struct mmc_request *mrq)
98 struct mmc_command *cmd = mrq->cmd;
101 if (err && cmd->retries && mmc_host_is_spi(host)) {
102 if (cmd->resp[0] & R1_SPI_ILLEGAL_COMMAND)
106 if (err && cmd->retries) {
107 pr_debug("%s: req failed (CMD%u): %d, retrying...\n",
108 mmc_hostname(host), cmd->opcode, err);
112 host->ops->request(host, mrq);
114 led_trigger_event(host->led, LED_OFF);
116 pr_debug("%s: req done (CMD%u): %d: %08x %08x %08x %08x\n",
117 mmc_hostname(host), cmd->opcode, err,
118 cmd->resp[0], cmd->resp[1],
119 cmd->resp[2], cmd->resp[3]);
122 pr_debug("%s: %d bytes transferred: %d\n",
124 mrq->data->bytes_xfered, mrq->data->error);
128 pr_debug("%s: (CMD%u): %d: %08x %08x %08x %08x\n",
129 mmc_hostname(host), mrq->stop->opcode,
131 mrq->stop->resp[0], mrq->stop->resp[1],
132 mrq->stop->resp[2], mrq->stop->resp[3]);
138 mmc_host_clk_gate(host);
142 EXPORT_SYMBOL(mmc_request_done);
145 mmc_start_request(struct mmc_host *host, struct mmc_request *mrq)
147 #ifdef CONFIG_MMC_DEBUG
149 struct scatterlist *sg;
152 pr_debug("%s: starting CMD%u arg %08x flags %08x\n",
153 mmc_hostname(host), mrq->cmd->opcode,
154 mrq->cmd->arg, mrq->cmd->flags);
157 pr_debug("%s: blksz %d blocks %d flags %08x "
158 "tsac %d ms nsac %d\n",
159 mmc_hostname(host), mrq->data->blksz,
160 mrq->data->blocks, mrq->data->flags,
161 mrq->data->timeout_ns / 1000000,
162 mrq->data->timeout_clks);
166 pr_debug("%s: CMD%u arg %08x flags %08x\n",
167 mmc_hostname(host), mrq->stop->opcode,
168 mrq->stop->arg, mrq->stop->flags);
171 WARN_ON(!host->claimed);
173 led_trigger_event(host->led, LED_FULL);
178 BUG_ON(mrq->data->blksz > host->max_blk_size);
179 BUG_ON(mrq->data->blocks > host->max_blk_count);
180 BUG_ON(mrq->data->blocks * mrq->data->blksz >
183 #ifdef CONFIG_MMC_DEBUG
185 for_each_sg(mrq->data->sg, sg, mrq->data->sg_len, i)
187 BUG_ON(sz != mrq->data->blocks * mrq->data->blksz);
190 mrq->cmd->data = mrq->data;
191 mrq->data->error = 0;
192 mrq->data->mrq = mrq;
194 mrq->data->stop = mrq->stop;
195 mrq->stop->error = 0;
196 mrq->stop->mrq = mrq;
199 mmc_host_clk_ungate(host);
200 host->ops->request(host, mrq);
203 static void mmc_wait_done(struct mmc_request *mrq)
205 complete(mrq->done_data);
209 * mmc_wait_for_req - start a request and wait for completion
210 * @host: MMC host to start command
211 * @mrq: MMC request to start
213 * Start a new MMC custom command request for a host, and wait
214 * for the command to complete. Does not attempt to parse the
217 void mmc_wait_for_req(struct mmc_host *host, struct mmc_request *mrq)
219 DECLARE_COMPLETION_ONSTACK(complete);
221 mrq->done_data = &complete;
222 mrq->done = mmc_wait_done;
224 mmc_start_request(host, mrq);
226 wait_for_completion(&complete);
229 EXPORT_SYMBOL(mmc_wait_for_req);
232 * mmc_wait_for_cmd - start a command and wait for completion
233 * @host: MMC host to start command
234 * @cmd: MMC command to start
235 * @retries: maximum number of retries
237 * Start a new MMC command for a host, and wait for the command
238 * to complete. Return any error that occurred while the command
239 * was executing. Do not attempt to parse the response.
241 int mmc_wait_for_cmd(struct mmc_host *host, struct mmc_command *cmd, int retries)
243 struct mmc_request mrq;
245 WARN_ON(!host->claimed);
247 memset(&mrq, 0, sizeof(struct mmc_request));
249 memset(cmd->resp, 0, sizeof(cmd->resp));
250 cmd->retries = retries;
255 mmc_wait_for_req(host, &mrq);
260 EXPORT_SYMBOL(mmc_wait_for_cmd);
263 * mmc_set_data_timeout - set the timeout for a data command
264 * @data: data phase for command
265 * @card: the MMC card associated with the data transfer
267 * Computes the data timeout parameters according to the
268 * correct algorithm given the card type.
270 void mmc_set_data_timeout(struct mmc_data *data, const struct mmc_card *card)
275 * SDIO cards only define an upper 1 s limit on access.
277 if (mmc_card_sdio(card)) {
278 data->timeout_ns = 1000000000;
279 data->timeout_clks = 0;
284 * SD cards use a 100 multiplier rather than 10
286 mult = mmc_card_sd(card) ? 100 : 10;
289 * Scale up the multiplier (and therefore the timeout) by
290 * the r2w factor for writes.
292 if (data->flags & MMC_DATA_WRITE)
293 mult <<= card->csd.r2w_factor;
295 data->timeout_ns = card->csd.tacc_ns * mult;
296 data->timeout_clks = card->csd.tacc_clks * mult;
299 * SD cards also have an upper limit on the timeout.
301 if (mmc_card_sd(card)) {
302 unsigned int timeout_us, limit_us;
304 timeout_us = data->timeout_ns / 1000;
305 if (mmc_host_clk_rate(card->host))
306 timeout_us += data->timeout_clks * 1000 /
307 (mmc_host_clk_rate(card->host) / 1000);
309 if (data->flags & MMC_DATA_WRITE)
311 * The limit is really 250 ms, but that is
312 * insufficient for some crappy cards.
319 * SDHC cards always use these fixed values.
321 if (timeout_us > limit_us || mmc_card_blockaddr(card)) {
322 data->timeout_ns = limit_us * 1000;
323 data->timeout_clks = 0;
327 * Some cards need very high timeouts if driven in SPI mode.
328 * The worst observed timeout was 900ms after writing a
329 * continuous stream of data until the internal logic
332 if (mmc_host_is_spi(card->host)) {
333 if (data->flags & MMC_DATA_WRITE) {
334 if (data->timeout_ns < 1000000000)
335 data->timeout_ns = 1000000000; /* 1s */
337 if (data->timeout_ns < 100000000)
338 data->timeout_ns = 100000000; /* 100ms */
342 EXPORT_SYMBOL(mmc_set_data_timeout);
345 * mmc_align_data_size - pads a transfer size to a more optimal value
346 * @card: the MMC card associated with the data transfer
347 * @sz: original transfer size
349 * Pads the original data size with a number of extra bytes in
350 * order to avoid controller bugs and/or performance hits
351 * (e.g. some controllers revert to PIO for certain sizes).
353 * Returns the improved size, which might be unmodified.
355 * Note that this function is only relevant when issuing a
356 * single scatter gather entry.
358 unsigned int mmc_align_data_size(struct mmc_card *card, unsigned int sz)
361 * FIXME: We don't have a system for the controller to tell
362 * the core about its problems yet, so for now we just 32-bit
365 sz = ((sz + 3) / 4) * 4;
369 EXPORT_SYMBOL(mmc_align_data_size);
372 * mmc_host_enable - enable a host.
373 * @host: mmc host to enable
375 * Hosts that support power saving can use the 'enable' and 'disable'
376 * methods to exit and enter power saving states. For more information
377 * see comments for struct mmc_host_ops.
379 int mmc_host_enable(struct mmc_host *host)
381 if (!(host->caps & MMC_CAP_DISABLE))
384 if (host->en_dis_recurs)
387 if (host->nesting_cnt++)
390 cancel_delayed_work_sync(&host->disable);
395 if (host->ops->enable) {
398 host->en_dis_recurs = 1;
399 err = host->ops->enable(host);
400 host->en_dis_recurs = 0;
403 pr_debug("%s: enable error %d\n",
404 mmc_hostname(host), err);
411 EXPORT_SYMBOL(mmc_host_enable);
413 static int mmc_host_do_disable(struct mmc_host *host, int lazy)
415 if (host->ops->disable) {
418 host->en_dis_recurs = 1;
419 err = host->ops->disable(host, lazy);
420 host->en_dis_recurs = 0;
423 pr_debug("%s: disable error %d\n",
424 mmc_hostname(host), err);
428 unsigned long delay = msecs_to_jiffies(err);
430 mmc_schedule_delayed_work(&host->disable, delay);
438 * mmc_host_disable - disable a host.
439 * @host: mmc host to disable
441 * Hosts that support power saving can use the 'enable' and 'disable'
442 * methods to exit and enter power saving states. For more information
443 * see comments for struct mmc_host_ops.
445 int mmc_host_disable(struct mmc_host *host)
449 if (!(host->caps & MMC_CAP_DISABLE))
452 if (host->en_dis_recurs)
455 if (--host->nesting_cnt)
461 err = mmc_host_do_disable(host, 0);
464 EXPORT_SYMBOL(mmc_host_disable);
467 * __mmc_claim_host - exclusively claim a host
468 * @host: mmc host to claim
469 * @abort: whether or not the operation should be aborted
471 * Claim a host for a set of operations. If @abort is non null and
472 * dereference a non-zero value then this will return prematurely with
473 * that non-zero value without acquiring the lock. Returns zero
474 * with the lock held otherwise.
476 int __mmc_claim_host(struct mmc_host *host, atomic_t *abort)
478 DECLARE_WAITQUEUE(wait, current);
484 add_wait_queue(&host->wq, &wait);
485 spin_lock_irqsave(&host->lock, flags);
487 set_current_state(TASK_UNINTERRUPTIBLE);
488 stop = abort ? atomic_read(abort) : 0;
489 if (stop || !host->claimed || host->claimer == current)
491 spin_unlock_irqrestore(&host->lock, flags);
493 spin_lock_irqsave(&host->lock, flags);
495 set_current_state(TASK_RUNNING);
498 host->claimer = current;
499 host->claim_cnt += 1;
502 spin_unlock_irqrestore(&host->lock, flags);
503 remove_wait_queue(&host->wq, &wait);
505 mmc_host_enable(host);
509 EXPORT_SYMBOL(__mmc_claim_host);
512 * mmc_try_claim_host - try exclusively to claim a host
513 * @host: mmc host to claim
515 * Returns %1 if the host is claimed, %0 otherwise.
517 int mmc_try_claim_host(struct mmc_host *host)
519 int claimed_host = 0;
522 spin_lock_irqsave(&host->lock, flags);
523 if (!host->claimed || host->claimer == current) {
525 host->claimer = current;
526 host->claim_cnt += 1;
529 spin_unlock_irqrestore(&host->lock, flags);
532 EXPORT_SYMBOL(mmc_try_claim_host);
534 static void mmc_do_release_host(struct mmc_host *host)
538 spin_lock_irqsave(&host->lock, flags);
539 if (--host->claim_cnt) {
540 /* Release for nested claim */
541 spin_unlock_irqrestore(&host->lock, flags);
544 host->claimer = NULL;
545 spin_unlock_irqrestore(&host->lock, flags);
550 void mmc_host_deeper_disable(struct work_struct *work)
552 struct mmc_host *host =
553 container_of(work, struct mmc_host, disable.work);
555 /* If the host is claimed then we do not want to disable it anymore */
556 if (!mmc_try_claim_host(host))
558 mmc_host_do_disable(host, 1);
559 mmc_do_release_host(host);
563 * mmc_host_lazy_disable - lazily disable a host.
564 * @host: mmc host to disable
566 * Hosts that support power saving can use the 'enable' and 'disable'
567 * methods to exit and enter power saving states. For more information
568 * see comments for struct mmc_host_ops.
570 int mmc_host_lazy_disable(struct mmc_host *host)
572 if (!(host->caps & MMC_CAP_DISABLE))
575 if (host->en_dis_recurs)
578 if (--host->nesting_cnt)
584 if (host->disable_delay) {
585 mmc_schedule_delayed_work(&host->disable,
586 msecs_to_jiffies(host->disable_delay));
589 return mmc_host_do_disable(host, 1);
591 EXPORT_SYMBOL(mmc_host_lazy_disable);
594 * mmc_release_host - release a host
595 * @host: mmc host to release
597 * Release a MMC host, allowing others to claim the host
598 * for their operations.
600 void mmc_release_host(struct mmc_host *host)
602 WARN_ON(!host->claimed);
604 mmc_host_lazy_disable(host);
606 mmc_do_release_host(host);
609 EXPORT_SYMBOL(mmc_release_host);
612 * Internal function that does the actual ios call to the host driver,
613 * optionally printing some debug output.
615 static inline void mmc_set_ios(struct mmc_host *host)
617 struct mmc_ios *ios = &host->ios;
619 pr_debug("%s: clock %uHz busmode %u powermode %u cs %u Vdd %u "
620 "width %u timing %u\n",
621 mmc_hostname(host), ios->clock, ios->bus_mode,
622 ios->power_mode, ios->chip_select, ios->vdd,
623 ios->bus_width, ios->timing);
626 mmc_set_ungated(host);
627 host->ops->set_ios(host, ios);
631 * Control chip select pin on a host.
633 void mmc_set_chip_select(struct mmc_host *host, int mode)
635 host->ios.chip_select = mode;
640 * Sets the host clock to the highest possible frequency that
643 void mmc_set_clock(struct mmc_host *host, unsigned int hz)
645 WARN_ON(hz < host->f_min);
647 if (hz > host->f_max)
650 host->ios.clock = hz;
654 #ifdef CONFIG_MMC_CLKGATE
656 * This gates the clock by setting it to 0 Hz.
658 void mmc_gate_clock(struct mmc_host *host)
662 spin_lock_irqsave(&host->clk_lock, flags);
663 host->clk_old = host->ios.clock;
665 host->clk_gated = true;
666 spin_unlock_irqrestore(&host->clk_lock, flags);
671 * This restores the clock from gating by using the cached
674 void mmc_ungate_clock(struct mmc_host *host)
677 * We should previously have gated the clock, so the clock shall
678 * be 0 here! The clock may however be 0 during initialization,
679 * when some request operations are performed before setting
680 * the frequency. When ungate is requested in that situation
681 * we just ignore the call.
684 BUG_ON(host->ios.clock);
685 /* This call will also set host->clk_gated to false */
686 mmc_set_clock(host, host->clk_old);
690 void mmc_set_ungated(struct mmc_host *host)
695 * We've been given a new frequency while the clock is gated,
696 * so make sure we regard this as ungating it.
698 spin_lock_irqsave(&host->clk_lock, flags);
699 host->clk_gated = false;
700 spin_unlock_irqrestore(&host->clk_lock, flags);
704 void mmc_set_ungated(struct mmc_host *host)
710 * Change the bus mode (open drain/push-pull) of a host.
712 void mmc_set_bus_mode(struct mmc_host *host, unsigned int mode)
714 host->ios.bus_mode = mode;
719 * Change data bus width and DDR mode of a host.
721 void mmc_set_bus_width_ddr(struct mmc_host *host, unsigned int width,
724 host->ios.bus_width = width;
730 * Change data bus width of a host.
732 void mmc_set_bus_width(struct mmc_host *host, unsigned int width)
734 mmc_set_bus_width_ddr(host, width, MMC_SDR_MODE);
738 * mmc_vdd_to_ocrbitnum - Convert a voltage to the OCR bit number
740 * @low_bits: prefer low bits in boundary cases
742 * This function returns the OCR bit number according to the provided @vdd
743 * value. If conversion is not possible a negative errno value returned.
745 * Depending on the @low_bits flag the function prefers low or high OCR bits
746 * on boundary voltages. For example,
747 * with @low_bits = true, 3300 mV translates to ilog2(MMC_VDD_32_33);
748 * with @low_bits = false, 3300 mV translates to ilog2(MMC_VDD_33_34);
750 * Any value in the [1951:1999] range translates to the ilog2(MMC_VDD_20_21).
752 static int mmc_vdd_to_ocrbitnum(int vdd, bool low_bits)
754 const int max_bit = ilog2(MMC_VDD_35_36);
757 if (vdd < 1650 || vdd > 3600)
760 if (vdd >= 1650 && vdd <= 1950)
761 return ilog2(MMC_VDD_165_195);
766 /* Base 2000 mV, step 100 mV, bit's base 8. */
767 bit = (vdd - 2000) / 100 + 8;
774 * mmc_vddrange_to_ocrmask - Convert a voltage range to the OCR mask
775 * @vdd_min: minimum voltage value (mV)
776 * @vdd_max: maximum voltage value (mV)
778 * This function returns the OCR mask bits according to the provided @vdd_min
779 * and @vdd_max values. If conversion is not possible the function returns 0.
781 * Notes wrt boundary cases:
782 * This function sets the OCR bits for all boundary voltages, for example
783 * [3300:3400] range is translated to MMC_VDD_32_33 | MMC_VDD_33_34 |
784 * MMC_VDD_34_35 mask.
786 u32 mmc_vddrange_to_ocrmask(int vdd_min, int vdd_max)
790 if (vdd_max < vdd_min)
793 /* Prefer high bits for the boundary vdd_max values. */
794 vdd_max = mmc_vdd_to_ocrbitnum(vdd_max, false);
798 /* Prefer low bits for the boundary vdd_min values. */
799 vdd_min = mmc_vdd_to_ocrbitnum(vdd_min, true);
803 /* Fill the mask, from max bit to min bit. */
804 while (vdd_max >= vdd_min)
805 mask |= 1 << vdd_max--;
809 EXPORT_SYMBOL(mmc_vddrange_to_ocrmask);
811 #ifdef CONFIG_REGULATOR
814 * mmc_regulator_get_ocrmask - return mask of supported voltages
815 * @supply: regulator to use
817 * This returns either a negative errno, or a mask of voltages that
818 * can be provided to MMC/SD/SDIO devices using the specified voltage
819 * regulator. This would normally be called before registering the
822 int mmc_regulator_get_ocrmask(struct regulator *supply)
828 count = regulator_count_voltages(supply);
832 for (i = 0; i < count; i++) {
836 vdd_uV = regulator_list_voltage(supply, i);
840 vdd_mV = vdd_uV / 1000;
841 result |= mmc_vddrange_to_ocrmask(vdd_mV, vdd_mV);
846 EXPORT_SYMBOL(mmc_regulator_get_ocrmask);
849 * mmc_regulator_set_ocr - set regulator to match host->ios voltage
850 * @mmc: the host to regulate
851 * @supply: regulator to use
852 * @vdd_bit: zero for power off, else a bit number (host->ios.vdd)
854 * Returns zero on success, else negative errno.
856 * MMC host drivers may use this to enable or disable a regulator using
857 * a particular supply voltage. This would normally be called from the
860 int mmc_regulator_set_ocr(struct mmc_host *mmc,
861 struct regulator *supply,
862 unsigned short vdd_bit)
871 /* REVISIT mmc_vddrange_to_ocrmask() may have set some
872 * bits this regulator doesn't quite support ... don't
873 * be too picky, most cards and regulators are OK with
874 * a 0.1V range goof (it's a small error percentage).
876 tmp = vdd_bit - ilog2(MMC_VDD_165_195);
878 min_uV = 1650 * 1000;
879 max_uV = 1950 * 1000;
881 min_uV = 1900 * 1000 + tmp * 100 * 1000;
882 max_uV = min_uV + 100 * 1000;
885 /* avoid needless changes to this voltage; the regulator
886 * might not allow this operation
888 voltage = regulator_get_voltage(supply);
891 else if (voltage < min_uV || voltage > max_uV)
892 result = regulator_set_voltage(supply, min_uV, max_uV);
896 if (result == 0 && !mmc->regulator_enabled) {
897 result = regulator_enable(supply);
899 mmc->regulator_enabled = true;
901 } else if (mmc->regulator_enabled) {
902 result = regulator_disable(supply);
904 mmc->regulator_enabled = false;
908 dev_err(mmc_dev(mmc),
909 "could not set regulator OCR (%d)\n", result);
912 EXPORT_SYMBOL(mmc_regulator_set_ocr);
914 #endif /* CONFIG_REGULATOR */
917 * Mask off any voltages we don't support and select
920 u32 mmc_select_voltage(struct mmc_host *host, u32 ocr)
924 ocr &= host->ocr_avail;
935 pr_warning("%s: host doesn't support card's voltages\n",
944 * Select timing parameters for host.
946 void mmc_set_timing(struct mmc_host *host, unsigned int timing)
948 host->ios.timing = timing;
953 * Apply power to the MMC stack. This is a two-stage process.
954 * First, we enable power to the card without the clock running.
955 * We then wait a bit for the power to stabilise. Finally,
956 * enable the bus drivers and clock to the card.
958 * We must _NOT_ enable the clock prior to power stablising.
960 * If a host does all the power sequencing itself, ignore the
961 * initial MMC_POWER_UP stage.
963 static void mmc_power_up(struct mmc_host *host)
967 /* If ocr is set, we use it */
969 bit = ffs(host->ocr) - 1;
971 bit = fls(host->ocr_avail) - 1;
974 if (mmc_host_is_spi(host)) {
975 host->ios.chip_select = MMC_CS_HIGH;
976 host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
978 host->ios.chip_select = MMC_CS_DONTCARE;
979 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
981 host->ios.power_mode = MMC_POWER_UP;
982 host->ios.bus_width = MMC_BUS_WIDTH_1;
983 host->ios.timing = MMC_TIMING_LEGACY;
987 * This delay should be sufficient to allow the power supply
988 * to reach the minimum voltage.
992 host->ios.clock = host->f_init;
994 host->ios.power_mode = MMC_POWER_ON;
998 * This delay must be at least 74 clock sizes, or 1 ms, or the
999 * time required to reach a stable voltage.
1004 static void mmc_power_off(struct mmc_host *host)
1006 host->ios.clock = 0;
1008 if (!mmc_host_is_spi(host)) {
1009 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
1010 host->ios.chip_select = MMC_CS_DONTCARE;
1012 host->ios.power_mode = MMC_POWER_OFF;
1013 host->ios.bus_width = MMC_BUS_WIDTH_1;
1014 host->ios.timing = MMC_TIMING_LEGACY;
1019 * Cleanup when the last reference to the bus operator is dropped.
1021 static void __mmc_release_bus(struct mmc_host *host)
1024 BUG_ON(host->bus_refs);
1025 BUG_ON(!host->bus_dead);
1027 host->bus_ops = NULL;
1031 * Increase reference count of bus operator
1033 static inline void mmc_bus_get(struct mmc_host *host)
1035 unsigned long flags;
1037 spin_lock_irqsave(&host->lock, flags);
1039 spin_unlock_irqrestore(&host->lock, flags);
1043 * Decrease reference count of bus operator and free it if
1044 * it is the last reference.
1046 static inline void mmc_bus_put(struct mmc_host *host)
1048 unsigned long flags;
1050 spin_lock_irqsave(&host->lock, flags);
1052 if ((host->bus_refs == 0) && host->bus_ops)
1053 __mmc_release_bus(host);
1054 spin_unlock_irqrestore(&host->lock, flags);
1058 * Assign a mmc bus handler to a host. Only one bus handler may control a
1059 * host at any given time.
1061 void mmc_attach_bus(struct mmc_host *host, const struct mmc_bus_ops *ops)
1063 unsigned long flags;
1068 WARN_ON(!host->claimed);
1070 spin_lock_irqsave(&host->lock, flags);
1072 BUG_ON(host->bus_ops);
1073 BUG_ON(host->bus_refs);
1075 host->bus_ops = ops;
1079 spin_unlock_irqrestore(&host->lock, flags);
1083 * Remove the current bus handler from a host. Assumes that there are
1084 * no interesting cards left, so the bus is powered down.
1086 void mmc_detach_bus(struct mmc_host *host)
1088 unsigned long flags;
1092 WARN_ON(!host->claimed);
1093 WARN_ON(!host->bus_ops);
1095 spin_lock_irqsave(&host->lock, flags);
1099 spin_unlock_irqrestore(&host->lock, flags);
1101 mmc_power_off(host);
1107 * mmc_detect_change - process change of state on a MMC socket
1108 * @host: host which changed state.
1109 * @delay: optional delay to wait before detection (jiffies)
1111 * MMC drivers should call this when they detect a card has been
1112 * inserted or removed. The MMC layer will confirm that any
1113 * present card is still functional, and initialize any newly
1116 void mmc_detect_change(struct mmc_host *host, unsigned long delay)
1118 #ifdef CONFIG_MMC_DEBUG
1119 unsigned long flags;
1120 spin_lock_irqsave(&host->lock, flags);
1121 WARN_ON(host->removed);
1122 spin_unlock_irqrestore(&host->lock, flags);
1125 mmc_schedule_delayed_work(&host->detect, delay);
1128 EXPORT_SYMBOL(mmc_detect_change);
1130 void mmc_init_erase(struct mmc_card *card)
1134 if (is_power_of_2(card->erase_size))
1135 card->erase_shift = ffs(card->erase_size) - 1;
1137 card->erase_shift = 0;
1140 * It is possible to erase an arbitrarily large area of an SD or MMC
1141 * card. That is not desirable because it can take a long time
1142 * (minutes) potentially delaying more important I/O, and also the
1143 * timeout calculations become increasingly hugely over-estimated.
1144 * Consequently, 'pref_erase' is defined as a guide to limit erases
1145 * to that size and alignment.
1147 * For SD cards that define Allocation Unit size, limit erases to one
1148 * Allocation Unit at a time. For MMC cards that define High Capacity
1149 * Erase Size, whether it is switched on or not, limit to that size.
1150 * Otherwise just have a stab at a good value. For modern cards it
1151 * will end up being 4MiB. Note that if the value is too small, it
1152 * can end up taking longer to erase.
1154 if (mmc_card_sd(card) && card->ssr.au) {
1155 card->pref_erase = card->ssr.au;
1156 card->erase_shift = ffs(card->ssr.au) - 1;
1157 } else if (card->ext_csd.hc_erase_size) {
1158 card->pref_erase = card->ext_csd.hc_erase_size;
1160 sz = (card->csd.capacity << (card->csd.read_blkbits - 9)) >> 11;
1162 card->pref_erase = 512 * 1024 / 512;
1164 card->pref_erase = 1024 * 1024 / 512;
1166 card->pref_erase = 2 * 1024 * 1024 / 512;
1168 card->pref_erase = 4 * 1024 * 1024 / 512;
1169 if (card->pref_erase < card->erase_size)
1170 card->pref_erase = card->erase_size;
1172 sz = card->pref_erase % card->erase_size;
1174 card->pref_erase += card->erase_size - sz;
1179 static void mmc_set_mmc_erase_timeout(struct mmc_card *card,
1180 struct mmc_command *cmd,
1181 unsigned int arg, unsigned int qty)
1183 unsigned int erase_timeout;
1185 if (card->ext_csd.erase_group_def & 1) {
1186 /* High Capacity Erase Group Size uses HC timeouts */
1187 if (arg == MMC_TRIM_ARG)
1188 erase_timeout = card->ext_csd.trim_timeout;
1190 erase_timeout = card->ext_csd.hc_erase_timeout;
1192 /* CSD Erase Group Size uses write timeout */
1193 unsigned int mult = (10 << card->csd.r2w_factor);
1194 unsigned int timeout_clks = card->csd.tacc_clks * mult;
1195 unsigned int timeout_us;
1197 /* Avoid overflow: e.g. tacc_ns=80000000 mult=1280 */
1198 if (card->csd.tacc_ns < 1000000)
1199 timeout_us = (card->csd.tacc_ns * mult) / 1000;
1201 timeout_us = (card->csd.tacc_ns / 1000) * mult;
1204 * ios.clock is only a target. The real clock rate might be
1205 * less but not that much less, so fudge it by multiplying by 2.
1208 timeout_us += (timeout_clks * 1000) /
1209 (card->host->ios.clock / 1000);
1211 erase_timeout = timeout_us / 1000;
1214 * Theoretically, the calculation could underflow so round up
1215 * to 1ms in that case.
1221 /* Multiplier for secure operations */
1222 if (arg & MMC_SECURE_ARGS) {
1223 if (arg == MMC_SECURE_ERASE_ARG)
1224 erase_timeout *= card->ext_csd.sec_erase_mult;
1226 erase_timeout *= card->ext_csd.sec_trim_mult;
1229 erase_timeout *= qty;
1232 * Ensure at least a 1 second timeout for SPI as per
1233 * 'mmc_set_data_timeout()'
1235 if (mmc_host_is_spi(card->host) && erase_timeout < 1000)
1236 erase_timeout = 1000;
1238 cmd->erase_timeout = erase_timeout;
1241 static void mmc_set_sd_erase_timeout(struct mmc_card *card,
1242 struct mmc_command *cmd, unsigned int arg,
1245 if (card->ssr.erase_timeout) {
1246 /* Erase timeout specified in SD Status Register (SSR) */
1247 cmd->erase_timeout = card->ssr.erase_timeout * qty +
1248 card->ssr.erase_offset;
1251 * Erase timeout not specified in SD Status Register (SSR) so
1252 * use 250ms per write block.
1254 cmd->erase_timeout = 250 * qty;
1257 /* Must not be less than 1 second */
1258 if (cmd->erase_timeout < 1000)
1259 cmd->erase_timeout = 1000;
1262 static void mmc_set_erase_timeout(struct mmc_card *card,
1263 struct mmc_command *cmd, unsigned int arg,
1266 if (mmc_card_sd(card))
1267 mmc_set_sd_erase_timeout(card, cmd, arg, qty);
1269 mmc_set_mmc_erase_timeout(card, cmd, arg, qty);
1272 static int mmc_do_erase(struct mmc_card *card, unsigned int from,
1273 unsigned int to, unsigned int arg)
1275 struct mmc_command cmd;
1276 unsigned int qty = 0;
1280 * qty is used to calculate the erase timeout which depends on how many
1281 * erase groups (or allocation units in SD terminology) are affected.
1282 * We count erasing part of an erase group as one erase group.
1283 * For SD, the allocation units are always a power of 2. For MMC, the
1284 * erase group size is almost certainly also power of 2, but it does not
1285 * seem to insist on that in the JEDEC standard, so we fall back to
1286 * division in that case. SD may not specify an allocation unit size,
1287 * in which case the timeout is based on the number of write blocks.
1289 * Note that the timeout for secure trim 2 will only be correct if the
1290 * number of erase groups specified is the same as the total of all
1291 * preceding secure trim 1 commands. Since the power may have been
1292 * lost since the secure trim 1 commands occurred, it is generally
1293 * impossible to calculate the secure trim 2 timeout correctly.
1295 if (card->erase_shift)
1296 qty += ((to >> card->erase_shift) -
1297 (from >> card->erase_shift)) + 1;
1298 else if (mmc_card_sd(card))
1299 qty += to - from + 1;
1301 qty += ((to / card->erase_size) -
1302 (from / card->erase_size)) + 1;
1304 if (!mmc_card_blockaddr(card)) {
1309 memset(&cmd, 0, sizeof(struct mmc_command));
1310 if (mmc_card_sd(card))
1311 cmd.opcode = SD_ERASE_WR_BLK_START;
1313 cmd.opcode = MMC_ERASE_GROUP_START;
1315 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
1316 err = mmc_wait_for_cmd(card->host, &cmd, 0);
1318 printk(KERN_ERR "mmc_erase: group start error %d, "
1319 "status %#x\n", err, cmd.resp[0]);
1324 memset(&cmd, 0, sizeof(struct mmc_command));
1325 if (mmc_card_sd(card))
1326 cmd.opcode = SD_ERASE_WR_BLK_END;
1328 cmd.opcode = MMC_ERASE_GROUP_END;
1330 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
1331 err = mmc_wait_for_cmd(card->host, &cmd, 0);
1333 printk(KERN_ERR "mmc_erase: group end error %d, status %#x\n",
1339 memset(&cmd, 0, sizeof(struct mmc_command));
1340 cmd.opcode = MMC_ERASE;
1342 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1343 mmc_set_erase_timeout(card, &cmd, arg, qty);
1344 err = mmc_wait_for_cmd(card->host, &cmd, 0);
1346 printk(KERN_ERR "mmc_erase: erase error %d, status %#x\n",
1352 if (mmc_host_is_spi(card->host))
1356 memset(&cmd, 0, sizeof(struct mmc_command));
1357 cmd.opcode = MMC_SEND_STATUS;
1358 cmd.arg = card->rca << 16;
1359 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
1360 /* Do not retry else we can't see errors */
1361 err = mmc_wait_for_cmd(card->host, &cmd, 0);
1362 if (err || (cmd.resp[0] & 0xFDF92000)) {
1363 printk(KERN_ERR "error %d requesting status %#x\n",
1368 } while (!(cmd.resp[0] & R1_READY_FOR_DATA) ||
1369 R1_CURRENT_STATE(cmd.resp[0]) == 7);
1375 * mmc_erase - erase sectors.
1376 * @card: card to erase
1377 * @from: first sector to erase
1378 * @nr: number of sectors to erase
1379 * @arg: erase command argument (SD supports only %MMC_ERASE_ARG)
1381 * Caller must claim host before calling this function.
1383 int mmc_erase(struct mmc_card *card, unsigned int from, unsigned int nr,
1386 unsigned int rem, to = from + nr;
1388 if (!(card->host->caps & MMC_CAP_ERASE) ||
1389 !(card->csd.cmdclass & CCC_ERASE))
1392 if (!card->erase_size)
1395 if (mmc_card_sd(card) && arg != MMC_ERASE_ARG)
1398 if ((arg & MMC_SECURE_ARGS) &&
1399 !(card->ext_csd.sec_feature_support & EXT_CSD_SEC_ER_EN))
1402 if ((arg & MMC_TRIM_ARGS) &&
1403 !(card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN))
1406 if (arg == MMC_SECURE_ERASE_ARG) {
1407 if (from % card->erase_size || nr % card->erase_size)
1411 if (arg == MMC_ERASE_ARG) {
1412 rem = from % card->erase_size;
1414 rem = card->erase_size - rem;
1421 rem = nr % card->erase_size;
1434 /* 'from' and 'to' are inclusive */
1437 return mmc_do_erase(card, from, to, arg);
1439 EXPORT_SYMBOL(mmc_erase);
1441 int mmc_can_erase(struct mmc_card *card)
1443 if ((card->host->caps & MMC_CAP_ERASE) &&
1444 (card->csd.cmdclass & CCC_ERASE) && card->erase_size)
1448 EXPORT_SYMBOL(mmc_can_erase);
1450 int mmc_can_trim(struct mmc_card *card)
1452 if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN)
1456 EXPORT_SYMBOL(mmc_can_trim);
1458 int mmc_can_secure_erase_trim(struct mmc_card *card)
1460 if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_ER_EN)
1464 EXPORT_SYMBOL(mmc_can_secure_erase_trim);
1466 int mmc_erase_group_aligned(struct mmc_card *card, unsigned int from,
1469 if (!card->erase_size)
1471 if (from % card->erase_size || nr % card->erase_size)
1475 EXPORT_SYMBOL(mmc_erase_group_aligned);
1477 int mmc_set_blocklen(struct mmc_card *card, unsigned int blocklen)
1479 struct mmc_command cmd;
1481 if (mmc_card_blockaddr(card) || mmc_card_ddr_mode(card))
1484 memset(&cmd, 0, sizeof(struct mmc_command));
1485 cmd.opcode = MMC_SET_BLOCKLEN;
1487 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
1488 return mmc_wait_for_cmd(card->host, &cmd, 5);
1490 EXPORT_SYMBOL(mmc_set_blocklen);
1492 static int mmc_rescan_try_freq(struct mmc_host *host, unsigned freq)
1494 host->f_init = freq;
1496 #ifdef CONFIG_MMC_DEBUG
1497 pr_info("%s: %s: trying to init card at %u Hz\n",
1498 mmc_hostname(host), __func__, host->f_init);
1504 mmc_send_if_cond(host, host->ocr_avail);
1506 /* Order's important: probe SDIO, then SD, then MMC */
1507 if (!mmc_attach_sdio(host))
1509 if (!mmc_attach_sd(host))
1511 if (!mmc_attach_mmc(host))
1514 mmc_power_off(host);
1518 void mmc_rescan(struct work_struct *work)
1520 static const unsigned freqs[] = { 400000, 300000, 200000, 100000 };
1521 struct mmc_host *host =
1522 container_of(work, struct mmc_host, detect.work);
1524 bool extend_wakelock = false;
1526 if (host->rescan_disable)
1532 * if there is a _removable_ card registered, check whether it is
1535 if (host->bus_ops && host->bus_ops->detect && !host->bus_dead
1536 && !(host->caps & MMC_CAP_NONREMOVABLE))
1537 host->bus_ops->detect(host);
1540 * Let mmc_bus_put() free the bus/bus_ops if we've found that
1541 * the card is no longer present.
1546 /* if there still is a card present, stop here */
1547 if (host->bus_ops != NULL) {
1553 * Only we can add a new handler, so it's safe to
1554 * release the lock here.
1558 if (host->ops->get_cd && host->ops->get_cd(host) == 0)
1561 mmc_claim_host(host);
1562 for (i = 0; i < ARRAY_SIZE(freqs); i++) {
1563 if (!mmc_rescan_try_freq(host, max(freqs[i], host->f_min))) {
1564 extend_wakelock = true;
1567 if (freqs[i] < host->f_min)
1570 mmc_release_host(host);
1573 if (extend_wakelock)
1574 wake_lock_timeout(&mmc_delayed_work_wake_lock, HZ / 2);
1576 wake_unlock(&mmc_delayed_work_wake_lock);
1577 if (host->caps & MMC_CAP_NEEDS_POLL)
1578 mmc_schedule_delayed_work(&host->detect, HZ);
1581 void mmc_start_host(struct mmc_host *host)
1583 mmc_power_off(host);
1584 mmc_detect_change(host, 0);
1587 void mmc_stop_host(struct mmc_host *host)
1589 #ifdef CONFIG_MMC_DEBUG
1590 unsigned long flags;
1591 spin_lock_irqsave(&host->lock, flags);
1593 spin_unlock_irqrestore(&host->lock, flags);
1596 if (host->caps & MMC_CAP_DISABLE)
1597 cancel_delayed_work(&host->disable);
1598 cancel_delayed_work_sync(&host->detect);
1599 mmc_flush_scheduled_work();
1601 /* clear pm flags now and let card drivers set them as needed */
1605 if (host->bus_ops && !host->bus_dead) {
1606 if (host->bus_ops->remove)
1607 host->bus_ops->remove(host);
1609 mmc_claim_host(host);
1610 mmc_detach_bus(host);
1611 mmc_release_host(host);
1619 mmc_power_off(host);
1622 int mmc_power_save_host(struct mmc_host *host)
1628 if (!host->bus_ops || host->bus_dead || !host->bus_ops->power_restore) {
1633 if (host->bus_ops->power_save)
1634 ret = host->bus_ops->power_save(host);
1638 mmc_power_off(host);
1642 EXPORT_SYMBOL(mmc_power_save_host);
1644 int mmc_power_restore_host(struct mmc_host *host)
1650 if (!host->bus_ops || host->bus_dead || !host->bus_ops->power_restore) {
1656 ret = host->bus_ops->power_restore(host);
1662 EXPORT_SYMBOL(mmc_power_restore_host);
1664 int mmc_card_awake(struct mmc_host *host)
1670 if (host->bus_ops && !host->bus_dead && host->bus_ops->awake)
1671 err = host->bus_ops->awake(host);
1677 EXPORT_SYMBOL(mmc_card_awake);
1679 int mmc_card_sleep(struct mmc_host *host)
1685 if (host->bus_ops && !host->bus_dead && host->bus_ops->awake)
1686 err = host->bus_ops->sleep(host);
1692 EXPORT_SYMBOL(mmc_card_sleep);
1694 int mmc_card_can_sleep(struct mmc_host *host)
1696 struct mmc_card *card = host->card;
1698 if (card && mmc_card_mmc(card) && card->ext_csd.rev >= 3)
1702 EXPORT_SYMBOL(mmc_card_can_sleep);
1707 * mmc_suspend_host - suspend a host
1710 int mmc_suspend_host(struct mmc_host *host)
1714 if (host->caps & MMC_CAP_DISABLE)
1715 cancel_delayed_work(&host->disable);
1716 cancel_delayed_work(&host->detect);
1717 mmc_flush_scheduled_work();
1720 if (host->bus_ops && !host->bus_dead) {
1721 if (host->bus_ops->suspend)
1722 err = host->bus_ops->suspend(host);
1723 if (err == -ENOSYS || !host->bus_ops->resume) {
1725 * We simply "remove" the card in this case.
1726 * It will be redetected on resume.
1728 if (host->bus_ops->remove)
1729 host->bus_ops->remove(host);
1730 mmc_claim_host(host);
1731 mmc_detach_bus(host);
1732 mmc_release_host(host);
1739 if (!err && !(host->pm_flags & MMC_PM_KEEP_POWER))
1740 mmc_power_off(host);
1745 EXPORT_SYMBOL(mmc_suspend_host);
1748 * mmc_resume_host - resume a previously suspended host
1751 int mmc_resume_host(struct mmc_host *host)
1756 if (host->bus_ops && !host->bus_dead) {
1757 if (!(host->pm_flags & MMC_PM_KEEP_POWER)) {
1759 mmc_select_voltage(host, host->ocr);
1761 * Tell runtime PM core we just powered up the card,
1762 * since it still believes the card is powered off.
1763 * Note that currently runtime PM is only enabled
1764 * for SDIO cards that are MMC_CAP_POWER_OFF_CARD
1766 if (mmc_card_sdio(host->card) &&
1767 (host->caps & MMC_CAP_POWER_OFF_CARD)) {
1768 pm_runtime_disable(&host->card->dev);
1769 pm_runtime_set_active(&host->card->dev);
1770 pm_runtime_enable(&host->card->dev);
1773 BUG_ON(!host->bus_ops->resume);
1774 err = host->bus_ops->resume(host);
1776 printk(KERN_WARNING "%s: error %d during resume "
1777 "(card was removed?)\n",
1778 mmc_hostname(host), err);
1786 EXPORT_SYMBOL(mmc_resume_host);
1788 /* Do the card removal on suspend if card is assumed removeable
1789 * Do that in pm notifier while userspace isn't yet frozen, so we will be able
1792 int mmc_pm_notify(struct notifier_block *notify_block,
1793 unsigned long mode, void *unused)
1795 struct mmc_host *host = container_of(
1796 notify_block, struct mmc_host, pm_notify);
1797 unsigned long flags;
1801 case PM_HIBERNATION_PREPARE:
1802 case PM_SUSPEND_PREPARE:
1804 spin_lock_irqsave(&host->lock, flags);
1805 host->rescan_disable = 1;
1806 spin_unlock_irqrestore(&host->lock, flags);
1807 cancel_delayed_work_sync(&host->detect);
1809 if (!host->bus_ops || host->bus_ops->suspend)
1812 mmc_claim_host(host);
1814 if (host->bus_ops->remove)
1815 host->bus_ops->remove(host);
1817 mmc_detach_bus(host);
1818 mmc_release_host(host);
1822 case PM_POST_SUSPEND:
1823 case PM_POST_HIBERNATION:
1824 case PM_POST_RESTORE:
1826 spin_lock_irqsave(&host->lock, flags);
1827 host->rescan_disable = 0;
1828 spin_unlock_irqrestore(&host->lock, flags);
1829 mmc_detect_change(host, 0);
1837 #ifdef CONFIG_MMC_EMBEDDED_SDIO
1838 void mmc_set_embedded_sdio_data(struct mmc_host *host,
1839 struct sdio_cis *cis,
1840 struct sdio_cccr *cccr,
1841 struct sdio_embedded_func *funcs,
1844 host->embedded_sdio_data.cis = cis;
1845 host->embedded_sdio_data.cccr = cccr;
1846 host->embedded_sdio_data.funcs = funcs;
1847 host->embedded_sdio_data.num_funcs = num_funcs;
1850 EXPORT_SYMBOL(mmc_set_embedded_sdio_data);
1853 static int __init mmc_init(void)
1857 workqueue = alloc_ordered_workqueue("kmmcd", 0);
1861 wake_lock_init(&mmc_delayed_work_wake_lock, WAKE_LOCK_SUSPEND,
1862 "mmc_delayed_work");
1864 ret = mmc_register_bus();
1866 goto destroy_workqueue;
1868 ret = mmc_register_host_class();
1870 goto unregister_bus;
1872 ret = sdio_register_bus();
1874 goto unregister_host_class;
1878 unregister_host_class:
1879 mmc_unregister_host_class();
1881 mmc_unregister_bus();
1883 destroy_workqueue(workqueue);
1884 wake_lock_destroy(&mmc_delayed_work_wake_lock);
1889 static void __exit mmc_exit(void)
1891 sdio_unregister_bus();
1892 mmc_unregister_host_class();
1893 mmc_unregister_bus();
1894 destroy_workqueue(workqueue);
1895 wake_lock_destroy(&mmc_delayed_work_wake_lock);
1898 subsys_initcall(mmc_init);
1899 module_exit(mmc_exit);
1901 MODULE_LICENSE("GPL");