uart_cpu_sparc64.c revision 206451
1296341Sdelphij/*-
2238405Sjkim * Copyright (c) 2003, 2004 Marcel Moolenaar
3238405Sjkim * Copyright (c) 2004 - 2006 Marius Strobl <marius@FreeBSD.org>
4238405Sjkim * All rights reserved.
5238405Sjkim *
6238405Sjkim * Redistribution and use in source and binary forms, with or without
7238405Sjkim * modification, are permitted provided that the following conditions
8238405Sjkim * are met:
9238405Sjkim *
10238405Sjkim * 1. Redistributions of source code must retain the above copyright
11238405Sjkim *    notice, this list of conditions and the following disclaimer.
12238405Sjkim * 2. Redistributions in binary form must reproduce the above copyright
13238405Sjkim *    notice, this list of conditions and the following disclaimer in the
14238405Sjkim *    documentation and/or other materials provided with the distribution.
15238405Sjkim *
16238405Sjkim * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17238405Sjkim * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18238405Sjkim * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19238405Sjkim * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20238405Sjkim * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21238405Sjkim * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22238405Sjkim * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23238405Sjkim * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24238405Sjkim * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25238405Sjkim * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26238405Sjkim */
27238405Sjkim
28238405Sjkim#include <sys/cdefs.h>
29238405Sjkim__FBSDID("$FreeBSD: head/sys/dev/uart/uart_cpu_sparc64.c 206451 2010-04-10 11:52:12Z marius $");
30238405Sjkim
31238405Sjkim#include <sys/param.h>
32238405Sjkim#include <sys/systm.h>
33238405Sjkim
34238405Sjkim#include <machine/bus.h>
35238405Sjkim#include <machine/bus_private.h>
36238405Sjkim
37238405Sjkim#include <dev/ofw/openfirm.h>
38238405Sjkim#include <machine/ofw_machdep.h>
39238405Sjkim
40238405Sjkim#include <dev/uart/uart.h>
41279264Sdelphij#include <dev/uart/uart_cpu.h>
42279264Sdelphij
43238405Sjkimbus_space_tag_t uart_bus_space_io;
44238405Sjkimbus_space_tag_t uart_bus_space_mem;
45238405Sjkim
46238405Sjkimstatic struct bus_space_tag bst_store[3];
47238405Sjkim
48238405Sjkim/*
49238405Sjkim * Determine which channel of a SCC a device referenced by a full device
50238405Sjkim * path or as an alias is (in the latter case we try to look up the device
51238405Sjkim * path via the /aliases node).
52238405Sjkim * Only the device paths of devices which are used for TTYs really allow
53279264Sdelphij * to do this as they look like these (taken from /aliases nodes):
54279264Sdelphij * ttya:  '/central/fhc/zs@0,902000:a'
55279264Sdelphij * ttyc:  '/pci@1f,0/pci@1,1/ebus@1/se@14,400000:a'
56238405Sjkim * Additionally, for device paths of SCCs which are connected to a RSC
57279264Sdelphij * (Remote System Control) device we can hardcode the appropriate channel.
58279264Sdelphij * Such device paths look like these:
59279264Sdelphij * rsc:   '/pci@1f,4000/ebus@1/se@14,200000:ssp'
60279264Sdelphij * ttyc:  '/pci@1f,4000/ebus@1/se@14,200000:ssp'
61279264Sdelphij */
62279264Sdelphijstatic int
63238405Sjkimuart_cpu_channel(char *dev)
64279264Sdelphij{
65279264Sdelphij	char alias[64];
66279264Sdelphij	phandle_t aliases;
67279264Sdelphij	int len;
68279264Sdelphij	const char *p;
69238405Sjkim
70279264Sdelphij	strcpy(alias, dev);
71238405Sjkim	if ((aliases = OF_finddevice("/aliases")) != -1)
72238405Sjkim		(void)OF_getprop(aliases, dev, alias, sizeof(alias));
73238405Sjkim	len = strlen(alias);
74238405Sjkim	if ((p = rindex(alias, ':')) == NULL)
75238405Sjkim		return (0);
76238405Sjkim	p++;
77238405Sjkim	if (p - alias == len - 1 && (*p == 'a' || *p == 'b'))
78238405Sjkim		return (*p - 'a' + 1);
79238405Sjkim	if (strcmp(p, "ssp") == 0)
80238405Sjkim		return (1);
81238405Sjkim	return (0);
82238405Sjkim}
83238405Sjkim
84238405Sjkimint
85238405Sjkimuart_cpu_eqres(struct uart_bas *b1, struct uart_bas *b2)
86238405Sjkim{
87238405Sjkim
88238405Sjkim	return ((b1->bsh == b2->bsh) ? 1 : 0);
89238405Sjkim}
90238405Sjkim
91238405Sjkim/*
92238405Sjkim * Get the package handle of the UART that is selected as the console, if
93238405Sjkim * the console is an UART of course. Note that we enforce that both input
94238405Sjkim * and output are selected.
95238405Sjkim * Note that the currently active console (i.e. /chosen/stdout and
96238405Sjkim * /chosen/stdin) may not be the same as the device selected in the
97238405Sjkim * environment (ie /options/output-device and /options/input-device) because
98238405Sjkim * keyboard and screen were selected but the keyboard was unplugged or the
99238405Sjkim * user has changed the environment. In the latter case I would assume that
100238405Sjkim * the user expects that FreeBSD uses the new console setting.
101238405Sjkim * For weirder configurations, use ofw_console(4).
102238405Sjkim */
103238405Sjkimstatic phandle_t
104238405Sjkimuart_cpu_getdev_console(phandle_t options, char *dev, size_t devsz)
105238405Sjkim{
106238405Sjkim	char buf[sizeof("serial")];
107238405Sjkim	ihandle_t inst;
108238405Sjkim	phandle_t chosen, input, output;
109238405Sjkim
110238405Sjkim	if (OF_getprop(options, "input-device", dev, devsz) == -1)
111238405Sjkim		return (-1);
112238405Sjkim	input = OF_finddevice(dev);
113238405Sjkim	if (OF_getprop(options, "output-device", dev, devsz) == -1)
114238405Sjkim		return (-1);
115238405Sjkim	output = OF_finddevice(dev);
116238405Sjkim	if (input == -1 || output == -1 ||
117238405Sjkim	    OF_getproplen(input, "keyboard") >= 0) {
118238405Sjkim		if ((chosen = OF_finddevice("/chosen")) == -1)
119238405Sjkim			return (-1);
120238405Sjkim		if (OF_getprop(chosen, "stdin", &inst, sizeof(inst)) == -1)
121238405Sjkim			return (-1);
122238405Sjkim		if ((input = OF_instance_to_package(inst)) == -1)
123238405Sjkim			return (-1);
124238405Sjkim		if (OF_getprop(chosen, "stdout", &inst, sizeof(inst)) == -1)
125238405Sjkim			return (-1);
126238405Sjkim		if ((output = OF_instance_to_package(inst)) == -1)
127238405Sjkim			return (-1);
128238405Sjkim		snprintf(dev, devsz, "ttya");
129238405Sjkim	}
130238405Sjkim	if (input != output)
131238405Sjkim		return (-1);
132238405Sjkim	if (OF_getprop(input, "device_type", buf, sizeof(buf)) == -1)
133238405Sjkim		return (-1);
134238405Sjkim	if (strcmp(buf, "serial") != 0)
135238405Sjkim		return (-1);
136296341Sdelphij	/* For a Serengeti console device point to the bootbus controller. */
137238405Sjkim	if (OF_getprop(input, "name", buf, sizeof(buf)) > 0 &&
138238405Sjkim	    !strcmp(buf, "sgcn")) {
139238405Sjkim		if ((chosen = OF_finddevice("/chosen")) == -1)
140238405Sjkim			return (-1);
141238405Sjkim		if (OF_getprop(chosen, "iosram", &input, sizeof(input)) == -1)
142238405Sjkim			return (-1);
143238405Sjkim	}
144238405Sjkim	return (input);
145238405Sjkim}
146238405Sjkim
147238405Sjkim/*
148238405Sjkim * Get the package handle of the UART that's selected as the debug port.
149238405Sjkim * Since there's no place for this in the OF, we use the kernel environment
150238405Sjkim * variable "hw.uart.dbgport". Note however that the variable is not a
151238405Sjkim * list of attributes. It's single device name or alias, as known by
152238405Sjkim * the OF.
153238405Sjkim */
154238405Sjkimstatic phandle_t
155238405Sjkimuart_cpu_getdev_dbgport(char *dev, size_t devsz)
156238405Sjkim{
157238405Sjkim	char buf[sizeof("serial")];
158238405Sjkim	phandle_t input;
159238405Sjkim
160279264Sdelphij	if (!getenv_string("hw.uart.dbgport", dev, devsz))
161238405Sjkim		return (-1);
162238405Sjkim	if ((input = OF_finddevice(dev)) == -1)
163238405Sjkim		return (-1);
164238405Sjkim	if (OF_getprop(input, "device_type", buf, sizeof(buf)) == -1)
165238405Sjkim		return (-1);
166238405Sjkim	if (strcmp(buf, "serial") != 0)
167238405Sjkim		return (-1);
168238405Sjkim	return (input);
169238405Sjkim}
170238405Sjkim
171238405Sjkim/*
172238405Sjkim * Get the package handle of the UART that is selected as the keyboard port,
173238405Sjkim * if it's actually used to connect the keyboard according to the OF. I.e.
174238405Sjkim * this will return the UART used to connect the keyboard regardless whether
175238405Sjkim * it's stdin or not, however not in case the user or the OF gave preference
176238405Sjkim * to e.g. a PS/2 keyboard by setting /aliases/keyboard accordingly.
177238405Sjkim */
178238405Sjkimstatic phandle_t
179238405Sjkimuart_cpu_getdev_keyboard(char *dev, size_t devsz)
180238405Sjkim{
181238405Sjkim	char buf[sizeof("serial")];
182238405Sjkim	phandle_t input;
183238405Sjkim
184238405Sjkim	if ((input = OF_finddevice("keyboard")) == -1)
185238405Sjkim		return (-1);
186238405Sjkim	if (OF_getprop(input, "device_type", buf, sizeof(buf)) == -1)
187238405Sjkim		return (-1);
188238405Sjkim	if (strcmp(buf, "serial") != 0)
189238405Sjkim		return (-1);
190238405Sjkim	if (OF_getprop(input, "name", dev, devsz) == -1)
191238405Sjkim		return (-1);
192238405Sjkim	/*
193238405Sjkim	 * So far this also matched PS/2 keyboard nodes so make sure it's
194238405Sjkim	 * one of the SCCs/UARTs known to be used to connect keyboards.
195279264Sdelphij	 */
196279264Sdelphij	if (strcmp(dev, "su") && strcmp(dev, "su_pnp") && strcmp(dev, "zs"))
197279264Sdelphij		return (-1);
198238405Sjkim	return (input);
199238405Sjkim}
200238405Sjkim
201238405Sjkimint
202238405Sjkimuart_cpu_getdev(int devtype, struct uart_devinfo *di)
203238405Sjkim{
204238405Sjkim	char buf[32], compat[32], dev[64];
205238405Sjkim	struct uart_class *class;
206238405Sjkim	phandle_t input, options;
207238405Sjkim	bus_addr_t addr;
208238405Sjkim	int baud, bits, error, range, space, stop;
209238405Sjkim	char flag, par;
210238405Sjkim
211238405Sjkim	if ((options = OF_finddevice("/options")) == -1)
212238405Sjkim		return (ENXIO);
213238405Sjkim	switch (devtype) {
214238405Sjkim	case UART_DEV_CONSOLE:
215238405Sjkim		input = uart_cpu_getdev_console(options, dev, sizeof(dev));
216238405Sjkim		break;
217238405Sjkim	case UART_DEV_DBGPORT:
218238405Sjkim		input = uart_cpu_getdev_dbgport(dev, sizeof(dev));
219238405Sjkim		break;
220238405Sjkim	case UART_DEV_KEYBOARD:
221238405Sjkim		input = uart_cpu_getdev_keyboard(dev, sizeof(dev));
222279264Sdelphij		break;
223238405Sjkim	default:
224238405Sjkim		input = -1;
225238405Sjkim		break;
226238405Sjkim	}
227238405Sjkim	if (input == -1)
228238405Sjkim		return (ENXIO);
229238405Sjkim	error = OF_decode_addr(input, 0, &space, &addr);
230238405Sjkim	if (error)
231238405Sjkim		return (error);
232238405Sjkim
233238405Sjkim	/* Get the device class. */
234238405Sjkim	if (OF_getprop(input, "name", buf, sizeof(buf)) == -1)
235238405Sjkim		return (ENXIO);
236238405Sjkim	if (OF_getprop(input, "compatible", compat, sizeof(compat)) == -1)
237238405Sjkim		compat[0] = '\0';
238238405Sjkim	di->bas.regshft = 0;
239238405Sjkim	di->bas.rclk = 0;
240238405Sjkim	class = NULL;
241238405Sjkim	if (!strcmp(buf, "se") || !strcmp(compat, "sab82532")) {
242238405Sjkim		class = &uart_sab82532_class;
243238405Sjkim		/* SAB82532 are only known to be used for TTYs. */
244238405Sjkim		if ((di->bas.chan = uart_cpu_channel(dev)) == 0)
245238405Sjkim			return (ENXIO);
246238405Sjkim		addr += uart_getrange(class) * (di->bas.chan - 1);
247238405Sjkim	} else if (!strcmp(buf, "zs")) {
248238405Sjkim		class = &uart_z8530_class;
249238405Sjkim		if ((di->bas.chan = uart_cpu_channel(dev)) == 0) {
250238405Sjkim			/*
251238405Sjkim			 * There's no way to determine from OF which
252238405Sjkim			 * channel has the keyboard. Should always be
253238405Sjkim			 * on channel 1 however.
254			 */
255			if (devtype == UART_DEV_KEYBOARD)
256				di->bas.chan = 1;
257			else
258				return (ENXIO);
259		}
260		di->bas.regshft = 1;
261		range = uart_getrange(class) << di->bas.regshft;
262		addr += range - range * (di->bas.chan - 1);
263	} else if (!strcmp(buf, "lom-console") || !strcmp(buf, "su") ||
264	    !strcmp(buf, "su_pnp") || !strcmp(compat, "rsc-console") ||
265	    !strcmp(compat, "su") || !strcmp(compat, "su16550") ||
266	    !strcmp(compat, "su16552")) {
267		class = &uart_ns8250_class;
268		di->bas.chan = 0;
269	} else if (!strcmp(compat, "sgsbbc")) {
270		class = &uart_sbbc_class;
271		di->bas.chan = 0;
272	}
273	if (class == NULL)
274		return (ENXIO);
275
276	/* Fill in the device info. */
277	di->ops = uart_getops(class);
278	di->bas.bst = &bst_store[devtype];
279	di->bas.bsh = sparc64_fake_bustag(space, addr, di->bas.bst);
280
281	/* Get the line settings. */
282	if (devtype == UART_DEV_KEYBOARD)
283		di->baudrate = 1200;
284	else if (!strcmp(compat, "rsc-console"))
285		di->baudrate = 115200;
286	else
287		di->baudrate = 9600;
288	di->databits = 8;
289	di->stopbits = 1;
290	di->parity = UART_PARITY_NONE;
291	snprintf(buf, sizeof(buf), "%s-mode", dev);
292	if (OF_getprop(options, buf, buf, sizeof(buf)) == -1 &&
293	    OF_getprop(input, "ssp-console-modes", buf, sizeof(buf)) == -1)
294		return (0);
295	if (sscanf(buf, "%d,%d,%c,%d,%c", &baud, &bits, &par, &stop, &flag)
296	    != 5)
297		return (0);
298	di->baudrate = baud;
299	di->databits = bits;
300	di->stopbits = stop;
301	di->parity = (par == 'n') ? UART_PARITY_NONE :
302	    (par == 'o') ? UART_PARITY_ODD : UART_PARITY_EVEN;
303	return (0);
304}
305