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