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>
22 #include <asm/cache.h>
23 #include <u-boot/crc.h>
24 /* TODO: can we just include all these headers whether needed or not? */
25 #if defined(CONFIG_CMD_BEDBUG)
26 #include <bedbug/type.h>
33 #include <env_internal.h>
38 #if defined(CONFIG_CMD_KGDB)
44 #ifdef CONFIG_BITBANGMII
50 #include <onenand_uboot.h>
53 #include <status_led.h>
54 #include <stdio_dev.h>
58 #ifdef CONFIG_ADDR_MAP
61 #include <asm/sections.h>
63 #include <linux/compiler.h>
64 #include <linux/err.h>
65 #include <efi_loader.h>
67 #if defined(CONFIG_GPIO_HOG)
71 DECLARE_GLOBAL_DATA_PTR;
73 ulong monitor_flash_len;
75 __weak int board_flash_wp_on(void)
78 * Most flashes can't be detected when write protection is enabled,
79 * so provide a way to let U-Boot gracefully ignore write protected
85 __weak void cpu_secondary_init_r(void)
89 static int initr_secondary_cpu(void)
92 * after non-volatile devices & environment is setup and cpu code have
93 * another round to deal with any initialization that might require
94 * full access to the environment or loading of some image (firmware)
95 * from a non-volatile device
97 /* TODO: maybe define this for all archs? */
98 cpu_secondary_init_r();
103 static int initr_trace(void)
106 trace_init(gd->trace_buff, CONFIG_TRACE_BUFFER_SIZE);
112 static int initr_reloc(void)
114 /* tell others: relocation done */
115 gd->flags |= GD_FLG_RELOC | GD_FLG_FULL_MALLOC_INIT;
122 * Some of these functions are needed purely because the functions they
123 * call return void. If we change them to return 0, these stubs can go away.
125 static int initr_caches(void)
133 __weak int fixup_cpu(void)
138 static int initr_reloc_global_data(void)
141 monitor_flash_len = _end - __image_copy_start;
142 #elif defined(CONFIG_NDS32) || defined(CONFIG_RISCV)
143 monitor_flash_len = (ulong)&_end - (ulong)&_start;
144 #elif !defined(CONFIG_SANDBOX) && !defined(CONFIG_NIOS2)
145 monitor_flash_len = (ulong)&__init_end - gd->relocaddr;
147 #if defined(CONFIG_MPC85xx) || defined(CONFIG_MPC86xx)
149 * The gd->cpu pointer is set to an address in flash before relocation.
150 * We need to update it to point to the same CPU entry in RAM.
151 * TODO: why not just add gd->reloc_ofs?
153 gd->arch.cpu += gd->relocaddr - CONFIG_SYS_MONITOR_BASE;
156 * If we didn't know the cpu mask & # cores, we can save them of
157 * now rather than 'computing' them constantly
161 #ifdef CONFIG_SYS_RELOC_GD_ENV_ADDR
163 * Relocate the early env_addr pointer unless we know it is not inside
164 * the binary. Some systems need this and for the rest, it doesn't hurt.
166 gd->env_addr += gd->reloc_off;
168 #ifdef CONFIG_OF_EMBED
170 * The fdt_blob needs to be moved to new relocation address
171 * incase of FDT blob is embedded with in image
173 gd->fdt_blob += gd->reloc_off;
175 #ifdef CONFIG_EFI_LOADER
177 * On the ARM architecture gd is mapped to a fixed register (r9 or x18).
178 * As this register may be overwritten by an EFI payload we save it here
179 * and restore it on every callback entered.
183 efi_runtime_relocate(gd->relocaddr, NULL);
189 static int initr_serial(void)
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 */
236 static int initr_pci(void)
238 if (IS_ENABLED(CONFIG_PCI_INIT_R))
245 static int initr_barrier(void)
248 /* TODO: Can we not use dmb() macros for this? */
254 static int initr_malloc(void)
258 #if CONFIG_VAL(SYS_MALLOC_F_LEN)
259 debug("Pre-reloc malloc() used %#lx bytes (%ld KB)\n", gd->malloc_ptr,
260 gd->malloc_ptr / 1024);
262 /* The malloc area is immediately below the monitor copy in DRAM */
264 * This value MUST match the value of gd->start_addr_sp in board_f.c:
265 * reserve_noncached().
267 malloc_start = gd->relocaddr - TOTAL_MALLOC_LEN;
268 mem_malloc_init((ulong)map_sysmem(malloc_start, TOTAL_MALLOC_LEN),
273 static int initr_console_record(void)
275 #if defined(CONFIG_CONSOLE_RECORD)
276 return console_record_init();
282 #ifdef CONFIG_SYS_NONCACHED_MEMORY
283 static int initr_noncached(void)
290 #ifdef CONFIG_OF_LIVE
291 static int initr_of_live(void)
295 bootstage_start(BOOTSTAGE_ID_ACCUM_OF_LIVE, "of_live");
296 ret = of_live_build(gd->fdt_blob, (struct device_node **)&gd->of_root);
297 bootstage_accum(BOOTSTAGE_ID_ACCUM_OF_LIVE);
306 static int initr_dm(void)
310 /* Save the pre-reloc driver model and start a new one */
311 gd->dm_root_f = gd->dm_root;
316 bootstage_start(BOOTSTAGE_ID_ACCUM_DM_R, "dm_r");
317 ret = dm_init_and_scan(false);
318 bootstage_accum(BOOTSTAGE_ID_ACCUM_DM_R);
326 static int initr_dm_devices(void)
330 if (IS_ENABLED(CONFIG_TIMER_EARLY)) {
331 ret = dm_timer_init();
339 static int initr_bootstage(void)
341 bootstage_mark_name(BOOTSTAGE_ID_START_UBOOT_R, "board_init_r");
346 __weak int power_init_board(void)
351 static int initr_announce(void)
353 debug("Now running in RAM - U-Boot at: %08lx\n", gd->relocaddr);
357 #ifdef CONFIG_NEEDS_MANUAL_RELOC
358 static int initr_manual_reloc_cmdtable(void)
360 fixup_cmdtable(ll_entry_start(struct cmd_tbl, cmd),
361 ll_entry_count(struct cmd_tbl, cmd));
366 static int initr_binman(void)
368 if (!CONFIG_IS_ENABLED(BINMAN_FDT))
371 return binman_init();
374 #if defined(CONFIG_MTD_NOR_FLASH)
375 static int initr_flash(void)
377 ulong flash_size = 0;
382 if (board_flash_wp_on())
383 printf("Uninitialized - Write Protect On\n");
385 flash_size = flash_init();
387 print_size(flash_size, "");
388 #ifdef CONFIG_SYS_FLASH_CHECKSUM
390 * Compute and print flash CRC if flashchecksum is set to 'y'
392 * NOTE: Maybe we should add some WATCHDOG_RESET()? XXX
394 if (env_get_yesno("flashchecksum") == 1) {
395 const uchar *flash_base = (const uchar *)CONFIG_SYS_FLASH_BASE;
397 printf(" CRC: %08X", crc32(0,
401 #endif /* CONFIG_SYS_FLASH_CHECKSUM */
404 /* update start of FLASH memory */
405 #ifdef CONFIG_SYS_FLASH_BASE
406 bd->bi_flashstart = CONFIG_SYS_FLASH_BASE;
408 /* size of FLASH memory (final value) */
409 bd->bi_flashsize = flash_size;
411 #if defined(CONFIG_SYS_UPDATE_FLASH_SIZE)
412 /* Make a update of the Memctrl. */
413 update_flash_size(flash_size);
416 #if defined(CONFIG_OXC) || defined(CONFIG_RMU)
417 /* flash mapped at end of memory map */
418 bd->bi_flashoffset = CONFIG_SYS_TEXT_BASE + flash_size;
419 #elif CONFIG_SYS_MONITOR_BASE == CONFIG_SYS_FLASH_BASE
420 bd->bi_flashoffset = monitor_flash_len; /* reserved area for monitor */
426 #ifdef CONFIG_CMD_NAND
427 /* go init the NAND */
428 static int initr_nand(void)
432 printf("%lu MiB\n", nand_size() / 1024);
437 #if defined(CONFIG_CMD_ONENAND)
438 /* go init the NAND */
439 static int initr_onenand(void)
448 static int initr_mmc(void)
451 mmc_initialize(gd->bd);
457 * Tell if it's OK to load the environment early in boot.
459 * If CONFIG_OF_CONTROL is defined, we'll check with the FDT to see
460 * if this is OK (defaulting to saying it's OK).
462 * NOTE: Loading the environment early can be a bad idea if security is
463 * important, since no verification is done on the environment.
465 * @return 0 if environment should not be loaded, !=0 if it is ok to load
467 static int should_load_env(void)
469 if (IS_ENABLED(CONFIG_OF_CONTROL))
470 return fdtdec_get_config_int(gd->fdt_blob,
471 "load-environment", 1);
473 if (IS_ENABLED(CONFIG_DELAY_ENVIRONMENT))
479 static int initr_env(void)
481 /* initialize environment */
482 if (should_load_env())
485 env_set_default(NULL, 0);
487 if (IS_ENABLED(CONFIG_OF_CONTROL))
488 env_set_hex("fdtcontroladdr",
489 (unsigned long)map_to_sysmem(gd->fdt_blob));
491 /* Initialize from environment */
492 image_load_addr = env_get_ulong("loadaddr", 16, image_load_addr);
497 #ifdef CONFIG_SYS_BOOTPARAMS_LEN
498 static int initr_malloc_bootparams(void)
500 gd->bd->bi_boot_params = (ulong)malloc(CONFIG_SYS_BOOTPARAMS_LEN);
501 if (!gd->bd->bi_boot_params) {
502 puts("WARNING: Cannot allocate space for boot parameters\n");
509 static int initr_jumptable(void)
515 #if defined(CONFIG_API)
516 static int initr_api(void)
524 #ifdef CONFIG_CMD_NET
525 static int initr_ethaddr(void)
529 /* kept around for legacy kernels only ... ignore the next section */
530 eth_env_get_enetaddr("ethaddr", bd->bi_enetaddr);
531 #ifdef CONFIG_HAS_ETH1
532 eth_env_get_enetaddr("eth1addr", bd->bi_enet1addr);
534 #ifdef CONFIG_HAS_ETH2
535 eth_env_get_enetaddr("eth2addr", bd->bi_enet2addr);
537 #ifdef CONFIG_HAS_ETH3
538 eth_env_get_enetaddr("eth3addr", bd->bi_enet3addr);
540 #ifdef CONFIG_HAS_ETH4
541 eth_env_get_enetaddr("eth4addr", bd->bi_enet4addr);
543 #ifdef CONFIG_HAS_ETH5
544 eth_env_get_enetaddr("eth5addr", bd->bi_enet5addr);
548 #endif /* CONFIG_CMD_NET */
550 #ifdef CONFIG_CMD_KGDB
551 static int initr_kgdb(void)
559 #if defined(CONFIG_LED_STATUS)
560 static int initr_status_led(void)
562 #if defined(CONFIG_LED_STATUS_BOOT)
563 status_led_set(CONFIG_LED_STATUS_BOOT, CONFIG_LED_STATUS_BLINKING);
571 #if defined(CONFIG_SCSI) && !defined(CONFIG_DM_SCSI)
572 static int initr_scsi(void)
582 #ifdef CONFIG_BITBANGMII
583 static int initr_bbmii(void)
590 #ifdef CONFIG_CMD_NET
591 static int initr_net(void)
595 #if defined(CONFIG_RESET_PHY_R)
596 debug("Reset Ethernet PHY\n");
604 static int initr_post(void)
606 post_run(NULL, POST_RAM | post_bootmode_get(0));
611 #if defined(CONFIG_IDE) && !defined(CONFIG_BLK)
612 static int initr_ide(void)
615 #if defined(CONFIG_START_IDE)
616 if (board_start_ide())
625 #if defined(CONFIG_PRAM)
627 * Export available size of memory for Linux, taking into account the
628 * protected RAM at top of memory
635 pram = env_get_ulong("pram", 10, CONFIG_PRAM);
636 sprintf(memsz, "%ldk", (long int)((gd->ram_size / 1024) - pram));
637 env_set("mem", memsz);
643 static int run_main_loop(void)
645 #ifdef CONFIG_SANDBOX
646 sandbox_main_loop_init();
648 /* main_loop() can return to retry autoboot, if so just run it again */
655 * We hope to remove most of the driver-related init and do it if/when
656 * the driver is later used.
658 * TODO: perhaps reset the watchdog in the initcall function after each call?
660 static init_fnc_t init_sequence_r[] = {
663 /* TODO: could x86/PPC have this also perhaps? */
666 /* Note: For Freescale LS2 SoCs, new MMU table is created in DDR.
667 * A temporary mapping of IFC high region is since removed,
668 * so environmental variables in NOR flash is not available
669 * until board_init() is called below to remap IFC to high
673 initr_reloc_global_data,
674 #if defined(CONFIG_SYS_INIT_RAM_LOCK) && defined(CONFIG_E500)
675 initr_unlock_ram_in_cache,
680 initr_bootstage, /* Needs malloc() but has its own timer */
681 initr_console_record,
682 #ifdef CONFIG_SYS_NONCACHED_MEMORY
685 #ifdef CONFIG_OF_LIVE
691 #if defined(CONFIG_ARM) || defined(CONFIG_NDS32) || defined(CONFIG_RISCV) || \
692 defined(CONFIG_SANDBOX)
693 board_init, /* Setup chipselects */
696 * TODO: printing of the clock inforamtion of the board is now
697 * implemented as part of bdinfo command. Currently only support for
698 * davinci SOC's is added. Remove this check once all the board
702 set_cpu_clk_info, /* Setup clock information */
704 #ifdef CONFIG_EFI_LOADER
708 #ifdef CONFIG_FSP_VERSION2
715 #if CONFIG_IS_ENABLED(WDT)
718 INIT_FUNC_WATCHDOG_RESET
719 #ifdef CONFIG_NEEDS_MANUAL_RELOC
720 initr_manual_reloc_cmdtable,
722 #if defined(CONFIG_PPC) || defined(CONFIG_M68K) || defined(CONFIG_MIPS)
725 #ifdef CONFIG_ADDR_MAP
728 #if defined(CONFIG_BOARD_EARLY_INIT_R)
731 INIT_FUNC_WATCHDOG_RESET
735 INIT_FUNC_WATCHDOG_RESET
736 #if defined(CONFIG_PCI) && defined(CONFIG_SYS_EARLY_PCI_INIT)
738 * Do early PCI configuration _before_ the flash gets initialised,
739 * because PCU resources are crucial for flash access on some boards.
743 #ifdef CONFIG_ARCH_EARLY_INIT_R
747 #ifdef CONFIG_MTD_NOR_FLASH
750 INIT_FUNC_WATCHDOG_RESET
751 #if defined(CONFIG_PPC) || defined(CONFIG_M68K) || defined(CONFIG_X86)
752 /* initialize higher level parts of CPU like time base and timers */
755 #ifdef CONFIG_CMD_NAND
758 #ifdef CONFIG_CMD_ONENAND
765 #ifdef CONFIG_SYS_BOOTPARAMS_LEN
766 initr_malloc_bootparams,
768 INIT_FUNC_WATCHDOG_RESET
770 #if defined(CONFIG_ID_EEPROM) || defined(CONFIG_SYS_I2C_MAC_OFFSET)
771 mac_read_from_eeprom,
773 INIT_FUNC_WATCHDOG_RESET
774 #if defined(CONFIG_PCI) && !defined(CONFIG_SYS_EARLY_PCI_INIT)
776 * Do pci configuration
785 console_init_r, /* fully init console as a device */
786 #ifdef CONFIG_DISPLAY_BOARDINFO_LATE
790 #ifdef CONFIG_ARCH_MISC_INIT
791 arch_misc_init, /* miscellaneous arch-dependent init */
793 #ifdef CONFIG_MISC_INIT_R
794 misc_init_r, /* miscellaneous platform-dependent init */
796 INIT_FUNC_WATCHDOG_RESET
797 #ifdef CONFIG_CMD_KGDB
801 #if defined(CONFIG_MICROBLAZE) || defined(CONFIG_M68K)
802 timer_init, /* initialize timer */
804 #if defined(CONFIG_LED_STATUS)
807 /* PPC has a udelay(20) here dating from 2002. Why? */
808 #ifdef CONFIG_CMD_NET
811 #if defined(CONFIG_GPIO_HOG)
814 #ifdef CONFIG_BOARD_LATE_INIT
817 #if defined(CONFIG_SCSI) && !defined(CONFIG_DM_SCSI)
818 INIT_FUNC_WATCHDOG_RESET
821 #ifdef CONFIG_BITBANGMII
824 #ifdef CONFIG_CMD_NET
825 INIT_FUNC_WATCHDOG_RESET
831 #if defined(CONFIG_IDE) && !defined(CONFIG_BLK)
834 #ifdef CONFIG_LAST_STAGE_INIT
835 INIT_FUNC_WATCHDOG_RESET
837 * Some parts can be only initialized if all others (like
838 * Interrupts) are up and running (i.e. the PC-style ISA
843 #ifdef CONFIG_CMD_BEDBUG
844 INIT_FUNC_WATCHDOG_RESET
847 #if defined(CONFIG_PRAM)
850 #if defined(CONFIG_M68K) && defined(CONFIG_BLOCK_CACHE)
856 void board_init_r(gd_t *new_gd, ulong dest_addr)
859 * Set up the new global data pointer. So far only x86 does this
861 * TODO(sjg@chromium.org): Consider doing this for all archs, or
862 * dropping the new_gd parameter.
864 #if CONFIG_IS_ENABLED(X86_64)
865 arch_setup_gd(new_gd);
868 #ifdef CONFIG_NEEDS_MANUAL_RELOC
872 #if !defined(CONFIG_X86) && !defined(CONFIG_ARM) && !defined(CONFIG_ARM64)
875 gd->flags &= ~GD_FLG_LOG_READY;
877 #ifdef CONFIG_NEEDS_MANUAL_RELOC
878 for (i = 0; i < ARRAY_SIZE(init_sequence_r); i++)
879 init_sequence_r[i] += gd->reloc_off;
882 if (initcall_run_list(init_sequence_r))
885 /* NOTREACHED - run_main_loop() does not return */