memcg: bugfix for memory hotplug
[linux-2.6/zen-sources.git] / drivers / mmc / core / core.c
blobf7284b905eb38c083882eaec993885aff5598b2c
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-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>
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 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.
45 int use_spi_crc = 1;
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,
52 unsigned long delay)
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);
65 /**
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;
76 int err = cmd->error;
78 if (err && cmd->retries && mmc_host_is_spi(host)) {
79 if (cmd->resp[0] & R1_SPI_ILLEGAL_COMMAND)
80 cmd->retries = 0;
83 if (err && cmd->retries) {
84 pr_debug("%s: req failed (CMD%u): %d, retrying...\n",
85 mmc_hostname(host), cmd->opcode, err);
87 cmd->retries--;
88 cmd->error = 0;
89 host->ops->request(host, mrq);
90 } else {
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]);
98 if (mrq->data) {
99 pr_debug("%s: %d bytes transferred: %d\n",
100 mmc_hostname(host),
101 mrq->data->bytes_xfered, mrq->data->error);
104 if (mrq->stop) {
105 pr_debug("%s: (CMD%u): %d: %08x %08x %08x %08x\n",
106 mmc_hostname(host), mrq->stop->opcode,
107 mrq->stop->error,
108 mrq->stop->resp[0], mrq->stop->resp[1],
109 mrq->stop->resp[2], mrq->stop->resp[3]);
112 if (mrq->done)
113 mrq->done(mrq);
117 EXPORT_SYMBOL(mmc_request_done);
119 static void
120 mmc_start_request(struct mmc_host *host, struct mmc_request *mrq)
122 #ifdef CONFIG_MMC_DEBUG
123 unsigned int i, sz;
124 struct scatterlist *sg;
125 #endif
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);
131 if (mrq->data) {
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);
140 if (mrq->stop) {
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);
150 mrq->cmd->error = 0;
151 mrq->cmd->mrq = mrq;
152 if (mrq->data) {
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 >
156 host->max_req_size);
158 #ifdef CONFIG_MMC_DEBUG
159 sz = 0;
160 for_each_sg(mrq->data->sg, sg, mrq->data->sg_len, i)
161 sz += sg->length;
162 BUG_ON(sz != mrq->data->blocks * mrq->data->blksz);
163 #endif
165 mrq->cmd->data = mrq->data;
166 mrq->data->error = 0;
167 mrq->data->mrq = mrq;
168 if (mrq->stop) {
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
189 * response.
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;
226 mrq.cmd = cmd;
227 cmd->data = NULL;
229 mmc_wait_for_req(host, &mrq);
231 return cmd->error;
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)
246 unsigned int mult;
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;
254 return;
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)
284 * The limit is really 250 ms, but that is
285 * insufficient for some crappy cards.
287 limit_us = 300000;
288 else
289 limit_us = 100000;
292 * SDHC cards always use these fixed values.
294 if (timeout_us > limit_us || mmc_card_blockaddr(card)) {
295 data->timeout_ns = limit_us * 1000;
296 data->timeout_clks = 0;
300 EXPORT_SYMBOL(mmc_set_data_timeout);
303 * mmc_align_data_size - pads a transfer size to a more optimal value
304 * @card: the MMC card associated with the data transfer
305 * @sz: original transfer size
307 * Pads the original data size with a number of extra bytes in
308 * order to avoid controller bugs and/or performance hits
309 * (e.g. some controllers revert to PIO for certain sizes).
311 * Returns the improved size, which might be unmodified.
313 * Note that this function is only relevant when issuing a
314 * single scatter gather entry.
316 unsigned int mmc_align_data_size(struct mmc_card *card, unsigned int sz)
319 * FIXME: We don't have a system for the controller to tell
320 * the core about its problems yet, so for now we just 32-bit
321 * align the size.
323 sz = ((sz + 3) / 4) * 4;
325 return sz;
327 EXPORT_SYMBOL(mmc_align_data_size);
330 * __mmc_claim_host - exclusively claim a host
331 * @host: mmc host to claim
332 * @abort: whether or not the operation should be aborted
334 * Claim a host for a set of operations. If @abort is non null and
335 * dereference a non-zero value then this will return prematurely with
336 * that non-zero value without acquiring the lock. Returns zero
337 * with the lock held otherwise.
339 int __mmc_claim_host(struct mmc_host *host, atomic_t *abort)
341 DECLARE_WAITQUEUE(wait, current);
342 unsigned long flags;
343 int stop;
345 might_sleep();
347 add_wait_queue(&host->wq, &wait);
348 spin_lock_irqsave(&host->lock, flags);
349 while (1) {
350 set_current_state(TASK_UNINTERRUPTIBLE);
351 stop = abort ? atomic_read(abort) : 0;
352 if (stop || !host->claimed)
353 break;
354 spin_unlock_irqrestore(&host->lock, flags);
355 schedule();
356 spin_lock_irqsave(&host->lock, flags);
358 set_current_state(TASK_RUNNING);
359 if (!stop)
360 host->claimed = 1;
361 else
362 wake_up(&host->wq);
363 spin_unlock_irqrestore(&host->lock, flags);
364 remove_wait_queue(&host->wq, &wait);
365 return stop;
368 EXPORT_SYMBOL(__mmc_claim_host);
371 * mmc_release_host - release a host
372 * @host: mmc host to release
374 * Release a MMC host, allowing others to claim the host
375 * for their operations.
377 void mmc_release_host(struct mmc_host *host)
379 unsigned long flags;
381 WARN_ON(!host->claimed);
383 spin_lock_irqsave(&host->lock, flags);
384 host->claimed = 0;
385 spin_unlock_irqrestore(&host->lock, flags);
387 wake_up(&host->wq);
390 EXPORT_SYMBOL(mmc_release_host);
393 * Internal function that does the actual ios call to the host driver,
394 * optionally printing some debug output.
396 static inline void mmc_set_ios(struct mmc_host *host)
398 struct mmc_ios *ios = &host->ios;
400 pr_debug("%s: clock %uHz busmode %u powermode %u cs %u Vdd %u "
401 "width %u timing %u\n",
402 mmc_hostname(host), ios->clock, ios->bus_mode,
403 ios->power_mode, ios->chip_select, ios->vdd,
404 ios->bus_width, ios->timing);
406 host->ops->set_ios(host, ios);
410 * Control chip select pin on a host.
412 void mmc_set_chip_select(struct mmc_host *host, int mode)
414 host->ios.chip_select = mode;
415 mmc_set_ios(host);
419 * Sets the host clock to the highest possible frequency that
420 * is below "hz".
422 void mmc_set_clock(struct mmc_host *host, unsigned int hz)
424 WARN_ON(hz < host->f_min);
426 if (hz > host->f_max)
427 hz = host->f_max;
429 host->ios.clock = hz;
430 mmc_set_ios(host);
434 * Change the bus mode (open drain/push-pull) of a host.
436 void mmc_set_bus_mode(struct mmc_host *host, unsigned int mode)
438 host->ios.bus_mode = mode;
439 mmc_set_ios(host);
443 * Change data bus width of a host.
445 void mmc_set_bus_width(struct mmc_host *host, unsigned int width)
447 host->ios.bus_width = width;
448 mmc_set_ios(host);
452 * Mask off any voltages we don't support and select
453 * the lowest voltage
455 u32 mmc_select_voltage(struct mmc_host *host, u32 ocr)
457 int bit;
459 ocr &= host->ocr_avail;
461 bit = ffs(ocr);
462 if (bit) {
463 bit -= 1;
465 ocr &= 3 << bit;
467 host->ios.vdd = bit;
468 mmc_set_ios(host);
469 } else {
470 ocr = 0;
473 return ocr;
477 * Select timing parameters for host.
479 void mmc_set_timing(struct mmc_host *host, unsigned int timing)
481 host->ios.timing = timing;
482 mmc_set_ios(host);
486 * Apply power to the MMC stack. This is a two-stage process.
487 * First, we enable power to the card without the clock running.
488 * We then wait a bit for the power to stabilise. Finally,
489 * enable the bus drivers and clock to the card.
491 * We must _NOT_ enable the clock prior to power stablising.
493 * If a host does all the power sequencing itself, ignore the
494 * initial MMC_POWER_UP stage.
496 static void mmc_power_up(struct mmc_host *host)
498 int bit = fls(host->ocr_avail) - 1;
500 host->ios.vdd = bit;
501 if (mmc_host_is_spi(host)) {
502 host->ios.chip_select = MMC_CS_HIGH;
503 host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
504 } else {
505 host->ios.chip_select = MMC_CS_DONTCARE;
506 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
508 host->ios.power_mode = MMC_POWER_UP;
509 host->ios.bus_width = MMC_BUS_WIDTH_1;
510 host->ios.timing = MMC_TIMING_LEGACY;
511 mmc_set_ios(host);
514 * This delay should be sufficient to allow the power supply
515 * to reach the minimum voltage.
517 mmc_delay(2);
519 host->ios.clock = host->f_min;
520 host->ios.power_mode = MMC_POWER_ON;
521 mmc_set_ios(host);
524 * This delay must be at least 74 clock sizes, or 1 ms, or the
525 * time required to reach a stable voltage.
527 mmc_delay(2);
530 static void mmc_power_off(struct mmc_host *host)
532 host->ios.clock = 0;
533 host->ios.vdd = 0;
534 if (!mmc_host_is_spi(host)) {
535 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
536 host->ios.chip_select = MMC_CS_DONTCARE;
538 host->ios.power_mode = MMC_POWER_OFF;
539 host->ios.bus_width = MMC_BUS_WIDTH_1;
540 host->ios.timing = MMC_TIMING_LEGACY;
541 mmc_set_ios(host);
545 * Cleanup when the last reference to the bus operator is dropped.
547 static void __mmc_release_bus(struct mmc_host *host)
549 BUG_ON(!host);
550 BUG_ON(host->bus_refs);
551 BUG_ON(!host->bus_dead);
553 host->bus_ops = NULL;
557 * Increase reference count of bus operator
559 static inline void mmc_bus_get(struct mmc_host *host)
561 unsigned long flags;
563 spin_lock_irqsave(&host->lock, flags);
564 host->bus_refs++;
565 spin_unlock_irqrestore(&host->lock, flags);
569 * Decrease reference count of bus operator and free it if
570 * it is the last reference.
572 static inline void mmc_bus_put(struct mmc_host *host)
574 unsigned long flags;
576 spin_lock_irqsave(&host->lock, flags);
577 host->bus_refs--;
578 if ((host->bus_refs == 0) && host->bus_ops)
579 __mmc_release_bus(host);
580 spin_unlock_irqrestore(&host->lock, flags);
584 * Assign a mmc bus handler to a host. Only one bus handler may control a
585 * host at any given time.
587 void mmc_attach_bus(struct mmc_host *host, const struct mmc_bus_ops *ops)
589 unsigned long flags;
591 BUG_ON(!host);
592 BUG_ON(!ops);
594 WARN_ON(!host->claimed);
596 spin_lock_irqsave(&host->lock, flags);
598 BUG_ON(host->bus_ops);
599 BUG_ON(host->bus_refs);
601 host->bus_ops = ops;
602 host->bus_refs = 1;
603 host->bus_dead = 0;
605 spin_unlock_irqrestore(&host->lock, flags);
609 * Remove the current bus handler from a host. Assumes that there are
610 * no interesting cards left, so the bus is powered down.
612 void mmc_detach_bus(struct mmc_host *host)
614 unsigned long flags;
616 BUG_ON(!host);
618 WARN_ON(!host->claimed);
619 WARN_ON(!host->bus_ops);
621 spin_lock_irqsave(&host->lock, flags);
623 host->bus_dead = 1;
625 spin_unlock_irqrestore(&host->lock, flags);
627 mmc_power_off(host);
629 mmc_bus_put(host);
633 * mmc_detect_change - process change of state on a MMC socket
634 * @host: host which changed state.
635 * @delay: optional delay to wait before detection (jiffies)
637 * MMC drivers should call this when they detect a card has been
638 * inserted or removed. The MMC layer will confirm that any
639 * present card is still functional, and initialize any newly
640 * inserted.
642 void mmc_detect_change(struct mmc_host *host, unsigned long delay)
644 #ifdef CONFIG_MMC_DEBUG
645 unsigned long flags;
646 spin_lock_irqsave(&host->lock, flags);
647 WARN_ON(host->removed);
648 spin_unlock_irqrestore(&host->lock, flags);
649 #endif
651 mmc_schedule_delayed_work(&host->detect, delay);
654 EXPORT_SYMBOL(mmc_detect_change);
657 void mmc_rescan(struct work_struct *work)
659 struct mmc_host *host =
660 container_of(work, struct mmc_host, detect.work);
661 u32 ocr;
662 int err;
664 mmc_bus_get(host);
666 if (host->bus_ops == NULL) {
668 * Only we can add a new handler, so it's safe to
669 * release the lock here.
671 mmc_bus_put(host);
673 if (host->ops->get_cd && host->ops->get_cd(host) == 0)
674 goto out;
676 mmc_claim_host(host);
678 mmc_power_up(host);
679 mmc_go_idle(host);
681 mmc_send_if_cond(host, host->ocr_avail);
684 * First we search for SDIO...
686 err = mmc_send_io_op_cond(host, 0, &ocr);
687 if (!err) {
688 if (mmc_attach_sdio(host, ocr))
689 mmc_power_off(host);
690 goto out;
694 * ...then normal SD...
696 err = mmc_send_app_op_cond(host, 0, &ocr);
697 if (!err) {
698 if (mmc_attach_sd(host, ocr))
699 mmc_power_off(host);
700 goto out;
704 * ...and finally MMC.
706 err = mmc_send_op_cond(host, 0, &ocr);
707 if (!err) {
708 if (mmc_attach_mmc(host, ocr))
709 mmc_power_off(host);
710 goto out;
713 mmc_release_host(host);
714 mmc_power_off(host);
715 } else {
716 if (host->bus_ops->detect && !host->bus_dead)
717 host->bus_ops->detect(host);
719 mmc_bus_put(host);
721 out:
722 if (host->caps & MMC_CAP_NEEDS_POLL)
723 mmc_schedule_delayed_work(&host->detect, HZ);
726 void mmc_start_host(struct mmc_host *host)
728 mmc_power_off(host);
729 mmc_detect_change(host, 0);
732 void mmc_stop_host(struct mmc_host *host)
734 #ifdef CONFIG_MMC_DEBUG
735 unsigned long flags;
736 spin_lock_irqsave(&host->lock, flags);
737 host->removed = 1;
738 spin_unlock_irqrestore(&host->lock, flags);
739 #endif
741 mmc_flush_scheduled_work();
743 mmc_bus_get(host);
744 if (host->bus_ops && !host->bus_dead) {
745 if (host->bus_ops->remove)
746 host->bus_ops->remove(host);
748 mmc_claim_host(host);
749 mmc_detach_bus(host);
750 mmc_release_host(host);
752 mmc_bus_put(host);
754 BUG_ON(host->card);
756 mmc_power_off(host);
759 #ifdef CONFIG_PM
762 * mmc_suspend_host - suspend a host
763 * @host: mmc host
764 * @state: suspend mode (PM_SUSPEND_xxx)
766 int mmc_suspend_host(struct mmc_host *host, pm_message_t state)
768 mmc_flush_scheduled_work();
770 mmc_bus_get(host);
771 if (host->bus_ops && !host->bus_dead) {
772 if (host->bus_ops->suspend)
773 host->bus_ops->suspend(host);
774 if (!host->bus_ops->resume) {
775 if (host->bus_ops->remove)
776 host->bus_ops->remove(host);
778 mmc_claim_host(host);
779 mmc_detach_bus(host);
780 mmc_release_host(host);
783 mmc_bus_put(host);
785 mmc_power_off(host);
787 return 0;
790 EXPORT_SYMBOL(mmc_suspend_host);
793 * mmc_resume_host - resume a previously suspended host
794 * @host: mmc host
796 int mmc_resume_host(struct mmc_host *host)
798 mmc_bus_get(host);
799 if (host->bus_ops && !host->bus_dead) {
800 mmc_power_up(host);
801 BUG_ON(!host->bus_ops->resume);
802 host->bus_ops->resume(host);
804 mmc_bus_put(host);
807 * We add a slight delay here so that resume can progress
808 * in parallel.
810 mmc_detect_change(host, 1);
812 return 0;
815 EXPORT_SYMBOL(mmc_resume_host);
817 #endif
819 static int __init mmc_init(void)
821 int ret;
823 workqueue = create_singlethread_workqueue("kmmcd");
824 if (!workqueue)
825 return -ENOMEM;
827 ret = mmc_register_bus();
828 if (ret)
829 goto destroy_workqueue;
831 ret = mmc_register_host_class();
832 if (ret)
833 goto unregister_bus;
835 ret = sdio_register_bus();
836 if (ret)
837 goto unregister_host_class;
839 return 0;
841 unregister_host_class:
842 mmc_unregister_host_class();
843 unregister_bus:
844 mmc_unregister_bus();
845 destroy_workqueue:
846 destroy_workqueue(workqueue);
848 return ret;
851 static void __exit mmc_exit(void)
853 sdio_unregister_bus();
854 mmc_unregister_host_class();
855 mmc_unregister_bus();
856 destroy_workqueue(workqueue);
859 subsys_initcall(mmc_init);
860 module_exit(mmc_exit);
862 MODULE_LICENSE("GPL");