1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2011 The Chromium OS Authors.
4 * (C) Copyright 2002-2006
5 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
8 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
9 * Marius Groeger <mgroeger@sysgo.de>
14 #include <bootstage.h>
23 #include <asm/cache.h>
24 #include <u-boot/crc.h>
25 /* TODO: can we just include all these headers whether needed or not? */
26 #if defined(CONFIG_CMD_BEDBUG)
27 #include <bedbug/type.h>
34 #include <env_internal.h>
39 #if defined(CONFIG_CMD_KGDB)
45 #ifdef CONFIG_BITBANGMII
52 #include <onenand_uboot.h>
56 #include <status_led.h>
57 #include <stdio_dev.h>
64 #ifdef CONFIG_ADDR_MAP
67 #include <asm/sections.h>
69 #include <linux/compiler.h>
70 #include <linux/err.h>
71 #include <efi_loader.h>
73 #if defined(CONFIG_GPIO_HOG)
77 DECLARE_GLOBAL_DATA_PTR;
79 ulong monitor_flash_len;
81 __weak int board_flash_wp_on(void)
84 * Most flashes can't be detected when write protection is enabled,
85 * so provide a way to let U-Boot gracefully ignore write protected
91 __weak void cpu_secondary_init_r(void)
95 static int initr_secondary_cpu(void)
98 * after non-volatile devices & environment is setup and cpu code have
99 * another round to deal with any initialization that might require
100 * full access to the environment or loading of some image (firmware)
101 * from a non-volatile device
103 /* TODO: maybe define this for all archs? */
104 cpu_secondary_init_r();
109 static int initr_trace(void)
112 trace_init(gd->trace_buff, CONFIG_TRACE_BUFFER_SIZE);
118 static int initr_reloc(void)
120 /* tell others: relocation done */
121 gd->flags |= GD_FLG_RELOC | GD_FLG_FULL_MALLOC_INIT;
128 * Some of these functions are needed purely because the functions they
129 * call return void. If we change them to return 0, these stubs can go away.
131 static int initr_caches(void)
139 __weak int fixup_cpu(void)
144 static int initr_reloc_global_data(void)
147 monitor_flash_len = _end - __image_copy_start;
148 #elif defined(CONFIG_NDS32) || defined(CONFIG_RISCV)
149 monitor_flash_len = (ulong)&_end - (ulong)&_start;
150 #elif !defined(CONFIG_SANDBOX) && !defined(CONFIG_NIOS2)
151 monitor_flash_len = (ulong)&__init_end - gd->relocaddr;
153 #if defined(CONFIG_MPC85xx) || defined(CONFIG_MPC86xx)
155 * The gd->cpu pointer is set to an address in flash before relocation.
156 * We need to update it to point to the same CPU entry in RAM.
157 * TODO: why not just add gd->reloc_ofs?
159 gd->arch.cpu += gd->relocaddr - CONFIG_SYS_MONITOR_BASE;
162 * If we didn't know the cpu mask & # cores, we can save them of
163 * now rather than 'computing' them constantly
167 #ifdef CONFIG_SYS_RELOC_GD_ENV_ADDR
169 * Relocate the early env_addr pointer unless we know it is not inside
170 * the binary. Some systems need this and for the rest, it doesn't hurt.
172 gd->env_addr += gd->reloc_off;
174 #ifdef CONFIG_OF_EMBED
176 * The fdt_blob needs to be moved to new relocation address
177 * incase of FDT blob is embedded with in image
179 gd->fdt_blob += gd->reloc_off;
181 #ifdef CONFIG_EFI_LOADER
183 * On the ARM architecture gd is mapped to a fixed register (r9 or x18).
184 * As this register may be overwritten by an EFI payload we save it here
185 * and restore it on every callback entered.
189 efi_runtime_relocate(gd->relocaddr, NULL);
195 #if defined(CONFIG_PPC) || defined(CONFIG_M68K) || defined(CONFIG_MIPS)
196 static int initr_trap(void)
199 * Setup trap handlers
201 #if defined(CONFIG_PPC)
202 trap_init(gd->relocaddr);
204 trap_init(CONFIG_SYS_SDRAM_BASE);
210 #ifdef CONFIG_ADDR_MAP
211 static int initr_addr_map(void)
220 static int initr_post_backlog(void)
222 post_output_backlog();
227 #if defined(CONFIG_SYS_INIT_RAM_LOCK) && defined(CONFIG_E500)
228 static int initr_unlock_ram_in_cache(void)
230 unlock_ram_in_cache(); /* it's time to unlock D-cache in e500 */
235 #ifdef CONFIG_PCI_ENDPOINT
236 static int initr_pci_ep(void)
245 static int initr_pci(void)
247 if (IS_ENABLED(CONFIG_PCI_INIT_R))
254 static int initr_barrier(void)
257 /* TODO: Can we not use dmb() macros for this? */
263 static int initr_malloc(void)
267 #if CONFIG_VAL(SYS_MALLOC_F_LEN)
268 debug("Pre-reloc malloc() used %#lx bytes (%ld KB)\n", gd->malloc_ptr,
269 gd->malloc_ptr / 1024);
271 /* The malloc area is immediately below the monitor copy in DRAM */
273 * This value MUST match the value of gd->start_addr_sp in board_f.c:
274 * reserve_noncached().
276 malloc_start = gd->relocaddr - TOTAL_MALLOC_LEN;
277 mem_malloc_init((ulong)map_sysmem(malloc_start, TOTAL_MALLOC_LEN),
282 static int initr_console_record(void)
284 #if defined(CONFIG_CONSOLE_RECORD)
285 return console_record_init();
291 #ifdef CONFIG_SYS_NONCACHED_MEMORY
292 static int initr_noncached(void)
299 #ifdef CONFIG_OF_LIVE
300 static int initr_of_live(void)
304 bootstage_start(BOOTSTAGE_ID_ACCUM_OF_LIVE, "of_live");
305 ret = of_live_build(gd->fdt_blob, (struct device_node **)&gd->of_root);
306 bootstage_accum(BOOTSTAGE_ID_ACCUM_OF_LIVE);
315 static int initr_dm(void)
319 /* Save the pre-reloc driver model and start a new one */
320 gd->dm_root_f = gd->dm_root;
325 bootstage_start(BOOTSTAGE_ID_ACCUM_DM_R, "dm_r");
326 ret = dm_init_and_scan(false);
327 bootstage_accum(BOOTSTAGE_ID_ACCUM_DM_R);
335 static int initr_dm_devices(void)
339 if (IS_ENABLED(CONFIG_TIMER_EARLY)) {
340 ret = dm_timer_init();
345 if (IS_ENABLED(CONFIG_MULTIPLEXER)) {
347 * Initialize the multiplexer controls to their default state.
348 * This must be done early as other drivers may unknowingly
359 static int initr_bootstage(void)
361 bootstage_mark_name(BOOTSTAGE_ID_START_UBOOT_R, "board_init_r");
366 __weak int power_init_board(void)
371 static int initr_announce(void)
373 debug("Now running in RAM - U-Boot at: %08lx\n", gd->relocaddr);
377 #ifdef CONFIG_NEEDS_MANUAL_RELOC
378 static int initr_manual_reloc_cmdtable(void)
380 fixup_cmdtable(ll_entry_start(struct cmd_tbl, cmd),
381 ll_entry_count(struct cmd_tbl, cmd));
386 static int initr_binman(void)
388 if (!CONFIG_IS_ENABLED(BINMAN_FDT))
391 return binman_init();
394 #if defined(CONFIG_MTD_NOR_FLASH)
395 __weak int is_flash_available(void)
400 static int initr_flash(void)
402 ulong flash_size = 0;
403 struct bd_info *bd = gd->bd;
405 if (!is_flash_available())
410 if (board_flash_wp_on())
411 printf("Uninitialized - Write Protect On\n");
413 flash_size = flash_init();
415 print_size(flash_size, "");
416 #ifdef CONFIG_SYS_FLASH_CHECKSUM
418 * Compute and print flash CRC if flashchecksum is set to 'y'
420 * NOTE: Maybe we should add some WATCHDOG_RESET()? XXX
422 if (env_get_yesno("flashchecksum") == 1) {
423 const uchar *flash_base = (const uchar *)CONFIG_SYS_FLASH_BASE;
425 printf(" CRC: %08X", crc32(0,
429 #endif /* CONFIG_SYS_FLASH_CHECKSUM */
432 /* update start of FLASH memory */
433 #ifdef CONFIG_SYS_FLASH_BASE
434 bd->bi_flashstart = CONFIG_SYS_FLASH_BASE;
436 /* size of FLASH memory (final value) */
437 bd->bi_flashsize = flash_size;
439 #if defined(CONFIG_SYS_UPDATE_FLASH_SIZE)
440 /* Make a update of the Memctrl. */
441 update_flash_size(flash_size);
444 #if defined(CONFIG_OXC) || defined(CONFIG_RMU)
445 /* flash mapped at end of memory map */
446 bd->bi_flashoffset = CONFIG_SYS_TEXT_BASE + flash_size;
447 #elif CONFIG_SYS_MONITOR_BASE == CONFIG_SYS_FLASH_BASE
448 bd->bi_flashoffset = monitor_flash_len; /* reserved area for monitor */
454 #ifdef CONFIG_CMD_NAND
455 /* go init the NAND */
456 static int initr_nand(void)
460 printf("%lu MiB\n", nand_size() / 1024);
465 #if defined(CONFIG_CMD_ONENAND)
466 /* go init the NAND */
467 static int initr_onenand(void)
476 static int initr_mmc(void)
479 mmc_initialize(gd->bd);
485 static int initr_xen(void)
492 #ifdef CONFIG_PVBLOCK
493 static int initr_pvblock(void)
502 * Tell if it's OK to load the environment early in boot.
504 * If CONFIG_OF_CONTROL is defined, we'll check with the FDT to see
505 * if this is OK (defaulting to saying it's OK).
507 * NOTE: Loading the environment early can be a bad idea if security is
508 * important, since no verification is done on the environment.
510 * @return 0 if environment should not be loaded, !=0 if it is ok to load
512 static int should_load_env(void)
514 if (IS_ENABLED(CONFIG_OF_CONTROL))
515 return fdtdec_get_config_int(gd->fdt_blob,
516 "load-environment", 1);
518 if (IS_ENABLED(CONFIG_DELAY_ENVIRONMENT))
524 static int initr_env(void)
526 /* initialize environment */
527 if (should_load_env())
530 env_set_default(NULL, 0);
532 if (IS_ENABLED(CONFIG_OF_CONTROL))
533 env_set_hex("fdtcontroladdr",
534 (unsigned long)map_to_sysmem(gd->fdt_blob));
536 /* Initialize from environment */
537 image_load_addr = env_get_ulong("loadaddr", 16, image_load_addr);
542 #ifdef CONFIG_SYS_BOOTPARAMS_LEN
543 static int initr_malloc_bootparams(void)
545 gd->bd->bi_boot_params = (ulong)malloc(CONFIG_SYS_BOOTPARAMS_LEN);
546 if (!gd->bd->bi_boot_params) {
547 puts("WARNING: Cannot allocate space for boot parameters\n");
554 static int initr_jumptable(void)
560 #if defined(CONFIG_API)
561 static int initr_api(void)
569 #ifdef CONFIG_CMD_NET
570 static int initr_ethaddr(void)
572 struct bd_info *bd = gd->bd;
574 /* kept around for legacy kernels only ... ignore the next section */
575 eth_env_get_enetaddr("ethaddr", bd->bi_enetaddr);
579 #endif /* CONFIG_CMD_NET */
581 #ifdef CONFIG_CMD_KGDB
582 static int initr_kgdb(void)
590 #if defined(CONFIG_LED_STATUS)
591 static int initr_status_led(void)
593 #if defined(CONFIG_LED_STATUS_BOOT)
594 status_led_set(CONFIG_LED_STATUS_BOOT, CONFIG_LED_STATUS_BLINKING);
602 #if defined(CONFIG_SCSI) && !defined(CONFIG_DM_SCSI)
603 static int initr_scsi(void)
613 #ifdef CONFIG_BITBANGMII
614 static int initr_bbmii(void)
621 #ifdef CONFIG_CMD_NET
622 static int initr_net(void)
626 #if defined(CONFIG_RESET_PHY_R)
627 debug("Reset Ethernet PHY\n");
635 static int initr_post(void)
637 post_run(NULL, POST_RAM | post_bootmode_get(0));
642 #if defined(CONFIG_IDE) && !defined(CONFIG_BLK)
643 static int initr_ide(void)
646 #if defined(CONFIG_START_IDE)
647 if (board_start_ide())
656 #if defined(CONFIG_PRAM)
658 * Export available size of memory for Linux, taking into account the
659 * protected RAM at top of memory
666 pram = env_get_ulong("pram", 10, CONFIG_PRAM);
667 sprintf(memsz, "%ldk", (long int)((gd->ram_size / 1024) - pram));
668 env_set("mem", memsz);
674 static int run_main_loop(void)
676 #ifdef CONFIG_SANDBOX
677 sandbox_main_loop_init();
679 /* main_loop() can return to retry autoboot, if so just run it again */
686 * We hope to remove most of the driver-related init and do it if/when
687 * the driver is later used.
689 * TODO: perhaps reset the watchdog in the initcall function after each call?
691 static init_fnc_t init_sequence_r[] = {
694 /* TODO: could x86/PPC have this also perhaps? */
697 /* Note: For Freescale LS2 SoCs, new MMU table is created in DDR.
698 * A temporary mapping of IFC high region is since removed,
699 * so environmental variables in NOR flash is not available
700 * until board_init() is called below to remap IFC to high
704 initr_reloc_global_data,
705 #if defined(CONFIG_SYS_INIT_RAM_LOCK) && defined(CONFIG_E500)
706 initr_unlock_ram_in_cache,
711 initr_bootstage, /* Needs malloc() but has its own timer */
712 initr_console_record,
713 #ifdef CONFIG_SYS_NONCACHED_MEMORY
716 #ifdef CONFIG_OF_LIVE
722 #if defined(CONFIG_ARM) || defined(CONFIG_NDS32) || defined(CONFIG_RISCV) || \
723 defined(CONFIG_SANDBOX)
724 board_init, /* Setup chipselects */
727 * TODO: printing of the clock inforamtion of the board is now
728 * implemented as part of bdinfo command. Currently only support for
729 * davinci SOC's is added. Remove this check once all the board
733 set_cpu_clk_info, /* Setup clock information */
735 #ifdef CONFIG_EFI_LOADER
739 #ifdef CONFIG_FSP_VERSION2
746 #if CONFIG_IS_ENABLED(WDT)
749 INIT_FUNC_WATCHDOG_RESET
750 #if defined(CONFIG_NEEDS_MANUAL_RELOC) && defined(CONFIG_BLOCK_CACHE)
753 #ifdef CONFIG_NEEDS_MANUAL_RELOC
754 initr_manual_reloc_cmdtable,
756 #if defined(CONFIG_PPC) || defined(CONFIG_M68K) || defined(CONFIG_MIPS)
759 #ifdef CONFIG_ADDR_MAP
762 #if defined(CONFIG_BOARD_EARLY_INIT_R)
765 INIT_FUNC_WATCHDOG_RESET
769 INIT_FUNC_WATCHDOG_RESET
770 #if defined(CONFIG_PCI) && defined(CONFIG_SYS_EARLY_PCI_INIT)
772 * Do early PCI configuration _before_ the flash gets initialised,
773 * because PCU resources are crucial for flash access on some boards.
777 #ifdef CONFIG_ARCH_EARLY_INIT_R
781 #ifdef CONFIG_MTD_NOR_FLASH
784 INIT_FUNC_WATCHDOG_RESET
785 #if defined(CONFIG_PPC) || defined(CONFIG_M68K) || defined(CONFIG_X86)
786 /* initialize higher level parts of CPU like time base and timers */
789 #ifdef CONFIG_CMD_NAND
792 #ifdef CONFIG_CMD_ONENAND
801 #ifdef CONFIG_PVBLOCK
805 #ifdef CONFIG_SYS_BOOTPARAMS_LEN
806 initr_malloc_bootparams,
808 INIT_FUNC_WATCHDOG_RESET
810 #if defined(CONFIG_ID_EEPROM) || defined(CONFIG_SYS_I2C_MAC_OFFSET)
811 mac_read_from_eeprom,
813 INIT_FUNC_WATCHDOG_RESET
814 #if defined(CONFIG_PCI) && !defined(CONFIG_SYS_EARLY_PCI_INIT)
816 * Do pci configuration
825 console_init_r, /* fully init console as a device */
826 #ifdef CONFIG_DISPLAY_BOARDINFO_LATE
830 #ifdef CONFIG_ARCH_MISC_INIT
831 arch_misc_init, /* miscellaneous arch-dependent init */
833 #ifdef CONFIG_MISC_INIT_R
834 misc_init_r, /* miscellaneous platform-dependent init */
836 INIT_FUNC_WATCHDOG_RESET
837 #ifdef CONFIG_CMD_KGDB
841 #if defined(CONFIG_MICROBLAZE) || defined(CONFIG_M68K)
842 timer_init, /* initialize timer */
844 #if defined(CONFIG_LED_STATUS)
847 /* PPC has a udelay(20) here dating from 2002. Why? */
848 #ifdef CONFIG_CMD_NET
851 #if defined(CONFIG_GPIO_HOG)
854 #ifdef CONFIG_BOARD_LATE_INIT
857 #if defined(CONFIG_SCSI) && !defined(CONFIG_DM_SCSI)
858 INIT_FUNC_WATCHDOG_RESET
861 #ifdef CONFIG_BITBANGMII
864 #ifdef CONFIG_PCI_ENDPOINT
867 #ifdef CONFIG_CMD_NET
868 INIT_FUNC_WATCHDOG_RESET
874 #if defined(CONFIG_IDE) && !defined(CONFIG_BLK)
877 #ifdef CONFIG_LAST_STAGE_INIT
878 INIT_FUNC_WATCHDOG_RESET
880 * Some parts can be only initialized if all others (like
881 * Interrupts) are up and running (i.e. the PC-style ISA
886 #ifdef CONFIG_CMD_BEDBUG
887 INIT_FUNC_WATCHDOG_RESET
890 #if defined(CONFIG_PRAM)
896 void board_init_r(gd_t *new_gd, ulong dest_addr)
899 * Set up the new global data pointer. So far only x86 does this
901 * TODO(sjg@chromium.org): Consider doing this for all archs, or
902 * dropping the new_gd parameter.
904 #if CONFIG_IS_ENABLED(X86_64)
905 arch_setup_gd(new_gd);
908 #ifdef CONFIG_NEEDS_MANUAL_RELOC
912 #if !defined(CONFIG_X86) && !defined(CONFIG_ARM) && !defined(CONFIG_ARM64)
915 gd->flags &= ~GD_FLG_LOG_READY;
917 #ifdef CONFIG_NEEDS_MANUAL_RELOC
918 for (i = 0; i < ARRAY_SIZE(init_sequence_r); i++)
919 init_sequence_r[i] += gd->reloc_off;
922 if (initcall_run_list(init_sequence_r))
925 /* NOTREACHED - run_main_loop() does not return */