1/*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2003, 2004 Marcel Moolenaar
5 * Copyright (c) 2004 - 2006 Marius Strobl <marius@FreeBSD.org>
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
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.
17 *
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.
28 */
29
30#include <sys/cdefs.h>
31__FBSDID("$FreeBSD$");
32
33#include <sys/param.h>
34#include <sys/systm.h>
35
36#include <machine/bus.h>
37#include <machine/bus_private.h>
38
39#include <dev/ofw/openfirm.h>
40#include <machine/ofw_machdep.h>
41
42#include <dev/uart/uart.h>
43#include <dev/uart/uart_cpu.h>
44
45bus_space_tag_t uart_bus_space_io;
46bus_space_tag_t uart_bus_space_mem;
47
48static struct bus_space_tag bst_store[3];
49
50/*
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'
63 */
64static int
65uart_cpu_channel(char *dev)
66{
67	char alias[64];
68	phandle_t aliases;
69	int len;
70	const char *p;
71
72	strcpy(alias, dev);
73	if ((aliases = OF_finddevice("/aliases")) != -1)
74		(void)OF_getprop(aliases, dev, alias, sizeof(alias));
75	len = strlen(alias);
76	if ((p = strrchr(alias, ':')) == NULL)
77		return (0);
78	p++;
79	if (p - alias == len - 1 && (*p == 'a' || *p == 'b'))
80		return (*p - 'a' + 1);
81	if (strcmp(p, "ssp") == 0)
82		return (1);
83	return (0);
84}
85
86int
87uart_cpu_eqres(struct uart_bas *b1, struct uart_bas *b2)
88{
89
90	return ((b1->bsh == b2->bsh) ? 1 : 0);
91}
92
93/*
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).
104 */
105static phandle_t
106uart_cpu_getdev_console(phandle_t options, char *dev, size_t devsz)
107{
108	char buf[sizeof("serial")];
109	ihandle_t inst;
110	phandle_t chosen, input, output;
111
112	if (OF_getprop(options, "input-device", dev, devsz) == -1)
113		return (-1);
114	input = OF_finddevice(dev);
115	if (OF_getprop(options, "output-device", dev, devsz) == -1)
116		return (-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)
121			return (-1);
122		if (OF_getprop(chosen, "stdin", &inst, sizeof(inst)) == -1)
123			return (-1);
124		if ((input = OF_instance_to_package(inst)) == -1)
125			return (-1);
126		if (OF_getprop(chosen, "stdout", &inst, sizeof(inst)) == -1)
127			return (-1);
128		if ((output = OF_instance_to_package(inst)) == -1)
129			return (-1);
130		snprintf(dev, devsz, "ttya");
131	}
132	if (input != output)
133		return (-1);
134	if (OF_getprop(input, "device_type", buf, sizeof(buf)) == -1)
135		return (-1);
136	if (strcmp(buf, "serial") != 0)
137		return (-1);
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)
142			return (-1);
143		if (OF_getprop(chosen, "iosram", &input, sizeof(input)) == -1)
144			return (-1);
145	}
146	return (input);
147}
148
149/*
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
154 * the OF.
155 */
156static phandle_t
157uart_cpu_getdev_dbgport(char *dev, size_t devsz)
158{
159	char buf[sizeof("serial")];
160	phandle_t input;
161
162	if (!getenv_string("hw.uart.dbgport", dev, devsz))
163		return (-1);
164	if ((input = OF_finddevice(dev)) == -1)
165		return (-1);
166	if (OF_getprop(input, "device_type", buf, sizeof(buf)) == -1)
167		return (-1);
168	if (strcmp(buf, "serial") != 0)
169		return (-1);
170	return (input);
171}
172
173/*
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.
179 */
180static phandle_t
181uart_cpu_getdev_keyboard(char *dev, size_t devsz)
182{
183	char buf[sizeof("serial")];
184	phandle_t input;
185
186	if ((input = OF_finddevice("keyboard")) == -1)
187		return (-1);
188	if (OF_getprop(input, "device_type", buf, sizeof(buf)) == -1)
189		return (-1);
190	if (strcmp(buf, "serial") != 0)
191		return (-1);
192	if (OF_getprop(input, "name", dev, devsz) == -1)
193		return (-1);
194	/*
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.
197	 */
198	if (strcmp(dev, "su") && strcmp(dev, "su_pnp") && strcmp(dev, "zs"))
199		return (-1);
200	return (input);
201}
202
203int
204uart_cpu_getdev(int devtype, struct uart_devinfo *di)
205{
206	char buf[32], compat[32], dev[64];
207	struct uart_class *class;
208	phandle_t input, options;
209	bus_addr_t addr;
210	int baud, bits, error, range, space, stop;
211	char flag, par;
212
213	if ((options = OF_finddevice("/options")) == -1)
214		return (ENXIO);
215	switch (devtype) {
216	case UART_DEV_CONSOLE:
217		input = uart_cpu_getdev_console(options, dev, sizeof(dev));
218		break;
219	case UART_DEV_DBGPORT:
220		input = uart_cpu_getdev_dbgport(dev, sizeof(dev));
221		break;
222	case UART_DEV_KEYBOARD:
223		input = uart_cpu_getdev_keyboard(dev, sizeof(dev));
224		break;
225	default:
226		input = -1;
227		break;
228	}
229	if (input == -1)
230		return (ENXIO);
231	error = OF_decode_addr(input, 0, &space, &addr);
232	if (error)
233		return (error);
234
235	/* Get the device class. */
236	if (OF_getprop(input, "name", buf, sizeof(buf)) == -1)
237		return (ENXIO);
238	if (OF_getprop(input, "compatible", compat, sizeof(compat)) == -1)
239		compat[0] = '\0';
240	di->bas.regshft = 0;
241	di->bas.rclk = 0;
242	class = NULL;
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)
248			return (ENXIO);
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) {
253			/*
254			 * There's no way to determine from OF which
255			 * channel has the keyboard. Should always be
256			 * on channel 1 however.
257			 */
258			if (devtype == UART_DEV_KEYBOARD)
259				di->bas.chan = 1;
260			else
261				return (ENXIO);
262		}
263		di->bas.regshft = 1;
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;
271		di->bas.chan = 0;
272	} else if (!strcmp(compat, "sgsbbc")) {
273		class = &uart_sbbc_class;
274		di->bas.chan = 0;
275	}
276	if (class == NULL)
277		return (ENXIO);
278
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);
283
284	/* Get the line settings. */
285	if (devtype == UART_DEV_KEYBOARD)
286		di->baudrate = 1200;
287	else if (!strcmp(compat, "rsc-console"))
288		di->baudrate = 115200;
289	else
290		di->baudrate = 9600;
291	di->databits = 8;
292	di->stopbits = 1;
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)
297		return (0);
298	if (sscanf(buf, "%d,%d,%c,%d,%c", &baud, &bits, &par, &stop, &flag)
299	    != 5)
300		return (0);
301	di->baudrate = baud;
302	di->databits = bits;
303	di->stopbits = stop;
304	di->parity = (par == 'n') ? UART_PARITY_NONE :
305	    (par == 'o') ? UART_PARITY_ODD : UART_PARITY_EVEN;
306	return (0);
307}
308