1 /*- 2 * Copyright (c) 1999 Cameron Grant <cg@freebsd.org> 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 16 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 23 * SUCH DAMAGE. 24 * 25 * $FreeBSD: src/sys/dev/sound/pci/solo.c,v 1.35.2.4 2006/07/13 01:53:54 yongari Exp $ 26 * $DragonFly: src/sys/dev/sound/pci/solo.c,v 1.9 2007/01/04 21:47:02 corecode Exp $ 27 */ 28 29 #include <dev/sound/pcm/sound.h> 30 31 #include <bus/pci/pcireg.h> 32 #include <bus/pci/pcivar.h> 33 34 #include <dev/sound/isa/sb.h> 35 #include <dev/sound/chip.h> 36 37 #include "mixer_if.h" 38 39 SND_DECLARE_FILE("$DragonFly: src/sys/dev/sound/pci/solo.c,v 1.9 2007/01/04 21:47:02 corecode Exp $"); 40 41 #define SOLO_DEFAULT_BUFSZ 16384 42 #define ABS(x) (((x) < 0)? -(x) : (x)) 43 44 /* if defined, playback always uses the 2nd channel and full duplex works */ 45 #define ESS18XX_DUPLEX 1 46 47 /* more accurate clocks and split audio1/audio2 rates */ 48 #define ESS18XX_NEWSPEED 49 50 /* 1 = INTR_MPSAFE, 0 = GIANT */ 51 #define ESS18XX_MPSAFE 1 52 53 static u_int32_t ess_playfmt[] = { 54 AFMT_U8, 55 AFMT_STEREO | AFMT_U8, 56 AFMT_S8, 57 AFMT_STEREO | AFMT_S8, 58 AFMT_S16_LE, 59 AFMT_STEREO | AFMT_S16_LE, 60 AFMT_U16_LE, 61 AFMT_STEREO | AFMT_U16_LE, 62 0 63 }; 64 static struct pcmchan_caps ess_playcaps = {6000, 48000, ess_playfmt, 0}; 65 66 /* 67 * Recording output is byte-swapped 68 */ 69 static u_int32_t ess_recfmt[] = { 70 AFMT_U8, 71 AFMT_STEREO | AFMT_U8, 72 AFMT_S8, 73 AFMT_STEREO | AFMT_S8, 74 AFMT_S16_BE, 75 AFMT_STEREO | AFMT_S16_BE, 76 AFMT_U16_BE, 77 AFMT_STEREO | AFMT_U16_BE, 78 0 79 }; 80 static struct pcmchan_caps ess_reccaps = {6000, 48000, ess_recfmt, 0}; 81 82 struct ess_info; 83 84 struct ess_chinfo { 85 struct ess_info *parent; 86 struct pcm_channel *channel; 87 struct snd_dbuf *buffer; 88 int dir, hwch, stopping; 89 u_int32_t fmt, spd, blksz; 90 }; 91 92 struct ess_info { 93 struct resource *io, *sb, *vc, *mpu, *gp; /* I/O address for the board */ 94 struct resource *irq; 95 void *ih; 96 bus_dma_tag_t parent_dmat; 97 98 int simplex_dir, type, duplex:1, newspeed:1, dmasz[2]; 99 unsigned int bufsz; 100 101 struct ess_chinfo pch, rch; 102 #if ESS18XX_MPSAFE == 1 103 struct spinlock *lock; 104 #endif 105 }; 106 107 #if ESS18XX_MPSAFE == 1 108 #define ess_lock(_ess) snd_mtxlock((_ess)->lock) 109 #define ess_unlock(_ess) snd_mtxunlock((_ess)->lock) 110 #define ess_lock_assert(_ess) snd_mtxassert((_ess)->lock) 111 #else 112 #define ess_lock(_ess) 113 #define ess_unlock(_ess) 114 #define ess_lock_assert(_ess) 115 #endif 116 117 static int ess_rd(struct ess_info *sc, int reg); 118 static void ess_wr(struct ess_info *sc, int reg, u_int8_t val); 119 static int ess_dspready(struct ess_info *sc); 120 static int ess_cmd(struct ess_info *sc, u_char val); 121 static int ess_cmd1(struct ess_info *sc, u_char cmd, int val); 122 static int ess_get_byte(struct ess_info *sc); 123 static void ess_setmixer(struct ess_info *sc, u_int port, u_int value); 124 static int ess_getmixer(struct ess_info *sc, u_int port); 125 static int ess_reset_dsp(struct ess_info *sc); 126 127 static int ess_write(struct ess_info *sc, u_char reg, int val); 128 static int ess_read(struct ess_info *sc, u_char reg); 129 130 static void ess_intr(void *arg); 131 static int ess_setupch(struct ess_info *sc, int ch, int dir, int spd, u_int32_t fmt, int len); 132 static int ess_start(struct ess_chinfo *ch); 133 static int ess_stop(struct ess_chinfo *ch); 134 135 static int ess_dmasetup(struct ess_info *sc, int ch, u_int32_t base, u_int16_t cnt, int dir); 136 static int ess_dmapos(struct ess_info *sc, int ch); 137 static int ess_dmatrigger(struct ess_info *sc, int ch, int go); 138 139 /* 140 * Common code for the midi and pcm functions 141 * 142 * ess_cmd write a single byte to the CMD port. 143 * ess_cmd1 write a CMD + 1 byte arg 144 * ess_cmd2 write a CMD + 2 byte arg 145 * ess_get_byte returns a single byte from the DSP data port 146 * 147 * ess_write is actually ess_cmd1 148 * ess_read access ext. regs via ess_cmd(0xc0, reg) followed by ess_get_byte 149 */ 150 151 static int 152 port_rd(struct resource *port, int regno, int size) 153 { 154 bus_space_tag_t st = rman_get_bustag(port); 155 bus_space_handle_t sh = rman_get_bushandle(port); 156 157 switch (size) { 158 case 1: 159 return bus_space_read_1(st, sh, regno); 160 case 2: 161 return bus_space_read_2(st, sh, regno); 162 case 4: 163 return bus_space_read_4(st, sh, regno); 164 default: 165 return 0xffffffff; 166 } 167 } 168 169 static void 170 port_wr(struct resource *port, int regno, u_int32_t data, int size) 171 { 172 bus_space_tag_t st = rman_get_bustag(port); 173 bus_space_handle_t sh = rman_get_bushandle(port); 174 175 switch (size) { 176 case 1: 177 bus_space_write_1(st, sh, regno, data); 178 break; 179 case 2: 180 bus_space_write_2(st, sh, regno, data); 181 break; 182 case 4: 183 bus_space_write_4(st, sh, regno, data); 184 break; 185 } 186 } 187 188 static int 189 ess_rd(struct ess_info *sc, int reg) 190 { 191 return port_rd(sc->sb, reg, 1); 192 } 193 194 static void 195 ess_wr(struct ess_info *sc, int reg, u_int8_t val) 196 { 197 port_wr(sc->sb, reg, val, 1); 198 } 199 200 static int 201 ess_dspready(struct ess_info *sc) 202 { 203 return ((ess_rd(sc, SBDSP_STATUS) & 0x80) == 0); 204 } 205 206 static int 207 ess_dspwr(struct ess_info *sc, u_char val) 208 { 209 int i; 210 211 for (i = 0; i < 1000; i++) { 212 if (ess_dspready(sc)) { 213 ess_wr(sc, SBDSP_CMD, val); 214 return 1; 215 } 216 if (i > 10) DELAY((i > 100)? 1000 : 10); 217 } 218 kprintf("ess_dspwr(0x%02x) timed out.\n", val); 219 return 0; 220 } 221 222 static int 223 ess_cmd(struct ess_info *sc, u_char val) 224 { 225 DEB(kprintf("ess_cmd: %x\n", val)); 226 return ess_dspwr(sc, val); 227 } 228 229 static int 230 ess_cmd1(struct ess_info *sc, u_char cmd, int val) 231 { 232 DEB(kprintf("ess_cmd1: %x, %x\n", cmd, val)); 233 if (ess_dspwr(sc, cmd)) { 234 return ess_dspwr(sc, val & 0xff); 235 } else return 0; 236 } 237 238 static void 239 ess_setmixer(struct ess_info *sc, u_int port, u_int value) 240 { 241 DEB(kprintf("ess_setmixer: reg=%x, val=%x\n", port, value);) 242 ess_wr(sc, SB_MIX_ADDR, (u_char) (port & 0xff)); /* Select register */ 243 DELAY(10); 244 ess_wr(sc, SB_MIX_DATA, (u_char) (value & 0xff)); 245 DELAY(10); 246 } 247 248 static int 249 ess_getmixer(struct ess_info *sc, u_int port) 250 { 251 int val; 252 253 ess_wr(sc, SB_MIX_ADDR, (u_char) (port & 0xff)); /* Select register */ 254 DELAY(10); 255 val = ess_rd(sc, SB_MIX_DATA); 256 DELAY(10); 257 258 return val; 259 } 260 261 static int 262 ess_get_byte(struct ess_info *sc) 263 { 264 int i; 265 266 for (i = 1000; i > 0; i--) { 267 if (ess_rd(sc, 0xc) & 0x40) 268 return ess_rd(sc, DSP_READ); 269 else 270 DELAY(20); 271 } 272 return -1; 273 } 274 275 static int 276 ess_write(struct ess_info *sc, u_char reg, int val) 277 { 278 return ess_cmd1(sc, reg, val); 279 } 280 281 static int 282 ess_read(struct ess_info *sc, u_char reg) 283 { 284 return (ess_cmd(sc, 0xc0) && ess_cmd(sc, reg))? ess_get_byte(sc) : -1; 285 } 286 287 static int 288 ess_reset_dsp(struct ess_info *sc) 289 { 290 DEB(kprintf("ess_reset_dsp\n")); 291 ess_wr(sc, SBDSP_RST, 3); 292 DELAY(100); 293 ess_wr(sc, SBDSP_RST, 0); 294 if (ess_get_byte(sc) != 0xAA) { 295 DEB(kprintf("ess_reset_dsp failed\n")); 296 /* 297 rman_get_start(d->io_base))); 298 */ 299 return ENXIO; /* Sorry */ 300 } 301 ess_cmd(sc, 0xc6); 302 return 0; 303 } 304 305 static void 306 ess_intr(void *arg) 307 { 308 struct ess_info *sc = (struct ess_info *)arg; 309 int src, pirq = 0, rirq = 0; 310 311 ess_lock(sc); 312 src = 0; 313 if (ess_getmixer(sc, 0x7a) & 0x80) 314 src |= 2; 315 if (ess_rd(sc, 0x0c) & 0x01) 316 src |= 1; 317 318 if (src == 0) { 319 ess_unlock(sc); 320 return; 321 } 322 323 if (sc->duplex) { 324 pirq = (src & sc->pch.hwch)? 1 : 0; 325 rirq = (src & sc->rch.hwch)? 1 : 0; 326 } else { 327 if (sc->simplex_dir == PCMDIR_PLAY) 328 pirq = 1; 329 if (sc->simplex_dir == PCMDIR_REC) 330 rirq = 1; 331 if (!pirq && !rirq) 332 kprintf("solo: IRQ neither playback nor rec!\n"); 333 } 334 335 DEB(kprintf("ess_intr: pirq:%d rirq:%d\n",pirq,rirq)); 336 337 if (pirq) { 338 if (sc->pch.stopping) { 339 ess_dmatrigger(sc, sc->pch.hwch, 0); 340 sc->pch.stopping = 0; 341 if (sc->pch.hwch == 1) 342 ess_write(sc, 0xb8, ess_read(sc, 0xb8) & ~0x01); 343 else 344 ess_setmixer(sc, 0x78, ess_getmixer(sc, 0x78) & ~0x03); 345 } 346 ess_unlock(sc); 347 chn_intr(sc->pch.channel); 348 ess_lock(sc); 349 } 350 351 if (rirq) { 352 if (sc->rch.stopping) { 353 ess_dmatrigger(sc, sc->rch.hwch, 0); 354 sc->rch.stopping = 0; 355 /* XXX: will this stop audio2? */ 356 ess_write(sc, 0xb8, ess_read(sc, 0xb8) & ~0x01); 357 } 358 ess_unlock(sc); 359 chn_intr(sc->rch.channel); 360 ess_lock(sc); 361 } 362 363 if (src & 2) 364 ess_setmixer(sc, 0x7a, ess_getmixer(sc, 0x7a) & ~0x80); 365 if (src & 1) 366 ess_rd(sc, DSP_DATA_AVAIL); 367 368 ess_unlock(sc); 369 } 370 371 /* utility functions for ESS */ 372 static u_int8_t 373 ess_calcspeed8(int *spd) 374 { 375 int speed = *spd; 376 u_int32_t t; 377 378 if (speed > 22000) { 379 t = (795500 + speed / 2) / speed; 380 speed = (795500 + t / 2) / t; 381 t = (256 - t) | 0x80; 382 } else { 383 t = (397700 + speed / 2) / speed; 384 speed = (397700 + t / 2) / t; 385 t = 128 - t; 386 } 387 *spd = speed; 388 return t & 0x000000ff; 389 } 390 391 static u_int8_t 392 ess_calcspeed9(int *spd) 393 { 394 int speed, s0, s1, use0; 395 u_int8_t t0, t1; 396 397 /* rate = source / (256 - divisor) */ 398 /* divisor = 256 - (source / rate) */ 399 speed = *spd; 400 t0 = 128 - (793800 / speed); 401 s0 = 793800 / (128 - t0); 402 403 t1 = 128 - (768000 / speed); 404 s1 = 768000 / (128 - t1); 405 t1 |= 0x80; 406 407 use0 = (ABS(speed - s0) < ABS(speed - s1))? 1 : 0; 408 409 *spd = use0? s0 : s1; 410 return use0? t0 : t1; 411 } 412 413 static u_int8_t 414 ess_calcfilter(int spd) 415 { 416 int cutoff; 417 418 /* cutoff = 7160000 / (256 - divisor) */ 419 /* divisor = 256 - (7160000 / cutoff) */ 420 cutoff = (spd * 9 * 82) / 20; 421 return (256 - (7160000 / cutoff)); 422 } 423 424 static int 425 ess_setupch(struct ess_info *sc, int ch, int dir, int spd, u_int32_t fmt, int len) 426 { 427 int play = (dir == PCMDIR_PLAY)? 1 : 0; 428 int b16 = (fmt & AFMT_16BIT)? 1 : 0; 429 int stereo = (fmt & AFMT_STEREO)? 1 : 0; 430 int unsign = (fmt == AFMT_U8 || fmt == AFMT_U16_LE || fmt == AFMT_U16_BE)? 1 : 0; 431 u_int8_t spdval, fmtval; 432 433 DEB(kprintf("ess_setupch\n")); 434 spdval = (sc->newspeed)? ess_calcspeed9(&spd) : ess_calcspeed8(&spd); 435 436 sc->simplex_dir = play ? PCMDIR_PLAY : PCMDIR_REC ; 437 438 if (ch == 1) { 439 KASSERT((dir == PCMDIR_PLAY) || (dir == PCMDIR_REC), ("ess_setupch: dir1 bad")); 440 len = -len; 441 /* transfer length low */ 442 ess_write(sc, 0xa4, len & 0x00ff); 443 /* transfer length high */ 444 ess_write(sc, 0xa5, (len & 0xff00) >> 8); 445 /* autoinit, dma dir */ 446 ess_write(sc, 0xb8, 0x04 | (play? 0x00 : 0x0a)); 447 /* mono/stereo */ 448 ess_write(sc, 0xa8, (ess_read(sc, 0xa8) & ~0x03) | (stereo? 0x01 : 0x02)); 449 /* demand mode, 4 bytes/xfer */ 450 ess_write(sc, 0xb9, 0x02); 451 /* sample rate */ 452 ess_write(sc, 0xa1, spdval); 453 /* filter cutoff */ 454 ess_write(sc, 0xa2, ess_calcfilter(spd)); 455 /* setup dac/adc */ 456 /* 457 if (play) 458 ess_write(sc, 0xb6, unsign? 0x80 : 0x00); 459 */ 460 /* mono, b16: signed, load signal */ 461 /* 462 ess_write(sc, 0xb7, 0x51 | (unsign? 0x00 : 0x20)); 463 */ 464 /* setup fifo */ 465 ess_write(sc, 0xb7, 0x91 | (unsign? 0x00 : 0x20) | 466 (b16? 0x04 : 0x00) | 467 (stereo? 0x08 : 0x40)); 468 /* irq control */ 469 ess_write(sc, 0xb1, (ess_read(sc, 0xb1) & 0x0f) | 0x50); 470 /* drq control */ 471 ess_write(sc, 0xb2, (ess_read(sc, 0xb2) & 0x0f) | 0x50); 472 } else if (ch == 2) { 473 KASSERT(dir == PCMDIR_PLAY, ("ess_setupch: dir2 bad")); 474 len >>= 1; 475 len = -len; 476 /* transfer length low */ 477 ess_setmixer(sc, 0x74, len & 0x00ff); 478 /* transfer length high */ 479 ess_setmixer(sc, 0x76, (len & 0xff00) >> 8); 480 /* autoinit, 4 bytes/req */ 481 ess_setmixer(sc, 0x78, 0x10); 482 fmtval = b16 | (stereo << 1) | ((!unsign) << 2); 483 /* enable irq, set format */ 484 ess_setmixer(sc, 0x7a, 0x40 | fmtval); 485 if (sc->newspeed) { 486 /* sample rate */ 487 ess_setmixer(sc, 0x70, spdval); 488 /* filter cutoff */ 489 ess_setmixer(sc, 0x72, ess_calcfilter(spd)); 490 } 491 492 } 493 return 0; 494 } 495 static int 496 ess_start(struct ess_chinfo *ch) 497 { 498 struct ess_info *sc = ch->parent; 499 500 DEB(kprintf("ess_start\n");); 501 ess_setupch(sc, ch->hwch, ch->dir, ch->spd, ch->fmt, ch->blksz); 502 ch->stopping = 0; 503 if (ch->hwch == 1) { 504 ess_write(sc, 0xb8, ess_read(sc, 0xb8) | 0x01); 505 if (ch->dir == PCMDIR_PLAY) { 506 #if 0 507 DELAY(100000); /* 100 ms */ 508 #endif 509 ess_cmd(sc, 0xd1); 510 } 511 } else 512 ess_setmixer(sc, 0x78, ess_getmixer(sc, 0x78) | 0x03); 513 return 0; 514 } 515 516 static int 517 ess_stop(struct ess_chinfo *ch) 518 { 519 struct ess_info *sc = ch->parent; 520 521 DEB(kprintf("ess_stop\n")); 522 ch->stopping = 1; 523 if (ch->hwch == 1) 524 ess_write(sc, 0xb8, ess_read(sc, 0xb8) & ~0x04); 525 else 526 ess_setmixer(sc, 0x78, ess_getmixer(sc, 0x78) & ~0x10); 527 DEB(kprintf("done with stop\n")); 528 return 0; 529 } 530 531 /* -------------------------------------------------------------------- */ 532 /* channel interface for ESS18xx */ 533 static void * 534 esschan_init(kobj_t obj, void *devinfo, struct snd_dbuf *b, struct pcm_channel *c, int dir) 535 { 536 struct ess_info *sc = devinfo; 537 struct ess_chinfo *ch = (dir == PCMDIR_PLAY)? &sc->pch : &sc->rch; 538 539 DEB(kprintf("esschan_init\n")); 540 ch->parent = sc; 541 ch->channel = c; 542 ch->buffer = b; 543 ch->dir = dir; 544 if (sndbuf_alloc(ch->buffer, sc->parent_dmat, sc->bufsz) != 0) 545 return NULL; 546 ch->hwch = 1; 547 if ((dir == PCMDIR_PLAY) && (sc->duplex)) 548 ch->hwch = 2; 549 return ch; 550 } 551 552 static int 553 esschan_setformat(kobj_t obj, void *data, u_int32_t format) 554 { 555 struct ess_chinfo *ch = data; 556 557 ch->fmt = format; 558 return 0; 559 } 560 561 static int 562 esschan_setspeed(kobj_t obj, void *data, u_int32_t speed) 563 { 564 struct ess_chinfo *ch = data; 565 struct ess_info *sc = ch->parent; 566 567 ch->spd = speed; 568 if (sc->newspeed) 569 ess_calcspeed9(&ch->spd); 570 else 571 ess_calcspeed8(&ch->spd); 572 return ch->spd; 573 } 574 575 static int 576 esschan_setblocksize(kobj_t obj, void *data, u_int32_t blocksize) 577 { 578 struct ess_chinfo *ch = data; 579 580 ch->blksz = blocksize; 581 return ch->blksz; 582 } 583 584 static int 585 esschan_trigger(kobj_t obj, void *data, int go) 586 { 587 struct ess_chinfo *ch = data; 588 struct ess_info *sc = ch->parent; 589 590 DEB(kprintf("esschan_trigger: %d\n",go)); 591 if (go == PCMTRIG_EMLDMAWR || go == PCMTRIG_EMLDMARD) 592 return 0; 593 594 ess_lock(sc); 595 switch (go) { 596 case PCMTRIG_START: 597 ess_dmasetup(sc, ch->hwch, sndbuf_getbufaddr(ch->buffer), sndbuf_getsize(ch->buffer), ch->dir); 598 ess_dmatrigger(sc, ch->hwch, 1); 599 ess_start(ch); 600 break; 601 602 case PCMTRIG_STOP: 603 case PCMTRIG_ABORT: 604 default: 605 ess_stop(ch); 606 break; 607 } 608 ess_unlock(sc); 609 return 0; 610 } 611 612 static int 613 esschan_getptr(kobj_t obj, void *data) 614 { 615 struct ess_chinfo *ch = data; 616 struct ess_info *sc = ch->parent; 617 int ret; 618 619 ess_lock(sc); 620 ret = ess_dmapos(sc, ch->hwch); 621 ess_unlock(sc); 622 return ret; 623 } 624 625 static struct pcmchan_caps * 626 esschan_getcaps(kobj_t obj, void *data) 627 { 628 struct ess_chinfo *ch = data; 629 630 return (ch->dir == PCMDIR_PLAY)? &ess_playcaps : &ess_reccaps; 631 } 632 633 static kobj_method_t esschan_methods[] = { 634 KOBJMETHOD(channel_init, esschan_init), 635 KOBJMETHOD(channel_setformat, esschan_setformat), 636 KOBJMETHOD(channel_setspeed, esschan_setspeed), 637 KOBJMETHOD(channel_setblocksize, esschan_setblocksize), 638 KOBJMETHOD(channel_trigger, esschan_trigger), 639 KOBJMETHOD(channel_getptr, esschan_getptr), 640 KOBJMETHOD(channel_getcaps, esschan_getcaps), 641 { 0, 0 } 642 }; 643 CHANNEL_DECLARE(esschan); 644 645 /************************************************************/ 646 647 static int 648 essmix_init(struct snd_mixer *m) 649 { 650 struct ess_info *sc = mix_getdevinfo(m); 651 652 mix_setrecdevs(m, SOUND_MASK_CD | SOUND_MASK_MIC | SOUND_MASK_LINE | 653 SOUND_MASK_IMIX); 654 655 mix_setdevs(m, SOUND_MASK_SYNTH | SOUND_MASK_PCM | SOUND_MASK_LINE | 656 SOUND_MASK_MIC | SOUND_MASK_CD | SOUND_MASK_VOLUME | 657 SOUND_MASK_LINE1); 658 659 ess_setmixer(sc, 0, 0); /* reset */ 660 661 return 0; 662 } 663 664 static int 665 essmix_set(struct snd_mixer *m, unsigned dev, unsigned left, unsigned right) 666 { 667 struct ess_info *sc = mix_getdevinfo(m); 668 int preg = 0, rreg = 0, l, r; 669 670 l = (left * 15) / 100; 671 r = (right * 15) / 100; 672 switch (dev) { 673 case SOUND_MIXER_SYNTH: 674 preg = 0x36; 675 rreg = 0x6b; 676 break; 677 678 case SOUND_MIXER_PCM: 679 preg = 0x14; 680 rreg = 0x7c; 681 break; 682 683 case SOUND_MIXER_LINE: 684 preg = 0x3e; 685 rreg = 0x6e; 686 break; 687 688 case SOUND_MIXER_MIC: 689 preg = 0x1a; 690 rreg = 0x68; 691 break; 692 693 case SOUND_MIXER_LINE1: 694 preg = 0x3a; 695 rreg = 0x6c; 696 break; 697 698 case SOUND_MIXER_CD: 699 preg = 0x38; 700 rreg = 0x6a; 701 break; 702 703 case SOUND_MIXER_VOLUME: 704 l = left? (left * 63) / 100 : 64; 705 r = right? (right * 63) / 100 : 64; 706 ess_setmixer(sc, 0x60, l); 707 ess_setmixer(sc, 0x62, r); 708 left = (l == 64)? 0 : (l * 100) / 63; 709 right = (r == 64)? 0 : (r * 100) / 63; 710 return left | (right << 8); 711 } 712 713 if (preg) 714 ess_setmixer(sc, preg, (l << 4) | r); 715 if (rreg) 716 ess_setmixer(sc, rreg, (l << 4) | r); 717 718 left = (l * 100) / 15; 719 right = (r * 100) / 15; 720 721 return left | (right << 8); 722 } 723 724 static int 725 essmix_setrecsrc(struct snd_mixer *m, u_int32_t src) 726 { 727 struct ess_info *sc = mix_getdevinfo(m); 728 u_char recdev; 729 730 switch (src) { 731 case SOUND_MASK_CD: 732 recdev = 0x02; 733 break; 734 735 case SOUND_MASK_LINE: 736 recdev = 0x06; 737 break; 738 739 case SOUND_MASK_IMIX: 740 recdev = 0x05; 741 break; 742 743 case SOUND_MASK_MIC: 744 default: 745 recdev = 0x00; 746 src = SOUND_MASK_MIC; 747 break; 748 } 749 750 ess_setmixer(sc, 0x1c, recdev); 751 752 return src; 753 } 754 755 static kobj_method_t solomixer_methods[] = { 756 KOBJMETHOD(mixer_init, essmix_init), 757 KOBJMETHOD(mixer_set, essmix_set), 758 KOBJMETHOD(mixer_setrecsrc, essmix_setrecsrc), 759 { 0, 0 } 760 }; 761 MIXER_DECLARE(solomixer); 762 763 /************************************************************/ 764 765 static int 766 ess_dmasetup(struct ess_info *sc, int ch, u_int32_t base, u_int16_t cnt, int dir) 767 { 768 KASSERT(ch == 1 || ch == 2, ("bad ch")); 769 sc->dmasz[ch - 1] = cnt; 770 if (ch == 1) { 771 port_wr(sc->vc, 0x8, 0xc4, 1); /* command */ 772 port_wr(sc->vc, 0xd, 0xff, 1); /* reset */ 773 port_wr(sc->vc, 0xf, 0x01, 1); /* mask */ 774 port_wr(sc->vc, 0xb, dir == PCMDIR_PLAY? 0x58 : 0x54, 1); /* mode */ 775 port_wr(sc->vc, 0x0, base, 4); 776 port_wr(sc->vc, 0x4, cnt - 1, 2); 777 778 } else if (ch == 2) { 779 port_wr(sc->io, 0x6, 0x08, 1); /* autoinit */ 780 port_wr(sc->io, 0x0, base, 4); 781 port_wr(sc->io, 0x4, cnt, 2); 782 } 783 return 0; 784 } 785 786 static int 787 ess_dmapos(struct ess_info *sc, int ch) 788 { 789 int p = 0, i = 0, j = 0; 790 791 KASSERT(ch == 1 || ch == 2, ("bad ch")); 792 if (ch == 1) { 793 794 /* 795 * During recording, this register is known to give back 796 * garbage if it's not quiescent while being read. That's 797 * why we spl, stop the DMA, and try over and over until 798 * adjacent reads are "close", in the right order and not 799 * bigger than is otherwise possible. 800 */ 801 ess_dmatrigger(sc, ch, 0); 802 DELAY(20); 803 do { 804 DELAY(10); 805 if (j > 1) 806 kprintf("DMA count reg bogus: %04x & %04x\n", 807 i, p); 808 i = port_rd(sc->vc, 0x4, 2) + 1; 809 p = port_rd(sc->vc, 0x4, 2) + 1; 810 } while ((p > sc->dmasz[ch - 1] || i < p || (p - i) > 0x8) && j++ < 1000); 811 ess_dmatrigger(sc, ch, 1); 812 } 813 else if (ch == 2) 814 p = port_rd(sc->io, 0x4, 2); 815 return sc->dmasz[ch - 1] - p; 816 } 817 818 static int 819 ess_dmatrigger(struct ess_info *sc, int ch, int go) 820 { 821 KASSERT(ch == 1 || ch == 2, ("bad ch")); 822 if (ch == 1) 823 port_wr(sc->vc, 0xf, go? 0x00 : 0x01, 1); /* mask */ 824 else if (ch == 2) 825 port_wr(sc->io, 0x6, 0x08 | (go? 0x02 : 0x00), 1); /* autoinit */ 826 return 0; 827 } 828 829 static void 830 ess_release_resources(struct ess_info *sc, device_t dev) 831 { 832 if (sc->irq) { 833 if (sc->ih) 834 bus_teardown_intr(dev, sc->irq, sc->ih); 835 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->irq); 836 sc->irq = 0; 837 } 838 if (sc->io) { 839 bus_release_resource(dev, SYS_RES_IOPORT, PCIR_BAR(0), sc->io); 840 sc->io = 0; 841 } 842 843 if (sc->sb) { 844 bus_release_resource(dev, SYS_RES_IOPORT, PCIR_BAR(1), sc->sb); 845 sc->sb = 0; 846 } 847 848 if (sc->vc) { 849 bus_release_resource(dev, SYS_RES_IOPORT, PCIR_BAR(2), sc->vc); 850 sc->vc = 0; 851 } 852 853 if (sc->mpu) { 854 bus_release_resource(dev, SYS_RES_IOPORT, PCIR_BAR(3), sc->mpu); 855 sc->mpu = 0; 856 } 857 858 if (sc->gp) { 859 bus_release_resource(dev, SYS_RES_IOPORT, PCIR_BAR(4), sc->gp); 860 sc->gp = 0; 861 } 862 863 if (sc->parent_dmat) { 864 bus_dma_tag_destroy(sc->parent_dmat); 865 sc->parent_dmat = 0; 866 } 867 868 #if ESS18XX_MPSAFE == 1 869 if (sc->lock) { 870 snd_mtxfree(sc->lock); 871 sc->lock = NULL; 872 } 873 #endif 874 875 kfree(sc, M_DEVBUF); 876 } 877 878 static int 879 ess_alloc_resources(struct ess_info *sc, device_t dev) 880 { 881 int rid; 882 883 rid = PCIR_BAR(0); 884 sc->io = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid, RF_ACTIVE); 885 886 rid = PCIR_BAR(1); 887 sc->sb = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid, RF_ACTIVE); 888 889 rid = PCIR_BAR(2); 890 sc->vc = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid, RF_ACTIVE); 891 892 rid = PCIR_BAR(3); 893 sc->mpu = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid, RF_ACTIVE); 894 895 rid = PCIR_BAR(4); 896 sc->gp = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid, RF_ACTIVE); 897 898 rid = 0; 899 sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, 900 RF_ACTIVE | RF_SHAREABLE); 901 902 #if ESS18XX_MPSAFE == 1 903 sc->lock = snd_mtxcreate(device_get_nameunit(dev), "sound softc"); 904 905 return (sc->irq && sc->io && sc->sb && sc->vc && 906 sc->mpu && sc->gp && sc->lock)? 0 : ENXIO; 907 #else 908 return (sc->irq && sc->io && sc->sb && sc->vc && sc->mpu && sc->gp)? 0 : ENXIO; 909 #endif 910 } 911 912 static int 913 ess_probe(device_t dev) 914 { 915 char *s = NULL; 916 u_int32_t subdev; 917 918 subdev = (pci_get_subdevice(dev) << 16) | pci_get_subvendor(dev); 919 switch (pci_get_devid(dev)) { 920 case 0x1969125d: 921 if (subdev == 0x8888125d) 922 s = "ESS Solo-1E"; 923 else if (subdev == 0x1818125d) 924 s = "ESS Solo-1"; 925 else 926 s = "ESS Solo-1 (unknown vendor)"; 927 break; 928 } 929 930 if (s) 931 device_set_desc(dev, s); 932 return s ? BUS_PROBE_DEFAULT : ENXIO; 933 } 934 935 #define ESS_PCI_LEGACYCONTROL 0x40 936 #define ESS_PCI_CONFIG 0x50 937 #define ESS_PCI_DDMACONTROL 0x60 938 939 static int 940 ess_suspend(device_t dev) 941 { 942 return 0; 943 } 944 945 static int 946 ess_resume(device_t dev) 947 { 948 uint16_t ddma; 949 uint32_t data; 950 struct ess_info *sc = pcm_getdevinfo(dev); 951 952 ess_lock(sc); 953 data = pci_read_config(dev, PCIR_COMMAND, 2); 954 data |= PCIM_CMD_PORTEN | PCIM_CMD_BUSMASTEREN; 955 pci_write_config(dev, PCIR_COMMAND, data, 2); 956 data = pci_read_config(dev, PCIR_COMMAND, 2); 957 958 ddma = rman_get_start(sc->vc) | 1; 959 pci_write_config(dev, ESS_PCI_LEGACYCONTROL, 0x805f, 2); 960 pci_write_config(dev, ESS_PCI_DDMACONTROL, ddma, 2); 961 pci_write_config(dev, ESS_PCI_CONFIG, 0, 2); 962 963 if (ess_reset_dsp(sc)) { 964 ess_unlock(sc); 965 goto no; 966 } 967 ess_unlock(sc); 968 if (mixer_reinit(dev)) 969 goto no; 970 ess_lock(sc); 971 if (sc->newspeed) 972 ess_setmixer(sc, 0x71, 0x2a); 973 974 port_wr(sc->io, 0x7, 0xb0, 1); /* enable irqs */ 975 ess_unlock(sc); 976 977 return 0; 978 no: 979 return EIO; 980 } 981 982 static int 983 ess_attach(device_t dev) 984 { 985 struct ess_info *sc; 986 char status[SND_STATUSLEN]; 987 u_int16_t ddma; 988 u_int32_t data; 989 990 sc = (struct ess_info *)kmalloc(sizeof *sc, M_DEVBUF, M_NOWAIT | M_ZERO); 991 if (!sc) 992 return ENXIO; 993 994 data = pci_read_config(dev, PCIR_COMMAND, 2); 995 data |= PCIM_CMD_PORTEN | PCIM_CMD_BUSMASTEREN; 996 pci_write_config(dev, PCIR_COMMAND, data, 2); 997 data = pci_read_config(dev, PCIR_COMMAND, 2); 998 999 if (ess_alloc_resources(sc, dev)) 1000 goto no; 1001 1002 sc->bufsz = pcm_getbuffersize(dev, 4096, SOLO_DEFAULT_BUFSZ, 65536); 1003 1004 ddma = rman_get_start(sc->vc) | 1; 1005 pci_write_config(dev, ESS_PCI_LEGACYCONTROL, 0x805f, 2); 1006 pci_write_config(dev, ESS_PCI_DDMACONTROL, ddma, 2); 1007 pci_write_config(dev, ESS_PCI_CONFIG, 0, 2); 1008 1009 port_wr(sc->io, 0x7, 0xb0, 1); /* enable irqs */ 1010 #ifdef ESS18XX_DUPLEX 1011 sc->duplex = 1; 1012 #else 1013 sc->duplex = 0; 1014 #endif 1015 1016 #ifdef ESS18XX_NEWSPEED 1017 sc->newspeed = 1; 1018 #else 1019 sc->newspeed = 0; 1020 #endif 1021 if (snd_setup_intr(dev, sc->irq, 1022 #if ESS18XX_MPSAFE == 1 1023 INTR_MPSAFE 1024 #else 1025 0 1026 #endif 1027 , ess_intr, sc, &sc->ih)) { 1028 device_printf(dev, "unable to map interrupt\n"); 1029 goto no; 1030 } 1031 1032 if (!sc->duplex) 1033 pcm_setflags(dev, pcm_getflags(dev) | SD_F_SIMPLEX); 1034 1035 #if 0 1036 if (bus_dma_tag_create(/*parent*/NULL, /*alignment*/65536, /*boundary*/0, 1037 #endif 1038 if (bus_dma_tag_create(/*parent*/NULL, /*alignment*/2, /*boundary*/0, 1039 /*lowaddr*/BUS_SPACE_MAXADDR_24BIT, 1040 /*highaddr*/BUS_SPACE_MAXADDR, 1041 /*filter*/NULL, /*filterarg*/NULL, 1042 /*maxsize*/sc->bufsz, /*nsegments*/1, 1043 /*maxsegz*/0x3ffff, 1044 /*flags*/0, 1045 &sc->parent_dmat) != 0) { 1046 device_printf(dev, "unable to create dma tag\n"); 1047 goto no; 1048 } 1049 1050 if (ess_reset_dsp(sc)) 1051 goto no; 1052 1053 if (sc->newspeed) 1054 ess_setmixer(sc, 0x71, 0x2a); 1055 1056 if (mixer_init(dev, &solomixer_class, sc)) 1057 goto no; 1058 1059 ksnprintf(status, SND_STATUSLEN, "at io 0x%lx,0x%lx,0x%lx irq %ld %s", 1060 rman_get_start(sc->io), rman_get_start(sc->sb), rman_get_start(sc->vc), 1061 rman_get_start(sc->irq),PCM_KLDSTRING(snd_solo)); 1062 1063 if (pcm_register(dev, sc, 1, 1)) 1064 goto no; 1065 pcm_addchan(dev, PCMDIR_REC, &esschan_class, sc); 1066 pcm_addchan(dev, PCMDIR_PLAY, &esschan_class, sc); 1067 pcm_setstatus(dev, status); 1068 1069 return 0; 1070 1071 no: 1072 ess_release_resources(sc, dev); 1073 return ENXIO; 1074 } 1075 1076 static int 1077 ess_detach(device_t dev) 1078 { 1079 int r; 1080 struct ess_info *sc; 1081 1082 r = pcm_unregister(dev); 1083 if (r) 1084 return r; 1085 1086 sc = pcm_getdevinfo(dev); 1087 ess_release_resources(sc, dev); 1088 return 0; 1089 } 1090 1091 static device_method_t ess_methods[] = { 1092 /* Device interface */ 1093 DEVMETHOD(device_probe, ess_probe), 1094 DEVMETHOD(device_attach, ess_attach), 1095 DEVMETHOD(device_detach, ess_detach), 1096 DEVMETHOD(device_resume, ess_resume), 1097 DEVMETHOD(device_suspend, ess_suspend), 1098 1099 { 0, 0 } 1100 }; 1101 1102 static driver_t ess_driver = { 1103 "pcm", 1104 ess_methods, 1105 PCM_SOFTC_SIZE, 1106 }; 1107 1108 DRIVER_MODULE(snd_solo, pci, ess_driver, pcm_devclass, 0, 0); 1109 MODULE_DEPEND(snd_solo, sound, SOUND_MINVER, SOUND_PREFVER, SOUND_MAXVER); 1110 MODULE_VERSION(snd_solo, 1); 1111 1112 1113 1114