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>
19 #include <linux/mmc/sdio_ids.h>
30 #ifdef CONFIG_MMC_EMBEDDED_SDIO
31 #include <linux/mmc/sdio_ids.h>
34 static int sdio_read_fbr(struct sdio_func *func)
39 if (mmc_card_nonstd_func_interface(func->card)) {
40 func->class = SDIO_CLASS_NONE;
44 ret = mmc_io_rw_direct(func->card, 0, 0,
45 SDIO_FBR_BASE(func->num) + SDIO_FBR_STD_IF, 0, &data);
52 ret = mmc_io_rw_direct(func->card, 0, 0,
53 SDIO_FBR_BASE(func->num) + SDIO_FBR_STD_IF_EXT, 0, &data);
64 static int sdio_init_func(struct mmc_card *card, unsigned int fn)
67 struct sdio_func *func;
69 BUG_ON(fn > SDIO_MAX_FUNCS);
71 func = sdio_alloc_func(card);
77 if (!(card->quirks & MMC_QUIRK_NONSTD_SDIO)) {
78 ret = sdio_read_fbr(func);
82 ret = sdio_read_func_cis(func);
86 func->vendor = func->card->cis.vendor;
87 func->device = func->card->cis.device;
88 func->max_blksize = func->card->cis.blksize;
91 card->sdio_func[fn - 1] = func;
97 * It is okay to remove the function here even though we hold
98 * the host lock as we haven't registered the device yet.
100 sdio_remove_func(func);
104 static int sdio_read_cccr(struct mmc_card *card)
110 memset(&card->cccr, 0, sizeof(struct sdio_cccr));
112 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_CCCR, 0, &data);
116 cccr_vsn = data & 0x0f;
118 if (cccr_vsn > SDIO_CCCR_REV_1_20) {
119 printk(KERN_ERR "%s: unrecognised CCCR structure version %d\n",
120 mmc_hostname(card->host), cccr_vsn);
124 card->cccr.sdio_vsn = (data & 0xf0) >> 4;
126 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_CAPS, 0, &data);
130 if (data & SDIO_CCCR_CAP_SMB)
131 card->cccr.multi_block = 1;
132 if (data & SDIO_CCCR_CAP_LSC)
133 card->cccr.low_speed = 1;
134 if (data & SDIO_CCCR_CAP_4BLS)
135 card->cccr.wide_bus = 1;
137 if (cccr_vsn >= SDIO_CCCR_REV_1_10) {
138 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_POWER, 0, &data);
142 if (data & SDIO_POWER_SMPC)
143 card->cccr.high_power = 1;
146 if (cccr_vsn >= SDIO_CCCR_REV_1_20) {
147 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_SPEED, 0, &data);
151 if (data & SDIO_SPEED_SHS)
152 card->cccr.high_speed = 1;
159 static int sdio_enable_wide(struct mmc_card *card)
164 if (!(card->host->caps & MMC_CAP_4_BIT_DATA))
167 if (card->cccr.low_speed && !card->cccr.wide_bus)
170 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl);
174 ctrl |= SDIO_BUS_WIDTH_4BIT;
176 ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL);
184 * If desired, disconnect the pull-up resistor on CD/DAT[3] (pin 1)
185 * of the card. This may be required on certain setups of boards,
186 * controllers and embedded sdio device which do not need the card's
187 * pull-up. As a result, card detection is disabled and power is saved.
189 static int sdio_disable_cd(struct mmc_card *card)
194 if (!mmc_card_disable_cd(card))
197 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl);
201 ctrl |= SDIO_BUS_CD_DISABLE;
203 return mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL);
207 * Devices that remain active during a system suspend are
208 * put back into 1-bit mode.
210 static int sdio_disable_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 if (!(ctrl & SDIO_BUS_WIDTH_4BIT))
228 ctrl &= ~SDIO_BUS_WIDTH_4BIT;
229 ctrl |= SDIO_BUS_ASYNC_INT;
231 ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL);
235 mmc_set_bus_width(card->host, MMC_BUS_WIDTH_1);
241 static int sdio_enable_4bit_bus(struct mmc_card *card)
245 if (card->type == MMC_TYPE_SDIO)
246 return sdio_enable_wide(card);
248 if ((card->host->caps & MMC_CAP_4_BIT_DATA) &&
249 (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) {
250 err = mmc_app_set_bus_width(card, MMC_BUS_WIDTH_4);
256 err = sdio_enable_wide(card);
258 mmc_app_set_bus_width(card, MMC_BUS_WIDTH_1);
265 * Test if the card supports high-speed mode and, if so, switch to it.
267 static int mmc_sdio_switch_hs(struct mmc_card *card, int enable)
272 if (!(card->host->caps & MMC_CAP_SD_HIGHSPEED))
275 if (!card->cccr.high_speed)
278 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_SPEED, 0, &speed);
283 speed |= SDIO_SPEED_EHS;
285 speed &= ~SDIO_SPEED_EHS;
287 ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_SPEED, speed, NULL);
295 * Enable SDIO/combo card's high-speed mode. Return 0/1 if [not]supported.
297 static int sdio_enable_hs(struct mmc_card *card)
301 ret = mmc_sdio_switch_hs(card, true);
302 if (ret <= 0 || card->type == MMC_TYPE_SDIO)
305 ret = mmc_sd_switch_hs(card);
307 mmc_sdio_switch_hs(card, false);
312 static unsigned mmc_sdio_get_max_clock(struct mmc_card *card)
316 if (mmc_card_highspeed(card)) {
318 * The SDIO specification doesn't mention how
319 * the CIS transfer speed register relates to
320 * high-speed, but it seems that 50 MHz is
325 max_dtr = card->cis.max_dtr;
328 if (card->type == MMC_TYPE_SD_COMBO)
329 max_dtr = min(max_dtr, mmc_sd_get_max_clock(card));
335 * Handle the detection and initialisation of a card.
337 * In the case of a resume, "oldcard" will contain the card
338 * we're trying to reinitialise.
340 static int mmc_sdio_init_card(struct mmc_host *host, u32 ocr,
341 struct mmc_card *oldcard, int powered_resume)
343 struct mmc_card *card;
347 WARN_ON(!host->claimed);
350 * Inform the card of the voltage
352 if (!powered_resume) {
353 err = mmc_send_io_op_cond(host, host->ocr, &ocr);
359 * For SPI, enable CRC as appropriate.
361 if (mmc_host_is_spi(host)) {
362 err = mmc_spi_set_crc(host, use_spi_crc);
368 * Allocate card structure.
370 card = mmc_alloc_card(host, NULL);
376 if ((ocr & R4_MEMORY_PRESENT) &&
377 mmc_sd_get_cid(host, host->ocr & ocr, card->raw_cid, NULL) == 0) {
378 card->type = MMC_TYPE_SD_COMBO;
380 if (oldcard && (oldcard->type != MMC_TYPE_SD_COMBO ||
381 memcmp(card->raw_cid, oldcard->raw_cid, sizeof(card->raw_cid)) != 0)) {
382 mmc_remove_card(card);
386 card->type = MMC_TYPE_SDIO;
388 if (oldcard && oldcard->type != MMC_TYPE_SDIO) {
389 mmc_remove_card(card);
395 * Call the optional HC's init_card function to handle quirks.
397 if (host->ops->init_card)
398 host->ops->init_card(host, card);
401 * For native busses: set card RCA and quit open drain mode.
403 if (!powered_resume && !mmc_host_is_spi(host)) {
404 err = mmc_send_relative_addr(host, &card->rca);
409 * Update oldcard with the new RCA received from the SDIO
410 * device -- we're doing this so that it's updated in the
411 * "card" struct when oldcard overwrites that later.
414 oldcard->rca = card->rca;
416 mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
420 * Read CSD, before selecting the card
422 if (!oldcard && card->type == MMC_TYPE_SD_COMBO) {
423 err = mmc_sd_get_csd(host, card);
427 mmc_decode_cid(card);
431 * Select card, as all following commands rely on that.
433 if (!powered_resume && !mmc_host_is_spi(host)) {
434 err = mmc_select_card(card);
439 if (card->quirks & MMC_QUIRK_NONSTD_SDIO) {
441 * This is non-standard SDIO device, meaning it doesn't
442 * have any CIA (Common I/O area) registers present.
443 * It's host's responsibility to fill cccr and cis
444 * structures in init_card().
446 mmc_set_clock(host, card->cis.max_dtr);
448 if (card->cccr.high_speed) {
449 mmc_card_set_highspeed(card);
450 mmc_set_timing(card->host, MMC_TIMING_SD_HS);
456 #ifdef CONFIG_MMC_EMBEDDED_SDIO
457 if (host->embedded_sdio_data.cccr)
458 memcpy(&card->cccr, host->embedded_sdio_data.cccr, sizeof(struct sdio_cccr));
462 * Read the common registers.
464 err = sdio_read_cccr(card);
467 #ifdef CONFIG_MMC_EMBEDDED_SDIO
471 #ifdef CONFIG_MMC_EMBEDDED_SDIO
472 if (host->embedded_sdio_data.cis)
473 memcpy(&card->cis, host->embedded_sdio_data.cis, sizeof(struct sdio_cis));
477 * Read the common CIS tuples.
479 err = sdio_read_common_cis(card);
482 #ifdef CONFIG_MMC_EMBEDDED_SDIO
487 int same = (card->cis.vendor == oldcard->cis.vendor &&
488 card->cis.device == oldcard->cis.device);
489 mmc_remove_card(card);
495 mmc_fixup_device(card, NULL);
497 if (card->type == MMC_TYPE_SD_COMBO) {
498 err = mmc_sd_setup_card(host, card, oldcard != NULL);
499 /* handle as SDIO-only card if memory init failed */
502 if (mmc_host_is_spi(host))
503 /* should not fail, as it worked previously */
504 mmc_spi_set_crc(host, use_spi_crc);
505 card->type = MMC_TYPE_SDIO;
507 card->dev.type = &sd_type;
511 * If needed, disconnect card detection pull-up resistor.
513 err = sdio_disable_cd(card);
518 * Switch to high-speed (if supported).
520 err = sdio_enable_hs(card);
522 mmc_sd_go_highspeed(card);
527 * Change to the card's maximum speed.
529 mmc_set_clock(host, mmc_sdio_get_max_clock(card));
532 * Switch to wider bus (if supported).
534 err = sdio_enable_4bit_bus(card);
536 mmc_set_bus_width(card->host, MMC_BUS_WIDTH_4);
547 mmc_remove_card(card);
554 * Host is being removed. Free up the current card.
556 static void mmc_sdio_remove(struct mmc_host *host)
563 for (i = 0;i < host->card->sdio_funcs;i++) {
564 if (host->card->sdio_func[i]) {
565 sdio_remove_func(host->card->sdio_func[i]);
566 host->card->sdio_func[i] = NULL;
570 mmc_remove_card(host->card);
575 * Card detection callback from host.
577 static void mmc_sdio_detect(struct mmc_host *host)
584 /* Make sure card is powered before detecting it */
585 if (host->caps & MMC_CAP_POWER_OFF_CARD) {
586 err = pm_runtime_get_sync(&host->card->dev);
591 mmc_claim_host(host);
594 * Just check if our card has been removed.
596 err = mmc_select_card(host->card);
598 mmc_release_host(host);
601 * Tell PM core it's OK to power off the card now.
603 * The _sync variant is used in order to ensure that the card
604 * is left powered off in case an error occurred, and the card
605 * is going to be removed.
607 * Since there is no specific reason to believe a new user
608 * is about to show up at this point, the _sync variant is
611 if (host->caps & MMC_CAP_POWER_OFF_CARD)
612 pm_runtime_put_sync(&host->card->dev);
616 mmc_sdio_remove(host);
618 mmc_claim_host(host);
619 mmc_detach_bus(host);
620 mmc_release_host(host);
625 * SDIO suspend. We need to suspend all functions separately.
626 * Therefore all registered functions must have drivers with suspend
627 * and resume methods. Failing that we simply remove the whole card.
629 static int mmc_sdio_suspend(struct mmc_host *host)
633 for (i = 0; i < host->card->sdio_funcs; i++) {
634 struct sdio_func *func = host->card->sdio_func[i];
635 if (func && sdio_func_present(func) && func->dev.driver) {
636 const struct dev_pm_ops *pmops = func->dev.driver->pm;
637 if (!pmops || !pmops->suspend || !pmops->resume) {
638 /* force removal of entire card in that case */
641 err = pmops->suspend(&func->dev);
646 while (err && --i >= 0) {
647 struct sdio_func *func = host->card->sdio_func[i];
648 if (func && sdio_func_present(func) && func->dev.driver) {
649 const struct dev_pm_ops *pmops = func->dev.driver->pm;
650 pmops->resume(&func->dev);
654 if (!err && mmc_card_keep_power(host) && mmc_card_wake_sdio_irq(host)) {
655 mmc_claim_host(host);
656 sdio_disable_wide(host->card);
657 mmc_release_host(host);
663 static int mmc_sdio_resume(struct mmc_host *host)
670 /* Basic card reinitialization. */
671 mmc_claim_host(host);
673 /* No need to reinitialize powered-resumed nonremovable cards */
674 if (mmc_card_is_removable(host) || !mmc_card_keep_power(host))
675 err = mmc_sdio_init_card(host, host->ocr, host->card,
676 mmc_card_keep_power(host));
677 else if (mmc_card_keep_power(host) && mmc_card_wake_sdio_irq(host)) {
678 /* We may have switched to 1-bit mode during suspend */
679 err = sdio_enable_4bit_bus(host->card);
681 mmc_set_bus_width(host, MMC_BUS_WIDTH_4);
686 if (!err && host->sdio_irqs)
687 mmc_signal_sdio_irq(host);
688 mmc_release_host(host);
691 * If the card looked to be the same as before suspending, then
692 * we proceed to resume all card functions. If one of them returns
693 * an error then we simply return that error to the core and the
694 * card will be redetected as new. It is the responsibility of
695 * the function driver to perform further tests with the extra
696 * knowledge it has of the card to confirm the card is indeed the
697 * same as before suspending (same MAC address for network cards,
698 * etc.) and return an error otherwise.
700 for (i = 0; !err && i < host->card->sdio_funcs; i++) {
701 struct sdio_func *func = host->card->sdio_func[i];
702 if (func && sdio_func_present(func) && func->dev.driver) {
703 const struct dev_pm_ops *pmops = func->dev.driver->pm;
704 err = pmops->resume(&func->dev);
711 static int mmc_sdio_power_restore(struct mmc_host *host)
719 mmc_claim_host(host);
722 * Reset the card by performing the same steps that are taken by
723 * mmc_rescan_try_freq() and mmc_attach_sdio() during a "normal" probe.
725 * sdio_reset() is technically not needed. Having just powered up the
726 * hardware, it should already be in reset state. However, some
727 * platforms (such as SD8686 on OLPC) do not instantly cut power,
728 * meaning that a reset is required when restoring power soon after
729 * powering off. It is harmless in other cases.
731 * The CMD5 reset (mmc_send_io_op_cond()), according to the SDIO spec,
732 * is not necessary for non-removable cards. However, it is required
733 * for OLPC SD8686 (which expects a [CMD5,5,3,7] init sequence), and
734 * harmless in other situations.
736 * With these steps taken, mmc_select_voltage() is also required to
737 * restore the correct voltage setting of the card.
741 mmc_send_if_cond(host, host->ocr_avail);
743 ret = mmc_send_io_op_cond(host, 0, &ocr);
747 if (host->ocr_avail_sdio)
748 host->ocr_avail = host->ocr_avail_sdio;
750 host->ocr = mmc_select_voltage(host, ocr & ~0x7F);
756 ret = mmc_sdio_init_card(host, host->ocr, host->card,
757 mmc_card_keep_power(host));
758 if (!ret && host->sdio_irqs)
759 mmc_signal_sdio_irq(host);
762 mmc_release_host(host);
767 static const struct mmc_bus_ops mmc_sdio_ops = {
768 .remove = mmc_sdio_remove,
769 .detect = mmc_sdio_detect,
770 .suspend = mmc_sdio_suspend,
771 .resume = mmc_sdio_resume,
772 .power_restore = mmc_sdio_power_restore,
777 * Starting point for SDIO card init.
779 int mmc_attach_sdio(struct mmc_host *host)
783 struct mmc_card *card;
786 WARN_ON(!host->claimed);
788 err = mmc_send_io_op_cond(host, 0, &ocr);
792 mmc_attach_bus(host, &mmc_sdio_ops);
793 if (host->ocr_avail_sdio)
794 host->ocr_avail = host->ocr_avail_sdio;
797 * Sanity check the voltages that the card claims to
801 printk(KERN_WARNING "%s: card claims to support voltages "
802 "below the defined range. These will be ignored.\n",
807 host->ocr = mmc_select_voltage(host, ocr);
810 * Can we support the voltage(s) of the card(s)?
818 * Detect and init the card.
820 err = mmc_sdio_init_card(host, host->ocr, NULL, 0);
826 * Enable runtime PM only if supported by host+card+board
828 if (host->caps & MMC_CAP_POWER_OFF_CARD) {
830 * Let runtime PM core know our card is active
832 err = pm_runtime_set_active(&card->dev);
837 * Enable runtime PM for this card
839 pm_runtime_enable(&card->dev);
843 * The number of functions on the card is encoded inside
846 funcs = (ocr & 0x70000000) >> 28;
847 card->sdio_funcs = 0;
849 #ifdef CONFIG_MMC_EMBEDDED_SDIO
850 if (host->embedded_sdio_data.funcs)
851 card->sdio_funcs = funcs = host->embedded_sdio_data.num_funcs;
855 * Initialize (but don't add) all present functions.
857 for (i = 0; i < funcs; i++, card->sdio_funcs++) {
858 #ifdef CONFIG_MMC_EMBEDDED_SDIO
859 if (host->embedded_sdio_data.funcs) {
860 struct sdio_func *tmp;
862 tmp = sdio_alloc_func(host->card);
866 card->sdio_func[i] = tmp;
867 tmp->class = host->embedded_sdio_data.funcs[i].f_class;
868 tmp->max_blksize = host->embedded_sdio_data.funcs[i].f_maxblksize;
869 tmp->vendor = card->cis.vendor;
870 tmp->device = card->cis.device;
873 err = sdio_init_func(host->card, i + 1);
876 #ifdef CONFIG_MMC_EMBEDDED_SDIO
880 * Enable Runtime PM for this func (if supported)
882 if (host->caps & MMC_CAP_POWER_OFF_CARD)
883 pm_runtime_enable(&card->sdio_func[i]->dev);
887 * First add the card to the driver model...
889 mmc_release_host(host);
890 err = mmc_add_card(host->card);
895 * ...then the SDIO functions.
897 for (i = 0;i < funcs;i++) {
898 err = sdio_add_func(host->card->sdio_func[i]);
903 mmc_claim_host(host);
908 /* Remove without lock if the device has been added. */
909 mmc_sdio_remove(host);
910 mmc_claim_host(host);
912 /* And with lock if it hasn't been added. */
913 mmc_release_host(host);
915 mmc_sdio_remove(host);
916 mmc_claim_host(host);
918 mmc_detach_bus(host);
920 printk(KERN_ERR "%s: error %d whilst initialising SDIO card\n",
921 mmc_hostname(host), err);
926 int sdio_reset_comm(struct mmc_card *card)
928 struct mmc_host *host = card->host;
932 printk("%s():\n", __func__);
933 mmc_claim_host(host);
937 mmc_set_clock(host, host->f_min);
939 err = mmc_send_io_op_cond(host, 0, &ocr);
943 host->ocr = mmc_select_voltage(host, ocr);
949 err = mmc_send_io_op_cond(host, host->ocr, &ocr);
953 if (mmc_host_is_spi(host)) {
954 err = mmc_spi_set_crc(host, use_spi_crc);
959 if (!mmc_host_is_spi(host)) {
960 err = mmc_send_relative_addr(host, &card->rca);
963 mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
965 if (!mmc_host_is_spi(host)) {
966 err = mmc_select_card(card);
972 * Switch to high-speed (if supported).
974 err = sdio_enable_hs(card);
976 mmc_sd_go_highspeed(card);
981 * Change to the card's maximum speed.
983 mmc_set_clock(host, mmc_sdio_get_max_clock(card));
985 err = sdio_enable_4bit_bus(card);
987 mmc_set_bus_width(host, MMC_BUS_WIDTH_4);
991 mmc_release_host(host);
994 printk("%s: Error resetting SDIO communications (%d)\n",
995 mmc_hostname(host), err);
996 mmc_release_host(host);
999 EXPORT_SYMBOL(sdio_reset_comm);