spl_mmc.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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 <linux/compiler.h>
  12. #include <asm/u-boot.h>
  13. #include <mmc.h>
  14. #include <image.h>
  15. DECLARE_GLOBAL_DATA_PTR;
  16. static int mmc_load_image_raw_sector(struct mmc *mmc, unsigned long sector)
  17. {
  18. unsigned long count;
  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. count = mmc->block_dev.block_read(0, sector, 1, header);
  25. if (count == 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. count = mmc->block_dev.block_read(0, sector, image_size_sectors,
  35. (void *) spl_image.load_addr);
  36. end:
  37. if (count == 0) {
  38. #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
  39. puts("spl: mmc block read error\n");
  40. #endif
  41. return -1;
  42. }
  43. return 0;
  44. }
  45. #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION
  46. static int mmc_load_image_raw_partition(struct mmc *mmc, int partition)
  47. {
  48. disk_partition_t info;
  49. int err;
  50. err = get_partition_info(&mmc->block_dev, partition, &info);
  51. if (err) {
  52. #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
  53. puts("spl: partition error\n");
  54. #endif
  55. return -1;
  56. }
  57. return mmc_load_image_raw_sector(mmc, info.start);
  58. }
  59. #endif
  60. #ifdef CONFIG_SPL_OS_BOOT
  61. static int mmc_load_image_raw_os(struct mmc *mmc)
  62. {
  63. unsigned long count;
  64. count = mmc->block_dev.block_read(0,
  65. CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR,
  66. CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS,
  67. (void *) CONFIG_SYS_SPL_ARGS_ADDR);
  68. if (count == 0) {
  69. #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
  70. puts("spl: mmc block read error\n");
  71. #endif
  72. return -1;
  73. }
  74. return mmc_load_image_raw_sector(mmc,
  75. CONFIG_SYS_MMCSD_RAW_MODE_KERNEL_SECTOR);
  76. }
  77. #endif
  78. void spl_mmc_load_image(void)
  79. {
  80. struct mmc *mmc;
  81. u32 boot_mode;
  82. int err;
  83. __maybe_unused int part;
  84. mmc_initialize(gd->bd);
  85. /* We register only one device. So, the dev id is always 0 */
  86. mmc = find_mmc_device(0);
  87. if (!mmc) {
  88. #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
  89. puts("spl: mmc device not found\n");
  90. #endif
  91. hang();
  92. }
  93. err = mmc_init(mmc);
  94. if (err) {
  95. #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
  96. printf("spl: mmc init failed with error: %d\n", err);
  97. #endif
  98. hang();
  99. }
  100. boot_mode = spl_boot_mode();
  101. switch (boot_mode) {
  102. case MMCSD_MODE_RAW:
  103. debug("spl: mmc boot mode: raw\n");
  104. #ifdef CONFIG_SPL_OS_BOOT
  105. if (!spl_start_uboot()) {
  106. err = mmc_load_image_raw_os(mmc);
  107. if (!err)
  108. return;
  109. }
  110. #endif
  111. #if defined(CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION)
  112. err = mmc_load_image_raw_partition(mmc,
  113. CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION);
  114. if (!err)
  115. return;
  116. #elif defined(CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR)
  117. err = mmc_load_image_raw_sector(mmc,
  118. CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR);
  119. if (!err)
  120. return;
  121. #endif
  122. case MMCSD_MODE_FS:
  123. debug("spl: mmc boot mode: fs\n");
  124. #ifdef CONFIG_SYS_MMCSD_FS_BOOT_PARTITION
  125. #ifdef CONFIG_SPL_FAT_SUPPORT
  126. #ifdef CONFIG_SPL_OS_BOOT
  127. if (!spl_start_uboot()) {
  128. err = spl_load_image_fat_os(&mmc->block_dev,
  129. CONFIG_SYS_MMCSD_FS_BOOT_PARTITION);
  130. if (!err)
  131. return;
  132. }
  133. #endif
  134. #ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
  135. err = spl_load_image_fat(&mmc->block_dev,
  136. CONFIG_SYS_MMCSD_FS_BOOT_PARTITION,
  137. CONFIG_SPL_FS_LOAD_PAYLOAD_NAME);
  138. if (!err)
  139. return;
  140. #endif
  141. #endif
  142. #ifdef CONFIG_SPL_EXT_SUPPORT
  143. #ifdef CONFIG_SPL_OS_BOOT
  144. if (!spl_start_uboot()) {
  145. err = spl_load_image_ext_os(&mmc->block_dev,
  146. CONFIG_SYS_MMCSD_FS_BOOT_PARTITION);
  147. if (!err)
  148. return;
  149. }
  150. #endif
  151. #ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
  152. err = spl_load_image_ext(&mmc->block_dev,
  153. CONFIG_SYS_MMCSD_FS_BOOT_PARTITION,
  154. CONFIG_SPL_FS_LOAD_PAYLOAD_NAME);
  155. if (!err)
  156. return;
  157. #endif
  158. #endif
  159. #endif
  160. #ifdef CONFIG_SUPPORT_EMMC_BOOT
  161. case MMCSD_MODE_EMMCBOOT:
  162. /*
  163. * We need to check what the partition is configured to.
  164. * 1 and 2 match up to boot0 / boot1 and 7 is user data
  165. * which is the first physical partition (0).
  166. */
  167. part = (mmc->part_config >> 3) & PART_ACCESS_MASK;
  168. if (part == 7)
  169. part = 0;
  170. if (mmc_switch_part(0, part)) {
  171. #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
  172. puts("spl: mmc partition switch failed\n");
  173. #endif
  174. hang();
  175. }
  176. #ifdef CONFIG_SPL_OS_BOOT
  177. if (!spl_start_uboot()) {
  178. err = mmc_load_image_raw_os(mmc);
  179. if (!err)
  180. return;
  181. }
  182. #endif
  183. #if defined(CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION)
  184. err = mmc_load_image_raw_partition(mmc,
  185. CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION);
  186. if (!err)
  187. return;
  188. #elif defined(CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR)
  189. err = mmc_load_image_raw_sector(mmc,
  190. CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR);
  191. if (!err)
  192. return;
  193. #endif
  194. #endif
  195. case MMCSD_MODE_UNDEFINED:
  196. default:
  197. #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
  198. if (err)
  199. puts("spl: mmc: no boot mode left to try\n");
  200. else
  201. puts("spl: mmc: wrong boot mode\n");
  202. #endif
  203. hang();
  204. }
  205. }