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