1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2004 Marcel Moolenaar
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/bus.h>
33
34 #include <machine/bus.h>
35 #include <machine/vmparam.h>
36
37 #include <dev/uart/uart.h>
38 #include <dev/uart/uart_cpu.h>
39
40 #define UART_TAG_BR 0
41 #define UART_TAG_CH 1
42 #define UART_TAG_DB 2
43 #define UART_TAG_DT 3
44 #define UART_TAG_IO 4
45 #define UART_TAG_MM 5
46 #define UART_TAG_PA 6
47 #define UART_TAG_RS 7
48 #define UART_TAG_SB 8
49 #define UART_TAG_XO 9
50 #define UART_TAG_BD 10
51
52 static struct uart_class *uart_classes[] = {
53 &uart_ns8250_class,
54 &uart_z8530_class,
55 #if defined(__arm__)
56 &uart_s3c2410_class,
57 #endif
58 };
59
60 static bus_addr_t
uart_parse_addr(const char ** p)61 uart_parse_addr(const char **p)
62 {
63 return (strtoul(*p, (char**)(uintptr_t)p, 0));
64 }
65
66 static struct uart_class *
uart_parse_class(struct uart_class * class,const char ** p)67 uart_parse_class(struct uart_class *class, const char **p)
68 {
69 struct uart_class *uc;
70 const char *nm;
71 size_t len;
72 u_int i;
73
74 for (i = 0; i < nitems(uart_classes); i++) {
75 uc = uart_classes[i];
76 nm = uart_getname(uc);
77 if (nm == NULL || *nm == '\0')
78 continue;
79 len = strlen(nm);
80 if (strncmp(nm, *p, len) == 0) {
81 *p += len;
82 return (uc);
83 }
84 }
85 return (class);
86 }
87
88 static long
uart_parse_long(const char ** p)89 uart_parse_long(const char **p)
90 {
91 return (strtol(*p, (char**)(uintptr_t)p, 0));
92 }
93
94 static int
uart_parse_parity(const char ** p)95 uart_parse_parity(const char **p)
96 {
97 if (!strncmp(*p, "even", 4)) {
98 *p += 4;
99 return UART_PARITY_EVEN;
100 }
101 if (!strncmp(*p, "mark", 4)) {
102 *p += 4;
103 return UART_PARITY_MARK;
104 }
105 if (!strncmp(*p, "none", 4)) {
106 *p += 4;
107 return UART_PARITY_NONE;
108 }
109 if (!strncmp(*p, "odd", 3)) {
110 *p += 3;
111 return UART_PARITY_ODD;
112 }
113 if (!strncmp(*p, "space", 5)) {
114 *p += 5;
115 return UART_PARITY_SPACE;
116 }
117 return (-1);
118 }
119
120 static int
uart_parse_tag(const char ** p)121 uart_parse_tag(const char **p)
122 {
123 int tag;
124
125 if ((*p)[0] == 'b' && (*p)[1] == 'd') {
126 tag = UART_TAG_BD;
127 goto out;
128 }
129 if ((*p)[0] == 'b' && (*p)[1] == 'r') {
130 tag = UART_TAG_BR;
131 goto out;
132 }
133 if ((*p)[0] == 'c' && (*p)[1] == 'h') {
134 tag = UART_TAG_CH;
135 goto out;
136 }
137 if ((*p)[0] == 'd' && (*p)[1] == 'b') {
138 tag = UART_TAG_DB;
139 goto out;
140 }
141 if ((*p)[0] == 'd' && (*p)[1] == 't') {
142 tag = UART_TAG_DT;
143 goto out;
144 }
145 if ((*p)[0] == 'i' && (*p)[1] == 'o') {
146 tag = UART_TAG_IO;
147 goto out;
148 }
149 if ((*p)[0] == 'm' && (*p)[1] == 'm') {
150 tag = UART_TAG_MM;
151 goto out;
152 }
153 if ((*p)[0] == 'p' && (*p)[1] == 'a') {
154 tag = UART_TAG_PA;
155 goto out;
156 }
157 if ((*p)[0] == 'r' && (*p)[1] == 's') {
158 tag = UART_TAG_RS;
159 goto out;
160 }
161 if ((*p)[0] == 's' && (*p)[1] == 'b') {
162 tag = UART_TAG_SB;
163 goto out;
164 }
165 if ((*p)[0] == 'x' && (*p)[1] == 'o') {
166 tag = UART_TAG_XO;
167 goto out;
168 }
169 return (-1);
170
171 out:
172 *p += 2;
173 if ((*p)[0] != ':' && (*p)[0] != '=')
174 return (-1);
175 (*p)++;
176 return (tag);
177 }
178
179 /*
180 * Parse a device specification. The specification is a list of attributes
181 * separated by commas. Each attribute is a tag-value pair with the tag and
182 * value separated by a colon. Supported tags are:
183 *
184 * bd = Busy Detect
185 * br = Baudrate
186 * ch = Channel
187 * db = Data bits
188 * dt = Device type
189 * io = I/O port address
190 * mm = Memory mapped I/O address
191 * pa = Parity
192 * rs = Register shift
193 * sb = Stopbits
194 * xo = Device clock (xtal oscillator)
195 *
196 * The io and mm tags are mutually exclusive.
197 */
198
199 int
uart_getenv(int devtype,struct uart_devinfo * di,struct uart_class * class)200 uart_getenv(int devtype, struct uart_devinfo *di, struct uart_class *class)
201 {
202 const char *spec;
203 char *cp;
204 bus_addr_t addr = ~0U;
205 int error;
206
207 /*
208 * All uart_class references are weak. Make sure the default
209 * device class has been compiled-in.
210 */
211 if (class == NULL)
212 return (ENXIO);
213
214 /*
215 * Check the environment variables "hw.uart.console" and
216 * "hw.uart.dbgport". These variables, when present, specify
217 * which UART port is to be used as serial console or debug
218 * port (resp).
219 */
220 switch (devtype) {
221 case UART_DEV_CONSOLE:
222 cp = kern_getenv("hw.uart.console");
223 break;
224 case UART_DEV_DBGPORT:
225 cp = kern_getenv("hw.uart.dbgport");
226 break;
227 default:
228 cp = NULL;
229 break;
230 }
231
232 if (cp == NULL)
233 return (ENXIO);
234
235 /* Set defaults. */
236 di->bas.chan = 0;
237 di->bas.regshft = 0;
238 di->bas.rclk = 0;
239 di->baudrate = 0;
240 di->databits = 8;
241 di->stopbits = 1;
242 di->parity = UART_PARITY_NONE;
243
244 /* Parse the attributes. */
245 spec = cp;
246 for (;;) {
247 switch (uart_parse_tag(&spec)) {
248 case UART_TAG_BD:
249 di->bas.busy_detect = uart_parse_long(&spec);
250 break;
251 case UART_TAG_BR:
252 di->baudrate = uart_parse_long(&spec);
253 break;
254 case UART_TAG_CH:
255 di->bas.chan = uart_parse_long(&spec);
256 break;
257 case UART_TAG_DB:
258 di->databits = uart_parse_long(&spec);
259 break;
260 case UART_TAG_DT:
261 class = uart_parse_class(class, &spec);
262 break;
263 case UART_TAG_IO:
264 di->bas.bst = uart_bus_space_io;
265 addr = uart_parse_addr(&spec);
266 break;
267 case UART_TAG_MM:
268 di->bas.bst = uart_bus_space_mem;
269 addr = uart_parse_addr(&spec);
270 break;
271 case UART_TAG_PA:
272 di->parity = uart_parse_parity(&spec);
273 break;
274 case UART_TAG_RS:
275 di->bas.regshft = uart_parse_long(&spec);
276 break;
277 case UART_TAG_SB:
278 di->stopbits = uart_parse_long(&spec);
279 break;
280 case UART_TAG_XO:
281 di->bas.rclk = uart_parse_long(&spec);
282 break;
283 default:
284 goto inval;
285 }
286 if (*spec == '\0')
287 break;
288 if (*spec != ',')
289 goto inval;
290 spec++;
291 }
292
293 /*
294 * If we still have an invalid address, the specification must be
295 * missing an I/O port or memory address. We don't like that.
296 */
297 if (addr == ~0U)
298 goto inval;
299 freeenv(cp);
300
301 /*
302 * Accept only the well-known baudrates. Any invalid baudrate
303 * is silently replaced with a 0-valued baudrate. The 0 baudrate
304 * has special meaning. It means that we're not supposed to
305 * program the baudrate and simply communicate with whatever
306 * speed the hardware is currently programmed for.
307 */
308 if (di->baudrate >= 19200) {
309 if (di->baudrate % 19200)
310 di->baudrate = 0;
311 } else if (di->baudrate >= 1200) {
312 if (di->baudrate % 1200)
313 di->baudrate = 0;
314 } else if (di->baudrate > 0) {
315 if (di->baudrate % 75)
316 di->baudrate = 0;
317 } else
318 di->baudrate = 0;
319
320 /* Set the ops and create a bus space handle. */
321 di->ops = uart_getops(class);
322 error = bus_space_map(di->bas.bst, addr, uart_getrange(class), 0,
323 &di->bas.bsh);
324 return (error);
325 inval:
326 printf("warning: bad uart specification: %s\n", cp);
327 freeenv(cp);
328 return (EINVAL);
329 }
330