uart_cpu_sparc64.c revision 133738
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 133738 2004-08-15 02:17:20Z 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	ihandle_t stdin, stdout;
98	phandle_t chosen, input;
99
100	if (OF_getprop(options, "input-device", dev, devsz) == -1)
101		return (-1);
102	if ((input = OF_finddevice(dev)) == -1)
103		return (-1);
104	if (OF_getprop(options, "output-device", buf, sizeof(buf)) == -1)
105		return (-1);
106	if (!strcmp(dev, "keyboard") && !strcmp(buf, "screen")) {
107		if ((chosen = OF_finddevice("/chosen")) == -1)
108			return (-1);
109		if (OF_getprop(chosen, "stdin", &stdin, sizeof(stdin)) == -1)
110			return (-1);
111		if ((input = OF_instance_to_package(stdin)) == -1)
112			return (-1);
113		if (OF_getprop(chosen, "stdout", &stdout, sizeof(stdout)) == -1)
114			return (-1);
115		if (OF_instance_to_package(stdout) != input)
116			return (-1);
117		snprintf(dev, devsz, "ttya");
118	} else if (OF_finddevice(buf) != input)
119		return (-1);
120	if (OF_getprop(input, "device_type", buf, sizeof(buf)) == -1)
121		return (-1);
122	if (strcmp(buf, "serial") != 0)
123		return (-1);
124	return (input);
125}
126
127/*
128 * Get the address of the UART that's selected as the debug port. Since
129 * there's no place for this in the OF, we use the kernel environment
130 * variable "hw.uart.dbgport". Note however that the variable is not a
131 * list of attributes. It's single device name or alias, as known by
132 * the OF.
133 */
134static phandle_t
135uart_cpu_getdev_dbgport(phandle_t options, char *dev, size_t devsz)
136{
137	char buf[32];
138	phandle_t input;
139
140	if (!getenv_string("hw.uart.dbgport", dev, devsz))
141		return (-1);
142	if ((input = OF_finddevice(dev)) == -1)
143		return (-1);
144	if (OF_getprop(input, "device_type", buf, sizeof(buf)) == -1)
145		return (-1);
146	if (strcmp(buf, "serial") != 0)
147		return (-1);
148	return (input);
149}
150
151static phandle_t
152uart_cpu_getdev_keyboard(phandle_t root, char *dev, size_t devsz)
153{
154	char buf[32];
155	phandle_t child, node;
156
157	child = OF_child(root);
158	while (child != 0 && child != -1) {
159		if (OF_getprop(child, "device_type", buf, sizeof(buf)) != -1 &&
160		    !strcmp(buf, "serial") &&
161		    OF_getprop(child, "keyboard", buf, sizeof(buf)) != -1) {
162			OF_getprop(child, "name", dev, devsz);
163			return (child);
164		}
165		if ((node = uart_cpu_getdev_keyboard(child, dev, devsz)) != -1)
166			return (node);
167		child = OF_peer(child);
168	}
169	return (-1);
170}
171
172int
173uart_cpu_getdev(int devtype, struct uart_devinfo *di)
174{
175	char buf[32], dev[32], compat[32];
176	phandle_t input, options;
177	bus_addr_t addr;
178	int baud, bits, error, space, stop;
179	char flag, par;
180
181	if ((options = OF_finddevice("/options")) == -1)
182		return (ENXIO);
183	switch (devtype) {
184	case UART_DEV_CONSOLE:
185		input = uart_cpu_getdev_console(options, dev, sizeof(dev));
186		break;
187	case UART_DEV_DBGPORT:
188		input = uart_cpu_getdev_dbgport(options, dev, sizeof(dev));
189		break;
190	case UART_DEV_KEYBOARD:
191		input = uart_cpu_getdev_keyboard(OF_peer(0), dev, sizeof(dev));
192		break;
193	default:
194		input = -1;
195		break;
196	}
197	if (input == -1)
198		return (ENXIO);
199	error = OF_decode_addr(input, &space, &addr);
200	if (error)
201		return (error);
202
203	/* Get the device class. */
204	if (OF_getprop(input, "name", buf, sizeof(buf)) == -1)
205		return (ENXIO);
206	if (OF_getprop(input, "compatible", compat, sizeof(compat)) == -1)
207		compat[0] = '\0';
208	di->bas.regshft = 0;
209	di->bas.rclk = 0;
210	if (!strcmp(buf, "se")) {
211		di->ops = uart_sab82532_ops;
212		di->bas.chan = uart_cpu_channel(dev);
213		addr += 64 * (di->bas.chan - 1);
214	} else if (!strcmp(buf, "zs")) {
215		di->ops = uart_z8530_ops;
216		di->bas.chan = uart_cpu_channel(dev);
217		di->bas.regshft = 1;
218		addr += 4 - 4 * (di->bas.chan - 1);
219	} else if (!strcmp(buf, "su") || !strcmp(buf, "su_pnp") ||
220	    !strcmp(compat, "su") || !strcmp(compat, "su16550")) {
221		di->ops = uart_ns8250_ops;
222		di->bas.chan = 0;
223	} else
224		return (ENXIO);
225
226	/* Fill in the device info. */
227	di->bas.bst = &bst_store[devtype];
228	di->bas.bsh = sparc64_fake_bustag(space, addr, di->bas.bst);
229
230	/* Get the line settings. */
231	if (devtype == UART_DEV_KEYBOARD)
232		di->baudrate = 1200;
233	else
234		di->baudrate = 9600;
235	di->databits = 8;
236	di->stopbits = 1;
237	di->parity = UART_PARITY_NONE;
238	snprintf(buf, sizeof(buf), "%s-mode", dev);
239	if (OF_getprop(options, buf, buf, sizeof(buf)) == -1)
240		return (0);
241	if (sscanf(buf, "%d,%d,%c,%d,%c", &baud, &bits, &par, &stop, &flag)
242	    != 5)
243		return (0);
244	di->baudrate = baud;
245	di->databits = bits;
246	di->stopbits = stop;
247	di->parity = (par == 'n') ? UART_PARITY_NONE :
248	    (par == 'o') ? UART_PARITY_ODD : UART_PARITY_EVEN;
249	return (0);
250}
251
252void
253uart_cpu_identify(driver_t *driver, device_t parent)
254{
255#ifdef DEV_ISA
256	char buf[32];
257	struct isa_regs reg;
258	device_t child;
259	phandle_t node;
260	ofw_isa_intr_t intr;
261#endif
262
263#ifdef DEV_ISA
264	if (strcmp(device_get_name(parent), "isa") == 0) {
265		if ((node = ofw_bus_get_node(device_get_parent(parent))) == 0)
266			return;
267		for (node = OF_child(node); node != 0; node = OF_peer(node)) {
268			if (OF_getprop(node, "name", buf, sizeof(buf)) == -1)
269				continue;
270			if (strcmp(buf, "serial") != 0)
271				continue;
272			if ((OF_getprop(node, "reg", &reg,
273			    sizeof(reg)) == -1) ||
274		    	    (OF_getprop(node, "interrupts", &intr,
275			    sizeof(intr)) == -1))
276				continue;
277			if ((child = BUS_ADD_CHILD(parent, ISA_ORDER_SENSITIVE,
278			    uart_driver_name, -1)) == NULL)
279				return;
280			bus_set_resource(child, SYS_RES_IOPORT, 0,
281			    ISA_REG_PHYS(&reg), reg.size);
282			bus_set_resource(child, SYS_RES_IRQ, 0, intr, 1);
283		}
284	}
285#endif
286}
287