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>
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
124 struct scatterlist *sg;
127 pr_debug("%s: starting CMD%u arg %08x flags %08x\n",
128 mmc_hostname(host), mrq->cmd->opcode,
129 mrq->cmd->arg, mrq->cmd->flags);
132 pr_debug("%s: blksz %d blocks %d flags %08x "
133 "tsac %d ms nsac %d\n",
134 mmc_hostname(host), mrq->data->blksz,
135 mrq->data->blocks, mrq->data->flags,
136 mrq->data->timeout_ns / 1000000,
137 mrq->data->timeout_clks);
141 pr_debug("%s: CMD%u arg %08x flags %08x\n",
142 mmc_hostname(host), mrq->stop->opcode,
143 mrq->stop->arg, mrq->stop->flags);
146 WARN_ON(!host->claimed);
148 led_trigger_event(host->led, LED_FULL);
153 BUG_ON(mrq->data->blksz > host->max_blk_size);
154 BUG_ON(mrq->data->blocks > host->max_blk_count);
155 BUG_ON(mrq->data->blocks * mrq->data->blksz >
158 #ifdef CONFIG_MMC_DEBUG
160 for_each_sg(mrq->data->sg, sg, mrq->data->sg_len, i)
162 BUG_ON(sz != mrq->data->blocks * mrq->data->blksz);
165 mrq->cmd->data = mrq->data;
166 mrq->data->error = 0;
167 mrq->data->mrq = mrq;
169 mrq->data->stop = mrq->stop;
170 mrq->stop->error = 0;
171 mrq->stop->mrq = mrq;
174 host->ops->request(host, mrq);
177 static void mmc_wait_done(struct mmc_request *mrq)
179 complete(mrq->done_data);
183 * mmc_wait_for_req - start a request and wait for completion
184 * @host: MMC host to start command
185 * @mrq: MMC request to start
187 * Start a new MMC custom command request for a host, and wait
188 * for the command to complete. Does not attempt to parse the
191 void mmc_wait_for_req(struct mmc_host *host, struct mmc_request *mrq)
193 DECLARE_COMPLETION_ONSTACK(complete);
195 mrq->done_data = &complete;
196 mrq->done = mmc_wait_done;
198 mmc_start_request(host, mrq);
200 wait_for_completion(&complete);
203 EXPORT_SYMBOL(mmc_wait_for_req);
206 * mmc_wait_for_cmd - start a command and wait for completion
207 * @host: MMC host to start command
208 * @cmd: MMC command to start
209 * @retries: maximum number of retries
211 * Start a new MMC command for a host, and wait for the command
212 * to complete. Return any error that occurred while the command
213 * was executing. Do not attempt to parse the response.
215 int mmc_wait_for_cmd(struct mmc_host *host, struct mmc_command *cmd, int retries)
217 struct mmc_request mrq;
219 WARN_ON(!host->claimed);
221 memset(&mrq, 0, sizeof(struct mmc_request));
223 memset(cmd->resp, 0, sizeof(cmd->resp));
224 cmd->retries = retries;
229 mmc_wait_for_req(host, &mrq);
234 EXPORT_SYMBOL(mmc_wait_for_cmd);
237 * mmc_set_data_timeout - set the timeout for a data command
238 * @data: data phase for command
239 * @card: the MMC card associated with the data transfer
241 * Computes the data timeout parameters according to the
242 * correct algorithm given the card type.
244 void mmc_set_data_timeout(struct mmc_data *data, const struct mmc_card *card)
249 * SDIO cards only define an upper 1 s limit on access.
251 if (mmc_card_sdio(card)) {
252 data->timeout_ns = 1000000000;
253 data->timeout_clks = 0;
258 * SD cards use a 100 multiplier rather than 10
260 mult = mmc_card_sd(card) ? 100 : 10;
263 * Scale up the multiplier (and therefore the timeout) by
264 * the r2w factor for writes.
266 if (data->flags & MMC_DATA_WRITE)
267 mult <<= card->csd.r2w_factor;
269 data->timeout_ns = card->csd.tacc_ns * mult;
270 data->timeout_clks = card->csd.tacc_clks * mult;
273 * SD cards also have an upper limit on the timeout.
275 if (mmc_card_sd(card)) {
276 unsigned int timeout_us, limit_us;
278 timeout_us = data->timeout_ns / 1000;
279 timeout_us += data->timeout_clks * 1000 /
280 (card->host->ios.clock / 1000);
282 if (data->flags & MMC_DATA_WRITE)
288 * SDHC cards always use these fixed values.
290 if (timeout_us > limit_us || mmc_card_blockaddr(card)) {
291 data->timeout_ns = limit_us * 1000;
292 data->timeout_clks = 0;
296 EXPORT_SYMBOL(mmc_set_data_timeout);
299 * mmc_align_data_size - pads a transfer size to a more optimal value
300 * @card: the MMC card associated with the data transfer
301 * @sz: original transfer size
303 * Pads the original data size with a number of extra bytes in
304 * order to avoid controller bugs and/or performance hits
305 * (e.g. some controllers revert to PIO for certain sizes).
307 * Returns the improved size, which might be unmodified.
309 * Note that this function is only relevant when issuing a
310 * single scatter gather entry.
312 unsigned int mmc_align_data_size(struct mmc_card *card, unsigned int sz)
315 * FIXME: We don't have a system for the controller to tell
316 * the core about its problems yet, so for now we just 32-bit
319 sz = ((sz + 3) / 4) * 4;
323 EXPORT_SYMBOL(mmc_align_data_size);
326 * __mmc_claim_host - exclusively claim a host
327 * @host: mmc host to claim
328 * @abort: whether or not the operation should be aborted
330 * Claim a host for a set of operations. If @abort is non null and
331 * dereference a non-zero value then this will return prematurely with
332 * that non-zero value without acquiring the lock. Returns zero
333 * with the lock held otherwise.
335 int __mmc_claim_host(struct mmc_host *host, atomic_t *abort)
337 DECLARE_WAITQUEUE(wait, current);
343 add_wait_queue(&host->wq, &wait);
344 spin_lock_irqsave(&host->lock, flags);
346 set_current_state(TASK_UNINTERRUPTIBLE);
347 stop = abort ? atomic_read(abort) : 0;
348 if (stop || !host->claimed)
350 spin_unlock_irqrestore(&host->lock, flags);
352 spin_lock_irqsave(&host->lock, flags);
354 set_current_state(TASK_RUNNING);
359 spin_unlock_irqrestore(&host->lock, flags);
360 remove_wait_queue(&host->wq, &wait);
364 EXPORT_SYMBOL(__mmc_claim_host);
367 * mmc_release_host - release a host
368 * @host: mmc host to release
370 * Release a MMC host, allowing others to claim the host
371 * for their operations.
373 void mmc_release_host(struct mmc_host *host)
377 WARN_ON(!host->claimed);
379 spin_lock_irqsave(&host->lock, flags);
381 spin_unlock_irqrestore(&host->lock, flags);
386 EXPORT_SYMBOL(mmc_release_host);
389 * Internal function that does the actual ios call to the host driver,
390 * optionally printing some debug output.
392 static inline void mmc_set_ios(struct mmc_host *host)
394 struct mmc_ios *ios = &host->ios;
396 pr_debug("%s: clock %uHz busmode %u powermode %u cs %u Vdd %u "
397 "width %u timing %u\n",
398 mmc_hostname(host), ios->clock, ios->bus_mode,
399 ios->power_mode, ios->chip_select, ios->vdd,
400 ios->bus_width, ios->timing);
402 host->ops->set_ios(host, ios);
406 * Control chip select pin on a host.
408 void mmc_set_chip_select(struct mmc_host *host, int mode)
410 host->ios.chip_select = mode;
415 * Sets the host clock to the highest possible frequency that
418 void mmc_set_clock(struct mmc_host *host, unsigned int hz)
420 WARN_ON(hz < host->f_min);
422 if (hz > host->f_max)
425 host->ios.clock = hz;
430 * Change the bus mode (open drain/push-pull) of a host.
432 void mmc_set_bus_mode(struct mmc_host *host, unsigned int mode)
434 host->ios.bus_mode = mode;
439 * Change data bus width of a host.
441 void mmc_set_bus_width(struct mmc_host *host, unsigned int width)
443 host->ios.bus_width = width;
448 * Mask off any voltages we don't support and select
451 u32 mmc_select_voltage(struct mmc_host *host, u32 ocr)
455 ocr &= host->ocr_avail;
473 * Select timing parameters for host.
475 void mmc_set_timing(struct mmc_host *host, unsigned int timing)
477 host->ios.timing = timing;
482 * Apply power to the MMC stack. This is a two-stage process.
483 * First, we enable power to the card without the clock running.
484 * We then wait a bit for the power to stabilise. Finally,
485 * enable the bus drivers and clock to the card.
487 * We must _NOT_ enable the clock prior to power stablising.
489 * If a host does all the power sequencing itself, ignore the
490 * initial MMC_POWER_UP stage.
492 static void mmc_power_up(struct mmc_host *host)
494 int bit = fls(host->ocr_avail) - 1;
497 if (mmc_host_is_spi(host)) {
498 host->ios.chip_select = MMC_CS_HIGH;
499 host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
501 host->ios.chip_select = MMC_CS_DONTCARE;
502 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
504 host->ios.power_mode = MMC_POWER_UP;
505 host->ios.bus_width = MMC_BUS_WIDTH_1;
506 host->ios.timing = MMC_TIMING_LEGACY;
510 * This delay should be sufficient to allow the power supply
511 * to reach the minimum voltage.
515 host->ios.clock = host->f_min;
516 host->ios.power_mode = MMC_POWER_ON;
520 * This delay must be at least 74 clock sizes, or 1 ms, or the
521 * time required to reach a stable voltage.
526 static void mmc_power_off(struct mmc_host *host)
530 if (!mmc_host_is_spi(host)) {
531 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
532 host->ios.chip_select = MMC_CS_DONTCARE;
534 host->ios.power_mode = MMC_POWER_OFF;
535 host->ios.bus_width = MMC_BUS_WIDTH_1;
536 host->ios.timing = MMC_TIMING_LEGACY;
541 * Cleanup when the last reference to the bus operator is dropped.
543 static void __mmc_release_bus(struct mmc_host *host)
546 BUG_ON(host->bus_refs);
547 BUG_ON(!host->bus_dead);
549 host->bus_ops = NULL;
553 * Increase reference count of bus operator
555 static inline void mmc_bus_get(struct mmc_host *host)
559 spin_lock_irqsave(&host->lock, flags);
561 spin_unlock_irqrestore(&host->lock, flags);
565 * Decrease reference count of bus operator and free it if
566 * it is the last reference.
568 static inline void mmc_bus_put(struct mmc_host *host)
572 spin_lock_irqsave(&host->lock, flags);
574 if ((host->bus_refs == 0) && host->bus_ops)
575 __mmc_release_bus(host);
576 spin_unlock_irqrestore(&host->lock, flags);
580 * Assign a mmc bus handler to a host. Only one bus handler may control a
581 * host at any given time.
583 void mmc_attach_bus(struct mmc_host *host, const struct mmc_bus_ops *ops)
590 WARN_ON(!host->claimed);
592 spin_lock_irqsave(&host->lock, flags);
594 BUG_ON(host->bus_ops);
595 BUG_ON(host->bus_refs);
601 spin_unlock_irqrestore(&host->lock, flags);
605 * Remove the current bus handler from a host. Assumes that there are
606 * no interesting cards left, so the bus is powered down.
608 void mmc_detach_bus(struct mmc_host *host)
614 WARN_ON(!host->claimed);
615 WARN_ON(!host->bus_ops);
617 spin_lock_irqsave(&host->lock, flags);
621 spin_unlock_irqrestore(&host->lock, flags);
629 * mmc_detect_change - process change of state on a MMC socket
630 * @host: host which changed state.
631 * @delay: optional delay to wait before detection (jiffies)
633 * MMC drivers should call this when they detect a card has been
634 * inserted or removed. The MMC layer will confirm that any
635 * present card is still functional, and initialize any newly
638 void mmc_detect_change(struct mmc_host *host, unsigned long delay)
640 #ifdef CONFIG_MMC_DEBUG
642 spin_lock_irqsave(&host->lock, flags);
643 WARN_ON(host->removed);
644 spin_unlock_irqrestore(&host->lock, flags);
647 mmc_schedule_delayed_work(&host->detect, delay);
650 EXPORT_SYMBOL(mmc_detect_change);
653 void mmc_rescan(struct work_struct *work)
655 struct mmc_host *host =
656 container_of(work, struct mmc_host, detect.work);
662 if (host->bus_ops == NULL) {
664 * Only we can add a new handler, so it's safe to
665 * release the lock here.
669 if (host->ops->get_cd && host->ops->get_cd(host) == 0)
672 mmc_claim_host(host);
677 mmc_send_if_cond(host, host->ocr_avail);
680 * First we search for SDIO...
682 err = mmc_send_io_op_cond(host, 0, &ocr);
684 if (mmc_attach_sdio(host, ocr))
690 * ...then normal SD...
692 err = mmc_send_app_op_cond(host, 0, &ocr);
694 if (mmc_attach_sd(host, ocr))
700 * ...and finally MMC.
702 err = mmc_send_op_cond(host, 0, &ocr);
704 if (mmc_attach_mmc(host, ocr))
709 mmc_release_host(host);
712 if (host->bus_ops->detect && !host->bus_dead)
713 host->bus_ops->detect(host);
718 if (host->caps & MMC_CAP_NEEDS_POLL)
719 mmc_schedule_delayed_work(&host->detect, HZ);
722 void mmc_start_host(struct mmc_host *host)
725 mmc_detect_change(host, 0);
728 void mmc_stop_host(struct mmc_host *host)
730 #ifdef CONFIG_MMC_DEBUG
732 spin_lock_irqsave(&host->lock, flags);
734 spin_unlock_irqrestore(&host->lock, flags);
737 mmc_flush_scheduled_work();
740 if (host->bus_ops && !host->bus_dead) {
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);
758 * mmc_suspend_host - suspend a host
760 * @state: suspend mode (PM_SUSPEND_xxx)
762 int mmc_suspend_host(struct mmc_host *host, pm_message_t state)
764 mmc_flush_scheduled_work();
767 if (host->bus_ops && !host->bus_dead) {
768 if (host->bus_ops->suspend)
769 host->bus_ops->suspend(host);
770 if (!host->bus_ops->resume) {
771 if (host->bus_ops->remove)
772 host->bus_ops->remove(host);
774 mmc_claim_host(host);
775 mmc_detach_bus(host);
776 mmc_release_host(host);
786 EXPORT_SYMBOL(mmc_suspend_host);
789 * mmc_resume_host - resume a previously suspended host
792 int mmc_resume_host(struct mmc_host *host)
795 if (host->bus_ops && !host->bus_dead) {
797 BUG_ON(!host->bus_ops->resume);
798 host->bus_ops->resume(host);
803 * We add a slight delay here so that resume can progress
806 mmc_detect_change(host, 1);
811 EXPORT_SYMBOL(mmc_resume_host);
815 static int __init mmc_init(void)
819 workqueue = create_singlethread_workqueue("kmmcd");
823 ret = mmc_register_bus();
825 goto destroy_workqueue;
827 ret = mmc_register_host_class();
831 ret = sdio_register_bus();
833 goto unregister_host_class;
837 unregister_host_class:
838 mmc_unregister_host_class();
840 mmc_unregister_bus();
842 destroy_workqueue(workqueue);
847 static void __exit mmc_exit(void)
849 sdio_unregister_bus();
850 mmc_unregister_host_class();
851 mmc_unregister_bus();
852 destroy_workqueue(workqueue);
855 subsys_initcall(mmc_init);
856 module_exit(mmc_exit);
858 MODULE_LICENSE("GPL");