]>
Commit | Line | Data |
---|---|---|
9aac5887 RH |
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 | */ | |
470ca0de PH |
13 | |
14 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt | |
15 | ||
9aac5887 RH |
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> | |
b0b6abd3 RH |
21 | #include <linux/sizes.h> |
22 | #include <linux/mod_devicetable.h> | |
9aac5887 RH |
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 = { | |
60efcf04 | 31 | .name = "uart", /* 8250 console switch requires this name */ |
9aac5887 RH |
32 | .flags = CON_PRINTBUFFER | CON_BOOT, |
33 | .index = -1, | |
34 | }; | |
35 | ||
36 | static struct earlycon_device early_console_dev = { | |
37 | .con = &early_con, | |
38 | }; | |
39 | ||
470ca0de PH |
40 | extern struct earlycon_id __earlycon_table[]; |
41 | static const struct earlycon_id __earlycon_table_sentinel | |
42 | __used __section(__earlycon_table_end); | |
43 | ||
b0b6abd3 RH |
44 | static const struct of_device_id __earlycon_of_table_sentinel |
45 | __used __section(__earlycon_of_table_end); | |
46 | ||
9aac5887 RH |
47 | static void __iomem * __init earlycon_map(unsigned long paddr, size_t size) |
48 | { | |
49 | void __iomem *base; | |
50 | #ifdef CONFIG_FIX_EARLYCON_MEM | |
51 | set_fixmap_io(FIX_EARLYCON_MEM_BASE, paddr & PAGE_MASK); | |
52 | base = (void __iomem *)__fix_to_virt(FIX_EARLYCON_MEM_BASE); | |
53 | base += paddr & ~PAGE_MASK; | |
54 | #else | |
55 | base = ioremap(paddr, size); | |
56 | #endif | |
57 | if (!base) | |
58 | pr_err("%s: Couldn't map 0x%llx\n", __func__, | |
59 | (unsigned long long)paddr); | |
60 | ||
61 | return base; | |
62 | } | |
63 | ||
73abaf87 | 64 | static int __init parse_options(struct earlycon_device *device, char *options) |
9aac5887 RH |
65 | { |
66 | struct uart_port *port = &device->port; | |
73abaf87 | 67 | int length; |
9aac5887 RH |
68 | unsigned long addr; |
69 | ||
73abaf87 PH |
70 | if (uart_parse_earlycon(options, &port->iotype, &addr, &options)) |
71 | return -EINVAL; | |
9aac5887 | 72 | |
73abaf87 PH |
73 | switch (port->iotype) { |
74 | case UPIO_MEM32: | |
6e63be3f | 75 | case UPIO_MEM32BE: |
73abaf87 PH |
76 | port->regshift = 2; /* fall-through */ |
77 | case UPIO_MEM: | |
9aac5887 | 78 | port->mapbase = addr; |
73abaf87 PH |
79 | break; |
80 | case UPIO_PORT: | |
9aac5887 | 81 | port->iobase = addr; |
73abaf87 PH |
82 | break; |
83 | default: | |
9aac5887 RH |
84 | return -EINVAL; |
85 | } | |
86 | ||
9aac5887 | 87 | if (options) { |
e26f1db9 | 88 | device->baud = simple_strtoul(options, NULL, 0); |
9aac5887 RH |
89 | length = min(strcspn(options, " ") + 1, |
90 | (size_t)(sizeof(device->options))); | |
91 | strlcpy(device->options, options, length); | |
92 | } | |
93 | ||
6e63be3f NC |
94 | if (port->iotype == UPIO_MEM || port->iotype == UPIO_MEM32 || |
95 | port->iotype == UPIO_MEM32BE) | |
9aac5887 | 96 | pr_info("Early serial console at MMIO%s 0x%llx (options '%s')\n", |
6e63be3f NC |
97 | (port->iotype == UPIO_MEM) ? "" : |
98 | (port->iotype == UPIO_MEM32) ? "32" : "32be", | |
9aac5887 RH |
99 | (unsigned long long)port->mapbase, |
100 | device->options); | |
101 | else | |
102 | pr_info("Early serial console at I/O port 0x%lx (options '%s')\n", | |
103 | port->iobase, | |
104 | device->options); | |
105 | ||
106 | return 0; | |
107 | } | |
108 | ||
470ca0de | 109 | static int __init register_earlycon(char *buf, const struct earlycon_id *match) |
9aac5887 RH |
110 | { |
111 | int err; | |
9aac5887 RH |
112 | struct uart_port *port = &early_console_dev.port; |
113 | ||
9aac5887 | 114 | /* On parsing error, pass the options buf to the setup function */ |
60846880 | 115 | if (buf && !parse_options(&early_console_dev, buf)) |
9aac5887 RH |
116 | buf = NULL; |
117 | ||
feed5bab | 118 | port->uartclk = BASE_BAUD * 16; |
9aac5887 RH |
119 | if (port->mapbase) |
120 | port->membase = earlycon_map(port->mapbase, 64); | |
121 | ||
122 | early_console_dev.con->data = &early_console_dev; | |
470ca0de | 123 | err = match->setup(&early_console_dev, buf); |
9aac5887 RH |
124 | if (err < 0) |
125 | return err; | |
126 | if (!early_console_dev.con->write) | |
127 | return -ENODEV; | |
128 | ||
129 | register_console(early_console_dev.con); | |
130 | return 0; | |
131 | } | |
b0b6abd3 | 132 | |
470ca0de PH |
133 | /** |
134 | * setup_earlycon - match and register earlycon console | |
135 | * @buf: earlycon param string | |
136 | * | |
137 | * Registers the earlycon console matching the earlycon specified | |
138 | * in the param string @buf. Acceptable param strings are of the form | |
6e63be3f | 139 | * <name>,io|mmio|mmio32|mmio32be,<addr>,<options> |
470ca0de PH |
140 | * <name>,0x<addr>,<options> |
141 | * <name>,<options> | |
142 | * <name> | |
143 | * | |
144 | * Only for the third form does the earlycon setup() method receive the | |
145 | * <options> string in the 'options' parameter; all other forms set | |
146 | * the parameter to NULL. | |
147 | * | |
148 | * Returns 0 if an attempt to register the earlycon was made, | |
149 | * otherwise negative error code | |
150 | */ | |
151 | int __init setup_earlycon(char *buf) | |
7c53cb3d | 152 | { |
470ca0de | 153 | const struct earlycon_id *match; |
7c53cb3d | 154 | |
470ca0de PH |
155 | if (!buf || !buf[0]) |
156 | return -EINVAL; | |
7c53cb3d | 157 | |
470ca0de PH |
158 | if (early_con.flags & CON_ENABLED) |
159 | return -EALREADY; | |
7c53cb3d | 160 | |
470ca0de PH |
161 | for (match = __earlycon_table; match->name[0]; match++) { |
162 | size_t len = strlen(match->name); | |
7c53cb3d | 163 | |
470ca0de PH |
164 | if (strncmp(buf, match->name, len)) |
165 | continue; | |
166 | ||
167 | if (buf[len]) { | |
168 | if (buf[len] != ',') | |
169 | continue; | |
170 | buf += len + 1; | |
171 | } else | |
172 | buf = NULL; | |
173 | ||
174 | return register_earlycon(buf, match); | |
175 | } | |
176 | ||
177 | return -ENOENT; | |
178 | } | |
179 | ||
180 | /* early_param wrapper for setup_earlycon() */ | |
181 | static int __init param_setup_earlycon(char *buf) | |
182 | { | |
183 | int err; | |
184 | ||
185 | /* | |
186 | * Just 'earlycon' is a valid param for devicetree earlycons; | |
187 | * don't generate a warning from parse_early_params() in that case | |
188 | */ | |
189 | if (!buf || !buf[0]) | |
190 | return 0; | |
191 | ||
192 | err = setup_earlycon(buf); | |
66c53aaa PH |
193 | if (err == -ENOENT || err == -EALREADY) |
194 | return 0; | |
470ca0de | 195 | return err; |
7c53cb3d | 196 | } |
470ca0de | 197 | early_param("earlycon", param_setup_earlycon); |
7c53cb3d | 198 | |
b0b6abd3 RH |
199 | int __init of_setup_earlycon(unsigned long addr, |
200 | int (*setup)(struct earlycon_device *, const char *)) | |
201 | { | |
202 | int err; | |
203 | struct uart_port *port = &early_console_dev.port; | |
204 | ||
205 | port->iotype = UPIO_MEM; | |
206 | port->mapbase = addr; | |
207 | port->uartclk = BASE_BAUD * 16; | |
208 | port->membase = earlycon_map(addr, SZ_4K); | |
209 | ||
210 | early_console_dev.con->data = &early_console_dev; | |
211 | err = setup(&early_console_dev, NULL); | |
212 | if (err < 0) | |
213 | return err; | |
214 | if (!early_console_dev.con->write) | |
215 | return -ENODEV; | |
216 | ||
217 | ||
218 | register_console(early_console_dev.con); | |
219 | return 0; | |
220 | } |