300e00a2dab6df3d737ef89748009c43c26f8266
[oweals/u-boot.git] / lib_ppc / bootm.c
1 /*
2  * (C) Copyright 2008 Semihalf
3  *
4  * (C) Copyright 2000-2006
5  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
6  *
7  * See file CREDITS for list of people who contributed to this
8  * project.
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License as
12  * published by the Free Software Foundation; either version 2 of
13  * the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
23  * MA 02111-1307 USA
24  */
25
26
27 #include <common.h>
28 #include <watchdog.h>
29 #include <command.h>
30 #include <image.h>
31 #include <malloc.h>
32 #include <zlib.h>
33 #include <bzlib.h>
34 #include <environment.h>
35 #include <asm/byteorder.h>
36
37 #if defined(CONFIG_OF_LIBFDT)
38 #include <fdt.h>
39 #include <libfdt.h>
40 #include <fdt_support.h>
41
42 #endif
43
44 #ifdef CFG_INIT_RAM_LOCK
45 #include <asm/cache.h>
46 #endif
47
48 DECLARE_GLOBAL_DATA_PTR;
49
50 extern int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
51 extern ulong get_effective_memsize(void);
52 static ulong get_sp (void);
53 static void set_clocks_in_mhz (bd_t *kbd);
54
55 #ifndef CFG_LINUX_LOWMEM_MAX_SIZE
56 #define CFG_LINUX_LOWMEM_MAX_SIZE       (768*1024*1024)
57 #endif
58
59 void  __attribute__((noinline))
60 do_bootm_linux(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
61                 bootm_headers_t *images)
62 {
63         ulong   sp;
64
65         ulong   initrd_start, initrd_end;
66         ulong   rd_len;
67         ulong   size;
68         phys_size_t bootm_size;
69
70         ulong   cmd_start, cmd_end, bootmap_base;
71         bd_t    *kbd;
72         void    (*kernel)(bd_t *, ulong r4, ulong r5, ulong r6,
73                           ulong r7, ulong r8, ulong r9);
74         int     ret;
75         ulong   of_size = images->ft_len;
76         struct lmb *lmb = &images->lmb;
77
78 #if defined(CONFIG_OF_LIBFDT)
79         char    *of_flat_tree = images->ft_addr;
80 #endif
81
82         kernel = (void (*)(bd_t *, ulong, ulong, ulong,
83                            ulong, ulong, ulong))images->ep;
84
85         bootmap_base = getenv_bootm_low();
86         bootm_size = getenv_bootm_size();
87
88 #ifdef DEBUG
89         if (((u64)bootmap_base + bootm_size) >
90             (CFG_SDRAM_BASE + (u64)gd->ram_size))
91                 puts("WARNING: bootm_low + bootm_size exceed total memory\n");
92         if ((bootmap_base + bootm_size) > get_effective_memsize())
93                 puts("WARNING: bootm_low + bootm_size exceed eff. memory\n");
94 #endif
95
96         size = min(bootm_size, get_effective_memsize());
97         size = min(size, CFG_LINUX_LOWMEM_MAX_SIZE);
98
99         if (size < bootm_size) {
100                 ulong base = bootmap_base + size;
101                 printf("WARNING: adjusting available memory to %lx\n", size);
102                 lmb_reserve(lmb, base, bootm_size - size);
103         }
104
105         /*
106          * Booting a (Linux) kernel image
107          *
108          * Allocate space for command line and board info - the
109          * address should be as high as possible within the reach of
110          * the kernel (see CFG_BOOTMAPSZ settings), but in unused
111          * memory, which means far enough below the current stack
112          * pointer.
113          */
114         sp = get_sp();
115         debug ("## Current stack ends at 0x%08lx\n", sp);
116
117         /* adjust sp by 1K to be safe */
118         sp -= 1024;
119         lmb_reserve(lmb, sp, (CFG_SDRAM_BASE + get_effective_memsize() - sp));
120
121         if (!of_size) {
122                 /* allocate space and init command line */
123                 ret = boot_get_cmdline (lmb, &cmd_start, &cmd_end, bootmap_base);
124                 if (ret) {
125                         puts("ERROR with allocation of cmdline\n");
126                         goto error;
127                 }
128
129                 /* allocate space for kernel copy of board info */
130                 ret = boot_get_kbd (lmb, &kbd, bootmap_base);
131                 if (ret) {
132                         puts("ERROR with allocation of kernel bd\n");
133                         goto error;
134                 }
135                 set_clocks_in_mhz(kbd);
136         }
137
138         rd_len = images->rd_end - images->rd_start;
139
140 #if defined(CONFIG_OF_LIBFDT)
141         ret = boot_relocate_fdt(lmb, bootmap_base, &of_flat_tree, &of_size);
142         if (ret)
143                 goto error;
144
145         /*
146          * Add the chosen node if it doesn't exist, add the env and bd_t
147          * if the user wants it (the logic is in the subroutines).
148          */
149         if (of_size) {
150                 /* pass in dummy initrd info, we'll fix up later */
151                 if (fdt_chosen(of_flat_tree, images->rd_start, images->rd_end, 0) < 0) {
152                         puts ("ERROR: ");
153                         puts ("/chosen node create failed");
154                         puts (" - must RESET the board to recover.\n");
155                         goto error;
156                 }
157 #ifdef CONFIG_OF_BOARD_SETUP
158                 /* Call the board-specific fixup routine */
159                 ft_board_setup(of_flat_tree, gd->bd);
160 #endif
161         }
162
163         /* Fixup the fdt memreserve now that we know how big it is */
164         if (of_flat_tree) {
165                 /* Delete the old LMB reservation */
166                 lmb_free(lmb, (phys_addr_t)(u32)of_flat_tree,
167                                 (phys_size_t)fdt_totalsize(of_flat_tree));
168
169                 ret = fdt_resize(of_flat_tree);
170                 if (ret < 0)
171                         goto error;
172                 of_size = ret;
173
174                 /* Create a new LMB reservation */
175                 lmb_reserve(lmb, (ulong)of_flat_tree, of_size);
176         }
177 #endif  /* CONFIG_OF_LIBFDT */
178
179         ret = boot_ramdisk_high (lmb, images->rd_start, rd_len, &initrd_start, &initrd_end);
180         if (ret)
181                 goto error;
182
183 #if defined(CONFIG_OF_LIBFDT)
184         /* fixup the initrd now that we know where it should be */
185         if ((of_flat_tree) && (initrd_start && initrd_end)) {
186                 uint64_t addr, size;
187                 int  total = fdt_num_mem_rsv(of_flat_tree);
188                 int  j;
189
190                 /* Look for the dummy entry and delete it */
191                 for (j = 0; j < total; j++) {
192                         fdt_get_mem_rsv(of_flat_tree, j, &addr, &size);
193                         if (addr == images->rd_start) {
194                                 fdt_del_mem_rsv(of_flat_tree, j);
195                                 break;
196                         }
197                 }
198
199                 ret = fdt_add_mem_rsv(of_flat_tree, initrd_start,
200                                         initrd_end - initrd_start + 1);
201                 if (ret < 0) {
202                         printf("fdt_chosen: %s\n", fdt_strerror(ret));
203                         goto error;
204                 }
205
206                 do_fixup_by_path_u32(of_flat_tree, "/chosen",
207                                         "linux,initrd-start", initrd_start, 0);
208                 do_fixup_by_path_u32(of_flat_tree, "/chosen",
209                                         "linux,initrd-end", initrd_end, 0);
210         }
211 #endif
212         debug ("## Transferring control to Linux (at address %08lx) ...\n",
213                 (ulong)kernel);
214
215         show_boot_progress (15);
216
217 #if defined(CFG_INIT_RAM_LOCK) && !defined(CONFIG_E500)
218         unlock_ram_in_cache();
219 #endif
220
221 #if defined(CONFIG_OF_LIBFDT)
222         if (of_flat_tree) {     /* device tree; boot new style */
223                 /*
224                  * Linux Kernel Parameters (passing device tree):
225                  *   r3: pointer to the fdt
226                  *   r4: 0
227                  *   r5: 0
228                  *   r6: epapr magic
229                  *   r7: size of IMA in bytes
230                  *   r8: 0
231                  *   r9: 0
232                  */
233 #if defined(CONFIG_85xx) || defined(CONFIG_440)
234  #define EPAPR_MAGIC    (0x45504150)
235 #else
236  #define EPAPR_MAGIC    (0x65504150)
237 #endif
238
239                 debug ("   Booting using OF flat tree...\n");
240                 (*kernel) ((bd_t *)of_flat_tree, 0, 0, EPAPR_MAGIC,
241                            CFG_BOOTMAPSZ, 0, 0);
242                 /* does not return */
243         } else
244 #endif
245         {
246                 /*
247                  * Linux Kernel Parameters (passing board info data):
248                  *   r3: ptr to board info data
249                  *   r4: initrd_start or 0 if no initrd
250                  *   r5: initrd_end - unused if r4 is 0
251                  *   r6: Start of command line string
252                  *   r7: End   of command line string
253                  *   r8: 0
254                  *   r9: 0
255                  */
256                 debug ("   Booting using board info...\n");
257                 (*kernel) (kbd, initrd_start, initrd_end,
258                            cmd_start, cmd_end, 0, 0);
259                 /* does not return */
260         }
261         return ;
262
263 error:
264         do_reset (cmdtp, flag, argc, argv);
265         return ;
266 }
267
268 static ulong get_sp (void)
269 {
270         ulong sp;
271
272         asm( "mr %0,1": "=r"(sp) : );
273         return sp;
274 }
275
276 static void set_clocks_in_mhz (bd_t *kbd)
277 {
278         char    *s;
279
280         if ((s = getenv ("clocks_in_mhz")) != NULL) {
281                 /* convert all clock information to MHz */
282                 kbd->bi_intfreq /= 1000000L;
283                 kbd->bi_busfreq /= 1000000L;
284 #if defined(CONFIG_MPC8220)
285                 kbd->bi_inpfreq /= 1000000L;
286                 kbd->bi_pcifreq /= 1000000L;
287                 kbd->bi_pevfreq /= 1000000L;
288                 kbd->bi_flbfreq /= 1000000L;
289                 kbd->bi_vcofreq /= 1000000L;
290 #endif
291 #if defined(CONFIG_CPM2)
292                 kbd->bi_cpmfreq /= 1000000L;
293                 kbd->bi_brgfreq /= 1000000L;
294                 kbd->bi_sccfreq /= 1000000L;
295                 kbd->bi_vco     /= 1000000L;
296 #endif
297 #if defined(CONFIG_MPC5xxx)
298                 kbd->bi_ipbfreq /= 1000000L;
299                 kbd->bi_pcifreq /= 1000000L;
300 #endif /* CONFIG_MPC5xxx */
301         }
302 }