xref: /rk3399_rockchip-uboot/common/board_r.c (revision d8db5367cf2573bd2878866cf70446102183652c)
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 	return 0;
554 #else
555 	const char env_minimum[] = {
556 		ENV_MEM_LAYOUT_SETTINGS
557 #ifdef ENV_MEM_LAYOUT_SETTINGS1
558 		ENV_MEM_LAYOUT_SETTINGS1
559 #endif
560 #ifdef RKIMG_DET_BOOTDEV
561 		RKIMG_DET_BOOTDEV
562 #endif
563 	};
564 
565 	return set_board_env((char *)env_minimum, ENV_SIZE, 0, true);
566 #endif
567 }
568 
569 #if !defined(CONFIG_ENV_IS_NOWHERE)
570 /*
571  * storage has been initialized in board_init(), we could switch env
572  * from nowhere to storage, i.e. CONFIG_ENV_IS_IN_xxx=y.
573  */
574 static int initr_env_switch(void)
575 {
576 	ALLOC_CACHE_ALIGN_BUFFER(env_t, env_nowhere, 1);
577 	char *data;
578 	int ret;
579 
580 	data = env_get("bootargs");
581 	if (data) {
582 		env_set("bootargs_tmp", data);
583 		env_set("bootargs", NULL);
584 	}
585 
586 	/* Export nowhere env for late use */
587 	ret = env_export(env_nowhere);
588 	if (ret) {
589 		printf("%s: export nowhere env fail, ret=%d\n", __func__, ret);
590 		return -EINVAL;
591 	}
592 
593 	/* Destroy nowhere env and import storage env */
594 	initr_env();
595 
596 	/* Append/override nowhere env to storage env */
597 	set_board_env((char *)env_nowhere->data, ENV_SIZE, H_NOCLEAR, false);
598 
599 	/*
600 	 * Restore nowhere bootargs to append/override the one in env storage.
601 	 *
602 	 * Without this, the entire "bootargs" in storage env is replaces by
603 	 * the one in env_nowhere->data.
604 	 */
605 	data = env_get("bootargs_tmp");
606 	if (data) {
607 		env_update("bootargs", data);
608 		env_set("bootargs_tmp", NULL);
609 	}
610 
611 	return 0;
612 }
613 #endif	/* CONFIG_ENV_IS_NOWHERE */
614 #endif	/* CONFIG_USING_KERNEL_DTB */
615 
616 #ifdef CONFIG_SYS_BOOTPARAMS_LEN
617 static int initr_malloc_bootparams(void)
618 {
619 	gd->bd->bi_boot_params = (ulong)malloc(CONFIG_SYS_BOOTPARAMS_LEN);
620 	if (!gd->bd->bi_boot_params) {
621 		puts("WARNING: Cannot allocate space for boot parameters\n");
622 		return -ENOMEM;
623 	}
624 	return 0;
625 }
626 #endif
627 
628 static int initr_jumptable(void)
629 {
630 	jumptable_init();
631 	return 0;
632 }
633 
634 #if defined(CONFIG_API)
635 static int initr_api(void)
636 {
637 	/* Initialize API */
638 	api_init();
639 	return 0;
640 }
641 #endif
642 
643 /* enable exceptions */
644 #ifdef CONFIG_ARM
645 static int initr_enable_interrupts(void)
646 {
647 	enable_interrupts();
648 	return 0;
649 }
650 #endif
651 
652 #ifdef CONFIG_CMD_NET
653 static int initr_ethaddr(void)
654 {
655 	bd_t *bd = gd->bd;
656 
657 	/* kept around for legacy kernels only ... ignore the next section */
658 	eth_env_get_enetaddr("ethaddr", bd->bi_enetaddr);
659 #ifdef CONFIG_HAS_ETH1
660 	eth_env_get_enetaddr("eth1addr", bd->bi_enet1addr);
661 #endif
662 #ifdef CONFIG_HAS_ETH2
663 	eth_env_get_enetaddr("eth2addr", bd->bi_enet2addr);
664 #endif
665 #ifdef CONFIG_HAS_ETH3
666 	eth_env_get_enetaddr("eth3addr", bd->bi_enet3addr);
667 #endif
668 #ifdef CONFIG_HAS_ETH4
669 	eth_env_get_enetaddr("eth4addr", bd->bi_enet4addr);
670 #endif
671 #ifdef CONFIG_HAS_ETH5
672 	eth_env_get_enetaddr("eth5addr", bd->bi_enet5addr);
673 #endif
674 	return 0;
675 }
676 #endif /* CONFIG_CMD_NET */
677 
678 #ifdef CONFIG_CMD_KGDB
679 static int initr_kgdb(void)
680 {
681 	puts("KGDB:  ");
682 	kgdb_init();
683 	return 0;
684 }
685 #endif
686 
687 #if defined(CONFIG_LED_STATUS)
688 static int initr_status_led(void)
689 {
690 #if defined(CONFIG_LED_STATUS_BOOT)
691 	status_led_set(CONFIG_LED_STATUS_BOOT, CONFIG_LED_STATUS_BLINKING);
692 #else
693 	status_led_init();
694 #endif
695 	return 0;
696 }
697 #endif
698 
699 #if defined(CONFIG_SCSI) && !defined(CONFIG_DM_SCSI)
700 static int initr_scsi(void)
701 {
702 	puts("SCSI:  ");
703 	scsi_init();
704 
705 	return 0;
706 }
707 #endif
708 
709 #ifdef CONFIG_BITBANGMII
710 static int initr_bbmii(void)
711 {
712 	bb_miiphy_init();
713 	return 0;
714 }
715 #endif
716 
717 #ifdef CONFIG_CMD_NET
718 static int initr_net(void)
719 {
720 	puts("Net:   ");
721 	eth_initialize();
722 #if defined(CONFIG_RESET_PHY_R)
723 	debug("Reset Ethernet PHY\n");
724 	reset_phy();
725 #endif
726 	return 0;
727 }
728 #endif
729 
730 #ifdef CONFIG_POST
731 static int initr_post(void)
732 {
733 	post_run(NULL, POST_RAM | post_bootmode_get(0));
734 	return 0;
735 }
736 #endif
737 
738 #if defined(CONFIG_CMD_PCMCIA) && !defined(CONFIG_IDE)
739 static int initr_pcmcia(void)
740 {
741 	puts("PCMCIA:");
742 	pcmcia_init();
743 	return 0;
744 }
745 #endif
746 
747 #if defined(CONFIG_IDE)
748 static int initr_ide(void)
749 {
750 	puts("IDE:   ");
751 #if defined(CONFIG_START_IDE)
752 	if (board_start_ide())
753 		ide_init();
754 #else
755 	ide_init();
756 #endif
757 	return 0;
758 }
759 #endif
760 
761 #if defined(CONFIG_PRAM)
762 /*
763  * Export available size of memory for Linux, taking into account the
764  * protected RAM at top of memory
765  */
766 int initr_mem(void)
767 {
768 	ulong pram = 0;
769 	char memsz[32];
770 
771 # ifdef CONFIG_PRAM
772 	pram = env_get_ulong("pram", 10, CONFIG_PRAM);
773 # endif
774 	sprintf(memsz, "%ldk", (long int) ((gd->ram_size / 1024) - pram));
775 	env_set("mem", memsz);
776 
777 	return 0;
778 }
779 #endif
780 
781 #ifdef CONFIG_CMD_BEDBUG
782 static int initr_bedbug(void)
783 {
784 	bedbug_init();
785 
786 	return 0;
787 }
788 #endif
789 
790 #ifdef CONFIG_PS2KBD
791 static int initr_kbd(void)
792 {
793 	puts("PS/2:  ");
794 	kbd_init();
795 	return 0;
796 }
797 #endif
798 
799 __weak int interrupt_debugger_init(void)
800 {
801 	return 0;
802 }
803 
804 __weak int board_initr_caches_fixup(void)
805 {
806 	return 0;
807 }
808 
809 static int run_main_loop(void)
810 {
811 #ifdef CONFIG_SANDBOX
812 	sandbox_main_loop_init();
813 #endif
814 	/* main_loop() can return to retry autoboot, if so just run it again */
815 	for (;;)
816 		main_loop();
817 	return 0;
818 }
819 
820 /*
821  * Over time we hope to remove these functions with code fragments and
822  * stub funtcions, and instead call the relevant function directly.
823  *
824  * We also hope to remove most of the driver-related init and do it if/when
825  * the driver is later used.
826  *
827  * TODO: perhaps reset the watchdog in the initcall function after each call?
828  */
829 static init_fnc_t init_sequence_r[] = {
830 	initr_trace,
831 	initr_reloc,
832 	/* TODO: could x86/PPC have this also perhaps? */
833 #ifdef CONFIG_ARM
834 	initr_caches,
835 	/* Note: For Freescale LS2 SoCs, new MMU table is created in DDR.
836 	 *	 A temporary mapping of IFC high region is since removed,
837 	 *	 so environmental variables in NOR flash is not availble
838 	 *	 until board_init() is called below to remap IFC to high
839 	 *	 region.
840 	 */
841 #endif
842 	initr_reloc_global_data,
843 
844 	/*
845 	 * Some platform requires to reserve memory regions for some firmware
846 	 * to avoid kernel touches it, but U-Boot may have communication with
847 	 * firmware by share memory. So that we had better reserve firmware
848 	 * region after the initr_caches() which enables MMU and init
849 	 * translation table, we need firmware region to be mapped as cacheable
850 	 * like other regions, otherwise there would be dcache coherence issue
851 	 * between firmware and U-Boot.
852 	 */
853 	board_initr_caches_fixup,
854 
855 #if defined(CONFIG_SYS_INIT_RAM_LOCK) && defined(CONFIG_E500)
856 	initr_unlock_ram_in_cache,
857 #endif
858 	initr_barrier,
859 	initr_malloc,
860 #ifdef CONFIG_BIDRAM
861 	bidram_initr,
862 #endif
863 #ifdef CONFIG_SYSMEM
864 	sysmem_initr,
865 #endif
866 	log_init,
867 	initr_bootstage,	/* Needs malloc() but has its own timer */
868 	initr_console_record,
869 #ifdef CONFIG_SYS_NONCACHED_MEMORY
870 	initr_noncached,
871 #endif
872 	bootstage_relocate,
873 
874 	interrupt_init,
875 #ifdef CONFIG_ARM
876 	initr_enable_interrupts,
877 #endif
878 	interrupt_debugger_init,
879 
880 #ifdef CONFIG_OF_LIVE
881 	initr_of_live,
882 #endif
883 #ifdef CONFIG_DM
884 	initr_dm,
885 #endif
886 
887 #ifdef CONFIG_USING_KERNEL_DTB
888 	initr_env_nowhere,
889 #endif
890 #if defined(CONFIG_BOARD_EARLY_INIT_R)
891 	board_early_init_r,
892 #endif
893 
894 #if defined(CONFIG_ARM) || defined(CONFIG_NDS32) || defined(CONFIG_RISCV)
895 	board_init,	/* Setup chipselects */
896 #endif
897 #if defined(CONFIG_USING_KERNEL_DTB) && !defined(CONFIG_ENV_IS_NOWHERE)
898 	initr_env_switch,
899 #elif defined(CONFIG_ENVF)
900 	env_load,
901 #endif
902 
903 	/*
904 	 * TODO: printing of the clock inforamtion of the board is now
905 	 * implemented as part of bdinfo command. Currently only support for
906 	 * davinci SOC's is added. Remove this check once all the board
907 	 * implement this.
908 	 */
909 #ifdef CONFIG_CLOCKS
910 	set_cpu_clk_info, /* Setup clock information */
911 #endif
912 #ifdef CONFIG_EFI_LOADER
913 	efi_memory_init,
914 #endif
915 	stdio_init_tables,
916 	initr_serial,
917 	initr_announce,
918 	INIT_FUNC_WATCHDOG_RESET
919 #ifdef CONFIG_NEEDS_MANUAL_RELOC
920 	initr_manual_reloc_cmdtable,
921 #endif
922 #if defined(CONFIG_PPC) || defined(CONFIG_M68K) || defined(CONFIG_MIPS)
923 	initr_trap,
924 #endif
925 #ifdef CONFIG_ADDR_MAP
926 	initr_addr_map,
927 #endif
928 	INIT_FUNC_WATCHDOG_RESET
929 #ifdef CONFIG_POST
930 	initr_post_backlog,
931 #endif
932 	INIT_FUNC_WATCHDOG_RESET
933 #if defined(CONFIG_PCI) && defined(CONFIG_SYS_EARLY_PCI_INIT)
934 	/*
935 	 * Do early PCI configuration _before_ the flash gets initialised,
936 	 * because PCU ressources are crucial for flash access on some boards.
937 	 */
938 	initr_pci,
939 #endif
940 #ifdef CONFIG_ARCH_EARLY_INIT_R
941 	arch_early_init_r,
942 #endif
943 	power_init_board,
944 #ifdef CONFIG_MTD_NOR_FLASH
945 	initr_flash,
946 #endif
947 	INIT_FUNC_WATCHDOG_RESET
948 #if defined(CONFIG_PPC) || defined(CONFIG_M68K) || defined(CONFIG_X86)
949 	/* initialize higher level parts of CPU like time base and timers */
950 	cpu_init_r,
951 #endif
952 #ifdef CONFIG_PPC
953 	initr_spi,
954 #endif
955 #ifdef CONFIG_CMD_NAND
956 	initr_nand,
957 #endif
958 #ifdef CONFIG_CMD_ONENAND
959 	initr_onenand,
960 #endif
961 #ifdef CONFIG_MTD_BLK
962 	initr_mtd_blk,
963 #endif
964 #ifdef CONFIG_MMC
965 	initr_mmc,
966 #endif
967 #ifndef CONFIG_USING_KERNEL_DTB
968 	initr_env,
969 #endif
970 #ifdef CONFIG_SYS_BOOTPARAMS_LEN
971 	initr_malloc_bootparams,
972 #endif
973 	INIT_FUNC_WATCHDOG_RESET
974 	initr_secondary_cpu,
975 #if defined(CONFIG_ID_EEPROM) || defined(CONFIG_SYS_I2C_MAC_OFFSET)
976 	mac_read_from_eeprom,
977 #endif
978 	INIT_FUNC_WATCHDOG_RESET
979 #if defined(CONFIG_PCI) && !defined(CONFIG_SYS_EARLY_PCI_INIT)
980 	/*
981 	 * Do pci configuration
982 	 */
983 	initr_pci,
984 #endif
985 	stdio_add_devices,
986 	initr_jumptable,
987 #ifdef CONFIG_API
988 	initr_api,
989 #endif
990 	console_init_r,		/* fully init console as a device */
991 #ifdef CONFIG_DISPLAY_BOARDINFO_LATE
992 	console_announce_r,
993 	show_board_info,
994 #endif
995 #ifdef CONFIG_ARCH_MISC_INIT
996 	arch_misc_init,		/* miscellaneous arch-dependent init */
997 #endif
998 #ifdef CONFIG_MISC_INIT_R
999 	misc_init_r,		/* miscellaneous platform-dependent init */
1000 #endif
1001 	INIT_FUNC_WATCHDOG_RESET
1002 #ifdef CONFIG_CMD_KGDB
1003 	initr_kgdb,
1004 #endif
1005 
1006 #if defined(CONFIG_MICROBLAZE) || defined(CONFIG_M68K)
1007 	timer_init,		/* initialize timer */
1008 #endif
1009 #if defined(CONFIG_LED_STATUS)
1010 	initr_status_led,
1011 #endif
1012 	/* PPC has a udelay(20) here dating from 2002. Why? */
1013 #ifdef CONFIG_CMD_NET
1014 	initr_ethaddr,
1015 #endif
1016 #ifdef CONFIG_BOARD_LATE_INIT
1017 	board_late_init,
1018 #endif
1019 #if defined(CONFIG_SCSI) && !defined(CONFIG_DM_SCSI)
1020 	INIT_FUNC_WATCHDOG_RESET
1021 	initr_scsi,
1022 #endif
1023 #ifdef CONFIG_BITBANGMII
1024 	initr_bbmii,
1025 #endif
1026 #ifdef CONFIG_CMD_NET
1027 	INIT_FUNC_WATCHDOG_RESET
1028 	initr_net,
1029 #endif
1030 #ifdef CONFIG_POST
1031 	initr_post,
1032 #endif
1033 #if defined(CONFIG_CMD_PCMCIA) && !defined(CONFIG_IDE)
1034 	initr_pcmcia,
1035 #endif
1036 #if defined(CONFIG_IDE)
1037 	initr_ide,
1038 #endif
1039 #ifdef CONFIG_LAST_STAGE_INIT
1040 	INIT_FUNC_WATCHDOG_RESET
1041 	/*
1042 	 * Some parts can be only initialized if all others (like
1043 	 * Interrupts) are up and running (i.e. the PC-style ISA
1044 	 * keyboard).
1045 	 */
1046 	last_stage_init,
1047 #endif
1048 #ifdef CONFIG_CMD_BEDBUG
1049 	INIT_FUNC_WATCHDOG_RESET
1050 	initr_bedbug,
1051 #endif
1052 #if defined(CONFIG_PRAM)
1053 	initr_mem,
1054 #endif
1055 #ifdef CONFIG_PS2KBD
1056 	initr_kbd,
1057 #endif
1058 	run_main_loop,
1059 };
1060 
1061 void board_init_r(gd_t *new_gd, ulong dest_addr)
1062 {
1063 	/*
1064 	 * Set up the new global data pointer. So far only x86 does this
1065 	 * here.
1066 	 * TODO(sjg@chromium.org): Consider doing this for all archs, or
1067 	 * dropping the new_gd parameter.
1068 	 */
1069 #if CONFIG_IS_ENABLED(X86_64)
1070 	arch_setup_gd(new_gd);
1071 #endif
1072 
1073 #ifdef CONFIG_NEEDS_MANUAL_RELOC
1074 	int i;
1075 #endif
1076 
1077 #if !defined(CONFIG_X86) && !defined(CONFIG_ARM) && !defined(CONFIG_ARM64)
1078 	gd = new_gd;
1079 #endif
1080 	gd->flags &= ~GD_FLG_LOG_READY;
1081 
1082 #ifdef CONFIG_NEEDS_MANUAL_RELOC
1083 	for (i = 0; i < ARRAY_SIZE(init_sequence_r); i++)
1084 		init_sequence_r[i] += gd->reloc_off;
1085 #endif
1086 
1087 	if (initcall_run_list(init_sequence_r))
1088 		hang();
1089 
1090 	/* NOTREACHED - run_main_loop() does not return */
1091 	hang();
1092 }
1093