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