uart_cpu_sparc64.c revision 127741
1119815Smarcel/* 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 127741 2004-04-02 07:33:35Z marcel $"); 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 47119815Smarcelstatic int 48119815Smarceluart_cpu_channel(char *dev) 49119815Smarcel{ 50119815Smarcel char alias[64]; 51119815Smarcel phandle_t aliases; 52119815Smarcel int len; 53119815Smarcel 54119815Smarcel strcpy(alias, dev); 55119815Smarcel if ((aliases = OF_finddevice("/aliases")) != -1) 56119815Smarcel OF_getprop(aliases, dev, alias, sizeof(alias)); 57119815Smarcel len = strlen(alias); 58119815Smarcel if (len < 2 || alias[len - 2] != ':' || alias[len - 1] < 'a' || 59119815Smarcel alias[len - 1] > 'b') 60119815Smarcel return (0); 61120452Smarcel return (alias[len - 1] - 'a' + 1); 62119815Smarcel} 63119815Smarcel 64119815Smarcelint 65119866Smarceluart_cpu_eqres(struct uart_bas *b1, struct uart_bas *b2) 66119866Smarcel{ 67119866Smarcel 68119866Smarcel return ((b1->bsh == b2->bsh) ? 1 : 0); 69119866Smarcel} 70119866Smarcel 71127741Smarcel/* 72127741Smarcel * Get the address of the UART that is selected as the console, if the 73127741Smarcel * console is an UART of course. Note that we enforce that both stdin and 74127741Smarcel * stdout are selected. For weird configurations, use ofw_console(4). 75127741Smarcel * Note that the currently active console (i.e. /chosen/stdout and 76127741Smarcel * /chosen/stdin) may not be the same as the device selected in the 77127741Smarcel * environment (ie /options/output-device and /options/input-device) because 78127741Smarcel * the user may have changed the environment. In that case I would assume 79127741Smarcel * that the user expects that FreeBSD uses the new console setting. There's 80127741Smarcel * no choice, really. 81127741Smarcel */ 82122466Sjakestatic phandle_t 83127741Smarceluart_cpu_getdev_console(phandle_t options, char *dev, size_t devsz) 84122466Sjake{ 85122466Sjake char buf[32]; 86127741Smarcel phandle_t input; 87122466Sjake 88127741Smarcel if (OF_getprop(options, "input-device", dev, devsz) == -1) 89127741Smarcel return (-1); 90127741Smarcel if ((input = OF_finddevice(dev)) == -1) 91127741Smarcel return (-1); 92127741Smarcel if (OF_getprop(input, "device_type", buf, sizeof(buf)) == -1) 93127741Smarcel return (-1); 94127741Smarcel if (strcmp(buf, "serial") != 0) 95127741Smarcel return (-1); 96127741Smarcel if (OF_getprop(options, "output-device", buf, sizeof(buf)) == -1) 97127741Smarcel return (-1); 98127741Smarcel if (OF_finddevice(buf) != input) 99127741Smarcel return (-1); 100127741Smarcel return (input); 101127741Smarcel} 102127741Smarcel 103127741Smarcel/* 104127741Smarcel * Get the address of the UART that's selected as the debug port. Since 105127741Smarcel * there's no place for this in the OF, we use the kernel environment 106127741Smarcel * variable "hw.uart.dbgport". Note however that the variable is not a 107127741Smarcel * list of attributes. It's single device name or alias, as known by 108127741Smarcel * the OF. 109127741Smarcel */ 110127741Smarcelstatic phandle_t 111127741Smarceluart_cpu_getdev_dbgport(phandle_t options, char *dev, size_t devsz) 112127741Smarcel{ 113127741Smarcel char buf[32]; 114127741Smarcel phandle_t input; 115127741Smarcel 116127741Smarcel if (!getenv_string("hw.uart.dbgport", dev, devsz)) 117127741Smarcel return (-1); 118127741Smarcel if ((input = OF_finddevice(dev)) == -1) 119127741Smarcel return (-1); 120127741Smarcel if (OF_getprop(input, "device_type", buf, sizeof(buf)) == -1) 121127741Smarcel return (-1); 122127741Smarcel if (strcmp(buf, "serial") != 0) 123127741Smarcel return (-1); 124127741Smarcel return (input); 125127741Smarcel} 126127741Smarcel 127127741Smarcelstatic phandle_t 128127741Smarceluart_cpu_getdev_keyboard(phandle_t root, char *dev, size_t devsz) 129127741Smarcel{ 130127741Smarcel char buf[32]; 131127741Smarcel phandle_t child, node; 132127741Smarcel 133127741Smarcel child = OF_child(root); 134127741Smarcel while (child != 0 && child != -1) { 135122466Sjake if (OF_getprop(child, "device_type", buf, sizeof(buf)) != -1 && 136122466Sjake !strcmp(buf, "serial") && 137127741Smarcel OF_getprop(child, "keyboard", buf, sizeof(buf)) != -1) { 138127741Smarcel OF_getprop(child, "name", dev, devsz); 139122466Sjake return (child); 140127741Smarcel } 141127741Smarcel if ((node = uart_cpu_getdev_keyboard(child, dev, devsz)) != -1) 142122466Sjake return (node); 143127741Smarcel child = OF_peer(child); 144122466Sjake } 145122466Sjake return (-1); 146122466Sjake} 147122466Sjake 148119866Smarcelint 149119815Smarceluart_cpu_getdev(int devtype, struct uart_devinfo *di) 150119815Smarcel{ 151120009Stmm char buf[32], dev[32], compat[32]; 152127741Smarcel phandle_t input, options; 153119815Smarcel bus_addr_t addr; 154120452Smarcel int baud, bits, error, space, stop; 155119815Smarcel char flag, par; 156119815Smarcel 157127741Smarcel if ((options = OF_finddevice("/options")) == -1) 158127741Smarcel return (ENXIO); 159127741Smarcel switch (devtype) { 160127741Smarcel case UART_DEV_CONSOLE: 161127741Smarcel input = uart_cpu_getdev_console(options, dev, sizeof(dev)); 162127741Smarcel break; 163127741Smarcel case UART_DEV_DBGPORT: 164127741Smarcel input = uart_cpu_getdev_dbgport(options, dev, sizeof(dev)); 165127741Smarcel break; 166127741Smarcel case UART_DEV_KEYBOARD: 167127741Smarcel input = uart_cpu_getdev_keyboard(OF_peer(0), dev, sizeof(dev)); 168127741Smarcel break; 169127741Smarcel default: 170127741Smarcel input = -1; 171127741Smarcel break; 172127741Smarcel } 173127741Smarcel if (input == -1) 174127741Smarcel return (ENXIO); 175119815Smarcel error = OF_decode_addr(input, &space, &addr); 176119815Smarcel if (error) 177119815Smarcel return (error); 178119815Smarcel 179119815Smarcel /* Get the device class. */ 180119815Smarcel if (OF_getprop(input, "name", buf, sizeof(buf)) == -1) 181119815Smarcel return (ENXIO); 182120009Stmm if (OF_getprop(input, "compatible", compat, sizeof(compat)) == -1) 183120009Stmm compat[0] = '\0'; 184119815Smarcel di->bas.regshft = 0; 185119815Smarcel di->bas.rclk = 0; 186119815Smarcel if (!strcmp(buf, "se")) { 187119815Smarcel di->ops = uart_sab82532_ops; 188120452Smarcel di->bas.chan = uart_cpu_channel(dev); 189120452Smarcel addr += 64 * (di->bas.chan - 1); 190119815Smarcel } else if (!strcmp(buf, "zs")) { 191119815Smarcel di->ops = uart_z8530_ops; 192120452Smarcel di->bas.chan = uart_cpu_channel(dev); 193119815Smarcel di->bas.regshft = 1; 194120452Smarcel addr += 4 - 4 * (di->bas.chan - 1); 195120009Stmm } else if (!strcmp(buf, "su") || !strcmp(buf, "su_pnp") || 196120452Smarcel !strcmp(compat, "su") || !strcmp(compat, "su16550")) { 197119815Smarcel di->ops = uart_ns8250_ops; 198120452Smarcel di->bas.chan = 0; 199120452Smarcel } else 200119815Smarcel return (ENXIO); 201119815Smarcel 202119815Smarcel /* Fill in the device info. */ 203119815Smarcel di->bas.bst = &bst_store[devtype]; 204119815Smarcel di->bas.bsh = sparc64_fake_bustag(space, addr, di->bas.bst); 205119815Smarcel 206119815Smarcel /* Get the line settings. */ 207120545Sjake if (devtype == UART_DEV_KEYBOARD) 208120545Sjake di->baudrate = 1200; 209120545Sjake else 210120545Sjake di->baudrate = 9600; 211119815Smarcel di->databits = 8; 212119815Smarcel di->stopbits = 1; 213119815Smarcel di->parity = UART_PARITY_NONE; 214119815Smarcel snprintf(buf, sizeof(buf), "%s-mode", dev); 215119815Smarcel if (OF_getprop(options, buf, buf, sizeof(buf)) == -1) 216119815Smarcel return (0); 217119815Smarcel if (sscanf(buf, "%d,%d,%c,%d,%c", &baud, &bits, &par, &stop, &flag) 218119815Smarcel != 5) 219119815Smarcel return (0); 220119815Smarcel di->baudrate = baud; 221119815Smarcel di->databits = bits; 222119815Smarcel di->stopbits = stop; 223119815Smarcel di->parity = (par == 'n') ? UART_PARITY_NONE : 224119815Smarcel (par == 'o') ? UART_PARITY_ODD : UART_PARITY_EVEN; 225119815Smarcel return (0); 226119815Smarcel} 227