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 566810e08eeSFredrik Soderstedt /* 567810e08eeSFredrik Soderstedt * SPI mode doesn't define CMD19 and tuning is only valid for SDR50 and 568810e08eeSFredrik Soderstedt * SDR104 mode SD-cards. Note that tuning is mandatory for SDR104. 569810e08eeSFredrik Soderstedt */ 570810e08eeSFredrik Soderstedt if (!mmc_host_is_spi(card->host) && card->host->ops->execute_tuning && 571810e08eeSFredrik Soderstedt ((card->sw_caps.sd3_bus_mode & SD_MODE_UHS_SDR50) || 572810e08eeSFredrik Soderstedt (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_SDR104))) { 573810e08eeSFredrik Soderstedt mmc_host_clk_hold(card->host); 574a4924c71SGirish K S err = card->host->ops->execute_tuning(card->host, 575a4924c71SGirish K S MMC_SEND_TUNING_BLOCK); 576810e08eeSFredrik Soderstedt mmc_host_clk_release(card->host); 577810e08eeSFredrik Soderstedt } 578a303c531SPhilip Rakity 579a303c531SPhilip Rakity out: 580a303c531SPhilip Rakity 581a303c531SPhilip Rakity return err; 582a303c531SPhilip Rakity } 583a303c531SPhilip Rakity 584d16f5770SPierre Ossman /* 58517d33e14SNicolas Pitre * Handle the detection and initialisation of a card. 58617d33e14SNicolas Pitre * 58717d33e14SNicolas Pitre * In the case of a resume, "oldcard" will contain the card 58817d33e14SNicolas Pitre * we're trying to reinitialise. 58917d33e14SNicolas Pitre */ 59017d33e14SNicolas Pitre static int mmc_sdio_init_card(struct mmc_host *host, u32 ocr, 5913bca4cf7SChris Ball struct mmc_card *oldcard, int powered_resume) 59217d33e14SNicolas Pitre { 59317d33e14SNicolas Pitre struct mmc_card *card; 59417d33e14SNicolas Pitre int err; 5950797e5f1SJohan Rudholm int retries = 10; 596db4a0d05SUlf Hansson u32 rocr = 0; 59769041150SUlf Hansson u32 ocr_card = ocr; 59817d33e14SNicolas Pitre 59917d33e14SNicolas Pitre BUG_ON(!host); 60017d33e14SNicolas Pitre WARN_ON(!host->claimed); 60117d33e14SNicolas Pitre 602db4a0d05SUlf Hansson /* to query card if 1.8V signalling is supported */ 603db4a0d05SUlf Hansson if (mmc_host_uhs(host)) 604db4a0d05SUlf Hansson ocr |= R4_18V_PRESENT; 605db4a0d05SUlf Hansson 6060797e5f1SJohan Rudholm try_again: 6070797e5f1SJohan Rudholm if (!retries) { 6080797e5f1SJohan Rudholm pr_warning("%s: Skipping voltage switch\n", 6090797e5f1SJohan Rudholm mmc_hostname(host)); 6100797e5f1SJohan Rudholm ocr &= ~R4_18V_PRESENT; 6110797e5f1SJohan Rudholm } 6120797e5f1SJohan Rudholm 61317d33e14SNicolas Pitre /* 61417d33e14SNicolas Pitre * Inform the card of the voltage 61517d33e14SNicolas Pitre */ 6163bca4cf7SChris Ball if (!powered_resume) { 617db4a0d05SUlf Hansson err = mmc_send_io_op_cond(host, ocr, &rocr); 61817d33e14SNicolas Pitre if (err) 61917d33e14SNicolas Pitre goto err; 6203bca4cf7SChris Ball } 62117d33e14SNicolas Pitre 62217d33e14SNicolas Pitre /* 62317d33e14SNicolas Pitre * For SPI, enable CRC as appropriate. 62417d33e14SNicolas Pitre */ 62517d33e14SNicolas Pitre if (mmc_host_is_spi(host)) { 62617d33e14SNicolas Pitre err = mmc_spi_set_crc(host, use_spi_crc); 62717d33e14SNicolas Pitre if (err) 62817d33e14SNicolas Pitre goto err; 62917d33e14SNicolas Pitre } 63017d33e14SNicolas Pitre 63117d33e14SNicolas Pitre /* 63217d33e14SNicolas Pitre * Allocate card structure. 63317d33e14SNicolas Pitre */ 63417d33e14SNicolas Pitre card = mmc_alloc_card(host, NULL); 63517d33e14SNicolas Pitre if (IS_ERR(card)) { 63617d33e14SNicolas Pitre err = PTR_ERR(card); 63717d33e14SNicolas Pitre goto err; 63817d33e14SNicolas Pitre } 63917d33e14SNicolas Pitre 640db4a0d05SUlf Hansson if ((rocr & R4_MEMORY_PRESENT) && 641db4a0d05SUlf Hansson mmc_sd_get_cid(host, ocr & rocr, card->raw_cid, NULL) == 0) { 6427310ece8SMichal Miroslaw card->type = MMC_TYPE_SD_COMBO; 6437310ece8SMichal Miroslaw 6447310ece8SMichal Miroslaw if (oldcard && (oldcard->type != MMC_TYPE_SD_COMBO || 6457310ece8SMichal Miroslaw memcmp(card->raw_cid, oldcard->raw_cid, sizeof(card->raw_cid)) != 0)) { 6467310ece8SMichal Miroslaw mmc_remove_card(card); 6477310ece8SMichal Miroslaw return -ENOENT; 6487310ece8SMichal Miroslaw } 6497310ece8SMichal Miroslaw } else { 65017d33e14SNicolas Pitre card->type = MMC_TYPE_SDIO; 65117d33e14SNicolas Pitre 6527310ece8SMichal Miroslaw if (oldcard && oldcard->type != MMC_TYPE_SDIO) { 6537310ece8SMichal Miroslaw mmc_remove_card(card); 6547310ece8SMichal Miroslaw return -ENOENT; 6557310ece8SMichal Miroslaw } 6567310ece8SMichal Miroslaw } 6577310ece8SMichal Miroslaw 65817d33e14SNicolas Pitre /* 6593fcb027dSDaniel Mack * Call the optional HC's init_card function to handle quirks. 6603fcb027dSDaniel Mack */ 6613fcb027dSDaniel Mack if (host->ops->init_card) 6623fcb027dSDaniel Mack host->ops->init_card(host, card); 6633fcb027dSDaniel Mack 6643fcb027dSDaniel Mack /* 665a303c531SPhilip Rakity * If the host and card support UHS-I mode request the card 666a303c531SPhilip Rakity * to switch to 1.8V signaling level. No 1.8v signalling if 6677122bbb0SMasanari Iida * UHS mode is not enabled to maintain compatibility and some 668a303c531SPhilip Rakity * systems that claim 1.8v signalling in fact do not support 669a303c531SPhilip Rakity * it. 670a303c531SPhilip Rakity */ 671db4a0d05SUlf Hansson if (!powered_resume && (rocr & ocr & R4_18V_PRESENT)) { 6720f791fdaSUlf Hansson err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_180, 673db4a0d05SUlf Hansson ocr); 6740797e5f1SJohan Rudholm if (err == -EAGAIN) { 6750797e5f1SJohan Rudholm sdio_reset(host); 6760797e5f1SJohan Rudholm mmc_go_idle(host); 6770797e5f1SJohan Rudholm mmc_send_if_cond(host, host->ocr_avail); 6780797e5f1SJohan Rudholm mmc_remove_card(card); 6790797e5f1SJohan Rudholm retries--; 6800797e5f1SJohan Rudholm goto try_again; 6810797e5f1SJohan Rudholm } else if (err) { 682a303c531SPhilip Rakity ocr &= ~R4_18V_PRESENT; 683a303c531SPhilip Rakity } 684a303c531SPhilip Rakity err = 0; 685a303c531SPhilip Rakity } else { 686a303c531SPhilip Rakity ocr &= ~R4_18V_PRESENT; 687a303c531SPhilip Rakity } 688a303c531SPhilip Rakity 689a303c531SPhilip Rakity /* 69017d33e14SNicolas Pitre * For native busses: set card RCA and quit open drain mode. 69117d33e14SNicolas Pitre */ 6923bca4cf7SChris Ball if (!powered_resume && !mmc_host_is_spi(host)) { 69317d33e14SNicolas Pitre err = mmc_send_relative_addr(host, &card->rca); 69417d33e14SNicolas Pitre if (err) 69517d33e14SNicolas Pitre goto remove; 69617d33e14SNicolas Pitre 6970aab3995SStefan Nilsson XK /* 6980aab3995SStefan Nilsson XK * Update oldcard with the new RCA received from the SDIO 6990aab3995SStefan Nilsson XK * device -- we're doing this so that it's updated in the 7000aab3995SStefan Nilsson XK * "card" struct when oldcard overwrites that later. 7010aab3995SStefan Nilsson XK */ 7020aab3995SStefan Nilsson XK if (oldcard) 7030aab3995SStefan Nilsson XK oldcard->rca = card->rca; 70417d33e14SNicolas Pitre } 70517d33e14SNicolas Pitre 70617d33e14SNicolas Pitre /* 7077310ece8SMichal Miroslaw * Read CSD, before selecting the card 7087310ece8SMichal Miroslaw */ 7097310ece8SMichal Miroslaw if (!oldcard && card->type == MMC_TYPE_SD_COMBO) { 7107310ece8SMichal Miroslaw err = mmc_sd_get_csd(host, card); 7117310ece8SMichal Miroslaw if (err) 7127310ece8SMichal Miroslaw return err; 7137310ece8SMichal Miroslaw 7147310ece8SMichal Miroslaw mmc_decode_cid(card); 7157310ece8SMichal Miroslaw } 7167310ece8SMichal Miroslaw 7177310ece8SMichal Miroslaw /* 71817d33e14SNicolas Pitre * Select card, as all following commands rely on that. 71917d33e14SNicolas Pitre */ 7203bca4cf7SChris Ball if (!powered_resume && !mmc_host_is_spi(host)) { 72117d33e14SNicolas Pitre err = mmc_select_card(card); 72217d33e14SNicolas Pitre if (err) 72317d33e14SNicolas Pitre goto remove; 72417d33e14SNicolas Pitre } 72517d33e14SNicolas Pitre 7266f51be3dSGrazvydas Ignotas if (card->quirks & MMC_QUIRK_NONSTD_SDIO) { 7276f51be3dSGrazvydas Ignotas /* 7286f51be3dSGrazvydas Ignotas * This is non-standard SDIO device, meaning it doesn't 7296f51be3dSGrazvydas Ignotas * have any CIA (Common I/O area) registers present. 7306f51be3dSGrazvydas Ignotas * It's host's responsibility to fill cccr and cis 7316f51be3dSGrazvydas Ignotas * structures in init_card(). 7326f51be3dSGrazvydas Ignotas */ 7336f51be3dSGrazvydas Ignotas mmc_set_clock(host, card->cis.max_dtr); 7346f51be3dSGrazvydas Ignotas 7356f51be3dSGrazvydas Ignotas if (card->cccr.high_speed) { 7366f51be3dSGrazvydas Ignotas mmc_card_set_highspeed(card); 7376f51be3dSGrazvydas Ignotas mmc_set_timing(card->host, MMC_TIMING_SD_HS); 7386f51be3dSGrazvydas Ignotas } 7396f51be3dSGrazvydas Ignotas 7406f51be3dSGrazvydas Ignotas goto finish; 7416f51be3dSGrazvydas Ignotas } 7426f51be3dSGrazvydas Ignotas 74317d33e14SNicolas Pitre /* 74417d33e14SNicolas Pitre * Read the common registers. 74517d33e14SNicolas Pitre */ 7462d0d68f5SPhilip Rakity err = sdio_read_cccr(card, ocr); 74717d33e14SNicolas Pitre if (err) 74817d33e14SNicolas Pitre goto remove; 74917d33e14SNicolas Pitre 75017d33e14SNicolas Pitre /* 75117d33e14SNicolas Pitre * Read the common CIS tuples. 75217d33e14SNicolas Pitre */ 75317d33e14SNicolas Pitre err = sdio_read_common_cis(card); 75417d33e14SNicolas Pitre if (err) 75517d33e14SNicolas Pitre goto remove; 75617d33e14SNicolas Pitre 75717d33e14SNicolas Pitre if (oldcard) { 75817d33e14SNicolas Pitre int same = (card->cis.vendor == oldcard->cis.vendor && 75917d33e14SNicolas Pitre card->cis.device == oldcard->cis.device); 76017d33e14SNicolas Pitre mmc_remove_card(card); 7617310ece8SMichal Miroslaw if (!same) 7627310ece8SMichal Miroslaw return -ENOENT; 7637310ece8SMichal Miroslaw 76417d33e14SNicolas Pitre card = oldcard; 76517d33e14SNicolas Pitre } 76669041150SUlf Hansson card->ocr = ocr_card; 76732780cd1SAndrei Warkentin mmc_fixup_device(card, NULL); 76817d33e14SNicolas Pitre 7697310ece8SMichal Miroslaw if (card->type == MMC_TYPE_SD_COMBO) { 7707310ece8SMichal Miroslaw err = mmc_sd_setup_card(host, card, oldcard != NULL); 7717310ece8SMichal Miroslaw /* handle as SDIO-only card if memory init failed */ 7727310ece8SMichal Miroslaw if (err) { 7737310ece8SMichal Miroslaw mmc_go_idle(host); 7747310ece8SMichal Miroslaw if (mmc_host_is_spi(host)) 7757310ece8SMichal Miroslaw /* should not fail, as it worked previously */ 7767310ece8SMichal Miroslaw mmc_spi_set_crc(host, use_spi_crc); 7777310ece8SMichal Miroslaw card->type = MMC_TYPE_SDIO; 7787310ece8SMichal Miroslaw } else 7797310ece8SMichal Miroslaw card->dev.type = &sd_type; 7807310ece8SMichal Miroslaw } 7817310ece8SMichal Miroslaw 7827310ece8SMichal Miroslaw /* 7837310ece8SMichal Miroslaw * If needed, disconnect card detection pull-up resistor. 7847310ece8SMichal Miroslaw */ 7857310ece8SMichal Miroslaw err = sdio_disable_cd(card); 7867310ece8SMichal Miroslaw if (err) 7877310ece8SMichal Miroslaw goto remove; 7887310ece8SMichal Miroslaw 789a303c531SPhilip Rakity /* Initialization sequence for UHS-I cards */ 790a303c531SPhilip Rakity /* Only if card supports 1.8v and UHS signaling */ 791a303c531SPhilip Rakity if ((ocr & R4_18V_PRESENT) && card->sw_caps.sd3_bus_mode) { 792a303c531SPhilip Rakity err = mmc_sdio_init_uhs_card(card); 793a303c531SPhilip Rakity if (err) 794a303c531SPhilip Rakity goto remove; 795a303c531SPhilip Rakity 796a303c531SPhilip Rakity /* Card is an ultra-high-speed card */ 797a303c531SPhilip Rakity mmc_card_set_uhs(card); 798a303c531SPhilip Rakity } else { 79917d33e14SNicolas Pitre /* 80017d33e14SNicolas Pitre * Switch to high-speed (if supported). 80117d33e14SNicolas Pitre */ 80217d33e14SNicolas Pitre err = sdio_enable_hs(card); 80371578a1eSMichal Miroslaw if (err > 0) 80471578a1eSMichal Miroslaw mmc_sd_go_highspeed(card); 80571578a1eSMichal Miroslaw else if (err) 80617d33e14SNicolas Pitre goto remove; 80717d33e14SNicolas Pitre 80817d33e14SNicolas Pitre /* 80917d33e14SNicolas Pitre * Change to the card's maximum speed. 81017d33e14SNicolas Pitre */ 81171578a1eSMichal Miroslaw mmc_set_clock(host, mmc_sdio_get_max_clock(card)); 81217d33e14SNicolas Pitre 81317d33e14SNicolas Pitre /* 81417d33e14SNicolas Pitre * Switch to wider bus (if supported). 81517d33e14SNicolas Pitre */ 8167310ece8SMichal Miroslaw err = sdio_enable_4bit_bus(card); 8177310ece8SMichal Miroslaw if (err > 0) 8187310ece8SMichal Miroslaw mmc_set_bus_width(card->host, MMC_BUS_WIDTH_4); 8197310ece8SMichal Miroslaw else if (err) 82017d33e14SNicolas Pitre goto remove; 821a303c531SPhilip Rakity } 8226f51be3dSGrazvydas Ignotas finish: 82317d33e14SNicolas Pitre if (!oldcard) 82417d33e14SNicolas Pitre host->card = card; 82517d33e14SNicolas Pitre return 0; 82617d33e14SNicolas Pitre 82717d33e14SNicolas Pitre remove: 82817d33e14SNicolas Pitre if (!oldcard) 82917d33e14SNicolas Pitre mmc_remove_card(card); 83017d33e14SNicolas Pitre 83117d33e14SNicolas Pitre err: 83217d33e14SNicolas Pitre return err; 83317d33e14SNicolas Pitre } 83417d33e14SNicolas Pitre 83517d33e14SNicolas Pitre /* 8365c4e6f13SPierre Ossman * Host is being removed. Free up the current card. 8375c4e6f13SPierre Ossman */ 8385c4e6f13SPierre Ossman static void mmc_sdio_remove(struct mmc_host *host) 8395c4e6f13SPierre Ossman { 840e29a7d73SPierre Ossman int i; 841e29a7d73SPierre Ossman 8425c4e6f13SPierre Ossman BUG_ON(!host); 8435c4e6f13SPierre Ossman BUG_ON(!host->card); 8445c4e6f13SPierre Ossman 845e29a7d73SPierre Ossman for (i = 0;i < host->card->sdio_funcs;i++) { 846e29a7d73SPierre Ossman if (host->card->sdio_func[i]) { 847e29a7d73SPierre Ossman sdio_remove_func(host->card->sdio_func[i]); 848e29a7d73SPierre Ossman host->card->sdio_func[i] = NULL; 849e29a7d73SPierre Ossman } 850e29a7d73SPierre Ossman } 851e29a7d73SPierre Ossman 8525c4e6f13SPierre Ossman mmc_remove_card(host->card); 8535c4e6f13SPierre Ossman host->card = NULL; 8545c4e6f13SPierre Ossman } 8555c4e6f13SPierre Ossman 8565c4e6f13SPierre Ossman /* 857d3049504SAdrian Hunter * Card detection - card is alive. 858d3049504SAdrian Hunter */ 859d3049504SAdrian Hunter static int mmc_sdio_alive(struct mmc_host *host) 860d3049504SAdrian Hunter { 861d3049504SAdrian Hunter return mmc_select_card(host->card); 862d3049504SAdrian Hunter } 863d3049504SAdrian Hunter 864d3049504SAdrian Hunter /* 8655c4e6f13SPierre Ossman * Card detection callback from host. 8665c4e6f13SPierre Ossman */ 8675c4e6f13SPierre Ossman static void mmc_sdio_detect(struct mmc_host *host) 8685c4e6f13SPierre Ossman { 8695c4e6f13SPierre Ossman int err; 8705c4e6f13SPierre Ossman 8715c4e6f13SPierre Ossman BUG_ON(!host); 8725c4e6f13SPierre Ossman BUG_ON(!host->card); 8735c4e6f13SPierre Ossman 87487973ba2SOhad Ben-Cohen /* Make sure card is powered before detecting it */ 875ed919b01SOhad Ben-Cohen if (host->caps & MMC_CAP_POWER_OFF_CARD) { 87687973ba2SOhad Ben-Cohen err = pm_runtime_get_sync(&host->card->dev); 8773bffb800SLi Fei if (err < 0) { 8783bffb800SLi Fei pm_runtime_put_noidle(&host->card->dev); 87987973ba2SOhad Ben-Cohen goto out; 880ed919b01SOhad Ben-Cohen } 8813bffb800SLi Fei } 88287973ba2SOhad Ben-Cohen 8835c4e6f13SPierre Ossman mmc_claim_host(host); 8845c4e6f13SPierre Ossman 8855c4e6f13SPierre Ossman /* 8865c4e6f13SPierre Ossman * Just check if our card has been removed. 8875c4e6f13SPierre Ossman */ 888d3049504SAdrian Hunter err = _mmc_detect_card_removed(host); 8895c4e6f13SPierre Ossman 8905c4e6f13SPierre Ossman mmc_release_host(host); 8915c4e6f13SPierre Ossman 8924d0812c3SOhad Ben-Cohen /* 8934d0812c3SOhad Ben-Cohen * Tell PM core it's OK to power off the card now. 8944d0812c3SOhad Ben-Cohen * 8954d0812c3SOhad Ben-Cohen * The _sync variant is used in order to ensure that the card 8964d0812c3SOhad Ben-Cohen * is left powered off in case an error occurred, and the card 8974d0812c3SOhad Ben-Cohen * is going to be removed. 8984d0812c3SOhad Ben-Cohen * 8994d0812c3SOhad Ben-Cohen * Since there is no specific reason to believe a new user 9004d0812c3SOhad Ben-Cohen * is about to show up at this point, the _sync variant is 9014d0812c3SOhad Ben-Cohen * desirable anyway. 9024d0812c3SOhad Ben-Cohen */ 903ed919b01SOhad Ben-Cohen if (host->caps & MMC_CAP_POWER_OFF_CARD) 9044d0812c3SOhad Ben-Cohen pm_runtime_put_sync(&host->card->dev); 9054d0812c3SOhad Ben-Cohen 90687973ba2SOhad Ben-Cohen out: 9075c4e6f13SPierre Ossman if (err) { 9085c4e6f13SPierre Ossman mmc_sdio_remove(host); 9095c4e6f13SPierre Ossman 9105c4e6f13SPierre Ossman mmc_claim_host(host); 9115c4e6f13SPierre Ossman mmc_detach_bus(host); 9127f7e4129SUlf Hansson mmc_power_off(host); 9135c4e6f13SPierre Ossman mmc_release_host(host); 9145c4e6f13SPierre Ossman } 9155c4e6f13SPierre Ossman } 9165c4e6f13SPierre Ossman 91717d33e14SNicolas Pitre /* 918810caddbSUlf Hansson * SDIO pre_suspend. We need to suspend all functions separately. 91917d33e14SNicolas Pitre * Therefore all registered functions must have drivers with suspend 92017d33e14SNicolas Pitre * and resume methods. Failing that we simply remove the whole card. 92117d33e14SNicolas Pitre */ 922810caddbSUlf Hansson static int mmc_sdio_pre_suspend(struct mmc_host *host) 92317d33e14SNicolas Pitre { 92495cdfb72SNicolas Pitre int i, err = 0; 92517d33e14SNicolas Pitre 92617d33e14SNicolas Pitre for (i = 0; i < host->card->sdio_funcs; i++) { 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 if (!pmops || !pmops->suspend || !pmops->resume) { 93195cdfb72SNicolas Pitre /* force removal of entire card in that case */ 93295cdfb72SNicolas Pitre err = -ENOSYS; 933810caddbSUlf Hansson break; 934810caddbSUlf Hansson } 935810caddbSUlf Hansson } 936810caddbSUlf Hansson } 937810caddbSUlf Hansson 938810caddbSUlf Hansson return err; 939810caddbSUlf Hansson } 940810caddbSUlf Hansson 941810caddbSUlf Hansson /* 942810caddbSUlf Hansson * SDIO suspend. Suspend all functions separately. 943810caddbSUlf Hansson */ 944810caddbSUlf Hansson static int mmc_sdio_suspend(struct mmc_host *host) 945810caddbSUlf Hansson { 946810caddbSUlf Hansson int i, err = 0; 947810caddbSUlf Hansson 948810caddbSUlf Hansson for (i = 0; i < host->card->sdio_funcs; i++) { 949810caddbSUlf Hansson struct sdio_func *func = host->card->sdio_func[i]; 950810caddbSUlf Hansson if (func && sdio_func_present(func) && func->dev.driver) { 951810caddbSUlf Hansson const struct dev_pm_ops *pmops = func->dev.driver->pm; 95295cdfb72SNicolas Pitre err = pmops->suspend(&func->dev); 95395cdfb72SNicolas Pitre if (err) 95495cdfb72SNicolas Pitre break; 95517d33e14SNicolas Pitre } 95617d33e14SNicolas Pitre } 95795cdfb72SNicolas Pitre while (err && --i >= 0) { 95817d33e14SNicolas Pitre struct sdio_func *func = host->card->sdio_func[i]; 95917d33e14SNicolas Pitre if (func && sdio_func_present(func) && func->dev.driver) { 96017d33e14SNicolas Pitre const struct dev_pm_ops *pmops = func->dev.driver->pm; 96117d33e14SNicolas Pitre pmops->resume(&func->dev); 96217d33e14SNicolas Pitre } 96317d33e14SNicolas Pitre } 96495cdfb72SNicolas Pitre 9656b93d01fSOhad Ben-Cohen if (!err && mmc_card_keep_power(host) && mmc_card_wake_sdio_irq(host)) { 9666b5eda36SDaniel Drake mmc_claim_host(host); 9676b5eda36SDaniel Drake sdio_disable_wide(host->card); 9686b5eda36SDaniel Drake mmc_release_host(host); 9696b5eda36SDaniel Drake } 9706b5eda36SDaniel Drake 97174590263SUlf Hansson if (!err && !mmc_card_keep_power(host)) 97274590263SUlf Hansson mmc_power_off(host); 97374590263SUlf Hansson 97495cdfb72SNicolas Pitre return err; 97595cdfb72SNicolas Pitre } 97695cdfb72SNicolas Pitre 97795cdfb72SNicolas Pitre static int mmc_sdio_resume(struct mmc_host *host) 97895cdfb72SNicolas Pitre { 979080bc977SOhad Ben-Cohen int i, err = 0; 98095cdfb72SNicolas Pitre 98195cdfb72SNicolas Pitre BUG_ON(!host); 98295cdfb72SNicolas Pitre BUG_ON(!host->card); 98395cdfb72SNicolas Pitre 98495cdfb72SNicolas Pitre /* Basic card reinitialization. */ 98595cdfb72SNicolas Pitre mmc_claim_host(host); 986080bc977SOhad Ben-Cohen 98774590263SUlf Hansson /* Restore power if needed */ 98874590263SUlf Hansson if (!mmc_card_keep_power(host)) { 98969041150SUlf Hansson mmc_power_up(host, host->card->ocr); 99074590263SUlf Hansson /* 99174590263SUlf Hansson * Tell runtime PM core we just powered up the card, 99274590263SUlf Hansson * since it still believes the card is powered off. 99374590263SUlf Hansson * Note that currently runtime PM is only enabled 99474590263SUlf Hansson * for SDIO cards that are MMC_CAP_POWER_OFF_CARD 99574590263SUlf Hansson */ 99674590263SUlf Hansson if (host->caps & MMC_CAP_POWER_OFF_CARD) { 99774590263SUlf Hansson pm_runtime_disable(&host->card->dev); 99874590263SUlf Hansson pm_runtime_set_active(&host->card->dev); 99974590263SUlf Hansson pm_runtime_enable(&host->card->dev); 100074590263SUlf Hansson } 100174590263SUlf Hansson } 100274590263SUlf Hansson 1003080bc977SOhad Ben-Cohen /* No need to reinitialize powered-resumed nonremovable cards */ 100451aa66a5SSubhash Jadavani if (mmc_card_is_removable(host) || !mmc_card_keep_power(host)) { 100551aa66a5SSubhash Jadavani sdio_reset(host); 100651aa66a5SSubhash Jadavani mmc_go_idle(host); 100769041150SUlf Hansson err = mmc_sdio_init_card(host, host->card->ocr, host->card, 1008a5e9425dSOhad Ben-Cohen mmc_card_keep_power(host)); 100951aa66a5SSubhash Jadavani } else if (mmc_card_keep_power(host) && mmc_card_wake_sdio_irq(host)) { 1010080bc977SOhad Ben-Cohen /* We may have switched to 1-bit mode during suspend */ 1011080bc977SOhad Ben-Cohen err = sdio_enable_4bit_bus(host->card); 1012080bc977SOhad Ben-Cohen if (err > 0) { 1013080bc977SOhad Ben-Cohen mmc_set_bus_width(host, MMC_BUS_WIDTH_4); 1014080bc977SOhad Ben-Cohen err = 0; 1015080bc977SOhad Ben-Cohen } 1016080bc977SOhad Ben-Cohen } 1017080bc977SOhad Ben-Cohen 101840216842SNicolas Pitre if (!err && host->sdio_irqs) 1019bbbc4c4dSNicolas Pitre wake_up_process(host->sdio_irq_thread); 102095cdfb72SNicolas Pitre mmc_release_host(host); 102195cdfb72SNicolas Pitre 102295cdfb72SNicolas Pitre /* 102395cdfb72SNicolas Pitre * If the card looked to be the same as before suspending, then 102495cdfb72SNicolas Pitre * we proceed to resume all card functions. If one of them returns 102595cdfb72SNicolas Pitre * an error then we simply return that error to the core and the 102695cdfb72SNicolas Pitre * card will be redetected as new. It is the responsibility of 102795cdfb72SNicolas Pitre * the function driver to perform further tests with the extra 102895cdfb72SNicolas Pitre * knowledge it has of the card to confirm the card is indeed the 102995cdfb72SNicolas Pitre * same as before suspending (same MAC address for network cards, 103095cdfb72SNicolas Pitre * etc.) and return an error otherwise. 103195cdfb72SNicolas Pitre */ 103295cdfb72SNicolas Pitre for (i = 0; !err && i < host->card->sdio_funcs; i++) { 103395cdfb72SNicolas Pitre struct sdio_func *func = host->card->sdio_func[i]; 103495cdfb72SNicolas Pitre if (func && sdio_func_present(func) && func->dev.driver) { 103595cdfb72SNicolas Pitre const struct dev_pm_ops *pmops = func->dev.driver->pm; 103695cdfb72SNicolas Pitre err = pmops->resume(&func->dev); 103795cdfb72SNicolas Pitre } 103895cdfb72SNicolas Pitre } 103995cdfb72SNicolas Pitre 104074590263SUlf Hansson host->pm_flags &= ~MMC_PM_KEEP_POWER; 104195cdfb72SNicolas Pitre return err; 104217d33e14SNicolas Pitre } 10435c4e6f13SPierre Ossman 1044d3fe37b1SOhad Ben-Cohen static int mmc_sdio_power_restore(struct mmc_host *host) 1045d3fe37b1SOhad Ben-Cohen { 1046d3fe37b1SOhad Ben-Cohen int ret; 1047d3fe37b1SOhad Ben-Cohen 1048d3fe37b1SOhad Ben-Cohen BUG_ON(!host); 1049d3fe37b1SOhad Ben-Cohen BUG_ON(!host->card); 1050d3fe37b1SOhad Ben-Cohen 1051d3fe37b1SOhad Ben-Cohen mmc_claim_host(host); 1052c6e633adSDaniel Drake 1053c6e633adSDaniel Drake /* 1054c6e633adSDaniel Drake * Reset the card by performing the same steps that are taken by 1055c6e633adSDaniel Drake * mmc_rescan_try_freq() and mmc_attach_sdio() during a "normal" probe. 1056c6e633adSDaniel Drake * 1057c6e633adSDaniel Drake * sdio_reset() is technically not needed. Having just powered up the 1058c6e633adSDaniel Drake * hardware, it should already be in reset state. However, some 1059c6e633adSDaniel Drake * platforms (such as SD8686 on OLPC) do not instantly cut power, 1060c6e633adSDaniel Drake * meaning that a reset is required when restoring power soon after 1061c6e633adSDaniel Drake * powering off. It is harmless in other cases. 1062c6e633adSDaniel Drake * 1063c6e633adSDaniel Drake * The CMD5 reset (mmc_send_io_op_cond()), according to the SDIO spec, 1064c6e633adSDaniel Drake * is not necessary for non-removable cards. However, it is required 1065c6e633adSDaniel Drake * for OLPC SD8686 (which expects a [CMD5,5,3,7] init sequence), and 1066c6e633adSDaniel Drake * harmless in other situations. 1067c6e633adSDaniel Drake * 1068c6e633adSDaniel Drake */ 1069e7747475SUlf Hansson 1070c6e633adSDaniel Drake sdio_reset(host); 1071c6e633adSDaniel Drake mmc_go_idle(host); 1072c6e633adSDaniel Drake mmc_send_if_cond(host, host->ocr_avail); 1073c6e633adSDaniel Drake 1074*ce69d37bSUlf Hansson ret = mmc_send_io_op_cond(host, 0, NULL); 1075c6e633adSDaniel Drake if (ret) 1076c6e633adSDaniel Drake goto out; 1077c6e633adSDaniel Drake 1078*ce69d37bSUlf Hansson ret = mmc_sdio_init_card(host, host->card->ocr, host->card, 1079a5e9425dSOhad Ben-Cohen mmc_card_keep_power(host)); 1080d3fe37b1SOhad Ben-Cohen if (!ret && host->sdio_irqs) 1081d3fe37b1SOhad Ben-Cohen mmc_signal_sdio_irq(host); 1082c6e633adSDaniel Drake 1083c6e633adSDaniel Drake out: 1084d3fe37b1SOhad Ben-Cohen mmc_release_host(host); 1085d3fe37b1SOhad Ben-Cohen 1086d3fe37b1SOhad Ben-Cohen return ret; 1087d3fe37b1SOhad Ben-Cohen } 1088d3fe37b1SOhad Ben-Cohen 108912d01d0bSUlf Hansson static int mmc_sdio_runtime_suspend(struct mmc_host *host) 109012d01d0bSUlf Hansson { 109112d01d0bSUlf Hansson /* No references to the card, cut the power to it. */ 109212d01d0bSUlf Hansson mmc_power_off(host); 109312d01d0bSUlf Hansson return 0; 109412d01d0bSUlf Hansson } 109512d01d0bSUlf Hansson 109612d01d0bSUlf Hansson static int mmc_sdio_runtime_resume(struct mmc_host *host) 109712d01d0bSUlf Hansson { 109812d01d0bSUlf Hansson /* Restore power and re-initialize. */ 109969041150SUlf Hansson mmc_power_up(host, host->card->ocr); 110012d01d0bSUlf Hansson return mmc_sdio_power_restore(host); 110112d01d0bSUlf Hansson } 110212d01d0bSUlf Hansson 11035c4e6f13SPierre Ossman static const struct mmc_bus_ops mmc_sdio_ops = { 11045c4e6f13SPierre Ossman .remove = mmc_sdio_remove, 11055c4e6f13SPierre Ossman .detect = mmc_sdio_detect, 1106810caddbSUlf Hansson .pre_suspend = mmc_sdio_pre_suspend, 110717d33e14SNicolas Pitre .suspend = mmc_sdio_suspend, 110817d33e14SNicolas Pitre .resume = mmc_sdio_resume, 110912d01d0bSUlf Hansson .runtime_suspend = mmc_sdio_runtime_suspend, 111012d01d0bSUlf Hansson .runtime_resume = mmc_sdio_runtime_resume, 1111d3fe37b1SOhad Ben-Cohen .power_restore = mmc_sdio_power_restore, 1112d3049504SAdrian Hunter .alive = mmc_sdio_alive, 11135c4e6f13SPierre Ossman }; 11145c4e6f13SPierre Ossman 11155c4e6f13SPierre Ossman 11165c4e6f13SPierre Ossman /* 11175c4e6f13SPierre Ossman * Starting point for SDIO card init. 11185c4e6f13SPierre Ossman */ 1119807e8e40SAndy Ross int mmc_attach_sdio(struct mmc_host *host) 11205c4e6f13SPierre Ossman { 1121807e8e40SAndy Ross int err, i, funcs; 112269041150SUlf Hansson u32 ocr, rocr; 11235c4e6f13SPierre Ossman struct mmc_card *card; 11245c4e6f13SPierre Ossman 11255c4e6f13SPierre Ossman BUG_ON(!host); 1126d84075c8SPierre Ossman WARN_ON(!host->claimed); 11275c4e6f13SPierre Ossman 1128807e8e40SAndy Ross err = mmc_send_io_op_cond(host, 0, &ocr); 1129807e8e40SAndy Ross if (err) 1130807e8e40SAndy Ross return err; 1131807e8e40SAndy Ross 11325c4e6f13SPierre Ossman mmc_attach_bus(host, &mmc_sdio_ops); 11338f230f45STakashi Iwai if (host->ocr_avail_sdio) 11348f230f45STakashi Iwai host->ocr_avail = host->ocr_avail_sdio; 11355c4e6f13SPierre Ossman 11365c4e6f13SPierre Ossman /* 11375c4e6f13SPierre Ossman * Sanity check the voltages that the card claims to 11385c4e6f13SPierre Ossman * support. 11395c4e6f13SPierre Ossman */ 11405c4e6f13SPierre Ossman if (ocr & 0x7F) { 1141a3c76eb9SGirish K S pr_warning("%s: card claims to support voltages " 11425c4e6f13SPierre Ossman "below the defined range. These will be ignored.\n", 11435c4e6f13SPierre Ossman mmc_hostname(host)); 11445c4e6f13SPierre Ossman ocr &= ~0x7F; 11455c4e6f13SPierre Ossman } 11465c4e6f13SPierre Ossman 114769041150SUlf Hansson rocr = mmc_select_voltage(host, ocr); 11485c4e6f13SPierre Ossman 11495c4e6f13SPierre Ossman /* 11505c4e6f13SPierre Ossman * Can we support the voltage(s) of the card(s)? 11515c4e6f13SPierre Ossman */ 115269041150SUlf Hansson if (!rocr) { 11535c4e6f13SPierre Ossman err = -EINVAL; 11545c4e6f13SPierre Ossman goto err; 11555c4e6f13SPierre Ossman } 11565c4e6f13SPierre Ossman 11575c4e6f13SPierre Ossman /* 115817d33e14SNicolas Pitre * Detect and init the card. 11595c4e6f13SPierre Ossman */ 116069041150SUlf Hansson err = mmc_sdio_init_card(host, rocr, NULL, 0); 11615c4e6f13SPierre Ossman if (err) 11625c4e6f13SPierre Ossman goto err; 1163ec2ed700SUlf Hansson 116417d33e14SNicolas Pitre card = host->card; 1165af517150SDavid Brownell 1166af517150SDavid Brownell /* 1167ed919b01SOhad Ben-Cohen * Enable runtime PM only if supported by host+card+board 1168ed919b01SOhad Ben-Cohen */ 1169ed919b01SOhad Ben-Cohen if (host->caps & MMC_CAP_POWER_OFF_CARD) { 1170ed919b01SOhad Ben-Cohen /* 117181968561SOhad Ben-Cohen * Let runtime PM core know our card is active 117281968561SOhad Ben-Cohen */ 117381968561SOhad Ben-Cohen err = pm_runtime_set_active(&card->dev); 117481968561SOhad Ben-Cohen if (err) 117581968561SOhad Ben-Cohen goto remove; 117681968561SOhad Ben-Cohen 117781968561SOhad Ben-Cohen /* 117881968561SOhad Ben-Cohen * Enable runtime PM for this card 117981968561SOhad Ben-Cohen */ 118081968561SOhad Ben-Cohen pm_runtime_enable(&card->dev); 1181ed919b01SOhad Ben-Cohen } 118281968561SOhad Ben-Cohen 118381968561SOhad Ben-Cohen /* 11845c4e6f13SPierre Ossman * The number of functions on the card is encoded inside 11855c4e6f13SPierre Ossman * the ocr. 11865c4e6f13SPierre Ossman */ 1187e8812793SMatt Fleming funcs = (ocr & 0x70000000) >> 28; 1188e8812793SMatt Fleming card->sdio_funcs = 0; 11894ff6471cSPierre Ossman 11904ff6471cSPierre Ossman /* 1191e29a7d73SPierre Ossman * Initialize (but don't add) all present functions. 1192e29a7d73SPierre Ossman */ 1193e8812793SMatt Fleming for (i = 0; i < funcs; i++, card->sdio_funcs++) { 1194e29a7d73SPierre Ossman err = sdio_init_func(host->card, i + 1); 1195e29a7d73SPierre Ossman if (err) 1196e29a7d73SPierre Ossman goto remove; 119740bba0c1SOhad Ben-Cohen 119840bba0c1SOhad Ben-Cohen /* 1199ed919b01SOhad Ben-Cohen * Enable Runtime PM for this func (if supported) 120040bba0c1SOhad Ben-Cohen */ 1201ed919b01SOhad Ben-Cohen if (host->caps & MMC_CAP_POWER_OFF_CARD) 120240bba0c1SOhad Ben-Cohen pm_runtime_enable(&card->sdio_func[i]->dev); 1203e29a7d73SPierre Ossman } 12045c4e6f13SPierre Ossman 1205e29a7d73SPierre Ossman /* 1206e29a7d73SPierre Ossman * First add the card to the driver model... 1207e29a7d73SPierre Ossman */ 1208807e8e40SAndy Ross mmc_release_host(host); 12095c4e6f13SPierre Ossman err = mmc_add_card(host->card); 12105c4e6f13SPierre Ossman if (err) 1211e29a7d73SPierre Ossman goto remove_added; 1212e29a7d73SPierre Ossman 1213e29a7d73SPierre Ossman /* 1214e29a7d73SPierre Ossman * ...then the SDIO functions. 1215e29a7d73SPierre Ossman */ 1216e29a7d73SPierre Ossman for (i = 0;i < funcs;i++) { 1217e29a7d73SPierre Ossman err = sdio_add_func(host->card->sdio_func[i]); 1218e29a7d73SPierre Ossman if (err) 1219e29a7d73SPierre Ossman goto remove_added; 1220e29a7d73SPierre Ossman } 12215c4e6f13SPierre Ossman 122234497913SDmitry Shmidt mmc_claim_host(host); 12235c4e6f13SPierre Ossman return 0; 12245c4e6f13SPierre Ossman 1225e29a7d73SPierre Ossman 1226e29a7d73SPierre Ossman remove_added: 1227e29a7d73SPierre Ossman /* Remove without lock if the device has been added. */ 1228e29a7d73SPierre Ossman mmc_sdio_remove(host); 12295c4e6f13SPierre Ossman mmc_claim_host(host); 1230e29a7d73SPierre Ossman remove: 1231e29a7d73SPierre Ossman /* And with lock if it hasn't been added. */ 1232807e8e40SAndy Ross mmc_release_host(host); 1233e29a7d73SPierre Ossman if (host->card) 1234e29a7d73SPierre Ossman mmc_sdio_remove(host); 1235807e8e40SAndy Ross mmc_claim_host(host); 12365c4e6f13SPierre Ossman err: 12375c4e6f13SPierre Ossman mmc_detach_bus(host); 12385c4e6f13SPierre Ossman 1239a3c76eb9SGirish K S pr_err("%s: error %d whilst initialising SDIO card\n", 12405c4e6f13SPierre Ossman mmc_hostname(host), err); 12415c4e6f13SPierre Ossman 12425c4e6f13SPierre Ossman return err; 12435c4e6f13SPierre Ossman } 12445c4e6f13SPierre Ossman 1245