1 // SPDX-License-Identifier: GPL-2.0+
4 * Texas Instruments, <www.ti.com>
6 * Aneesh V <aneesh@ti.com>
10 #include <binman_sym.h>
13 #include <asm/u-boot.h>
20 #include <linux/compiler.h>
21 #include <fdt_support.h>
23 DECLARE_GLOBAL_DATA_PTR;
25 #ifndef CONFIG_SYS_UBOOT_START
26 #define CONFIG_SYS_UBOOT_START CONFIG_SYS_TEXT_BASE
28 #ifndef CONFIG_SYS_MONITOR_LEN
29 /* Unknown U-Boot size, let's assume it will not be more than 200 KB */
30 #define CONFIG_SYS_MONITOR_LEN (200 * 1024)
33 u32 *boot_params_ptr = NULL;
35 /* See spl.h for information about this */
36 binman_sym_declare(ulong, u_boot_any, pos);
38 /* Define board data structure */
39 static bd_t bdata __attribute__ ((section(".data")));
42 * Board-specific Platform code can reimplement show_boot_progress () if needed
44 __weak void show_boot_progress(int val) {}
47 * Default function to determine if u-boot or the OS should
48 * be started. This implementation always returns 1.
50 * Please implement your own board specific funcion to do this.
53 * 0 to not start u-boot
54 * positive if u-boot should start
56 #ifdef CONFIG_SPL_OS_BOOT
57 __weak int spl_start_uboot(void)
59 puts("SPL: Please implement spl_start_uboot() for your board\n");
60 puts("SPL: Direct Linux boot not active!\n");
64 /* weak default platform specific function to initialize
67 __weak int dram_init_banksize(void)
73 * Weak default function for arch specific zImage check. Return zero
74 * and fill start and end address if image is recognized.
76 int __weak bootz_setup(ulong image, ulong *start, ulong *end)
82 void spl_fixup_fdt(void)
84 #if defined(CONFIG_SPL_OF_LIBFDT) && defined(CONFIG_SYS_SPL_ARGS_ADDR)
85 void *fdt_blob = (void *)CONFIG_SYS_SPL_ARGS_ADDR;
88 err = fdt_check_header(fdt_blob);
90 printf("fdt_root: %s\n", fdt_strerror(err));
94 /* fixup the memory dt node */
95 err = fdt_shrink_to_minimum(fdt_blob, 0);
97 printf("spl: fdt_shrink_to_minimum err - %d\n", err);
101 err = arch_fixup_fdt(fdt_blob);
103 printf("spl: arch_fixup_fdt err - %d\n", err);
110 * Weak default function for board specific cleanup/preparation before
111 * Linux boot. Some boards/platforms might not need it, so just provide
112 * an empty stub here.
114 __weak void spl_board_prepare_for_linux(void)
119 __weak void spl_board_prepare_for_boot(void)
124 void spl_set_header_raw_uboot(struct spl_image_info *spl_image)
126 ulong u_boot_pos = binman_sym(ulong, u_boot_any, pos);
128 spl_image->size = CONFIG_SYS_MONITOR_LEN;
131 * Binman error cases: address of the end of the previous region or the
132 * start of the image's entry area (usually 0) if there is no previous
135 if (u_boot_pos && u_boot_pos != BINMAN_SYM_MISSING) {
136 /* Binman does not support separated entry addresses */
137 spl_image->entry_point = u_boot_pos;
138 spl_image->load_addr = u_boot_pos;
140 spl_image->entry_point = CONFIG_SYS_UBOOT_START;
141 spl_image->load_addr = CONFIG_SYS_TEXT_BASE;
143 spl_image->os = IH_OS_U_BOOT;
144 spl_image->name = "U-Boot";
147 int spl_parse_image_header(struct spl_image_info *spl_image,
148 const struct image_header *header)
150 if (image_get_magic(header) == IH_MAGIC) {
151 #ifdef CONFIG_SPL_LEGACY_IMAGE_SUPPORT
152 u32 header_size = sizeof(struct image_header);
154 if (spl_image->flags & SPL_COPY_PAYLOAD_ONLY) {
156 * On some system (e.g. powerpc), the load-address and
157 * entry-point is located at address 0. We can't load
158 * to 0-0x40. So skip header in this case.
160 spl_image->load_addr = image_get_load(header);
161 spl_image->entry_point = image_get_ep(header);
162 spl_image->size = image_get_data_size(header);
164 spl_image->entry_point = image_get_load(header);
165 /* Load including the header */
166 spl_image->load_addr = spl_image->entry_point -
168 spl_image->size = image_get_data_size(header) +
171 spl_image->os = image_get_os(header);
172 spl_image->name = image_get_name(header);
173 debug("spl: payload image: %.*s load addr: 0x%lx size: %d\n",
174 IH_NMLEN, spl_image->name,
175 spl_image->load_addr, spl_image->size);
177 /* LEGACY image not supported */
178 debug("Legacy boot image support not enabled, proceeding to other boot methods\n");
182 #ifdef CONFIG_SPL_PANIC_ON_RAW_IMAGE
184 * CONFIG_SPL_PANIC_ON_RAW_IMAGE is defined when the
185 * code which loads images in SPL cannot guarantee that
186 * absolutely all read errors will be reported.
187 * An example is the LPC32XX MLC NAND driver, which
188 * will consider that a completely unreadable NAND block
189 * is bad, and thus should be skipped silently.
191 panic("** no mkimage signature but raw image not supported");
194 #ifdef CONFIG_SPL_OS_BOOT
197 if (!bootz_setup((ulong)header, &start, &end)) {
198 spl_image->name = "Linux";
199 spl_image->os = IH_OS_LINUX;
200 spl_image->load_addr = CONFIG_SYS_LOAD_ADDR;
201 spl_image->entry_point = CONFIG_SYS_LOAD_ADDR;
202 spl_image->size = end - start;
203 debug("spl: payload zImage, load addr: 0x%lx size: %d\n",
204 spl_image->load_addr, spl_image->size);
209 #ifdef CONFIG_SPL_RAW_IMAGE_SUPPORT
210 /* Signature not found - assume u-boot.bin */
211 debug("mkimage signature not found - ih_magic = %x\n",
213 spl_set_header_raw_uboot(spl_image);
215 /* RAW image not supported, proceed to other boot methods. */
216 debug("Raw boot image support not enabled, proceeding to other boot methods\n");
224 __weak void __noreturn jump_to_image_no_args(struct spl_image_info *spl_image)
226 typedef void __noreturn (*image_entry_noargs_t)(void);
228 image_entry_noargs_t image_entry =
229 (image_entry_noargs_t)spl_image->entry_point;
231 debug("image entry point: 0x%lX\n", spl_image->entry_point);
235 static int spl_common_init(bool setup_malloc)
239 debug("spl_early_init()\n");
241 #if CONFIG_VAL(SYS_MALLOC_F_LEN)
243 #ifdef CONFIG_MALLOC_F_ADDR
244 gd->malloc_base = CONFIG_MALLOC_F_ADDR;
246 gd->malloc_limit = CONFIG_VAL(SYS_MALLOC_F_LEN);
250 ret = bootstage_init(true);
252 debug("%s: Failed to set up bootstage: ret=%d\n", __func__,
256 bootstage_mark_name(BOOTSTAGE_ID_START_SPL, "spl");
257 if (CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)) {
258 ret = fdtdec_setup();
260 debug("fdtdec_setup() returned error %d\n", ret);
264 if (CONFIG_IS_ENABLED(DM)) {
265 bootstage_start(BOOTSTATE_ID_ACCUM_DM_SPL, "dm_spl");
266 /* With CONFIG_SPL_OF_PLATDATA, bring in all devices */
267 ret = dm_init_and_scan(!CONFIG_IS_ENABLED(OF_PLATDATA));
268 bootstage_accum(BOOTSTATE_ID_ACCUM_DM_SPL);
270 debug("dm_init_and_scan() returned error %d\n", ret);
278 void spl_set_bd(void)
284 int spl_early_init(void)
288 ret = spl_common_init(true);
291 gd->flags |= GD_FLG_SPL_EARLY_INIT;
299 bool setup_malloc = !(IS_ENABLED(CONFIG_SPL_STACK_R) &&
300 IS_ENABLED(CONFIG_SPL_SYS_MALLOC_SIMPLE));
302 if (!(gd->flags & GD_FLG_SPL_EARLY_INIT)) {
303 ret = spl_common_init(setup_malloc);
307 gd->flags |= GD_FLG_SPL_INIT;
312 #ifndef BOOT_DEVICE_NONE
313 #define BOOT_DEVICE_NONE 0xdeadbeef
316 __weak void board_boot_order(u32 *spl_boot_list)
318 spl_boot_list[0] = spl_boot_device();
321 static struct spl_image_loader *spl_ll_find_loader(uint boot_device)
323 struct spl_image_loader *drv =
324 ll_entry_start(struct spl_image_loader, spl_image_loader);
326 ll_entry_count(struct spl_image_loader, spl_image_loader);
327 struct spl_image_loader *entry;
329 for (entry = drv; entry != drv + n_ents; entry++) {
330 if (boot_device == entry->boot_device)
338 static int spl_load_image(struct spl_image_info *spl_image,
339 struct spl_image_loader *loader)
341 struct spl_boot_device bootdev;
343 bootdev.boot_device = loader->boot_device;
344 bootdev.boot_device_name = NULL;
346 return loader->load_image(spl_image, &bootdev);
350 * boot_from_devices() - Try loading an booting U-Boot from a list of devices
352 * @spl_image: Place to put the image details if successful
353 * @spl_boot_list: List of boot devices to try
354 * @count: Number of elements in spl_boot_list
355 * @return 0 if OK, -ve on error
357 static int boot_from_devices(struct spl_image_info *spl_image,
358 u32 spl_boot_list[], int count)
362 for (i = 0; i < count && spl_boot_list[i] != BOOT_DEVICE_NONE; i++) {
363 struct spl_image_loader *loader;
365 loader = spl_ll_find_loader(spl_boot_list[i]);
366 #if defined(CONFIG_SPL_SERIAL_SUPPORT) && defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
368 printf("Trying to boot from %s\n", loader->name);
370 puts("SPL: Unsupported Boot Device!\n");
372 if (loader && !spl_load_image(spl_image, loader))
379 void board_init_r(gd_t *dummy1, ulong dummy2)
381 u32 spl_boot_list[] = {
388 struct spl_image_info spl_image;
390 debug(">>spl:board_init_r()\n");
394 #ifdef CONFIG_SPL_OS_BOOT
395 dram_init_banksize();
398 #if defined(CONFIG_SYS_SPL_MALLOC_START)
399 mem_malloc_init(CONFIG_SYS_SPL_MALLOC_START,
400 CONFIG_SYS_SPL_MALLOC_SIZE);
401 gd->flags |= GD_FLG_FULL_MALLOC_INIT;
403 if (!(gd->flags & GD_FLG_SPL_INIT)) {
407 #if !defined(CONFIG_PPC) && !defined(CONFIG_ARCH_MX6)
409 * timer_init() does not exist on PPC systems. The timer is initialized
410 * and enabled (decrementer) in interrupt_init() here.
415 #if CONFIG_IS_ENABLED(BOARD_INIT)
419 memset(&spl_image, '\0', sizeof(spl_image));
420 #ifdef CONFIG_SYS_SPL_ARGS_ADDR
421 spl_image.arg = (void *)CONFIG_SYS_SPL_ARGS_ADDR;
423 board_boot_order(spl_boot_list);
425 if (boot_from_devices(&spl_image, spl_boot_list,
426 ARRAY_SIZE(spl_boot_list))) {
427 puts("SPL: failed to boot from all boot devices\n");
431 #ifdef CONFIG_CPU_V7M
432 spl_image.entry_point |= 0x1;
434 switch (spl_image.os) {
436 debug("Jumping to U-Boot\n");
438 #if CONFIG_IS_ENABLED(ATF)
439 case IH_OS_ARM_TRUSTED_FIRMWARE:
440 debug("Jumping to U-Boot via ARM Trusted Firmware\n");
441 spl_invoke_atf(&spl_image);
444 #ifdef CONFIG_SPL_OS_BOOT
446 debug("Jumping to Linux\n");
448 spl_board_prepare_for_linux();
449 jump_to_image_linux(&spl_image);
452 debug("Unsupported OS image.. Jumping nevertheless..\n");
454 #if CONFIG_VAL(SYS_MALLOC_F_LEN) && !defined(CONFIG_SYS_SPL_MALLOC_SIZE)
455 debug("SPL malloc() used %#lx bytes (%ld KB)\n", gd->malloc_ptr,
456 gd->malloc_ptr / 1024);
458 #ifdef CONFIG_BOOTSTAGE_STASH
461 bootstage_mark_name(BOOTSTAGE_ID_END_SPL, "end_spl");
462 ret = bootstage_stash((void *)CONFIG_BOOTSTAGE_STASH_ADDR,
463 CONFIG_BOOTSTAGE_STASH_SIZE);
465 debug("Failed to stash bootstage: err=%d\n", ret);
468 debug("loaded - jumping to U-Boot...\n");
469 spl_board_prepare_for_boot();
470 jump_to_image_no_args(&spl_image);
473 #ifdef CONFIG_SPL_SERIAL_SUPPORT
475 * This requires UART clocks to be enabled. In order for this to work the
476 * caller must ensure that the gd pointer is valid.
478 void preloader_console_init(void)
480 gd->baudrate = CONFIG_BAUDRATE;
482 serial_init(); /* serial communications setup */
484 gd->have_console = 1;
486 #ifndef CONFIG_SPL_DISABLE_BANNER_PRINT
487 puts("\nU-Boot SPL " PLAIN_VERSION " (" U_BOOT_DATE " - " \
488 U_BOOT_TIME " " U_BOOT_TZ ")\n");
490 #ifdef CONFIG_SPL_DISPLAY_PRINT
497 * spl_relocate_stack_gd() - Relocate stack ready for board_init_r() execution
499 * Sometimes board_init_f() runs with a stack in SRAM but we want to use SDRAM
500 * for the main board_init_r() execution. This is typically because we need
501 * more stack space for things like the MMC sub-system.
503 * This function calculates the stack position, copies the global_data into
504 * place, sets the new gd (except for ARM, for which setting GD within a C
505 * function may not always work) and returns the new stack position. The
506 * caller is responsible for setting up the sp register and, in the case
507 * of ARM, setting up gd.
509 * All of this is done using the same layout and alignments as done in
510 * board_init_f_init_reserve() / board_init_f_alloc_reserve().
512 * @return new stack location, or 0 to use the same stack
514 ulong spl_relocate_stack_gd(void)
516 #ifdef CONFIG_SPL_STACK_R
518 ulong ptr = CONFIG_SPL_STACK_R_ADDR;
520 #if defined(CONFIG_SPL_SYS_MALLOC_SIMPLE) && CONFIG_VAL(SYS_MALLOC_F_LEN)
521 if (CONFIG_SPL_STACK_R_MALLOC_SIMPLE_LEN) {
522 ptr -= CONFIG_SPL_STACK_R_MALLOC_SIMPLE_LEN;
523 gd->malloc_base = ptr;
524 gd->malloc_limit = CONFIG_SPL_STACK_R_MALLOC_SIMPLE_LEN;
528 /* Get stack position: use 8-byte alignment for ABI compliance */
529 ptr = CONFIG_SPL_STACK_R_ADDR - roundup(sizeof(gd_t),16);
530 new_gd = (gd_t *)ptr;
531 memcpy(new_gd, (void *)gd, sizeof(gd_t));
532 #if CONFIG_IS_ENABLED(DM)
533 dm_fixup_for_gd_move(new_gd);
535 #if !defined(CONFIG_ARM)