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