blob: d2ee3dc5cca207f0296c11e30a130d972e32fdd1 [file] [log] [blame]
Marian Balakowicz5d3cc552008-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 Balakowicz75824382008-01-31 13:20:06 +010026#define DEBUG
27
Marian Balakowicz5d3cc552008-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 Balakowiczd3c5eb62008-01-31 13:20:08 +010042
43static void fdt_error (const char *msg);
Marian Balakowicz7b325452008-01-31 13:58:20 +010044static ulong get_fdt (ulong alloc_current, cmd_tbl_t *cmdtp, int flag,
45 int argc, char *argv[],
46 image_header_t *hdr, char **of_flat_tree);
Marian Balakowicz5d3cc552008-01-08 18:11:43 +010047#endif
48
Marian Balakowicz5d3cc552008-01-08 18:11:43 +010049#ifdef CFG_INIT_RAM_LOCK
50#include <asm/cache.h>
51#endif
52
Marian Balakowicz5d3cc552008-01-08 18:11:43 +010053DECLARE_GLOBAL_DATA_PTR;
Marian Balakowicz5d3cc552008-01-08 18:11:43 +010054
Marian Balakowicz5d3cc552008-01-08 18:11:43 +010055extern int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
Marian Balakowiczceaed2b2008-01-31 13:57:17 +010056static ulong get_sp (void);
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +010057static void set_clocks_in_mhz (bd_t *kbd);
Marian Balakowicz5d3cc552008-01-08 18:11:43 +010058
59void __attribute__((noinline))
Marian Balakowiczf13e7b22008-01-08 18:12:17 +010060do_bootm_linux(cmd_tbl_t *cmdtp, int flag,
Marian Balakowicz5d3cc552008-01-08 18:11:43 +010061 int argc, char *argv[],
Marian Balakowiczf13e7b22008-01-08 18:12:17 +010062 image_header_t *hdr,
Marian Balakowicz5d3cc552008-01-08 18:11:43 +010063 int verify)
64{
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +010065 ulong sp, sp_limit, alloc_current;
66
Marian Balakowiczf13e7b22008-01-08 18:12:17 +010067 ulong initrd_start, initrd_end;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +010068 ulong rd_data_start, rd_data_end, rd_len;
Marian Balakowiczf13e7b22008-01-08 18:12:17 +010069
70 ulong cmd_start, cmd_end;
Marian Balakowicz5d3cc552008-01-08 18:11:43 +010071 bd_t *kbd;
72 void (*kernel)(bd_t *, ulong, ulong, ulong, ulong);
Marian Balakowiczf13e7b22008-01-08 18:12:17 +010073
Marian Balakowicz4a2ad5f2008-01-31 13:20:07 +010074#if defined(CONFIG_OF_LIBFDT)
Marian Balakowicz7b325452008-01-31 13:58:20 +010075 char *of_flat_tree;
Marian Balakowicz5d3cc552008-01-08 18:11:43 +010076#endif
77
Marian Balakowicz5d3cc552008-01-08 18:11:43 +010078 /*
79 * Booting a (Linux) kernel image
80 *
81 * Allocate space for command line and board info - the
82 * address should be as high as possible within the reach of
83 * the kernel (see CFG_BOOTMAPSZ settings), but in unused
84 * memory, which means far enough below the current stack
85 * pointer.
86 */
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +010087 sp = get_sp();
88 debug ("## Current stack ends at 0x%08lx ", sp);
Marian Balakowicz5d3cc552008-01-08 18:11:43 +010089
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +010090 alloc_current = sp_limit = get_boot_sp_limit(sp);
91 debug ("=> set upper limit to 0x%08lx\n", sp_limit);
Marian Balakowicz5d3cc552008-01-08 18:11:43 +010092
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +010093 /* allocate space and init command line */
94 alloc_current = get_boot_cmdline (alloc_current, &cmd_start, &cmd_end);
Marian Balakowicz5d3cc552008-01-08 18:11:43 +010095
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +010096 /* allocate space for kernel copy of board info */
97 alloc_current = get_boot_kbd (alloc_current, &kbd);
98 set_clocks_in_mhz(kbd);
Marian Balakowicz5d3cc552008-01-08 18:11:43 +010099
Marian Balakowiczceaed2b2008-01-31 13:57:17 +0100100 /* find kernel */
Marian Balakowicz5d3cc552008-01-08 18:11:43 +0100101 kernel = (void (*)(bd_t *, ulong, ulong, ulong, ulong))image_get_ep (hdr);
102
Marian Balakowiczceaed2b2008-01-31 13:57:17 +0100103 /* find ramdisk */
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100104 get_ramdisk (cmdtp, flag, argc, argv, hdr, verify,
105 IH_ARCH_PPC, &rd_data_start, &rd_data_end);
Marian Balakowiczceaed2b2008-01-31 13:57:17 +0100106
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100107 rd_len = rd_data_end - rd_data_start;
Marian Balakowicz5d3cc552008-01-08 18:11:43 +0100108
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +0100109 alloc_current = ramdisk_high (alloc_current, rd_data_start, rd_len,
110 kbd, sp_limit, get_sp (),
Marian Balakowiczceaed2b2008-01-31 13:57:17 +0100111 &initrd_start, &initrd_end);
112
Marian Balakowicz4a2ad5f2008-01-31 13:20:07 +0100113#if defined(CONFIG_OF_LIBFDT)
Marian Balakowiczceaed2b2008-01-31 13:57:17 +0100114 /* find flattened device tree */
Marian Balakowicz7b325452008-01-31 13:58:20 +0100115 alloc_current = get_fdt (alloc_current,
116 cmdtp, flag, argc, argv, hdr, &of_flat_tree);
Marian Balakowicz4a2ad5f2008-01-31 13:20:07 +0100117
Marian Balakowicz5d3cc552008-01-08 18:11:43 +0100118 /*
119 * Add the chosen node if it doesn't exist, add the env and bd_t
120 * if the user wants it (the logic is in the subroutines).
121 */
122 if (of_flat_tree) {
123 if (fdt_chosen(of_flat_tree, initrd_start, initrd_end, 0) < 0) {
Marian Balakowiczd3c5eb62008-01-31 13:20:08 +0100124 fdt_error ("/chosen node create failed");
Marian Balakowicz5d3cc552008-01-08 18:11:43 +0100125 do_reset (cmdtp, flag, argc, argv);
126 }
127#ifdef CONFIG_OF_HAS_UBOOT_ENV
128 if (fdt_env(of_flat_tree) < 0) {
Marian Balakowiczd3c5eb62008-01-31 13:20:08 +0100129 fdt_error ("/u-boot-env node create failed");
Marian Balakowicz5d3cc552008-01-08 18:11:43 +0100130 do_reset (cmdtp, flag, argc, argv);
131 }
132#endif
133#ifdef CONFIG_OF_HAS_BD_T
134 if (fdt_bd_t(of_flat_tree) < 0) {
Marian Balakowiczd3c5eb62008-01-31 13:20:08 +0100135 fdt_error ("/bd_t node create failed");
Marian Balakowicz5d3cc552008-01-08 18:11:43 +0100136 do_reset (cmdtp, flag, argc, argv);
137 }
138#endif
139#ifdef CONFIG_OF_BOARD_SETUP
140 /* Call the board-specific fixup routine */
141 ft_board_setup(of_flat_tree, gd->bd);
142#endif
143 }
Marian Balakowicz4a2ad5f2008-01-31 13:20:07 +0100144#endif /* CONFIG_OF_LIBFDT */
Marian Balakowiczd45d5a12008-01-08 18:11:43 +0100145
Marian Balakowicz5d3cc552008-01-08 18:11:43 +0100146 debug ("## Transferring control to Linux (at address %08lx) ...\n",
147 (ulong)kernel);
148
149 show_boot_progress (15);
150
151#if defined(CFG_INIT_RAM_LOCK) && !defined(CONFIG_E500)
152 unlock_ram_in_cache();
153#endif
154
Marian Balakowicz4a2ad5f2008-01-31 13:20:07 +0100155#if defined(CONFIG_OF_LIBFDT)
Marian Balakowicz5d3cc552008-01-08 18:11:43 +0100156 if (of_flat_tree) { /* device tree; boot new style */
157 /*
158 * Linux Kernel Parameters (passing device tree):
159 * r3: pointer to the fdt, followed by the board info data
160 * r4: physical pointer to the kernel itself
161 * r5: NULL
162 * r6: NULL
163 * r7: NULL
164 */
165 (*kernel) ((bd_t *)of_flat_tree, (ulong)kernel, 0, 0, 0);
166 /* does not return */
167 }
168#endif
169 /*
170 * Linux Kernel Parameters (passing board info data):
171 * r3: ptr to board info data
172 * r4: initrd_start or 0 if no initrd
173 * r5: initrd_end - unused if r4 is 0
174 * r6: Start of command line string
175 * r7: End of command line string
176 */
177 (*kernel) (kbd, initrd_start, initrd_end, cmd_start, cmd_end);
178 /* does not return */
179}
Marian Balakowiczd3c5eb62008-01-31 13:20:08 +0100180
Marian Balakowiczceaed2b2008-01-31 13:57:17 +0100181static ulong get_sp (void)
182{
183 ulong sp;
184
185 asm( "mr %0,1": "=r"(sp) : );
186 return sp;
187}
188
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +0100189static void set_clocks_in_mhz (bd_t *kbd)
190{
191 char *s;
192
193 if ((s = getenv ("clocks_in_mhz")) != NULL) {
194 /* convert all clock information to MHz */
195 kbd->bi_intfreq /= 1000000L;
196 kbd->bi_busfreq /= 1000000L;
197#if defined(CONFIG_MPC8220)
198 kbd->bi_inpfreq /= 1000000L;
199 kbd->bi_pcifreq /= 1000000L;
200 kbd->bi_pevfreq /= 1000000L;
201 kbd->bi_flbfreq /= 1000000L;
202 kbd->bi_vcofreq /= 1000000L;
203#endif
204#if defined(CONFIG_CPM2)
205 kbd->bi_cpmfreq /= 1000000L;
206 kbd->bi_brgfreq /= 1000000L;
207 kbd->bi_sccfreq /= 1000000L;
208 kbd->bi_vco /= 1000000L;
209#endif
210#if defined(CONFIG_MPC5xxx)
211 kbd->bi_ipbfreq /= 1000000L;
212 kbd->bi_pcifreq /= 1000000L;
213#endif /* CONFIG_MPC5xxx */
214 }
215}
216
Marian Balakowiczd3c5eb62008-01-31 13:20:08 +0100217#if defined(CONFIG_OF_LIBFDT)
218static void fdt_error (const char *msg)
219{
220 puts ("ERROR: ");
221 puts (msg);
222 puts (" - must RESET the board to recover.\n");
223}
Marian Balakowicz7b325452008-01-31 13:58:20 +0100224
225static ulong get_fdt (ulong alloc_current,
226 cmd_tbl_t *cmdtp, int flag,
227 int argc, char *argv[],
228 image_header_t *hdr, char **of_flat_tree)
229{
230 image_header_t *fdt_hdr;
231 ulong fdt_relocate = 0;
232 char *fdt = NULL;
233 ulong new_alloc_current;
234
235 if(argc > 3) {
236 fdt = (char *)simple_strtoul (argv[3], NULL, 16);
Marian Balakowiczfff888a12008-02-21 17:20:19 +0100237
238 debug ("## Checking for 'FDT'/'FDT image' at %08lx\n", fdt);
239
240 /* copy from dataflash if needed */
241 fdt = (char *)gen_get_image ((ulong)fdt);
Marian Balakowicz7b325452008-01-31 13:58:20 +0100242 fdt_hdr = (image_header_t *)fdt;
243
244 if (fdt_check_header (fdt) == 0) {
245 printf ("## Flattened Device Tree blob at %08lx\n", fdt);
246#ifndef CFG_NO_FLASH
247 if (addr2info ((ulong)fdt) != NULL)
248 fdt_relocate = 1;
249#endif
250 } else if (image_check_magic (fdt_hdr)) {
251 ulong image_start, image_end;
252 ulong load_start, load_end;
253
254 printf ("## Flattened Device Tree Image at %08lx\n",
255 fdt_hdr);
256
Marian Balakowicz2242f532008-02-21 17:27:41 +0100257 image_print_contents (fdt_hdr);
Marian Balakowicz7b325452008-01-31 13:58:20 +0100258
259 image_start = (ulong)fdt_hdr;
260 image_end = image_get_image_end (fdt_hdr);
261
262 load_start = image_get_load (fdt_hdr);
263 load_end = load_start + image_get_data_size (fdt_hdr);
264
265 if ((load_start < image_end) && (load_end > image_start)) {
266 fdt_error ("fdt overwritten");
267 do_reset (cmdtp, flag, argc, argv);
268 }
269
270 puts (" Verifying Checksum ... ");
271 if (!image_check_hcrc (fdt_hdr)) {
272 fdt_error ("fdt header checksum invalid");
273 do_reset (cmdtp, flag, argc, argv);
274 }
275
276 if (!image_check_dcrc (fdt_hdr)) {
277 fdt_error ("fdt checksum invalid");
278 do_reset (cmdtp, flag, argc, argv);
279 }
280 puts ("OK\n");
281
282 if (!image_check_type (fdt_hdr, IH_TYPE_FLATDT)) {
283 fdt_error ("uImage is not a fdt");
284 do_reset (cmdtp, flag, argc, argv);
285 }
286 if (image_get_comp (fdt_hdr) != IH_COMP_NONE) {
287 fdt_error ("uImage is compressed");
288 do_reset (cmdtp, flag, argc, argv);
289 }
290 if (fdt_check_header ((char *)image_get_data (fdt_hdr)) != 0) {
291 fdt_error ("uImage data is not a fdt");
292 do_reset (cmdtp, flag, argc, argv);
293 }
294
295 memmove ((void *)image_get_load (fdt_hdr),
296 (void *)image_get_data (fdt_hdr),
297 image_get_data_size (fdt_hdr));
298
299 fdt = (char *)image_get_load (fdt_hdr);
300 } else {
301 fdt_error ("Did not find a Flattened Device Tree");
302 do_reset (cmdtp, flag, argc, argv);
303 }
304 printf (" Booting using the fdt at 0x%x\n",
305 fdt);
306 } else if (image_check_type (hdr, IH_TYPE_MULTI)) {
307 ulong fdt_data, fdt_len;
308
309 printf ("## Flattened Device Tree from multi "
310 "component Image at %08lX\n", (ulong)hdr);
311
312 image_multi_getimg (hdr, 2, &fdt_data, &fdt_len);
313 if (fdt_len) {
314
315 fdt = (char *)fdt_data;
316 printf (" Booting using the fdt at 0x%x\n", fdt);
317
318#ifndef CFG_NO_FLASH
319 /* move the blob if it is in flash (set of_relocte) */
320 if (addr2info ((ulong)fdt) != NULL)
321 fdt_relocate = 1;
322#endif
323
324 if (fdt_check_header (fdt) != 0) {
325 fdt_error ("image is not a fdt");
326 do_reset (cmdtp, flag, argc, argv);
327 }
328
329 if (be32_to_cpu (fdt_totalsize (fdt)) != fdt_len) {
330 fdt_error ("fdt size != image size");
331 do_reset (cmdtp, flag, argc, argv);
332 }
333 } else {
334 debug (" Did not find a Flattened Device Tree");
335 }
336 }
337
338#ifdef CFG_BOOTMAPSZ
339 /*
340 * The blob must be within CFG_BOOTMAPSZ,
341 * so we flag it to be copied if it is not.
342 */
343 if (fdt >= (char *)CFG_BOOTMAPSZ)
344 fdt_relocate = 1;
345#endif
346
347 /* move flattend device tree if needed */
348 if (fdt_relocate) {
349 int err;
350 ulong of_start, of_len;
351
352 of_len = be32_to_cpu (fdt_totalsize (fdt));
353
354 /* position on a 4K boundary before the alloc_current */
355 of_start = alloc_current - of_len;
356 of_start &= ~(4096 - 1); /* align on page */
357
358 debug ("## device tree at 0x%08lX ... 0x%08lX (len=%ld=0x%lX)\n",
359 (ulong)fdt, (ulong)fdt + of_len - 1,
360 of_len, of_len);
361
362 printf (" Loading Device Tree to %08lx, end %08lx ... ",
363 of_start, of_start + of_len - 1);
364
365 err = fdt_open_into (fdt, (void *)of_start, of_len);
366 if (err != 0) {
367 fdt_error ("fdt move failed");
368 do_reset (cmdtp, flag, argc, argv);
369 }
370 puts ("OK\n");
371
372 *of_flat_tree = (char *)of_start;
373 new_alloc_current = of_start;
374 } else {
375 *of_flat_tree = fdt;
376 new_alloc_current = alloc_current;
377 }
378
379 return new_alloc_current;
380}
Marian Balakowiczd3c5eb62008-01-31 13:20:08 +0100381#endif