puc.c revision 131371
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: head/sys/dev/puc/puc.c 131371 2004-06-30 21:27:59Z imp $"); 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: head/sys/dev/puc/puc.c 131371 2004-06-30 21:27:59Z imp $"); 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#define __RMAN_RESOURCE_VISIBLE /* Shouldn't be there */ 88#include "opt_puc.h" 89#define __RMAN_RESOURCE_VISIBLE 90#include <sys/param.h> 91#include <sys/systm.h> 92#include <sys/kernel.h> 93#include <sys/bus.h> 94#include <sys/conf.h> 95#include <sys/malloc.h> 96 97#include <machine/bus.h> 98#include <machine/resource.h> 99#include <sys/rman.h> 100 101#include <dev/pci/pcireg.h> 102#include <dev/pci/pcivar.h> 103 104#define PUC_ENTRAILS 1 105#include <dev/puc/pucvar.h> 106 107struct puc_device { 108 struct resource_list resources; 109 int port; 110 int regshft; 111 u_int serialfreq; 112 u_int subtype; 113}; 114 115static void puc_intr(void *arg); 116 117static int puc_find_free_unit(char *); 118#ifdef PUC_DEBUG 119static void puc_print_resource_list(struct resource_list *); 120#endif 121 122devclass_t puc_devclass; 123 124static int 125puc_port_bar_index(struct puc_softc *sc, int bar) 126{ 127 int i; 128 129 for (i = 0; i < PUC_MAX_BAR; i += 1) { 130 if (!sc->sc_bar_mappings[i].used) 131 break; 132 if (sc->sc_bar_mappings[i].bar == bar) 133 return (i); 134 } 135 sc->sc_bar_mappings[i].bar = bar; 136 sc->sc_bar_mappings[i].used = 1; 137 return (i); 138} 139 140static int 141puc_probe_ilr(struct puc_softc *sc, struct resource *res) 142{ 143 u_char t1, t2; 144 int i; 145 146 switch (sc->sc_desc.ilr_type) { 147 case PUC_ILR_TYPE_DIGI: 148 sc->ilr_st = rman_get_bustag(res); 149 sc->ilr_sh = rman_get_bushandle(res); 150 for (i = 0; i < 2 && sc->sc_desc.ilr_offset[i] != 0; i++) { 151 t1 = bus_space_read_1(sc->ilr_st, sc->ilr_sh, 152 sc->sc_desc.ilr_offset[i]); 153 t1 = ~t1; 154 bus_space_write_1(sc->ilr_st, sc->ilr_sh, 155 sc->sc_desc.ilr_offset[i], t1); 156 t2 = bus_space_read_1(sc->ilr_st, sc->ilr_sh, 157 sc->sc_desc.ilr_offset[i]); 158 if (t2 == t1) 159 return (0); 160 } 161 return (1); 162 163 default: 164 break; 165 } 166 return (0); 167} 168 169int 170puc_attach(device_t dev, const struct puc_device_description *desc) 171{ 172 char *typestr; 173 int bidx, childunit, i, irq_setup, ressz, rid, type; 174 struct puc_softc *sc; 175 struct puc_device *pdev; 176 struct resource *res; 177 struct resource_list_entry *rle; 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 (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 (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 = malloc(sizeof(struct resource), M_DEVBUF, 320 M_WAITOK | M_ZERO); 321 if (rle->res == NULL) { 322 free(pdev, M_DEVBUF); 323 return (ENOMEM); 324 } 325 326 rle->res->r_start = rman_get_start(res) + 327 sc->sc_desc.ports[i].offset; 328 rle->res->r_end = rle->res->r_start + ressz - 1; 329 rle->res->r_bustag = rman_get_bustag(res); 330 bus_space_subregion(rle->res->r_bustag, 331 rman_get_bushandle(res), 332 sc->sc_desc.ports[i].offset, ressz, 333 &rle->res->r_bushandle); 334 } 335 336 pdev->port = i + 1; 337 pdev->serialfreq = sc->sc_desc.ports[i].serialfreq; 338 pdev->subtype = sc->sc_desc.ports[i].type & 339 PUC_PORT_SUBTYPE_MASK; 340 pdev->regshft = sc->sc_desc.ports[i].regshft; 341 342 childunit = puc_find_free_unit(typestr); 343 if (childunit < 0 && strcmp(typestr, "uart") != 0) { 344 typestr = "uart"; 345 childunit = puc_find_free_unit(typestr); 346 } 347 sc->sc_ports[i].dev = device_add_child(dev, typestr, 348 childunit); 349 if (sc->sc_ports[i].dev == NULL) { 350 if (sc->barmuxed) { 351 bus_space_unmap(rman_get_bustag(rle->res), 352 rman_get_bushandle(rle->res), ressz); 353 free(rle->res, M_DEVBUF); 354 free(pdev, M_DEVBUF); 355 } 356 continue; 357 } 358 device_set_ivars(sc->sc_ports[i].dev, pdev); 359 device_set_desc(sc->sc_ports[i].dev, sc->sc_desc.name); 360#ifdef PUC_DEBUG 361 printf("puc: type %d, bar %x, offset %x\n", 362 sc->sc_desc.ports[i].type, 363 sc->sc_desc.ports[i].bar, 364 sc->sc_desc.ports[i].offset); 365 puc_print_resource_list(&pdev->resources); 366#endif 367 device_set_flags(sc->sc_ports[i].dev, 368 sc->sc_desc.ports[i].flags); 369 if (device_probe_and_attach(sc->sc_ports[i].dev) != 0) { 370 if (sc->barmuxed) { 371 bus_space_unmap(rman_get_bustag(rle->res), 372 rman_get_bushandle(rle->res), ressz); 373 free(rle->res, M_DEVBUF); 374 free(pdev, M_DEVBUF); 375 } 376 } 377 } 378 379#ifdef PUC_DEBUG 380 bootverbose = 0; 381#endif 382 return (0); 383} 384 385static u_int32_t 386puc_ilr_read(struct puc_softc *sc) 387{ 388 u_int32_t mask; 389 int i; 390 391 mask = 0; 392 switch (sc->sc_desc.ilr_type) { 393 case PUC_ILR_TYPE_DIGI: 394 for (i = 1; i >= 0 && sc->sc_desc.ilr_offset[i] != 0; i--) { 395 mask = (mask << 8) | (bus_space_read_1(sc->ilr_st, 396 sc->ilr_sh, sc->sc_desc.ilr_offset[i]) & 0xff); 397 } 398 break; 399 400 default: 401 mask = 0xffffffff; 402 break; 403 } 404 return (mask); 405} 406 407/* 408 * This is an interrupt handler. For boards that can't tell us which 409 * device generated the interrupt it just calls all the registered 410 * handlers sequencially, but for boards that can tell us which 411 * device(s) generated the interrupt it calls only handlers for devices 412 * that actually generated the interrupt. 413 */ 414static void 415puc_intr(void *arg) 416{ 417 int i; 418 u_int32_t ilr_mask; 419 struct puc_softc *sc; 420 421 sc = (struct puc_softc *)arg; 422 ilr_mask = sc->ilr_enabled ? puc_ilr_read(sc) : 0xffffffff; 423 for (i = 0; i < PUC_MAX_PORTS; i++) 424 if (sc->sc_ports[i].ihand != NULL && 425 ((ilr_mask >> i) & 0x00000001)) 426 (sc->sc_ports[i].ihand)(sc->sc_ports[i].ihandarg); 427} 428 429static int 430puc_find_free_unit(char *name) 431{ 432 devclass_t dc; 433 int start; 434 int unit; 435 436 unit = 0; 437 start = 0; 438 while (resource_int_value(name, unit, "port", &start) == 0 && 439 start > 0) 440 unit++; 441 dc = devclass_find(name); 442 if (dc == NULL) 443 return (-1); 444 while (devclass_get_device(dc, unit)) 445 unit++; 446#ifdef PUC_DEBUG 447 printf("puc: Using %s%d\n", name, unit); 448#endif 449 return (unit); 450} 451 452#ifdef PUC_DEBUG 453static void 454puc_print_resource_list(struct resource_list *rl) 455{ 456#if 0 457 struct resource_list_entry *rle; 458 459 printf("print_resource_list: rl %p\n", rl); 460 SLIST_FOREACH(rle, rl, link) 461 printf(" type %x, rid %x start %lx end %lx count %lx\n", 462 rle->type, rle->rid, rle->start, rle->end, rle->count); 463 printf("print_resource_list: end.\n"); 464#endif 465} 466#endif 467 468struct resource * 469puc_alloc_resource(device_t dev, device_t child, int type, int *rid, 470 u_long start, u_long end, u_long count, u_int flags) 471{ 472 struct puc_device *pdev; 473 struct resource *retval; 474 struct resource_list *rl; 475 struct resource_list_entry *rle; 476 device_t my_child; 477 478 /* 479 * in the case of a child of child we need to find our immediate child 480 */ 481 for (my_child = child; device_get_parent(my_child) != dev; 482 my_child = device_get_parent(my_child)); 483 484 pdev = device_get_ivars(my_child); 485 rl = &pdev->resources; 486 487#ifdef PUC_DEBUG 488 printf("puc_alloc_resource: pdev %p, looking for t %x, r %x\n", 489 pdev, type, *rid); 490 puc_print_resource_list(rl); 491#endif 492 retval = NULL; 493 rle = resource_list_find(rl, type, *rid); 494 if (rle) { 495#ifdef PUC_DEBUG 496 printf("found rle, %lx, %lx, %lx\n", rle->start, rle->end, 497 rle->count); 498#endif 499 retval = rle->res; 500 } 501#ifdef PUC_DEBUG 502 else 503 printf("oops rle is gone\n"); 504#endif 505 506 return (retval); 507} 508 509int 510puc_release_resource(device_t dev, device_t child, int type, int rid, 511 struct resource *res) 512{ 513 return (0); 514} 515 516int 517puc_get_resource(device_t dev, device_t child, int type, int rid, 518 u_long *startp, u_long *countp) 519{ 520 struct puc_device *pdev; 521 struct resource_list *rl; 522 struct resource_list_entry *rle; 523 524 pdev = device_get_ivars(child); 525 rl = &pdev->resources; 526 527#ifdef PUC_DEBUG 528 printf("puc_get_resource: pdev %p, looking for t %x, r %x\n", pdev, 529 type, rid); 530 puc_print_resource_list(rl); 531#endif 532 rle = resource_list_find(rl, type, rid); 533 if (rle) { 534#ifdef PUC_DEBUG 535 printf("found rle %p,", rle); 536#endif 537 if (startp != NULL) 538 *startp = rle->start; 539 if (countp != NULL) 540 *countp = rle->count; 541#ifdef PUC_DEBUG 542 printf(" %lx, %lx\n", rle->start, rle->count); 543#endif 544 return (0); 545 } else 546 printf("oops rle is gone\n"); 547 return (ENXIO); 548} 549 550int 551puc_setup_intr(device_t dev, device_t child, struct resource *r, int flags, 552 void (*ihand)(void *), void *arg, void **cookiep) 553{ 554 int i; 555 struct puc_softc *sc; 556 557 sc = (struct puc_softc *)device_get_softc(dev); 558 if ((flags & INTR_FAST) != sc->fastintr) 559 return (ENXIO); 560 for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) { 561 if (sc->sc_ports[i].dev == child) { 562 if (sc->sc_ports[i].ihand != 0) 563 return (ENXIO); 564 sc->sc_ports[i].ihand = ihand; 565 sc->sc_ports[i].ihandarg = arg; 566 *cookiep = arg; 567 return (0); 568 } 569 } 570 return (ENXIO); 571} 572 573int 574puc_teardown_intr(device_t dev, device_t child, struct resource *r, 575 void *cookie) 576{ 577 int i; 578 struct puc_softc *sc; 579 580 sc = (struct puc_softc *)device_get_softc(dev); 581 for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) { 582 if (sc->sc_ports[i].dev == child) { 583 sc->sc_ports[i].ihand = NULL; 584 sc->sc_ports[i].ihandarg = NULL; 585 return (0); 586 } 587 } 588 return (ENXIO); 589} 590 591int 592puc_read_ivar(device_t dev, device_t child, int index, uintptr_t *result) 593{ 594 struct puc_device *pdev; 595 596 pdev = device_get_ivars(child); 597 if (pdev == NULL) 598 return (ENOENT); 599 600 switch(index) { 601 case PUC_IVAR_FREQ: 602 *result = pdev->serialfreq; 603 break; 604 case PUC_IVAR_PORT: 605 *result = pdev->port; 606 break; 607 case PUC_IVAR_REGSHFT: 608 *result = pdev->regshft; 609 break; 610 case PUC_IVAR_SUBTYPE: 611 *result = pdev->subtype; 612 break; 613 default: 614 return (ENOENT); 615 } 616 return (0); 617} 618