blob: b3b8bc290378d4629e71dc9cead41d43803bfc78 [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>
Simon Glass3ba929a2020-10-30 21:38:53 -060015#include <asm/global_data.h>
Macpaul Lin0d1f4742011-10-11 22:33:19 +000016#include <u-boot/zlib.h>
17#include <asm/byteorder.h>
rickf1113c92017-05-18 14:37:53 +080018#include <asm/bootm.h>
Macpaul Lin0d1f4742011-10-11 22:33:19 +000019
20DECLARE_GLOBAL_DATA_PTR;
21
22#if defined(CONFIG_SETUP_MEMORY_TAGS) || \
23 defined(CONFIG_CMDLINE_TAG) || \
24 defined(CONFIG_INITRD_TAG) || \
25 defined(CONFIG_SERIAL_TAG) || \
26 defined(CONFIG_REVISION_TAG)
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +090027static void setup_start_tag(struct bd_info *bd);
Macpaul Lin0d1f4742011-10-11 22:33:19 +000028
29# ifdef CONFIG_SETUP_MEMORY_TAGS
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +090030static void setup_memory_tags(struct bd_info *bd);
Macpaul Lin0d1f4742011-10-11 22:33:19 +000031# endif
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +090032static void setup_commandline_tag(struct bd_info *bd, char *commandline);
Macpaul Lin0d1f4742011-10-11 22:33:19 +000033
34# ifdef CONFIG_INITRD_TAG
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +090035static void setup_initrd_tag(struct bd_info *bd, ulong initrd_start,
36 ulong initrd_end);
Macpaul Lin0d1f4742011-10-11 22:33:19 +000037# endif
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +090038static void setup_end_tag(struct bd_info *bd);
Macpaul Lin0d1f4742011-10-11 22:33:19 +000039
40static struct tag *params;
41#endif /* CONFIG_SETUP_MEMORY_TAGS || CONFIG_CMDLINE_TAG || CONFIG_INITRD_TAG */
42
43int do_bootm_linux(int flag, int argc, char *argv[], bootm_headers_t *images)
44{
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +090045 struct bd_info *bd = gd->bd;
Macpaul Lin0d1f4742011-10-11 22:33:19 +000046 char *s;
47 int machid = bd->bi_arch_number;
48 void (*theKernel)(int zero, int arch, uint params);
49
50#ifdef CONFIG_CMDLINE_TAG
Simon Glass64b723f2017-08-03 12:22:12 -060051 char *commandline = env_get("bootargs");
Macpaul Lin0d1f4742011-10-11 22:33:19 +000052#endif
53
Andreas Bießmannda233ce2013-07-02 13:57:44 +020054 /*
55 * allow the PREP bootm subcommand, it is required for bootm to work
56 */
57 if (flag & BOOTM_STATE_OS_PREP)
58 return 0;
59
Macpaul Lin0d1f4742011-10-11 22:33:19 +000060 if ((flag != 0) && (flag != BOOTM_STATE_OS_GO))
61 return 1;
62
63 theKernel = (void (*)(int, int, uint))images->ep;
64
Simon Glass64b723f2017-08-03 12:22:12 -060065 s = env_get("machid");
Macpaul Lin0d1f4742011-10-11 22:33:19 +000066 if (s) {
67 machid = simple_strtoul(s, NULL, 16);
68 printf("Using machid 0x%x from environment\n", machid);
69 }
70
Simon Glass0169e6b2012-02-13 13:51:18 +000071 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
Macpaul Lin0d1f4742011-10-11 22:33:19 +000072
73 debug("## Transferring control to Linux (at address %08lx) ...\n",
74 (ulong)theKernel);
75
rickf1113c92017-05-18 14:37:53 +080076 if (IMAGE_ENABLE_OF_LIBFDT && images->ft_len) {
77#ifdef CONFIG_OF_LIBFDT
78 debug("using: FDT\n");
79 if (image_setup_linux(images)) {
80 printf("FDT creation failed! hanging...");
81 hang();
82 }
83#endif
84 } else if (BOOTM_ENABLE_TAGS) {
Macpaul Lin0d1f4742011-10-11 22:33:19 +000085#if defined(CONFIG_SETUP_MEMORY_TAGS) || \
86 defined(CONFIG_CMDLINE_TAG) || \
87 defined(CONFIG_INITRD_TAG) || \
88 defined(CONFIG_SERIAL_TAG) || \
89 defined(CONFIG_REVISION_TAG)
90 setup_start_tag(bd);
91#ifdef CONFIG_SERIAL_TAG
92 setup_serial_tag(&params);
93#endif
94#ifdef CONFIG_REVISION_TAG
95 setup_revision_tag(&params);
96#endif
97#ifdef CONFIG_SETUP_MEMORY_TAGS
98 setup_memory_tags(bd);
99#endif
100#ifdef CONFIG_CMDLINE_TAG
101 setup_commandline_tag(bd, commandline);
102#endif
103#ifdef CONFIG_INITRD_TAG
104 if (images->rd_start && images->rd_end)
105 setup_initrd_tag(bd, images->rd_start, images->rd_end);
106#endif
107 setup_end_tag(bd);
108#endif
109
110 /* we assume that the kernel is in place */
111 printf("\nStarting kernel ...\n\n");
112
113#ifdef CONFIG_USB_DEVICE
114 {
115 extern void udc_disconnect(void);
116 udc_disconnect();
117 }
118#endif
rickf1113c92017-05-18 14:37:53 +0800119 }
Macpaul Lin0d1f4742011-10-11 22:33:19 +0000120 cleanup_before_linux();
rickf1113c92017-05-18 14:37:53 +0800121 if (IMAGE_ENABLE_OF_LIBFDT && images->ft_len)
122 theKernel(0, machid, (unsigned long)images->ft_addr);
123 else
124 theKernel(0, machid, bd->bi_boot_params);
Macpaul Lin0d1f4742011-10-11 22:33:19 +0000125 /* does not return */
126
127 return 1;
128}
129
Macpaul Lin0d1f4742011-10-11 22:33:19 +0000130#if defined(CONFIG_SETUP_MEMORY_TAGS) || \
131 defined(CONFIG_CMDLINE_TAG) || \
132 defined(CONFIG_INITRD_TAG) || \
133 defined(CONFIG_SERIAL_TAG) || \
134 defined(CONFIG_REVISION_TAG)
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +0900135static void setup_start_tag(struct bd_info *bd)
Macpaul Lin0d1f4742011-10-11 22:33:19 +0000136{
137 params = (struct tag *)bd->bi_boot_params;
138
139 params->hdr.tag = ATAG_CORE;
140 params->hdr.size = tag_size(tag_core);
141
142 params->u.core.flags = 0;
143 params->u.core.pagesize = 0;
144 params->u.core.rootdev = 0;
145
146 params = tag_next(params);
147}
148
Macpaul Lin0d1f4742011-10-11 22:33:19 +0000149#ifdef CONFIG_SETUP_MEMORY_TAGS
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +0900150static void setup_memory_tags(struct bd_info *bd)
Macpaul Lin0d1f4742011-10-11 22:33:19 +0000151{
152 int i;
153
154 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
155 params->hdr.tag = ATAG_MEM;
156 params->hdr.size = tag_size(tag_mem32);
157
158 params->u.mem.start = bd->bi_dram[i].start;
159 params->u.mem.size = bd->bi_dram[i].size;
160
161 params = tag_next(params);
162 }
163}
164#endif /* CONFIG_SETUP_MEMORY_TAGS */
165
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +0900166static void setup_commandline_tag(struct bd_info *bd, char *commandline)
Macpaul Lin0d1f4742011-10-11 22:33:19 +0000167{
168 char *p;
169
170 if (!commandline)
171 return;
172
173 /* eat leading white space */
174 for (p = commandline; *p == ' '; p++)
175 ;
176
177 /* skip non-existent command lines so the kernel will still
178 * use its default command line.
179 */
180 if (*p == '\0')
181 return;
182
183 params->hdr.tag = ATAG_CMDLINE;
184 params->hdr.size =
185 (sizeof(struct tag_header) + strlen(p) + 1 + 4) >> 2;
186
187 strcpy(params->u.cmdline.cmdline, p)
188 ;
189
190 params = tag_next(params);
191}
192
Macpaul Lin0d1f4742011-10-11 22:33:19 +0000193#ifdef CONFIG_INITRD_TAG
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +0900194static void setup_initrd_tag(struct bd_info *bd, ulong initrd_start,
195 ulong initrd_end)
Macpaul Lin0d1f4742011-10-11 22:33:19 +0000196{
197 /* an ATAG_INITRD node tells the kernel where the compressed
198 * ramdisk can be found. ATAG_RDIMG is a better name, actually.
199 */
200 params->hdr.tag = ATAG_INITRD2;
201 params->hdr.size = tag_size(tag_initrd);
202
203 params->u.initrd.start = initrd_start;
204 params->u.initrd.size = initrd_end - initrd_start;
205
206 params = tag_next(params);
207}
208#endif /* CONFIG_INITRD_TAG */
209
210#ifdef CONFIG_SERIAL_TAG
211void setup_serial_tag(struct tag **tmp)
212{
213 struct tag *params = *tmp;
214 struct tag_serialnr serialnr;
215 void get_board_serial(struct tag_serialnr *serialnr);
216
217 get_board_serial(&serialnr);
218 params->hdr.tag = ATAG_SERIAL;
219 params->hdr.size = tag_size(tag_serialnr);
220 params->u.serialnr.low = serialnr.low;
221 params->u.serialnr.high = serialnr.high;
222 params = tag_next(params);
223 *tmp = params;
224}
225#endif
226
227#ifdef CONFIG_REVISION_TAG
228void setup_revision_tag(struct tag **in_params)
229{
230 u32 rev = 0;
231 u32 get_board_rev(void);
232
233 rev = get_board_rev();
234 params->hdr.tag = ATAG_REVISION;
235 params->hdr.size = tag_size(tag_revision);
236 params->u.revision.rev = rev;
237 params = tag_next(params);
238}
239#endif /* CONFIG_REVISION_TAG */
240
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +0900241static void setup_end_tag(struct bd_info *bd)
Macpaul Lin0d1f4742011-10-11 22:33:19 +0000242{
243 params->hdr.tag = ATAG_NONE;
244 params->hdr.size = 0;
245}
246
247#endif /* CONFIG_SETUP_MEMORY_TAGS || CONFIG_CMDLINE_TAG || CONFIG_INITRD_TAG */