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 #ifdef CONFIG_MMC_EMBEDDED_SDIO
30 #include <linux/mmc/sdio_ids.h>
33 static int sdio_read_fbr(struct sdio_func *func)
38 ret = mmc_io_rw_direct(func->card, 0, 0,
39 SDIO_FBR_BASE(func->num) + SDIO_FBR_STD_IF, 0, &data);
46 ret = mmc_io_rw_direct(func->card, 0, 0,
47 SDIO_FBR_BASE(func->num) + SDIO_FBR_STD_IF_EXT, 0, &data);
58 static int sdio_init_func(struct mmc_card *card, unsigned int fn)
61 struct sdio_func *func;
63 BUG_ON(fn > SDIO_MAX_FUNCS);
65 func = sdio_alloc_func(card);
71 if (!(card->quirks & MMC_QUIRK_NONSTD_SDIO)) {
72 ret = sdio_read_fbr(func);
76 ret = sdio_read_func_cis(func);
80 func->vendor = func->card->cis.vendor;
81 func->device = func->card->cis.device;
82 func->max_blksize = func->card->cis.blksize;
85 card->sdio_func[fn - 1] = func;
91 * It is okay to remove the function here even though we hold
92 * the host lock as we haven't registered the device yet.
94 sdio_remove_func(func);
98 static int sdio_read_cccr(struct mmc_card *card)
104 memset(&card->cccr, 0, sizeof(struct sdio_cccr));
106 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_CCCR, 0, &data);
110 cccr_vsn = data & 0x0f;
112 if (cccr_vsn > SDIO_CCCR_REV_1_20) {
113 printk(KERN_ERR "%s: unrecognised CCCR structure version %d\n",
114 mmc_hostname(card->host), cccr_vsn);
118 card->cccr.sdio_vsn = (data & 0xf0) >> 4;
120 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_CAPS, 0, &data);
124 if (data & SDIO_CCCR_CAP_SMB)
125 card->cccr.multi_block = 1;
126 if (data & SDIO_CCCR_CAP_LSC)
127 card->cccr.low_speed = 1;
128 if (data & SDIO_CCCR_CAP_4BLS)
129 card->cccr.wide_bus = 1;
131 if (cccr_vsn >= SDIO_CCCR_REV_1_10) {
132 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_POWER, 0, &data);
136 if (data & SDIO_POWER_SMPC)
137 card->cccr.high_power = 1;
140 if (cccr_vsn >= SDIO_CCCR_REV_1_20) {
141 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_SPEED, 0, &data);
145 if (data & SDIO_SPEED_SHS)
146 card->cccr.high_speed = 1;
153 static int sdio_enable_wide(struct mmc_card *card)
158 if (!(card->host->caps & MMC_CAP_4_BIT_DATA))
161 if (card->cccr.low_speed && !card->cccr.wide_bus)
164 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl);
168 ctrl |= SDIO_BUS_WIDTH_4BIT;
170 ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL);
178 * If desired, disconnect the pull-up resistor on CD/DAT[3] (pin 1)
179 * of the card. This may be required on certain setups of boards,
180 * controllers and embedded sdio device which do not need the card's
181 * pull-up. As a result, card detection is disabled and power is saved.
183 static int sdio_disable_cd(struct mmc_card *card)
188 if (!card->cccr.disable_cd)
191 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl);
195 ctrl |= SDIO_BUS_CD_DISABLE;
197 return mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL);
201 * Devices that remain active during a system suspend are
202 * put back into 1-bit mode.
204 static int sdio_disable_wide(struct mmc_card *card)
209 if (!(card->host->caps & MMC_CAP_4_BIT_DATA))
212 if (card->cccr.low_speed && !card->cccr.wide_bus)
215 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl);
219 if (!(ctrl & SDIO_BUS_WIDTH_4BIT))
222 ctrl &= ~SDIO_BUS_WIDTH_4BIT;
223 ctrl |= SDIO_BUS_ASYNC_INT;
225 ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL);
229 mmc_set_bus_width(card->host, MMC_BUS_WIDTH_1);
235 static int sdio_enable_4bit_bus(struct mmc_card *card)
239 if (card->type == MMC_TYPE_SDIO)
240 return sdio_enable_wide(card);
242 if ((card->host->caps & MMC_CAP_4_BIT_DATA) &&
243 (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) {
244 err = mmc_app_set_bus_width(card, MMC_BUS_WIDTH_4);
250 err = sdio_enable_wide(card);
252 mmc_app_set_bus_width(card, MMC_BUS_WIDTH_1);
259 * Test if the card supports high-speed mode and, if so, switch to it.
261 static int mmc_sdio_switch_hs(struct mmc_card *card, int enable)
266 if (!(card->host->caps & MMC_CAP_SD_HIGHSPEED))
269 if (!card->cccr.high_speed)
272 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_SPEED, 0, &speed);
277 speed |= SDIO_SPEED_EHS;
279 speed &= ~SDIO_SPEED_EHS;
281 ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_SPEED, speed, NULL);
289 * Enable SDIO/combo card's high-speed mode. Return 0/1 if [not]supported.
291 static int sdio_enable_hs(struct mmc_card *card)
295 ret = mmc_sdio_switch_hs(card, true);
296 if (ret <= 0 || card->type == MMC_TYPE_SDIO)
299 ret = mmc_sd_switch_hs(card);
301 mmc_sdio_switch_hs(card, false);
306 static unsigned mmc_sdio_get_max_clock(struct mmc_card *card)
310 if (mmc_card_highspeed(card)) {
312 * The SDIO specification doesn't mention how
313 * the CIS transfer speed register relates to
314 * high-speed, but it seems that 50 MHz is
319 max_dtr = card->cis.max_dtr;
322 if (card->type == MMC_TYPE_SD_COMBO)
323 max_dtr = min(max_dtr, mmc_sd_get_max_clock(card));
329 * Handle the detection and initialisation of a card.
331 * In the case of a resume, "oldcard" will contain the card
332 * we're trying to reinitialise.
334 static int mmc_sdio_init_card(struct mmc_host *host, u32 ocr,
335 struct mmc_card *oldcard, int powered_resume)
337 struct mmc_card *card;
341 WARN_ON(!host->claimed);
344 * Inform the card of the voltage
346 if (!powered_resume) {
347 err = mmc_send_io_op_cond(host, host->ocr, &ocr);
353 * For SPI, enable CRC as appropriate.
355 if (mmc_host_is_spi(host)) {
356 err = mmc_spi_set_crc(host, use_spi_crc);
362 * Allocate card structure.
364 card = mmc_alloc_card(host, NULL);
370 if (ocr & R4_MEMORY_PRESENT
371 && mmc_sd_get_cid(host, host->ocr & ocr, card->raw_cid) == 0) {
372 card->type = MMC_TYPE_SD_COMBO;
374 if (oldcard && (oldcard->type != MMC_TYPE_SD_COMBO ||
375 memcmp(card->raw_cid, oldcard->raw_cid, sizeof(card->raw_cid)) != 0)) {
376 mmc_remove_card(card);
380 card->type = MMC_TYPE_SDIO;
382 if (oldcard && oldcard->type != MMC_TYPE_SDIO) {
383 mmc_remove_card(card);
389 * Call the optional HC's init_card function to handle quirks.
391 if (host->ops->init_card)
392 host->ops->init_card(host, card);
395 * For native busses: set card RCA and quit open drain mode.
397 if (!powered_resume && !mmc_host_is_spi(host)) {
398 err = mmc_send_relative_addr(host, &card->rca);
402 mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
406 * Read CSD, before selecting the card
408 if (!oldcard && card->type == MMC_TYPE_SD_COMBO) {
409 err = mmc_sd_get_csd(host, card);
413 mmc_decode_cid(card);
417 * Select card, as all following commands rely on that.
419 if (!powered_resume && !mmc_host_is_spi(host)) {
420 err = mmc_select_card(card);
425 if (card->quirks & MMC_QUIRK_NONSTD_SDIO) {
427 * This is non-standard SDIO device, meaning it doesn't
428 * have any CIA (Common I/O area) registers present.
429 * It's host's responsibility to fill cccr and cis
430 * structures in init_card().
432 mmc_set_clock(host, card->cis.max_dtr);
434 if (card->cccr.high_speed) {
435 mmc_card_set_highspeed(card);
436 mmc_set_timing(card->host, MMC_TIMING_SD_HS);
442 #ifdef CONFIG_MMC_EMBEDDED_SDIO
443 if (host->embedded_sdio_data.cccr)
444 memcpy(&card->cccr, host->embedded_sdio_data.cccr, sizeof(struct sdio_cccr));
448 * Read the common registers.
450 err = sdio_read_cccr(card);
453 #ifdef CONFIG_MMC_EMBEDDED_SDIO
457 #ifdef CONFIG_MMC_EMBEDDED_SDIO
458 if (host->embedded_sdio_data.cis)
459 memcpy(&card->cis, host->embedded_sdio_data.cis, sizeof(struct sdio_cis));
463 * Read the common CIS tuples.
465 err = sdio_read_common_cis(card);
468 #ifdef CONFIG_MMC_EMBEDDED_SDIO
473 int same = (card->cis.vendor == oldcard->cis.vendor &&
474 card->cis.device == oldcard->cis.device);
475 mmc_remove_card(card);
482 if (card->type == MMC_TYPE_SD_COMBO) {
483 err = mmc_sd_setup_card(host, card, oldcard != NULL);
484 /* handle as SDIO-only card if memory init failed */
487 if (mmc_host_is_spi(host))
488 /* should not fail, as it worked previously */
489 mmc_spi_set_crc(host, use_spi_crc);
490 card->type = MMC_TYPE_SDIO;
492 card->dev.type = &sd_type;
496 * If needed, disconnect card detection pull-up resistor.
498 err = sdio_disable_cd(card);
503 * Switch to high-speed (if supported).
505 err = sdio_enable_hs(card);
507 mmc_sd_go_highspeed(card);
512 * Change to the card's maximum speed.
514 mmc_set_clock(host, mmc_sdio_get_max_clock(card));
517 * Switch to wider bus (if supported).
519 err = sdio_enable_4bit_bus(card);
521 mmc_set_bus_width(card->host, MMC_BUS_WIDTH_4);
532 mmc_remove_card(card);
539 * Host is being removed. Free up the current card.
541 static void mmc_sdio_remove(struct mmc_host *host)
548 for (i = 0;i < host->card->sdio_funcs;i++) {
549 if (host->card->sdio_func[i]) {
550 sdio_remove_func(host->card->sdio_func[i]);
551 host->card->sdio_func[i] = NULL;
555 mmc_remove_card(host->card);
560 * Card detection callback from host.
562 static void mmc_sdio_detect(struct mmc_host *host)
569 /* Make sure card is powered before detecting it */
570 if (host->caps & MMC_CAP_POWER_OFF_CARD) {
571 err = pm_runtime_get_sync(&host->card->dev);
576 mmc_claim_host(host);
579 * Just check if our card has been removed.
581 err = mmc_select_card(host->card);
583 mmc_release_host(host);
586 * Tell PM core it's OK to power off the card now.
588 * The _sync variant is used in order to ensure that the card
589 * is left powered off in case an error occurred, and the card
590 * is going to be removed.
592 * Since there is no specific reason to believe a new user
593 * is about to show up at this point, the _sync variant is
596 if (host->caps & MMC_CAP_POWER_OFF_CARD)
597 pm_runtime_put_sync(&host->card->dev);
601 mmc_sdio_remove(host);
603 mmc_claim_host(host);
604 mmc_detach_bus(host);
605 mmc_release_host(host);
610 * SDIO suspend. We need to suspend all functions separately.
611 * Therefore all registered functions must have drivers with suspend
612 * and resume methods. Failing that we simply remove the whole card.
614 static int mmc_sdio_suspend(struct mmc_host *host)
618 for (i = 0; i < host->card->sdio_funcs; i++) {
619 struct sdio_func *func = host->card->sdio_func[i];
620 if (func && sdio_func_present(func) && func->dev.driver) {
621 const struct dev_pm_ops *pmops = func->dev.driver->pm;
622 if (!pmops || !pmops->suspend || !pmops->resume) {
623 /* force removal of entire card in that case */
626 err = pmops->suspend(&func->dev);
631 while (err && --i >= 0) {
632 struct sdio_func *func = host->card->sdio_func[i];
633 if (func && sdio_func_present(func) && func->dev.driver) {
634 const struct dev_pm_ops *pmops = func->dev.driver->pm;
635 pmops->resume(&func->dev);
639 if (!err && host->pm_flags & MMC_PM_KEEP_POWER) {
640 mmc_claim_host(host);
641 sdio_disable_wide(host->card);
642 mmc_release_host(host);
648 static int mmc_sdio_resume(struct mmc_host *host)
655 /* Basic card reinitialization. */
656 mmc_claim_host(host);
658 /* No need to reinitialize powered-resumed nonremovable cards */
659 if (mmc_card_is_removable(host) || !mmc_card_is_powered_resumed(host))
660 err = mmc_sdio_init_card(host, host->ocr, host->card,
661 (host->pm_flags & MMC_PM_KEEP_POWER));
662 else if (mmc_card_is_powered_resumed(host)) {
663 /* We may have switched to 1-bit mode during suspend */
664 err = sdio_enable_4bit_bus(host->card);
666 mmc_set_bus_width(host, MMC_BUS_WIDTH_4);
671 if (!err && host->sdio_irqs)
672 mmc_signal_sdio_irq(host);
673 mmc_release_host(host);
676 * If the card looked to be the same as before suspending, then
677 * we proceed to resume all card functions. If one of them returns
678 * an error then we simply return that error to the core and the
679 * card will be redetected as new. It is the responsibility of
680 * the function driver to perform further tests with the extra
681 * knowledge it has of the card to confirm the card is indeed the
682 * same as before suspending (same MAC address for network cards,
683 * etc.) and return an error otherwise.
685 for (i = 0; !err && i < host->card->sdio_funcs; i++) {
686 struct sdio_func *func = host->card->sdio_func[i];
687 if (func && sdio_func_present(func) && func->dev.driver) {
688 const struct dev_pm_ops *pmops = func->dev.driver->pm;
689 err = pmops->resume(&func->dev);
696 static int mmc_sdio_power_restore(struct mmc_host *host)
703 mmc_claim_host(host);
704 ret = mmc_sdio_init_card(host, host->ocr, host->card,
705 (host->pm_flags & MMC_PM_KEEP_POWER));
706 if (!ret && host->sdio_irqs)
707 mmc_signal_sdio_irq(host);
708 mmc_release_host(host);
713 static const struct mmc_bus_ops mmc_sdio_ops = {
714 .remove = mmc_sdio_remove,
715 .detect = mmc_sdio_detect,
716 .suspend = mmc_sdio_suspend,
717 .resume = mmc_sdio_resume,
718 .power_restore = mmc_sdio_power_restore,
723 * Starting point for SDIO card init.
725 int mmc_attach_sdio(struct mmc_host *host)
729 struct mmc_card *card;
732 WARN_ON(!host->claimed);
734 err = mmc_send_io_op_cond(host, 0, &ocr);
738 mmc_attach_bus(host, &mmc_sdio_ops);
739 if (host->ocr_avail_sdio)
740 host->ocr_avail = host->ocr_avail_sdio;
743 * Sanity check the voltages that the card claims to
747 printk(KERN_WARNING "%s: card claims to support voltages "
748 "below the defined range. These will be ignored.\n",
753 host->ocr = mmc_select_voltage(host, ocr);
756 * Can we support the voltage(s) of the card(s)?
764 * Detect and init the card.
766 err = mmc_sdio_init_card(host, host->ocr, NULL, 0);
772 * Enable runtime PM only if supported by host+card+board
774 if (host->caps & MMC_CAP_POWER_OFF_CARD) {
776 * Let runtime PM core know our card is active
778 err = pm_runtime_set_active(&card->dev);
783 * Enable runtime PM for this card
785 pm_runtime_enable(&card->dev);
789 * The number of functions on the card is encoded inside
792 funcs = (ocr & 0x70000000) >> 28;
793 card->sdio_funcs = 0;
795 #ifdef CONFIG_MMC_EMBEDDED_SDIO
796 if (host->embedded_sdio_data.funcs)
797 card->sdio_funcs = funcs = host->embedded_sdio_data.num_funcs;
801 * Initialize (but don't add) all present functions.
803 for (i = 0; i < funcs; i++, card->sdio_funcs++) {
804 #ifdef CONFIG_MMC_EMBEDDED_SDIO
805 if (host->embedded_sdio_data.funcs) {
806 struct sdio_func *tmp;
808 tmp = sdio_alloc_func(host->card);
812 card->sdio_func[i] = tmp;
813 tmp->class = host->embedded_sdio_data.funcs[i].f_class;
814 tmp->max_blksize = host->embedded_sdio_data.funcs[i].f_maxblksize;
815 tmp->vendor = card->cis.vendor;
816 tmp->device = card->cis.device;
819 err = sdio_init_func(host->card, i + 1);
822 #ifdef CONFIG_MMC_EMBEDDED_SDIO
826 * Enable Runtime PM for this func (if supported)
828 if (host->caps & MMC_CAP_POWER_OFF_CARD)
829 pm_runtime_enable(&card->sdio_func[i]->dev);
833 * First add the card to the driver model...
835 mmc_release_host(host);
836 err = mmc_add_card(host->card);
841 * ...then the SDIO functions.
843 for (i = 0;i < funcs;i++) {
844 err = sdio_add_func(host->card->sdio_func[i]);
849 mmc_claim_host(host);
854 /* Remove without lock if the device has been added. */
855 mmc_sdio_remove(host);
856 mmc_claim_host(host);
858 /* And with lock if it hasn't been added. */
859 mmc_release_host(host);
861 mmc_sdio_remove(host);
862 mmc_claim_host(host);
864 mmc_detach_bus(host);
866 printk(KERN_ERR "%s: error %d whilst initialising SDIO card\n",
867 mmc_hostname(host), err);
872 int sdio_reset_comm(struct mmc_card *card)
874 struct mmc_host *host = card->host;
878 printk("%s():\n", __func__);
879 mmc_claim_host(host);
883 mmc_set_clock(host, host->f_min);
885 err = mmc_send_io_op_cond(host, 0, &ocr);
889 host->ocr = mmc_select_voltage(host, ocr);
895 err = mmc_send_io_op_cond(host, host->ocr, &ocr);
899 if (mmc_host_is_spi(host)) {
900 err = mmc_spi_set_crc(host, use_spi_crc);
905 if (!mmc_host_is_spi(host)) {
906 err = mmc_send_relative_addr(host, &card->rca);
909 mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
911 if (!mmc_host_is_spi(host)) {
912 err = mmc_select_card(card);
918 * Switch to high-speed (if supported).
920 err = sdio_enable_hs(card);
925 * Change to the card's maximum speed.
927 if (mmc_card_highspeed(card)) {
929 * The SDIO specification doesn't mention how
930 * the CIS transfer speed register relates to
931 * high-speed, but it seems that 50 MHz is
934 mmc_set_clock(host, 50000000);
936 mmc_set_clock(host, card->cis.max_dtr);
939 err = sdio_enable_wide(card);
942 mmc_release_host(host);
945 printk("%s: Error resetting SDIO communications (%d)\n",
946 mmc_hostname(host), err);
947 mmc_release_host(host);
950 EXPORT_SYMBOL(sdio_reset_comm);