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