1 /* 2 * Copyright (c) 2003, 2004 Marcel Moolenaar 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27 #include <sys/cdefs.h> 28 __FBSDID("$FreeBSD$"); 29 30 #include <sys/param.h> 31 #include <sys/systm.h> 32 33 #include <machine/bus.h> 34 #include <machine/bus_private.h> 35 36 #include <dev/ofw/openfirm.h> 37 #include <machine/ofw_machdep.h> 38 39 #include <dev/uart/uart.h> 40 #include <dev/uart/uart_cpu.h> 41 42 bus_space_tag_t uart_bus_space_io; 43 bus_space_tag_t uart_bus_space_mem; 44 45 static struct bus_space_tag bst_store[3]; 46 47 static int 48 uart_cpu_channel(char *dev) 49 { 50 char alias[64]; 51 phandle_t aliases; 52 int len; 53 54 strcpy(alias, dev); 55 if ((aliases = OF_finddevice("/aliases")) != -1) 56 OF_getprop(aliases, dev, alias, sizeof(alias)); 57 len = strlen(alias); 58 if (len < 2 || alias[len - 2] != ':' || alias[len - 1] < 'a' || 59 alias[len - 1] > 'b') 60 return (0); 61 return (alias[len - 1] - 'a' + 1); 62 } 63 64 int 65 uart_cpu_eqres(struct uart_bas *b1, struct uart_bas *b2) 66 { 67 68 return ((b1->bsh == b2->bsh) ? 1 : 0); 69 } 70 71 /* 72 * Get the address of the UART that is selected as the console, if the 73 * console is an UART of course. Note that we enforce that both stdin and 74 * stdout are selected. 75 * Note that the currently active console (i.e. /chosen/stdout and 76 * /chosen/stdin) may not be the same as the device selected in the 77 * environment (ie /options/output-device and /options/input-device) because 78 * keyboard and screen were selected but the keyboard was unplugged or the 79 * user has changed the environment. In the latter case I would assume that 80 * the user expects that FreeBSD uses the new console setting. 81 * For weirder configurations, use ofw_console(4). 82 */ 83 static phandle_t 84 uart_cpu_getdev_console(phandle_t options, char *dev, size_t devsz) 85 { 86 char buf[32]; 87 phandle_t input; 88 89 if (OF_getprop(options, "input-device", dev, devsz) == -1) 90 return (-1); 91 if ((input = OF_finddevice(dev)) == -1) 92 return (-1); 93 if (OF_getprop(options, "output-device", buf, sizeof(buf)) == -1) 94 return (-1); 95 if (!strcmp(dev, "keyboard") && !strcmp(buf, "screen")) { 96 phandle_t chosen; 97 ihandle_t stdin, stdout; 98 99 if ((chosen = OF_finddevice("/chosen")) == -1) 100 return (-1); 101 if (OF_getprop(chosen, "stdin", &stdin, sizeof(stdin)) == -1) 102 return (-1); 103 if ((input = OF_instance_to_package(stdin)) == -1) 104 return (-1); 105 if (OF_getprop(chosen, "stdout", &stdout, sizeof(stdout)) == -1) 106 return (-1); 107 if (OF_instance_to_package(stdout) != input) 108 return (-1); 109 snprintf(dev, devsz, "ttya"); 110 } else if (OF_finddevice(buf) != input) 111 return (-1); 112 if (OF_getprop(input, "device_type", buf, sizeof(buf)) == -1) 113 return (-1); 114 if (strcmp(buf, "serial") != 0) 115 return (-1); 116 return (input); 117 } 118 119 /* 120 * Get the address of the UART that's selected as the debug port. Since 121 * there's no place for this in the OF, we use the kernel environment 122 * variable "hw.uart.dbgport". Note however that the variable is not a 123 * list of attributes. It's single device name or alias, as known by 124 * the OF. 125 */ 126 static phandle_t 127 uart_cpu_getdev_dbgport(phandle_t options, char *dev, size_t devsz) 128 { 129 char buf[32]; 130 phandle_t input; 131 132 if (!getenv_string("hw.uart.dbgport", dev, devsz)) 133 return (-1); 134 if ((input = OF_finddevice(dev)) == -1) 135 return (-1); 136 if (OF_getprop(input, "device_type", buf, sizeof(buf)) == -1) 137 return (-1); 138 if (strcmp(buf, "serial") != 0) 139 return (-1); 140 return (input); 141 } 142 143 static phandle_t 144 uart_cpu_getdev_keyboard(phandle_t root, char *dev, size_t devsz) 145 { 146 char buf[32]; 147 phandle_t child, node; 148 149 child = OF_child(root); 150 while (child != 0 && child != -1) { 151 if (OF_getprop(child, "device_type", buf, sizeof(buf)) != -1 && 152 !strcmp(buf, "serial") && 153 OF_getprop(child, "keyboard", buf, sizeof(buf)) != -1) { 154 OF_getprop(child, "name", dev, devsz); 155 return (child); 156 } 157 if ((node = uart_cpu_getdev_keyboard(child, dev, devsz)) != -1) 158 return (node); 159 child = OF_peer(child); 160 } 161 return (-1); 162 } 163 164 int 165 uart_cpu_getdev(int devtype, struct uart_devinfo *di) 166 { 167 char buf[32], dev[32], compat[32]; 168 phandle_t input, options; 169 bus_addr_t addr; 170 int baud, bits, error, space, stop; 171 char flag, par; 172 173 if ((options = OF_finddevice("/options")) == -1) 174 return (ENXIO); 175 switch (devtype) { 176 case UART_DEV_CONSOLE: 177 input = uart_cpu_getdev_console(options, dev, sizeof(dev)); 178 break; 179 case UART_DEV_DBGPORT: 180 input = uart_cpu_getdev_dbgport(options, dev, sizeof(dev)); 181 break; 182 case UART_DEV_KEYBOARD: 183 input = uart_cpu_getdev_keyboard(OF_peer(0), dev, sizeof(dev)); 184 break; 185 default: 186 input = -1; 187 break; 188 } 189 if (input == -1) 190 return (ENXIO); 191 error = OF_decode_addr(input, &space, &addr); 192 if (error) 193 return (error); 194 195 /* Get the device class. */ 196 if (OF_getprop(input, "name", buf, sizeof(buf)) == -1) 197 return (ENXIO); 198 if (OF_getprop(input, "compatible", compat, sizeof(compat)) == -1) 199 compat[0] = '\0'; 200 di->bas.regshft = 0; 201 di->bas.rclk = 0; 202 if (!strcmp(buf, "se")) { 203 di->ops = uart_sab82532_ops; 204 di->bas.chan = uart_cpu_channel(dev); 205 addr += 64 * (di->bas.chan - 1); 206 } else if (!strcmp(buf, "zs")) { 207 di->ops = uart_z8530_ops; 208 di->bas.chan = uart_cpu_channel(dev); 209 di->bas.regshft = 1; 210 addr += 4 - 4 * (di->bas.chan - 1); 211 } else if (!strcmp(buf, "su") || !strcmp(buf, "su_pnp") || 212 !strcmp(compat, "su") || !strcmp(compat, "su16550")) { 213 di->ops = uart_ns8250_ops; 214 di->bas.chan = 0; 215 } else 216 return (ENXIO); 217 218 /* Fill in the device info. */ 219 di->bas.bst = &bst_store[devtype]; 220 di->bas.bsh = sparc64_fake_bustag(space, addr, di->bas.bst); 221 222 /* Get the line settings. */ 223 if (devtype == UART_DEV_KEYBOARD) 224 di->baudrate = 1200; 225 else 226 di->baudrate = 9600; 227 di->databits = 8; 228 di->stopbits = 1; 229 di->parity = UART_PARITY_NONE; 230 snprintf(buf, sizeof(buf), "%s-mode", dev); 231 if (OF_getprop(options, buf, buf, sizeof(buf)) == -1) 232 return (0); 233 if (sscanf(buf, "%d,%d,%c,%d,%c", &baud, &bits, &par, &stop, &flag) 234 != 5) 235 return (0); 236 di->baudrate = baud; 237 di->databits = bits; 238 di->stopbits = stop; 239 di->parity = (par == 'n') ? UART_PARITY_NONE : 240 (par == 'o') ? UART_PARITY_ODD : UART_PARITY_EVEN; 241 return (0); 242 } 243