2 * linux/drivers/mmc/sdio.c
4 * Copyright 2006-2007 Pierre Ossman
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or (at
9 * your option) any later version.
12 #include <linux/err.h>
13 #include <linux/pm_runtime.h>
15 #include <linux/mmc/host.h>
16 #include <linux/mmc/card.h>
17 #include <linux/mmc/sdio.h>
18 #include <linux/mmc/sdio_func.h>
29 static int sdio_read_fbr(struct sdio_func
*func
)
34 ret
= mmc_io_rw_direct(func
->card
, 0, 0,
35 SDIO_FBR_BASE(func
->num
) + SDIO_FBR_STD_IF
, 0, &data
);
42 ret
= mmc_io_rw_direct(func
->card
, 0, 0,
43 SDIO_FBR_BASE(func
->num
) + SDIO_FBR_STD_IF_EXT
, 0, &data
);
54 static int sdio_init_func(struct mmc_card
*card
, unsigned int fn
)
57 struct sdio_func
*func
;
59 BUG_ON(fn
> SDIO_MAX_FUNCS
);
61 func
= sdio_alloc_func(card
);
67 if (!(card
->quirks
& MMC_QUIRK_NONSTD_SDIO
)) {
68 ret
= sdio_read_fbr(func
);
72 ret
= sdio_read_func_cis(func
);
76 func
->vendor
= func
->card
->cis
.vendor
;
77 func
->device
= func
->card
->cis
.device
;
78 func
->max_blksize
= func
->card
->cis
.blksize
;
81 card
->sdio_func
[fn
- 1] = func
;
87 * It is okay to remove the function here even though we hold
88 * the host lock as we haven't registered the device yet.
90 sdio_remove_func(func
);
94 static int sdio_read_cccr(struct mmc_card
*card
)
100 memset(&card
->cccr
, 0, sizeof(struct sdio_cccr
));
102 ret
= mmc_io_rw_direct(card
, 0, 0, SDIO_CCCR_CCCR
, 0, &data
);
106 cccr_vsn
= data
& 0x0f;
108 if (cccr_vsn
> SDIO_CCCR_REV_1_20
) {
109 printk(KERN_ERR
"%s: unrecognised CCCR structure version %d\n",
110 mmc_hostname(card
->host
), cccr_vsn
);
114 card
->cccr
.sdio_vsn
= (data
& 0xf0) >> 4;
116 ret
= mmc_io_rw_direct(card
, 0, 0, SDIO_CCCR_CAPS
, 0, &data
);
120 if (data
& SDIO_CCCR_CAP_SMB
)
121 card
->cccr
.multi_block
= 1;
122 if (data
& SDIO_CCCR_CAP_LSC
)
123 card
->cccr
.low_speed
= 1;
124 if (data
& SDIO_CCCR_CAP_4BLS
)
125 card
->cccr
.wide_bus
= 1;
127 if (cccr_vsn
>= SDIO_CCCR_REV_1_10
) {
128 ret
= mmc_io_rw_direct(card
, 0, 0, SDIO_CCCR_POWER
, 0, &data
);
132 if (data
& SDIO_POWER_SMPC
)
133 card
->cccr
.high_power
= 1;
136 if (cccr_vsn
>= SDIO_CCCR_REV_1_20
) {
137 ret
= mmc_io_rw_direct(card
, 0, 0, SDIO_CCCR_SPEED
, 0, &data
);
141 if (data
& SDIO_SPEED_SHS
)
142 card
->cccr
.high_speed
= 1;
149 static int sdio_enable_wide(struct mmc_card
*card
)
154 if (!(card
->host
->caps
& MMC_CAP_4_BIT_DATA
))
157 if (card
->cccr
.low_speed
&& !card
->cccr
.wide_bus
)
160 ret
= mmc_io_rw_direct(card
, 0, 0, SDIO_CCCR_IF
, 0, &ctrl
);
164 ctrl
|= SDIO_BUS_WIDTH_4BIT
;
166 ret
= mmc_io_rw_direct(card
, 1, 0, SDIO_CCCR_IF
, ctrl
, NULL
);
174 * If desired, disconnect the pull-up resistor on CD/DAT[3] (pin 1)
175 * of the card. This may be required on certain setups of boards,
176 * controllers and embedded sdio device which do not need the card's
177 * pull-up. As a result, card detection is disabled and power is saved.
179 static int sdio_disable_cd(struct mmc_card
*card
)
184 if (!card
->cccr
.disable_cd
)
187 ret
= mmc_io_rw_direct(card
, 0, 0, SDIO_CCCR_IF
, 0, &ctrl
);
191 ctrl
|= SDIO_BUS_CD_DISABLE
;
193 return mmc_io_rw_direct(card
, 1, 0, SDIO_CCCR_IF
, ctrl
, NULL
);
197 * Devices that remain active during a system suspend are
198 * put back into 1-bit mode.
200 static int sdio_disable_wide(struct mmc_card
*card
)
205 if (!(card
->host
->caps
& MMC_CAP_4_BIT_DATA
))
208 if (card
->cccr
.low_speed
&& !card
->cccr
.wide_bus
)
211 ret
= mmc_io_rw_direct(card
, 0, 0, SDIO_CCCR_IF
, 0, &ctrl
);
215 if (!(ctrl
& SDIO_BUS_WIDTH_4BIT
))
218 ctrl
&= ~SDIO_BUS_WIDTH_4BIT
;
219 ctrl
|= SDIO_BUS_ASYNC_INT
;
221 ret
= mmc_io_rw_direct(card
, 1, 0, SDIO_CCCR_IF
, ctrl
, NULL
);
225 mmc_set_bus_width(card
->host
, MMC_BUS_WIDTH_1
);
231 static int sdio_enable_4bit_bus(struct mmc_card
*card
)
235 if (card
->type
== MMC_TYPE_SDIO
)
236 return sdio_enable_wide(card
);
238 if ((card
->host
->caps
& MMC_CAP_4_BIT_DATA
) &&
239 (card
->scr
.bus_widths
& SD_SCR_BUS_WIDTH_4
)) {
240 err
= mmc_app_set_bus_width(card
, MMC_BUS_WIDTH_4
);
246 err
= sdio_enable_wide(card
);
248 mmc_app_set_bus_width(card
, MMC_BUS_WIDTH_1
);
255 * Test if the card supports high-speed mode and, if so, switch to it.
257 static int mmc_sdio_switch_hs(struct mmc_card
*card
, int enable
)
262 if (!(card
->host
->caps
& MMC_CAP_SD_HIGHSPEED
))
265 if (!card
->cccr
.high_speed
)
268 ret
= mmc_io_rw_direct(card
, 0, 0, SDIO_CCCR_SPEED
, 0, &speed
);
273 speed
|= SDIO_SPEED_EHS
;
275 speed
&= ~SDIO_SPEED_EHS
;
277 ret
= mmc_io_rw_direct(card
, 1, 0, SDIO_CCCR_SPEED
, speed
, NULL
);
285 * Enable SDIO/combo card's high-speed mode. Return 0/1 if [not]supported.
287 static int sdio_enable_hs(struct mmc_card
*card
)
291 ret
= mmc_sdio_switch_hs(card
, true);
292 if (ret
<= 0 || card
->type
== MMC_TYPE_SDIO
)
295 ret
= mmc_sd_switch_hs(card
);
297 mmc_sdio_switch_hs(card
, false);
302 static unsigned mmc_sdio_get_max_clock(struct mmc_card
*card
)
306 if (mmc_card_highspeed(card
)) {
308 * The SDIO specification doesn't mention how
309 * the CIS transfer speed register relates to
310 * high-speed, but it seems that 50 MHz is
315 max_dtr
= card
->cis
.max_dtr
;
318 if (card
->type
== MMC_TYPE_SD_COMBO
)
319 max_dtr
= min(max_dtr
, mmc_sd_get_max_clock(card
));
325 * Handle the detection and initialisation of a card.
327 * In the case of a resume, "oldcard" will contain the card
328 * we're trying to reinitialise.
330 static int mmc_sdio_init_card(struct mmc_host
*host
, u32 ocr
,
331 struct mmc_card
*oldcard
, int powered_resume
)
333 struct mmc_card
*card
;
337 WARN_ON(!host
->claimed
);
340 * Inform the card of the voltage
342 if (!powered_resume
) {
343 err
= mmc_send_io_op_cond(host
, host
->ocr
, &ocr
);
349 * For SPI, enable CRC as appropriate.
351 if (mmc_host_is_spi(host
)) {
352 err
= mmc_spi_set_crc(host
, use_spi_crc
);
358 * Allocate card structure.
360 card
= mmc_alloc_card(host
, NULL
);
366 if (ocr
& R4_MEMORY_PRESENT
367 && mmc_sd_get_cid(host
, host
->ocr
& ocr
, card
->raw_cid
) == 0) {
368 card
->type
= MMC_TYPE_SD_COMBO
;
370 if (oldcard
&& (oldcard
->type
!= MMC_TYPE_SD_COMBO
||
371 memcmp(card
->raw_cid
, oldcard
->raw_cid
, sizeof(card
->raw_cid
)) != 0)) {
372 mmc_remove_card(card
);
376 card
->type
= MMC_TYPE_SDIO
;
378 if (oldcard
&& oldcard
->type
!= MMC_TYPE_SDIO
) {
379 mmc_remove_card(card
);
385 * Call the optional HC's init_card function to handle quirks.
387 if (host
->ops
->init_card
)
388 host
->ops
->init_card(host
, card
);
391 * For native busses: set card RCA and quit open drain mode.
393 if (!powered_resume
&& !mmc_host_is_spi(host
)) {
394 err
= mmc_send_relative_addr(host
, &card
->rca
);
399 * Update oldcard with the new RCA received from the SDIO
400 * device -- we're doing this so that it's updated in the
401 * "card" struct when oldcard overwrites that later.
404 oldcard
->rca
= card
->rca
;
406 mmc_set_bus_mode(host
, MMC_BUSMODE_PUSHPULL
);
410 * Read CSD, before selecting the card
412 if (!oldcard
&& card
->type
== MMC_TYPE_SD_COMBO
) {
413 err
= mmc_sd_get_csd(host
, card
);
417 mmc_decode_cid(card
);
421 * Select card, as all following commands rely on that.
423 if (!powered_resume
&& !mmc_host_is_spi(host
)) {
424 err
= mmc_select_card(card
);
429 if (card
->quirks
& MMC_QUIRK_NONSTD_SDIO
) {
431 * This is non-standard SDIO device, meaning it doesn't
432 * have any CIA (Common I/O area) registers present.
433 * It's host's responsibility to fill cccr and cis
434 * structures in init_card().
436 mmc_set_clock(host
, card
->cis
.max_dtr
);
438 if (card
->cccr
.high_speed
) {
439 mmc_card_set_highspeed(card
);
440 mmc_set_timing(card
->host
, MMC_TIMING_SD_HS
);
447 * Read the common registers.
449 err
= sdio_read_cccr(card
);
454 * Read the common CIS tuples.
456 err
= sdio_read_common_cis(card
);
461 int same
= (card
->cis
.vendor
== oldcard
->cis
.vendor
&&
462 card
->cis
.device
== oldcard
->cis
.device
);
463 mmc_remove_card(card
);
469 mmc_fixup_device(card
);
471 if (card
->type
== MMC_TYPE_SD_COMBO
) {
472 err
= mmc_sd_setup_card(host
, card
, oldcard
!= NULL
);
473 /* handle as SDIO-only card if memory init failed */
476 if (mmc_host_is_spi(host
))
477 /* should not fail, as it worked previously */
478 mmc_spi_set_crc(host
, use_spi_crc
);
479 card
->type
= MMC_TYPE_SDIO
;
481 card
->dev
.type
= &sd_type
;
485 * If needed, disconnect card detection pull-up resistor.
487 err
= sdio_disable_cd(card
);
492 * Switch to high-speed (if supported).
494 err
= sdio_enable_hs(card
);
496 mmc_sd_go_highspeed(card
);
501 * Change to the card's maximum speed.
503 mmc_set_clock(host
, mmc_sdio_get_max_clock(card
));
506 * Switch to wider bus (if supported).
508 err
= sdio_enable_4bit_bus(card
);
510 mmc_set_bus_width(card
->host
, MMC_BUS_WIDTH_4
);
521 mmc_remove_card(card
);
528 * Host is being removed. Free up the current card.
530 static void mmc_sdio_remove(struct mmc_host
*host
)
537 for (i
= 0;i
< host
->card
->sdio_funcs
;i
++) {
538 if (host
->card
->sdio_func
[i
]) {
539 sdio_remove_func(host
->card
->sdio_func
[i
]);
540 host
->card
->sdio_func
[i
] = NULL
;
544 mmc_remove_card(host
->card
);
549 * Card detection callback from host.
551 static void mmc_sdio_detect(struct mmc_host
*host
)
558 /* Make sure card is powered before detecting it */
559 if (host
->caps
& MMC_CAP_POWER_OFF_CARD
) {
560 err
= pm_runtime_get_sync(&host
->card
->dev
);
565 mmc_claim_host(host
);
568 * Just check if our card has been removed.
570 err
= mmc_select_card(host
->card
);
572 mmc_release_host(host
);
575 * Tell PM core it's OK to power off the card now.
577 * The _sync variant is used in order to ensure that the card
578 * is left powered off in case an error occurred, and the card
579 * is going to be removed.
581 * Since there is no specific reason to believe a new user
582 * is about to show up at this point, the _sync variant is
585 if (host
->caps
& MMC_CAP_POWER_OFF_CARD
)
586 pm_runtime_put_sync(&host
->card
->dev
);
590 mmc_sdio_remove(host
);
592 mmc_claim_host(host
);
593 mmc_detach_bus(host
);
594 mmc_release_host(host
);
599 * SDIO suspend. We need to suspend all functions separately.
600 * Therefore all registered functions must have drivers with suspend
601 * and resume methods. Failing that we simply remove the whole card.
603 static int mmc_sdio_suspend(struct mmc_host
*host
)
607 for (i
= 0; i
< host
->card
->sdio_funcs
; i
++) {
608 struct sdio_func
*func
= host
->card
->sdio_func
[i
];
609 if (func
&& sdio_func_present(func
) && func
->dev
.driver
) {
610 const struct dev_pm_ops
*pmops
= func
->dev
.driver
->pm
;
611 if (!pmops
|| !pmops
->suspend
|| !pmops
->resume
) {
612 /* force removal of entire card in that case */
615 err
= pmops
->suspend(&func
->dev
);
620 while (err
&& --i
>= 0) {
621 struct sdio_func
*func
= host
->card
->sdio_func
[i
];
622 if (func
&& sdio_func_present(func
) && func
->dev
.driver
) {
623 const struct dev_pm_ops
*pmops
= func
->dev
.driver
->pm
;
624 pmops
->resume(&func
->dev
);
628 if (!err
&& host
->pm_flags
& MMC_PM_KEEP_POWER
) {
629 mmc_claim_host(host
);
630 sdio_disable_wide(host
->card
);
631 mmc_release_host(host
);
637 static int mmc_sdio_resume(struct mmc_host
*host
)
644 /* Basic card reinitialization. */
645 mmc_claim_host(host
);
647 /* No need to reinitialize powered-resumed nonremovable cards */
648 if (mmc_card_is_removable(host
) || !mmc_card_is_powered_resumed(host
))
649 err
= mmc_sdio_init_card(host
, host
->ocr
, host
->card
,
650 (host
->pm_flags
& MMC_PM_KEEP_POWER
));
651 else if (mmc_card_is_powered_resumed(host
)) {
652 /* We may have switched to 1-bit mode during suspend */
653 err
= sdio_enable_4bit_bus(host
->card
);
655 mmc_set_bus_width(host
, MMC_BUS_WIDTH_4
);
660 if (!err
&& host
->sdio_irqs
)
661 mmc_signal_sdio_irq(host
);
662 mmc_release_host(host
);
665 * If the card looked to be the same as before suspending, then
666 * we proceed to resume all card functions. If one of them returns
667 * an error then we simply return that error to the core and the
668 * card will be redetected as new. It is the responsibility of
669 * the function driver to perform further tests with the extra
670 * knowledge it has of the card to confirm the card is indeed the
671 * same as before suspending (same MAC address for network cards,
672 * etc.) and return an error otherwise.
674 for (i
= 0; !err
&& i
< host
->card
->sdio_funcs
; i
++) {
675 struct sdio_func
*func
= host
->card
->sdio_func
[i
];
676 if (func
&& sdio_func_present(func
) && func
->dev
.driver
) {
677 const struct dev_pm_ops
*pmops
= func
->dev
.driver
->pm
;
678 err
= pmops
->resume(&func
->dev
);
685 static int mmc_sdio_power_restore(struct mmc_host
*host
)
692 mmc_claim_host(host
);
693 ret
= mmc_sdio_init_card(host
, host
->ocr
, host
->card
,
694 (host
->pm_flags
& MMC_PM_KEEP_POWER
));
695 if (!ret
&& host
->sdio_irqs
)
696 mmc_signal_sdio_irq(host
);
697 mmc_release_host(host
);
702 static const struct mmc_bus_ops mmc_sdio_ops
= {
703 .remove
= mmc_sdio_remove
,
704 .detect
= mmc_sdio_detect
,
705 .suspend
= mmc_sdio_suspend
,
706 .resume
= mmc_sdio_resume
,
707 .power_restore
= mmc_sdio_power_restore
,
712 * Starting point for SDIO card init.
714 int mmc_attach_sdio(struct mmc_host
*host
)
718 struct mmc_card
*card
;
721 WARN_ON(!host
->claimed
);
723 err
= mmc_send_io_op_cond(host
, 0, &ocr
);
727 mmc_attach_bus(host
, &mmc_sdio_ops
);
728 if (host
->ocr_avail_sdio
)
729 host
->ocr_avail
= host
->ocr_avail_sdio
;
732 * Sanity check the voltages that the card claims to
736 printk(KERN_WARNING
"%s: card claims to support voltages "
737 "below the defined range. These will be ignored.\n",
742 host
->ocr
= mmc_select_voltage(host
, ocr
);
745 * Can we support the voltage(s) of the card(s)?
753 * Detect and init the card.
755 err
= mmc_sdio_init_card(host
, host
->ocr
, NULL
, 0);
761 * Enable runtime PM only if supported by host+card+board
763 if (host
->caps
& MMC_CAP_POWER_OFF_CARD
) {
765 * Let runtime PM core know our card is active
767 err
= pm_runtime_set_active(&card
->dev
);
772 * Enable runtime PM for this card
774 pm_runtime_enable(&card
->dev
);
778 * The number of functions on the card is encoded inside
781 funcs
= (ocr
& 0x70000000) >> 28;
782 card
->sdio_funcs
= 0;
785 * Initialize (but don't add) all present functions.
787 for (i
= 0; i
< funcs
; i
++, card
->sdio_funcs
++) {
788 err
= sdio_init_func(host
->card
, i
+ 1);
793 * Enable Runtime PM for this func (if supported)
795 if (host
->caps
& MMC_CAP_POWER_OFF_CARD
)
796 pm_runtime_enable(&card
->sdio_func
[i
]->dev
);
800 * First add the card to the driver model...
802 mmc_release_host(host
);
803 err
= mmc_add_card(host
->card
);
808 * ...then the SDIO functions.
810 for (i
= 0;i
< funcs
;i
++) {
811 err
= sdio_add_func(host
->card
->sdio_func
[i
]);
816 mmc_claim_host(host
);
821 /* Remove without lock if the device has been added. */
822 mmc_sdio_remove(host
);
823 mmc_claim_host(host
);
825 /* And with lock if it hasn't been added. */
826 mmc_release_host(host
);
828 mmc_sdio_remove(host
);
829 mmc_claim_host(host
);
831 mmc_detach_bus(host
);
833 printk(KERN_ERR
"%s: error %d whilst initialising SDIO card\n",
834 mmc_hostname(host
), err
);