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> |
| 9 | #include <command.h> |
Simon Glass | 0af6e2d | 2019-08-01 09:46:52 -0600 | [diff] [blame] | 10 | #include <env.h> |
Simon Glass | f11478f | 2019-12-28 10:45:07 -0700 | [diff] [blame] | 11 | #include <hang.h> |
Macpaul Lin | 0d1f474 | 2011-10-11 22:33:19 +0000 | [diff] [blame] | 12 | #include <image.h> |
| 13 | #include <u-boot/zlib.h> |
| 14 | #include <asm/byteorder.h> |
rick | f1113c9 | 2017-05-18 14:37:53 +0800 | [diff] [blame] | 15 | #include <asm/bootm.h> |
Macpaul Lin | 0d1f474 | 2011-10-11 22:33:19 +0000 | [diff] [blame] | 16 | |
| 17 | DECLARE_GLOBAL_DATA_PTR; |
| 18 | |
| 19 | #if defined(CONFIG_SETUP_MEMORY_TAGS) || \ |
| 20 | defined(CONFIG_CMDLINE_TAG) || \ |
| 21 | defined(CONFIG_INITRD_TAG) || \ |
| 22 | defined(CONFIG_SERIAL_TAG) || \ |
| 23 | defined(CONFIG_REVISION_TAG) |
| 24 | static void setup_start_tag(bd_t *bd); |
| 25 | |
| 26 | # ifdef CONFIG_SETUP_MEMORY_TAGS |
| 27 | static void setup_memory_tags(bd_t *bd); |
| 28 | # endif |
| 29 | static void setup_commandline_tag(bd_t *bd, char *commandline); |
| 30 | |
| 31 | # ifdef CONFIG_INITRD_TAG |
| 32 | static void setup_initrd_tag(bd_t *bd, ulong initrd_start, ulong initrd_end); |
| 33 | # endif |
| 34 | static void setup_end_tag(bd_t *bd); |
| 35 | |
| 36 | static struct tag *params; |
| 37 | #endif /* CONFIG_SETUP_MEMORY_TAGS || CONFIG_CMDLINE_TAG || CONFIG_INITRD_TAG */ |
| 38 | |
| 39 | int do_bootm_linux(int flag, int argc, char *argv[], bootm_headers_t *images) |
| 40 | { |
| 41 | bd_t *bd = gd->bd; |
| 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) { |
| 63 | machid = simple_strtoul(s, NULL, 16); |
| 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 | |
rick | f1113c9 | 2017-05-18 14:37:53 +0800 | [diff] [blame] | 72 | if (IMAGE_ENABLE_OF_LIBFDT && images->ft_len) { |
| 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) { |
Macpaul Lin | 0d1f474 | 2011-10-11 22:33:19 +0000 | [diff] [blame] | 81 | #if defined(CONFIG_SETUP_MEMORY_TAGS) || \ |
| 82 | defined(CONFIG_CMDLINE_TAG) || \ |
| 83 | defined(CONFIG_INITRD_TAG) || \ |
| 84 | defined(CONFIG_SERIAL_TAG) || \ |
| 85 | defined(CONFIG_REVISION_TAG) |
| 86 | setup_start_tag(bd); |
| 87 | #ifdef CONFIG_SERIAL_TAG |
| 88 | setup_serial_tag(¶ms); |
| 89 | #endif |
| 90 | #ifdef CONFIG_REVISION_TAG |
| 91 | setup_revision_tag(¶ms); |
| 92 | #endif |
| 93 | #ifdef CONFIG_SETUP_MEMORY_TAGS |
| 94 | setup_memory_tags(bd); |
| 95 | #endif |
| 96 | #ifdef CONFIG_CMDLINE_TAG |
| 97 | setup_commandline_tag(bd, commandline); |
| 98 | #endif |
| 99 | #ifdef CONFIG_INITRD_TAG |
| 100 | if (images->rd_start && images->rd_end) |
| 101 | setup_initrd_tag(bd, images->rd_start, images->rd_end); |
| 102 | #endif |
| 103 | setup_end_tag(bd); |
| 104 | #endif |
| 105 | |
| 106 | /* we assume that the kernel is in place */ |
| 107 | printf("\nStarting kernel ...\n\n"); |
| 108 | |
| 109 | #ifdef CONFIG_USB_DEVICE |
| 110 | { |
| 111 | extern void udc_disconnect(void); |
| 112 | udc_disconnect(); |
| 113 | } |
| 114 | #endif |
rick | f1113c9 | 2017-05-18 14:37:53 +0800 | [diff] [blame] | 115 | } |
Macpaul Lin | 0d1f474 | 2011-10-11 22:33:19 +0000 | [diff] [blame] | 116 | cleanup_before_linux(); |
rick | f1113c9 | 2017-05-18 14:37:53 +0800 | [diff] [blame] | 117 | if (IMAGE_ENABLE_OF_LIBFDT && images->ft_len) |
| 118 | theKernel(0, machid, (unsigned long)images->ft_addr); |
| 119 | else |
| 120 | theKernel(0, machid, bd->bi_boot_params); |
Macpaul Lin | 0d1f474 | 2011-10-11 22:33:19 +0000 | [diff] [blame] | 121 | /* does not return */ |
| 122 | |
| 123 | return 1; |
| 124 | } |
| 125 | |
Macpaul Lin | 0d1f474 | 2011-10-11 22:33:19 +0000 | [diff] [blame] | 126 | #if defined(CONFIG_SETUP_MEMORY_TAGS) || \ |
| 127 | defined(CONFIG_CMDLINE_TAG) || \ |
| 128 | defined(CONFIG_INITRD_TAG) || \ |
| 129 | defined(CONFIG_SERIAL_TAG) || \ |
| 130 | defined(CONFIG_REVISION_TAG) |
| 131 | static void setup_start_tag(bd_t *bd) |
| 132 | { |
| 133 | params = (struct tag *)bd->bi_boot_params; |
| 134 | |
| 135 | params->hdr.tag = ATAG_CORE; |
| 136 | params->hdr.size = tag_size(tag_core); |
| 137 | |
| 138 | params->u.core.flags = 0; |
| 139 | params->u.core.pagesize = 0; |
| 140 | params->u.core.rootdev = 0; |
| 141 | |
| 142 | params = tag_next(params); |
| 143 | } |
| 144 | |
Macpaul Lin | 0d1f474 | 2011-10-11 22:33:19 +0000 | [diff] [blame] | 145 | #ifdef CONFIG_SETUP_MEMORY_TAGS |
| 146 | static void setup_memory_tags(bd_t *bd) |
| 147 | { |
| 148 | int i; |
| 149 | |
| 150 | for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) { |
| 151 | params->hdr.tag = ATAG_MEM; |
| 152 | params->hdr.size = tag_size(tag_mem32); |
| 153 | |
| 154 | params->u.mem.start = bd->bi_dram[i].start; |
| 155 | params->u.mem.size = bd->bi_dram[i].size; |
| 156 | |
| 157 | params = tag_next(params); |
| 158 | } |
| 159 | } |
| 160 | #endif /* CONFIG_SETUP_MEMORY_TAGS */ |
| 161 | |
Macpaul Lin | 0d1f474 | 2011-10-11 22:33:19 +0000 | [diff] [blame] | 162 | static void setup_commandline_tag(bd_t *bd, char *commandline) |
| 163 | { |
| 164 | char *p; |
| 165 | |
| 166 | if (!commandline) |
| 167 | return; |
| 168 | |
| 169 | /* eat leading white space */ |
| 170 | for (p = commandline; *p == ' '; p++) |
| 171 | ; |
| 172 | |
| 173 | /* skip non-existent command lines so the kernel will still |
| 174 | * use its default command line. |
| 175 | */ |
| 176 | if (*p == '\0') |
| 177 | return; |
| 178 | |
| 179 | params->hdr.tag = ATAG_CMDLINE; |
| 180 | params->hdr.size = |
| 181 | (sizeof(struct tag_header) + strlen(p) + 1 + 4) >> 2; |
| 182 | |
| 183 | strcpy(params->u.cmdline.cmdline, p) |
| 184 | ; |
| 185 | |
| 186 | params = tag_next(params); |
| 187 | } |
| 188 | |
Macpaul Lin | 0d1f474 | 2011-10-11 22:33:19 +0000 | [diff] [blame] | 189 | #ifdef CONFIG_INITRD_TAG |
| 190 | static void setup_initrd_tag(bd_t *bd, ulong initrd_start, ulong initrd_end) |
| 191 | { |
| 192 | /* an ATAG_INITRD node tells the kernel where the compressed |
| 193 | * ramdisk can be found. ATAG_RDIMG is a better name, actually. |
| 194 | */ |
| 195 | params->hdr.tag = ATAG_INITRD2; |
| 196 | params->hdr.size = tag_size(tag_initrd); |
| 197 | |
| 198 | params->u.initrd.start = initrd_start; |
| 199 | params->u.initrd.size = initrd_end - initrd_start; |
| 200 | |
| 201 | params = tag_next(params); |
| 202 | } |
| 203 | #endif /* CONFIG_INITRD_TAG */ |
| 204 | |
| 205 | #ifdef CONFIG_SERIAL_TAG |
| 206 | void setup_serial_tag(struct tag **tmp) |
| 207 | { |
| 208 | struct tag *params = *tmp; |
| 209 | struct tag_serialnr serialnr; |
| 210 | void get_board_serial(struct tag_serialnr *serialnr); |
| 211 | |
| 212 | get_board_serial(&serialnr); |
| 213 | params->hdr.tag = ATAG_SERIAL; |
| 214 | params->hdr.size = tag_size(tag_serialnr); |
| 215 | params->u.serialnr.low = serialnr.low; |
| 216 | params->u.serialnr.high = serialnr.high; |
| 217 | params = tag_next(params); |
| 218 | *tmp = params; |
| 219 | } |
| 220 | #endif |
| 221 | |
| 222 | #ifdef CONFIG_REVISION_TAG |
| 223 | void setup_revision_tag(struct tag **in_params) |
| 224 | { |
| 225 | u32 rev = 0; |
| 226 | u32 get_board_rev(void); |
| 227 | |
| 228 | rev = get_board_rev(); |
| 229 | params->hdr.tag = ATAG_REVISION; |
| 230 | params->hdr.size = tag_size(tag_revision); |
| 231 | params->u.revision.rev = rev; |
| 232 | params = tag_next(params); |
| 233 | } |
| 234 | #endif /* CONFIG_REVISION_TAG */ |
| 235 | |
Macpaul Lin | 0d1f474 | 2011-10-11 22:33:19 +0000 | [diff] [blame] | 236 | static void setup_end_tag(bd_t *bd) |
| 237 | { |
| 238 | params->hdr.tag = ATAG_NONE; |
| 239 | params->hdr.size = 0; |
| 240 | } |
| 241 | |
| 242 | #endif /* CONFIG_SETUP_MEMORY_TAGS || CONFIG_CMDLINE_TAG || CONFIG_INITRD_TAG */ |