7089667bde93c47fb39c07f07a95a38f8ed32aaf
[cascardo/linux.git] / drivers / tty / serial / earlycon.c
1 /*
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  */
13
14 #define pr_fmt(fmt)     KBUILD_MODNAME ": " fmt
15
16 #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>
21 #include <linux/sizes.h>
22 #include <linux/of_fdt.h>
23
24 #ifdef CONFIG_FIX_EARLYCON_MEM
25 #include <asm/fixmap.h>
26 #endif
27
28 #include <asm/serial.h>
29
30 static struct console early_con = {
31         .name =         "uart",         /* fixed up at earlycon registration */
32         .flags =        CON_PRINTBUFFER | CON_BOOT,
33         .index =        0,
34 };
35
36 static struct earlycon_device early_console_dev = {
37         .con = &early_con,
38 };
39
40 static 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
57 static 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
76 static int __init parse_options(struct earlycon_device *device, char *options)
77 {
78         struct uart_port *port = &device->port;
79         int length;
80         unsigned long addr;
81
82         if (uart_parse_earlycon(options, &port->iotype, &addr, &options))
83                 return -EINVAL;
84
85         switch (port->iotype) {
86         case UPIO_MEM:
87                 port->mapbase = addr;
88                 break;
89         case UPIO_MEM16:
90                 port->regshift = 1;
91                 port->mapbase = addr;
92                 break;
93         case UPIO_MEM32:
94         case UPIO_MEM32BE:
95                 port->regshift = 2;
96                 port->mapbase = addr;
97                 break;
98         case UPIO_PORT:
99                 port->iobase = addr;
100                 break;
101         default:
102                 return -EINVAL;
103         }
104
105         if (options) {
106                 device->baud = simple_strtoul(options, NULL, 0);
107                 length = min(strcspn(options, " ") + 1,
108                              (size_t)(sizeof(device->options)));
109                 strlcpy(device->options, options, length);
110         }
111
112         if (port->iotype == UPIO_MEM || port->iotype == UPIO_MEM16 ||
113             port->iotype == UPIO_MEM32 || port->iotype == UPIO_MEM32BE)
114                 pr_info("Early serial console at MMIO%s 0x%llx (options '%s')\n",
115                         (port->iotype == UPIO_MEM) ? "" :
116                         (port->iotype == UPIO_MEM16) ? "16" :
117                         (port->iotype == UPIO_MEM32) ? "32" : "32be",
118                         (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
128 static int __init register_earlycon(char *buf, const struct earlycon_id *match)
129 {
130         int err;
131         struct uart_port *port = &early_console_dev.port;
132
133         /* On parsing error, pass the options buf to the setup function */
134         if (buf && !parse_options(&early_console_dev, buf))
135                 buf = NULL;
136
137         spin_lock_init(&port->lock);
138         port->uartclk = BASE_BAUD * 16;
139         if (port->mapbase)
140                 port->membase = earlycon_map(port->mapbase, 64);
141
142         earlycon_init(&early_console_dev, match->name);
143         err = match->setup(&early_console_dev, buf);
144         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 }
152
153 /**
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
159  *         <name>,io|mmio|mmio32|mmio32be,<addr>,<options>
160  *         <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  */
171 int __init setup_earlycon(char *buf)
172 {
173         const struct earlycon_id *match;
174
175         if (!buf || !buf[0])
176                 return -EINVAL;
177
178         if (early_con.flags & CON_ENABLED)
179                 return -EALREADY;
180
181         for (match = __earlycon_table; match < __earlycon_table_end; match++) {
182                 size_t len = strlen(match->name);
183
184                 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;
198 }
199
200 /* early_param wrapper for setup_earlycon() */
201 static 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);
213         if (err == -ENOENT || err == -EALREADY)
214                 return 0;
215         return err;
216 }
217 early_param("earlycon", param_setup_earlycon);
218
219 #ifdef CONFIG_OF_EARLY_FLATTREE
220
221 int __init of_setup_earlycon(unsigned long addr,
222                              const struct earlycon_id *match,
223                              unsigned long node,
224                              const char *options)
225 {
226         int err;
227         struct uart_port *port = &early_console_dev.port;
228         const __be32 *val;
229         bool big_endian;
230
231         spin_lock_init(&port->lock);
232         port->iotype = UPIO_MEM;
233         port->mapbase = addr;
234         port->uartclk = BASE_BAUD * 16;
235         port->membase = earlycon_map(addr, SZ_4K);
236
237         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
264         if (options) {
265                 strlcpy(early_console_dev.options, options,
266                         sizeof(early_console_dev.options));
267         }
268         earlycon_init(&early_console_dev, match->name);
269         err = match->setup(&early_console_dev, options);
270         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 }
279
280 #endif /* CONFIG_OF_EARLY_FLATTREE */