rjw | 1f88458 | 2022-01-06 17:20:42 +0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2014 Linaro Ltd. |
| 3 | * Author: Rob Herring <robh@kernel.org> |
| 4 | * |
| 5 | * Based on 8250 earlycon: |
| 6 | * (c) Copyright 2004 Hewlett-Packard Development Company, L.P. |
| 7 | * Bjorn Helgaas <bjorn.helgaas@hp.com> |
| 8 | * |
| 9 | * This program is free software: you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License version 2 as |
| 11 | * published by the Free Software Foundation. |
| 12 | */ |
| 13 | |
| 14 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 15 | |
| 16 | #include <linux/console.h> |
| 17 | #include <linux/kernel.h> |
| 18 | #include <linux/init.h> |
| 19 | #include <linux/io.h> |
| 20 | #include <linux/serial_core.h> |
| 21 | #include <linux/sizes.h> |
| 22 | #include <linux/of.h> |
| 23 | #include <linux/of_fdt.h> |
| 24 | #include <linux/acpi.h> |
| 25 | |
| 26 | #ifdef CONFIG_FIX_EARLYCON_MEM |
| 27 | #include <asm/fixmap.h> |
| 28 | #endif |
| 29 | |
| 30 | #include <asm/serial.h> |
| 31 | |
| 32 | static struct console early_con = { |
| 33 | .name = "uart", /* fixed up at earlycon registration */ |
| 34 | .flags = CON_PRINTBUFFER | CON_BOOT, |
| 35 | .index = 0, |
| 36 | }; |
| 37 | |
| 38 | static struct earlycon_device early_console_dev = { |
| 39 | .con = &early_con, |
| 40 | }; |
| 41 | |
| 42 | static void __iomem * __init earlycon_map(resource_size_t paddr, size_t size) |
| 43 | { |
| 44 | void __iomem *base; |
| 45 | #ifdef CONFIG_FIX_EARLYCON_MEM |
| 46 | set_fixmap_io(FIX_EARLYCON_MEM_BASE, paddr & PAGE_MASK); |
| 47 | base = (void __iomem *)__fix_to_virt(FIX_EARLYCON_MEM_BASE); |
| 48 | base += paddr & ~PAGE_MASK; |
| 49 | #else |
| 50 | base = ioremap(paddr, size); |
| 51 | #endif |
| 52 | if (!base) |
| 53 | pr_err("%s: Couldn't map %pa\n", __func__, &paddr); |
| 54 | |
| 55 | return base; |
| 56 | } |
| 57 | |
| 58 | static void __init earlycon_init(struct earlycon_device *device, |
| 59 | const char *name) |
| 60 | { |
| 61 | struct console *earlycon = device->con; |
| 62 | struct uart_port *port = &device->port; |
| 63 | const char *s; |
| 64 | size_t len; |
| 65 | |
| 66 | /* scan backwards from end of string for first non-numeral */ |
| 67 | for (s = name + strlen(name); |
| 68 | s > name && s[-1] >= '0' && s[-1] <= '9'; |
| 69 | s--) |
| 70 | ; |
| 71 | if (*s) |
| 72 | earlycon->index = simple_strtoul(s, NULL, 10); |
| 73 | len = s - name; |
| 74 | strlcpy(earlycon->name, name, min(len + 1, sizeof(earlycon->name))); |
| 75 | earlycon->data = &early_console_dev; |
| 76 | |
| 77 | if (port->iotype == UPIO_MEM || port->iotype == UPIO_MEM16 || |
| 78 | port->iotype == UPIO_MEM32 || port->iotype == UPIO_MEM32BE) |
| 79 | pr_info("%s%d at MMIO%s %pa (options '%s')\n", |
| 80 | earlycon->name, earlycon->index, |
| 81 | (port->iotype == UPIO_MEM) ? "" : |
| 82 | (port->iotype == UPIO_MEM16) ? "16" : |
| 83 | (port->iotype == UPIO_MEM32) ? "32" : "32be", |
| 84 | &port->mapbase, device->options); |
| 85 | else |
| 86 | pr_info("%s%d at I/O port 0x%lx (options '%s')\n", |
| 87 | earlycon->name, earlycon->index, |
| 88 | port->iobase, device->options); |
| 89 | } |
| 90 | |
| 91 | static int __init parse_options(struct earlycon_device *device, char *options) |
| 92 | { |
| 93 | struct uart_port *port = &device->port; |
| 94 | int length; |
| 95 | resource_size_t addr; |
| 96 | |
| 97 | if (uart_parse_earlycon(options, &port->iotype, &addr, &options)) |
| 98 | return -EINVAL; |
| 99 | |
| 100 | switch (port->iotype) { |
| 101 | case UPIO_MEM: |
| 102 | port->mapbase = addr; |
| 103 | break; |
| 104 | case UPIO_MEM16: |
| 105 | port->regshift = 1; |
| 106 | port->mapbase = addr; |
| 107 | break; |
| 108 | case UPIO_MEM32: |
| 109 | case UPIO_MEM32BE: |
| 110 | port->regshift = 2; |
| 111 | port->mapbase = addr; |
| 112 | break; |
| 113 | case UPIO_PORT: |
| 114 | port->iobase = addr; |
| 115 | break; |
| 116 | default: |
| 117 | return -EINVAL; |
| 118 | } |
| 119 | |
| 120 | if (options) { |
| 121 | device->baud = simple_strtoul(options, NULL, 0); |
| 122 | length = min(strcspn(options, " ") + 1, |
| 123 | (size_t)(sizeof(device->options))); |
| 124 | strlcpy(device->options, options, length); |
| 125 | } |
| 126 | |
| 127 | return 0; |
| 128 | } |
| 129 | |
| 130 | static int __init register_earlycon(char *buf, const struct earlycon_id *match) |
| 131 | { |
| 132 | int err; |
| 133 | struct uart_port *port = &early_console_dev.port; |
| 134 | |
| 135 | /* On parsing error, pass the options buf to the setup function */ |
| 136 | if (buf && !parse_options(&early_console_dev, buf)) |
| 137 | buf = NULL; |
| 138 | |
| 139 | spin_lock_init(&port->lock); |
| 140 | port->uartclk = BASE_BAUD * 16; |
| 141 | if (port->mapbase) |
| 142 | port->membase = earlycon_map(port->mapbase, 64); |
| 143 | |
| 144 | earlycon_init(&early_console_dev, match->name); |
| 145 | err = match->setup(&early_console_dev, buf); |
| 146 | if (err < 0) |
| 147 | return err; |
| 148 | if (!early_console_dev.con->write) |
| 149 | return -ENODEV; |
| 150 | |
| 151 | register_console(early_console_dev.con); |
| 152 | return 0; |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * setup_earlycon - match and register earlycon console |
| 157 | * @buf: earlycon param string |
| 158 | * |
| 159 | * Registers the earlycon console matching the earlycon specified |
| 160 | * in the param string @buf. Acceptable param strings are of the form |
| 161 | * <name>,io|mmio|mmio32|mmio32be,<addr>,<options> |
| 162 | * <name>,0x<addr>,<options> |
| 163 | * <name>,<options> |
| 164 | * <name> |
| 165 | * |
| 166 | * Only for the third form does the earlycon setup() method receive the |
| 167 | * <options> string in the 'options' parameter; all other forms set |
| 168 | * the parameter to NULL. |
| 169 | * |
| 170 | * Returns 0 if an attempt to register the earlycon was made, |
| 171 | * otherwise negative error code |
| 172 | */ |
| 173 | int __init setup_earlycon(char *buf) |
| 174 | { |
| 175 | const struct earlycon_id **p_match; |
| 176 | |
| 177 | if (!buf || !buf[0]) |
| 178 | return -EINVAL; |
| 179 | |
| 180 | if (early_con.flags & CON_ENABLED) |
| 181 | return -EALREADY; |
| 182 | |
| 183 | for (p_match = __earlycon_table; p_match < __earlycon_table_end; |
| 184 | p_match++) { |
| 185 | const struct earlycon_id *match = *p_match; |
| 186 | size_t len = strlen(match->name); |
| 187 | |
| 188 | if (strncmp(buf, match->name, len)) |
| 189 | continue; |
| 190 | |
| 191 | if (buf[len]) { |
| 192 | if (buf[len] != ',') |
| 193 | continue; |
| 194 | buf += len + 1; |
| 195 | } else |
| 196 | buf = NULL; |
| 197 | |
| 198 | return register_earlycon(buf, match); |
| 199 | } |
| 200 | |
| 201 | return -ENOENT; |
| 202 | } |
| 203 | |
| 204 | /* |
| 205 | * When CONFIG_ACPI_SPCR_TABLE is defined, "earlycon" without parameters in |
| 206 | * command line does not start DT earlycon immediately, instead it defers |
| 207 | * starting it until DT/ACPI decision is made. At that time if ACPI is enabled |
| 208 | * call parse_spcr(), else call early_init_dt_scan_chosen_stdout() |
| 209 | */ |
| 210 | bool earlycon_init_is_deferred __initdata; |
| 211 | |
| 212 | /* early_param wrapper for setup_earlycon() */ |
| 213 | static int __init param_setup_earlycon(char *buf) |
| 214 | { |
| 215 | int err; |
| 216 | |
| 217 | /* |
| 218 | * Just 'earlycon' is a valid param for devicetree earlycons; |
| 219 | * don't generate a warning from parse_early_params() in that case |
| 220 | */ |
| 221 | if (!buf || !buf[0]) { |
| 222 | if (IS_ENABLED(CONFIG_ACPI_SPCR_TABLE)) { |
| 223 | earlycon_init_is_deferred = true; |
| 224 | return 0; |
| 225 | } else if (!buf) { |
| 226 | return early_init_dt_scan_chosen_stdout(); |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | err = setup_earlycon(buf); |
| 231 | if (err == -ENOENT || err == -EALREADY) |
| 232 | return 0; |
| 233 | return err; |
| 234 | } |
| 235 | early_param("earlycon", param_setup_earlycon); |
| 236 | |
| 237 | #ifdef CONFIG_OF_EARLY_FLATTREE |
| 238 | |
| 239 | int __init of_setup_earlycon(const struct earlycon_id *match, |
| 240 | unsigned long node, |
| 241 | const char *options) |
| 242 | { |
| 243 | int err; |
| 244 | struct uart_port *port = &early_console_dev.port; |
| 245 | const __be32 *val; |
| 246 | bool big_endian; |
| 247 | u64 addr; |
| 248 | |
| 249 | spin_lock_init(&port->lock); |
| 250 | port->iotype = UPIO_MEM; |
| 251 | addr = of_flat_dt_translate_address(node); |
| 252 | if (addr == OF_BAD_ADDR) { |
| 253 | pr_warn("[%s] bad address\n", match->name); |
| 254 | return -ENXIO; |
| 255 | } |
| 256 | port->mapbase = addr; |
| 257 | |
| 258 | val = of_get_flat_dt_prop(node, "reg-offset", NULL); |
| 259 | if (val) |
| 260 | port->mapbase += be32_to_cpu(*val); |
| 261 | port->membase = earlycon_map(port->mapbase, SZ_4K); |
| 262 | |
| 263 | val = of_get_flat_dt_prop(node, "reg-shift", NULL); |
| 264 | if (val) |
| 265 | port->regshift = be32_to_cpu(*val); |
| 266 | big_endian = of_get_flat_dt_prop(node, "big-endian", NULL) != NULL || |
| 267 | (IS_ENABLED(CONFIG_CPU_BIG_ENDIAN) && |
| 268 | of_get_flat_dt_prop(node, "native-endian", NULL) != NULL); |
| 269 | val = of_get_flat_dt_prop(node, "reg-io-width", NULL); |
| 270 | if (val) { |
| 271 | switch (be32_to_cpu(*val)) { |
| 272 | case 1: |
| 273 | port->iotype = UPIO_MEM; |
| 274 | break; |
| 275 | case 2: |
| 276 | port->iotype = UPIO_MEM16; |
| 277 | break; |
| 278 | case 4: |
| 279 | port->iotype = (big_endian) ? UPIO_MEM32BE : UPIO_MEM32; |
| 280 | break; |
| 281 | default: |
| 282 | pr_warn("[%s] unsupported reg-io-width\n", match->name); |
| 283 | return -EINVAL; |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | val = of_get_flat_dt_prop(node, "current-speed", NULL); |
| 288 | if (val) |
| 289 | early_console_dev.baud = be32_to_cpu(*val); |
| 290 | |
| 291 | val = of_get_flat_dt_prop(node, "clock-frequency", NULL); |
| 292 | if (val) |
| 293 | port->uartclk = be32_to_cpu(*val); |
| 294 | |
| 295 | if (options) { |
| 296 | early_console_dev.baud = simple_strtoul(options, NULL, 0); |
| 297 | strlcpy(early_console_dev.options, options, |
| 298 | sizeof(early_console_dev.options)); |
| 299 | } |
| 300 | earlycon_init(&early_console_dev, match->name); |
| 301 | err = match->setup(&early_console_dev, options); |
| 302 | if (err < 0) |
| 303 | return err; |
| 304 | if (!early_console_dev.con->write) |
| 305 | return -ENODEV; |
| 306 | |
| 307 | |
| 308 | register_console(early_console_dev.con); |
| 309 | return 0; |
| 310 | } |
| 311 | |
| 312 | #endif /* CONFIG_OF_EARLY_FLATTREE */ |