1 /* $NetBSD: puc.c,v 1.21 2004/09/13 12:55:48 drochner 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.21 2004/09/13 12:55:48 drochner 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 <sys/termios.h> 66 #include <dev/ic/comreg.h> 67 #include <dev/ic/comvar.h> 68 69 #include "locators.h" 70 #include "opt_puccn.h" 71 72 struct puc_softc { 73 struct device sc_dev; 74 75 /* static configuration data */ 76 const struct puc_device_description *sc_desc; 77 78 /* card-global dynamic data */ 79 void *sc_ih; 80 struct { 81 int mapped; 82 bus_addr_t a; 83 bus_size_t s; 84 bus_space_tag_t t; 85 bus_space_handle_t h; 86 } sc_bar_mappings[6]; /* XXX constant */ 87 88 /* per-port dynamic data */ 89 struct { 90 struct device *dev; 91 92 /* filled in by port attachments */ 93 int (*ihand) __P((void *)); 94 void *ihandarg; 95 } sc_ports[PUC_MAX_PORTS]; 96 }; 97 98 int puc_match __P((struct device *, struct cfdata *, void *)); 99 void puc_attach __P((struct device *, struct device *, void *)); 100 int puc_print __P((void *, const char *)); 101 int puc_submatch __P((struct device *, struct cfdata *, 102 const locdesc_t *, void *)); 103 104 CFATTACH_DECL(puc, sizeof(struct puc_softc), 105 puc_match, puc_attach, NULL, NULL); 106 107 const struct puc_device_description * 108 puc_find_description __P((pcireg_t, pcireg_t, pcireg_t, pcireg_t)); 109 static const char * 110 puc_port_type_name __P((int)); 111 112 int 113 puc_match(parent, match, aux) 114 struct device *parent; 115 struct cfdata *match; 116 void *aux; 117 { 118 struct pci_attach_args *pa = aux; 119 const struct puc_device_description *desc; 120 pcireg_t bhlc, subsys; 121 122 bhlc = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_BHLC_REG); 123 if (PCI_HDRTYPE_TYPE(bhlc) != 0) 124 return (0); 125 126 subsys = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_SUBSYS_ID_REG); 127 128 desc = puc_find_description(PCI_VENDOR(pa->pa_id), 129 PCI_PRODUCT(pa->pa_id), PCI_VENDOR(subsys), PCI_PRODUCT(subsys)); 130 if (desc != NULL) 131 return (10); 132 133 #if 0 134 /* 135 * XXX this is obviously bogus. eventually, we might want 136 * XXX to match communications/modem, etc., but that needs some 137 * XXX special work in the match fn. 138 */ 139 /* 140 * Match class/subclass, so we can tell people to compile kernel 141 * with options that cause this driver to spew. 142 */ 143 if (PCI_CLASS(pa->pa_class) == PCI_CLASS_COMMUNICATIONS) { 144 switch (PCI_SUBCLASS(pa->pa_class)) { 145 case PCI_SUBCLASS_COMMUNICATIONS_SERIAL: 146 case PCI_SUBCLASS_COMMUNICATIONS_MODEM: 147 return (1); 148 } 149 } 150 #endif 151 152 return (0); 153 } 154 155 void 156 puc_attach(parent, self, aux) 157 struct device *parent, *self; 158 void *aux; 159 { 160 struct puc_softc *sc = (struct puc_softc *)self; 161 struct pci_attach_args *pa = aux; 162 struct puc_attach_args paa; 163 pci_intr_handle_t intrhandle; 164 pcireg_t subsys; 165 int i, barindex; 166 bus_addr_t base; 167 bus_space_tag_t tag; 168 #ifdef PUCCN 169 bus_space_handle_t ioh; 170 #endif 171 int help[2]; 172 locdesc_t *ldesc = (void *)help; /* XXX */ 173 174 subsys = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_SUBSYS_ID_REG); 175 sc->sc_desc = puc_find_description(PCI_VENDOR(pa->pa_id), 176 PCI_PRODUCT(pa->pa_id), PCI_VENDOR(subsys), PCI_PRODUCT(subsys)); 177 if (sc->sc_desc == NULL) { 178 /* 179 * This was a class/subclass match, so tell people to compile 180 * kernel with options that cause this driver to spew. 181 */ 182 #ifdef PUC_PRINT_REGS 183 printf(":\n"); 184 pci_conf_print(pa->pa_pc, pa->pa_tag, NULL); 185 #else 186 printf(": unknown PCI communications device\n"); 187 printf("%s: compile kernel with PUC_PRINT_REGS and larger\n", 188 sc->sc_dev.dv_xname); 189 printf("%s: mesage buffer (via 'options MSGBUFSIZE=...'),\n", 190 sc->sc_dev.dv_xname); 191 printf("%s: and report the result with send-pr\n", 192 sc->sc_dev.dv_xname); 193 #endif 194 return; 195 } 196 197 printf(": %s (", sc->sc_desc->name); 198 for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) 199 printf("%s%s", i ? ", " : "", 200 puc_port_type_name(sc->sc_desc->ports[i].type)); 201 printf(")\n"); 202 203 for (i = 0; i < 6; i++) { 204 pcireg_t bar, type; 205 206 sc->sc_bar_mappings[i].mapped = 0; 207 208 bar = pci_conf_read(pa->pa_pc, pa->pa_tag, 209 PCI_MAPREG_START + 4 * i); /* XXX const */ 210 if (bar == 0) /* BAR not implemented(?) */ 211 continue; 212 213 type = (PCI_MAPREG_TYPE(bar) == PCI_MAPREG_TYPE_IO ? 214 PCI_MAPREG_TYPE_IO : PCI_MAPREG_MEM_TYPE(bar)); 215 216 if (type == PCI_MAPREG_TYPE_IO) { 217 tag = pa->pa_iot; 218 base = PCI_MAPREG_IO_ADDR(bar); 219 } else { 220 tag = pa->pa_memt; 221 base = PCI_MAPREG_MEM_ADDR(bar); 222 } 223 #ifdef PUCCN 224 if (com_is_console(tag, base, &ioh)) { 225 sc->sc_bar_mappings[i].mapped = 1; 226 sc->sc_bar_mappings[i].a = base; 227 sc->sc_bar_mappings[i].s = COM_NPORTS; 228 sc->sc_bar_mappings[i].t = tag; 229 sc->sc_bar_mappings[i].h = ioh; 230 continue; 231 } 232 #endif 233 sc->sc_bar_mappings[i].mapped = (pci_mapreg_map(pa, 234 PCI_MAPREG_START + 4 * i, type, 0, 235 &sc->sc_bar_mappings[i].t, &sc->sc_bar_mappings[i].h, 236 &sc->sc_bar_mappings[i].a, &sc->sc_bar_mappings[i].s) 237 == 0); 238 if (sc->sc_bar_mappings[i].mapped) 239 continue; 240 241 printf("%s: couldn't map BAR at offset 0x%lx\n", 242 sc->sc_dev.dv_xname, (long)(PCI_MAPREG_START + 4 * i)); 243 } 244 245 /* Map interrupt. */ 246 if (pci_intr_map(pa, &intrhandle)) { 247 printf("%s: couldn't map interrupt\n", sc->sc_dev.dv_xname); 248 return; 249 } 250 /* 251 * XXX the sub-devices establish the interrupts, for the 252 * XXX following reasons: 253 * XXX 254 * XXX * we can't really know what IPLs they'd want 255 * XXX 256 * XXX * the MD dispatching code can ("should") dispatch 257 * XXX chained interrupts better than we can. 258 * XXX 259 * XXX It would be nice if we could indicate to the MD interrupt 260 * XXX handling code that the interrupt line used by the device 261 * XXX was a PCI (level triggered) interrupt. 262 * XXX 263 * XXX It's not pretty, but hey, what is? 264 */ 265 266 /* Configure each port. */ 267 for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) { 268 bus_space_handle_t subregion_handle; 269 270 /* make sure the base address register is mapped */ 271 barindex = PUC_PORT_BAR_INDEX(sc->sc_desc->ports[i].bar); 272 if (!sc->sc_bar_mappings[barindex].mapped) { 273 printf("%s: %s port uses unmapped BAR (0x%x)\n", 274 sc->sc_dev.dv_xname, 275 puc_port_type_name(sc->sc_desc->ports[i].type), 276 sc->sc_desc->ports[i].bar); 277 continue; 278 } 279 280 /* set up to configure the child device */ 281 paa.port = i; 282 paa.type = sc->sc_desc->ports[i].type; 283 paa.flags = sc->sc_desc->ports[i].flags; 284 paa.pc = pa->pa_pc; 285 paa.tag = pa->pa_tag; 286 paa.intrhandle = intrhandle; 287 paa.a = sc->sc_bar_mappings[barindex].a; 288 paa.t = sc->sc_bar_mappings[barindex].t; 289 paa.dmat = pa->pa_dmat; 290 paa.dmat64 = pa->pa_dmat64; 291 292 if ( 293 #ifdef PUCCN 294 !com_is_console(sc->sc_bar_mappings[barindex].t, 295 sc->sc_bar_mappings[barindex].a, &subregion_handle) 296 && 297 #endif 298 bus_space_subregion(sc->sc_bar_mappings[barindex].t, 299 sc->sc_bar_mappings[barindex].h, 300 sc->sc_desc->ports[i].offset, 301 sc->sc_bar_mappings[barindex].s - 302 sc->sc_desc->ports[i].offset, 303 &subregion_handle) != 0) { 304 printf("%s: couldn't get subregion for port %d\n", 305 sc->sc_dev.dv_xname, i); 306 continue; 307 } 308 paa.h = subregion_handle; 309 310 #if 0 311 printf("%s: port %d: %s @ (index %d) 0x%x (0x%lx, 0x%lx)\n", 312 sc->sc_dev.dv_xname, paa.port, 313 puc_port_type_name(paa.type), barindex, (int)paa.a, 314 (long)paa.t, (long)paa.h); 315 #endif 316 317 ldesc->len = 1; 318 ldesc->locs[PUCCF_PORT] = i; 319 320 /* and configure it */ 321 sc->sc_ports[i].dev = config_found_sm_loc(self, "puc", ldesc, 322 &paa, puc_print, puc_submatch); 323 } 324 } 325 326 int 327 puc_print(aux, pnp) 328 void *aux; 329 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 int 340 puc_submatch(parent, cf, ldesc, aux) 341 struct device *parent; 342 struct cfdata *cf; 343 const locdesc_t *ldesc; 344 void *aux; 345 { 346 347 if (cf->cf_loc[PUCCF_PORT] != PUCCF_PORT_DEFAULT && 348 cf->cf_loc[PUCCF_PORT] != ldesc->locs[PUCCF_PORT]) 349 return 0; 350 351 return (config_match(parent, cf, aux)); 352 } 353 354 const struct puc_device_description * 355 puc_find_description(vend, prod, svend, sprod) 356 pcireg_t vend, prod, svend, sprod; 357 { 358 int i; 359 360 #define checkreg(val, index) \ 361 (((val) & puc_devices[i].rmask[(index)]) == puc_devices[i].rval[(index)]) 362 363 for (i = 0; puc_devices[i].name != NULL; i++) { 364 if (checkreg(vend, PUC_REG_VEND) && 365 checkreg(prod, PUC_REG_PROD) && 366 checkreg(svend, PUC_REG_SVEND) && 367 checkreg(sprod, PUC_REG_SPROD)) 368 return (&puc_devices[i]); 369 } 370 371 #undef checkreg 372 373 return (NULL); 374 } 375 376 static const char * 377 puc_port_type_name(type) 378 int type; 379 { 380 381 switch (type) { 382 case PUC_PORT_TYPE_COM: 383 return "com"; 384 case PUC_PORT_TYPE_LPT: 385 return "lpt"; 386 default: 387 panic("puc_port_type_name %d", type); 388 } 389 } 390