2 * Copyright (C) 2014 Linaro Ltd.
5 * Based on 8250 earlycon:
6 * (c) Copyright 2004 Hewlett-Packard Development Company, L.P.
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.
14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16 #include <linux/console.h>
17 #include <linux/kernel.h>
18 #include <linux/init.h>
20 #include <linux/serial_core.h>
21 #include <linux/sizes.h>
23 #include <linux/of_fdt.h>
25 #ifdef CONFIG_FIX_EARLYCON_MEM
26 #include <asm/fixmap.h>
29 #include <asm/serial.h>
31 static struct console early_con = {
32 .name = "uart", /* fixed up at earlycon registration */
33 .flags = CON_PRINTBUFFER | CON_BOOT,
37 static struct earlycon_device early_console_dev = {
41 static void __iomem * __init earlycon_map(unsigned long paddr, size_t size)
44 #ifdef CONFIG_FIX_EARLYCON_MEM
45 set_fixmap_io(FIX_EARLYCON_MEM_BASE, paddr & PAGE_MASK);
46 base = (void __iomem *)__fix_to_virt(FIX_EARLYCON_MEM_BASE);
47 base += paddr & ~PAGE_MASK;
49 base = ioremap(paddr, size);
52 pr_err("%s: Couldn't map 0x%llx\n", __func__,
53 (unsigned long long)paddr);
58 static void __init earlycon_init(struct earlycon_device *device,
61 struct console *earlycon = device->con;
62 struct uart_port *port = &device->port;
66 /* scan backwards from end of string for first non-numeral */
67 for (s = name + strlen(name);
68 s > name && s[-1] >= '0' && s[-1] <= '9';
72 earlycon->index = simple_strtoul(s, NULL, 10);
74 strlcpy(earlycon->name, name, min(len + 1, sizeof(earlycon->name)));
75 earlycon->data = &early_console_dev;
77 if (port->iotype == UPIO_MEM || port->iotype == UPIO_MEM16 ||
78 port->iotype == UPIO_MEM32 || port->iotype == UPIO_MEM32BE)
79 pr_info("%s%d at MMIO%s %pa (options '%s')\n",
80 earlycon->name, earlycon->index,
81 (port->iotype == UPIO_MEM) ? "" :
82 (port->iotype == UPIO_MEM16) ? "16" :
83 (port->iotype == UPIO_MEM32) ? "32" : "32be",
84 &port->mapbase, device->options);
86 pr_info("%s%d at I/O port 0x%lx (options '%s')\n",
87 earlycon->name, earlycon->index,
88 port->iobase, device->options);
91 static int __init parse_options(struct earlycon_device *device, char *options)
93 struct uart_port *port = &device->port;
97 if (uart_parse_earlycon(options, &port->iotype, &addr, &options))
100 switch (port->iotype) {
102 port->mapbase = addr;
106 port->mapbase = addr;
111 port->mapbase = addr;
121 device->baud = simple_strtoul(options, NULL, 0);
122 length = min(strcspn(options, " ") + 1,
123 (size_t)(sizeof(device->options)));
124 strlcpy(device->options, options, length);
130 static int __init register_earlycon(char *buf, const struct earlycon_id *match)
133 struct uart_port *port = &early_console_dev.port;
135 /* On parsing error, pass the options buf to the setup function */
136 if (buf && !parse_options(&early_console_dev, buf))
139 spin_lock_init(&port->lock);
140 port->uartclk = BASE_BAUD * 16;
142 port->membase = earlycon_map(port->mapbase, 64);
144 earlycon_init(&early_console_dev, match->name);
145 err = match->setup(&early_console_dev, buf);
148 if (!early_console_dev.con->write)
151 register_console(early_console_dev.con);
156 * setup_earlycon - match and register earlycon console
157 * @buf: earlycon param string
159 * Registers the earlycon console matching the earlycon specified
160 * in the param string @buf. Acceptable param strings are of the form
161 * <name>,io|mmio|mmio32|mmio32be,<addr>,<options>
162 * <name>,0x<addr>,<options>
166 * Only for the third form does the earlycon setup() method receive the
167 * <options> string in the 'options' parameter; all other forms set
168 * the parameter to NULL.
170 * Returns 0 if an attempt to register the earlycon was made,
171 * otherwise negative error code
173 int __init setup_earlycon(char *buf)
175 const struct earlycon_id *match;
180 if (early_con.flags & CON_ENABLED)
183 for (match = __earlycon_table; match < __earlycon_table_end; match++) {
184 size_t len = strlen(match->name);
186 if (strncmp(buf, match->name, len))
196 return register_earlycon(buf, match);
202 /* early_param wrapper for setup_earlycon() */
203 static int __init param_setup_earlycon(char *buf)
208 * Just 'earlycon' is a valid param for devicetree earlycons;
209 * don't generate a warning from parse_early_params() in that case
214 err = setup_earlycon(buf);
215 if (err == -ENOENT || err == -EALREADY)
219 early_param("earlycon", param_setup_earlycon);
221 #ifdef CONFIG_OF_EARLY_FLATTREE
223 int __init of_setup_earlycon(const struct earlycon_id *match,
228 struct uart_port *port = &early_console_dev.port;
233 spin_lock_init(&port->lock);
234 port->iotype = UPIO_MEM;
235 addr = of_flat_dt_translate_address(node);
236 if (addr == OF_BAD_ADDR) {
237 pr_warn("[%s] bad address\n", match->name);
240 port->mapbase = addr;
241 port->uartclk = BASE_BAUD * 16;
242 port->membase = earlycon_map(port->mapbase, SZ_4K);
244 val = of_get_flat_dt_prop(node, "reg-offset", NULL);
246 port->mapbase += be32_to_cpu(*val);
247 val = of_get_flat_dt_prop(node, "reg-shift", NULL);
249 port->regshift = be32_to_cpu(*val);
250 big_endian = of_get_flat_dt_prop(node, "big-endian", NULL) != NULL ||
251 (IS_ENABLED(CONFIG_CPU_BIG_ENDIAN) &&
252 of_get_flat_dt_prop(node, "native-endian", NULL) != NULL);
253 val = of_get_flat_dt_prop(node, "reg-io-width", NULL);
255 switch (be32_to_cpu(*val)) {
257 port->iotype = UPIO_MEM;
260 port->iotype = UPIO_MEM16;
263 port->iotype = (big_endian) ? UPIO_MEM32BE : UPIO_MEM32;
266 pr_warn("[%s] unsupported reg-io-width\n", match->name);
272 strlcpy(early_console_dev.options, options,
273 sizeof(early_console_dev.options));
275 earlycon_init(&early_console_dev, match->name);
276 err = match->setup(&early_console_dev, options);
279 if (!early_console_dev.con->write)
283 register_console(early_console_dev.con);
287 #endif /* CONFIG_OF_EARLY_FLATTREE */