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, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * 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 #include "opt_puc.h" 85 #include <sys/param.h> 86 #include <sys/systm.h> 87 #include <sys/kernel.h> 88 #include <sys/bus.h> 89 #include <sys/conf.h> 90 #include <sys/malloc.h> 91 92 #include <machine/bus.h> 93 #include <machine/resource.h> 94 #include <sys/rman.h> 95 96 #include <dev/pci/pcireg.h> 97 #include <dev/pci/pcivar.h> 98 99 #define PUC_ENTRAILS 1 100 #include <dev/puc/pucvar.h> 101 102 struct puc_device { 103 struct resource_list resources; 104 int port; 105 int regshft; 106 u_int serialfreq; 107 u_int subtype; 108 }; 109 110 static void puc_intr(void *arg); 111 112 static int puc_find_free_unit(char *); 113 #ifdef PUC_DEBUG 114 static void puc_print_resource_list(struct resource_list *); 115 #endif 116 117 devclass_t puc_devclass; 118 119 static int 120 puc_port_bar_index(struct puc_softc *sc, int bar) 121 { 122 int i; 123 124 for (i = 0; i < PUC_MAX_BAR; i += 1) { 125 if (!sc->sc_bar_mappings[i].used) 126 break; 127 if (sc->sc_bar_mappings[i].bar == bar) 128 return (i); 129 } 130 if (i == PUC_MAX_BAR) { 131 printf("%s: out of bars!\n", __func__); 132 return (-1); 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 bus_space_handle_t bh; 178 179 if (desc == NULL) 180 return (ENXIO); 181 182 sc = (struct puc_softc *)device_get_softc(dev); 183 bzero(sc, sizeof(*sc)); 184 sc->sc_desc = *desc; 185 186 #ifdef PUC_DEBUG 187 bootverbose = 1; 188 189 printf("puc: name: %s\n", sc->sc_desc.name); 190 #endif 191 rid = 0; 192 res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, 193 RF_ACTIVE | RF_SHAREABLE); 194 if (!res) 195 return (ENXIO); 196 197 sc->irqres = res; 198 sc->irqrid = rid; 199 #ifdef PUC_FASTINTR 200 irq_setup = BUS_SETUP_INTR(device_get_parent(dev), dev, res, 201 INTR_TYPE_TTY | INTR_FAST, puc_intr, sc, &sc->intr_cookie); 202 if (irq_setup == 0) 203 sc->fastintr = INTR_FAST; 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 #else 208 irq_setup = BUS_SETUP_INTR(device_get_parent(dev), dev, res, 209 INTR_TYPE_TTY, puc_intr, sc, &sc->intr_cookie); 210 #endif 211 if (irq_setup != 0) 212 return (ENXIO); 213 214 rid = 0; 215 for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) { 216 if (i > 0 && rid == sc->sc_desc.ports[i].bar) 217 sc->barmuxed = 1; 218 rid = sc->sc_desc.ports[i].bar; 219 bidx = puc_port_bar_index(sc, rid); 220 221 if (bidx < 0 || sc->sc_bar_mappings[bidx].res != NULL) 222 continue; 223 224 type = (sc->sc_desc.ports[i].flags & PUC_FLAGS_MEMORY) 225 ? SYS_RES_MEMORY : SYS_RES_IOPORT; 226 227 res = bus_alloc_resource_any(dev, type, &rid, 228 RF_ACTIVE); 229 if (res == NULL && 230 sc->sc_desc.ports[i].flags & PUC_FLAGS_ALTRES) { 231 type = (type == SYS_RES_IOPORT) 232 ? SYS_RES_MEMORY : SYS_RES_IOPORT; 233 res = bus_alloc_resource_any(dev, type, &rid, 234 RF_ACTIVE); 235 } 236 if (res == NULL) { 237 device_printf(dev, "could not get resource\n"); 238 continue; 239 } 240 sc->sc_bar_mappings[bidx].type = type; 241 sc->sc_bar_mappings[bidx].res = res; 242 243 if (sc->sc_desc.ilr_type != PUC_ILR_TYPE_NONE) { 244 sc->ilr_enabled = puc_probe_ilr(sc, res); 245 if (sc->ilr_enabled) 246 device_printf(dev, "ILR enabled\n"); 247 else 248 device_printf(dev, "ILR disabled\n"); 249 } 250 #ifdef PUC_DEBUG 251 printf("%s rid %d bst %lx, start %lx, end %lx\n", 252 (type == SYS_RES_MEMORY) ? "memory" : "port", rid, 253 (u_long)rman_get_bustag(res), (u_long)rman_get_start(res), 254 (u_long)rman_get_end(res)); 255 #endif 256 } 257 258 if (desc->init != NULL) { 259 i = desc->init(sc); 260 if (i != 0) 261 return (i); 262 } 263 264 for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) { 265 rid = sc->sc_desc.ports[i].bar; 266 bidx = puc_port_bar_index(sc, rid); 267 if (bidx < 0 || sc->sc_bar_mappings[bidx].res == NULL) 268 continue; 269 270 switch (sc->sc_desc.ports[i].type & ~PUC_PORT_SUBTYPE_MASK) { 271 case PUC_PORT_TYPE_COM: 272 typestr = "sio"; 273 break; 274 case PUC_PORT_TYPE_LPT: 275 typestr = "ppc"; 276 break; 277 case PUC_PORT_TYPE_UART: 278 typestr = "uart"; 279 break; 280 default: 281 continue; 282 } 283 switch (sc->sc_desc.ports[i].type & PUC_PORT_SUBTYPE_MASK) { 284 case PUC_PORT_UART_SAB82532: 285 ressz = 64; 286 break; 287 case PUC_PORT_UART_Z8530: 288 ressz = 2; 289 break; 290 default: 291 ressz = 8; 292 break; 293 } 294 pdev = malloc(sizeof(struct puc_device), M_DEVBUF, 295 M_NOWAIT | M_ZERO); 296 if (!pdev) 297 continue; 298 resource_list_init(&pdev->resources); 299 300 /* First fake up an IRQ resource. */ 301 resource_list_add(&pdev->resources, SYS_RES_IRQ, 0, 302 rman_get_start(sc->irqres), rman_get_end(sc->irqres), 303 rman_get_end(sc->irqres) - rman_get_start(sc->irqres) + 1); 304 rle = resource_list_find(&pdev->resources, SYS_RES_IRQ, 0); 305 rle->res = sc->irqres; 306 307 /* Now fake an IOPORT or MEMORY resource */ 308 res = sc->sc_bar_mappings[bidx].res; 309 type = sc->sc_bar_mappings[bidx].type; 310 resource_list_add(&pdev->resources, type, 0, 311 rman_get_start(res) + sc->sc_desc.ports[i].offset, 312 rman_get_start(res) + sc->sc_desc.ports[i].offset 313 + ressz - 1, ressz); 314 rle = resource_list_find(&pdev->resources, type, 0); 315 316 if (sc->barmuxed == 0) { 317 rle->res = sc->sc_bar_mappings[bidx].res; 318 } else { 319 rle->res = rman_secret_puc_alloc_resource(M_WAITOK); 320 if (rle->res == NULL) { 321 free(pdev, M_DEVBUF); 322 return (ENOMEM); 323 } 324 325 rman_set_start(rle->res, rman_get_start(res) + 326 sc->sc_desc.ports[i].offset); 327 rman_set_end(rle->res, rman_get_start(rle->res) + 328 ressz - 1); 329 rman_set_bustag(rle->res, rman_get_bustag(res)); 330 bus_space_subregion(rman_get_bustag(rle->res), 331 rman_get_bushandle(res), 332 sc->sc_desc.ports[i].offset, ressz, 333 &bh); 334 rman_set_bushandle(rle->res, bh); 335 } 336 337 pdev->port = i + 1; 338 pdev->serialfreq = sc->sc_desc.ports[i].serialfreq; 339 pdev->subtype = sc->sc_desc.ports[i].type & 340 PUC_PORT_SUBTYPE_MASK; 341 pdev->regshft = sc->sc_desc.ports[i].regshft; 342 343 childunit = puc_find_free_unit(typestr); 344 if (childunit < 0 && strcmp(typestr, "uart") != 0) { 345 typestr = "uart"; 346 childunit = puc_find_free_unit(typestr); 347 } 348 sc->sc_ports[i].dev = device_add_child(dev, typestr, 349 childunit); 350 if (sc->sc_ports[i].dev == NULL) { 351 if (sc->barmuxed) { 352 bus_space_unmap(rman_get_bustag(rle->res), 353 rman_get_bushandle(rle->res), ressz); 354 rman_secret_puc_free_resource(rle->res); 355 free(pdev, M_DEVBUF); 356 } 357 continue; 358 } 359 device_set_ivars(sc->sc_ports[i].dev, pdev); 360 device_set_desc(sc->sc_ports[i].dev, sc->sc_desc.name); 361 #ifdef PUC_DEBUG 362 printf("puc: type %d, bar %x, offset %x\n", 363 sc->sc_desc.ports[i].type, 364 sc->sc_desc.ports[i].bar, 365 sc->sc_desc.ports[i].offset); 366 puc_print_resource_list(&pdev->resources); 367 #endif 368 device_set_flags(sc->sc_ports[i].dev, 369 sc->sc_desc.ports[i].flags); 370 if (device_probe_and_attach(sc->sc_ports[i].dev) != 0) { 371 if (sc->barmuxed) { 372 bus_space_unmap(rman_get_bustag(rle->res), 373 rman_get_bushandle(rle->res), ressz); 374 rman_secret_puc_free_resource(rle->res); 375 free(pdev, M_DEVBUF); 376 } 377 } 378 } 379 380 #ifdef PUC_DEBUG 381 bootverbose = 0; 382 #endif 383 return (0); 384 } 385 386 static u_int32_t 387 puc_ilr_read(struct puc_softc *sc) 388 { 389 u_int32_t mask; 390 int i; 391 392 mask = 0; 393 switch (sc->sc_desc.ilr_type) { 394 case PUC_ILR_TYPE_DIGI: 395 for (i = 1; i >= 0 && sc->sc_desc.ilr_offset[i] != 0; i--) { 396 mask = (mask << 8) | (bus_space_read_1(sc->ilr_st, 397 sc->ilr_sh, sc->sc_desc.ilr_offset[i]) & 0xff); 398 } 399 break; 400 401 default: 402 mask = 0xffffffff; 403 break; 404 } 405 return (mask); 406 } 407 408 /* 409 * This is an interrupt handler. For boards that can't tell us which 410 * device generated the interrupt it just calls all the registered 411 * handlers sequencially, but for boards that can tell us which 412 * device(s) generated the interrupt it calls only handlers for devices 413 * that actually generated the interrupt. 414 */ 415 static void 416 puc_intr(void *arg) 417 { 418 int i; 419 u_int32_t ilr_mask; 420 struct puc_softc *sc; 421 422 sc = (struct puc_softc *)arg; 423 ilr_mask = sc->ilr_enabled ? puc_ilr_read(sc) : 0xffffffff; 424 for (i = 0; i < PUC_MAX_PORTS; i++) 425 if (sc->sc_ports[i].ihand != NULL && 426 ((ilr_mask >> i) & 0x00000001)) 427 (sc->sc_ports[i].ihand)(sc->sc_ports[i].ihandarg); 428 } 429 430 static int 431 puc_find_free_unit(char *name) 432 { 433 devclass_t dc; 434 int start; 435 int unit; 436 437 unit = 0; 438 start = 0; 439 while (resource_int_value(name, unit, "port", &start) == 0 && 440 start > 0) 441 unit++; 442 dc = devclass_find(name); 443 if (dc == NULL) 444 return (-1); 445 while (devclass_get_device(dc, unit)) 446 unit++; 447 #ifdef PUC_DEBUG 448 printf("puc: Using %s%d\n", name, unit); 449 #endif 450 return (unit); 451 } 452 453 #ifdef PUC_DEBUG 454 static void 455 puc_print_resource_list(struct resource_list *rl) 456 { 457 #if 0 458 struct resource_list_entry *rle; 459 460 printf("print_resource_list: rl %p\n", rl); 461 SLIST_FOREACH(rle, rl, link) 462 printf(" type %x, rid %x start %lx end %lx count %lx\n", 463 rle->type, rle->rid, rle->start, rle->end, rle->count); 464 printf("print_resource_list: end.\n"); 465 #endif 466 } 467 #endif 468 469 struct resource * 470 puc_alloc_resource(device_t dev, device_t child, int type, int *rid, 471 u_long start, u_long end, u_long count, u_int flags) 472 { 473 struct puc_device *pdev; 474 struct resource *retval; 475 struct resource_list *rl; 476 struct resource_list_entry *rle; 477 device_t my_child; 478 479 /* 480 * in the case of a child of child we need to find our immediate child 481 */ 482 for (my_child = child; device_get_parent(my_child) != dev; 483 my_child = device_get_parent(my_child)); 484 485 pdev = device_get_ivars(my_child); 486 rl = &pdev->resources; 487 488 #ifdef PUC_DEBUG 489 printf("puc_alloc_resource: pdev %p, looking for t %x, r %x\n", 490 pdev, type, *rid); 491 puc_print_resource_list(rl); 492 #endif 493 retval = NULL; 494 rle = resource_list_find(rl, type, *rid); 495 if (rle) { 496 #ifdef PUC_DEBUG 497 printf("found rle, %lx, %lx, %lx\n", rle->start, rle->end, 498 rle->count); 499 #endif 500 retval = rle->res; 501 } 502 #ifdef PUC_DEBUG 503 else 504 printf("oops rle is gone\n"); 505 #endif 506 507 return (retval); 508 } 509 510 int 511 puc_release_resource(device_t dev, device_t child, int type, int rid, 512 struct resource *res) 513 { 514 return (0); 515 } 516 517 int 518 puc_get_resource(device_t dev, device_t child, int type, int rid, 519 u_long *startp, u_long *countp) 520 { 521 struct puc_device *pdev; 522 struct resource_list *rl; 523 struct resource_list_entry *rle; 524 525 pdev = device_get_ivars(child); 526 rl = &pdev->resources; 527 528 #ifdef PUC_DEBUG 529 printf("puc_get_resource: pdev %p, looking for t %x, r %x\n", pdev, 530 type, rid); 531 puc_print_resource_list(rl); 532 #endif 533 rle = resource_list_find(rl, type, rid); 534 if (rle) { 535 #ifdef PUC_DEBUG 536 printf("found rle %p,", rle); 537 #endif 538 if (startp != NULL) 539 *startp = rle->start; 540 if (countp != NULL) 541 *countp = rle->count; 542 #ifdef PUC_DEBUG 543 printf(" %lx, %lx\n", rle->start, rle->count); 544 #endif 545 return (0); 546 } else 547 printf("oops rle is gone\n"); 548 return (ENXIO); 549 } 550 551 int 552 puc_setup_intr(device_t dev, device_t child, struct resource *r, int flags, 553 void (*ihand)(void *), void *arg, void **cookiep) 554 { 555 int i; 556 struct puc_softc *sc; 557 558 sc = (struct puc_softc *)device_get_softc(dev); 559 if ((flags & INTR_FAST) != sc->fastintr) 560 return (ENXIO); 561 for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) { 562 if (sc->sc_ports[i].dev == child) { 563 if (sc->sc_ports[i].ihand != 0) 564 return (ENXIO); 565 sc->sc_ports[i].ihand = ihand; 566 sc->sc_ports[i].ihandarg = arg; 567 *cookiep = arg; 568 return (0); 569 } 570 } 571 return (ENXIO); 572 } 573 574 int 575 puc_teardown_intr(device_t dev, device_t child, struct resource *r, 576 void *cookie) 577 { 578 int i; 579 struct puc_softc *sc; 580 581 sc = (struct puc_softc *)device_get_softc(dev); 582 for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) { 583 if (sc->sc_ports[i].dev == child) { 584 sc->sc_ports[i].ihand = NULL; 585 sc->sc_ports[i].ihandarg = NULL; 586 return (0); 587 } 588 } 589 return (ENXIO); 590 } 591 592 int 593 puc_read_ivar(device_t dev, device_t child, int index, uintptr_t *result) 594 { 595 struct puc_device *pdev; 596 597 pdev = device_get_ivars(child); 598 if (pdev == NULL) 599 return (ENOENT); 600 601 switch(index) { 602 case PUC_IVAR_FREQ: 603 *result = pdev->serialfreq; 604 break; 605 case PUC_IVAR_PORT: 606 *result = pdev->port; 607 break; 608 case PUC_IVAR_REGSHFT: 609 *result = pdev->regshft; 610 break; 611 case PUC_IVAR_SUBTYPE: 612 *result = pdev->subtype; 613 break; 614 default: 615 return (ENOENT); 616 } 617 return (0); 618 } 619