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