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