1 /* $NetBSD: auvia.c,v 1.28 2002/11/04 16:38:49 kent Exp $ */ 2 3 /*- 4 * Copyright (c) 2000 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Tyler C. Sarna 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the NetBSD 21 * Foundation, Inc. and its contributors. 22 * 4. Neither the name of The NetBSD Foundation nor the names of its 23 * contributors may be used to endorse or promote products derived 24 * from this software without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 36 * POSSIBILITY OF SUCH DAMAGE. 37 */ 38 39 /* 40 * VIA Technologies VT82C686A / VT8233 / VT8235 Southbridge Audio Driver 41 * 42 * Documentation links: 43 * 44 * ftp://ftp.alsa-project.org/pub/manuals/via/686a.pdf 45 * ftp://ftp.alsa-project.org/pub/manuals/general/ac97r21.pdf 46 * ftp://ftp.alsa-project.org/pub/manuals/ad/AD1881_0.pdf (example AC'97 codec) 47 */ 48 49 #include <sys/cdefs.h> 50 __KERNEL_RCSID(0, "$NetBSD: auvia.c,v 1.28 2002/11/04 16:38:49 kent Exp $"); 51 52 #include <sys/param.h> 53 #include <sys/systm.h> 54 #include <sys/malloc.h> 55 #include <sys/device.h> 56 #include <sys/audioio.h> 57 58 #include <uvm/uvm_extern.h> 59 60 #include <dev/pci/pcidevs.h> 61 #include <dev/pci/pcivar.h> 62 63 #include <dev/audio_if.h> 64 #include <dev/mulaw.h> 65 #include <dev/auconv.h> 66 67 #include <dev/ic/ac97reg.h> 68 #include <dev/ic/ac97var.h> 69 70 #include <dev/pci/auviavar.h> 71 72 struct auvia_dma { 73 struct auvia_dma *next; 74 caddr_t addr; 75 size_t size; 76 bus_dmamap_t map; 77 bus_dma_segment_t seg; 78 }; 79 80 struct auvia_dma_op { 81 u_int32_t ptr; 82 u_int32_t flags; 83 #define AUVIA_DMAOP_EOL 0x80000000 84 #define AUVIA_DMAOP_FLAG 0x40000000 85 #define AUVIA_DMAOP_STOP 0x20000000 86 #define AUVIA_DMAOP_COUNT(x) ((x)&0x00FFFFFF) 87 }; 88 89 int auvia_match(struct device *, struct cfdata *, void *); 90 void auvia_attach(struct device *, struct device *, void *); 91 int auvia_open(void *, int); 92 void auvia_close(void *); 93 int auvia_query_encoding(void *, struct audio_encoding *); 94 void auvia_set_params_sub(struct auvia_softc *, struct auvia_softc_chan *, 95 struct audio_params *); 96 int auvia_set_params(void *, int, int, struct audio_params *, 97 struct audio_params *); 98 int auvia_round_blocksize(void *, int); 99 int auvia_halt_output(void *); 100 int auvia_halt_input(void *); 101 int auvia_getdev(void *, struct audio_device *); 102 int auvia_set_port(void *, mixer_ctrl_t *); 103 int auvia_get_port(void *, mixer_ctrl_t *); 104 int auvia_query_devinfo(void *, mixer_devinfo_t *); 105 void * auvia_malloc(void *, int, size_t, int, int); 106 void auvia_free(void *, void *, int); 107 size_t auvia_round_buffersize(void *, int, size_t); 108 paddr_t auvia_mappage(void *, void *, off_t, int); 109 int auvia_get_props(void *); 110 int auvia_build_dma_ops(struct auvia_softc *, struct auvia_softc_chan *, 111 struct auvia_dma *, void *, void *, int); 112 int auvia_trigger_output(void *, void *, void *, int, void (*)(void *), 113 void *, struct audio_params *); 114 int auvia_trigger_input(void *, void *, void *, int, void (*)(void *), 115 void *, struct audio_params *); 116 117 int auvia_intr __P((void *)); 118 119 CFATTACH_DECL(auvia, sizeof (struct auvia_softc), 120 auvia_match, auvia_attach, NULL, NULL); 121 122 #define AUVIA_PCICONF_JUNK 0x40 123 #define AUVIA_PCICONF_ENABLES 0x00FF0000 /* reg 42 mask */ 124 #define AUVIA_PCICONF_ACLINKENAB 0x00008000 /* ac link enab */ 125 #define AUVIA_PCICONF_ACNOTRST 0x00004000 /* ~(ac reset) */ 126 #define AUVIA_PCICONF_ACSYNC 0x00002000 /* ac sync */ 127 #define AUVIA_PCICONF_ACVSR 0x00000800 /* var. samp. rate */ 128 #define AUVIA_PCICONF_ACSGD 0x00000400 /* SGD enab */ 129 #define AUVIA_PCICONF_ACFM 0x00000200 /* FM enab */ 130 #define AUVIA_PCICONF_ACSB 0x00000100 /* SB enab */ 131 132 #define AUVIA_PLAY_BASE 0x00 133 #define AUVIA_RECORD_BASE 0x10 134 135 /* *_RP_* are offsets from AUVIA_PLAY_BASE or AUVIA_RECORD_BASE */ 136 #define AUVIA_RP_STAT 0x00 137 #define AUVIA_RPSTAT_INTR 0x03 138 #define AUVIA_RP_CONTROL 0x01 139 #define AUVIA_RPCTRL_START 0x80 140 #define AUVIA_RPCTRL_TERMINATE 0x40 141 #define AUVIA_RPCTRL_AUTOSTART 0x20 142 /* The following are 8233 specific */ 143 #define AUVIA_RPCTRL_STOP 0x04 144 #define AUVIA_RPCTRL_EOL 0x02 145 #define AUVIA_RPCTRL_FLAG 0x01 146 #define AUVIA_RP_MODE 0x02 /* 82c686 specific */ 147 #define AUVIA_RPMODE_INTR_FLAG 0x01 148 #define AUVIA_RPMODE_INTR_EOL 0x02 149 #define AUVIA_RPMODE_STEREO 0x10 150 #define AUVIA_RPMODE_16BIT 0x20 151 #define AUVIA_RPMODE_AUTOSTART 0x80 152 #define AUVIA_RP_DMAOPS_BASE 0x04 153 154 #define VIA8233_RP_DXS_LVOL 0x02 155 #define VIA8233_RP_DXS_RVOL 0x03 156 #define VIA8233_RP_RATEFMT 0x08 157 #define VIA8233_RATEFMT_48K 0xfffff 158 #define VIA8233_RATEFMT_STEREO 0x00100000 159 #define VIA8233_RATEFMT_16BIT 0x00200000 160 161 #define VIA_RP_DMAOPS_COUNT 0x0c 162 163 #define VIA8233_MP_BASE 0x40 164 /* STAT, CONTROL, DMAOPS_BASE, DMAOPS_COUNT are valid */ 165 #define VIA8233_OFF_MP_FORMAT 0x02 166 #define VIA8233_MP_FORMAT_8BIT 0x00 167 #define VIA8233_MP_FORMAT_16BIT 0x80 168 #define VIA8233_MP_FORMAT_CHANNLE_MASK 0x70 /* 1, 2, 4, 6 */ 169 #define VIA8233_OFF_MP_SCRATCH 0x03 170 #define VIA8233_OFF_MP_STOP 0x08 171 172 #define AUVIA_CODEC_CTL 0x80 173 #define AUVIA_CODEC_READ 0x00800000 174 #define AUVIA_CODEC_BUSY 0x01000000 175 #define AUVIA_CODEC_PRIVALID 0x02000000 176 #define AUVIA_CODEC_INDEX(x) ((x)<<16) 177 178 #define CH_WRITE1(sc, ch, off, v) \ 179 bus_space_write_1((sc)->sc_iot, (sc)->sc_ioh, (ch)->sc_base + (off), v) 180 #define CH_WRITE4(sc, ch, off, v) \ 181 bus_space_write_4((sc)->sc_iot, (sc)->sc_ioh, (ch)->sc_base + (off), v) 182 #define CH_READ1(sc, ch, off) \ 183 bus_space_read_1((sc)->sc_iot, (sc)->sc_ioh, (ch)->sc_base + (off)) 184 #define CH_READ4(sc, ch, off) \ 185 bus_space_read_4((sc)->sc_iot, (sc)->sc_ioh, (ch)->sc_base + (off)) 186 187 #define TIMEOUT 50 188 189 struct audio_hw_if auvia_hw_if = { 190 auvia_open, 191 auvia_close, 192 NULL, /* drain */ 193 auvia_query_encoding, 194 auvia_set_params, 195 auvia_round_blocksize, 196 NULL, /* commit_settings */ 197 NULL, /* init_output */ 198 NULL, /* init_input */ 199 NULL, /* start_output */ 200 NULL, /* start_input */ 201 auvia_halt_output, 202 auvia_halt_input, 203 NULL, /* speaker_ctl */ 204 auvia_getdev, 205 NULL, /* setfd */ 206 auvia_set_port, 207 auvia_get_port, 208 auvia_query_devinfo, 209 auvia_malloc, 210 auvia_free, 211 auvia_round_buffersize, 212 auvia_mappage, 213 auvia_get_props, 214 auvia_trigger_output, 215 auvia_trigger_input, 216 NULL, /* dev_ioctl */ 217 }; 218 219 int auvia_attach_codec(void *, struct ac97_codec_if *); 220 int auvia_write_codec(void *, u_int8_t, u_int16_t); 221 int auvia_read_codec(void *, u_int8_t, u_int16_t *); 222 void auvia_reset_codec(void *); 223 int auvia_waitready_codec(struct auvia_softc *sc); 224 int auvia_waitvalid_codec(struct auvia_softc *sc); 225 226 227 int 228 auvia_match(struct device *parent, struct cfdata *match, void *aux) 229 { 230 struct pci_attach_args *pa = (struct pci_attach_args *) aux; 231 232 if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_VIATECH) 233 return 0; 234 switch (PCI_PRODUCT(pa->pa_id)) { 235 case PCI_PRODUCT_VIATECH_VT82C686A_AC97: 236 case PCI_PRODUCT_VIATECH_VT8233_AC97: 237 break; 238 default: 239 return 0; 240 } 241 242 return 1; 243 } 244 245 246 void 247 auvia_attach(struct device *parent, struct device *self, void *aux) 248 { 249 struct pci_attach_args *pa = aux; 250 struct auvia_softc *sc = (struct auvia_softc *) self; 251 const char *intrstr = NULL; 252 pci_chipset_tag_t pc = pa->pa_pc; 253 pcitag_t pt = pa->pa_tag; 254 pci_intr_handle_t ih; 255 bus_size_t iosize; 256 pcireg_t pr; 257 int r; 258 259 sc->sc_play.sc_base = AUVIA_PLAY_BASE; 260 sc->sc_record.sc_base = AUVIA_RECORD_BASE; 261 if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_VIATECH_VT8233_AC97) { 262 sc->sc_flags |= AUVIA_FLAGS_VT8233; 263 sc->sc_play.sc_base = VIA8233_MP_BASE; 264 } 265 266 if (pci_mapreg_map(pa, 0x10, PCI_MAPREG_TYPE_IO, 0, &sc->sc_iot, 267 &sc->sc_ioh, NULL, &iosize)) { 268 printf(": can't map i/o space\n"); 269 return; 270 } 271 272 sc->sc_dmat = pa->pa_dmat; 273 sc->sc_pc = pc; 274 sc->sc_pt = pt; 275 276 r = PCI_REVISION(pa->pa_class); 277 if (sc->sc_flags & AUVIA_FLAGS_VT8233) { 278 sprintf(sc->sc_revision, "0x%02X", r); 279 if (r < 0x50) { 280 printf(": VIA VT8233 AC'97 Audio (rev %s)\n", 281 sc->sc_revision); 282 } else { 283 printf(": VIA VT8235 AC'97 Audio (rev %s)\n", 284 sc->sc_revision); 285 } 286 } else { 287 sc->sc_revision[1] = '\0'; 288 if (r == 0x20) { 289 sc->sc_revision[0] = 'H'; 290 } else if ((r >= 0x10) && (r <= 0x14)) { 291 sc->sc_revision[0] = 'A' + (r - 0x10); 292 } else { 293 sprintf(sc->sc_revision, "0x%02X", r); 294 } 295 296 printf(": VIA VT82C686A AC'97 Audio (rev %s)\n", 297 sc->sc_revision); 298 } 299 300 if (pci_intr_map(pa, &ih)) { 301 printf(": couldn't map interrupt\n"); 302 bus_space_unmap(sc->sc_iot, sc->sc_ioh, iosize); 303 return; 304 } 305 intrstr = pci_intr_string(pc, ih); 306 307 sc->sc_ih = pci_intr_establish(pc, ih, IPL_AUDIO, auvia_intr, sc); 308 if (sc->sc_ih == NULL) { 309 printf("%s: couldn't establish interrupt",sc->sc_dev.dv_xname); 310 if (intrstr != NULL) 311 printf(" at %s", intrstr); 312 printf("\n"); 313 bus_space_unmap(sc->sc_iot, sc->sc_ioh, iosize); 314 return; 315 } 316 317 printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr); 318 319 /* disable SBPro compat & others */ 320 pr = pci_conf_read(pc, pt, AUVIA_PCICONF_JUNK); 321 322 pr &= ~AUVIA_PCICONF_ENABLES; /* clear compat function enables */ 323 /* XXX what to do about MIDI, FM, joystick? */ 324 325 pr |= (AUVIA_PCICONF_ACLINKENAB | AUVIA_PCICONF_ACNOTRST 326 | AUVIA_PCICONF_ACVSR | AUVIA_PCICONF_ACSGD); 327 328 pr &= ~(AUVIA_PCICONF_ACFM | AUVIA_PCICONF_ACSB); 329 330 pci_conf_write(pc, pt, AUVIA_PCICONF_JUNK, pr); 331 332 sc->host_if.arg = sc; 333 sc->host_if.attach = auvia_attach_codec; 334 sc->host_if.read = auvia_read_codec; 335 sc->host_if.write = auvia_write_codec; 336 sc->host_if.reset = auvia_reset_codec; 337 338 if ((r = ac97_attach(&sc->host_if)) != 0) { 339 printf("%s: can't attach codec (error 0x%X)\n", 340 sc->sc_dev.dv_xname, r); 341 pci_intr_disestablish(pc, sc->sc_ih); 342 bus_space_unmap(sc->sc_iot, sc->sc_ioh, iosize); 343 return; 344 } 345 346 audio_attach_mi(&auvia_hw_if, sc, &sc->sc_dev); 347 } 348 349 350 int 351 auvia_attach_codec(void *addr, struct ac97_codec_if *cif) 352 { 353 struct auvia_softc *sc = addr; 354 355 sc->codec_if = cif; 356 357 return 0; 358 } 359 360 361 void 362 auvia_reset_codec(void *addr) 363 { 364 #ifdef notyet /* XXX seems to make codec become unready... ??? */ 365 struct auvia_softc *sc = addr; 366 pcireg_t r; 367 368 /* perform a codec cold reset */ 369 370 r = pci_conf_read(sc->sc_pc, sc->sc_pt, AUVIA_PCICONF_JUNK); 371 372 r &= ~AUVIA_PCICONF_ACNOTRST; /* enable RESET (active low) */ 373 pci_conf_write(sc->sc_pc, sc->sc_pt, AUVIA_PCICONF_JUNK, r); 374 delay(2); 375 376 r |= AUVIA_PCICONF_ACNOTRST; /* disable RESET (inactive high) */ 377 pci_conf_write(sc->sc_pc, sc->sc_pt, AUVIA_PCICONF_JUNK, r); 378 delay(200); 379 380 auvia_waitready_codec(sc); 381 #endif 382 } 383 384 385 int 386 auvia_waitready_codec(struct auvia_softc *sc) 387 { 388 int i; 389 390 /* poll until codec not busy */ 391 for (i = 0; (i < TIMEOUT) && (bus_space_read_4(sc->sc_iot, sc->sc_ioh, 392 AUVIA_CODEC_CTL) & AUVIA_CODEC_BUSY); i++) 393 delay(1); 394 if (i >= TIMEOUT) { 395 printf("%s: codec busy\n", sc->sc_dev.dv_xname); 396 return 1; 397 } 398 399 return 0; 400 } 401 402 403 int 404 auvia_waitvalid_codec(struct auvia_softc *sc) 405 { 406 int i; 407 408 /* poll until codec valid */ 409 for (i = 0; (i < TIMEOUT) && !(bus_space_read_4(sc->sc_iot, sc->sc_ioh, 410 AUVIA_CODEC_CTL) & AUVIA_CODEC_PRIVALID); i++) 411 delay(1); 412 if (i >= TIMEOUT) { 413 printf("%s: codec invalid\n", sc->sc_dev.dv_xname); 414 return 1; 415 } 416 417 return 0; 418 } 419 420 421 int 422 auvia_write_codec(void *addr, u_int8_t reg, u_int16_t val) 423 { 424 struct auvia_softc *sc = addr; 425 426 if (auvia_waitready_codec(sc)) 427 return 1; 428 429 bus_space_write_4(sc->sc_iot, sc->sc_ioh, AUVIA_CODEC_CTL, 430 AUVIA_CODEC_PRIVALID | AUVIA_CODEC_INDEX(reg) | val); 431 432 return 0; 433 } 434 435 436 int 437 auvia_read_codec(void *addr, u_int8_t reg, u_int16_t *val) 438 { 439 struct auvia_softc *sc = addr; 440 441 if (auvia_waitready_codec(sc)) 442 return 1; 443 444 bus_space_write_4(sc->sc_iot, sc->sc_ioh, AUVIA_CODEC_CTL, 445 AUVIA_CODEC_PRIVALID | AUVIA_CODEC_READ | AUVIA_CODEC_INDEX(reg)); 446 447 if (auvia_waitready_codec(sc)) 448 return 1; 449 450 if (auvia_waitvalid_codec(sc)) 451 return 1; 452 453 *val = bus_space_read_2(sc->sc_iot, sc->sc_ioh, AUVIA_CODEC_CTL); 454 455 return 0; 456 } 457 458 459 int 460 auvia_open(void *addr, int flags) 461 { 462 return 0; 463 } 464 465 466 void 467 auvia_close(void *addr) 468 { 469 struct auvia_softc *sc = addr; 470 471 auvia_halt_output(sc); 472 auvia_halt_input(sc); 473 474 sc->sc_play.sc_intr = NULL; 475 sc->sc_record.sc_intr = NULL; 476 } 477 478 479 int 480 auvia_query_encoding(void *addr, struct audio_encoding *fp) 481 { 482 switch (fp->index) { 483 case 0: 484 strcpy(fp->name, AudioEulinear); 485 fp->encoding = AUDIO_ENCODING_ULINEAR; 486 fp->precision = 8; 487 fp->flags = 0; 488 return (0); 489 case 1: 490 strcpy(fp->name, AudioEmulaw); 491 fp->encoding = AUDIO_ENCODING_ULAW; 492 fp->precision = 8; 493 fp->flags = AUDIO_ENCODINGFLAG_EMULATED; 494 return (0); 495 case 2: 496 strcpy(fp->name, AudioEalaw); 497 fp->encoding = AUDIO_ENCODING_ALAW; 498 fp->precision = 8; 499 fp->flags = AUDIO_ENCODINGFLAG_EMULATED; 500 return (0); 501 case 3: 502 strcpy(fp->name, AudioEslinear); 503 fp->encoding = AUDIO_ENCODING_SLINEAR; 504 fp->precision = 8; 505 fp->flags = AUDIO_ENCODINGFLAG_EMULATED; 506 return (0); 507 case 4: 508 strcpy(fp->name, AudioEslinear_le); 509 fp->encoding = AUDIO_ENCODING_SLINEAR_LE; 510 fp->precision = 16; 511 fp->flags = 0; 512 return (0); 513 case 5: 514 strcpy(fp->name, AudioEulinear_le); 515 fp->encoding = AUDIO_ENCODING_ULINEAR_LE; 516 fp->precision = 16; 517 fp->flags = AUDIO_ENCODINGFLAG_EMULATED; 518 return (0); 519 case 6: 520 strcpy(fp->name, AudioEslinear_be); 521 fp->encoding = AUDIO_ENCODING_SLINEAR_BE; 522 fp->precision = 16; 523 fp->flags = AUDIO_ENCODINGFLAG_EMULATED; 524 return (0); 525 case 7: 526 strcpy(fp->name, AudioEulinear_be); 527 fp->encoding = AUDIO_ENCODING_ULINEAR_BE; 528 fp->precision = 16; 529 fp->flags = AUDIO_ENCODINGFLAG_EMULATED; 530 return (0); 531 default: 532 return (EINVAL); 533 } 534 } 535 536 void 537 auvia_set_params_sub(struct auvia_softc *sc, struct auvia_softc_chan *ch, 538 struct audio_params *p) 539 { 540 u_int32_t v; 541 u_int16_t regval; 542 543 if (!(sc->sc_flags & AUVIA_FLAGS_VT8233)) { 544 regval = (p->channels == 2 ? AUVIA_RPMODE_STEREO : 0) 545 | (p->precision * p->factor == 16 ? 546 AUVIA_RPMODE_16BIT : 0) 547 | AUVIA_RPMODE_INTR_FLAG | AUVIA_RPMODE_INTR_EOL 548 | AUVIA_RPMODE_AUTOSTART; 549 ch->sc_reg = regval; 550 } else if (ch->sc_base != VIA8233_MP_BASE) { 551 v = CH_READ4(sc, ch, VIA8233_RP_RATEFMT); 552 v &= ~(VIA8233_RATEFMT_48K | VIA8233_RATEFMT_STEREO 553 | VIA8233_RATEFMT_16BIT); 554 555 v |= VIA8233_RATEFMT_48K * (p->sample_rate / 20) 556 / (48000 / 20); 557 if (p->channels == 2) 558 v |= VIA8233_RATEFMT_STEREO; 559 if (p->precision == 16) 560 v |= VIA8233_RATEFMT_16BIT; 561 562 CH_WRITE4(sc, ch, VIA8233_RP_RATEFMT, v); 563 } else { 564 static const u_int32_t slottab[7] = 565 { 0, 0xff000011, 0xff000021, 0, 566 0xff004321, 0, 0xff436521}; 567 568 regval = (p->hw_precision == 16 569 ? VIA8233_MP_FORMAT_16BIT : VIA8233_MP_FORMAT_8BIT) 570 | (p->hw_channels << 4); 571 CH_WRITE1(sc, ch, VIA8233_OFF_MP_FORMAT, regval); 572 CH_WRITE4(sc, ch, VIA8233_OFF_MP_STOP, slottab[p->hw_channels]); 573 } 574 } 575 576 int 577 auvia_set_params(void *addr, int setmode, int usemode, 578 struct audio_params *play, struct audio_params *rec) 579 { 580 struct auvia_softc *sc = addr; 581 struct auvia_softc_chan *ch; 582 struct audio_params *p; 583 struct ac97_codec_if* codec; 584 int reg, mode; 585 u_int16_t ext_id; 586 587 codec = sc->codec_if; 588 /* for mode in (RECORD, PLAY) */ 589 for (mode = AUMODE_RECORD; mode != -1; 590 mode = mode == AUMODE_RECORD ? AUMODE_PLAY : -1) { 591 if ((setmode & mode) == 0) 592 continue; 593 594 if (mode == AUMODE_PLAY ) { 595 p = play; 596 ch = &sc->sc_play; 597 reg = AC97_REG_PCM_FRONT_DAC_RATE; 598 } else { 599 p = rec; 600 ch = &sc->sc_record; 601 reg = AC97_REG_PCM_LR_ADC_RATE; 602 } 603 604 if (ch->sc_base == VIA8233_MP_BASE && mode == AUMODE_PLAY) { 605 ext_id = codec->vtbl->get_extcaps(codec); 606 if (p->channels == 1) { 607 /* ok */ 608 } else if (p->channels == 2) { 609 /* ok */ 610 } else if (p->channels == 4 611 && ext_id & AC97_EXT_AUDIO_SDAC) { 612 /* ok */ 613 #define BITS_6CH (AC97_EXT_AUDIO_SDAC | AC97_EXT_AUDIO_CDAC | AC97_EXT_AUDIO_LDAC) 614 } else if (p->channels == 6 615 && (ext_id & BITS_6CH) == BITS_6CH) { 616 /* ok */ 617 } else { 618 return (EINVAL); 619 } 620 } else { 621 if (p->channels != 1 && p->channels != 2) 622 return (EINVAL); 623 } 624 if (p->sample_rate < 4000 || p->sample_rate > 48000 || 625 (p->precision != 8 && p->precision != 16)) 626 return (EINVAL); 627 628 if (IS_FIXED_RATE(codec)) { 629 /* Enable aurateconv */ 630 p->hw_sample_rate = AC97_SINGLE_RATE; 631 } else { 632 if (codec->vtbl->set_rate(codec, reg, &p->sample_rate)) 633 return (EINVAL); 634 reg = AC97_REG_PCM_SURR_DAC_RATE; 635 if (p->channels >= 4 636 && codec->vtbl->set_rate(codec, reg, 637 &p->sample_rate)) 638 return (EINVAL); 639 reg = AC97_REG_PCM_LFE_DAC_RATE; 640 if (p->channels == 6 641 && codec->vtbl->set_rate(codec, reg, 642 &p->sample_rate)) 643 return (EINVAL); 644 } 645 646 p->factor = 1; 647 p->sw_code = 0; 648 switch (p->encoding) { 649 case AUDIO_ENCODING_SLINEAR_BE: 650 if (p->precision == 16) { 651 p->sw_code = swap_bytes; 652 p->hw_encoding = AUDIO_ENCODING_SLINEAR_LE; 653 } else { 654 p->sw_code = change_sign8; 655 p->hw_encoding = AUDIO_ENCODING_ULINEAR; 656 } 657 break; 658 case AUDIO_ENCODING_SLINEAR_LE: 659 if (p->precision != 16) { 660 p->sw_code = change_sign8; 661 p->hw_encoding = AUDIO_ENCODING_ULINEAR; 662 } 663 break; 664 case AUDIO_ENCODING_ULINEAR_BE: 665 if (p->precision == 16) { 666 if (mode == AUMODE_PLAY) 667 p->sw_code = swap_bytes_change_sign16_le; 668 else 669 p->sw_code = change_sign16_swap_bytes_le; 670 p->hw_encoding = AUDIO_ENCODING_SLINEAR_LE; 671 } 672 break; 673 case AUDIO_ENCODING_ULINEAR_LE: 674 if (p->precision == 16) { 675 p->sw_code = change_sign16_le; 676 p->hw_encoding = AUDIO_ENCODING_SLINEAR_LE; 677 } 678 break; 679 case AUDIO_ENCODING_ULAW: 680 if (p->precision != 8) 681 return (EINVAL); 682 if (mode == AUMODE_PLAY) { 683 p->factor = 2; 684 p->sw_code = mulaw_to_slinear16_le; 685 p->hw_encoding = AUDIO_ENCODING_SLINEAR_LE; 686 p->hw_precision = 16; 687 } else if (!IS_FIXED_RATE(codec)) { 688 p->sw_code = ulinear8_to_mulaw; 689 p->hw_encoding = AUDIO_ENCODING_ULINEAR; 690 } else { 691 /* aurateconv supports no 8 bit PCM */ 692 p->factor = 2; 693 p->sw_code = slinear16_to_mulaw_le; 694 p->hw_encoding = AUDIO_ENCODING_SLINEAR_LE; 695 p->hw_precision = 16; 696 } 697 break; 698 case AUDIO_ENCODING_ALAW: 699 if (p->precision != 8) 700 return (EINVAL); 701 if (mode == AUMODE_PLAY) { 702 p->factor = 2; 703 p->sw_code = alaw_to_slinear16_le; 704 p->hw_encoding = AUDIO_ENCODING_SLINEAR_LE; 705 p->hw_precision = 16; 706 } else if (!IS_FIXED_RATE(codec)) { 707 p->sw_code = ulinear8_to_alaw; 708 p->hw_encoding = AUDIO_ENCODING_ULINEAR; 709 } else { 710 /* aurateconv supports no 8 bit PCM */ 711 p->factor = 2; 712 p->sw_code = slinear16_to_alaw_le; 713 p->hw_encoding = AUDIO_ENCODING_SLINEAR_LE; 714 p->hw_precision = 16; 715 } 716 break; 717 default: 718 return (EINVAL); 719 } 720 auvia_set_params_sub(sc, ch, p); 721 } 722 723 return 0; 724 } 725 726 727 int 728 auvia_round_blocksize(void *addr, int blk) 729 { 730 return (blk & -32); 731 } 732 733 734 int 735 auvia_halt_output(void *addr) 736 { 737 struct auvia_softc *sc = addr; 738 struct auvia_softc_chan *ch = &(sc->sc_play); 739 740 CH_WRITE1(sc, ch, AUVIA_RP_CONTROL, AUVIA_RPCTRL_TERMINATE); 741 return 0; 742 } 743 744 745 int 746 auvia_halt_input(void *addr) 747 { 748 struct auvia_softc *sc = addr; 749 struct auvia_softc_chan *ch = &(sc->sc_record); 750 751 CH_WRITE1(sc, ch, AUVIA_RP_CONTROL, AUVIA_RPCTRL_TERMINATE); 752 return 0; 753 } 754 755 756 int 757 auvia_getdev(void *addr, struct audio_device *retp) 758 { 759 struct auvia_softc *sc = addr; 760 761 if (retp) { 762 if (sc->sc_flags & AUVIA_FLAGS_VT8233) { 763 strncpy(retp->name, "VIA VT8233/8235", 764 sizeof(retp->name)); 765 } else { 766 strncpy(retp->name, "VIA VT82C686A", 767 sizeof(retp->name)); 768 } 769 strncpy(retp->version, sc->sc_revision, sizeof(retp->version)); 770 strncpy(retp->config, "auvia", sizeof(retp->config)); 771 } 772 773 return 0; 774 } 775 776 777 int 778 auvia_set_port(void *addr, mixer_ctrl_t *cp) 779 { 780 struct auvia_softc *sc = addr; 781 782 return (sc->codec_if->vtbl->mixer_set_port(sc->codec_if, cp)); 783 } 784 785 786 int 787 auvia_get_port(void *addr, mixer_ctrl_t *cp) 788 { 789 struct auvia_softc *sc = addr; 790 791 return (sc->codec_if->vtbl->mixer_get_port(sc->codec_if, cp)); 792 } 793 794 795 int 796 auvia_query_devinfo(void *addr, mixer_devinfo_t *dip) 797 { 798 struct auvia_softc *sc = addr; 799 800 return (sc->codec_if->vtbl->query_devinfo(sc->codec_if, dip)); 801 } 802 803 804 void * 805 auvia_malloc(void *addr, int direction, size_t size, int pool, int flags) 806 { 807 struct auvia_softc *sc = addr; 808 struct auvia_dma *p; 809 int error; 810 int rseg; 811 812 p = malloc(sizeof(*p), pool, flags); 813 if (!p) 814 return 0; 815 816 p->size = size; 817 if ((error = bus_dmamem_alloc(sc->sc_dmat, size, PAGE_SIZE, 0, &p->seg, 818 1, &rseg, BUS_DMA_NOWAIT)) != 0) { 819 printf("%s: unable to allocate dma, error = %d\n", 820 sc->sc_dev.dv_xname, error); 821 goto fail_alloc; 822 } 823 824 if ((error = bus_dmamem_map(sc->sc_dmat, &p->seg, rseg, size, &p->addr, 825 BUS_DMA_NOWAIT | BUS_DMA_COHERENT)) != 0) { 826 printf("%s: unable to map dma, error = %d\n", 827 sc->sc_dev.dv_xname, error); 828 goto fail_map; 829 } 830 831 if ((error = bus_dmamap_create(sc->sc_dmat, size, 1, size, 0, 832 BUS_DMA_NOWAIT, &p->map)) != 0) { 833 printf("%s: unable to create dma map, error = %d\n", 834 sc->sc_dev.dv_xname, error); 835 goto fail_create; 836 } 837 838 if ((error = bus_dmamap_load(sc->sc_dmat, p->map, p->addr, size, NULL, 839 BUS_DMA_NOWAIT)) != 0) { 840 printf("%s: unable to load dma map, error = %d\n", 841 sc->sc_dev.dv_xname, error); 842 goto fail_load; 843 } 844 845 p->next = sc->sc_dmas; 846 sc->sc_dmas = p; 847 848 return p->addr; 849 850 851 fail_load: 852 bus_dmamap_destroy(sc->sc_dmat, p->map); 853 fail_create: 854 bus_dmamem_unmap(sc->sc_dmat, p->addr, size); 855 fail_map: 856 bus_dmamem_free(sc->sc_dmat, &p->seg, 1); 857 fail_alloc: 858 free(p, pool); 859 return 0; 860 } 861 862 863 void 864 auvia_free(void *addr, void *ptr, int pool) 865 { 866 struct auvia_softc *sc = addr; 867 struct auvia_dma **pp, *p; 868 869 for (pp = &(sc->sc_dmas); (p = *pp) != NULL; pp = &p->next) 870 if (p->addr == ptr) { 871 bus_dmamap_unload(sc->sc_dmat, p->map); 872 bus_dmamap_destroy(sc->sc_dmat, p->map); 873 bus_dmamem_unmap(sc->sc_dmat, p->addr, p->size); 874 bus_dmamem_free(sc->sc_dmat, &p->seg, 1); 875 876 *pp = p->next; 877 free(p, pool); 878 return; 879 } 880 881 panic("auvia_free: trying to free unallocated memory"); 882 } 883 884 885 size_t 886 auvia_round_buffersize(void *addr, int direction, size_t size) 887 { 888 return size; 889 } 890 891 892 paddr_t 893 auvia_mappage(void *addr, void *mem, off_t off, int prot) 894 { 895 struct auvia_softc *sc = addr; 896 struct auvia_dma *p; 897 898 if (off < 0) 899 return -1; 900 901 for (p = sc->sc_dmas; p && p->addr != mem; p = p->next) 902 ; 903 904 if (!p) 905 return -1; 906 907 return bus_dmamem_mmap(sc->sc_dmat, &p->seg, 1, off, prot, 908 BUS_DMA_WAITOK); 909 } 910 911 912 int 913 auvia_get_props(void *addr) 914 { 915 struct auvia_softc *sc = addr; 916 int props; 917 918 props = AUDIO_PROP_INDEPENDENT | AUDIO_PROP_FULLDUPLEX; 919 /* 920 * Even if the codec is fixed-rate, set_param() succeeds for any sample 921 * rate because of aurateconv. Applications can't know what rate the 922 * device can process in the case of mmap(). 923 */ 924 if (!IS_FIXED_RATE(sc->codec_if)) 925 props |= AUDIO_PROP_MMAP; 926 return props; 927 } 928 929 930 int 931 auvia_build_dma_ops(struct auvia_softc *sc, struct auvia_softc_chan *ch, 932 struct auvia_dma *p, void *start, void *end, int blksize) 933 { 934 struct auvia_dma_op *op; 935 struct auvia_dma *dp; 936 bus_addr_t s, e; 937 size_t l; 938 int segs; 939 940 s = p->map->dm_segs[0].ds_addr; 941 l = ((char *)end - (char *)start); 942 e = s + l; 943 segs = (l + blksize - 1) / blksize; 944 945 if (segs > (ch->sc_dma_op_count)) { 946 /* if old list was too small, free it */ 947 if (ch->sc_dma_ops) { 948 auvia_free(sc, ch->sc_dma_ops, M_DEVBUF); 949 } 950 951 ch->sc_dma_ops = auvia_malloc(sc, 0, 952 sizeof(struct auvia_dma_op) * segs, M_DEVBUF, M_WAITOK); 953 954 if (ch->sc_dma_ops == NULL) { 955 printf("%s: couldn't build dmaops\n", sc->sc_dev.dv_xname); 956 return 1; 957 } 958 959 for (dp = sc->sc_dmas; 960 dp && dp->addr != (void *)(ch->sc_dma_ops); 961 dp = dp->next) 962 ; 963 964 if (!dp) 965 panic("%s: build_dma_ops: where'd my memory go??? " 966 "address (%p)\n", sc->sc_dev.dv_xname, 967 ch->sc_dma_ops); 968 969 ch->sc_dma_op_count = segs; 970 ch->sc_dma_ops_dma = dp; 971 } 972 973 dp = ch->sc_dma_ops_dma; 974 op = ch->sc_dma_ops; 975 976 while (l) { 977 op->ptr = s; 978 l = l - blksize; 979 if (!l) { 980 /* if last block */ 981 op->flags = AUVIA_DMAOP_EOL | blksize; 982 } else { 983 op->flags = AUVIA_DMAOP_FLAG | blksize; 984 } 985 s += blksize; 986 op++; 987 } 988 989 return 0; 990 } 991 992 993 int 994 auvia_trigger_output(void *addr, void *start, void *end, 995 int blksize, void (*intr)(void *), void *arg, 996 struct audio_params *param) 997 { 998 struct auvia_softc *sc = addr; 999 struct auvia_softc_chan *ch = &(sc->sc_play); 1000 struct auvia_dma *p; 1001 1002 for (p = sc->sc_dmas; p && p->addr != start; p = p->next) 1003 ; 1004 1005 if (!p) 1006 panic("auvia_trigger_output: request with bad start " 1007 "address (%p)", start); 1008 1009 if (auvia_build_dma_ops(sc, ch, p, start, end, blksize)) { 1010 return 1; 1011 } 1012 1013 ch->sc_intr = intr; 1014 ch->sc_arg = arg; 1015 1016 CH_WRITE4(sc, ch, AUVIA_RP_DMAOPS_BASE, 1017 ch->sc_dma_ops_dma->map->dm_segs[0].ds_addr); 1018 1019 if (sc->sc_flags & AUVIA_FLAGS_VT8233) { 1020 if (ch->sc_base != VIA8233_MP_BASE) { 1021 CH_WRITE1(sc, ch, VIA8233_RP_DXS_LVOL, 0); 1022 CH_WRITE1(sc, ch, VIA8233_RP_DXS_RVOL, 0); 1023 } 1024 CH_WRITE1(sc, ch, AUVIA_RP_CONTROL, 1025 AUVIA_RPCTRL_START | AUVIA_RPCTRL_AUTOSTART | 1026 AUVIA_RPCTRL_STOP | AUVIA_RPCTRL_EOL | AUVIA_RPCTRL_FLAG); 1027 } else { 1028 CH_WRITE1(sc, ch, AUVIA_RP_MODE, ch->sc_reg); 1029 CH_WRITE1(sc, ch, AUVIA_RP_CONTROL, AUVIA_RPCTRL_START); 1030 } 1031 1032 return 0; 1033 } 1034 1035 1036 int 1037 auvia_trigger_input(void *addr, void *start, void *end, 1038 int blksize, void (*intr)(void *), void *arg, 1039 struct audio_params *param) 1040 { 1041 struct auvia_softc *sc = addr; 1042 struct auvia_softc_chan *ch = &(sc->sc_record); 1043 struct auvia_dma *p; 1044 1045 for (p = sc->sc_dmas; p && p->addr != start; p = p->next) 1046 ; 1047 1048 if (!p) 1049 panic("auvia_trigger_input: request with bad start " 1050 "address (%p)", start); 1051 1052 if (auvia_build_dma_ops(sc, ch, p, start, end, blksize)) { 1053 return 1; 1054 } 1055 1056 ch->sc_intr = intr; 1057 ch->sc_arg = arg; 1058 1059 CH_WRITE4(sc, ch, AUVIA_RP_DMAOPS_BASE, 1060 ch->sc_dma_ops_dma->map->dm_segs[0].ds_addr); 1061 1062 if (sc->sc_flags & AUVIA_FLAGS_VT8233) { 1063 CH_WRITE1(sc, ch, VIA8233_RP_DXS_LVOL, 0); 1064 CH_WRITE1(sc, ch, VIA8233_RP_DXS_RVOL, 0); 1065 CH_WRITE1(sc, ch, AUVIA_RP_CONTROL, 1066 AUVIA_RPCTRL_START | AUVIA_RPCTRL_AUTOSTART | 1067 AUVIA_RPCTRL_STOP | AUVIA_RPCTRL_EOL | AUVIA_RPCTRL_FLAG); 1068 } else { 1069 CH_WRITE1(sc, ch, AUVIA_RP_MODE, ch->sc_reg); 1070 CH_WRITE1(sc, ch, AUVIA_RP_CONTROL, AUVIA_RPCTRL_START); 1071 } 1072 1073 return 0; 1074 } 1075 1076 1077 int 1078 auvia_intr(void *arg) 1079 { 1080 struct auvia_softc *sc = arg; 1081 struct auvia_softc_chan *ch; 1082 u_int8_t r; 1083 int rval; 1084 1085 rval = 0; 1086 1087 ch = &sc->sc_record; 1088 r = CH_READ1(sc, ch, AUVIA_RP_STAT); 1089 if (r & AUVIA_RPSTAT_INTR) { 1090 if (sc->sc_record.sc_intr) 1091 sc->sc_record.sc_intr(sc->sc_record.sc_arg); 1092 1093 /* clear interrupts */ 1094 CH_WRITE1(sc, ch, AUVIA_RP_STAT, AUVIA_RPSTAT_INTR); 1095 rval = 1; 1096 } 1097 1098 ch = &sc->sc_play; 1099 r = CH_READ1(sc, ch, AUVIA_RP_STAT); 1100 if (r & AUVIA_RPSTAT_INTR) { 1101 if (sc->sc_play.sc_intr) 1102 sc->sc_play.sc_intr(sc->sc_play.sc_arg); 1103 1104 /* clear interrupts */ 1105 CH_WRITE1(sc, ch, AUVIA_RP_STAT, AUVIA_RPSTAT_INTR); 1106 rval = 1; 1107 } 1108 1109 return rval; 1110 } 1111