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>
23 #include <linux/log2.h>
24 #include <linux/regulator/consumer.h>
26 #include <linux/mmc/card.h>
27 #include <linux/mmc/host.h>
28 #include <linux/mmc/mmc.h>
29 #include <linux/mmc/sd.h>
40 static struct workqueue_struct *workqueue;
43 * Enabling software CRCs on the data blocks can be a significant (30%)
44 * performance cost, and for other reasons may not always be desired.
45 * So we allow it it to be disabled.
48 module_param(use_spi_crc, bool, 0);
51 * Internal function. Schedule delayed work in the MMC work queue.
53 static int mmc_schedule_delayed_work(struct delayed_work *work,
56 return queue_delayed_work(workqueue, work, delay);
60 * Internal function. Flush all scheduled work from the MMC work queue.
62 static void mmc_flush_scheduled_work(void)
64 flush_workqueue(workqueue);
68 * mmc_request_done - finish processing an MMC request
69 * @host: MMC host which completed request
70 * @mrq: MMC request which request
72 * MMC drivers should call this function when they have completed
73 * their processing of a request.
75 void mmc_request_done(struct mmc_host *host, struct mmc_request *mrq)
77 struct mmc_command *cmd = mrq->cmd;
80 if (err && cmd->retries && mmc_host_is_spi(host)) {
81 if (cmd->resp[0] & R1_SPI_ILLEGAL_COMMAND)
85 if (err && cmd->retries) {
86 pr_debug("%s: req failed (CMD%u): %d, retrying...\n",
87 mmc_hostname(host), cmd->opcode, err);
91 host->ops->request(host, mrq);
93 led_trigger_event(host->led, LED_OFF);
95 pr_debug("%s: req done (CMD%u): %d: %08x %08x %08x %08x\n",
96 mmc_hostname(host), cmd->opcode, err,
97 cmd->resp[0], cmd->resp[1],
98 cmd->resp[2], cmd->resp[3]);
101 pr_debug("%s: %d bytes transferred: %d\n",
103 mrq->data->bytes_xfered, mrq->data->error);
107 pr_debug("%s: (CMD%u): %d: %08x %08x %08x %08x\n",
108 mmc_hostname(host), mrq->stop->opcode,
110 mrq->stop->resp[0], mrq->stop->resp[1],
111 mrq->stop->resp[2], mrq->stop->resp[3]);
119 EXPORT_SYMBOL(mmc_request_done);
122 mmc_start_request(struct mmc_host *host, struct mmc_request *mrq)
124 #ifdef CONFIG_MMC_DEBUG
126 struct scatterlist *sg;
129 pr_debug("%s: starting CMD%u arg %08x flags %08x\n",
130 mmc_hostname(host), mrq->cmd->opcode,
131 mrq->cmd->arg, mrq->cmd->flags);
134 pr_debug("%s: blksz %d blocks %d flags %08x "
135 "tsac %d ms nsac %d\n",
136 mmc_hostname(host), mrq->data->blksz,
137 mrq->data->blocks, mrq->data->flags,
138 mrq->data->timeout_ns / 1000000,
139 mrq->data->timeout_clks);
143 pr_debug("%s: CMD%u arg %08x flags %08x\n",
144 mmc_hostname(host), mrq->stop->opcode,
145 mrq->stop->arg, mrq->stop->flags);
148 WARN_ON(!host->claimed);
150 led_trigger_event(host->led, LED_FULL);
155 BUG_ON(mrq->data->blksz > host->max_blk_size);
156 BUG_ON(mrq->data->blocks > host->max_blk_count);
157 BUG_ON(mrq->data->blocks * mrq->data->blksz >
160 #ifdef CONFIG_MMC_DEBUG
162 for_each_sg(mrq->data->sg, sg, mrq->data->sg_len, i)
164 BUG_ON(sz != mrq->data->blocks * mrq->data->blksz);
167 mrq->cmd->data = mrq->data;
168 mrq->data->error = 0;
169 mrq->data->mrq = mrq;
171 mrq->data->stop = mrq->stop;
172 mrq->stop->error = 0;
173 mrq->stop->mrq = mrq;
176 host->ops->request(host, mrq);
179 static void mmc_wait_done(struct mmc_request *mrq)
181 complete(mrq->done_data);
185 * mmc_wait_for_req - start a request and wait for completion
186 * @host: MMC host to start command
187 * @mrq: MMC request to start
189 * Start a new MMC custom command request for a host, and wait
190 * for the command to complete. Does not attempt to parse the
193 void mmc_wait_for_req(struct mmc_host *host, struct mmc_request *mrq)
195 DECLARE_COMPLETION_ONSTACK(complete);
197 mrq->done_data = &complete;
198 mrq->done = mmc_wait_done;
200 mmc_start_request(host, mrq);
202 wait_for_completion(&complete);
205 EXPORT_SYMBOL(mmc_wait_for_req);
208 * mmc_wait_for_cmd - start a command and wait for completion
209 * @host: MMC host to start command
210 * @cmd: MMC command to start
211 * @retries: maximum number of retries
213 * Start a new MMC command for a host, and wait for the command
214 * to complete. Return any error that occurred while the command
215 * was executing. Do not attempt to parse the response.
217 int mmc_wait_for_cmd(struct mmc_host *host, struct mmc_command *cmd, int retries)
219 struct mmc_request mrq;
221 WARN_ON(!host->claimed);
223 memset(&mrq, 0, sizeof(struct mmc_request));
225 memset(cmd->resp, 0, sizeof(cmd->resp));
226 cmd->retries = retries;
231 mmc_wait_for_req(host, &mrq);
236 EXPORT_SYMBOL(mmc_wait_for_cmd);
239 * mmc_set_data_timeout - set the timeout for a data command
240 * @data: data phase for command
241 * @card: the MMC card associated with the data transfer
243 * Computes the data timeout parameters according to the
244 * correct algorithm given the card type.
246 void mmc_set_data_timeout(struct mmc_data *data, const struct mmc_card *card)
251 * SDIO cards only define an upper 1 s limit on access.
253 if (mmc_card_sdio(card)) {
254 data->timeout_ns = 1000000000;
255 data->timeout_clks = 0;
260 * SD cards use a 100 multiplier rather than 10
262 mult = mmc_card_sd(card) ? 100 : 10;
265 * Scale up the multiplier (and therefore the timeout) by
266 * the r2w factor for writes.
268 if (data->flags & MMC_DATA_WRITE)
269 mult <<= card->csd.r2w_factor;
271 data->timeout_ns = card->csd.tacc_ns * mult;
272 data->timeout_clks = card->csd.tacc_clks * mult;
275 * SD cards also have an upper limit on the timeout.
277 if (mmc_card_sd(card)) {
278 unsigned int timeout_us, limit_us;
280 timeout_us = data->timeout_ns / 1000;
281 timeout_us += data->timeout_clks * 1000 /
282 (card->host->ios.clock / 1000);
284 if (data->flags & MMC_DATA_WRITE)
286 * The limit is really 250 ms, but that is
287 * insufficient for some crappy cards.
294 * SDHC cards always use these fixed values.
296 if (timeout_us > limit_us || mmc_card_blockaddr(card)) {
297 data->timeout_ns = limit_us * 1000;
298 data->timeout_clks = 0;
302 EXPORT_SYMBOL(mmc_set_data_timeout);
305 * mmc_align_data_size - pads a transfer size to a more optimal value
306 * @card: the MMC card associated with the data transfer
307 * @sz: original transfer size
309 * Pads the original data size with a number of extra bytes in
310 * order to avoid controller bugs and/or performance hits
311 * (e.g. some controllers revert to PIO for certain sizes).
313 * Returns the improved size, which might be unmodified.
315 * Note that this function is only relevant when issuing a
316 * single scatter gather entry.
318 unsigned int mmc_align_data_size(struct mmc_card *card, unsigned int sz)
321 * FIXME: We don't have a system for the controller to tell
322 * the core about its problems yet, so for now we just 32-bit
325 sz = ((sz + 3) / 4) * 4;
329 EXPORT_SYMBOL(mmc_align_data_size);
332 * __mmc_claim_host - exclusively claim a host
333 * @host: mmc host to claim
334 * @abort: whether or not the operation should be aborted
336 * Claim a host for a set of operations. If @abort is non null and
337 * dereference a non-zero value then this will return prematurely with
338 * that non-zero value without acquiring the lock. Returns zero
339 * with the lock held otherwise.
341 int __mmc_claim_host(struct mmc_host *host, atomic_t *abort)
343 DECLARE_WAITQUEUE(wait, current);
349 add_wait_queue(&host->wq, &wait);
350 spin_lock_irqsave(&host->lock, flags);
352 set_current_state(TASK_UNINTERRUPTIBLE);
353 stop = abort ? atomic_read(abort) : 0;
354 if (stop || !host->claimed)
356 spin_unlock_irqrestore(&host->lock, flags);
358 spin_lock_irqsave(&host->lock, flags);
360 set_current_state(TASK_RUNNING);
365 spin_unlock_irqrestore(&host->lock, flags);
366 remove_wait_queue(&host->wq, &wait);
370 EXPORT_SYMBOL(__mmc_claim_host);
373 * mmc_release_host - release a host
374 * @host: mmc host to release
376 * Release a MMC host, allowing others to claim the host
377 * for their operations.
379 void mmc_release_host(struct mmc_host *host)
383 WARN_ON(!host->claimed);
385 spin_lock_irqsave(&host->lock, flags);
387 spin_unlock_irqrestore(&host->lock, flags);
392 EXPORT_SYMBOL(mmc_release_host);
395 * Internal function that does the actual ios call to the host driver,
396 * optionally printing some debug output.
398 static inline void mmc_set_ios(struct mmc_host *host)
400 struct mmc_ios *ios = &host->ios;
402 pr_debug("%s: clock %uHz busmode %u powermode %u cs %u Vdd %u "
403 "width %u timing %u\n",
404 mmc_hostname(host), ios->clock, ios->bus_mode,
405 ios->power_mode, ios->chip_select, ios->vdd,
406 ios->bus_width, ios->timing);
408 host->ops->set_ios(host, ios);
412 * Control chip select pin on a host.
414 void mmc_set_chip_select(struct mmc_host *host, int mode)
416 host->ios.chip_select = mode;
421 * Sets the host clock to the highest possible frequency that
424 void mmc_set_clock(struct mmc_host *host, unsigned int hz)
426 WARN_ON(hz < host->f_min);
428 if (hz > host->f_max)
431 host->ios.clock = hz;
436 * Change the bus mode (open drain/push-pull) of a host.
438 void mmc_set_bus_mode(struct mmc_host *host, unsigned int mode)
440 host->ios.bus_mode = mode;
445 * Change data bus width of a host.
447 void mmc_set_bus_width(struct mmc_host *host, unsigned int width)
449 host->ios.bus_width = width;
454 * mmc_vdd_to_ocrbitnum - Convert a voltage to the OCR bit number
456 * @low_bits: prefer low bits in boundary cases
458 * This function returns the OCR bit number according to the provided @vdd
459 * value. If conversion is not possible a negative errno value returned.
461 * Depending on the @low_bits flag the function prefers low or high OCR bits
462 * on boundary voltages. For example,
463 * with @low_bits = true, 3300 mV translates to ilog2(MMC_VDD_32_33);
464 * with @low_bits = false, 3300 mV translates to ilog2(MMC_VDD_33_34);
466 * Any value in the [1951:1999] range translates to the ilog2(MMC_VDD_20_21).
468 static int mmc_vdd_to_ocrbitnum(int vdd, bool low_bits)
470 const int max_bit = ilog2(MMC_VDD_35_36);
473 if (vdd < 1650 || vdd > 3600)
476 if (vdd >= 1650 && vdd <= 1950)
477 return ilog2(MMC_VDD_165_195);
482 /* Base 2000 mV, step 100 mV, bit's base 8. */
483 bit = (vdd - 2000) / 100 + 8;
490 * mmc_vddrange_to_ocrmask - Convert a voltage range to the OCR mask
491 * @vdd_min: minimum voltage value (mV)
492 * @vdd_max: maximum voltage value (mV)
494 * This function returns the OCR mask bits according to the provided @vdd_min
495 * and @vdd_max values. If conversion is not possible the function returns 0.
497 * Notes wrt boundary cases:
498 * This function sets the OCR bits for all boundary voltages, for example
499 * [3300:3400] range is translated to MMC_VDD_32_33 | MMC_VDD_33_34 |
500 * MMC_VDD_34_35 mask.
502 u32 mmc_vddrange_to_ocrmask(int vdd_min, int vdd_max)
506 if (vdd_max < vdd_min)
509 /* Prefer high bits for the boundary vdd_max values. */
510 vdd_max = mmc_vdd_to_ocrbitnum(vdd_max, false);
514 /* Prefer low bits for the boundary vdd_min values. */
515 vdd_min = mmc_vdd_to_ocrbitnum(vdd_min, true);
519 /* Fill the mask, from max bit to min bit. */
520 while (vdd_max >= vdd_min)
521 mask |= 1 << vdd_max--;
525 EXPORT_SYMBOL(mmc_vddrange_to_ocrmask);
527 #ifdef CONFIG_REGULATOR
530 * mmc_regulator_get_ocrmask - return mask of supported voltages
531 * @supply: regulator to use
533 * This returns either a negative errno, or a mask of voltages that
534 * can be provided to MMC/SD/SDIO devices using the specified voltage
535 * regulator. This would normally be called before registering the
538 int mmc_regulator_get_ocrmask(struct regulator *supply)
544 count = regulator_count_voltages(supply);
548 for (i = 0; i < count; i++) {
552 vdd_uV = regulator_list_voltage(supply, i);
556 vdd_mV = vdd_uV / 1000;
557 result |= mmc_vddrange_to_ocrmask(vdd_mV, vdd_mV);
562 EXPORT_SYMBOL(mmc_regulator_get_ocrmask);
565 * mmc_regulator_set_ocr - set regulator to match host->ios voltage
566 * @vdd_bit: zero for power off, else a bit number (host->ios.vdd)
567 * @supply: regulator to use
569 * Returns zero on success, else negative errno.
571 * MMC host drivers may use this to enable or disable a regulator using
572 * a particular supply voltage. This would normally be called from the
575 int mmc_regulator_set_ocr(struct regulator *supply, unsigned short vdd_bit)
581 enabled = regulator_is_enabled(supply);
589 /* REVISIT mmc_vddrange_to_ocrmask() may have set some
590 * bits this regulator doesn't quite support ... don't
591 * be too picky, most cards and regulators are OK with
592 * a 0.1V range goof (it's a small error percentage).
594 tmp = vdd_bit - ilog2(MMC_VDD_165_195);
596 min_uV = 1650 * 1000;
597 max_uV = 1950 * 1000;
599 min_uV = 1900 * 1000 + tmp * 100 * 1000;
600 max_uV = min_uV + 100 * 1000;
603 /* avoid needless changes to this voltage; the regulator
604 * might not allow this operation
606 voltage = regulator_get_voltage(supply);
609 else if (voltage < min_uV || voltage > max_uV)
610 result = regulator_set_voltage(supply, min_uV, max_uV);
614 if (result == 0 && !enabled)
615 result = regulator_enable(supply);
616 } else if (enabled) {
617 result = regulator_disable(supply);
622 EXPORT_SYMBOL(mmc_regulator_set_ocr);
627 * Mask off any voltages we don't support and select
630 u32 mmc_select_voltage(struct mmc_host *host, u32 ocr)
634 ocr &= host->ocr_avail;
645 pr_warning("%s: host doesn't support card's voltages\n",
654 * Select timing parameters for host.
656 void mmc_set_timing(struct mmc_host *host, unsigned int timing)
658 host->ios.timing = timing;
663 * Apply power to the MMC stack. This is a two-stage process.
664 * First, we enable power to the card without the clock running.
665 * We then wait a bit for the power to stabilise. Finally,
666 * enable the bus drivers and clock to the card.
668 * We must _NOT_ enable the clock prior to power stablising.
670 * If a host does all the power sequencing itself, ignore the
671 * initial MMC_POWER_UP stage.
673 static void mmc_power_up(struct mmc_host *host)
675 int bit = fls(host->ocr_avail) - 1;
678 if (mmc_host_is_spi(host)) {
679 host->ios.chip_select = MMC_CS_HIGH;
680 host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
682 host->ios.chip_select = MMC_CS_DONTCARE;
683 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
685 host->ios.power_mode = MMC_POWER_UP;
686 host->ios.bus_width = MMC_BUS_WIDTH_1;
687 host->ios.timing = MMC_TIMING_LEGACY;
691 * This delay should be sufficient to allow the power supply
692 * to reach the minimum voltage.
696 host->ios.clock = host->f_min;
697 host->ios.power_mode = MMC_POWER_ON;
701 * This delay must be at least 74 clock sizes, or 1 ms, or the
702 * time required to reach a stable voltage.
707 static void mmc_power_off(struct mmc_host *host)
711 if (!mmc_host_is_spi(host)) {
712 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
713 host->ios.chip_select = MMC_CS_DONTCARE;
715 host->ios.power_mode = MMC_POWER_OFF;
716 host->ios.bus_width = MMC_BUS_WIDTH_1;
717 host->ios.timing = MMC_TIMING_LEGACY;
722 * Cleanup when the last reference to the bus operator is dropped.
724 static void __mmc_release_bus(struct mmc_host *host)
727 BUG_ON(host->bus_refs);
728 BUG_ON(!host->bus_dead);
730 host->bus_ops = NULL;
734 * Increase reference count of bus operator
736 static inline void mmc_bus_get(struct mmc_host *host)
740 spin_lock_irqsave(&host->lock, flags);
742 spin_unlock_irqrestore(&host->lock, flags);
746 * Decrease reference count of bus operator and free it if
747 * it is the last reference.
749 static inline void mmc_bus_put(struct mmc_host *host)
753 spin_lock_irqsave(&host->lock, flags);
755 if ((host->bus_refs == 0) && host->bus_ops)
756 __mmc_release_bus(host);
757 spin_unlock_irqrestore(&host->lock, flags);
761 * Assign a mmc bus handler to a host. Only one bus handler may control a
762 * host at any given time.
764 void mmc_attach_bus(struct mmc_host *host, const struct mmc_bus_ops *ops)
771 WARN_ON(!host->claimed);
773 spin_lock_irqsave(&host->lock, flags);
775 BUG_ON(host->bus_ops);
776 BUG_ON(host->bus_refs);
782 spin_unlock_irqrestore(&host->lock, flags);
786 * Remove the current bus handler from a host. Assumes that there are
787 * no interesting cards left, so the bus is powered down.
789 void mmc_detach_bus(struct mmc_host *host)
795 WARN_ON(!host->claimed);
796 WARN_ON(!host->bus_ops);
798 spin_lock_irqsave(&host->lock, flags);
802 spin_unlock_irqrestore(&host->lock, flags);
810 * mmc_detect_change - process change of state on a MMC socket
811 * @host: host which changed state.
812 * @delay: optional delay to wait before detection (jiffies)
814 * MMC drivers should call this when they detect a card has been
815 * inserted or removed. The MMC layer will confirm that any
816 * present card is still functional, and initialize any newly
819 void mmc_detect_change(struct mmc_host *host, unsigned long delay)
821 #ifdef CONFIG_MMC_DEBUG
823 spin_lock_irqsave(&host->lock, flags);
824 WARN_ON(host->removed);
825 spin_unlock_irqrestore(&host->lock, flags);
828 mmc_schedule_delayed_work(&host->detect, delay);
831 EXPORT_SYMBOL(mmc_detect_change);
834 void mmc_rescan(struct work_struct *work)
836 struct mmc_host *host =
837 container_of(work, struct mmc_host, detect.work);
843 if (host->bus_ops == NULL) {
845 * Only we can add a new handler, so it's safe to
846 * release the lock here.
850 if (host->ops->get_cd && host->ops->get_cd(host) == 0)
853 mmc_claim_host(host);
858 mmc_send_if_cond(host, host->ocr_avail);
861 * First we search for SDIO...
863 err = mmc_send_io_op_cond(host, 0, &ocr);
865 if (mmc_attach_sdio(host, ocr))
871 * ...then normal SD...
873 err = mmc_send_app_op_cond(host, 0, &ocr);
875 if (mmc_attach_sd(host, ocr))
881 * ...and finally MMC.
883 err = mmc_send_op_cond(host, 0, &ocr);
885 if (mmc_attach_mmc(host, ocr))
890 mmc_release_host(host);
893 if (host->bus_ops->detect && !host->bus_dead)
894 host->bus_ops->detect(host);
899 if (host->caps & MMC_CAP_NEEDS_POLL)
900 mmc_schedule_delayed_work(&host->detect, HZ);
903 void mmc_start_host(struct mmc_host *host)
906 mmc_detect_change(host, 0);
909 void mmc_stop_host(struct mmc_host *host)
911 #ifdef CONFIG_MMC_DEBUG
913 spin_lock_irqsave(&host->lock, flags);
915 spin_unlock_irqrestore(&host->lock, flags);
918 mmc_flush_scheduled_work();
921 if (host->bus_ops && !host->bus_dead) {
922 if (host->bus_ops->remove)
923 host->bus_ops->remove(host);
925 mmc_claim_host(host);
926 mmc_detach_bus(host);
927 mmc_release_host(host);
939 * mmc_suspend_host - suspend a host
941 * @state: suspend mode (PM_SUSPEND_xxx)
943 int mmc_suspend_host(struct mmc_host *host, pm_message_t state)
945 mmc_flush_scheduled_work();
948 if (host->bus_ops && !host->bus_dead) {
949 if (host->bus_ops->suspend)
950 host->bus_ops->suspend(host);
951 if (!host->bus_ops->resume) {
952 if (host->bus_ops->remove)
953 host->bus_ops->remove(host);
955 mmc_claim_host(host);
956 mmc_detach_bus(host);
957 mmc_release_host(host);
967 EXPORT_SYMBOL(mmc_suspend_host);
970 * mmc_resume_host - resume a previously suspended host
973 int mmc_resume_host(struct mmc_host *host)
976 if (host->bus_ops && !host->bus_dead) {
978 BUG_ON(!host->bus_ops->resume);
979 host->bus_ops->resume(host);
984 * We add a slight delay here so that resume can progress
987 mmc_detect_change(host, 1);
992 EXPORT_SYMBOL(mmc_resume_host);
996 static int __init mmc_init(void)
1000 workqueue = create_singlethread_workqueue("kmmcd");
1004 ret = mmc_register_bus();
1006 goto destroy_workqueue;
1008 ret = mmc_register_host_class();
1010 goto unregister_bus;
1012 ret = sdio_register_bus();
1014 goto unregister_host_class;
1018 unregister_host_class:
1019 mmc_unregister_host_class();
1021 mmc_unregister_bus();
1023 destroy_workqueue(workqueue);
1028 static void __exit mmc_exit(void)
1030 sdio_unregister_bus();
1031 mmc_unregister_host_class();
1032 mmc_unregister_bus();
1033 destroy_workqueue(workqueue);
1036 subsys_initcall(mmc_init);
1037 module_exit(mmc_exit);
1039 MODULE_LICENSE("GPL");