blob: 87d2d6ee70df4200d841bb2a5ec6164640ced4fc [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenk12490652004-04-18 21:13:41 +00002/*
Michal Simek922ce202007-03-11 13:48:24 +01003 * (C) Copyright 2007 Michal Simek
wdenk12490652004-04-18 21:13:41 +00004 * (C) Copyright 2004 Atmark Techno, Inc.
5 *
Michal Simek922ce202007-03-11 13:48:24 +01006 * Michal SIMEK <monstr@monstr.eu>
wdenk12490652004-04-18 21:13:41 +00007 * Yasushi SHOJI <yashi@atmark-techno.com>
wdenk12490652004-04-18 21:13:41 +00008 */
9
10#include <common.h>
11#include <command.h>
Simon Glass63334482019-11-14 12:57:39 -070012#include <cpu_func.h>
Simon Glass0af6e2d2019-08-01 09:46:52 -060013#include <env.h>
Simon Glasse3ee2fb2016-02-22 22:55:43 -070014#include <fdt_support.h>
Simon Glassf11478f2019-12-28 10:45:07 -070015#include <hang.h>
Michal Simek922ce202007-03-11 13:48:24 +010016#include <image.h>
Simon Glass274e0b02020-05-10 11:39:56 -060017#include <asm/cache.h>
Jean-Christophe PLAGNIOL-VILLARD6bb94492009-04-04 12:49:11 +020018#include <u-boot/zlib.h>
Michal Simek922ce202007-03-11 13:48:24 +010019#include <asm/byteorder.h>
wdenk12490652004-04-18 21:13:41 +000020
Michal Simek040872e2019-09-25 10:45:51 +020021DECLARE_GLOBAL_DATA_PTR;
22
23static ulong get_sp(void)
24{
25 ulong ret;
26
27 asm("addik %0, r1, 0" : "=r"(ret) : );
28 return ret;
29}
30
31void arch_lmb_reserve(struct lmb *lmb)
32{
33 ulong sp, bank_end;
34 int bank;
35
36 /*
37 * Booting a (Linux) kernel image
38 *
39 * Allocate space for command line and board info - the
40 * address should be as high as possible within the reach of
41 * the kernel (see CONFIG_SYS_BOOTMAPSZ settings), but in unused
42 * memory, which means far enough below the current stack
43 * pointer.
44 */
45 sp = get_sp();
46 debug("## Current stack ends at 0x%08lx ", sp);
47
48 /* adjust sp by 4K to be safe */
49 sp -= 4096;
50 for (bank = 0; bank < CONFIG_NR_DRAM_BANKS; bank++) {
51 if (sp < gd->bd->bi_dram[bank].start)
52 continue;
53 bank_end = gd->bd->bi_dram[bank].start +
54 gd->bd->bi_dram[bank].size;
55 if (sp >= bank_end)
56 continue;
57 lmb_reserve(lmb, sp, bank_end - sp);
58 break;
59 }
60}
61
Michal Simeke37afb52019-09-25 09:47:02 +020062static void boot_jump_linux(bootm_headers_t *images, int flag)
wdenk12490652004-04-18 21:13:41 +000063{
Michal Simeke37afb52019-09-25 09:47:02 +020064 void (*thekernel)(char *cmdline, ulong rd, ulong dt);
65 ulong dt = (ulong)images->ft_addr;
66 ulong rd_start = images->initrd_start;
67 ulong cmdline = images->cmdline_start;
68 int fake = (flag & BOOTM_STATE_OS_FAKE_GO);
Arun Bhanu2304c052010-04-15 18:27:17 +080069
Michal Simekc78ce292013-05-02 12:51:48 +020070 thekernel = (void (*)(char *, ulong, ulong))images->ep;
Arun Bhanu2304c052010-04-15 18:27:17 +080071
Michal Simek1facc8f2019-10-17 13:16:56 +020072 debug("## Transferring control to Linux (at address 0x%08lx) ",
73 (ulong)thekernel);
74 debug("cmdline 0x%08lx, ramdisk 0x%08lx, FDT 0x%08lx...\n",
75 cmdline, rd_start, dt);
76 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
77
78 printf("\nStarting kernel ...%s\n\n", fake ?
79 "(fake run for tracing)" : "");
80 bootstage_mark_name(BOOTSTAGE_ID_BOOTM_HANDOFF, "start_kernel");
Michal Simek922ce202007-03-11 13:48:24 +010081
Michal Simek80c42c72010-04-16 12:01:32 +020082#ifdef XILINX_USE_DCACHE
Michal Simek80c42c72010-04-16 12:01:32 +020083 flush_cache(0, XILINX_DCACHE_BYTE_SIZE);
Michal Simek80c42c72010-04-16 12:01:32 +020084#endif
Michal Simeke37afb52019-09-25 09:47:02 +020085
86 if (!fake) {
87 /*
88 * Linux Kernel Parameters (passing device tree):
89 * r5: pointer to command line
90 * r6: pointer to ramdisk
91 * r7: pointer to the fdt, followed by the board info data
92 */
93 thekernel((char *)cmdline, rd_start, dt);
94 /* does not return */
95 }
96}
97
98static void boot_prep_linux(bootm_headers_t *images)
99{
100 if (IMAGE_ENABLE_OF_LIBFDT && images->ft_len) {
Michal Simek4bbe2ac2019-10-17 14:12:48 +0200101 debug("using: FDT\n");
Michal Simeke37afb52019-09-25 09:47:02 +0200102 if (image_setup_linux(images)) {
103 printf("FDT creation failed! hanging...");
104 hang();
105 }
106 }
107}
108
109int do_bootm_linux(int flag, int argc, char * const argv[],
110 bootm_headers_t *images)
111{
112 images->cmdline_start = (ulong)env_get("bootargs");
113
114 /* cmdline init is the part of 'prep' and nothing to do for 'bdt' */
115 if (flag & BOOTM_STATE_OS_BD_T || flag & BOOTM_STATE_OS_CMDLINE)
116 return -1;
117
118 if (flag & BOOTM_STATE_OS_PREP) {
119 boot_prep_linux(images);
120 return 0;
121 }
122
123 if (flag & (BOOTM_STATE_OS_GO | BOOTM_STATE_OS_FAKE_GO)) {
124 boot_jump_linux(images, flag);
125 return 0;
126 }
Jean-Christophe PLAGNIOL-VILLARD7ed7a272008-09-10 22:48:09 +0200127
Michal Simeke37afb52019-09-25 09:47:02 +0200128 boot_prep_linux(images);
129 boot_jump_linux(images, flag);
Kumar Gala48626aa2008-08-15 08:24:45 -0500130 return 1;
wdenk12490652004-04-18 21:13:41 +0000131}