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);
46 #ifdef CFG_INIT_RAM_LOCK
47 #include <asm/cache.h>
50 DECLARE_GLOBAL_DATA_PTR;
52 extern int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
53 static ulong get_sp (void);
55 #if defined(CONFIG_CMD_BDI)
56 extern int do_bdinfo (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
59 void __attribute__((noinline))
60 do_bootm_linux(cmd_tbl_t *cmdtp, int flag,
61 int argc, char *argv[],
65 ulong initrd_start, initrd_end;
66 ulong rd_data_start, rd_data_end, rd_len;
68 ulong cmd_start, cmd_end;
74 void (*kernel)(bd_t *, ulong, ulong, ulong, ulong);
76 #if defined(CONFIG_OF_LIBFDT)
77 image_header_t *fdt_hdr;
78 char *of_flat_tree = NULL;
83 * Booting a (Linux) kernel image
85 * Allocate space for command line and board info - the
86 * address should be as high as possible within the reach of
87 * the kernel (see CFG_BOOTMAPSZ settings), but in unused
88 * memory, which means far enough below the current stack
93 debug ("## Current stack ends at 0x%08lX ", sp_limit);
95 sp_limit -= 2048; /* just to be sure */
96 if (sp_limit > CFG_BOOTMAPSZ)
97 sp_limit = CFG_BOOTMAPSZ;
100 debug ("=> set upper limit to 0x%08lX\n", sp_limit);
102 cmdline = (char *)((sp_limit - CFG_BARGSIZE) & ~0xF);
103 kbd = (bd_t *)(((ulong)cmdline - sizeof(bd_t)) & ~0xF);
105 if ((s = getenv("bootargs")) == NULL)
110 cmd_start = (ulong)&cmdline[0];
111 cmd_end = cmd_start + strlen(cmdline);
116 printf ("## cmdline at 0x%08lX ... 0x%08lX\n", cmd_start, cmd_end);
118 #if defined(CONFIG_CMD_BDI)
119 do_bdinfo (NULL, 0, 0, NULL);
123 if ((s = getenv ("clocks_in_mhz")) != NULL) {
124 /* convert all clock information to MHz */
125 kbd->bi_intfreq /= 1000000L;
126 kbd->bi_busfreq /= 1000000L;
127 #if defined(CONFIG_MPC8220)
128 kbd->bi_inpfreq /= 1000000L;
129 kbd->bi_pcifreq /= 1000000L;
130 kbd->bi_pevfreq /= 1000000L;
131 kbd->bi_flbfreq /= 1000000L;
132 kbd->bi_vcofreq /= 1000000L;
134 #if defined(CONFIG_CPM2)
135 kbd->bi_cpmfreq /= 1000000L;
136 kbd->bi_brgfreq /= 1000000L;
137 kbd->bi_sccfreq /= 1000000L;
138 kbd->bi_vco /= 1000000L;
140 #if defined(CONFIG_MPC5xxx)
141 kbd->bi_ipbfreq /= 1000000L;
142 kbd->bi_pcifreq /= 1000000L;
143 #endif /* CONFIG_MPC5xxx */
147 kernel = (void (*)(bd_t *, ulong, ulong, ulong, ulong))image_get_ep (hdr);
150 get_ramdisk (cmdtp, flag, argc, argv, hdr, verify,
151 IH_ARCH_PPC, &rd_data_start, &rd_data_end);
153 rd_len = rd_data_end - rd_data_start;
155 ramdisk_high (rd_data_start, rd_len, kbd, sp_limit, get_sp (),
156 &initrd_start, &initrd_end);
158 #if defined(CONFIG_OF_LIBFDT)
159 /* find flattened device tree */
161 of_flat_tree = (char *) simple_strtoul(argv[3], NULL, 16);
162 fdt_hdr = (image_header_t *)of_flat_tree;
164 if (fdt_check_header (of_flat_tree) == 0) {
166 if (addr2info((ulong)of_flat_tree) != NULL)
167 of_data = (ulong)of_flat_tree;
169 } else if (image_check_magic (fdt_hdr)) {
170 ulong image_start, image_end;
171 ulong load_start, load_end;
173 printf ("## Flat Device Tree at %08lX\n", fdt_hdr);
174 print_image_hdr (fdt_hdr);
176 image_start = (ulong)fdt_hdr;
177 image_end = image_get_image_end (fdt_hdr);
179 load_start = image_get_load (fdt_hdr);
180 load_end = load_start + image_get_data_size (fdt_hdr);
182 if ((load_start < image_end) && (load_end > image_start)) {
183 fdt_error ("fdt overwritten");
184 do_reset (cmdtp, flag, argc, argv);
187 puts (" Verifying Checksum ... ");
188 if (!image_check_hcrc (fdt_hdr)) {
189 fdt_error ("fdt header checksum invalid");
190 do_reset (cmdtp, flag, argc, argv);
193 if (!image_check_dcrc (fdt_hdr)) {
194 fdt_error ("fdt checksum invalid");
195 do_reset (cmdtp, flag, argc, argv);
199 if (!image_check_type (fdt_hdr, IH_TYPE_FLATDT)) {
200 fdt_error ("uImage is not a fdt");
201 do_reset (cmdtp, flag, argc, argv);
203 if (image_get_comp (fdt_hdr) != IH_COMP_NONE) {
204 fdt_error ("uImage is compressed");
205 do_reset (cmdtp, flag, argc, argv);
207 if (fdt_check_header (of_flat_tree + image_get_header_size ()) != 0) {
208 fdt_error ("uImage data is not a fdt");
209 do_reset (cmdtp, flag, argc, argv);
212 memmove ((void *)image_get_load (fdt_hdr),
213 (void *)(of_flat_tree + image_get_header_size ()),
214 image_get_data_size (fdt_hdr));
216 of_flat_tree = (char *)image_get_load (fdt_hdr);
218 fdt_error ("Did not find a Flattened Device Tree");
219 do_reset (cmdtp, flag, argc, argv);
221 printf (" Booting using the fdt at 0x%x\n",
223 } else if (image_check_type (hdr, IH_TYPE_MULTI)) {
224 ulong fdt_data, fdt_len;
226 image_multi_getimg (hdr, 2, &fdt_data, &fdt_len);
229 of_flat_tree = (char *)fdt_data;
232 /* move the blob if it is in flash (set of_data to !null) */
233 if (addr2info ((ulong)of_flat_tree) != NULL)
234 of_data = (ulong)of_flat_tree;
237 if (fdt_check_header (of_flat_tree) != 0) {
238 fdt_error ("image is not a fdt");
239 do_reset (cmdtp, flag, argc, argv);
242 if (be32_to_cpu (fdt_totalsize (of_flat_tree)) != fdt_len) {
243 fdt_error ("fdt size != image size");
244 do_reset (cmdtp, flag, argc, argv);
251 #if defined(CONFIG_OF_LIBFDT)
255 * The blob must be within CFG_BOOTMAPSZ,
256 * so we flag it to be copied if it is not.
258 if (of_flat_tree >= (char *)CFG_BOOTMAPSZ)
259 of_data = (ulong)of_flat_tree;
262 /* move of_flat_tree if needed */
265 ulong of_start, of_len;
267 of_len = be32_to_cpu(fdt_totalsize(of_data));
269 /* position on a 4K boundary before the kbd */
270 of_start = (ulong)kbd - of_len;
271 of_start &= ~(4096 - 1); /* align on page */
272 debug ("## device tree at 0x%08lX ... 0x%08lX (len=%ld=0x%lX)\n",
273 of_data, of_data + of_len - 1, of_len, of_len);
275 of_flat_tree = (char *)of_start;
276 printf (" Loading Device Tree to %08lx, end %08lx ... ",
277 of_start, of_start + of_len - 1);
278 err = fdt_open_into((void *)of_data, (void *)of_start, of_len);
280 fdt_error ("fdt move failed");
281 do_reset (cmdtp, flag, argc, argv);
286 * Add the chosen node if it doesn't exist, add the env and bd_t
287 * if the user wants it (the logic is in the subroutines).
290 if (fdt_chosen(of_flat_tree, initrd_start, initrd_end, 0) < 0) {
291 fdt_error ("/chosen node create failed");
292 do_reset (cmdtp, flag, argc, argv);
294 #ifdef CONFIG_OF_HAS_UBOOT_ENV
295 if (fdt_env(of_flat_tree) < 0) {
296 fdt_error ("/u-boot-env node create failed");
297 do_reset (cmdtp, flag, argc, argv);
300 #ifdef CONFIG_OF_HAS_BD_T
301 if (fdt_bd_t(of_flat_tree) < 0) {
302 fdt_error ("/bd_t node create failed");
303 do_reset (cmdtp, flag, argc, argv);
306 #ifdef CONFIG_OF_BOARD_SETUP
307 /* Call the board-specific fixup routine */
308 ft_board_setup(of_flat_tree, gd->bd);
311 #endif /* CONFIG_OF_LIBFDT */
313 debug ("## Transferring control to Linux (at address %08lx) ...\n",
316 show_boot_progress (15);
318 #if defined(CFG_INIT_RAM_LOCK) && !defined(CONFIG_E500)
319 unlock_ram_in_cache();
322 #if defined(CONFIG_OF_LIBFDT)
323 if (of_flat_tree) { /* device tree; boot new style */
325 * Linux Kernel Parameters (passing device tree):
326 * r3: pointer to the fdt, followed by the board info data
327 * r4: physical pointer to the kernel itself
332 (*kernel) ((bd_t *)of_flat_tree, (ulong)kernel, 0, 0, 0);
333 /* does not return */
337 * Linux Kernel Parameters (passing board info data):
338 * r3: ptr to board info data
339 * r4: initrd_start or 0 if no initrd
340 * r5: initrd_end - unused if r4 is 0
341 * r6: Start of command line string
342 * r7: End of command line string
344 (*kernel) (kbd, initrd_start, initrd_end, cmd_start, cmd_end);
345 /* does not return */
348 static ulong get_sp (void)
352 asm( "mr %0,1": "=r"(sp) : );
356 #if defined(CONFIG_OF_LIBFDT)
357 static void fdt_error (const char *msg)
361 puts (" - must RESET the board to recover.\n");