2 * linux/drivers/mmc/sdio.c
4 * Copyright 2006-2007 Pierre Ossman
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or (at
9 * your option) any later version.
12 #include <linux/err.h>
13 #include <linux/pm_runtime.h>
15 #include <linux/mmc/host.h>
16 #include <linux/mmc/card.h>
17 #include <linux/mmc/sdio.h>
18 #include <linux/mmc/sdio_func.h>
19 #include <linux/mmc/sdio_ids.h>
20 #include <linux/mmc/mmc.h>
32 #ifdef CONFIG_MMC_EMBEDDED_SDIO
33 #include <linux/mmc/sdio_ids.h>
36 static int sdio_read_fbr(struct sdio_func *func)
41 if (mmc_card_nonstd_func_interface(func->card)) {
42 func->class = SDIO_CLASS_NONE;
46 ret = mmc_io_rw_direct(func->card, 0, 0,
47 SDIO_FBR_BASE(func->num) + SDIO_FBR_STD_IF, 0, &data);
54 ret = mmc_io_rw_direct(func->card, 0, 0,
55 SDIO_FBR_BASE(func->num) + SDIO_FBR_STD_IF_EXT, 0, &data);
66 static int sdio_init_func(struct mmc_card *card, unsigned int fn)
69 struct sdio_func *func;
71 BUG_ON(fn > SDIO_MAX_FUNCS);
73 func = sdio_alloc_func(card);
79 if (!(card->quirks & MMC_QUIRK_NONSTD_SDIO)) {
80 ret = sdio_read_fbr(func);
84 ret = sdio_read_func_cis(func);
88 func->vendor = func->card->cis.vendor;
89 func->device = func->card->cis.device;
90 func->max_blksize = func->card->cis.blksize;
93 card->sdio_func[fn - 1] = func;
99 * It is okay to remove the function here even though we hold
100 * the host lock as we haven't registered the device yet.
102 sdio_remove_func(func);
106 static int sdio_read_cccr(struct mmc_card *card,u32 ocr)
110 int uhs = ocr & R4_18V_PRESENT;
114 memset(&card->cccr, 0, sizeof(struct sdio_cccr));
116 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_CCCR, 0, &data);
120 cccr_vsn = data & 0x0f;
122 if (cccr_vsn > SDIO_CCCR_REV_3_00) {
123 printk(KERN_ERR "%s: unrecognised CCCR structure version %d\n",
124 mmc_hostname(card->host), cccr_vsn);
128 card->cccr.sdio_vsn = (data & 0xf0) >> 4;
130 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_CAPS, 0, &data);
134 if (data & SDIO_CCCR_CAP_SMB)
135 card->cccr.multi_block = 1;
136 if (data & SDIO_CCCR_CAP_LSC)
137 card->cccr.low_speed = 1;
138 if (data & SDIO_CCCR_CAP_4BLS)
139 card->cccr.wide_bus = 1;
141 if (cccr_vsn >= SDIO_CCCR_REV_1_10) {
142 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_POWER, 0, &data);
146 if (data & SDIO_POWER_SMPC)
147 card->cccr.high_power = 1;
150 if (cccr_vsn >= SDIO_CCCR_REV_1_20) {
151 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_SPEED, 0, &data);
155 card->scr.sda_spec3 = 0;
156 card->sw_caps.sd3_bus_mode = 0;
157 card->sw_caps.sd3_drv_type = 0;
159 if (cccr_vsn >= SDIO_CCCR_REV_3_00 && uhs) {
160 card->scr.sda_spec3 = 1;
161 ret = mmc_io_rw_direct(card, 0, 0,
162 SDIO_CCCR_UHS, 0, &data);
166 if (mmc_host_uhs(card->host)) {
167 if (data & SDIO_UHS_DDR50)
168 card->sw_caps.sd3_bus_mode
169 |= SD_MODE_UHS_DDR50;
171 if (data & SDIO_UHS_SDR50)
172 card->sw_caps.sd3_bus_mode
173 |= SD_MODE_UHS_SDR50;
175 if (data & SDIO_UHS_SDR104)
176 card->sw_caps.sd3_bus_mode
177 |= SD_MODE_UHS_SDR104;
180 ret = mmc_io_rw_direct(card, 0, 0,
181 SDIO_CCCR_DRIVE_STRENGTH, 0, &data);
185 if (data & SDIO_DRIVE_SDTA)
186 card->sw_caps.sd3_drv_type |= SD_DRIVER_TYPE_A;
187 if (data & SDIO_DRIVE_SDTC)
188 card->sw_caps.sd3_drv_type |= SD_DRIVER_TYPE_C;
189 if (data & SDIO_DRIVE_SDTD)
190 card->sw_caps.sd3_drv_type |= SD_DRIVER_TYPE_D;
193 if (!card->sw_caps.sd3_bus_mode) {
194 if (data & SDIO_SPEED_SHS)
195 card->cccr.high_speed = 1;
196 card->sw_caps.hs_max_dtr = 50000000;
198 card->cccr.high_speed = 0;
199 card->sw_caps.hs_max_dtr = 25000000;
208 static int sdio_enable_wide(struct mmc_card *card)
213 if (!(card->host->caps & MMC_CAP_4_BIT_DATA))
216 if (card->cccr.low_speed && !card->cccr.wide_bus)
219 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl);
223 if ((ctrl & SDIO_BUS_WIDTH_MASK) == SDIO_BUS_WIDTH_RESERVED)
224 printk("%s: SDIO_CCCR_IF is invalid: 0x%02x\n",
225 mmc_hostname(card->host), ctrl);
227 /* set as 4-bit bus width */
228 ctrl &= ~SDIO_BUS_WIDTH_MASK;
229 ctrl |= SDIO_BUS_WIDTH_4BIT;
231 ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL);
239 * If desired, disconnect the pull-up resistor on CD/DAT[3] (pin 1)
240 * of the card. This may be required on certain setups of boards,
241 * controllers and embedded sdio device which do not need the card's
242 * pull-up. As a result, card detection is disabled and power is saved.
244 static int sdio_disable_cd(struct mmc_card *card)
249 if (!mmc_card_disable_cd(card))
252 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl);
256 ctrl |= SDIO_BUS_CD_DISABLE;
258 return mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL);
262 * Devices that remain active during a system suspend are
263 * put back into 1-bit mode.
265 static int sdio_disable_wide(struct mmc_card *card)
270 if (!(card->host->caps & MMC_CAP_4_BIT_DATA))
273 if (card->cccr.low_speed && !card->cccr.wide_bus)
276 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl);
280 if (!(ctrl & SDIO_BUS_WIDTH_4BIT))
283 ctrl &= ~SDIO_BUS_WIDTH_4BIT;
284 ctrl |= SDIO_BUS_ASYNC_INT;
286 ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL);
290 mmc_set_bus_width(card->host, MMC_BUS_WIDTH_1);
296 static int sdio_enable_4bit_bus(struct mmc_card *card)
300 if (card->type == MMC_TYPE_SDIO)
301 return sdio_enable_wide(card);
303 if ((card->host->caps & MMC_CAP_4_BIT_DATA) &&
304 (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) {
305 err = mmc_app_set_bus_width(card, MMC_BUS_WIDTH_4);
311 err = sdio_enable_wide(card);
313 mmc_app_set_bus_width(card, MMC_BUS_WIDTH_1);
320 * Test if the card supports high-speed mode and, if so, switch to it.
322 static int mmc_sdio_switch_hs(struct mmc_card *card, int enable)
327 if (!(card->host->caps & MMC_CAP_SD_HIGHSPEED))
330 if (!card->cccr.high_speed)
333 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_SPEED, 0, &speed);
338 speed |= SDIO_SPEED_EHS;
340 speed &= ~SDIO_SPEED_EHS;
342 ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_SPEED, speed, NULL);
350 * Enable SDIO/combo card's high-speed mode. Return 0/1 if [not]supported.
352 static int sdio_enable_hs(struct mmc_card *card)
356 ret = mmc_sdio_switch_hs(card, true);
357 if (ret <= 0 || card->type == MMC_TYPE_SDIO)
360 ret = mmc_sd_switch_hs(card);
362 mmc_sdio_switch_hs(card, false);
367 static unsigned mmc_sdio_get_max_clock(struct mmc_card *card)
371 if (mmc_card_highspeed(card)) {
373 * The SDIO specification doesn't mention how
374 * the CIS transfer speed register relates to
375 * high-speed, but it seems that 50 MHz is
380 max_dtr = card->cis.max_dtr;
383 if (card->type == MMC_TYPE_SD_COMBO)
384 max_dtr = min(max_dtr, mmc_sd_get_max_clock(card));
389 static unsigned char host_drive_to_sdio_drive(int host_strength)
391 switch (host_strength) {
392 case MMC_SET_DRIVER_TYPE_A:
393 return SDIO_DTSx_SET_TYPE_A;
394 case MMC_SET_DRIVER_TYPE_B:
395 return SDIO_DTSx_SET_TYPE_B;
396 case MMC_SET_DRIVER_TYPE_C:
397 return SDIO_DTSx_SET_TYPE_C;
398 case MMC_SET_DRIVER_TYPE_D:
399 return SDIO_DTSx_SET_TYPE_D;
401 return SDIO_DTSx_SET_TYPE_B;
406 static void sdio_select_driver_type(struct mmc_card *card)
408 int host_drv_type = SD_DRIVER_TYPE_B;
409 int card_drv_type = SD_DRIVER_TYPE_B;
411 unsigned char card_strength;
415 * If the host doesn't support any of the Driver Types A,C or D,
416 * or there is no board specific handler then default Driver
419 if (!(card->host->caps &
420 (MMC_CAP_DRIVER_TYPE_A |
421 MMC_CAP_DRIVER_TYPE_C |
422 MMC_CAP_DRIVER_TYPE_D)))
425 if (!card->host->ops->select_drive_strength)
428 if (card->host->caps & MMC_CAP_DRIVER_TYPE_A)
429 host_drv_type |= SD_DRIVER_TYPE_A;
431 if (card->host->caps & MMC_CAP_DRIVER_TYPE_C)
432 host_drv_type |= SD_DRIVER_TYPE_C;
434 if (card->host->caps & MMC_CAP_DRIVER_TYPE_D)
435 host_drv_type |= SD_DRIVER_TYPE_D;
437 if (card->sw_caps.sd3_drv_type & SD_DRIVER_TYPE_A)
438 card_drv_type |= SD_DRIVER_TYPE_A;
440 if (card->sw_caps.sd3_drv_type & SD_DRIVER_TYPE_C)
441 card_drv_type |= SD_DRIVER_TYPE_C;
443 if (card->sw_caps.sd3_drv_type & SD_DRIVER_TYPE_D)
444 card_drv_type |= SD_DRIVER_TYPE_D;
447 * The drive strength that the hardware can support
448 * depends on the board design. Pass the appropriate
449 * information and let the hardware specific code
450 * return what is possible given the options
452 drive_strength = card->host->ops->select_drive_strength(
453 card->sw_caps.uhs_max_dtr,
454 host_drv_type, card_drv_type);
456 /* if error just use default for drive strength B */
457 err = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_DRIVE_STRENGTH, 0,
462 card_strength &= ~(SDIO_DRIVE_DTSx_MASK<<SDIO_DRIVE_DTSx_SHIFT);
463 card_strength |= host_drive_to_sdio_drive(drive_strength);
465 err = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_DRIVE_STRENGTH,
466 card_strength, NULL);
468 /* if error default to drive strength B */
470 mmc_set_driver_type(card->host, drive_strength);
474 static int sdio_set_bus_speed_mode(struct mmc_card *card)
476 unsigned int bus_speed, timing;
481 * If the host doesn't support any of the UHS-I modes, fallback on
484 if (!mmc_host_uhs(card->host))
487 bus_speed = SDIO_SPEED_SDR12;
488 timing = MMC_TIMING_UHS_SDR12;
489 if ((card->host->caps & MMC_CAP_UHS_SDR104) &&
490 (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_SDR104)) {
491 bus_speed = SDIO_SPEED_SDR104;
492 timing = MMC_TIMING_UHS_SDR104;
493 card->sw_caps.uhs_max_dtr = UHS_SDR104_MAX_DTR;
494 card->sd_bus_speed = UHS_SDR104_BUS_SPEED;
495 } else if ((card->host->caps & MMC_CAP_UHS_DDR50) &&
496 (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_DDR50)) {
497 bus_speed = SDIO_SPEED_DDR50;
498 timing = MMC_TIMING_UHS_DDR50;
499 card->sw_caps.uhs_max_dtr = UHS_DDR50_MAX_DTR;
500 card->sd_bus_speed = UHS_DDR50_BUS_SPEED;
501 } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
502 MMC_CAP_UHS_SDR50)) && (card->sw_caps.sd3_bus_mode &
503 SD_MODE_UHS_SDR50)) {
504 bus_speed = SDIO_SPEED_SDR50;
505 timing = MMC_TIMING_UHS_SDR50;
506 card->sw_caps.uhs_max_dtr = UHS_SDR50_MAX_DTR;
507 card->sd_bus_speed = UHS_SDR50_BUS_SPEED;
508 } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
509 MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR25)) &&
510 (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_SDR25)) {
511 bus_speed = SDIO_SPEED_SDR25;
512 timing = MMC_TIMING_UHS_SDR25;
513 card->sw_caps.uhs_max_dtr = UHS_SDR25_MAX_DTR;
514 card->sd_bus_speed = UHS_SDR25_BUS_SPEED;
515 } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
516 MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR25 |
517 MMC_CAP_UHS_SDR12)) && (card->sw_caps.sd3_bus_mode &
518 SD_MODE_UHS_SDR12)) {
519 bus_speed = SDIO_SPEED_SDR12;
520 timing = MMC_TIMING_UHS_SDR12;
521 card->sw_caps.uhs_max_dtr = UHS_SDR12_MAX_DTR;
522 card->sd_bus_speed = UHS_SDR12_BUS_SPEED;
525 err = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_SPEED, 0, &speed);
529 speed &= ~SDIO_SPEED_BSS_MASK;
531 err = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_SPEED, speed, NULL);
536 mmc_set_timing(card->host, timing);
537 mmc_set_clock(card->host, card->sw_caps.uhs_max_dtr);
545 * UHS-I specific initialization procedure
547 static int mmc_sdio_init_uhs_card(struct mmc_card *card)
551 if (!card->scr.sda_spec3)
555 * Switch to wider bus (if supported).
557 if (card->host->caps & MMC_CAP_4_BIT_DATA) {
558 err = sdio_enable_4bit_bus(card);
560 mmc_set_bus_width(card->host, MMC_BUS_WIDTH_4);
565 /* Set the driver strength for the card */
566 sdio_select_driver_type(card);
568 /* Set bus speed mode of the card */
569 err = sdio_set_bus_speed_mode(card);
575 * SPI mode doesn't define CMD19 and tuning is only valid for SDR50 and
576 * SDR104 mode SD-cards. Note that tuning is mandatory for SDR104.
578 if (!mmc_host_is_spi(card->host) && card->host->ops->execute_tuning &&
579 ((card->sw_caps.sd3_bus_mode == SD_MODE_UHS_SDR50) ||
580 (card->sw_caps.sd3_bus_mode == SD_MODE_UHS_SDR104))) {
581 mmc_host_clk_hold(card->host);
582 err = card->host->ops->execute_tuning(card->host,MMC_SEND_TUNING_BLOCK);
583 mmc_host_clk_release(card->host);
593 * Handle the detection and initialisation of a card.
595 * In the case of a resume, "oldcard" will contain the card
596 * we're trying to reinitialise.
598 static int mmc_sdio_init_card(struct mmc_host *host, u32 ocr,
599 struct mmc_card *oldcard, int powered_resume)
601 struct mmc_card *card;
606 WARN_ON(!host->claimed);
611 pr_warning("%s: Skipping voltage switch\n",
613 ocr &= ~R4_18V_PRESENT;
614 host->ocr &= ~R4_18V_PRESENT;
618 * Inform the card of the voltage
620 if (!powered_resume) {
621 err = mmc_send_io_op_cond(host, host->ocr, &ocr);
623 #if defined(CONFIG_SDMMC_RK29) && !defined(CONFIG_SDMMC_RK29_OLD)
624 printk(KERN_ERR "%s..%d.. ====*Identify the card as SDIO , but OCR error, so fail to initialize. [%s]\n", \
625 __FUNCTION__, __LINE__, mmc_hostname(host));
632 * For SPI, enable CRC as appropriate.
634 if (mmc_host_is_spi(host)) {
635 err = mmc_spi_set_crc(host, use_spi_crc);
641 * Allocate card structure.
643 card = mmc_alloc_card(host, NULL);
649 if ((ocr & R4_MEMORY_PRESENT) &&
650 mmc_sd_get_cid(host, host->ocr & ocr, card->raw_cid, NULL) == 0) {
651 card->type = MMC_TYPE_SD_COMBO;
653 if (oldcard && (oldcard->type != MMC_TYPE_SD_COMBO ||
654 memcmp(card->raw_cid, oldcard->raw_cid, sizeof(card->raw_cid)) != 0)) {
655 mmc_remove_card(card);
659 card->type = MMC_TYPE_SDIO;
661 if (oldcard && oldcard->type != MMC_TYPE_SDIO) {
662 mmc_remove_card(card);
668 * Call the optional HC's init_card function to handle quirks.
670 if (host->ops->init_card)
671 host->ops->init_card(host, card);
674 * If the host and card support UHS-I mode request the card
675 * to switch to 1.8V signaling level. No 1.8v signalling if
676 * UHS mode is not enabled to maintain compatibility and some
677 * systems that claim 1.8v signalling in fact do not support
680 if (!powered_resume && (ocr & R4_18V_PRESENT) && mmc_host_uhs(host)) {
681 err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_180,1);
682 if (err == -EAGAIN) {
685 mmc_send_if_cond(host, host->ocr_avail);
686 mmc_remove_card(card);
690 ocr &= ~R4_18V_PRESENT;
691 host->ocr &= ~R4_18V_PRESENT;
695 ocr &= ~R4_18V_PRESENT;
696 host->ocr &= ~R4_18V_PRESENT;
700 * For native busses: set card RCA and quit open drain mode.
702 if (!powered_resume && !mmc_host_is_spi(host)) {
703 err = mmc_send_relative_addr(host, &card->rca);
708 * Update oldcard with the new RCA received from the SDIO
709 * device -- we're doing this so that it's updated in the
710 * "card" struct when oldcard overwrites that later.
713 oldcard->rca = card->rca;
715 mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
719 * Read CSD, before selecting the card
721 if (!oldcard && card->type == MMC_TYPE_SD_COMBO) {
722 err = mmc_sd_get_csd(host, card);
726 mmc_decode_cid(card);
730 * Select card, as all following commands rely on that.
732 if (!powered_resume && !mmc_host_is_spi(host)) {
733 err = mmc_select_card(card);
738 if (card->quirks & MMC_QUIRK_NONSTD_SDIO) {
740 * This is non-standard SDIO device, meaning it doesn't
741 * have any CIA (Common I/O area) registers present.
742 * It's host's responsibility to fill cccr and cis
743 * structures in init_card().
745 mmc_set_clock(host, card->cis.max_dtr);
747 if (card->cccr.high_speed) {
748 mmc_card_set_highspeed(card);
749 mmc_set_timing(card->host, MMC_TIMING_SD_HS);
755 #ifdef CONFIG_MMC_EMBEDDED_SDIO
756 if (host->embedded_sdio_data.cccr)
757 memcpy(&card->cccr, host->embedded_sdio_data.cccr, sizeof(struct sdio_cccr));
761 * Read the common registers.
763 err = sdio_read_cccr(card,ocr);
766 #ifdef CONFIG_MMC_EMBEDDED_SDIO
770 #ifdef CONFIG_MMC_EMBEDDED_SDIO
771 if (host->embedded_sdio_data.cis)
772 memcpy(&card->cis, host->embedded_sdio_data.cis, sizeof(struct sdio_cis));
776 * Read the common CIS tuples.
778 err = sdio_read_common_cis(card);
781 #ifdef CONFIG_MMC_EMBEDDED_SDIO
786 int same = (card->cis.vendor == oldcard->cis.vendor &&
787 card->cis.device == oldcard->cis.device);
788 mmc_remove_card(card);
794 mmc_fixup_device(card, NULL);
796 if (card->type == MMC_TYPE_SD_COMBO) {
797 err = mmc_sd_setup_card(host, card, oldcard != NULL);
798 /* handle as SDIO-only card if memory init failed */
801 if (mmc_host_is_spi(host))
802 /* should not fail, as it worked previously */
803 mmc_spi_set_crc(host, use_spi_crc);
804 card->type = MMC_TYPE_SDIO;
806 card->dev.type = &sd_type;
810 * If needed, disconnect card detection pull-up resistor.
812 err = sdio_disable_cd(card);
816 /* Initialization sequence for UHS-I cards */
817 /* Only if card supports 1.8v and UHS signaling */
818 if ((ocr & R4_18V_PRESENT) && card->sw_caps.sd3_bus_mode) {
819 err = mmc_sdio_init_uhs_card(card);
823 /* Card is an ultra-high-speed card */
824 mmc_card_set_uhs(card);
827 * Switch to high-speed (if supported).
829 err = sdio_enable_hs(card);
831 mmc_sd_go_highspeed(card);
836 * Change to the card's maximum speed.
838 mmc_set_clock(host, mmc_sdio_get_max_clock(card));
841 * Switch to wider bus (if supported).
843 err = sdio_enable_4bit_bus(card);
845 mmc_set_bus_width(card->host, MMC_BUS_WIDTH_4);
856 mmc_remove_card(card);
863 * Host is being removed. Free up the current card.
865 static void mmc_sdio_remove(struct mmc_host *host)
872 for (i = 0;i < host->card->sdio_funcs;i++) {
873 if (host->card->sdio_func[i]) {
874 sdio_remove_func(host->card->sdio_func[i]);
875 host->card->sdio_func[i] = NULL;
879 mmc_remove_card(host->card);
884 * Card detection callback from host.
886 static void mmc_sdio_detect(struct mmc_host *host)
893 /* Make sure card is powered before detecting it */
894 if (host->caps & MMC_CAP_POWER_OFF_CARD) {
895 err = pm_runtime_get_sync(&host->card->dev);
900 mmc_claim_host(host);
903 * Just check if our card has been removed.
905 err = mmc_select_card(host->card);
907 mmc_release_host(host);
910 * Tell PM core it's OK to power off the card now.
912 * The _sync variant is used in order to ensure that the card
913 * is left powered off in case an error occurred, and the card
914 * is going to be removed.
916 * Since there is no specific reason to believe a new user
917 * is about to show up at this point, the _sync variant is
920 if (host->caps & MMC_CAP_POWER_OFF_CARD)
921 pm_runtime_put_sync(&host->card->dev);
925 mmc_sdio_remove(host);
927 mmc_claim_host(host);
928 mmc_detach_bus(host);
930 mmc_release_host(host);
935 * SDIO suspend. We need to suspend all functions separately.
936 * Therefore all registered functions must have drivers with suspend
937 * and resume methods. Failing that we simply remove the whole card.
939 static int mmc_sdio_suspend(struct mmc_host *host)
943 for (i = 0; i < host->card->sdio_funcs; i++) {
944 struct sdio_func *func = host->card->sdio_func[i];
945 if (func && sdio_func_present(func) && func->dev.driver) {
946 const struct dev_pm_ops *pmops = func->dev.driver->pm;
947 if (!pmops || !pmops->suspend || !pmops->resume) {
948 /* force removal of entire card in that case */
951 err = pmops->suspend(&func->dev);
956 while (err && --i >= 0) {
957 struct sdio_func *func = host->card->sdio_func[i];
958 if (func && sdio_func_present(func) && func->dev.driver) {
959 const struct dev_pm_ops *pmops = func->dev.driver->pm;
960 pmops->resume(&func->dev);
964 if (!err && mmc_card_keep_power(host) && mmc_card_wake_sdio_irq(host)) {
965 mmc_claim_host(host);
966 sdio_disable_wide(host->card);
967 mmc_release_host(host);
973 static int mmc_sdio_resume(struct mmc_host *host)
980 /* Basic card reinitialization. */
981 mmc_claim_host(host);
983 /* No need to reinitialize powered-resumed nonremovable cards */
984 if (mmc_card_is_removable(host) || !mmc_card_keep_power(host))
985 err = mmc_sdio_init_card(host, host->ocr, host->card,
986 mmc_card_keep_power(host));
987 else if (mmc_card_keep_power(host) && mmc_card_wake_sdio_irq(host)) {
988 /* We may have switched to 1-bit mode during suspend */
989 err = sdio_enable_4bit_bus(host->card);
991 mmc_set_bus_width(host, MMC_BUS_WIDTH_4);
996 #if defined(CONFIG_MTK_COMBO) && defined(CONFIG_MTK_COMBO_DRIVER_VERSION_JB2)
997 /* sdio_funcs are NOT resumed yet! Signal irq only in host driver. */
999 // do not to wake up sdio_irq_thread; noted by xbw at 2013-05-08
1002 if (!err && host->sdio_irqs)
1003 wake_up_process(host->sdio_irq_thread);
1005 mmc_release_host(host);
1008 * If the card looked to be the same as before suspending, then
1009 * we proceed to resume all card functions. If one of them returns
1010 * an error then we simply return that error to the core and the
1011 * card will be redetected as new. It is the responsibility of
1012 * the function driver to perform further tests with the extra
1013 * knowledge it has of the card to confirm the card is indeed the
1014 * same as before suspending (same MAC address for network cards,
1015 * etc.) and return an error otherwise.
1017 for (i = 0; !err && i < host->card->sdio_funcs; i++) {
1018 struct sdio_func *func = host->card->sdio_func[i];
1019 if (func && sdio_func_present(func) && func->dev.driver) {
1020 const struct dev_pm_ops *pmops = func->dev.driver->pm;
1021 err = pmops->resume(&func->dev);
1028 static int mmc_sdio_power_restore(struct mmc_host *host)
1034 BUG_ON(!host->card);
1036 mmc_claim_host(host);
1039 * Reset the card by performing the same steps that are taken by
1040 * mmc_rescan_try_freq() and mmc_attach_sdio() during a "normal" probe.
1042 * sdio_reset() is technically not needed. Having just powered up the
1043 * hardware, it should already be in reset state. However, some
1044 * platforms (such as SD8686 on OLPC) do not instantly cut power,
1045 * meaning that a reset is required when restoring power soon after
1046 * powering off. It is harmless in other cases.
1048 * The CMD5 reset (mmc_send_io_op_cond()), according to the SDIO spec,
1049 * is not necessary for non-removable cards. However, it is required
1050 * for OLPC SD8686 (which expects a [CMD5,5,3,7] init sequence), and
1051 * harmless in other situations.
1053 * With these steps taken, mmc_select_voltage() is also required to
1054 * restore the correct voltage setting of the card.
1058 mmc_send_if_cond(host, host->ocr_avail);
1060 ret = mmc_send_io_op_cond(host, 0, &ocr);
1064 if (host->ocr_avail_sdio)
1065 host->ocr_avail = host->ocr_avail_sdio;
1067 host->ocr = mmc_select_voltage(host, ocr & ~0x7F);
1073 ret = mmc_sdio_init_card(host, host->ocr, host->card,
1074 mmc_card_keep_power(host));
1075 if (!ret && host->sdio_irqs)
1076 mmc_signal_sdio_irq(host);
1079 mmc_release_host(host);
1084 static const struct mmc_bus_ops mmc_sdio_ops = {
1085 .remove = mmc_sdio_remove,
1086 .detect = mmc_sdio_detect,
1087 .suspend = mmc_sdio_suspend,
1088 .resume = mmc_sdio_resume,
1089 .power_restore = mmc_sdio_power_restore,
1094 * Starting point for SDIO card init.
1096 int mmc_attach_sdio(struct mmc_host *host)
1100 struct mmc_card *card;
1103 WARN_ON(!host->claimed);
1105 err = mmc_send_io_op_cond(host, 0, &ocr);
1107 #if defined(CONFIG_SDMMC_RK29) && !defined(CONFIG_SDMMC_RK29_OLD)
1109 return 0xFF;//return err; //Modifyed by xbw at 2011-11-17
1111 printk(KERN_INFO "\n%s..%d.. ===== Begin to identify card as SDIO-card. [%s]\n",\
1112 __FUNCTION__, __LINE__, mmc_hostname(host));
1117 mmc_attach_bus(host, &mmc_sdio_ops);
1118 if (host->ocr_avail_sdio)
1119 host->ocr_avail = host->ocr_avail_sdio;
1122 * Sanity check the voltages that the card claims to
1126 printk(KERN_WARNING "%s: card claims to support voltages "
1127 "below the defined range. These will be ignored.\n",
1128 mmc_hostname(host));
1132 host->ocr = mmc_select_voltage(host, ocr);
1135 * Can we support the voltage(s) of the card(s)?
1143 * Detect and init the card.
1145 err = mmc_sdio_init_card(host, host->ocr, NULL, 0);
1151 * Enable runtime PM only if supported by host+card+board
1153 if (host->caps & MMC_CAP_POWER_OFF_CARD) {
1155 * Let runtime PM core know our card is active
1157 err = pm_runtime_set_active(&card->dev);
1162 * Enable runtime PM for this card
1164 pm_runtime_enable(&card->dev);
1168 * The number of functions on the card is encoded inside
1171 funcs = (ocr & 0x70000000) >> 28;
1172 card->sdio_funcs = 0;
1174 #ifdef CONFIG_MMC_EMBEDDED_SDIO
1175 if (host->embedded_sdio_data.funcs)
1176 card->sdio_funcs = funcs = host->embedded_sdio_data.num_funcs;
1180 * Initialize (but don't add) all present functions.
1182 for (i = 0; i < funcs; i++, card->sdio_funcs++) {
1183 #ifdef CONFIG_MMC_EMBEDDED_SDIO
1184 if (host->embedded_sdio_data.funcs) {
1185 struct sdio_func *tmp;
1187 tmp = sdio_alloc_func(host->card);
1191 card->sdio_func[i] = tmp;
1192 tmp->class = host->embedded_sdio_data.funcs[i].f_class;
1193 tmp->max_blksize = host->embedded_sdio_data.funcs[i].f_maxblksize;
1194 tmp->vendor = card->cis.vendor;
1195 tmp->device = card->cis.device;
1198 err = sdio_init_func(host->card, i + 1);
1201 #ifdef CONFIG_MMC_EMBEDDED_SDIO
1205 * Enable Runtime PM for this func (if supported)
1207 if (host->caps & MMC_CAP_POWER_OFF_CARD)
1208 pm_runtime_enable(&card->sdio_func[i]->dev);
1212 * First add the card to the driver model...
1214 mmc_release_host(host);
1215 err = mmc_add_card(host->card);
1220 * ...then the SDIO functions.
1222 for (i = 0;i < funcs;i++) {
1223 err = sdio_add_func(host->card->sdio_func[i]);
1228 mmc_claim_host(host);
1233 /* Remove without lock if the device has been added. */
1234 mmc_sdio_remove(host);
1235 mmc_claim_host(host);
1237 /* And with lock if it hasn't been added. */
1238 mmc_release_host(host);
1240 mmc_sdio_remove(host);
1241 mmc_claim_host(host);
1243 mmc_detach_bus(host);
1245 printk(KERN_ERR "%s: error %d whilst initialising SDIO card\n",
1246 mmc_hostname(host), err);
1251 int sdio_reset_comm(struct mmc_card *card)
1253 struct mmc_host *host = card->host;
1257 printk("%s():\n", __func__);
1258 mmc_claim_host(host);
1260 #if defined(CONFIG_SDMMC_RK29) && !defined(CONFIG_SDMMC_RK29_OLD)
1261 host->sdmmc_host_hw_init(mmc_priv(host)); //added by xbw , at 2011-10-18
1262 host->ios.clock = host->f_min; //for avoid 25MHz once again during init process.noted by xbw at 2011-11-14
1267 mmc_set_clock(host, host->f_min);
1269 err = mmc_send_io_op_cond(host, 0, &ocr);
1273 host->ocr = mmc_select_voltage(host, ocr);
1279 err = mmc_send_io_op_cond(host, host->ocr, &ocr);
1283 if (mmc_host_is_spi(host)) {
1284 err = mmc_spi_set_crc(host, use_spi_crc);
1289 if (!mmc_host_is_spi(host)) {
1290 err = mmc_send_relative_addr(host, &card->rca);
1293 mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
1295 if (!mmc_host_is_spi(host)) {
1296 err = mmc_select_card(card);
1302 * Switch to high-speed (if supported).
1304 err = sdio_enable_hs(card);
1306 mmc_sd_go_highspeed(card);
1311 * Change to the card's maximum speed.
1313 mmc_set_clock(host, mmc_sdio_get_max_clock(card));
1315 err = sdio_enable_4bit_bus(card);
1317 mmc_set_bus_width(host, MMC_BUS_WIDTH_4);
1321 mmc_release_host(host);
1324 printk("%s: Error resetting SDIO communications (%d)\n",
1325 mmc_hostname(host), err);
1326 mmc_release_host(host);
1329 EXPORT_SYMBOL(sdio_reset_comm);