2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2003, 2004 Marcel Moolenaar
5 * Copyright (c) 2004 - 2006 Marius Strobl <marius@FreeBSD.org>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
34 #include <sys/systm.h>
36 #include <machine/bus.h>
37 #include <machine/bus_private.h>
39 #include <dev/ofw/openfirm.h>
40 #include <machine/ofw_machdep.h>
42 #include <dev/uart/uart.h>
43 #include <dev/uart/uart_cpu.h>
45 bus_space_tag_t uart_bus_space_io;
46 bus_space_tag_t uart_bus_space_mem;
48 static struct bus_space_tag bst_store[3];
51 * Determine which channel of a SCC a device referenced by a full device
52 * path or as an alias is (in the latter case we try to look up the device
53 * path via the /aliases node).
54 * Only the device paths of devices which are used for TTYs really allow
55 * to do this as they look like these (taken from /aliases nodes):
56 * ttya: '/central/fhc/zs@0,902000:a'
57 * ttyc: '/pci@1f,0/pci@1,1/ebus@1/se@14,400000:a'
58 * Additionally, for device paths of SCCs which are connected to a RSC
59 * (Remote System Control) device we can hardcode the appropriate channel.
60 * Such device paths look like these:
61 * rsc: '/pci@1f,4000/ebus@1/se@14,200000:ssp'
62 * ttyc: '/pci@1f,4000/ebus@1/se@14,200000:ssp'
65 uart_cpu_channel(char *dev)
73 if ((aliases = OF_finddevice("/aliases")) != -1)
74 (void)OF_getprop(aliases, dev, alias, sizeof(alias));
76 if ((p = strrchr(alias, ':')) == NULL)
79 if (p - alias == len - 1 && (*p == 'a' || *p == 'b'))
80 return (*p - 'a' + 1);
81 if (strcmp(p, "ssp") == 0)
87 uart_cpu_eqres(struct uart_bas *b1, struct uart_bas *b2)
90 return ((b1->bsh == b2->bsh) ? 1 : 0);
94 * Get the package handle of the UART that is selected as the console, if
95 * the console is an UART of course. Note that we enforce that both input
96 * and output are selected.
97 * Note that the currently active console (i.e. /chosen/stdout and
98 * /chosen/stdin) may not be the same as the device selected in the
99 * environment (ie /options/output-device and /options/input-device) because
100 * keyboard and screen were selected but the keyboard was unplugged or the
101 * user has changed the environment. In the latter case I would assume that
102 * the user expects that FreeBSD uses the new console setting.
103 * For weirder configurations, use ofw_console(4).
106 uart_cpu_getdev_console(phandle_t options, char *dev, size_t devsz)
108 char buf[sizeof("serial")];
110 phandle_t chosen, input, output;
112 if (OF_getprop(options, "input-device", dev, devsz) == -1)
114 input = OF_finddevice(dev);
115 if (OF_getprop(options, "output-device", dev, devsz) == -1)
117 output = OF_finddevice(dev);
118 if (input == -1 || output == -1 ||
119 OF_getproplen(input, "keyboard") >= 0) {
120 if ((chosen = OF_finddevice("/chosen")) == -1)
122 if (OF_getprop(chosen, "stdin", &inst, sizeof(inst)) == -1)
124 if ((input = OF_instance_to_package(inst)) == -1)
126 if (OF_getprop(chosen, "stdout", &inst, sizeof(inst)) == -1)
128 if ((output = OF_instance_to_package(inst)) == -1)
130 snprintf(dev, devsz, "ttya");
134 if (OF_getprop(input, "device_type", buf, sizeof(buf)) == -1)
136 if (strcmp(buf, "serial") != 0)
138 /* For a Serengeti console device point to the bootbus controller. */
139 if (OF_getprop(input, "name", buf, sizeof(buf)) > 0 &&
140 !strcmp(buf, "sgcn")) {
141 if ((chosen = OF_finddevice("/chosen")) == -1)
143 if (OF_getprop(chosen, "iosram", &input, sizeof(input)) == -1)
150 * Get the package handle of the UART that's selected as the debug port.
151 * Since there's no place for this in the OF, we use the kernel environment
152 * variable "hw.uart.dbgport". Note however that the variable is not a
153 * list of attributes. It's single device name or alias, as known by
157 uart_cpu_getdev_dbgport(char *dev, size_t devsz)
159 char buf[sizeof("serial")];
162 if (!getenv_string("hw.uart.dbgport", dev, devsz))
164 if ((input = OF_finddevice(dev)) == -1)
166 if (OF_getprop(input, "device_type", buf, sizeof(buf)) == -1)
168 if (strcmp(buf, "serial") != 0)
174 * Get the package handle of the UART that is selected as the keyboard port,
175 * if it's actually used to connect the keyboard according to the OF. I.e.
176 * this will return the UART used to connect the keyboard regardless whether
177 * it's stdin or not, however not in case the user or the OF gave preference
178 * to e.g. a PS/2 keyboard by setting /aliases/keyboard accordingly.
181 uart_cpu_getdev_keyboard(char *dev, size_t devsz)
183 char buf[sizeof("serial")];
186 if ((input = OF_finddevice("keyboard")) == -1)
188 if (OF_getprop(input, "device_type", buf, sizeof(buf)) == -1)
190 if (strcmp(buf, "serial") != 0)
192 if (OF_getprop(input, "name", dev, devsz) == -1)
195 * So far this also matched PS/2 keyboard nodes so make sure it's
196 * one of the SCCs/UARTs known to be used to connect keyboards.
198 if (strcmp(dev, "su") && strcmp(dev, "su_pnp") && strcmp(dev, "zs"))
204 uart_cpu_getdev(int devtype, struct uart_devinfo *di)
206 char buf[32], compat[32], dev[64];
207 struct uart_class *class;
208 phandle_t input, options;
210 int baud, bits, error, range, space, stop;
213 if ((options = OF_finddevice("/options")) == -1)
216 case UART_DEV_CONSOLE:
217 input = uart_cpu_getdev_console(options, dev, sizeof(dev));
219 case UART_DEV_DBGPORT:
220 input = uart_cpu_getdev_dbgport(dev, sizeof(dev));
222 case UART_DEV_KEYBOARD:
223 input = uart_cpu_getdev_keyboard(dev, sizeof(dev));
231 error = OF_decode_addr(input, 0, &space, &addr);
235 /* Get the device class. */
236 if (OF_getprop(input, "name", buf, sizeof(buf)) == -1)
238 if (OF_getprop(input, "compatible", compat, sizeof(compat)) == -1)
243 if (!strcmp(buf, "se") || !strcmp(buf, "FJSV,se") ||
244 !strcmp(compat, "sab82532")) {
245 class = &uart_sab82532_class;
246 /* SAB82532 are only known to be used for TTYs. */
247 if ((di->bas.chan = uart_cpu_channel(dev)) == 0)
249 addr += uart_getrange(class) * (di->bas.chan - 1);
250 } else if (!strcmp(buf, "zs")) {
251 class = &uart_z8530_class;
252 if ((di->bas.chan = uart_cpu_channel(dev)) == 0) {
254 * There's no way to determine from OF which
255 * channel has the keyboard. Should always be
256 * on channel 1 however.
258 if (devtype == UART_DEV_KEYBOARD)
264 range = uart_getrange(class) << di->bas.regshft;
265 addr += range - range * (di->bas.chan - 1);
266 } else if (!strcmp(buf, "lom-console") || !strcmp(buf, "su") ||
267 !strcmp(buf, "su_pnp") || !strcmp(compat, "rsc-console") ||
268 !strcmp(compat, "su") || !strcmp(compat, "su16550") ||
269 !strcmp(compat, "su16552")) {
270 class = &uart_ns8250_class;
272 } else if (!strcmp(compat, "sgsbbc")) {
273 class = &uart_sbbc_class;
279 /* Fill in the device info. */
280 di->ops = uart_getops(class);
281 di->bas.bst = &bst_store[devtype];
282 di->bas.bsh = sparc64_fake_bustag(space, addr, di->bas.bst);
284 /* Get the line settings. */
285 if (devtype == UART_DEV_KEYBOARD)
287 else if (!strcmp(compat, "rsc-console"))
288 di->baudrate = 115200;
293 di->parity = UART_PARITY_NONE;
294 snprintf(buf, sizeof(buf), "%s-mode", dev);
295 if (OF_getprop(options, buf, buf, sizeof(buf)) == -1 &&
296 OF_getprop(input, "ssp-console-modes", buf, sizeof(buf)) == -1)
298 if (sscanf(buf, "%d,%d,%c,%d,%c", &baud, &bits, &par, &stop, &flag)
304 di->parity = (par == 'n') ? UART_PARITY_NONE :
305 (par == 'o') ? UART_PARITY_ODD : UART_PARITY_EVEN;