15c4e6f13SPierre Ossman /* 25c4e6f13SPierre Ossman * linux/drivers/mmc/sdio.c 35c4e6f13SPierre Ossman * 45c4e6f13SPierre Ossman * Copyright 2006-2007 Pierre Ossman 55c4e6f13SPierre Ossman * 65c4e6f13SPierre Ossman * This program is free software; you can redistribute it and/or modify 75c4e6f13SPierre Ossman * it under the terms of the GNU General Public License as published by 85c4e6f13SPierre Ossman * the Free Software Foundation; either version 2 of the License, or (at 95c4e6f13SPierre Ossman * your option) any later version. 105c4e6f13SPierre Ossman */ 115c4e6f13SPierre Ossman 125c4e6f13SPierre Ossman #include <linux/err.h> 1381968561SOhad Ben-Cohen #include <linux/pm_runtime.h> 145c4e6f13SPierre Ossman 155c4e6f13SPierre Ossman #include <linux/mmc/host.h> 165c4e6f13SPierre Ossman #include <linux/mmc/card.h> 1735c66c19SPierre Ossman #include <linux/mmc/sdio.h> 18e29a7d73SPierre Ossman #include <linux/mmc/sdio_func.h> 19eab40687SOhad Ben-Cohen #include <linux/mmc/sdio_ids.h> 205c4e6f13SPierre Ossman 215c4e6f13SPierre Ossman #include "core.h" 225c4e6f13SPierre Ossman #include "bus.h" 2371578a1eSMichal Miroslaw #include "sd.h" 24e29a7d73SPierre Ossman #include "sdio_bus.h" 255c4e6f13SPierre Ossman #include "mmc_ops.h" 265c4e6f13SPierre Ossman #include "sd_ops.h" 275c4e6f13SPierre Ossman #include "sdio_ops.h" 28b7261261SNicolas Pitre #include "sdio_cis.h" 295c4e6f13SPierre Ossman 300597007fSPierre Ossman static int sdio_read_fbr(struct sdio_func *func) 310597007fSPierre Ossman { 320597007fSPierre Ossman int ret; 330597007fSPierre Ossman unsigned char data; 340597007fSPierre Ossman 35eab40687SOhad Ben-Cohen if (mmc_card_nonstd_func_interface(func->card)) { 36eab40687SOhad Ben-Cohen func->class = SDIO_CLASS_NONE; 37eab40687SOhad Ben-Cohen return 0; 38eab40687SOhad Ben-Cohen } 39eab40687SOhad Ben-Cohen 400597007fSPierre Ossman ret = mmc_io_rw_direct(func->card, 0, 0, 417616ee95SDavid Vrabel SDIO_FBR_BASE(func->num) + SDIO_FBR_STD_IF, 0, &data); 420597007fSPierre Ossman if (ret) 430597007fSPierre Ossman goto out; 440597007fSPierre Ossman 450597007fSPierre Ossman data &= 0x0f; 460597007fSPierre Ossman 470597007fSPierre Ossman if (data == 0x0f) { 480597007fSPierre Ossman ret = mmc_io_rw_direct(func->card, 0, 0, 497616ee95SDavid Vrabel SDIO_FBR_BASE(func->num) + SDIO_FBR_STD_IF_EXT, 0, &data); 500597007fSPierre Ossman if (ret) 510597007fSPierre Ossman goto out; 520597007fSPierre Ossman } 530597007fSPierre Ossman 540597007fSPierre Ossman func->class = data; 550597007fSPierre Ossman 560597007fSPierre Ossman out: 570597007fSPierre Ossman return ret; 580597007fSPierre Ossman } 590597007fSPierre Ossman 60e29a7d73SPierre Ossman static int sdio_init_func(struct mmc_card *card, unsigned int fn) 61e29a7d73SPierre Ossman { 620597007fSPierre Ossman int ret; 63e29a7d73SPierre Ossman struct sdio_func *func; 64e29a7d73SPierre Ossman 65e29a7d73SPierre Ossman BUG_ON(fn > SDIO_MAX_FUNCS); 66e29a7d73SPierre Ossman 67e29a7d73SPierre Ossman func = sdio_alloc_func(card); 68e29a7d73SPierre Ossman if (IS_ERR(func)) 69e29a7d73SPierre Ossman return PTR_ERR(func); 70e29a7d73SPierre Ossman 71e29a7d73SPierre Ossman func->num = fn; 72e29a7d73SPierre Ossman 736f51be3dSGrazvydas Ignotas if (!(card->quirks & MMC_QUIRK_NONSTD_SDIO)) { 740597007fSPierre Ossman ret = sdio_read_fbr(func); 750597007fSPierre Ossman if (ret) 760597007fSPierre Ossman goto fail; 770597007fSPierre Ossman 781a632f8cSPierre Ossman ret = sdio_read_func_cis(func); 79b7261261SNicolas Pitre if (ret) 80b7261261SNicolas Pitre goto fail; 816f51be3dSGrazvydas Ignotas } else { 826f51be3dSGrazvydas Ignotas func->vendor = func->card->cis.vendor; 836f51be3dSGrazvydas Ignotas func->device = func->card->cis.device; 846f51be3dSGrazvydas Ignotas func->max_blksize = func->card->cis.blksize; 856f51be3dSGrazvydas Ignotas } 86b7261261SNicolas Pitre 87e29a7d73SPierre Ossman card->sdio_func[fn - 1] = func; 88e29a7d73SPierre Ossman 89e29a7d73SPierre Ossman return 0; 900597007fSPierre Ossman 910597007fSPierre Ossman fail: 920597007fSPierre Ossman /* 930597007fSPierre Ossman * It is okay to remove the function here even though we hold 940597007fSPierre Ossman * the host lock as we haven't registered the device yet. 950597007fSPierre Ossman */ 960597007fSPierre Ossman sdio_remove_func(func); 970597007fSPierre Ossman return ret; 98e29a7d73SPierre Ossman } 99e29a7d73SPierre Ossman 10035c66c19SPierre Ossman static int sdio_read_cccr(struct mmc_card *card) 10135c66c19SPierre Ossman { 10235c66c19SPierre Ossman int ret; 10335c66c19SPierre Ossman int cccr_vsn; 10435c66c19SPierre Ossman unsigned char data; 10535c66c19SPierre Ossman 10635c66c19SPierre Ossman memset(&card->cccr, 0, sizeof(struct sdio_cccr)); 10735c66c19SPierre Ossman 10835c66c19SPierre Ossman ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_CCCR, 0, &data); 10935c66c19SPierre Ossman if (ret) 11035c66c19SPierre Ossman goto out; 11135c66c19SPierre Ossman 11235c66c19SPierre Ossman cccr_vsn = data & 0x0f; 11335c66c19SPierre Ossman 11435c66c19SPierre Ossman if (cccr_vsn > SDIO_CCCR_REV_1_20) { 11535c66c19SPierre Ossman printk(KERN_ERR "%s: unrecognised CCCR structure version %d\n", 11635c66c19SPierre Ossman mmc_hostname(card->host), cccr_vsn); 11735c66c19SPierre Ossman return -EINVAL; 11835c66c19SPierre Ossman } 11935c66c19SPierre Ossman 12035c66c19SPierre Ossman card->cccr.sdio_vsn = (data & 0xf0) >> 4; 12135c66c19SPierre Ossman 12235c66c19SPierre Ossman ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_CAPS, 0, &data); 12335c66c19SPierre Ossman if (ret) 12435c66c19SPierre Ossman goto out; 12535c66c19SPierre Ossman 12635c66c19SPierre Ossman if (data & SDIO_CCCR_CAP_SMB) 12735c66c19SPierre Ossman card->cccr.multi_block = 1; 12835c66c19SPierre Ossman if (data & SDIO_CCCR_CAP_LSC) 12935c66c19SPierre Ossman card->cccr.low_speed = 1; 13035c66c19SPierre Ossman if (data & SDIO_CCCR_CAP_4BLS) 13135c66c19SPierre Ossman card->cccr.wide_bus = 1; 13235c66c19SPierre Ossman 13335c66c19SPierre Ossman if (cccr_vsn >= SDIO_CCCR_REV_1_10) { 13435c66c19SPierre Ossman ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_POWER, 0, &data); 13535c66c19SPierre Ossman if (ret) 13635c66c19SPierre Ossman goto out; 13735c66c19SPierre Ossman 13835c66c19SPierre Ossman if (data & SDIO_POWER_SMPC) 13935c66c19SPierre Ossman card->cccr.high_power = 1; 14035c66c19SPierre Ossman } 14135c66c19SPierre Ossman 14235c66c19SPierre Ossman if (cccr_vsn >= SDIO_CCCR_REV_1_20) { 14335c66c19SPierre Ossman ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_SPEED, 0, &data); 14435c66c19SPierre Ossman if (ret) 14535c66c19SPierre Ossman goto out; 14635c66c19SPierre Ossman 14735c66c19SPierre Ossman if (data & SDIO_SPEED_SHS) 14835c66c19SPierre Ossman card->cccr.high_speed = 1; 14935c66c19SPierre Ossman } 15035c66c19SPierre Ossman 15135c66c19SPierre Ossman out: 15235c66c19SPierre Ossman return ret; 15335c66c19SPierre Ossman } 15435c66c19SPierre Ossman 1554ff6471cSPierre Ossman static int sdio_enable_wide(struct mmc_card *card) 1564ff6471cSPierre Ossman { 1574ff6471cSPierre Ossman int ret; 1584ff6471cSPierre Ossman u8 ctrl; 1594ff6471cSPierre Ossman 1604ff6471cSPierre Ossman if (!(card->host->caps & MMC_CAP_4_BIT_DATA)) 1614ff6471cSPierre Ossman return 0; 1624ff6471cSPierre Ossman 1634ff6471cSPierre Ossman if (card->cccr.low_speed && !card->cccr.wide_bus) 1644ff6471cSPierre Ossman return 0; 1654ff6471cSPierre Ossman 1664ff6471cSPierre Ossman ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl); 1674ff6471cSPierre Ossman if (ret) 1684ff6471cSPierre Ossman return ret; 1694ff6471cSPierre Ossman 1704ff6471cSPierre Ossman ctrl |= SDIO_BUS_WIDTH_4BIT; 1714ff6471cSPierre Ossman 1724ff6471cSPierre Ossman ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL); 1734ff6471cSPierre Ossman if (ret) 1744ff6471cSPierre Ossman return ret; 1754ff6471cSPierre Ossman 1767310ece8SMichal Miroslaw return 1; 1774ff6471cSPierre Ossman } 1784ff6471cSPierre Ossman 1795c4e6f13SPierre Ossman /* 180006ebd5dSOhad Ben-Cohen * If desired, disconnect the pull-up resistor on CD/DAT[3] (pin 1) 181006ebd5dSOhad Ben-Cohen * of the card. This may be required on certain setups of boards, 182006ebd5dSOhad Ben-Cohen * controllers and embedded sdio device which do not need the card's 183006ebd5dSOhad Ben-Cohen * pull-up. As a result, card detection is disabled and power is saved. 184006ebd5dSOhad Ben-Cohen */ 185006ebd5dSOhad Ben-Cohen static int sdio_disable_cd(struct mmc_card *card) 186006ebd5dSOhad Ben-Cohen { 187006ebd5dSOhad Ben-Cohen int ret; 188006ebd5dSOhad Ben-Cohen u8 ctrl; 189006ebd5dSOhad Ben-Cohen 1902059a02dSOhad Ben-Cohen if (!mmc_card_disable_cd(card)) 191006ebd5dSOhad Ben-Cohen return 0; 192006ebd5dSOhad Ben-Cohen 193006ebd5dSOhad Ben-Cohen ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl); 194006ebd5dSOhad Ben-Cohen if (ret) 195006ebd5dSOhad Ben-Cohen return ret; 196006ebd5dSOhad Ben-Cohen 197006ebd5dSOhad Ben-Cohen ctrl |= SDIO_BUS_CD_DISABLE; 198006ebd5dSOhad Ben-Cohen 199006ebd5dSOhad Ben-Cohen return mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL); 200006ebd5dSOhad Ben-Cohen } 201006ebd5dSOhad Ben-Cohen 202006ebd5dSOhad Ben-Cohen /* 2036b5eda36SDaniel Drake * Devices that remain active during a system suspend are 2046b5eda36SDaniel Drake * put back into 1-bit mode. 2056b5eda36SDaniel Drake */ 2066b5eda36SDaniel Drake static int sdio_disable_wide(struct mmc_card *card) 2076b5eda36SDaniel Drake { 2086b5eda36SDaniel Drake int ret; 2096b5eda36SDaniel Drake u8 ctrl; 2106b5eda36SDaniel Drake 2116b5eda36SDaniel Drake if (!(card->host->caps & MMC_CAP_4_BIT_DATA)) 2126b5eda36SDaniel Drake return 0; 2136b5eda36SDaniel Drake 2146b5eda36SDaniel Drake if (card->cccr.low_speed && !card->cccr.wide_bus) 2156b5eda36SDaniel Drake return 0; 2166b5eda36SDaniel Drake 2176b5eda36SDaniel Drake ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl); 2186b5eda36SDaniel Drake if (ret) 2196b5eda36SDaniel Drake return ret; 2206b5eda36SDaniel Drake 2216b5eda36SDaniel Drake if (!(ctrl & SDIO_BUS_WIDTH_4BIT)) 2226b5eda36SDaniel Drake return 0; 2236b5eda36SDaniel Drake 2246b5eda36SDaniel Drake ctrl &= ~SDIO_BUS_WIDTH_4BIT; 2256b5eda36SDaniel Drake ctrl |= SDIO_BUS_ASYNC_INT; 2266b5eda36SDaniel Drake 2276b5eda36SDaniel Drake ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL); 2286b5eda36SDaniel Drake if (ret) 2296b5eda36SDaniel Drake return ret; 2306b5eda36SDaniel Drake 2316b5eda36SDaniel Drake mmc_set_bus_width(card->host, MMC_BUS_WIDTH_1); 2326b5eda36SDaniel Drake 2336b5eda36SDaniel Drake return 0; 2346b5eda36SDaniel Drake } 2356b5eda36SDaniel Drake 2367310ece8SMichal Miroslaw 2377310ece8SMichal Miroslaw static int sdio_enable_4bit_bus(struct mmc_card *card) 2387310ece8SMichal Miroslaw { 2397310ece8SMichal Miroslaw int err; 2407310ece8SMichal Miroslaw 2417310ece8SMichal Miroslaw if (card->type == MMC_TYPE_SDIO) 2427310ece8SMichal Miroslaw return sdio_enable_wide(card); 2437310ece8SMichal Miroslaw 2447310ece8SMichal Miroslaw if ((card->host->caps & MMC_CAP_4_BIT_DATA) && 2457310ece8SMichal Miroslaw (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) { 2467310ece8SMichal Miroslaw err = mmc_app_set_bus_width(card, MMC_BUS_WIDTH_4); 2477310ece8SMichal Miroslaw if (err) 2487310ece8SMichal Miroslaw return err; 2497310ece8SMichal Miroslaw } else 2507310ece8SMichal Miroslaw return 0; 2517310ece8SMichal Miroslaw 2527310ece8SMichal Miroslaw err = sdio_enable_wide(card); 2537310ece8SMichal Miroslaw if (err <= 0) 2547310ece8SMichal Miroslaw mmc_app_set_bus_width(card, MMC_BUS_WIDTH_1); 2557310ece8SMichal Miroslaw 2567310ece8SMichal Miroslaw return err; 2577310ece8SMichal Miroslaw } 2587310ece8SMichal Miroslaw 2597310ece8SMichal Miroslaw 2606b5eda36SDaniel Drake /* 261d16f5770SPierre Ossman * Test if the card supports high-speed mode and, if so, switch to it. 262d16f5770SPierre Ossman */ 2637310ece8SMichal Miroslaw static int mmc_sdio_switch_hs(struct mmc_card *card, int enable) 264d16f5770SPierre Ossman { 265d16f5770SPierre Ossman int ret; 266d16f5770SPierre Ossman u8 speed; 267d16f5770SPierre Ossman 268d16f5770SPierre Ossman if (!(card->host->caps & MMC_CAP_SD_HIGHSPEED)) 269d16f5770SPierre Ossman return 0; 270d16f5770SPierre Ossman 271d16f5770SPierre Ossman if (!card->cccr.high_speed) 272d16f5770SPierre Ossman return 0; 273d16f5770SPierre Ossman 274d16f5770SPierre Ossman ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_SPEED, 0, &speed); 275d16f5770SPierre Ossman if (ret) 276d16f5770SPierre Ossman return ret; 277d16f5770SPierre Ossman 2787310ece8SMichal Miroslaw if (enable) 279d16f5770SPierre Ossman speed |= SDIO_SPEED_EHS; 2807310ece8SMichal Miroslaw else 2817310ece8SMichal Miroslaw speed &= ~SDIO_SPEED_EHS; 282d16f5770SPierre Ossman 283d16f5770SPierre Ossman ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_SPEED, speed, NULL); 284d16f5770SPierre Ossman if (ret) 285d16f5770SPierre Ossman return ret; 286d16f5770SPierre Ossman 28771578a1eSMichal Miroslaw return 1; 28871578a1eSMichal Miroslaw } 289d16f5770SPierre Ossman 2907310ece8SMichal Miroslaw /* 2917310ece8SMichal Miroslaw * Enable SDIO/combo card's high-speed mode. Return 0/1 if [not]supported. 2927310ece8SMichal Miroslaw */ 2937310ece8SMichal Miroslaw static int sdio_enable_hs(struct mmc_card *card) 2947310ece8SMichal Miroslaw { 2957310ece8SMichal Miroslaw int ret; 2967310ece8SMichal Miroslaw 2977310ece8SMichal Miroslaw ret = mmc_sdio_switch_hs(card, true); 2987310ece8SMichal Miroslaw if (ret <= 0 || card->type == MMC_TYPE_SDIO) 2997310ece8SMichal Miroslaw return ret; 3007310ece8SMichal Miroslaw 3017310ece8SMichal Miroslaw ret = mmc_sd_switch_hs(card); 3027310ece8SMichal Miroslaw if (ret <= 0) 3037310ece8SMichal Miroslaw mmc_sdio_switch_hs(card, false); 3047310ece8SMichal Miroslaw 3057310ece8SMichal Miroslaw return ret; 3067310ece8SMichal Miroslaw } 3077310ece8SMichal Miroslaw 30871578a1eSMichal Miroslaw static unsigned mmc_sdio_get_max_clock(struct mmc_card *card) 30971578a1eSMichal Miroslaw { 31071578a1eSMichal Miroslaw unsigned max_dtr; 31171578a1eSMichal Miroslaw 31271578a1eSMichal Miroslaw if (mmc_card_highspeed(card)) { 31371578a1eSMichal Miroslaw /* 31471578a1eSMichal Miroslaw * The SDIO specification doesn't mention how 31571578a1eSMichal Miroslaw * the CIS transfer speed register relates to 31671578a1eSMichal Miroslaw * high-speed, but it seems that 50 MHz is 31771578a1eSMichal Miroslaw * mandatory. 31871578a1eSMichal Miroslaw */ 31971578a1eSMichal Miroslaw max_dtr = 50000000; 32071578a1eSMichal Miroslaw } else { 32171578a1eSMichal Miroslaw max_dtr = card->cis.max_dtr; 32271578a1eSMichal Miroslaw } 32371578a1eSMichal Miroslaw 3247310ece8SMichal Miroslaw if (card->type == MMC_TYPE_SD_COMBO) 3257310ece8SMichal Miroslaw max_dtr = min(max_dtr, mmc_sd_get_max_clock(card)); 3267310ece8SMichal Miroslaw 32771578a1eSMichal Miroslaw return max_dtr; 328d16f5770SPierre Ossman } 329d16f5770SPierre Ossman 330d16f5770SPierre Ossman /* 33117d33e14SNicolas Pitre * Handle the detection and initialisation of a card. 33217d33e14SNicolas Pitre * 33317d33e14SNicolas Pitre * In the case of a resume, "oldcard" will contain the card 33417d33e14SNicolas Pitre * we're trying to reinitialise. 33517d33e14SNicolas Pitre */ 33617d33e14SNicolas Pitre static int mmc_sdio_init_card(struct mmc_host *host, u32 ocr, 3373bca4cf7SChris Ball struct mmc_card *oldcard, int powered_resume) 33817d33e14SNicolas Pitre { 33917d33e14SNicolas Pitre struct mmc_card *card; 34017d33e14SNicolas Pitre int err; 34117d33e14SNicolas Pitre 34217d33e14SNicolas Pitre BUG_ON(!host); 34317d33e14SNicolas Pitre WARN_ON(!host->claimed); 34417d33e14SNicolas Pitre 34517d33e14SNicolas Pitre /* 34617d33e14SNicolas Pitre * Inform the card of the voltage 34717d33e14SNicolas Pitre */ 3483bca4cf7SChris Ball if (!powered_resume) { 34917d33e14SNicolas Pitre err = mmc_send_io_op_cond(host, host->ocr, &ocr); 35017d33e14SNicolas Pitre if (err) 35117d33e14SNicolas Pitre goto err; 3523bca4cf7SChris Ball } 35317d33e14SNicolas Pitre 35417d33e14SNicolas Pitre /* 35517d33e14SNicolas Pitre * For SPI, enable CRC as appropriate. 35617d33e14SNicolas Pitre */ 35717d33e14SNicolas Pitre if (mmc_host_is_spi(host)) { 35817d33e14SNicolas Pitre err = mmc_spi_set_crc(host, use_spi_crc); 35917d33e14SNicolas Pitre if (err) 36017d33e14SNicolas Pitre goto err; 36117d33e14SNicolas Pitre } 36217d33e14SNicolas Pitre 36317d33e14SNicolas Pitre /* 36417d33e14SNicolas Pitre * Allocate card structure. 36517d33e14SNicolas Pitre */ 36617d33e14SNicolas Pitre card = mmc_alloc_card(host, NULL); 36717d33e14SNicolas Pitre if (IS_ERR(card)) { 36817d33e14SNicolas Pitre err = PTR_ERR(card); 36917d33e14SNicolas Pitre goto err; 37017d33e14SNicolas Pitre } 37117d33e14SNicolas Pitre 372d6d50a15SArindam Nath if ((ocr & R4_MEMORY_PRESENT) && 373d6d50a15SArindam Nath mmc_sd_get_cid(host, host->ocr & ocr, card->raw_cid, NULL) == 0) { 3747310ece8SMichal Miroslaw card->type = MMC_TYPE_SD_COMBO; 3757310ece8SMichal Miroslaw 3767310ece8SMichal Miroslaw if (oldcard && (oldcard->type != MMC_TYPE_SD_COMBO || 3777310ece8SMichal Miroslaw memcmp(card->raw_cid, oldcard->raw_cid, sizeof(card->raw_cid)) != 0)) { 3787310ece8SMichal Miroslaw mmc_remove_card(card); 3797310ece8SMichal Miroslaw return -ENOENT; 3807310ece8SMichal Miroslaw } 3817310ece8SMichal Miroslaw } else { 38217d33e14SNicolas Pitre card->type = MMC_TYPE_SDIO; 38317d33e14SNicolas Pitre 3847310ece8SMichal Miroslaw if (oldcard && oldcard->type != MMC_TYPE_SDIO) { 3857310ece8SMichal Miroslaw mmc_remove_card(card); 3867310ece8SMichal Miroslaw return -ENOENT; 3877310ece8SMichal Miroslaw } 3887310ece8SMichal Miroslaw } 3897310ece8SMichal Miroslaw 39017d33e14SNicolas Pitre /* 3913fcb027dSDaniel Mack * Call the optional HC's init_card function to handle quirks. 3923fcb027dSDaniel Mack */ 3933fcb027dSDaniel Mack if (host->ops->init_card) 3943fcb027dSDaniel Mack host->ops->init_card(host, card); 3953fcb027dSDaniel Mack 3963fcb027dSDaniel Mack /* 39717d33e14SNicolas Pitre * For native busses: set card RCA and quit open drain mode. 39817d33e14SNicolas Pitre */ 3993bca4cf7SChris Ball if (!powered_resume && !mmc_host_is_spi(host)) { 40017d33e14SNicolas Pitre err = mmc_send_relative_addr(host, &card->rca); 40117d33e14SNicolas Pitre if (err) 40217d33e14SNicolas Pitre goto remove; 40317d33e14SNicolas Pitre 4040aab3995SStefan Nilsson XK /* 4050aab3995SStefan Nilsson XK * Update oldcard with the new RCA received from the SDIO 4060aab3995SStefan Nilsson XK * device -- we're doing this so that it's updated in the 4070aab3995SStefan Nilsson XK * "card" struct when oldcard overwrites that later. 4080aab3995SStefan Nilsson XK */ 4090aab3995SStefan Nilsson XK if (oldcard) 4100aab3995SStefan Nilsson XK oldcard->rca = card->rca; 4110aab3995SStefan Nilsson XK 41217d33e14SNicolas Pitre mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL); 41317d33e14SNicolas Pitre } 41417d33e14SNicolas Pitre 41517d33e14SNicolas Pitre /* 4167310ece8SMichal Miroslaw * Read CSD, before selecting the card 4177310ece8SMichal Miroslaw */ 4187310ece8SMichal Miroslaw if (!oldcard && card->type == MMC_TYPE_SD_COMBO) { 4197310ece8SMichal Miroslaw err = mmc_sd_get_csd(host, card); 4207310ece8SMichal Miroslaw if (err) 4217310ece8SMichal Miroslaw return err; 4227310ece8SMichal Miroslaw 4237310ece8SMichal Miroslaw mmc_decode_cid(card); 4247310ece8SMichal Miroslaw } 4257310ece8SMichal Miroslaw 4267310ece8SMichal Miroslaw /* 42717d33e14SNicolas Pitre * Select card, as all following commands rely on that. 42817d33e14SNicolas Pitre */ 4293bca4cf7SChris Ball if (!powered_resume && !mmc_host_is_spi(host)) { 43017d33e14SNicolas Pitre err = mmc_select_card(card); 43117d33e14SNicolas Pitre if (err) 43217d33e14SNicolas Pitre goto remove; 43317d33e14SNicolas Pitre } 43417d33e14SNicolas Pitre 4356f51be3dSGrazvydas Ignotas if (card->quirks & MMC_QUIRK_NONSTD_SDIO) { 4366f51be3dSGrazvydas Ignotas /* 4376f51be3dSGrazvydas Ignotas * This is non-standard SDIO device, meaning it doesn't 4386f51be3dSGrazvydas Ignotas * have any CIA (Common I/O area) registers present. 4396f51be3dSGrazvydas Ignotas * It's host's responsibility to fill cccr and cis 4406f51be3dSGrazvydas Ignotas * structures in init_card(). 4416f51be3dSGrazvydas Ignotas */ 4426f51be3dSGrazvydas Ignotas mmc_set_clock(host, card->cis.max_dtr); 4436f51be3dSGrazvydas Ignotas 4446f51be3dSGrazvydas Ignotas if (card->cccr.high_speed) { 4456f51be3dSGrazvydas Ignotas mmc_card_set_highspeed(card); 4466f51be3dSGrazvydas Ignotas mmc_set_timing(card->host, MMC_TIMING_SD_HS); 4476f51be3dSGrazvydas Ignotas } 4486f51be3dSGrazvydas Ignotas 4496f51be3dSGrazvydas Ignotas goto finish; 4506f51be3dSGrazvydas Ignotas } 4516f51be3dSGrazvydas Ignotas 45217d33e14SNicolas Pitre /* 45317d33e14SNicolas Pitre * Read the common registers. 45417d33e14SNicolas Pitre */ 45517d33e14SNicolas Pitre err = sdio_read_cccr(card); 45617d33e14SNicolas Pitre if (err) 45717d33e14SNicolas Pitre goto remove; 45817d33e14SNicolas Pitre 45917d33e14SNicolas Pitre /* 46017d33e14SNicolas Pitre * Read the common CIS tuples. 46117d33e14SNicolas Pitre */ 46217d33e14SNicolas Pitre err = sdio_read_common_cis(card); 46317d33e14SNicolas Pitre if (err) 46417d33e14SNicolas Pitre goto remove; 46517d33e14SNicolas Pitre 46617d33e14SNicolas Pitre if (oldcard) { 46717d33e14SNicolas Pitre int same = (card->cis.vendor == oldcard->cis.vendor && 46817d33e14SNicolas Pitre card->cis.device == oldcard->cis.device); 46917d33e14SNicolas Pitre mmc_remove_card(card); 4707310ece8SMichal Miroslaw if (!same) 4717310ece8SMichal Miroslaw return -ENOENT; 4727310ece8SMichal Miroslaw 47317d33e14SNicolas Pitre card = oldcard; 47417d33e14SNicolas Pitre } 47532780cd1SAndrei Warkentin mmc_fixup_device(card, NULL); 47617d33e14SNicolas Pitre 4777310ece8SMichal Miroslaw if (card->type == MMC_TYPE_SD_COMBO) { 4787310ece8SMichal Miroslaw err = mmc_sd_setup_card(host, card, oldcard != NULL); 4797310ece8SMichal Miroslaw /* handle as SDIO-only card if memory init failed */ 4807310ece8SMichal Miroslaw if (err) { 4817310ece8SMichal Miroslaw mmc_go_idle(host); 4827310ece8SMichal Miroslaw if (mmc_host_is_spi(host)) 4837310ece8SMichal Miroslaw /* should not fail, as it worked previously */ 4847310ece8SMichal Miroslaw mmc_spi_set_crc(host, use_spi_crc); 4857310ece8SMichal Miroslaw card->type = MMC_TYPE_SDIO; 4867310ece8SMichal Miroslaw } else 4877310ece8SMichal Miroslaw card->dev.type = &sd_type; 4887310ece8SMichal Miroslaw } 4897310ece8SMichal Miroslaw 4907310ece8SMichal Miroslaw /* 4917310ece8SMichal Miroslaw * If needed, disconnect card detection pull-up resistor. 4927310ece8SMichal Miroslaw */ 4937310ece8SMichal Miroslaw err = sdio_disable_cd(card); 4947310ece8SMichal Miroslaw if (err) 4957310ece8SMichal Miroslaw goto remove; 4967310ece8SMichal Miroslaw 49717d33e14SNicolas Pitre /* 49817d33e14SNicolas Pitre * Switch to high-speed (if supported). 49917d33e14SNicolas Pitre */ 50017d33e14SNicolas Pitre err = sdio_enable_hs(card); 50171578a1eSMichal Miroslaw if (err > 0) 50271578a1eSMichal Miroslaw mmc_sd_go_highspeed(card); 50371578a1eSMichal Miroslaw else if (err) 50417d33e14SNicolas Pitre goto remove; 50517d33e14SNicolas Pitre 50617d33e14SNicolas Pitre /* 50717d33e14SNicolas Pitre * Change to the card's maximum speed. 50817d33e14SNicolas Pitre */ 50971578a1eSMichal Miroslaw mmc_set_clock(host, mmc_sdio_get_max_clock(card)); 51017d33e14SNicolas Pitre 51117d33e14SNicolas Pitre /* 51217d33e14SNicolas Pitre * Switch to wider bus (if supported). 51317d33e14SNicolas Pitre */ 5147310ece8SMichal Miroslaw err = sdio_enable_4bit_bus(card); 5157310ece8SMichal Miroslaw if (err > 0) 5167310ece8SMichal Miroslaw mmc_set_bus_width(card->host, MMC_BUS_WIDTH_4); 5177310ece8SMichal Miroslaw else if (err) 51817d33e14SNicolas Pitre goto remove; 51917d33e14SNicolas Pitre 5206f51be3dSGrazvydas Ignotas finish: 52117d33e14SNicolas Pitre if (!oldcard) 52217d33e14SNicolas Pitre host->card = card; 52317d33e14SNicolas Pitre return 0; 52417d33e14SNicolas Pitre 52517d33e14SNicolas Pitre remove: 52617d33e14SNicolas Pitre if (!oldcard) 52717d33e14SNicolas Pitre mmc_remove_card(card); 52817d33e14SNicolas Pitre 52917d33e14SNicolas Pitre err: 53017d33e14SNicolas Pitre return err; 53117d33e14SNicolas Pitre } 53217d33e14SNicolas Pitre 53317d33e14SNicolas Pitre /* 5345c4e6f13SPierre Ossman * Host is being removed. Free up the current card. 5355c4e6f13SPierre Ossman */ 5365c4e6f13SPierre Ossman static void mmc_sdio_remove(struct mmc_host *host) 5375c4e6f13SPierre Ossman { 538e29a7d73SPierre Ossman int i; 539e29a7d73SPierre Ossman 5405c4e6f13SPierre Ossman BUG_ON(!host); 5415c4e6f13SPierre Ossman BUG_ON(!host->card); 5425c4e6f13SPierre Ossman 543e29a7d73SPierre Ossman for (i = 0;i < host->card->sdio_funcs;i++) { 544e29a7d73SPierre Ossman if (host->card->sdio_func[i]) { 545e29a7d73SPierre Ossman sdio_remove_func(host->card->sdio_func[i]); 546e29a7d73SPierre Ossman host->card->sdio_func[i] = NULL; 547e29a7d73SPierre Ossman } 548e29a7d73SPierre Ossman } 549e29a7d73SPierre Ossman 5505c4e6f13SPierre Ossman mmc_remove_card(host->card); 5515c4e6f13SPierre Ossman host->card = NULL; 5525c4e6f13SPierre Ossman } 5535c4e6f13SPierre Ossman 5545c4e6f13SPierre Ossman /* 5555c4e6f13SPierre Ossman * Card detection callback from host. 5565c4e6f13SPierre Ossman */ 5575c4e6f13SPierre Ossman static void mmc_sdio_detect(struct mmc_host *host) 5585c4e6f13SPierre Ossman { 5595c4e6f13SPierre Ossman int err; 5605c4e6f13SPierre Ossman 5615c4e6f13SPierre Ossman BUG_ON(!host); 5625c4e6f13SPierre Ossman BUG_ON(!host->card); 5635c4e6f13SPierre Ossman 56487973ba2SOhad Ben-Cohen /* Make sure card is powered before detecting it */ 565ed919b01SOhad Ben-Cohen if (host->caps & MMC_CAP_POWER_OFF_CARD) { 56687973ba2SOhad Ben-Cohen err = pm_runtime_get_sync(&host->card->dev); 56787973ba2SOhad Ben-Cohen if (err < 0) 56887973ba2SOhad Ben-Cohen goto out; 569ed919b01SOhad Ben-Cohen } 57087973ba2SOhad Ben-Cohen 5715c4e6f13SPierre Ossman mmc_claim_host(host); 5725c4e6f13SPierre Ossman 5735c4e6f13SPierre Ossman /* 5745c4e6f13SPierre Ossman * Just check if our card has been removed. 5755c4e6f13SPierre Ossman */ 5765c4e6f13SPierre Ossman err = mmc_select_card(host->card); 5775c4e6f13SPierre Ossman 5785c4e6f13SPierre Ossman mmc_release_host(host); 5795c4e6f13SPierre Ossman 5804d0812c3SOhad Ben-Cohen /* 5814d0812c3SOhad Ben-Cohen * Tell PM core it's OK to power off the card now. 5824d0812c3SOhad Ben-Cohen * 5834d0812c3SOhad Ben-Cohen * The _sync variant is used in order to ensure that the card 5844d0812c3SOhad Ben-Cohen * is left powered off in case an error occurred, and the card 5854d0812c3SOhad Ben-Cohen * is going to be removed. 5864d0812c3SOhad Ben-Cohen * 5874d0812c3SOhad Ben-Cohen * Since there is no specific reason to believe a new user 5884d0812c3SOhad Ben-Cohen * is about to show up at this point, the _sync variant is 5894d0812c3SOhad Ben-Cohen * desirable anyway. 5904d0812c3SOhad Ben-Cohen */ 591ed919b01SOhad Ben-Cohen if (host->caps & MMC_CAP_POWER_OFF_CARD) 5924d0812c3SOhad Ben-Cohen pm_runtime_put_sync(&host->card->dev); 5934d0812c3SOhad Ben-Cohen 59487973ba2SOhad Ben-Cohen out: 5955c4e6f13SPierre Ossman if (err) { 5965c4e6f13SPierre Ossman mmc_sdio_remove(host); 5975c4e6f13SPierre Ossman 5985c4e6f13SPierre Ossman mmc_claim_host(host); 5995c4e6f13SPierre Ossman mmc_detach_bus(host); 6005c4e6f13SPierre Ossman mmc_release_host(host); 6015c4e6f13SPierre Ossman } 6025c4e6f13SPierre Ossman } 6035c4e6f13SPierre Ossman 60417d33e14SNicolas Pitre /* 60517d33e14SNicolas Pitre * SDIO suspend. We need to suspend all functions separately. 60617d33e14SNicolas Pitre * Therefore all registered functions must have drivers with suspend 60717d33e14SNicolas Pitre * and resume methods. Failing that we simply remove the whole card. 60817d33e14SNicolas Pitre */ 60995cdfb72SNicolas Pitre static int mmc_sdio_suspend(struct mmc_host *host) 61017d33e14SNicolas Pitre { 61195cdfb72SNicolas Pitre int i, err = 0; 61217d33e14SNicolas Pitre 61317d33e14SNicolas Pitre for (i = 0; i < host->card->sdio_funcs; i++) { 61417d33e14SNicolas Pitre struct sdio_func *func = host->card->sdio_func[i]; 61517d33e14SNicolas Pitre if (func && sdio_func_present(func) && func->dev.driver) { 61617d33e14SNicolas Pitre const struct dev_pm_ops *pmops = func->dev.driver->pm; 61717d33e14SNicolas Pitre if (!pmops || !pmops->suspend || !pmops->resume) { 61895cdfb72SNicolas Pitre /* force removal of entire card in that case */ 61995cdfb72SNicolas Pitre err = -ENOSYS; 62095cdfb72SNicolas Pitre } else 62195cdfb72SNicolas Pitre err = pmops->suspend(&func->dev); 62295cdfb72SNicolas Pitre if (err) 62395cdfb72SNicolas Pitre break; 62417d33e14SNicolas Pitre } 62517d33e14SNicolas Pitre } 62695cdfb72SNicolas Pitre while (err && --i >= 0) { 62717d33e14SNicolas Pitre struct sdio_func *func = host->card->sdio_func[i]; 62817d33e14SNicolas Pitre if (func && sdio_func_present(func) && func->dev.driver) { 62917d33e14SNicolas Pitre const struct dev_pm_ops *pmops = func->dev.driver->pm; 63017d33e14SNicolas Pitre pmops->resume(&func->dev); 63117d33e14SNicolas Pitre } 63217d33e14SNicolas Pitre } 63395cdfb72SNicolas Pitre 6346b93d01fSOhad Ben-Cohen if (!err && mmc_card_keep_power(host) && mmc_card_wake_sdio_irq(host)) { 6356b5eda36SDaniel Drake mmc_claim_host(host); 6366b5eda36SDaniel Drake sdio_disable_wide(host->card); 6376b5eda36SDaniel Drake mmc_release_host(host); 6386b5eda36SDaniel Drake } 6396b5eda36SDaniel Drake 64095cdfb72SNicolas Pitre return err; 64195cdfb72SNicolas Pitre } 64295cdfb72SNicolas Pitre 64395cdfb72SNicolas Pitre static int mmc_sdio_resume(struct mmc_host *host) 64495cdfb72SNicolas Pitre { 645080bc977SOhad Ben-Cohen int i, err = 0; 64695cdfb72SNicolas Pitre 64795cdfb72SNicolas Pitre BUG_ON(!host); 64895cdfb72SNicolas Pitre BUG_ON(!host->card); 64995cdfb72SNicolas Pitre 65095cdfb72SNicolas Pitre /* Basic card reinitialization. */ 65195cdfb72SNicolas Pitre mmc_claim_host(host); 652080bc977SOhad Ben-Cohen 653080bc977SOhad Ben-Cohen /* No need to reinitialize powered-resumed nonremovable cards */ 654a5e9425dSOhad Ben-Cohen if (mmc_card_is_removable(host) || !mmc_card_keep_power(host)) 6553bca4cf7SChris Ball err = mmc_sdio_init_card(host, host->ocr, host->card, 656a5e9425dSOhad Ben-Cohen mmc_card_keep_power(host)); 6576b93d01fSOhad Ben-Cohen else if (mmc_card_keep_power(host) && mmc_card_wake_sdio_irq(host)) { 658080bc977SOhad Ben-Cohen /* We may have switched to 1-bit mode during suspend */ 659080bc977SOhad Ben-Cohen err = sdio_enable_4bit_bus(host->card); 660080bc977SOhad Ben-Cohen if (err > 0) { 661080bc977SOhad Ben-Cohen mmc_set_bus_width(host, MMC_BUS_WIDTH_4); 662080bc977SOhad Ben-Cohen err = 0; 663080bc977SOhad Ben-Cohen } 664080bc977SOhad Ben-Cohen } 665080bc977SOhad Ben-Cohen 66640216842SNicolas Pitre if (!err && host->sdio_irqs) 66740216842SNicolas Pitre mmc_signal_sdio_irq(host); 66895cdfb72SNicolas Pitre mmc_release_host(host); 66995cdfb72SNicolas Pitre 67095cdfb72SNicolas Pitre /* 67195cdfb72SNicolas Pitre * If the card looked to be the same as before suspending, then 67295cdfb72SNicolas Pitre * we proceed to resume all card functions. If one of them returns 67395cdfb72SNicolas Pitre * an error then we simply return that error to the core and the 67495cdfb72SNicolas Pitre * card will be redetected as new. It is the responsibility of 67595cdfb72SNicolas Pitre * the function driver to perform further tests with the extra 67695cdfb72SNicolas Pitre * knowledge it has of the card to confirm the card is indeed the 67795cdfb72SNicolas Pitre * same as before suspending (same MAC address for network cards, 67895cdfb72SNicolas Pitre * etc.) and return an error otherwise. 67995cdfb72SNicolas Pitre */ 68095cdfb72SNicolas Pitre for (i = 0; !err && i < host->card->sdio_funcs; i++) { 68195cdfb72SNicolas Pitre struct sdio_func *func = host->card->sdio_func[i]; 68295cdfb72SNicolas Pitre if (func && sdio_func_present(func) && func->dev.driver) { 68395cdfb72SNicolas Pitre const struct dev_pm_ops *pmops = func->dev.driver->pm; 68495cdfb72SNicolas Pitre err = pmops->resume(&func->dev); 68595cdfb72SNicolas Pitre } 68695cdfb72SNicolas Pitre } 68795cdfb72SNicolas Pitre 68895cdfb72SNicolas Pitre return err; 68917d33e14SNicolas Pitre } 6905c4e6f13SPierre Ossman 691d3fe37b1SOhad Ben-Cohen static int mmc_sdio_power_restore(struct mmc_host *host) 692d3fe37b1SOhad Ben-Cohen { 693d3fe37b1SOhad Ben-Cohen int ret; 694*c6e633adSDaniel Drake u32 ocr; 695d3fe37b1SOhad Ben-Cohen 696d3fe37b1SOhad Ben-Cohen BUG_ON(!host); 697d3fe37b1SOhad Ben-Cohen BUG_ON(!host->card); 698d3fe37b1SOhad Ben-Cohen 699d3fe37b1SOhad Ben-Cohen mmc_claim_host(host); 700*c6e633adSDaniel Drake 701*c6e633adSDaniel Drake /* 702*c6e633adSDaniel Drake * Reset the card by performing the same steps that are taken by 703*c6e633adSDaniel Drake * mmc_rescan_try_freq() and mmc_attach_sdio() during a "normal" probe. 704*c6e633adSDaniel Drake * 705*c6e633adSDaniel Drake * sdio_reset() is technically not needed. Having just powered up the 706*c6e633adSDaniel Drake * hardware, it should already be in reset state. However, some 707*c6e633adSDaniel Drake * platforms (such as SD8686 on OLPC) do not instantly cut power, 708*c6e633adSDaniel Drake * meaning that a reset is required when restoring power soon after 709*c6e633adSDaniel Drake * powering off. It is harmless in other cases. 710*c6e633adSDaniel Drake * 711*c6e633adSDaniel Drake * The CMD5 reset (mmc_send_io_op_cond()), according to the SDIO spec, 712*c6e633adSDaniel Drake * is not necessary for non-removable cards. However, it is required 713*c6e633adSDaniel Drake * for OLPC SD8686 (which expects a [CMD5,5,3,7] init sequence), and 714*c6e633adSDaniel Drake * harmless in other situations. 715*c6e633adSDaniel Drake * 716*c6e633adSDaniel Drake * With these steps taken, mmc_select_voltage() is also required to 717*c6e633adSDaniel Drake * restore the correct voltage setting of the card. 718*c6e633adSDaniel Drake */ 719*c6e633adSDaniel Drake sdio_reset(host); 720*c6e633adSDaniel Drake mmc_go_idle(host); 721*c6e633adSDaniel Drake mmc_send_if_cond(host, host->ocr_avail); 722*c6e633adSDaniel Drake 723*c6e633adSDaniel Drake ret = mmc_send_io_op_cond(host, 0, &ocr); 724*c6e633adSDaniel Drake if (ret) 725*c6e633adSDaniel Drake goto out; 726*c6e633adSDaniel Drake 727*c6e633adSDaniel Drake if (host->ocr_avail_sdio) 728*c6e633adSDaniel Drake host->ocr_avail = host->ocr_avail_sdio; 729*c6e633adSDaniel Drake 730*c6e633adSDaniel Drake host->ocr = mmc_select_voltage(host, ocr & ~0x7F); 731*c6e633adSDaniel Drake if (!host->ocr) { 732*c6e633adSDaniel Drake ret = -EINVAL; 733*c6e633adSDaniel Drake goto out; 734*c6e633adSDaniel Drake } 735*c6e633adSDaniel Drake 736d3fe37b1SOhad Ben-Cohen ret = mmc_sdio_init_card(host, host->ocr, host->card, 737a5e9425dSOhad Ben-Cohen mmc_card_keep_power(host)); 738d3fe37b1SOhad Ben-Cohen if (!ret && host->sdio_irqs) 739d3fe37b1SOhad Ben-Cohen mmc_signal_sdio_irq(host); 740*c6e633adSDaniel Drake 741*c6e633adSDaniel Drake out: 742d3fe37b1SOhad Ben-Cohen mmc_release_host(host); 743d3fe37b1SOhad Ben-Cohen 744d3fe37b1SOhad Ben-Cohen return ret; 745d3fe37b1SOhad Ben-Cohen } 746d3fe37b1SOhad Ben-Cohen 7475c4e6f13SPierre Ossman static const struct mmc_bus_ops mmc_sdio_ops = { 7485c4e6f13SPierre Ossman .remove = mmc_sdio_remove, 7495c4e6f13SPierre Ossman .detect = mmc_sdio_detect, 75017d33e14SNicolas Pitre .suspend = mmc_sdio_suspend, 75117d33e14SNicolas Pitre .resume = mmc_sdio_resume, 752d3fe37b1SOhad Ben-Cohen .power_restore = mmc_sdio_power_restore, 7535c4e6f13SPierre Ossman }; 7545c4e6f13SPierre Ossman 7555c4e6f13SPierre Ossman 7565c4e6f13SPierre Ossman /* 7575c4e6f13SPierre Ossman * Starting point for SDIO card init. 7585c4e6f13SPierre Ossman */ 759807e8e40SAndy Ross int mmc_attach_sdio(struct mmc_host *host) 7605c4e6f13SPierre Ossman { 761807e8e40SAndy Ross int err, i, funcs; 762807e8e40SAndy Ross u32 ocr; 7635c4e6f13SPierre Ossman struct mmc_card *card; 7645c4e6f13SPierre Ossman 7655c4e6f13SPierre Ossman BUG_ON(!host); 766d84075c8SPierre Ossman WARN_ON(!host->claimed); 7675c4e6f13SPierre Ossman 768807e8e40SAndy Ross err = mmc_send_io_op_cond(host, 0, &ocr); 769807e8e40SAndy Ross if (err) 770807e8e40SAndy Ross return err; 771807e8e40SAndy Ross 7725c4e6f13SPierre Ossman mmc_attach_bus(host, &mmc_sdio_ops); 7738f230f45STakashi Iwai if (host->ocr_avail_sdio) 7748f230f45STakashi Iwai host->ocr_avail = host->ocr_avail_sdio; 7755c4e6f13SPierre Ossman 7765c4e6f13SPierre Ossman /* 7775c4e6f13SPierre Ossman * Sanity check the voltages that the card claims to 7785c4e6f13SPierre Ossman * support. 7795c4e6f13SPierre Ossman */ 7805c4e6f13SPierre Ossman if (ocr & 0x7F) { 7815c4e6f13SPierre Ossman printk(KERN_WARNING "%s: card claims to support voltages " 7825c4e6f13SPierre Ossman "below the defined range. These will be ignored.\n", 7835c4e6f13SPierre Ossman mmc_hostname(host)); 7845c4e6f13SPierre Ossman ocr &= ~0x7F; 7855c4e6f13SPierre Ossman } 7865c4e6f13SPierre Ossman 7875c4e6f13SPierre Ossman host->ocr = mmc_select_voltage(host, ocr); 7885c4e6f13SPierre Ossman 7895c4e6f13SPierre Ossman /* 7905c4e6f13SPierre Ossman * Can we support the voltage(s) of the card(s)? 7915c4e6f13SPierre Ossman */ 7925c4e6f13SPierre Ossman if (!host->ocr) { 7935c4e6f13SPierre Ossman err = -EINVAL; 7945c4e6f13SPierre Ossman goto err; 7955c4e6f13SPierre Ossman } 7965c4e6f13SPierre Ossman 7975c4e6f13SPierre Ossman /* 79817d33e14SNicolas Pitre * Detect and init the card. 7995c4e6f13SPierre Ossman */ 8003bca4cf7SChris Ball err = mmc_sdio_init_card(host, host->ocr, NULL, 0); 8015c4e6f13SPierre Ossman if (err) 8025c4e6f13SPierre Ossman goto err; 80317d33e14SNicolas Pitre card = host->card; 804af517150SDavid Brownell 805af517150SDavid Brownell /* 806ed919b01SOhad Ben-Cohen * Enable runtime PM only if supported by host+card+board 807ed919b01SOhad Ben-Cohen */ 808ed919b01SOhad Ben-Cohen if (host->caps & MMC_CAP_POWER_OFF_CARD) { 809ed919b01SOhad Ben-Cohen /* 81081968561SOhad Ben-Cohen * Let runtime PM core know our card is active 81181968561SOhad Ben-Cohen */ 81281968561SOhad Ben-Cohen err = pm_runtime_set_active(&card->dev); 81381968561SOhad Ben-Cohen if (err) 81481968561SOhad Ben-Cohen goto remove; 81581968561SOhad Ben-Cohen 81681968561SOhad Ben-Cohen /* 81781968561SOhad Ben-Cohen * Enable runtime PM for this card 81881968561SOhad Ben-Cohen */ 81981968561SOhad Ben-Cohen pm_runtime_enable(&card->dev); 820ed919b01SOhad Ben-Cohen } 82181968561SOhad Ben-Cohen 82281968561SOhad Ben-Cohen /* 8235c4e6f13SPierre Ossman * The number of functions on the card is encoded inside 8245c4e6f13SPierre Ossman * the ocr. 8255c4e6f13SPierre Ossman */ 826e8812793SMatt Fleming funcs = (ocr & 0x70000000) >> 28; 827e8812793SMatt Fleming card->sdio_funcs = 0; 8284ff6471cSPierre Ossman 8294ff6471cSPierre Ossman /* 830e29a7d73SPierre Ossman * Initialize (but don't add) all present functions. 831e29a7d73SPierre Ossman */ 832e8812793SMatt Fleming for (i = 0; i < funcs; i++, card->sdio_funcs++) { 833e29a7d73SPierre Ossman err = sdio_init_func(host->card, i + 1); 834e29a7d73SPierre Ossman if (err) 835e29a7d73SPierre Ossman goto remove; 83640bba0c1SOhad Ben-Cohen 83740bba0c1SOhad Ben-Cohen /* 838ed919b01SOhad Ben-Cohen * Enable Runtime PM for this func (if supported) 83940bba0c1SOhad Ben-Cohen */ 840ed919b01SOhad Ben-Cohen if (host->caps & MMC_CAP_POWER_OFF_CARD) 84140bba0c1SOhad Ben-Cohen pm_runtime_enable(&card->sdio_func[i]->dev); 842e29a7d73SPierre Ossman } 8435c4e6f13SPierre Ossman 844e29a7d73SPierre Ossman /* 845e29a7d73SPierre Ossman * First add the card to the driver model... 846e29a7d73SPierre Ossman */ 847807e8e40SAndy Ross mmc_release_host(host); 8485c4e6f13SPierre Ossman err = mmc_add_card(host->card); 8495c4e6f13SPierre Ossman if (err) 850e29a7d73SPierre Ossman goto remove_added; 851e29a7d73SPierre Ossman 852e29a7d73SPierre Ossman /* 853e29a7d73SPierre Ossman * ...then the SDIO functions. 854e29a7d73SPierre Ossman */ 855e29a7d73SPierre Ossman for (i = 0;i < funcs;i++) { 856e29a7d73SPierre Ossman err = sdio_add_func(host->card->sdio_func[i]); 857e29a7d73SPierre Ossman if (err) 858e29a7d73SPierre Ossman goto remove_added; 859e29a7d73SPierre Ossman } 8605c4e6f13SPierre Ossman 86134497913SDmitry Shmidt mmc_claim_host(host); 8625c4e6f13SPierre Ossman return 0; 8635c4e6f13SPierre Ossman 864e29a7d73SPierre Ossman 865e29a7d73SPierre Ossman remove_added: 866e29a7d73SPierre Ossman /* Remove without lock if the device has been added. */ 867e29a7d73SPierre Ossman mmc_sdio_remove(host); 8685c4e6f13SPierre Ossman mmc_claim_host(host); 869e29a7d73SPierre Ossman remove: 870e29a7d73SPierre Ossman /* And with lock if it hasn't been added. */ 871807e8e40SAndy Ross mmc_release_host(host); 872e29a7d73SPierre Ossman if (host->card) 873e29a7d73SPierre Ossman mmc_sdio_remove(host); 874807e8e40SAndy Ross mmc_claim_host(host); 8755c4e6f13SPierre Ossman err: 8765c4e6f13SPierre Ossman mmc_detach_bus(host); 8775c4e6f13SPierre Ossman 8785c4e6f13SPierre Ossman printk(KERN_ERR "%s: error %d whilst initialising SDIO card\n", 8795c4e6f13SPierre Ossman mmc_hostname(host), err); 8805c4e6f13SPierre Ossman 8815c4e6f13SPierre Ossman return err; 8825c4e6f13SPierre Ossman } 8835c4e6f13SPierre Ossman 884