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