1 // SPDX-License-Identifier: GPL-2.0
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>
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13 #include <linux/console.h>
14 #include <linux/kernel.h>
15 #include <linux/init.h>
17 #include <linux/serial_core.h>
18 #include <linux/sizes.h>
20 #include <linux/of_fdt.h>
21 #include <linux/acpi.h>
23 #ifdef CONFIG_FIX_EARLYCON_MEM
24 #include <asm/fixmap.h>
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,
35 static struct earlycon_device early_console_dev = {
39 static void __iomem * __init earlycon_map(resource_size_t paddr, size_t size)
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;
47 base = ioremap(paddr, size);
50 pr_err("%s: Couldn't map %pa\n", __func__, &paddr);
55 static void __init earlycon_init(struct earlycon_device *device,
58 struct console *earlycon = device->con;
62 /* scan backwards from end of string for first non-numeral */
63 for (s = name + strlen(name);
64 s > name && s[-1] >= '0' && s[-1] <= '9';
68 earlycon->index = simple_strtoul(s, NULL, 10);
70 strscpy(earlycon->name, name, min(len + 1, sizeof(earlycon->name)));
71 earlycon->data = &early_console_dev;
74 static void __init earlycon_print_info(struct earlycon_device *device)
76 struct console *earlycon = device->con;
77 struct uart_port *port = &device->port;
79 if (port->iotype == UPIO_MEM || port->iotype == UPIO_MEM16 ||
80 port->iotype == UPIO_MEM32 || port->iotype == UPIO_MEM32BE)
81 pr_info("%s%d at MMIO%s %pa (options '%s')\n",
82 earlycon->name, earlycon->index,
83 (port->iotype == UPIO_MEM) ? "" :
84 (port->iotype == UPIO_MEM16) ? "16" :
85 (port->iotype == UPIO_MEM32) ? "32" : "32be",
86 &port->mapbase, device->options);
88 pr_info("%s%d at I/O port 0x%lx (options '%s')\n",
89 earlycon->name, earlycon->index,
90 port->iobase, device->options);
93 static int __init parse_options(struct earlycon_device *device, char *options)
95 struct uart_port *port = &device->port;
99 if (uart_parse_earlycon(options, &port->iotype, &addr, &options))
102 switch (port->iotype) {
104 port->mapbase = addr;
108 port->mapbase = addr;
113 port->mapbase = addr;
123 device->baud = simple_strtoul(options, NULL, 0);
124 length = min(strcspn(options, " ") + 1,
125 (size_t)(sizeof(device->options)));
126 strscpy(device->options, options, length);
132 static int __init register_earlycon(char *buf, const struct earlycon_id *match)
135 struct uart_port *port = &early_console_dev.port;
137 /* On parsing error, pass the options buf to the setup function */
138 if (buf && !parse_options(&early_console_dev, buf))
141 spin_lock_init(&port->lock);
142 port->uartclk = BASE_BAUD * 16;
144 port->membase = earlycon_map(port->mapbase, 64);
146 earlycon_init(&early_console_dev, match->name);
147 err = match->setup(&early_console_dev, buf);
148 earlycon_print_info(&early_console_dev);
151 if (!early_console_dev.con->write)
154 register_console(early_console_dev.con);
159 * setup_earlycon - match and register earlycon console
160 * @buf: earlycon param string
162 * Registers the earlycon console matching the earlycon specified
163 * in the param string @buf. Acceptable param strings are of the form
164 * <name>,io|mmio|mmio32|mmio32be,<addr>,<options>
165 * <name>,0x<addr>,<options>
169 * Only for the third form does the earlycon setup() method receive the
170 * <options> string in the 'options' parameter; all other forms set
171 * the parameter to NULL.
173 * Returns 0 if an attempt to register the earlycon was made,
174 * otherwise negative error code
176 int __init setup_earlycon(char *buf)
178 const struct earlycon_id *match;
179 bool empty_compatible = true;
184 if (early_con.flags & CON_ENABLED)
188 for (match = __earlycon_table; match < __earlycon_table_end; match++) {
189 size_t len = strlen(match->name);
191 if (strncmp(buf, match->name, len))
194 /* prefer entries with empty compatible */
195 if (empty_compatible && *match->compatible)
205 return register_earlycon(buf, match);
208 if (empty_compatible) {
209 empty_compatible = false;
217 * This defers the initialization of the early console until after ACPI has
220 bool earlycon_acpi_spcr_enable __initdata;
222 /* early_param wrapper for setup_earlycon() */
223 static int __init param_setup_earlycon(char *buf)
227 /* Just 'earlycon' is a valid param for devicetree and ACPI SPCR. */
228 if (!buf || !buf[0]) {
229 if (IS_ENABLED(CONFIG_ACPI_SPCR_TABLE)) {
230 earlycon_acpi_spcr_enable = true;
233 return early_init_dt_scan_chosen_stdout();
237 err = setup_earlycon(buf);
238 if (err == -ENOENT || err == -EALREADY)
242 early_param("earlycon", param_setup_earlycon);
244 #ifdef CONFIG_OF_EARLY_FLATTREE
246 int __init of_setup_earlycon(const struct earlycon_id *match,
251 struct uart_port *port = &early_console_dev.port;
256 if (early_con.flags & CON_ENABLED)
259 spin_lock_init(&port->lock);
260 port->iotype = UPIO_MEM;
261 addr = of_flat_dt_translate_address(node);
262 if (addr == OF_BAD_ADDR) {
263 pr_warn("[%s] bad address\n", match->name);
266 port->mapbase = addr;
268 val = of_get_flat_dt_prop(node, "reg-offset", NULL);
270 port->mapbase += be32_to_cpu(*val);
271 port->membase = earlycon_map(port->mapbase, SZ_4K);
273 val = of_get_flat_dt_prop(node, "reg-shift", NULL);
275 port->regshift = be32_to_cpu(*val);
276 big_endian = of_get_flat_dt_prop(node, "big-endian", NULL) != NULL ||
277 (IS_ENABLED(CONFIG_CPU_BIG_ENDIAN) &&
278 of_get_flat_dt_prop(node, "native-endian", NULL) != NULL);
279 val = of_get_flat_dt_prop(node, "reg-io-width", NULL);
281 switch (be32_to_cpu(*val)) {
283 port->iotype = UPIO_MEM;
286 port->iotype = UPIO_MEM16;
289 port->iotype = (big_endian) ? UPIO_MEM32BE : UPIO_MEM32;
292 pr_warn("[%s] unsupported reg-io-width\n", match->name);
297 val = of_get_flat_dt_prop(node, "current-speed", NULL);
299 early_console_dev.baud = be32_to_cpu(*val);
301 val = of_get_flat_dt_prop(node, "clock-frequency", NULL);
303 port->uartclk = be32_to_cpu(*val);
306 early_console_dev.baud = simple_strtoul(options, NULL, 0);
307 strscpy(early_console_dev.options, options,
308 sizeof(early_console_dev.options));
310 earlycon_init(&early_console_dev, match->name);
311 err = match->setup(&early_console_dev, options);
312 earlycon_print_info(&early_console_dev);
315 if (!early_console_dev.con->write)
319 register_console(early_console_dev.con);
323 #endif /* CONFIG_OF_EARLY_FLATTREE */