uart_cpu_sparc64.c revision 155322
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 155322 2006-02-04 23:27:16Z 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 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 */ 146static phandle_t 147uart_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 */ 170static phandle_t 171uart_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 193int 194uart_cpu_getdev(int devtype, struct uart_devinfo *di) 195{ 196 char buf[32], compat[32], dev[64]; 197 phandle_t input, options; 198 bus_addr_t addr; 199 int baud, bits, error, space, stop; 200 char flag, par; 201 202 if ((options = OF_finddevice("/options")) == -1) 203 return (ENXIO); 204 switch (devtype) { 205 case UART_DEV_CONSOLE: 206 input = uart_cpu_getdev_console(options, dev, sizeof(dev)); 207 break; 208 case UART_DEV_DBGPORT: 209 input = uart_cpu_getdev_dbgport(dev, sizeof(dev)); 210 break; 211 case UART_DEV_KEYBOARD: 212 input = uart_cpu_getdev_keyboard(dev, sizeof(dev)); 213 break; 214 default: 215 input = -1; 216 break; 217 } 218 if (input == -1) 219 return (ENXIO); 220 error = OF_decode_addr(input, 0, &space, &addr); 221 if (error) 222 return (error); 223 224 /* Get the device class. */ 225 if (OF_getprop(input, "name", buf, sizeof(buf)) == -1) 226 return (ENXIO); 227 if (OF_getprop(input, "compatible", compat, sizeof(compat)) == -1) 228 compat[0] = '\0'; 229 di->bas.regshft = 0; 230 di->bas.rclk = 0; 231 if (!strcmp(buf, "se") || !strcmp(compat, "sab82532")) { 232 di->ops = uart_sab82532_ops; 233 /* SAB82532 are only known to be used for TTYs. */ 234 if ((di->bas.chan = uart_cpu_channel(dev)) == 0) 235 return (ENXIO); 236 addr += 64 * (di->bas.chan - 1); 237 } else if (!strcmp(buf, "zs")) { 238 di->ops = uart_z8530_ops; 239 if ((di->bas.chan = uart_cpu_channel(dev)) == 0) { 240 /* 241 * There's no way to determine from OF which 242 * channel has the keyboard. Should always be 243 * on channel 1 however. 244 */ 245 if (devtype == UART_DEV_KEYBOARD) 246 di->bas.chan = 1; 247 else 248 return (ENXIO); 249 } 250 di->bas.regshft = 1; 251 addr += 4 - 4 * (di->bas.chan - 1); 252 } else if (!strcmp(buf, "lom-console") || !strcmp(buf, "su") || 253 !strcmp(buf, "su_pnp") || !strcmp(compat, "rsc-console") || 254 !strcmp(compat, "su") || !strcmp(compat, "su16550")) { 255 di->ops = uart_ns8250_ops; 256 di->bas.chan = 0; 257 } else 258 return (ENXIO); 259 260 /* Fill in the device info. */ 261 di->bas.bst = &bst_store[devtype]; 262 di->bas.bsh = sparc64_fake_bustag(space, addr, di->bas.bst); 263 264 /* Get the line settings. */ 265 if (devtype == UART_DEV_KEYBOARD) 266 di->baudrate = 1200; 267 else if (!strcmp(compat, "rsc-console")) 268 di->baudrate = 115200; 269 else 270 di->baudrate = 9600; 271 di->databits = 8; 272 di->stopbits = 1; 273 di->parity = UART_PARITY_NONE; 274 snprintf(buf, sizeof(buf), "%s-mode", dev); 275 if (OF_getprop(options, buf, buf, sizeof(buf)) == -1 && 276 OF_getprop(input, "ssp-console-modes", buf, sizeof(buf)) == -1) 277 return (0); 278 if (sscanf(buf, "%d,%d,%c,%d,%c", &baud, &bits, &par, &stop, &flag) 279 != 5) 280 return (0); 281 di->baudrate = baud; 282 di->databits = bits; 283 di->stopbits = stop; 284 di->parity = (par == 'n') ? UART_PARITY_NONE : 285 (par == 'o') ? UART_PARITY_ODD : UART_PARITY_EVEN; 286 return (0); 287} 288