blob: a24278380fec2a6a67b2513514f08ecd6688dfe2 [file] [log] [blame]
Greg Kroah-Hartmane3b3d0f2017-11-06 18:11:51 +01001// SPDX-License-Identifier: GPL-2.0
Rob Herring9aac5882014-04-18 17:19:55 -05002/*
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>
Rob Herring9aac5882014-04-18 17:19:55 -05009 */
Peter Hurley470ca0d2015-03-09 16:27:21 -040010
11#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12
Rob Herring9aac5882014-04-18 17:19:55 -050013#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>
Rob Herringb0b6abd2014-03-27 08:06:16 -050018#include <linux/sizes.h>
Peter Hurleyc90fe9c2016-01-16 15:23:44 -080019#include <linux/of.h>
Peter Hurley088da2a2016-01-16 15:23:43 -080020#include <linux/of_fdt.h>
Aleksey Makarovad1696f2016-09-27 23:54:13 +030021#include <linux/acpi.h>
Rob Herring9aac5882014-04-18 17:19:55 -050022
23#ifdef CONFIG_FIX_EARLYCON_MEM
24#include <asm/fixmap.h>
25#endif
26
27#include <asm/serial.h>
28
29static struct console early_con = {
Peter Hurleycda64e62016-01-16 15:23:40 -080030 .name = "uart", /* fixed up at earlycon registration */
Rob Herring9aac5882014-04-18 17:19:55 -050031 .flags = CON_PRINTBUFFER | CON_BOOT,
Peter Hurleycda64e62016-01-16 15:23:40 -080032 .index = 0,
Rob Herring9aac5882014-04-18 17:19:55 -050033};
34
35static struct earlycon_device early_console_dev = {
36 .con = &early_con,
37};
38
Alexander Sverdlin46e36682016-09-02 13:20:21 +020039static void __iomem * __init earlycon_map(resource_size_t paddr, size_t size)
Rob Herring9aac5882014-04-18 17:19:55 -050040{
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)
Alexander Sverdlin46e36682016-09-02 13:20:21 +020050 pr_err("%s: Couldn't map %pa\n", __func__, &paddr);
Rob Herring9aac5882014-04-18 17:19:55 -050051
52 return base;
53}
54
Peter Hurleycda64e62016-01-16 15:23:40 -080055static void __init earlycon_init(struct earlycon_device *device,
56 const char *name)
57{
58 struct console *earlycon = device->con;
Peter Hurley8e773732016-01-16 15:23:45 -080059 struct uart_port *port = &device->port;
Peter Hurleycda64e62016-01-16 15:23:40 -080060 const char *s;
61 size_t len;
62
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--)
67 ;
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;
Peter Hurley8e773732016-01-16 15:23:45 -080073
74 if (port->iotype == UPIO_MEM || port->iotype == UPIO_MEM16 ||
75 port->iotype == UPIO_MEM32 || port->iotype == UPIO_MEM32BE)
Peter Hurleyb9693982016-01-16 15:23:46 -080076 pr_info("%s%d at MMIO%s %pa (options '%s')\n",
77 earlycon->name, earlycon->index,
Peter Hurley8e773732016-01-16 15:23:45 -080078 (port->iotype == UPIO_MEM) ? "" :
79 (port->iotype == UPIO_MEM16) ? "16" :
80 (port->iotype == UPIO_MEM32) ? "32" : "32be",
Peter Hurleyb9693982016-01-16 15:23:46 -080081 &port->mapbase, device->options);
Peter Hurley8e773732016-01-16 15:23:45 -080082 else
Peter Hurleyb9693982016-01-16 15:23:46 -080083 pr_info("%s%d at I/O port 0x%lx (options '%s')\n",
84 earlycon->name, earlycon->index,
85 port->iobase, device->options);
Peter Hurleycda64e62016-01-16 15:23:40 -080086}
87
Peter Hurley73abaf82015-03-01 11:05:46 -050088static int __init parse_options(struct earlycon_device *device, char *options)
Rob Herring9aac5882014-04-18 17:19:55 -050089{
90 struct uart_port *port = &device->port;
Peter Hurley73abaf82015-03-01 11:05:46 -050091 int length;
Alexander Sverdlin46e36682016-09-02 13:20:21 +020092 resource_size_t addr;
Rob Herring9aac5882014-04-18 17:19:55 -050093
Peter Hurley73abaf82015-03-01 11:05:46 -050094 if (uart_parse_earlycon(options, &port->iotype, &addr, &options))
95 return -EINVAL;
Rob Herring9aac5882014-04-18 17:19:55 -050096
Peter Hurley73abaf82015-03-01 11:05:46 -050097 switch (port->iotype) {
Masahiro Yamadabd94c402015-10-28 12:46:05 +090098 case UPIO_MEM:
99 port->mapbase = addr;
100 break;
101 case UPIO_MEM16:
102 port->regshift = 1;
103 port->mapbase = addr;
104 break;
Peter Hurley73abaf82015-03-01 11:05:46 -0500105 case UPIO_MEM32:
Noam Camus6e63be32015-05-25 06:54:28 +0300106 case UPIO_MEM32BE:
Masahiro Yamadabd94c402015-10-28 12:46:05 +0900107 port->regshift = 2;
Rob Herring9aac5882014-04-18 17:19:55 -0500108 port->mapbase = addr;
Peter Hurley73abaf82015-03-01 11:05:46 -0500109 break;
110 case UPIO_PORT:
Rob Herring9aac5882014-04-18 17:19:55 -0500111 port->iobase = addr;
Peter Hurley73abaf82015-03-01 11:05:46 -0500112 break;
113 default:
Rob Herring9aac5882014-04-18 17:19:55 -0500114 return -EINVAL;
115 }
116
Rob Herring9aac5882014-04-18 17:19:55 -0500117 if (options) {
Rob Herringe26f1db2014-04-30 19:48:29 -0500118 device->baud = simple_strtoul(options, NULL, 0);
Rob Herring9aac5882014-04-18 17:19:55 -0500119 length = min(strcspn(options, " ") + 1,
120 (size_t)(sizeof(device->options)));
121 strlcpy(device->options, options, length);
122 }
123
Rob Herring9aac5882014-04-18 17:19:55 -0500124 return 0;
125}
126
Peter Hurley470ca0d2015-03-09 16:27:21 -0400127static int __init register_earlycon(char *buf, const struct earlycon_id *match)
Rob Herring9aac5882014-04-18 17:19:55 -0500128{
129 int err;
Rob Herring9aac5882014-04-18 17:19:55 -0500130 struct uart_port *port = &early_console_dev.port;
131
Rob Herring9aac5882014-04-18 17:19:55 -0500132 /* On parsing error, pass the options buf to the setup function */
Peter Hurley60846882015-03-09 16:27:19 -0400133 if (buf && !parse_options(&early_console_dev, buf))
Rob Herring9aac5882014-04-18 17:19:55 -0500134 buf = NULL;
135
Geert Uytterhoevene1dd3be2015-11-27 11:13:48 +0100136 spin_lock_init(&port->lock);
Peter Hurleyfeed5ba2015-03-09 16:27:15 -0400137 port->uartclk = BASE_BAUD * 16;
Rob Herring9aac5882014-04-18 17:19:55 -0500138 if (port->mapbase)
139 port->membase = earlycon_map(port->mapbase, 64);
140
Peter Hurleycda64e62016-01-16 15:23:40 -0800141 earlycon_init(&early_console_dev, match->name);
Peter Hurley470ca0d2015-03-09 16:27:21 -0400142 err = match->setup(&early_console_dev, buf);
Rob Herring9aac5882014-04-18 17:19:55 -0500143 if (err < 0)
144 return err;
145 if (!early_console_dev.con->write)
146 return -ENODEV;
147
148 register_console(early_console_dev.con);
149 return 0;
150}
Rob Herringb0b6abd2014-03-27 08:06:16 -0500151
Peter Hurley470ca0d2015-03-09 16:27:21 -0400152/**
153 * setup_earlycon - match and register earlycon console
154 * @buf: earlycon param string
155 *
156 * Registers the earlycon console matching the earlycon specified
157 * in the param string @buf. Acceptable param strings are of the form
Noam Camus6e63be32015-05-25 06:54:28 +0300158 * <name>,io|mmio|mmio32|mmio32be,<addr>,<options>
Peter Hurley470ca0d2015-03-09 16:27:21 -0400159 * <name>,0x<addr>,<options>
160 * <name>,<options>
161 * <name>
162 *
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.
166 *
167 * Returns 0 if an attempt to register the earlycon was made,
168 * otherwise negative error code
169 */
170int __init setup_earlycon(char *buf)
Peter Hurley7c53cb32015-03-09 16:27:20 -0400171{
Peter Hurley470ca0d2015-03-09 16:27:21 -0400172 const struct earlycon_id *match;
Peter Hurley7c53cb32015-03-09 16:27:20 -0400173
Peter Hurley470ca0d2015-03-09 16:27:21 -0400174 if (!buf || !buf[0])
175 return -EINVAL;
Peter Hurley7c53cb32015-03-09 16:27:20 -0400176
Peter Hurley470ca0d2015-03-09 16:27:21 -0400177 if (early_con.flags & CON_ENABLED)
178 return -EALREADY;
Peter Hurley7c53cb32015-03-09 16:27:20 -0400179
Peter Hurley2eaa7902016-01-16 15:23:39 -0800180 for (match = __earlycon_table; match < __earlycon_table_end; match++) {
Peter Hurley470ca0d2015-03-09 16:27:21 -0400181 size_t len = strlen(match->name);
Peter Hurley7c53cb32015-03-09 16:27:20 -0400182
Peter Hurley470ca0d2015-03-09 16:27:21 -0400183 if (strncmp(buf, match->name, len))
184 continue;
185
186 if (buf[len]) {
187 if (buf[len] != ',')
188 continue;
189 buf += len + 1;
190 } else
191 buf = NULL;
192
193 return register_earlycon(buf, match);
194 }
195
196 return -ENOENT;
Peter Hurley7c53cb32015-03-09 16:27:20 -0400197}
198
Aleksey Makarovad1696f2016-09-27 23:54:13 +0300199/*
Prarit Bhargava0231d002018-01-18 10:09:51 -0500200 * This defers the initialization of the early console until after ACPI has
201 * been initialized.
Aleksey Makarovad1696f2016-09-27 23:54:13 +0300202 */
Prarit Bhargava0231d002018-01-18 10:09:51 -0500203bool earlycon_acpi_spcr_enable __initdata;
Aleksey Makarovad1696f2016-09-27 23:54:13 +0300204
Peter Hurley470ca0d2015-03-09 16:27:21 -0400205/* early_param wrapper for setup_earlycon() */
206static int __init param_setup_earlycon(char *buf)
207{
208 int err;
209
Prarit Bhargava0231d002018-01-18 10:09:51 -0500210 /* Just 'earlycon' is a valid param for devicetree and ACPI SPCR. */
Aleksey Makarovad1696f2016-09-27 23:54:13 +0300211 if (!buf || !buf[0]) {
212 if (IS_ENABLED(CONFIG_ACPI_SPCR_TABLE)) {
Prarit Bhargava0231d002018-01-18 10:09:51 -0500213 earlycon_acpi_spcr_enable = true;
Aleksey Makarovad1696f2016-09-27 23:54:13 +0300214 return 0;
Jeffy Chen447ef992017-08-19 09:43:17 +0800215 } else if (!buf) {
Aleksey Makarovad1696f2016-09-27 23:54:13 +0300216 return early_init_dt_scan_chosen_stdout();
217 }
218 }
Peter Hurley470ca0d2015-03-09 16:27:21 -0400219
220 err = setup_earlycon(buf);
Peter Hurley66c53aa2015-05-07 14:19:21 -0400221 if (err == -ENOENT || err == -EALREADY)
222 return 0;
Peter Hurley470ca0d2015-03-09 16:27:21 -0400223 return err;
224}
225early_param("earlycon", param_setup_earlycon);
226
Peter Hurley84776142016-01-16 15:23:38 -0800227#ifdef CONFIG_OF_EARLY_FLATTREE
228
Peter Hurleyc90fe9c2016-01-16 15:23:44 -0800229int __init of_setup_earlycon(const struct earlycon_id *match,
Peter Hurley088da2a2016-01-16 15:23:43 -0800230 unsigned long node,
Peter Hurley4d118c92016-01-16 15:23:42 -0800231 const char *options)
Rob Herringb0b6abd2014-03-27 08:06:16 -0500232{
233 int err;
234 struct uart_port *port = &early_console_dev.port;
Peter Hurley088da2a2016-01-16 15:23:43 -0800235 const __be32 *val;
236 bool big_endian;
Peter Hurleyc90fe9c2016-01-16 15:23:44 -0800237 u64 addr;
Rob Herringb0b6abd2014-03-27 08:06:16 -0500238
Geert Uytterhoevene1dd3be2015-11-27 11:13:48 +0100239 spin_lock_init(&port->lock);
Rob Herringb0b6abd2014-03-27 08:06:16 -0500240 port->iotype = UPIO_MEM;
Peter Hurleyc90fe9c2016-01-16 15:23:44 -0800241 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;
245 }
Rob Herringb0b6abd2014-03-27 08:06:16 -0500246 port->mapbase = addr;
247 port->uartclk = BASE_BAUD * 16;
Rob Herringb0b6abd2014-03-27 08:06:16 -0500248
Peter Hurley088da2a2016-01-16 15:23:43 -0800249 val = of_get_flat_dt_prop(node, "reg-offset", NULL);
250 if (val)
251 port->mapbase += be32_to_cpu(*val);
Greentime Hu1f66dd32018-02-13 17:09:08 +0800252 port->membase = earlycon_map(port->mapbase, SZ_4K);
253
Peter Hurley088da2a2016-01-16 15:23:43 -0800254 val = of_get_flat_dt_prop(node, "reg-shift", NULL);
255 if (val)
256 port->regshift = be32_to_cpu(*val);
257 big_endian = of_get_flat_dt_prop(node, "big-endian", NULL) != NULL ||
258 (IS_ENABLED(CONFIG_CPU_BIG_ENDIAN) &&
259 of_get_flat_dt_prop(node, "native-endian", NULL) != NULL);
260 val = of_get_flat_dt_prop(node, "reg-io-width", NULL);
261 if (val) {
262 switch (be32_to_cpu(*val)) {
263 case 1:
264 port->iotype = UPIO_MEM;
265 break;
266 case 2:
267 port->iotype = UPIO_MEM16;
268 break;
269 case 4:
270 port->iotype = (big_endian) ? UPIO_MEM32BE : UPIO_MEM32;
271 break;
272 default:
273 pr_warn("[%s] unsupported reg-io-width\n", match->name);
274 return -EINVAL;
275 }
276 }
277
Eugeniy Paltsev31cb9a82017-08-21 19:22:13 +0300278 val = of_get_flat_dt_prop(node, "current-speed", NULL);
279 if (val)
280 early_console_dev.baud = be32_to_cpu(*val);
281
Peter Hurley4d118c92016-01-16 15:23:42 -0800282 if (options) {
Eugeniy Paltsev31cb9a82017-08-21 19:22:13 +0300283 early_console_dev.baud = simple_strtoul(options, NULL, 0);
Peter Hurley4d118c92016-01-16 15:23:42 -0800284 strlcpy(early_console_dev.options, options,
285 sizeof(early_console_dev.options));
286 }
Peter Hurley05d96132016-01-16 15:23:41 -0800287 earlycon_init(&early_console_dev, match->name);
Peter Hurley4d118c92016-01-16 15:23:42 -0800288 err = match->setup(&early_console_dev, options);
Rob Herringb0b6abd2014-03-27 08:06:16 -0500289 if (err < 0)
290 return err;
291 if (!early_console_dev.con->write)
292 return -ENODEV;
293
294
295 register_console(early_console_dev.con);
296 return 0;
297}
Peter Hurley84776142016-01-16 15:23:38 -0800298
299#endif /* CONFIG_OF_EARLY_FLATTREE */