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 #ifdef CONFIG_MMC_EMBEDDED_SDIO
29 #include <linux/mmc/sdio_ids.h>
32 static int sdio_read_fbr(struct sdio_func *func)
37 ret = mmc_io_rw_direct(func->card, 0, 0,
38 SDIO_FBR_BASE(func->num) + SDIO_FBR_STD_IF, 0, &data);
45 ret = mmc_io_rw_direct(func->card, 0, 0,
46 SDIO_FBR_BASE(func->num) + SDIO_FBR_STD_IF_EXT, 0, &data);
57 static int sdio_init_func(struct mmc_card *card, unsigned int fn)
60 struct sdio_func *func;
62 BUG_ON(fn > SDIO_MAX_FUNCS);
64 func = sdio_alloc_func(card);
70 if (!(card->quirks & MMC_QUIRK_NONSTD_SDIO)) {
71 ret = sdio_read_fbr(func);
75 ret = sdio_read_func_cis(func);
79 func->vendor = func->card->cis.vendor;
80 func->device = func->card->cis.device;
81 func->max_blksize = func->card->cis.blksize;
84 card->sdio_func[fn - 1] = func;
90 * It is okay to remove the function here even though we hold
91 * the host lock as we haven't registered the device yet.
93 sdio_remove_func(func);
97 static int sdio_read_cccr(struct mmc_card *card)
103 memset(&card->cccr, 0, sizeof(struct sdio_cccr));
105 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_CCCR, 0, &data);
109 cccr_vsn = data & 0x0f;
111 if (cccr_vsn > SDIO_CCCR_REV_1_20) {
112 printk(KERN_ERR "%s: unrecognised CCCR structure version %d\n",
113 mmc_hostname(card->host), cccr_vsn);
117 card->cccr.sdio_vsn = (data & 0xf0) >> 4;
119 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_CAPS, 0, &data);
123 if (data & SDIO_CCCR_CAP_SMB)
124 card->cccr.multi_block = 1;
125 if (data & SDIO_CCCR_CAP_LSC)
126 card->cccr.low_speed = 1;
127 if (data & SDIO_CCCR_CAP_4BLS)
128 card->cccr.wide_bus = 1;
130 if (cccr_vsn >= SDIO_CCCR_REV_1_10) {
131 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_POWER, 0, &data);
135 if (data & SDIO_POWER_SMPC)
136 card->cccr.high_power = 1;
139 if (cccr_vsn >= SDIO_CCCR_REV_1_20) {
140 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_SPEED, 0, &data);
144 if (data & SDIO_SPEED_SHS)
145 card->cccr.high_speed = 1;
152 static int sdio_enable_wide(struct mmc_card *card)
157 if (!(card->host->caps & MMC_CAP_4_BIT_DATA))
160 if (card->cccr.low_speed && !card->cccr.wide_bus)
163 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl);
167 ctrl |= SDIO_BUS_WIDTH_4BIT;
169 ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL);
177 * If desired, disconnect the pull-up resistor on CD/DAT[3] (pin 1)
178 * of the card. This may be required on certain setups of boards,
179 * controllers and embedded sdio device which do not need the card's
180 * pull-up. As a result, card detection is disabled and power is saved.
182 static int sdio_disable_cd(struct mmc_card *card)
187 if (!card->cccr.disable_cd)
190 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl);
194 ctrl |= SDIO_BUS_CD_DISABLE;
196 return mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL);
200 * Devices that remain active during a system suspend are
201 * put back into 1-bit mode.
203 static int sdio_disable_wide(struct mmc_card *card)
208 if (!(card->host->caps & MMC_CAP_4_BIT_DATA))
211 if (card->cccr.low_speed && !card->cccr.wide_bus)
214 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl);
218 if (!(ctrl & SDIO_BUS_WIDTH_4BIT))
221 ctrl &= ~SDIO_BUS_WIDTH_4BIT;
222 ctrl |= SDIO_BUS_ASYNC_INT;
224 ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL);
228 mmc_set_bus_width(card->host, MMC_BUS_WIDTH_1);
234 static int sdio_enable_4bit_bus(struct mmc_card *card)
238 if (card->type == MMC_TYPE_SDIO)
239 return sdio_enable_wide(card);
241 if ((card->host->caps & MMC_CAP_4_BIT_DATA) &&
242 (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) {
243 err = mmc_app_set_bus_width(card, MMC_BUS_WIDTH_4);
249 err = sdio_enable_wide(card);
251 mmc_app_set_bus_width(card, MMC_BUS_WIDTH_1);
258 * Test if the card supports high-speed mode and, if so, switch to it.
260 static int mmc_sdio_switch_hs(struct mmc_card *card, int enable)
265 if (!(card->host->caps & MMC_CAP_FORCE_HS)) {
266 if (!(card->host->caps & MMC_CAP_SD_HIGHSPEED))
269 if (!card->cccr.high_speed)
273 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_SPEED, 0, &speed);
278 speed |= SDIO_SPEED_EHS;
280 speed &= ~SDIO_SPEED_EHS;
282 ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_SPEED, speed, NULL);
290 * Enable SDIO/combo card's high-speed mode. Return 0/1 if [not]supported.
292 static int sdio_enable_hs(struct mmc_card *card)
296 ret = mmc_sdio_switch_hs(card, true);
297 if (ret <= 0 || card->type == MMC_TYPE_SDIO)
300 ret = mmc_sd_switch_hs(card);
302 mmc_sdio_switch_hs(card, false);
307 static unsigned mmc_sdio_get_max_clock(struct mmc_card *card)
311 if (mmc_card_highspeed(card)) {
313 * The SDIO specification doesn't mention how
314 * the CIS transfer speed register relates to
315 * high-speed, but it seems that 50 MHz is
320 max_dtr = card->cis.max_dtr;
323 if (card->type == MMC_TYPE_SD_COMBO)
324 max_dtr = min(max_dtr, mmc_sd_get_max_clock(card));
330 * Handle the detection and initialisation of a card.
332 * In the case of a resume, "oldcard" will contain the card
333 * we're trying to reinitialise.
335 static int mmc_sdio_init_card(struct mmc_host *host, u32 ocr,
336 struct mmc_card *oldcard, int powered_resume)
338 struct mmc_card *card;
342 WARN_ON(!host->claimed);
345 * Inform the card of the voltage
347 if (!powered_resume) {
348 err = mmc_send_io_op_cond(host, host->ocr, &ocr);
350 #if defined(CONFIG_SDMMC_RK29) && !defined(CONFIG_SDMMC_RK29_OLD)
351 printk("%s..%d.. ====*Identify the card as SDIO , but OCR error, so fail to initialize.===xbw[%s]===\n", \
352 __FUNCTION__, __LINE__, mmc_hostname(host));
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) == 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);
408 #if defined(CONFIG_SDMMC_RK29) && defined(CONFIG_SDMMC_RK29_OLD) //old driver add the code ,reform to kernel2.6.38
410 * Update oldcard with the new RCA received from the SDIO
411 * device -- we're doing this so that it's updated in the
412 * "card" struct when oldcard overwrites that later.
415 oldcard->rca = card->rca;
418 mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
422 * Read CSD, before selecting the card
424 if (!oldcard && card->type == MMC_TYPE_SD_COMBO) {
425 err = mmc_sd_get_csd(host, card);
429 mmc_decode_cid(card);
433 * Select card, as all following commands rely on that.
435 if (!powered_resume && !mmc_host_is_spi(host)) {
436 err = mmc_select_card(card);
441 if (card->quirks & MMC_QUIRK_NONSTD_SDIO) {
443 * This is non-standard SDIO device, meaning it doesn't
444 * have any CIA (Common I/O area) registers present.
445 * It's host's responsibility to fill cccr and cis
446 * structures in init_card().
448 mmc_set_clock(host, card->cis.max_dtr);
450 if (card->cccr.high_speed) {
451 mmc_card_set_highspeed(card);
452 mmc_set_timing(card->host, MMC_TIMING_SD_HS);
458 #ifdef CONFIG_MMC_EMBEDDED_SDIO
459 if (host->embedded_sdio_data.cccr)
460 memcpy(&card->cccr, host->embedded_sdio_data.cccr, sizeof(struct sdio_cccr));
464 * Read the common registers.
466 err = sdio_read_cccr(card);
469 #ifdef CONFIG_MMC_EMBEDDED_SDIO
473 #ifdef CONFIG_MMC_EMBEDDED_SDIO
474 if (host->embedded_sdio_data.cis)
475 memcpy(&card->cis, host->embedded_sdio_data.cis, sizeof(struct sdio_cis));
479 * Read the common CIS tuples.
481 err = sdio_read_common_cis(card);
484 #ifdef CONFIG_MMC_EMBEDDED_SDIO
489 int same = (card->cis.vendor == oldcard->cis.vendor &&
490 card->cis.device == oldcard->cis.device);
491 mmc_remove_card(card);
498 mmc_fixup_device(card, NULL);
500 if (card->type == MMC_TYPE_SD_COMBO) {
501 err = mmc_sd_setup_card(host, card, oldcard != NULL);
502 /* handle as SDIO-only card if memory init failed */
505 if (mmc_host_is_spi(host))
506 /* should not fail, as it worked previously */
507 mmc_spi_set_crc(host, use_spi_crc);
508 card->type = MMC_TYPE_SDIO;
510 card->dev.type = &sd_type;
514 * If needed, disconnect card detection pull-up resistor.
516 err = sdio_disable_cd(card);
521 * Switch to high-speed (if supported).
523 err = sdio_enable_hs(card);
525 mmc_sd_go_highspeed(card);
530 * Change to the card's maximum speed.
532 mmc_set_clock(host, mmc_sdio_get_max_clock(card));
535 * Switch to wider bus (if supported).
537 err = sdio_enable_4bit_bus(card);
539 mmc_set_bus_width(card->host, MMC_BUS_WIDTH_4);
550 mmc_remove_card(card);
557 * Host is being removed. Free up the current card.
559 static void mmc_sdio_remove(struct mmc_host *host)
566 for (i = 0;i < host->card->sdio_funcs;i++) {
567 if (host->card->sdio_func[i]) {
568 sdio_remove_func(host->card->sdio_func[i]);
569 host->card->sdio_func[i] = NULL;
573 mmc_remove_card(host->card);
578 * Card detection callback from host.
580 static void mmc_sdio_detect(struct mmc_host *host)
587 mmc_claim_host(host);
590 * Just check if our card has been removed.
592 err = mmc_select_card(host->card);
594 mmc_release_host(host);
597 mmc_sdio_remove(host);
599 mmc_claim_host(host);
600 mmc_detach_bus(host);
601 mmc_release_host(host);
606 * SDIO suspend. We need to suspend all functions separately.
607 * Therefore all registered functions must have drivers with suspend
608 * and resume methods. Failing that we simply remove the whole card.
610 static int mmc_sdio_suspend(struct mmc_host *host)
614 for (i = 0; i < host->card->sdio_funcs; i++) {
615 struct sdio_func *func = host->card->sdio_func[i];
616 if (func && sdio_func_present(func) && func->dev.driver) {
617 const struct dev_pm_ops *pmops = func->dev.driver->pm;
618 if (!pmops || !pmops->suspend || !pmops->resume) {
619 /* force removal of entire card in that case */
622 err = pmops->suspend(&func->dev);
627 while (err && --i >= 0) {
628 struct sdio_func *func = host->card->sdio_func[i];
629 if (func && sdio_func_present(func) && func->dev.driver) {
630 const struct dev_pm_ops *pmops = func->dev.driver->pm;
631 pmops->resume(&func->dev);
635 if (!err && host->pm_flags & MMC_PM_KEEP_POWER) {
636 mmc_claim_host(host);
637 sdio_disable_wide(host->card);
638 mmc_release_host(host);
644 static int mmc_sdio_resume(struct mmc_host *host)
651 /* Basic card reinitialization. */
652 mmc_claim_host(host);
653 err = mmc_sdio_init_card(host, host->ocr, host->card,
654 (host->pm_flags & MMC_PM_KEEP_POWER));
656 /* We may have switched to 1-bit mode during suspend. */
657 err = sdio_enable_4bit_bus(host->card);
659 mmc_set_bus_width(host, MMC_BUS_WIDTH_4);
663 if (!err && host->sdio_irqs)
664 mmc_signal_sdio_irq(host);
665 mmc_release_host(host);
668 * If the card looked to be the same as before suspending, then
669 * we proceed to resume all card functions. If one of them returns
670 * an error then we simply return that error to the core and the
671 * card will be redetected as new. It is the responsibility of
672 * the function driver to perform further tests with the extra
673 * knowledge it has of the card to confirm the card is indeed the
674 * same as before suspending (same MAC address for network cards,
675 * etc.) and return an error otherwise.
677 for (i = 0; !err && i < host->card->sdio_funcs; i++) {
678 struct sdio_func *func = host->card->sdio_func[i];
679 if (func && sdio_func_present(func) && func->dev.driver) {
680 const struct dev_pm_ops *pmops = func->dev.driver->pm;
681 err = pmops->resume(&func->dev);
688 static const struct mmc_bus_ops mmc_sdio_ops = {
689 .remove = mmc_sdio_remove,
690 .detect = mmc_sdio_detect,
691 .suspend = mmc_sdio_suspend,
692 .resume = mmc_sdio_resume,
697 * Starting point for SDIO card init.
699 int mmc_attach_sdio(struct mmc_host *host, u32 ocr)
703 struct mmc_card *card;
706 WARN_ON(!host->claimed);
708 mmc_attach_bus(host, &mmc_sdio_ops);
711 * Sanity check the voltages that the card claims to
715 printk(KERN_WARNING "%s: card claims to support voltages "
716 "below the defined range. These will be ignored.\n",
721 host->ocr = mmc_select_voltage(host, ocr);
724 * Can we support the voltage(s) of the card(s)?
732 * Detect and init the card.
734 err = mmc_sdio_init_card(host, host->ocr, NULL, 0);
740 * The number of functions on the card is encoded inside
743 funcs = (ocr & 0x70000000) >> 28;
744 card->sdio_funcs = 0;
746 #ifdef CONFIG_MMC_EMBEDDED_SDIO
747 if (host->embedded_sdio_data.funcs)
748 card->sdio_funcs = funcs = host->embedded_sdio_data.num_funcs;
752 * Initialize (but don't add) all present functions.
754 for (i = 0; i < funcs; i++, card->sdio_funcs++) {
755 #ifdef CONFIG_MMC_EMBEDDED_SDIO
756 if (host->embedded_sdio_data.funcs) {
757 struct sdio_func *tmp;
759 tmp = sdio_alloc_func(host->card);
763 card->sdio_func[i] = tmp;
764 tmp->class = host->embedded_sdio_data.funcs[i].f_class;
765 tmp->max_blksize = host->embedded_sdio_data.funcs[i].f_maxblksize;
766 tmp->vendor = card->cis.vendor;
767 tmp->device = card->cis.device;
770 err = sdio_init_func(host->card, i + 1);
773 #ifdef CONFIG_MMC_EMBEDDED_SDIO
778 mmc_release_host(host);
781 * First add the card to the driver model...
783 err = mmc_add_card(host->card);
788 * ...then the SDIO functions.
790 for (i = 0;i < funcs;i++) {
791 err = sdio_add_func(host->card->sdio_func[i]);
800 /* Remove without lock if the device has been added. */
801 mmc_sdio_remove(host);
802 mmc_claim_host(host);
804 /* And with lock if it hasn't been added. */
806 mmc_sdio_remove(host);
808 mmc_detach_bus(host);
809 mmc_release_host(host);
811 printk(KERN_ERR "%s: error %d whilst initialising SDIO card\n",
812 mmc_hostname(host), err);
817 int sdio_reset_comm(struct mmc_card *card)
819 struct mmc_host *host = card->host;
823 printk("%s():\n", __func__);
824 mmc_claim_host(host);
828 mmc_set_clock(host, host->f_min);
830 err = mmc_send_io_op_cond(host, 0, &ocr);
834 host->ocr = mmc_select_voltage(host, ocr);
840 err = mmc_send_io_op_cond(host, host->ocr, &ocr);
844 if (mmc_host_is_spi(host)) {
845 err = mmc_spi_set_crc(host, use_spi_crc);
850 if (!mmc_host_is_spi(host)) {
851 err = mmc_send_relative_addr(host, &card->rca);
854 mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
856 if (!mmc_host_is_spi(host)) {
857 err = mmc_select_card(card);
863 * Switch to high-speed (if supported).
865 err = sdio_enable_hs(card);
867 mmc_sd_go_highspeed(card);
872 * Change to the card's maximum speed.
874 mmc_set_clock(host, mmc_sdio_get_max_clock(card));
876 err = sdio_enable_4bit_bus(card);
878 mmc_set_bus_width(host, MMC_BUS_WIDTH_4);
882 mmc_release_host(host);
885 printk("%s: Error resetting SDIO communications (%d)\n",
886 mmc_hostname(host), err);
887 mmc_release_host(host);
890 EXPORT_SYMBOL(sdio_reset_comm);