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