xref: /rk3399_rockchip-uboot/arch/nds32/lib/bootm.c (revision b841b6e94662b3b21a56d6ecaab64dcdfb0d311c)
1463d47f6SMacpaul Lin /*
2463d47f6SMacpaul Lin  * Copyright (C) 2011 Andes Technology Corporation
3463d47f6SMacpaul Lin  * Shawn Lin, Andes Technology Corporation <nobuhiro@andestech.com>
4463d47f6SMacpaul Lin  * Macpaul Lin, Andes Technology Corporation <macpaul@andestech.com>
5463d47f6SMacpaul Lin  *
61a459660SWolfgang Denk  * SPDX-License-Identifier:	GPL-2.0+
7463d47f6SMacpaul Lin  */
8463d47f6SMacpaul Lin 
9463d47f6SMacpaul Lin #include <common.h>
10463d47f6SMacpaul Lin #include <command.h>
11463d47f6SMacpaul Lin #include <image.h>
12463d47f6SMacpaul Lin #include <u-boot/zlib.h>
13463d47f6SMacpaul Lin #include <asm/byteorder.h>
14*b841b6e9Srick #include <asm/bootm.h>
15463d47f6SMacpaul Lin 
16463d47f6SMacpaul Lin DECLARE_GLOBAL_DATA_PTR;
17463d47f6SMacpaul Lin 
1886132af7Srick int arch_fixup_fdt(void *blob)
1986132af7Srick {
2086132af7Srick 	return 0;
2186132af7Srick }
2286132af7Srick 
2386132af7Srick 
24463d47f6SMacpaul Lin #if defined(CONFIG_SETUP_MEMORY_TAGS) || \
25463d47f6SMacpaul Lin 	defined(CONFIG_CMDLINE_TAG) || \
26463d47f6SMacpaul Lin 	defined(CONFIG_INITRD_TAG) || \
27463d47f6SMacpaul Lin 	defined(CONFIG_SERIAL_TAG) || \
28463d47f6SMacpaul Lin 	defined(CONFIG_REVISION_TAG)
29463d47f6SMacpaul Lin static void setup_start_tag(bd_t *bd);
30463d47f6SMacpaul Lin 
31463d47f6SMacpaul Lin # ifdef CONFIG_SETUP_MEMORY_TAGS
32463d47f6SMacpaul Lin static void setup_memory_tags(bd_t *bd);
33463d47f6SMacpaul Lin # endif
34463d47f6SMacpaul Lin static void setup_commandline_tag(bd_t *bd, char *commandline);
35463d47f6SMacpaul Lin 
36463d47f6SMacpaul Lin # ifdef CONFIG_INITRD_TAG
37463d47f6SMacpaul Lin static void setup_initrd_tag(bd_t *bd, ulong initrd_start, ulong initrd_end);
38463d47f6SMacpaul Lin # endif
39463d47f6SMacpaul Lin static void setup_end_tag(bd_t *bd);
40463d47f6SMacpaul Lin 
41463d47f6SMacpaul Lin static struct tag *params;
42463d47f6SMacpaul Lin #endif /* CONFIG_SETUP_MEMORY_TAGS || CONFIG_CMDLINE_TAG || CONFIG_INITRD_TAG */
43463d47f6SMacpaul Lin 
44463d47f6SMacpaul Lin int do_bootm_linux(int flag, int argc, char *argv[], bootm_headers_t *images)
45463d47f6SMacpaul Lin {
46463d47f6SMacpaul Lin 	bd_t	*bd = gd->bd;
47463d47f6SMacpaul Lin 	char	*s;
48463d47f6SMacpaul Lin 	int	machid = bd->bi_arch_number;
49463d47f6SMacpaul Lin 	void	(*theKernel)(int zero, int arch, uint params);
50463d47f6SMacpaul Lin 
51463d47f6SMacpaul Lin #ifdef CONFIG_CMDLINE_TAG
52463d47f6SMacpaul Lin 	char *commandline = getenv("bootargs");
53463d47f6SMacpaul Lin #endif
54463d47f6SMacpaul Lin 
552cb0e55aSAndreas Bießmann 	/*
562cb0e55aSAndreas Bießmann 	 * allow the PREP bootm subcommand, it is required for bootm to work
572cb0e55aSAndreas Bießmann 	 */
582cb0e55aSAndreas Bießmann 	if (flag & BOOTM_STATE_OS_PREP)
592cb0e55aSAndreas Bießmann 		return 0;
602cb0e55aSAndreas Bießmann 
61463d47f6SMacpaul Lin 	if ((flag != 0) && (flag != BOOTM_STATE_OS_GO))
62463d47f6SMacpaul Lin 		return 1;
63463d47f6SMacpaul Lin 
64463d47f6SMacpaul Lin 	theKernel = (void (*)(int, int, uint))images->ep;
65463d47f6SMacpaul Lin 
66463d47f6SMacpaul Lin 	s = getenv("machid");
67463d47f6SMacpaul Lin 	if (s) {
68463d47f6SMacpaul Lin 		machid = simple_strtoul(s, NULL, 16);
69463d47f6SMacpaul Lin 		printf("Using machid 0x%x from environment\n", machid);
70463d47f6SMacpaul Lin 	}
71463d47f6SMacpaul Lin 
72770605e4SSimon Glass 	bootstage_mark(BOOTSTAGE_ID_RUN_OS);
73463d47f6SMacpaul Lin 
74463d47f6SMacpaul Lin 	debug("## Transferring control to Linux (at address %08lx) ...\n",
75463d47f6SMacpaul Lin 	       (ulong)theKernel);
76463d47f6SMacpaul Lin 
77*b841b6e9Srick 	if (IMAGE_ENABLE_OF_LIBFDT && images->ft_len) {
78*b841b6e9Srick #ifdef CONFIG_OF_LIBFDT
79*b841b6e9Srick 		debug("using: FDT\n");
80*b841b6e9Srick 		if (image_setup_linux(images)) {
81*b841b6e9Srick 			printf("FDT creation failed! hanging...");
82*b841b6e9Srick 			hang();
83*b841b6e9Srick 		}
84*b841b6e9Srick #endif
85*b841b6e9Srick 	} else if (BOOTM_ENABLE_TAGS) {
86463d47f6SMacpaul Lin #if defined(CONFIG_SETUP_MEMORY_TAGS) || \
87463d47f6SMacpaul Lin 	defined(CONFIG_CMDLINE_TAG) || \
88463d47f6SMacpaul Lin 	defined(CONFIG_INITRD_TAG) || \
89463d47f6SMacpaul Lin 	defined(CONFIG_SERIAL_TAG) || \
90463d47f6SMacpaul Lin 	defined(CONFIG_REVISION_TAG)
91463d47f6SMacpaul Lin 	setup_start_tag(bd);
92463d47f6SMacpaul Lin #ifdef CONFIG_SERIAL_TAG
93463d47f6SMacpaul Lin 	setup_serial_tag(&params);
94463d47f6SMacpaul Lin #endif
95463d47f6SMacpaul Lin #ifdef CONFIG_REVISION_TAG
96463d47f6SMacpaul Lin 	setup_revision_tag(&params);
97463d47f6SMacpaul Lin #endif
98463d47f6SMacpaul Lin #ifdef CONFIG_SETUP_MEMORY_TAGS
99463d47f6SMacpaul Lin 	setup_memory_tags(bd);
100463d47f6SMacpaul Lin #endif
101463d47f6SMacpaul Lin #ifdef CONFIG_CMDLINE_TAG
102463d47f6SMacpaul Lin 	setup_commandline_tag(bd, commandline);
103463d47f6SMacpaul Lin #endif
104463d47f6SMacpaul Lin #ifdef CONFIG_INITRD_TAG
105463d47f6SMacpaul Lin 	if (images->rd_start && images->rd_end)
106463d47f6SMacpaul Lin 		setup_initrd_tag(bd, images->rd_start, images->rd_end);
107463d47f6SMacpaul Lin #endif
108463d47f6SMacpaul Lin 	setup_end_tag(bd);
109463d47f6SMacpaul Lin #endif
110463d47f6SMacpaul Lin 
111463d47f6SMacpaul Lin 	/* we assume that the kernel is in place */
112463d47f6SMacpaul Lin 	printf("\nStarting kernel ...\n\n");
113463d47f6SMacpaul Lin 
114463d47f6SMacpaul Lin #ifdef CONFIG_USB_DEVICE
115463d47f6SMacpaul Lin 	{
116463d47f6SMacpaul Lin 		extern void udc_disconnect(void);
117463d47f6SMacpaul Lin 		udc_disconnect();
118463d47f6SMacpaul Lin 	}
119463d47f6SMacpaul Lin #endif
120*b841b6e9Srick 	}
121463d47f6SMacpaul Lin 	cleanup_before_linux();
122*b841b6e9Srick 	if (IMAGE_ENABLE_OF_LIBFDT && images->ft_len)
123*b841b6e9Srick 		theKernel(0, machid, (unsigned long)images->ft_addr);
124*b841b6e9Srick 	else
125463d47f6SMacpaul Lin 		theKernel(0, machid, bd->bi_boot_params);
126463d47f6SMacpaul Lin 	/* does not return */
127463d47f6SMacpaul Lin 
128463d47f6SMacpaul Lin 	return 1;
129463d47f6SMacpaul Lin }
130463d47f6SMacpaul Lin 
131463d47f6SMacpaul Lin #if defined(CONFIG_SETUP_MEMORY_TAGS) || \
132463d47f6SMacpaul Lin 	defined(CONFIG_CMDLINE_TAG) || \
133463d47f6SMacpaul Lin 	defined(CONFIG_INITRD_TAG) || \
134463d47f6SMacpaul Lin 	defined(CONFIG_SERIAL_TAG) || \
135463d47f6SMacpaul Lin 	defined(CONFIG_REVISION_TAG)
136463d47f6SMacpaul Lin static void setup_start_tag(bd_t *bd)
137463d47f6SMacpaul Lin {
138463d47f6SMacpaul Lin 	params = (struct tag *)bd->bi_boot_params;
139463d47f6SMacpaul Lin 
140463d47f6SMacpaul Lin 	params->hdr.tag = ATAG_CORE;
141463d47f6SMacpaul Lin 	params->hdr.size = tag_size(tag_core);
142463d47f6SMacpaul Lin 
143463d47f6SMacpaul Lin 	params->u.core.flags = 0;
144463d47f6SMacpaul Lin 	params->u.core.pagesize = 0;
145463d47f6SMacpaul Lin 	params->u.core.rootdev = 0;
146463d47f6SMacpaul Lin 
147463d47f6SMacpaul Lin 	params = tag_next(params);
148463d47f6SMacpaul Lin }
149463d47f6SMacpaul Lin 
150463d47f6SMacpaul Lin #ifdef CONFIG_SETUP_MEMORY_TAGS
151463d47f6SMacpaul Lin static void setup_memory_tags(bd_t *bd)
152463d47f6SMacpaul Lin {
153463d47f6SMacpaul Lin 	int i;
154463d47f6SMacpaul Lin 
155463d47f6SMacpaul Lin 	for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
156463d47f6SMacpaul Lin 		params->hdr.tag = ATAG_MEM;
157463d47f6SMacpaul Lin 		params->hdr.size = tag_size(tag_mem32);
158463d47f6SMacpaul Lin 
159463d47f6SMacpaul Lin 		params->u.mem.start = bd->bi_dram[i].start;
160463d47f6SMacpaul Lin 		params->u.mem.size = bd->bi_dram[i].size;
161463d47f6SMacpaul Lin 
162463d47f6SMacpaul Lin 		params = tag_next(params);
163463d47f6SMacpaul Lin 	}
164463d47f6SMacpaul Lin }
165463d47f6SMacpaul Lin #endif /* CONFIG_SETUP_MEMORY_TAGS */
166463d47f6SMacpaul Lin 
167463d47f6SMacpaul Lin static void setup_commandline_tag(bd_t *bd, char *commandline)
168463d47f6SMacpaul Lin {
169463d47f6SMacpaul Lin 	char *p;
170463d47f6SMacpaul Lin 
171463d47f6SMacpaul Lin 	if (!commandline)
172463d47f6SMacpaul Lin 		return;
173463d47f6SMacpaul Lin 
174463d47f6SMacpaul Lin 	/* eat leading white space */
175463d47f6SMacpaul Lin 	for (p = commandline; *p == ' '; p++)
176463d47f6SMacpaul Lin 		;
177463d47f6SMacpaul Lin 
178463d47f6SMacpaul Lin 	/* skip non-existent command lines so the kernel will still
179463d47f6SMacpaul Lin 	 * use its default command line.
180463d47f6SMacpaul Lin 	 */
181463d47f6SMacpaul Lin 	if (*p == '\0')
182463d47f6SMacpaul Lin 		return;
183463d47f6SMacpaul Lin 
184463d47f6SMacpaul Lin 	params->hdr.tag = ATAG_CMDLINE;
185463d47f6SMacpaul Lin 	params->hdr.size =
186463d47f6SMacpaul Lin 		(sizeof(struct tag_header) + strlen(p) + 1 + 4) >> 2;
187463d47f6SMacpaul Lin 
188463d47f6SMacpaul Lin 	strcpy(params->u.cmdline.cmdline, p)
189463d47f6SMacpaul Lin 		;
190463d47f6SMacpaul Lin 
191463d47f6SMacpaul Lin 	params = tag_next(params);
192463d47f6SMacpaul Lin }
193463d47f6SMacpaul Lin 
194463d47f6SMacpaul Lin #ifdef CONFIG_INITRD_TAG
195463d47f6SMacpaul Lin static void setup_initrd_tag(bd_t *bd, ulong initrd_start, ulong initrd_end)
196463d47f6SMacpaul Lin {
197463d47f6SMacpaul Lin 	/* an ATAG_INITRD node tells the kernel where the compressed
198463d47f6SMacpaul Lin 	 * ramdisk can be found. ATAG_RDIMG is a better name, actually.
199463d47f6SMacpaul Lin 	 */
200463d47f6SMacpaul Lin 	params->hdr.tag = ATAG_INITRD2;
201463d47f6SMacpaul Lin 	params->hdr.size = tag_size(tag_initrd);
202463d47f6SMacpaul Lin 
203463d47f6SMacpaul Lin 	params->u.initrd.start = initrd_start;
204463d47f6SMacpaul Lin 	params->u.initrd.size = initrd_end - initrd_start;
205463d47f6SMacpaul Lin 
206463d47f6SMacpaul Lin 	params = tag_next(params);
207463d47f6SMacpaul Lin }
208463d47f6SMacpaul Lin #endif /* CONFIG_INITRD_TAG */
209463d47f6SMacpaul Lin 
210463d47f6SMacpaul Lin #ifdef CONFIG_SERIAL_TAG
211463d47f6SMacpaul Lin void setup_serial_tag(struct tag **tmp)
212463d47f6SMacpaul Lin {
213463d47f6SMacpaul Lin 	struct tag *params = *tmp;
214463d47f6SMacpaul Lin 	struct tag_serialnr serialnr;
215463d47f6SMacpaul Lin 	void get_board_serial(struct tag_serialnr *serialnr);
216463d47f6SMacpaul Lin 
217463d47f6SMacpaul Lin 	get_board_serial(&serialnr);
218463d47f6SMacpaul Lin 	params->hdr.tag = ATAG_SERIAL;
219463d47f6SMacpaul Lin 	params->hdr.size = tag_size(tag_serialnr);
220463d47f6SMacpaul Lin 	params->u.serialnr.low = serialnr.low;
221463d47f6SMacpaul Lin 	params->u.serialnr.high = serialnr.high;
222463d47f6SMacpaul Lin 	params = tag_next(params);
223463d47f6SMacpaul Lin 	*tmp = params;
224463d47f6SMacpaul Lin }
225463d47f6SMacpaul Lin #endif
226463d47f6SMacpaul Lin 
227463d47f6SMacpaul Lin #ifdef CONFIG_REVISION_TAG
228463d47f6SMacpaul Lin void setup_revision_tag(struct tag **in_params)
229463d47f6SMacpaul Lin {
230463d47f6SMacpaul Lin 	u32 rev = 0;
231463d47f6SMacpaul Lin 	u32 get_board_rev(void);
232463d47f6SMacpaul Lin 
233463d47f6SMacpaul Lin 	rev = get_board_rev();
234463d47f6SMacpaul Lin 	params->hdr.tag = ATAG_REVISION;
235463d47f6SMacpaul Lin 	params->hdr.size = tag_size(tag_revision);
236463d47f6SMacpaul Lin 	params->u.revision.rev = rev;
237463d47f6SMacpaul Lin 	params = tag_next(params);
238463d47f6SMacpaul Lin }
239463d47f6SMacpaul Lin #endif  /* CONFIG_REVISION_TAG */
240463d47f6SMacpaul Lin 
241463d47f6SMacpaul Lin static void setup_end_tag(bd_t *bd)
242463d47f6SMacpaul Lin {
243463d47f6SMacpaul Lin 	params->hdr.tag = ATAG_NONE;
244463d47f6SMacpaul Lin 	params->hdr.size = 0;
245463d47f6SMacpaul Lin }
246463d47f6SMacpaul Lin 
247463d47f6SMacpaul Lin #endif /* CONFIG_SETUP_MEMORY_TAGS || CONFIG_CMDLINE_TAG || CONFIG_INITRD_TAG */
248