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>
30 static int sdio_read_fbr(struct sdio_func *func)
35 if (mmc_card_nonstd_func_interface(func->card)) {
36 func->class = SDIO_CLASS_NONE;
40 ret = mmc_io_rw_direct(func->card, 0, 0,
41 SDIO_FBR_BASE(func->num) + SDIO_FBR_STD_IF, 0, &data);
48 ret = mmc_io_rw_direct(func->card, 0, 0,
49 SDIO_FBR_BASE(func->num) + SDIO_FBR_STD_IF_EXT, 0, &data);
60 static int sdio_init_func(struct mmc_card *card, unsigned int fn)
63 struct sdio_func *func;
65 BUG_ON(fn > SDIO_MAX_FUNCS);
67 func = sdio_alloc_func(card);
73 if (!(card->quirks & MMC_QUIRK_NONSTD_SDIO)) {
74 ret = sdio_read_fbr(func);
78 ret = sdio_read_func_cis(func);
82 func->vendor = func->card->cis.vendor;
83 func->device = func->card->cis.device;
84 func->max_blksize = func->card->cis.blksize;
87 card->sdio_func[fn - 1] = func;
93 * It is okay to remove the function here even though we hold
94 * the host lock as we haven't registered the device yet.
96 sdio_remove_func(func);
100 static int sdio_read_cccr(struct mmc_card *card)
107 memset(&card->cccr, 0, sizeof(struct sdio_cccr));
109 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_CCCR, 0, &data);
113 cccr_vsn = data & 0x0f;
115 if (cccr_vsn > SDIO_CCCR_REV_3_00) {
116 pr_err("%s: unrecognised CCCR structure version %d\n",
117 mmc_hostname(card->host), cccr_vsn);
121 card->cccr.sdio_vsn = (data & 0xf0) >> 4;
123 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_CAPS, 0, &data);
127 if (data & SDIO_CCCR_CAP_SMB)
128 card->cccr.multi_block = 1;
129 if (data & SDIO_CCCR_CAP_LSC)
130 card->cccr.low_speed = 1;
131 if (data & SDIO_CCCR_CAP_4BLS)
132 card->cccr.wide_bus = 1;
134 if (cccr_vsn >= SDIO_CCCR_REV_1_10) {
135 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_POWER, 0, &data);
139 if (data & SDIO_POWER_SMPC)
140 card->cccr.high_power = 1;
143 if (cccr_vsn >= SDIO_CCCR_REV_1_20) {
144 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_SPEED, 0, &speed);
148 card->scr.sda_spec3 = 0;
149 card->sw_caps.sd3_bus_mode = 0;
150 card->sw_caps.sd3_drv_type = 0;
151 if (cccr_vsn >= SDIO_CCCR_REV_3_00) {
152 card->scr.sda_spec3 = 1;
153 ret = mmc_io_rw_direct(card, 0, 0,
154 SDIO_CCCR_UHS, 0, &data);
158 if (card->host->caps &
159 (MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 |
160 MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR104 |
161 MMC_CAP_UHS_DDR50)) {
162 if (data & SDIO_UHS_DDR50)
163 card->sw_caps.sd3_bus_mode
164 |= SD_MODE_UHS_DDR50;
166 if (data & SDIO_UHS_SDR50)
167 card->sw_caps.sd3_bus_mode
168 |= SD_MODE_UHS_SDR50;
170 if (data & SDIO_UHS_SDR104)
171 card->sw_caps.sd3_bus_mode
172 |= SD_MODE_UHS_SDR104;
175 ret = mmc_io_rw_direct(card, 0, 0,
176 SDIO_CCCR_DRIVE_STRENGTH, 0, &data);
180 if (data & SDIO_DRIVE_SDTA)
181 card->sw_caps.sd3_drv_type |= SD_DRIVER_TYPE_A;
182 if (data & SDIO_DRIVE_SDTC)
183 card->sw_caps.sd3_drv_type |= SD_DRIVER_TYPE_C;
184 if (data & SDIO_DRIVE_SDTD)
185 card->sw_caps.sd3_drv_type |= SD_DRIVER_TYPE_D;
188 /* if no uhs mode ensure we check for high speed */
189 if (!card->sw_caps.sd3_bus_mode) {
190 if (speed & SDIO_SPEED_SHS) {
191 card->cccr.high_speed = 1;
192 card->sw_caps.hs_max_dtr = 50000000;
194 card->cccr.high_speed = 0;
195 card->sw_caps.hs_max_dtr = 25000000;
204 static int sdio_enable_wide(struct mmc_card *card)
209 if (!(card->host->caps & MMC_CAP_4_BIT_DATA))
212 if (card->cccr.low_speed && !card->cccr.wide_bus)
215 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl);
219 ctrl |= SDIO_BUS_WIDTH_4BIT;
221 ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL);
229 * If desired, disconnect the pull-up resistor on CD/DAT[3] (pin 1)
230 * of the card. This may be required on certain setups of boards,
231 * controllers and embedded sdio device which do not need the card's
232 * pull-up. As a result, card detection is disabled and power is saved.
234 static int sdio_disable_cd(struct mmc_card *card)
239 if (!mmc_card_disable_cd(card))
242 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl);
246 ctrl |= SDIO_BUS_CD_DISABLE;
248 return mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL);
252 * Devices that remain active during a system suspend are
253 * put back into 1-bit mode.
255 static int sdio_disable_wide(struct mmc_card *card)
260 if (!(card->host->caps & MMC_CAP_4_BIT_DATA))
263 if (card->cccr.low_speed && !card->cccr.wide_bus)
266 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl);
270 if (!(ctrl & SDIO_BUS_WIDTH_4BIT))
273 ctrl &= ~SDIO_BUS_WIDTH_4BIT;
274 ctrl |= SDIO_BUS_ASYNC_INT;
276 ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL);
280 mmc_set_bus_width(card->host, MMC_BUS_WIDTH_1);
286 static int sdio_enable_4bit_bus(struct mmc_card *card)
290 if (card->type == MMC_TYPE_SDIO)
291 return sdio_enable_wide(card);
293 if ((card->host->caps & MMC_CAP_4_BIT_DATA) &&
294 (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) {
295 err = mmc_app_set_bus_width(card, MMC_BUS_WIDTH_4);
301 err = sdio_enable_wide(card);
303 mmc_app_set_bus_width(card, MMC_BUS_WIDTH_1);
310 * Test if the card supports high-speed mode and, if so, switch to it.
312 static int mmc_sdio_switch_hs(struct mmc_card *card, int enable)
317 if (!(card->host->caps & MMC_CAP_SD_HIGHSPEED))
320 if (!card->cccr.high_speed)
323 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_SPEED, 0, &speed);
328 speed |= SDIO_SPEED_EHS;
330 speed &= ~SDIO_SPEED_EHS;
332 ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_SPEED, speed, NULL);
340 * Enable SDIO/combo card's high-speed mode. Return 0/1 if [not]supported.
342 static int sdio_enable_hs(struct mmc_card *card)
346 ret = mmc_sdio_switch_hs(card, true);
347 if (ret <= 0 || card->type == MMC_TYPE_SDIO)
350 ret = mmc_sd_switch_hs(card);
352 mmc_sdio_switch_hs(card, false);
357 static unsigned mmc_sdio_get_max_clock(struct mmc_card *card)
361 if (mmc_card_highspeed(card)) {
363 * The SDIO specification doesn't mention how
364 * the CIS transfer speed register relates to
365 * high-speed, but it seems that 50 MHz is
370 max_dtr = card->cis.max_dtr;
373 if (card->type == MMC_TYPE_SD_COMBO)
374 max_dtr = min(max_dtr, mmc_sd_get_max_clock(card));
379 static unsigned char host_drive_to_sdio_drive(int host_strength)
381 switch (host_strength) {
382 case MMC_SET_DRIVER_TYPE_A:
383 return SDIO_DTSx_SET_TYPE_A;
384 case MMC_SET_DRIVER_TYPE_B:
385 return SDIO_DTSx_SET_TYPE_B;
386 case MMC_SET_DRIVER_TYPE_C:
387 return SDIO_DTSx_SET_TYPE_C;
388 case MMC_SET_DRIVER_TYPE_D:
389 return SDIO_DTSx_SET_TYPE_D;
391 return SDIO_DTSx_SET_TYPE_B;
395 static void sdio_select_driver_type(struct mmc_card *card)
397 int host_drv_type = SD_DRIVER_TYPE_B;
398 int card_drv_type = SD_DRIVER_TYPE_B;
400 unsigned char card_strength;
404 * If the host doesn't support any of the Driver Types A,C or D,
405 * or there is no board specific handler then default Driver
408 if (!(card->host->caps &
409 (MMC_CAP_DRIVER_TYPE_A |
410 MMC_CAP_DRIVER_TYPE_C |
411 MMC_CAP_DRIVER_TYPE_D)))
414 if (!card->host->ops->select_drive_strength)
417 if (card->host->caps & MMC_CAP_DRIVER_TYPE_A)
418 host_drv_type |= SD_DRIVER_TYPE_A;
420 if (card->host->caps & MMC_CAP_DRIVER_TYPE_C)
421 host_drv_type |= SD_DRIVER_TYPE_C;
423 if (card->host->caps & MMC_CAP_DRIVER_TYPE_D)
424 host_drv_type |= SD_DRIVER_TYPE_D;
426 if (card->sw_caps.sd3_drv_type & SD_DRIVER_TYPE_A)
427 card_drv_type |= SD_DRIVER_TYPE_A;
429 if (card->sw_caps.sd3_drv_type & SD_DRIVER_TYPE_C)
430 card_drv_type |= SD_DRIVER_TYPE_C;
432 if (card->sw_caps.sd3_drv_type & SD_DRIVER_TYPE_D)
433 card_drv_type |= SD_DRIVER_TYPE_D;
436 * The drive strength that the hardware can support
437 * depends on the board design. Pass the appropriate
438 * information and let the hardware specific code
439 * return what is possible given the options
441 drive_strength = card->host->ops->select_drive_strength(
442 card->sw_caps.uhs_max_dtr,
443 host_drv_type, card_drv_type);
445 /* if error just use default for drive strength B */
446 err = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_DRIVE_STRENGTH, 0,
451 card_strength &= ~(SDIO_DRIVE_DTSx_MASK<<SDIO_DRIVE_DTSx_SHIFT);
452 card_strength |= host_drive_to_sdio_drive(drive_strength);
454 err = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_DRIVE_STRENGTH,
455 card_strength, NULL);
457 /* if error default to drive strength B */
459 mmc_set_driver_type(card->host, drive_strength);
463 static int sdio_set_bus_speed_mode(struct mmc_card *card)
465 unsigned int bus_speed, timing;
470 * If the host doesn't support any of the UHS-I modes, fallback on
473 if (!(card->host->caps & (MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 |
474 MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_DDR50)))
477 bus_speed = SDIO_SPEED_SDR12;
478 timing = MMC_TIMING_UHS_SDR12;
479 if ((card->host->caps & MMC_CAP_UHS_SDR104) &&
480 (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_SDR104)) {
481 bus_speed = SDIO_SPEED_SDR104;
482 timing = MMC_TIMING_UHS_SDR104;
483 card->sw_caps.uhs_max_dtr = UHS_SDR104_MAX_DTR;
484 } else if ((card->host->caps & MMC_CAP_UHS_DDR50) &&
485 (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_DDR50)) {
486 bus_speed = SDIO_SPEED_DDR50;
487 timing = MMC_TIMING_UHS_DDR50;
488 card->sw_caps.uhs_max_dtr = UHS_DDR50_MAX_DTR;
489 } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
490 MMC_CAP_UHS_SDR50)) && (card->sw_caps.sd3_bus_mode &
491 SD_MODE_UHS_SDR50)) {
492 bus_speed = SDIO_SPEED_SDR50;
493 timing = MMC_TIMING_UHS_SDR50;
494 card->sw_caps.uhs_max_dtr = UHS_SDR50_MAX_DTR;
495 } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
496 MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR25)) &&
497 (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_SDR25)) {
498 bus_speed = SDIO_SPEED_SDR25;
499 timing = MMC_TIMING_UHS_SDR25;
500 card->sw_caps.uhs_max_dtr = UHS_SDR25_MAX_DTR;
501 } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
502 MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR25 |
503 MMC_CAP_UHS_SDR12)) && (card->sw_caps.sd3_bus_mode &
504 SD_MODE_UHS_SDR12)) {
505 bus_speed = SDIO_SPEED_SDR12;
506 timing = MMC_TIMING_UHS_SDR12;
507 card->sw_caps.uhs_max_dtr = UHS_SDR12_MAX_DTR;
510 err = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_SPEED, 0, &speed);
514 speed &= ~SDIO_SPEED_BSS_MASK;
516 err = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_SPEED, speed, NULL);
521 mmc_set_timing(card->host, timing);
522 mmc_set_clock(card->host, card->sw_caps.uhs_max_dtr);
529 * UHS-I specific initialization procedure
531 static int mmc_sdio_init_uhs_card(struct mmc_card *card)
535 if (!card->scr.sda_spec3)
539 * Switch to wider bus (if supported).
541 if (card->host->caps & MMC_CAP_4_BIT_DATA) {
542 err = sdio_enable_4bit_bus(card);
544 mmc_set_bus_width(card->host, MMC_BUS_WIDTH_4);
549 /* Set the driver strength for the card */
550 sdio_select_driver_type(card);
552 /* Set bus speed mode of the card */
553 err = sdio_set_bus_speed_mode(card);
557 /* Initialize and start re-tuning timer */
558 if (!mmc_host_is_spi(card->host) && card->host->ops->execute_tuning)
559 err = card->host->ops->execute_tuning(card->host);
567 * Handle the detection and initialisation of a card.
569 * In the case of a resume, "oldcard" will contain the card
570 * we're trying to reinitialise.
572 static int mmc_sdio_init_card(struct mmc_host *host, u32 ocr,
573 struct mmc_card *oldcard, int powered_resume)
575 struct mmc_card *card;
579 WARN_ON(!host->claimed);
582 * Inform the card of the voltage
584 if (!powered_resume) {
585 err = mmc_send_io_op_cond(host, host->ocr, &ocr);
591 * For SPI, enable CRC as appropriate.
593 if (mmc_host_is_spi(host)) {
594 err = mmc_spi_set_crc(host, use_spi_crc);
600 * Allocate card structure.
602 card = mmc_alloc_card(host, NULL);
608 if ((ocr & R4_MEMORY_PRESENT) &&
609 mmc_sd_get_cid(host, host->ocr & ocr, card->raw_cid, NULL) == 0) {
610 card->type = MMC_TYPE_SD_COMBO;
612 if (oldcard && (oldcard->type != MMC_TYPE_SD_COMBO ||
613 memcmp(card->raw_cid, oldcard->raw_cid, sizeof(card->raw_cid)) != 0)) {
614 mmc_remove_card(card);
618 card->type = MMC_TYPE_SDIO;
620 if (oldcard && oldcard->type != MMC_TYPE_SDIO) {
621 mmc_remove_card(card);
627 * Call the optional HC's init_card function to handle quirks.
629 if (host->ops->init_card)
630 host->ops->init_card(host, card);
633 * If the host and card support UHS-I mode request the card
634 * to switch to 1.8V signaling level. No 1.8v signalling if
635 * UHS mode is not enabled to maintain compatibilty and some
636 * systems that claim 1.8v signalling in fact do not support
639 if ((ocr & R4_18V_PRESENT) &&
641 (MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 |
642 MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR104 |
643 MMC_CAP_UHS_DDR50))) {
644 err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_180,
647 ocr &= ~R4_18V_PRESENT;
648 host->ocr &= ~R4_18V_PRESENT;
652 ocr &= ~R4_18V_PRESENT;
653 host->ocr &= ~R4_18V_PRESENT;
657 * For native busses: set card RCA and quit open drain mode.
659 if (!powered_resume && !mmc_host_is_spi(host)) {
660 err = mmc_send_relative_addr(host, &card->rca);
665 * Update oldcard with the new RCA received from the SDIO
666 * device -- we're doing this so that it's updated in the
667 * "card" struct when oldcard overwrites that later.
670 oldcard->rca = card->rca;
674 * Read CSD, before selecting the card
676 if (!oldcard && card->type == MMC_TYPE_SD_COMBO) {
677 err = mmc_sd_get_csd(host, card);
681 mmc_decode_cid(card);
685 * Select card, as all following commands rely on that.
687 if (!powered_resume && !mmc_host_is_spi(host)) {
688 err = mmc_select_card(card);
693 if (card->quirks & MMC_QUIRK_NONSTD_SDIO) {
695 * This is non-standard SDIO device, meaning it doesn't
696 * have any CIA (Common I/O area) registers present.
697 * It's host's responsibility to fill cccr and cis
698 * structures in init_card().
700 mmc_set_clock(host, card->cis.max_dtr);
702 if (card->cccr.high_speed) {
703 mmc_card_set_highspeed(card);
704 mmc_set_timing(card->host, MMC_TIMING_SD_HS);
711 * Read the common registers.
713 err = sdio_read_cccr(card);
718 * Read the common CIS tuples.
720 err = sdio_read_common_cis(card);
725 int same = (card->cis.vendor == oldcard->cis.vendor &&
726 card->cis.device == oldcard->cis.device);
727 mmc_remove_card(card);
733 mmc_fixup_device(card, NULL);
735 if (card->type == MMC_TYPE_SD_COMBO) {
736 err = mmc_sd_setup_card(host, card, oldcard != NULL);
737 /* handle as SDIO-only card if memory init failed */
740 if (mmc_host_is_spi(host))
741 /* should not fail, as it worked previously */
742 mmc_spi_set_crc(host, use_spi_crc);
743 card->type = MMC_TYPE_SDIO;
745 card->dev.type = &sd_type;
749 * If needed, disconnect card detection pull-up resistor.
751 err = sdio_disable_cd(card);
755 /* Initialization sequence for UHS-I cards */
756 /* Only if card supports 1.8v and UHS signaling */
757 if ((ocr & R4_18V_PRESENT) && card->sw_caps.sd3_bus_mode) {
758 err = mmc_sdio_init_uhs_card(card);
762 /* Card is an ultra-high-speed card */
763 mmc_card_set_uhs(card);
766 * Switch to high-speed (if supported).
768 err = sdio_enable_hs(card);
770 mmc_sd_go_highspeed(card);
775 * Change to the card's maximum speed.
777 mmc_set_clock(host, mmc_sdio_get_max_clock(card));
780 * Switch to wider bus (if supported).
782 err = sdio_enable_4bit_bus(card);
784 mmc_set_bus_width(card->host, MMC_BUS_WIDTH_4);
795 mmc_remove_card(card);
802 * Host is being removed. Free up the current card.
804 static void mmc_sdio_remove(struct mmc_host *host)
811 for (i = 0;i < host->card->sdio_funcs;i++) {
812 if (host->card->sdio_func[i]) {
813 sdio_remove_func(host->card->sdio_func[i]);
814 host->card->sdio_func[i] = NULL;
818 mmc_remove_card(host->card);
823 * Card detection - card is alive.
825 static int mmc_sdio_alive(struct mmc_host *host)
827 return mmc_select_card(host->card);
831 * Card detection callback from host.
833 static void mmc_sdio_detect(struct mmc_host *host)
840 /* Make sure card is powered before detecting it */
841 if (host->caps & MMC_CAP_POWER_OFF_CARD) {
842 err = pm_runtime_get_sync(&host->card->dev);
847 mmc_claim_host(host);
850 * Just check if our card has been removed.
852 err = _mmc_detect_card_removed(host);
854 mmc_release_host(host);
857 * Tell PM core it's OK to power off the card now.
859 * The _sync variant is used in order to ensure that the card
860 * is left powered off in case an error occurred, and the card
861 * is going to be removed.
863 * Since there is no specific reason to believe a new user
864 * is about to show up at this point, the _sync variant is
867 if (host->caps & MMC_CAP_POWER_OFF_CARD)
868 pm_runtime_put_sync(&host->card->dev);
872 mmc_sdio_remove(host);
874 mmc_claim_host(host);
875 mmc_detach_bus(host);
877 mmc_release_host(host);
882 * SDIO suspend. We need to suspend all functions separately.
883 * Therefore all registered functions must have drivers with suspend
884 * and resume methods. Failing that we simply remove the whole card.
886 static int mmc_sdio_suspend(struct mmc_host *host)
890 for (i = 0; i < host->card->sdio_funcs; i++) {
891 struct sdio_func *func = host->card->sdio_func[i];
892 if (func && sdio_func_present(func) && func->dev.driver) {
893 const struct dev_pm_ops *pmops = func->dev.driver->pm;
894 if (!pmops || !pmops->suspend || !pmops->resume) {
895 /* force removal of entire card in that case */
898 err = pmops->suspend(&func->dev);
903 while (err && --i >= 0) {
904 struct sdio_func *func = host->card->sdio_func[i];
905 if (func && sdio_func_present(func) && func->dev.driver) {
906 const struct dev_pm_ops *pmops = func->dev.driver->pm;
907 pmops->resume(&func->dev);
911 if (!err && mmc_card_keep_power(host) && mmc_card_wake_sdio_irq(host)) {
912 mmc_claim_host(host);
913 sdio_disable_wide(host->card);
914 mmc_release_host(host);
920 static int mmc_sdio_resume(struct mmc_host *host)
927 /* Basic card reinitialization. */
928 mmc_claim_host(host);
930 /* No need to reinitialize powered-resumed nonremovable cards */
931 if (mmc_card_is_removable(host) || !mmc_card_keep_power(host))
932 err = mmc_sdio_init_card(host, host->ocr, host->card,
933 mmc_card_keep_power(host));
934 else if (mmc_card_keep_power(host) && mmc_card_wake_sdio_irq(host)) {
935 /* We may have switched to 1-bit mode during suspend */
936 err = sdio_enable_4bit_bus(host->card);
938 mmc_set_bus_width(host, MMC_BUS_WIDTH_4);
943 if (!err && host->sdio_irqs)
944 mmc_signal_sdio_irq(host);
945 mmc_release_host(host);
948 * If the card looked to be the same as before suspending, then
949 * we proceed to resume all card functions. If one of them returns
950 * an error then we simply return that error to the core and the
951 * card will be redetected as new. It is the responsibility of
952 * the function driver to perform further tests with the extra
953 * knowledge it has of the card to confirm the card is indeed the
954 * same as before suspending (same MAC address for network cards,
955 * etc.) and return an error otherwise.
957 for (i = 0; !err && i < host->card->sdio_funcs; i++) {
958 struct sdio_func *func = host->card->sdio_func[i];
959 if (func && sdio_func_present(func) && func->dev.driver) {
960 const struct dev_pm_ops *pmops = func->dev.driver->pm;
961 err = pmops->resume(&func->dev);
968 static int mmc_sdio_power_restore(struct mmc_host *host)
976 mmc_claim_host(host);
979 * Reset the card by performing the same steps that are taken by
980 * mmc_rescan_try_freq() and mmc_attach_sdio() during a "normal" probe.
982 * sdio_reset() is technically not needed. Having just powered up the
983 * hardware, it should already be in reset state. However, some
984 * platforms (such as SD8686 on OLPC) do not instantly cut power,
985 * meaning that a reset is required when restoring power soon after
986 * powering off. It is harmless in other cases.
988 * The CMD5 reset (mmc_send_io_op_cond()), according to the SDIO spec,
989 * is not necessary for non-removable cards. However, it is required
990 * for OLPC SD8686 (which expects a [CMD5,5,3,7] init sequence), and
991 * harmless in other situations.
993 * With these steps taken, mmc_select_voltage() is also required to
994 * restore the correct voltage setting of the card.
998 mmc_send_if_cond(host, host->ocr_avail);
1000 ret = mmc_send_io_op_cond(host, 0, &ocr);
1004 if (host->ocr_avail_sdio)
1005 host->ocr_avail = host->ocr_avail_sdio;
1007 host->ocr = mmc_select_voltage(host, ocr & ~0x7F);
1013 ret = mmc_sdio_init_card(host, host->ocr, host->card,
1014 mmc_card_keep_power(host));
1015 if (!ret && host->sdio_irqs)
1016 mmc_signal_sdio_irq(host);
1019 mmc_release_host(host);
1024 static const struct mmc_bus_ops mmc_sdio_ops = {
1025 .remove = mmc_sdio_remove,
1026 .detect = mmc_sdio_detect,
1027 .suspend = mmc_sdio_suspend,
1028 .resume = mmc_sdio_resume,
1029 .power_restore = mmc_sdio_power_restore,
1030 .alive = mmc_sdio_alive,
1035 * Starting point for SDIO card init.
1037 int mmc_attach_sdio(struct mmc_host *host)
1041 struct mmc_card *card;
1044 WARN_ON(!host->claimed);
1046 err = mmc_send_io_op_cond(host, 0, &ocr);
1050 mmc_attach_bus(host, &mmc_sdio_ops);
1051 if (host->ocr_avail_sdio)
1052 host->ocr_avail = host->ocr_avail_sdio;
1055 * Sanity check the voltages that the card claims to
1059 pr_warning("%s: card claims to support voltages "
1060 "below the defined range. These will be ignored.\n",
1061 mmc_hostname(host));
1065 host->ocr = mmc_select_voltage(host, ocr);
1068 * Can we support the voltage(s) of the card(s)?
1076 * Detect and init the card.
1078 err = mmc_sdio_init_card(host, host->ocr, NULL, 0);
1080 if (err == -EAGAIN) {
1082 * Retry initialization with S18R set to 0.
1084 host->ocr &= ~R4_18V_PRESENT;
1085 err = mmc_sdio_init_card(host, host->ocr, NULL, 0);
1093 * Enable runtime PM only if supported by host+card+board
1095 if (host->caps & MMC_CAP_POWER_OFF_CARD) {
1097 * Let runtime PM core know our card is active
1099 err = pm_runtime_set_active(&card->dev);
1104 * Enable runtime PM for this card
1106 pm_runtime_enable(&card->dev);
1110 * The number of functions on the card is encoded inside
1113 funcs = (ocr & 0x70000000) >> 28;
1114 card->sdio_funcs = 0;
1117 * Initialize (but don't add) all present functions.
1119 for (i = 0; i < funcs; i++, card->sdio_funcs++) {
1120 err = sdio_init_func(host->card, i + 1);
1125 * Enable Runtime PM for this func (if supported)
1127 if (host->caps & MMC_CAP_POWER_OFF_CARD)
1128 pm_runtime_enable(&card->sdio_func[i]->dev);
1132 * First add the card to the driver model...
1134 mmc_release_host(host);
1135 err = mmc_add_card(host->card);
1140 * ...then the SDIO functions.
1142 for (i = 0;i < funcs;i++) {
1143 err = sdio_add_func(host->card->sdio_func[i]);
1148 mmc_claim_host(host);
1153 /* Remove without lock if the device has been added. */
1154 mmc_sdio_remove(host);
1155 mmc_claim_host(host);
1157 /* And with lock if it hasn't been added. */
1158 mmc_release_host(host);
1160 mmc_sdio_remove(host);
1161 mmc_claim_host(host);
1163 mmc_detach_bus(host);
1165 pr_err("%s: error %d whilst initialising SDIO card\n",
1166 mmc_hostname(host), err);