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