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) {
144 pr_debug("%s: req failed (CMD%u): %d, retrying...\n",
145 mmc_hostname(host), cmd->opcode, err);
149 host->ops->request(host, mrq);
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 wait_for_completion(&mrq->completion);
260 * mmc_pre_req - Prepare for a new request
261 * @host: MMC host to prepare command
262 * @mrq: MMC request to prepare for
263 * @is_first_req: true if there is no previous started request
264 * that may run in parellel to this call, otherwise false
266 * mmc_pre_req() is called in prior to mmc_start_req() to let
267 * host prepare for the new request. Preparation of a request may be
268 * performed while another request is running on the host.
270 static void mmc_pre_req(struct mmc_host *host, struct mmc_request *mrq,
273 if (host->ops->pre_req)
274 host->ops->pre_req(host, mrq, is_first_req);
278 * mmc_post_req - Post process a completed request
279 * @host: MMC host to post process command
280 * @mrq: MMC request to post process for
281 * @err: Error, if non zero, clean up any resources made in pre_req
283 * Let the host post process a completed request. Post processing of
284 * a request may be performed while another reuqest is running.
286 static void mmc_post_req(struct mmc_host *host, struct mmc_request *mrq,
289 if (host->ops->post_req)
290 host->ops->post_req(host, mrq, err);
294 * mmc_start_req - start a non-blocking request
295 * @host: MMC host to start command
296 * @areq: async request to start
297 * @error: out parameter returns 0 for success, otherwise non zero
299 * Start a new MMC custom command request for a host.
300 * If there is on ongoing async request wait for completion
301 * of that request and start the new one and return.
302 * Does not wait for the new request to complete.
304 * Returns the completed request, NULL in case of none completed.
305 * Wait for the an ongoing request (previoulsy started) to complete and
306 * return the completed request. If there is no ongoing request, NULL
307 * is returned without waiting. NULL is not an error condition.
309 struct mmc_async_req *mmc_start_req(struct mmc_host *host,
310 struct mmc_async_req *areq, int *error)
313 struct mmc_async_req *data = host->areq;
315 /* Prepare a new request */
317 mmc_pre_req(host, areq->mrq, !host->areq);
320 mmc_wait_for_req_done(host, host->areq->mrq);
321 err = host->areq->err_check(host->card, host->areq);
323 /* post process the completed failed request */
324 mmc_post_req(host, host->areq->mrq, 0);
327 * Cancel the new prepared request, because
328 * it can't run until the failed
329 * request has been properly handled.
331 mmc_post_req(host, areq->mrq, -EINVAL);
339 __mmc_start_req(host, areq->mrq);
342 mmc_post_req(host, host->areq->mrq, 0);
350 EXPORT_SYMBOL(mmc_start_req);
353 * mmc_wait_for_req - start a request and wait for completion
354 * @host: MMC host to start command
355 * @mrq: MMC request to start
357 * Start a new MMC custom command request for a host, and wait
358 * for the command to complete. Does not attempt to parse the
361 void mmc_wait_for_req(struct mmc_host *host, struct mmc_request *mrq)
363 __mmc_start_req(host, mrq);
364 mmc_wait_for_req_done(host, mrq);
366 EXPORT_SYMBOL(mmc_wait_for_req);
369 * mmc_wait_for_cmd - start a command and wait for completion
370 * @host: MMC host to start command
371 * @cmd: MMC command to start
372 * @retries: maximum number of retries
374 * Start a new MMC command for a host, and wait for the command
375 * to complete. Return any error that occurred while the command
376 * was executing. Do not attempt to parse the response.
378 int mmc_wait_for_cmd(struct mmc_host *host, struct mmc_command *cmd, int retries)
380 struct mmc_request mrq = {NULL};
382 WARN_ON(!host->claimed);
384 memset(cmd->resp, 0, sizeof(cmd->resp));
385 cmd->retries = retries;
390 mmc_wait_for_req(host, &mrq);
395 EXPORT_SYMBOL(mmc_wait_for_cmd);
398 * mmc_set_data_timeout - set the timeout for a data command
399 * @data: data phase for command
400 * @card: the MMC card associated with the data transfer
402 * Computes the data timeout parameters according to the
403 * correct algorithm given the card type.
405 void mmc_set_data_timeout(struct mmc_data *data, const struct mmc_card *card)
410 * SDIO cards only define an upper 1 s limit on access.
412 if (mmc_card_sdio(card)) {
413 data->timeout_ns = 1000000000;
414 data->timeout_clks = 0;
419 * SD cards use a 100 multiplier rather than 10
421 mult = mmc_card_sd(card) ? 100 : 10;
424 * Scale up the multiplier (and therefore the timeout) by
425 * the r2w factor for writes.
427 if (data->flags & MMC_DATA_WRITE)
428 mult <<= card->csd.r2w_factor;
430 data->timeout_ns = card->csd.tacc_ns * mult;
431 data->timeout_clks = card->csd.tacc_clks * mult;
434 * SD cards also have an upper limit on the timeout.
436 if (mmc_card_sd(card)) {
437 unsigned int timeout_us, limit_us;
439 timeout_us = data->timeout_ns / 1000;
440 if (mmc_host_clk_rate(card->host))
441 timeout_us += data->timeout_clks * 1000 /
442 (mmc_host_clk_rate(card->host) / 1000);
444 if (data->flags & MMC_DATA_WRITE)
446 * The limit is really 250 ms, but that is
447 * insufficient for some crappy cards.
454 * SDHC cards always use these fixed values.
456 if (timeout_us > limit_us || mmc_card_blockaddr(card)) {
457 data->timeout_ns = limit_us * 1000;
458 data->timeout_clks = 0;
462 * Some cards need very high timeouts if driven in SPI mode.
463 * The worst observed timeout was 900ms after writing a
464 * continuous stream of data until the internal logic
467 if (mmc_host_is_spi(card->host)) {
468 if (data->flags & MMC_DATA_WRITE) {
469 if (data->timeout_ns < 1000000000)
470 data->timeout_ns = 1000000000; /* 1s */
472 if (data->timeout_ns < 100000000)
473 data->timeout_ns = 100000000; /* 100ms */
477 EXPORT_SYMBOL(mmc_set_data_timeout);
480 * mmc_align_data_size - pads a transfer size to a more optimal value
481 * @card: the MMC card associated with the data transfer
482 * @sz: original transfer size
484 * Pads the original data size with a number of extra bytes in
485 * order to avoid controller bugs and/or performance hits
486 * (e.g. some controllers revert to PIO for certain sizes).
488 * Returns the improved size, which might be unmodified.
490 * Note that this function is only relevant when issuing a
491 * single scatter gather entry.
493 unsigned int mmc_align_data_size(struct mmc_card *card, unsigned int sz)
496 * FIXME: We don't have a system for the controller to tell
497 * the core about its problems yet, so for now we just 32-bit
500 sz = ((sz + 3) / 4) * 4;
504 EXPORT_SYMBOL(mmc_align_data_size);
507 * mmc_host_enable - enable a host.
508 * @host: mmc host to enable
510 * Hosts that support power saving can use the 'enable' and 'disable'
511 * methods to exit and enter power saving states. For more information
512 * see comments for struct mmc_host_ops.
514 int mmc_host_enable(struct mmc_host *host)
516 if (!(host->caps & MMC_CAP_DISABLE))
519 if (host->en_dis_recurs)
522 if (host->nesting_cnt++)
525 cancel_delayed_work_sync(&host->disable);
530 if (host->ops->enable) {
533 host->en_dis_recurs = 1;
534 err = host->ops->enable(host);
535 host->en_dis_recurs = 0;
538 pr_debug("%s: enable error %d\n",
539 mmc_hostname(host), err);
546 EXPORT_SYMBOL(mmc_host_enable);
548 static int mmc_host_do_disable(struct mmc_host *host, int lazy)
550 if (host->ops->disable) {
553 host->en_dis_recurs = 1;
554 err = host->ops->disable(host, lazy);
555 host->en_dis_recurs = 0;
558 pr_debug("%s: disable error %d\n",
559 mmc_hostname(host), err);
563 unsigned long delay = msecs_to_jiffies(err);
565 mmc_schedule_delayed_work(&host->disable, delay);
573 * mmc_host_disable - disable a host.
574 * @host: mmc host to disable
576 * Hosts that support power saving can use the 'enable' and 'disable'
577 * methods to exit and enter power saving states. For more information
578 * see comments for struct mmc_host_ops.
580 int mmc_host_disable(struct mmc_host *host)
584 if (!(host->caps & MMC_CAP_DISABLE))
587 if (host->en_dis_recurs)
590 if (--host->nesting_cnt)
596 err = mmc_host_do_disable(host, 0);
599 EXPORT_SYMBOL(mmc_host_disable);
602 * __mmc_claim_host - exclusively claim a host
603 * @host: mmc host to claim
604 * @abort: whether or not the operation should be aborted
606 * Claim a host for a set of operations. If @abort is non null and
607 * dereference a non-zero value then this will return prematurely with
608 * that non-zero value without acquiring the lock. Returns zero
609 * with the lock held otherwise.
611 int __mmc_claim_host(struct mmc_host *host, atomic_t *abort)
613 DECLARE_WAITQUEUE(wait, current);
619 add_wait_queue(&host->wq, &wait);
620 spin_lock_irqsave(&host->lock, flags);
622 set_current_state(TASK_UNINTERRUPTIBLE);
623 stop = abort ? atomic_read(abort) : 0;
624 if (stop || !host->claimed || host->claimer == current)
626 spin_unlock_irqrestore(&host->lock, flags);
628 spin_lock_irqsave(&host->lock, flags);
630 set_current_state(TASK_RUNNING);
633 host->claimer = current;
634 host->claim_cnt += 1;
637 spin_unlock_irqrestore(&host->lock, flags);
638 remove_wait_queue(&host->wq, &wait);
640 mmc_host_enable(host);
644 EXPORT_SYMBOL(__mmc_claim_host);
647 * mmc_try_claim_host - try exclusively to claim a host
648 * @host: mmc host to claim
650 * Returns %1 if the host is claimed, %0 otherwise.
652 int mmc_try_claim_host(struct mmc_host *host)
654 int claimed_host = 0;
657 spin_lock_irqsave(&host->lock, flags);
658 if (!host->claimed || host->claimer == current) {
660 host->claimer = current;
661 host->claim_cnt += 1;
664 spin_unlock_irqrestore(&host->lock, flags);
667 EXPORT_SYMBOL(mmc_try_claim_host);
670 * mmc_do_release_host - release a claimed host
671 * @host: mmc host to release
673 * If you successfully claimed a host, this function will
676 void mmc_do_release_host(struct mmc_host *host)
680 spin_lock_irqsave(&host->lock, flags);
681 if (--host->claim_cnt) {
682 /* Release for nested claim */
683 spin_unlock_irqrestore(&host->lock, flags);
686 host->claimer = NULL;
687 spin_unlock_irqrestore(&host->lock, flags);
691 EXPORT_SYMBOL(mmc_do_release_host);
693 void mmc_host_deeper_disable(struct work_struct *work)
695 struct mmc_host *host =
696 container_of(work, struct mmc_host, disable.work);
698 /* If the host is claimed then we do not want to disable it anymore */
699 if (!mmc_try_claim_host(host))
701 mmc_host_do_disable(host, 1);
702 mmc_do_release_host(host);
706 * mmc_host_lazy_disable - lazily disable a host.
707 * @host: mmc host to disable
709 * Hosts that support power saving can use the 'enable' and 'disable'
710 * methods to exit and enter power saving states. For more information
711 * see comments for struct mmc_host_ops.
713 int mmc_host_lazy_disable(struct mmc_host *host)
715 if (!(host->caps & MMC_CAP_DISABLE))
718 if (host->en_dis_recurs)
721 if (--host->nesting_cnt)
727 if (host->disable_delay) {
728 mmc_schedule_delayed_work(&host->disable,
729 msecs_to_jiffies(host->disable_delay));
732 return mmc_host_do_disable(host, 1);
734 EXPORT_SYMBOL(mmc_host_lazy_disable);
737 * mmc_release_host - release a host
738 * @host: mmc host to release
740 * Release a MMC host, allowing others to claim the host
741 * for their operations.
743 void mmc_release_host(struct mmc_host *host)
745 WARN_ON(!host->claimed);
747 mmc_host_lazy_disable(host);
749 mmc_do_release_host(host);
752 EXPORT_SYMBOL(mmc_release_host);
755 * Internal function that does the actual ios call to the host driver,
756 * optionally printing some debug output.
758 static inline void mmc_set_ios(struct mmc_host *host)
760 struct mmc_ios *ios = &host->ios;
762 pr_debug("%s: clock %uHz busmode %u powermode %u cs %u Vdd %u "
763 "width %u timing %u\n",
764 mmc_hostname(host), ios->clock, ios->bus_mode,
765 ios->power_mode, ios->chip_select, ios->vdd,
766 ios->bus_width, ios->timing);
769 mmc_set_ungated(host);
770 host->ops->set_ios(host, ios);
774 * Control chip select pin on a host.
776 void mmc_set_chip_select(struct mmc_host *host, int mode)
778 mmc_host_clk_hold(host);
779 host->ios.chip_select = mode;
781 mmc_host_clk_release(host);
785 * Sets the host clock to the highest possible frequency that
788 static void __mmc_set_clock(struct mmc_host *host, unsigned int hz)
790 WARN_ON(hz < host->f_min);
792 if (hz > host->f_max)
795 host->ios.clock = hz;
799 void mmc_set_clock(struct mmc_host *host, unsigned int hz)
801 mmc_host_clk_hold(host);
802 __mmc_set_clock(host, hz);
803 mmc_host_clk_release(host);
806 #ifdef CONFIG_MMC_CLKGATE
808 * This gates the clock by setting it to 0 Hz.
810 void mmc_gate_clock(struct mmc_host *host)
814 spin_lock_irqsave(&host->clk_lock, flags);
815 host->clk_old = host->ios.clock;
817 host->clk_gated = true;
818 spin_unlock_irqrestore(&host->clk_lock, flags);
823 * This restores the clock from gating by using the cached
826 void mmc_ungate_clock(struct mmc_host *host)
829 * We should previously have gated the clock, so the clock shall
830 * be 0 here! The clock may however be 0 during initialization,
831 * when some request operations are performed before setting
832 * the frequency. When ungate is requested in that situation
833 * we just ignore the call.
836 BUG_ON(host->ios.clock);
837 /* This call will also set host->clk_gated to false */
838 __mmc_set_clock(host, host->clk_old);
842 void mmc_set_ungated(struct mmc_host *host)
847 * We've been given a new frequency while the clock is gated,
848 * so make sure we regard this as ungating it.
850 spin_lock_irqsave(&host->clk_lock, flags);
851 host->clk_gated = false;
852 spin_unlock_irqrestore(&host->clk_lock, flags);
856 void mmc_set_ungated(struct mmc_host *host)
862 * Change the bus mode (open drain/push-pull) of a host.
864 void mmc_set_bus_mode(struct mmc_host *host, unsigned int mode)
866 mmc_host_clk_hold(host);
867 host->ios.bus_mode = mode;
869 mmc_host_clk_release(host);
873 * Change data bus width of a host.
875 void mmc_set_bus_width(struct mmc_host *host, unsigned int width)
877 mmc_host_clk_hold(host);
878 host->ios.bus_width = width;
880 mmc_host_clk_release(host);
884 * mmc_vdd_to_ocrbitnum - Convert a voltage to the OCR bit number
886 * @low_bits: prefer low bits in boundary cases
888 * This function returns the OCR bit number according to the provided @vdd
889 * value. If conversion is not possible a negative errno value returned.
891 * Depending on the @low_bits flag the function prefers low or high OCR bits
892 * on boundary voltages. For example,
893 * with @low_bits = true, 3300 mV translates to ilog2(MMC_VDD_32_33);
894 * with @low_bits = false, 3300 mV translates to ilog2(MMC_VDD_33_34);
896 * Any value in the [1951:1999] range translates to the ilog2(MMC_VDD_20_21).
898 static int mmc_vdd_to_ocrbitnum(int vdd, bool low_bits)
900 const int max_bit = ilog2(MMC_VDD_35_36);
903 if (vdd < 1650 || vdd > 3600)
906 if (vdd >= 1650 && vdd <= 1950)
907 return ilog2(MMC_VDD_165_195);
912 /* Base 2000 mV, step 100 mV, bit's base 8. */
913 bit = (vdd - 2000) / 100 + 8;
920 * mmc_vddrange_to_ocrmask - Convert a voltage range to the OCR mask
921 * @vdd_min: minimum voltage value (mV)
922 * @vdd_max: maximum voltage value (mV)
924 * This function returns the OCR mask bits according to the provided @vdd_min
925 * and @vdd_max values. If conversion is not possible the function returns 0.
927 * Notes wrt boundary cases:
928 * This function sets the OCR bits for all boundary voltages, for example
929 * [3300:3400] range is translated to MMC_VDD_32_33 | MMC_VDD_33_34 |
930 * MMC_VDD_34_35 mask.
932 u32 mmc_vddrange_to_ocrmask(int vdd_min, int vdd_max)
936 if (vdd_max < vdd_min)
939 /* Prefer high bits for the boundary vdd_max values. */
940 vdd_max = mmc_vdd_to_ocrbitnum(vdd_max, false);
944 /* Prefer low bits for the boundary vdd_min values. */
945 vdd_min = mmc_vdd_to_ocrbitnum(vdd_min, true);
949 /* Fill the mask, from max bit to min bit. */
950 while (vdd_max >= vdd_min)
951 mask |= 1 << vdd_max--;
955 EXPORT_SYMBOL(mmc_vddrange_to_ocrmask);
957 #ifdef CONFIG_REGULATOR
960 * mmc_regulator_get_ocrmask - return mask of supported voltages
961 * @supply: regulator to use
963 * This returns either a negative errno, or a mask of voltages that
964 * can be provided to MMC/SD/SDIO devices using the specified voltage
965 * regulator. This would normally be called before registering the
968 int mmc_regulator_get_ocrmask(struct regulator *supply)
974 count = regulator_count_voltages(supply);
978 for (i = 0; i < count; i++) {
982 vdd_uV = regulator_list_voltage(supply, i);
986 vdd_mV = vdd_uV / 1000;
987 result |= mmc_vddrange_to_ocrmask(vdd_mV, vdd_mV);
992 EXPORT_SYMBOL(mmc_regulator_get_ocrmask);
995 * mmc_regulator_set_ocr - set regulator to match host->ios voltage
996 * @mmc: the host to regulate
997 * @supply: regulator to use
998 * @vdd_bit: zero for power off, else a bit number (host->ios.vdd)
1000 * Returns zero on success, else negative errno.
1002 * MMC host drivers may use this to enable or disable a regulator using
1003 * a particular supply voltage. This would normally be called from the
1006 int mmc_regulator_set_ocr(struct mmc_host *mmc,
1007 struct regulator *supply,
1008 unsigned short vdd_bit)
1017 /* REVISIT mmc_vddrange_to_ocrmask() may have set some
1018 * bits this regulator doesn't quite support ... don't
1019 * be too picky, most cards and regulators are OK with
1020 * a 0.1V range goof (it's a small error percentage).
1022 tmp = vdd_bit - ilog2(MMC_VDD_165_195);
1024 min_uV = 1650 * 1000;
1025 max_uV = 1950 * 1000;
1027 min_uV = 1900 * 1000 + tmp * 100 * 1000;
1028 max_uV = min_uV + 100 * 1000;
1031 /* avoid needless changes to this voltage; the regulator
1032 * might not allow this operation
1034 voltage = regulator_get_voltage(supply);
1037 else if (voltage < min_uV || voltage > max_uV)
1038 result = regulator_set_voltage(supply, min_uV, max_uV);
1042 if (result == 0 && !mmc->regulator_enabled) {
1043 result = regulator_enable(supply);
1045 mmc->regulator_enabled = true;
1047 } else if (mmc->regulator_enabled) {
1048 result = regulator_disable(supply);
1050 mmc->regulator_enabled = false;
1054 dev_err(mmc_dev(mmc),
1055 "could not set regulator OCR (%d)\n", result);
1058 EXPORT_SYMBOL(mmc_regulator_set_ocr);
1060 #endif /* CONFIG_REGULATOR */
1063 * Mask off any voltages we don't support and select
1064 * the lowest voltage
1066 u32 mmc_select_voltage(struct mmc_host *host, u32 ocr)
1070 ocr &= host->ocr_avail;
1078 mmc_host_clk_hold(host);
1079 host->ios.vdd = bit;
1081 mmc_host_clk_release(host);
1083 pr_warning("%s: host doesn't support card's voltages\n",
1084 mmc_hostname(host));
1091 int mmc_set_signal_voltage(struct mmc_host *host, int signal_voltage, bool cmd11)
1093 struct mmc_command cmd = {0};
1099 * Send CMD11 only if the request is to switch the card to
1102 if ((signal_voltage != MMC_SIGNAL_VOLTAGE_330) && cmd11) {
1103 cmd.opcode = SD_SWITCH_VOLTAGE;
1105 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
1107 err = mmc_wait_for_cmd(host, &cmd, 0);
1111 if (!mmc_host_is_spi(host) && (cmd.resp[0] & R1_ERROR))
1115 host->ios.signal_voltage = signal_voltage;
1117 if (host->ops->start_signal_voltage_switch)
1118 err = host->ops->start_signal_voltage_switch(host, &host->ios);
1124 * Select timing parameters for host.
1126 void mmc_set_timing(struct mmc_host *host, unsigned int timing)
1128 mmc_host_clk_hold(host);
1129 host->ios.timing = timing;
1131 mmc_host_clk_release(host);
1135 * Select appropriate driver type for host.
1137 void mmc_set_driver_type(struct mmc_host *host, unsigned int drv_type)
1139 mmc_host_clk_hold(host);
1140 host->ios.drv_type = drv_type;
1142 mmc_host_clk_release(host);
1146 * Apply power to the MMC stack. This is a two-stage process.
1147 * First, we enable power to the card without the clock running.
1148 * We then wait a bit for the power to stabilise. Finally,
1149 * enable the bus drivers and clock to the card.
1151 * We must _NOT_ enable the clock prior to power stablising.
1153 * If a host does all the power sequencing itself, ignore the
1154 * initial MMC_POWER_UP stage.
1156 static void mmc_power_up(struct mmc_host *host)
1160 mmc_host_clk_hold(host);
1162 /* If ocr is set, we use it */
1164 bit = ffs(host->ocr) - 1;
1166 bit = fls(host->ocr_avail) - 1;
1168 host->ios.vdd = bit;
1169 if (mmc_host_is_spi(host))
1170 host->ios.chip_select = MMC_CS_HIGH;
1172 host->ios.chip_select = MMC_CS_DONTCARE;
1173 host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
1174 host->ios.power_mode = MMC_POWER_UP;
1175 host->ios.bus_width = MMC_BUS_WIDTH_1;
1176 host->ios.timing = MMC_TIMING_LEGACY;
1180 * This delay should be sufficient to allow the power supply
1181 * to reach the minimum voltage.
1185 host->ios.clock = host->f_init;
1187 host->ios.power_mode = MMC_POWER_ON;
1191 * This delay must be at least 74 clock sizes, or 1 ms, or the
1192 * time required to reach a stable voltage.
1196 mmc_host_clk_release(host);
1199 void mmc_power_off(struct mmc_host *host)
1201 mmc_host_clk_hold(host);
1203 host->ios.clock = 0;
1207 * Reset ocr mask to be the highest possible voltage supported for
1208 * this mmc host. This value will be used at next power up.
1210 host->ocr = 1 << (fls(host->ocr_avail) - 1);
1212 if (!mmc_host_is_spi(host)) {
1213 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
1214 host->ios.chip_select = MMC_CS_DONTCARE;
1216 host->ios.power_mode = MMC_POWER_OFF;
1217 host->ios.bus_width = MMC_BUS_WIDTH_1;
1218 host->ios.timing = MMC_TIMING_LEGACY;
1222 * Some configurations, such as the 802.11 SDIO card in the OLPC
1223 * XO-1.5, require a short delay after poweroff before the card
1224 * can be successfully turned on again.
1228 mmc_host_clk_release(host);
1232 * Cleanup when the last reference to the bus operator is dropped.
1234 static void __mmc_release_bus(struct mmc_host *host)
1237 BUG_ON(host->bus_refs);
1238 BUG_ON(!host->bus_dead);
1240 host->bus_ops = NULL;
1244 * Increase reference count of bus operator
1246 static inline void mmc_bus_get(struct mmc_host *host)
1248 unsigned long flags;
1250 spin_lock_irqsave(&host->lock, flags);
1252 spin_unlock_irqrestore(&host->lock, flags);
1256 * Decrease reference count of bus operator and free it if
1257 * it is the last reference.
1259 static inline void mmc_bus_put(struct mmc_host *host)
1261 unsigned long flags;
1263 spin_lock_irqsave(&host->lock, flags);
1265 if ((host->bus_refs == 0) && host->bus_ops)
1266 __mmc_release_bus(host);
1267 spin_unlock_irqrestore(&host->lock, flags);
1271 * Assign a mmc bus handler to a host. Only one bus handler may control a
1272 * host at any given time.
1274 void mmc_attach_bus(struct mmc_host *host, const struct mmc_bus_ops *ops)
1276 unsigned long flags;
1281 WARN_ON(!host->claimed);
1283 spin_lock_irqsave(&host->lock, flags);
1285 BUG_ON(host->bus_ops);
1286 BUG_ON(host->bus_refs);
1288 host->bus_ops = ops;
1292 spin_unlock_irqrestore(&host->lock, flags);
1296 * Remove the current bus handler from a host.
1298 void mmc_detach_bus(struct mmc_host *host)
1300 unsigned long flags;
1304 WARN_ON(!host->claimed);
1305 WARN_ON(!host->bus_ops);
1307 spin_lock_irqsave(&host->lock, flags);
1311 spin_unlock_irqrestore(&host->lock, flags);
1317 * mmc_detect_change - process change of state on a MMC socket
1318 * @host: host which changed state.
1319 * @delay: optional delay to wait before detection (jiffies)
1321 * MMC drivers should call this when they detect a card has been
1322 * inserted or removed. The MMC layer will confirm that any
1323 * present card is still functional, and initialize any newly
1326 void mmc_detect_change(struct mmc_host *host, unsigned long delay)
1328 #ifdef CONFIG_MMC_DEBUG
1329 unsigned long flags;
1330 spin_lock_irqsave(&host->lock, flags);
1331 WARN_ON(host->removed);
1332 spin_unlock_irqrestore(&host->lock, flags);
1335 mmc_schedule_delayed_work(&host->detect, delay);
1338 EXPORT_SYMBOL(mmc_detect_change);
1340 void mmc_init_erase(struct mmc_card *card)
1344 if (is_power_of_2(card->erase_size))
1345 card->erase_shift = ffs(card->erase_size) - 1;
1347 card->erase_shift = 0;
1350 * It is possible to erase an arbitrarily large area of an SD or MMC
1351 * card. That is not desirable because it can take a long time
1352 * (minutes) potentially delaying more important I/O, and also the
1353 * timeout calculations become increasingly hugely over-estimated.
1354 * Consequently, 'pref_erase' is defined as a guide to limit erases
1355 * to that size and alignment.
1357 * For SD cards that define Allocation Unit size, limit erases to one
1358 * Allocation Unit at a time. For MMC cards that define High Capacity
1359 * Erase Size, whether it is switched on or not, limit to that size.
1360 * Otherwise just have a stab at a good value. For modern cards it
1361 * will end up being 4MiB. Note that if the value is too small, it
1362 * can end up taking longer to erase.
1364 if (mmc_card_sd(card) && card->ssr.au) {
1365 card->pref_erase = card->ssr.au;
1366 card->erase_shift = ffs(card->ssr.au) - 1;
1367 } else if (card->ext_csd.hc_erase_size) {
1368 card->pref_erase = card->ext_csd.hc_erase_size;
1370 sz = (card->csd.capacity << (card->csd.read_blkbits - 9)) >> 11;
1372 card->pref_erase = 512 * 1024 / 512;
1374 card->pref_erase = 1024 * 1024 / 512;
1376 card->pref_erase = 2 * 1024 * 1024 / 512;
1378 card->pref_erase = 4 * 1024 * 1024 / 512;
1379 if (card->pref_erase < card->erase_size)
1380 card->pref_erase = card->erase_size;
1382 sz = card->pref_erase % card->erase_size;
1384 card->pref_erase += card->erase_size - sz;
1389 static unsigned int mmc_mmc_erase_timeout(struct mmc_card *card,
1390 unsigned int arg, unsigned int qty)
1392 unsigned int erase_timeout;
1394 if (card->ext_csd.erase_group_def & 1) {
1395 /* High Capacity Erase Group Size uses HC timeouts */
1396 if (arg == MMC_TRIM_ARG)
1397 erase_timeout = card->ext_csd.trim_timeout;
1399 erase_timeout = card->ext_csd.hc_erase_timeout;
1401 /* CSD Erase Group Size uses write timeout */
1402 unsigned int mult = (10 << card->csd.r2w_factor);
1403 unsigned int timeout_clks = card->csd.tacc_clks * mult;
1404 unsigned int timeout_us;
1406 /* Avoid overflow: e.g. tacc_ns=80000000 mult=1280 */
1407 if (card->csd.tacc_ns < 1000000)
1408 timeout_us = (card->csd.tacc_ns * mult) / 1000;
1410 timeout_us = (card->csd.tacc_ns / 1000) * mult;
1413 * ios.clock is only a target. The real clock rate might be
1414 * less but not that much less, so fudge it by multiplying by 2.
1417 timeout_us += (timeout_clks * 1000) /
1418 (mmc_host_clk_rate(card->host) / 1000);
1420 erase_timeout = timeout_us / 1000;
1423 * Theoretically, the calculation could underflow so round up
1424 * to 1ms in that case.
1430 /* Multiplier for secure operations */
1431 if (arg & MMC_SECURE_ARGS) {
1432 if (arg == MMC_SECURE_ERASE_ARG)
1433 erase_timeout *= card->ext_csd.sec_erase_mult;
1435 erase_timeout *= card->ext_csd.sec_trim_mult;
1438 erase_timeout *= qty;
1441 * Ensure at least a 1 second timeout for SPI as per
1442 * 'mmc_set_data_timeout()'
1444 if (mmc_host_is_spi(card->host) && erase_timeout < 1000)
1445 erase_timeout = 1000;
1447 return erase_timeout;
1450 static unsigned int mmc_sd_erase_timeout(struct mmc_card *card,
1454 unsigned int erase_timeout;
1456 if (card->ssr.erase_timeout) {
1457 /* Erase timeout specified in SD Status Register (SSR) */
1458 erase_timeout = card->ssr.erase_timeout * qty +
1459 card->ssr.erase_offset;
1462 * Erase timeout not specified in SD Status Register (SSR) so
1463 * use 250ms per write block.
1465 erase_timeout = 250 * qty;
1468 /* Must not be less than 1 second */
1469 if (erase_timeout < 1000)
1470 erase_timeout = 1000;
1472 return erase_timeout;
1475 static unsigned int mmc_erase_timeout(struct mmc_card *card,
1479 if (mmc_card_sd(card))
1480 return mmc_sd_erase_timeout(card, arg, qty);
1482 return mmc_mmc_erase_timeout(card, arg, qty);
1485 static int mmc_do_erase(struct mmc_card *card, unsigned int from,
1486 unsigned int to, unsigned int arg)
1488 struct mmc_command cmd = {0};
1489 unsigned int qty = 0;
1493 * qty is used to calculate the erase timeout which depends on how many
1494 * erase groups (or allocation units in SD terminology) are affected.
1495 * We count erasing part of an erase group as one erase group.
1496 * For SD, the allocation units are always a power of 2. For MMC, the
1497 * erase group size is almost certainly also power of 2, but it does not
1498 * seem to insist on that in the JEDEC standard, so we fall back to
1499 * division in that case. SD may not specify an allocation unit size,
1500 * in which case the timeout is based on the number of write blocks.
1502 * Note that the timeout for secure trim 2 will only be correct if the
1503 * number of erase groups specified is the same as the total of all
1504 * preceding secure trim 1 commands. Since the power may have been
1505 * lost since the secure trim 1 commands occurred, it is generally
1506 * impossible to calculate the secure trim 2 timeout correctly.
1508 if (card->erase_shift)
1509 qty += ((to >> card->erase_shift) -
1510 (from >> card->erase_shift)) + 1;
1511 else if (mmc_card_sd(card))
1512 qty += to - from + 1;
1514 qty += ((to / card->erase_size) -
1515 (from / card->erase_size)) + 1;
1517 if (!mmc_card_blockaddr(card)) {
1522 if (mmc_card_sd(card))
1523 cmd.opcode = SD_ERASE_WR_BLK_START;
1525 cmd.opcode = MMC_ERASE_GROUP_START;
1527 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
1528 err = mmc_wait_for_cmd(card->host, &cmd, 0);
1530 printk(KERN_ERR "mmc_erase: group start error %d, "
1531 "status %#x\n", err, cmd.resp[0]);
1536 memset(&cmd, 0, sizeof(struct mmc_command));
1537 if (mmc_card_sd(card))
1538 cmd.opcode = SD_ERASE_WR_BLK_END;
1540 cmd.opcode = MMC_ERASE_GROUP_END;
1542 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
1543 err = mmc_wait_for_cmd(card->host, &cmd, 0);
1545 printk(KERN_ERR "mmc_erase: group end error %d, status %#x\n",
1551 memset(&cmd, 0, sizeof(struct mmc_command));
1552 cmd.opcode = MMC_ERASE;
1554 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1555 cmd.cmd_timeout_ms = mmc_erase_timeout(card, arg, qty);
1556 err = mmc_wait_for_cmd(card->host, &cmd, 0);
1558 printk(KERN_ERR "mmc_erase: erase error %d, status %#x\n",
1564 if (mmc_host_is_spi(card->host))
1568 memset(&cmd, 0, sizeof(struct mmc_command));
1569 cmd.opcode = MMC_SEND_STATUS;
1570 cmd.arg = card->rca << 16;
1571 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
1572 /* Do not retry else we can't see errors */
1573 err = mmc_wait_for_cmd(card->host, &cmd, 0);
1574 if (err || (cmd.resp[0] & 0xFDF92000)) {
1575 printk(KERN_ERR "error %d requesting status %#x\n",
1580 } while (!(cmd.resp[0] & R1_READY_FOR_DATA) ||
1581 R1_CURRENT_STATE(cmd.resp[0]) == R1_STATE_PRG);
1587 * mmc_erase - erase sectors.
1588 * @card: card to erase
1589 * @from: first sector to erase
1590 * @nr: number of sectors to erase
1591 * @arg: erase command argument (SD supports only %MMC_ERASE_ARG)
1593 * Caller must claim host before calling this function.
1595 int mmc_erase(struct mmc_card *card, unsigned int from, unsigned int nr,
1598 unsigned int rem, to = from + nr;
1600 if (!(card->host->caps & MMC_CAP_ERASE) ||
1601 !(card->csd.cmdclass & CCC_ERASE))
1604 if (!card->erase_size)
1607 if (mmc_card_sd(card) && arg != MMC_ERASE_ARG)
1610 if ((arg & MMC_SECURE_ARGS) &&
1611 !(card->ext_csd.sec_feature_support & EXT_CSD_SEC_ER_EN))
1614 if ((arg & MMC_TRIM_ARGS) &&
1615 !(card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN))
1618 if (arg == MMC_SECURE_ERASE_ARG) {
1619 if (from % card->erase_size || nr % card->erase_size)
1623 if (arg == MMC_ERASE_ARG) {
1624 rem = from % card->erase_size;
1626 rem = card->erase_size - rem;
1633 rem = nr % card->erase_size;
1646 /* 'from' and 'to' are inclusive */
1649 return mmc_do_erase(card, from, to, arg);
1651 EXPORT_SYMBOL(mmc_erase);
1653 int mmc_can_erase(struct mmc_card *card)
1655 if ((card->host->caps & MMC_CAP_ERASE) &&
1656 (card->csd.cmdclass & CCC_ERASE) && card->erase_size)
1660 EXPORT_SYMBOL(mmc_can_erase);
1662 int mmc_can_trim(struct mmc_card *card)
1664 if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN)
1668 EXPORT_SYMBOL(mmc_can_trim);
1670 int mmc_can_secure_erase_trim(struct mmc_card *card)
1672 if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_ER_EN)
1676 EXPORT_SYMBOL(mmc_can_secure_erase_trim);
1678 int mmc_erase_group_aligned(struct mmc_card *card, unsigned int from,
1681 if (!card->erase_size)
1683 if (from % card->erase_size || nr % card->erase_size)
1687 EXPORT_SYMBOL(mmc_erase_group_aligned);
1689 static unsigned int mmc_do_calc_max_discard(struct mmc_card *card,
1692 struct mmc_host *host = card->host;
1693 unsigned int max_discard, x, y, qty = 0, max_qty, timeout;
1694 unsigned int last_timeout = 0;
1696 if (card->erase_shift)
1697 max_qty = UINT_MAX >> card->erase_shift;
1698 else if (mmc_card_sd(card))
1701 max_qty = UINT_MAX / card->erase_size;
1703 /* Find the largest qty with an OK timeout */
1706 for (x = 1; x && x <= max_qty && max_qty - x >= qty; x <<= 1) {
1707 timeout = mmc_erase_timeout(card, arg, qty + x);
1708 if (timeout > host->max_discard_to)
1710 if (timeout < last_timeout)
1712 last_timeout = timeout;
1724 /* Convert qty to sectors */
1725 if (card->erase_shift)
1726 max_discard = --qty << card->erase_shift;
1727 else if (mmc_card_sd(card))
1730 max_discard = --qty * card->erase_size;
1735 unsigned int mmc_calc_max_discard(struct mmc_card *card)
1737 struct mmc_host *host = card->host;
1738 unsigned int max_discard, max_trim;
1740 if (!host->max_discard_to)
1744 * Without erase_group_def set, MMC erase timeout depends on clock
1745 * frequence which can change. In that case, the best choice is
1746 * just the preferred erase size.
1748 if (mmc_card_mmc(card) && !(card->ext_csd.erase_group_def & 1))
1749 return card->pref_erase;
1751 max_discard = mmc_do_calc_max_discard(card, MMC_ERASE_ARG);
1752 if (mmc_can_trim(card)) {
1753 max_trim = mmc_do_calc_max_discard(card, MMC_TRIM_ARG);
1754 if (max_trim < max_discard)
1755 max_discard = max_trim;
1756 } else if (max_discard < card->erase_size) {
1759 pr_debug("%s: calculated max. discard sectors %u for timeout %u ms\n",
1760 mmc_hostname(host), max_discard, host->max_discard_to);
1763 EXPORT_SYMBOL(mmc_calc_max_discard);
1765 int mmc_set_blocklen(struct mmc_card *card, unsigned int blocklen)
1767 struct mmc_command cmd = {0};
1769 if (mmc_card_blockaddr(card) || mmc_card_ddr_mode(card))
1772 cmd.opcode = MMC_SET_BLOCKLEN;
1774 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
1775 return mmc_wait_for_cmd(card->host, &cmd, 5);
1777 EXPORT_SYMBOL(mmc_set_blocklen);
1779 static void mmc_hw_reset_for_init(struct mmc_host *host)
1781 if (!(host->caps & MMC_CAP_HW_RESET) || !host->ops->hw_reset)
1783 mmc_host_clk_hold(host);
1784 host->ops->hw_reset(host);
1785 mmc_host_clk_release(host);
1788 int mmc_can_reset(struct mmc_card *card)
1792 if (!mmc_card_mmc(card))
1794 rst_n_function = card->ext_csd.rst_n_function;
1795 if ((rst_n_function & EXT_CSD_RST_N_EN_MASK) != EXT_CSD_RST_N_ENABLED)
1799 EXPORT_SYMBOL(mmc_can_reset);
1801 static int mmc_do_hw_reset(struct mmc_host *host, int check)
1803 struct mmc_card *card = host->card;
1805 if (!host->bus_ops->power_restore)
1808 if (!(host->caps & MMC_CAP_HW_RESET) || !host->ops->hw_reset)
1814 if (!mmc_can_reset(card))
1817 mmc_host_clk_hold(host);
1818 mmc_set_clock(host, host->f_init);
1820 host->ops->hw_reset(host);
1822 /* If the reset has happened, then a status command will fail */
1824 struct mmc_command cmd = {0};
1827 cmd.opcode = MMC_SEND_STATUS;
1828 if (!mmc_host_is_spi(card->host))
1829 cmd.arg = card->rca << 16;
1830 cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
1831 err = mmc_wait_for_cmd(card->host, &cmd, 0);
1833 mmc_host_clk_release(host);
1838 host->card->state &= ~(MMC_STATE_HIGHSPEED | MMC_STATE_HIGHSPEED_DDR);
1839 if (mmc_host_is_spi(host)) {
1840 host->ios.chip_select = MMC_CS_HIGH;
1841 host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
1843 host->ios.chip_select = MMC_CS_DONTCARE;
1844 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
1846 host->ios.bus_width = MMC_BUS_WIDTH_1;
1847 host->ios.timing = MMC_TIMING_LEGACY;
1850 mmc_host_clk_release(host);
1852 return host->bus_ops->power_restore(host);
1855 int mmc_hw_reset(struct mmc_host *host)
1857 return mmc_do_hw_reset(host, 0);
1859 EXPORT_SYMBOL(mmc_hw_reset);
1861 int mmc_hw_reset_check(struct mmc_host *host)
1863 return mmc_do_hw_reset(host, 1);
1865 EXPORT_SYMBOL(mmc_hw_reset_check);
1867 static int mmc_rescan_try_freq(struct mmc_host *host, unsigned freq)
1869 host->f_init = freq;
1871 #ifdef CONFIG_MMC_DEBUG
1872 pr_info("%s: %s: trying to init card at %u Hz\n",
1873 mmc_hostname(host), __func__, host->f_init);
1878 * Some eMMCs (with VCCQ always on) may not be reset after power up, so
1879 * do a hardware reset if possible.
1881 mmc_hw_reset_for_init(host);
1884 * sdio_reset sends CMD52 to reset card. Since we do not know
1885 * if the card is being re-initialized, just send it. CMD52
1886 * should be ignored by SD/eMMC cards.
1891 mmc_send_if_cond(host, host->ocr_avail);
1893 /* Order's important: probe SDIO, then SD, then MMC */
1894 if (!mmc_attach_sdio(host))
1896 if (!mmc_attach_sd(host))
1898 if (!mmc_attach_mmc(host))
1901 mmc_power_off(host);
1905 void mmc_rescan(struct work_struct *work)
1907 static const unsigned freqs[] = { 400000, 300000, 200000, 100000 };
1908 struct mmc_host *host =
1909 container_of(work, struct mmc_host, detect.work);
1912 if (host->rescan_disable)
1918 * if there is a _removable_ card registered, check whether it is
1921 if (host->bus_ops && host->bus_ops->detect && !host->bus_dead
1922 && !(host->caps & MMC_CAP_NONREMOVABLE))
1923 host->bus_ops->detect(host);
1926 * Let mmc_bus_put() free the bus/bus_ops if we've found that
1927 * the card is no longer present.
1932 /* if there still is a card present, stop here */
1933 if (host->bus_ops != NULL) {
1939 * Only we can add a new handler, so it's safe to
1940 * release the lock here.
1944 if (host->ops->get_cd && host->ops->get_cd(host) == 0)
1947 mmc_claim_host(host);
1948 for (i = 0; i < ARRAY_SIZE(freqs); i++) {
1949 if (!mmc_rescan_try_freq(host, max(freqs[i], host->f_min)))
1951 if (freqs[i] <= host->f_min)
1954 mmc_release_host(host);
1957 if (host->caps & MMC_CAP_NEEDS_POLL)
1958 mmc_schedule_delayed_work(&host->detect, HZ);
1961 void mmc_start_host(struct mmc_host *host)
1963 mmc_power_off(host);
1964 mmc_detect_change(host, 0);
1967 void mmc_stop_host(struct mmc_host *host)
1969 #ifdef CONFIG_MMC_DEBUG
1970 unsigned long flags;
1971 spin_lock_irqsave(&host->lock, flags);
1973 spin_unlock_irqrestore(&host->lock, flags);
1976 if (host->caps & MMC_CAP_DISABLE)
1977 cancel_delayed_work(&host->disable);
1978 cancel_delayed_work_sync(&host->detect);
1979 mmc_flush_scheduled_work();
1981 /* clear pm flags now and let card drivers set them as needed */
1985 if (host->bus_ops && !host->bus_dead) {
1986 if (host->bus_ops->remove)
1987 host->bus_ops->remove(host);
1989 mmc_claim_host(host);
1990 mmc_detach_bus(host);
1991 mmc_power_off(host);
1992 mmc_release_host(host);
2000 mmc_power_off(host);
2003 int mmc_power_save_host(struct mmc_host *host)
2007 #ifdef CONFIG_MMC_DEBUG
2008 pr_info("%s: %s: powering down\n", mmc_hostname(host), __func__);
2013 if (!host->bus_ops || host->bus_dead || !host->bus_ops->power_restore) {
2018 if (host->bus_ops->power_save)
2019 ret = host->bus_ops->power_save(host);
2023 mmc_power_off(host);
2027 EXPORT_SYMBOL(mmc_power_save_host);
2029 int mmc_power_restore_host(struct mmc_host *host)
2033 #ifdef CONFIG_MMC_DEBUG
2034 pr_info("%s: %s: powering up\n", mmc_hostname(host), __func__);
2039 if (!host->bus_ops || host->bus_dead || !host->bus_ops->power_restore) {
2045 ret = host->bus_ops->power_restore(host);
2051 EXPORT_SYMBOL(mmc_power_restore_host);
2053 int mmc_card_awake(struct mmc_host *host)
2059 if (host->bus_ops && !host->bus_dead && host->bus_ops->awake)
2060 err = host->bus_ops->awake(host);
2066 EXPORT_SYMBOL(mmc_card_awake);
2068 int mmc_card_sleep(struct mmc_host *host)
2074 if (host->bus_ops && !host->bus_dead && host->bus_ops->awake)
2075 err = host->bus_ops->sleep(host);
2081 EXPORT_SYMBOL(mmc_card_sleep);
2083 int mmc_card_can_sleep(struct mmc_host *host)
2085 struct mmc_card *card = host->card;
2087 if (card && mmc_card_mmc(card) && card->ext_csd.rev >= 3)
2091 EXPORT_SYMBOL(mmc_card_can_sleep);
2096 * mmc_suspend_host - suspend a host
2099 int mmc_suspend_host(struct mmc_host *host)
2103 if (host->caps & MMC_CAP_DISABLE)
2104 cancel_delayed_work(&host->disable);
2105 cancel_delayed_work(&host->detect);
2106 mmc_flush_scheduled_work();
2109 if (host->bus_ops && !host->bus_dead) {
2110 if (host->bus_ops->suspend)
2111 err = host->bus_ops->suspend(host);
2112 if (err == -ENOSYS || !host->bus_ops->resume) {
2114 * We simply "remove" the card in this case.
2115 * It will be redetected on resume.
2117 if (host->bus_ops->remove)
2118 host->bus_ops->remove(host);
2119 mmc_claim_host(host);
2120 mmc_detach_bus(host);
2121 mmc_power_off(host);
2122 mmc_release_host(host);
2129 if (!err && !mmc_card_keep_power(host))
2130 mmc_power_off(host);
2135 EXPORT_SYMBOL(mmc_suspend_host);
2138 * mmc_resume_host - resume a previously suspended host
2141 int mmc_resume_host(struct mmc_host *host)
2146 if (host->bus_ops && !host->bus_dead) {
2147 if (!mmc_card_keep_power(host)) {
2149 mmc_select_voltage(host, host->ocr);
2151 * Tell runtime PM core we just powered up the card,
2152 * since it still believes the card is powered off.
2153 * Note that currently runtime PM is only enabled
2154 * for SDIO cards that are MMC_CAP_POWER_OFF_CARD
2156 if (mmc_card_sdio(host->card) &&
2157 (host->caps & MMC_CAP_POWER_OFF_CARD)) {
2158 pm_runtime_disable(&host->card->dev);
2159 pm_runtime_set_active(&host->card->dev);
2160 pm_runtime_enable(&host->card->dev);
2163 BUG_ON(!host->bus_ops->resume);
2164 err = host->bus_ops->resume(host);
2166 printk(KERN_WARNING "%s: error %d during resume "
2167 "(card was removed?)\n",
2168 mmc_hostname(host), err);
2172 host->pm_flags &= ~MMC_PM_KEEP_POWER;
2177 EXPORT_SYMBOL(mmc_resume_host);
2179 /* Do the card removal on suspend if card is assumed removeable
2180 * Do that in pm notifier while userspace isn't yet frozen, so we will be able
2183 int mmc_pm_notify(struct notifier_block *notify_block,
2184 unsigned long mode, void *unused)
2186 struct mmc_host *host = container_of(
2187 notify_block, struct mmc_host, pm_notify);
2188 unsigned long flags;
2192 case PM_HIBERNATION_PREPARE:
2193 case PM_SUSPEND_PREPARE:
2195 spin_lock_irqsave(&host->lock, flags);
2196 host->rescan_disable = 1;
2197 spin_unlock_irqrestore(&host->lock, flags);
2198 cancel_delayed_work_sync(&host->detect);
2200 if (!host->bus_ops || host->bus_ops->suspend)
2203 mmc_claim_host(host);
2205 if (host->bus_ops->remove)
2206 host->bus_ops->remove(host);
2208 mmc_detach_bus(host);
2209 mmc_power_off(host);
2210 mmc_release_host(host);
2214 case PM_POST_SUSPEND:
2215 case PM_POST_HIBERNATION:
2216 case PM_POST_RESTORE:
2218 spin_lock_irqsave(&host->lock, flags);
2219 host->rescan_disable = 0;
2220 spin_unlock_irqrestore(&host->lock, flags);
2221 mmc_detect_change(host, 0);
2229 static int __init mmc_init(void)
2233 workqueue = alloc_ordered_workqueue("kmmcd", 0);
2237 ret = mmc_register_bus();
2239 goto destroy_workqueue;
2241 ret = mmc_register_host_class();
2243 goto unregister_bus;
2245 ret = sdio_register_bus();
2247 goto unregister_host_class;
2251 unregister_host_class:
2252 mmc_unregister_host_class();
2254 mmc_unregister_bus();
2256 destroy_workqueue(workqueue);
2261 static void __exit mmc_exit(void)
2263 sdio_unregister_bus();
2264 mmc_unregister_host_class();
2265 mmc_unregister_bus();
2266 destroy_workqueue(workqueue);
2269 subsys_initcall(mmc_init);
2270 module_exit(mmc_exit);
2272 MODULE_LICENSE("GPL");