1 /* 2 * linux/drivers/mmc/core/sd.c 3 * 4 * Copyright (C) 2003-2004 Russell King, All Rights Reserved. 5 * SD support Copyright (C) 2004 Ian Molton, All Rights Reserved. 6 * Copyright (C) 2005-2007 Pierre Ossman, 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/sizes.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 #include <linux/mmc/sd.h> 23 24 #include "core.h" 25 #include "bus.h" 26 #include "mmc_ops.h" 27 #include "sd.h" 28 #include "sd_ops.h" 29 30 static const unsigned int tran_exp[] = { 31 10000, 100000, 1000000, 10000000, 32 0, 0, 0, 0 33 }; 34 35 static const unsigned char tran_mant[] = { 36 0, 10, 12, 13, 15, 20, 25, 30, 37 35, 40, 45, 50, 55, 60, 70, 80, 38 }; 39 40 static const unsigned int tacc_exp[] = { 41 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 42 }; 43 44 static const unsigned int tacc_mant[] = { 45 0, 10, 12, 13, 15, 20, 25, 30, 46 35, 40, 45, 50, 55, 60, 70, 80, 47 }; 48 49 static const unsigned int sd_au_size[] = { 50 0, SZ_16K / 512, SZ_32K / 512, SZ_64K / 512, 51 SZ_128K / 512, SZ_256K / 512, SZ_512K / 512, SZ_1M / 512, 52 SZ_2M / 512, SZ_4M / 512, SZ_8M / 512, (SZ_8M + SZ_4M) / 512, 53 SZ_16M / 512, (SZ_16M + SZ_8M) / 512, SZ_32M / 512, SZ_64M / 512, 54 }; 55 56 #define UNSTUFF_BITS(resp,start,size) \ 57 ({ \ 58 const int __size = size; \ 59 const u32 __mask = (__size < 32 ? 1 << __size : 0) - 1; \ 60 const int __off = 3 - ((start) / 32); \ 61 const int __shft = (start) & 31; \ 62 u32 __res; \ 63 \ 64 __res = resp[__off] >> __shft; \ 65 if (__size + __shft > 32) \ 66 __res |= resp[__off-1] << ((32 - __shft) % 32); \ 67 __res & __mask; \ 68 }) 69 70 /* 71 * Given the decoded CSD structure, decode the raw CID to our CID structure. 72 */ 73 void mmc_decode_cid(struct mmc_card *card) 74 { 75 u32 *resp = card->raw_cid; 76 77 /* 78 * SD doesn't currently have a version field so we will 79 * have to assume we can parse this. 80 */ 81 card->cid.manfid = UNSTUFF_BITS(resp, 120, 8); 82 card->cid.oemid = UNSTUFF_BITS(resp, 104, 16); 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.hwrev = UNSTUFF_BITS(resp, 60, 4); 89 card->cid.fwrev = UNSTUFF_BITS(resp, 56, 4); 90 card->cid.serial = UNSTUFF_BITS(resp, 24, 32); 91 card->cid.year = UNSTUFF_BITS(resp, 12, 8); 92 card->cid.month = UNSTUFF_BITS(resp, 8, 4); 93 94 card->cid.year += 2000; /* SD cards year offset */ 95 } 96 97 /* 98 * Given a 128-bit response, decode to our card CSD structure. 99 */ 100 static int mmc_decode_csd(struct mmc_card *card) 101 { 102 struct mmc_csd *csd = &card->csd; 103 unsigned int e, m, csd_struct; 104 u32 *resp = card->raw_csd; 105 106 csd_struct = UNSTUFF_BITS(resp, 126, 2); 107 108 switch (csd_struct) { 109 case 0: 110 m = UNSTUFF_BITS(resp, 115, 4); 111 e = UNSTUFF_BITS(resp, 112, 3); 112 csd->tacc_ns = (tacc_exp[e] * tacc_mant[m] + 9) / 10; 113 csd->tacc_clks = UNSTUFF_BITS(resp, 104, 8) * 100; 114 115 m = UNSTUFF_BITS(resp, 99, 4); 116 e = UNSTUFF_BITS(resp, 96, 3); 117 csd->max_dtr = tran_exp[e] * tran_mant[m]; 118 csd->cmdclass = UNSTUFF_BITS(resp, 84, 12); 119 120 e = UNSTUFF_BITS(resp, 47, 3); 121 m = UNSTUFF_BITS(resp, 62, 12); 122 csd->capacity = (1 + m) << (e + 2); 123 124 csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4); 125 csd->read_partial = UNSTUFF_BITS(resp, 79, 1); 126 csd->write_misalign = UNSTUFF_BITS(resp, 78, 1); 127 csd->read_misalign = UNSTUFF_BITS(resp, 77, 1); 128 csd->dsr_imp = UNSTUFF_BITS(resp, 76, 1); 129 csd->r2w_factor = UNSTUFF_BITS(resp, 26, 3); 130 csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4); 131 csd->write_partial = UNSTUFF_BITS(resp, 21, 1); 132 133 if (UNSTUFF_BITS(resp, 46, 1)) { 134 csd->erase_size = 1; 135 } else if (csd->write_blkbits >= 9) { 136 csd->erase_size = UNSTUFF_BITS(resp, 39, 7) + 1; 137 csd->erase_size <<= csd->write_blkbits - 9; 138 } 139 break; 140 case 1: 141 /* 142 * This is a block-addressed SDHC or SDXC card. Most 143 * interesting fields are unused and have fixed 144 * values. To avoid getting tripped by buggy cards, 145 * we assume those fixed values ourselves. 146 */ 147 mmc_card_set_blockaddr(card); 148 149 csd->tacc_ns = 0; /* Unused */ 150 csd->tacc_clks = 0; /* Unused */ 151 152 m = UNSTUFF_BITS(resp, 99, 4); 153 e = UNSTUFF_BITS(resp, 96, 3); 154 csd->max_dtr = tran_exp[e] * tran_mant[m]; 155 csd->cmdclass = UNSTUFF_BITS(resp, 84, 12); 156 csd->c_size = UNSTUFF_BITS(resp, 48, 22); 157 158 /* SDXC cards have a minimum C_SIZE of 0x00FFFF */ 159 if (csd->c_size >= 0xFFFF) 160 mmc_card_set_ext_capacity(card); 161 162 m = UNSTUFF_BITS(resp, 48, 22); 163 csd->capacity = (1 + m) << 10; 164 165 csd->read_blkbits = 9; 166 csd->read_partial = 0; 167 csd->write_misalign = 0; 168 csd->read_misalign = 0; 169 csd->r2w_factor = 4; /* Unused */ 170 csd->write_blkbits = 9; 171 csd->write_partial = 0; 172 csd->erase_size = 1; 173 break; 174 default: 175 pr_err("%s: unrecognised CSD structure version %d\n", 176 mmc_hostname(card->host), csd_struct); 177 return -EINVAL; 178 } 179 180 card->erase_size = csd->erase_size; 181 182 return 0; 183 } 184 185 /* 186 * Given a 64-bit response, decode to our card SCR structure. 187 */ 188 static int mmc_decode_scr(struct mmc_card *card) 189 { 190 struct sd_scr *scr = &card->scr; 191 unsigned int scr_struct; 192 u32 resp[4]; 193 194 resp[3] = card->raw_scr[1]; 195 resp[2] = card->raw_scr[0]; 196 197 scr_struct = UNSTUFF_BITS(resp, 60, 4); 198 if (scr_struct != 0) { 199 pr_err("%s: unrecognised SCR structure version %d\n", 200 mmc_hostname(card->host), scr_struct); 201 return -EINVAL; 202 } 203 204 scr->sda_vsn = UNSTUFF_BITS(resp, 56, 4); 205 scr->bus_widths = UNSTUFF_BITS(resp, 48, 4); 206 if (scr->sda_vsn == SCR_SPEC_VER_2) 207 /* Check if Physical Layer Spec v3.0 is supported */ 208 scr->sda_spec3 = UNSTUFF_BITS(resp, 47, 1); 209 210 if (UNSTUFF_BITS(resp, 55, 1)) 211 card->erased_byte = 0xFF; 212 else 213 card->erased_byte = 0x0; 214 215 if (scr->sda_spec3) 216 scr->cmds = UNSTUFF_BITS(resp, 32, 2); 217 return 0; 218 } 219 220 /* 221 * Fetch and process SD Status register. 222 */ 223 static int mmc_read_ssr(struct mmc_card *card) 224 { 225 unsigned int au, es, et, eo; 226 int i; 227 228 if (!(card->csd.cmdclass & CCC_APP_SPEC)) { 229 pr_warn("%s: card lacks mandatory SD Status function\n", 230 mmc_hostname(card->host)); 231 return 0; 232 } 233 234 if (mmc_app_sd_status(card, card->raw_ssr)) { 235 pr_warn("%s: problem reading SD Status register\n", 236 mmc_hostname(card->host)); 237 return 0; 238 } 239 240 for (i = 0; i < 16; i++) 241 card->raw_ssr[i] = be32_to_cpu(card->raw_ssr[i]); 242 243 /* 244 * UNSTUFF_BITS only works with four u32s so we have to offset the 245 * bitfield positions accordingly. 246 */ 247 au = UNSTUFF_BITS(card->raw_ssr, 428 - 384, 4); 248 if (au) { 249 if (au <= 9 || card->scr.sda_spec3) { 250 card->ssr.au = sd_au_size[au]; 251 es = UNSTUFF_BITS(card->raw_ssr, 408 - 384, 16); 252 et = UNSTUFF_BITS(card->raw_ssr, 402 - 384, 6); 253 if (es && et) { 254 eo = UNSTUFF_BITS(card->raw_ssr, 400 - 384, 2); 255 card->ssr.erase_timeout = (et * 1000) / es; 256 card->ssr.erase_offset = eo * 1000; 257 } 258 } else { 259 pr_warn("%s: SD Status: Invalid Allocation Unit size\n", 260 mmc_hostname(card->host)); 261 } 262 } 263 264 return 0; 265 } 266 267 /* 268 * Fetches and decodes switch information 269 */ 270 static int mmc_read_switch(struct mmc_card *card) 271 { 272 int err; 273 u8 *status; 274 275 if (card->scr.sda_vsn < SCR_SPEC_VER_1) 276 return 0; 277 278 if (!(card->csd.cmdclass & CCC_SWITCH)) { 279 pr_warn("%s: card lacks mandatory switch function, performance might suffer\n", 280 mmc_hostname(card->host)); 281 return 0; 282 } 283 284 err = -EIO; 285 286 status = kmalloc(64, GFP_KERNEL); 287 if (!status) { 288 pr_err("%s: could not allocate a buffer for " 289 "switch capabilities.\n", 290 mmc_hostname(card->host)); 291 return -ENOMEM; 292 } 293 294 /* 295 * Find out the card's support bits with a mode 0 operation. 296 * The argument does not matter, as the support bits do not 297 * change with the arguments. 298 */ 299 err = mmc_sd_switch(card, 0, 0, 0, status); 300 if (err) { 301 /* 302 * If the host or the card can't do the switch, 303 * fail more gracefully. 304 */ 305 if (err != -EINVAL && err != -ENOSYS && err != -EFAULT) 306 goto out; 307 308 pr_warn("%s: problem reading Bus Speed modes\n", 309 mmc_hostname(card->host)); 310 err = 0; 311 312 goto out; 313 } 314 315 if (status[13] & SD_MODE_HIGH_SPEED) 316 card->sw_caps.hs_max_dtr = HIGH_SPEED_MAX_DTR; 317 318 if (card->scr.sda_spec3) { 319 card->sw_caps.sd3_bus_mode = status[13]; 320 /* Driver Strengths supported by the card */ 321 card->sw_caps.sd3_drv_type = status[9]; 322 card->sw_caps.sd3_curr_limit = status[7] | status[6] << 8; 323 } 324 325 out: 326 kfree(status); 327 328 return err; 329 } 330 331 /* 332 * Test if the card supports high-speed mode and, if so, switch to it. 333 */ 334 int mmc_sd_switch_hs(struct mmc_card *card) 335 { 336 int err; 337 u8 *status; 338 339 if (card->scr.sda_vsn < SCR_SPEC_VER_1) 340 return 0; 341 342 if (!(card->csd.cmdclass & CCC_SWITCH)) 343 return 0; 344 345 if (!(card->host->caps & MMC_CAP_SD_HIGHSPEED)) 346 return 0; 347 348 if (card->sw_caps.hs_max_dtr == 0) 349 return 0; 350 351 status = kmalloc(64, GFP_KERNEL); 352 if (!status) { 353 pr_err("%s: could not allocate a buffer for " 354 "switch capabilities.\n", mmc_hostname(card->host)); 355 return -ENOMEM; 356 } 357 358 err = mmc_sd_switch(card, 1, 0, 1, status); 359 if (err) 360 goto out; 361 362 if ((status[16] & 0xF) != 1) { 363 pr_warn("%s: Problem switching card into high-speed mode!\n", 364 mmc_hostname(card->host)); 365 err = 0; 366 } else { 367 err = 1; 368 } 369 370 out: 371 kfree(status); 372 373 return err; 374 } 375 376 static int sd_select_driver_type(struct mmc_card *card, u8 *status) 377 { 378 int card_drv_type, drive_strength, drv_type; 379 int err; 380 381 card->drive_strength = 0; 382 383 card_drv_type = card->sw_caps.sd3_drv_type | SD_DRIVER_TYPE_B; 384 385 drive_strength = mmc_select_drive_strength(card, 386 card->sw_caps.uhs_max_dtr, 387 card_drv_type, &drv_type); 388 389 if (drive_strength) { 390 err = mmc_sd_switch(card, 1, 2, drive_strength, status); 391 if (err) 392 return err; 393 if ((status[15] & 0xF) != drive_strength) { 394 pr_warn("%s: Problem setting drive strength!\n", 395 mmc_hostname(card->host)); 396 return 0; 397 } 398 card->drive_strength = drive_strength; 399 } 400 401 if (drv_type) 402 mmc_set_driver_type(card->host, drv_type); 403 404 return 0; 405 } 406 407 static void sd_update_bus_speed_mode(struct mmc_card *card) 408 { 409 /* 410 * If the host doesn't support any of the UHS-I modes, fallback on 411 * default speed. 412 */ 413 if (!mmc_host_uhs(card->host)) { 414 card->sd_bus_speed = 0; 415 return; 416 } 417 418 if ((card->host->caps & MMC_CAP_UHS_SDR104) && 419 (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_SDR104)) { 420 card->sd_bus_speed = UHS_SDR104_BUS_SPEED; 421 } else if ((card->host->caps & MMC_CAP_UHS_DDR50) && 422 (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_DDR50)) { 423 card->sd_bus_speed = UHS_DDR50_BUS_SPEED; 424 } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 | 425 MMC_CAP_UHS_SDR50)) && (card->sw_caps.sd3_bus_mode & 426 SD_MODE_UHS_SDR50)) { 427 card->sd_bus_speed = UHS_SDR50_BUS_SPEED; 428 } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 | 429 MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR25)) && 430 (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_SDR25)) { 431 card->sd_bus_speed = UHS_SDR25_BUS_SPEED; 432 } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 | 433 MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR25 | 434 MMC_CAP_UHS_SDR12)) && (card->sw_caps.sd3_bus_mode & 435 SD_MODE_UHS_SDR12)) { 436 card->sd_bus_speed = UHS_SDR12_BUS_SPEED; 437 } 438 } 439 440 static int sd_set_bus_speed_mode(struct mmc_card *card, u8 *status) 441 { 442 int err; 443 unsigned int timing = 0; 444 445 switch (card->sd_bus_speed) { 446 case UHS_SDR104_BUS_SPEED: 447 timing = MMC_TIMING_UHS_SDR104; 448 card->sw_caps.uhs_max_dtr = UHS_SDR104_MAX_DTR; 449 break; 450 case UHS_DDR50_BUS_SPEED: 451 timing = MMC_TIMING_UHS_DDR50; 452 card->sw_caps.uhs_max_dtr = UHS_DDR50_MAX_DTR; 453 break; 454 case UHS_SDR50_BUS_SPEED: 455 timing = MMC_TIMING_UHS_SDR50; 456 card->sw_caps.uhs_max_dtr = UHS_SDR50_MAX_DTR; 457 break; 458 case UHS_SDR25_BUS_SPEED: 459 timing = MMC_TIMING_UHS_SDR25; 460 card->sw_caps.uhs_max_dtr = UHS_SDR25_MAX_DTR; 461 break; 462 case UHS_SDR12_BUS_SPEED: 463 timing = MMC_TIMING_UHS_SDR12; 464 card->sw_caps.uhs_max_dtr = UHS_SDR12_MAX_DTR; 465 break; 466 default: 467 return 0; 468 } 469 470 err = mmc_sd_switch(card, 1, 0, card->sd_bus_speed, status); 471 if (err) 472 return err; 473 474 if ((status[16] & 0xF) != card->sd_bus_speed) 475 pr_warn("%s: Problem setting bus speed mode!\n", 476 mmc_hostname(card->host)); 477 else { 478 mmc_set_timing(card->host, timing); 479 mmc_set_clock(card->host, card->sw_caps.uhs_max_dtr); 480 } 481 482 return 0; 483 } 484 485 /* Get host's max current setting at its current voltage */ 486 static u32 sd_get_host_max_current(struct mmc_host *host) 487 { 488 u32 voltage, max_current; 489 490 voltage = 1 << host->ios.vdd; 491 switch (voltage) { 492 case MMC_VDD_165_195: 493 max_current = host->max_current_180; 494 break; 495 case MMC_VDD_29_30: 496 case MMC_VDD_30_31: 497 max_current = host->max_current_300; 498 break; 499 case MMC_VDD_32_33: 500 case MMC_VDD_33_34: 501 max_current = host->max_current_330; 502 break; 503 default: 504 max_current = 0; 505 } 506 507 return max_current; 508 } 509 510 static int sd_set_current_limit(struct mmc_card *card, u8 *status) 511 { 512 int current_limit = SD_SET_CURRENT_NO_CHANGE; 513 int err; 514 u32 max_current; 515 516 /* 517 * Current limit switch is only defined for SDR50, SDR104, and DDR50 518 * bus speed modes. For other bus speed modes, we do not change the 519 * current limit. 520 */ 521 if ((card->sd_bus_speed != UHS_SDR50_BUS_SPEED) && 522 (card->sd_bus_speed != UHS_SDR104_BUS_SPEED) && 523 (card->sd_bus_speed != UHS_DDR50_BUS_SPEED)) 524 return 0; 525 526 /* 527 * Host has different current capabilities when operating at 528 * different voltages, so find out its max current first. 529 */ 530 max_current = sd_get_host_max_current(card->host); 531 532 /* 533 * We only check host's capability here, if we set a limit that is 534 * higher than the card's maximum current, the card will be using its 535 * maximum current, e.g. if the card's maximum current is 300ma, and 536 * when we set current limit to 200ma, the card will draw 200ma, and 537 * when we set current limit to 400/600/800ma, the card will draw its 538 * maximum 300ma from the host. 539 * 540 * The above is incorrect: if we try to set a current limit that is 541 * not supported by the card, the card can rightfully error out the 542 * attempt, and remain at the default current limit. This results 543 * in a 300mA card being limited to 200mA even though the host 544 * supports 800mA. Failures seen with SanDisk 8GB UHS cards with 545 * an iMX6 host. --rmk 546 */ 547 if (max_current >= 800 && 548 card->sw_caps.sd3_curr_limit & SD_MAX_CURRENT_800) 549 current_limit = SD_SET_CURRENT_LIMIT_800; 550 else if (max_current >= 600 && 551 card->sw_caps.sd3_curr_limit & SD_MAX_CURRENT_600) 552 current_limit = SD_SET_CURRENT_LIMIT_600; 553 else if (max_current >= 400 && 554 card->sw_caps.sd3_curr_limit & SD_MAX_CURRENT_400) 555 current_limit = SD_SET_CURRENT_LIMIT_400; 556 else if (max_current >= 200 && 557 card->sw_caps.sd3_curr_limit & SD_MAX_CURRENT_200) 558 current_limit = SD_SET_CURRENT_LIMIT_200; 559 560 if (current_limit != SD_SET_CURRENT_NO_CHANGE) { 561 err = mmc_sd_switch(card, 1, 3, current_limit, status); 562 if (err) 563 return err; 564 565 if (((status[15] >> 4) & 0x0F) != current_limit) 566 pr_warn("%s: Problem setting current limit!\n", 567 mmc_hostname(card->host)); 568 569 } 570 571 return 0; 572 } 573 574 /* 575 * UHS-I specific initialization procedure 576 */ 577 static int mmc_sd_init_uhs_card(struct mmc_card *card) 578 { 579 int err; 580 u8 *status; 581 582 if (!card->scr.sda_spec3) 583 return 0; 584 585 if (!(card->csd.cmdclass & CCC_SWITCH)) 586 return 0; 587 588 status = kmalloc(64, GFP_KERNEL); 589 if (!status) { 590 pr_err("%s: could not allocate a buffer for " 591 "switch capabilities.\n", mmc_hostname(card->host)); 592 return -ENOMEM; 593 } 594 595 /* Set 4-bit bus width */ 596 if ((card->host->caps & MMC_CAP_4_BIT_DATA) && 597 (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) { 598 err = mmc_app_set_bus_width(card, MMC_BUS_WIDTH_4); 599 if (err) 600 goto out; 601 602 mmc_set_bus_width(card->host, MMC_BUS_WIDTH_4); 603 } 604 605 /* 606 * Select the bus speed mode depending on host 607 * and card capability. 608 */ 609 sd_update_bus_speed_mode(card); 610 611 /* Set the driver strength for the card */ 612 err = sd_select_driver_type(card, status); 613 if (err) 614 goto out; 615 616 /* Set current limit for the card */ 617 err = sd_set_current_limit(card, status); 618 if (err) 619 goto out; 620 621 /* Set bus speed mode of the card */ 622 err = sd_set_bus_speed_mode(card, status); 623 if (err) 624 goto out; 625 626 /* 627 * SPI mode doesn't define CMD19 and tuning is only valid for SDR50 and 628 * SDR104 mode SD-cards. Note that tuning is mandatory for SDR104. 629 */ 630 if (!mmc_host_is_spi(card->host) && 631 (card->host->ios.timing == MMC_TIMING_UHS_SDR50 || 632 card->host->ios.timing == MMC_TIMING_UHS_DDR50 || 633 card->host->ios.timing == MMC_TIMING_UHS_SDR104)) { 634 err = mmc_execute_tuning(card); 635 636 /* 637 * As SD Specifications Part1 Physical Layer Specification 638 * Version 3.01 says, CMD19 tuning is available for unlocked 639 * cards in transfer state of 1.8V signaling mode. The small 640 * difference between v3.00 and 3.01 spec means that CMD19 641 * tuning is also available for DDR50 mode. 642 */ 643 if (err && card->host->ios.timing == MMC_TIMING_UHS_DDR50) { 644 pr_warn("%s: ddr50 tuning failed\n", 645 mmc_hostname(card->host)); 646 err = 0; 647 } 648 } 649 650 out: 651 kfree(status); 652 653 return err; 654 } 655 656 MMC_DEV_ATTR(cid, "%08x%08x%08x%08x\n", card->raw_cid[0], card->raw_cid[1], 657 card->raw_cid[2], card->raw_cid[3]); 658 MMC_DEV_ATTR(csd, "%08x%08x%08x%08x\n", card->raw_csd[0], card->raw_csd[1], 659 card->raw_csd[2], card->raw_csd[3]); 660 MMC_DEV_ATTR(scr, "%08x%08x\n", card->raw_scr[0], card->raw_scr[1]); 661 MMC_DEV_ATTR(ssr, 662 "%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x\n", 663 card->raw_ssr[0], card->raw_ssr[1], card->raw_ssr[2], 664 card->raw_ssr[3], card->raw_ssr[4], card->raw_ssr[5], 665 card->raw_ssr[6], card->raw_ssr[7], card->raw_ssr[8], 666 card->raw_ssr[9], card->raw_ssr[10], card->raw_ssr[11], 667 card->raw_ssr[12], card->raw_ssr[13], card->raw_ssr[14], 668 card->raw_ssr[15]); 669 MMC_DEV_ATTR(date, "%02d/%04d\n", card->cid.month, card->cid.year); 670 MMC_DEV_ATTR(erase_size, "%u\n", card->erase_size << 9); 671 MMC_DEV_ATTR(preferred_erase_size, "%u\n", card->pref_erase << 9); 672 MMC_DEV_ATTR(fwrev, "0x%x\n", card->cid.fwrev); 673 MMC_DEV_ATTR(hwrev, "0x%x\n", card->cid.hwrev); 674 MMC_DEV_ATTR(manfid, "0x%06x\n", card->cid.manfid); 675 MMC_DEV_ATTR(name, "%s\n", card->cid.prod_name); 676 MMC_DEV_ATTR(oemid, "0x%04x\n", card->cid.oemid); 677 MMC_DEV_ATTR(serial, "0x%08x\n", card->cid.serial); 678 MMC_DEV_ATTR(ocr, "%08x\n", card->ocr); 679 680 681 static ssize_t mmc_dsr_show(struct device *dev, 682 struct device_attribute *attr, 683 char *buf) 684 { 685 struct mmc_card *card = mmc_dev_to_card(dev); 686 struct mmc_host *host = card->host; 687 688 if (card->csd.dsr_imp && host->dsr_req) 689 return sprintf(buf, "0x%x\n", host->dsr); 690 else 691 /* return default DSR value */ 692 return sprintf(buf, "0x%x\n", 0x404); 693 } 694 695 static DEVICE_ATTR(dsr, S_IRUGO, mmc_dsr_show, NULL); 696 697 static struct attribute *sd_std_attrs[] = { 698 &dev_attr_cid.attr, 699 &dev_attr_csd.attr, 700 &dev_attr_scr.attr, 701 &dev_attr_ssr.attr, 702 &dev_attr_date.attr, 703 &dev_attr_erase_size.attr, 704 &dev_attr_preferred_erase_size.attr, 705 &dev_attr_fwrev.attr, 706 &dev_attr_hwrev.attr, 707 &dev_attr_manfid.attr, 708 &dev_attr_name.attr, 709 &dev_attr_oemid.attr, 710 &dev_attr_serial.attr, 711 &dev_attr_ocr.attr, 712 &dev_attr_dsr.attr, 713 NULL, 714 }; 715 ATTRIBUTE_GROUPS(sd_std); 716 717 struct device_type sd_type = { 718 .groups = sd_std_groups, 719 }; 720 721 /* 722 * Fetch CID from card. 723 */ 724 int mmc_sd_get_cid(struct mmc_host *host, u32 ocr, u32 *cid, u32 *rocr) 725 { 726 int err; 727 u32 max_current; 728 int retries = 10; 729 u32 pocr = ocr; 730 731 try_again: 732 if (!retries) { 733 ocr &= ~SD_OCR_S18R; 734 pr_warn("%s: Skipping voltage switch\n", mmc_hostname(host)); 735 } 736 737 /* 738 * Since we're changing the OCR value, we seem to 739 * need to tell some cards to go back to the idle 740 * state. We wait 1ms to give cards time to 741 * respond. 742 */ 743 mmc_go_idle(host); 744 745 /* 746 * If SD_SEND_IF_COND indicates an SD 2.0 747 * compliant card and we should set bit 30 748 * of the ocr to indicate that we can handle 749 * block-addressed SDHC cards. 750 */ 751 err = mmc_send_if_cond(host, ocr); 752 if (!err) 753 ocr |= SD_OCR_CCS; 754 755 /* 756 * If the host supports one of UHS-I modes, request the card 757 * to switch to 1.8V signaling level. If the card has failed 758 * repeatedly to switch however, skip this. 759 */ 760 if (retries && mmc_host_uhs(host)) 761 ocr |= SD_OCR_S18R; 762 763 /* 764 * If the host can supply more than 150mA at current voltage, 765 * XPC should be set to 1. 766 */ 767 max_current = sd_get_host_max_current(host); 768 if (max_current > 150) 769 ocr |= SD_OCR_XPC; 770 771 err = mmc_send_app_op_cond(host, ocr, rocr); 772 if (err) 773 return err; 774 775 /* 776 * In case CCS and S18A in the response is set, start Signal Voltage 777 * Switch procedure. SPI mode doesn't support CMD11. 778 */ 779 if (!mmc_host_is_spi(host) && rocr && 780 ((*rocr & 0x41000000) == 0x41000000)) { 781 err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_180, 782 pocr); 783 if (err == -EAGAIN) { 784 retries--; 785 goto try_again; 786 } else if (err) { 787 retries = 0; 788 goto try_again; 789 } 790 } 791 792 if (mmc_host_is_spi(host)) 793 err = mmc_send_cid(host, cid); 794 else 795 err = mmc_all_send_cid(host, cid); 796 797 return err; 798 } 799 800 int mmc_sd_get_csd(struct mmc_host *host, struct mmc_card *card) 801 { 802 int err; 803 804 /* 805 * Fetch CSD from card. 806 */ 807 err = mmc_send_csd(card, card->raw_csd); 808 if (err) 809 return err; 810 811 err = mmc_decode_csd(card); 812 if (err) 813 return err; 814 815 return 0; 816 } 817 818 static int mmc_sd_get_ro(struct mmc_host *host) 819 { 820 int ro; 821 822 /* 823 * Some systems don't feature a write-protect pin and don't need one. 824 * E.g. because they only have micro-SD card slot. For those systems 825 * assume that the SD card is always read-write. 826 */ 827 if (host->caps2 & MMC_CAP2_NO_WRITE_PROTECT) 828 return 0; 829 830 if (!host->ops->get_ro) 831 return -1; 832 833 ro = host->ops->get_ro(host); 834 835 return ro; 836 } 837 838 int mmc_sd_setup_card(struct mmc_host *host, struct mmc_card *card, 839 bool reinit) 840 { 841 int err; 842 843 if (!reinit) { 844 /* 845 * Fetch SCR from card. 846 */ 847 err = mmc_app_send_scr(card, card->raw_scr); 848 if (err) 849 return err; 850 851 err = mmc_decode_scr(card); 852 if (err) 853 return err; 854 855 /* 856 * Fetch and process SD Status register. 857 */ 858 err = mmc_read_ssr(card); 859 if (err) 860 return err; 861 862 /* Erase init depends on CSD and SSR */ 863 mmc_init_erase(card); 864 865 /* 866 * Fetch switch information from card. 867 */ 868 err = mmc_read_switch(card); 869 if (err) 870 return err; 871 } 872 873 /* 874 * For SPI, enable CRC as appropriate. 875 * This CRC enable is located AFTER the reading of the 876 * card registers because some SDHC cards are not able 877 * to provide valid CRCs for non-512-byte blocks. 878 */ 879 if (mmc_host_is_spi(host)) { 880 err = mmc_spi_set_crc(host, use_spi_crc); 881 if (err) 882 return err; 883 } 884 885 /* 886 * Check if read-only switch is active. 887 */ 888 if (!reinit) { 889 int ro = mmc_sd_get_ro(host); 890 891 if (ro < 0) { 892 pr_warn("%s: host does not support reading read-only switch, assuming write-enable\n", 893 mmc_hostname(host)); 894 } else if (ro > 0) { 895 mmc_card_set_readonly(card); 896 } 897 } 898 899 return 0; 900 } 901 902 unsigned mmc_sd_get_max_clock(struct mmc_card *card) 903 { 904 unsigned max_dtr = (unsigned int)-1; 905 906 if (mmc_card_hs(card)) { 907 if (max_dtr > card->sw_caps.hs_max_dtr) 908 max_dtr = card->sw_caps.hs_max_dtr; 909 } else if (max_dtr > card->csd.max_dtr) { 910 max_dtr = card->csd.max_dtr; 911 } 912 913 return max_dtr; 914 } 915 916 /* 917 * Handle the detection and initialisation of a card. 918 * 919 * In the case of a resume, "oldcard" will contain the card 920 * we're trying to reinitialise. 921 */ 922 static int mmc_sd_init_card(struct mmc_host *host, u32 ocr, 923 struct mmc_card *oldcard) 924 { 925 struct mmc_card *card; 926 int err; 927 u32 cid[4]; 928 u32 rocr = 0; 929 930 BUG_ON(!host); 931 WARN_ON(!host->claimed); 932 933 err = mmc_sd_get_cid(host, ocr, cid, &rocr); 934 if (err) 935 return err; 936 937 if (oldcard) { 938 if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0) 939 return -ENOENT; 940 941 card = oldcard; 942 } else { 943 /* 944 * Allocate card structure. 945 */ 946 card = mmc_alloc_card(host, &sd_type); 947 if (IS_ERR(card)) 948 return PTR_ERR(card); 949 950 card->ocr = ocr; 951 card->type = MMC_TYPE_SD; 952 memcpy(card->raw_cid, cid, sizeof(card->raw_cid)); 953 } 954 955 /* 956 * Call the optional HC's init_card function to handle quirks. 957 */ 958 if (host->ops->init_card) 959 host->ops->init_card(host, card); 960 961 /* 962 * For native busses: get card RCA and quit open drain mode. 963 */ 964 if (!mmc_host_is_spi(host)) { 965 err = mmc_send_relative_addr(host, &card->rca); 966 if (err) 967 goto free_card; 968 } 969 970 if (!oldcard) { 971 err = mmc_sd_get_csd(host, card); 972 if (err) 973 goto free_card; 974 975 mmc_decode_cid(card); 976 } 977 978 /* 979 * handling only for cards supporting DSR and hosts requesting 980 * DSR configuration 981 */ 982 if (card->csd.dsr_imp && host->dsr_req) 983 mmc_set_dsr(host); 984 985 /* 986 * Select card, as all following commands rely on that. 987 */ 988 if (!mmc_host_is_spi(host)) { 989 err = mmc_select_card(card); 990 if (err) 991 goto free_card; 992 } 993 994 err = mmc_sd_setup_card(host, card, oldcard != NULL); 995 if (err) 996 goto free_card; 997 998 /* Initialization sequence for UHS-I cards */ 999 if (rocr & SD_ROCR_S18A) { 1000 err = mmc_sd_init_uhs_card(card); 1001 if (err) 1002 goto free_card; 1003 } else { 1004 /* 1005 * Attempt to change to high-speed (if supported) 1006 */ 1007 err = mmc_sd_switch_hs(card); 1008 if (err > 0) 1009 mmc_set_timing(card->host, MMC_TIMING_SD_HS); 1010 else if (err) 1011 goto free_card; 1012 1013 /* 1014 * Set bus speed. 1015 */ 1016 mmc_set_clock(host, mmc_sd_get_max_clock(card)); 1017 1018 /* 1019 * Switch to wider bus (if supported). 1020 */ 1021 if ((host->caps & MMC_CAP_4_BIT_DATA) && 1022 (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) { 1023 err = mmc_app_set_bus_width(card, MMC_BUS_WIDTH_4); 1024 if (err) 1025 goto free_card; 1026 1027 mmc_set_bus_width(host, MMC_BUS_WIDTH_4); 1028 } 1029 } 1030 1031 host->card = card; 1032 return 0; 1033 1034 free_card: 1035 if (!oldcard) 1036 mmc_remove_card(card); 1037 1038 return err; 1039 } 1040 1041 /* 1042 * Host is being removed. Free up the current card. 1043 */ 1044 static void mmc_sd_remove(struct mmc_host *host) 1045 { 1046 BUG_ON(!host); 1047 BUG_ON(!host->card); 1048 1049 mmc_remove_card(host->card); 1050 host->card = NULL; 1051 } 1052 1053 /* 1054 * Card detection - card is alive. 1055 */ 1056 static int mmc_sd_alive(struct mmc_host *host) 1057 { 1058 return mmc_send_status(host->card, NULL); 1059 } 1060 1061 /* 1062 * Card detection callback from host. 1063 */ 1064 static void mmc_sd_detect(struct mmc_host *host) 1065 { 1066 int err; 1067 1068 BUG_ON(!host); 1069 BUG_ON(!host->card); 1070 1071 mmc_get_card(host->card); 1072 1073 /* 1074 * Just check if our card has been removed. 1075 */ 1076 err = _mmc_detect_card_removed(host); 1077 1078 mmc_put_card(host->card); 1079 1080 if (err) { 1081 mmc_sd_remove(host); 1082 1083 mmc_claim_host(host); 1084 mmc_detach_bus(host); 1085 mmc_power_off(host); 1086 mmc_release_host(host); 1087 } 1088 } 1089 1090 static int _mmc_sd_suspend(struct mmc_host *host) 1091 { 1092 int err = 0; 1093 1094 BUG_ON(!host); 1095 BUG_ON(!host->card); 1096 1097 mmc_claim_host(host); 1098 1099 if (mmc_card_suspended(host->card)) 1100 goto out; 1101 1102 if (!mmc_host_is_spi(host)) 1103 err = mmc_deselect_cards(host); 1104 1105 if (!err) { 1106 mmc_power_off(host); 1107 mmc_card_set_suspended(host->card); 1108 } 1109 1110 out: 1111 mmc_release_host(host); 1112 return err; 1113 } 1114 1115 /* 1116 * Callback for suspend 1117 */ 1118 static int mmc_sd_suspend(struct mmc_host *host) 1119 { 1120 int err; 1121 1122 err = _mmc_sd_suspend(host); 1123 if (!err) { 1124 pm_runtime_disable(&host->card->dev); 1125 pm_runtime_set_suspended(&host->card->dev); 1126 } 1127 1128 return err; 1129 } 1130 1131 /* 1132 * This function tries to determine if the same card is still present 1133 * and, if so, restore all state to it. 1134 */ 1135 static int _mmc_sd_resume(struct mmc_host *host) 1136 { 1137 int err = 0; 1138 1139 BUG_ON(!host); 1140 BUG_ON(!host->card); 1141 1142 mmc_claim_host(host); 1143 1144 if (!mmc_card_suspended(host->card)) 1145 goto out; 1146 1147 mmc_power_up(host, host->card->ocr); 1148 err = mmc_sd_init_card(host, host->card->ocr, host->card); 1149 mmc_card_clr_suspended(host->card); 1150 1151 out: 1152 mmc_release_host(host); 1153 return err; 1154 } 1155 1156 /* 1157 * Callback for resume 1158 */ 1159 static int mmc_sd_resume(struct mmc_host *host) 1160 { 1161 pm_runtime_enable(&host->card->dev); 1162 return 0; 1163 } 1164 1165 /* 1166 * Callback for runtime_suspend. 1167 */ 1168 static int mmc_sd_runtime_suspend(struct mmc_host *host) 1169 { 1170 int err; 1171 1172 if (!(host->caps & MMC_CAP_AGGRESSIVE_PM)) 1173 return 0; 1174 1175 err = _mmc_sd_suspend(host); 1176 if (err) 1177 pr_err("%s: error %d doing aggressive suspend\n", 1178 mmc_hostname(host), err); 1179 1180 return err; 1181 } 1182 1183 /* 1184 * Callback for runtime_resume. 1185 */ 1186 static int mmc_sd_runtime_resume(struct mmc_host *host) 1187 { 1188 int err; 1189 1190 err = _mmc_sd_resume(host); 1191 if (err && err != -ENOMEDIUM) 1192 pr_err("%s: error %d doing runtime resume\n", 1193 mmc_hostname(host), err); 1194 1195 return 0; 1196 } 1197 1198 static int mmc_sd_reset(struct mmc_host *host) 1199 { 1200 mmc_power_cycle(host, host->card->ocr); 1201 return mmc_sd_init_card(host, host->card->ocr, host->card); 1202 } 1203 1204 static const struct mmc_bus_ops mmc_sd_ops = { 1205 .remove = mmc_sd_remove, 1206 .detect = mmc_sd_detect, 1207 .runtime_suspend = mmc_sd_runtime_suspend, 1208 .runtime_resume = mmc_sd_runtime_resume, 1209 .suspend = mmc_sd_suspend, 1210 .resume = mmc_sd_resume, 1211 .alive = mmc_sd_alive, 1212 .shutdown = mmc_sd_suspend, 1213 .reset = mmc_sd_reset, 1214 }; 1215 1216 /* 1217 * Starting point for SD card init. 1218 */ 1219 int mmc_attach_sd(struct mmc_host *host) 1220 { 1221 int err; 1222 u32 ocr, rocr; 1223 1224 BUG_ON(!host); 1225 WARN_ON(!host->claimed); 1226 1227 err = mmc_send_app_op_cond(host, 0, &ocr); 1228 if (err) 1229 return err; 1230 1231 mmc_attach_bus(host, &mmc_sd_ops); 1232 if (host->ocr_avail_sd) 1233 host->ocr_avail = host->ocr_avail_sd; 1234 1235 /* 1236 * We need to get OCR a different way for SPI. 1237 */ 1238 if (mmc_host_is_spi(host)) { 1239 mmc_go_idle(host); 1240 1241 err = mmc_spi_read_ocr(host, 0, &ocr); 1242 if (err) 1243 goto err; 1244 } 1245 1246 rocr = mmc_select_voltage(host, ocr); 1247 1248 /* 1249 * Can we support the voltage(s) of the card(s)? 1250 */ 1251 if (!rocr) { 1252 err = -EINVAL; 1253 goto err; 1254 } 1255 1256 /* 1257 * Detect and init the card. 1258 */ 1259 err = mmc_sd_init_card(host, rocr, NULL); 1260 if (err) 1261 goto err; 1262 1263 mmc_release_host(host); 1264 err = mmc_add_card(host->card); 1265 if (err) 1266 goto remove_card; 1267 1268 mmc_claim_host(host); 1269 return 0; 1270 1271 remove_card: 1272 mmc_remove_card(host->card); 1273 host->card = NULL; 1274 mmc_claim_host(host); 1275 err: 1276 mmc_detach_bus(host); 1277 1278 pr_err("%s: error %d whilst initialising SD card\n", 1279 mmc_hostname(host), err); 1280 1281 return err; 1282 } 1283