spl.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  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 <image.h>
  17. #include <malloc.h>
  18. #include <dm/root.h>
  19. #include <linux/compiler.h>
  20. #include <fdt_support.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. /* Define board data structure */
  31. static bd_t bdata __attribute__ ((section(".data")));
  32. /*
  33. * Board-specific Platform code can reimplement show_boot_progress () if needed
  34. */
  35. __weak void show_boot_progress(int val) {}
  36. /*
  37. * Default function to determine if u-boot or the OS should
  38. * be started. This implementation always returns 1.
  39. *
  40. * Please implement your own board specific funcion to do this.
  41. *
  42. * RETURN
  43. * 0 to not start u-boot
  44. * positive if u-boot should start
  45. */
  46. #ifdef CONFIG_SPL_OS_BOOT
  47. __weak int spl_start_uboot(void)
  48. {
  49. puts("SPL: Please implement spl_start_uboot() for your board\n");
  50. puts("SPL: Direct Linux boot not active!\n");
  51. return 1;
  52. }
  53. /*
  54. * Weak default function for arch specific zImage check. Return zero
  55. * and fill start and end address if image is recognized.
  56. */
  57. int __weak bootz_setup(ulong image, ulong *start, ulong *end)
  58. {
  59. return 1;
  60. }
  61. #endif
  62. void spl_fixup_fdt(void)
  63. {
  64. #ifdef CONFIG_SPL_OF_LIBFDT
  65. void *fdt_blob = (void *)CONFIG_SYS_SPL_ARGS_ADDR;
  66. int err;
  67. err = fdt_check_header(fdt_blob);
  68. if (err < 0) {
  69. printf("fdt_root: %s\n", fdt_strerror(err));
  70. return;
  71. }
  72. /* fixup the memory dt node */
  73. err = fdt_shrink_to_minimum(fdt_blob, 0);
  74. if (err == 0) {
  75. printf("spl: fdt_shrink_to_minimum err - %d\n", err);
  76. return;
  77. }
  78. err = arch_fixup_fdt(fdt_blob);
  79. if (err) {
  80. printf("spl: arch_fixup_fdt err - %d\n", err);
  81. return;
  82. }
  83. #endif
  84. }
  85. /*
  86. * Weak default function for board specific cleanup/preparation before
  87. * Linux boot. Some boards/platforms might not need it, so just provide
  88. * an empty stub here.
  89. */
  90. __weak void spl_board_prepare_for_linux(void)
  91. {
  92. /* Nothing to do! */
  93. }
  94. __weak void spl_board_prepare_for_boot(void)
  95. {
  96. /* Nothing to do! */
  97. }
  98. void spl_set_header_raw_uboot(struct spl_image_info *spl_image)
  99. {
  100. spl_image->size = CONFIG_SYS_MONITOR_LEN;
  101. spl_image->entry_point = CONFIG_SYS_UBOOT_START;
  102. spl_image->load_addr = CONFIG_SYS_TEXT_BASE;
  103. spl_image->os = IH_OS_U_BOOT;
  104. spl_image->name = "U-Boot";
  105. }
  106. int spl_parse_image_header(struct spl_image_info *spl_image,
  107. const struct image_header *header)
  108. {
  109. if (image_get_magic(header) == IH_MAGIC) {
  110. #ifdef CONFIG_SPL_LEGACY_IMAGE_SUPPORT
  111. u32 header_size = sizeof(struct image_header);
  112. if (spl_image->flags & SPL_COPY_PAYLOAD_ONLY) {
  113. /*
  114. * On some system (e.g. powerpc), the load-address and
  115. * entry-point is located at address 0. We can't load
  116. * to 0-0x40. So skip header in this case.
  117. */
  118. spl_image->load_addr = image_get_load(header);
  119. spl_image->entry_point = image_get_ep(header);
  120. spl_image->size = image_get_data_size(header);
  121. } else {
  122. spl_image->entry_point = image_get_load(header);
  123. /* Load including the header */
  124. spl_image->load_addr = spl_image->entry_point -
  125. header_size;
  126. spl_image->size = image_get_data_size(header) +
  127. header_size;
  128. }
  129. spl_image->os = image_get_os(header);
  130. spl_image->name = image_get_name(header);
  131. debug("spl: payload image: %.*s load addr: 0x%x size: %d\n",
  132. (int)sizeof(spl_image->name), spl_image->name,
  133. spl_image->load_addr, spl_image->size);
  134. #else
  135. /* LEGACY image not supported */
  136. debug("Legacy boot image support not enabled, proceeding to other boot methods");
  137. return -EINVAL;
  138. #endif
  139. } else {
  140. #ifdef CONFIG_SPL_PANIC_ON_RAW_IMAGE
  141. /*
  142. * CONFIG_SPL_PANIC_ON_RAW_IMAGE is defined when the
  143. * code which loads images in SPL cannot guarantee that
  144. * absolutely all read errors will be reported.
  145. * An example is the LPC32XX MLC NAND driver, which
  146. * will consider that a completely unreadable NAND block
  147. * is bad, and thus should be skipped silently.
  148. */
  149. panic("** no mkimage signature but raw image not supported");
  150. #endif
  151. #ifdef CONFIG_SPL_OS_BOOT
  152. ulong start, end;
  153. if (!bootz_setup((ulong)header, &start, &end)) {
  154. spl_image->name = "Linux";
  155. spl_image->os = IH_OS_LINUX;
  156. spl_image->load_addr = CONFIG_SYS_LOAD_ADDR;
  157. spl_image->entry_point = CONFIG_SYS_LOAD_ADDR;
  158. spl_image->size = end - start;
  159. debug("spl: payload zImage, load addr: 0x%x size: %d\n",
  160. spl_image->load_addr, spl_image->size);
  161. return 0;
  162. }
  163. #endif
  164. #ifdef CONFIG_SPL_RAW_IMAGE_SUPPORT
  165. /* Signature not found - assume u-boot.bin */
  166. debug("mkimage signature not found - ih_magic = %x\n",
  167. header->ih_magic);
  168. spl_set_header_raw_uboot(spl_image);
  169. #else
  170. /* RAW image not supported, proceed to other boot methods. */
  171. debug("Raw boot image support not enabled, proceeding to other boot methods");
  172. return -EINVAL;
  173. #endif
  174. }
  175. return 0;
  176. }
  177. __weak void __noreturn jump_to_image_no_args(struct spl_image_info *spl_image)
  178. {
  179. typedef void __noreturn (*image_entry_noargs_t)(void);
  180. image_entry_noargs_t image_entry =
  181. (image_entry_noargs_t)(unsigned long)spl_image->entry_point;
  182. debug("image entry point: 0x%X\n", spl_image->entry_point);
  183. image_entry();
  184. }
  185. static int spl_common_init(bool setup_malloc)
  186. {
  187. int ret;
  188. debug("spl_early_init()\n");
  189. #if defined(CONFIG_SYS_MALLOC_F_LEN)
  190. if (setup_malloc) {
  191. #ifdef CONFIG_MALLOC_F_ADDR
  192. gd->malloc_base = CONFIG_MALLOC_F_ADDR;
  193. #endif
  194. gd->malloc_limit = CONFIG_SYS_MALLOC_F_LEN;
  195. gd->malloc_ptr = 0;
  196. }
  197. #endif
  198. if (CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)) {
  199. ret = fdtdec_setup();
  200. if (ret) {
  201. debug("fdtdec_setup() returned error %d\n", ret);
  202. return ret;
  203. }
  204. }
  205. if (IS_ENABLED(CONFIG_SPL_DM)) {
  206. /* With CONFIG_OF_PLATDATA, bring in all devices */
  207. ret = dm_init_and_scan(!CONFIG_IS_ENABLED(OF_PLATDATA));
  208. if (ret) {
  209. debug("dm_init_and_scan() returned error %d\n", ret);
  210. return ret;
  211. }
  212. }
  213. return 0;
  214. }
  215. int spl_early_init(void)
  216. {
  217. int ret;
  218. ret = spl_common_init(true);
  219. if (ret)
  220. return ret;
  221. gd->flags |= GD_FLG_SPL_EARLY_INIT;
  222. return 0;
  223. }
  224. int spl_init(void)
  225. {
  226. int ret;
  227. bool setup_malloc = !(IS_ENABLED(CONFIG_SPL_STACK_R) &&
  228. IS_ENABLED(CONFIG_SPL_SYS_MALLOC_SIMPLE));
  229. if (!(gd->flags & GD_FLG_SPL_EARLY_INIT)) {
  230. ret = spl_common_init(setup_malloc);
  231. if (ret)
  232. return ret;
  233. }
  234. gd->flags |= GD_FLG_SPL_INIT;
  235. return 0;
  236. }
  237. #ifndef BOOT_DEVICE_NONE
  238. #define BOOT_DEVICE_NONE 0xdeadbeef
  239. #endif
  240. __weak void board_boot_order(u32 *spl_boot_list)
  241. {
  242. spl_boot_list[0] = spl_boot_device();
  243. }
  244. static struct spl_image_loader *spl_ll_find_loader(uint boot_device)
  245. {
  246. struct spl_image_loader *drv =
  247. ll_entry_start(struct spl_image_loader, spl_image_loader);
  248. const int n_ents =
  249. ll_entry_count(struct spl_image_loader, spl_image_loader);
  250. struct spl_image_loader *entry;
  251. for (entry = drv; entry != drv + n_ents; entry++) {
  252. if (boot_device == entry->boot_device)
  253. return entry;
  254. }
  255. /* Not found */
  256. return NULL;
  257. }
  258. static int spl_load_image(struct spl_image_info *spl_image,
  259. struct spl_image_loader *loader)
  260. {
  261. struct spl_boot_device bootdev;
  262. bootdev.boot_device = loader->boot_device;
  263. bootdev.boot_device_name = NULL;
  264. return loader->load_image(spl_image, &bootdev);
  265. }
  266. /**
  267. * boot_from_devices() - Try loading an booting U-Boot from a list of devices
  268. *
  269. * @spl_image: Place to put the image details if successful
  270. * @spl_boot_list: List of boot devices to try
  271. * @count: Number of elements in spl_boot_list
  272. * @return 0 if OK, -ve on error
  273. */
  274. static int boot_from_devices(struct spl_image_info *spl_image,
  275. u32 spl_boot_list[], int count)
  276. {
  277. int i;
  278. for (i = 0; i < count && spl_boot_list[i] != BOOT_DEVICE_NONE; i++) {
  279. struct spl_image_loader *loader;
  280. loader = spl_ll_find_loader(spl_boot_list[i]);
  281. #if defined(CONFIG_SPL_SERIAL_SUPPORT) && defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
  282. if (loader)
  283. printf("Trying to boot from %s\n", loader->name);
  284. else
  285. puts("SPL: Unsupported Boot Device!\n");
  286. #endif
  287. if (loader && !spl_load_image(spl_image, loader))
  288. return 0;
  289. }
  290. return -ENODEV;
  291. }
  292. #if !(defined(CONFIG_SYS_ICACHE_OFF) && defined(CONFIG_SYS_DCACHE_OFF)) && \
  293. defined(CONFIG_ARM)
  294. static int reserve_mmu(void)
  295. {
  296. phys_addr_t ram_top = 0;
  297. /* reserve TLB table */
  298. gd->arch.tlb_size = PGTABLE_SIZE;
  299. #ifdef CONFIG_SYS_SDRAM_BASE
  300. ram_top = CONFIG_SYS_SDRAM_BASE;
  301. #endif
  302. ram_top += get_effective_memsize();
  303. gd->arch.tlb_addr = ram_top - gd->arch.tlb_size;
  304. debug("TLB table from %08lx to %08lx\n", gd->arch.tlb_addr,
  305. gd->arch.tlb_addr + gd->arch.tlb_size);
  306. return 0;
  307. }
  308. __weak void dram_init_banksize(void)
  309. {
  310. #if defined(CONFIG_NR_DRAM_BANKS) && defined(CONFIG_SYS_SDRAM_BASE)
  311. gd->bd->bi_dram[0].start = CONFIG_SYS_SDRAM_BASE;
  312. gd->bd->bi_dram[0].size = get_effective_memsize();
  313. #endif
  314. }
  315. #endif
  316. void board_init_r(gd_t *dummy1, ulong dummy2)
  317. {
  318. u32 spl_boot_list[] = {
  319. BOOT_DEVICE_NONE,
  320. BOOT_DEVICE_NONE,
  321. BOOT_DEVICE_NONE,
  322. BOOT_DEVICE_NONE,
  323. BOOT_DEVICE_NONE,
  324. };
  325. struct spl_image_info spl_image;
  326. debug(">>spl:board_init_r()\n");
  327. gd->bd = &bdata;
  328. #if !(defined(CONFIG_SYS_ICACHE_OFF) && defined(CONFIG_SYS_DCACHE_OFF)) && \
  329. defined(CONFIG_ARM)
  330. dram_init_banksize();
  331. reserve_mmu();
  332. enable_caches();
  333. #endif
  334. #if defined(CONFIG_SYS_SPL_MALLOC_START)
  335. mem_malloc_init(CONFIG_SYS_SPL_MALLOC_START,
  336. CONFIG_SYS_SPL_MALLOC_SIZE);
  337. gd->flags |= GD_FLG_FULL_MALLOC_INIT;
  338. #endif
  339. if (!(gd->flags & GD_FLG_SPL_INIT)) {
  340. if (spl_init())
  341. hang();
  342. }
  343. #ifndef CONFIG_PPC
  344. /*
  345. * timer_init() does not exist on PPC systems. The timer is initialized
  346. * and enabled (decrementer) in interrupt_init() here.
  347. */
  348. timer_init();
  349. #endif
  350. #ifdef CONFIG_SPL_BOARD_INIT
  351. spl_board_init();
  352. #endif
  353. memset(&spl_image, '\0', sizeof(spl_image));
  354. board_boot_order(spl_boot_list);
  355. if (boot_from_devices(&spl_image, spl_boot_list,
  356. ARRAY_SIZE(spl_boot_list))) {
  357. puts("SPL: failed to boot from all boot devices\n");
  358. hang();
  359. }
  360. #if !(defined(CONFIG_SYS_ICACHE_OFF) && defined(CONFIG_SYS_DCACHE_OFF)) && \
  361. defined(CONFIG_ARM)
  362. cleanup_before_linux();
  363. #endif
  364. switch (spl_image.os) {
  365. case IH_OS_U_BOOT:
  366. debug("Jumping to U-Boot\n");
  367. break;
  368. #ifdef CONFIG_SPL_OS_BOOT
  369. case IH_OS_LINUX:
  370. debug("Jumping to Linux\n");
  371. spl_fixup_fdt();
  372. spl_board_prepare_for_linux();
  373. jump_to_image_linux(&spl_image,
  374. (void *)CONFIG_SYS_SPL_ARGS_ADDR);
  375. #endif
  376. default:
  377. debug("Unsupported OS image.. Jumping nevertheless..\n");
  378. }
  379. #if defined(CONFIG_SYS_MALLOC_F_LEN) && !defined(CONFIG_SYS_SPL_MALLOC_SIZE)
  380. debug("SPL malloc() used %#lx bytes (%ld KB)\n", gd->malloc_ptr,
  381. gd->malloc_ptr / 1024);
  382. #endif
  383. debug("loaded - jumping to U-Boot...\n");
  384. spl_board_prepare_for_boot();
  385. jump_to_image_no_args(&spl_image);
  386. }
  387. /*
  388. * This requires UART clocks to be enabled. In order for this to work the
  389. * caller must ensure that the gd pointer is valid.
  390. */
  391. void preloader_console_init(void)
  392. {
  393. gd->baudrate = CONFIG_BAUDRATE;
  394. serial_init(); /* serial communications setup */
  395. gd->have_console = 1;
  396. puts("\nU-Boot SPL " PLAIN_VERSION " (" U_BOOT_DATE " - " \
  397. U_BOOT_TIME ")\n");
  398. #ifdef CONFIG_SPL_DISPLAY_PRINT
  399. spl_display_print();
  400. #endif
  401. }
  402. /**
  403. * spl_relocate_stack_gd() - Relocate stack ready for board_init_r() execution
  404. *
  405. * Sometimes board_init_f() runs with a stack in SRAM but we want to use SDRAM
  406. * for the main board_init_r() execution. This is typically because we need
  407. * more stack space for things like the MMC sub-system.
  408. *
  409. * This function calculates the stack position, copies the global_data into
  410. * place, sets the new gd (except for ARM, for which setting GD within a C
  411. * function may not always work) and returns the new stack position. The
  412. * caller is responsible for setting up the sp register and, in the case
  413. * of ARM, setting up gd.
  414. *
  415. * All of this is done using the same layout and alignments as done in
  416. * board_init_f_init_reserve() / board_init_f_alloc_reserve().
  417. *
  418. * @return new stack location, or 0 to use the same stack
  419. */
  420. ulong spl_relocate_stack_gd(void)
  421. {
  422. #ifdef CONFIG_SPL_STACK_R
  423. gd_t *new_gd;
  424. ulong ptr = CONFIG_SPL_STACK_R_ADDR;
  425. #ifdef CONFIG_SPL_SYS_MALLOC_SIMPLE
  426. if (CONFIG_SPL_STACK_R_MALLOC_SIMPLE_LEN) {
  427. ptr -= CONFIG_SPL_STACK_R_MALLOC_SIMPLE_LEN;
  428. gd->malloc_base = ptr;
  429. gd->malloc_limit = CONFIG_SPL_STACK_R_MALLOC_SIMPLE_LEN;
  430. gd->malloc_ptr = 0;
  431. }
  432. #endif
  433. /* Get stack position: use 8-byte alignment for ABI compliance */
  434. ptr = CONFIG_SPL_STACK_R_ADDR - roundup(sizeof(gd_t),16);
  435. new_gd = (gd_t *)ptr;
  436. memcpy(new_gd, (void *)gd, sizeof(gd_t));
  437. #if CONFIG_IS_ENABLED(DM)
  438. dm_fixup_for_gd_move(new_gd);
  439. #endif
  440. #if !defined(CONFIG_ARM)
  441. gd = new_gd;
  442. #endif
  443. return ptr;
  444. #else
  445. return 0;
  446. #endif
  447. }