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>
14 #include <linux/mmc/host.h>
15 #include <linux/mmc/card.h>
16 #include <linux/mmc/sdio.h>
17 #include <linux/mmc/sdio_func.h>
28 static int sdio_read_fbr(struct sdio_func
*func
)
33 ret
= mmc_io_rw_direct(func
->card
, 0, 0,
34 SDIO_FBR_BASE(func
->num
) + SDIO_FBR_STD_IF
, 0, &data
);
41 ret
= mmc_io_rw_direct(func
->card
, 0, 0,
42 SDIO_FBR_BASE(func
->num
) + SDIO_FBR_STD_IF_EXT
, 0, &data
);
53 static int sdio_init_func(struct mmc_card
*card
, unsigned int fn
)
56 struct sdio_func
*func
;
58 BUG_ON(fn
> SDIO_MAX_FUNCS
);
60 func
= sdio_alloc_func(card
);
66 if (!(card
->quirks
& MMC_QUIRK_NONSTD_SDIO
)) {
67 ret
= sdio_read_fbr(func
);
71 ret
= sdio_read_func_cis(func
);
75 func
->vendor
= func
->card
->cis
.vendor
;
76 func
->device
= func
->card
->cis
.device
;
77 func
->max_blksize
= func
->card
->cis
.blksize
;
80 card
->sdio_func
[fn
- 1] = func
;
86 * It is okay to remove the function here even though we hold
87 * the host lock as we haven't registered the device yet.
89 sdio_remove_func(func
);
93 static int sdio_read_cccr(struct mmc_card
*card
)
99 memset(&card
->cccr
, 0, sizeof(struct sdio_cccr
));
101 ret
= mmc_io_rw_direct(card
, 0, 0, SDIO_CCCR_CCCR
, 0, &data
);
105 cccr_vsn
= data
& 0x0f;
107 if (cccr_vsn
> SDIO_CCCR_REV_1_20
) {
108 printk(KERN_ERR
"%s: unrecognised CCCR structure version %d\n",
109 mmc_hostname(card
->host
), cccr_vsn
);
113 card
->cccr
.sdio_vsn
= (data
& 0xf0) >> 4;
115 ret
= mmc_io_rw_direct(card
, 0, 0, SDIO_CCCR_CAPS
, 0, &data
);
119 if (data
& SDIO_CCCR_CAP_SMB
)
120 card
->cccr
.multi_block
= 1;
121 if (data
& SDIO_CCCR_CAP_LSC
)
122 card
->cccr
.low_speed
= 1;
123 if (data
& SDIO_CCCR_CAP_4BLS
)
124 card
->cccr
.wide_bus
= 1;
126 if (cccr_vsn
>= SDIO_CCCR_REV_1_10
) {
127 ret
= mmc_io_rw_direct(card
, 0, 0, SDIO_CCCR_POWER
, 0, &data
);
131 if (data
& SDIO_POWER_SMPC
)
132 card
->cccr
.high_power
= 1;
135 if (cccr_vsn
>= SDIO_CCCR_REV_1_20
) {
136 ret
= mmc_io_rw_direct(card
, 0, 0, SDIO_CCCR_SPEED
, 0, &data
);
140 if (data
& SDIO_SPEED_SHS
)
141 card
->cccr
.high_speed
= 1;
148 static int sdio_enable_wide(struct mmc_card
*card
)
153 if (!(card
->host
->caps
& MMC_CAP_4_BIT_DATA
))
156 if (card
->cccr
.low_speed
&& !card
->cccr
.wide_bus
)
159 ret
= mmc_io_rw_direct(card
, 0, 0, SDIO_CCCR_IF
, 0, &ctrl
);
163 ctrl
|= SDIO_BUS_WIDTH_4BIT
;
165 ret
= mmc_io_rw_direct(card
, 1, 0, SDIO_CCCR_IF
, ctrl
, NULL
);
173 * If desired, disconnect the pull-up resistor on CD/DAT[3] (pin 1)
174 * of the card. This may be required on certain setups of boards,
175 * controllers and embedded sdio device which do not need the card's
176 * pull-up. As a result, card detection is disabled and power is saved.
178 static int sdio_disable_cd(struct mmc_card
*card
)
183 if (!card
->cccr
.disable_cd
)
186 ret
= mmc_io_rw_direct(card
, 0, 0, SDIO_CCCR_IF
, 0, &ctrl
);
190 ctrl
|= SDIO_BUS_CD_DISABLE
;
192 return mmc_io_rw_direct(card
, 1, 0, SDIO_CCCR_IF
, ctrl
, NULL
);
196 * Devices that remain active during a system suspend are
197 * put back into 1-bit mode.
199 static int sdio_disable_wide(struct mmc_card
*card
)
204 if (!(card
->host
->caps
& MMC_CAP_4_BIT_DATA
))
207 if (card
->cccr
.low_speed
&& !card
->cccr
.wide_bus
)
210 ret
= mmc_io_rw_direct(card
, 0, 0, SDIO_CCCR_IF
, 0, &ctrl
);
214 if (!(ctrl
& SDIO_BUS_WIDTH_4BIT
))
217 ctrl
&= ~SDIO_BUS_WIDTH_4BIT
;
218 ctrl
|= SDIO_BUS_ASYNC_INT
;
220 ret
= mmc_io_rw_direct(card
, 1, 0, SDIO_CCCR_IF
, ctrl
, NULL
);
224 mmc_set_bus_width(card
->host
, MMC_BUS_WIDTH_1
);
230 static int sdio_enable_4bit_bus(struct mmc_card
*card
)
234 if (card
->type
== MMC_TYPE_SDIO
)
235 return sdio_enable_wide(card
);
237 if ((card
->host
->caps
& MMC_CAP_4_BIT_DATA
) &&
238 (card
->scr
.bus_widths
& SD_SCR_BUS_WIDTH_4
)) {
239 err
= mmc_app_set_bus_width(card
, MMC_BUS_WIDTH_4
);
245 err
= sdio_enable_wide(card
);
247 mmc_app_set_bus_width(card
, MMC_BUS_WIDTH_1
);
254 * Test if the card supports high-speed mode and, if so, switch to it.
256 static int mmc_sdio_switch_hs(struct mmc_card
*card
, int enable
)
261 if (!(card
->host
->caps
& MMC_CAP_SD_HIGHSPEED
))
264 if (!card
->cccr
.high_speed
)
267 ret
= mmc_io_rw_direct(card
, 0, 0, SDIO_CCCR_SPEED
, 0, &speed
);
272 speed
|= SDIO_SPEED_EHS
;
274 speed
&= ~SDIO_SPEED_EHS
;
276 ret
= mmc_io_rw_direct(card
, 1, 0, SDIO_CCCR_SPEED
, speed
, NULL
);
284 * Enable SDIO/combo card's high-speed mode. Return 0/1 if [not]supported.
286 static int sdio_enable_hs(struct mmc_card
*card
)
290 ret
= mmc_sdio_switch_hs(card
, true);
291 if (ret
<= 0 || card
->type
== MMC_TYPE_SDIO
)
294 ret
= mmc_sd_switch_hs(card
);
296 mmc_sdio_switch_hs(card
, false);
301 static unsigned mmc_sdio_get_max_clock(struct mmc_card
*card
)
305 if (mmc_card_highspeed(card
)) {
307 * The SDIO specification doesn't mention how
308 * the CIS transfer speed register relates to
309 * high-speed, but it seems that 50 MHz is
314 max_dtr
= card
->cis
.max_dtr
;
317 if (card
->type
== MMC_TYPE_SD_COMBO
)
318 max_dtr
= min(max_dtr
, mmc_sd_get_max_clock(card
));
324 * Handle the detection and initialisation of a card.
326 * In the case of a resume, "oldcard" will contain the card
327 * we're trying to reinitialise.
329 static int mmc_sdio_init_card(struct mmc_host
*host
, u32 ocr
,
330 struct mmc_card
*oldcard
, int powered_resume
)
332 struct mmc_card
*card
;
336 WARN_ON(!host
->claimed
);
339 * Inform the card of the voltage
341 if (!powered_resume
) {
342 err
= mmc_send_io_op_cond(host
, host
->ocr
, &ocr
);
348 * For SPI, enable CRC as appropriate.
350 if (mmc_host_is_spi(host
)) {
351 err
= mmc_spi_set_crc(host
, use_spi_crc
);
357 * Allocate card structure.
359 card
= mmc_alloc_card(host
, NULL
);
365 err
= mmc_sd_get_cid(host
, host
->ocr
& ocr
, card
->raw_cid
);
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
);
398 mmc_set_bus_mode(host
, MMC_BUSMODE_PUSHPULL
);
402 * Read CSD, before selecting the card
404 if (!oldcard
&& card
->type
== MMC_TYPE_SD_COMBO
) {
405 err
= mmc_sd_get_csd(host
, card
);
409 mmc_decode_cid(card
);
413 * Select card, as all following commands rely on that.
415 if (!powered_resume
&& !mmc_host_is_spi(host
)) {
416 err
= mmc_select_card(card
);
421 if (card
->quirks
& MMC_QUIRK_NONSTD_SDIO
) {
423 * This is non-standard SDIO device, meaning it doesn't
424 * have any CIA (Common I/O area) registers present.
425 * It's host's responsibility to fill cccr and cis
426 * structures in init_card().
428 mmc_set_clock(host
, card
->cis
.max_dtr
);
430 if (card
->cccr
.high_speed
) {
431 mmc_card_set_highspeed(card
);
432 mmc_set_timing(card
->host
, MMC_TIMING_SD_HS
);
439 * Read the common registers.
441 err
= sdio_read_cccr(card
);
446 * Read the common CIS tuples.
448 err
= sdio_read_common_cis(card
);
453 int same
= (card
->cis
.vendor
== oldcard
->cis
.vendor
&&
454 card
->cis
.device
== oldcard
->cis
.device
);
455 mmc_remove_card(card
);
463 if (card
->type
== MMC_TYPE_SD_COMBO
) {
464 err
= mmc_sd_setup_card(host
, card
, oldcard
!= NULL
);
465 /* handle as SDIO-only card if memory init failed */
468 if (mmc_host_is_spi(host
))
469 /* should not fail, as it worked previously */
470 mmc_spi_set_crc(host
, use_spi_crc
);
471 card
->type
= MMC_TYPE_SDIO
;
473 card
->dev
.type
= &sd_type
;
477 * If needed, disconnect card detection pull-up resistor.
479 err
= sdio_disable_cd(card
);
484 * Switch to high-speed (if supported).
486 err
= sdio_enable_hs(card
);
488 mmc_sd_go_highspeed(card
);
493 * Change to the card's maximum speed.
495 mmc_set_clock(host
, mmc_sdio_get_max_clock(card
));
498 * Switch to wider bus (if supported).
500 err
= sdio_enable_4bit_bus(card
);
502 mmc_set_bus_width(card
->host
, MMC_BUS_WIDTH_4
);
513 mmc_remove_card(card
);
520 * Host is being removed. Free up the current card.
522 static void mmc_sdio_remove(struct mmc_host
*host
)
529 for (i
= 0;i
< host
->card
->sdio_funcs
;i
++) {
530 if (host
->card
->sdio_func
[i
]) {
531 sdio_remove_func(host
->card
->sdio_func
[i
]);
532 host
->card
->sdio_func
[i
] = NULL
;
536 mmc_remove_card(host
->card
);
541 * Card detection callback from host.
543 static void mmc_sdio_detect(struct mmc_host
*host
)
550 mmc_claim_host(host
);
553 * Just check if our card has been removed.
555 err
= mmc_select_card(host
->card
);
557 mmc_release_host(host
);
560 mmc_sdio_remove(host
);
562 mmc_claim_host(host
);
563 mmc_detach_bus(host
);
564 mmc_release_host(host
);
569 * SDIO suspend. We need to suspend all functions separately.
570 * Therefore all registered functions must have drivers with suspend
571 * and resume methods. Failing that we simply remove the whole card.
573 static int mmc_sdio_suspend(struct mmc_host
*host
)
577 for (i
= 0; i
< host
->card
->sdio_funcs
; i
++) {
578 struct sdio_func
*func
= host
->card
->sdio_func
[i
];
579 if (func
&& sdio_func_present(func
) && func
->dev
.driver
) {
580 const struct dev_pm_ops
*pmops
= func
->dev
.driver
->pm
;
581 if (!pmops
|| !pmops
->suspend
|| !pmops
->resume
) {
582 /* force removal of entire card in that case */
585 err
= pmops
->suspend(&func
->dev
);
590 while (err
&& --i
>= 0) {
591 struct sdio_func
*func
= host
->card
->sdio_func
[i
];
592 if (func
&& sdio_func_present(func
) && func
->dev
.driver
) {
593 const struct dev_pm_ops
*pmops
= func
->dev
.driver
->pm
;
594 pmops
->resume(&func
->dev
);
598 if (!err
&& host
->pm_flags
& MMC_PM_KEEP_POWER
) {
599 mmc_claim_host(host
);
600 sdio_disable_wide(host
->card
);
601 mmc_release_host(host
);
607 static int mmc_sdio_resume(struct mmc_host
*host
)
614 /* Basic card reinitialization. */
615 mmc_claim_host(host
);
616 err
= mmc_sdio_init_card(host
, host
->ocr
, host
->card
,
617 (host
->pm_flags
& MMC_PM_KEEP_POWER
));
619 /* We may have switched to 1-bit mode during suspend. */
620 err
= sdio_enable_4bit_bus(host
->card
);
622 mmc_set_bus_width(host
, MMC_BUS_WIDTH_4
);
626 if (!err
&& host
->sdio_irqs
)
627 mmc_signal_sdio_irq(host
);
628 mmc_release_host(host
);
631 * If the card looked to be the same as before suspending, then
632 * we proceed to resume all card functions. If one of them returns
633 * an error then we simply return that error to the core and the
634 * card will be redetected as new. It is the responsibility of
635 * the function driver to perform further tests with the extra
636 * knowledge it has of the card to confirm the card is indeed the
637 * same as before suspending (same MAC address for network cards,
638 * etc.) and return an error otherwise.
640 for (i
= 0; !err
&& i
< host
->card
->sdio_funcs
; i
++) {
641 struct sdio_func
*func
= host
->card
->sdio_func
[i
];
642 if (func
&& sdio_func_present(func
) && func
->dev
.driver
) {
643 const struct dev_pm_ops
*pmops
= func
->dev
.driver
->pm
;
644 err
= pmops
->resume(&func
->dev
);
651 static const struct mmc_bus_ops mmc_sdio_ops
= {
652 .remove
= mmc_sdio_remove
,
653 .detect
= mmc_sdio_detect
,
654 .suspend
= mmc_sdio_suspend
,
655 .resume
= mmc_sdio_resume
,
660 * Starting point for SDIO card init.
662 int mmc_attach_sdio(struct mmc_host
*host
, u32 ocr
)
666 struct mmc_card
*card
;
669 WARN_ON(!host
->claimed
);
671 mmc_attach_bus(host
, &mmc_sdio_ops
);
674 * Sanity check the voltages that the card claims to
678 printk(KERN_WARNING
"%s: card claims to support voltages "
679 "below the defined range. These will be ignored.\n",
684 host
->ocr
= mmc_select_voltage(host
, ocr
);
687 * Can we support the voltage(s) of the card(s)?
695 * Detect and init the card.
697 err
= mmc_sdio_init_card(host
, host
->ocr
, NULL
, 0);
703 * The number of functions on the card is encoded inside
706 funcs
= (ocr
& 0x70000000) >> 28;
707 card
->sdio_funcs
= 0;
710 * Initialize (but don't add) all present functions.
712 for (i
= 0; i
< funcs
; i
++, card
->sdio_funcs
++) {
713 err
= sdio_init_func(host
->card
, i
+ 1);
718 mmc_release_host(host
);
721 * First add the card to the driver model...
723 err
= mmc_add_card(host
->card
);
728 * ...then the SDIO functions.
730 for (i
= 0;i
< funcs
;i
++) {
731 err
= sdio_add_func(host
->card
->sdio_func
[i
]);
740 /* Remove without lock if the device has been added. */
741 mmc_sdio_remove(host
);
742 mmc_claim_host(host
);
744 /* And with lock if it hasn't been added. */
746 mmc_sdio_remove(host
);
748 mmc_detach_bus(host
);
749 mmc_release_host(host
);
751 printk(KERN_ERR
"%s: error %d whilst initialising SDIO card\n",
752 mmc_hostname(host
), err
);