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> |
| 22 | #include <linux/mod_devicetable.h> |
Rob Herring | 9aac588 | 2014-04-18 17:19:55 -0500 | [diff] [blame] | 23 | |
| 24 | #ifdef CONFIG_FIX_EARLYCON_MEM |
| 25 | #include <asm/fixmap.h> |
| 26 | #endif |
| 27 | |
| 28 | #include <asm/serial.h> |
| 29 | |
| 30 | static struct console early_con = { |
Rob Herring | 60efcf0 | 2014-06-12 12:52:44 -0500 | [diff] [blame] | 31 | .name = "uart", /* 8250 console switch requires this name */ |
Rob Herring | 9aac588 | 2014-04-18 17:19:55 -0500 | [diff] [blame] | 32 | .flags = CON_PRINTBUFFER | CON_BOOT, |
| 33 | .index = -1, |
| 34 | }; |
| 35 | |
| 36 | static struct earlycon_device early_console_dev = { |
| 37 | .con = &early_con, |
| 38 | }; |
| 39 | |
Peter Hurley | 470ca0d | 2015-03-09 16:27:21 -0400 | [diff] [blame] | 40 | extern struct earlycon_id __earlycon_table[]; |
| 41 | static const struct earlycon_id __earlycon_table_sentinel |
| 42 | __used __section(__earlycon_table_end); |
| 43 | |
Rob Herring | b0b6abd | 2014-03-27 08:06:16 -0500 | [diff] [blame] | 44 | static const struct of_device_id __earlycon_of_table_sentinel |
| 45 | __used __section(__earlycon_of_table_end); |
| 46 | |
Rob Herring | 9aac588 | 2014-04-18 17:19:55 -0500 | [diff] [blame] | 47 | static void __iomem * __init earlycon_map(unsigned long paddr, size_t size) |
| 48 | { |
| 49 | void __iomem *base; |
| 50 | #ifdef CONFIG_FIX_EARLYCON_MEM |
| 51 | set_fixmap_io(FIX_EARLYCON_MEM_BASE, paddr & PAGE_MASK); |
| 52 | base = (void __iomem *)__fix_to_virt(FIX_EARLYCON_MEM_BASE); |
| 53 | base += paddr & ~PAGE_MASK; |
| 54 | #else |
| 55 | base = ioremap(paddr, size); |
| 56 | #endif |
| 57 | if (!base) |
| 58 | pr_err("%s: Couldn't map 0x%llx\n", __func__, |
| 59 | (unsigned long long)paddr); |
| 60 | |
| 61 | return base; |
| 62 | } |
| 63 | |
Peter Hurley | 73abaf8 | 2015-03-01 11:05:46 -0500 | [diff] [blame] | 64 | static int __init parse_options(struct earlycon_device *device, char *options) |
Rob Herring | 9aac588 | 2014-04-18 17:19:55 -0500 | [diff] [blame] | 65 | { |
| 66 | struct uart_port *port = &device->port; |
Peter Hurley | 73abaf8 | 2015-03-01 11:05:46 -0500 | [diff] [blame] | 67 | int length; |
Rob Herring | 9aac588 | 2014-04-18 17:19:55 -0500 | [diff] [blame] | 68 | unsigned long addr; |
| 69 | |
Peter Hurley | 73abaf8 | 2015-03-01 11:05:46 -0500 | [diff] [blame] | 70 | if (uart_parse_earlycon(options, &port->iotype, &addr, &options)) |
| 71 | return -EINVAL; |
Rob Herring | 9aac588 | 2014-04-18 17:19:55 -0500 | [diff] [blame] | 72 | |
Peter Hurley | 73abaf8 | 2015-03-01 11:05:46 -0500 | [diff] [blame] | 73 | switch (port->iotype) { |
Masahiro Yamada | bd94c40 | 2015-10-28 12:46:05 +0900 | [diff] [blame] | 74 | case UPIO_MEM: |
| 75 | port->mapbase = addr; |
| 76 | break; |
| 77 | case UPIO_MEM16: |
| 78 | port->regshift = 1; |
| 79 | port->mapbase = addr; |
| 80 | break; |
Peter Hurley | 73abaf8 | 2015-03-01 11:05:46 -0500 | [diff] [blame] | 81 | case UPIO_MEM32: |
Noam Camus | 6e63be3 | 2015-05-25 06:54:28 +0300 | [diff] [blame] | 82 | case UPIO_MEM32BE: |
Masahiro Yamada | bd94c40 | 2015-10-28 12:46:05 +0900 | [diff] [blame] | 83 | port->regshift = 2; |
Rob Herring | 9aac588 | 2014-04-18 17:19:55 -0500 | [diff] [blame] | 84 | port->mapbase = addr; |
Peter Hurley | 73abaf8 | 2015-03-01 11:05:46 -0500 | [diff] [blame] | 85 | break; |
| 86 | case UPIO_PORT: |
Rob Herring | 9aac588 | 2014-04-18 17:19:55 -0500 | [diff] [blame] | 87 | port->iobase = addr; |
Peter Hurley | 73abaf8 | 2015-03-01 11:05:46 -0500 | [diff] [blame] | 88 | break; |
| 89 | default: |
Rob Herring | 9aac588 | 2014-04-18 17:19:55 -0500 | [diff] [blame] | 90 | return -EINVAL; |
| 91 | } |
| 92 | |
Rob Herring | 9aac588 | 2014-04-18 17:19:55 -0500 | [diff] [blame] | 93 | if (options) { |
Rob Herring | e26f1db | 2014-04-30 19:48:29 -0500 | [diff] [blame] | 94 | device->baud = simple_strtoul(options, NULL, 0); |
Rob Herring | 9aac588 | 2014-04-18 17:19:55 -0500 | [diff] [blame] | 95 | length = min(strcspn(options, " ") + 1, |
| 96 | (size_t)(sizeof(device->options))); |
| 97 | strlcpy(device->options, options, length); |
| 98 | } |
| 99 | |
Masahiro Yamada | bd94c40 | 2015-10-28 12:46:05 +0900 | [diff] [blame] | 100 | if (port->iotype == UPIO_MEM || port->iotype == UPIO_MEM16 || |
| 101 | port->iotype == UPIO_MEM32 || port->iotype == UPIO_MEM32BE) |
Rob Herring | 9aac588 | 2014-04-18 17:19:55 -0500 | [diff] [blame] | 102 | 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] | 103 | (port->iotype == UPIO_MEM) ? "" : |
Masahiro Yamada | bd94c40 | 2015-10-28 12:46:05 +0900 | [diff] [blame] | 104 | (port->iotype == UPIO_MEM16) ? "16" : |
Noam Camus | 6e63be3 | 2015-05-25 06:54:28 +0300 | [diff] [blame] | 105 | (port->iotype == UPIO_MEM32) ? "32" : "32be", |
Rob Herring | 9aac588 | 2014-04-18 17:19:55 -0500 | [diff] [blame] | 106 | (unsigned long long)port->mapbase, |
| 107 | device->options); |
| 108 | else |
| 109 | pr_info("Early serial console at I/O port 0x%lx (options '%s')\n", |
| 110 | port->iobase, |
| 111 | device->options); |
| 112 | |
| 113 | return 0; |
| 114 | } |
| 115 | |
Peter Hurley | 470ca0d | 2015-03-09 16:27:21 -0400 | [diff] [blame] | 116 | static int __init register_earlycon(char *buf, const struct earlycon_id *match) |
Rob Herring | 9aac588 | 2014-04-18 17:19:55 -0500 | [diff] [blame] | 117 | { |
| 118 | int err; |
Rob Herring | 9aac588 | 2014-04-18 17:19:55 -0500 | [diff] [blame] | 119 | struct uart_port *port = &early_console_dev.port; |
| 120 | |
Rob Herring | 9aac588 | 2014-04-18 17:19:55 -0500 | [diff] [blame] | 121 | /* On parsing error, pass the options buf to the setup function */ |
Peter Hurley | 6084688 | 2015-03-09 16:27:19 -0400 | [diff] [blame] | 122 | if (buf && !parse_options(&early_console_dev, buf)) |
Rob Herring | 9aac588 | 2014-04-18 17:19:55 -0500 | [diff] [blame] | 123 | buf = NULL; |
| 124 | |
Geert Uytterhoeven | e1dd3be | 2015-11-27 11:13:48 +0100 | [diff] [blame] | 125 | spin_lock_init(&port->lock); |
Peter Hurley | feed5ba | 2015-03-09 16:27:15 -0400 | [diff] [blame] | 126 | port->uartclk = BASE_BAUD * 16; |
Rob Herring | 9aac588 | 2014-04-18 17:19:55 -0500 | [diff] [blame] | 127 | if (port->mapbase) |
| 128 | port->membase = earlycon_map(port->mapbase, 64); |
| 129 | |
| 130 | early_console_dev.con->data = &early_console_dev; |
Peter Hurley | 470ca0d | 2015-03-09 16:27:21 -0400 | [diff] [blame] | 131 | err = match->setup(&early_console_dev, buf); |
Rob Herring | 9aac588 | 2014-04-18 17:19:55 -0500 | [diff] [blame] | 132 | if (err < 0) |
| 133 | return err; |
| 134 | if (!early_console_dev.con->write) |
| 135 | return -ENODEV; |
| 136 | |
| 137 | register_console(early_console_dev.con); |
| 138 | return 0; |
| 139 | } |
Rob Herring | b0b6abd | 2014-03-27 08:06:16 -0500 | [diff] [blame] | 140 | |
Peter Hurley | 470ca0d | 2015-03-09 16:27:21 -0400 | [diff] [blame] | 141 | /** |
| 142 | * setup_earlycon - match and register earlycon console |
| 143 | * @buf: earlycon param string |
| 144 | * |
| 145 | * Registers the earlycon console matching the earlycon specified |
| 146 | * in the param string @buf. Acceptable param strings are of the form |
Noam Camus | 6e63be3 | 2015-05-25 06:54:28 +0300 | [diff] [blame] | 147 | * <name>,io|mmio|mmio32|mmio32be,<addr>,<options> |
Peter Hurley | 470ca0d | 2015-03-09 16:27:21 -0400 | [diff] [blame] | 148 | * <name>,0x<addr>,<options> |
| 149 | * <name>,<options> |
| 150 | * <name> |
| 151 | * |
| 152 | * Only for the third form does the earlycon setup() method receive the |
| 153 | * <options> string in the 'options' parameter; all other forms set |
| 154 | * the parameter to NULL. |
| 155 | * |
| 156 | * Returns 0 if an attempt to register the earlycon was made, |
| 157 | * otherwise negative error code |
| 158 | */ |
| 159 | int __init setup_earlycon(char *buf) |
Peter Hurley | 7c53cb3 | 2015-03-09 16:27:20 -0400 | [diff] [blame] | 160 | { |
Peter Hurley | 470ca0d | 2015-03-09 16:27:21 -0400 | [diff] [blame] | 161 | const struct earlycon_id *match; |
Peter Hurley | 7c53cb3 | 2015-03-09 16:27:20 -0400 | [diff] [blame] | 162 | |
Peter Hurley | 470ca0d | 2015-03-09 16:27:21 -0400 | [diff] [blame] | 163 | if (!buf || !buf[0]) |
| 164 | return -EINVAL; |
Peter Hurley | 7c53cb3 | 2015-03-09 16:27:20 -0400 | [diff] [blame] | 165 | |
Peter Hurley | 470ca0d | 2015-03-09 16:27:21 -0400 | [diff] [blame] | 166 | if (early_con.flags & CON_ENABLED) |
| 167 | return -EALREADY; |
Peter Hurley | 7c53cb3 | 2015-03-09 16:27:20 -0400 | [diff] [blame] | 168 | |
Peter Hurley | 470ca0d | 2015-03-09 16:27:21 -0400 | [diff] [blame] | 169 | for (match = __earlycon_table; match->name[0]; match++) { |
| 170 | size_t len = strlen(match->name); |
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 | if (strncmp(buf, match->name, len)) |
| 173 | continue; |
| 174 | |
| 175 | if (buf[len]) { |
| 176 | if (buf[len] != ',') |
| 177 | continue; |
| 178 | buf += len + 1; |
| 179 | } else |
| 180 | buf = NULL; |
| 181 | |
| 182 | return register_earlycon(buf, match); |
| 183 | } |
| 184 | |
| 185 | return -ENOENT; |
Peter Hurley | 7c53cb3 | 2015-03-09 16:27:20 -0400 | [diff] [blame] | 186 | } |
| 187 | |
Peter Hurley | 470ca0d | 2015-03-09 16:27:21 -0400 | [diff] [blame] | 188 | /* early_param wrapper for setup_earlycon() */ |
| 189 | static int __init param_setup_earlycon(char *buf) |
| 190 | { |
| 191 | int err; |
| 192 | |
| 193 | /* |
| 194 | * Just 'earlycon' is a valid param for devicetree earlycons; |
| 195 | * don't generate a warning from parse_early_params() in that case |
| 196 | */ |
| 197 | if (!buf || !buf[0]) |
| 198 | return 0; |
| 199 | |
| 200 | err = setup_earlycon(buf); |
Peter Hurley | 66c53aa | 2015-05-07 14:19:21 -0400 | [diff] [blame] | 201 | if (err == -ENOENT || err == -EALREADY) |
| 202 | return 0; |
Peter Hurley | 470ca0d | 2015-03-09 16:27:21 -0400 | [diff] [blame] | 203 | return err; |
| 204 | } |
| 205 | early_param("earlycon", param_setup_earlycon); |
| 206 | |
Peter Hurley | 8477614 | 2016-01-16 15:23:38 -0800 | [diff] [blame^] | 207 | #ifdef CONFIG_OF_EARLY_FLATTREE |
| 208 | |
Rob Herring | b0b6abd | 2014-03-27 08:06:16 -0500 | [diff] [blame] | 209 | int __init of_setup_earlycon(unsigned long addr, |
| 210 | int (*setup)(struct earlycon_device *, const char *)) |
| 211 | { |
| 212 | int err; |
| 213 | struct uart_port *port = &early_console_dev.port; |
| 214 | |
Geert Uytterhoeven | e1dd3be | 2015-11-27 11:13:48 +0100 | [diff] [blame] | 215 | spin_lock_init(&port->lock); |
Rob Herring | b0b6abd | 2014-03-27 08:06:16 -0500 | [diff] [blame] | 216 | port->iotype = UPIO_MEM; |
| 217 | port->mapbase = addr; |
| 218 | port->uartclk = BASE_BAUD * 16; |
| 219 | port->membase = earlycon_map(addr, SZ_4K); |
| 220 | |
| 221 | early_console_dev.con->data = &early_console_dev; |
| 222 | err = setup(&early_console_dev, NULL); |
| 223 | if (err < 0) |
| 224 | return err; |
| 225 | if (!early_console_dev.con->write) |
| 226 | return -ENODEV; |
| 227 | |
| 228 | |
| 229 | register_console(early_console_dev.con); |
| 230 | return 0; |
| 231 | } |
Peter Hurley | 8477614 | 2016-01-16 15:23:38 -0800 | [diff] [blame^] | 232 | |
| 233 | #endif /* CONFIG_OF_EARLY_FLATTREE */ |