2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2006 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>
37 #include <machine/bus.h>
38 #include <machine/ofw_machdep.h>
40 #include <dev/ofw/ofw_bus_subr.h>
41 #include <dev/ofw/openfirm.h>
42 #include <dev/uart/uart.h>
43 #include <dev/uart/uart_cpu.h>
45 bus_space_tag_t uart_bus_space_io = &bs_le_tag;
46 bus_space_tag_t uart_bus_space_mem = &bs_le_tag;
49 uart_cpu_eqres(struct uart_bas *b1, struct uart_bas *b2)
52 return ((pmap_kextract(b1->bsh) == pmap_kextract(b2->bsh)) ? 1 : 0);
56 ofw_get_uart_console(phandle_t opts, phandle_t *result, const char *inputdev,
57 const char *outputdev)
62 if (OF_getprop(opts, inputdev, buf, sizeof(buf)) == -1)
64 input = OF_finddevice(buf);
68 if (outputdev != NULL) {
69 if (OF_getprop(opts, outputdev, buf, sizeof(buf)) == -1)
71 if (OF_finddevice(buf) != input)
80 ofw_get_console_phandle_path(phandle_t node, phandle_t *result,
90 size = OF_getproplen(node, prop);
93 OF_getprop(node, prop, &field, sizeof(field));
95 /* This property might be either a ihandle or path. Hooray. */
98 if (field.buf[size - 1] == 0)
99 output = OF_finddevice(field.buf);
100 if (output == -1 && size == 4)
101 output = OF_instance_to_package(field.ref);
112 uart_cpu_getdev(int devtype, struct uart_devinfo *di)
115 struct uart_class *class;
116 phandle_t input, opts, chosen;
119 opts = OF_finddevice("/options");
120 chosen = OF_finddevice("/chosen");
122 case UART_DEV_CONSOLE:
124 if (chosen != -1 && error != 0)
125 error = ofw_get_uart_console(chosen, &input,
126 "stdout-path", NULL);
127 if (chosen != -1 && error != 0)
128 error = ofw_get_uart_console(chosen, &input,
129 "linux,stdout-path", NULL);
130 if (chosen != -1 && error != 0)
131 error = ofw_get_console_phandle_path(chosen, &input,
133 if (chosen != -1 && error != 0)
134 error = ofw_get_uart_console(chosen, &input,
136 if (chosen != -1 && error != 0)
137 error = ofw_get_console_phandle_path(chosen, &input,
139 if (opts != -1 && error != 0)
140 error = ofw_get_uart_console(opts, &input,
141 "input-device", "output-device");
142 if (opts != -1 && error != 0)
143 error = ofw_get_uart_console(opts, &input,
144 "input-device-1", "output-device-1");
146 input = OF_finddevice("serial0"); /* Last ditch */
154 case UART_DEV_DBGPORT:
155 if (!getenv_string("hw.uart.dbgport", buf, sizeof(buf)))
157 input = OF_finddevice(buf);
165 if (OF_getprop(input, "device_type", buf, sizeof(buf)) == -1)
167 if (strcmp(buf, "serial") != 0)
170 if (ofw_bus_node_is_compatible(input, "chrp,es")) {
171 class = &uart_z8530_class;
174 } else if (ofw_bus_node_is_compatible(input,"ns16550") ||
175 ofw_bus_node_is_compatible(input,"ns8250")) {
176 class = &uart_ns8250_class;
185 error = OF_decode_addr(input, 0, &di->bas.bst, &di->bas.bsh, NULL);
189 di->ops = uart_getops(class);
191 if (OF_getprop(input, "clock-frequency", &di->bas.rclk,
192 sizeof(di->bas.rclk)) == -1)
193 di->bas.rclk = 230400;
194 if (OF_getprop(input, "current-speed", &di->baudrate,
195 sizeof(di->baudrate)) == -1)
197 OF_getprop(input, "reg-shift", &di->bas.regshft,
198 sizeof(di->bas.regshft));
202 di->parity = UART_PARITY_NONE;