spl.c 12 KB

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