spl.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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 <dm.h>
  11. #include <spl.h>
  12. #include <asm/u-boot.h>
  13. #include <nand.h>
  14. #include <fat.h>
  15. #include <version.h>
  16. #include <i2c.h>
  17. #include <image.h>
  18. #include <malloc.h>
  19. #include <dm/root.h>
  20. #include <linux/compiler.h>
  21. DECLARE_GLOBAL_DATA_PTR;
  22. #ifndef CONFIG_SYS_UBOOT_START
  23. #define CONFIG_SYS_UBOOT_START CONFIG_SYS_TEXT_BASE
  24. #endif
  25. #ifndef CONFIG_SYS_MONITOR_LEN
  26. /* Unknown U-Boot size, let's assume it will not be more than 200 KB */
  27. #define CONFIG_SYS_MONITOR_LEN (200 * 1024)
  28. #endif
  29. u32 *boot_params_ptr = NULL;
  30. struct spl_image_info spl_image;
  31. /* Define board data structure */
  32. static bd_t bdata __attribute__ ((section(".data")));
  33. /*
  34. * Default function to determine if u-boot or the OS should
  35. * be started. This implementation always returns 1.
  36. *
  37. * Please implement your own board specific funcion to do this.
  38. *
  39. * RETURN
  40. * 0 to not start u-boot
  41. * positive if u-boot should start
  42. */
  43. #ifdef CONFIG_SPL_OS_BOOT
  44. __weak int spl_start_uboot(void)
  45. {
  46. puts("SPL: Please implement spl_start_uboot() for your board\n");
  47. puts("SPL: Direct Linux boot not active!\n");
  48. return 1;
  49. }
  50. #endif
  51. /*
  52. * Weak default function for board specific cleanup/preparation before
  53. * Linux boot. Some boards/platforms might not need it, so just provide
  54. * an empty stub here.
  55. */
  56. __weak void spl_board_prepare_for_linux(void)
  57. {
  58. /* Nothing to do! */
  59. }
  60. void spl_set_header_raw_uboot(void)
  61. {
  62. spl_image.size = CONFIG_SYS_MONITOR_LEN;
  63. spl_image.entry_point = CONFIG_SYS_UBOOT_START;
  64. spl_image.load_addr = CONFIG_SYS_TEXT_BASE;
  65. spl_image.os = IH_OS_U_BOOT;
  66. spl_image.name = "U-Boot";
  67. }
  68. void spl_parse_image_header(const struct image_header *header)
  69. {
  70. u32 header_size = sizeof(struct image_header);
  71. if (image_get_magic(header) == IH_MAGIC) {
  72. if (spl_image.flags & SPL_COPY_PAYLOAD_ONLY) {
  73. /*
  74. * On some system (e.g. powerpc), the load-address and
  75. * entry-point is located at address 0. We can't load
  76. * to 0-0x40. So skip header in this case.
  77. */
  78. spl_image.load_addr = image_get_load(header);
  79. spl_image.entry_point = image_get_ep(header);
  80. spl_image.size = image_get_data_size(header);
  81. } else {
  82. spl_image.entry_point = image_get_load(header);
  83. /* Load including the header */
  84. spl_image.load_addr = spl_image.entry_point -
  85. header_size;
  86. spl_image.size = image_get_data_size(header) +
  87. header_size;
  88. }
  89. spl_image.os = image_get_os(header);
  90. spl_image.name = image_get_name(header);
  91. debug("spl: payload image: %.*s load addr: 0x%x size: %d\n",
  92. (int)sizeof(spl_image.name), spl_image.name,
  93. spl_image.load_addr, spl_image.size);
  94. } else {
  95. #ifdef CONFIG_SPL_PANIC_ON_RAW_IMAGE
  96. /*
  97. * CONFIG_SPL_PANIC_ON_RAW_IMAGE is defined when the
  98. * code which loads images in SPL cannot guarantee that
  99. * absolutely all read errors will be reported.
  100. * An example is the LPC32XX MLC NAND driver, which
  101. * will consider that a completely unreadable NAND block
  102. * is bad, and thus should be skipped silently.
  103. */
  104. panic("** no mkimage signature but raw image not supported");
  105. #else
  106. /* Signature not found - assume u-boot.bin */
  107. debug("mkimage signature not found - ih_magic = %x\n",
  108. header->ih_magic);
  109. spl_set_header_raw_uboot();
  110. #endif
  111. }
  112. }
  113. __weak void __noreturn jump_to_image_no_args(struct spl_image_info *spl_image)
  114. {
  115. typedef void __noreturn (*image_entry_noargs_t)(void);
  116. image_entry_noargs_t image_entry =
  117. (image_entry_noargs_t)(unsigned long)spl_image->entry_point;
  118. debug("image entry point: 0x%X\n", spl_image->entry_point);
  119. image_entry();
  120. }
  121. #ifdef CONFIG_SPL_RAM_DEVICE
  122. static void spl_ram_load_image(void)
  123. {
  124. const struct image_header *header;
  125. /*
  126. * Get the header. It will point to an address defined by handoff
  127. * which will tell where the image located inside the flash. For
  128. * now, it will temporary fixed to address pointed by U-Boot.
  129. */
  130. header = (struct image_header *)
  131. (CONFIG_SYS_TEXT_BASE - sizeof(struct image_header));
  132. spl_parse_image_header(header);
  133. }
  134. #endif
  135. void board_init_r(gd_t *dummy1, ulong dummy2)
  136. {
  137. u32 boot_device;
  138. int ret;
  139. debug(">>spl:board_init_r()\n");
  140. #if defined(CONFIG_SYS_SPL_MALLOC_START)
  141. mem_malloc_init(CONFIG_SYS_SPL_MALLOC_START,
  142. CONFIG_SYS_SPL_MALLOC_SIZE);
  143. gd->flags |= GD_FLG_FULL_MALLOC_INIT;
  144. #elif defined(CONFIG_SYS_MALLOC_F_LEN)
  145. gd->malloc_limit = CONFIG_SYS_MALLOC_F_LEN;
  146. gd->malloc_ptr = 0;
  147. #endif
  148. if (IS_ENABLED(CONFIG_OF_CONTROL) &&
  149. !IS_ENABLED(CONFIG_SPL_DISABLE_OF_CONTROL)) {
  150. ret = fdtdec_setup();
  151. if (ret) {
  152. debug("fdtdec_setup() returned error %d\n", ret);
  153. hang();
  154. }
  155. }
  156. if (IS_ENABLED(CONFIG_SPL_DM)) {
  157. ret = dm_init_and_scan(true);
  158. if (ret) {
  159. debug("dm_init_and_scan() returned error %d\n", ret);
  160. hang();
  161. }
  162. }
  163. #ifndef CONFIG_PPC
  164. /*
  165. * timer_init() does not exist on PPC systems. The timer is initialized
  166. * and enabled (decrementer) in interrupt_init() here.
  167. */
  168. timer_init();
  169. #endif
  170. #ifdef CONFIG_SPL_BOARD_INIT
  171. spl_board_init();
  172. #endif
  173. boot_device = spl_boot_device();
  174. debug("boot device - %d\n", boot_device);
  175. switch (boot_device) {
  176. #ifdef CONFIG_SPL_RAM_DEVICE
  177. case BOOT_DEVICE_RAM:
  178. spl_ram_load_image();
  179. break;
  180. #endif
  181. #ifdef CONFIG_SPL_MMC_SUPPORT
  182. case BOOT_DEVICE_MMC1:
  183. case BOOT_DEVICE_MMC2:
  184. case BOOT_DEVICE_MMC2_2:
  185. spl_mmc_load_image();
  186. break;
  187. #endif
  188. #ifdef CONFIG_SPL_NAND_SUPPORT
  189. case BOOT_DEVICE_NAND:
  190. spl_nand_load_image();
  191. break;
  192. #endif
  193. #ifdef CONFIG_SPL_ONENAND_SUPPORT
  194. case BOOT_DEVICE_ONENAND:
  195. spl_onenand_load_image();
  196. break;
  197. #endif
  198. #ifdef CONFIG_SPL_NOR_SUPPORT
  199. case BOOT_DEVICE_NOR:
  200. spl_nor_load_image();
  201. break;
  202. #endif
  203. #ifdef CONFIG_SPL_YMODEM_SUPPORT
  204. case BOOT_DEVICE_UART:
  205. spl_ymodem_load_image();
  206. break;
  207. #endif
  208. #ifdef CONFIG_SPL_SPI_SUPPORT
  209. case BOOT_DEVICE_SPI:
  210. spl_spi_load_image();
  211. break;
  212. #endif
  213. #ifdef CONFIG_SPL_ETH_SUPPORT
  214. case BOOT_DEVICE_CPGMAC:
  215. #ifdef CONFIG_SPL_ETH_DEVICE
  216. spl_net_load_image(CONFIG_SPL_ETH_DEVICE);
  217. #else
  218. spl_net_load_image(NULL);
  219. #endif
  220. break;
  221. #endif
  222. #ifdef CONFIG_SPL_USBETH_SUPPORT
  223. case BOOT_DEVICE_USBETH:
  224. spl_net_load_image("usb_ether");
  225. break;
  226. #endif
  227. #ifdef CONFIG_SPL_USB_SUPPORT
  228. case BOOT_DEVICE_USB:
  229. spl_usb_load_image();
  230. break;
  231. #endif
  232. #ifdef CONFIG_SPL_SATA_SUPPORT
  233. case BOOT_DEVICE_SATA:
  234. spl_sata_load_image();
  235. break;
  236. #endif
  237. #ifdef CONFIG_SPL_BOARD_LOAD_IMAGE
  238. case BOOT_DEVICE_BOARD:
  239. spl_board_load_image();
  240. break;
  241. #endif
  242. default:
  243. #if defined(CONFIG_SPL_SERIAL_SUPPORT) && defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
  244. puts("SPL: Unsupported Boot Device!\n");
  245. #endif
  246. hang();
  247. }
  248. switch (spl_image.os) {
  249. case IH_OS_U_BOOT:
  250. debug("Jumping to U-Boot\n");
  251. break;
  252. #ifdef CONFIG_SPL_OS_BOOT
  253. case IH_OS_LINUX:
  254. debug("Jumping to Linux\n");
  255. spl_board_prepare_for_linux();
  256. jump_to_image_linux((void *)CONFIG_SYS_SPL_ARGS_ADDR);
  257. #endif
  258. default:
  259. debug("Unsupported OS image.. Jumping nevertheless..\n");
  260. }
  261. #if defined(CONFIG_SYS_MALLOC_F_LEN) && !defined(CONFIG_SYS_SPL_MALLOC_SIZE)
  262. debug("SPL malloc() used %#lx bytes (%ld KB)\n", gd->malloc_ptr,
  263. gd->malloc_ptr / 1024);
  264. #endif
  265. jump_to_image_no_args(&spl_image);
  266. }
  267. /*
  268. * This requires UART clocks to be enabled. In order for this to work the
  269. * caller must ensure that the gd pointer is valid.
  270. */
  271. void preloader_console_init(void)
  272. {
  273. gd->bd = &bdata;
  274. gd->baudrate = CONFIG_BAUDRATE;
  275. serial_init(); /* serial communications setup */
  276. gd->have_console = 1;
  277. puts("\nU-Boot SPL " PLAIN_VERSION " (" U_BOOT_DATE " - " \
  278. U_BOOT_TIME ")\n");
  279. #ifdef CONFIG_SPL_DISPLAY_PRINT
  280. spl_display_print();
  281. #endif
  282. }
  283. /**
  284. * spl_relocate_stack_gd() - Relocate stack ready for board_init_r() execution
  285. *
  286. * Sometimes board_init_f() runs with a stack in SRAM but we want to use SDRAM
  287. * for the main board_init_r() execution. This is typically because we need
  288. * more stack space for things like the MMC sub-system.
  289. *
  290. * This function calculates the stack position, copies the global_data into
  291. * place and returns the new stack position. The caller is responsible for
  292. * setting up the sp register.
  293. *
  294. * @return new stack location, or 0 to use the same stack
  295. */
  296. ulong spl_relocate_stack_gd(void)
  297. {
  298. #ifdef CONFIG_SPL_STACK_R
  299. gd_t *new_gd;
  300. ulong ptr;
  301. /* Get stack position: use 8-byte alignment for ABI compliance */
  302. ptr = CONFIG_SPL_STACK_R_ADDR - sizeof(gd_t);
  303. ptr &= ~7;
  304. new_gd = (gd_t *)ptr;
  305. memcpy(new_gd, (void *)gd, sizeof(gd_t));
  306. gd = new_gd;
  307. /* Clear the BSS. */
  308. memset(__bss_start, 0, __bss_end - __bss_start);
  309. return ptr;
  310. #else
  311. return 0;
  312. #endif
  313. }