1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2011 Andes Technology Corporation
4  * Shawn Lin, Andes Technology Corporation <nobuhiro@andestech.com>
5  * Macpaul Lin, Andes Technology Corporation <macpaul@andestech.com>
6  * Rick Chen, Andes Technology Corporation <rick@andestech.com>
7  */
8 
9 #include <bootstage.h>
10 #include <bootm.h>
11 #include <command.h>
12 #include <dm.h>
13 #include <fdt_support.h>
14 #include <hang.h>
15 #include <log.h>
16 #include <asm/global_data.h>
17 #include <dm/root.h>
18 #include <image.h>
19 #include <asm/byteorder.h>
20 #include <asm/csr.h>
21 #include <asm/smp.h>
22 #include <dm/device.h>
23 #include <dm/root.h>
24 #include <u-boot/zlib.h>
25 
26 DECLARE_GLOBAL_DATA_PTR;
27 
board_quiesce_devices(void)28 __weak void board_quiesce_devices(void)
29 {
30 }
31 
32 /**
33  * announce_and_cleanup() - Print message and prepare for kernel boot
34  *
35  * @fake: non-zero to do everything except actually boot
36  */
announce_and_cleanup(int fake)37 static void announce_and_cleanup(int fake)
38 {
39 	printf("\nStarting kernel ...%s\n\n", fake ?
40 		"(fake run for tracing)" : "");
41 	bootstage_mark_name(BOOTSTAGE_ID_BOOTM_HANDOFF, "start_kernel");
42 #ifdef CONFIG_BOOTSTAGE_FDT
43 	bootstage_fdt_add_report();
44 #endif
45 #if CONFIG_IS_ENABLED(BOOTSTAGE_REPORT)
46 	bootstage_report();
47 #endif
48 
49 	board_quiesce_devices();
50 
51 	/*
52 	 * Call remove function of all devices with a removal flag set.
53 	 * This may be useful for last-stage operations, like cancelling
54 	 * of DMA operation or releasing device internal buffers.
55 	 */
56 	dm_remove_devices_active();
57 
58 	cleanup_before_linux();
59 }
60 
boot_prep_linux(struct bootm_headers * images)61 static void boot_prep_linux(struct bootm_headers *images)
62 {
63 	if (CONFIG_IS_ENABLED(OF_LIBFDT) && IS_ENABLED(CONFIG_LMB) && images->ft_len) {
64 		debug("using: FDT\n");
65 		if (image_setup_linux(images)) {
66 			printf("FDT creation failed! hanging...");
67 			hang();
68 		}
69 	} else {
70 		printf("Device tree not found or missing FDT support\n");
71 		hang();
72 	}
73 }
74 
boot_jump_linux(struct bootm_headers * images,int flag)75 static void boot_jump_linux(struct bootm_headers *images, int flag)
76 {
77 	void (*kernel)(ulong hart, void *dtb);
78 	int fake = (flag & BOOTM_STATE_OS_FAKE_GO);
79 #ifdef CONFIG_SMP
80 	int ret;
81 #endif
82 
83 	kernel = (void (*)(ulong, void *))images->ep;
84 
85 	bootstage_mark(BOOTSTAGE_ID_RUN_OS);
86 
87 	debug("## Transferring control to kernel (at address %08lx) ...\n",
88 	      (ulong)kernel);
89 
90 	announce_and_cleanup(fake);
91 
92 	if (!fake) {
93 		if (CONFIG_IS_ENABLED(OF_LIBFDT) && images->ft_len) {
94 #ifdef CONFIG_SMP
95 			ret = smp_call_function(images->ep,
96 						(ulong)images->ft_addr, 0, 0);
97 			if (ret)
98 				hang();
99 #endif
100 			kernel(gd->arch.boot_hart, images->ft_addr);
101 		}
102 	}
103 }
104 
do_bootm_linux(int flag,struct bootm_info * bmi)105 int do_bootm_linux(int flag, struct bootm_info *bmi)
106 {
107 	struct bootm_headers *images = bmi->images;
108 
109 	/* No need for those on RISC-V */
110 	if (flag & BOOTM_STATE_OS_BD_T || flag & BOOTM_STATE_OS_CMDLINE)
111 		return -1;
112 
113 	if (flag & BOOTM_STATE_OS_PREP) {
114 		boot_prep_linux(images);
115 		return 0;
116 	}
117 
118 	if (flag & (BOOTM_STATE_OS_GO | BOOTM_STATE_OS_FAKE_GO)) {
119 		boot_jump_linux(images, flag);
120 		return 0;
121 	}
122 
123 	boot_prep_linux(images);
124 	boot_jump_linux(images, flag);
125 	return 0;
126 }
127 
do_bootm_vxworks(int flag,struct bootm_info * bmi)128 int do_bootm_vxworks(int flag, struct bootm_info *bmi)
129 {
130 	return do_bootm_linux(flag, bmi);
131 }
132