puc.c revision 90925
119304Speter/* $NetBSD: puc.c,v 1.7 2000/07/29 17:43:38 jlam Exp $ */ 219304Speter 319304Speter/*- 419304Speter * Copyright (c) 2002 JF Hay. All rights reserved. 519304Speter * Copyright (c) 2000 M. Warner Losh. All rights reserved. 619304Speter * 719304Speter * Redistribution and use in source and binary forms, with or without 819304Speter * modification, are permitted provided that the following conditions 919304Speter * are met: 1019304Speter * 1. Redistributions of source code must retain the above copyright 1119304Speter * notice unmodified, this list of conditions, and the following 1219304Speter * disclaimer. 13254225Speter * 2. Redistributions in binary form must reproduce the above copyright 1419304Speter * notice, this list of conditions and the following disclaimer in the 1519304Speter * documentation and/or other materials provided with the distribution. 1619304Speter * 1719304Speter * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 1819304Speter * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 1919304Speter * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 2019304Speter * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 2119304Speter * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 2219304Speter * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2319304Speter * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2419304Speter * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2519304Speter * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 2619304Speter * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2719304Speter */ 2819304Speter 2919304Speter/* 3019304Speter * Copyright (c) 1996, 1998, 1999 3119304Speter * Christopher G. Demetriou. All rights reserved. 3219304Speter * 3319304Speter * Redistribution and use in source and binary forms, with or without 3419304Speter * modification, are permitted provided that the following conditions 3519304Speter * are met: 3619304Speter * 1. Redistributions of source code must retain the above copyright 3719304Speter * notice, this list of conditions and the following disclaimer. 3819304Speter * 2. Redistributions in binary form must reproduce the above copyright 3919304Speter * notice, this list of conditions and the following disclaimer in the 4019304Speter * documentation and/or other materials provided with the distribution. 4119304Speter * 3. All advertising materials mentioning features or use of this software 4219304Speter * must display the following acknowledgement: 4319304Speter * This product includes software developed by Christopher G. Demetriou 4419304Speter * for the NetBSD Project. 4519304Speter * 4. The name of the author may not be used to endorse or promote products 46254225Speter * derived from this software without specific prior written permission 4719304Speter * 4819304Speter * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 4919304Speter * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 5019304Speter * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 5119304Speter * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 5219304Speter * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 5319304Speter * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 5419304Speter * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 5519304Speter * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 5619304Speter * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 5719304Speter * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 5819304Speter */ 5919304Speter 6019304Speter#include <sys/cdefs.h> 6119304Speter__FBSDID("$FreeBSD: head/sys/dev/puc/puc.c 90925 2002-02-19 14:58:11Z nyan $"); 6219304Speter 6319304Speter/* 6419304Speter * PCI "universal" communication card device driver, glues com, lpt, 6519304Speter * and similar ports to PCI via bridge chip often much larger than 6619304Speter * the devices being glued. 6719304Speter * 6819304Speter * Author: Christopher G. Demetriou, May 14, 1998 (derived from NetBSD 6919304Speter * sys/dev/pci/pciide.c, revision 1.6). 7019304Speter * 7119304Speter * These devices could be (and some times are) described as 72254225Speter * communications/{serial,parallel}, etc. devices with known 7319304Speter * programming interfaces, but those programming interfaces (in 74254225Speter * particular the BAR assignments for devices, etc.) in fact are not 7519304Speter * particularly well defined. 7619304Speter * 7719304Speter * After I/we have seen more of these devices, it may be possible 7819304Speter * to generalize some of these bits. In particular, devices which 7919304Speter * describe themselves as communications/serial/16[45]50, and 8019304Speter * communications/parallel/??? might be attached via direct 8119304Speter * 'com' and 'lpt' attachments to pci. 8219304Speter */ 8319304Speter 8419304Speter#include <sys/param.h> 8519304Speter#include <sys/systm.h> 8619304Speter#include <sys/kernel.h> 8719304Speter#include <sys/bus.h> 8819304Speter#include <sys/conf.h> 8919304Speter#include <sys/malloc.h> 9019304Speter 9119304Speter#include <machine/bus.h> 9219304Speter#include <machine/resource.h> 9319304Speter#include <sys/rman.h> 9419304Speter 9519304Speter#if __FreeBSD_version < 500000 9619304Speter#include <pci/pcireg.h> 9719304Speter#include <pci/pcivar.h> 9819304Speter#else 9919304Speter#include <dev/pci/pcireg.h> 10019304Speter#include <dev/pci/pcivar.h> 10119304Speter#endif 10219304Speter#include <dev/puc/pucvar.h> 10319304Speter 10419304Speter#include <opt_puc.h> 10519304Speter 10619304Speterstruct puc_softc { 10719304Speter const struct puc_device_description *sc_desc; 10819304Speter 10919304Speter /* card-global dynamic data */ 11019304Speter int barmuxed; 11119304Speter int irqrid; 11219304Speter struct resource *irqres; 11319304Speter void *intr_cookie; 11419304Speter 11519304Speter struct { 11619304Speter struct resource *res; 11719304Speter } sc_bar_mappings[PUC_MAX_BAR]; 11819304Speter 11919304Speter /* per-port dynamic data */ 12019304Speter struct { 12119304Speter struct device *dev; 122254225Speter /* filled in by bus_setup_intr() */ 12319304Speter void (*ihand) __P((void *)); 124254225Speter void *ihandarg; 12519304Speter } sc_ports[PUC_MAX_PORTS]; 12619304Speter}; 12719304Speter 12819304Speterstruct puc_device { 129254225Speter struct resource_list resources; 13019304Speter u_int serialfreq; 13119304Speter}; 13219304Speter 13319304Speterstatic int puc_pci_probe(device_t dev); 13419304Speterstatic int puc_pci_attach(device_t dev); 13519304Speterstatic void puc_intr(void *arg); 13619304Speter 13719304Speterstatic struct resource *puc_alloc_resource(device_t, device_t, int, int *, 13819304Speter u_long, u_long, u_long, u_int); 13919304Speterstatic int puc_release_resource(device_t, device_t, int, int, 14019304Speter struct resource *); 14119304Speterstatic int puc_get_resource(device_t, device_t, int, int, u_long *, u_long *); 142254225Speterstatic int puc_setup_intr(device_t, device_t, struct resource *, int, 14319304Speter void (*)(void *), void *, void **); 14419304Speterstatic int puc_teardown_intr(device_t, device_t, struct resource *, 14519304Speter void *); 14619304Speterstatic int puc_read_ivar(device_t, device_t, int, uintptr_t *); 14719304Speter 148254225Speterstatic const struct puc_device_description *puc_find_description(uint32_t, 14919304Speter uint32_t, uint32_t, uint32_t); 15019304Speterstatic void puc_config_superio(device_t); 15119304Speterstatic void puc_config_win877(struct resource *); 15219304Speterstatic int puc_find_free_unit(char *); 15319304Speter#ifdef PUC_DEBUG 15419304Speterstatic void puc_print_win877(bus_space_tag_t, bus_space_handle_t, u_int, 15519304Speter u_int); 15619304Speterstatic void puc_print_resource_list(struct resource_list *); 15719304Speter#endif 15819304Speter 15919304Speterstatic int 16019304Speterpuc_pci_probe(device_t dev) 16119304Speter{ 162254225Speter uint32_t v1, v2, d1, d2; 16319304Speter const struct puc_device_description *desc; 16419304Speter 16519304Speter if (pci_read_config(dev, PCIR_HEADERTYPE, 1) != 0) 16619304Speter return (ENXIO); 16719304Speter 16819304Speter v1 = pci_read_config(dev, PCIR_VENDOR, 2); 16919304Speter d1 = pci_read_config(dev, PCIR_DEVICE, 2); 17019304Speter v2 = pci_read_config(dev, PCIR_SUBVEND_0, 2); 17119304Speter d2 = pci_read_config(dev, PCIR_SUBDEV_0, 2); 17219304Speter 17319304Speter desc = puc_find_description(v1, d1, v2, d2); 17419304Speter if (desc == NULL) 17519304Speter return (ENXIO); 17619304Speter device_set_desc(dev, desc->name); 17719304Speter return (0); 17819304Speter} 17919304Speter 18019304Speterstatic int 18119304Speterpuc_pci_attach(device_t dev) 18219304Speter{ 18319304Speter char *typestr; 18419304Speter int bidx, childunit, i, irq_setup, rid; 18519304Speter uint32_t v1, v2, d1, d2; 18619304Speter struct puc_softc *sc; 18719304Speter struct puc_device *pdev; 18819304Speter struct resource *res; 18919304Speter struct resource_list_entry *rle; 19019304Speter 19119304Speter sc = (struct puc_softc *)device_get_softc(dev); 19219304Speter bzero(sc, sizeof(*sc)); 19319304Speter v1 = pci_read_config(dev, PCIR_VENDOR, 2); 19419304Speter d1 = pci_read_config(dev, PCIR_DEVICE, 2); 19519304Speter v2 = pci_read_config(dev, PCIR_SUBVEND_0, 2); 19619304Speter d2 = pci_read_config(dev, PCIR_SUBDEV_0, 2); 19719304Speter sc->sc_desc = puc_find_description(v1, d1, v2, d2); 19819304Speter if (sc->sc_desc == NULL) 19919304Speter return (ENXIO); 20019304Speter 20119304Speter#ifdef PUC_DEBUG 20219304Speter bootverbose = 1; 20319304Speter 20419304Speter printf("puc: name: %s\n", sc->sc_desc->name); 20519304Speter#endif 20619304Speter rid = 0; 20719304Speter res = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1, 20819304Speter RF_ACTIVE | RF_SHAREABLE); 20919304Speter if (!res) 21019304Speter return (ENXIO); 211254225Speter 21219304Speter sc->irqres = res; 21319304Speter sc->irqrid = rid; 21419304Speter#ifdef PUC_FASTINTR 21519304Speter irq_setup = BUS_SETUP_INTR(device_get_parent(dev), dev, res, 21619304Speter INTR_TYPE_TTY | INTR_FAST, puc_intr, sc, &sc->intr_cookie); 217254225Speter#else 21819304Speter irq_setup = ENXIO; 21919304Speter#endif 22019304Speter if (irq_setup != 0) 22119304Speter irq_setup = BUS_SETUP_INTR(device_get_parent(dev), dev, res, 22219304Speter INTR_TYPE_TTY, puc_intr, sc, &sc->intr_cookie); 223 if (irq_setup != 0) 224 return (ENXIO); 225 226 rid = 0; 227 for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) { 228 if (rid == sc->sc_desc->ports[i].bar) 229 sc->barmuxed = 1; 230 rid = sc->sc_desc->ports[i].bar; 231 bidx = PUC_PORT_BAR_INDEX(rid); 232 233 if (sc->sc_bar_mappings[bidx].res != NULL) 234 continue; 235 res = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 236 0ul, ~0ul, 1, RF_ACTIVE); 237 if (res == NULL) { 238 printf("could not get resource\n"); 239 continue; 240 } 241 sc->sc_bar_mappings[bidx].res = res; 242#ifdef PUC_DEBUG 243 printf("port bst %x, start %x, end %x\n", 244 (u_int)rman_get_bustag(res), (u_int)rman_get_start(res), 245 (u_int)rman_get_end(res)); 246#endif 247 } 248 249 puc_config_superio(dev); 250 251 for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) { 252 rid = sc->sc_desc->ports[i].bar; 253 bidx = PUC_PORT_BAR_INDEX(rid); 254 if (sc->sc_bar_mappings[bidx].res == NULL) 255 continue; 256 257 switch (sc->sc_desc->ports[i].type) { 258 case PUC_PORT_TYPE_COM: 259 typestr = "sio"; 260 break; 261 default: 262 continue; 263 } 264 pdev = malloc(sizeof(struct puc_device), M_DEVBUF, 265 M_NOWAIT | M_ZERO); 266 if (!pdev) 267 continue; 268 resource_list_init(&pdev->resources); 269 270 /* First fake up an IRQ resource. */ 271 resource_list_add(&pdev->resources, SYS_RES_IRQ, 0, 272 rman_get_start(sc->irqres), rman_get_end(sc->irqres), 273 rman_get_end(sc->irqres) - rman_get_start(sc->irqres) + 1); 274 rle = resource_list_find(&pdev->resources, SYS_RES_IRQ, 0); 275 rle->res = sc->irqres; 276 277 /* Now fake an IOPORT resource */ 278 res = sc->sc_bar_mappings[bidx].res; 279 resource_list_add(&pdev->resources, SYS_RES_IOPORT, 0, 280 rman_get_start(res) + sc->sc_desc->ports[i].offset, 281 rman_get_end(res) + sc->sc_desc->ports[i].offset + 8 - 1, 282 8); 283 rle = resource_list_find(&pdev->resources, SYS_RES_IOPORT, 0); 284 285 if (sc->barmuxed == 0) { 286 rle->res = sc->sc_bar_mappings[bidx].res; 287 } else { 288 rle->res = malloc(sizeof(struct resource), M_DEVBUF, 289 M_WAITOK | M_ZERO); 290 if (rle->res == NULL) { 291 free(pdev, M_DEVBUF); 292 return (ENOMEM); 293 } 294 295 rle->res->r_start = rman_get_start(res) + 296 sc->sc_desc->ports[i].offset; 297 rle->res->r_end = rle->res->r_start + 8 - 1; 298 rle->res->r_bustag = rman_get_bustag(res); 299 bus_space_subregion(rle->res->r_bustag, 300 rman_get_bushandle(res), 301 sc->sc_desc->ports[i].offset, 8, 302 &rle->res->r_bushandle); 303 } 304 305 pdev->serialfreq = sc->sc_desc->ports[i].serialfreq; 306 307 childunit = puc_find_free_unit(typestr); 308 sc->sc_ports[i].dev = device_add_child(dev, typestr, childunit); 309 if (sc->sc_ports[i].dev == NULL) { 310 if (sc->barmuxed) { 311 bus_space_unmap(rman_get_bustag(rle->res), 312 rman_get_bushandle(rle->res), 313 8); 314 free(rle->res, M_DEVBUF); 315 free(pdev, M_DEVBUF); 316 } 317 continue; 318 } 319 device_set_ivars(sc->sc_ports[i].dev, pdev); 320 device_set_desc(sc->sc_ports[i].dev, sc->sc_desc->name); 321 if (!bootverbose) 322 device_quiet(sc->sc_ports[i].dev); 323#ifdef PUC_DEBUG 324 printf("puc: type %d, bar %x, offset %x\n", 325 sc->sc_desc->ports[i].type, 326 sc->sc_desc->ports[i].bar, 327 sc->sc_desc->ports[i].offset); 328 print_resource_list(&pdev->resources); 329#endif 330 if (device_probe_and_attach(sc->sc_ports[i].dev) != 0) { 331 if (sc->barmuxed) { 332 bus_space_unmap(rman_get_bustag(rle->res), 333 rman_get_bushandle(rle->res), 334 8); 335 free(rle->res, M_DEVBUF); 336 free(pdev, M_DEVBUF); 337 } 338 } 339 } 340 341#ifdef PUC_DEBUG 342 bootverbose = 0; 343#endif 344 return (0); 345} 346 347/* 348 * This is just an brute force interrupt handler. It just calls all the 349 * registered handlers sequencially. 350 * 351 * Later on we should maybe have a different handler for boards that can 352 * tell us which device generated the interrupt. 353 */ 354static void 355puc_intr(void *arg) 356{ 357 int i; 358 struct puc_softc *sc; 359 360 sc = (struct puc_softc *)arg; 361 for (i = 0; i < PUC_MAX_PORTS; i++) 362 if (sc->sc_ports[i].ihand != NULL) 363 (sc->sc_ports[i].ihand)(sc->sc_ports[i].ihandarg); 364} 365 366static const struct puc_device_description * 367puc_find_description(uint32_t vend, uint32_t prod, uint32_t svend, 368 uint32_t sprod) 369{ 370 int i; 371 372#define checkreg(val, index) \ 373 (((val) & puc_devices[i].rmask[(index)]) == puc_devices[i].rval[(index)]) 374 375 for (i = 0; puc_devices[i].name != NULL; i++) { 376 if (checkreg(vend, PUC_REG_VEND) && 377 checkreg(prod, PUC_REG_PROD) && 378 checkreg(svend, PUC_REG_SVEND) && 379 checkreg(sprod, PUC_REG_SPROD)) 380 return (&puc_devices[i]); 381 } 382 383#undef checkreg 384 385 return (NULL); 386} 387 388/* 389 * It might be possible to make these more generic if we can detect patterns. 390 * For instance maybe if the size of a bar is 0x400 (the old isa space) it 391 * might contain one or more superio chips. 392 */ 393static void 394puc_config_superio(device_t dev) 395{ 396 struct puc_softc *sc = (struct puc_softc *)device_get_softc(dev); 397 398 if (sc->sc_desc->rval[PUC_REG_VEND] == 0x1592 && 399 sc->sc_desc->rval[PUC_REG_PROD] == 0x0781) 400 puc_config_win877(sc->sc_bar_mappings[0].res); 401} 402 403#define rdspio(indx) (bus_space_write_1(bst, bsh, efir, indx), \ 404 bus_space_read_1(bst, bsh, efdr)) 405#define wrspio(indx,data) (bus_space_write_1(bst, bsh, efir, indx), \ 406 bus_space_write_1(bst, bsh, efdr, data)) 407 408#ifdef PUC_DEBUG 409static void 410puc_print_win877(bus_space_tag_t bst, bus_space_handle_t bsh, u_int efir, 411 u_int efdr) 412{ 413 u_char cr00, cr01, cr04, cr09, cr0d, cr14, cr15, cr16, cr17; 414 u_char cr18, cr19, cr24, cr25, cr28, cr2c, cr31, cr32; 415 416 cr00 = rdspio(0x00); 417 cr01 = rdspio(0x01); 418 cr04 = rdspio(0x04); 419 cr09 = rdspio(0x09); 420 cr0d = rdspio(0x0d); 421 cr14 = rdspio(0x14); 422 cr15 = rdspio(0x15); 423 cr16 = rdspio(0x16); 424 cr17 = rdspio(0x17); 425 cr18 = rdspio(0x18); 426 cr19 = rdspio(0x19); 427 cr24 = rdspio(0x24); 428 cr25 = rdspio(0x25); 429 cr28 = rdspio(0x28); 430 cr2c = rdspio(0x2c); 431 cr31 = rdspio(0x31); 432 cr32 = rdspio(0x32); 433 printf("877T: cr00 %x, cr01 %x, cr04 %x, cr09 %x, cr0d %x, cr14 %x, " 434 "cr15 %x, cr16 %x, cr17 %x, cr18 %x, cr19 %x, cr24 %x, cr25 %x, " 435 "cr28 %x, cr2c %x, cr31 %x, cr32 %x\n", cr00, cr01, cr04, cr09, 436 cr0d, cr14, cr15, cr16, cr17, 437 cr18, cr19, cr24, cr25, cr28, cr2c, cr31, cr32); 438} 439#endif 440 441static void 442puc_config_win877(struct resource *res) 443{ 444 u_char val; 445 u_int efir, efdr; 446 bus_space_tag_t bst; 447 bus_space_handle_t bsh; 448 449 bst = rman_get_bustag(res); 450 bsh = rman_get_bushandle(res); 451 452 /* configure the first W83877TF */ 453 bus_space_write_1(bst, bsh, 0x250, 0x89); 454 efir = 0x251; 455 efdr = 0x252; 456 val = rdspio(0x09) & 0x0f; 457 if (val != 0x0c) { 458 printf("conf_win877: Oops not a W83877TF\n"); 459 return; 460 } 461 462#ifdef PUC_DEBUG 463 printf("before: "); 464 puc_print_win877(bst, bsh, efir, efdr); 465#endif 466 467 val = rdspio(0x16); 468 val |= 0x04; 469 wrspio(0x16, val); 470 val &= ~0x04; 471 wrspio(0x16, val); 472 473 wrspio(0x24, 0x2e8 >> 2); 474 wrspio(0x25, 0x2f8 >> 2); 475 wrspio(0x17, 0x03); 476 wrspio(0x28, 0x43); 477 478#ifdef PUC_DEBUG 479 printf("after: "); 480 puc_print_win877(bst, bsh, efir, efdr); 481#endif 482 483 bus_space_write_1(bst, bsh, 0x250, 0xaa); 484 485 /* configure the second W83877TF */ 486 bus_space_write_1(bst, bsh, 0x3f0, 0x87); 487 bus_space_write_1(bst, bsh, 0x3f0, 0x87); 488 efir = 0x3f0; 489 efdr = 0x3f1; 490 val = rdspio(0x09) & 0x0f; 491 if (val != 0x0c) { 492 printf("conf_win877: Oops not a W83877TF\n"); 493 return; 494 } 495 496#ifdef PUC_DEBUG 497 printf("before: "); 498 puc_print_win877(bst, bsh, efir, efdr); 499#endif 500 501 val = rdspio(0x16); 502 val |= 0x04; 503 wrspio(0x16, val); 504 val &= ~0x04; 505 wrspio(0x16, val); 506 507 wrspio(0x24, 0x3e8 >> 2); 508 wrspio(0x25, 0x3f8 >> 2); 509 wrspio(0x17, 0x03); 510 wrspio(0x28, 0x43); 511 512#ifdef PUC_DEBUG 513 printf("after: "); 514 puc_print_win877(bst, bsh, efir, efdr); 515#endif 516 517 bus_space_write_1(bst, bsh, 0x3f0, 0xaa); 518} 519 520#undef rdspio 521#undef wrspio 522 523static int puc_find_free_unit(char *name) 524{ 525 devclass_t dc; 526 int start; 527 int unit; 528 529 unit = 0; 530 start = 0; 531 while (resource_int_value(name, unit, "port", &start) == 0 && 532 start > 0) 533 unit++; 534 dc = devclass_find(name); 535 if (dc == NULL) 536 return (-1); 537 while (devclass_get_device(dc, unit)) 538 unit++; 539#ifdef PUC_DEBUG 540 printf("puc: Using %s%d\n", name, unit); 541#endif 542 return (unit); 543} 544 545#ifdef PUC_DEBUG 546static void 547puc_print_resource_list(struct resource_list *rl) 548{ 549 struct resource_list_entry *rle; 550 551 printf("print_resource_list: rl %p\n", rl); 552 SLIST_FOREACH(rle, rl, link) 553 printf("type %x, rid %x\n", rle->type, rle->rid); 554 printf("print_resource_list: end.\n"); 555} 556#endif 557 558static struct resource * 559puc_alloc_resource(device_t dev, device_t child, int type, int *rid, 560 u_long start, u_long end, u_long count, u_int flags) 561{ 562 struct puc_device *pdev; 563 struct resource *retval; 564 struct resource_list *rl; 565 struct resource_list_entry *rle; 566 567 pdev = device_get_ivars(child); 568 rl = &pdev->resources; 569 570#ifdef PUC_DEBUG 571 printf("puc_alloc_resource: pdev %p, looking for t %x, r %x\n", 572 pdev, type, *rid); 573 puc_print_resource_list(rl); 574#endif 575 retval = NULL; 576 rle = resource_list_find(rl, type, *rid); 577 if (rle) { 578 start = rle->start; 579 end = rle->end; 580 count = rle->count; 581#ifdef PUC_DEBUG 582 printf("found rle, %lx, %lx, %lx\n", start, end, count); 583#endif 584 retval = rle->res; 585 } else 586 printf("oops rle is gone\n"); 587 588 return (retval); 589} 590 591static int 592puc_release_resource(device_t dev, device_t child, int type, int rid, 593 struct resource *res) 594{ 595 return (0); 596} 597 598static int 599puc_get_resource(device_t dev, device_t child, int type, int rid, 600 u_long *startp, u_long *countp) 601{ 602 struct puc_device *pdev; 603 struct resource_list *rl; 604 struct resource_list_entry *rle; 605 606 pdev = device_get_ivars(child); 607 rl = &pdev->resources; 608 609#ifdef PUC_DEBUG 610 printf("puc_get_resource: pdev %p, looking for t %x, r %x\n", pdev, 611 type, rid); 612 puc_print_resource_list(rl); 613#endif 614 rle = resource_list_find(rl, type, rid); 615 if (rle) { 616#ifdef PUC_DEBUG 617 printf("found rle %p,", rle); 618#endif 619 if (startp != NULL) 620 *startp = rle->start; 621 if (countp != NULL) 622 *countp = rle->count; 623#ifdef PUC_DEBUG 624 printf(" %lx, %lx\n", rle->start, rle->count); 625#endif 626 return (0); 627 } else 628 printf("oops rle is gone\n"); 629 return (ENXIO); 630} 631 632static int 633puc_setup_intr(device_t dev, device_t child, struct resource *r, int flags, 634 void (*ihand)(void *), void *arg, void **cookiep) 635{ 636 int i; 637 struct puc_softc *sc; 638 639 sc = (struct puc_softc *)device_get_softc(dev); 640 for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) { 641 if (sc->sc_ports[i].dev == child) { 642 if (sc->sc_ports[i].ihand != 0) 643 return (ENXIO); 644 sc->sc_ports[i].ihand = ihand; 645 sc->sc_ports[i].ihandarg = arg; 646 *cookiep = arg; 647 return (0); 648 } 649 } 650 return (ENXIO); 651} 652 653static int 654puc_teardown_intr(device_t dev, device_t child, struct resource *r, 655 void *cookie) 656{ 657 int i; 658 struct puc_softc *sc; 659 660 sc = (struct puc_softc *)device_get_softc(dev); 661 for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) { 662 if (sc->sc_ports[i].dev == child) { 663 sc->sc_ports[i].ihand = NULL; 664 sc->sc_ports[i].ihandarg = NULL; 665 return (0); 666 } 667 } 668 return (ENXIO); 669} 670 671static int 672puc_read_ivar(device_t dev, device_t child, int index, uintptr_t *result) 673{ 674 struct puc_device *pdev; 675 676 pdev = device_get_ivars(child); 677 if (pdev == NULL) 678 return (ENOENT); 679 680 switch(index) { 681 case PUC_IVAR_FREQ: 682 *result = pdev->serialfreq; 683 break; 684 default: 685 return (ENOENT); 686 } 687 return (0); 688} 689 690static device_method_t puc_pci_methods[] = { 691 /* Device interface */ 692 DEVMETHOD(device_probe, puc_pci_probe), 693 DEVMETHOD(device_attach, puc_pci_attach), 694 695 DEVMETHOD(bus_alloc_resource, puc_alloc_resource), 696 DEVMETHOD(bus_release_resource, puc_release_resource), 697 DEVMETHOD(bus_get_resource, puc_get_resource), 698 DEVMETHOD(bus_read_ivar, puc_read_ivar), 699 DEVMETHOD(bus_setup_intr, puc_setup_intr), 700 DEVMETHOD(bus_teardown_intr, puc_teardown_intr), 701 DEVMETHOD(bus_print_child, bus_generic_print_child), 702 DEVMETHOD(bus_driver_added, bus_generic_driver_added), 703 { 0, 0 } 704}; 705 706static driver_t puc_pci_driver = { 707 "puc", 708 puc_pci_methods, 709 sizeof(struct puc_softc), 710}; 711 712static devclass_t puc_devclass; 713 714DRIVER_MODULE(puc, pci, puc_pci_driver, puc_devclass, 0, 0); 715