spl.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  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. * Board-specific Platform code can reimplement show_boot_progress () if needed
  35. */
  36. __weak void show_boot_progress(int val) {}
  37. /*
  38. * Default function to determine if u-boot or the OS should
  39. * be started. This implementation always returns 1.
  40. *
  41. * Please implement your own board specific funcion to do this.
  42. *
  43. * RETURN
  44. * 0 to not start u-boot
  45. * positive if u-boot should start
  46. */
  47. #ifdef CONFIG_SPL_OS_BOOT
  48. __weak int spl_start_uboot(void)
  49. {
  50. puts("SPL: Please implement spl_start_uboot() for your board\n");
  51. puts("SPL: Direct Linux boot not active!\n");
  52. return 1;
  53. }
  54. #endif
  55. /*
  56. * Weak default function for board specific cleanup/preparation before
  57. * Linux boot. Some boards/platforms might not need it, so just provide
  58. * an empty stub here.
  59. */
  60. __weak void spl_board_prepare_for_linux(void)
  61. {
  62. /* Nothing to do! */
  63. }
  64. __weak void spl_board_prepare_for_boot(void)
  65. {
  66. /* Nothing to do! */
  67. }
  68. void spl_set_header_raw_uboot(void)
  69. {
  70. spl_image.size = CONFIG_SYS_MONITOR_LEN;
  71. spl_image.entry_point = CONFIG_SYS_UBOOT_START;
  72. spl_image.load_addr = CONFIG_SYS_TEXT_BASE;
  73. spl_image.os = IH_OS_U_BOOT;
  74. spl_image.name = "U-Boot";
  75. }
  76. int spl_parse_image_header(const struct image_header *header)
  77. {
  78. u32 header_size = sizeof(struct image_header);
  79. if (image_get_magic(header) == IH_MAGIC) {
  80. if (spl_image.flags & SPL_COPY_PAYLOAD_ONLY) {
  81. /*
  82. * On some system (e.g. powerpc), the load-address and
  83. * entry-point is located at address 0. We can't load
  84. * to 0-0x40. So skip header in this case.
  85. */
  86. spl_image.load_addr = image_get_load(header);
  87. spl_image.entry_point = image_get_ep(header);
  88. spl_image.size = image_get_data_size(header);
  89. } else {
  90. spl_image.entry_point = image_get_load(header);
  91. /* Load including the header */
  92. spl_image.load_addr = spl_image.entry_point -
  93. header_size;
  94. spl_image.size = image_get_data_size(header) +
  95. header_size;
  96. }
  97. spl_image.os = image_get_os(header);
  98. spl_image.name = image_get_name(header);
  99. debug("spl: payload image: %.*s load addr: 0x%x size: %d\n",
  100. (int)sizeof(spl_image.name), spl_image.name,
  101. spl_image.load_addr, spl_image.size);
  102. } else {
  103. #ifdef CONFIG_SPL_PANIC_ON_RAW_IMAGE
  104. /*
  105. * CONFIG_SPL_PANIC_ON_RAW_IMAGE is defined when the
  106. * code which loads images in SPL cannot guarantee that
  107. * absolutely all read errors will be reported.
  108. * An example is the LPC32XX MLC NAND driver, which
  109. * will consider that a completely unreadable NAND block
  110. * is bad, and thus should be skipped silently.
  111. */
  112. panic("** no mkimage signature but raw image not supported");
  113. #elif defined(CONFIG_SPL_ABORT_ON_RAW_IMAGE)
  114. /* Signature not found, proceed to other boot methods. */
  115. return -EINVAL;
  116. #else
  117. /* Signature not found - assume u-boot.bin */
  118. debug("mkimage signature not found - ih_magic = %x\n",
  119. header->ih_magic);
  120. spl_set_header_raw_uboot();
  121. #endif
  122. }
  123. return 0;
  124. }
  125. __weak void __noreturn jump_to_image_no_args(struct spl_image_info *spl_image)
  126. {
  127. typedef void __noreturn (*image_entry_noargs_t)(void);
  128. image_entry_noargs_t image_entry =
  129. (image_entry_noargs_t)(unsigned long)spl_image->entry_point;
  130. debug("image entry point: 0x%X\n", spl_image->entry_point);
  131. image_entry();
  132. }
  133. #ifndef CONFIG_SPL_LOAD_FIT_ADDRESS
  134. # define CONFIG_SPL_LOAD_FIT_ADDRESS 0
  135. #endif
  136. #ifdef CONFIG_SPL_RAM_DEVICE
  137. static ulong spl_ram_load_read(struct spl_load_info *load, ulong sector,
  138. ulong count, void *buf)
  139. {
  140. debug("%s: sector %lx, count %lx, buf %lx\n",
  141. __func__, sector, count, (ulong)buf);
  142. memcpy(buf, (void *)(CONFIG_SPL_LOAD_FIT_ADDRESS + sector), count);
  143. return count;
  144. }
  145. static int spl_ram_load_image(void)
  146. {
  147. struct image_header *header;
  148. header = (struct image_header *)CONFIG_SPL_LOAD_FIT_ADDRESS;
  149. if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
  150. image_get_magic(header) == FDT_MAGIC) {
  151. struct spl_load_info load;
  152. debug("Found FIT\n");
  153. load.bl_len = 1;
  154. load.read = spl_ram_load_read;
  155. spl_load_simple_fit(&load, 0, header);
  156. } else {
  157. debug("Legacy image\n");
  158. /*
  159. * Get the header. It will point to an address defined by
  160. * handoff which will tell where the image located inside
  161. * the flash. For now, it will temporary fixed to address
  162. * pointed by U-Boot.
  163. */
  164. header = (struct image_header *)
  165. (CONFIG_SYS_TEXT_BASE - sizeof(struct image_header));
  166. spl_parse_image_header(header);
  167. }
  168. return 0;
  169. }
  170. #endif
  171. int spl_init(void)
  172. {
  173. int ret;
  174. debug("spl_init()\n");
  175. #if defined(CONFIG_SYS_MALLOC_F_LEN)
  176. #ifdef CONFIG_MALLOC_F_ADDR
  177. gd->malloc_base = CONFIG_MALLOC_F_ADDR;
  178. #endif
  179. gd->malloc_limit = CONFIG_SYS_MALLOC_F_LEN;
  180. gd->malloc_ptr = 0;
  181. #endif
  182. if (CONFIG_IS_ENABLED(OF_CONTROL)) {
  183. ret = fdtdec_setup();
  184. if (ret) {
  185. debug("fdtdec_setup() returned error %d\n", ret);
  186. return ret;
  187. }
  188. }
  189. if (IS_ENABLED(CONFIG_SPL_DM)) {
  190. ret = dm_init_and_scan(true);
  191. if (ret) {
  192. debug("dm_init_and_scan() returned error %d\n", ret);
  193. return ret;
  194. }
  195. }
  196. gd->flags |= GD_FLG_SPL_INIT;
  197. return 0;
  198. }
  199. #ifndef BOOT_DEVICE_NONE
  200. #define BOOT_DEVICE_NONE 0xdeadbeef
  201. #endif
  202. static u32 spl_boot_list[] = {
  203. BOOT_DEVICE_NONE,
  204. BOOT_DEVICE_NONE,
  205. BOOT_DEVICE_NONE,
  206. BOOT_DEVICE_NONE,
  207. BOOT_DEVICE_NONE,
  208. };
  209. __weak void board_boot_order(u32 *spl_boot_list)
  210. {
  211. spl_boot_list[0] = spl_boot_device();
  212. }
  213. #ifdef CONFIG_SPL_BOARD_LOAD_IMAGE
  214. __weak void spl_board_announce_boot_device(void) { }
  215. #endif
  216. #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
  217. struct boot_device_name {
  218. u32 boot_dev;
  219. const char *name;
  220. };
  221. struct boot_device_name boot_name_table[] = {
  222. #ifdef CONFIG_SPL_RAM_DEVICE
  223. { BOOT_DEVICE_RAM, "RAM" },
  224. #endif
  225. #ifdef CONFIG_SPL_MMC_SUPPORT
  226. { BOOT_DEVICE_MMC1, "MMC1" },
  227. { BOOT_DEVICE_MMC2, "MMC2" },
  228. { BOOT_DEVICE_MMC2_2, "MMC2_2" },
  229. #endif
  230. #ifdef CONFIG_SPL_NAND_SUPPORT
  231. { BOOT_DEVICE_NAND, "NAND" },
  232. #endif
  233. #ifdef CONFIG_SPL_ONENAND_SUPPORT
  234. { BOOT_DEVICE_ONENAND, "OneNAND" },
  235. #endif
  236. #ifdef CONFIG_SPL_NOR_SUPPORT
  237. { BOOT_DEVICE_NOR, "NOR" },
  238. #endif
  239. #ifdef CONFIG_SPL_YMODEM_SUPPORT
  240. { BOOT_DEVICE_UART, "UART" },
  241. #endif
  242. #ifdef CONFIG_SPL_SPI_SUPPORT
  243. { BOOT_DEVICE_SPI, "SPI" },
  244. #endif
  245. #ifdef CONFIG_SPL_ETH_SUPPORT
  246. #ifdef CONFIG_SPL_ETH_DEVICE
  247. { BOOT_DEVICE_CPGMAC, "eth device" },
  248. #else
  249. { BOOT_DEVICE_CPGMAC, "net" },
  250. #endif
  251. #endif
  252. #ifdef CONFIG_SPL_USBETH_SUPPORT
  253. { BOOT_DEVICE_USBETH, "USB eth" },
  254. #endif
  255. #ifdef CONFIG_SPL_USB_SUPPORT
  256. { BOOT_DEVICE_USB, "USB" },
  257. #endif
  258. #ifdef CONFIG_SPL_SATA_SUPPORT
  259. { BOOT_DEVICE_SATA, "SATA" },
  260. #endif
  261. /* Keep this entry last */
  262. { BOOT_DEVICE_NONE, "unknown boot device" },
  263. };
  264. static void announce_boot_device(u32 boot_device)
  265. {
  266. int i;
  267. puts("Trying to boot from ");
  268. #ifdef CONFIG_SPL_BOARD_LOAD_IMAGE
  269. if (boot_device == BOOT_DEVICE_BOARD) {
  270. spl_board_announce_boot_device();
  271. puts("\n");
  272. return;
  273. }
  274. #endif
  275. for (i = 0; i < ARRAY_SIZE(boot_name_table) - 1; i++) {
  276. if (boot_name_table[i].boot_dev == boot_device)
  277. break;
  278. }
  279. printf("%s\n", boot_name_table[i].name);
  280. }
  281. #else
  282. static inline void announce_boot_device(u32 boot_device) { }
  283. #endif
  284. static int spl_load_image(u32 boot_device)
  285. {
  286. switch (boot_device) {
  287. #ifdef CONFIG_SPL_RAM_DEVICE
  288. case BOOT_DEVICE_RAM:
  289. return spl_ram_load_image();
  290. #endif
  291. #ifdef CONFIG_SPL_MMC_SUPPORT
  292. case BOOT_DEVICE_MMC1:
  293. case BOOT_DEVICE_MMC2:
  294. case BOOT_DEVICE_MMC2_2:
  295. return spl_mmc_load_image(boot_device);
  296. #endif
  297. #ifdef CONFIG_SPL_NAND_SUPPORT
  298. case BOOT_DEVICE_NAND:
  299. return spl_nand_load_image();
  300. #endif
  301. #ifdef CONFIG_SPL_ONENAND_SUPPORT
  302. case BOOT_DEVICE_ONENAND:
  303. return spl_onenand_load_image();
  304. #endif
  305. #ifdef CONFIG_SPL_NOR_SUPPORT
  306. case BOOT_DEVICE_NOR:
  307. return spl_nor_load_image();
  308. #endif
  309. #ifdef CONFIG_SPL_YMODEM_SUPPORT
  310. case BOOT_DEVICE_UART:
  311. return spl_ymodem_load_image();
  312. #endif
  313. #ifdef CONFIG_SPL_SPI_SUPPORT
  314. case BOOT_DEVICE_SPI:
  315. return spl_spi_load_image();
  316. #endif
  317. #ifdef CONFIG_SPL_ETH_SUPPORT
  318. case BOOT_DEVICE_CPGMAC:
  319. #ifdef CONFIG_SPL_ETH_DEVICE
  320. return spl_net_load_image(CONFIG_SPL_ETH_DEVICE);
  321. #else
  322. return spl_net_load_image(NULL);
  323. #endif
  324. #endif
  325. #ifdef CONFIG_SPL_USBETH_SUPPORT
  326. case BOOT_DEVICE_USBETH:
  327. return spl_net_load_image("usb_ether");
  328. #endif
  329. #ifdef CONFIG_SPL_USB_SUPPORT
  330. case BOOT_DEVICE_USB:
  331. return spl_usb_load_image();
  332. #endif
  333. #ifdef CONFIG_SPL_SATA_SUPPORT
  334. case BOOT_DEVICE_SATA:
  335. return spl_sata_load_image();
  336. #endif
  337. #ifdef CONFIG_SPL_BOARD_LOAD_IMAGE
  338. case BOOT_DEVICE_BOARD:
  339. return spl_board_load_image();
  340. #endif
  341. default:
  342. #if defined(CONFIG_SPL_SERIAL_SUPPORT) && defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
  343. puts("SPL: Unsupported Boot Device!\n");
  344. #endif
  345. return -ENODEV;
  346. }
  347. return -EINVAL;
  348. }
  349. void board_init_r(gd_t *dummy1, ulong dummy2)
  350. {
  351. int i;
  352. debug(">>spl:board_init_r()\n");
  353. #if defined(CONFIG_SYS_SPL_MALLOC_START)
  354. mem_malloc_init(CONFIG_SYS_SPL_MALLOC_START,
  355. CONFIG_SYS_SPL_MALLOC_SIZE);
  356. gd->flags |= GD_FLG_FULL_MALLOC_INIT;
  357. #endif
  358. if (!(gd->flags & GD_FLG_SPL_INIT)) {
  359. if (spl_init())
  360. hang();
  361. }
  362. #ifndef CONFIG_PPC
  363. /*
  364. * timer_init() does not exist on PPC systems. The timer is initialized
  365. * and enabled (decrementer) in interrupt_init() here.
  366. */
  367. timer_init();
  368. #endif
  369. #ifdef CONFIG_SPL_BOARD_INIT
  370. spl_board_init();
  371. #endif
  372. board_boot_order(spl_boot_list);
  373. for (i = 0; i < ARRAY_SIZE(spl_boot_list) &&
  374. spl_boot_list[i] != BOOT_DEVICE_NONE; i++) {
  375. announce_boot_device(spl_boot_list[i]);
  376. if (!spl_load_image(spl_boot_list[i]))
  377. break;
  378. }
  379. if (i == ARRAY_SIZE(spl_boot_list) ||
  380. spl_boot_list[i] == BOOT_DEVICE_NONE) {
  381. puts("SPL: failed to boot from all boot devices\n");
  382. hang();
  383. }
  384. switch (spl_image.os) {
  385. case IH_OS_U_BOOT:
  386. debug("Jumping to U-Boot\n");
  387. break;
  388. #ifdef CONFIG_SPL_OS_BOOT
  389. case IH_OS_LINUX:
  390. debug("Jumping to Linux\n");
  391. spl_board_prepare_for_linux();
  392. jump_to_image_linux((void *)CONFIG_SYS_SPL_ARGS_ADDR);
  393. #endif
  394. default:
  395. debug("Unsupported OS image.. Jumping nevertheless..\n");
  396. }
  397. #if defined(CONFIG_SYS_MALLOC_F_LEN) && !defined(CONFIG_SYS_SPL_MALLOC_SIZE)
  398. debug("SPL malloc() used %#lx bytes (%ld KB)\n", gd->malloc_ptr,
  399. gd->malloc_ptr / 1024);
  400. #endif
  401. debug("loaded - jumping to U-Boot...");
  402. spl_board_prepare_for_boot();
  403. jump_to_image_no_args(&spl_image);
  404. }
  405. /*
  406. * This requires UART clocks to be enabled. In order for this to work the
  407. * caller must ensure that the gd pointer is valid.
  408. */
  409. void preloader_console_init(void)
  410. {
  411. gd->bd = &bdata;
  412. gd->baudrate = CONFIG_BAUDRATE;
  413. serial_init(); /* serial communications setup */
  414. gd->have_console = 1;
  415. puts("\nU-Boot SPL " PLAIN_VERSION " (" U_BOOT_DATE " - " \
  416. U_BOOT_TIME ")\n");
  417. #ifdef CONFIG_SPL_DISPLAY_PRINT
  418. spl_display_print();
  419. #endif
  420. }
  421. /**
  422. * spl_relocate_stack_gd() - Relocate stack ready for board_init_r() execution
  423. *
  424. * Sometimes board_init_f() runs with a stack in SRAM but we want to use SDRAM
  425. * for the main board_init_r() execution. This is typically because we need
  426. * more stack space for things like the MMC sub-system.
  427. *
  428. * This function calculates the stack position, copies the global_data into
  429. * place, sets the new gd (except for ARM, for which setting GD within a C
  430. * function may not always work) and returns the new stack position. The
  431. * caller is responsible for setting up the sp register and, in the case
  432. * of ARM, setting up gd.
  433. *
  434. * All of this is done using the same layout and alignments as done in
  435. * board_init_f_init_reserve() / board_init_f_alloc_reserve().
  436. *
  437. * @return new stack location, or 0 to use the same stack
  438. */
  439. ulong spl_relocate_stack_gd(void)
  440. {
  441. #ifdef CONFIG_SPL_STACK_R
  442. gd_t *new_gd;
  443. ulong ptr = CONFIG_SPL_STACK_R_ADDR;
  444. #ifdef CONFIG_SPL_SYS_MALLOC_SIMPLE
  445. if (CONFIG_SPL_STACK_R_MALLOC_SIMPLE_LEN) {
  446. ptr -= CONFIG_SPL_STACK_R_MALLOC_SIMPLE_LEN;
  447. gd->malloc_base = ptr;
  448. gd->malloc_limit = CONFIG_SPL_STACK_R_MALLOC_SIMPLE_LEN;
  449. gd->malloc_ptr = 0;
  450. }
  451. #endif
  452. /* Get stack position: use 8-byte alignment for ABI compliance */
  453. ptr = CONFIG_SPL_STACK_R_ADDR - roundup(sizeof(gd_t),16);
  454. new_gd = (gd_t *)ptr;
  455. memcpy(new_gd, (void *)gd, sizeof(gd_t));
  456. #if !defined(CONFIG_ARM)
  457. gd = new_gd;
  458. #endif
  459. return ptr;
  460. #else
  461. return 0;
  462. #endif
  463. }