1 /* $NetBSD: neo.c,v 1.56 2021/09/06 21:56:04 andvar Exp $ */ 2 3 /* 4 * Copyright (c) 1999 Cameron Grant <gandalf@vilnya.demon.co.uk> 5 * All rights reserved. 6 * 7 * Derived from the public domain Linux driver 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHERIN CONTRACT, STRICT 26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THEPOSSIBILITY OF 28 * SUCH DAMAGE. 29 * 30 * FreeBSD: src/sys/dev/sound/pci/neomagic.c,v 1.8 2000/03/20 15:30:50 cg Exp 31 * OpenBSD: neo.c,v 1.4 2000/07/19 09:04:37 csapuntz Exp 32 */ 33 34 #include <sys/cdefs.h> 35 __KERNEL_RCSID(0, "$NetBSD: neo.c,v 1.56 2021/09/06 21:56:04 andvar Exp $"); 36 37 #include <sys/param.h> 38 #include <sys/systm.h> 39 #include <sys/kernel.h> 40 #include <sys/kmem.h> 41 #include <sys/device.h> 42 #include <sys/bus.h> 43 #include <sys/audioio.h> 44 45 #include <dev/audio/audio_if.h> 46 47 #include <dev/ic/ac97var.h> 48 49 #include <dev/pci/pcidevs.h> 50 #include <dev/pci/pcivar.h> 51 #include <dev/pci/neoreg.h> 52 #include <dev/pci/neo-coeff.h> 53 54 /* -------------------------------------------------------------------- */ 55 /* 56 * As of 04/13/00, public documentation on the Neomagic 256 is not available. 57 * These comments were gleaned by looking at the driver carefully. 58 * 59 * The Neomagic 256 AV/ZX chips provide both video and audio capabilities 60 * on one chip. About 2-6 megabytes of memory are associated with 61 * the chip. Most of this goes to video frame buffers, but some is used for 62 * audio buffering 63 * 64 * Unlike most PCI audio chips, the Neomagic chip does not rely on DMA. 65 * Instead, the chip allows you to carve out two ring buffers out of its 66 * memory. However you carve this and how much you can carve seems to be 67 * voodoo. The algorithm is in nm_init. 68 * 69 * Most Neomagic audio chips use the AC-97 codec interface. However, there 70 * seem to be a select few chips 256AV chips that do not support AC-97. 71 * This driver does not support them but there are rumors that it 72 * might work with wss isa drivers. This might require some playing around 73 * with your BIOS. 74 * 75 * The Neomagic 256 AV/ZX have 2 PCI I/O region descriptors. Both of 76 * them describe a memory region. The frame buffer is the first region 77 * and the register set is the second region. 78 * 79 * The register manipulation logic is taken from the Linux driver, 80 * which is in the public domain. 81 * 82 * The Neomagic is even nice enough to map the AC-97 codec registers into 83 * the register space to allow direct manipulation. Watch out, accessing 84 * AC-97 registers on the Neomagic requires great delicateness, otherwise 85 * the thing will hang the PCI bus, rendering your system frozen. 86 * 87 * For one, it seems the Neomagic status register that reports AC-97 88 * readiness should NOT be polled more often than once each 1ms. 89 * 90 * Also, writes to the AC-97 register space may take order 40us to 91 * complete. 92 * 93 * Unlike many sound engines, the Neomagic does not support (as far as 94 * we know :) the notion of interrupting every n bytes transferred, 95 * unlike many DMA engines. Instead, it allows you to specify one 96 * location in each ring buffer (called the watermark). When the chip 97 * passes that location while playing, it signals an interrupt. 98 * 99 * The ring buffer size is currently 16k. That is about 100ms of audio 100 * at 44.1kHz/stero/16 bit. However, to keep the buffer full, interrupts 101 * are generated more often than that, so 20-40 interrupts per second 102 * should not be unexpected. Increasing BUFFSIZE should help minimize 103 * of glitches due to drivers that spend too much time looping at high 104 * privilege levels as well as the impact of badly written audio 105 * interface clients. 106 * 107 * TO-DO list: 108 * Figure out interaction with video stuff (look at Xfree86 driver?) 109 * 110 * Figure out how to shrink that huge table neo-coeff.h 111 */ 112 113 #define NM_BUFFSIZE 16384 114 115 /* device private data */ 116 struct neo_softc { 117 device_t dev; 118 kmutex_t lock; 119 kmutex_t intr_lock; 120 121 bus_space_tag_t bufiot; 122 bus_space_handle_t bufioh; 123 124 bus_space_tag_t regiot; 125 bus_space_handle_t regioh; 126 127 uint32_t type; 128 void *ih; 129 130 void (*pintr)(void *); /* DMA completion intr handler */ 131 void *parg; /* arg for intr() */ 132 133 void (*rintr)(void *); /* DMA completion intr handler */ 134 void *rarg; /* arg for intr() */ 135 136 vaddr_t buf_vaddr; 137 vaddr_t rbuf_vaddr; 138 vaddr_t pbuf_vaddr; 139 int pbuf_allocated; 140 int rbuf_allocated; 141 142 bus_addr_t buf_pciaddr; 143 bus_addr_t rbuf_pciaddr; 144 bus_addr_t pbuf_pciaddr; 145 146 uint32_t ac97_base, ac97_status, ac97_busy; 147 uint32_t buftop, pbuf, rbuf, cbuf, acbuf; 148 uint32_t playint, recint, misc1int, misc2int; 149 uint32_t irsz, badintr; 150 151 uint32_t pbufsize; 152 uint32_t rbufsize; 153 154 uint32_t pblksize; 155 uint32_t rblksize; 156 157 uint32_t pwmark; 158 uint32_t rwmark; 159 160 struct ac97_codec_if *codec_if; 161 struct ac97_host_if host_if; 162 }; 163 164 /* -------------------------------------------------------------------- */ 165 166 /* 167 * prototypes 168 */ 169 170 static int nm_waitcd(struct neo_softc *); 171 static int nm_loadcoeff(struct neo_softc *, int, int); 172 static int nm_init(struct neo_softc *); 173 174 static int neo_match(device_t, cfdata_t, void *); 175 static void neo_attach(device_t, device_t, void *); 176 static int neo_intr(void *); 177 static int neo_rate2index(u_int); 178 179 static int neo_query_format(void *, audio_format_query_t *); 180 static int neo_set_format(void *, int, 181 const audio_params_t *, const audio_params_t *, 182 audio_filter_reg_t *, audio_filter_reg_t *); 183 static int neo_round_blocksize(void *, int, int, const audio_params_t *); 184 static int neo_trigger_output(void *, void *, void *, int, 185 void (*)(void *), void *, 186 const audio_params_t *); 187 static int neo_trigger_input(void *, void *, void *, int, 188 void (*)(void *), void *, 189 const audio_params_t *); 190 static int neo_halt_output(void *); 191 static int neo_halt_input(void *); 192 static int neo_getdev(void *, struct audio_device *); 193 static int neo_mixer_set_port(void *, mixer_ctrl_t *); 194 static int neo_mixer_get_port(void *, mixer_ctrl_t *); 195 static int neo_attach_codec(void *, struct ac97_codec_if *); 196 static int neo_read_codec(void *, uint8_t, uint16_t *); 197 static int neo_write_codec(void *, uint8_t, uint16_t); 198 static int neo_reset_codec(void *); 199 static enum ac97_host_flags neo_flags_codec(void *); 200 static int neo_query_devinfo(void *, mixer_devinfo_t *); 201 static void * neo_malloc(void *, int, size_t); 202 static void neo_free(void *, void *, size_t); 203 static size_t neo_round_buffersize(void *, int, size_t); 204 static int neo_get_props(void *); 205 static void neo_get_locks(void *, kmutex_t **, kmutex_t **); 206 207 CFATTACH_DECL_NEW(neo, sizeof(struct neo_softc), 208 neo_match, neo_attach, NULL, NULL); 209 210 static struct audio_device neo_device = { 211 "NeoMagic 256", 212 "", 213 "neo" 214 }; 215 216 /* 217 * The frequency list in this format is also referred from neo_rate2index(). 218 * So don't rearrange or delete entries. 219 */ 220 static const struct audio_format neo_formats[] = { 221 { 222 .mode = AUMODE_PLAY | AUMODE_RECORD, 223 .encoding = AUDIO_ENCODING_SLINEAR_LE, 224 .validbits = 16, 225 .precision = 16, 226 .channels = 2, 227 .channel_mask = AUFMT_STEREO, 228 .frequency_type = 8, 229 .frequency = 230 { 8000, 11025, 16000, 22050, 24000, 32000, 44100, 48000 }, 231 }, 232 }; 233 #define NEO_NFORMATS __arraycount(neo_formats) 234 235 /* -------------------------------------------------------------------- */ 236 237 static const struct audio_hw_if neo_hw_if = { 238 .query_format = neo_query_format, 239 .set_format = neo_set_format, 240 .round_blocksize = neo_round_blocksize, 241 .halt_output = neo_halt_output, 242 .halt_input = neo_halt_input, 243 .getdev = neo_getdev, 244 .set_port = neo_mixer_set_port, 245 .get_port = neo_mixer_get_port, 246 .query_devinfo = neo_query_devinfo, 247 .allocm = neo_malloc, 248 .freem = neo_free, 249 .round_buffersize = neo_round_buffersize, 250 .get_props = neo_get_props, 251 .trigger_output = neo_trigger_output, 252 .trigger_input = neo_trigger_input, 253 .get_locks = neo_get_locks, 254 }; 255 256 /* -------------------------------------------------------------------- */ 257 258 #define nm_rd_1(sc, regno) \ 259 bus_space_read_1((sc)->regiot, (sc)->regioh, (regno)) 260 261 #define nm_rd_2(sc, regno) \ 262 bus_space_read_2((sc)->regiot, (sc)->regioh, (regno)) 263 264 #define nm_rd_4(sc, regno) \ 265 bus_space_read_4((sc)->regiot, (sc)->regioh, (regno)) 266 267 #define nm_wr_1(sc, regno, val) \ 268 bus_space_write_1((sc)->regiot, (sc)->regioh, (regno), (val)) 269 270 #define nm_wr_2(sc, regno, val) \ 271 bus_space_write_2((sc)->regiot, (sc)->regioh, (regno), (val)) 272 273 #define nm_wr_4(sc, regno, val) \ 274 bus_space_write_4((sc)->regiot, (sc)->regioh, (regno), (val)) 275 276 #define nm_rdbuf_4(sc, regno) \ 277 bus_space_read_4((sc)->bufiot, (sc)->bufioh, (regno)) 278 279 #define nm_wrbuf_1(sc, regno, val) \ 280 bus_space_write_1((sc)->bufiot, (sc)->bufioh, (regno), (val)) 281 282 /* ac97 codec */ 283 static int 284 nm_waitcd(struct neo_softc *sc) 285 { 286 int cnt; 287 int fail; 288 289 cnt = 10; 290 fail = 1; 291 while (cnt-- > 0) { 292 if (nm_rd_2(sc, sc->ac97_status) & sc->ac97_busy) 293 DELAY(100); 294 else { 295 fail = 0; 296 break; 297 } 298 } 299 return fail; 300 } 301 302 static void 303 nm_ackint(struct neo_softc *sc, uint32_t num) 304 { 305 306 switch (sc->type) { 307 case PCI_PRODUCT_NEOMAGIC_NMMM256AV_AU: 308 nm_wr_2(sc, NM_INT_REG, num << 1); 309 break; 310 311 case PCI_PRODUCT_NEOMAGIC_NMMM256ZX_AU: 312 nm_wr_4(sc, NM_INT_REG, num); 313 break; 314 } 315 } 316 317 static int 318 nm_loadcoeff(struct neo_softc *sc, int dir, int num) 319 { 320 int ofs, sz, i; 321 uint32_t addr; 322 323 addr = (dir == AUMODE_PLAY)? 0x01c : 0x21c; 324 if (dir == AUMODE_RECORD) 325 num += 8; 326 sz = coefficientSizes[num]; 327 ofs = 0; 328 while (num-- > 0) 329 ofs+= coefficientSizes[num]; 330 for (i = 0; i < sz; i++) 331 nm_wrbuf_1(sc, sc->cbuf + i, coefficients[ofs + i]); 332 nm_wr_4(sc, addr, sc->cbuf); 333 if (dir == AUMODE_PLAY) 334 sz--; 335 nm_wr_4(sc, addr + 4, sc->cbuf + sz); 336 return 0; 337 } 338 339 /* The interrupt handler */ 340 static int 341 neo_intr(void *p) 342 { 343 struct neo_softc *sc; 344 int status, x; 345 int rv; 346 347 sc = (struct neo_softc *)p; 348 mutex_spin_enter(&sc->intr_lock); 349 350 rv = 0; 351 status = (sc->irsz == 2) ? 352 nm_rd_2(sc, NM_INT_REG) : 353 nm_rd_4(sc, NM_INT_REG); 354 355 if (status & sc->playint) { 356 status &= ~sc->playint; 357 358 sc->pwmark += sc->pblksize; 359 sc->pwmark %= sc->pbufsize; 360 361 nm_wr_4(sc, NM_PBUFFER_WMARK, sc->pbuf + sc->pwmark); 362 363 nm_ackint(sc, sc->playint); 364 365 if (sc->pintr) 366 (*sc->pintr)(sc->parg); 367 368 rv = 1; 369 } 370 if (status & sc->recint) { 371 status &= ~sc->recint; 372 373 sc->rwmark += sc->rblksize; 374 sc->rwmark %= sc->rbufsize; 375 nm_wr_4(sc, NM_RBUFFER_WMARK, sc->rbuf + sc->rwmark); 376 nm_ackint(sc, sc->recint); 377 if (sc->rintr) 378 (*sc->rintr)(sc->rarg); 379 380 rv = 1; 381 } 382 if (status & sc->misc1int) { 383 status &= ~sc->misc1int; 384 nm_ackint(sc, sc->misc1int); 385 x = nm_rd_1(sc, 0x400); 386 nm_wr_1(sc, 0x400, x | 2); 387 printf("%s: misc int 1\n", device_xname(sc->dev)); 388 rv = 1; 389 } 390 if (status & sc->misc2int) { 391 status &= ~sc->misc2int; 392 nm_ackint(sc, sc->misc2int); 393 x = nm_rd_1(sc, 0x400); 394 nm_wr_1(sc, 0x400, x & ~2); 395 printf("%s: misc int 2\n", device_xname(sc->dev)); 396 rv = 1; 397 } 398 if (status) { 399 status &= ~sc->misc2int; 400 nm_ackint(sc, sc->misc2int); 401 printf("%s: unknown int\n", device_xname(sc->dev)); 402 rv = 1; 403 } 404 405 mutex_spin_exit(&sc->intr_lock); 406 return rv; 407 } 408 409 /* -------------------------------------------------------------------- */ 410 411 /* 412 * Probe and attach the card 413 */ 414 415 static int 416 nm_init(struct neo_softc *sc) 417 { 418 uint32_t ofs, i; 419 420 switch (sc->type) { 421 case PCI_PRODUCT_NEOMAGIC_NMMM256AV_AU: 422 sc->ac97_base = NM_MIXER_OFFSET; 423 sc->ac97_status = NM_MIXER_STATUS_OFFSET; 424 sc->ac97_busy = NM_MIXER_READY_MASK; 425 426 sc->buftop = 2560 * 1024; 427 428 sc->irsz = 2; 429 sc->playint = NM_PLAYBACK_INT; 430 sc->recint = NM_RECORD_INT; 431 sc->misc1int = NM_MISC_INT_1; 432 sc->misc2int = NM_MISC_INT_2; 433 break; 434 435 case PCI_PRODUCT_NEOMAGIC_NMMM256ZX_AU: 436 sc->ac97_base = NM_MIXER_OFFSET; 437 sc->ac97_status = NM2_MIXER_STATUS_OFFSET; 438 sc->ac97_busy = NM2_MIXER_READY_MASK; 439 440 sc->buftop = (nm_rd_2(sc, 0xa0b) ? 6144 : 4096) * 1024; 441 442 sc->irsz = 4; 443 sc->playint = NM2_PLAYBACK_INT; 444 sc->recint = NM2_RECORD_INT; 445 sc->misc1int = NM2_MISC_INT_1; 446 sc->misc2int = NM2_MISC_INT_2; 447 break; 448 #ifdef DIAGNOSTIC 449 default: 450 panic("nm_init: impossible"); 451 #endif 452 } 453 454 sc->badintr = 0; 455 ofs = sc->buftop - 0x0400; 456 sc->buftop -= 0x1400; 457 458 if ((nm_rdbuf_4(sc, ofs) & NM_SIG_MASK) == NM_SIGNATURE) { 459 i = nm_rdbuf_4(sc, ofs + 4); 460 if (i != 0 && i != 0xffffffff) 461 sc->buftop = i; 462 } 463 464 sc->cbuf = sc->buftop - NM_MAX_COEFFICIENT; 465 sc->rbuf = sc->cbuf - NM_BUFFSIZE; 466 sc->pbuf = sc->rbuf - NM_BUFFSIZE; 467 sc->acbuf = sc->pbuf - (NM_TOTAL_COEFF_COUNT * 4); 468 469 sc->buf_vaddr = (vaddr_t) bus_space_vaddr(sc->bufiot, sc->bufioh); 470 sc->rbuf_vaddr = sc->buf_vaddr + sc->rbuf; 471 sc->pbuf_vaddr = sc->buf_vaddr + sc->pbuf; 472 473 sc->rbuf_pciaddr = sc->buf_pciaddr + sc->rbuf; 474 sc->pbuf_pciaddr = sc->buf_pciaddr + sc->pbuf; 475 476 nm_wr_1(sc, 0, 0x11); 477 nm_wr_1(sc, NM_RECORD_ENABLE_REG, 0); 478 nm_wr_2(sc, 0x214, 0); 479 480 return 0; 481 } 482 483 static int 484 neo_match(device_t parent, cfdata_t match, void *aux) 485 { 486 struct pci_attach_args *pa; 487 pcireg_t subdev; 488 489 pa = aux; 490 if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_NEOMAGIC) 491 return 0; 492 493 subdev = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_SUBSYS_ID_REG); 494 495 switch (PCI_PRODUCT(pa->pa_id)) { 496 case PCI_PRODUCT_NEOMAGIC_NMMM256AV_AU: 497 /* 498 * We have to weed-out the non-AC'97 versions of 499 * the chip (i.e. the ones that are known to work 500 * in WSS emulation mode), as they won't work with 501 * this driver. 502 */ 503 switch (PCI_VENDOR(subdev)) { 504 case PCI_VENDOR_DELL: 505 switch (PCI_PRODUCT(subdev)) { 506 case 0x008f: 507 return 0; 508 } 509 break; 510 511 case PCI_VENDOR_HP: 512 switch (PCI_PRODUCT(subdev)) { 513 case 0x0007: 514 return 0; 515 } 516 break; 517 518 case PCI_VENDOR_IBM: 519 switch (PCI_PRODUCT(subdev)) { 520 case 0x00dd: 521 return 0; 522 } 523 break; 524 } 525 return 1; 526 527 case PCI_PRODUCT_NEOMAGIC_NMMM256ZX_AU: 528 return 1; 529 } 530 531 return 0; 532 } 533 534 static bool 535 neo_resume(device_t dv, const pmf_qual_t *qual) 536 { 537 struct neo_softc *sc = device_private(dv); 538 539 mutex_enter(&sc->lock); 540 mutex_spin_enter(&sc->intr_lock); 541 nm_init(sc); 542 mutex_spin_exit(&sc->intr_lock); 543 sc->codec_if->vtbl->restore_ports(sc->codec_if); 544 mutex_exit(&sc->lock); 545 546 return true; 547 } 548 549 static void 550 neo_attach(device_t parent, device_t self, void *aux) 551 { 552 struct neo_softc *sc; 553 struct pci_attach_args *pa; 554 pci_chipset_tag_t pc; 555 char const *intrstr; 556 pci_intr_handle_t ih; 557 pcireg_t csr; 558 int error; 559 char intrbuf[PCI_INTRSTR_LEN]; 560 561 sc = device_private(self); 562 sc->dev = self; 563 pa = aux; 564 pc = pa->pa_pc; 565 566 sc->type = PCI_PRODUCT(pa->pa_id); 567 568 printf(": NeoMagic 256%s audio\n", 569 sc->type == PCI_PRODUCT_NEOMAGIC_NMMM256AV_AU ? "AV" : "ZX"); 570 571 /* Map I/O register */ 572 if (pci_mapreg_map(pa, PCI_MAPREG_START, PCI_MAPREG_TYPE_MEM, 0, 573 &sc->bufiot, &sc->bufioh, &sc->buf_pciaddr, NULL)) { 574 aprint_error_dev(self, "can't map buffer\n"); 575 return; 576 } 577 578 if (pci_mapreg_map(pa, PCI_MAPREG_START + 4, PCI_MAPREG_TYPE_MEM, 579 BUS_SPACE_MAP_LINEAR, &sc->regiot, &sc->regioh, NULL, NULL)) { 580 aprint_error_dev(self, "can't map registers\n"); 581 return; 582 } 583 584 /* Map and establish the interrupt. */ 585 if (pci_intr_map(pa, &ih)) { 586 aprint_error_dev(self, "couldn't map interrupt\n"); 587 return; 588 } 589 590 mutex_init(&sc->lock, MUTEX_DEFAULT, IPL_NONE); 591 mutex_init(&sc->intr_lock, MUTEX_DEFAULT, IPL_AUDIO); 592 593 intrstr = pci_intr_string(pc, ih, intrbuf, sizeof(intrbuf)); 594 sc->ih = pci_intr_establish_xname(pc, ih, IPL_AUDIO, neo_intr, sc, 595 device_xname(self)); 596 597 if (sc->ih == NULL) { 598 aprint_error_dev(self, "couldn't establish interrupt"); 599 if (intrstr != NULL) 600 aprint_error(" at %s", intrstr); 601 aprint_error("\n"); 602 mutex_destroy(&sc->lock); 603 mutex_destroy(&sc->intr_lock); 604 return; 605 } 606 aprint_normal_dev(self, "interrupting at %s\n", intrstr); 607 608 mutex_spin_enter(&sc->intr_lock); 609 error = nm_init(sc); 610 mutex_spin_exit(&sc->intr_lock); 611 if (error != 0) { 612 mutex_destroy(&sc->lock); 613 mutex_destroy(&sc->intr_lock); 614 return; 615 } 616 617 /* Enable the device. */ 618 csr = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG); 619 pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, 620 csr | PCI_COMMAND_MASTER_ENABLE); 621 622 sc->host_if.arg = sc; 623 sc->host_if.attach = neo_attach_codec; 624 sc->host_if.read = neo_read_codec; 625 sc->host_if.write = neo_write_codec; 626 sc->host_if.reset = neo_reset_codec; 627 sc->host_if.flags = neo_flags_codec; 628 629 if (ac97_attach(&sc->host_if, self, &sc->lock) != 0) { 630 mutex_destroy(&sc->lock); 631 mutex_destroy(&sc->intr_lock); 632 return; 633 } 634 635 if (!pmf_device_register(self, NULL, neo_resume)) 636 aprint_error_dev(self, "couldn't establish power handler\n"); 637 638 audio_attach_mi(&neo_hw_if, sc, self); 639 } 640 641 static int 642 neo_read_codec(void *v, uint8_t a, uint16_t *d) 643 { 644 struct neo_softc *sc; 645 646 sc = v; 647 if (!nm_waitcd(sc)) { 648 *d = nm_rd_2(sc, sc->ac97_base + a); 649 DELAY(1000); 650 return 0; 651 } 652 653 return ENXIO; 654 } 655 656 657 static int 658 neo_write_codec(void *v, u_int8_t a, u_int16_t d) 659 { 660 struct neo_softc *sc; 661 int cnt; 662 663 sc = v; 664 cnt = 3; 665 if (!nm_waitcd(sc)) { 666 while (cnt-- > 0) { 667 nm_wr_2(sc, sc->ac97_base + a, d); 668 if (!nm_waitcd(sc)) { 669 DELAY(1000); 670 return 0; 671 } 672 } 673 } 674 675 return ENXIO; 676 } 677 678 static int 679 neo_attach_codec(void *v, struct ac97_codec_if *codec_if) 680 { 681 struct neo_softc *sc; 682 683 sc = v; 684 sc->codec_if = codec_if; 685 return 0; 686 } 687 688 static int 689 neo_reset_codec(void *v) 690 { 691 struct neo_softc *sc; 692 693 sc = v; 694 nm_wr_1(sc, 0x6c0, 0x01); 695 nm_wr_1(sc, 0x6cc, 0x87); 696 nm_wr_1(sc, 0x6cc, 0x80); 697 nm_wr_1(sc, 0x6cc, 0x00); 698 return 0; 699 } 700 701 static enum ac97_host_flags 702 neo_flags_codec(void *v) 703 { 704 705 return AC97_HOST_DONT_READ; 706 } 707 708 static int 709 neo_query_format(void *addr, audio_format_query_t *afp) 710 { 711 712 return audio_query_format(neo_formats, NEO_NFORMATS, afp); 713 } 714 715 /* Return index number of sample_rate */ 716 static int 717 neo_rate2index(u_int sample_rate) 718 { 719 int i; 720 721 for (i = 0; i < neo_formats[0].frequency_type; i++) { 722 if (sample_rate == neo_formats[0].frequency[i]) 723 return i; 724 } 725 726 /* NOTREACHED */ 727 panic("neo_formats.frequency mismatch?"); 728 } 729 730 /* Todo: don't commit settings to card until we've verified all parameters */ 731 static int 732 neo_set_format(void *addr, int setmode, 733 const audio_params_t *play, const audio_params_t *rec, 734 audio_filter_reg_t *pfil, audio_filter_reg_t *rfil) 735 { 736 struct neo_softc *sc; 737 const audio_params_t *p; 738 uint32_t base; 739 uint8_t x; 740 int mode; 741 742 sc = addr; 743 for (mode = AUMODE_RECORD; mode != -1; 744 mode = mode == AUMODE_RECORD ? AUMODE_PLAY : -1) { 745 if ((setmode & mode) == 0) 746 continue; 747 748 p = mode == AUMODE_PLAY ? play : rec; 749 750 x = neo_rate2index(p->sample_rate); 751 nm_loadcoeff(sc, mode, x); 752 753 x <<= 4; 754 x &= NM_RATE_MASK; 755 x |= NM_RATE_BITS_16; 756 x |= NM_RATE_STEREO; 757 758 base = (mode == AUMODE_PLAY)? 759 NM_PLAYBACK_REG_OFFSET : NM_RECORD_REG_OFFSET; 760 nm_wr_1(sc, base + NM_RATE_REG_OFFSET, x); 761 } 762 763 return 0; 764 } 765 766 static int 767 neo_round_blocksize(void *addr, int blk, int mode, 768 const audio_params_t *param) 769 { 770 771 /* The number of blocks must be 3 or greater. */ 772 return NM_BUFFSIZE / 4; 773 } 774 775 static int 776 neo_trigger_output(void *addr, void *start, void *end, int blksize, 777 void (*intr)(void *), void *arg, const audio_params_t *param) 778 { 779 struct neo_softc *sc; 780 int ssz; 781 782 sc = addr; 783 sc->pintr = intr; 784 sc->parg = arg; 785 786 ssz = (param->precision == 16) ? 2 : 1; 787 if (param->channels == 2) 788 ssz <<= 1; 789 790 sc->pbufsize = ((char*)end - (char *)start); 791 sc->pblksize = blksize; 792 sc->pwmark = blksize; 793 794 nm_wr_4(sc, NM_PBUFFER_START, sc->pbuf); 795 nm_wr_4(sc, NM_PBUFFER_END, sc->pbuf + sc->pbufsize - ssz); 796 nm_wr_4(sc, NM_PBUFFER_CURRP, sc->pbuf); 797 nm_wr_4(sc, NM_PBUFFER_WMARK, sc->pbuf + sc->pwmark); 798 nm_wr_1(sc, NM_PLAYBACK_ENABLE_REG, NM_PLAYBACK_FREERUN | 799 NM_PLAYBACK_ENABLE_FLAG); 800 nm_wr_2(sc, NM_AUDIO_MUTE_REG, 0); 801 802 return 0; 803 } 804 805 static int 806 neo_trigger_input(void *addr, void *start, void *end, int blksize, 807 void (*intr)(void *), void *arg, const audio_params_t *param) 808 { 809 struct neo_softc *sc; 810 int ssz; 811 812 sc = addr; 813 sc->rintr = intr; 814 sc->rarg = arg; 815 816 ssz = (param->precision == 16) ? 2 : 1; 817 if (param->channels == 2) 818 ssz <<= 1; 819 820 sc->rbufsize = ((char*)end - (char *)start); 821 sc->rblksize = blksize; 822 sc->rwmark = blksize; 823 824 /* 825 * XXX Doesn't it need to subtract ssz from BUFFER_END like 826 * trigger_output()? 827 */ 828 nm_wr_4(sc, NM_RBUFFER_START, sc->rbuf); 829 nm_wr_4(sc, NM_RBUFFER_END, sc->rbuf + sc->rbufsize); 830 nm_wr_4(sc, NM_RBUFFER_CURRP, sc->rbuf); 831 nm_wr_4(sc, NM_RBUFFER_WMARK, sc->rbuf + sc->rwmark); 832 nm_wr_1(sc, NM_RECORD_ENABLE_REG, NM_RECORD_FREERUN | 833 NM_RECORD_ENABLE_FLAG); 834 835 return 0; 836 } 837 838 static int 839 neo_halt_output(void *addr) 840 { 841 struct neo_softc *sc; 842 843 sc = (struct neo_softc *)addr; 844 nm_wr_1(sc, NM_PLAYBACK_ENABLE_REG, 0); 845 nm_wr_2(sc, NM_AUDIO_MUTE_REG, NM_AUDIO_MUTE_BOTH); 846 sc->pintr = 0; 847 848 return 0; 849 } 850 851 static int 852 neo_halt_input(void *addr) 853 { 854 struct neo_softc *sc; 855 856 sc = (struct neo_softc *)addr; 857 nm_wr_1(sc, NM_RECORD_ENABLE_REG, 0); 858 sc->rintr = 0; 859 860 return 0; 861 } 862 863 static int 864 neo_getdev(void *addr, struct audio_device *retp) 865 { 866 867 *retp = neo_device; 868 return 0; 869 } 870 871 static int 872 neo_mixer_set_port(void *addr, mixer_ctrl_t *cp) 873 { 874 struct neo_softc *sc; 875 876 sc = addr; 877 return sc->codec_if->vtbl->mixer_set_port(sc->codec_if, cp); 878 } 879 880 static int 881 neo_mixer_get_port(void *addr, mixer_ctrl_t *cp) 882 { 883 struct neo_softc *sc; 884 885 sc = addr; 886 return sc->codec_if->vtbl->mixer_get_port(sc->codec_if, cp); 887 } 888 889 static int 890 neo_query_devinfo(void *addr, mixer_devinfo_t *dip) 891 { 892 struct neo_softc *sc; 893 894 sc = addr; 895 return sc->codec_if->vtbl->query_devinfo(sc->codec_if, dip); 896 } 897 898 static void * 899 neo_malloc(void *addr, int direction, size_t size) 900 { 901 struct neo_softc *sc; 902 void *rv; 903 904 sc = addr; 905 rv = NULL; 906 switch (direction) { 907 case AUMODE_PLAY: 908 if (sc->pbuf_allocated == 0) { 909 rv = (void *) sc->pbuf_vaddr; 910 sc->pbuf_allocated = 1; 911 } 912 break; 913 914 case AUMODE_RECORD: 915 if (sc->rbuf_allocated == 0) { 916 rv = (void *) sc->rbuf_vaddr; 917 sc->rbuf_allocated = 1; 918 } 919 break; 920 } 921 922 return rv; 923 } 924 925 static void 926 neo_free(void *addr, void *ptr, size_t size) 927 { 928 struct neo_softc *sc; 929 vaddr_t v; 930 931 sc = addr; 932 v = (vaddr_t)ptr; 933 if (v == sc->pbuf_vaddr) 934 sc->pbuf_allocated = 0; 935 else if (v == sc->rbuf_vaddr) 936 sc->rbuf_allocated = 0; 937 else 938 printf("neo_free: bad address %p\n", ptr); 939 } 940 941 static size_t 942 neo_round_buffersize(void *addr, int direction, size_t size) 943 { 944 945 return NM_BUFFSIZE; 946 } 947 948 static int 949 neo_get_props(void *addr) 950 { 951 952 return AUDIO_PROP_PLAYBACK | AUDIO_PROP_CAPTURE | 953 AUDIO_PROP_INDEPENDENT | AUDIO_PROP_FULLDUPLEX; 954 } 955 956 static void 957 neo_get_locks(void *addr, kmutex_t **intr, kmutex_t **thread) 958 { 959 struct neo_softc *sc; 960 961 sc = addr; 962 *intr = &sc->intr_lock; 963 *thread = &sc->lock; 964 } 965