blob: 1bd273085685ba9c4d424e7ac761a6b8bde5828d [file] [log] [blame]
Simon Schwarz6a597b92012-03-15 04:01:45 +00001/* Copyright (C) 2011
2 * Corscience GmbH & Co. KG - Simon Schwarz <schwarz@corscience.de>
3 * - Added prep subcommand support
4 * - Reorganized source - modeled after powerpc version
5 *
wdenkc6097192002-11-03 00:24:07 +00006 * (C) Copyright 2002
7 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
8 * Marius Groeger <mgroeger@sysgo.de>
9 *
10 * Copyright (C) 2001 Erik Mouw (J.A.K.Mouw@its.tudelft.nl)
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
wdenk29e7f5a2004-03-12 00:14:09 +000019 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
wdenkc6097192002-11-03 00:24:07 +000020 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
wdenk29e7f5a2004-03-12 00:14:09 +000024 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
wdenkc6097192002-11-03 00:24:07 +000025 *
26 */
27
28#include <common.h>
29#include <command.h>
wdenkc6097192002-11-03 00:24:07 +000030#include <image.h>
Jean-Christophe PLAGNIOL-VILLARD6bb94492009-04-04 12:49:11 +020031#include <u-boot/zlib.h>
wdenkc6097192002-11-03 00:24:07 +000032#include <asm/byteorder.h>
John Rigbyab51f1e2010-10-13 13:57:36 -060033#include <fdt.h>
34#include <libfdt.h>
35#include <fdt_support.h>
Simon Schwarz6a597b92012-03-15 04:01:45 +000036#include <asm/bootm.h>
Pali Rohár5ebe4f12012-10-19 02:00:04 +000037#include <linux/compiler.h>
wdenkc6097192002-11-03 00:24:07 +000038
Wolfgang Denk6405a152006-03-31 18:32:53 +020039DECLARE_GLOBAL_DATA_PTR;
40
Simon Schwarz6a597b92012-03-15 04:01:45 +000041#if defined(CONFIG_SETUP_MEMORY_TAGS) || \
42 defined(CONFIG_CMDLINE_TAG) || \
43 defined(CONFIG_INITRD_TAG) || \
44 defined(CONFIG_SERIAL_TAG) || \
45 defined(CONFIG_REVISION_TAG)
wdenkc6097192002-11-03 00:24:07 +000046static struct tag *params;
John Rigbyab51f1e2010-10-13 13:57:36 -060047#endif
48
Simon Schwarz6a597b92012-03-15 04:01:45 +000049static ulong get_sp(void)
50{
51 ulong ret;
52
53 asm("mov %0, sp" : "=r"(ret) : );
54 return ret;
55}
56
John Rigbyab51f1e2010-10-13 13:57:36 -060057void arch_lmb_reserve(struct lmb *lmb)
58{
59 ulong sp;
60
61 /*
62 * Booting a (Linux) kernel image
63 *
64 * Allocate space for command line and board info - the
65 * address should be as high as possible within the reach of
66 * the kernel (see CONFIG_SYS_BOOTMAPSZ settings), but in unused
67 * memory, which means far enough below the current stack
68 * pointer.
69 */
70 sp = get_sp();
71 debug("## Current stack ends at 0x%08lx ", sp);
72
Rob Herring13137f32012-06-28 03:54:11 +000073 /* adjust sp by 4K to be safe */
74 sp -= 4096;
John Rigbyab51f1e2010-10-13 13:57:36 -060075 lmb_reserve(lmb, sp,
76 gd->bd->bi_dram[0].start + gd->bd->bi_dram[0].size - sp);
77}
78
John Rigbyab51f1e2010-10-13 13:57:36 -060079#ifdef CONFIG_OF_LIBFDT
John Rigbyab51f1e2010-10-13 13:57:36 -060080static int fixup_memory_node(void *blob)
81{
82 bd_t *bd = gd->bd;
83 int bank;
84 u64 start[CONFIG_NR_DRAM_BANKS];
85 u64 size[CONFIG_NR_DRAM_BANKS];
86
87 for (bank = 0; bank < CONFIG_NR_DRAM_BANKS; bank++) {
88 start[bank] = bd->bi_dram[bank].start;
89 size[bank] = bd->bi_dram[bank].size;
wdenk29e7f5a2004-03-12 00:14:09 +000090 }
John Rigbyab51f1e2010-10-13 13:57:36 -060091
92 return fdt_fixup_memory_banks(blob, start, size, CONFIG_NR_DRAM_BANKS);
93}
Simon Schwarz6a597b92012-03-15 04:01:45 +000094#endif
John Rigbyab51f1e2010-10-13 13:57:36 -060095
Simon Schwarz6a597b92012-03-15 04:01:45 +000096static void announce_and_cleanup(void)
John Rigbyab51f1e2010-10-13 13:57:36 -060097{
Simon Schwarz6a597b92012-03-15 04:01:45 +000098 printf("\nStarting kernel ...\n\n");
99 bootstage_mark_name(BOOTSTAGE_ID_BOOTM_HANDOFF, "start_kernel");
Simon Glass0fa36a42012-09-28 08:56:37 +0000100#ifdef CONFIG_BOOTSTAGE_FDT
101 bootstage_fdt_add_report();
102#endif
Simon Schwarz6a597b92012-03-15 04:01:45 +0000103#ifdef CONFIG_BOOTSTAGE_REPORT
104 bootstage_report();
105#endif
Wolfgang Denk20ec3672008-09-08 23:26:22 +0200106
Simon Schwarz6a597b92012-03-15 04:01:45 +0000107#ifdef CONFIG_USB_DEVICE
108 udc_disconnect();
John Rigbyab51f1e2010-10-13 13:57:36 -0600109#endif
Simon Schwarz6a597b92012-03-15 04:01:45 +0000110 cleanup_before_linux();
111}
wdenkc6097192002-11-03 00:24:07 +0000112
Simon Schwarz6a597b92012-03-15 04:01:45 +0000113#if defined(CONFIG_SETUP_MEMORY_TAGS) || \
114 defined(CONFIG_CMDLINE_TAG) || \
115 defined(CONFIG_INITRD_TAG) || \
116 defined(CONFIG_SERIAL_TAG) || \
117 defined(CONFIG_REVISION_TAG)
wdenk86765902003-12-06 23:55:10 +0000118static void setup_start_tag (bd_t *bd)
wdenkc6097192002-11-03 00:24:07 +0000119{
Simon Schwarz6a597b92012-03-15 04:01:45 +0000120 params = (struct tag *)bd->bi_boot_params;
wdenkc6097192002-11-03 00:24:07 +0000121
wdenk86765902003-12-06 23:55:10 +0000122 params->hdr.tag = ATAG_CORE;
123 params->hdr.size = tag_size (tag_core);
wdenkc6097192002-11-03 00:24:07 +0000124
wdenk86765902003-12-06 23:55:10 +0000125 params->u.core.flags = 0;
126 params->u.core.pagesize = 0;
127 params->u.core.rootdev = 0;
wdenkc6097192002-11-03 00:24:07 +0000128
wdenk86765902003-12-06 23:55:10 +0000129 params = tag_next (params);
wdenkc6097192002-11-03 00:24:07 +0000130}
Simon Schwarz6a597b92012-03-15 04:01:45 +0000131#endif
wdenkc6097192002-11-03 00:24:07 +0000132
wdenk699b13a2002-11-03 18:03:52 +0000133#ifdef CONFIG_SETUP_MEMORY_TAGS
Simon Schwarz6a597b92012-03-15 04:01:45 +0000134static void setup_memory_tags(bd_t *bd)
wdenkc6097192002-11-03 00:24:07 +0000135{
wdenk86765902003-12-06 23:55:10 +0000136 int i;
wdenkc6097192002-11-03 00:24:07 +0000137
wdenk86765902003-12-06 23:55:10 +0000138 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
139 params->hdr.tag = ATAG_MEM;
140 params->hdr.size = tag_size (tag_mem32);
wdenkc6097192002-11-03 00:24:07 +0000141
wdenk86765902003-12-06 23:55:10 +0000142 params->u.mem.start = bd->bi_dram[i].start;
143 params->u.mem.size = bd->bi_dram[i].size;
wdenkc6097192002-11-03 00:24:07 +0000144
wdenk86765902003-12-06 23:55:10 +0000145 params = tag_next (params);
146 }
wdenkc6097192002-11-03 00:24:07 +0000147}
Simon Schwarz6a597b92012-03-15 04:01:45 +0000148#endif
wdenkc6097192002-11-03 00:24:07 +0000149
Simon Schwarz6a597b92012-03-15 04:01:45 +0000150#ifdef CONFIG_CMDLINE_TAG
151static void setup_commandline_tag(bd_t *bd, char *commandline)
wdenkc6097192002-11-03 00:24:07 +0000152{
wdenk86765902003-12-06 23:55:10 +0000153 char *p;
wdenkc6097192002-11-03 00:24:07 +0000154
wdenkf16b5162004-03-23 21:43:07 +0000155 if (!commandline)
156 return;
157
wdenk86765902003-12-06 23:55:10 +0000158 /* eat leading white space */
159 for (p = commandline; *p == ' '; p++);
wdenkc6097192002-11-03 00:24:07 +0000160
wdenk86765902003-12-06 23:55:10 +0000161 /* skip non-existent command lines so the kernel will still
162 * use its default command line.
163 */
164 if (*p == '\0')
165 return;
wdenkc6097192002-11-03 00:24:07 +0000166
wdenk86765902003-12-06 23:55:10 +0000167 params->hdr.tag = ATAG_CMDLINE;
168 params->hdr.size =
169 (sizeof (struct tag_header) + strlen (p) + 1 + 4) >> 2;
wdenkc6097192002-11-03 00:24:07 +0000170
wdenk86765902003-12-06 23:55:10 +0000171 strcpy (params->u.cmdline.cmdline, p);
wdenkc6097192002-11-03 00:24:07 +0000172
wdenk86765902003-12-06 23:55:10 +0000173 params = tag_next (params);
wdenkc6097192002-11-03 00:24:07 +0000174}
Simon Schwarz6a597b92012-03-15 04:01:45 +0000175#endif
wdenkc6097192002-11-03 00:24:07 +0000176
wdenk699b13a2002-11-03 18:03:52 +0000177#ifdef CONFIG_INITRD_TAG
Simon Schwarz6a597b92012-03-15 04:01:45 +0000178static void setup_initrd_tag(bd_t *bd, ulong initrd_start, ulong initrd_end)
wdenkc6097192002-11-03 00:24:07 +0000179{
wdenk86765902003-12-06 23:55:10 +0000180 /* an ATAG_INITRD node tells the kernel where the compressed
181 * ramdisk can be found. ATAG_RDIMG is a better name, actually.
182 */
wdenk19c8fb72004-04-18 22:26:17 +0000183 params->hdr.tag = ATAG_INITRD2;
wdenk86765902003-12-06 23:55:10 +0000184 params->hdr.size = tag_size (tag_initrd);
wdenkc6097192002-11-03 00:24:07 +0000185
wdenk86765902003-12-06 23:55:10 +0000186 params->u.initrd.start = initrd_start;
187 params->u.initrd.size = initrd_end - initrd_start;
wdenkc6097192002-11-03 00:24:07 +0000188
wdenk86765902003-12-06 23:55:10 +0000189 params = tag_next (params);
wdenkc6097192002-11-03 00:24:07 +0000190}
Simon Schwarz6a597b92012-03-15 04:01:45 +0000191#endif
wdenkc6097192002-11-03 00:24:07 +0000192
wdenka21ad7b2005-05-19 22:39:42 +0000193#ifdef CONFIG_SERIAL_TAG
Simon Schwarz6a597b92012-03-15 04:01:45 +0000194void setup_serial_tag(struct tag **tmp)
wdenka21ad7b2005-05-19 22:39:42 +0000195{
196 struct tag *params = *tmp;
197 struct tag_serialnr serialnr;
198 void get_board_serial(struct tag_serialnr *serialnr);
199
200 get_board_serial(&serialnr);
201 params->hdr.tag = ATAG_SERIAL;
202 params->hdr.size = tag_size (tag_serialnr);
203 params->u.serialnr.low = serialnr.low;
204 params->u.serialnr.high= serialnr.high;
205 params = tag_next (params);
206 *tmp = params;
207}
208#endif
209
wdenkcb99da52005-01-12 00:15:14 +0000210#ifdef CONFIG_REVISION_TAG
211void setup_revision_tag(struct tag **in_params)
212{
213 u32 rev = 0;
wdenkcb99da52005-01-12 00:15:14 +0000214 u32 get_board_rev(void);
215
216 rev = get_board_rev();
wdenkcb99da52005-01-12 00:15:14 +0000217 params->hdr.tag = ATAG_REVISION;
218 params->hdr.size = tag_size (tag_revision);
219 params->u.revision.rev = rev;
220 params = tag_next (params);
221}
Simon Schwarz6a597b92012-03-15 04:01:45 +0000222#endif
wdenkcb99da52005-01-12 00:15:14 +0000223
Simon Schwarz6a597b92012-03-15 04:01:45 +0000224#if defined(CONFIG_SETUP_MEMORY_TAGS) || \
225 defined(CONFIG_CMDLINE_TAG) || \
226 defined(CONFIG_INITRD_TAG) || \
227 defined(CONFIG_SERIAL_TAG) || \
228 defined(CONFIG_REVISION_TAG)
229static void setup_end_tag(bd_t *bd)
wdenkc6097192002-11-03 00:24:07 +0000230{
wdenk86765902003-12-06 23:55:10 +0000231 params->hdr.tag = ATAG_NONE;
232 params->hdr.size = 0;
wdenkc6097192002-11-03 00:24:07 +0000233}
Simon Schwarz6a597b92012-03-15 04:01:45 +0000234#endif
wdenkc6097192002-11-03 00:24:07 +0000235
Simon Schwarz6a597b92012-03-15 04:01:45 +0000236#ifdef CONFIG_OF_LIBFDT
237static int create_fdt(bootm_headers_t *images)
John Rigbyab51f1e2010-10-13 13:57:36 -0600238{
Simon Schwarz6a597b92012-03-15 04:01:45 +0000239 ulong of_size = images->ft_len;
240 char **of_flat_tree = &images->ft_addr;
241 ulong *initrd_start = &images->initrd_start;
242 ulong *initrd_end = &images->initrd_end;
243 struct lmb *lmb = &images->lmb;
244 ulong rd_len;
245 int ret;
John Rigbyab51f1e2010-10-13 13:57:36 -0600246
Simon Schwarz6a597b92012-03-15 04:01:45 +0000247 debug("using: FDT\n");
248
249 boot_fdt_add_mem_rsv_regions(lmb, *of_flat_tree);
250
251 rd_len = images->rd_end - images->rd_start;
252 ret = boot_ramdisk_high(lmb, images->rd_start, rd_len,
253 initrd_start, initrd_end);
254 if (ret)
255 return ret;
256
257 ret = boot_relocate_fdt(lmb, of_flat_tree, &of_size);
258 if (ret)
259 return ret;
260
261 fdt_chosen(*of_flat_tree, 1);
262 fixup_memory_node(*of_flat_tree);
Stephen Warren6d32e642012-04-19 11:09:49 +0000263 fdt_fixup_ethernet(*of_flat_tree);
Simon Schwarz6a597b92012-03-15 04:01:45 +0000264 fdt_initrd(*of_flat_tree, *initrd_start, *initrd_end, 1);
Joe Hershbergerceea0772012-08-17 10:28:50 +0000265#ifdef CONFIG_OF_BOARD_SETUP
266 ft_board_setup(*of_flat_tree, gd->bd);
267#endif
Simon Schwarz6a597b92012-03-15 04:01:45 +0000268
269 return 0;
270}
271#endif
272
Pali Rohár5ebe4f12012-10-19 02:00:04 +0000273__weak void setup_board_tags(struct tag **in_params) {}
274
Simon Schwarz6a597b92012-03-15 04:01:45 +0000275/* Subcommand: PREP */
276static void boot_prep_linux(bootm_headers_t *images)
277{
278#ifdef CONFIG_CMDLINE_TAG
279 char *commandline = getenv("bootargs");
280#endif
281
282#ifdef CONFIG_OF_LIBFDT
283 if (images->ft_len) {
284 debug("using: FDT\n");
285 if (create_fdt(images)) {
286 printf("FDT creation failed! hanging...");
287 hang();
288 }
289 } else
290#endif
291 {
292#if defined(CONFIG_SETUP_MEMORY_TAGS) || \
293 defined(CONFIG_CMDLINE_TAG) || \
294 defined(CONFIG_INITRD_TAG) || \
295 defined(CONFIG_SERIAL_TAG) || \
296 defined(CONFIG_REVISION_TAG)
297 debug("using: ATAGS\n");
298 setup_start_tag(gd->bd);
299#ifdef CONFIG_SERIAL_TAG
300 setup_serial_tag(&params);
301#endif
302#ifdef CONFIG_CMDLINE_TAG
303 setup_commandline_tag(gd->bd, commandline);
304#endif
305#ifdef CONFIG_REVISION_TAG
306 setup_revision_tag(&params);
307#endif
308#ifdef CONFIG_SETUP_MEMORY_TAGS
309 setup_memory_tags(gd->bd);
310#endif
311#ifdef CONFIG_INITRD_TAG
312 if (images->rd_start && images->rd_end)
313 setup_initrd_tag(gd->bd, images->rd_start,
314 images->rd_end);
315#endif
Pali Rohár5ebe4f12012-10-19 02:00:04 +0000316 setup_board_tags(&params);
Simon Schwarz6a597b92012-03-15 04:01:45 +0000317 setup_end_tag(gd->bd);
318#else /* all tags */
319 printf("FDT and ATAGS support not compiled in - hanging\n");
320 hang();
321#endif /* all tags */
322 }
323}
324
325/* Subcommand: GO */
326static void boot_jump_linux(bootm_headers_t *images)
327{
328 unsigned long machid = gd->bd->bi_arch_number;
329 char *s;
330 void (*kernel_entry)(int zero, int arch, uint params);
Stephen Warrenc570fba2012-04-19 11:34:00 +0000331 unsigned long r2;
Simon Schwarz6a597b92012-03-15 04:01:45 +0000332
333 kernel_entry = (void (*)(int, int, uint))images->ep;
334
335 s = getenv("machid");
336 if (s) {
337 strict_strtoul(s, 16, &machid);
338 printf("Using machid 0x%lx from environment\n", machid);
339 }
340
341 debug("## Transferring control to Linux (at address %08lx)" \
342 "...\n", (ulong) kernel_entry);
343 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
344 announce_and_cleanup();
Stephen Warrenc570fba2012-04-19 11:34:00 +0000345
346#ifdef CONFIG_OF_LIBFDT
347 if (images->ft_len)
348 r2 = (unsigned long)images->ft_addr;
349 else
350#endif
351 r2 = gd->bd->bi_boot_params;
352
353 kernel_entry(0, machid, r2);
Simon Schwarz6a597b92012-03-15 04:01:45 +0000354}
355
356/* Main Entry point for arm bootm implementation
357 *
358 * Modeled after the powerpc implementation
359 * DIFFERENCE: Instead of calling prep and go at the end
360 * they are called if subcommand is equal 0.
361 */
362int do_bootm_linux(int flag, int argc, char *argv[], bootm_headers_t *images)
363{
364 /* No need for those on ARM */
365 if (flag & BOOTM_STATE_OS_BD_T || flag & BOOTM_STATE_OS_CMDLINE)
366 return -1;
367
368 if (flag & BOOTM_STATE_OS_PREP) {
369 boot_prep_linux(images);
370 return 0;
371 }
372
373 if (flag & BOOTM_STATE_OS_GO) {
374 boot_jump_linux(images);
375 return 0;
376 }
377
378 boot_prep_linux(images);
379 boot_jump_linux(images);
380 return 0;
John Rigbyab51f1e2010-10-13 13:57:36 -0600381}
Marek Vasutcf41a9b2012-03-14 21:52:45 +0000382
383#ifdef CONFIG_CMD_BOOTZ
384
385struct zimage_header {
386 uint32_t code[9];
387 uint32_t zi_magic;
388 uint32_t zi_start;
389 uint32_t zi_end;
390};
391
392#define LINUX_ARM_ZIMAGE_MAGIC 0x016f2818
393
394int bootz_setup(void *image, void **start, void **end)
395{
396 struct zimage_header *zi = (struct zimage_header *)image;
397
398 if (zi->zi_magic != LINUX_ARM_ZIMAGE_MAGIC) {
399 puts("Bad Linux ARM zImage magic!\n");
400 return 1;
401 }
402
403 *start = (void *)zi->zi_start;
404 *end = (void *)zi->zi_end;
405
406 debug("Kernel image @ 0x%08x [ 0x%08x - 0x%08x ]\n",
407 (uint32_t)image, (uint32_t)*start, (uint32_t)*end);
408
409 return 0;
410}
411#endif /* CONFIG_CMD_BOOTZ */