uart_cpu_sparc64.c revision 141753
1139749Simp/*- 2127215Smarcel * Copyright (c) 2003, 2004 Marcel Moolenaar 3119815Smarcel * All rights reserved. 4119815Smarcel * 5119815Smarcel * Redistribution and use in source and binary forms, with or without 6119815Smarcel * modification, are permitted provided that the following conditions 7119815Smarcel * are met: 8119815Smarcel * 9119815Smarcel * 1. Redistributions of source code must retain the above copyright 10119815Smarcel * notice, this list of conditions and the following disclaimer. 11119815Smarcel * 2. Redistributions in binary form must reproduce the above copyright 12119815Smarcel * notice, this list of conditions and the following disclaimer in the 13119815Smarcel * documentation and/or other materials provided with the distribution. 14119815Smarcel * 15119815Smarcel * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16119815Smarcel * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17119815Smarcel * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18119815Smarcel * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19119815Smarcel * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20119815Smarcel * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21119815Smarcel * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22119815Smarcel * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23119815Smarcel * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24119815Smarcel * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25119815Smarcel */ 26119815Smarcel 27119815Smarcel#include <sys/cdefs.h> 28119815Smarcel__FBSDID("$FreeBSD: head/sys/dev/uart/uart_cpu_sparc64.c 141753 2005-02-12 19:13:51Z marius $"); 29119815Smarcel 30119815Smarcel#include <sys/param.h> 31119815Smarcel#include <sys/systm.h> 32119815Smarcel 33119815Smarcel#include <machine/bus.h> 34119815Smarcel#include <machine/bus_private.h> 35119815Smarcel 36119815Smarcel#include <dev/ofw/openfirm.h> 37119815Smarcel#include <machine/ofw_machdep.h> 38119815Smarcel 39119815Smarcel#include <dev/uart/uart.h> 40119815Smarcel#include <dev/uart/uart_cpu.h> 41119815Smarcel 42127215Smarcelbus_space_tag_t uart_bus_space_io; 43127215Smarcelbus_space_tag_t uart_bus_space_mem; 44127215Smarcel 45119815Smarcelstatic struct bus_space_tag bst_store[3]; 46119815Smarcel 47138157Smarius/* 48138157Smarius * Determine which channel of a SCC a device referenced by an alias is. 49138157Smarius * The information present in the OF device tree only allows to do this 50138157Smarius * for "ttyX" aliases. If a device is a channel of a SCC its property 51138157Smarius * in the /aliases node looks like one of these: 52138157Smarius * ttya: '/central/fhc/zs@0,902000:a' 53138157Smarius * ttyc: '/pci@1f,0/pci@1,1/ebus@1/se@14,400000:a' 54138157Smarius */ 55119815Smarcelstatic int 56119815Smarceluart_cpu_channel(char *dev) 57119815Smarcel{ 58119815Smarcel char alias[64]; 59119815Smarcel phandle_t aliases; 60119815Smarcel int len; 61119815Smarcel 62119815Smarcel strcpy(alias, dev); 63119815Smarcel if ((aliases = OF_finddevice("/aliases")) != -1) 64119815Smarcel OF_getprop(aliases, dev, alias, sizeof(alias)); 65119815Smarcel len = strlen(alias); 66119815Smarcel if (len < 2 || alias[len - 2] != ':' || alias[len - 1] < 'a' || 67119815Smarcel alias[len - 1] > 'b') 68119815Smarcel return (0); 69120452Smarcel return (alias[len - 1] - 'a' + 1); 70119815Smarcel} 71119815Smarcel 72119815Smarcelint 73119866Smarceluart_cpu_eqres(struct uart_bas *b1, struct uart_bas *b2) 74119866Smarcel{ 75119866Smarcel 76119866Smarcel return ((b1->bsh == b2->bsh) ? 1 : 0); 77119866Smarcel} 78119866Smarcel 79127741Smarcel/* 80138157Smarius * Get the package handle of the UART that is selected as the console, if 81138157Smarius * the console is an UART of course. Note that we enforce that both stdin 82138157Smarius * and stdout are selected. 83127741Smarcel * Note that the currently active console (i.e. /chosen/stdout and 84127741Smarcel * /chosen/stdin) may not be the same as the device selected in the 85127741Smarcel * environment (ie /options/output-device and /options/input-device) because 86127825Smarcel * keyboard and screen were selected but the keyboard was unplugged or the 87127825Smarcel * user has changed the environment. In the latter case I would assume that 88127825Smarcel * the user expects that FreeBSD uses the new console setting. 89127825Smarcel * For weirder configurations, use ofw_console(4). 90127741Smarcel */ 91122466Sjakestatic phandle_t 92127741Smarceluart_cpu_getdev_console(phandle_t options, char *dev, size_t devsz) 93122466Sjake{ 94122466Sjake char buf[32]; 95133738Smarius ihandle_t stdin, stdout; 96133738Smarius phandle_t chosen, input; 97122466Sjake 98127741Smarcel if (OF_getprop(options, "input-device", dev, devsz) == -1) 99127741Smarcel return (-1); 100127741Smarcel if ((input = OF_finddevice(dev)) == -1) 101127741Smarcel return (-1); 102127825Smarcel if (OF_getprop(options, "output-device", buf, sizeof(buf)) == -1) 103127825Smarcel return (-1); 104127825Smarcel if (!strcmp(dev, "keyboard") && !strcmp(buf, "screen")) { 105127825Smarcel if ((chosen = OF_finddevice("/chosen")) == -1) 106127825Smarcel return (-1); 107127825Smarcel if (OF_getprop(chosen, "stdin", &stdin, sizeof(stdin)) == -1) 108127825Smarcel return (-1); 109127825Smarcel if ((input = OF_instance_to_package(stdin)) == -1) 110127825Smarcel return (-1); 111127825Smarcel if (OF_getprop(chosen, "stdout", &stdout, sizeof(stdout)) == -1) 112127825Smarcel return (-1); 113127825Smarcel if (OF_instance_to_package(stdout) != input) 114127825Smarcel return (-1); 115127825Smarcel snprintf(dev, devsz, "ttya"); 116127825Smarcel } else if (OF_finddevice(buf) != input) 117127825Smarcel return (-1); 118127741Smarcel if (OF_getprop(input, "device_type", buf, sizeof(buf)) == -1) 119127741Smarcel return (-1); 120127741Smarcel if (strcmp(buf, "serial") != 0) 121127741Smarcel return (-1); 122127741Smarcel return (input); 123127741Smarcel} 124127741Smarcel 125127741Smarcel/* 126138157Smarius * Get the package handle of the UART that's selected as the debug port. 127138157Smarius * Since there's no place for this in the OF, we use the kernel environment 128127741Smarcel * variable "hw.uart.dbgport". Note however that the variable is not a 129127741Smarcel * list of attributes. It's single device name or alias, as known by 130127741Smarcel * the OF. 131127741Smarcel */ 132127741Smarcelstatic phandle_t 133127741Smarceluart_cpu_getdev_dbgport(phandle_t options, char *dev, size_t devsz) 134127741Smarcel{ 135127741Smarcel char buf[32]; 136127741Smarcel phandle_t input; 137127741Smarcel 138127741Smarcel if (!getenv_string("hw.uart.dbgport", dev, devsz)) 139127741Smarcel return (-1); 140127741Smarcel if ((input = OF_finddevice(dev)) == -1) 141127741Smarcel return (-1); 142127741Smarcel if (OF_getprop(input, "device_type", buf, sizeof(buf)) == -1) 143127741Smarcel return (-1); 144127741Smarcel if (strcmp(buf, "serial") != 0) 145127741Smarcel return (-1); 146127741Smarcel return (input); 147127741Smarcel} 148127741Smarcel 149138157Smarius/* 150138157Smarius * Get the package handle of the device that is selected as the keyboard 151138157Smarius * port. 152138157Smarius * XXX this also matches PS/2 keyboard controllers and most likely also 153138157Smarius * USB keyboards. 154138157Smarius */ 155127741Smarcelstatic phandle_t 156127741Smarceluart_cpu_getdev_keyboard(phandle_t root, char *dev, size_t devsz) 157127741Smarcel{ 158127741Smarcel char buf[32]; 159127741Smarcel phandle_t child, node; 160127741Smarcel 161127741Smarcel child = OF_child(root); 162127741Smarcel while (child != 0 && child != -1) { 163122466Sjake if (OF_getprop(child, "device_type", buf, sizeof(buf)) != -1 && 164122466Sjake !strcmp(buf, "serial") && 165127741Smarcel OF_getprop(child, "keyboard", buf, sizeof(buf)) != -1) { 166127741Smarcel OF_getprop(child, "name", dev, devsz); 167122466Sjake return (child); 168127741Smarcel } 169127741Smarcel if ((node = uart_cpu_getdev_keyboard(child, dev, devsz)) != -1) 170122466Sjake return (node); 171127741Smarcel child = OF_peer(child); 172122466Sjake } 173122466Sjake return (-1); 174122466Sjake} 175122466Sjake 176119866Smarcelint 177119815Smarceluart_cpu_getdev(int devtype, struct uart_devinfo *di) 178119815Smarcel{ 179120009Stmm char buf[32], dev[32], compat[32]; 180127741Smarcel phandle_t input, options; 181119815Smarcel bus_addr_t addr; 182120452Smarcel int baud, bits, error, space, stop; 183119815Smarcel char flag, par; 184119815Smarcel 185127741Smarcel if ((options = OF_finddevice("/options")) == -1) 186127741Smarcel return (ENXIO); 187127741Smarcel switch (devtype) { 188127741Smarcel case UART_DEV_CONSOLE: 189127741Smarcel input = uart_cpu_getdev_console(options, dev, sizeof(dev)); 190127741Smarcel break; 191127741Smarcel case UART_DEV_DBGPORT: 192127741Smarcel input = uart_cpu_getdev_dbgport(options, dev, sizeof(dev)); 193127741Smarcel break; 194127741Smarcel case UART_DEV_KEYBOARD: 195127741Smarcel input = uart_cpu_getdev_keyboard(OF_peer(0), dev, sizeof(dev)); 196127741Smarcel break; 197127741Smarcel default: 198127741Smarcel input = -1; 199127741Smarcel break; 200127741Smarcel } 201127741Smarcel if (input == -1) 202127741Smarcel return (ENXIO); 203141753Smarius error = OF_decode_addr(input, 0, &space, &addr); 204119815Smarcel if (error) 205119815Smarcel return (error); 206119815Smarcel 207119815Smarcel /* Get the device class. */ 208119815Smarcel if (OF_getprop(input, "name", buf, sizeof(buf)) == -1) 209119815Smarcel return (ENXIO); 210120009Stmm if (OF_getprop(input, "compatible", compat, sizeof(compat)) == -1) 211120009Stmm compat[0] = '\0'; 212119815Smarcel di->bas.regshft = 0; 213119815Smarcel di->bas.rclk = 0; 214119815Smarcel if (!strcmp(buf, "se")) { 215119815Smarcel di->ops = uart_sab82532_ops; 216138157Smarius /* SAB82532 are only known to be used for TTYs. */ 217138157Smarius if ((di->bas.chan = uart_cpu_channel(dev)) == 0) 218138157Smarius return (ENXIO); 219120452Smarcel addr += 64 * (di->bas.chan - 1); 220119815Smarcel } else if (!strcmp(buf, "zs")) { 221119815Smarcel di->ops = uart_z8530_ops; 222138157Smarius if ((di->bas.chan = uart_cpu_channel(dev)) == 0) { 223138157Smarius /* 224138157Smarius * There's no way to determine from OF which 225138157Smarius * channel has the keyboard. Should always be 226138157Smarius * on channel 1 however. 227138157Smarius */ 228138157Smarius if (devtype == UART_DEV_KEYBOARD) 229138157Smarius di->bas.chan = 1; 230138157Smarius else 231138157Smarius return (ENXIO); 232138157Smarius } 233119815Smarcel di->bas.regshft = 1; 234120452Smarcel addr += 4 - 4 * (di->bas.chan - 1); 235120009Stmm } else if (!strcmp(buf, "su") || !strcmp(buf, "su_pnp") || 236120452Smarcel !strcmp(compat, "su") || !strcmp(compat, "su16550")) { 237119815Smarcel di->ops = uart_ns8250_ops; 238120452Smarcel di->bas.chan = 0; 239120452Smarcel } else 240119815Smarcel return (ENXIO); 241119815Smarcel 242119815Smarcel /* Fill in the device info. */ 243119815Smarcel di->bas.bst = &bst_store[devtype]; 244119815Smarcel di->bas.bsh = sparc64_fake_bustag(space, addr, di->bas.bst); 245119815Smarcel 246119815Smarcel /* Get the line settings. */ 247120545Sjake if (devtype == UART_DEV_KEYBOARD) 248120545Sjake di->baudrate = 1200; 249120545Sjake else 250120545Sjake di->baudrate = 9600; 251119815Smarcel di->databits = 8; 252119815Smarcel di->stopbits = 1; 253119815Smarcel di->parity = UART_PARITY_NONE; 254119815Smarcel snprintf(buf, sizeof(buf), "%s-mode", dev); 255119815Smarcel if (OF_getprop(options, buf, buf, sizeof(buf)) == -1) 256119815Smarcel return (0); 257119815Smarcel if (sscanf(buf, "%d,%d,%c,%d,%c", &baud, &bits, &par, &stop, &flag) 258119815Smarcel != 5) 259119815Smarcel return (0); 260119815Smarcel di->baudrate = baud; 261119815Smarcel di->databits = bits; 262119815Smarcel di->stopbits = stop; 263119815Smarcel di->parity = (par == 'n') ? UART_PARITY_NONE : 264119815Smarcel (par == 'o') ? UART_PARITY_ODD : UART_PARITY_EVEN; 265119815Smarcel return (0); 266119815Smarcel} 267