xref: /rk3399_rockchip-uboot/common/board_r.c (revision 2a3fb7bb049d69d96f3bc7dae8caa756fdc8a613)
1 /*
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  *
10  * SPDX-License-Identifier:	GPL-2.0+
11  */
12 
13 #include <common.h>
14 #include <api.h>
15 /* 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
19 #include <command.h>
20 #include <console.h>
21 #include <dm.h>
22 #include <environment.h>
23 #include <fdtdec.h>
24 #include <ide.h>
25 #include <initcall.h>
26 #include <init_helpers.h>
27 #ifdef CONFIG_PS2KBD
28 #include <keyboard.h>
29 #endif
30 #if defined(CONFIG_CMD_KGDB)
31 #include <kgdb.h>
32 #endif
33 #include <malloc.h>
34 #include <mapmem.h>
35 #include <memalign.h>
36 #ifdef CONFIG_BITBANGMII
37 #include <miiphy.h>
38 #endif
39 #include <mmc.h>
40 #include <nand.h>
41 #include <of_live.h>
42 #include <onenand_uboot.h>
43 #include <scsi.h>
44 #include <serial.h>
45 #include <spi.h>
46 #include <stdio_dev.h>
47 #include <timer.h>
48 #include <trace.h>
49 #include <watchdog.h>
50 #ifdef CONFIG_ADDR_MAP
51 #include <asm/mmu.h>
52 #endif
53 #include <asm/sections.h>
54 #include <dm/root.h>
55 #include <linux/compiler.h>
56 #include <linux/err.h>
57 #include <efi_loader.h>
58 #include <sysmem.h>
59 #include <bidram.h>
60 
61 DECLARE_GLOBAL_DATA_PTR;
62 
63 ulong monitor_flash_len;
64 
65 __weak int board_flash_wp_on(void)
66 {
67 	/*
68 	 * Most flashes can't be detected when write protection is enabled,
69 	 * so provide a way to let U-Boot gracefully ignore write protected
70 	 * devices.
71 	 */
72 	return 0;
73 }
74 
75 __weak void cpu_secondary_init_r(void)
76 {
77 }
78 
79 static int initr_secondary_cpu(void)
80 {
81 	/*
82 	 * after non-volatile devices & environment is setup and cpu code have
83 	 * another round to deal with any initialization that might require
84 	 * full access to the environment or loading of some image (firmware)
85 	 * from a non-volatile device
86 	 */
87 	/* TODO: maybe define this for all archs? */
88 	cpu_secondary_init_r();
89 
90 	return 0;
91 }
92 
93 static int initr_trace(void)
94 {
95 #ifdef CONFIG_TRACE
96 	trace_init(gd->trace_buff, CONFIG_TRACE_BUFFER_SIZE);
97 #endif
98 
99 	return 0;
100 }
101 
102 static int initr_reloc(void)
103 {
104 	/* tell others: relocation done */
105 	gd->flags |= GD_FLG_RELOC | GD_FLG_FULL_MALLOC_INIT;
106 
107 	return 0;
108 }
109 
110 #ifdef CONFIG_ARM
111 /*
112  * Some of these functions are needed purely because the functions they
113  * call return void. If we change them to return 0, these stubs can go away.
114  */
115 static int initr_caches(void)
116 {
117 	/* Enable caches */
118 	enable_caches();
119 	return 0;
120 }
121 #endif
122 
123 __weak int fixup_cpu(void)
124 {
125 	return 0;
126 }
127 
128 static int initr_reloc_global_data(void)
129 {
130 #ifdef __ARM__
131 	monitor_flash_len = _end - __image_copy_start;
132 #elif defined(CONFIG_NDS32) || defined(CONFIG_RISCV)
133 	monitor_flash_len = (ulong)&_end - (ulong)&_start;
134 #elif !defined(CONFIG_SANDBOX) && !defined(CONFIG_NIOS2)
135 	monitor_flash_len = (ulong)&__init_end - gd->relocaddr;
136 #endif
137 #if defined(CONFIG_MPC85xx) || defined(CONFIG_MPC86xx)
138 	/*
139 	 * The gd->cpu pointer is set to an address in flash before relocation.
140 	 * We need to update it to point to the same CPU entry in RAM.
141 	 * TODO: why not just add gd->reloc_ofs?
142 	 */
143 	gd->arch.cpu += gd->relocaddr - CONFIG_SYS_MONITOR_BASE;
144 
145 	/*
146 	 * If we didn't know the cpu mask & # cores, we can save them of
147 	 * now rather than 'computing' them constantly
148 	 */
149 	fixup_cpu();
150 #endif
151 #ifdef CONFIG_SYS_EXTRA_ENV_RELOC
152 	/*
153 	 * Some systems need to relocate the env_addr pointer early because the
154 	 * location it points to will get invalidated before env_relocate is
155 	 * called.  One example is on systems that might use a L2 or L3 cache
156 	 * in SRAM mode and initialize that cache from SRAM mode back to being
157 	 * a cache in cpu_init_r.
158 	 */
159 	gd->env_addr += gd->relocaddr - CONFIG_SYS_MONITOR_BASE;
160 #endif
161 #ifdef CONFIG_OF_EMBED
162 	/*
163 	* The fdt_blob needs to be moved to new relocation address
164 	* incase of FDT blob is embedded with in image
165 	*/
166 	gd->fdt_blob += gd->reloc_off;
167 #endif
168 #ifdef CONFIG_EFI_LOADER
169 	efi_runtime_relocate(gd->relocaddr, NULL);
170 #endif
171 
172 	return 0;
173 }
174 
175 static int initr_serial(void)
176 {
177 	serial_initialize();
178 	return 0;
179 }
180 
181 #if defined(CONFIG_PPC) || defined(CONFIG_M68K) || defined(CONFIG_MIPS)
182 static int initr_trap(void)
183 {
184 	/*
185 	 * Setup trap handlers
186 	 */
187 #if defined(CONFIG_PPC)
188 	trap_init(gd->relocaddr);
189 #else
190 	trap_init(CONFIG_SYS_SDRAM_BASE);
191 #endif
192 	return 0;
193 }
194 #endif
195 
196 #ifdef CONFIG_ADDR_MAP
197 static int initr_addr_map(void)
198 {
199 	init_addr_map();
200 
201 	return 0;
202 }
203 #endif
204 
205 #ifdef CONFIG_POST
206 static int initr_post_backlog(void)
207 {
208 	post_output_backlog();
209 	return 0;
210 }
211 #endif
212 
213 #if defined(CONFIG_SYS_INIT_RAM_LOCK) && defined(CONFIG_E500)
214 static int initr_unlock_ram_in_cache(void)
215 {
216 	unlock_ram_in_cache();	/* it's time to unlock D-cache in e500 */
217 	return 0;
218 }
219 #endif
220 
221 #ifdef CONFIG_PCI
222 static int initr_pci(void)
223 {
224 #ifndef CONFIG_DM_PCI
225 	pci_init();
226 #endif
227 
228 	return 0;
229 }
230 #endif
231 
232 static int initr_barrier(void)
233 {
234 #ifdef CONFIG_PPC
235 	/* TODO: Can we not use dmb() macros for this? */
236 	asm("sync ; isync");
237 #endif
238 	return 0;
239 }
240 
241 static int initr_malloc(void)
242 {
243 	ulong malloc_start;
244 
245 #if CONFIG_VAL(SYS_MALLOC_F_LEN)
246 	debug("Pre-reloc malloc() used %#lx bytes (%ld KB)\n", gd->malloc_ptr,
247 	      gd->malloc_ptr / 1024);
248 #endif
249 	/* The malloc area is immediately below the monitor copy in DRAM */
250 	malloc_start = gd->relocaddr - TOTAL_MALLOC_LEN;
251 	mem_malloc_init((ulong)map_sysmem(malloc_start, TOTAL_MALLOC_LEN),
252 			TOTAL_MALLOC_LEN);
253 	return 0;
254 }
255 
256 static int initr_console_record(void)
257 {
258 #if defined(CONFIG_CONSOLE_RECORD)
259 	int ret;
260 
261 	ret = console_record_init();
262 	if (!ret)
263 		console_record_reset_enable();
264 	return ret;
265 #else
266 	return 0;
267 #endif
268 }
269 
270 #ifdef CONFIG_SYS_NONCACHED_MEMORY
271 static int initr_noncached(void)
272 {
273 	noncached_init();
274 	return 0;
275 }
276 #endif
277 
278 #ifdef CONFIG_OF_LIVE
279 static int initr_of_live(void)
280 {
281 	int ret;
282 
283 	bootstage_start(BOOTSTAGE_ID_ACCUM_OF_LIVE, "of_live");
284 	ret = of_live_build(gd->fdt_blob, (struct device_node **)&gd->of_root);
285 	bootstage_accum(BOOTSTAGE_ID_ACCUM_OF_LIVE);
286 	if (ret)
287 		return ret;
288 
289 	return 0;
290 }
291 #endif
292 
293 #ifdef CONFIG_DM
294 static int initr_dm(void)
295 {
296 	int ret;
297 
298 	/* Save the pre-reloc driver model and start a new one */
299 	gd->dm_root_f = gd->dm_root;
300 	gd->dm_root = NULL;
301 #ifdef CONFIG_TIMER
302 	gd->timer = NULL;
303 #endif
304 	bootstage_start(BOOTSTATE_ID_ACCUM_DM_R, "dm_r");
305 	ret = dm_init_and_scan(false);
306 	bootstage_accum(BOOTSTATE_ID_ACCUM_DM_R);
307 	if (ret)
308 		return ret;
309 #ifdef CONFIG_TIMER_EARLY
310 	ret = dm_timer_init();
311 	if (ret)
312 		return ret;
313 #endif
314 
315 	return 0;
316 }
317 #endif
318 
319 static int initr_bootstage(void)
320 {
321 	bootstage_mark_name(BOOTSTAGE_ID_START_UBOOT_R, "board_init_r");
322 
323 	return 0;
324 }
325 
326 __weak int power_init_board(void)
327 {
328 	return 0;
329 }
330 
331 static int initr_announce(void)
332 {
333 	ulong addr;
334 
335 #ifndef CONFIG_SKIP_RELOCATE_UBOOT
336 	addr = gd->relocaddr;
337 #else
338 	addr = CONFIG_SYS_TEXT_BASE;
339 #endif
340 	debug("Now running in RAM - U-Boot at: %08lx\n", addr);
341 
342 	return 0;
343 }
344 
345 #ifdef CONFIG_NEEDS_MANUAL_RELOC
346 static int initr_manual_reloc_cmdtable(void)
347 {
348 	fixup_cmdtable(ll_entry_start(cmd_tbl_t, cmd),
349 		       ll_entry_count(cmd_tbl_t, cmd));
350 	return 0;
351 }
352 #endif
353 
354 #if defined(CONFIG_MTD_NOR_FLASH)
355 static int initr_flash(void)
356 {
357 	ulong flash_size = 0;
358 	bd_t *bd = gd->bd;
359 
360 	puts("Flash: ");
361 
362 	if (board_flash_wp_on())
363 		printf("Uninitialized - Write Protect On\n");
364 	else
365 		flash_size = flash_init();
366 
367 	print_size(flash_size, "");
368 #ifdef CONFIG_SYS_FLASH_CHECKSUM
369 	/*
370 	* Compute and print flash CRC if flashchecksum is set to 'y'
371 	*
372 	* NOTE: Maybe we should add some WATCHDOG_RESET()? XXX
373 	*/
374 	if (env_get_yesno("flashchecksum") == 1) {
375 		printf("  CRC: %08X", crc32(0,
376 			(const unsigned char *) CONFIG_SYS_FLASH_BASE,
377 			flash_size));
378 	}
379 #endif /* CONFIG_SYS_FLASH_CHECKSUM */
380 	putc('\n');
381 
382 	/* update start of FLASH memory    */
383 #ifdef CONFIG_SYS_FLASH_BASE
384 	bd->bi_flashstart = CONFIG_SYS_FLASH_BASE;
385 #endif
386 	/* size of FLASH memory (final value) */
387 	bd->bi_flashsize = flash_size;
388 
389 #if defined(CONFIG_SYS_UPDATE_FLASH_SIZE)
390 	/* Make a update of the Memctrl. */
391 	update_flash_size(flash_size);
392 #endif
393 
394 
395 #if defined(CONFIG_OXC) || defined(CONFIG_RMU)
396 	/* flash mapped at end of memory map */
397 	bd->bi_flashoffset = CONFIG_SYS_TEXT_BASE + flash_size;
398 #elif CONFIG_SYS_MONITOR_BASE == CONFIG_SYS_FLASH_BASE
399 	bd->bi_flashoffset = monitor_flash_len;	/* reserved area for monitor */
400 #endif
401 	return 0;
402 }
403 #endif
404 
405 #if defined(CONFIG_PPC) && !defined(CONFIG_DM_SPI)
406 static int initr_spi(void)
407 {
408 	/* MPC8xx does this here */
409 #ifdef CONFIG_MPC8XX_SPI
410 #if !defined(CONFIG_ENV_IS_IN_EEPROM)
411 	spi_init_f();
412 #endif
413 	spi_init_r();
414 #endif
415 	return 0;
416 }
417 #endif
418 
419 #ifdef CONFIG_CMD_NAND
420 /* go init the NAND */
421 static int initr_nand(void)
422 {
423 #ifndef CONFIG_USING_KERNEL_DTB
424 	puts("NAND:  ");
425 	nand_init();
426 	printf("%lu MiB\n", nand_size() / 1024);
427 #endif
428 	return 0;
429 }
430 #endif
431 
432 #if defined(CONFIG_CMD_ONENAND)
433 /* go init the NAND */
434 static int initr_onenand(void)
435 {
436 	puts("NAND:  ");
437 	onenand_init();
438 	return 0;
439 }
440 #endif
441 
442 #ifdef CONFIG_MMC
443 static int initr_mmc(void)
444 {
445 /*
446  * When CONFIG_USING_KERNEL_DTB is enabled, mmc has been initialized earlier.
447  */
448 #ifndef CONFIG_USING_KERNEL_DTB
449 	puts("MMC:   ");
450 	mmc_initialize(gd->bd);
451 #endif
452 	return 0;
453 }
454 #endif
455 
456 #ifdef CONFIG_MTD_BLK
457 static int initr_mtd_blk(void)
458 {
459 #ifndef CONFIG_USING_KERNEL_DTB
460 	struct blk_desc *dev_desc;
461 
462 	puts("mtd_blk:   ");
463 	dev_desc = rockchip_get_bootdev();
464 	if (dev_desc)
465 		mtd_blk_map_partitions(dev_desc);
466 #endif
467 	return 0;
468 }
469 #endif
470 
471 #if !defined(CONFIG_USING_KERNEL_DTB) || !defined(CONFIG_ENV_IS_NOWHERE)
472 /*
473  * Tell if it's OK to load the environment early in boot.
474  *
475  * If CONFIG_OF_CONTROL is defined, we'll check with the FDT to see
476  * if this is OK (defaulting to saying it's OK).
477  *
478  * NOTE: Loading the environment early can be a bad idea if security is
479  *       important, since no verification is done on the environment.
480  *
481  * @return 0 if environment should not be loaded, !=0 if it is ok to load
482  */
483 static int should_load_env(void)
484 {
485 #ifdef CONFIG_OF_CONTROL
486 	return fdtdec_get_config_int(gd->fdt_blob, "load-environment", 1);
487 #elif defined CONFIG_DELAY_ENVIRONMENT
488 	return 0;
489 #else
490 	return 1;
491 #endif
492 }
493 
494 static int initr_env(void)
495 {
496 	/* initialize environment */
497 	if (should_load_env())
498 		env_relocate();
499 	else
500 		set_default_env(NULL);
501 #ifdef CONFIG_OF_CONTROL
502 	env_set_addr("fdtcontroladdr", gd->fdt_blob);
503 #endif
504 
505 	/* Initialize from environment */
506 	load_addr = env_get_ulong("loadaddr", 16, load_addr);
507 
508 	return 0;
509 }
510 #endif
511 
512 #ifdef CONFIG_USING_KERNEL_DTB
513 /*
514  * If !defined(CONFIG_ENV_IS_NOWHERE):
515  *
516  * Storage env or kernel dtb load depends on bootdev, while bootdev
517  * depends on env varname: devtype, devnum and rkimg_bootdev, etc.
518  * So we have to use nowhere env firstly and cover the storage env
519  * after it is loaded.
520  *
521  * Providing a minimum and necessary nowhere env for board init to
522  * avoid covering the other varnames in storage env.
523  */
524 static int initr_env_nowhere(void)
525 {
526 #ifdef CONFIG_ENV_IS_NOWHERE
527 	set_default_env(NULL);
528 	return 0;
529 #else
530 	const char env_minimum[] = {
531 		ENV_MEM_LAYOUT_SETTINGS
532 #ifdef ENV_MEM_LAYOUT_SETTINGS1
533 		ENV_MEM_LAYOUT_SETTINGS1
534 #endif
535 #ifdef RKIMG_DET_BOOTDEV
536 		RKIMG_DET_BOOTDEV
537 #endif
538 	};
539 
540 	return set_board_env((char *)env_minimum, ENV_SIZE, 0, true);
541 #endif
542 }
543 
544 #if !defined(CONFIG_ENV_IS_NOWHERE)
545 /*
546  * storage has been initialized in board_init(), we could switch env
547  * from nowhere to storage, i.e. CONFIG_ENV_IS_IN_xxx=y.
548  */
549 static int initr_env_switch(void)
550 {
551 	ALLOC_CACHE_ALIGN_BUFFER(env_t, env_nowhere, 1);
552 	char *data;
553 	int ret;
554 
555 	data = env_get("bootargs");
556 	if (data) {
557 		env_set("bootargs_tmp", data);
558 		env_set("bootargs", NULL);
559 	}
560 
561 	/* Export nowhere env for late use */
562 	ret = env_export(env_nowhere);
563 	if (ret) {
564 		printf("%s: export nowhere env fail, ret=%d\n", __func__, ret);
565 		return -EINVAL;
566 	}
567 
568 	/* Destroy nowhere env and import storage env */
569 	initr_env();
570 
571 	/* Append/override nowhere env to storage env */
572 	set_board_env((char *)env_nowhere->data, ENV_SIZE, H_NOCLEAR, false);
573 
574 	/*
575 	 * Restore nowhere bootargs to append/override the one in env storage.
576 	 *
577 	 * Without this, the entire "bootargs" in storage env is replaces by
578 	 * the one in env_nowhere->data.
579 	 */
580 	data = env_get("bootargs_tmp");
581 	if (data) {
582 		env_update("bootargs", data);
583 		env_set("bootargs_tmp", NULL);
584 	}
585 
586 	return 0;
587 }
588 #endif	/* CONFIG_ENV_IS_NOWHERE */
589 #endif	/* CONFIG_USING_KERNEL_DTB */
590 
591 #ifdef CONFIG_SYS_BOOTPARAMS_LEN
592 static int initr_malloc_bootparams(void)
593 {
594 	gd->bd->bi_boot_params = (ulong)malloc(CONFIG_SYS_BOOTPARAMS_LEN);
595 	if (!gd->bd->bi_boot_params) {
596 		puts("WARNING: Cannot allocate space for boot parameters\n");
597 		return -ENOMEM;
598 	}
599 	return 0;
600 }
601 #endif
602 
603 static int initr_jumptable(void)
604 {
605 	jumptable_init();
606 	return 0;
607 }
608 
609 #if defined(CONFIG_API)
610 static int initr_api(void)
611 {
612 	/* Initialize API */
613 	api_init();
614 	return 0;
615 }
616 #endif
617 
618 /* enable exceptions */
619 #ifdef CONFIG_ARM
620 static int initr_enable_interrupts(void)
621 {
622 	enable_interrupts();
623 	return 0;
624 }
625 #endif
626 
627 #ifdef CONFIG_CMD_NET
628 static int initr_ethaddr(void)
629 {
630 	bd_t *bd = gd->bd;
631 
632 	/* kept around for legacy kernels only ... ignore the next section */
633 	eth_env_get_enetaddr("ethaddr", bd->bi_enetaddr);
634 #ifdef CONFIG_HAS_ETH1
635 	eth_env_get_enetaddr("eth1addr", bd->bi_enet1addr);
636 #endif
637 #ifdef CONFIG_HAS_ETH2
638 	eth_env_get_enetaddr("eth2addr", bd->bi_enet2addr);
639 #endif
640 #ifdef CONFIG_HAS_ETH3
641 	eth_env_get_enetaddr("eth3addr", bd->bi_enet3addr);
642 #endif
643 #ifdef CONFIG_HAS_ETH4
644 	eth_env_get_enetaddr("eth4addr", bd->bi_enet4addr);
645 #endif
646 #ifdef CONFIG_HAS_ETH5
647 	eth_env_get_enetaddr("eth5addr", bd->bi_enet5addr);
648 #endif
649 	return 0;
650 }
651 #endif /* CONFIG_CMD_NET */
652 
653 #ifdef CONFIG_CMD_KGDB
654 static int initr_kgdb(void)
655 {
656 	puts("KGDB:  ");
657 	kgdb_init();
658 	return 0;
659 }
660 #endif
661 
662 #if defined(CONFIG_LED_STATUS)
663 static int initr_status_led(void)
664 {
665 #if defined(CONFIG_LED_STATUS_BOOT)
666 	status_led_set(CONFIG_LED_STATUS_BOOT, CONFIG_LED_STATUS_BLINKING);
667 #else
668 	status_led_init();
669 #endif
670 	return 0;
671 }
672 #endif
673 
674 #if defined(CONFIG_SCSI) && !defined(CONFIG_DM_SCSI)
675 static int initr_scsi(void)
676 {
677 	puts("SCSI:  ");
678 	scsi_init();
679 
680 	return 0;
681 }
682 #endif
683 
684 #ifdef CONFIG_BITBANGMII
685 static int initr_bbmii(void)
686 {
687 	bb_miiphy_init();
688 	return 0;
689 }
690 #endif
691 
692 #ifdef CONFIG_CMD_NET
693 static int initr_net(void)
694 {
695 	puts("Net:   ");
696 	eth_initialize();
697 #if defined(CONFIG_RESET_PHY_R)
698 	debug("Reset Ethernet PHY\n");
699 	reset_phy();
700 #endif
701 	return 0;
702 }
703 #endif
704 
705 #ifdef CONFIG_POST
706 static int initr_post(void)
707 {
708 	post_run(NULL, POST_RAM | post_bootmode_get(0));
709 	return 0;
710 }
711 #endif
712 
713 #if defined(CONFIG_CMD_PCMCIA) && !defined(CONFIG_IDE)
714 static int initr_pcmcia(void)
715 {
716 	puts("PCMCIA:");
717 	pcmcia_init();
718 	return 0;
719 }
720 #endif
721 
722 #if defined(CONFIG_IDE)
723 static int initr_ide(void)
724 {
725 	puts("IDE:   ");
726 #if defined(CONFIG_START_IDE)
727 	if (board_start_ide())
728 		ide_init();
729 #else
730 	ide_init();
731 #endif
732 	return 0;
733 }
734 #endif
735 
736 #if defined(CONFIG_PRAM)
737 /*
738  * Export available size of memory for Linux, taking into account the
739  * protected RAM at top of memory
740  */
741 int initr_mem(void)
742 {
743 	ulong pram = 0;
744 	char memsz[32];
745 
746 # ifdef CONFIG_PRAM
747 	pram = env_get_ulong("pram", 10, CONFIG_PRAM);
748 # endif
749 	sprintf(memsz, "%ldk", (long int) ((gd->ram_size / 1024) - pram));
750 	env_set("mem", memsz);
751 
752 	return 0;
753 }
754 #endif
755 
756 #ifdef CONFIG_CMD_BEDBUG
757 static int initr_bedbug(void)
758 {
759 	bedbug_init();
760 
761 	return 0;
762 }
763 #endif
764 
765 #ifdef CONFIG_PS2KBD
766 static int initr_kbd(void)
767 {
768 	puts("PS/2:  ");
769 	kbd_init();
770 	return 0;
771 }
772 #endif
773 
774 __weak int interrupt_debugger_init(void)
775 {
776 	return 0;
777 }
778 
779 __weak int board_initr_caches_fixup(void)
780 {
781 	return 0;
782 }
783 
784 static int run_main_loop(void)
785 {
786 #ifdef CONFIG_SANDBOX
787 	sandbox_main_loop_init();
788 #endif
789 	/* main_loop() can return to retry autoboot, if so just run it again */
790 	for (;;)
791 		main_loop();
792 	return 0;
793 }
794 
795 /*
796  * Over time we hope to remove these functions with code fragments and
797  * stub funtcions, and instead call the relevant function directly.
798  *
799  * We also hope to remove most of the driver-related init and do it if/when
800  * the driver is later used.
801  *
802  * TODO: perhaps reset the watchdog in the initcall function after each call?
803  */
804 static init_fnc_t init_sequence_r[] = {
805 	initr_trace,
806 	initr_reloc,
807 	/* TODO: could x86/PPC have this also perhaps? */
808 #ifdef CONFIG_ARM
809 	initr_caches,
810 	/* Note: For Freescale LS2 SoCs, new MMU table is created in DDR.
811 	 *	 A temporary mapping of IFC high region is since removed,
812 	 *	 so environmental variables in NOR flash is not availble
813 	 *	 until board_init() is called below to remap IFC to high
814 	 *	 region.
815 	 */
816 #endif
817 	initr_reloc_global_data,
818 
819 	/*
820 	 * Some platform requires to reserve memory regions for some firmware
821 	 * to avoid kernel touches it, but U-Boot may have communication with
822 	 * firmware by share memory. So that we had better reserve firmware
823 	 * region after the initr_caches() which enables MMU and init
824 	 * translation table, we need firmware region to be mapped as cacheable
825 	 * like other regions, otherwise there would be dcache coherence issue
826 	 * between firmware and U-Boot.
827 	 */
828 	board_initr_caches_fixup,
829 
830 #if defined(CONFIG_SYS_INIT_RAM_LOCK) && defined(CONFIG_E500)
831 	initr_unlock_ram_in_cache,
832 #endif
833 	initr_barrier,
834 	initr_malloc,
835 #ifdef CONFIG_BIDRAM
836 	bidram_initr,
837 #endif
838 #ifdef CONFIG_SYSMEM
839 	sysmem_initr,
840 #endif
841 	log_init,
842 	initr_bootstage,	/* Needs malloc() but has its own timer */
843 	initr_console_record,
844 #ifdef CONFIG_SYS_NONCACHED_MEMORY
845 	initr_noncached,
846 #endif
847 	bootstage_relocate,
848 
849 	interrupt_init,
850 #ifdef CONFIG_ARM
851 	initr_enable_interrupts,
852 #endif
853 	interrupt_debugger_init,
854 
855 #ifdef CONFIG_OF_LIVE
856 	initr_of_live,
857 #endif
858 #ifdef CONFIG_DM
859 	initr_dm,
860 #endif
861 
862 #ifdef CONFIG_USING_KERNEL_DTB
863 	initr_env_nowhere,
864 #endif
865 #if defined(CONFIG_BOARD_EARLY_INIT_R)
866 	board_early_init_r,
867 #endif
868 
869 #if defined(CONFIG_ARM) || defined(CONFIG_NDS32) || defined(CONFIG_RISCV)
870 	board_init,	/* Setup chipselects */
871 #endif
872 
873 #if defined(CONFIG_USING_KERNEL_DTB) && !defined(CONFIG_ENV_IS_NOWHERE)
874 	initr_env_switch,
875 #endif
876 
877 	/*
878 	 * TODO: printing of the clock inforamtion of the board is now
879 	 * implemented as part of bdinfo command. Currently only support for
880 	 * davinci SOC's is added. Remove this check once all the board
881 	 * implement this.
882 	 */
883 #ifdef CONFIG_CLOCKS
884 	set_cpu_clk_info, /* Setup clock information */
885 #endif
886 #ifdef CONFIG_EFI_LOADER
887 	efi_memory_init,
888 #endif
889 	stdio_init_tables,
890 	initr_serial,
891 	initr_announce,
892 	INIT_FUNC_WATCHDOG_RESET
893 #ifdef CONFIG_NEEDS_MANUAL_RELOC
894 	initr_manual_reloc_cmdtable,
895 #endif
896 #if defined(CONFIG_PPC) || defined(CONFIG_M68K) || defined(CONFIG_MIPS)
897 	initr_trap,
898 #endif
899 #ifdef CONFIG_ADDR_MAP
900 	initr_addr_map,
901 #endif
902 	INIT_FUNC_WATCHDOG_RESET
903 #ifdef CONFIG_POST
904 	initr_post_backlog,
905 #endif
906 	INIT_FUNC_WATCHDOG_RESET
907 #if defined(CONFIG_PCI) && defined(CONFIG_SYS_EARLY_PCI_INIT)
908 	/*
909 	 * Do early PCI configuration _before_ the flash gets initialised,
910 	 * because PCU ressources are crucial for flash access on some boards.
911 	 */
912 	initr_pci,
913 #endif
914 #ifdef CONFIG_ARCH_EARLY_INIT_R
915 	arch_early_init_r,
916 #endif
917 	power_init_board,
918 #ifdef CONFIG_MTD_NOR_FLASH
919 	initr_flash,
920 #endif
921 	INIT_FUNC_WATCHDOG_RESET
922 #if defined(CONFIG_PPC) || defined(CONFIG_M68K) || defined(CONFIG_X86)
923 	/* initialize higher level parts of CPU like time base and timers */
924 	cpu_init_r,
925 #endif
926 #ifdef CONFIG_PPC
927 	initr_spi,
928 #endif
929 #ifdef CONFIG_CMD_NAND
930 	initr_nand,
931 #endif
932 #ifdef CONFIG_CMD_ONENAND
933 	initr_onenand,
934 #endif
935 #ifdef CONFIG_MTD_BLK
936 	initr_mtd_blk,
937 #endif
938 #ifdef CONFIG_MMC
939 	initr_mmc,
940 #endif
941 #ifndef CONFIG_USING_KERNEL_DTB
942 	initr_env,
943 #endif
944 #ifdef CONFIG_SYS_BOOTPARAMS_LEN
945 	initr_malloc_bootparams,
946 #endif
947 	INIT_FUNC_WATCHDOG_RESET
948 	initr_secondary_cpu,
949 #if defined(CONFIG_ID_EEPROM) || defined(CONFIG_SYS_I2C_MAC_OFFSET)
950 	mac_read_from_eeprom,
951 #endif
952 	INIT_FUNC_WATCHDOG_RESET
953 #if defined(CONFIG_PCI) && !defined(CONFIG_SYS_EARLY_PCI_INIT)
954 	/*
955 	 * Do pci configuration
956 	 */
957 	initr_pci,
958 #endif
959 	stdio_add_devices,
960 	initr_jumptable,
961 #ifdef CONFIG_API
962 	initr_api,
963 #endif
964 	console_init_r,		/* fully init console as a device */
965 #ifdef CONFIG_DISPLAY_BOARDINFO_LATE
966 	console_announce_r,
967 	show_board_info,
968 #endif
969 #ifdef CONFIG_ARCH_MISC_INIT
970 	arch_misc_init,		/* miscellaneous arch-dependent init */
971 #endif
972 #ifdef CONFIG_MISC_INIT_R
973 	misc_init_r,		/* miscellaneous platform-dependent init */
974 #endif
975 	INIT_FUNC_WATCHDOG_RESET
976 #ifdef CONFIG_CMD_KGDB
977 	initr_kgdb,
978 #endif
979 
980 #if defined(CONFIG_MICROBLAZE) || defined(CONFIG_M68K)
981 	timer_init,		/* initialize timer */
982 #endif
983 #if defined(CONFIG_LED_STATUS)
984 	initr_status_led,
985 #endif
986 	/* PPC has a udelay(20) here dating from 2002. Why? */
987 #ifdef CONFIG_CMD_NET
988 	initr_ethaddr,
989 #endif
990 #ifdef CONFIG_BOARD_LATE_INIT
991 	board_late_init,
992 #endif
993 #if defined(CONFIG_SCSI) && !defined(CONFIG_DM_SCSI)
994 	INIT_FUNC_WATCHDOG_RESET
995 	initr_scsi,
996 #endif
997 #ifdef CONFIG_BITBANGMII
998 	initr_bbmii,
999 #endif
1000 #ifdef CONFIG_CMD_NET
1001 	INIT_FUNC_WATCHDOG_RESET
1002 	initr_net,
1003 #endif
1004 #ifdef CONFIG_POST
1005 	initr_post,
1006 #endif
1007 #if defined(CONFIG_CMD_PCMCIA) && !defined(CONFIG_IDE)
1008 	initr_pcmcia,
1009 #endif
1010 #if defined(CONFIG_IDE)
1011 	initr_ide,
1012 #endif
1013 #ifdef CONFIG_LAST_STAGE_INIT
1014 	INIT_FUNC_WATCHDOG_RESET
1015 	/*
1016 	 * Some parts can be only initialized if all others (like
1017 	 * Interrupts) are up and running (i.e. the PC-style ISA
1018 	 * keyboard).
1019 	 */
1020 	last_stage_init,
1021 #endif
1022 #ifdef CONFIG_CMD_BEDBUG
1023 	INIT_FUNC_WATCHDOG_RESET
1024 	initr_bedbug,
1025 #endif
1026 #if defined(CONFIG_PRAM)
1027 	initr_mem,
1028 #endif
1029 #ifdef CONFIG_PS2KBD
1030 	initr_kbd,
1031 #endif
1032 	run_main_loop,
1033 };
1034 
1035 void board_init_r(gd_t *new_gd, ulong dest_addr)
1036 {
1037 	/*
1038 	 * Set up the new global data pointer. So far only x86 does this
1039 	 * here.
1040 	 * TODO(sjg@chromium.org): Consider doing this for all archs, or
1041 	 * dropping the new_gd parameter.
1042 	 */
1043 #if CONFIG_IS_ENABLED(X86_64)
1044 	arch_setup_gd(new_gd);
1045 #endif
1046 
1047 #ifdef CONFIG_NEEDS_MANUAL_RELOC
1048 	int i;
1049 #endif
1050 
1051 #if !defined(CONFIG_X86) && !defined(CONFIG_ARM) && !defined(CONFIG_ARM64)
1052 	gd = new_gd;
1053 #endif
1054 	gd->flags &= ~GD_FLG_LOG_READY;
1055 
1056 #ifdef CONFIG_NEEDS_MANUAL_RELOC
1057 	for (i = 0; i < ARRAY_SIZE(init_sequence_r); i++)
1058 		init_sequence_r[i] += gd->reloc_off;
1059 #endif
1060 
1061 	if (initcall_run_list(init_sequence_r))
1062 		hang();
1063 
1064 	/* NOTREACHED - run_main_loop() does not return */
1065 	hang();
1066 }
1067