Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  *  linux/drivers/mmc/sdio.c
0004  *
0005  *  Copyright 2006-2007 Pierre Ossman
0006  */
0007 
0008 #include <linux/err.h>
0009 #include <linux/pm_runtime.h>
0010 #include <linux/sysfs.h>
0011 
0012 #include <linux/mmc/host.h>
0013 #include <linux/mmc/card.h>
0014 #include <linux/mmc/mmc.h>
0015 #include <linux/mmc/sdio.h>
0016 #include <linux/mmc/sdio_func.h>
0017 #include <linux/mmc/sdio_ids.h>
0018 
0019 #include "core.h"
0020 #include "card.h"
0021 #include "host.h"
0022 #include "bus.h"
0023 #include "quirks.h"
0024 #include "sd.h"
0025 #include "sdio_bus.h"
0026 #include "mmc_ops.h"
0027 #include "sd_ops.h"
0028 #include "sdio_ops.h"
0029 #include "sdio_cis.h"
0030 
0031 MMC_DEV_ATTR(vendor, "0x%04x\n", card->cis.vendor);
0032 MMC_DEV_ATTR(device, "0x%04x\n", card->cis.device);
0033 MMC_DEV_ATTR(revision, "%u.%u\n", card->major_rev, card->minor_rev);
0034 MMC_DEV_ATTR(ocr, "0x%08x\n", card->ocr);
0035 MMC_DEV_ATTR(rca, "0x%04x\n", card->rca);
0036 
0037 #define sdio_info_attr(num)                                 \
0038 static ssize_t info##num##_show(struct device *dev, struct device_attribute *attr, char *buf)   \
0039 {                                               \
0040     struct mmc_card *card = mmc_dev_to_card(dev);                       \
0041                                                 \
0042     if (num > card->num_info)                               \
0043         return -ENODATA;                                \
0044     if (!card->info[num - 1][0])                                \
0045         return 0;                                   \
0046     return sysfs_emit(buf, "%s\n", card->info[num - 1]);                    \
0047 }                                               \
0048 static DEVICE_ATTR_RO(info##num)
0049 
0050 sdio_info_attr(1);
0051 sdio_info_attr(2);
0052 sdio_info_attr(3);
0053 sdio_info_attr(4);
0054 
0055 static struct attribute *sdio_std_attrs[] = {
0056     &dev_attr_vendor.attr,
0057     &dev_attr_device.attr,
0058     &dev_attr_revision.attr,
0059     &dev_attr_info1.attr,
0060     &dev_attr_info2.attr,
0061     &dev_attr_info3.attr,
0062     &dev_attr_info4.attr,
0063     &dev_attr_ocr.attr,
0064     &dev_attr_rca.attr,
0065     NULL,
0066 };
0067 ATTRIBUTE_GROUPS(sdio_std);
0068 
0069 static struct device_type sdio_type = {
0070     .groups = sdio_std_groups,
0071 };
0072 
0073 static int sdio_read_fbr(struct sdio_func *func)
0074 {
0075     int ret;
0076     unsigned char data;
0077 
0078     if (mmc_card_nonstd_func_interface(func->card)) {
0079         func->class = SDIO_CLASS_NONE;
0080         return 0;
0081     }
0082 
0083     ret = mmc_io_rw_direct(func->card, 0, 0,
0084         SDIO_FBR_BASE(func->num) + SDIO_FBR_STD_IF, 0, &data);
0085     if (ret)
0086         goto out;
0087 
0088     data &= 0x0f;
0089 
0090     if (data == 0x0f) {
0091         ret = mmc_io_rw_direct(func->card, 0, 0,
0092             SDIO_FBR_BASE(func->num) + SDIO_FBR_STD_IF_EXT, 0, &data);
0093         if (ret)
0094             goto out;
0095     }
0096 
0097     func->class = data;
0098 
0099 out:
0100     return ret;
0101 }
0102 
0103 static int sdio_init_func(struct mmc_card *card, unsigned int fn)
0104 {
0105     int ret;
0106     struct sdio_func *func;
0107 
0108     if (WARN_ON(fn > SDIO_MAX_FUNCS))
0109         return -EINVAL;
0110 
0111     func = sdio_alloc_func(card);
0112     if (IS_ERR(func))
0113         return PTR_ERR(func);
0114 
0115     func->num = fn;
0116 
0117     if (!(card->quirks & MMC_QUIRK_NONSTD_SDIO)) {
0118         ret = sdio_read_fbr(func);
0119         if (ret)
0120             goto fail;
0121 
0122         ret = sdio_read_func_cis(func);
0123         if (ret)
0124             goto fail;
0125     } else {
0126         func->vendor = func->card->cis.vendor;
0127         func->device = func->card->cis.device;
0128         func->max_blksize = func->card->cis.blksize;
0129     }
0130 
0131     card->sdio_func[fn - 1] = func;
0132 
0133     return 0;
0134 
0135 fail:
0136     /*
0137      * It is okay to remove the function here even though we hold
0138      * the host lock as we haven't registered the device yet.
0139      */
0140     sdio_remove_func(func);
0141     return ret;
0142 }
0143 
0144 static int sdio_read_cccr(struct mmc_card *card, u32 ocr)
0145 {
0146     int ret;
0147     int cccr_vsn;
0148     int uhs = ocr & R4_18V_PRESENT;
0149     unsigned char data;
0150     unsigned char speed;
0151 
0152     ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_CCCR, 0, &data);
0153     if (ret)
0154         goto out;
0155 
0156     cccr_vsn = data & 0x0f;
0157 
0158     if (cccr_vsn > SDIO_CCCR_REV_3_00) {
0159         pr_err("%s: unrecognised CCCR structure version %d\n",
0160             mmc_hostname(card->host), cccr_vsn);
0161         return -EINVAL;
0162     }
0163 
0164     card->cccr.sdio_vsn = (data & 0xf0) >> 4;
0165 
0166     ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_CAPS, 0, &data);
0167     if (ret)
0168         goto out;
0169 
0170     if (data & SDIO_CCCR_CAP_SMB)
0171         card->cccr.multi_block = 1;
0172     if (data & SDIO_CCCR_CAP_LSC)
0173         card->cccr.low_speed = 1;
0174     if (data & SDIO_CCCR_CAP_4BLS)
0175         card->cccr.wide_bus = 1;
0176 
0177     if (cccr_vsn >= SDIO_CCCR_REV_1_10) {
0178         ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_POWER, 0, &data);
0179         if (ret)
0180             goto out;
0181 
0182         if (data & SDIO_POWER_SMPC)
0183             card->cccr.high_power = 1;
0184     }
0185 
0186     if (cccr_vsn >= SDIO_CCCR_REV_1_20) {
0187         ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_SPEED, 0, &speed);
0188         if (ret)
0189             goto out;
0190 
0191         card->scr.sda_spec3 = 0;
0192         card->sw_caps.sd3_bus_mode = 0;
0193         card->sw_caps.sd3_drv_type = 0;
0194         if (cccr_vsn >= SDIO_CCCR_REV_3_00 && uhs) {
0195             card->scr.sda_spec3 = 1;
0196             ret = mmc_io_rw_direct(card, 0, 0,
0197                 SDIO_CCCR_UHS, 0, &data);
0198             if (ret)
0199                 goto out;
0200 
0201             if (mmc_host_uhs(card->host)) {
0202                 if (data & SDIO_UHS_DDR50)
0203                     card->sw_caps.sd3_bus_mode
0204                         |= SD_MODE_UHS_DDR50 | SD_MODE_UHS_SDR50
0205                             | SD_MODE_UHS_SDR25 | SD_MODE_UHS_SDR12;
0206 
0207                 if (data & SDIO_UHS_SDR50)
0208                     card->sw_caps.sd3_bus_mode
0209                         |= SD_MODE_UHS_SDR50 | SD_MODE_UHS_SDR25
0210                             | SD_MODE_UHS_SDR12;
0211 
0212                 if (data & SDIO_UHS_SDR104)
0213                     card->sw_caps.sd3_bus_mode
0214                         |= SD_MODE_UHS_SDR104 | SD_MODE_UHS_SDR50
0215                             | SD_MODE_UHS_SDR25 | SD_MODE_UHS_SDR12;
0216             }
0217 
0218             ret = mmc_io_rw_direct(card, 0, 0,
0219                 SDIO_CCCR_DRIVE_STRENGTH, 0, &data);
0220             if (ret)
0221                 goto out;
0222 
0223             if (data & SDIO_DRIVE_SDTA)
0224                 card->sw_caps.sd3_drv_type |= SD_DRIVER_TYPE_A;
0225             if (data & SDIO_DRIVE_SDTC)
0226                 card->sw_caps.sd3_drv_type |= SD_DRIVER_TYPE_C;
0227             if (data & SDIO_DRIVE_SDTD)
0228                 card->sw_caps.sd3_drv_type |= SD_DRIVER_TYPE_D;
0229 
0230             ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_INTERRUPT_EXT, 0, &data);
0231             if (ret)
0232                 goto out;
0233 
0234             if (data & SDIO_INTERRUPT_EXT_SAI) {
0235                 data |= SDIO_INTERRUPT_EXT_EAI;
0236                 ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_INTERRUPT_EXT,
0237                                data, NULL);
0238                 if (ret)
0239                     goto out;
0240 
0241                 card->cccr.enable_async_irq = 1;
0242             }
0243         }
0244 
0245         /* if no uhs mode ensure we check for high speed */
0246         if (!card->sw_caps.sd3_bus_mode) {
0247             if (speed & SDIO_SPEED_SHS) {
0248                 card->cccr.high_speed = 1;
0249                 card->sw_caps.hs_max_dtr = 50000000;
0250             } else {
0251                 card->cccr.high_speed = 0;
0252                 card->sw_caps.hs_max_dtr = 25000000;
0253             }
0254         }
0255     }
0256 
0257 out:
0258     return ret;
0259 }
0260 
0261 static int sdio_enable_wide(struct mmc_card *card)
0262 {
0263     int ret;
0264     u8 ctrl;
0265 
0266     if (!(card->host->caps & MMC_CAP_4_BIT_DATA))
0267         return 0;
0268 
0269     if (card->cccr.low_speed && !card->cccr.wide_bus)
0270         return 0;
0271 
0272     ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl);
0273     if (ret)
0274         return ret;
0275 
0276     if ((ctrl & SDIO_BUS_WIDTH_MASK) == SDIO_BUS_WIDTH_RESERVED)
0277         pr_warn("%s: SDIO_CCCR_IF is invalid: 0x%02x\n",
0278             mmc_hostname(card->host), ctrl);
0279 
0280     /* set as 4-bit bus width */
0281     ctrl &= ~SDIO_BUS_WIDTH_MASK;
0282     ctrl |= SDIO_BUS_WIDTH_4BIT;
0283 
0284     ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL);
0285     if (ret)
0286         return ret;
0287 
0288     return 1;
0289 }
0290 
0291 /*
0292  * If desired, disconnect the pull-up resistor on CD/DAT[3] (pin 1)
0293  * of the card. This may be required on certain setups of boards,
0294  * controllers and embedded sdio device which do not need the card's
0295  * pull-up. As a result, card detection is disabled and power is saved.
0296  */
0297 static int sdio_disable_cd(struct mmc_card *card)
0298 {
0299     int ret;
0300     u8 ctrl;
0301 
0302     if (!mmc_card_disable_cd(card))
0303         return 0;
0304 
0305     ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl);
0306     if (ret)
0307         return ret;
0308 
0309     ctrl |= SDIO_BUS_CD_DISABLE;
0310 
0311     return mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL);
0312 }
0313 
0314 /*
0315  * Devices that remain active during a system suspend are
0316  * put back into 1-bit mode.
0317  */
0318 static int sdio_disable_wide(struct mmc_card *card)
0319 {
0320     int ret;
0321     u8 ctrl;
0322 
0323     if (!(card->host->caps & MMC_CAP_4_BIT_DATA))
0324         return 0;
0325 
0326     if (card->cccr.low_speed && !card->cccr.wide_bus)
0327         return 0;
0328 
0329     ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl);
0330     if (ret)
0331         return ret;
0332 
0333     if (!(ctrl & SDIO_BUS_WIDTH_4BIT))
0334         return 0;
0335 
0336     ctrl &= ~SDIO_BUS_WIDTH_4BIT;
0337     ctrl |= SDIO_BUS_ASYNC_INT;
0338 
0339     ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL);
0340     if (ret)
0341         return ret;
0342 
0343     mmc_set_bus_width(card->host, MMC_BUS_WIDTH_1);
0344 
0345     return 0;
0346 }
0347 
0348 static int sdio_disable_4bit_bus(struct mmc_card *card)
0349 {
0350     int err;
0351 
0352     if (mmc_card_sdio(card))
0353         goto out;
0354 
0355     if (!(card->host->caps & MMC_CAP_4_BIT_DATA))
0356         return 0;
0357 
0358     if (!(card->scr.bus_widths & SD_SCR_BUS_WIDTH_4))
0359         return 0;
0360 
0361     err = mmc_app_set_bus_width(card, MMC_BUS_WIDTH_1);
0362     if (err)
0363         return err;
0364 
0365 out:
0366     return sdio_disable_wide(card);
0367 }
0368 
0369 
0370 static int sdio_enable_4bit_bus(struct mmc_card *card)
0371 {
0372     int err;
0373 
0374     err = sdio_enable_wide(card);
0375     if (err <= 0)
0376         return err;
0377     if (mmc_card_sdio(card))
0378         goto out;
0379 
0380     if (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4) {
0381         err = mmc_app_set_bus_width(card, MMC_BUS_WIDTH_4);
0382         if (err) {
0383             sdio_disable_wide(card);
0384             return err;
0385         }
0386     }
0387 out:
0388     mmc_set_bus_width(card->host, MMC_BUS_WIDTH_4);
0389 
0390     return 0;
0391 }
0392 
0393 
0394 /*
0395  * Test if the card supports high-speed mode and, if so, switch to it.
0396  */
0397 static int mmc_sdio_switch_hs(struct mmc_card *card, int enable)
0398 {
0399     int ret;
0400     u8 speed;
0401 
0402     if (!(card->host->caps & MMC_CAP_SD_HIGHSPEED))
0403         return 0;
0404 
0405     if (!card->cccr.high_speed)
0406         return 0;
0407 
0408     ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_SPEED, 0, &speed);
0409     if (ret)
0410         return ret;
0411 
0412     if (enable)
0413         speed |= SDIO_SPEED_EHS;
0414     else
0415         speed &= ~SDIO_SPEED_EHS;
0416 
0417     ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_SPEED, speed, NULL);
0418     if (ret)
0419         return ret;
0420 
0421     return 1;
0422 }
0423 
0424 /*
0425  * Enable SDIO/combo card's high-speed mode. Return 0/1 if [not]supported.
0426  */
0427 static int sdio_enable_hs(struct mmc_card *card)
0428 {
0429     int ret;
0430 
0431     ret = mmc_sdio_switch_hs(card, true);
0432     if (ret <= 0 || mmc_card_sdio(card))
0433         return ret;
0434 
0435     ret = mmc_sd_switch_hs(card);
0436     if (ret <= 0)
0437         mmc_sdio_switch_hs(card, false);
0438 
0439     return ret;
0440 }
0441 
0442 static unsigned mmc_sdio_get_max_clock(struct mmc_card *card)
0443 {
0444     unsigned max_dtr;
0445 
0446     if (mmc_card_hs(card)) {
0447         /*
0448          * The SDIO specification doesn't mention how
0449          * the CIS transfer speed register relates to
0450          * high-speed, but it seems that 50 MHz is
0451          * mandatory.
0452          */
0453         max_dtr = 50000000;
0454     } else {
0455         max_dtr = card->cis.max_dtr;
0456     }
0457 
0458     if (mmc_card_sd_combo(card))
0459         max_dtr = min(max_dtr, mmc_sd_get_max_clock(card));
0460 
0461     return max_dtr;
0462 }
0463 
0464 static unsigned char host_drive_to_sdio_drive(int host_strength)
0465 {
0466     switch (host_strength) {
0467     case MMC_SET_DRIVER_TYPE_A:
0468         return SDIO_DTSx_SET_TYPE_A;
0469     case MMC_SET_DRIVER_TYPE_B:
0470         return SDIO_DTSx_SET_TYPE_B;
0471     case MMC_SET_DRIVER_TYPE_C:
0472         return SDIO_DTSx_SET_TYPE_C;
0473     case MMC_SET_DRIVER_TYPE_D:
0474         return SDIO_DTSx_SET_TYPE_D;
0475     default:
0476         return SDIO_DTSx_SET_TYPE_B;
0477     }
0478 }
0479 
0480 static void sdio_select_driver_type(struct mmc_card *card)
0481 {
0482     int card_drv_type, drive_strength, drv_type;
0483     unsigned char card_strength;
0484     int err;
0485 
0486     card->drive_strength = 0;
0487 
0488     card_drv_type = card->sw_caps.sd3_drv_type | SD_DRIVER_TYPE_B;
0489 
0490     drive_strength = mmc_select_drive_strength(card,
0491                            card->sw_caps.uhs_max_dtr,
0492                            card_drv_type, &drv_type);
0493 
0494     if (drive_strength) {
0495         /* if error just use default for drive strength B */
0496         err = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_DRIVE_STRENGTH, 0,
0497                        &card_strength);
0498         if (err)
0499             return;
0500 
0501         card_strength &= ~(SDIO_DRIVE_DTSx_MASK<<SDIO_DRIVE_DTSx_SHIFT);
0502         card_strength |= host_drive_to_sdio_drive(drive_strength);
0503 
0504         /* if error default to drive strength B */
0505         err = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_DRIVE_STRENGTH,
0506                        card_strength, NULL);
0507         if (err)
0508             return;
0509         card->drive_strength = drive_strength;
0510     }
0511 
0512     if (drv_type)
0513         mmc_set_driver_type(card->host, drv_type);
0514 }
0515 
0516 
0517 static int sdio_set_bus_speed_mode(struct mmc_card *card)
0518 {
0519     unsigned int bus_speed, timing;
0520     int err;
0521     unsigned char speed;
0522     unsigned int max_rate;
0523 
0524     /*
0525      * If the host doesn't support any of the UHS-I modes, fallback on
0526      * default speed.
0527      */
0528     if (!mmc_host_uhs(card->host))
0529         return 0;
0530 
0531     bus_speed = SDIO_SPEED_SDR12;
0532     timing = MMC_TIMING_UHS_SDR12;
0533     if ((card->host->caps & MMC_CAP_UHS_SDR104) &&
0534         (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_SDR104)) {
0535             bus_speed = SDIO_SPEED_SDR104;
0536             timing = MMC_TIMING_UHS_SDR104;
0537             card->sw_caps.uhs_max_dtr = UHS_SDR104_MAX_DTR;
0538             card->sd_bus_speed = UHS_SDR104_BUS_SPEED;
0539     } else if ((card->host->caps & MMC_CAP_UHS_DDR50) &&
0540            (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_DDR50)) {
0541             bus_speed = SDIO_SPEED_DDR50;
0542             timing = MMC_TIMING_UHS_DDR50;
0543             card->sw_caps.uhs_max_dtr = UHS_DDR50_MAX_DTR;
0544             card->sd_bus_speed = UHS_DDR50_BUS_SPEED;
0545     } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
0546             MMC_CAP_UHS_SDR50)) && (card->sw_caps.sd3_bus_mode &
0547             SD_MODE_UHS_SDR50)) {
0548             bus_speed = SDIO_SPEED_SDR50;
0549             timing = MMC_TIMING_UHS_SDR50;
0550             card->sw_caps.uhs_max_dtr = UHS_SDR50_MAX_DTR;
0551             card->sd_bus_speed = UHS_SDR50_BUS_SPEED;
0552     } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
0553             MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR25)) &&
0554            (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_SDR25)) {
0555             bus_speed = SDIO_SPEED_SDR25;
0556             timing = MMC_TIMING_UHS_SDR25;
0557             card->sw_caps.uhs_max_dtr = UHS_SDR25_MAX_DTR;
0558             card->sd_bus_speed = UHS_SDR25_BUS_SPEED;
0559     } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
0560             MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR25 |
0561             MMC_CAP_UHS_SDR12)) && (card->sw_caps.sd3_bus_mode &
0562             SD_MODE_UHS_SDR12)) {
0563             bus_speed = SDIO_SPEED_SDR12;
0564             timing = MMC_TIMING_UHS_SDR12;
0565             card->sw_caps.uhs_max_dtr = UHS_SDR12_MAX_DTR;
0566             card->sd_bus_speed = UHS_SDR12_BUS_SPEED;
0567     }
0568 
0569     err = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_SPEED, 0, &speed);
0570     if (err)
0571         return err;
0572 
0573     speed &= ~SDIO_SPEED_BSS_MASK;
0574     speed |= bus_speed;
0575     err = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_SPEED, speed, NULL);
0576     if (err)
0577         return err;
0578 
0579     max_rate = min_not_zero(card->quirk_max_rate,
0580                 card->sw_caps.uhs_max_dtr);
0581 
0582     mmc_set_timing(card->host, timing);
0583     mmc_set_clock(card->host, max_rate);
0584 
0585     return 0;
0586 }
0587 
0588 /*
0589  * UHS-I specific initialization procedure
0590  */
0591 static int mmc_sdio_init_uhs_card(struct mmc_card *card)
0592 {
0593     int err;
0594 
0595     if (!card->scr.sda_spec3)
0596         return 0;
0597 
0598     /* Switch to wider bus */
0599     err = sdio_enable_4bit_bus(card);
0600     if (err)
0601         goto out;
0602 
0603     /* Set the driver strength for the card */
0604     sdio_select_driver_type(card);
0605 
0606     /* Set bus speed mode of the card */
0607     err = sdio_set_bus_speed_mode(card);
0608     if (err)
0609         goto out;
0610 
0611     /*
0612      * SPI mode doesn't define CMD19 and tuning is only valid for SDR50 and
0613      * SDR104 mode SD-cards. Note that tuning is mandatory for SDR104.
0614      */
0615     if (!mmc_host_is_spi(card->host) &&
0616         ((card->host->ios.timing == MMC_TIMING_UHS_SDR50) ||
0617           (card->host->ios.timing == MMC_TIMING_UHS_SDR104)))
0618         err = mmc_execute_tuning(card);
0619 out:
0620     return err;
0621 }
0622 
0623 static int mmc_sdio_pre_init(struct mmc_host *host, u32 ocr,
0624                  struct mmc_card *card)
0625 {
0626     if (card)
0627         mmc_remove_card(card);
0628 
0629     /*
0630      * Reset the card by performing the same steps that are taken by
0631      * mmc_rescan_try_freq() and mmc_attach_sdio() during a "normal" probe.
0632      *
0633      * sdio_reset() is technically not needed. Having just powered up the
0634      * hardware, it should already be in reset state. However, some
0635      * platforms (such as SD8686 on OLPC) do not instantly cut power,
0636      * meaning that a reset is required when restoring power soon after
0637      * powering off. It is harmless in other cases.
0638      *
0639      * The CMD5 reset (mmc_send_io_op_cond()), according to the SDIO spec,
0640      * is not necessary for non-removable cards. However, it is required
0641      * for OLPC SD8686 (which expects a [CMD5,5,3,7] init sequence), and
0642      * harmless in other situations.
0643      *
0644      */
0645 
0646     sdio_reset(host);
0647     mmc_go_idle(host);
0648     mmc_send_if_cond(host, ocr);
0649     return mmc_send_io_op_cond(host, 0, NULL);
0650 }
0651 
0652 /*
0653  * Handle the detection and initialisation of a card.
0654  *
0655  * In the case of a resume, "oldcard" will contain the card
0656  * we're trying to reinitialise.
0657  */
0658 static int mmc_sdio_init_card(struct mmc_host *host, u32 ocr,
0659                   struct mmc_card *oldcard)
0660 {
0661     struct mmc_card *card;
0662     int err;
0663     int retries = 10;
0664     u32 rocr = 0;
0665     u32 ocr_card = ocr;
0666 
0667     WARN_ON(!host->claimed);
0668 
0669     /* to query card if 1.8V signalling is supported */
0670     if (mmc_host_uhs(host))
0671         ocr |= R4_18V_PRESENT;
0672 
0673 try_again:
0674     if (!retries) {
0675         pr_warn("%s: Skipping voltage switch\n", mmc_hostname(host));
0676         ocr &= ~R4_18V_PRESENT;
0677     }
0678 
0679     /*
0680      * Inform the card of the voltage
0681      */
0682     err = mmc_send_io_op_cond(host, ocr, &rocr);
0683     if (err)
0684         return err;
0685 
0686     /*
0687      * For SPI, enable CRC as appropriate.
0688      */
0689     if (mmc_host_is_spi(host)) {
0690         err = mmc_spi_set_crc(host, use_spi_crc);
0691         if (err)
0692             return err;
0693     }
0694 
0695     /*
0696      * Allocate card structure.
0697      */
0698     card = mmc_alloc_card(host, &sdio_type);
0699     if (IS_ERR(card))
0700         return PTR_ERR(card);
0701 
0702     if ((rocr & R4_MEMORY_PRESENT) &&
0703         mmc_sd_get_cid(host, ocr & rocr, card->raw_cid, NULL) == 0) {
0704         card->type = MMC_TYPE_SD_COMBO;
0705 
0706         if (oldcard && (!mmc_card_sd_combo(oldcard) ||
0707             memcmp(card->raw_cid, oldcard->raw_cid, sizeof(card->raw_cid)) != 0)) {
0708             err = -ENOENT;
0709             goto mismatch;
0710         }
0711     } else {
0712         card->type = MMC_TYPE_SDIO;
0713 
0714         if (oldcard && !mmc_card_sdio(oldcard)) {
0715             err = -ENOENT;
0716             goto mismatch;
0717         }
0718     }
0719 
0720     /*
0721      * Call the optional HC's init_card function to handle quirks.
0722      */
0723     if (host->ops->init_card)
0724         host->ops->init_card(host, card);
0725     mmc_fixup_device(card, sdio_card_init_methods);
0726 
0727     card->ocr = ocr_card;
0728 
0729     /*
0730      * If the host and card support UHS-I mode request the card
0731      * to switch to 1.8V signaling level.  No 1.8v signalling if
0732      * UHS mode is not enabled to maintain compatibility and some
0733      * systems that claim 1.8v signalling in fact do not support
0734      * it. Per SDIO spec v3, section 3.1.2, if the voltage is already
0735      * 1.8v, the card sets S18A to 0 in the R4 response. So it will
0736      * fails to check rocr & R4_18V_PRESENT,  but we still need to
0737      * try to init uhs card. sdio_read_cccr will take over this task
0738      * to make sure which speed mode should work.
0739      */
0740     if (rocr & ocr & R4_18V_PRESENT) {
0741         err = mmc_set_uhs_voltage(host, ocr_card);
0742         if (err == -EAGAIN) {
0743             mmc_sdio_pre_init(host, ocr_card, card);
0744             retries--;
0745             goto try_again;
0746         } else if (err) {
0747             ocr &= ~R4_18V_PRESENT;
0748         }
0749     }
0750 
0751     /*
0752      * For native busses:  set card RCA and quit open drain mode.
0753      */
0754     if (!mmc_host_is_spi(host)) {
0755         err = mmc_send_relative_addr(host, &card->rca);
0756         if (err)
0757             goto remove;
0758 
0759         /*
0760          * Update oldcard with the new RCA received from the SDIO
0761          * device -- we're doing this so that it's updated in the
0762          * "card" struct when oldcard overwrites that later.
0763          */
0764         if (oldcard)
0765             oldcard->rca = card->rca;
0766     }
0767 
0768     /*
0769      * Read CSD, before selecting the card
0770      */
0771     if (!oldcard && mmc_card_sd_combo(card)) {
0772         err = mmc_sd_get_csd(card);
0773         if (err)
0774             goto remove;
0775 
0776         mmc_decode_cid(card);
0777     }
0778 
0779     /*
0780      * Select card, as all following commands rely on that.
0781      */
0782     if (!mmc_host_is_spi(host)) {
0783         err = mmc_select_card(card);
0784         if (err)
0785             goto remove;
0786     }
0787 
0788     if (card->quirks & MMC_QUIRK_NONSTD_SDIO) {
0789         /*
0790          * This is non-standard SDIO device, meaning it doesn't
0791          * have any CIA (Common I/O area) registers present.
0792          * It's host's responsibility to fill cccr and cis
0793          * structures in init_card().
0794          */
0795         mmc_set_clock(host, card->cis.max_dtr);
0796 
0797         if (card->cccr.high_speed) {
0798             mmc_set_timing(card->host, MMC_TIMING_SD_HS);
0799         }
0800 
0801         if (oldcard)
0802             mmc_remove_card(card);
0803         else
0804             host->card = card;
0805 
0806         return 0;
0807     }
0808 
0809     /*
0810      * Read the common registers. Note that we should try to
0811      * validate whether UHS would work or not.
0812      */
0813     err = sdio_read_cccr(card, ocr);
0814     if (err) {
0815         mmc_sdio_pre_init(host, ocr_card, card);
0816         if (ocr & R4_18V_PRESENT) {
0817             /* Retry init sequence, but without R4_18V_PRESENT. */
0818             retries = 0;
0819             goto try_again;
0820         }
0821         return err;
0822     }
0823 
0824     /*
0825      * Read the common CIS tuples.
0826      */
0827     err = sdio_read_common_cis(card);
0828     if (err)
0829         goto remove;
0830 
0831     if (oldcard) {
0832         if (card->cis.vendor == oldcard->cis.vendor &&
0833             card->cis.device == oldcard->cis.device) {
0834             mmc_remove_card(card);
0835             card = oldcard;
0836         } else {
0837             err = -ENOENT;
0838             goto mismatch;
0839         }
0840     }
0841 
0842     mmc_fixup_device(card, sdio_fixup_methods);
0843 
0844     if (mmc_card_sd_combo(card)) {
0845         err = mmc_sd_setup_card(host, card, oldcard != NULL);
0846         /* handle as SDIO-only card if memory init failed */
0847         if (err) {
0848             mmc_go_idle(host);
0849             if (mmc_host_is_spi(host))
0850                 /* should not fail, as it worked previously */
0851                 mmc_spi_set_crc(host, use_spi_crc);
0852             card->type = MMC_TYPE_SDIO;
0853         } else
0854             card->dev.type = &sd_type;
0855     }
0856 
0857     /*
0858      * If needed, disconnect card detection pull-up resistor.
0859      */
0860     err = sdio_disable_cd(card);
0861     if (err)
0862         goto remove;
0863 
0864     /* Initialization sequence for UHS-I cards */
0865     /* Only if card supports 1.8v and UHS signaling */
0866     if ((ocr & R4_18V_PRESENT) && card->sw_caps.sd3_bus_mode) {
0867         err = mmc_sdio_init_uhs_card(card);
0868         if (err)
0869             goto remove;
0870     } else {
0871         /*
0872          * Switch to high-speed (if supported).
0873          */
0874         err = sdio_enable_hs(card);
0875         if (err > 0)
0876             mmc_set_timing(card->host, MMC_TIMING_SD_HS);
0877         else if (err)
0878             goto remove;
0879 
0880         /*
0881          * Change to the card's maximum speed.
0882          */
0883         mmc_set_clock(host, mmc_sdio_get_max_clock(card));
0884 
0885         /*
0886          * Switch to wider bus (if supported).
0887          */
0888         err = sdio_enable_4bit_bus(card);
0889         if (err)
0890             goto remove;
0891     }
0892 
0893     if (host->caps2 & MMC_CAP2_AVOID_3_3V &&
0894         host->ios.signal_voltage == MMC_SIGNAL_VOLTAGE_330) {
0895         pr_err("%s: Host failed to negotiate down from 3.3V\n",
0896             mmc_hostname(host));
0897         err = -EINVAL;
0898         goto remove;
0899     }
0900 
0901     host->card = card;
0902     return 0;
0903 
0904 mismatch:
0905     pr_debug("%s: Perhaps the card was replaced\n", mmc_hostname(host));
0906 remove:
0907     if (oldcard != card)
0908         mmc_remove_card(card);
0909     return err;
0910 }
0911 
0912 static int mmc_sdio_reinit_card(struct mmc_host *host)
0913 {
0914     int ret;
0915 
0916     ret = mmc_sdio_pre_init(host, host->card->ocr, NULL);
0917     if (ret)
0918         return ret;
0919 
0920     return mmc_sdio_init_card(host, host->card->ocr, host->card);
0921 }
0922 
0923 /*
0924  * Host is being removed. Free up the current card.
0925  */
0926 static void mmc_sdio_remove(struct mmc_host *host)
0927 {
0928     int i;
0929 
0930     for (i = 0;i < host->card->sdio_funcs;i++) {
0931         if (host->card->sdio_func[i]) {
0932             sdio_remove_func(host->card->sdio_func[i]);
0933             host->card->sdio_func[i] = NULL;
0934         }
0935     }
0936 
0937     mmc_remove_card(host->card);
0938     host->card = NULL;
0939 }
0940 
0941 /*
0942  * Card detection - card is alive.
0943  */
0944 static int mmc_sdio_alive(struct mmc_host *host)
0945 {
0946     return mmc_select_card(host->card);
0947 }
0948 
0949 /*
0950  * Card detection callback from host.
0951  */
0952 static void mmc_sdio_detect(struct mmc_host *host)
0953 {
0954     int err;
0955 
0956     /* Make sure card is powered before detecting it */
0957     if (host->caps & MMC_CAP_POWER_OFF_CARD) {
0958         err = pm_runtime_resume_and_get(&host->card->dev);
0959         if (err < 0)
0960             goto out;
0961     }
0962 
0963     mmc_claim_host(host);
0964 
0965     /*
0966      * Just check if our card has been removed.
0967      */
0968     err = _mmc_detect_card_removed(host);
0969 
0970     mmc_release_host(host);
0971 
0972     /*
0973      * Tell PM core it's OK to power off the card now.
0974      *
0975      * The _sync variant is used in order to ensure that the card
0976      * is left powered off in case an error occurred, and the card
0977      * is going to be removed.
0978      *
0979      * Since there is no specific reason to believe a new user
0980      * is about to show up at this point, the _sync variant is
0981      * desirable anyway.
0982      */
0983     if (host->caps & MMC_CAP_POWER_OFF_CARD)
0984         pm_runtime_put_sync(&host->card->dev);
0985 
0986 out:
0987     if (err) {
0988         mmc_sdio_remove(host);
0989 
0990         mmc_claim_host(host);
0991         mmc_detach_bus(host);
0992         mmc_power_off(host);
0993         mmc_release_host(host);
0994     }
0995 }
0996 
0997 /*
0998  * SDIO pre_suspend.  We need to suspend all functions separately.
0999  * Therefore all registered functions must have drivers with suspend
1000  * and resume methods.  Failing that we simply remove the whole card.
1001  */
1002 static int mmc_sdio_pre_suspend(struct mmc_host *host)
1003 {
1004     int i;
1005 
1006     for (i = 0; i < host->card->sdio_funcs; i++) {
1007         struct sdio_func *func = host->card->sdio_func[i];
1008         if (func && sdio_func_present(func) && func->dev.driver) {
1009             const struct dev_pm_ops *pmops = func->dev.driver->pm;
1010             if (!pmops || !pmops->suspend || !pmops->resume)
1011                 /* force removal of entire card in that case */
1012                 goto remove;
1013         }
1014     }
1015 
1016     return 0;
1017 
1018 remove:
1019     if (!mmc_card_is_removable(host)) {
1020         dev_warn(mmc_dev(host),
1021              "missing suspend/resume ops for non-removable SDIO card\n");
1022         /* Don't remove a non-removable card - we can't re-detect it. */
1023         return 0;
1024     }
1025 
1026     /* Remove the SDIO card and let it be re-detected later on. */
1027     mmc_sdio_remove(host);
1028     mmc_claim_host(host);
1029     mmc_detach_bus(host);
1030     mmc_power_off(host);
1031     mmc_release_host(host);
1032     host->pm_flags = 0;
1033 
1034     return 0;
1035 }
1036 
1037 /*
1038  * SDIO suspend.  Suspend all functions separately.
1039  */
1040 static int mmc_sdio_suspend(struct mmc_host *host)
1041 {
1042     WARN_ON(host->sdio_irqs && !mmc_card_keep_power(host));
1043 
1044     /* Prevent processing of SDIO IRQs in suspended state. */
1045     mmc_card_set_suspended(host->card);
1046     cancel_delayed_work_sync(&host->sdio_irq_work);
1047 
1048     mmc_claim_host(host);
1049 
1050     if (mmc_card_keep_power(host) && mmc_card_wake_sdio_irq(host))
1051         sdio_disable_4bit_bus(host->card);
1052 
1053     if (!mmc_card_keep_power(host)) {
1054         mmc_power_off(host);
1055     } else if (host->retune_period) {
1056         mmc_retune_timer_stop(host);
1057         mmc_retune_needed(host);
1058     }
1059 
1060     mmc_release_host(host);
1061 
1062     return 0;
1063 }
1064 
1065 static int mmc_sdio_resume(struct mmc_host *host)
1066 {
1067     int err = 0;
1068 
1069     /* Basic card reinitialization. */
1070     mmc_claim_host(host);
1071 
1072     /*
1073      * Restore power and reinitialize the card when needed. Note that a
1074      * removable card is checked from a detect work later on in the resume
1075      * process.
1076      */
1077     if (!mmc_card_keep_power(host)) {
1078         mmc_power_up(host, host->card->ocr);
1079         /*
1080          * Tell runtime PM core we just powered up the card,
1081          * since it still believes the card is powered off.
1082          * Note that currently runtime PM is only enabled
1083          * for SDIO cards that are MMC_CAP_POWER_OFF_CARD
1084          */
1085         if (host->caps & MMC_CAP_POWER_OFF_CARD) {
1086             pm_runtime_disable(&host->card->dev);
1087             pm_runtime_set_active(&host->card->dev);
1088             pm_runtime_enable(&host->card->dev);
1089         }
1090         err = mmc_sdio_reinit_card(host);
1091     } else if (mmc_card_wake_sdio_irq(host)) {
1092         /* We may have switched to 1-bit mode during suspend */
1093         err = sdio_enable_4bit_bus(host->card);
1094     }
1095 
1096     if (err)
1097         goto out;
1098 
1099     /* Allow SDIO IRQs to be processed again. */
1100     mmc_card_clr_suspended(host->card);
1101 
1102     if (host->sdio_irqs) {
1103         if (!(host->caps2 & MMC_CAP2_SDIO_IRQ_NOTHREAD))
1104             wake_up_process(host->sdio_irq_thread);
1105         else if (host->caps & MMC_CAP_SDIO_IRQ)
1106             queue_delayed_work(system_wq, &host->sdio_irq_work, 0);
1107     }
1108 
1109 out:
1110     mmc_release_host(host);
1111 
1112     host->pm_flags &= ~MMC_PM_KEEP_POWER;
1113     return err;
1114 }
1115 
1116 static int mmc_sdio_runtime_suspend(struct mmc_host *host)
1117 {
1118     /* No references to the card, cut the power to it. */
1119     mmc_claim_host(host);
1120     mmc_power_off(host);
1121     mmc_release_host(host);
1122 
1123     return 0;
1124 }
1125 
1126 static int mmc_sdio_runtime_resume(struct mmc_host *host)
1127 {
1128     int ret;
1129 
1130     /* Restore power and re-initialize. */
1131     mmc_claim_host(host);
1132     mmc_power_up(host, host->card->ocr);
1133     ret = mmc_sdio_reinit_card(host);
1134     mmc_release_host(host);
1135 
1136     return ret;
1137 }
1138 
1139 /*
1140  * SDIO HW reset
1141  *
1142  * Returns 0 if the HW reset was executed synchronously, returns 1 if the HW
1143  * reset was asynchronously scheduled, else a negative error code.
1144  */
1145 static int mmc_sdio_hw_reset(struct mmc_host *host)
1146 {
1147     struct mmc_card *card = host->card;
1148 
1149     /*
1150      * In case the card is shared among multiple func drivers, reset the
1151      * card through a rescan work. In this way it will be removed and
1152      * re-detected, thus all func drivers becomes informed about it.
1153      */
1154     if (atomic_read(&card->sdio_funcs_probed) > 1) {
1155         if (mmc_card_removed(card))
1156             return 1;
1157         host->rescan_entered = 0;
1158         mmc_card_set_removed(card);
1159         _mmc_detect_change(host, 0, false);
1160         return 1;
1161     }
1162 
1163     /*
1164      * A single func driver has been probed, then let's skip the heavy
1165      * hotplug dance above and execute the reset immediately.
1166      */
1167     mmc_power_cycle(host, card->ocr);
1168     return mmc_sdio_reinit_card(host);
1169 }
1170 
1171 static int mmc_sdio_sw_reset(struct mmc_host *host)
1172 {
1173     mmc_set_clock(host, host->f_init);
1174     sdio_reset(host);
1175     mmc_go_idle(host);
1176 
1177     mmc_set_initial_state(host);
1178     mmc_set_initial_signal_voltage(host);
1179 
1180     return mmc_sdio_reinit_card(host);
1181 }
1182 
1183 static const struct mmc_bus_ops mmc_sdio_ops = {
1184     .remove = mmc_sdio_remove,
1185     .detect = mmc_sdio_detect,
1186     .pre_suspend = mmc_sdio_pre_suspend,
1187     .suspend = mmc_sdio_suspend,
1188     .resume = mmc_sdio_resume,
1189     .runtime_suspend = mmc_sdio_runtime_suspend,
1190     .runtime_resume = mmc_sdio_runtime_resume,
1191     .alive = mmc_sdio_alive,
1192     .hw_reset = mmc_sdio_hw_reset,
1193     .sw_reset = mmc_sdio_sw_reset,
1194 };
1195 
1196 
1197 /*
1198  * Starting point for SDIO card init.
1199  */
1200 int mmc_attach_sdio(struct mmc_host *host)
1201 {
1202     int err, i, funcs;
1203     u32 ocr, rocr;
1204     struct mmc_card *card;
1205 
1206     WARN_ON(!host->claimed);
1207 
1208     err = mmc_send_io_op_cond(host, 0, &ocr);
1209     if (err)
1210         return err;
1211 
1212     mmc_attach_bus(host, &mmc_sdio_ops);
1213     if (host->ocr_avail_sdio)
1214         host->ocr_avail = host->ocr_avail_sdio;
1215 
1216 
1217     rocr = mmc_select_voltage(host, ocr);
1218 
1219     /*
1220      * Can we support the voltage(s) of the card(s)?
1221      */
1222     if (!rocr) {
1223         err = -EINVAL;
1224         goto err;
1225     }
1226 
1227     /*
1228      * Detect and init the card.
1229      */
1230     err = mmc_sdio_init_card(host, rocr, NULL);
1231     if (err)
1232         goto err;
1233 
1234     card = host->card;
1235 
1236     /*
1237      * Enable runtime PM only if supported by host+card+board
1238      */
1239     if (host->caps & MMC_CAP_POWER_OFF_CARD) {
1240         /*
1241          * Do not allow runtime suspend until after SDIO function
1242          * devices are added.
1243          */
1244         pm_runtime_get_noresume(&card->dev);
1245 
1246         /*
1247          * Let runtime PM core know our card is active
1248          */
1249         err = pm_runtime_set_active(&card->dev);
1250         if (err)
1251             goto remove;
1252 
1253         /*
1254          * Enable runtime PM for this card
1255          */
1256         pm_runtime_enable(&card->dev);
1257     }
1258 
1259     /*
1260      * The number of functions on the card is encoded inside
1261      * the ocr.
1262      */
1263     funcs = (ocr & 0x70000000) >> 28;
1264     card->sdio_funcs = 0;
1265 
1266     /*
1267      * Initialize (but don't add) all present functions.
1268      */
1269     for (i = 0; i < funcs; i++, card->sdio_funcs++) {
1270         err = sdio_init_func(host->card, i + 1);
1271         if (err)
1272             goto remove;
1273 
1274         /*
1275          * Enable Runtime PM for this func (if supported)
1276          */
1277         if (host->caps & MMC_CAP_POWER_OFF_CARD)
1278             pm_runtime_enable(&card->sdio_func[i]->dev);
1279     }
1280 
1281     /*
1282      * First add the card to the driver model...
1283      */
1284     mmc_release_host(host);
1285     err = mmc_add_card(host->card);
1286     if (err)
1287         goto remove_added;
1288 
1289     /*
1290      * ...then the SDIO functions.
1291      */
1292     for (i = 0;i < funcs;i++) {
1293         err = sdio_add_func(host->card->sdio_func[i]);
1294         if (err)
1295             goto remove_added;
1296     }
1297 
1298     if (host->caps & MMC_CAP_POWER_OFF_CARD)
1299         pm_runtime_put(&card->dev);
1300 
1301     mmc_claim_host(host);
1302     return 0;
1303 
1304 
1305 remove:
1306     mmc_release_host(host);
1307 remove_added:
1308     /*
1309      * The devices are being deleted so it is not necessary to disable
1310      * runtime PM. Similarly we also don't pm_runtime_put() the SDIO card
1311      * because it needs to be active to remove any function devices that
1312      * were probed, and after that it gets deleted.
1313      */
1314     mmc_sdio_remove(host);
1315     mmc_claim_host(host);
1316 err:
1317     mmc_detach_bus(host);
1318 
1319     pr_err("%s: error %d whilst initialising SDIO card\n",
1320         mmc_hostname(host), err);
1321 
1322     return err;
1323 }
1324