blob: c08b62c47e353b277e033085d0df29a3092794e6 [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 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02007 * SPDX-License-Identifier: GPL-2.0+
Marian Balakowicz2437cf22008-01-08 18:11:43 +01008 */
9
Marian Balakowiczf92332b2008-01-31 13:20:06 +010010
Marian Balakowicz2437cf22008-01-08 18:11:43 +010011#include <common.h>
12#include <watchdog.h>
13#include <command.h>
14#include <image.h>
15#include <malloc.h>
Jean-Christophe PLAGNIOL-VILLARD6bb94492009-04-04 12:49:11 +020016#include <u-boot/zlib.h>
Marian Balakowicz2437cf22008-01-08 18:11:43 +010017#include <bzlib.h>
18#include <environment.h>
19#include <asm/byteorder.h>
Kumar Gala365024c2011-01-31 15:51:20 -060020#include <asm/mp.h>
Marian Balakowicz2437cf22008-01-08 18:11:43 +010021
22#if defined(CONFIG_OF_LIBFDT)
Marian Balakowicz2437cf22008-01-08 18:11:43 +010023#include <libfdt.h>
24#include <fdt_support.h>
Wolfgang Denk1e0f07e2009-07-26 23:28:02 +020025#endif
26
27#ifdef CONFIG_SYS_INIT_RAM_LOCK
28#include <asm/cache.h>
Marian Balakowicz2437cf22008-01-08 18:11:43 +010029#endif
30
Marian Balakowicz2437cf22008-01-08 18:11:43 +010031DECLARE_GLOBAL_DATA_PTR;
Marian Balakowicz2437cf22008-01-08 18:11:43 +010032
Marian Balakowicz9c701e82008-01-31 13:57:17 +010033static ulong get_sp (void);
Miao Yan1bd54562013-11-28 17:51:38 +080034extern void ft_fixup_num_cores(void *blob);
Marian Balakowicz2efc2642008-01-31 13:58:13 +010035static void set_clocks_in_mhz (bd_t *kbd);
Marian Balakowicz2437cf22008-01-08 18:11:43 +010036
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020037#ifndef CONFIG_SYS_LINUX_LOWMEM_MAX_SIZE
38#define CONFIG_SYS_LINUX_LOWMEM_MAX_SIZE (768*1024*1024)
Kumar Galac5bacfd2008-02-27 21:51:50 -060039#endif
40
Kumar Galaffccb572008-10-21 17:25:46 -050041static void boot_jump_linux(bootm_headers_t *images)
42{
43 void (*kernel)(bd_t *, ulong r4, ulong r5, ulong r6,
44 ulong r7, ulong r8, ulong r9);
45#ifdef CONFIG_OF_LIBFDT
46 char *of_flat_tree = images->ft_addr;
47#endif
48
49 kernel = (void (*)(bd_t *, ulong, ulong, ulong,
50 ulong, ulong, ulong))images->ep;
51 debug ("## Transferring control to Linux (at address %08lx) ...\n",
52 (ulong)kernel);
53
Simon Glass0169e6b2012-02-13 13:51:18 +000054 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
Kumar Galaffccb572008-10-21 17:25:46 -050055
Wolfgang Denk1e0f07e2009-07-26 23:28:02 +020056#if defined(CONFIG_SYS_INIT_RAM_LOCK) && !defined(CONFIG_E500)
57 unlock_ram_in_cache();
58#endif
59
Kumar Galaffccb572008-10-21 17:25:46 -050060#if defined(CONFIG_OF_LIBFDT)
61 if (of_flat_tree) { /* device tree; boot new style */
62 /*
63 * Linux Kernel Parameters (passing device tree):
64 * r3: pointer to the fdt
65 * r4: 0
66 * r5: 0
67 * r6: epapr magic
68 * r7: size of IMA in bytes
69 * r8: 0
70 * r9: 0
71 */
Kumar Galaffccb572008-10-21 17:25:46 -050072 debug (" Booting using OF flat tree...\n");
Heiko Schochercd1bd4c2009-08-12 10:17:03 +020073 WATCHDOG_RESET ();
Kumar Galaffccb572008-10-21 17:25:46 -050074 (*kernel) ((bd_t *)of_flat_tree, 0, 0, EPAPR_MAGIC,
Grant Likely26396382011-03-28 09:58:43 +000075 getenv_bootm_mapsize(), 0, 0);
Kumar Galaffccb572008-10-21 17:25:46 -050076 /* does not return */
77 } else
78#endif
79 {
80 /*
81 * Linux Kernel Parameters (passing board info data):
82 * r3: ptr to board info data
83 * r4: initrd_start or 0 if no initrd
84 * r5: initrd_end - unused if r4 is 0
85 * r6: Start of command line string
86 * r7: End of command line string
87 * r8: 0
88 * r9: 0
89 */
90 ulong cmd_start = images->cmdline_start;
91 ulong cmd_end = images->cmdline_end;
92 ulong initrd_start = images->initrd_start;
93 ulong initrd_end = images->initrd_end;
94 bd_t *kbd = images->kbd;
95
96 debug (" Booting using board info...\n");
Heiko Schochercd1bd4c2009-08-12 10:17:03 +020097 WATCHDOG_RESET ();
Kumar Galaffccb572008-10-21 17:25:46 -050098 (*kernel) (kbd, initrd_start, initrd_end,
99 cmd_start, cmd_end, 0, 0);
100 /* does not return */
101 }
102 return ;
103}
104
Kumar Galab02d7222008-10-16 21:52:08 -0500105void arch_lmb_reserve(struct lmb *lmb)
Marian Balakowicz2437cf22008-01-08 18:11:43 +0100106{
Becky Bruced26d67c2008-06-09 20:37:18 -0500107 phys_size_t bootm_size;
Kumar Galab02d7222008-10-16 21:52:08 -0500108 ulong size, sp, bootmap_base;
Kumar Gala93467bc2008-08-15 08:24:36 -0500109
Kumar Galac5bacfd2008-02-27 21:51:50 -0600110 bootmap_base = getenv_bootm_low();
Becky Bruced26d67c2008-06-09 20:37:18 -0500111 bootm_size = getenv_bootm_size();
Kumar Galac5bacfd2008-02-27 21:51:50 -0600112
113#ifdef DEBUG
Becky Bruced26d67c2008-06-09 20:37:18 -0500114 if (((u64)bootmap_base + bootm_size) >
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200115 (CONFIG_SYS_SDRAM_BASE + (u64)gd->ram_size))
Kumar Galac5bacfd2008-02-27 21:51:50 -0600116 puts("WARNING: bootm_low + bootm_size exceed total memory\n");
Becky Bruced26d67c2008-06-09 20:37:18 -0500117 if ((bootmap_base + bootm_size) > get_effective_memsize())
Kumar Galac5bacfd2008-02-27 21:51:50 -0600118 puts("WARNING: bootm_low + bootm_size exceed eff. memory\n");
119#endif
120
Becky Bruced26d67c2008-06-09 20:37:18 -0500121 size = min(bootm_size, get_effective_memsize());
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200122 size = min(size, CONFIG_SYS_LINUX_LOWMEM_MAX_SIZE);
Kumar Galac5bacfd2008-02-27 21:51:50 -0600123
Becky Bruced26d67c2008-06-09 20:37:18 -0500124 if (size < bootm_size) {
Kumar Galac5bacfd2008-02-27 21:51:50 -0600125 ulong base = bootmap_base + size;
Andrew Klossnere4ad4542008-07-07 06:41:14 -0700126 printf("WARNING: adjusting available memory to %lx\n", size);
Becky Bruced26d67c2008-06-09 20:37:18 -0500127 lmb_reserve(lmb, base, bootm_size - size);
Kumar Galac5bacfd2008-02-27 21:51:50 -0600128 }
Kumar Galab937bb72008-02-27 21:51:49 -0600129
Marian Balakowicz2437cf22008-01-08 18:11:43 +0100130 /*
131 * Booting a (Linux) kernel image
132 *
133 * Allocate space for command line and board info - the
134 * address should be as high as possible within the reach of
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200135 * the kernel (see CONFIG_SYS_BOOTMAPSZ settings), but in unused
Marian Balakowicz2437cf22008-01-08 18:11:43 +0100136 * memory, which means far enough below the current stack
137 * pointer.
138 */
Marian Balakowicz2efc2642008-01-31 13:58:13 +0100139 sp = get_sp();
Kumar Galac5bacfd2008-02-27 21:51:50 -0600140 debug ("## Current stack ends at 0x%08lx\n", sp);
Marian Balakowicz2437cf22008-01-08 18:11:43 +0100141
Norbert van Bolhuis58cce9e2010-03-19 15:34:25 +0100142 /* adjust sp by 4K to be safe */
143 sp -= 4096;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200144 lmb_reserve(lmb, sp, (CONFIG_SYS_SDRAM_BASE + get_effective_memsize() - sp));
Marian Balakowicz2437cf22008-01-08 18:11:43 +0100145
Kumar Gala365024c2011-01-31 15:51:20 -0600146#ifdef CONFIG_MP
147 cpu_mp_lmb_reserve(lmb);
148#endif
149
Kumar Galab02d7222008-10-16 21:52:08 -0500150 return ;
151}
152
Kumar Gala180c5812011-12-07 04:42:58 +0000153static void boot_prep_linux(bootm_headers_t *images)
154{
155#ifdef CONFIG_MP
156 /*
157 * if we are MP make sure to flush the device tree so any changes are
158 * made visibile to all other cores. In AMP boot scenarios the cores
159 * might not be HW cache coherent with each other.
160 */
161 flush_cache((unsigned long)images->ft_addr, images->ft_len);
162#endif
163}
164
Kumar Galaffccb572008-10-21 17:25:46 -0500165static int boot_cmdline_linux(bootm_headers_t *images)
166{
Kumar Galaffccb572008-10-21 17:25:46 -0500167 ulong of_size = images->ft_len;
168 struct lmb *lmb = &images->lmb;
169 ulong *cmd_start = &images->cmdline_start;
170 ulong *cmd_end = &images->cmdline_end;
Kumar Galab02d7222008-10-16 21:52:08 -0500171
Kumar Galaffccb572008-10-21 17:25:46 -0500172 int ret = 0;
Kumar Galab02d7222008-10-16 21:52:08 -0500173
Kumar Galaa56d7d52008-02-27 21:51:44 -0600174 if (!of_size) {
175 /* allocate space and init command line */
Grant Likelydfff7a22011-03-28 09:58:34 +0000176 ret = boot_get_cmdline (lmb, cmd_start, cmd_end);
Kumar Galab937bb72008-02-27 21:51:49 -0600177 if (ret) {
178 puts("ERROR with allocation of cmdline\n");
Kumar Galaffccb572008-10-21 17:25:46 -0500179 return ret;
Kumar Galab937bb72008-02-27 21:51:49 -0600180 }
Kumar Galaffccb572008-10-21 17:25:46 -0500181 }
182
183 return ret;
184}
185
186static int boot_bd_t_linux(bootm_headers_t *images)
187{
Kumar Galaffccb572008-10-21 17:25:46 -0500188 ulong of_size = images->ft_len;
189 struct lmb *lmb = &images->lmb;
190 bd_t **kbd = &images->kbd;
191
192 int ret = 0;
Kumar Galaa56d7d52008-02-27 21:51:44 -0600193
Kumar Galaffccb572008-10-21 17:25:46 -0500194 if (!of_size) {
Kumar Galaa56d7d52008-02-27 21:51:44 -0600195 /* allocate space for kernel copy of board info */
Grant Likelydfff7a22011-03-28 09:58:34 +0000196 ret = boot_get_kbd (lmb, kbd);
Kumar Galab937bb72008-02-27 21:51:49 -0600197 if (ret) {
198 puts("ERROR with allocation of kernel bd\n");
Kumar Galaffccb572008-10-21 17:25:46 -0500199 return ret;
Kumar Galab937bb72008-02-27 21:51:49 -0600200 }
Kumar Galaffccb572008-10-21 17:25:46 -0500201 set_clocks_in_mhz(*kbd);
Kumar Galaa56d7d52008-02-27 21:51:44 -0600202 }
Marian Balakowicz2437cf22008-01-08 18:11:43 +0100203
Kumar Galaffccb572008-10-21 17:25:46 -0500204 return ret;
205}
206
207static int boot_body_linux(bootm_headers_t *images)
208{
Kumar Galaffccb572008-10-21 17:25:46 -0500209 int ret;
210
Kumar Galaffccb572008-10-21 17:25:46 -0500211 /* allocate space for kernel copy of board info */
212 ret = boot_bd_t_linux(images);
213 if (ret)
214 return ret;
215
Simon Glassd4337d52013-05-08 08:06:04 +0000216 ret = image_setup_linux(images);
Kumar Galaffccb572008-10-21 17:25:46 -0500217 if (ret)
218 return ret;
Marian Balakowicz2437cf22008-01-08 18:11:43 +0100219
Kumar Galaffccb572008-10-21 17:25:46 -0500220 return 0;
221}
Kumar Gala56bdf1d2008-02-27 21:51:45 -0600222
Kim Phillipsa1b457a2012-10-29 13:34:23 +0000223noinline
Wolfgang Denk6262d0212010-06-28 22:00:46 +0200224int do_bootm_linux(int flag, int argc, char * const argv[], bootm_headers_t *images)
Kumar Galaffccb572008-10-21 17:25:46 -0500225{
226 int ret;
Marian Balakowicz2437cf22008-01-08 18:11:43 +0100227
Kumar Galaffccb572008-10-21 17:25:46 -0500228 if (flag & BOOTM_STATE_OS_CMDLINE) {
229 boot_cmdline_linux(images);
230 return 0;
231 }
Marian Balakowicz2437cf22008-01-08 18:11:43 +0100232
Kumar Galaffccb572008-10-21 17:25:46 -0500233 if (flag & BOOTM_STATE_OS_BD_T) {
234 boot_bd_t_linux(images);
235 return 0;
236 }
Marian Balakowicz2437cf22008-01-08 18:11:43 +0100237
Kumar Gala180c5812011-12-07 04:42:58 +0000238 if (flag & BOOTM_STATE_OS_PREP) {
239 boot_prep_linux(images);
Kumar Galaffccb572008-10-21 17:25:46 -0500240 return 0;
Kumar Gala180c5812011-12-07 04:42:58 +0000241 }
Kumar Galadcdcfea2008-08-15 08:24:31 -0500242
Kumar Gala180c5812011-12-07 04:42:58 +0000243 boot_prep_linux(images);
Kumar Galaffccb572008-10-21 17:25:46 -0500244 ret = boot_body_linux(images);
245 if (ret)
246 return ret;
247 boot_jump_linux(images);
Kumar Gala18f4c0f2008-02-27 21:51:46 -0600248
Kumar Galaffccb572008-10-21 17:25:46 -0500249 return 0;
Marian Balakowicz2437cf22008-01-08 18:11:43 +0100250}
Marian Balakowicz28fb6152008-01-31 13:20:08 +0100251
Marian Balakowicz9c701e82008-01-31 13:57:17 +0100252static ulong get_sp (void)
253{
254 ulong sp;
255
256 asm( "mr %0,1": "=r"(sp) : );
257 return sp;
258}
259
Marian Balakowicz2efc2642008-01-31 13:58:13 +0100260static void set_clocks_in_mhz (bd_t *kbd)
261{
262 char *s;
263
264 if ((s = getenv ("clocks_in_mhz")) != NULL) {
265 /* convert all clock information to MHz */
266 kbd->bi_intfreq /= 1000000L;
267 kbd->bi_busfreq /= 1000000L;
Marian Balakowicz2efc2642008-01-31 13:58:13 +0100268#if defined(CONFIG_CPM2)
269 kbd->bi_cpmfreq /= 1000000L;
270 kbd->bi_brgfreq /= 1000000L;
271 kbd->bi_sccfreq /= 1000000L;
Wolfgang Denk8f702ad2008-03-26 11:48:46 +0100272 kbd->bi_vco /= 1000000L;
Marian Balakowicz2efc2642008-01-31 13:58:13 +0100273#endif
274#if defined(CONFIG_MPC5xxx)
275 kbd->bi_ipbfreq /= 1000000L;
276 kbd->bi_pcifreq /= 1000000L;
277#endif /* CONFIG_MPC5xxx */
278 }
Marian Balakowicz351d3e32008-02-27 11:02:26 +0100279}
Miao Yan1bd54562013-11-28 17:51:38 +0800280
281#if defined(CONFIG_BOOTM_VXWORKS)
282void boot_prep_vxworks(bootm_headers_t *images)
283{
284#if defined(CONFIG_OF_LIBFDT)
285 int off;
286 u64 base, size;
287
288 if (!images->ft_addr)
289 return;
290
291 base = (u64)gd->bd->bi_memstart;
292 size = (u64)gd->bd->bi_memsize;
293
294 off = fdt_path_offset(images->ft_addr, "/memory");
295 if (off < 0)
296 fdt_fixup_memory(images->ft_addr, base, size);
297
298#if defined(CONFIG_MP)
299#if defined(CONFIG_MPC85xx)
300 ft_fixup_cpu(images->ft_addr, base + size);
301 ft_fixup_num_cores(images->ft_addr);
302#elif defined(CONFIG_MPC86xx)
303 off = fdt_add_mem_rsv(images->ft_addr,
304 determine_mp_bootpg(NULL), (u64)4096);
305 if (off < 0)
306 printf("## WARNING %s: %s\n", __func__, fdt_strerror(off));
307 ft_fixup_num_cores(images->ft_addr);
308#endif
309 flush_cache((unsigned long)images->ft_addr, images->ft_len);
310#endif
311#endif
312}
313
314void boot_jump_vxworks(bootm_headers_t *images)
315{
316 /* PowerPC VxWorks boot interface conforms to the ePAPR standard
317 * general purpuse registers:
318 *
319 * r3: Effective address of the device tree image
320 * r4: 0
321 * r5: 0
322 * r6: ePAPR magic value
323 * r7: shall be the size of the boot IMA in bytes
324 * r8: 0
325 * r9: 0
326 * TCR: WRC = 0, no watchdog timer reset will occur
327 */
328 WATCHDOG_RESET();
329
330 ((void (*)(void *, ulong, ulong, ulong,
331 ulong, ulong, ulong))images->ep)(images->ft_addr,
332 0, 0, EPAPR_MAGIC, getenv_bootm_mapsize(), 0, 0);
333}
334#endif