1 /* $OpenBSD: puc.c,v 1.14 2009/03/03 16:52:25 deraadt Exp $ */ 2 /* $NetBSD: puc.c,v 1.3 1999/02/06 06:29:54 cgd Exp $ */ 3 4 /* 5 * Copyright (c) 1996, 1998, 1999 6 * Christopher G. Demetriou. 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. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by Christopher G. Demetriou 19 * for the NetBSD Project. 20 * 4. The name of the author may not be used to endorse or promote products 21 * derived from this software without specific prior written permission 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 33 */ 34 35 /* 36 * PCI "universal" communication card device driver, glues com, lpt, 37 * and similar ports to PCI via bridge chip often much larger than 38 * the devices being glued. 39 * 40 * Author: Christopher G. Demetriou, May 14, 1998 (derived from NetBSD 41 * sys/dev/pci/pciide.c, revision 1.6). 42 * 43 * These devices could be (and some times are) described as 44 * communications/{serial,parallel}, etc. devices with known 45 * programming interfaces, but those programming interfaces (in 46 * particular the BAR assignments for devices, etc.) in fact are not 47 * particularly well defined. 48 * 49 * After I/we have seen more of these devices, it may be possible 50 * to generalize some of these bits. In particular, devices which 51 * describe themselves as communications/serial/16[45]50, and 52 * communications/parallel/??? might be attached via direct 53 * 'com' and 'lpt' attachments to pci. 54 */ 55 56 #include <sys/param.h> 57 #include <sys/systm.h> 58 #include <sys/device.h> 59 60 #include <dev/pci/pcireg.h> 61 #include <dev/pci/pcivar.h> 62 #include <dev/pci/pucvar.h> 63 64 #include <dev/pci/pcidevs.h> 65 66 struct puc_pci_softc { 67 struct puc_softc sc_psc; 68 69 pci_chipset_tag_t pc; 70 pci_intr_handle_t ih; 71 }; 72 73 int puc_pci_match(struct device *, void *, void *); 74 void puc_pci_attach(struct device *, struct device *, void *); 75 int puc_pci_detach(struct device *, int); 76 const char *puc_pci_intr_string(struct puc_attach_args *); 77 void *puc_pci_intr_establish(struct puc_attach_args *, int, 78 int (*)(void *), void *, char *); 79 80 struct cfattach puc_pci_ca = { 81 sizeof(struct puc_pci_softc), puc_pci_match, 82 puc_pci_attach, puc_pci_detach 83 }; 84 85 struct cfdriver puc_cd = { 86 NULL, "puc", DV_DULL 87 }; 88 89 const char *puc_port_type_name(int); 90 91 int 92 puc_pci_match(struct device *parent, void *match, void *aux) 93 { 94 struct pci_attach_args *pa = aux; 95 const struct puc_device_description *desc; 96 pcireg_t bhlc, subsys; 97 98 bhlc = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_BHLC_REG); 99 if (PCI_HDRTYPE_TYPE(bhlc) != 0) 100 return (0); 101 102 subsys = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_SUBSYS_ID_REG); 103 104 desc = puc_find_description(PCI_VENDOR(pa->pa_id), 105 PCI_PRODUCT(pa->pa_id), PCI_VENDOR(subsys), PCI_PRODUCT(subsys)); 106 if (desc != NULL) 107 return (10); 108 109 /* 110 * Match class/subclass, so we can tell people to compile kernel 111 * with options that cause this driver to spew. 112 */ 113 if (PCI_CLASS(pa->pa_class) == PCI_CLASS_COMMUNICATIONS && 114 PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_BRIDGE_PCI) 115 return (1); 116 117 return (0); 118 } 119 120 const char * 121 puc_pci_intr_string(struct puc_attach_args *paa) 122 { 123 struct puc_pci_softc *sc = paa->puc; 124 125 return (pci_intr_string(sc->pc, sc->ih)); 126 } 127 128 void * 129 puc_pci_intr_establish(struct puc_attach_args *paa, int type, 130 int (*func)(void *), void *arg, char *name) 131 { 132 struct puc_pci_softc *sc = paa->puc; 133 struct puc_softc *psc = &sc->sc_psc; 134 135 psc->sc_ports[paa->port].intrhand = 136 pci_intr_establish(sc->pc, sc->ih, type, func, arg, name); 137 138 return (psc->sc_ports[paa->port].intrhand); 139 } 140 141 void 142 puc_pci_attach(struct device *parent, struct device *self, void *aux) 143 { 144 struct puc_pci_softc *psc = (struct puc_pci_softc *)self; 145 struct puc_softc *sc = &psc->sc_psc; 146 struct pci_attach_args *pa = aux; 147 struct puc_attach_args paa; 148 pcireg_t subsys; 149 int i; 150 151 subsys = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_SUBSYS_ID_REG); 152 sc->sc_desc = puc_find_description(PCI_VENDOR(pa->pa_id), 153 PCI_PRODUCT(pa->pa_id), PCI_VENDOR(subsys), PCI_PRODUCT(subsys)); 154 if (sc->sc_desc == NULL) { 155 /* 156 * This was a class/subclass match, so tell people to compile 157 * kernel with options that cause this driver to spew. 158 */ 159 #ifdef PUC_PRINT_REGS 160 printf(":\n"); 161 pci_conf_print(pa->pa_pc, pa->pa_tag, NULL); 162 #else 163 printf(": unknown PCI communications device\n"); 164 printf("%s: compile kernel with PUC_PRINT_REGS and larger\n", 165 sc->sc_dev.dv_xname); 166 printf("%s: message buffer (via 'options MSGBUFSIZE=...'),\n", 167 sc->sc_dev.dv_xname); 168 printf("%s: and report the result with sendbug(1)\n", 169 sc->sc_dev.dv_xname); 170 #endif 171 return; 172 } 173 174 puc_print_ports(sc->sc_desc); 175 176 /* 177 * XXX This driver assumes that 'com' ports attached to it 178 * XXX can not be console. That isn't unreasonable, because PCI 179 * XXX devices are supposed to be dynamically mapped, and com 180 * XXX console ports want fixed addresses. When/if baseboard 181 * XXX 'com' ports are identified as PCI/communications/serial 182 * XXX devices and are known to be mapped at the standard 183 * XXX addresses, if they can be the system console then we have 184 * XXX to cope with doing the mapping right. Then this will get 185 * XXX really ugly. Of course, by then we might know the real 186 * XXX definition of PCI/communications/serial, and attach 'com' 187 * XXX directly on PCI. 188 */ 189 for (i = 0; i < PUC_NBARS; i++) { 190 pcireg_t type; 191 int bar; 192 193 sc->sc_bar_mappings[i].mapped = 0; 194 bar = PCI_MAPREG_START + 4 * i; 195 if (!pci_mapreg_probe(pa->pa_pc, pa->pa_tag, bar, &type)) 196 continue; 197 198 sc->sc_bar_mappings[i].mapped = (pci_mapreg_map(pa, bar, type, 199 0, &sc->sc_bar_mappings[i].t, &sc->sc_bar_mappings[i].h, 200 &sc->sc_bar_mappings[i].a, &sc->sc_bar_mappings[i].s, 0) 201 == 0); 202 if (sc->sc_bar_mappings[i].mapped) 203 continue; 204 205 printf("%s: couldn't map BAR at offset 0x%lx\n", 206 sc->sc_dev.dv_xname, (long)bar); 207 } 208 209 /* Map interrupt. */ 210 psc->pc = pa->pa_pc; 211 if (pci_intr_map(pa, &psc->ih)) { 212 printf("%s: couldn't map interrupt\n", sc->sc_dev.dv_xname); 213 return; 214 } 215 216 paa.puc = sc; 217 paa.hwtype = 0; /* autodetect */ 218 paa.intr_string = &puc_pci_intr_string; 219 paa.intr_establish = &puc_pci_intr_establish; 220 221 /* 222 * If this is a serial card with a known specific chip, provide 223 * the UART type. 224 */ 225 if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_PLX && 226 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_PLX_CRONYX_OMEGA) 227 paa.hwtype = 0x08; /* XXX COM_UART_ST16C654 */ 228 229 puc_common_attach(sc, &paa); 230 } 231 232 void 233 puc_common_attach(struct puc_softc *sc, struct puc_attach_args *paa) 234 { 235 int i, bar; 236 237 /* 238 * XXX the sub-devices establish the interrupts, for the 239 * XXX following reasons: 240 * XXX 241 * XXX * we can't really know what IPLs they'd want 242 * XXX 243 * XXX * the MD dispatching code can ("should") dispatch 244 * XXX chained interrupts better than we can. 245 * XXX 246 * XXX It would be nice if we could indicate to the MD interrupt 247 * XXX handling code that the interrupt line used by the device 248 * XXX was a PCI (level triggered) interrupt. 249 * XXX 250 * XXX It's not pretty, but hey, what is? 251 */ 252 253 /* Configure each port. */ 254 for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) { 255 /* Skip unknown ports */ 256 if (sc->sc_desc->ports[i].type != PUC_PORT_TYPE_COM && 257 sc->sc_desc->ports[i].type != PUC_PORT_TYPE_LPT) 258 continue; 259 /* make sure the base address register is mapped */ 260 bar = PUC_PORT_BAR_INDEX(sc->sc_desc->ports[i].bar); 261 if (!sc->sc_bar_mappings[bar].mapped) { 262 printf("%s: %s port uses unmapped BAR (0x%x)\n", 263 sc->sc_dev.dv_xname, 264 puc_port_type_name(sc->sc_desc->ports[i].type), 265 sc->sc_desc->ports[i].bar); 266 continue; 267 } 268 269 /* set up to configure the child device */ 270 paa->port = i; 271 paa->type = sc->sc_desc->ports[i].type; 272 paa->flags = sc->sc_desc->ports[i].flags; 273 paa->a = sc->sc_bar_mappings[bar].a; 274 paa->t = sc->sc_bar_mappings[bar].t; 275 276 if (bus_space_subregion(sc->sc_bar_mappings[bar].t, 277 sc->sc_bar_mappings[bar].h, sc->sc_desc->ports[i].offset, 278 sc->sc_bar_mappings[bar].s - sc->sc_desc->ports[i].offset, 279 &paa->h)) { 280 printf("%s: couldn't get subregion for port %d\n", 281 sc->sc_dev.dv_xname, i); 282 continue; 283 } 284 285 #if 0 286 if (autoconf_verbose) 287 printf("%s: port %d: %s @ (index %d) 0x%x " 288 "(0x%lx, 0x%lx)\n", sc->sc_dev.dv_xname, paa->port, 289 puc_port_type_name(paa->type), bar, (int)paa->a, 290 (long)paa->t, (long)paa->h); 291 #endif 292 293 /* and configure it */ 294 sc->sc_ports[i].dev = config_found_sm(&sc->sc_dev, paa, 295 puc_print, puc_submatch); 296 } 297 } 298 299 int 300 puc_pci_detach(struct device *self, int flags) 301 { 302 struct puc_pci_softc *sc = (struct puc_pci_softc *)self; 303 struct puc_softc *psc = &sc->sc_psc; 304 int i, rv; 305 306 for (i = PUC_MAX_PORTS; i--; ) { 307 if (psc->sc_ports[i].intrhand) 308 pci_intr_disestablish(sc->pc, 309 psc->sc_ports[i].intrhand); 310 if (psc->sc_ports[i].dev) 311 if ((rv = config_detach(psc->sc_ports[i].dev, flags))) 312 return (rv); 313 } 314 315 for (i = PUC_NBARS; i--; ) 316 if (psc->sc_bar_mappings[i].mapped) 317 bus_space_unmap(psc->sc_bar_mappings[i].t, 318 psc->sc_bar_mappings[i].h, 319 psc->sc_bar_mappings[i].s); 320 321 return (0); 322 } 323 324 int 325 puc_print(void *aux, const char *pnp) 326 { 327 struct puc_attach_args *paa = aux; 328 329 if (pnp) 330 printf("%s at %s", puc_port_type_name(paa->type), pnp); 331 printf(" port %d", paa->port); 332 return (UNCONF); 333 } 334 335 int 336 puc_submatch(struct device *parent, void *vcf, void *aux) 337 { 338 struct cfdata *cf = (struct cfdata *)vcf; 339 struct puc_attach_args *aa = aux; 340 341 if (cf->cf_loc[0] != -1 && cf->cf_loc[0] != aa->port) 342 return 0; 343 return ((*cf->cf_attach->ca_match)(parent, cf, aux)); 344 } 345 346 const struct puc_device_description * 347 puc_find_description(u_int16_t vend, u_int16_t prod, 348 u_int16_t svend, u_int16_t sprod) 349 { 350 int i; 351 352 #define checkreg(val, index) \ 353 (((val) & puc_devices[i].rmask[(index)]) == puc_devices[i].rval[(index)]) 354 #define pucdevdone(idx) \ 355 (puc_devices[idx].rval[0] == 0 && puc_devices[idx].rval[1] == 0 \ 356 && puc_devices[idx].rval[2] == 0 && puc_devices[idx].rval[3] == 0) 357 358 for (i = 0; !pucdevdone(i); i++) { 359 if (checkreg(vend, PUC_REG_VEND) && 360 checkreg(prod, PUC_REG_PROD) && 361 checkreg(svend, PUC_REG_SVEND) && 362 checkreg(sprod, PUC_REG_SPROD)) 363 return (&puc_devices[i]); 364 } 365 366 #undef devdone 367 #undef checkreg 368 369 return (NULL); 370 } 371 372 const char * 373 puc_port_type_name(int type) 374 { 375 376 switch (type) { 377 case PUC_PORT_TYPE_COM: 378 return "com"; 379 case PUC_PORT_TYPE_LPT: 380 return "lpt"; 381 default: 382 return "unknown"; 383 } 384 } 385 386 void 387 puc_print_ports(const struct puc_device_description *desc) 388 { 389 int i, ncom, nlpt; 390 391 printf(": ports: "); 392 for (i = ncom = nlpt = 0; PUC_PORT_VALID(desc, i); i++) { 393 switch (desc->ports[i].type) { 394 case PUC_PORT_TYPE_COM: 395 ncom++; 396 break; 397 case PUC_PORT_TYPE_LPT: 398 nlpt++; 399 break; 400 default: 401 printf("port %d unknown type %d ", i, 402 desc->ports[i].type); 403 } 404 } 405 if (ncom) 406 printf("%d com", ncom); 407 if (nlpt) { 408 if (ncom) 409 printf(", "); 410 printf("%d lpt", nlpt); 411 } 412 printf("\n"); 413 } 414