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