xref: /openbmc/u-boot/arch/powerpc/lib/bootm.c (revision e8f80a5a)
1*83d290c5STom Rini // SPDX-License-Identifier: GPL-2.0+
2a47a12beSStefan Roese /*
3a47a12beSStefan Roese  * (C) Copyright 2008 Semihalf
4a47a12beSStefan Roese  *
5a47a12beSStefan Roese  * (C) Copyright 2000-2006
6a47a12beSStefan Roese  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
7a47a12beSStefan Roese  */
8a47a12beSStefan Roese 
9a47a12beSStefan Roese 
10a47a12beSStefan Roese #include <common.h>
11a47a12beSStefan Roese #include <watchdog.h>
12a47a12beSStefan Roese #include <command.h>
13a47a12beSStefan Roese #include <image.h>
14a47a12beSStefan Roese #include <malloc.h>
15a47a12beSStefan Roese #include <u-boot/zlib.h>
16a47a12beSStefan Roese #include <bzlib.h>
17a47a12beSStefan Roese #include <environment.h>
18a47a12beSStefan Roese #include <asm/byteorder.h>
19561e710aSKumar Gala #include <asm/mp.h>
2008dd988bSChristophe Leroy #include <bootm.h>
2108dd988bSChristophe Leroy #include <vxworks.h>
22a47a12beSStefan Roese 
23a47a12beSStefan Roese #if defined(CONFIG_OF_LIBFDT)
24b08c8c48SMasahiro Yamada #include <linux/libfdt.h>
25a47a12beSStefan Roese #include <fdt_support.h>
26a47a12beSStefan Roese #endif
27a47a12beSStefan Roese 
28a47a12beSStefan Roese #ifdef CONFIG_SYS_INIT_RAM_LOCK
29a47a12beSStefan Roese #include <asm/cache.h>
30a47a12beSStefan Roese #endif
31a47a12beSStefan Roese 
32a47a12beSStefan Roese DECLARE_GLOBAL_DATA_PTR;
33a47a12beSStefan Roese 
34a47a12beSStefan Roese static ulong get_sp (void);
35871a57bbSMiao Yan extern void ft_fixup_num_cores(void *blob);
36a47a12beSStefan Roese static void set_clocks_in_mhz (bd_t *kbd);
37a47a12beSStefan Roese 
38a47a12beSStefan Roese #ifndef CONFIG_SYS_LINUX_LOWMEM_MAX_SIZE
39a47a12beSStefan Roese #define CONFIG_SYS_LINUX_LOWMEM_MAX_SIZE	(768*1024*1024)
40a47a12beSStefan Roese #endif
41a47a12beSStefan Roese 
boot_jump_linux(bootm_headers_t * images)42a47a12beSStefan Roese static void boot_jump_linux(bootm_headers_t *images)
43a47a12beSStefan Roese {
44a47a12beSStefan Roese 	void	(*kernel)(bd_t *, ulong r4, ulong r5, ulong r6,
45a47a12beSStefan Roese 			  ulong r7, ulong r8, ulong r9);
46a47a12beSStefan Roese #ifdef CONFIG_OF_LIBFDT
47a47a12beSStefan Roese 	char *of_flat_tree = images->ft_addr;
48a47a12beSStefan Roese #endif
49a47a12beSStefan Roese 
50a47a12beSStefan Roese 	kernel = (void (*)(bd_t *, ulong, ulong, ulong,
51a47a12beSStefan Roese 			   ulong, ulong, ulong))images->ep;
52a47a12beSStefan Roese 	debug ("## Transferring control to Linux (at address %08lx) ...\n",
53a47a12beSStefan Roese 		(ulong)kernel);
54a47a12beSStefan Roese 
55770605e4SSimon Glass 	bootstage_mark(BOOTSTAGE_ID_RUN_OS);
56a47a12beSStefan Roese 
57b892465dSMela Custodio #ifdef CONFIG_BOOTSTAGE_FDT
58b892465dSMela Custodio 	bootstage_fdt_add_report();
59b892465dSMela Custodio #endif
60b892465dSMela Custodio #ifdef CONFIG_BOOTSTAGE_REPORT
61b892465dSMela Custodio 	bootstage_report();
62b892465dSMela Custodio #endif
63b892465dSMela Custodio 
64a47a12beSStefan Roese #if defined(CONFIG_SYS_INIT_RAM_LOCK) && !defined(CONFIG_E500)
65a47a12beSStefan Roese 	unlock_ram_in_cache();
66a47a12beSStefan Roese #endif
67a47a12beSStefan Roese 
68a47a12beSStefan Roese #if defined(CONFIG_OF_LIBFDT)
69a47a12beSStefan Roese 	if (of_flat_tree) {	/* device tree; boot new style */
70a47a12beSStefan Roese 		/*
71a47a12beSStefan Roese 		 * Linux Kernel Parameters (passing device tree):
72a47a12beSStefan Roese 		 *   r3: pointer to the fdt
73a47a12beSStefan Roese 		 *   r4: 0
74a47a12beSStefan Roese 		 *   r5: 0
75a47a12beSStefan Roese 		 *   r6: epapr magic
76a47a12beSStefan Roese 		 *   r7: size of IMA in bytes
77a47a12beSStefan Roese 		 *   r8: 0
78a47a12beSStefan Roese 		 *   r9: 0
79a47a12beSStefan Roese 		 */
80a47a12beSStefan Roese 		debug ("   Booting using OF flat tree...\n");
81a47a12beSStefan Roese 		WATCHDOG_RESET ();
82a47a12beSStefan Roese 		(*kernel) ((bd_t *)of_flat_tree, 0, 0, EPAPR_MAGIC,
83723806ccSSimon Glass 			   env_get_bootm_mapsize(), 0, 0);
84a47a12beSStefan Roese 		/* does not return */
85a47a12beSStefan Roese 	} else
86a47a12beSStefan Roese #endif
87a47a12beSStefan Roese 	{
88a47a12beSStefan Roese 		/*
89a47a12beSStefan Roese 		 * Linux Kernel Parameters (passing board info data):
90a47a12beSStefan Roese 		 *   r3: ptr to board info data
91a47a12beSStefan Roese 		 *   r4: initrd_start or 0 if no initrd
92a47a12beSStefan Roese 		 *   r5: initrd_end - unused if r4 is 0
93a47a12beSStefan Roese 		 *   r6: Start of command line string
94a47a12beSStefan Roese 		 *   r7: End   of command line string
95a47a12beSStefan Roese 		 *   r8: 0
96a47a12beSStefan Roese 		 *   r9: 0
97a47a12beSStefan Roese 		 */
98a47a12beSStefan Roese 		ulong cmd_start = images->cmdline_start;
99a47a12beSStefan Roese 		ulong cmd_end = images->cmdline_end;
100a47a12beSStefan Roese 		ulong initrd_start = images->initrd_start;
101a47a12beSStefan Roese 		ulong initrd_end = images->initrd_end;
102a47a12beSStefan Roese 		bd_t *kbd = images->kbd;
103a47a12beSStefan Roese 
104a47a12beSStefan Roese 		debug ("   Booting using board info...\n");
105a47a12beSStefan Roese 		WATCHDOG_RESET ();
106a47a12beSStefan Roese 		(*kernel) (kbd, initrd_start, initrd_end,
107a47a12beSStefan Roese 			   cmd_start, cmd_end, 0, 0);
108a47a12beSStefan Roese 		/* does not return */
109a47a12beSStefan Roese 	}
110a47a12beSStefan Roese 	return ;
111a47a12beSStefan Roese }
112a47a12beSStefan Roese 
arch_lmb_reserve(struct lmb * lmb)113a47a12beSStefan Roese void arch_lmb_reserve(struct lmb *lmb)
114a47a12beSStefan Roese {
115a47a12beSStefan Roese 	phys_size_t bootm_size;
116a47a12beSStefan Roese 	ulong size, sp, bootmap_base;
117a47a12beSStefan Roese 
118723806ccSSimon Glass 	bootmap_base = env_get_bootm_low();
119723806ccSSimon Glass 	bootm_size = env_get_bootm_size();
120a47a12beSStefan Roese 
121a47a12beSStefan Roese #ifdef DEBUG
122a47a12beSStefan Roese 	if (((u64)bootmap_base + bootm_size) >
123a47a12beSStefan Roese 	    (CONFIG_SYS_SDRAM_BASE + (u64)gd->ram_size))
124a47a12beSStefan Roese 		puts("WARNING: bootm_low + bootm_size exceed total memory\n");
125a47a12beSStefan Roese 	if ((bootmap_base + bootm_size) > get_effective_memsize())
126a47a12beSStefan Roese 		puts("WARNING: bootm_low + bootm_size exceed eff. memory\n");
127a47a12beSStefan Roese #endif
128a47a12beSStefan Roese 
129a47a12beSStefan Roese 	size = min(bootm_size, get_effective_memsize());
130b4141195SMasahiro Yamada 	size = min(size, (ulong)CONFIG_SYS_LINUX_LOWMEM_MAX_SIZE);
131a47a12beSStefan Roese 
132a47a12beSStefan Roese 	if (size < bootm_size) {
133a47a12beSStefan Roese 		ulong base = bootmap_base + size;
134a47a12beSStefan Roese 		printf("WARNING: adjusting available memory to %lx\n", size);
135a47a12beSStefan Roese 		lmb_reserve(lmb, base, bootm_size - size);
136a47a12beSStefan Roese 	}
137a47a12beSStefan Roese 
138a47a12beSStefan Roese 	/*
139a47a12beSStefan Roese 	 * Booting a (Linux) kernel image
140a47a12beSStefan Roese 	 *
141a47a12beSStefan Roese 	 * Allocate space for command line and board info - the
142a47a12beSStefan Roese 	 * address should be as high as possible within the reach of
143a47a12beSStefan Roese 	 * the kernel (see CONFIG_SYS_BOOTMAPSZ settings), but in unused
144a47a12beSStefan Roese 	 * memory, which means far enough below the current stack
145a47a12beSStefan Roese 	 * pointer.
146a47a12beSStefan Roese 	 */
147a47a12beSStefan Roese 	sp = get_sp();
148a47a12beSStefan Roese 	debug ("## Current stack ends at 0x%08lx\n", sp);
149a47a12beSStefan Roese 
1503882d7a5SNorbert van Bolhuis 	/* adjust sp by 4K to be safe */
1513882d7a5SNorbert van Bolhuis 	sp -= 4096;
152a47a12beSStefan Roese 	lmb_reserve(lmb, sp, (CONFIG_SYS_SDRAM_BASE + get_effective_memsize() - sp));
153a47a12beSStefan Roese 
154561e710aSKumar Gala #ifdef CONFIG_MP
155561e710aSKumar Gala 	cpu_mp_lmb_reserve(lmb);
156561e710aSKumar Gala #endif
157561e710aSKumar Gala 
158a47a12beSStefan Roese 	return ;
159a47a12beSStefan Roese }
160a47a12beSStefan Roese 
boot_prep_linux(bootm_headers_t * images)1613b200110SKumar Gala static void boot_prep_linux(bootm_headers_t *images)
1623b200110SKumar Gala {
1633b200110SKumar Gala #ifdef CONFIG_MP
1643b200110SKumar Gala 	/*
1653b200110SKumar Gala 	 * if we are MP make sure to flush the device tree so any changes are
1663b200110SKumar Gala 	 * made visibile to all other cores.  In AMP boot scenarios the cores
1673b200110SKumar Gala 	 * might not be HW cache coherent with each other.
1683b200110SKumar Gala 	 */
1693b200110SKumar Gala 	flush_cache((unsigned long)images->ft_addr, images->ft_len);
1703b200110SKumar Gala #endif
1713b200110SKumar Gala }
1723b200110SKumar Gala 
boot_cmdline_linux(bootm_headers_t * images)173a47a12beSStefan Roese static int boot_cmdline_linux(bootm_headers_t *images)
174a47a12beSStefan Roese {
175a47a12beSStefan Roese 	ulong of_size = images->ft_len;
176a47a12beSStefan Roese 	struct lmb *lmb = &images->lmb;
177a47a12beSStefan Roese 	ulong *cmd_start = &images->cmdline_start;
178a47a12beSStefan Roese 	ulong *cmd_end = &images->cmdline_end;
179a47a12beSStefan Roese 
180a47a12beSStefan Roese 	int ret = 0;
181a47a12beSStefan Roese 
182a47a12beSStefan Roese 	if (!of_size) {
183a47a12beSStefan Roese 		/* allocate space and init command line */
184590d3cacSGrant Likely 		ret = boot_get_cmdline (lmb, cmd_start, cmd_end);
185a47a12beSStefan Roese 		if (ret) {
186a47a12beSStefan Roese 			puts("ERROR with allocation of cmdline\n");
187a47a12beSStefan Roese 			return ret;
188a47a12beSStefan Roese 		}
189a47a12beSStefan Roese 	}
190a47a12beSStefan Roese 
191a47a12beSStefan Roese 	return ret;
192a47a12beSStefan Roese }
193a47a12beSStefan Roese 
boot_bd_t_linux(bootm_headers_t * images)194a47a12beSStefan Roese static int boot_bd_t_linux(bootm_headers_t *images)
195a47a12beSStefan Roese {
196a47a12beSStefan Roese 	ulong of_size = images->ft_len;
197a47a12beSStefan Roese 	struct lmb *lmb = &images->lmb;
198a47a12beSStefan Roese 	bd_t **kbd = &images->kbd;
199a47a12beSStefan Roese 
200a47a12beSStefan Roese 	int ret = 0;
201a47a12beSStefan Roese 
202a47a12beSStefan Roese 	if (!of_size) {
203a47a12beSStefan Roese 		/* allocate space for kernel copy of board info */
204590d3cacSGrant Likely 		ret = boot_get_kbd (lmb, kbd);
205a47a12beSStefan Roese 		if (ret) {
206a47a12beSStefan Roese 			puts("ERROR with allocation of kernel bd\n");
207a47a12beSStefan Roese 			return ret;
208a47a12beSStefan Roese 		}
209a47a12beSStefan Roese 		set_clocks_in_mhz(*kbd);
210a47a12beSStefan Roese 	}
211a47a12beSStefan Roese 
212a47a12beSStefan Roese 	return ret;
213a47a12beSStefan Roese }
214a47a12beSStefan Roese 
boot_body_linux(bootm_headers_t * images)215a47a12beSStefan Roese static int boot_body_linux(bootm_headers_t *images)
216a47a12beSStefan Roese {
217a47a12beSStefan Roese 	int ret;
218a47a12beSStefan Roese 
219a47a12beSStefan Roese 	/* allocate space for kernel copy of board info */
220a47a12beSStefan Roese 	ret = boot_bd_t_linux(images);
221a47a12beSStefan Roese 	if (ret)
222a47a12beSStefan Roese 		return ret;
223a47a12beSStefan Roese 
2243e51266aSSimon Glass 	ret = image_setup_linux(images);
225a47a12beSStefan Roese 	if (ret)
226a47a12beSStefan Roese 		return ret;
227a47a12beSStefan Roese 
228a47a12beSStefan Roese 	return 0;
229a47a12beSStefan Roese }
230a47a12beSStefan Roese 
231eef1cf2dSKim Phillips noinline
do_bootm_linux(int flag,int argc,char * const argv[],bootm_headers_t * images)23254841ab5SWolfgang Denk int do_bootm_linux(int flag, int argc, char * const argv[], bootm_headers_t *images)
233a47a12beSStefan Roese {
234a47a12beSStefan Roese 	int	ret;
235a47a12beSStefan Roese 
236a47a12beSStefan Roese 	if (flag & BOOTM_STATE_OS_CMDLINE) {
237a47a12beSStefan Roese 		boot_cmdline_linux(images);
238a47a12beSStefan Roese 		return 0;
239a47a12beSStefan Roese 	}
240a47a12beSStefan Roese 
241a47a12beSStefan Roese 	if (flag & BOOTM_STATE_OS_BD_T) {
242a47a12beSStefan Roese 		boot_bd_t_linux(images);
243a47a12beSStefan Roese 		return 0;
244a47a12beSStefan Roese 	}
245a47a12beSStefan Roese 
2463b200110SKumar Gala 	if (flag & BOOTM_STATE_OS_PREP) {
2473b200110SKumar Gala 		boot_prep_linux(images);
248a47a12beSStefan Roese 		return 0;
2493b200110SKumar Gala 	}
250a47a12beSStefan Roese 
2513b200110SKumar Gala 	boot_prep_linux(images);
252a47a12beSStefan Roese 	ret = boot_body_linux(images);
253a47a12beSStefan Roese 	if (ret)
254a47a12beSStefan Roese 		return ret;
255a47a12beSStefan Roese 	boot_jump_linux(images);
256a47a12beSStefan Roese 
257a47a12beSStefan Roese 	return 0;
258a47a12beSStefan Roese }
259a47a12beSStefan Roese 
get_sp(void)260a47a12beSStefan Roese static ulong get_sp (void)
261a47a12beSStefan Roese {
262a47a12beSStefan Roese 	ulong sp;
263a47a12beSStefan Roese 
264a47a12beSStefan Roese 	asm( "mr %0,1": "=r"(sp) : );
265a47a12beSStefan Roese 	return sp;
266a47a12beSStefan Roese }
267a47a12beSStefan Roese 
set_clocks_in_mhz(bd_t * kbd)268a47a12beSStefan Roese static void set_clocks_in_mhz (bd_t *kbd)
269a47a12beSStefan Roese {
270a47a12beSStefan Roese 	char	*s;
271a47a12beSStefan Roese 
27200caae6dSSimon Glass 	s = env_get("clocks_in_mhz");
27300caae6dSSimon Glass 	if (s) {
274a47a12beSStefan Roese 		/* convert all clock information to MHz */
275a47a12beSStefan Roese 		kbd->bi_intfreq /= 1000000L;
276a47a12beSStefan Roese 		kbd->bi_busfreq /= 1000000L;
277a47a12beSStefan Roese #if defined(CONFIG_CPM2)
278a47a12beSStefan Roese 		kbd->bi_cpmfreq /= 1000000L;
279a47a12beSStefan Roese 		kbd->bi_brgfreq /= 1000000L;
280a47a12beSStefan Roese 		kbd->bi_sccfreq /= 1000000L;
281a47a12beSStefan Roese 		kbd->bi_vco	/= 1000000L;
282a47a12beSStefan Roese #endif
283a47a12beSStefan Roese 	}
284a47a12beSStefan Roese }
285871a57bbSMiao Yan 
286871a57bbSMiao Yan #if defined(CONFIG_BOOTM_VXWORKS)
boot_prep_vxworks(bootm_headers_t * images)287871a57bbSMiao Yan void boot_prep_vxworks(bootm_headers_t *images)
288871a57bbSMiao Yan {
289871a57bbSMiao Yan #if defined(CONFIG_OF_LIBFDT)
290871a57bbSMiao Yan 	int off;
291871a57bbSMiao Yan 	u64 base, size;
292871a57bbSMiao Yan 
293871a57bbSMiao Yan 	if (!images->ft_addr)
294871a57bbSMiao Yan 		return;
295871a57bbSMiao Yan 
296871a57bbSMiao Yan 	base = (u64)gd->bd->bi_memstart;
297871a57bbSMiao Yan 	size = (u64)gd->bd->bi_memsize;
298871a57bbSMiao Yan 
299871a57bbSMiao Yan 	off = fdt_path_offset(images->ft_addr, "/memory");
300871a57bbSMiao Yan 	if (off < 0)
301871a57bbSMiao Yan 		fdt_fixup_memory(images->ft_addr, base, size);
302871a57bbSMiao Yan 
303871a57bbSMiao Yan #if defined(CONFIG_MP)
304871a57bbSMiao Yan #if defined(CONFIG_MPC85xx)
305871a57bbSMiao Yan 	ft_fixup_cpu(images->ft_addr, base + size);
306871a57bbSMiao Yan 	ft_fixup_num_cores(images->ft_addr);
307871a57bbSMiao Yan #elif defined(CONFIG_MPC86xx)
308871a57bbSMiao Yan 	off = fdt_add_mem_rsv(images->ft_addr,
309871a57bbSMiao Yan 			determine_mp_bootpg(NULL), (u64)4096);
310871a57bbSMiao Yan 	if (off < 0)
311871a57bbSMiao Yan 		printf("## WARNING %s: %s\n", __func__, fdt_strerror(off));
312871a57bbSMiao Yan 	ft_fixup_num_cores(images->ft_addr);
313871a57bbSMiao Yan #endif
314871a57bbSMiao Yan 	flush_cache((unsigned long)images->ft_addr, images->ft_len);
315871a57bbSMiao Yan #endif
316871a57bbSMiao Yan #endif
317871a57bbSMiao Yan }
318871a57bbSMiao Yan 
boot_jump_vxworks(bootm_headers_t * images)319871a57bbSMiao Yan void boot_jump_vxworks(bootm_headers_t *images)
320871a57bbSMiao Yan {
321871a57bbSMiao Yan 	/* PowerPC VxWorks boot interface conforms to the ePAPR standard
322871a57bbSMiao Yan 	 * general purpuse registers:
323871a57bbSMiao Yan 	 *
324871a57bbSMiao Yan 	 *	r3: Effective address of the device tree image
325871a57bbSMiao Yan 	 *	r4: 0
326871a57bbSMiao Yan 	 *	r5: 0
327871a57bbSMiao Yan 	 *	r6: ePAPR magic value
328871a57bbSMiao Yan 	 *	r7: shall be the size of the boot IMA in bytes
329871a57bbSMiao Yan 	 *	r8: 0
330871a57bbSMiao Yan 	 *	r9: 0
331871a57bbSMiao Yan 	 *	TCR: WRC = 0, no watchdog timer reset will occur
332871a57bbSMiao Yan 	 */
333871a57bbSMiao Yan 	WATCHDOG_RESET();
334871a57bbSMiao Yan 
335871a57bbSMiao Yan 	((void (*)(void *, ulong, ulong, ulong,
336871a57bbSMiao Yan 		ulong, ulong, ulong))images->ep)(images->ft_addr,
337723806ccSSimon Glass 		0, 0, EPAPR_MAGIC, env_get_bootm_mapsize(), 0, 0);
338871a57bbSMiao Yan }
339871a57bbSMiao Yan #endif
340