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 = {0};
241 WARN_ON(!host->claimed);
243 memset(cmd->resp, 0, sizeof(cmd->resp));
244 cmd->retries = retries;
249 mmc_wait_for_req(host, &mrq);
254 EXPORT_SYMBOL(mmc_wait_for_cmd);
257 * mmc_set_data_timeout - set the timeout for a data command
258 * @data: data phase for command
259 * @card: the MMC card associated with the data transfer
261 * Computes the data timeout parameters according to the
262 * correct algorithm given the card type.
264 void mmc_set_data_timeout(struct mmc_data *data, const struct mmc_card *card)
269 * SDIO cards only define an upper 1 s limit on access.
271 if (mmc_card_sdio(card)) {
272 data->timeout_ns = 1000000000;
273 data->timeout_clks = 0;
278 * SD cards use a 100 multiplier rather than 10
280 mult = mmc_card_sd(card) ? 100 : 10;
283 * Scale up the multiplier (and therefore the timeout) by
284 * the r2w factor for writes.
286 if (data->flags & MMC_DATA_WRITE)
287 mult <<= card->csd.r2w_factor;
289 data->timeout_ns = card->csd.tacc_ns * mult;
290 data->timeout_clks = card->csd.tacc_clks * mult;
293 * SD cards also have an upper limit on the timeout.
295 if (mmc_card_sd(card)) {
296 unsigned int timeout_us, limit_us;
298 timeout_us = data->timeout_ns / 1000;
299 if (mmc_host_clk_rate(card->host))
300 timeout_us += data->timeout_clks * 1000 /
301 (mmc_host_clk_rate(card->host) / 1000);
303 if (data->flags & MMC_DATA_WRITE)
305 * The limit is really 250 ms, but that is
306 * insufficient for some crappy cards.
313 * SDHC cards always use these fixed values.
315 if (timeout_us > limit_us || mmc_card_blockaddr(card)) {
316 data->timeout_ns = limit_us * 1000;
317 data->timeout_clks = 0;
321 * Some cards need very high timeouts if driven in SPI mode.
322 * The worst observed timeout was 900ms after writing a
323 * continuous stream of data until the internal logic
326 if (mmc_host_is_spi(card->host)) {
327 if (data->flags & MMC_DATA_WRITE) {
328 if (data->timeout_ns < 1000000000)
329 data->timeout_ns = 1000000000; /* 1s */
331 if (data->timeout_ns < 100000000)
332 data->timeout_ns = 100000000; /* 100ms */
336 EXPORT_SYMBOL(mmc_set_data_timeout);
339 * mmc_align_data_size - pads a transfer size to a more optimal value
340 * @card: the MMC card associated with the data transfer
341 * @sz: original transfer size
343 * Pads the original data size with a number of extra bytes in
344 * order to avoid controller bugs and/or performance hits
345 * (e.g. some controllers revert to PIO for certain sizes).
347 * Returns the improved size, which might be unmodified.
349 * Note that this function is only relevant when issuing a
350 * single scatter gather entry.
352 unsigned int mmc_align_data_size(struct mmc_card *card, unsigned int sz)
355 * FIXME: We don't have a system for the controller to tell
356 * the core about its problems yet, so for now we just 32-bit
359 sz = ((sz + 3) / 4) * 4;
363 EXPORT_SYMBOL(mmc_align_data_size);
366 * mmc_host_enable - enable a host.
367 * @host: mmc host to enable
369 * Hosts that support power saving can use the 'enable' and 'disable'
370 * methods to exit and enter power saving states. For more information
371 * see comments for struct mmc_host_ops.
373 int mmc_host_enable(struct mmc_host *host)
375 if (!(host->caps & MMC_CAP_DISABLE))
378 if (host->en_dis_recurs)
381 if (host->nesting_cnt++)
384 cancel_delayed_work_sync(&host->disable);
389 if (host->ops->enable) {
392 host->en_dis_recurs = 1;
393 err = host->ops->enable(host);
394 host->en_dis_recurs = 0;
397 pr_debug("%s: enable error %d\n",
398 mmc_hostname(host), err);
405 EXPORT_SYMBOL(mmc_host_enable);
407 static int mmc_host_do_disable(struct mmc_host *host, int lazy)
409 if (host->ops->disable) {
412 host->en_dis_recurs = 1;
413 err = host->ops->disable(host, lazy);
414 host->en_dis_recurs = 0;
417 pr_debug("%s: disable error %d\n",
418 mmc_hostname(host), err);
422 unsigned long delay = msecs_to_jiffies(err);
424 mmc_schedule_delayed_work(&host->disable, delay);
432 * mmc_host_disable - disable a host.
433 * @host: mmc host to disable
435 * Hosts that support power saving can use the 'enable' and 'disable'
436 * methods to exit and enter power saving states. For more information
437 * see comments for struct mmc_host_ops.
439 int mmc_host_disable(struct mmc_host *host)
443 if (!(host->caps & MMC_CAP_DISABLE))
446 if (host->en_dis_recurs)
449 if (--host->nesting_cnt)
455 err = mmc_host_do_disable(host, 0);
458 EXPORT_SYMBOL(mmc_host_disable);
461 * __mmc_claim_host - exclusively claim a host
462 * @host: mmc host to claim
463 * @abort: whether or not the operation should be aborted
465 * Claim a host for a set of operations. If @abort is non null and
466 * dereference a non-zero value then this will return prematurely with
467 * that non-zero value without acquiring the lock. Returns zero
468 * with the lock held otherwise.
470 int __mmc_claim_host(struct mmc_host *host, atomic_t *abort)
472 DECLARE_WAITQUEUE(wait, current);
478 add_wait_queue(&host->wq, &wait);
479 spin_lock_irqsave(&host->lock, flags);
481 set_current_state(TASK_UNINTERRUPTIBLE);
482 stop = abort ? atomic_read(abort) : 0;
483 if (stop || !host->claimed || host->claimer == current)
485 spin_unlock_irqrestore(&host->lock, flags);
487 spin_lock_irqsave(&host->lock, flags);
489 set_current_state(TASK_RUNNING);
492 host->claimer = current;
493 host->claim_cnt += 1;
496 spin_unlock_irqrestore(&host->lock, flags);
497 remove_wait_queue(&host->wq, &wait);
499 mmc_host_enable(host);
503 EXPORT_SYMBOL(__mmc_claim_host);
506 * mmc_try_claim_host - try exclusively to claim a host
507 * @host: mmc host to claim
509 * Returns %1 if the host is claimed, %0 otherwise.
511 int mmc_try_claim_host(struct mmc_host *host)
513 int claimed_host = 0;
516 spin_lock_irqsave(&host->lock, flags);
517 if (!host->claimed || host->claimer == current) {
519 host->claimer = current;
520 host->claim_cnt += 1;
523 spin_unlock_irqrestore(&host->lock, flags);
526 EXPORT_SYMBOL(mmc_try_claim_host);
529 * mmc_do_release_host - release a claimed host
530 * @host: mmc host to release
532 * If you successfully claimed a host, this function will
535 void mmc_do_release_host(struct mmc_host *host)
539 spin_lock_irqsave(&host->lock, flags);
540 if (--host->claim_cnt) {
541 /* Release for nested claim */
542 spin_unlock_irqrestore(&host->lock, flags);
545 host->claimer = NULL;
546 spin_unlock_irqrestore(&host->lock, flags);
550 EXPORT_SYMBOL(mmc_do_release_host);
552 void mmc_host_deeper_disable(struct work_struct *work)
554 struct mmc_host *host =
555 container_of(work, struct mmc_host, disable.work);
557 /* If the host is claimed then we do not want to disable it anymore */
558 if (!mmc_try_claim_host(host))
560 mmc_host_do_disable(host, 1);
561 mmc_do_release_host(host);
565 * mmc_host_lazy_disable - lazily disable a host.
566 * @host: mmc host to disable
568 * Hosts that support power saving can use the 'enable' and 'disable'
569 * methods to exit and enter power saving states. For more information
570 * see comments for struct mmc_host_ops.
572 int mmc_host_lazy_disable(struct mmc_host *host)
574 if (!(host->caps & MMC_CAP_DISABLE))
577 if (host->en_dis_recurs)
580 if (--host->nesting_cnt)
586 if (host->disable_delay) {
587 mmc_schedule_delayed_work(&host->disable,
588 msecs_to_jiffies(host->disable_delay));
591 return mmc_host_do_disable(host, 1);
593 EXPORT_SYMBOL(mmc_host_lazy_disable);
596 * mmc_release_host - release a host
597 * @host: mmc host to release
599 * Release a MMC host, allowing others to claim the host
600 * for their operations.
602 void mmc_release_host(struct mmc_host *host)
604 WARN_ON(!host->claimed);
606 mmc_host_lazy_disable(host);
608 mmc_do_release_host(host);
611 EXPORT_SYMBOL(mmc_release_host);
614 * Internal function that does the actual ios call to the host driver,
615 * optionally printing some debug output.
617 static inline void mmc_set_ios(struct mmc_host *host)
619 struct mmc_ios *ios = &host->ios;
621 pr_debug("%s: clock %uHz busmode %u powermode %u cs %u Vdd %u "
622 "width %u timing %u\n",
623 mmc_hostname(host), ios->clock, ios->bus_mode,
624 ios->power_mode, ios->chip_select, ios->vdd,
625 ios->bus_width, ios->timing);
628 mmc_set_ungated(host);
629 host->ops->set_ios(host, ios);
633 * Control chip select pin on a host.
635 void mmc_set_chip_select(struct mmc_host *host, int mode)
637 host->ios.chip_select = mode;
642 * Sets the host clock to the highest possible frequency that
645 void mmc_set_clock(struct mmc_host *host, unsigned int hz)
647 WARN_ON(hz < host->f_min);
649 if (hz > host->f_max)
652 host->ios.clock = hz;
656 #ifdef CONFIG_MMC_CLKGATE
658 * This gates the clock by setting it to 0 Hz.
660 void mmc_gate_clock(struct mmc_host *host)
664 spin_lock_irqsave(&host->clk_lock, flags);
665 host->clk_old = host->ios.clock;
667 host->clk_gated = true;
668 spin_unlock_irqrestore(&host->clk_lock, flags);
673 * This restores the clock from gating by using the cached
676 void mmc_ungate_clock(struct mmc_host *host)
679 * We should previously have gated the clock, so the clock shall
680 * be 0 here! The clock may however be 0 during initialization,
681 * when some request operations are performed before setting
682 * the frequency. When ungate is requested in that situation
683 * we just ignore the call.
686 BUG_ON(host->ios.clock);
687 /* This call will also set host->clk_gated to false */
688 mmc_set_clock(host, host->clk_old);
692 void mmc_set_ungated(struct mmc_host *host)
697 * We've been given a new frequency while the clock is gated,
698 * so make sure we regard this as ungating it.
700 spin_lock_irqsave(&host->clk_lock, flags);
701 host->clk_gated = false;
702 spin_unlock_irqrestore(&host->clk_lock, flags);
706 void mmc_set_ungated(struct mmc_host *host)
712 * Change the bus mode (open drain/push-pull) of a host.
714 void mmc_set_bus_mode(struct mmc_host *host, unsigned int mode)
716 host->ios.bus_mode = mode;
721 * Change data bus width and DDR mode of a host.
723 void mmc_set_bus_width_ddr(struct mmc_host *host, unsigned int width,
726 host->ios.bus_width = width;
732 * Change data bus width of a host.
734 void mmc_set_bus_width(struct mmc_host *host, unsigned int width)
736 mmc_set_bus_width_ddr(host, width, MMC_SDR_MODE);
740 * mmc_vdd_to_ocrbitnum - Convert a voltage to the OCR bit number
742 * @low_bits: prefer low bits in boundary cases
744 * This function returns the OCR bit number according to the provided @vdd
745 * value. If conversion is not possible a negative errno value returned.
747 * Depending on the @low_bits flag the function prefers low or high OCR bits
748 * on boundary voltages. For example,
749 * with @low_bits = true, 3300 mV translates to ilog2(MMC_VDD_32_33);
750 * with @low_bits = false, 3300 mV translates to ilog2(MMC_VDD_33_34);
752 * Any value in the [1951:1999] range translates to the ilog2(MMC_VDD_20_21).
754 static int mmc_vdd_to_ocrbitnum(int vdd, bool low_bits)
756 const int max_bit = ilog2(MMC_VDD_35_36);
759 if (vdd < 1650 || vdd > 3600)
762 if (vdd >= 1650 && vdd <= 1950)
763 return ilog2(MMC_VDD_165_195);
768 /* Base 2000 mV, step 100 mV, bit's base 8. */
769 bit = (vdd - 2000) / 100 + 8;
776 * mmc_vddrange_to_ocrmask - Convert a voltage range to the OCR mask
777 * @vdd_min: minimum voltage value (mV)
778 * @vdd_max: maximum voltage value (mV)
780 * This function returns the OCR mask bits according to the provided @vdd_min
781 * and @vdd_max values. If conversion is not possible the function returns 0.
783 * Notes wrt boundary cases:
784 * This function sets the OCR bits for all boundary voltages, for example
785 * [3300:3400] range is translated to MMC_VDD_32_33 | MMC_VDD_33_34 |
786 * MMC_VDD_34_35 mask.
788 u32 mmc_vddrange_to_ocrmask(int vdd_min, int vdd_max)
792 if (vdd_max < vdd_min)
795 /* Prefer high bits for the boundary vdd_max values. */
796 vdd_max = mmc_vdd_to_ocrbitnum(vdd_max, false);
800 /* Prefer low bits for the boundary vdd_min values. */
801 vdd_min = mmc_vdd_to_ocrbitnum(vdd_min, true);
805 /* Fill the mask, from max bit to min bit. */
806 while (vdd_max >= vdd_min)
807 mask |= 1 << vdd_max--;
811 EXPORT_SYMBOL(mmc_vddrange_to_ocrmask);
813 #ifdef CONFIG_REGULATOR
816 * mmc_regulator_get_ocrmask - return mask of supported voltages
817 * @supply: regulator to use
819 * This returns either a negative errno, or a mask of voltages that
820 * can be provided to MMC/SD/SDIO devices using the specified voltage
821 * regulator. This would normally be called before registering the
824 int mmc_regulator_get_ocrmask(struct regulator *supply)
830 count = regulator_count_voltages(supply);
834 for (i = 0; i < count; i++) {
838 vdd_uV = regulator_list_voltage(supply, i);
842 vdd_mV = vdd_uV / 1000;
843 result |= mmc_vddrange_to_ocrmask(vdd_mV, vdd_mV);
848 EXPORT_SYMBOL(mmc_regulator_get_ocrmask);
851 * mmc_regulator_set_ocr - set regulator to match host->ios voltage
852 * @mmc: the host to regulate
853 * @supply: regulator to use
854 * @vdd_bit: zero for power off, else a bit number (host->ios.vdd)
856 * Returns zero on success, else negative errno.
858 * MMC host drivers may use this to enable or disable a regulator using
859 * a particular supply voltage. This would normally be called from the
862 int mmc_regulator_set_ocr(struct mmc_host *mmc,
863 struct regulator *supply,
864 unsigned short vdd_bit)
873 /* REVISIT mmc_vddrange_to_ocrmask() may have set some
874 * bits this regulator doesn't quite support ... don't
875 * be too picky, most cards and regulators are OK with
876 * a 0.1V range goof (it's a small error percentage).
878 tmp = vdd_bit - ilog2(MMC_VDD_165_195);
880 min_uV = 1650 * 1000;
881 max_uV = 1950 * 1000;
883 min_uV = 1900 * 1000 + tmp * 100 * 1000;
884 max_uV = min_uV + 100 * 1000;
887 /* avoid needless changes to this voltage; the regulator
888 * might not allow this operation
890 voltage = regulator_get_voltage(supply);
893 else if (voltage < min_uV || voltage > max_uV)
894 result = regulator_set_voltage(supply, min_uV, max_uV);
898 if (result == 0 && !mmc->regulator_enabled) {
899 result = regulator_enable(supply);
901 mmc->regulator_enabled = true;
903 } else if (mmc->regulator_enabled) {
904 result = regulator_disable(supply);
906 mmc->regulator_enabled = false;
910 dev_err(mmc_dev(mmc),
911 "could not set regulator OCR (%d)\n", result);
914 EXPORT_SYMBOL(mmc_regulator_set_ocr);
916 #endif /* CONFIG_REGULATOR */
919 * Mask off any voltages we don't support and select
922 u32 mmc_select_voltage(struct mmc_host *host, u32 ocr)
926 ocr &= host->ocr_avail;
937 pr_warning("%s: host doesn't support card's voltages\n",
945 int mmc_set_signal_voltage(struct mmc_host *host, int signal_voltage)
947 struct mmc_command cmd = {0};
953 * Send CMD11 only if the request is to switch the card to
956 if (signal_voltage == MMC_SIGNAL_VOLTAGE_180) {
957 cmd.opcode = SD_SWITCH_VOLTAGE;
959 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
961 err = mmc_wait_for_cmd(host, &cmd, 0);
965 if (!mmc_host_is_spi(host) && (cmd.resp[0] & R1_ERROR))
969 host->ios.signal_voltage = signal_voltage;
971 if (host->ops->start_signal_voltage_switch)
972 err = host->ops->start_signal_voltage_switch(host, &host->ios);
978 * Select timing parameters for host.
980 void mmc_set_timing(struct mmc_host *host, unsigned int timing)
982 host->ios.timing = timing;
987 * Select appropriate driver type for host.
989 void mmc_set_driver_type(struct mmc_host *host, unsigned int drv_type)
991 host->ios.drv_type = drv_type;
996 * Apply power to the MMC stack. This is a two-stage process.
997 * First, we enable power to the card without the clock running.
998 * We then wait a bit for the power to stabilise. Finally,
999 * enable the bus drivers and clock to the card.
1001 * We must _NOT_ enable the clock prior to power stablising.
1003 * If a host does all the power sequencing itself, ignore the
1004 * initial MMC_POWER_UP stage.
1006 static void mmc_power_up(struct mmc_host *host)
1010 /* If ocr is set, we use it */
1012 bit = ffs(host->ocr) - 1;
1014 bit = fls(host->ocr_avail) - 1;
1016 host->ios.vdd = bit;
1017 if (mmc_host_is_spi(host)) {
1018 host->ios.chip_select = MMC_CS_HIGH;
1019 host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
1021 host->ios.chip_select = MMC_CS_DONTCARE;
1022 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
1024 host->ios.power_mode = MMC_POWER_UP;
1025 host->ios.bus_width = MMC_BUS_WIDTH_1;
1026 host->ios.timing = MMC_TIMING_LEGACY;
1030 * This delay should be sufficient to allow the power supply
1031 * to reach the minimum voltage.
1035 host->ios.clock = host->f_init;
1037 host->ios.power_mode = MMC_POWER_ON;
1041 * This delay must be at least 74 clock sizes, or 1 ms, or the
1042 * time required to reach a stable voltage.
1047 static void mmc_power_off(struct mmc_host *host)
1049 host->ios.clock = 0;
1053 * Reset ocr mask to be the highest possible voltage supported for
1054 * this mmc host. This value will be used at next power up.
1056 host->ocr = 1 << (fls(host->ocr_avail) - 1);
1058 if (!mmc_host_is_spi(host)) {
1059 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
1060 host->ios.chip_select = MMC_CS_DONTCARE;
1062 host->ios.power_mode = MMC_POWER_OFF;
1063 host->ios.bus_width = MMC_BUS_WIDTH_1;
1064 host->ios.timing = MMC_TIMING_LEGACY;
1069 * Cleanup when the last reference to the bus operator is dropped.
1071 static void __mmc_release_bus(struct mmc_host *host)
1074 BUG_ON(host->bus_refs);
1075 BUG_ON(!host->bus_dead);
1077 host->bus_ops = NULL;
1081 * Increase reference count of bus operator
1083 static inline void mmc_bus_get(struct mmc_host *host)
1085 unsigned long flags;
1087 spin_lock_irqsave(&host->lock, flags);
1089 spin_unlock_irqrestore(&host->lock, flags);
1093 * Decrease reference count of bus operator and free it if
1094 * it is the last reference.
1096 static inline void mmc_bus_put(struct mmc_host *host)
1098 unsigned long flags;
1100 spin_lock_irqsave(&host->lock, flags);
1102 if ((host->bus_refs == 0) && host->bus_ops)
1103 __mmc_release_bus(host);
1104 spin_unlock_irqrestore(&host->lock, flags);
1108 * Assign a mmc bus handler to a host. Only one bus handler may control a
1109 * host at any given time.
1111 void mmc_attach_bus(struct mmc_host *host, const struct mmc_bus_ops *ops)
1113 unsigned long flags;
1118 WARN_ON(!host->claimed);
1120 spin_lock_irqsave(&host->lock, flags);
1122 BUG_ON(host->bus_ops);
1123 BUG_ON(host->bus_refs);
1125 host->bus_ops = ops;
1129 spin_unlock_irqrestore(&host->lock, flags);
1133 * Remove the current bus handler from a host. Assumes that there are
1134 * no interesting cards left, so the bus is powered down.
1136 void mmc_detach_bus(struct mmc_host *host)
1138 unsigned long flags;
1142 WARN_ON(!host->claimed);
1143 WARN_ON(!host->bus_ops);
1145 spin_lock_irqsave(&host->lock, flags);
1149 spin_unlock_irqrestore(&host->lock, flags);
1151 mmc_power_off(host);
1157 * mmc_detect_change - process change of state on a MMC socket
1158 * @host: host which changed state.
1159 * @delay: optional delay to wait before detection (jiffies)
1161 * MMC drivers should call this when they detect a card has been
1162 * inserted or removed. The MMC layer will confirm that any
1163 * present card is still functional, and initialize any newly
1166 void mmc_detect_change(struct mmc_host *host, unsigned long delay)
1168 #ifdef CONFIG_MMC_DEBUG
1169 unsigned long flags;
1170 spin_lock_irqsave(&host->lock, flags);
1171 WARN_ON(host->removed);
1172 spin_unlock_irqrestore(&host->lock, flags);
1175 mmc_schedule_delayed_work(&host->detect, delay);
1178 EXPORT_SYMBOL(mmc_detect_change);
1180 void mmc_init_erase(struct mmc_card *card)
1184 if (is_power_of_2(card->erase_size))
1185 card->erase_shift = ffs(card->erase_size) - 1;
1187 card->erase_shift = 0;
1190 * It is possible to erase an arbitrarily large area of an SD or MMC
1191 * card. That is not desirable because it can take a long time
1192 * (minutes) potentially delaying more important I/O, and also the
1193 * timeout calculations become increasingly hugely over-estimated.
1194 * Consequently, 'pref_erase' is defined as a guide to limit erases
1195 * to that size and alignment.
1197 * For SD cards that define Allocation Unit size, limit erases to one
1198 * Allocation Unit at a time. For MMC cards that define High Capacity
1199 * Erase Size, whether it is switched on or not, limit to that size.
1200 * Otherwise just have a stab at a good value. For modern cards it
1201 * will end up being 4MiB. Note that if the value is too small, it
1202 * can end up taking longer to erase.
1204 if (mmc_card_sd(card) && card->ssr.au) {
1205 card->pref_erase = card->ssr.au;
1206 card->erase_shift = ffs(card->ssr.au) - 1;
1207 } else if (card->ext_csd.hc_erase_size) {
1208 card->pref_erase = card->ext_csd.hc_erase_size;
1210 sz = (card->csd.capacity << (card->csd.read_blkbits - 9)) >> 11;
1212 card->pref_erase = 512 * 1024 / 512;
1214 card->pref_erase = 1024 * 1024 / 512;
1216 card->pref_erase = 2 * 1024 * 1024 / 512;
1218 card->pref_erase = 4 * 1024 * 1024 / 512;
1219 if (card->pref_erase < card->erase_size)
1220 card->pref_erase = card->erase_size;
1222 sz = card->pref_erase % card->erase_size;
1224 card->pref_erase += card->erase_size - sz;
1229 static unsigned int mmc_mmc_erase_timeout(struct mmc_card *card,
1230 unsigned int arg, unsigned int qty)
1232 unsigned int erase_timeout;
1234 if (card->ext_csd.erase_group_def & 1) {
1235 /* High Capacity Erase Group Size uses HC timeouts */
1236 if (arg == MMC_TRIM_ARG)
1237 erase_timeout = card->ext_csd.trim_timeout;
1239 erase_timeout = card->ext_csd.hc_erase_timeout;
1241 /* CSD Erase Group Size uses write timeout */
1242 unsigned int mult = (10 << card->csd.r2w_factor);
1243 unsigned int timeout_clks = card->csd.tacc_clks * mult;
1244 unsigned int timeout_us;
1246 /* Avoid overflow: e.g. tacc_ns=80000000 mult=1280 */
1247 if (card->csd.tacc_ns < 1000000)
1248 timeout_us = (card->csd.tacc_ns * mult) / 1000;
1250 timeout_us = (card->csd.tacc_ns / 1000) * mult;
1253 * ios.clock is only a target. The real clock rate might be
1254 * less but not that much less, so fudge it by multiplying by 2.
1257 timeout_us += (timeout_clks * 1000) /
1258 (card->host->ios.clock / 1000);
1260 erase_timeout = timeout_us / 1000;
1263 * Theoretically, the calculation could underflow so round up
1264 * to 1ms in that case.
1270 /* Multiplier for secure operations */
1271 if (arg & MMC_SECURE_ARGS) {
1272 if (arg == MMC_SECURE_ERASE_ARG)
1273 erase_timeout *= card->ext_csd.sec_erase_mult;
1275 erase_timeout *= card->ext_csd.sec_trim_mult;
1278 erase_timeout *= qty;
1281 * Ensure at least a 1 second timeout for SPI as per
1282 * 'mmc_set_data_timeout()'
1284 if (mmc_host_is_spi(card->host) && erase_timeout < 1000)
1285 erase_timeout = 1000;
1287 return erase_timeout;
1290 static unsigned int mmc_sd_erase_timeout(struct mmc_card *card,
1294 unsigned int erase_timeout;
1296 if (card->ssr.erase_timeout) {
1297 /* Erase timeout specified in SD Status Register (SSR) */
1298 erase_timeout = card->ssr.erase_timeout * qty +
1299 card->ssr.erase_offset;
1302 * Erase timeout not specified in SD Status Register (SSR) so
1303 * use 250ms per write block.
1305 erase_timeout = 250 * qty;
1308 /* Must not be less than 1 second */
1309 if (erase_timeout < 1000)
1310 erase_timeout = 1000;
1312 return erase_timeout;
1315 static unsigned int mmc_erase_timeout(struct mmc_card *card,
1319 if (mmc_card_sd(card))
1320 return mmc_sd_erase_timeout(card, arg, qty);
1322 return mmc_mmc_erase_timeout(card, arg, qty);
1325 static int mmc_do_erase(struct mmc_card *card, unsigned int from,
1326 unsigned int to, unsigned int arg)
1328 struct mmc_command cmd = {0};
1329 unsigned int qty = 0;
1333 * qty is used to calculate the erase timeout which depends on how many
1334 * erase groups (or allocation units in SD terminology) are affected.
1335 * We count erasing part of an erase group as one erase group.
1336 * For SD, the allocation units are always a power of 2. For MMC, the
1337 * erase group size is almost certainly also power of 2, but it does not
1338 * seem to insist on that in the JEDEC standard, so we fall back to
1339 * division in that case. SD may not specify an allocation unit size,
1340 * in which case the timeout is based on the number of write blocks.
1342 * Note that the timeout for secure trim 2 will only be correct if the
1343 * number of erase groups specified is the same as the total of all
1344 * preceding secure trim 1 commands. Since the power may have been
1345 * lost since the secure trim 1 commands occurred, it is generally
1346 * impossible to calculate the secure trim 2 timeout correctly.
1348 if (card->erase_shift)
1349 qty += ((to >> card->erase_shift) -
1350 (from >> card->erase_shift)) + 1;
1351 else if (mmc_card_sd(card))
1352 qty += to - from + 1;
1354 qty += ((to / card->erase_size) -
1355 (from / card->erase_size)) + 1;
1357 if (!mmc_card_blockaddr(card)) {
1362 if (mmc_card_sd(card))
1363 cmd.opcode = SD_ERASE_WR_BLK_START;
1365 cmd.opcode = MMC_ERASE_GROUP_START;
1367 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
1368 err = mmc_wait_for_cmd(card->host, &cmd, 0);
1370 printk(KERN_ERR "mmc_erase: group start error %d, "
1371 "status %#x\n", err, cmd.resp[0]);
1376 memset(&cmd, 0, sizeof(struct mmc_command));
1377 if (mmc_card_sd(card))
1378 cmd.opcode = SD_ERASE_WR_BLK_END;
1380 cmd.opcode = MMC_ERASE_GROUP_END;
1382 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
1383 err = mmc_wait_for_cmd(card->host, &cmd, 0);
1385 printk(KERN_ERR "mmc_erase: group end error %d, status %#x\n",
1391 memset(&cmd, 0, sizeof(struct mmc_command));
1392 cmd.opcode = MMC_ERASE;
1394 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1395 cmd.cmd_timeout_ms = mmc_erase_timeout(card, arg, qty);
1396 err = mmc_wait_for_cmd(card->host, &cmd, 0);
1398 printk(KERN_ERR "mmc_erase: erase error %d, status %#x\n",
1404 if (mmc_host_is_spi(card->host))
1408 memset(&cmd, 0, sizeof(struct mmc_command));
1409 cmd.opcode = MMC_SEND_STATUS;
1410 cmd.arg = card->rca << 16;
1411 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
1412 /* Do not retry else we can't see errors */
1413 err = mmc_wait_for_cmd(card->host, &cmd, 0);
1414 if (err || (cmd.resp[0] & 0xFDF92000)) {
1415 printk(KERN_ERR "error %d requesting status %#x\n",
1420 } while (!(cmd.resp[0] & R1_READY_FOR_DATA) ||
1421 R1_CURRENT_STATE(cmd.resp[0]) == 7);
1427 * mmc_erase - erase sectors.
1428 * @card: card to erase
1429 * @from: first sector to erase
1430 * @nr: number of sectors to erase
1431 * @arg: erase command argument (SD supports only %MMC_ERASE_ARG)
1433 * Caller must claim host before calling this function.
1435 int mmc_erase(struct mmc_card *card, unsigned int from, unsigned int nr,
1438 unsigned int rem, to = from + nr;
1440 if (!(card->host->caps & MMC_CAP_ERASE) ||
1441 !(card->csd.cmdclass & CCC_ERASE))
1444 if (!card->erase_size)
1447 if (mmc_card_sd(card) && arg != MMC_ERASE_ARG)
1450 if ((arg & MMC_SECURE_ARGS) &&
1451 !(card->ext_csd.sec_feature_support & EXT_CSD_SEC_ER_EN))
1454 if ((arg & MMC_TRIM_ARGS) &&
1455 !(card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN))
1458 if (arg == MMC_SECURE_ERASE_ARG) {
1459 if (from % card->erase_size || nr % card->erase_size)
1463 if (arg == MMC_ERASE_ARG) {
1464 rem = from % card->erase_size;
1466 rem = card->erase_size - rem;
1473 rem = nr % card->erase_size;
1486 /* 'from' and 'to' are inclusive */
1489 return mmc_do_erase(card, from, to, arg);
1491 EXPORT_SYMBOL(mmc_erase);
1493 int mmc_can_erase(struct mmc_card *card)
1495 if ((card->host->caps & MMC_CAP_ERASE) &&
1496 (card->csd.cmdclass & CCC_ERASE) && card->erase_size)
1500 EXPORT_SYMBOL(mmc_can_erase);
1502 int mmc_can_trim(struct mmc_card *card)
1504 if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN)
1508 EXPORT_SYMBOL(mmc_can_trim);
1510 int mmc_can_secure_erase_trim(struct mmc_card *card)
1512 if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_ER_EN)
1516 EXPORT_SYMBOL(mmc_can_secure_erase_trim);
1518 int mmc_erase_group_aligned(struct mmc_card *card, unsigned int from,
1521 if (!card->erase_size)
1523 if (from % card->erase_size || nr % card->erase_size)
1527 EXPORT_SYMBOL(mmc_erase_group_aligned);
1529 int mmc_set_blocklen(struct mmc_card *card, unsigned int blocklen)
1531 struct mmc_command cmd = {0};
1533 if (mmc_card_blockaddr(card) || mmc_card_ddr_mode(card))
1536 cmd.opcode = MMC_SET_BLOCKLEN;
1538 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
1539 return mmc_wait_for_cmd(card->host, &cmd, 5);
1541 EXPORT_SYMBOL(mmc_set_blocklen);
1543 static int mmc_rescan_try_freq(struct mmc_host *host, unsigned freq)
1545 host->f_init = freq;
1547 #ifdef CONFIG_MMC_DEBUG
1548 pr_info("%s: %s: trying to init card at %u Hz\n",
1549 mmc_hostname(host), __func__, host->f_init);
1554 * sdio_reset sends CMD52 to reset card. Since we do not know
1555 * if the card is being re-initialized, just send it. CMD52
1556 * should be ignored by SD/eMMC cards.
1561 mmc_send_if_cond(host, host->ocr_avail);
1563 /* Order's important: probe SDIO, then SD, then MMC */
1564 if (!mmc_attach_sdio(host))
1566 if (!mmc_attach_sd(host))
1568 if (!mmc_attach_mmc(host))
1571 mmc_power_off(host);
1575 void mmc_rescan(struct work_struct *work)
1577 static const unsigned freqs[] = { 400000, 300000, 200000, 100000 };
1578 struct mmc_host *host =
1579 container_of(work, struct mmc_host, detect.work);
1582 if (host->rescan_disable)
1588 * if there is a _removable_ card registered, check whether it is
1591 if (host->bus_ops && host->bus_ops->detect && !host->bus_dead
1592 && !(host->caps & MMC_CAP_NONREMOVABLE))
1593 host->bus_ops->detect(host);
1596 * Let mmc_bus_put() free the bus/bus_ops if we've found that
1597 * the card is no longer present.
1602 /* if there still is a card present, stop here */
1603 if (host->bus_ops != NULL) {
1609 * Only we can add a new handler, so it's safe to
1610 * release the lock here.
1614 if (host->ops->get_cd && host->ops->get_cd(host) == 0)
1617 mmc_claim_host(host);
1618 for (i = 0; i < ARRAY_SIZE(freqs); i++) {
1619 if (!mmc_rescan_try_freq(host, max(freqs[i], host->f_min)))
1621 if (freqs[i] < host->f_min)
1624 mmc_release_host(host);
1627 if (host->caps & MMC_CAP_NEEDS_POLL)
1628 mmc_schedule_delayed_work(&host->detect, HZ);
1631 void mmc_start_host(struct mmc_host *host)
1633 mmc_power_off(host);
1634 mmc_detect_change(host, 0);
1637 void mmc_stop_host(struct mmc_host *host)
1639 #ifdef CONFIG_MMC_DEBUG
1640 unsigned long flags;
1641 spin_lock_irqsave(&host->lock, flags);
1643 spin_unlock_irqrestore(&host->lock, flags);
1646 if (host->caps & MMC_CAP_DISABLE)
1647 cancel_delayed_work(&host->disable);
1648 cancel_delayed_work_sync(&host->detect);
1649 mmc_flush_scheduled_work();
1651 /* clear pm flags now and let card drivers set them as needed */
1655 if (host->bus_ops && !host->bus_dead) {
1656 if (host->bus_ops->remove)
1657 host->bus_ops->remove(host);
1659 mmc_claim_host(host);
1660 mmc_detach_bus(host);
1661 mmc_release_host(host);
1669 mmc_power_off(host);
1672 int mmc_power_save_host(struct mmc_host *host)
1678 if (!host->bus_ops || host->bus_dead || !host->bus_ops->power_restore) {
1683 if (host->bus_ops->power_save)
1684 ret = host->bus_ops->power_save(host);
1688 mmc_power_off(host);
1692 EXPORT_SYMBOL(mmc_power_save_host);
1694 int mmc_power_restore_host(struct mmc_host *host)
1700 if (!host->bus_ops || host->bus_dead || !host->bus_ops->power_restore) {
1706 ret = host->bus_ops->power_restore(host);
1712 EXPORT_SYMBOL(mmc_power_restore_host);
1714 int mmc_card_awake(struct mmc_host *host)
1720 if (host->bus_ops && !host->bus_dead && host->bus_ops->awake)
1721 err = host->bus_ops->awake(host);
1727 EXPORT_SYMBOL(mmc_card_awake);
1729 int mmc_card_sleep(struct mmc_host *host)
1735 if (host->bus_ops && !host->bus_dead && host->bus_ops->awake)
1736 err = host->bus_ops->sleep(host);
1742 EXPORT_SYMBOL(mmc_card_sleep);
1744 int mmc_card_can_sleep(struct mmc_host *host)
1746 struct mmc_card *card = host->card;
1748 if (card && mmc_card_mmc(card) && card->ext_csd.rev >= 3)
1752 EXPORT_SYMBOL(mmc_card_can_sleep);
1757 * mmc_suspend_host - suspend a host
1760 int mmc_suspend_host(struct mmc_host *host)
1764 if (host->caps & MMC_CAP_DISABLE)
1765 cancel_delayed_work(&host->disable);
1766 cancel_delayed_work(&host->detect);
1767 mmc_flush_scheduled_work();
1770 if (host->bus_ops && !host->bus_dead) {
1771 if (host->bus_ops->suspend)
1772 err = host->bus_ops->suspend(host);
1773 if (err == -ENOSYS || !host->bus_ops->resume) {
1775 * We simply "remove" the card in this case.
1776 * It will be redetected on resume.
1778 if (host->bus_ops->remove)
1779 host->bus_ops->remove(host);
1780 mmc_claim_host(host);
1781 mmc_detach_bus(host);
1782 mmc_release_host(host);
1789 if (!err && !mmc_card_keep_power(host))
1790 mmc_power_off(host);
1795 EXPORT_SYMBOL(mmc_suspend_host);
1798 * mmc_resume_host - resume a previously suspended host
1801 int mmc_resume_host(struct mmc_host *host)
1806 if (host->bus_ops && !host->bus_dead) {
1807 if (!mmc_card_keep_power(host)) {
1809 mmc_select_voltage(host, host->ocr);
1811 * Tell runtime PM core we just powered up the card,
1812 * since it still believes the card is powered off.
1813 * Note that currently runtime PM is only enabled
1814 * for SDIO cards that are MMC_CAP_POWER_OFF_CARD
1816 if (mmc_card_sdio(host->card) &&
1817 (host->caps & MMC_CAP_POWER_OFF_CARD)) {
1818 pm_runtime_disable(&host->card->dev);
1819 pm_runtime_set_active(&host->card->dev);
1820 pm_runtime_enable(&host->card->dev);
1823 BUG_ON(!host->bus_ops->resume);
1824 err = host->bus_ops->resume(host);
1826 printk(KERN_WARNING "%s: error %d during resume "
1827 "(card was removed?)\n",
1828 mmc_hostname(host), err);
1836 EXPORT_SYMBOL(mmc_resume_host);
1838 /* Do the card removal on suspend if card is assumed removeable
1839 * Do that in pm notifier while userspace isn't yet frozen, so we will be able
1842 int mmc_pm_notify(struct notifier_block *notify_block,
1843 unsigned long mode, void *unused)
1845 struct mmc_host *host = container_of(
1846 notify_block, struct mmc_host, pm_notify);
1847 unsigned long flags;
1851 case PM_HIBERNATION_PREPARE:
1852 case PM_SUSPEND_PREPARE:
1854 spin_lock_irqsave(&host->lock, flags);
1855 host->rescan_disable = 1;
1856 spin_unlock_irqrestore(&host->lock, flags);
1857 cancel_delayed_work_sync(&host->detect);
1859 if (!host->bus_ops || host->bus_ops->suspend)
1862 mmc_claim_host(host);
1864 if (host->bus_ops->remove)
1865 host->bus_ops->remove(host);
1867 mmc_detach_bus(host);
1868 mmc_release_host(host);
1872 case PM_POST_SUSPEND:
1873 case PM_POST_HIBERNATION:
1874 case PM_POST_RESTORE:
1876 spin_lock_irqsave(&host->lock, flags);
1877 host->rescan_disable = 0;
1878 spin_unlock_irqrestore(&host->lock, flags);
1879 mmc_detect_change(host, 0);
1887 static int __init mmc_init(void)
1891 workqueue = alloc_ordered_workqueue("kmmcd", 0);
1895 ret = mmc_register_bus();
1897 goto destroy_workqueue;
1899 ret = mmc_register_host_class();
1901 goto unregister_bus;
1903 ret = sdio_register_bus();
1905 goto unregister_host_class;
1909 unregister_host_class:
1910 mmc_unregister_host_class();
1912 mmc_unregister_bus();
1914 destroy_workqueue(workqueue);
1919 static void __exit mmc_exit(void)
1921 sdio_unregister_bus();
1922 mmc_unregister_host_class();
1923 mmc_unregister_bus();
1924 destroy_workqueue(workqueue);
1927 subsys_initcall(mmc_init);
1928 module_exit(mmc_exit);
1930 MODULE_LICENSE("GPL");