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[] = {
60 static size_t uart_nclasses = sizeof(uart_classes) / sizeof(uart_classes[0]);
63 uart_parse_addr(const char **p)
65 return (strtoul(*p, (char**)(uintptr_t)p, 0));
68 static struct uart_class *
69 uart_parse_class(struct uart_class *class, const char **p)
71 struct uart_class *uc;
76 for (i = 0; i < uart_nclasses; i++) {
78 nm = uart_getname(uc);
79 if (nm == NULL || *nm == '\0')
82 if (strncmp(nm, *p, len) == 0) {
91 uart_parse_long(const char **p)
93 return (strtol(*p, (char**)(uintptr_t)p, 0));
97 uart_parse_parity(const char **p)
99 if (!strncmp(*p, "even", 4)) {
101 return UART_PARITY_EVEN;
103 if (!strncmp(*p, "mark", 4)) {
105 return UART_PARITY_MARK;
107 if (!strncmp(*p, "none", 4)) {
109 return UART_PARITY_NONE;
111 if (!strncmp(*p, "odd", 3)) {
113 return UART_PARITY_ODD;
115 if (!strncmp(*p, "space", 5)) {
117 return UART_PARITY_SPACE;
123 uart_parse_tag(const char **p)
127 if ((*p)[0] == 'b' && (*p)[1] == 'r') {
131 if ((*p)[0] == 'c' && (*p)[1] == 'h') {
135 if ((*p)[0] == 'd' && (*p)[1] == 'b') {
139 if ((*p)[0] == 'd' && (*p)[1] == 't') {
143 if ((*p)[0] == 'i' && (*p)[1] == 'o') {
147 if ((*p)[0] == 'm' && (*p)[1] == 'm') {
151 if ((*p)[0] == 'p' && (*p)[1] == 'a') {
155 if ((*p)[0] == 'r' && (*p)[1] == 's') {
159 if ((*p)[0] == 's' && (*p)[1] == 'b') {
163 if ((*p)[0] == 'x' && (*p)[1] == 'o') {
178 * Parse a device specification. The specification is a list of attributes
179 * separated by commas. Each attribute is a tag-value pair with the tag and
180 * value separated by a colon. Supported tags are:
186 * io = I/O port address
187 * mm = Memory mapped I/O address
189 * rs = Register shift
191 * xo = Device clock (xtal oscillator)
193 * The io and mm tags are mutually exclusive.
197 uart_getenv(int devtype, struct uart_devinfo *di, struct uart_class *class)
200 bus_addr_t addr = ~0U;
204 * All uart_class references are weak. Make sure the default
205 * device class has been compiled-in.
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
216 if (devtype == UART_DEV_CONSOLE)
217 spec = getenv("hw.uart.console");
218 else if (devtype == UART_DEV_DBGPORT)
219 spec = getenv("hw.uart.dbgport");
232 di->parity = UART_PARITY_NONE;
234 /* Parse the attributes. */
236 switch (uart_parse_tag(&spec)) {
238 di->baudrate = uart_parse_long(&spec);
241 di->bas.chan = uart_parse_long(&spec);
244 di->databits = uart_parse_long(&spec);
247 class = uart_parse_class(class, &spec);
250 di->bas.bst = uart_bus_space_io;
251 addr = uart_parse_addr(&spec);
254 di->bas.bst = uart_bus_space_mem;
255 addr = uart_parse_addr(&spec);
258 di->parity = uart_parse_parity(&spec);
261 di->bas.regshft = uart_parse_long(&spec);
264 di->stopbits = uart_parse_long(&spec);
267 di->bas.rclk = uart_parse_long(&spec);
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.
287 * Accept only the well-known baudrates. Any invalid baudrate
288 * is silently replaced with a 0-valued baudrate. The 0 baudrate
289 * has special meaning. It means that we're not supposed to
290 * program the baudrate and simply communicate with whatever
291 * speed the hardware is currently programmed for.
293 if (di->baudrate >= 19200) {
294 if (di->baudrate % 19200)
296 } else if (di->baudrate >= 1200) {
297 if (di->baudrate % 1200)
299 } else if (di->baudrate > 0) {
300 if (di->baudrate % 75)
305 /* Set the ops and create a bus space handle. */
306 di->ops = uart_getops(class);
307 error = bus_space_map(di->bas.bst, addr, uart_getrange(class), 0,