uart_cpu_sparc64.c revision 149111
1139749Simp/*-
2127215Smarcel * Copyright (c) 2003, 2004 Marcel Moolenaar
3119815Smarcel * All rights reserved.
4119815Smarcel *
5119815Smarcel * Redistribution and use in source and binary forms, with or without
6119815Smarcel * modification, are permitted provided that the following conditions
7119815Smarcel * are met:
8119815Smarcel *
9119815Smarcel * 1. Redistributions of source code must retain the above copyright
10119815Smarcel *    notice, this list of conditions and the following disclaimer.
11119815Smarcel * 2. Redistributions in binary form must reproduce the above copyright
12119815Smarcel *    notice, this list of conditions and the following disclaimer in the
13119815Smarcel *    documentation and/or other materials provided with the distribution.
14119815Smarcel *
15119815Smarcel * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16119815Smarcel * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17119815Smarcel * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18119815Smarcel * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19119815Smarcel * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20119815Smarcel * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21119815Smarcel * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22119815Smarcel * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23119815Smarcel * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24119815Smarcel * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25119815Smarcel */
26119815Smarcel
27119815Smarcel#include <sys/cdefs.h>
28119815Smarcel__FBSDID("$FreeBSD: head/sys/dev/uart/uart_cpu_sparc64.c 149111 2005-08-15 20:58:36Z marius $");
29119815Smarcel
30119815Smarcel#include <sys/param.h>
31119815Smarcel#include <sys/systm.h>
32119815Smarcel
33119815Smarcel#include <machine/bus.h>
34119815Smarcel#include <machine/bus_private.h>
35119815Smarcel
36119815Smarcel#include <dev/ofw/openfirm.h>
37119815Smarcel#include <machine/ofw_machdep.h>
38119815Smarcel
39119815Smarcel#include <dev/uart/uart.h>
40119815Smarcel#include <dev/uart/uart_cpu.h>
41119815Smarcel
42127215Smarcelbus_space_tag_t uart_bus_space_io;
43127215Smarcelbus_space_tag_t uart_bus_space_mem;
44127215Smarcel
45119815Smarcelstatic struct bus_space_tag bst_store[3];
46119815Smarcel
47138157Smarius/*
48138157Smarius * Determine which channel of a SCC a device referenced by an alias is.
49138157Smarius * The information present in the OF device tree only allows to do this
50138157Smarius * for "ttyX" aliases. If a device is a channel of a SCC its property
51138157Smarius * in the /aliases node looks like one of these:
52138157Smarius * ttya:  '/central/fhc/zs@0,902000:a'
53138157Smarius * ttyc:  '/pci@1f,0/pci@1,1/ebus@1/se@14,400000:a'
54138157Smarius */
55119815Smarcelstatic int
56119815Smarceluart_cpu_channel(char *dev)
57119815Smarcel{
58119815Smarcel	char alias[64];
59119815Smarcel	phandle_t aliases;
60119815Smarcel	int len;
61119815Smarcel
62119815Smarcel	strcpy(alias, dev);
63119815Smarcel	if ((aliases = OF_finddevice("/aliases")) != -1)
64119815Smarcel		OF_getprop(aliases, dev, alias, sizeof(alias));
65119815Smarcel	len = strlen(alias);
66119815Smarcel	if (len < 2 || alias[len - 2] != ':' || alias[len - 1] < 'a' ||
67119815Smarcel	    alias[len - 1] > 'b')
68119815Smarcel		return (0);
69120452Smarcel	return (alias[len - 1] - 'a' + 1);
70119815Smarcel}
71119815Smarcel
72119815Smarcelint
73119866Smarceluart_cpu_eqres(struct uart_bas *b1, struct uart_bas *b2)
74119866Smarcel{
75119866Smarcel
76119866Smarcel	return ((b1->bsh == b2->bsh) ? 1 : 0);
77119866Smarcel}
78119866Smarcel
79127741Smarcel/*
80138157Smarius * Get the package handle of the UART that is selected as the console, if
81149111Smarius * the console is an UART of course. Note that we enforce that both input
82149111Smarius * and output are selected.
83127741Smarcel * Note that the currently active console (i.e. /chosen/stdout and
84127741Smarcel * /chosen/stdin) may not be the same as the device selected in the
85127741Smarcel * environment (ie /options/output-device and /options/input-device) because
86127825Smarcel * keyboard and screen were selected but the keyboard was unplugged or the
87127825Smarcel * user has changed the environment. In the latter case I would assume that
88127825Smarcel * the user expects that FreeBSD uses the new console setting.
89127825Smarcel * For weirder configurations, use ofw_console(4).
90127741Smarcel */
91122466Sjakestatic phandle_t
92127741Smarceluart_cpu_getdev_console(phandle_t options, char *dev, size_t devsz)
93122466Sjake{
94149111Smarius	char buf[sizeof("serial")];
95149111Smarius	ihandle_t inst;
96149111Smarius	phandle_t chosen, input, output;
97122466Sjake
98127741Smarcel	if (OF_getprop(options, "input-device", dev, devsz) == -1)
99127741Smarcel		return (-1);
100149111Smarius	input = OF_finddevice(dev);
101149111Smarius	if (OF_getprop(options, "output-device", dev, devsz) == -1)
102127825Smarcel		return (-1);
103149111Smarius	output = OF_finddevice(dev);
104149111Smarius	if (input == -1 || output == -1 ||
105149111Smarius	    OF_getproplen(input, "keyboard") >= 0) {
106127825Smarcel		if ((chosen = OF_finddevice("/chosen")) == -1)
107127825Smarcel			return (-1);
108149111Smarius		if (OF_getprop(chosen, "stdin", &inst, sizeof(inst)) == -1)
109127825Smarcel			return (-1);
110149111Smarius		if ((input = OF_instance_to_package(inst)) == -1)
111127825Smarcel			return (-1);
112149111Smarius		if (OF_getprop(chosen, "stdout", &inst, sizeof(inst)) == -1)
113127825Smarcel			return (-1);
114149111Smarius		if ((output = OF_instance_to_package(inst)) == -1)
115127825Smarcel			return (-1);
116127825Smarcel		snprintf(dev, devsz, "ttya");
117143468Smarius	}
118149111Smarius	if (input != output)
119149111Smarius		return (-1);
120127741Smarcel	if (OF_getprop(input, "device_type", buf, sizeof(buf)) == -1)
121127741Smarcel		return (-1);
122127741Smarcel	if (strcmp(buf, "serial") != 0)
123127741Smarcel		return (-1);
124127741Smarcel	return (input);
125127741Smarcel}
126127741Smarcel
127127741Smarcel/*
128138157Smarius * Get the package handle of the UART that's selected as the debug port.
129138157Smarius * Since there's no place for this in the OF, we use the kernel environment
130127741Smarcel * variable "hw.uart.dbgport". Note however that the variable is not a
131127741Smarcel * list of attributes. It's single device name or alias, as known by
132127741Smarcel * the OF.
133127741Smarcel */
134127741Smarcelstatic phandle_t
135149111Smariusuart_cpu_getdev_dbgport(char *dev, size_t devsz)
136127741Smarcel{
137149111Smarius	char buf[sizeof("serial")];
138127741Smarcel	phandle_t input;
139127741Smarcel
140127741Smarcel	if (!getenv_string("hw.uart.dbgport", dev, devsz))
141127741Smarcel		return (-1);
142127741Smarcel	if ((input = OF_finddevice(dev)) == -1)
143127741Smarcel		return (-1);
144127741Smarcel	if (OF_getprop(input, "device_type", buf, sizeof(buf)) == -1)
145127741Smarcel		return (-1);
146127741Smarcel	if (strcmp(buf, "serial") != 0)
147127741Smarcel		return (-1);
148127741Smarcel	return (input);
149127741Smarcel}
150127741Smarcel
151138157Smarius/*
152146972Smarius * Get the package handle of the UART that is selected as the keyboard port,
153146972Smarius * if it's actually used to connect the keyboard according to the OF. I.e.
154146972Smarius * this will return the UART used to connect the keyboard regardless whether
155146972Smarius * it's stdin or not, however not in case the user or the OF gave preference
156146972Smarius * to e.g. a PS/2 keyboard by setting /aliases/keyboard accordingly.
157138157Smarius */
158127741Smarcelstatic phandle_t
159146972Smariusuart_cpu_getdev_keyboard(char *dev, size_t devsz)
160127741Smarcel{
161149111Smarius	char buf[sizeof("serial")];
162146972Smarius	phandle_t input;
163127741Smarcel
164146972Smarius	if ((input = OF_finddevice("keyboard")) == -1)
165146972Smarius		return (-1);
166146972Smarius	if (OF_getprop(input, "device_type", buf, sizeof(buf)) == -1)
167146972Smarius		return (-1);
168146972Smarius	if (strcmp(buf, "serial") != 0)
169146972Smarius		return (-1);
170146972Smarius	if (OF_getprop(input, "name", dev, devsz) == -1)
171146972Smarius		return (-1);
172146972Smarius	/*
173146972Smarius	 * So far this also matched PS/2 keyboard nodes so make sure it's
174146972Smarius	 * one of the SCCs/UARTs known to be used to connect keyboards.
175146972Smarius	 */
176146972Smarius	if (strcmp(dev, "su") && strcmp(dev, "su_pnp") && strcmp(dev, "zs"))
177146972Smarius		return (-1);
178146972Smarius	return (input);
179122466Sjake}
180122466Sjake
181119866Smarcelint
182119815Smarceluart_cpu_getdev(int devtype, struct uart_devinfo *di)
183119815Smarcel{
184149111Smarius	char buf[32], compat[32], dev[64];
185127741Smarcel	phandle_t input, options;
186119815Smarcel	bus_addr_t addr;
187120452Smarcel	int baud, bits, error, space, stop;
188119815Smarcel	char flag, par;
189119815Smarcel
190127741Smarcel	if ((options = OF_finddevice("/options")) == -1)
191127741Smarcel		return (ENXIO);
192127741Smarcel	switch (devtype) {
193127741Smarcel	case UART_DEV_CONSOLE:
194127741Smarcel		input = uart_cpu_getdev_console(options, dev, sizeof(dev));
195127741Smarcel		break;
196127741Smarcel	case UART_DEV_DBGPORT:
197149111Smarius		input = uart_cpu_getdev_dbgport(dev, sizeof(dev));
198127741Smarcel		break;
199127741Smarcel	case UART_DEV_KEYBOARD:
200146972Smarius		input = uart_cpu_getdev_keyboard(dev, sizeof(dev));
201127741Smarcel		break;
202127741Smarcel	default:
203127741Smarcel		input = -1;
204127741Smarcel		break;
205127741Smarcel	}
206127741Smarcel	if (input == -1)
207127741Smarcel		return (ENXIO);
208141753Smarius	error = OF_decode_addr(input, 0, &space, &addr);
209119815Smarcel	if (error)
210119815Smarcel		return (error);
211119815Smarcel
212119815Smarcel	/* Get the device class. */
213119815Smarcel	if (OF_getprop(input, "name", buf, sizeof(buf)) == -1)
214119815Smarcel		return (ENXIO);
215120009Stmm	if (OF_getprop(input, "compatible", compat, sizeof(compat)) == -1)
216120009Stmm		compat[0] = '\0';
217119815Smarcel	di->bas.regshft = 0;
218119815Smarcel	di->bas.rclk = 0;
219148824Smarius	if (!strcmp(buf, "se") || !strcmp(compat, "sab82532")) {
220119815Smarcel		di->ops = uart_sab82532_ops;
221138157Smarius		/* SAB82532 are only known to be used for TTYs. */
222138157Smarius		if ((di->bas.chan = uart_cpu_channel(dev)) == 0)
223138157Smarius			return (ENXIO);
224120452Smarcel		addr += 64 * (di->bas.chan - 1);
225119815Smarcel	} else if (!strcmp(buf, "zs")) {
226119815Smarcel		di->ops = uart_z8530_ops;
227138157Smarius		if ((di->bas.chan = uart_cpu_channel(dev)) == 0) {
228138157Smarius			/*
229138157Smarius			 * There's no way to determine from OF which
230138157Smarius			 * channel has the keyboard. Should always be
231138157Smarius			 * on channel 1 however.
232138157Smarius			 */
233138157Smarius			if (devtype == UART_DEV_KEYBOARD)
234138157Smarius				di->bas.chan = 1;
235138157Smarius			else
236138157Smarius				return (ENXIO);
237138157Smarius		}
238119815Smarcel		di->bas.regshft = 1;
239120452Smarcel		addr += 4 - 4 * (di->bas.chan - 1);
240120009Stmm	} else if (!strcmp(buf, "su") || !strcmp(buf, "su_pnp") ||
241120452Smarcel	    !strcmp(compat, "su") || !strcmp(compat, "su16550")) {
242119815Smarcel		di->ops = uart_ns8250_ops;
243120452Smarcel		di->bas.chan = 0;
244120452Smarcel	} else
245119815Smarcel		return (ENXIO);
246119815Smarcel
247119815Smarcel	/* Fill in the device info. */
248119815Smarcel	di->bas.bst = &bst_store[devtype];
249119815Smarcel	di->bas.bsh = sparc64_fake_bustag(space, addr, di->bas.bst);
250119815Smarcel
251119815Smarcel	/* Get the line settings. */
252120545Sjake	if (devtype == UART_DEV_KEYBOARD)
253120545Sjake		di->baudrate = 1200;
254120545Sjake	else
255120545Sjake		di->baudrate = 9600;
256119815Smarcel	di->databits = 8;
257119815Smarcel	di->stopbits = 1;
258119815Smarcel	di->parity = UART_PARITY_NONE;
259119815Smarcel	snprintf(buf, sizeof(buf), "%s-mode", dev);
260119815Smarcel	if (OF_getprop(options, buf, buf, sizeof(buf)) == -1)
261119815Smarcel		return (0);
262119815Smarcel	if (sscanf(buf, "%d,%d,%c,%d,%c", &baud, &bits, &par, &stop, &flag)
263119815Smarcel	    != 5)
264119815Smarcel		return (0);
265119815Smarcel	di->baudrate = baud;
266119815Smarcel	di->databits = bits;
267119815Smarcel	di->stopbits = stop;
268119815Smarcel	di->parity = (par == 'n') ? UART_PARITY_NONE :
269119815Smarcel	    (par == 'o') ? UART_PARITY_ODD : UART_PARITY_EVEN;
270119815Smarcel	return (0);
271119815Smarcel}
272