blob: b9a4625b86906d63c81dfcad9074791ebecd7b2a [file] [log] [blame]
Rob Herring9aac5882014-04-18 17:19:55 -05001/*
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 Hurley470ca0d2015-03-09 16:27:21 -040013
14#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15
Rob Herring9aac5882014-04-18 17:19:55 -050016#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 Herringb0b6abd2014-03-27 08:06:16 -050021#include <linux/sizes.h>
Peter Hurleyc90fe9c2016-01-16 15:23:44 -080022#include <linux/of.h>
Peter Hurley088da2a2016-01-16 15:23:43 -080023#include <linux/of_fdt.h>
Aleksey Makarovad1696f2016-09-27 23:54:13 +030024#include <linux/acpi.h>
Rob Herring9aac5882014-04-18 17:19:55 -050025
26#ifdef CONFIG_FIX_EARLYCON_MEM
27#include <asm/fixmap.h>
28#endif
29
30#include <asm/serial.h>
31
32static struct console early_con = {
Peter Hurleycda64e62016-01-16 15:23:40 -080033 .name = "uart", /* fixed up at earlycon registration */
Rob Herring9aac5882014-04-18 17:19:55 -050034 .flags = CON_PRINTBUFFER | CON_BOOT,
Peter Hurleycda64e62016-01-16 15:23:40 -080035 .index = 0,
Rob Herring9aac5882014-04-18 17:19:55 -050036};
37
38static struct earlycon_device early_console_dev = {
39 .con = &early_con,
40};
41
Alexander Sverdlin46e36682016-09-02 13:20:21 +020042static void __iomem * __init earlycon_map(resource_size_t paddr, size_t size)
Rob Herring9aac5882014-04-18 17:19:55 -050043{
44 void __iomem *base;
45#ifdef CONFIG_FIX_EARLYCON_MEM
46 set_fixmap_io(FIX_EARLYCON_MEM_BASE, paddr & PAGE_MASK);
47 base = (void __iomem *)__fix_to_virt(FIX_EARLYCON_MEM_BASE);
48 base += paddr & ~PAGE_MASK;
49#else
50 base = ioremap(paddr, size);
51#endif
52 if (!base)
Alexander Sverdlin46e36682016-09-02 13:20:21 +020053 pr_err("%s: Couldn't map %pa\n", __func__, &paddr);
Rob Herring9aac5882014-04-18 17:19:55 -050054
55 return base;
56}
57
Peter Hurleycda64e62016-01-16 15:23:40 -080058static void __init earlycon_init(struct earlycon_device *device,
59 const char *name)
60{
61 struct console *earlycon = device->con;
Peter Hurley8e773732016-01-16 15:23:45 -080062 struct uart_port *port = &device->port;
Peter Hurleycda64e62016-01-16 15:23:40 -080063 const char *s;
64 size_t len;
65
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';
69 s--)
70 ;
71 if (*s)
72 earlycon->index = simple_strtoul(s, NULL, 10);
73 len = s - name;
74 strlcpy(earlycon->name, name, min(len + 1, sizeof(earlycon->name)));
75 earlycon->data = &early_console_dev;
Peter Hurley8e773732016-01-16 15:23:45 -080076
77 if (port->iotype == UPIO_MEM || port->iotype == UPIO_MEM16 ||
78 port->iotype == UPIO_MEM32 || port->iotype == UPIO_MEM32BE)
Peter Hurleyb9693982016-01-16 15:23:46 -080079 pr_info("%s%d at MMIO%s %pa (options '%s')\n",
80 earlycon->name, earlycon->index,
Peter Hurley8e773732016-01-16 15:23:45 -080081 (port->iotype == UPIO_MEM) ? "" :
82 (port->iotype == UPIO_MEM16) ? "16" :
83 (port->iotype == UPIO_MEM32) ? "32" : "32be",
Peter Hurleyb9693982016-01-16 15:23:46 -080084 &port->mapbase, device->options);
Peter Hurley8e773732016-01-16 15:23:45 -080085 else
Peter Hurleyb9693982016-01-16 15:23:46 -080086 pr_info("%s%d at I/O port 0x%lx (options '%s')\n",
87 earlycon->name, earlycon->index,
88 port->iobase, device->options);
Peter Hurleycda64e62016-01-16 15:23:40 -080089}
90
Peter Hurley73abaf82015-03-01 11:05:46 -050091static int __init parse_options(struct earlycon_device *device, char *options)
Rob Herring9aac5882014-04-18 17:19:55 -050092{
93 struct uart_port *port = &device->port;
Peter Hurley73abaf82015-03-01 11:05:46 -050094 int length;
Alexander Sverdlin46e36682016-09-02 13:20:21 +020095 resource_size_t addr;
Rob Herring9aac5882014-04-18 17:19:55 -050096
Peter Hurley73abaf82015-03-01 11:05:46 -050097 if (uart_parse_earlycon(options, &port->iotype, &addr, &options))
98 return -EINVAL;
Rob Herring9aac5882014-04-18 17:19:55 -050099
Peter Hurley73abaf82015-03-01 11:05:46 -0500100 switch (port->iotype) {
Masahiro Yamadabd94c402015-10-28 12:46:05 +0900101 case UPIO_MEM:
102 port->mapbase = addr;
103 break;
104 case UPIO_MEM16:
105 port->regshift = 1;
106 port->mapbase = addr;
107 break;
Peter Hurley73abaf82015-03-01 11:05:46 -0500108 case UPIO_MEM32:
Noam Camus6e63be32015-05-25 06:54:28 +0300109 case UPIO_MEM32BE:
Masahiro Yamadabd94c402015-10-28 12:46:05 +0900110 port->regshift = 2;
Rob Herring9aac5882014-04-18 17:19:55 -0500111 port->mapbase = addr;
Peter Hurley73abaf82015-03-01 11:05:46 -0500112 break;
113 case UPIO_PORT:
Rob Herring9aac5882014-04-18 17:19:55 -0500114 port->iobase = addr;
Peter Hurley73abaf82015-03-01 11:05:46 -0500115 break;
116 default:
Rob Herring9aac5882014-04-18 17:19:55 -0500117 return -EINVAL;
118 }
119
Rob Herring9aac5882014-04-18 17:19:55 -0500120 if (options) {
Rob Herringe26f1db2014-04-30 19:48:29 -0500121 device->baud = simple_strtoul(options, NULL, 0);
Rob Herring9aac5882014-04-18 17:19:55 -0500122 length = min(strcspn(options, " ") + 1,
123 (size_t)(sizeof(device->options)));
124 strlcpy(device->options, options, length);
125 }
126
Rob Herring9aac5882014-04-18 17:19:55 -0500127 return 0;
128}
129
Peter Hurley470ca0d2015-03-09 16:27:21 -0400130static int __init register_earlycon(char *buf, const struct earlycon_id *match)
Rob Herring9aac5882014-04-18 17:19:55 -0500131{
132 int err;
Rob Herring9aac5882014-04-18 17:19:55 -0500133 struct uart_port *port = &early_console_dev.port;
134
Rob Herring9aac5882014-04-18 17:19:55 -0500135 /* On parsing error, pass the options buf to the setup function */
Peter Hurley60846882015-03-09 16:27:19 -0400136 if (buf && !parse_options(&early_console_dev, buf))
Rob Herring9aac5882014-04-18 17:19:55 -0500137 buf = NULL;
138
Geert Uytterhoevene1dd3be2015-11-27 11:13:48 +0100139 spin_lock_init(&port->lock);
Peter Hurleyfeed5ba2015-03-09 16:27:15 -0400140 port->uartclk = BASE_BAUD * 16;
Rob Herring9aac5882014-04-18 17:19:55 -0500141 if (port->mapbase)
142 port->membase = earlycon_map(port->mapbase, 64);
143
Peter Hurleycda64e62016-01-16 15:23:40 -0800144 earlycon_init(&early_console_dev, match->name);
Peter Hurley470ca0d2015-03-09 16:27:21 -0400145 err = match->setup(&early_console_dev, buf);
Rob Herring9aac5882014-04-18 17:19:55 -0500146 if (err < 0)
147 return err;
148 if (!early_console_dev.con->write)
149 return -ENODEV;
150
151 register_console(early_console_dev.con);
152 return 0;
153}
Rob Herringb0b6abd2014-03-27 08:06:16 -0500154
Peter Hurley470ca0d2015-03-09 16:27:21 -0400155/**
156 * setup_earlycon - match and register earlycon console
157 * @buf: earlycon param string
158 *
159 * Registers the earlycon console matching the earlycon specified
160 * in the param string @buf. Acceptable param strings are of the form
Noam Camus6e63be32015-05-25 06:54:28 +0300161 * <name>,io|mmio|mmio32|mmio32be,<addr>,<options>
Peter Hurley470ca0d2015-03-09 16:27:21 -0400162 * <name>,0x<addr>,<options>
163 * <name>,<options>
164 * <name>
165 *
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.
169 *
170 * Returns 0 if an attempt to register the earlycon was made,
171 * otherwise negative error code
172 */
173int __init setup_earlycon(char *buf)
Peter Hurley7c53cb32015-03-09 16:27:20 -0400174{
Daniel Kurtza72ac672018-04-06 17:21:53 -0600175 const struct earlycon_id **p_match;
Peter Hurley7c53cb32015-03-09 16:27:20 -0400176
Peter Hurley470ca0d2015-03-09 16:27:21 -0400177 if (!buf || !buf[0])
178 return -EINVAL;
Peter Hurley7c53cb32015-03-09 16:27:20 -0400179
Peter Hurley470ca0d2015-03-09 16:27:21 -0400180 if (early_con.flags & CON_ENABLED)
181 return -EALREADY;
Peter Hurley7c53cb32015-03-09 16:27:20 -0400182
Daniel Kurtza72ac672018-04-06 17:21:53 -0600183 for (p_match = __earlycon_table; p_match < __earlycon_table_end;
184 p_match++) {
185 const struct earlycon_id *match = *p_match;
Peter Hurley470ca0d2015-03-09 16:27:21 -0400186 size_t len = strlen(match->name);
Peter Hurley7c53cb32015-03-09 16:27:20 -0400187
Peter Hurley470ca0d2015-03-09 16:27:21 -0400188 if (strncmp(buf, match->name, len))
189 continue;
190
191 if (buf[len]) {
192 if (buf[len] != ',')
193 continue;
194 buf += len + 1;
195 } else
196 buf = NULL;
197
198 return register_earlycon(buf, match);
199 }
200
201 return -ENOENT;
Peter Hurley7c53cb32015-03-09 16:27:20 -0400202}
203
Aleksey Makarovad1696f2016-09-27 23:54:13 +0300204/*
205 * When CONFIG_ACPI_SPCR_TABLE is defined, "earlycon" without parameters in
206 * command line does not start DT earlycon immediately, instead it defers
207 * starting it until DT/ACPI decision is made. At that time if ACPI is enabled
208 * call parse_spcr(), else call early_init_dt_scan_chosen_stdout()
209 */
210bool earlycon_init_is_deferred __initdata;
211
Peter Hurley470ca0d2015-03-09 16:27:21 -0400212/* early_param wrapper for setup_earlycon() */
213static int __init param_setup_earlycon(char *buf)
214{
215 int err;
216
217 /*
218 * Just 'earlycon' is a valid param for devicetree earlycons;
219 * don't generate a warning from parse_early_params() in that case
220 */
Aleksey Makarovad1696f2016-09-27 23:54:13 +0300221 if (!buf || !buf[0]) {
222 if (IS_ENABLED(CONFIG_ACPI_SPCR_TABLE)) {
223 earlycon_init_is_deferred = true;
224 return 0;
225 } else {
226 return early_init_dt_scan_chosen_stdout();
227 }
228 }
Peter Hurley470ca0d2015-03-09 16:27:21 -0400229
230 err = setup_earlycon(buf);
Peter Hurley66c53aa2015-05-07 14:19:21 -0400231 if (err == -ENOENT || err == -EALREADY)
232 return 0;
Peter Hurley470ca0d2015-03-09 16:27:21 -0400233 return err;
234}
235early_param("earlycon", param_setup_earlycon);
236
Peter Hurley84776142016-01-16 15:23:38 -0800237#ifdef CONFIG_OF_EARLY_FLATTREE
238
Peter Hurleyc90fe9c2016-01-16 15:23:44 -0800239int __init of_setup_earlycon(const struct earlycon_id *match,
Peter Hurley088da2a2016-01-16 15:23:43 -0800240 unsigned long node,
Peter Hurley4d118c92016-01-16 15:23:42 -0800241 const char *options)
Rob Herringb0b6abd2014-03-27 08:06:16 -0500242{
243 int err;
244 struct uart_port *port = &early_console_dev.port;
Peter Hurley088da2a2016-01-16 15:23:43 -0800245 const __be32 *val;
246 bool big_endian;
Peter Hurleyc90fe9c2016-01-16 15:23:44 -0800247 u64 addr;
Rob Herringb0b6abd2014-03-27 08:06:16 -0500248
Geert Uytterhoevene1dd3be2015-11-27 11:13:48 +0100249 spin_lock_init(&port->lock);
Rob Herringb0b6abd2014-03-27 08:06:16 -0500250 port->iotype = UPIO_MEM;
Peter Hurleyc90fe9c2016-01-16 15:23:44 -0800251 addr = of_flat_dt_translate_address(node);
252 if (addr == OF_BAD_ADDR) {
253 pr_warn("[%s] bad address\n", match->name);
254 return -ENXIO;
255 }
Rob Herringb0b6abd2014-03-27 08:06:16 -0500256 port->mapbase = addr;
257 port->uartclk = BASE_BAUD * 16;
Rob Herringb0b6abd2014-03-27 08:06:16 -0500258
Peter Hurley088da2a2016-01-16 15:23:43 -0800259 val = of_get_flat_dt_prop(node, "reg-offset", NULL);
260 if (val)
261 port->mapbase += be32_to_cpu(*val);
Greentime Hubd0d6732018-02-13 17:09:08 +0800262 port->membase = earlycon_map(port->mapbase, SZ_4K);
263
Peter Hurley088da2a2016-01-16 15:23:43 -0800264 val = of_get_flat_dt_prop(node, "reg-shift", NULL);
265 if (val)
266 port->regshift = be32_to_cpu(*val);
267 big_endian = of_get_flat_dt_prop(node, "big-endian", NULL) != NULL ||
268 (IS_ENABLED(CONFIG_CPU_BIG_ENDIAN) &&
269 of_get_flat_dt_prop(node, "native-endian", NULL) != NULL);
270 val = of_get_flat_dt_prop(node, "reg-io-width", NULL);
271 if (val) {
272 switch (be32_to_cpu(*val)) {
273 case 1:
274 port->iotype = UPIO_MEM;
275 break;
276 case 2:
277 port->iotype = UPIO_MEM16;
278 break;
279 case 4:
280 port->iotype = (big_endian) ? UPIO_MEM32BE : UPIO_MEM32;
281 break;
282 default:
283 pr_warn("[%s] unsupported reg-io-width\n", match->name);
284 return -EINVAL;
285 }
286 }
287
Peter Hurley4d118c92016-01-16 15:23:42 -0800288 if (options) {
289 strlcpy(early_console_dev.options, options,
290 sizeof(early_console_dev.options));
291 }
Peter Hurley05d96132016-01-16 15:23:41 -0800292 earlycon_init(&early_console_dev, match->name);
Peter Hurley4d118c92016-01-16 15:23:42 -0800293 err = match->setup(&early_console_dev, options);
Rob Herringb0b6abd2014-03-27 08:06:16 -0500294 if (err < 0)
295 return err;
296 if (!early_console_dev.con->write)
297 return -ENODEV;
298
299
300 register_console(early_console_dev.con);
301 return 0;
302}
Peter Hurley84776142016-01-16 15:23:38 -0800303
304#endif /* CONFIG_OF_EARLY_FLATTREE */