uart_cpu_sparc64.c revision 206451
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: head/sys/dev/uart/uart_cpu_sparc64.c 206451 2010-04-10 11:52:12Z marius $"); 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 43bus_space_tag_t uart_bus_space_io; 44bus_space_tag_t uart_bus_space_mem; 45 46static 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 */ 62static int 63uart_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 84int 85uart_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 */ 103static phandle_t 104uart_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 */ 154static phandle_t 155uart_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 */ 178static phandle_t 179uart_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 201int 202uart_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(compat, "sab82532")) { 242 class = &uart_sab82532_class; 243 /* SAB82532 are only known to be used for TTYs. */ 244 if ((di->bas.chan = uart_cpu_channel(dev)) == 0) 245 return (ENXIO); 246 addr += uart_getrange(class) * (di->bas.chan - 1); 247 } else if (!strcmp(buf, "zs")) { 248 class = &uart_z8530_class; 249 if ((di->bas.chan = uart_cpu_channel(dev)) == 0) { 250 /* 251 * There's no way to determine from OF which 252 * channel has the keyboard. Should always be 253 * on channel 1 however. 254 */ 255 if (devtype == UART_DEV_KEYBOARD) 256 di->bas.chan = 1; 257 else 258 return (ENXIO); 259 } 260 di->bas.regshft = 1; 261 range = uart_getrange(class) << di->bas.regshft; 262 addr += range - range * (di->bas.chan - 1); 263 } else if (!strcmp(buf, "lom-console") || !strcmp(buf, "su") || 264 !strcmp(buf, "su_pnp") || !strcmp(compat, "rsc-console") || 265 !strcmp(compat, "su") || !strcmp(compat, "su16550") || 266 !strcmp(compat, "su16552")) { 267 class = &uart_ns8250_class; 268 di->bas.chan = 0; 269 } else if (!strcmp(compat, "sgsbbc")) { 270 class = &uart_sbbc_class; 271 di->bas.chan = 0; 272 } 273 if (class == NULL) 274 return (ENXIO); 275 276 /* Fill in the device info. */ 277 di->ops = uart_getops(class); 278 di->bas.bst = &bst_store[devtype]; 279 di->bas.bsh = sparc64_fake_bustag(space, addr, di->bas.bst); 280 281 /* Get the line settings. */ 282 if (devtype == UART_DEV_KEYBOARD) 283 di->baudrate = 1200; 284 else if (!strcmp(compat, "rsc-console")) 285 di->baudrate = 115200; 286 else 287 di->baudrate = 9600; 288 di->databits = 8; 289 di->stopbits = 1; 290 di->parity = UART_PARITY_NONE; 291 snprintf(buf, sizeof(buf), "%s-mode", dev); 292 if (OF_getprop(options, buf, buf, sizeof(buf)) == -1 && 293 OF_getprop(input, "ssp-console-modes", buf, sizeof(buf)) == -1) 294 return (0); 295 if (sscanf(buf, "%d,%d,%c,%d,%c", &baud, &bits, &par, &stop, &flag) 296 != 5) 297 return (0); 298 di->baudrate = baud; 299 di->databits = bits; 300 di->stopbits = stop; 301 di->parity = (par == 'n') ? UART_PARITY_NONE : 302 (par == 'o') ? UART_PARITY_ODD : UART_PARITY_EVEN; 303 return (0); 304} 305