blob: 0f4479a58bc28cde01f693de18d8149abf662885 [file] [log] [blame]
Simon Glassdb397fa2013-03-11 14:30:37 +00001/*
2 * Copyright (c) 2011 The Chromium OS Authors.
3 * (C) Copyright 2002-2006
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 *
6 * (C) Copyright 2002
7 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
8 * Marius Groeger <mgroeger@sysgo.de>
9 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +020010 * SPDX-License-Identifier: GPL-2.0+
Simon Glassdb397fa2013-03-11 14:30:37 +000011 */
12
13#include <common.h>
Simon Glassac53a5c2017-05-17 08:22:40 -060014#include <api.h>
Simon Glass5af29bd2013-03-14 07:20:12 +000015/* TODO: can we just include all these headers whether needed or not? */
16#if defined(CONFIG_CMD_BEDBUG)
17#include <bedbug/type.h>
18#endif
Tom Rini23415e52015-12-07 08:23:29 -050019#include <command.h>
Simon Glassa73bda42015-11-08 23:47:45 -070020#include <console.h>
Simon Glasseea87102014-02-26 15:59:20 -070021#include <dm.h>
Simon Glassdb397fa2013-03-11 14:30:37 +000022#include <environment.h>
23#include <fdtdec.h>
Simon Glass5af29bd2013-03-14 07:20:12 +000024#include <ide.h>
Simon Glassdb397fa2013-03-11 14:30:37 +000025#include <initcall.h>
Simon Glass5af29bd2013-03-14 07:20:12 +000026#if defined(CONFIG_CMD_KGDB)
27#include <kgdb.h>
28#endif
Simon Glassdb397fa2013-03-11 14:30:37 +000029#include <malloc.h>
Joe Hershberger65b905b2015-03-22 17:08:59 -050030#include <mapmem.h>
Simon Glass5af29bd2013-03-14 07:20:12 +000031#ifdef CONFIG_BITBANGMII
32#include <miiphy.h>
33#endif
Simon Glassdb397fa2013-03-11 14:30:37 +000034#include <mmc.h>
35#include <nand.h>
Simon Glass46e09772017-05-18 20:08:56 -060036#include <of_live.h>
Simon Glassdb397fa2013-03-11 14:30:37 +000037#include <onenand_uboot.h>
Simon Glass5af29bd2013-03-14 07:20:12 +000038#include <scsi.h>
Simon Glassdb397fa2013-03-11 14:30:37 +000039#include <serial.h>
Simon Glass5af29bd2013-03-14 07:20:12 +000040#include <spi.h>
Simon Glassdb397fa2013-03-11 14:30:37 +000041#include <stdio_dev.h>
Simon Glass8e4f80f2016-02-24 09:14:50 -070042#include <timer.h>
Simon Glass209a1a62013-06-11 11:14:42 -070043#include <trace.h>
Simon Glass5af29bd2013-03-14 07:20:12 +000044#include <watchdog.h>
45#ifdef CONFIG_ADDR_MAP
46#include <asm/mmu.h>
47#endif
Simon Glassdb397fa2013-03-11 14:30:37 +000048#include <asm/sections.h>
Simon Glasseea87102014-02-26 15:59:20 -070049#include <dm/root.h>
Simon Glass5af29bd2013-03-14 07:20:12 +000050#include <linux/compiler.h>
Simon Glasseea87102014-02-26 15:59:20 -070051#include <linux/err.h>
Alexander Graf0bd425a2016-03-04 01:10:01 +010052#include <efi_loader.h>
Simon Glassdb397fa2013-03-11 14:30:37 +000053
54DECLARE_GLOBAL_DATA_PTR;
55
56ulong monitor_flash_len;
57
Jeroen Hofstee45846052014-10-08 22:57:22 +020058__weak int board_flash_wp_on(void)
Simon Glass5af29bd2013-03-14 07:20:12 +000059{
60 /*
61 * Most flashes can't be detected when write protection is enabled,
62 * so provide a way to let U-Boot gracefully ignore write protected
63 * devices.
64 */
65 return 0;
66}
67
Jeroen Hofstee45846052014-10-08 22:57:22 +020068__weak void cpu_secondary_init_r(void)
Simon Glass5af29bd2013-03-14 07:20:12 +000069{
70}
71
Simon Glass5af29bd2013-03-14 07:20:12 +000072static int initr_secondary_cpu(void)
73{
74 /*
75 * after non-volatile devices & environment is setup and cpu code have
76 * another round to deal with any initialization that might require
77 * full access to the environment or loading of some image (firmware)
78 * from a non-volatile device
79 */
80 /* TODO: maybe define this for all archs? */
81 cpu_secondary_init_r();
82
83 return 0;
84}
Simon Glassdb397fa2013-03-11 14:30:37 +000085
Simon Glass209a1a62013-06-11 11:14:42 -070086static int initr_trace(void)
87{
88#ifdef CONFIG_TRACE
89 trace_init(gd->trace_buff, CONFIG_TRACE_BUFFER_SIZE);
90#endif
91
92 return 0;
93}
94
Simon Glassdb397fa2013-03-11 14:30:37 +000095static int initr_reloc(void)
96{
Simon Glass94890462014-11-10 17:16:43 -070097 /* tell others: relocation done */
98 gd->flags |= GD_FLG_RELOC | GD_FLG_FULL_MALLOC_INIT;
Simon Glassdb397fa2013-03-11 14:30:37 +000099
100 return 0;
101}
102
103#ifdef CONFIG_ARM
104/*
105 * Some of these functions are needed purely because the functions they
106 * call return void. If we change them to return 0, these stubs can go away.
107 */
108static int initr_caches(void)
109{
110 /* Enable caches */
111 enable_caches();
112 return 0;
113}
114#endif
115
Simon Glass5af29bd2013-03-14 07:20:12 +0000116__weak int fixup_cpu(void)
117{
118 return 0;
119}
120
Simon Glassdb397fa2013-03-11 14:30:37 +0000121static int initr_reloc_global_data(void)
122{
Albert ARIBAUD6e294722014-02-22 17:53:43 +0100123#ifdef __ARM__
124 monitor_flash_len = _end - __image_copy_start;
Rick Chen3301bfc2017-12-26 13:55:58 +0800125#elif defined(CONFIG_NDS32) || defined(CONFIG_RISCV)
Kun-Hua Huang89299e22015-08-24 14:52:35 +0800126 monitor_flash_len = (ulong)&_end - (ulong)&_start;
Thomas Choucce3e752014-08-22 11:36:47 +0800127#elif !defined(CONFIG_SANDBOX) && !defined(CONFIG_NIOS2)
Masahiro Yamadad1589242013-05-27 00:37:30 +0000128 monitor_flash_len = (ulong)&__init_end - gd->relocaddr;
Simon Glassdb397fa2013-03-11 14:30:37 +0000129#endif
Simon Glass5af29bd2013-03-14 07:20:12 +0000130#if defined(CONFIG_MPC85xx) || defined(CONFIG_MPC86xx)
131 /*
132 * The gd->cpu pointer is set to an address in flash before relocation.
133 * We need to update it to point to the same CPU entry in RAM.
134 * TODO: why not just add gd->reloc_ofs?
135 */
Masahiro Yamadad1589242013-05-27 00:37:30 +0000136 gd->arch.cpu += gd->relocaddr - CONFIG_SYS_MONITOR_BASE;
Simon Glass5af29bd2013-03-14 07:20:12 +0000137
138 /*
139 * If we didn't know the cpu mask & # cores, we can save them of
140 * now rather than 'computing' them constantly
141 */
142 fixup_cpu();
143#endif
144#ifdef CONFIG_SYS_EXTRA_ENV_RELOC
145 /*
146 * Some systems need to relocate the env_addr pointer early because the
147 * location it points to will get invalidated before env_relocate is
148 * called. One example is on systems that might use a L2 or L3 cache
149 * in SRAM mode and initialize that cache from SRAM mode back to being
150 * a cache in cpu_init_r.
151 */
Masahiro Yamadad1589242013-05-27 00:37:30 +0000152 gd->env_addr += gd->relocaddr - CONFIG_SYS_MONITOR_BASE;
Simon Glass5af29bd2013-03-14 07:20:12 +0000153#endif
Siva Durga Prasad Paladuguabffd312015-12-03 15:46:03 +0100154#ifdef CONFIG_OF_EMBED
155 /*
Mario Sixee9b75b2018-01-15 11:10:30 +0100156 * The fdt_blob needs to be moved to new relocation address
157 * incase of FDT blob is embedded with in image
158 */
Siva Durga Prasad Paladuguabffd312015-12-03 15:46:03 +0100159 gd->fdt_blob += gd->reloc_off;
160#endif
Alexander Graf0bd425a2016-03-04 01:10:01 +0100161#ifdef CONFIG_EFI_LOADER
162 efi_runtime_relocate(gd->relocaddr, NULL);
163#endif
Siva Durga Prasad Paladuguabffd312015-12-03 15:46:03 +0100164
Simon Glass5af29bd2013-03-14 07:20:12 +0000165 return 0;
Simon Glassdb397fa2013-03-11 14:30:37 +0000166}
167
168static int initr_serial(void)
169{
170 serial_initialize();
171 return 0;
172}
173
Daniel Schwierzeckb9da7da2016-01-09 18:34:15 +0100174#if defined(CONFIG_PPC) || defined(CONFIG_M68K) || defined(CONFIG_MIPS)
Simon Glass5af29bd2013-03-14 07:20:12 +0000175static int initr_trap(void)
176{
177 /*
178 * Setup trap handlers
179 */
angelo@sysam.itf245ae92015-02-12 01:40:17 +0100180#if defined(CONFIG_PPC)
Masahiro Yamadad1589242013-05-27 00:37:30 +0000181 trap_init(gd->relocaddr);
angelo@sysam.itf245ae92015-02-12 01:40:17 +0100182#else
183 trap_init(CONFIG_SYS_SDRAM_BASE);
184#endif
Simon Glass5af29bd2013-03-14 07:20:12 +0000185 return 0;
186}
187#endif
188
189#ifdef CONFIG_ADDR_MAP
190static int initr_addr_map(void)
191{
192 init_addr_map();
193
194 return 0;
195}
196#endif
197
Simon Glassdb397fa2013-03-11 14:30:37 +0000198#ifdef CONFIG_POST
199static int initr_post_backlog(void)
200{
201 post_output_backlog();
202 return 0;
203}
204#endif
205
Simon Glass5af29bd2013-03-14 07:20:12 +0000206#if defined(CONFIG_SYS_INIT_RAM_LOCK) && defined(CONFIG_E500)
207static int initr_unlock_ram_in_cache(void)
208{
209 unlock_ram_in_cache(); /* it's time to unlock D-cache in e500 */
210 return 0;
211}
212#endif
213
214#ifdef CONFIG_PCI
215static int initr_pci(void)
216{
Simon Glassb94dc892015-03-05 12:25:25 -0700217#ifndef CONFIG_DM_PCI
Simon Glass5af29bd2013-03-14 07:20:12 +0000218 pci_init();
Simon Glassb94dc892015-03-05 12:25:25 -0700219#endif
Simon Glass5af29bd2013-03-14 07:20:12 +0000220
221 return 0;
222}
223#endif
224
Simon Glass5af29bd2013-03-14 07:20:12 +0000225static int initr_barrier(void)
226{
227#ifdef CONFIG_PPC
228 /* TODO: Can we not use dmb() macros for this? */
229 asm("sync ; isync");
230#endif
231 return 0;
232}
233
Simon Glassdb397fa2013-03-11 14:30:37 +0000234static int initr_malloc(void)
235{
236 ulong malloc_start;
237
Andy Yan1fa20e4d2017-07-24 17:43:34 +0800238#if CONFIG_VAL(SYS_MALLOC_F_LEN)
Simon Glass863e4042014-07-10 22:23:28 -0600239 debug("Pre-reloc malloc() used %#lx bytes (%ld KB)\n", gd->malloc_ptr,
240 gd->malloc_ptr / 1024);
241#endif
Simon Glassdb397fa2013-03-11 14:30:37 +0000242 /* The malloc area is immediately below the monitor copy in DRAM */
Masahiro Yamadad1589242013-05-27 00:37:30 +0000243 malloc_start = gd->relocaddr - TOTAL_MALLOC_LEN;
Simon Glass62cf9122013-04-26 02:53:43 +0000244 mem_malloc_init((ulong)map_sysmem(malloc_start, TOTAL_MALLOC_LEN),
245 TOTAL_MALLOC_LEN);
Simon Glassdb397fa2013-03-11 14:30:37 +0000246 return 0;
247}
Thierry Redingc97d9742014-12-09 22:25:22 -0700248
Simon Glass1bb49232015-11-08 23:47:48 -0700249static int initr_console_record(void)
250{
251#if defined(CONFIG_CONSOLE_RECORD)
252 return console_record_init();
253#else
254 return 0;
255#endif
256}
257
Jan Kiszkaa714a6b92015-03-09 07:47:18 +0100258#ifdef CONFIG_SYS_NONCACHED_MEMORY
259static int initr_noncached(void)
260{
261 noncached_init();
262 return 0;
263}
264#endif
265
Simon Glass46e09772017-05-18 20:08:56 -0600266#ifdef CONFIG_OF_LIVE
267static int initr_of_live(void)
268{
Simon Glassfda60522017-05-22 05:05:36 -0600269 int ret;
270
271 bootstage_start(BOOTSTAGE_ID_ACCUM_OF_LIVE, "of_live");
272 ret = of_live_build(gd->fdt_blob, (struct device_node **)&gd->of_root);
273 bootstage_accum(BOOTSTAGE_ID_ACCUM_OF_LIVE);
274 if (ret)
275 return ret;
276
277 return 0;
Simon Glass46e09772017-05-18 20:08:56 -0600278}
279#endif
280
Simon Glasseea87102014-02-26 15:59:20 -0700281#ifdef CONFIG_DM
282static int initr_dm(void)
283{
Simon Glass8e4f80f2016-02-24 09:14:50 -0700284 int ret;
285
Simon Glassa730c5d2014-07-23 06:55:04 -0600286 /* Save the pre-reloc driver model and start a new one */
287 gd->dm_root_f = gd->dm_root;
288 gd->dm_root = NULL;
Simon Glassb0fb7bd2016-03-11 22:06:46 -0700289#ifdef CONFIG_TIMER
290 gd->timer = NULL;
291#endif
Simon Glass405e2b02017-05-22 05:05:32 -0600292 bootstage_start(BOOTSTATE_ID_ACCUM_DM_R, "dm_r");
Simon Glass8e4f80f2016-02-24 09:14:50 -0700293 ret = dm_init_and_scan(false);
Simon Glass405e2b02017-05-22 05:05:32 -0600294 bootstage_accum(BOOTSTATE_ID_ACCUM_DM_R);
Simon Glass8e4f80f2016-02-24 09:14:50 -0700295 if (ret)
296 return ret;
297#ifdef CONFIG_TIMER_EARLY
Simon Glass8e4f80f2016-02-24 09:14:50 -0700298 ret = dm_timer_init();
299 if (ret)
300 return ret;
Thomas Chou78815972015-10-09 13:48:56 +0800301#endif
Simon Glass8e4f80f2016-02-24 09:14:50 -0700302
303 return 0;
Simon Glasseea87102014-02-26 15:59:20 -0700304}
305#endif
306
Simon Glass7edd07d2015-11-28 22:16:35 -0700307static int initr_bootstage(void)
308{
Simon Glass7edd07d2015-11-28 22:16:35 -0700309 bootstage_mark_name(BOOTSTAGE_ID_START_UBOOT_R, "board_init_r");
310
311 return 0;
312}
313
Simon Glassdb397fa2013-03-11 14:30:37 +0000314__weak int power_init_board(void)
315{
316 return 0;
317}
318
319static int initr_announce(void)
320{
Masahiro Yamadad1589242013-05-27 00:37:30 +0000321 debug("Now running in RAM - U-Boot at: %08lx\n", gd->relocaddr);
Simon Glassdb397fa2013-03-11 14:30:37 +0000322 return 0;
323}
324
Andreas Bießmann9e4f3962015-01-20 00:29:05 +0100325#ifdef CONFIG_NEEDS_MANUAL_RELOC
326static int initr_manual_reloc_cmdtable(void)
327{
328 fixup_cmdtable(ll_entry_start(cmd_tbl_t, cmd),
329 ll_entry_count(cmd_tbl_t, cmd));
330 return 0;
331}
332#endif
333
Masahiro Yamada8cea9b52017-02-11 22:43:54 +0900334#if defined(CONFIG_MTD_NOR_FLASH)
Simon Glassdb397fa2013-03-11 14:30:37 +0000335static int initr_flash(void)
336{
Simon Glass5af29bd2013-03-14 07:20:12 +0000337 ulong flash_size = 0;
338 bd_t *bd = gd->bd;
Simon Glassdb397fa2013-03-11 14:30:37 +0000339
340 puts("Flash: ");
341
Masahiro Yamadaeb0e02b2014-12-05 12:20:58 +0900342 if (board_flash_wp_on())
Simon Glass5af29bd2013-03-14 07:20:12 +0000343 printf("Uninitialized - Write Protect On\n");
Masahiro Yamadaeb0e02b2014-12-05 12:20:58 +0900344 else
Simon Glass5af29bd2013-03-14 07:20:12 +0000345 flash_size = flash_init();
Masahiro Yamadaeb0e02b2014-12-05 12:20:58 +0900346
Simon Glassdb397fa2013-03-11 14:30:37 +0000347 print_size(flash_size, "");
348#ifdef CONFIG_SYS_FLASH_CHECKSUM
349 /*
Mario Sixee9b75b2018-01-15 11:10:30 +0100350 * Compute and print flash CRC if flashchecksum is set to 'y'
351 *
352 * NOTE: Maybe we should add some WATCHDOG_RESET()? XXX
353 */
Simon Glass22c34c22017-08-03 12:22:13 -0600354 if (env_get_yesno("flashchecksum") == 1) {
Mario Sixee9b75b2018-01-15 11:10:30 +0100355 const uchar *flash_base = (const uchar *)CONFIG_SYS_FLASH_BASE;
356
Simon Glassdb397fa2013-03-11 14:30:37 +0000357 printf(" CRC: %08X", crc32(0,
Mario Sixee9b75b2018-01-15 11:10:30 +0100358 flash_base,
359 flash_size));
Simon Glassdb397fa2013-03-11 14:30:37 +0000360 }
361#endif /* CONFIG_SYS_FLASH_CHECKSUM */
362 putc('\n');
363
Simon Glass5af29bd2013-03-14 07:20:12 +0000364 /* update start of FLASH memory */
365#ifdef CONFIG_SYS_FLASH_BASE
366 bd->bi_flashstart = CONFIG_SYS_FLASH_BASE;
367#endif
368 /* size of FLASH memory (final value) */
369 bd->bi_flashsize = flash_size;
370
371#if defined(CONFIG_SYS_UPDATE_FLASH_SIZE)
372 /* Make a update of the Memctrl. */
373 update_flash_size(flash_size);
374#endif
375
Simon Glass5af29bd2013-03-14 07:20:12 +0000376#if defined(CONFIG_OXC) || defined(CONFIG_RMU)
377 /* flash mapped at end of memory map */
378 bd->bi_flashoffset = CONFIG_SYS_TEXT_BASE + flash_size;
379#elif CONFIG_SYS_MONITOR_BASE == CONFIG_SYS_FLASH_BASE
380 bd->bi_flashoffset = monitor_flash_len; /* reserved area for monitor */
381#endif
382 return 0;
383}
384#endif
385
Simon Glass55316ad2014-10-13 23:41:54 -0600386#if defined(CONFIG_PPC) && !defined(CONFIG_DM_SPI)
Simon Glass5af29bd2013-03-14 07:20:12 +0000387static int initr_spi(void)
388{
389 /* PPC does this here */
390#ifdef CONFIG_SPI
391#if !defined(CONFIG_ENV_IS_IN_EEPROM)
392 spi_init_f();
393#endif
394 spi_init_r();
395#endif
Simon Glassdb397fa2013-03-11 14:30:37 +0000396 return 0;
397}
398#endif
399
400#ifdef CONFIG_CMD_NAND
401/* go init the NAND */
Jeroen Hofsteed9e87622014-10-08 22:57:32 +0200402static int initr_nand(void)
Simon Glassdb397fa2013-03-11 14:30:37 +0000403{
404 puts("NAND: ");
405 nand_init();
Hou Zhiqiang9c8b3c72017-03-17 16:12:31 +0800406 printf("%lu MiB\n", nand_size() / 1024);
Simon Glassdb397fa2013-03-11 14:30:37 +0000407 return 0;
408}
409#endif
410
411#if defined(CONFIG_CMD_ONENAND)
412/* go init the NAND */
Jeroen Hofsteed9e87622014-10-08 22:57:32 +0200413static int initr_onenand(void)
Simon Glassdb397fa2013-03-11 14:30:37 +0000414{
415 puts("NAND: ");
416 onenand_init();
417 return 0;
418}
419#endif
420
Masahiro Yamada0a780172017-05-09 20:31:39 +0900421#ifdef CONFIG_MMC
Jeroen Hofsteed9e87622014-10-08 22:57:32 +0200422static int initr_mmc(void)
Simon Glassdb397fa2013-03-11 14:30:37 +0000423{
424 puts("MMC: ");
425 mmc_initialize(gd->bd);
426 return 0;
427}
428#endif
429
Simon Glassdb397fa2013-03-11 14:30:37 +0000430/*
431 * Tell if it's OK to load the environment early in boot.
432 *
Masahiro Yamadab970a892016-02-05 20:49:39 +0900433 * If CONFIG_OF_CONTROL is defined, we'll check with the FDT to see
Simon Glassdb397fa2013-03-11 14:30:37 +0000434 * if this is OK (defaulting to saying it's OK).
435 *
436 * NOTE: Loading the environment early can be a bad idea if security is
437 * important, since no verification is done on the environment.
438 *
439 * @return 0 if environment should not be loaded, !=0 if it is ok to load
440 */
441static int should_load_env(void)
442{
443#ifdef CONFIG_OF_CONTROL
444 return fdtdec_get_config_int(gd->fdt_blob, "load-environment", 1);
445#elif defined CONFIG_DELAY_ENVIRONMENT
446 return 0;
447#else
448 return 1;
449#endif
450}
451
452static int initr_env(void)
453{
454 /* initialize environment */
455 if (should_load_env())
456 env_relocate();
457 else
458 set_default_env(NULL);
Thomas Chou4fda2812015-10-16 08:44:51 +0800459#ifdef CONFIG_OF_CONTROL
Simon Glass4d949a22017-08-03 12:22:10 -0600460 env_set_addr("fdtcontroladdr", gd->fdt_blob);
Thomas Chou4fda2812015-10-16 08:44:51 +0800461#endif
Simon Glassdb397fa2013-03-11 14:30:37 +0000462
463 /* Initialize from environment */
Simon Glass22c34c22017-08-03 12:22:13 -0600464 load_addr = env_get_ulong("loadaddr", 16, load_addr);
Simon Glass5af29bd2013-03-14 07:20:12 +0000465
Simon Glass5af29bd2013-03-14 07:20:12 +0000466 return 0;
467}
468
Andreas Bießmann111bbf52015-02-06 23:06:47 +0100469#ifdef CONFIG_SYS_BOOTPARAMS_LEN
470static int initr_malloc_bootparams(void)
471{
472 gd->bd->bi_boot_params = (ulong)malloc(CONFIG_SYS_BOOTPARAMS_LEN);
473 if (!gd->bd->bi_boot_params) {
474 puts("WARNING: Cannot allocate space for boot parameters\n");
475 return -ENOMEM;
476 }
477 return 0;
478}
479#endif
480
Simon Glassdb397fa2013-03-11 14:30:37 +0000481static int initr_jumptable(void)
482{
483 jumptable_init();
484 return 0;
485}
486
487#if defined(CONFIG_API)
488static int initr_api(void)
489{
490 /* Initialize API */
491 api_init();
492 return 0;
493}
494#endif
495
Simon Glassdb397fa2013-03-11 14:30:37 +0000496/* enable exceptions */
Andy Shevchenko8cb5cdd2017-07-05 16:25:22 +0300497#ifdef CONFIG_ARM
Simon Glassdb397fa2013-03-11 14:30:37 +0000498static int initr_enable_interrupts(void)
499{
500 enable_interrupts();
501 return 0;
502}
Simon Glass2e4622d2013-03-11 07:40:13 +0000503#endif
Simon Glassdb397fa2013-03-11 14:30:37 +0000504
505#ifdef CONFIG_CMD_NET
506static int initr_ethaddr(void)
507{
Simon Glass5af29bd2013-03-14 07:20:12 +0000508 bd_t *bd = gd->bd;
509
510 /* kept around for legacy kernels only ... ignore the next section */
Simon Glass399a9ce2017-08-03 12:22:14 -0600511 eth_env_get_enetaddr("ethaddr", bd->bi_enetaddr);
Simon Glass5af29bd2013-03-14 07:20:12 +0000512#ifdef CONFIG_HAS_ETH1
Simon Glass399a9ce2017-08-03 12:22:14 -0600513 eth_env_get_enetaddr("eth1addr", bd->bi_enet1addr);
Simon Glass5af29bd2013-03-14 07:20:12 +0000514#endif
515#ifdef CONFIG_HAS_ETH2
Simon Glass399a9ce2017-08-03 12:22:14 -0600516 eth_env_get_enetaddr("eth2addr", bd->bi_enet2addr);
Simon Glass5af29bd2013-03-14 07:20:12 +0000517#endif
518#ifdef CONFIG_HAS_ETH3
Simon Glass399a9ce2017-08-03 12:22:14 -0600519 eth_env_get_enetaddr("eth3addr", bd->bi_enet3addr);
Simon Glass5af29bd2013-03-14 07:20:12 +0000520#endif
521#ifdef CONFIG_HAS_ETH4
Simon Glass399a9ce2017-08-03 12:22:14 -0600522 eth_env_get_enetaddr("eth4addr", bd->bi_enet4addr);
Simon Glass5af29bd2013-03-14 07:20:12 +0000523#endif
524#ifdef CONFIG_HAS_ETH5
Simon Glass399a9ce2017-08-03 12:22:14 -0600525 eth_env_get_enetaddr("eth5addr", bd->bi_enet5addr);
Simon Glass5af29bd2013-03-14 07:20:12 +0000526#endif
527 return 0;
528}
529#endif /* CONFIG_CMD_NET */
530
531#ifdef CONFIG_CMD_KGDB
532static int initr_kgdb(void)
533{
534 puts("KGDB: ");
535 kgdb_init();
536 return 0;
537}
538#endif
539
Uri Mashiach4892d392017-01-19 10:51:45 +0200540#if defined(CONFIG_LED_STATUS)
Simon Glass5af29bd2013-03-14 07:20:12 +0000541static int initr_status_led(void)
542{
Uri Mashiach4892d392017-01-19 10:51:45 +0200543#if defined(CONFIG_LED_STATUS_BOOT)
544 status_led_set(CONFIG_LED_STATUS_BOOT, CONFIG_LED_STATUS_BLINKING);
Bernhard Nortmannc5dcb4a2015-08-21 15:13:21 +0200545#else
546 status_led_init();
547#endif
Simon Glass5af29bd2013-03-14 07:20:12 +0000548 return 0;
549}
550#endif
551
Michal Simekc886f352016-09-08 15:06:45 +0200552#if defined(CONFIG_SCSI) && !defined(CONFIG_DM_SCSI)
Simon Glass5af29bd2013-03-14 07:20:12 +0000553static int initr_scsi(void)
554{
Simon Glass5af29bd2013-03-14 07:20:12 +0000555 puts("SCSI: ");
556 scsi_init();
Simon Glassdb397fa2013-03-11 14:30:37 +0000557
558 return 0;
559}
Ian Campbell63d424b2014-07-21 19:23:18 +0100560#endif
Simon Glassdb397fa2013-03-11 14:30:37 +0000561
562#ifdef CONFIG_BITBANGMII
563static int initr_bbmii(void)
564{
565 bb_miiphy_init();
566 return 0;
567}
568#endif
569
570#ifdef CONFIG_CMD_NET
571static int initr_net(void)
572{
573 puts("Net: ");
Joe Hershberger3dbe17e2015-03-22 17:09:06 -0500574 eth_initialize();
Simon Glassdb397fa2013-03-11 14:30:37 +0000575#if defined(CONFIG_RESET_PHY_R)
576 debug("Reset Ethernet PHY\n");
577 reset_phy();
578#endif
579 return 0;
580}
581#endif
582
583#ifdef CONFIG_POST
584static int initr_post(void)
585{
586 post_run(NULL, POST_RAM | post_bootmode_get(0));
587 return 0;
588}
589#endif
590
Simon Glassb569a012017-05-17 03:25:30 -0600591#if defined(CONFIG_CMD_PCMCIA) && !defined(CONFIG_IDE)
Simon Glass5af29bd2013-03-14 07:20:12 +0000592static int initr_pcmcia(void)
593{
594 puts("PCMCIA:");
595 pcmcia_init();
596 return 0;
597}
598#endif
599
Simon Glassb569a012017-05-17 03:25:30 -0600600#if defined(CONFIG_IDE)
Simon Glass5af29bd2013-03-14 07:20:12 +0000601static int initr_ide(void)
602{
Simon Glass5af29bd2013-03-14 07:20:12 +0000603 puts("IDE: ");
Simon Glass5af29bd2013-03-14 07:20:12 +0000604#if defined(CONFIG_START_IDE)
605 if (board_start_ide())
606 ide_init();
607#else
608 ide_init();
609#endif
610 return 0;
611}
612#endif
613
Simon Glass49badb92017-12-04 13:48:23 -0700614#if defined(CONFIG_PRAM)
Simon Glassdb397fa2013-03-11 14:30:37 +0000615/*
616 * Export available size of memory for Linux, taking into account the
617 * protected RAM at top of memory
618 */
619int initr_mem(void)
620{
621 ulong pram = 0;
622 char memsz[32];
623
Simon Glass22c34c22017-08-03 12:22:13 -0600624 pram = env_get_ulong("pram", 10, CONFIG_PRAM);
Mario Sixee9b75b2018-01-15 11:10:30 +0100625 sprintf(memsz, "%ldk", (long int)((gd->ram_size / 1024) - pram));
Simon Glass6a38e412017-08-03 12:22:09 -0600626 env_set("mem", memsz);
Simon Glass5af29bd2013-03-14 07:20:12 +0000627
628 return 0;
Simon Glassdb397fa2013-03-11 14:30:37 +0000629}
630#endif
631
Simon Glass5af29bd2013-03-14 07:20:12 +0000632#ifdef CONFIG_CMD_BEDBUG
633static int initr_bedbug(void)
634{
635 bedbug_init();
636
637 return 0;
638}
639#endif
640
Simon Glassdb397fa2013-03-11 14:30:37 +0000641static int run_main_loop(void)
642{
Simon Glass62cf9122013-04-26 02:53:43 +0000643#ifdef CONFIG_SANDBOX
644 sandbox_main_loop_init();
645#endif
Simon Glassdb397fa2013-03-11 14:30:37 +0000646 /* main_loop() can return to retry autoboot, if so just run it again */
647 for (;;)
648 main_loop();
649 return 0;
650}
651
652/*
653 * Over time we hope to remove these functions with code fragments and
Simon Goldschmidt639725e2018-02-13 13:18:27 +0100654 * stub functions, and instead call the relevant function directly.
Simon Glassdb397fa2013-03-11 14:30:37 +0000655 *
656 * We also hope to remove most of the driver-related init and do it if/when
657 * the driver is later used.
Simon Glass5af29bd2013-03-14 07:20:12 +0000658 *
659 * TODO: perhaps reset the watchdog in the initcall function after each call?
Simon Glassdb397fa2013-03-11 14:30:37 +0000660 */
Simon Glass2031fad2017-01-16 07:03:50 -0700661static init_fnc_t init_sequence_r[] = {
Simon Glass209a1a62013-06-11 11:14:42 -0700662 initr_trace,
Simon Glassdb397fa2013-03-11 14:30:37 +0000663 initr_reloc,
Simon Glass5af29bd2013-03-14 07:20:12 +0000664 /* TODO: could x86/PPC have this also perhaps? */
Simon Glassdb397fa2013-03-11 14:30:37 +0000665#ifdef CONFIG_ARM
666 initr_caches,
York Sun820a18f2015-03-20 19:28:11 -0700667 /* Note: For Freescale LS2 SoCs, new MMU table is created in DDR.
668 * A temporary mapping of IFC high region is since removed,
Mario Sixee9b75b2018-01-15 11:10:30 +0100669 * so environmental variables in NOR flash is not available
York Sun820a18f2015-03-20 19:28:11 -0700670 * until board_init() is called below to remap IFC to high
671 * region.
672 */
Simon Glass712da5e2014-09-03 17:37:01 -0600673#endif
674 initr_reloc_global_data,
York Sun6e78b3b2014-10-02 15:20:10 -0700675#if defined(CONFIG_SYS_INIT_RAM_LOCK) && defined(CONFIG_E500)
676 initr_unlock_ram_in_cache,
677#endif
Simon Glass712da5e2014-09-03 17:37:01 -0600678 initr_barrier,
679 initr_malloc,
Simon Glass55e32ba2017-12-04 13:48:28 -0700680 log_init,
Simon Glasse635af12017-05-22 05:05:31 -0600681 initr_bootstage, /* Needs malloc() but has its own timer */
Simon Glass1bb49232015-11-08 23:47:48 -0700682 initr_console_record,
Jan Kiszkaa714a6b92015-03-09 07:47:18 +0100683#ifdef CONFIG_SYS_NONCACHED_MEMORY
684 initr_noncached,
685#endif
Simon Glass712da5e2014-09-03 17:37:01 -0600686 bootstage_relocate,
Simon Glass46e09772017-05-18 20:08:56 -0600687#ifdef CONFIG_OF_LIVE
688 initr_of_live,
689#endif
Simon Glass712da5e2014-09-03 17:37:01 -0600690#ifdef CONFIG_DM
691 initr_dm,
692#endif
Rick Chen3301bfc2017-12-26 13:55:58 +0800693#if defined(CONFIG_ARM) || defined(CONFIG_NDS32) || defined(CONFIG_RISCV)
Simon Glassdb397fa2013-03-11 14:30:37 +0000694 board_init, /* Setup chipselects */
695#endif
Simon Glass5af29bd2013-03-14 07:20:12 +0000696 /*
697 * TODO: printing of the clock inforamtion of the board is now
698 * implemented as part of bdinfo command. Currently only support for
699 * davinci SOC's is added. Remove this check once all the board
700 * implement this.
701 */
702#ifdef CONFIG_CLOCKS
703 set_cpu_clk_info, /* Setup clock information */
704#endif
Alexander Graf8623f922016-03-04 01:10:04 +0100705#ifdef CONFIG_EFI_LOADER
706 efi_memory_init,
707#endif
Simon Glass712da5e2014-09-03 17:37:01 -0600708 stdio_init_tables,
Simon Glassdb397fa2013-03-11 14:30:37 +0000709 initr_serial,
710 initr_announce,
Simon Glass5af29bd2013-03-14 07:20:12 +0000711 INIT_FUNC_WATCHDOG_RESET
Andreas Bießmann9e4f3962015-01-20 00:29:05 +0100712#ifdef CONFIG_NEEDS_MANUAL_RELOC
713 initr_manual_reloc_cmdtable,
714#endif
Daniel Schwierzeckb9da7da2016-01-09 18:34:15 +0100715#if defined(CONFIG_PPC) || defined(CONFIG_M68K) || defined(CONFIG_MIPS)
Simon Glass5af29bd2013-03-14 07:20:12 +0000716 initr_trap,
717#endif
718#ifdef CONFIG_ADDR_MAP
719 initr_addr_map,
720#endif
721#if defined(CONFIG_BOARD_EARLY_INIT_R)
722 board_early_init_r,
723#endif
724 INIT_FUNC_WATCHDOG_RESET
Simon Glassdb397fa2013-03-11 14:30:37 +0000725#ifdef CONFIG_POST
726 initr_post_backlog,
727#endif
Simon Glass5af29bd2013-03-14 07:20:12 +0000728 INIT_FUNC_WATCHDOG_RESET
Simon Glass5af29bd2013-03-14 07:20:12 +0000729#if defined(CONFIG_PCI) && defined(CONFIG_SYS_EARLY_PCI_INIT)
730 /*
731 * Do early PCI configuration _before_ the flash gets initialised,
Mario Sixee9b75b2018-01-15 11:10:30 +0100732 * because PCU resources are crucial for flash access on some boards.
Simon Glass5af29bd2013-03-14 07:20:12 +0000733 */
734 initr_pci,
735#endif
Simon Glassdb397fa2013-03-11 14:30:37 +0000736#ifdef CONFIG_ARCH_EARLY_INIT_R
737 arch_early_init_r,
738#endif
739 power_init_board,
Masahiro Yamada8cea9b52017-02-11 22:43:54 +0900740#ifdef CONFIG_MTD_NOR_FLASH
Simon Glassdb397fa2013-03-11 14:30:37 +0000741 initr_flash,
742#endif
Simon Glass5af29bd2013-03-14 07:20:12 +0000743 INIT_FUNC_WATCHDOG_RESET
Tom Rinifef2bd12017-03-14 11:08:11 -0400744#if defined(CONFIG_PPC) || defined(CONFIG_M68K) || defined(CONFIG_X86)
Simon Glass5af29bd2013-03-14 07:20:12 +0000745 /* initialize higher level parts of CPU like time base and timers */
746 cpu_init_r,
Simon Glass6f5567d2013-03-05 14:39:53 +0000747#endif
748#ifdef CONFIG_PPC
Simon Glass5af29bd2013-03-14 07:20:12 +0000749 initr_spi,
750#endif
Simon Glassdb397fa2013-03-11 14:30:37 +0000751#ifdef CONFIG_CMD_NAND
752 initr_nand,
753#endif
754#ifdef CONFIG_CMD_ONENAND
755 initr_onenand,
756#endif
Masahiro Yamada0a780172017-05-09 20:31:39 +0900757#ifdef CONFIG_MMC
Simon Glassdb397fa2013-03-11 14:30:37 +0000758 initr_mmc,
759#endif
Simon Glassdb397fa2013-03-11 14:30:37 +0000760 initr_env,
Andreas Bießmann111bbf52015-02-06 23:06:47 +0100761#ifdef CONFIG_SYS_BOOTPARAMS_LEN
762 initr_malloc_bootparams,
763#endif
Simon Glass5af29bd2013-03-14 07:20:12 +0000764 INIT_FUNC_WATCHDOG_RESET
765 initr_secondary_cpu,
Simon Glass5af29bd2013-03-14 07:20:12 +0000766#if defined(CONFIG_ID_EEPROM) || defined(CONFIG_SYS_I2C_MAC_OFFSET)
767 mac_read_from_eeprom,
768#endif
769 INIT_FUNC_WATCHDOG_RESET
770#if defined(CONFIG_PCI) && !defined(CONFIG_SYS_EARLY_PCI_INIT)
771 /*
772 * Do pci configuration
773 */
774 initr_pci,
775#endif
Simon Glass712da5e2014-09-03 17:37:01 -0600776 stdio_add_devices,
Simon Glassdb397fa2013-03-11 14:30:37 +0000777 initr_jumptable,
778#ifdef CONFIG_API
779 initr_api,
780#endif
781 console_init_r, /* fully init console as a device */
782#ifdef CONFIG_DISPLAY_BOARDINFO_LATE
Simon Glassc9c5c9f2017-06-15 21:37:50 -0600783 console_announce_r,
Masahiro Yamada9607f7a2015-01-14 17:07:05 +0900784 show_board_info,
Simon Glassdb397fa2013-03-11 14:30:37 +0000785#endif
786#ifdef CONFIG_ARCH_MISC_INIT
787 arch_misc_init, /* miscellaneous arch-dependent init */
788#endif
789#ifdef CONFIG_MISC_INIT_R
790 misc_init_r, /* miscellaneous platform-dependent init */
791#endif
Simon Glass5af29bd2013-03-14 07:20:12 +0000792 INIT_FUNC_WATCHDOG_RESET
793#ifdef CONFIG_CMD_KGDB
794 initr_kgdb,
795#endif
Simon Glassdb397fa2013-03-11 14:30:37 +0000796 interrupt_init,
Andy Shevchenko8cb5cdd2017-07-05 16:25:22 +0300797#ifdef CONFIG_ARM
Simon Glassdb397fa2013-03-11 14:30:37 +0000798 initr_enable_interrupts,
Simon Glass5af29bd2013-03-14 07:20:12 +0000799#endif
Andy Shevchenko8cb5cdd2017-07-05 16:25:22 +0300800#if defined(CONFIG_MICROBLAZE) || defined(CONFIG_M68K)
Simon Glass6f5567d2013-03-05 14:39:53 +0000801 timer_init, /* initialize timer */
802#endif
Uri Mashiach4892d392017-01-19 10:51:45 +0200803#if defined(CONFIG_LED_STATUS)
Simon Glass5af29bd2013-03-14 07:20:12 +0000804 initr_status_led,
805#endif
806 /* PPC has a udelay(20) here dating from 2002. Why? */
Simon Glassdb397fa2013-03-11 14:30:37 +0000807#ifdef CONFIG_CMD_NET
808 initr_ethaddr,
809#endif
810#ifdef CONFIG_BOARD_LATE_INIT
811 board_late_init,
812#endif
Michal Simekc886f352016-09-08 15:06:45 +0200813#if defined(CONFIG_SCSI) && !defined(CONFIG_DM_SCSI)
Simon Glass5af29bd2013-03-14 07:20:12 +0000814 INIT_FUNC_WATCHDOG_RESET
815 initr_scsi,
816#endif
Simon Glassdb397fa2013-03-11 14:30:37 +0000817#ifdef CONFIG_BITBANGMII
818 initr_bbmii,
819#endif
820#ifdef CONFIG_CMD_NET
Simon Glass5af29bd2013-03-14 07:20:12 +0000821 INIT_FUNC_WATCHDOG_RESET
Simon Glassdb397fa2013-03-11 14:30:37 +0000822 initr_net,
823#endif
824#ifdef CONFIG_POST
825 initr_post,
826#endif
Simon Glassb569a012017-05-17 03:25:30 -0600827#if defined(CONFIG_CMD_PCMCIA) && !defined(CONFIG_IDE)
Simon Glass5af29bd2013-03-14 07:20:12 +0000828 initr_pcmcia,
829#endif
Simon Glassb569a012017-05-17 03:25:30 -0600830#if defined(CONFIG_IDE)
Simon Glass5af29bd2013-03-14 07:20:12 +0000831 initr_ide,
832#endif
833#ifdef CONFIG_LAST_STAGE_INIT
834 INIT_FUNC_WATCHDOG_RESET
835 /*
836 * Some parts can be only initialized if all others (like
837 * Interrupts) are up and running (i.e. the PC-style ISA
838 * keyboard).
839 */
840 last_stage_init,
841#endif
842#ifdef CONFIG_CMD_BEDBUG
843 INIT_FUNC_WATCHDOG_RESET
844 initr_bedbug,
845#endif
Simon Glass49badb92017-12-04 13:48:23 -0700846#if defined(CONFIG_PRAM)
Simon Glass5af29bd2013-03-14 07:20:12 +0000847 initr_mem,
848#endif
Simon Glassdb397fa2013-03-11 14:30:37 +0000849 run_main_loop,
850};
851
852void board_init_r(gd_t *new_gd, ulong dest_addr)
853{
Simon Glass96c867d2017-01-16 07:03:51 -0700854 /*
855 * Set up the new global data pointer. So far only x86 does this
856 * here.
857 * TODO(sjg@chromium.org): Consider doing this for all archs, or
858 * dropping the new_gd parameter.
859 */
860#if CONFIG_IS_ENABLED(X86_64)
861 arch_setup_gd(new_gd);
862#endif
863
Alexey Brodkin3f0516f2014-01-20 14:30:39 +0400864#ifdef CONFIG_NEEDS_MANUAL_RELOC
865 int i;
866#endif
867
Jeroen Hofstee498f97f2014-07-30 21:54:49 +0200868#if !defined(CONFIG_X86) && !defined(CONFIG_ARM) && !defined(CONFIG_ARM64)
Simon Glassdb397fa2013-03-11 14:30:37 +0000869 gd = new_gd;
Simon Glass6f5567d2013-03-05 14:39:53 +0000870#endif
Simon Glass55e32ba2017-12-04 13:48:28 -0700871 gd->flags &= ~GD_FLG_LOG_READY;
Alexey Brodkin3f0516f2014-01-20 14:30:39 +0400872
873#ifdef CONFIG_NEEDS_MANUAL_RELOC
874 for (i = 0; i < ARRAY_SIZE(init_sequence_r); i++)
875 init_sequence_r[i] += gd->reloc_off;
876#endif
877
Simon Glassdb397fa2013-03-11 14:30:37 +0000878 if (initcall_run_list(init_sequence_r))
879 hang();
880
881 /* NOTREACHED - run_main_loop() does not return */
882 hang();
883}