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>
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 pr_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, &speed);
155 card->scr.sda_spec3 = 0;
156 card->sw_caps.sd3_bus_mode = 0;
157 card->sw_caps.sd3_drv_type = 0;
158 if (cccr_vsn >= SDIO_CCCR_REV_3_00 && uhs) {
159 card->scr.sda_spec3 = 1;
160 ret = mmc_io_rw_direct(card, 0, 0,
161 SDIO_CCCR_UHS, 0, &data);
165 if (mmc_host_uhs(card->host)) {
166 if (data & SDIO_UHS_DDR50)
167 card->sw_caps.sd3_bus_mode
168 |= SD_MODE_UHS_DDR50;
170 if (data & SDIO_UHS_SDR50)
171 card->sw_caps.sd3_bus_mode
172 |= SD_MODE_UHS_SDR50;
174 if (data & SDIO_UHS_SDR104)
175 card->sw_caps.sd3_bus_mode
176 |= SD_MODE_UHS_SDR104;
179 ret = mmc_io_rw_direct(card, 0, 0,
180 SDIO_CCCR_DRIVE_STRENGTH, 0, &data);
184 if (data & SDIO_DRIVE_SDTA)
185 card->sw_caps.sd3_drv_type |= SD_DRIVER_TYPE_A;
186 if (data & SDIO_DRIVE_SDTC)
187 card->sw_caps.sd3_drv_type |= SD_DRIVER_TYPE_C;
188 if (data & SDIO_DRIVE_SDTD)
189 card->sw_caps.sd3_drv_type |= SD_DRIVER_TYPE_D;
192 /* if no uhs mode ensure we check for high speed */
193 if (!card->sw_caps.sd3_bus_mode) {
194 if (speed & 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 pr_warn("%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 err = sdio_enable_wide(card);
302 else if ((card->host->caps & MMC_CAP_4_BIT_DATA) &&
303 (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) {
304 err = mmc_app_set_bus_width(card, MMC_BUS_WIDTH_4);
307 err = sdio_enable_wide(card);
309 mmc_app_set_bus_width(card, MMC_BUS_WIDTH_1);
314 mmc_set_bus_width(card->host, MMC_BUS_WIDTH_4);
323 * Test if the card supports high-speed mode and, if so, switch to it.
325 static int mmc_sdio_switch_hs(struct mmc_card *card, int enable)
330 if (!(card->host->caps & MMC_CAP_SD_HIGHSPEED))
333 if (!card->cccr.high_speed)
336 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_SPEED, 0, &speed);
341 speed |= SDIO_SPEED_EHS;
343 speed &= ~SDIO_SPEED_EHS;
345 ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_SPEED, speed, NULL);
353 * Enable SDIO/combo card's high-speed mode. Return 0/1 if [not]supported.
355 static int sdio_enable_hs(struct mmc_card *card)
359 ret = mmc_sdio_switch_hs(card, true);
360 if (ret <= 0 || card->type == MMC_TYPE_SDIO)
363 ret = mmc_sd_switch_hs(card);
365 mmc_sdio_switch_hs(card, false);
370 static unsigned mmc_sdio_get_max_clock(struct mmc_card *card)
374 if (mmc_card_hs(card)) {
376 * The SDIO specification doesn't mention how
377 * the CIS transfer speed register relates to
378 * high-speed, but it seems that 50 MHz is
383 max_dtr = card->cis.max_dtr;
386 if (card->type == MMC_TYPE_SD_COMBO)
387 max_dtr = min(max_dtr, mmc_sd_get_max_clock(card));
392 static unsigned char host_drive_to_sdio_drive(int host_strength)
394 switch (host_strength) {
395 case MMC_SET_DRIVER_TYPE_A:
396 return SDIO_DTSx_SET_TYPE_A;
397 case MMC_SET_DRIVER_TYPE_B:
398 return SDIO_DTSx_SET_TYPE_B;
399 case MMC_SET_DRIVER_TYPE_C:
400 return SDIO_DTSx_SET_TYPE_C;
401 case MMC_SET_DRIVER_TYPE_D:
402 return SDIO_DTSx_SET_TYPE_D;
404 return SDIO_DTSx_SET_TYPE_B;
408 static void sdio_select_driver_type(struct mmc_card *card)
410 int card_drv_type, drive_strength, drv_type;
411 unsigned char card_strength;
414 card->drive_strength = 0;
416 card_drv_type = card->sw_caps.sd3_drv_type | SD_DRIVER_TYPE_B;
418 drive_strength = mmc_select_drive_strength(card,
419 card->sw_caps.uhs_max_dtr,
420 card_drv_type, &drv_type);
422 if (drive_strength) {
423 /* if error just use default for drive strength B */
424 err = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_DRIVE_STRENGTH, 0,
429 card_strength &= ~(SDIO_DRIVE_DTSx_MASK<<SDIO_DRIVE_DTSx_SHIFT);
430 card_strength |= host_drive_to_sdio_drive(drive_strength);
432 /* if error default to drive strength B */
433 err = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_DRIVE_STRENGTH,
434 card_strength, NULL);
437 card->drive_strength = drive_strength;
441 mmc_set_driver_type(card->host, drv_type);
445 static int sdio_set_bus_speed_mode(struct mmc_card *card)
447 unsigned int bus_speed, timing;
452 * If the host doesn't support any of the UHS-I modes, fallback on
455 if (!mmc_host_uhs(card->host))
458 bus_speed = SDIO_SPEED_SDR12;
459 timing = MMC_TIMING_UHS_SDR12;
460 if ((card->host->caps & MMC_CAP_UHS_SDR104) &&
461 (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_SDR104)) {
462 bus_speed = SDIO_SPEED_SDR104;
463 timing = MMC_TIMING_UHS_SDR104;
464 card->sw_caps.uhs_max_dtr = UHS_SDR104_MAX_DTR;
465 card->sd_bus_speed = UHS_SDR104_BUS_SPEED;
466 } else if ((card->host->caps & MMC_CAP_UHS_DDR50) &&
467 (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_DDR50)) {
468 bus_speed = SDIO_SPEED_DDR50;
469 timing = MMC_TIMING_UHS_DDR50;
470 card->sw_caps.uhs_max_dtr = UHS_DDR50_MAX_DTR;
471 card->sd_bus_speed = UHS_DDR50_BUS_SPEED;
472 } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
473 MMC_CAP_UHS_SDR50)) && (card->sw_caps.sd3_bus_mode &
474 SD_MODE_UHS_SDR50)) {
475 bus_speed = SDIO_SPEED_SDR50;
476 timing = MMC_TIMING_UHS_SDR50;
477 card->sw_caps.uhs_max_dtr = UHS_SDR50_MAX_DTR;
478 card->sd_bus_speed = UHS_SDR50_BUS_SPEED;
479 } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
480 MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR25)) &&
481 (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_SDR25)) {
482 bus_speed = SDIO_SPEED_SDR25;
483 timing = MMC_TIMING_UHS_SDR25;
484 card->sw_caps.uhs_max_dtr = UHS_SDR25_MAX_DTR;
485 card->sd_bus_speed = UHS_SDR25_BUS_SPEED;
486 } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
487 MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR25 |
488 MMC_CAP_UHS_SDR12)) && (card->sw_caps.sd3_bus_mode &
489 SD_MODE_UHS_SDR12)) {
490 bus_speed = SDIO_SPEED_SDR12;
491 timing = MMC_TIMING_UHS_SDR12;
492 card->sw_caps.uhs_max_dtr = UHS_SDR12_MAX_DTR;
493 card->sd_bus_speed = UHS_SDR12_BUS_SPEED;
496 err = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_SPEED, 0, &speed);
500 speed &= ~SDIO_SPEED_BSS_MASK;
502 err = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_SPEED, speed, NULL);
507 mmc_set_timing(card->host, timing);
508 mmc_set_clock(card->host, card->sw_caps.uhs_max_dtr);
515 * UHS-I specific initialization procedure
517 static int mmc_sdio_init_uhs_card(struct mmc_card *card)
521 if (!card->scr.sda_spec3)
525 * Switch to wider bus (if supported).
527 if (card->host->caps & MMC_CAP_4_BIT_DATA)
528 err = sdio_enable_4bit_bus(card);
530 /* Set the driver strength for the card */
531 sdio_select_driver_type(card);
533 /* Set bus speed mode of the card */
534 err = sdio_set_bus_speed_mode(card);
539 * SPI mode doesn't define CMD19 and tuning is only valid for SDR50 and
540 * SDR104 mode SD-cards. Note that tuning is mandatory for SDR104.
542 if (!mmc_host_is_spi(card->host) &&
543 ((card->sw_caps.sd3_bus_mode & SD_MODE_UHS_SDR50) ||
544 (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_SDR104)))
545 err = mmc_execute_tuning(card);
551 * Handle the detection and initialisation of a card.
553 * In the case of a resume, "oldcard" will contain the card
554 * we're trying to reinitialise.
556 static int mmc_sdio_init_card(struct mmc_host *host, u32 ocr,
557 struct mmc_card *oldcard, int powered_resume)
559 struct mmc_card *card;
566 WARN_ON(!host->claimed);
568 /* to query card if 1.8V signalling is supported */
569 if (mmc_host_uhs(host))
570 ocr |= R4_18V_PRESENT;
574 pr_warn("%s: Skipping voltage switch\n", mmc_hostname(host));
575 ocr &= ~R4_18V_PRESENT;
579 * Inform the card of the voltage
581 if (!powered_resume) {
582 err = mmc_send_io_op_cond(host, ocr, &rocr);
588 * For SPI, enable CRC as appropriate.
590 if (mmc_host_is_spi(host)) {
591 err = mmc_spi_set_crc(host, use_spi_crc);
597 * Allocate card structure.
599 card = mmc_alloc_card(host, NULL);
605 if ((rocr & R4_MEMORY_PRESENT) &&
606 mmc_sd_get_cid(host, ocr & rocr, card->raw_cid, NULL) == 0) {
607 card->type = MMC_TYPE_SD_COMBO;
609 if (oldcard && (oldcard->type != MMC_TYPE_SD_COMBO ||
610 memcmp(card->raw_cid, oldcard->raw_cid, sizeof(card->raw_cid)) != 0)) {
611 mmc_remove_card(card);
615 card->type = MMC_TYPE_SDIO;
617 if (oldcard && oldcard->type != MMC_TYPE_SDIO) {
618 mmc_remove_card(card);
624 * Call the optional HC's init_card function to handle quirks.
626 if (host->ops->init_card)
627 host->ops->init_card(host, card);
630 * If the host and card support UHS-I mode request the card
631 * to switch to 1.8V signaling level. No 1.8v signalling if
632 * UHS mode is not enabled to maintain compatibility and some
633 * systems that claim 1.8v signalling in fact do not support
636 if (!powered_resume && (rocr & ocr & R4_18V_PRESENT)) {
637 err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_180,
639 if (err == -EAGAIN) {
642 mmc_send_if_cond(host, host->ocr_avail);
643 mmc_remove_card(card);
647 ocr &= ~R4_18V_PRESENT;
651 ocr &= ~R4_18V_PRESENT;
655 * For native busses: set card RCA and quit open drain mode.
657 if (!powered_resume && !mmc_host_is_spi(host)) {
658 err = mmc_send_relative_addr(host, &card->rca);
663 * Update oldcard with the new RCA received from the SDIO
664 * device -- we're doing this so that it's updated in the
665 * "card" struct when oldcard overwrites that later.
668 oldcard->rca = card->rca;
672 * Read CSD, before selecting the card
674 if (!oldcard && card->type == MMC_TYPE_SD_COMBO) {
675 err = mmc_sd_get_csd(host, card);
679 mmc_decode_cid(card);
683 * Select card, as all following commands rely on that.
685 if (!powered_resume && !mmc_host_is_spi(host)) {
686 err = mmc_select_card(card);
691 if (card->quirks & MMC_QUIRK_NONSTD_SDIO) {
693 * This is non-standard SDIO device, meaning it doesn't
694 * have any CIA (Common I/O area) registers present.
695 * It's host's responsibility to fill cccr and cis
696 * structures in init_card().
698 mmc_set_clock(host, card->cis.max_dtr);
700 if (card->cccr.high_speed) {
701 mmc_set_timing(card->host, MMC_TIMING_SD_HS);
707 #ifdef CONFIG_MMC_EMBEDDED_SDIO
708 if (host->embedded_sdio_data.cccr)
709 memcpy(&card->cccr, host->embedded_sdio_data.cccr, sizeof(struct sdio_cccr));
713 * Read the common registers.
715 err = sdio_read_cccr(card, ocr);
718 #ifdef CONFIG_MMC_EMBEDDED_SDIO
722 #ifdef CONFIG_MMC_EMBEDDED_SDIO
723 if (host->embedded_sdio_data.cis)
724 memcpy(&card->cis, host->embedded_sdio_data.cis, sizeof(struct sdio_cis));
728 * Read the common CIS tuples.
730 err = sdio_read_common_cis(card);
733 #ifdef CONFIG_MMC_EMBEDDED_SDIO
738 int same = (card->cis.vendor == oldcard->cis.vendor &&
739 card->cis.device == oldcard->cis.device);
740 mmc_remove_card(card);
746 card->ocr = ocr_card;
747 mmc_fixup_device(card, NULL);
749 if (card->type == MMC_TYPE_SD_COMBO) {
750 err = mmc_sd_setup_card(host, card, oldcard != NULL);
751 /* handle as SDIO-only card if memory init failed */
754 if (mmc_host_is_spi(host))
755 /* should not fail, as it worked previously */
756 mmc_spi_set_crc(host, use_spi_crc);
757 card->type = MMC_TYPE_SDIO;
759 card->dev.type = &sd_type;
763 * If needed, disconnect card detection pull-up resistor.
765 err = sdio_disable_cd(card);
769 /* Initialization sequence for UHS-I cards */
770 /* Only if card supports 1.8v and UHS signaling */
771 if ((ocr & R4_18V_PRESENT) && card->sw_caps.sd3_bus_mode) {
772 err = mmc_sdio_init_uhs_card(card);
777 * Switch to high-speed (if supported).
779 err = sdio_enable_hs(card);
781 mmc_set_timing(card->host, MMC_TIMING_SD_HS);
786 * Change to the card's maximum speed.
788 mmc_set_clock(host, mmc_sdio_get_max_clock(card));
791 * Switch to wider bus (if supported).
793 err = sdio_enable_4bit_bus(card);
804 mmc_remove_card(card);
811 * Host is being removed. Free up the current card.
813 static void mmc_sdio_remove(struct mmc_host *host)
820 for (i = 0;i < host->card->sdio_funcs;i++) {
821 if (host->card->sdio_func[i]) {
822 sdio_remove_func(host->card->sdio_func[i]);
823 host->card->sdio_func[i] = NULL;
827 mmc_remove_card(host->card);
832 * Card detection - card is alive.
834 static int mmc_sdio_alive(struct mmc_host *host)
836 return mmc_select_card(host->card);
840 * Card detection callback from host.
842 static void mmc_sdio_detect(struct mmc_host *host)
849 /* Make sure card is powered before detecting it */
850 if (host->caps & MMC_CAP_POWER_OFF_CARD) {
851 err = pm_runtime_get_sync(&host->card->dev);
853 pm_runtime_put_noidle(&host->card->dev);
858 mmc_claim_host(host);
861 * Just check if our card has been removed.
863 err = _mmc_detect_card_removed(host);
865 mmc_release_host(host);
868 * Tell PM core it's OK to power off the card now.
870 * The _sync variant is used in order to ensure that the card
871 * is left powered off in case an error occurred, and the card
872 * is going to be removed.
874 * Since there is no specific reason to believe a new user
875 * is about to show up at this point, the _sync variant is
878 if (host->caps & MMC_CAP_POWER_OFF_CARD)
879 pm_runtime_put_sync(&host->card->dev);
883 mmc_sdio_remove(host);
885 mmc_claim_host(host);
886 mmc_detach_bus(host);
888 mmc_release_host(host);
893 * SDIO pre_suspend. We need to suspend all functions separately.
894 * Therefore all registered functions must have drivers with suspend
895 * and resume methods. Failing that we simply remove the whole card.
897 static int mmc_sdio_pre_suspend(struct mmc_host *host)
901 for (i = 0; i < host->card->sdio_funcs; i++) {
902 struct sdio_func *func = host->card->sdio_func[i];
903 if (func && sdio_func_present(func) && func->dev.driver) {
904 const struct dev_pm_ops *pmops = func->dev.driver->pm;
905 if (!pmops || !pmops->suspend || !pmops->resume) {
906 /* force removal of entire card in that case */
917 * SDIO suspend. Suspend all functions separately.
919 static int mmc_sdio_suspend(struct mmc_host *host)
921 mmc_claim_host(host);
923 if (mmc_card_keep_power(host) && mmc_card_wake_sdio_irq(host))
924 sdio_disable_wide(host->card);
926 if (!mmc_card_keep_power(host)) {
928 } else if (host->retune_period) {
929 mmc_retune_timer_stop(host);
930 mmc_retune_needed(host);
933 mmc_release_host(host);
938 static int mmc_sdio_resume(struct mmc_host *host)
945 /* Basic card reinitialization. */
946 mmc_claim_host(host);
948 /* Restore power if needed */
949 if (!mmc_card_keep_power(host)) {
950 mmc_power_up(host, host->card->ocr);
952 * Tell runtime PM core we just powered up the card,
953 * since it still believes the card is powered off.
954 * Note that currently runtime PM is only enabled
955 * for SDIO cards that are MMC_CAP_POWER_OFF_CARD
957 if (host->caps & MMC_CAP_POWER_OFF_CARD) {
958 pm_runtime_disable(&host->card->dev);
959 pm_runtime_set_active(&host->card->dev);
960 pm_runtime_enable(&host->card->dev);
964 /* No need to reinitialize powered-resumed nonremovable cards */
965 if (mmc_card_is_removable(host) || !mmc_card_keep_power(host)) {
968 mmc_send_if_cond(host, host->card->ocr);
969 err = mmc_send_io_op_cond(host, 0, NULL);
971 err = mmc_sdio_init_card(host, host->card->ocr,
973 mmc_card_keep_power(host));
974 } else if (mmc_card_keep_power(host) && mmc_card_wake_sdio_irq(host)) {
975 /* We may have switched to 1-bit mode during suspend */
976 err = sdio_enable_4bit_bus(host->card);
979 if (!err && host->sdio_irqs) {
980 if (!(host->caps2 & MMC_CAP2_SDIO_IRQ_NOTHREAD))
981 wake_up_process(host->sdio_irq_thread);
982 else if (host->caps & MMC_CAP_SDIO_IRQ)
983 host->ops->enable_sdio_irq(host, 1);
986 mmc_release_host(host);
988 host->pm_flags &= ~MMC_PM_KEEP_POWER;
992 static int mmc_sdio_power_restore(struct mmc_host *host)
999 mmc_claim_host(host);
1002 * Reset the card by performing the same steps that are taken by
1003 * mmc_rescan_try_freq() and mmc_attach_sdio() during a "normal" probe.
1005 * sdio_reset() is technically not needed. Having just powered up the
1006 * hardware, it should already be in reset state. However, some
1007 * platforms (such as SD8686 on OLPC) do not instantly cut power,
1008 * meaning that a reset is required when restoring power soon after
1009 * powering off. It is harmless in other cases.
1011 * The CMD5 reset (mmc_send_io_op_cond()), according to the SDIO spec,
1012 * is not necessary for non-removable cards. However, it is required
1013 * for OLPC SD8686 (which expects a [CMD5,5,3,7] init sequence), and
1014 * harmless in other situations.
1020 mmc_send_if_cond(host, host->card->ocr);
1022 ret = mmc_send_io_op_cond(host, 0, NULL);
1026 ret = mmc_sdio_init_card(host, host->card->ocr, host->card,
1027 mmc_card_keep_power(host));
1028 if (!ret && host->sdio_irqs)
1029 mmc_signal_sdio_irq(host);
1032 mmc_release_host(host);
1037 static int mmc_sdio_runtime_suspend(struct mmc_host *host)
1039 /* No references to the card, cut the power to it. */
1040 mmc_claim_host(host);
1041 mmc_power_off(host);
1042 mmc_release_host(host);
1047 static int mmc_sdio_runtime_resume(struct mmc_host *host)
1051 /* Restore power and re-initialize. */
1052 mmc_claim_host(host);
1053 mmc_power_up(host, host->card->ocr);
1054 ret = mmc_sdio_power_restore(host);
1055 mmc_release_host(host);
1060 static int mmc_sdio_reset(struct mmc_host *host)
1062 mmc_power_cycle(host, host->card->ocr);
1063 return mmc_sdio_power_restore(host);
1066 static const struct mmc_bus_ops mmc_sdio_ops = {
1067 .remove = mmc_sdio_remove,
1068 .detect = mmc_sdio_detect,
1069 .pre_suspend = mmc_sdio_pre_suspend,
1070 .suspend = mmc_sdio_suspend,
1071 .resume = mmc_sdio_resume,
1072 .runtime_suspend = mmc_sdio_runtime_suspend,
1073 .runtime_resume = mmc_sdio_runtime_resume,
1074 .power_restore = mmc_sdio_power_restore,
1075 .alive = mmc_sdio_alive,
1076 .reset = mmc_sdio_reset,
1081 * Starting point for SDIO card init.
1083 int mmc_attach_sdio(struct mmc_host *host)
1087 struct mmc_card *card;
1090 WARN_ON(!host->claimed);
1092 err = mmc_send_io_op_cond(host, 0, &ocr);
1096 mmc_attach_bus(host, &mmc_sdio_ops);
1097 if (host->ocr_avail_sdio)
1098 host->ocr_avail = host->ocr_avail_sdio;
1101 rocr = mmc_select_voltage(host, ocr);
1104 * Can we support the voltage(s) of the card(s)?
1112 * Detect and init the card.
1114 err = mmc_sdio_init_card(host, rocr, NULL, 0);
1121 * Enable runtime PM only if supported by host+card+board
1123 if (host->caps & MMC_CAP_POWER_OFF_CARD) {
1125 * Let runtime PM core know our card is active
1127 err = pm_runtime_set_active(&card->dev);
1132 * Enable runtime PM for this card
1134 pm_runtime_enable(&card->dev);
1138 * The number of functions on the card is encoded inside
1141 funcs = (ocr & 0x70000000) >> 28;
1142 card->sdio_funcs = 0;
1144 #ifdef CONFIG_MMC_EMBEDDED_SDIO
1145 if (host->embedded_sdio_data.funcs)
1146 card->sdio_funcs = funcs = host->embedded_sdio_data.num_funcs;
1150 * Initialize (but don't add) all present functions.
1152 for (i = 0; i < funcs; i++, card->sdio_funcs++) {
1153 #ifdef CONFIG_MMC_EMBEDDED_SDIO
1154 if (host->embedded_sdio_data.funcs) {
1155 struct sdio_func *tmp;
1157 tmp = sdio_alloc_func(host->card);
1161 card->sdio_func[i] = tmp;
1162 tmp->class = host->embedded_sdio_data.funcs[i].f_class;
1163 tmp->max_blksize = host->embedded_sdio_data.funcs[i].f_maxblksize;
1164 tmp->vendor = card->cis.vendor;
1165 tmp->device = card->cis.device;
1168 err = sdio_init_func(host->card, i + 1);
1171 #ifdef CONFIG_MMC_EMBEDDED_SDIO
1175 * Enable Runtime PM for this func (if supported)
1177 if (host->caps & MMC_CAP_POWER_OFF_CARD)
1178 pm_runtime_enable(&card->sdio_func[i]->dev);
1182 * First add the card to the driver model...
1184 mmc_release_host(host);
1185 err = mmc_add_card(host->card);
1190 * ...then the SDIO functions.
1192 for (i = 0;i < funcs;i++) {
1193 err = sdio_add_func(host->card->sdio_func[i]);
1198 mmc_claim_host(host);
1203 /* Remove without lock if the device has been added. */
1204 mmc_sdio_remove(host);
1205 mmc_claim_host(host);
1207 /* And with lock if it hasn't been added. */
1208 mmc_release_host(host);
1210 mmc_sdio_remove(host);
1211 mmc_claim_host(host);
1213 mmc_detach_bus(host);
1215 pr_err("%s: error %d whilst initialising SDIO card\n",
1216 mmc_hostname(host), err);
1221 int sdio_reset_comm(struct mmc_card *card)
1223 struct mmc_host *host = card->host;
1228 printk("%s():\n", __func__);
1229 mmc_claim_host(host);
1233 mmc_set_clock(host, host->f_min);
1235 err = mmc_send_io_op_cond(host, 0, &ocr);
1239 rocr = mmc_select_voltage(host, ocr);
1245 err = mmc_sdio_init_card(host, rocr, card, 0);
1249 if (mmc_host_is_spi(host)) {
1250 err = mmc_spi_set_crc(host, use_spi_crc);
1255 if (!mmc_host_is_spi(host)) {
1256 err = mmc_send_relative_addr(host, &card->rca);
1259 mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
1261 if (!mmc_host_is_spi(host)) {
1262 err = mmc_select_card(card);
1268 * Switch to high-speed (if supported).
1270 err = sdio_enable_hs(card);
1272 mmc_sd_go_highspeed(card);
1277 * Change to the card's maximum speed.
1279 mmc_set_clock(host, mmc_sdio_get_max_clock(card));
1281 err = sdio_enable_4bit_bus(card);
1283 mmc_set_bus_width(host, MMC_BUS_WIDTH_4);
1287 mmc_release_host(host);
1290 printk("%s: Error resetting SDIO communications (%d)\n",
1291 mmc_hostname(host), err);
1292 mmc_release_host(host);
1295 EXPORT_SYMBOL(sdio_reset_comm);