1 /* $OpenBSD: esa.c,v 1.35 2018/09/14 08:37:34 miko Exp $ */ 2 /* $NetBSD: esa.c,v 1.12 2002/03/24 14:17:35 jmcneill Exp $ */ 3 4 /* 5 * Copyright (c) 2001, 2002 Jared D. McNeill <jmcneill@invisible.ca> 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. The name of the author may not be used to endorse or promote products 14 * derived from this software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 /* 30 * Shamelessly stolen from NetBSD who based it on FreeBSD's who in turn 31 * based it on Linux's driver. What a wonderful world. 32 * 33 * 34 * ESS Allegro-1 / Maestro3 Audio Driver 35 * 36 * Based on the FreeBSD maestro3 driver and the NetBSD eap driver. 37 * Original driver by Don Kim. 38 * 39 * The list management code could possibly be written better, but what 40 * we have right now does the job nicely. Thanks to Zach Brown <zab@zabbo.net> 41 * and Andrew MacDonald <amac@epsilon.yi.org> for helping me debug the 42 * problems with the original list management code present in the Linux 43 * driver. 44 */ 45 46 #include <sys/errno.h> 47 #include <sys/param.h> 48 #include <sys/systm.h> 49 #include <sys/malloc.h> 50 #include <sys/device.h> 51 #include <sys/conf.h> 52 #include <sys/exec.h> 53 #include <sys/selinfo.h> 54 #include <sys/audioio.h> 55 56 #include <machine/bus.h> 57 #include <machine/intr.h> 58 59 #include <dev/pci/pcidevs.h> 60 #include <dev/pci/pcivar.h> 61 62 #include <dev/audio_if.h> 63 #include <dev/ic/ac97.h> 64 65 #include <dev/pci/esareg.h> 66 #include <dev/pci/esavar.h> 67 #include <dev/microcode/esa/esadsp.h> 68 69 #define PCI_CBIO 0x10 70 71 #define ESA_DAC_DATA 0x1100 72 73 enum { 74 ESS_ALLEGRO1, 75 ESS_MAESTRO3 76 }; 77 78 static struct esa_card_type { 79 u_int16_t pci_vendor_id; 80 u_int16_t pci_product_id; 81 int type; 82 int delay1, delay2; 83 } esa_card_types[] = { 84 { PCI_VENDOR_ESSTECH, PCI_PRODUCT_ESSTECH_ES1989, 85 ESS_ALLEGRO1, 50, 800 }, 86 { PCI_VENDOR_ESSTECH, PCI_PRODUCT_ESSTECH_MAESTRO3, 87 ESS_MAESTRO3, 20, 500 }, 88 { PCI_VENDOR_ESSTECH, PCI_PRODUCT_ESSTECH_MAESTRO3_2, 89 ESS_MAESTRO3, 20, 500 }, 90 { 0, 0, 0, 0, 0 } 91 }; 92 93 int esa_match(struct device *, void *, void *); 94 void esa_attach(struct device *, struct device *, void *); 95 int esa_detach(struct device *, int); 96 int esa_activate(struct device *, int); 97 98 /* audio(9) functions */ 99 int esa_open(void *, int); 100 void esa_close(void *); 101 int esa_set_params(void *, int, int, struct audio_params *, 102 struct audio_params *); 103 int esa_round_blocksize(void *, int); 104 int esa_commit_settings(void *); 105 int esa_halt_output(void *); 106 int esa_halt_input(void *); 107 int esa_set_port(void *, mixer_ctrl_t *); 108 int esa_get_port(void *, mixer_ctrl_t *); 109 int esa_query_devinfo(void *, mixer_devinfo_t *); 110 void * esa_malloc(void *, int, size_t, int, int); 111 void esa_free(void *, void *, int); 112 size_t esa_round_buffersize(void *, int, size_t); 113 int esa_get_props(void *); 114 int esa_trigger_output(void *, void *, void *, int, 115 void (*)(void *), void *, 116 struct audio_params *); 117 int esa_trigger_input(void *, void *, void *, int, 118 void (*)(void *), void *, 119 struct audio_params *); 120 121 int esa_intr(void *); 122 int esa_allocmem(struct esa_softc *, size_t, size_t, 123 struct esa_dma *); 124 int esa_freemem(struct esa_softc *, struct esa_dma *); 125 126 /* Supporting subroutines */ 127 u_int16_t esa_read_assp(struct esa_softc *, u_int16_t, u_int16_t); 128 void esa_write_assp(struct esa_softc *, u_int16_t, u_int16_t, 129 u_int16_t); 130 int esa_init_codec(struct esa_softc *); 131 int esa_attach_codec(void *, struct ac97_codec_if *); 132 int esa_read_codec(void *, u_int8_t, u_int16_t *); 133 int esa_write_codec(void *, u_int8_t, u_int16_t); 134 void esa_reset_codec(void *); 135 enum ac97_host_flags esa_flags_codec(void *); 136 int esa_wait(struct esa_softc *); 137 int esa_init(struct esa_softc *); 138 void esa_config(struct esa_softc *); 139 u_int8_t esa_assp_halt(struct esa_softc *); 140 void esa_codec_reset(struct esa_softc *); 141 int esa_amp_enable(struct esa_softc *); 142 void esa_enable_interrupts(struct esa_softc *); 143 u_int32_t esa_get_pointer(struct esa_softc *, struct esa_channel *); 144 145 /* list management */ 146 int esa_add_list(struct esa_voice *, struct esa_list *, u_int16_t, 147 int); 148 void esa_remove_list(struct esa_voice *, struct esa_list *, int); 149 150 /* power management */ 151 void esa_suspend(struct esa_softc *); 152 void esa_resume(struct esa_softc *); 153 154 struct audio_hw_if esa_hw_if = { 155 esa_open, 156 esa_close, 157 esa_set_params, 158 esa_round_blocksize, 159 esa_commit_settings, 160 NULL, /* init_output */ 161 NULL, /* init_input */ 162 NULL, /* start_output */ 163 NULL, /* start_input */ 164 esa_halt_output, 165 esa_halt_input, 166 NULL, /* speaker_ctl */ 167 NULL, /* getfd */ 168 esa_set_port, 169 esa_get_port, 170 esa_query_devinfo, 171 esa_malloc, 172 esa_free, 173 esa_round_buffersize, 174 esa_get_props, 175 esa_trigger_output, 176 esa_trigger_input 177 }; 178 179 struct cfdriver esa_cd = { 180 NULL, "esa", DV_DULL 181 }; 182 183 struct cfattach esa_ca = { 184 sizeof(struct esa_softc), esa_match, esa_attach, 185 esa_detach, esa_activate 186 }; 187 188 /* 189 * audio(9) functions 190 */ 191 192 int 193 esa_open(void *hdl, int flags) 194 { 195 196 return (0); 197 } 198 199 void 200 esa_close(void *hdl) 201 { 202 203 return; 204 } 205 206 int 207 esa_set_params(void *hdl, int setmode, int usemode, struct audio_params *play, 208 struct audio_params *rec) 209 { 210 struct esa_voice *vc = hdl; 211 struct esa_channel *ch; 212 struct audio_params *p; 213 int mode; 214 215 for (mode = AUMODE_RECORD; mode != -1; 216 mode = (mode == AUMODE_RECORD) ? AUMODE_PLAY : -1) { 217 if ((setmode & mode) == 0) 218 continue; 219 220 switch (mode) { 221 case AUMODE_PLAY: 222 p = play; 223 ch = &vc->play; 224 break; 225 case AUMODE_RECORD: 226 p = rec; 227 ch = &vc->rec; 228 break; 229 } 230 231 if (p->sample_rate < ESA_MINRATE) 232 p->sample_rate = ESA_MINRATE; 233 if (p->sample_rate > ESA_MAXRATE) 234 p->sample_rate = ESA_MAXRATE; 235 if (p->precision > 16) 236 p->precision = 16; 237 if (p->channels > 2) 238 p->channels = 2; 239 240 switch(p->encoding) { 241 case AUDIO_ENCODING_SLINEAR_LE: 242 if (p->precision != 16) 243 return EINVAL; 244 break; 245 case AUDIO_ENCODING_ULINEAR_LE: 246 case AUDIO_ENCODING_ULINEAR_BE: 247 if (p->precision != 8) 248 return EINVAL; 249 break; 250 default: 251 return (EINVAL); 252 } 253 p->bps = AUDIO_BPS(p->precision); 254 p->msb = 1; 255 256 ch->mode = *p; 257 } 258 259 return (0); 260 } 261 262 int 263 esa_commit_settings(void *hdl) 264 { 265 struct esa_voice *vc = hdl; 266 struct esa_softc *sc = (struct esa_softc *)vc->parent; 267 struct audio_params *p = &vc->play.mode; 268 struct audio_params *r = &vc->rec.mode; 269 u_int32_t data; 270 u_int32_t freq; 271 int data_bytes = (((ESA_MINISRC_TMP_BUFFER_SIZE & ~1) + 272 (ESA_MINISRC_IN_BUFFER_SIZE & ~1) + 273 (ESA_MINISRC_OUT_BUFFER_SIZE & ~1) + 4) + 255) 274 &~ 255; 275 276 /* playback */ 277 vc->play.data_offset = ESA_DAC_DATA + (data_bytes * vc->index); 278 if (p->channels == 1) 279 data = 1; 280 else 281 data = 0; 282 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, 283 vc->play.data_offset + ESA_SRC3_MODE_OFFSET, 284 data); 285 if (p->precision == 8) 286 data = 1; 287 else 288 data = 0; 289 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, 290 vc->play.data_offset + ESA_SRC3_WORD_LENGTH_OFFSET, 291 data); 292 if ((freq = ((p->sample_rate << 15) + 24000) / 48000) != 0) { 293 freq--; 294 } 295 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, 296 vc->play.data_offset + ESA_CDATA_FREQUENCY, freq); 297 298 /* recording */ 299 vc->rec.data_offset = ESA_DAC_DATA + (data_bytes * vc->index) + 300 (data_bytes / 2); 301 if (r->channels == 1) 302 data = 1; 303 else 304 data = 0; 305 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, 306 vc->rec.data_offset + ESA_SRC3_MODE_OFFSET, 307 data); 308 if (r->precision == 8) 309 data = 1; 310 else 311 data = 0; 312 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, 313 vc->rec.data_offset + ESA_SRC3_WORD_LENGTH_OFFSET, 314 data); 315 if ((freq = ((r->sample_rate << 15) + 24000) / 48000) != 0) { 316 freq--; 317 } 318 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, 319 vc->rec.data_offset + ESA_CDATA_FREQUENCY, freq); 320 321 return (0); 322 }; 323 324 int 325 esa_round_blocksize(void *hdl, int bs) 326 { 327 struct esa_voice *vc = hdl; 328 329 /* 330 * Surely there has to be a better solution... 331 */ 332 vc->play.blksize = vc->rec.blksize = 4096; 333 334 return (vc->play.blksize); 335 } 336 337 int 338 esa_halt_output(void *hdl) 339 { 340 struct esa_voice *vc = hdl; 341 struct esa_softc *sc = (struct esa_softc *)vc->parent; 342 bus_space_tag_t iot = sc->sc_iot; 343 bus_space_handle_t ioh = sc->sc_ioh; 344 u_int16_t data; 345 346 if (vc->play.active == 0) 347 return (0); 348 349 mtx_enter(&audio_lock); 350 vc->play.active = 0; 351 352 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, 353 ESA_CDATA_INSTANCE_READY + vc->play.data_offset, 0); 354 355 sc->sc_ntimers--; 356 if (sc->sc_ntimers == 0) { 357 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, 358 ESA_KDATA_TIMER_COUNT_RELOAD, 0); 359 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, 360 ESA_KDATA_TIMER_COUNT_CURRENT, 0); 361 data = bus_space_read_2(iot, ioh, ESA_HOST_INT_CTRL); 362 bus_space_write_2(iot, ioh, ESA_HOST_INT_CTRL, 363 data & ~ESA_CLKRUN_GEN_ENABLE); 364 } 365 366 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, 367 ESA_KDATA_MIXER_TASK_NUMBER, 368 sc->mixer_list.indexmap[vc->index]); 369 /* remove ourselves from the packed lists */ 370 esa_remove_list(vc, &sc->mixer_list, vc->index); 371 esa_remove_list(vc, &sc->dma_list, vc->index); 372 esa_remove_list(vc, &sc->msrc_list, vc->index); 373 mtx_leave(&audio_lock); 374 return (0); 375 } 376 377 int 378 esa_halt_input(void *hdl) 379 { 380 struct esa_voice *vc = hdl; 381 struct esa_softc *sc = (struct esa_softc *)vc->parent; 382 bus_space_tag_t iot = sc->sc_iot; 383 bus_space_handle_t ioh = sc->sc_ioh; 384 u_int32_t data; 385 386 if (vc->rec.active == 0) 387 return (0); 388 389 mtx_enter(&audio_lock); 390 vc->rec.active = 0; 391 392 sc->sc_ntimers--; 393 if (sc->sc_ntimers == 0) { 394 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, 395 ESA_KDATA_TIMER_COUNT_RELOAD, 0); 396 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, 397 ESA_KDATA_TIMER_COUNT_CURRENT, 0); 398 data = bus_space_read_2(iot, ioh, ESA_HOST_INT_CTRL); 399 bus_space_write_2(iot, ioh, ESA_HOST_INT_CTRL, 400 data & ~ESA_CLKRUN_GEN_ENABLE); 401 } 402 403 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, vc->rec.data_offset + 404 ESA_CDATA_INSTANCE_READY, 0); 405 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, ESA_KDATA_ADC1_REQUEST, 406 0); 407 408 /* remove ourselves from the packed lists */ 409 esa_remove_list(vc, &sc->adc1_list, vc->index + ESA_NUM_VOICES); 410 esa_remove_list(vc, &sc->dma_list, vc->index + ESA_NUM_VOICES); 411 esa_remove_list(vc, &sc->msrc_list, vc->index + ESA_NUM_VOICES); 412 mtx_leave(&audio_lock); 413 return (0); 414 } 415 416 void * 417 esa_malloc(void *hdl, int direction, size_t size, int type, int flags) 418 { 419 struct esa_voice *vc = hdl; 420 struct esa_softc *sc = (struct esa_softc *)vc->parent; 421 struct esa_dma *p; 422 int error; 423 424 p = malloc(sizeof(*p), type, flags); 425 if (!p) 426 return (0); 427 error = esa_allocmem(sc, size, 16, p); 428 if (error) { 429 free(p, type, 0); 430 printf("%s: esa_malloc: not enough memory\n", 431 sc->sc_dev.dv_xname); 432 return (0); 433 } 434 p->next = vc->dma; 435 vc->dma = p; 436 437 return (KERNADDR(p)); 438 } 439 440 void 441 esa_free(void *hdl, void *addr, int type) 442 { 443 struct esa_voice *vc = hdl; 444 struct esa_softc *sc = (struct esa_softc *)vc->parent; 445 struct esa_dma *p; 446 struct esa_dma **pp; 447 448 for (pp = &vc->dma; (p = *pp) != NULL; pp = &p->next) 449 if (KERNADDR(p) == addr) { 450 esa_freemem(sc, p); 451 *pp = p->next; 452 free(p, type, 0); 453 return; 454 } 455 } 456 457 int 458 esa_set_port(void *hdl, mixer_ctrl_t *mc) 459 { 460 struct esa_voice *vc = hdl; 461 struct esa_softc *sc = (struct esa_softc *)vc->parent; 462 463 return (sc->codec_if->vtbl->mixer_set_port(sc->codec_if, mc)); 464 } 465 466 int 467 esa_get_port(void *hdl, mixer_ctrl_t *mc) 468 { 469 struct esa_voice *vc = hdl; 470 struct esa_softc *sc = (struct esa_softc *)vc->parent; 471 472 return (sc->codec_if->vtbl->mixer_get_port(sc->codec_if, mc)); 473 } 474 475 int 476 esa_query_devinfo(void *hdl, mixer_devinfo_t *di) 477 { 478 struct esa_voice *vc = hdl; 479 struct esa_softc *sc = (struct esa_softc *)vc->parent; 480 481 return (sc->codec_if->vtbl->query_devinfo(sc->codec_if, di)); 482 } 483 484 size_t 485 esa_round_buffersize(void *hdl, int direction, size_t bufsize) 486 { 487 struct esa_voice *vc = hdl; 488 489 /* 490 * We must be able to do better than this... 491 */ 492 vc->play.bufsize = vc->rec.bufsize = 65536; 493 494 return (vc->play.bufsize); 495 } 496 497 int 498 esa_get_props(void *hdl) 499 { 500 501 return (AUDIO_PROP_MMAP | AUDIO_PROP_INDEPENDENT | AUDIO_PROP_FULLDUPLEX); 502 } 503 504 int 505 esa_trigger_output(void *hdl, void *start, void *end, int blksize, 506 void (*intr)(void *), void *intrarg, 507 struct audio_params *param) 508 { 509 struct esa_voice *vc = hdl; 510 struct esa_softc *sc = (struct esa_softc *)vc->parent; 511 struct esa_dma *p; 512 bus_space_tag_t iot = sc->sc_iot; 513 bus_space_handle_t ioh = sc->sc_ioh; 514 u_int32_t data; 515 u_int32_t bufaddr; 516 u_int32_t i; 517 size_t size; 518 519 int data_bytes = (((ESA_MINISRC_TMP_BUFFER_SIZE & ~1) + 520 (ESA_MINISRC_IN_BUFFER_SIZE & ~1) + 521 (ESA_MINISRC_OUT_BUFFER_SIZE & ~1) + 4) + 255) 522 &~ 255; 523 int dac_data = ESA_DAC_DATA + (data_bytes * vc->index); 524 int dsp_in_size = ESA_MINISRC_IN_BUFFER_SIZE - (0x20 * 2); 525 int dsp_out_size = ESA_MINISRC_OUT_BUFFER_SIZE - (0x20 * 2); 526 int dsp_in_buf = dac_data + (ESA_MINISRC_TMP_BUFFER_SIZE / 2); 527 int dsp_out_buf = dsp_in_buf + (dsp_in_size / 2) + 1; 528 529 if (vc->play.active) 530 return (EINVAL); 531 532 for (p = vc->dma; p && KERNADDR(p) != start; p = p->next) 533 ; 534 if (!p) { 535 printf("%s: esa_trigger_output: bad addr %p\n", 536 sc->sc_dev.dv_xname, start); 537 return (EINVAL); 538 } 539 540 vc->play.active = 1; 541 vc->play.intr = intr; 542 vc->play.arg = intrarg; 543 vc->play.pos = 0; 544 vc->play.count = 0; 545 vc->play.buf = start; 546 size = (size_t)(((caddr_t)end - (caddr_t)start)); 547 bufaddr = DMAADDR(p); 548 vc->play.start = bufaddr; 549 550 #define LO(x) ((x) & 0x0000ffff) 551 #define HI(x) ((x) >> 16) 552 553 mtx_enter(&audio_lock); 554 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data + 555 ESA_CDATA_HOST_SRC_ADDRL, LO(bufaddr)); 556 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data + 557 ESA_CDATA_HOST_SRC_ADDRH, HI(bufaddr)); 558 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data + 559 ESA_CDATA_HOST_SRC_END_PLUS_1L, LO(bufaddr + size)); 560 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data + 561 ESA_CDATA_HOST_SRC_END_PLUS_1H, HI(bufaddr + size)); 562 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data + 563 ESA_CDATA_HOST_SRC_CURRENTL, LO(bufaddr)); 564 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data + 565 ESA_CDATA_HOST_SRC_CURRENTH, HI(bufaddr)); 566 567 /* DSP buffers */ 568 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data + 569 ESA_CDATA_IN_BUF_BEGIN, dsp_in_buf); 570 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data + 571 ESA_CDATA_IN_BUF_END_PLUS_1, dsp_in_buf + (dsp_in_size / 2)); 572 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data + 573 ESA_CDATA_IN_BUF_HEAD, dsp_in_buf); 574 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data + 575 ESA_CDATA_IN_BUF_TAIL, dsp_in_buf); 576 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data + 577 ESA_CDATA_OUT_BUF_BEGIN, dsp_out_buf); 578 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data + 579 ESA_CDATA_OUT_BUF_END_PLUS_1, dsp_out_buf + (dsp_out_size / 2)); 580 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data + 581 ESA_CDATA_OUT_BUF_HEAD, dsp_out_buf); 582 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data + 583 ESA_CDATA_OUT_BUF_TAIL, dsp_out_buf); 584 585 /* Some per-client initializers */ 586 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data + 587 ESA_SRC3_DIRECTION_OFFSET + 12, dac_data + 40 + 8); 588 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data + 589 ESA_SRC3_DIRECTION_OFFSET + 19, 0x400 + ESA_MINISRC_COEF_LOC); 590 /* Enable or disable low-pass filter? (0xff if rate > 45000) */ 591 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data + 592 ESA_SRC3_DIRECTION_OFFSET + 22, 593 vc->play.mode.sample_rate > 45000 ? 0xff : 0); 594 /* Tell it which way DMA is going */ 595 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data + 596 ESA_CDATA_DMA_CONTROL, 597 ESA_DMACONTROL_AUTOREPEAT + ESA_DMAC_PAGE3_SELECTOR + 598 ESA_DMAC_BLOCKF_SELECTOR); 599 600 /* Set an armload of static initializers */ 601 for (i = 0; i < nitems(esa_playvals); i++) 602 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data + 603 esa_playvals[i].addr, esa_playvals[i].val); 604 605 /* Put us in the packed task lists */ 606 esa_add_list(vc, &sc->msrc_list, dac_data >> ESA_DP_SHIFT_COUNT, 607 vc->index); 608 esa_add_list(vc, &sc->dma_list, dac_data >> ESA_DP_SHIFT_COUNT, 609 vc->index); 610 esa_add_list(vc, &sc->mixer_list, dac_data >> ESA_DP_SHIFT_COUNT, 611 vc->index); 612 #undef LO 613 #undef HI 614 615 sc->sc_ntimers++; 616 617 if (sc->sc_ntimers == 1) { 618 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, 619 ESA_KDATA_TIMER_COUNT_RELOAD, 240); 620 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, 621 ESA_KDATA_TIMER_COUNT_CURRENT, 240); 622 data = bus_space_read_2(iot, ioh, ESA_HOST_INT_CTRL); 623 bus_space_write_2(iot, ioh, ESA_HOST_INT_CTRL, 624 data | ESA_CLKRUN_GEN_ENABLE); 625 } 626 627 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data + 628 ESA_CDATA_INSTANCE_READY, 1); 629 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, 630 ESA_KDATA_MIXER_TASK_NUMBER, 631 sc->mixer_list.indexmap[vc->index]); 632 mtx_leave(&audio_lock); 633 return (0); 634 } 635 636 int 637 esa_trigger_input(void *hdl, void *start, void *end, int blksize, 638 void (*intr)(void *), void *intrarg, 639 struct audio_params *param) 640 { 641 struct esa_voice *vc = hdl; 642 struct esa_softc *sc = (struct esa_softc *)vc->parent; 643 struct esa_dma *p; 644 bus_space_tag_t iot = sc->sc_iot; 645 bus_space_handle_t ioh = sc->sc_ioh; 646 u_int32_t data; 647 u_int32_t bufaddr; 648 u_int32_t i; 649 size_t size; 650 int data_bytes = (((ESA_MINISRC_TMP_BUFFER_SIZE & ~1) + 651 (ESA_MINISRC_IN_BUFFER_SIZE & ~1) + 652 (ESA_MINISRC_OUT_BUFFER_SIZE & ~1) + 4) + 255) 653 &~ 255; 654 int adc_data = ESA_DAC_DATA + (data_bytes * vc->index) + 655 (data_bytes / 2); 656 int dsp_in_size = ESA_MINISRC_IN_BUFFER_SIZE - (0x10 * 2); 657 int dsp_out_size = ESA_MINISRC_OUT_BUFFER_SIZE - (0x10 * 2); 658 int dsp_in_buf = adc_data + (ESA_MINISRC_TMP_BUFFER_SIZE / 2); 659 int dsp_out_buf = dsp_in_buf + (dsp_in_size / 2) + 1; 660 vc->rec.data_offset = adc_data; 661 662 /* We only support 1 recording channel */ 663 if (vc->index > 0) 664 return (ENODEV); 665 666 if (vc->rec.active) 667 return (EINVAL); 668 669 for (p = vc->dma; p && KERNADDR(p) != start; p = p->next) 670 ; 671 if (!p) { 672 printf("%s: esa_trigger_input: bad addr %p\n", 673 sc->sc_dev.dv_xname, start); 674 return (EINVAL); 675 } 676 677 vc->rec.active = 1; 678 vc->rec.intr = intr; 679 vc->rec.arg = intrarg; 680 vc->rec.pos = 0; 681 vc->rec.count = 0; 682 vc->rec.buf = start; 683 size = (size_t)(((caddr_t)end - (caddr_t)start)); 684 bufaddr = DMAADDR(p); 685 vc->rec.start = bufaddr; 686 687 #define LO(x) ((x) & 0x0000ffff) 688 #define HI(x) ((x) >> 16) 689 mtx_enter(&audio_lock); 690 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data + 691 ESA_CDATA_HOST_SRC_ADDRL, LO(bufaddr)); 692 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data + 693 ESA_CDATA_HOST_SRC_ADDRH, HI(bufaddr)); 694 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data + 695 ESA_CDATA_HOST_SRC_END_PLUS_1L, LO(bufaddr + size)); 696 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data + 697 ESA_CDATA_HOST_SRC_END_PLUS_1H, HI(bufaddr + size)); 698 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data + 699 ESA_CDATA_HOST_SRC_CURRENTL, LO(bufaddr)); 700 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data + 701 ESA_CDATA_HOST_SRC_CURRENTH, HI(bufaddr)); 702 703 /* DSP buffers */ 704 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data + 705 ESA_CDATA_IN_BUF_BEGIN, dsp_in_buf); 706 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data + 707 ESA_CDATA_IN_BUF_END_PLUS_1, dsp_in_buf + (dsp_in_size / 2)); 708 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data + 709 ESA_CDATA_IN_BUF_HEAD, dsp_in_buf); 710 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data + 711 ESA_CDATA_IN_BUF_TAIL, dsp_in_buf); 712 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data + 713 ESA_CDATA_OUT_BUF_BEGIN, dsp_out_buf); 714 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data + 715 ESA_CDATA_OUT_BUF_END_PLUS_1, dsp_out_buf + (dsp_out_size / 2)); 716 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data + 717 ESA_CDATA_OUT_BUF_HEAD, dsp_out_buf); 718 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data + 719 ESA_CDATA_OUT_BUF_TAIL, dsp_out_buf); 720 721 /* Some per-client initializers */ 722 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data + 723 ESA_SRC3_DIRECTION_OFFSET + 12, adc_data + 40 + 8); 724 /* Tell it which way DMA is going */ 725 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data + 726 ESA_CDATA_DMA_CONTROL, 727 ESA_DMACONTROL_DIRECTION + ESA_DMACONTROL_AUTOREPEAT + 728 ESA_DMAC_PAGE3_SELECTOR + ESA_DMAC_BLOCKF_SELECTOR); 729 730 /* Set an armload of static initializers */ 731 for (i = 0; i < nitems(esa_recvals); i++) 732 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data + 733 esa_recvals[i].addr, esa_recvals[i].val); 734 735 /* Put us in the packed task lists */ 736 esa_add_list(vc, &sc->adc1_list, adc_data >> ESA_DP_SHIFT_COUNT, 737 vc->index + ESA_NUM_VOICES); 738 esa_add_list(vc, &sc->msrc_list, adc_data >> ESA_DP_SHIFT_COUNT, 739 vc->index + ESA_NUM_VOICES); 740 esa_add_list(vc, &sc->dma_list, adc_data >> ESA_DP_SHIFT_COUNT, 741 vc->index + ESA_NUM_VOICES); 742 #undef LO 743 #undef HI 744 745 sc->sc_ntimers++; 746 if (sc->sc_ntimers == 1) { 747 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, 748 ESA_KDATA_TIMER_COUNT_RELOAD, 240); 749 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, 750 ESA_KDATA_TIMER_COUNT_CURRENT, 240); 751 data = bus_space_read_2(iot, ioh, ESA_HOST_INT_CTRL); 752 bus_space_write_2(iot, ioh, ESA_HOST_INT_CTRL, 753 data | ESA_CLKRUN_GEN_ENABLE); 754 } 755 756 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data + 757 ESA_CDATA_INSTANCE_READY, 1); 758 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, ESA_KDATA_ADC1_REQUEST, 759 1); 760 mtx_leave(&audio_lock); 761 return (0); 762 } 763 764 /* Interrupt handler */ 765 766 int 767 esa_intr(void *hdl) 768 { 769 struct esa_softc *sc = hdl; 770 struct esa_voice *vc; 771 bus_space_tag_t iot = sc->sc_iot; 772 bus_space_handle_t ioh = sc->sc_ioh; 773 u_int8_t status, ctl; 774 u_int32_t pos; 775 u_int32_t diff; 776 u_int32_t play_blksize, play_bufsize; 777 u_int32_t rec_blksize, rec_bufsize; 778 int i, claimed = 0; 779 780 mtx_enter(&audio_lock); 781 status = bus_space_read_1(iot, ioh, ESA_HOST_INT_STATUS); 782 if (status == 0xff) { 783 mtx_leave(&audio_lock); 784 return (0); 785 } 786 787 /* ack the interrupt */ 788 bus_space_write_1(iot, ioh, ESA_HOST_INT_STATUS, status); 789 790 if (status & ESA_HV_INT_PENDING) { 791 u_int8_t event; 792 793 printf("%s: hardware volume interrupt\n", sc->sc_dev.dv_xname); 794 event = bus_space_read_1(iot, ioh, ESA_HW_VOL_COUNTER_MASTER); 795 switch(event) { 796 case 0x99: 797 case 0xaa: 798 case 0x66: 799 case 0x88: 800 printf("%s: esa_intr: FIXME\n", sc->sc_dev.dv_xname); 801 break; 802 default: 803 printf("%s: unknown hwvol event 0x%02x\n", 804 sc->sc_dev.dv_xname, event); 805 break; 806 } 807 bus_space_write_1(iot, ioh, ESA_HW_VOL_COUNTER_MASTER, 0x88); 808 claimed = 1; 809 } 810 811 if (status & ESA_ASSP_INT_PENDING) { 812 ctl = bus_space_read_1(iot, ioh, ESA_ASSP_CONTROL_B); 813 if (!(ctl & ESA_STOP_ASSP_CLOCK)) { 814 ctl = bus_space_read_1(iot, ioh, 815 ESA_ASSP_HOST_INT_STATUS); 816 if (ctl & ESA_DSP2HOST_REQ_TIMER) { 817 bus_space_write_1(iot, ioh, 818 ESA_ASSP_HOST_INT_STATUS, 819 ESA_DSP2HOST_REQ_TIMER); 820 for (i = 0; i < ESA_NUM_VOICES; i++) { 821 vc = &sc->voice[i]; 822 if (vc->play.active) { 823 play_blksize = vc->play.blksize; 824 play_bufsize = vc->play.bufsize; 825 pos = esa_get_pointer(sc, &vc->play) 826 % play_bufsize; 827 diff = (play_bufsize + pos - vc->play.pos) 828 % play_bufsize; 829 vc->play.pos = pos; 830 vc->play.count += diff; 831 while(vc->play.count >= play_blksize) { 832 vc->play.count -= play_blksize; 833 (*vc->play.intr)(vc->play.arg); 834 } 835 } 836 if (vc->rec.active) { 837 rec_blksize = vc->rec.blksize; 838 rec_bufsize = vc->rec.bufsize; 839 pos = esa_get_pointer(sc, &vc->rec) 840 % rec_bufsize; 841 diff = (rec_bufsize + pos - vc->rec.pos) 842 % rec_bufsize; 843 vc->rec.pos = pos; 844 vc->rec.count += diff; 845 while(vc->rec.count >= rec_blksize) { 846 vc->rec.count -= rec_blksize; 847 (*vc->rec.intr)(vc->rec.arg); 848 } 849 } 850 } 851 } 852 } 853 claimed = 1; 854 } 855 mtx_leave(&audio_lock); 856 return (claimed); 857 } 858 859 int 860 esa_allocmem(struct esa_softc *sc, size_t size, size_t align, 861 struct esa_dma *p) 862 { 863 int error; 864 865 p->size = size; 866 error = bus_dmamem_alloc(sc->sc_dmat, p->size, align, 0, 867 p->segs, sizeof(p->segs) / sizeof(p->segs[0]), 868 &p->nsegs, BUS_DMA_NOWAIT); 869 if (error) 870 return (error); 871 872 error = bus_dmamem_map(sc->sc_dmat, p->segs, p->nsegs, p->size, 873 &p->addr, BUS_DMA_NOWAIT | BUS_DMA_COHERENT); 874 if (error) 875 goto free; 876 877 error = bus_dmamap_create(sc->sc_dmat, p->size, 1, p->size, 0, 878 BUS_DMA_NOWAIT, &p->map); 879 if (error) 880 goto unmap; 881 882 error = bus_dmamap_load(sc->sc_dmat, p->map, p->addr, p->size, NULL, 883 BUS_DMA_NOWAIT); 884 if (error) 885 goto destroy; 886 887 return (0); 888 889 destroy: 890 bus_dmamap_destroy(sc->sc_dmat, p->map); 891 unmap: 892 bus_dmamem_unmap(sc->sc_dmat, p->addr, p->size); 893 free: 894 bus_dmamem_free(sc->sc_dmat, p->segs, p->nsegs); 895 896 return (error); 897 } 898 899 int 900 esa_freemem(struct esa_softc *sc, struct esa_dma *p) 901 { 902 903 bus_dmamap_unload(sc->sc_dmat, p->map); 904 bus_dmamap_destroy(sc->sc_dmat, p->map); 905 bus_dmamem_unmap(sc->sc_dmat, p->addr, p->size); 906 bus_dmamem_free(sc->sc_dmat, p->segs, p->nsegs); 907 908 return (0); 909 } 910 911 /* 912 * Supporting Subroutines 913 */ 914 const struct pci_matchid esa_devices[] = { 915 { PCI_VENDOR_ESSTECH, PCI_PRODUCT_ESSTECH_ES1989 }, 916 { PCI_VENDOR_ESSTECH, PCI_PRODUCT_ESSTECH_MAESTRO3 }, 917 { PCI_VENDOR_ESSTECH, PCI_PRODUCT_ESSTECH_MAESTRO3_2 }, 918 }; 919 920 int 921 esa_match(struct device *dev, void *match, void *aux) 922 { 923 return (pci_matchbyid((struct pci_attach_args *)aux, esa_devices, 924 nitems(esa_devices))); 925 } 926 927 void 928 esa_attach(struct device *parent, struct device *self, void *aux) 929 { 930 struct esa_softc *sc = (struct esa_softc *)self; 931 struct pci_attach_args *pa = (struct pci_attach_args *)aux; 932 pcitag_t tag = pa->pa_tag; 933 pci_chipset_tag_t pc = pa->pa_pc; 934 pci_intr_handle_t ih; 935 struct esa_card_type *card; 936 const char *intrstr; 937 int i, len; 938 939 for (card = esa_card_types; card->pci_vendor_id; card++) 940 if (PCI_VENDOR(pa->pa_id) == card->pci_vendor_id && 941 PCI_PRODUCT(pa->pa_id) == card->pci_product_id) { 942 sc->type = card->type; 943 sc->delay1 = card->delay1; 944 sc->delay2 = card->delay2; 945 break; 946 } 947 948 /* Map I/O register */ 949 if (pci_mapreg_map(pa, PCI_CBIO, PCI_MAPREG_TYPE_IO, 0, 950 &sc->sc_iot, &sc->sc_ioh, &sc->sc_iob, &sc->sc_ios, 0)) { 951 printf(": can't map i/o space\n"); 952 return; 953 } 954 955 /* Initialize softc */ 956 sc->sc_tag = tag; 957 sc->sc_pct = pc; 958 sc->sc_dmat = pa->pa_dmat; 959 960 /* Map and establish an interrupt */ 961 if (pci_intr_map(pa, &ih)) { 962 printf(": can't map interrupt\n"); 963 bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios); 964 return; 965 } 966 intrstr = pci_intr_string(pc, ih); 967 sc->sc_ih = pci_intr_establish(pc, ih, IPL_AUDIO | IPL_MPSAFE, 968 esa_intr, self, sc->sc_dev.dv_xname); 969 if (sc->sc_ih == NULL) { 970 printf(": can't establish interrupt"); 971 if (intrstr != NULL) 972 printf(" at %s", intrstr); 973 printf("\n"); 974 bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios); 975 return; 976 } 977 printf(": %s\n", intrstr); 978 979 /* Power up chip */ 980 pci_set_powerstate(pc, tag, PCI_PMCSR_STATE_D0); 981 982 /* Init chip */ 983 if (esa_init(sc) == -1) { 984 printf("%s: esa_attach: unable to initialize the card\n", 985 sc->sc_dev.dv_xname); 986 pci_intr_disestablish(pc, sc->sc_ih); 987 bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios); 988 return; 989 } 990 991 /* create suspend save area */ 992 len = sizeof(u_int16_t) * (ESA_REV_B_CODE_MEMORY_LENGTH 993 + ESA_REV_B_DATA_MEMORY_LENGTH + 1); 994 sc->savemem = malloc(len, M_DEVBUF, M_NOWAIT | M_ZERO); 995 if (sc->savemem == NULL) { 996 printf("%s: unable to allocate suspend buffer\n", 997 sc->sc_dev.dv_xname); 998 pci_intr_disestablish(pc, sc->sc_ih); 999 bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios); 1000 return; 1001 } 1002 1003 /* 1004 * Every card I've seen has had their channels swapped with respect 1005 * to the mixer. Ie: 1006 * $ mixerctl -w outputs.master=0,191 1007 * Would result in the _right_ speaker being turned off. 1008 * 1009 * So, we will swap the left and right mixer channels to compensate 1010 * for this. 1011 */ 1012 sc->codec_flags |= AC97_HOST_SWAPPED_CHANNELS; 1013 sc->codec_flags |= AC97_HOST_DONT_READ; 1014 1015 /* Attach AC97 host interface */ 1016 sc->host_if.arg = self; 1017 sc->host_if.attach = esa_attach_codec; 1018 sc->host_if.read = esa_read_codec; 1019 sc->host_if.write = esa_write_codec; 1020 sc->host_if.reset = esa_reset_codec; 1021 sc->host_if.flags = esa_flags_codec; 1022 1023 if (ac97_attach(&sc->host_if) != 0) { 1024 pci_intr_disestablish(pc, sc->sc_ih); 1025 bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios); 1026 free(sc->savemem, M_DEVBUF, 0); 1027 return; 1028 } 1029 1030 /* initialize list management structures */ 1031 sc->mixer_list.mem_addr = ESA_KDATA_MIXER_XFER0; 1032 sc->mixer_list.max = ESA_MAX_VIRTUAL_MIXER_CHANNELS; 1033 sc->adc1_list.mem_addr = ESA_KDATA_ADC1_XFER0; 1034 sc->adc1_list.max = ESA_MAX_VIRTUAL_ADC1_CHANNELS; 1035 sc->dma_list.mem_addr = ESA_KDATA_DMA_XFER0; 1036 sc->dma_list.max = ESA_MAX_VIRTUAL_DMA_CHANNELS; 1037 sc->msrc_list.mem_addr = ESA_KDATA_INSTANCE0_MINISRC; 1038 sc->msrc_list.max = ESA_MAX_INSTANCE_MINISRC; 1039 1040 /* initialize index maps */ 1041 for (i = 0; i < ESA_NUM_VOICES * 2; i++) { 1042 sc->mixer_list.indexmap[i] = -1; 1043 sc->msrc_list.indexmap[i] = -1; 1044 sc->dma_list.indexmap[i] = -1; 1045 sc->adc1_list.indexmap[i] = -1; 1046 } 1047 for (i = 0; i < ESA_NUM_VOICES; i++) { 1048 sc->voice[i].parent = (struct device *)sc; 1049 sc->voice[i].index = i; 1050 sc->sc_audiodev[i] = 1051 audio_attach_mi(&esa_hw_if, &sc->voice[i], &sc->sc_dev); 1052 } 1053 } 1054 1055 int 1056 esa_detach(struct device *self, int flags) 1057 { 1058 struct esa_softc *sc = (struct esa_softc *)self; 1059 int i; 1060 1061 for (i = 0; i < ESA_NUM_VOICES; i++) { 1062 if (sc->sc_audiodev[i] != NULL) 1063 config_detach(sc->sc_audiodev[i], flags); 1064 } 1065 1066 if (sc->sc_ih != NULL) 1067 pci_intr_disestablish(sc->sc_pct, sc->sc_ih); 1068 if (sc->sc_ios) 1069 bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios); 1070 1071 free(sc->savemem, M_DEVBUF, 0); 1072 1073 return (0); 1074 } 1075 1076 u_int16_t 1077 esa_read_assp(struct esa_softc *sc, u_int16_t region, u_int16_t index) 1078 { 1079 u_int16_t data; 1080 bus_space_tag_t iot = sc->sc_iot; 1081 bus_space_handle_t ioh = sc->sc_ioh; 1082 1083 bus_space_write_2(iot, ioh, ESA_DSP_PORT_MEMORY_TYPE, 1084 region & ESA_MEMTYPE_MASK); 1085 bus_space_write_2(iot, ioh, ESA_DSP_PORT_MEMORY_INDEX, index); 1086 data = bus_space_read_2(iot, ioh, ESA_DSP_PORT_MEMORY_DATA); 1087 1088 return (data); 1089 } 1090 1091 void 1092 esa_write_assp(struct esa_softc *sc, u_int16_t region, u_int16_t index, 1093 u_int16_t data) 1094 { 1095 bus_space_tag_t iot = sc->sc_iot; 1096 bus_space_handle_t ioh = sc->sc_ioh; 1097 1098 bus_space_write_2(iot, ioh, ESA_DSP_PORT_MEMORY_TYPE, 1099 region & ESA_MEMTYPE_MASK); 1100 bus_space_write_2(iot, ioh, ESA_DSP_PORT_MEMORY_INDEX, index); 1101 bus_space_write_2(iot, ioh, ESA_DSP_PORT_MEMORY_DATA, data); 1102 1103 return; 1104 } 1105 1106 int 1107 esa_init_codec(struct esa_softc *sc) 1108 { 1109 bus_space_tag_t iot = sc->sc_iot; 1110 bus_space_handle_t ioh = sc->sc_ioh; 1111 u_int32_t data; 1112 1113 data = bus_space_read_1(iot, ioh, ESA_CODEC_COMMAND); 1114 1115 return ((data & 0x1) ? 0 : 1); 1116 } 1117 1118 int 1119 esa_attach_codec(void *aux, struct ac97_codec_if *codec_if) 1120 { 1121 struct esa_softc *sc = aux; 1122 1123 sc->codec_if = codec_if; 1124 1125 return (0); 1126 } 1127 1128 int 1129 esa_read_codec(void *aux, u_int8_t reg, u_int16_t *result) 1130 { 1131 struct esa_softc *sc = aux; 1132 bus_space_tag_t iot = sc->sc_iot; 1133 bus_space_handle_t ioh = sc->sc_ioh; 1134 1135 if (esa_wait(sc)) 1136 printf("%s: esa_read_codec: timed out\n", sc->sc_dev.dv_xname); 1137 bus_space_write_1(iot, ioh, ESA_CODEC_COMMAND, (reg & 0x7f) | 0x80); 1138 delay(50); 1139 if (esa_wait(sc)) 1140 printf("%s: esa_read_codec: timed out\n", sc->sc_dev.dv_xname); 1141 *result = bus_space_read_2(iot, ioh, ESA_CODEC_DATA); 1142 1143 return (0); 1144 } 1145 1146 int 1147 esa_write_codec(void *aux, u_int8_t reg, u_int16_t data) 1148 { 1149 struct esa_softc *sc = aux; 1150 bus_space_tag_t iot = sc->sc_iot; 1151 bus_space_handle_t ioh = sc->sc_ioh; 1152 1153 if (esa_wait(sc)) { 1154 printf("%s: esa_write_codec: timed out\n", sc->sc_dev.dv_xname); 1155 return (-1); 1156 } 1157 bus_space_write_2(iot, ioh, ESA_CODEC_DATA, data); 1158 bus_space_write_1(iot, ioh, ESA_CODEC_COMMAND, reg & 0x7f); 1159 delay(50); 1160 1161 return (0); 1162 } 1163 1164 void 1165 esa_reset_codec(void *aux) 1166 { 1167 1168 return; 1169 } 1170 1171 enum ac97_host_flags 1172 esa_flags_codec(void *aux) 1173 { 1174 struct esa_softc *sc = aux; 1175 1176 return (sc->codec_flags); 1177 } 1178 1179 int 1180 esa_wait(struct esa_softc *sc) 1181 { 1182 int i, val; 1183 bus_space_tag_t iot = sc->sc_iot; 1184 bus_space_handle_t ioh = sc->sc_ioh; 1185 1186 for (i = 0; i < 20; i++) { 1187 val = bus_space_read_1(iot, ioh, ESA_CODEC_STATUS); 1188 if ((val & 1) == 0) 1189 return (0); 1190 delay(2); 1191 } 1192 1193 return (-1); 1194 } 1195 1196 int 1197 esa_init(struct esa_softc *sc) 1198 { 1199 struct esa_voice *vc; 1200 bus_space_tag_t iot = sc->sc_iot; 1201 bus_space_handle_t ioh = sc->sc_ioh; 1202 pcitag_t tag = sc->sc_tag; 1203 pci_chipset_tag_t pc = sc->sc_pct; 1204 u_int32_t data, i, size; 1205 u_int8_t reset_state; 1206 int data_bytes = (((ESA_MINISRC_TMP_BUFFER_SIZE & ~1) + 1207 (ESA_MINISRC_IN_BUFFER_SIZE & ~1) + 1208 (ESA_MINISRC_OUT_BUFFER_SIZE & ~1) + 4) + 255) 1209 &~ 255; 1210 1211 /* Disable legacy emulation */ 1212 data = pci_conf_read(pc, tag, PCI_LEGACY_AUDIO_CTRL); 1213 data |= DISABLE_LEGACY; 1214 pci_conf_write(pc, tag, PCI_LEGACY_AUDIO_CTRL, data); 1215 1216 esa_config(sc); 1217 1218 reset_state = esa_assp_halt(sc); 1219 1220 esa_init_codec(sc); 1221 esa_codec_reset(sc); 1222 1223 /* Zero kernel and mixer data */ 1224 size = ESA_REV_B_DATA_MEMORY_UNIT_LENGTH * ESA_NUM_UNITS_KERNEL_DATA; 1225 for (i = 0; i < size / 2; i++) { 1226 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, 1227 ESA_KDATA_BASE_ADDR + i, 0); 1228 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, 1229 ESA_KDATA_BASE_ADDR2 + i, 0); 1230 } 1231 1232 /* Init DMA pointer */ 1233 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, ESA_KDATA_CURRENT_DMA, 1234 ESA_KDATA_DMA_XFER0); 1235 1236 /* Write kernel code into memory */ 1237 size = nitems(esa_assp_kernel_image); 1238 for (i = 0; i < size; i++) 1239 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_CODE, 1240 ESA_REV_B_CODE_MEMORY_BEGIN + i, esa_assp_kernel_image[i]); 1241 1242 size = nitems(esa_assp_minisrc_image); 1243 for (i = 0; i < size; i++) 1244 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_CODE, 0x400 + i, 1245 esa_assp_minisrc_image[i]); 1246 1247 /* Write the coefficients for the low pass filter */ 1248 size = nitems(esa_minisrc_lpf_image); 1249 for (i = 0; i < size; i++) 1250 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_CODE, 1251 0x400 + ESA_MINISRC_COEF_LOC + i, esa_minisrc_lpf_image[i]); 1252 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_CODE, 1253 0x400 + ESA_MINISRC_COEF_LOC + size, 0x8000); 1254 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, ESA_KDATA_TASK0, 0x400); 1255 /* Init the mixer number */ 1256 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, 1257 ESA_KDATA_MIXER_TASK_NUMBER, 0); 1258 /* Extreme kernel master volume */ 1259 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, 1260 ESA_KDATA_DAC_LEFT_VOLUME, ESA_ARB_VOLUME); 1261 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, 1262 ESA_KDATA_DAC_RIGHT_VOLUME, ESA_ARB_VOLUME); 1263 1264 if (esa_amp_enable(sc)) 1265 return (-1); 1266 1267 /* Zero entire DAC/ADC area */ 1268 for (i = 0x1100; i < 0x1c00; i++) 1269 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, i, 0); 1270 1271 /* set some sane defaults */ 1272 for (i = 0; i < ESA_NUM_VOICES; i++) { 1273 vc = &sc->voice[i]; 1274 vc->play.data_offset = ESA_DAC_DATA + (data_bytes * i); 1275 vc->rec.data_offset = ESA_DAC_DATA + (data_bytes * i * 2); 1276 } 1277 1278 esa_enable_interrupts(sc); 1279 1280 bus_space_write_1(iot, ioh, ESA_DSP_PORT_CONTROL_REG_B, 1281 reset_state | ESA_REGB_ENABLE_RESET); 1282 1283 return (0); 1284 } 1285 1286 void 1287 esa_config(struct esa_softc *sc) 1288 { 1289 bus_space_tag_t iot = sc->sc_iot; 1290 bus_space_handle_t ioh = sc->sc_ioh; 1291 pcitag_t tag = sc->sc_tag; 1292 pci_chipset_tag_t pc = sc->sc_pct; 1293 u_int32_t data; 1294 1295 data = pci_conf_read(pc, tag, ESA_PCI_ALLEGRO_CONFIG); 1296 data &= ESA_REDUCED_DEBOUNCE; 1297 data |= ESA_PM_CTRL_ENABLE | ESA_CLK_DIV_BY_49 | ESA_USE_PCI_TIMING; 1298 pci_conf_write(pc, tag, ESA_PCI_ALLEGRO_CONFIG, data); 1299 1300 bus_space_write_1(iot, ioh, ESA_ASSP_CONTROL_B, ESA_RESET_ASSP); 1301 data = pci_conf_read(pc, tag, ESA_PCI_ALLEGRO_CONFIG); 1302 data &= ~ESA_INT_CLK_SELECT; 1303 if (sc->type == ESS_MAESTRO3) { 1304 data &= ~ESA_INT_CLK_MULT_ENABLE; 1305 data |= ESA_INT_CLK_SRC_NOT_PCI; 1306 } 1307 data &= ~(ESA_CLK_MULT_MODE_SELECT | ESA_CLK_MULT_MODE_SELECT_2); 1308 pci_conf_write(pc, tag, ESA_PCI_ALLEGRO_CONFIG, data); 1309 1310 if (sc->type == ESS_ALLEGRO1) { 1311 data = pci_conf_read(pc, tag, ESA_PCI_USER_CONFIG); 1312 data |= ESA_IN_CLK_12MHZ_SELECT; 1313 pci_conf_write(pc, tag, ESA_PCI_USER_CONFIG, data); 1314 } 1315 1316 data = bus_space_read_1(iot, ioh, ESA_ASSP_CONTROL_A); 1317 data &= ~(ESA_DSP_CLK_36MHZ_SELECT | ESA_ASSP_CLK_49MHZ_SELECT); 1318 data |= ESA_ASSP_CLK_49MHZ_SELECT; /* XXX: Assumes 49MHz DSP */ 1319 data |= ESA_ASSP_0_WS_ENABLE; 1320 bus_space_write_1(iot, ioh, ESA_ASSP_CONTROL_A, data); 1321 1322 bus_space_write_1(iot, ioh, ESA_ASSP_CONTROL_B, ESA_RUN_ASSP); 1323 1324 return; 1325 } 1326 1327 u_int8_t 1328 esa_assp_halt(struct esa_softc *sc) 1329 { 1330 bus_space_tag_t iot = sc->sc_iot; 1331 bus_space_handle_t ioh = sc->sc_ioh; 1332 u_int8_t data, reset_state; 1333 1334 data = bus_space_read_1(iot, ioh, ESA_DSP_PORT_CONTROL_REG_B); 1335 reset_state = data & ~ESA_REGB_STOP_CLOCK; 1336 delay(10000); /* XXX use tsleep */ 1337 bus_space_write_1(iot, ioh, ESA_DSP_PORT_CONTROL_REG_B, 1338 reset_state & ~ESA_REGB_ENABLE_RESET); 1339 delay(10000); /* XXX use tsleep */ 1340 1341 return (reset_state); 1342 } 1343 1344 void 1345 esa_codec_reset(struct esa_softc *sc) 1346 { 1347 bus_space_tag_t iot = sc->sc_iot; 1348 bus_space_handle_t ioh = sc->sc_ioh; 1349 u_int16_t data, dir; 1350 int retry = 0; 1351 1352 do { 1353 data = bus_space_read_2(iot, ioh, ESA_GPIO_DIRECTION); 1354 dir = data | 0x10; /* assuming pci bus master? */ 1355 1356 /* remote codec config */ 1357 data = bus_space_read_2(iot, ioh, ESA_RING_BUS_CTRL_B); 1358 bus_space_write_2(iot, ioh, ESA_RING_BUS_CTRL_B, 1359 data & ~ESA_SECOND_CODEC_ID_MASK); 1360 data = bus_space_read_2(iot, ioh, ESA_SDO_OUT_DEST_CTRL); 1361 bus_space_write_2(iot, ioh, ESA_SDO_OUT_DEST_CTRL, 1362 data & ~ESA_COMMAND_ADDR_OUT); 1363 data = bus_space_read_2(iot, ioh, ESA_SDO_IN_DEST_CTRL); 1364 bus_space_write_2(iot, ioh, ESA_SDO_IN_DEST_CTRL, 1365 data & ~ESA_STATUS_ADDR_IN); 1366 1367 bus_space_write_2(iot, ioh, ESA_RING_BUS_CTRL_A, 1368 ESA_IO_SRAM_ENABLE); 1369 delay(20); 1370 1371 bus_space_write_2(iot, ioh, ESA_GPIO_DIRECTION, 1372 dir & ~ESA_GPO_PRIMARY_AC97); 1373 bus_space_write_2(iot, ioh, ESA_GPIO_MASK, 1374 ~ESA_GPO_PRIMARY_AC97); 1375 bus_space_write_2(iot, ioh, ESA_GPIO_DATA, 0); 1376 bus_space_write_2(iot, ioh, ESA_GPIO_DIRECTION, 1377 dir | ESA_GPO_PRIMARY_AC97); 1378 delay(sc->delay1 * 1000); 1379 bus_space_write_2(iot, ioh, ESA_GPIO_DATA, 1380 ESA_GPO_PRIMARY_AC97); 1381 delay(5); 1382 bus_space_write_2(iot, ioh, ESA_RING_BUS_CTRL_A, 1383 ESA_IO_SRAM_ENABLE | ESA_SERIAL_AC_LINK_ENABLE); 1384 bus_space_write_2(iot, ioh, ESA_GPIO_MASK, ~0); 1385 delay(sc->delay2 * 1000); 1386 1387 esa_read_codec(sc, 0x7c, &data); 1388 if ((data == 0) || (data == 0xffff)) { 1389 retry++; 1390 if (retry > 3) { 1391 printf("%s: esa_codec_reset: failed\n", 1392 sc->sc_dev.dv_xname); 1393 break; 1394 } 1395 printf("%s: esa_codec_reset: retrying\n", 1396 sc->sc_dev.dv_xname); 1397 } else 1398 retry = 0; 1399 } while (retry); 1400 1401 return; 1402 } 1403 1404 int 1405 esa_amp_enable(struct esa_softc *sc) 1406 { 1407 bus_space_tag_t iot = sc->sc_iot; 1408 bus_space_handle_t ioh = sc->sc_ioh; 1409 u_int32_t gpo, polarity_port, polarity; 1410 u_int16_t data; 1411 1412 switch (sc->type) { 1413 case ESS_ALLEGRO1: 1414 polarity_port = 0x1800; 1415 break; 1416 case ESS_MAESTRO3: 1417 polarity_port = 0x1100; 1418 break; 1419 default: 1420 printf("%s: esa_amp_enable: Unknown chip type!!!\n", 1421 sc->sc_dev.dv_xname); 1422 return (1); 1423 } 1424 1425 gpo = (polarity_port >> 8) & 0x0f; 1426 polarity = polarity_port >> 12; 1427 polarity = !polarity; /* Enable */ 1428 polarity = polarity << gpo; 1429 gpo = 1 << gpo; 1430 bus_space_write_2(iot, ioh, ESA_GPIO_MASK, ~gpo); 1431 data = bus_space_read_2(iot, ioh, ESA_GPIO_DIRECTION); 1432 bus_space_write_2(iot, ioh, ESA_GPIO_DIRECTION, data | gpo); 1433 data = ESA_GPO_SECONDARY_AC97 | ESA_GPO_PRIMARY_AC97 | polarity; 1434 bus_space_write_2(iot, ioh, ESA_GPIO_DATA, data); 1435 bus_space_write_2(iot, ioh, ESA_GPIO_MASK, ~0); 1436 1437 return (0); 1438 } 1439 1440 void 1441 esa_enable_interrupts(struct esa_softc *sc) 1442 { 1443 bus_space_tag_t iot = sc->sc_iot; 1444 bus_space_handle_t ioh = sc->sc_ioh; 1445 u_int8_t data; 1446 1447 bus_space_write_2(iot, ioh, ESA_HOST_INT_CTRL, 1448 ESA_ASSP_INT_ENABLE | ESA_HV_INT_ENABLE); 1449 data = bus_space_read_1(iot, ioh, ESA_ASSP_CONTROL_C); 1450 bus_space_write_1(iot, ioh, ESA_ASSP_CONTROL_C, 1451 data | ESA_ASSP_HOST_INT_ENABLE); 1452 } 1453 1454 /* 1455 * List management 1456 */ 1457 int 1458 esa_add_list(struct esa_voice *vc, struct esa_list *el, 1459 u_int16_t val, int index) 1460 { 1461 struct esa_softc *sc = (struct esa_softc *)vc->parent; 1462 1463 el->indexmap[index] = el->currlen; 1464 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, 1465 el->mem_addr + el->currlen, 1466 val); 1467 1468 return (el->currlen++); 1469 } 1470 1471 void 1472 esa_remove_list(struct esa_voice *vc, struct esa_list *el, int index) 1473 { 1474 struct esa_softc *sc = (struct esa_softc *)vc->parent; 1475 u_int16_t val; 1476 int lastindex = el->currlen - 1; 1477 int vindex = el->indexmap[index]; 1478 int i; 1479 1480 /* reset our virtual index */ 1481 el->indexmap[index] = -1; 1482 1483 if (vindex != lastindex) { 1484 val = esa_read_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, 1485 el->mem_addr + lastindex); 1486 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, 1487 el->mem_addr + vindex, 1488 val); 1489 for (i = 0; i < ESA_NUM_VOICES * 2; i++) 1490 if (el->indexmap[i] == lastindex) 1491 break; 1492 if (i >= ESA_NUM_VOICES * 2) 1493 printf("%s: esa_remove_list: invalid task index\n", 1494 sc->sc_dev.dv_xname); 1495 else 1496 el->indexmap[i] = vindex; 1497 } 1498 1499 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, 1500 el->mem_addr + lastindex, 0); 1501 el->currlen--; 1502 1503 return; 1504 } 1505 1506 int 1507 esa_activate(struct device *self, int act) 1508 { 1509 struct esa_softc *sc = (struct esa_softc *)self; 1510 1511 switch (act) { 1512 case DVACT_SUSPEND: 1513 esa_suspend(sc); 1514 break; 1515 case DVACT_RESUME: 1516 esa_resume(sc); 1517 break; 1518 } 1519 return 0; 1520 } 1521 1522 void 1523 esa_suspend(struct esa_softc *sc) 1524 { 1525 bus_space_tag_t iot = sc->sc_iot; 1526 bus_space_handle_t ioh = sc->sc_ioh; 1527 int i, index; 1528 1529 index = 0; 1530 1531 bus_space_write_2(iot, ioh, ESA_HOST_INT_CTRL, 0); 1532 bus_space_write_1(iot, ioh, ESA_ASSP_CONTROL_C, 0); 1533 1534 esa_assp_halt(sc); 1535 1536 /* Save ASSP state */ 1537 for (i = ESA_REV_B_CODE_MEMORY_BEGIN; i <= ESA_REV_B_CODE_MEMORY_END; 1538 i++) 1539 sc->savemem[index++] = esa_read_assp(sc, 1540 ESA_MEMTYPE_INTERNAL_CODE, i); 1541 for (i = ESA_REV_B_DATA_MEMORY_BEGIN; i <= ESA_REV_B_DATA_MEMORY_END; 1542 i++) 1543 sc->savemem[index++] = esa_read_assp(sc, 1544 ESA_MEMTYPE_INTERNAL_DATA, i); 1545 } 1546 1547 void 1548 esa_resume(struct esa_softc *sc) 1549 { 1550 bus_space_tag_t iot = sc->sc_iot; 1551 bus_space_handle_t ioh = sc->sc_ioh; 1552 int i, index; 1553 u_int8_t reset_state; 1554 1555 index = 0; 1556 1557 esa_config(sc); 1558 1559 reset_state = esa_assp_halt(sc); 1560 1561 esa_codec_reset(sc); 1562 1563 /* restore ASSP */ 1564 for (i = ESA_REV_B_CODE_MEMORY_BEGIN; i <= ESA_REV_B_CODE_MEMORY_END; 1565 i++) 1566 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_CODE, i, 1567 sc->savemem[index++]); 1568 for (i = ESA_REV_B_DATA_MEMORY_BEGIN; i <= ESA_REV_B_DATA_MEMORY_END; 1569 i++) 1570 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, i, 1571 sc->savemem[index++]); 1572 1573 esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, ESA_KDATA_DMA_ACTIVE, 0); 1574 bus_space_write_1(iot, ioh, ESA_DSP_PORT_CONTROL_REG_B, 1575 reset_state | ESA_REGB_ENABLE_RESET); 1576 1577 esa_enable_interrupts(sc); 1578 esa_amp_enable(sc); 1579 } 1580 1581 u_int32_t 1582 esa_get_pointer(struct esa_softc *sc, struct esa_channel *ch) 1583 { 1584 u_int16_t hi = 0, lo = 0; 1585 u_int32_t addr; 1586 int data_offset = ch->data_offset; 1587 1588 hi = esa_read_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, data_offset + 1589 ESA_CDATA_HOST_SRC_CURRENTH); 1590 lo = esa_read_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, data_offset + 1591 ESA_CDATA_HOST_SRC_CURRENTL); 1592 1593 addr = lo | ((u_int32_t)hi << 16); 1594 return (addr - ch->start); 1595 } 1596