Committer: Michael Beasley <mike@snafu.setup>
[mikesnafu-overlay.git] / drivers / mmc / core / core.c
blobb96667448eb5618ffcfae4e02e15cfaeca52146f
1 /*
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>
29 #include "core.h"
30 #include "bus.h"
31 #include "host.h"
32 #include "sdio_bus.h"
34 #include "mmc_ops.h"
35 #include "sd_ops.h"
36 #include "sdio_ops.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.
49 int use_spi_crc = 1;
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,
56 unsigned long delay)
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);
69 /**
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;
80 int err = cmd->error;
82 if (err && cmd->retries && mmc_host_is_spi(host)) {
83 if (cmd->resp[0] & R1_SPI_ILLEGAL_COMMAND)
84 cmd->retries = 0;
87 if (err && cmd->retries) {
88 pr_debug("%s: req failed (CMD%u): %d, retrying...\n",
89 mmc_hostname(host), cmd->opcode, err);
91 cmd->retries--;
92 cmd->error = 0;
93 host->ops->request(host, mrq);
94 } else {
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]);
102 if (mrq->data) {
103 pr_debug("%s: %d bytes transferred: %d\n",
104 mmc_hostname(host),
105 mrq->data->bytes_xfered, mrq->data->error);
108 if (mrq->stop) {
109 pr_debug("%s: (CMD%u): %d: %08x %08x %08x %08x\n",
110 mmc_hostname(host), mrq->stop->opcode,
111 mrq->stop->error,
112 mrq->stop->resp[0], mrq->stop->resp[1],
113 mrq->stop->resp[2], mrq->stop->resp[3]);
116 if (mrq->done)
117 mrq->done(mrq);
121 EXPORT_SYMBOL(mmc_request_done);
123 static void
124 mmc_start_request(struct mmc_host *host, struct mmc_request *mrq)
126 #ifdef CONFIG_MMC_DEBUG
127 unsigned int i, sz;
128 #endif
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);
134 if (mrq->data) {
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);
143 if (mrq->stop) {
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);
153 mrq->cmd->error = 0;
154 mrq->cmd->mrq = mrq;
155 if (mrq->data) {
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 >
159 host->max_req_size);
161 #ifdef CONFIG_MMC_DEBUG
162 sz = 0;
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);
166 #endif
168 mrq->cmd->data = mrq->data;
169 mrq->data->error = 0;
170 mrq->data->mrq = mrq;
171 if (mrq->stop) {
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
192 * response.
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;
229 mrq.cmd = cmd;
230 cmd->data = NULL;
232 mmc_wait_for_req(host, &mrq);
234 return cmd->error;
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)
249 unsigned int mult;
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;
257 return;
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)
286 limit_us = 250000;
287 else
288 limit_us = 100000;
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);
314 unsigned long flags;
315 int stop;
317 might_sleep();
319 add_wait_queue(&host->wq, &wait);
320 spin_lock_irqsave(&host->lock, flags);
321 while (1) {
322 set_current_state(TASK_UNINTERRUPTIBLE);
323 stop = abort ? atomic_read(abort) : 0;
324 if (stop || !host->claimed)
325 break;
326 spin_unlock_irqrestore(&host->lock, flags);
327 schedule();
328 spin_lock_irqsave(&host->lock, flags);
330 set_current_state(TASK_RUNNING);
331 if (!stop)
332 host->claimed = 1;
333 else
334 wake_up(&host->wq);
335 spin_unlock_irqrestore(&host->lock, flags);
336 remove_wait_queue(&host->wq, &wait);
337 return stop;
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)
351 unsigned long flags;
353 WARN_ON(!host->claimed);
355 spin_lock_irqsave(&host->lock, flags);
356 host->claimed = 0;
357 spin_unlock_irqrestore(&host->lock, flags);
359 wake_up(&host->wq);
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;
387 mmc_set_ios(host);
391 * Sets the host clock to the highest possible frequency that
392 * is below "hz".
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)
399 hz = host->f_max;
401 host->ios.clock = hz;
402 mmc_set_ios(host);
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;
411 mmc_set_ios(host);
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;
420 mmc_set_ios(host);
424 * Mask off any voltages we don't support and select
425 * the lowest voltage
427 u32 mmc_select_voltage(struct mmc_host *host, u32 ocr)
429 int bit;
431 ocr &= host->ocr_avail;
433 bit = ffs(ocr);
434 if (bit) {
435 bit -= 1;
437 ocr &= 3 << bit;
439 host->ios.vdd = bit;
440 mmc_set_ios(host);
441 } else {
442 ocr = 0;
445 return ocr;
449 * Select timing parameters for host.
451 void mmc_set_timing(struct mmc_host *host, unsigned int timing)
453 host->ios.timing = timing;
454 mmc_set_ios(host);
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;
472 host->ios.vdd = bit;
473 if (mmc_host_is_spi(host)) {
474 host->ios.chip_select = MMC_CS_HIGH;
475 host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
476 } else {
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;
483 mmc_set_ios(host);
486 * This delay should be sufficient to allow the power supply
487 * to reach the minimum voltage.
489 mmc_delay(2);
491 host->ios.clock = host->f_min;
492 host->ios.power_mode = MMC_POWER_ON;
493 mmc_set_ios(host);
496 * This delay must be at least 74 clock sizes, or 1 ms, or the
497 * time required to reach a stable voltage.
499 mmc_delay(2);
502 static void mmc_power_off(struct mmc_host *host)
504 host->ios.clock = 0;
505 host->ios.vdd = 0;
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;
513 mmc_set_ios(host);
517 * Cleanup when the last reference to the bus operator is dropped.
519 void __mmc_release_bus(struct mmc_host *host)
521 BUG_ON(!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)
533 unsigned long flags;
535 spin_lock_irqsave(&host->lock, flags);
536 host->bus_refs++;
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)
546 unsigned long flags;
548 spin_lock_irqsave(&host->lock, flags);
549 host->bus_refs--;
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)
561 unsigned long flags;
563 BUG_ON(!host);
564 BUG_ON(!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);
573 host->bus_ops = ops;
574 host->bus_refs = 1;
575 host->bus_dead = 0;
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)
586 unsigned long flags;
588 BUG_ON(!host);
590 WARN_ON(!host->claimed);
591 WARN_ON(!host->bus_ops);
593 spin_lock_irqsave(&host->lock, flags);
595 host->bus_dead = 1;
597 spin_unlock_irqrestore(&host->lock, flags);
599 mmc_power_off(host);
601 mmc_bus_put(host);
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
612 * inserted.
614 void mmc_detect_change(struct mmc_host *host, unsigned long delay)
616 #ifdef CONFIG_MMC_DEBUG
617 unsigned long flags;
618 spin_lock_irqsave(&host->lock, flags);
619 WARN_ON(host->removed);
620 spin_unlock_irqrestore(&host->lock, flags);
621 #endif
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);
633 u32 ocr;
634 int err;
636 mmc_bus_get(host);
638 if (host->bus_ops == NULL) {
640 * Only we can add a new handler, so it's safe to
641 * release the lock here.
643 mmc_bus_put(host);
645 mmc_claim_host(host);
647 mmc_power_up(host);
648 mmc_go_idle(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);
656 if (!err) {
657 if (mmc_attach_sdio(host, ocr))
658 mmc_power_off(host);
659 return;
663 * ...then normal SD...
665 err = mmc_send_app_op_cond(host, 0, &ocr);
666 if (!err) {
667 if (mmc_attach_sd(host, ocr))
668 mmc_power_off(host);
669 return;
673 * ...and finally MMC.
675 err = mmc_send_op_cond(host, 0, &ocr);
676 if (!err) {
677 if (mmc_attach_mmc(host, ocr))
678 mmc_power_off(host);
679 return;
682 mmc_release_host(host);
683 mmc_power_off(host);
684 } else {
685 if (host->bus_ops->detect && !host->bus_dead)
686 host->bus_ops->detect(host);
688 mmc_bus_put(host);
692 void mmc_start_host(struct mmc_host *host)
694 mmc_power_off(host);
695 mmc_detect_change(host, 0);
698 void mmc_stop_host(struct mmc_host *host)
700 #ifdef CONFIG_MMC_DEBUG
701 unsigned long flags;
702 spin_lock_irqsave(&host->lock, flags);
703 host->removed = 1;
704 spin_unlock_irqrestore(&host->lock, flags);
705 #endif
707 mmc_flush_scheduled_work();
709 mmc_bus_get(host);
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);
718 mmc_bus_put(host);
720 BUG_ON(host->card);
722 mmc_power_off(host);
725 #ifdef CONFIG_PM
728 * mmc_suspend_host - suspend a host
729 * @host: mmc 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();
736 mmc_bus_get(host);
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);
749 mmc_bus_put(host);
751 mmc_power_off(host);
753 return 0;
756 EXPORT_SYMBOL(mmc_suspend_host);
759 * mmc_resume_host - resume a previously suspended host
760 * @host: mmc host
762 int mmc_resume_host(struct mmc_host *host)
764 mmc_bus_get(host);
765 if (host->bus_ops && !host->bus_dead) {
766 mmc_power_up(host);
767 BUG_ON(!host->bus_ops->resume);
768 host->bus_ops->resume(host);
770 mmc_bus_put(host);
773 * We add a slight delay here so that resume can progress
774 * in parallel.
776 mmc_detect_change(host, 1);
778 return 0;
781 EXPORT_SYMBOL(mmc_resume_host);
783 #endif
785 static int __init mmc_init(void)
787 int ret;
789 workqueue = create_singlethread_workqueue("kmmcd");
790 if (!workqueue)
791 return -ENOMEM;
793 ret = mmc_register_bus();
794 if (ret)
795 goto destroy_workqueue;
797 ret = mmc_register_host_class();
798 if (ret)
799 goto unregister_bus;
801 ret = sdio_register_bus();
802 if (ret)
803 goto unregister_host_class;
805 return 0;
807 unregister_host_class:
808 mmc_unregister_host_class();
809 unregister_bus:
810 mmc_unregister_bus();
811 destroy_workqueue:
812 destroy_workqueue(workqueue);
814 return ret;
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");