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/suspend.h>
27 #include <linux/fault-inject.h>
28 #include <linux/random.h>
30 #include <linux/mmc/card.h>
31 #include <linux/mmc/host.h>
32 #include <linux/mmc/mmc.h>
33 #include <linux/mmc/sd.h>
44 static struct workqueue_struct *workqueue;
47 * Enabling software CRCs on the data blocks can be a significant (30%)
48 * performance cost, and for other reasons may not always be desired.
49 * So we allow it it to be disabled.
52 module_param(use_spi_crc, bool, 0);
55 * We normally treat cards as removed during suspend if they are not
56 * known to be on a non-removable bus, to avoid the risk of writing
57 * back data to a different card after resume. Allow this to be
58 * overridden if necessary.
60 #ifdef CONFIG_MMC_UNSAFE_RESUME
61 int mmc_assume_removable;
63 int mmc_assume_removable = 1;
65 EXPORT_SYMBOL(mmc_assume_removable);
66 module_param_named(removable, mmc_assume_removable, bool, 0644);
69 "MMC/SD cards are removable and may be removed during suspend");
72 * Internal function. Schedule delayed work in the MMC work queue.
74 static int mmc_schedule_delayed_work(struct delayed_work *work,
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);
88 #ifdef CONFIG_FAIL_MMC_REQUEST
91 * Internal function. Inject random data errors.
92 * If mmc_data is NULL no errors are injected.
94 static void mmc_should_fail_request(struct mmc_host *host,
95 struct mmc_request *mrq)
97 struct mmc_command *cmd = mrq->cmd;
98 struct mmc_data *data = mrq->data;
99 static const int data_errors[] = {
108 if (cmd->error || data->error ||
109 !should_fail(&host->fail_mmc_request, data->blksz * data->blocks))
112 data->error = data_errors[random32() % ARRAY_SIZE(data_errors)];
113 data->bytes_xfered = (random32() % (data->bytes_xfered >> 9)) << 9;
116 #else /* CONFIG_FAIL_MMC_REQUEST */
118 static inline void mmc_should_fail_request(struct mmc_host *host,
119 struct mmc_request *mrq)
123 #endif /* CONFIG_FAIL_MMC_REQUEST */
126 * mmc_request_done - finish processing an MMC request
127 * @host: MMC host which completed request
128 * @mrq: MMC request which request
130 * MMC drivers should call this function when they have completed
131 * their processing of a request.
133 void mmc_request_done(struct mmc_host *host, struct mmc_request *mrq)
135 struct mmc_command *cmd = mrq->cmd;
136 int err = cmd->error;
138 if (err && cmd->retries && mmc_host_is_spi(host)) {
139 if (cmd->resp[0] & R1_SPI_ILLEGAL_COMMAND)
143 if (err && cmd->retries) {
145 * Request starter must handle retries - see
146 * mmc_wait_for_req_done().
151 mmc_should_fail_request(host, mrq);
153 led_trigger_event(host->led, LED_OFF);
155 pr_debug("%s: req done (CMD%u): %d: %08x %08x %08x %08x\n",
156 mmc_hostname(host), cmd->opcode, err,
157 cmd->resp[0], cmd->resp[1],
158 cmd->resp[2], cmd->resp[3]);
161 pr_debug("%s: %d bytes transferred: %d\n",
163 mrq->data->bytes_xfered, mrq->data->error);
167 pr_debug("%s: (CMD%u): %d: %08x %08x %08x %08x\n",
168 mmc_hostname(host), mrq->stop->opcode,
170 mrq->stop->resp[0], mrq->stop->resp[1],
171 mrq->stop->resp[2], mrq->stop->resp[3]);
177 mmc_host_clk_release(host);
181 EXPORT_SYMBOL(mmc_request_done);
184 mmc_start_request(struct mmc_host *host, struct mmc_request *mrq)
186 #ifdef CONFIG_MMC_DEBUG
188 struct scatterlist *sg;
191 pr_debug("%s: starting CMD%u arg %08x flags %08x\n",
192 mmc_hostname(host), mrq->cmd->opcode,
193 mrq->cmd->arg, mrq->cmd->flags);
196 pr_debug("%s: blksz %d blocks %d flags %08x "
197 "tsac %d ms nsac %d\n",
198 mmc_hostname(host), mrq->data->blksz,
199 mrq->data->blocks, mrq->data->flags,
200 mrq->data->timeout_ns / 1000000,
201 mrq->data->timeout_clks);
205 pr_debug("%s: CMD%u arg %08x flags %08x\n",
206 mmc_hostname(host), mrq->stop->opcode,
207 mrq->stop->arg, mrq->stop->flags);
210 WARN_ON(!host->claimed);
215 BUG_ON(mrq->data->blksz > host->max_blk_size);
216 BUG_ON(mrq->data->blocks > host->max_blk_count);
217 BUG_ON(mrq->data->blocks * mrq->data->blksz >
220 #ifdef CONFIG_MMC_DEBUG
222 for_each_sg(mrq->data->sg, sg, mrq->data->sg_len, i)
224 BUG_ON(sz != mrq->data->blocks * mrq->data->blksz);
227 mrq->cmd->data = mrq->data;
228 mrq->data->error = 0;
229 mrq->data->mrq = mrq;
231 mrq->data->stop = mrq->stop;
232 mrq->stop->error = 0;
233 mrq->stop->mrq = mrq;
236 mmc_host_clk_hold(host);
237 led_trigger_event(host->led, LED_FULL);
238 host->ops->request(host, mrq);
241 static void mmc_wait_done(struct mmc_request *mrq)
243 complete(&mrq->completion);
246 static void __mmc_start_req(struct mmc_host *host, struct mmc_request *mrq)
248 init_completion(&mrq->completion);
249 mrq->done = mmc_wait_done;
250 mmc_start_request(host, mrq);
253 static void mmc_wait_for_req_done(struct mmc_host *host,
254 struct mmc_request *mrq)
256 struct mmc_command *cmd;
259 wait_for_completion(&mrq->completion);
262 if (!cmd->error || !cmd->retries)
265 pr_debug("%s: req failed (CMD%u): %d, retrying...\n",
266 mmc_hostname(host), cmd->opcode, cmd->error);
269 host->ops->request(host, mrq);
274 * mmc_pre_req - Prepare for a new request
275 * @host: MMC host to prepare command
276 * @mrq: MMC request to prepare for
277 * @is_first_req: true if there is no previous started request
278 * that may run in parellel to this call, otherwise false
280 * mmc_pre_req() is called in prior to mmc_start_req() to let
281 * host prepare for the new request. Preparation of a request may be
282 * performed while another request is running on the host.
284 static void mmc_pre_req(struct mmc_host *host, struct mmc_request *mrq,
287 if (host->ops->pre_req)
288 host->ops->pre_req(host, mrq, is_first_req);
292 * mmc_post_req - Post process a completed request
293 * @host: MMC host to post process command
294 * @mrq: MMC request to post process for
295 * @err: Error, if non zero, clean up any resources made in pre_req
297 * Let the host post process a completed request. Post processing of
298 * a request may be performed while another reuqest is running.
300 static void mmc_post_req(struct mmc_host *host, struct mmc_request *mrq,
303 if (host->ops->post_req)
304 host->ops->post_req(host, mrq, err);
308 * mmc_start_req - start a non-blocking request
309 * @host: MMC host to start command
310 * @areq: async request to start
311 * @error: out parameter returns 0 for success, otherwise non zero
313 * Start a new MMC custom command request for a host.
314 * If there is on ongoing async request wait for completion
315 * of that request and start the new one and return.
316 * Does not wait for the new request to complete.
318 * Returns the completed request, NULL in case of none completed.
319 * Wait for the an ongoing request (previoulsy started) to complete and
320 * return the completed request. If there is no ongoing request, NULL
321 * is returned without waiting. NULL is not an error condition.
323 struct mmc_async_req *mmc_start_req(struct mmc_host *host,
324 struct mmc_async_req *areq, int *error)
327 struct mmc_async_req *data = host->areq;
329 /* Prepare a new request */
331 mmc_pre_req(host, areq->mrq, !host->areq);
334 mmc_wait_for_req_done(host, host->areq->mrq);
335 err = host->areq->err_check(host->card, host->areq);
337 /* post process the completed failed request */
338 mmc_post_req(host, host->areq->mrq, 0);
341 * Cancel the new prepared request, because
342 * it can't run until the failed
343 * request has been properly handled.
345 mmc_post_req(host, areq->mrq, -EINVAL);
353 __mmc_start_req(host, areq->mrq);
356 mmc_post_req(host, host->areq->mrq, 0);
364 EXPORT_SYMBOL(mmc_start_req);
367 * mmc_wait_for_req - start a request and wait for completion
368 * @host: MMC host to start command
369 * @mrq: MMC request to start
371 * Start a new MMC custom command request for a host, and wait
372 * for the command to complete. Does not attempt to parse the
375 void mmc_wait_for_req(struct mmc_host *host, struct mmc_request *mrq)
377 __mmc_start_req(host, mrq);
378 mmc_wait_for_req_done(host, mrq);
380 EXPORT_SYMBOL(mmc_wait_for_req);
383 * mmc_wait_for_cmd - start a command and wait for completion
384 * @host: MMC host to start command
385 * @cmd: MMC command to start
386 * @retries: maximum number of retries
388 * Start a new MMC command for a host, and wait for the command
389 * to complete. Return any error that occurred while the command
390 * was executing. Do not attempt to parse the response.
392 int mmc_wait_for_cmd(struct mmc_host *host, struct mmc_command *cmd, int retries)
394 struct mmc_request mrq = {NULL};
396 WARN_ON(!host->claimed);
398 memset(cmd->resp, 0, sizeof(cmd->resp));
399 cmd->retries = retries;
404 mmc_wait_for_req(host, &mrq);
409 EXPORT_SYMBOL(mmc_wait_for_cmd);
412 * mmc_set_data_timeout - set the timeout for a data command
413 * @data: data phase for command
414 * @card: the MMC card associated with the data transfer
416 * Computes the data timeout parameters according to the
417 * correct algorithm given the card type.
419 void mmc_set_data_timeout(struct mmc_data *data, const struct mmc_card *card)
424 * SDIO cards only define an upper 1 s limit on access.
426 if (mmc_card_sdio(card)) {
427 data->timeout_ns = 1000000000;
428 data->timeout_clks = 0;
433 * SD cards use a 100 multiplier rather than 10
435 mult = mmc_card_sd(card) ? 100 : 10;
438 * Scale up the multiplier (and therefore the timeout) by
439 * the r2w factor for writes.
441 if (data->flags & MMC_DATA_WRITE)
442 mult <<= card->csd.r2w_factor;
444 data->timeout_ns = card->csd.tacc_ns * mult;
445 data->timeout_clks = card->csd.tacc_clks * mult;
448 * SD cards also have an upper limit on the timeout.
450 if (mmc_card_sd(card)) {
451 unsigned int timeout_us, limit_us;
453 timeout_us = data->timeout_ns / 1000;
454 if (mmc_host_clk_rate(card->host))
455 timeout_us += data->timeout_clks * 1000 /
456 (mmc_host_clk_rate(card->host) / 1000);
458 if (data->flags & MMC_DATA_WRITE)
460 * The limit is really 250 ms, but that is
461 * insufficient for some crappy cards.
468 * SDHC cards always use these fixed values.
470 if (timeout_us > limit_us || mmc_card_blockaddr(card)) {
471 data->timeout_ns = limit_us * 1000;
472 data->timeout_clks = 0;
476 * Some cards need very high timeouts if driven in SPI mode.
477 * The worst observed timeout was 900ms after writing a
478 * continuous stream of data until the internal logic
481 if (mmc_host_is_spi(card->host)) {
482 if (data->flags & MMC_DATA_WRITE) {
483 if (data->timeout_ns < 1000000000)
484 data->timeout_ns = 1000000000; /* 1s */
486 if (data->timeout_ns < 100000000)
487 data->timeout_ns = 100000000; /* 100ms */
491 EXPORT_SYMBOL(mmc_set_data_timeout);
494 * mmc_align_data_size - pads a transfer size to a more optimal value
495 * @card: the MMC card associated with the data transfer
496 * @sz: original transfer size
498 * Pads the original data size with a number of extra bytes in
499 * order to avoid controller bugs and/or performance hits
500 * (e.g. some controllers revert to PIO for certain sizes).
502 * Returns the improved size, which might be unmodified.
504 * Note that this function is only relevant when issuing a
505 * single scatter gather entry.
507 unsigned int mmc_align_data_size(struct mmc_card *card, unsigned int sz)
510 * FIXME: We don't have a system for the controller to tell
511 * the core about its problems yet, so for now we just 32-bit
514 sz = ((sz + 3) / 4) * 4;
518 EXPORT_SYMBOL(mmc_align_data_size);
521 * mmc_host_enable - enable a host.
522 * @host: mmc host to enable
524 * Hosts that support power saving can use the 'enable' and 'disable'
525 * methods to exit and enter power saving states. For more information
526 * see comments for struct mmc_host_ops.
528 int mmc_host_enable(struct mmc_host *host)
530 if (!(host->caps & MMC_CAP_DISABLE))
533 if (host->en_dis_recurs)
536 if (host->nesting_cnt++)
539 cancel_delayed_work_sync(&host->disable);
544 if (host->ops->enable) {
547 host->en_dis_recurs = 1;
548 err = host->ops->enable(host);
549 host->en_dis_recurs = 0;
552 pr_debug("%s: enable error %d\n",
553 mmc_hostname(host), err);
560 EXPORT_SYMBOL(mmc_host_enable);
562 static int mmc_host_do_disable(struct mmc_host *host, int lazy)
564 if (host->ops->disable) {
567 host->en_dis_recurs = 1;
568 err = host->ops->disable(host, lazy);
569 host->en_dis_recurs = 0;
572 pr_debug("%s: disable error %d\n",
573 mmc_hostname(host), err);
577 unsigned long delay = msecs_to_jiffies(err);
579 mmc_schedule_delayed_work(&host->disable, delay);
587 * mmc_host_disable - disable a host.
588 * @host: mmc host to disable
590 * Hosts that support power saving can use the 'enable' and 'disable'
591 * methods to exit and enter power saving states. For more information
592 * see comments for struct mmc_host_ops.
594 int mmc_host_disable(struct mmc_host *host)
598 if (!(host->caps & MMC_CAP_DISABLE))
601 if (host->en_dis_recurs)
604 if (--host->nesting_cnt)
610 err = mmc_host_do_disable(host, 0);
613 EXPORT_SYMBOL(mmc_host_disable);
616 * __mmc_claim_host - exclusively claim a host
617 * @host: mmc host to claim
618 * @abort: whether or not the operation should be aborted
620 * Claim a host for a set of operations. If @abort is non null and
621 * dereference a non-zero value then this will return prematurely with
622 * that non-zero value without acquiring the lock. Returns zero
623 * with the lock held otherwise.
625 int __mmc_claim_host(struct mmc_host *host, atomic_t *abort)
627 DECLARE_WAITQUEUE(wait, current);
633 add_wait_queue(&host->wq, &wait);
634 spin_lock_irqsave(&host->lock, flags);
636 set_current_state(TASK_UNINTERRUPTIBLE);
637 stop = abort ? atomic_read(abort) : 0;
638 if (stop || !host->claimed || host->claimer == current)
640 spin_unlock_irqrestore(&host->lock, flags);
642 spin_lock_irqsave(&host->lock, flags);
644 set_current_state(TASK_RUNNING);
647 host->claimer = current;
648 host->claim_cnt += 1;
651 spin_unlock_irqrestore(&host->lock, flags);
652 remove_wait_queue(&host->wq, &wait);
654 mmc_host_enable(host);
658 EXPORT_SYMBOL(__mmc_claim_host);
661 * mmc_try_claim_host - try exclusively to claim a host
662 * @host: mmc host to claim
664 * Returns %1 if the host is claimed, %0 otherwise.
666 int mmc_try_claim_host(struct mmc_host *host)
668 int claimed_host = 0;
671 spin_lock_irqsave(&host->lock, flags);
672 if (!host->claimed || host->claimer == current) {
674 host->claimer = current;
675 host->claim_cnt += 1;
678 spin_unlock_irqrestore(&host->lock, flags);
681 EXPORT_SYMBOL(mmc_try_claim_host);
684 * mmc_do_release_host - release a claimed host
685 * @host: mmc host to release
687 * If you successfully claimed a host, this function will
690 void mmc_do_release_host(struct mmc_host *host)
694 spin_lock_irqsave(&host->lock, flags);
695 if (--host->claim_cnt) {
696 /* Release for nested claim */
697 spin_unlock_irqrestore(&host->lock, flags);
700 host->claimer = NULL;
701 spin_unlock_irqrestore(&host->lock, flags);
705 EXPORT_SYMBOL(mmc_do_release_host);
707 void mmc_host_deeper_disable(struct work_struct *work)
709 struct mmc_host *host =
710 container_of(work, struct mmc_host, disable.work);
712 /* If the host is claimed then we do not want to disable it anymore */
713 if (!mmc_try_claim_host(host))
715 mmc_host_do_disable(host, 1);
716 mmc_do_release_host(host);
720 * mmc_host_lazy_disable - lazily disable a host.
721 * @host: mmc host to disable
723 * Hosts that support power saving can use the 'enable' and 'disable'
724 * methods to exit and enter power saving states. For more information
725 * see comments for struct mmc_host_ops.
727 int mmc_host_lazy_disable(struct mmc_host *host)
729 if (!(host->caps & MMC_CAP_DISABLE))
732 if (host->en_dis_recurs)
735 if (--host->nesting_cnt)
741 if (host->disable_delay) {
742 mmc_schedule_delayed_work(&host->disable,
743 msecs_to_jiffies(host->disable_delay));
746 return mmc_host_do_disable(host, 1);
748 EXPORT_SYMBOL(mmc_host_lazy_disable);
751 * mmc_release_host - release a host
752 * @host: mmc host to release
754 * Release a MMC host, allowing others to claim the host
755 * for their operations.
757 void mmc_release_host(struct mmc_host *host)
759 WARN_ON(!host->claimed);
761 mmc_host_lazy_disable(host);
763 mmc_do_release_host(host);
766 EXPORT_SYMBOL(mmc_release_host);
769 * Internal function that does the actual ios call to the host driver,
770 * optionally printing some debug output.
772 static inline void mmc_set_ios(struct mmc_host *host)
774 struct mmc_ios *ios = &host->ios;
776 pr_debug("%s: clock %uHz busmode %u powermode %u cs %u Vdd %u "
777 "width %u timing %u\n",
778 mmc_hostname(host), ios->clock, ios->bus_mode,
779 ios->power_mode, ios->chip_select, ios->vdd,
780 ios->bus_width, ios->timing);
783 mmc_set_ungated(host);
784 host->ops->set_ios(host, ios);
788 * Control chip select pin on a host.
790 void mmc_set_chip_select(struct mmc_host *host, int mode)
792 mmc_host_clk_hold(host);
793 host->ios.chip_select = mode;
795 mmc_host_clk_release(host);
799 * Sets the host clock to the highest possible frequency that
802 static void __mmc_set_clock(struct mmc_host *host, unsigned int hz)
804 WARN_ON(hz < host->f_min);
806 if (hz > host->f_max)
809 host->ios.clock = hz;
813 void mmc_set_clock(struct mmc_host *host, unsigned int hz)
815 mmc_host_clk_hold(host);
816 __mmc_set_clock(host, hz);
817 mmc_host_clk_release(host);
820 #ifdef CONFIG_MMC_CLKGATE
822 * This gates the clock by setting it to 0 Hz.
824 void mmc_gate_clock(struct mmc_host *host)
828 spin_lock_irqsave(&host->clk_lock, flags);
829 host->clk_old = host->ios.clock;
831 host->clk_gated = true;
832 spin_unlock_irqrestore(&host->clk_lock, flags);
837 * This restores the clock from gating by using the cached
840 void mmc_ungate_clock(struct mmc_host *host)
843 * We should previously have gated the clock, so the clock shall
844 * be 0 here! The clock may however be 0 during initialization,
845 * when some request operations are performed before setting
846 * the frequency. When ungate is requested in that situation
847 * we just ignore the call.
850 BUG_ON(host->ios.clock);
851 /* This call will also set host->clk_gated to false */
852 __mmc_set_clock(host, host->clk_old);
856 void mmc_set_ungated(struct mmc_host *host)
861 * We've been given a new frequency while the clock is gated,
862 * so make sure we regard this as ungating it.
864 spin_lock_irqsave(&host->clk_lock, flags);
865 host->clk_gated = false;
866 spin_unlock_irqrestore(&host->clk_lock, flags);
870 void mmc_set_ungated(struct mmc_host *host)
876 * Change the bus mode (open drain/push-pull) of a host.
878 void mmc_set_bus_mode(struct mmc_host *host, unsigned int mode)
880 mmc_host_clk_hold(host);
881 host->ios.bus_mode = mode;
883 mmc_host_clk_release(host);
887 * Change data bus width of a host.
889 void mmc_set_bus_width(struct mmc_host *host, unsigned int width)
891 mmc_host_clk_hold(host);
892 host->ios.bus_width = width;
894 mmc_host_clk_release(host);
898 * mmc_vdd_to_ocrbitnum - Convert a voltage to the OCR bit number
900 * @low_bits: prefer low bits in boundary cases
902 * This function returns the OCR bit number according to the provided @vdd
903 * value. If conversion is not possible a negative errno value returned.
905 * Depending on the @low_bits flag the function prefers low or high OCR bits
906 * on boundary voltages. For example,
907 * with @low_bits = true, 3300 mV translates to ilog2(MMC_VDD_32_33);
908 * with @low_bits = false, 3300 mV translates to ilog2(MMC_VDD_33_34);
910 * Any value in the [1951:1999] range translates to the ilog2(MMC_VDD_20_21).
912 static int mmc_vdd_to_ocrbitnum(int vdd, bool low_bits)
914 const int max_bit = ilog2(MMC_VDD_35_36);
917 if (vdd < 1650 || vdd > 3600)
920 if (vdd >= 1650 && vdd <= 1950)
921 return ilog2(MMC_VDD_165_195);
926 /* Base 2000 mV, step 100 mV, bit's base 8. */
927 bit = (vdd - 2000) / 100 + 8;
934 * mmc_vddrange_to_ocrmask - Convert a voltage range to the OCR mask
935 * @vdd_min: minimum voltage value (mV)
936 * @vdd_max: maximum voltage value (mV)
938 * This function returns the OCR mask bits according to the provided @vdd_min
939 * and @vdd_max values. If conversion is not possible the function returns 0.
941 * Notes wrt boundary cases:
942 * This function sets the OCR bits for all boundary voltages, for example
943 * [3300:3400] range is translated to MMC_VDD_32_33 | MMC_VDD_33_34 |
944 * MMC_VDD_34_35 mask.
946 u32 mmc_vddrange_to_ocrmask(int vdd_min, int vdd_max)
950 if (vdd_max < vdd_min)
953 /* Prefer high bits for the boundary vdd_max values. */
954 vdd_max = mmc_vdd_to_ocrbitnum(vdd_max, false);
958 /* Prefer low bits for the boundary vdd_min values. */
959 vdd_min = mmc_vdd_to_ocrbitnum(vdd_min, true);
963 /* Fill the mask, from max bit to min bit. */
964 while (vdd_max >= vdd_min)
965 mask |= 1 << vdd_max--;
969 EXPORT_SYMBOL(mmc_vddrange_to_ocrmask);
971 #ifdef CONFIG_REGULATOR
974 * mmc_regulator_get_ocrmask - return mask of supported voltages
975 * @supply: regulator to use
977 * This returns either a negative errno, or a mask of voltages that
978 * can be provided to MMC/SD/SDIO devices using the specified voltage
979 * regulator. This would normally be called before registering the
982 int mmc_regulator_get_ocrmask(struct regulator *supply)
988 count = regulator_count_voltages(supply);
992 for (i = 0; i < count; i++) {
996 vdd_uV = regulator_list_voltage(supply, i);
1000 vdd_mV = vdd_uV / 1000;
1001 result |= mmc_vddrange_to_ocrmask(vdd_mV, vdd_mV);
1006 EXPORT_SYMBOL(mmc_regulator_get_ocrmask);
1009 * mmc_regulator_set_ocr - set regulator to match host->ios voltage
1010 * @mmc: the host to regulate
1011 * @supply: regulator to use
1012 * @vdd_bit: zero for power off, else a bit number (host->ios.vdd)
1014 * Returns zero on success, else negative errno.
1016 * MMC host drivers may use this to enable or disable a regulator using
1017 * a particular supply voltage. This would normally be called from the
1020 int mmc_regulator_set_ocr(struct mmc_host *mmc,
1021 struct regulator *supply,
1022 unsigned short vdd_bit)
1031 /* REVISIT mmc_vddrange_to_ocrmask() may have set some
1032 * bits this regulator doesn't quite support ... don't
1033 * be too picky, most cards and regulators are OK with
1034 * a 0.1V range goof (it's a small error percentage).
1036 tmp = vdd_bit - ilog2(MMC_VDD_165_195);
1038 min_uV = 1650 * 1000;
1039 max_uV = 1950 * 1000;
1041 min_uV = 1900 * 1000 + tmp * 100 * 1000;
1042 max_uV = min_uV + 100 * 1000;
1045 /* avoid needless changes to this voltage; the regulator
1046 * might not allow this operation
1048 voltage = regulator_get_voltage(supply);
1051 else if (voltage < min_uV || voltage > max_uV)
1052 result = regulator_set_voltage(supply, min_uV, max_uV);
1056 if (result == 0 && !mmc->regulator_enabled) {
1057 result = regulator_enable(supply);
1059 mmc->regulator_enabled = true;
1061 } else if (mmc->regulator_enabled) {
1062 result = regulator_disable(supply);
1064 mmc->regulator_enabled = false;
1068 dev_err(mmc_dev(mmc),
1069 "could not set regulator OCR (%d)\n", result);
1072 EXPORT_SYMBOL(mmc_regulator_set_ocr);
1074 #endif /* CONFIG_REGULATOR */
1077 * Mask off any voltages we don't support and select
1078 * the lowest voltage
1080 u32 mmc_select_voltage(struct mmc_host *host, u32 ocr)
1084 ocr &= host->ocr_avail;
1092 mmc_host_clk_hold(host);
1093 host->ios.vdd = bit;
1095 mmc_host_clk_release(host);
1097 pr_warning("%s: host doesn't support card's voltages\n",
1098 mmc_hostname(host));
1105 int mmc_set_signal_voltage(struct mmc_host *host, int signal_voltage, bool cmd11)
1107 struct mmc_command cmd = {0};
1113 * Send CMD11 only if the request is to switch the card to
1116 if ((signal_voltage != MMC_SIGNAL_VOLTAGE_330) && cmd11) {
1117 cmd.opcode = SD_SWITCH_VOLTAGE;
1119 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
1121 err = mmc_wait_for_cmd(host, &cmd, 0);
1125 if (!mmc_host_is_spi(host) && (cmd.resp[0] & R1_ERROR))
1129 host->ios.signal_voltage = signal_voltage;
1131 if (host->ops->start_signal_voltage_switch)
1132 err = host->ops->start_signal_voltage_switch(host, &host->ios);
1138 * Select timing parameters for host.
1140 void mmc_set_timing(struct mmc_host *host, unsigned int timing)
1142 mmc_host_clk_hold(host);
1143 host->ios.timing = timing;
1145 mmc_host_clk_release(host);
1149 * Select appropriate driver type for host.
1151 void mmc_set_driver_type(struct mmc_host *host, unsigned int drv_type)
1153 mmc_host_clk_hold(host);
1154 host->ios.drv_type = drv_type;
1156 mmc_host_clk_release(host);
1160 * Apply power to the MMC stack. This is a two-stage process.
1161 * First, we enable power to the card without the clock running.
1162 * We then wait a bit for the power to stabilise. Finally,
1163 * enable the bus drivers and clock to the card.
1165 * We must _NOT_ enable the clock prior to power stablising.
1167 * If a host does all the power sequencing itself, ignore the
1168 * initial MMC_POWER_UP stage.
1170 static void mmc_power_up(struct mmc_host *host)
1174 mmc_host_clk_hold(host);
1176 /* If ocr is set, we use it */
1178 bit = ffs(host->ocr) - 1;
1180 bit = fls(host->ocr_avail) - 1;
1182 host->ios.vdd = bit;
1183 if (mmc_host_is_spi(host))
1184 host->ios.chip_select = MMC_CS_HIGH;
1186 host->ios.chip_select = MMC_CS_DONTCARE;
1187 host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
1188 host->ios.power_mode = MMC_POWER_UP;
1189 host->ios.bus_width = MMC_BUS_WIDTH_1;
1190 host->ios.timing = MMC_TIMING_LEGACY;
1194 * This delay should be sufficient to allow the power supply
1195 * to reach the minimum voltage.
1199 host->ios.clock = host->f_init;
1201 host->ios.power_mode = MMC_POWER_ON;
1205 * This delay must be at least 74 clock sizes, or 1 ms, or the
1206 * time required to reach a stable voltage.
1210 mmc_host_clk_release(host);
1213 void mmc_power_off(struct mmc_host *host)
1215 mmc_host_clk_hold(host);
1217 host->ios.clock = 0;
1221 * Reset ocr mask to be the highest possible voltage supported for
1222 * this mmc host. This value will be used at next power up.
1224 host->ocr = 1 << (fls(host->ocr_avail) - 1);
1226 if (!mmc_host_is_spi(host)) {
1227 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
1228 host->ios.chip_select = MMC_CS_DONTCARE;
1230 host->ios.power_mode = MMC_POWER_OFF;
1231 host->ios.bus_width = MMC_BUS_WIDTH_1;
1232 host->ios.timing = MMC_TIMING_LEGACY;
1236 * Some configurations, such as the 802.11 SDIO card in the OLPC
1237 * XO-1.5, require a short delay after poweroff before the card
1238 * can be successfully turned on again.
1242 mmc_host_clk_release(host);
1246 * Cleanup when the last reference to the bus operator is dropped.
1248 static void __mmc_release_bus(struct mmc_host *host)
1251 BUG_ON(host->bus_refs);
1252 BUG_ON(!host->bus_dead);
1254 host->bus_ops = NULL;
1258 * Increase reference count of bus operator
1260 static inline void mmc_bus_get(struct mmc_host *host)
1262 unsigned long flags;
1264 spin_lock_irqsave(&host->lock, flags);
1266 spin_unlock_irqrestore(&host->lock, flags);
1270 * Decrease reference count of bus operator and free it if
1271 * it is the last reference.
1273 static inline void mmc_bus_put(struct mmc_host *host)
1275 unsigned long flags;
1277 spin_lock_irqsave(&host->lock, flags);
1279 if ((host->bus_refs == 0) && host->bus_ops)
1280 __mmc_release_bus(host);
1281 spin_unlock_irqrestore(&host->lock, flags);
1285 * Assign a mmc bus handler to a host. Only one bus handler may control a
1286 * host at any given time.
1288 void mmc_attach_bus(struct mmc_host *host, const struct mmc_bus_ops *ops)
1290 unsigned long flags;
1295 WARN_ON(!host->claimed);
1297 spin_lock_irqsave(&host->lock, flags);
1299 BUG_ON(host->bus_ops);
1300 BUG_ON(host->bus_refs);
1302 host->bus_ops = ops;
1306 spin_unlock_irqrestore(&host->lock, flags);
1310 * Remove the current bus handler from a host.
1312 void mmc_detach_bus(struct mmc_host *host)
1314 unsigned long flags;
1318 WARN_ON(!host->claimed);
1319 WARN_ON(!host->bus_ops);
1321 spin_lock_irqsave(&host->lock, flags);
1325 spin_unlock_irqrestore(&host->lock, flags);
1331 * mmc_detect_change - process change of state on a MMC socket
1332 * @host: host which changed state.
1333 * @delay: optional delay to wait before detection (jiffies)
1335 * MMC drivers should call this when they detect a card has been
1336 * inserted or removed. The MMC layer will confirm that any
1337 * present card is still functional, and initialize any newly
1340 void mmc_detect_change(struct mmc_host *host, unsigned long delay)
1342 #ifdef CONFIG_MMC_DEBUG
1343 unsigned long flags;
1344 spin_lock_irqsave(&host->lock, flags);
1345 WARN_ON(host->removed);
1346 spin_unlock_irqrestore(&host->lock, flags);
1349 mmc_schedule_delayed_work(&host->detect, delay);
1352 EXPORT_SYMBOL(mmc_detect_change);
1354 void mmc_init_erase(struct mmc_card *card)
1358 if (is_power_of_2(card->erase_size))
1359 card->erase_shift = ffs(card->erase_size) - 1;
1361 card->erase_shift = 0;
1364 * It is possible to erase an arbitrarily large area of an SD or MMC
1365 * card. That is not desirable because it can take a long time
1366 * (minutes) potentially delaying more important I/O, and also the
1367 * timeout calculations become increasingly hugely over-estimated.
1368 * Consequently, 'pref_erase' is defined as a guide to limit erases
1369 * to that size and alignment.
1371 * For SD cards that define Allocation Unit size, limit erases to one
1372 * Allocation Unit at a time. For MMC cards that define High Capacity
1373 * Erase Size, whether it is switched on or not, limit to that size.
1374 * Otherwise just have a stab at a good value. For modern cards it
1375 * will end up being 4MiB. Note that if the value is too small, it
1376 * can end up taking longer to erase.
1378 if (mmc_card_sd(card) && card->ssr.au) {
1379 card->pref_erase = card->ssr.au;
1380 card->erase_shift = ffs(card->ssr.au) - 1;
1381 } else if (card->ext_csd.hc_erase_size) {
1382 card->pref_erase = card->ext_csd.hc_erase_size;
1384 sz = (card->csd.capacity << (card->csd.read_blkbits - 9)) >> 11;
1386 card->pref_erase = 512 * 1024 / 512;
1388 card->pref_erase = 1024 * 1024 / 512;
1390 card->pref_erase = 2 * 1024 * 1024 / 512;
1392 card->pref_erase = 4 * 1024 * 1024 / 512;
1393 if (card->pref_erase < card->erase_size)
1394 card->pref_erase = card->erase_size;
1396 sz = card->pref_erase % card->erase_size;
1398 card->pref_erase += card->erase_size - sz;
1403 static unsigned int mmc_mmc_erase_timeout(struct mmc_card *card,
1404 unsigned int arg, unsigned int qty)
1406 unsigned int erase_timeout;
1408 if (card->ext_csd.erase_group_def & 1) {
1409 /* High Capacity Erase Group Size uses HC timeouts */
1410 if (arg == MMC_TRIM_ARG)
1411 erase_timeout = card->ext_csd.trim_timeout;
1413 erase_timeout = card->ext_csd.hc_erase_timeout;
1415 /* CSD Erase Group Size uses write timeout */
1416 unsigned int mult = (10 << card->csd.r2w_factor);
1417 unsigned int timeout_clks = card->csd.tacc_clks * mult;
1418 unsigned int timeout_us;
1420 /* Avoid overflow: e.g. tacc_ns=80000000 mult=1280 */
1421 if (card->csd.tacc_ns < 1000000)
1422 timeout_us = (card->csd.tacc_ns * mult) / 1000;
1424 timeout_us = (card->csd.tacc_ns / 1000) * mult;
1427 * ios.clock is only a target. The real clock rate might be
1428 * less but not that much less, so fudge it by multiplying by 2.
1431 timeout_us += (timeout_clks * 1000) /
1432 (mmc_host_clk_rate(card->host) / 1000);
1434 erase_timeout = timeout_us / 1000;
1437 * Theoretically, the calculation could underflow so round up
1438 * to 1ms in that case.
1444 /* Multiplier for secure operations */
1445 if (arg & MMC_SECURE_ARGS) {
1446 if (arg == MMC_SECURE_ERASE_ARG)
1447 erase_timeout *= card->ext_csd.sec_erase_mult;
1449 erase_timeout *= card->ext_csd.sec_trim_mult;
1452 erase_timeout *= qty;
1455 * Ensure at least a 1 second timeout for SPI as per
1456 * 'mmc_set_data_timeout()'
1458 if (mmc_host_is_spi(card->host) && erase_timeout < 1000)
1459 erase_timeout = 1000;
1461 return erase_timeout;
1464 static unsigned int mmc_sd_erase_timeout(struct mmc_card *card,
1468 unsigned int erase_timeout;
1470 if (card->ssr.erase_timeout) {
1471 /* Erase timeout specified in SD Status Register (SSR) */
1472 erase_timeout = card->ssr.erase_timeout * qty +
1473 card->ssr.erase_offset;
1476 * Erase timeout not specified in SD Status Register (SSR) so
1477 * use 250ms per write block.
1479 erase_timeout = 250 * qty;
1482 /* Must not be less than 1 second */
1483 if (erase_timeout < 1000)
1484 erase_timeout = 1000;
1486 return erase_timeout;
1489 static unsigned int mmc_erase_timeout(struct mmc_card *card,
1493 if (mmc_card_sd(card))
1494 return mmc_sd_erase_timeout(card, arg, qty);
1496 return mmc_mmc_erase_timeout(card, arg, qty);
1499 static int mmc_do_erase(struct mmc_card *card, unsigned int from,
1500 unsigned int to, unsigned int arg)
1502 struct mmc_command cmd = {0};
1503 unsigned int qty = 0;
1507 * qty is used to calculate the erase timeout which depends on how many
1508 * erase groups (or allocation units in SD terminology) are affected.
1509 * We count erasing part of an erase group as one erase group.
1510 * For SD, the allocation units are always a power of 2. For MMC, the
1511 * erase group size is almost certainly also power of 2, but it does not
1512 * seem to insist on that in the JEDEC standard, so we fall back to
1513 * division in that case. SD may not specify an allocation unit size,
1514 * in which case the timeout is based on the number of write blocks.
1516 * Note that the timeout for secure trim 2 will only be correct if the
1517 * number of erase groups specified is the same as the total of all
1518 * preceding secure trim 1 commands. Since the power may have been
1519 * lost since the secure trim 1 commands occurred, it is generally
1520 * impossible to calculate the secure trim 2 timeout correctly.
1522 if (card->erase_shift)
1523 qty += ((to >> card->erase_shift) -
1524 (from >> card->erase_shift)) + 1;
1525 else if (mmc_card_sd(card))
1526 qty += to - from + 1;
1528 qty += ((to / card->erase_size) -
1529 (from / card->erase_size)) + 1;
1531 if (!mmc_card_blockaddr(card)) {
1536 if (mmc_card_sd(card))
1537 cmd.opcode = SD_ERASE_WR_BLK_START;
1539 cmd.opcode = MMC_ERASE_GROUP_START;
1541 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
1542 err = mmc_wait_for_cmd(card->host, &cmd, 0);
1544 pr_err("mmc_erase: group start error %d, "
1545 "status %#x\n", err, cmd.resp[0]);
1550 memset(&cmd, 0, sizeof(struct mmc_command));
1551 if (mmc_card_sd(card))
1552 cmd.opcode = SD_ERASE_WR_BLK_END;
1554 cmd.opcode = MMC_ERASE_GROUP_END;
1556 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
1557 err = mmc_wait_for_cmd(card->host, &cmd, 0);
1559 pr_err("mmc_erase: group end error %d, status %#x\n",
1565 memset(&cmd, 0, sizeof(struct mmc_command));
1566 cmd.opcode = MMC_ERASE;
1568 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1569 cmd.cmd_timeout_ms = mmc_erase_timeout(card, arg, qty);
1570 err = mmc_wait_for_cmd(card->host, &cmd, 0);
1572 pr_err("mmc_erase: erase error %d, status %#x\n",
1578 if (mmc_host_is_spi(card->host))
1582 memset(&cmd, 0, sizeof(struct mmc_command));
1583 cmd.opcode = MMC_SEND_STATUS;
1584 cmd.arg = card->rca << 16;
1585 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
1586 /* Do not retry else we can't see errors */
1587 err = mmc_wait_for_cmd(card->host, &cmd, 0);
1588 if (err || (cmd.resp[0] & 0xFDF92000)) {
1589 pr_err("error %d requesting status %#x\n",
1594 } while (!(cmd.resp[0] & R1_READY_FOR_DATA) ||
1595 R1_CURRENT_STATE(cmd.resp[0]) == R1_STATE_PRG);
1601 * mmc_erase - erase sectors.
1602 * @card: card to erase
1603 * @from: first sector to erase
1604 * @nr: number of sectors to erase
1605 * @arg: erase command argument (SD supports only %MMC_ERASE_ARG)
1607 * Caller must claim host before calling this function.
1609 int mmc_erase(struct mmc_card *card, unsigned int from, unsigned int nr,
1612 unsigned int rem, to = from + nr;
1614 if (!(card->host->caps & MMC_CAP_ERASE) ||
1615 !(card->csd.cmdclass & CCC_ERASE))
1618 if (!card->erase_size)
1621 if (mmc_card_sd(card) && arg != MMC_ERASE_ARG)
1624 if ((arg & MMC_SECURE_ARGS) &&
1625 !(card->ext_csd.sec_feature_support & EXT_CSD_SEC_ER_EN))
1628 if ((arg & MMC_TRIM_ARGS) &&
1629 !(card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN))
1632 if (arg == MMC_SECURE_ERASE_ARG) {
1633 if (from % card->erase_size || nr % card->erase_size)
1637 if (arg == MMC_ERASE_ARG) {
1638 rem = from % card->erase_size;
1640 rem = card->erase_size - rem;
1647 rem = nr % card->erase_size;
1660 /* 'from' and 'to' are inclusive */
1663 return mmc_do_erase(card, from, to, arg);
1665 EXPORT_SYMBOL(mmc_erase);
1667 int mmc_can_erase(struct mmc_card *card)
1669 if ((card->host->caps & MMC_CAP_ERASE) &&
1670 (card->csd.cmdclass & CCC_ERASE) && card->erase_size)
1674 EXPORT_SYMBOL(mmc_can_erase);
1676 int mmc_can_trim(struct mmc_card *card)
1678 if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN)
1682 EXPORT_SYMBOL(mmc_can_trim);
1684 int mmc_can_secure_erase_trim(struct mmc_card *card)
1686 if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_ER_EN)
1690 EXPORT_SYMBOL(mmc_can_secure_erase_trim);
1692 int mmc_erase_group_aligned(struct mmc_card *card, unsigned int from,
1695 if (!card->erase_size)
1697 if (from % card->erase_size || nr % card->erase_size)
1701 EXPORT_SYMBOL(mmc_erase_group_aligned);
1703 static unsigned int mmc_do_calc_max_discard(struct mmc_card *card,
1706 struct mmc_host *host = card->host;
1707 unsigned int max_discard, x, y, qty = 0, max_qty, timeout;
1708 unsigned int last_timeout = 0;
1710 if (card->erase_shift)
1711 max_qty = UINT_MAX >> card->erase_shift;
1712 else if (mmc_card_sd(card))
1715 max_qty = UINT_MAX / card->erase_size;
1717 /* Find the largest qty with an OK timeout */
1720 for (x = 1; x && x <= max_qty && max_qty - x >= qty; x <<= 1) {
1721 timeout = mmc_erase_timeout(card, arg, qty + x);
1722 if (timeout > host->max_discard_to)
1724 if (timeout < last_timeout)
1726 last_timeout = timeout;
1738 /* Convert qty to sectors */
1739 if (card->erase_shift)
1740 max_discard = --qty << card->erase_shift;
1741 else if (mmc_card_sd(card))
1744 max_discard = --qty * card->erase_size;
1749 unsigned int mmc_calc_max_discard(struct mmc_card *card)
1751 struct mmc_host *host = card->host;
1752 unsigned int max_discard, max_trim;
1754 if (!host->max_discard_to)
1758 * Without erase_group_def set, MMC erase timeout depends on clock
1759 * frequence which can change. In that case, the best choice is
1760 * just the preferred erase size.
1762 if (mmc_card_mmc(card) && !(card->ext_csd.erase_group_def & 1))
1763 return card->pref_erase;
1765 max_discard = mmc_do_calc_max_discard(card, MMC_ERASE_ARG);
1766 if (mmc_can_trim(card)) {
1767 max_trim = mmc_do_calc_max_discard(card, MMC_TRIM_ARG);
1768 if (max_trim < max_discard)
1769 max_discard = max_trim;
1770 } else if (max_discard < card->erase_size) {
1773 pr_debug("%s: calculated max. discard sectors %u for timeout %u ms\n",
1774 mmc_hostname(host), max_discard, host->max_discard_to);
1777 EXPORT_SYMBOL(mmc_calc_max_discard);
1779 int mmc_set_blocklen(struct mmc_card *card, unsigned int blocklen)
1781 struct mmc_command cmd = {0};
1783 if (mmc_card_blockaddr(card) || mmc_card_ddr_mode(card))
1786 cmd.opcode = MMC_SET_BLOCKLEN;
1788 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
1789 return mmc_wait_for_cmd(card->host, &cmd, 5);
1791 EXPORT_SYMBOL(mmc_set_blocklen);
1793 static void mmc_hw_reset_for_init(struct mmc_host *host)
1795 if (!(host->caps & MMC_CAP_HW_RESET) || !host->ops->hw_reset)
1797 mmc_host_clk_hold(host);
1798 host->ops->hw_reset(host);
1799 mmc_host_clk_release(host);
1802 int mmc_can_reset(struct mmc_card *card)
1806 if (!mmc_card_mmc(card))
1808 rst_n_function = card->ext_csd.rst_n_function;
1809 if ((rst_n_function & EXT_CSD_RST_N_EN_MASK) != EXT_CSD_RST_N_ENABLED)
1813 EXPORT_SYMBOL(mmc_can_reset);
1815 static int mmc_do_hw_reset(struct mmc_host *host, int check)
1817 struct mmc_card *card = host->card;
1819 if (!host->bus_ops->power_restore)
1822 if (!(host->caps & MMC_CAP_HW_RESET) || !host->ops->hw_reset)
1828 if (!mmc_can_reset(card))
1831 mmc_host_clk_hold(host);
1832 mmc_set_clock(host, host->f_init);
1834 host->ops->hw_reset(host);
1836 /* If the reset has happened, then a status command will fail */
1838 struct mmc_command cmd = {0};
1841 cmd.opcode = MMC_SEND_STATUS;
1842 if (!mmc_host_is_spi(card->host))
1843 cmd.arg = card->rca << 16;
1844 cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
1845 err = mmc_wait_for_cmd(card->host, &cmd, 0);
1847 mmc_host_clk_release(host);
1852 host->card->state &= ~(MMC_STATE_HIGHSPEED | MMC_STATE_HIGHSPEED_DDR);
1853 if (mmc_host_is_spi(host)) {
1854 host->ios.chip_select = MMC_CS_HIGH;
1855 host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
1857 host->ios.chip_select = MMC_CS_DONTCARE;
1858 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
1860 host->ios.bus_width = MMC_BUS_WIDTH_1;
1861 host->ios.timing = MMC_TIMING_LEGACY;
1864 mmc_host_clk_release(host);
1866 return host->bus_ops->power_restore(host);
1869 int mmc_hw_reset(struct mmc_host *host)
1871 return mmc_do_hw_reset(host, 0);
1873 EXPORT_SYMBOL(mmc_hw_reset);
1875 int mmc_hw_reset_check(struct mmc_host *host)
1877 return mmc_do_hw_reset(host, 1);
1879 EXPORT_SYMBOL(mmc_hw_reset_check);
1881 static int mmc_rescan_try_freq(struct mmc_host *host, unsigned freq)
1883 host->f_init = freq;
1885 #ifdef CONFIG_MMC_DEBUG
1886 pr_info("%s: %s: trying to init card at %u Hz\n",
1887 mmc_hostname(host), __func__, host->f_init);
1892 * Some eMMCs (with VCCQ always on) may not be reset after power up, so
1893 * do a hardware reset if possible.
1895 mmc_hw_reset_for_init(host);
1898 * sdio_reset sends CMD52 to reset card. Since we do not know
1899 * if the card is being re-initialized, just send it. CMD52
1900 * should be ignored by SD/eMMC cards.
1905 mmc_send_if_cond(host, host->ocr_avail);
1907 /* Order's important: probe SDIO, then SD, then MMC */
1908 if (!mmc_attach_sdio(host))
1910 if (!mmc_attach_sd(host))
1912 if (!mmc_attach_mmc(host))
1915 mmc_power_off(host);
1919 void mmc_rescan(struct work_struct *work)
1921 static const unsigned freqs[] = { 400000, 300000, 200000, 100000 };
1922 struct mmc_host *host =
1923 container_of(work, struct mmc_host, detect.work);
1926 if (host->rescan_disable)
1932 * if there is a _removable_ card registered, check whether it is
1935 if (host->bus_ops && host->bus_ops->detect && !host->bus_dead
1936 && !(host->caps & MMC_CAP_NONREMOVABLE))
1937 host->bus_ops->detect(host);
1940 * Let mmc_bus_put() free the bus/bus_ops if we've found that
1941 * the card is no longer present.
1946 /* if there still is a card present, stop here */
1947 if (host->bus_ops != NULL) {
1953 * Only we can add a new handler, so it's safe to
1954 * release the lock here.
1958 if (host->ops->get_cd && host->ops->get_cd(host) == 0)
1961 mmc_claim_host(host);
1962 for (i = 0; i < ARRAY_SIZE(freqs); i++) {
1963 if (!mmc_rescan_try_freq(host, max(freqs[i], host->f_min)))
1965 if (freqs[i] <= host->f_min)
1968 mmc_release_host(host);
1971 if (host->caps & MMC_CAP_NEEDS_POLL)
1972 mmc_schedule_delayed_work(&host->detect, HZ);
1975 void mmc_start_host(struct mmc_host *host)
1977 mmc_power_off(host);
1978 mmc_detect_change(host, 0);
1981 void mmc_stop_host(struct mmc_host *host)
1983 #ifdef CONFIG_MMC_DEBUG
1984 unsigned long flags;
1985 spin_lock_irqsave(&host->lock, flags);
1987 spin_unlock_irqrestore(&host->lock, flags);
1990 if (host->caps & MMC_CAP_DISABLE)
1991 cancel_delayed_work(&host->disable);
1992 cancel_delayed_work_sync(&host->detect);
1993 mmc_flush_scheduled_work();
1995 /* clear pm flags now and let card drivers set them as needed */
1999 if (host->bus_ops && !host->bus_dead) {
2000 if (host->bus_ops->remove)
2001 host->bus_ops->remove(host);
2003 mmc_claim_host(host);
2004 mmc_detach_bus(host);
2005 mmc_power_off(host);
2006 mmc_release_host(host);
2014 mmc_power_off(host);
2017 int mmc_power_save_host(struct mmc_host *host)
2021 #ifdef CONFIG_MMC_DEBUG
2022 pr_info("%s: %s: powering down\n", mmc_hostname(host), __func__);
2027 if (!host->bus_ops || host->bus_dead || !host->bus_ops->power_restore) {
2032 if (host->bus_ops->power_save)
2033 ret = host->bus_ops->power_save(host);
2037 mmc_power_off(host);
2041 EXPORT_SYMBOL(mmc_power_save_host);
2043 int mmc_power_restore_host(struct mmc_host *host)
2047 #ifdef CONFIG_MMC_DEBUG
2048 pr_info("%s: %s: powering up\n", mmc_hostname(host), __func__);
2053 if (!host->bus_ops || host->bus_dead || !host->bus_ops->power_restore) {
2059 ret = host->bus_ops->power_restore(host);
2065 EXPORT_SYMBOL(mmc_power_restore_host);
2067 int mmc_card_awake(struct mmc_host *host)
2073 if (host->bus_ops && !host->bus_dead && host->bus_ops->awake)
2074 err = host->bus_ops->awake(host);
2080 EXPORT_SYMBOL(mmc_card_awake);
2082 int mmc_card_sleep(struct mmc_host *host)
2088 if (host->bus_ops && !host->bus_dead && host->bus_ops->awake)
2089 err = host->bus_ops->sleep(host);
2095 EXPORT_SYMBOL(mmc_card_sleep);
2097 int mmc_card_can_sleep(struct mmc_host *host)
2099 struct mmc_card *card = host->card;
2101 if (card && mmc_card_mmc(card) && card->ext_csd.rev >= 3)
2105 EXPORT_SYMBOL(mmc_card_can_sleep);
2110 * mmc_suspend_host - suspend a host
2113 int mmc_suspend_host(struct mmc_host *host)
2117 if (host->caps & MMC_CAP_DISABLE)
2118 cancel_delayed_work(&host->disable);
2119 cancel_delayed_work(&host->detect);
2120 mmc_flush_scheduled_work();
2123 if (host->bus_ops && !host->bus_dead) {
2124 if (host->bus_ops->suspend)
2125 err = host->bus_ops->suspend(host);
2126 if (err == -ENOSYS || !host->bus_ops->resume) {
2128 * We simply "remove" the card in this case.
2129 * It will be redetected on resume.
2131 if (host->bus_ops->remove)
2132 host->bus_ops->remove(host);
2133 mmc_claim_host(host);
2134 mmc_detach_bus(host);
2135 mmc_power_off(host);
2136 mmc_release_host(host);
2143 if (!err && !mmc_card_keep_power(host))
2144 mmc_power_off(host);
2149 EXPORT_SYMBOL(mmc_suspend_host);
2152 * mmc_resume_host - resume a previously suspended host
2155 int mmc_resume_host(struct mmc_host *host)
2160 if (host->bus_ops && !host->bus_dead) {
2161 if (!mmc_card_keep_power(host)) {
2163 mmc_select_voltage(host, host->ocr);
2165 * Tell runtime PM core we just powered up the card,
2166 * since it still believes the card is powered off.
2167 * Note that currently runtime PM is only enabled
2168 * for SDIO cards that are MMC_CAP_POWER_OFF_CARD
2170 if (mmc_card_sdio(host->card) &&
2171 (host->caps & MMC_CAP_POWER_OFF_CARD)) {
2172 pm_runtime_disable(&host->card->dev);
2173 pm_runtime_set_active(&host->card->dev);
2174 pm_runtime_enable(&host->card->dev);
2177 BUG_ON(!host->bus_ops->resume);
2178 err = host->bus_ops->resume(host);
2180 pr_warning("%s: error %d during resume "
2181 "(card was removed?)\n",
2182 mmc_hostname(host), err);
2186 host->pm_flags &= ~MMC_PM_KEEP_POWER;
2191 EXPORT_SYMBOL(mmc_resume_host);
2193 /* Do the card removal on suspend if card is assumed removeable
2194 * Do that in pm notifier while userspace isn't yet frozen, so we will be able
2197 int mmc_pm_notify(struct notifier_block *notify_block,
2198 unsigned long mode, void *unused)
2200 struct mmc_host *host = container_of(
2201 notify_block, struct mmc_host, pm_notify);
2202 unsigned long flags;
2206 case PM_HIBERNATION_PREPARE:
2207 case PM_SUSPEND_PREPARE:
2209 spin_lock_irqsave(&host->lock, flags);
2210 host->rescan_disable = 1;
2211 spin_unlock_irqrestore(&host->lock, flags);
2212 cancel_delayed_work_sync(&host->detect);
2214 if (!host->bus_ops || host->bus_ops->suspend)
2217 mmc_claim_host(host);
2219 if (host->bus_ops->remove)
2220 host->bus_ops->remove(host);
2222 mmc_detach_bus(host);
2223 mmc_power_off(host);
2224 mmc_release_host(host);
2228 case PM_POST_SUSPEND:
2229 case PM_POST_HIBERNATION:
2230 case PM_POST_RESTORE:
2232 spin_lock_irqsave(&host->lock, flags);
2233 host->rescan_disable = 0;
2234 spin_unlock_irqrestore(&host->lock, flags);
2235 mmc_detect_change(host, 0);
2243 static int __init mmc_init(void)
2247 workqueue = alloc_ordered_workqueue("kmmcd", 0);
2251 ret = mmc_register_bus();
2253 goto destroy_workqueue;
2255 ret = mmc_register_host_class();
2257 goto unregister_bus;
2259 ret = sdio_register_bus();
2261 goto unregister_host_class;
2265 unregister_host_class:
2266 mmc_unregister_host_class();
2268 mmc_unregister_bus();
2270 destroy_workqueue(workqueue);
2275 static void __exit mmc_exit(void)
2277 sdio_unregister_bus();
2278 mmc_unregister_host_class();
2279 mmc_unregister_bus();
2280 destroy_workqueue(workqueue);
2283 subsys_initcall(mmc_init);
2284 module_exit(mmc_exit);
2286 MODULE_LICENSE("GPL");