1 /*-
2 * Copyright (c) 2003, 2004 Marcel Moolenaar
3 * Copyright (c) 2004 - 2006 Marius Strobl <marius@FreeBSD.org>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30
31 #include <sys/param.h>
32 #include <sys/systm.h>
33
34 #include <machine/bus.h>
35 #include <machine/bus_private.h>
36
37 #include <dev/ofw/openfirm.h>
38 #include <machine/ofw_machdep.h>
39
40 #include <dev/uart/uart.h>
41 #include <dev/uart/uart_cpu.h>
42
43 bus_space_tag_t uart_bus_space_io;
44 bus_space_tag_t uart_bus_space_mem;
45
46 static struct bus_space_tag bst_store[3];
47
48 /*
49 * Determine which channel of a SCC a device referenced by a full device
50 * path or as an alias is (in the latter case we try to look up the device
51 * path via the /aliases node).
52 * Only the device paths of devices which are used for TTYs really allow
53 * to do this as they look like these (taken from /aliases nodes):
54 * ttya: '/central/fhc/zs@0,902000:a'
55 * ttyc: '/pci@1f,0/pci@1,1/ebus@1/se@14,400000:a'
56 * Additionally, for device paths of SCCs which are connected to a RSC
57 * (Remote System Control) device we can hardcode the appropriate channel.
58 * Such device paths look like these:
59 * rsc: '/pci@1f,4000/ebus@1/se@14,200000:ssp'
60 * ttyc: '/pci@1f,4000/ebus@1/se@14,200000:ssp'
61 */
62 static int
63 uart_cpu_channel(char *dev)
64 {
65 char alias[64];
66 phandle_t aliases;
67 int len;
68 const char *p;
69
70 strcpy(alias, dev);
71 if ((aliases = OF_finddevice("/aliases")) != -1)
72 (void)OF_getprop(aliases, dev, alias, sizeof(alias));
73 len = strlen(alias);
74 if ((p = rindex(alias, ':')) == NULL)
75 return (0);
76 p++;
77 if (p - alias == len - 1 && (*p == 'a' || *p == 'b'))
78 return (*p - 'a' + 1);
79 if (strcmp(p, "ssp") == 0)
80 return (1);
81 return (0);
82 }
83
84 int
85 uart_cpu_eqres(struct uart_bas *b1, struct uart_bas *b2)
86 {
87
88 return ((b1->bsh == b2->bsh) ? 1 : 0);
89 }
90
91 /*
92 * Get the package handle of the UART that is selected as the console, if
93 * the console is an UART of course. Note that we enforce that both input
94 * and output are selected.
95 * Note that the currently active console (i.e. /chosen/stdout and
96 * /chosen/stdin) may not be the same as the device selected in the
97 * environment (ie /options/output-device and /options/input-device) because
98 * keyboard and screen were selected but the keyboard was unplugged or the
99 * user has changed the environment. In the latter case I would assume that
100 * the user expects that FreeBSD uses the new console setting.
101 * For weirder configurations, use ofw_console(4).
102 */
103 static phandle_t
104 uart_cpu_getdev_console(phandle_t options, char *dev, size_t devsz)
105 {
106 char buf[sizeof("serial")];
107 ihandle_t inst;
108 phandle_t chosen, input, output;
109
110 if (OF_getprop(options, "input-device", dev, devsz) == -1)
111 return (-1);
112 input = OF_finddevice(dev);
113 if (OF_getprop(options, "output-device", dev, devsz) == -1)
114 return (-1);
115 output = OF_finddevice(dev);
116 if (input == -1 || output == -1 ||
117 OF_getproplen(input, "keyboard") >= 0) {
118 if ((chosen = OF_finddevice("/chosen")) == -1)
119 return (-1);
120 if (OF_getprop(chosen, "stdin", &inst, sizeof(inst)) == -1)
121 return (-1);
122 if ((input = OF_instance_to_package(inst)) == -1)
123 return (-1);
124 if (OF_getprop(chosen, "stdout", &inst, sizeof(inst)) == -1)
125 return (-1);
126 if ((output = OF_instance_to_package(inst)) == -1)
127 return (-1);
128 snprintf(dev, devsz, "ttya");
129 }
130 if (input != output)
131 return (-1);
132 if (OF_getprop(input, "device_type", buf, sizeof(buf)) == -1)
133 return (-1);
134 if (strcmp(buf, "serial") != 0)
135 return (-1);
136 /* For a Serengeti console device point to the bootbus controller. */
137 if (OF_getprop(input, "name", buf, sizeof(buf)) > 0 &&
138 !strcmp(buf, "sgcn")) {
139 if ((chosen = OF_finddevice("/chosen")) == -1)
140 return (-1);
141 if (OF_getprop(chosen, "iosram", &input, sizeof(input)) == -1)
142 return (-1);
143 }
144 return (input);
145 }
146
147 /*
148 * Get the package handle of the UART that's selected as the debug port.
149 * Since there's no place for this in the OF, we use the kernel environment
150 * variable "hw.uart.dbgport". Note however that the variable is not a
151 * list of attributes. It's single device name or alias, as known by
152 * the OF.
153 */
154 static phandle_t
155 uart_cpu_getdev_dbgport(char *dev, size_t devsz)
156 {
157 char buf[sizeof("serial")];
158 phandle_t input;
159
160 if (!getenv_string("hw.uart.dbgport", dev, devsz))
161 return (-1);
162 if ((input = OF_finddevice(dev)) == -1)
163 return (-1);
164 if (OF_getprop(input, "device_type", buf, sizeof(buf)) == -1)
165 return (-1);
166 if (strcmp(buf, "serial") != 0)
167 return (-1);
168 return (input);
169 }
170
171 /*
172 * Get the package handle of the UART that is selected as the keyboard port,
173 * if it's actually used to connect the keyboard according to the OF. I.e.
174 * this will return the UART used to connect the keyboard regardless whether
175 * it's stdin or not, however not in case the user or the OF gave preference
176 * to e.g. a PS/2 keyboard by setting /aliases/keyboard accordingly.
177 */
178 static phandle_t
179 uart_cpu_getdev_keyboard(char *dev, size_t devsz)
180 {
181 char buf[sizeof("serial")];
182 phandle_t input;
183
184 if ((input = OF_finddevice("keyboard")) == -1)
185 return (-1);
186 if (OF_getprop(input, "device_type", buf, sizeof(buf)) == -1)
187 return (-1);
188 if (strcmp(buf, "serial") != 0)
189 return (-1);
190 if (OF_getprop(input, "name", dev, devsz) == -1)
191 return (-1);
192 /*
193 * So far this also matched PS/2 keyboard nodes so make sure it's
194 * one of the SCCs/UARTs known to be used to connect keyboards.
195 */
196 if (strcmp(dev, "su") && strcmp(dev, "su_pnp") && strcmp(dev, "zs"))
197 return (-1);
198 return (input);
199 }
200
201 int
202 uart_cpu_getdev(int devtype, struct uart_devinfo *di)
203 {
204 char buf[32], compat[32], dev[64];
205 struct uart_class *class;
206 phandle_t input, options;
207 bus_addr_t addr;
208 int baud, bits, error, range, space, stop;
209 char flag, par;
210
211 if ((options = OF_finddevice("/options")) == -1)
212 return (ENXIO);
213 switch (devtype) {
214 case UART_DEV_CONSOLE:
215 input = uart_cpu_getdev_console(options, dev, sizeof(dev));
216 break;
217 case UART_DEV_DBGPORT:
218 input = uart_cpu_getdev_dbgport(dev, sizeof(dev));
219 break;
220 case UART_DEV_KEYBOARD:
221 input = uart_cpu_getdev_keyboard(dev, sizeof(dev));
222 break;
223 default:
224 input = -1;
225 break;
226 }
227 if (input == -1)
228 return (ENXIO);
229 error = OF_decode_addr(input, 0, &space, &addr);
230 if (error)
231 return (error);
232
233 /* Get the device class. */
234 if (OF_getprop(input, "name", buf, sizeof(buf)) == -1)
235 return (ENXIO);
236 if (OF_getprop(input, "compatible", compat, sizeof(compat)) == -1)
237 compat[0] = '\0';
238 di->bas.regshft = 0;
239 di->bas.rclk = 0;
240 class = NULL;
241 if (!strcmp(buf, "se") || !strcmp(buf, "FJSV,se") ||
242 !strcmp(compat, "sab82532")) {
243 class = &uart_sab82532_class;
244 /* SAB82532 are only known to be used for TTYs. */
245 if ((di->bas.chan = uart_cpu_channel(dev)) == 0)
246 return (ENXIO);
247 addr += uart_getrange(class) * (di->bas.chan - 1);
248 } else if (!strcmp(buf, "zs")) {
249 class = &uart_z8530_class;
250 if ((di->bas.chan = uart_cpu_channel(dev)) == 0) {
251 /*
252 * There's no way to determine from OF which
253 * channel has the keyboard. Should always be
254 * on channel 1 however.
255 */
256 if (devtype == UART_DEV_KEYBOARD)
257 di->bas.chan = 1;
258 else
259 return (ENXIO);
260 }
261 di->bas.regshft = 1;
262 range = uart_getrange(class) << di->bas.regshft;
263 addr += range - range * (di->bas.chan - 1);
264 } else if (!strcmp(buf, "lom-console") || !strcmp(buf, "su") ||
265 !strcmp(buf, "su_pnp") || !strcmp(compat, "rsc-console") ||
266 !strcmp(compat, "su") || !strcmp(compat, "su16550") ||
267 !strcmp(compat, "su16552")) {
268 class = &uart_ns8250_class;
269 di->bas.chan = 0;
270 } else if (!strcmp(compat, "sgsbbc")) {
271 class = &uart_sbbc_class;
272 di->bas.chan = 0;
273 }
274 if (class == NULL)
275 return (ENXIO);
276
277 /* Fill in the device info. */
278 di->ops = uart_getops(class);
279 di->bas.bst = &bst_store[devtype];
280 di->bas.bsh = sparc64_fake_bustag(space, addr, di->bas.bst);
281
282 /* Get the line settings. */
283 if (devtype == UART_DEV_KEYBOARD)
284 di->baudrate = 1200;
285 else if (!strcmp(compat, "rsc-console"))
286 di->baudrate = 115200;
287 else
288 di->baudrate = 9600;
289 di->databits = 8;
290 di->stopbits = 1;
291 di->parity = UART_PARITY_NONE;
292 snprintf(buf, sizeof(buf), "%s-mode", dev);
293 if (OF_getprop(options, buf, buf, sizeof(buf)) == -1 &&
294 OF_getprop(input, "ssp-console-modes", buf, sizeof(buf)) == -1)
295 return (0);
296 if (sscanf(buf, "%d,%d,%c,%d,%c", &baud, &bits, &par, &stop, &flag)
297 != 5)
298 return (0);
299 di->baudrate = baud;
300 di->databits = bits;
301 di->stopbits = stop;
302 di->parity = (par == 'n') ? UART_PARITY_NONE :
303 (par == 'o') ? UART_PARITY_ODD : UART_PARITY_EVEN;
304 return (0);
305 }
Cache object: 10880f226f4b4d982b2b3f379d2fc8ce
|