3 * Texas Instruments, <www.ti.com>
5 * Aneesh V <aneesh@ti.com>
7 * SPDX-License-Identifier: GPL-2.0+
12 #include <asm/u-boot.h>
20 #include <linux/compiler.h>
22 DECLARE_GLOBAL_DATA_PTR;
24 #ifndef CONFIG_SYS_UBOOT_START
25 #define CONFIG_SYS_UBOOT_START CONFIG_SYS_TEXT_BASE
27 #ifndef CONFIG_SYS_MONITOR_LEN
28 /* Unknown U-Boot size, let's assume it will not be more than 200 KB */
29 #define CONFIG_SYS_MONITOR_LEN (200 * 1024)
32 u32 *boot_params_ptr = NULL;
33 struct spl_image_info spl_image;
35 /* Define board data structure */
36 static bd_t bdata __attribute__ ((section(".data")));
39 * Default function to determine if u-boot or the OS should
40 * be started. This implementation always returns 1.
42 * Please implement your own board specific funcion to do this.
45 * 0 to not start u-boot
46 * positive if u-boot should start
48 #ifdef CONFIG_SPL_OS_BOOT
49 __weak int spl_start_uboot(void)
51 puts("SPL: Please implement spl_start_uboot() for your board\n");
52 puts("SPL: Direct Linux boot not active!\n");
58 * Weak default function for board specific cleanup/preparation before
59 * Linux boot. Some boards/platforms might not need it, so just provide
62 __weak void spl_board_prepare_for_linux(void)
67 __weak void spl_board_prepare_for_boot(void)
72 void spl_set_header_raw_uboot(void)
74 spl_image.size = CONFIG_SYS_MONITOR_LEN;
75 spl_image.entry_point = CONFIG_SYS_UBOOT_START;
76 spl_image.load_addr = CONFIG_SYS_TEXT_BASE;
77 spl_image.os = IH_OS_U_BOOT;
78 spl_image.name = "U-Boot";
81 int spl_parse_image_header(const struct image_header *header)
83 u32 header_size = sizeof(struct image_header);
85 if (image_get_magic(header) == IH_MAGIC) {
86 if (spl_image.flags & SPL_COPY_PAYLOAD_ONLY) {
88 * On some system (e.g. powerpc), the load-address and
89 * entry-point is located at address 0. We can't load
90 * to 0-0x40. So skip header in this case.
92 spl_image.load_addr = image_get_load(header);
93 spl_image.entry_point = image_get_ep(header);
94 spl_image.size = image_get_data_size(header);
96 spl_image.entry_point = image_get_load(header);
97 /* Load including the header */
98 spl_image.load_addr = spl_image.entry_point -
100 spl_image.size = image_get_data_size(header) +
103 spl_image.os = image_get_os(header);
104 spl_image.name = image_get_name(header);
105 debug("spl: payload image: %.*s load addr: 0x%x size: %d\n",
106 (int)sizeof(spl_image.name), spl_image.name,
107 spl_image.load_addr, spl_image.size);
109 #ifdef CONFIG_SPL_PANIC_ON_RAW_IMAGE
111 * CONFIG_SPL_PANIC_ON_RAW_IMAGE is defined when the
112 * code which loads images in SPL cannot guarantee that
113 * absolutely all read errors will be reported.
114 * An example is the LPC32XX MLC NAND driver, which
115 * will consider that a completely unreadable NAND block
116 * is bad, and thus should be skipped silently.
118 panic("** no mkimage signature but raw image not supported");
119 #elif defined(CONFIG_SPL_ABORT_ON_RAW_IMAGE)
120 /* Signature not found, proceed to other boot methods. */
123 /* Signature not found - assume u-boot.bin */
124 debug("mkimage signature not found - ih_magic = %x\n",
126 spl_set_header_raw_uboot();
132 __weak void __noreturn jump_to_image_no_args(struct spl_image_info *spl_image)
134 typedef void __noreturn (*image_entry_noargs_t)(void);
136 image_entry_noargs_t image_entry =
137 (image_entry_noargs_t)(unsigned long)spl_image->entry_point;
139 debug("image entry point: 0x%X\n", spl_image->entry_point);
143 #ifdef CONFIG_SPL_RAM_DEVICE
144 static int spl_ram_load_image(void)
146 const struct image_header *header;
149 * Get the header. It will point to an address defined by handoff
150 * which will tell where the image located inside the flash. For
151 * now, it will temporary fixed to address pointed by U-Boot.
153 header = (struct image_header *)
154 (CONFIG_SYS_TEXT_BASE - sizeof(struct image_header));
156 spl_parse_image_header(header);
166 debug("spl_init()\n");
167 #if defined(CONFIG_SYS_MALLOC_F_LEN)
168 gd->malloc_limit = CONFIG_SYS_MALLOC_F_LEN;
171 if (CONFIG_IS_ENABLED(OF_CONTROL)) {
172 ret = fdtdec_setup();
174 debug("fdtdec_setup() returned error %d\n", ret);
178 if (IS_ENABLED(CONFIG_SPL_DM)) {
179 ret = dm_init_and_scan(true);
181 debug("dm_init_and_scan() returned error %d\n", ret);
185 gd->flags |= GD_FLG_SPL_INIT;
190 #ifndef BOOT_DEVICE_NONE
191 #define BOOT_DEVICE_NONE 0xdeadbeef
194 static u32 spl_boot_list[] = {
202 __weak void board_boot_order(u32 *spl_boot_list)
204 spl_boot_list[0] = spl_boot_device();
207 #ifdef CONFIG_SPL_BOARD_LOAD_IMAGE
208 __weak void spl_board_announce_boot_device(void) { }
211 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
212 struct boot_device_name {
217 struct boot_device_name boot_name_table[] = {
218 #ifdef CONFIG_SPL_RAM_DEVICE
219 { BOOT_DEVICE_RAM, "RAM" },
221 #ifdef CONFIG_SPL_MMC_SUPPORT
222 { BOOT_DEVICE_MMC1, "MMC1" },
223 { BOOT_DEVICE_MMC2, "MMC2" },
224 { BOOT_DEVICE_MMC2_2, "MMC2_2" },
226 #ifdef CONFIG_SPL_NAND_SUPPORT
227 { BOOT_DEVICE_NAND, "NAND" },
229 #ifdef CONFIG_SPL_ONENAND_SUPPORT
230 { BOOT_DEVICE_ONENAND, "OneNAND" },
232 #ifdef CONFIG_SPL_NOR_SUPPORT
233 { BOOT_DEVICE_NOR, "NOR" },
235 #ifdef CONFIG_SPL_YMODEM_SUPPORT
236 { BOOT_DEVICE_UART, "UART" },
238 #ifdef CONFIG_SPL_SPI_SUPPORT
239 { BOOT_DEVICE_SPI, "SPI" },
241 #ifdef CONFIG_SPL_ETH_SUPPORT
242 #ifdef CONFIG_SPL_ETH_DEVICE
243 { BOOT_DEVICE_CPGMAC, "eth device" },
245 { BOOT_DEVICE_CPGMAC, "net" },
248 #ifdef CONFIG_SPL_USBETH_SUPPORT
249 { BOOT_DEVICE_USBETH, "USB eth" },
251 #ifdef CONFIG_SPL_USB_SUPPORT
252 { BOOT_DEVICE_USB, "USB" },
254 #ifdef CONFIG_SPL_SATA_SUPPORT
255 { BOOT_DEVICE_SATA, "SATA" },
257 /* Keep this entry last */
258 { BOOT_DEVICE_NONE, "unknown boot device" },
261 static void announce_boot_device(u32 boot_device)
265 puts("Trying to boot from ");
267 #ifdef CONFIG_SPL_BOARD_LOAD_IMAGE
268 if (boot_device == BOOT_DEVICE_BOARD) {
269 spl_board_announce_boot_device();
274 for (i = 0; i < ARRAY_SIZE(boot_name_table) - 1; i++) {
275 if (boot_name_table[i].boot_dev == boot_device)
279 printf("%s\n", boot_name_table[i].name);
282 static inline void announce_boot_device(u32 boot_device) { }
285 static int spl_load_image(u32 boot_device)
287 switch (boot_device) {
288 #ifdef CONFIG_SPL_RAM_DEVICE
289 case BOOT_DEVICE_RAM:
290 return spl_ram_load_image();
292 #ifdef CONFIG_SPL_MMC_SUPPORT
293 case BOOT_DEVICE_MMC1:
294 case BOOT_DEVICE_MMC2:
295 case BOOT_DEVICE_MMC2_2:
296 return spl_mmc_load_image(boot_device);
298 #ifdef CONFIG_SPL_NAND_SUPPORT
299 case BOOT_DEVICE_NAND:
300 return spl_nand_load_image();
302 #ifdef CONFIG_SPL_ONENAND_SUPPORT
303 case BOOT_DEVICE_ONENAND:
304 return spl_onenand_load_image();
306 #ifdef CONFIG_SPL_NOR_SUPPORT
307 case BOOT_DEVICE_NOR:
308 return spl_nor_load_image();
310 #ifdef CONFIG_SPL_YMODEM_SUPPORT
311 case BOOT_DEVICE_UART:
312 return spl_ymodem_load_image();
314 #ifdef CONFIG_SPL_SPI_SUPPORT
315 case BOOT_DEVICE_SPI:
316 return spl_spi_load_image();
318 #ifdef CONFIG_SPL_ETH_SUPPORT
319 case BOOT_DEVICE_CPGMAC:
320 #ifdef CONFIG_SPL_ETH_DEVICE
321 return spl_net_load_image(CONFIG_SPL_ETH_DEVICE);
323 return spl_net_load_image(NULL);
326 #ifdef CONFIG_SPL_USBETH_SUPPORT
327 case BOOT_DEVICE_USBETH:
328 return spl_net_load_image("usb_ether");
330 #ifdef CONFIG_SPL_USB_SUPPORT
331 case BOOT_DEVICE_USB:
332 return spl_usb_load_image();
334 #ifdef CONFIG_SPL_SATA_SUPPORT
335 case BOOT_DEVICE_SATA:
336 return spl_sata_load_image();
338 #ifdef CONFIG_SPL_BOARD_LOAD_IMAGE
339 case BOOT_DEVICE_BOARD:
340 return spl_board_load_image();
343 #if defined(CONFIG_SPL_SERIAL_SUPPORT) && defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
344 puts("SPL: Unsupported Boot Device!\n");
352 void board_init_r(gd_t *dummy1, ulong dummy2)
356 debug(">>spl:board_init_r()\n");
358 #if defined(CONFIG_SYS_SPL_MALLOC_START)
359 mem_malloc_init(CONFIG_SYS_SPL_MALLOC_START,
360 CONFIG_SYS_SPL_MALLOC_SIZE);
361 gd->flags |= GD_FLG_FULL_MALLOC_INIT;
363 if (!(gd->flags & GD_FLG_SPL_INIT)) {
369 * timer_init() does not exist on PPC systems. The timer is initialized
370 * and enabled (decrementer) in interrupt_init() here.
375 #ifdef CONFIG_SPL_BOARD_INIT
379 board_boot_order(spl_boot_list);
380 for (i = 0; i < ARRAY_SIZE(spl_boot_list) &&
381 spl_boot_list[i] != BOOT_DEVICE_NONE; i++) {
382 announce_boot_device(spl_boot_list[i]);
383 if (!spl_load_image(spl_boot_list[i]))
387 if (i == ARRAY_SIZE(spl_boot_list) ||
388 spl_boot_list[i] == BOOT_DEVICE_NONE) {
389 puts("SPL: failed to boot from all boot devices\n");
393 switch (spl_image.os) {
395 debug("Jumping to U-Boot\n");
397 #ifdef CONFIG_SPL_OS_BOOT
399 debug("Jumping to Linux\n");
400 spl_board_prepare_for_linux();
401 jump_to_image_linux((void *)CONFIG_SYS_SPL_ARGS_ADDR);
404 debug("Unsupported OS image.. Jumping nevertheless..\n");
406 #if defined(CONFIG_SYS_MALLOC_F_LEN) && !defined(CONFIG_SYS_SPL_MALLOC_SIZE)
407 debug("SPL malloc() used %#lx bytes (%ld KB)\n", gd->malloc_ptr,
408 gd->malloc_ptr / 1024);
411 debug("loaded - jumping to U-Boot...");
412 spl_board_prepare_for_boot();
413 jump_to_image_no_args(&spl_image);
417 * This requires UART clocks to be enabled. In order for this to work the
418 * caller must ensure that the gd pointer is valid.
420 void preloader_console_init(void)
423 gd->baudrate = CONFIG_BAUDRATE;
425 serial_init(); /* serial communications setup */
427 gd->have_console = 1;
429 puts("\nU-Boot SPL " PLAIN_VERSION " (" U_BOOT_DATE " - " \
431 #ifdef CONFIG_SPL_DISPLAY_PRINT
437 * spl_relocate_stack_gd() - Relocate stack ready for board_init_r() execution
439 * Sometimes board_init_f() runs with a stack in SRAM but we want to use SDRAM
440 * for the main board_init_r() execution. This is typically because we need
441 * more stack space for things like the MMC sub-system.
443 * This function calculates the stack position, copies the global_data into
444 * place, sets the new gd (except for ARM, for which setting GD within a C
445 * function may not always work) and returns the new stack position. The
446 * caller is responsible for setting up the sp register and, in the case
447 * of ARM, setting up gd.
449 * All of this is done using the same layout and alignments as done in
450 * board_init_f_init_reserve() / board_init_f_alloc_reserve().
452 * @return new stack location, or 0 to use the same stack
454 ulong spl_relocate_stack_gd(void)
456 #ifdef CONFIG_SPL_STACK_R
458 ulong ptr = CONFIG_SPL_STACK_R_ADDR;
460 #ifdef CONFIG_SPL_SYS_MALLOC_SIMPLE
461 if (CONFIG_SPL_STACK_R_MALLOC_SIMPLE_LEN) {
462 if (!(gd->flags & GD_FLG_SPL_INIT))
463 panic_str("spl_init must be called before heap reloc");
465 ptr -= CONFIG_SPL_STACK_R_MALLOC_SIMPLE_LEN;
466 gd->malloc_base = ptr;
467 gd->malloc_limit = CONFIG_SPL_STACK_R_MALLOC_SIMPLE_LEN;
471 /* Get stack position: use 8-byte alignment for ABI compliance */
472 ptr = CONFIG_SPL_STACK_R_ADDR - roundup(sizeof(gd_t),16);
473 new_gd = (gd_t *)ptr;
474 memcpy(new_gd, (void *)gd, sizeof(gd_t));
475 #if !defined(CONFIG_ARM)