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