blob: add544da550ad33e7fdb1e84acd23eaa86261183 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glassdb397fa2013-03-11 14:30:37 +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 Glassdb397fa2013-03-11 14:30:37 +000010 */
11
12#include <common.h>
Simon Glassac53a5c2017-05-17 08:22:40 -060013#include <api.h>
Simon Glass1ea97892020-05-10 11:40:00 -060014#include <bootstage.h>
Simon Glass63334482019-11-14 12:57:39 -070015#include <cpu_func.h>
Simon Glass6ba03922019-12-28 10:44:46 -070016#include <exports.h>
Simon Glass8e201882020-05-10 11:39:54 -060017#include <flash.h>
Simon Glassf11478f2019-12-28 10:45:07 -070018#include <hang.h>
Simon Glass85f13782019-12-28 10:45:03 -070019#include <image.h>
Simon Glass8f3f7612019-11-14 12:57:42 -070020#include <irq_func.h>
Simon Glass0f2af882020-05-10 11:40:05 -060021#include <log.h>
Simon Glass0c364412019-12-28 10:44:48 -070022#include <net.h>
Simon Glass274e0b02020-05-10 11:39:56 -060023#include <asm/cache.h>
Simon Glass48b6c6b2019-11-14 12:57:16 -070024#include <u-boot/crc.h>
Simon Glass5af29bd2013-03-14 07:20:12 +000025/* TODO: can we just include all these headers whether needed or not? */
26#if defined(CONFIG_CMD_BEDBUG)
27#include <bedbug/type.h>
28#endif
Simon Glassdd7fb9b2019-12-06 21:41:34 -070029#include <binman.h>
Tom Rini23415e52015-12-07 08:23:29 -050030#include <command.h>
Simon Glassa73bda42015-11-08 23:47:45 -070031#include <console.h>
Simon Glasseea87102014-02-26 15:59:20 -070032#include <dm.h>
Simon Glass8fe40932019-08-01 09:46:44 -060033#include <env.h>
Simon Glass9d1f6192019-08-02 09:44:25 -060034#include <env_internal.h>
Simon Glassdb397fa2013-03-11 14:30:37 +000035#include <fdtdec.h>
Simon Glass5af29bd2013-03-14 07:20:12 +000036#include <ide.h>
Simon Glassa5037e22019-12-28 10:44:39 -070037#include <init.h>
Simon Glassdb397fa2013-03-11 14:30:37 +000038#include <initcall.h>
Simon Glass5af29bd2013-03-14 07:20:12 +000039#if defined(CONFIG_CMD_KGDB)
40#include <kgdb.h>
41#endif
Simon Glass9b61c7c2019-11-14 12:57:41 -070042#include <irq_func.h>
Simon Glassdb397fa2013-03-11 14:30:37 +000043#include <malloc.h>
Joe Hershberger65b905b2015-03-22 17:08:59 -050044#include <mapmem.h>
Simon Glass5af29bd2013-03-14 07:20:12 +000045#ifdef CONFIG_BITBANGMII
46#include <miiphy.h>
47#endif
Simon Glassdb397fa2013-03-11 14:30:37 +000048#include <mmc.h>
49#include <nand.h>
Simon Glass46e09772017-05-18 20:08:56 -060050#include <of_live.h>
Simon Glassdb397fa2013-03-11 14:30:37 +000051#include <onenand_uboot.h>
Simon Glass5af29bd2013-03-14 07:20:12 +000052#include <scsi.h>
Simon Glassdb397fa2013-03-11 14:30:37 +000053#include <serial.h>
Simon Glasse7872cb2019-11-14 12:57:11 -070054#include <status_led.h>
Simon Glassdb397fa2013-03-11 14:30:37 +000055#include <stdio_dev.h>
Simon Glass8e4f80f2016-02-24 09:14:50 -070056#include <timer.h>
Simon Glass209a1a62013-06-11 11:14:42 -070057#include <trace.h>
Simon Glass5af29bd2013-03-14 07:20:12 +000058#include <watchdog.h>
Oleksandr Andrushchenko2280d332020-08-06 12:42:48 +030059#ifdef CONFIG_XEN
60#include <xen.h>
61#endif
Simon Glass5af29bd2013-03-14 07:20:12 +000062#ifdef CONFIG_ADDR_MAP
63#include <asm/mmu.h>
64#endif
Simon Glassdb397fa2013-03-11 14:30:37 +000065#include <asm/sections.h>
Simon Glasseea87102014-02-26 15:59:20 -070066#include <dm/root.h>
Simon Glass5af29bd2013-03-14 07:20:12 +000067#include <linux/compiler.h>
Simon Glasseea87102014-02-26 15:59:20 -070068#include <linux/err.h>
Alexander Graf0bd425a2016-03-04 01:10:01 +010069#include <efi_loader.h>
Stefan Roese502acb02019-04-11 15:58:44 +020070#include <wdt.h>
Heiko Schocher58e4c382019-07-17 06:59:51 +020071#if defined(CONFIG_GPIO_HOG)
Heiko Schocher39cb3402019-06-12 06:11:46 +020072#include <asm/gpio.h>
73#endif
Simon Glassdb397fa2013-03-11 14:30:37 +000074
75DECLARE_GLOBAL_DATA_PTR;
76
77ulong monitor_flash_len;
78
Jeroen Hofstee45846052014-10-08 22:57:22 +020079__weak int board_flash_wp_on(void)
Simon Glass5af29bd2013-03-14 07:20:12 +000080{
81 /*
82 * Most flashes can't be detected when write protection is enabled,
83 * so provide a way to let U-Boot gracefully ignore write protected
84 * devices.
85 */
86 return 0;
87}
88
Jeroen Hofstee45846052014-10-08 22:57:22 +020089__weak void cpu_secondary_init_r(void)
Simon Glass5af29bd2013-03-14 07:20:12 +000090{
91}
92
Simon Glass5af29bd2013-03-14 07:20:12 +000093static int initr_secondary_cpu(void)
94{
95 /*
96 * after non-volatile devices & environment is setup and cpu code have
97 * another round to deal with any initialization that might require
98 * full access to the environment or loading of some image (firmware)
99 * from a non-volatile device
100 */
101 /* TODO: maybe define this for all archs? */
102 cpu_secondary_init_r();
103
104 return 0;
105}
Simon Glassdb397fa2013-03-11 14:30:37 +0000106
Simon Glass209a1a62013-06-11 11:14:42 -0700107static int initr_trace(void)
108{
109#ifdef CONFIG_TRACE
110 trace_init(gd->trace_buff, CONFIG_TRACE_BUFFER_SIZE);
111#endif
112
113 return 0;
114}
115
Simon Glassdb397fa2013-03-11 14:30:37 +0000116static int initr_reloc(void)
117{
Simon Glass94890462014-11-10 17:16:43 -0700118 /* tell others: relocation done */
119 gd->flags |= GD_FLG_RELOC | GD_FLG_FULL_MALLOC_INIT;
Simon Glassdb397fa2013-03-11 14:30:37 +0000120
121 return 0;
122}
123
124#ifdef CONFIG_ARM
125/*
126 * Some of these functions are needed purely because the functions they
127 * call return void. If we change them to return 0, these stubs can go away.
128 */
129static int initr_caches(void)
130{
131 /* Enable caches */
132 enable_caches();
133 return 0;
134}
135#endif
136
Simon Glass5af29bd2013-03-14 07:20:12 +0000137__weak int fixup_cpu(void)
138{
139 return 0;
140}
141
Simon Glassdb397fa2013-03-11 14:30:37 +0000142static int initr_reloc_global_data(void)
143{
Albert ARIBAUD6e294722014-02-22 17:53:43 +0100144#ifdef __ARM__
145 monitor_flash_len = _end - __image_copy_start;
Rick Chen3301bfc2017-12-26 13:55:58 +0800146#elif defined(CONFIG_NDS32) || defined(CONFIG_RISCV)
Kun-Hua Huang89299e22015-08-24 14:52:35 +0800147 monitor_flash_len = (ulong)&_end - (ulong)&_start;
Thomas Choucce3e752014-08-22 11:36:47 +0800148#elif !defined(CONFIG_SANDBOX) && !defined(CONFIG_NIOS2)
Masahiro Yamadad1589242013-05-27 00:37:30 +0000149 monitor_flash_len = (ulong)&__init_end - gd->relocaddr;
Simon Glassdb397fa2013-03-11 14:30:37 +0000150#endif
Simon Glass5af29bd2013-03-14 07:20:12 +0000151#if defined(CONFIG_MPC85xx) || defined(CONFIG_MPC86xx)
152 /*
153 * The gd->cpu pointer is set to an address in flash before relocation.
154 * We need to update it to point to the same CPU entry in RAM.
155 * TODO: why not just add gd->reloc_ofs?
156 */
Masahiro Yamadad1589242013-05-27 00:37:30 +0000157 gd->arch.cpu += gd->relocaddr - CONFIG_SYS_MONITOR_BASE;
Simon Glass5af29bd2013-03-14 07:20:12 +0000158
159 /*
160 * If we didn't know the cpu mask & # cores, we can save them of
161 * now rather than 'computing' them constantly
162 */
163 fixup_cpu();
164#endif
Tom Rinica63e712019-11-12 22:46:36 -0500165#ifdef CONFIG_SYS_RELOC_GD_ENV_ADDR
Simon Glass5af29bd2013-03-14 07:20:12 +0000166 /*
Simon Goldschmidt77a29332018-09-27 12:38:34 +0200167 * Relocate the early env_addr pointer unless we know it is not inside
168 * the binary. Some systems need this and for the rest, it doesn't hurt.
Simon Glass5af29bd2013-03-14 07:20:12 +0000169 */
Simon Goldschmidt77a29332018-09-27 12:38:34 +0200170 gd->env_addr += gd->reloc_off;
Simon Glass5af29bd2013-03-14 07:20:12 +0000171#endif
Siva Durga Prasad Paladuguabffd312015-12-03 15:46:03 +0100172#ifdef CONFIG_OF_EMBED
173 /*
Mario Sixee9b75b2018-01-15 11:10:30 +0100174 * The fdt_blob needs to be moved to new relocation address
175 * incase of FDT blob is embedded with in image
176 */
Siva Durga Prasad Paladuguabffd312015-12-03 15:46:03 +0100177 gd->fdt_blob += gd->reloc_off;
178#endif
Alexander Graf0bd425a2016-03-04 01:10:01 +0100179#ifdef CONFIG_EFI_LOADER
Heinrich Schuchardt3f2860f2019-04-10 00:32:07 +0200180 /*
181 * On the ARM architecture gd is mapped to a fixed register (r9 or x18).
182 * As this register may be overwritten by an EFI payload we save it here
183 * and restore it on every callback entered.
184 */
185 efi_save_gd();
186
Alexander Graf0bd425a2016-03-04 01:10:01 +0100187 efi_runtime_relocate(gd->relocaddr, NULL);
188#endif
Siva Durga Prasad Paladuguabffd312015-12-03 15:46:03 +0100189
Simon Glass5af29bd2013-03-14 07:20:12 +0000190 return 0;
Simon Glassdb397fa2013-03-11 14:30:37 +0000191}
192
Daniel Schwierzeckb9da7da2016-01-09 18:34:15 +0100193#if defined(CONFIG_PPC) || defined(CONFIG_M68K) || defined(CONFIG_MIPS)
Simon Glass5af29bd2013-03-14 07:20:12 +0000194static int initr_trap(void)
195{
196 /*
197 * Setup trap handlers
198 */
angelo@sysam.itf245ae92015-02-12 01:40:17 +0100199#if defined(CONFIG_PPC)
Masahiro Yamadad1589242013-05-27 00:37:30 +0000200 trap_init(gd->relocaddr);
angelo@sysam.itf245ae92015-02-12 01:40:17 +0100201#else
202 trap_init(CONFIG_SYS_SDRAM_BASE);
203#endif
Simon Glass5af29bd2013-03-14 07:20:12 +0000204 return 0;
205}
206#endif
207
208#ifdef CONFIG_ADDR_MAP
209static int initr_addr_map(void)
210{
211 init_addr_map();
212
213 return 0;
214}
215#endif
216
Simon Glassdb397fa2013-03-11 14:30:37 +0000217#ifdef CONFIG_POST
218static int initr_post_backlog(void)
219{
220 post_output_backlog();
221 return 0;
222}
223#endif
224
Simon Glass5af29bd2013-03-14 07:20:12 +0000225#if defined(CONFIG_SYS_INIT_RAM_LOCK) && defined(CONFIG_E500)
226static int initr_unlock_ram_in_cache(void)
227{
228 unlock_ram_in_cache(); /* it's time to unlock D-cache in e500 */
229 return 0;
230}
231#endif
232
Xiaowei Bao97434b42020-07-09 23:31:34 +0800233#ifdef CONFIG_PCI_ENDPOINT
234static int initr_pci_ep(void)
235{
236 pci_ep_init();
237
238 return 0;
239}
240#endif
241
Simon Glass5af29bd2013-03-14 07:20:12 +0000242#ifdef CONFIG_PCI
243static int initr_pci(void)
244{
Ovidiu Panait6746c672020-05-06 20:38:44 +0300245 if (IS_ENABLED(CONFIG_PCI_INIT_R))
246 pci_init();
Simon Glass5af29bd2013-03-14 07:20:12 +0000247
248 return 0;
249}
250#endif
251
Simon Glass5af29bd2013-03-14 07:20:12 +0000252static int initr_barrier(void)
253{
254#ifdef CONFIG_PPC
255 /* TODO: Can we not use dmb() macros for this? */
256 asm("sync ; isync");
257#endif
258 return 0;
259}
260
Simon Glassdb397fa2013-03-11 14:30:37 +0000261static int initr_malloc(void)
262{
263 ulong malloc_start;
264
Andy Yan1fa20e4d2017-07-24 17:43:34 +0800265#if CONFIG_VAL(SYS_MALLOC_F_LEN)
Simon Glass863e4042014-07-10 22:23:28 -0600266 debug("Pre-reloc malloc() used %#lx bytes (%ld KB)\n", gd->malloc_ptr,
267 gd->malloc_ptr / 1024);
268#endif
Simon Glassdb397fa2013-03-11 14:30:37 +0000269 /* The malloc area is immediately below the monitor copy in DRAM */
Stephen Warren9b496432019-08-27 11:54:31 -0600270 /*
271 * This value MUST match the value of gd->start_addr_sp in board_f.c:
272 * reserve_noncached().
273 */
Masahiro Yamadad1589242013-05-27 00:37:30 +0000274 malloc_start = gd->relocaddr - TOTAL_MALLOC_LEN;
Simon Glass62cf9122013-04-26 02:53:43 +0000275 mem_malloc_init((ulong)map_sysmem(malloc_start, TOTAL_MALLOC_LEN),
276 TOTAL_MALLOC_LEN);
Simon Glassdb397fa2013-03-11 14:30:37 +0000277 return 0;
278}
Thierry Redingc97d9742014-12-09 22:25:22 -0700279
Simon Glass1bb49232015-11-08 23:47:48 -0700280static int initr_console_record(void)
281{
282#if defined(CONFIG_CONSOLE_RECORD)
283 return console_record_init();
284#else
285 return 0;
286#endif
287}
288
Jan Kiszkaa714a6b92015-03-09 07:47:18 +0100289#ifdef CONFIG_SYS_NONCACHED_MEMORY
290static int initr_noncached(void)
291{
292 noncached_init();
293 return 0;
294}
295#endif
296
Simon Glass46e09772017-05-18 20:08:56 -0600297#ifdef CONFIG_OF_LIVE
298static int initr_of_live(void)
299{
Simon Glassfda60522017-05-22 05:05:36 -0600300 int ret;
301
302 bootstage_start(BOOTSTAGE_ID_ACCUM_OF_LIVE, "of_live");
303 ret = of_live_build(gd->fdt_blob, (struct device_node **)&gd->of_root);
304 bootstage_accum(BOOTSTAGE_ID_ACCUM_OF_LIVE);
305 if (ret)
306 return ret;
307
308 return 0;
Simon Glass46e09772017-05-18 20:08:56 -0600309}
310#endif
311
Simon Glasseea87102014-02-26 15:59:20 -0700312#ifdef CONFIG_DM
313static int initr_dm(void)
314{
Simon Glass8e4f80f2016-02-24 09:14:50 -0700315 int ret;
316
Simon Glassa730c5d2014-07-23 06:55:04 -0600317 /* Save the pre-reloc driver model and start a new one */
318 gd->dm_root_f = gd->dm_root;
319 gd->dm_root = NULL;
Simon Glassb0fb7bd2016-03-11 22:06:46 -0700320#ifdef CONFIG_TIMER
321 gd->timer = NULL;
322#endif
Simon Glassea6a6092020-05-10 11:39:59 -0600323 bootstage_start(BOOTSTAGE_ID_ACCUM_DM_R, "dm_r");
Simon Glass8e4f80f2016-02-24 09:14:50 -0700324 ret = dm_init_and_scan(false);
Simon Glassea6a6092020-05-10 11:39:59 -0600325 bootstage_accum(BOOTSTAGE_ID_ACCUM_DM_R);
Simon Glass8e4f80f2016-02-24 09:14:50 -0700326 if (ret)
327 return ret;
Simon Glass8e4f80f2016-02-24 09:14:50 -0700328
329 return 0;
Simon Glasseea87102014-02-26 15:59:20 -0700330}
331#endif
332
Simon Glass3199afb2019-12-06 21:41:46 -0700333static int initr_dm_devices(void)
334{
335 int ret;
336
337 if (IS_ENABLED(CONFIG_TIMER_EARLY)) {
338 ret = dm_timer_init();
339 if (ret)
340 return ret;
341 }
342
343 return 0;
344}
345
Simon Glass7edd07d2015-11-28 22:16:35 -0700346static int initr_bootstage(void)
347{
Simon Glass7edd07d2015-11-28 22:16:35 -0700348 bootstage_mark_name(BOOTSTAGE_ID_START_UBOOT_R, "board_init_r");
349
350 return 0;
351}
352
Simon Glassdb397fa2013-03-11 14:30:37 +0000353__weak int power_init_board(void)
354{
355 return 0;
356}
357
358static int initr_announce(void)
359{
Masahiro Yamadad1589242013-05-27 00:37:30 +0000360 debug("Now running in RAM - U-Boot at: %08lx\n", gd->relocaddr);
Simon Glassdb397fa2013-03-11 14:30:37 +0000361 return 0;
362}
363
Andreas Bießmann9e4f3962015-01-20 00:29:05 +0100364#ifdef CONFIG_NEEDS_MANUAL_RELOC
365static int initr_manual_reloc_cmdtable(void)
366{
Simon Glassed38aef2020-05-10 11:40:03 -0600367 fixup_cmdtable(ll_entry_start(struct cmd_tbl, cmd),
368 ll_entry_count(struct cmd_tbl, cmd));
Andreas Bießmann9e4f3962015-01-20 00:29:05 +0100369 return 0;
370}
371#endif
372
Simon Glassdd7fb9b2019-12-06 21:41:34 -0700373static int initr_binman(void)
374{
375 if (!CONFIG_IS_ENABLED(BINMAN_FDT))
376 return 0;
377
378 return binman_init();
379}
380
Masahiro Yamada8cea9b52017-02-11 22:43:54 +0900381#if defined(CONFIG_MTD_NOR_FLASH)
Pankit Gargd766ddd2020-04-30 15:54:05 +0530382__weak int is_flash_available(void)
383{
384 return 1;
385}
386
Simon Glassdb397fa2013-03-11 14:30:37 +0000387static int initr_flash(void)
388{
Simon Glass5af29bd2013-03-14 07:20:12 +0000389 ulong flash_size = 0;
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +0900390 struct bd_info *bd = gd->bd;
Simon Glassdb397fa2013-03-11 14:30:37 +0000391
Pankit Gargd766ddd2020-04-30 15:54:05 +0530392 if (!is_flash_available())
393 return 0;
394
Simon Glassdb397fa2013-03-11 14:30:37 +0000395 puts("Flash: ");
396
Masahiro Yamadaeb0e02b2014-12-05 12:20:58 +0900397 if (board_flash_wp_on())
Simon Glass5af29bd2013-03-14 07:20:12 +0000398 printf("Uninitialized - Write Protect On\n");
Masahiro Yamadaeb0e02b2014-12-05 12:20:58 +0900399 else
Simon Glass5af29bd2013-03-14 07:20:12 +0000400 flash_size = flash_init();
Masahiro Yamadaeb0e02b2014-12-05 12:20:58 +0900401
Simon Glassdb397fa2013-03-11 14:30:37 +0000402 print_size(flash_size, "");
403#ifdef CONFIG_SYS_FLASH_CHECKSUM
404 /*
Mario Sixee9b75b2018-01-15 11:10:30 +0100405 * Compute and print flash CRC if flashchecksum is set to 'y'
406 *
407 * NOTE: Maybe we should add some WATCHDOG_RESET()? XXX
408 */
Simon Glass22c34c22017-08-03 12:22:13 -0600409 if (env_get_yesno("flashchecksum") == 1) {
Mario Sixee9b75b2018-01-15 11:10:30 +0100410 const uchar *flash_base = (const uchar *)CONFIG_SYS_FLASH_BASE;
411
Simon Glassdb397fa2013-03-11 14:30:37 +0000412 printf(" CRC: %08X", crc32(0,
Mario Sixee9b75b2018-01-15 11:10:30 +0100413 flash_base,
414 flash_size));
Simon Glassdb397fa2013-03-11 14:30:37 +0000415 }
416#endif /* CONFIG_SYS_FLASH_CHECKSUM */
417 putc('\n');
418
Simon Glass5af29bd2013-03-14 07:20:12 +0000419 /* update start of FLASH memory */
420#ifdef CONFIG_SYS_FLASH_BASE
421 bd->bi_flashstart = CONFIG_SYS_FLASH_BASE;
422#endif
423 /* size of FLASH memory (final value) */
424 bd->bi_flashsize = flash_size;
425
426#if defined(CONFIG_SYS_UPDATE_FLASH_SIZE)
427 /* Make a update of the Memctrl. */
428 update_flash_size(flash_size);
429#endif
430
Simon Glass5af29bd2013-03-14 07:20:12 +0000431#if defined(CONFIG_OXC) || defined(CONFIG_RMU)
432 /* flash mapped at end of memory map */
433 bd->bi_flashoffset = CONFIG_SYS_TEXT_BASE + flash_size;
434#elif CONFIG_SYS_MONITOR_BASE == CONFIG_SYS_FLASH_BASE
435 bd->bi_flashoffset = monitor_flash_len; /* reserved area for monitor */
436#endif
437 return 0;
438}
439#endif
440
Simon Glassdb397fa2013-03-11 14:30:37 +0000441#ifdef CONFIG_CMD_NAND
442/* go init the NAND */
Jeroen Hofsteed9e87622014-10-08 22:57:32 +0200443static int initr_nand(void)
Simon Glassdb397fa2013-03-11 14:30:37 +0000444{
445 puts("NAND: ");
446 nand_init();
Hou Zhiqiang9c8b3c72017-03-17 16:12:31 +0800447 printf("%lu MiB\n", nand_size() / 1024);
Simon Glassdb397fa2013-03-11 14:30:37 +0000448 return 0;
449}
450#endif
451
452#if defined(CONFIG_CMD_ONENAND)
453/* go init the NAND */
Jeroen Hofsteed9e87622014-10-08 22:57:32 +0200454static int initr_onenand(void)
Simon Glassdb397fa2013-03-11 14:30:37 +0000455{
456 puts("NAND: ");
457 onenand_init();
458 return 0;
459}
460#endif
461
Masahiro Yamada0a780172017-05-09 20:31:39 +0900462#ifdef CONFIG_MMC
Jeroen Hofsteed9e87622014-10-08 22:57:32 +0200463static int initr_mmc(void)
Simon Glassdb397fa2013-03-11 14:30:37 +0000464{
465 puts("MMC: ");
466 mmc_initialize(gd->bd);
467 return 0;
468}
469#endif
470
Oleksandr Andrushchenko2280d332020-08-06 12:42:48 +0300471#ifdef CONFIG_XEN
472static int initr_xen(void)
473{
474 xen_init();
475 return 0;
476}
477#endif
Simon Glassdb397fa2013-03-11 14:30:37 +0000478/*
479 * Tell if it's OK to load the environment early in boot.
480 *
Masahiro Yamadab970a892016-02-05 20:49:39 +0900481 * If CONFIG_OF_CONTROL is defined, we'll check with the FDT to see
Simon Glassdb397fa2013-03-11 14:30:37 +0000482 * if this is OK (defaulting to saying it's OK).
483 *
484 * NOTE: Loading the environment early can be a bad idea if security is
485 * important, since no verification is done on the environment.
486 *
487 * @return 0 if environment should not be loaded, !=0 if it is ok to load
488 */
489static int should_load_env(void)
490{
Ovidiu Panaitd7145f22020-05-06 20:38:43 +0300491 if (IS_ENABLED(CONFIG_OF_CONTROL))
492 return fdtdec_get_config_int(gd->fdt_blob,
493 "load-environment", 1);
494
495 if (IS_ENABLED(CONFIG_DELAY_ENVIRONMENT))
496 return 0;
497
Simon Glassdb397fa2013-03-11 14:30:37 +0000498 return 1;
Simon Glassdb397fa2013-03-11 14:30:37 +0000499}
500
501static int initr_env(void)
502{
503 /* initialize environment */
504 if (should_load_env())
505 env_relocate();
506 else
Simon Glass97385862019-08-01 09:47:00 -0600507 env_set_default(NULL, 0);
Ovidiu Panaitd7145f22020-05-06 20:38:43 +0300508
509 if (IS_ENABLED(CONFIG_OF_CONTROL))
510 env_set_hex("fdtcontroladdr",
511 (unsigned long)map_to_sysmem(gd->fdt_blob));
Simon Glassdb397fa2013-03-11 14:30:37 +0000512
513 /* Initialize from environment */
Simon Glass892265d2019-12-28 10:45:02 -0700514 image_load_addr = env_get_ulong("loadaddr", 16, image_load_addr);
Simon Glass5af29bd2013-03-14 07:20:12 +0000515
Simon Glass5af29bd2013-03-14 07:20:12 +0000516 return 0;
517}
518
Andreas Bießmann111bbf52015-02-06 23:06:47 +0100519#ifdef CONFIG_SYS_BOOTPARAMS_LEN
520static int initr_malloc_bootparams(void)
521{
522 gd->bd->bi_boot_params = (ulong)malloc(CONFIG_SYS_BOOTPARAMS_LEN);
523 if (!gd->bd->bi_boot_params) {
524 puts("WARNING: Cannot allocate space for boot parameters\n");
525 return -ENOMEM;
526 }
527 return 0;
528}
529#endif
530
Simon Glassdb397fa2013-03-11 14:30:37 +0000531static int initr_jumptable(void)
532{
533 jumptable_init();
534 return 0;
535}
536
537#if defined(CONFIG_API)
538static int initr_api(void)
539{
540 /* Initialize API */
541 api_init();
542 return 0;
543}
544#endif
545
Simon Glassdb397fa2013-03-11 14:30:37 +0000546#ifdef CONFIG_CMD_NET
547static int initr_ethaddr(void)
548{
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +0900549 struct bd_info *bd = gd->bd;
Simon Glass5af29bd2013-03-14 07:20:12 +0000550
551 /* kept around for legacy kernels only ... ignore the next section */
Simon Glass399a9ce2017-08-03 12:22:14 -0600552 eth_env_get_enetaddr("ethaddr", bd->bi_enetaddr);
Simon Glassf7cc66e2020-05-10 14:16:51 -0600553
Simon Glass5af29bd2013-03-14 07:20:12 +0000554 return 0;
555}
556#endif /* CONFIG_CMD_NET */
557
558#ifdef CONFIG_CMD_KGDB
559static int initr_kgdb(void)
560{
561 puts("KGDB: ");
562 kgdb_init();
563 return 0;
564}
565#endif
566
Uri Mashiach4892d392017-01-19 10:51:45 +0200567#if defined(CONFIG_LED_STATUS)
Simon Glass5af29bd2013-03-14 07:20:12 +0000568static int initr_status_led(void)
569{
Uri Mashiach4892d392017-01-19 10:51:45 +0200570#if defined(CONFIG_LED_STATUS_BOOT)
571 status_led_set(CONFIG_LED_STATUS_BOOT, CONFIG_LED_STATUS_BLINKING);
Bernhard Nortmannc5dcb4a2015-08-21 15:13:21 +0200572#else
573 status_led_init();
574#endif
Simon Glass5af29bd2013-03-14 07:20:12 +0000575 return 0;
576}
577#endif
578
Michal Simekc886f352016-09-08 15:06:45 +0200579#if defined(CONFIG_SCSI) && !defined(CONFIG_DM_SCSI)
Simon Glass5af29bd2013-03-14 07:20:12 +0000580static int initr_scsi(void)
581{
Simon Glass5af29bd2013-03-14 07:20:12 +0000582 puts("SCSI: ");
583 scsi_init();
Heinrich Schuchardt34cef302018-06-15 07:01:26 +0200584 puts("\n");
Simon Glassdb397fa2013-03-11 14:30:37 +0000585
586 return 0;
587}
Ian Campbell63d424b2014-07-21 19:23:18 +0100588#endif
Simon Glassdb397fa2013-03-11 14:30:37 +0000589
590#ifdef CONFIG_BITBANGMII
591static int initr_bbmii(void)
592{
593 bb_miiphy_init();
594 return 0;
595}
596#endif
597
598#ifdef CONFIG_CMD_NET
599static int initr_net(void)
600{
601 puts("Net: ");
Joe Hershberger3dbe17e2015-03-22 17:09:06 -0500602 eth_initialize();
Simon Glassdb397fa2013-03-11 14:30:37 +0000603#if defined(CONFIG_RESET_PHY_R)
604 debug("Reset Ethernet PHY\n");
605 reset_phy();
606#endif
607 return 0;
608}
609#endif
610
611#ifdef CONFIG_POST
612static int initr_post(void)
613{
614 post_run(NULL, POST_RAM | post_bootmode_get(0));
615 return 0;
616}
617#endif
618
Bin Mengf05614e2018-06-17 05:57:50 -0700619#if defined(CONFIG_IDE) && !defined(CONFIG_BLK)
Simon Glass5af29bd2013-03-14 07:20:12 +0000620static int initr_ide(void)
621{
Simon Glass5af29bd2013-03-14 07:20:12 +0000622 puts("IDE: ");
Simon Glass5af29bd2013-03-14 07:20:12 +0000623#if defined(CONFIG_START_IDE)
624 if (board_start_ide())
625 ide_init();
626#else
627 ide_init();
628#endif
629 return 0;
630}
631#endif
632
Simon Glass49badb92017-12-04 13:48:23 -0700633#if defined(CONFIG_PRAM)
Simon Glassdb397fa2013-03-11 14:30:37 +0000634/*
635 * Export available size of memory for Linux, taking into account the
636 * protected RAM at top of memory
637 */
638int initr_mem(void)
639{
640 ulong pram = 0;
641 char memsz[32];
642
Simon Glass22c34c22017-08-03 12:22:13 -0600643 pram = env_get_ulong("pram", 10, CONFIG_PRAM);
Mario Sixee9b75b2018-01-15 11:10:30 +0100644 sprintf(memsz, "%ldk", (long int)((gd->ram_size / 1024) - pram));
Simon Glass6a38e412017-08-03 12:22:09 -0600645 env_set("mem", memsz);
Simon Glass5af29bd2013-03-14 07:20:12 +0000646
647 return 0;
Simon Glassdb397fa2013-03-11 14:30:37 +0000648}
649#endif
650
651static int run_main_loop(void)
652{
Simon Glass62cf9122013-04-26 02:53:43 +0000653#ifdef CONFIG_SANDBOX
654 sandbox_main_loop_init();
655#endif
Simon Glassdb397fa2013-03-11 14:30:37 +0000656 /* main_loop() can return to retry autoboot, if so just run it again */
657 for (;;)
658 main_loop();
659 return 0;
660}
661
662/*
Alexander Graff204f012019-01-31 16:06:23 +0100663 * We hope to remove most of the driver-related init and do it if/when
Simon Glassdb397fa2013-03-11 14:30:37 +0000664 * the driver is later used.
Simon Glass5af29bd2013-03-14 07:20:12 +0000665 *
666 * TODO: perhaps reset the watchdog in the initcall function after each call?
Simon Glassdb397fa2013-03-11 14:30:37 +0000667 */
Simon Glass2031fad2017-01-16 07:03:50 -0700668static init_fnc_t init_sequence_r[] = {
Simon Glass209a1a62013-06-11 11:14:42 -0700669 initr_trace,
Simon Glassdb397fa2013-03-11 14:30:37 +0000670 initr_reloc,
Simon Glass5af29bd2013-03-14 07:20:12 +0000671 /* TODO: could x86/PPC have this also perhaps? */
Simon Glassdb397fa2013-03-11 14:30:37 +0000672#ifdef CONFIG_ARM
673 initr_caches,
York Sun820a18f2015-03-20 19:28:11 -0700674 /* Note: For Freescale LS2 SoCs, new MMU table is created in DDR.
675 * A temporary mapping of IFC high region is since removed,
Mario Sixee9b75b2018-01-15 11:10:30 +0100676 * so environmental variables in NOR flash is not available
York Sun820a18f2015-03-20 19:28:11 -0700677 * until board_init() is called below to remap IFC to high
678 * region.
679 */
Simon Glass712da5e2014-09-03 17:37:01 -0600680#endif
681 initr_reloc_global_data,
York Sun6e78b3b2014-10-02 15:20:10 -0700682#if defined(CONFIG_SYS_INIT_RAM_LOCK) && defined(CONFIG_E500)
683 initr_unlock_ram_in_cache,
684#endif
Simon Glass712da5e2014-09-03 17:37:01 -0600685 initr_barrier,
686 initr_malloc,
Simon Glass55e32ba2017-12-04 13:48:28 -0700687 log_init,
Simon Glasse635af12017-05-22 05:05:31 -0600688 initr_bootstage, /* Needs malloc() but has its own timer */
Simon Glass1bb49232015-11-08 23:47:48 -0700689 initr_console_record,
Jan Kiszkaa714a6b92015-03-09 07:47:18 +0100690#ifdef CONFIG_SYS_NONCACHED_MEMORY
691 initr_noncached,
692#endif
Simon Glass46e09772017-05-18 20:08:56 -0600693#ifdef CONFIG_OF_LIVE
694 initr_of_live,
695#endif
Simon Glass712da5e2014-09-03 17:37:01 -0600696#ifdef CONFIG_DM
697 initr_dm,
698#endif
Patrick Delaunay29bf6032018-07-27 16:37:09 +0200699#if defined(CONFIG_ARM) || defined(CONFIG_NDS32) || defined(CONFIG_RISCV) || \
700 defined(CONFIG_SANDBOX)
Simon Glassdb397fa2013-03-11 14:30:37 +0000701 board_init, /* Setup chipselects */
702#endif
Simon Glass5af29bd2013-03-14 07:20:12 +0000703 /*
704 * TODO: printing of the clock inforamtion of the board is now
705 * implemented as part of bdinfo command. Currently only support for
706 * davinci SOC's is added. Remove this check once all the board
707 * implement this.
708 */
709#ifdef CONFIG_CLOCKS
710 set_cpu_clk_info, /* Setup clock information */
711#endif
Alexander Graf8623f922016-03-04 01:10:04 +0100712#ifdef CONFIG_EFI_LOADER
713 efi_memory_init,
714#endif
Simon Glassdd7fb9b2019-12-06 21:41:34 -0700715 initr_binman,
Simon Glass564a4a02019-12-06 21:42:20 -0700716#ifdef CONFIG_FSP_VERSION2
717 arch_fsp_init_r,
718#endif
Simon Glass3199afb2019-12-06 21:41:46 -0700719 initr_dm_devices,
Simon Glass712da5e2014-09-03 17:37:01 -0600720 stdio_init_tables,
Ovidiu Panaitbfcd5c52020-07-24 14:12:23 +0300721 serial_initialize,
Simon Glassdb397fa2013-03-11 14:30:37 +0000722 initr_announce,
Marek Vasut55ec91b2019-06-09 03:46:21 +0200723#if CONFIG_IS_ENABLED(WDT)
developere49871c2019-05-16 17:19:13 +0800724 initr_watchdog,
725#endif
Simon Glass5af29bd2013-03-14 07:20:12 +0000726 INIT_FUNC_WATCHDOG_RESET
Ovidiu Panait97dd43f2020-07-24 14:12:25 +0300727#if defined(CONFIG_NEEDS_MANUAL_RELOC) && defined(CONFIG_BLOCK_CACHE)
728 blkcache_init,
729#endif
Andreas Bießmann9e4f3962015-01-20 00:29:05 +0100730#ifdef CONFIG_NEEDS_MANUAL_RELOC
731 initr_manual_reloc_cmdtable,
732#endif
Daniel Schwierzeckb9da7da2016-01-09 18:34:15 +0100733#if defined(CONFIG_PPC) || defined(CONFIG_M68K) || defined(CONFIG_MIPS)
Simon Glass5af29bd2013-03-14 07:20:12 +0000734 initr_trap,
735#endif
736#ifdef CONFIG_ADDR_MAP
737 initr_addr_map,
738#endif
739#if defined(CONFIG_BOARD_EARLY_INIT_R)
740 board_early_init_r,
741#endif
742 INIT_FUNC_WATCHDOG_RESET
Simon Glassdb397fa2013-03-11 14:30:37 +0000743#ifdef CONFIG_POST
744 initr_post_backlog,
745#endif
Simon Glass5af29bd2013-03-14 07:20:12 +0000746 INIT_FUNC_WATCHDOG_RESET
Simon Glass5af29bd2013-03-14 07:20:12 +0000747#if defined(CONFIG_PCI) && defined(CONFIG_SYS_EARLY_PCI_INIT)
748 /*
749 * Do early PCI configuration _before_ the flash gets initialised,
Mario Sixee9b75b2018-01-15 11:10:30 +0100750 * because PCU resources are crucial for flash access on some boards.
Simon Glass5af29bd2013-03-14 07:20:12 +0000751 */
752 initr_pci,
753#endif
Simon Glassdb397fa2013-03-11 14:30:37 +0000754#ifdef CONFIG_ARCH_EARLY_INIT_R
755 arch_early_init_r,
756#endif
757 power_init_board,
Masahiro Yamada8cea9b52017-02-11 22:43:54 +0900758#ifdef CONFIG_MTD_NOR_FLASH
Simon Glassdb397fa2013-03-11 14:30:37 +0000759 initr_flash,
760#endif
Simon Glass5af29bd2013-03-14 07:20:12 +0000761 INIT_FUNC_WATCHDOG_RESET
Tom Rinifef2bd12017-03-14 11:08:11 -0400762#if defined(CONFIG_PPC) || defined(CONFIG_M68K) || defined(CONFIG_X86)
Simon Glass5af29bd2013-03-14 07:20:12 +0000763 /* initialize higher level parts of CPU like time base and timers */
764 cpu_init_r,
Simon Glass6f5567d2013-03-05 14:39:53 +0000765#endif
Simon Glassdb397fa2013-03-11 14:30:37 +0000766#ifdef CONFIG_CMD_NAND
767 initr_nand,
768#endif
769#ifdef CONFIG_CMD_ONENAND
770 initr_onenand,
771#endif
Masahiro Yamada0a780172017-05-09 20:31:39 +0900772#ifdef CONFIG_MMC
Simon Glassdb397fa2013-03-11 14:30:37 +0000773 initr_mmc,
774#endif
Oleksandr Andrushchenko2280d332020-08-06 12:42:48 +0300775#ifdef CONFIG_XEN
776 initr_xen,
777#endif
Simon Glassdb397fa2013-03-11 14:30:37 +0000778 initr_env,
Andreas Bießmann111bbf52015-02-06 23:06:47 +0100779#ifdef CONFIG_SYS_BOOTPARAMS_LEN
780 initr_malloc_bootparams,
781#endif
Simon Glass5af29bd2013-03-14 07:20:12 +0000782 INIT_FUNC_WATCHDOG_RESET
783 initr_secondary_cpu,
Simon Glass5af29bd2013-03-14 07:20:12 +0000784#if defined(CONFIG_ID_EEPROM) || defined(CONFIG_SYS_I2C_MAC_OFFSET)
785 mac_read_from_eeprom,
786#endif
787 INIT_FUNC_WATCHDOG_RESET
788#if defined(CONFIG_PCI) && !defined(CONFIG_SYS_EARLY_PCI_INIT)
789 /*
790 * Do pci configuration
791 */
792 initr_pci,
793#endif
Simon Glass712da5e2014-09-03 17:37:01 -0600794 stdio_add_devices,
Simon Glassdb397fa2013-03-11 14:30:37 +0000795 initr_jumptable,
796#ifdef CONFIG_API
797 initr_api,
798#endif
799 console_init_r, /* fully init console as a device */
800#ifdef CONFIG_DISPLAY_BOARDINFO_LATE
Simon Glassc9c5c9f2017-06-15 21:37:50 -0600801 console_announce_r,
Masahiro Yamada9607f7a2015-01-14 17:07:05 +0900802 show_board_info,
Simon Glassdb397fa2013-03-11 14:30:37 +0000803#endif
804#ifdef CONFIG_ARCH_MISC_INIT
805 arch_misc_init, /* miscellaneous arch-dependent init */
806#endif
807#ifdef CONFIG_MISC_INIT_R
808 misc_init_r, /* miscellaneous platform-dependent init */
809#endif
Simon Glass5af29bd2013-03-14 07:20:12 +0000810 INIT_FUNC_WATCHDOG_RESET
811#ifdef CONFIG_CMD_KGDB
812 initr_kgdb,
813#endif
Simon Glassdb397fa2013-03-11 14:30:37 +0000814 interrupt_init,
Andy Shevchenko8cb5cdd2017-07-05 16:25:22 +0300815#if defined(CONFIG_MICROBLAZE) || defined(CONFIG_M68K)
Simon Glass6f5567d2013-03-05 14:39:53 +0000816 timer_init, /* initialize timer */
817#endif
Uri Mashiach4892d392017-01-19 10:51:45 +0200818#if defined(CONFIG_LED_STATUS)
Simon Glass5af29bd2013-03-14 07:20:12 +0000819 initr_status_led,
820#endif
821 /* PPC has a udelay(20) here dating from 2002. Why? */
Simon Glassdb397fa2013-03-11 14:30:37 +0000822#ifdef CONFIG_CMD_NET
823 initr_ethaddr,
824#endif
Heiko Schocher58e4c382019-07-17 06:59:51 +0200825#if defined(CONFIG_GPIO_HOG)
Heiko Schocher39cb3402019-06-12 06:11:46 +0200826 gpio_hog_probe_all,
827#endif
Simon Glassdb397fa2013-03-11 14:30:37 +0000828#ifdef CONFIG_BOARD_LATE_INIT
829 board_late_init,
830#endif
Michal Simekc886f352016-09-08 15:06:45 +0200831#if defined(CONFIG_SCSI) && !defined(CONFIG_DM_SCSI)
Simon Glass5af29bd2013-03-14 07:20:12 +0000832 INIT_FUNC_WATCHDOG_RESET
833 initr_scsi,
834#endif
Simon Glassdb397fa2013-03-11 14:30:37 +0000835#ifdef CONFIG_BITBANGMII
836 initr_bbmii,
837#endif
Xiaowei Bao97434b42020-07-09 23:31:34 +0800838#ifdef CONFIG_PCI_ENDPOINT
839 initr_pci_ep,
840#endif
Simon Glassdb397fa2013-03-11 14:30:37 +0000841#ifdef CONFIG_CMD_NET
Simon Glass5af29bd2013-03-14 07:20:12 +0000842 INIT_FUNC_WATCHDOG_RESET
Simon Glassdb397fa2013-03-11 14:30:37 +0000843 initr_net,
844#endif
845#ifdef CONFIG_POST
846 initr_post,
847#endif
Bin Mengf05614e2018-06-17 05:57:50 -0700848#if defined(CONFIG_IDE) && !defined(CONFIG_BLK)
Simon Glass5af29bd2013-03-14 07:20:12 +0000849 initr_ide,
850#endif
851#ifdef CONFIG_LAST_STAGE_INIT
852 INIT_FUNC_WATCHDOG_RESET
853 /*
854 * Some parts can be only initialized if all others (like
855 * Interrupts) are up and running (i.e. the PC-style ISA
856 * keyboard).
857 */
858 last_stage_init,
859#endif
860#ifdef CONFIG_CMD_BEDBUG
861 INIT_FUNC_WATCHDOG_RESET
Ovidiu Panait8637bec2020-04-20 10:31:46 +0300862 bedbug_init,
Simon Glass5af29bd2013-03-14 07:20:12 +0000863#endif
Simon Glass49badb92017-12-04 13:48:23 -0700864#if defined(CONFIG_PRAM)
Simon Glass5af29bd2013-03-14 07:20:12 +0000865 initr_mem,
866#endif
Simon Glassdb397fa2013-03-11 14:30:37 +0000867 run_main_loop,
868};
869
870void board_init_r(gd_t *new_gd, ulong dest_addr)
871{
Simon Glass96c867d2017-01-16 07:03:51 -0700872 /*
873 * Set up the new global data pointer. So far only x86 does this
874 * here.
875 * TODO(sjg@chromium.org): Consider doing this for all archs, or
876 * dropping the new_gd parameter.
877 */
878#if CONFIG_IS_ENABLED(X86_64)
879 arch_setup_gd(new_gd);
880#endif
881
Alexey Brodkin3f0516f2014-01-20 14:30:39 +0400882#ifdef CONFIG_NEEDS_MANUAL_RELOC
883 int i;
884#endif
885
Jeroen Hofstee498f97f2014-07-30 21:54:49 +0200886#if !defined(CONFIG_X86) && !defined(CONFIG_ARM) && !defined(CONFIG_ARM64)
Simon Glassdb397fa2013-03-11 14:30:37 +0000887 gd = new_gd;
Simon Glass6f5567d2013-03-05 14:39:53 +0000888#endif
Simon Glass55e32ba2017-12-04 13:48:28 -0700889 gd->flags &= ~GD_FLG_LOG_READY;
Alexey Brodkin3f0516f2014-01-20 14:30:39 +0400890
891#ifdef CONFIG_NEEDS_MANUAL_RELOC
892 for (i = 0; i < ARRAY_SIZE(init_sequence_r); i++)
893 init_sequence_r[i] += gd->reloc_off;
894#endif
895
Simon Glassdb397fa2013-03-11 14:30:37 +0000896 if (initcall_run_list(init_sequence_r))
897 hang();
898
899 /* NOTREACHED - run_main_loop() does not return */
900 hang();
901}