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