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-2007 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>
24 #include <linux/mmc/card.h>
25 #include <linux/mmc/host.h>
26 #include <linux/mmc/mmc.h>
27 #include <linux/mmc/sd.h>
38 extern int mmc_attach_mmc(struct mmc_host
*host
, u32 ocr
);
39 extern int mmc_attach_sd(struct mmc_host
*host
, u32 ocr
);
40 extern int mmc_attach_sdio(struct mmc_host
*host
, u32 ocr
);
42 static struct workqueue_struct
*workqueue
;
45 * Enabling software CRCs on the data blocks can be a significant (30%)
46 * performance cost, and for other reasons may not always be desired.
47 * So we allow it it to be disabled.
50 module_param(use_spi_crc
, bool, 0);
53 * Internal function. Schedule delayed work in the MMC work queue.
55 static int mmc_schedule_delayed_work(struct delayed_work
*work
,
58 return queue_delayed_work(workqueue
, work
, delay
);
62 * Internal function. Flush all scheduled work from the MMC work queue.
64 static void mmc_flush_scheduled_work(void)
66 flush_workqueue(workqueue
);
70 * mmc_request_done - finish processing an MMC request
71 * @host: MMC host which completed request
72 * @mrq: MMC request which request
74 * MMC drivers should call this function when they have completed
75 * their processing of a request.
77 void mmc_request_done(struct mmc_host
*host
, struct mmc_request
*mrq
)
79 struct mmc_command
*cmd
= mrq
->cmd
;
82 if (err
&& cmd
->retries
&& mmc_host_is_spi(host
)) {
83 if (cmd
->resp
[0] & R1_SPI_ILLEGAL_COMMAND
)
87 if (err
&& cmd
->retries
) {
88 pr_debug("%s: req failed (CMD%u): %d, retrying...\n",
89 mmc_hostname(host
), cmd
->opcode
, err
);
93 host
->ops
->request(host
, mrq
);
95 led_trigger_event(host
->led
, LED_OFF
);
97 pr_debug("%s: req done (CMD%u): %d: %08x %08x %08x %08x\n",
98 mmc_hostname(host
), cmd
->opcode
, err
,
99 cmd
->resp
[0], cmd
->resp
[1],
100 cmd
->resp
[2], cmd
->resp
[3]);
103 pr_debug("%s: %d bytes transferred: %d\n",
105 mrq
->data
->bytes_xfered
, mrq
->data
->error
);
109 pr_debug("%s: (CMD%u): %d: %08x %08x %08x %08x\n",
110 mmc_hostname(host
), mrq
->stop
->opcode
,
112 mrq
->stop
->resp
[0], mrq
->stop
->resp
[1],
113 mrq
->stop
->resp
[2], mrq
->stop
->resp
[3]);
121 EXPORT_SYMBOL(mmc_request_done
);
124 mmc_start_request(struct mmc_host
*host
, struct mmc_request
*mrq
)
126 #ifdef CONFIG_MMC_DEBUG
130 pr_debug("%s: starting CMD%u arg %08x flags %08x\n",
131 mmc_hostname(host
), mrq
->cmd
->opcode
,
132 mrq
->cmd
->arg
, mrq
->cmd
->flags
);
135 pr_debug("%s: blksz %d blocks %d flags %08x "
136 "tsac %d ms nsac %d\n",
137 mmc_hostname(host
), mrq
->data
->blksz
,
138 mrq
->data
->blocks
, mrq
->data
->flags
,
139 mrq
->data
->timeout_ns
/ 1000000,
140 mrq
->data
->timeout_clks
);
144 pr_debug("%s: CMD%u arg %08x flags %08x\n",
145 mmc_hostname(host
), mrq
->stop
->opcode
,
146 mrq
->stop
->arg
, mrq
->stop
->flags
);
149 WARN_ON(!host
->claimed
);
151 led_trigger_event(host
->led
, LED_FULL
);
156 BUG_ON(mrq
->data
->blksz
> host
->max_blk_size
);
157 BUG_ON(mrq
->data
->blocks
> host
->max_blk_count
);
158 BUG_ON(mrq
->data
->blocks
* mrq
->data
->blksz
>
161 #ifdef CONFIG_MMC_DEBUG
163 for (i
= 0;i
< mrq
->data
->sg_len
;i
++)
164 sz
+= mrq
->data
->sg
[i
].length
;
165 BUG_ON(sz
!= mrq
->data
->blocks
* mrq
->data
->blksz
);
168 mrq
->cmd
->data
= mrq
->data
;
169 mrq
->data
->error
= 0;
170 mrq
->data
->mrq
= mrq
;
172 mrq
->data
->stop
= mrq
->stop
;
173 mrq
->stop
->error
= 0;
174 mrq
->stop
->mrq
= mrq
;
177 host
->ops
->request(host
, mrq
);
180 static void mmc_wait_done(struct mmc_request
*mrq
)
182 complete(mrq
->done_data
);
186 * mmc_wait_for_req - start a request and wait for completion
187 * @host: MMC host to start command
188 * @mrq: MMC request to start
190 * Start a new MMC custom command request for a host, and wait
191 * for the command to complete. Does not attempt to parse the
194 void mmc_wait_for_req(struct mmc_host
*host
, struct mmc_request
*mrq
)
196 DECLARE_COMPLETION_ONSTACK(complete
);
198 mrq
->done_data
= &complete
;
199 mrq
->done
= mmc_wait_done
;
201 mmc_start_request(host
, mrq
);
203 wait_for_completion(&complete
);
206 EXPORT_SYMBOL(mmc_wait_for_req
);
209 * mmc_wait_for_cmd - start a command and wait for completion
210 * @host: MMC host to start command
211 * @cmd: MMC command to start
212 * @retries: maximum number of retries
214 * Start a new MMC command for a host, and wait for the command
215 * to complete. Return any error that occurred while the command
216 * was executing. Do not attempt to parse the response.
218 int mmc_wait_for_cmd(struct mmc_host
*host
, struct mmc_command
*cmd
, int retries
)
220 struct mmc_request mrq
;
222 WARN_ON(!host
->claimed
);
224 memset(&mrq
, 0, sizeof(struct mmc_request
));
226 memset(cmd
->resp
, 0, sizeof(cmd
->resp
));
227 cmd
->retries
= retries
;
232 mmc_wait_for_req(host
, &mrq
);
237 EXPORT_SYMBOL(mmc_wait_for_cmd
);
240 * mmc_set_data_timeout - set the timeout for a data command
241 * @data: data phase for command
242 * @card: the MMC card associated with the data transfer
244 * Computes the data timeout parameters according to the
245 * correct algorithm given the card type.
247 void mmc_set_data_timeout(struct mmc_data
*data
, const struct mmc_card
*card
)
252 * SDIO cards only define an upper 1 s limit on access.
254 if (mmc_card_sdio(card
)) {
255 data
->timeout_ns
= 1000000000;
256 data
->timeout_clks
= 0;
261 * SD cards use a 100 multiplier rather than 10
263 mult
= mmc_card_sd(card
) ? 100 : 10;
266 * Scale up the multiplier (and therefore the timeout) by
267 * the r2w factor for writes.
269 if (data
->flags
& MMC_DATA_WRITE
)
270 mult
<<= card
->csd
.r2w_factor
;
272 data
->timeout_ns
= card
->csd
.tacc_ns
* mult
;
273 data
->timeout_clks
= card
->csd
.tacc_clks
* mult
;
276 * SD cards also have an upper limit on the timeout.
278 if (mmc_card_sd(card
)) {
279 unsigned int timeout_us
, limit_us
;
281 timeout_us
= data
->timeout_ns
/ 1000;
282 timeout_us
+= data
->timeout_clks
* 1000 /
283 (card
->host
->ios
.clock
/ 1000);
285 if (data
->flags
& MMC_DATA_WRITE
)
291 * SDHC cards always use these fixed values.
293 if (timeout_us
> limit_us
|| mmc_card_blockaddr(card
)) {
294 data
->timeout_ns
= limit_us
* 1000;
295 data
->timeout_clks
= 0;
299 EXPORT_SYMBOL(mmc_set_data_timeout
);
302 * __mmc_claim_host - exclusively claim a host
303 * @host: mmc host to claim
304 * @abort: whether or not the operation should be aborted
306 * Claim a host for a set of operations. If @abort is non null and
307 * dereference a non-zero value then this will return prematurely with
308 * that non-zero value without acquiring the lock. Returns zero
309 * with the lock held otherwise.
311 int __mmc_claim_host(struct mmc_host
*host
, atomic_t
*abort
)
313 DECLARE_WAITQUEUE(wait
, current
);
319 add_wait_queue(&host
->wq
, &wait
);
320 spin_lock_irqsave(&host
->lock
, flags
);
322 set_current_state(TASK_UNINTERRUPTIBLE
);
323 stop
= abort
? atomic_read(abort
) : 0;
324 if (stop
|| !host
->claimed
)
326 spin_unlock_irqrestore(&host
->lock
, flags
);
328 spin_lock_irqsave(&host
->lock
, flags
);
330 set_current_state(TASK_RUNNING
);
335 spin_unlock_irqrestore(&host
->lock
, flags
);
336 remove_wait_queue(&host
->wq
, &wait
);
340 EXPORT_SYMBOL(__mmc_claim_host
);
343 * mmc_release_host - release a host
344 * @host: mmc host to release
346 * Release a MMC host, allowing others to claim the host
347 * for their operations.
349 void mmc_release_host(struct mmc_host
*host
)
353 WARN_ON(!host
->claimed
);
355 spin_lock_irqsave(&host
->lock
, flags
);
357 spin_unlock_irqrestore(&host
->lock
, flags
);
362 EXPORT_SYMBOL(mmc_release_host
);
365 * Internal function that does the actual ios call to the host driver,
366 * optionally printing some debug output.
368 static inline void mmc_set_ios(struct mmc_host
*host
)
370 struct mmc_ios
*ios
= &host
->ios
;
372 pr_debug("%s: clock %uHz busmode %u powermode %u cs %u Vdd %u "
373 "width %u timing %u\n",
374 mmc_hostname(host
), ios
->clock
, ios
->bus_mode
,
375 ios
->power_mode
, ios
->chip_select
, ios
->vdd
,
376 ios
->bus_width
, ios
->timing
);
378 host
->ops
->set_ios(host
, ios
);
382 * Control chip select pin on a host.
384 void mmc_set_chip_select(struct mmc_host
*host
, int mode
)
386 host
->ios
.chip_select
= mode
;
391 * Sets the host clock to the highest possible frequency that
394 void mmc_set_clock(struct mmc_host
*host
, unsigned int hz
)
396 WARN_ON(hz
< host
->f_min
);
398 if (hz
> host
->f_max
)
401 host
->ios
.clock
= hz
;
406 * Change the bus mode (open drain/push-pull) of a host.
408 void mmc_set_bus_mode(struct mmc_host
*host
, unsigned int mode
)
410 host
->ios
.bus_mode
= mode
;
415 * Change data bus width of a host.
417 void mmc_set_bus_width(struct mmc_host
*host
, unsigned int width
)
419 host
->ios
.bus_width
= width
;
424 * Mask off any voltages we don't support and select
427 u32
mmc_select_voltage(struct mmc_host
*host
, u32 ocr
)
431 ocr
&= host
->ocr_avail
;
449 * Select timing parameters for host.
451 void mmc_set_timing(struct mmc_host
*host
, unsigned int timing
)
453 host
->ios
.timing
= timing
;
458 * Apply power to the MMC stack. This is a two-stage process.
459 * First, we enable power to the card without the clock running.
460 * We then wait a bit for the power to stabilise. Finally,
461 * enable the bus drivers and clock to the card.
463 * We must _NOT_ enable the clock prior to power stablising.
465 * If a host does all the power sequencing itself, ignore the
466 * initial MMC_POWER_UP stage.
468 static void mmc_power_up(struct mmc_host
*host
)
470 int bit
= fls(host
->ocr_avail
) - 1;
473 if (mmc_host_is_spi(host
)) {
474 host
->ios
.chip_select
= MMC_CS_HIGH
;
475 host
->ios
.bus_mode
= MMC_BUSMODE_PUSHPULL
;
477 host
->ios
.chip_select
= MMC_CS_DONTCARE
;
478 host
->ios
.bus_mode
= MMC_BUSMODE_OPENDRAIN
;
480 host
->ios
.power_mode
= MMC_POWER_UP
;
481 host
->ios
.bus_width
= MMC_BUS_WIDTH_1
;
482 host
->ios
.timing
= MMC_TIMING_LEGACY
;
486 * This delay should be sufficient to allow the power supply
487 * to reach the minimum voltage.
491 host
->ios
.clock
= host
->f_min
;
492 host
->ios
.power_mode
= MMC_POWER_ON
;
496 * This delay must be at least 74 clock sizes, or 1 ms, or the
497 * time required to reach a stable voltage.
502 static void mmc_power_off(struct mmc_host
*host
)
506 if (!mmc_host_is_spi(host
)) {
507 host
->ios
.bus_mode
= MMC_BUSMODE_OPENDRAIN
;
508 host
->ios
.chip_select
= MMC_CS_DONTCARE
;
510 host
->ios
.power_mode
= MMC_POWER_OFF
;
511 host
->ios
.bus_width
= MMC_BUS_WIDTH_1
;
512 host
->ios
.timing
= MMC_TIMING_LEGACY
;
517 * Cleanup when the last reference to the bus operator is dropped.
519 void __mmc_release_bus(struct mmc_host
*host
)
522 BUG_ON(host
->bus_refs
);
523 BUG_ON(!host
->bus_dead
);
525 host
->bus_ops
= NULL
;
529 * Increase reference count of bus operator
531 static inline void mmc_bus_get(struct mmc_host
*host
)
535 spin_lock_irqsave(&host
->lock
, flags
);
537 spin_unlock_irqrestore(&host
->lock
, flags
);
541 * Decrease reference count of bus operator and free it if
542 * it is the last reference.
544 static inline void mmc_bus_put(struct mmc_host
*host
)
548 spin_lock_irqsave(&host
->lock
, flags
);
550 if ((host
->bus_refs
== 0) && host
->bus_ops
)
551 __mmc_release_bus(host
);
552 spin_unlock_irqrestore(&host
->lock
, flags
);
556 * Assign a mmc bus handler to a host. Only one bus handler may control a
557 * host at any given time.
559 void mmc_attach_bus(struct mmc_host
*host
, const struct mmc_bus_ops
*ops
)
566 WARN_ON(!host
->claimed
);
568 spin_lock_irqsave(&host
->lock
, flags
);
570 BUG_ON(host
->bus_ops
);
571 BUG_ON(host
->bus_refs
);
577 spin_unlock_irqrestore(&host
->lock
, flags
);
581 * Remove the current bus handler from a host. Assumes that there are
582 * no interesting cards left, so the bus is powered down.
584 void mmc_detach_bus(struct mmc_host
*host
)
590 WARN_ON(!host
->claimed
);
591 WARN_ON(!host
->bus_ops
);
593 spin_lock_irqsave(&host
->lock
, flags
);
597 spin_unlock_irqrestore(&host
->lock
, flags
);
605 * mmc_detect_change - process change of state on a MMC socket
606 * @host: host which changed state.
607 * @delay: optional delay to wait before detection (jiffies)
609 * MMC drivers should call this when they detect a card has been
610 * inserted or removed. The MMC layer will confirm that any
611 * present card is still functional, and initialize any newly
614 void mmc_detect_change(struct mmc_host
*host
, unsigned long delay
)
616 #ifdef CONFIG_MMC_DEBUG
618 spin_lock_irqsave(&host
->lock
, flags
);
619 WARN_ON(host
->removed
);
620 spin_unlock_irqrestore(&host
->lock
, flags
);
623 mmc_schedule_delayed_work(&host
->detect
, delay
);
626 EXPORT_SYMBOL(mmc_detect_change
);
629 void mmc_rescan(struct work_struct
*work
)
631 struct mmc_host
*host
=
632 container_of(work
, struct mmc_host
, detect
.work
);
638 if (host
->bus_ops
== NULL
) {
640 * Only we can add a new handler, so it's safe to
641 * release the lock here.
645 mmc_claim_host(host
);
650 mmc_send_if_cond(host
, host
->ocr_avail
);
653 * First we search for SDIO...
655 err
= mmc_send_io_op_cond(host
, 0, &ocr
);
657 if (mmc_attach_sdio(host
, ocr
))
663 * ...then normal SD...
665 err
= mmc_send_app_op_cond(host
, 0, &ocr
);
667 if (mmc_attach_sd(host
, ocr
))
673 * ...and finally MMC.
675 err
= mmc_send_op_cond(host
, 0, &ocr
);
677 if (mmc_attach_mmc(host
, ocr
))
682 mmc_release_host(host
);
685 if (host
->bus_ops
->detect
&& !host
->bus_dead
)
686 host
->bus_ops
->detect(host
);
692 void mmc_start_host(struct mmc_host
*host
)
695 mmc_detect_change(host
, 0);
698 void mmc_stop_host(struct mmc_host
*host
)
700 #ifdef CONFIG_MMC_DEBUG
702 spin_lock_irqsave(&host
->lock
, flags
);
704 spin_unlock_irqrestore(&host
->lock
, flags
);
707 mmc_flush_scheduled_work();
710 if (host
->bus_ops
&& !host
->bus_dead
) {
711 if (host
->bus_ops
->remove
)
712 host
->bus_ops
->remove(host
);
714 mmc_claim_host(host
);
715 mmc_detach_bus(host
);
716 mmc_release_host(host
);
728 * mmc_suspend_host - suspend a host
730 * @state: suspend mode (PM_SUSPEND_xxx)
732 int mmc_suspend_host(struct mmc_host
*host
, pm_message_t state
)
734 mmc_flush_scheduled_work();
737 if (host
->bus_ops
&& !host
->bus_dead
) {
738 if (host
->bus_ops
->suspend
)
739 host
->bus_ops
->suspend(host
);
740 if (!host
->bus_ops
->resume
) {
741 if (host
->bus_ops
->remove
)
742 host
->bus_ops
->remove(host
);
744 mmc_claim_host(host
);
745 mmc_detach_bus(host
);
746 mmc_release_host(host
);
756 EXPORT_SYMBOL(mmc_suspend_host
);
759 * mmc_resume_host - resume a previously suspended host
762 int mmc_resume_host(struct mmc_host
*host
)
765 if (host
->bus_ops
&& !host
->bus_dead
) {
767 BUG_ON(!host
->bus_ops
->resume
);
768 host
->bus_ops
->resume(host
);
773 * We add a slight delay here so that resume can progress
776 mmc_detect_change(host
, 1);
781 EXPORT_SYMBOL(mmc_resume_host
);
785 static int __init
mmc_init(void)
789 workqueue
= create_singlethread_workqueue("kmmcd");
793 ret
= mmc_register_bus();
795 goto destroy_workqueue
;
797 ret
= mmc_register_host_class();
801 ret
= sdio_register_bus();
803 goto unregister_host_class
;
807 unregister_host_class
:
808 mmc_unregister_host_class();
810 mmc_unregister_bus();
812 destroy_workqueue(workqueue
);
817 static void __exit
mmc_exit(void)
819 sdio_unregister_bus();
820 mmc_unregister_host_class();
821 mmc_unregister_bus();
822 destroy_workqueue(workqueue
);
825 subsys_initcall(mmc_init
);
826 module_exit(mmc_exit
);
828 MODULE_LICENSE("GPL");