Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Macpaul Lin | 0d1f474 | 2011-10-11 22:33:19 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2011 Andes Technology Corporation |
| 4 | * Shawn Lin, Andes Technology Corporation <nobuhiro@andestech.com> |
| 5 | * Macpaul Lin, Andes Technology Corporation <macpaul@andestech.com> |
Macpaul Lin | 0d1f474 | 2011-10-11 22:33:19 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <common.h> |
Simon Glass | 1ea9789 | 2020-05-10 11:40:00 -0600 | [diff] [blame] | 9 | #include <bootstage.h> |
Macpaul Lin | 0d1f474 | 2011-10-11 22:33:19 +0000 | [diff] [blame] | 10 | #include <command.h> |
Simon Glass | 0af6e2d | 2019-08-01 09:46:52 -0600 | [diff] [blame] | 11 | #include <env.h> |
Simon Glass | f11478f | 2019-12-28 10:45:07 -0700 | [diff] [blame] | 12 | #include <hang.h> |
Macpaul Lin | 0d1f474 | 2011-10-11 22:33:19 +0000 | [diff] [blame] | 13 | #include <image.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 14 | #include <log.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 15 | #include <asm/global_data.h> |
Macpaul Lin | 0d1f474 | 2011-10-11 22:33:19 +0000 | [diff] [blame] | 16 | #include <u-boot/zlib.h> |
| 17 | #include <asm/byteorder.h> |
rick | f1113c9 | 2017-05-18 14:37:53 +0800 | [diff] [blame] | 18 | #include <asm/bootm.h> |
Macpaul Lin | 0d1f474 | 2011-10-11 22:33:19 +0000 | [diff] [blame] | 19 | |
| 20 | DECLARE_GLOBAL_DATA_PTR; |
| 21 | |
Patrick Delaunay | 65d4b17 | 2021-09-03 10:24:39 +0200 | [diff] [blame] | 22 | #ifdef CONFIG_SUPPORT_PASSING_ATAGS |
Masahiro Yamada | f7ed78b | 2020-06-26 15:13:33 +0900 | [diff] [blame] | 23 | static void setup_start_tag(struct bd_info *bd); |
Macpaul Lin | 0d1f474 | 2011-10-11 22:33:19 +0000 | [diff] [blame] | 24 | |
| 25 | # ifdef CONFIG_SETUP_MEMORY_TAGS |
Masahiro Yamada | f7ed78b | 2020-06-26 15:13:33 +0900 | [diff] [blame] | 26 | static void setup_memory_tags(struct bd_info *bd); |
Macpaul Lin | 0d1f474 | 2011-10-11 22:33:19 +0000 | [diff] [blame] | 27 | # endif |
Masahiro Yamada | f7ed78b | 2020-06-26 15:13:33 +0900 | [diff] [blame] | 28 | static void setup_commandline_tag(struct bd_info *bd, char *commandline); |
Macpaul Lin | 0d1f474 | 2011-10-11 22:33:19 +0000 | [diff] [blame] | 29 | |
| 30 | # ifdef CONFIG_INITRD_TAG |
Masahiro Yamada | f7ed78b | 2020-06-26 15:13:33 +0900 | [diff] [blame] | 31 | static void setup_initrd_tag(struct bd_info *bd, ulong initrd_start, |
| 32 | ulong initrd_end); |
Macpaul Lin | 0d1f474 | 2011-10-11 22:33:19 +0000 | [diff] [blame] | 33 | # endif |
Masahiro Yamada | f7ed78b | 2020-06-26 15:13:33 +0900 | [diff] [blame] | 34 | static void setup_end_tag(struct bd_info *bd); |
Macpaul Lin | 0d1f474 | 2011-10-11 22:33:19 +0000 | [diff] [blame] | 35 | |
| 36 | static struct tag *params; |
Patrick Delaunay | 65d4b17 | 2021-09-03 10:24:39 +0200 | [diff] [blame] | 37 | #endif /* CONFIG_SUPPORT_PASSING_ATAGS */ |
Macpaul Lin | 0d1f474 | 2011-10-11 22:33:19 +0000 | [diff] [blame] | 38 | |
| 39 | int do_bootm_linux(int flag, int argc, char *argv[], bootm_headers_t *images) |
| 40 | { |
Masahiro Yamada | f7ed78b | 2020-06-26 15:13:33 +0900 | [diff] [blame] | 41 | struct bd_info *bd = gd->bd; |
Macpaul Lin | 0d1f474 | 2011-10-11 22:33:19 +0000 | [diff] [blame] | 42 | char *s; |
| 43 | int machid = bd->bi_arch_number; |
| 44 | void (*theKernel)(int zero, int arch, uint params); |
| 45 | |
| 46 | #ifdef CONFIG_CMDLINE_TAG |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 47 | char *commandline = env_get("bootargs"); |
Macpaul Lin | 0d1f474 | 2011-10-11 22:33:19 +0000 | [diff] [blame] | 48 | #endif |
| 49 | |
Andreas Bießmann | da233ce | 2013-07-02 13:57:44 +0200 | [diff] [blame] | 50 | /* |
| 51 | * allow the PREP bootm subcommand, it is required for bootm to work |
| 52 | */ |
| 53 | if (flag & BOOTM_STATE_OS_PREP) |
| 54 | return 0; |
| 55 | |
Macpaul Lin | 0d1f474 | 2011-10-11 22:33:19 +0000 | [diff] [blame] | 56 | if ((flag != 0) && (flag != BOOTM_STATE_OS_GO)) |
| 57 | return 1; |
| 58 | |
| 59 | theKernel = (void (*)(int, int, uint))images->ep; |
| 60 | |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 61 | s = env_get("machid"); |
Macpaul Lin | 0d1f474 | 2011-10-11 22:33:19 +0000 | [diff] [blame] | 62 | if (s) { |
Simon Glass | 3ff49ec | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 63 | machid = hextoul(s, NULL); |
Macpaul Lin | 0d1f474 | 2011-10-11 22:33:19 +0000 | [diff] [blame] | 64 | printf("Using machid 0x%x from environment\n", machid); |
| 65 | } |
| 66 | |
Simon Glass | 0169e6b | 2012-02-13 13:51:18 +0000 | [diff] [blame] | 67 | bootstage_mark(BOOTSTAGE_ID_RUN_OS); |
Macpaul Lin | 0d1f474 | 2011-10-11 22:33:19 +0000 | [diff] [blame] | 68 | |
| 69 | debug("## Transferring control to Linux (at address %08lx) ...\n", |
| 70 | (ulong)theKernel); |
| 71 | |
Simon Glass | 85c057e | 2021-09-25 19:43:21 -0600 | [diff] [blame] | 72 | if (CONFIG_IS_ENABLED(OF_LIBFDT) && images->ft_len) { |
rick | f1113c9 | 2017-05-18 14:37:53 +0800 | [diff] [blame] | 73 | #ifdef CONFIG_OF_LIBFDT |
| 74 | debug("using: FDT\n"); |
| 75 | if (image_setup_linux(images)) { |
| 76 | printf("FDT creation failed! hanging..."); |
| 77 | hang(); |
| 78 | } |
| 79 | #endif |
| 80 | } else if (BOOTM_ENABLE_TAGS) { |
Patrick Delaunay | 65d4b17 | 2021-09-03 10:24:39 +0200 | [diff] [blame] | 81 | #ifdef CONFIG_SUPPORT_PASSING_ATAGS |
Macpaul Lin | 0d1f474 | 2011-10-11 22:33:19 +0000 | [diff] [blame] | 82 | setup_start_tag(bd); |
| 83 | #ifdef CONFIG_SERIAL_TAG |
| 84 | setup_serial_tag(¶ms); |
| 85 | #endif |
| 86 | #ifdef CONFIG_REVISION_TAG |
| 87 | setup_revision_tag(¶ms); |
| 88 | #endif |
| 89 | #ifdef CONFIG_SETUP_MEMORY_TAGS |
| 90 | setup_memory_tags(bd); |
| 91 | #endif |
| 92 | #ifdef CONFIG_CMDLINE_TAG |
| 93 | setup_commandline_tag(bd, commandline); |
| 94 | #endif |
| 95 | #ifdef CONFIG_INITRD_TAG |
| 96 | if (images->rd_start && images->rd_end) |
| 97 | setup_initrd_tag(bd, images->rd_start, images->rd_end); |
| 98 | #endif |
| 99 | setup_end_tag(bd); |
| 100 | #endif |
| 101 | |
| 102 | /* we assume that the kernel is in place */ |
| 103 | printf("\nStarting kernel ...\n\n"); |
| 104 | |
| 105 | #ifdef CONFIG_USB_DEVICE |
| 106 | { |
| 107 | extern void udc_disconnect(void); |
| 108 | udc_disconnect(); |
| 109 | } |
| 110 | #endif |
rick | f1113c9 | 2017-05-18 14:37:53 +0800 | [diff] [blame] | 111 | } |
Macpaul Lin | 0d1f474 | 2011-10-11 22:33:19 +0000 | [diff] [blame] | 112 | cleanup_before_linux(); |
Simon Glass | 85c057e | 2021-09-25 19:43:21 -0600 | [diff] [blame] | 113 | if (CONFIG_IS_ENABLED(OF_LIBFDT) && images->ft_len) |
rick | f1113c9 | 2017-05-18 14:37:53 +0800 | [diff] [blame] | 114 | theKernel(0, machid, (unsigned long)images->ft_addr); |
| 115 | else |
| 116 | theKernel(0, machid, bd->bi_boot_params); |
Macpaul Lin | 0d1f474 | 2011-10-11 22:33:19 +0000 | [diff] [blame] | 117 | /* does not return */ |
| 118 | |
| 119 | return 1; |
| 120 | } |
| 121 | |
Patrick Delaunay | 65d4b17 | 2021-09-03 10:24:39 +0200 | [diff] [blame] | 122 | #ifdef CONFIG_SUPPORT_PASSING_ATAGS |
Masahiro Yamada | f7ed78b | 2020-06-26 15:13:33 +0900 | [diff] [blame] | 123 | static void setup_start_tag(struct bd_info *bd) |
Macpaul Lin | 0d1f474 | 2011-10-11 22:33:19 +0000 | [diff] [blame] | 124 | { |
| 125 | params = (struct tag *)bd->bi_boot_params; |
| 126 | |
| 127 | params->hdr.tag = ATAG_CORE; |
| 128 | params->hdr.size = tag_size(tag_core); |
| 129 | |
| 130 | params->u.core.flags = 0; |
| 131 | params->u.core.pagesize = 0; |
| 132 | params->u.core.rootdev = 0; |
| 133 | |
| 134 | params = tag_next(params); |
| 135 | } |
| 136 | |
Macpaul Lin | 0d1f474 | 2011-10-11 22:33:19 +0000 | [diff] [blame] | 137 | #ifdef CONFIG_SETUP_MEMORY_TAGS |
Masahiro Yamada | f7ed78b | 2020-06-26 15:13:33 +0900 | [diff] [blame] | 138 | static void setup_memory_tags(struct bd_info *bd) |
Macpaul Lin | 0d1f474 | 2011-10-11 22:33:19 +0000 | [diff] [blame] | 139 | { |
| 140 | int i; |
| 141 | |
| 142 | for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) { |
| 143 | params->hdr.tag = ATAG_MEM; |
| 144 | params->hdr.size = tag_size(tag_mem32); |
| 145 | |
| 146 | params->u.mem.start = bd->bi_dram[i].start; |
| 147 | params->u.mem.size = bd->bi_dram[i].size; |
| 148 | |
| 149 | params = tag_next(params); |
| 150 | } |
| 151 | } |
| 152 | #endif /* CONFIG_SETUP_MEMORY_TAGS */ |
| 153 | |
Masahiro Yamada | f7ed78b | 2020-06-26 15:13:33 +0900 | [diff] [blame] | 154 | static void setup_commandline_tag(struct bd_info *bd, char *commandline) |
Macpaul Lin | 0d1f474 | 2011-10-11 22:33:19 +0000 | [diff] [blame] | 155 | { |
| 156 | char *p; |
| 157 | |
| 158 | if (!commandline) |
| 159 | return; |
| 160 | |
| 161 | /* eat leading white space */ |
| 162 | for (p = commandline; *p == ' '; p++) |
| 163 | ; |
| 164 | |
| 165 | /* skip non-existent command lines so the kernel will still |
| 166 | * use its default command line. |
| 167 | */ |
| 168 | if (*p == '\0') |
| 169 | return; |
| 170 | |
| 171 | params->hdr.tag = ATAG_CMDLINE; |
| 172 | params->hdr.size = |
| 173 | (sizeof(struct tag_header) + strlen(p) + 1 + 4) >> 2; |
| 174 | |
| 175 | strcpy(params->u.cmdline.cmdline, p) |
| 176 | ; |
| 177 | |
| 178 | params = tag_next(params); |
| 179 | } |
| 180 | |
Macpaul Lin | 0d1f474 | 2011-10-11 22:33:19 +0000 | [diff] [blame] | 181 | #ifdef CONFIG_INITRD_TAG |
Masahiro Yamada | f7ed78b | 2020-06-26 15:13:33 +0900 | [diff] [blame] | 182 | static void setup_initrd_tag(struct bd_info *bd, ulong initrd_start, |
| 183 | ulong initrd_end) |
Macpaul Lin | 0d1f474 | 2011-10-11 22:33:19 +0000 | [diff] [blame] | 184 | { |
| 185 | /* an ATAG_INITRD node tells the kernel where the compressed |
| 186 | * ramdisk can be found. ATAG_RDIMG is a better name, actually. |
| 187 | */ |
| 188 | params->hdr.tag = ATAG_INITRD2; |
| 189 | params->hdr.size = tag_size(tag_initrd); |
| 190 | |
| 191 | params->u.initrd.start = initrd_start; |
| 192 | params->u.initrd.size = initrd_end - initrd_start; |
| 193 | |
| 194 | params = tag_next(params); |
| 195 | } |
| 196 | #endif /* CONFIG_INITRD_TAG */ |
| 197 | |
| 198 | #ifdef CONFIG_SERIAL_TAG |
| 199 | void setup_serial_tag(struct tag **tmp) |
| 200 | { |
| 201 | struct tag *params = *tmp; |
| 202 | struct tag_serialnr serialnr; |
| 203 | void get_board_serial(struct tag_serialnr *serialnr); |
| 204 | |
| 205 | get_board_serial(&serialnr); |
| 206 | params->hdr.tag = ATAG_SERIAL; |
| 207 | params->hdr.size = tag_size(tag_serialnr); |
| 208 | params->u.serialnr.low = serialnr.low; |
| 209 | params->u.serialnr.high = serialnr.high; |
| 210 | params = tag_next(params); |
| 211 | *tmp = params; |
| 212 | } |
| 213 | #endif |
| 214 | |
| 215 | #ifdef CONFIG_REVISION_TAG |
| 216 | void setup_revision_tag(struct tag **in_params) |
| 217 | { |
| 218 | u32 rev = 0; |
| 219 | u32 get_board_rev(void); |
| 220 | |
| 221 | rev = get_board_rev(); |
| 222 | params->hdr.tag = ATAG_REVISION; |
| 223 | params->hdr.size = tag_size(tag_revision); |
| 224 | params->u.revision.rev = rev; |
| 225 | params = tag_next(params); |
| 226 | } |
| 227 | #endif /* CONFIG_REVISION_TAG */ |
| 228 | |
Masahiro Yamada | f7ed78b | 2020-06-26 15:13:33 +0900 | [diff] [blame] | 229 | static void setup_end_tag(struct bd_info *bd) |
Macpaul Lin | 0d1f474 | 2011-10-11 22:33:19 +0000 | [diff] [blame] | 230 | { |
| 231 | params->hdr.tag = ATAG_NONE; |
| 232 | params->hdr.size = 0; |
| 233 | } |
| 234 | |
Patrick Delaunay | 65d4b17 | 2021-09-03 10:24:39 +0200 | [diff] [blame] | 235 | #endif /* CONFIG_SUPPORT_PASSING_ATAGS */ |
Marek Vasut | e7b2bca | 2021-09-10 22:47:14 +0200 | [diff] [blame] | 236 | |
| 237 | static ulong get_sp(void) |
| 238 | { |
| 239 | ulong ret; |
| 240 | |
| 241 | asm("move %0, $sp" : "=r"(ret) : ); |
| 242 | return ret; |
| 243 | } |
| 244 | |
| 245 | void arch_lmb_reserve(struct lmb *lmb) |
| 246 | { |
| 247 | arch_lmb_reserve_generic(lmb, get_sp(), gd->ram_top, 4096); |
| 248 | } |