1 /* $OpenBSD: puc.c,v 1.23 2013/05/30 16:15:02 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 #include <sys/tty.h> 60 61 #include <dev/pci/pcireg.h> 62 #include <dev/pci/pcivar.h> 63 #include <dev/pci/pucvar.h> 64 65 #include <dev/pci/pcidevs.h> 66 67 #include <dev/ic/comreg.h> 68 #include <dev/ic/comvar.h> 69 70 struct puc_pci_softc { 71 struct puc_softc sc_psc; 72 73 pci_chipset_tag_t pc; 74 pci_intr_handle_t ih; 75 }; 76 77 int puc_pci_match(struct device *, void *, void *); 78 void puc_pci_attach(struct device *, struct device *, void *); 79 int puc_pci_detach(struct device *, int); 80 const char *puc_pci_intr_string(struct puc_attach_args *); 81 void *puc_pci_intr_establish(struct puc_attach_args *, int, 82 int (*)(void *), void *, char *); 83 84 struct cfattach puc_pci_ca = { 85 sizeof(struct puc_pci_softc), puc_pci_match, 86 puc_pci_attach, puc_pci_detach 87 }; 88 89 struct cfdriver puc_cd = { 90 NULL, "puc", DV_DULL 91 }; 92 93 const char *puc_port_type_name(int); 94 95 int 96 puc_pci_match(struct device *parent, void *match, void *aux) 97 { 98 struct pci_attach_args *pa = aux; 99 const struct puc_device_description *desc; 100 pcireg_t bhlc, subsys; 101 102 bhlc = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_BHLC_REG); 103 if (PCI_HDRTYPE_TYPE(bhlc) != 0) 104 return (0); 105 106 subsys = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_SUBSYS_ID_REG); 107 108 desc = puc_find_description(PCI_VENDOR(pa->pa_id), 109 PCI_PRODUCT(pa->pa_id), PCI_VENDOR(subsys), PCI_PRODUCT(subsys)); 110 if (desc != NULL) 111 return (1); 112 113 return (0); 114 } 115 116 const char * 117 puc_pci_intr_string(struct puc_attach_args *paa) 118 { 119 struct puc_pci_softc *sc = paa->puc; 120 121 return (pci_intr_string(sc->pc, sc->ih)); 122 } 123 124 void * 125 puc_pci_intr_establish(struct puc_attach_args *paa, int type, 126 int (*func)(void *), void *arg, char *name) 127 { 128 struct puc_pci_softc *sc = paa->puc; 129 struct puc_softc *psc = &sc->sc_psc; 130 131 psc->sc_ports[paa->port].intrhand = 132 pci_intr_establish(sc->pc, sc->ih, type, func, arg, name); 133 134 return (psc->sc_ports[paa->port].intrhand); 135 } 136 137 void 138 puc_pci_attach(struct device *parent, struct device *self, void *aux) 139 { 140 struct puc_pci_softc *psc = (struct puc_pci_softc *)self; 141 struct puc_softc *sc = &psc->sc_psc; 142 struct pci_attach_args *pa = aux; 143 struct puc_attach_args paa; 144 pcireg_t subsys; 145 int i; 146 147 subsys = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_SUBSYS_ID_REG); 148 sc->sc_desc = puc_find_description(PCI_VENDOR(pa->pa_id), 149 PCI_PRODUCT(pa->pa_id), PCI_VENDOR(subsys), PCI_PRODUCT(subsys)); 150 151 puc_print_ports(sc->sc_desc); 152 153 for (i = 0; i < PUC_NBARS; i++) { 154 pcireg_t type; 155 int bar; 156 157 sc->sc_bar_mappings[i].mapped = 0; 158 bar = PCI_MAPREG_START + 4 * i; 159 if (!pci_mapreg_probe(pa->pa_pc, pa->pa_tag, bar, &type)) 160 continue; 161 162 sc->sc_bar_mappings[i].mapped = (pci_mapreg_map(pa, bar, type, 163 0, &sc->sc_bar_mappings[i].t, &sc->sc_bar_mappings[i].h, 164 &sc->sc_bar_mappings[i].a, &sc->sc_bar_mappings[i].s, 0) 165 == 0); 166 if (sc->sc_bar_mappings[i].mapped) 167 continue; 168 169 /* 170 * If a port on this card is used as serial console, 171 * mapping the associated BAR will fail because the 172 * bus space is already mapped. In that case, we try 173 * to re-use the already existing mapping. 174 * Unfortunately this means that if a BAR is used to 175 * support multiple ports, only the first port will 176 * work. 177 */ 178 if (pci_mapreg_info(pa->pa_pc, pa->pa_tag, bar, type, 179 &sc->sc_bar_mappings[i].a, NULL, NULL) == 0 && 180 pa->pa_iot == comconsiot && 181 sc->sc_bar_mappings[i].a == comconsaddr) { 182 sc->sc_bar_mappings[i].t = comconsiot; 183 sc->sc_bar_mappings[i].h = comconsioh; 184 sc->sc_bar_mappings[i].s = COM_NPORTS; 185 sc->sc_bar_mappings[i].mapped = 1; 186 continue; 187 } 188 189 printf("%s: couldn't map BAR at offset 0x%lx\n", 190 sc->sc_dev.dv_xname, (long)bar); 191 } 192 193 /* Map interrupt. */ 194 psc->pc = pa->pa_pc; 195 if (pci_intr_map(pa, &psc->ih)) { 196 printf("%s: couldn't map interrupt\n", sc->sc_dev.dv_xname); 197 return; 198 } 199 200 paa.puc = sc; 201 paa.intr_string = &puc_pci_intr_string; 202 paa.intr_establish = &puc_pci_intr_establish; 203 204 puc_common_attach(sc, &paa); 205 } 206 207 void 208 puc_common_attach(struct puc_softc *sc, struct puc_attach_args *paa) 209 { 210 const struct puc_device_description *desc = sc->sc_desc; 211 int i, bar; 212 213 /* Configure each port. */ 214 for (i = 0; i < PUC_MAX_PORTS; i++) { 215 if (desc->ports[i].type == 0) /* neither com or lpt */ 216 continue; 217 /* make sure the base address register is mapped */ 218 bar = PUC_PORT_BAR_INDEX(desc->ports[i].bar); 219 if (!sc->sc_bar_mappings[bar].mapped) { 220 printf("%s: %s port uses unmapped BAR (0x%x)\n", 221 sc->sc_dev.dv_xname, 222 puc_port_type_name(desc->ports[i].type), 223 desc->ports[i].bar); 224 continue; 225 } 226 227 /* set up to configure the child device */ 228 paa->port = i; 229 paa->a = sc->sc_bar_mappings[bar].a; 230 paa->t = sc->sc_bar_mappings[bar].t; 231 232 paa->type = desc->ports[i].type; 233 234 if (desc->ports[i].offset >= sc->sc_bar_mappings[bar].s || 235 bus_space_subregion(sc->sc_bar_mappings[bar].t, 236 sc->sc_bar_mappings[bar].h, desc->ports[i].offset, 237 sc->sc_bar_mappings[bar].s - desc->ports[i].offset, 238 &paa->h)) { 239 printf("%s: couldn't get subregion for port %d\n", 240 sc->sc_dev.dv_xname, i); 241 continue; 242 } 243 244 #if 0 245 if (autoconf_verbose) 246 printf("%s: port %d: %s @ (index %d) 0x%x " 247 "(0x%lx, 0x%lx)\n", sc->sc_dev.dv_xname, paa->port, 248 puc_port_type_name(paa->type), bar, (int)paa->a, 249 (long)paa->t, (long)paa->h); 250 #endif 251 252 /* and configure it */ 253 sc->sc_ports[i].dev = config_found_sm(&sc->sc_dev, paa, 254 puc_print, puc_submatch); 255 } 256 } 257 258 int 259 puc_pci_detach(struct device *self, int flags) 260 { 261 struct puc_pci_softc *sc = (struct puc_pci_softc *)self; 262 struct puc_softc *psc = &sc->sc_psc; 263 int i, rv; 264 265 for (i = PUC_MAX_PORTS; i--; ) { 266 if (psc->sc_ports[i].intrhand) 267 pci_intr_disestablish(sc->pc, 268 psc->sc_ports[i].intrhand); 269 if (psc->sc_ports[i].dev) 270 if ((rv = config_detach(psc->sc_ports[i].dev, flags))) 271 return (rv); 272 } 273 274 for (i = PUC_NBARS; i--; ) 275 if (psc->sc_bar_mappings[i].mapped) 276 bus_space_unmap(psc->sc_bar_mappings[i].t, 277 psc->sc_bar_mappings[i].h, 278 psc->sc_bar_mappings[i].s); 279 280 return (0); 281 } 282 283 int 284 puc_print(void *aux, const char *pnp) 285 { 286 struct puc_attach_args *paa = aux; 287 288 if (pnp) 289 printf("%s at %s", puc_port_type_name(paa->type), pnp); 290 printf(" port %d", paa->port); 291 return (UNCONF); 292 } 293 294 int 295 puc_submatch(struct device *parent, void *vcf, void *aux) 296 { 297 struct cfdata *cf = (struct cfdata *)vcf; 298 struct puc_attach_args *aa = aux; 299 300 if (cf->cf_loc[0] != -1 && cf->cf_loc[0] != aa->port) 301 return 0; 302 return ((*cf->cf_attach->ca_match)(parent, cf, aux)); 303 } 304 305 const struct puc_device_description * 306 puc_find_description(u_int16_t vend, u_int16_t prod, 307 u_int16_t svend, u_int16_t sprod) 308 { 309 int i; 310 311 for (i = 0; i < puc_ndevs; i++) 312 if ((vend & puc_devs[i].rmask[0]) == puc_devs[i].rval[0] && 313 (prod & puc_devs[i].rmask[1]) == puc_devs[i].rval[1] && 314 (svend & puc_devs[i].rmask[2]) == puc_devs[i].rval[2] && 315 (sprod & puc_devs[i].rmask[3]) == puc_devs[i].rval[3]) 316 return (&puc_devs[i]); 317 318 return (NULL); 319 } 320 321 const char * 322 puc_port_type_name(int type) 323 { 324 325 if (PUC_IS_COM(type)) 326 return "com"; 327 if (PUC_IS_LPT(type)) 328 return "lpt"; 329 return (NULL); 330 } 331 332 void 333 puc_print_ports(const struct puc_device_description *desc) 334 { 335 int i, ncom, nlpt; 336 337 printf(": ports: "); 338 for (i = ncom = nlpt = 0; i < PUC_MAX_PORTS; i++) { 339 if (PUC_IS_COM(desc->ports[i].type)) 340 ncom++; 341 else if (PUC_IS_LPT(desc->ports[i].type)) 342 nlpt++; 343 } 344 if (ncom) 345 printf("%d com", ncom); 346 if (nlpt) { 347 if (ncom) 348 printf(", "); 349 printf("%d lpt", nlpt); 350 } 351 printf("\n"); 352 } 353