1 /*- 2 * Copyright (c) 2001 Orion Hodson <O.Hodson@cs.ucl.ac.uk> 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 * This card has the annoying habit of "clicking" when attached and 27 * detached, haven't been able to remedy this with any combination of 28 * muting. 29 * 30 * $FreeBSD: src/sys/dev/sound/pci/vibes.c,v 1.19.2.1 2006/01/24 18:54:22 joel Exp $ 31 */ 32 33 #include <dev/sound/pcm/sound.h> 34 #include <dev/sound/pci/vibes.h> 35 36 #include <bus/pci/pcireg.h> 37 #include <bus/pci/pcivar.h> 38 39 #include "mixer_if.h" 40 41 SND_DECLARE_FILE("$DragonFly: src/sys/dev/sound/pci/vibes.c,v 1.10 2007/01/04 21:47:02 corecode Exp $"); 42 43 /* ------------------------------------------------------------------------- */ 44 /* Constants */ 45 46 #define SV_PCI_ID 0xca005333 47 #define SV_DEFAULT_BUFSZ 16384 48 #define SV_MIN_BLKSZ 128 49 #define SV_INTR_PER_BUFFER 2 50 51 #ifndef DEB 52 #define DEB(x) /* (x) */ 53 #endif 54 55 /* ------------------------------------------------------------------------- */ 56 /* Structures */ 57 58 struct sc_info; 59 60 struct sc_chinfo { 61 struct sc_info *parent; 62 struct pcm_channel *channel; 63 struct snd_dbuf *buffer; 64 u_int32_t fmt, spd; 65 int dir; 66 int dma_active, dma_was_active; 67 }; 68 69 struct sc_info { 70 device_t dev; 71 72 /* DMA buffer allocator */ 73 bus_dma_tag_t parent_dmat; 74 75 /* Enhanced register resources */ 76 struct resource *enh_reg; 77 bus_space_tag_t enh_st; 78 bus_space_handle_t enh_sh; 79 int enh_type; 80 int enh_rid; 81 82 /* DMA configuration */ 83 struct resource *dmaa_reg, *dmac_reg; 84 bus_space_tag_t dmaa_st, dmac_st; 85 bus_space_handle_t dmaa_sh, dmac_sh; 86 int dmaa_type, dmac_type; 87 int dmaa_rid, dmac_rid; 88 89 /* Interrupt resources */ 90 struct resource *irq; 91 int irqid; 92 void *ih; 93 94 /* User configurable buffer size */ 95 unsigned int bufsz; 96 97 struct sc_chinfo rch, pch; 98 u_int8_t rev; 99 }; 100 101 static u_int32_t sc_fmt[] = { 102 AFMT_U8, 103 AFMT_U8 | AFMT_STEREO, 104 AFMT_S16_LE, 105 AFMT_S16_LE | AFMT_STEREO, 106 0 107 }; 108 109 static struct pcmchan_caps sc_caps = {8000, 48000, sc_fmt, 0}; 110 111 /* ------------------------------------------------------------------------- */ 112 /* Register Manipulations */ 113 114 #define sv_direct_set(x, y, z) _sv_direct_set(x, y, z, __LINE__) 115 116 static u_int8_t 117 sv_direct_get(struct sc_info *sc, u_int8_t reg) 118 { 119 return bus_space_read_1(sc->enh_st, sc->enh_sh, reg); 120 } 121 122 static void 123 _sv_direct_set(struct sc_info *sc, u_int8_t reg, u_int8_t val, int line) 124 { 125 u_int8_t n; 126 bus_space_write_1(sc->enh_st, sc->enh_sh, reg, val); 127 128 n = sv_direct_get(sc, reg); 129 if (n != val) { 130 device_printf(sc->dev, "sv_direct_set register 0x%02x %d != %d from line %d\n", reg, n, val, line); 131 } 132 } 133 134 static u_int8_t 135 sv_indirect_get(struct sc_info *sc, u_int8_t reg) 136 { 137 if (reg == SV_REG_FORMAT || reg == SV_REG_ANALOG_PWR) 138 reg |= SV_CM_INDEX_MCE; 139 140 bus_space_write_1(sc->enh_st, sc->enh_sh, SV_CM_INDEX, reg); 141 return bus_space_read_1(sc->enh_st, sc->enh_sh, SV_CM_DATA); 142 } 143 144 #define sv_indirect_set(x, y, z) _sv_indirect_set(x, y, z, __LINE__) 145 146 static void 147 _sv_indirect_set(struct sc_info *sc, u_int8_t reg, u_int8_t val, int line) 148 { 149 if (reg == SV_REG_FORMAT || reg == SV_REG_ANALOG_PWR) 150 reg |= SV_CM_INDEX_MCE; 151 152 bus_space_write_1(sc->enh_st, sc->enh_sh, SV_CM_INDEX, reg); 153 bus_space_write_1(sc->enh_st, sc->enh_sh, SV_CM_DATA, val); 154 155 reg &= ~SV_CM_INDEX_MCE; 156 if (reg != SV_REG_ADC_PLLM) { 157 u_int8_t n; 158 n = sv_indirect_get(sc, reg); 159 if (n != val) { 160 device_printf(sc->dev, "sv_indirect_set register 0x%02x %d != %d line %d\n", reg, n, val, line); 161 } 162 } 163 } 164 165 static void 166 sv_dma_set_config(bus_space_tag_t st, bus_space_handle_t sh, 167 u_int32_t base, u_int32_t count, u_int8_t mode) 168 { 169 bus_space_write_4(st, sh, SV_DMA_ADDR, base); 170 bus_space_write_4(st, sh, SV_DMA_COUNT, count & 0xffffff); 171 bus_space_write_1(st, sh, SV_DMA_MODE, mode); 172 173 DEB(kprintf("base 0x%08x count %5d mode 0x%02x\n", 174 base, count, mode)); 175 } 176 177 static u_int32_t 178 sv_dma_get_count(bus_space_tag_t st, bus_space_handle_t sh) 179 { 180 return bus_space_read_4(st, sh, SV_DMA_COUNT) & 0xffffff; 181 } 182 183 /* ------------------------------------------------------------------------- */ 184 /* Play / Record Common Interface */ 185 186 static void * 187 svchan_init(kobj_t obj, void *devinfo, struct snd_dbuf *b, struct pcm_channel *c, int dir) 188 { 189 struct sc_info *sc = devinfo; 190 struct sc_chinfo *ch; 191 ch = (dir == PCMDIR_PLAY) ? &sc->pch : &sc->rch; 192 193 ch->parent = sc; 194 ch->channel = c; 195 ch->dir = dir; 196 197 if (sndbuf_alloc(b, sc->parent_dmat, sc->bufsz) != 0) { 198 DEB(kprintf("svchan_init failed\n")); 199 return NULL; 200 } 201 ch->buffer = b; 202 ch->fmt = AFMT_U8; 203 ch->spd = DSP_DEFAULT_SPEED; 204 ch->dma_active = ch->dma_was_active = 0; 205 206 return ch; 207 } 208 209 static struct pcmchan_caps * 210 svchan_getcaps(kobj_t obj, void *data) 211 { 212 return &sc_caps; 213 } 214 215 static int 216 svchan_setblocksize(kobj_t obj, void *data, u_int32_t blocksize) 217 { 218 struct sc_chinfo *ch = data; 219 struct sc_info *sc = ch->parent; 220 221 /* user has requested interrupts every blocksize bytes */ 222 RANGE(blocksize, SV_MIN_BLKSZ, sc->bufsz / SV_INTR_PER_BUFFER); 223 sndbuf_resize(ch->buffer, SV_INTR_PER_BUFFER, blocksize); 224 DEB(kprintf("svchan_setblocksize: %d\n", blocksize)); 225 return blocksize; 226 } 227 228 static int 229 svchan_setformat(kobj_t obj, void *data, u_int32_t format) 230 { 231 struct sc_chinfo *ch = data; 232 /* NB Just note format here as setting format register 233 * generates noise if dma channel is inactive. */ 234 ch->fmt = (format & AFMT_STEREO) ? SV_AFMT_STEREO : SV_AFMT_MONO; 235 ch->fmt |= (format & AFMT_16BIT) ? SV_AFMT_S16 : SV_AFMT_U8; 236 return 0; 237 } 238 239 static int 240 svchan_setspeed(kobj_t obj, void *data, u_int32_t speed) 241 { 242 struct sc_chinfo *ch = data; 243 RANGE(speed, 8000, 48000); 244 ch->spd = speed; 245 return speed; 246 } 247 248 /* ------------------------------------------------------------------------- */ 249 /* Recording interface */ 250 251 static int 252 sv_set_recspeed(struct sc_info *sc, u_int32_t speed) 253 { 254 u_int32_t f_out, f_actual; 255 u_int32_t rs, re, r, best_r = 0, r2, t, n, best_n = 0; 256 int32_t m, best_m = 0, ms, me, err, min_err; 257 258 /* This algorithm is a variant described in sonicvibes.pdf 259 * appendix A. This search is marginally more extensive and 260 * results in (nominally) better sample rate matching. */ 261 262 f_out = SV_F_SCALE * speed; 263 min_err = 0x7fffffff; 264 265 /* Find bounds of r to examine, rs <= r <= re */ 266 t = 80000000 / f_out; 267 for (rs = 1; (1 << rs) < t; rs++); 268 269 t = 150000000 / f_out; 270 for (re = 1; (2 << re) < t; re++); 271 if (re > 7) re = 7; 272 273 /* Search over r, n, m */ 274 for (r = rs; r <= re; r++) { 275 r2 = (1 << r); 276 for (n = 3; n < 34; n++) { 277 m = f_out * n / (SV_F_REF / r2); 278 ms = (m > 3) ? (m - 1) : 3; 279 me = (m < 129) ? (m + 1) : 129; 280 for (m = ms; m <= me; m++) { 281 f_actual = m * SV_F_REF / (n * r2); 282 if (f_actual > f_out) { 283 err = f_actual - f_out; 284 } else { 285 err = f_out - f_actual; 286 } 287 if (err < min_err) { 288 best_r = r; 289 best_m = m - 2; 290 best_n = n - 2; 291 min_err = err; 292 if (err == 0) break; 293 } 294 } 295 } 296 } 297 298 sv_indirect_set(sc, SV_REG_ADC_PLLM, best_m); 299 sv_indirect_set(sc, SV_REG_ADC_PLLN, 300 SV_ADC_PLLN(best_n) | SV_ADC_PLLR(best_r)); 301 DEB(kprintf("svrchan_setspeed: %d -> PLLM 0x%02x PLLNR 0x%08x\n", 302 speed, 303 sv_indirect_get(sc, SV_REG_ADC_PLLM), 304 sv_indirect_get(sc, SV_REG_ADC_PLLN))); 305 return 0; 306 } 307 308 static int 309 svrchan_trigger(kobj_t obj, void *data, int go) 310 { 311 struct sc_chinfo *ch = data; 312 struct sc_info *sc = ch->parent; 313 u_int32_t count, enable; 314 u_int8_t v; 315 316 switch(go) { 317 case PCMTRIG_START: 318 /* Set speed */ 319 sv_set_recspeed(sc, ch->spd); 320 321 /* Set format */ 322 v = sv_indirect_get(sc, SV_REG_FORMAT) & ~SV_AFMT_DMAC_MSK; 323 v |= SV_AFMT_DMAC(ch->fmt); 324 sv_indirect_set(sc, SV_REG_FORMAT, v); 325 326 /* Program DMA */ 327 count = sndbuf_getsize(ch->buffer) / 2; /* DMAC uses words */ 328 sv_dma_set_config(sc->dmac_st, sc->dmac_sh, 329 sndbuf_getbufaddr(ch->buffer), 330 count - 1, 331 SV_DMA_MODE_AUTO | SV_DMA_MODE_RD); 332 count = count / SV_INTR_PER_BUFFER - 1; 333 sv_indirect_set(sc, SV_REG_DMAC_COUNT_HI, count >> 8); 334 sv_indirect_set(sc, SV_REG_DMAC_COUNT_LO, count & 0xff); 335 336 /* Enable DMA */ 337 enable = sv_indirect_get(sc, SV_REG_ENABLE) | SV_RECORD_ENABLE; 338 sv_indirect_set(sc, SV_REG_ENABLE, enable); 339 ch->dma_active = 1; 340 break; 341 case PCMTRIG_ABORT: 342 enable = sv_indirect_get(sc, SV_REG_ENABLE) & ~SV_RECORD_ENABLE; 343 sv_indirect_set(sc, SV_REG_ENABLE, enable); 344 ch->dma_active = 0; 345 break; 346 } 347 348 return 0; 349 } 350 351 static int 352 svrchan_getptr(kobj_t obj, void *data) 353 { 354 struct sc_chinfo *ch = data; 355 struct sc_info *sc = ch->parent; 356 u_int32_t sz, remain; 357 358 sz = sndbuf_getsize(ch->buffer); 359 /* DMAC uses words */ 360 remain = (sv_dma_get_count(sc->dmac_st, sc->dmac_sh) + 1) * 2; 361 return sz - remain; 362 } 363 364 static kobj_method_t svrchan_methods[] = { 365 KOBJMETHOD(channel_init, svchan_init), 366 KOBJMETHOD(channel_setformat, svchan_setformat), 367 KOBJMETHOD(channel_setspeed, svchan_setspeed), 368 KOBJMETHOD(channel_setblocksize, svchan_setblocksize), 369 KOBJMETHOD(channel_trigger, svrchan_trigger), 370 KOBJMETHOD(channel_getptr, svrchan_getptr), 371 KOBJMETHOD(channel_getcaps, svchan_getcaps), 372 KOBJMETHOD_END 373 }; 374 CHANNEL_DECLARE(svrchan); 375 376 /* ------------------------------------------------------------------------- */ 377 /* Playback interface */ 378 379 static int 380 svpchan_trigger(kobj_t obj, void *data, int go) 381 { 382 struct sc_chinfo *ch = data; 383 struct sc_info *sc = ch->parent; 384 u_int32_t count, enable, speed; 385 u_int8_t v; 386 387 switch(go) { 388 case PCMTRIG_START: 389 /* Set speed */ 390 speed = (ch->spd * 65536) / 48000; 391 if (speed > 65535) 392 speed = 65535; 393 sv_indirect_set(sc, SV_REG_PCM_SAMPLING_HI, speed >> 8); 394 sv_indirect_set(sc, SV_REG_PCM_SAMPLING_LO, speed & 0xff); 395 396 /* Set format */ 397 v = sv_indirect_get(sc, SV_REG_FORMAT) & ~SV_AFMT_DMAA_MSK; 398 v |= SV_AFMT_DMAA(ch->fmt); 399 sv_indirect_set(sc, SV_REG_FORMAT, v); 400 401 /* Program DMA */ 402 count = sndbuf_getsize(ch->buffer); 403 sv_dma_set_config(sc->dmaa_st, sc->dmaa_sh, 404 sndbuf_getbufaddr(ch->buffer), 405 count - 1, 406 SV_DMA_MODE_AUTO | SV_DMA_MODE_WR); 407 count = count / SV_INTR_PER_BUFFER - 1; 408 sv_indirect_set(sc, SV_REG_DMAA_COUNT_HI, count >> 8); 409 sv_indirect_set(sc, SV_REG_DMAA_COUNT_LO, count & 0xff); 410 411 /* Enable DMA */ 412 enable = sv_indirect_get(sc, SV_REG_ENABLE); 413 enable = (enable | SV_PLAY_ENABLE) & ~SV_PLAYBACK_PAUSE; 414 sv_indirect_set(sc, SV_REG_ENABLE, enable); 415 ch->dma_active = 1; 416 break; 417 case PCMTRIG_ABORT: 418 enable = sv_indirect_get(sc, SV_REG_ENABLE) & ~SV_PLAY_ENABLE; 419 sv_indirect_set(sc, SV_REG_ENABLE, enable); 420 ch->dma_active = 0; 421 break; 422 } 423 424 return 0; 425 } 426 427 static int 428 svpchan_getptr(kobj_t obj, void *data) 429 { 430 struct sc_chinfo *ch = data; 431 struct sc_info *sc = ch->parent; 432 u_int32_t sz, remain; 433 434 sz = sndbuf_getsize(ch->buffer); 435 /* DMAA uses bytes */ 436 remain = sv_dma_get_count(sc->dmaa_st, sc->dmaa_sh) + 1; 437 return (sz - remain); 438 } 439 440 static kobj_method_t svpchan_methods[] = { 441 KOBJMETHOD(channel_init, svchan_init), 442 KOBJMETHOD(channel_setformat, svchan_setformat), 443 KOBJMETHOD(channel_setspeed, svchan_setspeed), 444 KOBJMETHOD(channel_setblocksize, svchan_setblocksize), 445 KOBJMETHOD(channel_trigger, svpchan_trigger), 446 KOBJMETHOD(channel_getptr, svpchan_getptr), 447 KOBJMETHOD(channel_getcaps, svchan_getcaps), 448 KOBJMETHOD_END 449 }; 450 CHANNEL_DECLARE(svpchan); 451 452 /* ------------------------------------------------------------------------- */ 453 /* Mixer support */ 454 455 static const struct sv_mix_props { 456 u_int8_t reg; /* Register */ 457 u_int8_t stereo:1; /* Supports 2 channels */ 458 u_int8_t mute:1; /* Supports muting */ 459 u_int8_t neg:1; /* Negative gain */ 460 u_int8_t max; /* Max gain */ 461 u_int8_t iselect; /* Input selector */ 462 } mt [SOUND_MIXER_NRDEVICES] = { 463 [SOUND_MIXER_LINE1] = {SV_REG_AUX1, 1, 1, 1, SV_DEFAULT_MAX, SV_INPUT_AUX1}, 464 [SOUND_MIXER_CD] = {SV_REG_CD, 1, 1, 1, SV_DEFAULT_MAX, SV_INPUT_CD}, 465 [SOUND_MIXER_LINE] = {SV_REG_LINE, 1, 1, 1, SV_DEFAULT_MAX, SV_INPUT_LINE}, 466 [SOUND_MIXER_MIC] = {SV_REG_MIC, 0, 1, 1, SV_MIC_MAX, SV_INPUT_MIC}, 467 [SOUND_MIXER_SYNTH] = {SV_REG_SYNTH, 0, 1, 1, SV_DEFAULT_MAX, 0}, 468 [SOUND_MIXER_LINE2] = {SV_REG_AUX2, 1, 1, 1, SV_DEFAULT_MAX, SV_INPUT_AUX2}, 469 [SOUND_MIXER_VOLUME] = {SV_REG_MIX, 1, 1, 1, SV_DEFAULT_MAX, 0}, 470 [SOUND_MIXER_PCM] = {SV_REG_PCM, 1, 1, 1, SV_PCM_MAX, 0}, 471 [SOUND_MIXER_RECLEV] = {SV_REG_ADC_INPUT, 1, 0, 0, SV_ADC_MAX, 0}, 472 }; 473 474 static void 475 sv_channel_gain(struct sc_info *sc, u_int32_t dev, u_int32_t gain, u_int32_t channel) 476 { 477 u_int8_t v; 478 int32_t g; 479 480 g = mt[dev].max * gain / 100; 481 if (mt[dev].neg) 482 g = mt[dev].max - g; 483 v = sv_indirect_get(sc, mt[dev].reg + channel) & ~mt[dev].max; 484 v |= g; 485 486 if (mt[dev].mute) { 487 if (gain == 0) { 488 v |= SV_MUTE; 489 } else { 490 v &= ~SV_MUTE; 491 } 492 } 493 sv_indirect_set(sc, mt[dev].reg + channel, v); 494 } 495 496 static int 497 sv_gain(struct sc_info *sc, u_int32_t dev, u_int32_t left, u_int32_t right) 498 { 499 sv_channel_gain(sc, dev, left, 0); 500 if (mt[dev].stereo) 501 sv_channel_gain(sc, dev, right, 1); 502 return 0; 503 } 504 505 static void 506 sv_mix_mute_all(struct sc_info *sc) 507 { 508 int32_t i; 509 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) { 510 if (mt[i].reg) sv_gain(sc, i, 0, 0); 511 } 512 } 513 514 static int 515 sv_mix_init(struct snd_mixer *m) 516 { 517 u_int32_t i, v; 518 519 for(i = v = 0; i < SOUND_MIXER_NRDEVICES; i++) { 520 if (mt[i].max) v |= (1 << i); 521 } 522 mix_setdevs(m, v); 523 524 for(i = v = 0; i < SOUND_MIXER_NRDEVICES; i++) { 525 if (mt[i].iselect) v |= (1 << i); 526 } 527 mix_setrecdevs(m, v); 528 529 return 0; 530 } 531 532 static int 533 sv_mix_set(struct snd_mixer *m, u_int32_t dev, u_int32_t left, u_int32_t right) 534 { 535 struct sc_info *sc = mix_getdevinfo(m); 536 return sv_gain(sc, dev, left, right); 537 } 538 539 static int 540 sv_mix_setrecsrc(struct snd_mixer *m, u_int32_t mask) 541 { 542 struct sc_info *sc = mix_getdevinfo(m); 543 u_int32_t i, v; 544 545 v = sv_indirect_get(sc, SV_REG_ADC_INPUT) & SV_INPUT_GAIN_MASK; 546 for(i = 0; i < SOUND_MIXER_NRDEVICES; i++) { 547 if ((1 << i) & mask) { 548 v |= mt[i].iselect; 549 } 550 } 551 DEB(kprintf("sv_mix_setrecsrc: mask 0x%08x adc_input 0x%02x\n", mask, v)); 552 sv_indirect_set(sc, SV_REG_ADC_INPUT, v); 553 return mask; 554 } 555 556 static kobj_method_t sv_mixer_methods[] = { 557 KOBJMETHOD(mixer_init, sv_mix_init), 558 KOBJMETHOD(mixer_set, sv_mix_set), 559 KOBJMETHOD(mixer_setrecsrc, sv_mix_setrecsrc), 560 KOBJMETHOD_END 561 }; 562 MIXER_DECLARE(sv_mixer); 563 564 /* ------------------------------------------------------------------------- */ 565 /* Power management and reset */ 566 567 static void 568 sv_power(struct sc_info *sc, int state) 569 { 570 u_int8_t v; 571 572 switch (state) { 573 case 0: 574 /* power on */ 575 v = sv_indirect_get(sc, SV_REG_ANALOG_PWR) &~ SV_ANALOG_OFF; 576 v |= SV_ANALOG_OFF_SRS | SV_ANALOG_OFF_SPLL; 577 sv_indirect_set(sc, SV_REG_ANALOG_PWR, v); 578 v = sv_indirect_get(sc, SV_REG_DIGITAL_PWR) &~ SV_DIGITAL_OFF; 579 v |= SV_DIGITAL_OFF_SYN | SV_DIGITAL_OFF_MU | SV_DIGITAL_OFF_GP; 580 sv_indirect_set(sc, SV_REG_DIGITAL_PWR, v); 581 break; 582 default: 583 /* power off */ 584 v = sv_indirect_get(sc, SV_REG_ANALOG_PWR) | SV_ANALOG_OFF; 585 sv_indirect_set(sc, SV_REG_ANALOG_PWR, v); 586 v = sv_indirect_get(sc, SV_REG_DIGITAL_PWR) | SV_DIGITAL_OFF; 587 sv_indirect_set(sc, SV_REG_DIGITAL_PWR, SV_DIGITAL_OFF); 588 break; 589 } 590 DEB(kprintf("Power state %d\n", state)); 591 } 592 593 static int 594 sv_init(struct sc_info *sc) 595 { 596 u_int8_t v; 597 598 /* Effect reset */ 599 v = sv_direct_get(sc, SV_CM_CONTROL) & ~SV_CM_CONTROL_ENHANCED; 600 v |= SV_CM_CONTROL_RESET; 601 sv_direct_set(sc, SV_CM_CONTROL, v); 602 DELAY(50); 603 604 v = sv_direct_get(sc, SV_CM_CONTROL) & ~SV_CM_CONTROL_RESET; 605 sv_direct_set(sc, SV_CM_CONTROL, v); 606 DELAY(50); 607 608 /* Set in enhanced mode */ 609 v = sv_direct_get(sc, SV_CM_CONTROL); 610 v |= SV_CM_CONTROL_ENHANCED; 611 sv_direct_set(sc, SV_CM_CONTROL, v); 612 613 /* Enable interrupts (UDM and MIDM are superfluous) */ 614 v = sv_direct_get(sc, SV_CM_IMR); 615 v &= ~(SV_CM_IMR_AMSK | SV_CM_IMR_CMSK | SV_CM_IMR_SMSK); 616 sv_direct_set(sc, SV_CM_IMR, v); 617 618 /* Select ADC PLL for ADC clock */ 619 v = sv_indirect_get(sc, SV_REG_CLOCK_SOURCE) & ~SV_CLOCK_ALTERNATE; 620 sv_indirect_set(sc, SV_REG_CLOCK_SOURCE, v); 621 622 /* Disable loopback - binds ADC and DAC rates */ 623 v = sv_indirect_get(sc, SV_REG_LOOPBACK) & ~SV_LOOPBACK_ENABLE; 624 sv_indirect_set(sc, SV_REG_LOOPBACK, v); 625 626 /* Disable SRS */ 627 v = sv_indirect_get(sc, SV_REG_SRS_SPACE) | SV_SRS_DISABLED; 628 sv_indirect_set(sc, SV_REG_SRS_SPACE, v); 629 630 /* Get revision */ 631 sc->rev = sv_indirect_get(sc, SV_REG_REVISION); 632 633 return 0; 634 } 635 636 static int 637 sv_suspend(device_t dev) 638 { 639 struct sc_info *sc = pcm_getdevinfo(dev); 640 641 sc->rch.dma_was_active = sc->rch.dma_active; 642 svrchan_trigger(NULL, &sc->rch, PCMTRIG_ABORT); 643 644 sc->pch.dma_was_active = sc->pch.dma_active; 645 svrchan_trigger(NULL, &sc->pch, PCMTRIG_ABORT); 646 647 sv_mix_mute_all(sc); 648 sv_power(sc, 3); 649 650 return 0; 651 } 652 653 static int 654 sv_resume(device_t dev) 655 { 656 struct sc_info *sc = pcm_getdevinfo(dev); 657 658 sv_mix_mute_all(sc); 659 sv_power(sc, 0); 660 if (sv_init(sc) == -1) { 661 device_printf(dev, "unable to reinitialize the card\n"); 662 return ENXIO; 663 } 664 665 if (mixer_reinit(dev) == -1) { 666 device_printf(dev, "unable to reinitialize the mixer\n"); 667 return ENXIO; 668 } 669 670 if (sc->rch.dma_was_active) { 671 svrchan_trigger(0, &sc->rch, PCMTRIG_START); 672 } 673 674 if (sc->pch.dma_was_active) { 675 svpchan_trigger(0, &sc->pch, PCMTRIG_START); 676 } 677 678 return 0; 679 } 680 681 /* ------------------------------------------------------------------------- */ 682 /* Resource related */ 683 684 static void 685 sv_intr(void *data) 686 { 687 struct sc_info *sc = data; 688 u_int8_t status; 689 690 status = sv_direct_get(sc, SV_CM_STATUS); 691 if (status & SV_CM_STATUS_AINT) 692 chn_intr(sc->pch.channel); 693 694 if (status & SV_CM_STATUS_CINT) 695 chn_intr(sc->rch.channel); 696 697 status &= ~(SV_CM_STATUS_AINT|SV_CM_STATUS_CINT); 698 DEB(if (status) kprintf("intr 0x%02x ?\n", status)); 699 700 return; 701 } 702 703 static int 704 sv_probe(device_t dev) 705 { 706 switch(pci_get_devid(dev)) { 707 case SV_PCI_ID: 708 device_set_desc(dev, "S3 Sonicvibes"); 709 return BUS_PROBE_DEFAULT; 710 default: 711 return ENXIO; 712 } 713 } 714 715 static int 716 sv_attach(device_t dev) { 717 struct sc_info *sc; 718 u_int32_t data; 719 char status[SND_STATUSLEN]; 720 u_long midi_start, games_start, count, sdmaa, sdmac, ml, mu; 721 722 sc = kmalloc(sizeof(struct sc_info), M_DEVBUF, M_WAITOK | M_ZERO); 723 sc->dev = dev; 724 725 data = pci_read_config(dev, PCIR_COMMAND, 2); 726 data |= (PCIM_CMD_PORTEN|PCIM_CMD_BUSMASTEREN); 727 pci_write_config(dev, PCIR_COMMAND, data, 2); 728 data = pci_read_config(dev, PCIR_COMMAND, 2); 729 730 if (pci_get_powerstate(dev) != PCI_POWERSTATE_D0) { 731 device_printf(dev, "chip is in D%d power mode " 732 "-- setting to D0\n", pci_get_powerstate(dev)); 733 pci_set_powerstate(dev, PCI_POWERSTATE_D0); 734 } 735 sc->enh_rid = SV_PCI_ENHANCED; 736 sc->enh_type = SYS_RES_IOPORT; 737 sc->enh_reg = bus_alloc_resource(dev, sc->enh_type, 738 &sc->enh_rid, 0, ~0, 739 SV_PCI_ENHANCED_SIZE, RF_ACTIVE); 740 if (sc->enh_reg == NULL) { 741 device_printf(dev, "sv_attach: cannot allocate enh\n"); 742 return ENXIO; 743 } 744 sc->enh_st = rman_get_bustag(sc->enh_reg); 745 sc->enh_sh = rman_get_bushandle(sc->enh_reg); 746 747 data = pci_read_config(dev, SV_PCI_DMAA, 4); 748 DEB(kprintf("sv_attach: initial dmaa 0x%08x\n", data)); 749 data = pci_read_config(dev, SV_PCI_DMAC, 4); 750 DEB(kprintf("sv_attach: initial dmac 0x%08x\n", data)); 751 752 /* Initialize DMA_A and DMA_C */ 753 pci_write_config(dev, SV_PCI_DMAA, SV_PCI_DMA_EXTENDED, 4); 754 pci_write_config(dev, SV_PCI_DMAC, 0, 4); 755 756 /* Register IRQ handler */ 757 sc->irqid = 0; 758 sc->irq = bus_alloc_resource(dev, SYS_RES_IRQ, &sc->irqid, 759 0, ~0, 1, RF_ACTIVE | RF_SHAREABLE); 760 if (!sc->irq || 761 bus_setup_intr(dev, sc->irq, INTR_TYPE_AV, sv_intr, sc, &sc->ih, NULL)) { 762 device_printf(dev, "sv_attach: Unable to map interrupt\n"); 763 goto fail; 764 } 765 766 sc->bufsz = pcm_getbuffersize(dev, 4096, SV_DEFAULT_BUFSZ, 65536); 767 if (bus_dma_tag_create(/*parent*/NULL, /*alignment*/2, /*boundary*/0, 768 /*lowaddr*/BUS_SPACE_MAXADDR_24BIT, 769 /*highaddr*/BUS_SPACE_MAXADDR, 770 /*filter*/NULL, /*filterarg*/NULL, 771 /*maxsize*/sc->bufsz, /*nsegments*/1, 772 /*maxsegz*/0x3ffff, /*flags*/0, 773 &sc->parent_dmat) != 0) { 774 device_printf(dev, "sv_attach: Unable to create dma tag\n"); 775 goto fail; 776 } 777 778 /* Power up and initialize */ 779 sv_mix_mute_all(sc); 780 sv_power(sc, 0); 781 sv_init(sc); 782 783 if (mixer_init(dev, &sv_mixer_class, sc) != 0) { 784 device_printf(dev, "sv_attach: Mixer failed to initialize\n"); 785 goto fail; 786 } 787 788 /* XXX This is a hack, and it's ugly. Okay, the deal is this 789 * card has two more io regions that available for automatic 790 * configuration by the pci code. These need to be allocated 791 * to used as control registers for the DMA engines. 792 * Unfortunately FBSD has no bus_space_foo() functions so we 793 * have to grab port space in region of existing resources. Go 794 * for space between midi and game ports. 795 */ 796 bus_get_resource(dev, SYS_RES_IOPORT, SV_PCI_MIDI, &midi_start, &count); 797 bus_get_resource(dev, SYS_RES_IOPORT, SV_PCI_GAMES, &games_start, &count); 798 799 if (games_start < midi_start) { 800 ml = games_start; 801 mu = midi_start; 802 } else { 803 ml = midi_start; 804 mu = games_start; 805 } 806 /* Check assumptions about space availability and 807 alignment. How driver loaded can determine whether 808 games_start > midi_start or vice versa */ 809 if ((mu - ml >= 0x800) || 810 ((mu - ml) % 0x200)) { 811 device_printf(dev, "sv_attach: resource assumptions not met " 812 "(midi 0x%08lx, games 0x%08lx)\n", 813 midi_start, games_start); 814 goto fail; 815 } 816 817 sdmaa = ml + 0x40; 818 sdmac = sdmaa + 0x40; 819 820 /* Add resources to list of pci resources for this device - from here on 821 * they look like normal pci resources. */ 822 bus_set_resource(dev, SYS_RES_IOPORT, SV_PCI_DMAA, 823 sdmaa, SV_PCI_DMAA_SIZE, -1); 824 bus_set_resource(dev, SYS_RES_IOPORT, SV_PCI_DMAC, 825 sdmac, SV_PCI_DMAC_SIZE, -1); 826 827 /* Cache resource short-cuts for dma_a */ 828 sc->dmaa_rid = SV_PCI_DMAA; 829 sc->dmaa_type = SYS_RES_IOPORT; 830 sc->dmaa_reg = bus_alloc_resource(dev, sc->dmaa_type, 831 &sc->dmaa_rid, 0, ~0, 832 SV_PCI_ENHANCED_SIZE, RF_ACTIVE); 833 if (sc->dmaa_reg == NULL) { 834 device_printf(dev, "sv_attach: cannot allocate dmaa\n"); 835 goto fail; 836 } 837 sc->dmaa_st = rman_get_bustag(sc->dmaa_reg); 838 sc->dmaa_sh = rman_get_bushandle(sc->dmaa_reg); 839 840 /* Poke port into dma_a configuration, nb bit flags to enable dma */ 841 data = pci_read_config(dev, SV_PCI_DMAA, 4) | SV_PCI_DMA_ENABLE | SV_PCI_DMA_EXTENDED; 842 data = ((u_int32_t)sdmaa & 0xfffffff0) | (data & 0x0f); 843 pci_write_config(dev, SV_PCI_DMAA, data, 4); 844 DEB(kprintf("dmaa: 0x%x 0x%x\n", data, pci_read_config(dev, SV_PCI_DMAA, 4))); 845 846 /* Cache resource short-cuts for dma_c */ 847 sc->dmac_rid = SV_PCI_DMAC; 848 sc->dmac_type = SYS_RES_IOPORT; 849 sc->dmac_reg = bus_alloc_resource(dev, sc->dmac_type, 850 &sc->dmac_rid, 0, ~0, 851 SV_PCI_ENHANCED_SIZE, RF_ACTIVE); 852 if (sc->dmac_reg == NULL) { 853 device_printf(dev, "sv_attach: cannot allocate dmac\n"); 854 goto fail; 855 } 856 sc->dmac_st = rman_get_bustag(sc->dmac_reg); 857 sc->dmac_sh = rman_get_bushandle(sc->dmac_reg); 858 859 /* Poke port into dma_c configuration, nb bit flags to enable dma */ 860 data = pci_read_config(dev, SV_PCI_DMAC, 4) | SV_PCI_DMA_ENABLE | SV_PCI_DMA_EXTENDED; 861 data = ((u_int32_t)sdmac & 0xfffffff0) | (data & 0x0f); 862 pci_write_config(dev, SV_PCI_DMAC, data, 4); 863 DEB(kprintf("dmac: 0x%x 0x%x\n", data, pci_read_config(dev, SV_PCI_DMAC, 4))); 864 865 if (bootverbose) 866 kprintf("Sonicvibes: revision %d.\n", sc->rev); 867 868 if (pcm_register(dev, sc, 1, 1)) { 869 device_printf(dev, "sv_attach: pcm_register fail\n"); 870 goto fail; 871 } 872 873 pcm_addchan(dev, PCMDIR_PLAY, &svpchan_class, sc); 874 pcm_addchan(dev, PCMDIR_REC, &svrchan_class, sc); 875 876 ksnprintf(status, SND_STATUSLEN, "at io 0x%lx irq %ld %s", 877 rman_get_start(sc->enh_reg), rman_get_start(sc->irq),PCM_KLDSTRING(snd_vibes)); 878 pcm_setstatus(dev, status); 879 880 DEB(kprintf("sv_attach: succeeded\n")); 881 882 return 0; 883 884 fail: 885 if (sc->parent_dmat) 886 bus_dma_tag_destroy(sc->parent_dmat); 887 if (sc->ih) 888 bus_teardown_intr(dev, sc->irq, sc->ih); 889 if (sc->irq) 890 bus_release_resource(dev, SYS_RES_IRQ, sc->irqid, sc->irq); 891 if (sc->enh_reg) 892 bus_release_resource(dev, sc->enh_type, sc->enh_rid, sc->enh_reg); 893 if (sc->dmaa_reg) 894 bus_release_resource(dev, sc->dmaa_type, sc->dmaa_rid, sc->dmaa_reg); 895 if (sc->dmac_reg) 896 bus_release_resource(dev, sc->dmac_type, sc->dmac_rid, sc->dmac_reg); 897 return ENXIO; 898 } 899 900 static int 901 sv_detach(device_t dev) { 902 struct sc_info *sc; 903 int r; 904 905 r = pcm_unregister(dev); 906 if (r) return r; 907 908 sc = pcm_getdevinfo(dev); 909 sv_mix_mute_all(sc); 910 sv_power(sc, 3); 911 912 bus_dma_tag_destroy(sc->parent_dmat); 913 bus_teardown_intr(dev, sc->irq, sc->ih); 914 bus_release_resource(dev, SYS_RES_IRQ, sc->irqid, sc->irq); 915 bus_release_resource(dev, sc->enh_type, sc->enh_rid, sc->enh_reg); 916 bus_release_resource(dev, sc->dmaa_type, sc->dmaa_rid, sc->dmaa_reg); 917 bus_release_resource(dev, sc->dmac_type, sc->dmac_rid, sc->dmac_reg); 918 919 kfree(sc, M_DEVBUF); 920 921 return 0; 922 } 923 924 static device_method_t sc_methods[] = { 925 DEVMETHOD(device_probe, sv_probe), 926 DEVMETHOD(device_attach, sv_attach), 927 DEVMETHOD(device_detach, sv_detach), 928 DEVMETHOD(device_resume, sv_resume), 929 DEVMETHOD(device_suspend, sv_suspend), 930 DEVMETHOD_END 931 }; 932 933 static driver_t sonicvibes_driver = { 934 "pcm", 935 sc_methods, 936 PCM_SOFTC_SIZE 937 }; 938 939 DRIVER_MODULE(snd_vibes, pci, sonicvibes_driver, pcm_devclass, NULL, NULL); 940 MODULE_DEPEND(snd_vibes, sound, SOUND_MINVER, SOUND_PREFVER, SOUND_MAXVER); 941 MODULE_VERSION(snd_vibes, 1); 942