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 };
56
57 static bus_addr_t
uart_parse_addr(const char ** p)58 uart_parse_addr(const char **p)
59 {
60 return (strtoul(*p, (char**)(uintptr_t)p, 0));
61 }
62
63 static struct uart_class *
uart_parse_class(struct uart_class * class,const char ** p)64 uart_parse_class(struct uart_class *class, const char **p)
65 {
66 struct uart_class *uc;
67 const char *nm;
68 size_t len;
69 u_int i;
70
71 for (i = 0; i < nitems(uart_classes); i++) {
72 uc = uart_classes[i];
73 nm = uart_getname(uc);
74 if (nm == NULL || *nm == '\0')
75 continue;
76 len = strlen(nm);
77 if (strncmp(nm, *p, len) == 0) {
78 *p += len;
79 return (uc);
80 }
81 }
82 return (class);
83 }
84
85 static long
uart_parse_long(const char ** p)86 uart_parse_long(const char **p)
87 {
88 return (strtol(*p, (char**)(uintptr_t)p, 0));
89 }
90
91 static int
uart_parse_parity(const char ** p)92 uart_parse_parity(const char **p)
93 {
94 if (!strncmp(*p, "even", 4)) {
95 *p += 4;
96 return UART_PARITY_EVEN;
97 }
98 if (!strncmp(*p, "mark", 4)) {
99 *p += 4;
100 return UART_PARITY_MARK;
101 }
102 if (!strncmp(*p, "none", 4)) {
103 *p += 4;
104 return UART_PARITY_NONE;
105 }
106 if (!strncmp(*p, "odd", 3)) {
107 *p += 3;
108 return UART_PARITY_ODD;
109 }
110 if (!strncmp(*p, "space", 5)) {
111 *p += 5;
112 return UART_PARITY_SPACE;
113 }
114 return (-1);
115 }
116
117 static int
uart_parse_tag(const char ** p)118 uart_parse_tag(const char **p)
119 {
120 int tag;
121
122 if ((*p)[0] == 'b' && (*p)[1] == 'd') {
123 tag = UART_TAG_BD;
124 goto out;
125 }
126 if ((*p)[0] == 'b' && (*p)[1] == 'r') {
127 tag = UART_TAG_BR;
128 goto out;
129 }
130 if ((*p)[0] == 'c' && (*p)[1] == 'h') {
131 tag = UART_TAG_CH;
132 goto out;
133 }
134 if ((*p)[0] == 'd' && (*p)[1] == 'b') {
135 tag = UART_TAG_DB;
136 goto out;
137 }
138 if ((*p)[0] == 'd' && (*p)[1] == 't') {
139 tag = UART_TAG_DT;
140 goto out;
141 }
142 if ((*p)[0] == 'i' && (*p)[1] == 'o') {
143 tag = UART_TAG_IO;
144 goto out;
145 }
146 if ((*p)[0] == 'm' && (*p)[1] == 'm') {
147 tag = UART_TAG_MM;
148 goto out;
149 }
150 if ((*p)[0] == 'p' && (*p)[1] == 'a') {
151 tag = UART_TAG_PA;
152 goto out;
153 }
154 if ((*p)[0] == 'r' && (*p)[1] == 's') {
155 tag = UART_TAG_RS;
156 goto out;
157 }
158 if ((*p)[0] == 's' && (*p)[1] == 'b') {
159 tag = UART_TAG_SB;
160 goto out;
161 }
162 if ((*p)[0] == 'x' && (*p)[1] == 'o') {
163 tag = UART_TAG_XO;
164 goto out;
165 }
166 return (-1);
167
168 out:
169 *p += 2;
170 if ((*p)[0] != ':' && (*p)[0] != '=')
171 return (-1);
172 (*p)++;
173 return (tag);
174 }
175
176 /*
177 * Parse a device specification. The specification is a list of attributes
178 * separated by commas. Each attribute is a tag-value pair with the tag and
179 * value separated by a colon. Supported tags are:
180 *
181 * bd = Busy Detect
182 * br = Baudrate
183 * ch = Channel
184 * db = Data bits
185 * dt = Device type
186 * io = I/O port address
187 * mm = Memory mapped I/O address
188 * pa = Parity
189 * rs = Register shift
190 * sb = Stopbits
191 * xo = Device clock (xtal oscillator)
192 *
193 * The io and mm tags are mutually exclusive.
194 */
195
196 int
uart_getenv(int devtype,struct uart_devinfo * di,struct uart_class * class)197 uart_getenv(int devtype, struct uart_devinfo *di, struct uart_class *class)
198 {
199 const char *spec;
200 char *cp;
201 bus_addr_t addr = ~0U;
202 int error;
203
204 /*
205 * All uart_class references are weak. Make sure the default
206 * device class has been compiled-in.
207 */
208 if (class == NULL)
209 return (ENXIO);
210
211 /*
212 * Check the environment variables "hw.uart.console" and
213 * "hw.uart.dbgport". These variables, when present, specify
214 * which UART port is to be used as serial console or debug
215 * port (resp).
216 */
217 switch (devtype) {
218 case UART_DEV_CONSOLE:
219 cp = kern_getenv("hw.uart.console");
220 break;
221 case UART_DEV_DBGPORT:
222 cp = kern_getenv("hw.uart.dbgport");
223 break;
224 default:
225 cp = NULL;
226 break;
227 }
228
229 if (cp == NULL)
230 return (ENXIO);
231
232 /* Set defaults. */
233 di->bas.chan = 0;
234 di->bas.regshft = 0;
235 di->bas.rclk = 0;
236 di->baudrate = 0;
237 di->databits = 8;
238 di->stopbits = 1;
239 di->parity = UART_PARITY_NONE;
240
241 /* Parse the attributes. */
242 spec = cp;
243 for (;;) {
244 switch (uart_parse_tag(&spec)) {
245 case UART_TAG_BD:
246 di->bas.busy_detect = uart_parse_long(&spec);
247 break;
248 case UART_TAG_BR:
249 di->baudrate = uart_parse_long(&spec);
250 break;
251 case UART_TAG_CH:
252 di->bas.chan = uart_parse_long(&spec);
253 break;
254 case UART_TAG_DB:
255 di->databits = uart_parse_long(&spec);
256 break;
257 case UART_TAG_DT:
258 class = uart_parse_class(class, &spec);
259 break;
260 case UART_TAG_IO:
261 di->bas.bst = uart_bus_space_io;
262 addr = uart_parse_addr(&spec);
263 break;
264 case UART_TAG_MM:
265 di->bas.bst = uart_bus_space_mem;
266 addr = uart_parse_addr(&spec);
267 break;
268 case UART_TAG_PA:
269 di->parity = uart_parse_parity(&spec);
270 break;
271 case UART_TAG_RS:
272 di->bas.regshft = uart_parse_long(&spec);
273 break;
274 case UART_TAG_SB:
275 di->stopbits = uart_parse_long(&spec);
276 break;
277 case UART_TAG_XO:
278 di->bas.rclk = uart_parse_long(&spec);
279 break;
280 default:
281 goto inval;
282 }
283 if (*spec == '\0')
284 break;
285 if (*spec != ',')
286 goto inval;
287 spec++;
288 }
289
290 /*
291 * If we still have an invalid address, the specification must be
292 * missing an I/O port or memory address. We don't like that.
293 */
294 if (addr == ~0U)
295 goto inval;
296 freeenv(cp);
297
298 /*
299 * Accept only the well-known baudrates. Any invalid baudrate
300 * is silently replaced with a 0-valued baudrate. The 0 baudrate
301 * has special meaning. It means that we're not supposed to
302 * program the baudrate and simply communicate with whatever
303 * speed the hardware is currently programmed for.
304 */
305 if (di->baudrate >= 19200) {
306 if (di->baudrate % 19200)
307 di->baudrate = 0;
308 } else if (di->baudrate >= 1200) {
309 if (di->baudrate % 1200)
310 di->baudrate = 0;
311 } else if (di->baudrate > 0) {
312 if (di->baudrate % 75)
313 di->baudrate = 0;
314 } else
315 di->baudrate = 0;
316
317 /* Set the ops and create a bus space handle. */
318 di->ops = uart_getops(class);
319 error = bus_space_map(di->bas.bst, addr, uart_getrange(class), 0,
320 &di->bas.bsh);
321 return (error);
322 inval:
323 printf("warning: bad uart specification: %s\n", cp);
324 freeenv(cp);
325 return (EINVAL);
326 }
327