| 620 | /* Send get interface condition, CMD8. This is new for SD 2.x and enables |
| 621 | * getting the High Capacity Support flag HCS and checks that the |
| 622 | * voltage is right. Some MMCs accept this command but will still fail |
| 623 | * on ACMD41. SD 1.x cards will fails this command and don't support |
| 624 | * HCS (>2G capacity). SD spec requires the correct CRC7 be sent |
| 625 | * even when in SPI mode. So this will just change the default CRC7 |
| 626 | * from 0x95 to 0x87 (correct for this CMD/arg) and keep it there for |
| 627 | * all subsequent commands (which just require a don't care CRC byte). |
| 628 | */ |
| 629 | e->command[6] = 0x87; |
| 630 | rv = sd_card_send_command(e, SD_CARD_CMD_SEND_IF_COND, 0x000001aa); |
| 631 | |
| 632 | /* If passes CMD8 and response is not 0x1aa and card is SD, will |
| 633 | * reject the card. Is this right? XXX (Some MMC pass CMD8 !) |
| 634 | */ |
| 635 | volt_bad = 0; |
| 636 | |
| 637 | /* Regardless of whether CMD8 above passes or fails, send ACMD41. If |
| 638 | * card is MMC it will fail. But older SD <2.0 (which fail CMD8) will |
| 639 | * always stay "idle" if cmd_arg is non-zero, so set to 0 above on fail. |
| 640 | */ |
| 641 | if (rv > -1) { |
| 642 | cmd_arg = 1L << 30; |
| 643 | /* CMD8 passed, check response for voltage OK */ |
| 644 | rbuf = e->response + e->response_index; |
| 645 | if ( !((rbuf[3] & 0x01) && (rbuf[4]==0x0AA)) ) |
| 646 | volt_bad = 1; |
| 647 | } |
| 648 | else { |
| 649 | /* failed CMD8 so SD v1.x or MMC */ |
| 650 | cmd_arg = 0; |
| 651 | } |
| 652 | |
| 653 | /* Do SD style init commands first, CMD55 then ACMD41. */ |
| 654 | assume_sd = 1; |
| 655 | |
| 656 | /* assume high capacity until proven wrong (applies to SD |
| 657 | * and not yet existing MMC). */ |
| 658 | hcs = 1; |
| 659 | |
615 | | while (true) { |
616 | | rv = sd_card_send_command( e, SD_CARD_CMD_APP_CMD, 0); |
617 | | RTEMS_CLEANUP_RV_SC( rv, sc, sd_card_driver_init_cleanup, "Send: SD_CARD_CMD_APP_CMD"); |
618 | | rv = sd_card_send_command( e, SD_CARD_ACMD_SD_SEND_OP_COND, 0); |
619 | | RTEMS_CLEANUP_RV_SC( rv, sc, sd_card_driver_init_cleanup, "Send: SD_CARD_ACMD_SD_SEND_OP_COND"); |
| 661 | while (1) { |
| 662 | if (assume_sd) { |
| 663 | /* This command (CMD55) supported by SD and (most?) MMCs */ |
| 664 | rv = sd_card_send_command( e, SD_CARD_CMD_APP_CMD, 0); |
| 665 | if (rv < 0) { |
| 666 | /* Will fail for MMC per SanDisk SD manual but ok with my MMCs. |
| 667 | * MMC spec JEDEC JESD84-B41.pdf indicates 55 *is* supported! |
| 668 | */ |
| 669 | assume_sd = 0; |
| 670 | RTEMS_SYSLOG("CMD55 failed. Assume MMC and try CMD1\n"); |
| 671 | continue; |
| 672 | } |
| 673 | |
| 674 | /* This command (ACMD41) only supported by SD. Always fails if MMC. */ |
| 675 | rv = sd_card_send_command( e, SD_CARD_ACMD_SD_SEND_OP_COND, cmd_arg); |
| 676 | if (rv < 0) { |
| 677 | /* This *will* fail for MMC. If fails, bad/no card or card |
| 678 | * is MMC, do CMD58 then CMD1. |
| 679 | */ |
| 680 | assume_sd = 0; |
| 681 | #ifdef CHECK_HC_MMC |
| 682 | RTEMS_SYSLOG("ACMD41 failed. Assume MMC and do CMD58 (once) then CMD1\n"); |
| 683 | cmd_arg = 1L << 30; |
| 684 | do_cmd58 = 1; |
| 685 | #endif |
| 686 | continue; |
| 687 | } else { |
| 688 | /* passed ACMD41 so SD. If volt_bad it passed CMD8 but the |
| 689 | * response indicated voltage bad, so reject the card. |
| 690 | */ |
| 691 | if (volt_bad) |
| 692 | CLEANUP_RVSC( -1, sc, sd_card_driver_init_cleanup, "Bad voltage for SD"); |
| 693 | } |
| 694 | } else { |
| 695 | #ifdef CHECK_HC_MMC |
| 696 | /* |
| 697 | * Does not seem to be SD card. Do init for MMC. First send |
| 698 | * CMD58 once to enable check for HCS (similar to CMD8 of SD) with |
| 699 | * bits 30:29 set to 10b. This will work for MMC >=4.2. Older |
| 700 | * cards (<=4.1) may may not respond to CMD1 unless CMD58 |
| 701 | * is sent again with zero argument. |
| 702 | */ |
| 703 | if (do_cmd58) { |
| 704 | rv = sd_card_send_command( e, SD_CARD_CMD_READ_OCR, cmd_arg); |
| 705 | CLEANUP_RVSC( rv, sc, sd_card_driver_init_cleanup, "Failed 1st CMD58 for MMC"); |
| 706 | do_cmd58 = 0; /* a one-shot call */ |
| 707 | } |
| 708 | |
| 709 | /* do CMD1 */ |
| 710 | rv = sd_card_send_command( e, SD_CARD_CMD_SEND_OP_COND, 0); |
| 711 | if (cmd_arg == 0) { |
| 712 | CLEANUP_RVSC( rv, sc, sd_card_driver_init_cleanup, "Failed CMD58 for MMC"); |
| 713 | } else if (rv < 0) { |
| 714 | /* Send CMD58 again with zero arg value. Proves card is not hcs. */ |
| 715 | cmd_arg = 0; |
| 716 | do_cmd58 = 1; |
| 717 | hcs = 0; |
| 718 | } |
| 719 | #else |
| 720 | rv = sd_card_send_command( e, SD_CARD_CMD_SEND_OP_COND, 0); |
| 721 | CLEANUP_RVSC( rv, sc, sd_card_driver_init_cleanup, "Failed CMD58 for MMC"); |
| 722 | #endif |
| 723 | } |
628 | | }; |
| 733 | } |
| 734 | |
| 735 | /* now we know if we are sd or mmc */ |
| 736 | if (assume_sd) { |
| 737 | if (cmd_arg == 0) { |
| 738 | /* SD is <2.0 so never high capacity (<=2G) */ |
| 739 | hcs = 0; |
| 740 | } else { |
| 741 | /* SD is definitely 2.x. Now need to send CMD58 to get |
| 742 | * the OCR to see if the HCS bit is set (capacity >2G) |
| 743 | * or if bit is off (capacity <=2G, standard capacity). |
| 744 | */ |
| 745 | rv = sd_card_send_command( e, SD_CARD_CMD_READ_OCR, 0); |
| 746 | CLEANUP_RVSC( rv, sc, sd_card_driver_init_cleanup, "Failed CMD58 for SD 2.x"); |
| 747 | |
| 748 | /* The HCS bit of OCR should be in the 2nd response |
| 749 | * byte received (the 1st is the status byte "R1"). |
| 750 | * HCS is bit 30, one below the MSBit. |
| 751 | */ |
| 752 | hcs = e->response[e->response_index +1] & 0x40; |
| 753 | } |
| 754 | } else { |
| 755 | #if CHECK_HC_MMC |
| 756 | /* card is MMC. Unless already proven to be not HCS (< 4.2) must do |
| 757 | * CMD58 again to check the OCR bits 30:29. |
| 758 | */ |
| 759 | if (hcs) { |
| 760 | /* arg should still be correct since was never set to 0 */ |
| 761 | rv = sd_card_send_command( e, SD_CARD_CMD_READ_OCR, cmd_arg); |
| 762 | CLEANUP_RVSC( rv, sc, sd_card_driver_init_cleanup, "Failed CMD58 for MMC 4.2"); |
| 763 | |
| 764 | /* The HCS bit of OCR should be in the 2nd response |
| 765 | * byte received (the 1st is the status byte "R1"). |
| 766 | * High capacity only when [30:29] is 10b. |
| 767 | */ |
| 768 | hcs = ((e->response[1 + e->response_index] & 0x60) == 0x40); |
| 769 | } |
| 770 | #else |
| 771 | /* MMC is assumed to always be low capacity */ |
| 772 | hcs = 0; |
| 773 | #endif |
| 774 | } |
| 775 | /* if hcs (>2G parts) means data are read and written by block number |
| 776 | * not byte number. |
| 777 | */ |
| 778 | e->block_addr = hcs; |
| 779 | |