Rob Herring | 9aac588 | 2014-04-18 17:19:55 -0500 | [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 | */ |
Peter Hurley | 470ca0d | 2015-03-09 16:27:21 -0400 | [diff] [blame] | 13 | |
| 14 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 15 | |
Rob Herring | 9aac588 | 2014-04-18 17:19:55 -0500 | [diff] [blame] | 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> |
Rob Herring | b0b6abd | 2014-03-27 08:06:16 -0500 | [diff] [blame] | 21 | #include <linux/sizes.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 | |
| 39 | static void __iomem * __init earlycon_map(unsigned long paddr, size_t size) |
| 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) |
| 50 | pr_err("%s: Couldn't map 0x%llx\n", __func__, |
| 51 | (unsigned long long)paddr); |
| 52 | |
| 53 | return base; |
| 54 | } |
| 55 | |
Peter Hurley | cda64e6 | 2016-01-16 15:23:40 -0800 | [diff] [blame] | 56 | static void __init earlycon_init(struct earlycon_device *device, |
| 57 | const char *name) |
| 58 | { |
| 59 | struct console *earlycon = device->con; |
| 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; |
| 73 | } |
| 74 | |
Peter Hurley | 73abaf8 | 2015-03-01 11:05:46 -0500 | [diff] [blame] | 75 | static int __init parse_options(struct earlycon_device *device, char *options) |
Rob Herring | 9aac588 | 2014-04-18 17:19:55 -0500 | [diff] [blame] | 76 | { |
| 77 | struct uart_port *port = &device->port; |
Peter Hurley | 73abaf8 | 2015-03-01 11:05:46 -0500 | [diff] [blame] | 78 | int length; |
Rob Herring | 9aac588 | 2014-04-18 17:19:55 -0500 | [diff] [blame] | 79 | unsigned long addr; |
| 80 | |
Peter Hurley | 73abaf8 | 2015-03-01 11:05:46 -0500 | [diff] [blame] | 81 | if (uart_parse_earlycon(options, &port->iotype, &addr, &options)) |
| 82 | return -EINVAL; |
Rob Herring | 9aac588 | 2014-04-18 17:19:55 -0500 | [diff] [blame] | 83 | |
Peter Hurley | 73abaf8 | 2015-03-01 11:05:46 -0500 | [diff] [blame] | 84 | switch (port->iotype) { |
Masahiro Yamada | bd94c40 | 2015-10-28 12:46:05 +0900 | [diff] [blame] | 85 | case UPIO_MEM: |
| 86 | port->mapbase = addr; |
| 87 | break; |
| 88 | case UPIO_MEM16: |
| 89 | port->regshift = 1; |
| 90 | port->mapbase = addr; |
| 91 | break; |
Peter Hurley | 73abaf8 | 2015-03-01 11:05:46 -0500 | [diff] [blame] | 92 | case UPIO_MEM32: |
Noam Camus | 6e63be3 | 2015-05-25 06:54:28 +0300 | [diff] [blame] | 93 | case UPIO_MEM32BE: |
Masahiro Yamada | bd94c40 | 2015-10-28 12:46:05 +0900 | [diff] [blame] | 94 | port->regshift = 2; |
Rob Herring | 9aac588 | 2014-04-18 17:19:55 -0500 | [diff] [blame] | 95 | port->mapbase = addr; |
Peter Hurley | 73abaf8 | 2015-03-01 11:05:46 -0500 | [diff] [blame] | 96 | break; |
| 97 | case UPIO_PORT: |
Rob Herring | 9aac588 | 2014-04-18 17:19:55 -0500 | [diff] [blame] | 98 | port->iobase = addr; |
Peter Hurley | 73abaf8 | 2015-03-01 11:05:46 -0500 | [diff] [blame] | 99 | break; |
| 100 | default: |
Rob Herring | 9aac588 | 2014-04-18 17:19:55 -0500 | [diff] [blame] | 101 | return -EINVAL; |
| 102 | } |
| 103 | |
Rob Herring | 9aac588 | 2014-04-18 17:19:55 -0500 | [diff] [blame] | 104 | if (options) { |
Rob Herring | e26f1db | 2014-04-30 19:48:29 -0500 | [diff] [blame] | 105 | device->baud = simple_strtoul(options, NULL, 0); |
Rob Herring | 9aac588 | 2014-04-18 17:19:55 -0500 | [diff] [blame] | 106 | length = min(strcspn(options, " ") + 1, |
| 107 | (size_t)(sizeof(device->options))); |
| 108 | strlcpy(device->options, options, length); |
| 109 | } |
| 110 | |
Masahiro Yamada | bd94c40 | 2015-10-28 12:46:05 +0900 | [diff] [blame] | 111 | if (port->iotype == UPIO_MEM || port->iotype == UPIO_MEM16 || |
| 112 | port->iotype == UPIO_MEM32 || port->iotype == UPIO_MEM32BE) |
Rob Herring | 9aac588 | 2014-04-18 17:19:55 -0500 | [diff] [blame] | 113 | pr_info("Early serial console at MMIO%s 0x%llx (options '%s')\n", |
Noam Camus | 6e63be3 | 2015-05-25 06:54:28 +0300 | [diff] [blame] | 114 | (port->iotype == UPIO_MEM) ? "" : |
Masahiro Yamada | bd94c40 | 2015-10-28 12:46:05 +0900 | [diff] [blame] | 115 | (port->iotype == UPIO_MEM16) ? "16" : |
Noam Camus | 6e63be3 | 2015-05-25 06:54:28 +0300 | [diff] [blame] | 116 | (port->iotype == UPIO_MEM32) ? "32" : "32be", |
Rob Herring | 9aac588 | 2014-04-18 17:19:55 -0500 | [diff] [blame] | 117 | (unsigned long long)port->mapbase, |
| 118 | device->options); |
| 119 | else |
| 120 | pr_info("Early serial console at I/O port 0x%lx (options '%s')\n", |
| 121 | port->iobase, |
| 122 | device->options); |
| 123 | |
| 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 | |
Peter Hurley | 470ca0d | 2015-03-09 16:27:21 -0400 | [diff] [blame] | 199 | /* early_param wrapper for setup_earlycon() */ |
| 200 | static int __init param_setup_earlycon(char *buf) |
| 201 | { |
| 202 | int err; |
| 203 | |
| 204 | /* |
| 205 | * Just 'earlycon' is a valid param for devicetree earlycons; |
| 206 | * don't generate a warning from parse_early_params() in that case |
| 207 | */ |
| 208 | if (!buf || !buf[0]) |
| 209 | return 0; |
| 210 | |
| 211 | err = setup_earlycon(buf); |
Peter Hurley | 66c53aa | 2015-05-07 14:19:21 -0400 | [diff] [blame] | 212 | if (err == -ENOENT || err == -EALREADY) |
| 213 | return 0; |
Peter Hurley | 470ca0d | 2015-03-09 16:27:21 -0400 | [diff] [blame] | 214 | return err; |
| 215 | } |
| 216 | early_param("earlycon", param_setup_earlycon); |
| 217 | |
Peter Hurley | 8477614 | 2016-01-16 15:23:38 -0800 | [diff] [blame] | 218 | #ifdef CONFIG_OF_EARLY_FLATTREE |
| 219 | |
Rob Herring | b0b6abd | 2014-03-27 08:06:16 -0500 | [diff] [blame] | 220 | int __init of_setup_earlycon(unsigned long addr, |
Peter Hurley | 4d118c9 | 2016-01-16 15:23:42 -0800 | [diff] [blame^] | 221 | const struct earlycon_id *match, |
| 222 | const char *options) |
Rob Herring | b0b6abd | 2014-03-27 08:06:16 -0500 | [diff] [blame] | 223 | { |
| 224 | int err; |
| 225 | struct uart_port *port = &early_console_dev.port; |
| 226 | |
Geert Uytterhoeven | e1dd3be | 2015-11-27 11:13:48 +0100 | [diff] [blame] | 227 | spin_lock_init(&port->lock); |
Rob Herring | b0b6abd | 2014-03-27 08:06:16 -0500 | [diff] [blame] | 228 | port->iotype = UPIO_MEM; |
| 229 | port->mapbase = addr; |
| 230 | port->uartclk = BASE_BAUD * 16; |
| 231 | port->membase = earlycon_map(addr, SZ_4K); |
| 232 | |
Peter Hurley | 4d118c9 | 2016-01-16 15:23:42 -0800 | [diff] [blame^] | 233 | if (options) { |
| 234 | strlcpy(early_console_dev.options, options, |
| 235 | sizeof(early_console_dev.options)); |
| 236 | } |
Peter Hurley | 05d9613 | 2016-01-16 15:23:41 -0800 | [diff] [blame] | 237 | earlycon_init(&early_console_dev, match->name); |
Peter Hurley | 4d118c9 | 2016-01-16 15:23:42 -0800 | [diff] [blame^] | 238 | err = match->setup(&early_console_dev, options); |
Rob Herring | b0b6abd | 2014-03-27 08:06:16 -0500 | [diff] [blame] | 239 | if (err < 0) |
| 240 | return err; |
| 241 | if (!early_console_dev.con->write) |
| 242 | return -ENODEV; |
| 243 | |
| 244 | |
| 245 | register_console(early_console_dev.con); |
| 246 | return 0; |
| 247 | } |
Peter Hurley | 8477614 | 2016-01-16 15:23:38 -0800 | [diff] [blame] | 248 | |
| 249 | #endif /* CONFIG_OF_EARLY_FLATTREE */ |