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 static struct workqueue_struct
*workqueue
;
41 * Enabling software CRCs on the data blocks can be a significant (30%)
42 * performance cost, and for other reasons may not always be desired.
43 * So we allow it it to be disabled.
46 module_param(use_spi_crc
, bool, 0);
49 * Internal function. Schedule delayed work in the MMC work queue.
51 static int mmc_schedule_delayed_work(struct delayed_work
*work
,
54 return queue_delayed_work(workqueue
, work
, delay
);
58 * Internal function. Flush all scheduled work from the MMC work queue.
60 static void mmc_flush_scheduled_work(void)
62 flush_workqueue(workqueue
);
66 * mmc_request_done - finish processing an MMC request
67 * @host: MMC host which completed request
68 * @mrq: MMC request which request
70 * MMC drivers should call this function when they have completed
71 * their processing of a request.
73 void mmc_request_done(struct mmc_host
*host
, struct mmc_request
*mrq
)
75 struct mmc_command
*cmd
= mrq
->cmd
;
78 if (err
&& cmd
->retries
&& mmc_host_is_spi(host
)) {
79 if (cmd
->resp
[0] & R1_SPI_ILLEGAL_COMMAND
)
83 if (err
&& cmd
->retries
) {
84 pr_debug("%s: req failed (CMD%u): %d, retrying...\n",
85 mmc_hostname(host
), cmd
->opcode
, err
);
89 host
->ops
->request(host
, mrq
);
91 led_trigger_event(host
->led
, LED_OFF
);
93 pr_debug("%s: req done (CMD%u): %d: %08x %08x %08x %08x\n",
94 mmc_hostname(host
), cmd
->opcode
, err
,
95 cmd
->resp
[0], cmd
->resp
[1],
96 cmd
->resp
[2], cmd
->resp
[3]);
99 pr_debug("%s: %d bytes transferred: %d\n",
101 mrq
->data
->bytes_xfered
, mrq
->data
->error
);
105 pr_debug("%s: (CMD%u): %d: %08x %08x %08x %08x\n",
106 mmc_hostname(host
), mrq
->stop
->opcode
,
108 mrq
->stop
->resp
[0], mrq
->stop
->resp
[1],
109 mrq
->stop
->resp
[2], mrq
->stop
->resp
[3]);
117 EXPORT_SYMBOL(mmc_request_done
);
120 mmc_start_request(struct mmc_host
*host
, struct mmc_request
*mrq
)
122 #ifdef CONFIG_MMC_DEBUG
126 pr_debug("%s: starting CMD%u arg %08x flags %08x\n",
127 mmc_hostname(host
), mrq
->cmd
->opcode
,
128 mrq
->cmd
->arg
, mrq
->cmd
->flags
);
131 pr_debug("%s: blksz %d blocks %d flags %08x "
132 "tsac %d ms nsac %d\n",
133 mmc_hostname(host
), mrq
->data
->blksz
,
134 mrq
->data
->blocks
, mrq
->data
->flags
,
135 mrq
->data
->timeout_ns
/ 1000000,
136 mrq
->data
->timeout_clks
);
140 pr_debug("%s: CMD%u arg %08x flags %08x\n",
141 mmc_hostname(host
), mrq
->stop
->opcode
,
142 mrq
->stop
->arg
, mrq
->stop
->flags
);
145 WARN_ON(!host
->claimed
);
147 led_trigger_event(host
->led
, LED_FULL
);
152 BUG_ON(mrq
->data
->blksz
> host
->max_blk_size
);
153 BUG_ON(mrq
->data
->blocks
> host
->max_blk_count
);
154 BUG_ON(mrq
->data
->blocks
* mrq
->data
->blksz
>
157 #ifdef CONFIG_MMC_DEBUG
159 for (i
= 0;i
< mrq
->data
->sg_len
;i
++)
160 sz
+= mrq
->data
->sg
[i
].length
;
161 BUG_ON(sz
!= mrq
->data
->blocks
* mrq
->data
->blksz
);
164 mrq
->cmd
->data
= mrq
->data
;
165 mrq
->data
->error
= 0;
166 mrq
->data
->mrq
= mrq
;
168 mrq
->data
->stop
= mrq
->stop
;
169 mrq
->stop
->error
= 0;
170 mrq
->stop
->mrq
= mrq
;
173 host
->ops
->request(host
, mrq
);
176 static void mmc_wait_done(struct mmc_request
*mrq
)
178 complete(mrq
->done_data
);
182 * mmc_wait_for_req - start a request and wait for completion
183 * @host: MMC host to start command
184 * @mrq: MMC request to start
186 * Start a new MMC custom command request for a host, and wait
187 * for the command to complete. Does not attempt to parse the
190 void mmc_wait_for_req(struct mmc_host
*host
, struct mmc_request
*mrq
)
192 DECLARE_COMPLETION_ONSTACK(complete
);
194 mrq
->done_data
= &complete
;
195 mrq
->done
= mmc_wait_done
;
197 mmc_start_request(host
, mrq
);
199 wait_for_completion(&complete
);
202 EXPORT_SYMBOL(mmc_wait_for_req
);
205 * mmc_wait_for_cmd - start a command and wait for completion
206 * @host: MMC host to start command
207 * @cmd: MMC command to start
208 * @retries: maximum number of retries
210 * Start a new MMC command for a host, and wait for the command
211 * to complete. Return any error that occurred while the command
212 * was executing. Do not attempt to parse the response.
214 int mmc_wait_for_cmd(struct mmc_host
*host
, struct mmc_command
*cmd
, int retries
)
216 struct mmc_request mrq
;
218 WARN_ON(!host
->claimed
);
220 memset(&mrq
, 0, sizeof(struct mmc_request
));
222 memset(cmd
->resp
, 0, sizeof(cmd
->resp
));
223 cmd
->retries
= retries
;
228 mmc_wait_for_req(host
, &mrq
);
233 EXPORT_SYMBOL(mmc_wait_for_cmd
);
236 * mmc_set_data_timeout - set the timeout for a data command
237 * @data: data phase for command
238 * @card: the MMC card associated with the data transfer
240 * Computes the data timeout parameters according to the
241 * correct algorithm given the card type.
243 void mmc_set_data_timeout(struct mmc_data
*data
, const struct mmc_card
*card
)
248 * SDIO cards only define an upper 1 s limit on access.
250 if (mmc_card_sdio(card
)) {
251 data
->timeout_ns
= 1000000000;
252 data
->timeout_clks
= 0;
257 * SD cards use a 100 multiplier rather than 10
259 mult
= mmc_card_sd(card
) ? 100 : 10;
262 * Scale up the multiplier (and therefore the timeout) by
263 * the r2w factor for writes.
265 if (data
->flags
& MMC_DATA_WRITE
)
266 mult
<<= card
->csd
.r2w_factor
;
268 data
->timeout_ns
= card
->csd
.tacc_ns
* mult
;
269 data
->timeout_clks
= card
->csd
.tacc_clks
* mult
;
272 * SD cards also have an upper limit on the timeout.
274 if (mmc_card_sd(card
)) {
275 unsigned int timeout_us
, limit_us
;
277 timeout_us
= data
->timeout_ns
/ 1000;
278 timeout_us
+= data
->timeout_clks
* 1000 /
279 (card
->host
->ios
.clock
/ 1000);
281 if (data
->flags
& MMC_DATA_WRITE
)
287 * SDHC cards always use these fixed values.
289 if (timeout_us
> limit_us
|| mmc_card_blockaddr(card
)) {
290 data
->timeout_ns
= limit_us
* 1000;
291 data
->timeout_clks
= 0;
295 EXPORT_SYMBOL(mmc_set_data_timeout
);
298 * __mmc_claim_host - exclusively claim a host
299 * @host: mmc host to claim
300 * @abort: whether or not the operation should be aborted
302 * Claim a host for a set of operations. If @abort is non null and
303 * dereference a non-zero value then this will return prematurely with
304 * that non-zero value without acquiring the lock. Returns zero
305 * with the lock held otherwise.
307 int __mmc_claim_host(struct mmc_host
*host
, atomic_t
*abort
)
309 DECLARE_WAITQUEUE(wait
, current
);
315 add_wait_queue(&host
->wq
, &wait
);
316 spin_lock_irqsave(&host
->lock
, flags
);
318 set_current_state(TASK_UNINTERRUPTIBLE
);
319 stop
= abort
? atomic_read(abort
) : 0;
320 if (stop
|| !host
->claimed
)
322 spin_unlock_irqrestore(&host
->lock
, flags
);
324 spin_lock_irqsave(&host
->lock
, flags
);
326 set_current_state(TASK_RUNNING
);
331 spin_unlock_irqrestore(&host
->lock
, flags
);
332 remove_wait_queue(&host
->wq
, &wait
);
336 EXPORT_SYMBOL(__mmc_claim_host
);
339 * mmc_release_host - release a host
340 * @host: mmc host to release
342 * Release a MMC host, allowing others to claim the host
343 * for their operations.
345 void mmc_release_host(struct mmc_host
*host
)
349 WARN_ON(!host
->claimed
);
351 spin_lock_irqsave(&host
->lock
, flags
);
353 spin_unlock_irqrestore(&host
->lock
, flags
);
358 EXPORT_SYMBOL(mmc_release_host
);
361 * Internal function that does the actual ios call to the host driver,
362 * optionally printing some debug output.
364 static inline void mmc_set_ios(struct mmc_host
*host
)
366 struct mmc_ios
*ios
= &host
->ios
;
368 pr_debug("%s: clock %uHz busmode %u powermode %u cs %u Vdd %u "
369 "width %u timing %u\n",
370 mmc_hostname(host
), ios
->clock
, ios
->bus_mode
,
371 ios
->power_mode
, ios
->chip_select
, ios
->vdd
,
372 ios
->bus_width
, ios
->timing
);
374 host
->ops
->set_ios(host
, ios
);
378 * Control chip select pin on a host.
380 void mmc_set_chip_select(struct mmc_host
*host
, int mode
)
382 host
->ios
.chip_select
= mode
;
387 * Sets the host clock to the highest possible frequency that
390 void mmc_set_clock(struct mmc_host
*host
, unsigned int hz
)
392 WARN_ON(hz
< host
->f_min
);
394 if (hz
> host
->f_max
)
397 host
->ios
.clock
= hz
;
402 * Change the bus mode (open drain/push-pull) of a host.
404 void mmc_set_bus_mode(struct mmc_host
*host
, unsigned int mode
)
406 host
->ios
.bus_mode
= mode
;
411 * Change data bus width of a host.
413 void mmc_set_bus_width(struct mmc_host
*host
, unsigned int width
)
415 host
->ios
.bus_width
= width
;
420 * Mask off any voltages we don't support and select
423 u32
mmc_select_voltage(struct mmc_host
*host
, u32 ocr
)
427 ocr
&= host
->ocr_avail
;
445 * Select timing parameters for host.
447 void mmc_set_timing(struct mmc_host
*host
, unsigned int timing
)
449 host
->ios
.timing
= timing
;
454 * Apply power to the MMC stack. This is a two-stage process.
455 * First, we enable power to the card without the clock running.
456 * We then wait a bit for the power to stabilise. Finally,
457 * enable the bus drivers and clock to the card.
459 * We must _NOT_ enable the clock prior to power stablising.
461 * If a host does all the power sequencing itself, ignore the
462 * initial MMC_POWER_UP stage.
464 static void mmc_power_up(struct mmc_host
*host
)
466 int bit
= fls(host
->ocr_avail
) - 1;
469 if (mmc_host_is_spi(host
)) {
470 host
->ios
.chip_select
= MMC_CS_HIGH
;
471 host
->ios
.bus_mode
= MMC_BUSMODE_PUSHPULL
;
473 host
->ios
.chip_select
= MMC_CS_DONTCARE
;
474 host
->ios
.bus_mode
= MMC_BUSMODE_OPENDRAIN
;
476 host
->ios
.power_mode
= MMC_POWER_UP
;
477 host
->ios
.bus_width
= MMC_BUS_WIDTH_1
;
478 host
->ios
.timing
= MMC_TIMING_LEGACY
;
482 * This delay should be sufficient to allow the power supply
483 * to reach the minimum voltage.
487 host
->ios
.clock
= host
->f_min
;
488 host
->ios
.power_mode
= MMC_POWER_ON
;
492 * This delay must be at least 74 clock sizes, or 1 ms, or the
493 * time required to reach a stable voltage.
498 static void mmc_power_off(struct mmc_host
*host
)
502 if (!mmc_host_is_spi(host
)) {
503 host
->ios
.bus_mode
= MMC_BUSMODE_OPENDRAIN
;
504 host
->ios
.chip_select
= MMC_CS_DONTCARE
;
506 host
->ios
.power_mode
= MMC_POWER_OFF
;
507 host
->ios
.bus_width
= MMC_BUS_WIDTH_1
;
508 host
->ios
.timing
= MMC_TIMING_LEGACY
;
513 * Cleanup when the last reference to the bus operator is dropped.
515 static void __mmc_release_bus(struct mmc_host
*host
)
518 BUG_ON(host
->bus_refs
);
519 BUG_ON(!host
->bus_dead
);
521 host
->bus_ops
= NULL
;
525 * Increase reference count of bus operator
527 static inline void mmc_bus_get(struct mmc_host
*host
)
531 spin_lock_irqsave(&host
->lock
, flags
);
533 spin_unlock_irqrestore(&host
->lock
, flags
);
537 * Decrease reference count of bus operator and free it if
538 * it is the last reference.
540 static inline void mmc_bus_put(struct mmc_host
*host
)
544 spin_lock_irqsave(&host
->lock
, flags
);
546 if ((host
->bus_refs
== 0) && host
->bus_ops
)
547 __mmc_release_bus(host
);
548 spin_unlock_irqrestore(&host
->lock
, flags
);
552 * Assign a mmc bus handler to a host. Only one bus handler may control a
553 * host at any given time.
555 void mmc_attach_bus(struct mmc_host
*host
, const struct mmc_bus_ops
*ops
)
562 WARN_ON(!host
->claimed
);
564 spin_lock_irqsave(&host
->lock
, flags
);
566 BUG_ON(host
->bus_ops
);
567 BUG_ON(host
->bus_refs
);
573 spin_unlock_irqrestore(&host
->lock
, flags
);
577 * Remove the current bus handler from a host. Assumes that there are
578 * no interesting cards left, so the bus is powered down.
580 void mmc_detach_bus(struct mmc_host
*host
)
586 WARN_ON(!host
->claimed
);
587 WARN_ON(!host
->bus_ops
);
589 spin_lock_irqsave(&host
->lock
, flags
);
593 spin_unlock_irqrestore(&host
->lock
, flags
);
601 * mmc_detect_change - process change of state on a MMC socket
602 * @host: host which changed state.
603 * @delay: optional delay to wait before detection (jiffies)
605 * MMC drivers should call this when they detect a card has been
606 * inserted or removed. The MMC layer will confirm that any
607 * present card is still functional, and initialize any newly
610 void mmc_detect_change(struct mmc_host
*host
, unsigned long delay
)
612 #ifdef CONFIG_MMC_DEBUG
614 spin_lock_irqsave(&host
->lock
, flags
);
615 WARN_ON(host
->removed
);
616 spin_unlock_irqrestore(&host
->lock
, flags
);
619 mmc_schedule_delayed_work(&host
->detect
, delay
);
622 EXPORT_SYMBOL(mmc_detect_change
);
625 void mmc_rescan(struct work_struct
*work
)
627 struct mmc_host
*host
=
628 container_of(work
, struct mmc_host
, detect
.work
);
634 if (host
->bus_ops
== NULL
) {
636 * Only we can add a new handler, so it's safe to
637 * release the lock here.
641 mmc_claim_host(host
);
646 mmc_send_if_cond(host
, host
->ocr_avail
);
649 * First we search for SDIO...
651 err
= mmc_send_io_op_cond(host
, 0, &ocr
);
653 if (mmc_attach_sdio(host
, ocr
))
659 * ...then normal SD...
661 err
= mmc_send_app_op_cond(host
, 0, &ocr
);
663 if (mmc_attach_sd(host
, ocr
))
669 * ...and finally MMC.
671 err
= mmc_send_op_cond(host
, 0, &ocr
);
673 if (mmc_attach_mmc(host
, ocr
))
678 mmc_release_host(host
);
681 if (host
->bus_ops
->detect
&& !host
->bus_dead
)
682 host
->bus_ops
->detect(host
);
688 void mmc_start_host(struct mmc_host
*host
)
691 mmc_detect_change(host
, 0);
694 void mmc_stop_host(struct mmc_host
*host
)
696 #ifdef CONFIG_MMC_DEBUG
698 spin_lock_irqsave(&host
->lock
, flags
);
700 spin_unlock_irqrestore(&host
->lock
, flags
);
703 mmc_flush_scheduled_work();
706 if (host
->bus_ops
&& !host
->bus_dead
) {
707 if (host
->bus_ops
->remove
)
708 host
->bus_ops
->remove(host
);
710 mmc_claim_host(host
);
711 mmc_detach_bus(host
);
712 mmc_release_host(host
);
724 * mmc_suspend_host - suspend a host
726 * @state: suspend mode (PM_SUSPEND_xxx)
728 int mmc_suspend_host(struct mmc_host
*host
, pm_message_t state
)
730 mmc_flush_scheduled_work();
733 if (host
->bus_ops
&& !host
->bus_dead
) {
734 if (host
->bus_ops
->suspend
)
735 host
->bus_ops
->suspend(host
);
736 if (!host
->bus_ops
->resume
) {
737 if (host
->bus_ops
->remove
)
738 host
->bus_ops
->remove(host
);
740 mmc_claim_host(host
);
741 mmc_detach_bus(host
);
742 mmc_release_host(host
);
752 EXPORT_SYMBOL(mmc_suspend_host
);
755 * mmc_resume_host - resume a previously suspended host
758 int mmc_resume_host(struct mmc_host
*host
)
761 if (host
->bus_ops
&& !host
->bus_dead
) {
763 BUG_ON(!host
->bus_ops
->resume
);
764 host
->bus_ops
->resume(host
);
769 * We add a slight delay here so that resume can progress
772 mmc_detect_change(host
, 1);
777 EXPORT_SYMBOL(mmc_resume_host
);
781 static int __init
mmc_init(void)
785 workqueue
= create_singlethread_workqueue("kmmcd");
789 ret
= mmc_register_bus();
791 goto destroy_workqueue
;
793 ret
= mmc_register_host_class();
797 ret
= sdio_register_bus();
799 goto unregister_host_class
;
803 unregister_host_class
:
804 mmc_unregister_host_class();
806 mmc_unregister_bus();
808 destroy_workqueue(workqueue
);
813 static void __exit
mmc_exit(void)
815 sdio_unregister_bus();
816 mmc_unregister_host_class();
817 mmc_unregister_bus();
818 destroy_workqueue(workqueue
);
821 subsys_initcall(mmc_init
);
822 module_exit(mmc_exit
);
824 MODULE_LICENSE("GPL");