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