blob: 00d24fd211c05a64ea225135c1524b9ec0b744e7 [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>
9 *
10 * This program is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
13 */
Peter Hurley470ca0d2015-03-09 16:27:21 -040014
15#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16
Rob Herring9aac5882014-04-18 17:19:55 -050017#include <linux/console.h>
18#include <linux/kernel.h>
19#include <linux/init.h>
20#include <linux/io.h>
21#include <linux/serial_core.h>
Rob Herringb0b6abd2014-03-27 08:06:16 -050022#include <linux/sizes.h>
Peter Hurleyc90fe9c2016-01-16 15:23:44 -080023#include <linux/of.h>
Peter Hurley088da2a2016-01-16 15:23:43 -080024#include <linux/of_fdt.h>
Aleksey Makarovad1696f2016-09-27 23:54:13 +030025#include <linux/acpi.h>
Rob Herring9aac5882014-04-18 17:19:55 -050026
27#ifdef CONFIG_FIX_EARLYCON_MEM
28#include <asm/fixmap.h>
29#endif
30
31#include <asm/serial.h>
32
33static struct console early_con = {
Peter Hurleycda64e62016-01-16 15:23:40 -080034 .name = "uart", /* fixed up at earlycon registration */
Rob Herring9aac5882014-04-18 17:19:55 -050035 .flags = CON_PRINTBUFFER | CON_BOOT,
Peter Hurleycda64e62016-01-16 15:23:40 -080036 .index = 0,
Rob Herring9aac5882014-04-18 17:19:55 -050037};
38
39static struct earlycon_device early_console_dev = {
40 .con = &early_con,
41};
42
Alexander Sverdlin46e36682016-09-02 13:20:21 +020043static void __iomem * __init earlycon_map(resource_size_t paddr, size_t size)
Rob Herring9aac5882014-04-18 17:19:55 -050044{
45 void __iomem *base;
46#ifdef CONFIG_FIX_EARLYCON_MEM
47 set_fixmap_io(FIX_EARLYCON_MEM_BASE, paddr & PAGE_MASK);
48 base = (void __iomem *)__fix_to_virt(FIX_EARLYCON_MEM_BASE);
49 base += paddr & ~PAGE_MASK;
50#else
51 base = ioremap(paddr, size);
52#endif
53 if (!base)
Alexander Sverdlin46e36682016-09-02 13:20:21 +020054 pr_err("%s: Couldn't map %pa\n", __func__, &paddr);
Rob Herring9aac5882014-04-18 17:19:55 -050055
56 return base;
57}
58
Peter Hurleycda64e62016-01-16 15:23:40 -080059static void __init earlycon_init(struct earlycon_device *device,
60 const char *name)
61{
62 struct console *earlycon = device->con;
Peter Hurley8e773732016-01-16 15:23:45 -080063 struct uart_port *port = &device->port;
Peter Hurleycda64e62016-01-16 15:23:40 -080064 const char *s;
65 size_t len;
66
67 /* scan backwards from end of string for first non-numeral */
68 for (s = name + strlen(name);
69 s > name && s[-1] >= '0' && s[-1] <= '9';
70 s--)
71 ;
72 if (*s)
73 earlycon->index = simple_strtoul(s, NULL, 10);
74 len = s - name;
75 strlcpy(earlycon->name, name, min(len + 1, sizeof(earlycon->name)));
76 earlycon->data = &early_console_dev;
Peter Hurley8e773732016-01-16 15:23:45 -080077
78 if (port->iotype == UPIO_MEM || port->iotype == UPIO_MEM16 ||
79 port->iotype == UPIO_MEM32 || port->iotype == UPIO_MEM32BE)
Peter Hurleyb9693982016-01-16 15:23:46 -080080 pr_info("%s%d at MMIO%s %pa (options '%s')\n",
81 earlycon->name, earlycon->index,
Peter Hurley8e773732016-01-16 15:23:45 -080082 (port->iotype == UPIO_MEM) ? "" :
83 (port->iotype == UPIO_MEM16) ? "16" :
84 (port->iotype == UPIO_MEM32) ? "32" : "32be",
Peter Hurleyb9693982016-01-16 15:23:46 -080085 &port->mapbase, device->options);
Peter Hurley8e773732016-01-16 15:23:45 -080086 else
Peter Hurleyb9693982016-01-16 15:23:46 -080087 pr_info("%s%d at I/O port 0x%lx (options '%s')\n",
88 earlycon->name, earlycon->index,
89 port->iobase, device->options);
Peter Hurleycda64e62016-01-16 15:23:40 -080090}
91
Peter Hurley73abaf82015-03-01 11:05:46 -050092static int __init parse_options(struct earlycon_device *device, char *options)
Rob Herring9aac5882014-04-18 17:19:55 -050093{
94 struct uart_port *port = &device->port;
Peter Hurley73abaf82015-03-01 11:05:46 -050095 int length;
Alexander Sverdlin46e36682016-09-02 13:20:21 +020096 resource_size_t addr;
Rob Herring9aac5882014-04-18 17:19:55 -050097
Peter Hurley73abaf82015-03-01 11:05:46 -050098 if (uart_parse_earlycon(options, &port->iotype, &addr, &options))
99 return -EINVAL;
Rob Herring9aac5882014-04-18 17:19:55 -0500100
Peter Hurley73abaf82015-03-01 11:05:46 -0500101 switch (port->iotype) {
Masahiro Yamadabd94c402015-10-28 12:46:05 +0900102 case UPIO_MEM:
103 port->mapbase = addr;
104 break;
105 case UPIO_MEM16:
106 port->regshift = 1;
107 port->mapbase = addr;
108 break;
Peter Hurley73abaf82015-03-01 11:05:46 -0500109 case UPIO_MEM32:
Noam Camus6e63be32015-05-25 06:54:28 +0300110 case UPIO_MEM32BE:
Masahiro Yamadabd94c402015-10-28 12:46:05 +0900111 port->regshift = 2;
Rob Herring9aac5882014-04-18 17:19:55 -0500112 port->mapbase = addr;
Peter Hurley73abaf82015-03-01 11:05:46 -0500113 break;
114 case UPIO_PORT:
Rob Herring9aac5882014-04-18 17:19:55 -0500115 port->iobase = addr;
Peter Hurley73abaf82015-03-01 11:05:46 -0500116 break;
117 default:
Rob Herring9aac5882014-04-18 17:19:55 -0500118 return -EINVAL;
119 }
120
Rob Herring9aac5882014-04-18 17:19:55 -0500121 if (options) {
Rob Herringe26f1db2014-04-30 19:48:29 -0500122 device->baud = simple_strtoul(options, NULL, 0);
Rob Herring9aac5882014-04-18 17:19:55 -0500123 length = min(strcspn(options, " ") + 1,
124 (size_t)(sizeof(device->options)));
125 strlcpy(device->options, options, length);
126 }
127
Rob Herring9aac5882014-04-18 17:19:55 -0500128 return 0;
129}
130
Peter Hurley470ca0d2015-03-09 16:27:21 -0400131static int __init register_earlycon(char *buf, const struct earlycon_id *match)
Rob Herring9aac5882014-04-18 17:19:55 -0500132{
133 int err;
Rob Herring9aac5882014-04-18 17:19:55 -0500134 struct uart_port *port = &early_console_dev.port;
135
Rob Herring9aac5882014-04-18 17:19:55 -0500136 /* On parsing error, pass the options buf to the setup function */
Peter Hurley60846882015-03-09 16:27:19 -0400137 if (buf && !parse_options(&early_console_dev, buf))
Rob Herring9aac5882014-04-18 17:19:55 -0500138 buf = NULL;
139
Geert Uytterhoevene1dd3be2015-11-27 11:13:48 +0100140 spin_lock_init(&port->lock);
Peter Hurleyfeed5ba2015-03-09 16:27:15 -0400141 port->uartclk = BASE_BAUD * 16;
Rob Herring9aac5882014-04-18 17:19:55 -0500142 if (port->mapbase)
143 port->membase = earlycon_map(port->mapbase, 64);
144
Peter Hurleycda64e62016-01-16 15:23:40 -0800145 earlycon_init(&early_console_dev, match->name);
Peter Hurley470ca0d2015-03-09 16:27:21 -0400146 err = match->setup(&early_console_dev, buf);
Rob Herring9aac5882014-04-18 17:19:55 -0500147 if (err < 0)
148 return err;
149 if (!early_console_dev.con->write)
150 return -ENODEV;
151
152 register_console(early_console_dev.con);
153 return 0;
154}
Rob Herringb0b6abd2014-03-27 08:06:16 -0500155
Peter Hurley470ca0d2015-03-09 16:27:21 -0400156/**
157 * setup_earlycon - match and register earlycon console
158 * @buf: earlycon param string
159 *
160 * Registers the earlycon console matching the earlycon specified
161 * in the param string @buf. Acceptable param strings are of the form
Noam Camus6e63be32015-05-25 06:54:28 +0300162 * <name>,io|mmio|mmio32|mmio32be,<addr>,<options>
Peter Hurley470ca0d2015-03-09 16:27:21 -0400163 * <name>,0x<addr>,<options>
164 * <name>,<options>
165 * <name>
166 *
167 * Only for the third form does the earlycon setup() method receive the
168 * <options> string in the 'options' parameter; all other forms set
169 * the parameter to NULL.
170 *
171 * Returns 0 if an attempt to register the earlycon was made,
172 * otherwise negative error code
173 */
174int __init setup_earlycon(char *buf)
Peter Hurley7c53cb32015-03-09 16:27:20 -0400175{
Peter Hurley470ca0d2015-03-09 16:27:21 -0400176 const struct earlycon_id *match;
Peter Hurley7c53cb32015-03-09 16:27:20 -0400177
Peter Hurley470ca0d2015-03-09 16:27:21 -0400178 if (!buf || !buf[0])
179 return -EINVAL;
Peter Hurley7c53cb32015-03-09 16:27:20 -0400180
Peter Hurley470ca0d2015-03-09 16:27:21 -0400181 if (early_con.flags & CON_ENABLED)
182 return -EALREADY;
Peter Hurley7c53cb32015-03-09 16:27:20 -0400183
Peter Hurley2eaa7902016-01-16 15:23:39 -0800184 for (match = __earlycon_table; match < __earlycon_table_end; match++) {
Peter Hurley470ca0d2015-03-09 16:27:21 -0400185 size_t len = strlen(match->name);
Peter Hurley7c53cb32015-03-09 16:27:20 -0400186
Peter Hurley470ca0d2015-03-09 16:27:21 -0400187 if (strncmp(buf, match->name, len))
188 continue;
189
190 if (buf[len]) {
191 if (buf[len] != ',')
192 continue;
193 buf += len + 1;
194 } else
195 buf = NULL;
196
197 return register_earlycon(buf, match);
198 }
199
200 return -ENOENT;
Peter Hurley7c53cb32015-03-09 16:27:20 -0400201}
202
Aleksey Makarovad1696f2016-09-27 23:54:13 +0300203/*
204 * When CONFIG_ACPI_SPCR_TABLE is defined, "earlycon" without parameters in
205 * command line does not start DT earlycon immediately, instead it defers
206 * starting it until DT/ACPI decision is made. At that time if ACPI is enabled
207 * call parse_spcr(), else call early_init_dt_scan_chosen_stdout()
208 */
209bool earlycon_init_is_deferred __initdata;
210
Peter Hurley470ca0d2015-03-09 16:27:21 -0400211/* early_param wrapper for setup_earlycon() */
212static int __init param_setup_earlycon(char *buf)
213{
214 int err;
215
216 /*
217 * Just 'earlycon' is a valid param for devicetree earlycons;
218 * don't generate a warning from parse_early_params() in that case
219 */
Aleksey Makarovad1696f2016-09-27 23:54:13 +0300220 if (!buf || !buf[0]) {
221 if (IS_ENABLED(CONFIG_ACPI_SPCR_TABLE)) {
222 earlycon_init_is_deferred = true;
223 return 0;
Jeffy Chen447ef992017-08-19 09:43:17 +0800224 } else if (!buf) {
Aleksey Makarovad1696f2016-09-27 23:54:13 +0300225 return early_init_dt_scan_chosen_stdout();
226 }
227 }
Peter Hurley470ca0d2015-03-09 16:27:21 -0400228
229 err = setup_earlycon(buf);
Peter Hurley66c53aa2015-05-07 14:19:21 -0400230 if (err == -ENOENT || err == -EALREADY)
231 return 0;
Peter Hurley470ca0d2015-03-09 16:27:21 -0400232 return err;
233}
234early_param("earlycon", param_setup_earlycon);
235
Peter Hurley84776142016-01-16 15:23:38 -0800236#ifdef CONFIG_OF_EARLY_FLATTREE
237
Peter Hurleyc90fe9c2016-01-16 15:23:44 -0800238int __init of_setup_earlycon(const struct earlycon_id *match,
Peter Hurley088da2a2016-01-16 15:23:43 -0800239 unsigned long node,
Peter Hurley4d118c92016-01-16 15:23:42 -0800240 const char *options)
Rob Herringb0b6abd2014-03-27 08:06:16 -0500241{
242 int err;
243 struct uart_port *port = &early_console_dev.port;
Peter Hurley088da2a2016-01-16 15:23:43 -0800244 const __be32 *val;
245 bool big_endian;
Peter Hurleyc90fe9c2016-01-16 15:23:44 -0800246 u64 addr;
Rob Herringb0b6abd2014-03-27 08:06:16 -0500247
Geert Uytterhoevene1dd3be2015-11-27 11:13:48 +0100248 spin_lock_init(&port->lock);
Rob Herringb0b6abd2014-03-27 08:06:16 -0500249 port->iotype = UPIO_MEM;
Peter Hurleyc90fe9c2016-01-16 15:23:44 -0800250 addr = of_flat_dt_translate_address(node);
251 if (addr == OF_BAD_ADDR) {
252 pr_warn("[%s] bad address\n", match->name);
253 return -ENXIO;
254 }
Rob Herringb0b6abd2014-03-27 08:06:16 -0500255 port->mapbase = addr;
256 port->uartclk = BASE_BAUD * 16;
Peter Hurleyc90fe9c2016-01-16 15:23:44 -0800257 port->membase = earlycon_map(port->mapbase, SZ_4K);
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);
262 val = of_get_flat_dt_prop(node, "reg-shift", NULL);
263 if (val)
264 port->regshift = be32_to_cpu(*val);
265 big_endian = of_get_flat_dt_prop(node, "big-endian", NULL) != NULL ||
266 (IS_ENABLED(CONFIG_CPU_BIG_ENDIAN) &&
267 of_get_flat_dt_prop(node, "native-endian", NULL) != NULL);
268 val = of_get_flat_dt_prop(node, "reg-io-width", NULL);
269 if (val) {
270 switch (be32_to_cpu(*val)) {
271 case 1:
272 port->iotype = UPIO_MEM;
273 break;
274 case 2:
275 port->iotype = UPIO_MEM16;
276 break;
277 case 4:
278 port->iotype = (big_endian) ? UPIO_MEM32BE : UPIO_MEM32;
279 break;
280 default:
281 pr_warn("[%s] unsupported reg-io-width\n", match->name);
282 return -EINVAL;
283 }
284 }
285
Eugeniy Paltsev31cb9a82017-08-21 19:22:13 +0300286 val = of_get_flat_dt_prop(node, "current-speed", NULL);
287 if (val)
288 early_console_dev.baud = be32_to_cpu(*val);
289
Peter Hurley4d118c92016-01-16 15:23:42 -0800290 if (options) {
Eugeniy Paltsev31cb9a82017-08-21 19:22:13 +0300291 early_console_dev.baud = simple_strtoul(options, NULL, 0);
Peter Hurley4d118c92016-01-16 15:23:42 -0800292 strlcpy(early_console_dev.options, options,
293 sizeof(early_console_dev.options));
294 }
Peter Hurley05d96132016-01-16 15:23:41 -0800295 earlycon_init(&early_console_dev, match->name);
Peter Hurley4d118c92016-01-16 15:23:42 -0800296 err = match->setup(&early_console_dev, options);
Rob Herringb0b6abd2014-03-27 08:06:16 -0500297 if (err < 0)
298 return err;
299 if (!early_console_dev.con->write)
300 return -ENODEV;
301
302
303 register_console(early_console_dev.con);
304 return 0;
305}
Peter Hurley84776142016-01-16 15:23:38 -0800306
307#endif /* CONFIG_OF_EARLY_FLATTREE */