blob: e4e17dd717e1ef06f0073dba5ba9177acc359398 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glassc45e3592013-03-11 06:49:53 +00002/*
3 * Copyright (c) 2011 The Chromium OS Authors.
4 * (C) Copyright 2002-2006
5 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
6 *
7 * (C) Copyright 2002
8 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
9 * Marius Groeger <mgroeger@sysgo.de>
Simon Glassc45e3592013-03-11 06:49:53 +000010 */
11
Tom Rinidec7ea02024-05-20 13:35:03 -060012#include <config.h>
Simon Glassa815dab2018-11-15 18:43:52 -070013#include <bloblist.h>
Simon Glass1ea97892020-05-10 11:40:00 -060014#include <bootstage.h>
Simon Glass85d65312019-12-28 10:44:58 -070015#include <clock_legacy.h>
Simon Glassa73bda42015-11-08 23:47:45 -070016#include <console.h>
Mario Six97bbb602018-08-06 10:23:41 +020017#include <cpu.h>
Simon Glass1fa70f82019-11-14 12:57:34 -070018#include <cpu_func.h>
Stefan Roese7513df32022-09-02 13:57:50 +020019#include <cyclic.h>
Simon Glass1ab16922022-07-31 12:28:48 -060020#include <display_options.h>
Simon Glassa730c5d2014-07-23 06:55:04 -060021#include <dm.h>
Simon Glass79fd2142019-08-01 09:46:43 -060022#include <env.h>
Simon Glass9d1f6192019-08-02 09:44:25 -060023#include <env_internal.h>
Simon Glass4f542532022-03-04 08:43:02 -070024#include <event.h>
Simon Glassc45e3592013-03-11 06:49:53 +000025#include <fdtdec.h>
Simon Glass15393432013-04-20 08:42:41 +000026#include <fs.h>
Simon Glassf11478f2019-12-28 10:45:07 -070027#include <hang.h>
Simon Glass50250b52013-03-11 14:30:42 +000028#include <i2c.h>
Simon Glass6980b6b2019-11-14 12:57:45 -070029#include <init.h>
Simon Glassc45e3592013-03-11 06:49:53 +000030#include <initcall.h>
Simon Glass0f2af882020-05-10 11:40:05 -060031#include <log.h>
Simon Glassd1d087d2015-02-27 22:06:36 -070032#include <malloc.h>
Joe Hershberger65b905b2015-03-22 17:08:59 -050033#include <mapmem.h>
Simon Glass62cf9122013-04-26 02:53:43 +000034#include <os.h>
Simon Glassc45e3592013-03-11 06:49:53 +000035#include <post.h>
Simon Glass6ab91072017-03-31 08:40:38 -060036#include <relocate.h>
Simon Glass36736182019-11-14 12:57:24 -070037#include <serial.h>
Simon Glasse14f1a22018-11-15 18:44:09 -070038#include <spl.h>
Jeroen Hofsteea802b982014-06-23 23:20:19 +020039#include <status_led.h>
Mario Six4481a5d2018-08-06 10:23:34 +020040#include <sysreset.h>
Jerome Forissier1fd58d42025-04-04 15:50:36 +020041#include <time.h>
Simon Glass8e4f80f2016-02-24 09:14:50 -070042#include <timer.h>
Simon Glass209a1a62013-06-11 11:14:42 -070043#include <trace.h>
Simon Glass45aec8e2024-08-07 16:47:34 -060044#include <upl.h>
Simon Glassfce58f52016-01-18 19:52:21 -070045#include <video.h>
Simon Glass50250b52013-03-11 14:30:42 +000046#include <watchdog.h>
Simon Glass274e0b02020-05-10 11:39:56 -060047#include <asm/cache.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060048#include <asm/global_data.h>
Simon Glassc45e3592013-03-11 06:49:53 +000049#include <asm/io.h>
50#include <asm/sections.h>
Simon Glassa730c5d2014-07-23 06:55:04 -060051#include <dm/root.h>
Simon Glassb3c12562017-03-31 08:40:35 -060052#include <linux/errno.h>
Pali Rohár8dc23ef2022-09-18 13:23:27 +020053#include <linux/log2.h>
Simon Glassc45e3592013-03-11 06:49:53 +000054
Simon Glassc45e3592013-03-11 06:49:53 +000055DECLARE_GLOBAL_DATA_PTR;
Simon Glassc45e3592013-03-11 06:49:53 +000056
57/*
Simon Glass839855c2015-04-28 20:25:03 -060058 * TODO(sjg@chromium.org): IMO this code should be
Simon Glassc45e3592013-03-11 06:49:53 +000059 * refactored to a single function, something like:
60 *
61 * void led_set_state(enum led_colour_t colour, int on);
62 */
63/************************************************************************
64 * Coloured LED functionality
65 ************************************************************************
66 * May be supplied by boards if desired
67 */
Jeroen Hofsteea802b982014-06-23 23:20:19 +020068__weak void coloured_LED_init(void) {}
69__weak void red_led_on(void) {}
70__weak void red_led_off(void) {}
71__weak void green_led_on(void) {}
72__weak void green_led_off(void) {}
73__weak void yellow_led_on(void) {}
74__weak void yellow_led_off(void) {}
75__weak void blue_led_on(void) {}
76__weak void blue_led_off(void) {}
Simon Glassc45e3592013-03-11 06:49:53 +000077
78/*
79 * Why is gd allocated a register? Prior to reloc it might be better to
80 * just pass it around to each function in this file?
81 *
82 * After reloc one could argue that it is hardly used and doesn't need
83 * to be in a register. Or if it is it should perhaps hold pointers to all
84 * global data for all modules, so that post-reloc we can avoid the massive
85 * literal pool we get on ARM. Or perhaps just encourage each module to use
86 * a structure...
87 */
88
Sonic Zhangf503a522014-07-17 19:01:34 +080089#if defined(CONFIG_WATCHDOG) || defined(CONFIG_HW_WATCHDOG)
Simon Glass50250b52013-03-11 14:30:42 +000090static int init_func_watchdog_init(void)
91{
Tom Rini210ebce2017-03-14 11:08:10 -040092# if defined(CONFIG_HW_WATCHDOG) && \
93 (defined(CONFIG_M68K) || defined(CONFIG_MICROBLAZE) || \
Prasanthi Chellakumar0509c4e2018-10-09 11:46:40 -070094 defined(CONFIG_SH) || \
Anatolij Gustschin87db2942016-06-13 14:24:23 +020095 defined(CONFIG_DESIGNWARE_WATCHDOG) || \
Stefan Roeseee86af22015-03-10 08:04:36 +010096 defined(CONFIG_IMX_WATCHDOG))
Sonic Zhangf503a522014-07-17 19:01:34 +080097 hw_watchdog_init();
Simon Glass50250b52013-03-11 14:30:42 +000098 puts(" Watchdog enabled\n");
Anatolij Gustschind3aa98a2016-06-13 14:24:24 +020099# endif
Stefan Roese80877fa2022-09-02 14:10:46 +0200100 schedule();
Simon Glass50250b52013-03-11 14:30:42 +0000101
102 return 0;
103}
104
105int init_func_watchdog_reset(void)
106{
Stefan Roese80877fa2022-09-02 14:10:46 +0200107 schedule();
Simon Glass50250b52013-03-11 14:30:42 +0000108
109 return 0;
110}
111#endif /* CONFIG_WATCHDOG */
112
Jeroen Hofstee45846052014-10-08 22:57:22 +0200113__weak void board_add_ram_info(int use_default)
Simon Glass50250b52013-03-11 14:30:42 +0000114{
115 /* please define platform specific board_add_ram_info() */
116}
117
Simon Glassc45e3592013-03-11 06:49:53 +0000118static int init_baud_rate(void)
119{
Simon Glass22c34c22017-08-03 12:22:13 -0600120 gd->baudrate = env_get_ulong("baudrate", 10, CONFIG_BAUDRATE);
Simon Glassc45e3592013-03-11 06:49:53 +0000121 return 0;
122}
123
124static int display_text_info(void)
125{
Ben Stoltz1930e8d2015-07-31 09:31:37 -0600126#if !defined(CONFIG_SANDBOX) && !defined(CONFIG_EFI_APP)
Daniel Schwierzeck17c2af62014-11-15 23:46:53 +0100127 ulong bss_start, bss_end, text_base;
Simon Glassc45e3592013-03-11 06:49:53 +0000128
Shiji Yangeff11fa2023-08-03 09:47:17 +0800129 bss_start = (ulong)__bss_start;
130 bss_end = (ulong)__bss_end;
Albert ARIBAUD6e294722014-02-22 17:53:43 +0100131
Simon Glass72cc5382022-10-20 18:22:39 -0600132#ifdef CONFIG_TEXT_BASE
133 text_base = CONFIG_TEXT_BASE;
Sonic Zhangf503a522014-07-17 19:01:34 +0800134#else
Daniel Schwierzeck17c2af62014-11-15 23:46:53 +0100135 text_base = CONFIG_SYS_MONITOR_BASE;
Sonic Zhangf503a522014-07-17 19:01:34 +0800136#endif
Daniel Schwierzeck17c2af62014-11-15 23:46:53 +0100137
138 debug("U-Boot code: %08lX -> %08lX BSS: -> %08lX\n",
Mario Six80b66dd2018-01-15 11:10:02 +0100139 text_base, bss_start, bss_end);
Simon Glass62cf9122013-04-26 02:53:43 +0000140#endif
Simon Glassc45e3592013-03-11 06:49:53 +0000141
Simon Glassc45e3592013-03-11 06:49:53 +0000142 return 0;
143}
144
Mario Six4481a5d2018-08-06 10:23:34 +0200145#ifdef CONFIG_SYSRESET
146static int print_resetinfo(void)
147{
148 struct udevice *dev;
149 char status[256];
Michal Suchanek32c58c12022-10-10 20:29:40 +0200150 bool status_printed = false;
Mario Six4481a5d2018-08-06 10:23:34 +0200151 int ret;
152
Bin Meng50a132b2023-07-22 00:15:21 +0800153 /*
154 * Not all boards have sysreset drivers available during early
Michal Suchanek32c58c12022-10-10 20:29:40 +0200155 * boot, so don't fail if one can't be found.
156 */
157 for (ret = uclass_first_device_check(UCLASS_SYSRESET, &dev); dev;
Bin Meng50a132b2023-07-22 00:15:21 +0800158 ret = uclass_next_device_check(&dev)) {
Michal Suchanek32c58c12022-10-10 20:29:40 +0200159 if (ret) {
160 debug("%s: %s sysreset device (error: %d)\n",
161 __func__, dev->name, ret);
162 continue;
163 }
Mario Six4481a5d2018-08-06 10:23:34 +0200164
Michal Suchanek32c58c12022-10-10 20:29:40 +0200165 if (!sysreset_get_status(dev, status, sizeof(status))) {
166 printf("%s%s", status_printed ? " " : "", status);
167 status_printed = true;
168 }
169 }
170 if (status_printed)
171 printf("\n");
Mario Six4481a5d2018-08-06 10:23:34 +0200172
173 return 0;
174}
175#endif
176
Mario Six97bbb602018-08-06 10:23:41 +0200177#if defined(CONFIG_DISPLAY_CPUINFO) && CONFIG_IS_ENABLED(CPU)
178static int print_cpuinfo(void)
179{
180 struct udevice *dev;
181 char desc[512];
182 int ret;
183
Ye Li28abafd2020-05-03 21:58:50 +0800184 dev = cpu_get_current_dev();
185 if (!dev) {
186 debug("%s: Could not get CPU device\n",
187 __func__);
188 return -ENODEV;
Mario Six97bbb602018-08-06 10:23:41 +0200189 }
190
191 ret = cpu_get_desc(dev, desc, sizeof(desc));
192 if (ret) {
193 debug("%s: Could not get CPU description (err = %d)\n",
194 dev->name, ret);
195 return ret;
196 }
197
Bin Mengbe2269f2018-10-10 22:06:55 -0700198 printf("CPU: %s\n", desc);
Mario Six97bbb602018-08-06 10:23:41 +0200199
200 return 0;
201}
202#endif
203
Simon Glassc45e3592013-03-11 06:49:53 +0000204static int announce_dram_init(void)
205{
206 puts("DRAM: ");
207 return 0;
208}
209
Pali Rohár8dc23ef2022-09-18 13:23:27 +0200210/*
211 * From input size calculate its nearest rounded unit scale (multiply of 2^10)
212 * and value in calculated unit scale multiplied by 10 (as fractional fixed
213 * point number with one decimal digit), which is human natural format,
214 * same what uses print_size() function for displaying. Mathematically it is:
215 * round_nearest(val * 2^scale) = size * 10; where: 10 <= val < 10240.
216 *
217 * For example for size=87654321 we calculate scale=20 and val=836 which means
218 * that input has natural human format 83.6 M (mega = 2^20).
219 */
220#define compute_size_scale_val(size, scale, val) do { \
221 scale = ilog2(size) / 10 * 10; \
222 val = (10 * size + ((1ULL << scale) >> 1)) >> scale; \
223 if (val == 10240) { val = 10; scale += 10; } \
224} while (0)
225
226/*
227 * Check if the sizes in their natural units written in decimal format with
228 * one fraction number are same.
229 */
230static int sizes_near(unsigned long long size1, unsigned long long size2)
231{
232 unsigned int size1_scale, size1_val, size2_scale, size2_val;
233
234 compute_size_scale_val(size1, size1_scale, size1_val);
235 compute_size_scale_val(size2, size2_scale, size2_val);
236
237 return size1_scale == size2_scale && size1_val == size2_val;
238}
239
Simon Glassc45e3592013-03-11 06:49:53 +0000240static int show_dram_config(void)
241{
York Sun60ac15a2014-05-02 17:28:05 -0700242 unsigned long long size;
Simon Glassc45e3592013-03-11 06:49:53 +0000243 int i;
244
245 debug("\nRAM Configuration:\n");
246 for (i = size = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
247 size += gd->bd->bi_dram[i].size;
Bin Mengc8964482015-08-06 01:31:20 -0700248 debug("Bank #%d: %llx ", i,
249 (unsigned long long)(gd->bd->bi_dram[i].start));
Simon Glassc45e3592013-03-11 06:49:53 +0000250#ifdef DEBUG
251 print_size(gd->bd->bi_dram[i].size, "\n");
252#endif
253 }
254 debug("\nDRAM: ");
Simon Glassc45e3592013-03-11 06:49:53 +0000255
Pali Rohár8dc23ef2022-09-18 13:23:27 +0200256 print_size(gd->ram_size, "");
257 if (!sizes_near(gd->ram_size, size)) {
Neha Malcom Francis802e00e2025-03-19 19:33:26 +0530258 printf(" (total ");
Pali Rohár8dc23ef2022-09-18 13:23:27 +0200259 print_size(size, ")");
260 }
Simon Glass50250b52013-03-11 14:30:42 +0000261 board_add_ram_info(0);
262 putc('\n');
Simon Glassc45e3592013-03-11 06:49:53 +0000263
264 return 0;
265}
266
Simon Glass2f949c32017-03-31 08:40:32 -0600267__weak int dram_init_banksize(void)
Simon Glassc45e3592013-03-11 06:49:53 +0000268{
Stefan Roese90cda992020-08-12 13:02:39 +0200269 gd->bd->bi_dram[0].start = gd->ram_base;
Simon Glassc45e3592013-03-11 06:49:53 +0000270 gd->bd->bi_dram[0].size = get_effective_memsize();
Simon Glass2f949c32017-03-31 08:40:32 -0600271
272 return 0;
Simon Glassc45e3592013-03-11 06:49:53 +0000273}
274
Tom Rini52b2e262021-08-18 23:12:24 -0400275#if CONFIG_IS_ENABLED(SYS_I2C_LEGACY)
Simon Glass50250b52013-03-11 14:30:42 +0000276static int init_func_i2c(void)
277{
278 puts("I2C: ");
trema6612902013-09-21 18:13:34 +0200279 i2c_init_all();
Simon Glass50250b52013-03-11 14:30:42 +0000280 puts("ready\n");
281 return 0;
282}
283#endif
284
Simon Glassc45e3592013-03-11 06:49:53 +0000285static int setup_mon_len(void)
286{
Stefan Bosch26f51302024-01-26 12:50:55 +0000287#if defined(CONFIG_ARCH_NEXELL)
288 gd->mon_len = (ulong)__bss_end - (ulong)__image_copy_start;
289#elif defined(__ARM__) || defined(__MICROBLAZE__)
Shiji Yangeff11fa2023-08-03 09:47:17 +0800290 gd->mon_len = (ulong)__bss_end - (ulong)_start;
Simon Glass7e9f5882023-01-15 14:15:40 -0700291#elif defined(CONFIG_SANDBOX) && !defined(__riscv)
Shiji Yangeff11fa2023-08-03 09:47:17 +0800292 gd->mon_len = (ulong)_end - (ulong)_init;
Heinrich Schuchardte7301bb2021-05-19 12:02:39 +0200293#elif defined(CONFIG_SANDBOX)
Simon Glass7e9f5882023-01-15 14:15:40 -0700294 /* gcc does not provide _init in crti.o on RISC-V */
Heinrich Schuchardte7301bb2021-05-19 12:02:39 +0200295 gd->mon_len = 0;
296#elif defined(CONFIG_EFI_APP)
Shiji Yangeff11fa2023-08-03 09:47:17 +0800297 gd->mon_len = (ulong)_end - (ulong)_init;
Tom Rini210ebce2017-03-14 11:08:10 -0400298#elif defined(CONFIG_NIOS2) || defined(CONFIG_XTENSA)
Sonic Zhangf503a522014-07-17 19:01:34 +0800299 gd->mon_len = CONFIG_SYS_MONITOR_LEN;
Tom Rini53320122022-04-06 09:21:25 -0400300#elif defined(CONFIG_SH) || defined(CONFIG_RISCV)
Shiji Yangeff11fa2023-08-03 09:47:17 +0800301 gd->mon_len = (ulong)(__bss_end) - (ulong)(_start);
Simon Glass90632bd2016-05-14 18:49:28 -0600302#elif defined(CONFIG_SYS_MONITOR_BASE)
Shiji Yangeff11fa2023-08-03 09:47:17 +0800303 /* TODO: use (ulong)__bss_end - (ulong)__text_start; ? */
304 gd->mon_len = (ulong)__bss_end - CONFIG_SYS_MONITOR_BASE;
Simon Glass9c9f44a2013-03-11 07:06:48 +0000305#endif
Simon Glassc45e3592013-03-11 06:49:53 +0000306 return 0;
307}
308
Simon Glass1577b132024-12-01 07:42:35 -0700309static int setup_spl_handoff(void)
310{
311#if CONFIG_IS_ENABLED(HANDOFF)
312 gd->spl_handoff = bloblist_find(BLOBLISTT_U_BOOT_SPL_HANDOFF,
313 sizeof(struct spl_handoff));
314 debug("Found SPL hand-off info %p\n", gd->spl_handoff);
315#endif
316
317 return 0;
318}
319
Simon Glassc45e3592013-03-11 06:49:53 +0000320__weak int arch_cpu_init(void)
321{
322 return 0;
323}
324
Paul Burton1f508dd2016-09-21 11:18:46 +0100325__weak int mach_cpu_init(void)
326{
327 return 0;
328}
329
Simon Glassc45e3592013-03-11 06:49:53 +0000330/* Get the top of usable RAM */
Heinrich Schuchardt51a9aac2023-08-12 20:16:58 +0200331__weak phys_addr_t board_get_usable_ram_top(phys_size_t total_size)
Simon Glassc45e3592013-03-11 06:49:53 +0000332{
Tom Rinibb4dd962022-11-16 13:10:37 -0500333#if defined(CFG_SYS_SDRAM_BASE) && CFG_SYS_SDRAM_BASE > 0
Stephen Warren0ba4a8a2014-12-23 10:34:49 -0700334 /*
Simon Glass839855c2015-04-28 20:25:03 -0600335 * Detect whether we have so much RAM that it goes past the end of our
Stephen Warren0ba4a8a2014-12-23 10:34:49 -0700336 * 32-bit address space. If so, clip the usable RAM so it doesn't.
337 */
Tom Rinibb4dd962022-11-16 13:10:37 -0500338 if (gd->ram_top < CFG_SYS_SDRAM_BASE)
Stephen Warren0ba4a8a2014-12-23 10:34:49 -0700339 /*
340 * Will wrap back to top of 32-bit space when reservations
341 * are made.
342 */
343 return 0;
344#endif
Simon Glassc45e3592013-03-11 06:49:53 +0000345 return gd->ram_top;
346}
347
Ovidiu Panaitbbce5f32022-09-13 21:31:28 +0300348__weak int arch_setup_dest_addr(void)
349{
350 return 0;
351}
352
Simon Glassc45e3592013-03-11 06:49:53 +0000353static int setup_dest_addr(void)
354{
Simon Glass0b6675d2024-08-21 10:19:16 -0600355 debug("Monitor len: %08x\n", gd->mon_len);
Simon Glassc45e3592013-03-11 06:49:53 +0000356 /*
357 * Ram is setup, size stored in gd !!
358 */
Pali Rohárad37d422022-09-09 17:32:41 +0200359 debug("Ram size: %08llX\n", (unsigned long long)gd->ram_size);
Tom Rini5c1e7272022-04-06 10:33:32 -0400360#if CONFIG_VAL(SYS_MEM_TOP_HIDE)
Simon Glassc45e3592013-03-11 06:49:53 +0000361 /*
362 * Subtract specified amount of memory to hide so that it won't
363 * get "touched" at all by U-Boot. By fixing up gd->ram_size
364 * the Linux kernel should now get passed the now "corrected"
York Sun4de24ef2017-03-06 09:02:28 -0800365 * memory size and won't touch it either. This should work
366 * for arch/ppc and arch/powerpc. Only Linux board ports in
367 * arch/powerpc with bootwrapper support, that recalculate the
368 * memory size from the SDRAM controller setup will have to
369 * get fixed.
Simon Glassc45e3592013-03-11 06:49:53 +0000370 */
York Sun4de24ef2017-03-06 09:02:28 -0800371 gd->ram_size -= CONFIG_SYS_MEM_TOP_HIDE;
372#endif
Tom Rinibb4dd962022-11-16 13:10:37 -0500373#ifdef CFG_SYS_SDRAM_BASE
374 gd->ram_base = CFG_SYS_SDRAM_BASE;
Simon Glassc45e3592013-03-11 06:49:53 +0000375#endif
Siva Durga Prasad Paladugu94a1d522018-07-16 15:56:10 +0530376 gd->ram_top = gd->ram_base + get_effective_memsize();
Simon Glassc45e3592013-03-11 06:49:53 +0000377 gd->ram_top = board_get_usable_ram_top(gd->mon_len);
Masahiro Yamadad1589242013-05-27 00:37:30 +0000378 gd->relocaddr = gd->ram_top;
Pali Rohárad37d422022-09-09 17:32:41 +0200379 debug("Ram top: %08llX\n", (unsigned long long)gd->ram_top);
Ovidiu Panaitbbce5f32022-09-13 21:31:28 +0300380
381 return arch_setup_dest_addr();
Simon Glassc45e3592013-03-11 06:49:53 +0000382}
383
Tom Rini0bb9b092022-12-04 10:13:37 -0500384#ifdef CFG_PRAM
Simon Glassc45e3592013-03-11 06:49:53 +0000385/* reserve protected RAM */
386static int reserve_pram(void)
387{
388 ulong reg;
389
Tom Rini0bb9b092022-12-04 10:13:37 -0500390 reg = env_get_ulong("pram", 10, CFG_PRAM);
Masahiro Yamadad1589242013-05-27 00:37:30 +0000391 gd->relocaddr -= (reg << 10); /* size is in kB */
Simon Glassc45e3592013-03-11 06:49:53 +0000392 debug("Reserving %ldk for protected RAM at %08lx\n", reg,
Masahiro Yamadad1589242013-05-27 00:37:30 +0000393 gd->relocaddr);
Simon Glassc45e3592013-03-11 06:49:53 +0000394 return 0;
395}
Tom Rini0bb9b092022-12-04 10:13:37 -0500396#endif /* CFG_PRAM */
Simon Glassc45e3592013-03-11 06:49:53 +0000397
398/* Round memory pointer down to next 4 kB limit */
399static int reserve_round_4k(void)
400{
Masahiro Yamadad1589242013-05-27 00:37:30 +0000401 gd->relocaddr &= ~(4096 - 1);
Simon Glassc45e3592013-03-11 06:49:53 +0000402 return 0;
403}
404
Ovidiu Panait2a2941b2020-03-29 20:57:41 +0300405__weak int arch_reserve_mmu(void)
406{
407 return 0;
408}
409
Devarsh Thakkar46245d42023-12-05 21:25:19 +0530410static int reserve_video_from_videoblob(void)
Simon Glassfce58f52016-01-18 19:52:21 -0700411{
Simon Glassd4dce4a2024-09-29 19:49:36 -0600412 if (IS_ENABLED(CONFIG_SPL_VIDEO_HANDOFF) && xpl_phase() > PHASE_SPL) {
Nikhil M Jainf7ec5312023-07-18 14:27:31 +0530413 struct video_handoff *ho;
Devarsh Thakkar2febd462023-12-05 21:25:20 +0530414 int ret = 0;
Nikhil M Jainf7ec5312023-07-18 14:27:31 +0530415
416 ho = bloblist_find(BLOBLISTT_U_BOOT_VIDEO, sizeof(*ho));
417 if (!ho)
Devarsh Thakkar2febd462023-12-05 21:25:20 +0530418 return log_msg_ret("Missing video bloblist", -ENOENT);
419
420 ret = video_reserve_from_bloblist(ho);
421 if (ret)
422 return log_msg_ret("Invalid Video handoff info", ret);
Devarsh Thakkar46245d42023-12-05 21:25:19 +0530423
424 /* Sanity check fb from blob is before current relocaddr */
425 if (likely(gd->relocaddr > (unsigned long)ho->fb))
426 gd->relocaddr = ho->fb;
427 }
428
429 return 0;
430}
431
432/*
433 * Check if any bloblist received specifying reserved areas from previous stage and adjust
434 * gd->relocaddr accordingly, so that we start reserving after pre-reserved areas
435 * from previous stage.
436 *
437 * NOTE:
438 * IT is recommended that all bloblists from previous stage are reserved from ram_top
439 * as next stage will simply start reserving further regions after them.
440 */
441static int setup_relocaddr_from_bloblist(void)
442{
443 reserve_video_from_videoblob();
444
445 return 0;
446}
447
448static int reserve_video(void)
449{
450 if (CONFIG_IS_ENABLED(VIDEO)) {
Simon Glassb24a7d92022-10-16 15:57:41 -0600451 ulong addr;
452 int ret;
Simon Glassfce58f52016-01-18 19:52:21 -0700453
Simon Glassb24a7d92022-10-16 15:57:41 -0600454 addr = gd->relocaddr;
455 ret = video_reserve(&addr);
456 if (ret)
457 return ret;
458 debug("Reserving %luk for video at: %08lx\n",
459 ((unsigned long)gd->relocaddr - addr) >> 10, addr);
460 gd->relocaddr = addr;
461 }
Simon Glass50250b52013-03-11 14:30:42 +0000462
463 return 0;
464}
Simon Glass50250b52013-03-11 14:30:42 +0000465
Simon Glass1008da02016-01-18 19:52:20 -0700466static int reserve_trace(void)
467{
468#ifdef CONFIG_TRACE
469 gd->relocaddr -= CONFIG_TRACE_BUFFER_SIZE;
470 gd->trace_buff = map_sysmem(gd->relocaddr, CONFIG_TRACE_BUFFER_SIZE);
Heinrich Schuchardtc960b142019-06-14 21:52:22 +0200471 debug("Reserving %luk for trace data at: %08lx\n",
472 (unsigned long)CONFIG_TRACE_BUFFER_SIZE >> 10, gd->relocaddr);
Simon Glass1008da02016-01-18 19:52:20 -0700473#endif
474
475 return 0;
476}
477
Simon Glassc45e3592013-03-11 06:49:53 +0000478static int reserve_uboot(void)
479{
Alexey Brodkinc76af2a2018-05-25 16:08:14 +0300480 if (!(gd->flags & GD_FLG_SKIP_RELOC)) {
481 /*
482 * reserve memory for U-Boot code, data & bss
483 * round down to next 4 kB limit
484 */
485 gd->relocaddr -= gd->mon_len;
486 gd->relocaddr &= ~(4096 - 1);
487 #if defined(CONFIG_E500) || defined(CONFIG_MIPS)
488 /* round down to next 64 kB limit so that IVPR stays aligned */
489 gd->relocaddr &= ~(65536 - 1);
490 #endif
Simon Glassc45e3592013-03-11 06:49:53 +0000491
Simon Glass0b6675d2024-08-21 10:19:16 -0600492 debug("Reserving %dk for U-Boot at: %08lx\n",
Alexey Brodkinc76af2a2018-05-25 16:08:14 +0300493 gd->mon_len >> 10, gd->relocaddr);
494 }
Masahiro Yamadad1589242013-05-27 00:37:30 +0000495
496 gd->start_addr_sp = gd->relocaddr;
497
Simon Glassc45e3592013-03-11 06:49:53 +0000498 return 0;
499}
500
Patrick Delaunaye177cbc2020-03-10 10:15:05 +0100501/*
502 * reserve after start_addr_sp the requested size and make the stack pointer
503 * 16-byte aligned, this alignment is needed for cast on the reserved memory
504 * ref = x86_64 ABI: https://reviews.llvm.org/D30049: 16 bytes
505 * = ARMv8 Instruction Set Overview: quad word, 16 bytes
506 */
507static unsigned long reserve_stack_aligned(size_t size)
508{
509 return ALIGN_DOWN(gd->start_addr_sp - size, 16);
510}
511
Vikas Manocha4d49e102019-08-16 09:57:44 -0700512#ifdef CONFIG_SYS_NONCACHED_MEMORY
513static int reserve_noncached(void)
514{
Stephen Warren9b496432019-08-27 11:54:31 -0600515 /*
Simon Glassa9c12f12024-10-21 10:19:25 +0200516 * The value of gd->start_addr_sp must match the value of
517 * mem_malloc_start calculated in board_r.c:initr_malloc(), which is
518 * passed to dlmalloc.c:mem_malloc_init() and then used by
Stephen Warren9b496432019-08-27 11:54:31 -0600519 * cache.c:noncached_init()
520 *
521 * These calculations must match the code in cache.c:noncached_init()
522 */
523 gd->start_addr_sp = ALIGN(gd->start_addr_sp, MMU_SECTION_SIZE) -
524 MMU_SECTION_SIZE;
525 gd->start_addr_sp -= ALIGN(CONFIG_SYS_NONCACHED_MEMORY,
526 MMU_SECTION_SIZE);
Vikas Manocha4d49e102019-08-16 09:57:44 -0700527 debug("Reserving %dM for noncached_alloc() at: %08lx\n",
528 CONFIG_SYS_NONCACHED_MEMORY >> 20, gd->start_addr_sp);
529
530 return 0;
531}
532#endif
533
Simon Glassc45e3592013-03-11 06:49:53 +0000534/* reserve memory for malloc() area */
535static int reserve_malloc(void)
536{
Patrick Delaunaye177cbc2020-03-10 10:15:05 +0100537 gd->start_addr_sp = reserve_stack_aligned(TOTAL_MALLOC_LEN);
Simon Glassc45e3592013-03-11 06:49:53 +0000538 debug("Reserving %dk for malloc() at: %08lx\n",
Mario Six80b66dd2018-01-15 11:10:02 +0100539 TOTAL_MALLOC_LEN >> 10, gd->start_addr_sp);
Vikas Manocha4d49e102019-08-16 09:57:44 -0700540#ifdef CONFIG_SYS_NONCACHED_MEMORY
541 reserve_noncached();
542#endif
543
Simon Glassc45e3592013-03-11 06:49:53 +0000544 return 0;
545}
546
547/* (permanently) allocate a Board Info struct */
548static int reserve_board(void)
549{
Sonic Zhangf503a522014-07-17 19:01:34 +0800550 if (!gd->bd) {
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +0900551 gd->start_addr_sp = reserve_stack_aligned(sizeof(struct bd_info));
552 gd->bd = (struct bd_info *)map_sysmem(gd->start_addr_sp,
553 sizeof(struct bd_info));
554 memset(gd->bd, '\0', sizeof(struct bd_info));
Sonic Zhangf503a522014-07-17 19:01:34 +0800555 debug("Reserving %zu Bytes for Board Info at: %08lx\n",
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +0900556 sizeof(struct bd_info), gd->start_addr_sp);
Sonic Zhangf503a522014-07-17 19:01:34 +0800557 }
Simon Glassc45e3592013-03-11 06:49:53 +0000558 return 0;
559}
560
Simon Glassc45e3592013-03-11 06:49:53 +0000561static int reserve_global_data(void)
562{
Patrick Delaunaye177cbc2020-03-10 10:15:05 +0100563 gd->start_addr_sp = reserve_stack_aligned(sizeof(gd_t));
Masahiro Yamadad1589242013-05-27 00:37:30 +0000564 gd->new_gd = (gd_t *)map_sysmem(gd->start_addr_sp, sizeof(gd_t));
Simon Glassc45e3592013-03-11 06:49:53 +0000565 debug("Reserving %zu Bytes for Global Data at: %08lx\n",
Mario Six80b66dd2018-01-15 11:10:02 +0100566 sizeof(gd_t), gd->start_addr_sp);
Simon Glassc45e3592013-03-11 06:49:53 +0000567 return 0;
568}
569
570static int reserve_fdt(void)
571{
Ovidiu Panaitb6225b52020-11-28 10:43:07 +0200572 if (!IS_ENABLED(CONFIG_OF_EMBED)) {
573 /*
574 * If the device tree is sitting immediately above our image
575 * then we must relocate it. If it is embedded in the data
576 * section, then it will be relocated with other data.
577 */
578 if (gd->fdt_blob) {
Simon Glass00f860f2024-08-21 10:19:10 -0600579 gd->boardf->fdt_size =
580 ALIGN(fdt_totalsize(gd->fdt_blob), 32);
Simon Glassc45e3592013-03-11 06:49:53 +0000581
Simon Glass00f860f2024-08-21 10:19:10 -0600582 gd->start_addr_sp = reserve_stack_aligned(
583 gd->boardf->fdt_size);
584 gd->boardf->new_fdt = map_sysmem(gd->start_addr_sp,
585 gd->boardf->fdt_size);
Ovidiu Panaitb6225b52020-11-28 10:43:07 +0200586 debug("Reserving %lu Bytes for FDT at: %08lx\n",
Simon Glass00f860f2024-08-21 10:19:10 -0600587 gd->boardf->fdt_size, gd->start_addr_sp);
Ovidiu Panaitb6225b52020-11-28 10:43:07 +0200588 }
Simon Glassc45e3592013-03-11 06:49:53 +0000589 }
590
591 return 0;
592}
593
Simon Glassb9aff922017-05-22 05:05:30 -0600594static int reserve_bootstage(void)
595{
596#ifdef CONFIG_BOOTSTAGE
Simon Glassa036dbb2024-10-21 10:19:28 +0200597 int size = bootstage_get_size(true);
Simon Glassb9aff922017-05-22 05:05:30 -0600598
Patrick Delaunaye177cbc2020-03-10 10:15:05 +0100599 gd->start_addr_sp = reserve_stack_aligned(size);
Simon Glassd5d6f682024-08-21 10:19:11 -0600600 gd->boardf->new_bootstage = map_sysmem(gd->start_addr_sp, size);
Simon Glassb9aff922017-05-22 05:05:30 -0600601 debug("Reserving %#x Bytes for bootstage at: %08lx\n", size,
602 gd->start_addr_sp);
603#endif
604
605 return 0;
606}
607
Patrick Delaunaya0a2b212018-03-13 13:57:00 +0100608__weak int arch_reserve_stacks(void)
Simon Glassc45e3592013-03-11 06:49:53 +0000609{
Andreas Bießmann25429862015-02-06 23:06:45 +0100610 return 0;
611}
Simon Glass4d2aee82013-03-05 14:39:45 +0000612
Andreas Bießmann25429862015-02-06 23:06:45 +0100613static int reserve_stacks(void)
614{
615 /* make stack pointer 16-byte aligned */
Patrick Delaunaye177cbc2020-03-10 10:15:05 +0100616 gd->start_addr_sp = reserve_stack_aligned(16);
Simon Glassc45e3592013-03-11 06:49:53 +0000617
618 /*
Simon Glass839855c2015-04-28 20:25:03 -0600619 * let the architecture-specific code tailor gd->start_addr_sp and
Andreas Bießmann25429862015-02-06 23:06:45 +0100620 * gd->irq_sp
Simon Glassc45e3592013-03-11 06:49:53 +0000621 */
Andreas Bießmann25429862015-02-06 23:06:45 +0100622 return arch_reserve_stacks();
Simon Glassc45e3592013-03-11 06:49:53 +0000623}
624
Simon Glassa815dab2018-11-15 18:43:52 -0700625static int reserve_bloblist(void)
626{
627#ifdef CONFIG_BLOBLIST
Raymond Maoba72dd12025-02-19 16:02:19 -0800628 ulong size = bloblist_get_total_size();
629
630 if (size < CONFIG_BLOBLIST_SIZE_RELOC)
631 size = CONFIG_BLOBLIST_SIZE_RELOC;
632
Simon Glass9e945052020-09-27 18:46:18 -0600633 /* Align to a 4KB boundary for easier reading of addresses */
Raymond Maoba72dd12025-02-19 16:02:19 -0800634 gd->start_addr_sp = ALIGN_DOWN(gd->start_addr_sp - size, 0x1000);
635 gd->boardf->new_bloblist = map_sysmem(gd->start_addr_sp, size);
Simon Glassa815dab2018-11-15 18:43:52 -0700636#endif
637
638 return 0;
639}
640
Simon Glassc45e3592013-03-11 06:49:53 +0000641static int display_new_sp(void)
642{
Masahiro Yamadad1589242013-05-27 00:37:30 +0000643 debug("New Stack Pointer is: %08lx\n", gd->start_addr_sp);
Simon Glassc45e3592013-03-11 06:49:53 +0000644
645 return 0;
646}
647
Ovidiu Panait3d0b0402020-07-24 14:12:15 +0300648__weak int arch_setup_bdinfo(void)
Ovidiu Panait0c5e9a02020-07-24 14:12:14 +0300649{
650 return 0;
651}
652
Ovidiu Panait3d0b0402020-07-24 14:12:15 +0300653int setup_bdinfo(void)
654{
655 return arch_setup_bdinfo();
656}
657
Simon Glassc45e3592013-03-11 06:49:53 +0000658#ifdef CONFIG_POST
659static int init_post(void)
660{
661 post_bootmode_init();
662 post_run(NULL, POST_ROM | post_bootmode_get(0));
663
664 return 0;
665}
666#endif
667
Simon Glassc45e3592013-03-11 06:49:53 +0000668static int reloc_fdt(void)
669{
Ovidiu Panaitb6225b52020-11-28 10:43:07 +0200670 if (!IS_ENABLED(CONFIG_OF_EMBED)) {
Simon Glass7f67b372024-08-21 10:19:09 -0600671 if (gd->boardf->new_fdt) {
672 memcpy(gd->boardf->new_fdt, gd->fdt_blob,
Ovidiu Panaitb6225b52020-11-28 10:43:07 +0200673 fdt_totalsize(gd->fdt_blob));
Simon Glass7f67b372024-08-21 10:19:09 -0600674 gd->fdt_blob = gd->boardf->new_fdt;
Ovidiu Panaitb6225b52020-11-28 10:43:07 +0200675 }
Simon Glassc45e3592013-03-11 06:49:53 +0000676 }
677
678 return 0;
679}
680
Simon Glassb9aff922017-05-22 05:05:30 -0600681static int reloc_bootstage(void)
682{
683#ifdef CONFIG_BOOTSTAGE
684 if (gd->flags & GD_FLG_SKIP_RELOC)
685 return 0;
Simon Glassd5d6f682024-08-21 10:19:11 -0600686 if (gd->boardf->new_bootstage)
687 bootstage_relocate(gd->boardf->new_bootstage);
Simon Glassb9aff922017-05-22 05:05:30 -0600688#endif
689
690 return 0;
691}
692
Simon Glassa815dab2018-11-15 18:43:52 -0700693static int reloc_bloblist(void)
694{
695#ifdef CONFIG_BLOBLIST
Simon Glass5d2199d2021-11-03 21:09:20 -0600696 /*
697 * Relocate only if we are supposed to send it
698 */
699 if ((gd->flags & GD_FLG_SKIP_RELOC) &&
700 CONFIG_BLOBLIST_SIZE == CONFIG_BLOBLIST_SIZE_RELOC) {
701 debug("Not relocating bloblist\n");
Simon Glassa815dab2018-11-15 18:43:52 -0700702 return 0;
Simon Glass5d2199d2021-11-03 21:09:20 -0600703 }
Simon Glassda0eeb82024-08-21 10:19:12 -0600704 if (gd->boardf->new_bloblist) {
Raymond Maoba72dd12025-02-19 16:02:19 -0800705 ulong size = bloblist_get_total_size();
706
707 if (size < CONFIG_BLOBLIST_SIZE_RELOC)
708 size = CONFIG_BLOBLIST_SIZE_RELOC;
709
710 debug("Copying bloblist from %p to %p, size %lx\n",
711 gd->bloblist, gd->boardf->new_bloblist, size);
712 return bloblist_reloc(gd->boardf->new_bloblist, size);
Simon Glassa815dab2018-11-15 18:43:52 -0700713 }
714#endif
715
716 return 0;
717}
718
Eugene Urieve84d69a2024-03-31 23:03:25 +0300719void mcheck_on_ramrelocation(size_t offset);
Simon Glassc45e3592013-03-11 06:49:53 +0000720static int setup_reloc(void)
721{
Marek Vasut8c4a68e2021-11-13 18:34:04 +0100722 if (!(gd->flags & GD_FLG_SKIP_RELOC)) {
Simon Glass72cc5382022-10-20 18:22:39 -0600723#ifdef CONFIG_TEXT_BASE
Lothar Waßmann160583b2017-06-08 10:18:25 +0200724#ifdef ARM
Marek Vasut8c4a68e2021-11-13 18:34:04 +0100725 gd->reloc_off = gd->relocaddr - (unsigned long)__image_copy_start;
Michal Simekf942ebb2022-06-24 14:15:01 +0200726#elif defined(CONFIG_MICROBLAZE)
727 gd->reloc_off = gd->relocaddr - (u32)_start;
Lothar Waßmann160583b2017-06-08 10:18:25 +0200728#elif defined(CONFIG_M68K)
Marek Vasut8c4a68e2021-11-13 18:34:04 +0100729 /*
730 * On all ColdFire arch cpu, monitor code starts always
731 * just after the default vector table location, so at 0x400
732 */
Simon Glass72cc5382022-10-20 18:22:39 -0600733 gd->reloc_off = gd->relocaddr - (CONFIG_TEXT_BASE + 0x400);
Simon Glass752707a2019-04-08 13:20:41 -0600734#elif !defined(CONFIG_SANDBOX)
Simon Glass72cc5382022-10-20 18:22:39 -0600735 gd->reloc_off = gd->relocaddr - CONFIG_TEXT_BASE;
angelo@sysam.itf245ae92015-02-12 01:40:17 +0100736#endif
Sonic Zhangf503a522014-07-17 19:01:34 +0800737#endif
Marek Vasut8c4a68e2021-11-13 18:34:04 +0100738 }
739
Simon Glassc45e3592013-03-11 06:49:53 +0000740 memcpy(gd->new_gd, (char *)gd, sizeof(gd_t));
741
Marek Vasut8c4a68e2021-11-13 18:34:04 +0100742 if (gd->flags & GD_FLG_SKIP_RELOC) {
743 debug("Skipping relocation due to flag\n");
744 } else {
Eugene Urieve84d69a2024-03-31 23:03:25 +0300745#ifdef MCHECK_HEAP_PROTECTION
746 mcheck_on_ramrelocation(gd->reloc_off);
747#endif
Marek Vasut8c4a68e2021-11-13 18:34:04 +0100748 debug("Relocation Offset is: %08lx\n", gd->reloc_off);
749 debug("Relocating to %08lx, new gd at %08lx, sp at %08lx\n",
750 gd->relocaddr, (ulong)map_to_sysmem(gd->new_gd),
751 gd->start_addr_sp);
752 }
Simon Glassc45e3592013-03-11 06:49:53 +0000753
754 return 0;
755}
756
Jerome Forissier1fd58d42025-04-04 15:50:36 +0200757#if CONFIG_IS_ENABLED(OF_BOARD_FIXUP)
mario.six@gdsys.cc7e9b9d62017-02-22 16:07:22 +0100758static int fix_fdt(void)
759{
760 return board_fix_fdt((void *)gd->fdt_blob);
761}
762#endif
763
Simon Glassc45e3592013-03-11 06:49:53 +0000764/* ARM calls relocate_code from its crt0.S */
Simon Glasse6b03502023-07-15 21:38:52 -0600765#if !defined(CONFIG_ARM) && !defined(CONFIG_SANDBOX)
Simon Glassc45e3592013-03-11 06:49:53 +0000766
767static int jump_to_copy(void)
768{
Simon Glass00dd17a2015-08-04 12:33:39 -0600769 if (gd->flags & GD_FLG_SKIP_RELOC)
770 return 0;
Simon Glass6d179872013-03-05 14:39:52 +0000771 /*
772 * x86 is special, but in a nice way. It uses a trampoline which
773 * enables the dcache if possible.
774 *
775 * For now, other archs use relocate_code(), which is implemented
776 * similarly for all archs. When we do generic relocation, hopefully
777 * we can make all archs enable the dcache prior to relocation.
778 */
Alexey Brodkin913e9f02015-02-24 19:40:36 +0300779#if defined(CONFIG_X86) || defined(CONFIG_ARC)
Simon Glass6d179872013-03-05 14:39:52 +0000780 /*
781 * SDRAM and console are now initialised. The final stack can now
782 * be setup in SDRAM. Code execution will continue in Flash, but
783 * with the stack in SDRAM and Global Data in temporary memory
784 * (CPU cache)
785 */
Simon Glass0e27b872015-08-10 20:44:32 -0600786 arch_setup_gd(gd->new_gd);
Simon Glasse6b03502023-07-15 21:38:52 -0600787# if CONFIG_IS_ENABLED(X86_64)
788 board_init_f_r_trampoline64(gd->new_gd, gd->start_addr_sp);
789# else
790 board_init_f_r_trampoline(gd->start_addr_sp);
791# endif
Simon Glass6d179872013-03-05 14:39:52 +0000792#else
Masahiro Yamadad1589242013-05-27 00:37:30 +0000793 relocate_code(gd->start_addr_sp, gd->new_gd, gd->relocaddr);
Simon Glass6d179872013-03-05 14:39:52 +0000794#endif
Simon Glassc45e3592013-03-11 06:49:53 +0000795
796 return 0;
797}
798#endif
799
800/* Record the board_init_f() bootstage (after arch_cpu_init()) */
Simon Glass88200332017-05-22 05:05:25 -0600801static int initf_bootstage(void)
Simon Glassc45e3592013-03-11 06:49:53 +0000802{
Simon Glassc55d5c32017-06-07 10:28:46 -0600803 bool from_spl = IS_ENABLED(CONFIG_SPL_BOOTSTAGE) &&
804 IS_ENABLED(CONFIG_BOOTSTAGE_STASH);
Simon Glass88200332017-05-22 05:05:25 -0600805 int ret;
806
Simon Glass01154cb2017-05-22 05:05:35 -0600807 ret = bootstage_init(!from_spl);
Simon Glass88200332017-05-22 05:05:25 -0600808 if (ret)
809 return ret;
Simon Glass01154cb2017-05-22 05:05:35 -0600810 if (from_spl) {
Jonas Karlmanca052692024-08-03 12:41:44 +0000811 ret = bootstage_unstash_default();
Simon Glass01154cb2017-05-22 05:05:35 -0600812 if (ret && ret != -ENOENT) {
813 debug("Failed to unstash bootstage: err=%d\n", ret);
814 return ret;
815 }
816 }
Simon Glass88200332017-05-22 05:05:25 -0600817
Simon Glassc45e3592013-03-11 06:49:53 +0000818 bootstage_mark_name(BOOTSTAGE_ID_START_UBOOT_F, "board_init_f");
819
820 return 0;
821}
822
Simon Glassa730c5d2014-07-23 06:55:04 -0600823static int initf_dm(void)
824{
Simon Glassa730c5d2014-07-23 06:55:04 -0600825 int ret;
826
Simon Glass9ab26112024-11-20 08:36:39 -0700827 if (!CONFIG_IS_ENABLED(SYS_MALLOC_F))
828 return 0;
829
Simon Glassea6a6092020-05-10 11:39:59 -0600830 bootstage_start(BOOTSTAGE_ID_ACCUM_DM_F, "dm_f");
Simon Glassa730c5d2014-07-23 06:55:04 -0600831 ret = dm_init_and_scan(true);
832 if (ret)
833 return ret;
Ovidiu Panait525a2ec2020-11-28 10:43:05 +0200834
Simon Glass775fc112024-11-20 08:36:41 -0700835 ret = dm_autoprobe();
836 if (ret)
837 return ret;
Simon Glass079ef952025-02-26 09:26:14 -0700838 bootstage_accum(BOOTSTAGE_ID_ACCUM_DM_F);
Simon Glass775fc112024-11-20 08:36:41 -0700839
Ovidiu Panait525a2ec2020-11-28 10:43:05 +0200840 if (IS_ENABLED(CONFIG_TIMER_EARLY)) {
841 ret = dm_timer_init();
842 if (ret)
843 return ret;
844 }
Simon Glassa730c5d2014-07-23 06:55:04 -0600845
846 return 0;
847}
848
Simon Glass5ded7e52015-01-19 22:16:12 -0700849/* Architecture-specific memory reservation */
850__weak int reserve_arch(void)
851{
852 return 0;
853}
854
Ovidiu Panait8e0319f2020-01-22 22:28:25 +0200855__weak int checkcpu(void)
856{
857 return 0;
858}
859
Ovidiu Panaitc508b272020-02-05 08:54:42 +0200860__weak int clear_bss(void)
861{
862 return 0;
863}
864
Simon Glass45aec8e2024-08-07 16:47:34 -0600865static int initf_upl(void)
866{
867 struct upl *upl;
868 int ret;
869
870 if (!IS_ENABLED(CONFIG_UPL_IN) || !(gd->flags & GD_FLG_UPL))
871 return 0;
872
873 upl = malloc(sizeof(struct upl));
874 if (upl)
875 ret = upl_read_handoff(upl, oftree_default());
876 if (ret) {
877 printf("UPL handoff: read failure (err=%dE)\n", ret);
878 return ret;
879 }
880 gd_set_upl(upl);
881
882 return 0;
883}
884
Jerome Forissier1fd58d42025-04-04 15:50:36 +0200885static void initcall_run_f(void)
886{
887 /*
888 * Please do not add logic to this function (variables, if (), etc.).
889 * For simplicity it should remain an ordered list of function calls.
890 */
891 INITCALL(setup_mon_len);
892#if CONFIG_IS_ENABLED(OF_CONTROL)
893 INITCALL(fdtdec_setup);
Simon Glass26b78b22015-02-27 22:06:34 -0700894#endif
Jerome Forissier1fd58d42025-04-04 15:50:36 +0200895#if CONFIG_IS_ENABLED(TRACE_EARLY)
896 INITCALL(trace_early_init);
Kevin Hilman676f0192014-12-09 15:03:58 -0800897#endif
Jerome Forissier1fd58d42025-04-04 15:50:36 +0200898 INITCALL(initf_malloc);
899 INITCALL(initf_upl);
900 INITCALL(log_init);
901 INITCALL(initf_bootstage); /* uses its own timer, so does not need DM */
902 INITCALL(event_init);
903 INITCALL(bloblist_maybe_init);
904 INITCALL(setup_spl_handoff);
905#if CONFIG_IS_ENABLED(CONSOLE_RECORD_INIT_F)
906 INITCALL(console_record_init);
Ovidiu Panait85a31ac2020-11-28 10:43:04 +0200907#endif
Jerome Forissier1fd58d42025-04-04 15:50:36 +0200908 INITCALL_EVT(EVT_FSP_INIT_F);
909 INITCALL(arch_cpu_init); /* basic arch cpu dependent setup */
910 INITCALL(mach_cpu_init); /* SoC/machine dependent CPU setup */
911 INITCALL(initf_dm);
912#if CONFIG_IS_ENABLED(BOARD_EARLY_INIT_F)
913 INITCALL(board_early_init_f);
Simon Glassc45e3592013-03-11 06:49:53 +0000914#endif
Simon Glass6829d8c2017-03-28 10:27:26 -0600915#if defined(CONFIG_PPC) || defined(CONFIG_SYS_FSL_CLK) || defined(CONFIG_M68K)
Simon Glass70064a72017-03-28 10:27:19 -0600916 /* get CPU and bus clocks according to the environment variable */
Jerome Forissier1fd58d42025-04-04 15:50:36 +0200917 INITCALL(get_clocks); /* get CPU and bus clocks (etc.) */
Simon Glasse8d20d42017-03-28 10:27:23 -0600918#endif
Marek Vasut4c77f062023-03-23 01:20:40 +0100919#if !defined(CONFIG_M68K) || (defined(CONFIG_M68K) && !defined(CONFIG_MCFTMR))
Jerome Forissier1fd58d42025-04-04 15:50:36 +0200920 INITCALL(timer_init); /* initialize timer */
Angelo Dureghellocd226852017-05-10 23:58:06 +0200921#endif
Jerome Forissier1fd58d42025-04-04 15:50:36 +0200922#if CONFIG_IS_ENABLED(BOARD_POSTCLK_INIT)
923 INITCALL(board_postclk_init);
Simon Glass50250b52013-03-11 14:30:42 +0000924#endif
Jerome Forissier1fd58d42025-04-04 15:50:36 +0200925 INITCALL(env_init); /* initialize environment */
926 INITCALL(init_baud_rate); /* initialze baudrate settings */
927 INITCALL(serial_init); /* serial communications setup */
928 INITCALL(console_init_f); /* stage 1 init of console */
929 INITCALL(display_options); /* say that we are here */
930 INITCALL(display_text_info); /* show debugging info if required */
931 INITCALL(checkcpu);
932#if CONFIG_IS_ENABLED(SYSRESET)
933 INITCALL(print_resetinfo);
Mario Six4481a5d2018-08-06 10:23:34 +0200934#endif
Jerome Forissier1fd58d42025-04-04 15:50:36 +0200935 /* display cpu info (and speed) */
936#if CONFIG_IS_ENABLED(DISPLAY_CPUINFO)
937 INITCALL(print_cpuinfo);
Simon Glass68c1d012017-01-23 13:31:25 -0700938#endif
Jerome Forissier1fd58d42025-04-04 15:50:36 +0200939#if CONFIG_IS_ENABLED(DTB_RESELECT)
940 INITCALL(embedded_dtb_select);
Cooper Jr., Franklind8b354a2017-06-16 17:25:12 -0500941#endif
Jerome Forissier1fd58d42025-04-04 15:50:36 +0200942#if CONFIG_IS_ENABLED(DISPLAY_BOARDINFO)
943 INITCALL(show_board_info);
Simon Glassc45e3592013-03-11 06:49:53 +0000944#endif
Jerome Forissier1fd58d42025-04-04 15:50:36 +0200945 WATCHDOG_INIT();
946 INITCALL_EVT(EVT_MISC_INIT_F);
947 WATCHDOG_RESET();
Tom Rini52b2e262021-08-18 23:12:24 -0400948#if CONFIG_IS_ENABLED(SYS_I2C_LEGACY)
Jerome Forissier1fd58d42025-04-04 15:50:36 +0200949 INITCALL(init_func_i2c);
Simon Glass50250b52013-03-11 14:30:42 +0000950#endif
Jerome Forissier1fd58d42025-04-04 15:50:36 +0200951 INITCALL(announce_dram_init);
952 INITCALL(dram_init); /* configure available RAM banks */
953#if CONFIG_IS_ENABLED(POST)
954 INITCALL(post_init_f);
Simon Glass50250b52013-03-11 14:30:42 +0000955#endif
Jerome Forissier1fd58d42025-04-04 15:50:36 +0200956 WATCHDOG_RESET();
Tom Rini6a5dccc2022-11-16 13:10:41 -0500957#if defined(CFG_SYS_DRAM_TEST)
Jerome Forissier1fd58d42025-04-04 15:50:36 +0200958 INITCALL(testdram);
Tom Rini6a5dccc2022-11-16 13:10:41 -0500959#endif /* CFG_SYS_DRAM_TEST */
Jerome Forissier1fd58d42025-04-04 15:50:36 +0200960 WATCHDOG_RESET();
961#if CONFIG_IS_ENABLED(POST)
962 INITCALL(init_post);
Simon Glassc45e3592013-03-11 06:49:53 +0000963#endif
Jerome Forissier1fd58d42025-04-04 15:50:36 +0200964 WATCHDOG_RESET();
Simon Glassc45e3592013-03-11 06:49:53 +0000965 /*
966 * Now that we have DRAM mapped and working, we can
967 * relocate the code and continue running from DRAM.
968 *
969 * Reserve memory at end of RAM for (top down in that order):
970 * - area that won't get touched by U-Boot and Linux (optional)
971 * - kernel log buffer
972 * - protected RAM
973 * - LCD framebuffer
974 * - monitor code
975 * - board info struct
976 */
Jerome Forissier1fd58d42025-04-04 15:50:36 +0200977 INITCALL(setup_dest_addr);
978#if CONFIG_IS_ENABLED(OF_BOARD_FIXUP) && \
979 !CONFIG_IS_ENABLED(OF_INITIAL_DTB_READONLY)
980 INITCALL(fix_fdt);
Pragnesh Patelad51fec2020-08-13 10:12:26 +0530981#endif
Tom Rini0bb9b092022-12-04 10:13:37 -0500982#ifdef CFG_PRAM
Jerome Forissier1fd58d42025-04-04 15:50:36 +0200983 INITCALL(reserve_pram);
Simon Glassc45e3592013-03-11 06:49:53 +0000984#endif
Jerome Forissier1fd58d42025-04-04 15:50:36 +0200985 INITCALL(reserve_round_4k);
986 INITCALL(setup_relocaddr_from_bloblist);
987 INITCALL(arch_reserve_mmu);
988 INITCALL(reserve_video);
989 INITCALL(reserve_trace);
990 INITCALL(reserve_uboot);
991 INITCALL(reserve_malloc);
992 INITCALL(reserve_board);
993 INITCALL(reserve_global_data);
994 INITCALL(reserve_fdt);
995#if CONFIG_IS_ENABLED(OF_BOARD_FIXUP) && \
996 CONFIG_IS_ENABLED(OF_INITIAL_DTB_READONLY)
997 INITCALL(reloc_fdt);
998 INITCALL(fix_fdt);
Pali Rohár64a15c82024-06-06 18:33:21 +0200999#endif
Jerome Forissier1fd58d42025-04-04 15:50:36 +02001000 INITCALL(reserve_bootstage);
1001 INITCALL(reserve_bloblist);
1002 INITCALL(reserve_arch);
1003 INITCALL(reserve_stacks);
1004 INITCALL(dram_init_banksize);
1005 INITCALL(show_dram_config);
1006 WATCHDOG_RESET();
1007 INITCALL(setup_bdinfo);
1008 INITCALL(display_new_sp);
1009 WATCHDOG_RESET();
1010#if !CONFIG_IS_ENABLED(OF_BOARD_FIXUP) || \
1011 !CONFIG_IS_ENABLED(INITIAL_DTB_READONLY)
1012 INITCALL(reloc_fdt);
Pali Rohár64a15c82024-06-06 18:33:21 +02001013#endif
Jerome Forissier1fd58d42025-04-04 15:50:36 +02001014 INITCALL(reloc_bootstage);
1015 INITCALL(reloc_bloblist);
1016 INITCALL(setup_reloc);
1017#if CONFIG_IS_ENABLED(X86) || CONFIG_IS_ENABLED(ARC)
1018 INITCALL(copy_uboot_to_ram);
1019 INITCALL(do_elf_reloc_fixups);
Simon Glassd50b2f42015-01-01 16:18:09 -07001020#endif
Jerome Forissier1fd58d42025-04-04 15:50:36 +02001021 INITCALL(clear_bss);
Rasmus Villemoesc794e492022-10-28 13:50:54 +02001022 /*
1023 * Deregister all cyclic functions before relocation, so that
1024 * gd->cyclic_list does not contain any references to pre-relocation
1025 * devices. Drivers will register their cyclic functions anew when the
1026 * devices are probed again.
1027 *
1028 * This should happen as late as possible so that the window where a
1029 * watchdog device is not serviced is as small as possible.
1030 */
Jerome Forissier1fd58d42025-04-04 15:50:36 +02001031 INITCALL(cyclic_unregister_all);
1032#if !CONFIG_IS_ENABLED(ARM) && !CONFIG_IS_ENABLED(SANDBOX)
1033 INITCALL(jump_to_copy);
Simon Glassc45e3592013-03-11 06:49:53 +00001034#endif
Jerome Forissier1fd58d42025-04-04 15:50:36 +02001035}
Simon Glassc45e3592013-03-11 06:49:53 +00001036
1037void board_init_f(ulong boot_flags)
1038{
Simon Glass7f67b372024-08-21 10:19:09 -06001039 struct board_f boardf;
1040
Simon Glassc45e3592013-03-11 06:49:53 +00001041 gd->flags = boot_flags;
Simon Glassd4b0fdb2024-08-21 10:19:04 -06001042 gd->flags &= ~GD_FLG_HAVE_CONSOLE;
Simon Glass7f67b372024-08-21 10:19:09 -06001043 gd->boardf = &boardf;
Simon Glassc45e3592013-03-11 06:49:53 +00001044
Jerome Forissier1fd58d42025-04-04 15:50:36 +02001045 initcall_run_f();
Simon Glassc45e3592013-03-11 06:49:53 +00001046
Ben Stoltz1930e8d2015-07-31 09:31:37 -06001047#if !defined(CONFIG_ARM) && !defined(CONFIG_SANDBOX) && \
Alexey Brodkinc157ab92015-12-16 19:24:10 +03001048 !defined(CONFIG_EFI_APP) && !CONFIG_IS_ENABLED(X86_64) && \
1049 !defined(CONFIG_ARC)
Simon Glassc45e3592013-03-11 06:49:53 +00001050 /* NOTREACHED - jump_to_copy() does not return */
1051 hang();
1052#endif
1053}
Simon Glass6d179872013-03-05 14:39:52 +00001054
Alexey Brodkin913e9f02015-02-24 19:40:36 +03001055#if defined(CONFIG_X86) || defined(CONFIG_ARC)
Simon Glass6d179872013-03-05 14:39:52 +00001056/*
1057 * For now this code is only used on x86.
1058 *
Jerome Forissier1fd58d42025-04-04 15:50:36 +02001059 * Run init functions which are run when U-Boot is executing from Flash with a
1060 * semi-limited 'C' environment.
Simon Glass6d179872013-03-05 14:39:52 +00001061 * The following limitations must be considered when implementing an
1062 * '_f_r' function:
1063 * - 'static' variables are read-only
1064 * - Global Data (gd->xxx) is read/write
1065 *
1066 * The '_f_r' sequence must, as a minimum, copy U-Boot to RAM (if
1067 * supported). It _should_, if possible, copy global data to RAM and
1068 * initialise the CPU caches (to speed up the relocation process)
1069 *
1070 * NOTE: At present only x86 uses this route, but it is intended that
1071 * all archs will move to this when generic relocation is implemented.
1072 */
Jerome Forissier1fd58d42025-04-04 15:50:36 +02001073static void initcall_run_f_r(void)
1074{
1075#if CONFIG_IS_ENABLED(X86_64)
1076 INITCALL(init_cache_f_r);
Simon Glass6e1a81a2017-01-16 07:03:49 -07001077#endif
Jerome Forissier1fd58d42025-04-04 15:50:36 +02001078}
Simon Glass6d179872013-03-05 14:39:52 +00001079
1080void board_init_f_r(void)
1081{
Jerome Forissier1fd58d42025-04-04 15:50:36 +02001082 initcall_run_f_r();
Simon Glass6d179872013-03-05 14:39:52 +00001083
1084 /*
Simon Glass51f73f12016-03-11 22:06:51 -07001085 * The pre-relocation drivers may be using memory that has now gone
1086 * away. Mark serial as unavailable - this will fall back to the debug
1087 * UART if available.
Simon Glass55e32ba2017-12-04 13:48:28 -07001088 *
1089 * Do the same with log drivers since the memory may not be available.
Simon Glass51f73f12016-03-11 22:06:51 -07001090 */
Simon Glass55e32ba2017-12-04 13:48:28 -07001091 gd->flags &= ~(GD_FLG_SERIAL_READY | GD_FLG_LOG_READY);
Simon Glassb77fe1f2017-09-05 19:49:45 -06001092#ifdef CONFIG_TIMER
1093 gd->timer = NULL;
1094#endif
Simon Glass51f73f12016-03-11 22:06:51 -07001095
1096 /*
Simon Glass6d179872013-03-05 14:39:52 +00001097 * U-Boot has been copied into SDRAM, the BSS has been cleared etc.
1098 * Transfer execution from Flash to RAM by calculating the address
1099 * of the in-RAM copy of board_init_r() and calling it
1100 */
Alexey Brodkin9c832f12015-02-25 17:59:02 +03001101 (board_init_r + gd->reloc_off)((gd_t *)gd, gd->relocaddr);
Simon Glass6d179872013-03-05 14:39:52 +00001102
1103 /* NOTREACHED - board_init_r() does not return */
1104 hang();
1105}
Alexey Brodkin73503182015-03-24 11:12:47 +03001106#endif /* CONFIG_X86 */