xref: /openbmc/u-boot/arch/powerpc/lib/bootm.c (revision 54841ab50c20d6fa6c9cc3eb826989da3a22d934)
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>
36a47a12beSStefan Roese 
37a47a12beSStefan Roese #if defined(CONFIG_OF_LIBFDT)
38a47a12beSStefan Roese #include <fdt.h>
39a47a12beSStefan Roese #include <libfdt.h>
40a47a12beSStefan Roese #include <fdt_support.h>
41a47a12beSStefan Roese 
42a47a12beSStefan Roese #endif
43a47a12beSStefan Roese 
44a47a12beSStefan Roese #ifdef CONFIG_SYS_INIT_RAM_LOCK
45a47a12beSStefan Roese #include <asm/cache.h>
46a47a12beSStefan Roese #endif
47a47a12beSStefan Roese 
48a47a12beSStefan Roese DECLARE_GLOBAL_DATA_PTR;
49a47a12beSStefan Roese 
50*54841ab5SWolfgang Denk extern int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
51a47a12beSStefan Roese extern ulong get_effective_memsize(void);
52a47a12beSStefan Roese static ulong get_sp (void);
53a47a12beSStefan Roese static void set_clocks_in_mhz (bd_t *kbd);
54a47a12beSStefan Roese 
55a47a12beSStefan Roese #ifndef CONFIG_SYS_LINUX_LOWMEM_MAX_SIZE
56a47a12beSStefan Roese #define CONFIG_SYS_LINUX_LOWMEM_MAX_SIZE	(768*1024*1024)
57a47a12beSStefan Roese #endif
58a47a12beSStefan Roese 
59a47a12beSStefan Roese static void boot_jump_linux(bootm_headers_t *images)
60a47a12beSStefan Roese {
61a47a12beSStefan Roese 	void	(*kernel)(bd_t *, ulong r4, ulong r5, ulong r6,
62a47a12beSStefan Roese 			  ulong r7, ulong r8, ulong r9);
63a47a12beSStefan Roese #ifdef CONFIG_OF_LIBFDT
64a47a12beSStefan Roese 	char *of_flat_tree = images->ft_addr;
65a47a12beSStefan Roese #endif
66a47a12beSStefan Roese 
67a47a12beSStefan Roese 	kernel = (void (*)(bd_t *, ulong, ulong, ulong,
68a47a12beSStefan Roese 			   ulong, ulong, ulong))images->ep;
69a47a12beSStefan Roese 	debug ("## Transferring control to Linux (at address %08lx) ...\n",
70a47a12beSStefan Roese 		(ulong)kernel);
71a47a12beSStefan Roese 
72a47a12beSStefan Roese 	show_boot_progress (15);
73a47a12beSStefan Roese 
74a47a12beSStefan Roese #if defined(CONFIG_SYS_INIT_RAM_LOCK) && !defined(CONFIG_E500)
75a47a12beSStefan Roese 	unlock_ram_in_cache();
76a47a12beSStefan Roese #endif
77a47a12beSStefan Roese 
78a47a12beSStefan Roese #if defined(CONFIG_OF_LIBFDT)
79a47a12beSStefan Roese 	if (of_flat_tree) {	/* device tree; boot new style */
80a47a12beSStefan Roese 		/*
81a47a12beSStefan Roese 		 * Linux Kernel Parameters (passing device tree):
82a47a12beSStefan Roese 		 *   r3: pointer to the fdt
83a47a12beSStefan Roese 		 *   r4: 0
84a47a12beSStefan Roese 		 *   r5: 0
85a47a12beSStefan Roese 		 *   r6: epapr magic
86a47a12beSStefan Roese 		 *   r7: size of IMA in bytes
87a47a12beSStefan Roese 		 *   r8: 0
88a47a12beSStefan Roese 		 *   r9: 0
89a47a12beSStefan Roese 		 */
90a47a12beSStefan Roese #if defined(CONFIG_85xx) || defined(CONFIG_440)
91a47a12beSStefan Roese  #define EPAPR_MAGIC	(0x45504150)
92a47a12beSStefan Roese #else
93a47a12beSStefan Roese  #define EPAPR_MAGIC	(0x65504150)
94a47a12beSStefan Roese #endif
95a47a12beSStefan Roese 
96a47a12beSStefan Roese 		debug ("   Booting using OF flat tree...\n");
97a47a12beSStefan Roese 		WATCHDOG_RESET ();
98a47a12beSStefan Roese 		(*kernel) ((bd_t *)of_flat_tree, 0, 0, EPAPR_MAGIC,
99a47a12beSStefan Roese 			   CONFIG_SYS_BOOTMAPSZ, 0, 0);
100a47a12beSStefan Roese 		/* does not return */
101a47a12beSStefan Roese 	} else
102a47a12beSStefan Roese #endif
103a47a12beSStefan Roese 	{
104a47a12beSStefan Roese 		/*
105a47a12beSStefan Roese 		 * Linux Kernel Parameters (passing board info data):
106a47a12beSStefan Roese 		 *   r3: ptr to board info data
107a47a12beSStefan Roese 		 *   r4: initrd_start or 0 if no initrd
108a47a12beSStefan Roese 		 *   r5: initrd_end - unused if r4 is 0
109a47a12beSStefan Roese 		 *   r6: Start of command line string
110a47a12beSStefan Roese 		 *   r7: End   of command line string
111a47a12beSStefan Roese 		 *   r8: 0
112a47a12beSStefan Roese 		 *   r9: 0
113a47a12beSStefan Roese 		 */
114a47a12beSStefan Roese 		ulong cmd_start = images->cmdline_start;
115a47a12beSStefan Roese 		ulong cmd_end = images->cmdline_end;
116a47a12beSStefan Roese 		ulong initrd_start = images->initrd_start;
117a47a12beSStefan Roese 		ulong initrd_end = images->initrd_end;
118a47a12beSStefan Roese 		bd_t *kbd = images->kbd;
119a47a12beSStefan Roese 
120a47a12beSStefan Roese 		debug ("   Booting using board info...\n");
121a47a12beSStefan Roese 		WATCHDOG_RESET ();
122a47a12beSStefan Roese 		(*kernel) (kbd, initrd_start, initrd_end,
123a47a12beSStefan Roese 			   cmd_start, cmd_end, 0, 0);
124a47a12beSStefan Roese 		/* does not return */
125a47a12beSStefan Roese 	}
126a47a12beSStefan Roese 	return ;
127a47a12beSStefan Roese }
128a47a12beSStefan Roese 
129a47a12beSStefan Roese void arch_lmb_reserve(struct lmb *lmb)
130a47a12beSStefan Roese {
131a47a12beSStefan Roese 	phys_size_t bootm_size;
132a47a12beSStefan Roese 	ulong size, sp, bootmap_base;
133a47a12beSStefan Roese 
134a47a12beSStefan Roese 	bootmap_base = getenv_bootm_low();
135a47a12beSStefan Roese 	bootm_size = getenv_bootm_size();
136a47a12beSStefan Roese 
137a47a12beSStefan Roese #ifdef DEBUG
138a47a12beSStefan Roese 	if (((u64)bootmap_base + bootm_size) >
139a47a12beSStefan Roese 	    (CONFIG_SYS_SDRAM_BASE + (u64)gd->ram_size))
140a47a12beSStefan Roese 		puts("WARNING: bootm_low + bootm_size exceed total memory\n");
141a47a12beSStefan Roese 	if ((bootmap_base + bootm_size) > get_effective_memsize())
142a47a12beSStefan Roese 		puts("WARNING: bootm_low + bootm_size exceed eff. memory\n");
143a47a12beSStefan Roese #endif
144a47a12beSStefan Roese 
145a47a12beSStefan Roese 	size = min(bootm_size, get_effective_memsize());
146a47a12beSStefan Roese 	size = min(size, CONFIG_SYS_LINUX_LOWMEM_MAX_SIZE);
147a47a12beSStefan Roese 
148a47a12beSStefan Roese 	if (size < bootm_size) {
149a47a12beSStefan Roese 		ulong base = bootmap_base + size;
150a47a12beSStefan Roese 		printf("WARNING: adjusting available memory to %lx\n", size);
151a47a12beSStefan Roese 		lmb_reserve(lmb, base, bootm_size - size);
152a47a12beSStefan Roese 	}
153a47a12beSStefan Roese 
154a47a12beSStefan Roese 	/*
155a47a12beSStefan Roese 	 * Booting a (Linux) kernel image
156a47a12beSStefan Roese 	 *
157a47a12beSStefan Roese 	 * Allocate space for command line and board info - the
158a47a12beSStefan Roese 	 * address should be as high as possible within the reach of
159a47a12beSStefan Roese 	 * the kernel (see CONFIG_SYS_BOOTMAPSZ settings), but in unused
160a47a12beSStefan Roese 	 * memory, which means far enough below the current stack
161a47a12beSStefan Roese 	 * pointer.
162a47a12beSStefan Roese 	 */
163a47a12beSStefan Roese 	sp = get_sp();
164a47a12beSStefan Roese 	debug ("## Current stack ends at 0x%08lx\n", sp);
165a47a12beSStefan Roese 
1663882d7a5SNorbert van Bolhuis 	/* adjust sp by 4K to be safe */
1673882d7a5SNorbert van Bolhuis 	sp -= 4096;
168a47a12beSStefan Roese 	lmb_reserve(lmb, sp, (CONFIG_SYS_SDRAM_BASE + get_effective_memsize() - sp));
169a47a12beSStefan Roese 
170a47a12beSStefan Roese 	return ;
171a47a12beSStefan Roese }
172a47a12beSStefan Roese 
173a47a12beSStefan Roese static void boot_prep_linux(void)
174a47a12beSStefan Roese {
175a47a12beSStefan Roese #ifdef CONFIG_MP
176a47a12beSStefan Roese 	/* if we are MP make sure to flush the dcache() to any changes are made
177a47a12beSStefan Roese 	 * visibile to all other cores */
178a47a12beSStefan Roese 	flush_dcache();
179a47a12beSStefan Roese #endif
180a47a12beSStefan Roese 	return ;
181a47a12beSStefan Roese }
182a47a12beSStefan Roese 
183a47a12beSStefan Roese static int boot_cmdline_linux(bootm_headers_t *images)
184a47a12beSStefan Roese {
185a47a12beSStefan Roese 	ulong bootmap_base = getenv_bootm_low();
186a47a12beSStefan Roese 	ulong of_size = images->ft_len;
187a47a12beSStefan Roese 	struct lmb *lmb = &images->lmb;
188a47a12beSStefan Roese 	ulong *cmd_start = &images->cmdline_start;
189a47a12beSStefan Roese 	ulong *cmd_end = &images->cmdline_end;
190a47a12beSStefan Roese 
191a47a12beSStefan Roese 	int ret = 0;
192a47a12beSStefan Roese 
193a47a12beSStefan Roese 	if (!of_size) {
194a47a12beSStefan Roese 		/* allocate space and init command line */
195a47a12beSStefan Roese 		ret = boot_get_cmdline (lmb, cmd_start, cmd_end, bootmap_base);
196a47a12beSStefan Roese 		if (ret) {
197a47a12beSStefan Roese 			puts("ERROR with allocation of cmdline\n");
198a47a12beSStefan Roese 			return ret;
199a47a12beSStefan Roese 		}
200a47a12beSStefan Roese 	}
201a47a12beSStefan Roese 
202a47a12beSStefan Roese 	return ret;
203a47a12beSStefan Roese }
204a47a12beSStefan Roese 
205a47a12beSStefan Roese static int boot_bd_t_linux(bootm_headers_t *images)
206a47a12beSStefan Roese {
207a47a12beSStefan Roese 	ulong bootmap_base = getenv_bootm_low();
208a47a12beSStefan Roese 	ulong of_size = images->ft_len;
209a47a12beSStefan Roese 	struct lmb *lmb = &images->lmb;
210a47a12beSStefan Roese 	bd_t **kbd = &images->kbd;
211a47a12beSStefan Roese 
212a47a12beSStefan Roese 	int ret = 0;
213a47a12beSStefan Roese 
214a47a12beSStefan Roese 	if (!of_size) {
215a47a12beSStefan Roese 		/* allocate space for kernel copy of board info */
216a47a12beSStefan Roese 		ret = boot_get_kbd (lmb, kbd, bootmap_base);
217a47a12beSStefan Roese 		if (ret) {
218a47a12beSStefan Roese 			puts("ERROR with allocation of kernel bd\n");
219a47a12beSStefan Roese 			return ret;
220a47a12beSStefan Roese 		}
221a47a12beSStefan Roese 		set_clocks_in_mhz(*kbd);
222a47a12beSStefan Roese 	}
223a47a12beSStefan Roese 
224a47a12beSStefan Roese 	return ret;
225a47a12beSStefan Roese }
226a47a12beSStefan Roese 
227a47a12beSStefan Roese static int boot_body_linux(bootm_headers_t *images)
228a47a12beSStefan Roese {
229a47a12beSStefan Roese 	ulong rd_len;
230a47a12beSStefan Roese 	struct lmb *lmb = &images->lmb;
231a47a12beSStefan Roese 	ulong *initrd_start = &images->initrd_start;
232a47a12beSStefan Roese 	ulong *initrd_end = &images->initrd_end;
233a47a12beSStefan Roese #if defined(CONFIG_OF_LIBFDT)
234a47a12beSStefan Roese 	ulong bootmap_base = getenv_bootm_low();
235a47a12beSStefan Roese 	ulong of_size = images->ft_len;
236a47a12beSStefan Roese 	char **of_flat_tree = &images->ft_addr;
237a47a12beSStefan Roese #endif
238a47a12beSStefan Roese 
239a47a12beSStefan Roese 	int ret;
240a47a12beSStefan Roese 
241a47a12beSStefan Roese 	/* allocate space and init command line */
242a47a12beSStefan Roese 	ret = boot_cmdline_linux(images);
243a47a12beSStefan Roese 	if (ret)
244a47a12beSStefan Roese 		return ret;
245a47a12beSStefan Roese 
246a47a12beSStefan Roese 	/* allocate space for kernel copy of board info */
247a47a12beSStefan Roese 	ret = boot_bd_t_linux(images);
248a47a12beSStefan Roese 	if (ret)
249a47a12beSStefan Roese 		return ret;
250a47a12beSStefan Roese 
251a47a12beSStefan Roese 	rd_len = images->rd_end - images->rd_start;
252a47a12beSStefan Roese 	ret = boot_ramdisk_high (lmb, images->rd_start, rd_len, initrd_start, initrd_end);
253a47a12beSStefan Roese 	if (ret)
254a47a12beSStefan Roese 		return ret;
255a47a12beSStefan Roese 
256a47a12beSStefan Roese #if defined(CONFIG_OF_LIBFDT)
257a47a12beSStefan Roese 	ret = boot_relocate_fdt(lmb, bootmap_base, of_flat_tree, &of_size);
258a47a12beSStefan Roese 	if (ret)
259a47a12beSStefan Roese 		return ret;
260a47a12beSStefan Roese 
261a47a12beSStefan Roese 	/*
262a47a12beSStefan Roese 	 * Add the chosen node if it doesn't exist, add the env and bd_t
263a47a12beSStefan Roese 	 * if the user wants it (the logic is in the subroutines).
264a47a12beSStefan Roese 	 */
265a47a12beSStefan Roese 	if (of_size) {
266a47a12beSStefan Roese 		if (fdt_chosen(*of_flat_tree, 1) < 0) {
267a47a12beSStefan Roese 			puts ("ERROR: ");
268a47a12beSStefan Roese 			puts ("/chosen node create failed");
269a47a12beSStefan Roese 			puts (" - must RESET the board to recover.\n");
270a47a12beSStefan Roese 			return -1;
271a47a12beSStefan Roese 		}
272a47a12beSStefan Roese #ifdef CONFIG_OF_BOARD_SETUP
273a47a12beSStefan Roese 		/* Call the board-specific fixup routine */
274a47a12beSStefan Roese 		ft_board_setup(*of_flat_tree, gd->bd);
275a47a12beSStefan Roese #endif
276a47a12beSStefan Roese 
277a47a12beSStefan Roese 		/* Delete the old LMB reservation */
278a47a12beSStefan Roese 		lmb_free(lmb, (phys_addr_t)(u32)*of_flat_tree,
279a47a12beSStefan Roese 				(phys_size_t)fdt_totalsize(*of_flat_tree));
280a47a12beSStefan Roese 
281a47a12beSStefan Roese 		ret = fdt_resize(*of_flat_tree);
282a47a12beSStefan Roese 		if (ret < 0)
283a47a12beSStefan Roese 			return ret;
284a47a12beSStefan Roese 		of_size = ret;
285a47a12beSStefan Roese 
286a47a12beSStefan Roese 		if (*initrd_start && *initrd_end)
287a47a12beSStefan Roese 			of_size += FDT_RAMDISK_OVERHEAD;
288a47a12beSStefan Roese 		/* Create a new LMB reservation */
289a47a12beSStefan Roese 		lmb_reserve(lmb, (ulong)*of_flat_tree, of_size);
290a47a12beSStefan Roese 
291a47a12beSStefan Roese 		/* fixup the initrd now that we know where it should be */
292a47a12beSStefan Roese 		if (*initrd_start && *initrd_end)
293a47a12beSStefan Roese 			fdt_initrd(*of_flat_tree, *initrd_start, *initrd_end, 1);
294a47a12beSStefan Roese 	}
295a47a12beSStefan Roese #endif	/* CONFIG_OF_LIBFDT */
296a47a12beSStefan Roese 	return 0;
297a47a12beSStefan Roese }
298a47a12beSStefan Roese 
299a47a12beSStefan Roese __attribute__((noinline))
300*54841ab5SWolfgang Denk int do_bootm_linux(int flag, int argc, char * const argv[], bootm_headers_t *images)
301a47a12beSStefan Roese {
302a47a12beSStefan Roese 	int	ret;
303a47a12beSStefan Roese 
304a47a12beSStefan Roese 	if (flag & BOOTM_STATE_OS_CMDLINE) {
305a47a12beSStefan Roese 		boot_cmdline_linux(images);
306a47a12beSStefan Roese 		return 0;
307a47a12beSStefan Roese 	}
308a47a12beSStefan Roese 
309a47a12beSStefan Roese 	if (flag & BOOTM_STATE_OS_BD_T) {
310a47a12beSStefan Roese 		boot_bd_t_linux(images);
311a47a12beSStefan Roese 		return 0;
312a47a12beSStefan Roese 	}
313a47a12beSStefan Roese 
314a47a12beSStefan Roese 	if (flag & BOOTM_STATE_OS_PREP) {
315a47a12beSStefan Roese 		boot_prep_linux();
316a47a12beSStefan Roese 		return 0;
317a47a12beSStefan Roese 	}
318a47a12beSStefan Roese 
319a47a12beSStefan Roese 	if (flag & BOOTM_STATE_OS_GO) {
320a47a12beSStefan Roese 		boot_jump_linux(images);
321a47a12beSStefan Roese 		return 0;
322a47a12beSStefan Roese 	}
323a47a12beSStefan Roese 
324a47a12beSStefan Roese 	boot_prep_linux();
325a47a12beSStefan Roese 	ret = boot_body_linux(images);
326a47a12beSStefan Roese 	if (ret)
327a47a12beSStefan Roese 		return ret;
328a47a12beSStefan Roese 	boot_jump_linux(images);
329a47a12beSStefan Roese 
330a47a12beSStefan Roese 	return 0;
331a47a12beSStefan Roese }
332a47a12beSStefan Roese 
333a47a12beSStefan Roese static ulong get_sp (void)
334a47a12beSStefan Roese {
335a47a12beSStefan Roese 	ulong sp;
336a47a12beSStefan Roese 
337a47a12beSStefan Roese 	asm( "mr %0,1": "=r"(sp) : );
338a47a12beSStefan Roese 	return sp;
339a47a12beSStefan Roese }
340a47a12beSStefan Roese 
341a47a12beSStefan Roese static void set_clocks_in_mhz (bd_t *kbd)
342a47a12beSStefan Roese {
343a47a12beSStefan Roese 	char	*s;
344a47a12beSStefan Roese 
345a47a12beSStefan Roese 	if ((s = getenv ("clocks_in_mhz")) != NULL) {
346a47a12beSStefan Roese 		/* convert all clock information to MHz */
347a47a12beSStefan Roese 		kbd->bi_intfreq /= 1000000L;
348a47a12beSStefan Roese 		kbd->bi_busfreq /= 1000000L;
349a47a12beSStefan Roese #if defined(CONFIG_MPC8220)
350a47a12beSStefan Roese 		kbd->bi_inpfreq /= 1000000L;
351a47a12beSStefan Roese 		kbd->bi_pcifreq /= 1000000L;
352a47a12beSStefan Roese 		kbd->bi_pevfreq /= 1000000L;
353a47a12beSStefan Roese 		kbd->bi_flbfreq /= 1000000L;
354a47a12beSStefan Roese 		kbd->bi_vcofreq /= 1000000L;
355a47a12beSStefan Roese #endif
356a47a12beSStefan Roese #if defined(CONFIG_CPM2)
357a47a12beSStefan Roese 		kbd->bi_cpmfreq /= 1000000L;
358a47a12beSStefan Roese 		kbd->bi_brgfreq /= 1000000L;
359a47a12beSStefan Roese 		kbd->bi_sccfreq /= 1000000L;
360a47a12beSStefan Roese 		kbd->bi_vco	/= 1000000L;
361a47a12beSStefan Roese #endif
362a47a12beSStefan Roese #if defined(CONFIG_MPC5xxx)
363a47a12beSStefan Roese 		kbd->bi_ipbfreq /= 1000000L;
364a47a12beSStefan Roese 		kbd->bi_pcifreq /= 1000000L;
365a47a12beSStefan Roese #endif /* CONFIG_MPC5xxx */
366a47a12beSStefan Roese 	}
367a47a12beSStefan Roese }
368