1 /* 2 * linux/drivers/mmc/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 15 #include <linux/mmc/host.h> 16 #include <linux/mmc/card.h> 17 #include <linux/mmc/mmc.h> 18 #include <linux/mmc/sd.h> 19 20 #include "core.h" 21 #include "sysfs.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 static 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("%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 BUG_ON(!mmc_card_sd(card)); 170 171 resp[3] = card->raw_scr[1]; 172 resp[2] = card->raw_scr[0]; 173 174 scr_struct = UNSTUFF_BITS(resp, 60, 4); 175 if (scr_struct != 0) { 176 printk("%s: unrecognised SCR structure version %d\n", 177 mmc_hostname(card->host), scr_struct); 178 return -EINVAL; 179 } 180 181 scr->sda_vsn = UNSTUFF_BITS(resp, 56, 4); 182 scr->bus_widths = UNSTUFF_BITS(resp, 48, 4); 183 184 return 0; 185 } 186 187 /* 188 * Fetches and decodes switch information 189 */ 190 static int mmc_read_switch(struct mmc_card *card) 191 { 192 int err; 193 u8 *status; 194 195 if (card->scr.sda_vsn < SCR_SPEC_VER_1) 196 return MMC_ERR_NONE; 197 198 if (!(card->csd.cmdclass & CCC_SWITCH)) { 199 printk(KERN_WARNING "%s: card lacks mandatory switch " 200 "function, performance might suffer.\n", 201 mmc_hostname(card->host)); 202 return MMC_ERR_NONE; 203 } 204 205 err = MMC_ERR_FAILED; 206 207 status = kmalloc(64, GFP_KERNEL); 208 if (!status) { 209 printk("%s: could not allocate a buffer for switch " 210 "capabilities.\n", 211 mmc_hostname(card->host)); 212 return err; 213 } 214 215 err = mmc_sd_switch(card, 0, 0, 1, status); 216 if (err != MMC_ERR_NONE) { 217 printk(KERN_WARNING "%s: problem reading switch " 218 "capabilities, performance might suffer.\n", 219 mmc_hostname(card->host)); 220 err = MMC_ERR_NONE; 221 goto out; 222 } 223 224 if (status[13] & 0x02) 225 card->sw_caps.hs_max_dtr = 50000000; 226 227 out: 228 kfree(status); 229 230 return err; 231 } 232 233 /* 234 * Test if the card supports high-speed mode and, if so, switch to it. 235 */ 236 static int mmc_switch_hs(struct mmc_card *card) 237 { 238 int err; 239 u8 *status; 240 241 if (card->scr.sda_vsn < SCR_SPEC_VER_1) 242 return MMC_ERR_NONE; 243 244 if (!(card->csd.cmdclass & CCC_SWITCH)) 245 return MMC_ERR_NONE; 246 247 if (!(card->host->caps & MMC_CAP_SD_HIGHSPEED)) 248 return MMC_ERR_NONE; 249 250 if (card->sw_caps.hs_max_dtr == 0) 251 return MMC_ERR_NONE; 252 253 err = MMC_ERR_FAILED; 254 255 status = kmalloc(64, GFP_KERNEL); 256 if (!status) { 257 printk("%s: could not allocate a buffer for switch " 258 "capabilities.\n", 259 mmc_hostname(card->host)); 260 return err; 261 } 262 263 err = mmc_sd_switch(card, 1, 0, 1, status); 264 if (err != MMC_ERR_NONE) 265 goto out; 266 267 if ((status[16] & 0xF) != 1) { 268 printk(KERN_WARNING "%s: Problem switching card " 269 "into high-speed mode!\n", 270 mmc_hostname(card->host)); 271 } else { 272 mmc_card_set_highspeed(card); 273 mmc_set_timing(card->host, MMC_TIMING_SD_HS); 274 } 275 276 out: 277 kfree(status); 278 279 return err; 280 } 281 282 /* 283 * Handle the detection and initialisation of a card. 284 * 285 * In the case of a resume, "curcard" will contain the card 286 * we're trying to reinitialise. 287 */ 288 static int mmc_sd_init_card(struct mmc_host *host, u32 ocr, 289 struct mmc_card *oldcard) 290 { 291 struct mmc_card *card; 292 int err; 293 u32 cid[4]; 294 unsigned int max_dtr; 295 296 BUG_ON(!host); 297 BUG_ON(!host->claimed); 298 299 /* 300 * Since we're changing the OCR value, we seem to 301 * need to tell some cards to go back to the idle 302 * state. We wait 1ms to give cards time to 303 * respond. 304 */ 305 mmc_go_idle(host); 306 307 /* 308 * If SD_SEND_IF_COND indicates an SD 2.0 309 * compliant card and we should set bit 30 310 * of the ocr to indicate that we can handle 311 * block-addressed SDHC cards. 312 */ 313 err = mmc_send_if_cond(host, ocr); 314 if (err == MMC_ERR_NONE) 315 ocr |= 1 << 30; 316 317 err = mmc_send_app_op_cond(host, ocr, NULL); 318 if (err != MMC_ERR_NONE) 319 goto err; 320 321 /* 322 * Fetch CID from card. 323 */ 324 err = mmc_all_send_cid(host, cid); 325 if (err != MMC_ERR_NONE) 326 goto err; 327 328 if (oldcard) { 329 if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0) 330 goto err; 331 332 card = oldcard; 333 } else { 334 /* 335 * Allocate card structure. 336 */ 337 card = mmc_alloc_card(host); 338 if (IS_ERR(card)) 339 goto err; 340 341 card->type = MMC_TYPE_SD; 342 memcpy(card->raw_cid, cid, sizeof(card->raw_cid)); 343 } 344 345 /* 346 * Set card RCA. 347 */ 348 err = mmc_send_relative_addr(host, &card->rca); 349 if (err != MMC_ERR_NONE) 350 goto free_card; 351 352 mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL); 353 354 if (!oldcard) { 355 /* 356 * Fetch CSD from card. 357 */ 358 err = mmc_send_csd(card, card->raw_csd); 359 if (err != MMC_ERR_NONE) 360 goto free_card; 361 362 err = mmc_decode_csd(card); 363 if (err < 0) 364 goto free_card; 365 366 mmc_decode_cid(card); 367 } 368 369 /* 370 * Select card, as all following commands rely on that. 371 */ 372 err = mmc_select_card(card); 373 if (err != MMC_ERR_NONE) 374 goto free_card; 375 376 if (!oldcard) { 377 /* 378 * Fetch SCR from card. 379 */ 380 err = mmc_app_send_scr(card, card->raw_scr); 381 if (err != MMC_ERR_NONE) 382 goto free_card; 383 384 err = mmc_decode_scr(card); 385 if (err < 0) 386 goto free_card; 387 388 /* 389 * Fetch switch information from card. 390 */ 391 err = mmc_read_switch(card); 392 if (err != MMC_ERR_NONE) 393 goto free_card; 394 } 395 396 /* 397 * Attempt to change to high-speed (if supported) 398 */ 399 err = mmc_switch_hs(card); 400 if (err != MMC_ERR_NONE) 401 goto free_card; 402 403 /* 404 * Compute bus speed. 405 */ 406 max_dtr = (unsigned int)-1; 407 408 if (mmc_card_highspeed(card)) { 409 if (max_dtr > card->sw_caps.hs_max_dtr) 410 max_dtr = card->sw_caps.hs_max_dtr; 411 } else if (max_dtr > card->csd.max_dtr) { 412 max_dtr = card->csd.max_dtr; 413 } 414 415 mmc_set_clock(host, max_dtr); 416 417 /* 418 * Switch to wider bus (if supported). 419 */ 420 if ((host->caps & MMC_CAP_4_BIT_DATA) && 421 (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) { 422 err = mmc_app_set_bus_width(card, MMC_BUS_WIDTH_4); 423 if (err != MMC_ERR_NONE) 424 goto free_card; 425 426 mmc_set_bus_width(host, MMC_BUS_WIDTH_4); 427 } 428 429 /* 430 * Check if read-only switch is active. 431 */ 432 if (!oldcard) { 433 if (!host->ops->get_ro) { 434 printk(KERN_WARNING "%s: host does not " 435 "support reading read-only " 436 "switch. assuming write-enable.\n", 437 mmc_hostname(host)); 438 } else { 439 if (host->ops->get_ro(host)) 440 mmc_card_set_readonly(card); 441 } 442 } 443 444 if (!oldcard) 445 host->card = card; 446 447 return MMC_ERR_NONE; 448 449 free_card: 450 if (!oldcard) 451 mmc_remove_card(card); 452 err: 453 454 return MMC_ERR_FAILED; 455 } 456 457 /* 458 * Host is being removed. Free up the current card. 459 */ 460 static void mmc_sd_remove(struct mmc_host *host) 461 { 462 BUG_ON(!host); 463 BUG_ON(!host->card); 464 465 mmc_remove_card(host->card); 466 host->card = NULL; 467 } 468 469 /* 470 * Card detection callback from host. 471 */ 472 static void mmc_sd_detect(struct mmc_host *host) 473 { 474 int err; 475 476 BUG_ON(!host); 477 BUG_ON(!host->card); 478 479 mmc_claim_host(host); 480 481 /* 482 * Just check if our card has been removed. 483 */ 484 err = mmc_send_status(host->card, NULL); 485 486 mmc_release_host(host); 487 488 if (err != MMC_ERR_NONE) { 489 mmc_sd_remove(host); 490 491 mmc_claim_host(host); 492 mmc_detach_bus(host); 493 mmc_release_host(host); 494 } 495 } 496 497 MMC_ATTR_FN(cid, "%08x%08x%08x%08x\n", card->raw_cid[0], card->raw_cid[1], 498 card->raw_cid[2], card->raw_cid[3]); 499 MMC_ATTR_FN(csd, "%08x%08x%08x%08x\n", card->raw_csd[0], card->raw_csd[1], 500 card->raw_csd[2], card->raw_csd[3]); 501 MMC_ATTR_FN(scr, "%08x%08x\n", card->raw_scr[0], card->raw_scr[1]); 502 MMC_ATTR_FN(date, "%02d/%04d\n", card->cid.month, card->cid.year); 503 MMC_ATTR_FN(fwrev, "0x%x\n", card->cid.fwrev); 504 MMC_ATTR_FN(hwrev, "0x%x\n", card->cid.hwrev); 505 MMC_ATTR_FN(manfid, "0x%06x\n", card->cid.manfid); 506 MMC_ATTR_FN(name, "%s\n", card->cid.prod_name); 507 MMC_ATTR_FN(oemid, "0x%04x\n", card->cid.oemid); 508 MMC_ATTR_FN(serial, "0x%08x\n", card->cid.serial); 509 510 static struct device_attribute mmc_sd_dev_attrs[] = { 511 MMC_ATTR_RO(cid), 512 MMC_ATTR_RO(csd), 513 MMC_ATTR_RO(scr), 514 MMC_ATTR_RO(date), 515 MMC_ATTR_RO(fwrev), 516 MMC_ATTR_RO(hwrev), 517 MMC_ATTR_RO(manfid), 518 MMC_ATTR_RO(name), 519 MMC_ATTR_RO(oemid), 520 MMC_ATTR_RO(serial), 521 __ATTR_NULL, 522 }; 523 524 /* 525 * Adds sysfs entries as relevant. 526 */ 527 static int mmc_sd_sysfs_add(struct mmc_host *host, struct mmc_card *card) 528 { 529 int ret; 530 531 ret = mmc_add_attrs(card, mmc_sd_dev_attrs); 532 if (ret < 0) 533 return ret; 534 535 return 0; 536 } 537 538 /* 539 * Removes the sysfs entries added by mmc_sysfs_add(). 540 */ 541 static void mmc_sd_sysfs_remove(struct mmc_host *host, struct mmc_card *card) 542 { 543 mmc_remove_attrs(card, mmc_sd_dev_attrs); 544 } 545 546 #ifdef CONFIG_MMC_UNSAFE_RESUME 547 548 /* 549 * Suspend callback from host. 550 */ 551 static void mmc_sd_suspend(struct mmc_host *host) 552 { 553 BUG_ON(!host); 554 BUG_ON(!host->card); 555 556 mmc_claim_host(host); 557 mmc_deselect_cards(host); 558 host->card->state &= ~MMC_STATE_HIGHSPEED; 559 mmc_release_host(host); 560 } 561 562 /* 563 * Resume callback from host. 564 * 565 * This function tries to determine if the same card is still present 566 * and, if so, restore all state to it. 567 */ 568 static void mmc_sd_resume(struct mmc_host *host) 569 { 570 int err; 571 572 BUG_ON(!host); 573 BUG_ON(!host->card); 574 575 mmc_claim_host(host); 576 577 err = mmc_sd_init_card(host, host->ocr, host->card); 578 if (err != MMC_ERR_NONE) { 579 mmc_sd_remove(host); 580 mmc_detach_bus(host); 581 } 582 583 mmc_release_host(host); 584 } 585 586 #else 587 588 #define mmc_sd_suspend NULL 589 #define mmc_sd_resume NULL 590 591 #endif 592 593 static const struct mmc_bus_ops mmc_sd_ops = { 594 .remove = mmc_sd_remove, 595 .detect = mmc_sd_detect, 596 .sysfs_add = mmc_sd_sysfs_add, 597 .sysfs_remove = mmc_sd_sysfs_remove, 598 .suspend = mmc_sd_suspend, 599 .resume = mmc_sd_resume, 600 }; 601 602 /* 603 * Starting point for SD card init. 604 */ 605 int mmc_attach_sd(struct mmc_host *host, u32 ocr) 606 { 607 int err; 608 609 BUG_ON(!host); 610 BUG_ON(!host->claimed); 611 612 mmc_attach_bus(host, &mmc_sd_ops); 613 614 /* 615 * Sanity check the voltages that the card claims to 616 * support. 617 */ 618 if (ocr & 0x7F) { 619 printk(KERN_WARNING "%s: card claims to support voltages " 620 "below the defined range. These will be ignored.\n", 621 mmc_hostname(host)); 622 ocr &= ~0x7F; 623 } 624 625 if (ocr & MMC_VDD_165_195) { 626 printk(KERN_WARNING "%s: SD card claims to support the " 627 "incompletely defined 'low voltage range'. This " 628 "will be ignored.\n", mmc_hostname(host)); 629 ocr &= ~MMC_VDD_165_195; 630 } 631 632 host->ocr = mmc_select_voltage(host, ocr); 633 634 /* 635 * Can we support the voltage(s) of the card(s)? 636 */ 637 if (!host->ocr) 638 goto err; 639 640 /* 641 * Detect and init the card. 642 */ 643 err = mmc_sd_init_card(host, host->ocr, NULL); 644 if (err != MMC_ERR_NONE) 645 goto err; 646 647 mmc_release_host(host); 648 649 err = mmc_add_card(host->card); 650 if (err) 651 goto reclaim_host; 652 653 return 0; 654 655 reclaim_host: 656 mmc_claim_host(host); 657 mmc_remove_card(host->card); 658 host->card = NULL; 659 err: 660 mmc_detach_bus(host); 661 mmc_release_host(host); 662 663 return 0; 664 } 665 666