puc.c revision 118292
1221431Sjonathan/* $NetBSD: puc.c,v 1.7 2000/07/29 17:43:38 jlam Exp $ */ 2221431Sjonathan 3221431Sjonathan/*- 4224651Sjonathan * Copyright (c) 2002 JF Hay. All rights reserved. 5224651Sjonathan * Copyright (c) 2000 M. Warner Losh. All rights reserved. 6224651Sjonathan * 7224651Sjonathan * Redistribution and use in source and binary forms, with or without 8224793Sjonathan * modification, are permitted provided that the following conditions 9224793Sjonathan * are met: 10224793Sjonathan * 1. Redistributions of source code must retain the above copyright 11221431Sjonathan * notice unmodified, this list of conditions, and the following 12221431Sjonathan * disclaimer. 13221431Sjonathan * 2. Redistributions in binary form must reproduce the above copyright 14224651Sjonathan * notice, this list of conditions and the following disclaimer in the 15224651Sjonathan * documentation and/or other materials provided with the distribution. 16224651Sjonathan * 17224651Sjonathan * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18224651Sjonathan * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19224651Sjonathan * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20224651Sjonathan * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21224651Sjonathan * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22224651Sjonathan * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23224651Sjonathan * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24221431Sjonathan * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29/* 30 * Copyright (c) 1996, 1998, 1999 31 * Christopher G. Demetriou. All rights reserved. 32 * 33 * Redistribution and use in source and binary forms, with or without 34 * modification, are permitted provided that the following conditions 35 * are met: 36 * 1. Redistributions of source code must retain the above copyright 37 * notice, this list of conditions and the following disclaimer. 38 * 2. Redistributions in binary form must reproduce the above copyright 39 * notice, this list of conditions and the following disclaimer in the 40 * documentation and/or other materials provided with the distribution. 41 * 3. All advertising materials mentioning features or use of this software 42 * must display the following acknowledgement: 43 * This product includes software developed by Christopher G. Demetriou 44 * for the NetBSD Project. 45 * 4. The name of the author may not be used to endorse or promote products 46 * derived from this software without specific prior written permission 47 * 48 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 49 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 50 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 51 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 52 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 53 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 54 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 55 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 56 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 57 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 58 */ 59 60#include <sys/cdefs.h> 61__FBSDID("$FreeBSD: head/sys/dev/puc/puc.c 118292 2003-08-01 02:25:32Z ambrisko $"); 62 63/* 64 * PCI "universal" communication card device driver, glues com, lpt, 65 * and similar ports to PCI via bridge chip often much larger than 66 * the devices being glued. 67 * 68 * Author: Christopher G. Demetriou, May 14, 1998 (derived from NetBSD 69 * sys/dev/pci/pciide.c, revision 1.6). 70 * 71 * These devices could be (and some times are) described as 72 * communications/{serial,parallel}, etc. devices with known 73 * programming interfaces, but those programming interfaces (in 74 * particular the BAR assignments for devices, etc.) in fact are not 75 * particularly well defined. 76 * 77 * After I/we have seen more of these devices, it may be possible 78 * to generalize some of these bits. In particular, devices which 79 * describe themselves as communications/serial/16[45]50, and 80 * communications/parallel/??? might be attached via direct 81 * 'com' and 'lpt' attachments to pci. 82 */ 83 84#include "opt_puc.h" 85 86#include <sys/param.h> 87#include <sys/systm.h> 88#include <sys/kernel.h> 89#include <sys/bus.h> 90#include <sys/conf.h> 91#include <sys/malloc.h> 92 93#include <machine/bus.h> 94#include <machine/resource.h> 95#include <sys/rman.h> 96 97#include <dev/pci/pcireg.h> 98#include <dev/pci/pcivar.h> 99 100#define PUC_ENTRAILS 1 101#include <dev/puc/pucvar.h> 102 103struct puc_device { 104 struct resource_list resources; 105 u_int serialfreq; 106}; 107 108static void puc_intr(void *arg); 109 110static int puc_find_free_unit(char *); 111#ifdef PUC_DEBUG 112static void puc_print_resource_list(struct resource_list *); 113#endif 114 115devclass_t puc_devclass; 116 117static int 118puc_port_bar_index(struct puc_softc *sc, int bar) 119{ 120 int i; 121 122 for (i = 0; i < PUC_MAX_BAR; i += 1) { 123 if (!sc->sc_bar_mappings[i].used) 124 break; 125 if (sc->sc_bar_mappings[i].bar == bar) 126 return (i); 127 } 128 sc->sc_bar_mappings[i].bar = bar; 129 sc->sc_bar_mappings[i].used = 1; 130 return (i); 131} 132 133static int 134puc_probe_ilr(struct puc_softc *sc, struct resource *res) 135{ 136 u_char t1, t2; 137 int i; 138 139 switch (sc->sc_desc->ilr_type) { 140 case PUC_ILR_TYPE_DIGI: 141 sc->ilr_st = rman_get_bustag(res); 142 sc->ilr_sh = rman_get_bushandle(res); 143 for (i = 0; i < 2 && sc->sc_desc->ilr_offset[i] != 0; i++) { 144 t1 = bus_space_read_1(sc->ilr_st, sc->ilr_sh, 145 sc->sc_desc->ilr_offset[i]); 146 t1 = ~t1; 147 bus_space_write_1(sc->ilr_st, sc->ilr_sh, 148 sc->sc_desc->ilr_offset[i], t1); 149 t2 = bus_space_read_1(sc->ilr_st, sc->ilr_sh, 150 sc->sc_desc->ilr_offset[i]); 151 if (t2 == t1) 152 return (0); 153 } 154 return (1); 155 156 default: 157 break; 158 } 159 return (0); 160} 161 162int 163puc_attach(device_t dev, const struct puc_device_description *desc) 164{ 165 char *typestr; 166 int bidx, childunit, i, irq_setup, rid, type; 167 struct puc_softc *sc; 168 struct puc_device *pdev; 169 struct resource *res; 170 struct resource_list_entry *rle; 171 172 sc = (struct puc_softc *)device_get_softc(dev); 173 bzero(sc, sizeof(*sc)); 174 sc->sc_desc = desc; 175 if (sc->sc_desc == NULL) 176 return (ENXIO); 177 178#ifdef PUC_DEBUG 179 bootverbose = 1; 180 181 printf("puc: name: %s\n", sc->sc_desc->name); 182#endif 183 rid = 0; 184 res = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1, 185 RF_ACTIVE | RF_SHAREABLE); 186 if (!res) 187 return (ENXIO); 188 189 sc->irqres = res; 190 sc->irqrid = rid; 191#ifdef PUC_FASTINTR 192 irq_setup = BUS_SETUP_INTR(device_get_parent(dev), dev, res, 193 INTR_TYPE_TTY | INTR_FAST, puc_intr, sc, &sc->intr_cookie); 194 if (irq_setup == 0) 195 sc->fastintr = INTR_FAST; 196 else 197 irq_setup = BUS_SETUP_INTR(device_get_parent(dev), dev, res, 198 INTR_TYPE_TTY, puc_intr, sc, &sc->intr_cookie); 199#else 200 irq_setup = BUS_SETUP_INTR(device_get_parent(dev), dev, res, 201 INTR_TYPE_TTY, puc_intr, sc, &sc->intr_cookie); 202#endif 203 if (irq_setup != 0) 204 return (ENXIO); 205 206 rid = 0; 207 for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) { 208 if (i > 0 && rid == sc->sc_desc->ports[i].bar) 209 sc->barmuxed = 1; 210 rid = sc->sc_desc->ports[i].bar; 211 bidx = puc_port_bar_index(sc, rid); 212 213 if (sc->sc_bar_mappings[bidx].res != NULL) 214 continue; 215 216 type = (sc->sc_desc->ports[i].flags & PUC_FLAGS_MEMORY) 217 ? SYS_RES_MEMORY : SYS_RES_IOPORT; 218 219 res = bus_alloc_resource(dev, type, &rid, 0ul, ~0ul, 1, 220 RF_ACTIVE); 221 if (res == NULL) { 222 printf("could not get resource\n"); 223 continue; 224 } 225 sc->sc_bar_mappings[bidx].type = type; 226 sc->sc_bar_mappings[bidx].res = res; 227 228 if (sc->sc_desc->ilr_type != PUC_ILR_TYPE_NONE) { 229 sc->ilr_enabled = puc_probe_ilr(sc, res); 230 if (sc->ilr_enabled) 231 device_printf(dev, "ILR enabled\n"); 232 else 233 device_printf(dev, "ILR disabled\n"); 234 } 235#ifdef PUC_DEBUG 236 printf("%s rid %d bst %x, start %x, end %x\n", 237 (type == SYS_RES_MEMORY) ? "memory" : "port", rid, 238 (u_int)rman_get_bustag(res), (u_int)rman_get_start(res), 239 (u_int)rman_get_end(res)); 240#endif 241 } 242 243 if (desc->init != NULL) { 244 i = desc->init(sc); 245 if (i != 0) 246 return (i); 247 } 248 249 for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) { 250 rid = sc->sc_desc->ports[i].bar; 251 bidx = puc_port_bar_index(sc, rid); 252 if (sc->sc_bar_mappings[bidx].res == NULL) 253 continue; 254 255 switch (sc->sc_desc->ports[i].type) { 256 case PUC_PORT_TYPE_COM: 257 typestr = "sio"; 258 break; 259 case PUC_PORT_TYPE_LPT: 260 typestr = "ppc"; 261 break; 262 default: 263 continue; 264 } 265 pdev = malloc(sizeof(struct puc_device), M_DEVBUF, 266 M_NOWAIT | M_ZERO); 267 if (!pdev) 268 continue; 269 resource_list_init(&pdev->resources); 270 271 /* First fake up an IRQ resource. */ 272 resource_list_add(&pdev->resources, SYS_RES_IRQ, 0, 273 rman_get_start(sc->irqres), rman_get_end(sc->irqres), 274 rman_get_end(sc->irqres) - rman_get_start(sc->irqres) + 1); 275 rle = resource_list_find(&pdev->resources, SYS_RES_IRQ, 0); 276 rle->res = sc->irqres; 277 278 /* Now fake an IOPORT or MEMORY resource */ 279 res = sc->sc_bar_mappings[bidx].res; 280 type = sc->sc_bar_mappings[bidx].type; 281 resource_list_add(&pdev->resources, type, 0, 282 rman_get_start(res) + sc->sc_desc->ports[i].offset, 283 rman_get_start(res) + sc->sc_desc->ports[i].offset + 8 - 1, 284 8); 285 rle = resource_list_find(&pdev->resources, type, 0); 286 287 if (sc->barmuxed == 0) { 288 rle->res = sc->sc_bar_mappings[bidx].res; 289 } else { 290 rle->res = malloc(sizeof(struct resource), M_DEVBUF, 291 M_WAITOK | M_ZERO); 292 if (rle->res == NULL) { 293 free(pdev, M_DEVBUF); 294 return (ENOMEM); 295 } 296 297 rle->res->r_start = rman_get_start(res) + 298 sc->sc_desc->ports[i].offset; 299 rle->res->r_end = rle->res->r_start + 8 - 1; 300 rle->res->r_bustag = rman_get_bustag(res); 301 bus_space_subregion(rle->res->r_bustag, 302 rman_get_bushandle(res), 303 sc->sc_desc->ports[i].offset, 8, 304 &rle->res->r_bushandle); 305 } 306 307 pdev->serialfreq = sc->sc_desc->ports[i].serialfreq; 308 309 childunit = puc_find_free_unit(typestr); 310 sc->sc_ports[i].dev = device_add_child(dev, typestr, childunit); 311 if (sc->sc_ports[i].dev == NULL) { 312 if (sc->barmuxed) { 313 bus_space_unmap(rman_get_bustag(rle->res), 314 rman_get_bushandle(rle->res), 8); 315 free(rle->res, M_DEVBUF); 316 free(pdev, M_DEVBUF); 317 } 318 continue; 319 } 320 device_set_ivars(sc->sc_ports[i].dev, pdev); 321 device_set_desc(sc->sc_ports[i].dev, sc->sc_desc->name); 322 if (!bootverbose) 323 device_quiet(sc->sc_ports[i].dev); 324#ifdef PUC_DEBUG 325 printf("puc: type %d, bar %x, offset %x\n", 326 sc->sc_desc->ports[i].type, 327 sc->sc_desc->ports[i].bar, 328 sc->sc_desc->ports[i].offset); 329 puc_print_resource_list(&pdev->resources); 330#endif 331 device_set_flags(sc->sc_ports[i].dev, 332 sc->sc_desc->ports[i].flags); 333 if (device_probe_and_attach(sc->sc_ports[i].dev) != 0) { 334 if (sc->barmuxed) { 335 bus_space_unmap(rman_get_bustag(rle->res), 336 rman_get_bushandle(rle->res), 337 8); 338 free(rle->res, M_DEVBUF); 339 free(pdev, M_DEVBUF); 340 } 341 } 342 } 343 344#ifdef PUC_DEBUG 345 bootverbose = 0; 346#endif 347 return (0); 348} 349 350static u_int32_t 351puc_ilr_read(struct puc_softc *sc) 352{ 353 u_int32_t mask; 354 int i; 355 356 mask = 0; 357 switch (sc->sc_desc->ilr_type) { 358 case PUC_ILR_TYPE_DIGI: 359 for (i = 1; i >= 0 && sc->sc_desc->ilr_offset[i] != 0; i--) { 360 mask = (mask << 8) | (bus_space_read_1(sc->ilr_st, 361 sc->ilr_sh, sc->sc_desc->ilr_offset[i]) & 0xff); 362 } 363 break; 364 365 default: 366 mask = 0xffffffff; 367 break; 368 } 369 return (mask); 370} 371 372/* 373 * This is an interrupt handler. For boards that can't tell us which 374 * device generated the interrupt it just calls all the registered 375 * handlers sequencially, but for boards that can tell us which 376 * device(s) generated the interrupt it calls only handlers for devices 377 * that actually generated the interrupt. 378 */ 379static void 380puc_intr(void *arg) 381{ 382 int i; 383 u_int32_t ilr_mask; 384 struct puc_softc *sc; 385 386 sc = (struct puc_softc *)arg; 387 ilr_mask = sc->ilr_enabled ? puc_ilr_read(sc) : 0xffffffff; 388 for (i = 0; i < PUC_MAX_PORTS; i++) 389 if (sc->sc_ports[i].ihand != NULL && 390 ((ilr_mask >> i) & 0x00000001)) 391 (sc->sc_ports[i].ihand)(sc->sc_ports[i].ihandarg); 392} 393 394const struct puc_device_description * 395puc_find_description(uint32_t vend, uint32_t prod, uint32_t svend, 396 uint32_t sprod) 397{ 398 int i; 399 400#define checkreg(val, index) \ 401 (((val) & puc_devices[i].rmask[(index)]) == puc_devices[i].rval[(index)]) 402 403 for (i = 0; puc_devices[i].name != NULL; i++) { 404 if (checkreg(vend, PUC_REG_VEND) && 405 checkreg(prod, PUC_REG_PROD) && 406 checkreg(svend, PUC_REG_SVEND) && 407 checkreg(sprod, PUC_REG_SPROD)) 408 return (&puc_devices[i]); 409 } 410 411#undef checkreg 412 413 return (NULL); 414} 415 416static int 417puc_find_free_unit(char *name) 418{ 419 devclass_t dc; 420 int start; 421 int unit; 422 423 unit = 0; 424 start = 0; 425 while (resource_int_value(name, unit, "port", &start) == 0 && 426 start > 0) 427 unit++; 428 dc = devclass_find(name); 429 if (dc == NULL) 430 return (-1); 431 while (devclass_get_device(dc, unit)) 432 unit++; 433#ifdef PUC_DEBUG 434 printf("puc: Using %s%d\n", name, unit); 435#endif 436 return (unit); 437} 438 439#ifdef PUC_DEBUG 440static void 441puc_print_resource_list(struct resource_list *rl) 442{ 443#if 0 444 struct resource_list_entry *rle; 445 446 printf("print_resource_list: rl %p\n", rl); 447 SLIST_FOREACH(rle, rl, link) 448 printf(" type %x, rid %x start %x end %x count %x\n", 449 rle->type, rle->rid, rle->start, rle->end, rle->count); 450 printf("print_resource_list: end.\n"); 451#endif 452} 453#endif 454 455struct resource * 456puc_alloc_resource(device_t dev, device_t child, int type, int *rid, 457 u_long start, u_long end, u_long count, u_int flags) 458{ 459 struct puc_device *pdev; 460 struct resource *retval; 461 struct resource_list *rl; 462 struct resource_list_entry *rle; 463 device_t my_child; 464 465 /* 466 * in the case of a child of child we need to find our immediate child 467 */ 468 for (my_child = child; device_get_parent(my_child) != dev; 469 my_child = device_get_parent(my_child)); 470 471 pdev = device_get_ivars(my_child); 472 rl = &pdev->resources; 473 474#ifdef PUC_DEBUG 475 printf("puc_alloc_resource: pdev %p, looking for t %x, r %x\n", 476 pdev, type, *rid); 477 puc_print_resource_list(rl); 478#endif 479 retval = NULL; 480 rle = resource_list_find(rl, type, *rid); 481 if (rle) { 482 start = rle->start; 483 end = rle->end; 484 count = rle->count; 485#ifdef PUC_DEBUG 486 printf("found rle, %lx, %lx, %lx\n", start, end, count); 487#endif 488 retval = rle->res; 489 } 490#ifdef PUC_DEBUG 491 else 492 printf("oops rle is gone\n"); 493#endif 494 495 return (retval); 496} 497 498int 499puc_release_resource(device_t dev, device_t child, int type, int rid, 500 struct resource *res) 501{ 502 return (0); 503} 504 505int 506puc_get_resource(device_t dev, device_t child, int type, int rid, 507 u_long *startp, u_long *countp) 508{ 509 struct puc_device *pdev; 510 struct resource_list *rl; 511 struct resource_list_entry *rle; 512 513 pdev = device_get_ivars(child); 514 rl = &pdev->resources; 515 516#ifdef PUC_DEBUG 517 printf("puc_get_resource: pdev %p, looking for t %x, r %x\n", pdev, 518 type, rid); 519 puc_print_resource_list(rl); 520#endif 521 rle = resource_list_find(rl, type, rid); 522 if (rle) { 523#ifdef PUC_DEBUG 524 printf("found rle %p,", rle); 525#endif 526 if (startp != NULL) 527 *startp = rle->start; 528 if (countp != NULL) 529 *countp = rle->count; 530#ifdef PUC_DEBUG 531 printf(" %lx, %lx\n", rle->start, rle->count); 532#endif 533 return (0); 534 } else 535 printf("oops rle is gone\n"); 536 return (ENXIO); 537} 538 539int 540puc_setup_intr(device_t dev, device_t child, struct resource *r, int flags, 541 void (*ihand)(void *), void *arg, void **cookiep) 542{ 543 int i; 544 struct puc_softc *sc; 545 546 sc = (struct puc_softc *)device_get_softc(dev); 547 if ((flags & INTR_FAST) != sc->fastintr) 548 return (ENXIO); 549 for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) { 550 if (sc->sc_ports[i].dev == child) { 551 if (sc->sc_ports[i].ihand != 0) 552 return (ENXIO); 553 sc->sc_ports[i].ihand = ihand; 554 sc->sc_ports[i].ihandarg = arg; 555 *cookiep = arg; 556 return (0); 557 } 558 } 559 return (ENXIO); 560} 561 562int 563puc_teardown_intr(device_t dev, device_t child, struct resource *r, 564 void *cookie) 565{ 566 int i; 567 struct puc_softc *sc; 568 569 sc = (struct puc_softc *)device_get_softc(dev); 570 for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) { 571 if (sc->sc_ports[i].dev == child) { 572 sc->sc_ports[i].ihand = NULL; 573 sc->sc_ports[i].ihandarg = NULL; 574 return (0); 575 } 576 } 577 return (ENXIO); 578} 579 580int 581puc_read_ivar(device_t dev, device_t child, int index, uintptr_t *result) 582{ 583 struct puc_device *pdev; 584 585 pdev = device_get_ivars(child); 586 if (pdev == NULL) 587 return (ENOENT); 588 589 switch(index) { 590 case PUC_IVAR_FREQ: 591 *result = pdev->serialfreq; 592 break; 593 default: 594 return (ENOENT); 595 } 596 return (0); 597} 598