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> 17a4924c71SGirish K S #include <linux/mmc/mmc.h> 1835c66c19SPierre Ossman #include <linux/mmc/sdio.h> 19e29a7d73SPierre Ossman #include <linux/mmc/sdio_func.h> 20eab40687SOhad Ben-Cohen #include <linux/mmc/sdio_ids.h> 215c4e6f13SPierre Ossman 225c4e6f13SPierre Ossman #include "core.h" 235c4e6f13SPierre Ossman #include "bus.h" 2471578a1eSMichal Miroslaw #include "sd.h" 25e29a7d73SPierre Ossman #include "sdio_bus.h" 265c4e6f13SPierre Ossman #include "mmc_ops.h" 275c4e6f13SPierre Ossman #include "sd_ops.h" 285c4e6f13SPierre Ossman #include "sdio_ops.h" 29b7261261SNicolas Pitre #include "sdio_cis.h" 305c4e6f13SPierre Ossman 310597007fSPierre Ossman static int sdio_read_fbr(struct sdio_func *func) 320597007fSPierre Ossman { 330597007fSPierre Ossman int ret; 340597007fSPierre Ossman unsigned char data; 350597007fSPierre Ossman 36eab40687SOhad Ben-Cohen if (mmc_card_nonstd_func_interface(func->card)) { 37eab40687SOhad Ben-Cohen func->class = SDIO_CLASS_NONE; 38eab40687SOhad Ben-Cohen return 0; 39eab40687SOhad Ben-Cohen } 40eab40687SOhad Ben-Cohen 410597007fSPierre Ossman ret = mmc_io_rw_direct(func->card, 0, 0, 427616ee95SDavid Vrabel SDIO_FBR_BASE(func->num) + SDIO_FBR_STD_IF, 0, &data); 430597007fSPierre Ossman if (ret) 440597007fSPierre Ossman goto out; 450597007fSPierre Ossman 460597007fSPierre Ossman data &= 0x0f; 470597007fSPierre Ossman 480597007fSPierre Ossman if (data == 0x0f) { 490597007fSPierre Ossman ret = mmc_io_rw_direct(func->card, 0, 0, 507616ee95SDavid Vrabel SDIO_FBR_BASE(func->num) + SDIO_FBR_STD_IF_EXT, 0, &data); 510597007fSPierre Ossman if (ret) 520597007fSPierre Ossman goto out; 530597007fSPierre Ossman } 540597007fSPierre Ossman 550597007fSPierre Ossman func->class = data; 560597007fSPierre Ossman 570597007fSPierre Ossman out: 580597007fSPierre Ossman return ret; 590597007fSPierre Ossman } 600597007fSPierre Ossman 61e29a7d73SPierre Ossman static int sdio_init_func(struct mmc_card *card, unsigned int fn) 62e29a7d73SPierre Ossman { 630597007fSPierre Ossman int ret; 64e29a7d73SPierre Ossman struct sdio_func *func; 65e29a7d73SPierre Ossman 66e29a7d73SPierre Ossman BUG_ON(fn > SDIO_MAX_FUNCS); 67e29a7d73SPierre Ossman 68e29a7d73SPierre Ossman func = sdio_alloc_func(card); 69e29a7d73SPierre Ossman if (IS_ERR(func)) 70e29a7d73SPierre Ossman return PTR_ERR(func); 71e29a7d73SPierre Ossman 72e29a7d73SPierre Ossman func->num = fn; 73e29a7d73SPierre Ossman 746f51be3dSGrazvydas Ignotas if (!(card->quirks & MMC_QUIRK_NONSTD_SDIO)) { 750597007fSPierre Ossman ret = sdio_read_fbr(func); 760597007fSPierre Ossman if (ret) 770597007fSPierre Ossman goto fail; 780597007fSPierre Ossman 791a632f8cSPierre Ossman ret = sdio_read_func_cis(func); 80b7261261SNicolas Pitre if (ret) 81b7261261SNicolas Pitre goto fail; 826f51be3dSGrazvydas Ignotas } else { 836f51be3dSGrazvydas Ignotas func->vendor = func->card->cis.vendor; 846f51be3dSGrazvydas Ignotas func->device = func->card->cis.device; 856f51be3dSGrazvydas Ignotas func->max_blksize = func->card->cis.blksize; 866f51be3dSGrazvydas Ignotas } 87b7261261SNicolas Pitre 88e29a7d73SPierre Ossman card->sdio_func[fn - 1] = func; 89e29a7d73SPierre Ossman 90e29a7d73SPierre Ossman return 0; 910597007fSPierre Ossman 920597007fSPierre Ossman fail: 930597007fSPierre Ossman /* 940597007fSPierre Ossman * It is okay to remove the function here even though we hold 950597007fSPierre Ossman * the host lock as we haven't registered the device yet. 960597007fSPierre Ossman */ 970597007fSPierre Ossman sdio_remove_func(func); 980597007fSPierre Ossman return ret; 99e29a7d73SPierre Ossman } 100e29a7d73SPierre Ossman 1012d0d68f5SPhilip Rakity static int sdio_read_cccr(struct mmc_card *card, u32 ocr) 10235c66c19SPierre Ossman { 10335c66c19SPierre Ossman int ret; 10435c66c19SPierre Ossman int cccr_vsn; 1052d0d68f5SPhilip Rakity int uhs = ocr & R4_18V_PRESENT; 10635c66c19SPierre Ossman unsigned char data; 107a303c531SPhilip Rakity unsigned char speed; 10835c66c19SPierre Ossman 10935c66c19SPierre Ossman memset(&card->cccr, 0, sizeof(struct sdio_cccr)); 11035c66c19SPierre Ossman 11135c66c19SPierre Ossman ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_CCCR, 0, &data); 11235c66c19SPierre Ossman if (ret) 11335c66c19SPierre Ossman goto out; 11435c66c19SPierre Ossman 11535c66c19SPierre Ossman cccr_vsn = data & 0x0f; 11635c66c19SPierre Ossman 117b4625dabSBing Zhao if (cccr_vsn > SDIO_CCCR_REV_3_00) { 118a3c76eb9SGirish K S pr_err("%s: unrecognised CCCR structure version %d\n", 11935c66c19SPierre Ossman mmc_hostname(card->host), cccr_vsn); 12035c66c19SPierre Ossman return -EINVAL; 12135c66c19SPierre Ossman } 12235c66c19SPierre Ossman 12335c66c19SPierre Ossman card->cccr.sdio_vsn = (data & 0xf0) >> 4; 12435c66c19SPierre Ossman 12535c66c19SPierre Ossman ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_CAPS, 0, &data); 12635c66c19SPierre Ossman if (ret) 12735c66c19SPierre Ossman goto out; 12835c66c19SPierre Ossman 12935c66c19SPierre Ossman if (data & SDIO_CCCR_CAP_SMB) 13035c66c19SPierre Ossman card->cccr.multi_block = 1; 13135c66c19SPierre Ossman if (data & SDIO_CCCR_CAP_LSC) 13235c66c19SPierre Ossman card->cccr.low_speed = 1; 13335c66c19SPierre Ossman if (data & SDIO_CCCR_CAP_4BLS) 13435c66c19SPierre Ossman card->cccr.wide_bus = 1; 13535c66c19SPierre Ossman 13635c66c19SPierre Ossman if (cccr_vsn >= SDIO_CCCR_REV_1_10) { 13735c66c19SPierre Ossman ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_POWER, 0, &data); 13835c66c19SPierre Ossman if (ret) 13935c66c19SPierre Ossman goto out; 14035c66c19SPierre Ossman 14135c66c19SPierre Ossman if (data & SDIO_POWER_SMPC) 14235c66c19SPierre Ossman card->cccr.high_power = 1; 14335c66c19SPierre Ossman } 14435c66c19SPierre Ossman 14535c66c19SPierre Ossman if (cccr_vsn >= SDIO_CCCR_REV_1_20) { 146a303c531SPhilip Rakity ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_SPEED, 0, &speed); 14735c66c19SPierre Ossman if (ret) 14835c66c19SPierre Ossman goto out; 14935c66c19SPierre Ossman 150a303c531SPhilip Rakity card->scr.sda_spec3 = 0; 151a303c531SPhilip Rakity card->sw_caps.sd3_bus_mode = 0; 152a303c531SPhilip Rakity card->sw_caps.sd3_drv_type = 0; 1532d0d68f5SPhilip Rakity if (cccr_vsn >= SDIO_CCCR_REV_3_00 && uhs) { 154a303c531SPhilip Rakity card->scr.sda_spec3 = 1; 155a303c531SPhilip Rakity ret = mmc_io_rw_direct(card, 0, 0, 156a303c531SPhilip Rakity SDIO_CCCR_UHS, 0, &data); 157a303c531SPhilip Rakity if (ret) 158a303c531SPhilip Rakity goto out; 159a303c531SPhilip Rakity 16041875e38SSujit Reddy Thumma if (mmc_host_uhs(card->host)) { 161a303c531SPhilip Rakity if (data & SDIO_UHS_DDR50) 162a303c531SPhilip Rakity card->sw_caps.sd3_bus_mode 163a303c531SPhilip Rakity |= SD_MODE_UHS_DDR50; 164a303c531SPhilip Rakity 165a303c531SPhilip Rakity if (data & SDIO_UHS_SDR50) 166a303c531SPhilip Rakity card->sw_caps.sd3_bus_mode 167a303c531SPhilip Rakity |= SD_MODE_UHS_SDR50; 168a303c531SPhilip Rakity 169a303c531SPhilip Rakity if (data & SDIO_UHS_SDR104) 170a303c531SPhilip Rakity card->sw_caps.sd3_bus_mode 171a303c531SPhilip Rakity |= SD_MODE_UHS_SDR104; 172a303c531SPhilip Rakity } 173a303c531SPhilip Rakity 174a303c531SPhilip Rakity ret = mmc_io_rw_direct(card, 0, 0, 175a303c531SPhilip Rakity SDIO_CCCR_DRIVE_STRENGTH, 0, &data); 176a303c531SPhilip Rakity if (ret) 177a303c531SPhilip Rakity goto out; 178a303c531SPhilip Rakity 179a303c531SPhilip Rakity if (data & SDIO_DRIVE_SDTA) 180a303c531SPhilip Rakity card->sw_caps.sd3_drv_type |= SD_DRIVER_TYPE_A; 181a303c531SPhilip Rakity if (data & SDIO_DRIVE_SDTC) 182a303c531SPhilip Rakity card->sw_caps.sd3_drv_type |= SD_DRIVER_TYPE_C; 183a303c531SPhilip Rakity if (data & SDIO_DRIVE_SDTD) 184a303c531SPhilip Rakity card->sw_caps.sd3_drv_type |= SD_DRIVER_TYPE_D; 185a303c531SPhilip Rakity } 186a303c531SPhilip Rakity 187a303c531SPhilip Rakity /* if no uhs mode ensure we check for high speed */ 188a303c531SPhilip Rakity if (!card->sw_caps.sd3_bus_mode) { 189a303c531SPhilip Rakity if (speed & SDIO_SPEED_SHS) { 19035c66c19SPierre Ossman card->cccr.high_speed = 1; 191a303c531SPhilip Rakity card->sw_caps.hs_max_dtr = 50000000; 192a303c531SPhilip Rakity } else { 193a303c531SPhilip Rakity card->cccr.high_speed = 0; 194a303c531SPhilip Rakity card->sw_caps.hs_max_dtr = 25000000; 195a303c531SPhilip Rakity } 196a303c531SPhilip Rakity } 19735c66c19SPierre Ossman } 19835c66c19SPierre Ossman 19935c66c19SPierre Ossman out: 20035c66c19SPierre Ossman return ret; 20135c66c19SPierre Ossman } 20235c66c19SPierre Ossman 2034ff6471cSPierre Ossman static int sdio_enable_wide(struct mmc_card *card) 2044ff6471cSPierre Ossman { 2054ff6471cSPierre Ossman int ret; 2064ff6471cSPierre Ossman u8 ctrl; 2074ff6471cSPierre Ossman 2084ff6471cSPierre Ossman if (!(card->host->caps & MMC_CAP_4_BIT_DATA)) 2094ff6471cSPierre Ossman return 0; 2104ff6471cSPierre Ossman 2114ff6471cSPierre Ossman if (card->cccr.low_speed && !card->cccr.wide_bus) 2124ff6471cSPierre Ossman return 0; 2134ff6471cSPierre Ossman 2144ff6471cSPierre Ossman ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl); 2154ff6471cSPierre Ossman if (ret) 2164ff6471cSPierre Ossman return ret; 2174ff6471cSPierre Ossman 2182a0fe914SYong Ding if ((ctrl & SDIO_BUS_WIDTH_MASK) == SDIO_BUS_WIDTH_RESERVED) 2192a0fe914SYong Ding pr_warning("%s: SDIO_CCCR_IF is invalid: 0x%02x\n", 2202a0fe914SYong Ding mmc_hostname(card->host), ctrl); 2212a0fe914SYong Ding 2222a0fe914SYong Ding /* set as 4-bit bus width */ 2232a0fe914SYong Ding ctrl &= ~SDIO_BUS_WIDTH_MASK; 2244ff6471cSPierre Ossman ctrl |= SDIO_BUS_WIDTH_4BIT; 2254ff6471cSPierre Ossman 2264ff6471cSPierre Ossman ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL); 2274ff6471cSPierre Ossman if (ret) 2284ff6471cSPierre Ossman return ret; 2294ff6471cSPierre Ossman 2307310ece8SMichal Miroslaw return 1; 2314ff6471cSPierre Ossman } 2324ff6471cSPierre Ossman 2335c4e6f13SPierre Ossman /* 234006ebd5dSOhad Ben-Cohen * If desired, disconnect the pull-up resistor on CD/DAT[3] (pin 1) 235006ebd5dSOhad Ben-Cohen * of the card. This may be required on certain setups of boards, 236006ebd5dSOhad Ben-Cohen * controllers and embedded sdio device which do not need the card's 237006ebd5dSOhad Ben-Cohen * pull-up. As a result, card detection is disabled and power is saved. 238006ebd5dSOhad Ben-Cohen */ 239006ebd5dSOhad Ben-Cohen static int sdio_disable_cd(struct mmc_card *card) 240006ebd5dSOhad Ben-Cohen { 241006ebd5dSOhad Ben-Cohen int ret; 242006ebd5dSOhad Ben-Cohen u8 ctrl; 243006ebd5dSOhad Ben-Cohen 2442059a02dSOhad Ben-Cohen if (!mmc_card_disable_cd(card)) 245006ebd5dSOhad Ben-Cohen return 0; 246006ebd5dSOhad Ben-Cohen 247006ebd5dSOhad Ben-Cohen ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl); 248006ebd5dSOhad Ben-Cohen if (ret) 249006ebd5dSOhad Ben-Cohen return ret; 250006ebd5dSOhad Ben-Cohen 251006ebd5dSOhad Ben-Cohen ctrl |= SDIO_BUS_CD_DISABLE; 252006ebd5dSOhad Ben-Cohen 253006ebd5dSOhad Ben-Cohen return mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL); 254006ebd5dSOhad Ben-Cohen } 255006ebd5dSOhad Ben-Cohen 256006ebd5dSOhad Ben-Cohen /* 2576b5eda36SDaniel Drake * Devices that remain active during a system suspend are 2586b5eda36SDaniel Drake * put back into 1-bit mode. 2596b5eda36SDaniel Drake */ 2606b5eda36SDaniel Drake static int sdio_disable_wide(struct mmc_card *card) 2616b5eda36SDaniel Drake { 2626b5eda36SDaniel Drake int ret; 2636b5eda36SDaniel Drake u8 ctrl; 2646b5eda36SDaniel Drake 2656b5eda36SDaniel Drake if (!(card->host->caps & MMC_CAP_4_BIT_DATA)) 2666b5eda36SDaniel Drake return 0; 2676b5eda36SDaniel Drake 2686b5eda36SDaniel Drake if (card->cccr.low_speed && !card->cccr.wide_bus) 2696b5eda36SDaniel Drake return 0; 2706b5eda36SDaniel Drake 2716b5eda36SDaniel Drake ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl); 2726b5eda36SDaniel Drake if (ret) 2736b5eda36SDaniel Drake return ret; 2746b5eda36SDaniel Drake 2756b5eda36SDaniel Drake if (!(ctrl & SDIO_BUS_WIDTH_4BIT)) 2766b5eda36SDaniel Drake return 0; 2776b5eda36SDaniel Drake 2786b5eda36SDaniel Drake ctrl &= ~SDIO_BUS_WIDTH_4BIT; 2796b5eda36SDaniel Drake ctrl |= SDIO_BUS_ASYNC_INT; 2806b5eda36SDaniel Drake 2816b5eda36SDaniel Drake ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL); 2826b5eda36SDaniel Drake if (ret) 2836b5eda36SDaniel Drake return ret; 2846b5eda36SDaniel Drake 2856b5eda36SDaniel Drake mmc_set_bus_width(card->host, MMC_BUS_WIDTH_1); 2866b5eda36SDaniel Drake 2876b5eda36SDaniel Drake return 0; 2886b5eda36SDaniel Drake } 2896b5eda36SDaniel Drake 2907310ece8SMichal Miroslaw 2917310ece8SMichal Miroslaw static int sdio_enable_4bit_bus(struct mmc_card *card) 2927310ece8SMichal Miroslaw { 2937310ece8SMichal Miroslaw int err; 2947310ece8SMichal Miroslaw 2957310ece8SMichal Miroslaw if (card->type == MMC_TYPE_SDIO) 2967310ece8SMichal Miroslaw return sdio_enable_wide(card); 2977310ece8SMichal Miroslaw 2987310ece8SMichal Miroslaw if ((card->host->caps & MMC_CAP_4_BIT_DATA) && 2997310ece8SMichal Miroslaw (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) { 3007310ece8SMichal Miroslaw err = mmc_app_set_bus_width(card, MMC_BUS_WIDTH_4); 3017310ece8SMichal Miroslaw if (err) 3027310ece8SMichal Miroslaw return err; 3037310ece8SMichal Miroslaw } else 3047310ece8SMichal Miroslaw return 0; 3057310ece8SMichal Miroslaw 3067310ece8SMichal Miroslaw err = sdio_enable_wide(card); 3077310ece8SMichal Miroslaw if (err <= 0) 3087310ece8SMichal Miroslaw mmc_app_set_bus_width(card, MMC_BUS_WIDTH_1); 3097310ece8SMichal Miroslaw 3107310ece8SMichal Miroslaw return err; 3117310ece8SMichal Miroslaw } 3127310ece8SMichal Miroslaw 3137310ece8SMichal Miroslaw 3146b5eda36SDaniel Drake /* 315d16f5770SPierre Ossman * Test if the card supports high-speed mode and, if so, switch to it. 316d16f5770SPierre Ossman */ 3177310ece8SMichal Miroslaw static int mmc_sdio_switch_hs(struct mmc_card *card, int enable) 318d16f5770SPierre Ossman { 319d16f5770SPierre Ossman int ret; 320d16f5770SPierre Ossman u8 speed; 321d16f5770SPierre Ossman 322d16f5770SPierre Ossman if (!(card->host->caps & MMC_CAP_SD_HIGHSPEED)) 323d16f5770SPierre Ossman return 0; 324d16f5770SPierre Ossman 325d16f5770SPierre Ossman if (!card->cccr.high_speed) 326d16f5770SPierre Ossman return 0; 327d16f5770SPierre Ossman 328d16f5770SPierre Ossman ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_SPEED, 0, &speed); 329d16f5770SPierre Ossman if (ret) 330d16f5770SPierre Ossman return ret; 331d16f5770SPierre Ossman 3327310ece8SMichal Miroslaw if (enable) 333d16f5770SPierre Ossman speed |= SDIO_SPEED_EHS; 3347310ece8SMichal Miroslaw else 3357310ece8SMichal Miroslaw speed &= ~SDIO_SPEED_EHS; 336d16f5770SPierre Ossman 337d16f5770SPierre Ossman ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_SPEED, speed, NULL); 338d16f5770SPierre Ossman if (ret) 339d16f5770SPierre Ossman return ret; 340d16f5770SPierre Ossman 34171578a1eSMichal Miroslaw return 1; 34271578a1eSMichal Miroslaw } 343d16f5770SPierre Ossman 3447310ece8SMichal Miroslaw /* 3457310ece8SMichal Miroslaw * Enable SDIO/combo card's high-speed mode. Return 0/1 if [not]supported. 3467310ece8SMichal Miroslaw */ 3477310ece8SMichal Miroslaw static int sdio_enable_hs(struct mmc_card *card) 3487310ece8SMichal Miroslaw { 3497310ece8SMichal Miroslaw int ret; 3507310ece8SMichal Miroslaw 3517310ece8SMichal Miroslaw ret = mmc_sdio_switch_hs(card, true); 3527310ece8SMichal Miroslaw if (ret <= 0 || card->type == MMC_TYPE_SDIO) 3537310ece8SMichal Miroslaw return ret; 3547310ece8SMichal Miroslaw 3557310ece8SMichal Miroslaw ret = mmc_sd_switch_hs(card); 3567310ece8SMichal Miroslaw if (ret <= 0) 3577310ece8SMichal Miroslaw mmc_sdio_switch_hs(card, false); 3587310ece8SMichal Miroslaw 3597310ece8SMichal Miroslaw return ret; 3607310ece8SMichal Miroslaw } 3617310ece8SMichal Miroslaw 36271578a1eSMichal Miroslaw static unsigned mmc_sdio_get_max_clock(struct mmc_card *card) 36371578a1eSMichal Miroslaw { 36471578a1eSMichal Miroslaw unsigned max_dtr; 36571578a1eSMichal Miroslaw 36671578a1eSMichal Miroslaw if (mmc_card_highspeed(card)) { 36771578a1eSMichal Miroslaw /* 36871578a1eSMichal Miroslaw * The SDIO specification doesn't mention how 36971578a1eSMichal Miroslaw * the CIS transfer speed register relates to 37071578a1eSMichal Miroslaw * high-speed, but it seems that 50 MHz is 37171578a1eSMichal Miroslaw * mandatory. 37271578a1eSMichal Miroslaw */ 37371578a1eSMichal Miroslaw max_dtr = 50000000; 37471578a1eSMichal Miroslaw } else { 37571578a1eSMichal Miroslaw max_dtr = card->cis.max_dtr; 37671578a1eSMichal Miroslaw } 37771578a1eSMichal Miroslaw 3787310ece8SMichal Miroslaw if (card->type == MMC_TYPE_SD_COMBO) 3797310ece8SMichal Miroslaw max_dtr = min(max_dtr, mmc_sd_get_max_clock(card)); 3807310ece8SMichal Miroslaw 38171578a1eSMichal Miroslaw return max_dtr; 382d16f5770SPierre Ossman } 383d16f5770SPierre Ossman 384a303c531SPhilip Rakity static unsigned char host_drive_to_sdio_drive(int host_strength) 385a303c531SPhilip Rakity { 386a303c531SPhilip Rakity switch (host_strength) { 387a303c531SPhilip Rakity case MMC_SET_DRIVER_TYPE_A: 388a303c531SPhilip Rakity return SDIO_DTSx_SET_TYPE_A; 389a303c531SPhilip Rakity case MMC_SET_DRIVER_TYPE_B: 390a303c531SPhilip Rakity return SDIO_DTSx_SET_TYPE_B; 391a303c531SPhilip Rakity case MMC_SET_DRIVER_TYPE_C: 392a303c531SPhilip Rakity return SDIO_DTSx_SET_TYPE_C; 393a303c531SPhilip Rakity case MMC_SET_DRIVER_TYPE_D: 394a303c531SPhilip Rakity return SDIO_DTSx_SET_TYPE_D; 395a303c531SPhilip Rakity default: 396a303c531SPhilip Rakity return SDIO_DTSx_SET_TYPE_B; 397a303c531SPhilip Rakity } 398a303c531SPhilip Rakity } 399a303c531SPhilip Rakity 400a303c531SPhilip Rakity static void sdio_select_driver_type(struct mmc_card *card) 401a303c531SPhilip Rakity { 402a303c531SPhilip Rakity int host_drv_type = SD_DRIVER_TYPE_B; 403a303c531SPhilip Rakity int card_drv_type = SD_DRIVER_TYPE_B; 404a303c531SPhilip Rakity int drive_strength; 405a303c531SPhilip Rakity unsigned char card_strength; 406a303c531SPhilip Rakity int err; 407a303c531SPhilip Rakity 408a303c531SPhilip Rakity /* 409a303c531SPhilip Rakity * If the host doesn't support any of the Driver Types A,C or D, 410a303c531SPhilip Rakity * or there is no board specific handler then default Driver 411a303c531SPhilip Rakity * Type B is used. 412a303c531SPhilip Rakity */ 413a303c531SPhilip Rakity if (!(card->host->caps & 414a303c531SPhilip Rakity (MMC_CAP_DRIVER_TYPE_A | 415a303c531SPhilip Rakity MMC_CAP_DRIVER_TYPE_C | 416a303c531SPhilip Rakity MMC_CAP_DRIVER_TYPE_D))) 417a303c531SPhilip Rakity return; 418a303c531SPhilip Rakity 419a303c531SPhilip Rakity if (!card->host->ops->select_drive_strength) 420a303c531SPhilip Rakity return; 421a303c531SPhilip Rakity 422a303c531SPhilip Rakity if (card->host->caps & MMC_CAP_DRIVER_TYPE_A) 423a303c531SPhilip Rakity host_drv_type |= SD_DRIVER_TYPE_A; 424a303c531SPhilip Rakity 425a303c531SPhilip Rakity if (card->host->caps & MMC_CAP_DRIVER_TYPE_C) 426a303c531SPhilip Rakity host_drv_type |= SD_DRIVER_TYPE_C; 427a303c531SPhilip Rakity 428a303c531SPhilip Rakity if (card->host->caps & MMC_CAP_DRIVER_TYPE_D) 429a303c531SPhilip Rakity host_drv_type |= SD_DRIVER_TYPE_D; 430a303c531SPhilip Rakity 431a303c531SPhilip Rakity if (card->sw_caps.sd3_drv_type & SD_DRIVER_TYPE_A) 432a303c531SPhilip Rakity card_drv_type |= SD_DRIVER_TYPE_A; 433a303c531SPhilip Rakity 434a303c531SPhilip Rakity if (card->sw_caps.sd3_drv_type & SD_DRIVER_TYPE_C) 435a303c531SPhilip Rakity card_drv_type |= SD_DRIVER_TYPE_C; 436a303c531SPhilip Rakity 437a303c531SPhilip Rakity if (card->sw_caps.sd3_drv_type & SD_DRIVER_TYPE_D) 438a303c531SPhilip Rakity card_drv_type |= SD_DRIVER_TYPE_D; 439a303c531SPhilip Rakity 440a303c531SPhilip Rakity /* 441a303c531SPhilip Rakity * The drive strength that the hardware can support 442a303c531SPhilip Rakity * depends on the board design. Pass the appropriate 443a303c531SPhilip Rakity * information and let the hardware specific code 444a303c531SPhilip Rakity * return what is possible given the options 445a303c531SPhilip Rakity */ 446a303c531SPhilip Rakity drive_strength = card->host->ops->select_drive_strength( 447a303c531SPhilip Rakity card->sw_caps.uhs_max_dtr, 448a303c531SPhilip Rakity host_drv_type, card_drv_type); 449a303c531SPhilip Rakity 450a303c531SPhilip Rakity /* if error just use default for drive strength B */ 451a303c531SPhilip Rakity err = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_DRIVE_STRENGTH, 0, 452a303c531SPhilip Rakity &card_strength); 453a303c531SPhilip Rakity if (err) 454a303c531SPhilip Rakity return; 455a303c531SPhilip Rakity 456a303c531SPhilip Rakity card_strength &= ~(SDIO_DRIVE_DTSx_MASK<<SDIO_DRIVE_DTSx_SHIFT); 457a303c531SPhilip Rakity card_strength |= host_drive_to_sdio_drive(drive_strength); 458a303c531SPhilip Rakity 459a303c531SPhilip Rakity err = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_DRIVE_STRENGTH, 460a303c531SPhilip Rakity card_strength, NULL); 461a303c531SPhilip Rakity 462a303c531SPhilip Rakity /* if error default to drive strength B */ 463a303c531SPhilip Rakity if (!err) 464a303c531SPhilip Rakity mmc_set_driver_type(card->host, drive_strength); 465a303c531SPhilip Rakity } 466a303c531SPhilip Rakity 467a303c531SPhilip Rakity 468a303c531SPhilip Rakity static int sdio_set_bus_speed_mode(struct mmc_card *card) 469a303c531SPhilip Rakity { 470a303c531SPhilip Rakity unsigned int bus_speed, timing; 471a303c531SPhilip Rakity int err; 472a303c531SPhilip Rakity unsigned char speed; 473a303c531SPhilip Rakity 474a303c531SPhilip Rakity /* 475a303c531SPhilip Rakity * If the host doesn't support any of the UHS-I modes, fallback on 476a303c531SPhilip Rakity * default speed. 477a303c531SPhilip Rakity */ 47841875e38SSujit Reddy Thumma if (!mmc_host_uhs(card->host)) 479a303c531SPhilip Rakity return 0; 480a303c531SPhilip Rakity 481a303c531SPhilip Rakity bus_speed = SDIO_SPEED_SDR12; 482a303c531SPhilip Rakity timing = MMC_TIMING_UHS_SDR12; 483a303c531SPhilip Rakity if ((card->host->caps & MMC_CAP_UHS_SDR104) && 484a303c531SPhilip Rakity (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_SDR104)) { 485a303c531SPhilip Rakity bus_speed = SDIO_SPEED_SDR104; 486a303c531SPhilip Rakity timing = MMC_TIMING_UHS_SDR104; 487a303c531SPhilip Rakity card->sw_caps.uhs_max_dtr = UHS_SDR104_MAX_DTR; 48877e2ff08SSubhash Jadavani card->sd_bus_speed = UHS_SDR104_BUS_SPEED; 489a303c531SPhilip Rakity } else if ((card->host->caps & MMC_CAP_UHS_DDR50) && 490a303c531SPhilip Rakity (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_DDR50)) { 491a303c531SPhilip Rakity bus_speed = SDIO_SPEED_DDR50; 492a303c531SPhilip Rakity timing = MMC_TIMING_UHS_DDR50; 493a303c531SPhilip Rakity card->sw_caps.uhs_max_dtr = UHS_DDR50_MAX_DTR; 49477e2ff08SSubhash Jadavani card->sd_bus_speed = UHS_DDR50_BUS_SPEED; 495a303c531SPhilip Rakity } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 | 496a303c531SPhilip Rakity MMC_CAP_UHS_SDR50)) && (card->sw_caps.sd3_bus_mode & 497a303c531SPhilip Rakity SD_MODE_UHS_SDR50)) { 498a303c531SPhilip Rakity bus_speed = SDIO_SPEED_SDR50; 499a303c531SPhilip Rakity timing = MMC_TIMING_UHS_SDR50; 500a303c531SPhilip Rakity card->sw_caps.uhs_max_dtr = UHS_SDR50_MAX_DTR; 50177e2ff08SSubhash Jadavani card->sd_bus_speed = UHS_SDR50_BUS_SPEED; 502a303c531SPhilip Rakity } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 | 503a303c531SPhilip Rakity MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR25)) && 504a303c531SPhilip Rakity (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_SDR25)) { 505a303c531SPhilip Rakity bus_speed = SDIO_SPEED_SDR25; 506a303c531SPhilip Rakity timing = MMC_TIMING_UHS_SDR25; 507a303c531SPhilip Rakity card->sw_caps.uhs_max_dtr = UHS_SDR25_MAX_DTR; 50877e2ff08SSubhash Jadavani card->sd_bus_speed = UHS_SDR25_BUS_SPEED; 509a303c531SPhilip Rakity } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 | 510a303c531SPhilip Rakity MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR25 | 511a303c531SPhilip Rakity MMC_CAP_UHS_SDR12)) && (card->sw_caps.sd3_bus_mode & 512a303c531SPhilip Rakity SD_MODE_UHS_SDR12)) { 513a303c531SPhilip Rakity bus_speed = SDIO_SPEED_SDR12; 514a303c531SPhilip Rakity timing = MMC_TIMING_UHS_SDR12; 515a303c531SPhilip Rakity card->sw_caps.uhs_max_dtr = UHS_SDR12_MAX_DTR; 51677e2ff08SSubhash Jadavani card->sd_bus_speed = UHS_SDR12_BUS_SPEED; 517a303c531SPhilip Rakity } 518a303c531SPhilip Rakity 519a303c531SPhilip Rakity err = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_SPEED, 0, &speed); 520a303c531SPhilip Rakity if (err) 521a303c531SPhilip Rakity return err; 522a303c531SPhilip Rakity 523a303c531SPhilip Rakity speed &= ~SDIO_SPEED_BSS_MASK; 524a303c531SPhilip Rakity speed |= bus_speed; 525a303c531SPhilip Rakity err = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_SPEED, speed, NULL); 526a303c531SPhilip Rakity if (err) 527a303c531SPhilip Rakity return err; 528a303c531SPhilip Rakity 529a303c531SPhilip Rakity if (bus_speed) { 530a303c531SPhilip Rakity mmc_set_timing(card->host, timing); 531a303c531SPhilip Rakity mmc_set_clock(card->host, card->sw_caps.uhs_max_dtr); 532a303c531SPhilip Rakity } 533a303c531SPhilip Rakity 534a303c531SPhilip Rakity return 0; 535a303c531SPhilip Rakity } 536a303c531SPhilip Rakity 537a303c531SPhilip Rakity /* 538a303c531SPhilip Rakity * UHS-I specific initialization procedure 539a303c531SPhilip Rakity */ 540a303c531SPhilip Rakity static int mmc_sdio_init_uhs_card(struct mmc_card *card) 541a303c531SPhilip Rakity { 542a303c531SPhilip Rakity int err; 543a303c531SPhilip Rakity 544a303c531SPhilip Rakity if (!card->scr.sda_spec3) 545a303c531SPhilip Rakity return 0; 546a303c531SPhilip Rakity 547a303c531SPhilip Rakity /* 548a303c531SPhilip Rakity * Switch to wider bus (if supported). 549a303c531SPhilip Rakity */ 550a303c531SPhilip Rakity if (card->host->caps & MMC_CAP_4_BIT_DATA) { 551a303c531SPhilip Rakity err = sdio_enable_4bit_bus(card); 552a303c531SPhilip Rakity if (err > 0) { 553a303c531SPhilip Rakity mmc_set_bus_width(card->host, MMC_BUS_WIDTH_4); 554a303c531SPhilip Rakity err = 0; 555a303c531SPhilip Rakity } 556a303c531SPhilip Rakity } 557a303c531SPhilip Rakity 558a303c531SPhilip Rakity /* Set the driver strength for the card */ 559a303c531SPhilip Rakity sdio_select_driver_type(card); 560a303c531SPhilip Rakity 561a303c531SPhilip Rakity /* Set bus speed mode of the card */ 562a303c531SPhilip Rakity err = sdio_set_bus_speed_mode(card); 563a303c531SPhilip Rakity if (err) 564a303c531SPhilip Rakity goto out; 565a303c531SPhilip Rakity 566a303c531SPhilip Rakity /* Initialize and start re-tuning timer */ 567a303c531SPhilip Rakity if (!mmc_host_is_spi(card->host) && card->host->ops->execute_tuning) 568a4924c71SGirish K S err = card->host->ops->execute_tuning(card->host, 569a4924c71SGirish K S MMC_SEND_TUNING_BLOCK); 570a303c531SPhilip Rakity 571a303c531SPhilip Rakity out: 572a303c531SPhilip Rakity 573a303c531SPhilip Rakity return err; 574a303c531SPhilip Rakity } 575a303c531SPhilip Rakity 576d16f5770SPierre Ossman /* 57717d33e14SNicolas Pitre * Handle the detection and initialisation of a card. 57817d33e14SNicolas Pitre * 57917d33e14SNicolas Pitre * In the case of a resume, "oldcard" will contain the card 58017d33e14SNicolas Pitre * we're trying to reinitialise. 58117d33e14SNicolas Pitre */ 58217d33e14SNicolas Pitre static int mmc_sdio_init_card(struct mmc_host *host, u32 ocr, 5833bca4cf7SChris Ball struct mmc_card *oldcard, int powered_resume) 58417d33e14SNicolas Pitre { 58517d33e14SNicolas Pitre struct mmc_card *card; 58617d33e14SNicolas Pitre int err; 5870797e5f1SJohan Rudholm int retries = 10; 58817d33e14SNicolas Pitre 58917d33e14SNicolas Pitre BUG_ON(!host); 59017d33e14SNicolas Pitre WARN_ON(!host->claimed); 59117d33e14SNicolas Pitre 5920797e5f1SJohan Rudholm try_again: 5930797e5f1SJohan Rudholm if (!retries) { 5940797e5f1SJohan Rudholm pr_warning("%s: Skipping voltage switch\n", 5950797e5f1SJohan Rudholm mmc_hostname(host)); 5960797e5f1SJohan Rudholm ocr &= ~R4_18V_PRESENT; 5970797e5f1SJohan Rudholm host->ocr &= ~R4_18V_PRESENT; 5980797e5f1SJohan Rudholm } 5990797e5f1SJohan Rudholm 60017d33e14SNicolas Pitre /* 60117d33e14SNicolas Pitre * Inform the card of the voltage 60217d33e14SNicolas Pitre */ 6033bca4cf7SChris Ball if (!powered_resume) { 60417d33e14SNicolas Pitre err = mmc_send_io_op_cond(host, host->ocr, &ocr); 60517d33e14SNicolas Pitre if (err) 60617d33e14SNicolas Pitre goto err; 6073bca4cf7SChris Ball } 60817d33e14SNicolas Pitre 60917d33e14SNicolas Pitre /* 61017d33e14SNicolas Pitre * For SPI, enable CRC as appropriate. 61117d33e14SNicolas Pitre */ 61217d33e14SNicolas Pitre if (mmc_host_is_spi(host)) { 61317d33e14SNicolas Pitre err = mmc_spi_set_crc(host, use_spi_crc); 61417d33e14SNicolas Pitre if (err) 61517d33e14SNicolas Pitre goto err; 61617d33e14SNicolas Pitre } 61717d33e14SNicolas Pitre 61817d33e14SNicolas Pitre /* 61917d33e14SNicolas Pitre * Allocate card structure. 62017d33e14SNicolas Pitre */ 62117d33e14SNicolas Pitre card = mmc_alloc_card(host, NULL); 62217d33e14SNicolas Pitre if (IS_ERR(card)) { 62317d33e14SNicolas Pitre err = PTR_ERR(card); 62417d33e14SNicolas Pitre goto err; 62517d33e14SNicolas Pitre } 62617d33e14SNicolas Pitre 627d6d50a15SArindam Nath if ((ocr & R4_MEMORY_PRESENT) && 628d6d50a15SArindam Nath mmc_sd_get_cid(host, host->ocr & ocr, card->raw_cid, NULL) == 0) { 6297310ece8SMichal Miroslaw card->type = MMC_TYPE_SD_COMBO; 6307310ece8SMichal Miroslaw 6317310ece8SMichal Miroslaw if (oldcard && (oldcard->type != MMC_TYPE_SD_COMBO || 6327310ece8SMichal Miroslaw memcmp(card->raw_cid, oldcard->raw_cid, sizeof(card->raw_cid)) != 0)) { 6337310ece8SMichal Miroslaw mmc_remove_card(card); 6347310ece8SMichal Miroslaw return -ENOENT; 6357310ece8SMichal Miroslaw } 6367310ece8SMichal Miroslaw } else { 63717d33e14SNicolas Pitre card->type = MMC_TYPE_SDIO; 63817d33e14SNicolas Pitre 6397310ece8SMichal Miroslaw if (oldcard && oldcard->type != MMC_TYPE_SDIO) { 6407310ece8SMichal Miroslaw mmc_remove_card(card); 6417310ece8SMichal Miroslaw return -ENOENT; 6427310ece8SMichal Miroslaw } 6437310ece8SMichal Miroslaw } 6447310ece8SMichal Miroslaw 64517d33e14SNicolas Pitre /* 6463fcb027dSDaniel Mack * Call the optional HC's init_card function to handle quirks. 6473fcb027dSDaniel Mack */ 6483fcb027dSDaniel Mack if (host->ops->init_card) 6493fcb027dSDaniel Mack host->ops->init_card(host, card); 6503fcb027dSDaniel Mack 6513fcb027dSDaniel Mack /* 652a303c531SPhilip Rakity * If the host and card support UHS-I mode request the card 653a303c531SPhilip Rakity * to switch to 1.8V signaling level. No 1.8v signalling if 6547122bbb0SMasanari Iida * UHS mode is not enabled to maintain compatibility and some 655a303c531SPhilip Rakity * systems that claim 1.8v signalling in fact do not support 656a303c531SPhilip Rakity * it. 657a303c531SPhilip Rakity */ 6580797e5f1SJohan Rudholm if (!powered_resume && (ocr & R4_18V_PRESENT) && mmc_host_uhs(host)) { 659567c8903SJohan Rudholm err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_180); 6600797e5f1SJohan Rudholm if (err == -EAGAIN) { 6610797e5f1SJohan Rudholm sdio_reset(host); 6620797e5f1SJohan Rudholm mmc_go_idle(host); 6630797e5f1SJohan Rudholm mmc_send_if_cond(host, host->ocr_avail); 6640797e5f1SJohan Rudholm mmc_remove_card(card); 6650797e5f1SJohan Rudholm retries--; 6660797e5f1SJohan Rudholm goto try_again; 6670797e5f1SJohan Rudholm } else if (err) { 668a303c531SPhilip Rakity ocr &= ~R4_18V_PRESENT; 669a303c531SPhilip Rakity host->ocr &= ~R4_18V_PRESENT; 670a303c531SPhilip Rakity } 671a303c531SPhilip Rakity err = 0; 672a303c531SPhilip Rakity } else { 673a303c531SPhilip Rakity ocr &= ~R4_18V_PRESENT; 674a303c531SPhilip Rakity host->ocr &= ~R4_18V_PRESENT; 675a303c531SPhilip Rakity } 676a303c531SPhilip Rakity 677a303c531SPhilip Rakity /* 67817d33e14SNicolas Pitre * For native busses: set card RCA and quit open drain mode. 67917d33e14SNicolas Pitre */ 6803bca4cf7SChris Ball if (!powered_resume && !mmc_host_is_spi(host)) { 68117d33e14SNicolas Pitre err = mmc_send_relative_addr(host, &card->rca); 68217d33e14SNicolas Pitre if (err) 68317d33e14SNicolas Pitre goto remove; 68417d33e14SNicolas Pitre 6850aab3995SStefan Nilsson XK /* 6860aab3995SStefan Nilsson XK * Update oldcard with the new RCA received from the SDIO 6870aab3995SStefan Nilsson XK * device -- we're doing this so that it's updated in the 6880aab3995SStefan Nilsson XK * "card" struct when oldcard overwrites that later. 6890aab3995SStefan Nilsson XK */ 6900aab3995SStefan Nilsson XK if (oldcard) 6910aab3995SStefan Nilsson XK oldcard->rca = card->rca; 69217d33e14SNicolas Pitre } 69317d33e14SNicolas Pitre 69417d33e14SNicolas Pitre /* 6957310ece8SMichal Miroslaw * Read CSD, before selecting the card 6967310ece8SMichal Miroslaw */ 6977310ece8SMichal Miroslaw if (!oldcard && card->type == MMC_TYPE_SD_COMBO) { 6987310ece8SMichal Miroslaw err = mmc_sd_get_csd(host, card); 6997310ece8SMichal Miroslaw if (err) 7007310ece8SMichal Miroslaw return err; 7017310ece8SMichal Miroslaw 7027310ece8SMichal Miroslaw mmc_decode_cid(card); 7037310ece8SMichal Miroslaw } 7047310ece8SMichal Miroslaw 7057310ece8SMichal Miroslaw /* 70617d33e14SNicolas Pitre * Select card, as all following commands rely on that. 70717d33e14SNicolas Pitre */ 7083bca4cf7SChris Ball if (!powered_resume && !mmc_host_is_spi(host)) { 70917d33e14SNicolas Pitre err = mmc_select_card(card); 71017d33e14SNicolas Pitre if (err) 71117d33e14SNicolas Pitre goto remove; 71217d33e14SNicolas Pitre } 71317d33e14SNicolas Pitre 7146f51be3dSGrazvydas Ignotas if (card->quirks & MMC_QUIRK_NONSTD_SDIO) { 7156f51be3dSGrazvydas Ignotas /* 7166f51be3dSGrazvydas Ignotas * This is non-standard SDIO device, meaning it doesn't 7176f51be3dSGrazvydas Ignotas * have any CIA (Common I/O area) registers present. 7186f51be3dSGrazvydas Ignotas * It's host's responsibility to fill cccr and cis 7196f51be3dSGrazvydas Ignotas * structures in init_card(). 7206f51be3dSGrazvydas Ignotas */ 7216f51be3dSGrazvydas Ignotas mmc_set_clock(host, card->cis.max_dtr); 7226f51be3dSGrazvydas Ignotas 7236f51be3dSGrazvydas Ignotas if (card->cccr.high_speed) { 7246f51be3dSGrazvydas Ignotas mmc_card_set_highspeed(card); 7256f51be3dSGrazvydas Ignotas mmc_set_timing(card->host, MMC_TIMING_SD_HS); 7266f51be3dSGrazvydas Ignotas } 7276f51be3dSGrazvydas Ignotas 7286f51be3dSGrazvydas Ignotas goto finish; 7296f51be3dSGrazvydas Ignotas } 7306f51be3dSGrazvydas Ignotas 73117d33e14SNicolas Pitre /* 73217d33e14SNicolas Pitre * Read the common registers. 73317d33e14SNicolas Pitre */ 7342d0d68f5SPhilip Rakity err = sdio_read_cccr(card, ocr); 73517d33e14SNicolas Pitre if (err) 73617d33e14SNicolas Pitre goto remove; 73717d33e14SNicolas Pitre 73817d33e14SNicolas Pitre /* 73917d33e14SNicolas Pitre * Read the common CIS tuples. 74017d33e14SNicolas Pitre */ 74117d33e14SNicolas Pitre err = sdio_read_common_cis(card); 74217d33e14SNicolas Pitre if (err) 74317d33e14SNicolas Pitre goto remove; 74417d33e14SNicolas Pitre 74517d33e14SNicolas Pitre if (oldcard) { 74617d33e14SNicolas Pitre int same = (card->cis.vendor == oldcard->cis.vendor && 74717d33e14SNicolas Pitre card->cis.device == oldcard->cis.device); 74817d33e14SNicolas Pitre mmc_remove_card(card); 7497310ece8SMichal Miroslaw if (!same) 7507310ece8SMichal Miroslaw return -ENOENT; 7517310ece8SMichal Miroslaw 75217d33e14SNicolas Pitre card = oldcard; 75317d33e14SNicolas Pitre } 75432780cd1SAndrei Warkentin mmc_fixup_device(card, NULL); 75517d33e14SNicolas Pitre 7567310ece8SMichal Miroslaw if (card->type == MMC_TYPE_SD_COMBO) { 7577310ece8SMichal Miroslaw err = mmc_sd_setup_card(host, card, oldcard != NULL); 7587310ece8SMichal Miroslaw /* handle as SDIO-only card if memory init failed */ 7597310ece8SMichal Miroslaw if (err) { 7607310ece8SMichal Miroslaw mmc_go_idle(host); 7617310ece8SMichal Miroslaw if (mmc_host_is_spi(host)) 7627310ece8SMichal Miroslaw /* should not fail, as it worked previously */ 7637310ece8SMichal Miroslaw mmc_spi_set_crc(host, use_spi_crc); 7647310ece8SMichal Miroslaw card->type = MMC_TYPE_SDIO; 7657310ece8SMichal Miroslaw } else 7667310ece8SMichal Miroslaw card->dev.type = &sd_type; 7677310ece8SMichal Miroslaw } 7687310ece8SMichal Miroslaw 7697310ece8SMichal Miroslaw /* 7707310ece8SMichal Miroslaw * If needed, disconnect card detection pull-up resistor. 7717310ece8SMichal Miroslaw */ 7727310ece8SMichal Miroslaw err = sdio_disable_cd(card); 7737310ece8SMichal Miroslaw if (err) 7747310ece8SMichal Miroslaw goto remove; 7757310ece8SMichal Miroslaw 776a303c531SPhilip Rakity /* Initialization sequence for UHS-I cards */ 777a303c531SPhilip Rakity /* Only if card supports 1.8v and UHS signaling */ 778a303c531SPhilip Rakity if ((ocr & R4_18V_PRESENT) && card->sw_caps.sd3_bus_mode) { 779a303c531SPhilip Rakity err = mmc_sdio_init_uhs_card(card); 780a303c531SPhilip Rakity if (err) 781a303c531SPhilip Rakity goto remove; 782a303c531SPhilip Rakity 783a303c531SPhilip Rakity /* Card is an ultra-high-speed card */ 784a303c531SPhilip Rakity mmc_card_set_uhs(card); 785a303c531SPhilip Rakity } else { 78617d33e14SNicolas Pitre /* 78717d33e14SNicolas Pitre * Switch to high-speed (if supported). 78817d33e14SNicolas Pitre */ 78917d33e14SNicolas Pitre err = sdio_enable_hs(card); 79071578a1eSMichal Miroslaw if (err > 0) 79171578a1eSMichal Miroslaw mmc_sd_go_highspeed(card); 79271578a1eSMichal Miroslaw else if (err) 79317d33e14SNicolas Pitre goto remove; 79417d33e14SNicolas Pitre 79517d33e14SNicolas Pitre /* 79617d33e14SNicolas Pitre * Change to the card's maximum speed. 79717d33e14SNicolas Pitre */ 79871578a1eSMichal Miroslaw mmc_set_clock(host, mmc_sdio_get_max_clock(card)); 79917d33e14SNicolas Pitre 80017d33e14SNicolas Pitre /* 80117d33e14SNicolas Pitre * Switch to wider bus (if supported). 80217d33e14SNicolas Pitre */ 8037310ece8SMichal Miroslaw err = sdio_enable_4bit_bus(card); 8047310ece8SMichal Miroslaw if (err > 0) 8057310ece8SMichal Miroslaw mmc_set_bus_width(card->host, MMC_BUS_WIDTH_4); 8067310ece8SMichal Miroslaw else if (err) 80717d33e14SNicolas Pitre goto remove; 808a303c531SPhilip Rakity } 8096f51be3dSGrazvydas Ignotas finish: 81017d33e14SNicolas Pitre if (!oldcard) 81117d33e14SNicolas Pitre host->card = card; 81217d33e14SNicolas Pitre return 0; 81317d33e14SNicolas Pitre 81417d33e14SNicolas Pitre remove: 81517d33e14SNicolas Pitre if (!oldcard) 81617d33e14SNicolas Pitre mmc_remove_card(card); 81717d33e14SNicolas Pitre 81817d33e14SNicolas Pitre err: 81917d33e14SNicolas Pitre return err; 82017d33e14SNicolas Pitre } 82117d33e14SNicolas Pitre 82217d33e14SNicolas Pitre /* 8235c4e6f13SPierre Ossman * Host is being removed. Free up the current card. 8245c4e6f13SPierre Ossman */ 8255c4e6f13SPierre Ossman static void mmc_sdio_remove(struct mmc_host *host) 8265c4e6f13SPierre Ossman { 827e29a7d73SPierre Ossman int i; 828e29a7d73SPierre Ossman 8295c4e6f13SPierre Ossman BUG_ON(!host); 8305c4e6f13SPierre Ossman BUG_ON(!host->card); 8315c4e6f13SPierre Ossman 832e29a7d73SPierre Ossman for (i = 0;i < host->card->sdio_funcs;i++) { 833e29a7d73SPierre Ossman if (host->card->sdio_func[i]) { 834e29a7d73SPierre Ossman sdio_remove_func(host->card->sdio_func[i]); 835e29a7d73SPierre Ossman host->card->sdio_func[i] = NULL; 836e29a7d73SPierre Ossman } 837e29a7d73SPierre Ossman } 838e29a7d73SPierre Ossman 8395c4e6f13SPierre Ossman mmc_remove_card(host->card); 8405c4e6f13SPierre Ossman host->card = NULL; 8415c4e6f13SPierre Ossman } 8425c4e6f13SPierre Ossman 8435c4e6f13SPierre Ossman /* 844d3049504SAdrian Hunter * Card detection - card is alive. 845d3049504SAdrian Hunter */ 846d3049504SAdrian Hunter static int mmc_sdio_alive(struct mmc_host *host) 847d3049504SAdrian Hunter { 848d3049504SAdrian Hunter return mmc_select_card(host->card); 849d3049504SAdrian Hunter } 850d3049504SAdrian Hunter 851d3049504SAdrian Hunter /* 8525c4e6f13SPierre Ossman * Card detection callback from host. 8535c4e6f13SPierre Ossman */ 8545c4e6f13SPierre Ossman static void mmc_sdio_detect(struct mmc_host *host) 8555c4e6f13SPierre Ossman { 8565c4e6f13SPierre Ossman int err; 8575c4e6f13SPierre Ossman 8585c4e6f13SPierre Ossman BUG_ON(!host); 8595c4e6f13SPierre Ossman BUG_ON(!host->card); 8605c4e6f13SPierre Ossman 86187973ba2SOhad Ben-Cohen /* Make sure card is powered before detecting it */ 862ed919b01SOhad Ben-Cohen if (host->caps & MMC_CAP_POWER_OFF_CARD) { 86387973ba2SOhad Ben-Cohen err = pm_runtime_get_sync(&host->card->dev); 864*3bffb800SLi Fei if (err < 0) { 865*3bffb800SLi Fei pm_runtime_put_noidle(&host->card->dev); 86687973ba2SOhad Ben-Cohen goto out; 867ed919b01SOhad Ben-Cohen } 868*3bffb800SLi Fei } 86987973ba2SOhad Ben-Cohen 8705c4e6f13SPierre Ossman mmc_claim_host(host); 8715c4e6f13SPierre Ossman 8725c4e6f13SPierre Ossman /* 8735c4e6f13SPierre Ossman * Just check if our card has been removed. 8745c4e6f13SPierre Ossman */ 875d3049504SAdrian Hunter err = _mmc_detect_card_removed(host); 8765c4e6f13SPierre Ossman 8775c4e6f13SPierre Ossman mmc_release_host(host); 8785c4e6f13SPierre Ossman 8794d0812c3SOhad Ben-Cohen /* 8804d0812c3SOhad Ben-Cohen * Tell PM core it's OK to power off the card now. 8814d0812c3SOhad Ben-Cohen * 8824d0812c3SOhad Ben-Cohen * The _sync variant is used in order to ensure that the card 8834d0812c3SOhad Ben-Cohen * is left powered off in case an error occurred, and the card 8844d0812c3SOhad Ben-Cohen * is going to be removed. 8854d0812c3SOhad Ben-Cohen * 8864d0812c3SOhad Ben-Cohen * Since there is no specific reason to believe a new user 8874d0812c3SOhad Ben-Cohen * is about to show up at this point, the _sync variant is 8884d0812c3SOhad Ben-Cohen * desirable anyway. 8894d0812c3SOhad Ben-Cohen */ 890ed919b01SOhad Ben-Cohen if (host->caps & MMC_CAP_POWER_OFF_CARD) 8914d0812c3SOhad Ben-Cohen pm_runtime_put_sync(&host->card->dev); 8924d0812c3SOhad Ben-Cohen 89387973ba2SOhad Ben-Cohen out: 8945c4e6f13SPierre Ossman if (err) { 8955c4e6f13SPierre Ossman mmc_sdio_remove(host); 8965c4e6f13SPierre Ossman 8975c4e6f13SPierre Ossman mmc_claim_host(host); 8985c4e6f13SPierre Ossman mmc_detach_bus(host); 8997f7e4129SUlf Hansson mmc_power_off(host); 9005c4e6f13SPierre Ossman mmc_release_host(host); 9015c4e6f13SPierre Ossman } 9025c4e6f13SPierre Ossman } 9035c4e6f13SPierre Ossman 90417d33e14SNicolas Pitre /* 90517d33e14SNicolas Pitre * SDIO suspend. We need to suspend all functions separately. 90617d33e14SNicolas Pitre * Therefore all registered functions must have drivers with suspend 90717d33e14SNicolas Pitre * and resume methods. Failing that we simply remove the whole card. 90817d33e14SNicolas Pitre */ 90995cdfb72SNicolas Pitre static int mmc_sdio_suspend(struct mmc_host *host) 91017d33e14SNicolas Pitre { 91195cdfb72SNicolas Pitre int i, err = 0; 91217d33e14SNicolas Pitre 91317d33e14SNicolas Pitre for (i = 0; i < host->card->sdio_funcs; i++) { 91417d33e14SNicolas Pitre struct sdio_func *func = host->card->sdio_func[i]; 91517d33e14SNicolas Pitre if (func && sdio_func_present(func) && func->dev.driver) { 91617d33e14SNicolas Pitre const struct dev_pm_ops *pmops = func->dev.driver->pm; 91717d33e14SNicolas Pitre if (!pmops || !pmops->suspend || !pmops->resume) { 91895cdfb72SNicolas Pitre /* force removal of entire card in that case */ 91995cdfb72SNicolas Pitre err = -ENOSYS; 92095cdfb72SNicolas Pitre } else 92195cdfb72SNicolas Pitre err = pmops->suspend(&func->dev); 92295cdfb72SNicolas Pitre if (err) 92395cdfb72SNicolas Pitre break; 92417d33e14SNicolas Pitre } 92517d33e14SNicolas Pitre } 92695cdfb72SNicolas Pitre while (err && --i >= 0) { 92717d33e14SNicolas Pitre struct sdio_func *func = host->card->sdio_func[i]; 92817d33e14SNicolas Pitre if (func && sdio_func_present(func) && func->dev.driver) { 92917d33e14SNicolas Pitre const struct dev_pm_ops *pmops = func->dev.driver->pm; 93017d33e14SNicolas Pitre pmops->resume(&func->dev); 93117d33e14SNicolas Pitre } 93217d33e14SNicolas Pitre } 93395cdfb72SNicolas Pitre 9346b93d01fSOhad Ben-Cohen if (!err && mmc_card_keep_power(host) && mmc_card_wake_sdio_irq(host)) { 9356b5eda36SDaniel Drake mmc_claim_host(host); 9366b5eda36SDaniel Drake sdio_disable_wide(host->card); 9376b5eda36SDaniel Drake mmc_release_host(host); 9386b5eda36SDaniel Drake } 9396b5eda36SDaniel Drake 94095cdfb72SNicolas Pitre return err; 94195cdfb72SNicolas Pitre } 94295cdfb72SNicolas Pitre 94395cdfb72SNicolas Pitre static int mmc_sdio_resume(struct mmc_host *host) 94495cdfb72SNicolas Pitre { 945080bc977SOhad Ben-Cohen int i, err = 0; 94695cdfb72SNicolas Pitre 94795cdfb72SNicolas Pitre BUG_ON(!host); 94895cdfb72SNicolas Pitre BUG_ON(!host->card); 94995cdfb72SNicolas Pitre 95095cdfb72SNicolas Pitre /* Basic card reinitialization. */ 95195cdfb72SNicolas Pitre mmc_claim_host(host); 952080bc977SOhad Ben-Cohen 953080bc977SOhad Ben-Cohen /* No need to reinitialize powered-resumed nonremovable cards */ 95451aa66a5SSubhash Jadavani if (mmc_card_is_removable(host) || !mmc_card_keep_power(host)) { 95551aa66a5SSubhash Jadavani sdio_reset(host); 95651aa66a5SSubhash Jadavani mmc_go_idle(host); 9573bca4cf7SChris Ball err = mmc_sdio_init_card(host, host->ocr, host->card, 958a5e9425dSOhad Ben-Cohen mmc_card_keep_power(host)); 95951aa66a5SSubhash Jadavani } else if (mmc_card_keep_power(host) && mmc_card_wake_sdio_irq(host)) { 960080bc977SOhad Ben-Cohen /* We may have switched to 1-bit mode during suspend */ 961080bc977SOhad Ben-Cohen err = sdio_enable_4bit_bus(host->card); 962080bc977SOhad Ben-Cohen if (err > 0) { 963080bc977SOhad Ben-Cohen mmc_set_bus_width(host, MMC_BUS_WIDTH_4); 964080bc977SOhad Ben-Cohen err = 0; 965080bc977SOhad Ben-Cohen } 966080bc977SOhad Ben-Cohen } 967080bc977SOhad Ben-Cohen 96840216842SNicolas Pitre if (!err && host->sdio_irqs) 969bbbc4c4dSNicolas Pitre wake_up_process(host->sdio_irq_thread); 97095cdfb72SNicolas Pitre mmc_release_host(host); 97195cdfb72SNicolas Pitre 97295cdfb72SNicolas Pitre /* 97395cdfb72SNicolas Pitre * If the card looked to be the same as before suspending, then 97495cdfb72SNicolas Pitre * we proceed to resume all card functions. If one of them returns 97595cdfb72SNicolas Pitre * an error then we simply return that error to the core and the 97695cdfb72SNicolas Pitre * card will be redetected as new. It is the responsibility of 97795cdfb72SNicolas Pitre * the function driver to perform further tests with the extra 97895cdfb72SNicolas Pitre * knowledge it has of the card to confirm the card is indeed the 97995cdfb72SNicolas Pitre * same as before suspending (same MAC address for network cards, 98095cdfb72SNicolas Pitre * etc.) and return an error otherwise. 98195cdfb72SNicolas Pitre */ 98295cdfb72SNicolas Pitre for (i = 0; !err && i < host->card->sdio_funcs; i++) { 98395cdfb72SNicolas Pitre struct sdio_func *func = host->card->sdio_func[i]; 98495cdfb72SNicolas Pitre if (func && sdio_func_present(func) && func->dev.driver) { 98595cdfb72SNicolas Pitre const struct dev_pm_ops *pmops = func->dev.driver->pm; 98695cdfb72SNicolas Pitre err = pmops->resume(&func->dev); 98795cdfb72SNicolas Pitre } 98895cdfb72SNicolas Pitre } 98995cdfb72SNicolas Pitre 99095cdfb72SNicolas Pitre return err; 99117d33e14SNicolas Pitre } 9925c4e6f13SPierre Ossman 993d3fe37b1SOhad Ben-Cohen static int mmc_sdio_power_restore(struct mmc_host *host) 994d3fe37b1SOhad Ben-Cohen { 995d3fe37b1SOhad Ben-Cohen int ret; 996c6e633adSDaniel Drake u32 ocr; 997d3fe37b1SOhad Ben-Cohen 998d3fe37b1SOhad Ben-Cohen BUG_ON(!host); 999d3fe37b1SOhad Ben-Cohen BUG_ON(!host->card); 1000d3fe37b1SOhad Ben-Cohen 1001d3fe37b1SOhad Ben-Cohen mmc_claim_host(host); 1002c6e633adSDaniel Drake 1003c6e633adSDaniel Drake /* 1004c6e633adSDaniel Drake * Reset the card by performing the same steps that are taken by 1005c6e633adSDaniel Drake * mmc_rescan_try_freq() and mmc_attach_sdio() during a "normal" probe. 1006c6e633adSDaniel Drake * 1007c6e633adSDaniel Drake * sdio_reset() is technically not needed. Having just powered up the 1008c6e633adSDaniel Drake * hardware, it should already be in reset state. However, some 1009c6e633adSDaniel Drake * platforms (such as SD8686 on OLPC) do not instantly cut power, 1010c6e633adSDaniel Drake * meaning that a reset is required when restoring power soon after 1011c6e633adSDaniel Drake * powering off. It is harmless in other cases. 1012c6e633adSDaniel Drake * 1013c6e633adSDaniel Drake * The CMD5 reset (mmc_send_io_op_cond()), according to the SDIO spec, 1014c6e633adSDaniel Drake * is not necessary for non-removable cards. However, it is required 1015c6e633adSDaniel Drake * for OLPC SD8686 (which expects a [CMD5,5,3,7] init sequence), and 1016c6e633adSDaniel Drake * harmless in other situations. 1017c6e633adSDaniel Drake * 1018c6e633adSDaniel Drake * With these steps taken, mmc_select_voltage() is also required to 1019c6e633adSDaniel Drake * restore the correct voltage setting of the card. 1020c6e633adSDaniel Drake */ 1021e7747475SUlf Hansson 1022c6e633adSDaniel Drake sdio_reset(host); 1023c6e633adSDaniel Drake mmc_go_idle(host); 1024c6e633adSDaniel Drake mmc_send_if_cond(host, host->ocr_avail); 1025c6e633adSDaniel Drake 1026c6e633adSDaniel Drake ret = mmc_send_io_op_cond(host, 0, &ocr); 1027c6e633adSDaniel Drake if (ret) 1028c6e633adSDaniel Drake goto out; 1029c6e633adSDaniel Drake 1030c6e633adSDaniel Drake if (host->ocr_avail_sdio) 1031c6e633adSDaniel Drake host->ocr_avail = host->ocr_avail_sdio; 1032c6e633adSDaniel Drake 1033c6e633adSDaniel Drake host->ocr = mmc_select_voltage(host, ocr & ~0x7F); 1034c6e633adSDaniel Drake if (!host->ocr) { 1035c6e633adSDaniel Drake ret = -EINVAL; 1036c6e633adSDaniel Drake goto out; 1037c6e633adSDaniel Drake } 1038c6e633adSDaniel Drake 103941875e38SSujit Reddy Thumma if (mmc_host_uhs(host)) 104041875e38SSujit Reddy Thumma /* to query card if 1.8V signalling is supported */ 104141875e38SSujit Reddy Thumma host->ocr |= R4_18V_PRESENT; 104241875e38SSujit Reddy Thumma 1043d3fe37b1SOhad Ben-Cohen ret = mmc_sdio_init_card(host, host->ocr, host->card, 1044a5e9425dSOhad Ben-Cohen mmc_card_keep_power(host)); 1045d3fe37b1SOhad Ben-Cohen if (!ret && host->sdio_irqs) 1046d3fe37b1SOhad Ben-Cohen mmc_signal_sdio_irq(host); 1047c6e633adSDaniel Drake 1048c6e633adSDaniel Drake out: 1049d3fe37b1SOhad Ben-Cohen mmc_release_host(host); 1050d3fe37b1SOhad Ben-Cohen 1051d3fe37b1SOhad Ben-Cohen return ret; 1052d3fe37b1SOhad Ben-Cohen } 1053d3fe37b1SOhad Ben-Cohen 10545c4e6f13SPierre Ossman static const struct mmc_bus_ops mmc_sdio_ops = { 10555c4e6f13SPierre Ossman .remove = mmc_sdio_remove, 10565c4e6f13SPierre Ossman .detect = mmc_sdio_detect, 105717d33e14SNicolas Pitre .suspend = mmc_sdio_suspend, 105817d33e14SNicolas Pitre .resume = mmc_sdio_resume, 1059d3fe37b1SOhad Ben-Cohen .power_restore = mmc_sdio_power_restore, 1060d3049504SAdrian Hunter .alive = mmc_sdio_alive, 10615c4e6f13SPierre Ossman }; 10625c4e6f13SPierre Ossman 10635c4e6f13SPierre Ossman 10645c4e6f13SPierre Ossman /* 10655c4e6f13SPierre Ossman * Starting point for SDIO card init. 10665c4e6f13SPierre Ossman */ 1067807e8e40SAndy Ross int mmc_attach_sdio(struct mmc_host *host) 10685c4e6f13SPierre Ossman { 1069807e8e40SAndy Ross int err, i, funcs; 1070807e8e40SAndy Ross u32 ocr; 10715c4e6f13SPierre Ossman struct mmc_card *card; 10725c4e6f13SPierre Ossman 10735c4e6f13SPierre Ossman BUG_ON(!host); 1074d84075c8SPierre Ossman WARN_ON(!host->claimed); 10755c4e6f13SPierre Ossman 1076807e8e40SAndy Ross err = mmc_send_io_op_cond(host, 0, &ocr); 1077807e8e40SAndy Ross if (err) 1078807e8e40SAndy Ross return err; 1079807e8e40SAndy Ross 10805c4e6f13SPierre Ossman mmc_attach_bus(host, &mmc_sdio_ops); 10818f230f45STakashi Iwai if (host->ocr_avail_sdio) 10828f230f45STakashi Iwai host->ocr_avail = host->ocr_avail_sdio; 10835c4e6f13SPierre Ossman 10845c4e6f13SPierre Ossman /* 10855c4e6f13SPierre Ossman * Sanity check the voltages that the card claims to 10865c4e6f13SPierre Ossman * support. 10875c4e6f13SPierre Ossman */ 10885c4e6f13SPierre Ossman if (ocr & 0x7F) { 1089a3c76eb9SGirish K S pr_warning("%s: card claims to support voltages " 10905c4e6f13SPierre Ossman "below the defined range. These will be ignored.\n", 10915c4e6f13SPierre Ossman mmc_hostname(host)); 10925c4e6f13SPierre Ossman ocr &= ~0x7F; 10935c4e6f13SPierre Ossman } 10945c4e6f13SPierre Ossman 10955c4e6f13SPierre Ossman host->ocr = mmc_select_voltage(host, ocr); 10965c4e6f13SPierre Ossman 10975c4e6f13SPierre Ossman /* 10985c4e6f13SPierre Ossman * Can we support the voltage(s) of the card(s)? 10995c4e6f13SPierre Ossman */ 11005c4e6f13SPierre Ossman if (!host->ocr) { 11015c4e6f13SPierre Ossman err = -EINVAL; 11025c4e6f13SPierre Ossman goto err; 11035c4e6f13SPierre Ossman } 11045c4e6f13SPierre Ossman 11055c4e6f13SPierre Ossman /* 110617d33e14SNicolas Pitre * Detect and init the card. 11075c4e6f13SPierre Ossman */ 110841875e38SSujit Reddy Thumma if (mmc_host_uhs(host)) 110941875e38SSujit Reddy Thumma /* to query card if 1.8V signalling is supported */ 111041875e38SSujit Reddy Thumma host->ocr |= R4_18V_PRESENT; 111141875e38SSujit Reddy Thumma 11123bca4cf7SChris Ball err = mmc_sdio_init_card(host, host->ocr, NULL, 0); 1113a303c531SPhilip Rakity if (err) { 1114a303c531SPhilip Rakity if (err == -EAGAIN) { 1115a303c531SPhilip Rakity /* 1116a303c531SPhilip Rakity * Retry initialization with S18R set to 0. 1117a303c531SPhilip Rakity */ 1118a303c531SPhilip Rakity host->ocr &= ~R4_18V_PRESENT; 1119a303c531SPhilip Rakity err = mmc_sdio_init_card(host, host->ocr, NULL, 0); 1120a303c531SPhilip Rakity } 11215c4e6f13SPierre Ossman if (err) 11225c4e6f13SPierre Ossman goto err; 1123a303c531SPhilip Rakity } 112417d33e14SNicolas Pitre card = host->card; 1125af517150SDavid Brownell 1126af517150SDavid Brownell /* 1127ed919b01SOhad Ben-Cohen * Enable runtime PM only if supported by host+card+board 1128ed919b01SOhad Ben-Cohen */ 1129ed919b01SOhad Ben-Cohen if (host->caps & MMC_CAP_POWER_OFF_CARD) { 1130ed919b01SOhad Ben-Cohen /* 113181968561SOhad Ben-Cohen * Let runtime PM core know our card is active 113281968561SOhad Ben-Cohen */ 113381968561SOhad Ben-Cohen err = pm_runtime_set_active(&card->dev); 113481968561SOhad Ben-Cohen if (err) 113581968561SOhad Ben-Cohen goto remove; 113681968561SOhad Ben-Cohen 113781968561SOhad Ben-Cohen /* 113881968561SOhad Ben-Cohen * Enable runtime PM for this card 113981968561SOhad Ben-Cohen */ 114081968561SOhad Ben-Cohen pm_runtime_enable(&card->dev); 1141ed919b01SOhad Ben-Cohen } 114281968561SOhad Ben-Cohen 114381968561SOhad Ben-Cohen /* 11445c4e6f13SPierre Ossman * The number of functions on the card is encoded inside 11455c4e6f13SPierre Ossman * the ocr. 11465c4e6f13SPierre Ossman */ 1147e8812793SMatt Fleming funcs = (ocr & 0x70000000) >> 28; 1148e8812793SMatt Fleming card->sdio_funcs = 0; 11494ff6471cSPierre Ossman 11504ff6471cSPierre Ossman /* 1151e29a7d73SPierre Ossman * Initialize (but don't add) all present functions. 1152e29a7d73SPierre Ossman */ 1153e8812793SMatt Fleming for (i = 0; i < funcs; i++, card->sdio_funcs++) { 1154e29a7d73SPierre Ossman err = sdio_init_func(host->card, i + 1); 1155e29a7d73SPierre Ossman if (err) 1156e29a7d73SPierre Ossman goto remove; 115740bba0c1SOhad Ben-Cohen 115840bba0c1SOhad Ben-Cohen /* 1159ed919b01SOhad Ben-Cohen * Enable Runtime PM for this func (if supported) 116040bba0c1SOhad Ben-Cohen */ 1161ed919b01SOhad Ben-Cohen if (host->caps & MMC_CAP_POWER_OFF_CARD) 116240bba0c1SOhad Ben-Cohen pm_runtime_enable(&card->sdio_func[i]->dev); 1163e29a7d73SPierre Ossman } 11645c4e6f13SPierre Ossman 1165e29a7d73SPierre Ossman /* 1166e29a7d73SPierre Ossman * First add the card to the driver model... 1167e29a7d73SPierre Ossman */ 1168807e8e40SAndy Ross mmc_release_host(host); 11695c4e6f13SPierre Ossman err = mmc_add_card(host->card); 11705c4e6f13SPierre Ossman if (err) 1171e29a7d73SPierre Ossman goto remove_added; 1172e29a7d73SPierre Ossman 1173e29a7d73SPierre Ossman /* 1174e29a7d73SPierre Ossman * ...then the SDIO functions. 1175e29a7d73SPierre Ossman */ 1176e29a7d73SPierre Ossman for (i = 0;i < funcs;i++) { 1177e29a7d73SPierre Ossman err = sdio_add_func(host->card->sdio_func[i]); 1178e29a7d73SPierre Ossman if (err) 1179e29a7d73SPierre Ossman goto remove_added; 1180e29a7d73SPierre Ossman } 11815c4e6f13SPierre Ossman 118234497913SDmitry Shmidt mmc_claim_host(host); 11835c4e6f13SPierre Ossman return 0; 11845c4e6f13SPierre Ossman 1185e29a7d73SPierre Ossman 1186e29a7d73SPierre Ossman remove_added: 1187e29a7d73SPierre Ossman /* Remove without lock if the device has been added. */ 1188e29a7d73SPierre Ossman mmc_sdio_remove(host); 11895c4e6f13SPierre Ossman mmc_claim_host(host); 1190e29a7d73SPierre Ossman remove: 1191e29a7d73SPierre Ossman /* And with lock if it hasn't been added. */ 1192807e8e40SAndy Ross mmc_release_host(host); 1193e29a7d73SPierre Ossman if (host->card) 1194e29a7d73SPierre Ossman mmc_sdio_remove(host); 1195807e8e40SAndy Ross mmc_claim_host(host); 11965c4e6f13SPierre Ossman err: 11975c4e6f13SPierre Ossman mmc_detach_bus(host); 11985c4e6f13SPierre Ossman 1199a3c76eb9SGirish K S pr_err("%s: error %d whilst initialising SDIO card\n", 12005c4e6f13SPierre Ossman mmc_hostname(host), err); 12015c4e6f13SPierre Ossman 12025c4e6f13SPierre Ossman return err; 12035c4e6f13SPierre Ossman } 12045c4e6f13SPierre Ossman 1205