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