b70877932d47d3a3242bc76b25b69926becd1ab0
[releases.git] / earlycon.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2014 Linaro Ltd.
4  * Author: Rob Herring <robh@kernel.org>
5  *
6  * Based on 8250 earlycon:
7  * (c) Copyright 2004 Hewlett-Packard Development Company, L.P.
8  *      Bjorn Helgaas <bjorn.helgaas@hp.com>
9  */
10
11 #define pr_fmt(fmt)     KBUILD_MODNAME ": " fmt
12
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>
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 = {
30         .name =         "uart",         /* fixed up at earlycon registration */
31         .flags =        CON_PRINTBUFFER | CON_BOOT,
32         .index =        0,
33 };
34
35 static struct earlycon_device early_console_dev = {
36         .con = &early_con,
37 };
38
39 static void __iomem * __init earlycon_map(resource_size_t 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 %pa\n", __func__, &paddr);
51
52         return base;
53 }
54
55 static void __init earlycon_init(struct earlycon_device *device,
56                                  const char *name)
57 {
58         struct console *earlycon = device->con;
59         const char *s;
60         size_t len;
61
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';
65              s--)
66                 ;
67         if (*s)
68                 earlycon->index = simple_strtoul(s, NULL, 10);
69         len = s - name;
70         strlcpy(earlycon->name, name, min(len + 1, sizeof(earlycon->name)));
71         earlycon->data = &early_console_dev;
72 }
73
74 static void __init earlycon_print_info(struct earlycon_device *device)
75 {
76         struct console *earlycon = device->con;
77         struct uart_port *port = &device->port;
78
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);
87         else
88                 pr_info("%s%d at I/O port 0x%lx (options '%s')\n",
89                         earlycon->name, earlycon->index,
90                         port->iobase, device->options);
91 }
92
93 static int __init parse_options(struct earlycon_device *device, char *options)
94 {
95         struct uart_port *port = &device->port;
96         int length;
97         resource_size_t addr;
98
99         if (uart_parse_earlycon(options, &port->iotype, &addr, &options))
100                 return -EINVAL;
101
102         switch (port->iotype) {
103         case UPIO_MEM:
104                 port->mapbase = addr;
105                 break;
106         case UPIO_MEM16:
107                 port->regshift = 1;
108                 port->mapbase = addr;
109                 break;
110         case UPIO_MEM32:
111         case UPIO_MEM32BE:
112                 port->regshift = 2;
113                 port->mapbase = addr;
114                 break;
115         case UPIO_PORT:
116                 port->iobase = addr;
117                 break;
118         default:
119                 return -EINVAL;
120         }
121
122         if (options) {
123                 device->baud = simple_strtoul(options, NULL, 0);
124                 length = min(strcspn(options, " ") + 1,
125                              (size_t)(sizeof(device->options)));
126                 strlcpy(device->options, options, length);
127         }
128
129         return 0;
130 }
131
132 static int __init register_earlycon(char *buf, const struct earlycon_id *match)
133 {
134         int err;
135         struct uart_port *port = &early_console_dev.port;
136
137         /* On parsing error, pass the options buf to the setup function */
138         if (buf && !parse_options(&early_console_dev, buf))
139                 buf = NULL;
140
141         spin_lock_init(&port->lock);
142         port->uartclk = BASE_BAUD * 16;
143         if (port->mapbase)
144                 port->membase = earlycon_map(port->mapbase, 64);
145
146         earlycon_init(&early_console_dev, match->name);
147         err = match->setup(&early_console_dev, buf);
148         earlycon_print_info(&early_console_dev);
149         if (err < 0)
150                 return err;
151         if (!early_console_dev.con->write)
152                 return -ENODEV;
153
154         register_console(early_console_dev.con);
155         return 0;
156 }
157
158 /**
159  *      setup_earlycon - match and register earlycon console
160  *      @buf:   earlycon param string
161  *
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>
166  *         <name>,<options>
167  *         <name>
168  *
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.
172  *
173  *      Returns 0 if an attempt to register the earlycon was made,
174  *      otherwise negative error code
175  */
176 int __init setup_earlycon(char *buf)
177 {
178         const struct earlycon_id **p_match;
179         bool empty_compatible = true;
180
181         if (!buf || !buf[0])
182                 return -EINVAL;
183
184         if (early_con.flags & CON_ENABLED)
185                 return -EALREADY;
186
187 again:
188         for (p_match = __earlycon_table; p_match < __earlycon_table_end;
189              p_match++) {
190                 const struct earlycon_id *match = *p_match;
191                 size_t len = strlen(match->name);
192
193                 if (strncmp(buf, match->name, len))
194                         continue;
195
196                 /* prefer entries with empty compatible */
197                 if (empty_compatible && *match->compatible)
198                         continue;
199
200                 if (buf[len]) {
201                         if (buf[len] != ',')
202                                 continue;
203                         buf += len + 1;
204                 } else
205                         buf = NULL;
206
207                 return register_earlycon(buf, match);
208         }
209
210         if (empty_compatible) {
211                 empty_compatible = false;
212                 goto again;
213         }
214
215         return -ENOENT;
216 }
217
218 /*
219  * This defers the initialization of the early console until after ACPI has
220  * been initialized.
221  */
222 bool earlycon_acpi_spcr_enable __initdata;
223
224 /* early_param wrapper for setup_earlycon() */
225 static int __init param_setup_earlycon(char *buf)
226 {
227         int err;
228
229         /* Just 'earlycon' is a valid param for devicetree and ACPI SPCR. */
230         if (!buf || !buf[0]) {
231                 if (IS_ENABLED(CONFIG_ACPI_SPCR_TABLE)) {
232                         earlycon_acpi_spcr_enable = true;
233                         return 0;
234                 } else if (!buf) {
235                         return early_init_dt_scan_chosen_stdout();
236                 }
237         }
238
239         err = setup_earlycon(buf);
240         if (err == -ENOENT || err == -EALREADY)
241                 return 0;
242         return err;
243 }
244 early_param("earlycon", param_setup_earlycon);
245
246 #ifdef CONFIG_OF_EARLY_FLATTREE
247
248 int __init of_setup_earlycon(const struct earlycon_id *match,
249                              unsigned long node,
250                              const char *options)
251 {
252         int err;
253         struct uart_port *port = &early_console_dev.port;
254         const __be32 *val;
255         bool big_endian;
256         u64 addr;
257
258         spin_lock_init(&port->lock);
259         port->iotype = UPIO_MEM;
260         addr = of_flat_dt_translate_address(node);
261         if (addr == OF_BAD_ADDR) {
262                 pr_warn("[%s] bad address\n", match->name);
263                 return -ENXIO;
264         }
265         port->mapbase = addr;
266
267         val = of_get_flat_dt_prop(node, "reg-offset", NULL);
268         if (val)
269                 port->mapbase += be32_to_cpu(*val);
270         port->membase = earlycon_map(port->mapbase, SZ_4K);
271
272         val = of_get_flat_dt_prop(node, "reg-shift", NULL);
273         if (val)
274                 port->regshift = be32_to_cpu(*val);
275         big_endian = of_get_flat_dt_prop(node, "big-endian", NULL) != NULL ||
276                 (IS_ENABLED(CONFIG_CPU_BIG_ENDIAN) &&
277                  of_get_flat_dt_prop(node, "native-endian", NULL) != NULL);
278         val = of_get_flat_dt_prop(node, "reg-io-width", NULL);
279         if (val) {
280                 switch (be32_to_cpu(*val)) {
281                 case 1:
282                         port->iotype = UPIO_MEM;
283                         break;
284                 case 2:
285                         port->iotype = UPIO_MEM16;
286                         break;
287                 case 4:
288                         port->iotype = (big_endian) ? UPIO_MEM32BE : UPIO_MEM32;
289                         break;
290                 default:
291                         pr_warn("[%s] unsupported reg-io-width\n", match->name);
292                         return -EINVAL;
293                 }
294         }
295
296         val = of_get_flat_dt_prop(node, "current-speed", NULL);
297         if (val)
298                 early_console_dev.baud = be32_to_cpu(*val);
299
300         val = of_get_flat_dt_prop(node, "clock-frequency", NULL);
301         if (val)
302                 port->uartclk = be32_to_cpu(*val);
303
304         if (options) {
305                 early_console_dev.baud = simple_strtoul(options, NULL, 0);
306                 strlcpy(early_console_dev.options, options,
307                         sizeof(early_console_dev.options));
308         }
309         earlycon_init(&early_console_dev, match->name);
310         err = match->setup(&early_console_dev, options);
311         earlycon_print_info(&early_console_dev);
312         if (err < 0)
313                 return err;
314         if (!early_console_dev.con->write)
315                 return -ENODEV;
316
317
318         register_console(early_console_dev.con);
319         return 0;
320 }
321
322 #endif /* CONFIG_OF_EARLY_FLATTREE */