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 /* 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$"); 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 #define __RMAN_RESOURCE_VISIBLE /* Shouldn't be there */ 85 #include "opt_puc.h" 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 103 struct puc_device { 104 struct resource_list resources; 105 int port; 106 int regshft; 107 u_int serialfreq; 108 u_int subtype; 109 }; 110 111 static void puc_intr(void *arg); 112 113 static int puc_find_free_unit(char *); 114 #ifdef PUC_DEBUG 115 static void puc_print_resource_list(struct resource_list *); 116 #endif 117 118 devclass_t puc_devclass; 119 120 static int 121 puc_port_bar_index(struct puc_softc *sc, int bar) 122 { 123 int i; 124 125 for (i = 0; i < PUC_MAX_BAR; i += 1) { 126 if (!sc->sc_bar_mappings[i].used) 127 break; 128 if (sc->sc_bar_mappings[i].bar == bar) 129 return (i); 130 } 131 sc->sc_bar_mappings[i].bar = bar; 132 sc->sc_bar_mappings[i].used = 1; 133 return (i); 134 } 135 136 static int 137 puc_probe_ilr(struct puc_softc *sc, struct resource *res) 138 { 139 u_char t1, t2; 140 int i; 141 142 switch (sc->sc_desc.ilr_type) { 143 case PUC_ILR_TYPE_DIGI: 144 sc->ilr_st = rman_get_bustag(res); 145 sc->ilr_sh = rman_get_bushandle(res); 146 for (i = 0; i < 2 && sc->sc_desc.ilr_offset[i] != 0; i++) { 147 t1 = bus_space_read_1(sc->ilr_st, sc->ilr_sh, 148 sc->sc_desc.ilr_offset[i]); 149 t1 = ~t1; 150 bus_space_write_1(sc->ilr_st, sc->ilr_sh, 151 sc->sc_desc.ilr_offset[i], t1); 152 t2 = bus_space_read_1(sc->ilr_st, sc->ilr_sh, 153 sc->sc_desc.ilr_offset[i]); 154 if (t2 == t1) 155 return (0); 156 } 157 return (1); 158 159 default: 160 break; 161 } 162 return (0); 163 } 164 165 int 166 puc_attach(device_t dev, const struct puc_device_description *desc) 167 { 168 char *typestr; 169 int bidx, childunit, i, irq_setup, ressz, rid, type; 170 struct puc_softc *sc; 171 struct puc_device *pdev; 172 struct resource *res; 173 struct resource_list_entry *rle; 174 bus_space_handle_t bh; 175 176 if (desc == NULL) 177 return (ENXIO); 178 179 sc = (struct puc_softc *)device_get_softc(dev); 180 bzero(sc, sizeof(*sc)); 181 sc->sc_desc = *desc; 182 183 #ifdef PUC_DEBUG 184 bootverbose = 1; 185 186 printf("puc: name: %s\n", sc->sc_desc.name); 187 #endif 188 rid = 0; 189 res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, 190 RF_ACTIVE | RF_SHAREABLE); 191 if (!res) 192 return (ENXIO); 193 194 sc->irqres = res; 195 sc->irqrid = rid; 196 #ifdef PUC_FASTINTR 197 irq_setup = BUS_SETUP_INTR(device_get_parent(dev), dev, res, 198 INTR_TYPE_TTY | INTR_FAST, puc_intr, sc, &sc->intr_cookie); 199 if (irq_setup == 0) 200 sc->fastintr = INTR_FAST; 201 else 202 irq_setup = BUS_SETUP_INTR(device_get_parent(dev), dev, res, 203 INTR_TYPE_TTY, puc_intr, sc, &sc->intr_cookie); 204 #else 205 irq_setup = BUS_SETUP_INTR(device_get_parent(dev), dev, res, 206 INTR_TYPE_TTY, puc_intr, sc, &sc->intr_cookie); 207 #endif 208 if (irq_setup != 0) 209 return (ENXIO); 210 211 rid = 0; 212 for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) { 213 if (i > 0 && rid == sc->sc_desc.ports[i].bar) 214 sc->barmuxed = 1; 215 rid = sc->sc_desc.ports[i].bar; 216 bidx = puc_port_bar_index(sc, rid); 217 218 if (sc->sc_bar_mappings[bidx].res != NULL) 219 continue; 220 221 type = (sc->sc_desc.ports[i].flags & PUC_FLAGS_MEMORY) 222 ? SYS_RES_MEMORY : SYS_RES_IOPORT; 223 224 res = bus_alloc_resource_any(dev, type, &rid, 225 RF_ACTIVE); 226 if (res == NULL && 227 sc->sc_desc.ports[i].flags & PUC_FLAGS_ALTRES) { 228 type = (type == SYS_RES_IOPORT) 229 ? SYS_RES_MEMORY : SYS_RES_IOPORT; 230 res = bus_alloc_resource_any(dev, type, &rid, 231 RF_ACTIVE); 232 } 233 if (res == NULL) { 234 device_printf(dev, "could not get resource\n"); 235 continue; 236 } 237 sc->sc_bar_mappings[bidx].type = type; 238 sc->sc_bar_mappings[bidx].res = res; 239 240 if (sc->sc_desc.ilr_type != PUC_ILR_TYPE_NONE) { 241 sc->ilr_enabled = puc_probe_ilr(sc, res); 242 if (sc->ilr_enabled) 243 device_printf(dev, "ILR enabled\n"); 244 else 245 device_printf(dev, "ILR disabled\n"); 246 } 247 #ifdef PUC_DEBUG 248 printf("%s rid %d bst %lx, start %lx, end %lx\n", 249 (type == SYS_RES_MEMORY) ? "memory" : "port", rid, 250 (u_long)rman_get_bustag(res), (u_long)rman_get_start(res), 251 (u_long)rman_get_end(res)); 252 #endif 253 } 254 255 if (desc->init != NULL) { 256 i = desc->init(sc); 257 if (i != 0) 258 return (i); 259 } 260 261 for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) { 262 rid = sc->sc_desc.ports[i].bar; 263 bidx = puc_port_bar_index(sc, rid); 264 if (sc->sc_bar_mappings[bidx].res == NULL) 265 continue; 266 267 switch (sc->sc_desc.ports[i].type & ~PUC_PORT_SUBTYPE_MASK) { 268 case PUC_PORT_TYPE_COM: 269 typestr = "sio"; 270 break; 271 case PUC_PORT_TYPE_LPT: 272 typestr = "ppc"; 273 break; 274 case PUC_PORT_TYPE_UART: 275 typestr = "uart"; 276 break; 277 default: 278 continue; 279 } 280 switch (sc->sc_desc.ports[i].type & PUC_PORT_SUBTYPE_MASK) { 281 case PUC_PORT_UART_SAB82532: 282 ressz = 64; 283 break; 284 case PUC_PORT_UART_Z8530: 285 ressz = 2; 286 break; 287 default: 288 ressz = 8; 289 break; 290 } 291 pdev = malloc(sizeof(struct puc_device), M_DEVBUF, 292 M_NOWAIT | M_ZERO); 293 if (!pdev) 294 continue; 295 resource_list_init(&pdev->resources); 296 297 /* First fake up an IRQ resource. */ 298 resource_list_add(&pdev->resources, SYS_RES_IRQ, 0, 299 rman_get_start(sc->irqres), rman_get_end(sc->irqres), 300 rman_get_end(sc->irqres) - rman_get_start(sc->irqres) + 1); 301 rle = resource_list_find(&pdev->resources, SYS_RES_IRQ, 0); 302 rle->res = sc->irqres; 303 304 /* Now fake an IOPORT or MEMORY resource */ 305 res = sc->sc_bar_mappings[bidx].res; 306 type = sc->sc_bar_mappings[bidx].type; 307 resource_list_add(&pdev->resources, type, 0, 308 rman_get_start(res) + sc->sc_desc.ports[i].offset, 309 rman_get_start(res) + sc->sc_desc.ports[i].offset 310 + ressz - 1, ressz); 311 rle = resource_list_find(&pdev->resources, type, 0); 312 313 if (sc->barmuxed == 0) { 314 rle->res = sc->sc_bar_mappings[bidx].res; 315 } else { 316 rle->res = malloc(sizeof(struct resource), M_DEVBUF, 317 M_WAITOK | M_ZERO); 318 if (rle->res == NULL) { 319 free(pdev, M_DEVBUF); 320 return (ENOMEM); 321 } 322 323 rman_set_start(rle->res, rman_get_start(res) + 324 sc->sc_desc.ports[i].offset); 325 rman_set_end(rle->res, rman_get_start(rle->res) + 326 ressz - 1); 327 rman_set_bustag(rle->res, rman_get_bustag(res)); 328 bus_space_subregion(rman_get_bustag(rle->res), 329 rman_get_bushandle(res), 330 sc->sc_desc.ports[i].offset, ressz, 331 &bh); 332 rman_set_bushandle(rle->res, bh); 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