uart_cpu_sparc64.c revision 122466
1/*
2 * Copyright (c) 2003 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 122466 2003-11-11 06:52:04Z jake $");
29
30#include <sys/param.h>
31#include <sys/systm.h>
32
33#include <machine/bus.h>
34#include <machine/bus_private.h>
35
36#include <dev/ofw/openfirm.h>
37#include <machine/ofw_machdep.h>
38
39#include <dev/uart/uart.h>
40#include <dev/uart/uart_cpu.h>
41
42static phandle_t uart_cpu_getdev_keyboard(phandle_t root);
43
44static struct bus_space_tag bst_store[3];
45
46static int
47uart_cpu_channel(char *dev)
48{
49	char alias[64];
50	phandle_t aliases;
51	int len;
52
53	strcpy(alias, dev);
54	if ((aliases = OF_finddevice("/aliases")) != -1)
55		OF_getprop(aliases, dev, alias, sizeof(alias));
56	len = strlen(alias);
57	if (len < 2 || alias[len - 2] != ':' || alias[len - 1] < 'a' ||
58	    alias[len - 1] > 'b')
59		return (0);
60	return (alias[len - 1] - 'a' + 1);
61}
62
63int
64uart_cpu_eqres(struct uart_bas *b1, struct uart_bas *b2)
65{
66
67	return ((b1->bsh == b2->bsh) ? 1 : 0);
68}
69
70static phandle_t
71uart_cpu_getdev_keyboard(phandle_t root)
72{
73	phandle_t child;
74	phandle_t node;
75	char buf[32];
76
77	for (child = OF_child(root); child != 0 && child != -1;
78	    child = OF_peer(child)) {
79		if (OF_getprop(child, "device_type", buf, sizeof(buf)) != -1 &&
80		    !strcmp(buf, "serial") &&
81		    OF_getprop(child, "keyboard", buf, sizeof(buf)) != -1)
82			return (child);
83		if ((node = uart_cpu_getdev_keyboard(child)) != -1)
84			return (node);
85	}
86	return (-1);
87}
88
89int
90uart_cpu_getdev(int devtype, struct uart_devinfo *di)
91{
92	char buf[32], dev[32], compat[32];
93	phandle_t input, options, output;
94	bus_addr_t addr;
95	int baud, bits, error, space, stop;
96	char flag, par;
97
98	/*
99	 * Get the address of the UART that is selected as the console, if
100	 * the console is an UART of course. Note that we enforce that both
101	 * stdin and stdout are selected. For weird configurations, use
102	 * ofw_console(4).
103	 * Note that the currently active console (ie /chosen/stdout and
104	 * /chosen/stdin) may not be the same as the device selected in the
105	 * environment (ie /options/output-device and /options/input-device)
106	 * because the user may have changed the environment. In that case
107	 * I would assume that the user expects that FreeBSD uses the new
108	 * console setting. There's choice choice, really.
109	 */
110	 if ((options = OF_finddevice("/options")) == -1)
111		 return (ENXIO);
112	if (devtype == UART_DEV_CONSOLE) {
113		if (OF_getprop(options, "input-device", dev, sizeof(dev)) == -1)
114			return (ENXIO);
115		if ((input = OF_finddevice(dev)) == -1)
116			return (ENXIO);
117		if (OF_getprop(input, "device_type", buf, sizeof(buf)) == -1)
118			return (ENXIO);
119		if (strcmp(buf, "serial"))
120			return (ENODEV);
121		if (OF_getprop(options, "output-device", buf, sizeof(buf))
122		    == -1)
123			return (ENXIO);
124		if ((output = OF_finddevice(buf)) == -1)
125			return (ENXIO);
126		if (input != output)
127			return (ENXIO);
128	} else if (devtype == UART_DEV_KEYBOARD) {
129		if ((input = uart_cpu_getdev_keyboard(OF_peer(0))) == -1)
130			return (ENXIO);
131	} else
132		return (ENODEV);
133
134	error = OF_decode_addr(input, &space, &addr);
135	if (error)
136		return (error);
137
138	/* Get the device class. */
139	if (OF_getprop(input, "name", buf, sizeof(buf)) == -1)
140		return (ENXIO);
141	if (OF_getprop(input, "compatible", compat, sizeof(compat)) == -1)
142		compat[0] = '\0';
143	di->bas.regshft = 0;
144	di->bas.rclk = 0;
145	if (!strcmp(buf, "se")) {
146		di->ops = uart_sab82532_ops;
147		di->bas.chan = uart_cpu_channel(dev);
148		addr += 64 * (di->bas.chan - 1);
149	} else if (!strcmp(buf, "zs")) {
150		di->ops = uart_z8530_ops;
151		di->bas.chan = uart_cpu_channel(dev);
152		di->bas.regshft = 1;
153		addr += 4 - 4 * (di->bas.chan - 1);
154	} else if (!strcmp(buf, "su") || !strcmp(buf, "su_pnp") ||
155	    !strcmp(compat, "su") || !strcmp(compat, "su16550")) {
156		di->ops = uart_ns8250_ops;
157		di->bas.chan = 0;
158	} else
159		return (ENXIO);
160
161	/* Fill in the device info. */
162	di->bas.bst = &bst_store[devtype];
163	di->bas.bsh = sparc64_fake_bustag(space, addr, di->bas.bst);
164
165	/* Get the line settings. */
166	if (devtype == UART_DEV_KEYBOARD)
167		di->baudrate = 1200;
168	else
169		di->baudrate = 9600;
170	di->databits = 8;
171	di->stopbits = 1;
172	di->parity = UART_PARITY_NONE;
173	snprintf(buf, sizeof(buf), "%s-mode", dev);
174	if (OF_getprop(options, buf, buf, sizeof(buf)) == -1)
175		return (0);
176	if (sscanf(buf, "%d,%d,%c,%d,%c", &baud, &bits, &par, &stop, &flag)
177	    != 5)
178		return (0);
179	di->baudrate = baud;
180	di->databits = bits;
181	di->stopbits = stop;
182	di->parity = (par == 'n') ? UART_PARITY_NONE :
183	    (par == 'o') ? UART_PARITY_ODD : UART_PARITY_EVEN;
184	return (0);
185}
186