blob: f3b30c57a37cd03b0c368dcd73aff49fc0b29478 [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 <libfdt.h>
34#include <fdt_support.h>
Simon Schwarz6a597b92012-03-15 04:01:45 +000035#include <asm/bootm.h>
Pali Rohár5ebe4f12012-10-19 02:00:04 +000036#include <linux/compiler.h>
wdenkc6097192002-11-03 00:24:07 +000037
Wolfgang Denk6405a152006-03-31 18:32:53 +020038DECLARE_GLOBAL_DATA_PTR;
39
Simon Schwarz6a597b92012-03-15 04:01:45 +000040#if defined(CONFIG_SETUP_MEMORY_TAGS) || \
41 defined(CONFIG_CMDLINE_TAG) || \
42 defined(CONFIG_INITRD_TAG) || \
43 defined(CONFIG_SERIAL_TAG) || \
44 defined(CONFIG_REVISION_TAG)
wdenkc6097192002-11-03 00:24:07 +000045static struct tag *params;
John Rigbyab51f1e2010-10-13 13:57:36 -060046#endif
47
Simon Schwarz6a597b92012-03-15 04:01:45 +000048static ulong get_sp(void)
49{
50 ulong ret;
51
52 asm("mov %0, sp" : "=r"(ret) : );
53 return ret;
54}
55
John Rigbyab51f1e2010-10-13 13:57:36 -060056void arch_lmb_reserve(struct lmb *lmb)
57{
58 ulong sp;
59
60 /*
61 * Booting a (Linux) kernel image
62 *
63 * Allocate space for command line and board info - the
64 * address should be as high as possible within the reach of
65 * the kernel (see CONFIG_SYS_BOOTMAPSZ settings), but in unused
66 * memory, which means far enough below the current stack
67 * pointer.
68 */
69 sp = get_sp();
70 debug("## Current stack ends at 0x%08lx ", sp);
71
Rob Herring13137f32012-06-28 03:54:11 +000072 /* adjust sp by 4K to be safe */
73 sp -= 4096;
John Rigbyab51f1e2010-10-13 13:57:36 -060074 lmb_reserve(lmb, sp,
75 gd->bd->bi_dram[0].start + gd->bd->bi_dram[0].size - sp);
76}
77
John Rigbyab51f1e2010-10-13 13:57:36 -060078#ifdef CONFIG_OF_LIBFDT
John Rigbyab51f1e2010-10-13 13:57:36 -060079static int fixup_memory_node(void *blob)
80{
81 bd_t *bd = gd->bd;
82 int bank;
83 u64 start[CONFIG_NR_DRAM_BANKS];
84 u64 size[CONFIG_NR_DRAM_BANKS];
85
86 for (bank = 0; bank < CONFIG_NR_DRAM_BANKS; bank++) {
87 start[bank] = bd->bi_dram[bank].start;
88 size[bank] = bd->bi_dram[bank].size;
wdenk29e7f5a2004-03-12 00:14:09 +000089 }
John Rigbyab51f1e2010-10-13 13:57:36 -060090
91 return fdt_fixup_memory_banks(blob, start, size, CONFIG_NR_DRAM_BANKS);
92}
Simon Schwarz6a597b92012-03-15 04:01:45 +000093#endif
John Rigbyab51f1e2010-10-13 13:57:36 -060094
Simon Schwarz6a597b92012-03-15 04:01:45 +000095static void announce_and_cleanup(void)
John Rigbyab51f1e2010-10-13 13:57:36 -060096{
Simon Schwarz6a597b92012-03-15 04:01:45 +000097 printf("\nStarting kernel ...\n\n");
98 bootstage_mark_name(BOOTSTAGE_ID_BOOTM_HANDOFF, "start_kernel");
Simon Glass0fa36a42012-09-28 08:56:37 +000099#ifdef CONFIG_BOOTSTAGE_FDT
100 bootstage_fdt_add_report();
101#endif
Simon Schwarz6a597b92012-03-15 04:01:45 +0000102#ifdef CONFIG_BOOTSTAGE_REPORT
103 bootstage_report();
104#endif
Wolfgang Denk20ec3672008-09-08 23:26:22 +0200105
Simon Schwarz6a597b92012-03-15 04:01:45 +0000106#ifdef CONFIG_USB_DEVICE
107 udc_disconnect();
John Rigbyab51f1e2010-10-13 13:57:36 -0600108#endif
Simon Schwarz6a597b92012-03-15 04:01:45 +0000109 cleanup_before_linux();
110}
wdenkc6097192002-11-03 00:24:07 +0000111
Simon Schwarz6a597b92012-03-15 04:01:45 +0000112#if defined(CONFIG_SETUP_MEMORY_TAGS) || \
113 defined(CONFIG_CMDLINE_TAG) || \
114 defined(CONFIG_INITRD_TAG) || \
115 defined(CONFIG_SERIAL_TAG) || \
116 defined(CONFIG_REVISION_TAG)
wdenk86765902003-12-06 23:55:10 +0000117static void setup_start_tag (bd_t *bd)
wdenkc6097192002-11-03 00:24:07 +0000118{
Simon Schwarz6a597b92012-03-15 04:01:45 +0000119 params = (struct tag *)bd->bi_boot_params;
wdenkc6097192002-11-03 00:24:07 +0000120
wdenk86765902003-12-06 23:55:10 +0000121 params->hdr.tag = ATAG_CORE;
122 params->hdr.size = tag_size (tag_core);
wdenkc6097192002-11-03 00:24:07 +0000123
wdenk86765902003-12-06 23:55:10 +0000124 params->u.core.flags = 0;
125 params->u.core.pagesize = 0;
126 params->u.core.rootdev = 0;
wdenkc6097192002-11-03 00:24:07 +0000127
wdenk86765902003-12-06 23:55:10 +0000128 params = tag_next (params);
wdenkc6097192002-11-03 00:24:07 +0000129}
Simon Schwarz6a597b92012-03-15 04:01:45 +0000130#endif
wdenkc6097192002-11-03 00:24:07 +0000131
wdenk699b13a2002-11-03 18:03:52 +0000132#ifdef CONFIG_SETUP_MEMORY_TAGS
Simon Schwarz6a597b92012-03-15 04:01:45 +0000133static void setup_memory_tags(bd_t *bd)
wdenkc6097192002-11-03 00:24:07 +0000134{
wdenk86765902003-12-06 23:55:10 +0000135 int i;
wdenkc6097192002-11-03 00:24:07 +0000136
wdenk86765902003-12-06 23:55:10 +0000137 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
138 params->hdr.tag = ATAG_MEM;
139 params->hdr.size = tag_size (tag_mem32);
wdenkc6097192002-11-03 00:24:07 +0000140
wdenk86765902003-12-06 23:55:10 +0000141 params->u.mem.start = bd->bi_dram[i].start;
142 params->u.mem.size = bd->bi_dram[i].size;
wdenkc6097192002-11-03 00:24:07 +0000143
wdenk86765902003-12-06 23:55:10 +0000144 params = tag_next (params);
145 }
wdenkc6097192002-11-03 00:24:07 +0000146}
Simon Schwarz6a597b92012-03-15 04:01:45 +0000147#endif
wdenkc6097192002-11-03 00:24:07 +0000148
Simon Schwarz6a597b92012-03-15 04:01:45 +0000149#ifdef CONFIG_CMDLINE_TAG
150static void setup_commandline_tag(bd_t *bd, char *commandline)
wdenkc6097192002-11-03 00:24:07 +0000151{
wdenk86765902003-12-06 23:55:10 +0000152 char *p;
wdenkc6097192002-11-03 00:24:07 +0000153
wdenkf16b5162004-03-23 21:43:07 +0000154 if (!commandline)
155 return;
156
wdenk86765902003-12-06 23:55:10 +0000157 /* eat leading white space */
158 for (p = commandline; *p == ' '; p++);
wdenkc6097192002-11-03 00:24:07 +0000159
wdenk86765902003-12-06 23:55:10 +0000160 /* skip non-existent command lines so the kernel will still
161 * use its default command line.
162 */
163 if (*p == '\0')
164 return;
wdenkc6097192002-11-03 00:24:07 +0000165
wdenk86765902003-12-06 23:55:10 +0000166 params->hdr.tag = ATAG_CMDLINE;
167 params->hdr.size =
168 (sizeof (struct tag_header) + strlen (p) + 1 + 4) >> 2;
wdenkc6097192002-11-03 00:24:07 +0000169
wdenk86765902003-12-06 23:55:10 +0000170 strcpy (params->u.cmdline.cmdline, p);
wdenkc6097192002-11-03 00:24:07 +0000171
wdenk86765902003-12-06 23:55:10 +0000172 params = tag_next (params);
wdenkc6097192002-11-03 00:24:07 +0000173}
Simon Schwarz6a597b92012-03-15 04:01:45 +0000174#endif
wdenkc6097192002-11-03 00:24:07 +0000175
wdenk699b13a2002-11-03 18:03:52 +0000176#ifdef CONFIG_INITRD_TAG
Simon Schwarz6a597b92012-03-15 04:01:45 +0000177static void setup_initrd_tag(bd_t *bd, ulong initrd_start, ulong initrd_end)
wdenkc6097192002-11-03 00:24:07 +0000178{
wdenk86765902003-12-06 23:55:10 +0000179 /* an ATAG_INITRD node tells the kernel where the compressed
180 * ramdisk can be found. ATAG_RDIMG is a better name, actually.
181 */
wdenk19c8fb72004-04-18 22:26:17 +0000182 params->hdr.tag = ATAG_INITRD2;
wdenk86765902003-12-06 23:55:10 +0000183 params->hdr.size = tag_size (tag_initrd);
wdenkc6097192002-11-03 00:24:07 +0000184
wdenk86765902003-12-06 23:55:10 +0000185 params->u.initrd.start = initrd_start;
186 params->u.initrd.size = initrd_end - initrd_start;
wdenkc6097192002-11-03 00:24:07 +0000187
wdenk86765902003-12-06 23:55:10 +0000188 params = tag_next (params);
wdenkc6097192002-11-03 00:24:07 +0000189}
Simon Schwarz6a597b92012-03-15 04:01:45 +0000190#endif
wdenkc6097192002-11-03 00:24:07 +0000191
wdenka21ad7b2005-05-19 22:39:42 +0000192#ifdef CONFIG_SERIAL_TAG
Simon Schwarz6a597b92012-03-15 04:01:45 +0000193void setup_serial_tag(struct tag **tmp)
wdenka21ad7b2005-05-19 22:39:42 +0000194{
195 struct tag *params = *tmp;
196 struct tag_serialnr serialnr;
197 void get_board_serial(struct tag_serialnr *serialnr);
198
199 get_board_serial(&serialnr);
200 params->hdr.tag = ATAG_SERIAL;
201 params->hdr.size = tag_size (tag_serialnr);
202 params->u.serialnr.low = serialnr.low;
203 params->u.serialnr.high= serialnr.high;
204 params = tag_next (params);
205 *tmp = params;
206}
207#endif
208
wdenkcb99da52005-01-12 00:15:14 +0000209#ifdef CONFIG_REVISION_TAG
210void setup_revision_tag(struct tag **in_params)
211{
212 u32 rev = 0;
wdenkcb99da52005-01-12 00:15:14 +0000213 u32 get_board_rev(void);
214
215 rev = get_board_rev();
wdenkcb99da52005-01-12 00:15:14 +0000216 params->hdr.tag = ATAG_REVISION;
217 params->hdr.size = tag_size (tag_revision);
218 params->u.revision.rev = rev;
219 params = tag_next (params);
220}
Simon Schwarz6a597b92012-03-15 04:01:45 +0000221#endif
wdenkcb99da52005-01-12 00:15:14 +0000222
Simon Schwarz6a597b92012-03-15 04:01:45 +0000223#if defined(CONFIG_SETUP_MEMORY_TAGS) || \
224 defined(CONFIG_CMDLINE_TAG) || \
225 defined(CONFIG_INITRD_TAG) || \
226 defined(CONFIG_SERIAL_TAG) || \
227 defined(CONFIG_REVISION_TAG)
228static void setup_end_tag(bd_t *bd)
wdenkc6097192002-11-03 00:24:07 +0000229{
wdenk86765902003-12-06 23:55:10 +0000230 params->hdr.tag = ATAG_NONE;
231 params->hdr.size = 0;
wdenkc6097192002-11-03 00:24:07 +0000232}
Simon Schwarz6a597b92012-03-15 04:01:45 +0000233#endif
wdenkc6097192002-11-03 00:24:07 +0000234
Simon Schwarz6a597b92012-03-15 04:01:45 +0000235#ifdef CONFIG_OF_LIBFDT
236static int create_fdt(bootm_headers_t *images)
John Rigbyab51f1e2010-10-13 13:57:36 -0600237{
Simon Schwarz6a597b92012-03-15 04:01:45 +0000238 ulong of_size = images->ft_len;
239 char **of_flat_tree = &images->ft_addr;
240 ulong *initrd_start = &images->initrd_start;
241 ulong *initrd_end = &images->initrd_end;
242 struct lmb *lmb = &images->lmb;
243 ulong rd_len;
244 int ret;
John Rigbyab51f1e2010-10-13 13:57:36 -0600245
Simon Schwarz6a597b92012-03-15 04:01:45 +0000246 debug("using: FDT\n");
247
248 boot_fdt_add_mem_rsv_regions(lmb, *of_flat_tree);
249
250 rd_len = images->rd_end - images->rd_start;
251 ret = boot_ramdisk_high(lmb, images->rd_start, rd_len,
252 initrd_start, initrd_end);
253 if (ret)
254 return ret;
255
256 ret = boot_relocate_fdt(lmb, of_flat_tree, &of_size);
257 if (ret)
258 return ret;
259
260 fdt_chosen(*of_flat_tree, 1);
261 fixup_memory_node(*of_flat_tree);
Stephen Warren6d32e642012-04-19 11:09:49 +0000262 fdt_fixup_ethernet(*of_flat_tree);
Simon Schwarz6a597b92012-03-15 04:01:45 +0000263 fdt_initrd(*of_flat_tree, *initrd_start, *initrd_end, 1);
Joe Hershbergerceea0772012-08-17 10:28:50 +0000264#ifdef CONFIG_OF_BOARD_SETUP
265 ft_board_setup(*of_flat_tree, gd->bd);
266#endif
Simon Schwarz6a597b92012-03-15 04:01:45 +0000267
268 return 0;
269}
270#endif
271
Pali Rohár5ebe4f12012-10-19 02:00:04 +0000272__weak void setup_board_tags(struct tag **in_params) {}
273
Simon Schwarz6a597b92012-03-15 04:01:45 +0000274/* Subcommand: PREP */
275static void boot_prep_linux(bootm_headers_t *images)
276{
277#ifdef CONFIG_CMDLINE_TAG
278 char *commandline = getenv("bootargs");
279#endif
280
281#ifdef CONFIG_OF_LIBFDT
282 if (images->ft_len) {
283 debug("using: FDT\n");
284 if (create_fdt(images)) {
285 printf("FDT creation failed! hanging...");
286 hang();
287 }
288 } else
289#endif
290 {
291#if defined(CONFIG_SETUP_MEMORY_TAGS) || \
292 defined(CONFIG_CMDLINE_TAG) || \
293 defined(CONFIG_INITRD_TAG) || \
294 defined(CONFIG_SERIAL_TAG) || \
295 defined(CONFIG_REVISION_TAG)
296 debug("using: ATAGS\n");
297 setup_start_tag(gd->bd);
298#ifdef CONFIG_SERIAL_TAG
299 setup_serial_tag(&params);
300#endif
301#ifdef CONFIG_CMDLINE_TAG
302 setup_commandline_tag(gd->bd, commandline);
303#endif
304#ifdef CONFIG_REVISION_TAG
305 setup_revision_tag(&params);
306#endif
307#ifdef CONFIG_SETUP_MEMORY_TAGS
308 setup_memory_tags(gd->bd);
309#endif
310#ifdef CONFIG_INITRD_TAG
311 if (images->rd_start && images->rd_end)
312 setup_initrd_tag(gd->bd, images->rd_start,
313 images->rd_end);
314#endif
Pali Rohár5ebe4f12012-10-19 02:00:04 +0000315 setup_board_tags(&params);
Simon Schwarz6a597b92012-03-15 04:01:45 +0000316 setup_end_tag(gd->bd);
317#else /* all tags */
318 printf("FDT and ATAGS support not compiled in - hanging\n");
319 hang();
320#endif /* all tags */
321 }
322}
323
324/* Subcommand: GO */
325static void boot_jump_linux(bootm_headers_t *images)
326{
327 unsigned long machid = gd->bd->bi_arch_number;
328 char *s;
329 void (*kernel_entry)(int zero, int arch, uint params);
Stephen Warrenc570fba2012-04-19 11:34:00 +0000330 unsigned long r2;
Simon Schwarz6a597b92012-03-15 04:01:45 +0000331
332 kernel_entry = (void (*)(int, int, uint))images->ep;
333
334 s = getenv("machid");
335 if (s) {
336 strict_strtoul(s, 16, &machid);
337 printf("Using machid 0x%lx from environment\n", machid);
338 }
339
340 debug("## Transferring control to Linux (at address %08lx)" \
341 "...\n", (ulong) kernel_entry);
342 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
343 announce_and_cleanup();
Stephen Warrenc570fba2012-04-19 11:34:00 +0000344
345#ifdef CONFIG_OF_LIBFDT
346 if (images->ft_len)
347 r2 = (unsigned long)images->ft_addr;
348 else
349#endif
350 r2 = gd->bd->bi_boot_params;
351
352 kernel_entry(0, machid, r2);
Simon Schwarz6a597b92012-03-15 04:01:45 +0000353}
354
355/* Main Entry point for arm bootm implementation
356 *
357 * Modeled after the powerpc implementation
358 * DIFFERENCE: Instead of calling prep and go at the end
359 * they are called if subcommand is equal 0.
360 */
361int do_bootm_linux(int flag, int argc, char *argv[], bootm_headers_t *images)
362{
363 /* No need for those on ARM */
364 if (flag & BOOTM_STATE_OS_BD_T || flag & BOOTM_STATE_OS_CMDLINE)
365 return -1;
366
367 if (flag & BOOTM_STATE_OS_PREP) {
368 boot_prep_linux(images);
369 return 0;
370 }
371
372 if (flag & BOOTM_STATE_OS_GO) {
373 boot_jump_linux(images);
374 return 0;
375 }
376
377 boot_prep_linux(images);
378 boot_jump_linux(images);
379 return 0;
John Rigbyab51f1e2010-10-13 13:57:36 -0600380}
Marek Vasutcf41a9b2012-03-14 21:52:45 +0000381
382#ifdef CONFIG_CMD_BOOTZ
383
384struct zimage_header {
385 uint32_t code[9];
386 uint32_t zi_magic;
387 uint32_t zi_start;
388 uint32_t zi_end;
389};
390
391#define LINUX_ARM_ZIMAGE_MAGIC 0x016f2818
392
393int bootz_setup(void *image, void **start, void **end)
394{
395 struct zimage_header *zi = (struct zimage_header *)image;
396
397 if (zi->zi_magic != LINUX_ARM_ZIMAGE_MAGIC) {
398 puts("Bad Linux ARM zImage magic!\n");
399 return 1;
400 }
401
402 *start = (void *)zi->zi_start;
403 *end = (void *)zi->zi_end;
404
405 debug("Kernel image @ 0x%08x [ 0x%08x - 0x%08x ]\n",
406 (uint32_t)image, (uint32_t)*start, (uint32_t)*end);
407
408 return 0;
409}
410#endif /* CONFIG_CMD_BOOTZ */