Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[cris-mirror.git] / drivers / tty / serial / earlycon.c
blob870e84fb6e39ebb8fbfbc31f1a3608caddf6072d
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (C) 2014 Linaro Ltd.
4 * Author: Rob Herring <robh@kernel.org>
6 * Based on 8250 earlycon:
7 * (c) Copyright 2004 Hewlett-Packard Development Company, L.P.
8 * Bjorn Helgaas <bjorn.helgaas@hp.com>
9 */
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
18 #include <linux/sizes.h>
19 #include <linux/of.h>
20 #include <linux/of_fdt.h>
21 #include <linux/acpi.h>
23 #ifdef CONFIG_FIX_EARLYCON_MEM
24 #include <asm/fixmap.h>
25 #endif
27 #include <asm/serial.h>
29 static struct console early_con = {
30 .name = "uart", /* fixed up at earlycon registration */
31 .flags = CON_PRINTBUFFER | CON_BOOT,
32 .index = 0,
35 static struct earlycon_device early_console_dev = {
36 .con = &early_con,
39 static void __iomem * __init earlycon_map(resource_size_t paddr, size_t size)
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 %pa\n", __func__, &paddr);
52 return base;
55 static void __init earlycon_init(struct earlycon_device *device,
56 const char *name)
58 struct console *earlycon = device->con;
59 struct uart_port *port = &device->port;
60 const char *s;
61 size_t len;
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--)
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;
74 if (port->iotype == UPIO_MEM || port->iotype == UPIO_MEM16 ||
75 port->iotype == UPIO_MEM32 || port->iotype == UPIO_MEM32BE)
76 pr_info("%s%d at MMIO%s %pa (options '%s')\n",
77 earlycon->name, earlycon->index,
78 (port->iotype == UPIO_MEM) ? "" :
79 (port->iotype == UPIO_MEM16) ? "16" :
80 (port->iotype == UPIO_MEM32) ? "32" : "32be",
81 &port->mapbase, device->options);
82 else
83 pr_info("%s%d at I/O port 0x%lx (options '%s')\n",
84 earlycon->name, earlycon->index,
85 port->iobase, device->options);
88 static int __init parse_options(struct earlycon_device *device, char *options)
90 struct uart_port *port = &device->port;
91 int length;
92 resource_size_t addr;
94 if (uart_parse_earlycon(options, &port->iotype, &addr, &options))
95 return -EINVAL;
97 switch (port->iotype) {
98 case UPIO_MEM:
99 port->mapbase = addr;
100 break;
101 case UPIO_MEM16:
102 port->regshift = 1;
103 port->mapbase = addr;
104 break;
105 case UPIO_MEM32:
106 case UPIO_MEM32BE:
107 port->regshift = 2;
108 port->mapbase = addr;
109 break;
110 case UPIO_PORT:
111 port->iobase = addr;
112 break;
113 default:
114 return -EINVAL;
117 if (options) {
118 device->baud = simple_strtoul(options, NULL, 0);
119 length = min(strcspn(options, " ") + 1,
120 (size_t)(sizeof(device->options)));
121 strlcpy(device->options, options, length);
124 return 0;
127 static int __init register_earlycon(char *buf, const struct earlycon_id *match)
129 int err;
130 struct uart_port *port = &early_console_dev.port;
132 /* On parsing error, pass the options buf to the setup function */
133 if (buf && !parse_options(&early_console_dev, buf))
134 buf = NULL;
136 spin_lock_init(&port->lock);
137 port->uartclk = BASE_BAUD * 16;
138 if (port->mapbase)
139 port->membase = earlycon_map(port->mapbase, 64);
141 earlycon_init(&early_console_dev, match->name);
142 err = match->setup(&early_console_dev, buf);
143 if (err < 0)
144 return err;
145 if (!early_console_dev.con->write)
146 return -ENODEV;
148 register_console(early_console_dev.con);
149 return 0;
153 * setup_earlycon - match and register earlycon console
154 * @buf: earlycon param string
156 * Registers the earlycon console matching the earlycon specified
157 * in the param string @buf. Acceptable param strings are of the form
158 * <name>,io|mmio|mmio32|mmio32be,<addr>,<options>
159 * <name>,0x<addr>,<options>
160 * <name>,<options>
161 * <name>
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.
167 * Returns 0 if an attempt to register the earlycon was made,
168 * otherwise negative error code
170 int __init setup_earlycon(char *buf)
172 const struct earlycon_id *match;
174 if (!buf || !buf[0])
175 return -EINVAL;
177 if (early_con.flags & CON_ENABLED)
178 return -EALREADY;
180 for (match = __earlycon_table; match < __earlycon_table_end; match++) {
181 size_t len = strlen(match->name);
183 if (strncmp(buf, match->name, len))
184 continue;
186 if (buf[len]) {
187 if (buf[len] != ',')
188 continue;
189 buf += len + 1;
190 } else
191 buf = NULL;
193 return register_earlycon(buf, match);
196 return -ENOENT;
200 * This defers the initialization of the early console until after ACPI has
201 * been initialized.
203 bool earlycon_acpi_spcr_enable __initdata;
205 /* early_param wrapper for setup_earlycon() */
206 static int __init param_setup_earlycon(char *buf)
208 int err;
210 /* Just 'earlycon' is a valid param for devicetree and ACPI SPCR. */
211 if (!buf || !buf[0]) {
212 if (IS_ENABLED(CONFIG_ACPI_SPCR_TABLE)) {
213 earlycon_acpi_spcr_enable = true;
214 return 0;
215 } else if (!buf) {
216 return early_init_dt_scan_chosen_stdout();
220 err = setup_earlycon(buf);
221 if (err == -ENOENT || err == -EALREADY)
222 return 0;
223 return err;
225 early_param("earlycon", param_setup_earlycon);
227 #ifdef CONFIG_OF_EARLY_FLATTREE
229 int __init of_setup_earlycon(const struct earlycon_id *match,
230 unsigned long node,
231 const char *options)
233 int err;
234 struct uart_port *port = &early_console_dev.port;
235 const __be32 *val;
236 bool big_endian;
237 u64 addr;
239 spin_lock_init(&port->lock);
240 port->iotype = UPIO_MEM;
241 addr = of_flat_dt_translate_address(node);
242 if (addr == OF_BAD_ADDR) {
243 pr_warn("[%s] bad address\n", match->name);
244 return -ENXIO;
246 port->mapbase = addr;
247 port->uartclk = BASE_BAUD * 16;
248 port->membase = earlycon_map(port->mapbase, SZ_4K);
250 val = of_get_flat_dt_prop(node, "reg-offset", NULL);
251 if (val)
252 port->mapbase += be32_to_cpu(*val);
253 val = of_get_flat_dt_prop(node, "reg-shift", NULL);
254 if (val)
255 port->regshift = be32_to_cpu(*val);
256 big_endian = of_get_flat_dt_prop(node, "big-endian", NULL) != NULL ||
257 (IS_ENABLED(CONFIG_CPU_BIG_ENDIAN) &&
258 of_get_flat_dt_prop(node, "native-endian", NULL) != NULL);
259 val = of_get_flat_dt_prop(node, "reg-io-width", NULL);
260 if (val) {
261 switch (be32_to_cpu(*val)) {
262 case 1:
263 port->iotype = UPIO_MEM;
264 break;
265 case 2:
266 port->iotype = UPIO_MEM16;
267 break;
268 case 4:
269 port->iotype = (big_endian) ? UPIO_MEM32BE : UPIO_MEM32;
270 break;
271 default:
272 pr_warn("[%s] unsupported reg-io-width\n", match->name);
273 return -EINVAL;
277 val = of_get_flat_dt_prop(node, "current-speed", NULL);
278 if (val)
279 early_console_dev.baud = be32_to_cpu(*val);
281 if (options) {
282 early_console_dev.baud = simple_strtoul(options, NULL, 0);
283 strlcpy(early_console_dev.options, options,
284 sizeof(early_console_dev.options));
286 earlycon_init(&early_console_dev, match->name);
287 err = match->setup(&early_console_dev, options);
288 if (err < 0)
289 return err;
290 if (!early_console_dev.con->write)
291 return -ENODEV;
294 register_console(early_console_dev.con);
295 return 0;
298 #endif /* CONFIG_OF_EARLY_FLATTREE */