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.
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  * keyboard and screen were selected but the keyboard was unplugged or the
79  * user has changed the environment. In the latter case I would assume that
80  * the user expects that FreeBSD uses the new console setting.
81  * For weirder configurations, use ofw_console(4).
82  */
83 static phandle_t
84 uart_cpu_getdev_console(phandle_t options, char *dev, size_t devsz)
85 {
86 	char buf[32];
87 	ihandle_t stdin, stdout;
88 	phandle_t chosen, input;
89 
90 	if (OF_getprop(options, "input-device", dev, devsz) == -1)
91 		return (-1);
92 	if ((input = OF_finddevice(dev)) == -1)
93 		return (-1);
94 	if (OF_getprop(options, "output-device", buf, sizeof(buf)) == -1)
95 		return (-1);
96 	if (!strcmp(dev, "keyboard") && !strcmp(buf, "screen")) {
97 		if ((chosen = OF_finddevice("/chosen")) == -1)
98 			return (-1);
99 		if (OF_getprop(chosen, "stdin", &stdin, sizeof(stdin)) == -1)
100 			return (-1);
101 		if ((input = OF_instance_to_package(stdin)) == -1)
102 			return (-1);
103 		if (OF_getprop(chosen, "stdout", &stdout, sizeof(stdout)) == -1)
104 			return (-1);
105 		if (OF_instance_to_package(stdout) != input)
106 			return (-1);
107 		snprintf(dev, devsz, "ttya");
108 	} else if (OF_finddevice(buf) != input)
109 		return (-1);
110 	if (OF_getprop(input, "device_type", buf, sizeof(buf)) == -1)
111 		return (-1);
112 	if (strcmp(buf, "serial") != 0)
113 		return (-1);
114 	return (input);
115 }
116 
117 /*
118  * Get the address of the UART that's selected as the debug port. Since
119  * there's no place for this in the OF, we use the kernel environment
120  * variable "hw.uart.dbgport". Note however that the variable is not a
121  * list of attributes. It's single device name or alias, as known by
122  * the OF.
123  */
124 static phandle_t
125 uart_cpu_getdev_dbgport(phandle_t options, char *dev, size_t devsz)
126 {
127 	char buf[32];
128 	phandle_t input;
129 
130 	if (!getenv_string("hw.uart.dbgport", dev, devsz))
131 		return (-1);
132 	if ((input = OF_finddevice(dev)) == -1)
133 		return (-1);
134 	if (OF_getprop(input, "device_type", buf, sizeof(buf)) == -1)
135 		return (-1);
136 	if (strcmp(buf, "serial") != 0)
137 		return (-1);
138 	return (input);
139 }
140 
141 static phandle_t
142 uart_cpu_getdev_keyboard(phandle_t root, char *dev, size_t devsz)
143 {
144 	char buf[32];
145 	phandle_t child, node;
146 
147 	child = OF_child(root);
148 	while (child != 0 && child != -1) {
149 		if (OF_getprop(child, "device_type", buf, sizeof(buf)) != -1 &&
150 		    !strcmp(buf, "serial") &&
151 		    OF_getprop(child, "keyboard", buf, sizeof(buf)) != -1) {
152 			OF_getprop(child, "name", dev, devsz);
153 			return (child);
154 		}
155 		if ((node = uart_cpu_getdev_keyboard(child, dev, devsz)) != -1)
156 			return (node);
157 		child = OF_peer(child);
158 	}
159 	return (-1);
160 }
161 
162 int
163 uart_cpu_getdev(int devtype, struct uart_devinfo *di)
164 {
165 	char buf[32], dev[32], compat[32];
166 	phandle_t input, options;
167 	bus_addr_t addr;
168 	int baud, bits, error, space, stop;
169 	char flag, par;
170 
171 	if ((options = OF_finddevice("/options")) == -1)
172 		return (ENXIO);
173 	switch (devtype) {
174 	case UART_DEV_CONSOLE:
175 		input = uart_cpu_getdev_console(options, dev, sizeof(dev));
176 		break;
177 	case UART_DEV_DBGPORT:
178 		input = uart_cpu_getdev_dbgport(options, dev, sizeof(dev));
179 		break;
180 	case UART_DEV_KEYBOARD:
181 		input = uart_cpu_getdev_keyboard(OF_peer(0), dev, sizeof(dev));
182 		break;
183 	default:
184 		input = -1;
185 		break;
186 	}
187 	if (input == -1)
188 		return (ENXIO);
189 	error = OF_decode_addr(input, &space, &addr);
190 	if (error)
191 		return (error);
192 
193 	/* Get the device class. */
194 	if (OF_getprop(input, "name", buf, sizeof(buf)) == -1)
195 		return (ENXIO);
196 	if (OF_getprop(input, "compatible", compat, sizeof(compat)) == -1)
197 		compat[0] = '\0';
198 	di->bas.regshft = 0;
199 	di->bas.rclk = 0;
200 	if (!strcmp(buf, "se")) {
201 		di->ops = uart_sab82532_ops;
202 		di->bas.chan = uart_cpu_channel(dev);
203 		addr += 64 * (di->bas.chan - 1);
204 	} else if (!strcmp(buf, "zs")) {
205 		di->ops = uart_z8530_ops;
206 		di->bas.chan = uart_cpu_channel(dev);
207 		di->bas.regshft = 1;
208 		addr += 4 - 4 * (di->bas.chan - 1);
209 	} else if (!strcmp(buf, "su") || !strcmp(buf, "su_pnp") ||
210 	    !strcmp(compat, "su") || !strcmp(compat, "su16550")) {
211 		di->ops = uart_ns8250_ops;
212 		di->bas.chan = 0;
213 	} else
214 		return (ENXIO);
215 
216 	/* Fill in the device info. */
217 	di->bas.bst = &bst_store[devtype];
218 	di->bas.bsh = sparc64_fake_bustag(space, addr, di->bas.bst);
219 
220 	/* Get the line settings. */
221 	if (devtype == UART_DEV_KEYBOARD)
222 		di->baudrate = 1200;
223 	else
224 		di->baudrate = 9600;
225 	di->databits = 8;
226 	di->stopbits = 1;
227 	di->parity = UART_PARITY_NONE;
228 	snprintf(buf, sizeof(buf), "%s-mode", dev);
229 	if (OF_getprop(options, buf, buf, sizeof(buf)) == -1)
230 		return (0);
231 	if (sscanf(buf, "%d,%d,%c,%d,%c", &baud, &bits, &par, &stop, &flag)
232 	    != 5)
233 		return (0);
234 	di->baudrate = baud;
235 	di->databits = bits;
236 	di->stopbits = stop;
237 	di->parity = (par == 'n') ? UART_PARITY_NONE :
238 	    (par == 'o') ? UART_PARITY_ODD : UART_PARITY_EVEN;
239 	return (0);
240 }
241