spl.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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 int 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. return 0;
  134. }
  135. #endif
  136. int spl_init(void)
  137. {
  138. int ret;
  139. debug("spl_init()\n");
  140. #if defined(CONFIG_SYS_MALLOC_F_LEN)
  141. gd->malloc_limit = CONFIG_SYS_MALLOC_F_LEN;
  142. gd->malloc_ptr = 0;
  143. #endif
  144. if (CONFIG_IS_ENABLED(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. #ifndef BOOT_DEVICE_NONE
  162. #define BOOT_DEVICE_NONE 0xdeadbeef
  163. #endif
  164. static u32 spl_boot_list[] = {
  165. BOOT_DEVICE_NONE,
  166. BOOT_DEVICE_NONE,
  167. BOOT_DEVICE_NONE,
  168. BOOT_DEVICE_NONE,
  169. BOOT_DEVICE_NONE,
  170. };
  171. __weak void board_boot_order(u32 *spl_boot_list)
  172. {
  173. spl_boot_list[0] = spl_boot_device();
  174. }
  175. #ifdef CONFIG_SPL_BOARD_LOAD_IMAGE
  176. __weak void spl_board_announce_boot_device(void) { }
  177. #endif
  178. #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
  179. struct boot_device_name {
  180. u32 boot_dev;
  181. const char *name;
  182. };
  183. struct boot_device_name boot_name_table[] = {
  184. #ifdef CONFIG_SPL_RAM_DEVICE
  185. { BOOT_DEVICE_RAM, "RAM" },
  186. #endif
  187. #ifdef CONFIG_SPL_MMC_SUPPORT
  188. { BOOT_DEVICE_MMC1, "MMC" },
  189. { BOOT_DEVICE_MMC2, "MMC" },
  190. { BOOT_DEVICE_MMC2_2, "MMC" },
  191. #endif
  192. #ifdef CONFIG_SPL_NAND_SUPPORT
  193. { BOOT_DEVICE_NAND, "NAND" },
  194. #endif
  195. #ifdef CONFIG_SPL_ONENAND_SUPPORT
  196. { BOOT_DEVICE_ONENAND, "OneNAND" },
  197. #endif
  198. #ifdef CONFIG_SPL_NOR_SUPPORT
  199. { BOOT_DEVICE_NOR, "NOR" },
  200. #endif
  201. #ifdef CONFIG_SPL_YMODEM_SUPPORT
  202. { BOOT_DEVICE_UART, "UART" },
  203. #endif
  204. #ifdef CONFIG_SPL_SPI_SUPPORT
  205. { BOOT_DEVICE_SPI, "SPI" },
  206. #endif
  207. #ifdef CONFIG_SPL_ETH_SUPPORT
  208. #ifdef CONFIG_SPL_ETH_DEVICE
  209. { BOOT_DEVICE_CPGMAC, "eth device" },
  210. #else
  211. { BOOT_DEVICE_CPGMAC, "net" },
  212. #endif
  213. #endif
  214. #ifdef CONFIG_SPL_USBETH_SUPPORT
  215. { BOOT_DEVICE_USBETH, "USB eth" },
  216. #endif
  217. #ifdef CONFIG_SPL_USB_SUPPORT
  218. { BOOT_DEVICE_USB, "USB" },
  219. #endif
  220. #ifdef CONFIG_SPL_SATA_SUPPORT
  221. { BOOT_DEVICE_SATA, "SATA" },
  222. #endif
  223. /* Keep this entry last */
  224. { BOOT_DEVICE_NONE, "unknown boot device" },
  225. };
  226. static void announce_boot_device(u32 boot_device)
  227. {
  228. int i;
  229. puts("Trying to boot from ");
  230. #ifdef CONFIG_SPL_BOARD_LOAD_IMAGE
  231. if (boot_device == BOOT_DEVICE_BOARD) {
  232. spl_board_announce_boot_device();
  233. puts("\n");
  234. return;
  235. }
  236. #endif
  237. for (i = 0; i < ARRAY_SIZE(boot_name_table) - 1; i++) {
  238. if (boot_name_table[i].boot_dev == boot_device)
  239. break;
  240. }
  241. printf("%s\n", boot_name_table[i].name);
  242. }
  243. #else
  244. static inline void announce_boot_device(u32 boot_device) { }
  245. #endif
  246. static int spl_load_image(u32 boot_device)
  247. {
  248. switch (boot_device) {
  249. #ifdef CONFIG_SPL_RAM_DEVICE
  250. case BOOT_DEVICE_RAM:
  251. return spl_ram_load_image();
  252. #endif
  253. #ifdef CONFIG_SPL_MMC_SUPPORT
  254. case BOOT_DEVICE_MMC1:
  255. case BOOT_DEVICE_MMC2:
  256. case BOOT_DEVICE_MMC2_2:
  257. return spl_mmc_load_image(boot_device);
  258. #endif
  259. #ifdef CONFIG_SPL_NAND_SUPPORT
  260. case BOOT_DEVICE_NAND:
  261. return spl_nand_load_image();
  262. #endif
  263. #ifdef CONFIG_SPL_ONENAND_SUPPORT
  264. case BOOT_DEVICE_ONENAND:
  265. return spl_onenand_load_image();
  266. #endif
  267. #ifdef CONFIG_SPL_NOR_SUPPORT
  268. case BOOT_DEVICE_NOR:
  269. return spl_nor_load_image();
  270. #endif
  271. #ifdef CONFIG_SPL_YMODEM_SUPPORT
  272. case BOOT_DEVICE_UART:
  273. return spl_ymodem_load_image();
  274. #endif
  275. #ifdef CONFIG_SPL_SPI_SUPPORT
  276. case BOOT_DEVICE_SPI:
  277. return spl_spi_load_image();
  278. #endif
  279. #ifdef CONFIG_SPL_ETH_SUPPORT
  280. case BOOT_DEVICE_CPGMAC:
  281. #ifdef CONFIG_SPL_ETH_DEVICE
  282. return spl_net_load_image(CONFIG_SPL_ETH_DEVICE);
  283. #else
  284. return spl_net_load_image(NULL);
  285. #endif
  286. #endif
  287. #ifdef CONFIG_SPL_USBETH_SUPPORT
  288. case BOOT_DEVICE_USBETH:
  289. return spl_net_load_image("usb_ether");
  290. #endif
  291. #ifdef CONFIG_SPL_USB_SUPPORT
  292. case BOOT_DEVICE_USB:
  293. return spl_usb_load_image();
  294. #endif
  295. #ifdef CONFIG_SPL_SATA_SUPPORT
  296. case BOOT_DEVICE_SATA:
  297. return spl_sata_load_image();
  298. #endif
  299. #ifdef CONFIG_SPL_BOARD_LOAD_IMAGE
  300. case BOOT_DEVICE_BOARD:
  301. return spl_board_load_image();
  302. #endif
  303. default:
  304. #if defined(CONFIG_SPL_SERIAL_SUPPORT) && defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
  305. puts("SPL: Unsupported Boot Device!\n");
  306. #endif
  307. return -ENODEV;
  308. }
  309. return -EINVAL;
  310. }
  311. void board_init_r(gd_t *dummy1, ulong dummy2)
  312. {
  313. int i;
  314. debug(">>spl:board_init_r()\n");
  315. #if defined(CONFIG_SYS_SPL_MALLOC_START)
  316. mem_malloc_init(CONFIG_SYS_SPL_MALLOC_START,
  317. CONFIG_SYS_SPL_MALLOC_SIZE);
  318. gd->flags |= GD_FLG_FULL_MALLOC_INIT;
  319. #endif
  320. if (!(gd->flags & GD_FLG_SPL_INIT)) {
  321. if (spl_init())
  322. hang();
  323. }
  324. #ifndef CONFIG_PPC
  325. /*
  326. * timer_init() does not exist on PPC systems. The timer is initialized
  327. * and enabled (decrementer) in interrupt_init() here.
  328. */
  329. timer_init();
  330. #endif
  331. #ifdef CONFIG_SPL_BOARD_INIT
  332. spl_board_init();
  333. #endif
  334. board_boot_order(spl_boot_list);
  335. for (i = 0; i < ARRAY_SIZE(spl_boot_list) &&
  336. spl_boot_list[i] != BOOT_DEVICE_NONE; i++) {
  337. announce_boot_device(spl_boot_list[i]);
  338. if (!spl_load_image(spl_boot_list[i]))
  339. break;
  340. }
  341. if (i == ARRAY_SIZE(spl_boot_list) ||
  342. spl_boot_list[i] == BOOT_DEVICE_NONE) {
  343. puts("SPL: failed to boot from all boot devices\n");
  344. hang();
  345. }
  346. switch (spl_image.os) {
  347. case IH_OS_U_BOOT:
  348. debug("Jumping to U-Boot\n");
  349. break;
  350. #ifdef CONFIG_SPL_OS_BOOT
  351. case IH_OS_LINUX:
  352. debug("Jumping to Linux\n");
  353. spl_board_prepare_for_linux();
  354. jump_to_image_linux((void *)CONFIG_SYS_SPL_ARGS_ADDR);
  355. #endif
  356. default:
  357. debug("Unsupported OS image.. Jumping nevertheless..\n");
  358. }
  359. #if defined(CONFIG_SYS_MALLOC_F_LEN) && !defined(CONFIG_SYS_SPL_MALLOC_SIZE)
  360. debug("SPL malloc() used %#lx bytes (%ld KB)\n", gd->malloc_ptr,
  361. gd->malloc_ptr / 1024);
  362. #endif
  363. debug("loaded - jumping to U-Boot...");
  364. jump_to_image_no_args(&spl_image);
  365. }
  366. /*
  367. * This requires UART clocks to be enabled. In order for this to work the
  368. * caller must ensure that the gd pointer is valid.
  369. */
  370. void preloader_console_init(void)
  371. {
  372. gd->bd = &bdata;
  373. gd->baudrate = CONFIG_BAUDRATE;
  374. serial_init(); /* serial communications setup */
  375. gd->have_console = 1;
  376. puts("\nU-Boot SPL " PLAIN_VERSION " (" U_BOOT_DATE " - " \
  377. U_BOOT_TIME ")\n");
  378. #ifdef CONFIG_SPL_DISPLAY_PRINT
  379. spl_display_print();
  380. #endif
  381. }
  382. /**
  383. * spl_relocate_stack_gd() - Relocate stack ready for board_init_r() execution
  384. *
  385. * Sometimes board_init_f() runs with a stack in SRAM but we want to use SDRAM
  386. * for the main board_init_r() execution. This is typically because we need
  387. * more stack space for things like the MMC sub-system.
  388. *
  389. * This function calculates the stack position, copies the global_data into
  390. * place and returns the new stack position. The caller is responsible for
  391. * setting up the sp register.
  392. *
  393. * @return new stack location, or 0 to use the same stack
  394. */
  395. ulong spl_relocate_stack_gd(void)
  396. {
  397. #ifdef CONFIG_SPL_STACK_R
  398. gd_t *new_gd;
  399. ulong ptr;
  400. /* Get stack position: use 8-byte alignment for ABI compliance */
  401. ptr = CONFIG_SPL_STACK_R_ADDR - sizeof(gd_t);
  402. ptr &= ~7;
  403. new_gd = (gd_t *)ptr;
  404. memcpy(new_gd, (void *)gd, sizeof(gd_t));
  405. gd = new_gd;
  406. #ifdef CONFIG_SPL_SYS_MALLOC_SIMPLE
  407. if (CONFIG_SPL_STACK_R_MALLOC_SIMPLE_LEN) {
  408. if (!(gd->flags & GD_FLG_SPL_INIT))
  409. panic_str("spl_init must be called before heap reloc");
  410. ptr -= CONFIG_SPL_STACK_R_MALLOC_SIMPLE_LEN;
  411. gd->malloc_base = ptr;
  412. gd->malloc_limit = CONFIG_SPL_STACK_R_MALLOC_SIMPLE_LEN;
  413. gd->malloc_ptr = 0;
  414. }
  415. #endif
  416. return ptr;
  417. #else
  418. return 0;
  419. #endif
  420. }