xref: /openbmc/u-boot/arch/powerpc/lib/bootm.c (revision 561e710a979c86a0c647dd70d143cafc123bcd64)
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>
36*561e710aSKumar Gala #include <asm/mp.h>
37a47a12beSStefan Roese 
38a47a12beSStefan Roese #if defined(CONFIG_OF_LIBFDT)
39a47a12beSStefan Roese #include <fdt.h>
40a47a12beSStefan Roese #include <libfdt.h>
41a47a12beSStefan Roese #include <fdt_support.h>
42a47a12beSStefan Roese 
43a47a12beSStefan Roese #endif
44a47a12beSStefan Roese 
45a47a12beSStefan Roese #ifdef CONFIG_SYS_INIT_RAM_LOCK
46a47a12beSStefan Roese #include <asm/cache.h>
47a47a12beSStefan Roese #endif
48a47a12beSStefan Roese 
49a47a12beSStefan Roese DECLARE_GLOBAL_DATA_PTR;
50a47a12beSStefan Roese 
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 
170*561e710aSKumar Gala #ifdef CONFIG_MP
171*561e710aSKumar Gala 	cpu_mp_lmb_reserve(lmb);
172*561e710aSKumar Gala #endif
173*561e710aSKumar Gala 
174a47a12beSStefan Roese 	return ;
175a47a12beSStefan Roese }
176a47a12beSStefan Roese 
177a47a12beSStefan Roese static void boot_prep_linux(void)
178a47a12beSStefan Roese {
179a47a12beSStefan Roese #ifdef CONFIG_MP
180a47a12beSStefan Roese 	/* if we are MP make sure to flush the dcache() to any changes are made
181a47a12beSStefan Roese 	 * visibile to all other cores */
182a47a12beSStefan Roese 	flush_dcache();
183a47a12beSStefan Roese #endif
184a47a12beSStefan Roese 	return ;
185a47a12beSStefan Roese }
186a47a12beSStefan Roese 
187a47a12beSStefan Roese static int boot_cmdline_linux(bootm_headers_t *images)
188a47a12beSStefan Roese {
189a47a12beSStefan Roese 	ulong bootmap_base = getenv_bootm_low();
190a47a12beSStefan Roese 	ulong of_size = images->ft_len;
191a47a12beSStefan Roese 	struct lmb *lmb = &images->lmb;
192a47a12beSStefan Roese 	ulong *cmd_start = &images->cmdline_start;
193a47a12beSStefan Roese 	ulong *cmd_end = &images->cmdline_end;
194a47a12beSStefan Roese 
195a47a12beSStefan Roese 	int ret = 0;
196a47a12beSStefan Roese 
197a47a12beSStefan Roese 	if (!of_size) {
198a47a12beSStefan Roese 		/* allocate space and init command line */
199a47a12beSStefan Roese 		ret = boot_get_cmdline (lmb, cmd_start, cmd_end, bootmap_base);
200a47a12beSStefan Roese 		if (ret) {
201a47a12beSStefan Roese 			puts("ERROR with allocation of cmdline\n");
202a47a12beSStefan Roese 			return ret;
203a47a12beSStefan Roese 		}
204a47a12beSStefan Roese 	}
205a47a12beSStefan Roese 
206a47a12beSStefan Roese 	return ret;
207a47a12beSStefan Roese }
208a47a12beSStefan Roese 
209a47a12beSStefan Roese static int boot_bd_t_linux(bootm_headers_t *images)
210a47a12beSStefan Roese {
211a47a12beSStefan Roese 	ulong bootmap_base = getenv_bootm_low();
212a47a12beSStefan Roese 	ulong of_size = images->ft_len;
213a47a12beSStefan Roese 	struct lmb *lmb = &images->lmb;
214a47a12beSStefan Roese 	bd_t **kbd = &images->kbd;
215a47a12beSStefan Roese 
216a47a12beSStefan Roese 	int ret = 0;
217a47a12beSStefan Roese 
218a47a12beSStefan Roese 	if (!of_size) {
219a47a12beSStefan Roese 		/* allocate space for kernel copy of board info */
220a47a12beSStefan Roese 		ret = boot_get_kbd (lmb, kbd, bootmap_base);
221a47a12beSStefan Roese 		if (ret) {
222a47a12beSStefan Roese 			puts("ERROR with allocation of kernel bd\n");
223a47a12beSStefan Roese 			return ret;
224a47a12beSStefan Roese 		}
225a47a12beSStefan Roese 		set_clocks_in_mhz(*kbd);
226a47a12beSStefan Roese 	}
227a47a12beSStefan Roese 
228a47a12beSStefan Roese 	return ret;
229a47a12beSStefan Roese }
230a47a12beSStefan Roese 
231a47a12beSStefan Roese static int boot_body_linux(bootm_headers_t *images)
232a47a12beSStefan Roese {
233a47a12beSStefan Roese 	ulong rd_len;
234a47a12beSStefan Roese 	struct lmb *lmb = &images->lmb;
235a47a12beSStefan Roese 	ulong *initrd_start = &images->initrd_start;
236a47a12beSStefan Roese 	ulong *initrd_end = &images->initrd_end;
237a47a12beSStefan Roese #if defined(CONFIG_OF_LIBFDT)
238a47a12beSStefan Roese 	ulong bootmap_base = getenv_bootm_low();
239a47a12beSStefan Roese 	ulong of_size = images->ft_len;
240a47a12beSStefan Roese 	char **of_flat_tree = &images->ft_addr;
241a47a12beSStefan Roese #endif
242a47a12beSStefan Roese 
243a47a12beSStefan Roese 	int ret;
244a47a12beSStefan Roese 
245a47a12beSStefan Roese 	/* allocate space and init command line */
246a47a12beSStefan Roese 	ret = boot_cmdline_linux(images);
247a47a12beSStefan Roese 	if (ret)
248a47a12beSStefan Roese 		return ret;
249a47a12beSStefan Roese 
250a47a12beSStefan Roese 	/* allocate space for kernel copy of board info */
251a47a12beSStefan Roese 	ret = boot_bd_t_linux(images);
252a47a12beSStefan Roese 	if (ret)
253a47a12beSStefan Roese 		return ret;
254a47a12beSStefan Roese 
255a47a12beSStefan Roese 	rd_len = images->rd_end - images->rd_start;
256a47a12beSStefan Roese 	ret = boot_ramdisk_high (lmb, images->rd_start, rd_len, initrd_start, initrd_end);
257a47a12beSStefan Roese 	if (ret)
258a47a12beSStefan Roese 		return ret;
259a47a12beSStefan Roese 
260958e1206SStephan Linz #if defined(CONFIG_OF_LIBFDT) && defined(CONFIG_SYS_BOOTMAPSZ)
261a47a12beSStefan Roese 	ret = boot_relocate_fdt(lmb, bootmap_base, of_flat_tree, &of_size);
262a47a12beSStefan Roese 	if (ret)
263a47a12beSStefan Roese 		return ret;
264a47a12beSStefan Roese 
265a47a12beSStefan Roese 	/*
266a47a12beSStefan Roese 	 * Add the chosen node if it doesn't exist, add the env and bd_t
267a47a12beSStefan Roese 	 * if the user wants it (the logic is in the subroutines).
268a47a12beSStefan Roese 	 */
269a47a12beSStefan Roese 	if (of_size) {
270a47a12beSStefan Roese 		if (fdt_chosen(*of_flat_tree, 1) < 0) {
271a47a12beSStefan Roese 			puts ("ERROR: ");
272a47a12beSStefan Roese 			puts ("/chosen node create failed");
273a47a12beSStefan Roese 			puts (" - must RESET the board to recover.\n");
274a47a12beSStefan Roese 			return -1;
275a47a12beSStefan Roese 		}
276a47a12beSStefan Roese #ifdef CONFIG_OF_BOARD_SETUP
277a47a12beSStefan Roese 		/* Call the board-specific fixup routine */
278a47a12beSStefan Roese 		ft_board_setup(*of_flat_tree, gd->bd);
279a47a12beSStefan Roese #endif
280a47a12beSStefan Roese 
281a47a12beSStefan Roese 		/* Delete the old LMB reservation */
282a47a12beSStefan Roese 		lmb_free(lmb, (phys_addr_t)(u32)*of_flat_tree,
283a47a12beSStefan Roese 				(phys_size_t)fdt_totalsize(*of_flat_tree));
284a47a12beSStefan Roese 
285a47a12beSStefan Roese 		ret = fdt_resize(*of_flat_tree);
286a47a12beSStefan Roese 		if (ret < 0)
287a47a12beSStefan Roese 			return ret;
288a47a12beSStefan Roese 		of_size = ret;
289a47a12beSStefan Roese 
290a47a12beSStefan Roese 		if (*initrd_start && *initrd_end)
291a47a12beSStefan Roese 			of_size += FDT_RAMDISK_OVERHEAD;
292a47a12beSStefan Roese 		/* Create a new LMB reservation */
293a47a12beSStefan Roese 		lmb_reserve(lmb, (ulong)*of_flat_tree, of_size);
294a47a12beSStefan Roese 
295a47a12beSStefan Roese 		/* fixup the initrd now that we know where it should be */
296a47a12beSStefan Roese 		if (*initrd_start && *initrd_end)
297a47a12beSStefan Roese 			fdt_initrd(*of_flat_tree, *initrd_start, *initrd_end, 1);
298a47a12beSStefan Roese 	}
299958e1206SStephan Linz #endif	/* CONFIG_OF_LIBFDT && CONFIG_SYS_BOOTMAPSZ */
300a47a12beSStefan Roese 	return 0;
301a47a12beSStefan Roese }
302a47a12beSStefan Roese 
303a47a12beSStefan Roese __attribute__((noinline))
30454841ab5SWolfgang Denk int do_bootm_linux(int flag, int argc, char * const argv[], bootm_headers_t *images)
305a47a12beSStefan Roese {
306a47a12beSStefan Roese 	int	ret;
307a47a12beSStefan Roese 
308a47a12beSStefan Roese 	if (flag & BOOTM_STATE_OS_CMDLINE) {
309a47a12beSStefan Roese 		boot_cmdline_linux(images);
310a47a12beSStefan Roese 		return 0;
311a47a12beSStefan Roese 	}
312a47a12beSStefan Roese 
313a47a12beSStefan Roese 	if (flag & BOOTM_STATE_OS_BD_T) {
314a47a12beSStefan Roese 		boot_bd_t_linux(images);
315a47a12beSStefan Roese 		return 0;
316a47a12beSStefan Roese 	}
317a47a12beSStefan Roese 
318a47a12beSStefan Roese 	if (flag & BOOTM_STATE_OS_PREP) {
319a47a12beSStefan Roese 		boot_prep_linux();
320a47a12beSStefan Roese 		return 0;
321a47a12beSStefan Roese 	}
322a47a12beSStefan Roese 
323a47a12beSStefan Roese 	if (flag & BOOTM_STATE_OS_GO) {
324a47a12beSStefan Roese 		boot_jump_linux(images);
325a47a12beSStefan Roese 		return 0;
326a47a12beSStefan Roese 	}
327a47a12beSStefan Roese 
328a47a12beSStefan Roese 	boot_prep_linux();
329a47a12beSStefan Roese 	ret = boot_body_linux(images);
330a47a12beSStefan Roese 	if (ret)
331a47a12beSStefan Roese 		return ret;
332a47a12beSStefan Roese 	boot_jump_linux(images);
333a47a12beSStefan Roese 
334a47a12beSStefan Roese 	return 0;
335a47a12beSStefan Roese }
336a47a12beSStefan Roese 
337a47a12beSStefan Roese static ulong get_sp (void)
338a47a12beSStefan Roese {
339a47a12beSStefan Roese 	ulong sp;
340a47a12beSStefan Roese 
341a47a12beSStefan Roese 	asm( "mr %0,1": "=r"(sp) : );
342a47a12beSStefan Roese 	return sp;
343a47a12beSStefan Roese }
344a47a12beSStefan Roese 
345a47a12beSStefan Roese static void set_clocks_in_mhz (bd_t *kbd)
346a47a12beSStefan Roese {
347a47a12beSStefan Roese 	char	*s;
348a47a12beSStefan Roese 
349a47a12beSStefan Roese 	if ((s = getenv ("clocks_in_mhz")) != NULL) {
350a47a12beSStefan Roese 		/* convert all clock information to MHz */
351a47a12beSStefan Roese 		kbd->bi_intfreq /= 1000000L;
352a47a12beSStefan Roese 		kbd->bi_busfreq /= 1000000L;
353a47a12beSStefan Roese #if defined(CONFIG_MPC8220)
354a47a12beSStefan Roese 		kbd->bi_inpfreq /= 1000000L;
355a47a12beSStefan Roese 		kbd->bi_pcifreq /= 1000000L;
356a47a12beSStefan Roese 		kbd->bi_pevfreq /= 1000000L;
357a47a12beSStefan Roese 		kbd->bi_flbfreq /= 1000000L;
358a47a12beSStefan Roese 		kbd->bi_vcofreq /= 1000000L;
359a47a12beSStefan Roese #endif
360a47a12beSStefan Roese #if defined(CONFIG_CPM2)
361a47a12beSStefan Roese 		kbd->bi_cpmfreq /= 1000000L;
362a47a12beSStefan Roese 		kbd->bi_brgfreq /= 1000000L;
363a47a12beSStefan Roese 		kbd->bi_sccfreq /= 1000000L;
364a47a12beSStefan Roese 		kbd->bi_vco	/= 1000000L;
365a47a12beSStefan Roese #endif
366a47a12beSStefan Roese #if defined(CONFIG_MPC5xxx)
367a47a12beSStefan Roese 		kbd->bi_ipbfreq /= 1000000L;
368a47a12beSStefan Roese 		kbd->bi_pcifreq /= 1000000L;
369a47a12beSStefan Roese #endif /* CONFIG_MPC5xxx */
370a47a12beSStefan Roese 	}
371a47a12beSStefan Roese }
372