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 #ifdef CONFIG_MMC_EMBEDDED_SDIO
32 #include <linux/mmc/sdio_ids.h>
35 static int sdio_read_fbr(struct sdio_func *func)
40 if (mmc_card_nonstd_func_interface(func->card)) {
41 func->class = SDIO_CLASS_NONE;
45 ret = mmc_io_rw_direct(func->card, 0, 0,
46 SDIO_FBR_BASE(func->num) + SDIO_FBR_STD_IF, 0, &data);
53 ret = mmc_io_rw_direct(func->card, 0, 0,
54 SDIO_FBR_BASE(func->num) + SDIO_FBR_STD_IF_EXT, 0, &data);
65 static int sdio_init_func(struct mmc_card *card, unsigned int fn)
68 struct sdio_func *func;
70 BUG_ON(fn > SDIO_MAX_FUNCS);
72 func = sdio_alloc_func(card);
78 if (!(card->quirks & MMC_QUIRK_NONSTD_SDIO)) {
79 ret = sdio_read_fbr(func);
83 ret = sdio_read_func_cis(func);
87 func->vendor = func->card->cis.vendor;
88 func->device = func->card->cis.device;
89 func->max_blksize = func->card->cis.blksize;
92 card->sdio_func[fn - 1] = func;
98 * It is okay to remove the function here even though we hold
99 * the host lock as we haven't registered the device yet.
101 sdio_remove_func(func);
105 static int sdio_read_cccr(struct mmc_card *card, u32 ocr)
109 int uhs = ocr & R4_18V_PRESENT;
113 memset(&card->cccr, 0, sizeof(struct sdio_cccr));
115 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_CCCR, 0, &data);
119 cccr_vsn = data & 0x0f;
121 if (cccr_vsn > SDIO_CCCR_REV_3_00) {
122 printk(KERN_ERR "%s: unrecognised CCCR structure version %d\n",
123 mmc_hostname(card->host), cccr_vsn);
127 card->cccr.sdio_vsn = (data & 0xf0) >> 4;
129 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_CAPS, 0, &data);
133 if (data & SDIO_CCCR_CAP_SMB)
134 card->cccr.multi_block = 1;
135 if (data & SDIO_CCCR_CAP_LSC)
136 card->cccr.low_speed = 1;
137 if (data & SDIO_CCCR_CAP_4BLS)
138 card->cccr.wide_bus = 1;
140 if (cccr_vsn >= SDIO_CCCR_REV_1_10) {
141 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_POWER, 0, &data);
145 if (data & SDIO_POWER_SMPC)
146 card->cccr.high_power = 1;
149 if (cccr_vsn >= SDIO_CCCR_REV_1_20) {
150 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_SPEED, 0, &speed);
154 card->scr.sda_spec3 = 0;
155 card->sw_caps.sd3_bus_mode = 0;
156 card->sw_caps.sd3_drv_type = 0;
157 if (cccr_vsn >= SDIO_CCCR_REV_3_00 && uhs) {
158 card->scr.sda_spec3 = 1;
159 ret = mmc_io_rw_direct(card, 0, 0,
160 SDIO_CCCR_UHS, 0, &data);
164 if (card->host->caps &
165 (MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 |
166 MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR104 |
167 MMC_CAP_UHS_DDR50)) {
168 if (data & SDIO_UHS_DDR50)
169 card->sw_caps.sd3_bus_mode
170 |= SD_MODE_UHS_DDR50;
172 if (data & SDIO_UHS_SDR50)
173 card->sw_caps.sd3_bus_mode
174 |= SD_MODE_UHS_SDR50;
176 if (data & SDIO_UHS_SDR104)
177 card->sw_caps.sd3_bus_mode
178 |= SD_MODE_UHS_SDR104;
181 ret = mmc_io_rw_direct(card, 0, 0,
182 SDIO_CCCR_DRIVE_STRENGTH, 0, &data);
186 if (data & SDIO_DRIVE_SDTA)
187 card->sw_caps.sd3_drv_type |= SD_DRIVER_TYPE_A;
188 if (data & SDIO_DRIVE_SDTC)
189 card->sw_caps.sd3_drv_type |= SD_DRIVER_TYPE_C;
190 if (data & SDIO_DRIVE_SDTD)
191 card->sw_caps.sd3_drv_type |= SD_DRIVER_TYPE_D;
194 /* if no uhs mode ensure we check for high speed */
195 if (!card->sw_caps.sd3_bus_mode) {
196 if (speed & SDIO_SPEED_SHS) {
197 card->cccr.high_speed = 1;
198 card->sw_caps.hs_max_dtr = 50000000;
200 card->cccr.high_speed = 0;
201 card->sw_caps.hs_max_dtr = 25000000;
210 static int sdio_enable_wide(struct mmc_card *card)
215 if (!(card->host->caps & MMC_CAP_4_BIT_DATA))
218 if (card->cccr.low_speed && !card->cccr.wide_bus)
221 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl);
225 ctrl |= SDIO_BUS_WIDTH_4BIT;
227 ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL);
235 * If desired, disconnect the pull-up resistor on CD/DAT[3] (pin 1)
236 * of the card. This may be required on certain setups of boards,
237 * controllers and embedded sdio device which do not need the card's
238 * pull-up. As a result, card detection is disabled and power is saved.
240 static int sdio_disable_cd(struct mmc_card *card)
245 if (!mmc_card_disable_cd(card))
248 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl);
252 ctrl |= SDIO_BUS_CD_DISABLE;
254 return mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL);
258 * Devices that remain active during a system suspend are
259 * put back into 1-bit mode.
261 static int sdio_disable_wide(struct mmc_card *card)
266 if (!(card->host->caps & MMC_CAP_4_BIT_DATA))
269 if (card->cccr.low_speed && !card->cccr.wide_bus)
272 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl);
276 if (!(ctrl & SDIO_BUS_WIDTH_4BIT))
279 ctrl &= ~SDIO_BUS_WIDTH_4BIT;
280 ctrl |= SDIO_BUS_ASYNC_INT;
282 ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL);
286 mmc_set_bus_width(card->host, MMC_BUS_WIDTH_1);
292 static int sdio_enable_4bit_bus(struct mmc_card *card)
296 if (card->type == MMC_TYPE_SDIO)
297 return sdio_enable_wide(card);
299 if ((card->host->caps & MMC_CAP_4_BIT_DATA) &&
300 (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) {
301 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);
316 * Test if the card supports high-speed mode and, if so, switch to it.
318 static int mmc_sdio_switch_hs(struct mmc_card *card, int enable)
323 if (!(card->host->caps & MMC_CAP_SD_HIGHSPEED))
326 if (!card->cccr.high_speed)
329 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_SPEED, 0, &speed);
334 speed |= SDIO_SPEED_EHS;
336 speed &= ~SDIO_SPEED_EHS;
338 ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_SPEED, speed, NULL);
346 * Enable SDIO/combo card's high-speed mode. Return 0/1 if [not]supported.
348 static int sdio_enable_hs(struct mmc_card *card)
352 ret = mmc_sdio_switch_hs(card, true);
353 if (ret <= 0 || card->type == MMC_TYPE_SDIO)
356 ret = mmc_sd_switch_hs(card);
358 mmc_sdio_switch_hs(card, false);
363 static unsigned mmc_sdio_get_max_clock(struct mmc_card *card)
367 if (mmc_card_highspeed(card)) {
369 * The SDIO specification doesn't mention how
370 * the CIS transfer speed register relates to
371 * high-speed, but it seems that 50 MHz is
376 max_dtr = card->cis.max_dtr;
379 if (card->type == MMC_TYPE_SD_COMBO)
380 max_dtr = min(max_dtr, mmc_sd_get_max_clock(card));
385 static unsigned char host_drive_to_sdio_drive(int host_strength)
387 switch (host_strength) {
388 case MMC_SET_DRIVER_TYPE_A:
389 return SDIO_DTSx_SET_TYPE_A;
390 case MMC_SET_DRIVER_TYPE_B:
391 return SDIO_DTSx_SET_TYPE_B;
392 case MMC_SET_DRIVER_TYPE_C:
393 return SDIO_DTSx_SET_TYPE_C;
394 case MMC_SET_DRIVER_TYPE_D:
395 return SDIO_DTSx_SET_TYPE_D;
397 return SDIO_DTSx_SET_TYPE_B;
401 static void sdio_select_driver_type(struct mmc_card *card)
403 int host_drv_type = SD_DRIVER_TYPE_B;
404 int card_drv_type = SD_DRIVER_TYPE_B;
406 unsigned char card_strength;
410 * If the host doesn't support any of the Driver Types A,C or D,
411 * or there is no board specific handler then default Driver
414 if (!(card->host->caps &
415 (MMC_CAP_DRIVER_TYPE_A |
416 MMC_CAP_DRIVER_TYPE_C |
417 MMC_CAP_DRIVER_TYPE_D)))
420 if (!card->host->ops->select_drive_strength)
423 if (card->host->caps & MMC_CAP_DRIVER_TYPE_A)
424 host_drv_type |= SD_DRIVER_TYPE_A;
426 if (card->host->caps & MMC_CAP_DRIVER_TYPE_C)
427 host_drv_type |= SD_DRIVER_TYPE_C;
429 if (card->host->caps & MMC_CAP_DRIVER_TYPE_D)
430 host_drv_type |= SD_DRIVER_TYPE_D;
432 if (card->sw_caps.sd3_drv_type & SD_DRIVER_TYPE_A)
433 card_drv_type |= SD_DRIVER_TYPE_A;
435 if (card->sw_caps.sd3_drv_type & SD_DRIVER_TYPE_C)
436 card_drv_type |= SD_DRIVER_TYPE_C;
438 if (card->sw_caps.sd3_drv_type & SD_DRIVER_TYPE_D)
439 card_drv_type |= SD_DRIVER_TYPE_D;
442 * The drive strength that the hardware can support
443 * depends on the board design. Pass the appropriate
444 * information and let the hardware specific code
445 * return what is possible given the options
447 drive_strength = card->host->ops->select_drive_strength(
448 card->sw_caps.uhs_max_dtr,
449 host_drv_type, card_drv_type);
451 /* if error just use default for drive strength B */
452 err = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_DRIVE_STRENGTH, 0,
457 card_strength &= ~(SDIO_DRIVE_DTSx_MASK<<SDIO_DRIVE_DTSx_SHIFT);
458 card_strength |= host_drive_to_sdio_drive(drive_strength);
460 err = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_DRIVE_STRENGTH,
461 card_strength, NULL);
463 /* if error default to drive strength B */
465 mmc_set_driver_type(card->host, drive_strength);
469 static int sdio_set_bus_speed_mode(struct mmc_card *card)
471 unsigned int bus_speed, timing;
476 * If the host doesn't support any of the UHS-I modes, fallback on
479 if (!(card->host->caps & (MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 |
480 MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_DDR50)))
483 bus_speed = SDIO_SPEED_SDR12;
484 timing = MMC_TIMING_UHS_SDR12;
485 if ((card->host->caps & MMC_CAP_UHS_SDR104) &&
486 (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_SDR104)) {
487 bus_speed = SDIO_SPEED_SDR104;
488 timing = MMC_TIMING_UHS_SDR104;
489 card->sw_caps.uhs_max_dtr = UHS_SDR104_MAX_DTR;
490 } else if ((card->host->caps & MMC_CAP_UHS_DDR50) &&
491 (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_DDR50)) {
492 bus_speed = SDIO_SPEED_DDR50;
493 timing = MMC_TIMING_UHS_DDR50;
494 card->sw_caps.uhs_max_dtr = UHS_DDR50_MAX_DTR;
495 } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
496 MMC_CAP_UHS_SDR50)) && (card->sw_caps.sd3_bus_mode &
497 SD_MODE_UHS_SDR50)) {
498 bus_speed = SDIO_SPEED_SDR50;
499 timing = MMC_TIMING_UHS_SDR50;
500 card->sw_caps.uhs_max_dtr = UHS_SDR50_MAX_DTR;
501 } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
502 MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR25)) &&
503 (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_SDR25)) {
504 bus_speed = SDIO_SPEED_SDR25;
505 timing = MMC_TIMING_UHS_SDR25;
506 card->sw_caps.uhs_max_dtr = UHS_SDR25_MAX_DTR;
507 } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
508 MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR25 |
509 MMC_CAP_UHS_SDR12)) && (card->sw_caps.sd3_bus_mode &
510 SD_MODE_UHS_SDR12)) {
511 bus_speed = SDIO_SPEED_SDR12;
512 timing = MMC_TIMING_UHS_SDR12;
513 card->sw_caps.uhs_max_dtr = UHS_SDR12_MAX_DTR;
516 err = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_SPEED, 0, &speed);
520 speed &= ~SDIO_SPEED_BSS_MASK;
522 err = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_SPEED, speed, NULL);
527 mmc_set_timing(card->host, timing);
528 mmc_set_clock(card->host, card->sw_caps.uhs_max_dtr);
535 * UHS-I specific initialization procedure
537 static int mmc_sdio_init_uhs_card(struct mmc_card *card)
541 if (!card->scr.sda_spec3)
545 * Switch to wider bus (if supported).
547 if (card->host->caps & MMC_CAP_4_BIT_DATA) {
548 err = sdio_enable_4bit_bus(card);
550 mmc_set_bus_width(card->host, MMC_BUS_WIDTH_4);
555 /* Set the driver strength for the card */
556 sdio_select_driver_type(card);
558 /* Set bus speed mode of the card */
559 err = sdio_set_bus_speed_mode(card);
563 /* Initialize and start re-tuning timer */
564 if (!mmc_host_is_spi(card->host) && card->host->ops->execute_tuning)
565 err = card->host->ops->execute_tuning(card->host,
566 MMC_SEND_TUNING_BLOCK);
574 * Handle the detection and initialisation of a card.
576 * In the case of a resume, "oldcard" will contain the card
577 * we're trying to reinitialise.
579 static int mmc_sdio_init_card(struct mmc_host *host, u32 ocr,
580 struct mmc_card *oldcard, int powered_resume)
582 struct mmc_card *card;
586 WARN_ON(!host->claimed);
589 * Inform the card of the voltage
591 if (!powered_resume) {
592 err = mmc_send_io_op_cond(host, host->ocr, &ocr);
598 * For SPI, enable CRC as appropriate.
600 if (mmc_host_is_spi(host)) {
601 err = mmc_spi_set_crc(host, use_spi_crc);
607 * Allocate card structure.
609 card = mmc_alloc_card(host, NULL);
615 if ((ocr & R4_MEMORY_PRESENT) &&
616 mmc_sd_get_cid(host, host->ocr & ocr, card->raw_cid, NULL) == 0) {
617 card->type = MMC_TYPE_SD_COMBO;
619 if (oldcard && (oldcard->type != MMC_TYPE_SD_COMBO ||
620 memcmp(card->raw_cid, oldcard->raw_cid, sizeof(card->raw_cid)) != 0)) {
621 mmc_remove_card(card);
625 card->type = MMC_TYPE_SDIO;
627 if (oldcard && oldcard->type != MMC_TYPE_SDIO) {
628 mmc_remove_card(card);
634 * Call the optional HC's init_card function to handle quirks.
636 if (host->ops->init_card)
637 host->ops->init_card(host, card);
640 * If the host and card support UHS-I mode request the card
641 * to switch to 1.8V signaling level. No 1.8v signalling if
642 * UHS mode is not enabled to maintain compatibilty and some
643 * systems that claim 1.8v signalling in fact do not support
646 if ((ocr & R4_18V_PRESENT) &&
648 (MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 |
649 MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR104 |
650 MMC_CAP_UHS_DDR50))) {
651 err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_180,
654 ocr &= ~R4_18V_PRESENT;
655 host->ocr &= ~R4_18V_PRESENT;
659 ocr &= ~R4_18V_PRESENT;
660 host->ocr &= ~R4_18V_PRESENT;
664 * For native busses: set card RCA and quit open drain mode.
666 if (!powered_resume && !mmc_host_is_spi(host)) {
667 err = mmc_send_relative_addr(host, &card->rca);
672 * Update oldcard with the new RCA received from the SDIO
673 * device -- we're doing this so that it's updated in the
674 * "card" struct when oldcard overwrites that later.
677 oldcard->rca = card->rca;
679 mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
683 * Read CSD, before selecting the card
685 if (!oldcard && card->type == MMC_TYPE_SD_COMBO) {
686 err = mmc_sd_get_csd(host, card);
690 mmc_decode_cid(card);
694 * Select card, as all following commands rely on that.
696 if (!powered_resume && !mmc_host_is_spi(host)) {
697 err = mmc_select_card(card);
702 if (card->quirks & MMC_QUIRK_NONSTD_SDIO) {
704 * This is non-standard SDIO device, meaning it doesn't
705 * have any CIA (Common I/O area) registers present.
706 * It's host's responsibility to fill cccr and cis
707 * structures in init_card().
709 mmc_set_clock(host, card->cis.max_dtr);
711 if (card->cccr.high_speed) {
712 mmc_card_set_highspeed(card);
713 mmc_set_timing(card->host, MMC_TIMING_SD_HS);
719 #ifdef CONFIG_MMC_EMBEDDED_SDIO
720 if (host->embedded_sdio_data.cccr)
721 memcpy(&card->cccr, host->embedded_sdio_data.cccr, sizeof(struct sdio_cccr));
725 * Read the common registers.
727 err = sdio_read_cccr(card, ocr);
730 #ifdef CONFIG_MMC_EMBEDDED_SDIO
734 #ifdef CONFIG_MMC_EMBEDDED_SDIO
735 if (host->embedded_sdio_data.cis)
736 memcpy(&card->cis, host->embedded_sdio_data.cis, sizeof(struct sdio_cis));
740 * Read the common CIS tuples.
742 err = sdio_read_common_cis(card);
745 #ifdef CONFIG_MMC_EMBEDDED_SDIO
750 int same = (card->cis.vendor == oldcard->cis.vendor &&
751 card->cis.device == oldcard->cis.device);
752 mmc_remove_card(card);
758 mmc_fixup_device(card, NULL);
760 if (card->type == MMC_TYPE_SD_COMBO) {
761 err = mmc_sd_setup_card(host, card, oldcard != NULL);
762 /* handle as SDIO-only card if memory init failed */
765 if (mmc_host_is_spi(host))
766 /* should not fail, as it worked previously */
767 mmc_spi_set_crc(host, use_spi_crc);
768 card->type = MMC_TYPE_SDIO;
770 card->dev.type = &sd_type;
774 * If needed, disconnect card detection pull-up resistor.
776 err = sdio_disable_cd(card);
780 /* Initialization sequence for UHS-I cards */
781 /* Only if card supports 1.8v and UHS signaling */
782 if ((ocr & R4_18V_PRESENT) && card->sw_caps.sd3_bus_mode) {
783 err = mmc_sdio_init_uhs_card(card);
787 /* Card is an ultra-high-speed card */
788 mmc_card_set_uhs(card);
791 * Switch to high-speed (if supported).
793 err = sdio_enable_hs(card);
795 mmc_sd_go_highspeed(card);
800 * Change to the card's maximum speed.
802 mmc_set_clock(host, mmc_sdio_get_max_clock(card));
805 * Switch to wider bus (if supported).
807 err = sdio_enable_4bit_bus(card);
809 mmc_set_bus_width(card->host, MMC_BUS_WIDTH_4);
820 mmc_remove_card(card);
827 * Host is being removed. Free up the current card.
829 static void mmc_sdio_remove(struct mmc_host *host)
836 for (i = 0;i < host->card->sdio_funcs;i++) {
837 if (host->card->sdio_func[i]) {
838 sdio_remove_func(host->card->sdio_func[i]);
839 host->card->sdio_func[i] = NULL;
843 mmc_remove_card(host->card);
848 * Card detection - card is alive.
850 static int mmc_sdio_alive(struct mmc_host *host)
852 return mmc_select_card(host->card);
856 * Card detection callback from host.
858 static void mmc_sdio_detect(struct mmc_host *host)
865 /* Make sure card is powered before detecting it */
866 if (host->caps & MMC_CAP_POWER_OFF_CARD) {
867 err = pm_runtime_get_sync(&host->card->dev);
872 mmc_claim_host(host);
875 * Just check if our card has been removed.
877 err = _mmc_detect_card_removed(host);
879 mmc_release_host(host);
882 * Tell PM core it's OK to power off the card now.
884 * The _sync variant is used in order to ensure that the card
885 * is left powered off in case an error occurred, and the card
886 * is going to be removed.
888 * Since there is no specific reason to believe a new user
889 * is about to show up at this point, the _sync variant is
892 if (host->caps & MMC_CAP_POWER_OFF_CARD)
893 pm_runtime_put_sync(&host->card->dev);
897 mmc_sdio_remove(host);
899 mmc_claim_host(host);
900 mmc_detach_bus(host);
902 mmc_release_host(host);
907 * SDIO suspend. We need to suspend all functions separately.
908 * Therefore all registered functions must have drivers with suspend
909 * and resume methods. Failing that we simply remove the whole card.
911 static int mmc_sdio_suspend(struct mmc_host *host)
915 for (i = 0; i < host->card->sdio_funcs; i++) {
916 struct sdio_func *func = host->card->sdio_func[i];
917 if (func && sdio_func_present(func) && func->dev.driver) {
918 const struct dev_pm_ops *pmops = func->dev.driver->pm;
919 if (!pmops || !pmops->suspend || !pmops->resume) {
920 /* force removal of entire card in that case */
923 err = pmops->suspend(&func->dev);
928 while (err && --i >= 0) {
929 struct sdio_func *func = host->card->sdio_func[i];
930 if (func && sdio_func_present(func) && func->dev.driver) {
931 const struct dev_pm_ops *pmops = func->dev.driver->pm;
932 pmops->resume(&func->dev);
936 #ifdef CONFIG_MACH_PX
938 if (!err && mmc_card_keep_power(host) && mmc_card_wake_sdio_irq(host)) {
939 mmc_claim_host(host);
940 sdio_disable_wide(host->card);
941 mmc_release_host(host);
948 static int mmc_sdio_resume(struct mmc_host *host)
955 /* Basic card reinitialization. */
956 mmc_claim_host(host);
958 /* No need to reinitialize powered-resumed nonremovable cards */
959 if (mmc_card_is_removable(host) || !mmc_card_keep_power(host))
960 err = mmc_sdio_init_card(host, host->ocr, host->card,
961 mmc_card_keep_power(host));
962 else if (mmc_card_keep_power(host) && mmc_card_wake_sdio_irq(host)) {
963 /* We may have switched to 1-bit mode during suspend */
964 err = sdio_enable_4bit_bus(host->card);
966 mmc_set_bus_width(host, MMC_BUS_WIDTH_4);
971 if (!err && host->sdio_irqs)
972 mmc_signal_sdio_irq(host);
973 mmc_release_host(host);
976 * If the card looked to be the same as before suspending, then
977 * we proceed to resume all card functions. If one of them returns
978 * an error then we simply return that error to the core and the
979 * card will be redetected as new. It is the responsibility of
980 * the function driver to perform further tests with the extra
981 * knowledge it has of the card to confirm the card is indeed the
982 * same as before suspending (same MAC address for network cards,
983 * etc.) and return an error otherwise.
985 for (i = 0; !err && i < host->card->sdio_funcs; i++) {
986 struct sdio_func *func = host->card->sdio_func[i];
987 if (func && sdio_func_present(func) && func->dev.driver) {
988 const struct dev_pm_ops *pmops = func->dev.driver->pm;
989 err = pmops->resume(&func->dev);
996 static int mmc_sdio_power_restore(struct mmc_host *host)
1002 BUG_ON(!host->card);
1004 mmc_claim_host(host);
1007 * Reset the card by performing the same steps that are taken by
1008 * mmc_rescan_try_freq() and mmc_attach_sdio() during a "normal" probe.
1010 * sdio_reset() is technically not needed. Having just powered up the
1011 * hardware, it should already be in reset state. However, some
1012 * platforms (such as SD8686 on OLPC) do not instantly cut power,
1013 * meaning that a reset is required when restoring power soon after
1014 * powering off. It is harmless in other cases.
1016 * The CMD5 reset (mmc_send_io_op_cond()), according to the SDIO spec,
1017 * is not necessary for non-removable cards. However, it is required
1018 * for OLPC SD8686 (which expects a [CMD5,5,3,7] init sequence), and
1019 * harmless in other situations.
1021 * With these steps taken, mmc_select_voltage() is also required to
1022 * restore the correct voltage setting of the card.
1026 mmc_send_if_cond(host, host->ocr_avail);
1028 ret = mmc_send_io_op_cond(host, 0, &ocr);
1032 if (host->ocr_avail_sdio)
1033 host->ocr_avail = host->ocr_avail_sdio;
1035 host->ocr = mmc_select_voltage(host, ocr & ~0xFF);
1041 ret = mmc_sdio_init_card(host, host->ocr, host->card,
1042 mmc_card_keep_power(host));
1043 if (!ret && host->sdio_irqs)
1044 mmc_signal_sdio_irq(host);
1047 mmc_release_host(host);
1052 static const struct mmc_bus_ops mmc_sdio_ops = {
1053 .remove = mmc_sdio_remove,
1054 .detect = mmc_sdio_detect,
1055 .suspend = mmc_sdio_suspend,
1056 .resume = mmc_sdio_resume,
1057 .power_restore = mmc_sdio_power_restore,
1058 .alive = mmc_sdio_alive,
1063 * Starting point for SDIO card init.
1065 int mmc_attach_sdio(struct mmc_host *host)
1069 struct mmc_card *card;
1072 WARN_ON(!host->claimed);
1074 err = mmc_send_io_op_cond(host, 0, &ocr);
1078 mmc_attach_bus(host, &mmc_sdio_ops);
1079 if (host->ocr_avail_sdio)
1080 host->ocr_avail = host->ocr_avail_sdio;
1083 * Sanity check the voltages that the card claims to
1087 printk(KERN_WARNING "%s: card claims to support voltages "
1088 "below the defined range. These will be ignored.\n",
1089 mmc_hostname(host));
1093 host->ocr = mmc_select_voltage(host, ocr);
1096 * Can we support the voltage(s) of the card(s)?
1104 * Detect and init the card.
1106 err = mmc_sdio_init_card(host, host->ocr, NULL, 0);
1108 if (err == -EAGAIN) {
1110 * Retry initialization with S18R set to 0.
1112 host->ocr &= ~R4_18V_PRESENT;
1113 err = mmc_sdio_init_card(host, host->ocr, 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 printk(KERN_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;
1227 printk("%s():\n", __func__);
1228 mmc_claim_host(host);
1232 mmc_set_clock(host, host->f_min);
1234 err = mmc_send_io_op_cond(host, 0, &ocr);
1239 printk(KERN_WARNING "%s: card claims to support voltages "
1240 "below the defined range. These will be ignored.\n",
1241 mmc_hostname(host));
1245 host->ocr = mmc_select_voltage(host, ocr);
1251 err = mmc_send_io_op_cond(host, host->ocr, &ocr);
1255 if (mmc_host_is_spi(host)) {
1256 err = mmc_spi_set_crc(host, use_spi_crc);
1261 if (!mmc_host_is_spi(host)) {
1262 err = mmc_send_relative_addr(host, &card->rca);
1265 mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
1267 if (!mmc_host_is_spi(host)) {
1268 err = mmc_select_card(card);
1274 * Switch to high-speed (if supported).
1276 err = sdio_enable_hs(card);
1278 mmc_sd_go_highspeed(card);
1283 * Change to the card's maximum speed.
1285 mmc_set_clock(host, mmc_sdio_get_max_clock(card));
1287 err = sdio_enable_4bit_bus(card);
1289 mmc_set_bus_width(host, MMC_BUS_WIDTH_4);
1293 mmc_release_host(host);
1296 printk("%s: Error resetting SDIO communications (%d)\n",
1297 mmc_hostname(host), err);
1298 mmc_release_host(host);
1301 EXPORT_SYMBOL(sdio_reset_comm);