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>
14b841b6e9Srick #include <asm/bootm.h>
153a53e99cSSimon Glass #include <asm/setup.h>
16463d47f6SMacpaul Lin
17463d47f6SMacpaul Lin DECLARE_GLOBAL_DATA_PTR;
18463d47f6SMacpaul Lin
19463d47f6SMacpaul Lin #if defined(CONFIG_SETUP_MEMORY_TAGS) || \
20463d47f6SMacpaul Lin defined(CONFIG_CMDLINE_TAG) || \
21463d47f6SMacpaul Lin defined(CONFIG_INITRD_TAG) || \
22463d47f6SMacpaul Lin defined(CONFIG_SERIAL_TAG) || \
23463d47f6SMacpaul Lin defined(CONFIG_REVISION_TAG)
24463d47f6SMacpaul Lin static void setup_start_tag(bd_t *bd);
25463d47f6SMacpaul Lin
26463d47f6SMacpaul Lin # ifdef CONFIG_SETUP_MEMORY_TAGS
27463d47f6SMacpaul Lin static void setup_memory_tags(bd_t *bd);
28463d47f6SMacpaul Lin # endif
29463d47f6SMacpaul Lin static void setup_commandline_tag(bd_t *bd, char *commandline);
30463d47f6SMacpaul Lin
31463d47f6SMacpaul Lin # ifdef CONFIG_INITRD_TAG
32463d47f6SMacpaul Lin static void setup_initrd_tag(bd_t *bd, ulong initrd_start, ulong initrd_end);
33463d47f6SMacpaul Lin # endif
34463d47f6SMacpaul Lin static void setup_end_tag(bd_t *bd);
35463d47f6SMacpaul Lin
36463d47f6SMacpaul Lin static struct tag *params;
37463d47f6SMacpaul Lin #endif /* CONFIG_SETUP_MEMORY_TAGS || CONFIG_CMDLINE_TAG || CONFIG_INITRD_TAG */
38463d47f6SMacpaul Lin
do_bootm_linux(int flag,int argc,char * argv[],bootm_headers_t * images)39463d47f6SMacpaul Lin int do_bootm_linux(int flag, int argc, char *argv[], bootm_headers_t *images)
40463d47f6SMacpaul Lin {
41463d47f6SMacpaul Lin bd_t *bd = gd->bd;
42463d47f6SMacpaul Lin char *s;
43463d47f6SMacpaul Lin int machid = bd->bi_arch_number;
44463d47f6SMacpaul Lin void (*theKernel)(int zero, int arch, uint params);
45463d47f6SMacpaul Lin
46463d47f6SMacpaul Lin #ifdef CONFIG_CMDLINE_TAG
47*00caae6dSSimon Glass char *commandline = env_get("bootargs");
48463d47f6SMacpaul Lin #endif
49463d47f6SMacpaul Lin
502cb0e55aSAndreas Bießmann /*
512cb0e55aSAndreas Bießmann * allow the PREP bootm subcommand, it is required for bootm to work
522cb0e55aSAndreas Bießmann */
532cb0e55aSAndreas Bießmann if (flag & BOOTM_STATE_OS_PREP)
542cb0e55aSAndreas Bießmann return 0;
552cb0e55aSAndreas Bießmann
56463d47f6SMacpaul Lin if ((flag != 0) && (flag != BOOTM_STATE_OS_GO))
57463d47f6SMacpaul Lin return 1;
58463d47f6SMacpaul Lin
59463d47f6SMacpaul Lin theKernel = (void (*)(int, int, uint))images->ep;
60463d47f6SMacpaul Lin
61*00caae6dSSimon Glass s = env_get("machid");
62463d47f6SMacpaul Lin if (s) {
63463d47f6SMacpaul Lin machid = simple_strtoul(s, NULL, 16);
64463d47f6SMacpaul Lin printf("Using machid 0x%x from environment\n", machid);
65463d47f6SMacpaul Lin }
66463d47f6SMacpaul Lin
67770605e4SSimon Glass bootstage_mark(BOOTSTAGE_ID_RUN_OS);
68463d47f6SMacpaul Lin
69463d47f6SMacpaul Lin debug("## Transferring control to Linux (at address %08lx) ...\n",
70463d47f6SMacpaul Lin (ulong)theKernel);
71463d47f6SMacpaul Lin
72b841b6e9Srick if (IMAGE_ENABLE_OF_LIBFDT && images->ft_len) {
73b841b6e9Srick #ifdef CONFIG_OF_LIBFDT
74b841b6e9Srick debug("using: FDT\n");
75b841b6e9Srick if (image_setup_linux(images)) {
76b841b6e9Srick printf("FDT creation failed! hanging...");
77b841b6e9Srick hang();
78b841b6e9Srick }
79b841b6e9Srick #endif
80b841b6e9Srick } else if (BOOTM_ENABLE_TAGS) {
81463d47f6SMacpaul Lin #if defined(CONFIG_SETUP_MEMORY_TAGS) || \
82463d47f6SMacpaul Lin defined(CONFIG_CMDLINE_TAG) || \
83463d47f6SMacpaul Lin defined(CONFIG_INITRD_TAG) || \
84463d47f6SMacpaul Lin defined(CONFIG_SERIAL_TAG) || \
85463d47f6SMacpaul Lin defined(CONFIG_REVISION_TAG)
86463d47f6SMacpaul Lin setup_start_tag(bd);
87463d47f6SMacpaul Lin #ifdef CONFIG_SERIAL_TAG
88463d47f6SMacpaul Lin setup_serial_tag(¶ms);
89463d47f6SMacpaul Lin #endif
90463d47f6SMacpaul Lin #ifdef CONFIG_REVISION_TAG
91463d47f6SMacpaul Lin setup_revision_tag(¶ms);
92463d47f6SMacpaul Lin #endif
93463d47f6SMacpaul Lin #ifdef CONFIG_SETUP_MEMORY_TAGS
94463d47f6SMacpaul Lin setup_memory_tags(bd);
95463d47f6SMacpaul Lin #endif
96463d47f6SMacpaul Lin #ifdef CONFIG_CMDLINE_TAG
97463d47f6SMacpaul Lin setup_commandline_tag(bd, commandline);
98463d47f6SMacpaul Lin #endif
99463d47f6SMacpaul Lin #ifdef CONFIG_INITRD_TAG
100463d47f6SMacpaul Lin if (images->rd_start && images->rd_end)
101463d47f6SMacpaul Lin setup_initrd_tag(bd, images->rd_start, images->rd_end);
102463d47f6SMacpaul Lin #endif
103463d47f6SMacpaul Lin setup_end_tag(bd);
104463d47f6SMacpaul Lin #endif
105463d47f6SMacpaul Lin
106463d47f6SMacpaul Lin /* we assume that the kernel is in place */
107463d47f6SMacpaul Lin printf("\nStarting kernel ...\n\n");
108463d47f6SMacpaul Lin
109463d47f6SMacpaul Lin #ifdef CONFIG_USB_DEVICE
110463d47f6SMacpaul Lin {
111463d47f6SMacpaul Lin extern void udc_disconnect(void);
112463d47f6SMacpaul Lin udc_disconnect();
113463d47f6SMacpaul Lin }
114463d47f6SMacpaul Lin #endif
115b841b6e9Srick }
116463d47f6SMacpaul Lin cleanup_before_linux();
117b841b6e9Srick if (IMAGE_ENABLE_OF_LIBFDT && images->ft_len)
118b841b6e9Srick theKernel(0, machid, (unsigned long)images->ft_addr);
119b841b6e9Srick else
120463d47f6SMacpaul Lin theKernel(0, machid, bd->bi_boot_params);
121463d47f6SMacpaul Lin /* does not return */
122463d47f6SMacpaul Lin
123463d47f6SMacpaul Lin return 1;
124463d47f6SMacpaul Lin }
125463d47f6SMacpaul Lin
126463d47f6SMacpaul Lin #if defined(CONFIG_SETUP_MEMORY_TAGS) || \
127463d47f6SMacpaul Lin defined(CONFIG_CMDLINE_TAG) || \
128463d47f6SMacpaul Lin defined(CONFIG_INITRD_TAG) || \
129463d47f6SMacpaul Lin defined(CONFIG_SERIAL_TAG) || \
130463d47f6SMacpaul Lin defined(CONFIG_REVISION_TAG)
setup_start_tag(bd_t * bd)131463d47f6SMacpaul Lin static void setup_start_tag(bd_t *bd)
132463d47f6SMacpaul Lin {
133463d47f6SMacpaul Lin params = (struct tag *)bd->bi_boot_params;
134463d47f6SMacpaul Lin
135463d47f6SMacpaul Lin params->hdr.tag = ATAG_CORE;
136463d47f6SMacpaul Lin params->hdr.size = tag_size(tag_core);
137463d47f6SMacpaul Lin
138463d47f6SMacpaul Lin params->u.core.flags = 0;
139463d47f6SMacpaul Lin params->u.core.pagesize = 0;
140463d47f6SMacpaul Lin params->u.core.rootdev = 0;
141463d47f6SMacpaul Lin
142463d47f6SMacpaul Lin params = tag_next(params);
143463d47f6SMacpaul Lin }
144463d47f6SMacpaul Lin
145463d47f6SMacpaul Lin #ifdef CONFIG_SETUP_MEMORY_TAGS
setup_memory_tags(bd_t * bd)146463d47f6SMacpaul Lin static void setup_memory_tags(bd_t *bd)
147463d47f6SMacpaul Lin {
148463d47f6SMacpaul Lin int i;
149463d47f6SMacpaul Lin
150463d47f6SMacpaul Lin for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
151463d47f6SMacpaul Lin params->hdr.tag = ATAG_MEM;
152463d47f6SMacpaul Lin params->hdr.size = tag_size(tag_mem32);
153463d47f6SMacpaul Lin
154463d47f6SMacpaul Lin params->u.mem.start = bd->bi_dram[i].start;
155463d47f6SMacpaul Lin params->u.mem.size = bd->bi_dram[i].size;
156463d47f6SMacpaul Lin
157463d47f6SMacpaul Lin params = tag_next(params);
158463d47f6SMacpaul Lin }
159463d47f6SMacpaul Lin }
160463d47f6SMacpaul Lin #endif /* CONFIG_SETUP_MEMORY_TAGS */
161463d47f6SMacpaul Lin
setup_commandline_tag(bd_t * bd,char * commandline)162463d47f6SMacpaul Lin static void setup_commandline_tag(bd_t *bd, char *commandline)
163463d47f6SMacpaul Lin {
164463d47f6SMacpaul Lin char *p;
165463d47f6SMacpaul Lin
166463d47f6SMacpaul Lin if (!commandline)
167463d47f6SMacpaul Lin return;
168463d47f6SMacpaul Lin
169463d47f6SMacpaul Lin /* eat leading white space */
170463d47f6SMacpaul Lin for (p = commandline; *p == ' '; p++)
171463d47f6SMacpaul Lin ;
172463d47f6SMacpaul Lin
173463d47f6SMacpaul Lin /* skip non-existent command lines so the kernel will still
174463d47f6SMacpaul Lin * use its default command line.
175463d47f6SMacpaul Lin */
176463d47f6SMacpaul Lin if (*p == '\0')
177463d47f6SMacpaul Lin return;
178463d47f6SMacpaul Lin
179463d47f6SMacpaul Lin params->hdr.tag = ATAG_CMDLINE;
180463d47f6SMacpaul Lin params->hdr.size =
181463d47f6SMacpaul Lin (sizeof(struct tag_header) + strlen(p) + 1 + 4) >> 2;
182463d47f6SMacpaul Lin
183463d47f6SMacpaul Lin strcpy(params->u.cmdline.cmdline, p)
184463d47f6SMacpaul Lin ;
185463d47f6SMacpaul Lin
186463d47f6SMacpaul Lin params = tag_next(params);
187463d47f6SMacpaul Lin }
188463d47f6SMacpaul Lin
189463d47f6SMacpaul Lin #ifdef CONFIG_INITRD_TAG
setup_initrd_tag(bd_t * bd,ulong initrd_start,ulong initrd_end)190463d47f6SMacpaul Lin static void setup_initrd_tag(bd_t *bd, ulong initrd_start, ulong initrd_end)
191463d47f6SMacpaul Lin {
192463d47f6SMacpaul Lin /* an ATAG_INITRD node tells the kernel where the compressed
193463d47f6SMacpaul Lin * ramdisk can be found. ATAG_RDIMG is a better name, actually.
194463d47f6SMacpaul Lin */
195463d47f6SMacpaul Lin params->hdr.tag = ATAG_INITRD2;
196463d47f6SMacpaul Lin params->hdr.size = tag_size(tag_initrd);
197463d47f6SMacpaul Lin
198463d47f6SMacpaul Lin params->u.initrd.start = initrd_start;
199463d47f6SMacpaul Lin params->u.initrd.size = initrd_end - initrd_start;
200463d47f6SMacpaul Lin
201463d47f6SMacpaul Lin params = tag_next(params);
202463d47f6SMacpaul Lin }
203463d47f6SMacpaul Lin #endif /* CONFIG_INITRD_TAG */
204463d47f6SMacpaul Lin
205463d47f6SMacpaul Lin #ifdef CONFIG_SERIAL_TAG
setup_serial_tag(struct tag ** tmp)206463d47f6SMacpaul Lin void setup_serial_tag(struct tag **tmp)
207463d47f6SMacpaul Lin {
208463d47f6SMacpaul Lin struct tag *params = *tmp;
209463d47f6SMacpaul Lin struct tag_serialnr serialnr;
210463d47f6SMacpaul Lin void get_board_serial(struct tag_serialnr *serialnr);
211463d47f6SMacpaul Lin
212463d47f6SMacpaul Lin get_board_serial(&serialnr);
213463d47f6SMacpaul Lin params->hdr.tag = ATAG_SERIAL;
214463d47f6SMacpaul Lin params->hdr.size = tag_size(tag_serialnr);
215463d47f6SMacpaul Lin params->u.serialnr.low = serialnr.low;
216463d47f6SMacpaul Lin params->u.serialnr.high = serialnr.high;
217463d47f6SMacpaul Lin params = tag_next(params);
218463d47f6SMacpaul Lin *tmp = params;
219463d47f6SMacpaul Lin }
220463d47f6SMacpaul Lin #endif
221463d47f6SMacpaul Lin
222463d47f6SMacpaul Lin #ifdef CONFIG_REVISION_TAG
setup_revision_tag(struct tag ** in_params)223463d47f6SMacpaul Lin void setup_revision_tag(struct tag **in_params)
224463d47f6SMacpaul Lin {
225463d47f6SMacpaul Lin u32 rev = 0;
226463d47f6SMacpaul Lin u32 get_board_rev(void);
227463d47f6SMacpaul Lin
228463d47f6SMacpaul Lin rev = get_board_rev();
229463d47f6SMacpaul Lin params->hdr.tag = ATAG_REVISION;
230463d47f6SMacpaul Lin params->hdr.size = tag_size(tag_revision);
231463d47f6SMacpaul Lin params->u.revision.rev = rev;
232463d47f6SMacpaul Lin params = tag_next(params);
233463d47f6SMacpaul Lin }
234463d47f6SMacpaul Lin #endif /* CONFIG_REVISION_TAG */
235463d47f6SMacpaul Lin
setup_end_tag(bd_t * bd)236463d47f6SMacpaul Lin static void setup_end_tag(bd_t *bd)
237463d47f6SMacpaul Lin {
238463d47f6SMacpaul Lin params->hdr.tag = ATAG_NONE;
239463d47f6SMacpaul Lin params->hdr.size = 0;
240463d47f6SMacpaul Lin }
241463d47f6SMacpaul Lin
242463d47f6SMacpaul Lin #endif /* CONFIG_SETUP_MEMORY_TAGS || CONFIG_CMDLINE_TAG || CONFIG_INITRD_TAG */
243