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$");
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 
42 bus_space_tag_t uart_bus_space_io;
43 bus_space_tag_t uart_bus_space_mem;
44 
45 static struct bus_space_tag bst_store[3];
46 
47 static int
48 uart_cpu_channel(char *dev)
49 {
50 	char alias[64];
51 	phandle_t aliases;
52 	int len;
53 
54 	strcpy(alias, dev);
55 	if ((aliases = OF_finddevice("/aliases")) != -1)
56 		OF_getprop(aliases, dev, alias, sizeof(alias));
57 	len = strlen(alias);
58 	if (len < 2 || alias[len - 2] != ':' || alias[len - 1] < 'a' ||
59 	    alias[len - 1] > 'b')
60 		return (0);
61 	return (alias[len - 1] - 'a' + 1);
62 }
63 
64 int
65 uart_cpu_eqres(struct uart_bas *b1, struct uart_bas *b2)
66 {
67 
68 	return ((b1->bsh == b2->bsh) ? 1 : 0);
69 }
70 
71 /*
72  * Get the address of the UART that is selected as the console, if the
73  * console is an UART of course. Note that we enforce that both stdin and
74  * stdout are selected. For weird configurations, use ofw_console(4).
75  * Note that the currently active console (i.e. /chosen/stdout and
76  * /chosen/stdin) may not be the same as the device selected in the
77  * environment (ie /options/output-device and /options/input-device) because
78  * the user may have changed the environment. In that case I would assume
79  * that the user expects that FreeBSD uses the new console setting. There's
80  * no choice, really.
81  */
82 static phandle_t
83 uart_cpu_getdev_console(phandle_t options, char *dev, size_t devsz)
84 {
85 	char buf[32];
86 	phandle_t input;
87 
88 	if (OF_getprop(options, "input-device", dev, devsz) == -1)
89 		return (-1);
90 	if ((input = OF_finddevice(dev)) == -1)
91 		return (-1);
92 	if (OF_getprop(input, "device_type", buf, sizeof(buf)) == -1)
93 		return (-1);
94 	if (strcmp(buf, "serial") != 0)
95 		return (-1);
96 	if (OF_getprop(options, "output-device", buf, sizeof(buf)) == -1)
97 		return (-1);
98 	if (OF_finddevice(buf) != input)
99 		return (-1);
100 	return (input);
101 }
102 
103 /*
104  * Get the address of the UART that's selected as the debug port. Since
105  * there's no place for this in the OF, we use the kernel environment
106  * variable "hw.uart.dbgport". Note however that the variable is not a
107  * list of attributes. It's single device name or alias, as known by
108  * the OF.
109  */
110 static phandle_t
111 uart_cpu_getdev_dbgport(phandle_t options, char *dev, size_t devsz)
112 {
113 	char buf[32];
114 	phandle_t input;
115 
116 	if (!getenv_string("hw.uart.dbgport", dev, devsz))
117 		return (-1);
118 	if ((input = OF_finddevice(dev)) == -1)
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 static phandle_t
128 uart_cpu_getdev_keyboard(phandle_t root, char *dev, size_t devsz)
129 {
130 	char buf[32];
131 	phandle_t child, node;
132 
133 	child = OF_child(root);
134 	while (child != 0 && child != -1) {
135 		if (OF_getprop(child, "device_type", buf, sizeof(buf)) != -1 &&
136 		    !strcmp(buf, "serial") &&
137 		    OF_getprop(child, "keyboard", buf, sizeof(buf)) != -1) {
138 			OF_getprop(child, "name", dev, devsz);
139 			return (child);
140 		}
141 		if ((node = uart_cpu_getdev_keyboard(child, dev, devsz)) != -1)
142 			return (node);
143 		child = OF_peer(child);
144 	}
145 	return (-1);
146 }
147 
148 int
149 uart_cpu_getdev(int devtype, struct uart_devinfo *di)
150 {
151 	char buf[32], dev[32], compat[32];
152 	phandle_t input, options;
153 	bus_addr_t addr;
154 	int baud, bits, error, space, stop;
155 	char flag, par;
156 
157 	if ((options = OF_finddevice("/options")) == -1)
158 		return (ENXIO);
159 	switch (devtype) {
160 	case UART_DEV_CONSOLE:
161 		input = uart_cpu_getdev_console(options, dev, sizeof(dev));
162 		break;
163 	case UART_DEV_DBGPORT:
164 		input = uart_cpu_getdev_dbgport(options, dev, sizeof(dev));
165 		break;
166 	case UART_DEV_KEYBOARD:
167 		input = uart_cpu_getdev_keyboard(OF_peer(0), dev, sizeof(dev));
168 		break;
169 	default:
170 		input = -1;
171 		break;
172 	}
173 	if (input == -1)
174 		return (ENXIO);
175 	error = OF_decode_addr(input, &space, &addr);
176 	if (error)
177 		return (error);
178 
179 	/* Get the device class. */
180 	if (OF_getprop(input, "name", buf, sizeof(buf)) == -1)
181 		return (ENXIO);
182 	if (OF_getprop(input, "compatible", compat, sizeof(compat)) == -1)
183 		compat[0] = '\0';
184 	di->bas.regshft = 0;
185 	di->bas.rclk = 0;
186 	if (!strcmp(buf, "se")) {
187 		di->ops = uart_sab82532_ops;
188 		di->bas.chan = uart_cpu_channel(dev);
189 		addr += 64 * (di->bas.chan - 1);
190 	} else if (!strcmp(buf, "zs")) {
191 		di->ops = uart_z8530_ops;
192 		di->bas.chan = uart_cpu_channel(dev);
193 		di->bas.regshft = 1;
194 		addr += 4 - 4 * (di->bas.chan - 1);
195 	} else if (!strcmp(buf, "su") || !strcmp(buf, "su_pnp") ||
196 	    !strcmp(compat, "su") || !strcmp(compat, "su16550")) {
197 		di->ops = uart_ns8250_ops;
198 		di->bas.chan = 0;
199 	} else
200 		return (ENXIO);
201 
202 	/* Fill in the device info. */
203 	di->bas.bst = &bst_store[devtype];
204 	di->bas.bsh = sparc64_fake_bustag(space, addr, di->bas.bst);
205 
206 	/* Get the line settings. */
207 	if (devtype == UART_DEV_KEYBOARD)
208 		di->baudrate = 1200;
209 	else
210 		di->baudrate = 9600;
211 	di->databits = 8;
212 	di->stopbits = 1;
213 	di->parity = UART_PARITY_NONE;
214 	snprintf(buf, sizeof(buf), "%s-mode", dev);
215 	if (OF_getprop(options, buf, buf, sizeof(buf)) == -1)
216 		return (0);
217 	if (sscanf(buf, "%d,%d,%c,%d,%c", &baud, &bits, &par, &stop, &flag)
218 	    != 5)
219 		return (0);
220 	di->baudrate = baud;
221 	di->databits = bits;
222 	di->stopbits = stop;
223 	di->parity = (par == 'n') ? UART_PARITY_NONE :
224 	    (par == 'o') ? UART_PARITY_ODD : UART_PARITY_EVEN;
225 	return (0);
226 }
227