blob: 7eb8712102607fdd18f5fd349f2efa217286d689 [file] [log] [blame]
Marian Balakowicz2437cf22008-01-08 18:11:43 +01001/*
2 * (C) Copyright 2008 Semihalf
3 *
4 * (C) Copyright 2000-2006
5 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
6 *
7 * See file CREDITS for list of people who contributed to this
8 * project.
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation; either version 2 of
13 * the License, or (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
23 * MA 02111-1307 USA
24 */
25
Marian Balakowiczf92332b2008-01-31 13:20:06 +010026#define DEBUG
27
Marian Balakowicz2437cf22008-01-08 18:11:43 +010028#include <common.h>
29#include <watchdog.h>
30#include <command.h>
31#include <image.h>
32#include <malloc.h>
33#include <zlib.h>
34#include <bzlib.h>
35#include <environment.h>
36#include <asm/byteorder.h>
37
38#if defined(CONFIG_OF_LIBFDT)
39#include <fdt.h>
40#include <libfdt.h>
41#include <fdt_support.h>
Marian Balakowicz28fb6152008-01-31 13:20:08 +010042
43static void fdt_error (const char *msg);
Marian Balakowicz2437cf22008-01-08 18:11:43 +010044#endif
45
Marian Balakowicz2437cf22008-01-08 18:11:43 +010046#ifdef CFG_INIT_RAM_LOCK
47#include <asm/cache.h>
48#endif
49
Marian Balakowicz2437cf22008-01-08 18:11:43 +010050DECLARE_GLOBAL_DATA_PTR;
Marian Balakowicz2437cf22008-01-08 18:11:43 +010051
Marian Balakowicz2437cf22008-01-08 18:11:43 +010052extern int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
Marian Balakowicz9c701e82008-01-31 13:57:17 +010053static ulong get_sp (void);
Marian Balakowicz2efc2642008-01-31 13:58:13 +010054static void set_clocks_in_mhz (bd_t *kbd);
Marian Balakowicz2437cf22008-01-08 18:11:43 +010055
56void __attribute__((noinline))
Marian Balakowiczb4a12a92008-01-08 18:12:17 +010057do_bootm_linux(cmd_tbl_t *cmdtp, int flag,
Marian Balakowicz2437cf22008-01-08 18:11:43 +010058 int argc, char *argv[],
Marian Balakowiczb4a12a92008-01-08 18:12:17 +010059 image_header_t *hdr,
Marian Balakowicz2437cf22008-01-08 18:11:43 +010060 int verify)
61{
Marian Balakowicz2efc2642008-01-31 13:58:13 +010062 ulong sp, sp_limit, alloc_current;
63
Marian Balakowiczb4a12a92008-01-08 18:12:17 +010064 ulong initrd_start, initrd_end;
Marian Balakowicz95418502008-01-31 13:55:39 +010065 ulong rd_data_start, rd_data_end, rd_len;
Marian Balakowiczb4a12a92008-01-08 18:12:17 +010066
67 ulong cmd_start, cmd_end;
Marian Balakowicz2437cf22008-01-08 18:11:43 +010068 bd_t *kbd;
69 void (*kernel)(bd_t *, ulong, ulong, ulong, ulong);
Marian Balakowiczb4a12a92008-01-08 18:12:17 +010070
Marian Balakowiczd2160852008-01-31 13:20:07 +010071#if defined(CONFIG_OF_LIBFDT)
Marian Balakowiczb4a12a92008-01-08 18:12:17 +010072 image_header_t *fdt_hdr;
Marian Balakowicz2437cf22008-01-08 18:11:43 +010073 char *of_flat_tree = NULL;
74 ulong of_data = 0;
75#endif
76
Marian Balakowicz2437cf22008-01-08 18:11:43 +010077 /*
78 * Booting a (Linux) kernel image
79 *
80 * Allocate space for command line and board info - the
81 * address should be as high as possible within the reach of
82 * the kernel (see CFG_BOOTMAPSZ settings), but in unused
83 * memory, which means far enough below the current stack
84 * pointer.
85 */
Marian Balakowicz2efc2642008-01-31 13:58:13 +010086 sp = get_sp();
87 debug ("## Current stack ends at 0x%08lx ", sp);
Marian Balakowicz2437cf22008-01-08 18:11:43 +010088
Marian Balakowicz2efc2642008-01-31 13:58:13 +010089 alloc_current = sp_limit = get_boot_sp_limit(sp);
90 debug ("=> set upper limit to 0x%08lx\n", sp_limit);
Marian Balakowicz2437cf22008-01-08 18:11:43 +010091
Marian Balakowicz2efc2642008-01-31 13:58:13 +010092 /* allocate space and init command line */
93 alloc_current = get_boot_cmdline (alloc_current, &cmd_start, &cmd_end);
Marian Balakowicz2437cf22008-01-08 18:11:43 +010094
Marian Balakowicz2efc2642008-01-31 13:58:13 +010095 /* allocate space for kernel copy of board info */
96 alloc_current = get_boot_kbd (alloc_current, &kbd);
97 set_clocks_in_mhz(kbd);
Marian Balakowicz2437cf22008-01-08 18:11:43 +010098
Marian Balakowicz9c701e82008-01-31 13:57:17 +010099 /* find kernel */
Marian Balakowicz2437cf22008-01-08 18:11:43 +0100100 kernel = (void (*)(bd_t *, ulong, ulong, ulong, ulong))image_get_ep (hdr);
101
Marian Balakowicz9c701e82008-01-31 13:57:17 +0100102 /* find ramdisk */
Marian Balakowicz95418502008-01-31 13:55:39 +0100103 get_ramdisk (cmdtp, flag, argc, argv, hdr, verify,
104 IH_ARCH_PPC, &rd_data_start, &rd_data_end);
Marian Balakowicz9c701e82008-01-31 13:57:17 +0100105
Marian Balakowicz95418502008-01-31 13:55:39 +0100106 rd_len = rd_data_end - rd_data_start;
Marian Balakowicz2437cf22008-01-08 18:11:43 +0100107
Marian Balakowicz2efc2642008-01-31 13:58:13 +0100108 alloc_current = ramdisk_high (alloc_current, rd_data_start, rd_len,
109 kbd, sp_limit, get_sp (),
Marian Balakowicz9c701e82008-01-31 13:57:17 +0100110 &initrd_start, &initrd_end);
111
Marian Balakowiczd2160852008-01-31 13:20:07 +0100112#if defined(CONFIG_OF_LIBFDT)
Marian Balakowicz9c701e82008-01-31 13:57:17 +0100113 /* find flattened device tree */
Marian Balakowicz2437cf22008-01-08 18:11:43 +0100114 if(argc > 3) {
115 of_flat_tree = (char *) simple_strtoul(argv[3], NULL, 16);
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100116 fdt_hdr = (image_header_t *)of_flat_tree;
Marian Balakowiczd2160852008-01-31 13:20:07 +0100117
Marian Balakowiczed68d472008-01-08 18:11:44 +0100118 if (fdt_check_header (of_flat_tree) == 0) {
Marian Balakowicz2437cf22008-01-08 18:11:43 +0100119#ifndef CFG_NO_FLASH
120 if (addr2info((ulong)of_flat_tree) != NULL)
121 of_data = (ulong)of_flat_tree;
122#endif
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100123 } else if (image_check_magic (fdt_hdr)) {
Marian Balakowiczf92332b2008-01-31 13:20:06 +0100124 ulong image_start, image_end;
125 ulong load_start, load_end;
126
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100127 printf ("## Flat Device Tree at %08lX\n", fdt_hdr);
128 print_image_hdr (fdt_hdr);
Marian Balakowicz2437cf22008-01-08 18:11:43 +0100129
Marian Balakowiczf92332b2008-01-31 13:20:06 +0100130 image_start = (ulong)fdt_hdr;
131 image_end = image_get_image_end (fdt_hdr);
132
133 load_start = image_get_load (fdt_hdr);
134 load_end = load_start + image_get_data_size (fdt_hdr);
135
136 if ((load_start < image_end) && (load_end > image_start)) {
Marian Balakowicz28fb6152008-01-31 13:20:08 +0100137 fdt_error ("fdt overwritten");
Marian Balakowicz2437cf22008-01-08 18:11:43 +0100138 do_reset (cmdtp, flag, argc, argv);
139 }
140
141 puts (" Verifying Checksum ... ");
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100142 if (!image_check_hcrc (fdt_hdr)) {
Marian Balakowicz28fb6152008-01-31 13:20:08 +0100143 fdt_error ("fdt header checksum invalid");
Marian Balakowicz2437cf22008-01-08 18:11:43 +0100144 do_reset (cmdtp, flag, argc, argv);
145 }
146
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100147 if (!image_check_dcrc (fdt_hdr)) {
Marian Balakowicz28fb6152008-01-31 13:20:08 +0100148 fdt_error ("fdt checksum invalid");
Marian Balakowicz2437cf22008-01-08 18:11:43 +0100149 do_reset (cmdtp, flag, argc, argv);
150 }
151 puts ("OK\n");
152
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100153 if (!image_check_type (fdt_hdr, IH_TYPE_FLATDT)) {
Marian Balakowicz28fb6152008-01-31 13:20:08 +0100154 fdt_error ("uImage is not a fdt");
Marian Balakowicz2437cf22008-01-08 18:11:43 +0100155 do_reset (cmdtp, flag, argc, argv);
156 }
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100157 if (image_get_comp (fdt_hdr) != IH_COMP_NONE) {
Marian Balakowicz28fb6152008-01-31 13:20:08 +0100158 fdt_error ("uImage is compressed");
Marian Balakowicz2437cf22008-01-08 18:11:43 +0100159 do_reset (cmdtp, flag, argc, argv);
160 }
Marian Balakowicz2437cf22008-01-08 18:11:43 +0100161 if (fdt_check_header (of_flat_tree + image_get_header_size ()) != 0) {
Marian Balakowicz28fb6152008-01-31 13:20:08 +0100162 fdt_error ("uImage data is not a fdt");
Marian Balakowicz2437cf22008-01-08 18:11:43 +0100163 do_reset (cmdtp, flag, argc, argv);
164 }
165
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100166 memmove ((void *)image_get_load (fdt_hdr),
Marian Balakowicz2437cf22008-01-08 18:11:43 +0100167 (void *)(of_flat_tree + image_get_header_size ()),
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100168 image_get_data_size (fdt_hdr));
Marian Balakowicz2437cf22008-01-08 18:11:43 +0100169
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100170 of_flat_tree = (char *)image_get_load (fdt_hdr);
Marian Balakowicz2437cf22008-01-08 18:11:43 +0100171 } else {
Marian Balakowicz28fb6152008-01-31 13:20:08 +0100172 fdt_error ("Did not find a Flattened Device Tree");
Marian Balakowicz2437cf22008-01-08 18:11:43 +0100173 do_reset (cmdtp, flag, argc, argv);
174 }
175 printf (" Booting using the fdt at 0x%x\n",
176 of_flat_tree);
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100177 } else if (image_check_type (hdr, IH_TYPE_MULTI)) {
178 ulong fdt_data, fdt_len;
Marian Balakowicz2437cf22008-01-08 18:11:43 +0100179
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100180 image_multi_getimg (hdr, 2, &fdt_data, &fdt_len);
181 if (fdt_len) {
Marian Balakowicz2437cf22008-01-08 18:11:43 +0100182
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100183 of_flat_tree = (char *)fdt_data;
Marian Balakowicz2437cf22008-01-08 18:11:43 +0100184
185#ifndef CFG_NO_FLASH
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100186 /* move the blob if it is in flash (set of_data to !null) */
187 if (addr2info ((ulong)of_flat_tree) != NULL)
188 of_data = (ulong)of_flat_tree;
Marian Balakowicz2437cf22008-01-08 18:11:43 +0100189#endif
190
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100191 if (fdt_check_header (of_flat_tree) != 0) {
Marian Balakowicz28fb6152008-01-31 13:20:08 +0100192 fdt_error ("image is not a fdt");
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100193 do_reset (cmdtp, flag, argc, argv);
194 }
Marian Balakowicz2437cf22008-01-08 18:11:43 +0100195
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100196 if (be32_to_cpu (fdt_totalsize (of_flat_tree)) != fdt_len) {
Marian Balakowicz28fb6152008-01-31 13:20:08 +0100197 fdt_error ("fdt size != image size");
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100198 do_reset (cmdtp, flag, argc, argv);
199 }
Marian Balakowicz2437cf22008-01-08 18:11:43 +0100200 }
201 }
202#endif
Marian Balakowicz2437cf22008-01-08 18:11:43 +0100203
Marian Balakowicz2437cf22008-01-08 18:11:43 +0100204
205#if defined(CONFIG_OF_LIBFDT)
206
207#ifdef CFG_BOOTMAPSZ
208 /*
209 * The blob must be within CFG_BOOTMAPSZ,
210 * so we flag it to be copied if it is not.
211 */
212 if (of_flat_tree >= (char *)CFG_BOOTMAPSZ)
213 of_data = (ulong)of_flat_tree;
214#endif
215
216 /* move of_flat_tree if needed */
217 if (of_data) {
218 int err;
219 ulong of_start, of_len;
220
221 of_len = be32_to_cpu(fdt_totalsize(of_data));
222
Marian Balakowicz2efc2642008-01-31 13:58:13 +0100223 /* position on a 4K boundary before the alloc_current */
224 of_start = alloc_current - of_len;
Marian Balakowicz2437cf22008-01-08 18:11:43 +0100225 of_start &= ~(4096 - 1); /* align on page */
226 debug ("## device tree at 0x%08lX ... 0x%08lX (len=%ld=0x%lX)\n",
227 of_data, of_data + of_len - 1, of_len, of_len);
228
229 of_flat_tree = (char *)of_start;
230 printf (" Loading Device Tree to %08lx, end %08lx ... ",
231 of_start, of_start + of_len - 1);
232 err = fdt_open_into((void *)of_data, (void *)of_start, of_len);
233 if (err != 0) {
Marian Balakowicz28fb6152008-01-31 13:20:08 +0100234 fdt_error ("fdt move failed");
Marian Balakowicz2437cf22008-01-08 18:11:43 +0100235 do_reset (cmdtp, flag, argc, argv);
236 }
237 puts ("OK\n");
238 }
239 /*
240 * Add the chosen node if it doesn't exist, add the env and bd_t
241 * if the user wants it (the logic is in the subroutines).
242 */
243 if (of_flat_tree) {
244 if (fdt_chosen(of_flat_tree, initrd_start, initrd_end, 0) < 0) {
Marian Balakowicz28fb6152008-01-31 13:20:08 +0100245 fdt_error ("/chosen node create failed");
Marian Balakowicz2437cf22008-01-08 18:11:43 +0100246 do_reset (cmdtp, flag, argc, argv);
247 }
248#ifdef CONFIG_OF_HAS_UBOOT_ENV
249 if (fdt_env(of_flat_tree) < 0) {
Marian Balakowicz28fb6152008-01-31 13:20:08 +0100250 fdt_error ("/u-boot-env node create failed");
Marian Balakowicz2437cf22008-01-08 18:11:43 +0100251 do_reset (cmdtp, flag, argc, argv);
252 }
253#endif
254#ifdef CONFIG_OF_HAS_BD_T
255 if (fdt_bd_t(of_flat_tree) < 0) {
Marian Balakowicz28fb6152008-01-31 13:20:08 +0100256 fdt_error ("/bd_t node create failed");
Marian Balakowicz2437cf22008-01-08 18:11:43 +0100257 do_reset (cmdtp, flag, argc, argv);
258 }
259#endif
260#ifdef CONFIG_OF_BOARD_SETUP
261 /* Call the board-specific fixup routine */
262 ft_board_setup(of_flat_tree, gd->bd);
263#endif
Marian Balakowicz2437cf22008-01-08 18:11:43 +0100264 }
Marian Balakowiczd2160852008-01-31 13:20:07 +0100265#endif /* CONFIG_OF_LIBFDT */
Marian Balakowicz751b2b82008-01-08 18:11:43 +0100266
Marian Balakowicz2437cf22008-01-08 18:11:43 +0100267 debug ("## Transferring control to Linux (at address %08lx) ...\n",
268 (ulong)kernel);
269
270 show_boot_progress (15);
271
272#if defined(CFG_INIT_RAM_LOCK) && !defined(CONFIG_E500)
273 unlock_ram_in_cache();
274#endif
275
Marian Balakowiczd2160852008-01-31 13:20:07 +0100276#if defined(CONFIG_OF_LIBFDT)
Marian Balakowicz2437cf22008-01-08 18:11:43 +0100277 if (of_flat_tree) { /* device tree; boot new style */
278 /*
279 * Linux Kernel Parameters (passing device tree):
280 * r3: pointer to the fdt, followed by the board info data
281 * r4: physical pointer to the kernel itself
282 * r5: NULL
283 * r6: NULL
284 * r7: NULL
285 */
286 (*kernel) ((bd_t *)of_flat_tree, (ulong)kernel, 0, 0, 0);
287 /* does not return */
288 }
289#endif
290 /*
291 * Linux Kernel Parameters (passing board info data):
292 * r3: ptr to board info data
293 * r4: initrd_start or 0 if no initrd
294 * r5: initrd_end - unused if r4 is 0
295 * r6: Start of command line string
296 * r7: End of command line string
297 */
298 (*kernel) (kbd, initrd_start, initrd_end, cmd_start, cmd_end);
299 /* does not return */
300}
Marian Balakowicz28fb6152008-01-31 13:20:08 +0100301
Marian Balakowicz9c701e82008-01-31 13:57:17 +0100302static ulong get_sp (void)
303{
304 ulong sp;
305
306 asm( "mr %0,1": "=r"(sp) : );
307 return sp;
308}
309
Marian Balakowicz2efc2642008-01-31 13:58:13 +0100310static void set_clocks_in_mhz (bd_t *kbd)
311{
312 char *s;
313
314 if ((s = getenv ("clocks_in_mhz")) != NULL) {
315 /* convert all clock information to MHz */
316 kbd->bi_intfreq /= 1000000L;
317 kbd->bi_busfreq /= 1000000L;
318#if defined(CONFIG_MPC8220)
319 kbd->bi_inpfreq /= 1000000L;
320 kbd->bi_pcifreq /= 1000000L;
321 kbd->bi_pevfreq /= 1000000L;
322 kbd->bi_flbfreq /= 1000000L;
323 kbd->bi_vcofreq /= 1000000L;
324#endif
325#if defined(CONFIG_CPM2)
326 kbd->bi_cpmfreq /= 1000000L;
327 kbd->bi_brgfreq /= 1000000L;
328 kbd->bi_sccfreq /= 1000000L;
329 kbd->bi_vco /= 1000000L;
330#endif
331#if defined(CONFIG_MPC5xxx)
332 kbd->bi_ipbfreq /= 1000000L;
333 kbd->bi_pcifreq /= 1000000L;
334#endif /* CONFIG_MPC5xxx */
335 }
336}
337
Marian Balakowicz28fb6152008-01-31 13:20:08 +0100338#if defined(CONFIG_OF_LIBFDT)
339static void fdt_error (const char *msg)
340{
341 puts ("ERROR: ");
342 puts (msg);
343 puts (" - must RESET the board to recover.\n");
344}
345#endif