blob: e7c25942965b780b917494b4ba161e68f994dc29 [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 Glass63334482019-11-14 12:57:39 -070014#include <cpu_func.h>
Simon Glass8f3f7612019-11-14 12:57:42 -070015#include <irq_func.h>
Simon Glass48b6c6b2019-11-14 12:57:16 -070016#include <u-boot/crc.h>
Simon Glass5af29bd2013-03-14 07:20:12 +000017/* TODO: can we just include all these headers whether needed or not? */
18#if defined(CONFIG_CMD_BEDBUG)
19#include <bedbug/type.h>
20#endif
Simon Glassdd7fb9b2019-12-06 21:41:34 -070021#include <binman.h>
Tom Rini23415e52015-12-07 08:23:29 -050022#include <command.h>
Simon Glassa73bda42015-11-08 23:47:45 -070023#include <console.h>
Simon Glasseea87102014-02-26 15:59:20 -070024#include <dm.h>
Simon Glass8fe40932019-08-01 09:46:44 -060025#include <env.h>
Simon Glass9d1f6192019-08-02 09:44:25 -060026#include <env_internal.h>
Simon Glassdb397fa2013-03-11 14:30:37 +000027#include <fdtdec.h>
Simon Glass5af29bd2013-03-14 07:20:12 +000028#include <ide.h>
Simon Glass8fc96b02019-12-28 10:44:39 -070029#include <init.h>
Simon Glassdb397fa2013-03-11 14:30:37 +000030#include <initcall.h>
Simon Glass5af29bd2013-03-14 07:20:12 +000031#if defined(CONFIG_CMD_KGDB)
32#include <kgdb.h>
33#endif
Simon Glass9b61c7c2019-11-14 12:57:41 -070034#include <irq_func.h>
Simon Glassdb397fa2013-03-11 14:30:37 +000035#include <malloc.h>
Joe Hershberger65b905b2015-03-22 17:08:59 -050036#include <mapmem.h>
Simon Glass5af29bd2013-03-14 07:20:12 +000037#ifdef CONFIG_BITBANGMII
38#include <miiphy.h>
39#endif
Simon Glassdb397fa2013-03-11 14:30:37 +000040#include <mmc.h>
41#include <nand.h>
Simon Glass46e09772017-05-18 20:08:56 -060042#include <of_live.h>
Simon Glassdb397fa2013-03-11 14:30:37 +000043#include <onenand_uboot.h>
Simon Glass5af29bd2013-03-14 07:20:12 +000044#include <scsi.h>
Simon Glassdb397fa2013-03-11 14:30:37 +000045#include <serial.h>
Simon Glasse7872cb2019-11-14 12:57:11 -070046#include <status_led.h>
Simon Glassdb397fa2013-03-11 14:30:37 +000047#include <stdio_dev.h>
Simon Glass8e4f80f2016-02-24 09:14:50 -070048#include <timer.h>
Simon Glass209a1a62013-06-11 11:14:42 -070049#include <trace.h>
Simon Glass5af29bd2013-03-14 07:20:12 +000050#include <watchdog.h>
51#ifdef CONFIG_ADDR_MAP
52#include <asm/mmu.h>
53#endif
Simon Glassdb397fa2013-03-11 14:30:37 +000054#include <asm/sections.h>
Simon Glasseea87102014-02-26 15:59:20 -070055#include <dm/root.h>
Simon Glass5af29bd2013-03-14 07:20:12 +000056#include <linux/compiler.h>
Simon Glasseea87102014-02-26 15:59:20 -070057#include <linux/err.h>
Alexander Graf0bd425a2016-03-04 01:10:01 +010058#include <efi_loader.h>
Stefan Roese502acb02019-04-11 15:58:44 +020059#include <wdt.h>
Heiko Schocher58e4c382019-07-17 06:59:51 +020060#if defined(CONFIG_GPIO_HOG)
Heiko Schocher39cb3402019-06-12 06:11:46 +020061#include <asm/gpio.h>
62#endif
Simon Glassdb397fa2013-03-11 14:30:37 +000063
64DECLARE_GLOBAL_DATA_PTR;
65
66ulong monitor_flash_len;
67
Jeroen Hofstee45846052014-10-08 22:57:22 +020068__weak int board_flash_wp_on(void)
Simon Glass5af29bd2013-03-14 07:20:12 +000069{
70 /*
71 * Most flashes can't be detected when write protection is enabled,
72 * so provide a way to let U-Boot gracefully ignore write protected
73 * devices.
74 */
75 return 0;
76}
77
Jeroen Hofstee45846052014-10-08 22:57:22 +020078__weak void cpu_secondary_init_r(void)
Simon Glass5af29bd2013-03-14 07:20:12 +000079{
80}
81
Simon Glass5af29bd2013-03-14 07:20:12 +000082static int initr_secondary_cpu(void)
83{
84 /*
85 * after non-volatile devices & environment is setup and cpu code have
86 * another round to deal with any initialization that might require
87 * full access to the environment or loading of some image (firmware)
88 * from a non-volatile device
89 */
90 /* TODO: maybe define this for all archs? */
91 cpu_secondary_init_r();
92
93 return 0;
94}
Simon Glassdb397fa2013-03-11 14:30:37 +000095
Simon Glass209a1a62013-06-11 11:14:42 -070096static int initr_trace(void)
97{
98#ifdef CONFIG_TRACE
99 trace_init(gd->trace_buff, CONFIG_TRACE_BUFFER_SIZE);
100#endif
101
102 return 0;
103}
104
Simon Glassdb397fa2013-03-11 14:30:37 +0000105static int initr_reloc(void)
106{
Simon Glass94890462014-11-10 17:16:43 -0700107 /* tell others: relocation done */
108 gd->flags |= GD_FLG_RELOC | GD_FLG_FULL_MALLOC_INIT;
Simon Glassdb397fa2013-03-11 14:30:37 +0000109
110 return 0;
111}
112
113#ifdef CONFIG_ARM
114/*
115 * Some of these functions are needed purely because the functions they
116 * call return void. If we change them to return 0, these stubs can go away.
117 */
118static int initr_caches(void)
119{
120 /* Enable caches */
121 enable_caches();
122 return 0;
123}
124#endif
125
Simon Glass5af29bd2013-03-14 07:20:12 +0000126__weak int fixup_cpu(void)
127{
128 return 0;
129}
130
Simon Glassdb397fa2013-03-11 14:30:37 +0000131static int initr_reloc_global_data(void)
132{
Albert ARIBAUD6e294722014-02-22 17:53:43 +0100133#ifdef __ARM__
134 monitor_flash_len = _end - __image_copy_start;
Rick Chen3301bfc2017-12-26 13:55:58 +0800135#elif defined(CONFIG_NDS32) || defined(CONFIG_RISCV)
Kun-Hua Huang89299e22015-08-24 14:52:35 +0800136 monitor_flash_len = (ulong)&_end - (ulong)&_start;
Thomas Choucce3e752014-08-22 11:36:47 +0800137#elif !defined(CONFIG_SANDBOX) && !defined(CONFIG_NIOS2)
Masahiro Yamadad1589242013-05-27 00:37:30 +0000138 monitor_flash_len = (ulong)&__init_end - gd->relocaddr;
Simon Glassdb397fa2013-03-11 14:30:37 +0000139#endif
Simon Glass5af29bd2013-03-14 07:20:12 +0000140#if defined(CONFIG_MPC85xx) || defined(CONFIG_MPC86xx)
141 /*
142 * The gd->cpu pointer is set to an address in flash before relocation.
143 * We need to update it to point to the same CPU entry in RAM.
144 * TODO: why not just add gd->reloc_ofs?
145 */
Masahiro Yamadad1589242013-05-27 00:37:30 +0000146 gd->arch.cpu += gd->relocaddr - CONFIG_SYS_MONITOR_BASE;
Simon Glass5af29bd2013-03-14 07:20:12 +0000147
148 /*
149 * If we didn't know the cpu mask & # cores, we can save them of
150 * now rather than 'computing' them constantly
151 */
152 fixup_cpu();
153#endif
Tom Rinica63e712019-11-12 22:46:36 -0500154#ifdef CONFIG_SYS_RELOC_GD_ENV_ADDR
Simon Glass5af29bd2013-03-14 07:20:12 +0000155 /*
Simon Goldschmidt77a29332018-09-27 12:38:34 +0200156 * Relocate the early env_addr pointer unless we know it is not inside
157 * the binary. Some systems need this and for the rest, it doesn't hurt.
Simon Glass5af29bd2013-03-14 07:20:12 +0000158 */
Simon Goldschmidt77a29332018-09-27 12:38:34 +0200159 gd->env_addr += gd->reloc_off;
Simon Glass5af29bd2013-03-14 07:20:12 +0000160#endif
Siva Durga Prasad Paladuguabffd312015-12-03 15:46:03 +0100161#ifdef CONFIG_OF_EMBED
162 /*
Mario Sixee9b75b2018-01-15 11:10:30 +0100163 * The fdt_blob needs to be moved to new relocation address
164 * incase of FDT blob is embedded with in image
165 */
Siva Durga Prasad Paladuguabffd312015-12-03 15:46:03 +0100166 gd->fdt_blob += gd->reloc_off;
167#endif
Alexander Graf0bd425a2016-03-04 01:10:01 +0100168#ifdef CONFIG_EFI_LOADER
Heinrich Schuchardt3f2860f2019-04-10 00:32:07 +0200169 /*
170 * On the ARM architecture gd is mapped to a fixed register (r9 or x18).
171 * As this register may be overwritten by an EFI payload we save it here
172 * and restore it on every callback entered.
173 */
174 efi_save_gd();
175
Alexander Graf0bd425a2016-03-04 01:10:01 +0100176 efi_runtime_relocate(gd->relocaddr, NULL);
177#endif
Siva Durga Prasad Paladuguabffd312015-12-03 15:46:03 +0100178
Simon Glass5af29bd2013-03-14 07:20:12 +0000179 return 0;
Simon Glassdb397fa2013-03-11 14:30:37 +0000180}
181
182static int initr_serial(void)
183{
184 serial_initialize();
185 return 0;
186}
187
Daniel Schwierzeckb9da7da2016-01-09 18:34:15 +0100188#if defined(CONFIG_PPC) || defined(CONFIG_M68K) || defined(CONFIG_MIPS)
Simon Glass5af29bd2013-03-14 07:20:12 +0000189static int initr_trap(void)
190{
191 /*
192 * Setup trap handlers
193 */
angelo@sysam.itf245ae92015-02-12 01:40:17 +0100194#if defined(CONFIG_PPC)
Masahiro Yamadad1589242013-05-27 00:37:30 +0000195 trap_init(gd->relocaddr);
angelo@sysam.itf245ae92015-02-12 01:40:17 +0100196#else
197 trap_init(CONFIG_SYS_SDRAM_BASE);
198#endif
Simon Glass5af29bd2013-03-14 07:20:12 +0000199 return 0;
200}
201#endif
202
203#ifdef CONFIG_ADDR_MAP
204static int initr_addr_map(void)
205{
206 init_addr_map();
207
208 return 0;
209}
210#endif
211
Simon Glassdb397fa2013-03-11 14:30:37 +0000212#ifdef CONFIG_POST
213static int initr_post_backlog(void)
214{
215 post_output_backlog();
216 return 0;
217}
218#endif
219
Simon Glass5af29bd2013-03-14 07:20:12 +0000220#if defined(CONFIG_SYS_INIT_RAM_LOCK) && defined(CONFIG_E500)
221static int initr_unlock_ram_in_cache(void)
222{
223 unlock_ram_in_cache(); /* it's time to unlock D-cache in e500 */
224 return 0;
225}
226#endif
227
228#ifdef CONFIG_PCI
229static int initr_pci(void)
230{
Simon Glassb94dc892015-03-05 12:25:25 -0700231#ifndef CONFIG_DM_PCI
Simon Glass5af29bd2013-03-14 07:20:12 +0000232 pci_init();
Simon Glassb94dc892015-03-05 12:25:25 -0700233#endif
Simon Glass5af29bd2013-03-14 07:20:12 +0000234
235 return 0;
236}
237#endif
238
Simon Glass5af29bd2013-03-14 07:20:12 +0000239static int initr_barrier(void)
240{
241#ifdef CONFIG_PPC
242 /* TODO: Can we not use dmb() macros for this? */
243 asm("sync ; isync");
244#endif
245 return 0;
246}
247
Simon Glassdb397fa2013-03-11 14:30:37 +0000248static int initr_malloc(void)
249{
250 ulong malloc_start;
251
Andy Yan1fa20e4d2017-07-24 17:43:34 +0800252#if CONFIG_VAL(SYS_MALLOC_F_LEN)
Simon Glass863e4042014-07-10 22:23:28 -0600253 debug("Pre-reloc malloc() used %#lx bytes (%ld KB)\n", gd->malloc_ptr,
254 gd->malloc_ptr / 1024);
255#endif
Simon Glassdb397fa2013-03-11 14:30:37 +0000256 /* The malloc area is immediately below the monitor copy in DRAM */
Stephen Warren9b496432019-08-27 11:54:31 -0600257 /*
258 * This value MUST match the value of gd->start_addr_sp in board_f.c:
259 * reserve_noncached().
260 */
Masahiro Yamadad1589242013-05-27 00:37:30 +0000261 malloc_start = gd->relocaddr - TOTAL_MALLOC_LEN;
Simon Glass62cf9122013-04-26 02:53:43 +0000262 mem_malloc_init((ulong)map_sysmem(malloc_start, TOTAL_MALLOC_LEN),
263 TOTAL_MALLOC_LEN);
Simon Glassdb397fa2013-03-11 14:30:37 +0000264 return 0;
265}
Thierry Redingc97d9742014-12-09 22:25:22 -0700266
Simon Glass1bb49232015-11-08 23:47:48 -0700267static int initr_console_record(void)
268{
269#if defined(CONFIG_CONSOLE_RECORD)
270 return console_record_init();
271#else
272 return 0;
273#endif
274}
275
Jan Kiszkaa714a6b92015-03-09 07:47:18 +0100276#ifdef CONFIG_SYS_NONCACHED_MEMORY
277static int initr_noncached(void)
278{
279 noncached_init();
280 return 0;
281}
282#endif
283
Simon Glass46e09772017-05-18 20:08:56 -0600284#ifdef CONFIG_OF_LIVE
285static int initr_of_live(void)
286{
Simon Glassfda60522017-05-22 05:05:36 -0600287 int ret;
288
289 bootstage_start(BOOTSTAGE_ID_ACCUM_OF_LIVE, "of_live");
290 ret = of_live_build(gd->fdt_blob, (struct device_node **)&gd->of_root);
291 bootstage_accum(BOOTSTAGE_ID_ACCUM_OF_LIVE);
292 if (ret)
293 return ret;
294
295 return 0;
Simon Glass46e09772017-05-18 20:08:56 -0600296}
297#endif
298
Simon Glasseea87102014-02-26 15:59:20 -0700299#ifdef CONFIG_DM
300static int initr_dm(void)
301{
Simon Glass8e4f80f2016-02-24 09:14:50 -0700302 int ret;
303
Simon Glassa730c5d2014-07-23 06:55:04 -0600304 /* Save the pre-reloc driver model and start a new one */
305 gd->dm_root_f = gd->dm_root;
306 gd->dm_root = NULL;
Simon Glassb0fb7bd2016-03-11 22:06:46 -0700307#ifdef CONFIG_TIMER
308 gd->timer = NULL;
309#endif
Simon Glass405e2b02017-05-22 05:05:32 -0600310 bootstage_start(BOOTSTATE_ID_ACCUM_DM_R, "dm_r");
Simon Glass8e4f80f2016-02-24 09:14:50 -0700311 ret = dm_init_and_scan(false);
Simon Glass405e2b02017-05-22 05:05:32 -0600312 bootstage_accum(BOOTSTATE_ID_ACCUM_DM_R);
Simon Glass8e4f80f2016-02-24 09:14:50 -0700313 if (ret)
314 return ret;
Simon Glass8e4f80f2016-02-24 09:14:50 -0700315
316 return 0;
Simon Glasseea87102014-02-26 15:59:20 -0700317}
318#endif
319
Simon Glass3199afb2019-12-06 21:41:46 -0700320static int initr_dm_devices(void)
321{
322 int ret;
323
324 if (IS_ENABLED(CONFIG_TIMER_EARLY)) {
325 ret = dm_timer_init();
326 if (ret)
327 return ret;
328 }
329
330 return 0;
331}
332
Simon Glass7edd07d2015-11-28 22:16:35 -0700333static int initr_bootstage(void)
334{
Simon Glass7edd07d2015-11-28 22:16:35 -0700335 bootstage_mark_name(BOOTSTAGE_ID_START_UBOOT_R, "board_init_r");
336
337 return 0;
338}
339
Simon Glassdb397fa2013-03-11 14:30:37 +0000340__weak int power_init_board(void)
341{
342 return 0;
343}
344
345static int initr_announce(void)
346{
Masahiro Yamadad1589242013-05-27 00:37:30 +0000347 debug("Now running in RAM - U-Boot at: %08lx\n", gd->relocaddr);
Simon Glassdb397fa2013-03-11 14:30:37 +0000348 return 0;
349}
350
Andreas Bießmann9e4f3962015-01-20 00:29:05 +0100351#ifdef CONFIG_NEEDS_MANUAL_RELOC
352static int initr_manual_reloc_cmdtable(void)
353{
354 fixup_cmdtable(ll_entry_start(cmd_tbl_t, cmd),
355 ll_entry_count(cmd_tbl_t, cmd));
356 return 0;
357}
358#endif
359
Simon Glassdd7fb9b2019-12-06 21:41:34 -0700360static int initr_binman(void)
361{
362 if (!CONFIG_IS_ENABLED(BINMAN_FDT))
363 return 0;
364
365 return binman_init();
366}
367
Masahiro Yamada8cea9b52017-02-11 22:43:54 +0900368#if defined(CONFIG_MTD_NOR_FLASH)
Simon Glassdb397fa2013-03-11 14:30:37 +0000369static int initr_flash(void)
370{
Simon Glass5af29bd2013-03-14 07:20:12 +0000371 ulong flash_size = 0;
372 bd_t *bd = gd->bd;
Simon Glassdb397fa2013-03-11 14:30:37 +0000373
374 puts("Flash: ");
375
Masahiro Yamadaeb0e02b2014-12-05 12:20:58 +0900376 if (board_flash_wp_on())
Simon Glass5af29bd2013-03-14 07:20:12 +0000377 printf("Uninitialized - Write Protect On\n");
Masahiro Yamadaeb0e02b2014-12-05 12:20:58 +0900378 else
Simon Glass5af29bd2013-03-14 07:20:12 +0000379 flash_size = flash_init();
Masahiro Yamadaeb0e02b2014-12-05 12:20:58 +0900380
Simon Glassdb397fa2013-03-11 14:30:37 +0000381 print_size(flash_size, "");
382#ifdef CONFIG_SYS_FLASH_CHECKSUM
383 /*
Mario Sixee9b75b2018-01-15 11:10:30 +0100384 * Compute and print flash CRC if flashchecksum is set to 'y'
385 *
386 * NOTE: Maybe we should add some WATCHDOG_RESET()? XXX
387 */
Simon Glass22c34c22017-08-03 12:22:13 -0600388 if (env_get_yesno("flashchecksum") == 1) {
Mario Sixee9b75b2018-01-15 11:10:30 +0100389 const uchar *flash_base = (const uchar *)CONFIG_SYS_FLASH_BASE;
390
Simon Glassdb397fa2013-03-11 14:30:37 +0000391 printf(" CRC: %08X", crc32(0,
Mario Sixee9b75b2018-01-15 11:10:30 +0100392 flash_base,
393 flash_size));
Simon Glassdb397fa2013-03-11 14:30:37 +0000394 }
395#endif /* CONFIG_SYS_FLASH_CHECKSUM */
396 putc('\n');
397
Simon Glass5af29bd2013-03-14 07:20:12 +0000398 /* update start of FLASH memory */
399#ifdef CONFIG_SYS_FLASH_BASE
400 bd->bi_flashstart = CONFIG_SYS_FLASH_BASE;
401#endif
402 /* size of FLASH memory (final value) */
403 bd->bi_flashsize = flash_size;
404
405#if defined(CONFIG_SYS_UPDATE_FLASH_SIZE)
406 /* Make a update of the Memctrl. */
407 update_flash_size(flash_size);
408#endif
409
Simon Glass5af29bd2013-03-14 07:20:12 +0000410#if defined(CONFIG_OXC) || defined(CONFIG_RMU)
411 /* flash mapped at end of memory map */
412 bd->bi_flashoffset = CONFIG_SYS_TEXT_BASE + flash_size;
413#elif CONFIG_SYS_MONITOR_BASE == CONFIG_SYS_FLASH_BASE
414 bd->bi_flashoffset = monitor_flash_len; /* reserved area for monitor */
415#endif
416 return 0;
417}
418#endif
419
Simon Glassdb397fa2013-03-11 14:30:37 +0000420#ifdef CONFIG_CMD_NAND
421/* go init the NAND */
Jeroen Hofsteed9e87622014-10-08 22:57:32 +0200422static int initr_nand(void)
Simon Glassdb397fa2013-03-11 14:30:37 +0000423{
424 puts("NAND: ");
425 nand_init();
Hou Zhiqiang9c8b3c72017-03-17 16:12:31 +0800426 printf("%lu MiB\n", nand_size() / 1024);
Simon Glassdb397fa2013-03-11 14:30:37 +0000427 return 0;
428}
429#endif
430
431#if defined(CONFIG_CMD_ONENAND)
432/* go init the NAND */
Jeroen Hofsteed9e87622014-10-08 22:57:32 +0200433static int initr_onenand(void)
Simon Glassdb397fa2013-03-11 14:30:37 +0000434{
435 puts("NAND: ");
436 onenand_init();
437 return 0;
438}
439#endif
440
Masahiro Yamada0a780172017-05-09 20:31:39 +0900441#ifdef CONFIG_MMC
Jeroen Hofsteed9e87622014-10-08 22:57:32 +0200442static int initr_mmc(void)
Simon Glassdb397fa2013-03-11 14:30:37 +0000443{
444 puts("MMC: ");
445 mmc_initialize(gd->bd);
446 return 0;
447}
448#endif
449
Simon Glassdb397fa2013-03-11 14:30:37 +0000450/*
451 * Tell if it's OK to load the environment early in boot.
452 *
Masahiro Yamadab970a892016-02-05 20:49:39 +0900453 * If CONFIG_OF_CONTROL is defined, we'll check with the FDT to see
Simon Glassdb397fa2013-03-11 14:30:37 +0000454 * if this is OK (defaulting to saying it's OK).
455 *
456 * NOTE: Loading the environment early can be a bad idea if security is
457 * important, since no verification is done on the environment.
458 *
459 * @return 0 if environment should not be loaded, !=0 if it is ok to load
460 */
461static int should_load_env(void)
462{
463#ifdef CONFIG_OF_CONTROL
464 return fdtdec_get_config_int(gd->fdt_blob, "load-environment", 1);
465#elif defined CONFIG_DELAY_ENVIRONMENT
466 return 0;
467#else
468 return 1;
469#endif
470}
471
472static int initr_env(void)
473{
474 /* initialize environment */
475 if (should_load_env())
476 env_relocate();
477 else
Simon Glass97385862019-08-01 09:47:00 -0600478 env_set_default(NULL, 0);
Thomas Chou4fda2812015-10-16 08:44:51 +0800479#ifdef CONFIG_OF_CONTROL
Heinrich Schuchardt370a1762018-11-18 17:58:50 +0100480 env_set_hex("fdtcontroladdr",
481 (unsigned long)map_to_sysmem(gd->fdt_blob));
Thomas Chou4fda2812015-10-16 08:44:51 +0800482#endif
Simon Glassdb397fa2013-03-11 14:30:37 +0000483
484 /* Initialize from environment */
Simon Glass22c34c22017-08-03 12:22:13 -0600485 load_addr = env_get_ulong("loadaddr", 16, load_addr);
Simon Glass5af29bd2013-03-14 07:20:12 +0000486
Simon Glass5af29bd2013-03-14 07:20:12 +0000487 return 0;
488}
489
Andreas Bießmann111bbf52015-02-06 23:06:47 +0100490#ifdef CONFIG_SYS_BOOTPARAMS_LEN
491static int initr_malloc_bootparams(void)
492{
493 gd->bd->bi_boot_params = (ulong)malloc(CONFIG_SYS_BOOTPARAMS_LEN);
494 if (!gd->bd->bi_boot_params) {
495 puts("WARNING: Cannot allocate space for boot parameters\n");
496 return -ENOMEM;
497 }
498 return 0;
499}
500#endif
501
Simon Glassdb397fa2013-03-11 14:30:37 +0000502static int initr_jumptable(void)
503{
504 jumptable_init();
505 return 0;
506}
507
508#if defined(CONFIG_API)
509static int initr_api(void)
510{
511 /* Initialize API */
512 api_init();
513 return 0;
514}
515#endif
516
Simon Glassdb397fa2013-03-11 14:30:37 +0000517/* enable exceptions */
Andy Shevchenko8cb5cdd2017-07-05 16:25:22 +0300518#ifdef CONFIG_ARM
Simon Glassdb397fa2013-03-11 14:30:37 +0000519static int initr_enable_interrupts(void)
520{
521 enable_interrupts();
522 return 0;
523}
Simon Glass2e4622d2013-03-11 07:40:13 +0000524#endif
Simon Glassdb397fa2013-03-11 14:30:37 +0000525
526#ifdef CONFIG_CMD_NET
527static int initr_ethaddr(void)
528{
Simon Glass5af29bd2013-03-14 07:20:12 +0000529 bd_t *bd = gd->bd;
530
531 /* kept around for legacy kernels only ... ignore the next section */
Simon Glass399a9ce2017-08-03 12:22:14 -0600532 eth_env_get_enetaddr("ethaddr", bd->bi_enetaddr);
Simon Glass5af29bd2013-03-14 07:20:12 +0000533#ifdef CONFIG_HAS_ETH1
Simon Glass399a9ce2017-08-03 12:22:14 -0600534 eth_env_get_enetaddr("eth1addr", bd->bi_enet1addr);
Simon Glass5af29bd2013-03-14 07:20:12 +0000535#endif
536#ifdef CONFIG_HAS_ETH2
Simon Glass399a9ce2017-08-03 12:22:14 -0600537 eth_env_get_enetaddr("eth2addr", bd->bi_enet2addr);
Simon Glass5af29bd2013-03-14 07:20:12 +0000538#endif
539#ifdef CONFIG_HAS_ETH3
Simon Glass399a9ce2017-08-03 12:22:14 -0600540 eth_env_get_enetaddr("eth3addr", bd->bi_enet3addr);
Simon Glass5af29bd2013-03-14 07:20:12 +0000541#endif
542#ifdef CONFIG_HAS_ETH4
Simon Glass399a9ce2017-08-03 12:22:14 -0600543 eth_env_get_enetaddr("eth4addr", bd->bi_enet4addr);
Simon Glass5af29bd2013-03-14 07:20:12 +0000544#endif
545#ifdef CONFIG_HAS_ETH5
Simon Glass399a9ce2017-08-03 12:22:14 -0600546 eth_env_get_enetaddr("eth5addr", bd->bi_enet5addr);
Simon Glass5af29bd2013-03-14 07:20:12 +0000547#endif
548 return 0;
549}
550#endif /* CONFIG_CMD_NET */
551
552#ifdef CONFIG_CMD_KGDB
553static int initr_kgdb(void)
554{
555 puts("KGDB: ");
556 kgdb_init();
557 return 0;
558}
559#endif
560
Uri Mashiach4892d392017-01-19 10:51:45 +0200561#if defined(CONFIG_LED_STATUS)
Simon Glass5af29bd2013-03-14 07:20:12 +0000562static int initr_status_led(void)
563{
Uri Mashiach4892d392017-01-19 10:51:45 +0200564#if defined(CONFIG_LED_STATUS_BOOT)
565 status_led_set(CONFIG_LED_STATUS_BOOT, CONFIG_LED_STATUS_BLINKING);
Bernhard Nortmannc5dcb4a2015-08-21 15:13:21 +0200566#else
567 status_led_init();
568#endif
Simon Glass5af29bd2013-03-14 07:20:12 +0000569 return 0;
570}
571#endif
572
Michal Simekc886f352016-09-08 15:06:45 +0200573#if defined(CONFIG_SCSI) && !defined(CONFIG_DM_SCSI)
Simon Glass5af29bd2013-03-14 07:20:12 +0000574static int initr_scsi(void)
575{
Simon Glass5af29bd2013-03-14 07:20:12 +0000576 puts("SCSI: ");
577 scsi_init();
Heinrich Schuchardt34cef302018-06-15 07:01:26 +0200578 puts("\n");
Simon Glassdb397fa2013-03-11 14:30:37 +0000579
580 return 0;
581}
Ian Campbell63d424b2014-07-21 19:23:18 +0100582#endif
Simon Glassdb397fa2013-03-11 14:30:37 +0000583
584#ifdef CONFIG_BITBANGMII
585static int initr_bbmii(void)
586{
587 bb_miiphy_init();
588 return 0;
589}
590#endif
591
592#ifdef CONFIG_CMD_NET
593static int initr_net(void)
594{
595 puts("Net: ");
Joe Hershberger3dbe17e2015-03-22 17:09:06 -0500596 eth_initialize();
Simon Glassdb397fa2013-03-11 14:30:37 +0000597#if defined(CONFIG_RESET_PHY_R)
598 debug("Reset Ethernet PHY\n");
599 reset_phy();
600#endif
601 return 0;
602}
603#endif
604
605#ifdef CONFIG_POST
606static int initr_post(void)
607{
608 post_run(NULL, POST_RAM | post_bootmode_get(0));
609 return 0;
610}
611#endif
612
Bin Mengf05614e2018-06-17 05:57:50 -0700613#if defined(CONFIG_IDE) && !defined(CONFIG_BLK)
Simon Glass5af29bd2013-03-14 07:20:12 +0000614static int initr_ide(void)
615{
Simon Glass5af29bd2013-03-14 07:20:12 +0000616 puts("IDE: ");
Simon Glass5af29bd2013-03-14 07:20:12 +0000617#if defined(CONFIG_START_IDE)
618 if (board_start_ide())
619 ide_init();
620#else
621 ide_init();
622#endif
623 return 0;
624}
625#endif
626
Simon Glass49badb92017-12-04 13:48:23 -0700627#if defined(CONFIG_PRAM)
Simon Glassdb397fa2013-03-11 14:30:37 +0000628/*
629 * Export available size of memory for Linux, taking into account the
630 * protected RAM at top of memory
631 */
632int initr_mem(void)
633{
634 ulong pram = 0;
635 char memsz[32];
636
Simon Glass22c34c22017-08-03 12:22:13 -0600637 pram = env_get_ulong("pram", 10, CONFIG_PRAM);
Mario Sixee9b75b2018-01-15 11:10:30 +0100638 sprintf(memsz, "%ldk", (long int)((gd->ram_size / 1024) - pram));
Simon Glass6a38e412017-08-03 12:22:09 -0600639 env_set("mem", memsz);
Simon Glass5af29bd2013-03-14 07:20:12 +0000640
641 return 0;
Simon Glassdb397fa2013-03-11 14:30:37 +0000642}
643#endif
644
Simon Glass5af29bd2013-03-14 07:20:12 +0000645#ifdef CONFIG_CMD_BEDBUG
646static int initr_bedbug(void)
647{
648 bedbug_init();
649
650 return 0;
651}
652#endif
653
Simon Glassdb397fa2013-03-11 14:30:37 +0000654static int run_main_loop(void)
655{
Simon Glass62cf9122013-04-26 02:53:43 +0000656#ifdef CONFIG_SANDBOX
657 sandbox_main_loop_init();
658#endif
Simon Glassdb397fa2013-03-11 14:30:37 +0000659 /* main_loop() can return to retry autoboot, if so just run it again */
660 for (;;)
661 main_loop();
662 return 0;
663}
664
665/*
Alexander Graff204f012019-01-31 16:06:23 +0100666 * We hope to remove most of the driver-related init and do it if/when
Simon Glassdb397fa2013-03-11 14:30:37 +0000667 * the driver is later used.
Simon Glass5af29bd2013-03-14 07:20:12 +0000668 *
669 * TODO: perhaps reset the watchdog in the initcall function after each call?
Simon Glassdb397fa2013-03-11 14:30:37 +0000670 */
Simon Glass2031fad2017-01-16 07:03:50 -0700671static init_fnc_t init_sequence_r[] = {
Simon Glass209a1a62013-06-11 11:14:42 -0700672 initr_trace,
Simon Glassdb397fa2013-03-11 14:30:37 +0000673 initr_reloc,
Simon Glass5af29bd2013-03-14 07:20:12 +0000674 /* TODO: could x86/PPC have this also perhaps? */
Simon Glassdb397fa2013-03-11 14:30:37 +0000675#ifdef CONFIG_ARM
676 initr_caches,
York Sun820a18f2015-03-20 19:28:11 -0700677 /* Note: For Freescale LS2 SoCs, new MMU table is created in DDR.
678 * A temporary mapping of IFC high region is since removed,
Mario Sixee9b75b2018-01-15 11:10:30 +0100679 * so environmental variables in NOR flash is not available
York Sun820a18f2015-03-20 19:28:11 -0700680 * until board_init() is called below to remap IFC to high
681 * region.
682 */
Simon Glass712da5e2014-09-03 17:37:01 -0600683#endif
684 initr_reloc_global_data,
York Sun6e78b3b2014-10-02 15:20:10 -0700685#if defined(CONFIG_SYS_INIT_RAM_LOCK) && defined(CONFIG_E500)
686 initr_unlock_ram_in_cache,
687#endif
Simon Glass712da5e2014-09-03 17:37:01 -0600688 initr_barrier,
689 initr_malloc,
Simon Glass55e32ba2017-12-04 13:48:28 -0700690 log_init,
Simon Glasse635af12017-05-22 05:05:31 -0600691 initr_bootstage, /* Needs malloc() but has its own timer */
Simon Glass1bb49232015-11-08 23:47:48 -0700692 initr_console_record,
Jan Kiszkaa714a6b92015-03-09 07:47:18 +0100693#ifdef CONFIG_SYS_NONCACHED_MEMORY
694 initr_noncached,
695#endif
Simon Glass46e09772017-05-18 20:08:56 -0600696#ifdef CONFIG_OF_LIVE
697 initr_of_live,
698#endif
Simon Glass712da5e2014-09-03 17:37:01 -0600699#ifdef CONFIG_DM
700 initr_dm,
701#endif
Patrick Delaunay29bf6032018-07-27 16:37:09 +0200702#if defined(CONFIG_ARM) || defined(CONFIG_NDS32) || defined(CONFIG_RISCV) || \
703 defined(CONFIG_SANDBOX)
Simon Glassdb397fa2013-03-11 14:30:37 +0000704 board_init, /* Setup chipselects */
705#endif
Simon Glass5af29bd2013-03-14 07:20:12 +0000706 /*
707 * TODO: printing of the clock inforamtion of the board is now
708 * implemented as part of bdinfo command. Currently only support for
709 * davinci SOC's is added. Remove this check once all the board
710 * implement this.
711 */
712#ifdef CONFIG_CLOCKS
713 set_cpu_clk_info, /* Setup clock information */
714#endif
Alexander Graf8623f922016-03-04 01:10:04 +0100715#ifdef CONFIG_EFI_LOADER
716 efi_memory_init,
717#endif
Simon Glassdd7fb9b2019-12-06 21:41:34 -0700718 initr_binman,
Simon Glass564a4a02019-12-06 21:42:20 -0700719#ifdef CONFIG_FSP_VERSION2
720 arch_fsp_init_r,
721#endif
Simon Glass3199afb2019-12-06 21:41:46 -0700722 initr_dm_devices,
Simon Glass712da5e2014-09-03 17:37:01 -0600723 stdio_init_tables,
Simon Glassdb397fa2013-03-11 14:30:37 +0000724 initr_serial,
725 initr_announce,
Marek Vasut55ec91b2019-06-09 03:46:21 +0200726#if CONFIG_IS_ENABLED(WDT)
developere49871c2019-05-16 17:19:13 +0800727 initr_watchdog,
728#endif
Simon Glass5af29bd2013-03-14 07:20:12 +0000729 INIT_FUNC_WATCHDOG_RESET
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
Simon Glassdb397fa2013-03-11 14:30:37 +0000775 initr_env,
Andreas Bießmann111bbf52015-02-06 23:06:47 +0100776#ifdef CONFIG_SYS_BOOTPARAMS_LEN
777 initr_malloc_bootparams,
778#endif
Simon Glass5af29bd2013-03-14 07:20:12 +0000779 INIT_FUNC_WATCHDOG_RESET
780 initr_secondary_cpu,
Simon Glass5af29bd2013-03-14 07:20:12 +0000781#if defined(CONFIG_ID_EEPROM) || defined(CONFIG_SYS_I2C_MAC_OFFSET)
782 mac_read_from_eeprom,
783#endif
784 INIT_FUNC_WATCHDOG_RESET
785#if defined(CONFIG_PCI) && !defined(CONFIG_SYS_EARLY_PCI_INIT)
786 /*
787 * Do pci configuration
788 */
789 initr_pci,
790#endif
Simon Glass712da5e2014-09-03 17:37:01 -0600791 stdio_add_devices,
Simon Glassdb397fa2013-03-11 14:30:37 +0000792 initr_jumptable,
793#ifdef CONFIG_API
794 initr_api,
795#endif
796 console_init_r, /* fully init console as a device */
797#ifdef CONFIG_DISPLAY_BOARDINFO_LATE
Simon Glassc9c5c9f2017-06-15 21:37:50 -0600798 console_announce_r,
Masahiro Yamada9607f7a2015-01-14 17:07:05 +0900799 show_board_info,
Simon Glassdb397fa2013-03-11 14:30:37 +0000800#endif
801#ifdef CONFIG_ARCH_MISC_INIT
802 arch_misc_init, /* miscellaneous arch-dependent init */
803#endif
804#ifdef CONFIG_MISC_INIT_R
805 misc_init_r, /* miscellaneous platform-dependent init */
806#endif
Simon Glass5af29bd2013-03-14 07:20:12 +0000807 INIT_FUNC_WATCHDOG_RESET
808#ifdef CONFIG_CMD_KGDB
809 initr_kgdb,
810#endif
Simon Glassdb397fa2013-03-11 14:30:37 +0000811 interrupt_init,
Andy Shevchenko8cb5cdd2017-07-05 16:25:22 +0300812#ifdef CONFIG_ARM
Simon Glassdb397fa2013-03-11 14:30:37 +0000813 initr_enable_interrupts,
Simon Glass5af29bd2013-03-14 07:20:12 +0000814#endif
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
838#ifdef CONFIG_CMD_NET
Simon Glass5af29bd2013-03-14 07:20:12 +0000839 INIT_FUNC_WATCHDOG_RESET
Simon Glassdb397fa2013-03-11 14:30:37 +0000840 initr_net,
841#endif
842#ifdef CONFIG_POST
843 initr_post,
844#endif
Bin Mengf05614e2018-06-17 05:57:50 -0700845#if defined(CONFIG_IDE) && !defined(CONFIG_BLK)
Simon Glass5af29bd2013-03-14 07:20:12 +0000846 initr_ide,
847#endif
848#ifdef CONFIG_LAST_STAGE_INIT
849 INIT_FUNC_WATCHDOG_RESET
850 /*
851 * Some parts can be only initialized if all others (like
852 * Interrupts) are up and running (i.e. the PC-style ISA
853 * keyboard).
854 */
855 last_stage_init,
856#endif
857#ifdef CONFIG_CMD_BEDBUG
858 INIT_FUNC_WATCHDOG_RESET
859 initr_bedbug,
860#endif
Simon Glass49badb92017-12-04 13:48:23 -0700861#if defined(CONFIG_PRAM)
Simon Glass5af29bd2013-03-14 07:20:12 +0000862 initr_mem,
863#endif
Simon Glassdb397fa2013-03-11 14:30:37 +0000864 run_main_loop,
865};
866
867void board_init_r(gd_t *new_gd, ulong dest_addr)
868{
Simon Glass96c867d2017-01-16 07:03:51 -0700869 /*
870 * Set up the new global data pointer. So far only x86 does this
871 * here.
872 * TODO(sjg@chromium.org): Consider doing this for all archs, or
873 * dropping the new_gd parameter.
874 */
875#if CONFIG_IS_ENABLED(X86_64)
876 arch_setup_gd(new_gd);
877#endif
878
Alexey Brodkin3f0516f2014-01-20 14:30:39 +0400879#ifdef CONFIG_NEEDS_MANUAL_RELOC
880 int i;
881#endif
882
Jeroen Hofstee498f97f2014-07-30 21:54:49 +0200883#if !defined(CONFIG_X86) && !defined(CONFIG_ARM) && !defined(CONFIG_ARM64)
Simon Glassdb397fa2013-03-11 14:30:37 +0000884 gd = new_gd;
Simon Glass6f5567d2013-03-05 14:39:53 +0000885#endif
Simon Glass55e32ba2017-12-04 13:48:28 -0700886 gd->flags &= ~GD_FLG_LOG_READY;
Alexey Brodkin3f0516f2014-01-20 14:30:39 +0400887
888#ifdef CONFIG_NEEDS_MANUAL_RELOC
889 for (i = 0; i < ARRAY_SIZE(init_sequence_r); i++)
890 init_sequence_r[i] += gd->reloc_off;
891#endif
892
Simon Glassdb397fa2013-03-11 14:30:37 +0000893 if (initcall_run_list(init_sequence_r))
894 hang();
895
896 /* NOTREACHED - run_main_loop() does not return */
897 hang();
898}