xref: /openbmc/u-boot/arch/powerpc/lib/bootm.c (revision 3e51266a4e530026c002e210754c737a0caaa050)
1a47a12beSStefan Roese /*
2a47a12beSStefan Roese  * (C) Copyright 2008 Semihalf
3a47a12beSStefan Roese  *
4a47a12beSStefan Roese  * (C) Copyright 2000-2006
5a47a12beSStefan Roese  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
6a47a12beSStefan Roese  *
7a47a12beSStefan Roese  * See file CREDITS for list of people who contributed to this
8a47a12beSStefan Roese  * project.
9a47a12beSStefan Roese  *
10a47a12beSStefan Roese  * This program is free software; you can redistribute it and/or
11a47a12beSStefan Roese  * modify it under the terms of the GNU General Public License as
12a47a12beSStefan Roese  * published by the Free Software Foundation; either version 2 of
13a47a12beSStefan Roese  * the License, or (at your option) any later version.
14a47a12beSStefan Roese  *
15a47a12beSStefan Roese  * This program is distributed in the hope that it will be useful,
16a47a12beSStefan Roese  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17a47a12beSStefan Roese  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18a47a12beSStefan Roese  * GNU General Public License for more details.
19a47a12beSStefan Roese  *
20a47a12beSStefan Roese  * You should have received a copy of the GNU General Public License
21a47a12beSStefan Roese  * along with this program; if not, write to the Free Software
22a47a12beSStefan Roese  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
23a47a12beSStefan Roese  * MA 02111-1307 USA
24a47a12beSStefan Roese  */
25a47a12beSStefan Roese 
26a47a12beSStefan Roese 
27a47a12beSStefan Roese #include <common.h>
28a47a12beSStefan Roese #include <watchdog.h>
29a47a12beSStefan Roese #include <command.h>
30a47a12beSStefan Roese #include <image.h>
31a47a12beSStefan Roese #include <malloc.h>
32a47a12beSStefan Roese #include <u-boot/zlib.h>
33a47a12beSStefan Roese #include <bzlib.h>
34a47a12beSStefan Roese #include <environment.h>
35a47a12beSStefan Roese #include <asm/byteorder.h>
36561e710aSKumar Gala #include <asm/mp.h>
37a47a12beSStefan Roese 
38a47a12beSStefan Roese #if defined(CONFIG_OF_LIBFDT)
39a47a12beSStefan Roese #include <libfdt.h>
40a47a12beSStefan Roese #include <fdt_support.h>
41a47a12beSStefan Roese #endif
42a47a12beSStefan Roese 
43a47a12beSStefan Roese #ifdef CONFIG_SYS_INIT_RAM_LOCK
44a47a12beSStefan Roese #include <asm/cache.h>
45a47a12beSStefan Roese #endif
46a47a12beSStefan Roese 
47a47a12beSStefan Roese DECLARE_GLOBAL_DATA_PTR;
48a47a12beSStefan Roese 
49a47a12beSStefan Roese extern ulong get_effective_memsize(void);
50a47a12beSStefan Roese static ulong get_sp (void);
51a47a12beSStefan Roese static void set_clocks_in_mhz (bd_t *kbd);
52a47a12beSStefan Roese 
53a47a12beSStefan Roese #ifndef CONFIG_SYS_LINUX_LOWMEM_MAX_SIZE
54a47a12beSStefan Roese #define CONFIG_SYS_LINUX_LOWMEM_MAX_SIZE	(768*1024*1024)
55a47a12beSStefan Roese #endif
56a47a12beSStefan Roese 
57a47a12beSStefan Roese static void boot_jump_linux(bootm_headers_t *images)
58a47a12beSStefan Roese {
59a47a12beSStefan Roese 	void	(*kernel)(bd_t *, ulong r4, ulong r5, ulong r6,
60a47a12beSStefan Roese 			  ulong r7, ulong r8, ulong r9);
61a47a12beSStefan Roese #ifdef CONFIG_OF_LIBFDT
62a47a12beSStefan Roese 	char *of_flat_tree = images->ft_addr;
63a47a12beSStefan Roese #endif
64a47a12beSStefan Roese 
65a47a12beSStefan Roese 	kernel = (void (*)(bd_t *, ulong, ulong, ulong,
66a47a12beSStefan Roese 			   ulong, ulong, ulong))images->ep;
67a47a12beSStefan Roese 	debug ("## Transferring control to Linux (at address %08lx) ...\n",
68a47a12beSStefan Roese 		(ulong)kernel);
69a47a12beSStefan Roese 
70770605e4SSimon Glass 	bootstage_mark(BOOTSTAGE_ID_RUN_OS);
71a47a12beSStefan Roese 
72a47a12beSStefan Roese #if defined(CONFIG_SYS_INIT_RAM_LOCK) && !defined(CONFIG_E500)
73a47a12beSStefan Roese 	unlock_ram_in_cache();
74a47a12beSStefan Roese #endif
75a47a12beSStefan Roese 
76a47a12beSStefan Roese #if defined(CONFIG_OF_LIBFDT)
77a47a12beSStefan Roese 	if (of_flat_tree) {	/* device tree; boot new style */
78a47a12beSStefan Roese 		/*
79a47a12beSStefan Roese 		 * Linux Kernel Parameters (passing device tree):
80a47a12beSStefan Roese 		 *   r3: pointer to the fdt
81a47a12beSStefan Roese 		 *   r4: 0
82a47a12beSStefan Roese 		 *   r5: 0
83a47a12beSStefan Roese 		 *   r6: epapr magic
84a47a12beSStefan Roese 		 *   r7: size of IMA in bytes
85a47a12beSStefan Roese 		 *   r8: 0
86a47a12beSStefan Roese 		 *   r9: 0
87a47a12beSStefan Roese 		 */
88a47a12beSStefan Roese 		debug ("   Booting using OF flat tree...\n");
89a47a12beSStefan Roese 		WATCHDOG_RESET ();
90a47a12beSStefan Roese 		(*kernel) ((bd_t *)of_flat_tree, 0, 0, EPAPR_MAGIC,
91c3624e6eSGrant Likely 			   getenv_bootm_mapsize(), 0, 0);
92a47a12beSStefan Roese 		/* does not return */
93a47a12beSStefan Roese 	} else
94a47a12beSStefan Roese #endif
95a47a12beSStefan Roese 	{
96a47a12beSStefan Roese 		/*
97a47a12beSStefan Roese 		 * Linux Kernel Parameters (passing board info data):
98a47a12beSStefan Roese 		 *   r3: ptr to board info data
99a47a12beSStefan Roese 		 *   r4: initrd_start or 0 if no initrd
100a47a12beSStefan Roese 		 *   r5: initrd_end - unused if r4 is 0
101a47a12beSStefan Roese 		 *   r6: Start of command line string
102a47a12beSStefan Roese 		 *   r7: End   of command line string
103a47a12beSStefan Roese 		 *   r8: 0
104a47a12beSStefan Roese 		 *   r9: 0
105a47a12beSStefan Roese 		 */
106a47a12beSStefan Roese 		ulong cmd_start = images->cmdline_start;
107a47a12beSStefan Roese 		ulong cmd_end = images->cmdline_end;
108a47a12beSStefan Roese 		ulong initrd_start = images->initrd_start;
109a47a12beSStefan Roese 		ulong initrd_end = images->initrd_end;
110a47a12beSStefan Roese 		bd_t *kbd = images->kbd;
111a47a12beSStefan Roese 
112a47a12beSStefan Roese 		debug ("   Booting using board info...\n");
113a47a12beSStefan Roese 		WATCHDOG_RESET ();
114a47a12beSStefan Roese 		(*kernel) (kbd, initrd_start, initrd_end,
115a47a12beSStefan Roese 			   cmd_start, cmd_end, 0, 0);
116a47a12beSStefan Roese 		/* does not return */
117a47a12beSStefan Roese 	}
118a47a12beSStefan Roese 	return ;
119a47a12beSStefan Roese }
120a47a12beSStefan Roese 
121a47a12beSStefan Roese void arch_lmb_reserve(struct lmb *lmb)
122a47a12beSStefan Roese {
123a47a12beSStefan Roese 	phys_size_t bootm_size;
124a47a12beSStefan Roese 	ulong size, sp, bootmap_base;
125a47a12beSStefan Roese 
126a47a12beSStefan Roese 	bootmap_base = getenv_bootm_low();
127a47a12beSStefan Roese 	bootm_size = getenv_bootm_size();
128a47a12beSStefan Roese 
129a47a12beSStefan Roese #ifdef DEBUG
130a47a12beSStefan Roese 	if (((u64)bootmap_base + bootm_size) >
131a47a12beSStefan Roese 	    (CONFIG_SYS_SDRAM_BASE + (u64)gd->ram_size))
132a47a12beSStefan Roese 		puts("WARNING: bootm_low + bootm_size exceed total memory\n");
133a47a12beSStefan Roese 	if ((bootmap_base + bootm_size) > get_effective_memsize())
134a47a12beSStefan Roese 		puts("WARNING: bootm_low + bootm_size exceed eff. memory\n");
135a47a12beSStefan Roese #endif
136a47a12beSStefan Roese 
137a47a12beSStefan Roese 	size = min(bootm_size, get_effective_memsize());
138a47a12beSStefan Roese 	size = min(size, CONFIG_SYS_LINUX_LOWMEM_MAX_SIZE);
139a47a12beSStefan Roese 
140a47a12beSStefan Roese 	if (size < bootm_size) {
141a47a12beSStefan Roese 		ulong base = bootmap_base + size;
142a47a12beSStefan Roese 		printf("WARNING: adjusting available memory to %lx\n", size);
143a47a12beSStefan Roese 		lmb_reserve(lmb, base, bootm_size - size);
144a47a12beSStefan Roese 	}
145a47a12beSStefan Roese 
146a47a12beSStefan Roese 	/*
147a47a12beSStefan Roese 	 * Booting a (Linux) kernel image
148a47a12beSStefan Roese 	 *
149a47a12beSStefan Roese 	 * Allocate space for command line and board info - the
150a47a12beSStefan Roese 	 * address should be as high as possible within the reach of
151a47a12beSStefan Roese 	 * the kernel (see CONFIG_SYS_BOOTMAPSZ settings), but in unused
152a47a12beSStefan Roese 	 * memory, which means far enough below the current stack
153a47a12beSStefan Roese 	 * pointer.
154a47a12beSStefan Roese 	 */
155a47a12beSStefan Roese 	sp = get_sp();
156a47a12beSStefan Roese 	debug ("## Current stack ends at 0x%08lx\n", sp);
157a47a12beSStefan Roese 
1583882d7a5SNorbert van Bolhuis 	/* adjust sp by 4K to be safe */
1593882d7a5SNorbert van Bolhuis 	sp -= 4096;
160a47a12beSStefan Roese 	lmb_reserve(lmb, sp, (CONFIG_SYS_SDRAM_BASE + get_effective_memsize() - sp));
161a47a12beSStefan Roese 
162561e710aSKumar Gala #ifdef CONFIG_MP
163561e710aSKumar Gala 	cpu_mp_lmb_reserve(lmb);
164561e710aSKumar Gala #endif
165561e710aSKumar Gala 
166a47a12beSStefan Roese 	return ;
167a47a12beSStefan Roese }
168a47a12beSStefan Roese 
1693b200110SKumar Gala static void boot_prep_linux(bootm_headers_t *images)
1703b200110SKumar Gala {
1713b200110SKumar Gala #ifdef CONFIG_MP
1723b200110SKumar Gala 	/*
1733b200110SKumar Gala 	 * if we are MP make sure to flush the device tree so any changes are
1743b200110SKumar Gala 	 * made visibile to all other cores.  In AMP boot scenarios the cores
1753b200110SKumar Gala 	 * might not be HW cache coherent with each other.
1763b200110SKumar Gala 	 */
1773b200110SKumar Gala 	flush_cache((unsigned long)images->ft_addr, images->ft_len);
1783b200110SKumar Gala #endif
1793b200110SKumar Gala }
1803b200110SKumar Gala 
181a47a12beSStefan Roese static int boot_cmdline_linux(bootm_headers_t *images)
182a47a12beSStefan Roese {
183a47a12beSStefan Roese 	ulong of_size = images->ft_len;
184a47a12beSStefan Roese 	struct lmb *lmb = &images->lmb;
185a47a12beSStefan Roese 	ulong *cmd_start = &images->cmdline_start;
186a47a12beSStefan Roese 	ulong *cmd_end = &images->cmdline_end;
187a47a12beSStefan Roese 
188a47a12beSStefan Roese 	int ret = 0;
189a47a12beSStefan Roese 
190a47a12beSStefan Roese 	if (!of_size) {
191a47a12beSStefan Roese 		/* allocate space and init command line */
192590d3cacSGrant Likely 		ret = boot_get_cmdline (lmb, cmd_start, cmd_end);
193a47a12beSStefan Roese 		if (ret) {
194a47a12beSStefan Roese 			puts("ERROR with allocation of cmdline\n");
195a47a12beSStefan Roese 			return ret;
196a47a12beSStefan Roese 		}
197a47a12beSStefan Roese 	}
198a47a12beSStefan Roese 
199a47a12beSStefan Roese 	return ret;
200a47a12beSStefan Roese }
201a47a12beSStefan Roese 
202a47a12beSStefan Roese static int boot_bd_t_linux(bootm_headers_t *images)
203a47a12beSStefan Roese {
204a47a12beSStefan Roese 	ulong of_size = images->ft_len;
205a47a12beSStefan Roese 	struct lmb *lmb = &images->lmb;
206a47a12beSStefan Roese 	bd_t **kbd = &images->kbd;
207a47a12beSStefan Roese 
208a47a12beSStefan Roese 	int ret = 0;
209a47a12beSStefan Roese 
210a47a12beSStefan Roese 	if (!of_size) {
211a47a12beSStefan Roese 		/* allocate space for kernel copy of board info */
212590d3cacSGrant Likely 		ret = boot_get_kbd (lmb, kbd);
213a47a12beSStefan Roese 		if (ret) {
214a47a12beSStefan Roese 			puts("ERROR with allocation of kernel bd\n");
215a47a12beSStefan Roese 			return ret;
216a47a12beSStefan Roese 		}
217a47a12beSStefan Roese 		set_clocks_in_mhz(*kbd);
218a47a12beSStefan Roese 	}
219a47a12beSStefan Roese 
220a47a12beSStefan Roese 	return ret;
221a47a12beSStefan Roese }
222a47a12beSStefan Roese 
223a47a12beSStefan Roese static int boot_body_linux(bootm_headers_t *images)
224a47a12beSStefan Roese {
225a47a12beSStefan Roese 	int ret;
226a47a12beSStefan Roese 
227a47a12beSStefan Roese 	/* allocate space for kernel copy of board info */
228a47a12beSStefan Roese 	ret = boot_bd_t_linux(images);
229a47a12beSStefan Roese 	if (ret)
230a47a12beSStefan Roese 		return ret;
231a47a12beSStefan Roese 
232*3e51266aSSimon Glass 	ret = image_setup_linux(images);
233a47a12beSStefan Roese 	if (ret)
234a47a12beSStefan Roese 		return ret;
235a47a12beSStefan Roese 
236a47a12beSStefan Roese 	return 0;
237a47a12beSStefan Roese }
238a47a12beSStefan Roese 
239eef1cf2dSKim Phillips noinline
24054841ab5SWolfgang Denk int do_bootm_linux(int flag, int argc, char * const argv[], bootm_headers_t *images)
241a47a12beSStefan Roese {
242a47a12beSStefan Roese 	int	ret;
243a47a12beSStefan Roese 
244a47a12beSStefan Roese 	if (flag & BOOTM_STATE_OS_CMDLINE) {
245a47a12beSStefan Roese 		boot_cmdline_linux(images);
246a47a12beSStefan Roese 		return 0;
247a47a12beSStefan Roese 	}
248a47a12beSStefan Roese 
249a47a12beSStefan Roese 	if (flag & BOOTM_STATE_OS_BD_T) {
250a47a12beSStefan Roese 		boot_bd_t_linux(images);
251a47a12beSStefan Roese 		return 0;
252a47a12beSStefan Roese 	}
253a47a12beSStefan Roese 
2543b200110SKumar Gala 	if (flag & BOOTM_STATE_OS_PREP) {
2553b200110SKumar Gala 		boot_prep_linux(images);
256a47a12beSStefan Roese 		return 0;
2573b200110SKumar Gala 	}
258a47a12beSStefan Roese 
259a47a12beSStefan Roese 	if (flag & BOOTM_STATE_OS_GO) {
260a47a12beSStefan Roese 		boot_jump_linux(images);
261a47a12beSStefan Roese 		return 0;
262a47a12beSStefan Roese 	}
263a47a12beSStefan Roese 
2643b200110SKumar Gala 	boot_prep_linux(images);
265a47a12beSStefan Roese 	ret = boot_body_linux(images);
266a47a12beSStefan Roese 	if (ret)
267a47a12beSStefan Roese 		return ret;
268a47a12beSStefan Roese 	boot_jump_linux(images);
269a47a12beSStefan Roese 
270a47a12beSStefan Roese 	return 0;
271a47a12beSStefan Roese }
272a47a12beSStefan Roese 
273a47a12beSStefan Roese static ulong get_sp (void)
274a47a12beSStefan Roese {
275a47a12beSStefan Roese 	ulong sp;
276a47a12beSStefan Roese 
277a47a12beSStefan Roese 	asm( "mr %0,1": "=r"(sp) : );
278a47a12beSStefan Roese 	return sp;
279a47a12beSStefan Roese }
280a47a12beSStefan Roese 
281a47a12beSStefan Roese static void set_clocks_in_mhz (bd_t *kbd)
282a47a12beSStefan Roese {
283a47a12beSStefan Roese 	char	*s;
284a47a12beSStefan Roese 
285a47a12beSStefan Roese 	if ((s = getenv ("clocks_in_mhz")) != NULL) {
286a47a12beSStefan Roese 		/* convert all clock information to MHz */
287a47a12beSStefan Roese 		kbd->bi_intfreq /= 1000000L;
288a47a12beSStefan Roese 		kbd->bi_busfreq /= 1000000L;
289a47a12beSStefan Roese #if defined(CONFIG_MPC8220)
290a47a12beSStefan Roese 		kbd->bi_inpfreq /= 1000000L;
291a47a12beSStefan Roese 		kbd->bi_pcifreq /= 1000000L;
292a47a12beSStefan Roese 		kbd->bi_pevfreq /= 1000000L;
293a47a12beSStefan Roese 		kbd->bi_flbfreq /= 1000000L;
294a47a12beSStefan Roese 		kbd->bi_vcofreq /= 1000000L;
295a47a12beSStefan Roese #endif
296a47a12beSStefan Roese #if defined(CONFIG_CPM2)
297a47a12beSStefan Roese 		kbd->bi_cpmfreq /= 1000000L;
298a47a12beSStefan Roese 		kbd->bi_brgfreq /= 1000000L;
299a47a12beSStefan Roese 		kbd->bi_sccfreq /= 1000000L;
300a47a12beSStefan Roese 		kbd->bi_vco	/= 1000000L;
301a47a12beSStefan Roese #endif
302a47a12beSStefan Roese #if defined(CONFIG_MPC5xxx)
303a47a12beSStefan Roese 		kbd->bi_ipbfreq /= 1000000L;
304a47a12beSStefan Roese 		kbd->bi_pcifreq /= 1000000L;
305a47a12beSStefan Roese #endif /* CONFIG_MPC5xxx */
306a47a12beSStefan Roese 	}
307a47a12beSStefan Roese }
308