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