]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - sys/dev/uart/uart_cpu_sparc64.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / sys / dev / uart / uart_cpu_sparc64.c
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         return (input);
137 }
138
139 /*
140  * Get the package handle of the UART that's selected as the debug port.
141  * Since there's no place for this in the OF, we use the kernel environment
142  * variable "hw.uart.dbgport". Note however that the variable is not a
143  * list of attributes. It's single device name or alias, as known by
144  * the OF.
145  */
146 static phandle_t
147 uart_cpu_getdev_dbgport(char *dev, size_t devsz)
148 {
149         char buf[sizeof("serial")];
150         phandle_t input;
151
152         if (!getenv_string("hw.uart.dbgport", dev, devsz))
153                 return (-1);
154         if ((input = OF_finddevice(dev)) == -1)
155                 return (-1);
156         if (OF_getprop(input, "device_type", buf, sizeof(buf)) == -1)
157                 return (-1);
158         if (strcmp(buf, "serial") != 0)
159                 return (-1);
160         return (input);
161 }
162
163 /*
164  * Get the package handle of the UART that is selected as the keyboard port,
165  * if it's actually used to connect the keyboard according to the OF. I.e.
166  * this will return the UART used to connect the keyboard regardless whether
167  * it's stdin or not, however not in case the user or the OF gave preference
168  * to e.g. a PS/2 keyboard by setting /aliases/keyboard accordingly.
169  */
170 static phandle_t
171 uart_cpu_getdev_keyboard(char *dev, size_t devsz)
172 {
173         char buf[sizeof("serial")];
174         phandle_t input;
175
176         if ((input = OF_finddevice("keyboard")) == -1)
177                 return (-1);
178         if (OF_getprop(input, "device_type", buf, sizeof(buf)) == -1)
179                 return (-1);
180         if (strcmp(buf, "serial") != 0)
181                 return (-1);
182         if (OF_getprop(input, "name", dev, devsz) == -1)
183                 return (-1);
184         /*
185          * So far this also matched PS/2 keyboard nodes so make sure it's
186          * one of the SCCs/UARTs known to be used to connect keyboards.
187          */
188         if (strcmp(dev, "su") && strcmp(dev, "su_pnp") && strcmp(dev, "zs"))
189                 return (-1);
190         return (input);
191 }
192
193 int
194 uart_cpu_getdev(int devtype, struct uart_devinfo *di)
195 {
196         char buf[32], compat[32], dev[64];
197         struct uart_class *class;
198         phandle_t input, options;
199         bus_addr_t addr;
200         int baud, bits, error, range, space, stop;
201         char flag, par;
202
203         if ((options = OF_finddevice("/options")) == -1)
204                 return (ENXIO);
205         switch (devtype) {
206         case UART_DEV_CONSOLE:
207                 input = uart_cpu_getdev_console(options, dev, sizeof(dev));
208                 break;
209         case UART_DEV_DBGPORT:
210                 input = uart_cpu_getdev_dbgport(dev, sizeof(dev));
211                 break;
212         case UART_DEV_KEYBOARD:
213                 input = uart_cpu_getdev_keyboard(dev, sizeof(dev));
214                 break;
215         default:
216                 input = -1;
217                 break;
218         }
219         if (input == -1)
220                 return (ENXIO);
221         error = OF_decode_addr(input, 0, &space, &addr);
222         if (error)
223                 return (error);
224
225         /* Get the device class. */
226         if (OF_getprop(input, "name", buf, sizeof(buf)) == -1)
227                 return (ENXIO);
228         if (OF_getprop(input, "compatible", compat, sizeof(compat)) == -1)
229                 compat[0] = '\0';
230         di->bas.regshft = 0;
231         di->bas.rclk = 0;
232         class = NULL;
233         if (!strcmp(buf, "se") || !strcmp(compat, "sab82532")) {
234                 class = &uart_sab82532_class;
235                 /* SAB82532 are only known to be used for TTYs. */
236                 if ((di->bas.chan = uart_cpu_channel(dev)) == 0)
237                         return (ENXIO);
238                 addr += uart_getrange(class) * (di->bas.chan - 1);
239         } else if (!strcmp(buf, "zs")) {
240                 class = &uart_z8530_class;
241                 if ((di->bas.chan = uart_cpu_channel(dev)) == 0) {
242                         /*
243                          * There's no way to determine from OF which
244                          * channel has the keyboard. Should always be
245                          * on channel 1 however.
246                          */
247                         if (devtype == UART_DEV_KEYBOARD)
248                                 di->bas.chan = 1;
249                         else
250                                 return (ENXIO);
251                 }
252                 di->bas.regshft = 1;
253                 range = uart_getrange(class) << di->bas.regshft;
254                 addr += range - range * (di->bas.chan - 1);
255         } else if (!strcmp(buf, "lom-console") || !strcmp(buf, "su") ||
256             !strcmp(buf, "su_pnp") || !strcmp(compat, "rsc-console") ||
257             !strcmp(compat, "su") || !strcmp(compat, "su16550") ||
258             !strcmp(compat, "su16552")) {
259                 class = &uart_ns8250_class;
260                 di->bas.chan = 0;
261         }
262         if (class == NULL)
263                 return (ENXIO);
264
265         /* Fill in the device info. */
266         di->ops = uart_getops(class);
267         di->bas.bst = &bst_store[devtype];
268         di->bas.bsh = sparc64_fake_bustag(space, addr, di->bas.bst);
269
270         /* Get the line settings. */
271         if (devtype == UART_DEV_KEYBOARD)
272                 di->baudrate = 1200;
273         else if (!strcmp(compat, "rsc-console"))
274                 di->baudrate = 115200;
275         else
276                 di->baudrate = 9600;
277         di->databits = 8;
278         di->stopbits = 1;
279         di->parity = UART_PARITY_NONE;
280         snprintf(buf, sizeof(buf), "%s-mode", dev);
281         if (OF_getprop(options, buf, buf, sizeof(buf)) == -1 &&
282             OF_getprop(input, "ssp-console-modes", buf, sizeof(buf)) == -1)
283                 return (0);
284         if (sscanf(buf, "%d,%d,%c,%d,%c", &baud, &bits, &par, &stop, &flag)
285             != 5)
286                 return (0);
287         di->baudrate = baud;
288         di->databits = bits;
289         di->stopbits = stop;
290         di->parity = (par == 'n') ? UART_PARITY_NONE :
291             (par == 'o') ? UART_PARITY_ODD : UART_PARITY_EVEN;
292         return (0);
293 }