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 module_param_named(removable
, mmc_assume_removable
, bool, 0644);
64 "MMC/SD cards are removable and may be removed during suspend");
67 * Internal function. Schedule delayed work in the MMC work queue.
69 static int mmc_schedule_delayed_work(struct delayed_work
*work
,
72 return queue_delayed_work(workqueue
, work
, delay
);
76 * Internal function. Flush all scheduled work from the MMC work queue.
78 static void mmc_flush_scheduled_work(void)
80 flush_workqueue(workqueue
);
84 * mmc_request_done - finish processing an MMC request
85 * @host: MMC host which completed request
86 * @mrq: MMC request which request
88 * MMC drivers should call this function when they have completed
89 * their processing of a request.
91 void mmc_request_done(struct mmc_host
*host
, struct mmc_request
*mrq
)
93 struct mmc_command
*cmd
= mrq
->cmd
;
96 if (err
&& cmd
->retries
&& mmc_host_is_spi(host
)) {
97 if (cmd
->resp
[0] & R1_SPI_ILLEGAL_COMMAND
)
101 if (err
&& cmd
->retries
) {
102 pr_debug("%s: req failed (CMD%u): %d, retrying...\n",
103 mmc_hostname(host
), cmd
->opcode
, err
);
107 host
->ops
->request(host
, mrq
);
109 led_trigger_event(host
->led
, LED_OFF
);
111 pr_debug("%s: req done (CMD%u): %d: %08x %08x %08x %08x\n",
112 mmc_hostname(host
), cmd
->opcode
, err
,
113 cmd
->resp
[0], cmd
->resp
[1],
114 cmd
->resp
[2], cmd
->resp
[3]);
117 pr_debug("%s: %d bytes transferred: %d\n",
119 mrq
->data
->bytes_xfered
, mrq
->data
->error
);
123 pr_debug("%s: (CMD%u): %d: %08x %08x %08x %08x\n",
124 mmc_hostname(host
), mrq
->stop
->opcode
,
126 mrq
->stop
->resp
[0], mrq
->stop
->resp
[1],
127 mrq
->stop
->resp
[2], mrq
->stop
->resp
[3]);
135 EXPORT_SYMBOL(mmc_request_done
);
138 mmc_start_request(struct mmc_host
*host
, struct mmc_request
*mrq
)
140 #ifdef CONFIG_MMC_DEBUG
142 struct scatterlist
*sg
;
145 pr_debug("%s: starting CMD%u arg %08x flags %08x\n",
146 mmc_hostname(host
), mrq
->cmd
->opcode
,
147 mrq
->cmd
->arg
, mrq
->cmd
->flags
);
150 pr_debug("%s: blksz %d blocks %d flags %08x "
151 "tsac %d ms nsac %d\n",
152 mmc_hostname(host
), mrq
->data
->blksz
,
153 mrq
->data
->blocks
, mrq
->data
->flags
,
154 mrq
->data
->timeout_ns
/ 1000000,
155 mrq
->data
->timeout_clks
);
159 pr_debug("%s: CMD%u arg %08x flags %08x\n",
160 mmc_hostname(host
), mrq
->stop
->opcode
,
161 mrq
->stop
->arg
, mrq
->stop
->flags
);
164 WARN_ON(!host
->claimed
);
166 led_trigger_event(host
->led
, LED_FULL
);
171 BUG_ON(mrq
->data
->blksz
> host
->max_blk_size
);
172 BUG_ON(mrq
->data
->blocks
> host
->max_blk_count
);
173 BUG_ON(mrq
->data
->blocks
* mrq
->data
->blksz
>
176 #ifdef CONFIG_MMC_DEBUG
178 for_each_sg(mrq
->data
->sg
, sg
, mrq
->data
->sg_len
, i
)
180 BUG_ON(sz
!= mrq
->data
->blocks
* mrq
->data
->blksz
);
183 mrq
->cmd
->data
= mrq
->data
;
184 mrq
->data
->error
= 0;
185 mrq
->data
->mrq
= mrq
;
187 mrq
->data
->stop
= mrq
->stop
;
188 mrq
->stop
->error
= 0;
189 mrq
->stop
->mrq
= mrq
;
192 host
->ops
->request(host
, mrq
);
195 static void mmc_wait_done(struct mmc_request
*mrq
)
197 complete(mrq
->done_data
);
201 * mmc_wait_for_req - start a request and wait for completion
202 * @host: MMC host to start command
203 * @mrq: MMC request to start
205 * Start a new MMC custom command request for a host, and wait
206 * for the command to complete. Does not attempt to parse the
209 void mmc_wait_for_req(struct mmc_host
*host
, struct mmc_request
*mrq
)
211 DECLARE_COMPLETION_ONSTACK(complete
);
213 mrq
->done_data
= &complete
;
214 mrq
->done
= mmc_wait_done
;
216 mmc_start_request(host
, mrq
);
218 wait_for_completion(&complete
);
221 EXPORT_SYMBOL(mmc_wait_for_req
);
224 * mmc_wait_for_cmd - start a command and wait for completion
225 * @host: MMC host to start command
226 * @cmd: MMC command to start
227 * @retries: maximum number of retries
229 * Start a new MMC command for a host, and wait for the command
230 * to complete. Return any error that occurred while the command
231 * was executing. Do not attempt to parse the response.
233 int mmc_wait_for_cmd(struct mmc_host
*host
, struct mmc_command
*cmd
, int retries
)
235 struct mmc_request mrq
;
237 WARN_ON(!host
->claimed
);
239 memset(&mrq
, 0, sizeof(struct mmc_request
));
241 memset(cmd
->resp
, 0, sizeof(cmd
->resp
));
242 cmd
->retries
= retries
;
247 mmc_wait_for_req(host
, &mrq
);
252 EXPORT_SYMBOL(mmc_wait_for_cmd
);
255 * mmc_set_data_timeout - set the timeout for a data command
256 * @data: data phase for command
257 * @card: the MMC card associated with the data transfer
259 * Computes the data timeout parameters according to the
260 * correct algorithm given the card type.
262 void mmc_set_data_timeout(struct mmc_data
*data
, const struct mmc_card
*card
)
267 * SDIO cards only define an upper 1 s limit on access.
269 if (mmc_card_sdio(card
)) {
270 data
->timeout_ns
= 1000000000;
271 data
->timeout_clks
= 0;
276 * SD cards use a 100 multiplier rather than 10
278 mult
= mmc_card_sd(card
) ? 100 : 10;
281 * Scale up the multiplier (and therefore the timeout) by
282 * the r2w factor for writes.
284 if (data
->flags
& MMC_DATA_WRITE
)
285 mult
<<= card
->csd
.r2w_factor
;
287 data
->timeout_ns
= card
->csd
.tacc_ns
* mult
;
288 data
->timeout_clks
= card
->csd
.tacc_clks
* mult
;
291 * SD cards also have an upper limit on the timeout.
293 if (mmc_card_sd(card
)) {
294 unsigned int timeout_us
, limit_us
;
296 timeout_us
= data
->timeout_ns
/ 1000;
297 timeout_us
+= data
->timeout_clks
* 1000 /
298 (card
->host
->ios
.clock
/ 1000);
300 if (data
->flags
& MMC_DATA_WRITE
)
302 * The limit is really 250 ms, but that is
303 * insufficient for some crappy cards.
310 * SDHC cards always use these fixed values.
312 if (timeout_us
> limit_us
|| mmc_card_blockaddr(card
)) {
313 data
->timeout_ns
= limit_us
* 1000;
314 data
->timeout_clks
= 0;
318 * Some cards need very high timeouts if driven in SPI mode.
319 * The worst observed timeout was 900ms after writing a
320 * continuous stream of data until the internal logic
323 if (mmc_host_is_spi(card
->host
)) {
324 if (data
->flags
& MMC_DATA_WRITE
) {
325 if (data
->timeout_ns
< 1000000000)
326 data
->timeout_ns
= 1000000000; /* 1s */
328 if (data
->timeout_ns
< 100000000)
329 data
->timeout_ns
= 100000000; /* 100ms */
333 EXPORT_SYMBOL(mmc_set_data_timeout
);
336 * mmc_align_data_size - pads a transfer size to a more optimal value
337 * @card: the MMC card associated with the data transfer
338 * @sz: original transfer size
340 * Pads the original data size with a number of extra bytes in
341 * order to avoid controller bugs and/or performance hits
342 * (e.g. some controllers revert to PIO for certain sizes).
344 * Returns the improved size, which might be unmodified.
346 * Note that this function is only relevant when issuing a
347 * single scatter gather entry.
349 unsigned int mmc_align_data_size(struct mmc_card
*card
, unsigned int sz
)
352 * FIXME: We don't have a system for the controller to tell
353 * the core about its problems yet, so for now we just 32-bit
356 sz
= ((sz
+ 3) / 4) * 4;
360 EXPORT_SYMBOL(mmc_align_data_size
);
363 * mmc_host_enable - enable a host.
364 * @host: mmc host to enable
366 * Hosts that support power saving can use the 'enable' and 'disable'
367 * methods to exit and enter power saving states. For more information
368 * see comments for struct mmc_host_ops.
370 int mmc_host_enable(struct mmc_host
*host
)
372 if (!(host
->caps
& MMC_CAP_DISABLE
))
375 if (host
->en_dis_recurs
)
378 if (host
->nesting_cnt
++)
381 cancel_delayed_work_sync(&host
->disable
);
386 if (host
->ops
->enable
) {
389 host
->en_dis_recurs
= 1;
390 err
= host
->ops
->enable(host
);
391 host
->en_dis_recurs
= 0;
394 pr_debug("%s: enable error %d\n",
395 mmc_hostname(host
), err
);
402 EXPORT_SYMBOL(mmc_host_enable
);
404 static int mmc_host_do_disable(struct mmc_host
*host
, int lazy
)
406 if (host
->ops
->disable
) {
409 host
->en_dis_recurs
= 1;
410 err
= host
->ops
->disable(host
, lazy
);
411 host
->en_dis_recurs
= 0;
414 pr_debug("%s: disable error %d\n",
415 mmc_hostname(host
), err
);
419 unsigned long delay
= msecs_to_jiffies(err
);
421 mmc_schedule_delayed_work(&host
->disable
, delay
);
429 * mmc_host_disable - disable a host.
430 * @host: mmc host to disable
432 * Hosts that support power saving can use the 'enable' and 'disable'
433 * methods to exit and enter power saving states. For more information
434 * see comments for struct mmc_host_ops.
436 int mmc_host_disable(struct mmc_host
*host
)
440 if (!(host
->caps
& MMC_CAP_DISABLE
))
443 if (host
->en_dis_recurs
)
446 if (--host
->nesting_cnt
)
452 err
= mmc_host_do_disable(host
, 0);
455 EXPORT_SYMBOL(mmc_host_disable
);
458 * __mmc_claim_host - exclusively claim a host
459 * @host: mmc host to claim
460 * @abort: whether or not the operation should be aborted
462 * Claim a host for a set of operations. If @abort is non null and
463 * dereference a non-zero value then this will return prematurely with
464 * that non-zero value without acquiring the lock. Returns zero
465 * with the lock held otherwise.
467 int __mmc_claim_host(struct mmc_host
*host
, atomic_t
*abort
)
469 DECLARE_WAITQUEUE(wait
, current
);
475 add_wait_queue(&host
->wq
, &wait
);
476 spin_lock_irqsave(&host
->lock
, flags
);
478 set_current_state(TASK_UNINTERRUPTIBLE
);
479 stop
= abort
? atomic_read(abort
) : 0;
480 if (stop
|| !host
->claimed
|| host
->claimer
== current
)
482 spin_unlock_irqrestore(&host
->lock
, flags
);
484 spin_lock_irqsave(&host
->lock
, flags
);
486 set_current_state(TASK_RUNNING
);
489 host
->claimer
= current
;
490 host
->claim_cnt
+= 1;
493 spin_unlock_irqrestore(&host
->lock
, flags
);
494 remove_wait_queue(&host
->wq
, &wait
);
496 mmc_host_enable(host
);
500 EXPORT_SYMBOL(__mmc_claim_host
);
503 * mmc_try_claim_host - try exclusively to claim a host
504 * @host: mmc host to claim
506 * Returns %1 if the host is claimed, %0 otherwise.
508 int mmc_try_claim_host(struct mmc_host
*host
)
510 int claimed_host
= 0;
513 spin_lock_irqsave(&host
->lock
, flags
);
514 if (!host
->claimed
|| host
->claimer
== current
) {
516 host
->claimer
= current
;
517 host
->claim_cnt
+= 1;
520 spin_unlock_irqrestore(&host
->lock
, flags
);
523 EXPORT_SYMBOL(mmc_try_claim_host
);
525 static void mmc_do_release_host(struct mmc_host
*host
)
529 spin_lock_irqsave(&host
->lock
, flags
);
530 if (--host
->claim_cnt
) {
531 /* Release for nested claim */
532 spin_unlock_irqrestore(&host
->lock
, flags
);
535 host
->claimer
= NULL
;
536 spin_unlock_irqrestore(&host
->lock
, flags
);
541 void mmc_host_deeper_disable(struct work_struct
*work
)
543 struct mmc_host
*host
=
544 container_of(work
, struct mmc_host
, disable
.work
);
546 /* If the host is claimed then we do not want to disable it anymore */
547 if (!mmc_try_claim_host(host
))
549 mmc_host_do_disable(host
, 1);
550 mmc_do_release_host(host
);
554 * mmc_host_lazy_disable - lazily disable a host.
555 * @host: mmc host to disable
557 * Hosts that support power saving can use the 'enable' and 'disable'
558 * methods to exit and enter power saving states. For more information
559 * see comments for struct mmc_host_ops.
561 int mmc_host_lazy_disable(struct mmc_host
*host
)
563 if (!(host
->caps
& MMC_CAP_DISABLE
))
566 if (host
->en_dis_recurs
)
569 if (--host
->nesting_cnt
)
575 if (host
->disable_delay
) {
576 mmc_schedule_delayed_work(&host
->disable
,
577 msecs_to_jiffies(host
->disable_delay
));
580 return mmc_host_do_disable(host
, 1);
582 EXPORT_SYMBOL(mmc_host_lazy_disable
);
585 * mmc_release_host - release a host
586 * @host: mmc host to release
588 * Release a MMC host, allowing others to claim the host
589 * for their operations.
591 void mmc_release_host(struct mmc_host
*host
)
593 WARN_ON(!host
->claimed
);
595 mmc_host_lazy_disable(host
);
597 mmc_do_release_host(host
);
600 EXPORT_SYMBOL(mmc_release_host
);
603 * Internal function that does the actual ios call to the host driver,
604 * optionally printing some debug output.
606 static inline void mmc_set_ios(struct mmc_host
*host
)
608 struct mmc_ios
*ios
= &host
->ios
;
610 pr_debug("%s: clock %uHz busmode %u powermode %u cs %u Vdd %u "
611 "width %u timing %u\n",
612 mmc_hostname(host
), ios
->clock
, ios
->bus_mode
,
613 ios
->power_mode
, ios
->chip_select
, ios
->vdd
,
614 ios
->bus_width
, ios
->timing
);
616 host
->ops
->set_ios(host
, ios
);
620 * Control chip select pin on a host.
622 void mmc_set_chip_select(struct mmc_host
*host
, int mode
)
624 host
->ios
.chip_select
= mode
;
629 * Sets the host clock to the highest possible frequency that
632 void mmc_set_clock(struct mmc_host
*host
, unsigned int hz
)
634 WARN_ON(hz
< host
->f_min
);
636 if (hz
> host
->f_max
)
639 host
->ios
.clock
= hz
;
644 * Change the bus mode (open drain/push-pull) of a host.
646 void mmc_set_bus_mode(struct mmc_host
*host
, unsigned int mode
)
648 host
->ios
.bus_mode
= mode
;
653 * Change data bus width of a host.
655 void mmc_set_bus_width(struct mmc_host
*host
, unsigned int width
)
657 host
->ios
.bus_width
= width
;
662 * mmc_vdd_to_ocrbitnum - Convert a voltage to the OCR bit number
664 * @low_bits: prefer low bits in boundary cases
666 * This function returns the OCR bit number according to the provided @vdd
667 * value. If conversion is not possible a negative errno value returned.
669 * Depending on the @low_bits flag the function prefers low or high OCR bits
670 * on boundary voltages. For example,
671 * with @low_bits = true, 3300 mV translates to ilog2(MMC_VDD_32_33);
672 * with @low_bits = false, 3300 mV translates to ilog2(MMC_VDD_33_34);
674 * Any value in the [1951:1999] range translates to the ilog2(MMC_VDD_20_21).
676 static int mmc_vdd_to_ocrbitnum(int vdd
, bool low_bits
)
678 const int max_bit
= ilog2(MMC_VDD_35_36
);
681 if (vdd
< 1650 || vdd
> 3600)
684 if (vdd
>= 1650 && vdd
<= 1950)
685 return ilog2(MMC_VDD_165_195
);
690 /* Base 2000 mV, step 100 mV, bit's base 8. */
691 bit
= (vdd
- 2000) / 100 + 8;
698 * mmc_vddrange_to_ocrmask - Convert a voltage range to the OCR mask
699 * @vdd_min: minimum voltage value (mV)
700 * @vdd_max: maximum voltage value (mV)
702 * This function returns the OCR mask bits according to the provided @vdd_min
703 * and @vdd_max values. If conversion is not possible the function returns 0.
705 * Notes wrt boundary cases:
706 * This function sets the OCR bits for all boundary voltages, for example
707 * [3300:3400] range is translated to MMC_VDD_32_33 | MMC_VDD_33_34 |
708 * MMC_VDD_34_35 mask.
710 u32
mmc_vddrange_to_ocrmask(int vdd_min
, int vdd_max
)
714 if (vdd_max
< vdd_min
)
717 /* Prefer high bits for the boundary vdd_max values. */
718 vdd_max
= mmc_vdd_to_ocrbitnum(vdd_max
, false);
722 /* Prefer low bits for the boundary vdd_min values. */
723 vdd_min
= mmc_vdd_to_ocrbitnum(vdd_min
, true);
727 /* Fill the mask, from max bit to min bit. */
728 while (vdd_max
>= vdd_min
)
729 mask
|= 1 << vdd_max
--;
733 EXPORT_SYMBOL(mmc_vddrange_to_ocrmask
);
735 #ifdef CONFIG_REGULATOR
738 * mmc_regulator_get_ocrmask - return mask of supported voltages
739 * @supply: regulator to use
741 * This returns either a negative errno, or a mask of voltages that
742 * can be provided to MMC/SD/SDIO devices using the specified voltage
743 * regulator. This would normally be called before registering the
746 int mmc_regulator_get_ocrmask(struct regulator
*supply
)
752 count
= regulator_count_voltages(supply
);
756 for (i
= 0; i
< count
; i
++) {
760 vdd_uV
= regulator_list_voltage(supply
, i
);
764 vdd_mV
= vdd_uV
/ 1000;
765 result
|= mmc_vddrange_to_ocrmask(vdd_mV
, vdd_mV
);
770 EXPORT_SYMBOL(mmc_regulator_get_ocrmask
);
773 * mmc_regulator_set_ocr - set regulator to match host->ios voltage
774 * @vdd_bit: zero for power off, else a bit number (host->ios.vdd)
775 * @supply: regulator to use
777 * Returns zero on success, else negative errno.
779 * MMC host drivers may use this to enable or disable a regulator using
780 * a particular supply voltage. This would normally be called from the
783 int mmc_regulator_set_ocr(struct regulator
*supply
, unsigned short vdd_bit
)
789 enabled
= regulator_is_enabled(supply
);
797 /* REVISIT mmc_vddrange_to_ocrmask() may have set some
798 * bits this regulator doesn't quite support ... don't
799 * be too picky, most cards and regulators are OK with
800 * a 0.1V range goof (it's a small error percentage).
802 tmp
= vdd_bit
- ilog2(MMC_VDD_165_195
);
804 min_uV
= 1650 * 1000;
805 max_uV
= 1950 * 1000;
807 min_uV
= 1900 * 1000 + tmp
* 100 * 1000;
808 max_uV
= min_uV
+ 100 * 1000;
811 /* avoid needless changes to this voltage; the regulator
812 * might not allow this operation
814 voltage
= regulator_get_voltage(supply
);
817 else if (voltage
< min_uV
|| voltage
> max_uV
)
818 result
= regulator_set_voltage(supply
, min_uV
, max_uV
);
822 if (result
== 0 && !enabled
)
823 result
= regulator_enable(supply
);
824 } else if (enabled
) {
825 result
= regulator_disable(supply
);
830 EXPORT_SYMBOL(mmc_regulator_set_ocr
);
835 * Mask off any voltages we don't support and select
838 u32
mmc_select_voltage(struct mmc_host
*host
, u32 ocr
)
842 ocr
&= host
->ocr_avail
;
853 pr_warning("%s: host doesn't support card's voltages\n",
862 * Select timing parameters for host.
864 void mmc_set_timing(struct mmc_host
*host
, unsigned int timing
)
866 host
->ios
.timing
= timing
;
871 * Apply power to the MMC stack. This is a two-stage process.
872 * First, we enable power to the card without the clock running.
873 * We then wait a bit for the power to stabilise. Finally,
874 * enable the bus drivers and clock to the card.
876 * We must _NOT_ enable the clock prior to power stablising.
878 * If a host does all the power sequencing itself, ignore the
879 * initial MMC_POWER_UP stage.
881 static void mmc_power_up(struct mmc_host
*host
)
885 /* If ocr is set, we use it */
887 bit
= ffs(host
->ocr
) - 1;
889 bit
= fls(host
->ocr_avail
) - 1;
892 if (mmc_host_is_spi(host
)) {
893 host
->ios
.chip_select
= MMC_CS_HIGH
;
894 host
->ios
.bus_mode
= MMC_BUSMODE_PUSHPULL
;
896 host
->ios
.chip_select
= MMC_CS_DONTCARE
;
897 host
->ios
.bus_mode
= MMC_BUSMODE_OPENDRAIN
;
899 host
->ios
.power_mode
= MMC_POWER_UP
;
900 host
->ios
.bus_width
= MMC_BUS_WIDTH_1
;
901 host
->ios
.timing
= MMC_TIMING_LEGACY
;
905 * This delay should be sufficient to allow the power supply
906 * to reach the minimum voltage.
910 if (host
->f_min
> 400000) {
911 pr_warning("%s: Minimum clock frequency too high for "
912 "identification mode\n", mmc_hostname(host
));
913 host
->ios
.clock
= host
->f_min
;
915 host
->ios
.clock
= 400000;
917 host
->ios
.power_mode
= MMC_POWER_ON
;
921 * This delay must be at least 74 clock sizes, or 1 ms, or the
922 * time required to reach a stable voltage.
927 static void mmc_power_off(struct mmc_host
*host
)
931 if (!mmc_host_is_spi(host
)) {
932 host
->ios
.bus_mode
= MMC_BUSMODE_OPENDRAIN
;
933 host
->ios
.chip_select
= MMC_CS_DONTCARE
;
935 host
->ios
.power_mode
= MMC_POWER_OFF
;
936 host
->ios
.bus_width
= MMC_BUS_WIDTH_1
;
937 host
->ios
.timing
= MMC_TIMING_LEGACY
;
942 * Cleanup when the last reference to the bus operator is dropped.
944 static void __mmc_release_bus(struct mmc_host
*host
)
947 BUG_ON(host
->bus_refs
);
948 BUG_ON(!host
->bus_dead
);
950 host
->bus_ops
= NULL
;
954 * Increase reference count of bus operator
956 static inline void mmc_bus_get(struct mmc_host
*host
)
960 spin_lock_irqsave(&host
->lock
, flags
);
962 spin_unlock_irqrestore(&host
->lock
, flags
);
966 * Decrease reference count of bus operator and free it if
967 * it is the last reference.
969 static inline void mmc_bus_put(struct mmc_host
*host
)
973 spin_lock_irqsave(&host
->lock
, flags
);
975 if ((host
->bus_refs
== 0) && host
->bus_ops
)
976 __mmc_release_bus(host
);
977 spin_unlock_irqrestore(&host
->lock
, flags
);
981 * Assign a mmc bus handler to a host. Only one bus handler may control a
982 * host at any given time.
984 void mmc_attach_bus(struct mmc_host
*host
, const struct mmc_bus_ops
*ops
)
991 WARN_ON(!host
->claimed
);
993 spin_lock_irqsave(&host
->lock
, flags
);
995 BUG_ON(host
->bus_ops
);
996 BUG_ON(host
->bus_refs
);
1002 spin_unlock_irqrestore(&host
->lock
, flags
);
1006 * Remove the current bus handler from a host. Assumes that there are
1007 * no interesting cards left, so the bus is powered down.
1009 void mmc_detach_bus(struct mmc_host
*host
)
1011 unsigned long flags
;
1015 WARN_ON(!host
->claimed
);
1016 WARN_ON(!host
->bus_ops
);
1018 spin_lock_irqsave(&host
->lock
, flags
);
1022 spin_unlock_irqrestore(&host
->lock
, flags
);
1024 mmc_power_off(host
);
1030 * mmc_detect_change - process change of state on a MMC socket
1031 * @host: host which changed state.
1032 * @delay: optional delay to wait before detection (jiffies)
1034 * MMC drivers should call this when they detect a card has been
1035 * inserted or removed. The MMC layer will confirm that any
1036 * present card is still functional, and initialize any newly
1039 void mmc_detect_change(struct mmc_host
*host
, unsigned long delay
)
1041 #ifdef CONFIG_MMC_DEBUG
1042 unsigned long flags
;
1043 spin_lock_irqsave(&host
->lock
, flags
);
1044 WARN_ON(host
->removed
);
1045 spin_unlock_irqrestore(&host
->lock
, flags
);
1048 mmc_schedule_delayed_work(&host
->detect
, delay
);
1051 EXPORT_SYMBOL(mmc_detect_change
);
1053 void mmc_init_erase(struct mmc_card
*card
)
1057 if (is_power_of_2(card
->erase_size
))
1058 card
->erase_shift
= ffs(card
->erase_size
) - 1;
1060 card
->erase_shift
= 0;
1063 * It is possible to erase an arbitrarily large area of an SD or MMC
1064 * card. That is not desirable because it can take a long time
1065 * (minutes) potentially delaying more important I/O, and also the
1066 * timeout calculations become increasingly hugely over-estimated.
1067 * Consequently, 'pref_erase' is defined as a guide to limit erases
1068 * to that size and alignment.
1070 * For SD cards that define Allocation Unit size, limit erases to one
1071 * Allocation Unit at a time. For MMC cards that define High Capacity
1072 * Erase Size, whether it is switched on or not, limit to that size.
1073 * Otherwise just have a stab at a good value. For modern cards it
1074 * will end up being 4MiB. Note that if the value is too small, it
1075 * can end up taking longer to erase.
1077 if (mmc_card_sd(card
) && card
->ssr
.au
) {
1078 card
->pref_erase
= card
->ssr
.au
;
1079 card
->erase_shift
= ffs(card
->ssr
.au
) - 1;
1080 } else if (card
->ext_csd
.hc_erase_size
) {
1081 card
->pref_erase
= card
->ext_csd
.hc_erase_size
;
1083 sz
= (card
->csd
.capacity
<< (card
->csd
.read_blkbits
- 9)) >> 11;
1085 card
->pref_erase
= 512 * 1024 / 512;
1087 card
->pref_erase
= 1024 * 1024 / 512;
1089 card
->pref_erase
= 2 * 1024 * 1024 / 512;
1091 card
->pref_erase
= 4 * 1024 * 1024 / 512;
1092 if (card
->pref_erase
< card
->erase_size
)
1093 card
->pref_erase
= card
->erase_size
;
1095 sz
= card
->pref_erase
% card
->erase_size
;
1097 card
->pref_erase
+= card
->erase_size
- sz
;
1102 static void mmc_set_mmc_erase_timeout(struct mmc_card
*card
,
1103 struct mmc_command
*cmd
,
1104 unsigned int arg
, unsigned int qty
)
1106 unsigned int erase_timeout
;
1108 if (card
->ext_csd
.erase_group_def
& 1) {
1109 /* High Capacity Erase Group Size uses HC timeouts */
1110 if (arg
== MMC_TRIM_ARG
)
1111 erase_timeout
= card
->ext_csd
.trim_timeout
;
1113 erase_timeout
= card
->ext_csd
.hc_erase_timeout
;
1115 /* CSD Erase Group Size uses write timeout */
1116 unsigned int mult
= (10 << card
->csd
.r2w_factor
);
1117 unsigned int timeout_clks
= card
->csd
.tacc_clks
* mult
;
1118 unsigned int timeout_us
;
1120 /* Avoid overflow: e.g. tacc_ns=80000000 mult=1280 */
1121 if (card
->csd
.tacc_ns
< 1000000)
1122 timeout_us
= (card
->csd
.tacc_ns
* mult
) / 1000;
1124 timeout_us
= (card
->csd
.tacc_ns
/ 1000) * mult
;
1127 * ios.clock is only a target. The real clock rate might be
1128 * less but not that much less, so fudge it by multiplying by 2.
1131 timeout_us
+= (timeout_clks
* 1000) /
1132 (card
->host
->ios
.clock
/ 1000);
1134 erase_timeout
= timeout_us
/ 1000;
1137 * Theoretically, the calculation could underflow so round up
1138 * to 1ms in that case.
1144 /* Multiplier for secure operations */
1145 if (arg
& MMC_SECURE_ARGS
) {
1146 if (arg
== MMC_SECURE_ERASE_ARG
)
1147 erase_timeout
*= card
->ext_csd
.sec_erase_mult
;
1149 erase_timeout
*= card
->ext_csd
.sec_trim_mult
;
1152 erase_timeout
*= qty
;
1155 * Ensure at least a 1 second timeout for SPI as per
1156 * 'mmc_set_data_timeout()'
1158 if (mmc_host_is_spi(card
->host
) && erase_timeout
< 1000)
1159 erase_timeout
= 1000;
1161 cmd
->erase_timeout
= erase_timeout
;
1164 static void mmc_set_sd_erase_timeout(struct mmc_card
*card
,
1165 struct mmc_command
*cmd
, unsigned int arg
,
1168 if (card
->ssr
.erase_timeout
) {
1169 /* Erase timeout specified in SD Status Register (SSR) */
1170 cmd
->erase_timeout
= card
->ssr
.erase_timeout
* qty
+
1171 card
->ssr
.erase_offset
;
1174 * Erase timeout not specified in SD Status Register (SSR) so
1175 * use 250ms per write block.
1177 cmd
->erase_timeout
= 250 * qty
;
1180 /* Must not be less than 1 second */
1181 if (cmd
->erase_timeout
< 1000)
1182 cmd
->erase_timeout
= 1000;
1185 static void mmc_set_erase_timeout(struct mmc_card
*card
,
1186 struct mmc_command
*cmd
, unsigned int arg
,
1189 if (mmc_card_sd(card
))
1190 mmc_set_sd_erase_timeout(card
, cmd
, arg
, qty
);
1192 mmc_set_mmc_erase_timeout(card
, cmd
, arg
, qty
);
1195 static int mmc_do_erase(struct mmc_card
*card
, unsigned int from
,
1196 unsigned int to
, unsigned int arg
)
1198 struct mmc_command cmd
;
1199 unsigned int qty
= 0;
1203 * qty is used to calculate the erase timeout which depends on how many
1204 * erase groups (or allocation units in SD terminology) are affected.
1205 * We count erasing part of an erase group as one erase group.
1206 * For SD, the allocation units are always a power of 2. For MMC, the
1207 * erase group size is almost certainly also power of 2, but it does not
1208 * seem to insist on that in the JEDEC standard, so we fall back to
1209 * division in that case. SD may not specify an allocation unit size,
1210 * in which case the timeout is based on the number of write blocks.
1212 * Note that the timeout for secure trim 2 will only be correct if the
1213 * number of erase groups specified is the same as the total of all
1214 * preceding secure trim 1 commands. Since the power may have been
1215 * lost since the secure trim 1 commands occurred, it is generally
1216 * impossible to calculate the secure trim 2 timeout correctly.
1218 if (card
->erase_shift
)
1219 qty
+= ((to
>> card
->erase_shift
) -
1220 (from
>> card
->erase_shift
)) + 1;
1221 else if (mmc_card_sd(card
))
1222 qty
+= to
- from
+ 1;
1224 qty
+= ((to
/ card
->erase_size
) -
1225 (from
/ card
->erase_size
)) + 1;
1227 if (!mmc_card_blockaddr(card
)) {
1232 memset(&cmd
, 0, sizeof(struct mmc_command
));
1233 if (mmc_card_sd(card
))
1234 cmd
.opcode
= SD_ERASE_WR_BLK_START
;
1236 cmd
.opcode
= MMC_ERASE_GROUP_START
;
1238 cmd
.flags
= MMC_RSP_SPI_R1
| MMC_RSP_R1
| MMC_CMD_AC
;
1239 err
= mmc_wait_for_cmd(card
->host
, &cmd
, 0);
1241 printk(KERN_ERR
"mmc_erase: group start error %d, "
1242 "status %#x\n", err
, cmd
.resp
[0]);
1247 memset(&cmd
, 0, sizeof(struct mmc_command
));
1248 if (mmc_card_sd(card
))
1249 cmd
.opcode
= SD_ERASE_WR_BLK_END
;
1251 cmd
.opcode
= MMC_ERASE_GROUP_END
;
1253 cmd
.flags
= MMC_RSP_SPI_R1
| MMC_RSP_R1
| MMC_CMD_AC
;
1254 err
= mmc_wait_for_cmd(card
->host
, &cmd
, 0);
1256 printk(KERN_ERR
"mmc_erase: group end error %d, status %#x\n",
1262 memset(&cmd
, 0, sizeof(struct mmc_command
));
1263 cmd
.opcode
= MMC_ERASE
;
1265 cmd
.flags
= MMC_RSP_SPI_R1B
| MMC_RSP_R1B
| MMC_CMD_AC
;
1266 mmc_set_erase_timeout(card
, &cmd
, arg
, qty
);
1267 err
= mmc_wait_for_cmd(card
->host
, &cmd
, 0);
1269 printk(KERN_ERR
"mmc_erase: erase error %d, status %#x\n",
1275 if (mmc_host_is_spi(card
->host
))
1279 memset(&cmd
, 0, sizeof(struct mmc_command
));
1280 cmd
.opcode
= MMC_SEND_STATUS
;
1281 cmd
.arg
= card
->rca
<< 16;
1282 cmd
.flags
= MMC_RSP_R1
| MMC_CMD_AC
;
1283 /* Do not retry else we can't see errors */
1284 err
= mmc_wait_for_cmd(card
->host
, &cmd
, 0);
1285 if (err
|| (cmd
.resp
[0] & 0xFDF92000)) {
1286 printk(KERN_ERR
"error %d requesting status %#x\n",
1291 } while (!(cmd
.resp
[0] & R1_READY_FOR_DATA
) ||
1292 R1_CURRENT_STATE(cmd
.resp
[0]) == 7);
1298 * mmc_erase - erase sectors.
1299 * @card: card to erase
1300 * @from: first sector to erase
1301 * @nr: number of sectors to erase
1302 * @arg: erase command argument (SD supports only %MMC_ERASE_ARG)
1304 * Caller must claim host before calling this function.
1306 int mmc_erase(struct mmc_card
*card
, unsigned int from
, unsigned int nr
,
1309 unsigned int rem
, to
= from
+ nr
;
1311 if (!(card
->host
->caps
& MMC_CAP_ERASE
) ||
1312 !(card
->csd
.cmdclass
& CCC_ERASE
))
1315 if (!card
->erase_size
)
1318 if (mmc_card_sd(card
) && arg
!= MMC_ERASE_ARG
)
1321 if ((arg
& MMC_SECURE_ARGS
) &&
1322 !(card
->ext_csd
.sec_feature_support
& EXT_CSD_SEC_ER_EN
))
1325 if ((arg
& MMC_TRIM_ARGS
) &&
1326 !(card
->ext_csd
.sec_feature_support
& EXT_CSD_SEC_GB_CL_EN
))
1329 if (arg
== MMC_SECURE_ERASE_ARG
) {
1330 if (from
% card
->erase_size
|| nr
% card
->erase_size
)
1334 if (arg
== MMC_ERASE_ARG
) {
1335 rem
= from
% card
->erase_size
;
1337 rem
= card
->erase_size
- rem
;
1344 rem
= nr
% card
->erase_size
;
1357 /* 'from' and 'to' are inclusive */
1360 return mmc_do_erase(card
, from
, to
, arg
);
1362 EXPORT_SYMBOL(mmc_erase
);
1364 int mmc_can_erase(struct mmc_card
*card
)
1366 if ((card
->host
->caps
& MMC_CAP_ERASE
) &&
1367 (card
->csd
.cmdclass
& CCC_ERASE
) && card
->erase_size
)
1371 EXPORT_SYMBOL(mmc_can_erase
);
1373 int mmc_can_trim(struct mmc_card
*card
)
1375 if (card
->ext_csd
.sec_feature_support
& EXT_CSD_SEC_GB_CL_EN
)
1379 EXPORT_SYMBOL(mmc_can_trim
);
1381 int mmc_can_secure_erase_trim(struct mmc_card
*card
)
1383 if (card
->ext_csd
.sec_feature_support
& EXT_CSD_SEC_ER_EN
)
1387 EXPORT_SYMBOL(mmc_can_secure_erase_trim
);
1389 int mmc_erase_group_aligned(struct mmc_card
*card
, unsigned int from
,
1392 if (!card
->erase_size
)
1394 if (from
% card
->erase_size
|| nr
% card
->erase_size
)
1398 EXPORT_SYMBOL(mmc_erase_group_aligned
);
1400 void mmc_rescan(struct work_struct
*work
)
1402 struct mmc_host
*host
=
1403 container_of(work
, struct mmc_host
, detect
.work
);
1406 unsigned long flags
;
1408 spin_lock_irqsave(&host
->lock
, flags
);
1410 if (host
->rescan_disable
) {
1411 spin_unlock_irqrestore(&host
->lock
, flags
);
1415 spin_unlock_irqrestore(&host
->lock
, flags
);
1420 /* if there is a card registered, check whether it is still present */
1421 if ((host
->bus_ops
!= NULL
) && host
->bus_ops
->detect
&& !host
->bus_dead
)
1422 host
->bus_ops
->detect(host
);
1429 /* if there still is a card present, stop here */
1430 if (host
->bus_ops
!= NULL
) {
1435 /* detect a newly inserted card */
1438 * Only we can add a new handler, so it's safe to
1439 * release the lock here.
1443 if (host
->ops
->get_cd
&& host
->ops
->get_cd(host
) == 0)
1446 mmc_claim_host(host
);
1452 mmc_send_if_cond(host
, host
->ocr_avail
);
1455 * First we search for SDIO...
1457 err
= mmc_send_io_op_cond(host
, 0, &ocr
);
1459 if (mmc_attach_sdio(host
, ocr
)) {
1460 mmc_claim_host(host
);
1461 /* try SDMEM (but not MMC) even if SDIO is broken */
1462 if (mmc_send_app_op_cond(host
, 0, &ocr
))
1465 if (mmc_attach_sd(host
, ocr
))
1466 mmc_power_off(host
);
1472 * ...then normal SD...
1474 err
= mmc_send_app_op_cond(host
, 0, &ocr
);
1476 if (mmc_attach_sd(host
, ocr
))
1477 mmc_power_off(host
);
1482 * ...and finally MMC.
1484 err
= mmc_send_op_cond(host
, 0, &ocr
);
1486 if (mmc_attach_mmc(host
, ocr
))
1487 mmc_power_off(host
);
1492 mmc_release_host(host
);
1493 mmc_power_off(host
);
1496 if (host
->caps
& MMC_CAP_NEEDS_POLL
)
1497 mmc_schedule_delayed_work(&host
->detect
, HZ
);
1500 void mmc_start_host(struct mmc_host
*host
)
1502 mmc_power_off(host
);
1503 mmc_detect_change(host
, 0);
1506 void mmc_stop_host(struct mmc_host
*host
)
1508 #ifdef CONFIG_MMC_DEBUG
1509 unsigned long flags
;
1510 spin_lock_irqsave(&host
->lock
, flags
);
1512 spin_unlock_irqrestore(&host
->lock
, flags
);
1515 if (host
->caps
& MMC_CAP_DISABLE
)
1516 cancel_delayed_work(&host
->disable
);
1517 cancel_delayed_work(&host
->detect
);
1518 mmc_flush_scheduled_work();
1520 /* clear pm flags now and let card drivers set them as needed */
1524 if (host
->bus_ops
&& !host
->bus_dead
) {
1525 if (host
->bus_ops
->remove
)
1526 host
->bus_ops
->remove(host
);
1528 mmc_claim_host(host
);
1529 mmc_detach_bus(host
);
1530 mmc_release_host(host
);
1538 mmc_power_off(host
);
1541 void mmc_power_save_host(struct mmc_host
*host
)
1545 if (!host
->bus_ops
|| host
->bus_dead
|| !host
->bus_ops
->power_restore
) {
1550 if (host
->bus_ops
->power_save
)
1551 host
->bus_ops
->power_save(host
);
1555 mmc_power_off(host
);
1557 EXPORT_SYMBOL(mmc_power_save_host
);
1559 void mmc_power_restore_host(struct mmc_host
*host
)
1563 if (!host
->bus_ops
|| host
->bus_dead
|| !host
->bus_ops
->power_restore
) {
1569 host
->bus_ops
->power_restore(host
);
1573 EXPORT_SYMBOL(mmc_power_restore_host
);
1575 int mmc_card_awake(struct mmc_host
*host
)
1581 if (host
->bus_ops
&& !host
->bus_dead
&& host
->bus_ops
->awake
)
1582 err
= host
->bus_ops
->awake(host
);
1588 EXPORT_SYMBOL(mmc_card_awake
);
1590 int mmc_card_sleep(struct mmc_host
*host
)
1596 if (host
->bus_ops
&& !host
->bus_dead
&& host
->bus_ops
->awake
)
1597 err
= host
->bus_ops
->sleep(host
);
1603 EXPORT_SYMBOL(mmc_card_sleep
);
1605 int mmc_card_can_sleep(struct mmc_host
*host
)
1607 struct mmc_card
*card
= host
->card
;
1609 if (card
&& mmc_card_mmc(card
) && card
->ext_csd
.rev
>= 3)
1613 EXPORT_SYMBOL(mmc_card_can_sleep
);
1618 * mmc_suspend_host - suspend a host
1621 int mmc_suspend_host(struct mmc_host
*host
)
1625 if (host
->caps
& MMC_CAP_DISABLE
)
1626 cancel_delayed_work(&host
->disable
);
1627 cancel_delayed_work(&host
->detect
);
1628 mmc_flush_scheduled_work();
1631 if (host
->bus_ops
&& !host
->bus_dead
) {
1632 if (host
->bus_ops
->suspend
)
1633 err
= host
->bus_ops
->suspend(host
);
1637 if (!err
&& !(host
->pm_flags
& MMC_PM_KEEP_POWER
))
1638 mmc_power_off(host
);
1643 EXPORT_SYMBOL(mmc_suspend_host
);
1646 * mmc_resume_host - resume a previously suspended host
1649 int mmc_resume_host(struct mmc_host
*host
)
1654 if (host
->bus_ops
&& !host
->bus_dead
) {
1655 if (!(host
->pm_flags
& MMC_PM_KEEP_POWER
)) {
1657 mmc_select_voltage(host
, host
->ocr
);
1659 BUG_ON(!host
->bus_ops
->resume
);
1660 err
= host
->bus_ops
->resume(host
);
1662 printk(KERN_WARNING
"%s: error %d during resume "
1663 "(card was removed?)\n",
1664 mmc_hostname(host
), err
);
1672 EXPORT_SYMBOL(mmc_resume_host
);
1674 /* Do the card removal on suspend if card is assumed removeable
1675 * Do that in pm notifier while userspace isn't yet frozen, so we will be able
1678 int mmc_pm_notify(struct notifier_block
*notify_block
,
1679 unsigned long mode
, void *unused
)
1681 struct mmc_host
*host
= container_of(
1682 notify_block
, struct mmc_host
, pm_notify
);
1683 unsigned long flags
;
1687 case PM_HIBERNATION_PREPARE
:
1688 case PM_SUSPEND_PREPARE
:
1690 spin_lock_irqsave(&host
->lock
, flags
);
1691 host
->rescan_disable
= 1;
1692 spin_unlock_irqrestore(&host
->lock
, flags
);
1693 cancel_delayed_work_sync(&host
->detect
);
1695 if (!host
->bus_ops
|| host
->bus_ops
->suspend
)
1698 mmc_claim_host(host
);
1700 if (host
->bus_ops
->remove
)
1701 host
->bus_ops
->remove(host
);
1703 mmc_detach_bus(host
);
1704 mmc_release_host(host
);
1708 case PM_POST_SUSPEND
:
1709 case PM_POST_HIBERNATION
:
1711 spin_lock_irqsave(&host
->lock
, flags
);
1712 host
->rescan_disable
= 0;
1713 spin_unlock_irqrestore(&host
->lock
, flags
);
1714 mmc_detect_change(host
, 0);
1722 static int __init
mmc_init(void)
1726 workqueue
= create_singlethread_workqueue("kmmcd");
1730 ret
= mmc_register_bus();
1732 goto destroy_workqueue
;
1734 ret
= mmc_register_host_class();
1736 goto unregister_bus
;
1738 ret
= sdio_register_bus();
1740 goto unregister_host_class
;
1744 unregister_host_class
:
1745 mmc_unregister_host_class();
1747 mmc_unregister_bus();
1749 destroy_workqueue(workqueue
);
1754 static void __exit
mmc_exit(void)
1756 sdio_unregister_bus();
1757 mmc_unregister_host_class();
1758 mmc_unregister_bus();
1759 destroy_workqueue(workqueue
);
1762 subsys_initcall(mmc_init
);
1763 module_exit(mmc_exit
);
1765 MODULE_LICENSE("GPL");