blob: baee9ad59af7ddd5ec881a7d1f52cbfc07c78b4b [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>
Rob Herring9aac5882014-04-18 17:19:55 -050024
25#ifdef CONFIG_FIX_EARLYCON_MEM
26#include <asm/fixmap.h>
27#endif
28
29#include <asm/serial.h>
30
31static struct console early_con = {
Peter Hurleycda64e62016-01-16 15:23:40 -080032 .name = "uart", /* fixed up at earlycon registration */
Rob Herring9aac5882014-04-18 17:19:55 -050033 .flags = CON_PRINTBUFFER | CON_BOOT,
Peter Hurleycda64e62016-01-16 15:23:40 -080034 .index = 0,
Rob Herring9aac5882014-04-18 17:19:55 -050035};
36
37static struct earlycon_device early_console_dev = {
38 .con = &early_con,
39};
40
41static void __iomem * __init earlycon_map(unsigned long paddr, size_t size)
42{
43 void __iomem *base;
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;
48#else
49 base = ioremap(paddr, size);
50#endif
51 if (!base)
52 pr_err("%s: Couldn't map 0x%llx\n", __func__,
53 (unsigned long long)paddr);
54
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;
62 const char *s;
63 size_t len;
64
65 /* scan backwards from end of string for first non-numeral */
66 for (s = name + strlen(name);
67 s > name && s[-1] >= '0' && s[-1] <= '9';
68 s--)
69 ;
70 if (*s)
71 earlycon->index = simple_strtoul(s, NULL, 10);
72 len = s - name;
73 strlcpy(earlycon->name, name, min(len + 1, sizeof(earlycon->name)));
74 earlycon->data = &early_console_dev;
75}
76
Peter Hurley73abaf82015-03-01 11:05:46 -050077static int __init parse_options(struct earlycon_device *device, char *options)
Rob Herring9aac5882014-04-18 17:19:55 -050078{
79 struct uart_port *port = &device->port;
Peter Hurley73abaf82015-03-01 11:05:46 -050080 int length;
Rob Herring9aac5882014-04-18 17:19:55 -050081 unsigned long addr;
82
Peter Hurley73abaf82015-03-01 11:05:46 -050083 if (uart_parse_earlycon(options, &port->iotype, &addr, &options))
84 return -EINVAL;
Rob Herring9aac5882014-04-18 17:19:55 -050085
Peter Hurley73abaf82015-03-01 11:05:46 -050086 switch (port->iotype) {
Masahiro Yamadabd94c402015-10-28 12:46:05 +090087 case UPIO_MEM:
88 port->mapbase = addr;
89 break;
90 case UPIO_MEM16:
91 port->regshift = 1;
92 port->mapbase = addr;
93 break;
Peter Hurley73abaf82015-03-01 11:05:46 -050094 case UPIO_MEM32:
Noam Camus6e63be32015-05-25 06:54:28 +030095 case UPIO_MEM32BE:
Masahiro Yamadabd94c402015-10-28 12:46:05 +090096 port->regshift = 2;
Rob Herring9aac5882014-04-18 17:19:55 -050097 port->mapbase = addr;
Peter Hurley73abaf82015-03-01 11:05:46 -050098 break;
99 case UPIO_PORT:
Rob Herring9aac5882014-04-18 17:19:55 -0500100 port->iobase = addr;
Peter Hurley73abaf82015-03-01 11:05:46 -0500101 break;
102 default:
Rob Herring9aac5882014-04-18 17:19:55 -0500103 return -EINVAL;
104 }
105
Rob Herring9aac5882014-04-18 17:19:55 -0500106 if (options) {
Rob Herringe26f1db2014-04-30 19:48:29 -0500107 device->baud = simple_strtoul(options, NULL, 0);
Rob Herring9aac5882014-04-18 17:19:55 -0500108 length = min(strcspn(options, " ") + 1,
109 (size_t)(sizeof(device->options)));
110 strlcpy(device->options, options, length);
111 }
112
Masahiro Yamadabd94c402015-10-28 12:46:05 +0900113 if (port->iotype == UPIO_MEM || port->iotype == UPIO_MEM16 ||
114 port->iotype == UPIO_MEM32 || port->iotype == UPIO_MEM32BE)
Rob Herring9aac5882014-04-18 17:19:55 -0500115 pr_info("Early serial console at MMIO%s 0x%llx (options '%s')\n",
Noam Camus6e63be32015-05-25 06:54:28 +0300116 (port->iotype == UPIO_MEM) ? "" :
Masahiro Yamadabd94c402015-10-28 12:46:05 +0900117 (port->iotype == UPIO_MEM16) ? "16" :
Noam Camus6e63be32015-05-25 06:54:28 +0300118 (port->iotype == UPIO_MEM32) ? "32" : "32be",
Rob Herring9aac5882014-04-18 17:19:55 -0500119 (unsigned long long)port->mapbase,
120 device->options);
121 else
122 pr_info("Early serial console at I/O port 0x%lx (options '%s')\n",
123 port->iobase,
124 device->options);
125
126 return 0;
127}
128
Peter Hurley470ca0d2015-03-09 16:27:21 -0400129static int __init register_earlycon(char *buf, const struct earlycon_id *match)
Rob Herring9aac5882014-04-18 17:19:55 -0500130{
131 int err;
Rob Herring9aac5882014-04-18 17:19:55 -0500132 struct uart_port *port = &early_console_dev.port;
133
Rob Herring9aac5882014-04-18 17:19:55 -0500134 /* On parsing error, pass the options buf to the setup function */
Peter Hurley60846882015-03-09 16:27:19 -0400135 if (buf && !parse_options(&early_console_dev, buf))
Rob Herring9aac5882014-04-18 17:19:55 -0500136 buf = NULL;
137
Geert Uytterhoevene1dd3be2015-11-27 11:13:48 +0100138 spin_lock_init(&port->lock);
Peter Hurleyfeed5ba2015-03-09 16:27:15 -0400139 port->uartclk = BASE_BAUD * 16;
Rob Herring9aac5882014-04-18 17:19:55 -0500140 if (port->mapbase)
141 port->membase = earlycon_map(port->mapbase, 64);
142
Peter Hurleycda64e62016-01-16 15:23:40 -0800143 earlycon_init(&early_console_dev, match->name);
Peter Hurley470ca0d2015-03-09 16:27:21 -0400144 err = match->setup(&early_console_dev, buf);
Rob Herring9aac5882014-04-18 17:19:55 -0500145 if (err < 0)
146 return err;
147 if (!early_console_dev.con->write)
148 return -ENODEV;
149
150 register_console(early_console_dev.con);
151 return 0;
152}
Rob Herringb0b6abd2014-03-27 08:06:16 -0500153
Peter Hurley470ca0d2015-03-09 16:27:21 -0400154/**
155 * setup_earlycon - match and register earlycon console
156 * @buf: earlycon param string
157 *
158 * Registers the earlycon console matching the earlycon specified
159 * in the param string @buf. Acceptable param strings are of the form
Noam Camus6e63be32015-05-25 06:54:28 +0300160 * <name>,io|mmio|mmio32|mmio32be,<addr>,<options>
Peter Hurley470ca0d2015-03-09 16:27:21 -0400161 * <name>,0x<addr>,<options>
162 * <name>,<options>
163 * <name>
164 *
165 * Only for the third form does the earlycon setup() method receive the
166 * <options> string in the 'options' parameter; all other forms set
167 * the parameter to NULL.
168 *
169 * Returns 0 if an attempt to register the earlycon was made,
170 * otherwise negative error code
171 */
172int __init setup_earlycon(char *buf)
Peter Hurley7c53cb32015-03-09 16:27:20 -0400173{
Peter Hurley470ca0d2015-03-09 16:27:21 -0400174 const struct earlycon_id *match;
Peter Hurley7c53cb32015-03-09 16:27:20 -0400175
Peter Hurley470ca0d2015-03-09 16:27:21 -0400176 if (!buf || !buf[0])
177 return -EINVAL;
Peter Hurley7c53cb32015-03-09 16:27:20 -0400178
Peter Hurley470ca0d2015-03-09 16:27:21 -0400179 if (early_con.flags & CON_ENABLED)
180 return -EALREADY;
Peter Hurley7c53cb32015-03-09 16:27:20 -0400181
Peter Hurley2eaa7902016-01-16 15:23:39 -0800182 for (match = __earlycon_table; match < __earlycon_table_end; match++) {
Peter Hurley470ca0d2015-03-09 16:27:21 -0400183 size_t len = strlen(match->name);
Peter Hurley7c53cb32015-03-09 16:27:20 -0400184
Peter Hurley470ca0d2015-03-09 16:27:21 -0400185 if (strncmp(buf, match->name, len))
186 continue;
187
188 if (buf[len]) {
189 if (buf[len] != ',')
190 continue;
191 buf += len + 1;
192 } else
193 buf = NULL;
194
195 return register_earlycon(buf, match);
196 }
197
198 return -ENOENT;
Peter Hurley7c53cb32015-03-09 16:27:20 -0400199}
200
Peter Hurley470ca0d2015-03-09 16:27:21 -0400201/* early_param wrapper for setup_earlycon() */
202static int __init param_setup_earlycon(char *buf)
203{
204 int err;
205
206 /*
207 * Just 'earlycon' is a valid param for devicetree earlycons;
208 * don't generate a warning from parse_early_params() in that case
209 */
210 if (!buf || !buf[0])
211 return 0;
212
213 err = setup_earlycon(buf);
Peter Hurley66c53aa2015-05-07 14:19:21 -0400214 if (err == -ENOENT || err == -EALREADY)
215 return 0;
Peter Hurley470ca0d2015-03-09 16:27:21 -0400216 return err;
217}
218early_param("earlycon", param_setup_earlycon);
219
Peter Hurley84776142016-01-16 15:23:38 -0800220#ifdef CONFIG_OF_EARLY_FLATTREE
221
Peter Hurleyc90fe9c2016-01-16 15:23:44 -0800222int __init of_setup_earlycon(const struct earlycon_id *match,
Peter Hurley088da2a2016-01-16 15:23:43 -0800223 unsigned long node,
Peter Hurley4d118c92016-01-16 15:23:42 -0800224 const char *options)
Rob Herringb0b6abd2014-03-27 08:06:16 -0500225{
226 int err;
227 struct uart_port *port = &early_console_dev.port;
Peter Hurley088da2a2016-01-16 15:23:43 -0800228 const __be32 *val;
229 bool big_endian;
Peter Hurleyc90fe9c2016-01-16 15:23:44 -0800230 u64 addr;
Rob Herringb0b6abd2014-03-27 08:06:16 -0500231
Geert Uytterhoevene1dd3be2015-11-27 11:13:48 +0100232 spin_lock_init(&port->lock);
Rob Herringb0b6abd2014-03-27 08:06:16 -0500233 port->iotype = UPIO_MEM;
Peter Hurleyc90fe9c2016-01-16 15:23:44 -0800234 addr = of_flat_dt_translate_address(node);
235 if (addr == OF_BAD_ADDR) {
236 pr_warn("[%s] bad address\n", match->name);
237 return -ENXIO;
238 }
Rob Herringb0b6abd2014-03-27 08:06:16 -0500239 port->mapbase = addr;
240 port->uartclk = BASE_BAUD * 16;
Peter Hurleyc90fe9c2016-01-16 15:23:44 -0800241 port->membase = earlycon_map(port->mapbase, SZ_4K);
Rob Herringb0b6abd2014-03-27 08:06:16 -0500242
Peter Hurley088da2a2016-01-16 15:23:43 -0800243 val = of_get_flat_dt_prop(node, "reg-offset", NULL);
244 if (val)
245 port->mapbase += be32_to_cpu(*val);
246 val = of_get_flat_dt_prop(node, "reg-shift", NULL);
247 if (val)
248 port->regshift = be32_to_cpu(*val);
249 big_endian = of_get_flat_dt_prop(node, "big-endian", NULL) != NULL ||
250 (IS_ENABLED(CONFIG_CPU_BIG_ENDIAN) &&
251 of_get_flat_dt_prop(node, "native-endian", NULL) != NULL);
252 val = of_get_flat_dt_prop(node, "reg-io-width", NULL);
253 if (val) {
254 switch (be32_to_cpu(*val)) {
255 case 1:
256 port->iotype = UPIO_MEM;
257 break;
258 case 2:
259 port->iotype = UPIO_MEM16;
260 break;
261 case 4:
262 port->iotype = (big_endian) ? UPIO_MEM32BE : UPIO_MEM32;
263 break;
264 default:
265 pr_warn("[%s] unsupported reg-io-width\n", match->name);
266 return -EINVAL;
267 }
268 }
269
Peter Hurley4d118c92016-01-16 15:23:42 -0800270 if (options) {
271 strlcpy(early_console_dev.options, options,
272 sizeof(early_console_dev.options));
273 }
Peter Hurley05d96132016-01-16 15:23:41 -0800274 earlycon_init(&early_console_dev, match->name);
Peter Hurley4d118c92016-01-16 15:23:42 -0800275 err = match->setup(&early_console_dev, options);
Rob Herringb0b6abd2014-03-27 08:06:16 -0500276 if (err < 0)
277 return err;
278 if (!early_console_dev.con->write)
279 return -ENODEV;
280
281
282 register_console(early_console_dev.con);
283 return 0;
284}
Peter Hurley84776142016-01-16 15:23:38 -0800285
286#endif /* CONFIG_OF_EARLY_FLATTREE */