2 * Copyright (c) 2004 Marcel Moolenaar
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/systm.h>
34 #include <machine/bus.h>
35 #include <machine/vmparam.h>
37 #include <dev/uart/uart.h>
38 #include <dev/uart/uart_cpu.h>
51 static struct uart_class *uart_classes[] = {
57 static size_t uart_nclasses = sizeof(uart_classes) / sizeof(uart_classes[0]);
60 uart_parse_addr(__const char **p)
62 return (strtoul(*p, (char**)(uintptr_t)p, 0));
65 static struct uart_class *
66 uart_parse_class(struct uart_class *class, __const char **p)
68 struct uart_class *uc;
73 for (i = 0; i < uart_nclasses; i++) {
75 nm = uart_getname(uc);
76 if (nm == NULL || *nm == '\0')
79 if (strncmp(nm, *p, len) == 0) {
88 uart_parse_long(__const char **p)
90 return (strtol(*p, (char**)(uintptr_t)p, 0));
94 uart_parse_parity(__const char **p)
96 if (!strncmp(*p, "even", 4)) {
98 return UART_PARITY_EVEN;
100 if (!strncmp(*p, "mark", 4)) {
102 return UART_PARITY_MARK;
104 if (!strncmp(*p, "none", 4)) {
106 return UART_PARITY_NONE;
108 if (!strncmp(*p, "odd", 3)) {
110 return UART_PARITY_ODD;
112 if (!strncmp(*p, "space", 5)) {
114 return UART_PARITY_SPACE;
120 uart_parse_tag(__const char **p)
124 if ((*p)[0] == 'b' && (*p)[1] == 'r') {
128 if ((*p)[0] == 'c' && (*p)[1] == 'h') {
132 if ((*p)[0] == 'd' && (*p)[1] == 'b') {
136 if ((*p)[0] == 'd' && (*p)[1] == 't') {
140 if ((*p)[0] == 'i' && (*p)[1] == 'o') {
144 if ((*p)[0] == 'm' && (*p)[1] == 'm') {
148 if ((*p)[0] == 'p' && (*p)[1] == 'a') {
152 if ((*p)[0] == 'r' && (*p)[1] == 's') {
156 if ((*p)[0] == 's' && (*p)[1] == 'b') {
160 if ((*p)[0] == 'x' && (*p)[1] == 'o') {
175 * Parse a device specification. The specification is a list of attributes
176 * separated by commas. Each attribute is a tag-value pair with the tag and
177 * value separated by a colon. Supported tags are:
183 * io = I/O port address
184 * mm = Memory mapped I/O address
186 * rs = Register shift
188 * xo = Device clock (xtal oscillator)
190 * The io and mm tags are mutually exclusive.
194 uart_getenv(int devtype, struct uart_devinfo *di, struct uart_class *class)
197 bus_addr_t addr = ~0U;
201 * All uart_class references are weak. Make sure the default
202 * device class has been compiled-in.
208 * Check the environment variables "hw.uart.console" and
209 * "hw.uart.dbgport". These variables, when present, specify
210 * which UART port is to be used as serial console or debug
213 if (devtype == UART_DEV_CONSOLE)
214 spec = getenv("hw.uart.console");
215 else if (devtype == UART_DEV_DBGPORT)
216 spec = getenv("hw.uart.dbgport");
229 di->parity = UART_PARITY_NONE;
231 /* Parse the attributes. */
233 switch (uart_parse_tag(&spec)) {
235 di->baudrate = uart_parse_long(&spec);
238 di->bas.chan = uart_parse_long(&spec);
241 di->databits = uart_parse_long(&spec);
244 class = uart_parse_class(class, &spec);
247 di->bas.bst = uart_bus_space_io;
248 addr = uart_parse_addr(&spec);
251 di->bas.bst = uart_bus_space_mem;
252 addr = uart_parse_addr(&spec);
255 di->parity = uart_parse_parity(&spec);
258 di->bas.regshft = uart_parse_long(&spec);
261 di->stopbits = uart_parse_long(&spec);
264 di->bas.rclk = uart_parse_long(&spec);
276 di->ops = uart_getops(class);
277 range = uart_getrange(class);
280 * If we still have an invalid address, the specification must be
281 * missing an I/O port or memory address. We don't like that if
282 * the class expects an I/O port or memory range.
284 if (addr == ~0U && range != 0)
287 /* Create a bus space handle if applicable. */
288 if (addr != ~0U && range != 0) {
289 error = bus_space_map(di->bas.bst, addr, range, 0,
296 * Accept only the well-known baudrates. Any invalid baudrate
297 * is silently replaced with a 0-valued baudrate. The 0 baudrate
298 * has special meaning. It means that we're not supposed to
299 * program the baudrate and simply communicate with whatever
300 * speed the hardware is currently programmed for.
302 if (di->baudrate >= 19200) {
303 if (di->baudrate % 19200)
305 } else if (di->baudrate >= 1200) {
306 if (di->baudrate % 1200)
308 } else if (di->baudrate > 0) {
309 if (di->baudrate % 75)