blob: 08c11b7c0d058158fe87cafea510a125efc31f4f [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
wdenkc6097192002-11-03 00:24:07 +000040static struct tag *params;
John Rigbyab51f1e2010-10-13 13:57:36 -060041
Simon Schwarz6a597b92012-03-15 04:01:45 +000042static ulong get_sp(void)
43{
44 ulong ret;
45
46 asm("mov %0, sp" : "=r"(ret) : );
47 return ret;
48}
49
John Rigbyab51f1e2010-10-13 13:57:36 -060050void arch_lmb_reserve(struct lmb *lmb)
51{
52 ulong sp;
53
54 /*
55 * Booting a (Linux) kernel image
56 *
57 * Allocate space for command line and board info - the
58 * address should be as high as possible within the reach of
59 * the kernel (see CONFIG_SYS_BOOTMAPSZ settings), but in unused
60 * memory, which means far enough below the current stack
61 * pointer.
62 */
63 sp = get_sp();
64 debug("## Current stack ends at 0x%08lx ", sp);
65
Rob Herring13137f32012-06-28 03:54:11 +000066 /* adjust sp by 4K to be safe */
67 sp -= 4096;
John Rigbyab51f1e2010-10-13 13:57:36 -060068 lmb_reserve(lmb, sp,
69 gd->bd->bi_dram[0].start + gd->bd->bi_dram[0].size - sp);
70}
71
John Rigbyab51f1e2010-10-13 13:57:36 -060072#ifdef CONFIG_OF_LIBFDT
John Rigbyab51f1e2010-10-13 13:57:36 -060073static int fixup_memory_node(void *blob)
74{
75 bd_t *bd = gd->bd;
76 int bank;
77 u64 start[CONFIG_NR_DRAM_BANKS];
78 u64 size[CONFIG_NR_DRAM_BANKS];
79
80 for (bank = 0; bank < CONFIG_NR_DRAM_BANKS; bank++) {
81 start[bank] = bd->bi_dram[bank].start;
82 size[bank] = bd->bi_dram[bank].size;
wdenk29e7f5a2004-03-12 00:14:09 +000083 }
John Rigbyab51f1e2010-10-13 13:57:36 -060084
85 return fdt_fixup_memory_banks(blob, start, size, CONFIG_NR_DRAM_BANKS);
86}
Simon Schwarz6a597b92012-03-15 04:01:45 +000087#endif
John Rigbyab51f1e2010-10-13 13:57:36 -060088
Simon Schwarz6a597b92012-03-15 04:01:45 +000089static void announce_and_cleanup(void)
John Rigbyab51f1e2010-10-13 13:57:36 -060090{
Simon Schwarz6a597b92012-03-15 04:01:45 +000091 printf("\nStarting kernel ...\n\n");
92 bootstage_mark_name(BOOTSTAGE_ID_BOOTM_HANDOFF, "start_kernel");
Simon Glass0fa36a42012-09-28 08:56:37 +000093#ifdef CONFIG_BOOTSTAGE_FDT
94 bootstage_fdt_add_report();
95#endif
Simon Schwarz6a597b92012-03-15 04:01:45 +000096#ifdef CONFIG_BOOTSTAGE_REPORT
97 bootstage_report();
98#endif
Wolfgang Denk20ec3672008-09-08 23:26:22 +020099
Simon Schwarz6a597b92012-03-15 04:01:45 +0000100#ifdef CONFIG_USB_DEVICE
101 udc_disconnect();
John Rigbyab51f1e2010-10-13 13:57:36 -0600102#endif
Simon Schwarz6a597b92012-03-15 04:01:45 +0000103 cleanup_before_linux();
104}
wdenkc6097192002-11-03 00:24:07 +0000105
wdenk86765902003-12-06 23:55:10 +0000106static void setup_start_tag (bd_t *bd)
wdenkc6097192002-11-03 00:24:07 +0000107{
Simon Schwarz6a597b92012-03-15 04:01:45 +0000108 params = (struct tag *)bd->bi_boot_params;
wdenkc6097192002-11-03 00:24:07 +0000109
wdenk86765902003-12-06 23:55:10 +0000110 params->hdr.tag = ATAG_CORE;
111 params->hdr.size = tag_size (tag_core);
wdenkc6097192002-11-03 00:24:07 +0000112
wdenk86765902003-12-06 23:55:10 +0000113 params->u.core.flags = 0;
114 params->u.core.pagesize = 0;
115 params->u.core.rootdev = 0;
wdenkc6097192002-11-03 00:24:07 +0000116
wdenk86765902003-12-06 23:55:10 +0000117 params = tag_next (params);
wdenkc6097192002-11-03 00:24:07 +0000118}
wdenkc6097192002-11-03 00:24:07 +0000119
Simon Schwarz6a597b92012-03-15 04:01:45 +0000120static void setup_memory_tags(bd_t *bd)
wdenkc6097192002-11-03 00:24:07 +0000121{
wdenk86765902003-12-06 23:55:10 +0000122 int i;
wdenkc6097192002-11-03 00:24:07 +0000123
wdenk86765902003-12-06 23:55:10 +0000124 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
125 params->hdr.tag = ATAG_MEM;
126 params->hdr.size = tag_size (tag_mem32);
wdenkc6097192002-11-03 00:24:07 +0000127
wdenk86765902003-12-06 23:55:10 +0000128 params->u.mem.start = bd->bi_dram[i].start;
129 params->u.mem.size = bd->bi_dram[i].size;
wdenkc6097192002-11-03 00:24:07 +0000130
wdenk86765902003-12-06 23:55:10 +0000131 params = tag_next (params);
132 }
wdenkc6097192002-11-03 00:24:07 +0000133}
wdenkc6097192002-11-03 00:24:07 +0000134
Simon Schwarz6a597b92012-03-15 04:01:45 +0000135static void setup_commandline_tag(bd_t *bd, char *commandline)
wdenkc6097192002-11-03 00:24:07 +0000136{
wdenk86765902003-12-06 23:55:10 +0000137 char *p;
wdenkc6097192002-11-03 00:24:07 +0000138
wdenkf16b5162004-03-23 21:43:07 +0000139 if (!commandline)
140 return;
141
wdenk86765902003-12-06 23:55:10 +0000142 /* eat leading white space */
143 for (p = commandline; *p == ' '; p++);
wdenkc6097192002-11-03 00:24:07 +0000144
wdenk86765902003-12-06 23:55:10 +0000145 /* skip non-existent command lines so the kernel will still
146 * use its default command line.
147 */
148 if (*p == '\0')
149 return;
wdenkc6097192002-11-03 00:24:07 +0000150
wdenk86765902003-12-06 23:55:10 +0000151 params->hdr.tag = ATAG_CMDLINE;
152 params->hdr.size =
153 (sizeof (struct tag_header) + strlen (p) + 1 + 4) >> 2;
wdenkc6097192002-11-03 00:24:07 +0000154
wdenk86765902003-12-06 23:55:10 +0000155 strcpy (params->u.cmdline.cmdline, p);
wdenkc6097192002-11-03 00:24:07 +0000156
wdenk86765902003-12-06 23:55:10 +0000157 params = tag_next (params);
wdenkc6097192002-11-03 00:24:07 +0000158}
wdenkc6097192002-11-03 00:24:07 +0000159
Simon Schwarz6a597b92012-03-15 04:01:45 +0000160static void setup_initrd_tag(bd_t *bd, ulong initrd_start, ulong initrd_end)
wdenkc6097192002-11-03 00:24:07 +0000161{
wdenk86765902003-12-06 23:55:10 +0000162 /* an ATAG_INITRD node tells the kernel where the compressed
163 * ramdisk can be found. ATAG_RDIMG is a better name, actually.
164 */
wdenk19c8fb72004-04-18 22:26:17 +0000165 params->hdr.tag = ATAG_INITRD2;
wdenk86765902003-12-06 23:55:10 +0000166 params->hdr.size = tag_size (tag_initrd);
wdenkc6097192002-11-03 00:24:07 +0000167
wdenk86765902003-12-06 23:55:10 +0000168 params->u.initrd.start = initrd_start;
169 params->u.initrd.size = initrd_end - initrd_start;
wdenkc6097192002-11-03 00:24:07 +0000170
wdenk86765902003-12-06 23:55:10 +0000171 params = tag_next (params);
wdenkc6097192002-11-03 00:24:07 +0000172}
wdenkc6097192002-11-03 00:24:07 +0000173
Simon Glass50ad2212013-05-08 08:06:02 +0000174static void setup_serial_tag(struct tag **tmp)
wdenka21ad7b2005-05-19 22:39:42 +0000175{
176 struct tag *params = *tmp;
177 struct tag_serialnr serialnr;
wdenka21ad7b2005-05-19 22:39:42 +0000178
179 get_board_serial(&serialnr);
180 params->hdr.tag = ATAG_SERIAL;
181 params->hdr.size = tag_size (tag_serialnr);
182 params->u.serialnr.low = serialnr.low;
183 params->u.serialnr.high= serialnr.high;
184 params = tag_next (params);
185 *tmp = params;
186}
wdenka21ad7b2005-05-19 22:39:42 +0000187
Simon Glass50ad2212013-05-08 08:06:02 +0000188static void setup_revision_tag(struct tag **in_params)
wdenkcb99da52005-01-12 00:15:14 +0000189{
190 u32 rev = 0;
wdenkcb99da52005-01-12 00:15:14 +0000191
192 rev = get_board_rev();
wdenkcb99da52005-01-12 00:15:14 +0000193 params->hdr.tag = ATAG_REVISION;
194 params->hdr.size = tag_size (tag_revision);
195 params->u.revision.rev = rev;
196 params = tag_next (params);
197}
wdenkcb99da52005-01-12 00:15:14 +0000198
Simon Schwarz6a597b92012-03-15 04:01:45 +0000199static void setup_end_tag(bd_t *bd)
wdenkc6097192002-11-03 00:24:07 +0000200{
wdenk86765902003-12-06 23:55:10 +0000201 params->hdr.tag = ATAG_NONE;
202 params->hdr.size = 0;
wdenkc6097192002-11-03 00:24:07 +0000203}
204
Simon Schwarz6a597b92012-03-15 04:01:45 +0000205#ifdef CONFIG_OF_LIBFDT
206static int create_fdt(bootm_headers_t *images)
John Rigbyab51f1e2010-10-13 13:57:36 -0600207{
Simon Schwarz6a597b92012-03-15 04:01:45 +0000208 ulong of_size = images->ft_len;
209 char **of_flat_tree = &images->ft_addr;
210 ulong *initrd_start = &images->initrd_start;
211 ulong *initrd_end = &images->initrd_end;
212 struct lmb *lmb = &images->lmb;
213 ulong rd_len;
214 int ret;
John Rigbyab51f1e2010-10-13 13:57:36 -0600215
Simon Schwarz6a597b92012-03-15 04:01:45 +0000216 debug("using: FDT\n");
217
218 boot_fdt_add_mem_rsv_regions(lmb, *of_flat_tree);
219
220 rd_len = images->rd_end - images->rd_start;
221 ret = boot_ramdisk_high(lmb, images->rd_start, rd_len,
222 initrd_start, initrd_end);
223 if (ret)
224 return ret;
225
226 ret = boot_relocate_fdt(lmb, of_flat_tree, &of_size);
227 if (ret)
228 return ret;
229
230 fdt_chosen(*of_flat_tree, 1);
231 fixup_memory_node(*of_flat_tree);
Stephen Warren6d32e642012-04-19 11:09:49 +0000232 fdt_fixup_ethernet(*of_flat_tree);
Simon Schwarz6a597b92012-03-15 04:01:45 +0000233 fdt_initrd(*of_flat_tree, *initrd_start, *initrd_end, 1);
Joe Hershbergerceea0772012-08-17 10:28:50 +0000234#ifdef CONFIG_OF_BOARD_SETUP
235 ft_board_setup(*of_flat_tree, gd->bd);
236#endif
Simon Schwarz6a597b92012-03-15 04:01:45 +0000237
238 return 0;
239}
240#endif
241
Pali Rohár5ebe4f12012-10-19 02:00:04 +0000242__weak void setup_board_tags(struct tag **in_params) {}
243
Simon Schwarz6a597b92012-03-15 04:01:45 +0000244/* Subcommand: PREP */
245static void boot_prep_linux(bootm_headers_t *images)
246{
Simon Schwarz6a597b92012-03-15 04:01:45 +0000247 char *commandline = getenv("bootargs");
Simon Schwarz6a597b92012-03-15 04:01:45 +0000248
Simon Glass50ad2212013-05-08 08:06:02 +0000249 if (IMAGE_ENABLE_OF_LIBFDT && images->ft_len) {
Simon Schwarz6a597b92012-03-15 04:01:45 +0000250#ifdef CONFIG_OF_LIBFDT
Simon Schwarz6a597b92012-03-15 04:01:45 +0000251 debug("using: FDT\n");
252 if (create_fdt(images)) {
253 printf("FDT creation failed! hanging...");
254 hang();
255 }
Simon Schwarz6a597b92012-03-15 04:01:45 +0000256#endif
Simon Glass50ad2212013-05-08 08:06:02 +0000257 } else if (BOOTM_ENABLE_TAGS) {
Simon Schwarz6a597b92012-03-15 04:01:45 +0000258 debug("using: ATAGS\n");
259 setup_start_tag(gd->bd);
Simon Glass50ad2212013-05-08 08:06:02 +0000260 if (BOOTM_ENABLE_SERIAL_TAG)
261 setup_serial_tag(&params);
262 if (BOOTM_ENABLE_CMDLINE_TAG)
263 setup_commandline_tag(gd->bd, commandline);
264 if (BOOTM_ENABLE_REVISION_TAG)
265 setup_revision_tag(&params);
266 if (BOOTM_ENABLE_MEMORY_TAGS)
267 setup_memory_tags(gd->bd);
268 if (BOOTM_ENABLE_INITRD_TAG) {
269 if (images->rd_start && images->rd_end) {
270 setup_initrd_tag(gd->bd, images->rd_start,
271 images->rd_end);
272 }
273 }
Pali Rohár5ebe4f12012-10-19 02:00:04 +0000274 setup_board_tags(&params);
Simon Schwarz6a597b92012-03-15 04:01:45 +0000275 setup_end_tag(gd->bd);
Simon Glass50ad2212013-05-08 08:06:02 +0000276 } else {
Simon Schwarz6a597b92012-03-15 04:01:45 +0000277 printf("FDT and ATAGS support not compiled in - hanging\n");
278 hang();
Simon Schwarz6a597b92012-03-15 04:01:45 +0000279 }
280}
281
282/* Subcommand: GO */
283static void boot_jump_linux(bootm_headers_t *images)
284{
285 unsigned long machid = gd->bd->bi_arch_number;
286 char *s;
287 void (*kernel_entry)(int zero, int arch, uint params);
Stephen Warrenc570fba2012-04-19 11:34:00 +0000288 unsigned long r2;
Simon Schwarz6a597b92012-03-15 04:01:45 +0000289
290 kernel_entry = (void (*)(int, int, uint))images->ep;
291
292 s = getenv("machid");
293 if (s) {
294 strict_strtoul(s, 16, &machid);
295 printf("Using machid 0x%lx from environment\n", machid);
296 }
297
298 debug("## Transferring control to Linux (at address %08lx)" \
299 "...\n", (ulong) kernel_entry);
300 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
301 announce_and_cleanup();
Stephen Warrenc570fba2012-04-19 11:34:00 +0000302
Simon Glass50ad2212013-05-08 08:06:02 +0000303 if (IMAGE_ENABLE_OF_LIBFDT && images->ft_len)
Stephen Warrenc570fba2012-04-19 11:34:00 +0000304 r2 = (unsigned long)images->ft_addr;
305 else
Stephen Warrenc570fba2012-04-19 11:34:00 +0000306 r2 = gd->bd->bi_boot_params;
307
308 kernel_entry(0, machid, r2);
Simon Schwarz6a597b92012-03-15 04:01:45 +0000309}
310
311/* Main Entry point for arm bootm implementation
312 *
313 * Modeled after the powerpc implementation
314 * DIFFERENCE: Instead of calling prep and go at the end
315 * they are called if subcommand is equal 0.
316 */
317int do_bootm_linux(int flag, int argc, char *argv[], bootm_headers_t *images)
318{
319 /* No need for those on ARM */
320 if (flag & BOOTM_STATE_OS_BD_T || flag & BOOTM_STATE_OS_CMDLINE)
321 return -1;
322
323 if (flag & BOOTM_STATE_OS_PREP) {
324 boot_prep_linux(images);
325 return 0;
326 }
327
328 if (flag & BOOTM_STATE_OS_GO) {
329 boot_jump_linux(images);
330 return 0;
331 }
332
333 boot_prep_linux(images);
334 boot_jump_linux(images);
335 return 0;
John Rigbyab51f1e2010-10-13 13:57:36 -0600336}
Marek Vasutcf41a9b2012-03-14 21:52:45 +0000337
338#ifdef CONFIG_CMD_BOOTZ
339
340struct zimage_header {
341 uint32_t code[9];
342 uint32_t zi_magic;
343 uint32_t zi_start;
344 uint32_t zi_end;
345};
346
347#define LINUX_ARM_ZIMAGE_MAGIC 0x016f2818
348
349int bootz_setup(void *image, void **start, void **end)
350{
351 struct zimage_header *zi = (struct zimage_header *)image;
352
353 if (zi->zi_magic != LINUX_ARM_ZIMAGE_MAGIC) {
354 puts("Bad Linux ARM zImage magic!\n");
355 return 1;
356 }
357
358 *start = (void *)zi->zi_start;
359 *end = (void *)zi->zi_end;
360
361 debug("Kernel image @ 0x%08x [ 0x%08x - 0x%08x ]\n",
362 (uint32_t)image, (uint32_t)*start, (uint32_t)*end);
363
364 return 0;
365}
366#endif /* CONFIG_CMD_BOOTZ */