blob: e552a342300507f047b9916e0d40355bf6b4ac9f [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Macpaul Lin0d1f4742011-10-11 22:33:19 +00002/*
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>
Macpaul Lin0d1f4742011-10-11 22:33:19 +00006 */
7
8#include <common.h>
Simon Glass1ea97892020-05-10 11:40:00 -06009#include <bootstage.h>
Macpaul Lin0d1f4742011-10-11 22:33:19 +000010#include <command.h>
Simon Glass0af6e2d2019-08-01 09:46:52 -060011#include <env.h>
Simon Glassf11478f2019-12-28 10:45:07 -070012#include <hang.h>
Macpaul Lin0d1f4742011-10-11 22:33:19 +000013#include <image.h>
Simon Glass0f2af882020-05-10 11:40:05 -060014#include <log.h>
Macpaul Lin0d1f4742011-10-11 22:33:19 +000015#include <u-boot/zlib.h>
16#include <asm/byteorder.h>
rickf1113c92017-05-18 14:37:53 +080017#include <asm/bootm.h>
Macpaul Lin0d1f4742011-10-11 22:33:19 +000018
19DECLARE_GLOBAL_DATA_PTR;
20
21#if defined(CONFIG_SETUP_MEMORY_TAGS) || \
22 defined(CONFIG_CMDLINE_TAG) || \
23 defined(CONFIG_INITRD_TAG) || \
24 defined(CONFIG_SERIAL_TAG) || \
25 defined(CONFIG_REVISION_TAG)
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +090026static void setup_start_tag(struct bd_info *bd);
Macpaul Lin0d1f4742011-10-11 22:33:19 +000027
28# ifdef CONFIG_SETUP_MEMORY_TAGS
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +090029static void setup_memory_tags(struct bd_info *bd);
Macpaul Lin0d1f4742011-10-11 22:33:19 +000030# endif
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +090031static void setup_commandline_tag(struct bd_info *bd, char *commandline);
Macpaul Lin0d1f4742011-10-11 22:33:19 +000032
33# ifdef CONFIG_INITRD_TAG
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +090034static void setup_initrd_tag(struct bd_info *bd, ulong initrd_start,
35 ulong initrd_end);
Macpaul Lin0d1f4742011-10-11 22:33:19 +000036# endif
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +090037static void setup_end_tag(struct bd_info *bd);
Macpaul Lin0d1f4742011-10-11 22:33:19 +000038
39static struct tag *params;
40#endif /* CONFIG_SETUP_MEMORY_TAGS || CONFIG_CMDLINE_TAG || CONFIG_INITRD_TAG */
41
42int do_bootm_linux(int flag, int argc, char *argv[], bootm_headers_t *images)
43{
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +090044 struct bd_info *bd = gd->bd;
Macpaul Lin0d1f4742011-10-11 22:33:19 +000045 char *s;
46 int machid = bd->bi_arch_number;
47 void (*theKernel)(int zero, int arch, uint params);
48
49#ifdef CONFIG_CMDLINE_TAG
Simon Glass64b723f2017-08-03 12:22:12 -060050 char *commandline = env_get("bootargs");
Macpaul Lin0d1f4742011-10-11 22:33:19 +000051#endif
52
Andreas Bießmannda233ce2013-07-02 13:57:44 +020053 /*
54 * allow the PREP bootm subcommand, it is required for bootm to work
55 */
56 if (flag & BOOTM_STATE_OS_PREP)
57 return 0;
58
Macpaul Lin0d1f4742011-10-11 22:33:19 +000059 if ((flag != 0) && (flag != BOOTM_STATE_OS_GO))
60 return 1;
61
62 theKernel = (void (*)(int, int, uint))images->ep;
63
Simon Glass64b723f2017-08-03 12:22:12 -060064 s = env_get("machid");
Macpaul Lin0d1f4742011-10-11 22:33:19 +000065 if (s) {
66 machid = simple_strtoul(s, NULL, 16);
67 printf("Using machid 0x%x from environment\n", machid);
68 }
69
Simon Glass0169e6b2012-02-13 13:51:18 +000070 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
Macpaul Lin0d1f4742011-10-11 22:33:19 +000071
72 debug("## Transferring control to Linux (at address %08lx) ...\n",
73 (ulong)theKernel);
74
rickf1113c92017-05-18 14:37:53 +080075 if (IMAGE_ENABLE_OF_LIBFDT && images->ft_len) {
76#ifdef CONFIG_OF_LIBFDT
77 debug("using: FDT\n");
78 if (image_setup_linux(images)) {
79 printf("FDT creation failed! hanging...");
80 hang();
81 }
82#endif
83 } else if (BOOTM_ENABLE_TAGS) {
Macpaul Lin0d1f4742011-10-11 22:33:19 +000084#if defined(CONFIG_SETUP_MEMORY_TAGS) || \
85 defined(CONFIG_CMDLINE_TAG) || \
86 defined(CONFIG_INITRD_TAG) || \
87 defined(CONFIG_SERIAL_TAG) || \
88 defined(CONFIG_REVISION_TAG)
89 setup_start_tag(bd);
90#ifdef CONFIG_SERIAL_TAG
91 setup_serial_tag(&params);
92#endif
93#ifdef CONFIG_REVISION_TAG
94 setup_revision_tag(&params);
95#endif
96#ifdef CONFIG_SETUP_MEMORY_TAGS
97 setup_memory_tags(bd);
98#endif
99#ifdef CONFIG_CMDLINE_TAG
100 setup_commandline_tag(bd, commandline);
101#endif
102#ifdef CONFIG_INITRD_TAG
103 if (images->rd_start && images->rd_end)
104 setup_initrd_tag(bd, images->rd_start, images->rd_end);
105#endif
106 setup_end_tag(bd);
107#endif
108
109 /* we assume that the kernel is in place */
110 printf("\nStarting kernel ...\n\n");
111
112#ifdef CONFIG_USB_DEVICE
113 {
114 extern void udc_disconnect(void);
115 udc_disconnect();
116 }
117#endif
rickf1113c92017-05-18 14:37:53 +0800118 }
Macpaul Lin0d1f4742011-10-11 22:33:19 +0000119 cleanup_before_linux();
rickf1113c92017-05-18 14:37:53 +0800120 if (IMAGE_ENABLE_OF_LIBFDT && images->ft_len)
121 theKernel(0, machid, (unsigned long)images->ft_addr);
122 else
123 theKernel(0, machid, bd->bi_boot_params);
Macpaul Lin0d1f4742011-10-11 22:33:19 +0000124 /* does not return */
125
126 return 1;
127}
128
Macpaul Lin0d1f4742011-10-11 22:33:19 +0000129#if defined(CONFIG_SETUP_MEMORY_TAGS) || \
130 defined(CONFIG_CMDLINE_TAG) || \
131 defined(CONFIG_INITRD_TAG) || \
132 defined(CONFIG_SERIAL_TAG) || \
133 defined(CONFIG_REVISION_TAG)
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +0900134static void setup_start_tag(struct bd_info *bd)
Macpaul Lin0d1f4742011-10-11 22:33:19 +0000135{
136 params = (struct tag *)bd->bi_boot_params;
137
138 params->hdr.tag = ATAG_CORE;
139 params->hdr.size = tag_size(tag_core);
140
141 params->u.core.flags = 0;
142 params->u.core.pagesize = 0;
143 params->u.core.rootdev = 0;
144
145 params = tag_next(params);
146}
147
Macpaul Lin0d1f4742011-10-11 22:33:19 +0000148#ifdef CONFIG_SETUP_MEMORY_TAGS
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +0900149static void setup_memory_tags(struct bd_info *bd)
Macpaul Lin0d1f4742011-10-11 22:33:19 +0000150{
151 int i;
152
153 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
154 params->hdr.tag = ATAG_MEM;
155 params->hdr.size = tag_size(tag_mem32);
156
157 params->u.mem.start = bd->bi_dram[i].start;
158 params->u.mem.size = bd->bi_dram[i].size;
159
160 params = tag_next(params);
161 }
162}
163#endif /* CONFIG_SETUP_MEMORY_TAGS */
164
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +0900165static void setup_commandline_tag(struct bd_info *bd, char *commandline)
Macpaul Lin0d1f4742011-10-11 22:33:19 +0000166{
167 char *p;
168
169 if (!commandline)
170 return;
171
172 /* eat leading white space */
173 for (p = commandline; *p == ' '; p++)
174 ;
175
176 /* skip non-existent command lines so the kernel will still
177 * use its default command line.
178 */
179 if (*p == '\0')
180 return;
181
182 params->hdr.tag = ATAG_CMDLINE;
183 params->hdr.size =
184 (sizeof(struct tag_header) + strlen(p) + 1 + 4) >> 2;
185
186 strcpy(params->u.cmdline.cmdline, p)
187 ;
188
189 params = tag_next(params);
190}
191
Macpaul Lin0d1f4742011-10-11 22:33:19 +0000192#ifdef CONFIG_INITRD_TAG
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +0900193static void setup_initrd_tag(struct bd_info *bd, ulong initrd_start,
194 ulong initrd_end)
Macpaul Lin0d1f4742011-10-11 22:33:19 +0000195{
196 /* an ATAG_INITRD node tells the kernel where the compressed
197 * ramdisk can be found. ATAG_RDIMG is a better name, actually.
198 */
199 params->hdr.tag = ATAG_INITRD2;
200 params->hdr.size = tag_size(tag_initrd);
201
202 params->u.initrd.start = initrd_start;
203 params->u.initrd.size = initrd_end - initrd_start;
204
205 params = tag_next(params);
206}
207#endif /* CONFIG_INITRD_TAG */
208
209#ifdef CONFIG_SERIAL_TAG
210void setup_serial_tag(struct tag **tmp)
211{
212 struct tag *params = *tmp;
213 struct tag_serialnr serialnr;
214 void get_board_serial(struct tag_serialnr *serialnr);
215
216 get_board_serial(&serialnr);
217 params->hdr.tag = ATAG_SERIAL;
218 params->hdr.size = tag_size(tag_serialnr);
219 params->u.serialnr.low = serialnr.low;
220 params->u.serialnr.high = serialnr.high;
221 params = tag_next(params);
222 *tmp = params;
223}
224#endif
225
226#ifdef CONFIG_REVISION_TAG
227void setup_revision_tag(struct tag **in_params)
228{
229 u32 rev = 0;
230 u32 get_board_rev(void);
231
232 rev = get_board_rev();
233 params->hdr.tag = ATAG_REVISION;
234 params->hdr.size = tag_size(tag_revision);
235 params->u.revision.rev = rev;
236 params = tag_next(params);
237}
238#endif /* CONFIG_REVISION_TAG */
239
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +0900240static void setup_end_tag(struct bd_info *bd)
Macpaul Lin0d1f4742011-10-11 22:33:19 +0000241{
242 params->hdr.tag = ATAG_NONE;
243 params->hdr.size = 0;
244}
245
246#endif /* CONFIG_SETUP_MEMORY_TAGS || CONFIG_CMDLINE_TAG || CONFIG_INITRD_TAG */