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