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