gusc.c revision 53682
1/*- 2 * Copyright (c) 1999 Seigo Tanimura 3 * Copyright (c) 1999 Ville-Pertti Keinonen 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 * 27 * $FreeBSD: head/sys/dev/sound/isa/gusc.c 53682 1999-11-25 01:13:52Z tanimura $ 28 */ 29 30#include "gusc.h" 31#include "isa.h" 32#include "pnp.h" 33 34#include <sys/param.h> 35#include <sys/systm.h> 36#include <sys/kernel.h> 37#include <sys/bus.h> 38#include <sys/malloc.h> 39#include <sys/module.h> 40#include <machine/resource.h> 41#include <machine/bus.h> 42#include <machine/clock.h> 43#include <sys/rman.h> 44#include <sys/soundcard.h> 45#include <dev/sound/pcm/sound.h> 46#include <dev/sound/chip.h> 47#include "bus_if.h" 48 49 50#if NISA > 0 51#include <isa/isavar.h> 52#include <isa/isa_common.h> 53#ifdef __alpha__ /* XXX workaround a stupid warning */ 54#include <alpha/isa/isavar.h> 55#endif 56#endif /* NISA > 0 */ 57 58#if NGUSC > 0 59 60#define LOGICALID_NOPNP 0 61#define LOGICALID_PCM 0x0000561e 62#define LOGICALID_OPL 0x0300561e 63#define LOGICALID_MIDI 0x0400561e 64 65/* Interrupt handler. */ 66struct gusc_ihandler { 67 void (*intr)(void *); 68 void *arg; 69}; 70 71/* Here is the parameter structure per a device. */ 72struct gusc_softc { 73 device_t dev; /* device */ 74 int io_rid[3]; /* io port rids */ 75 struct resource *io[3]; /* io port resources */ 76 int io_alloced[3]; /* io port alloc flag */ 77 int irq_rid; /* irq rids */ 78 struct resource *irq; /* irq resources */ 79 int irq_alloced; /* irq alloc flag */ 80 int drq_rid[2]; /* drq rids */ 81 struct resource *drq[2]; /* drq resources */ 82 int drq_alloced[2]; /* drq alloc flag */ 83 84 /* Interrupts are shared (XXX non-PnP only?) */ 85 struct gusc_ihandler midi_intr; 86 struct gusc_ihandler pcm_intr; 87}; 88 89typedef struct gusc_softc *sc_p; 90 91#if NISA > 0 92static int gusc_probe(device_t dev); 93static int gusc_attach(device_t dev); 94static int gusisa_probe(device_t dev); 95static void gusc_intr(void *); 96#endif /* NISA > 0 */ 97static struct resource *gusc_alloc_resource(device_t bus, device_t child, int type, int *rid, 98 u_long start, u_long end, u_long count, u_int flags); 99static int gusc_release_resource(device_t bus, device_t child, int type, int rid, 100 struct resource *r); 101 102#if notyet 103static device_t find_masterdev(sc_p scp); 104#endif /* notyet */ 105static int alloc_resource(sc_p scp); 106static int release_resource(sc_p scp); 107 108static devclass_t gusc_devclass; 109 110#if NISA > 0 111 112static int 113gusc_probe(device_t dev) 114{ 115 u_int32_t vend_id, logical_id; 116 char *s; 117 struct sndcard_func *func; 118 119 vend_id = isa_get_vendorid(dev); 120 if (vend_id == 0) 121 return gusisa_probe(dev); 122 123#if NPNP > 0 124 logical_id = isa_get_logicalid(dev); 125 s = NULL; 126 127 if (vend_id == 0x0100561e) { /* Gravis */ 128 switch (logical_id) { 129 case LOGICALID_PCM: 130 s = "Gravis UltraSound Plug & Play PCM"; 131 func = malloc(sizeof(struct sndcard_func), M_DEVBUF, M_NOWAIT); 132 if (func == NULL) 133 return (ENOMEM); 134 bzero(func, sizeof(*func)); 135 func->func = SCF_PCM; 136 device_add_child(dev, "pcm", -1, func); 137 break; 138#if notyet 139 case LOGICALID_OPL: 140 s = "Gravis UltraSound Plug & Play OPL"; 141 func = malloc(sizeof(struct sndcard_func), M_DEVBUF, M_NOWAIT); 142 if (func == NULL) 143 return (ENOMEM); 144 bzero(func, sizeof(*func)); 145 func->func = SCF_SYNTH; 146 device_add_child(dev, "midi", -1, func); 147 break; 148 case LOGICALID_MIDI: 149 s = "Gravis UltraSound Plug & Play MIDI"; 150 func = malloc(sizeof(struct sndcard_func), M_DEVBUF, M_NOWAIT); 151 if (func == NULL) 152 return (ENOMEM); 153 bzero(func, sizeof(*func)); 154 func->func = SCF_MIDI; 155 device_add_child(dev, "midi", -1, func); 156 break; 157#endif /* notyet */ 158 } 159 } 160 161 if (s != NULL) { 162 device_set_desc(dev, s); 163 return (0); 164 } 165#endif /* NPNP > 0 */ 166 167 return (ENXIO); 168} 169 170static void 171port_wr(struct resource *r, int i, unsigned char v) 172{ 173 bus_space_write_1(rman_get_bustag(r), rman_get_bushandle(r), i, v); 174} 175 176static int 177port_rd(struct resource *r, int i) 178{ 179 return bus_space_read_1(rman_get_bustag(r), rman_get_bushandle(r), i); 180} 181 182/* 183 * Probe for an old (non-PnP) GUS card on the ISA bus. 184 */ 185 186static int 187gusisa_probe(device_t dev) 188{ 189 struct resource *res, *res2; 190 int base, rid, rid2, s, flags; 191 unsigned char val; 192 193 base = isa_get_port(dev); 194 flags = device_get_flags(dev); 195 rid = 1; 196 res = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, base + 0x100, 197 base + 0x107, 8, RF_ACTIVE); 198 199 if (res == NULL) 200 return ENXIO; 201 202 res2 = NULL; 203 204 /* 205 * Check for the presence of some GUS card. Reset the card, 206 * then see if we can access the memory on it. 207 */ 208 209 port_wr(res, 3, 0x4c); 210 port_wr(res, 5, 0); 211 DELAY(30 * 1000); 212 213 port_wr(res, 3, 0x4c); 214 port_wr(res, 5, 1); 215 DELAY(30 * 1000); 216 217 s = splhigh(); 218 219 /* Write to DRAM. */ 220 221 port_wr(res, 3, 0x43); /* Register select */ 222 port_wr(res, 4, 0); /* Low addr */ 223 port_wr(res, 5, 0); /* Med addr */ 224 225 port_wr(res, 3, 0x44); /* Register select */ 226 port_wr(res, 4, 0); /* High addr */ 227 port_wr(res, 7, 0x55); /* DRAM */ 228 229 /* Read from DRAM. */ 230 231 port_wr(res, 3, 0x43); /* Register select */ 232 port_wr(res, 4, 0); /* Low addr */ 233 port_wr(res, 5, 0); /* Med addr */ 234 235 port_wr(res, 3, 0x44); /* Register select */ 236 port_wr(res, 4, 0); /* High addr */ 237 val = port_rd(res, 7); /* DRAM */ 238 239 splx(s); 240 241 if (val != 0x55) 242 goto fail; 243 244 rid2 = 0; 245 res2 = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid2, base, base, 1, 246 RF_ACTIVE); 247 248 if (res2 == NULL) 249 goto fail; 250 251 s = splhigh(); 252 port_wr(res2, 0x0f, 0x20); 253 val = port_rd(res2, 0x0f); 254 splx(s); 255 256 if (val == 0xff || (val & 0x06) == 0) 257 val = 0; 258 else { 259 val = port_rd(res2, 0x506); /* XXX Out of range. */ 260 if (val == 0xff) 261 val = 0; 262 } 263 264 bus_release_resource(dev, SYS_RES_IOPORT, rid2, res2); 265 bus_release_resource(dev, SYS_RES_IOPORT, rid, res); 266 267 if (val >= 10) { 268 struct sndcard_func *func; 269 270 /* Looks like a GUS MAX. Set the rest of the resources. */ 271 272 bus_set_resource(dev, SYS_RES_IOPORT, 2, base + 0x10c, 8); 273 274 if (flags & DV_F_DUAL_DMA) 275 bus_set_resource(dev, SYS_RES_DRQ, 1, 276 flags & DV_F_DRQ_MASK, 1); 277 278#if notyet 279 /* We can support the CS4231 and MIDI devices. */ 280 281 func = malloc(sizeof(struct sndcard_func), M_DEVBUF, M_NOWAIT); 282 if (func == NULL) 283 return ENOMEM; 284 bzero(func, sizeof *func); 285 func->func = SCF_MIDI; 286 device_add_child(dev, "midi", -1, func); 287#endif /* notyet */ 288 289 func = malloc(sizeof(struct sndcard_func), M_DEVBUF, M_NOWAIT); 290 if (func == NULL) 291 printf("xxx: gus pcm not attached, out of memory\n"); 292 else { 293 bzero(func, sizeof *func); 294 func->func = SCF_PCM; 295 device_add_child(dev, "pcm", -1, func); 296 } 297 device_set_desc(dev, "Gravis UltraSound MAX"); 298 return 0; 299 } else { 300 301 /* 302 * TODO: Support even older GUS cards. MIDI should work on 303 * all models. 304 */ 305 return ENXIO; 306 } 307 308fail: 309 bus_release_resource(dev, SYS_RES_IOPORT, rid, res); 310 return ENXIO; 311} 312 313static int 314gusc_attach(device_t dev) 315{ 316 sc_p scp; 317 int unit; 318 void *ih; 319 320 scp = device_get_softc(dev); 321 unit = device_get_unit(dev); 322 323 bzero(scp, sizeof(*scp)); 324 325 scp->dev = dev; 326 if (alloc_resource(scp)) { 327 release_resource(scp); 328 return (ENXIO); 329 } 330 331 bus_setup_intr(dev, scp->irq, INTR_TYPE_TTY, gusc_intr, scp, &ih); 332 bus_generic_attach(dev); 333 334 return (0); 335} 336 337/* 338 * Handle interrupts on GUS devices until there aren't any left. 339 */ 340static void 341gusc_intr(void *arg) 342{ 343 sc_p scp = (sc_p)arg; 344 int did_something; 345 346 do { 347 did_something = 0; 348 if (scp->pcm_intr.intr != NULL && 349 (port_rd(scp->io[2], 2) & 1)) { 350 (*scp->pcm_intr.intr)(scp->pcm_intr.arg); 351 did_something = 1; 352 } 353#if notyet 354 if (scp->midi_intr.intr != NULL && 355 (port_rd(scp->io[1], 0) & 0x80)) { 356 (*scp->midi_intr.intr)(scp->midi_intr.arg); 357 did_something = 1; 358 } 359#endif /* notyet */ 360 } while (did_something != 0); 361} 362#endif /* NISA > 0 */ 363 364static struct resource * 365gusc_alloc_resource(device_t bus, device_t child, int type, int *rid, 366 u_long start, u_long end, u_long count, u_int flags) 367{ 368 sc_p scp; 369 int *alloced, rid_max, alloced_max; 370 struct resource **res; 371 372 scp = device_get_softc(bus); 373 switch (type) { 374 case SYS_RES_IOPORT: 375 alloced = scp->io_alloced; 376 res = scp->io; 377 rid_max = 2; 378 alloced_max = 2; /* pcm + midi (more to include synth) */ 379 break; 380 case SYS_RES_IRQ: 381 alloced = &scp->irq_alloced; 382 res = &scp->irq; 383 rid_max = 0; 384 alloced_max = 2; /* pcm and midi share the single irq. */ 385 break; 386 case SYS_RES_DRQ: 387 alloced = scp->drq_alloced; 388 res = scp->drq; 389 rid_max = 1; 390 alloced_max = 1; 391 break; 392 default: 393 return (NULL); 394 } 395 396 if (*rid > rid_max || alloced[*rid] == alloced_max) 397 return (NULL); 398 399 alloced[*rid]++; 400 return (res[*rid]); 401} 402 403static int 404gusc_release_resource(device_t bus, device_t child, int type, int rid, 405 struct resource *r) 406{ 407 sc_p scp; 408 int *alloced, rid_max; 409 410 scp = device_get_softc(bus); 411 switch (type) { 412 case SYS_RES_IOPORT: 413 alloced = scp->io_alloced; 414 rid_max = 2; 415 break; 416 case SYS_RES_IRQ: 417 alloced = &scp->irq_alloced; 418 rid_max = 0; 419 break; 420 case SYS_RES_DRQ: 421 alloced = scp->drq_alloced; 422 rid_max = 1; 423 break; 424 default: 425 return (1); 426 } 427 428 if (rid > rid_max || alloced[rid] == 0) 429 return (1); 430 431 alloced[rid]--; 432 return (0); 433} 434 435static int 436gusc_setup_intr(device_t dev, device_t child, struct resource *irq, 437 int flags, driver_intr_t *intr, void *arg, void **cookiep) 438{ 439 sc_p scp = (sc_p)device_get_softc(dev); 440 devclass_t devclass; 441 442 devclass = device_get_devclass(child); 443 if (strcmp(devclass_get_name(devclass), "midi") == 0) { 444 scp->midi_intr.intr = intr; 445 scp->midi_intr.arg = arg; 446 return 0; 447 } else if (strcmp(devclass_get_name(devclass), "pcm") == 0) { 448 scp->pcm_intr.intr = intr; 449 scp->pcm_intr.arg = arg; 450 return 0; 451 } 452 return bus_generic_setup_intr(dev, child, irq, flags, intr, 453 arg, cookiep); 454} 455 456#if notyet 457static device_t 458find_masterdev(sc_p scp) 459{ 460 int i, units; 461 devclass_t devclass; 462 device_t dev; 463 464 devclass = device_get_devclass(scp->dev); 465 units = devclass_get_maxunit(devclass); 466 dev = NULL; 467 for (i = 0 ; i < units ; i++) { 468 dev = devclass_get_device(devclass, i); 469 if (isa_get_vendorid(dev) == isa_get_vendorid(scp->dev) 470 && isa_get_logicalid(dev) == LOGICALID_PCM 471 && isa_get_serial(dev) == isa_get_serial(scp->dev)) 472 break; 473 } 474 if (i == units) 475 return (NULL); 476 477 return (dev); 478} 479#endif /* notyet */ 480 481static int io_range[3] = {0x10, 0x8 , 0x4 }; 482static int io_offset[3] = {0x0 , 0x100, 0x10c}; 483static int 484alloc_resource(sc_p scp) 485{ 486 int i, base, lid, flags; 487#if notyet 488 device_t dev; 489#endif /* notyet */ 490 491 flags = 0; 492 if (isa_get_vendorid(scp->dev)) 493 lid = isa_get_logicalid(scp->dev); 494 else { 495 lid = LOGICALID_NOPNP; 496 flags = device_get_flags(scp->dev); 497 } 498 switch(lid) { 499 case LOGICALID_PCM: 500 case LOGICALID_NOPNP: /* XXX Non-PnP */ 501 if (lid == LOGICALID_NOPNP) 502 base = isa_get_port(scp->dev); 503 else 504 base = 0; 505 for (i = 0 ; i < sizeof(scp->io) / sizeof(*scp->io) ; i++) { 506 if (scp->io[i] == NULL) { 507 scp->io_rid[i] = i; 508 if (base == 0) 509 scp->io[i] = bus_alloc_resource(scp->dev, SYS_RES_IOPORT, &scp->io_rid[i], 510 0, ~0, io_range[i], RF_ACTIVE); 511 else 512 scp->io[i] = bus_alloc_resource(scp->dev, SYS_RES_IOPORT, &scp->io_rid[i], 513 base + io_offset[i], 514 base + io_offset[i] + io_range[i] - 1 515 , io_range[i], RF_ACTIVE); 516 if (scp->io[i] == NULL) 517 return (1); 518 scp->io_alloced[i] = 0; 519 } 520 } 521 if (scp->irq == NULL) { 522 scp->irq_rid = 0; 523 scp->irq = bus_alloc_resource(scp->dev, SYS_RES_IRQ, &scp->irq_rid, 524 0, ~0, 1, RF_ACTIVE | RF_SHAREABLE); 525 if (scp->irq == NULL) 526 return (1); 527 scp->irq_alloced = 0; 528 } 529 for (i = 0 ; i < sizeof(scp->drq) / sizeof(*scp->drq) ; i++) { 530 if (scp->drq[i] == NULL) { 531 scp->drq_rid[i] = i; 532 if (base == 0 || i == 0) 533 scp->drq[i] = bus_alloc_resource(scp->dev, SYS_RES_DRQ, &scp->drq_rid[i], 534 0, ~0, 1, RF_ACTIVE); 535 else if ((flags & DV_F_DUAL_DMA) != 0) 536 /* XXX The secondary drq is specified in the flag. */ 537 scp->drq[i] = bus_alloc_resource(scp->dev, SYS_RES_DRQ, &scp->drq_rid[i], 538 flags & DV_F_DRQ_MASK, 539 flags & DV_F_DRQ_MASK, 1, RF_ACTIVE); 540 if (scp->drq[i] == NULL) 541 return (1); 542 scp->drq_alloced[i] = 0; 543 } 544 } 545 break; 546#if notyet 547 case LOGICALID_OPL: 548 if (scp->io[0] == NULL) { 549 scp->io_rid[0] = 0; 550 scp->io[0] = bus_alloc_resource(scp->dev, SYS_RES_IOPORT, &scp->io_rid[0], 551 0, ~0, io_range[0], RF_ACTIVE); 552 if (scp->io[0] == NULL) 553 return (1); 554 scp->io_alloced[0] = 0; 555 } 556 break; 557 case LOGICALID_MIDI: 558 if (scp->io[0] == NULL) { 559 scp->io_rid[0] = 0; 560 scp->io[0] = bus_alloc_resource(scp->dev, SYS_RES_IOPORT, &scp->io_rid[0], 561 0, ~0, io_range[0], RF_ACTIVE); 562 if (scp->io[0] == NULL) 563 return (1); 564 scp->io_alloced[0] = 0; 565 } 566 if (scp->irq == NULL) { 567 /* The irq is shared with pcm audio. */ 568 dev = find_masterdev(scp); 569 if (dev == NULL) 570 return (1); 571 scp->irq_rid = 0; 572 scp->irq = BUS_ALLOC_RESOURCE(dev, NULL, SYS_RES_IRQ, &scp->irq_rid, 573 0, ~0, 1, RF_ACTIVE | RF_SHAREABLE); 574 if (scp->irq == NULL) 575 return (1); 576 scp->irq_alloced = 0; 577 } 578 break; 579#endif /* notyet */ 580 } 581 return (0); 582} 583 584static int 585release_resource(sc_p scp) 586{ 587 int i, lid, flags; 588#if notyet 589 device_t dev; 590#endif /* notyet */ 591 592 flags = 0; 593 if (isa_get_vendorid(scp->dev)) 594 lid = isa_get_logicalid(scp->dev); 595 else { 596 lid = LOGICALID_NOPNP; 597 flags = device_get_flags(scp->dev); 598 } 599 switch(lid) { 600 case LOGICALID_PCM: 601 case LOGICALID_NOPNP: /* XXX Non-PnP */ 602 for (i = 0 ; i < sizeof(scp->io) / sizeof(*scp->io) ; i++) { 603 if (scp->io[i] != NULL) { 604 bus_release_resource(scp->dev, SYS_RES_IOPORT, scp->io_rid[i], scp->io[i]); 605 scp->io[i] = NULL; 606 } 607 } 608 if (scp->irq != NULL) { 609 bus_release_resource(scp->dev, SYS_RES_IRQ, scp->irq_rid, scp->irq); 610 scp->irq = NULL; 611 } 612 for (i = 0 ; i < sizeof(scp->drq) / sizeof(*scp->drq) ; i++) { 613 if (scp->drq[i] != NULL) { 614 bus_release_resource(scp->dev, SYS_RES_DRQ, scp->drq_rid[i], scp->drq[i]); 615 scp->drq[i] = NULL; 616 } 617 } 618 break; 619#if notyet 620 case LOGICALID_OPL: 621 if (scp->io[0] != NULL) { 622 bus_release_resource(scp->dev, SYS_RES_IOPORT, scp->io_rid[0], scp->io[0]); 623 scp->io[0] = NULL; 624 } 625 break; 626 case LOGICALID_MIDI: 627 if (scp->io[0] != NULL) { 628 bus_release_resource(scp->dev, SYS_RES_IOPORT, scp->io_rid[0], scp->io[0]); 629 scp->io[0] = NULL; 630 } 631 if (scp->irq != NULL) { 632 /* The irq is shared with pcm audio. */ 633 dev = find_masterdev(scp); 634 if (dev == NULL) 635 return (1); 636 BUS_RELEASE_RESOURCE(dev, NULL, SYS_RES_IOPORT, scp->irq_rid, scp->irq); 637 scp->irq = NULL; 638 } 639 break; 640#endif /* notyet */ 641 } 642 return (0); 643} 644 645#if NISA > 0 646static device_method_t gusc_methods[] = { 647 /* Device interface */ 648 DEVMETHOD(device_probe, gusc_probe), 649 DEVMETHOD(device_attach, gusc_attach), 650 DEVMETHOD(device_detach, bus_generic_detach), 651 DEVMETHOD(device_shutdown, bus_generic_shutdown), 652 DEVMETHOD(device_suspend, bus_generic_suspend), 653 DEVMETHOD(device_resume, bus_generic_resume), 654 655 /* Bus interface */ 656 DEVMETHOD(bus_print_child, bus_generic_print_child), 657 DEVMETHOD(bus_alloc_resource, gusc_alloc_resource), 658 DEVMETHOD(bus_release_resource, gusc_release_resource), 659 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource), 660 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource), 661 DEVMETHOD(bus_setup_intr, gusc_setup_intr), 662 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr), 663 664 { 0, 0 } 665}; 666 667static driver_t gusc_driver = { 668 "gusc", 669 gusc_methods, 670 sizeof(struct gusc_softc), 671}; 672 673/* 674 * gusc can be attached to an isa bus. 675 */ 676DRIVER_MODULE(gusc, isa, gusc_driver, gusc_devclass, 0, 0); 677#endif /* NISA > 0 */ 678 679#endif /* NGUSC > 0 */ 680