uart_cpu_sparc64.c revision 146972
1163517Simp/*- 2163517Simp * Copyright (c) 2003, 2004 Marcel Moolenaar 3163517Simp * All rights reserved. 4163517Simp * 5163517Simp * Redistribution and use in source and binary forms, with or without 6163517Simp * modification, are permitted provided that the following conditions 7163517Simp * are met: 8163517Simp * 9163517Simp * 1. Redistributions of source code must retain the above copyright 10163517Simp * notice, this list of conditions and the following disclaimer. 11163517Simp * 2. Redistributions in binary form must reproduce the above copyright 12163517Simp * notice, this list of conditions and the following disclaimer in the 13163517Simp * documentation and/or other materials provided with the distribution. 14185265Simp * 15185265Simp * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16185265Simp * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17185265Simp * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18185265Simp * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19185265Simp * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20185265Simp * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21185265Simp * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22185265Simp * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23185265Simp * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24185265Simp * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25163517Simp */ 26163517Simp 27163517Simp#include <sys/cdefs.h> 28163517Simp__FBSDID("$FreeBSD: head/sys/dev/uart/uart_cpu_sparc64.c 146972 2005-06-04 21:33:18Z marius $"); 29172739Simp 30172739Simp#include <sys/param.h> 31163517Simp#include <sys/systm.h> 32163517Simp 33163517Simp#include <machine/bus.h> 34163517Simp#include <machine/bus_private.h> 35163517Simp 36163517Simp#include <dev/ofw/openfirm.h> 37163517Simp#include <machine/ofw_machdep.h> 38163517Simp 39163517Simp#include <dev/uart/uart.h> 40163517Simp#include <dev/uart/uart_cpu.h> 41163517Simp 42163517Simpbus_space_tag_t uart_bus_space_io; 43163517Simpbus_space_tag_t uart_bus_space_mem; 44163517Simp 45163517Simpstatic struct bus_space_tag bst_store[3]; 46163517Simp 47163517Simp/* 48163517Simp * Determine which channel of a SCC a device referenced by an alias is. 49163517Simp * The information present in the OF device tree only allows to do this 50163517Simp * for "ttyX" aliases. If a device is a channel of a SCC its property 51163517Simp * in the /aliases node looks like one of these: 52163517Simp * ttya: '/central/fhc/zs@0,902000:a' 53163517Simp * ttyc: '/pci@1f,0/pci@1,1/ebus@1/se@14,400000:a' 54163517Simp */ 55163517Simpstatic int 56163517Simpuart_cpu_channel(char *dev) 57234560Smarius{ 58234560Smarius char alias[64]; 59234560Smarius phandle_t aliases; 60163517Simp int len; 61163517Simp 62163517Simp strcpy(alias, dev); 63163517Simp if ((aliases = OF_finddevice("/aliases")) != -1) 64163517Simp OF_getprop(aliases, dev, alias, sizeof(alias)); 65163517Simp len = strlen(alias); 66163517Simp if (len < 2 || alias[len - 2] != ':' || alias[len - 1] < 'a' || 67163517Simp alias[len - 1] > 'b') 68163517Simp return (0); 69163517Simp return (alias[len - 1] - 'a' + 1); 70163517Simp} 71163517Simp 72163517Simpint 73163517Simpuart_cpu_eqres(struct uart_bas *b1, struct uart_bas *b2) 74163517Simp{ 75163517Simp 76163517Simp return ((b1->bsh == b2->bsh) ? 1 : 0); 77163517Simp} 78163517Simp 79163517Simp/* 80163517Simp * Get the package handle of the UART that is selected as the console, if 81163517Simp * the console is an UART of course. Note that we enforce that both stdin 82163517Simp * and stdout are selected. 83163517Simp * Note that the currently active console (i.e. /chosen/stdout and 84163517Simp * /chosen/stdin) may not be the same as the device selected in the 85163517Simp * environment (ie /options/output-device and /options/input-device) because 86163517Simp * keyboard and screen were selected but the keyboard was unplugged or the 87163517Simp * user has changed the environment. In the latter case I would assume that 88163517Simp * the user expects that FreeBSD uses the new console setting. 89163517Simp * For weirder configurations, use ofw_console(4). 90163517Simp */ 91163517Simpstatic phandle_t 92163517Simpuart_cpu_getdev_console(phandle_t options, char *dev, size_t devsz) 93163517Simp{ 94163517Simp char buf[32]; 95163517Simp ihandle_t stdin, stdout; 96163517Simp phandle_t chosen, input; 97163517Simp 98163517Simp if (OF_getprop(options, "input-device", dev, devsz) == -1) 99163517Simp return (-1); 100163517Simp if (OF_getprop(options, "output-device", buf, sizeof(buf)) == -1) 101163517Simp return (-1); 102163517Simp if (!strcmp(dev, "keyboard") && !strcmp(buf, "screen")) { 103163517Simp if ((chosen = OF_finddevice("/chosen")) == -1) 104163517Simp return (-1); 105163517Simp if (OF_getprop(chosen, "stdin", &stdin, sizeof(stdin)) == -1) 106163517Simp return (-1); 107163517Simp if ((input = OF_instance_to_package(stdin)) == -1) 108163517Simp return (-1); 109163517Simp if (OF_getprop(chosen, "stdout", &stdout, sizeof(stdout)) == -1) 110163517Simp return (-1); 111163517Simp if (OF_instance_to_package(stdout) != input) 112163517Simp return (-1); 113163517Simp snprintf(dev, devsz, "ttya"); 114163517Simp } else { 115163517Simp if ((input = OF_finddevice(dev)) == -1) 116163517Simp return (-1); 117163517Simp if (OF_finddevice(buf) != input) 118163517Simp return (-1); 119163517Simp } 120163517Simp if (OF_getprop(input, "device_type", buf, sizeof(buf)) == -1) 121270006Simp return (-1); 122270006Simp if (strcmp(buf, "serial") != 0) 123270006Simp return (-1); 124270006Simp return (input); 125270006Simp} 126270006Simp 127270006Simp/* 128270006Simp * Get the package handle of the UART that's selected as the debug port. 129270006Simp * Since there's no place for this in the OF, we use the kernel environment 130270006Simp * variable "hw.uart.dbgport". Note however that the variable is not a 131270006Simp * list of attributes. It's single device name or alias, as known by 132270006Simp * the OF. 133270006Simp */ 134270006Simpstatic phandle_t 135270006Simpuart_cpu_getdev_dbgport(phandle_t options, char *dev, size_t devsz) 136270006Simp{ 137270006Simp char buf[32]; 138270006Simp phandle_t input; 139270006Simp 140270006Simp if (!getenv_string("hw.uart.dbgport", dev, devsz)) 141270006Simp return (-1); 142270006Simp if ((input = OF_finddevice(dev)) == -1) 143270006Simp return (-1); 144270006Simp if (OF_getprop(input, "device_type", buf, sizeof(buf)) == -1) 145163517Simp return (-1); 146163517Simp if (strcmp(buf, "serial") != 0) 147163517Simp return (-1); 148163517Simp return (input); 149163517Simp} 150163517Simp 151163517Simp/* 152163517Simp * Get the package handle of the UART that is selected as the keyboard port, 153163517Simp * if it's actually used to connect the keyboard according to the OF. I.e. 154163517Simp * this will return the UART used to connect the keyboard regardless whether 155163517Simp * it's stdin or not, however not in case the user or the OF gave preference 156172739Simp * to e.g. a PS/2 keyboard by setting /aliases/keyboard accordingly. 157 */ 158static phandle_t 159uart_cpu_getdev_keyboard(char *dev, size_t devsz) 160{ 161 char buf[32]; 162 phandle_t input; 163 164 if ((input = OF_finddevice("keyboard")) == -1) 165 return (-1); 166 if (OF_getprop(input, "device_type", buf, sizeof(buf)) == -1) 167 return (-1); 168 if (strcmp(buf, "serial") != 0) 169 return (-1); 170 if (OF_getprop(input, "name", dev, devsz) == -1) 171 return (-1); 172 /* 173 * So far this also matched PS/2 keyboard nodes so make sure it's 174 * one of the SCCs/UARTs known to be used to connect keyboards. 175 */ 176 if (strcmp(dev, "su") && strcmp(dev, "su_pnp") && strcmp(dev, "zs")) 177 return (-1); 178 return (input); 179} 180 181int 182uart_cpu_getdev(int devtype, struct uart_devinfo *di) 183{ 184 char buf[32], dev[32], compat[32]; 185 phandle_t input, options; 186 bus_addr_t addr; 187 int baud, bits, error, space, stop; 188 char flag, par; 189 190 if ((options = OF_finddevice("/options")) == -1) 191 return (ENXIO); 192 switch (devtype) { 193 case UART_DEV_CONSOLE: 194 input = uart_cpu_getdev_console(options, dev, sizeof(dev)); 195 break; 196 case UART_DEV_DBGPORT: 197 input = uart_cpu_getdev_dbgport(options, dev, sizeof(dev)); 198 break; 199 case UART_DEV_KEYBOARD: 200 input = uart_cpu_getdev_keyboard(dev, sizeof(dev)); 201 break; 202 default: 203 input = -1; 204 break; 205 } 206 if (input == -1) 207 return (ENXIO); 208 error = OF_decode_addr(input, 0, &space, &addr); 209 if (error) 210 return (error); 211 212 /* Get the device class. */ 213 if (OF_getprop(input, "name", buf, sizeof(buf)) == -1) 214 return (ENXIO); 215 if (OF_getprop(input, "compatible", compat, sizeof(compat)) == -1) 216 compat[0] = '\0'; 217 di->bas.regshft = 0; 218 di->bas.rclk = 0; 219 if (!strcmp(buf, "se")) { 220 di->ops = uart_sab82532_ops; 221 /* SAB82532 are only known to be used for TTYs. */ 222 if ((di->bas.chan = uart_cpu_channel(dev)) == 0) 223 return (ENXIO); 224 addr += 64 * (di->bas.chan - 1); 225 } else if (!strcmp(buf, "zs")) { 226 di->ops = uart_z8530_ops; 227 if ((di->bas.chan = uart_cpu_channel(dev)) == 0) { 228 /* 229 * There's no way to determine from OF which 230 * channel has the keyboard. Should always be 231 * on channel 1 however. 232 */ 233 if (devtype == UART_DEV_KEYBOARD) 234 di->bas.chan = 1; 235 else 236 return (ENXIO); 237 } 238 di->bas.regshft = 1; 239 addr += 4 - 4 * (di->bas.chan - 1); 240 } else if (!strcmp(buf, "su") || !strcmp(buf, "su_pnp") || 241 !strcmp(compat, "su") || !strcmp(compat, "su16550")) { 242 di->ops = uart_ns8250_ops; 243 di->bas.chan = 0; 244 } else 245 return (ENXIO); 246 247 /* Fill in the device info. */ 248 di->bas.bst = &bst_store[devtype]; 249 di->bas.bsh = sparc64_fake_bustag(space, addr, di->bas.bst); 250 251 /* Get the line settings. */ 252 if (devtype == UART_DEV_KEYBOARD) 253 di->baudrate = 1200; 254 else 255 di->baudrate = 9600; 256 di->databits = 8; 257 di->stopbits = 1; 258 di->parity = UART_PARITY_NONE; 259 snprintf(buf, sizeof(buf), "%s-mode", dev); 260 if (OF_getprop(options, buf, buf, sizeof(buf)) == -1) 261 return (0); 262 if (sscanf(buf, "%d,%d,%c,%d,%c", &baud, &bits, &par, &stop, &flag) 263 != 5) 264 return (0); 265 di->baudrate = baud; 266 di->databits = bits; 267 di->stopbits = stop; 268 di->parity = (par == 'n') ? UART_PARITY_NONE : 269 (par == 'o') ? UART_PARITY_ODD : UART_PARITY_EVEN; 270 return (0); 271} 272