1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2011 The Chromium OS Authors.
4 * (C) Copyright 2002-2006
8 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
14 #include <bootstage.h>
17 #include <display_options.h>
19 #ifdef CONFIG_MTD_NOR_FLASH
27 #include <asm/cache.h>
28 #include <asm/global_data.h>
29 #include <u-boot/crc.h>
35 #include <env_internal.h>
49 #include <onenand_uboot.h>
53 #include <status_led.h>
54 #include <stdio_dev.h>
59 #include <asm/sections.h>
61 #include <dm/ofnode.h>
62 #include <linux/compiler.h>
63 #include <linux/err.h>
64 #include <efi_loader.h>
66 #include <asm-generic/gpio.h>
67 #include <efi_loader.h>
70 DECLARE_GLOBAL_DATA_PTR;
72 ulong monitor_flash_len;
74 __weak int board_flash_wp_on(void)
77 * Most flashes can't be detected when write protection is enabled,
78 * so provide a way to let U-Boot gracefully ignore write protected
84 __weak int cpu_secondary_init_r(void)
89 static int initr_trace(void)
92 trace_init(gd->trace_buff, CONFIG_TRACE_BUFFER_SIZE);
98 static int initr_reloc(void)
100 /* tell others: relocation done */
101 gd->flags |= GD_FLG_RELOC | GD_FLG_FULL_MALLOC_INIT;
106 #if defined(CONFIG_ARM) || defined(CONFIG_RISCV)
108 * Some of these functions are needed purely because the functions they
109 * call return void. If we change them to return 0, these stubs can go away.
111 static int initr_caches(void)
119 __weak int fixup_cpu(void)
124 static int initr_reloc_global_data(void)
127 monitor_flash_len = _end - __image_copy_start;
128 #elif defined(CONFIG_RISCV)
129 monitor_flash_len = (ulong)_end - (ulong)_start;
130 #elif !defined(CONFIG_SANDBOX) && !defined(CONFIG_NIOS2)
131 monitor_flash_len = (ulong)__init_end - gd->relocaddr;
133 #if defined(CONFIG_MPC85xx) || defined(CONFIG_MPC86xx)
135 * The gd->cpu pointer is set to an address in flash before relocation.
136 * We need to update it to point to the same CPU entry in RAM.
137 * TODO: why not just add gd->reloc_ofs?
139 gd->arch.cpu += gd->relocaddr - CONFIG_SYS_MONITOR_BASE;
142 * If we didn't know the cpu mask & # cores, we can save them of
143 * now rather than 'computing' them constantly
147 #ifdef CONFIG_SYS_RELOC_GD_ENV_ADDR
149 * Relocate the early env_addr pointer unless we know it is not inside
150 * the binary. Some systems need this and for the rest, it doesn't hurt.
152 gd->env_addr += gd->reloc_off;
154 #ifdef CONFIG_EFI_LOADER
156 * On the ARM architecture gd is mapped to a fixed register (r9 or x18).
157 * As this register may be overwritten by an EFI payload we save it here
158 * and restore it on every callback entered.
162 efi_runtime_relocate(gd->relocaddr, NULL);
168 __weak int arch_initr_trap(void)
173 #if defined(CONFIG_SYS_INIT_RAM_LOCK) && defined(CONFIG_E500)
174 static int initr_unlock_ram_in_cache(void)
176 unlock_ram_in_cache(); /* it's time to unlock D-cache in e500 */
181 static int initr_barrier(void)
184 /* TODO: Can we not use dmb() macros for this? */
190 static int initr_malloc(void)
194 #if CONFIG_VAL(SYS_MALLOC_F_LEN)
195 debug("Pre-reloc malloc() used %#lx bytes (%ld KB)\n", gd->malloc_ptr,
196 gd->malloc_ptr / 1024);
198 /* The malloc area is immediately below the monitor copy in DRAM */
200 * This value MUST match the value of gd->start_addr_sp in board_f.c:
201 * reserve_noncached().
203 start = gd->relocaddr - TOTAL_MALLOC_LEN;
204 gd_set_malloc_start(start);
205 mem_malloc_init((ulong)map_sysmem(start, TOTAL_MALLOC_LEN),
210 static int initr_of_live(void)
212 if (CONFIG_IS_ENABLED(OF_LIVE)) {
215 bootstage_start(BOOTSTAGE_ID_ACCUM_OF_LIVE, "of_live");
216 ret = of_live_build(gd->fdt_blob,
217 (struct device_node **)gd_of_root_ptr());
218 bootstage_accum(BOOTSTAGE_ID_ACCUM_OF_LIVE);
227 static int initr_dm(void)
233 /* Save the pre-reloc driver model and start a new one */
234 gd->dm_root_f = gd->dm_root;
239 bootstage_start(BOOTSTAGE_ID_ACCUM_DM_R, "dm_r");
240 ret = dm_init_and_scan(false);
241 bootstage_accum(BOOTSTAGE_ID_ACCUM_DM_R);
249 static int initr_dm_devices(void)
253 if (IS_ENABLED(CONFIG_TIMER_EARLY)) {
254 ret = dm_timer_init();
259 if (IS_ENABLED(CONFIG_MULTIPLEXER)) {
261 * Initialize the multiplexer controls to their default state.
262 * This must be done early as other drivers may unknowingly
273 static int initr_bootstage(void)
275 bootstage_mark_name(BOOTSTAGE_ID_START_UBOOT_R, "board_init_r");
280 __weak int power_init_board(void)
285 static int initr_announce(void)
287 debug("Now running in RAM - U-Boot at: %08lx\n", gd->relocaddr);
291 static int initr_binman(void)
295 if (!CONFIG_IS_ENABLED(BINMAN_FDT))
300 printf("binman_init failed:%d\n", ret);
305 #if defined(CONFIG_MTD_NOR_FLASH)
306 __weak int is_flash_available(void)
311 static int initr_flash(void)
313 ulong flash_size = 0;
314 struct bd_info *bd = gd->bd;
316 if (!is_flash_available())
321 if (board_flash_wp_on())
322 printf("Uninitialized - Write Protect On\n");
324 flash_size = flash_init();
326 print_size(flash_size, "");
327 #ifdef CONFIG_SYS_FLASH_CHECKSUM
329 * Compute and print flash CRC if flashchecksum is set to 'y'
331 * NOTE: Maybe we should add some schedule()? XXX
333 if (env_get_yesno("flashchecksum") == 1) {
334 const uchar *flash_base = (const uchar *)CFG_SYS_FLASH_BASE;
336 printf(" CRC: %08X", crc32(0,
340 #endif /* CONFIG_SYS_FLASH_CHECKSUM */
343 /* update start of FLASH memory */
344 #ifdef CFG_SYS_FLASH_BASE
345 bd->bi_flashstart = CFG_SYS_FLASH_BASE;
347 /* size of FLASH memory (final value) */
348 bd->bi_flashsize = flash_size;
350 #if defined(CONFIG_SYS_UPDATE_FLASH_SIZE)
351 /* Make a update of the Memctrl. */
352 update_flash_size(flash_size);
355 #if defined(CONFIG_OXC) || defined(CONFIG_RMU)
356 /* flash mapped at end of memory map */
357 bd->bi_flashoffset = CONFIG_TEXT_BASE + flash_size;
358 #elif CONFIG_SYS_MONITOR_BASE == CFG_SYS_FLASH_BASE
359 bd->bi_flashoffset = monitor_flash_len; /* reserved area for monitor */
365 #ifdef CONFIG_CMD_NAND
366 /* go init the NAND */
367 static int initr_nand(void)
371 printf("%lu MiB\n", nand_size() / 1024);
376 #if defined(CONFIG_CMD_ONENAND)
377 /* go init the NAND */
378 static int initr_onenand(void)
387 static int initr_mmc(void)
390 mmc_initialize(gd->bd);
395 #ifdef CONFIG_PVBLOCK
396 static int initr_pvblock(void)
405 * Tell if it's OK to load the environment early in boot.
407 * If CONFIG_OF_CONTROL is defined, we'll check with the FDT to see
408 * if this is OK (defaulting to saying it's OK).
410 * NOTE: Loading the environment early can be a bad idea if security is
411 * important, since no verification is done on the environment.
413 * Return: 0 if environment should not be loaded, !=0 if it is ok to load
415 static int should_load_env(void)
417 if (IS_ENABLED(CONFIG_OF_CONTROL))
418 return ofnode_conf_read_int("load-environment", 1);
420 if (IS_ENABLED(CONFIG_DELAY_ENVIRONMENT))
426 static int initr_env(void)
428 /* initialize environment */
429 if (should_load_env())
432 env_set_default(NULL, 0);
436 if (IS_ENABLED(CONFIG_OF_CONTROL))
437 env_set_hex("fdtcontroladdr",
438 (unsigned long)map_to_sysmem(gd->fdt_blob));
440 #if (IS_ENABLED(CONFIG_SAVE_PREV_BL_INITRAMFS_START_ADDR) || \
441 IS_ENABLED(CONFIG_SAVE_PREV_BL_FDT_ADDR))
445 /* Initialize from environment */
446 image_load_addr = env_get_ulong("loadaddr", 16, image_load_addr);
451 #ifdef CONFIG_SYS_MALLOC_BOOTPARAMS
452 static int initr_malloc_bootparams(void)
454 gd->bd->bi_boot_params = (ulong)malloc(CONFIG_SYS_BOOTPARAMS_LEN);
455 if (!gd->bd->bi_boot_params) {
456 puts("WARNING: Cannot allocate space for boot parameters\n");
463 #if defined(CONFIG_LED_STATUS)
464 static int initr_status_led(void)
466 #if defined(CONFIG_LED_STATUS_BOOT)
467 status_led_set(CONFIG_LED_STATUS_BOOT, CONFIG_LED_STATUS_BLINKING);
475 #if defined(CONFIG_SCSI) && !defined(CONFIG_DM_SCSI)
476 static int initr_scsi(void)
486 #ifdef CONFIG_CMD_NET
487 static int initr_net(void)
491 #if defined(CONFIG_RESET_PHY_R)
492 debug("Reset Ethernet PHY\n");
500 static int initr_post(void)
502 post_run(NULL, POST_RAM | post_bootmode_get(0));
507 #if defined(CFG_PRAM)
509 * Export available size of memory for Linux, taking into account the
510 * protected RAM at top of memory
517 pram = env_get_ulong("pram", 10, CFG_PRAM);
518 sprintf(memsz, "%ldk", (long int)((gd->ram_size / 1024) - pram));
519 env_set("mem", memsz);
525 static int dm_announce(void)
530 if (IS_ENABLED(CONFIG_DM)) {
531 dm_get_stats(&device_count, &uclass_count);
532 printf("Core: %d devices, %d uclasses", device_count,
534 if (CONFIG_IS_ENABLED(OF_REAL))
535 printf(", devicetree: %s", fdtdec_get_srcname());
537 if (IS_ENABLED(CONFIG_OF_HAS_PRIOR_STAGE) &&
538 (gd->fdt_src == FDTSRC_SEPARATE ||
539 gd->fdt_src == FDTSRC_EMBED)) {
540 printf("Warning: Unexpected devicetree source (not from a prior stage)");
541 printf("Warning: U-Boot may not function properly\n");
543 if (IS_ENABLED(CONFIG_OF_TAG_MIGRATE) &&
544 (gd->flags & GD_FLG_OF_TAG_MIGRATE))
546 * U-Boot will silently fail to work after 2023.07 if
547 * there are old tags present
549 printf("Warning: Device tree includes old 'u-boot,dm-' tags: please fix by 2023.07!\n");
555 static int run_main_loop(void)
557 #ifdef CONFIG_SANDBOX
558 sandbox_main_loop_init();
561 event_notify_null(EVT_MAIN_LOOP);
563 /* main_loop() can return to retry autoboot, if so just run it again */
570 * Over time we hope to remove these functions with code fragments and
571 * stub functions, and instead call the relevant function directly.
573 * We also hope to remove most of the driver-related init and do it if/when
574 * the driver is later used.
576 * TODO: perhaps reset the watchdog in the initcall function after each call?
578 static init_fnc_t init_sequence_r[] = {
582 /* TODO: could x86/PPC have this also perhaps? */
583 #if defined(CONFIG_ARM) || defined(CONFIG_RISCV)
585 /* Note: For Freescale LS2 SoCs, new MMU table is created in DDR.
586 * A temporary mapping of IFC high region is since removed,
587 * so environmental variables in NOR flash is not available
588 * until board_init() is called below to remap IFC to high
592 initr_reloc_global_data,
593 #if defined(CONFIG_SYS_INIT_RAM_LOCK) && defined(CONFIG_E500)
594 initr_unlock_ram_in_cache,
599 initr_bootstage, /* Needs malloc() but has its own timer */
600 #if defined(CONFIG_CONSOLE_RECORD)
603 #ifdef CONFIG_SYS_NONCACHED_MEMORY
610 #ifdef CONFIG_ADDR_MAP
613 #if defined(CONFIG_ARM) || defined(CONFIG_RISCV) || defined(CONFIG_SANDBOX)
614 board_init, /* Setup chipselects */
617 * TODO: printing of the clock inforamtion of the board is now
618 * implemented as part of bdinfo command. Currently only support for
619 * davinci SOC's is added. Remove this check once all the board
623 set_cpu_clk_info, /* Setup clock information */
625 #ifdef CONFIG_EFI_LOADER
629 #ifdef CONFIG_FSP_VERSION2
637 #if CONFIG_IS_ENABLED(WDT)
640 INIT_FUNC_WATCHDOG_RESET
642 #if defined(CONFIG_BOARD_EARLY_INIT_R)
645 INIT_FUNC_WATCHDOG_RESET
649 INIT_FUNC_WATCHDOG_RESET
650 #if defined(CONFIG_PCI_INIT_R) && defined(CONFIG_SYS_EARLY_PCI_INIT)
652 * Do early PCI configuration _before_ the flash gets initialised,
653 * because PCU resources are crucial for flash access on some boards.
657 #ifdef CONFIG_ARCH_EARLY_INIT_R
661 #ifdef CONFIG_MTD_NOR_FLASH
664 INIT_FUNC_WATCHDOG_RESET
665 #if defined(CONFIG_PPC) || defined(CONFIG_M68K) || defined(CONFIG_X86)
666 /* initialize higher level parts of CPU like time base and timers */
669 #ifdef CONFIG_EFI_LOADER
672 #ifdef CONFIG_CMD_NAND
675 #ifdef CONFIG_CMD_ONENAND
684 #ifdef CONFIG_PVBLOCK
688 #ifdef CONFIG_SYS_MALLOC_BOOTPARAMS
689 initr_malloc_bootparams,
691 INIT_FUNC_WATCHDOG_RESET
692 cpu_secondary_init_r,
693 #if defined(CONFIG_ID_EEPROM)
694 mac_read_from_eeprom,
696 INIT_FUNC_WATCHDOG_RESET
697 #if defined(CONFIG_PCI_INIT_R) && !defined(CONFIG_SYS_EARLY_PCI_INIT)
699 * Do pci configuration
708 console_init_r, /* fully init console as a device */
709 #ifdef CONFIG_DISPLAY_BOARDINFO_LATE
713 #ifdef CONFIG_ARCH_MISC_INIT
714 arch_misc_init, /* miscellaneous arch-dependent init */
716 #ifdef CONFIG_MISC_INIT_R
717 misc_init_r, /* miscellaneous platform-dependent init */
719 INIT_FUNC_WATCHDOG_RESET
720 #ifdef CONFIG_CMD_KGDB
724 #if defined(CONFIG_MICROBLAZE) || defined(CONFIG_M68K)
725 timer_init, /* initialize timer */
727 #if defined(CONFIG_LED_STATUS)
730 /* PPC has a udelay(20) here dating from 2002. Why? */
731 #ifdef CONFIG_BOARD_LATE_INIT
734 #if defined(CONFIG_SCSI) && !defined(CONFIG_DM_SCSI)
735 INIT_FUNC_WATCHDOG_RESET
738 #ifdef CONFIG_BITBANGMII
741 #ifdef CONFIG_PCI_ENDPOINT
744 #ifdef CONFIG_CMD_NET
745 INIT_FUNC_WATCHDOG_RESET
751 INIT_FUNC_WATCHDOG_RESET
752 INITCALL_EVENT(EVT_LAST_STAGE_INIT),
753 #if defined(CFG_PRAM)
759 void board_init_r(gd_t *new_gd, ulong dest_addr)
762 * The pre-relocation drivers may be using memory that has now gone
763 * away. Mark serial as unavailable - this will fall back to the debug
766 * Do the same with log drivers since the memory may not be available.
768 gd->flags &= ~(GD_FLG_SERIAL_READY | GD_FLG_LOG_READY);
771 * Set up the new global data pointer. So far only x86 does this
774 * dropping the new_gd parameter.
776 if (CONFIG_IS_ENABLED(X86_64) && !IS_ENABLED(CONFIG_EFI_APP))
777 arch_setup_gd(new_gd);
779 #if !defined(CONFIG_X86) && !defined(CONFIG_ARM) && !defined(CONFIG_ARM64)
782 gd->flags &= ~GD_FLG_LOG_READY;
784 if (initcall_run_list(init_sequence_r))
787 /* NOTREACHED - run_main_loop() does not return */