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