Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  *  linux/drivers/mmc/core/sd.c
0004  *
0005  *  Copyright (C) 2003-2004 Russell King, All Rights Reserved.
0006  *  SD support Copyright (C) 2004 Ian Molton, All Rights Reserved.
0007  *  Copyright (C) 2005-2007 Pierre Ossman, All Rights Reserved.
0008  */
0009 
0010 #include <linux/err.h>
0011 #include <linux/sizes.h>
0012 #include <linux/slab.h>
0013 #include <linux/stat.h>
0014 #include <linux/pm_runtime.h>
0015 #include <linux/random.h>
0016 #include <linux/scatterlist.h>
0017 #include <linux/sysfs.h>
0018 
0019 #include <linux/mmc/host.h>
0020 #include <linux/mmc/card.h>
0021 #include <linux/mmc/mmc.h>
0022 #include <linux/mmc/sd.h>
0023 
0024 #include "core.h"
0025 #include "card.h"
0026 #include "host.h"
0027 #include "bus.h"
0028 #include "mmc_ops.h"
0029 #include "sd.h"
0030 #include "sd_ops.h"
0031 
0032 static const unsigned int tran_exp[] = {
0033     10000,      100000,     1000000,    10000000,
0034     0,      0,      0,      0
0035 };
0036 
0037 static const unsigned char tran_mant[] = {
0038     0,  10, 12, 13, 15, 20, 25, 30,
0039     35, 40, 45, 50, 55, 60, 70, 80,
0040 };
0041 
0042 static const unsigned int taac_exp[] = {
0043     1,  10, 100,    1000,   10000,  100000, 1000000, 10000000,
0044 };
0045 
0046 static const unsigned int taac_mant[] = {
0047     0,  10, 12, 13, 15, 20, 25, 30,
0048     35, 40, 45, 50, 55, 60, 70, 80,
0049 };
0050 
0051 static const unsigned int sd_au_size[] = {
0052     0,      SZ_16K / 512,       SZ_32K / 512,   SZ_64K / 512,
0053     SZ_128K / 512,  SZ_256K / 512,      SZ_512K / 512,  SZ_1M / 512,
0054     SZ_2M / 512,    SZ_4M / 512,        SZ_8M / 512,    (SZ_8M + SZ_4M) / 512,
0055     SZ_16M / 512,   (SZ_16M + SZ_8M) / 512, SZ_32M / 512,   SZ_64M / 512,
0056 };
0057 
0058 #define UNSTUFF_BITS(resp,start,size)                   \
0059     ({                              \
0060         const int __size = size;                \
0061         const u32 __mask = (__size < 32 ? 1 << __size : 0) - 1; \
0062         const int __off = 3 - ((start) / 32);           \
0063         const int __shft = (start) & 31;            \
0064         u32 __res;                      \
0065                                     \
0066         __res = resp[__off] >> __shft;              \
0067         if (__size + __shft > 32)               \
0068             __res |= resp[__off-1] << ((32 - __shft) % 32); \
0069         __res & __mask;                     \
0070     })
0071 
0072 #define SD_POWEROFF_NOTIFY_TIMEOUT_MS 1000
0073 #define SD_WRITE_EXTR_SINGLE_TIMEOUT_MS 1000
0074 
0075 struct sd_busy_data {
0076     struct mmc_card *card;
0077     u8 *reg_buf;
0078 };
0079 
0080 /*
0081  * Given the decoded CSD structure, decode the raw CID to our CID structure.
0082  */
0083 void mmc_decode_cid(struct mmc_card *card)
0084 {
0085     u32 *resp = card->raw_cid;
0086 
0087     /*
0088      * Add the raw card ID (cid) data to the entropy pool. It doesn't
0089      * matter that not all of it is unique, it's just bonus entropy.
0090      */
0091     add_device_randomness(&card->raw_cid, sizeof(card->raw_cid));
0092 
0093     /*
0094      * SD doesn't currently have a version field so we will
0095      * have to assume we can parse this.
0096      */
0097     card->cid.manfid        = UNSTUFF_BITS(resp, 120, 8);
0098     card->cid.oemid         = UNSTUFF_BITS(resp, 104, 16);
0099     card->cid.prod_name[0]      = UNSTUFF_BITS(resp, 96, 8);
0100     card->cid.prod_name[1]      = UNSTUFF_BITS(resp, 88, 8);
0101     card->cid.prod_name[2]      = UNSTUFF_BITS(resp, 80, 8);
0102     card->cid.prod_name[3]      = UNSTUFF_BITS(resp, 72, 8);
0103     card->cid.prod_name[4]      = UNSTUFF_BITS(resp, 64, 8);
0104     card->cid.hwrev         = UNSTUFF_BITS(resp, 60, 4);
0105     card->cid.fwrev         = UNSTUFF_BITS(resp, 56, 4);
0106     card->cid.serial        = UNSTUFF_BITS(resp, 24, 32);
0107     card->cid.year          = UNSTUFF_BITS(resp, 12, 8);
0108     card->cid.month         = UNSTUFF_BITS(resp, 8, 4);
0109 
0110     card->cid.year += 2000; /* SD cards year offset */
0111 }
0112 
0113 /*
0114  * Given a 128-bit response, decode to our card CSD structure.
0115  */
0116 static int mmc_decode_csd(struct mmc_card *card)
0117 {
0118     struct mmc_csd *csd = &card->csd;
0119     unsigned int e, m, csd_struct;
0120     u32 *resp = card->raw_csd;
0121 
0122     csd_struct = UNSTUFF_BITS(resp, 126, 2);
0123 
0124     switch (csd_struct) {
0125     case 0:
0126         m = UNSTUFF_BITS(resp, 115, 4);
0127         e = UNSTUFF_BITS(resp, 112, 3);
0128         csd->taac_ns     = (taac_exp[e] * taac_mant[m] + 9) / 10;
0129         csd->taac_clks   = UNSTUFF_BITS(resp, 104, 8) * 100;
0130 
0131         m = UNSTUFF_BITS(resp, 99, 4);
0132         e = UNSTUFF_BITS(resp, 96, 3);
0133         csd->max_dtr      = tran_exp[e] * tran_mant[m];
0134         csd->cmdclass     = UNSTUFF_BITS(resp, 84, 12);
0135 
0136         e = UNSTUFF_BITS(resp, 47, 3);
0137         m = UNSTUFF_BITS(resp, 62, 12);
0138         csd->capacity     = (1 + m) << (e + 2);
0139 
0140         csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4);
0141         csd->read_partial = UNSTUFF_BITS(resp, 79, 1);
0142         csd->write_misalign = UNSTUFF_BITS(resp, 78, 1);
0143         csd->read_misalign = UNSTUFF_BITS(resp, 77, 1);
0144         csd->dsr_imp = UNSTUFF_BITS(resp, 76, 1);
0145         csd->r2w_factor = UNSTUFF_BITS(resp, 26, 3);
0146         csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4);
0147         csd->write_partial = UNSTUFF_BITS(resp, 21, 1);
0148 
0149         if (UNSTUFF_BITS(resp, 46, 1)) {
0150             csd->erase_size = 1;
0151         } else if (csd->write_blkbits >= 9) {
0152             csd->erase_size = UNSTUFF_BITS(resp, 39, 7) + 1;
0153             csd->erase_size <<= csd->write_blkbits - 9;
0154         }
0155 
0156         if (UNSTUFF_BITS(resp, 13, 1))
0157             mmc_card_set_readonly(card);
0158         break;
0159     case 1:
0160         /*
0161          * This is a block-addressed SDHC or SDXC card. Most
0162          * interesting fields are unused and have fixed
0163          * values. To avoid getting tripped by buggy cards,
0164          * we assume those fixed values ourselves.
0165          */
0166         mmc_card_set_blockaddr(card);
0167 
0168         csd->taac_ns     = 0; /* Unused */
0169         csd->taac_clks   = 0; /* Unused */
0170 
0171         m = UNSTUFF_BITS(resp, 99, 4);
0172         e = UNSTUFF_BITS(resp, 96, 3);
0173         csd->max_dtr      = tran_exp[e] * tran_mant[m];
0174         csd->cmdclass     = UNSTUFF_BITS(resp, 84, 12);
0175         csd->c_size   = UNSTUFF_BITS(resp, 48, 22);
0176 
0177         /* SDXC cards have a minimum C_SIZE of 0x00FFFF */
0178         if (csd->c_size >= 0xFFFF)
0179             mmc_card_set_ext_capacity(card);
0180 
0181         m = UNSTUFF_BITS(resp, 48, 22);
0182         csd->capacity     = (1 + m) << 10;
0183 
0184         csd->read_blkbits = 9;
0185         csd->read_partial = 0;
0186         csd->write_misalign = 0;
0187         csd->read_misalign = 0;
0188         csd->r2w_factor = 4; /* Unused */
0189         csd->write_blkbits = 9;
0190         csd->write_partial = 0;
0191         csd->erase_size = 1;
0192 
0193         if (UNSTUFF_BITS(resp, 13, 1))
0194             mmc_card_set_readonly(card);
0195         break;
0196     default:
0197         pr_err("%s: unrecognised CSD structure version %d\n",
0198             mmc_hostname(card->host), csd_struct);
0199         return -EINVAL;
0200     }
0201 
0202     card->erase_size = csd->erase_size;
0203 
0204     return 0;
0205 }
0206 
0207 /*
0208  * Given a 64-bit response, decode to our card SCR structure.
0209  */
0210 static int mmc_decode_scr(struct mmc_card *card)
0211 {
0212     struct sd_scr *scr = &card->scr;
0213     unsigned int scr_struct;
0214     u32 resp[4];
0215 
0216     resp[3] = card->raw_scr[1];
0217     resp[2] = card->raw_scr[0];
0218 
0219     scr_struct = UNSTUFF_BITS(resp, 60, 4);
0220     if (scr_struct != 0) {
0221         pr_err("%s: unrecognised SCR structure version %d\n",
0222             mmc_hostname(card->host), scr_struct);
0223         return -EINVAL;
0224     }
0225 
0226     scr->sda_vsn = UNSTUFF_BITS(resp, 56, 4);
0227     scr->bus_widths = UNSTUFF_BITS(resp, 48, 4);
0228     if (scr->sda_vsn == SCR_SPEC_VER_2)
0229         /* Check if Physical Layer Spec v3.0 is supported */
0230         scr->sda_spec3 = UNSTUFF_BITS(resp, 47, 1);
0231 
0232     if (scr->sda_spec3) {
0233         scr->sda_spec4 = UNSTUFF_BITS(resp, 42, 1);
0234         scr->sda_specx = UNSTUFF_BITS(resp, 38, 4);
0235     }
0236 
0237     if (UNSTUFF_BITS(resp, 55, 1))
0238         card->erased_byte = 0xFF;
0239     else
0240         card->erased_byte = 0x0;
0241 
0242     if (scr->sda_spec4)
0243         scr->cmds = UNSTUFF_BITS(resp, 32, 4);
0244     else if (scr->sda_spec3)
0245         scr->cmds = UNSTUFF_BITS(resp, 32, 2);
0246 
0247     /* SD Spec says: any SD Card shall set at least bits 0 and 2 */
0248     if (!(scr->bus_widths & SD_SCR_BUS_WIDTH_1) ||
0249         !(scr->bus_widths & SD_SCR_BUS_WIDTH_4)) {
0250         pr_err("%s: invalid bus width\n", mmc_hostname(card->host));
0251         return -EINVAL;
0252     }
0253 
0254     return 0;
0255 }
0256 
0257 /*
0258  * Fetch and process SD Status register.
0259  */
0260 static int mmc_read_ssr(struct mmc_card *card)
0261 {
0262     unsigned int au, es, et, eo;
0263     __be32 *raw_ssr;
0264     u32 resp[4] = {};
0265     u8 discard_support;
0266     int i;
0267 
0268     if (!(card->csd.cmdclass & CCC_APP_SPEC)) {
0269         pr_warn("%s: card lacks mandatory SD Status function\n",
0270             mmc_hostname(card->host));
0271         return 0;
0272     }
0273 
0274     raw_ssr = kmalloc(sizeof(card->raw_ssr), GFP_KERNEL);
0275     if (!raw_ssr)
0276         return -ENOMEM;
0277 
0278     if (mmc_app_sd_status(card, raw_ssr)) {
0279         pr_warn("%s: problem reading SD Status register\n",
0280             mmc_hostname(card->host));
0281         kfree(raw_ssr);
0282         return 0;
0283     }
0284 
0285     for (i = 0; i < 16; i++)
0286         card->raw_ssr[i] = be32_to_cpu(raw_ssr[i]);
0287 
0288     kfree(raw_ssr);
0289 
0290     /*
0291      * UNSTUFF_BITS only works with four u32s so we have to offset the
0292      * bitfield positions accordingly.
0293      */
0294     au = UNSTUFF_BITS(card->raw_ssr, 428 - 384, 4);
0295     if (au) {
0296         if (au <= 9 || card->scr.sda_spec3) {
0297             card->ssr.au = sd_au_size[au];
0298             es = UNSTUFF_BITS(card->raw_ssr, 408 - 384, 16);
0299             et = UNSTUFF_BITS(card->raw_ssr, 402 - 384, 6);
0300             if (es && et) {
0301                 eo = UNSTUFF_BITS(card->raw_ssr, 400 - 384, 2);
0302                 card->ssr.erase_timeout = (et * 1000) / es;
0303                 card->ssr.erase_offset = eo * 1000;
0304             }
0305         } else {
0306             pr_warn("%s: SD Status: Invalid Allocation Unit size\n",
0307                 mmc_hostname(card->host));
0308         }
0309     }
0310 
0311     /*
0312      * starting SD5.1 discard is supported if DISCARD_SUPPORT (b313) is set
0313      */
0314     resp[3] = card->raw_ssr[6];
0315     discard_support = UNSTUFF_BITS(resp, 313 - 288, 1);
0316     card->erase_arg = (card->scr.sda_specx && discard_support) ?
0317                 SD_DISCARD_ARG : SD_ERASE_ARG;
0318 
0319     return 0;
0320 }
0321 
0322 /*
0323  * Fetches and decodes switch information
0324  */
0325 static int mmc_read_switch(struct mmc_card *card)
0326 {
0327     int err;
0328     u8 *status;
0329 
0330     if (card->scr.sda_vsn < SCR_SPEC_VER_1)
0331         return 0;
0332 
0333     if (!(card->csd.cmdclass & CCC_SWITCH)) {
0334         pr_warn("%s: card lacks mandatory switch function, performance might suffer\n",
0335             mmc_hostname(card->host));
0336         return 0;
0337     }
0338 
0339     status = kmalloc(64, GFP_KERNEL);
0340     if (!status)
0341         return -ENOMEM;
0342 
0343     /*
0344      * Find out the card's support bits with a mode 0 operation.
0345      * The argument does not matter, as the support bits do not
0346      * change with the arguments.
0347      */
0348     err = mmc_sd_switch(card, 0, 0, 0, status);
0349     if (err) {
0350         /*
0351          * If the host or the card can't do the switch,
0352          * fail more gracefully.
0353          */
0354         if (err != -EINVAL && err != -ENOSYS && err != -EFAULT)
0355             goto out;
0356 
0357         pr_warn("%s: problem reading Bus Speed modes\n",
0358             mmc_hostname(card->host));
0359         err = 0;
0360 
0361         goto out;
0362     }
0363 
0364     if (status[13] & SD_MODE_HIGH_SPEED)
0365         card->sw_caps.hs_max_dtr = HIGH_SPEED_MAX_DTR;
0366 
0367     if (card->scr.sda_spec3) {
0368         card->sw_caps.sd3_bus_mode = status[13];
0369         /* Driver Strengths supported by the card */
0370         card->sw_caps.sd3_drv_type = status[9];
0371         card->sw_caps.sd3_curr_limit = status[7] | status[6] << 8;
0372     }
0373 
0374 out:
0375     kfree(status);
0376 
0377     return err;
0378 }
0379 
0380 /*
0381  * Test if the card supports high-speed mode and, if so, switch to it.
0382  */
0383 int mmc_sd_switch_hs(struct mmc_card *card)
0384 {
0385     int err;
0386     u8 *status;
0387 
0388     if (card->scr.sda_vsn < SCR_SPEC_VER_1)
0389         return 0;
0390 
0391     if (!(card->csd.cmdclass & CCC_SWITCH))
0392         return 0;
0393 
0394     if (!(card->host->caps & MMC_CAP_SD_HIGHSPEED))
0395         return 0;
0396 
0397     if (card->sw_caps.hs_max_dtr == 0)
0398         return 0;
0399 
0400     status = kmalloc(64, GFP_KERNEL);
0401     if (!status)
0402         return -ENOMEM;
0403 
0404     err = mmc_sd_switch(card, 1, 0, HIGH_SPEED_BUS_SPEED, status);
0405     if (err)
0406         goto out;
0407 
0408     if ((status[16] & 0xF) != HIGH_SPEED_BUS_SPEED) {
0409         pr_warn("%s: Problem switching card into high-speed mode!\n",
0410             mmc_hostname(card->host));
0411         err = 0;
0412     } else {
0413         err = 1;
0414     }
0415 
0416 out:
0417     kfree(status);
0418 
0419     return err;
0420 }
0421 
0422 static int sd_select_driver_type(struct mmc_card *card, u8 *status)
0423 {
0424     int card_drv_type, drive_strength, drv_type;
0425     int err;
0426 
0427     card->drive_strength = 0;
0428 
0429     card_drv_type = card->sw_caps.sd3_drv_type | SD_DRIVER_TYPE_B;
0430 
0431     drive_strength = mmc_select_drive_strength(card,
0432                            card->sw_caps.uhs_max_dtr,
0433                            card_drv_type, &drv_type);
0434 
0435     if (drive_strength) {
0436         err = mmc_sd_switch(card, 1, 2, drive_strength, status);
0437         if (err)
0438             return err;
0439         if ((status[15] & 0xF) != drive_strength) {
0440             pr_warn("%s: Problem setting drive strength!\n",
0441                 mmc_hostname(card->host));
0442             return 0;
0443         }
0444         card->drive_strength = drive_strength;
0445     }
0446 
0447     if (drv_type)
0448         mmc_set_driver_type(card->host, drv_type);
0449 
0450     return 0;
0451 }
0452 
0453 static void sd_update_bus_speed_mode(struct mmc_card *card)
0454 {
0455     /*
0456      * If the host doesn't support any of the UHS-I modes, fallback on
0457      * default speed.
0458      */
0459     if (!mmc_host_uhs(card->host)) {
0460         card->sd_bus_speed = 0;
0461         return;
0462     }
0463 
0464     if ((card->host->caps & MMC_CAP_UHS_SDR104) &&
0465         (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_SDR104)) {
0466             card->sd_bus_speed = UHS_SDR104_BUS_SPEED;
0467     } else if ((card->host->caps & MMC_CAP_UHS_DDR50) &&
0468            (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_DDR50)) {
0469             card->sd_bus_speed = UHS_DDR50_BUS_SPEED;
0470     } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
0471             MMC_CAP_UHS_SDR50)) && (card->sw_caps.sd3_bus_mode &
0472             SD_MODE_UHS_SDR50)) {
0473             card->sd_bus_speed = UHS_SDR50_BUS_SPEED;
0474     } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
0475             MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR25)) &&
0476            (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_SDR25)) {
0477             card->sd_bus_speed = UHS_SDR25_BUS_SPEED;
0478     } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
0479             MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR25 |
0480             MMC_CAP_UHS_SDR12)) && (card->sw_caps.sd3_bus_mode &
0481             SD_MODE_UHS_SDR12)) {
0482             card->sd_bus_speed = UHS_SDR12_BUS_SPEED;
0483     }
0484 }
0485 
0486 static int sd_set_bus_speed_mode(struct mmc_card *card, u8 *status)
0487 {
0488     int err;
0489     unsigned int timing = 0;
0490 
0491     switch (card->sd_bus_speed) {
0492     case UHS_SDR104_BUS_SPEED:
0493         timing = MMC_TIMING_UHS_SDR104;
0494         card->sw_caps.uhs_max_dtr = UHS_SDR104_MAX_DTR;
0495         break;
0496     case UHS_DDR50_BUS_SPEED:
0497         timing = MMC_TIMING_UHS_DDR50;
0498         card->sw_caps.uhs_max_dtr = UHS_DDR50_MAX_DTR;
0499         break;
0500     case UHS_SDR50_BUS_SPEED:
0501         timing = MMC_TIMING_UHS_SDR50;
0502         card->sw_caps.uhs_max_dtr = UHS_SDR50_MAX_DTR;
0503         break;
0504     case UHS_SDR25_BUS_SPEED:
0505         timing = MMC_TIMING_UHS_SDR25;
0506         card->sw_caps.uhs_max_dtr = UHS_SDR25_MAX_DTR;
0507         break;
0508     case UHS_SDR12_BUS_SPEED:
0509         timing = MMC_TIMING_UHS_SDR12;
0510         card->sw_caps.uhs_max_dtr = UHS_SDR12_MAX_DTR;
0511         break;
0512     default:
0513         return 0;
0514     }
0515 
0516     err = mmc_sd_switch(card, 1, 0, card->sd_bus_speed, status);
0517     if (err)
0518         return err;
0519 
0520     if ((status[16] & 0xF) != card->sd_bus_speed)
0521         pr_warn("%s: Problem setting bus speed mode!\n",
0522             mmc_hostname(card->host));
0523     else {
0524         mmc_set_timing(card->host, timing);
0525         mmc_set_clock(card->host, card->sw_caps.uhs_max_dtr);
0526     }
0527 
0528     return 0;
0529 }
0530 
0531 /* Get host's max current setting at its current voltage */
0532 static u32 sd_get_host_max_current(struct mmc_host *host)
0533 {
0534     u32 voltage, max_current;
0535 
0536     voltage = 1 << host->ios.vdd;
0537     switch (voltage) {
0538     case MMC_VDD_165_195:
0539         max_current = host->max_current_180;
0540         break;
0541     case MMC_VDD_29_30:
0542     case MMC_VDD_30_31:
0543         max_current = host->max_current_300;
0544         break;
0545     case MMC_VDD_32_33:
0546     case MMC_VDD_33_34:
0547         max_current = host->max_current_330;
0548         break;
0549     default:
0550         max_current = 0;
0551     }
0552 
0553     return max_current;
0554 }
0555 
0556 static int sd_set_current_limit(struct mmc_card *card, u8 *status)
0557 {
0558     int current_limit = SD_SET_CURRENT_NO_CHANGE;
0559     int err;
0560     u32 max_current;
0561 
0562     /*
0563      * Current limit switch is only defined for SDR50, SDR104, and DDR50
0564      * bus speed modes. For other bus speed modes, we do not change the
0565      * current limit.
0566      */
0567     if ((card->sd_bus_speed != UHS_SDR50_BUS_SPEED) &&
0568         (card->sd_bus_speed != UHS_SDR104_BUS_SPEED) &&
0569         (card->sd_bus_speed != UHS_DDR50_BUS_SPEED))
0570         return 0;
0571 
0572     /*
0573      * Host has different current capabilities when operating at
0574      * different voltages, so find out its max current first.
0575      */
0576     max_current = sd_get_host_max_current(card->host);
0577 
0578     /*
0579      * We only check host's capability here, if we set a limit that is
0580      * higher than the card's maximum current, the card will be using its
0581      * maximum current, e.g. if the card's maximum current is 300ma, and
0582      * when we set current limit to 200ma, the card will draw 200ma, and
0583      * when we set current limit to 400/600/800ma, the card will draw its
0584      * maximum 300ma from the host.
0585      *
0586      * The above is incorrect: if we try to set a current limit that is
0587      * not supported by the card, the card can rightfully error out the
0588      * attempt, and remain at the default current limit.  This results
0589      * in a 300mA card being limited to 200mA even though the host
0590      * supports 800mA. Failures seen with SanDisk 8GB UHS cards with
0591      * an iMX6 host. --rmk
0592      */
0593     if (max_current >= 800 &&
0594         card->sw_caps.sd3_curr_limit & SD_MAX_CURRENT_800)
0595         current_limit = SD_SET_CURRENT_LIMIT_800;
0596     else if (max_current >= 600 &&
0597          card->sw_caps.sd3_curr_limit & SD_MAX_CURRENT_600)
0598         current_limit = SD_SET_CURRENT_LIMIT_600;
0599     else if (max_current >= 400 &&
0600          card->sw_caps.sd3_curr_limit & SD_MAX_CURRENT_400)
0601         current_limit = SD_SET_CURRENT_LIMIT_400;
0602     else if (max_current >= 200 &&
0603          card->sw_caps.sd3_curr_limit & SD_MAX_CURRENT_200)
0604         current_limit = SD_SET_CURRENT_LIMIT_200;
0605 
0606     if (current_limit != SD_SET_CURRENT_NO_CHANGE) {
0607         err = mmc_sd_switch(card, 1, 3, current_limit, status);
0608         if (err)
0609             return err;
0610 
0611         if (((status[15] >> 4) & 0x0F) != current_limit)
0612             pr_warn("%s: Problem setting current limit!\n",
0613                 mmc_hostname(card->host));
0614 
0615     }
0616 
0617     return 0;
0618 }
0619 
0620 /*
0621  * UHS-I specific initialization procedure
0622  */
0623 static int mmc_sd_init_uhs_card(struct mmc_card *card)
0624 {
0625     int err;
0626     u8 *status;
0627 
0628     if (!(card->csd.cmdclass & CCC_SWITCH))
0629         return 0;
0630 
0631     status = kmalloc(64, GFP_KERNEL);
0632     if (!status)
0633         return -ENOMEM;
0634 
0635     /* Set 4-bit bus width */
0636     err = mmc_app_set_bus_width(card, MMC_BUS_WIDTH_4);
0637     if (err)
0638         goto out;
0639 
0640     mmc_set_bus_width(card->host, MMC_BUS_WIDTH_4);
0641 
0642     /*
0643      * Select the bus speed mode depending on host
0644      * and card capability.
0645      */
0646     sd_update_bus_speed_mode(card);
0647 
0648     /* Set the driver strength for the card */
0649     err = sd_select_driver_type(card, status);
0650     if (err)
0651         goto out;
0652 
0653     /* Set current limit for the card */
0654     err = sd_set_current_limit(card, status);
0655     if (err)
0656         goto out;
0657 
0658     /* Set bus speed mode of the card */
0659     err = sd_set_bus_speed_mode(card, status);
0660     if (err)
0661         goto out;
0662 
0663     /*
0664      * SPI mode doesn't define CMD19 and tuning is only valid for SDR50 and
0665      * SDR104 mode SD-cards. Note that tuning is mandatory for SDR104.
0666      */
0667     if (!mmc_host_is_spi(card->host) &&
0668         (card->host->ios.timing == MMC_TIMING_UHS_SDR50 ||
0669          card->host->ios.timing == MMC_TIMING_UHS_DDR50 ||
0670          card->host->ios.timing == MMC_TIMING_UHS_SDR104)) {
0671         err = mmc_execute_tuning(card);
0672 
0673         /*
0674          * As SD Specifications Part1 Physical Layer Specification
0675          * Version 3.01 says, CMD19 tuning is available for unlocked
0676          * cards in transfer state of 1.8V signaling mode. The small
0677          * difference between v3.00 and 3.01 spec means that CMD19
0678          * tuning is also available for DDR50 mode.
0679          */
0680         if (err && card->host->ios.timing == MMC_TIMING_UHS_DDR50) {
0681             pr_warn("%s: ddr50 tuning failed\n",
0682                 mmc_hostname(card->host));
0683             err = 0;
0684         }
0685     }
0686 
0687 out:
0688     kfree(status);
0689 
0690     return err;
0691 }
0692 
0693 MMC_DEV_ATTR(cid, "%08x%08x%08x%08x\n", card->raw_cid[0], card->raw_cid[1],
0694     card->raw_cid[2], card->raw_cid[3]);
0695 MMC_DEV_ATTR(csd, "%08x%08x%08x%08x\n", card->raw_csd[0], card->raw_csd[1],
0696     card->raw_csd[2], card->raw_csd[3]);
0697 MMC_DEV_ATTR(scr, "%08x%08x\n", card->raw_scr[0], card->raw_scr[1]);
0698 MMC_DEV_ATTR(ssr,
0699     "%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x\n",
0700         card->raw_ssr[0], card->raw_ssr[1], card->raw_ssr[2],
0701         card->raw_ssr[3], card->raw_ssr[4], card->raw_ssr[5],
0702         card->raw_ssr[6], card->raw_ssr[7], card->raw_ssr[8],
0703         card->raw_ssr[9], card->raw_ssr[10], card->raw_ssr[11],
0704         card->raw_ssr[12], card->raw_ssr[13], card->raw_ssr[14],
0705         card->raw_ssr[15]);
0706 MMC_DEV_ATTR(date, "%02d/%04d\n", card->cid.month, card->cid.year);
0707 MMC_DEV_ATTR(erase_size, "%u\n", card->erase_size << 9);
0708 MMC_DEV_ATTR(preferred_erase_size, "%u\n", card->pref_erase << 9);
0709 MMC_DEV_ATTR(fwrev, "0x%x\n", card->cid.fwrev);
0710 MMC_DEV_ATTR(hwrev, "0x%x\n", card->cid.hwrev);
0711 MMC_DEV_ATTR(manfid, "0x%06x\n", card->cid.manfid);
0712 MMC_DEV_ATTR(name, "%s\n", card->cid.prod_name);
0713 MMC_DEV_ATTR(oemid, "0x%04x\n", card->cid.oemid);
0714 MMC_DEV_ATTR(serial, "0x%08x\n", card->cid.serial);
0715 MMC_DEV_ATTR(ocr, "0x%08x\n", card->ocr);
0716 MMC_DEV_ATTR(rca, "0x%04x\n", card->rca);
0717 
0718 
0719 static ssize_t mmc_dsr_show(struct device *dev, struct device_attribute *attr,
0720                 char *buf)
0721 {
0722     struct mmc_card *card = mmc_dev_to_card(dev);
0723     struct mmc_host *host = card->host;
0724 
0725     if (card->csd.dsr_imp && host->dsr_req)
0726         return sysfs_emit(buf, "0x%x\n", host->dsr);
0727     /* return default DSR value */
0728     return sysfs_emit(buf, "0x%x\n", 0x404);
0729 }
0730 
0731 static DEVICE_ATTR(dsr, S_IRUGO, mmc_dsr_show, NULL);
0732 
0733 MMC_DEV_ATTR(vendor, "0x%04x\n", card->cis.vendor);
0734 MMC_DEV_ATTR(device, "0x%04x\n", card->cis.device);
0735 MMC_DEV_ATTR(revision, "%u.%u\n", card->major_rev, card->minor_rev);
0736 
0737 #define sdio_info_attr(num)                                 \
0738 static ssize_t info##num##_show(struct device *dev, struct device_attribute *attr, char *buf)   \
0739 {                                               \
0740     struct mmc_card *card = mmc_dev_to_card(dev);                       \
0741                                                 \
0742     if (num > card->num_info)                               \
0743         return -ENODATA;                                \
0744     if (!card->info[num - 1][0])                                \
0745         return 0;                                   \
0746     return sysfs_emit(buf, "%s\n", card->info[num - 1]);                    \
0747 }                                               \
0748 static DEVICE_ATTR_RO(info##num)
0749 
0750 sdio_info_attr(1);
0751 sdio_info_attr(2);
0752 sdio_info_attr(3);
0753 sdio_info_attr(4);
0754 
0755 static struct attribute *sd_std_attrs[] = {
0756     &dev_attr_vendor.attr,
0757     &dev_attr_device.attr,
0758     &dev_attr_revision.attr,
0759     &dev_attr_info1.attr,
0760     &dev_attr_info2.attr,
0761     &dev_attr_info3.attr,
0762     &dev_attr_info4.attr,
0763     &dev_attr_cid.attr,
0764     &dev_attr_csd.attr,
0765     &dev_attr_scr.attr,
0766     &dev_attr_ssr.attr,
0767     &dev_attr_date.attr,
0768     &dev_attr_erase_size.attr,
0769     &dev_attr_preferred_erase_size.attr,
0770     &dev_attr_fwrev.attr,
0771     &dev_attr_hwrev.attr,
0772     &dev_attr_manfid.attr,
0773     &dev_attr_name.attr,
0774     &dev_attr_oemid.attr,
0775     &dev_attr_serial.attr,
0776     &dev_attr_ocr.attr,
0777     &dev_attr_rca.attr,
0778     &dev_attr_dsr.attr,
0779     NULL,
0780 };
0781 
0782 static umode_t sd_std_is_visible(struct kobject *kobj, struct attribute *attr,
0783                  int index)
0784 {
0785     struct device *dev = kobj_to_dev(kobj);
0786     struct mmc_card *card = mmc_dev_to_card(dev);
0787 
0788     /* CIS vendor and device ids, revision and info string are available only for Combo cards */
0789     if ((attr == &dev_attr_vendor.attr ||
0790          attr == &dev_attr_device.attr ||
0791          attr == &dev_attr_revision.attr ||
0792          attr == &dev_attr_info1.attr ||
0793          attr == &dev_attr_info2.attr ||
0794          attr == &dev_attr_info3.attr ||
0795          attr == &dev_attr_info4.attr
0796         ) &&!mmc_card_sd_combo(card))
0797         return 0;
0798 
0799     return attr->mode;
0800 }
0801 
0802 static const struct attribute_group sd_std_group = {
0803     .attrs = sd_std_attrs,
0804     .is_visible = sd_std_is_visible,
0805 };
0806 __ATTRIBUTE_GROUPS(sd_std);
0807 
0808 struct device_type sd_type = {
0809     .groups = sd_std_groups,
0810 };
0811 
0812 /*
0813  * Fetch CID from card.
0814  */
0815 int mmc_sd_get_cid(struct mmc_host *host, u32 ocr, u32 *cid, u32 *rocr)
0816 {
0817     int err;
0818     u32 max_current;
0819     int retries = 10;
0820     u32 pocr = ocr;
0821 
0822 try_again:
0823     if (!retries) {
0824         ocr &= ~SD_OCR_S18R;
0825         pr_warn("%s: Skipping voltage switch\n", mmc_hostname(host));
0826     }
0827 
0828     /*
0829      * Since we're changing the OCR value, we seem to
0830      * need to tell some cards to go back to the idle
0831      * state.  We wait 1ms to give cards time to
0832      * respond.
0833      */
0834     mmc_go_idle(host);
0835 
0836     /*
0837      * If SD_SEND_IF_COND indicates an SD 2.0
0838      * compliant card and we should set bit 30
0839      * of the ocr to indicate that we can handle
0840      * block-addressed SDHC cards.
0841      */
0842     err = mmc_send_if_cond(host, ocr);
0843     if (!err)
0844         ocr |= SD_OCR_CCS;
0845 
0846     /*
0847      * If the host supports one of UHS-I modes, request the card
0848      * to switch to 1.8V signaling level. If the card has failed
0849      * repeatedly to switch however, skip this.
0850      */
0851     if (retries && mmc_host_uhs(host))
0852         ocr |= SD_OCR_S18R;
0853 
0854     /*
0855      * If the host can supply more than 150mA at current voltage,
0856      * XPC should be set to 1.
0857      */
0858     max_current = sd_get_host_max_current(host);
0859     if (max_current > 150)
0860         ocr |= SD_OCR_XPC;
0861 
0862     err = mmc_send_app_op_cond(host, ocr, rocr);
0863     if (err)
0864         return err;
0865 
0866     /*
0867      * In case the S18A bit is set in the response, let's start the signal
0868      * voltage switch procedure. SPI mode doesn't support CMD11.
0869      * Note that, according to the spec, the S18A bit is not valid unless
0870      * the CCS bit is set as well. We deliberately deviate from the spec in
0871      * regards to this, which allows UHS-I to be supported for SDSC cards.
0872      */
0873     if (!mmc_host_is_spi(host) && (ocr & SD_OCR_S18R) &&
0874         rocr && (*rocr & SD_ROCR_S18A)) {
0875         err = mmc_set_uhs_voltage(host, pocr);
0876         if (err == -EAGAIN) {
0877             retries--;
0878             goto try_again;
0879         } else if (err) {
0880             retries = 0;
0881             goto try_again;
0882         }
0883     }
0884 
0885     err = mmc_send_cid(host, cid);
0886     return err;
0887 }
0888 
0889 int mmc_sd_get_csd(struct mmc_card *card)
0890 {
0891     int err;
0892 
0893     /*
0894      * Fetch CSD from card.
0895      */
0896     err = mmc_send_csd(card, card->raw_csd);
0897     if (err)
0898         return err;
0899 
0900     err = mmc_decode_csd(card);
0901     if (err)
0902         return err;
0903 
0904     return 0;
0905 }
0906 
0907 static int mmc_sd_get_ro(struct mmc_host *host)
0908 {
0909     int ro;
0910 
0911     /*
0912      * Some systems don't feature a write-protect pin and don't need one.
0913      * E.g. because they only have micro-SD card slot. For those systems
0914      * assume that the SD card is always read-write.
0915      */
0916     if (host->caps2 & MMC_CAP2_NO_WRITE_PROTECT)
0917         return 0;
0918 
0919     if (!host->ops->get_ro)
0920         return -1;
0921 
0922     ro = host->ops->get_ro(host);
0923 
0924     return ro;
0925 }
0926 
0927 int mmc_sd_setup_card(struct mmc_host *host, struct mmc_card *card,
0928     bool reinit)
0929 {
0930     int err;
0931 
0932     if (!reinit) {
0933         /*
0934          * Fetch SCR from card.
0935          */
0936         err = mmc_app_send_scr(card);
0937         if (err)
0938             return err;
0939 
0940         err = mmc_decode_scr(card);
0941         if (err)
0942             return err;
0943 
0944         /*
0945          * Fetch and process SD Status register.
0946          */
0947         err = mmc_read_ssr(card);
0948         if (err)
0949             return err;
0950 
0951         /* Erase init depends on CSD and SSR */
0952         mmc_init_erase(card);
0953     }
0954 
0955     /*
0956      * Fetch switch information from card. Note, sd3_bus_mode can change if
0957      * voltage switch outcome changes, so do this always.
0958      */
0959     err = mmc_read_switch(card);
0960     if (err)
0961         return err;
0962 
0963     /*
0964      * For SPI, enable CRC as appropriate.
0965      * This CRC enable is located AFTER the reading of the
0966      * card registers because some SDHC cards are not able
0967      * to provide valid CRCs for non-512-byte blocks.
0968      */
0969     if (mmc_host_is_spi(host)) {
0970         err = mmc_spi_set_crc(host, use_spi_crc);
0971         if (err)
0972             return err;
0973     }
0974 
0975     /*
0976      * Check if read-only switch is active.
0977      */
0978     if (!reinit) {
0979         int ro = mmc_sd_get_ro(host);
0980 
0981         if (ro < 0) {
0982             pr_warn("%s: host does not support reading read-only switch, assuming write-enable\n",
0983                 mmc_hostname(host));
0984         } else if (ro > 0) {
0985             mmc_card_set_readonly(card);
0986         }
0987     }
0988 
0989     return 0;
0990 }
0991 
0992 unsigned mmc_sd_get_max_clock(struct mmc_card *card)
0993 {
0994     unsigned max_dtr = (unsigned int)-1;
0995 
0996     if (mmc_card_hs(card)) {
0997         if (max_dtr > card->sw_caps.hs_max_dtr)
0998             max_dtr = card->sw_caps.hs_max_dtr;
0999     } else if (max_dtr > card->csd.max_dtr) {
1000         max_dtr = card->csd.max_dtr;
1001     }
1002 
1003     return max_dtr;
1004 }
1005 
1006 static bool mmc_sd_card_using_v18(struct mmc_card *card)
1007 {
1008     /*
1009      * According to the SD spec., the Bus Speed Mode (function group 1) bits
1010      * 2 to 4 are zero if the card is initialized at 3.3V signal level. Thus
1011      * they can be used to determine if the card has already switched to
1012      * 1.8V signaling.
1013      */
1014     return card->sw_caps.sd3_bus_mode &
1015            (SD_MODE_UHS_SDR50 | SD_MODE_UHS_SDR104 | SD_MODE_UHS_DDR50);
1016 }
1017 
1018 static int sd_write_ext_reg(struct mmc_card *card, u8 fno, u8 page, u16 offset,
1019                 u8 reg_data)
1020 {
1021     struct mmc_host *host = card->host;
1022     struct mmc_request mrq = {};
1023     struct mmc_command cmd = {};
1024     struct mmc_data data = {};
1025     struct scatterlist sg;
1026     u8 *reg_buf;
1027 
1028     reg_buf = kzalloc(512, GFP_KERNEL);
1029     if (!reg_buf)
1030         return -ENOMEM;
1031 
1032     mrq.cmd = &cmd;
1033     mrq.data = &data;
1034 
1035     /*
1036      * Arguments of CMD49:
1037      * [31:31] MIO (0 = memory).
1038      * [30:27] FNO (function number).
1039      * [26:26] MW - mask write mode (0 = disable).
1040      * [25:18] page number.
1041      * [17:9] offset address.
1042      * [8:0] length (0 = 1 byte).
1043      */
1044     cmd.arg = fno << 27 | page << 18 | offset << 9;
1045 
1046     /* The first byte in the buffer is the data to be written. */
1047     reg_buf[0] = reg_data;
1048 
1049     data.flags = MMC_DATA_WRITE;
1050     data.blksz = 512;
1051     data.blocks = 1;
1052     data.sg = &sg;
1053     data.sg_len = 1;
1054     sg_init_one(&sg, reg_buf, 512);
1055 
1056     cmd.opcode = SD_WRITE_EXTR_SINGLE;
1057     cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1058 
1059     mmc_set_data_timeout(&data, card);
1060     mmc_wait_for_req(host, &mrq);
1061 
1062     kfree(reg_buf);
1063 
1064     /*
1065      * Note that, the SD card is allowed to signal busy on DAT0 up to 1s
1066      * after the CMD49. Although, let's leave this to be managed by the
1067      * caller.
1068      */
1069 
1070     if (cmd.error)
1071         return cmd.error;
1072     if (data.error)
1073         return data.error;
1074 
1075     return 0;
1076 }
1077 
1078 static int sd_read_ext_reg(struct mmc_card *card, u8 fno, u8 page,
1079                u16 offset, u16 len, u8 *reg_buf)
1080 {
1081     u32 cmd_args;
1082 
1083     /*
1084      * Command arguments of CMD48:
1085      * [31:31] MIO (0 = memory).
1086      * [30:27] FNO (function number).
1087      * [26:26] reserved (0).
1088      * [25:18] page number.
1089      * [17:9] offset address.
1090      * [8:0] length (0 = 1 byte, 1ff = 512 bytes).
1091      */
1092     cmd_args = fno << 27 | page << 18 | offset << 9 | (len -1);
1093 
1094     return mmc_send_adtc_data(card, card->host, SD_READ_EXTR_SINGLE,
1095                   cmd_args, reg_buf, 512);
1096 }
1097 
1098 static int sd_parse_ext_reg_power(struct mmc_card *card, u8 fno, u8 page,
1099                   u16 offset)
1100 {
1101     int err;
1102     u8 *reg_buf;
1103 
1104     reg_buf = kzalloc(512, GFP_KERNEL);
1105     if (!reg_buf)
1106         return -ENOMEM;
1107 
1108     /* Read the extension register for power management function. */
1109     err = sd_read_ext_reg(card, fno, page, offset, 512, reg_buf);
1110     if (err) {
1111         pr_warn("%s: error %d reading PM func of ext reg\n",
1112             mmc_hostname(card->host), err);
1113         goto out;
1114     }
1115 
1116     /* PM revision consists of 4 bits. */
1117     card->ext_power.rev = reg_buf[0] & 0xf;
1118 
1119     /* Power Off Notification support at bit 4. */
1120     if (reg_buf[1] & BIT(4))
1121         card->ext_power.feature_support |= SD_EXT_POWER_OFF_NOTIFY;
1122 
1123     /* Power Sustenance support at bit 5. */
1124     if (reg_buf[1] & BIT(5))
1125         card->ext_power.feature_support |= SD_EXT_POWER_SUSTENANCE;
1126 
1127     /* Power Down Mode support at bit 6. */
1128     if (reg_buf[1] & BIT(6))
1129         card->ext_power.feature_support |= SD_EXT_POWER_DOWN_MODE;
1130 
1131     card->ext_power.fno = fno;
1132     card->ext_power.page = page;
1133     card->ext_power.offset = offset;
1134 
1135 out:
1136     kfree(reg_buf);
1137     return err;
1138 }
1139 
1140 static int sd_parse_ext_reg_perf(struct mmc_card *card, u8 fno, u8 page,
1141                  u16 offset)
1142 {
1143     int err;
1144     u8 *reg_buf;
1145 
1146     reg_buf = kzalloc(512, GFP_KERNEL);
1147     if (!reg_buf)
1148         return -ENOMEM;
1149 
1150     err = sd_read_ext_reg(card, fno, page, offset, 512, reg_buf);
1151     if (err) {
1152         pr_warn("%s: error %d reading PERF func of ext reg\n",
1153             mmc_hostname(card->host), err);
1154         goto out;
1155     }
1156 
1157     /* PERF revision. */
1158     card->ext_perf.rev = reg_buf[0];
1159 
1160     /* FX_EVENT support at bit 0. */
1161     if (reg_buf[1] & BIT(0))
1162         card->ext_perf.feature_support |= SD_EXT_PERF_FX_EVENT;
1163 
1164     /* Card initiated self-maintenance support at bit 0. */
1165     if (reg_buf[2] & BIT(0))
1166         card->ext_perf.feature_support |= SD_EXT_PERF_CARD_MAINT;
1167 
1168     /* Host initiated self-maintenance support at bit 1. */
1169     if (reg_buf[2] & BIT(1))
1170         card->ext_perf.feature_support |= SD_EXT_PERF_HOST_MAINT;
1171 
1172     /* Cache support at bit 0. */
1173     if (reg_buf[4] & BIT(0))
1174         card->ext_perf.feature_support |= SD_EXT_PERF_CACHE;
1175 
1176     /* Command queue support indicated via queue depth bits (0 to 4). */
1177     if (reg_buf[6] & 0x1f)
1178         card->ext_perf.feature_support |= SD_EXT_PERF_CMD_QUEUE;
1179 
1180     card->ext_perf.fno = fno;
1181     card->ext_perf.page = page;
1182     card->ext_perf.offset = offset;
1183 
1184 out:
1185     kfree(reg_buf);
1186     return err;
1187 }
1188 
1189 static int sd_parse_ext_reg(struct mmc_card *card, u8 *gen_info_buf,
1190                 u16 *next_ext_addr)
1191 {
1192     u8 num_regs, fno, page;
1193     u16 sfc, offset, ext = *next_ext_addr;
1194     u32 reg_addr;
1195 
1196     /*
1197      * Parse only one register set per extension, as that is sufficient to
1198      * support the standard functions. This means another 48 bytes in the
1199      * buffer must be available.
1200      */
1201     if (ext + 48 > 512)
1202         return -EFAULT;
1203 
1204     /* Standard Function Code */
1205     memcpy(&sfc, &gen_info_buf[ext], 2);
1206 
1207     /* Address to the next extension. */
1208     memcpy(next_ext_addr, &gen_info_buf[ext + 40], 2);
1209 
1210     /* Number of registers for this extension. */
1211     num_regs = gen_info_buf[ext + 42];
1212 
1213     /* We support only one register per extension. */
1214     if (num_regs != 1)
1215         return 0;
1216 
1217     /* Extension register address. */
1218     memcpy(&reg_addr, &gen_info_buf[ext + 44], 4);
1219 
1220     /* 9 bits (0 to 8) contains the offset address. */
1221     offset = reg_addr & 0x1ff;
1222 
1223     /* 8 bits (9 to 16) contains the page number. */
1224     page = reg_addr >> 9 & 0xff ;
1225 
1226     /* 4 bits (18 to 21) contains the function number. */
1227     fno = reg_addr >> 18 & 0xf;
1228 
1229     /* Standard Function Code for power management. */
1230     if (sfc == 0x1)
1231         return sd_parse_ext_reg_power(card, fno, page, offset);
1232 
1233     /* Standard Function Code for performance enhancement. */
1234     if (sfc == 0x2)
1235         return sd_parse_ext_reg_perf(card, fno, page, offset);
1236 
1237     return 0;
1238 }
1239 
1240 static int sd_read_ext_regs(struct mmc_card *card)
1241 {
1242     int err, i;
1243     u8 num_ext, *gen_info_buf;
1244     u16 rev, len, next_ext_addr;
1245 
1246     if (mmc_host_is_spi(card->host))
1247         return 0;
1248 
1249     if (!(card->scr.cmds & SD_SCR_CMD48_SUPPORT))
1250         return 0;
1251 
1252     gen_info_buf = kzalloc(512, GFP_KERNEL);
1253     if (!gen_info_buf)
1254         return -ENOMEM;
1255 
1256     /*
1257      * Read 512 bytes of general info, which is found at function number 0,
1258      * at page 0 and with no offset.
1259      */
1260     err = sd_read_ext_reg(card, 0, 0, 0, 512, gen_info_buf);
1261     if (err) {
1262         pr_warn("%s: error %d reading general info of SD ext reg\n",
1263             mmc_hostname(card->host), err);
1264         goto out;
1265     }
1266 
1267     /* General info structure revision. */
1268     memcpy(&rev, &gen_info_buf[0], 2);
1269 
1270     /* Length of general info in bytes. */
1271     memcpy(&len, &gen_info_buf[2], 2);
1272 
1273     /* Number of extensions to be find. */
1274     num_ext = gen_info_buf[4];
1275 
1276     /* We support revision 0, but limit it to 512 bytes for simplicity. */
1277     if (rev != 0 || len > 512) {
1278         pr_warn("%s: non-supported SD ext reg layout\n",
1279             mmc_hostname(card->host));
1280         goto out;
1281     }
1282 
1283     /*
1284      * Parse the extension registers. The first extension should start
1285      * immediately after the general info header (16 bytes).
1286      */
1287     next_ext_addr = 16;
1288     for (i = 0; i < num_ext; i++) {
1289         err = sd_parse_ext_reg(card, gen_info_buf, &next_ext_addr);
1290         if (err) {
1291             pr_warn("%s: error %d parsing SD ext reg\n",
1292                 mmc_hostname(card->host), err);
1293             goto out;
1294         }
1295     }
1296 
1297 out:
1298     kfree(gen_info_buf);
1299     return err;
1300 }
1301 
1302 static bool sd_cache_enabled(struct mmc_host *host)
1303 {
1304     return host->card->ext_perf.feature_enabled & SD_EXT_PERF_CACHE;
1305 }
1306 
1307 static int sd_flush_cache(struct mmc_host *host)
1308 {
1309     struct mmc_card *card = host->card;
1310     u8 *reg_buf, fno, page;
1311     u16 offset;
1312     int err;
1313 
1314     if (!sd_cache_enabled(host))
1315         return 0;
1316 
1317     reg_buf = kzalloc(512, GFP_KERNEL);
1318     if (!reg_buf)
1319         return -ENOMEM;
1320 
1321     /*
1322      * Set Flush Cache at bit 0 in the performance enhancement register at
1323      * 261 bytes offset.
1324      */
1325     fno = card->ext_perf.fno;
1326     page = card->ext_perf.page;
1327     offset = card->ext_perf.offset + 261;
1328 
1329     err = sd_write_ext_reg(card, fno, page, offset, BIT(0));
1330     if (err) {
1331         pr_warn("%s: error %d writing Cache Flush bit\n",
1332             mmc_hostname(host), err);
1333         goto out;
1334     }
1335 
1336     err = mmc_poll_for_busy(card, SD_WRITE_EXTR_SINGLE_TIMEOUT_MS, false,
1337                 MMC_BUSY_EXTR_SINGLE);
1338     if (err)
1339         goto out;
1340 
1341     /*
1342      * Read the Flush Cache bit. The card shall reset it, to confirm that
1343      * it's has completed the flushing of the cache.
1344      */
1345     err = sd_read_ext_reg(card, fno, page, offset, 1, reg_buf);
1346     if (err) {
1347         pr_warn("%s: error %d reading Cache Flush bit\n",
1348             mmc_hostname(host), err);
1349         goto out;
1350     }
1351 
1352     if (reg_buf[0] & BIT(0))
1353         err = -ETIMEDOUT;
1354 out:
1355     kfree(reg_buf);
1356     return err;
1357 }
1358 
1359 static int sd_enable_cache(struct mmc_card *card)
1360 {
1361     u8 *reg_buf;
1362     int err;
1363 
1364     card->ext_perf.feature_enabled &= ~SD_EXT_PERF_CACHE;
1365 
1366     reg_buf = kzalloc(512, GFP_KERNEL);
1367     if (!reg_buf)
1368         return -ENOMEM;
1369 
1370     /*
1371      * Set Cache Enable at bit 0 in the performance enhancement register at
1372      * 260 bytes offset.
1373      */
1374     err = sd_write_ext_reg(card, card->ext_perf.fno, card->ext_perf.page,
1375                    card->ext_perf.offset + 260, BIT(0));
1376     if (err) {
1377         pr_warn("%s: error %d writing Cache Enable bit\n",
1378             mmc_hostname(card->host), err);
1379         goto out;
1380     }
1381 
1382     err = mmc_poll_for_busy(card, SD_WRITE_EXTR_SINGLE_TIMEOUT_MS, false,
1383                 MMC_BUSY_EXTR_SINGLE);
1384     if (!err)
1385         card->ext_perf.feature_enabled |= SD_EXT_PERF_CACHE;
1386 
1387 out:
1388     kfree(reg_buf);
1389     return err;
1390 }
1391 
1392 /*
1393  * Handle the detection and initialisation of a card.
1394  *
1395  * In the case of a resume, "oldcard" will contain the card
1396  * we're trying to reinitialise.
1397  */
1398 static int mmc_sd_init_card(struct mmc_host *host, u32 ocr,
1399     struct mmc_card *oldcard)
1400 {
1401     struct mmc_card *card;
1402     int err;
1403     u32 cid[4];
1404     u32 rocr = 0;
1405     bool v18_fixup_failed = false;
1406 
1407     WARN_ON(!host->claimed);
1408 retry:
1409     err = mmc_sd_get_cid(host, ocr, cid, &rocr);
1410     if (err)
1411         return err;
1412 
1413     if (oldcard) {
1414         if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0) {
1415             pr_debug("%s: Perhaps the card was replaced\n",
1416                 mmc_hostname(host));
1417             return -ENOENT;
1418         }
1419 
1420         card = oldcard;
1421     } else {
1422         /*
1423          * Allocate card structure.
1424          */
1425         card = mmc_alloc_card(host, &sd_type);
1426         if (IS_ERR(card))
1427             return PTR_ERR(card);
1428 
1429         card->ocr = ocr;
1430         card->type = MMC_TYPE_SD;
1431         memcpy(card->raw_cid, cid, sizeof(card->raw_cid));
1432     }
1433 
1434     /*
1435      * Call the optional HC's init_card function to handle quirks.
1436      */
1437     if (host->ops->init_card)
1438         host->ops->init_card(host, card);
1439 
1440     /*
1441      * For native busses:  get card RCA and quit open drain mode.
1442      */
1443     if (!mmc_host_is_spi(host)) {
1444         err = mmc_send_relative_addr(host, &card->rca);
1445         if (err)
1446             goto free_card;
1447     }
1448 
1449     if (!oldcard) {
1450         err = mmc_sd_get_csd(card);
1451         if (err)
1452             goto free_card;
1453 
1454         mmc_decode_cid(card);
1455     }
1456 
1457     /*
1458      * handling only for cards supporting DSR and hosts requesting
1459      * DSR configuration
1460      */
1461     if (card->csd.dsr_imp && host->dsr_req)
1462         mmc_set_dsr(host);
1463 
1464     /*
1465      * Select card, as all following commands rely on that.
1466      */
1467     if (!mmc_host_is_spi(host)) {
1468         err = mmc_select_card(card);
1469         if (err)
1470             goto free_card;
1471     }
1472 
1473     err = mmc_sd_setup_card(host, card, oldcard != NULL);
1474     if (err)
1475         goto free_card;
1476 
1477     /*
1478      * If the card has not been power cycled, it may still be using 1.8V
1479      * signaling. Detect that situation and try to initialize a UHS-I (1.8V)
1480      * transfer mode.
1481      */
1482     if (!v18_fixup_failed && !mmc_host_is_spi(host) && mmc_host_uhs(host) &&
1483         mmc_sd_card_using_v18(card) &&
1484         host->ios.signal_voltage != MMC_SIGNAL_VOLTAGE_180) {
1485         if (mmc_host_set_uhs_voltage(host) ||
1486             mmc_sd_init_uhs_card(card)) {
1487             v18_fixup_failed = true;
1488             mmc_power_cycle(host, ocr);
1489             if (!oldcard)
1490                 mmc_remove_card(card);
1491             goto retry;
1492         }
1493         goto cont;
1494     }
1495 
1496     /* Initialization sequence for UHS-I cards */
1497     if (rocr & SD_ROCR_S18A && mmc_host_uhs(host)) {
1498         err = mmc_sd_init_uhs_card(card);
1499         if (err)
1500             goto free_card;
1501     } else {
1502         /*
1503          * Attempt to change to high-speed (if supported)
1504          */
1505         err = mmc_sd_switch_hs(card);
1506         if (err > 0)
1507             mmc_set_timing(card->host, MMC_TIMING_SD_HS);
1508         else if (err)
1509             goto free_card;
1510 
1511         /*
1512          * Set bus speed.
1513          */
1514         mmc_set_clock(host, mmc_sd_get_max_clock(card));
1515 
1516         /*
1517          * Switch to wider bus (if supported).
1518          */
1519         if ((host->caps & MMC_CAP_4_BIT_DATA) &&
1520             (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) {
1521             err = mmc_app_set_bus_width(card, MMC_BUS_WIDTH_4);
1522             if (err)
1523                 goto free_card;
1524 
1525             mmc_set_bus_width(host, MMC_BUS_WIDTH_4);
1526         }
1527     }
1528 cont:
1529     if (!oldcard) {
1530         /* Read/parse the extension registers. */
1531         err = sd_read_ext_regs(card);
1532         if (err)
1533             goto free_card;
1534     }
1535 
1536     /* Enable internal SD cache if supported. */
1537     if (card->ext_perf.feature_support & SD_EXT_PERF_CACHE) {
1538         err = sd_enable_cache(card);
1539         if (err)
1540             goto free_card;
1541     }
1542 
1543     if (host->cqe_ops && !host->cqe_enabled) {
1544         err = host->cqe_ops->cqe_enable(host, card);
1545         if (!err) {
1546             host->cqe_enabled = true;
1547             host->hsq_enabled = true;
1548             pr_info("%s: Host Software Queue enabled\n",
1549                 mmc_hostname(host));
1550         }
1551     }
1552 
1553     if (host->caps2 & MMC_CAP2_AVOID_3_3V &&
1554         host->ios.signal_voltage == MMC_SIGNAL_VOLTAGE_330) {
1555         pr_err("%s: Host failed to negotiate down from 3.3V\n",
1556             mmc_hostname(host));
1557         err = -EINVAL;
1558         goto free_card;
1559     }
1560 
1561     host->card = card;
1562     return 0;
1563 
1564 free_card:
1565     if (!oldcard)
1566         mmc_remove_card(card);
1567 
1568     return err;
1569 }
1570 
1571 /*
1572  * Host is being removed. Free up the current card.
1573  */
1574 static void mmc_sd_remove(struct mmc_host *host)
1575 {
1576     mmc_remove_card(host->card);
1577     host->card = NULL;
1578 }
1579 
1580 /*
1581  * Card detection - card is alive.
1582  */
1583 static int mmc_sd_alive(struct mmc_host *host)
1584 {
1585     return mmc_send_status(host->card, NULL);
1586 }
1587 
1588 /*
1589  * Card detection callback from host.
1590  */
1591 static void mmc_sd_detect(struct mmc_host *host)
1592 {
1593     int err;
1594 
1595     mmc_get_card(host->card, NULL);
1596 
1597     /*
1598      * Just check if our card has been removed.
1599      */
1600     err = _mmc_detect_card_removed(host);
1601 
1602     mmc_put_card(host->card, NULL);
1603 
1604     if (err) {
1605         mmc_sd_remove(host);
1606 
1607         mmc_claim_host(host);
1608         mmc_detach_bus(host);
1609         mmc_power_off(host);
1610         mmc_release_host(host);
1611     }
1612 }
1613 
1614 static int sd_can_poweroff_notify(struct mmc_card *card)
1615 {
1616     return card->ext_power.feature_support & SD_EXT_POWER_OFF_NOTIFY;
1617 }
1618 
1619 static int sd_busy_poweroff_notify_cb(void *cb_data, bool *busy)
1620 {
1621     struct sd_busy_data *data = cb_data;
1622     struct mmc_card *card = data->card;
1623     int err;
1624 
1625     /*
1626      * Read the status register for the power management function. It's at
1627      * one byte offset and is one byte long. The Power Off Notification
1628      * Ready is bit 0.
1629      */
1630     err = sd_read_ext_reg(card, card->ext_power.fno, card->ext_power.page,
1631                   card->ext_power.offset + 1, 1, data->reg_buf);
1632     if (err) {
1633         pr_warn("%s: error %d reading status reg of PM func\n",
1634             mmc_hostname(card->host), err);
1635         return err;
1636     }
1637 
1638     *busy = !(data->reg_buf[0] & BIT(0));
1639     return 0;
1640 }
1641 
1642 static int sd_poweroff_notify(struct mmc_card *card)
1643 {
1644     struct sd_busy_data cb_data;
1645     u8 *reg_buf;
1646     int err;
1647 
1648     reg_buf = kzalloc(512, GFP_KERNEL);
1649     if (!reg_buf)
1650         return -ENOMEM;
1651 
1652     /*
1653      * Set the Power Off Notification bit in the power management settings
1654      * register at 2 bytes offset.
1655      */
1656     err = sd_write_ext_reg(card, card->ext_power.fno, card->ext_power.page,
1657                    card->ext_power.offset + 2, BIT(0));
1658     if (err) {
1659         pr_warn("%s: error %d writing Power Off Notify bit\n",
1660             mmc_hostname(card->host), err);
1661         goto out;
1662     }
1663 
1664     /* Find out when the command is completed. */
1665     err = mmc_poll_for_busy(card, SD_WRITE_EXTR_SINGLE_TIMEOUT_MS, false,
1666                 MMC_BUSY_EXTR_SINGLE);
1667     if (err)
1668         goto out;
1669 
1670     cb_data.card = card;
1671     cb_data.reg_buf = reg_buf;
1672     err = __mmc_poll_for_busy(card->host, 0, SD_POWEROFF_NOTIFY_TIMEOUT_MS,
1673                   &sd_busy_poweroff_notify_cb, &cb_data);
1674 
1675 out:
1676     kfree(reg_buf);
1677     return err;
1678 }
1679 
1680 static int _mmc_sd_suspend(struct mmc_host *host)
1681 {
1682     struct mmc_card *card = host->card;
1683     int err = 0;
1684 
1685     mmc_claim_host(host);
1686 
1687     if (mmc_card_suspended(card))
1688         goto out;
1689 
1690     if (sd_can_poweroff_notify(card))
1691         err = sd_poweroff_notify(card);
1692     else if (!mmc_host_is_spi(host))
1693         err = mmc_deselect_cards(host);
1694 
1695     if (!err) {
1696         mmc_power_off(host);
1697         mmc_card_set_suspended(card);
1698     }
1699 
1700 out:
1701     mmc_release_host(host);
1702     return err;
1703 }
1704 
1705 /*
1706  * Callback for suspend
1707  */
1708 static int mmc_sd_suspend(struct mmc_host *host)
1709 {
1710     int err;
1711 
1712     err = _mmc_sd_suspend(host);
1713     if (!err) {
1714         pm_runtime_disable(&host->card->dev);
1715         pm_runtime_set_suspended(&host->card->dev);
1716     }
1717 
1718     return err;
1719 }
1720 
1721 /*
1722  * This function tries to determine if the same card is still present
1723  * and, if so, restore all state to it.
1724  */
1725 static int _mmc_sd_resume(struct mmc_host *host)
1726 {
1727     int err = 0;
1728 
1729     mmc_claim_host(host);
1730 
1731     if (!mmc_card_suspended(host->card))
1732         goto out;
1733 
1734     mmc_power_up(host, host->card->ocr);
1735     err = mmc_sd_init_card(host, host->card->ocr, host->card);
1736     mmc_card_clr_suspended(host->card);
1737 
1738 out:
1739     mmc_release_host(host);
1740     return err;
1741 }
1742 
1743 /*
1744  * Callback for resume
1745  */
1746 static int mmc_sd_resume(struct mmc_host *host)
1747 {
1748     pm_runtime_enable(&host->card->dev);
1749     return 0;
1750 }
1751 
1752 /*
1753  * Callback for runtime_suspend.
1754  */
1755 static int mmc_sd_runtime_suspend(struct mmc_host *host)
1756 {
1757     int err;
1758 
1759     if (!(host->caps & MMC_CAP_AGGRESSIVE_PM))
1760         return 0;
1761 
1762     err = _mmc_sd_suspend(host);
1763     if (err)
1764         pr_err("%s: error %d doing aggressive suspend\n",
1765             mmc_hostname(host), err);
1766 
1767     return err;
1768 }
1769 
1770 /*
1771  * Callback for runtime_resume.
1772  */
1773 static int mmc_sd_runtime_resume(struct mmc_host *host)
1774 {
1775     int err;
1776 
1777     err = _mmc_sd_resume(host);
1778     if (err && err != -ENOMEDIUM)
1779         pr_err("%s: error %d doing runtime resume\n",
1780             mmc_hostname(host), err);
1781 
1782     return 0;
1783 }
1784 
1785 static int mmc_sd_hw_reset(struct mmc_host *host)
1786 {
1787     mmc_power_cycle(host, host->card->ocr);
1788     return mmc_sd_init_card(host, host->card->ocr, host->card);
1789 }
1790 
1791 static const struct mmc_bus_ops mmc_sd_ops = {
1792     .remove = mmc_sd_remove,
1793     .detect = mmc_sd_detect,
1794     .runtime_suspend = mmc_sd_runtime_suspend,
1795     .runtime_resume = mmc_sd_runtime_resume,
1796     .suspend = mmc_sd_suspend,
1797     .resume = mmc_sd_resume,
1798     .alive = mmc_sd_alive,
1799     .shutdown = mmc_sd_suspend,
1800     .hw_reset = mmc_sd_hw_reset,
1801     .cache_enabled = sd_cache_enabled,
1802     .flush_cache = sd_flush_cache,
1803 };
1804 
1805 /*
1806  * Starting point for SD card init.
1807  */
1808 int mmc_attach_sd(struct mmc_host *host)
1809 {
1810     int err;
1811     u32 ocr, rocr;
1812 
1813     WARN_ON(!host->claimed);
1814 
1815     err = mmc_send_app_op_cond(host, 0, &ocr);
1816     if (err)
1817         return err;
1818 
1819     mmc_attach_bus(host, &mmc_sd_ops);
1820     if (host->ocr_avail_sd)
1821         host->ocr_avail = host->ocr_avail_sd;
1822 
1823     /*
1824      * We need to get OCR a different way for SPI.
1825      */
1826     if (mmc_host_is_spi(host)) {
1827         mmc_go_idle(host);
1828 
1829         err = mmc_spi_read_ocr(host, 0, &ocr);
1830         if (err)
1831             goto err;
1832     }
1833 
1834     /*
1835      * Some SD cards claims an out of spec VDD voltage range. Let's treat
1836      * these bits as being in-valid and especially also bit7.
1837      */
1838     ocr &= ~0x7FFF;
1839 
1840     rocr = mmc_select_voltage(host, ocr);
1841 
1842     /*
1843      * Can we support the voltage(s) of the card(s)?
1844      */
1845     if (!rocr) {
1846         err = -EINVAL;
1847         goto err;
1848     }
1849 
1850     /*
1851      * Detect and init the card.
1852      */
1853     err = mmc_sd_init_card(host, rocr, NULL);
1854     if (err)
1855         goto err;
1856 
1857     mmc_release_host(host);
1858     err = mmc_add_card(host->card);
1859     if (err)
1860         goto remove_card;
1861 
1862     mmc_claim_host(host);
1863     return 0;
1864 
1865 remove_card:
1866     mmc_remove_card(host->card);
1867     host->card = NULL;
1868     mmc_claim_host(host);
1869 err:
1870     mmc_detach_bus(host);
1871 
1872     pr_err("%s: error %d whilst initialising SD card\n",
1873         mmc_hostname(host), err);
1874 
1875     return err;
1876 }