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>
26 #include <linux/mmc/card.h>
27 #include <linux/mmc/host.h>
28 #include <linux/mmc/mmc.h>
29 #include <linux/mmc/sd.h>
40 static struct workqueue_struct *workqueue;
43 * Enabling software CRCs on the data blocks can be a significant (30%)
44 * performance cost, and for other reasons may not always be desired.
45 * So we allow it it to be disabled.
48 module_param(use_spi_crc, bool, 0);
51 * We normally treat cards as removed during suspend if they are not
52 * known to be on a non-removable bus, to avoid the risk of writing
53 * back data to a different card after resume. Allow this to be
54 * overridden if necessary.
56 #ifdef CONFIG_MMC_UNSAFE_RESUME
57 int mmc_assume_removable;
59 int mmc_assume_removable = 1;
61 EXPORT_SYMBOL(mmc_assume_removable);
62 module_param_named(removable, mmc_assume_removable, bool, 0644);
65 "MMC/SD cards are removable and may be removed during suspend");
68 * Internal function. Schedule delayed work in the MMC work queue.
70 static int mmc_schedule_delayed_work(struct delayed_work *work,
73 return queue_delayed_work(workqueue, work, delay);
77 * Internal function. Flush all scheduled work from the MMC work queue.
79 static void mmc_flush_scheduled_work(void)
81 flush_workqueue(workqueue);
85 * mmc_request_done - finish processing an MMC request
86 * @host: MMC host which completed request
87 * @mrq: MMC request which request
89 * MMC drivers should call this function when they have completed
90 * their processing of a request.
92 void mmc_request_done(struct mmc_host *host, struct mmc_request *mrq)
94 struct mmc_command *cmd = mrq->cmd;
97 if (err && cmd->retries && mmc_host_is_spi(host)) {
98 if (cmd->resp[0] & R1_SPI_ILLEGAL_COMMAND)
102 if (err && cmd->retries) {
103 pr_debug("%s: req failed (CMD%u): %d, retrying...\n",
104 mmc_hostname(host), cmd->opcode, err);
108 host->ops->request(host, mrq);
110 led_trigger_event(host->led, LED_OFF);
112 pr_debug("%s: req done (CMD%u): %d: %08x %08x %08x %08x\n",
113 mmc_hostname(host), cmd->opcode, err,
114 cmd->resp[0], cmd->resp[1],
115 cmd->resp[2], cmd->resp[3]);
118 pr_debug("%s: %d bytes transferred: %d\n",
120 mrq->data->bytes_xfered, mrq->data->error);
124 pr_debug("%s: (CMD%u): %d: %08x %08x %08x %08x\n",
125 mmc_hostname(host), mrq->stop->opcode,
127 mrq->stop->resp[0], mrq->stop->resp[1],
128 mrq->stop->resp[2], mrq->stop->resp[3]);
136 EXPORT_SYMBOL(mmc_request_done);
139 mmc_start_request(struct mmc_host *host, struct mmc_request *mrq)
141 #ifdef CONFIG_MMC_DEBUG
143 struct scatterlist *sg;
146 pr_debug("%s: starting CMD%u arg %08x flags %08x\n",
147 mmc_hostname(host), mrq->cmd->opcode,
148 mrq->cmd->arg, mrq->cmd->flags);
151 pr_debug("%s: blksz %d blocks %d flags %08x "
152 "tsac %d ms nsac %d\n",
153 mmc_hostname(host), mrq->data->blksz,
154 mrq->data->blocks, mrq->data->flags,
155 mrq->data->timeout_ns / 1000000,
156 mrq->data->timeout_clks);
160 pr_debug("%s: CMD%u arg %08x flags %08x\n",
161 mmc_hostname(host), mrq->stop->opcode,
162 mrq->stop->arg, mrq->stop->flags);
165 WARN_ON(!host->claimed);
167 led_trigger_event(host->led, LED_FULL);
172 BUG_ON(mrq->data->blksz > host->max_blk_size);
173 BUG_ON(mrq->data->blocks > host->max_blk_count);
174 BUG_ON(mrq->data->blocks * mrq->data->blksz >
177 #ifdef CONFIG_MMC_DEBUG
179 for_each_sg(mrq->data->sg, sg, mrq->data->sg_len, i)
181 BUG_ON(sz != mrq->data->blocks * mrq->data->blksz);
184 mrq->cmd->data = mrq->data;
185 mrq->data->error = 0;
186 mrq->data->mrq = mrq;
188 mrq->data->stop = mrq->stop;
189 mrq->stop->error = 0;
190 mrq->stop->mrq = mrq;
193 host->ops->request(host, mrq);
196 static void mmc_wait_done(struct mmc_request *mrq)
198 complete(mrq->done_data);
202 * mmc_wait_for_req - start a request and wait for completion
203 * @host: MMC host to start command
204 * @mrq: MMC request to start
206 * Start a new MMC custom command request for a host, and wait
207 * for the command to complete. Does not attempt to parse the
210 void mmc_wait_for_req(struct mmc_host *host, struct mmc_request *mrq)
212 DECLARE_COMPLETION_ONSTACK(complete);
214 mrq->done_data = &complete;
215 mrq->done = mmc_wait_done;
217 mmc_start_request(host, mrq);
219 wait_for_completion(&complete);
222 EXPORT_SYMBOL(mmc_wait_for_req);
225 * mmc_wait_for_cmd - start a command and wait for completion
226 * @host: MMC host to start command
227 * @cmd: MMC command to start
228 * @retries: maximum number of retries
230 * Start a new MMC command for a host, and wait for the command
231 * to complete. Return any error that occurred while the command
232 * was executing. Do not attempt to parse the response.
234 int mmc_wait_for_cmd(struct mmc_host *host, struct mmc_command *cmd, int retries)
236 struct mmc_request mrq;
238 WARN_ON(!host->claimed);
240 memset(&mrq, 0, sizeof(struct mmc_request));
242 memset(cmd->resp, 0, sizeof(cmd->resp));
243 cmd->retries = retries;
248 mmc_wait_for_req(host, &mrq);
253 EXPORT_SYMBOL(mmc_wait_for_cmd);
256 * mmc_set_data_timeout - set the timeout for a data command
257 * @data: data phase for command
258 * @card: the MMC card associated with the data transfer
260 * Computes the data timeout parameters according to the
261 * correct algorithm given the card type.
263 void mmc_set_data_timeout(struct mmc_data *data, const struct mmc_card *card)
268 * SDIO cards only define an upper 1 s limit on access.
270 if (mmc_card_sdio(card)) {
271 data->timeout_ns = 1000000000;
272 data->timeout_clks = 0;
277 * SD cards use a 100 multiplier rather than 10
279 mult = mmc_card_sd(card) ? 100 : 10;
282 * Scale up the multiplier (and therefore the timeout) by
283 * the r2w factor for writes.
285 if (data->flags & MMC_DATA_WRITE)
286 mult <<= card->csd.r2w_factor;
288 data->timeout_ns = card->csd.tacc_ns * mult;
289 data->timeout_clks = card->csd.tacc_clks * mult;
292 * SD cards also have an upper limit on the timeout.
294 if (mmc_card_sd(card)) {
295 unsigned int timeout_us, limit_us;
297 timeout_us = data->timeout_ns / 1000;
298 timeout_us += data->timeout_clks * 1000 /
299 (card->host->ios.clock / 1000);
301 if (data->flags & MMC_DATA_WRITE)
303 * The limit is really 250 ms, but that is
304 * insufficient for some crappy cards.
311 * SDHC cards always use these fixed values.
313 if (timeout_us > limit_us || mmc_card_blockaddr(card)) {
314 data->timeout_ns = limit_us * 1000;
315 data->timeout_clks = 0;
319 * Some cards need very high timeouts if driven in SPI mode.
320 * The worst observed timeout was 900ms after writing a
321 * continuous stream of data until the internal logic
324 if (mmc_host_is_spi(card->host)) {
325 if (data->flags & MMC_DATA_WRITE) {
326 if (data->timeout_ns < 1000000000)
327 data->timeout_ns = 1000000000; /* 1s */
329 if (data->timeout_ns < 100000000)
330 data->timeout_ns = 100000000; /* 100ms */
334 EXPORT_SYMBOL(mmc_set_data_timeout);
337 * mmc_align_data_size - pads a transfer size to a more optimal value
338 * @card: the MMC card associated with the data transfer
339 * @sz: original transfer size
341 * Pads the original data size with a number of extra bytes in
342 * order to avoid controller bugs and/or performance hits
343 * (e.g. some controllers revert to PIO for certain sizes).
345 * Returns the improved size, which might be unmodified.
347 * Note that this function is only relevant when issuing a
348 * single scatter gather entry.
350 unsigned int mmc_align_data_size(struct mmc_card *card, unsigned int sz)
353 * FIXME: We don't have a system for the controller to tell
354 * the core about its problems yet, so for now we just 32-bit
357 sz = ((sz + 3) / 4) * 4;
361 EXPORT_SYMBOL(mmc_align_data_size);
364 * mmc_host_enable - enable a host.
365 * @host: mmc host to enable
367 * Hosts that support power saving can use the 'enable' and 'disable'
368 * methods to exit and enter power saving states. For more information
369 * see comments for struct mmc_host_ops.
371 int mmc_host_enable(struct mmc_host *host)
373 if (!(host->caps & MMC_CAP_DISABLE))
376 if (host->en_dis_recurs)
379 if (host->nesting_cnt++)
382 cancel_delayed_work_sync(&host->disable);
387 if (host->ops->enable) {
390 host->en_dis_recurs = 1;
391 err = host->ops->enable(host);
392 host->en_dis_recurs = 0;
395 pr_debug("%s: enable error %d\n",
396 mmc_hostname(host), err);
403 EXPORT_SYMBOL(mmc_host_enable);
405 static int mmc_host_do_disable(struct mmc_host *host, int lazy)
407 if (host->ops->disable) {
410 host->en_dis_recurs = 1;
411 err = host->ops->disable(host, lazy);
412 host->en_dis_recurs = 0;
415 pr_debug("%s: disable error %d\n",
416 mmc_hostname(host), err);
420 unsigned long delay = msecs_to_jiffies(err);
422 mmc_schedule_delayed_work(&host->disable, delay);
430 * mmc_host_disable - disable a host.
431 * @host: mmc host to disable
433 * Hosts that support power saving can use the 'enable' and 'disable'
434 * methods to exit and enter power saving states. For more information
435 * see comments for struct mmc_host_ops.
437 int mmc_host_disable(struct mmc_host *host)
441 if (!(host->caps & MMC_CAP_DISABLE))
444 if (host->en_dis_recurs)
447 if (--host->nesting_cnt)
453 err = mmc_host_do_disable(host, 0);
456 EXPORT_SYMBOL(mmc_host_disable);
459 * __mmc_claim_host - exclusively claim a host
460 * @host: mmc host to claim
461 * @abort: whether or not the operation should be aborted
463 * Claim a host for a set of operations. If @abort is non null and
464 * dereference a non-zero value then this will return prematurely with
465 * that non-zero value without acquiring the lock. Returns zero
466 * with the lock held otherwise.
468 int __mmc_claim_host(struct mmc_host *host, atomic_t *abort)
470 DECLARE_WAITQUEUE(wait, current);
476 add_wait_queue(&host->wq, &wait);
477 spin_lock_irqsave(&host->lock, flags);
479 set_current_state(TASK_UNINTERRUPTIBLE);
480 stop = abort ? atomic_read(abort) : 0;
481 if (stop || !host->claimed || host->claimer == current)
483 spin_unlock_irqrestore(&host->lock, flags);
485 spin_lock_irqsave(&host->lock, flags);
487 set_current_state(TASK_RUNNING);
490 host->claimer = current;
491 host->claim_cnt += 1;
494 spin_unlock_irqrestore(&host->lock, flags);
495 remove_wait_queue(&host->wq, &wait);
497 mmc_host_enable(host);
501 EXPORT_SYMBOL(__mmc_claim_host);
504 * mmc_try_claim_host - try exclusively to claim a host
505 * @host: mmc host to claim
507 * Returns %1 if the host is claimed, %0 otherwise.
509 int mmc_try_claim_host(struct mmc_host *host)
511 int claimed_host = 0;
514 spin_lock_irqsave(&host->lock, flags);
515 if (!host->claimed || host->claimer == current) {
517 host->claimer = current;
518 host->claim_cnt += 1;
521 spin_unlock_irqrestore(&host->lock, flags);
524 EXPORT_SYMBOL(mmc_try_claim_host);
526 static void mmc_do_release_host(struct mmc_host *host)
530 spin_lock_irqsave(&host->lock, flags);
531 if (--host->claim_cnt) {
532 /* Release for nested claim */
533 spin_unlock_irqrestore(&host->lock, flags);
536 host->claimer = NULL;
537 spin_unlock_irqrestore(&host->lock, flags);
542 void mmc_host_deeper_disable(struct work_struct *work)
544 struct mmc_host *host =
545 container_of(work, struct mmc_host, disable.work);
547 /* If the host is claimed then we do not want to disable it anymore */
548 if (!mmc_try_claim_host(host))
550 mmc_host_do_disable(host, 1);
551 mmc_do_release_host(host);
555 * mmc_host_lazy_disable - lazily disable a host.
556 * @host: mmc host to disable
558 * Hosts that support power saving can use the 'enable' and 'disable'
559 * methods to exit and enter power saving states. For more information
560 * see comments for struct mmc_host_ops.
562 int mmc_host_lazy_disable(struct mmc_host *host)
564 if (!(host->caps & MMC_CAP_DISABLE))
567 if (host->en_dis_recurs)
570 if (--host->nesting_cnt)
576 if (host->disable_delay) {
577 mmc_schedule_delayed_work(&host->disable,
578 msecs_to_jiffies(host->disable_delay));
581 return mmc_host_do_disable(host, 1);
583 EXPORT_SYMBOL(mmc_host_lazy_disable);
586 * mmc_release_host - release a host
587 * @host: mmc host to release
589 * Release a MMC host, allowing others to claim the host
590 * for their operations.
592 void mmc_release_host(struct mmc_host *host)
594 WARN_ON(!host->claimed);
596 mmc_host_lazy_disable(host);
598 mmc_do_release_host(host);
601 EXPORT_SYMBOL(mmc_release_host);
604 * Internal function that does the actual ios call to the host driver,
605 * optionally printing some debug output.
607 static inline void mmc_set_ios(struct mmc_host *host)
609 struct mmc_ios *ios = &host->ios;
611 pr_debug("%s: clock %uHz busmode %u powermode %u cs %u Vdd %u "
612 "width %u timing %u\n",
613 mmc_hostname(host), ios->clock, ios->bus_mode,
614 ios->power_mode, ios->chip_select, ios->vdd,
615 ios->bus_width, ios->timing);
617 host->ops->set_ios(host, ios);
621 * Control chip select pin on a host.
623 void mmc_set_chip_select(struct mmc_host *host, int mode)
625 host->ios.chip_select = mode;
630 * Sets the host clock to the highest possible frequency that
633 void mmc_set_clock(struct mmc_host *host, unsigned int hz)
635 WARN_ON(hz < host->f_min);
637 if (hz > host->f_max)
640 host->ios.clock = hz;
645 * Change the bus mode (open drain/push-pull) of a host.
647 void mmc_set_bus_mode(struct mmc_host *host, unsigned int mode)
649 host->ios.bus_mode = mode;
654 * Change data bus width of a host.
656 void mmc_set_bus_width(struct mmc_host *host, unsigned int width)
658 host->ios.bus_width = width;
663 * mmc_vdd_to_ocrbitnum - Convert a voltage to the OCR bit number
665 * @low_bits: prefer low bits in boundary cases
667 * This function returns the OCR bit number according to the provided @vdd
668 * value. If conversion is not possible a negative errno value returned.
670 * Depending on the @low_bits flag the function prefers low or high OCR bits
671 * on boundary voltages. For example,
672 * with @low_bits = true, 3300 mV translates to ilog2(MMC_VDD_32_33);
673 * with @low_bits = false, 3300 mV translates to ilog2(MMC_VDD_33_34);
675 * Any value in the [1951:1999] range translates to the ilog2(MMC_VDD_20_21).
677 static int mmc_vdd_to_ocrbitnum(int vdd, bool low_bits)
679 const int max_bit = ilog2(MMC_VDD_35_36);
682 if (vdd < 1650 || vdd > 3600)
685 if (vdd >= 1650 && vdd <= 1950)
686 return ilog2(MMC_VDD_165_195);
691 /* Base 2000 mV, step 100 mV, bit's base 8. */
692 bit = (vdd - 2000) / 100 + 8;
699 * mmc_vddrange_to_ocrmask - Convert a voltage range to the OCR mask
700 * @vdd_min: minimum voltage value (mV)
701 * @vdd_max: maximum voltage value (mV)
703 * This function returns the OCR mask bits according to the provided @vdd_min
704 * and @vdd_max values. If conversion is not possible the function returns 0.
706 * Notes wrt boundary cases:
707 * This function sets the OCR bits for all boundary voltages, for example
708 * [3300:3400] range is translated to MMC_VDD_32_33 | MMC_VDD_33_34 |
709 * MMC_VDD_34_35 mask.
711 u32 mmc_vddrange_to_ocrmask(int vdd_min, int vdd_max)
715 if (vdd_max < vdd_min)
718 /* Prefer high bits for the boundary vdd_max values. */
719 vdd_max = mmc_vdd_to_ocrbitnum(vdd_max, false);
723 /* Prefer low bits for the boundary vdd_min values. */
724 vdd_min = mmc_vdd_to_ocrbitnum(vdd_min, true);
728 /* Fill the mask, from max bit to min bit. */
729 while (vdd_max >= vdd_min)
730 mask |= 1 << vdd_max--;
734 EXPORT_SYMBOL(mmc_vddrange_to_ocrmask);
736 #ifdef CONFIG_REGULATOR
739 * mmc_regulator_get_ocrmask - return mask of supported voltages
740 * @supply: regulator to use
742 * This returns either a negative errno, or a mask of voltages that
743 * can be provided to MMC/SD/SDIO devices using the specified voltage
744 * regulator. This would normally be called before registering the
747 int mmc_regulator_get_ocrmask(struct regulator *supply)
753 count = regulator_count_voltages(supply);
757 for (i = 0; i < count; i++) {
761 vdd_uV = regulator_list_voltage(supply, i);
765 vdd_mV = vdd_uV / 1000;
766 result |= mmc_vddrange_to_ocrmask(vdd_mV, vdd_mV);
771 EXPORT_SYMBOL(mmc_regulator_get_ocrmask);
774 * mmc_regulator_set_ocr - set regulator to match host->ios voltage
775 * @vdd_bit: zero for power off, else a bit number (host->ios.vdd)
776 * @supply: regulator to use
778 * Returns zero on success, else negative errno.
780 * MMC host drivers may use this to enable or disable a regulator using
781 * a particular supply voltage. This would normally be called from the
784 int mmc_regulator_set_ocr(struct regulator *supply, unsigned short vdd_bit)
790 enabled = regulator_is_enabled(supply);
798 /* REVISIT mmc_vddrange_to_ocrmask() may have set some
799 * bits this regulator doesn't quite support ... don't
800 * be too picky, most cards and regulators are OK with
801 * a 0.1V range goof (it's a small error percentage).
803 tmp = vdd_bit - ilog2(MMC_VDD_165_195);
805 min_uV = 1650 * 1000;
806 max_uV = 1950 * 1000;
808 min_uV = 1900 * 1000 + tmp * 100 * 1000;
809 max_uV = min_uV + 100 * 1000;
812 /* avoid needless changes to this voltage; the regulator
813 * might not allow this operation
815 voltage = regulator_get_voltage(supply);
818 else if (voltage < min_uV || voltage > max_uV)
819 result = regulator_set_voltage(supply, min_uV, max_uV);
823 if (result == 0 && !enabled)
824 result = regulator_enable(supply);
825 } else if (enabled) {
826 result = regulator_disable(supply);
831 EXPORT_SYMBOL(mmc_regulator_set_ocr);
836 * Mask off any voltages we don't support and select
839 u32 mmc_select_voltage(struct mmc_host *host, u32 ocr)
843 ocr &= host->ocr_avail;
854 pr_warning("%s: host doesn't support card's voltages\n",
863 * Select timing parameters for host.
865 void mmc_set_timing(struct mmc_host *host, unsigned int timing)
867 host->ios.timing = timing;
872 * Apply power to the MMC stack. This is a two-stage process.
873 * First, we enable power to the card without the clock running.
874 * We then wait a bit for the power to stabilise. Finally,
875 * enable the bus drivers and clock to the card.
877 * We must _NOT_ enable the clock prior to power stablising.
879 * If a host does all the power sequencing itself, ignore the
880 * initial MMC_POWER_UP stage.
882 static void mmc_power_up(struct mmc_host *host)
886 /* If ocr is set, we use it */
888 bit = ffs(host->ocr) - 1;
890 bit = fls(host->ocr_avail) - 1;
893 if (mmc_host_is_spi(host)) {
894 host->ios.chip_select = MMC_CS_HIGH;
895 host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
897 host->ios.chip_select = MMC_CS_DONTCARE;
898 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
900 host->ios.power_mode = MMC_POWER_UP;
901 host->ios.bus_width = MMC_BUS_WIDTH_1;
902 host->ios.timing = MMC_TIMING_LEGACY;
906 * This delay should be sufficient to allow the power supply
907 * to reach the minimum voltage.
911 host->ios.clock = host->f_init;
913 host->ios.power_mode = MMC_POWER_ON;
917 * This delay must be at least 74 clock sizes, or 1 ms, or the
918 * time required to reach a stable voltage.
923 static void mmc_power_off(struct mmc_host *host)
927 if (!mmc_host_is_spi(host)) {
928 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
929 host->ios.chip_select = MMC_CS_DONTCARE;
931 host->ios.power_mode = MMC_POWER_OFF;
932 host->ios.bus_width = MMC_BUS_WIDTH_1;
933 host->ios.timing = MMC_TIMING_LEGACY;
938 * Cleanup when the last reference to the bus operator is dropped.
940 static void __mmc_release_bus(struct mmc_host *host)
943 BUG_ON(host->bus_refs);
944 BUG_ON(!host->bus_dead);
946 host->bus_ops = NULL;
950 * Increase reference count of bus operator
952 static inline void mmc_bus_get(struct mmc_host *host)
956 spin_lock_irqsave(&host->lock, flags);
958 spin_unlock_irqrestore(&host->lock, flags);
962 * Decrease reference count of bus operator and free it if
963 * it is the last reference.
965 static inline void mmc_bus_put(struct mmc_host *host)
969 spin_lock_irqsave(&host->lock, flags);
971 if ((host->bus_refs == 0) && host->bus_ops)
972 __mmc_release_bus(host);
973 spin_unlock_irqrestore(&host->lock, flags);
977 * Assign a mmc bus handler to a host. Only one bus handler may control a
978 * host at any given time.
980 void mmc_attach_bus(struct mmc_host *host, const struct mmc_bus_ops *ops)
987 WARN_ON(!host->claimed);
989 spin_lock_irqsave(&host->lock, flags);
991 BUG_ON(host->bus_ops);
992 BUG_ON(host->bus_refs);
998 spin_unlock_irqrestore(&host->lock, flags);
1002 * Remove the current bus handler from a host. Assumes that there are
1003 * no interesting cards left, so the bus is powered down.
1005 void mmc_detach_bus(struct mmc_host *host)
1007 unsigned long flags;
1011 WARN_ON(!host->claimed);
1012 WARN_ON(!host->bus_ops);
1014 spin_lock_irqsave(&host->lock, flags);
1018 spin_unlock_irqrestore(&host->lock, flags);
1020 mmc_power_off(host);
1026 * mmc_detect_change - process change of state on a MMC socket
1027 * @host: host which changed state.
1028 * @delay: optional delay to wait before detection (jiffies)
1030 * MMC drivers should call this when they detect a card has been
1031 * inserted or removed. The MMC layer will confirm that any
1032 * present card is still functional, and initialize any newly
1035 void mmc_detect_change(struct mmc_host *host, unsigned long delay)
1037 #ifdef CONFIG_MMC_DEBUG
1038 unsigned long flags;
1039 spin_lock_irqsave(&host->lock, flags);
1040 WARN_ON(host->removed);
1041 spin_unlock_irqrestore(&host->lock, flags);
1044 mmc_schedule_delayed_work(&host->detect, delay);
1047 EXPORT_SYMBOL(mmc_detect_change);
1049 void mmc_init_erase(struct mmc_card *card)
1053 if (is_power_of_2(card->erase_size))
1054 card->erase_shift = ffs(card->erase_size) - 1;
1056 card->erase_shift = 0;
1059 * It is possible to erase an arbitrarily large area of an SD or MMC
1060 * card. That is not desirable because it can take a long time
1061 * (minutes) potentially delaying more important I/O, and also the
1062 * timeout calculations become increasingly hugely over-estimated.
1063 * Consequently, 'pref_erase' is defined as a guide to limit erases
1064 * to that size and alignment.
1066 * For SD cards that define Allocation Unit size, limit erases to one
1067 * Allocation Unit at a time. For MMC cards that define High Capacity
1068 * Erase Size, whether it is switched on or not, limit to that size.
1069 * Otherwise just have a stab at a good value. For modern cards it
1070 * will end up being 4MiB. Note that if the value is too small, it
1071 * can end up taking longer to erase.
1073 if (mmc_card_sd(card) && card->ssr.au) {
1074 card->pref_erase = card->ssr.au;
1075 card->erase_shift = ffs(card->ssr.au) - 1;
1076 } else if (card->ext_csd.hc_erase_size) {
1077 card->pref_erase = card->ext_csd.hc_erase_size;
1079 sz = (card->csd.capacity << (card->csd.read_blkbits - 9)) >> 11;
1081 card->pref_erase = 512 * 1024 / 512;
1083 card->pref_erase = 1024 * 1024 / 512;
1085 card->pref_erase = 2 * 1024 * 1024 / 512;
1087 card->pref_erase = 4 * 1024 * 1024 / 512;
1088 if (card->pref_erase < card->erase_size)
1089 card->pref_erase = card->erase_size;
1091 sz = card->pref_erase % card->erase_size;
1093 card->pref_erase += card->erase_size - sz;
1098 static void mmc_set_mmc_erase_timeout(struct mmc_card *card,
1099 struct mmc_command *cmd,
1100 unsigned int arg, unsigned int qty)
1102 unsigned int erase_timeout;
1104 if (card->ext_csd.erase_group_def & 1) {
1105 /* High Capacity Erase Group Size uses HC timeouts */
1106 if (arg == MMC_TRIM_ARG)
1107 erase_timeout = card->ext_csd.trim_timeout;
1109 erase_timeout = card->ext_csd.hc_erase_timeout;
1111 /* CSD Erase Group Size uses write timeout */
1112 unsigned int mult = (10 << card->csd.r2w_factor);
1113 unsigned int timeout_clks = card->csd.tacc_clks * mult;
1114 unsigned int timeout_us;
1116 /* Avoid overflow: e.g. tacc_ns=80000000 mult=1280 */
1117 if (card->csd.tacc_ns < 1000000)
1118 timeout_us = (card->csd.tacc_ns * mult) / 1000;
1120 timeout_us = (card->csd.tacc_ns / 1000) * mult;
1123 * ios.clock is only a target. The real clock rate might be
1124 * less but not that much less, so fudge it by multiplying by 2.
1127 timeout_us += (timeout_clks * 1000) /
1128 (card->host->ios.clock / 1000);
1130 erase_timeout = timeout_us / 1000;
1133 * Theoretically, the calculation could underflow so round up
1134 * to 1ms in that case.
1140 /* Multiplier for secure operations */
1141 if (arg & MMC_SECURE_ARGS) {
1142 if (arg == MMC_SECURE_ERASE_ARG)
1143 erase_timeout *= card->ext_csd.sec_erase_mult;
1145 erase_timeout *= card->ext_csd.sec_trim_mult;
1148 erase_timeout *= qty;
1151 * Ensure at least a 1 second timeout for SPI as per
1152 * 'mmc_set_data_timeout()'
1154 if (mmc_host_is_spi(card->host) && erase_timeout < 1000)
1155 erase_timeout = 1000;
1157 cmd->erase_timeout = erase_timeout;
1160 static void mmc_set_sd_erase_timeout(struct mmc_card *card,
1161 struct mmc_command *cmd, unsigned int arg,
1164 if (card->ssr.erase_timeout) {
1165 /* Erase timeout specified in SD Status Register (SSR) */
1166 cmd->erase_timeout = card->ssr.erase_timeout * qty +
1167 card->ssr.erase_offset;
1170 * Erase timeout not specified in SD Status Register (SSR) so
1171 * use 250ms per write block.
1173 cmd->erase_timeout = 250 * qty;
1176 /* Must not be less than 1 second */
1177 if (cmd->erase_timeout < 1000)
1178 cmd->erase_timeout = 1000;
1181 static void mmc_set_erase_timeout(struct mmc_card *card,
1182 struct mmc_command *cmd, unsigned int arg,
1185 if (mmc_card_sd(card))
1186 mmc_set_sd_erase_timeout(card, cmd, arg, qty);
1188 mmc_set_mmc_erase_timeout(card, cmd, arg, qty);
1191 static int mmc_do_erase(struct mmc_card *card, unsigned int from,
1192 unsigned int to, unsigned int arg)
1194 struct mmc_command cmd;
1195 unsigned int qty = 0;
1199 * qty is used to calculate the erase timeout which depends on how many
1200 * erase groups (or allocation units in SD terminology) are affected.
1201 * We count erasing part of an erase group as one erase group.
1202 * For SD, the allocation units are always a power of 2. For MMC, the
1203 * erase group size is almost certainly also power of 2, but it does not
1204 * seem to insist on that in the JEDEC standard, so we fall back to
1205 * division in that case. SD may not specify an allocation unit size,
1206 * in which case the timeout is based on the number of write blocks.
1208 * Note that the timeout for secure trim 2 will only be correct if the
1209 * number of erase groups specified is the same as the total of all
1210 * preceding secure trim 1 commands. Since the power may have been
1211 * lost since the secure trim 1 commands occurred, it is generally
1212 * impossible to calculate the secure trim 2 timeout correctly.
1214 if (card->erase_shift)
1215 qty += ((to >> card->erase_shift) -
1216 (from >> card->erase_shift)) + 1;
1217 else if (mmc_card_sd(card))
1218 qty += to - from + 1;
1220 qty += ((to / card->erase_size) -
1221 (from / card->erase_size)) + 1;
1223 if (!mmc_card_blockaddr(card)) {
1228 memset(&cmd, 0, sizeof(struct mmc_command));
1229 if (mmc_card_sd(card))
1230 cmd.opcode = SD_ERASE_WR_BLK_START;
1232 cmd.opcode = MMC_ERASE_GROUP_START;
1234 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
1235 err = mmc_wait_for_cmd(card->host, &cmd, 0);
1237 printk(KERN_ERR "mmc_erase: group start error %d, "
1238 "status %#x\n", err, cmd.resp[0]);
1243 memset(&cmd, 0, sizeof(struct mmc_command));
1244 if (mmc_card_sd(card))
1245 cmd.opcode = SD_ERASE_WR_BLK_END;
1247 cmd.opcode = MMC_ERASE_GROUP_END;
1249 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
1250 err = mmc_wait_for_cmd(card->host, &cmd, 0);
1252 printk(KERN_ERR "mmc_erase: group end error %d, status %#x\n",
1258 memset(&cmd, 0, sizeof(struct mmc_command));
1259 cmd.opcode = MMC_ERASE;
1261 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1262 mmc_set_erase_timeout(card, &cmd, arg, qty);
1263 err = mmc_wait_for_cmd(card->host, &cmd, 0);
1265 printk(KERN_ERR "mmc_erase: erase error %d, status %#x\n",
1271 if (mmc_host_is_spi(card->host))
1275 memset(&cmd, 0, sizeof(struct mmc_command));
1276 cmd.opcode = MMC_SEND_STATUS;
1277 cmd.arg = card->rca << 16;
1278 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
1279 /* Do not retry else we can't see errors */
1280 err = mmc_wait_for_cmd(card->host, &cmd, 0);
1281 if (err || (cmd.resp[0] & 0xFDF92000)) {
1282 printk(KERN_ERR "error %d requesting status %#x\n",
1287 } while (!(cmd.resp[0] & R1_READY_FOR_DATA) ||
1288 R1_CURRENT_STATE(cmd.resp[0]) == 7);
1294 * mmc_erase - erase sectors.
1295 * @card: card to erase
1296 * @from: first sector to erase
1297 * @nr: number of sectors to erase
1298 * @arg: erase command argument (SD supports only %MMC_ERASE_ARG)
1300 * Caller must claim host before calling this function.
1302 int mmc_erase(struct mmc_card *card, unsigned int from, unsigned int nr,
1305 unsigned int rem, to = from + nr;
1307 if (!(card->host->caps & MMC_CAP_ERASE) ||
1308 !(card->csd.cmdclass & CCC_ERASE))
1311 if (!card->erase_size)
1314 if (mmc_card_sd(card) && arg != MMC_ERASE_ARG)
1317 if ((arg & MMC_SECURE_ARGS) &&
1318 !(card->ext_csd.sec_feature_support & EXT_CSD_SEC_ER_EN))
1321 if ((arg & MMC_TRIM_ARGS) &&
1322 !(card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN))
1325 if (arg == MMC_SECURE_ERASE_ARG) {
1326 if (from % card->erase_size || nr % card->erase_size)
1330 if (arg == MMC_ERASE_ARG) {
1331 rem = from % card->erase_size;
1333 rem = card->erase_size - rem;
1340 rem = nr % card->erase_size;
1353 /* 'from' and 'to' are inclusive */
1356 return mmc_do_erase(card, from, to, arg);
1358 EXPORT_SYMBOL(mmc_erase);
1360 int mmc_can_erase(struct mmc_card *card)
1362 if ((card->host->caps & MMC_CAP_ERASE) &&
1363 (card->csd.cmdclass & CCC_ERASE) && card->erase_size)
1367 EXPORT_SYMBOL(mmc_can_erase);
1369 int mmc_can_trim(struct mmc_card *card)
1371 if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN)
1375 EXPORT_SYMBOL(mmc_can_trim);
1377 int mmc_can_secure_erase_trim(struct mmc_card *card)
1379 if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_ER_EN)
1383 EXPORT_SYMBOL(mmc_can_secure_erase_trim);
1385 int mmc_erase_group_aligned(struct mmc_card *card, unsigned int from,
1388 if (!card->erase_size)
1390 if (from % card->erase_size || nr % card->erase_size)
1394 EXPORT_SYMBOL(mmc_erase_group_aligned);
1396 void mmc_rescan(struct work_struct *work)
1398 struct mmc_host *host =
1399 container_of(work, struct mmc_host, detect.work);
1402 unsigned long flags;
1404 const unsigned freqs[] = { 400000, 300000, 200000, 100000 };
1406 spin_lock_irqsave(&host->lock, flags);
1408 if (host->rescan_disable) {
1409 spin_unlock_irqrestore(&host->lock, flags);
1413 spin_unlock_irqrestore(&host->lock, flags);
1418 /* if there is a card registered, check whether it is still present */
1419 if ((host->bus_ops != NULL) && host->bus_ops->detect && !host->bus_dead)
1420 host->bus_ops->detect(host);
1427 /* if there still is a card present, stop here */
1428 if (host->bus_ops != NULL) {
1433 /* detect a newly inserted card */
1436 * Only we can add a new handler, so it's safe to
1437 * release the lock here.
1441 if (host->ops->get_cd && host->ops->get_cd(host) == 0)
1444 for (i = 0; i < ARRAY_SIZE(freqs); i++) {
1445 mmc_claim_host(host);
1447 if (freqs[i] >= host->f_min)
1448 host->f_init = freqs[i];
1449 else if (!i || freqs[i-1] > host->f_min)
1450 host->f_init = host->f_min;
1452 mmc_release_host(host);
1455 #ifdef CONFIG_MMC_DEBUG
1456 pr_info("%s: %s: trying to init card at %u Hz\n",
1457 mmc_hostname(host), __func__, host->f_init);
1463 mmc_send_if_cond(host, host->ocr_avail);
1466 * First we search for SDIO...
1468 err = mmc_send_io_op_cond(host, 0, &ocr);
1470 if (mmc_attach_sdio(host, ocr)) {
1471 mmc_claim_host(host);
1473 * Try SDMEM (but not MMC) even if SDIO
1476 if (mmc_send_app_op_cond(host, 0, &ocr))
1479 if (mmc_attach_sd(host, ocr))
1480 mmc_power_off(host);
1486 * ...then normal SD...
1488 err = mmc_send_app_op_cond(host, 0, &ocr);
1490 if (mmc_attach_sd(host, ocr))
1491 mmc_power_off(host);
1496 * ...and finally MMC.
1498 err = mmc_send_op_cond(host, 0, &ocr);
1500 if (mmc_attach_mmc(host, ocr))
1501 mmc_power_off(host);
1506 mmc_release_host(host);
1507 mmc_power_off(host);
1510 if (host->caps & MMC_CAP_NEEDS_POLL)
1511 mmc_schedule_delayed_work(&host->detect, HZ);
1514 void mmc_start_host(struct mmc_host *host)
1516 mmc_power_off(host);
1517 mmc_detect_change(host, 0);
1520 void mmc_stop_host(struct mmc_host *host)
1522 #ifdef CONFIG_MMC_DEBUG
1523 unsigned long flags;
1524 spin_lock_irqsave(&host->lock, flags);
1526 spin_unlock_irqrestore(&host->lock, flags);
1529 if (host->caps & MMC_CAP_DISABLE)
1530 cancel_delayed_work(&host->disable);
1531 cancel_delayed_work(&host->detect);
1532 mmc_flush_scheduled_work();
1534 /* clear pm flags now and let card drivers set them as needed */
1538 if (host->bus_ops && !host->bus_dead) {
1539 if (host->bus_ops->remove)
1540 host->bus_ops->remove(host);
1542 mmc_claim_host(host);
1543 mmc_detach_bus(host);
1544 mmc_release_host(host);
1552 mmc_power_off(host);
1555 void mmc_power_save_host(struct mmc_host *host)
1559 if (!host->bus_ops || host->bus_dead || !host->bus_ops->power_restore) {
1564 if (host->bus_ops->power_save)
1565 host->bus_ops->power_save(host);
1569 mmc_power_off(host);
1571 EXPORT_SYMBOL(mmc_power_save_host);
1573 void mmc_power_restore_host(struct mmc_host *host)
1577 if (!host->bus_ops || host->bus_dead || !host->bus_ops->power_restore) {
1583 host->bus_ops->power_restore(host);
1587 EXPORT_SYMBOL(mmc_power_restore_host);
1589 int mmc_card_awake(struct mmc_host *host)
1595 if (host->bus_ops && !host->bus_dead && host->bus_ops->awake)
1596 err = host->bus_ops->awake(host);
1602 EXPORT_SYMBOL(mmc_card_awake);
1604 int mmc_card_sleep(struct mmc_host *host)
1610 if (host->bus_ops && !host->bus_dead && host->bus_ops->awake)
1611 err = host->bus_ops->sleep(host);
1617 EXPORT_SYMBOL(mmc_card_sleep);
1619 int mmc_card_can_sleep(struct mmc_host *host)
1621 struct mmc_card *card = host->card;
1623 if (card && mmc_card_mmc(card) && card->ext_csd.rev >= 3)
1627 EXPORT_SYMBOL(mmc_card_can_sleep);
1632 * mmc_suspend_host - suspend a host
1635 int mmc_suspend_host(struct mmc_host *host)
1639 if (host->caps & MMC_CAP_DISABLE)
1640 cancel_delayed_work(&host->disable);
1641 cancel_delayed_work(&host->detect);
1642 mmc_flush_scheduled_work();
1645 if (host->bus_ops && !host->bus_dead) {
1646 if (host->bus_ops->suspend)
1647 err = host->bus_ops->suspend(host);
1648 if (err == -ENOSYS || !host->bus_ops->resume) {
1650 * We simply "remove" the card in this case.
1651 * It will be redetected on resume.
1653 if (host->bus_ops->remove)
1654 host->bus_ops->remove(host);
1655 mmc_claim_host(host);
1656 mmc_detach_bus(host);
1657 mmc_release_host(host);
1664 if (!err && !(host->pm_flags & MMC_PM_KEEP_POWER))
1665 mmc_power_off(host);
1670 EXPORT_SYMBOL(mmc_suspend_host);
1673 * mmc_resume_host - resume a previously suspended host
1676 int mmc_resume_host(struct mmc_host *host)
1681 if (host->bus_ops && !host->bus_dead) {
1682 if (!(host->pm_flags & MMC_PM_KEEP_POWER)) {
1684 mmc_select_voltage(host, host->ocr);
1686 BUG_ON(!host->bus_ops->resume);
1687 err = host->bus_ops->resume(host);
1689 printk(KERN_WARNING "%s: error %d during resume "
1690 "(card was removed?)\n",
1691 mmc_hostname(host), err);
1699 EXPORT_SYMBOL(mmc_resume_host);
1701 /* Do the card removal on suspend if card is assumed removeable
1702 * Do that in pm notifier while userspace isn't yet frozen, so we will be able
1705 int mmc_pm_notify(struct notifier_block *notify_block,
1706 unsigned long mode, void *unused)
1708 struct mmc_host *host = container_of(
1709 notify_block, struct mmc_host, pm_notify);
1710 unsigned long flags;
1714 case PM_HIBERNATION_PREPARE:
1715 case PM_SUSPEND_PREPARE:
1717 spin_lock_irqsave(&host->lock, flags);
1718 host->rescan_disable = 1;
1719 spin_unlock_irqrestore(&host->lock, flags);
1720 cancel_delayed_work_sync(&host->detect);
1722 if (!host->bus_ops || host->bus_ops->suspend)
1725 mmc_claim_host(host);
1727 if (host->bus_ops->remove)
1728 host->bus_ops->remove(host);
1730 mmc_detach_bus(host);
1731 mmc_release_host(host);
1735 case PM_POST_SUSPEND:
1736 case PM_POST_HIBERNATION:
1738 spin_lock_irqsave(&host->lock, flags);
1739 host->rescan_disable = 0;
1740 spin_unlock_irqrestore(&host->lock, flags);
1741 mmc_detect_change(host, 0);
1749 static int __init mmc_init(void)
1753 workqueue = create_singlethread_workqueue("kmmcd");
1757 ret = mmc_register_bus();
1759 goto destroy_workqueue;
1761 ret = mmc_register_host_class();
1763 goto unregister_bus;
1765 ret = sdio_register_bus();
1767 goto unregister_host_class;
1771 unregister_host_class:
1772 mmc_unregister_host_class();
1774 mmc_unregister_bus();
1776 destroy_workqueue(workqueue);
1781 static void __exit mmc_exit(void)
1783 sdio_unregister_bus();
1784 mmc_unregister_host_class();
1785 mmc_unregister_bus();
1786 destroy_workqueue(workqueue);
1789 subsys_initcall(mmc_init);
1790 module_exit(mmc_exit);
1792 MODULE_LICENSE("GPL");