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