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