1 /* $NetBSD: auixp.c,v 1.46 2019/05/08 13:40:18 isaki Exp $ */ 2 3 /* 4 * Copyright (c) 2004, 2005 Reinoud Zandijk <reinoud@netbsd.org> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. The name of the author may not be used to endorse or promote products 13 * derived from this software without specific prior written permission. 14 * 3. All advertising materials mentioning features or use of this software 15 * must display the following acknowledgement: 16 * This product includes software developed by the NetBSD 17 * Foundation, Inc. and its contributors. 18 * 4. Neither the name of The NetBSD Foundation nor the names of its 19 * contributors may be used to endorse or promote products derived 20 * from this software without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 */ 34 35 36 /* 37 * NetBSD audio driver for ATI IXP-{150,200,...} audio driver hardware. 38 * 39 * Recording and playback has been tested OK on various sample rates and 40 * encodings. 41 * 42 * Known problems and issues : 43 * - SPDIF is untested and needs some work still (LED stays off) 44 * - 32 bit audio playback failed last time i tried but that might an AC'97 45 * codec support problem. 46 * - 32 bit recording works but can't try out playing: see above. 47 * - no suspend/resume support yet. 48 * - multiple codecs are `supported' but not tested; the implemetation needs 49 * some cleaning up. 50 */ 51 52 #include <sys/cdefs.h> 53 __KERNEL_RCSID(0, "$NetBSD: auixp.c,v 1.46 2019/05/08 13:40:18 isaki Exp $"); 54 55 #include <sys/types.h> 56 #include <sys/errno.h> 57 #include <sys/null.h> 58 #include <sys/param.h> 59 #include <sys/systm.h> 60 #include <sys/kmem.h> 61 #include <sys/device.h> 62 #include <sys/conf.h> 63 #include <sys/exec.h> 64 #include <sys/select.h> 65 #include <sys/audioio.h> 66 #include <sys/queue.h> 67 #include <sys/bus.h> 68 #include <sys/intr.h> 69 70 #include <dev/audio/audio_if.h> 71 72 #include <dev/ic/ac97var.h> 73 #include <dev/ic/ac97reg.h> 74 75 #include <dev/pci/pcidevs.h> 76 #include <dev/pci/pcivar.h> 77 #include <dev/pci/auixpreg.h> 78 #include <dev/pci/auixpvar.h> 79 80 81 /* #define DEBUG_AUIXP */ 82 83 84 /* why isn't this base address register not in the headerfile? */ 85 #define PCI_CBIO 0x10 86 87 88 /* macro's used */ 89 #define KERNADDR(p) ((void *)((p)->addr)) 90 #define DMAADDR(p) ((p)->map->dm_segs[0].ds_addr) 91 92 93 /* the differences might be irrelevant */ 94 enum { 95 IXP_200, 96 IXP_300, 97 IXP_400 98 }; 99 100 101 /* our `cards' */ 102 static const struct auixp_card_type { 103 uint16_t pci_vendor_id; 104 uint16_t pci_product_id; 105 int type; 106 } auixp_card_types[] = { 107 { PCI_VENDOR_ATI, PCI_PRODUCT_ATI_IXP_AUDIO_200, IXP_200 }, 108 { PCI_VENDOR_ATI, PCI_PRODUCT_ATI_IXP_AUDIO_300, IXP_300 }, 109 { PCI_VENDOR_ATI, PCI_PRODUCT_ATI_IXP_AUDIO_400, IXP_400 }, 110 { 0, 0, 0 } 111 }; 112 113 114 struct audio_device auixp_device = { 115 "ATI IXP audio", 116 "", 117 "auixp" 118 }; 119 120 /* 121 * current AC'97 driver only supports SPDIF outputting channel 3&4 i.e. STEREO 122 */ 123 #define AUIXP_FORMAT(aumode, ch, chmask) \ 124 { \ 125 .mode = (aumode), \ 126 .encoding = AUDIO_ENCODING_SLINEAR_LE, \ 127 .validbits = 16, \ 128 .precision = 16, \ 129 .channels = (ch), \ 130 .channel_mask = (chmask), \ 131 .frequency_type = 0, \ 132 .frequency = { 7000, 48000 }, \ 133 } 134 static const struct audio_format auixp_formats[AUIXP_NFORMATS] = { 135 AUIXP_FORMAT(AUMODE_PLAY | AUMODE_RECORD, 2, AUFMT_STEREO), 136 AUIXP_FORMAT(AUMODE_PLAY , 4, AUFMT_SURROUND4), 137 AUIXP_FORMAT(AUMODE_PLAY , 6, AUFMT_DOLBY_5_1), 138 }; 139 140 /* codec detection constant indicating the interrupt flags */ 141 #define ALL_CODECS_NOT_READY \ 142 (ATI_REG_ISR_CODEC0_NOT_READY |\ 143 ATI_REG_ISR_CODEC1_NOT_READY |\ 144 ATI_REG_ISR_CODEC2_NOT_READY) 145 #define CODEC_CHECK_BITS (ALL_CODECS_NOT_READY|ATI_REG_ISR_NEW_FRAME) 146 147 148 /* autoconfig */ 149 static int auixp_match(device_t, cfdata_t, void *); 150 static void auixp_attach(device_t, device_t, void *); 151 static int auixp_detach(device_t, int); 152 153 154 /* audio(9) function prototypes */ 155 static int auixp_query_format(void *, audio_format_query_t *); 156 static int auixp_set_format(void *, int, 157 const audio_params_t *, const audio_params_t *, 158 audio_filter_reg_t *, audio_filter_reg_t *); 159 static int auixp_commit_settings(void *); 160 static int auixp_round_blocksize(void *, int, int, const audio_params_t *); 161 static int auixp_trigger_output(void *, void *, void *, int, 162 void (*)(void *), 163 void *, const audio_params_t *); 164 static int auixp_trigger_input(void *, void *, void *, int, 165 void (*)(void *), 166 void *, const audio_params_t *); 167 static int auixp_halt_output(void *); 168 static int auixp_halt_input(void *); 169 static int auixp_set_port(void *, mixer_ctrl_t *); 170 static int auixp_get_port(void *, mixer_ctrl_t *); 171 static int auixp_query_devinfo(void *, mixer_devinfo_t *); 172 static void * auixp_malloc(void *, int, size_t); 173 static void auixp_free(void *, void *, size_t); 174 static int auixp_getdev(void *, struct audio_device *); 175 static size_t auixp_round_buffersize(void *, int, size_t); 176 static int auixp_get_props(void *); 177 static int auixp_intr(void *); 178 static int auixp_allocmem(struct auixp_softc *, size_t, size_t, 179 struct auixp_dma *); 180 static int auixp_freemem(struct auixp_softc *, struct auixp_dma *); 181 182 /* Supporting subroutines */ 183 static int auixp_init(struct auixp_softc *); 184 static void auixp_autodetect_codecs(struct auixp_softc *); 185 static void auixp_post_config(device_t); 186 187 static void auixp_reset_aclink(struct auixp_softc *); 188 static int auixp_attach_codec(void *, struct ac97_codec_if *); 189 static int auixp_read_codec(void *, uint8_t, uint16_t *); 190 static int auixp_write_codec(void *, uint8_t, uint16_t); 191 static int auixp_wait_for_codecs(struct auixp_softc *, const char *); 192 static int auixp_reset_codec(void *); 193 static enum ac97_host_flags auixp_flags_codec(void *); 194 195 static void auixp_enable_dma(struct auixp_softc *, struct auixp_dma *); 196 static void auixp_disable_dma(struct auixp_softc *, struct auixp_dma *); 197 static void auixp_enable_interrupts(struct auixp_softc *); 198 static void auixp_disable_interrupts(struct auixp_softc *); 199 200 201 /* statics */ 202 static void auixp_link_daisychain(struct auixp_softc *, 203 struct auixp_dma *, struct auixp_dma *, 204 int, int); 205 static int auixp_allocate_dma_chain(struct auixp_softc *, 206 struct auixp_dma **); 207 static void auixp_program_dma_chain(struct auixp_softc *, 208 struct auixp_dma *); 209 static void auixp_dma_update(struct auixp_softc *, struct auixp_dma *); 210 static void auixp_update_busbusy(struct auixp_softc *); 211 static void auixp_get_locks(void *, kmutex_t **, kmutex_t **); 212 213 static bool auixp_resume(device_t, const pmf_qual_t *); 214 215 216 #ifdef DEBUG_AUIXP 217 static struct auixp_softc *static_sc; 218 static void auixp_dumpreg(void) __unused; 219 # define DPRINTF(x) printf x; 220 #else 221 # define DPRINTF(x) 222 #endif 223 224 225 static const struct audio_hw_if auixp_hw_if = { 226 .query_format = auixp_query_format, 227 .set_format = auixp_set_format, 228 .round_blocksize = auixp_round_blocksize, 229 .commit_settings = auixp_commit_settings, 230 .halt_output = auixp_halt_output, 231 .halt_input = auixp_halt_input, 232 .getdev = auixp_getdev, 233 .set_port = auixp_set_port, 234 .get_port = auixp_get_port, 235 .query_devinfo = auixp_query_devinfo, 236 .allocm = auixp_malloc, 237 .freem = auixp_free, 238 .round_buffersize = auixp_round_buffersize, 239 .get_props = auixp_get_props, 240 .trigger_output = auixp_trigger_output, 241 .trigger_input = auixp_trigger_input, 242 .get_locks = auixp_get_locks, 243 }; 244 245 246 CFATTACH_DECL_NEW(auixp, sizeof(struct auixp_softc), auixp_match, auixp_attach, 247 auixp_detach, NULL); 248 249 250 /* 251 * audio(9) functions 252 */ 253 254 static int 255 auixp_query_format(void *hdl, audio_format_query_t *afp) 256 { 257 struct auixp_codec *co; 258 struct auixp_softc *sc; 259 260 co = (struct auixp_codec *) hdl; 261 sc = co->sc; 262 return audio_query_format(sc->sc_formats, AUIXP_NFORMATS, afp); 263 } 264 265 266 static int 267 auixp_set_rate(struct auixp_codec *co, int mode, u_int srate) 268 { 269 int ret; 270 u_int ratetmp; 271 272 ratetmp = srate; 273 if (mode == AUMODE_RECORD) { 274 ret = co->codec_if->vtbl->set_rate(co->codec_if, 275 AC97_REG_PCM_LR_ADC_RATE, &ratetmp); 276 return ret; 277 } 278 279 /* play mode */ 280 ret = co->codec_if->vtbl->set_rate(co->codec_if, 281 AC97_REG_PCM_FRONT_DAC_RATE, &ratetmp); 282 if (ret) 283 return ret; 284 285 ratetmp = srate; 286 ret = co->codec_if->vtbl->set_rate(co->codec_if, 287 AC97_REG_PCM_SURR_DAC_RATE, &ratetmp); 288 if (ret) 289 return ret; 290 291 ratetmp = srate; 292 ret = co->codec_if->vtbl->set_rate(co->codec_if, 293 AC97_REG_PCM_LFE_DAC_RATE, &ratetmp); 294 return ret; 295 } 296 297 298 /* commit setting and program ATI IXP chip */ 299 static int 300 auixp_commit_settings(void *hdl) 301 { 302 struct auixp_codec *co; 303 struct auixp_softc *sc; 304 bus_space_tag_t iot; 305 bus_space_handle_t ioh; 306 struct audio_params *params; 307 uint32_t value; 308 309 /* XXX would it be better to stop interrupts first? XXX */ 310 co = (struct auixp_codec *) hdl; 311 sc = co->sc; 312 iot = sc->sc_iot; 313 ioh = sc->sc_ioh; 314 315 /* process input settings */ 316 params = &sc->sc_play_params; 317 318 /* set input interleaving (precision) */ 319 value = bus_space_read_4(iot, ioh, ATI_REG_CMD); 320 value &= ~ATI_REG_CMD_INTERLEAVE_IN; 321 if (params->precision <= 16) 322 value |= ATI_REG_CMD_INTERLEAVE_IN; 323 bus_space_write_4(iot, ioh, ATI_REG_CMD, value); 324 325 /* process output settings */ 326 params = &sc->sc_play_params; 327 328 value = bus_space_read_4(iot, ioh, ATI_REG_OUT_DMA_SLOT); 329 value &= ~ATI_REG_OUT_DMA_SLOT_MASK; 330 331 /* TODO SPDIF case for 8 channels */ 332 switch (params->channels) { 333 case 6: 334 value |= ATI_REG_OUT_DMA_SLOT_BIT(7) | 335 ATI_REG_OUT_DMA_SLOT_BIT(8); 336 /* fallthru */ 337 case 4: 338 value |= ATI_REG_OUT_DMA_SLOT_BIT(6) | 339 ATI_REG_OUT_DMA_SLOT_BIT(9); 340 /* fallthru */ 341 default: 342 value |= ATI_REG_OUT_DMA_SLOT_BIT(3) | 343 ATI_REG_OUT_DMA_SLOT_BIT(4); 344 break; 345 } 346 /* set output threshold */ 347 value |= 0x04 << ATI_REG_OUT_DMA_THRESHOLD_SHIFT; 348 bus_space_write_4(iot, ioh, ATI_REG_OUT_DMA_SLOT, value); 349 350 /* set output interleaving (precision) */ 351 value = bus_space_read_4(iot, ioh, ATI_REG_CMD); 352 value &= ~ATI_REG_CMD_INTERLEAVE_OUT; 353 if (params->precision <= 16) 354 value |= ATI_REG_CMD_INTERLEAVE_OUT; 355 bus_space_write_4(iot, ioh, ATI_REG_CMD, value); 356 357 /* enable 6 channel reordering */ 358 value = bus_space_read_4(iot, ioh, ATI_REG_6CH_REORDER); 359 value &= ~ATI_REG_6CH_REORDER_EN; 360 if (params->channels == 6) 361 value |= ATI_REG_6CH_REORDER_EN; 362 bus_space_write_4(iot, ioh, ATI_REG_6CH_REORDER, value); 363 364 if (sc->has_spdif) { 365 /* set SPDIF (if present) */ 366 value = bus_space_read_4(iot, ioh, ATI_REG_CMD); 367 value &= ~ATI_REG_CMD_SPDF_CONFIG_MASK; 368 value |= ATI_REG_CMD_SPDF_CONFIG_34; /* NetBSD AC'97 default */ 369 370 /* XXX this prolly is not nessisary unless splitted XXX */ 371 value &= ~ATI_REG_CMD_INTERLEAVE_SPDF; 372 if (params->precision <= 16) 373 value |= ATI_REG_CMD_INTERLEAVE_SPDF; 374 bus_space_write_4(iot, ioh, ATI_REG_CMD, value); 375 } 376 377 return 0; 378 } 379 380 381 /* set audio properties in desired setting */ 382 static int 383 auixp_set_format(void *hdl, int setmode, 384 const audio_params_t *play, const audio_params_t *rec, 385 audio_filter_reg_t *pfil, audio_filter_reg_t *rfil) 386 { 387 struct auixp_codec *co; 388 struct auixp_softc *sc; 389 const audio_params_t *params; 390 int mode, index; 391 392 /* 393 * In current NetBSD AC'97 implementation, SPDF is linked to channel 3 394 * and 4 i.e. stereo output. 395 */ 396 397 co = (struct auixp_codec *) hdl; 398 sc = co->sc; 399 for (mode = AUMODE_RECORD; mode != -1; 400 mode = (mode == AUMODE_RECORD) ? AUMODE_PLAY : -1) { 401 if ((setmode & mode) == 0) 402 continue; 403 404 params = (mode == AUMODE_PLAY) ? play : rec; 405 if (params == NULL) 406 continue; 407 408 index = audio_indexof_format(sc->sc_formats, AUIXP_NFORMATS, 409 mode, params); 410 411 /* if variable speed and we can't set the desired rate, fail */ 412 if ((sc->sc_formats[index].frequency_type != 1) && 413 auixp_set_rate(co, mode, params->sample_rate)) 414 return EINVAL; 415 416 /* preserve the settings */ 417 if (mode == AUMODE_PLAY) 418 sc->sc_play_params = *params; 419 if (mode == AUMODE_RECORD) 420 sc->sc_rec_params = *params; 421 } 422 423 return 0; 424 } 425 426 427 /* called to translate a requested blocksize to a hw-possible one */ 428 static int 429 auixp_round_blocksize(void *hdl, int bs, int mode, 430 const audio_params_t *param) 431 { 432 uint32_t new_bs; 433 434 new_bs = bs; 435 /* Be conservative; align to 32 bytes and maximise it to 64 kb */ 436 /* 256 kb possible */ 437 if (new_bs > 0x10000) 438 bs = 0x10000; /* 64 kb max */ 439 new_bs = (bs & ~0x20); /* 32 bytes align */ 440 441 return new_bs; 442 } 443 444 445 /* 446 * allocate dma capable memory and record its information for later retrieval 447 * when we program the dma chain itself. The trigger routines passes on the 448 * kernel virtual address we return here as a reference to the mapping. 449 */ 450 static void * 451 auixp_malloc(void *hdl, int direction, size_t size) 452 { 453 struct auixp_codec *co; 454 struct auixp_softc *sc; 455 struct auixp_dma *dma; 456 int error; 457 458 co = (struct auixp_codec *) hdl; 459 sc = co->sc; 460 /* get us a auixp_dma structure */ 461 dma = kmem_alloc(sizeof(*dma), KM_SLEEP); 462 463 /* get us a dma buffer itself */ 464 error = auixp_allocmem(sc, size, 16, dma); 465 if (error) { 466 kmem_free(dma, sizeof(*dma)); 467 aprint_error_dev(sc->sc_dev, "auixp_malloc: not enough memory\n"); 468 469 return NULL; 470 } 471 SLIST_INSERT_HEAD(&sc->sc_dma_list, dma, dma_chain); 472 473 DPRINTF(("auixp_malloc: returning kern %p, hw 0x%08x for %zd bytes " 474 "in %d segs\n", KERNADDR(dma), (uint32_t) DMAADDR(dma), dma->size, 475 dma->nsegs) 476 ); 477 478 return KERNADDR(dma); 479 } 480 481 482 /* 483 * free and release dma capable memory we allocated before and remove its 484 * recording 485 */ 486 static void 487 auixp_free(void *hdl, void *addr, size_t size) 488 { 489 struct auixp_codec *co; 490 struct auixp_softc *sc; 491 struct auixp_dma *dma; 492 493 co = (struct auixp_codec *) hdl; 494 sc = co->sc; 495 SLIST_FOREACH(dma, &sc->sc_dma_list, dma_chain) { 496 if (KERNADDR(dma) == addr) { 497 SLIST_REMOVE(&sc->sc_dma_list, dma, auixp_dma, 498 dma_chain); 499 auixp_freemem(sc, dma); 500 kmem_free(dma, sizeof(*dma)); 501 return; 502 } 503 } 504 } 505 506 507 static int 508 auixp_getdev(void *hdl, struct audio_device *ret) 509 { 510 511 *ret = auixp_device; 512 return 0; 513 } 514 515 516 /* pass request to AC'97 codec code */ 517 static int 518 auixp_set_port(void *hdl, mixer_ctrl_t *mc) 519 { 520 struct auixp_codec *co; 521 522 co = (struct auixp_codec *) hdl; 523 return co->codec_if->vtbl->mixer_set_port(co->codec_if, mc); 524 } 525 526 527 /* pass request to AC'97 codec code */ 528 static int 529 auixp_get_port(void *hdl, mixer_ctrl_t *mc) 530 { 531 struct auixp_codec *co; 532 533 co = (struct auixp_codec *) hdl; 534 return co->codec_if->vtbl->mixer_get_port(co->codec_if, mc); 535 } 536 537 /* pass request to AC'97 codec code */ 538 static int 539 auixp_query_devinfo(void *hdl, mixer_devinfo_t *di) 540 { 541 struct auixp_codec *co; 542 543 co = (struct auixp_codec *) hdl; 544 return co->codec_if->vtbl->query_devinfo(co->codec_if, di); 545 } 546 547 548 static size_t 549 auixp_round_buffersize(void *hdl, int direction, 550 size_t bufsize) 551 { 552 553 /* XXX force maximum? i.e. 256 kb? */ 554 return bufsize; 555 } 556 557 558 static int 559 auixp_get_props(void *hdl) 560 { 561 562 return AUDIO_PROP_MMAP | AUDIO_PROP_INDEPENDENT | AUDIO_PROP_FULLDUPLEX; 563 } 564 565 566 /* 567 * A dma descriptor has dma->nsegs segments defined in dma->segs set up when 568 * we claimed the memory. 569 * 570 * Due to our demand for one contiguous DMA area, we only have one segment. A 571 * c_dma structure is about 3 kb for the 256 entries we maximally program 572 * -arbitrary limit AFAIK- so all is most likely to be in one segment/page 573 * anyway. 574 * 575 * XXX ought to implement fragmented dma area XXX 576 * 577 * Note that _v variables depict kernel virtual addresses, _p variables depict 578 * physical addresses. 579 */ 580 static void 581 auixp_link_daisychain(struct auixp_softc *sc, 582 struct auixp_dma *c_dma, struct auixp_dma *s_dma, 583 int blksize, int blocks) 584 { 585 atiixp_dma_desc_t *caddr_v, *next_caddr_v; 586 uint32_t caddr_p, next_caddr_p, saddr_p; 587 int i; 588 589 /* just make sure we are not changing when its running */ 590 auixp_disable_dma(sc, c_dma); 591 592 /* setup dma chain start addresses */ 593 caddr_v = KERNADDR(c_dma); 594 caddr_p = DMAADDR(c_dma); 595 saddr_p = DMAADDR(s_dma); 596 597 /* program the requested number of blocks */ 598 for (i = 0; i < blocks; i++) { 599 /* clear the block just in case */ 600 memset(caddr_v, 0, sizeof(atiixp_dma_desc_t)); 601 602 /* round robin the chain dma addresses for its successor */ 603 next_caddr_v = caddr_v + 1; 604 next_caddr_p = caddr_p + sizeof(atiixp_dma_desc_t); 605 606 if (i == blocks-1) { 607 next_caddr_v = KERNADDR(c_dma); 608 next_caddr_p = DMAADDR(c_dma); 609 } 610 611 /* fill in the hardware dma chain descriptor in little-endian */ 612 caddr_v->addr = htole32(saddr_p); 613 caddr_v->status = htole16(0); 614 caddr_v->size = htole16((blksize >> 2)); /* in dwords (!!!) */ 615 caddr_v->next = htole32(next_caddr_p); 616 617 /* advance slot */ 618 saddr_p += blksize; /* XXX assuming contiguous XXX */ 619 caddr_v = next_caddr_v; 620 caddr_p = next_caddr_p; 621 } 622 } 623 624 625 static int 626 auixp_allocate_dma_chain(struct auixp_softc *sc, struct auixp_dma **dmap) 627 { 628 struct auixp_dma *dma; 629 int error; 630 631 /* allocate keeper of dma area */ 632 *dmap = NULL; 633 dma = kmem_zalloc(sizeof(struct auixp_dma), KM_SLEEP); 634 635 /* allocate for daisychain of IXP hardware-dma descriptors */ 636 error = auixp_allocmem(sc, DMA_DESC_CHAIN * sizeof(atiixp_dma_desc_t), 637 16, dma); 638 if (error) { 639 aprint_error_dev(sc->sc_dev, "can't malloc dma descriptor chain\n"); 640 kmem_free(dma, sizeof(*dma)); 641 return ENOMEM; 642 } 643 644 /* return info and initialise structure */ 645 dma->intr = NULL; 646 dma->intrarg = NULL; 647 648 *dmap = dma; 649 return 0; 650 } 651 652 653 /* program dma chain in its link address descriptor */ 654 static void 655 auixp_program_dma_chain(struct auixp_softc *sc, struct auixp_dma *dma) 656 { 657 bus_space_tag_t iot; 658 bus_space_handle_t ioh; 659 uint32_t value; 660 661 iot = sc->sc_iot; 662 ioh = sc->sc_ioh; 663 /* get hardware start address of DMA chain and set valid-flag in it */ 664 /* XXX always at start? XXX */ 665 value = DMAADDR(dma); 666 value = value | ATI_REG_LINKPTR_EN; 667 668 /* reset linkpointer */ 669 bus_space_write_4(iot, ioh, dma->linkptr, 0); 670 671 /* reset this DMA engine */ 672 auixp_disable_dma(sc, dma); 673 auixp_enable_dma(sc, dma); 674 675 /* program new DMA linkpointer */ 676 bus_space_write_4(iot, ioh, dma->linkptr, value); 677 } 678 679 680 /* called from interrupt code to signal end of one dma-slot */ 681 static void 682 auixp_dma_update(struct auixp_softc *sc, struct auixp_dma *dma) 683 { 684 685 /* be very paranoid */ 686 if (!dma) 687 panic("%s: update: dma = NULL", device_xname(sc->sc_dev)); 688 if (!dma->intr) 689 panic("%s: update: dma->intr = NULL", device_xname(sc->sc_dev)); 690 691 /* request more input from upper layer */ 692 (*dma->intr)(dma->intrarg); 693 } 694 695 696 /* 697 * The magic `busbusy' bit that needs to be set when dma is active; allowing 698 * busmastering? 699 */ 700 static void 701 auixp_update_busbusy(struct auixp_softc *sc) 702 { 703 bus_space_tag_t iot; 704 bus_space_handle_t ioh; 705 uint32_t value; 706 int running; 707 708 iot = sc->sc_iot; 709 ioh = sc->sc_ioh; 710 /* set bus-busy flag when either recording or playing is performed */ 711 value = bus_space_read_4(iot, ioh, ATI_REG_IER); 712 value &= ~ATI_REG_IER_SET_BUS_BUSY; 713 714 running = ((sc->sc_output_dma->running) || (sc->sc_input_dma->running)); 715 if (running) 716 value |= ATI_REG_IER_SET_BUS_BUSY; 717 718 bus_space_write_4(iot, ioh, ATI_REG_IER, value); 719 720 } 721 722 723 /* 724 * Called from upper audio layer to request playing audio, only called once; 725 * audio is refilled by calling the intr() function when space is available 726 * again. 727 */ 728 /* XXX allmost literaly a copy of trigger-input; could be factorised XXX */ 729 static int 730 auixp_trigger_output(void *hdl, void *start, void *end, int blksize, 731 void (*intr)(void *), void *intrarg, const audio_params_t *param) 732 { 733 struct auixp_codec *co; 734 struct auixp_softc *sc; 735 struct auixp_dma *chain_dma; 736 struct auixp_dma *sound_dma; 737 uint32_t blocks; 738 739 co = (struct auixp_codec *) hdl; 740 sc = co->sc; 741 chain_dma = sc->sc_output_dma; 742 /* add functions to call back */ 743 chain_dma->intr = intr; 744 chain_dma->intrarg = intrarg; 745 746 /* 747 * Program output DMA chain with blocks from [start...end] with 748 * blksize fragments. 749 * 750 * NOTE, we can assume its in one block since we asked for it to be in 751 * one contiguous blob; XXX change this? XXX 752 */ 753 blocks = (size_t) (((char *) end) - ((char *) start)) / blksize; 754 755 /* lookup `start' address in our list of DMA area's */ 756 SLIST_FOREACH(sound_dma, &sc->sc_dma_list, dma_chain) { 757 if (KERNADDR(sound_dma) == start) 758 break; 759 } 760 761 /* not ours ? then bail out */ 762 if (!sound_dma) { 763 printf("%s: auixp_trigger_output: bad sound addr %p\n", 764 device_xname(sc->sc_dev), start); 765 return EINVAL; 766 } 767 768 /* link round-robin daisychain and program hardware */ 769 auixp_link_daisychain(sc, chain_dma, sound_dma, blksize, blocks); 770 auixp_program_dma_chain(sc, chain_dma); 771 772 /* mark we are now able to run now */ 773 chain_dma->running = 1; 774 775 /* update bus-flags; XXX programs more flags XXX */ 776 auixp_update_busbusy(sc); 777 778 /* callbacks happen in interrupt routine */ 779 return 0; 780 } 781 782 783 /* halt output of audio, just disable its dma and update bus state */ 784 static int 785 auixp_halt_output(void *hdl) 786 { 787 struct auixp_codec *co; 788 struct auixp_softc *sc; 789 struct auixp_dma *dma; 790 791 co = (struct auixp_codec *) hdl; 792 sc = co->sc; 793 dma = sc->sc_output_dma; 794 auixp_disable_dma(sc, dma); 795 796 dma->running = 0; 797 auixp_update_busbusy(sc); 798 799 return 0; 800 } 801 802 803 /* XXX allmost literaly a copy of trigger-output; could be factorised XXX */ 804 static int 805 auixp_trigger_input(void *hdl, void *start, void *end, int blksize, 806 void (*intr)(void *), void *intrarg, const audio_params_t *param) 807 { 808 struct auixp_codec *co; 809 struct auixp_softc *sc; 810 struct auixp_dma *chain_dma; 811 struct auixp_dma *sound_dma; 812 uint32_t blocks; 813 814 co = (struct auixp_codec *) hdl; 815 sc = co->sc; 816 chain_dma = sc->sc_input_dma; 817 /* add functions to call back */ 818 chain_dma->intr = intr; 819 chain_dma->intrarg = intrarg; 820 821 /* 822 * Program output DMA chain with blocks from [start...end] with 823 * blksize fragments. 824 * 825 * NOTE, we can assume its in one block since we asked for it to be in 826 * one contiguous blob; XXX change this? XXX 827 */ 828 blocks = (size_t) (((char *) end) - ((char *) start)) / blksize; 829 830 /* lookup `start' address in our list of DMA area's */ 831 SLIST_FOREACH(sound_dma, &sc->sc_dma_list, dma_chain) { 832 if (KERNADDR(sound_dma) == start) 833 break; 834 } 835 836 /* not ours ? then bail out */ 837 if (!sound_dma) { 838 printf("%s: auixp_trigger_input: bad sound addr %p\n", 839 device_xname(sc->sc_dev), start); 840 return EINVAL; 841 } 842 843 /* link round-robin daisychain and program hardware */ 844 auixp_link_daisychain(sc, chain_dma, sound_dma, blksize, blocks); 845 auixp_program_dma_chain(sc, chain_dma); 846 847 /* mark we are now able to run now */ 848 chain_dma->running = 1; 849 850 /* update bus-flags; XXX programs more flags XXX */ 851 auixp_update_busbusy(sc); 852 853 /* callbacks happen in interrupt routine */ 854 return 0; 855 } 856 857 858 /* halt sampling audio, just disable its dma and update bus state */ 859 static int 860 auixp_halt_input(void *hdl) 861 { 862 struct auixp_codec *co; 863 struct auixp_softc *sc; 864 struct auixp_dma *dma; 865 866 co = (struct auixp_codec *) hdl; 867 sc = co->sc; 868 dma = sc->sc_input_dma; 869 auixp_disable_dma(sc, dma); 870 871 dma->running = 0; 872 auixp_update_busbusy(sc); 873 874 return 0; 875 } 876 877 878 /* 879 * IXP audio interrupt handler 880 * 881 * note that we return the number of bits handled; the return value is not 882 * documentated but i saw it implemented in other drivers. Prolly returning a 883 * value > 0 means "i've dealt with it" 884 * 885 */ 886 static int 887 auixp_intr(void *softc) 888 { 889 struct auixp_softc *sc; 890 bus_space_tag_t iot; 891 bus_space_handle_t ioh; 892 uint32_t status, enable, detected_codecs; 893 int ret; 894 895 sc = softc; 896 mutex_spin_enter(&sc->sc_intr_lock); 897 898 iot = sc->sc_iot; 899 ioh = sc->sc_ioh; 900 ret = 0; 901 /* get status from the interrupt status register */ 902 status = bus_space_read_4(iot, ioh, ATI_REG_ISR); 903 904 if (status == 0) { 905 mutex_spin_exit(&sc->sc_intr_lock); 906 return 0; 907 } 908 909 DPRINTF(("%s: (status = %x)\n", device_xname(sc->sc_dev), status)); 910 911 /* check DMA UPDATE flags for input & output */ 912 if (status & ATI_REG_ISR_IN_STATUS) { 913 ret++; DPRINTF(("IN_STATUS\n")); 914 auixp_dma_update(sc, sc->sc_input_dma); 915 } 916 if (status & ATI_REG_ISR_OUT_STATUS) { 917 ret++; DPRINTF(("OUT_STATUS\n")); 918 auixp_dma_update(sc, sc->sc_output_dma); 919 } 920 921 /* XXX XRUN flags not used/needed yet; should i implement it? XXX */ 922 /* acknowledge the interrupts nevertheless */ 923 if (status & ATI_REG_ISR_IN_XRUN) { 924 ret++; DPRINTF(("IN_XRUN\n")); 925 /* auixp_dma_xrun(sc, sc->sc_input_dma); */ 926 } 927 if (status & ATI_REG_ISR_OUT_XRUN) { 928 ret++; DPRINTF(("OUT_XRUN\n")); 929 /* auixp_dma_xrun(sc, sc->sc_output_dma); */ 930 } 931 932 /* check if we are looking for codec detection */ 933 if (status & CODEC_CHECK_BITS) { 934 ret++; 935 /* mark missing codecs as not ready */ 936 detected_codecs = status & CODEC_CHECK_BITS; 937 sc->sc_codec_not_ready_bits |= detected_codecs; 938 939 /* disable detected interrupt sources */ 940 enable = bus_space_read_4(iot, ioh, ATI_REG_IER); 941 enable &= ~detected_codecs; 942 bus_space_write_4(iot, ioh, ATI_REG_IER, enable); 943 } 944 945 /* acknowledge interrupt sources */ 946 bus_space_write_4(iot, ioh, ATI_REG_ISR, status); 947 948 mutex_spin_exit(&sc->sc_intr_lock); 949 return ret; 950 } 951 952 953 /* allocate memory for dma purposes; on failure of any of the steps, roll back */ 954 static int 955 auixp_allocmem(struct auixp_softc *sc, size_t size, 956 size_t align, struct auixp_dma *dma) 957 { 958 int error; 959 960 /* remember size */ 961 dma->size = size; 962 963 /* allocate DMA safe memory but in just one segment for now :( */ 964 error = bus_dmamem_alloc(sc->sc_dmat, dma->size, align, 0, 965 dma->segs, sizeof(dma->segs) / sizeof(dma->segs[0]), &dma->nsegs, 966 BUS_DMA_WAITOK); 967 if (error) 968 return error; 969 970 /* 971 * map allocated memory into kernel virtual address space and keep it 972 * coherent with the CPU. 973 */ 974 error = bus_dmamem_map(sc->sc_dmat, dma->segs, dma->nsegs, dma->size, 975 &dma->addr, BUS_DMA_WAITOK | BUS_DMA_COHERENT); 976 if (error) 977 goto free; 978 979 /* allocate associated dma handle and initialize it. */ 980 error = bus_dmamap_create(sc->sc_dmat, dma->size, 1, dma->size, 0, 981 BUS_DMA_WAITOK, &dma->map); 982 if (error) 983 goto unmap; 984 985 /* 986 * load the dma handle with mappings for a dma transfer; all pages 987 * need to be wired. 988 */ 989 error = bus_dmamap_load(sc->sc_dmat, dma->map, dma->addr, dma->size, NULL, 990 BUS_DMA_WAITOK); 991 if (error) 992 goto destroy; 993 994 return 0; 995 996 destroy: 997 bus_dmamap_destroy(sc->sc_dmat, dma->map); 998 unmap: 999 bus_dmamem_unmap(sc->sc_dmat, dma->addr, dma->size); 1000 free: 1001 bus_dmamem_free(sc->sc_dmat, dma->segs, dma->nsegs); 1002 1003 return error; 1004 } 1005 1006 1007 /* undo dma mapping and release memory allocated */ 1008 static int 1009 auixp_freemem(struct auixp_softc *sc, struct auixp_dma *p) 1010 { 1011 1012 bus_dmamap_unload(sc->sc_dmat, p->map); 1013 bus_dmamap_destroy(sc->sc_dmat, p->map); 1014 bus_dmamem_unmap(sc->sc_dmat, p->addr, p->size); 1015 bus_dmamem_free(sc->sc_dmat, p->segs, p->nsegs); 1016 1017 return 0; 1018 } 1019 1020 1021 /* 1022 * Attachment section 1023 */ 1024 1025 /* Is it my hardware? */ 1026 static int 1027 auixp_match(device_t dev, cfdata_t match, void *aux) 1028 { 1029 struct pci_attach_args *pa; 1030 1031 pa = (struct pci_attach_args *)aux; 1032 switch(PCI_VENDOR(pa->pa_id)) { 1033 case PCI_VENDOR_ATI: 1034 switch(PCI_PRODUCT(pa->pa_id)) { 1035 case PCI_PRODUCT_ATI_IXP_AUDIO_200: 1036 case PCI_PRODUCT_ATI_IXP_AUDIO_300: 1037 case PCI_PRODUCT_ATI_IXP_AUDIO_400: 1038 return 1; 1039 } 1040 } 1041 1042 return 0; 1043 } 1044 1045 1046 /* it is... now hook up and set up the resources we need */ 1047 static void 1048 auixp_attach(device_t parent, device_t self, void *aux) 1049 { 1050 struct auixp_softc *sc; 1051 struct pci_attach_args *pa; 1052 pcitag_t tag; 1053 pci_chipset_tag_t pc; 1054 pci_intr_handle_t ih; 1055 const struct auixp_card_type *card; 1056 const char *intrstr; 1057 uint32_t data; 1058 int error; 1059 char intrbuf[PCI_INTRSTR_LEN]; 1060 1061 sc = device_private(self); 1062 sc->sc_dev = self; 1063 pa = (struct pci_attach_args *)aux; 1064 tag = pa->pa_tag; 1065 pc = pa->pa_pc; 1066 #ifdef DEBUG_AUIXP 1067 static_sc = sc; 1068 #endif 1069 1070 /* print information confirming attachment */ 1071 pci_aprint_devinfo(pa, "Audio controller"); 1072 1073 /* set up details from our set of known `cards'/chips */ 1074 for (card = auixp_card_types; card->pci_vendor_id; card++) 1075 if (PCI_VENDOR(pa->pa_id) == card->pci_vendor_id && 1076 PCI_PRODUCT(pa->pa_id) == card->pci_product_id) { 1077 sc->type = card->type; 1078 break; 1079 } 1080 1081 /* device only has 32 bit non prefetchable memory */ 1082 /* set MEM space access and enable the card's busmastering */ 1083 data = pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG); 1084 data |= (PCI_COMMAND_MEM_ENABLE | PCI_COMMAND_MASTER_ENABLE); 1085 pci_conf_write(pc, tag, PCI_COMMAND_STATUS_REG, data); 1086 1087 /* map memory; its not sized -> what is the size? max PCI slot size? */ 1088 if (pci_mapreg_map(pa, PCI_CBIO, PCI_MAPREG_TYPE_MEM, 0, 1089 &sc->sc_iot, &sc->sc_ioh, &sc->sc_iob, &sc->sc_ios)) { 1090 aprint_error_dev(sc->sc_dev, "can't map memory space\n"); 1091 return; 1092 } 1093 1094 /* Initialize softc */ 1095 sc->sc_tag = tag; 1096 sc->sc_pct = pc; 1097 sc->sc_dmat = pa->pa_dmat; 1098 SLIST_INIT(&sc->sc_dma_list); 1099 1100 /* get us the auixp_dma structures */ 1101 auixp_allocate_dma_chain(sc, &sc->sc_output_dma); 1102 auixp_allocate_dma_chain(sc, &sc->sc_input_dma); 1103 1104 /* when that fails we are dead in the water */ 1105 if (!sc->sc_output_dma || !sc->sc_input_dma) 1106 return; 1107 1108 #if 0 1109 /* could preliminary program DMA chain */ 1110 auixp_program_dma_chain(sc, sc->sc_output_dma); 1111 auixp_program_dma_chain(sc, sc->sc_input_dma); 1112 #endif 1113 1114 /* map interrupt on the pci bus */ 1115 if (pci_intr_map(pa, &ih)) { 1116 aprint_error_dev(sc->sc_dev, "can't map interrupt\n"); 1117 return; 1118 } 1119 1120 /* where are we connected at ? */ 1121 intrstr = pci_intr_string(pc, ih, intrbuf, sizeof(intrbuf)); 1122 1123 mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE); 1124 mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_AUDIO); 1125 1126 /* establish interrupt routine hookup at IPL_AUDIO level */ 1127 sc->sc_ih = pci_intr_establish_xname(pc, ih, IPL_AUDIO, auixp_intr, 1128 self, device_xname(self)); 1129 if (sc->sc_ih == NULL) { 1130 aprint_error_dev(sc->sc_dev, "can't establish interrupt"); 1131 if (intrstr != NULL) 1132 aprint_error(" at %s", intrstr); 1133 aprint_error("\n"); 1134 return; 1135 } 1136 aprint_normal_dev(sc->sc_dev, "interrupting at %s\n", intrstr); 1137 1138 /* power up chip */ 1139 if ((error = pci_activate(pa->pa_pc, pa->pa_tag, self, 1140 pci_activate_null)) && error != EOPNOTSUPP) { 1141 aprint_error_dev(sc->sc_dev, "cannot activate %d\n", 1142 error); 1143 return; 1144 } 1145 1146 /* init chip */ 1147 if (auixp_init(sc) == -1) { 1148 aprint_error_dev(sc->sc_dev, 1149 "auixp_attach: unable to initialize the card\n"); 1150 return; 1151 } 1152 1153 if (!pmf_device_register(self, NULL, auixp_resume)) 1154 aprint_error_dev(self, "couldn't establish power handler\n"); 1155 1156 /* 1157 * delay further configuration of codecs and audio after interrupts 1158 * are enabled. 1159 */ 1160 config_interrupts(self, auixp_post_config); 1161 } 1162 1163 1164 /* called from autoconfigure system when interrupts are enabled */ 1165 static void 1166 auixp_post_config(device_t self) 1167 { 1168 struct auixp_softc *sc; 1169 struct auixp_codec *codec; 1170 int codec_nr; 1171 int i; 1172 1173 sc = device_private(self); 1174 /* detect the AC97 codecs */ 1175 auixp_autodetect_codecs(sc); 1176 1177 /* setup audio translation formats : following codec0 (!) */ 1178 codec = &sc->sc_codec[0]; 1179 if (!codec->present) { 1180 /* nothing??? then invalidate all formats */ 1181 for (i = 0; i < AUIXP_NFORMATS; i++) { 1182 AUFMT_INVALIDATE(&sc->sc_formats[i]); 1183 } 1184 return; 1185 } 1186 1187 /* copy formats and invalidate entries not suitable for codec0 */ 1188 memcpy(sc->sc_formats, auixp_formats, sizeof(auixp_formats)); 1189 mutex_enter(&sc->sc_lock); 1190 sc->has_4ch = AC97_IS_4CH(codec->codec_if); 1191 sc->has_6ch = AC97_IS_6CH(codec->codec_if); 1192 sc->is_fixed = AC97_IS_FIXED_RATE(codec->codec_if); 1193 sc->has_spdif = AC97_HAS_SPDIF(codec->codec_if); 1194 mutex_exit(&sc->sc_lock); 1195 1196 for (i = 0; i < AUIXP_NFORMATS; i++) { 1197 if (sc->is_fixed) { 1198 sc->sc_formats[i].frequency_type = 1; 1199 sc->sc_formats[i].frequency[0] = 48000; 1200 } 1201 switch (sc->sc_formats[i].channels) { 1202 case 4 : 1203 if (sc->has_4ch) 1204 break; 1205 AUFMT_INVALIDATE(&sc->sc_formats[i]); 1206 break; 1207 case 6 : 1208 if (sc->has_6ch) 1209 break; 1210 AUFMT_INVALIDATE(&sc->sc_formats[i]); 1211 break; 1212 default : 1213 break; 1214 } 1215 } 1216 1217 if (sc->has_spdif) { 1218 aprint_normal_dev(sc->sc_dev, "codec spdif support detected but disabled " 1219 "for now\n"); 1220 sc->has_spdif = 0; 1221 } 1222 1223 /* fill in the missing details about the dma channels. */ 1224 /* for output */ 1225 sc->sc_output_dma->linkptr = ATI_REG_OUT_DMA_LINKPTR; 1226 sc->sc_output_dma->dma_enable_bit = ATI_REG_CMD_OUT_DMA_EN | 1227 ATI_REG_CMD_SEND_EN; 1228 /* have spdif? then this too! XXX not seeing LED yet! XXX */ 1229 if (sc->has_spdif) 1230 sc->sc_output_dma->dma_enable_bit |= ATI_REG_CMD_SPDF_OUT_EN; 1231 1232 /* and for input */ 1233 sc->sc_input_dma->linkptr = ATI_REG_IN_DMA_LINKPTR; 1234 sc->sc_input_dma->dma_enable_bit = ATI_REG_CMD_IN_DMA_EN | 1235 ATI_REG_CMD_RECEIVE_EN; 1236 1237 /* attach audio devices for all detected codecs */ 1238 /* XXX wise? look at other multiple-codec able chipsets XXX */ 1239 for (codec_nr = 0; codec_nr < ATI_IXP_CODECS; codec_nr++) { 1240 codec = &sc->sc_codec[codec_nr]; 1241 if (codec->present) 1242 audio_attach_mi(&auixp_hw_if, codec, sc->sc_dev); 1243 } 1244 1245 /* done! now enable all interrupts we can service */ 1246 auixp_enable_interrupts(sc); 1247 } 1248 1249 static void 1250 auixp_enable_interrupts(struct auixp_softc *sc) 1251 { 1252 bus_space_tag_t iot; 1253 bus_space_handle_t ioh; 1254 uint32_t value; 1255 1256 iot = sc->sc_iot; 1257 ioh = sc->sc_ioh; 1258 1259 mutex_spin_enter(&sc->sc_intr_lock); 1260 1261 /* clear all pending */ 1262 bus_space_write_4(iot, ioh, ATI_REG_ISR, 0xffffffff); 1263 1264 /* enable all relevant interrupt sources we can handle */ 1265 value = bus_space_read_4(iot, ioh, ATI_REG_IER); 1266 1267 value |= ATI_REG_IER_IO_STATUS_EN; 1268 #ifdef notyet 1269 value |= ATI_REG_IER_IN_XRUN_EN; 1270 value |= ATI_REG_IER_OUT_XRUN_EN; 1271 1272 value |= ATI_REG_IER_SPDIF_XRUN_EN; 1273 value |= ATI_REG_IER_SPDF_STATUS_EN; 1274 #endif 1275 1276 bus_space_write_4(iot, ioh, ATI_REG_IER, value); 1277 1278 mutex_spin_exit(&sc->sc_intr_lock); 1279 } 1280 1281 1282 static void 1283 auixp_disable_interrupts(struct auixp_softc *sc) 1284 { 1285 bus_space_tag_t iot; 1286 bus_space_handle_t ioh; 1287 1288 iot = sc->sc_iot; 1289 ioh = sc->sc_ioh; 1290 1291 mutex_spin_enter(&sc->sc_intr_lock); 1292 1293 /* disable all interrupt sources */ 1294 bus_space_write_4(iot, ioh, ATI_REG_IER, 0); 1295 1296 /* clear all pending */ 1297 bus_space_write_4(iot, ioh, ATI_REG_ISR, 0xffffffff); 1298 1299 mutex_spin_exit(&sc->sc_intr_lock); 1300 } 1301 1302 1303 /* dismantle what we've set up by undoing setup */ 1304 static int 1305 auixp_detach(device_t self, int flags) 1306 { 1307 struct auixp_softc *sc; 1308 1309 sc = device_private(self); 1310 /* XXX shouldn't we just reset the chip? XXX */ 1311 /* 1312 * should we explicitly disable interrupt generation and acknowledge 1313 * what's left on? better be safe than sorry. 1314 */ 1315 auixp_disable_interrupts(sc); 1316 1317 /* tear down .... */ 1318 config_detach(sc->sc_dev, flags); /* XXX OK? XXX */ 1319 pmf_device_deregister(self); 1320 1321 if (sc->sc_ih != NULL) 1322 pci_intr_disestablish(sc->sc_pct, sc->sc_ih); 1323 if (sc->sc_ios) 1324 bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios); 1325 1326 mutex_destroy(&sc->sc_lock); 1327 mutex_destroy(&sc->sc_intr_lock); 1328 1329 return 0; 1330 } 1331 1332 1333 /* 1334 * codec handling 1335 * 1336 * IXP audio support can have upto 3 codecs! are they chained ? or 1337 * alternative outlets with the same audio feed i.e. with different mixer 1338 * settings? XXX does NetBSD support more than one audio codec? XXX 1339 */ 1340 1341 1342 static int 1343 auixp_attach_codec(void *aux, struct ac97_codec_if *codec_if) 1344 { 1345 struct auixp_codec *ixp_codec; 1346 1347 ixp_codec = aux; 1348 ixp_codec->codec_if = codec_if; 1349 ixp_codec->present = 1; 1350 1351 return 0; 1352 } 1353 1354 1355 static int 1356 auixp_read_codec(void *aux, uint8_t reg, uint16_t *result) 1357 { 1358 struct auixp_codec *co; 1359 struct auixp_softc *sc; 1360 bus_space_tag_t iot; 1361 bus_space_handle_t ioh; 1362 uint32_t data; 1363 int timeout; 1364 1365 co = aux; 1366 sc = co->sc; 1367 iot = sc->sc_iot; 1368 ioh = sc->sc_ioh; 1369 if (auixp_wait_for_codecs(sc, "read_codec")) 1370 return 0xffff; 1371 1372 /* build up command for reading codec register */ 1373 data = (reg << ATI_REG_PHYS_OUT_ADDR_SHIFT) | 1374 ATI_REG_PHYS_OUT_ADDR_EN | 1375 ATI_REG_PHYS_OUT_RW | 1376 co->codec_nr; 1377 1378 bus_space_write_4(iot, ioh, ATI_REG_PHYS_OUT_ADDR, data); 1379 1380 if (auixp_wait_for_codecs(sc, "read_codec")) 1381 return 0xffff; 1382 1383 /* wait until codec info is clocked in */ 1384 timeout = 500; /* 500*2 usec -> 0.001 sec */ 1385 do { 1386 data = bus_space_read_4(iot, ioh, ATI_REG_PHYS_IN_ADDR); 1387 if (data & ATI_REG_PHYS_IN_READ_FLAG) { 1388 DPRINTF(("read ac'97 codec reg 0x%x = 0x%08x\n", 1389 reg, data >> ATI_REG_PHYS_IN_DATA_SHIFT) 1390 ); 1391 *result = data >> ATI_REG_PHYS_IN_DATA_SHIFT; 1392 return 0; 1393 } 1394 DELAY(2); 1395 timeout--; 1396 } while (timeout > 0); 1397 1398 if (reg < 0x7c) 1399 printf("%s: codec read timeout! (reg %x)\n", 1400 device_xname(sc->sc_dev), reg); 1401 1402 return 0xffff; 1403 } 1404 1405 1406 static int 1407 auixp_write_codec(void *aux, uint8_t reg, uint16_t data) 1408 { 1409 struct auixp_codec *co; 1410 struct auixp_softc *sc; 1411 bus_space_tag_t iot; 1412 bus_space_handle_t ioh; 1413 uint32_t value; 1414 1415 DPRINTF(("write ac'97 codec reg 0x%x = 0x%08x\n", reg, data)); 1416 co = aux; 1417 sc = co->sc; 1418 iot = sc->sc_iot; 1419 ioh = sc->sc_ioh; 1420 if (auixp_wait_for_codecs(sc, "write_codec")) 1421 return -1; 1422 1423 /* build up command for writing codec register */ 1424 value = (((uint32_t) data) << ATI_REG_PHYS_OUT_DATA_SHIFT) | 1425 (((uint32_t) reg) << ATI_REG_PHYS_OUT_ADDR_SHIFT) | 1426 ATI_REG_PHYS_OUT_ADDR_EN | 1427 co->codec_nr; 1428 1429 bus_space_write_4(iot, ioh, ATI_REG_PHYS_OUT_ADDR, value); 1430 1431 return 0; 1432 } 1433 1434 1435 static int 1436 auixp_reset_codec(void *aux) 1437 { 1438 1439 /* nothing to be done? */ 1440 return 0; 1441 } 1442 1443 1444 static enum ac97_host_flags 1445 auixp_flags_codec(void *aux) 1446 { 1447 struct auixp_codec *ixp_codec; 1448 1449 ixp_codec = aux; 1450 return ixp_codec->codec_flags; 1451 } 1452 1453 1454 static int 1455 auixp_wait_for_codecs(struct auixp_softc *sc, const char *func) 1456 { 1457 bus_space_tag_t iot; 1458 bus_space_handle_t ioh; 1459 uint32_t value; 1460 int timeout; 1461 1462 iot = sc->sc_iot; 1463 ioh = sc->sc_ioh; 1464 /* wait until all codec transfers are done */ 1465 timeout = 500; /* 500*2 usec -> 0.001 sec */ 1466 do { 1467 value = bus_space_read_4(iot, ioh, ATI_REG_PHYS_OUT_ADDR); 1468 if ((value & ATI_REG_PHYS_OUT_ADDR_EN) == 0) 1469 return 0; 1470 1471 DELAY(2); 1472 timeout--; 1473 } while (timeout > 0); 1474 1475 printf("%s: %s: timed out\n", func, device_xname(sc->sc_dev)); 1476 return -1; 1477 } 1478 1479 1480 1481 static void 1482 auixp_autodetect_codecs(struct auixp_softc *sc) 1483 { 1484 bus_space_tag_t iot; 1485 bus_space_handle_t ioh; 1486 struct auixp_codec *codec; 1487 int timeout, codec_nr; 1488 1489 iot = sc->sc_iot; 1490 ioh = sc->sc_ioh; 1491 /* ATI IXP can have upto 3 codecs; mark all codecs as not existing */ 1492 sc->sc_codec_not_ready_bits = 0; 1493 sc->sc_num_codecs = 0; 1494 1495 /* enable all codecs to interrupt as well as the new frame interrupt */ 1496 bus_space_write_4(iot, ioh, ATI_REG_IER, CODEC_CHECK_BITS); 1497 1498 /* wait for the interrupts to happen */ 1499 timeout = 100; /* 100.000 usec -> 0.1 sec */ 1500 1501 while (timeout > 0) { 1502 DELAY(1000); 1503 if (sc->sc_codec_not_ready_bits) 1504 break; 1505 timeout--; 1506 } 1507 1508 if (timeout == 0) 1509 printf("%s: WARNING: timeout during codec detection; " 1510 "codecs might be present but haven't interrupted\n", 1511 device_xname(sc->sc_dev)); 1512 1513 /* disable all interrupts for now */ 1514 auixp_disable_interrupts(sc); 1515 1516 /* Attach AC97 host interfaces */ 1517 for (codec_nr = 0; codec_nr < ATI_IXP_CODECS; codec_nr++) { 1518 codec = &sc->sc_codec[codec_nr]; 1519 memset(codec, 0, sizeof(struct auixp_codec)); 1520 1521 codec->sc = sc; 1522 codec->codec_nr = codec_nr; 1523 codec->present = 0; 1524 1525 codec->host_if.arg = codec; 1526 codec->host_if.attach = auixp_attach_codec; 1527 codec->host_if.read = auixp_read_codec; 1528 codec->host_if.write = auixp_write_codec; 1529 codec->host_if.reset = auixp_reset_codec; 1530 codec->host_if.flags = auixp_flags_codec; 1531 } 1532 1533 if (!(sc->sc_codec_not_ready_bits & ATI_REG_ISR_CODEC0_NOT_READY)) { 1534 /* codec 0 present */ 1535 DPRINTF(("auixp : YAY! codec 0 present!\n")); 1536 if (ac97_attach(&sc->sc_codec[0].host_if, sc->sc_dev, 1537 &sc->sc_lock) == 0) 1538 sc->sc_num_codecs++; 1539 } 1540 1541 if (!(sc->sc_codec_not_ready_bits & ATI_REG_ISR_CODEC1_NOT_READY)) { 1542 /* codec 1 present */ 1543 DPRINTF(("auixp : YAY! codec 1 present!\n")); 1544 if (ac97_attach(&sc->sc_codec[1].host_if, sc->sc_dev, 1545 &sc->sc_lock) == 0) 1546 sc->sc_num_codecs++; 1547 } 1548 1549 if (!(sc->sc_codec_not_ready_bits & ATI_REG_ISR_CODEC2_NOT_READY)) { 1550 /* codec 2 present */ 1551 DPRINTF(("auixp : YAY! codec 2 present!\n")); 1552 if (ac97_attach(&sc->sc_codec[2].host_if, sc->sc_dev, 1553 &sc->sc_lock) == 0) 1554 sc->sc_num_codecs++; 1555 } 1556 1557 if (sc->sc_num_codecs == 0) { 1558 printf("%s: no codecs detected or " 1559 "no codecs managed to initialise\n", 1560 device_xname(sc->sc_dev)); 1561 return; 1562 } 1563 1564 } 1565 1566 1567 1568 /* initialisation routines */ 1569 1570 static void 1571 auixp_disable_dma(struct auixp_softc *sc, struct auixp_dma *dma) 1572 { 1573 bus_space_tag_t iot; 1574 bus_space_handle_t ioh; 1575 uint32_t value; 1576 1577 iot = sc->sc_iot; 1578 ioh = sc->sc_ioh; 1579 /* lets not stress the DMA engine more than nessisary */ 1580 value = bus_space_read_4(iot, ioh, ATI_REG_CMD); 1581 if (value & dma->dma_enable_bit) { 1582 value &= ~dma->dma_enable_bit; 1583 bus_space_write_4(iot, ioh, ATI_REG_CMD, value); 1584 } 1585 } 1586 1587 1588 static void 1589 auixp_enable_dma(struct auixp_softc *sc, struct auixp_dma *dma) 1590 { 1591 bus_space_tag_t iot; 1592 bus_space_handle_t ioh; 1593 uint32_t value; 1594 1595 iot = sc->sc_iot; 1596 ioh = sc->sc_ioh; 1597 /* lets not stress the DMA engine more than nessisary */ 1598 value = bus_space_read_4(iot, ioh, ATI_REG_CMD); 1599 if (!(value & dma->dma_enable_bit)) { 1600 value |= dma->dma_enable_bit; 1601 bus_space_write_4(iot, ioh, ATI_REG_CMD, value); 1602 } 1603 } 1604 1605 1606 static void 1607 auixp_reset_aclink(struct auixp_softc *sc) 1608 { 1609 bus_space_tag_t iot; 1610 bus_space_handle_t ioh; 1611 uint32_t value, timeout; 1612 1613 iot = sc->sc_iot; 1614 ioh = sc->sc_ioh; 1615 1616 /* if power is down, power it up */ 1617 value = bus_space_read_4(iot, ioh, ATI_REG_CMD); 1618 if (value & ATI_REG_CMD_POWERDOWN) { 1619 printf("%s: powering up\n", device_xname(sc->sc_dev)); 1620 1621 /* explicitly enable power */ 1622 value &= ~ATI_REG_CMD_POWERDOWN; 1623 bus_space_write_4(iot, ioh, ATI_REG_CMD, value); 1624 1625 /* have to wait at least 10 usec for it to initialise */ 1626 DELAY(20); 1627 }; 1628 1629 printf("%s: soft resetting aclink\n", device_xname(sc->sc_dev)); 1630 1631 /* perform a soft reset */ 1632 value = bus_space_read_4(iot, ioh, ATI_REG_CMD); 1633 value |= ATI_REG_CMD_AC_SOFT_RESET; 1634 bus_space_write_4(iot, ioh, ATI_REG_CMD, value); 1635 1636 /* need to read the CMD reg and wait aprox. 10 usec to init */ 1637 value = bus_space_read_4(iot, ioh, ATI_REG_CMD); 1638 DELAY(20); 1639 1640 /* clear soft reset flag again */ 1641 value = bus_space_read_4(iot, ioh, ATI_REG_CMD); 1642 value &= ~ATI_REG_CMD_AC_SOFT_RESET; 1643 bus_space_write_4(iot, ioh, ATI_REG_CMD, value); 1644 1645 /* check if the ac-link is working; reset device otherwise */ 1646 timeout = 10; 1647 value = bus_space_read_4(iot, ioh, ATI_REG_CMD); 1648 while (!(value & ATI_REG_CMD_ACLINK_ACTIVE)) { 1649 printf("%s: not up; resetting aclink hardware\n", 1650 device_xname(sc->sc_dev)); 1651 1652 /* dip aclink reset but keep the acsync */ 1653 value &= ~ATI_REG_CMD_AC_RESET; 1654 value |= ATI_REG_CMD_AC_SYNC; 1655 bus_space_write_4(iot, ioh, ATI_REG_CMD, value); 1656 1657 /* need to read CMD again and wait again (clocking in issue?) */ 1658 value = bus_space_read_4(iot, ioh, ATI_REG_CMD); 1659 DELAY(20); 1660 1661 /* assert aclink reset again */ 1662 value = bus_space_read_4(iot, ioh, ATI_REG_CMD); 1663 value |= ATI_REG_CMD_AC_RESET; 1664 bus_space_write_4(iot, ioh, ATI_REG_CMD, value); 1665 1666 /* check if its active now */ 1667 value = bus_space_read_4(iot, ioh, ATI_REG_CMD); 1668 1669 timeout--; 1670 if (timeout == 0) break; 1671 }; 1672 1673 if (timeout == 0) { 1674 printf("%s: giving up aclink reset\n", device_xname(sc->sc_dev)); 1675 }; 1676 if (timeout != 10) { 1677 printf("%s: aclink hardware reset successful\n", 1678 device_xname(sc->sc_dev)); 1679 }; 1680 1681 /* assert reset and sync for safety */ 1682 value = bus_space_read_4(iot, ioh, ATI_REG_CMD); 1683 value |= ATI_REG_CMD_AC_SYNC | ATI_REG_CMD_AC_RESET; 1684 bus_space_write_4(iot, ioh, ATI_REG_CMD, value); 1685 } 1686 1687 1688 /* chip hard init */ 1689 static int 1690 auixp_init(struct auixp_softc *sc) 1691 { 1692 bus_space_tag_t iot; 1693 bus_space_handle_t ioh; 1694 uint32_t value; 1695 1696 iot = sc->sc_iot; 1697 ioh = sc->sc_ioh; 1698 /* disable all interrupts and clear all sources */ 1699 auixp_disable_interrupts(sc); 1700 1701 /* clear all DMA enables (preserving rest of settings) */ 1702 value = bus_space_read_4(iot, ioh, ATI_REG_CMD); 1703 value &= ~( ATI_REG_CMD_IN_DMA_EN | 1704 ATI_REG_CMD_OUT_DMA_EN | 1705 ATI_REG_CMD_SPDF_OUT_EN ); 1706 bus_space_write_4(iot, ioh, ATI_REG_CMD, value); 1707 1708 /* Reset AC-link */ 1709 auixp_reset_aclink(sc); 1710 1711 /* 1712 * codecs get auto-detected later 1713 * 1714 * note: we are NOT enabling interrupts yet, no codecs have been 1715 * detected yet nor is anything else set up 1716 */ 1717 1718 return 0; 1719 } 1720 1721 static bool 1722 auixp_resume(device_t dv, const pmf_qual_t *qual) 1723 { 1724 struct auixp_softc *sc = device_private(dv); 1725 1726 mutex_enter(&sc->sc_lock); 1727 auixp_reset_codec(sc); 1728 delay(1000); 1729 (sc->sc_codec[0].codec_if->vtbl->restore_ports)(sc->sc_codec[0].codec_if); 1730 mutex_exit(&sc->sc_lock); 1731 1732 return true; 1733 } 1734 1735 #ifdef DEBUG_AUIXP 1736 1737 static void 1738 auixp_dumpreg(void) 1739 { 1740 struct auixp_softc *sc; 1741 bus_space_tag_t iot; 1742 bus_space_handle_t ioh; 1743 int i; 1744 1745 sc = static_sc; 1746 iot = sc->sc_iot; 1747 ioh = sc->sc_ioh; 1748 printf("%s register dump:\n", device_xname(sc->sc_dev)); 1749 for (i = 0; i < 256; i+=4) { 1750 printf("\t0x%02x: 0x%08x\n", i, bus_space_read_4(iot, ioh, i)); 1751 } 1752 printf("\n"); 1753 } 1754 #endif 1755 1756 static void 1757 auixp_get_locks(void *addr, kmutex_t **intr, kmutex_t **proc) 1758 { 1759 struct auixp_codec *co = addr; 1760 struct auixp_softc *sc = co->sc; 1761 1762 *intr = &sc->sc_intr_lock; 1763 *proc = &sc->sc_lock; 1764 } 1765