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