2 * linux/drivers/mmc/core/core.c
4 * Copyright (C) 2003-2004 Russell King, All Rights Reserved.
5 * SD support Copyright (C) 2004 Ian Molton, All Rights Reserved.
6 * Copyright (C) 2005-2008 Pierre Ossman, All Rights Reserved.
7 * MMCv4 support Copyright (C) 2006 Philip Langdale, All Rights Reserved.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
13 #include <linux/module.h>
14 #include <linux/init.h>
15 #include <linux/interrupt.h>
16 #include <linux/completion.h>
17 #include <linux/device.h>
18 #include <linux/delay.h>
19 #include <linux/pagemap.h>
20 #include <linux/err.h>
21 #include <linux/leds.h>
22 #include <linux/scatterlist.h>
23 #include <linux/log2.h>
24 #include <linux/regulator/consumer.h>
25 #include <linux/pm_runtime.h>
26 #include <linux/wakelock.h>
28 #include <linux/mmc/card.h>
29 #include <linux/mmc/host.h>
30 #include <linux/mmc/mmc.h>
31 #include <linux/mmc/sd.h>
42 static struct workqueue_struct *workqueue;
45 * Enabling software CRCs on the data blocks can be a significant (30%)
46 * performance cost, and for other reasons may not always be desired.
47 * So we allow it it to be disabled.
50 module_param(use_spi_crc, bool, 0);
53 * We normally treat cards as removed during suspend if they are not
54 * known to be on a non-removable bus, to avoid the risk of writing
55 * back data to a different card after resume. Allow this to be
56 * overridden if necessary.
58 #ifdef CONFIG_MMC_UNSAFE_RESUME
59 int mmc_assume_removable;
61 int mmc_assume_removable = 1;
63 EXPORT_SYMBOL(mmc_assume_removable);
64 module_param_named(removable, mmc_assume_removable, bool, 0644);
67 "MMC/SD cards are removable and may be removed during suspend");
70 * Internal function. Schedule delayed work in the MMC work queue.
72 static int mmc_schedule_delayed_work(struct delayed_work *work,
75 return queue_delayed_work(workqueue, work, delay);
79 * Internal function. Flush all scheduled work from the MMC work queue.
81 static void mmc_flush_scheduled_work(void)
83 flush_workqueue(workqueue);
87 * mmc_request_done - finish processing an MMC request
88 * @host: MMC host which completed request
89 * @mrq: MMC request which request
91 * MMC drivers should call this function when they have completed
92 * their processing of a request.
94 void mmc_request_done(struct mmc_host *host, struct mmc_request *mrq)
96 struct mmc_command *cmd = mrq->cmd;
99 if (err && cmd->retries && mmc_host_is_spi(host)) {
100 if (cmd->resp[0] & R1_SPI_ILLEGAL_COMMAND)
104 if (err && cmd->retries) {
105 pr_debug("%s: req failed (CMD%u): %d, retrying...\n",
106 mmc_hostname(host), cmd->opcode, err);
110 host->ops->request(host, mrq);
112 led_trigger_event(host->led, LED_OFF);
114 pr_debug("%s: req done (CMD%u): %d: %08x %08x %08x %08x\n",
115 mmc_hostname(host), cmd->opcode, err,
116 cmd->resp[0], cmd->resp[1],
117 cmd->resp[2], cmd->resp[3]);
120 pr_debug("%s: %d bytes transferred: %d\n",
122 mrq->data->bytes_xfered, mrq->data->error);
126 pr_debug("%s: (CMD%u): %d: %08x %08x %08x %08x\n",
127 mmc_hostname(host), mrq->stop->opcode,
129 mrq->stop->resp[0], mrq->stop->resp[1],
130 mrq->stop->resp[2], mrq->stop->resp[3]);
136 mmc_host_clk_release(host);
140 EXPORT_SYMBOL(mmc_request_done);
143 mmc_start_request(struct mmc_host *host, struct mmc_request *mrq)
145 #ifdef CONFIG_MMC_DEBUG
147 struct scatterlist *sg;
150 pr_debug("%s: starting CMD%u arg %08x flags %08x\n",
151 mmc_hostname(host), mrq->cmd->opcode,
152 mrq->cmd->arg, mrq->cmd->flags);
155 pr_debug("%s: blksz %d blocks %d flags %08x "
156 "tsac %d ms nsac %d\n",
157 mmc_hostname(host), mrq->data->blksz,
158 mrq->data->blocks, mrq->data->flags,
159 mrq->data->timeout_ns / 1000000,
160 mrq->data->timeout_clks);
164 pr_debug("%s: CMD%u arg %08x flags %08x\n",
165 mmc_hostname(host), mrq->stop->opcode,
166 mrq->stop->arg, mrq->stop->flags);
169 WARN_ON(!host->claimed);
174 BUG_ON(mrq->data->blksz > host->max_blk_size);
175 BUG_ON(mrq->data->blocks > host->max_blk_count);
176 BUG_ON(mrq->data->blocks * mrq->data->blksz >
179 #ifdef CONFIG_MMC_DEBUG
181 for_each_sg(mrq->data->sg, sg, mrq->data->sg_len, i)
183 BUG_ON(sz != mrq->data->blocks * mrq->data->blksz);
186 mrq->cmd->data = mrq->data;
187 mrq->data->error = 0;
188 mrq->data->mrq = mrq;
190 mrq->data->stop = mrq->stop;
191 mrq->stop->error = 0;
192 mrq->stop->mrq = mrq;
195 mmc_host_clk_hold(host);
196 led_trigger_event(host->led, LED_FULL);
197 host->ops->request(host, mrq);
200 static void mmc_wait_done(struct mmc_request *mrq)
202 complete(mrq->done_data);
206 * mmc_wait_for_req - start a request and wait for completion
207 * @host: MMC host to start command
208 * @mrq: MMC request to start
210 * Start a new MMC custom command request for a host, and wait
211 * for the command to complete. Does not attempt to parse the
214 void mmc_wait_for_req(struct mmc_host *host, struct mmc_request *mrq)
216 #if defined(CONFIG_SDMMC_RK29) && !defined(CONFIG_SDMMC_RK29_OLD)
217 unsigned long datasize, waittime = 0xFFFF;
221 DECLARE_COMPLETION_ONSTACK(complete);
223 mrq->done_data = &complete;
224 mrq->done = mmc_wait_done;
226 mmc_start_request(host, mrq);
228 #if defined(CONFIG_SDMMC_RK29) && !defined(CONFIG_SDMMC_RK29_OLD)
229 if( strncmp( mmc_hostname(host) ,"mmc0" , strlen("mmc0")) )
231 multi = (mrq->cmd->retries>0)?mrq->cmd->retries:1;
232 waittime = wait_for_completion_timeout(&complete,HZ*7*multi); //sdio; for cmd dead. Modifyed by xbw at 2011-06-02
236 //calculate the timeout value for SDMMC; added by xbw at 2011-09-27
239 unit = 3*(1<<20);// unit=3MB
240 datasize = mrq->data->blksz*mrq->data->blocks;
241 multi = datasize/unit;
242 multi += (datasize%unit)?1:0;
243 multi = (multi>0) ? multi : 1;
244 multi += (mrq->cmd->retries>0)?1:0;
245 waittime = wait_for_completion_timeout(&complete,HZ*7*multi); //It should be longer than bottom driver's time,due to the sum of two cmd time.
246 //modifyed by xbw at 2011-10-08
249 //rk29_sdmmc_request_end..2336... CMD12 wait busy timeout!!!!! ====xbw=[sd_mmc]====
250 //mmc_wait_for_req..236.. !!!!! wait for CMD25 timeout ===xbw[mmc0]===
254 multi = (mrq->cmd->retries>0)?mrq->cmd->retries:1;
255 waittime = wait_for_completion_timeout(&complete,HZ*7*multi);
262 mrq->cmd->error = -EIO;
263 printk(KERN_WARNING "%s..%d.. !!!!! wait for CMD%d timeout [%s]\n",\
264 __FUNCTION__, __LINE__, mrq->cmd->opcode, mmc_hostname(host));
267 wait_for_completion(&complete);
271 EXPORT_SYMBOL(mmc_wait_for_req);
274 * mmc_wait_for_cmd - start a command and wait for completion
275 * @host: MMC host to start command
276 * @cmd: MMC command to start
277 * @retries: maximum number of retries
279 * Start a new MMC command for a host, and wait for the command
280 * to complete. Return any error that occurred while the command
281 * was executing. Do not attempt to parse the response.
283 int mmc_wait_for_cmd(struct mmc_host *host, struct mmc_command *cmd, int retries)
285 struct mmc_request mrq = {0};
287 WARN_ON(!host->claimed);
289 memset(cmd->resp, 0, sizeof(cmd->resp));
290 cmd->retries = retries;
295 mmc_wait_for_req(host, &mrq);
300 EXPORT_SYMBOL(mmc_wait_for_cmd);
303 * mmc_set_data_timeout - set the timeout for a data command
304 * @data: data phase for command
305 * @card: the MMC card associated with the data transfer
307 * Computes the data timeout parameters according to the
308 * correct algorithm given the card type.
310 void mmc_set_data_timeout(struct mmc_data *data, const struct mmc_card *card)
315 * SDIO cards only define an upper 1 s limit on access.
317 if (mmc_card_sdio(card)) {
318 data->timeout_ns = 1000000000;
319 data->timeout_clks = 0;
324 * SD cards use a 100 multiplier rather than 10
326 mult = mmc_card_sd(card) ? 100 : 10;
329 * Scale up the multiplier (and therefore the timeout) by
330 * the r2w factor for writes.
332 if (data->flags & MMC_DATA_WRITE)
333 mult <<= card->csd.r2w_factor;
335 data->timeout_ns = card->csd.tacc_ns * mult;
336 data->timeout_clks = card->csd.tacc_clks * mult;
339 * SD cards also have an upper limit on the timeout.
341 if (mmc_card_sd(card)) {
342 unsigned int timeout_us, limit_us;
344 timeout_us = data->timeout_ns / 1000;
345 if (mmc_host_clk_rate(card->host))
346 timeout_us += data->timeout_clks * 1000 /
347 (mmc_host_clk_rate(card->host) / 1000);
349 if (data->flags & MMC_DATA_WRITE)
351 * The limit is really 250 ms, but that is
352 * insufficient for some crappy cards.
359 * SDHC cards always use these fixed values.
361 if (timeout_us > limit_us || mmc_card_blockaddr(card)) {
362 data->timeout_ns = limit_us * 1000;
363 data->timeout_clks = 0;
367 * Some cards need very high timeouts if driven in SPI mode.
368 * The worst observed timeout was 900ms after writing a
369 * continuous stream of data until the internal logic
372 if (mmc_host_is_spi(card->host)) {
373 if (data->flags & MMC_DATA_WRITE) {
374 if (data->timeout_ns < 1000000000)
375 data->timeout_ns = 1000000000; /* 1s */
377 if (data->timeout_ns < 100000000)
378 data->timeout_ns = 100000000; /* 100ms */
382 EXPORT_SYMBOL(mmc_set_data_timeout);
385 * mmc_align_data_size - pads a transfer size to a more optimal value
386 * @card: the MMC card associated with the data transfer
387 * @sz: original transfer size
389 * Pads the original data size with a number of extra bytes in
390 * order to avoid controller bugs and/or performance hits
391 * (e.g. some controllers revert to PIO for certain sizes).
393 * Returns the improved size, which might be unmodified.
395 * Note that this function is only relevant when issuing a
396 * single scatter gather entry.
398 unsigned int mmc_align_data_size(struct mmc_card *card, unsigned int sz)
401 * FIXME: We don't have a system for the controller to tell
402 * the core about its problems yet, so for now we just 32-bit
405 sz = ((sz + 3) / 4) * 4;
409 EXPORT_SYMBOL(mmc_align_data_size);
412 * mmc_host_enable - enable a host.
413 * @host: mmc host to enable
415 * Hosts that support power saving can use the 'enable' and 'disable'
416 * methods to exit and enter power saving states. For more information
417 * see comments for struct mmc_host_ops.
419 int mmc_host_enable(struct mmc_host *host)
421 if (!(host->caps & MMC_CAP_DISABLE))
424 if (host->en_dis_recurs)
427 if (host->nesting_cnt++)
430 cancel_delayed_work_sync(&host->disable);
435 if (host->ops->enable) {
438 host->en_dis_recurs = 1;
439 err = host->ops->enable(host);
440 host->en_dis_recurs = 0;
443 pr_debug("%s: enable error %d\n",
444 mmc_hostname(host), err);
451 EXPORT_SYMBOL(mmc_host_enable);
453 static int mmc_host_do_disable(struct mmc_host *host, int lazy)
455 if (host->ops->disable) {
458 host->en_dis_recurs = 1;
459 err = host->ops->disable(host, lazy);
460 host->en_dis_recurs = 0;
463 pr_debug("%s: disable error %d\n",
464 mmc_hostname(host), err);
468 unsigned long delay = msecs_to_jiffies(err);
470 mmc_schedule_delayed_work(&host->disable, delay);
478 * mmc_host_disable - disable a host.
479 * @host: mmc host to disable
481 * Hosts that support power saving can use the 'enable' and 'disable'
482 * methods to exit and enter power saving states. For more information
483 * see comments for struct mmc_host_ops.
485 int mmc_host_disable(struct mmc_host *host)
489 if (!(host->caps & MMC_CAP_DISABLE))
492 if (host->en_dis_recurs)
495 if (--host->nesting_cnt)
501 err = mmc_host_do_disable(host, 0);
504 EXPORT_SYMBOL(mmc_host_disable);
507 * __mmc_claim_host - exclusively claim a host
508 * @host: mmc host to claim
509 * @abort: whether or not the operation should be aborted
511 * Claim a host for a set of operations. If @abort is non null and
512 * dereference a non-zero value then this will return prematurely with
513 * that non-zero value without acquiring the lock. Returns zero
514 * with the lock held otherwise.
516 int __mmc_claim_host(struct mmc_host *host, atomic_t *abort)
518 DECLARE_WAITQUEUE(wait, current);
524 add_wait_queue(&host->wq, &wait);
525 spin_lock_irqsave(&host->lock, flags);
527 set_current_state(TASK_UNINTERRUPTIBLE);
528 stop = abort ? atomic_read(abort) : 0;
529 if (stop || !host->claimed || host->claimer == current)
531 spin_unlock_irqrestore(&host->lock, flags);
533 spin_lock_irqsave(&host->lock, flags);
535 set_current_state(TASK_RUNNING);
538 host->claimer = current;
539 host->claim_cnt += 1;
542 spin_unlock_irqrestore(&host->lock, flags);
543 remove_wait_queue(&host->wq, &wait);
545 mmc_host_enable(host);
549 EXPORT_SYMBOL(__mmc_claim_host);
552 * mmc_try_claim_host - try exclusively to claim a host
553 * @host: mmc host to claim
555 * Returns %1 if the host is claimed, %0 otherwise.
557 int mmc_try_claim_host(struct mmc_host *host)
559 int claimed_host = 0;
562 spin_lock_irqsave(&host->lock, flags);
563 if (!host->claimed || host->claimer == current) {
565 host->claimer = current;
566 host->claim_cnt += 1;
569 spin_unlock_irqrestore(&host->lock, flags);
572 EXPORT_SYMBOL(mmc_try_claim_host);
575 * mmc_do_release_host - release a claimed host
576 * @host: mmc host to release
578 * If you successfully claimed a host, this function will
581 void mmc_do_release_host(struct mmc_host *host)
585 spin_lock_irqsave(&host->lock, flags);
586 if (--host->claim_cnt) {
587 /* Release for nested claim */
588 spin_unlock_irqrestore(&host->lock, flags);
591 host->claimer = NULL;
592 spin_unlock_irqrestore(&host->lock, flags);
596 EXPORT_SYMBOL(mmc_do_release_host);
598 void mmc_host_deeper_disable(struct work_struct *work)
600 struct mmc_host *host =
601 container_of(work, struct mmc_host, disable.work);
603 /* If the host is claimed then we do not want to disable it anymore */
604 if (!mmc_try_claim_host(host))
606 mmc_host_do_disable(host, 1);
607 mmc_do_release_host(host);
611 * mmc_host_lazy_disable - lazily disable a host.
612 * @host: mmc host to disable
614 * Hosts that support power saving can use the 'enable' and 'disable'
615 * methods to exit and enter power saving states. For more information
616 * see comments for struct mmc_host_ops.
618 int mmc_host_lazy_disable(struct mmc_host *host)
620 if (!(host->caps & MMC_CAP_DISABLE))
623 if (host->en_dis_recurs)
626 if (--host->nesting_cnt)
632 if (host->disable_delay) {
633 mmc_schedule_delayed_work(&host->disable,
634 msecs_to_jiffies(host->disable_delay));
637 return mmc_host_do_disable(host, 1);
639 EXPORT_SYMBOL(mmc_host_lazy_disable);
642 * mmc_release_host - release a host
643 * @host: mmc host to release
645 * Release a MMC host, allowing others to claim the host
646 * for their operations.
648 void mmc_release_host(struct mmc_host *host)
650 WARN_ON(!host->claimed);
652 mmc_host_lazy_disable(host);
654 mmc_do_release_host(host);
657 EXPORT_SYMBOL(mmc_release_host);
660 * Internal function that does the actual ios call to the host driver,
661 * optionally printing some debug output.
663 static inline void mmc_set_ios(struct mmc_host *host)
665 struct mmc_ios *ios = &host->ios;
667 pr_debug("%s: clock %uHz busmode %u powermode %u cs %u Vdd %u "
668 "width %u timing %u\n",
669 mmc_hostname(host), ios->clock, ios->bus_mode,
670 ios->power_mode, ios->chip_select, ios->vdd,
671 ios->bus_width, ios->timing);
674 mmc_set_ungated(host);
675 host->ops->set_ios(host, ios);
679 * Control chip select pin on a host.
681 void mmc_set_chip_select(struct mmc_host *host, int mode)
683 mmc_host_clk_hold(host);
684 host->ios.chip_select = mode;
686 mmc_host_clk_release(host);
690 * Sets the host clock to the highest possible frequency that
693 static void __mmc_set_clock(struct mmc_host *host, unsigned int hz)
695 WARN_ON(hz < host->f_min);
697 if (hz > host->f_max)
700 host->ios.clock = hz;
704 void mmc_set_clock(struct mmc_host *host, unsigned int hz)
706 mmc_host_clk_hold(host);
707 __mmc_set_clock(host, hz);
708 mmc_host_clk_release(host);
711 #ifdef CONFIG_MMC_CLKGATE
713 * This gates the clock by setting it to 0 Hz.
715 void mmc_gate_clock(struct mmc_host *host)
719 spin_lock_irqsave(&host->clk_lock, flags);
720 host->clk_old = host->ios.clock;
722 host->clk_gated = true;
723 spin_unlock_irqrestore(&host->clk_lock, flags);
728 * This restores the clock from gating by using the cached
731 void mmc_ungate_clock(struct mmc_host *host)
734 * We should previously have gated the clock, so the clock shall
735 * be 0 here! The clock may however be 0 during initialization,
736 * when some request operations are performed before setting
737 * the frequency. When ungate is requested in that situation
738 * we just ignore the call.
741 BUG_ON(host->ios.clock);
742 /* This call will also set host->clk_gated to false */
743 __mmc_set_clock(host, host->clk_old);
747 void mmc_set_ungated(struct mmc_host *host)
752 * We've been given a new frequency while the clock is gated,
753 * so make sure we regard this as ungating it.
755 spin_lock_irqsave(&host->clk_lock, flags);
756 host->clk_gated = false;
757 spin_unlock_irqrestore(&host->clk_lock, flags);
761 void mmc_set_ungated(struct mmc_host *host)
767 * Change the bus mode (open drain/push-pull) of a host.
769 void mmc_set_bus_mode(struct mmc_host *host, unsigned int mode)
771 mmc_host_clk_hold(host);
772 host->ios.bus_mode = mode;
774 mmc_host_clk_release(host);
778 * Change data bus width of a host.
780 void mmc_set_bus_width(struct mmc_host *host, unsigned int width)
782 mmc_host_clk_hold(host);
783 host->ios.bus_width = width;
785 mmc_host_clk_release(host);
789 * mmc_vdd_to_ocrbitnum - Convert a voltage to the OCR bit number
791 * @low_bits: prefer low bits in boundary cases
793 * This function returns the OCR bit number according to the provided @vdd
794 * value. If conversion is not possible a negative errno value returned.
796 * Depending on the @low_bits flag the function prefers low or high OCR bits
797 * on boundary voltages. For example,
798 * with @low_bits = true, 3300 mV translates to ilog2(MMC_VDD_32_33);
799 * with @low_bits = false, 3300 mV translates to ilog2(MMC_VDD_33_34);
801 * Any value in the [1951:1999] range translates to the ilog2(MMC_VDD_20_21).
803 static int mmc_vdd_to_ocrbitnum(int vdd, bool low_bits)
805 const int max_bit = ilog2(MMC_VDD_35_36);
808 if (vdd < 1650 || vdd > 3600)
811 if (vdd >= 1650 && vdd <= 1950)
812 return ilog2(MMC_VDD_165_195);
817 /* Base 2000 mV, step 100 mV, bit's base 8. */
818 bit = (vdd - 2000) / 100 + 8;
825 * mmc_vddrange_to_ocrmask - Convert a voltage range to the OCR mask
826 * @vdd_min: minimum voltage value (mV)
827 * @vdd_max: maximum voltage value (mV)
829 * This function returns the OCR mask bits according to the provided @vdd_min
830 * and @vdd_max values. If conversion is not possible the function returns 0.
832 * Notes wrt boundary cases:
833 * This function sets the OCR bits for all boundary voltages, for example
834 * [3300:3400] range is translated to MMC_VDD_32_33 | MMC_VDD_33_34 |
835 * MMC_VDD_34_35 mask.
837 u32 mmc_vddrange_to_ocrmask(int vdd_min, int vdd_max)
841 if (vdd_max < vdd_min)
844 /* Prefer high bits for the boundary vdd_max values. */
845 vdd_max = mmc_vdd_to_ocrbitnum(vdd_max, false);
849 /* Prefer low bits for the boundary vdd_min values. */
850 vdd_min = mmc_vdd_to_ocrbitnum(vdd_min, true);
854 /* Fill the mask, from max bit to min bit. */
855 while (vdd_max >= vdd_min)
856 mask |= 1 << vdd_max--;
860 EXPORT_SYMBOL(mmc_vddrange_to_ocrmask);
862 #ifdef CONFIG_REGULATOR
865 * mmc_regulator_get_ocrmask - return mask of supported voltages
866 * @supply: regulator to use
868 * This returns either a negative errno, or a mask of voltages that
869 * can be provided to MMC/SD/SDIO devices using the specified voltage
870 * regulator. This would normally be called before registering the
873 int mmc_regulator_get_ocrmask(struct regulator *supply)
879 count = regulator_count_voltages(supply);
883 for (i = 0; i < count; i++) {
887 vdd_uV = regulator_list_voltage(supply, i);
891 vdd_mV = vdd_uV / 1000;
892 result |= mmc_vddrange_to_ocrmask(vdd_mV, vdd_mV);
897 EXPORT_SYMBOL(mmc_regulator_get_ocrmask);
900 * mmc_regulator_set_ocr - set regulator to match host->ios voltage
901 * @mmc: the host to regulate
902 * @supply: regulator to use
903 * @vdd_bit: zero for power off, else a bit number (host->ios.vdd)
905 * Returns zero on success, else negative errno.
907 * MMC host drivers may use this to enable or disable a regulator using
908 * a particular supply voltage. This would normally be called from the
911 int mmc_regulator_set_ocr(struct mmc_host *mmc,
912 struct regulator *supply,
913 unsigned short vdd_bit)
922 /* REVISIT mmc_vddrange_to_ocrmask() may have set some
923 * bits this regulator doesn't quite support ... don't
924 * be too picky, most cards and regulators are OK with
925 * a 0.1V range goof (it's a small error percentage).
927 tmp = vdd_bit - ilog2(MMC_VDD_165_195);
929 min_uV = 1650 * 1000;
930 max_uV = 1950 * 1000;
932 min_uV = 1900 * 1000 + tmp * 100 * 1000;
933 max_uV = min_uV + 100 * 1000;
936 /* avoid needless changes to this voltage; the regulator
937 * might not allow this operation
939 voltage = regulator_get_voltage(supply);
942 else if (voltage < min_uV || voltage > max_uV)
943 result = regulator_set_voltage(supply, min_uV, max_uV);
947 if (result == 0 && !mmc->regulator_enabled) {
948 result = regulator_enable(supply);
950 mmc->regulator_enabled = true;
952 } else if (mmc->regulator_enabled) {
953 result = regulator_disable(supply);
955 mmc->regulator_enabled = false;
959 dev_err(mmc_dev(mmc),
960 "could not set regulator OCR (%d)\n", result);
963 EXPORT_SYMBOL(mmc_regulator_set_ocr);
965 #endif /* CONFIG_REGULATOR */
968 * Mask off any voltages we don't support and select
971 u32 mmc_select_voltage(struct mmc_host *host, u32 ocr)
975 ocr &= host->ocr_avail;
983 mmc_host_clk_hold(host);
986 mmc_host_clk_release(host);
988 pr_warning("%s: host doesn't support card's voltages\n",
996 int mmc_set_signal_voltage(struct mmc_host *host, int signal_voltage, bool cmd11)
998 struct mmc_command cmd = {0};
1004 * Send CMD11 only if the request is to switch the card to
1007 if ((signal_voltage != MMC_SIGNAL_VOLTAGE_330) && cmd11) {
1008 cmd.opcode = SD_SWITCH_VOLTAGE;
1010 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
1012 err = mmc_wait_for_cmd(host, &cmd, 0);
1016 if (!mmc_host_is_spi(host) && (cmd.resp[0] & R1_ERROR))
1020 host->ios.signal_voltage = signal_voltage;
1022 if (host->ops->start_signal_voltage_switch)
1023 err = host->ops->start_signal_voltage_switch(host, &host->ios);
1029 * Select timing parameters for host.
1031 void mmc_set_timing(struct mmc_host *host, unsigned int timing)
1033 mmc_host_clk_hold(host);
1034 host->ios.timing = timing;
1036 mmc_host_clk_release(host);
1040 * Select appropriate driver type for host.
1042 void mmc_set_driver_type(struct mmc_host *host, unsigned int drv_type)
1044 mmc_host_clk_hold(host);
1045 host->ios.drv_type = drv_type;
1047 mmc_host_clk_release(host);
1051 * Apply power to the MMC stack. This is a two-stage process.
1052 * First, we enable power to the card without the clock running.
1053 * We then wait a bit for the power to stabilise. Finally,
1054 * enable the bus drivers and clock to the card.
1056 * We must _NOT_ enable the clock prior to power stablising.
1058 * If a host does all the power sequencing itself, ignore the
1059 * initial MMC_POWER_UP stage.
1061 static void mmc_power_up(struct mmc_host *host)
1065 mmc_host_clk_hold(host);
1067 /* If ocr is set, we use it */
1069 bit = ffs(host->ocr) - 1;
1071 bit = fls(host->ocr_avail) - 1;
1073 host->ios.vdd = bit;
1074 if (mmc_host_is_spi(host)) {
1075 host->ios.chip_select = MMC_CS_HIGH;
1076 host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
1078 host->ios.chip_select = MMC_CS_DONTCARE;
1079 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
1081 host->ios.power_mode = MMC_POWER_UP;
1082 host->ios.bus_width = MMC_BUS_WIDTH_1;
1083 host->ios.timing = MMC_TIMING_LEGACY;
1087 * This delay should be sufficient to allow the power supply
1088 * to reach the minimum voltage.
1092 #if defined(CONFIG_SDMMC_RK29) || !defined(CONFIG_SDMMC_RK29_OLD) //Modifyed by xbw at 2011-11-17
1093 host->ios.clock = host->f_min;
1095 host->ios.clock = host->f_init;
1098 host->ios.power_mode = MMC_POWER_ON;
1102 * This delay must be at least 74 clock sizes, or 1 ms, or the
1103 * time required to reach a stable voltage.
1107 mmc_host_clk_release(host);
1110 void mmc_power_off(struct mmc_host *host)
1112 mmc_host_clk_hold(host);
1114 host->ios.clock = 0;
1118 * Reset ocr mask to be the highest possible voltage supported for
1119 * this mmc host. This value will be used at next power up.
1121 host->ocr = 1 << (fls(host->ocr_avail) - 1);
1123 if (!mmc_host_is_spi(host)) {
1124 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
1125 host->ios.chip_select = MMC_CS_DONTCARE;
1127 host->ios.power_mode = MMC_POWER_OFF;
1128 host->ios.bus_width = MMC_BUS_WIDTH_1;
1129 host->ios.timing = MMC_TIMING_LEGACY;
1132 mmc_host_clk_release(host);
1136 * Cleanup when the last reference to the bus operator is dropped.
1138 static void __mmc_release_bus(struct mmc_host *host)
1141 BUG_ON(host->bus_refs);
1142 BUG_ON(!host->bus_dead);
1144 host->bus_ops = NULL;
1148 * Increase reference count of bus operator
1150 static inline void mmc_bus_get(struct mmc_host *host)
1152 unsigned long flags;
1154 spin_lock_irqsave(&host->lock, flags);
1156 spin_unlock_irqrestore(&host->lock, flags);
1160 * Decrease reference count of bus operator and free it if
1161 * it is the last reference.
1163 static inline void mmc_bus_put(struct mmc_host *host)
1165 unsigned long flags;
1167 spin_lock_irqsave(&host->lock, flags);
1169 if ((host->bus_refs == 0) && host->bus_ops)
1170 __mmc_release_bus(host);
1171 spin_unlock_irqrestore(&host->lock, flags);
1174 int mmc_resume_bus(struct mmc_host *host)
1176 unsigned long flags;
1178 if (!mmc_bus_needs_resume(host))
1181 printk("%s: Starting deferred resume\n", mmc_hostname(host));
1182 spin_lock_irqsave(&host->lock, flags);
1183 host->bus_resume_flags &= ~MMC_BUSRESUME_NEEDS_RESUME;
1184 host->rescan_disable = 0;
1185 spin_unlock_irqrestore(&host->lock, flags);
1188 if (host->bus_ops && !host->bus_dead) {
1190 BUG_ON(!host->bus_ops->resume);
1191 host->bus_ops->resume(host);
1194 if (host->bus_ops->detect && !host->bus_dead)
1195 host->bus_ops->detect(host);
1198 printk("%s: Deferred resume completed\n", mmc_hostname(host));
1202 EXPORT_SYMBOL(mmc_resume_bus);
1205 * Assign a mmc bus handler to a host. Only one bus handler may control a
1206 * host at any given time.
1208 void mmc_attach_bus(struct mmc_host *host, const struct mmc_bus_ops *ops)
1210 unsigned long flags;
1215 WARN_ON(!host->claimed);
1217 spin_lock_irqsave(&host->lock, flags);
1219 BUG_ON(host->bus_ops);
1220 BUG_ON(host->bus_refs);
1222 host->bus_ops = ops;
1226 spin_unlock_irqrestore(&host->lock, flags);
1230 * Remove the current bus handler from a host.
1232 void mmc_detach_bus(struct mmc_host *host)
1234 unsigned long flags;
1238 WARN_ON(!host->claimed);
1239 WARN_ON(!host->bus_ops);
1241 spin_lock_irqsave(&host->lock, flags);
1245 spin_unlock_irqrestore(&host->lock, flags);
1251 * mmc_detect_change - process change of state on a MMC socket
1252 * @host: host which changed state.
1253 * @delay: optional delay to wait before detection (jiffies)
1255 * MMC drivers should call this when they detect a card has been
1256 * inserted or removed. The MMC layer will confirm that any
1257 * present card is still functional, and initialize any newly
1260 void mmc_detect_change(struct mmc_host *host, unsigned long delay)
1262 #ifdef CONFIG_MMC_DEBUG
1263 unsigned long flags;
1264 spin_lock_irqsave(&host->lock, flags);
1265 WARN_ON(host->removed);
1266 spin_unlock_irqrestore(&host->lock, flags);
1269 wake_lock(&host->detect_wake_lock);
1270 mmc_schedule_delayed_work(&host->detect, delay);
1273 EXPORT_SYMBOL(mmc_detect_change);
1275 void mmc_init_erase(struct mmc_card *card)
1279 if (is_power_of_2(card->erase_size))
1280 card->erase_shift = ffs(card->erase_size) - 1;
1282 card->erase_shift = 0;
1285 * It is possible to erase an arbitrarily large area of an SD or MMC
1286 * card. That is not desirable because it can take a long time
1287 * (minutes) potentially delaying more important I/O, and also the
1288 * timeout calculations become increasingly hugely over-estimated.
1289 * Consequently, 'pref_erase' is defined as a guide to limit erases
1290 * to that size and alignment.
1292 * For SD cards that define Allocation Unit size, limit erases to one
1293 * Allocation Unit at a time. For MMC cards that define High Capacity
1294 * Erase Size, whether it is switched on or not, limit to that size.
1295 * Otherwise just have a stab at a good value. For modern cards it
1296 * will end up being 4MiB. Note that if the value is too small, it
1297 * can end up taking longer to erase.
1299 if (mmc_card_sd(card) && card->ssr.au) {
1300 card->pref_erase = card->ssr.au;
1301 card->erase_shift = ffs(card->ssr.au) - 1;
1302 } else if (card->ext_csd.hc_erase_size) {
1303 card->pref_erase = card->ext_csd.hc_erase_size;
1305 sz = (card->csd.capacity << (card->csd.read_blkbits - 9)) >> 11;
1307 card->pref_erase = 512 * 1024 / 512;
1309 card->pref_erase = 1024 * 1024 / 512;
1311 card->pref_erase = 2 * 1024 * 1024 / 512;
1313 card->pref_erase = 4 * 1024 * 1024 / 512;
1314 if (card->pref_erase < card->erase_size)
1315 card->pref_erase = card->erase_size;
1317 sz = card->pref_erase % card->erase_size;
1319 card->pref_erase += card->erase_size - sz;
1324 static unsigned int mmc_mmc_erase_timeout(struct mmc_card *card,
1325 unsigned int arg, unsigned int qty)
1327 unsigned int erase_timeout;
1329 if (card->ext_csd.erase_group_def & 1) {
1330 /* High Capacity Erase Group Size uses HC timeouts */
1331 if (arg == MMC_TRIM_ARG)
1332 erase_timeout = card->ext_csd.trim_timeout;
1334 erase_timeout = card->ext_csd.hc_erase_timeout;
1336 /* CSD Erase Group Size uses write timeout */
1337 unsigned int mult = (10 << card->csd.r2w_factor);
1338 unsigned int timeout_clks = card->csd.tacc_clks * mult;
1339 unsigned int timeout_us;
1341 /* Avoid overflow: e.g. tacc_ns=80000000 mult=1280 */
1342 if (card->csd.tacc_ns < 1000000)
1343 timeout_us = (card->csd.tacc_ns * mult) / 1000;
1345 timeout_us = (card->csd.tacc_ns / 1000) * mult;
1348 * ios.clock is only a target. The real clock rate might be
1349 * less but not that much less, so fudge it by multiplying by 2.
1352 timeout_us += (timeout_clks * 1000) /
1353 (mmc_host_clk_rate(card->host) / 1000);
1355 erase_timeout = timeout_us / 1000;
1358 * Theoretically, the calculation could underflow so round up
1359 * to 1ms in that case.
1365 /* Multiplier for secure operations */
1366 if (arg & MMC_SECURE_ARGS) {
1367 if (arg == MMC_SECURE_ERASE_ARG)
1368 erase_timeout *= card->ext_csd.sec_erase_mult;
1370 erase_timeout *= card->ext_csd.sec_trim_mult;
1373 erase_timeout *= qty;
1376 * Ensure at least a 1 second timeout for SPI as per
1377 * 'mmc_set_data_timeout()'
1379 if (mmc_host_is_spi(card->host) && erase_timeout < 1000)
1380 erase_timeout = 1000;
1382 return erase_timeout;
1385 static unsigned int mmc_sd_erase_timeout(struct mmc_card *card,
1389 unsigned int erase_timeout;
1391 if (card->ssr.erase_timeout) {
1392 /* Erase timeout specified in SD Status Register (SSR) */
1393 erase_timeout = card->ssr.erase_timeout * qty +
1394 card->ssr.erase_offset;
1397 * Erase timeout not specified in SD Status Register (SSR) so
1398 * use 250ms per write block.
1400 erase_timeout = 250 * qty;
1403 /* Must not be less than 1 second */
1404 if (erase_timeout < 1000)
1405 erase_timeout = 1000;
1407 return erase_timeout;
1410 static unsigned int mmc_erase_timeout(struct mmc_card *card,
1414 if (mmc_card_sd(card))
1415 return mmc_sd_erase_timeout(card, arg, qty);
1417 return mmc_mmc_erase_timeout(card, arg, qty);
1420 static int mmc_do_erase(struct mmc_card *card, unsigned int from,
1421 unsigned int to, unsigned int arg)
1423 struct mmc_command cmd = {0};
1424 unsigned int qty = 0;
1428 * qty is used to calculate the erase timeout which depends on how many
1429 * erase groups (or allocation units in SD terminology) are affected.
1430 * We count erasing part of an erase group as one erase group.
1431 * For SD, the allocation units are always a power of 2. For MMC, the
1432 * erase group size is almost certainly also power of 2, but it does not
1433 * seem to insist on that in the JEDEC standard, so we fall back to
1434 * division in that case. SD may not specify an allocation unit size,
1435 * in which case the timeout is based on the number of write blocks.
1437 * Note that the timeout for secure trim 2 will only be correct if the
1438 * number of erase groups specified is the same as the total of all
1439 * preceding secure trim 1 commands. Since the power may have been
1440 * lost since the secure trim 1 commands occurred, it is generally
1441 * impossible to calculate the secure trim 2 timeout correctly.
1443 if (card->erase_shift)
1444 qty += ((to >> card->erase_shift) -
1445 (from >> card->erase_shift)) + 1;
1446 else if (mmc_card_sd(card))
1447 qty += to - from + 1;
1449 qty += ((to / card->erase_size) -
1450 (from / card->erase_size)) + 1;
1452 if (!mmc_card_blockaddr(card)) {
1457 if (mmc_card_sd(card))
1458 cmd.opcode = SD_ERASE_WR_BLK_START;
1460 cmd.opcode = MMC_ERASE_GROUP_START;
1462 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
1463 err = mmc_wait_for_cmd(card->host, &cmd, 0);
1465 printk(KERN_ERR "mmc_erase: group start error %d, "
1466 "status %#x\n", err, cmd.resp[0]);
1471 memset(&cmd, 0, sizeof(struct mmc_command));
1472 if (mmc_card_sd(card))
1473 cmd.opcode = SD_ERASE_WR_BLK_END;
1475 cmd.opcode = MMC_ERASE_GROUP_END;
1477 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
1478 err = mmc_wait_for_cmd(card->host, &cmd, 0);
1480 printk(KERN_ERR "mmc_erase: group end error %d, status %#x\n",
1486 memset(&cmd, 0, sizeof(struct mmc_command));
1487 cmd.opcode = MMC_ERASE;
1489 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1490 cmd.cmd_timeout_ms = mmc_erase_timeout(card, arg, qty);
1491 err = mmc_wait_for_cmd(card->host, &cmd, 0);
1493 printk(KERN_ERR "mmc_erase: erase error %d, status %#x\n",
1499 if (mmc_host_is_spi(card->host))
1503 memset(&cmd, 0, sizeof(struct mmc_command));
1504 cmd.opcode = MMC_SEND_STATUS;
1505 cmd.arg = card->rca << 16;
1506 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
1507 /* Do not retry else we can't see errors */
1508 err = mmc_wait_for_cmd(card->host, &cmd, 0);
1509 if (err || (cmd.resp[0] & 0xFDF92000)) {
1510 printk(KERN_ERR "error %d requesting status %#x\n",
1515 } while (!(cmd.resp[0] & R1_READY_FOR_DATA) ||
1516 R1_CURRENT_STATE(cmd.resp[0]) == 7);
1522 * mmc_erase - erase sectors.
1523 * @card: card to erase
1524 * @from: first sector to erase
1525 * @nr: number of sectors to erase
1526 * @arg: erase command argument (SD supports only %MMC_ERASE_ARG)
1528 * Caller must claim host before calling this function.
1530 int mmc_erase(struct mmc_card *card, unsigned int from, unsigned int nr,
1533 unsigned int rem, to = from + nr;
1535 if (!(card->host->caps & MMC_CAP_ERASE) ||
1536 !(card->csd.cmdclass & CCC_ERASE))
1539 if (!card->erase_size)
1542 if (mmc_card_sd(card) && arg != MMC_ERASE_ARG)
1545 if ((arg & MMC_SECURE_ARGS) &&
1546 !(card->ext_csd.sec_feature_support & EXT_CSD_SEC_ER_EN))
1549 if ((arg & MMC_TRIM_ARGS) &&
1550 !(card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN))
1553 if (arg == MMC_SECURE_ERASE_ARG) {
1554 if (from % card->erase_size || nr % card->erase_size)
1558 if (arg == MMC_ERASE_ARG) {
1559 rem = from % card->erase_size;
1561 rem = card->erase_size - rem;
1568 rem = nr % card->erase_size;
1581 /* 'from' and 'to' are inclusive */
1584 return mmc_do_erase(card, from, to, arg);
1586 EXPORT_SYMBOL(mmc_erase);
1588 int mmc_can_erase(struct mmc_card *card)
1590 if ((card->host->caps & MMC_CAP_ERASE) &&
1591 (card->csd.cmdclass & CCC_ERASE) && card->erase_size)
1595 EXPORT_SYMBOL(mmc_can_erase);
1597 int mmc_can_trim(struct mmc_card *card)
1599 if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN)
1603 EXPORT_SYMBOL(mmc_can_trim);
1605 int mmc_can_secure_erase_trim(struct mmc_card *card)
1607 if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_ER_EN)
1611 EXPORT_SYMBOL(mmc_can_secure_erase_trim);
1613 int mmc_erase_group_aligned(struct mmc_card *card, unsigned int from,
1616 if (!card->erase_size)
1618 if (from % card->erase_size || nr % card->erase_size)
1622 EXPORT_SYMBOL(mmc_erase_group_aligned);
1624 int mmc_set_blocklen(struct mmc_card *card, unsigned int blocklen)
1626 struct mmc_command cmd = {0};
1628 if (mmc_card_blockaddr(card) || mmc_card_ddr_mode(card))
1631 cmd.opcode = MMC_SET_BLOCKLEN;
1633 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
1634 return mmc_wait_for_cmd(card->host, &cmd, 5);
1636 EXPORT_SYMBOL(mmc_set_blocklen);
1638 static int mmc_rescan_try_freq(struct mmc_host *host, unsigned freq)
1640 host->f_init = freq;
1642 #if defined(CONFIG_SDMMC_RK29) || !defined(CONFIG_SDMMC_RK29_OLD) //Modifyed by xbw at 2011-11-17
1646 #ifdef CONFIG_MMC_DEBUG
1647 pr_info("%s: %s: trying to init card at %u Hz\n",
1648 mmc_hostname(host), __func__, host->f_init);
1653 * sdio_reset sends CMD52 to reset card. Since we do not know
1654 * if the card is being re-initialized, just send it. CMD52
1655 * should be ignored by SD/eMMC cards.
1657 #if defined(CONFIG_SDMMC_RK29) || !defined(CONFIG_SDMMC_RK29_OLD) //Modifyed by xbw at 2011-11-17
1658 //the process is default for rockchip SDK. noted by xbw at 2011-11-17
1660 /* Order's important: probe SDIO, then SD, then MMC */
1662 #if !defined(CONFIG_USE_SDMMC0_FOR_WIFI_DEVELOP_BOARD)
1663 if( strncmp( mmc_hostname(host) ,"mmc0" , strlen("mmc0")) )
1665 //sdio_reset(host);//make no sense; noteed by xbw at 2011-12-14
1668 if (!(init_ret=mmc_attach_sdio(host)))
1670 printk(KERN_INFO "%s..%d.. ===== Initialize SDIO successfully. [%s]\n",\
1671 __FUNCTION__, __LINE__, mmc_hostname(host));
1678 printk(KERN_WARNING "\n=====\n %s..%d.. ===== Initialize SDIO-card unsuccessfully!!! [%s]\n=====\n",\
1679 __FUNCTION__, __LINE__, mmc_hostname(host));
1690 //sdio_reset(host); //make no sense; noteed by xbw at 2011-12-14
1693 if (!(init_ret=mmc_attach_sdio(host)))
1695 printk(KERN_INFO "%s..%d.. ===== Initialize SDIO successfully. [%s]\n",\
1696 __FUNCTION__, __LINE__, mmc_hostname(host));
1703 printk(KERN_WARNING "\n=====\n %s..%d.. ===== Initialize SDIO-card unsuccessfully!!! [%s]\n=====\n",\
1704 __FUNCTION__, __LINE__, mmc_hostname(host));
1709 #endif // #end--#if !defined(CONFIG_USE_SDMMC0_FOR_WIFI_DEVELOP_BOARD)
1711 if (!(init_ret=mmc_attach_sd(host)))
1713 printk(KERN_INFO "%s..%d.. ===== Initialize SD-card successfully. [%s]\n",\
1714 __FUNCTION__, __LINE__, mmc_hostname(host));
1722 printk(KERN_WARNING "\n=====\n%s..%d.. ===== Initialize SD-card unsuccessfully! [%s]\n====\n",\
1723 __FUNCTION__, __LINE__, mmc_hostname(host));
1730 if (!(init_ret=mmc_attach_mmc(host)))
1732 printk(KERN_INFO "%s...%d.. ===== Initialize MMC-card successfully. [%s]\n",\
1733 __FUNCTION__, __LINE__, mmc_hostname(host));
1741 printk(KERN_WARNING "\n =====\n%s..%d.. ===== Initialize MMC-card unsuccessfully!!! [%s]\n======\n",\
1742 __FUNCTION__, __LINE__, mmc_hostname(host));
1749 mmc_power_off(host);
1752 #else // the default process in ICS.
1757 mmc_send_if_cond(host, host->ocr_avail);
1759 /* Order's important: probe SDIO, then SD, then MMC */
1760 if (!mmc_attach_sdio(host))
1762 if (!mmc_attach_sd(host))
1764 if (!mmc_attach_mmc(host))
1767 mmc_power_off(host);
1773 void mmc_rescan(struct work_struct *work)
1775 static const unsigned freqs[] = { 400000, 300000, 200000, 100000 };
1776 struct mmc_host *host =
1777 container_of(work, struct mmc_host, detect.work);
1779 bool extend_wakelock = false;
1781 if (host->rescan_disable)
1787 * if there is a _removable_ card registered, check whether it is
1790 if (host->bus_ops && host->bus_ops->detect && !host->bus_dead
1791 && !(host->caps & MMC_CAP_NONREMOVABLE))
1792 host->bus_ops->detect(host);
1794 /* If the card was removed the bus will be marked
1795 * as dead - extend the wakelock so userspace
1798 extend_wakelock = 1;
1801 * Let mmc_bus_put() free the bus/bus_ops if we've found that
1802 * the card is no longer present.
1807 /* if there still is a card present, stop here */
1808 if (host->bus_ops != NULL) {
1814 * Only we can add a new handler, so it's safe to
1815 * release the lock here.
1819 #if defined(CONFIG_SDMMC_RK29) || !defined(CONFIG_SDMMC_RK29_OLD) //Modifyed by xbw at 2011-11-17
1820 printk(KERN_INFO "\n%s...%d.. ===== mmc_rescan Begin....[%s]\n",__FILE__, __LINE__, mmc_hostname(host));
1823 if (host->ops->get_cd && host->ops->get_cd(host) == 0)
1825 #if defined(CONFIG_SDMMC_RK29) || !defined(CONFIG_SDMMC_RK29_OLD) //Modifyed by xbw at 2011-11-17
1826 printk(KERN_WARNING "\n=================\n%s..%d.. ====find no SDMMC host. [%s]\n", \
1827 __FUNCTION__, __LINE__, mmc_hostname(host));
1833 mmc_claim_host(host);
1835 #if defined(CONFIG_SDMMC_RK29) || !defined(CONFIG_SDMMC_RK29_OLD) //Modifyed by xbw at 2011-11-17
1836 if (!mmc_rescan_try_freq(host, host->f_min))
1837 extend_wakelock = true;
1840 for (i = 0; i < ARRAY_SIZE(freqs); i++) {
1841 if (!mmc_rescan_try_freq(host, max(freqs[i], host->f_min))) {
1842 extend_wakelock = true;
1845 if (freqs[i] <= host->f_min)
1850 mmc_release_host(host);
1853 if (extend_wakelock)
1854 wake_lock_timeout(&host->detect_wake_lock, HZ / 2);
1856 wake_unlock(&host->detect_wake_lock);
1857 if (host->caps & MMC_CAP_NEEDS_POLL) {
1858 wake_lock(&host->detect_wake_lock);
1859 mmc_schedule_delayed_work(&host->detect, HZ);
1863 void mmc_start_host(struct mmc_host *host)
1865 mmc_power_off(host);
1866 mmc_detect_change(host, 0);
1869 void mmc_stop_host(struct mmc_host *host)
1871 #ifdef CONFIG_MMC_DEBUG
1872 unsigned long flags;
1873 spin_lock_irqsave(&host->lock, flags);
1875 spin_unlock_irqrestore(&host->lock, flags);
1878 if (host->caps & MMC_CAP_DISABLE)
1879 cancel_delayed_work(&host->disable);
1880 if (cancel_delayed_work_sync(&host->detect))
1881 wake_unlock(&host->detect_wake_lock);
1882 mmc_flush_scheduled_work();
1884 /* clear pm flags now and let card drivers set them as needed */
1888 if (host->bus_ops && !host->bus_dead) {
1889 if (host->bus_ops->remove)
1890 host->bus_ops->remove(host);
1892 mmc_claim_host(host);
1893 mmc_detach_bus(host);
1894 mmc_power_off(host);
1895 mmc_release_host(host);
1903 mmc_power_off(host);
1906 int mmc_power_save_host(struct mmc_host *host)
1912 if (!host->bus_ops || host->bus_dead || !host->bus_ops->power_restore) {
1917 if (host->bus_ops->power_save)
1918 ret = host->bus_ops->power_save(host);
1922 mmc_power_off(host);
1926 EXPORT_SYMBOL(mmc_power_save_host);
1928 int mmc_power_restore_host(struct mmc_host *host)
1934 if (!host->bus_ops || host->bus_dead || !host->bus_ops->power_restore) {
1940 ret = host->bus_ops->power_restore(host);
1946 EXPORT_SYMBOL(mmc_power_restore_host);
1948 int mmc_card_awake(struct mmc_host *host)
1954 if (host->bus_ops && !host->bus_dead && host->bus_ops->awake)
1955 err = host->bus_ops->awake(host);
1961 EXPORT_SYMBOL(mmc_card_awake);
1963 int mmc_card_sleep(struct mmc_host *host)
1969 if (host->bus_ops && !host->bus_dead && host->bus_ops->awake)
1970 err = host->bus_ops->sleep(host);
1976 EXPORT_SYMBOL(mmc_card_sleep);
1978 int mmc_card_can_sleep(struct mmc_host *host)
1980 struct mmc_card *card = host->card;
1982 if (card && mmc_card_mmc(card) && card->ext_csd.rev >= 3)
1986 EXPORT_SYMBOL(mmc_card_can_sleep);
1991 * mmc_suspend_host - suspend a host
1994 int mmc_suspend_host(struct mmc_host *host)
1998 if (mmc_bus_needs_resume(host))
2001 if (host->caps & MMC_CAP_DISABLE)
2002 cancel_delayed_work(&host->disable);
2003 if (cancel_delayed_work(&host->detect))
2004 wake_unlock(&host->detect_wake_lock);
2005 mmc_flush_scheduled_work();
2008 if (host->bus_ops && !host->bus_dead) {
2009 if (host->bus_ops->suspend)
2010 err = host->bus_ops->suspend(host);
2011 if (err == -ENOSYS || !host->bus_ops->resume) {
2013 * We simply "remove" the card in this case.
2014 * It will be redetected on resume.
2016 if (host->bus_ops->remove)
2017 host->bus_ops->remove(host);
2018 mmc_claim_host(host);
2019 mmc_detach_bus(host);
2020 mmc_power_off(host);
2021 mmc_release_host(host);
2028 if (!err && !mmc_card_keep_power(host))
2029 mmc_power_off(host);
2034 EXPORT_SYMBOL(mmc_suspend_host);
2037 * mmc_resume_host - resume a previously suspended host
2040 int mmc_resume_host(struct mmc_host *host)
2045 if (mmc_bus_manual_resume(host)) {
2046 host->bus_resume_flags |= MMC_BUSRESUME_NEEDS_RESUME;
2051 if (host->bus_ops && !host->bus_dead) {
2052 if (!mmc_card_keep_power(host)) {
2054 mmc_select_voltage(host, host->ocr);
2056 * Tell runtime PM core we just powered up the card,
2057 * since it still believes the card is powered off.
2058 * Note that currently runtime PM is only enabled
2059 * for SDIO cards that are MMC_CAP_POWER_OFF_CARD
2061 if (mmc_card_sdio(host->card) &&
2062 (host->caps & MMC_CAP_POWER_OFF_CARD)) {
2063 pm_runtime_disable(&host->card->dev);
2064 pm_runtime_set_active(&host->card->dev);
2065 pm_runtime_enable(&host->card->dev);
2068 BUG_ON(!host->bus_ops->resume);
2069 #if defined(CONFIG_SDMMC_RK29) && !defined(CONFIG_SDMMC_RK29_OLD)
2070 //panic if the card is being removed during the resume, deleted by xbw at 2011-06-20
2071 host->bus_ops->resume(host);
2074 err = host->bus_ops->resume(host);
2076 printk(KERN_WARNING "%s: error %d during resume "
2077 "(card was removed?)\n",
2078 mmc_hostname(host), err);
2083 host->pm_flags &= ~MMC_PM_KEEP_POWER;
2088 EXPORT_SYMBOL(mmc_resume_host);
2090 /* Do the card removal on suspend if card is assumed removeable
2091 * Do that in pm notifier while userspace isn't yet frozen, so we will be able
2094 int mmc_pm_notify(struct notifier_block *notify_block,
2095 unsigned long mode, void *unused)
2097 struct mmc_host *host = container_of(
2098 notify_block, struct mmc_host, pm_notify);
2099 unsigned long flags;
2103 case PM_HIBERNATION_PREPARE:
2104 case PM_SUSPEND_PREPARE:
2106 spin_lock_irqsave(&host->lock, flags);
2107 if (mmc_bus_needs_resume(host)) {
2108 spin_unlock_irqrestore(&host->lock, flags);
2111 host->rescan_disable = 1;
2112 spin_unlock_irqrestore(&host->lock, flags);
2113 if (cancel_delayed_work_sync(&host->detect))
2114 wake_unlock(&host->detect_wake_lock);
2116 if (!host->bus_ops || host->bus_ops->suspend)
2119 mmc_claim_host(host);
2121 if (host->bus_ops->remove)
2122 host->bus_ops->remove(host);
2124 mmc_detach_bus(host);
2125 mmc_power_off(host);
2126 mmc_release_host(host);
2130 case PM_POST_SUSPEND:
2131 case PM_POST_HIBERNATION:
2132 case PM_POST_RESTORE:
2134 spin_lock_irqsave(&host->lock, flags);
2135 if (mmc_bus_manual_resume(host)) {
2136 spin_unlock_irqrestore(&host->lock, flags);
2139 host->rescan_disable = 0;
2140 spin_unlock_irqrestore(&host->lock, flags);
2141 mmc_detect_change(host, 0);
2149 #ifdef CONFIG_MMC_EMBEDDED_SDIO
2150 void mmc_set_embedded_sdio_data(struct mmc_host *host,
2151 struct sdio_cis *cis,
2152 struct sdio_cccr *cccr,
2153 struct sdio_embedded_func *funcs,
2156 host->embedded_sdio_data.cis = cis;
2157 host->embedded_sdio_data.cccr = cccr;
2158 host->embedded_sdio_data.funcs = funcs;
2159 host->embedded_sdio_data.num_funcs = num_funcs;
2162 EXPORT_SYMBOL(mmc_set_embedded_sdio_data);
2165 static int __init mmc_init(void)
2169 workqueue = alloc_ordered_workqueue("kmmcd", 0);
2173 ret = mmc_register_bus();
2175 goto destroy_workqueue;
2177 ret = mmc_register_host_class();
2179 goto unregister_bus;
2181 ret = sdio_register_bus();
2183 goto unregister_host_class;
2187 unregister_host_class:
2188 mmc_unregister_host_class();
2190 mmc_unregister_bus();
2192 destroy_workqueue(workqueue);
2197 static void __exit mmc_exit(void)
2199 sdio_unregister_bus();
2200 mmc_unregister_host_class();
2201 mmc_unregister_bus();
2202 destroy_workqueue(workqueue);
2205 subsys_initcall(mmc_init);
2206 module_exit(mmc_exit);
2208 MODULE_LICENSE("GPL");