1 /* 2 * Copyright 2008, Freescale Semiconductor, Inc 3 * Andy Fleming 4 * 5 * Based vaguely on the Linux code 6 * 7 * See file CREDITS for list of people who contributed to this 8 * project. 9 * 10 * This program is free software; you can redistribute it and/or 11 * modify it under the terms of the GNU General Public License as 12 * published by the Free Software Foundation; either version 2 of 13 * the License, or (at your option) any later version. 14 * 15 * This program is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU General Public License for more details. 19 * 20 * You should have received a copy of the GNU General Public License 21 * along with this program; if not, write to the Free Software 22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, 23 * MA 02111-1307 USA 24 */ 25 26 #include <config.h> 27 #include <common.h> 28 #include <command.h> 29 #include <mmc.h> 30 #include <part.h> 31 #include <malloc.h> 32 #include <linux/list.h> 33 #include <mmc.h> 34 #include <div64.h> 35 36 static struct list_head mmc_devices; 37 static int cur_dev_num = -1; 38 39 int __board_mmc_getcd(u8 *cd, struct mmc *mmc) { 40 return -1; 41 } 42 43 int board_mmc_getcd(u8 *cd, struct mmc *mmc)__attribute__((weak, 44 alias("__board_mmc_getcd"))); 45 46 int mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data) 47 { 48 return mmc->send_cmd(mmc, cmd, data); 49 } 50 51 int mmc_set_blocklen(struct mmc *mmc, int len) 52 { 53 struct mmc_cmd cmd; 54 55 cmd.cmdidx = MMC_CMD_SET_BLOCKLEN; 56 cmd.resp_type = MMC_RSP_R1; 57 cmd.cmdarg = len; 58 cmd.flags = 0; 59 60 return mmc_send_cmd(mmc, &cmd, NULL); 61 } 62 63 struct mmc *find_mmc_device(int dev_num) 64 { 65 struct mmc *m; 66 struct list_head *entry; 67 68 list_for_each(entry, &mmc_devices) { 69 m = list_entry(entry, struct mmc, link); 70 71 if (m->block_dev.dev == dev_num) 72 return m; 73 } 74 75 printf("MMC Device %d not found\n", dev_num); 76 77 return NULL; 78 } 79 80 static ulong 81 mmc_write_blocks(struct mmc *mmc, ulong start, lbaint_t blkcnt, const void*src) 82 { 83 struct mmc_cmd cmd; 84 struct mmc_data data; 85 86 if ((start + blkcnt) > mmc->block_dev.lba) { 87 printf("MMC: block number 0x%lx exceeds max(0x%lx)\n", 88 start + blkcnt, mmc->block_dev.lba); 89 return 0; 90 } 91 92 if (blkcnt > 1) 93 cmd.cmdidx = MMC_CMD_WRITE_MULTIPLE_BLOCK; 94 else 95 cmd.cmdidx = MMC_CMD_WRITE_SINGLE_BLOCK; 96 97 if (mmc->high_capacity) 98 cmd.cmdarg = start; 99 else 100 cmd.cmdarg = start * mmc->write_bl_len; 101 102 cmd.resp_type = MMC_RSP_R1; 103 cmd.flags = 0; 104 105 data.src = src; 106 data.blocks = blkcnt; 107 data.blocksize = mmc->write_bl_len; 108 data.flags = MMC_DATA_WRITE; 109 110 if (mmc_send_cmd(mmc, &cmd, &data)) { 111 printf("mmc write failed\n"); 112 return 0; 113 } 114 115 if (blkcnt > 1) { 116 cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION; 117 cmd.cmdarg = 0; 118 cmd.resp_type = MMC_RSP_R1b; 119 cmd.flags = 0; 120 if (mmc_send_cmd(mmc, &cmd, NULL)) { 121 printf("mmc fail to send stop cmd\n"); 122 return 0; 123 } 124 } 125 126 return blkcnt; 127 } 128 129 static ulong 130 mmc_bwrite(int dev_num, ulong start, lbaint_t blkcnt, const void*src) 131 { 132 lbaint_t cur, blocks_todo = blkcnt; 133 134 struct mmc *mmc = find_mmc_device(dev_num); 135 if (!mmc) 136 return 0; 137 138 if (mmc_set_blocklen(mmc, mmc->write_bl_len)) 139 return 0; 140 141 do { 142 /* 143 * The 65535 constraint comes from some hardware has 144 * only 16 bit width block number counter 145 */ 146 cur = (blocks_todo > 65535) ? 65535 : blocks_todo; 147 if(mmc_write_blocks(mmc, start, cur, src) != cur) 148 return 0; 149 blocks_todo -= cur; 150 start += cur; 151 src += cur * mmc->write_bl_len; 152 } while (blocks_todo > 0); 153 154 return blkcnt; 155 } 156 157 int mmc_read_blocks(struct mmc *mmc, void *dst, ulong start, lbaint_t blkcnt) 158 { 159 struct mmc_cmd cmd; 160 struct mmc_data data; 161 162 if (blkcnt > 1) 163 cmd.cmdidx = MMC_CMD_READ_MULTIPLE_BLOCK; 164 else 165 cmd.cmdidx = MMC_CMD_READ_SINGLE_BLOCK; 166 167 if (mmc->high_capacity) 168 cmd.cmdarg = start; 169 else 170 cmd.cmdarg = start * mmc->read_bl_len; 171 172 cmd.resp_type = MMC_RSP_R1; 173 cmd.flags = 0; 174 175 data.dest = dst; 176 data.blocks = blkcnt; 177 data.blocksize = mmc->read_bl_len; 178 data.flags = MMC_DATA_READ; 179 180 if (mmc_send_cmd(mmc, &cmd, &data)) 181 return 0; 182 183 if (blkcnt > 1) { 184 cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION; 185 cmd.cmdarg = 0; 186 cmd.resp_type = MMC_RSP_R1b; 187 cmd.flags = 0; 188 if (mmc_send_cmd(mmc, &cmd, NULL)) { 189 printf("mmc fail to send stop cmd\n"); 190 return 0; 191 } 192 } 193 194 return blkcnt; 195 } 196 197 static ulong mmc_bread(int dev_num, ulong start, lbaint_t blkcnt, void *dst) 198 { 199 lbaint_t cur, blocks_todo = blkcnt; 200 201 if (blkcnt == 0) 202 return 0; 203 204 struct mmc *mmc = find_mmc_device(dev_num); 205 if (!mmc) 206 return 0; 207 208 if ((start + blkcnt) > mmc->block_dev.lba) { 209 printf("MMC: block number 0x%lx exceeds max(0x%lx)\n", 210 start + blkcnt, mmc->block_dev.lba); 211 return 0; 212 } 213 214 if (mmc_set_blocklen(mmc, mmc->read_bl_len)) 215 return 0; 216 217 do { 218 /* 219 * The 65535 constraint comes from some hardware has 220 * only 16 bit width block number counter 221 */ 222 cur = (blocks_todo > 65535) ? 65535 : blocks_todo; 223 if(mmc_read_blocks(mmc, dst, start, cur) != cur) 224 return 0; 225 blocks_todo -= cur; 226 start += cur; 227 dst += cur * mmc->read_bl_len; 228 } while (blocks_todo > 0); 229 230 return blkcnt; 231 } 232 233 int mmc_go_idle(struct mmc* mmc) 234 { 235 struct mmc_cmd cmd; 236 int err; 237 238 udelay(1000); 239 240 cmd.cmdidx = MMC_CMD_GO_IDLE_STATE; 241 cmd.cmdarg = 0; 242 cmd.resp_type = MMC_RSP_NONE; 243 cmd.flags = 0; 244 245 err = mmc_send_cmd(mmc, &cmd, NULL); 246 247 if (err) 248 return err; 249 250 udelay(2000); 251 252 return 0; 253 } 254 255 int 256 sd_send_op_cond(struct mmc *mmc) 257 { 258 int timeout = 1000; 259 int err; 260 struct mmc_cmd cmd; 261 262 do { 263 cmd.cmdidx = MMC_CMD_APP_CMD; 264 cmd.resp_type = MMC_RSP_R1; 265 cmd.cmdarg = 0; 266 cmd.flags = 0; 267 268 err = mmc_send_cmd(mmc, &cmd, NULL); 269 270 if (err) 271 return err; 272 273 cmd.cmdidx = SD_CMD_APP_SEND_OP_COND; 274 cmd.resp_type = MMC_RSP_R3; 275 276 /* 277 * Most cards do not answer if some reserved bits 278 * in the ocr are set. However, Some controller 279 * can set bit 7 (reserved for low voltages), but 280 * how to manage low voltages SD card is not yet 281 * specified. 282 */ 283 cmd.cmdarg = mmc->voltages & 0xff8000; 284 285 if (mmc->version == SD_VERSION_2) 286 cmd.cmdarg |= OCR_HCS; 287 288 err = mmc_send_cmd(mmc, &cmd, NULL); 289 290 if (err) 291 return err; 292 293 udelay(1000); 294 } while ((!(cmd.response[0] & OCR_BUSY)) && timeout--); 295 296 if (timeout <= 0) 297 return UNUSABLE_ERR; 298 299 if (mmc->version != SD_VERSION_2) 300 mmc->version = SD_VERSION_1_0; 301 302 mmc->ocr = cmd.response[0]; 303 304 mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS); 305 mmc->rca = 0; 306 307 return 0; 308 } 309 310 int mmc_send_op_cond(struct mmc *mmc) 311 { 312 int timeout = 1000; 313 struct mmc_cmd cmd; 314 int err; 315 316 /* Some cards seem to need this */ 317 mmc_go_idle(mmc); 318 319 do { 320 cmd.cmdidx = MMC_CMD_SEND_OP_COND; 321 cmd.resp_type = MMC_RSP_R3; 322 cmd.cmdarg = OCR_HCS | mmc->voltages; 323 cmd.flags = 0; 324 325 err = mmc_send_cmd(mmc, &cmd, NULL); 326 327 if (err) 328 return err; 329 330 udelay(1000); 331 } while (!(cmd.response[0] & OCR_BUSY) && timeout--); 332 333 if (timeout <= 0) 334 return UNUSABLE_ERR; 335 336 mmc->version = MMC_VERSION_UNKNOWN; 337 mmc->ocr = cmd.response[0]; 338 339 mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS); 340 mmc->rca = 0; 341 342 return 0; 343 } 344 345 346 int mmc_send_ext_csd(struct mmc *mmc, char *ext_csd) 347 { 348 struct mmc_cmd cmd; 349 struct mmc_data data; 350 int err; 351 352 /* Get the Card Status Register */ 353 cmd.cmdidx = MMC_CMD_SEND_EXT_CSD; 354 cmd.resp_type = MMC_RSP_R1; 355 cmd.cmdarg = 0; 356 cmd.flags = 0; 357 358 data.dest = ext_csd; 359 data.blocks = 1; 360 data.blocksize = 512; 361 data.flags = MMC_DATA_READ; 362 363 err = mmc_send_cmd(mmc, &cmd, &data); 364 365 return err; 366 } 367 368 369 int mmc_switch(struct mmc *mmc, u8 set, u8 index, u8 value) 370 { 371 struct mmc_cmd cmd; 372 373 cmd.cmdidx = MMC_CMD_SWITCH; 374 cmd.resp_type = MMC_RSP_R1b; 375 cmd.cmdarg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) | 376 (index << 16) | 377 (value << 8); 378 cmd.flags = 0; 379 380 return mmc_send_cmd(mmc, &cmd, NULL); 381 } 382 383 int mmc_change_freq(struct mmc *mmc) 384 { 385 char ext_csd[512]; 386 char cardtype; 387 int err; 388 389 mmc->card_caps = 0; 390 391 /* Only version 4 supports high-speed */ 392 if (mmc->version < MMC_VERSION_4) 393 return 0; 394 395 mmc->card_caps |= MMC_MODE_4BIT; 396 397 err = mmc_send_ext_csd(mmc, ext_csd); 398 399 if (err) 400 return err; 401 402 if (ext_csd[212] || ext_csd[213] || ext_csd[214] || ext_csd[215]) 403 mmc->high_capacity = 1; 404 405 cardtype = ext_csd[196] & 0xf; 406 407 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_HS_TIMING, 1); 408 409 if (err) 410 return err; 411 412 /* Now check to see that it worked */ 413 err = mmc_send_ext_csd(mmc, ext_csd); 414 415 if (err) 416 return err; 417 418 /* No high-speed support */ 419 if (!ext_csd[185]) 420 return 0; 421 422 /* High Speed is set, there are two types: 52MHz and 26MHz */ 423 if (cardtype & MMC_HS_52MHZ) 424 mmc->card_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS; 425 else 426 mmc->card_caps |= MMC_MODE_HS; 427 428 return 0; 429 } 430 431 int sd_switch(struct mmc *mmc, int mode, int group, u8 value, u8 *resp) 432 { 433 struct mmc_cmd cmd; 434 struct mmc_data data; 435 436 /* Switch the frequency */ 437 cmd.cmdidx = SD_CMD_SWITCH_FUNC; 438 cmd.resp_type = MMC_RSP_R1; 439 cmd.cmdarg = (mode << 31) | 0xffffff; 440 cmd.cmdarg &= ~(0xf << (group * 4)); 441 cmd.cmdarg |= value << (group * 4); 442 cmd.flags = 0; 443 444 data.dest = (char *)resp; 445 data.blocksize = 64; 446 data.blocks = 1; 447 data.flags = MMC_DATA_READ; 448 449 return mmc_send_cmd(mmc, &cmd, &data); 450 } 451 452 453 int sd_change_freq(struct mmc *mmc) 454 { 455 int err; 456 struct mmc_cmd cmd; 457 uint scr[2]; 458 uint switch_status[16]; 459 struct mmc_data data; 460 int timeout; 461 462 mmc->card_caps = 0; 463 464 /* Read the SCR to find out if this card supports higher speeds */ 465 cmd.cmdidx = MMC_CMD_APP_CMD; 466 cmd.resp_type = MMC_RSP_R1; 467 cmd.cmdarg = mmc->rca << 16; 468 cmd.flags = 0; 469 470 err = mmc_send_cmd(mmc, &cmd, NULL); 471 472 if (err) 473 return err; 474 475 cmd.cmdidx = SD_CMD_APP_SEND_SCR; 476 cmd.resp_type = MMC_RSP_R1; 477 cmd.cmdarg = 0; 478 cmd.flags = 0; 479 480 timeout = 3; 481 482 retry_scr: 483 data.dest = (char *)&scr; 484 data.blocksize = 8; 485 data.blocks = 1; 486 data.flags = MMC_DATA_READ; 487 488 err = mmc_send_cmd(mmc, &cmd, &data); 489 490 if (err) { 491 if (timeout--) 492 goto retry_scr; 493 494 return err; 495 } 496 497 mmc->scr[0] = __be32_to_cpu(scr[0]); 498 mmc->scr[1] = __be32_to_cpu(scr[1]); 499 500 switch ((mmc->scr[0] >> 24) & 0xf) { 501 case 0: 502 mmc->version = SD_VERSION_1_0; 503 break; 504 case 1: 505 mmc->version = SD_VERSION_1_10; 506 break; 507 case 2: 508 mmc->version = SD_VERSION_2; 509 break; 510 default: 511 mmc->version = SD_VERSION_1_0; 512 break; 513 } 514 515 /* Version 1.0 doesn't support switching */ 516 if (mmc->version == SD_VERSION_1_0) 517 return 0; 518 519 timeout = 4; 520 while (timeout--) { 521 err = sd_switch(mmc, SD_SWITCH_CHECK, 0, 1, 522 (u8 *)&switch_status); 523 524 if (err) 525 return err; 526 527 /* The high-speed function is busy. Try again */ 528 if (!(__be32_to_cpu(switch_status[7]) & SD_HIGHSPEED_BUSY)) 529 break; 530 } 531 532 if (mmc->scr[0] & SD_DATA_4BIT) 533 mmc->card_caps |= MMC_MODE_4BIT; 534 535 /* If high-speed isn't supported, we return */ 536 if (!(__be32_to_cpu(switch_status[3]) & SD_HIGHSPEED_SUPPORTED)) 537 return 0; 538 539 err = sd_switch(mmc, SD_SWITCH_SWITCH, 0, 1, (u8 *)&switch_status); 540 541 if (err) 542 return err; 543 544 if ((__be32_to_cpu(switch_status[4]) & 0x0f000000) == 0x01000000) 545 mmc->card_caps |= MMC_MODE_HS; 546 547 return 0; 548 } 549 550 /* frequency bases */ 551 /* divided by 10 to be nice to platforms without floating point */ 552 int fbase[] = { 553 10000, 554 100000, 555 1000000, 556 10000000, 557 }; 558 559 /* Multiplier values for TRAN_SPEED. Multiplied by 10 to be nice 560 * to platforms without floating point. 561 */ 562 int multipliers[] = { 563 0, /* reserved */ 564 10, 565 12, 566 13, 567 15, 568 20, 569 25, 570 30, 571 35, 572 40, 573 45, 574 50, 575 55, 576 60, 577 70, 578 80, 579 }; 580 581 void mmc_set_ios(struct mmc *mmc) 582 { 583 mmc->set_ios(mmc); 584 } 585 586 void mmc_set_clock(struct mmc *mmc, uint clock) 587 { 588 if (clock > mmc->f_max) 589 clock = mmc->f_max; 590 591 if (clock < mmc->f_min) 592 clock = mmc->f_min; 593 594 mmc->clock = clock; 595 596 mmc_set_ios(mmc); 597 } 598 599 void mmc_set_bus_width(struct mmc *mmc, uint width) 600 { 601 mmc->bus_width = width; 602 603 mmc_set_ios(mmc); 604 } 605 606 int mmc_startup(struct mmc *mmc) 607 { 608 int err; 609 uint mult, freq; 610 u64 cmult, csize; 611 struct mmc_cmd cmd; 612 char ext_csd[512]; 613 614 /* Put the Card in Identify Mode */ 615 cmd.cmdidx = MMC_CMD_ALL_SEND_CID; 616 cmd.resp_type = MMC_RSP_R2; 617 cmd.cmdarg = 0; 618 cmd.flags = 0; 619 620 err = mmc_send_cmd(mmc, &cmd, NULL); 621 622 if (err) 623 return err; 624 625 memcpy(mmc->cid, cmd.response, 16); 626 627 /* 628 * For MMC cards, set the Relative Address. 629 * For SD cards, get the Relatvie Address. 630 * This also puts the cards into Standby State 631 */ 632 cmd.cmdidx = SD_CMD_SEND_RELATIVE_ADDR; 633 cmd.cmdarg = mmc->rca << 16; 634 cmd.resp_type = MMC_RSP_R6; 635 cmd.flags = 0; 636 637 err = mmc_send_cmd(mmc, &cmd, NULL); 638 639 if (err) 640 return err; 641 642 if (IS_SD(mmc)) 643 mmc->rca = (cmd.response[0] >> 16) & 0xffff; 644 645 /* Get the Card-Specific Data */ 646 cmd.cmdidx = MMC_CMD_SEND_CSD; 647 cmd.resp_type = MMC_RSP_R2; 648 cmd.cmdarg = mmc->rca << 16; 649 cmd.flags = 0; 650 651 err = mmc_send_cmd(mmc, &cmd, NULL); 652 653 if (err) 654 return err; 655 656 mmc->csd[0] = cmd.response[0]; 657 mmc->csd[1] = cmd.response[1]; 658 mmc->csd[2] = cmd.response[2]; 659 mmc->csd[3] = cmd.response[3]; 660 661 if (mmc->version == MMC_VERSION_UNKNOWN) { 662 int version = (cmd.response[0] >> 26) & 0xf; 663 664 switch (version) { 665 case 0: 666 mmc->version = MMC_VERSION_1_2; 667 break; 668 case 1: 669 mmc->version = MMC_VERSION_1_4; 670 break; 671 case 2: 672 mmc->version = MMC_VERSION_2_2; 673 break; 674 case 3: 675 mmc->version = MMC_VERSION_3; 676 break; 677 case 4: 678 mmc->version = MMC_VERSION_4; 679 break; 680 default: 681 mmc->version = MMC_VERSION_1_2; 682 break; 683 } 684 } 685 686 /* divide frequency by 10, since the mults are 10x bigger */ 687 freq = fbase[(cmd.response[0] & 0x7)]; 688 mult = multipliers[((cmd.response[0] >> 3) & 0xf)]; 689 690 mmc->tran_speed = freq * mult; 691 692 mmc->read_bl_len = 1 << ((cmd.response[1] >> 16) & 0xf); 693 694 if (IS_SD(mmc)) 695 mmc->write_bl_len = mmc->read_bl_len; 696 else 697 mmc->write_bl_len = 1 << ((cmd.response[3] >> 22) & 0xf); 698 699 if (mmc->high_capacity) { 700 csize = (mmc->csd[1] & 0x3f) << 16 701 | (mmc->csd[2] & 0xffff0000) >> 16; 702 cmult = 8; 703 } else { 704 csize = (mmc->csd[1] & 0x3ff) << 2 705 | (mmc->csd[2] & 0xc0000000) >> 30; 706 cmult = (mmc->csd[2] & 0x00038000) >> 15; 707 } 708 709 mmc->capacity = (csize + 1) << (cmult + 2); 710 mmc->capacity *= mmc->read_bl_len; 711 712 if (mmc->read_bl_len > 512) 713 mmc->read_bl_len = 512; 714 715 if (mmc->write_bl_len > 512) 716 mmc->write_bl_len = 512; 717 718 /* Select the card, and put it into Transfer Mode */ 719 cmd.cmdidx = MMC_CMD_SELECT_CARD; 720 cmd.resp_type = MMC_RSP_R1b; 721 cmd.cmdarg = mmc->rca << 16; 722 cmd.flags = 0; 723 err = mmc_send_cmd(mmc, &cmd, NULL); 724 725 if (err) 726 return err; 727 728 if (!IS_SD(mmc) && (mmc->version >= MMC_VERSION_4)) { 729 /* check ext_csd version and capacity */ 730 err = mmc_send_ext_csd(mmc, ext_csd); 731 if (!err & (ext_csd[192] >= 2)) { 732 mmc->capacity = ext_csd[212] << 0 | ext_csd[213] << 8 | 733 ext_csd[214] << 16 | ext_csd[215] << 24; 734 mmc->capacity *= 512; 735 } 736 } 737 738 if (IS_SD(mmc)) 739 err = sd_change_freq(mmc); 740 else 741 err = mmc_change_freq(mmc); 742 743 if (err) 744 return err; 745 746 /* Restrict card's capabilities by what the host can do */ 747 mmc->card_caps &= mmc->host_caps; 748 749 if (IS_SD(mmc)) { 750 if (mmc->card_caps & MMC_MODE_4BIT) { 751 cmd.cmdidx = MMC_CMD_APP_CMD; 752 cmd.resp_type = MMC_RSP_R1; 753 cmd.cmdarg = mmc->rca << 16; 754 cmd.flags = 0; 755 756 err = mmc_send_cmd(mmc, &cmd, NULL); 757 if (err) 758 return err; 759 760 cmd.cmdidx = SD_CMD_APP_SET_BUS_WIDTH; 761 cmd.resp_type = MMC_RSP_R1; 762 cmd.cmdarg = 2; 763 cmd.flags = 0; 764 err = mmc_send_cmd(mmc, &cmd, NULL); 765 if (err) 766 return err; 767 768 mmc_set_bus_width(mmc, 4); 769 } 770 771 if (mmc->card_caps & MMC_MODE_HS) 772 mmc_set_clock(mmc, 50000000); 773 else 774 mmc_set_clock(mmc, 25000000); 775 } else { 776 if (mmc->card_caps & MMC_MODE_4BIT) { 777 /* Set the card to use 4 bit*/ 778 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, 779 EXT_CSD_BUS_WIDTH, 780 EXT_CSD_BUS_WIDTH_4); 781 782 if (err) 783 return err; 784 785 mmc_set_bus_width(mmc, 4); 786 } else if (mmc->card_caps & MMC_MODE_8BIT) { 787 /* Set the card to use 8 bit*/ 788 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, 789 EXT_CSD_BUS_WIDTH, 790 EXT_CSD_BUS_WIDTH_8); 791 792 if (err) 793 return err; 794 795 mmc_set_bus_width(mmc, 8); 796 } 797 798 if (mmc->card_caps & MMC_MODE_HS) { 799 if (mmc->card_caps & MMC_MODE_HS_52MHz) 800 mmc_set_clock(mmc, 52000000); 801 else 802 mmc_set_clock(mmc, 26000000); 803 } else 804 mmc_set_clock(mmc, 20000000); 805 } 806 807 /* fill in device description */ 808 mmc->block_dev.lun = 0; 809 mmc->block_dev.type = 0; 810 mmc->block_dev.blksz = mmc->read_bl_len; 811 mmc->block_dev.lba = lldiv(mmc->capacity, mmc->read_bl_len); 812 sprintf(mmc->block_dev.vendor, "Man %06x Snr %08x", mmc->cid[0] >> 8, 813 (mmc->cid[2] << 8) | (mmc->cid[3] >> 24)); 814 sprintf(mmc->block_dev.product, "%c%c%c%c%c", mmc->cid[0] & 0xff, 815 (mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff, 816 (mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff); 817 sprintf(mmc->block_dev.revision, "%d.%d", mmc->cid[2] >> 28, 818 (mmc->cid[2] >> 24) & 0xf); 819 init_part(&mmc->block_dev); 820 821 return 0; 822 } 823 824 int mmc_send_if_cond(struct mmc *mmc) 825 { 826 struct mmc_cmd cmd; 827 int err; 828 829 cmd.cmdidx = SD_CMD_SEND_IF_COND; 830 /* We set the bit if the host supports voltages between 2.7 and 3.6 V */ 831 cmd.cmdarg = ((mmc->voltages & 0xff8000) != 0) << 8 | 0xaa; 832 cmd.resp_type = MMC_RSP_R7; 833 cmd.flags = 0; 834 835 err = mmc_send_cmd(mmc, &cmd, NULL); 836 837 if (err) 838 return err; 839 840 if ((cmd.response[0] & 0xff) != 0xaa) 841 return UNUSABLE_ERR; 842 else 843 mmc->version = SD_VERSION_2; 844 845 return 0; 846 } 847 848 int mmc_register(struct mmc *mmc) 849 { 850 /* Setup the universal parts of the block interface just once */ 851 mmc->block_dev.if_type = IF_TYPE_MMC; 852 mmc->block_dev.dev = cur_dev_num++; 853 mmc->block_dev.removable = 1; 854 mmc->block_dev.block_read = mmc_bread; 855 mmc->block_dev.block_write = mmc_bwrite; 856 857 INIT_LIST_HEAD (&mmc->link); 858 859 list_add_tail (&mmc->link, &mmc_devices); 860 861 return 0; 862 } 863 864 block_dev_desc_t *mmc_get_dev(int dev) 865 { 866 struct mmc *mmc = find_mmc_device(dev); 867 868 return mmc ? &mmc->block_dev : NULL; 869 } 870 871 int mmc_init(struct mmc *mmc) 872 { 873 int err; 874 875 err = mmc->init(mmc); 876 877 if (err) 878 return err; 879 880 mmc_set_bus_width(mmc, 1); 881 mmc_set_clock(mmc, 1); 882 883 /* Reset the Card */ 884 err = mmc_go_idle(mmc); 885 886 if (err) 887 return err; 888 889 /* Test for SD version 2 */ 890 err = mmc_send_if_cond(mmc); 891 892 /* Now try to get the SD card's operating condition */ 893 err = sd_send_op_cond(mmc); 894 895 /* If the command timed out, we check for an MMC card */ 896 if (err == TIMEOUT) { 897 err = mmc_send_op_cond(mmc); 898 899 if (err) { 900 printf("Card did not respond to voltage select!\n"); 901 return UNUSABLE_ERR; 902 } 903 } 904 905 return mmc_startup(mmc); 906 } 907 908 /* 909 * CPU and board-specific MMC initializations. Aliased function 910 * signals caller to move on 911 */ 912 static int __def_mmc_init(bd_t *bis) 913 { 914 return -1; 915 } 916 917 int cpu_mmc_init(bd_t *bis) __attribute__((weak, alias("__def_mmc_init"))); 918 int board_mmc_init(bd_t *bis) __attribute__((weak, alias("__def_mmc_init"))); 919 920 void print_mmc_devices(char separator) 921 { 922 struct mmc *m; 923 struct list_head *entry; 924 925 list_for_each(entry, &mmc_devices) { 926 m = list_entry(entry, struct mmc, link); 927 928 printf("%s: %d", m->name, m->block_dev.dev); 929 930 if (entry->next != &mmc_devices) 931 printf("%c ", separator); 932 } 933 934 printf("\n"); 935 } 936 937 int mmc_initialize(bd_t *bis) 938 { 939 INIT_LIST_HEAD (&mmc_devices); 940 cur_dev_num = 0; 941 942 if (board_mmc_init(bis) < 0) 943 cpu_mmc_init(bis); 944 945 print_mmc_devices(','); 946 947 return 0; 948 } 949