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_warning("%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 return sdio_enable_wide(card
);
303 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
);
311 err
= sdio_enable_wide(card
);
313 mmc_app_set_bus_width(card
, MMC_BUS_WIDTH_1
);
320 * Test if the card supports high-speed mode and, if so, switch to it.
322 static int mmc_sdio_switch_hs(struct mmc_card
*card
, int enable
)
327 if (!(card
->host
->caps
& MMC_CAP_SD_HIGHSPEED
))
330 if (!card
->cccr
.high_speed
)
333 ret
= mmc_io_rw_direct(card
, 0, 0, SDIO_CCCR_SPEED
, 0, &speed
);
338 speed
|= SDIO_SPEED_EHS
;
340 speed
&= ~SDIO_SPEED_EHS
;
342 ret
= mmc_io_rw_direct(card
, 1, 0, SDIO_CCCR_SPEED
, speed
, NULL
);
350 * Enable SDIO/combo card's high-speed mode. Return 0/1 if [not]supported.
352 static int sdio_enable_hs(struct mmc_card
*card
)
356 ret
= mmc_sdio_switch_hs(card
, true);
357 if (ret
<= 0 || card
->type
== MMC_TYPE_SDIO
)
360 ret
= mmc_sd_switch_hs(card
);
362 mmc_sdio_switch_hs(card
, false);
367 static unsigned mmc_sdio_get_max_clock(struct mmc_card
*card
)
371 if (mmc_card_highspeed(card
)) {
373 * The SDIO specification doesn't mention how
374 * the CIS transfer speed register relates to
375 * high-speed, but it seems that 50 MHz is
380 max_dtr
= card
->cis
.max_dtr
;
383 if (card
->type
== MMC_TYPE_SD_COMBO
)
384 max_dtr
= min(max_dtr
, mmc_sd_get_max_clock(card
));
389 static unsigned char host_drive_to_sdio_drive(int host_strength
)
391 switch (host_strength
) {
392 case MMC_SET_DRIVER_TYPE_A
:
393 return SDIO_DTSx_SET_TYPE_A
;
394 case MMC_SET_DRIVER_TYPE_B
:
395 return SDIO_DTSx_SET_TYPE_B
;
396 case MMC_SET_DRIVER_TYPE_C
:
397 return SDIO_DTSx_SET_TYPE_C
;
398 case MMC_SET_DRIVER_TYPE_D
:
399 return SDIO_DTSx_SET_TYPE_D
;
401 return SDIO_DTSx_SET_TYPE_B
;
405 static void sdio_select_driver_type(struct mmc_card
*card
)
407 int host_drv_type
= SD_DRIVER_TYPE_B
;
408 int card_drv_type
= SD_DRIVER_TYPE_B
;
410 unsigned char card_strength
;
414 * If the host doesn't support any of the Driver Types A,C or D,
415 * or there is no board specific handler then default Driver
418 if (!(card
->host
->caps
&
419 (MMC_CAP_DRIVER_TYPE_A
|
420 MMC_CAP_DRIVER_TYPE_C
|
421 MMC_CAP_DRIVER_TYPE_D
)))
424 if (!card
->host
->ops
->select_drive_strength
)
427 if (card
->host
->caps
& MMC_CAP_DRIVER_TYPE_A
)
428 host_drv_type
|= SD_DRIVER_TYPE_A
;
430 if (card
->host
->caps
& MMC_CAP_DRIVER_TYPE_C
)
431 host_drv_type
|= SD_DRIVER_TYPE_C
;
433 if (card
->host
->caps
& MMC_CAP_DRIVER_TYPE_D
)
434 host_drv_type
|= SD_DRIVER_TYPE_D
;
436 if (card
->sw_caps
.sd3_drv_type
& SD_DRIVER_TYPE_A
)
437 card_drv_type
|= SD_DRIVER_TYPE_A
;
439 if (card
->sw_caps
.sd3_drv_type
& SD_DRIVER_TYPE_C
)
440 card_drv_type
|= SD_DRIVER_TYPE_C
;
442 if (card
->sw_caps
.sd3_drv_type
& SD_DRIVER_TYPE_D
)
443 card_drv_type
|= SD_DRIVER_TYPE_D
;
446 * The drive strength that the hardware can support
447 * depends on the board design. Pass the appropriate
448 * information and let the hardware specific code
449 * return what is possible given the options
451 drive_strength
= card
->host
->ops
->select_drive_strength(
452 card
->sw_caps
.uhs_max_dtr
,
453 host_drv_type
, card_drv_type
);
455 /* if error just use default for drive strength B */
456 err
= mmc_io_rw_direct(card
, 0, 0, SDIO_CCCR_DRIVE_STRENGTH
, 0,
461 card_strength
&= ~(SDIO_DRIVE_DTSx_MASK
<<SDIO_DRIVE_DTSx_SHIFT
);
462 card_strength
|= host_drive_to_sdio_drive(drive_strength
);
464 err
= mmc_io_rw_direct(card
, 1, 0, SDIO_CCCR_DRIVE_STRENGTH
,
465 card_strength
, NULL
);
467 /* if error default to drive strength B */
469 mmc_set_driver_type(card
->host
, drive_strength
);
473 static int sdio_set_bus_speed_mode(struct mmc_card
*card
)
475 unsigned int bus_speed
, timing
;
480 * If the host doesn't support any of the UHS-I modes, fallback on
483 if (!mmc_host_uhs(card
->host
))
486 bus_speed
= SDIO_SPEED_SDR12
;
487 timing
= MMC_TIMING_UHS_SDR12
;
488 if ((card
->host
->caps
& MMC_CAP_UHS_SDR104
) &&
489 (card
->sw_caps
.sd3_bus_mode
& SD_MODE_UHS_SDR104
)) {
490 bus_speed
= SDIO_SPEED_SDR104
;
491 timing
= MMC_TIMING_UHS_SDR104
;
492 card
->sw_caps
.uhs_max_dtr
= UHS_SDR104_MAX_DTR
;
493 card
->sd_bus_speed
= UHS_SDR104_BUS_SPEED
;
494 } else if ((card
->host
->caps
& MMC_CAP_UHS_DDR50
) &&
495 (card
->sw_caps
.sd3_bus_mode
& SD_MODE_UHS_DDR50
)) {
496 bus_speed
= SDIO_SPEED_DDR50
;
497 timing
= MMC_TIMING_UHS_DDR50
;
498 card
->sw_caps
.uhs_max_dtr
= UHS_DDR50_MAX_DTR
;
499 card
->sd_bus_speed
= UHS_DDR50_BUS_SPEED
;
500 } else if ((card
->host
->caps
& (MMC_CAP_UHS_SDR104
|
501 MMC_CAP_UHS_SDR50
)) && (card
->sw_caps
.sd3_bus_mode
&
502 SD_MODE_UHS_SDR50
)) {
503 bus_speed
= SDIO_SPEED_SDR50
;
504 timing
= MMC_TIMING_UHS_SDR50
;
505 card
->sw_caps
.uhs_max_dtr
= UHS_SDR50_MAX_DTR
;
506 card
->sd_bus_speed
= UHS_SDR50_BUS_SPEED
;
507 } else if ((card
->host
->caps
& (MMC_CAP_UHS_SDR104
|
508 MMC_CAP_UHS_SDR50
| MMC_CAP_UHS_SDR25
)) &&
509 (card
->sw_caps
.sd3_bus_mode
& SD_MODE_UHS_SDR25
)) {
510 bus_speed
= SDIO_SPEED_SDR25
;
511 timing
= MMC_TIMING_UHS_SDR25
;
512 card
->sw_caps
.uhs_max_dtr
= UHS_SDR25_MAX_DTR
;
513 card
->sd_bus_speed
= UHS_SDR25_BUS_SPEED
;
514 } else if ((card
->host
->caps
& (MMC_CAP_UHS_SDR104
|
515 MMC_CAP_UHS_SDR50
| MMC_CAP_UHS_SDR25
|
516 MMC_CAP_UHS_SDR12
)) && (card
->sw_caps
.sd3_bus_mode
&
517 SD_MODE_UHS_SDR12
)) {
518 bus_speed
= SDIO_SPEED_SDR12
;
519 timing
= MMC_TIMING_UHS_SDR12
;
520 card
->sw_caps
.uhs_max_dtr
= UHS_SDR12_MAX_DTR
;
521 card
->sd_bus_speed
= UHS_SDR12_BUS_SPEED
;
524 err
= mmc_io_rw_direct(card
, 0, 0, SDIO_CCCR_SPEED
, 0, &speed
);
528 speed
&= ~SDIO_SPEED_BSS_MASK
;
530 err
= mmc_io_rw_direct(card
, 1, 0, SDIO_CCCR_SPEED
, speed
, NULL
);
535 mmc_set_timing(card
->host
, timing
);
536 mmc_set_clock(card
->host
, card
->sw_caps
.uhs_max_dtr
);
543 * UHS-I specific initialization procedure
545 static int mmc_sdio_init_uhs_card(struct mmc_card
*card
)
549 if (!card
->scr
.sda_spec3
)
553 * Switch to wider bus (if supported).
555 if (card
->host
->caps
& MMC_CAP_4_BIT_DATA
) {
556 err
= sdio_enable_4bit_bus(card
);
558 mmc_set_bus_width(card
->host
, MMC_BUS_WIDTH_4
);
563 /* Set the driver strength for the card */
564 sdio_select_driver_type(card
);
566 /* Set bus speed mode of the card */
567 err
= sdio_set_bus_speed_mode(card
);
571 /* Initialize and start re-tuning timer */
572 if (!mmc_host_is_spi(card
->host
) && card
->host
->ops
->execute_tuning
)
573 err
= card
->host
->ops
->execute_tuning(card
->host
,
574 MMC_SEND_TUNING_BLOCK
);
582 * Handle the detection and initialisation of a card.
584 * In the case of a resume, "oldcard" will contain the card
585 * we're trying to reinitialise.
587 static int mmc_sdio_init_card(struct mmc_host
*host
, u32 ocr
,
588 struct mmc_card
*oldcard
, int powered_resume
)
590 struct mmc_card
*card
;
595 WARN_ON(!host
->claimed
);
599 pr_warning("%s: Skipping voltage switch\n",
601 ocr
&= ~R4_18V_PRESENT
;
602 host
->ocr
&= ~R4_18V_PRESENT
;
606 * Inform the card of the voltage
608 if (!powered_resume
) {
609 err
= mmc_send_io_op_cond(host
, host
->ocr
, &ocr
);
615 * For SPI, enable CRC as appropriate.
617 if (mmc_host_is_spi(host
)) {
618 err
= mmc_spi_set_crc(host
, use_spi_crc
);
624 * Allocate card structure.
626 card
= mmc_alloc_card(host
, NULL
);
632 if ((ocr
& R4_MEMORY_PRESENT
) &&
633 mmc_sd_get_cid(host
, host
->ocr
& ocr
, card
->raw_cid
, NULL
) == 0) {
634 card
->type
= MMC_TYPE_SD_COMBO
;
636 if (oldcard
&& (oldcard
->type
!= MMC_TYPE_SD_COMBO
||
637 memcmp(card
->raw_cid
, oldcard
->raw_cid
, sizeof(card
->raw_cid
)) != 0)) {
638 mmc_remove_card(card
);
642 card
->type
= MMC_TYPE_SDIO
;
644 if (oldcard
&& oldcard
->type
!= MMC_TYPE_SDIO
) {
645 mmc_remove_card(card
);
651 * Call the optional HC's init_card function to handle quirks.
653 if (host
->ops
->init_card
)
654 host
->ops
->init_card(host
, card
);
657 * If the host and card support UHS-I mode request the card
658 * to switch to 1.8V signaling level. No 1.8v signalling if
659 * UHS mode is not enabled to maintain compatibility and some
660 * systems that claim 1.8v signalling in fact do not support
663 if (!powered_resume
&& (ocr
& R4_18V_PRESENT
) && mmc_host_uhs(host
)) {
664 err
= mmc_set_signal_voltage(host
, MMC_SIGNAL_VOLTAGE_180
);
665 if (err
== -EAGAIN
) {
668 mmc_send_if_cond(host
, host
->ocr_avail
);
669 mmc_remove_card(card
);
673 ocr
&= ~R4_18V_PRESENT
;
674 host
->ocr
&= ~R4_18V_PRESENT
;
678 ocr
&= ~R4_18V_PRESENT
;
679 host
->ocr
&= ~R4_18V_PRESENT
;
683 * For native busses: set card RCA and quit open drain mode.
685 if (!powered_resume
&& !mmc_host_is_spi(host
)) {
686 err
= mmc_send_relative_addr(host
, &card
->rca
);
691 * Update oldcard with the new RCA received from the SDIO
692 * device -- we're doing this so that it's updated in the
693 * "card" struct when oldcard overwrites that later.
696 oldcard
->rca
= card
->rca
;
700 * Read CSD, before selecting the card
702 if (!oldcard
&& card
->type
== MMC_TYPE_SD_COMBO
) {
703 err
= mmc_sd_get_csd(host
, card
);
707 mmc_decode_cid(card
);
711 * Select card, as all following commands rely on that.
713 if (!powered_resume
&& !mmc_host_is_spi(host
)) {
714 err
= mmc_select_card(card
);
719 if (card
->quirks
& MMC_QUIRK_NONSTD_SDIO
) {
721 * This is non-standard SDIO device, meaning it doesn't
722 * have any CIA (Common I/O area) registers present.
723 * It's host's responsibility to fill cccr and cis
724 * structures in init_card().
726 mmc_set_clock(host
, card
->cis
.max_dtr
);
728 if (card
->cccr
.high_speed
) {
729 mmc_card_set_highspeed(card
);
730 mmc_set_timing(card
->host
, MMC_TIMING_SD_HS
);
736 #ifdef CONFIG_MMC_EMBEDDED_SDIO
737 if (host
->embedded_sdio_data
.cccr
)
738 memcpy(&card
->cccr
, host
->embedded_sdio_data
.cccr
, sizeof(struct sdio_cccr
));
742 * Read the common registers.
744 err
= sdio_read_cccr(card
, ocr
);
747 #ifdef CONFIG_MMC_EMBEDDED_SDIO
751 #ifdef CONFIG_MMC_EMBEDDED_SDIO
752 if (host
->embedded_sdio_data
.cis
)
753 memcpy(&card
->cis
, host
->embedded_sdio_data
.cis
, sizeof(struct sdio_cis
));
757 * Read the common CIS tuples.
759 err
= sdio_read_common_cis(card
);
762 #ifdef CONFIG_MMC_EMBEDDED_SDIO
767 int same
= (card
->cis
.vendor
== oldcard
->cis
.vendor
&&
768 card
->cis
.device
== oldcard
->cis
.device
);
769 mmc_remove_card(card
);
775 mmc_fixup_device(card
, NULL
);
777 if (card
->type
== MMC_TYPE_SD_COMBO
) {
778 err
= mmc_sd_setup_card(host
, card
, oldcard
!= NULL
);
779 /* handle as SDIO-only card if memory init failed */
782 if (mmc_host_is_spi(host
))
783 /* should not fail, as it worked previously */
784 mmc_spi_set_crc(host
, use_spi_crc
);
785 card
->type
= MMC_TYPE_SDIO
;
787 card
->dev
.type
= &sd_type
;
791 * If needed, disconnect card detection pull-up resistor.
793 err
= sdio_disable_cd(card
);
797 /* Initialization sequence for UHS-I cards */
798 /* Only if card supports 1.8v and UHS signaling */
799 if ((ocr
& R4_18V_PRESENT
) && card
->sw_caps
.sd3_bus_mode
) {
800 err
= mmc_sdio_init_uhs_card(card
);
804 /* Card is an ultra-high-speed card */
805 mmc_card_set_uhs(card
);
808 * Switch to high-speed (if supported).
810 err
= sdio_enable_hs(card
);
812 mmc_sd_go_highspeed(card
);
817 * Change to the card's maximum speed.
819 mmc_set_clock(host
, mmc_sdio_get_max_clock(card
));
822 * Switch to wider bus (if supported).
824 err
= sdio_enable_4bit_bus(card
);
826 mmc_set_bus_width(card
->host
, MMC_BUS_WIDTH_4
);
837 mmc_remove_card(card
);
844 * Host is being removed. Free up the current card.
846 static void mmc_sdio_remove(struct mmc_host
*host
)
853 for (i
= 0;i
< host
->card
->sdio_funcs
;i
++) {
854 if (host
->card
->sdio_func
[i
]) {
855 sdio_remove_func(host
->card
->sdio_func
[i
]);
856 host
->card
->sdio_func
[i
] = NULL
;
860 mmc_remove_card(host
->card
);
865 * Card detection - card is alive.
867 static int mmc_sdio_alive(struct mmc_host
*host
)
869 return mmc_select_card(host
->card
);
873 * Card detection callback from host.
875 static void mmc_sdio_detect(struct mmc_host
*host
)
882 /* Make sure card is powered before detecting it */
883 if (host
->caps
& MMC_CAP_POWER_OFF_CARD
) {
884 err
= pm_runtime_get_sync(&host
->card
->dev
);
886 pm_runtime_put_noidle(&host
->card
->dev
);
891 mmc_claim_host(host
);
894 * Just check if our card has been removed.
896 err
= _mmc_detect_card_removed(host
);
898 mmc_release_host(host
);
901 * Tell PM core it's OK to power off the card now.
903 * The _sync variant is used in order to ensure that the card
904 * is left powered off in case an error occurred, and the card
905 * is going to be removed.
907 * Since there is no specific reason to believe a new user
908 * is about to show up at this point, the _sync variant is
911 if (host
->caps
& MMC_CAP_POWER_OFF_CARD
)
912 pm_runtime_put_sync(&host
->card
->dev
);
916 mmc_sdio_remove(host
);
918 mmc_claim_host(host
);
919 mmc_detach_bus(host
);
921 mmc_release_host(host
);
926 * SDIO suspend. We need to suspend all functions separately.
927 * Therefore all registered functions must have drivers with suspend
928 * and resume methods. Failing that we simply remove the whole card.
930 static int mmc_sdio_suspend(struct mmc_host
*host
)
934 for (i
= 0; i
< host
->card
->sdio_funcs
; i
++) {
935 struct sdio_func
*func
= host
->card
->sdio_func
[i
];
936 if (func
&& sdio_func_present(func
) && func
->dev
.driver
) {
937 const struct dev_pm_ops
*pmops
= func
->dev
.driver
->pm
;
938 if (!pmops
|| !pmops
->suspend
|| !pmops
->resume
) {
939 /* force removal of entire card in that case */
942 err
= pmops
->suspend(&func
->dev
);
947 while (err
&& --i
>= 0) {
948 struct sdio_func
*func
= host
->card
->sdio_func
[i
];
949 if (func
&& sdio_func_present(func
) && func
->dev
.driver
) {
950 const struct dev_pm_ops
*pmops
= func
->dev
.driver
->pm
;
951 pmops
->resume(&func
->dev
);
955 if (!err
&& mmc_card_keep_power(host
) && mmc_card_wake_sdio_irq(host
)) {
956 mmc_claim_host(host
);
957 sdio_disable_wide(host
->card
);
958 mmc_release_host(host
);
964 static int mmc_sdio_resume(struct mmc_host
*host
)
971 /* Basic card reinitialization. */
972 mmc_claim_host(host
);
974 /* No need to reinitialize powered-resumed nonremovable cards */
975 if (mmc_card_is_removable(host
) || !mmc_card_keep_power(host
)) {
978 err
= mmc_sdio_init_card(host
, host
->ocr
, host
->card
,
979 mmc_card_keep_power(host
));
980 } else if (mmc_card_keep_power(host
) && mmc_card_wake_sdio_irq(host
)) {
981 /* We may have switched to 1-bit mode during suspend */
982 err
= sdio_enable_4bit_bus(host
->card
);
984 mmc_set_bus_width(host
, MMC_BUS_WIDTH_4
);
989 if (!err
&& host
->sdio_irqs
)
990 wake_up_process(host
->sdio_irq_thread
);
991 mmc_release_host(host
);
994 * If the card looked to be the same as before suspending, then
995 * we proceed to resume all card functions. If one of them returns
996 * an error then we simply return that error to the core and the
997 * card will be redetected as new. It is the responsibility of
998 * the function driver to perform further tests with the extra
999 * knowledge it has of the card to confirm the card is indeed the
1000 * same as before suspending (same MAC address for network cards,
1001 * etc.) and return an error otherwise.
1003 for (i
= 0; !err
&& i
< host
->card
->sdio_funcs
; i
++) {
1004 struct sdio_func
*func
= host
->card
->sdio_func
[i
];
1005 if (func
&& sdio_func_present(func
) && func
->dev
.driver
) {
1006 const struct dev_pm_ops
*pmops
= func
->dev
.driver
->pm
;
1007 err
= pmops
->resume(&func
->dev
);
1014 static int mmc_sdio_power_restore(struct mmc_host
*host
)
1020 BUG_ON(!host
->card
);
1022 mmc_claim_host(host
);
1025 * Reset the card by performing the same steps that are taken by
1026 * mmc_rescan_try_freq() and mmc_attach_sdio() during a "normal" probe.
1028 * sdio_reset() is technically not needed. Having just powered up the
1029 * hardware, it should already be in reset state. However, some
1030 * platforms (such as SD8686 on OLPC) do not instantly cut power,
1031 * meaning that a reset is required when restoring power soon after
1032 * powering off. It is harmless in other cases.
1034 * The CMD5 reset (mmc_send_io_op_cond()), according to the SDIO spec,
1035 * is not necessary for non-removable cards. However, it is required
1036 * for OLPC SD8686 (which expects a [CMD5,5,3,7] init sequence), and
1037 * harmless in other situations.
1039 * With these steps taken, mmc_select_voltage() is also required to
1040 * restore the correct voltage setting of the card.
1045 mmc_send_if_cond(host
, host
->ocr_avail
);
1047 ret
= mmc_send_io_op_cond(host
, 0, &ocr
);
1051 if (host
->ocr_avail_sdio
)
1052 host
->ocr_avail
= host
->ocr_avail_sdio
;
1054 host
->ocr
= mmc_select_voltage(host
, ocr
& ~0x7F);
1060 if (mmc_host_uhs(host
))
1061 /* to query card if 1.8V signalling is supported */
1062 host
->ocr
|= R4_18V_PRESENT
;
1064 ret
= mmc_sdio_init_card(host
, host
->ocr
, host
->card
,
1065 mmc_card_keep_power(host
));
1066 if (!ret
&& host
->sdio_irqs
)
1067 mmc_signal_sdio_irq(host
);
1070 mmc_release_host(host
);
1075 static const struct mmc_bus_ops mmc_sdio_ops
= {
1076 .remove
= mmc_sdio_remove
,
1077 .detect
= mmc_sdio_detect
,
1078 .suspend
= mmc_sdio_suspend
,
1079 .resume
= mmc_sdio_resume
,
1080 .power_restore
= mmc_sdio_power_restore
,
1081 .alive
= mmc_sdio_alive
,
1086 * Starting point for SDIO card init.
1088 int mmc_attach_sdio(struct mmc_host
*host
)
1092 struct mmc_card
*card
;
1095 WARN_ON(!host
->claimed
);
1097 err
= mmc_send_io_op_cond(host
, 0, &ocr
);
1101 mmc_attach_bus(host
, &mmc_sdio_ops
);
1102 if (host
->ocr_avail_sdio
)
1103 host
->ocr_avail
= host
->ocr_avail_sdio
;
1106 * Sanity check the voltages that the card claims to
1110 pr_warning("%s: card claims to support voltages "
1111 "below the defined range. These will be ignored.\n",
1112 mmc_hostname(host
));
1116 host
->ocr
= mmc_select_voltage(host
, ocr
);
1119 * Can we support the voltage(s) of the card(s)?
1127 * Detect and init the card.
1129 if (mmc_host_uhs(host
))
1130 /* to query card if 1.8V signalling is supported */
1131 host
->ocr
|= R4_18V_PRESENT
;
1133 err
= mmc_sdio_init_card(host
, host
->ocr
, NULL
, 0);
1135 if (err
== -EAGAIN
) {
1137 * Retry initialization with S18R set to 0.
1139 host
->ocr
&= ~R4_18V_PRESENT
;
1140 err
= mmc_sdio_init_card(host
, host
->ocr
, NULL
, 0);
1148 * Enable runtime PM only if supported by host+card+board
1150 if (host
->caps
& MMC_CAP_POWER_OFF_CARD
) {
1152 * Let runtime PM core know our card is active
1154 err
= pm_runtime_set_active(&card
->dev
);
1159 * Enable runtime PM for this card
1161 pm_runtime_enable(&card
->dev
);
1165 * The number of functions on the card is encoded inside
1168 funcs
= (ocr
& 0x70000000) >> 28;
1169 card
->sdio_funcs
= 0;
1171 #ifdef CONFIG_MMC_EMBEDDED_SDIO
1172 if (host
->embedded_sdio_data
.funcs
)
1173 card
->sdio_funcs
= funcs
= host
->embedded_sdio_data
.num_funcs
;
1177 * Initialize (but don't add) all present functions.
1179 for (i
= 0; i
< funcs
; i
++, card
->sdio_funcs
++) {
1180 #ifdef CONFIG_MMC_EMBEDDED_SDIO
1181 if (host
->embedded_sdio_data
.funcs
) {
1182 struct sdio_func
*tmp
;
1184 tmp
= sdio_alloc_func(host
->card
);
1188 card
->sdio_func
[i
] = tmp
;
1189 tmp
->class = host
->embedded_sdio_data
.funcs
[i
].f_class
;
1190 tmp
->max_blksize
= host
->embedded_sdio_data
.funcs
[i
].f_maxblksize
;
1191 tmp
->vendor
= card
->cis
.vendor
;
1192 tmp
->device
= card
->cis
.device
;
1195 err
= sdio_init_func(host
->card
, i
+ 1);
1198 #ifdef CONFIG_MMC_EMBEDDED_SDIO
1202 * Enable Runtime PM for this func (if supported)
1204 if (host
->caps
& MMC_CAP_POWER_OFF_CARD
)
1205 pm_runtime_enable(&card
->sdio_func
[i
]->dev
);
1209 * First add the card to the driver model...
1211 mmc_release_host(host
);
1212 err
= mmc_add_card(host
->card
);
1217 * ...then the SDIO functions.
1219 for (i
= 0;i
< funcs
;i
++) {
1220 err
= sdio_add_func(host
->card
->sdio_func
[i
]);
1225 mmc_claim_host(host
);
1230 /* Remove without lock if the device has been added. */
1231 mmc_sdio_remove(host
);
1232 mmc_claim_host(host
);
1234 /* And with lock if it hasn't been added. */
1235 mmc_release_host(host
);
1237 mmc_sdio_remove(host
);
1238 mmc_claim_host(host
);
1240 mmc_detach_bus(host
);
1242 pr_err("%s: error %d whilst initialising SDIO card\n",
1243 mmc_hostname(host
), err
);
1248 int sdio_reset_comm(struct mmc_card
*card
)
1250 struct mmc_host
*host
= card
->host
;
1254 printk("%s():\n", __func__
);
1255 mmc_claim_host(host
);
1259 mmc_set_clock(host
, host
->f_min
);
1261 err
= mmc_send_io_op_cond(host
, 0, &ocr
);
1265 host
->ocr
= mmc_select_voltage(host
, ocr
);
1271 err
= mmc_sdio_init_card(host
, host
->ocr
, card
, 0);
1275 mmc_release_host(host
);
1278 printk("%s: Error resetting SDIO communications (%d)\n",
1279 mmc_hostname(host
), err
);
1280 mmc_release_host(host
);
1283 EXPORT_SYMBOL(sdio_reset_comm
);