nommu: Add noMMU support to the DMA API
[linux-2.6.git] / drivers / mmc / core / core.c
blobd84c880fac84f41a0d388540d74d22a4a7649705
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>
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>
31 #include "core.h"
32 #include "bus.h"
33 #include "host.h"
34 #include "sdio_bus.h"
36 #include "mmc_ops.h"
37 #include "sd_ops.h"
38 #include "sdio_ops.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.
47 int use_spi_crc = 1;
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,
54 unsigned long delay)
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);
67 /**
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;
78 int err = cmd->error;
80 if (err && cmd->retries && mmc_host_is_spi(host)) {
81 if (cmd->resp[0] & R1_SPI_ILLEGAL_COMMAND)
82 cmd->retries = 0;
85 if (err && cmd->retries) {
86 pr_debug("%s: req failed (CMD%u): %d, retrying...\n",
87 mmc_hostname(host), cmd->opcode, err);
89 cmd->retries--;
90 cmd->error = 0;
91 host->ops->request(host, mrq);
92 } else {
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]);
100 if (mrq->data) {
101 pr_debug("%s: %d bytes transferred: %d\n",
102 mmc_hostname(host),
103 mrq->data->bytes_xfered, mrq->data->error);
106 if (mrq->stop) {
107 pr_debug("%s: (CMD%u): %d: %08x %08x %08x %08x\n",
108 mmc_hostname(host), mrq->stop->opcode,
109 mrq->stop->error,
110 mrq->stop->resp[0], mrq->stop->resp[1],
111 mrq->stop->resp[2], mrq->stop->resp[3]);
114 if (mrq->done)
115 mrq->done(mrq);
119 EXPORT_SYMBOL(mmc_request_done);
121 static void
122 mmc_start_request(struct mmc_host *host, struct mmc_request *mrq)
124 #ifdef CONFIG_MMC_DEBUG
125 unsigned int i, sz;
126 struct scatterlist *sg;
127 #endif
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);
133 if (mrq->data) {
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);
142 if (mrq->stop) {
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);
152 mrq->cmd->error = 0;
153 mrq->cmd->mrq = mrq;
154 if (mrq->data) {
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 >
158 host->max_req_size);
160 #ifdef CONFIG_MMC_DEBUG
161 sz = 0;
162 for_each_sg(mrq->data->sg, sg, mrq->data->sg_len, i)
163 sz += sg->length;
164 BUG_ON(sz != mrq->data->blocks * mrq->data->blksz);
165 #endif
167 mrq->cmd->data = mrq->data;
168 mrq->data->error = 0;
169 mrq->data->mrq = mrq;
170 if (mrq->stop) {
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
191 * response.
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;
228 mrq.cmd = cmd;
229 cmd->data = NULL;
231 mmc_wait_for_req(host, &mrq);
233 return cmd->error;
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)
248 unsigned int mult;
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;
256 return;
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.
289 limit_us = 300000;
290 else
291 limit_us = 100000;
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 * Some cards need very high timeouts if driven in SPI mode.
303 * The worst observed timeout was 900ms after writing a
304 * continuous stream of data until the internal logic
305 * overflowed.
307 if (mmc_host_is_spi(card->host)) {
308 if (data->flags & MMC_DATA_WRITE) {
309 if (data->timeout_ns < 1000000000)
310 data->timeout_ns = 1000000000; /* 1s */
311 } else {
312 if (data->timeout_ns < 100000000)
313 data->timeout_ns = 100000000; /* 100ms */
317 EXPORT_SYMBOL(mmc_set_data_timeout);
320 * mmc_align_data_size - pads a transfer size to a more optimal value
321 * @card: the MMC card associated with the data transfer
322 * @sz: original transfer size
324 * Pads the original data size with a number of extra bytes in
325 * order to avoid controller bugs and/or performance hits
326 * (e.g. some controllers revert to PIO for certain sizes).
328 * Returns the improved size, which might be unmodified.
330 * Note that this function is only relevant when issuing a
331 * single scatter gather entry.
333 unsigned int mmc_align_data_size(struct mmc_card *card, unsigned int sz)
336 * FIXME: We don't have a system for the controller to tell
337 * the core about its problems yet, so for now we just 32-bit
338 * align the size.
340 sz = ((sz + 3) / 4) * 4;
342 return sz;
344 EXPORT_SYMBOL(mmc_align_data_size);
347 * __mmc_claim_host - exclusively claim a host
348 * @host: mmc host to claim
349 * @abort: whether or not the operation should be aborted
351 * Claim a host for a set of operations. If @abort is non null and
352 * dereference a non-zero value then this will return prematurely with
353 * that non-zero value without acquiring the lock. Returns zero
354 * with the lock held otherwise.
356 int __mmc_claim_host(struct mmc_host *host, atomic_t *abort)
358 DECLARE_WAITQUEUE(wait, current);
359 unsigned long flags;
360 int stop;
362 might_sleep();
364 add_wait_queue(&host->wq, &wait);
365 spin_lock_irqsave(&host->lock, flags);
366 while (1) {
367 set_current_state(TASK_UNINTERRUPTIBLE);
368 stop = abort ? atomic_read(abort) : 0;
369 if (stop || !host->claimed)
370 break;
371 spin_unlock_irqrestore(&host->lock, flags);
372 schedule();
373 spin_lock_irqsave(&host->lock, flags);
375 set_current_state(TASK_RUNNING);
376 if (!stop)
377 host->claimed = 1;
378 else
379 wake_up(&host->wq);
380 spin_unlock_irqrestore(&host->lock, flags);
381 remove_wait_queue(&host->wq, &wait);
382 return stop;
385 EXPORT_SYMBOL(__mmc_claim_host);
388 * mmc_release_host - release a host
389 * @host: mmc host to release
391 * Release a MMC host, allowing others to claim the host
392 * for their operations.
394 void mmc_release_host(struct mmc_host *host)
396 unsigned long flags;
398 WARN_ON(!host->claimed);
400 spin_lock_irqsave(&host->lock, flags);
401 host->claimed = 0;
402 spin_unlock_irqrestore(&host->lock, flags);
404 wake_up(&host->wq);
407 EXPORT_SYMBOL(mmc_release_host);
410 * Internal function that does the actual ios call to the host driver,
411 * optionally printing some debug output.
413 static inline void mmc_set_ios(struct mmc_host *host)
415 struct mmc_ios *ios = &host->ios;
417 pr_debug("%s: clock %uHz busmode %u powermode %u cs %u Vdd %u "
418 "width %u timing %u\n",
419 mmc_hostname(host), ios->clock, ios->bus_mode,
420 ios->power_mode, ios->chip_select, ios->vdd,
421 ios->bus_width, ios->timing);
423 host->ops->set_ios(host, ios);
427 * Control chip select pin on a host.
429 void mmc_set_chip_select(struct mmc_host *host, int mode)
431 host->ios.chip_select = mode;
432 mmc_set_ios(host);
436 * Sets the host clock to the highest possible frequency that
437 * is below "hz".
439 void mmc_set_clock(struct mmc_host *host, unsigned int hz)
441 WARN_ON(hz < host->f_min);
443 if (hz > host->f_max)
444 hz = host->f_max;
446 host->ios.clock = hz;
447 mmc_set_ios(host);
451 * Change the bus mode (open drain/push-pull) of a host.
453 void mmc_set_bus_mode(struct mmc_host *host, unsigned int mode)
455 host->ios.bus_mode = mode;
456 mmc_set_ios(host);
460 * Change data bus width of a host.
462 void mmc_set_bus_width(struct mmc_host *host, unsigned int width)
464 host->ios.bus_width = width;
465 mmc_set_ios(host);
469 * mmc_vdd_to_ocrbitnum - Convert a voltage to the OCR bit number
470 * @vdd: voltage (mV)
471 * @low_bits: prefer low bits in boundary cases
473 * This function returns the OCR bit number according to the provided @vdd
474 * value. If conversion is not possible a negative errno value returned.
476 * Depending on the @low_bits flag the function prefers low or high OCR bits
477 * on boundary voltages. For example,
478 * with @low_bits = true, 3300 mV translates to ilog2(MMC_VDD_32_33);
479 * with @low_bits = false, 3300 mV translates to ilog2(MMC_VDD_33_34);
481 * Any value in the [1951:1999] range translates to the ilog2(MMC_VDD_20_21).
483 static int mmc_vdd_to_ocrbitnum(int vdd, bool low_bits)
485 const int max_bit = ilog2(MMC_VDD_35_36);
486 int bit;
488 if (vdd < 1650 || vdd > 3600)
489 return -EINVAL;
491 if (vdd >= 1650 && vdd <= 1950)
492 return ilog2(MMC_VDD_165_195);
494 if (low_bits)
495 vdd -= 1;
497 /* Base 2000 mV, step 100 mV, bit's base 8. */
498 bit = (vdd - 2000) / 100 + 8;
499 if (bit > max_bit)
500 return max_bit;
501 return bit;
505 * mmc_vddrange_to_ocrmask - Convert a voltage range to the OCR mask
506 * @vdd_min: minimum voltage value (mV)
507 * @vdd_max: maximum voltage value (mV)
509 * This function returns the OCR mask bits according to the provided @vdd_min
510 * and @vdd_max values. If conversion is not possible the function returns 0.
512 * Notes wrt boundary cases:
513 * This function sets the OCR bits for all boundary voltages, for example
514 * [3300:3400] range is translated to MMC_VDD_32_33 | MMC_VDD_33_34 |
515 * MMC_VDD_34_35 mask.
517 u32 mmc_vddrange_to_ocrmask(int vdd_min, int vdd_max)
519 u32 mask = 0;
521 if (vdd_max < vdd_min)
522 return 0;
524 /* Prefer high bits for the boundary vdd_max values. */
525 vdd_max = mmc_vdd_to_ocrbitnum(vdd_max, false);
526 if (vdd_max < 0)
527 return 0;
529 /* Prefer low bits for the boundary vdd_min values. */
530 vdd_min = mmc_vdd_to_ocrbitnum(vdd_min, true);
531 if (vdd_min < 0)
532 return 0;
534 /* Fill the mask, from max bit to min bit. */
535 while (vdd_max >= vdd_min)
536 mask |= 1 << vdd_max--;
538 return mask;
540 EXPORT_SYMBOL(mmc_vddrange_to_ocrmask);
542 #ifdef CONFIG_REGULATOR
545 * mmc_regulator_get_ocrmask - return mask of supported voltages
546 * @supply: regulator to use
548 * This returns either a negative errno, or a mask of voltages that
549 * can be provided to MMC/SD/SDIO devices using the specified voltage
550 * regulator. This would normally be called before registering the
551 * MMC host adapter.
553 int mmc_regulator_get_ocrmask(struct regulator *supply)
555 int result = 0;
556 int count;
557 int i;
559 count = regulator_count_voltages(supply);
560 if (count < 0)
561 return count;
563 for (i = 0; i < count; i++) {
564 int vdd_uV;
565 int vdd_mV;
567 vdd_uV = regulator_list_voltage(supply, i);
568 if (vdd_uV <= 0)
569 continue;
571 vdd_mV = vdd_uV / 1000;
572 result |= mmc_vddrange_to_ocrmask(vdd_mV, vdd_mV);
575 return result;
577 EXPORT_SYMBOL(mmc_regulator_get_ocrmask);
580 * mmc_regulator_set_ocr - set regulator to match host->ios voltage
581 * @vdd_bit: zero for power off, else a bit number (host->ios.vdd)
582 * @supply: regulator to use
584 * Returns zero on success, else negative errno.
586 * MMC host drivers may use this to enable or disable a regulator using
587 * a particular supply voltage. This would normally be called from the
588 * set_ios() method.
590 int mmc_regulator_set_ocr(struct regulator *supply, unsigned short vdd_bit)
592 int result = 0;
593 int min_uV, max_uV;
594 int enabled;
596 enabled = regulator_is_enabled(supply);
597 if (enabled < 0)
598 return enabled;
600 if (vdd_bit) {
601 int tmp;
602 int voltage;
604 /* REVISIT mmc_vddrange_to_ocrmask() may have set some
605 * bits this regulator doesn't quite support ... don't
606 * be too picky, most cards and regulators are OK with
607 * a 0.1V range goof (it's a small error percentage).
609 tmp = vdd_bit - ilog2(MMC_VDD_165_195);
610 if (tmp == 0) {
611 min_uV = 1650 * 1000;
612 max_uV = 1950 * 1000;
613 } else {
614 min_uV = 1900 * 1000 + tmp * 100 * 1000;
615 max_uV = min_uV + 100 * 1000;
618 /* avoid needless changes to this voltage; the regulator
619 * might not allow this operation
621 voltage = regulator_get_voltage(supply);
622 if (voltage < 0)
623 result = voltage;
624 else if (voltage < min_uV || voltage > max_uV)
625 result = regulator_set_voltage(supply, min_uV, max_uV);
626 else
627 result = 0;
629 if (result == 0 && !enabled)
630 result = regulator_enable(supply);
631 } else if (enabled) {
632 result = regulator_disable(supply);
635 return result;
637 EXPORT_SYMBOL(mmc_regulator_set_ocr);
639 #endif
642 * Mask off any voltages we don't support and select
643 * the lowest voltage
645 u32 mmc_select_voltage(struct mmc_host *host, u32 ocr)
647 int bit;
649 ocr &= host->ocr_avail;
651 bit = ffs(ocr);
652 if (bit) {
653 bit -= 1;
655 ocr &= 3 << bit;
657 host->ios.vdd = bit;
658 mmc_set_ios(host);
659 } else {
660 pr_warning("%s: host doesn't support card's voltages\n",
661 mmc_hostname(host));
662 ocr = 0;
665 return ocr;
669 * Select timing parameters for host.
671 void mmc_set_timing(struct mmc_host *host, unsigned int timing)
673 host->ios.timing = timing;
674 mmc_set_ios(host);
678 * Apply power to the MMC stack. This is a two-stage process.
679 * First, we enable power to the card without the clock running.
680 * We then wait a bit for the power to stabilise. Finally,
681 * enable the bus drivers and clock to the card.
683 * We must _NOT_ enable the clock prior to power stablising.
685 * If a host does all the power sequencing itself, ignore the
686 * initial MMC_POWER_UP stage.
688 static void mmc_power_up(struct mmc_host *host)
690 int bit = fls(host->ocr_avail) - 1;
692 host->ios.vdd = bit;
693 if (mmc_host_is_spi(host)) {
694 host->ios.chip_select = MMC_CS_HIGH;
695 host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
696 } else {
697 host->ios.chip_select = MMC_CS_DONTCARE;
698 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
700 host->ios.power_mode = MMC_POWER_UP;
701 host->ios.bus_width = MMC_BUS_WIDTH_1;
702 host->ios.timing = MMC_TIMING_LEGACY;
703 mmc_set_ios(host);
706 * This delay should be sufficient to allow the power supply
707 * to reach the minimum voltage.
709 mmc_delay(10);
711 if (host->f_min > 400000) {
712 pr_warning("%s: Minimum clock frequency too high for "
713 "identification mode\n", mmc_hostname(host));
714 host->ios.clock = host->f_min;
715 } else
716 host->ios.clock = 400000;
718 host->ios.power_mode = MMC_POWER_ON;
719 mmc_set_ios(host);
722 * This delay must be at least 74 clock sizes, or 1 ms, or the
723 * time required to reach a stable voltage.
725 mmc_delay(10);
728 static void mmc_power_off(struct mmc_host *host)
730 host->ios.clock = 0;
731 host->ios.vdd = 0;
732 if (!mmc_host_is_spi(host)) {
733 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
734 host->ios.chip_select = MMC_CS_DONTCARE;
736 host->ios.power_mode = MMC_POWER_OFF;
737 host->ios.bus_width = MMC_BUS_WIDTH_1;
738 host->ios.timing = MMC_TIMING_LEGACY;
739 mmc_set_ios(host);
743 * Cleanup when the last reference to the bus operator is dropped.
745 static void __mmc_release_bus(struct mmc_host *host)
747 BUG_ON(!host);
748 BUG_ON(host->bus_refs);
749 BUG_ON(!host->bus_dead);
751 host->bus_ops = NULL;
755 * Increase reference count of bus operator
757 static inline void mmc_bus_get(struct mmc_host *host)
759 unsigned long flags;
761 spin_lock_irqsave(&host->lock, flags);
762 host->bus_refs++;
763 spin_unlock_irqrestore(&host->lock, flags);
767 * Decrease reference count of bus operator and free it if
768 * it is the last reference.
770 static inline void mmc_bus_put(struct mmc_host *host)
772 unsigned long flags;
774 spin_lock_irqsave(&host->lock, flags);
775 host->bus_refs--;
776 if ((host->bus_refs == 0) && host->bus_ops)
777 __mmc_release_bus(host);
778 spin_unlock_irqrestore(&host->lock, flags);
782 * Assign a mmc bus handler to a host. Only one bus handler may control a
783 * host at any given time.
785 void mmc_attach_bus(struct mmc_host *host, const struct mmc_bus_ops *ops)
787 unsigned long flags;
789 BUG_ON(!host);
790 BUG_ON(!ops);
792 WARN_ON(!host->claimed);
794 spin_lock_irqsave(&host->lock, flags);
796 BUG_ON(host->bus_ops);
797 BUG_ON(host->bus_refs);
799 host->bus_ops = ops;
800 host->bus_refs = 1;
801 host->bus_dead = 0;
803 spin_unlock_irqrestore(&host->lock, flags);
807 * Remove the current bus handler from a host. Assumes that there are
808 * no interesting cards left, so the bus is powered down.
810 void mmc_detach_bus(struct mmc_host *host)
812 unsigned long flags;
814 BUG_ON(!host);
816 WARN_ON(!host->claimed);
817 WARN_ON(!host->bus_ops);
819 spin_lock_irqsave(&host->lock, flags);
821 host->bus_dead = 1;
823 spin_unlock_irqrestore(&host->lock, flags);
825 mmc_power_off(host);
827 mmc_bus_put(host);
831 * mmc_detect_change - process change of state on a MMC socket
832 * @host: host which changed state.
833 * @delay: optional delay to wait before detection (jiffies)
835 * MMC drivers should call this when they detect a card has been
836 * inserted or removed. The MMC layer will confirm that any
837 * present card is still functional, and initialize any newly
838 * inserted.
840 void mmc_detect_change(struct mmc_host *host, unsigned long delay)
842 #ifdef CONFIG_MMC_DEBUG
843 unsigned long flags;
844 spin_lock_irqsave(&host->lock, flags);
845 WARN_ON(host->removed);
846 spin_unlock_irqrestore(&host->lock, flags);
847 #endif
849 mmc_schedule_delayed_work(&host->detect, delay);
852 EXPORT_SYMBOL(mmc_detect_change);
855 void mmc_rescan(struct work_struct *work)
857 struct mmc_host *host =
858 container_of(work, struct mmc_host, detect.work);
859 u32 ocr;
860 int err;
862 mmc_bus_get(host);
864 /* if there is a card registered, check whether it is still present */
865 if ((host->bus_ops != NULL) && host->bus_ops->detect && !host->bus_dead)
866 host->bus_ops->detect(host);
868 mmc_bus_put(host);
871 mmc_bus_get(host);
873 /* if there still is a card present, stop here */
874 if (host->bus_ops != NULL) {
875 mmc_bus_put(host);
876 goto out;
879 /* detect a newly inserted card */
882 * Only we can add a new handler, so it's safe to
883 * release the lock here.
885 mmc_bus_put(host);
887 if (host->ops->get_cd && host->ops->get_cd(host) == 0)
888 goto out;
890 mmc_claim_host(host);
892 mmc_power_up(host);
893 mmc_go_idle(host);
895 mmc_send_if_cond(host, host->ocr_avail);
898 * First we search for SDIO...
900 err = mmc_send_io_op_cond(host, 0, &ocr);
901 if (!err) {
902 if (mmc_attach_sdio(host, ocr))
903 mmc_power_off(host);
904 goto out;
908 * ...then normal SD...
910 err = mmc_send_app_op_cond(host, 0, &ocr);
911 if (!err) {
912 if (mmc_attach_sd(host, ocr))
913 mmc_power_off(host);
914 goto out;
918 * ...and finally MMC.
920 err = mmc_send_op_cond(host, 0, &ocr);
921 if (!err) {
922 if (mmc_attach_mmc(host, ocr))
923 mmc_power_off(host);
924 goto out;
927 mmc_release_host(host);
928 mmc_power_off(host);
930 out:
931 if (host->caps & MMC_CAP_NEEDS_POLL)
932 mmc_schedule_delayed_work(&host->detect, HZ);
935 void mmc_start_host(struct mmc_host *host)
937 mmc_power_off(host);
938 mmc_detect_change(host, 0);
941 void mmc_stop_host(struct mmc_host *host)
943 #ifdef CONFIG_MMC_DEBUG
944 unsigned long flags;
945 spin_lock_irqsave(&host->lock, flags);
946 host->removed = 1;
947 spin_unlock_irqrestore(&host->lock, flags);
948 #endif
950 cancel_delayed_work(&host->detect);
951 mmc_flush_scheduled_work();
953 mmc_bus_get(host);
954 if (host->bus_ops && !host->bus_dead) {
955 if (host->bus_ops->remove)
956 host->bus_ops->remove(host);
958 mmc_claim_host(host);
959 mmc_detach_bus(host);
960 mmc_release_host(host);
962 mmc_bus_put(host);
964 BUG_ON(host->card);
966 mmc_power_off(host);
969 #ifdef CONFIG_PM
972 * mmc_suspend_host - suspend a host
973 * @host: mmc host
974 * @state: suspend mode (PM_SUSPEND_xxx)
976 int mmc_suspend_host(struct mmc_host *host, pm_message_t state)
978 cancel_delayed_work(&host->detect);
979 mmc_flush_scheduled_work();
981 mmc_bus_get(host);
982 if (host->bus_ops && !host->bus_dead) {
983 if (host->bus_ops->suspend)
984 host->bus_ops->suspend(host);
985 if (!host->bus_ops->resume) {
986 if (host->bus_ops->remove)
987 host->bus_ops->remove(host);
989 mmc_claim_host(host);
990 mmc_detach_bus(host);
991 mmc_release_host(host);
994 mmc_bus_put(host);
996 mmc_power_off(host);
998 return 0;
1001 EXPORT_SYMBOL(mmc_suspend_host);
1004 * mmc_resume_host - resume a previously suspended host
1005 * @host: mmc host
1007 int mmc_resume_host(struct mmc_host *host)
1009 mmc_bus_get(host);
1010 if (host->bus_ops && !host->bus_dead) {
1011 mmc_power_up(host);
1012 mmc_select_voltage(host, host->ocr);
1013 BUG_ON(!host->bus_ops->resume);
1014 host->bus_ops->resume(host);
1016 mmc_bus_put(host);
1019 * We add a slight delay here so that resume can progress
1020 * in parallel.
1022 mmc_detect_change(host, 1);
1024 return 0;
1027 EXPORT_SYMBOL(mmc_resume_host);
1029 #endif
1031 static int __init mmc_init(void)
1033 int ret;
1035 workqueue = create_singlethread_workqueue("kmmcd");
1036 if (!workqueue)
1037 return -ENOMEM;
1039 ret = mmc_register_bus();
1040 if (ret)
1041 goto destroy_workqueue;
1043 ret = mmc_register_host_class();
1044 if (ret)
1045 goto unregister_bus;
1047 ret = sdio_register_bus();
1048 if (ret)
1049 goto unregister_host_class;
1051 return 0;
1053 unregister_host_class:
1054 mmc_unregister_host_class();
1055 unregister_bus:
1056 mmc_unregister_bus();
1057 destroy_workqueue:
1058 destroy_workqueue(workqueue);
1060 return ret;
1063 static void __exit mmc_exit(void)
1065 sdio_unregister_bus();
1066 mmc_unregister_host_class();
1067 mmc_unregister_bus();
1068 destroy_workqueue(workqueue);
1071 subsys_initcall(mmc_init);
1072 module_exit(mmc_exit);
1074 MODULE_LICENSE("GPL");