uart_cpu_sparc64.c revision 133735
1/*
2 * Copyright (c) 2003, 2004 Marcel Moolenaar
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD: head/sys/dev/uart/uart_cpu_sparc64.c 133735 2004-08-14 23:54:27Z marius $");
29
30#include "opt_isa.h"
31
32#include <sys/param.h>
33#include <sys/systm.h>
34
35#include <machine/bus.h>
36#include <machine/bus_private.h>
37#include <machine/resource.h>
38
39#include <dev/ofw/ofw_bus.h>
40#include <dev/ofw/openfirm.h>
41#include <machine/ofw_machdep.h>
42
43#include <isa/isavar.h>
44
45#include <dev/uart/uart.h>
46#include <dev/uart/uart_bus.h>
47#include <dev/uart/uart_cpu.h>
48
49#include <sparc64/pci/ofw_pci.h>
50#include <sparc64/isa/ofw_isa.h>
51
52bus_space_tag_t uart_bus_space_io;
53bus_space_tag_t uart_bus_space_mem;
54
55static struct bus_space_tag bst_store[3];
56
57static int
58uart_cpu_channel(char *dev)
59{
60	char alias[64];
61	phandle_t aliases;
62	int len;
63
64	strcpy(alias, dev);
65	if ((aliases = OF_finddevice("/aliases")) != -1)
66		OF_getprop(aliases, dev, alias, sizeof(alias));
67	len = strlen(alias);
68	if (len < 2 || alias[len - 2] != ':' || alias[len - 1] < 'a' ||
69	    alias[len - 1] > 'b')
70		return (0);
71	return (alias[len - 1] - 'a' + 1);
72}
73
74int
75uart_cpu_eqres(struct uart_bas *b1, struct uart_bas *b2)
76{
77
78	return ((b1->bsh == b2->bsh) ? 1 : 0);
79}
80
81/*
82 * Get the address of the UART that is selected as the console, if the
83 * console is an UART of course. Note that we enforce that both stdin and
84 * stdout are selected.
85 * Note that the currently active console (i.e. /chosen/stdout and
86 * /chosen/stdin) may not be the same as the device selected in the
87 * environment (ie /options/output-device and /options/input-device) because
88 * keyboard and screen were selected but the keyboard was unplugged or the
89 * user has changed the environment. In the latter case I would assume that
90 * the user expects that FreeBSD uses the new console setting.
91 * For weirder configurations, use ofw_console(4).
92 */
93static phandle_t
94uart_cpu_getdev_console(phandle_t options, char *dev, size_t devsz)
95{
96	char buf[32];
97	phandle_t input;
98
99	if (OF_getprop(options, "input-device", dev, devsz) == -1)
100		return (-1);
101	if ((input = OF_finddevice(dev)) == -1)
102		return (-1);
103	if (OF_getprop(options, "output-device", buf, sizeof(buf)) == -1)
104		return (-1);
105	if (!strcmp(dev, "keyboard") && !strcmp(buf, "screen")) {
106		phandle_t chosen;
107		ihandle_t stdin, stdout;
108
109		if ((chosen = OF_finddevice("/chosen")) == -1)
110			return (-1);
111		if (OF_getprop(chosen, "stdin", &stdin, sizeof(stdin)) == -1)
112			return (-1);
113		if ((input = OF_instance_to_package(stdin)) == -1)
114			return (-1);
115		if (OF_getprop(chosen, "stdout", &stdout, sizeof(stdout)) == -1)
116			return (-1);
117		if (OF_instance_to_package(stdout) != input)
118			return (-1);
119		snprintf(dev, devsz, "ttya");
120	} else if (OF_finddevice(buf) != input)
121		return (-1);
122	if (OF_getprop(input, "device_type", buf, sizeof(buf)) == -1)
123		return (-1);
124	if (strcmp(buf, "serial") != 0)
125		return (-1);
126	return (input);
127}
128
129/*
130 * Get the address of the UART that's selected as the debug port. Since
131 * there's no place for this in the OF, we use the kernel environment
132 * variable "hw.uart.dbgport". Note however that the variable is not a
133 * list of attributes. It's single device name or alias, as known by
134 * the OF.
135 */
136static phandle_t
137uart_cpu_getdev_dbgport(phandle_t options, char *dev, size_t devsz)
138{
139	char buf[32];
140	phandle_t input;
141
142	if (!getenv_string("hw.uart.dbgport", dev, devsz))
143		return (-1);
144	if ((input = OF_finddevice(dev)) == -1)
145		return (-1);
146	if (OF_getprop(input, "device_type", buf, sizeof(buf)) == -1)
147		return (-1);
148	if (strcmp(buf, "serial") != 0)
149		return (-1);
150	return (input);
151}
152
153static phandle_t
154uart_cpu_getdev_keyboard(phandle_t root, char *dev, size_t devsz)
155{
156	char buf[32];
157	phandle_t child, node;
158
159	child = OF_child(root);
160	while (child != 0 && child != -1) {
161		if (OF_getprop(child, "device_type", buf, sizeof(buf)) != -1 &&
162		    !strcmp(buf, "serial") &&
163		    OF_getprop(child, "keyboard", buf, sizeof(buf)) != -1) {
164			OF_getprop(child, "name", dev, devsz);
165			return (child);
166		}
167		if ((node = uart_cpu_getdev_keyboard(child, dev, devsz)) != -1)
168			return (node);
169		child = OF_peer(child);
170	}
171	return (-1);
172}
173
174int
175uart_cpu_getdev(int devtype, struct uart_devinfo *di)
176{
177	char buf[32], dev[32], compat[32];
178	phandle_t input, options;
179	bus_addr_t addr;
180	int baud, bits, error, space, stop;
181	char flag, par;
182
183	if ((options = OF_finddevice("/options")) == -1)
184		return (ENXIO);
185	switch (devtype) {
186	case UART_DEV_CONSOLE:
187		input = uart_cpu_getdev_console(options, dev, sizeof(dev));
188		break;
189	case UART_DEV_DBGPORT:
190		input = uart_cpu_getdev_dbgport(options, dev, sizeof(dev));
191		break;
192	case UART_DEV_KEYBOARD:
193		input = uart_cpu_getdev_keyboard(OF_peer(0), dev, sizeof(dev));
194		break;
195	default:
196		input = -1;
197		break;
198	}
199	if (input == -1)
200		return (ENXIO);
201	error = OF_decode_addr(input, &space, &addr);
202	if (error)
203		return (error);
204
205	/* Get the device class. */
206	if (OF_getprop(input, "name", buf, sizeof(buf)) == -1)
207		return (ENXIO);
208	if (OF_getprop(input, "compatible", compat, sizeof(compat)) == -1)
209		compat[0] = '\0';
210	di->bas.regshft = 0;
211	di->bas.rclk = 0;
212	if (!strcmp(buf, "se")) {
213		di->ops = uart_sab82532_ops;
214		di->bas.chan = uart_cpu_channel(dev);
215		addr += 64 * (di->bas.chan - 1);
216	} else if (!strcmp(buf, "zs")) {
217		di->ops = uart_z8530_ops;
218		di->bas.chan = uart_cpu_channel(dev);
219		di->bas.regshft = 1;
220		addr += 4 - 4 * (di->bas.chan - 1);
221	} else if (!strcmp(buf, "su") || !strcmp(buf, "su_pnp") ||
222	    !strcmp(compat, "su") || !strcmp(compat, "su16550")) {
223		di->ops = uart_ns8250_ops;
224		di->bas.chan = 0;
225	} else
226		return (ENXIO);
227
228	/* Fill in the device info. */
229	di->bas.bst = &bst_store[devtype];
230	di->bas.bsh = sparc64_fake_bustag(space, addr, di->bas.bst);
231
232	/* Get the line settings. */
233	if (devtype == UART_DEV_KEYBOARD)
234		di->baudrate = 1200;
235	else
236		di->baudrate = 9600;
237	di->databits = 8;
238	di->stopbits = 1;
239	di->parity = UART_PARITY_NONE;
240	snprintf(buf, sizeof(buf), "%s-mode", dev);
241	if (OF_getprop(options, buf, buf, sizeof(buf)) == -1)
242		return (0);
243	if (sscanf(buf, "%d,%d,%c,%d,%c", &baud, &bits, &par, &stop, &flag)
244	    != 5)
245		return (0);
246	di->baudrate = baud;
247	di->databits = bits;
248	di->stopbits = stop;
249	di->parity = (par == 'n') ? UART_PARITY_NONE :
250	    (par == 'o') ? UART_PARITY_ODD : UART_PARITY_EVEN;
251	return (0);
252}
253
254void
255uart_cpu_identify(driver_t *driver, device_t parent)
256{
257#ifdef DEV_ISA
258	char buf[32];
259	struct isa_regs reg;
260	device_t child;
261	phandle_t node;
262	ofw_isa_intr_t intr;
263#endif
264
265#ifdef DEV_ISA
266	if (strcmp(device_get_name(parent), "isa") == 0) {
267		if ((node = ofw_bus_get_node(device_get_parent(parent))) == 0)
268			return;
269		for (node = OF_child(node); node != 0; node = OF_peer(node)) {
270			if (OF_getprop(node, "name", buf, sizeof(buf)) == -1)
271				continue;
272			if (strcmp(buf, "serial") != 0)
273				continue;
274			if ((OF_getprop(node, "reg", &reg,
275			    sizeof(reg)) == -1) ||
276		    	    (OF_getprop(node, "interrupts", &intr,
277			    sizeof(intr)) == -1))
278				continue;
279			if ((child = BUS_ADD_CHILD(parent, ISA_ORDER_SENSITIVE,
280			    uart_driver_name, -1)) == NULL)
281				return;
282			bus_set_resource(child, SYS_RES_IOPORT, 0,
283			    ISA_REG_PHYS(&reg), reg.size);
284			bus_set_resource(child, SYS_RES_IRQ, 0, intr, 1);
285		}
286	}
287#endif
288}
289