1 /* $OpenBSD: com_ebus.c,v 1.21 2011/09/28 11:02:37 dlg Exp $ */ 2 /* $NetBSD: com_ebus.c,v 1.6 2001/07/24 19:27:10 eeh Exp $ */ 3 4 /* 5 * Copyright (c) 1999, 2000 Matthew R. Green 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. The name of the author may not be used to endorse or promote products 17 * derived from this software without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 24 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 26 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 27 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 /* 33 * NS Super I/O PC87332VLJ "com" to ebus attachment 34 */ 35 36 #include <sys/types.h> 37 #include <sys/param.h> 38 #include <sys/systm.h> 39 #include <sys/device.h> 40 #include <sys/tty.h> 41 #include <sys/conf.h> 42 43 #include <machine/bus.h> 44 #include <machine/autoconf.h> 45 #include <machine/openfirm.h> 46 47 #include <sparc64/dev/ebusreg.h> 48 #include <sparc64/dev/ebusvar.h> 49 50 #include <dev/cons.h> 51 #include <dev/ic/comvar.h> 52 53 cdev_decl(com); /* XXX this belongs elsewhere */ 54 55 int com_ebus_match(struct device *, void *, void *); 56 void com_ebus_attach(struct device *, struct device *, void *); 57 int com_ebus_speed(struct ebus_attach_args *); 58 59 struct cfattach com_ebus_ca = { 60 sizeof(struct com_softc), com_ebus_match, com_ebus_attach 61 }; 62 63 static char *com_names[] = { 64 "su", 65 "su_pnp", 66 "rsc-console", 67 "lom-console", 68 NULL 69 }; 70 71 int 72 com_ebus_match(struct device *parent, void *match, void *aux) 73 { 74 struct ebus_attach_args *ea = aux; 75 int i; 76 77 for (i=0; com_names[i]; i++) 78 if (strcmp(ea->ea_name, com_names[i]) == 0) 79 return (1); 80 81 if (strcmp(ea->ea_name, "serial") == 0) { 82 char compat[80]; 83 84 /* Could be anything. */ 85 if ((i = OF_getproplen(ea->ea_node, "compatible")) && 86 OF_getprop(ea->ea_node, "compatible", compat, 87 sizeof(compat)) == i) { 88 if (strcmp(compat, "su16552") == 0 || 89 strcmp(compat, "su16550") == 0 || 90 strcmp(compat, "FJSV,su") == 0 || 91 strcmp(compat, "su") == 0) { 92 return (1); 93 } 94 } 95 } 96 return (0); 97 } 98 99 /* XXXART - was 1846200 */ 100 #define BAUD_BASE (1843200) 101 102 void 103 com_ebus_attach(struct device *parent, struct device *self, void *aux) 104 { 105 struct com_softc *sc = (void *)self; 106 struct ebus_attach_args *ea = aux; 107 int i, com_is_input, com_is_output; 108 int node, port; 109 char buf[32]; 110 111 sc->sc_iobase = EBUS_PADDR_FROM_REG(&ea->ea_regs[0]); 112 /* 113 * Addresses that should be supplied by the prom: 114 * - normal com registers 115 * - ns873xx configuration registers 116 * - DMA space 117 * The `com' driver does not use DMA accesses, so we can 118 * ignore that for now. We should enable the com port in 119 * the ns873xx registers here. XXX 120 * 121 * Use the prom address if there. 122 */ 123 if (ea->ea_nvaddrs) { 124 if (bus_space_map(ea->ea_memtag, ea->ea_vaddrs[0], 0, 125 BUS_SPACE_MAP_PROMADDRESS, &sc->sc_ioh) != 0) { 126 printf(": can't map register space\n"); 127 return; 128 } 129 sc->sc_iot = ea->ea_memtag; 130 } else if (ebus_bus_map(ea->ea_memtag, 0, 131 EBUS_PADDR_FROM_REG(&ea->ea_regs[0]), 132 ea->ea_regs[0].size, 0, 0, &sc->sc_ioh) == 0) { 133 sc->sc_iot = ea->ea_memtag; 134 } else if (ebus_bus_map(ea->ea_iotag, 0, 135 EBUS_PADDR_FROM_REG(&ea->ea_regs[0]), 136 ea->ea_regs[0].size, 0, 0, &sc->sc_ioh) == 0) { 137 sc->sc_iot = ea->ea_iotag; 138 } else { 139 printf(": can't map register space\n"); 140 return; 141 } 142 sc->sc_hwflags = 0; 143 sc->sc_swflags = 0; 144 sc->sc_frequency = BAUD_BASE; 145 146 for (i = 0; i < ea->ea_nintrs; i++) 147 bus_intr_establish(sc->sc_iot, ea->ea_intrs[i], 148 IPL_TTY, 0, comintr, sc, self->dv_xname); 149 150 /* 151 * Figure out if we're the console. 152 * 153 * The Fujitsu SPARC Enterprise M4000/M5000/M8000/M9000 has a 154 * serial port on each I/O board and a pseudo console that is 155 * redirected to one of these serial ports. The board number 156 * of the serial port in question is encoded in the "tty-port#" 157 * property of the pseudo console, so we figure out what our 158 * board number is by walking up the device tree, and check 159 * for a match. 160 */ 161 162 node = OF_instance_to_package(OF_stdin()); 163 com_is_input = (ea->ea_node == node); 164 if (OF_getprop(node, "name", buf, sizeof(buf)) > 0 && 165 strcmp(buf, "pseudo-console") == 0) { 166 port = getpropint(node, "tty-port#", -1); 167 node = OF_parent(OF_parent(ea->ea_node)); 168 com_is_input = (getpropint(node, "board#", -2) == port); 169 } 170 171 node = OF_instance_to_package(OF_stdout()); 172 com_is_output = (ea->ea_node == node); 173 if (OF_getprop(node, "name", buf, sizeof(buf)) > 0 && 174 strcmp(buf, "pseudo-console") == 0) { 175 port = getpropint(node, "tty-port#", -1); 176 node = OF_parent(OF_parent(ea->ea_node)); 177 com_is_output = (getpropint(node, "board#", -2) == port); 178 } 179 180 if (com_is_input || com_is_output) { 181 struct consdev *cn_orig; 182 int speed; 183 184 speed = com_ebus_speed(ea); 185 186 comconsioh = sc->sc_ioh; 187 cn_orig = cn_tab; 188 /* Attach com as the console. */ 189 if (comcnattach(sc->sc_iot, sc->sc_iobase, speed, 190 sc->sc_frequency, 191 ((TTYDEF_CFLAG & ~(CSIZE | PARENB))|CREAD | CS8 | HUPCL))) { 192 printf("Error: comcnattach failed\n"); 193 } 194 cn_tab = cn_orig; 195 if (com_is_input) { 196 cn_tab->cn_dev = /*XXX*/makedev(36, sc->sc_dev.dv_unit); 197 cn_tab->cn_probe = comcnprobe; 198 cn_tab->cn_init = comcninit; 199 cn_tab->cn_getc = comcngetc; 200 cn_tab->cn_pollc = comcnpollc; 201 } 202 if (com_is_output) 203 cn_tab->cn_putc = comcnputc; 204 } 205 206 /* 207 * Apparently shoving too much data down the TX FIFO on the 208 * Fujitsu SPARC Enterprise M4000/M5000 causes a hardware 209 * fault. Avoid this issue by setting the FIFO depth to 1. 210 * This will effectively disable the TX FIFO, but will still 211 * enable the RX FIFO, which is what we really care about. 212 */ 213 if (OF_getprop(ea->ea_node, "compatible", buf, sizeof(buf)) > 0 && 214 strcmp(buf, "FJSV,su") == 0) 215 sc->sc_uarttype = COM_UART_16550; 216 217 if (OF_getproplen(ea->ea_node, "keyboard") == 0) 218 printf(": keyboard"); 219 else if (OF_getproplen(ea->ea_node, "mouse") == 0) 220 printf(": mouse"); 221 222 /* Now attach the driver */ 223 com_attach_subr(sc); 224 } 225 226 int 227 com_ebus_speed(struct ebus_attach_args *ea) 228 { 229 char buf[128]; 230 char *name = NULL; 231 int aliases, options; 232 233 if (strcmp(ea->ea_name, "rsc-console") == 0) 234 return 115200; 235 236 aliases = OF_finddevice("/aliases"); 237 if (OF_getprop(aliases, "ttya", buf, sizeof(buf)) != -1 && 238 OF_finddevice(buf) == ea->ea_node) 239 name = "ttya-mode"; 240 if (OF_getprop(aliases, "ttyb", buf, sizeof(buf)) != -1 && 241 OF_finddevice(buf) == ea->ea_node) 242 name = "ttyb-mode"; 243 244 if (name == NULL) 245 return TTYDEF_SPEED; 246 247 options = OF_finddevice("/options"); 248 return (getpropspeed(options, name)); 249 } 250