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 waittime = wait_for_completion_timeout(&complete,HZ*2); //sdio; for cmd dead. Modifyed by xbw at 2011-06-02
215 //calculate the timeout value for SDMMC; added by xbw at 2011-09-27
218 unit = 3*(1<<20);// unit=3MB
219 datasize = mrq->data->blksz*mrq->data->blocks;
220 multi = datasize/unit;
221 multi += (datasize%unit)?1:0;
222 multi = (multi>0) ? multi : 1;
223 multi += (mrq->cmd->retries>0)?1:0;
224 waittime = wait_for_completion_timeout(&complete,HZ*5*multi); //It should be longer than bottom driver's time,due to the sum of two cmd time.
225 //modifyed by xbw at 2011-10-08
228 //rk29_sdmmc_request_end..2336... CMD12 wait busy timeout!!!!! ====xbw=[sd_mmc]====
229 //mmc_wait_for_req..236.. !!!!! wait for CMD25 timeout ===xbw[mmc0]===
233 waittime = wait_for_completion_timeout(&complete,HZ*2);
240 mrq->cmd->error = -EIO;
241 printk("%s..%d.. !!!!! wait for CMD%d timeout ===xbw[%s]===\n",\
242 __FUNCTION__, __LINE__, mrq->cmd->opcode, mmc_hostname(host));
245 wait_for_completion(&complete);
249 EXPORT_SYMBOL(mmc_wait_for_req);
252 * mmc_wait_for_cmd - start a command and wait for completion
253 * @host: MMC host to start command
254 * @cmd: MMC command to start
255 * @retries: maximum number of retries
257 * Start a new MMC command for a host, and wait for the command
258 * to complete. Return any error that occurred while the command
259 * was executing. Do not attempt to parse the response.
261 int mmc_wait_for_cmd(struct mmc_host *host, struct mmc_command *cmd, int retries)
263 struct mmc_request mrq;
265 WARN_ON(!host->claimed);
267 memset(&mrq, 0, sizeof(struct mmc_request));
269 memset(cmd->resp, 0, sizeof(cmd->resp));
270 cmd->retries = retries;
275 mmc_wait_for_req(host, &mrq);
280 EXPORT_SYMBOL(mmc_wait_for_cmd);
283 * mmc_set_data_timeout - set the timeout for a data command
284 * @data: data phase for command
285 * @card: the MMC card associated with the data transfer
287 * Computes the data timeout parameters according to the
288 * correct algorithm given the card type.
290 void mmc_set_data_timeout(struct mmc_data *data, const struct mmc_card *card)
295 * SDIO cards only define an upper 1 s limit on access.
297 if (mmc_card_sdio(card)) {
298 data->timeout_ns = 1000000000;
299 data->timeout_clks = 0;
304 * SD cards use a 100 multiplier rather than 10
306 mult = mmc_card_sd(card) ? 100 : 10;
309 * Scale up the multiplier (and therefore the timeout) by
310 * the r2w factor for writes.
312 if (data->flags & MMC_DATA_WRITE)
313 mult <<= card->csd.r2w_factor;
315 data->timeout_ns = card->csd.tacc_ns * mult;
316 data->timeout_clks = card->csd.tacc_clks * mult;
319 * SD cards also have an upper limit on the timeout.
321 if (mmc_card_sd(card)) {
322 unsigned int timeout_us, limit_us;
324 timeout_us = data->timeout_ns / 1000;
325 timeout_us += data->timeout_clks * 1000 /
326 (card->host->ios.clock / 1000);
328 if (data->flags & MMC_DATA_WRITE)
330 * The limit is really 250 ms, but that is
331 * insufficient for some crappy cards.
338 * SDHC cards always use these fixed values.
340 if (timeout_us > limit_us || mmc_card_blockaddr(card)) {
341 data->timeout_ns = limit_us * 1000;
342 data->timeout_clks = 0;
346 * Some cards need very high timeouts if driven in SPI mode.
347 * The worst observed timeout was 900ms after writing a
348 * continuous stream of data until the internal logic
351 if (mmc_host_is_spi(card->host)) {
352 if (data->flags & MMC_DATA_WRITE) {
353 if (data->timeout_ns < 1000000000)
354 data->timeout_ns = 1000000000; /* 1s */
356 if (data->timeout_ns < 100000000)
357 data->timeout_ns = 100000000; /* 100ms */
361 EXPORT_SYMBOL(mmc_set_data_timeout);
364 * mmc_align_data_size - pads a transfer size to a more optimal value
365 * @card: the MMC card associated with the data transfer
366 * @sz: original transfer size
368 * Pads the original data size with a number of extra bytes in
369 * order to avoid controller bugs and/or performance hits
370 * (e.g. some controllers revert to PIO for certain sizes).
372 * Returns the improved size, which might be unmodified.
374 * Note that this function is only relevant when issuing a
375 * single scatter gather entry.
377 unsigned int mmc_align_data_size(struct mmc_card *card, unsigned int sz)
380 * FIXME: We don't have a system for the controller to tell
381 * the core about its problems yet, so for now we just 32-bit
384 sz = ((sz + 3) / 4) * 4;
388 EXPORT_SYMBOL(mmc_align_data_size);
391 * mmc_host_enable - enable a host.
392 * @host: mmc host to enable
394 * Hosts that support power saving can use the 'enable' and 'disable'
395 * methods to exit and enter power saving states. For more information
396 * see comments for struct mmc_host_ops.
398 int mmc_host_enable(struct mmc_host *host)
400 if (!(host->caps & MMC_CAP_DISABLE))
403 if (host->en_dis_recurs)
406 if (host->nesting_cnt++)
409 cancel_delayed_work_sync(&host->disable);
414 if (host->ops->enable) {
417 host->en_dis_recurs = 1;
418 err = host->ops->enable(host);
419 host->en_dis_recurs = 0;
422 pr_debug("%s: enable error %d\n",
423 mmc_hostname(host), err);
430 EXPORT_SYMBOL(mmc_host_enable);
432 static int mmc_host_do_disable(struct mmc_host *host, int lazy)
434 if (host->ops->disable) {
437 host->en_dis_recurs = 1;
438 err = host->ops->disable(host, lazy);
439 host->en_dis_recurs = 0;
442 pr_debug("%s: disable error %d\n",
443 mmc_hostname(host), err);
447 unsigned long delay = msecs_to_jiffies(err);
449 mmc_schedule_delayed_work(&host->disable, delay);
457 * mmc_host_disable - disable a host.
458 * @host: mmc host to disable
460 * Hosts that support power saving can use the 'enable' and 'disable'
461 * methods to exit and enter power saving states. For more information
462 * see comments for struct mmc_host_ops.
464 int mmc_host_disable(struct mmc_host *host)
468 if (!(host->caps & MMC_CAP_DISABLE))
471 if (host->en_dis_recurs)
474 if (--host->nesting_cnt)
480 err = mmc_host_do_disable(host, 0);
483 EXPORT_SYMBOL(mmc_host_disable);
486 * __mmc_claim_host - exclusively claim a host
487 * @host: mmc host to claim
488 * @abort: whether or not the operation should be aborted
490 * Claim a host for a set of operations. If @abort is non null and
491 * dereference a non-zero value then this will return prematurely with
492 * that non-zero value without acquiring the lock. Returns zero
493 * with the lock held otherwise.
495 int __mmc_claim_host(struct mmc_host *host, atomic_t *abort)
497 DECLARE_WAITQUEUE(wait, current);
503 add_wait_queue(&host->wq, &wait);
504 spin_lock_irqsave(&host->lock, flags);
506 set_current_state(TASK_UNINTERRUPTIBLE);
507 stop = abort ? atomic_read(abort) : 0;
508 if (stop || !host->claimed || host->claimer == current)
510 spin_unlock_irqrestore(&host->lock, flags);
512 spin_lock_irqsave(&host->lock, flags);
514 set_current_state(TASK_RUNNING);
517 host->claimer = current;
518 host->claim_cnt += 1;
521 spin_unlock_irqrestore(&host->lock, flags);
522 remove_wait_queue(&host->wq, &wait);
524 mmc_host_enable(host);
528 EXPORT_SYMBOL(__mmc_claim_host);
531 * mmc_try_claim_host - try exclusively to claim a host
532 * @host: mmc host to claim
534 * Returns %1 if the host is claimed, %0 otherwise.
536 int mmc_try_claim_host(struct mmc_host *host)
538 int claimed_host = 0;
541 spin_lock_irqsave(&host->lock, flags);
542 if (!host->claimed || host->claimer == current) {
544 host->claimer = current;
545 host->claim_cnt += 1;
548 spin_unlock_irqrestore(&host->lock, flags);
551 EXPORT_SYMBOL(mmc_try_claim_host);
553 static void mmc_do_release_host(struct mmc_host *host)
557 spin_lock_irqsave(&host->lock, flags);
558 if (--host->claim_cnt) {
559 /* Release for nested claim */
560 spin_unlock_irqrestore(&host->lock, flags);
563 host->claimer = NULL;
564 spin_unlock_irqrestore(&host->lock, flags);
569 void mmc_host_deeper_disable(struct work_struct *work)
571 struct mmc_host *host =
572 container_of(work, struct mmc_host, disable.work);
574 /* If the host is claimed then we do not want to disable it anymore */
575 if (!mmc_try_claim_host(host))
577 mmc_host_do_disable(host, 1);
578 mmc_do_release_host(host);
581 wake_unlock(&mmc_delayed_work_wake_lock);
585 * mmc_host_lazy_disable - lazily disable a host.
586 * @host: mmc host to disable
588 * Hosts that support power saving can use the 'enable' and 'disable'
589 * methods to exit and enter power saving states. For more information
590 * see comments for struct mmc_host_ops.
592 int mmc_host_lazy_disable(struct mmc_host *host)
594 if (!(host->caps & MMC_CAP_DISABLE))
597 if (host->en_dis_recurs)
600 if (--host->nesting_cnt)
606 if (host->disable_delay) {
607 mmc_schedule_delayed_work(&host->disable,
608 msecs_to_jiffies(host->disable_delay));
611 return mmc_host_do_disable(host, 1);
613 EXPORT_SYMBOL(mmc_host_lazy_disable);
616 * mmc_release_host - release a host
617 * @host: mmc host to release
619 * Release a MMC host, allowing others to claim the host
620 * for their operations.
622 void mmc_release_host(struct mmc_host *host)
624 WARN_ON(!host->claimed);
626 mmc_host_lazy_disable(host);
628 mmc_do_release_host(host);
631 EXPORT_SYMBOL(mmc_release_host);
634 * Internal function that does the actual ios call to the host driver,
635 * optionally printing some debug output.
637 static inline void mmc_set_ios(struct mmc_host *host)
639 struct mmc_ios *ios = &host->ios;
641 pr_debug("%s: clock %uHz busmode %u powermode %u cs %u Vdd %u "
642 "width %u timing %u\n",
643 mmc_hostname(host), ios->clock, ios->bus_mode,
644 ios->power_mode, ios->chip_select, ios->vdd,
645 ios->bus_width, ios->timing);
647 host->ops->set_ios(host, ios);
651 * Control chip select pin on a host.
653 void mmc_set_chip_select(struct mmc_host *host, int mode)
655 host->ios.chip_select = mode;
660 * Sets the host clock to the highest possible frequency that
663 void mmc_set_clock(struct mmc_host *host, unsigned int hz)
665 WARN_ON(hz < host->f_min);
667 if (hz > host->f_max)
670 host->ios.clock = hz;
675 * Change the bus mode (open drain/push-pull) of a host.
677 void mmc_set_bus_mode(struct mmc_host *host, unsigned int mode)
679 host->ios.bus_mode = mode;
684 * Change data bus width of a host.
686 void mmc_set_bus_width(struct mmc_host *host, unsigned int width)
688 host->ios.bus_width = width;
693 * mmc_vdd_to_ocrbitnum - Convert a voltage to the OCR bit number
695 * @low_bits: prefer low bits in boundary cases
697 * This function returns the OCR bit number according to the provided @vdd
698 * value. If conversion is not possible a negative errno value returned.
700 * Depending on the @low_bits flag the function prefers low or high OCR bits
701 * on boundary voltages. For example,
702 * with @low_bits = true, 3300 mV translates to ilog2(MMC_VDD_32_33);
703 * with @low_bits = false, 3300 mV translates to ilog2(MMC_VDD_33_34);
705 * Any value in the [1951:1999] range translates to the ilog2(MMC_VDD_20_21).
707 static int mmc_vdd_to_ocrbitnum(int vdd, bool low_bits)
709 const int max_bit = ilog2(MMC_VDD_35_36);
712 if (vdd < 1650 || vdd > 3600)
715 if (vdd >= 1650 && vdd <= 1950)
716 return ilog2(MMC_VDD_165_195);
721 /* Base 2000 mV, step 100 mV, bit's base 8. */
722 bit = (vdd - 2000) / 100 + 8;
729 * mmc_vddrange_to_ocrmask - Convert a voltage range to the OCR mask
730 * @vdd_min: minimum voltage value (mV)
731 * @vdd_max: maximum voltage value (mV)
733 * This function returns the OCR mask bits according to the provided @vdd_min
734 * and @vdd_max values. If conversion is not possible the function returns 0.
736 * Notes wrt boundary cases:
737 * This function sets the OCR bits for all boundary voltages, for example
738 * [3300:3400] range is translated to MMC_VDD_32_33 | MMC_VDD_33_34 |
739 * MMC_VDD_34_35 mask.
741 u32 mmc_vddrange_to_ocrmask(int vdd_min, int vdd_max)
745 if (vdd_max < vdd_min)
748 /* Prefer high bits for the boundary vdd_max values. */
749 vdd_max = mmc_vdd_to_ocrbitnum(vdd_max, false);
753 /* Prefer low bits for the boundary vdd_min values. */
754 vdd_min = mmc_vdd_to_ocrbitnum(vdd_min, true);
758 /* Fill the mask, from max bit to min bit. */
759 while (vdd_max >= vdd_min)
760 mask |= 1 << vdd_max--;
764 EXPORT_SYMBOL(mmc_vddrange_to_ocrmask);
766 #ifdef CONFIG_REGULATOR
769 * mmc_regulator_get_ocrmask - return mask of supported voltages
770 * @supply: regulator to use
772 * This returns either a negative errno, or a mask of voltages that
773 * can be provided to MMC/SD/SDIO devices using the specified voltage
774 * regulator. This would normally be called before registering the
777 int mmc_regulator_get_ocrmask(struct regulator *supply)
783 count = regulator_count_voltages(supply);
787 for (i = 0; i < count; i++) {
791 vdd_uV = regulator_list_voltage(supply, i);
795 vdd_mV = vdd_uV / 1000;
796 result |= mmc_vddrange_to_ocrmask(vdd_mV, vdd_mV);
801 EXPORT_SYMBOL(mmc_regulator_get_ocrmask);
804 * mmc_regulator_set_ocr - set regulator to match host->ios voltage
805 * @vdd_bit: zero for power off, else a bit number (host->ios.vdd)
806 * @supply: regulator to use
808 * Returns zero on success, else negative errno.
810 * MMC host drivers may use this to enable or disable a regulator using
811 * a particular supply voltage. This would normally be called from the
814 int mmc_regulator_set_ocr(struct regulator *supply, unsigned short vdd_bit)
820 enabled = regulator_is_enabled(supply);
828 /* REVISIT mmc_vddrange_to_ocrmask() may have set some
829 * bits this regulator doesn't quite support ... don't
830 * be too picky, most cards and regulators are OK with
831 * a 0.1V range goof (it's a small error percentage).
833 tmp = vdd_bit - ilog2(MMC_VDD_165_195);
835 min_uV = 1650 * 1000;
836 max_uV = 1950 * 1000;
838 min_uV = 1900 * 1000 + tmp * 100 * 1000;
839 max_uV = min_uV + 100 * 1000;
842 /* avoid needless changes to this voltage; the regulator
843 * might not allow this operation
845 voltage = regulator_get_voltage(supply);
848 else if (voltage < min_uV || voltage > max_uV)
849 result = regulator_set_voltage(supply, min_uV, max_uV);
853 if (result == 0 && !enabled)
854 result = regulator_enable(supply);
855 } else if (enabled) {
856 result = regulator_disable(supply);
861 EXPORT_SYMBOL(mmc_regulator_set_ocr);
866 * Mask off any voltages we don't support and select
869 u32 mmc_select_voltage(struct mmc_host *host, u32 ocr)
873 ocr &= host->ocr_avail;
884 pr_warning("%s: host doesn't support card's voltages\n",
893 * Select timing parameters for host.
895 void mmc_set_timing(struct mmc_host *host, unsigned int timing)
897 host->ios.timing = timing;
902 * Apply power to the MMC stack. This is a two-stage process.
903 * First, we enable power to the card without the clock running.
904 * We then wait a bit for the power to stabilise. Finally,
905 * enable the bus drivers and clock to the card.
907 * We must _NOT_ enable the clock prior to power stablising.
909 * If a host does all the power sequencing itself, ignore the
910 * initial MMC_POWER_UP stage.
912 static void mmc_power_up(struct mmc_host *host)
916 /* If ocr is set, we use it */
918 bit = ffs(host->ocr) - 1;
920 bit = fls(host->ocr_avail) - 1;
923 if (mmc_host_is_spi(host)) {
924 host->ios.chip_select = MMC_CS_HIGH;
925 host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
927 host->ios.chip_select = MMC_CS_DONTCARE;
928 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
930 host->ios.power_mode = MMC_POWER_UP;
931 host->ios.bus_width = MMC_BUS_WIDTH_1;
932 host->ios.timing = MMC_TIMING_LEGACY;
936 * This delay should be sufficient to allow the power supply
937 * to reach the minimum voltage.
941 host->ios.clock = host->f_min;
943 host->ios.power_mode = MMC_POWER_ON;
947 * This delay must be at least 74 clock sizes, or 1 ms, or the
948 * time required to reach a stable voltage.
953 static void mmc_power_off(struct mmc_host *host)
957 if (!mmc_host_is_spi(host)) {
958 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
959 host->ios.chip_select = MMC_CS_DONTCARE;
961 host->ios.power_mode = MMC_POWER_OFF;
962 host->ios.bus_width = MMC_BUS_WIDTH_1;
963 host->ios.timing = MMC_TIMING_LEGACY;
968 * Cleanup when the last reference to the bus operator is dropped.
970 static void __mmc_release_bus(struct mmc_host *host)
973 BUG_ON(host->bus_refs);
974 BUG_ON(!host->bus_dead);
976 host->bus_ops = NULL;
980 * Increase reference count of bus operator
982 static inline void mmc_bus_get(struct mmc_host *host)
986 spin_lock_irqsave(&host->lock, flags);
988 spin_unlock_irqrestore(&host->lock, flags);
992 * Decrease reference count of bus operator and free it if
993 * it is the last reference.
995 static inline void mmc_bus_put(struct mmc_host *host)
999 spin_lock_irqsave(&host->lock, flags);
1001 if ((host->bus_refs == 0) && host->bus_ops)
1002 __mmc_release_bus(host);
1003 spin_unlock_irqrestore(&host->lock, flags);
1006 int mmc_resume_bus(struct mmc_host *host)
1008 if (!mmc_bus_needs_resume(host))
1011 printk("%s: Starting deferred resume\n", mmc_hostname(host));
1012 host->bus_resume_flags &= ~MMC_BUSRESUME_NEEDS_RESUME;
1014 if (host->bus_ops && !host->bus_dead) {
1016 BUG_ON(!host->bus_ops->resume);
1017 host->bus_ops->resume(host);
1020 if (host->bus_ops->detect && !host->bus_dead)
1021 host->bus_ops->detect(host);
1024 printk("%s: Deferred resume completed\n", mmc_hostname(host));
1028 EXPORT_SYMBOL(mmc_resume_bus);
1031 * Assign a mmc bus handler to a host. Only one bus handler may control a
1032 * host at any given time.
1034 void mmc_attach_bus(struct mmc_host *host, const struct mmc_bus_ops *ops)
1036 unsigned long flags;
1041 WARN_ON(!host->claimed);
1043 spin_lock_irqsave(&host->lock, flags);
1045 BUG_ON(host->bus_ops);
1046 BUG_ON(host->bus_refs);
1048 host->bus_ops = ops;
1052 spin_unlock_irqrestore(&host->lock, flags);
1056 * Remove the current bus handler from a host. Assumes that there are
1057 * no interesting cards left, so the bus is powered down.
1059 void mmc_detach_bus(struct mmc_host *host)
1061 unsigned long flags;
1065 WARN_ON(!host->claimed);
1066 WARN_ON(!host->bus_ops);
1068 spin_lock_irqsave(&host->lock, flags);
1072 spin_unlock_irqrestore(&host->lock, flags);
1074 mmc_power_off(host);
1080 * mmc_detect_change - process change of state on a MMC socket
1081 * @host: host which changed state.
1082 * @delay: optional delay to wait before detection (jiffies)
1084 * MMC drivers should call this when they detect a card has been
1085 * inserted or removed. The MMC layer will confirm that any
1086 * present card is still functional, and initialize any newly
1089 void mmc_detect_change(struct mmc_host *host, unsigned long delay)
1091 #ifdef CONFIG_MMC_DEBUG
1092 unsigned long flags;
1093 spin_lock_irqsave(&host->lock, flags);
1094 WARN_ON(host->removed);
1095 spin_unlock_irqrestore(&host->lock, flags);
1098 mmc_schedule_delayed_work(&host->detect, delay);
1101 EXPORT_SYMBOL(mmc_detect_change);
1103 #if defined(CONFIG_SDMMC_RK29) && !defined(CONFIG_SDMMC_RK29_OLD)
1104 void mmc_rescan(struct work_struct *work)
1106 struct mmc_host *host =
1107 container_of(work, struct mmc_host, detect.work);
1110 int extend_wakelock = 0;
1115 /* if there is a card registered, check whether it is still present */
1116 if ((host->bus_ops != NULL) && host->bus_ops->detect && !host->bus_dead)
1117 host->bus_ops->detect(host);
1119 /* If the card was removed the bus will be marked
1120 * as dead - extend the wakelock so userspace
1123 extend_wakelock = 1;
1130 /* if there still is a card present, stop here */
1131 if (host->bus_ops != NULL) {
1136 /* detect a newly inserted card */
1139 * Only we can add a new handler, so it's safe to
1140 * release the lock here.
1143 printk("\n%s...%d.. ===== mmc_rescan Begin....======xbw[%s]=====\n",__FILE__, __LINE__, mmc_hostname(host));
1145 if (host->ops->get_cd && host->ops->get_cd(host) == 0)
1147 printk("\n=================\n%s..%d.. ====find no SDMMC host.====xbw[%s]=====\n", \
1148 __FUNCTION__, __LINE__, mmc_hostname(host));
1153 mmc_claim_host(host);
1160 In oder to improve the initialization process in rockchip IC, I modify the following code about the the initialization process of SDIO-SD-MMC.
1161 So I deleted the CMD8 and add a conditional to distinguish between the two card type,i.e.SDMMC process and SDIO process.
1162 For detail,please refer to "RK29XX Technical Reference Manual" and "SD-MMC-SDIO Specifications".
1163 Noted by xbw@2011-04-09
1166 //mmc_send_if_cond(host, host->ocr_avail); //deleted by xbw@2011-04-09
1168 if( strncmp( mmc_hostname(host) ,"mmc0" , strlen("mmc0")) ){
1170 * First we search for SDIO...
1172 err = mmc_send_io_op_cond(host, 0, &ocr);
1174 printk("\n%s..%d.. ===== Begin to identify card as SDIO-card===xbw[%s]===\n",__FUNCTION__, __LINE__, mmc_hostname(host));
1176 if (mmc_attach_sdio(host, ocr))
1178 printk("\n=====\n %s..%d.. ===== Initialize SDIO-card unsuccessfully!!! ===xbw[%s]===\n=====\n",\
1179 __FUNCTION__, __LINE__, mmc_hostname(host));
1181 mmc_power_off(host);
1185 printk("%s..%d.. ===== Initialize SDIO successfully. ===xbw[%s]===\n",__FUNCTION__, __LINE__, mmc_hostname(host));
1187 extend_wakelock = 1;
1194 * ...then normal SD...
1196 err = mmc_send_app_op_cond(host, 0, &ocr);
1198 printk("\n%s..%d.. ===== Begin to identify card as SD-card ===xbw[%s]===\n",__FUNCTION__, __LINE__, mmc_hostname(host));
1200 if (mmc_attach_sd(host, ocr))
1202 printk("\n=====\n%s..%d.. ===== Initialize SD-card unsuccessfully!!! ===xbw[%s]===\n====\n",\
1203 __FUNCTION__, __LINE__, mmc_hostname(host));
1205 mmc_power_off(host);
1209 printk("%s..%d.. ===== Initialize SD-card successfully. ===xbw[%s]===\n",__FUNCTION__, __LINE__, mmc_hostname(host));
1211 extend_wakelock = 1;
1216 * ...and finally MMC.
1218 err = mmc_send_op_cond(host, 0, &ocr);
1220 printk("\n%s..%d.. ===== Begin to identify card as MMC-card ===xbw[%s]===\n", __FUNCTION__, __LINE__, mmc_hostname(host));
1222 if (mmc_attach_mmc(host, ocr))
1224 printk("\n =====\n%s..%d.. ===== Initialize MMC-card unsuccessfully!!! ===xbw[%s]===\n======\n",\
1225 __FUNCTION__, __LINE__, mmc_hostname(host));
1227 mmc_power_off(host);
1231 printk("%s...%d.. ===== Initialize MMC-card successfully. ===xbw[%s]===\n",__FUNCTION__, __LINE__, mmc_hostname(host));
1233 extend_wakelock = 1;
1237 mmc_release_host(host);
1238 mmc_power_off(host);
1242 if (extend_wakelock)
1243 wake_lock_timeout(&mmc_delayed_work_wake_lock, HZ / 2);
1245 wake_unlock(&mmc_delayed_work_wake_lock);
1247 if (host->caps & MMC_CAP_NEEDS_POLL)
1248 mmc_schedule_delayed_work(&host->detect, HZ);
1252 void mmc_rescan(struct work_struct *work)
1254 struct mmc_host *host =
1255 container_of(work, struct mmc_host, detect.work);
1258 int extend_wakelock = 0;
1260 #if defined(CONFIG_SDMMC_RK29) && defined(CONFIG_SDMMC_RK29_OLD)
1261 unsigned long flags;
1263 spin_lock_irqsave(&host->lock, flags);
1265 if (host->rescan_disable) {
1266 spin_unlock_irqrestore(&host->lock, flags);
1270 spin_unlock_irqrestore(&host->lock, flags);
1276 /* if there is a card registered, check whether it is still present */
1277 if ((host->bus_ops != NULL) && host->bus_ops->detect && !host->bus_dead)
1278 host->bus_ops->detect(host);
1280 /* If the card was removed the bus will be marked
1281 * as dead - extend the wakelock so userspace
1284 extend_wakelock = 1;
1291 /* if there still is a card present, stop here */
1292 if (host->bus_ops != NULL) {
1297 /* detect a newly inserted card */
1300 * Only we can add a new handler, so it's safe to
1301 * release the lock here.
1305 if (host->ops->get_cd && host->ops->get_cd(host) == 0)
1308 mmc_claim_host(host);
1313 mmc_send_if_cond(host, host->ocr_avail);
1316 * First we search for SDIO...
1318 err = mmc_send_io_op_cond(host, 0, &ocr);
1320 if (mmc_attach_sdio(host, ocr))
1321 mmc_power_off(host);
1322 extend_wakelock = 1;
1327 * ...then normal SD...
1329 err = mmc_send_app_op_cond(host, 0, &ocr);
1331 if (mmc_attach_sd(host, ocr))
1332 mmc_power_off(host);
1333 extend_wakelock = 1;
1338 * ...and finally MMC.
1340 err = mmc_send_op_cond(host, 0, &ocr);
1342 if (mmc_attach_mmc(host, ocr))
1343 mmc_power_off(host);
1344 extend_wakelock = 1;
1348 mmc_release_host(host);
1349 mmc_power_off(host);
1352 if (extend_wakelock)
1353 wake_lock_timeout(&mmc_delayed_work_wake_lock, HZ / 2);
1355 wake_unlock(&mmc_delayed_work_wake_lock);
1357 if (host->caps & MMC_CAP_NEEDS_POLL)
1358 mmc_schedule_delayed_work(&host->detect, HZ);
1362 void mmc_start_host(struct mmc_host *host)
1364 mmc_power_off(host);
1365 mmc_detect_change(host, 0);
1368 void mmc_stop_host(struct mmc_host *host)
1370 #ifdef CONFIG_MMC_DEBUG
1371 unsigned long flags;
1372 spin_lock_irqsave(&host->lock, flags);
1374 spin_unlock_irqrestore(&host->lock, flags);
1377 if (host->caps & MMC_CAP_DISABLE)
1378 cancel_delayed_work(&host->disable);
1379 cancel_delayed_work(&host->detect);
1380 mmc_flush_scheduled_work();
1383 if (host->bus_ops && !host->bus_dead) {
1384 if (host->bus_ops->remove)
1385 host->bus_ops->remove(host);
1387 mmc_claim_host(host);
1388 mmc_detach_bus(host);
1389 mmc_release_host(host);
1397 mmc_power_off(host);
1400 void mmc_power_save_host(struct mmc_host *host)
1404 if (!host->bus_ops || host->bus_dead || !host->bus_ops->power_restore) {
1409 if (host->bus_ops->power_save)
1410 host->bus_ops->power_save(host);
1414 mmc_power_off(host);
1416 EXPORT_SYMBOL(mmc_power_save_host);
1418 void mmc_power_restore_host(struct mmc_host *host)
1422 if (!host->bus_ops || host->bus_dead || !host->bus_ops->power_restore) {
1428 host->bus_ops->power_restore(host);
1432 EXPORT_SYMBOL(mmc_power_restore_host);
1434 int mmc_card_awake(struct mmc_host *host)
1440 if (host->bus_ops && !host->bus_dead && host->bus_ops->awake)
1441 err = host->bus_ops->awake(host);
1447 EXPORT_SYMBOL(mmc_card_awake);
1449 int mmc_card_sleep(struct mmc_host *host)
1455 if (host->bus_ops && !host->bus_dead && host->bus_ops->awake)
1456 err = host->bus_ops->sleep(host);
1462 EXPORT_SYMBOL(mmc_card_sleep);
1464 int mmc_card_can_sleep(struct mmc_host *host)
1466 struct mmc_card *card = host->card;
1468 if (card && mmc_card_mmc(card) && card->ext_csd.rev >= 3)
1472 EXPORT_SYMBOL(mmc_card_can_sleep);
1477 * mmc_suspend_host - suspend a host
1479 * @state: suspend mode (PM_SUSPEND_xxx)
1481 int mmc_suspend_host(struct mmc_host *host, pm_message_t state)
1485 if (mmc_bus_needs_resume(host))
1488 if (host->caps & MMC_CAP_DISABLE)
1489 cancel_delayed_work(&host->disable);
1490 cancel_delayed_work(&host->detect);
1491 mmc_flush_scheduled_work();
1494 if (host->bus_ops && !host->bus_dead) {
1495 if (host->bus_ops->suspend)
1496 err = host->bus_ops->suspend(host);
1498 #if defined(CONFIG_SDMMC_RK29) && defined(CONFIG_SDMMC_RK29_OLD)
1499 //deleted all detail code.
1501 if (err == -ENOSYS || !host->bus_ops->resume) {
1503 * We simply "remove" the card in this case.
1504 * It will be redetected on resume.
1506 if (host->bus_ops->remove)
1507 host->bus_ops->remove(host);
1508 mmc_claim_host(host);
1509 mmc_detach_bus(host);
1510 mmc_release_host(host);
1518 mmc_power_off(host);
1523 EXPORT_SYMBOL(mmc_suspend_host);
1526 * mmc_resume_host - resume a previously suspended host
1529 int mmc_resume_host(struct mmc_host *host)
1534 if (host->bus_resume_flags & MMC_BUSRESUME_MANUAL_RESUME) {
1535 host->bus_resume_flags |= MMC_BUSRESUME_NEEDS_RESUME;
1540 if (host->bus_ops && !host->bus_dead) {
1542 mmc_select_voltage(host, host->ocr);
1543 BUG_ON(!host->bus_ops->resume);
1545 #if defined(CONFIG_SDMMC_RK29) && defined(CONFIG_SDMMC_RK29_OLD)
1546 err = host->bus_ops->resume(host);
1548 printk(KERN_WARNING "%s: error %d during resume "
1549 "(card was removed?)\n",
1550 mmc_hostname(host), err);
1554 #elif defined(CONFIG_SDMMC_RK29) && !defined(CONFIG_SDMMC_RK29_OLD)
1555 //panic if the card is being removed during the resume, deleted by xbw at 2011-06-20
1556 host->bus_ops->resume(host);
1559 err = host->bus_ops->resume(host);
1561 printk(KERN_WARNING "%s: error %d during resume "
1562 "(card was removed?)\n",
1563 mmc_hostname(host), err);
1564 if (host->bus_ops->remove)
1565 host->bus_ops->remove(host);
1566 mmc_claim_host(host);
1567 mmc_detach_bus(host);
1568 mmc_release_host(host);
1569 /* no need to bother upper layers */
1576 #if defined(CONFIG_SDMMC_RK29) && defined(CONFIG_SDMMC_RK29_OLD)
1577 //delete the detect_change.
1580 * We add a slight delay here so that resume can progress
1583 mmc_detect_change(host, 1);
1587 EXPORT_SYMBOL(mmc_resume_host);
1589 ///old driver add the function reforming to kernel2.6.38
1590 #if defined(CONFIG_SDMMC_RK29) && defined(CONFIG_SDMMC_RK29_OLD)
1591 /* Do the card removal on suspend if card is assumed removeable
1592 * Do that in pm notifier while userspace isn't yet frozen, so we will be able
1595 int mmc_pm_notify(struct notifier_block *notify_block,
1596 unsigned long mode, void *unused)
1598 struct mmc_host *host = container_of(
1599 notify_block, struct mmc_host, pm_notify);
1600 unsigned long flags;
1604 case PM_HIBERNATION_PREPARE:
1605 case PM_SUSPEND_PREPARE:
1607 spin_lock_irqsave(&host->lock, flags);
1608 host->rescan_disable = 1;
1609 spin_unlock_irqrestore(&host->lock, flags);
1610 cancel_delayed_work_sync(&host->detect);
1612 if (!host->bus_ops || host->bus_ops->suspend)
1615 mmc_claim_host(host);
1617 if (host->bus_ops->remove)
1618 host->bus_ops->remove(host);
1620 mmc_detach_bus(host);
1621 mmc_release_host(host);
1624 case PM_POST_SUSPEND:
1625 case PM_POST_HIBERNATION:
1627 spin_lock_irqsave(&host->lock, flags);
1628 host->rescan_disable = 0;
1629 spin_unlock_irqrestore(&host->lock, flags);
1630 mmc_detect_change(host, 0);
1636 #endif// endif---defined(CONFIG_SDMMC_RK29) && defined(CONFIG_SDMMC_RK29_OLD)
1640 #ifdef CONFIG_MMC_EMBEDDED_SDIO
1641 void mmc_set_embedded_sdio_data(struct mmc_host *host,
1642 struct sdio_cis *cis,
1643 struct sdio_cccr *cccr,
1644 struct sdio_embedded_func *funcs,
1647 host->embedded_sdio_data.cis = cis;
1648 host->embedded_sdio_data.cccr = cccr;
1649 host->embedded_sdio_data.funcs = funcs;
1650 host->embedded_sdio_data.num_funcs = num_funcs;
1653 EXPORT_SYMBOL(mmc_set_embedded_sdio_data);
1656 static int __init mmc_init(void)
1660 wake_lock_init(&mmc_delayed_work_wake_lock, WAKE_LOCK_SUSPEND, "mmc_delayed_work");
1662 workqueue = create_singlethread_workqueue("kmmcd");
1666 ret = mmc_register_bus();
1668 goto destroy_workqueue;
1670 ret = mmc_register_host_class();
1672 goto unregister_bus;
1674 ret = sdio_register_bus();
1676 goto unregister_host_class;
1680 unregister_host_class:
1681 mmc_unregister_host_class();
1683 mmc_unregister_bus();
1685 destroy_workqueue(workqueue);
1690 static void __exit mmc_exit(void)
1692 sdio_unregister_bus();
1693 mmc_unregister_host_class();
1694 mmc_unregister_bus();
1695 destroy_workqueue(workqueue);
1696 wake_lock_destroy(&mmc_delayed_work_wake_lock);
1699 subsys_initcall(mmc_init);
1700 module_exit(mmc_exit);
1702 MODULE_LICENSE("GPL");