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 /*
48  * Determine which channel of a SCC a device referenced by an alias is.
49  * The information present in the OF device tree only allows to do this
50  * for "ttyX" aliases. If a device is a channel of a SCC its property
51  * in the /aliases node looks like one of these:
52  * ttya:  '/central/fhc/zs@0,902000:a'
53  * ttyc:  '/pci@1f,0/pci@1,1/ebus@1/se@14,400000:a'
54  */
55 static int
56 uart_cpu_channel(char *dev)
57 {
58 	char alias[64];
59 	phandle_t aliases;
60 	int len;
61 
62 	strcpy(alias, dev);
63 	if ((aliases = OF_finddevice("/aliases")) != -1)
64 		OF_getprop(aliases, dev, alias, sizeof(alias));
65 	len = strlen(alias);
66 	if (len < 2 || alias[len - 2] != ':' || alias[len - 1] < 'a' ||
67 	    alias[len - 1] > 'b')
68 		return (0);
69 	return (alias[len - 1] - 'a' + 1);
70 }
71 
72 int
73 uart_cpu_eqres(struct uart_bas *b1, struct uart_bas *b2)
74 {
75 
76 	return ((b1->bsh == b2->bsh) ? 1 : 0);
77 }
78 
79 /*
80  * Get the package handle of the UART that is selected as the console, if
81  * the console is an UART of course. Note that we enforce that both stdin
82  * and stdout are selected.
83  * Note that the currently active console (i.e. /chosen/stdout and
84  * /chosen/stdin) may not be the same as the device selected in the
85  * environment (ie /options/output-device and /options/input-device) because
86  * keyboard and screen were selected but the keyboard was unplugged or the
87  * user has changed the environment. In the latter case I would assume that
88  * the user expects that FreeBSD uses the new console setting.
89  * For weirder configurations, use ofw_console(4).
90  */
91 static phandle_t
92 uart_cpu_getdev_console(phandle_t options, char *dev, size_t devsz)
93 {
94 	char buf[32];
95 	ihandle_t stdin, stdout;
96 	phandle_t chosen, input;
97 
98 	if (OF_getprop(options, "input-device", dev, devsz) == -1)
99 		return (-1);
100 	if (OF_getprop(options, "output-device", buf, sizeof(buf)) == -1)
101 		return (-1);
102 	if (!strcmp(dev, "keyboard") && !strcmp(buf, "screen")) {
103 		if ((chosen = OF_finddevice("/chosen")) == -1)
104 			return (-1);
105 		if (OF_getprop(chosen, "stdin", &stdin, sizeof(stdin)) == -1)
106 			return (-1);
107 		if ((input = OF_instance_to_package(stdin)) == -1)
108 			return (-1);
109 		if (OF_getprop(chosen, "stdout", &stdout, sizeof(stdout)) == -1)
110 			return (-1);
111 		if (OF_instance_to_package(stdout) != input)
112 			return (-1);
113 		snprintf(dev, devsz, "ttya");
114 	} else {
115 		if ((input = OF_finddevice(dev)) == -1)
116 			return (-1);
117 		if (OF_finddevice(buf) != input)
118 			return (-1);
119 	}
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 package handle of the UART that's selected as the debug port.
129  * Since 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  */
134 static phandle_t
135 uart_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 
151 /*
152  * Get the package handle of the device that is selected as the keyboard
153  * port.
154  * XXX this also matches PS/2 keyboard controllers and most likely also
155  * USB keyboards.
156  */
157 static phandle_t
158 uart_cpu_getdev_keyboard(phandle_t root, char *dev, size_t devsz)
159 {
160 	char buf[32];
161 	phandle_t child, node;
162 
163 	child = OF_child(root);
164 	while (child != 0 && child != -1) {
165 		if (OF_getprop(child, "device_type", buf, sizeof(buf)) != -1 &&
166 		    !strcmp(buf, "serial") &&
167 		    OF_getprop(child, "keyboard", buf, sizeof(buf)) != -1) {
168 			OF_getprop(child, "name", dev, devsz);
169 			return (child);
170 		}
171 		if ((node = uart_cpu_getdev_keyboard(child, dev, devsz)) != -1)
172 			return (node);
173 		child = OF_peer(child);
174 	}
175 	return (-1);
176 }
177 
178 int
179 uart_cpu_getdev(int devtype, struct uart_devinfo *di)
180 {
181 	char buf[32], dev[32], compat[32];
182 	phandle_t input, options;
183 	bus_addr_t addr;
184 	int baud, bits, error, space, stop;
185 	char flag, par;
186 
187 	if ((options = OF_finddevice("/options")) == -1)
188 		return (ENXIO);
189 	switch (devtype) {
190 	case UART_DEV_CONSOLE:
191 		input = uart_cpu_getdev_console(options, dev, sizeof(dev));
192 		break;
193 	case UART_DEV_DBGPORT:
194 		input = uart_cpu_getdev_dbgport(options, dev, sizeof(dev));
195 		break;
196 	case UART_DEV_KEYBOARD:
197 		input = uart_cpu_getdev_keyboard(OF_peer(0), dev, sizeof(dev));
198 		break;
199 	default:
200 		input = -1;
201 		break;
202 	}
203 	if (input == -1)
204 		return (ENXIO);
205 	error = OF_decode_addr(input, 0, &space, &addr);
206 	if (error)
207 		return (error);
208 
209 	/* Get the device class. */
210 	if (OF_getprop(input, "name", buf, sizeof(buf)) == -1)
211 		return (ENXIO);
212 	if (OF_getprop(input, "compatible", compat, sizeof(compat)) == -1)
213 		compat[0] = '\0';
214 	di->bas.regshft = 0;
215 	di->bas.rclk = 0;
216 	if (!strcmp(buf, "se")) {
217 		di->ops = uart_sab82532_ops;
218 		/* SAB82532 are only known to be used for TTYs. */
219 		if ((di->bas.chan = uart_cpu_channel(dev)) == 0)
220 			return (ENXIO);
221 		addr += 64 * (di->bas.chan - 1);
222 	} else if (!strcmp(buf, "zs")) {
223 		di->ops = uart_z8530_ops;
224 		if ((di->bas.chan = uart_cpu_channel(dev)) == 0) {
225 			/*
226 			 * There's no way to determine from OF which
227 			 * channel has the keyboard. Should always be
228 			 * on channel 1 however.
229 			 */
230 			if (devtype == UART_DEV_KEYBOARD)
231 				di->bas.chan = 1;
232 			else
233 				return (ENXIO);
234 		}
235 		di->bas.regshft = 1;
236 		addr += 4 - 4 * (di->bas.chan - 1);
237 	} else if (!strcmp(buf, "su") || !strcmp(buf, "su_pnp") ||
238 	    !strcmp(compat, "su") || !strcmp(compat, "su16550")) {
239 		di->ops = uart_ns8250_ops;
240 		di->bas.chan = 0;
241 	} else
242 		return (ENXIO);
243 
244 	/* Fill in the device info. */
245 	di->bas.bst = &bst_store[devtype];
246 	di->bas.bsh = sparc64_fake_bustag(space, addr, di->bas.bst);
247 
248 	/* Get the line settings. */
249 	if (devtype == UART_DEV_KEYBOARD)
250 		di->baudrate = 1200;
251 	else
252 		di->baudrate = 9600;
253 	di->databits = 8;
254 	di->stopbits = 1;
255 	di->parity = UART_PARITY_NONE;
256 	snprintf(buf, sizeof(buf), "%s-mode", dev);
257 	if (OF_getprop(options, buf, buf, sizeof(buf)) == -1)
258 		return (0);
259 	if (sscanf(buf, "%d,%d,%c,%d,%c", &baud, &bits, &par, &stop, &flag)
260 	    != 5)
261 		return (0);
262 	di->baudrate = baud;
263 	di->databits = bits;
264 	di->stopbits = stop;
265 	di->parity = (par == 'n') ? UART_PARITY_NONE :
266 	    (par == 'o') ? UART_PARITY_ODD : UART_PARITY_EVEN;
267 	return (0);
268 }
269