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/mmc.h>
18 #include <linux/mmc/sdio.h>
19 #include <linux/mmc/sdio_func.h>
20 #include <linux/mmc/sdio_ids.h>
31 static int sdio_read_fbr(struct sdio_func *func)
36 if (mmc_card_nonstd_func_interface(func->card)) {
37 func->class = SDIO_CLASS_NONE;
41 ret = mmc_io_rw_direct(func->card, 0, 0,
42 SDIO_FBR_BASE(func->num) + SDIO_FBR_STD_IF, 0, &data);
49 ret = mmc_io_rw_direct(func->card, 0, 0,
50 SDIO_FBR_BASE(func->num) + SDIO_FBR_STD_IF_EXT, 0, &data);
61 static int sdio_init_func(struct mmc_card *card, unsigned int fn)
64 struct sdio_func *func;
66 BUG_ON(fn > SDIO_MAX_FUNCS);
68 func = sdio_alloc_func(card);
74 if (!(card->quirks & MMC_QUIRK_NONSTD_SDIO)) {
75 ret = sdio_read_fbr(func);
79 ret = sdio_read_func_cis(func);
83 func->vendor = func->card->cis.vendor;
84 func->device = func->card->cis.device;
85 func->max_blksize = func->card->cis.blksize;
88 card->sdio_func[fn - 1] = func;
94 * It is okay to remove the function here even though we hold
95 * the host lock as we haven't registered the device yet.
97 sdio_remove_func(func);
101 static int sdio_read_cccr(struct mmc_card *card)
108 memset(&card->cccr, 0, sizeof(struct sdio_cccr));
110 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_CCCR, 0, &data);
114 cccr_vsn = data & 0x0f;
116 if (cccr_vsn > SDIO_CCCR_REV_3_00) {
117 pr_err("%s: unrecognised CCCR structure version %d\n",
118 mmc_hostname(card->host), cccr_vsn);
122 card->cccr.sdio_vsn = (data & 0xf0) >> 4;
124 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_CAPS, 0, &data);
128 if (data & SDIO_CCCR_CAP_SMB)
129 card->cccr.multi_block = 1;
130 if (data & SDIO_CCCR_CAP_LSC)
131 card->cccr.low_speed = 1;
132 if (data & SDIO_CCCR_CAP_4BLS)
133 card->cccr.wide_bus = 1;
135 if (cccr_vsn >= SDIO_CCCR_REV_1_10) {
136 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_POWER, 0, &data);
140 if (data & SDIO_POWER_SMPC)
141 card->cccr.high_power = 1;
144 if (cccr_vsn >= SDIO_CCCR_REV_1_20) {
145 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_SPEED, 0, &speed);
149 card->scr.sda_spec3 = 0;
150 card->sw_caps.sd3_bus_mode = 0;
151 card->sw_caps.sd3_drv_type = 0;
152 if (cccr_vsn >= SDIO_CCCR_REV_3_00) {
153 card->scr.sda_spec3 = 1;
154 ret = mmc_io_rw_direct(card, 0, 0,
155 SDIO_CCCR_UHS, 0, &data);
159 if (card->host->caps &
160 (MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 |
161 MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR104 |
162 MMC_CAP_UHS_DDR50)) {
163 if (data & SDIO_UHS_DDR50)
164 card->sw_caps.sd3_bus_mode
165 |= SD_MODE_UHS_DDR50;
167 if (data & SDIO_UHS_SDR50)
168 card->sw_caps.sd3_bus_mode
169 |= SD_MODE_UHS_SDR50;
171 if (data & SDIO_UHS_SDR104)
172 card->sw_caps.sd3_bus_mode
173 |= SD_MODE_UHS_SDR104;
176 ret = mmc_io_rw_direct(card, 0, 0,
177 SDIO_CCCR_DRIVE_STRENGTH, 0, &data);
181 if (data & SDIO_DRIVE_SDTA)
182 card->sw_caps.sd3_drv_type |= SD_DRIVER_TYPE_A;
183 if (data & SDIO_DRIVE_SDTC)
184 card->sw_caps.sd3_drv_type |= SD_DRIVER_TYPE_C;
185 if (data & SDIO_DRIVE_SDTD)
186 card->sw_caps.sd3_drv_type |= SD_DRIVER_TYPE_D;
189 /* if no uhs mode ensure we check for high speed */
190 if (!card->sw_caps.sd3_bus_mode) {
191 if (speed & SDIO_SPEED_SHS) {
192 card->cccr.high_speed = 1;
193 card->sw_caps.hs_max_dtr = 50000000;
195 card->cccr.high_speed = 0;
196 card->sw_caps.hs_max_dtr = 25000000;
205 static int sdio_enable_wide(struct mmc_card *card)
210 if (!(card->host->caps & MMC_CAP_4_BIT_DATA))
213 if (card->cccr.low_speed && !card->cccr.wide_bus)
216 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl);
220 ctrl |= SDIO_BUS_WIDTH_4BIT;
222 ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL);
230 * If desired, disconnect the pull-up resistor on CD/DAT[3] (pin 1)
231 * of the card. This may be required on certain setups of boards,
232 * controllers and embedded sdio device which do not need the card's
233 * pull-up. As a result, card detection is disabled and power is saved.
235 static int sdio_disable_cd(struct mmc_card *card)
240 if (!mmc_card_disable_cd(card))
243 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl);
247 ctrl |= SDIO_BUS_CD_DISABLE;
249 return mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL);
253 * Devices that remain active during a system suspend are
254 * put back into 1-bit mode.
256 static int sdio_disable_wide(struct mmc_card *card)
261 if (!(card->host->caps & MMC_CAP_4_BIT_DATA))
264 if (card->cccr.low_speed && !card->cccr.wide_bus)
267 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl);
271 if (!(ctrl & SDIO_BUS_WIDTH_4BIT))
274 ctrl &= ~SDIO_BUS_WIDTH_4BIT;
275 ctrl |= SDIO_BUS_ASYNC_INT;
277 ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL);
281 mmc_set_bus_width(card->host, MMC_BUS_WIDTH_1);
287 static int sdio_enable_4bit_bus(struct mmc_card *card)
291 if (card->type == MMC_TYPE_SDIO)
292 return sdio_enable_wide(card);
294 if ((card->host->caps & MMC_CAP_4_BIT_DATA) &&
295 (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) {
296 err = mmc_app_set_bus_width(card, MMC_BUS_WIDTH_4);
302 err = sdio_enable_wide(card);
304 mmc_app_set_bus_width(card, MMC_BUS_WIDTH_1);
311 * Test if the card supports high-speed mode and, if so, switch to it.
313 static int mmc_sdio_switch_hs(struct mmc_card *card, int enable)
318 if (!(card->host->caps & MMC_CAP_SD_HIGHSPEED))
321 if (!card->cccr.high_speed)
324 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_SPEED, 0, &speed);
329 speed |= SDIO_SPEED_EHS;
331 speed &= ~SDIO_SPEED_EHS;
333 ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_SPEED, speed, NULL);
341 * Enable SDIO/combo card's high-speed mode. Return 0/1 if [not]supported.
343 static int sdio_enable_hs(struct mmc_card *card)
347 ret = mmc_sdio_switch_hs(card, true);
348 if (ret <= 0 || card->type == MMC_TYPE_SDIO)
351 ret = mmc_sd_switch_hs(card);
353 mmc_sdio_switch_hs(card, false);
358 static unsigned mmc_sdio_get_max_clock(struct mmc_card *card)
362 if (mmc_card_highspeed(card)) {
364 * The SDIO specification doesn't mention how
365 * the CIS transfer speed register relates to
366 * high-speed, but it seems that 50 MHz is
371 max_dtr = card->cis.max_dtr;
374 if (card->type == MMC_TYPE_SD_COMBO)
375 max_dtr = min(max_dtr, mmc_sd_get_max_clock(card));
380 static unsigned char host_drive_to_sdio_drive(int host_strength)
382 switch (host_strength) {
383 case MMC_SET_DRIVER_TYPE_A:
384 return SDIO_DTSx_SET_TYPE_A;
385 case MMC_SET_DRIVER_TYPE_B:
386 return SDIO_DTSx_SET_TYPE_B;
387 case MMC_SET_DRIVER_TYPE_C:
388 return SDIO_DTSx_SET_TYPE_C;
389 case MMC_SET_DRIVER_TYPE_D:
390 return SDIO_DTSx_SET_TYPE_D;
392 return SDIO_DTSx_SET_TYPE_B;
396 static void sdio_select_driver_type(struct mmc_card *card)
398 int host_drv_type = SD_DRIVER_TYPE_B;
399 int card_drv_type = SD_DRIVER_TYPE_B;
401 unsigned char card_strength;
405 * If the host doesn't support any of the Driver Types A,C or D,
406 * or there is no board specific handler then default Driver
409 if (!(card->host->caps &
410 (MMC_CAP_DRIVER_TYPE_A |
411 MMC_CAP_DRIVER_TYPE_C |
412 MMC_CAP_DRIVER_TYPE_D)))
415 if (!card->host->ops->select_drive_strength)
418 if (card->host->caps & MMC_CAP_DRIVER_TYPE_A)
419 host_drv_type |= SD_DRIVER_TYPE_A;
421 if (card->host->caps & MMC_CAP_DRIVER_TYPE_C)
422 host_drv_type |= SD_DRIVER_TYPE_C;
424 if (card->host->caps & MMC_CAP_DRIVER_TYPE_D)
425 host_drv_type |= SD_DRIVER_TYPE_D;
427 if (card->sw_caps.sd3_drv_type & SD_DRIVER_TYPE_A)
428 card_drv_type |= SD_DRIVER_TYPE_A;
430 if (card->sw_caps.sd3_drv_type & SD_DRIVER_TYPE_C)
431 card_drv_type |= SD_DRIVER_TYPE_C;
433 if (card->sw_caps.sd3_drv_type & SD_DRIVER_TYPE_D)
434 card_drv_type |= SD_DRIVER_TYPE_D;
437 * The drive strength that the hardware can support
438 * depends on the board design. Pass the appropriate
439 * information and let the hardware specific code
440 * return what is possible given the options
442 drive_strength = card->host->ops->select_drive_strength(
443 card->sw_caps.uhs_max_dtr,
444 host_drv_type, card_drv_type);
446 /* if error just use default for drive strength B */
447 err = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_DRIVE_STRENGTH, 0,
452 card_strength &= ~(SDIO_DRIVE_DTSx_MASK<<SDIO_DRIVE_DTSx_SHIFT);
453 card_strength |= host_drive_to_sdio_drive(drive_strength);
455 err = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_DRIVE_STRENGTH,
456 card_strength, NULL);
458 /* if error default to drive strength B */
460 mmc_set_driver_type(card->host, drive_strength);
464 static int sdio_set_bus_speed_mode(struct mmc_card *card)
466 unsigned int bus_speed, timing;
471 * If the host doesn't support any of the UHS-I modes, fallback on
474 if (!(card->host->caps & (MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 |
475 MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_DDR50)))
478 bus_speed = SDIO_SPEED_SDR12;
479 timing = MMC_TIMING_UHS_SDR12;
480 if ((card->host->caps & MMC_CAP_UHS_SDR104) &&
481 (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_SDR104)) {
482 bus_speed = SDIO_SPEED_SDR104;
483 timing = MMC_TIMING_UHS_SDR104;
484 card->sw_caps.uhs_max_dtr = UHS_SDR104_MAX_DTR;
485 } else if ((card->host->caps & MMC_CAP_UHS_DDR50) &&
486 (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_DDR50)) {
487 bus_speed = SDIO_SPEED_DDR50;
488 timing = MMC_TIMING_UHS_DDR50;
489 card->sw_caps.uhs_max_dtr = UHS_DDR50_MAX_DTR;
490 } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
491 MMC_CAP_UHS_SDR50)) && (card->sw_caps.sd3_bus_mode &
492 SD_MODE_UHS_SDR50)) {
493 bus_speed = SDIO_SPEED_SDR50;
494 timing = MMC_TIMING_UHS_SDR50;
495 card->sw_caps.uhs_max_dtr = UHS_SDR50_MAX_DTR;
496 } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
497 MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR25)) &&
498 (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_SDR25)) {
499 bus_speed = SDIO_SPEED_SDR25;
500 timing = MMC_TIMING_UHS_SDR25;
501 card->sw_caps.uhs_max_dtr = UHS_SDR25_MAX_DTR;
502 } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
503 MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR25 |
504 MMC_CAP_UHS_SDR12)) && (card->sw_caps.sd3_bus_mode &
505 SD_MODE_UHS_SDR12)) {
506 bus_speed = SDIO_SPEED_SDR12;
507 timing = MMC_TIMING_UHS_SDR12;
508 card->sw_caps.uhs_max_dtr = UHS_SDR12_MAX_DTR;
511 err = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_SPEED, 0, &speed);
515 speed &= ~SDIO_SPEED_BSS_MASK;
517 err = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_SPEED, speed, NULL);
522 mmc_set_timing(card->host, timing);
523 mmc_set_clock(card->host, card->sw_caps.uhs_max_dtr);
530 * UHS-I specific initialization procedure
532 static int mmc_sdio_init_uhs_card(struct mmc_card *card)
536 if (!card->scr.sda_spec3)
540 * Switch to wider bus (if supported).
542 if (card->host->caps & MMC_CAP_4_BIT_DATA) {
543 err = sdio_enable_4bit_bus(card);
545 mmc_set_bus_width(card->host, MMC_BUS_WIDTH_4);
550 /* Set the driver strength for the card */
551 sdio_select_driver_type(card);
553 /* Set bus speed mode of the card */
554 err = sdio_set_bus_speed_mode(card);
558 /* Initialize and start re-tuning timer */
559 if (!mmc_host_is_spi(card->host) && card->host->ops->execute_tuning)
560 err = card->host->ops->execute_tuning(card->host,
561 MMC_SEND_TUNING_BLOCK);
569 * Handle the detection and initialisation of a card.
571 * In the case of a resume, "oldcard" will contain the card
572 * we're trying to reinitialise.
574 static int mmc_sdio_init_card(struct mmc_host *host, u32 ocr,
575 struct mmc_card *oldcard, int powered_resume)
577 struct mmc_card *card;
581 WARN_ON(!host->claimed);
584 * Inform the card of the voltage
586 if (!powered_resume) {
587 err = mmc_send_io_op_cond(host, host->ocr, &ocr);
593 * For SPI, enable CRC as appropriate.
595 if (mmc_host_is_spi(host)) {
596 err = mmc_spi_set_crc(host, use_spi_crc);
602 * Allocate card structure.
604 card = mmc_alloc_card(host, NULL);
610 if ((ocr & R4_MEMORY_PRESENT) &&
611 mmc_sd_get_cid(host, host->ocr & ocr, card->raw_cid, NULL) == 0) {
612 card->type = MMC_TYPE_SD_COMBO;
614 if (oldcard && (oldcard->type != MMC_TYPE_SD_COMBO ||
615 memcmp(card->raw_cid, oldcard->raw_cid, sizeof(card->raw_cid)) != 0)) {
616 mmc_remove_card(card);
620 card->type = MMC_TYPE_SDIO;
622 if (oldcard && oldcard->type != MMC_TYPE_SDIO) {
623 mmc_remove_card(card);
629 * Call the optional HC's init_card function to handle quirks.
631 if (host->ops->init_card)
632 host->ops->init_card(host, card);
635 * If the host and card support UHS-I mode request the card
636 * to switch to 1.8V signaling level. No 1.8v signalling if
637 * UHS mode is not enabled to maintain compatibilty and some
638 * systems that claim 1.8v signalling in fact do not support
641 if ((ocr & R4_18V_PRESENT) &&
643 (MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 |
644 MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR104 |
645 MMC_CAP_UHS_DDR50))) {
646 err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_180,
649 ocr &= ~R4_18V_PRESENT;
650 host->ocr &= ~R4_18V_PRESENT;
654 ocr &= ~R4_18V_PRESENT;
655 host->ocr &= ~R4_18V_PRESENT;
659 * For native busses: set card RCA and quit open drain mode.
661 if (!powered_resume && !mmc_host_is_spi(host)) {
662 err = mmc_send_relative_addr(host, &card->rca);
667 * Update oldcard with the new RCA received from the SDIO
668 * device -- we're doing this so that it's updated in the
669 * "card" struct when oldcard overwrites that later.
672 oldcard->rca = card->rca;
676 * Read CSD, before selecting the card
678 if (!oldcard && card->type == MMC_TYPE_SD_COMBO) {
679 err = mmc_sd_get_csd(host, card);
683 mmc_decode_cid(card);
687 * Select card, as all following commands rely on that.
689 if (!powered_resume && !mmc_host_is_spi(host)) {
690 err = mmc_select_card(card);
695 if (card->quirks & MMC_QUIRK_NONSTD_SDIO) {
697 * This is non-standard SDIO device, meaning it doesn't
698 * have any CIA (Common I/O area) registers present.
699 * It's host's responsibility to fill cccr and cis
700 * structures in init_card().
702 mmc_set_clock(host, card->cis.max_dtr);
704 if (card->cccr.high_speed) {
705 mmc_card_set_highspeed(card);
706 mmc_set_timing(card->host, MMC_TIMING_SD_HS);
713 * Read the common registers.
715 err = sdio_read_cccr(card);
720 * Read the common CIS tuples.
722 err = sdio_read_common_cis(card);
727 int same = (card->cis.vendor == oldcard->cis.vendor &&
728 card->cis.device == oldcard->cis.device);
729 mmc_remove_card(card);
735 mmc_fixup_device(card, NULL);
737 if (card->type == MMC_TYPE_SD_COMBO) {
738 err = mmc_sd_setup_card(host, card, oldcard != NULL);
739 /* handle as SDIO-only card if memory init failed */
742 if (mmc_host_is_spi(host))
743 /* should not fail, as it worked previously */
744 mmc_spi_set_crc(host, use_spi_crc);
745 card->type = MMC_TYPE_SDIO;
747 card->dev.type = &sd_type;
751 * If needed, disconnect card detection pull-up resistor.
753 err = sdio_disable_cd(card);
757 /* Initialization sequence for UHS-I cards */
758 /* Only if card supports 1.8v and UHS signaling */
759 if ((ocr & R4_18V_PRESENT) && card->sw_caps.sd3_bus_mode) {
760 err = mmc_sdio_init_uhs_card(card);
764 /* Card is an ultra-high-speed card */
765 mmc_card_set_uhs(card);
768 * Switch to high-speed (if supported).
770 err = sdio_enable_hs(card);
772 mmc_sd_go_highspeed(card);
777 * Change to the card's maximum speed.
779 mmc_set_clock(host, mmc_sdio_get_max_clock(card));
782 * Switch to wider bus (if supported).
784 err = sdio_enable_4bit_bus(card);
786 mmc_set_bus_width(card->host, MMC_BUS_WIDTH_4);
797 mmc_remove_card(card);
804 * Host is being removed. Free up the current card.
806 static void mmc_sdio_remove(struct mmc_host *host)
813 for (i = 0;i < host->card->sdio_funcs;i++) {
814 if (host->card->sdio_func[i]) {
815 sdio_remove_func(host->card->sdio_func[i]);
816 host->card->sdio_func[i] = NULL;
820 mmc_remove_card(host->card);
825 * Card detection - card is alive.
827 static int mmc_sdio_alive(struct mmc_host *host)
829 return mmc_select_card(host->card);
833 * Card detection callback from host.
835 static void mmc_sdio_detect(struct mmc_host *host)
842 /* Make sure card is powered before detecting it */
843 if (host->caps & MMC_CAP_POWER_OFF_CARD) {
844 err = pm_runtime_get_sync(&host->card->dev);
849 mmc_claim_host(host);
852 * Just check if our card has been removed.
854 err = _mmc_detect_card_removed(host);
856 mmc_release_host(host);
859 * Tell PM core it's OK to power off the card now.
861 * The _sync variant is used in order to ensure that the card
862 * is left powered off in case an error occurred, and the card
863 * is going to be removed.
865 * Since there is no specific reason to believe a new user
866 * is about to show up at this point, the _sync variant is
869 if (host->caps & MMC_CAP_POWER_OFF_CARD)
870 pm_runtime_put_sync(&host->card->dev);
874 mmc_sdio_remove(host);
876 mmc_claim_host(host);
877 mmc_detach_bus(host);
879 mmc_release_host(host);
884 * SDIO suspend. We need to suspend all functions separately.
885 * Therefore all registered functions must have drivers with suspend
886 * and resume methods. Failing that we simply remove the whole card.
888 static int mmc_sdio_suspend(struct mmc_host *host)
892 for (i = 0; i < host->card->sdio_funcs; i++) {
893 struct sdio_func *func = host->card->sdio_func[i];
894 if (func && sdio_func_present(func) && func->dev.driver) {
895 const struct dev_pm_ops *pmops = func->dev.driver->pm;
896 if (!pmops || !pmops->suspend || !pmops->resume) {
897 /* force removal of entire card in that case */
900 err = pmops->suspend(&func->dev);
905 while (err && --i >= 0) {
906 struct sdio_func *func = host->card->sdio_func[i];
907 if (func && sdio_func_present(func) && func->dev.driver) {
908 const struct dev_pm_ops *pmops = func->dev.driver->pm;
909 pmops->resume(&func->dev);
913 if (!err && mmc_card_keep_power(host) && mmc_card_wake_sdio_irq(host)) {
914 mmc_claim_host(host);
915 sdio_disable_wide(host->card);
916 mmc_release_host(host);
922 static int mmc_sdio_resume(struct mmc_host *host)
929 /* Basic card reinitialization. */
930 mmc_claim_host(host);
932 /* No need to reinitialize powered-resumed nonremovable cards */
933 if (mmc_card_is_removable(host) || !mmc_card_keep_power(host))
934 err = mmc_sdio_init_card(host, host->ocr, host->card,
935 mmc_card_keep_power(host));
936 else if (mmc_card_keep_power(host) && mmc_card_wake_sdio_irq(host)) {
937 /* We may have switched to 1-bit mode during suspend */
938 err = sdio_enable_4bit_bus(host->card);
940 mmc_set_bus_width(host, MMC_BUS_WIDTH_4);
945 if (!err && host->sdio_irqs)
946 mmc_signal_sdio_irq(host);
947 mmc_release_host(host);
950 * If the card looked to be the same as before suspending, then
951 * we proceed to resume all card functions. If one of them returns
952 * an error then we simply return that error to the core and the
953 * card will be redetected as new. It is the responsibility of
954 * the function driver to perform further tests with the extra
955 * knowledge it has of the card to confirm the card is indeed the
956 * same as before suspending (same MAC address for network cards,
957 * etc.) and return an error otherwise.
959 for (i = 0; !err && i < host->card->sdio_funcs; i++) {
960 struct sdio_func *func = host->card->sdio_func[i];
961 if (func && sdio_func_present(func) && func->dev.driver) {
962 const struct dev_pm_ops *pmops = func->dev.driver->pm;
963 err = pmops->resume(&func->dev);
970 static int mmc_sdio_power_restore(struct mmc_host *host)
978 mmc_claim_host(host);
981 * Reset the card by performing the same steps that are taken by
982 * mmc_rescan_try_freq() and mmc_attach_sdio() during a "normal" probe.
984 * sdio_reset() is technically not needed. Having just powered up the
985 * hardware, it should already be in reset state. However, some
986 * platforms (such as SD8686 on OLPC) do not instantly cut power,
987 * meaning that a reset is required when restoring power soon after
988 * powering off. It is harmless in other cases.
990 * The CMD5 reset (mmc_send_io_op_cond()), according to the SDIO spec,
991 * is not necessary for non-removable cards. However, it is required
992 * for OLPC SD8686 (which expects a [CMD5,5,3,7] init sequence), and
993 * harmless in other situations.
995 * With these steps taken, mmc_select_voltage() is also required to
996 * restore the correct voltage setting of the card.
1000 mmc_send_if_cond(host, host->ocr_avail);
1002 ret = mmc_send_io_op_cond(host, 0, &ocr);
1006 if (host->ocr_avail_sdio)
1007 host->ocr_avail = host->ocr_avail_sdio;
1009 host->ocr = mmc_select_voltage(host, ocr & ~0x7F);
1015 ret = mmc_sdio_init_card(host, host->ocr, host->card,
1016 mmc_card_keep_power(host));
1017 if (!ret && host->sdio_irqs)
1018 mmc_signal_sdio_irq(host);
1021 mmc_release_host(host);
1026 static const struct mmc_bus_ops mmc_sdio_ops = {
1027 .remove = mmc_sdio_remove,
1028 .detect = mmc_sdio_detect,
1029 .suspend = mmc_sdio_suspend,
1030 .resume = mmc_sdio_resume,
1031 .power_restore = mmc_sdio_power_restore,
1032 .alive = mmc_sdio_alive,
1037 * Starting point for SDIO card init.
1039 int mmc_attach_sdio(struct mmc_host *host)
1043 struct mmc_card *card;
1046 WARN_ON(!host->claimed);
1048 err = mmc_send_io_op_cond(host, 0, &ocr);
1052 mmc_attach_bus(host, &mmc_sdio_ops);
1053 if (host->ocr_avail_sdio)
1054 host->ocr_avail = host->ocr_avail_sdio;
1057 * Sanity check the voltages that the card claims to
1061 pr_warning("%s: card claims to support voltages "
1062 "below the defined range. These will be ignored.\n",
1063 mmc_hostname(host));
1067 host->ocr = mmc_select_voltage(host, ocr);
1070 * Can we support the voltage(s) of the card(s)?
1078 * Detect and init the card.
1080 err = mmc_sdio_init_card(host, host->ocr, NULL, 0);
1082 if (err == -EAGAIN) {
1084 * Retry initialization with S18R set to 0.
1086 host->ocr &= ~R4_18V_PRESENT;
1087 err = mmc_sdio_init_card(host, host->ocr, NULL, 0);
1095 * Enable runtime PM only if supported by host+card+board
1097 if (host->caps & MMC_CAP_POWER_OFF_CARD) {
1099 * Let runtime PM core know our card is active
1101 err = pm_runtime_set_active(&card->dev);
1106 * Enable runtime PM for this card
1108 pm_runtime_enable(&card->dev);
1112 * The number of functions on the card is encoded inside
1115 funcs = (ocr & 0x70000000) >> 28;
1116 card->sdio_funcs = 0;
1119 * Initialize (but don't add) all present functions.
1121 for (i = 0; i < funcs; i++, card->sdio_funcs++) {
1122 err = sdio_init_func(host->card, i + 1);
1127 * Enable Runtime PM for this func (if supported)
1129 if (host->caps & MMC_CAP_POWER_OFF_CARD)
1130 pm_runtime_enable(&card->sdio_func[i]->dev);
1134 * First add the card to the driver model...
1136 mmc_release_host(host);
1137 err = mmc_add_card(host->card);
1142 * ...then the SDIO functions.
1144 for (i = 0;i < funcs;i++) {
1145 err = sdio_add_func(host->card->sdio_func[i]);
1150 mmc_claim_host(host);
1155 /* Remove without lock if the device has been added. */
1156 mmc_sdio_remove(host);
1157 mmc_claim_host(host);
1159 /* And with lock if it hasn't been added. */
1160 mmc_release_host(host);
1162 mmc_sdio_remove(host);
1163 mmc_claim_host(host);
1165 mmc_detach_bus(host);
1167 pr_err("%s: error %d whilst initialising SDIO card\n",
1168 mmc_hostname(host), err);