1 /* $NetBSD: puc.c,v 1.40 2018/11/30 16:26:59 jmcneill Exp $ */ 2 3 /* 4 * Copyright (c) 1996, 1998, 1999 5 * Christopher G. Demetriou. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by Christopher G. Demetriou 18 * for the NetBSD Project. 19 * 4. The name of the author may not be used to endorse or promote products 20 * derived from this software without specific prior written permission 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 */ 33 34 /* 35 * PCI "universal" communication card device driver, glues com, lpt, 36 * and similar ports to PCI via bridge chip often much larger than 37 * the devices being glued. 38 * 39 * Author: Christopher G. Demetriou, May 14, 1998 (derived from NetBSD 40 * sys/dev/pci/pciide.c, revision 1.6). 41 * 42 * These devices could be (and some times are) described as 43 * communications/{serial,parallel}, etc. devices with known 44 * programming interfaces, but those programming interfaces (in 45 * particular the BAR assignments for devices, etc.) in fact are not 46 * particularly well defined. 47 * 48 * After I/we have seen more of these devices, it may be possible 49 * to generalize some of these bits. In particular, devices which 50 * describe themselves as communications/serial/16[45]50, and 51 * communications/parallel/??? might be attached via direct 52 * 'com' and 'lpt' attachments to pci. 53 */ 54 55 #include <sys/cdefs.h> 56 __KERNEL_RCSID(0, "$NetBSD: puc.c,v 1.40 2018/11/30 16:26:59 jmcneill Exp $"); 57 58 #include <sys/param.h> 59 #include <sys/systm.h> 60 #include <sys/device.h> 61 62 #include <dev/pci/pcireg.h> 63 #include <dev/pci/pcivar.h> 64 #include <dev/pci/pucvar.h> 65 #include <dev/pci/pcidevs.h> 66 #include <sys/termios.h> 67 #include <dev/ic/comreg.h> 68 #include <dev/ic/comvar.h> 69 70 #include "locators.h" 71 #include "com.h" 72 73 struct puc_softc { 74 /* static configuration data */ 75 const struct puc_device_description *sc_desc; 76 77 /* card-global dynamic data */ 78 void *sc_ih; 79 struct { 80 int mapped; 81 bus_addr_t a; 82 bus_size_t s; 83 bus_space_tag_t t; 84 bus_space_handle_t h; 85 } sc_bar_mappings[6]; /* XXX constant */ 86 87 /* per-port dynamic data */ 88 struct { 89 device_t dev; 90 91 /* filled in by port attachments */ 92 int (*ihand)(void *); 93 void *ihandarg; 94 } sc_ports[PUC_MAX_PORTS]; 95 }; 96 97 static int puc_print(void *, const char *); 98 99 static const char *puc_port_type_name(int); 100 101 static int 102 puc_match(device_t parent, cfdata_t match, void *aux) 103 { 104 struct pci_attach_args *pa = aux; 105 const struct puc_device_description *desc; 106 pcireg_t bhlc, subsys; 107 108 bhlc = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_BHLC_REG); 109 if (PCI_HDRTYPE_TYPE(bhlc) != 0) 110 return (0); 111 112 subsys = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_SUBSYS_ID_REG); 113 114 desc = puc_find_description(PCI_VENDOR(pa->pa_id), 115 PCI_PRODUCT(pa->pa_id), PCI_VENDOR(subsys), PCI_PRODUCT(subsys)); 116 if (desc != NULL) 117 return (10); 118 119 #if 0 120 /* 121 * XXX this is obviously bogus. eventually, we might want 122 * XXX to match communications/modem, etc., but that needs some 123 * XXX special work in the match fn. 124 */ 125 /* 126 * Match class/subclass, so we can tell people to compile kernel 127 * with options that cause this driver to spew. 128 */ 129 if (PCI_CLASS(pa->pa_class) == PCI_CLASS_COMMUNICATIONS) { 130 switch (PCI_SUBCLASS(pa->pa_class)) { 131 case PCI_SUBCLASS_COMMUNICATIONS_SERIAL: 132 case PCI_SUBCLASS_COMMUNICATIONS_MODEM: 133 return (1); 134 } 135 } 136 #endif 137 138 return (0); 139 } 140 141 static void 142 puc_attach(device_t parent, device_t self, void *aux) 143 { 144 struct puc_softc *sc = device_private(self); 145 struct pci_attach_args *pa = aux; 146 struct puc_attach_args paa; 147 pci_intr_handle_t intrhandle; 148 pcireg_t subsys; 149 int i, barindex; 150 int locs[PUCCF_NLOCS]; 151 bool poll = false; 152 153 subsys = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_SUBSYS_ID_REG); 154 sc->sc_desc = puc_find_description(PCI_VENDOR(pa->pa_id), 155 PCI_PRODUCT(pa->pa_id), PCI_VENDOR(subsys), PCI_PRODUCT(subsys)); 156 if (sc->sc_desc == NULL) { 157 /* 158 * This was a class/subclass match, so tell people to compile 159 * kernel with options that cause this driver to spew. 160 */ 161 #ifdef PUC_PRINT_REGS 162 printf(":\n"); 163 pci_conf_print(pa->pa_pc, pa->pa_tag, NULL); 164 #else 165 printf(": unknown PCI communications device\n"); 166 printf("%s: compile kernel with PUC_PRINT_REGS and larger\n", 167 device_xname(self)); 168 printf("%s: message buffer (via 'options MSGBUFSIZE=...'),\n", 169 device_xname(self)); 170 printf("%s: and report the result with send-pr\n", 171 device_xname(self)); 172 #endif 173 return; 174 } 175 176 aprint_naive("\n"); 177 aprint_normal(": %s (", sc->sc_desc->name); 178 for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) 179 aprint_normal("%s%s", i ? ", " : "", 180 puc_port_type_name(sc->sc_desc->ports[i].type)); 181 aprint_normal(")\n"); 182 183 for (i = 0; i < 6; i++) { 184 pcireg_t bar, type; 185 186 sc->sc_bar_mappings[i].mapped = 0; 187 188 bar = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_BAR(i)); 189 if (bar == 0) /* BAR not implemented(?) */ 190 continue; 191 192 type = (PCI_MAPREG_TYPE(bar) == PCI_MAPREG_TYPE_IO ? 193 PCI_MAPREG_TYPE_IO : PCI_MAPREG_MEM_TYPE(bar)); 194 195 if (type == PCI_MAPREG_TYPE_IO) { 196 sc->sc_bar_mappings[i].t = pa->pa_iot; 197 sc->sc_bar_mappings[i].a = PCI_MAPREG_IO_ADDR(bar); 198 sc->sc_bar_mappings[i].s = PCI_MAPREG_IO_SIZE(bar); 199 } else { 200 sc->sc_bar_mappings[i].t = pa->pa_memt; 201 sc->sc_bar_mappings[i].a = PCI_MAPREG_MEM_ADDR(bar); 202 sc->sc_bar_mappings[i].s = PCI_MAPREG_MEM_SIZE(bar); 203 } 204 205 sc->sc_bar_mappings[i].mapped = (pci_mapreg_map(pa, 206 PCI_BAR(i), type, 0, 207 &sc->sc_bar_mappings[i].t, &sc->sc_bar_mappings[i].h, 208 &sc->sc_bar_mappings[i].a, &sc->sc_bar_mappings[i].s) 209 == 0); 210 if (sc->sc_bar_mappings[i].mapped) 211 continue; 212 213 aprint_debug_dev(self, "couldn't map BAR at offset 0x%lx\n", 214 (long)(PCI_MAPREG_START + 4 * i)); 215 } 216 217 /* Map interrupt. */ 218 if (pci_intr_map(pa, &intrhandle)) { 219 poll = true; 220 } 221 /* 222 * XXX the sub-devices establish the interrupts, for the 223 * XXX following reasons: 224 * XXX 225 * XXX * we can't really know what IPLs they'd want 226 * XXX 227 * XXX * the MD dispatching code can ("should") dispatch 228 * XXX chained interrupts better than we can. 229 * XXX 230 * XXX It would be nice if we could indicate to the MD interrupt 231 * XXX handling code that the interrupt line used by the device 232 * XXX was a PCI (level triggered) interrupt. 233 * XXX 234 * XXX It's not pretty, but hey, what is? 235 */ 236 237 /* SB16C10xx board specific initialization */ 238 if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_SYSTEMBASE && 239 (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_SYSTEMBASE_SB16C1050 || 240 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_SYSTEMBASE_SB16C1054 || 241 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_SYSTEMBASE_SB16C1058)) { 242 if (!sc->sc_bar_mappings[1].mapped) { 243 aprint_error_dev(self, 244 "optional register is not mapped\n"); 245 return; 246 } 247 #define SB16C105X_OPT_IMRREG0 0x0000000c 248 /* enable port 0-7 interrupt */ 249 bus_space_write_1(sc->sc_bar_mappings[1].t, 250 sc->sc_bar_mappings[1].h, SB16C105X_OPT_IMRREG0, 0xff); 251 } else { 252 if (!pmf_device_register(self, NULL, NULL)) 253 aprint_error_dev(self, 254 "couldn't establish power handler\n"); 255 } 256 257 /* Configure each port. */ 258 for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) { 259 barindex = PUC_PORT_BAR_INDEX(sc->sc_desc->ports[i].bar); 260 bus_space_handle_t subregion_handle; 261 int is_console = 0; 262 263 /* make sure the base address register is mapped */ 264 #if NCOM > 0 265 is_console = com_is_console(sc->sc_bar_mappings[barindex].t, 266 sc->sc_bar_mappings[barindex].a + 267 sc->sc_desc->ports[i].offset, &subregion_handle); 268 if (is_console) { 269 sc->sc_bar_mappings[barindex].mapped = 1; 270 #if defined(amd64) || defined(i386) 271 sc->sc_bar_mappings[barindex].h = subregion_handle - 272 sc->sc_desc->ports[i].offset; /* XXX hack */ 273 #endif 274 } 275 #endif 276 if (!sc->sc_bar_mappings[barindex].mapped) { 277 aprint_error_dev(self, 278 "%s port uses unmapped BAR (0x%x)\n", 279 puc_port_type_name(sc->sc_desc->ports[i].type), 280 sc->sc_desc->ports[i].bar); 281 continue; 282 } 283 284 /* set up to configure the child device */ 285 paa.port = i; 286 paa.type = sc->sc_desc->ports[i].type; 287 paa.flags = sc->sc_desc->ports[i].flags; 288 paa.pc = pa->pa_pc; 289 paa.tag = pa->pa_tag; 290 paa.intrhandle = intrhandle; 291 paa.poll = poll; 292 paa.a = sc->sc_bar_mappings[barindex].a + 293 sc->sc_desc->ports[i].offset; 294 paa.t = sc->sc_bar_mappings[barindex].t; 295 paa.dmat = pa->pa_dmat; 296 paa.dmat64 = pa->pa_dmat64; 297 298 if (!is_console && 299 bus_space_subregion(sc->sc_bar_mappings[barindex].t, 300 sc->sc_bar_mappings[barindex].h, 301 sc->sc_desc->ports[i].offset, 302 sc->sc_bar_mappings[barindex].s - 303 sc->sc_desc->ports[i].offset, 304 &subregion_handle) != 0) { 305 aprint_error_dev(self, 306 "couldn't get subregion for port %d\n", i); 307 continue; 308 } 309 paa.h = subregion_handle; 310 311 #if 0 312 printf("%s: port %d: %s @ (index %d) 0x%x (0x%lx, 0x%lx)\n", 313 device_xname(self), paa.port, puc_port_type_name(paa.type), 314 barindex, (int)paa.a, (long)paa.t, (long)paa.h); 315 #endif 316 317 locs[PUCCF_PORT] = i; 318 319 /* and configure it */ 320 sc->sc_ports[i].dev = config_found_sm_loc(self, "puc", locs, 321 &paa, puc_print, config_stdsubmatch); 322 } 323 } 324 325 CFATTACH_DECL_NEW(puc, sizeof(struct puc_softc), 326 puc_match, puc_attach, NULL, NULL); 327 328 static int 329 puc_print(void *aux, const char *pnp) 330 { 331 struct puc_attach_args *paa = aux; 332 333 if (pnp) 334 aprint_normal("%s at %s", puc_port_type_name(paa->type), pnp); 335 aprint_normal(" port %d", paa->port); 336 return (UNCONF); 337 } 338 339 const struct puc_device_description * 340 puc_find_description(pcireg_t vend, pcireg_t prod, pcireg_t svend, 341 pcireg_t sprod) 342 { 343 int i; 344 345 #define checkreg(val, index) \ 346 (((val) & puc_devices[i].rmask[(index)]) == puc_devices[i].rval[(index)]) 347 348 for (i = 0; puc_devices[i].name != NULL; i++) { 349 if (checkreg(vend, PUC_REG_VEND) && 350 checkreg(prod, PUC_REG_PROD) && 351 checkreg(svend, PUC_REG_SVEND) && 352 checkreg(sprod, PUC_REG_SPROD)) 353 return (&puc_devices[i]); 354 } 355 356 #undef checkreg 357 358 return (NULL); 359 } 360 361 static const char * 362 puc_port_type_name(int type) 363 { 364 365 switch (type) { 366 case PUC_PORT_TYPE_COM: 367 return "com"; 368 case PUC_PORT_TYPE_LPT: 369 return "lpt"; 370 default: 371 panic("puc_port_type_name %d", type); 372 } 373 } 374