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/module.h>
14 #include <linux/pm_runtime.h>
16 #include <linux/mmc/host.h>
17 #include <linux/mmc/card.h>
18 #include <linux/mmc/mmc.h>
19 #include <linux/mmc/sdio.h>
20 #include <linux/mmc/sdio_func.h>
21 #include <linux/mmc/sdio_ids.h>
33 #ifdef CONFIG_MMC_EMBEDDED_SDIO
34 #include <linux/mmc/sdio_ids.h>
37 static int sdio_read_fbr(struct sdio_func *func)
42 if (mmc_card_nonstd_func_interface(func->card)) {
43 func->class = SDIO_CLASS_NONE;
47 ret = mmc_io_rw_direct(func->card, 0, 0,
48 SDIO_FBR_BASE(func->num) + SDIO_FBR_STD_IF, 0, &data);
55 ret = mmc_io_rw_direct(func->card, 0, 0,
56 SDIO_FBR_BASE(func->num) + SDIO_FBR_STD_IF_EXT, 0, &data);
67 static int sdio_init_func(struct mmc_card *card, unsigned int fn)
70 struct sdio_func *func;
72 BUG_ON(fn > SDIO_MAX_FUNCS);
74 func = sdio_alloc_func(card);
80 if (!(card->quirks & MMC_QUIRK_NONSTD_SDIO)) {
81 ret = sdio_read_fbr(func);
85 ret = sdio_read_func_cis(func);
89 func->vendor = func->card->cis.vendor;
90 func->device = func->card->cis.device;
91 func->max_blksize = func->card->cis.blksize;
94 card->sdio_func[fn - 1] = func;
100 * It is okay to remove the function here even though we hold
101 * the host lock as we haven't registered the device yet.
103 sdio_remove_func(func);
107 static int sdio_read_cccr(struct mmc_card *card, u32 ocr)
111 int uhs = ocr & R4_18V_PRESENT;
115 memset(&card->cccr, 0, sizeof(struct sdio_cccr));
117 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_CCCR, 0, &data);
121 cccr_vsn = data & 0x0f;
123 if (cccr_vsn > SDIO_CCCR_REV_3_00) {
124 pr_err("%s: unrecognised CCCR structure version %d\n",
125 mmc_hostname(card->host), cccr_vsn);
129 card->cccr.sdio_vsn = (data & 0xf0) >> 4;
131 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_CAPS, 0, &data);
135 if (data & SDIO_CCCR_CAP_SMB)
136 card->cccr.multi_block = 1;
137 if (data & SDIO_CCCR_CAP_LSC)
138 card->cccr.low_speed = 1;
139 if (data & SDIO_CCCR_CAP_4BLS)
140 card->cccr.wide_bus = 1;
142 if (cccr_vsn >= SDIO_CCCR_REV_1_10) {
143 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_POWER, 0, &data);
147 if (data & SDIO_POWER_SMPC)
148 card->cccr.high_power = 1;
151 if (cccr_vsn >= SDIO_CCCR_REV_1_20) {
152 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_SPEED, 0, &speed);
156 card->scr.sda_spec3 = 0;
157 card->sw_caps.sd3_bus_mode = 0;
158 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 no uhs mode ensure we check for high speed */
194 if (!card->sw_caps.sd3_bus_mode) {
195 if (speed & SDIO_SPEED_SHS) {
196 card->cccr.high_speed = 1;
197 card->sw_caps.hs_max_dtr = 50000000;
199 card->cccr.high_speed = 0;
200 card->sw_caps.hs_max_dtr = 25000000;
209 static int sdio_enable_wide(struct mmc_card *card)
214 if (!(card->host->caps & MMC_CAP_4_BIT_DATA))
217 if (card->cccr.low_speed && !card->cccr.wide_bus)
220 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl);
224 if ((ctrl & SDIO_BUS_WIDTH_MASK) == SDIO_BUS_WIDTH_RESERVED)
225 pr_warn("%s: SDIO_CCCR_IF is invalid: 0x%02x\n",
226 mmc_hostname(card->host), ctrl);
228 /* set as 4-bit bus width */
229 ctrl &= ~SDIO_BUS_WIDTH_MASK;
230 ctrl |= SDIO_BUS_WIDTH_4BIT;
232 ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL);
240 * If desired, disconnect the pull-up resistor on CD/DAT[3] (pin 1)
241 * of the card. This may be required on certain setups of boards,
242 * controllers and embedded sdio device which do not need the card's
243 * pull-up. As a result, card detection is disabled and power is saved.
245 static int sdio_disable_cd(struct mmc_card *card)
250 if (!mmc_card_disable_cd(card))
253 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl);
257 ctrl |= SDIO_BUS_CD_DISABLE;
259 return mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL);
263 * Devices that remain active during a system suspend are
264 * put back into 1-bit mode.
266 static int sdio_disable_wide(struct mmc_card *card)
271 if (!(card->host->caps & MMC_CAP_4_BIT_DATA))
274 if (card->cccr.low_speed && !card->cccr.wide_bus)
277 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl);
281 if (!(ctrl & SDIO_BUS_WIDTH_4BIT))
284 ctrl &= ~SDIO_BUS_WIDTH_4BIT;
285 ctrl |= SDIO_BUS_ASYNC_INT;
287 ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL);
291 mmc_set_bus_width(card->host, MMC_BUS_WIDTH_1);
297 static int sdio_enable_4bit_bus(struct mmc_card *card)
301 if (card->type == MMC_TYPE_SDIO)
302 err = sdio_enable_wide(card);
303 else 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);
308 err = sdio_enable_wide(card);
310 mmc_app_set_bus_width(card, MMC_BUS_WIDTH_1);
315 mmc_set_bus_width(card->host, MMC_BUS_WIDTH_4);
324 * Test if the card supports high-speed mode and, if so, switch to it.
326 static int mmc_sdio_switch_hs(struct mmc_card *card, int enable)
331 if (!(card->host->caps & MMC_CAP_SD_HIGHSPEED))
334 if (!card->cccr.high_speed)
337 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_SPEED, 0, &speed);
342 speed |= SDIO_SPEED_EHS;
344 speed &= ~SDIO_SPEED_EHS;
346 ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_SPEED, speed, NULL);
354 * Enable SDIO/combo card's high-speed mode. Return 0/1 if [not]supported.
356 static int sdio_enable_hs(struct mmc_card *card)
360 ret = mmc_sdio_switch_hs(card, true);
361 if (ret <= 0 || card->type == MMC_TYPE_SDIO)
364 ret = mmc_sd_switch_hs(card);
366 mmc_sdio_switch_hs(card, false);
371 static unsigned mmc_sdio_get_max_clock(struct mmc_card *card)
375 if (mmc_card_hs(card)) {
377 * The SDIO specification doesn't mention how
378 * the CIS transfer speed register relates to
379 * high-speed, but it seems that 50 MHz is
384 max_dtr = card->cis.max_dtr;
387 if (card->type == MMC_TYPE_SD_COMBO)
388 max_dtr = min(max_dtr, mmc_sd_get_max_clock(card));
393 static unsigned char host_drive_to_sdio_drive(int host_strength)
395 switch (host_strength) {
396 case MMC_SET_DRIVER_TYPE_A:
397 return SDIO_DTSx_SET_TYPE_A;
398 case MMC_SET_DRIVER_TYPE_B:
399 return SDIO_DTSx_SET_TYPE_B;
400 case MMC_SET_DRIVER_TYPE_C:
401 return SDIO_DTSx_SET_TYPE_C;
402 case MMC_SET_DRIVER_TYPE_D:
403 return SDIO_DTSx_SET_TYPE_D;
405 return SDIO_DTSx_SET_TYPE_B;
409 static void sdio_select_driver_type(struct mmc_card *card)
411 int card_drv_type, drive_strength, drv_type;
412 unsigned char card_strength;
415 card->drive_strength = 0;
417 card_drv_type = card->sw_caps.sd3_drv_type | SD_DRIVER_TYPE_B;
419 drive_strength = mmc_select_drive_strength(card,
420 card->sw_caps.uhs_max_dtr,
421 card_drv_type, &drv_type);
423 if (drive_strength) {
424 /* if error just use default for drive strength B */
425 err = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_DRIVE_STRENGTH, 0,
430 card_strength &= ~(SDIO_DRIVE_DTSx_MASK<<SDIO_DRIVE_DTSx_SHIFT);
431 card_strength |= host_drive_to_sdio_drive(drive_strength);
433 /* if error default to drive strength B */
434 err = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_DRIVE_STRENGTH,
435 card_strength, NULL);
438 card->drive_strength = drive_strength;
442 mmc_set_driver_type(card->host, drv_type);
446 static int sdio_set_bus_speed_mode(struct mmc_card *card)
448 unsigned int bus_speed, timing;
453 * If the host doesn't support any of the UHS-I modes, fallback on
456 if (!mmc_host_uhs(card->host))
459 bus_speed = SDIO_SPEED_SDR12;
460 timing = MMC_TIMING_UHS_SDR12;
461 if ((card->host->caps & MMC_CAP_UHS_SDR104) &&
462 (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_SDR104)) {
463 bus_speed = SDIO_SPEED_SDR104;
464 timing = MMC_TIMING_UHS_SDR104;
465 card->sw_caps.uhs_max_dtr = UHS_SDR104_MAX_DTR;
466 card->sd_bus_speed = UHS_SDR104_BUS_SPEED;
467 } else if ((card->host->caps & MMC_CAP_UHS_DDR50) &&
468 (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_DDR50)) {
469 bus_speed = SDIO_SPEED_DDR50;
470 timing = MMC_TIMING_UHS_DDR50;
471 card->sw_caps.uhs_max_dtr = UHS_DDR50_MAX_DTR;
472 card->sd_bus_speed = UHS_DDR50_BUS_SPEED;
473 } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
474 MMC_CAP_UHS_SDR50)) && (card->sw_caps.sd3_bus_mode &
475 SD_MODE_UHS_SDR50)) {
476 bus_speed = SDIO_SPEED_SDR50;
477 timing = MMC_TIMING_UHS_SDR50;
478 card->sw_caps.uhs_max_dtr = UHS_SDR50_MAX_DTR;
479 card->sd_bus_speed = UHS_SDR50_BUS_SPEED;
480 } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
481 MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR25)) &&
482 (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_SDR25)) {
483 bus_speed = SDIO_SPEED_SDR25;
484 timing = MMC_TIMING_UHS_SDR25;
485 card->sw_caps.uhs_max_dtr = UHS_SDR25_MAX_DTR;
486 card->sd_bus_speed = UHS_SDR25_BUS_SPEED;
487 } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
488 MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR25 |
489 MMC_CAP_UHS_SDR12)) && (card->sw_caps.sd3_bus_mode &
490 SD_MODE_UHS_SDR12)) {
491 bus_speed = SDIO_SPEED_SDR12;
492 timing = MMC_TIMING_UHS_SDR12;
493 card->sw_caps.uhs_max_dtr = UHS_SDR12_MAX_DTR;
494 card->sd_bus_speed = UHS_SDR12_BUS_SPEED;
497 err = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_SPEED, 0, &speed);
501 speed &= ~SDIO_SPEED_BSS_MASK;
503 err = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_SPEED, speed, NULL);
508 mmc_set_timing(card->host, timing);
509 mmc_set_clock(card->host, card->sw_caps.uhs_max_dtr);
516 * UHS-I specific initialization procedure
518 static int mmc_sdio_init_uhs_card(struct mmc_card *card)
522 if (!card->scr.sda_spec3)
526 * Switch to wider bus (if supported).
528 if (card->host->caps & MMC_CAP_4_BIT_DATA)
529 err = sdio_enable_4bit_bus(card);
531 /* Set the driver strength for the card */
532 sdio_select_driver_type(card);
534 /* Set bus speed mode of the card */
535 err = sdio_set_bus_speed_mode(card);
540 * SPI mode doesn't define CMD19 and tuning is only valid for SDR50 and
541 * SDR104 mode SD-cards. Note that tuning is mandatory for SDR104.
543 if (!mmc_host_is_spi(card->host) &&
544 ((card->host->ios.timing == MMC_TIMING_UHS_SDR50) ||
545 (card->host->ios.timing == MMC_TIMING_UHS_SDR104)))
546 err = mmc_execute_tuning(card);
552 * Handle the detection and initialisation of a card.
554 * In the case of a resume, "oldcard" will contain the card
555 * we're trying to reinitialise.
557 static int mmc_sdio_init_card(struct mmc_host *host, u32 ocr,
558 struct mmc_card *oldcard, int powered_resume)
560 struct mmc_card *card;
567 WARN_ON(!host->claimed);
569 /* to query card if 1.8V signalling is supported */
570 if (mmc_host_uhs(host))
571 ocr |= R4_18V_PRESENT;
575 pr_warn("%s: Skipping voltage switch\n", mmc_hostname(host));
576 ocr &= ~R4_18V_PRESENT;
580 * Inform the card of the voltage
582 if (!powered_resume) {
583 err = mmc_send_io_op_cond(host, ocr, &rocr);
589 * For SPI, enable CRC as appropriate.
591 if (mmc_host_is_spi(host)) {
592 err = mmc_spi_set_crc(host, use_spi_crc);
598 * Allocate card structure.
600 card = mmc_alloc_card(host, NULL);
606 if ((rocr & R4_MEMORY_PRESENT) &&
607 mmc_sd_get_cid(host, ocr & rocr, card->raw_cid, NULL) == 0) {
608 card->type = MMC_TYPE_SD_COMBO;
610 if (oldcard && (oldcard->type != MMC_TYPE_SD_COMBO ||
611 memcmp(card->raw_cid, oldcard->raw_cid, sizeof(card->raw_cid)) != 0)) {
612 mmc_remove_card(card);
616 card->type = MMC_TYPE_SDIO;
618 if (oldcard && oldcard->type != MMC_TYPE_SDIO) {
619 mmc_remove_card(card);
625 * Call the optional HC's init_card function to handle quirks.
627 if (host->ops->init_card)
628 host->ops->init_card(host, card);
631 * If the host and card support UHS-I mode request the card
632 * to switch to 1.8V signaling level. No 1.8v signalling if
633 * UHS mode is not enabled to maintain compatibility and some
634 * systems that claim 1.8v signalling in fact do not support
637 if (!powered_resume && (rocr & ocr & R4_18V_PRESENT)) {
638 err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_180,
640 if (err == -EAGAIN) {
643 mmc_send_if_cond(host, host->ocr_avail);
644 mmc_remove_card(card);
648 ocr &= ~R4_18V_PRESENT;
652 ocr &= ~R4_18V_PRESENT;
656 * For native busses: set card RCA and quit open drain mode.
658 if (!powered_resume && !mmc_host_is_spi(host)) {
659 err = mmc_send_relative_addr(host, &card->rca);
664 * Update oldcard with the new RCA received from the SDIO
665 * device -- we're doing this so that it's updated in the
666 * "card" struct when oldcard overwrites that later.
669 oldcard->rca = card->rca;
673 * Read CSD, before selecting the card
675 if (!oldcard && card->type == MMC_TYPE_SD_COMBO) {
676 err = mmc_sd_get_csd(host, card);
680 mmc_decode_cid(card);
684 * Select card, as all following commands rely on that.
686 if (!powered_resume && !mmc_host_is_spi(host)) {
687 err = mmc_select_card(card);
692 if (card->quirks & MMC_QUIRK_NONSTD_SDIO) {
694 * This is non-standard SDIO device, meaning it doesn't
695 * have any CIA (Common I/O area) registers present.
696 * It's host's responsibility to fill cccr and cis
697 * structures in init_card().
699 mmc_set_clock(host, card->cis.max_dtr);
701 if (card->cccr.high_speed) {
702 mmc_set_timing(card->host, MMC_TIMING_SD_HS);
708 #ifdef CONFIG_MMC_EMBEDDED_SDIO
709 if (host->embedded_sdio_data.cccr)
710 memcpy(&card->cccr, host->embedded_sdio_data.cccr, sizeof(struct sdio_cccr));
714 * Read the common registers.
716 err = sdio_read_cccr(card, ocr);
719 #ifdef CONFIG_MMC_EMBEDDED_SDIO
723 #ifdef CONFIG_MMC_EMBEDDED_SDIO
724 if (host->embedded_sdio_data.cis)
725 memcpy(&card->cis, host->embedded_sdio_data.cis, sizeof(struct sdio_cis));
729 * Read the common CIS tuples.
731 err = sdio_read_common_cis(card);
734 #ifdef CONFIG_MMC_EMBEDDED_SDIO
739 int same = (card->cis.vendor == oldcard->cis.vendor &&
740 card->cis.device == oldcard->cis.device);
741 mmc_remove_card(card);
747 card->ocr = ocr_card;
748 mmc_fixup_device(card, NULL);
750 if (card->type == MMC_TYPE_SD_COMBO) {
751 err = mmc_sd_setup_card(host, card, oldcard != NULL);
752 /* handle as SDIO-only card if memory init failed */
755 if (mmc_host_is_spi(host))
756 /* should not fail, as it worked previously */
757 mmc_spi_set_crc(host, use_spi_crc);
758 card->type = MMC_TYPE_SDIO;
760 card->dev.type = &sd_type;
764 * If needed, disconnect card detection pull-up resistor.
766 err = sdio_disable_cd(card);
770 /* Initialization sequence for UHS-I cards */
771 /* Only if card supports 1.8v and UHS signaling */
772 if ((ocr & R4_18V_PRESENT) && card->sw_caps.sd3_bus_mode) {
773 err = mmc_sdio_init_uhs_card(card);
778 * Switch to high-speed (if supported).
780 err = sdio_enable_hs(card);
782 mmc_set_timing(card->host, MMC_TIMING_SD_HS);
787 * Change to the card's maximum speed.
789 mmc_set_clock(host, mmc_sdio_get_max_clock(card));
792 * Switch to wider bus (if supported).
794 err = sdio_enable_4bit_bus(card);
805 mmc_remove_card(card);
812 * Host is being removed. Free up the current card.
814 static void mmc_sdio_remove(struct mmc_host *host)
821 for (i = 0;i < host->card->sdio_funcs;i++) {
822 if (host->card->sdio_func[i]) {
823 sdio_remove_func(host->card->sdio_func[i]);
824 host->card->sdio_func[i] = NULL;
828 mmc_remove_card(host->card);
833 * Card detection - card is alive.
835 static int mmc_sdio_alive(struct mmc_host *host)
837 return mmc_select_card(host->card);
841 * Card detection callback from host.
843 static void mmc_sdio_detect(struct mmc_host *host)
850 /* Make sure card is powered before detecting it */
851 if (host->caps & MMC_CAP_POWER_OFF_CARD) {
852 err = pm_runtime_get_sync(&host->card->dev);
854 pm_runtime_put_noidle(&host->card->dev);
859 mmc_claim_host(host);
862 * Just check if our card has been removed.
864 err = _mmc_detect_card_removed(host);
866 mmc_release_host(host);
869 * Tell PM core it's OK to power off the card now.
871 * The _sync variant is used in order to ensure that the card
872 * is left powered off in case an error occurred, and the card
873 * is going to be removed.
875 * Since there is no specific reason to believe a new user
876 * is about to show up at this point, the _sync variant is
879 if (host->caps & MMC_CAP_POWER_OFF_CARD)
880 pm_runtime_put_sync(&host->card->dev);
884 mmc_sdio_remove(host);
886 mmc_claim_host(host);
887 mmc_detach_bus(host);
889 mmc_release_host(host);
894 * SDIO pre_suspend. We need to suspend all functions separately.
895 * Therefore all registered functions must have drivers with suspend
896 * and resume methods. Failing that we simply remove the whole card.
898 static int mmc_sdio_pre_suspend(struct mmc_host *host)
902 for (i = 0; i < host->card->sdio_funcs; i++) {
903 struct sdio_func *func = host->card->sdio_func[i];
904 if (func && sdio_func_present(func) && func->dev.driver) {
905 const struct dev_pm_ops *pmops = func->dev.driver->pm;
906 if (!pmops || !pmops->suspend || !pmops->resume) {
907 /* force removal of entire card in that case */
918 * SDIO suspend. Suspend all functions separately.
920 static int mmc_sdio_suspend(struct mmc_host *host)
922 mmc_claim_host(host);
924 if (mmc_card_keep_power(host) && mmc_card_wake_sdio_irq(host))
925 sdio_disable_wide(host->card);
927 if (!mmc_card_keep_power(host)) {
929 } else if (host->retune_period) {
930 mmc_retune_timer_stop(host);
931 mmc_retune_needed(host);
934 mmc_release_host(host);
939 static int mmc_sdio_resume(struct mmc_host *host)
946 /* Basic card reinitialization. */
947 mmc_claim_host(host);
949 /* Restore power if needed */
950 if (!mmc_card_keep_power(host)) {
951 mmc_power_up(host, host->card->ocr);
953 * Tell runtime PM core we just powered up the card,
954 * since it still believes the card is powered off.
955 * Note that currently runtime PM is only enabled
956 * for SDIO cards that are MMC_CAP_POWER_OFF_CARD
958 if (host->caps & MMC_CAP_POWER_OFF_CARD) {
959 pm_runtime_disable(&host->card->dev);
960 pm_runtime_set_active(&host->card->dev);
961 pm_runtime_enable(&host->card->dev);
965 /* No need to reinitialize powered-resumed nonremovable cards */
966 if (mmc_card_is_removable(host) || !mmc_card_keep_power(host)) {
969 mmc_send_if_cond(host, host->card->ocr);
970 err = mmc_send_io_op_cond(host, 0, NULL);
972 err = mmc_sdio_init_card(host, host->card->ocr,
974 mmc_card_keep_power(host));
975 } else if (mmc_card_keep_power(host) && mmc_card_wake_sdio_irq(host)) {
976 /* We may have switched to 1-bit mode during suspend */
977 err = sdio_enable_4bit_bus(host->card);
980 if (!err && host->sdio_irqs) {
981 if (!(host->caps2 & MMC_CAP2_SDIO_IRQ_NOTHREAD))
982 wake_up_process(host->sdio_irq_thread);
983 else if (host->caps & MMC_CAP_SDIO_IRQ)
984 host->ops->enable_sdio_irq(host, 1);
987 mmc_release_host(host);
989 host->pm_flags &= ~MMC_PM_KEEP_POWER;
993 static int mmc_sdio_power_restore(struct mmc_host *host)
1000 mmc_claim_host(host);
1003 * Reset the card by performing the same steps that are taken by
1004 * mmc_rescan_try_freq() and mmc_attach_sdio() during a "normal" probe.
1006 * sdio_reset() is technically not needed. Having just powered up the
1007 * hardware, it should already be in reset state. However, some
1008 * platforms (such as SD8686 on OLPC) do not instantly cut power,
1009 * meaning that a reset is required when restoring power soon after
1010 * powering off. It is harmless in other cases.
1012 * The CMD5 reset (mmc_send_io_op_cond()), according to the SDIO spec,
1013 * is not necessary for non-removable cards. However, it is required
1014 * for OLPC SD8686 (which expects a [CMD5,5,3,7] init sequence), and
1015 * harmless in other situations.
1021 mmc_send_if_cond(host, host->card->ocr);
1023 ret = mmc_send_io_op_cond(host, 0, NULL);
1027 ret = mmc_sdio_init_card(host, host->card->ocr, host->card,
1028 mmc_card_keep_power(host));
1029 if (!ret && host->sdio_irqs)
1030 mmc_signal_sdio_irq(host);
1033 mmc_release_host(host);
1038 static int mmc_sdio_runtime_suspend(struct mmc_host *host)
1040 /* No references to the card, cut the power to it. */
1041 mmc_claim_host(host);
1042 mmc_power_off(host);
1043 mmc_release_host(host);
1048 static int mmc_sdio_runtime_resume(struct mmc_host *host)
1052 /* Restore power and re-initialize. */
1053 mmc_claim_host(host);
1054 mmc_power_up(host, host->card->ocr);
1055 ret = mmc_sdio_power_restore(host);
1056 mmc_release_host(host);
1061 static int mmc_sdio_reset(struct mmc_host *host)
1063 mmc_power_cycle(host, host->card->ocr);
1064 return mmc_sdio_power_restore(host);
1067 static const struct mmc_bus_ops mmc_sdio_ops = {
1068 .remove = mmc_sdio_remove,
1069 .detect = mmc_sdio_detect,
1070 .pre_suspend = mmc_sdio_pre_suspend,
1071 .suspend = mmc_sdio_suspend,
1072 .resume = mmc_sdio_resume,
1073 .runtime_suspend = mmc_sdio_runtime_suspend,
1074 .runtime_resume = mmc_sdio_runtime_resume,
1075 .power_restore = mmc_sdio_power_restore,
1076 .alive = mmc_sdio_alive,
1077 .reset = mmc_sdio_reset,
1082 * Starting point for SDIO card init.
1084 int mmc_attach_sdio(struct mmc_host *host)
1088 struct mmc_card *card;
1091 WARN_ON(!host->claimed);
1093 err = mmc_send_io_op_cond(host, 0, &ocr);
1097 mmc_attach_bus(host, &mmc_sdio_ops);
1098 if (host->ocr_avail_sdio)
1099 host->ocr_avail = host->ocr_avail_sdio;
1102 rocr = mmc_select_voltage(host, ocr);
1105 * Can we support the voltage(s) of the card(s)?
1113 * Detect and init the card.
1115 err = mmc_sdio_init_card(host, rocr, NULL, 0);
1122 * Enable runtime PM only if supported by host+card+board
1124 if (host->caps & MMC_CAP_POWER_OFF_CARD) {
1126 * Let runtime PM core know our card is active
1128 err = pm_runtime_set_active(&card->dev);
1133 * Enable runtime PM for this card
1135 pm_runtime_enable(&card->dev);
1139 * The number of functions on the card is encoded inside
1142 funcs = (ocr & 0x70000000) >> 28;
1143 card->sdio_funcs = 0;
1145 #ifdef CONFIG_MMC_EMBEDDED_SDIO
1146 if (host->embedded_sdio_data.funcs)
1147 card->sdio_funcs = funcs = host->embedded_sdio_data.num_funcs;
1151 * Initialize (but don't add) all present functions.
1153 for (i = 0; i < funcs; i++, card->sdio_funcs++) {
1154 #ifdef CONFIG_MMC_EMBEDDED_SDIO
1155 if (host->embedded_sdio_data.funcs) {
1156 struct sdio_func *tmp;
1158 tmp = sdio_alloc_func(host->card);
1162 card->sdio_func[i] = tmp;
1163 tmp->class = host->embedded_sdio_data.funcs[i].f_class;
1164 tmp->max_blksize = host->embedded_sdio_data.funcs[i].f_maxblksize;
1165 tmp->vendor = card->cis.vendor;
1166 tmp->device = card->cis.device;
1169 err = sdio_init_func(host->card, i + 1);
1172 #ifdef CONFIG_MMC_EMBEDDED_SDIO
1176 * Enable Runtime PM for this func (if supported)
1178 if (host->caps & MMC_CAP_POWER_OFF_CARD)
1179 pm_runtime_enable(&card->sdio_func[i]->dev);
1183 * First add the card to the driver model...
1185 mmc_release_host(host);
1186 err = mmc_add_card(host->card);
1191 * ...then the SDIO functions.
1193 for (i = 0;i < funcs;i++) {
1194 err = sdio_add_func(host->card->sdio_func[i]);
1199 mmc_claim_host(host);
1204 /* Remove without lock if the device has been added. */
1205 mmc_sdio_remove(host);
1206 mmc_claim_host(host);
1208 /* And with lock if it hasn't been added. */
1209 mmc_release_host(host);
1211 mmc_sdio_remove(host);
1212 mmc_claim_host(host);
1214 mmc_detach_bus(host);
1216 pr_err("%s: error %d whilst initialising SDIO card\n",
1217 mmc_hostname(host), err);
1222 int sdio_reset_comm(struct mmc_card *card)
1224 struct mmc_host *host = card->host;
1229 printk("%s():\n", __func__);
1230 mmc_claim_host(host);
1232 mmc_retune_disable(host);
1234 mmc_power_cycle(host, host->card->ocr);
1237 mmc_set_clock(host, host->f_min);
1239 err = mmc_send_io_op_cond(host, 0, &ocr);
1243 rocr = mmc_select_voltage(host, ocr);
1249 err = mmc_sdio_init_card(host, rocr, card, 0);
1253 mmc_release_host(host);
1256 printk("%s: Error resetting SDIO communications (%d)\n",
1257 mmc_hostname(host), err);
1258 mmc_release_host(host);
1261 EXPORT_SYMBOL(sdio_reset_comm);