spl.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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. int spl_init(void)
  136. {
  137. int ret;
  138. debug("spl_init()\n");
  139. #if defined(CONFIG_SYS_MALLOC_F_LEN)
  140. gd->malloc_limit = CONFIG_SYS_MALLOC_F_LEN;
  141. gd->malloc_ptr = 0;
  142. #endif
  143. if (CONFIG_IS_ENABLED(OF_CONTROL)) {
  144. ret = fdtdec_setup();
  145. if (ret) {
  146. debug("fdtdec_setup() returned error %d\n", ret);
  147. return ret;
  148. }
  149. }
  150. if (IS_ENABLED(CONFIG_SPL_DM)) {
  151. ret = dm_init_and_scan(true);
  152. if (ret) {
  153. debug("dm_init_and_scan() returned error %d\n", ret);
  154. return ret;
  155. }
  156. }
  157. gd->flags |= GD_FLG_SPL_INIT;
  158. return 0;
  159. }
  160. void board_init_r(gd_t *dummy1, ulong dummy2)
  161. {
  162. u32 boot_device;
  163. debug(">>spl:board_init_r()\n");
  164. #if defined(CONFIG_SYS_SPL_MALLOC_START)
  165. mem_malloc_init(CONFIG_SYS_SPL_MALLOC_START,
  166. CONFIG_SYS_SPL_MALLOC_SIZE);
  167. gd->flags |= GD_FLG_FULL_MALLOC_INIT;
  168. #endif
  169. if (!(gd->flags & GD_FLG_SPL_INIT)) {
  170. if (spl_init())
  171. hang();
  172. }
  173. #ifndef CONFIG_PPC
  174. /*
  175. * timer_init() does not exist on PPC systems. The timer is initialized
  176. * and enabled (decrementer) in interrupt_init() here.
  177. */
  178. timer_init();
  179. #endif
  180. #ifdef CONFIG_SPL_BOARD_INIT
  181. spl_board_init();
  182. #endif
  183. boot_device = spl_boot_device();
  184. debug("boot device - %d\n", boot_device);
  185. switch (boot_device) {
  186. #ifdef CONFIG_SPL_RAM_DEVICE
  187. case BOOT_DEVICE_RAM:
  188. spl_ram_load_image();
  189. break;
  190. #endif
  191. #ifdef CONFIG_SPL_MMC_SUPPORT
  192. case BOOT_DEVICE_MMC1:
  193. case BOOT_DEVICE_MMC2:
  194. case BOOT_DEVICE_MMC2_2:
  195. spl_mmc_load_image();
  196. break;
  197. #endif
  198. #ifdef CONFIG_SPL_NAND_SUPPORT
  199. case BOOT_DEVICE_NAND:
  200. spl_nand_load_image();
  201. break;
  202. #endif
  203. #ifdef CONFIG_SPL_ONENAND_SUPPORT
  204. case BOOT_DEVICE_ONENAND:
  205. spl_onenand_load_image();
  206. break;
  207. #endif
  208. #ifdef CONFIG_SPL_NOR_SUPPORT
  209. case BOOT_DEVICE_NOR:
  210. spl_nor_load_image();
  211. break;
  212. #endif
  213. #ifdef CONFIG_SPL_YMODEM_SUPPORT
  214. case BOOT_DEVICE_UART:
  215. spl_ymodem_load_image();
  216. break;
  217. #endif
  218. #ifdef CONFIG_SPL_SPI_SUPPORT
  219. case BOOT_DEVICE_SPI:
  220. spl_spi_load_image();
  221. break;
  222. #endif
  223. #ifdef CONFIG_SPL_ETH_SUPPORT
  224. case BOOT_DEVICE_CPGMAC:
  225. #ifdef CONFIG_SPL_ETH_DEVICE
  226. spl_net_load_image(CONFIG_SPL_ETH_DEVICE);
  227. #else
  228. spl_net_load_image(NULL);
  229. #endif
  230. break;
  231. #endif
  232. #ifdef CONFIG_SPL_USBETH_SUPPORT
  233. case BOOT_DEVICE_USBETH:
  234. spl_net_load_image("usb_ether");
  235. break;
  236. #endif
  237. #ifdef CONFIG_SPL_USB_SUPPORT
  238. case BOOT_DEVICE_USB:
  239. spl_usb_load_image();
  240. break;
  241. #endif
  242. #ifdef CONFIG_SPL_SATA_SUPPORT
  243. case BOOT_DEVICE_SATA:
  244. spl_sata_load_image();
  245. break;
  246. #endif
  247. #ifdef CONFIG_SPL_BOARD_LOAD_IMAGE
  248. case BOOT_DEVICE_BOARD:
  249. spl_board_load_image();
  250. break;
  251. #endif
  252. default:
  253. #if defined(CONFIG_SPL_SERIAL_SUPPORT) && defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
  254. puts("SPL: Unsupported Boot Device!\n");
  255. #endif
  256. hang();
  257. }
  258. switch (spl_image.os) {
  259. case IH_OS_U_BOOT:
  260. debug("Jumping to U-Boot\n");
  261. break;
  262. #ifdef CONFIG_SPL_OS_BOOT
  263. case IH_OS_LINUX:
  264. debug("Jumping to Linux\n");
  265. spl_board_prepare_for_linux();
  266. jump_to_image_linux((void *)CONFIG_SYS_SPL_ARGS_ADDR);
  267. #endif
  268. default:
  269. debug("Unsupported OS image.. Jumping nevertheless..\n");
  270. }
  271. #if defined(CONFIG_SYS_MALLOC_F_LEN) && !defined(CONFIG_SYS_SPL_MALLOC_SIZE)
  272. debug("SPL malloc() used %#lx bytes (%ld KB)\n", gd->malloc_ptr,
  273. gd->malloc_ptr / 1024);
  274. #endif
  275. debug("loaded - jumping to U-Boot...");
  276. jump_to_image_no_args(&spl_image);
  277. }
  278. /*
  279. * This requires UART clocks to be enabled. In order for this to work the
  280. * caller must ensure that the gd pointer is valid.
  281. */
  282. void preloader_console_init(void)
  283. {
  284. gd->bd = &bdata;
  285. gd->baudrate = CONFIG_BAUDRATE;
  286. serial_init(); /* serial communications setup */
  287. gd->have_console = 1;
  288. puts("\nU-Boot SPL " PLAIN_VERSION " (" U_BOOT_DATE " - " \
  289. U_BOOT_TIME ")\n");
  290. #ifdef CONFIG_SPL_DISPLAY_PRINT
  291. spl_display_print();
  292. #endif
  293. }
  294. /**
  295. * spl_relocate_stack_gd() - Relocate stack ready for board_init_r() execution
  296. *
  297. * Sometimes board_init_f() runs with a stack in SRAM but we want to use SDRAM
  298. * for the main board_init_r() execution. This is typically because we need
  299. * more stack space for things like the MMC sub-system.
  300. *
  301. * This function calculates the stack position, copies the global_data into
  302. * place and returns the new stack position. The caller is responsible for
  303. * setting up the sp register.
  304. *
  305. * @return new stack location, or 0 to use the same stack
  306. */
  307. ulong spl_relocate_stack_gd(void)
  308. {
  309. #ifdef CONFIG_SPL_STACK_R
  310. gd_t *new_gd;
  311. ulong ptr;
  312. /* Get stack position: use 8-byte alignment for ABI compliance */
  313. ptr = CONFIG_SPL_STACK_R_ADDR - sizeof(gd_t);
  314. ptr &= ~7;
  315. new_gd = (gd_t *)ptr;
  316. memcpy(new_gd, (void *)gd, sizeof(gd_t));
  317. gd = new_gd;
  318. #ifdef CONFIG_SPL_SYS_MALLOC_SIMPLE
  319. if (CONFIG_SPL_STACK_R_MALLOC_SIMPLE_LEN) {
  320. if (!(gd->flags & GD_FLG_SPL_INIT))
  321. panic("spl_init must be called before heap reloc");
  322. ptr -= CONFIG_SPL_STACK_R_MALLOC_SIMPLE_LEN;
  323. gd->malloc_base = ptr;
  324. gd->malloc_limit = CONFIG_SPL_STACK_R_MALLOC_SIMPLE_LEN;
  325. gd->malloc_ptr = 0;
  326. }
  327. #endif
  328. return ptr;
  329. #else
  330. return 0;
  331. #endif
  332. }