f7303b790fcb3941489c5bee9a630a0a88a9b5e8
[platform/kernel/u-boot.git] / arch / microblaze / lib / bootm.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2007 Michal Simek
4  * (C) Copyright 2004 Atmark Techno, Inc.
5  *
6  * Michal  SIMEK <monstr@monstr.eu>
7  * Yasushi SHOJI <yashi@atmark-techno.com>
8  */
9
10 #include <common.h>
11 #include <bootstage.h>
12 #include <command.h>
13 #include <cpu_func.h>
14 #include <env.h>
15 #include <fdt_support.h>
16 #include <hang.h>
17 #include <image.h>
18 #include <asm/cache.h>
19 #include <u-boot/zlib.h>
20 #include <asm/byteorder.h>
21
22 DECLARE_GLOBAL_DATA_PTR;
23
24 static ulong get_sp(void)
25 {
26         ulong ret;
27
28         asm("addik %0, r1, 0" : "=r"(ret) : );
29         return ret;
30 }
31
32 void arch_lmb_reserve(struct lmb *lmb)
33 {
34         ulong sp, bank_end;
35         int bank;
36
37         /*
38          * Booting a (Linux) kernel image
39          *
40          * Allocate space for command line and board info - the
41          * address should be as high as possible within the reach of
42          * the kernel (see CONFIG_SYS_BOOTMAPSZ settings), but in unused
43          * memory, which means far enough below the current stack
44          * pointer.
45          */
46         sp = get_sp();
47         debug("## Current stack ends at 0x%08lx ", sp);
48
49         /* adjust sp by 4K to be safe */
50         sp -= 4096;
51         for (bank = 0; bank < CONFIG_NR_DRAM_BANKS; bank++) {
52                 if (sp < gd->bd->bi_dram[bank].start)
53                         continue;
54                 bank_end = gd->bd->bi_dram[bank].start +
55                         gd->bd->bi_dram[bank].size;
56                 if (sp >= bank_end)
57                         continue;
58                 lmb_reserve(lmb, sp, bank_end - sp);
59                 break;
60         }
61 }
62
63 static void boot_jump_linux(bootm_headers_t *images, int flag)
64 {
65         void (*thekernel)(char *cmdline, ulong rd, ulong dt);
66         ulong dt = (ulong)images->ft_addr;
67         ulong rd_start = images->initrd_start;
68         ulong cmdline = images->cmdline_start;
69         int fake = (flag & BOOTM_STATE_OS_FAKE_GO);
70
71         thekernel = (void (*)(char *, ulong, ulong))images->ep;
72
73         debug("## Transferring control to Linux (at address 0x%08lx) ",
74               (ulong)thekernel);
75         debug("cmdline 0x%08lx, ramdisk 0x%08lx, FDT 0x%08lx...\n",
76               cmdline, rd_start, dt);
77         bootstage_mark(BOOTSTAGE_ID_RUN_OS);
78
79         printf("\nStarting kernel ...%s\n\n", fake ?
80                "(fake run for tracing)" : "");
81         bootstage_mark_name(BOOTSTAGE_ID_BOOTM_HANDOFF, "start_kernel");
82
83 #ifdef XILINX_USE_DCACHE
84         flush_cache(0, XILINX_DCACHE_BYTE_SIZE);
85 #endif
86
87         if (!fake) {
88                 /*
89                  * Linux Kernel Parameters (passing device tree):
90                  * r5: pointer to command line
91                  * r6: pointer to ramdisk
92                  * r7: pointer to the fdt, followed by the board info data
93                  */
94                 thekernel((char *)cmdline, rd_start, dt);
95                 /* does not return */
96         }
97 }
98
99 static void boot_prep_linux(bootm_headers_t *images)
100 {
101         if (IMAGE_ENABLE_OF_LIBFDT && images->ft_len) {
102                 debug("using: FDT\n");
103                 if (image_setup_linux(images)) {
104                         printf("FDT creation failed! hanging...");
105                         hang();
106                 }
107         }
108 }
109
110 int do_bootm_linux(int flag, int argc, char * const argv[],
111                    bootm_headers_t *images)
112 {
113         images->cmdline_start = (ulong)env_get("bootargs");
114
115         /* cmdline init is the part of 'prep' and nothing to do for 'bdt' */
116         if (flag & BOOTM_STATE_OS_BD_T || flag & BOOTM_STATE_OS_CMDLINE)
117                 return -1;
118
119         if (flag & BOOTM_STATE_OS_PREP) {
120                 boot_prep_linux(images);
121                 return 0;
122         }
123
124         if (flag & (BOOTM_STATE_OS_GO | BOOTM_STATE_OS_FAKE_GO)) {
125                 boot_jump_linux(images, flag);
126                 return 0;
127         }
128
129         boot_prep_linux(images);
130         boot_jump_linux(images, flag);
131         return 1;
132 }