blob: 77349530f02e10cc324aa43b90cf36db71f00493 [file] [log] [blame]
wdenkc6097192002-11-03 00:24:07 +00001/*
2 * (C) Copyright 2002
3 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
4 * Marius Groeger <mgroeger@sysgo.de>
5 *
6 * Copyright (C) 2001 Erik Mouw (J.A.K.Mouw@its.tudelft.nl)
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
wdenk29e7f5a2004-03-12 00:14:09 +000015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
wdenkc6097192002-11-03 00:24:07 +000016 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
wdenk29e7f5a2004-03-12 00:14:09 +000020 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
wdenkc6097192002-11-03 00:24:07 +000021 *
22 */
23
24#include <common.h>
25#include <command.h>
wdenkc6097192002-11-03 00:24:07 +000026#include <image.h>
Jean-Christophe PLAGNIOL-VILLARD6bb94492009-04-04 12:49:11 +020027#include <u-boot/zlib.h>
wdenkc6097192002-11-03 00:24:07 +000028#include <asm/byteorder.h>
John Rigbyab51f1e2010-10-13 13:57:36 -060029#include <fdt.h>
30#include <libfdt.h>
31#include <fdt_support.h>
wdenkc6097192002-11-03 00:24:07 +000032
Wolfgang Denk6405a152006-03-31 18:32:53 +020033DECLARE_GLOBAL_DATA_PTR;
34
wdenkc6097192002-11-03 00:24:07 +000035#if defined (CONFIG_SETUP_MEMORY_TAGS) || \
36 defined (CONFIG_CMDLINE_TAG) || \
37 defined (CONFIG_INITRD_TAG) || \
wdenkbc01dd52004-01-02 16:05:07 +000038 defined (CONFIG_SERIAL_TAG) || \
Minkyu Kang977e3b62010-06-18 19:31:10 +090039 defined (CONFIG_REVISION_TAG)
wdenk86765902003-12-06 23:55:10 +000040static void setup_start_tag (bd_t *bd);
41
wdenk699b13a2002-11-03 18:03:52 +000042# ifdef CONFIG_SETUP_MEMORY_TAGS
wdenk86765902003-12-06 23:55:10 +000043static void setup_memory_tags (bd_t *bd);
wdenk699b13a2002-11-03 18:03:52 +000044# endif
wdenk86765902003-12-06 23:55:10 +000045static void setup_commandline_tag (bd_t *bd, char *commandline);
46
wdenk699b13a2002-11-03 18:03:52 +000047# ifdef CONFIG_INITRD_TAG
wdenk86765902003-12-06 23:55:10 +000048static void setup_initrd_tag (bd_t *bd, ulong initrd_start,
49 ulong initrd_end);
wdenk699b13a2002-11-03 18:03:52 +000050# endif
wdenk86765902003-12-06 23:55:10 +000051static void setup_end_tag (bd_t *bd);
52
wdenkc6097192002-11-03 00:24:07 +000053static struct tag *params;
54#endif /* CONFIG_SETUP_MEMORY_TAGS || CONFIG_CMDLINE_TAG || CONFIG_INITRD_TAG */
55
John Rigbyab51f1e2010-10-13 13:57:36 -060056static ulong get_sp(void);
57#if defined(CONFIG_OF_LIBFDT)
58static int bootm_linux_fdt(int machid, bootm_headers_t *images);
59#endif
60
61void arch_lmb_reserve(struct lmb *lmb)
62{
63 ulong sp;
64
65 /*
66 * Booting a (Linux) kernel image
67 *
68 * Allocate space for command line and board info - the
69 * address should be as high as possible within the reach of
70 * the kernel (see CONFIG_SYS_BOOTMAPSZ settings), but in unused
71 * memory, which means far enough below the current stack
72 * pointer.
73 */
74 sp = get_sp();
75 debug("## Current stack ends at 0x%08lx ", sp);
76
77 /* adjust sp by 1K to be safe */
78 sp -= 1024;
79 lmb_reserve(lmb, sp,
80 gd->bd->bi_dram[0].start + gd->bd->bi_dram[0].size - sp);
81}
82
83static void announce_and_cleanup(void)
84{
85 printf("\nStarting kernel ...\n\n");
86
87#ifdef CONFIG_USB_DEVICE
88 {
89 extern void udc_disconnect(void);
90 udc_disconnect();
91 }
92#endif
93 cleanup_before_linux();
94}
95
96int do_bootm_linux(int flag, int argc, char *argv[], bootm_headers_t *images)
wdenkc6097192002-11-03 00:24:07 +000097{
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +010098 bd_t *bd = gd->bd;
99 char *s;
100 int machid = bd->bi_arch_number;
John Rigbyab51f1e2010-10-13 13:57:36 -0600101 void (*kernel_entry)(int zero, int arch, uint params);
wdenk86765902003-12-06 23:55:10 +0000102
wdenkc6097192002-11-03 00:24:07 +0000103#ifdef CONFIG_CMDLINE_TAG
wdenk86765902003-12-06 23:55:10 +0000104 char *commandline = getenv ("bootargs");
wdenkc6097192002-11-03 00:24:07 +0000105#endif
106
Ilko Ilievbcbc8e42008-11-09 15:53:14 +0100107 if ((flag != 0) && (flag != BOOTM_STATE_OS_GO))
Kumar Gala18178bc2008-10-21 17:25:45 -0500108 return 1;
109
Uwe Kleine-König533937d2008-01-30 09:08:49 +0100110 s = getenv ("machid");
111 if (s) {
112 machid = simple_strtoul (s, NULL, 16);
113 printf ("Using machid 0x%x from environment\n", machid);
114 }
115
Heiko Schocher8a8ec532007-07-13 09:54:17 +0200116 show_boot_progress (15);
wdenk384ae022002-11-05 00:17:55 +0000117
John Rigbyab51f1e2010-10-13 13:57:36 -0600118#ifdef CONFIG_OF_LIBFDT
119 if (images->ft_len)
120 return bootm_linux_fdt(machid, images);
121#endif
122
123 kernel_entry = (void (*)(int, int, uint))images->ep;
124
wdenk86765902003-12-06 23:55:10 +0000125 debug ("## Transferring control to Linux (at address %08lx) ...\n",
John Rigbyab51f1e2010-10-13 13:57:36 -0600126 (ulong) kernel_entry);
wdenkc6097192002-11-03 00:24:07 +0000127
128#if defined (CONFIG_SETUP_MEMORY_TAGS) || \
129 defined (CONFIG_CMDLINE_TAG) || \
130 defined (CONFIG_INITRD_TAG) || \
wdenkbc01dd52004-01-02 16:05:07 +0000131 defined (CONFIG_SERIAL_TAG) || \
Minkyu Kang977e3b62010-06-18 19:31:10 +0900132 defined (CONFIG_REVISION_TAG)
wdenk86765902003-12-06 23:55:10 +0000133 setup_start_tag (bd);
wdenkbc01dd52004-01-02 16:05:07 +0000134#ifdef CONFIG_SERIAL_TAG
135 setup_serial_tag (&params);
136#endif
137#ifdef CONFIG_REVISION_TAG
138 setup_revision_tag (&params);
139#endif
wdenkc6097192002-11-03 00:24:07 +0000140#ifdef CONFIG_SETUP_MEMORY_TAGS
wdenk86765902003-12-06 23:55:10 +0000141 setup_memory_tags (bd);
wdenkc6097192002-11-03 00:24:07 +0000142#endif
143#ifdef CONFIG_CMDLINE_TAG
wdenk86765902003-12-06 23:55:10 +0000144 setup_commandline_tag (bd, commandline);
wdenkc6097192002-11-03 00:24:07 +0000145#endif
146#ifdef CONFIG_INITRD_TAG
Kumar Galafffb1432008-08-15 08:24:37 -0500147 if (images->rd_start && images->rd_end)
148 setup_initrd_tag (bd, images->rd_start, images->rd_end);
wdenkc6097192002-11-03 00:24:07 +0000149#endif
John Rigbyab51f1e2010-10-13 13:57:36 -0600150 setup_end_tag(bd);
wdenkc6097192002-11-03 00:24:07 +0000151#endif
152
John Rigbyab51f1e2010-10-13 13:57:36 -0600153 announce_and_cleanup();
wdenkc6097192002-11-03 00:24:07 +0000154
John Rigbyab51f1e2010-10-13 13:57:36 -0600155 kernel_entry(0, machid, bd->bi_boot_params);
156 /* does not return */
157
158 return 1;
159}
160
161#if defined(CONFIG_OF_LIBFDT)
162static int fixup_memory_node(void *blob)
163{
164 bd_t *bd = gd->bd;
165 int bank;
166 u64 start[CONFIG_NR_DRAM_BANKS];
167 u64 size[CONFIG_NR_DRAM_BANKS];
168
169 for (bank = 0; bank < CONFIG_NR_DRAM_BANKS; bank++) {
170 start[bank] = bd->bi_dram[bank].start;
171 size[bank] = bd->bi_dram[bank].size;
wdenk29e7f5a2004-03-12 00:14:09 +0000172 }
John Rigbyab51f1e2010-10-13 13:57:36 -0600173
174 return fdt_fixup_memory_banks(blob, start, size, CONFIG_NR_DRAM_BANKS);
175}
176
177static int bootm_linux_fdt(int machid, bootm_headers_t *images)
178{
179 ulong rd_len;
John Rigbyab51f1e2010-10-13 13:57:36 -0600180 void (*kernel_entry)(int zero, int dt_machid, void *dtblob);
181 ulong bootmap_base = getenv_bootm_low();
182 ulong of_size = images->ft_len;
183 char **of_flat_tree = &images->ft_addr;
184 ulong *initrd_start = &images->initrd_start;
185 ulong *initrd_end = &images->initrd_end;
186 struct lmb *lmb = &images->lmb;
187 int ret;
188
189 kernel_entry = (void (*)(int, int, void *))images->ep;
wdenk29e7f5a2004-03-12 00:14:09 +0000190
John Rigbyab51f1e2010-10-13 13:57:36 -0600191 rd_len = images->rd_end - images->rd_start;
192 ret = boot_ramdisk_high(lmb, images->rd_start, rd_len,
193 initrd_start, initrd_end);
194 if (ret)
195 return ret;
wdenkc6097192002-11-03 00:24:07 +0000196
John Rigbyab51f1e2010-10-13 13:57:36 -0600197 ret = boot_relocate_fdt(lmb, bootmap_base, of_flat_tree, &of_size);
198 if (ret)
199 return ret;
200
201 debug("## Transferring control to Linux (at address %08lx) ...\n",
202 (ulong) kernel_entry);
203
204 fdt_chosen(*of_flat_tree, 1);
205
206 fixup_memory_node(*of_flat_tree);
207
208 fdt_initrd(*of_flat_tree, *initrd_start, *initrd_end, 1);
209
210 announce_and_cleanup();
211
212 kernel_entry(0, machid, *of_flat_tree);
Marian Balakowiczdf8ff332008-03-12 10:33:00 +0100213 /* does not return */
Wolfgang Denk20ec3672008-09-08 23:26:22 +0200214
Kumar Gala48626aa2008-08-15 08:24:45 -0500215 return 1;
wdenkc6097192002-11-03 00:24:07 +0000216}
John Rigbyab51f1e2010-10-13 13:57:36 -0600217#endif
wdenkc6097192002-11-03 00:24:07 +0000218
219#if defined (CONFIG_SETUP_MEMORY_TAGS) || \
220 defined (CONFIG_CMDLINE_TAG) || \
221 defined (CONFIG_INITRD_TAG) || \
wdenkbc01dd52004-01-02 16:05:07 +0000222 defined (CONFIG_SERIAL_TAG) || \
Minkyu Kang977e3b62010-06-18 19:31:10 +0900223 defined (CONFIG_REVISION_TAG)
wdenk86765902003-12-06 23:55:10 +0000224static void setup_start_tag (bd_t *bd)
wdenkc6097192002-11-03 00:24:07 +0000225{
wdenk86765902003-12-06 23:55:10 +0000226 params = (struct tag *) bd->bi_boot_params;
wdenkc6097192002-11-03 00:24:07 +0000227
wdenk86765902003-12-06 23:55:10 +0000228 params->hdr.tag = ATAG_CORE;
229 params->hdr.size = tag_size (tag_core);
wdenkc6097192002-11-03 00:24:07 +0000230
wdenk86765902003-12-06 23:55:10 +0000231 params->u.core.flags = 0;
232 params->u.core.pagesize = 0;
233 params->u.core.rootdev = 0;
wdenkc6097192002-11-03 00:24:07 +0000234
wdenk86765902003-12-06 23:55:10 +0000235 params = tag_next (params);
wdenkc6097192002-11-03 00:24:07 +0000236}
237
238
wdenk699b13a2002-11-03 18:03:52 +0000239#ifdef CONFIG_SETUP_MEMORY_TAGS
wdenk86765902003-12-06 23:55:10 +0000240static void setup_memory_tags (bd_t *bd)
wdenkc6097192002-11-03 00:24:07 +0000241{
wdenk86765902003-12-06 23:55:10 +0000242 int i;
wdenkc6097192002-11-03 00:24:07 +0000243
wdenk86765902003-12-06 23:55:10 +0000244 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
245 params->hdr.tag = ATAG_MEM;
246 params->hdr.size = tag_size (tag_mem32);
wdenkc6097192002-11-03 00:24:07 +0000247
wdenk86765902003-12-06 23:55:10 +0000248 params->u.mem.start = bd->bi_dram[i].start;
249 params->u.mem.size = bd->bi_dram[i].size;
wdenkc6097192002-11-03 00:24:07 +0000250
wdenk86765902003-12-06 23:55:10 +0000251 params = tag_next (params);
252 }
wdenkc6097192002-11-03 00:24:07 +0000253}
wdenk86765902003-12-06 23:55:10 +0000254#endif /* CONFIG_SETUP_MEMORY_TAGS */
wdenkc6097192002-11-03 00:24:07 +0000255
256
wdenk86765902003-12-06 23:55:10 +0000257static void setup_commandline_tag (bd_t *bd, char *commandline)
wdenkc6097192002-11-03 00:24:07 +0000258{
wdenk86765902003-12-06 23:55:10 +0000259 char *p;
wdenkc6097192002-11-03 00:24:07 +0000260
wdenkf16b5162004-03-23 21:43:07 +0000261 if (!commandline)
262 return;
263
wdenk86765902003-12-06 23:55:10 +0000264 /* eat leading white space */
265 for (p = commandline; *p == ' '; p++);
wdenkc6097192002-11-03 00:24:07 +0000266
wdenk86765902003-12-06 23:55:10 +0000267 /* skip non-existent command lines so the kernel will still
268 * use its default command line.
269 */
270 if (*p == '\0')
271 return;
wdenkc6097192002-11-03 00:24:07 +0000272
wdenk86765902003-12-06 23:55:10 +0000273 params->hdr.tag = ATAG_CMDLINE;
274 params->hdr.size =
275 (sizeof (struct tag_header) + strlen (p) + 1 + 4) >> 2;
wdenkc6097192002-11-03 00:24:07 +0000276
wdenk86765902003-12-06 23:55:10 +0000277 strcpy (params->u.cmdline.cmdline, p);
wdenkc6097192002-11-03 00:24:07 +0000278
wdenk86765902003-12-06 23:55:10 +0000279 params = tag_next (params);
wdenkc6097192002-11-03 00:24:07 +0000280}
281
282
wdenk699b13a2002-11-03 18:03:52 +0000283#ifdef CONFIG_INITRD_TAG
wdenk86765902003-12-06 23:55:10 +0000284static void setup_initrd_tag (bd_t *bd, ulong initrd_start, ulong initrd_end)
wdenkc6097192002-11-03 00:24:07 +0000285{
wdenk86765902003-12-06 23:55:10 +0000286 /* an ATAG_INITRD node tells the kernel where the compressed
287 * ramdisk can be found. ATAG_RDIMG is a better name, actually.
288 */
wdenk19c8fb72004-04-18 22:26:17 +0000289 params->hdr.tag = ATAG_INITRD2;
wdenk86765902003-12-06 23:55:10 +0000290 params->hdr.size = tag_size (tag_initrd);
wdenkc6097192002-11-03 00:24:07 +0000291
wdenk86765902003-12-06 23:55:10 +0000292 params->u.initrd.start = initrd_start;
293 params->u.initrd.size = initrd_end - initrd_start;
wdenkc6097192002-11-03 00:24:07 +0000294
wdenk86765902003-12-06 23:55:10 +0000295 params = tag_next (params);
wdenkc6097192002-11-03 00:24:07 +0000296}
wdenk86765902003-12-06 23:55:10 +0000297#endif /* CONFIG_INITRD_TAG */
wdenkc6097192002-11-03 00:24:07 +0000298
wdenka21ad7b2005-05-19 22:39:42 +0000299#ifdef CONFIG_SERIAL_TAG
300void setup_serial_tag (struct tag **tmp)
301{
302 struct tag *params = *tmp;
303 struct tag_serialnr serialnr;
304 void get_board_serial(struct tag_serialnr *serialnr);
305
306 get_board_serial(&serialnr);
307 params->hdr.tag = ATAG_SERIAL;
308 params->hdr.size = tag_size (tag_serialnr);
309 params->u.serialnr.low = serialnr.low;
310 params->u.serialnr.high= serialnr.high;
311 params = tag_next (params);
312 *tmp = params;
313}
314#endif
315
wdenkcb99da52005-01-12 00:15:14 +0000316#ifdef CONFIG_REVISION_TAG
317void setup_revision_tag(struct tag **in_params)
318{
319 u32 rev = 0;
wdenkcb99da52005-01-12 00:15:14 +0000320 u32 get_board_rev(void);
321
322 rev = get_board_rev();
wdenkcb99da52005-01-12 00:15:14 +0000323 params->hdr.tag = ATAG_REVISION;
324 params->hdr.size = tag_size (tag_revision);
325 params->u.revision.rev = rev;
326 params = tag_next (params);
327}
328#endif /* CONFIG_REVISION_TAG */
329
wdenk86765902003-12-06 23:55:10 +0000330static void setup_end_tag (bd_t *bd)
wdenkc6097192002-11-03 00:24:07 +0000331{
wdenk86765902003-12-06 23:55:10 +0000332 params->hdr.tag = ATAG_NONE;
333 params->hdr.size = 0;
wdenkc6097192002-11-03 00:24:07 +0000334}
Po-Yu Chuange6b836e2011-02-15 22:54:35 +0000335#endif /* CONFIG_SETUP_MEMORY_TAGS || CONFIG_CMDLINE_TAG || CONFIG_INITRD_TAG */
wdenkc6097192002-11-03 00:24:07 +0000336
John Rigbyab51f1e2010-10-13 13:57:36 -0600337static ulong get_sp(void)
338{
339 ulong ret;
340
341 asm("mov %0, sp" : "=r"(ret) : );
342 return ret;
343}