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>
25 #include <linux/mmc/card.h>
26 #include <linux/mmc/host.h>
27 #include <linux/mmc/mmc.h>
28 #include <linux/mmc/sd.h>
39 static struct workqueue_struct
*workqueue
;
42 * Enabling software CRCs on the data blocks can be a significant (30%)
43 * performance cost, and for other reasons may not always be desired.
44 * So we allow it it to be disabled.
47 module_param(use_spi_crc
, bool, 0);
50 * Internal function. Schedule delayed work in the MMC work queue.
52 static int mmc_schedule_delayed_work(struct delayed_work
*work
,
55 return queue_delayed_work(workqueue
, work
, delay
);
59 * Internal function. Flush all scheduled work from the MMC work queue.
61 static void mmc_flush_scheduled_work(void)
63 flush_workqueue(workqueue
);
67 * mmc_request_done - finish processing an MMC request
68 * @host: MMC host which completed request
69 * @mrq: MMC request which request
71 * MMC drivers should call this function when they have completed
72 * their processing of a request.
74 void mmc_request_done(struct mmc_host
*host
, struct mmc_request
*mrq
)
76 struct mmc_command
*cmd
= mrq
->cmd
;
79 if (err
&& cmd
->retries
&& mmc_host_is_spi(host
)) {
80 if (cmd
->resp
[0] & R1_SPI_ILLEGAL_COMMAND
)
84 if (err
&& cmd
->retries
) {
85 pr_debug("%s: req failed (CMD%u): %d, retrying...\n",
86 mmc_hostname(host
), cmd
->opcode
, err
);
90 host
->ops
->request(host
, mrq
);
92 led_trigger_event(host
->led
, LED_OFF
);
94 pr_debug("%s: req done (CMD%u): %d: %08x %08x %08x %08x\n",
95 mmc_hostname(host
), cmd
->opcode
, err
,
96 cmd
->resp
[0], cmd
->resp
[1],
97 cmd
->resp
[2], cmd
->resp
[3]);
100 pr_debug("%s: %d bytes transferred: %d\n",
102 mrq
->data
->bytes_xfered
, mrq
->data
->error
);
106 pr_debug("%s: (CMD%u): %d: %08x %08x %08x %08x\n",
107 mmc_hostname(host
), mrq
->stop
->opcode
,
109 mrq
->stop
->resp
[0], mrq
->stop
->resp
[1],
110 mrq
->stop
->resp
[2], mrq
->stop
->resp
[3]);
118 EXPORT_SYMBOL(mmc_request_done
);
121 mmc_start_request(struct mmc_host
*host
, struct mmc_request
*mrq
)
123 #ifdef CONFIG_MMC_DEBUG
125 struct scatterlist
*sg
;
128 pr_debug("%s: starting CMD%u arg %08x flags %08x\n",
129 mmc_hostname(host
), mrq
->cmd
->opcode
,
130 mrq
->cmd
->arg
, mrq
->cmd
->flags
);
133 pr_debug("%s: blksz %d blocks %d flags %08x "
134 "tsac %d ms nsac %d\n",
135 mmc_hostname(host
), mrq
->data
->blksz
,
136 mrq
->data
->blocks
, mrq
->data
->flags
,
137 mrq
->data
->timeout_ns
/ 1000000,
138 mrq
->data
->timeout_clks
);
142 pr_debug("%s: CMD%u arg %08x flags %08x\n",
143 mmc_hostname(host
), mrq
->stop
->opcode
,
144 mrq
->stop
->arg
, mrq
->stop
->flags
);
147 WARN_ON(!host
->claimed
);
149 led_trigger_event(host
->led
, LED_FULL
);
154 BUG_ON(mrq
->data
->blksz
> host
->max_blk_size
);
155 BUG_ON(mrq
->data
->blocks
> host
->max_blk_count
);
156 BUG_ON(mrq
->data
->blocks
* mrq
->data
->blksz
>
159 #ifdef CONFIG_MMC_DEBUG
161 for_each_sg(mrq
->data
->sg
, sg
, mrq
->data
->sg_len
, i
)
163 BUG_ON(sz
!= mrq
->data
->blocks
* mrq
->data
->blksz
);
166 mrq
->cmd
->data
= mrq
->data
;
167 mrq
->data
->error
= 0;
168 mrq
->data
->mrq
= mrq
;
170 mrq
->data
->stop
= mrq
->stop
;
171 mrq
->stop
->error
= 0;
172 mrq
->stop
->mrq
= mrq
;
175 host
->ops
->request(host
, mrq
);
178 static void mmc_wait_done(struct mmc_request
*mrq
)
180 complete(mrq
->done_data
);
184 * mmc_wait_for_req - start a request and wait for completion
185 * @host: MMC host to start command
186 * @mrq: MMC request to start
188 * Start a new MMC custom command request for a host, and wait
189 * for the command to complete. Does not attempt to parse the
192 void mmc_wait_for_req(struct mmc_host
*host
, struct mmc_request
*mrq
)
194 DECLARE_COMPLETION_ONSTACK(complete
);
196 mrq
->done_data
= &complete
;
197 mrq
->done
= mmc_wait_done
;
199 mmc_start_request(host
, mrq
);
201 wait_for_completion(&complete
);
204 EXPORT_SYMBOL(mmc_wait_for_req
);
207 * mmc_wait_for_cmd - start a command and wait for completion
208 * @host: MMC host to start command
209 * @cmd: MMC command to start
210 * @retries: maximum number of retries
212 * Start a new MMC command for a host, and wait for the command
213 * to complete. Return any error that occurred while the command
214 * was executing. Do not attempt to parse the response.
216 int mmc_wait_for_cmd(struct mmc_host
*host
, struct mmc_command
*cmd
, int retries
)
218 struct mmc_request mrq
;
220 WARN_ON(!host
->claimed
);
222 memset(&mrq
, 0, sizeof(struct mmc_request
));
224 memset(cmd
->resp
, 0, sizeof(cmd
->resp
));
225 cmd
->retries
= retries
;
230 mmc_wait_for_req(host
, &mrq
);
235 EXPORT_SYMBOL(mmc_wait_for_cmd
);
238 * mmc_set_data_timeout - set the timeout for a data command
239 * @data: data phase for command
240 * @card: the MMC card associated with the data transfer
242 * Computes the data timeout parameters according to the
243 * correct algorithm given the card type.
245 void mmc_set_data_timeout(struct mmc_data
*data
, const struct mmc_card
*card
)
250 * SDIO cards only define an upper 1 s limit on access.
252 if (mmc_card_sdio(card
)) {
253 data
->timeout_ns
= 1000000000;
254 data
->timeout_clks
= 0;
259 * SD cards use a 100 multiplier rather than 10
261 mult
= mmc_card_sd(card
) ? 100 : 10;
264 * Scale up the multiplier (and therefore the timeout) by
265 * the r2w factor for writes.
267 if (data
->flags
& MMC_DATA_WRITE
)
268 mult
<<= card
->csd
.r2w_factor
;
270 data
->timeout_ns
= card
->csd
.tacc_ns
* mult
;
271 data
->timeout_clks
= card
->csd
.tacc_clks
* mult
;
274 * SD cards also have an upper limit on the timeout.
276 if (mmc_card_sd(card
)) {
277 unsigned int timeout_us
, limit_us
;
279 timeout_us
= data
->timeout_ns
/ 1000;
280 timeout_us
+= data
->timeout_clks
* 1000 /
281 (card
->host
->ios
.clock
/ 1000);
283 if (data
->flags
& MMC_DATA_WRITE
)
285 * The limit is really 250 ms, but that is
286 * insufficient for some crappy cards.
293 * SDHC cards always use these fixed values.
295 if (timeout_us
> limit_us
|| mmc_card_blockaddr(card
)) {
296 data
->timeout_ns
= limit_us
* 1000;
297 data
->timeout_clks
= 0;
301 EXPORT_SYMBOL(mmc_set_data_timeout
);
304 * mmc_align_data_size - pads a transfer size to a more optimal value
305 * @card: the MMC card associated with the data transfer
306 * @sz: original transfer size
308 * Pads the original data size with a number of extra bytes in
309 * order to avoid controller bugs and/or performance hits
310 * (e.g. some controllers revert to PIO for certain sizes).
312 * Returns the improved size, which might be unmodified.
314 * Note that this function is only relevant when issuing a
315 * single scatter gather entry.
317 unsigned int mmc_align_data_size(struct mmc_card
*card
, unsigned int sz
)
320 * FIXME: We don't have a system for the controller to tell
321 * the core about its problems yet, so for now we just 32-bit
324 sz
= ((sz
+ 3) / 4) * 4;
328 EXPORT_SYMBOL(mmc_align_data_size
);
331 * __mmc_claim_host - exclusively claim a host
332 * @host: mmc host to claim
333 * @abort: whether or not the operation should be aborted
335 * Claim a host for a set of operations. If @abort is non null and
336 * dereference a non-zero value then this will return prematurely with
337 * that non-zero value without acquiring the lock. Returns zero
338 * with the lock held otherwise.
340 int __mmc_claim_host(struct mmc_host
*host
, atomic_t
*abort
)
342 DECLARE_WAITQUEUE(wait
, current
);
348 add_wait_queue(&host
->wq
, &wait
);
349 spin_lock_irqsave(&host
->lock
, flags
);
351 set_current_state(TASK_UNINTERRUPTIBLE
);
352 stop
= abort
? atomic_read(abort
) : 0;
353 if (stop
|| !host
->claimed
)
355 spin_unlock_irqrestore(&host
->lock
, flags
);
357 spin_lock_irqsave(&host
->lock
, flags
);
359 set_current_state(TASK_RUNNING
);
364 spin_unlock_irqrestore(&host
->lock
, flags
);
365 remove_wait_queue(&host
->wq
, &wait
);
369 EXPORT_SYMBOL(__mmc_claim_host
);
372 * mmc_release_host - release a host
373 * @host: mmc host to release
375 * Release a MMC host, allowing others to claim the host
376 * for their operations.
378 void mmc_release_host(struct mmc_host
*host
)
382 WARN_ON(!host
->claimed
);
384 spin_lock_irqsave(&host
->lock
, flags
);
386 spin_unlock_irqrestore(&host
->lock
, flags
);
391 EXPORT_SYMBOL(mmc_release_host
);
394 * Internal function that does the actual ios call to the host driver,
395 * optionally printing some debug output.
397 static inline void mmc_set_ios(struct mmc_host
*host
)
399 struct mmc_ios
*ios
= &host
->ios
;
401 pr_debug("%s: clock %uHz busmode %u powermode %u cs %u Vdd %u "
402 "width %u timing %u\n",
403 mmc_hostname(host
), ios
->clock
, ios
->bus_mode
,
404 ios
->power_mode
, ios
->chip_select
, ios
->vdd
,
405 ios
->bus_width
, ios
->timing
);
407 host
->ops
->set_ios(host
, ios
);
411 * Control chip select pin on a host.
413 void mmc_set_chip_select(struct mmc_host
*host
, int mode
)
415 host
->ios
.chip_select
= mode
;
420 * Sets the host clock to the highest possible frequency that
423 void mmc_set_clock(struct mmc_host
*host
, unsigned int hz
)
425 WARN_ON(hz
< host
->f_min
);
427 if (hz
> host
->f_max
)
430 host
->ios
.clock
= hz
;
435 * Change the bus mode (open drain/push-pull) of a host.
437 void mmc_set_bus_mode(struct mmc_host
*host
, unsigned int mode
)
439 host
->ios
.bus_mode
= mode
;
444 * Change data bus width of a host.
446 void mmc_set_bus_width(struct mmc_host
*host
, unsigned int width
)
448 host
->ios
.bus_width
= width
;
453 * mmc_vdd_to_ocrbitnum - Convert a voltage to the OCR bit number
455 * @low_bits: prefer low bits in boundary cases
457 * This function returns the OCR bit number according to the provided @vdd
458 * value. If conversion is not possible a negative errno value returned.
460 * Depending on the @low_bits flag the function prefers low or high OCR bits
461 * on boundary voltages. For example,
462 * with @low_bits = true, 3300 mV translates to ilog2(MMC_VDD_32_33);
463 * with @low_bits = false, 3300 mV translates to ilog2(MMC_VDD_33_34);
465 * Any value in the [1951:1999] range translates to the ilog2(MMC_VDD_20_21).
467 static int mmc_vdd_to_ocrbitnum(int vdd
, bool low_bits
)
469 const int max_bit
= ilog2(MMC_VDD_35_36
);
472 if (vdd
< 1650 || vdd
> 3600)
475 if (vdd
>= 1650 && vdd
<= 1950)
476 return ilog2(MMC_VDD_165_195
);
481 /* Base 2000 mV, step 100 mV, bit's base 8. */
482 bit
= (vdd
- 2000) / 100 + 8;
489 * mmc_vddrange_to_ocrmask - Convert a voltage range to the OCR mask
490 * @vdd_min: minimum voltage value (mV)
491 * @vdd_max: maximum voltage value (mV)
493 * This function returns the OCR mask bits according to the provided @vdd_min
494 * and @vdd_max values. If conversion is not possible the function returns 0.
496 * Notes wrt boundary cases:
497 * This function sets the OCR bits for all boundary voltages, for example
498 * [3300:3400] range is translated to MMC_VDD_32_33 | MMC_VDD_33_34 |
499 * MMC_VDD_34_35 mask.
501 u32
mmc_vddrange_to_ocrmask(int vdd_min
, int vdd_max
)
505 if (vdd_max
< vdd_min
)
508 /* Prefer high bits for the boundary vdd_max values. */
509 vdd_max
= mmc_vdd_to_ocrbitnum(vdd_max
, false);
513 /* Prefer low bits for the boundary vdd_min values. */
514 vdd_min
= mmc_vdd_to_ocrbitnum(vdd_min
, true);
518 /* Fill the mask, from max bit to min bit. */
519 while (vdd_max
>= vdd_min
)
520 mask
|= 1 << vdd_max
--;
524 EXPORT_SYMBOL(mmc_vddrange_to_ocrmask
);
527 * Mask off any voltages we don't support and select
530 u32
mmc_select_voltage(struct mmc_host
*host
, u32 ocr
)
534 ocr
&= host
->ocr_avail
;
545 pr_warning("%s: host doesn't support card's voltages\n",
554 * Select timing parameters for host.
556 void mmc_set_timing(struct mmc_host
*host
, unsigned int timing
)
558 host
->ios
.timing
= timing
;
563 * Apply power to the MMC stack. This is a two-stage process.
564 * First, we enable power to the card without the clock running.
565 * We then wait a bit for the power to stabilise. Finally,
566 * enable the bus drivers and clock to the card.
568 * We must _NOT_ enable the clock prior to power stablising.
570 * If a host does all the power sequencing itself, ignore the
571 * initial MMC_POWER_UP stage.
573 static void mmc_power_up(struct mmc_host
*host
)
575 int bit
= fls(host
->ocr_avail
) - 1;
578 if (mmc_host_is_spi(host
)) {
579 host
->ios
.chip_select
= MMC_CS_HIGH
;
580 host
->ios
.bus_mode
= MMC_BUSMODE_PUSHPULL
;
582 host
->ios
.chip_select
= MMC_CS_DONTCARE
;
583 host
->ios
.bus_mode
= MMC_BUSMODE_OPENDRAIN
;
585 host
->ios
.power_mode
= MMC_POWER_UP
;
586 host
->ios
.bus_width
= MMC_BUS_WIDTH_1
;
587 host
->ios
.timing
= MMC_TIMING_LEGACY
;
591 * This delay should be sufficient to allow the power supply
592 * to reach the minimum voltage.
596 host
->ios
.clock
= host
->f_min
;
597 host
->ios
.power_mode
= MMC_POWER_ON
;
601 * This delay must be at least 74 clock sizes, or 1 ms, or the
602 * time required to reach a stable voltage.
607 static void mmc_power_off(struct mmc_host
*host
)
611 if (!mmc_host_is_spi(host
)) {
612 host
->ios
.bus_mode
= MMC_BUSMODE_OPENDRAIN
;
613 host
->ios
.chip_select
= MMC_CS_DONTCARE
;
615 host
->ios
.power_mode
= MMC_POWER_OFF
;
616 host
->ios
.bus_width
= MMC_BUS_WIDTH_1
;
617 host
->ios
.timing
= MMC_TIMING_LEGACY
;
622 * Cleanup when the last reference to the bus operator is dropped.
624 static void __mmc_release_bus(struct mmc_host
*host
)
627 BUG_ON(host
->bus_refs
);
628 BUG_ON(!host
->bus_dead
);
630 host
->bus_ops
= NULL
;
634 * Increase reference count of bus operator
636 static inline void mmc_bus_get(struct mmc_host
*host
)
640 spin_lock_irqsave(&host
->lock
, flags
);
642 spin_unlock_irqrestore(&host
->lock
, flags
);
646 * Decrease reference count of bus operator and free it if
647 * it is the last reference.
649 static inline void mmc_bus_put(struct mmc_host
*host
)
653 spin_lock_irqsave(&host
->lock
, flags
);
655 if ((host
->bus_refs
== 0) && host
->bus_ops
)
656 __mmc_release_bus(host
);
657 spin_unlock_irqrestore(&host
->lock
, flags
);
661 * Assign a mmc bus handler to a host. Only one bus handler may control a
662 * host at any given time.
664 void mmc_attach_bus(struct mmc_host
*host
, const struct mmc_bus_ops
*ops
)
671 WARN_ON(!host
->claimed
);
673 spin_lock_irqsave(&host
->lock
, flags
);
675 BUG_ON(host
->bus_ops
);
676 BUG_ON(host
->bus_refs
);
682 spin_unlock_irqrestore(&host
->lock
, flags
);
686 * Remove the current bus handler from a host. Assumes that there are
687 * no interesting cards left, so the bus is powered down.
689 void mmc_detach_bus(struct mmc_host
*host
)
695 WARN_ON(!host
->claimed
);
696 WARN_ON(!host
->bus_ops
);
698 spin_lock_irqsave(&host
->lock
, flags
);
702 spin_unlock_irqrestore(&host
->lock
, flags
);
710 * mmc_detect_change - process change of state on a MMC socket
711 * @host: host which changed state.
712 * @delay: optional delay to wait before detection (jiffies)
714 * MMC drivers should call this when they detect a card has been
715 * inserted or removed. The MMC layer will confirm that any
716 * present card is still functional, and initialize any newly
719 void mmc_detect_change(struct mmc_host
*host
, unsigned long delay
)
721 #ifdef CONFIG_MMC_DEBUG
723 spin_lock_irqsave(&host
->lock
, flags
);
724 WARN_ON(host
->removed
);
725 spin_unlock_irqrestore(&host
->lock
, flags
);
728 mmc_schedule_delayed_work(&host
->detect
, delay
);
731 EXPORT_SYMBOL(mmc_detect_change
);
734 void mmc_rescan(struct work_struct
*work
)
736 struct mmc_host
*host
=
737 container_of(work
, struct mmc_host
, detect
.work
);
743 if (host
->bus_ops
== NULL
) {
745 * Only we can add a new handler, so it's safe to
746 * release the lock here.
750 if (host
->ops
->get_cd
&& host
->ops
->get_cd(host
) == 0)
753 mmc_claim_host(host
);
758 mmc_send_if_cond(host
, host
->ocr_avail
);
761 * First we search for SDIO...
763 err
= mmc_send_io_op_cond(host
, 0, &ocr
);
765 if (mmc_attach_sdio(host
, ocr
))
771 * ...then normal SD...
773 err
= mmc_send_app_op_cond(host
, 0, &ocr
);
775 if (mmc_attach_sd(host
, ocr
))
781 * ...and finally MMC.
783 err
= mmc_send_op_cond(host
, 0, &ocr
);
785 if (mmc_attach_mmc(host
, ocr
))
790 mmc_release_host(host
);
793 if (host
->bus_ops
->detect
&& !host
->bus_dead
)
794 host
->bus_ops
->detect(host
);
799 if (host
->caps
& MMC_CAP_NEEDS_POLL
)
800 mmc_schedule_delayed_work(&host
->detect
, HZ
);
803 void mmc_start_host(struct mmc_host
*host
)
806 mmc_detect_change(host
, 0);
809 void mmc_stop_host(struct mmc_host
*host
)
811 #ifdef CONFIG_MMC_DEBUG
813 spin_lock_irqsave(&host
->lock
, flags
);
815 spin_unlock_irqrestore(&host
->lock
, flags
);
818 mmc_flush_scheduled_work();
821 if (host
->bus_ops
&& !host
->bus_dead
) {
822 if (host
->bus_ops
->remove
)
823 host
->bus_ops
->remove(host
);
825 mmc_claim_host(host
);
826 mmc_detach_bus(host
);
827 mmc_release_host(host
);
839 * mmc_suspend_host - suspend a host
841 * @state: suspend mode (PM_SUSPEND_xxx)
843 int mmc_suspend_host(struct mmc_host
*host
, pm_message_t state
)
845 mmc_flush_scheduled_work();
848 if (host
->bus_ops
&& !host
->bus_dead
) {
849 if (host
->bus_ops
->suspend
)
850 host
->bus_ops
->suspend(host
);
851 if (!host
->bus_ops
->resume
) {
852 if (host
->bus_ops
->remove
)
853 host
->bus_ops
->remove(host
);
855 mmc_claim_host(host
);
856 mmc_detach_bus(host
);
857 mmc_release_host(host
);
867 EXPORT_SYMBOL(mmc_suspend_host
);
870 * mmc_resume_host - resume a previously suspended host
873 int mmc_resume_host(struct mmc_host
*host
)
876 if (host
->bus_ops
&& !host
->bus_dead
) {
878 BUG_ON(!host
->bus_ops
->resume
);
879 host
->bus_ops
->resume(host
);
884 * We add a slight delay here so that resume can progress
887 mmc_detect_change(host
, 1);
892 EXPORT_SYMBOL(mmc_resume_host
);
896 static int __init
mmc_init(void)
900 workqueue
= create_singlethread_workqueue("kmmcd");
904 ret
= mmc_register_bus();
906 goto destroy_workqueue
;
908 ret
= mmc_register_host_class();
912 ret
= sdio_register_bus();
914 goto unregister_host_class
;
918 unregister_host_class
:
919 mmc_unregister_host_class();
921 mmc_unregister_bus();
923 destroy_workqueue(workqueue
);
928 static void __exit
mmc_exit(void)
930 sdio_unregister_bus();
931 mmc_unregister_host_class();
932 mmc_unregister_bus();
933 destroy_workqueue(workqueue
);
936 subsys_initcall(mmc_init
);
937 module_exit(mmc_exit
);
939 MODULE_LICENSE("GPL");