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>
25 #include <linux/wakelock.h>
27 #include <linux/mmc/card.h>
28 #include <linux/mmc/host.h>
29 #include <linux/mmc/mmc.h>
30 #include <linux/mmc/sd.h>
41 static struct workqueue_struct *workqueue;
42 static struct wake_lock mmc_delayed_work_wake_lock;
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.
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,
58 wake_lock(&mmc_delayed_work_wake_lock);
59 return queue_delayed_work(workqueue, work, delay);
63 * Internal function. Flush all scheduled work from the MMC work queue.
65 static void mmc_flush_scheduled_work(void)
67 flush_workqueue(workqueue);
71 * mmc_request_done - finish processing an MMC request
72 * @host: MMC host which completed request
73 * @mrq: MMC request which request
75 * MMC drivers should call this function when they have completed
76 * their processing of a request.
78 void mmc_request_done(struct mmc_host *host, struct mmc_request *mrq)
80 struct mmc_command *cmd = mrq->cmd;
83 if (err && cmd->retries && mmc_host_is_spi(host)) {
84 if (cmd->resp[0] & R1_SPI_ILLEGAL_COMMAND)
88 if (err && cmd->retries) {
89 pr_debug("%s: req failed (CMD%u): %d, retrying...\n",
90 mmc_hostname(host), cmd->opcode, err);
94 host->ops->request(host, mrq);
96 led_trigger_event(host->led, LED_OFF);
98 pr_debug("%s: req done (CMD%u): %d: %08x %08x %08x %08x\n",
99 mmc_hostname(host), cmd->opcode, err,
100 cmd->resp[0], cmd->resp[1],
101 cmd->resp[2], cmd->resp[3]);
104 pr_debug("%s: %d bytes transferred: %d\n",
106 mrq->data->bytes_xfered, mrq->data->error);
110 pr_debug("%s: (CMD%u): %d: %08x %08x %08x %08x\n",
111 mmc_hostname(host), mrq->stop->opcode,
113 mrq->stop->resp[0], mrq->stop->resp[1],
114 mrq->stop->resp[2], mrq->stop->resp[3]);
122 EXPORT_SYMBOL(mmc_request_done);
125 mmc_start_request(struct mmc_host *host, struct mmc_request *mrq)
127 #ifdef CONFIG_MMC_DEBUG
129 struct scatterlist *sg;
132 pr_debug("%s: starting CMD%u arg %08x flags %08x\n",
133 mmc_hostname(host), mrq->cmd->opcode,
134 mrq->cmd->arg, mrq->cmd->flags);
137 pr_debug("%s: blksz %d blocks %d flags %08x "
138 "tsac %d ms nsac %d\n",
139 mmc_hostname(host), mrq->data->blksz,
140 mrq->data->blocks, mrq->data->flags,
141 mrq->data->timeout_ns / 1000000,
142 mrq->data->timeout_clks);
146 pr_debug("%s: CMD%u arg %08x flags %08x\n",
147 mmc_hostname(host), mrq->stop->opcode,
148 mrq->stop->arg, mrq->stop->flags);
151 WARN_ON(!host->claimed);
153 led_trigger_event(host->led, LED_FULL);
158 BUG_ON(mrq->data->blksz > host->max_blk_size);
159 BUG_ON(mrq->data->blocks > host->max_blk_count);
160 BUG_ON(mrq->data->blocks * mrq->data->blksz >
163 #ifdef CONFIG_MMC_DEBUG
165 for_each_sg(mrq->data->sg, sg, mrq->data->sg_len, i)
167 BUG_ON(sz != mrq->data->blocks * mrq->data->blksz);
170 mrq->cmd->data = mrq->data;
171 mrq->data->error = 0;
172 mrq->data->mrq = mrq;
174 mrq->data->stop = mrq->stop;
175 mrq->stop->error = 0;
176 mrq->stop->mrq = mrq;
179 host->ops->request(host, mrq);
182 static void mmc_wait_done(struct mmc_request *mrq)
184 complete(mrq->done_data);
188 * mmc_wait_for_req - start a request and wait for completion
189 * @host: MMC host to start command
190 * @mrq: MMC request to start
192 * Start a new MMC custom command request for a host, and wait
193 * for the command to complete. Does not attempt to parse the
196 void mmc_wait_for_req(struct mmc_host *host, struct mmc_request *mrq)
198 unsigned long datasize, waittime=0xFFFF;
201 DECLARE_COMPLETION_ONSTACK(complete);
203 mrq->done_data = &complete;
204 mrq->done = mmc_wait_done;
206 mmc_start_request(host, mrq);
208 #if defined(CONFIG_SDMMC_RK29) && !defined(CONFIG_SDMMC_RK29_OLD)
209 if( strncmp( mmc_hostname(host) ,"mmc0" , strlen("mmc0")) )
211 multi = (mrq->cmd->retries>0)?mrq->cmd->retries:1;
212 waittime = wait_for_completion_timeout(&complete,HZ*7*multi); //sdio; for cmd dead. Modifyed by xbw at 2011-06-02
216 //calculate the timeout value for SDMMC; added by xbw at 2011-09-27
219 unit = 3*(1<<20);// unit=3MB
220 datasize = mrq->data->blksz*mrq->data->blocks;
221 multi = datasize/unit;
222 multi += (datasize%unit)?1:0;
223 multi = (multi>0) ? multi : 1;
224 multi += (mrq->cmd->retries>0)?1:0;
225 waittime = wait_for_completion_timeout(&complete,HZ*7*multi); //It should be longer than bottom driver's time,due to the sum of two cmd time.
226 //modifyed by xbw at 2011-10-08
229 //rk29_sdmmc_request_end..2336... CMD12 wait busy timeout!!!!! ====xbw=[sd_mmc]====
230 //mmc_wait_for_req..236.. !!!!! wait for CMD25 timeout ===xbw[mmc0]===
234 multi = (mrq->cmd->retries>0)?mrq->cmd->retries:1;
235 waittime = wait_for_completion_timeout(&complete,HZ*7*multi);
242 mrq->cmd->error = -EIO;
243 printk("%s..%d.. !!!!! wait for CMD%d timeout ===xbw[%s]===\n",\
244 __FUNCTION__, __LINE__, mrq->cmd->opcode, mmc_hostname(host));
247 wait_for_completion(&complete);
251 EXPORT_SYMBOL(mmc_wait_for_req);
254 * mmc_wait_for_cmd - start a command and wait for completion
255 * @host: MMC host to start command
256 * @cmd: MMC command to start
257 * @retries: maximum number of retries
259 * Start a new MMC command for a host, and wait for the command
260 * to complete. Return any error that occurred while the command
261 * was executing. Do not attempt to parse the response.
263 int mmc_wait_for_cmd(struct mmc_host *host, struct mmc_command *cmd, int retries)
265 struct mmc_request mrq;
267 WARN_ON(!host->claimed);
269 memset(&mrq, 0, sizeof(struct mmc_request));
271 memset(cmd->resp, 0, sizeof(cmd->resp));
272 cmd->retries = retries;
277 mmc_wait_for_req(host, &mrq);
282 EXPORT_SYMBOL(mmc_wait_for_cmd);
285 * mmc_set_data_timeout - set the timeout for a data command
286 * @data: data phase for command
287 * @card: the MMC card associated with the data transfer
289 * Computes the data timeout parameters according to the
290 * correct algorithm given the card type.
292 void mmc_set_data_timeout(struct mmc_data *data, const struct mmc_card *card)
297 * SDIO cards only define an upper 1 s limit on access.
299 if (mmc_card_sdio(card)) {
300 data->timeout_ns = 1000000000;
301 data->timeout_clks = 0;
306 * SD cards use a 100 multiplier rather than 10
308 mult = mmc_card_sd(card) ? 100 : 10;
311 * Scale up the multiplier (and therefore the timeout) by
312 * the r2w factor for writes.
314 if (data->flags & MMC_DATA_WRITE)
315 mult <<= card->csd.r2w_factor;
317 data->timeout_ns = card->csd.tacc_ns * mult;
318 data->timeout_clks = card->csd.tacc_clks * mult;
321 * SD cards also have an upper limit on the timeout.
323 if (mmc_card_sd(card)) {
324 unsigned int timeout_us, limit_us;
326 timeout_us = data->timeout_ns / 1000;
327 timeout_us += data->timeout_clks * 1000 /
328 (card->host->ios.clock / 1000);
330 if (data->flags & MMC_DATA_WRITE)
332 * The limit is really 250 ms, but that is
333 * insufficient for some crappy cards.
340 * SDHC cards always use these fixed values.
342 if (timeout_us > limit_us || mmc_card_blockaddr(card)) {
343 data->timeout_ns = limit_us * 1000;
344 data->timeout_clks = 0;
348 * Some cards need very high timeouts if driven in SPI mode.
349 * The worst observed timeout was 900ms after writing a
350 * continuous stream of data until the internal logic
353 if (mmc_host_is_spi(card->host)) {
354 if (data->flags & MMC_DATA_WRITE) {
355 if (data->timeout_ns < 1000000000)
356 data->timeout_ns = 1000000000; /* 1s */
358 if (data->timeout_ns < 100000000)
359 data->timeout_ns = 100000000; /* 100ms */
363 EXPORT_SYMBOL(mmc_set_data_timeout);
366 * mmc_align_data_size - pads a transfer size to a more optimal value
367 * @card: the MMC card associated with the data transfer
368 * @sz: original transfer size
370 * Pads the original data size with a number of extra bytes in
371 * order to avoid controller bugs and/or performance hits
372 * (e.g. some controllers revert to PIO for certain sizes).
374 * Returns the improved size, which might be unmodified.
376 * Note that this function is only relevant when issuing a
377 * single scatter gather entry.
379 unsigned int mmc_align_data_size(struct mmc_card *card, unsigned int sz)
382 * FIXME: We don't have a system for the controller to tell
383 * the core about its problems yet, so for now we just 32-bit
386 sz = ((sz + 3) / 4) * 4;
390 EXPORT_SYMBOL(mmc_align_data_size);
393 * mmc_host_enable - enable a host.
394 * @host: mmc host to enable
396 * Hosts that support power saving can use the 'enable' and 'disable'
397 * methods to exit and enter power saving states. For more information
398 * see comments for struct mmc_host_ops.
400 int mmc_host_enable(struct mmc_host *host)
402 if (!(host->caps & MMC_CAP_DISABLE))
405 if (host->en_dis_recurs)
408 if (host->nesting_cnt++)
411 cancel_delayed_work_sync(&host->disable);
416 if (host->ops->enable) {
419 host->en_dis_recurs = 1;
420 err = host->ops->enable(host);
421 host->en_dis_recurs = 0;
424 pr_debug("%s: enable error %d\n",
425 mmc_hostname(host), err);
432 EXPORT_SYMBOL(mmc_host_enable);
434 static int mmc_host_do_disable(struct mmc_host *host, int lazy)
436 if (host->ops->disable) {
439 host->en_dis_recurs = 1;
440 err = host->ops->disable(host, lazy);
441 host->en_dis_recurs = 0;
444 pr_debug("%s: disable error %d\n",
445 mmc_hostname(host), err);
449 unsigned long delay = msecs_to_jiffies(err);
451 mmc_schedule_delayed_work(&host->disable, delay);
459 * mmc_host_disable - disable a host.
460 * @host: mmc host to disable
462 * Hosts that support power saving can use the 'enable' and 'disable'
463 * methods to exit and enter power saving states. For more information
464 * see comments for struct mmc_host_ops.
466 int mmc_host_disable(struct mmc_host *host)
470 if (!(host->caps & MMC_CAP_DISABLE))
473 if (host->en_dis_recurs)
476 if (--host->nesting_cnt)
482 err = mmc_host_do_disable(host, 0);
485 EXPORT_SYMBOL(mmc_host_disable);
488 * __mmc_claim_host - exclusively claim a host
489 * @host: mmc host to claim
490 * @abort: whether or not the operation should be aborted
492 * Claim a host for a set of operations. If @abort is non null and
493 * dereference a non-zero value then this will return prematurely with
494 * that non-zero value without acquiring the lock. Returns zero
495 * with the lock held otherwise.
497 int __mmc_claim_host(struct mmc_host *host, atomic_t *abort)
499 DECLARE_WAITQUEUE(wait, current);
505 add_wait_queue(&host->wq, &wait);
506 spin_lock_irqsave(&host->lock, flags);
508 set_current_state(TASK_UNINTERRUPTIBLE);
509 stop = abort ? atomic_read(abort) : 0;
510 if (stop || !host->claimed || host->claimer == current)
512 spin_unlock_irqrestore(&host->lock, flags);
514 spin_lock_irqsave(&host->lock, flags);
516 set_current_state(TASK_RUNNING);
519 host->claimer = current;
520 host->claim_cnt += 1;
523 spin_unlock_irqrestore(&host->lock, flags);
524 remove_wait_queue(&host->wq, &wait);
526 mmc_host_enable(host);
530 EXPORT_SYMBOL(__mmc_claim_host);
533 * mmc_try_claim_host - try exclusively to claim a host
534 * @host: mmc host to claim
536 * Returns %1 if the host is claimed, %0 otherwise.
538 int mmc_try_claim_host(struct mmc_host *host)
540 int claimed_host = 0;
543 spin_lock_irqsave(&host->lock, flags);
544 if (!host->claimed || host->claimer == current) {
546 host->claimer = current;
547 host->claim_cnt += 1;
550 spin_unlock_irqrestore(&host->lock, flags);
553 EXPORT_SYMBOL(mmc_try_claim_host);
555 static void mmc_do_release_host(struct mmc_host *host)
559 spin_lock_irqsave(&host->lock, flags);
560 if (--host->claim_cnt) {
561 /* Release for nested claim */
562 spin_unlock_irqrestore(&host->lock, flags);
565 host->claimer = NULL;
566 spin_unlock_irqrestore(&host->lock, flags);
571 void mmc_host_deeper_disable(struct work_struct *work)
573 struct mmc_host *host =
574 container_of(work, struct mmc_host, disable.work);
576 /* If the host is claimed then we do not want to disable it anymore */
577 if (!mmc_try_claim_host(host))
579 mmc_host_do_disable(host, 1);
580 mmc_do_release_host(host);
583 wake_unlock(&mmc_delayed_work_wake_lock);
587 * mmc_host_lazy_disable - lazily disable a host.
588 * @host: mmc host to disable
590 * Hosts that support power saving can use the 'enable' and 'disable'
591 * methods to exit and enter power saving states. For more information
592 * see comments for struct mmc_host_ops.
594 int mmc_host_lazy_disable(struct mmc_host *host)
596 if (!(host->caps & MMC_CAP_DISABLE))
599 if (host->en_dis_recurs)
602 if (--host->nesting_cnt)
608 if (host->disable_delay) {
609 mmc_schedule_delayed_work(&host->disable,
610 msecs_to_jiffies(host->disable_delay));
613 return mmc_host_do_disable(host, 1);
615 EXPORT_SYMBOL(mmc_host_lazy_disable);
618 * mmc_release_host - release a host
619 * @host: mmc host to release
621 * Release a MMC host, allowing others to claim the host
622 * for their operations.
624 void mmc_release_host(struct mmc_host *host)
626 WARN_ON(!host->claimed);
628 mmc_host_lazy_disable(host);
630 mmc_do_release_host(host);
633 EXPORT_SYMBOL(mmc_release_host);
636 * Internal function that does the actual ios call to the host driver,
637 * optionally printing some debug output.
639 static inline void mmc_set_ios(struct mmc_host *host)
641 struct mmc_ios *ios = &host->ios;
643 pr_debug("%s: clock %uHz busmode %u powermode %u cs %u Vdd %u "
644 "width %u timing %u\n",
645 mmc_hostname(host), ios->clock, ios->bus_mode,
646 ios->power_mode, ios->chip_select, ios->vdd,
647 ios->bus_width, ios->timing);
649 host->ops->set_ios(host, ios);
653 * Control chip select pin on a host.
655 void mmc_set_chip_select(struct mmc_host *host, int mode)
657 host->ios.chip_select = mode;
662 * Sets the host clock to the highest possible frequency that
665 void mmc_set_clock(struct mmc_host *host, unsigned int hz)
667 WARN_ON(hz < host->f_min);
669 if (hz > host->f_max)
672 host->ios.clock = hz;
677 * Change the bus mode (open drain/push-pull) of a host.
679 void mmc_set_bus_mode(struct mmc_host *host, unsigned int mode)
681 host->ios.bus_mode = mode;
686 * Change data bus width of a host.
688 void mmc_set_bus_width(struct mmc_host *host, unsigned int width)
690 host->ios.bus_width = width;
695 * mmc_vdd_to_ocrbitnum - Convert a voltage to the OCR bit number
697 * @low_bits: prefer low bits in boundary cases
699 * This function returns the OCR bit number according to the provided @vdd
700 * value. If conversion is not possible a negative errno value returned.
702 * Depending on the @low_bits flag the function prefers low or high OCR bits
703 * on boundary voltages. For example,
704 * with @low_bits = true, 3300 mV translates to ilog2(MMC_VDD_32_33);
705 * with @low_bits = false, 3300 mV translates to ilog2(MMC_VDD_33_34);
707 * Any value in the [1951:1999] range translates to the ilog2(MMC_VDD_20_21).
709 static int mmc_vdd_to_ocrbitnum(int vdd, bool low_bits)
711 const int max_bit = ilog2(MMC_VDD_35_36);
714 if (vdd < 1650 || vdd > 3600)
717 if (vdd >= 1650 && vdd <= 1950)
718 return ilog2(MMC_VDD_165_195);
723 /* Base 2000 mV, step 100 mV, bit's base 8. */
724 bit = (vdd - 2000) / 100 + 8;
731 * mmc_vddrange_to_ocrmask - Convert a voltage range to the OCR mask
732 * @vdd_min: minimum voltage value (mV)
733 * @vdd_max: maximum voltage value (mV)
735 * This function returns the OCR mask bits according to the provided @vdd_min
736 * and @vdd_max values. If conversion is not possible the function returns 0.
738 * Notes wrt boundary cases:
739 * This function sets the OCR bits for all boundary voltages, for example
740 * [3300:3400] range is translated to MMC_VDD_32_33 | MMC_VDD_33_34 |
741 * MMC_VDD_34_35 mask.
743 u32 mmc_vddrange_to_ocrmask(int vdd_min, int vdd_max)
747 if (vdd_max < vdd_min)
750 /* Prefer high bits for the boundary vdd_max values. */
751 vdd_max = mmc_vdd_to_ocrbitnum(vdd_max, false);
755 /* Prefer low bits for the boundary vdd_min values. */
756 vdd_min = mmc_vdd_to_ocrbitnum(vdd_min, true);
760 /* Fill the mask, from max bit to min bit. */
761 while (vdd_max >= vdd_min)
762 mask |= 1 << vdd_max--;
766 EXPORT_SYMBOL(mmc_vddrange_to_ocrmask);
768 #ifdef CONFIG_REGULATOR
771 * mmc_regulator_get_ocrmask - return mask of supported voltages
772 * @supply: regulator to use
774 * This returns either a negative errno, or a mask of voltages that
775 * can be provided to MMC/SD/SDIO devices using the specified voltage
776 * regulator. This would normally be called before registering the
779 int mmc_regulator_get_ocrmask(struct regulator *supply)
785 count = regulator_count_voltages(supply);
789 for (i = 0; i < count; i++) {
793 vdd_uV = regulator_list_voltage(supply, i);
797 vdd_mV = vdd_uV / 1000;
798 result |= mmc_vddrange_to_ocrmask(vdd_mV, vdd_mV);
803 EXPORT_SYMBOL(mmc_regulator_get_ocrmask);
806 * mmc_regulator_set_ocr - set regulator to match host->ios voltage
807 * @vdd_bit: zero for power off, else a bit number (host->ios.vdd)
808 * @supply: regulator to use
810 * Returns zero on success, else negative errno.
812 * MMC host drivers may use this to enable or disable a regulator using
813 * a particular supply voltage. This would normally be called from the
816 int mmc_regulator_set_ocr(struct regulator *supply, unsigned short vdd_bit)
822 enabled = regulator_is_enabled(supply);
830 /* REVISIT mmc_vddrange_to_ocrmask() may have set some
831 * bits this regulator doesn't quite support ... don't
832 * be too picky, most cards and regulators are OK with
833 * a 0.1V range goof (it's a small error percentage).
835 tmp = vdd_bit - ilog2(MMC_VDD_165_195);
837 min_uV = 1650 * 1000;
838 max_uV = 1950 * 1000;
840 min_uV = 1900 * 1000 + tmp * 100 * 1000;
841 max_uV = min_uV + 100 * 1000;
844 /* avoid needless changes to this voltage; the regulator
845 * might not allow this operation
847 voltage = regulator_get_voltage(supply);
850 else if (voltage < min_uV || voltage > max_uV)
851 result = regulator_set_voltage(supply, min_uV, max_uV);
855 if (result == 0 && !enabled)
856 result = regulator_enable(supply);
857 } else if (enabled) {
858 result = regulator_disable(supply);
863 EXPORT_SYMBOL(mmc_regulator_set_ocr);
868 * Mask off any voltages we don't support and select
871 u32 mmc_select_voltage(struct mmc_host *host, u32 ocr)
875 ocr &= host->ocr_avail;
886 pr_warning("%s: host doesn't support card's voltages\n",
895 * Select timing parameters for host.
897 void mmc_set_timing(struct mmc_host *host, unsigned int timing)
899 host->ios.timing = timing;
904 * Apply power to the MMC stack. This is a two-stage process.
905 * First, we enable power to the card without the clock running.
906 * We then wait a bit for the power to stabilise. Finally,
907 * enable the bus drivers and clock to the card.
909 * We must _NOT_ enable the clock prior to power stablising.
911 * If a host does all the power sequencing itself, ignore the
912 * initial MMC_POWER_UP stage.
914 static void mmc_power_up(struct mmc_host *host)
918 /* If ocr is set, we use it */
920 bit = ffs(host->ocr) - 1;
922 bit = fls(host->ocr_avail) - 1;
925 if (mmc_host_is_spi(host)) {
926 host->ios.chip_select = MMC_CS_HIGH;
927 host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
929 host->ios.chip_select = MMC_CS_DONTCARE;
930 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
932 host->ios.power_mode = MMC_POWER_UP;
933 host->ios.bus_width = MMC_BUS_WIDTH_1;
934 host->ios.timing = MMC_TIMING_LEGACY;
938 * This delay should be sufficient to allow the power supply
939 * to reach the minimum voltage.
943 host->ios.clock = host->f_min;
945 host->ios.power_mode = MMC_POWER_ON;
949 * This delay must be at least 74 clock sizes, or 1 ms, or the
950 * time required to reach a stable voltage.
955 static void mmc_power_off(struct mmc_host *host)
959 if (!mmc_host_is_spi(host)) {
960 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
961 host->ios.chip_select = MMC_CS_DONTCARE;
963 host->ios.power_mode = MMC_POWER_OFF;
964 host->ios.bus_width = MMC_BUS_WIDTH_1;
965 host->ios.timing = MMC_TIMING_LEGACY;
970 * Cleanup when the last reference to the bus operator is dropped.
972 static void __mmc_release_bus(struct mmc_host *host)
975 BUG_ON(host->bus_refs);
976 BUG_ON(!host->bus_dead);
978 host->bus_ops = NULL;
982 * Increase reference count of bus operator
984 static inline void mmc_bus_get(struct mmc_host *host)
988 spin_lock_irqsave(&host->lock, flags);
990 spin_unlock_irqrestore(&host->lock, flags);
994 * Decrease reference count of bus operator and free it if
995 * it is the last reference.
997 static inline void mmc_bus_put(struct mmc_host *host)
1001 spin_lock_irqsave(&host->lock, flags);
1003 if ((host->bus_refs == 0) && host->bus_ops)
1004 __mmc_release_bus(host);
1005 spin_unlock_irqrestore(&host->lock, flags);
1008 int mmc_resume_bus(struct mmc_host *host)
1010 if (!mmc_bus_needs_resume(host))
1013 printk("%s: Starting deferred resume\n", mmc_hostname(host));
1014 host->bus_resume_flags &= ~MMC_BUSRESUME_NEEDS_RESUME;
1016 if (host->bus_ops && !host->bus_dead) {
1018 BUG_ON(!host->bus_ops->resume);
1019 host->bus_ops->resume(host);
1022 if (host->bus_ops->detect && !host->bus_dead)
1023 host->bus_ops->detect(host);
1026 printk("%s: Deferred resume completed\n", mmc_hostname(host));
1030 EXPORT_SYMBOL(mmc_resume_bus);
1033 * Assign a mmc bus handler to a host. Only one bus handler may control a
1034 * host at any given time.
1036 void mmc_attach_bus(struct mmc_host *host, const struct mmc_bus_ops *ops)
1038 unsigned long flags;
1043 WARN_ON(!host->claimed);
1045 spin_lock_irqsave(&host->lock, flags);
1047 BUG_ON(host->bus_ops);
1048 BUG_ON(host->bus_refs);
1050 host->bus_ops = ops;
1054 spin_unlock_irqrestore(&host->lock, flags);
1058 * Remove the current bus handler from a host. Assumes that there are
1059 * no interesting cards left, so the bus is powered down.
1061 void mmc_detach_bus(struct mmc_host *host)
1063 unsigned long flags;
1067 WARN_ON(!host->claimed);
1068 WARN_ON(!host->bus_ops);
1070 spin_lock_irqsave(&host->lock, flags);
1074 spin_unlock_irqrestore(&host->lock, flags);
1076 mmc_power_off(host);
1082 * mmc_detect_change - process change of state on a MMC socket
1083 * @host: host which changed state.
1084 * @delay: optional delay to wait before detection (jiffies)
1086 * MMC drivers should call this when they detect a card has been
1087 * inserted or removed. The MMC layer will confirm that any
1088 * present card is still functional, and initialize any newly
1091 void mmc_detect_change(struct mmc_host *host, unsigned long delay)
1093 #ifdef CONFIG_MMC_DEBUG
1094 unsigned long flags;
1095 spin_lock_irqsave(&host->lock, flags);
1096 WARN_ON(host->removed);
1097 spin_unlock_irqrestore(&host->lock, flags);
1100 mmc_schedule_delayed_work(&host->detect, delay);
1103 EXPORT_SYMBOL(mmc_detect_change);
1105 #if defined(CONFIG_SDMMC_RK29) && !defined(CONFIG_SDMMC_RK29_OLD)
1106 void mmc_rescan(struct work_struct *work)
1108 struct mmc_host *host =
1109 container_of(work, struct mmc_host, detect.work);
1112 int extend_wakelock = 0;
1117 /* if there is a card registered, check whether it is still present */
1118 if ((host->bus_ops != NULL) && host->bus_ops->detect && !host->bus_dead)
1119 host->bus_ops->detect(host);
1121 /* If the card was removed the bus will be marked
1122 * as dead - extend the wakelock so userspace
1125 extend_wakelock = 1;
1132 /* if there still is a card present, stop here */
1133 if (host->bus_ops != NULL) {
1138 /* detect a newly inserted card */
1141 * Only we can add a new handler, so it's safe to
1142 * release the lock here.
1145 printk("\n%s...%d.. ===== mmc_rescan Begin....======xbw[%s]=====\n",__FILE__, __LINE__, mmc_hostname(host));
1147 if (host->ops->get_cd && host->ops->get_cd(host) == 0)
1149 printk("\n=================\n%s..%d.. ====find no SDMMC host.====xbw[%s]=====\n", \
1150 __FUNCTION__, __LINE__, mmc_hostname(host));
1155 mmc_claim_host(host);
1162 In oder to improve the initialization process in rockchip IC, I modify the following code about the the initialization process of SDIO-SD-MMC.
1163 So I deleted the CMD8 and add a conditional to distinguish between the two card type,i.e.SDMMC process and SDIO process.
1164 For detail,please refer to "RK29XX Technical Reference Manual" and "SD-MMC-SDIO Specifications".
1165 Noted by xbw@2011-04-09
1168 //mmc_send_if_cond(host, host->ocr_avail); //deleted by xbw@2011-04-09
1169 #if !defined(CONFIG_USE_SDMMC0_FOR_WIFI_DEVELOP_BOARD)
1170 if( strncmp( mmc_hostname(host) ,"mmc0" , strlen("mmc0")) ){
1173 * First we search for SDIO...
1175 err = mmc_send_io_op_cond(host, 0, &ocr);
1177 printk("\n%s..%d.. ===== Begin to identify card as SDIO-card===xbw[%s]===\n",__FUNCTION__, __LINE__, mmc_hostname(host));
1179 if (mmc_attach_sdio(host, ocr))
1181 printk("\n=====\n %s..%d.. ===== Initialize SDIO-card unsuccessfully!!! ===xbw[%s]===\n=====\n",\
1182 __FUNCTION__, __LINE__, mmc_hostname(host));
1184 mmc_power_off(host);
1188 printk("%s..%d.. ===== Initialize SDIO successfully. ===xbw[%s]===\n",__FUNCTION__, __LINE__, mmc_hostname(host));
1190 extend_wakelock = 1;
1193 #if !defined(CONFIG_USE_SDMMC0_FOR_WIFI_DEVELOP_BOARD)
1198 * ...then normal SD...
1200 err = mmc_send_app_op_cond(host, 0, &ocr);
1202 printk("\n%s..%d.. ===== Begin to identify card as SD-card ===xbw[%s]===\n",__FUNCTION__, __LINE__, mmc_hostname(host));
1204 if (mmc_attach_sd(host, ocr))
1206 printk("\n=====\n%s..%d.. ===== Initialize SD-card unsuccessfully!!! ===xbw[%s]===\n====\n",\
1207 __FUNCTION__, __LINE__, mmc_hostname(host));
1209 mmc_power_off(host);
1213 printk("%s..%d.. ===== Initialize SD-card successfully. ===xbw[%s]===\n",__FUNCTION__, __LINE__, mmc_hostname(host));
1215 extend_wakelock = 1;
1220 * ...and finally MMC.
1222 err = mmc_send_op_cond(host, 0, &ocr);
1224 printk("\n%s..%d.. ===== Begin to identify card as MMC-card ===xbw[%s]===\n", __FUNCTION__, __LINE__, mmc_hostname(host));
1226 if (mmc_attach_mmc(host, ocr))
1228 printk("\n =====\n%s..%d.. ===== Initialize MMC-card unsuccessfully!!! ===xbw[%s]===\n======\n",\
1229 __FUNCTION__, __LINE__, mmc_hostname(host));
1231 mmc_power_off(host);
1235 printk("%s...%d.. ===== Initialize MMC-card successfully. ===xbw[%s]===\n",__FUNCTION__, __LINE__, mmc_hostname(host));
1237 extend_wakelock = 1;
1241 mmc_release_host(host);
1242 mmc_power_off(host);
1246 if (extend_wakelock)
1247 wake_lock_timeout(&mmc_delayed_work_wake_lock, HZ / 2);
1249 wake_unlock(&mmc_delayed_work_wake_lock);
1251 if (host->caps & MMC_CAP_NEEDS_POLL)
1252 mmc_schedule_delayed_work(&host->detect, HZ);
1256 void mmc_rescan(struct work_struct *work)
1258 struct mmc_host *host =
1259 container_of(work, struct mmc_host, detect.work);
1262 int extend_wakelock = 0;
1264 #if defined(CONFIG_SDMMC_RK29) && defined(CONFIG_SDMMC_RK29_OLD)
1265 unsigned long flags;
1267 spin_lock_irqsave(&host->lock, flags);
1269 if (host->rescan_disable) {
1270 spin_unlock_irqrestore(&host->lock, flags);
1274 spin_unlock_irqrestore(&host->lock, flags);
1280 /* if there is a card registered, check whether it is still present */
1281 if ((host->bus_ops != NULL) && host->bus_ops->detect && !host->bus_dead)
1282 host->bus_ops->detect(host);
1284 /* If the card was removed the bus will be marked
1285 * as dead - extend the wakelock so userspace
1288 extend_wakelock = 1;
1295 /* if there still is a card present, stop here */
1296 if (host->bus_ops != NULL) {
1301 /* detect a newly inserted card */
1304 * Only we can add a new handler, so it's safe to
1305 * release the lock here.
1309 if (host->ops->get_cd && host->ops->get_cd(host) == 0)
1312 mmc_claim_host(host);
1317 mmc_send_if_cond(host, host->ocr_avail);
1320 * First we search for SDIO...
1322 err = mmc_send_io_op_cond(host, 0, &ocr);
1324 if (mmc_attach_sdio(host, ocr))
1325 mmc_power_off(host);
1326 extend_wakelock = 1;
1331 * ...then normal SD...
1333 err = mmc_send_app_op_cond(host, 0, &ocr);
1335 if (mmc_attach_sd(host, ocr))
1336 mmc_power_off(host);
1337 extend_wakelock = 1;
1342 * ...and finally MMC.
1344 err = mmc_send_op_cond(host, 0, &ocr);
1346 if (mmc_attach_mmc(host, ocr))
1347 mmc_power_off(host);
1348 extend_wakelock = 1;
1352 mmc_release_host(host);
1353 mmc_power_off(host);
1356 if (extend_wakelock)
1357 wake_lock_timeout(&mmc_delayed_work_wake_lock, HZ / 2);
1359 wake_unlock(&mmc_delayed_work_wake_lock);
1361 if (host->caps & MMC_CAP_NEEDS_POLL)
1362 mmc_schedule_delayed_work(&host->detect, HZ);
1366 void mmc_start_host(struct mmc_host *host)
1368 mmc_power_off(host);
1369 mmc_detect_change(host, 0);
1372 void mmc_stop_host(struct mmc_host *host)
1374 #ifdef CONFIG_MMC_DEBUG
1375 unsigned long flags;
1376 spin_lock_irqsave(&host->lock, flags);
1378 spin_unlock_irqrestore(&host->lock, flags);
1381 if (host->caps & MMC_CAP_DISABLE)
1382 cancel_delayed_work(&host->disable);
1383 cancel_delayed_work(&host->detect);
1384 mmc_flush_scheduled_work();
1387 if (host->bus_ops && !host->bus_dead) {
1388 if (host->bus_ops->remove)
1389 host->bus_ops->remove(host);
1391 mmc_claim_host(host);
1392 mmc_detach_bus(host);
1393 mmc_release_host(host);
1401 mmc_power_off(host);
1404 void mmc_power_save_host(struct mmc_host *host)
1408 if (!host->bus_ops || host->bus_dead || !host->bus_ops->power_restore) {
1413 if (host->bus_ops->power_save)
1414 host->bus_ops->power_save(host);
1418 mmc_power_off(host);
1420 EXPORT_SYMBOL(mmc_power_save_host);
1422 void mmc_power_restore_host(struct mmc_host *host)
1426 if (!host->bus_ops || host->bus_dead || !host->bus_ops->power_restore) {
1432 host->bus_ops->power_restore(host);
1436 EXPORT_SYMBOL(mmc_power_restore_host);
1438 int mmc_card_awake(struct mmc_host *host)
1444 if (host->bus_ops && !host->bus_dead && host->bus_ops->awake)
1445 err = host->bus_ops->awake(host);
1451 EXPORT_SYMBOL(mmc_card_awake);
1453 int mmc_card_sleep(struct mmc_host *host)
1459 if (host->bus_ops && !host->bus_dead && host->bus_ops->awake)
1460 err = host->bus_ops->sleep(host);
1466 EXPORT_SYMBOL(mmc_card_sleep);
1468 int mmc_card_can_sleep(struct mmc_host *host)
1470 struct mmc_card *card = host->card;
1472 if (card && mmc_card_mmc(card) && card->ext_csd.rev >= 3)
1476 EXPORT_SYMBOL(mmc_card_can_sleep);
1481 * mmc_suspend_host - suspend a host
1483 * @state: suspend mode (PM_SUSPEND_xxx)
1485 int mmc_suspend_host(struct mmc_host *host, pm_message_t state)
1489 if (mmc_bus_needs_resume(host))
1492 if (host->caps & MMC_CAP_DISABLE)
1493 cancel_delayed_work(&host->disable);
1494 cancel_delayed_work(&host->detect);
1495 mmc_flush_scheduled_work();
1498 if (host->bus_ops && !host->bus_dead) {
1499 if (host->bus_ops->suspend)
1500 err = host->bus_ops->suspend(host);
1502 #if defined(CONFIG_SDMMC_RK29) && defined(CONFIG_SDMMC_RK29_OLD)
1503 //deleted all detail code.
1505 if (err == -ENOSYS || !host->bus_ops->resume) {
1507 * We simply "remove" the card in this case.
1508 * It will be redetected on resume.
1510 if (host->bus_ops->remove)
1511 host->bus_ops->remove(host);
1512 mmc_claim_host(host);
1513 mmc_detach_bus(host);
1514 mmc_release_host(host);
1522 mmc_power_off(host);
1527 EXPORT_SYMBOL(mmc_suspend_host);
1530 * mmc_resume_host - resume a previously suspended host
1533 int mmc_resume_host(struct mmc_host *host)
1538 if (host->bus_resume_flags & MMC_BUSRESUME_MANUAL_RESUME) {
1539 host->bus_resume_flags |= MMC_BUSRESUME_NEEDS_RESUME;
1544 if (host->bus_ops && !host->bus_dead) {
1546 mmc_select_voltage(host, host->ocr);
1547 BUG_ON(!host->bus_ops->resume);
1549 #if defined(CONFIG_SDMMC_RK29) && defined(CONFIG_SDMMC_RK29_OLD)
1550 err = host->bus_ops->resume(host);
1552 printk(KERN_WARNING "%s: error %d during resume "
1553 "(card was removed?)\n",
1554 mmc_hostname(host), err);
1558 #elif defined(CONFIG_SDMMC_RK29) && !defined(CONFIG_SDMMC_RK29_OLD)
1559 //panic if the card is being removed during the resume, deleted by xbw at 2011-06-20
1560 host->bus_ops->resume(host);
1563 err = host->bus_ops->resume(host);
1565 printk(KERN_WARNING "%s: error %d during resume "
1566 "(card was removed?)\n",
1567 mmc_hostname(host), err);
1568 if (host->bus_ops->remove)
1569 host->bus_ops->remove(host);
1570 mmc_claim_host(host);
1571 mmc_detach_bus(host);
1572 mmc_release_host(host);
1573 /* no need to bother upper layers */
1580 #if defined(CONFIG_SDMMC_RK29) && defined(CONFIG_SDMMC_RK29_OLD)
1581 //delete the detect_change.
1584 * We add a slight delay here so that resume can progress
1587 mmc_detect_change(host, 1);
1591 EXPORT_SYMBOL(mmc_resume_host);
1593 ///old driver add the function reforming to kernel2.6.38
1594 #if defined(CONFIG_SDMMC_RK29) && defined(CONFIG_SDMMC_RK29_OLD)
1595 /* Do the card removal on suspend if card is assumed removeable
1596 * Do that in pm notifier while userspace isn't yet frozen, so we will be able
1599 int mmc_pm_notify(struct notifier_block *notify_block,
1600 unsigned long mode, void *unused)
1602 struct mmc_host *host = container_of(
1603 notify_block, struct mmc_host, pm_notify);
1604 unsigned long flags;
1608 case PM_HIBERNATION_PREPARE:
1609 case PM_SUSPEND_PREPARE:
1611 spin_lock_irqsave(&host->lock, flags);
1612 host->rescan_disable = 1;
1613 spin_unlock_irqrestore(&host->lock, flags);
1614 cancel_delayed_work_sync(&host->detect);
1616 if (!host->bus_ops || host->bus_ops->suspend)
1619 mmc_claim_host(host);
1621 if (host->bus_ops->remove)
1622 host->bus_ops->remove(host);
1624 mmc_detach_bus(host);
1625 mmc_release_host(host);
1628 case PM_POST_SUSPEND:
1629 case PM_POST_HIBERNATION:
1631 spin_lock_irqsave(&host->lock, flags);
1632 host->rescan_disable = 0;
1633 spin_unlock_irqrestore(&host->lock, flags);
1634 mmc_detect_change(host, 0);
1640 #endif// endif---defined(CONFIG_SDMMC_RK29) && defined(CONFIG_SDMMC_RK29_OLD)
1644 #ifdef CONFIG_MMC_EMBEDDED_SDIO
1645 void mmc_set_embedded_sdio_data(struct mmc_host *host,
1646 struct sdio_cis *cis,
1647 struct sdio_cccr *cccr,
1648 struct sdio_embedded_func *funcs,
1651 host->embedded_sdio_data.cis = cis;
1652 host->embedded_sdio_data.cccr = cccr;
1653 host->embedded_sdio_data.funcs = funcs;
1654 host->embedded_sdio_data.num_funcs = num_funcs;
1657 EXPORT_SYMBOL(mmc_set_embedded_sdio_data);
1660 static int __init mmc_init(void)
1664 wake_lock_init(&mmc_delayed_work_wake_lock, WAKE_LOCK_SUSPEND, "mmc_delayed_work");
1666 workqueue = create_singlethread_workqueue("kmmcd");
1670 ret = mmc_register_bus();
1672 goto destroy_workqueue;
1674 ret = mmc_register_host_class();
1676 goto unregister_bus;
1678 ret = sdio_register_bus();
1680 goto unregister_host_class;
1684 unregister_host_class:
1685 mmc_unregister_host_class();
1687 mmc_unregister_bus();
1689 destroy_workqueue(workqueue);
1694 static void __exit mmc_exit(void)
1696 sdio_unregister_bus();
1697 mmc_unregister_host_class();
1698 mmc_unregister_bus();
1699 destroy_workqueue(workqueue);
1700 wake_lock_destroy(&mmc_delayed_work_wake_lock);
1703 subsys_initcall(mmc_init);
1704 module_exit(mmc_exit);
1706 MODULE_LICENSE("GPL");