blob: e7153b048003491264d6e0424fdfd37bfc8a9849 [file] [log] [blame]
Marian Balakowicz5d3cc552008-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 Denk1a459662013-07-08 09:37:19 +02007 * SPDX-License-Identifier: GPL-2.0+
Marian Balakowicz5d3cc552008-01-08 18:11:43 +01008 */
9
Marian Balakowicz75824382008-01-31 13:20:06 +010010
Marian Balakowicz5d3cc552008-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-VILLARDa31e0912009-04-04 12:49:11 +020016#include <u-boot/zlib.h>
Marian Balakowicz5d3cc552008-01-08 18:11:43 +010017#include <bzlib.h>
18#include <environment.h>
19#include <asm/byteorder.h>
Kumar Gala561e7102011-01-31 15:51:20 -060020#include <asm/mp.h>
Marian Balakowicz5d3cc552008-01-08 18:11:43 +010021
22#if defined(CONFIG_OF_LIBFDT)
Marian Balakowicz5d3cc552008-01-08 18:11:43 +010023#include <libfdt.h>
24#include <fdt_support.h>
Wolfgang Denk9c673522009-07-26 23:28:02 +020025#endif
26
27#ifdef CONFIG_SYS_INIT_RAM_LOCK
28#include <asm/cache.h>
Marian Balakowicz5d3cc552008-01-08 18:11:43 +010029#endif
30
Marian Balakowicz5d3cc552008-01-08 18:11:43 +010031DECLARE_GLOBAL_DATA_PTR;
Marian Balakowicz5d3cc552008-01-08 18:11:43 +010032
Kumar Galae822d7f2008-02-27 21:51:49 -060033extern ulong get_effective_memsize(void);
Marian Balakowiczceaed2b2008-01-31 13:57:17 +010034static ulong get_sp (void);
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +010035static void set_clocks_in_mhz (bd_t *kbd);
Marian Balakowicz5d3cc552008-01-08 18:11:43 +010036
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-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 Galad3f2fa02008-02-27 21:51:50 -060039#endif
40
Kumar Gala5a981272008-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 Glass770605e2012-02-13 13:51:18 +000054 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
Kumar Gala5a981272008-10-21 17:25:46 -050055
Wolfgang Denk9c673522009-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 Gala5a981272008-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 Gala5a981272008-10-21 17:25:46 -050072 debug (" Booting using OF flat tree...\n");
Heiko Schocher7ff66bb2009-08-12 10:17:03 +020073 WATCHDOG_RESET ();
Kumar Gala5a981272008-10-21 17:25:46 -050074 (*kernel) ((bd_t *)of_flat_tree, 0, 0, EPAPR_MAGIC,
Grant Likelyc3624e62011-03-28 09:58:43 +000075 getenv_bootm_mapsize(), 0, 0);
Kumar Gala5a981272008-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 Schocher7ff66bb2009-08-12 10:17:03 +020097 WATCHDOG_RESET ();
Kumar Gala5a981272008-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 Gala76da19d2008-10-16 21:52:08 -0500105void arch_lmb_reserve(struct lmb *lmb)
Marian Balakowicz5d3cc552008-01-08 18:11:43 +0100106{
Becky Bruce391fd932008-06-09 20:37:18 -0500107 phys_size_t bootm_size;
Kumar Gala76da19d2008-10-16 21:52:08 -0500108 ulong size, sp, bootmap_base;
Kumar Galac160a952008-08-15 08:24:36 -0500109
Kumar Galad3f2fa02008-02-27 21:51:50 -0600110 bootmap_base = getenv_bootm_low();
Becky Bruce391fd932008-06-09 20:37:18 -0500111 bootm_size = getenv_bootm_size();
Kumar Galad3f2fa02008-02-27 21:51:50 -0600112
113#ifdef DEBUG
Becky Bruce391fd932008-06-09 20:37:18 -0500114 if (((u64)bootmap_base + bootm_size) >
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200115 (CONFIG_SYS_SDRAM_BASE + (u64)gd->ram_size))
Kumar Galad3f2fa02008-02-27 21:51:50 -0600116 puts("WARNING: bootm_low + bootm_size exceed total memory\n");
Becky Bruce391fd932008-06-09 20:37:18 -0500117 if ((bootmap_base + bootm_size) > get_effective_memsize())
Kumar Galad3f2fa02008-02-27 21:51:50 -0600118 puts("WARNING: bootm_low + bootm_size exceed eff. memory\n");
119#endif
120
Becky Bruce391fd932008-06-09 20:37:18 -0500121 size = min(bootm_size, get_effective_memsize());
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200122 size = min(size, CONFIG_SYS_LINUX_LOWMEM_MAX_SIZE);
Kumar Galad3f2fa02008-02-27 21:51:50 -0600123
Becky Bruce391fd932008-06-09 20:37:18 -0500124 if (size < bootm_size) {
Kumar Galad3f2fa02008-02-27 21:51:50 -0600125 ulong base = bootmap_base + size;
Andrew Klossnerdc4b0b32008-07-07 06:41:14 -0700126 printf("WARNING: adjusting available memory to %lx\n", size);
Becky Bruce391fd932008-06-09 20:37:18 -0500127 lmb_reserve(lmb, base, bootm_size - size);
Kumar Galad3f2fa02008-02-27 21:51:50 -0600128 }
Kumar Galae822d7f2008-02-27 21:51:49 -0600129
Marian Balakowicz5d3cc552008-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-VILLARD6d0f6bc2008-10-16 15:01:15 +0200135 * the kernel (see CONFIG_SYS_BOOTMAPSZ settings), but in unused
Marian Balakowicz5d3cc552008-01-08 18:11:43 +0100136 * memory, which means far enough below the current stack
137 * pointer.
138 */
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +0100139 sp = get_sp();
Kumar Galad3f2fa02008-02-27 21:51:50 -0600140 debug ("## Current stack ends at 0x%08lx\n", sp);
Marian Balakowicz5d3cc552008-01-08 18:11:43 +0100141
Norbert van Bolhuis3882d7a2010-03-19 15:34:25 +0100142 /* adjust sp by 4K to be safe */
143 sp -= 4096;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200144 lmb_reserve(lmb, sp, (CONFIG_SYS_SDRAM_BASE + get_effective_memsize() - sp));
Marian Balakowicz5d3cc552008-01-08 18:11:43 +0100145
Kumar Gala561e7102011-01-31 15:51:20 -0600146#ifdef CONFIG_MP
147 cpu_mp_lmb_reserve(lmb);
148#endif
149
Kumar Gala76da19d2008-10-16 21:52:08 -0500150 return ;
151}
152
Kumar Gala3b200112011-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 Gala5a981272008-10-21 17:25:46 -0500165static int boot_cmdline_linux(bootm_headers_t *images)
166{
Kumar Gala5a981272008-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 Gala49c3a862008-10-21 17:25:45 -0500171
Kumar Gala5a981272008-10-21 17:25:46 -0500172 int ret = 0;
Kumar Gala76da19d2008-10-16 21:52:08 -0500173
Kumar Gala27953492008-02-27 21:51:44 -0600174 if (!of_size) {
175 /* allocate space and init command line */
Grant Likely590d3ca2011-03-28 09:58:34 +0000176 ret = boot_get_cmdline (lmb, cmd_start, cmd_end);
Kumar Galae822d7f2008-02-27 21:51:49 -0600177 if (ret) {
178 puts("ERROR with allocation of cmdline\n");
Kumar Gala5a981272008-10-21 17:25:46 -0500179 return ret;
Kumar Galae822d7f2008-02-27 21:51:49 -0600180 }
Kumar Gala27953492008-02-27 21:51:44 -0600181 }
Marian Balakowicz5d3cc552008-01-08 18:11:43 +0100182
Kumar Gala5a981272008-10-21 17:25:46 -0500183 return ret;
184}
185
186static int boot_bd_t_linux(bootm_headers_t *images)
187{
Kumar Gala5a981272008-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;
193
194 if (!of_size) {
195 /* allocate space for kernel copy of board info */
Grant Likely590d3ca2011-03-28 09:58:34 +0000196 ret = boot_get_kbd (lmb, kbd);
Kumar Gala5a981272008-10-21 17:25:46 -0500197 if (ret) {
198 puts("ERROR with allocation of kernel bd\n");
199 return ret;
200 }
201 set_clocks_in_mhz(*kbd);
202 }
203
204 return ret;
205}
206
207static int boot_body_linux(bootm_headers_t *images)
208{
Kumar Gala5a981272008-10-21 17:25:46 -0500209 int ret;
210
Kumar Gala5a981272008-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 Glass3e512662013-05-08 08:06:04 +0000216 ret = image_setup_linux(images);
Kumar Gala5a981272008-10-21 17:25:46 -0500217 if (ret)
218 return ret;
Marian Balakowicz5d3cc552008-01-08 18:11:43 +0100219
Kumar Gala5a981272008-10-21 17:25:46 -0500220 return 0;
221}
Marian Balakowiczd45d5a12008-01-08 18:11:43 +0100222
Kim Phillipseef1cf22012-10-29 13:34:23 +0000223noinline
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200224int do_bootm_linux(int flag, int argc, char * const argv[], bootm_headers_t *images)
Kumar Gala5a981272008-10-21 17:25:46 -0500225{
226 int ret;
Kumar Galad2bc0952008-02-27 21:51:45 -0600227
Kumar Gala5a981272008-10-21 17:25:46 -0500228 if (flag & BOOTM_STATE_OS_CMDLINE) {
229 boot_cmdline_linux(images);
230 return 0;
Marian Balakowiczbc8ed482008-03-12 10:32:53 +0100231 }
Kumar Gala274cea22008-02-27 21:51:46 -0600232
Kumar Gala5a981272008-10-21 17:25:46 -0500233 if (flag & BOOTM_STATE_OS_BD_T) {
234 boot_bd_t_linux(images);
235 return 0;
236 }
237
Kumar Gala3b200112011-12-07 04:42:58 +0000238 if (flag & BOOTM_STATE_OS_PREP) {
239 boot_prep_linux(images);
Kumar Gala5a981272008-10-21 17:25:46 -0500240 return 0;
Kumar Gala3b200112011-12-07 04:42:58 +0000241 }
Kumar Gala5a981272008-10-21 17:25:46 -0500242
Kumar Gala3b200112011-12-07 04:42:58 +0000243 boot_prep_linux(images);
Kumar Gala5a981272008-10-21 17:25:46 -0500244 ret = boot_body_linux(images);
245 if (ret)
246 return ret;
247 boot_jump_linux(images);
248
249 return 0;
Marian Balakowicz5d3cc552008-01-08 18:11:43 +0100250}
Marian Balakowiczd3c5eb62008-01-31 13:20:08 +0100251
Marian Balakowiczceaed2b2008-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 Balakowiczb6b0fe62008-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 Balakowiczb6b0fe62008-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 Denk438a4c12008-03-26 11:48:46 +0100272 kbd->bi_vco /= 1000000L;
Marian Balakowiczb6b0fe62008-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 }
279}