spl.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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 (IS_ENABLED(CONFIG_OF_CONTROL) &&
  144. !IS_ENABLED(CONFIG_SPL_DISABLE_OF_CONTROL)) {
  145. ret = fdtdec_setup();
  146. if (ret) {
  147. debug("fdtdec_setup() returned error %d\n", ret);
  148. return ret;
  149. }
  150. }
  151. if (IS_ENABLED(CONFIG_SPL_DM)) {
  152. ret = dm_init_and_scan(true);
  153. if (ret) {
  154. debug("dm_init_and_scan() returned error %d\n", ret);
  155. return ret;
  156. }
  157. }
  158. gd->flags |= GD_FLG_SPL_INIT;
  159. return 0;
  160. }
  161. void board_init_r(gd_t *dummy1, ulong dummy2)
  162. {
  163. u32 boot_device;
  164. debug(">>spl:board_init_r()\n");
  165. #if defined(CONFIG_SYS_SPL_MALLOC_START)
  166. mem_malloc_init(CONFIG_SYS_SPL_MALLOC_START,
  167. CONFIG_SYS_SPL_MALLOC_SIZE);
  168. gd->flags |= GD_FLG_FULL_MALLOC_INIT;
  169. #endif
  170. if (!(gd->flags & GD_FLG_SPL_INIT)) {
  171. if (spl_init())
  172. hang();
  173. }
  174. #ifndef CONFIG_PPC
  175. /*
  176. * timer_init() does not exist on PPC systems. The timer is initialized
  177. * and enabled (decrementer) in interrupt_init() here.
  178. */
  179. timer_init();
  180. #endif
  181. #ifdef CONFIG_SPL_BOARD_INIT
  182. spl_board_init();
  183. #endif
  184. boot_device = spl_boot_device();
  185. debug("boot device - %d\n", boot_device);
  186. switch (boot_device) {
  187. #ifdef CONFIG_SPL_RAM_DEVICE
  188. case BOOT_DEVICE_RAM:
  189. spl_ram_load_image();
  190. break;
  191. #endif
  192. #ifdef CONFIG_SPL_MMC_SUPPORT
  193. case BOOT_DEVICE_MMC1:
  194. case BOOT_DEVICE_MMC2:
  195. case BOOT_DEVICE_MMC2_2:
  196. spl_mmc_load_image();
  197. break;
  198. #endif
  199. #ifdef CONFIG_SPL_NAND_SUPPORT
  200. case BOOT_DEVICE_NAND:
  201. spl_nand_load_image();
  202. break;
  203. #endif
  204. #ifdef CONFIG_SPL_ONENAND_SUPPORT
  205. case BOOT_DEVICE_ONENAND:
  206. spl_onenand_load_image();
  207. break;
  208. #endif
  209. #ifdef CONFIG_SPL_NOR_SUPPORT
  210. case BOOT_DEVICE_NOR:
  211. spl_nor_load_image();
  212. break;
  213. #endif
  214. #ifdef CONFIG_SPL_YMODEM_SUPPORT
  215. case BOOT_DEVICE_UART:
  216. spl_ymodem_load_image();
  217. break;
  218. #endif
  219. #ifdef CONFIG_SPL_SPI_SUPPORT
  220. case BOOT_DEVICE_SPI:
  221. spl_spi_load_image();
  222. break;
  223. #endif
  224. #ifdef CONFIG_SPL_ETH_SUPPORT
  225. case BOOT_DEVICE_CPGMAC:
  226. #ifdef CONFIG_SPL_ETH_DEVICE
  227. spl_net_load_image(CONFIG_SPL_ETH_DEVICE);
  228. #else
  229. spl_net_load_image(NULL);
  230. #endif
  231. break;
  232. #endif
  233. #ifdef CONFIG_SPL_USBETH_SUPPORT
  234. case BOOT_DEVICE_USBETH:
  235. spl_net_load_image("usb_ether");
  236. break;
  237. #endif
  238. #ifdef CONFIG_SPL_USB_SUPPORT
  239. case BOOT_DEVICE_USB:
  240. spl_usb_load_image();
  241. break;
  242. #endif
  243. #ifdef CONFIG_SPL_SATA_SUPPORT
  244. case BOOT_DEVICE_SATA:
  245. spl_sata_load_image();
  246. break;
  247. #endif
  248. #ifdef CONFIG_SPL_BOARD_LOAD_IMAGE
  249. case BOOT_DEVICE_BOARD:
  250. spl_board_load_image();
  251. break;
  252. #endif
  253. default:
  254. #if defined(CONFIG_SPL_SERIAL_SUPPORT) && defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
  255. puts("SPL: Unsupported Boot Device!\n");
  256. #endif
  257. hang();
  258. }
  259. switch (spl_image.os) {
  260. case IH_OS_U_BOOT:
  261. debug("Jumping to U-Boot\n");
  262. break;
  263. #ifdef CONFIG_SPL_OS_BOOT
  264. case IH_OS_LINUX:
  265. debug("Jumping to Linux\n");
  266. spl_board_prepare_for_linux();
  267. jump_to_image_linux((void *)CONFIG_SYS_SPL_ARGS_ADDR);
  268. #endif
  269. default:
  270. debug("Unsupported OS image.. Jumping nevertheless..\n");
  271. }
  272. #if defined(CONFIG_SYS_MALLOC_F_LEN) && !defined(CONFIG_SYS_SPL_MALLOC_SIZE)
  273. debug("SPL malloc() used %#lx bytes (%ld KB)\n", gd->malloc_ptr,
  274. gd->malloc_ptr / 1024);
  275. #endif
  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. /* Clear the BSS. */
  319. memset(__bss_start, 0, __bss_end - __bss_start);
  320. return ptr;
  321. #else
  322. return 0;
  323. #endif
  324. }