blob: 7089667bde93c47fb39c07f07a95a38f8ed32aaf [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 Hurley088da2a2016-01-16 15:23:43 -080022#include <linux/of_fdt.h>
Rob Herring9aac5882014-04-18 17:19:55 -050023
24#ifdef CONFIG_FIX_EARLYCON_MEM
25#include <asm/fixmap.h>
26#endif
27
28#include <asm/serial.h>
29
30static struct console early_con = {
Peter Hurleycda64e62016-01-16 15:23:40 -080031 .name = "uart", /* fixed up at earlycon registration */
Rob Herring9aac5882014-04-18 17:19:55 -050032 .flags = CON_PRINTBUFFER | CON_BOOT,
Peter Hurleycda64e62016-01-16 15:23:40 -080033 .index = 0,
Rob Herring9aac5882014-04-18 17:19:55 -050034};
35
36static struct earlycon_device early_console_dev = {
37 .con = &early_con,
38};
39
40static void __iomem * __init earlycon_map(unsigned long paddr, size_t size)
41{
42 void __iomem *base;
43#ifdef CONFIG_FIX_EARLYCON_MEM
44 set_fixmap_io(FIX_EARLYCON_MEM_BASE, paddr & PAGE_MASK);
45 base = (void __iomem *)__fix_to_virt(FIX_EARLYCON_MEM_BASE);
46 base += paddr & ~PAGE_MASK;
47#else
48 base = ioremap(paddr, size);
49#endif
50 if (!base)
51 pr_err("%s: Couldn't map 0x%llx\n", __func__,
52 (unsigned long long)paddr);
53
54 return base;
55}
56
Peter Hurleycda64e62016-01-16 15:23:40 -080057static void __init earlycon_init(struct earlycon_device *device,
58 const char *name)
59{
60 struct console *earlycon = device->con;
61 const char *s;
62 size_t len;
63
64 /* scan backwards from end of string for first non-numeral */
65 for (s = name + strlen(name);
66 s > name && s[-1] >= '0' && s[-1] <= '9';
67 s--)
68 ;
69 if (*s)
70 earlycon->index = simple_strtoul(s, NULL, 10);
71 len = s - name;
72 strlcpy(earlycon->name, name, min(len + 1, sizeof(earlycon->name)));
73 earlycon->data = &early_console_dev;
74}
75
Peter Hurley73abaf82015-03-01 11:05:46 -050076static int __init parse_options(struct earlycon_device *device, char *options)
Rob Herring9aac5882014-04-18 17:19:55 -050077{
78 struct uart_port *port = &device->port;
Peter Hurley73abaf82015-03-01 11:05:46 -050079 int length;
Rob Herring9aac5882014-04-18 17:19:55 -050080 unsigned long addr;
81
Peter Hurley73abaf82015-03-01 11:05:46 -050082 if (uart_parse_earlycon(options, &port->iotype, &addr, &options))
83 return -EINVAL;
Rob Herring9aac5882014-04-18 17:19:55 -050084
Peter Hurley73abaf82015-03-01 11:05:46 -050085 switch (port->iotype) {
Masahiro Yamadabd94c402015-10-28 12:46:05 +090086 case UPIO_MEM:
87 port->mapbase = addr;
88 break;
89 case UPIO_MEM16:
90 port->regshift = 1;
91 port->mapbase = addr;
92 break;
Peter Hurley73abaf82015-03-01 11:05:46 -050093 case UPIO_MEM32:
Noam Camus6e63be32015-05-25 06:54:28 +030094 case UPIO_MEM32BE:
Masahiro Yamadabd94c402015-10-28 12:46:05 +090095 port->regshift = 2;
Rob Herring9aac5882014-04-18 17:19:55 -050096 port->mapbase = addr;
Peter Hurley73abaf82015-03-01 11:05:46 -050097 break;
98 case UPIO_PORT:
Rob Herring9aac5882014-04-18 17:19:55 -050099 port->iobase = addr;
Peter Hurley73abaf82015-03-01 11:05:46 -0500100 break;
101 default:
Rob Herring9aac5882014-04-18 17:19:55 -0500102 return -EINVAL;
103 }
104
Rob Herring9aac5882014-04-18 17:19:55 -0500105 if (options) {
Rob Herringe26f1db2014-04-30 19:48:29 -0500106 device->baud = simple_strtoul(options, NULL, 0);
Rob Herring9aac5882014-04-18 17:19:55 -0500107 length = min(strcspn(options, " ") + 1,
108 (size_t)(sizeof(device->options)));
109 strlcpy(device->options, options, length);
110 }
111
Masahiro Yamadabd94c402015-10-28 12:46:05 +0900112 if (port->iotype == UPIO_MEM || port->iotype == UPIO_MEM16 ||
113 port->iotype == UPIO_MEM32 || port->iotype == UPIO_MEM32BE)
Rob Herring9aac5882014-04-18 17:19:55 -0500114 pr_info("Early serial console at MMIO%s 0x%llx (options '%s')\n",
Noam Camus6e63be32015-05-25 06:54:28 +0300115 (port->iotype == UPIO_MEM) ? "" :
Masahiro Yamadabd94c402015-10-28 12:46:05 +0900116 (port->iotype == UPIO_MEM16) ? "16" :
Noam Camus6e63be32015-05-25 06:54:28 +0300117 (port->iotype == UPIO_MEM32) ? "32" : "32be",
Rob Herring9aac5882014-04-18 17:19:55 -0500118 (unsigned long long)port->mapbase,
119 device->options);
120 else
121 pr_info("Early serial console at I/O port 0x%lx (options '%s')\n",
122 port->iobase,
123 device->options);
124
125 return 0;
126}
127
Peter Hurley470ca0d2015-03-09 16:27:21 -0400128static int __init register_earlycon(char *buf, const struct earlycon_id *match)
Rob Herring9aac5882014-04-18 17:19:55 -0500129{
130 int err;
Rob Herring9aac5882014-04-18 17:19:55 -0500131 struct uart_port *port = &early_console_dev.port;
132
Rob Herring9aac5882014-04-18 17:19:55 -0500133 /* On parsing error, pass the options buf to the setup function */
Peter Hurley60846882015-03-09 16:27:19 -0400134 if (buf && !parse_options(&early_console_dev, buf))
Rob Herring9aac5882014-04-18 17:19:55 -0500135 buf = NULL;
136
Geert Uytterhoevene1dd3be2015-11-27 11:13:48 +0100137 spin_lock_init(&port->lock);
Peter Hurleyfeed5ba2015-03-09 16:27:15 -0400138 port->uartclk = BASE_BAUD * 16;
Rob Herring9aac5882014-04-18 17:19:55 -0500139 if (port->mapbase)
140 port->membase = earlycon_map(port->mapbase, 64);
141
Peter Hurleycda64e62016-01-16 15:23:40 -0800142 earlycon_init(&early_console_dev, match->name);
Peter Hurley470ca0d2015-03-09 16:27:21 -0400143 err = match->setup(&early_console_dev, buf);
Rob Herring9aac5882014-04-18 17:19:55 -0500144 if (err < 0)
145 return err;
146 if (!early_console_dev.con->write)
147 return -ENODEV;
148
149 register_console(early_console_dev.con);
150 return 0;
151}
Rob Herringb0b6abd2014-03-27 08:06:16 -0500152
Peter Hurley470ca0d2015-03-09 16:27:21 -0400153/**
154 * setup_earlycon - match and register earlycon console
155 * @buf: earlycon param string
156 *
157 * Registers the earlycon console matching the earlycon specified
158 * in the param string @buf. Acceptable param strings are of the form
Noam Camus6e63be32015-05-25 06:54:28 +0300159 * <name>,io|mmio|mmio32|mmio32be,<addr>,<options>
Peter Hurley470ca0d2015-03-09 16:27:21 -0400160 * <name>,0x<addr>,<options>
161 * <name>,<options>
162 * <name>
163 *
164 * Only for the third form does the earlycon setup() method receive the
165 * <options> string in the 'options' parameter; all other forms set
166 * the parameter to NULL.
167 *
168 * Returns 0 if an attempt to register the earlycon was made,
169 * otherwise negative error code
170 */
171int __init setup_earlycon(char *buf)
Peter Hurley7c53cb32015-03-09 16:27:20 -0400172{
Peter Hurley470ca0d2015-03-09 16:27:21 -0400173 const struct earlycon_id *match;
Peter Hurley7c53cb32015-03-09 16:27:20 -0400174
Peter Hurley470ca0d2015-03-09 16:27:21 -0400175 if (!buf || !buf[0])
176 return -EINVAL;
Peter Hurley7c53cb32015-03-09 16:27:20 -0400177
Peter Hurley470ca0d2015-03-09 16:27:21 -0400178 if (early_con.flags & CON_ENABLED)
179 return -EALREADY;
Peter Hurley7c53cb32015-03-09 16:27:20 -0400180
Peter Hurley2eaa7902016-01-16 15:23:39 -0800181 for (match = __earlycon_table; match < __earlycon_table_end; match++) {
Peter Hurley470ca0d2015-03-09 16:27:21 -0400182 size_t len = strlen(match->name);
Peter Hurley7c53cb32015-03-09 16:27:20 -0400183
Peter Hurley470ca0d2015-03-09 16:27:21 -0400184 if (strncmp(buf, match->name, len))
185 continue;
186
187 if (buf[len]) {
188 if (buf[len] != ',')
189 continue;
190 buf += len + 1;
191 } else
192 buf = NULL;
193
194 return register_earlycon(buf, match);
195 }
196
197 return -ENOENT;
Peter Hurley7c53cb32015-03-09 16:27:20 -0400198}
199
Peter Hurley470ca0d2015-03-09 16:27:21 -0400200/* early_param wrapper for setup_earlycon() */
201static int __init param_setup_earlycon(char *buf)
202{
203 int err;
204
205 /*
206 * Just 'earlycon' is a valid param for devicetree earlycons;
207 * don't generate a warning from parse_early_params() in that case
208 */
209 if (!buf || !buf[0])
210 return 0;
211
212 err = setup_earlycon(buf);
Peter Hurley66c53aa2015-05-07 14:19:21 -0400213 if (err == -ENOENT || err == -EALREADY)
214 return 0;
Peter Hurley470ca0d2015-03-09 16:27:21 -0400215 return err;
216}
217early_param("earlycon", param_setup_earlycon);
218
Peter Hurley84776142016-01-16 15:23:38 -0800219#ifdef CONFIG_OF_EARLY_FLATTREE
220
Rob Herringb0b6abd2014-03-27 08:06:16 -0500221int __init of_setup_earlycon(unsigned long addr,
Peter Hurley4d118c92016-01-16 15:23:42 -0800222 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;
Rob Herringb0b6abd2014-03-27 08:06:16 -0500230
Geert Uytterhoevene1dd3be2015-11-27 11:13:48 +0100231 spin_lock_init(&port->lock);
Rob Herringb0b6abd2014-03-27 08:06:16 -0500232 port->iotype = UPIO_MEM;
233 port->mapbase = addr;
234 port->uartclk = BASE_BAUD * 16;
235 port->membase = earlycon_map(addr, SZ_4K);
236
Peter Hurley088da2a2016-01-16 15:23:43 -0800237 val = of_get_flat_dt_prop(node, "reg-offset", NULL);
238 if (val)
239 port->mapbase += be32_to_cpu(*val);
240 val = of_get_flat_dt_prop(node, "reg-shift", NULL);
241 if (val)
242 port->regshift = be32_to_cpu(*val);
243 big_endian = of_get_flat_dt_prop(node, "big-endian", NULL) != NULL ||
244 (IS_ENABLED(CONFIG_CPU_BIG_ENDIAN) &&
245 of_get_flat_dt_prop(node, "native-endian", NULL) != NULL);
246 val = of_get_flat_dt_prop(node, "reg-io-width", NULL);
247 if (val) {
248 switch (be32_to_cpu(*val)) {
249 case 1:
250 port->iotype = UPIO_MEM;
251 break;
252 case 2:
253 port->iotype = UPIO_MEM16;
254 break;
255 case 4:
256 port->iotype = (big_endian) ? UPIO_MEM32BE : UPIO_MEM32;
257 break;
258 default:
259 pr_warn("[%s] unsupported reg-io-width\n", match->name);
260 return -EINVAL;
261 }
262 }
263
Peter Hurley4d118c92016-01-16 15:23:42 -0800264 if (options) {
265 strlcpy(early_console_dev.options, options,
266 sizeof(early_console_dev.options));
267 }
Peter Hurley05d96132016-01-16 15:23:41 -0800268 earlycon_init(&early_console_dev, match->name);
Peter Hurley4d118c92016-01-16 15:23:42 -0800269 err = match->setup(&early_console_dev, options);
Rob Herringb0b6abd2014-03-27 08:06:16 -0500270 if (err < 0)
271 return err;
272 if (!early_console_dev.con->write)
273 return -ENODEV;
274
275
276 register_console(early_console_dev.con);
277 return 0;
278}
Peter Hurley84776142016-01-16 15:23:38 -0800279
280#endif /* CONFIG_OF_EARLY_FLATTREE */