spl_mmc.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * (C) Copyright 2010
  3. * Texas Instruments, <www.ti.com>
  4. *
  5. * Aneesh V <aneesh@ti.com>
  6. *
  7. * SPDX-License-Identifier: GPL-2.0+
  8. */
  9. #include <common.h>
  10. #include <spl.h>
  11. #include <asm/u-boot.h>
  12. #include <mmc.h>
  13. #include <version.h>
  14. #include <image.h>
  15. DECLARE_GLOBAL_DATA_PTR;
  16. static int mmc_load_image_raw(struct mmc *mmc, unsigned long sector)
  17. {
  18. unsigned long err;
  19. u32 image_size_sectors;
  20. struct image_header *header;
  21. header = (struct image_header *)(CONFIG_SYS_TEXT_BASE -
  22. sizeof(struct image_header));
  23. /* read image header to find the image size & load address */
  24. err = mmc->block_dev.block_read(0, sector, 1, header);
  25. if (err == 0)
  26. goto end;
  27. if (image_get_magic(header) != IH_MAGIC)
  28. return -1;
  29. spl_parse_image_header(header);
  30. /* convert size to sectors - round up */
  31. image_size_sectors = (spl_image.size + mmc->read_bl_len - 1) /
  32. mmc->read_bl_len;
  33. /* Read the header too to avoid extra memcpy */
  34. err = mmc->block_dev.block_read(0, sector, image_size_sectors,
  35. (void *)spl_image.load_addr);
  36. end:
  37. #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
  38. if (err == 0)
  39. printf("spl: mmc blk read err - %lu\n", err);
  40. #endif
  41. return (err == 0);
  42. }
  43. #ifdef CONFIG_SPL_OS_BOOT
  44. static int mmc_load_image_raw_os(struct mmc *mmc)
  45. {
  46. if (!mmc->block_dev.block_read(0,
  47. CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR,
  48. CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS,
  49. (void *)CONFIG_SYS_SPL_ARGS_ADDR)) {
  50. #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
  51. printf("mmc args blk read error\n");
  52. #endif
  53. return -1;
  54. }
  55. return mmc_load_image_raw(mmc, CONFIG_SYS_MMCSD_RAW_MODE_KERNEL_SECTOR);
  56. }
  57. #endif
  58. void spl_mmc_load_image(void)
  59. {
  60. struct mmc *mmc;
  61. int err;
  62. u32 boot_mode;
  63. mmc_initialize(gd->bd);
  64. /* We register only one device. So, the dev id is always 0 */
  65. mmc = find_mmc_device(0);
  66. if (!mmc) {
  67. #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
  68. puts("spl: mmc device not found!!\n");
  69. #endif
  70. hang();
  71. }
  72. err = mmc_init(mmc);
  73. if (err) {
  74. #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
  75. printf("spl: mmc init failed: err - %d\n", err);
  76. #endif
  77. hang();
  78. }
  79. boot_mode = spl_boot_mode();
  80. if (boot_mode == MMCSD_MODE_RAW) {
  81. debug("boot mode - RAW\n");
  82. #ifdef CONFIG_SPL_OS_BOOT
  83. if (spl_start_uboot() || mmc_load_image_raw_os(mmc))
  84. #endif
  85. err = mmc_load_image_raw(mmc,
  86. CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR);
  87. #ifdef CONFIG_SPL_FAT_SUPPORT
  88. } else if (boot_mode == MMCSD_MODE_FAT) {
  89. debug("boot mode - FAT\n");
  90. #ifdef CONFIG_SPL_OS_BOOT
  91. if (spl_start_uboot() || spl_load_image_fat_os(&mmc->block_dev,
  92. CONFIG_SYS_MMC_SD_FAT_BOOT_PARTITION))
  93. #endif
  94. err = spl_load_image_fat(&mmc->block_dev,
  95. CONFIG_SYS_MMC_SD_FAT_BOOT_PARTITION,
  96. CONFIG_SPL_FAT_LOAD_PAYLOAD_NAME);
  97. #endif
  98. #ifdef CONFIG_SUPPORT_EMMC_BOOT
  99. } else if (boot_mode == MMCSD_MODE_EMMCBOOT) {
  100. /*
  101. * We need to check what the partition is configured to.
  102. * 1 and 2 match up to boot0 / boot1 and 7 is user data
  103. * which is the first physical partition (0).
  104. */
  105. int part = (mmc->part_config >> 3) & PART_ACCESS_MASK;
  106. if (part == 7)
  107. part = 0;
  108. if (mmc_switch_part(0, part)) {
  109. #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
  110. puts("MMC partition switch failed\n");
  111. #endif
  112. hang();
  113. }
  114. #ifdef CONFIG_SPL_OS_BOOT
  115. if (spl_start_uboot() || mmc_load_image_raw_os(mmc))
  116. #endif
  117. err = mmc_load_image_raw(mmc,
  118. CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR);
  119. #endif
  120. } else {
  121. #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
  122. puts("spl: wrong MMC boot mode\n");
  123. #endif
  124. hang();
  125. }
  126. if (err)
  127. hang();
  128. }