2 * (C) Copyright 2008 Semihalf
4 * (C) Copyright 2000-2006
5 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
7 * See file CREDITS for list of people who contributed to this
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.
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.
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,
35 #include <environment.h>
36 #include <asm/byteorder.h>
38 #if defined(CONFIG_OF_LIBFDT)
41 #include <fdt_support.h>
43 static void fdt_error (const char *msg);
44 static 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);
49 #ifdef CFG_INIT_RAM_LOCK
50 #include <asm/cache.h>
53 DECLARE_GLOBAL_DATA_PTR;
55 extern int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
56 static ulong get_sp (void);
57 static void set_clocks_in_mhz (bd_t *kbd);
59 void __attribute__((noinline))
60 do_bootm_linux(cmd_tbl_t *cmdtp, int flag,
61 int argc, char *argv[],
65 ulong sp, sp_limit, alloc_current;
67 ulong initrd_start, initrd_end;
68 ulong rd_data_start, rd_data_end, rd_len;
70 ulong cmd_start, cmd_end;
72 void (*kernel)(bd_t *, ulong, ulong, ulong, ulong);
74 #if defined(CONFIG_OF_LIBFDT)
79 * Booting a (Linux) kernel image
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
88 debug ("## Current stack ends at 0x%08lx ", sp);
90 alloc_current = sp_limit = get_boot_sp_limit(sp);
91 debug ("=> set upper limit to 0x%08lx\n", sp_limit);
93 /* allocate space and init command line */
94 alloc_current = get_boot_cmdline (alloc_current, &cmd_start, &cmd_end);
96 /* allocate space for kernel copy of board info */
97 alloc_current = get_boot_kbd (alloc_current, &kbd);
98 set_clocks_in_mhz(kbd);
101 kernel = (void (*)(bd_t *, ulong, ulong, ulong, ulong))image_get_ep (hdr);
104 get_ramdisk (cmdtp, flag, argc, argv, hdr, verify,
105 IH_ARCH_PPC, &rd_data_start, &rd_data_end);
107 rd_len = rd_data_end - rd_data_start;
109 alloc_current = ramdisk_high (alloc_current, rd_data_start, rd_len,
110 kbd, sp_limit, get_sp (),
111 &initrd_start, &initrd_end);
113 #if defined(CONFIG_OF_LIBFDT)
114 /* find flattened device tree */
115 alloc_current = get_fdt (alloc_current,
116 cmdtp, flag, argc, argv, hdr, &of_flat_tree);
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).
123 if (fdt_chosen(of_flat_tree, initrd_start, initrd_end, 0) < 0) {
124 fdt_error ("/chosen node create failed");
125 do_reset (cmdtp, flag, argc, argv);
127 #ifdef CONFIG_OF_HAS_UBOOT_ENV
128 if (fdt_env(of_flat_tree) < 0) {
129 fdt_error ("/u-boot-env node create failed");
130 do_reset (cmdtp, flag, argc, argv);
133 #ifdef CONFIG_OF_HAS_BD_T
134 if (fdt_bd_t(of_flat_tree) < 0) {
135 fdt_error ("/bd_t node create failed");
136 do_reset (cmdtp, flag, argc, argv);
139 #ifdef CONFIG_OF_BOARD_SETUP
140 /* Call the board-specific fixup routine */
141 ft_board_setup(of_flat_tree, gd->bd);
144 #endif /* CONFIG_OF_LIBFDT */
146 debug ("## Transferring control to Linux (at address %08lx) ...\n",
149 show_boot_progress (15);
151 #if defined(CFG_INIT_RAM_LOCK) && !defined(CONFIG_E500)
152 unlock_ram_in_cache();
155 #if defined(CONFIG_OF_LIBFDT)
156 if (of_flat_tree) { /* device tree; boot new style */
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
165 (*kernel) ((bd_t *)of_flat_tree, (ulong)kernel, 0, 0, 0);
166 /* does not return */
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
177 (*kernel) (kbd, initrd_start, initrd_end, cmd_start, cmd_end);
178 /* does not return */
181 static ulong get_sp (void)
185 asm( "mr %0,1": "=r"(sp) : );
189 static void set_clocks_in_mhz (bd_t *kbd)
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;
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;
210 #if defined(CONFIG_MPC5xxx)
211 kbd->bi_ipbfreq /= 1000000L;
212 kbd->bi_pcifreq /= 1000000L;
213 #endif /* CONFIG_MPC5xxx */
217 #if defined(CONFIG_OF_LIBFDT)
218 static void fdt_error (const char *msg)
222 puts (" - must RESET the board to recover.\n");
225 static 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)
230 image_header_t *fdt_hdr;
231 ulong fdt_relocate = 0;
233 ulong new_alloc_current;
236 fdt = (char *)simple_strtoul (argv[3], NULL, 16);
237 fdt_hdr = (image_header_t *)fdt;
239 if (fdt_check_header (fdt) == 0) {
240 printf ("## Flattened Device Tree blob at %08lx\n", fdt);
242 if (addr2info ((ulong)fdt) != NULL)
245 } else if (image_check_magic (fdt_hdr)) {
246 ulong image_start, image_end;
247 ulong load_start, load_end;
249 printf ("## Flattened Device Tree Image at %08lx\n",
252 print_image_hdr (fdt_hdr);
254 image_start = (ulong)fdt_hdr;
255 image_end = image_get_image_end (fdt_hdr);
257 load_start = image_get_load (fdt_hdr);
258 load_end = load_start + image_get_data_size (fdt_hdr);
260 if ((load_start < image_end) && (load_end > image_start)) {
261 fdt_error ("fdt overwritten");
262 do_reset (cmdtp, flag, argc, argv);
265 puts (" Verifying Checksum ... ");
266 if (!image_check_hcrc (fdt_hdr)) {
267 fdt_error ("fdt header checksum invalid");
268 do_reset (cmdtp, flag, argc, argv);
271 if (!image_check_dcrc (fdt_hdr)) {
272 fdt_error ("fdt checksum invalid");
273 do_reset (cmdtp, flag, argc, argv);
277 if (!image_check_type (fdt_hdr, IH_TYPE_FLATDT)) {
278 fdt_error ("uImage is not a fdt");
279 do_reset (cmdtp, flag, argc, argv);
281 if (image_get_comp (fdt_hdr) != IH_COMP_NONE) {
282 fdt_error ("uImage is compressed");
283 do_reset (cmdtp, flag, argc, argv);
285 if (fdt_check_header ((char *)image_get_data (fdt_hdr)) != 0) {
286 fdt_error ("uImage data is not a fdt");
287 do_reset (cmdtp, flag, argc, argv);
290 memmove ((void *)image_get_load (fdt_hdr),
291 (void *)image_get_data (fdt_hdr),
292 image_get_data_size (fdt_hdr));
294 fdt = (char *)image_get_load (fdt_hdr);
296 fdt_error ("Did not find a Flattened Device Tree");
297 do_reset (cmdtp, flag, argc, argv);
299 printf (" Booting using the fdt at 0x%x\n",
301 } else if (image_check_type (hdr, IH_TYPE_MULTI)) {
302 ulong fdt_data, fdt_len;
304 printf ("## Flattened Device Tree from multi "
305 "component Image at %08lX\n", (ulong)hdr);
307 image_multi_getimg (hdr, 2, &fdt_data, &fdt_len);
310 fdt = (char *)fdt_data;
311 printf (" Booting using the fdt at 0x%x\n", fdt);
314 /* move the blob if it is in flash (set of_relocte) */
315 if (addr2info ((ulong)fdt) != NULL)
319 if (fdt_check_header (fdt) != 0) {
320 fdt_error ("image is not a fdt");
321 do_reset (cmdtp, flag, argc, argv);
324 if (be32_to_cpu (fdt_totalsize (fdt)) != fdt_len) {
325 fdt_error ("fdt size != image size");
326 do_reset (cmdtp, flag, argc, argv);
329 debug (" Did not find a Flattened Device Tree");
335 * The blob must be within CFG_BOOTMAPSZ,
336 * so we flag it to be copied if it is not.
338 if (fdt >= (char *)CFG_BOOTMAPSZ)
342 /* move flattend device tree if needed */
345 ulong of_start, of_len;
347 of_len = be32_to_cpu (fdt_totalsize (fdt));
349 /* position on a 4K boundary before the alloc_current */
350 of_start = alloc_current - of_len;
351 of_start &= ~(4096 - 1); /* align on page */
353 debug ("## device tree at 0x%08lX ... 0x%08lX (len=%ld=0x%lX)\n",
354 (ulong)fdt, (ulong)fdt + of_len - 1,
357 printf (" Loading Device Tree to %08lx, end %08lx ... ",
358 of_start, of_start + of_len - 1);
360 err = fdt_open_into (fdt, (void *)of_start, of_len);
362 fdt_error ("fdt move failed");
363 do_reset (cmdtp, flag, argc, argv);
367 *of_flat_tree = (char *)of_start;
368 new_alloc_current = of_start;
371 new_alloc_current = alloc_current;
374 return new_alloc_current;