linux/drivers/mmc/core/mmc.c
<<
>>
Prefs
   1/*
   2 *  linux/drivers/mmc/core/mmc.c
   3 *
   4 *  Copyright (C) 2003-2004 Russell King, All Rights Reserved.
   5 *  Copyright (C) 2005-2007 Pierre Ossman, All Rights Reserved.
   6 *  MMCv4 support Copyright (C) 2006 Philip Langdale, All Rights Reserved.
   7 *
   8 * This program is free software; you can redistribute it and/or modify
   9 * it under the terms of the GNU General Public License version 2 as
  10 * published by the Free Software Foundation.
  11 */
  12
  13#include <linux/err.h>
  14#include <linux/of.h>
  15#include <linux/slab.h>
  16#include <linux/stat.h>
  17#include <linux/pm_runtime.h>
  18
  19#include <linux/mmc/host.h>
  20#include <linux/mmc/card.h>
  21#include <linux/mmc/mmc.h>
  22
  23#include "core.h"
  24#include "card.h"
  25#include "host.h"
  26#include "bus.h"
  27#include "mmc_ops.h"
  28#include "quirks.h"
  29#include "sd_ops.h"
  30
  31#define DEFAULT_CMD6_TIMEOUT_MS 500
  32
  33static const unsigned int tran_exp[] = {
  34        10000,          100000,         1000000,        10000000,
  35        0,              0,              0,              0
  36};
  37
  38static const unsigned char tran_mant[] = {
  39        0,      10,     12,     13,     15,     20,     25,     30,
  40        35,     40,     45,     50,     55,     60,     70,     80,
  41};
  42
  43static const unsigned int taac_exp[] = {
  44        1,      10,     100,    1000,   10000,  100000, 1000000, 10000000,
  45};
  46
  47static const unsigned int taac_mant[] = {
  48        0,      10,     12,     13,     15,     20,     25,     30,
  49        35,     40,     45,     50,     55,     60,     70,     80,
  50};
  51
  52#define UNSTUFF_BITS(resp,start,size)                                   \
  53        ({                                                              \
  54                const int __size = size;                                \
  55                const u32 __mask = (__size < 32 ? 1 << __size : 0) - 1; \
  56                const int __off = 3 - ((start) / 32);                   \
  57                const int __shft = (start) & 31;                        \
  58                u32 __res;                                              \
  59                                                                        \
  60                __res = resp[__off] >> __shft;                          \
  61                if (__size + __shft > 32)                               \
  62                        __res |= resp[__off-1] << ((32 - __shft) % 32); \
  63                __res & __mask;                                         \
  64        })
  65
  66/*
  67 * Given the decoded CSD structure, decode the raw CID to our CID structure.
  68 */
  69static int mmc_decode_cid(struct mmc_card *card)
  70{
  71        u32 *resp = card->raw_cid;
  72
  73        /*
  74         * The selection of the format here is based upon published
  75         * specs from sandisk and from what people have reported.
  76         */
  77        switch (card->csd.mmca_vsn) {
  78        case 0: /* MMC v1.0 - v1.2 */
  79        case 1: /* MMC v1.4 */
  80                card->cid.manfid        = UNSTUFF_BITS(resp, 104, 24);
  81                card->cid.prod_name[0]  = UNSTUFF_BITS(resp, 96, 8);
  82                card->cid.prod_name[1]  = UNSTUFF_BITS(resp, 88, 8);
  83                card->cid.prod_name[2]  = UNSTUFF_BITS(resp, 80, 8);
  84                card->cid.prod_name[3]  = UNSTUFF_BITS(resp, 72, 8);
  85                card->cid.prod_name[4]  = UNSTUFF_BITS(resp, 64, 8);
  86                card->cid.prod_name[5]  = UNSTUFF_BITS(resp, 56, 8);
  87                card->cid.prod_name[6]  = UNSTUFF_BITS(resp, 48, 8);
  88                card->cid.hwrev         = UNSTUFF_BITS(resp, 44, 4);
  89                card->cid.fwrev         = UNSTUFF_BITS(resp, 40, 4);
  90                card->cid.serial        = UNSTUFF_BITS(resp, 16, 24);
  91                card->cid.month         = UNSTUFF_BITS(resp, 12, 4);
  92                card->cid.year          = UNSTUFF_BITS(resp, 8, 4) + 1997;
  93                break;
  94
  95        case 2: /* MMC v2.0 - v2.2 */
  96        case 3: /* MMC v3.1 - v3.3 */
  97        case 4: /* MMC v4 */
  98                card->cid.manfid        = UNSTUFF_BITS(resp, 120, 8);
  99                card->cid.oemid         = UNSTUFF_BITS(resp, 104, 16);
 100                card->cid.prod_name[0]  = UNSTUFF_BITS(resp, 96, 8);
 101                card->cid.prod_name[1]  = UNSTUFF_BITS(resp, 88, 8);
 102                card->cid.prod_name[2]  = UNSTUFF_BITS(resp, 80, 8);
 103                card->cid.prod_name[3]  = UNSTUFF_BITS(resp, 72, 8);
 104                card->cid.prod_name[4]  = UNSTUFF_BITS(resp, 64, 8);
 105                card->cid.prod_name[5]  = UNSTUFF_BITS(resp, 56, 8);
 106                card->cid.prv           = UNSTUFF_BITS(resp, 48, 8);
 107                card->cid.serial        = UNSTUFF_BITS(resp, 16, 32);
 108                card->cid.month         = UNSTUFF_BITS(resp, 12, 4);
 109                card->cid.year          = UNSTUFF_BITS(resp, 8, 4) + 1997;
 110                break;
 111
 112        default:
 113                pr_err("%s: card has unknown MMCA version %d\n",
 114                        mmc_hostname(card->host), card->csd.mmca_vsn);
 115                return -EINVAL;
 116        }
 117
 118        return 0;
 119}
 120
 121static void mmc_set_erase_size(struct mmc_card *card)
 122{
 123        if (card->ext_csd.erase_group_def & 1)
 124                card->erase_size = card->ext_csd.hc_erase_size;
 125        else
 126                card->erase_size = card->csd.erase_size;
 127
 128        mmc_init_erase(card);
 129}
 130
 131/*
 132 * Given a 128-bit response, decode to our card CSD structure.
 133 */
 134static int mmc_decode_csd(struct mmc_card *card)
 135{
 136        struct mmc_csd *csd = &card->csd;
 137        unsigned int e, m, a, b;
 138        u32 *resp = card->raw_csd;
 139
 140        /*
 141         * We only understand CSD structure v1.1 and v1.2.
 142         * v1.2 has extra information in bits 15, 11 and 10.
 143         * We also support eMMC v4.4 & v4.41.
 144         */
 145        csd->structure = UNSTUFF_BITS(resp, 126, 2);
 146        if (csd->structure == 0) {
 147                pr_err("%s: unrecognised CSD structure version %d\n",
 148                        mmc_hostname(card->host), csd->structure);
 149                return -EINVAL;
 150        }
 151
 152        csd->mmca_vsn    = UNSTUFF_BITS(resp, 122, 4);
 153        m = UNSTUFF_BITS(resp, 115, 4);
 154        e = UNSTUFF_BITS(resp, 112, 3);
 155        csd->taac_ns     = (taac_exp[e] * taac_mant[m] + 9) / 10;
 156        csd->taac_clks   = UNSTUFF_BITS(resp, 104, 8) * 100;
 157
 158        m = UNSTUFF_BITS(resp, 99, 4);
 159        e = UNSTUFF_BITS(resp, 96, 3);
 160        csd->max_dtr      = tran_exp[e] * tran_mant[m];
 161        csd->cmdclass     = UNSTUFF_BITS(resp, 84, 12);
 162
 163        e = UNSTUFF_BITS(resp, 47, 3);
 164        m = UNSTUFF_BITS(resp, 62, 12);
 165        csd->capacity     = (1 + m) << (e + 2);
 166
 167        csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4);
 168        csd->read_partial = UNSTUFF_BITS(resp, 79, 1);
 169        csd->write_misalign = UNSTUFF_BITS(resp, 78, 1);
 170        csd->read_misalign = UNSTUFF_BITS(resp, 77, 1);
 171        csd->dsr_imp = UNSTUFF_BITS(resp, 76, 1);
 172        csd->r2w_factor = UNSTUFF_BITS(resp, 26, 3);
 173        csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4);
 174        csd->write_partial = UNSTUFF_BITS(resp, 21, 1);
 175
 176        if (csd->write_blkbits >= 9) {
 177                a = UNSTUFF_BITS(resp, 42, 5);
 178                b = UNSTUFF_BITS(resp, 37, 5);
 179                csd->erase_size = (a + 1) * (b + 1);
 180                csd->erase_size <<= csd->write_blkbits - 9;
 181        }
 182
 183        return 0;
 184}
 185
 186static void mmc_select_card_type(struct mmc_card *card)
 187{
 188        struct mmc_host *host = card->host;
 189        u8 card_type = card->ext_csd.raw_card_type;
 190        u32 caps = host->caps, caps2 = host->caps2;
 191        unsigned int hs_max_dtr = 0, hs200_max_dtr = 0;
 192        unsigned int avail_type = 0;
 193
 194        if (caps & MMC_CAP_MMC_HIGHSPEED &&
 195            card_type & EXT_CSD_CARD_TYPE_HS_26) {
 196                hs_max_dtr = MMC_HIGH_26_MAX_DTR;
 197                avail_type |= EXT_CSD_CARD_TYPE_HS_26;
 198        }
 199
 200        if (caps & MMC_CAP_MMC_HIGHSPEED &&
 201            card_type & EXT_CSD_CARD_TYPE_HS_52) {
 202                hs_max_dtr = MMC_HIGH_52_MAX_DTR;
 203                avail_type |= EXT_CSD_CARD_TYPE_HS_52;
 204        }
 205
 206        if (caps & (MMC_CAP_1_8V_DDR | MMC_CAP_3_3V_DDR) &&
 207            card_type & EXT_CSD_CARD_TYPE_DDR_1_8V) {
 208                hs_max_dtr = MMC_HIGH_DDR_MAX_DTR;
 209                avail_type |= EXT_CSD_CARD_TYPE_DDR_1_8V;
 210        }
 211
 212        if (caps & MMC_CAP_1_2V_DDR &&
 213            card_type & EXT_CSD_CARD_TYPE_DDR_1_2V) {
 214                hs_max_dtr = MMC_HIGH_DDR_MAX_DTR;
 215                avail_type |= EXT_CSD_CARD_TYPE_DDR_1_2V;
 216        }
 217
 218        if (caps2 & MMC_CAP2_HS200_1_8V_SDR &&
 219            card_type & EXT_CSD_CARD_TYPE_HS200_1_8V) {
 220                hs200_max_dtr = MMC_HS200_MAX_DTR;
 221                avail_type |= EXT_CSD_CARD_TYPE_HS200_1_8V;
 222        }
 223
 224        if (caps2 & MMC_CAP2_HS200_1_2V_SDR &&
 225            card_type & EXT_CSD_CARD_TYPE_HS200_1_2V) {
 226                hs200_max_dtr = MMC_HS200_MAX_DTR;
 227                avail_type |= EXT_CSD_CARD_TYPE_HS200_1_2V;
 228        }
 229
 230        if (caps2 & MMC_CAP2_HS400_1_8V &&
 231            card_type & EXT_CSD_CARD_TYPE_HS400_1_8V) {
 232                hs200_max_dtr = MMC_HS200_MAX_DTR;
 233                avail_type |= EXT_CSD_CARD_TYPE_HS400_1_8V;
 234        }
 235
 236        if (caps2 & MMC_CAP2_HS400_1_2V &&
 237            card_type & EXT_CSD_CARD_TYPE_HS400_1_2V) {
 238                hs200_max_dtr = MMC_HS200_MAX_DTR;
 239                avail_type |= EXT_CSD_CARD_TYPE_HS400_1_2V;
 240        }
 241
 242        if ((caps2 & MMC_CAP2_HS400_ES) &&
 243            card->ext_csd.strobe_support &&
 244            (avail_type & EXT_CSD_CARD_TYPE_HS400))
 245                avail_type |= EXT_CSD_CARD_TYPE_HS400ES;
 246
 247        card->ext_csd.hs_max_dtr = hs_max_dtr;
 248        card->ext_csd.hs200_max_dtr = hs200_max_dtr;
 249        card->mmc_avail_type = avail_type;
 250}
 251
 252static void mmc_manage_enhanced_area(struct mmc_card *card, u8 *ext_csd)
 253{
 254        u8 hc_erase_grp_sz, hc_wp_grp_sz;
 255
 256        /*
 257         * Disable these attributes by default
 258         */
 259        card->ext_csd.enhanced_area_offset = -EINVAL;
 260        card->ext_csd.enhanced_area_size = -EINVAL;
 261
 262        /*
 263         * Enhanced area feature support -- check whether the eMMC
 264         * card has the Enhanced area enabled.  If so, export enhanced
 265         * area offset and size to user by adding sysfs interface.
 266         */
 267        if ((ext_csd[EXT_CSD_PARTITION_SUPPORT] & 0x2) &&
 268            (ext_csd[EXT_CSD_PARTITION_ATTRIBUTE] & 0x1)) {
 269                if (card->ext_csd.partition_setting_completed) {
 270                        hc_erase_grp_sz =
 271                                ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
 272                        hc_wp_grp_sz =
 273                                ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
 274
 275                        /*
 276                         * calculate the enhanced data area offset, in bytes
 277                         */
 278                        card->ext_csd.enhanced_area_offset =
 279                                (((unsigned long long)ext_csd[139]) << 24) +
 280                                (((unsigned long long)ext_csd[138]) << 16) +
 281                                (((unsigned long long)ext_csd[137]) << 8) +
 282                                (((unsigned long long)ext_csd[136]));
 283                        if (mmc_card_blockaddr(card))
 284                                card->ext_csd.enhanced_area_offset <<= 9;
 285                        /*
 286                         * calculate the enhanced data area size, in kilobytes
 287                         */
 288                        card->ext_csd.enhanced_area_size =
 289                                (ext_csd[142] << 16) + (ext_csd[141] << 8) +
 290                                ext_csd[140];
 291                        card->ext_csd.enhanced_area_size *=
 292                                (size_t)(hc_erase_grp_sz * hc_wp_grp_sz);
 293                        card->ext_csd.enhanced_area_size <<= 9;
 294                } else {
 295                        pr_warn("%s: defines enhanced area without partition setting complete\n",
 296                                mmc_hostname(card->host));
 297                }
 298        }
 299}
 300
 301static void mmc_part_add(struct mmc_card *card, unsigned int size,
 302                         unsigned int part_cfg, char *name, int idx, bool ro,
 303                         int area_type)
 304{
 305        card->part[card->nr_parts].size = size;
 306        card->part[card->nr_parts].part_cfg = part_cfg;
 307        sprintf(card->part[card->nr_parts].name, name, idx);
 308        card->part[card->nr_parts].force_ro = ro;
 309        card->part[card->nr_parts].area_type = area_type;
 310        card->nr_parts++;
 311}
 312
 313static void mmc_manage_gp_partitions(struct mmc_card *card, u8 *ext_csd)
 314{
 315        int idx;
 316        u8 hc_erase_grp_sz, hc_wp_grp_sz;
 317        unsigned int part_size;
 318
 319        /*
 320         * General purpose partition feature support --
 321         * If ext_csd has the size of general purpose partitions,
 322         * set size, part_cfg, partition name in mmc_part.
 323         */
 324        if (ext_csd[EXT_CSD_PARTITION_SUPPORT] &
 325            EXT_CSD_PART_SUPPORT_PART_EN) {
 326                hc_erase_grp_sz =
 327                        ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
 328                hc_wp_grp_sz =
 329                        ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
 330
 331                for (idx = 0; idx < MMC_NUM_GP_PARTITION; idx++) {
 332                        if (!ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3] &&
 333                            !ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 1] &&
 334                            !ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 2])
 335                                continue;
 336                        if (card->ext_csd.partition_setting_completed == 0) {
 337                                pr_warn("%s: has partition size defined without partition complete\n",
 338                                        mmc_hostname(card->host));
 339                                break;
 340                        }
 341                        part_size =
 342                                (ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 2]
 343                                << 16) +
 344                                (ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 1]
 345                                << 8) +
 346                                ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3];
 347                        part_size *= (size_t)(hc_erase_grp_sz *
 348                                hc_wp_grp_sz);
 349                        mmc_part_add(card, part_size << 19,
 350                                EXT_CSD_PART_CONFIG_ACC_GP0 + idx,
 351                                "gp%d", idx, false,
 352                                MMC_BLK_DATA_AREA_GP);
 353                }
 354        }
 355}
 356
 357/* Minimum partition switch timeout in milliseconds */
 358#define MMC_MIN_PART_SWITCH_TIME        300
 359
 360/*
 361 * Decode extended CSD.
 362 */
 363static int mmc_decode_ext_csd(struct mmc_card *card, u8 *ext_csd)
 364{
 365        int err = 0, idx;
 366        unsigned int part_size;
 367        struct device_node *np;
 368        bool broken_hpi = false;
 369
 370        /* Version is coded in the CSD_STRUCTURE byte in the EXT_CSD register */
 371        card->ext_csd.raw_ext_csd_structure = ext_csd[EXT_CSD_STRUCTURE];
 372        if (card->csd.structure == 3) {
 373                if (card->ext_csd.raw_ext_csd_structure > 2) {
 374                        pr_err("%s: unrecognised EXT_CSD structure "
 375                                "version %d\n", mmc_hostname(card->host),
 376                                        card->ext_csd.raw_ext_csd_structure);
 377                        err = -EINVAL;
 378                        goto out;
 379                }
 380        }
 381
 382        np = mmc_of_find_child_device(card->host, 0);
 383        if (np && of_device_is_compatible(np, "mmc-card"))
 384                broken_hpi = of_property_read_bool(np, "broken-hpi");
 385        of_node_put(np);
 386
 387        /*
 388         * The EXT_CSD format is meant to be forward compatible. As long
 389         * as CSD_STRUCTURE does not change, all values for EXT_CSD_REV
 390         * are authorized, see JEDEC JESD84-B50 section B.8.
 391         */
 392        card->ext_csd.rev = ext_csd[EXT_CSD_REV];
 393
 394        /* fixup device after ext_csd revision field is updated */
 395        mmc_fixup_device(card, mmc_ext_csd_fixups);
 396
 397        card->ext_csd.raw_sectors[0] = ext_csd[EXT_CSD_SEC_CNT + 0];
 398        card->ext_csd.raw_sectors[1] = ext_csd[EXT_CSD_SEC_CNT + 1];
 399        card->ext_csd.raw_sectors[2] = ext_csd[EXT_CSD_SEC_CNT + 2];
 400        card->ext_csd.raw_sectors[3] = ext_csd[EXT_CSD_SEC_CNT + 3];
 401        if (card->ext_csd.rev >= 2) {
 402                card->ext_csd.sectors =
 403                        ext_csd[EXT_CSD_SEC_CNT + 0] << 0 |
 404                        ext_csd[EXT_CSD_SEC_CNT + 1] << 8 |
 405                        ext_csd[EXT_CSD_SEC_CNT + 2] << 16 |
 406                        ext_csd[EXT_CSD_SEC_CNT + 3] << 24;
 407
 408                /* Cards with density > 2GiB are sector addressed */
 409                if (card->ext_csd.sectors > (2u * 1024 * 1024 * 1024) / 512)
 410                        mmc_card_set_blockaddr(card);
 411        }
 412
 413        card->ext_csd.strobe_support = ext_csd[EXT_CSD_STROBE_SUPPORT];
 414        card->ext_csd.raw_card_type = ext_csd[EXT_CSD_CARD_TYPE];
 415        mmc_select_card_type(card);
 416
 417        card->ext_csd.raw_s_a_timeout = ext_csd[EXT_CSD_S_A_TIMEOUT];
 418        card->ext_csd.raw_erase_timeout_mult =
 419                ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT];
 420        card->ext_csd.raw_hc_erase_grp_size =
 421                ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
 422        if (card->ext_csd.rev >= 3) {
 423                u8 sa_shift = ext_csd[EXT_CSD_S_A_TIMEOUT];
 424                card->ext_csd.part_config = ext_csd[EXT_CSD_PART_CONFIG];
 425
 426                /* EXT_CSD value is in units of 10ms, but we store in ms */
 427                card->ext_csd.part_time = 10 * ext_csd[EXT_CSD_PART_SWITCH_TIME];
 428                /* Some eMMC set the value too low so set a minimum */
 429                if (card->ext_csd.part_time &&
 430                    card->ext_csd.part_time < MMC_MIN_PART_SWITCH_TIME)
 431                        card->ext_csd.part_time = MMC_MIN_PART_SWITCH_TIME;
 432
 433                /* Sleep / awake timeout in 100ns units */
 434                if (sa_shift > 0 && sa_shift <= 0x17)
 435                        card->ext_csd.sa_timeout =
 436                                        1 << ext_csd[EXT_CSD_S_A_TIMEOUT];
 437                card->ext_csd.erase_group_def =
 438                        ext_csd[EXT_CSD_ERASE_GROUP_DEF];
 439                card->ext_csd.hc_erase_timeout = 300 *
 440                        ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT];
 441                card->ext_csd.hc_erase_size =
 442                        ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] << 10;
 443
 444                card->ext_csd.rel_sectors = ext_csd[EXT_CSD_REL_WR_SEC_C];
 445
 446                /*
 447                 * There are two boot regions of equal size, defined in
 448                 * multiples of 128K.
 449                 */
 450                if (ext_csd[EXT_CSD_BOOT_MULT] && mmc_boot_partition_access(card->host)) {
 451                        for (idx = 0; idx < MMC_NUM_BOOT_PARTITION; idx++) {
 452                                part_size = ext_csd[EXT_CSD_BOOT_MULT] << 17;
 453                                mmc_part_add(card, part_size,
 454                                        EXT_CSD_PART_CONFIG_ACC_BOOT0 + idx,
 455                                        "boot%d", idx, true,
 456                                        MMC_BLK_DATA_AREA_BOOT);
 457                        }
 458                }
 459        }
 460
 461        card->ext_csd.raw_hc_erase_gap_size =
 462                ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
 463        card->ext_csd.raw_sec_trim_mult =
 464                ext_csd[EXT_CSD_SEC_TRIM_MULT];
 465        card->ext_csd.raw_sec_erase_mult =
 466                ext_csd[EXT_CSD_SEC_ERASE_MULT];
 467        card->ext_csd.raw_sec_feature_support =
 468                ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT];
 469        card->ext_csd.raw_trim_mult =
 470                ext_csd[EXT_CSD_TRIM_MULT];
 471        card->ext_csd.raw_partition_support = ext_csd[EXT_CSD_PARTITION_SUPPORT];
 472        card->ext_csd.raw_driver_strength = ext_csd[EXT_CSD_DRIVER_STRENGTH];
 473        if (card->ext_csd.rev >= 4) {
 474                if (ext_csd[EXT_CSD_PARTITION_SETTING_COMPLETED] &
 475                    EXT_CSD_PART_SETTING_COMPLETED)
 476                        card->ext_csd.partition_setting_completed = 1;
 477                else
 478                        card->ext_csd.partition_setting_completed = 0;
 479
 480                mmc_manage_enhanced_area(card, ext_csd);
 481
 482                mmc_manage_gp_partitions(card, ext_csd);
 483
 484                card->ext_csd.sec_trim_mult =
 485                        ext_csd[EXT_CSD_SEC_TRIM_MULT];
 486                card->ext_csd.sec_erase_mult =
 487                        ext_csd[EXT_CSD_SEC_ERASE_MULT];
 488                card->ext_csd.sec_feature_support =
 489                        ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT];
 490                card->ext_csd.trim_timeout = 300 *
 491                        ext_csd[EXT_CSD_TRIM_MULT];
 492
 493                /*
 494                 * Note that the call to mmc_part_add above defaults to read
 495                 * only. If this default assumption is changed, the call must
 496                 * take into account the value of boot_locked below.
 497                 */
 498                card->ext_csd.boot_ro_lock = ext_csd[EXT_CSD_BOOT_WP];
 499                card->ext_csd.boot_ro_lockable = true;
 500
 501                /* Save power class values */
 502                card->ext_csd.raw_pwr_cl_52_195 =
 503                        ext_csd[EXT_CSD_PWR_CL_52_195];
 504                card->ext_csd.raw_pwr_cl_26_195 =
 505                        ext_csd[EXT_CSD_PWR_CL_26_195];
 506                card->ext_csd.raw_pwr_cl_52_360 =
 507                        ext_csd[EXT_CSD_PWR_CL_52_360];
 508                card->ext_csd.raw_pwr_cl_26_360 =
 509                        ext_csd[EXT_CSD_PWR_CL_26_360];
 510                card->ext_csd.raw_pwr_cl_200_195 =
 511                        ext_csd[EXT_CSD_PWR_CL_200_195];
 512                card->ext_csd.raw_pwr_cl_200_360 =
 513                        ext_csd[EXT_CSD_PWR_CL_200_360];
 514                card->ext_csd.raw_pwr_cl_ddr_52_195 =
 515                        ext_csd[EXT_CSD_PWR_CL_DDR_52_195];
 516                card->ext_csd.raw_pwr_cl_ddr_52_360 =
 517                        ext_csd[EXT_CSD_PWR_CL_DDR_52_360];
 518                card->ext_csd.raw_pwr_cl_ddr_200_360 =
 519                        ext_csd[EXT_CSD_PWR_CL_DDR_200_360];
 520        }
 521
 522        if (card->ext_csd.rev >= 5) {
 523                /* Adjust production date as per JEDEC JESD84-B451 */
 524                if (card->cid.year < 2010)
 525                        card->cid.year += 16;
 526
 527                /* check whether the eMMC card supports BKOPS */
 528                if (!mmc_card_broken_hpi(card) &&
 529                    ext_csd[EXT_CSD_BKOPS_SUPPORT] & 0x1) {
 530                        card->ext_csd.bkops = 1;
 531                        card->ext_csd.man_bkops_en =
 532                                        (ext_csd[EXT_CSD_BKOPS_EN] &
 533                                                EXT_CSD_MANUAL_BKOPS_MASK);
 534                        card->ext_csd.raw_bkops_status =
 535                                ext_csd[EXT_CSD_BKOPS_STATUS];
 536                        if (card->ext_csd.man_bkops_en)
 537                                pr_debug("%s: MAN_BKOPS_EN bit is set\n",
 538                                        mmc_hostname(card->host));
 539                        card->ext_csd.auto_bkops_en =
 540                                        (ext_csd[EXT_CSD_BKOPS_EN] &
 541                                                EXT_CSD_AUTO_BKOPS_MASK);
 542                        if (card->ext_csd.auto_bkops_en)
 543                                pr_debug("%s: AUTO_BKOPS_EN bit is set\n",
 544                                        mmc_hostname(card->host));
 545                }
 546
 547                /* check whether the eMMC card supports HPI */
 548                if (!mmc_card_broken_hpi(card) &&
 549                    !broken_hpi && (ext_csd[EXT_CSD_HPI_FEATURES] & 0x1)) {
 550                        card->ext_csd.hpi = 1;
 551                        if (ext_csd[EXT_CSD_HPI_FEATURES] & 0x2)
 552                                card->ext_csd.hpi_cmd = MMC_STOP_TRANSMISSION;
 553                        else
 554                                card->ext_csd.hpi_cmd = MMC_SEND_STATUS;
 555                        /*
 556                         * Indicate the maximum timeout to close
 557                         * a command interrupted by HPI
 558                         */
 559                        card->ext_csd.out_of_int_time =
 560                                ext_csd[EXT_CSD_OUT_OF_INTERRUPT_TIME] * 10;
 561                }
 562
 563                card->ext_csd.rel_param = ext_csd[EXT_CSD_WR_REL_PARAM];
 564                card->ext_csd.rst_n_function = ext_csd[EXT_CSD_RST_N_FUNCTION];
 565
 566                /*
 567                 * RPMB regions are defined in multiples of 128K.
 568                 */
 569                card->ext_csd.raw_rpmb_size_mult = ext_csd[EXT_CSD_RPMB_MULT];
 570                if (ext_csd[EXT_CSD_RPMB_MULT] && mmc_host_cmd23(card->host)) {
 571                        mmc_part_add(card, ext_csd[EXT_CSD_RPMB_MULT] << 17,
 572                                EXT_CSD_PART_CONFIG_ACC_RPMB,
 573                                "rpmb", 0, false,
 574                                MMC_BLK_DATA_AREA_RPMB);
 575                }
 576        }
 577
 578        card->ext_csd.raw_erased_mem_count = ext_csd[EXT_CSD_ERASED_MEM_CONT];
 579        if (ext_csd[EXT_CSD_ERASED_MEM_CONT])
 580                card->erased_byte = 0xFF;
 581        else
 582                card->erased_byte = 0x0;
 583
 584        /* eMMC v4.5 or later */
 585        card->ext_csd.generic_cmd6_time = DEFAULT_CMD6_TIMEOUT_MS;
 586        if (card->ext_csd.rev >= 6) {
 587                card->ext_csd.feature_support |= MMC_DISCARD_FEATURE;
 588
 589                card->ext_csd.generic_cmd6_time = 10 *
 590                        ext_csd[EXT_CSD_GENERIC_CMD6_TIME];
 591                card->ext_csd.power_off_longtime = 10 *
 592                        ext_csd[EXT_CSD_POWER_OFF_LONG_TIME];
 593
 594                card->ext_csd.cache_size =
 595                        ext_csd[EXT_CSD_CACHE_SIZE + 0] << 0 |
 596                        ext_csd[EXT_CSD_CACHE_SIZE + 1] << 8 |
 597                        ext_csd[EXT_CSD_CACHE_SIZE + 2] << 16 |
 598                        ext_csd[EXT_CSD_CACHE_SIZE + 3] << 24;
 599
 600                if (ext_csd[EXT_CSD_DATA_SECTOR_SIZE] == 1)
 601                        card->ext_csd.data_sector_size = 4096;
 602                else
 603                        card->ext_csd.data_sector_size = 512;
 604
 605                if ((ext_csd[EXT_CSD_DATA_TAG_SUPPORT] & 1) &&
 606                    (ext_csd[EXT_CSD_TAG_UNIT_SIZE] <= 8)) {
 607                        card->ext_csd.data_tag_unit_size =
 608                        ((unsigned int) 1 << ext_csd[EXT_CSD_TAG_UNIT_SIZE]) *
 609                        (card->ext_csd.data_sector_size);
 610                } else {
 611                        card->ext_csd.data_tag_unit_size = 0;
 612                }
 613
 614                card->ext_csd.max_packed_writes =
 615                        ext_csd[EXT_CSD_MAX_PACKED_WRITES];
 616                card->ext_csd.max_packed_reads =
 617                        ext_csd[EXT_CSD_MAX_PACKED_READS];
 618        } else {
 619                card->ext_csd.data_sector_size = 512;
 620        }
 621
 622        /* eMMC v5 or later */
 623        if (card->ext_csd.rev >= 7) {
 624                memcpy(card->ext_csd.fwrev, &ext_csd[EXT_CSD_FIRMWARE_VERSION],
 625                       MMC_FIRMWARE_LEN);
 626                card->ext_csd.ffu_capable =
 627                        (ext_csd[EXT_CSD_SUPPORTED_MODE] & 0x1) &&
 628                        !(ext_csd[EXT_CSD_FW_CONFIG] & 0x1);
 629
 630                card->ext_csd.pre_eol_info = ext_csd[EXT_CSD_PRE_EOL_INFO];
 631                card->ext_csd.device_life_time_est_typ_a =
 632                        ext_csd[EXT_CSD_DEVICE_LIFE_TIME_EST_TYP_A];
 633                card->ext_csd.device_life_time_est_typ_b =
 634                        ext_csd[EXT_CSD_DEVICE_LIFE_TIME_EST_TYP_B];
 635        }
 636
 637        /* eMMC v5.1 or later */
 638        if (card->ext_csd.rev >= 8) {
 639                card->ext_csd.cmdq_support = ext_csd[EXT_CSD_CMDQ_SUPPORT] &
 640                                             EXT_CSD_CMDQ_SUPPORTED;
 641                card->ext_csd.cmdq_depth = (ext_csd[EXT_CSD_CMDQ_DEPTH] &
 642                                            EXT_CSD_CMDQ_DEPTH_MASK) + 1;
 643                /* Exclude inefficiently small queue depths */
 644                if (card->ext_csd.cmdq_depth <= 2) {
 645                        card->ext_csd.cmdq_support = false;
 646                        card->ext_csd.cmdq_depth = 0;
 647                }
 648                if (card->ext_csd.cmdq_support) {
 649                        pr_debug("%s: Command Queue supported depth %u\n",
 650                                 mmc_hostname(card->host),
 651                                 card->ext_csd.cmdq_depth);
 652                }
 653        }
 654out:
 655        return err;
 656}
 657
 658static int mmc_read_ext_csd(struct mmc_card *card)
 659{
 660        u8 *ext_csd;
 661        int err;
 662
 663        if (!mmc_can_ext_csd(card))
 664                return 0;
 665
 666        err = mmc_get_ext_csd(card, &ext_csd);
 667        if (err) {
 668                /* If the host or the card can't do the switch,
 669                 * fail more gracefully. */
 670                if ((err != -EINVAL)
 671                 && (err != -ENOSYS)
 672                 && (err != -EFAULT))
 673                        return err;
 674
 675                /*
 676                 * High capacity cards should have this "magic" size
 677                 * stored in their CSD.
 678                 */
 679                if (card->csd.capacity == (4096 * 512)) {
 680                        pr_err("%s: unable to read EXT_CSD on a possible high capacity card. Card will be ignored.\n",
 681                                mmc_hostname(card->host));
 682                } else {
 683                        pr_warn("%s: unable to read EXT_CSD, performance might suffer\n",
 684                                mmc_hostname(card->host));
 685                        err = 0;
 686                }
 687
 688                return err;
 689        }
 690
 691        err = mmc_decode_ext_csd(card, ext_csd);
 692        kfree(ext_csd);
 693        return err;
 694}
 695
 696static int mmc_compare_ext_csds(struct mmc_card *card, unsigned bus_width)
 697{
 698        u8 *bw_ext_csd;
 699        int err;
 700
 701        if (bus_width == MMC_BUS_WIDTH_1)
 702                return 0;
 703
 704        err = mmc_get_ext_csd(card, &bw_ext_csd);
 705        if (err)
 706                return err;
 707
 708        /* only compare read only fields */
 709        err = !((card->ext_csd.raw_partition_support ==
 710                        bw_ext_csd[EXT_CSD_PARTITION_SUPPORT]) &&
 711                (card->ext_csd.raw_erased_mem_count ==
 712                        bw_ext_csd[EXT_CSD_ERASED_MEM_CONT]) &&
 713                (card->ext_csd.rev ==
 714                        bw_ext_csd[EXT_CSD_REV]) &&
 715                (card->ext_csd.raw_ext_csd_structure ==
 716                        bw_ext_csd[EXT_CSD_STRUCTURE]) &&
 717                (card->ext_csd.raw_card_type ==
 718                        bw_ext_csd[EXT_CSD_CARD_TYPE]) &&
 719                (card->ext_csd.raw_s_a_timeout ==
 720                        bw_ext_csd[EXT_CSD_S_A_TIMEOUT]) &&
 721                (card->ext_csd.raw_hc_erase_gap_size ==
 722                        bw_ext_csd[EXT_CSD_HC_WP_GRP_SIZE]) &&
 723                (card->ext_csd.raw_erase_timeout_mult ==
 724                        bw_ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT]) &&
 725                (card->ext_csd.raw_hc_erase_grp_size ==
 726                        bw_ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE]) &&
 727                (card->ext_csd.raw_sec_trim_mult ==
 728                        bw_ext_csd[EXT_CSD_SEC_TRIM_MULT]) &&
 729                (card->ext_csd.raw_sec_erase_mult ==
 730                        bw_ext_csd[EXT_CSD_SEC_ERASE_MULT]) &&
 731                (card->ext_csd.raw_sec_feature_support ==
 732                        bw_ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT]) &&
 733                (card->ext_csd.raw_trim_mult ==
 734                        bw_ext_csd[EXT_CSD_TRIM_MULT]) &&
 735                (card->ext_csd.raw_sectors[0] ==
 736                        bw_ext_csd[EXT_CSD_SEC_CNT + 0]) &&
 737                (card->ext_csd.raw_sectors[1] ==
 738                        bw_ext_csd[EXT_CSD_SEC_CNT + 1]) &&
 739                (card->ext_csd.raw_sectors[2] ==
 740                        bw_ext_csd[EXT_CSD_SEC_CNT + 2]) &&
 741                (card->ext_csd.raw_sectors[3] ==
 742                        bw_ext_csd[EXT_CSD_SEC_CNT + 3]) &&
 743                (card->ext_csd.raw_pwr_cl_52_195 ==
 744                        bw_ext_csd[EXT_CSD_PWR_CL_52_195]) &&
 745                (card->ext_csd.raw_pwr_cl_26_195 ==
 746                        bw_ext_csd[EXT_CSD_PWR_CL_26_195]) &&
 747                (card->ext_csd.raw_pwr_cl_52_360 ==
 748                        bw_ext_csd[EXT_CSD_PWR_CL_52_360]) &&
 749                (card->ext_csd.raw_pwr_cl_26_360 ==
 750                        bw_ext_csd[EXT_CSD_PWR_CL_26_360]) &&
 751                (card->ext_csd.raw_pwr_cl_200_195 ==
 752                        bw_ext_csd[EXT_CSD_PWR_CL_200_195]) &&
 753                (card->ext_csd.raw_pwr_cl_200_360 ==
 754                        bw_ext_csd[EXT_CSD_PWR_CL_200_360]) &&
 755                (card->ext_csd.raw_pwr_cl_ddr_52_195 ==
 756                        bw_ext_csd[EXT_CSD_PWR_CL_DDR_52_195]) &&
 757                (card->ext_csd.raw_pwr_cl_ddr_52_360 ==
 758                        bw_ext_csd[EXT_CSD_PWR_CL_DDR_52_360]) &&
 759                (card->ext_csd.raw_pwr_cl_ddr_200_360 ==
 760                        bw_ext_csd[EXT_CSD_PWR_CL_DDR_200_360]));
 761
 762        if (err)
 763                err = -EINVAL;
 764
 765        kfree(bw_ext_csd);
 766        return err;
 767}
 768
 769MMC_DEV_ATTR(cid, "%08x%08x%08x%08x\n", card->raw_cid[0], card->raw_cid[1],
 770        card->raw_cid[2], card->raw_cid[3]);
 771MMC_DEV_ATTR(csd, "%08x%08x%08x%08x\n", card->raw_csd[0], card->raw_csd[1],
 772        card->raw_csd[2], card->raw_csd[3]);
 773MMC_DEV_ATTR(date, "%02d/%04d\n", card->cid.month, card->cid.year);
 774MMC_DEV_ATTR(erase_size, "%u\n", card->erase_size << 9);
 775MMC_DEV_ATTR(preferred_erase_size, "%u\n", card->pref_erase << 9);
 776MMC_DEV_ATTR(ffu_capable, "%d\n", card->ext_csd.ffu_capable);
 777MMC_DEV_ATTR(hwrev, "0x%x\n", card->cid.hwrev);
 778MMC_DEV_ATTR(manfid, "0x%06x\n", card->cid.manfid);
 779MMC_DEV_ATTR(name, "%s\n", card->cid.prod_name);
 780MMC_DEV_ATTR(oemid, "0x%04x\n", card->cid.oemid);
 781MMC_DEV_ATTR(prv, "0x%x\n", card->cid.prv);
 782MMC_DEV_ATTR(rev, "0x%x\n", card->ext_csd.rev);
 783MMC_DEV_ATTR(pre_eol_info, "0x%02x\n", card->ext_csd.pre_eol_info);
 784MMC_DEV_ATTR(life_time, "0x%02x 0x%02x\n",
 785        card->ext_csd.device_life_time_est_typ_a,
 786        card->ext_csd.device_life_time_est_typ_b);
 787MMC_DEV_ATTR(serial, "0x%08x\n", card->cid.serial);
 788MMC_DEV_ATTR(enhanced_area_offset, "%llu\n",
 789                card->ext_csd.enhanced_area_offset);
 790MMC_DEV_ATTR(enhanced_area_size, "%u\n", card->ext_csd.enhanced_area_size);
 791MMC_DEV_ATTR(raw_rpmb_size_mult, "%#x\n", card->ext_csd.raw_rpmb_size_mult);
 792MMC_DEV_ATTR(rel_sectors, "%#x\n", card->ext_csd.rel_sectors);
 793MMC_DEV_ATTR(ocr, "0x%08x\n", card->ocr);
 794MMC_DEV_ATTR(cmdq_en, "%d\n", card->ext_csd.cmdq_en);
 795
 796static ssize_t mmc_fwrev_show(struct device *dev,
 797                              struct device_attribute *attr,
 798                              char *buf)
 799{
 800        struct mmc_card *card = mmc_dev_to_card(dev);
 801
 802        if (card->ext_csd.rev < 7) {
 803                return sprintf(buf, "0x%x\n", card->cid.fwrev);
 804        } else {
 805                return sprintf(buf, "0x%*phN\n", MMC_FIRMWARE_LEN,
 806                               card->ext_csd.fwrev);
 807        }
 808}
 809
 810static DEVICE_ATTR(fwrev, S_IRUGO, mmc_fwrev_show, NULL);
 811
 812static ssize_t mmc_dsr_show(struct device *dev,
 813                            struct device_attribute *attr,
 814                            char *buf)
 815{
 816        struct mmc_card *card = mmc_dev_to_card(dev);
 817        struct mmc_host *host = card->host;
 818
 819        if (card->csd.dsr_imp && host->dsr_req)
 820                return sprintf(buf, "0x%x\n", host->dsr);
 821        else
 822                /* return default DSR value */
 823                return sprintf(buf, "0x%x\n", 0x404);
 824}
 825
 826static DEVICE_ATTR(dsr, S_IRUGO, mmc_dsr_show, NULL);
 827
 828static struct attribute *mmc_std_attrs[] = {
 829        &dev_attr_cid.attr,
 830        &dev_attr_csd.attr,
 831        &dev_attr_date.attr,
 832        &dev_attr_erase_size.attr,
 833        &dev_attr_preferred_erase_size.attr,
 834        &dev_attr_fwrev.attr,
 835        &dev_attr_ffu_capable.attr,
 836        &dev_attr_hwrev.attr,
 837        &dev_attr_manfid.attr,
 838        &dev_attr_name.attr,
 839        &dev_attr_oemid.attr,
 840        &dev_attr_prv.attr,
 841        &dev_attr_rev.attr,
 842        &dev_attr_pre_eol_info.attr,
 843        &dev_attr_life_time.attr,
 844        &dev_attr_serial.attr,
 845        &dev_attr_enhanced_area_offset.attr,
 846        &dev_attr_enhanced_area_size.attr,
 847        &dev_attr_raw_rpmb_size_mult.attr,
 848        &dev_attr_rel_sectors.attr,
 849        &dev_attr_ocr.attr,
 850        &dev_attr_dsr.attr,
 851        &dev_attr_cmdq_en.attr,
 852        NULL,
 853};
 854ATTRIBUTE_GROUPS(mmc_std);
 855
 856static struct device_type mmc_type = {
 857        .groups = mmc_std_groups,
 858};
 859
 860/*
 861 * Select the PowerClass for the current bus width
 862 * If power class is defined for 4/8 bit bus in the
 863 * extended CSD register, select it by executing the
 864 * mmc_switch command.
 865 */
 866static int __mmc_select_powerclass(struct mmc_card *card,
 867                                   unsigned int bus_width)
 868{
 869        struct mmc_host *host = card->host;
 870        struct mmc_ext_csd *ext_csd = &card->ext_csd;
 871        unsigned int pwrclass_val = 0;
 872        int err = 0;
 873
 874        switch (1 << host->ios.vdd) {
 875        case MMC_VDD_165_195:
 876                if (host->ios.clock <= MMC_HIGH_26_MAX_DTR)
 877                        pwrclass_val = ext_csd->raw_pwr_cl_26_195;
 878                else if (host->ios.clock <= MMC_HIGH_52_MAX_DTR)
 879                        pwrclass_val = (bus_width <= EXT_CSD_BUS_WIDTH_8) ?
 880                                ext_csd->raw_pwr_cl_52_195 :
 881                                ext_csd->raw_pwr_cl_ddr_52_195;
 882                else if (host->ios.clock <= MMC_HS200_MAX_DTR)
 883                        pwrclass_val = ext_csd->raw_pwr_cl_200_195;
 884                break;
 885        case MMC_VDD_27_28:
 886        case MMC_VDD_28_29:
 887        case MMC_VDD_29_30:
 888        case MMC_VDD_30_31:
 889        case MMC_VDD_31_32:
 890        case MMC_VDD_32_33:
 891        case MMC_VDD_33_34:
 892        case MMC_VDD_34_35:
 893        case MMC_VDD_35_36:
 894                if (host->ios.clock <= MMC_HIGH_26_MAX_DTR)
 895                        pwrclass_val = ext_csd->raw_pwr_cl_26_360;
 896                else if (host->ios.clock <= MMC_HIGH_52_MAX_DTR)
 897                        pwrclass_val = (bus_width <= EXT_CSD_BUS_WIDTH_8) ?
 898                                ext_csd->raw_pwr_cl_52_360 :
 899                                ext_csd->raw_pwr_cl_ddr_52_360;
 900                else if (host->ios.clock <= MMC_HS200_MAX_DTR)
 901                        pwrclass_val = (bus_width == EXT_CSD_DDR_BUS_WIDTH_8) ?
 902                                ext_csd->raw_pwr_cl_ddr_200_360 :
 903                                ext_csd->raw_pwr_cl_200_360;
 904                break;
 905        default:
 906                pr_warn("%s: Voltage range not supported for power class\n",
 907                        mmc_hostname(host));
 908                return -EINVAL;
 909        }
 910
 911        if (bus_width & (EXT_CSD_BUS_WIDTH_8 | EXT_CSD_DDR_BUS_WIDTH_8))
 912                pwrclass_val = (pwrclass_val & EXT_CSD_PWR_CL_8BIT_MASK) >>
 913                                EXT_CSD_PWR_CL_8BIT_SHIFT;
 914        else
 915                pwrclass_val = (pwrclass_val & EXT_CSD_PWR_CL_4BIT_MASK) >>
 916                                EXT_CSD_PWR_CL_4BIT_SHIFT;
 917
 918        /* If the power class is different from the default value */
 919        if (pwrclass_val > 0) {
 920                err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
 921                                 EXT_CSD_POWER_CLASS,
 922                                 pwrclass_val,
 923                                 card->ext_csd.generic_cmd6_time);
 924        }
 925
 926        return err;
 927}
 928
 929static int mmc_select_powerclass(struct mmc_card *card)
 930{
 931        struct mmc_host *host = card->host;
 932        u32 bus_width, ext_csd_bits;
 933        int err, ddr;
 934
 935        /* Power class selection is supported for versions >= 4.0 */
 936        if (!mmc_can_ext_csd(card))
 937                return 0;
 938
 939        bus_width = host->ios.bus_width;
 940        /* Power class values are defined only for 4/8 bit bus */
 941        if (bus_width == MMC_BUS_WIDTH_1)
 942                return 0;
 943
 944        ddr = card->mmc_avail_type & EXT_CSD_CARD_TYPE_DDR_52;
 945        if (ddr)
 946                ext_csd_bits = (bus_width == MMC_BUS_WIDTH_8) ?
 947                        EXT_CSD_DDR_BUS_WIDTH_8 : EXT_CSD_DDR_BUS_WIDTH_4;
 948        else
 949                ext_csd_bits = (bus_width == MMC_BUS_WIDTH_8) ?
 950                        EXT_CSD_BUS_WIDTH_8 :  EXT_CSD_BUS_WIDTH_4;
 951
 952        err = __mmc_select_powerclass(card, ext_csd_bits);
 953        if (err)
 954                pr_warn("%s: power class selection to bus width %d ddr %d failed\n",
 955                        mmc_hostname(host), 1 << bus_width, ddr);
 956
 957        return err;
 958}
 959
 960/*
 961 * Set the bus speed for the selected speed mode.
 962 */
 963static void mmc_set_bus_speed(struct mmc_card *card)
 964{
 965        unsigned int max_dtr = (unsigned int)-1;
 966
 967        if ((mmc_card_hs200(card) || mmc_card_hs400(card)) &&
 968             max_dtr > card->ext_csd.hs200_max_dtr) {
 969                gmb();
 970                max_dtr = card->ext_csd.hs200_max_dtr;
 971        } else if (mmc_card_hs(card) && max_dtr > card->ext_csd.hs_max_dtr) {
 972                gmb();
 973                max_dtr = card->ext_csd.hs_max_dtr;
 974        } else if (max_dtr > card->csd.max_dtr) {
 975                gmb();
 976                max_dtr = card->csd.max_dtr;
 977        }
 978
 979        mmc_set_clock(card->host, max_dtr);
 980}
 981
 982/*
 983 * Select the bus width amoung 4-bit and 8-bit(SDR).
 984 * If the bus width is changed successfully, return the selected width value.
 985 * Zero is returned instead of error value if the wide width is not supported.
 986 */
 987static int mmc_select_bus_width(struct mmc_card *card)
 988{
 989        static unsigned ext_csd_bits[] = {
 990                EXT_CSD_BUS_WIDTH_8,
 991                EXT_CSD_BUS_WIDTH_4,
 992        };
 993        static unsigned bus_widths[] = {
 994                MMC_BUS_WIDTH_8,
 995                MMC_BUS_WIDTH_4,
 996        };
 997        struct mmc_host *host = card->host;
 998        unsigned idx, bus_width = 0;
 999        int err = 0;
1000
1001        if (!mmc_can_ext_csd(card) ||
1002            !(host->caps & (MMC_CAP_4_BIT_DATA | MMC_CAP_8_BIT_DATA)))
1003                return 0;
1004
1005        idx = (host->caps & MMC_CAP_8_BIT_DATA) ? 0 : 1;
1006
1007        /*
1008         * Unlike SD, MMC cards dont have a configuration register to notify
1009         * supported bus width. So bus test command should be run to identify
1010         * the supported bus width or compare the ext csd values of current
1011         * bus width and ext csd values of 1 bit mode read earlier.
1012         */
1013        for (; idx < ARRAY_SIZE(bus_widths); idx++) {
1014                /*
1015                 * Host is capable of 8bit transfer, then switch
1016                 * the device to work in 8bit transfer mode. If the
1017                 * mmc switch command returns error then switch to
1018                 * 4bit transfer mode. On success set the corresponding
1019                 * bus width on the host.
1020                 */
1021                err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1022                                 EXT_CSD_BUS_WIDTH,
1023                                 ext_csd_bits[idx],
1024                                 card->ext_csd.generic_cmd6_time);
1025                if (err)
1026                        continue;
1027
1028                bus_width = bus_widths[idx];
1029                mmc_set_bus_width(host, bus_width);
1030
1031                /*
1032                 * If controller can't handle bus width test,
1033                 * compare ext_csd previously read in 1 bit mode
1034                 * against ext_csd at new bus width
1035                 */
1036                if (!(host->caps & MMC_CAP_BUS_WIDTH_TEST))
1037                        err = mmc_compare_ext_csds(card, bus_width);
1038                else
1039                        err = mmc_bus_test(card, bus_width);
1040
1041                if (!err) {
1042                        err = bus_width;
1043                        break;
1044                } else {
1045                        pr_warn("%s: switch to bus width %d failed\n",
1046                                mmc_hostname(host), 1 << bus_width);
1047                }
1048        }
1049
1050        return err;
1051}
1052
1053/*
1054 * Switch to the high-speed mode
1055 */
1056static int mmc_select_hs(struct mmc_card *card)
1057{
1058        int err;
1059
1060        err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1061                           EXT_CSD_HS_TIMING, EXT_CSD_TIMING_HS,
1062                           card->ext_csd.generic_cmd6_time, MMC_TIMING_MMC_HS,
1063                           true, true, true);
1064        if (err)
1065                pr_warn("%s: switch to high-speed failed, err:%d\n",
1066                        mmc_hostname(card->host), err);
1067
1068        return err;
1069}
1070
1071/*
1072 * Activate wide bus and DDR if supported.
1073 */
1074static int mmc_select_hs_ddr(struct mmc_card *card)
1075{
1076        struct mmc_host *host = card->host;
1077        u32 bus_width, ext_csd_bits;
1078        int err = 0;
1079
1080        if (!(card->mmc_avail_type & EXT_CSD_CARD_TYPE_DDR_52))
1081                return 0;
1082
1083        bus_width = host->ios.bus_width;
1084        if (bus_width == MMC_BUS_WIDTH_1)
1085                return 0;
1086
1087        ext_csd_bits = (bus_width == MMC_BUS_WIDTH_8) ?
1088                EXT_CSD_DDR_BUS_WIDTH_8 : EXT_CSD_DDR_BUS_WIDTH_4;
1089
1090        err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1091                           EXT_CSD_BUS_WIDTH,
1092                           ext_csd_bits,
1093                           card->ext_csd.generic_cmd6_time,
1094                           MMC_TIMING_MMC_DDR52,
1095                           true, true, true);
1096        if (err) {
1097                pr_err("%s: switch to bus width %d ddr failed\n",
1098                        mmc_hostname(host), 1 << bus_width);
1099                return err;
1100        }
1101
1102        /*
1103         * eMMC cards can support 3.3V to 1.2V i/o (vccq)
1104         * signaling.
1105         *
1106         * EXT_CSD_CARD_TYPE_DDR_1_8V means 3.3V or 1.8V vccq.
1107         *
1108         * 1.8V vccq at 3.3V core voltage (vcc) is not required
1109         * in the JEDEC spec for DDR.
1110         *
1111         * Even (e)MMC card can support 3.3v to 1.2v vccq, but not all
1112         * host controller can support this, like some of the SDHCI
1113         * controller which connect to an eMMC device. Some of these
1114         * host controller still needs to use 1.8v vccq for supporting
1115         * DDR mode.
1116         *
1117         * So the sequence will be:
1118         * if (host and device can both support 1.2v IO)
1119         *      use 1.2v IO;
1120         * else if (host and device can both support 1.8v IO)
1121         *      use 1.8v IO;
1122         * so if host and device can only support 3.3v IO, this is the
1123         * last choice.
1124         *
1125         * WARNING: eMMC rules are NOT the same as SD DDR
1126         */
1127        if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_DDR_1_2V) {
1128                err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_120);
1129                if (!err)
1130                        return 0;
1131        }
1132
1133        if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_DDR_1_8V &&
1134            host->caps & MMC_CAP_1_8V_DDR)
1135                err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_180);
1136
1137        /* make sure vccq is 3.3v after switching disaster */
1138        if (err)
1139                err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_330);
1140
1141        return err;
1142}
1143
1144static int mmc_select_hs400(struct mmc_card *card)
1145{
1146        struct mmc_host *host = card->host;
1147        unsigned int max_dtr;
1148        int err = 0;
1149        u8 val;
1150
1151        /*
1152         * HS400 mode requires 8-bit bus width
1153         */
1154        if (!(card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS400 &&
1155              host->ios.bus_width == MMC_BUS_WIDTH_8))
1156                return 0;
1157
1158        /* Switch card to HS mode */
1159        val = EXT_CSD_TIMING_HS;
1160        err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1161                           EXT_CSD_HS_TIMING, val,
1162                           card->ext_csd.generic_cmd6_time, 0,
1163                           true, false, true);
1164        if (err) {
1165                pr_err("%s: switch to high-speed from hs200 failed, err:%d\n",
1166                        mmc_hostname(host), err);
1167                return err;
1168        }
1169
1170        /* Set host controller to HS timing */
1171        mmc_set_timing(card->host, MMC_TIMING_MMC_HS);
1172
1173        /* Reduce frequency to HS frequency */
1174        max_dtr = card->ext_csd.hs_max_dtr;
1175        mmc_set_clock(host, max_dtr);
1176
1177        err = mmc_switch_status(card);
1178        if (err)
1179                goto out_err;
1180
1181        /* Switch card to DDR */
1182        err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1183                         EXT_CSD_BUS_WIDTH,
1184                         EXT_CSD_DDR_BUS_WIDTH_8,
1185                         card->ext_csd.generic_cmd6_time);
1186        if (err) {
1187                pr_err("%s: switch to bus width for hs400 failed, err:%d\n",
1188                        mmc_hostname(host), err);
1189                return err;
1190        }
1191
1192        /* Switch card to HS400 */
1193        val = EXT_CSD_TIMING_HS400 |
1194              card->drive_strength << EXT_CSD_DRV_STR_SHIFT;
1195        err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1196                           EXT_CSD_HS_TIMING, val,
1197                           card->ext_csd.generic_cmd6_time, 0,
1198                           true, false, true);
1199        if (err) {
1200                pr_err("%s: switch to hs400 failed, err:%d\n",
1201                         mmc_hostname(host), err);
1202                return err;
1203        }
1204
1205        /* Set host controller to HS400 timing and frequency */
1206        mmc_set_timing(host, MMC_TIMING_MMC_HS400);
1207        mmc_set_bus_speed(card);
1208
1209        err = mmc_switch_status(card);
1210        if (err)
1211                goto out_err;
1212
1213        return 0;
1214
1215out_err:
1216        pr_err("%s: %s failed, error %d\n", mmc_hostname(card->host),
1217               __func__, err);
1218        return err;
1219}
1220
1221int mmc_hs200_to_hs400(struct mmc_card *card)
1222{
1223        return mmc_select_hs400(card);
1224}
1225
1226int mmc_hs400_to_hs200(struct mmc_card *card)
1227{
1228        struct mmc_host *host = card->host;
1229        unsigned int max_dtr;
1230        int err;
1231        u8 val;
1232
1233        /* Reduce frequency to HS */
1234        max_dtr = card->ext_csd.hs_max_dtr;
1235        mmc_set_clock(host, max_dtr);
1236
1237        /* Switch HS400 to HS DDR */
1238        val = EXT_CSD_TIMING_HS;
1239        err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_HS_TIMING,
1240                           val, card->ext_csd.generic_cmd6_time, 0,
1241                           true, false, true);
1242        if (err)
1243                goto out_err;
1244
1245        mmc_set_timing(host, MMC_TIMING_MMC_DDR52);
1246
1247        err = mmc_switch_status(card);
1248        if (err)
1249                goto out_err;
1250
1251        /* Switch HS DDR to HS */
1252        err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BUS_WIDTH,
1253                           EXT_CSD_BUS_WIDTH_8, card->ext_csd.generic_cmd6_time,
1254                           0, true, false, true);
1255        if (err)
1256                goto out_err;
1257
1258        mmc_set_timing(host, MMC_TIMING_MMC_HS);
1259
1260        err = mmc_switch_status(card);
1261        if (err)
1262                goto out_err;
1263
1264        /* Switch HS to HS200 */
1265        val = EXT_CSD_TIMING_HS200 |
1266              card->drive_strength << EXT_CSD_DRV_STR_SHIFT;
1267        err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_HS_TIMING,
1268                           val, card->ext_csd.generic_cmd6_time, 0,
1269                           true, false, true);
1270        if (err)
1271                goto out_err;
1272
1273        mmc_set_timing(host, MMC_TIMING_MMC_HS200);
1274
1275        /*
1276         * For HS200, CRC errors are not a reliable way to know the switch
1277         * failed. If there really is a problem, we would expect tuning will
1278         * fail and the result ends up the same.
1279         */
1280        err = __mmc_switch_status(card, false);
1281        if (err)
1282                goto out_err;
1283
1284        mmc_set_bus_speed(card);
1285
1286        return 0;
1287
1288out_err:
1289        pr_err("%s: %s failed, error %d\n", mmc_hostname(card->host),
1290               __func__, err);
1291        return err;
1292}
1293
1294static void mmc_select_driver_type(struct mmc_card *card)
1295{
1296        int card_drv_type, drive_strength, drv_type = 0;
1297
1298        card_drv_type = card->ext_csd.raw_driver_strength |
1299                        mmc_driver_type_mask(0);
1300
1301        drive_strength = mmc_select_drive_strength(card,
1302                                                   card->ext_csd.hs200_max_dtr,
1303                                                   card_drv_type, &drv_type);
1304
1305        card->drive_strength = drive_strength;
1306
1307        if (drv_type)
1308                mmc_set_driver_type(card->host, drv_type);
1309}
1310
1311static int mmc_select_hs400es(struct mmc_card *card)
1312{
1313        struct mmc_host *host = card->host;
1314        int err = -EINVAL;
1315        u8 val;
1316
1317        if (!(host->caps & MMC_CAP_8_BIT_DATA)) {
1318                err = -ENOTSUPP;
1319                goto out_err;
1320        }
1321
1322        if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS400_1_2V)
1323                err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_120);
1324
1325        if (err && card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS400_1_8V)
1326                err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_180);
1327
1328        /* If fails try again during next card power cycle */
1329        if (err)
1330                goto out_err;
1331
1332        err = mmc_select_bus_width(card);
1333        if (err < 0)
1334                goto out_err;
1335
1336        /* Switch card to HS mode */
1337        err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1338                           EXT_CSD_HS_TIMING, EXT_CSD_TIMING_HS,
1339                           card->ext_csd.generic_cmd6_time, 0,
1340                           true, false, true);
1341        if (err) {
1342                pr_err("%s: switch to hs for hs400es failed, err:%d\n",
1343                        mmc_hostname(host), err);
1344                goto out_err;
1345        }
1346
1347        mmc_set_timing(host, MMC_TIMING_MMC_HS);
1348        err = mmc_switch_status(card);
1349        if (err)
1350                goto out_err;
1351
1352        mmc_set_clock(host, card->ext_csd.hs_max_dtr);
1353
1354        /* Switch card to DDR with strobe bit */
1355        val = EXT_CSD_DDR_BUS_WIDTH_8 | EXT_CSD_BUS_WIDTH_STROBE;
1356        err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1357                         EXT_CSD_BUS_WIDTH,
1358                         val,
1359                         card->ext_csd.generic_cmd6_time);
1360        if (err) {
1361                pr_err("%s: switch to bus width for hs400es failed, err:%d\n",
1362                        mmc_hostname(host), err);
1363                goto out_err;
1364        }
1365
1366        mmc_select_driver_type(card);
1367
1368        /* Switch card to HS400 */
1369        val = EXT_CSD_TIMING_HS400 |
1370              card->drive_strength << EXT_CSD_DRV_STR_SHIFT;
1371        err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1372                           EXT_CSD_HS_TIMING, val,
1373                           card->ext_csd.generic_cmd6_time, 0,
1374                           true, false, true);
1375        if (err) {
1376                pr_err("%s: switch to hs400es failed, err:%d\n",
1377                        mmc_hostname(host), err);
1378                goto out_err;
1379        }
1380
1381        /* Set host controller to HS400 timing and frequency */
1382        mmc_set_timing(host, MMC_TIMING_MMC_HS400);
1383
1384        /* Controller enable enhanced strobe function */
1385        host->ios.enhanced_strobe = true;
1386        if (host->ops->hs400_enhanced_strobe)
1387                host->ops->hs400_enhanced_strobe(host, &host->ios);
1388
1389        err = mmc_switch_status(card);
1390        if (err)
1391                goto out_err;
1392
1393        return 0;
1394
1395out_err:
1396        pr_err("%s: %s failed, error %d\n", mmc_hostname(card->host),
1397               __func__, err);
1398        return err;
1399}
1400
1401/*
1402 * For device supporting HS200 mode, the following sequence
1403 * should be done before executing the tuning process.
1404 * 1. set the desired bus width(4-bit or 8-bit, 1-bit is not supported)
1405 * 2. switch to HS200 mode
1406 * 3. set the clock to > 52Mhz and <=200MHz
1407 */
1408static int mmc_select_hs200(struct mmc_card *card)
1409{
1410        struct mmc_host *host = card->host;
1411        unsigned int old_timing, old_signal_voltage;
1412        int err = -EINVAL;
1413        u8 val;
1414
1415        old_signal_voltage = host->ios.signal_voltage;
1416        if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS200_1_2V)
1417                err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_120);
1418
1419        if (err && card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS200_1_8V)
1420                err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_180);
1421
1422        /* If fails try again during next card power cycle */
1423        if (err)
1424                return err;
1425
1426        mmc_select_driver_type(card);
1427
1428        /*
1429         * Set the bus width(4 or 8) with host's support and
1430         * switch to HS200 mode if bus width is set successfully.
1431         */
1432        err = mmc_select_bus_width(card);
1433        if (err > 0) {
1434                val = EXT_CSD_TIMING_HS200 |
1435                      card->drive_strength << EXT_CSD_DRV_STR_SHIFT;
1436                err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1437                                   EXT_CSD_HS_TIMING, val,
1438                                   card->ext_csd.generic_cmd6_time, 0,
1439                                   true, false, true);
1440                if (err)
1441                        goto err;
1442                old_timing = host->ios.timing;
1443                mmc_set_timing(host, MMC_TIMING_MMC_HS200);
1444
1445                /*
1446                 * For HS200, CRC errors are not a reliable way to know the
1447                 * switch failed. If there really is a problem, we would expect
1448                 * tuning will fail and the result ends up the same.
1449                 */
1450                err = __mmc_switch_status(card, false);
1451
1452                /*
1453                 * mmc_select_timing() assumes timing has not changed if
1454                 * it is a switch error.
1455                 */
1456                if (err == -EBADMSG)
1457                        mmc_set_timing(host, old_timing);
1458        }
1459err:
1460        if (err) {
1461                /* fall back to the old signal voltage, if fails report error */
1462                if (mmc_set_signal_voltage(host, old_signal_voltage))
1463                        err = -EIO;
1464
1465                pr_err("%s: %s failed, error %d\n", mmc_hostname(card->host),
1466                       __func__, err);
1467        }
1468        return err;
1469}
1470
1471/*
1472 * Activate High Speed, HS200 or HS400ES mode if supported.
1473 */
1474static int mmc_select_timing(struct mmc_card *card)
1475{
1476        int err = 0;
1477
1478        if (!mmc_can_ext_csd(card))
1479                goto bus_speed;
1480
1481        if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS400ES)
1482                err = mmc_select_hs400es(card);
1483        else if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS200)
1484                err = mmc_select_hs200(card);
1485        else if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS)
1486                err = mmc_select_hs(card);
1487
1488        if (err && err != -EBADMSG)
1489                return err;
1490
1491bus_speed:
1492        /*
1493         * Set the bus speed to the selected bus timing.
1494         * If timing is not selected, backward compatible is the default.
1495         */
1496        mmc_set_bus_speed(card);
1497        return 0;
1498}
1499
1500/*
1501 * Execute tuning sequence to seek the proper bus operating
1502 * conditions for HS200 and HS400, which sends CMD21 to the device.
1503 */
1504static int mmc_hs200_tuning(struct mmc_card *card)
1505{
1506        struct mmc_host *host = card->host;
1507
1508        /*
1509         * Timing should be adjusted to the HS400 target
1510         * operation frequency for tuning process
1511         */
1512        if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS400 &&
1513            host->ios.bus_width == MMC_BUS_WIDTH_8)
1514                if (host->ops->prepare_hs400_tuning)
1515                        host->ops->prepare_hs400_tuning(host, &host->ios);
1516
1517        return mmc_execute_tuning(card);
1518}
1519
1520/*
1521 * Handle the detection and initialisation of a card.
1522 *
1523 * In the case of a resume, "oldcard" will contain the card
1524 * we're trying to reinitialise.
1525 */
1526static int mmc_init_card(struct mmc_host *host, u32 ocr,
1527        struct mmc_card *oldcard)
1528{
1529        struct mmc_card *card;
1530        int err;
1531        u32 cid[4];
1532        u32 rocr;
1533
1534        WARN_ON(!host->claimed);
1535
1536        /* Set correct bus mode for MMC before attempting init */
1537        if (!mmc_host_is_spi(host))
1538                mmc_set_bus_mode(host, MMC_BUSMODE_OPENDRAIN);
1539
1540        /*
1541         * Since we're changing the OCR value, we seem to
1542         * need to tell some cards to go back to the idle
1543         * state.  We wait 1ms to give cards time to
1544         * respond.
1545         * mmc_go_idle is needed for eMMC that are asleep
1546         */
1547        mmc_go_idle(host);
1548
1549        /* The extra bit indicates that we support high capacity */
1550        err = mmc_send_op_cond(host, ocr | (1 << 30), &rocr);
1551        if (err)
1552                goto err;
1553
1554        /*
1555         * For SPI, enable CRC as appropriate.
1556         */
1557        if (mmc_host_is_spi(host)) {
1558                err = mmc_spi_set_crc(host, use_spi_crc);
1559                if (err)
1560                        goto err;
1561        }
1562
1563        /*
1564         * Fetch CID from card.
1565         */
1566        err = mmc_send_cid(host, cid);
1567        if (err)
1568                goto err;
1569
1570        if (oldcard) {
1571                if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0) {
1572                        err = -ENOENT;
1573                        goto err;
1574                }
1575
1576                card = oldcard;
1577        } else {
1578                /*
1579                 * Allocate card structure.
1580                 */
1581                card = mmc_alloc_card(host, &mmc_type);
1582                if (IS_ERR(card)) {
1583                        err = PTR_ERR(card);
1584                        goto err;
1585                }
1586
1587                card->ocr = ocr;
1588                card->type = MMC_TYPE_MMC;
1589                card->rca = 1;
1590                memcpy(card->raw_cid, cid, sizeof(card->raw_cid));
1591        }
1592
1593        /*
1594         * Call the optional HC's init_card function to handle quirks.
1595         */
1596        if (host->ops->init_card)
1597                host->ops->init_card(host, card);
1598
1599        /*
1600         * For native busses:  set card RCA and quit open drain mode.
1601         */
1602        if (!mmc_host_is_spi(host)) {
1603                err = mmc_set_relative_addr(card);
1604                if (err)
1605                        goto free_card;
1606
1607                mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
1608        }
1609
1610        if (!oldcard) {
1611                /*
1612                 * Fetch CSD from card.
1613                 */
1614                err = mmc_send_csd(card, card->raw_csd);
1615                if (err)
1616                        goto free_card;
1617
1618                err = mmc_decode_csd(card);
1619                if (err)
1620                        goto free_card;
1621                err = mmc_decode_cid(card);
1622                if (err)
1623                        goto free_card;
1624        }
1625
1626        /*
1627         * handling only for cards supporting DSR and hosts requesting
1628         * DSR configuration
1629         */
1630        if (card->csd.dsr_imp && host->dsr_req)
1631                mmc_set_dsr(host);
1632
1633        /*
1634         * Select card, as all following commands rely on that.
1635         */
1636        if (!mmc_host_is_spi(host)) {
1637                err = mmc_select_card(card);
1638                if (err)
1639                        goto free_card;
1640        }
1641
1642        if (!oldcard) {
1643                /* Read extended CSD. */
1644                err = mmc_read_ext_csd(card);
1645                if (err)
1646                        goto free_card;
1647
1648                /*
1649                 * If doing byte addressing, check if required to do sector
1650                 * addressing.  Handle the case of <2GB cards needing sector
1651                 * addressing.  See section 8.1 JEDEC Standard JED84-A441;
1652                 * ocr register has bit 30 set for sector addressing.
1653                 */
1654                if (rocr & BIT(30))
1655                        mmc_card_set_blockaddr(card);
1656
1657                /* Erase size depends on CSD and Extended CSD */
1658                mmc_set_erase_size(card);
1659        }
1660
1661        /* Enable ERASE_GRP_DEF. This bit is lost after a reset or power off. */
1662        if (card->ext_csd.rev >= 3) {
1663                err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1664                                 EXT_CSD_ERASE_GROUP_DEF, 1,
1665                                 card->ext_csd.generic_cmd6_time);
1666
1667                if (err && err != -EBADMSG)
1668                        goto free_card;
1669
1670                if (err) {
1671                        err = 0;
1672                        /*
1673                         * Just disable enhanced area off & sz
1674                         * will try to enable ERASE_GROUP_DEF
1675                         * during next time reinit
1676                         */
1677                        card->ext_csd.enhanced_area_offset = -EINVAL;
1678                        card->ext_csd.enhanced_area_size = -EINVAL;
1679                } else {
1680                        card->ext_csd.erase_group_def = 1;
1681                        /*
1682                         * enable ERASE_GRP_DEF successfully.
1683                         * This will affect the erase size, so
1684                         * here need to reset erase size
1685                         */
1686                        mmc_set_erase_size(card);
1687                }
1688        }
1689
1690        /*
1691         * Ensure eMMC user default partition is enabled
1692         */
1693        if (card->ext_csd.part_config & EXT_CSD_PART_CONFIG_ACC_MASK) {
1694                card->ext_csd.part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
1695                err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_PART_CONFIG,
1696                                 card->ext_csd.part_config,
1697                                 card->ext_csd.part_time);
1698                if (err && err != -EBADMSG)
1699                        goto free_card;
1700        }
1701
1702        /*
1703         * Enable power_off_notification byte in the ext_csd register
1704         */
1705        if (card->ext_csd.rev >= 6) {
1706                err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1707                                 EXT_CSD_POWER_OFF_NOTIFICATION,
1708                                 EXT_CSD_POWER_ON,
1709                                 card->ext_csd.generic_cmd6_time);
1710                if (err && err != -EBADMSG)
1711                        goto free_card;
1712
1713                /*
1714                 * The err can be -EBADMSG or 0,
1715                 * so check for success and update the flag
1716                 */
1717                if (!err)
1718                        card->ext_csd.power_off_notification = EXT_CSD_POWER_ON;
1719        }
1720
1721        /*
1722         * Select timing interface
1723         */
1724        err = mmc_select_timing(card);
1725        if (err)
1726                goto free_card;
1727
1728        if (mmc_card_hs200(card)) {
1729                err = mmc_hs200_tuning(card);
1730                if (err)
1731                        goto free_card;
1732
1733                err = mmc_select_hs400(card);
1734                if (err)
1735                        goto free_card;
1736        } else if (!mmc_card_hs400es(card)) {
1737                /* Select the desired bus width optionally */
1738                err = mmc_select_bus_width(card);
1739                if (err > 0 && mmc_card_hs(card)) {
1740                        err = mmc_select_hs_ddr(card);
1741                        if (err)
1742                                goto free_card;
1743                }
1744        }
1745
1746        /*
1747         * Choose the power class with selected bus interface
1748         */
1749        mmc_select_powerclass(card);
1750
1751        /*
1752         * Enable HPI feature (if supported)
1753         */
1754        if (card->ext_csd.hpi) {
1755                err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1756                                EXT_CSD_HPI_MGMT, 1,
1757                                card->ext_csd.generic_cmd6_time);
1758                if (err && err != -EBADMSG)
1759                        goto free_card;
1760                if (err) {
1761                        pr_warn("%s: Enabling HPI failed\n",
1762                                mmc_hostname(card->host));
1763                        err = 0;
1764                } else
1765                        card->ext_csd.hpi_en = 1;
1766        }
1767
1768        /*
1769         * If cache size is higher than 0, this indicates
1770         * the existence of cache and it can be turned on.
1771         */
1772        if (!mmc_card_broken_hpi(card) &&
1773            card->ext_csd.cache_size > 0) {
1774                err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1775                                EXT_CSD_CACHE_CTRL, 1,
1776                                card->ext_csd.generic_cmd6_time);
1777                if (err && err != -EBADMSG)
1778                        goto free_card;
1779
1780                /*
1781                 * Only if no error, cache is turned on successfully.
1782                 */
1783                if (err) {
1784                        pr_warn("%s: Cache is supported, but failed to turn on (%d)\n",
1785                                mmc_hostname(card->host), err);
1786                        card->ext_csd.cache_ctrl = 0;
1787                        err = 0;
1788                } else {
1789                        card->ext_csd.cache_ctrl = 1;
1790                }
1791        }
1792
1793        /*
1794         * Enable Command Queue if supported. Note that Packed Commands cannot
1795         * be used with Command Queue.
1796         */
1797        card->ext_csd.cmdq_en = false;
1798        if (card->ext_csd.cmdq_support && host->caps2 & MMC_CAP2_CQE) {
1799                err = mmc_cmdq_enable(card);
1800                if (err && err != -EBADMSG)
1801                        goto free_card;
1802                if (err) {
1803                        pr_warn("%s: Enabling CMDQ failed\n",
1804                                mmc_hostname(card->host));
1805                        card->ext_csd.cmdq_support = false;
1806                        card->ext_csd.cmdq_depth = 0;
1807                        err = 0;
1808                }
1809        }
1810        /*
1811         * In some cases (e.g. RPMB or mmc_test), the Command Queue must be
1812         * disabled for a time, so a flag is needed to indicate to re-enable the
1813         * Command Queue.
1814         */
1815        card->reenable_cmdq = card->ext_csd.cmdq_en;
1816
1817        if (card->ext_csd.cmdq_en && !host->cqe_enabled) {
1818                err = host->cqe_ops->cqe_enable(host, card);
1819                if (err) {
1820                        pr_err("%s: Failed to enable CQE, error %d\n",
1821                                mmc_hostname(host), err);
1822                } else {
1823                        host->cqe_enabled = true;
1824                        pr_info("%s: Command Queue Engine enabled\n",
1825                                mmc_hostname(host));
1826                }
1827        }
1828
1829        if (!oldcard)
1830                host->card = card;
1831
1832        return 0;
1833
1834free_card:
1835        if (!oldcard)
1836                mmc_remove_card(card);
1837err:
1838        return err;
1839}
1840
1841static int mmc_can_sleep(struct mmc_card *card)
1842{
1843        return (card && card->ext_csd.rev >= 3);
1844}
1845
1846static int mmc_sleep(struct mmc_host *host)
1847{
1848        struct mmc_command cmd = {};
1849        struct mmc_card *card = host->card;
1850        unsigned int timeout_ms = DIV_ROUND_UP(card->ext_csd.sa_timeout, 10000);
1851        int err;
1852
1853        /* Re-tuning can't be done once the card is deselected */
1854        mmc_retune_hold(host);
1855
1856        err = mmc_deselect_cards(host);
1857        if (err)
1858                goto out_release;
1859
1860        cmd.opcode = MMC_SLEEP_AWAKE;
1861        cmd.arg = card->rca << 16;
1862        cmd.arg |= 1 << 15;
1863
1864        /*
1865         * If the max_busy_timeout of the host is specified, validate it against
1866         * the sleep cmd timeout. A failure means we need to prevent the host
1867         * from doing hw busy detection, which is done by converting to a R1
1868         * response instead of a R1B.
1869         */
1870        if (host->max_busy_timeout && (timeout_ms > host->max_busy_timeout)) {
1871                cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
1872        } else {
1873                cmd.flags = MMC_RSP_R1B | MMC_CMD_AC;
1874                cmd.busy_timeout = timeout_ms;
1875        }
1876
1877        err = mmc_wait_for_cmd(host, &cmd, 0);
1878        if (err)
1879                goto out_release;
1880
1881        /*
1882         * If the host does not wait while the card signals busy, then we will
1883         * will have to wait the sleep/awake timeout.  Note, we cannot use the
1884         * SEND_STATUS command to poll the status because that command (and most
1885         * others) is invalid while the card sleeps.
1886         */
1887        if (!cmd.busy_timeout || !(host->caps & MMC_CAP_WAIT_WHILE_BUSY))
1888                mmc_delay(timeout_ms);
1889
1890out_release:
1891        mmc_retune_release(host);
1892        return err;
1893}
1894
1895static int mmc_can_poweroff_notify(const struct mmc_card *card)
1896{
1897        return card &&
1898                mmc_card_mmc(card) &&
1899                (card->ext_csd.power_off_notification == EXT_CSD_POWER_ON);
1900}
1901
1902static int mmc_poweroff_notify(struct mmc_card *card, unsigned int notify_type)
1903{
1904        unsigned int timeout = card->ext_csd.generic_cmd6_time;
1905        int err;
1906
1907        /* Use EXT_CSD_POWER_OFF_SHORT as default notification type. */
1908        if (notify_type == EXT_CSD_POWER_OFF_LONG)
1909                timeout = card->ext_csd.power_off_longtime;
1910
1911        err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1912                        EXT_CSD_POWER_OFF_NOTIFICATION,
1913                        notify_type, timeout, 0, true, false, false);
1914        if (err)
1915                pr_err("%s: Power Off Notification timed out, %u\n",
1916                       mmc_hostname(card->host), timeout);
1917
1918        /* Disable the power off notification after the switch operation. */
1919        card->ext_csd.power_off_notification = EXT_CSD_NO_POWER_NOTIFICATION;
1920
1921        return err;
1922}
1923
1924/*
1925 * Host is being removed. Free up the current card.
1926 */
1927static void mmc_remove(struct mmc_host *host)
1928{
1929        mmc_remove_card(host->card);
1930        host->card = NULL;
1931}
1932
1933/*
1934 * Card detection - card is alive.
1935 */
1936static int mmc_alive(struct mmc_host *host)
1937{
1938        return mmc_send_status(host->card, NULL);
1939}
1940
1941/*
1942 * Card detection callback from host.
1943 */
1944static void mmc_detect(struct mmc_host *host)
1945{
1946        int err;
1947
1948        mmc_get_card(host->card, NULL);
1949
1950        /*
1951         * Just check if our card has been removed.
1952         */
1953        err = _mmc_detect_card_removed(host);
1954
1955        mmc_put_card(host->card, NULL);
1956
1957        if (err) {
1958                mmc_remove(host);
1959
1960                mmc_claim_host(host);
1961                mmc_detach_bus(host);
1962                mmc_power_off(host);
1963                mmc_release_host(host);
1964        }
1965}
1966
1967static int _mmc_suspend(struct mmc_host *host, bool is_suspend)
1968{
1969        int err = 0;
1970        unsigned int notify_type = is_suspend ? EXT_CSD_POWER_OFF_SHORT :
1971                                        EXT_CSD_POWER_OFF_LONG;
1972
1973        mmc_claim_host(host);
1974
1975        if (mmc_card_suspended(host->card))
1976                goto out;
1977
1978        if (mmc_card_doing_bkops(host->card)) {
1979                err = mmc_stop_bkops(host->card);
1980                if (err)
1981                        goto out;
1982        }
1983
1984        err = mmc_flush_cache(host->card);
1985        if (err)
1986                goto out;
1987
1988        if (mmc_can_poweroff_notify(host->card) &&
1989                ((host->caps2 & MMC_CAP2_FULL_PWR_CYCLE) || !is_suspend))
1990                err = mmc_poweroff_notify(host->card, notify_type);
1991        else if (mmc_can_sleep(host->card))
1992                err = mmc_sleep(host);
1993        else if (!mmc_host_is_spi(host))
1994                err = mmc_deselect_cards(host);
1995
1996        if (!err) {
1997                mmc_power_off(host);
1998                mmc_card_set_suspended(host->card);
1999        }
2000out:
2001        mmc_release_host(host);
2002        return err;
2003}
2004
2005/*
2006 * Suspend callback
2007 */
2008static int mmc_suspend(struct mmc_host *host)
2009{
2010        int err;
2011
2012        err = _mmc_suspend(host, true);
2013        if (!err) {
2014                pm_runtime_disable(&host->card->dev);
2015                pm_runtime_set_suspended(&host->card->dev);
2016        }
2017
2018        return err;
2019}
2020
2021/*
2022 * This function tries to determine if the same card is still present
2023 * and, if so, restore all state to it.
2024 */
2025static int _mmc_resume(struct mmc_host *host)
2026{
2027        int err = 0;
2028
2029        mmc_claim_host(host);
2030
2031        if (!mmc_card_suspended(host->card))
2032                goto out;
2033
2034        mmc_power_up(host, host->card->ocr);
2035        err = mmc_init_card(host, host->card->ocr, host->card);
2036        mmc_card_clr_suspended(host->card);
2037
2038out:
2039        mmc_release_host(host);
2040        return err;
2041}
2042
2043/*
2044 * Shutdown callback
2045 */
2046static int mmc_shutdown(struct mmc_host *host)
2047{
2048        int err = 0;
2049
2050        /*
2051         * In a specific case for poweroff notify, we need to resume the card
2052         * before we can shutdown it properly.
2053         */
2054        if (mmc_can_poweroff_notify(host->card) &&
2055                !(host->caps2 & MMC_CAP2_FULL_PWR_CYCLE))
2056                err = _mmc_resume(host);
2057
2058        if (!err)
2059                err = _mmc_suspend(host, false);
2060
2061        return err;
2062}
2063
2064/*
2065 * Callback for resume.
2066 */
2067static int mmc_resume(struct mmc_host *host)
2068{
2069        pm_runtime_enable(&host->card->dev);
2070        return 0;
2071}
2072
2073/*
2074 * Callback for runtime_suspend.
2075 */
2076static int mmc_runtime_suspend(struct mmc_host *host)
2077{
2078        int err;
2079
2080        if (!(host->caps & MMC_CAP_AGGRESSIVE_PM))
2081                return 0;
2082
2083        err = _mmc_suspend(host, true);
2084        if (err)
2085                pr_err("%s: error %d doing aggressive suspend\n",
2086                        mmc_hostname(host), err);
2087
2088        return err;
2089}
2090
2091/*
2092 * Callback for runtime_resume.
2093 */
2094static int mmc_runtime_resume(struct mmc_host *host)
2095{
2096        int err;
2097
2098        err = _mmc_resume(host);
2099        if (err && err != -ENOMEDIUM)
2100                pr_err("%s: error %d doing runtime resume\n",
2101                        mmc_hostname(host), err);
2102
2103        return 0;
2104}
2105
2106static int mmc_can_reset(struct mmc_card *card)
2107{
2108        u8 rst_n_function;
2109
2110        rst_n_function = card->ext_csd.rst_n_function;
2111        if ((rst_n_function & EXT_CSD_RST_N_EN_MASK) != EXT_CSD_RST_N_ENABLED)
2112                return 0;
2113        return 1;
2114}
2115
2116static int mmc_reset(struct mmc_host *host)
2117{
2118        struct mmc_card *card = host->card;
2119
2120        /*
2121         * In the case of recovery, we can't expect flushing the cache to work
2122         * always, but we have a go and ignore errors.
2123         */
2124        mmc_flush_cache(host->card);
2125
2126        if ((host->caps & MMC_CAP_HW_RESET) && host->ops->hw_reset &&
2127             mmc_can_reset(card)) {
2128                /* If the card accept RST_n signal, send it. */
2129                mmc_set_clock(host, host->f_init);
2130                host->ops->hw_reset(host);
2131                /* Set initial state and call mmc_set_ios */
2132                mmc_set_initial_state(host);
2133        } else {
2134                /* Do a brute force power cycle */
2135                mmc_power_cycle(host, card->ocr);
2136        }
2137        return mmc_init_card(host, card->ocr, card);
2138}
2139
2140static const struct mmc_bus_ops mmc_ops = {
2141        .remove = mmc_remove,
2142        .detect = mmc_detect,
2143        .suspend = mmc_suspend,
2144        .resume = mmc_resume,
2145        .runtime_suspend = mmc_runtime_suspend,
2146        .runtime_resume = mmc_runtime_resume,
2147        .alive = mmc_alive,
2148        .shutdown = mmc_shutdown,
2149        .reset = mmc_reset,
2150};
2151
2152/*
2153 * Starting point for MMC card init.
2154 */
2155int mmc_attach_mmc(struct mmc_host *host)
2156{
2157        int err;
2158        u32 ocr, rocr;
2159
2160        WARN_ON(!host->claimed);
2161
2162        /* Set correct bus mode for MMC before attempting attach */
2163        if (!mmc_host_is_spi(host))
2164                mmc_set_bus_mode(host, MMC_BUSMODE_OPENDRAIN);
2165
2166        err = mmc_send_op_cond(host, 0, &ocr);
2167        if (err)
2168                return err;
2169
2170        mmc_attach_bus(host, &mmc_ops);
2171        if (host->ocr_avail_mmc)
2172                host->ocr_avail = host->ocr_avail_mmc;
2173
2174        /*
2175         * We need to get OCR a different way for SPI.
2176         */
2177        if (mmc_host_is_spi(host)) {
2178                err = mmc_spi_read_ocr(host, 1, &ocr);
2179                if (err)
2180                        goto err;
2181        }
2182
2183        rocr = mmc_select_voltage(host, ocr);
2184
2185        /*
2186         * Can we support the voltage of the card?
2187         */
2188        if (!rocr) {
2189                err = -EINVAL;
2190                goto err;
2191        }
2192
2193        /*
2194         * Detect and init the card.
2195         */
2196        err = mmc_init_card(host, rocr, NULL);
2197        if (err)
2198                goto err;
2199
2200        mmc_release_host(host);
2201        err = mmc_add_card(host->card);
2202        if (err)
2203                goto remove_card;
2204
2205        mmc_claim_host(host);
2206        return 0;
2207
2208remove_card:
2209        mmc_remove_card(host->card);
2210        mmc_claim_host(host);
2211        host->card = NULL;
2212err:
2213        mmc_detach_bus(host);
2214
2215        pr_err("%s: error %d whilst initialising MMC card\n",
2216                mmc_hostname(host), err);
2217
2218        return err;
2219}
2220