2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2004 Marcel Moolenaar
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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.
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/systm.h>
36 #include <machine/bus.h>
37 #include <machine/vmparam.h>
39 #include <dev/uart/uart.h>
40 #include <dev/uart/uart_cpu.h>
52 #define UART_TAG_BD 10
54 static struct uart_class *uart_classes[] = {
64 uart_parse_addr(const char **p)
66 return (strtoul(*p, (char**)(uintptr_t)p, 0));
69 static struct uart_class *
70 uart_parse_class(struct uart_class *class, const char **p)
72 struct uart_class *uc;
77 for (i = 0; i < nitems(uart_classes); i++) {
79 nm = uart_getname(uc);
80 if (nm == NULL || *nm == '\0')
83 if (strncmp(nm, *p, len) == 0) {
92 uart_parse_long(const char **p)
94 return (strtol(*p, (char**)(uintptr_t)p, 0));
98 uart_parse_parity(const char **p)
100 if (!strncmp(*p, "even", 4)) {
102 return UART_PARITY_EVEN;
104 if (!strncmp(*p, "mark", 4)) {
106 return UART_PARITY_MARK;
108 if (!strncmp(*p, "none", 4)) {
110 return UART_PARITY_NONE;
112 if (!strncmp(*p, "odd", 3)) {
114 return UART_PARITY_ODD;
116 if (!strncmp(*p, "space", 5)) {
118 return UART_PARITY_SPACE;
124 uart_parse_tag(const char **p)
128 if ((*p)[0] == 'b' && (*p)[1] == 'd') {
132 if ((*p)[0] == 'b' && (*p)[1] == 'r') {
136 if ((*p)[0] == 'c' && (*p)[1] == 'h') {
140 if ((*p)[0] == 'd' && (*p)[1] == 'b') {
144 if ((*p)[0] == 'd' && (*p)[1] == 't') {
148 if ((*p)[0] == 'i' && (*p)[1] == 'o') {
152 if ((*p)[0] == 'm' && (*p)[1] == 'm') {
156 if ((*p)[0] == 'p' && (*p)[1] == 'a') {
160 if ((*p)[0] == 'r' && (*p)[1] == 's') {
164 if ((*p)[0] == 's' && (*p)[1] == 'b') {
168 if ((*p)[0] == 'x' && (*p)[1] == 'o') {
183 * Parse a device specification. The specification is a list of attributes
184 * separated by commas. Each attribute is a tag-value pair with the tag and
185 * value separated by a colon. Supported tags are:
192 * io = I/O port address
193 * mm = Memory mapped I/O address
195 * rs = Register shift
197 * xo = Device clock (xtal oscillator)
199 * The io and mm tags are mutually exclusive.
203 uart_getenv(int devtype, struct uart_devinfo *di, struct uart_class *class)
207 bus_addr_t addr = ~0U;
211 * All uart_class references are weak. Make sure the default
212 * device class has been compiled-in.
218 * Check the environment variables "hw.uart.console" and
219 * "hw.uart.dbgport". These variables, when present, specify
220 * which UART port is to be used as serial console or debug
224 case UART_DEV_CONSOLE:
225 cp = kern_getenv("hw.uart.console");
227 case UART_DEV_DBGPORT:
228 cp = kern_getenv("hw.uart.dbgport");
245 di->parity = UART_PARITY_NONE;
247 /* Parse the attributes. */
250 switch (uart_parse_tag(&spec)) {
252 di->bas.busy_detect = uart_parse_long(&spec);
255 di->baudrate = uart_parse_long(&spec);
258 di->bas.chan = uart_parse_long(&spec);
261 di->databits = uart_parse_long(&spec);
264 class = uart_parse_class(class, &spec);
267 di->bas.bst = uart_bus_space_io;
268 addr = uart_parse_addr(&spec);
271 di->bas.bst = uart_bus_space_mem;
272 addr = uart_parse_addr(&spec);
275 di->parity = uart_parse_parity(&spec);
278 di->bas.regshft = uart_parse_long(&spec);
281 di->stopbits = uart_parse_long(&spec);
284 di->bas.rclk = uart_parse_long(&spec);
301 * If we still have an invalid address, the specification must be
302 * missing an I/O port or memory address. We don't like that.
308 * Accept only the well-known baudrates. Any invalid baudrate
309 * is silently replaced with a 0-valued baudrate. The 0 baudrate
310 * has special meaning. It means that we're not supposed to
311 * program the baudrate and simply communicate with whatever
312 * speed the hardware is currently programmed for.
314 if (di->baudrate >= 19200) {
315 if (di->baudrate % 19200)
317 } else if (di->baudrate >= 1200) {
318 if (di->baudrate % 1200)
320 } else if (di->baudrate > 0) {
321 if (di->baudrate % 75)
326 /* Set the ops and create a bus space handle. */
327 di->ops = uart_getops(class);
328 error = bus_space_map(di->bas.bst, addr, uart_getrange(class), 0,