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/slab.h> 15 16 #include <linux/mmc/host.h> 17 #include <linux/mmc/card.h> 18 #include <linux/mmc/mmc.h> 19 #include <linux/mmc/sd.h> 20 21 #include "core.h" 22 #include "bus.h" 23 #include "mmc_ops.h" 24 #include "sd_ops.h" 25 26 static const unsigned int tran_exp[] = { 27 10000, 100000, 1000000, 10000000, 28 0, 0, 0, 0 29 }; 30 31 static const unsigned char tran_mant[] = { 32 0, 10, 12, 13, 15, 20, 25, 30, 33 35, 40, 45, 50, 55, 60, 70, 80, 34 }; 35 36 static const unsigned int tacc_exp[] = { 37 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 38 }; 39 40 static const unsigned int tacc_mant[] = { 41 0, 10, 12, 13, 15, 20, 25, 30, 42 35, 40, 45, 50, 55, 60, 70, 80, 43 }; 44 45 #define UNSTUFF_BITS(resp,start,size) \ 46 ({ \ 47 const int __size = size; \ 48 const u32 __mask = (__size < 32 ? 1 << __size : 0) - 1; \ 49 const int __off = 3 - ((start) / 32); \ 50 const int __shft = (start) & 31; \ 51 u32 __res; \ 52 \ 53 __res = resp[__off] >> __shft; \ 54 if (__size + __shft > 32) \ 55 __res |= resp[__off-1] << ((32 - __shft) % 32); \ 56 __res & __mask; \ 57 }) 58 59 /* 60 * Given the decoded CSD structure, decode the raw CID to our CID structure. 61 */ 62 void mmc_decode_cid(struct mmc_card *card) 63 { 64 u32 *resp = card->raw_cid; 65 66 memset(&card->cid, 0, sizeof(struct mmc_cid)); 67 68 /* 69 * SD doesn't currently have a version field so we will 70 * have to assume we can parse this. 71 */ 72 card->cid.manfid = UNSTUFF_BITS(resp, 120, 8); 73 card->cid.oemid = UNSTUFF_BITS(resp, 104, 16); 74 card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8); 75 card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8); 76 card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8); 77 card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8); 78 card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8); 79 card->cid.hwrev = UNSTUFF_BITS(resp, 60, 4); 80 card->cid.fwrev = UNSTUFF_BITS(resp, 56, 4); 81 card->cid.serial = UNSTUFF_BITS(resp, 24, 32); 82 card->cid.year = UNSTUFF_BITS(resp, 12, 8); 83 card->cid.month = UNSTUFF_BITS(resp, 8, 4); 84 85 card->cid.year += 2000; /* SD cards year offset */ 86 } 87 88 /* 89 * Given a 128-bit response, decode to our card CSD structure. 90 */ 91 static int mmc_decode_csd(struct mmc_card *card) 92 { 93 struct mmc_csd *csd = &card->csd; 94 unsigned int e, m, csd_struct; 95 u32 *resp = card->raw_csd; 96 97 csd_struct = UNSTUFF_BITS(resp, 126, 2); 98 99 switch (csd_struct) { 100 case 0: 101 m = UNSTUFF_BITS(resp, 115, 4); 102 e = UNSTUFF_BITS(resp, 112, 3); 103 csd->tacc_ns = (tacc_exp[e] * tacc_mant[m] + 9) / 10; 104 csd->tacc_clks = UNSTUFF_BITS(resp, 104, 8) * 100; 105 106 m = UNSTUFF_BITS(resp, 99, 4); 107 e = UNSTUFF_BITS(resp, 96, 3); 108 csd->max_dtr = tran_exp[e] * tran_mant[m]; 109 csd->cmdclass = UNSTUFF_BITS(resp, 84, 12); 110 111 e = UNSTUFF_BITS(resp, 47, 3); 112 m = UNSTUFF_BITS(resp, 62, 12); 113 csd->capacity = (1 + m) << (e + 2); 114 115 csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4); 116 csd->read_partial = UNSTUFF_BITS(resp, 79, 1); 117 csd->write_misalign = UNSTUFF_BITS(resp, 78, 1); 118 csd->read_misalign = UNSTUFF_BITS(resp, 77, 1); 119 csd->r2w_factor = UNSTUFF_BITS(resp, 26, 3); 120 csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4); 121 csd->write_partial = UNSTUFF_BITS(resp, 21, 1); 122 break; 123 case 1: 124 /* 125 * This is a block-addressed SDHC card. Most 126 * interesting fields are unused and have fixed 127 * values. To avoid getting tripped by buggy cards, 128 * we assume those fixed values ourselves. 129 */ 130 mmc_card_set_blockaddr(card); 131 132 csd->tacc_ns = 0; /* Unused */ 133 csd->tacc_clks = 0; /* Unused */ 134 135 m = UNSTUFF_BITS(resp, 99, 4); 136 e = UNSTUFF_BITS(resp, 96, 3); 137 csd->max_dtr = tran_exp[e] * tran_mant[m]; 138 csd->cmdclass = UNSTUFF_BITS(resp, 84, 12); 139 140 m = UNSTUFF_BITS(resp, 48, 22); 141 csd->capacity = (1 + m) << 10; 142 143 csd->read_blkbits = 9; 144 csd->read_partial = 0; 145 csd->write_misalign = 0; 146 csd->read_misalign = 0; 147 csd->r2w_factor = 4; /* Unused */ 148 csd->write_blkbits = 9; 149 csd->write_partial = 0; 150 break; 151 default: 152 printk(KERN_ERR "%s: unrecognised CSD structure version %d\n", 153 mmc_hostname(card->host), csd_struct); 154 return -EINVAL; 155 } 156 157 return 0; 158 } 159 160 /* 161 * Given a 64-bit response, decode to our card SCR structure. 162 */ 163 static int mmc_decode_scr(struct mmc_card *card) 164 { 165 struct sd_scr *scr = &card->scr; 166 unsigned int scr_struct; 167 u32 resp[4]; 168 169 resp[3] = card->raw_scr[1]; 170 resp[2] = card->raw_scr[0]; 171 172 scr_struct = UNSTUFF_BITS(resp, 60, 4); 173 if (scr_struct != 0) { 174 printk(KERN_ERR "%s: unrecognised SCR structure version %d\n", 175 mmc_hostname(card->host), scr_struct); 176 return -EINVAL; 177 } 178 179 scr->sda_vsn = UNSTUFF_BITS(resp, 56, 4); 180 scr->bus_widths = UNSTUFF_BITS(resp, 48, 4); 181 182 return 0; 183 } 184 185 /* 186 * Fetches and decodes switch information 187 */ 188 static int mmc_read_switch(struct mmc_card *card) 189 { 190 int err; 191 u8 *status; 192 193 if (card->scr.sda_vsn < SCR_SPEC_VER_1) 194 return 0; 195 196 if (!(card->csd.cmdclass & CCC_SWITCH)) { 197 printk(KERN_WARNING "%s: card lacks mandatory switch " 198 "function, performance might suffer.\n", 199 mmc_hostname(card->host)); 200 return 0; 201 } 202 203 err = -EIO; 204 205 status = kmalloc(64, GFP_KERNEL); 206 if (!status) { 207 printk(KERN_ERR "%s: could not allocate a buffer for " 208 "switch capabilities.\n", mmc_hostname(card->host)); 209 return -ENOMEM; 210 } 211 212 err = mmc_sd_switch(card, 0, 0, 1, status); 213 if (err) { 214 /* If the host or the card can't do the switch, 215 * fail more gracefully. */ 216 if ((err != -EINVAL) 217 && (err != -ENOSYS) 218 && (err != -EFAULT)) 219 goto out; 220 221 printk(KERN_WARNING "%s: problem reading switch " 222 "capabilities, performance might suffer.\n", 223 mmc_hostname(card->host)); 224 err = 0; 225 226 goto out; 227 } 228 229 if (status[13] & 0x02) 230 card->sw_caps.hs_max_dtr = 50000000; 231 232 out: 233 kfree(status); 234 235 return err; 236 } 237 238 /* 239 * Test if the card supports high-speed mode and, if so, switch to it. 240 */ 241 int mmc_sd_switch_hs(struct mmc_card *card) 242 { 243 int err; 244 u8 *status; 245 246 if (card->scr.sda_vsn < SCR_SPEC_VER_1) 247 return 0; 248 249 if (!(card->csd.cmdclass & CCC_SWITCH)) 250 return 0; 251 252 if (!(card->host->caps & MMC_CAP_SD_HIGHSPEED)) 253 return 0; 254 255 if (card->sw_caps.hs_max_dtr == 0) 256 return 0; 257 258 err = -EIO; 259 260 status = kmalloc(64, GFP_KERNEL); 261 if (!status) { 262 printk(KERN_ERR "%s: could not allocate a buffer for " 263 "switch capabilities.\n", mmc_hostname(card->host)); 264 return -ENOMEM; 265 } 266 267 err = mmc_sd_switch(card, 1, 0, 1, status); 268 if (err) 269 goto out; 270 271 if ((status[16] & 0xF) != 1) { 272 printk(KERN_WARNING "%s: Problem switching card " 273 "into high-speed mode!\n", 274 mmc_hostname(card->host)); 275 err = 0; 276 } else { 277 err = 1; 278 } 279 280 out: 281 kfree(status); 282 283 return err; 284 } 285 286 MMC_DEV_ATTR(cid, "%08x%08x%08x%08x\n", card->raw_cid[0], card->raw_cid[1], 287 card->raw_cid[2], card->raw_cid[3]); 288 MMC_DEV_ATTR(csd, "%08x%08x%08x%08x\n", card->raw_csd[0], card->raw_csd[1], 289 card->raw_csd[2], card->raw_csd[3]); 290 MMC_DEV_ATTR(scr, "%08x%08x\n", card->raw_scr[0], card->raw_scr[1]); 291 MMC_DEV_ATTR(date, "%02d/%04d\n", card->cid.month, card->cid.year); 292 MMC_DEV_ATTR(fwrev, "0x%x\n", card->cid.fwrev); 293 MMC_DEV_ATTR(hwrev, "0x%x\n", card->cid.hwrev); 294 MMC_DEV_ATTR(manfid, "0x%06x\n", card->cid.manfid); 295 MMC_DEV_ATTR(name, "%s\n", card->cid.prod_name); 296 MMC_DEV_ATTR(oemid, "0x%04x\n", card->cid.oemid); 297 MMC_DEV_ATTR(serial, "0x%08x\n", card->cid.serial); 298 299 300 static struct attribute *sd_std_attrs[] = { 301 &dev_attr_cid.attr, 302 &dev_attr_csd.attr, 303 &dev_attr_scr.attr, 304 &dev_attr_date.attr, 305 &dev_attr_fwrev.attr, 306 &dev_attr_hwrev.attr, 307 &dev_attr_manfid.attr, 308 &dev_attr_name.attr, 309 &dev_attr_oemid.attr, 310 &dev_attr_serial.attr, 311 NULL, 312 }; 313 314 static struct attribute_group sd_std_attr_group = { 315 .attrs = sd_std_attrs, 316 }; 317 318 static const struct attribute_group *sd_attr_groups[] = { 319 &sd_std_attr_group, 320 NULL, 321 }; 322 323 struct device_type sd_type = { 324 .groups = sd_attr_groups, 325 }; 326 327 /* 328 * Fetch CID from card. 329 */ 330 int mmc_sd_get_cid(struct mmc_host *host, u32 ocr, u32 *cid) 331 { 332 int err; 333 334 /* 335 * Since we're changing the OCR value, we seem to 336 * need to tell some cards to go back to the idle 337 * state. We wait 1ms to give cards time to 338 * respond. 339 */ 340 mmc_go_idle(host); 341 342 /* 343 * If SD_SEND_IF_COND indicates an SD 2.0 344 * compliant card and we should set bit 30 345 * of the ocr to indicate that we can handle 346 * block-addressed SDHC cards. 347 */ 348 err = mmc_send_if_cond(host, ocr); 349 if (!err) 350 ocr |= 1 << 30; 351 352 err = mmc_send_app_op_cond(host, ocr, NULL); 353 if (err) 354 return err; 355 356 if (mmc_host_is_spi(host)) 357 err = mmc_send_cid(host, cid); 358 else 359 err = mmc_all_send_cid(host, cid); 360 361 return err; 362 } 363 364 int mmc_sd_get_csd(struct mmc_host *host, struct mmc_card *card) 365 { 366 int err; 367 368 /* 369 * Fetch CSD from card. 370 */ 371 err = mmc_send_csd(card, card->raw_csd); 372 if (err) 373 return err; 374 375 err = mmc_decode_csd(card); 376 if (err) 377 return err; 378 379 return 0; 380 } 381 382 int mmc_sd_setup_card(struct mmc_host *host, struct mmc_card *card, 383 bool reinit) 384 { 385 int err; 386 387 if (!reinit) { 388 /* 389 * Fetch SCR from card. 390 */ 391 err = mmc_app_send_scr(card, card->raw_scr); 392 if (err) 393 return err; 394 395 err = mmc_decode_scr(card); 396 if (err) 397 return err; 398 399 /* 400 * Fetch switch information from card. 401 */ 402 err = mmc_read_switch(card); 403 if (err) 404 return err; 405 } 406 407 /* 408 * For SPI, enable CRC as appropriate. 409 * This CRC enable is located AFTER the reading of the 410 * card registers because some SDHC cards are not able 411 * to provide valid CRCs for non-512-byte blocks. 412 */ 413 if (mmc_host_is_spi(host)) { 414 err = mmc_spi_set_crc(host, use_spi_crc); 415 if (err) 416 return err; 417 } 418 419 /* 420 * Check if read-only switch is active. 421 */ 422 if (!reinit) { 423 int ro = -1; 424 425 if (host->ops->get_ro) 426 ro = host->ops->get_ro(host); 427 428 if (ro < 0) { 429 printk(KERN_WARNING "%s: host does not " 430 "support reading read-only " 431 "switch. assuming write-enable.\n", 432 mmc_hostname(host)); 433 } else if (ro > 0) { 434 mmc_card_set_readonly(card); 435 } 436 } 437 438 return 0; 439 } 440 441 unsigned mmc_sd_get_max_clock(struct mmc_card *card) 442 { 443 unsigned max_dtr = (unsigned int)-1; 444 445 if (mmc_card_highspeed(card)) { 446 if (max_dtr > card->sw_caps.hs_max_dtr) 447 max_dtr = card->sw_caps.hs_max_dtr; 448 } else if (max_dtr > card->csd.max_dtr) { 449 max_dtr = card->csd.max_dtr; 450 } 451 452 return max_dtr; 453 } 454 455 void mmc_sd_go_highspeed(struct mmc_card *card) 456 { 457 mmc_card_set_highspeed(card); 458 mmc_set_timing(card->host, MMC_TIMING_SD_HS); 459 } 460 461 /* 462 * Handle the detection and initialisation of a card. 463 * 464 * In the case of a resume, "oldcard" will contain the card 465 * we're trying to reinitialise. 466 */ 467 static int mmc_sd_init_card(struct mmc_host *host, u32 ocr, 468 struct mmc_card *oldcard) 469 { 470 struct mmc_card *card; 471 int err; 472 u32 cid[4]; 473 474 BUG_ON(!host); 475 WARN_ON(!host->claimed); 476 477 err = mmc_sd_get_cid(host, ocr, cid); 478 if (err) 479 return err; 480 481 if (oldcard) { 482 if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0) 483 return -ENOENT; 484 485 card = oldcard; 486 } else { 487 /* 488 * Allocate card structure. 489 */ 490 card = mmc_alloc_card(host, &sd_type); 491 if (IS_ERR(card)) 492 return PTR_ERR(card); 493 494 card->type = MMC_TYPE_SD; 495 memcpy(card->raw_cid, cid, sizeof(card->raw_cid)); 496 } 497 498 /* 499 * For native busses: get card RCA and quit open drain mode. 500 */ 501 if (!mmc_host_is_spi(host)) { 502 err = mmc_send_relative_addr(host, &card->rca); 503 if (err) 504 return err; 505 506 mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL); 507 } 508 509 if (!oldcard) { 510 err = mmc_sd_get_csd(host, card); 511 if (err) 512 return err; 513 514 mmc_decode_cid(card); 515 } 516 517 /* 518 * Select card, as all following commands rely on that. 519 */ 520 if (!mmc_host_is_spi(host)) { 521 err = mmc_select_card(card); 522 if (err) 523 return err; 524 } 525 526 err = mmc_sd_setup_card(host, card, oldcard != NULL); 527 if (err) 528 goto free_card; 529 530 /* 531 * Attempt to change to high-speed (if supported) 532 */ 533 err = mmc_sd_switch_hs(card); 534 if (err > 0) 535 mmc_sd_go_highspeed(card); 536 else if (err) 537 goto free_card; 538 539 /* 540 * Set bus speed. 541 */ 542 mmc_set_clock(host, mmc_sd_get_max_clock(card)); 543 544 /* 545 * Switch to wider bus (if supported). 546 */ 547 if ((host->caps & MMC_CAP_4_BIT_DATA) && 548 (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) { 549 err = mmc_app_set_bus_width(card, MMC_BUS_WIDTH_4); 550 if (err) 551 goto free_card; 552 553 mmc_set_bus_width(host, MMC_BUS_WIDTH_4); 554 } 555 556 host->card = card; 557 return 0; 558 559 free_card: 560 if (!oldcard) 561 mmc_remove_card(card); 562 563 return err; 564 } 565 566 /* 567 * Host is being removed. Free up the current card. 568 */ 569 static void mmc_sd_remove(struct mmc_host *host) 570 { 571 BUG_ON(!host); 572 BUG_ON(!host->card); 573 574 mmc_remove_card(host->card); 575 host->card = NULL; 576 } 577 578 /* 579 * Card detection callback from host. 580 */ 581 static void mmc_sd_detect(struct mmc_host *host) 582 { 583 int err; 584 585 BUG_ON(!host); 586 BUG_ON(!host->card); 587 588 mmc_claim_host(host); 589 590 /* 591 * Just check if our card has been removed. 592 */ 593 err = mmc_send_status(host->card, NULL); 594 595 mmc_release_host(host); 596 597 if (err) { 598 mmc_sd_remove(host); 599 600 mmc_claim_host(host); 601 mmc_detach_bus(host); 602 mmc_release_host(host); 603 } 604 } 605 606 /* 607 * Suspend callback from host. 608 */ 609 static int mmc_sd_suspend(struct mmc_host *host) 610 { 611 BUG_ON(!host); 612 BUG_ON(!host->card); 613 614 mmc_claim_host(host); 615 if (!mmc_host_is_spi(host)) 616 mmc_deselect_cards(host); 617 host->card->state &= ~MMC_STATE_HIGHSPEED; 618 mmc_release_host(host); 619 620 return 0; 621 } 622 623 /* 624 * Resume callback from host. 625 * 626 * This function tries to determine if the same card is still present 627 * and, if so, restore all state to it. 628 */ 629 static int mmc_sd_resume(struct mmc_host *host) 630 { 631 int err; 632 633 BUG_ON(!host); 634 BUG_ON(!host->card); 635 636 mmc_claim_host(host); 637 err = mmc_sd_init_card(host, host->ocr, host->card); 638 mmc_release_host(host); 639 640 return err; 641 } 642 643 static void mmc_sd_power_restore(struct mmc_host *host) 644 { 645 host->card->state &= ~MMC_STATE_HIGHSPEED; 646 mmc_claim_host(host); 647 mmc_sd_init_card(host, host->ocr, host->card); 648 mmc_release_host(host); 649 } 650 651 static const struct mmc_bus_ops mmc_sd_ops = { 652 .remove = mmc_sd_remove, 653 .detect = mmc_sd_detect, 654 .suspend = NULL, 655 .resume = NULL, 656 .power_restore = mmc_sd_power_restore, 657 }; 658 659 static const struct mmc_bus_ops mmc_sd_ops_unsafe = { 660 .remove = mmc_sd_remove, 661 .detect = mmc_sd_detect, 662 .suspend = mmc_sd_suspend, 663 .resume = mmc_sd_resume, 664 .power_restore = mmc_sd_power_restore, 665 }; 666 667 static void mmc_sd_attach_bus_ops(struct mmc_host *host) 668 { 669 const struct mmc_bus_ops *bus_ops; 670 671 if (host->caps & MMC_CAP_NONREMOVABLE || !mmc_assume_removable) 672 bus_ops = &mmc_sd_ops_unsafe; 673 else 674 bus_ops = &mmc_sd_ops; 675 mmc_attach_bus(host, bus_ops); 676 } 677 678 /* 679 * Starting point for SD card init. 680 */ 681 int mmc_attach_sd(struct mmc_host *host, u32 ocr) 682 { 683 int err; 684 685 BUG_ON(!host); 686 WARN_ON(!host->claimed); 687 688 mmc_sd_attach_bus_ops(host); 689 690 /* 691 * We need to get OCR a different way for SPI. 692 */ 693 if (mmc_host_is_spi(host)) { 694 mmc_go_idle(host); 695 696 err = mmc_spi_read_ocr(host, 0, &ocr); 697 if (err) 698 goto err; 699 } 700 701 /* 702 * Sanity check the voltages that the card claims to 703 * support. 704 */ 705 if (ocr & 0x7F) { 706 printk(KERN_WARNING "%s: card claims to support voltages " 707 "below the defined range. These will be ignored.\n", 708 mmc_hostname(host)); 709 ocr &= ~0x7F; 710 } 711 712 if (ocr & MMC_VDD_165_195) { 713 printk(KERN_WARNING "%s: SD card claims to support the " 714 "incompletely defined 'low voltage range'. This " 715 "will be ignored.\n", mmc_hostname(host)); 716 ocr &= ~MMC_VDD_165_195; 717 } 718 719 host->ocr = mmc_select_voltage(host, ocr); 720 721 /* 722 * Can we support the voltage(s) of the card(s)? 723 */ 724 if (!host->ocr) { 725 err = -EINVAL; 726 goto err; 727 } 728 729 /* 730 * Detect and init the card. 731 */ 732 err = mmc_sd_init_card(host, host->ocr, NULL); 733 if (err) 734 goto err; 735 736 mmc_release_host(host); 737 738 err = mmc_add_card(host->card); 739 if (err) 740 goto remove_card; 741 742 return 0; 743 744 remove_card: 745 mmc_remove_card(host->card); 746 host->card = NULL; 747 mmc_claim_host(host); 748 err: 749 mmc_detach_bus(host); 750 mmc_release_host(host); 751 752 printk(KERN_ERR "%s: error %d whilst initialising SD card\n", 753 mmc_hostname(host), err); 754 755 return err; 756 } 757 758