xref: /openbmc/u-boot/arch/nds32/lib/bootm.c (revision 4e3349b6)
1 /*
2  * Copyright (C) 2011 Andes Technology Corporation
3  * Shawn Lin, Andes Technology Corporation <nobuhiro@andestech.com>
4  * Macpaul Lin, Andes Technology Corporation <macpaul@andestech.com>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307	 USA
19  *
20  */
21 
22 #include <common.h>
23 #include <command.h>
24 #include <image.h>
25 #include <u-boot/zlib.h>
26 #include <asm/byteorder.h>
27 
28 DECLARE_GLOBAL_DATA_PTR;
29 
30 #if defined(CONFIG_SETUP_MEMORY_TAGS) || \
31 	defined(CONFIG_CMDLINE_TAG) || \
32 	defined(CONFIG_INITRD_TAG) || \
33 	defined(CONFIG_SERIAL_TAG) || \
34 	defined(CONFIG_REVISION_TAG)
35 static void setup_start_tag(bd_t *bd);
36 
37 # ifdef CONFIG_SETUP_MEMORY_TAGS
38 static void setup_memory_tags(bd_t *bd);
39 # endif
40 static void setup_commandline_tag(bd_t *bd, char *commandline);
41 
42 # ifdef CONFIG_INITRD_TAG
43 static void setup_initrd_tag(bd_t *bd, ulong initrd_start, ulong initrd_end);
44 # endif
45 static void setup_end_tag(bd_t *bd);
46 
47 static struct tag *params;
48 #endif /* CONFIG_SETUP_MEMORY_TAGS || CONFIG_CMDLINE_TAG || CONFIG_INITRD_TAG */
49 
50 int do_bootm_linux(int flag, int argc, char *argv[], bootm_headers_t *images)
51 {
52 	bd_t	*bd = gd->bd;
53 	char	*s;
54 	int	machid = bd->bi_arch_number;
55 	void	(*theKernel)(int zero, int arch, uint params);
56 
57 #ifdef CONFIG_CMDLINE_TAG
58 	char *commandline = getenv("bootargs");
59 #endif
60 
61 	if ((flag != 0) && (flag != BOOTM_STATE_OS_GO))
62 		return 1;
63 
64 	theKernel = (void (*)(int, int, uint))images->ep;
65 
66 	s = getenv("machid");
67 	if (s) {
68 		machid = simple_strtoul(s, NULL, 16);
69 		printf("Using machid 0x%x from environment\n", machid);
70 	}
71 
72 	bootstage_mark(BOOTSTAGE_ID_RUN_OS);
73 
74 	debug("## Transferring control to Linux (at address %08lx) ...\n",
75 	       (ulong)theKernel);
76 
77 #if defined(CONFIG_SETUP_MEMORY_TAGS) || \
78 	defined(CONFIG_CMDLINE_TAG) || \
79 	defined(CONFIG_INITRD_TAG) || \
80 	defined(CONFIG_SERIAL_TAG) || \
81 	defined(CONFIG_REVISION_TAG)
82 	setup_start_tag(bd);
83 #ifdef CONFIG_SERIAL_TAG
84 	setup_serial_tag(&params);
85 #endif
86 #ifdef CONFIG_REVISION_TAG
87 	setup_revision_tag(&params);
88 #endif
89 #ifdef CONFIG_SETUP_MEMORY_TAGS
90 	setup_memory_tags(bd);
91 #endif
92 #ifdef CONFIG_CMDLINE_TAG
93 	setup_commandline_tag(bd, commandline);
94 #endif
95 #ifdef CONFIG_INITRD_TAG
96 	if (images->rd_start && images->rd_end)
97 		setup_initrd_tag(bd, images->rd_start, images->rd_end);
98 #endif
99 	setup_end_tag(bd);
100 #endif
101 
102 	/* we assume that the kernel is in place */
103 	printf("\nStarting kernel ...\n\n");
104 
105 #ifdef CONFIG_USB_DEVICE
106 	{
107 		extern void udc_disconnect(void);
108 		udc_disconnect();
109 	}
110 #endif
111 
112 	cleanup_before_linux();
113 
114 	theKernel(0, machid, bd->bi_boot_params);
115 	/* does not return */
116 
117 	return 1;
118 }
119 
120 
121 #if defined(CONFIG_SETUP_MEMORY_TAGS) || \
122 	defined(CONFIG_CMDLINE_TAG) || \
123 	defined(CONFIG_INITRD_TAG) || \
124 	defined(CONFIG_SERIAL_TAG) || \
125 	defined(CONFIG_REVISION_TAG)
126 static void setup_start_tag(bd_t *bd)
127 {
128 	params = (struct tag *)bd->bi_boot_params;
129 
130 	params->hdr.tag = ATAG_CORE;
131 	params->hdr.size = tag_size(tag_core);
132 
133 	params->u.core.flags = 0;
134 	params->u.core.pagesize = 0;
135 	params->u.core.rootdev = 0;
136 
137 	params = tag_next(params);
138 }
139 
140 
141 #ifdef CONFIG_SETUP_MEMORY_TAGS
142 static void setup_memory_tags(bd_t *bd)
143 {
144 	int i;
145 
146 	for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
147 		params->hdr.tag = ATAG_MEM;
148 		params->hdr.size = tag_size(tag_mem32);
149 
150 		params->u.mem.start = bd->bi_dram[i].start;
151 		params->u.mem.size = bd->bi_dram[i].size;
152 
153 		params = tag_next(params);
154 	}
155 }
156 #endif /* CONFIG_SETUP_MEMORY_TAGS */
157 
158 
159 static void setup_commandline_tag(bd_t *bd, char *commandline)
160 {
161 	char *p;
162 
163 	if (!commandline)
164 		return;
165 
166 	/* eat leading white space */
167 	for (p = commandline; *p == ' '; p++)
168 		;
169 
170 	/* skip non-existent command lines so the kernel will still
171 	 * use its default command line.
172 	 */
173 	if (*p == '\0')
174 		return;
175 
176 	params->hdr.tag = ATAG_CMDLINE;
177 	params->hdr.size =
178 		(sizeof(struct tag_header) + strlen(p) + 1 + 4) >> 2;
179 
180 	strcpy(params->u.cmdline.cmdline, p)
181 		;
182 
183 	params = tag_next(params);
184 }
185 
186 
187 #ifdef CONFIG_INITRD_TAG
188 static void setup_initrd_tag(bd_t *bd, ulong initrd_start, ulong initrd_end)
189 {
190 	/* an ATAG_INITRD node tells the kernel where the compressed
191 	 * ramdisk can be found. ATAG_RDIMG is a better name, actually.
192 	 */
193 	params->hdr.tag = ATAG_INITRD2;
194 	params->hdr.size = tag_size(tag_initrd);
195 
196 	params->u.initrd.start = initrd_start;
197 	params->u.initrd.size = initrd_end - initrd_start;
198 
199 	params = tag_next(params);
200 }
201 #endif /* CONFIG_INITRD_TAG */
202 
203 #ifdef CONFIG_SERIAL_TAG
204 void setup_serial_tag(struct tag **tmp)
205 {
206 	struct tag *params = *tmp;
207 	struct tag_serialnr serialnr;
208 	void get_board_serial(struct tag_serialnr *serialnr);
209 
210 	get_board_serial(&serialnr);
211 	params->hdr.tag = ATAG_SERIAL;
212 	params->hdr.size = tag_size(tag_serialnr);
213 	params->u.serialnr.low = serialnr.low;
214 	params->u.serialnr.high = serialnr.high;
215 	params = tag_next(params);
216 	*tmp = params;
217 }
218 #endif
219 
220 #ifdef CONFIG_REVISION_TAG
221 void setup_revision_tag(struct tag **in_params)
222 {
223 	u32 rev = 0;
224 	u32 get_board_rev(void);
225 
226 	rev = get_board_rev();
227 	params->hdr.tag = ATAG_REVISION;
228 	params->hdr.size = tag_size(tag_revision);
229 	params->u.revision.rev = rev;
230 	params = tag_next(params);
231 }
232 #endif  /* CONFIG_REVISION_TAG */
233 
234 
235 static void setup_end_tag(bd_t *bd)
236 {
237 	params->hdr.tag = ATAG_NONE;
238 	params->hdr.size = 0;
239 }
240 
241 #endif /* CONFIG_SETUP_MEMORY_TAGS || CONFIG_CMDLINE_TAG || CONFIG_INITRD_TAG */
242