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