common: Drop asm/global_data.h from common header
[platform/kernel/u-boot.git] / arch / nds32 / lib / bootm.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
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>
6  */
7
8 #include <common.h>
9 #include <bootstage.h>
10 #include <command.h>
11 #include <env.h>
12 #include <hang.h>
13 #include <image.h>
14 #include <log.h>
15 #include <asm/global_data.h>
16 #include <u-boot/zlib.h>
17 #include <asm/byteorder.h>
18 #include <asm/bootm.h>
19
20 DECLARE_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)
27 static void setup_start_tag(struct bd_info *bd);
28
29 # ifdef CONFIG_SETUP_MEMORY_TAGS
30 static void setup_memory_tags(struct bd_info *bd);
31 # endif
32 static void setup_commandline_tag(struct bd_info *bd, char *commandline);
33
34 # ifdef CONFIG_INITRD_TAG
35 static void setup_initrd_tag(struct bd_info *bd, ulong initrd_start,
36                              ulong initrd_end);
37 # endif
38 static void setup_end_tag(struct bd_info *bd);
39
40 static struct tag *params;
41 #endif /* CONFIG_SETUP_MEMORY_TAGS || CONFIG_CMDLINE_TAG || CONFIG_INITRD_TAG */
42
43 int do_bootm_linux(int flag, int argc, char *argv[], bootm_headers_t *images)
44 {
45         struct bd_info  *bd = gd->bd;
46         char    *s;
47         int     machid = bd->bi_arch_number;
48         void    (*theKernel)(int zero, int arch, uint params);
49
50 #ifdef CONFIG_CMDLINE_TAG
51         char *commandline = env_get("bootargs");
52 #endif
53
54         /*
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
60         if ((flag != 0) && (flag != BOOTM_STATE_OS_GO))
61                 return 1;
62
63         theKernel = (void (*)(int, int, uint))images->ep;
64
65         s = env_get("machid");
66         if (s) {
67                 machid = simple_strtoul(s, NULL, 16);
68                 printf("Using machid 0x%x from environment\n", machid);
69         }
70
71         bootstage_mark(BOOTSTAGE_ID_RUN_OS);
72
73         debug("## Transferring control to Linux (at address %08lx) ...\n",
74                (ulong)theKernel);
75
76         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) {
85 #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
119         }
120         cleanup_before_linux();
121         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);
125         /* does not return */
126
127         return 1;
128 }
129
130 #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)
135 static void setup_start_tag(struct bd_info *bd)
136 {
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
149 #ifdef CONFIG_SETUP_MEMORY_TAGS
150 static void setup_memory_tags(struct bd_info *bd)
151 {
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
166 static void setup_commandline_tag(struct bd_info *bd, char *commandline)
167 {
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
193 #ifdef CONFIG_INITRD_TAG
194 static void setup_initrd_tag(struct bd_info *bd, ulong initrd_start,
195                              ulong initrd_end)
196 {
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
211 void 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
228 void 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
241 static void setup_end_tag(struct bd_info *bd)
242 {
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 */