sbc.c revision 58385
1/*- 2 * Copyright (c) 1999 Seigo Tanimura 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 * 26 * $FreeBSD: head/sys/dev/sound/isa/sbc.c 58385 2000-03-20 15:34:27Z cg $ 27 */ 28 29#include "isa.h" 30 31#include <dev/sound/chip.h> 32#include <dev/sound/pcm/sound.h> 33#include <dev/sound/isa/sb.h> 34 35#define IO_MAX 3 36#define IRQ_MAX 1 37#define DRQ_MAX 2 38#define INTR_MAX 2 39 40struct sbc_ihl { 41 driver_intr_t *intr[INTR_MAX]; 42 void *intr_arg[INTR_MAX]; 43}; 44 45/* Here is the parameter structure per a device. */ 46struct sbc_softc { 47 device_t dev; /* device */ 48 49 int io_rid[IO_MAX]; /* io port rids */ 50 struct resource *io[IO_MAX]; /* io port resources */ 51 int io_alloced[IO_MAX]; /* io port alloc flag */ 52 53 int irq_rid[IRQ_MAX]; /* irq rids */ 54 struct resource *irq[IRQ_MAX]; /* irq resources */ 55 int irq_alloced[IRQ_MAX]; /* irq alloc flag */ 56 57 int drq_rid[DRQ_MAX]; /* drq rids */ 58 struct resource *drq[DRQ_MAX]; /* drq resources */ 59 int drq_alloced[DRQ_MAX]; /* drq alloc flag */ 60 61 struct sbc_ihl ihl[IRQ_MAX]; 62 63 void *ih; 64 65 u_int32_t bd_ver; 66}; 67 68static int sbc_probe(device_t dev); 69static int sbc_attach(device_t dev); 70static void sbc_intr(void *p); 71 72static struct resource *sbc_alloc_resource(device_t bus, device_t child, int type, int *rid, 73 u_long start, u_long end, u_long count, u_int flags); 74static int sbc_release_resource(device_t bus, device_t child, int type, int rid, 75 struct resource *r); 76static int sbc_setup_intr(device_t dev, device_t child, struct resource *irq, 77 int flags, driver_intr_t *intr, void *arg, 78 void **cookiep); 79static int sbc_teardown_intr(device_t dev, device_t child, struct resource *irq, 80 void *cookie); 81 82static int alloc_resource(struct sbc_softc *scp); 83static int release_resource(struct sbc_softc *scp); 84 85static devclass_t sbc_devclass; 86 87static int io_range[3] = {0x10, 0x2, 0x4}; 88 89static int sb_rd(struct resource *io, int reg); 90static void sb_wr(struct resource *io, int reg, u_int8_t val); 91static int sb_dspready(struct resource *io); 92static int sb_cmd(struct resource *io, u_char val); 93static u_int sb_get_byte(struct resource *io); 94static void sb_setmixer(struct resource *io, u_int port, u_int value); 95 96static int 97sb_rd(struct resource *io, int reg) 98{ 99 return bus_space_read_1(rman_get_bustag(io), 100 rman_get_bushandle(io), 101 reg); 102} 103 104static void 105sb_wr(struct resource *io, int reg, u_int8_t val) 106{ 107 return bus_space_write_1(rman_get_bustag(io), 108 rman_get_bushandle(io), 109 reg, val); 110} 111 112static int 113sb_dspready(struct resource *io) 114{ 115 return ((sb_rd(io, SBDSP_STATUS) & 0x80) == 0); 116} 117 118static int 119sb_dspwr(struct resource *io, u_char val) 120{ 121 int i; 122 123 for (i = 0; i < 1000; i++) { 124 if (sb_dspready(io)) { 125 sb_wr(io, SBDSP_CMD, val); 126 return 1; 127 } 128 if (i > 10) DELAY((i > 100)? 1000 : 10); 129 } 130 printf("sb_dspwr(0x%02x) timed out.\n", val); 131 return 0; 132} 133 134static int 135sb_cmd(struct resource *io, u_char val) 136{ 137 return sb_dspwr(io, val); 138} 139 140static void 141sb_setmixer(struct resource *io, u_int port, u_int value) 142{ 143 u_long flags; 144 145 flags = spltty(); 146 sb_wr(io, SB_MIX_ADDR, (u_char) (port & 0xff)); /* Select register */ 147 DELAY(10); 148 sb_wr(io, SB_MIX_DATA, (u_char) (value & 0xff)); 149 DELAY(10); 150 splx(flags); 151} 152 153static u_int 154sb_get_byte(struct resource *io) 155{ 156 int i; 157 158 for (i = 1000; i > 0; i--) { 159 if (sb_rd(io, DSP_DATA_AVAIL) & 0x80) 160 return sb_rd(io, DSP_READ); 161 else 162 DELAY(20); 163 } 164 return 0xffff; 165} 166 167static int 168sb_reset_dsp(struct resource *io) 169{ 170 sb_wr(io, SBDSP_RST, 3); 171 DELAY(100); 172 sb_wr(io, SBDSP_RST, 0); 173 return (sb_get_byte(io) == 0xAA)? 0 : ENXIO; 174} 175 176static int 177sb_identify_board(struct resource *io) 178{ 179 int ver, essver, rev; 180 181 sb_cmd(io, DSP_CMD_GETVER); /* Get version */ 182 ver = (sb_get_byte(io) << 8) | sb_get_byte(io); 183 if (ver < 0x100 || ver > 0x4ff) return 0; 184 if (ver == 0x0301) { 185 /* Try to detect ESS chips. */ 186 sb_cmd(io, DSP_CMD_GETID); /* Return ident. bytes. */ 187 essver = (sb_get_byte(io) << 8) | sb_get_byte(io); 188 rev = essver & 0x000f; 189 essver &= 0xfff0; 190 if (essver == 0x4880) ver |= 0x1000; 191 else if (essver == 0x6880) ver = 0x0500 | rev; 192 } 193 return ver; 194} 195 196static struct isa_pnp_id sbc_ids[] = { 197 {0x01008c0e, "Creative ViBRA16C"}, /* CTL0001 */ 198 {0x31008c0e, "Creative SB16/SB32"}, /* CTL0031 */ 199 {0x41008c0e, "Creative SB16/SB32"}, /* CTL0041 */ 200 {0x42008c0e, "Creative SB AWE64"}, /* CTL0042 */ 201 {0x43008c0e, "Creative ViBRA16X"}, /* CTL0043 */ 202 {0x44008c0e, "Creative SB AWE64 Gold"}, /* CTL0044 */ 203 {0x45008c0e, "Creative SB AWE64"}, /* CTL0045 */ 204 205 {0x01000000, "Avance Logic ALS100+"}, /* @@@0001 - ViBRA16X clone */ 206 {0x01100000, "Avance Asound 110"}, /* @@@1001 */ 207 {0x01200000, "Avance Logic ALS120"}, /* @@@2001 - ViBRA16X clone */ 208 209 {0x02017316, "ESS ES1688"}, /* ESS1688 */ 210 {0x68187316, "ESS ES1868"}, /* ESS1868 */ 211 {0x69187316, "ESS ES1869"}, /* ESS1869 */ 212 {0xacb0110e, "ESS ES1869 (Compaq OEM)"}, /* CPQb0ac */ 213 {0x78187316, "ESS ES1878"}, /* ESS1878 */ 214 {0x79187316, "ESS ES1879"}, /* ESS1879 */ 215 {0x88187316, "ESS ES1888"}, /* ESS1888 */ 216 {0x07017316, "ESS ES1888 (DEC OEM)"}, /* ESS0107 */ 217 {0} 218}; 219 220static int 221sbc_probe(device_t dev) 222{ 223 char *s = NULL; 224 u_int32_t lid, vid; 225 226 lid = isa_get_logicalid(dev); 227 vid = isa_get_vendorid(dev); 228 if (lid) { 229 if (lid == 0x01000000 && vid != 0x01009305) /* ALS0001 */ 230 return ENXIO; 231 /* Check pnp ids */ 232 return ISA_PNP_PROBE(device_get_parent(dev), dev, sbc_ids); 233 } else { 234 int rid = 0, ver; 235 struct resource *io; 236 237 io = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 238 0, ~0, 16, RF_ACTIVE); 239 if (!io) goto bad; 240 if (sb_reset_dsp(io)) goto bad2; 241 ver = sb_identify_board(io); 242 if (ver == 0) goto bad2; 243 switch ((ver & 0x00000f00) >> 8) { 244 case 1: 245 case 2: 246 s = "Soundblaster"; 247 break; 248 249 case 3: 250 s = (ver & 0x0000f000)? "ESS 488" : "Soundblaster Pro"; 251 break; 252 253 case 4: 254 s = "Soundblaster 16"; 255 break; 256 257 case 5: 258 s = (ver & 0x00000008)? "ESS 688" : "ESS 1688"; 259 break; 260 } 261 if (s) device_set_desc(dev, s); 262bad2: bus_release_resource(dev, SYS_RES_IOPORT, rid, io); 263bad: return s? 0 : ENXIO; 264 } 265} 266 267static int 268sbc_attach(device_t dev) 269{ 270 char *err = NULL; 271 struct sbc_softc *scp; 272 struct sndcard_func *func; 273 device_t child; 274 u_int32_t logical_id = isa_get_logicalid(dev); 275 int flags = device_get_flags(dev); 276 int f, dh, dl, x, irq, i; 277 278 if (!logical_id && (flags & DV_F_DUAL_DMA)) { 279 bus_set_resource(dev, SYS_RES_DRQ, 1, 280 flags & DV_F_DRQ_MASK, 1); 281 } 282 283 scp = device_get_softc(dev); 284 bzero(scp, sizeof(*scp)); 285 scp->dev = dev; 286 err = "alloc_resource"; 287 if (alloc_resource(scp)) goto bad; 288 289 err = "sb_reset_dsp"; 290 if (sb_reset_dsp(scp->io[0])) goto bad; 291 err = "sb_identify_board"; 292 scp->bd_ver = sb_identify_board(scp->io[0]) & 0x00000fff; 293 if (scp->bd_ver == 0) goto bad; 294 f = 0; 295 if (logical_id == 0x01200000 && scp->bd_ver < 0x0400) scp->bd_ver = 0x0499; 296 switch ((scp->bd_ver & 0x0f00) >> 8) { 297 case 1: /* old sound blaster has nothing... */ 298 break; 299 300 case 2: 301 f |= BD_F_DUP_MIDI; 302 if (scp->bd_ver > 0x200) f |= BD_F_MIX_CT1335; 303 break; 304 305 case 5: 306 f |= BD_F_ESS; 307 scp->bd_ver = 0x0301; 308 case 3: 309 f |= BD_F_DUP_MIDI | BD_F_MIX_CT1345; 310 break; 311 312 case 4: 313 f |= BD_F_SB16 | BD_F_MIX_CT1745; 314 if (scp->drq[0]) dl = rman_get_start(scp->drq[0]); else dl = -1; 315 if (scp->drq[1]) dh = rman_get_start(scp->drq[1]); else dh = dl; 316 if (!logical_id && (dh < dl)) { 317 struct resource *r; 318 r = scp->drq[0]; 319 scp->drq[0] = scp->drq[1]; 320 scp->drq[1] = r; 321 dl = rman_get_start(scp->drq[0]); 322 dh = rman_get_start(scp->drq[1]); 323 } 324 /* soft irq/dma configuration */ 325 x = -1; 326 irq = rman_get_start(scp->irq[0]); 327 if (irq == 5) x = 2; 328 else if (irq == 7) x = 4; 329 else if (irq == 9) x = 1; 330 else if (irq == 10) x = 8; 331 if (x == -1) { 332 err = "bad irq (5/7/9/10 valid)"; 333 goto bad; 334 } 335 else sb_setmixer(scp->io[0], IRQ_NR, x); 336 sb_setmixer(scp->io[0], DMA_NR, (1 << dh) | (1 << dl)); 337 device_printf(dev, "setting card to irq %d, drq %d", irq, dl); 338 if (dl != dh) printf(", %d", dh); 339 printf("\n"); 340 break; 341 } 342 343 switch (logical_id) { 344 case 0x43008c0e: /* CTL0043 */ 345 case 0x01200000: 346 case 0x01000000: 347 f |= BD_F_SB16X; 348 break; 349 } 350 scp->bd_ver |= f << 16; 351 352 err = "setup_intr"; 353 for (i = 0; i < IRQ_MAX; i++) { 354 if (bus_setup_intr(dev, scp->irq[i], INTR_TYPE_TTY, sbc_intr, &scp->ihl[i], &scp->ih)) 355 goto bad; 356 } 357 358 /* PCM Audio */ 359 func = malloc(sizeof(struct sndcard_func), M_DEVBUF, M_NOWAIT); 360 if (func == NULL) goto bad; 361 bzero(func, sizeof(*func)); 362 func->func = SCF_PCM; 363 child = device_add_child(dev, "pcm", -1); 364 device_set_ivars(child, func); 365 366#if notyet 367 /* Midi Interface */ 368 func = malloc(sizeof(struct sndcard_func), M_DEVBUF, M_NOWAIT); 369 if (func == NULL) goto bad; 370 bzero(func, sizeof(*func)); 371 func->func = SCF_MIDI; 372 child = device_add_child(dev, "midi", -1); 373 device_set_ivars(child, func); 374 375 /* OPL FM Synthesizer */ 376 func = malloc(sizeof(struct sndcard_func), M_DEVBUF, M_NOWAIT); 377 if (func == NULL) goto bad; 378 bzero(func, sizeof(*func)); 379 func->func = SCF_SYNTH; 380 child = device_add_child(dev, "midi", -1); 381 device_set_ivars(child, func); 382#endif /* notyet */ 383 384 /* probe/attach kids */ 385 bus_generic_attach(dev); 386 387 return (0); 388 389bad: if (err) device_printf(dev, "%s\n", err); 390 release_resource(scp); 391 return (ENXIO); 392} 393 394static void 395sbc_intr(void *p) 396{ 397 struct sbc_ihl *ihl = p; 398 int i; 399 400 i = 0; 401 while (i < INTR_MAX) { 402 if (ihl->intr[i] != NULL) ihl->intr[i](ihl->intr_arg[i]); 403 i++; 404 } 405} 406 407static int 408sbc_setup_intr(device_t dev, device_t child, struct resource *irq, 409 int flags, driver_intr_t *intr, void *arg, 410 void **cookiep) 411{ 412 struct sbc_softc *scp = device_get_softc(dev); 413 struct sbc_ihl *ihl = NULL; 414 int i; 415 416 i = 0; 417 while (i < IRQ_MAX) { 418 if (irq == scp->irq[i]) ihl = &scp->ihl[i]; 419 i++; 420 } 421 if (ihl == NULL) return (EINVAL); 422 i = 0; 423 while (i < INTR_MAX) { 424 if (ihl->intr[i] == NULL) { 425 ihl->intr[i] = intr; 426 ihl->intr_arg[i] = arg; 427 *cookiep = &ihl->intr[i]; 428 return 0; 429 } else i++; 430 } 431 return (EINVAL); 432} 433 434static int 435sbc_teardown_intr(device_t dev, device_t child, struct resource *irq, 436 void *cookie) 437{ 438 struct sbc_softc *scp = device_get_softc(dev); 439 struct sbc_ihl *ihl = NULL; 440 int i; 441 442 i = 0; 443 while (i < IRQ_MAX) { 444 if (irq == scp->irq[i]) ihl = &scp->ihl[i]; 445 i++; 446 } 447 if (ihl == NULL) return (EINVAL); 448 i = 0; 449 while (i < INTR_MAX) { 450 if (cookie == &ihl->intr[i]) { 451 ihl->intr[i] = NULL; 452 ihl->intr_arg[i] = NULL; 453 return 0; 454 } else i++; 455 } 456 return (EINVAL); 457} 458 459static struct resource * 460sbc_alloc_resource(device_t bus, device_t child, int type, int *rid, 461 u_long start, u_long end, u_long count, u_int flags) 462{ 463 struct sbc_softc *scp; 464 int *alloced, rid_max, alloced_max; 465 struct resource **res; 466 467 scp = device_get_softc(bus); 468 switch (type) { 469 case SYS_RES_IOPORT: 470 alloced = scp->io_alloced; 471 res = scp->io; 472 rid_max = IO_MAX - 1; 473 alloced_max = 1; 474 break; 475 case SYS_RES_DRQ: 476 alloced = scp->drq_alloced; 477 res = scp->drq; 478 rid_max = DRQ_MAX - 1; 479 alloced_max = 1; 480 break; 481 case SYS_RES_IRQ: 482 alloced = scp->irq_alloced; 483 res = scp->irq; 484 rid_max = IRQ_MAX - 1; 485 alloced_max = INTR_MAX; /* pcm and mpu may share the irq. */ 486 break; 487 default: 488 return (NULL); 489 } 490 491 if (*rid > rid_max || alloced[*rid] == alloced_max) 492 return (NULL); 493 494 alloced[*rid]++; 495 return (res[*rid]); 496} 497 498static int 499sbc_release_resource(device_t bus, device_t child, int type, int rid, 500 struct resource *r) 501{ 502 struct sbc_softc *scp; 503 int *alloced, rid_max; 504 505 scp = device_get_softc(bus); 506 switch (type) { 507 case SYS_RES_IOPORT: 508 alloced = scp->io_alloced; 509 rid_max = IO_MAX - 1; 510 break; 511 case SYS_RES_DRQ: 512 alloced = scp->drq_alloced; 513 rid_max = DRQ_MAX - 1; 514 break; 515 case SYS_RES_IRQ: 516 alloced = scp->irq_alloced; 517 rid_max = IRQ_MAX - 1; 518 break; 519 default: 520 return (1); 521 } 522 523 if (rid > rid_max || alloced[rid] == 0) 524 return (1); 525 526 alloced[rid]--; 527 return (0); 528} 529 530static int 531sbc_read_ivar(device_t bus, device_t dev, int index, uintptr_t * result) 532{ 533 struct sbc_softc *scp = device_get_softc(bus); 534 struct sndcard_func *func = device_get_ivars(dev); 535 536 switch (index) { 537 case 0: 538 *result = func->func; 539 break; 540 541 case 1: 542 *result = scp->bd_ver; 543 break; 544 545 default: 546 return ENOENT; 547 } 548 549 return 0; 550} 551 552static int 553sbc_write_ivar(device_t bus, device_t dev, 554 int index, uintptr_t value) 555{ 556 switch (index) { 557 case 0: 558 case 1: 559 return EINVAL; 560 561 default: 562 return (ENOENT); 563 } 564} 565 566static int 567alloc_resource(struct sbc_softc *scp) 568{ 569 int i; 570 571 for (i = 0 ; i < IO_MAX ; i++) { 572 if (scp->io[i] == NULL) { 573 scp->io_rid[i] = i; 574 scp->io[i] = bus_alloc_resource(scp->dev, SYS_RES_IOPORT, &scp->io_rid[i], 575 0, ~0, io_range[i], RF_ACTIVE); 576 if (i == 0 && scp->io[i] == NULL) 577 return (1); 578 scp->io_alloced[i] = 0; 579 } 580 } 581 for (i = 0 ; i < DRQ_MAX ; i++) { 582 if (scp->drq[i] == NULL) { 583 scp->drq_rid[i] = i; 584 scp->drq[i] = bus_alloc_resource(scp->dev, SYS_RES_DRQ, &scp->drq_rid[i], 585 0, ~0, 1, RF_ACTIVE); 586 if (i == 0 && scp->drq[i] == NULL) 587 return (1); 588 scp->drq_alloced[i] = 0; 589 } 590 } 591 for (i = 0 ; i < IRQ_MAX ; i++) { 592 if (scp->irq[i] == NULL) { 593 scp->irq_rid[i] = i; 594 scp->irq[i] = bus_alloc_resource(scp->dev, SYS_RES_IRQ, &scp->irq_rid[i], 595 0, ~0, 1, RF_ACTIVE); 596 if (i == 0 && scp->irq[i] == NULL) 597 return (1); 598 scp->irq_alloced[i] = 0; 599 } 600 } 601 return (0); 602} 603 604static int 605release_resource(struct sbc_softc *scp) 606{ 607 int i; 608 609 for (i = 0 ; i < IO_MAX ; i++) { 610 if (scp->io[i] != NULL) { 611 bus_release_resource(scp->dev, SYS_RES_IOPORT, scp->io_rid[i], scp->io[i]); 612 scp->io[i] = NULL; 613 } 614 } 615 for (i = 0 ; i < DRQ_MAX ; i++) { 616 if (scp->drq[i] != NULL) { 617 bus_release_resource(scp->dev, SYS_RES_DRQ, scp->drq_rid[i], scp->drq[i]); 618 scp->drq[i] = NULL; 619 } 620 } 621 for (i = 0 ; i < IRQ_MAX ; i++) { 622 if (scp->irq[i] != NULL) { 623 bus_release_resource(scp->dev, SYS_RES_IRQ, scp->irq_rid[i], scp->irq[i]); 624 scp->irq[i] = NULL; 625 } 626 } 627 return (0); 628} 629 630static device_method_t sbc_methods[] = { 631 /* Device interface */ 632 DEVMETHOD(device_probe, sbc_probe), 633 DEVMETHOD(device_attach, sbc_attach), 634 DEVMETHOD(device_detach, bus_generic_detach), 635 DEVMETHOD(device_shutdown, bus_generic_shutdown), 636 DEVMETHOD(device_suspend, bus_generic_suspend), 637 DEVMETHOD(device_resume, bus_generic_resume), 638 639 /* Bus interface */ 640 DEVMETHOD(bus_read_ivar, sbc_read_ivar), 641 DEVMETHOD(bus_write_ivar, sbc_write_ivar), 642 DEVMETHOD(bus_print_child, bus_generic_print_child), 643 DEVMETHOD(bus_alloc_resource, sbc_alloc_resource), 644 DEVMETHOD(bus_release_resource, sbc_release_resource), 645 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource), 646 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource), 647 DEVMETHOD(bus_setup_intr, sbc_setup_intr), 648 DEVMETHOD(bus_teardown_intr, sbc_teardown_intr), 649 650 { 0, 0 } 651}; 652 653static driver_t sbc_driver = { 654 "sbc", 655 sbc_methods, 656 sizeof(struct sbc_softc), 657}; 658 659/* sbc can be attached to an isa bus. */ 660DRIVER_MODULE(sbc, isa, sbc_driver, sbc_devclass, 0, 0); 661