1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright 2010 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 /* 27 * audioixp Audio Driver 28 * 29 * This driver supports audio hardware integrated in ATI IXP400 chipset. 30 * 31 * The IXP400 audio core is an AC'97 controller, which has independent 32 * channels for PCM in, PCM out. The AC'97 controller is a PCI bus master 33 * with scatter/gather support. Each channel has a DMA engine. Currently, 34 * we use only the PCM in and PCM out channels. Each DMA engine uses one 35 * buffer descriptor list. Each entry contains a pointer to a data buffer, 36 * status, length of the buffer being pointed to and the pointer to the next 37 * entry. Length of the buffer is in number of bytes. Interrupt will be 38 * triggered each time a entry is processed by hardware. 39 * 40 * System power management is not yet supported by the driver. 41 * 42 * NOTE: 43 * This driver depends on the misc/ac97 and drv/audio modules being 44 * loaded first. 45 */ 46 #include <sys/types.h> 47 #include <sys/modctl.h> 48 #include <sys/kmem.h> 49 #include <sys/conf.h> 50 #include <sys/ddi.h> 51 #include <sys/sunddi.h> 52 #include <sys/pci.h> 53 #include <sys/note.h> 54 #include <sys/audio/audio_driver.h> 55 #include <sys/audio/ac97.h> 56 #include "audioixp.h" 57 58 /* 59 * Module linkage routines for the kernel 60 */ 61 static int audioixp_ddi_attach(dev_info_t *, ddi_attach_cmd_t); 62 static int audioixp_ddi_detach(dev_info_t *, ddi_detach_cmd_t); 63 static int audioixp_quiesce(dev_info_t *); 64 static int audioixp_resume(dev_info_t *); 65 static int audioixp_suspend(dev_info_t *); 66 67 /* 68 * Entry point routine prototypes 69 */ 70 static int audioixp_open(void *, int, unsigned *, unsigned *, caddr_t *); 71 static void audioixp_close(void *); 72 static int audioixp_start(void *); 73 static void audioixp_stop(void *); 74 static int audioixp_format(void *); 75 static int audioixp_channels(void *); 76 static int audioixp_rate(void *); 77 static uint64_t audioixp_count(void *); 78 static void audioixp_sync(void *, unsigned); 79 80 static audio_engine_ops_t audioixp_engine_ops = { 81 AUDIO_ENGINE_VERSION, 82 audioixp_open, 83 audioixp_close, 84 audioixp_start, 85 audioixp_stop, 86 audioixp_count, 87 audioixp_format, 88 audioixp_channels, 89 audioixp_rate, 90 audioixp_sync, 91 NULL, 92 NULL, 93 NULL 94 }; 95 96 /* 97 * We drive audioixp in stereo only, so we don't want to display controls 98 * that are used for multichannel codecs. Note that this multichannel 99 * configuration limitation is a problem for audioixp devices. 100 */ 101 const char *audioixp_remove_ac97[] = { 102 AUDIO_CTRL_ID_CENTER, 103 AUDIO_CTRL_ID_LFE, 104 AUDIO_CTRL_ID_SURROUND, 105 AUDIO_CTRL_ID_JACK1, 106 AUDIO_CTRL_ID_JACK2, 107 }; 108 109 /* 110 * interrupt handler 111 */ 112 static uint_t audioixp_intr(caddr_t); 113 114 /* 115 * Local Routine Prototypes 116 */ 117 static int audioixp_attach(dev_info_t *); 118 static int audioixp_detach(dev_info_t *); 119 static int audioixp_alloc_port(audioixp_state_t *, int); 120 static void audioixp_start_port(audioixp_port_t *); 121 static void audioixp_stop_port(audioixp_port_t *); 122 static void audioixp_reset_port(audioixp_port_t *); 123 static void audioixp_update_port(audioixp_port_t *); 124 125 static int audioixp_codec_sync(audioixp_state_t *); 126 static void audioixp_wr97(void *, uint8_t, uint16_t); 127 static uint16_t audioixp_rd97(void *, uint8_t); 128 static int audioixp_reset_ac97(audioixp_state_t *); 129 static int audioixp_map_regs(audioixp_state_t *); 130 static void audioixp_unmap_regs(audioixp_state_t *); 131 static int audioixp_chip_init(audioixp_state_t *); 132 static void audioixp_destroy(audioixp_state_t *); 133 134 /* 135 * Global variables, but used only by this file. 136 */ 137 138 /* 139 * DDI Structures 140 */ 141 142 /* Device operations structure */ 143 static struct dev_ops audioixp_dev_ops = { 144 DEVO_REV, /* devo_rev */ 145 0, /* devo_refcnt */ 146 NULL, /* devo_getinfo */ 147 nulldev, /* devo_identify - obsolete */ 148 nulldev, /* devo_probe */ 149 audioixp_ddi_attach, /* devo_attach */ 150 audioixp_ddi_detach, /* devo_detach */ 151 nodev, /* devo_reset */ 152 NULL, /* devi_cb_ops */ 153 NULL, /* devo_bus_ops */ 154 NULL, /* devo_power */ 155 audioixp_quiesce, /* devo_quiesce */ 156 }; 157 158 /* Linkage structure for loadable drivers */ 159 static struct modldrv audioixp_modldrv = { 160 &mod_driverops, /* drv_modops */ 161 IXP_MOD_NAME, /* drv_linkinfo */ 162 &audioixp_dev_ops, /* drv_dev_ops */ 163 }; 164 165 /* Module linkage structure */ 166 static struct modlinkage audioixp_modlinkage = { 167 MODREV_1, /* ml_rev */ 168 (void *)&audioixp_modldrv, /* ml_linkage */ 169 NULL /* NULL terminates the list */ 170 }; 171 172 /* 173 * device access attributes for register mapping 174 */ 175 static struct ddi_device_acc_attr dev_attr = { 176 DDI_DEVICE_ATTR_V0, 177 DDI_STRUCTURE_LE_ACC, 178 DDI_STRICTORDER_ACC 179 }; 180 static struct ddi_device_acc_attr buf_attr = { 181 DDI_DEVICE_ATTR_V0, 182 DDI_NEVERSWAP_ACC, 183 DDI_STRICTORDER_ACC 184 }; 185 186 /* 187 * DMA attributes of buffer descriptor list 188 */ 189 static ddi_dma_attr_t bdlist_dma_attr = { 190 DMA_ATTR_V0, /* version */ 191 0, /* addr_lo */ 192 0xffffffff, /* addr_hi */ 193 0x0000ffff, /* count_max */ 194 8, /* align, BDL must be aligned on a 8-byte boundary */ 195 0x3c, /* burstsize */ 196 8, /* minxfer, set to the size of a BDlist entry */ 197 0x0000ffff, /* maxxfer */ 198 0x00000fff, /* seg, set to the RAM pagesize of intel platform */ 199 1, /* sgllen, there's no scatter-gather list */ 200 8, /* granular, set to the value of minxfer */ 201 0 /* flags, use virtual address */ 202 }; 203 204 /* 205 * DMA attributes of buffers to be used to receive/send audio data 206 */ 207 static ddi_dma_attr_t sample_buf_dma_attr = { 208 DMA_ATTR_V0, 209 0, /* addr_lo */ 210 0xffffffff, /* addr_hi */ 211 0x0001fffe, /* count_max */ 212 4, /* align, data buffer is aligned on a 2-byte boundary */ 213 0x3c, /* burstsize */ 214 4, /* minxfer, set to the size of a sample data */ 215 0x0001ffff, /* maxxfer */ 216 0x0001ffff, /* seg */ 217 1, /* sgllen, no scatter-gather */ 218 4, /* granular, set to the value of minxfer */ 219 0, /* flags, use virtual address */ 220 }; 221 222 /* 223 * _init() 224 * 225 * Description: 226 * Driver initialization, called when driver is first loaded. 227 * This is how access is initially given to all the static structures. 228 * 229 * Arguments: 230 * None 231 * 232 * Returns: 233 * ddi_soft_state_init() status, see ddi_soft_state_init(9f), or 234 * mod_install() status, see mod_install(9f) 235 */ 236 int 237 _init(void) 238 { 239 int error; 240 241 audio_init_ops(&audioixp_dev_ops, IXP_NAME); 242 243 if ((error = mod_install(&audioixp_modlinkage)) != 0) { 244 audio_fini_ops(&audioixp_dev_ops); 245 } 246 247 return (error); 248 } 249 250 /* 251 * _fini() 252 * 253 * Description: 254 * Module de-initialization, called when the driver is to be unloaded. 255 * 256 * Arguments: 257 * None 258 * 259 * Returns: 260 * mod_remove() status, see mod_remove(9f) 261 */ 262 int 263 _fini(void) 264 { 265 int error; 266 267 if ((error = mod_remove(&audioixp_modlinkage)) != 0) { 268 return (error); 269 } 270 271 audio_fini_ops(&audioixp_dev_ops); 272 273 return (0); 274 } 275 276 /* 277 * _info() 278 * 279 * Description: 280 * Module information, returns information about the driver. 281 * 282 * Arguments: 283 * modinfo *modinfop Pointer to the opaque modinfo structure 284 * 285 * Returns: 286 * mod_info() status, see mod_info(9f) 287 */ 288 int 289 _info(struct modinfo *modinfop) 290 { 291 return (mod_info(&audioixp_modlinkage, modinfop)); 292 } 293 294 295 /* ******************* Driver Entry Points ********************************* */ 296 297 /* 298 * audioixp_ddi_attach() 299 * 300 * Description: 301 * Attach an instance of the audioixp driver. 302 * 303 * Arguments: 304 * dev_info_t *dip Pointer to the device's dev_info struct 305 * ddi_attach_cmd_t cmd Attach command 306 * 307 * Returns: 308 * DDI_SUCCESS The driver was initialized properly 309 * DDI_FAILURE The driver couldn't be initialized properly 310 */ 311 static int 312 audioixp_ddi_attach(dev_info_t *dip, ddi_attach_cmd_t cmd) 313 { 314 switch (cmd) { 315 case DDI_ATTACH: 316 return (audioixp_attach(dip)); 317 318 /* 319 * now, no suspend/resume supported. we'll do it in the future. 320 */ 321 case DDI_RESUME: 322 return (audioixp_resume(dip)); 323 default: 324 return (DDI_FAILURE); 325 } 326 } 327 328 /* 329 * audioixp_ddi_detach() 330 * 331 * Description: 332 * Detach an instance of the audioixp driver. 333 * 334 * Arguments: 335 * dev_info_t *dip Pointer to the device's dev_info struct 336 * ddi_detach_cmd_t cmd Detach command 337 * 338 * Returns: 339 * DDI_SUCCESS The driver was detached 340 * DDI_FAILURE The driver couldn't be detached 341 */ 342 static int 343 audioixp_ddi_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 344 { 345 switch (cmd) { 346 case DDI_DETACH: 347 return (audioixp_detach(dip)); 348 349 /* 350 * now, no suspend/resume supported. we'll do it in the future. 351 */ 352 case DDI_SUSPEND: 353 return (audioixp_suspend(dip)); 354 355 default: 356 return (DDI_FAILURE); 357 } 358 } 359 360 static void 361 audioixp_stop_dma(audioixp_state_t *statep) 362 { 363 CLR32(IXP_AUDIO_CMD, IXP_AUDIO_CMD_EN_OUT_DMA); 364 CLR32(IXP_AUDIO_CMD, IXP_AUDIO_CMD_EN_IN_DMA); 365 } 366 367 static void 368 audioixp_disable_intr(audioixp_state_t *statep) 369 { 370 PUT32(IXP_AUDIO_INT, GET32(IXP_AUDIO_INT)); 371 PUT32(IXP_AUDIO_INT_EN, 0); 372 } 373 374 /* 375 * quiesce(9E) entry point. 376 * 377 * This function is called when the system is single-threaded at high 378 * PIL with preemption disabled. Therefore, this function must not be blocked. 379 * 380 * This function returns DDI_SUCCESS on success, or DDI_FAILURE on failure. 381 * DDI_FAILURE indicates an error condition and should almost never happen. 382 */ 383 static int 384 audioixp_quiesce(dev_info_t *dip) 385 { 386 audioixp_state_t *statep; 387 388 statep = ddi_get_driver_private(dip); 389 ASSERT(statep != NULL); 390 391 /* disable HW interrupt */ 392 audioixp_disable_intr(statep); 393 394 /* stop DMA engines */ 395 audioixp_stop_dma(statep); 396 397 return (DDI_SUCCESS); 398 } 399 400 static int 401 audioixp_suspend(dev_info_t *dip) 402 { 403 audioixp_state_t *statep; 404 405 statep = ddi_get_driver_private(dip); 406 ASSERT(statep != NULL); 407 408 ac97_suspend(statep->ac97); 409 mutex_enter(&statep->inst_lock); 410 411 statep->suspended = B_TRUE; 412 413 audioixp_disable_intr(statep); 414 audioixp_stop_dma(statep); 415 416 mutex_exit(&statep->inst_lock); 417 return (DDI_SUCCESS); 418 } 419 420 static void 421 audioixp_resume_port(audioixp_port_t *port) 422 { 423 if (port != NULL) { 424 if (port->engine != NULL) { 425 audio_engine_reset(port->engine); 426 } 427 } 428 audioixp_reset_port(port); 429 if (port->started) { 430 audioixp_start_port(port); 431 } else { 432 audioixp_stop_port(port); 433 } 434 } 435 436 static int 437 audioixp_resume(dev_info_t *dip) 438 { 439 audioixp_state_t *statep; 440 audio_dev_t *adev; 441 audioixp_port_t *rec_port, *play_port; 442 443 statep = ddi_get_driver_private(dip); 444 adev = statep->adev; 445 ASSERT(statep != NULL); 446 447 if (audioixp_chip_init(statep) != DDI_SUCCESS) { 448 audio_dev_warn(adev, "DDI_RESUME failed to init chip"); 449 return (DDI_SUCCESS); 450 } 451 452 ac97_resume(statep->ac97); 453 mutex_enter(&statep->inst_lock); 454 statep->suspended = B_FALSE; 455 456 rec_port = statep->rec_port; 457 play_port = statep->play_port; 458 459 audioixp_resume_port(rec_port); 460 audioixp_resume_port(play_port); 461 462 mutex_exit(&statep->inst_lock); 463 return (DDI_SUCCESS); 464 } 465 /* 466 * audioixp_intr() 467 * 468 * Description: 469 * Interrupt service routine for both play and record. For play we 470 * get the next buffers worth of audio. For record we send it on to 471 * the mixer. 472 * 473 * There's a hardware pointer which indicate memory location where 474 * the hardware is processing. We check this pointer to decide whether 475 * to handle the buffer and how many buffers should be handled. 476 * Refer to ATI IXP400/450 Register Reference Manual, page 193,194. 477 * 478 * Arguments: 479 * caddr_t arg Pointer to the interrupting device's state 480 * structure 481 * 482 * Returns: 483 * DDI_INTR_CLAIMED Interrupt claimed and processed 484 * DDI_INTR_UNCLAIMED Interrupt not claimed, and thus ignored 485 */ 486 static uint_t 487 audioixp_intr(caddr_t arg) 488 { 489 audioixp_state_t *statep; 490 uint32_t sr; 491 int claimed = DDI_INTR_UNCLAIMED; 492 493 statep = (void *)arg; 494 mutex_enter(&statep->inst_lock); 495 496 sr = GET32(IXP_AUDIO_INT); 497 498 /* PCM in interrupt */ 499 if (sr & IXP_AUDIO_INT_IN_DMA) { 500 claimed = DDI_INTR_CLAIMED; 501 PUT32(IXP_AUDIO_INT, IXP_AUDIO_INT_IN_DMA); 502 } 503 504 /* PCM out interrupt */ 505 if (sr & IXP_AUDIO_INT_OUT_DMA) { 506 claimed = DDI_INTR_CLAIMED; 507 PUT32(IXP_AUDIO_INT, IXP_AUDIO_INT_OUT_DMA); 508 } 509 510 /* system is too busy to process the input stream, ignore it */ 511 if (sr & IXP_AUDIO_INT_IN_DMA_OVERFLOW) { 512 claimed = DDI_INTR_CLAIMED; 513 PUT32(IXP_AUDIO_INT, IXP_AUDIO_INT_IN_DMA_OVERFLOW); 514 } 515 516 /* System is too busy, ignore it */ 517 if (sr & IXP_AUDIO_INT_OUT_DMA_UNDERFLOW) { 518 claimed = DDI_INTR_CLAIMED; 519 PUT32(IXP_AUDIO_INT, IXP_AUDIO_INT_OUT_DMA_UNDERFLOW); 520 } 521 522 /* update the kernel interrupt statistics */ 523 if ((claimed == DDI_INTR_CLAIMED) && statep->ksp) { 524 IXP_KIOP(statep)->intrs[KSTAT_INTR_HARD]++; 525 } 526 527 mutex_exit(&statep->inst_lock); 528 529 if (sr & IXP_AUDIO_INT_IN_DMA) { 530 audio_engine_produce(statep->rec_port->engine); 531 } 532 if (sr & IXP_AUDIO_INT_OUT_DMA) { 533 audio_engine_consume(statep->play_port->engine); 534 } 535 536 return (claimed); 537 } 538 539 /* 540 * audioixp_open() 541 * 542 * Description: 543 * Opens a DMA engine for use. 544 * 545 * Arguments: 546 * void *arg The DMA engine to set up 547 * int flag Open flags 548 * unsigned *fragfrp Receives number of frames per fragment 549 * unsigned *nfragsp Receives number of fragments 550 * caddr_t *bufp Receives kernel data buffer 551 * 552 * Returns: 553 * 0 on success 554 * errno on failure 555 */ 556 static int 557 audioixp_open(void *arg, int flag, 558 unsigned *fragfrp, unsigned *nfragsp, caddr_t *bufp) 559 { 560 audioixp_port_t *port = arg; 561 562 _NOTE(ARGUNUSED(flag)); 563 564 port->started = B_FALSE; 565 port->count = 0; 566 port->offset = 0; 567 *fragfrp = port->fragfr; 568 *nfragsp = IXP_BD_NUMS; 569 *bufp = port->samp_kaddr; 570 571 mutex_enter(&port->statep->inst_lock); 572 audioixp_reset_port(port); 573 mutex_exit(&port->statep->inst_lock); 574 575 return (0); 576 } 577 578 /* 579 * audioixp_close() 580 * 581 * Description: 582 * Closes an audio DMA engine that was previously opened. Since 583 * nobody is using it, we take this opportunity to possibly power 584 * down the entire device. 585 * 586 * Arguments: 587 * void *arg The DMA engine to shut down 588 */ 589 static void 590 audioixp_close(void *arg) 591 { 592 audioixp_port_t *port = arg; 593 audioixp_state_t *statep = port->statep; 594 595 mutex_enter(&statep->inst_lock); 596 audioixp_stop_port(port); 597 port->started = B_FALSE; 598 mutex_exit(&statep->inst_lock); 599 } 600 601 /* 602 * audioixp_stop() 603 * 604 * Description: 605 * This is called by the framework to stop a port that is 606 * transferring data. 607 * 608 * Arguments: 609 * void *arg The DMA engine to stop 610 */ 611 static void 612 audioixp_stop(void *arg) 613 { 614 audioixp_port_t *port = arg; 615 audioixp_state_t *statep = port->statep; 616 617 mutex_enter(&statep->inst_lock); 618 if (port->started) { 619 audioixp_stop_port(port); 620 } 621 port->started = B_FALSE; 622 mutex_exit(&statep->inst_lock); 623 } 624 625 /* 626 * audioixp_start() 627 * 628 * Description: 629 * This is called by the framework to start a port transferring data. 630 * 631 * Arguments: 632 * void *arg The DMA engine to start 633 * 634 * Returns: 635 * 0 on success (never fails, errno if it did) 636 */ 637 static int 638 audioixp_start(void *arg) 639 { 640 audioixp_port_t *port = arg; 641 audioixp_state_t *statep = port->statep; 642 643 mutex_enter(&statep->inst_lock); 644 if (!port->started) { 645 audioixp_start_port(port); 646 port->started = B_TRUE; 647 } 648 mutex_exit(&statep->inst_lock); 649 return (0); 650 } 651 652 /* 653 * audioixp_format() 654 * 655 * Description: 656 * This is called by the framework to query the format for the device. 657 * 658 * Arguments: 659 * void *arg The DMA engine to query 660 * 661 * Returns: 662 * AUDIO_FORMAT_S16_LE 663 */ 664 static int 665 audioixp_format(void *arg) 666 { 667 _NOTE(ARGUNUSED(arg)); 668 669 return (AUDIO_FORMAT_S16_LE); 670 } 671 672 /* 673 * audioixp_channels() 674 * 675 * Description: 676 * This is called by the framework to query the channels for the device. 677 * 678 * Arguments: 679 * void *arg The DMA engine to query 680 * 681 * Returns: 682 * Number of channels for the device. 683 */ 684 static int 685 audioixp_channels(void *arg) 686 { 687 audioixp_port_t *port = arg; 688 689 return (port->nchan); 690 } 691 692 /* 693 * audioixp_rate() 694 * 695 * Description: 696 * This is called by the framework to query the rate of the device. 697 * 698 * Arguments: 699 * void *arg The DMA engine to query 700 * 701 * Returns: 702 * 48000 703 */ 704 static int 705 audioixp_rate(void *arg) 706 { 707 _NOTE(ARGUNUSED(arg)); 708 709 return (48000); 710 } 711 712 /* 713 * audioixp_count() 714 * 715 * Description: 716 * This is called by the framework to get the engine's frame counter 717 * 718 * Arguments: 719 * void *arg The DMA engine to query 720 * 721 * Returns: 722 * frame count for current engine 723 */ 724 static uint64_t 725 audioixp_count(void *arg) 726 { 727 audioixp_port_t *port = arg; 728 audioixp_state_t *statep = port->statep; 729 uint64_t val; 730 731 mutex_enter(&statep->inst_lock); 732 audioixp_update_port(port); 733 val = port->count; 734 mutex_exit(&statep->inst_lock); 735 736 return (val); 737 } 738 739 /* 740 * audioixp_sync() 741 * 742 * Description: 743 * This is called by the framework to synchronize DMA caches. 744 * 745 * Arguments: 746 * void *arg The DMA engine to sync 747 */ 748 static void 749 audioixp_sync(void *arg, unsigned nframes) 750 { 751 audioixp_port_t *port = arg; 752 _NOTE(ARGUNUSED(nframes)); 753 754 (void) ddi_dma_sync(port->samp_dmah, 0, 0, port->sync_dir); 755 } 756 757 /* *********************** Local Routines *************************** */ 758 759 /* 760 * audioixp_alloc_port() 761 * 762 * Description: 763 * This routine allocates the DMA handles and the memory for the 764 * DMA engines to use. It also configures the BDL lists properly 765 * for use. 766 * 767 * Arguments: 768 * dev_info_t *dip Pointer to the device's devinfo 769 * 770 * Returns: 771 * DDI_SUCCESS Registers successfully mapped 772 * DDI_FAILURE Registers not successfully mapped 773 */ 774 static int 775 audioixp_alloc_port(audioixp_state_t *statep, int num) 776 { 777 ddi_dma_cookie_t cookie; 778 uint_t count; 779 int dir; 780 unsigned caps; 781 char *prop; 782 audio_dev_t *adev; 783 audioixp_port_t *port; 784 uint32_t paddr; 785 int rc; 786 dev_info_t *dip; 787 audioixp_bd_entry_t *bdentry; 788 789 adev = statep->adev; 790 dip = statep->dip; 791 792 port = kmem_zalloc(sizeof (*port), KM_SLEEP); 793 port->statep = statep; 794 port->started = B_FALSE; 795 port->num = num; 796 797 switch (num) { 798 case IXP_REC: 799 statep->rec_port = port; 800 prop = "record-interrupts"; 801 dir = DDI_DMA_READ; 802 caps = ENGINE_INPUT_CAP; 803 port->sync_dir = DDI_DMA_SYNC_FORKERNEL; 804 port->nchan = 2; 805 break; 806 case IXP_PLAY: 807 statep->play_port = port; 808 prop = "play-interrupts"; 809 dir = DDI_DMA_WRITE; 810 caps = ENGINE_OUTPUT_CAP; 811 port->sync_dir = DDI_DMA_SYNC_FORDEV; 812 /* 813 * We allow for end users to configure more channels 814 * than just two, but we default to just two. The 815 * default stereo configuration works well. On the 816 * configurations we have tested, we've found that 817 * more than two channels (or rather 6 channels) can 818 * cause inexplicable noise. The noise is more 819 * noticeable when the system is running under load. 820 * (Holding the space bar in "top" while playing an 821 * MP3 is an easy way to recreate it.) End users who 822 * want to experiment, or have configurations that 823 * don't suffer from this, may increase the channels 824 * by setting this max-channels property. We leave it 825 * undocumented for now. 826 */ 827 port->nchan = ddi_prop_get_int(DDI_DEV_T_ANY, dip, 0, 828 "max-channels", 2); 829 port->nchan = min(ac97_num_channels(statep->ac97), 830 port->nchan); 831 port->nchan &= ~1; /* make sure its an even number */ 832 port->nchan = max(port->nchan, 2); 833 break; 834 default: 835 audio_dev_warn(adev, "bad port number (%d)!", num); 836 return (DDI_FAILURE); 837 } 838 839 port->intrs = ddi_prop_get_int(DDI_DEV_T_ANY, dip, 840 DDI_PROP_DONTPASS, prop, IXP_INTS); 841 842 /* make sure the values are good */ 843 if (port->intrs < IXP_MIN_INTS) { 844 audio_dev_warn(adev, "%s too low, %d, resetting to %d", 845 prop, port->intrs, IXP_INTS); 846 port->intrs = IXP_INTS; 847 } else if (port->intrs > IXP_MAX_INTS) { 848 audio_dev_warn(adev, "%s too high, %d, resetting to %d", 849 prop, port->intrs, IXP_INTS); 850 port->intrs = IXP_INTS; 851 } 852 853 /* 854 * Figure out how much space we need. Sample rate is 48kHz, and 855 * we need to store 8 chunks. (Note that this means that low 856 * interrupt frequencies will require more RAM.) 857 */ 858 port->fragfr = 48000 / port->intrs; 859 port->fragfr = IXP_ROUNDUP(port->fragfr, IXP_MOD_SIZE); 860 port->fragsz = port->fragfr * port->nchan * 2; 861 port->samp_size = port->fragsz * IXP_BD_NUMS; 862 863 /* allocate dma handle */ 864 rc = ddi_dma_alloc_handle(dip, &sample_buf_dma_attr, DDI_DMA_SLEEP, 865 NULL, &port->samp_dmah); 866 if (rc != DDI_SUCCESS) { 867 audio_dev_warn(adev, "ddi_dma_alloc_handle failed: %d", rc); 868 return (DDI_FAILURE); 869 } 870 /* allocate DMA buffer */ 871 rc = ddi_dma_mem_alloc(port->samp_dmah, port->samp_size, &buf_attr, 872 DDI_DMA_CONSISTENT, DDI_DMA_SLEEP, NULL, &port->samp_kaddr, 873 &port->samp_size, &port->samp_acch); 874 if (rc == DDI_FAILURE) { 875 audio_dev_warn(adev, "dma_mem_alloc failed"); 876 return (DDI_FAILURE); 877 } 878 879 /* bind DMA buffer */ 880 rc = ddi_dma_addr_bind_handle(port->samp_dmah, NULL, 881 port->samp_kaddr, port->samp_size, dir|DDI_DMA_CONSISTENT, 882 DDI_DMA_SLEEP, NULL, &cookie, &count); 883 if ((rc != DDI_DMA_MAPPED) || (count != 1)) { 884 audio_dev_warn(adev, 885 "ddi_dma_addr_bind_handle failed: %d", rc); 886 return (DDI_FAILURE); 887 } 888 port->samp_paddr = cookie.dmac_address; 889 890 /* 891 * now, from here we allocate DMA memory for buffer descriptor list. 892 * we allocate adjacent DMA memory for all DMA engines. 893 */ 894 rc = ddi_dma_alloc_handle(dip, &bdlist_dma_attr, DDI_DMA_SLEEP, 895 NULL, &port->bdl_dmah); 896 if (rc != DDI_SUCCESS) { 897 audio_dev_warn(adev, "ddi_dma_alloc_handle(bdlist) failed"); 898 return (DDI_FAILURE); 899 } 900 901 /* 902 * we allocate all buffer descriptors lists in continuous dma memory. 903 */ 904 port->bdl_size = sizeof (audioixp_bd_entry_t) * IXP_BD_NUMS; 905 rc = ddi_dma_mem_alloc(port->bdl_dmah, port->bdl_size, 906 &dev_attr, DDI_DMA_CONSISTENT, DDI_DMA_SLEEP, NULL, 907 &port->bdl_kaddr, &port->bdl_size, &port->bdl_acch); 908 if (rc != DDI_SUCCESS) { 909 audio_dev_warn(adev, "ddi_dma_mem_alloc(bdlist) failed"); 910 return (DDI_FAILURE); 911 } 912 913 rc = ddi_dma_addr_bind_handle(port->bdl_dmah, NULL, port->bdl_kaddr, 914 port->bdl_size, DDI_DMA_WRITE|DDI_DMA_CONSISTENT, DDI_DMA_SLEEP, 915 NULL, &cookie, &count); 916 if ((rc != DDI_DMA_MAPPED) || (count != 1)) { 917 audio_dev_warn(adev, "addr_bind_handle failed"); 918 return (DDI_FAILURE); 919 } 920 port->bdl_paddr = cookie.dmac_address; 921 922 /* 923 * Wire up the BD list. 924 */ 925 paddr = port->samp_paddr; 926 bdentry = (void *)port->bdl_kaddr; 927 928 for (int i = 0; i < IXP_BD_NUMS; i++) { 929 930 /* set base address of buffer */ 931 ddi_put32(port->bdl_acch, &bdentry->buf_base, paddr); 932 ddi_put16(port->bdl_acch, &bdentry->status, 0); 933 ddi_put16(port->bdl_acch, &bdentry->buf_len, port->fragsz / 4); 934 ddi_put32(port->bdl_acch, &bdentry->next, port->bdl_paddr + 935 (((i + 1) % IXP_BD_NUMS) * sizeof (audioixp_bd_entry_t))); 936 paddr += port->fragsz; 937 bdentry++; 938 } 939 (void) ddi_dma_sync(port->bdl_dmah, 0, 0, DDI_DMA_SYNC_FORDEV); 940 941 port->engine = audio_engine_alloc(&audioixp_engine_ops, caps); 942 if (port->engine == NULL) { 943 audio_dev_warn(adev, "audio_engine_alloc failed"); 944 return (DDI_FAILURE); 945 } 946 947 audio_engine_set_private(port->engine, port); 948 audio_dev_add_engine(adev, port->engine); 949 950 return (DDI_SUCCESS); 951 } 952 953 /* 954 * audioixp_free_port() 955 * 956 * Description: 957 * This routine unbinds the DMA cookies, frees the DMA buffers, 958 * deallocates the DMA handles. 959 * 960 * Arguments: 961 * audioixp_port_t *port The port structure for a DMA engine. 962 */ 963 static void 964 audioixp_free_port(audioixp_port_t *port) 965 { 966 if (port == NULL) 967 return; 968 969 if (port->engine) { 970 audio_dev_remove_engine(port->statep->adev, port->engine); 971 audio_engine_free(port->engine); 972 } 973 if (port->bdl_paddr) { 974 (void) ddi_dma_unbind_handle(port->bdl_dmah); 975 } 976 if (port->bdl_acch) { 977 ddi_dma_mem_free(&port->bdl_acch); 978 } 979 if (port->bdl_dmah) { 980 ddi_dma_free_handle(&port->bdl_dmah); 981 } 982 if (port->samp_paddr) { 983 (void) ddi_dma_unbind_handle(port->samp_dmah); 984 } 985 if (port->samp_acch) { 986 ddi_dma_mem_free(&port->samp_acch); 987 } 988 if (port->samp_dmah) { 989 ddi_dma_free_handle(&port->samp_dmah); 990 } 991 kmem_free(port, sizeof (*port)); 992 } 993 994 /* 995 * audioixp_start_port() 996 * 997 * Description: 998 * This routine starts the DMA engine. 999 * 1000 * Arguments: 1001 * audioixp_port_t *port Port of DMA engine to start. 1002 */ 1003 static void 1004 audioixp_start_port(audioixp_port_t *port) 1005 { 1006 audioixp_state_t *statep = port->statep; 1007 1008 ASSERT(mutex_owned(&statep->inst_lock)); 1009 1010 /* if suspended, then do nothing else */ 1011 if (statep->suspended) { 1012 return; 1013 } 1014 1015 if (port->num == IXP_REC) { 1016 SET32(IXP_AUDIO_CMD, IXP_AUDIO_CMD_EN_IN); 1017 } else { 1018 SET32(IXP_AUDIO_CMD, IXP_AUDIO_CMD_EN_OUT); 1019 } 1020 } 1021 1022 /* 1023 * audioixp_stop_port() 1024 * 1025 * Description: 1026 * This routine stops the DMA engine. 1027 * 1028 * Arguments: 1029 * audioixp_port_t *port Port of DMA engine to stop. 1030 */ 1031 static void 1032 audioixp_stop_port(audioixp_port_t *port) 1033 { 1034 audioixp_state_t *statep = port->statep; 1035 1036 ASSERT(mutex_owned(&statep->inst_lock)); 1037 1038 /* if suspended, then do nothing else */ 1039 if (statep->suspended) { 1040 return; 1041 } 1042 if (port->num == IXP_REC) { 1043 CLR32(IXP_AUDIO_CMD, IXP_AUDIO_CMD_EN_IN); 1044 } else { 1045 CLR32(IXP_AUDIO_CMD, IXP_AUDIO_CMD_EN_OUT); 1046 } 1047 } 1048 1049 /* 1050 * audioixp_reset_port() 1051 * 1052 * Description: 1053 * This routine resets the DMA engine pareparing it for work. 1054 * 1055 * Arguments: 1056 * audioixp_port_t *port Port of DMA engine to reset. 1057 */ 1058 static void 1059 audioixp_reset_port(audioixp_port_t *port) 1060 { 1061 audioixp_state_t *statep = port->statep; 1062 1063 ASSERT(mutex_owned(&statep->inst_lock)); 1064 1065 port->offset = 0; 1066 1067 if (statep->suspended) 1068 return; 1069 1070 /* 1071 * Perform full reset of the engine, and enable its interrupts 1072 * but leave it turned off. 1073 */ 1074 if (port->num == IXP_REC) { 1075 PUT32(IXP_AUDIO_FIFO_FLUSH, IXP_AUDIO_FIFO_FLUSH_IN); 1076 SET32(IXP_AUDIO_CMD, IXP_AUDIO_CMD_INTER_IN); 1077 1078 SET32(IXP_AUDIO_CMD, IXP_AUDIO_CMD_EN_IN_DMA); 1079 PUT32(IXP_AUDIO_IN_DMA_LINK_P, 1080 port->bdl_paddr | IXP_AUDIO_IN_DMA_LINK_P_EN); 1081 1082 } else { 1083 uint32_t slot = GET32(IXP_AUDIO_OUT_DMA_SLOT_EN_THRESHOLD); 1084 PUT32(IXP_AUDIO_FIFO_FLUSH, IXP_AUDIO_FIFO_FLUSH_OUT); 1085 /* clear all slots */ 1086 slot &= ~ (IXP_AUDIO_OUT_DMA_SLOT_3 | 1087 IXP_AUDIO_OUT_DMA_SLOT_4 | 1088 IXP_AUDIO_OUT_DMA_SLOT_5 | 1089 IXP_AUDIO_OUT_DMA_SLOT_6 | 1090 IXP_AUDIO_OUT_DMA_SLOT_7 | 1091 IXP_AUDIO_OUT_DMA_SLOT_8 | 1092 IXP_AUDIO_OUT_DMA_SLOT_9 | 1093 IXP_AUDIO_OUT_DMA_SLOT_10 | 1094 IXP_AUDIO_OUT_DMA_SLOT_11 | 1095 IXP_AUDIO_OUT_DMA_SLOT_12); 1096 /* enable AC'97 output slots (depending on output channels) */ 1097 slot |= IXP_AUDIO_OUT_DMA_SLOT_3 | 1098 IXP_AUDIO_OUT_DMA_SLOT_4; 1099 if (port->nchan >= 4) { 1100 slot |= IXP_AUDIO_OUT_DMA_SLOT_6 | 1101 IXP_AUDIO_OUT_DMA_SLOT_9; 1102 } 1103 if (port->nchan >= 6) { 1104 slot |= IXP_AUDIO_OUT_DMA_SLOT_7 | 1105 IXP_AUDIO_OUT_DMA_SLOT_8; 1106 } 1107 1108 PUT32(IXP_AUDIO_OUT_DMA_SLOT_EN_THRESHOLD, slot); 1109 1110 SET32(IXP_AUDIO_CMD, IXP_AUDIO_CMD_INTER_OUT); 1111 1112 SET32(IXP_AUDIO_CMD, IXP_AUDIO_CMD_EN_OUT_DMA); 1113 PUT32(IXP_AUDIO_OUT_DMA_LINK_P, 1114 port->bdl_paddr | IXP_AUDIO_OUT_DMA_LINK_P_EN); 1115 } 1116 } 1117 1118 /* 1119 * audioixp_update_port() 1120 * 1121 * Description: 1122 * This routine updates the ports frame counter from hardware, and 1123 * gracefully handles wraps. 1124 * 1125 * Arguments: 1126 * audioixp_port_t *port The port to update. 1127 */ 1128 static void 1129 audioixp_update_port(audioixp_port_t *port) 1130 { 1131 audioixp_state_t *statep = port->statep; 1132 unsigned regoff; 1133 unsigned n; 1134 int loop; 1135 uint32_t offset; 1136 uint32_t paddr; 1137 1138 if (statep->suspended) { 1139 return; 1140 } 1141 if (port->num == IXP_REC) { 1142 regoff = IXP_AUDIO_IN_DMA_DT_CUR; 1143 } else { 1144 regoff = IXP_AUDIO_OUT_DMA_DT_CUR; 1145 } 1146 1147 /* 1148 * Apparently it may take several tries to get an update on the 1149 * position. Is this a hardware bug? 1150 */ 1151 for (loop = 100; loop; loop--) { 1152 paddr = GET32(regoff); 1153 1154 /* make sure address is reasonable */ 1155 if ((paddr < port->samp_paddr) || 1156 (paddr >= (port->samp_paddr + port->samp_size))) { 1157 continue; 1158 } 1159 1160 offset = paddr - port->samp_paddr; 1161 1162 if (offset >= port->offset) { 1163 n = offset - port->offset; 1164 } else { 1165 n = offset + (port->samp_size - port->offset); 1166 } 1167 port->offset = offset; 1168 port->count += (n / (port->nchan * sizeof (uint16_t))); 1169 return; 1170 } 1171 1172 audio_dev_warn(statep->adev, "Unable to update count (h/w bug?)"); 1173 } 1174 1175 1176 /* 1177 * audioixp_map_regs() 1178 * 1179 * Description: 1180 * The registers are mapped in. 1181 * 1182 * Arguments: 1183 * audioixp_state_t *state The device's state structure 1184 * 1185 * Returns: 1186 * DDI_SUCCESS Registers successfully mapped 1187 * DDI_FAILURE Registers not successfully mapped 1188 */ 1189 static int 1190 audioixp_map_regs(audioixp_state_t *statep) 1191 { 1192 dev_info_t *dip = statep->dip; 1193 1194 /* map PCI config space */ 1195 if (pci_config_setup(statep->dip, &statep->pcih) == DDI_FAILURE) { 1196 audio_dev_warn(statep->adev, "unable to map PCI config space"); 1197 return (DDI_FAILURE); 1198 } 1199 1200 /* map audio mixer register */ 1201 if ((ddi_regs_map_setup(dip, IXP_IO_AM_REGS, &statep->regsp, 0, 0, 1202 &dev_attr, &statep->regsh)) != DDI_SUCCESS) { 1203 audio_dev_warn(statep->adev, "unable to map audio registers"); 1204 return (DDI_FAILURE); 1205 } 1206 return (DDI_SUCCESS); 1207 } 1208 1209 /* 1210 * audioixp_unmap_regs() 1211 * 1212 * Description: 1213 * This routine unmaps control registers. 1214 * 1215 * Arguments: 1216 * audioixp_state_t *state The device's state structure 1217 */ 1218 static void 1219 audioixp_unmap_regs(audioixp_state_t *statep) 1220 { 1221 if (statep->regsh) { 1222 ddi_regs_map_free(&statep->regsh); 1223 } 1224 1225 if (statep->pcih) { 1226 pci_config_teardown(&statep->pcih); 1227 } 1228 } 1229 1230 /* 1231 * audioixp_codec_ready() 1232 * 1233 * Description: 1234 * This routine checks the state of codecs. It checks the flag to confirm 1235 * that primary codec is ready. 1236 * 1237 * Arguments: 1238 * audioixp_state_t *state The device's state structure 1239 * 1240 * Returns: 1241 * DDI_SUCCESS codec is ready 1242 * DDI_FAILURE codec is not ready 1243 */ 1244 static int 1245 audioixp_codec_ready(audioixp_state_t *statep) 1246 { 1247 uint32_t sr; 1248 1249 PUT32(IXP_AUDIO_INT, 0xffffffff); 1250 drv_usecwait(1000); 1251 1252 sr = GET32(IXP_AUDIO_INT); 1253 if (sr & IXP_AUDIO_INT_CODEC0_NOT_READY) { 1254 PUT32(IXP_AUDIO_INT, IXP_AUDIO_INT_CODEC0_NOT_READY); 1255 audio_dev_warn(statep->adev, "primary codec not ready"); 1256 1257 return (DDI_FAILURE); 1258 } 1259 return (DDI_SUCCESS); 1260 } 1261 1262 /* 1263 * audioixp_codec_sync() 1264 * 1265 * Description: 1266 * Serialize access to the AC97 audio mixer registers. 1267 * 1268 * Arguments: 1269 * audioixp_state_t *state The device's state structure 1270 * 1271 * Returns: 1272 * DDI_SUCCESS Ready for an I/O access to the codec 1273 * DDI_FAILURE An I/O access is currently in progress, can't 1274 * perform another I/O access. 1275 */ 1276 static int 1277 audioixp_codec_sync(audioixp_state_t *statep) 1278 { 1279 int i; 1280 uint32_t cmd; 1281 1282 for (i = 0; i < 300; i++) { 1283 cmd = GET32(IXP_AUDIO_OUT_PHY_ADDR_DATA); 1284 if (!(cmd & IXP_AUDIO_OUT_PHY_EN)) { 1285 return (DDI_SUCCESS); 1286 } 1287 drv_usecwait(10); 1288 } 1289 1290 audio_dev_warn(statep->adev, "unable to synchronize codec"); 1291 return (DDI_FAILURE); 1292 } 1293 1294 /* 1295 * audioixp_rd97() 1296 * 1297 * Description: 1298 * Get the specific AC97 Codec register. 1299 * 1300 * Arguments: 1301 * void *arg The device's state structure 1302 * uint8_t reg AC97 register number 1303 * 1304 * Returns: 1305 * Register value. 1306 */ 1307 static uint16_t 1308 audioixp_rd97(void *arg, uint8_t reg) 1309 { 1310 audioixp_state_t *statep = arg; 1311 uint32_t value; 1312 uint32_t result; 1313 1314 if (audioixp_codec_sync(statep) != DDI_SUCCESS) 1315 return (0xffff); 1316 1317 value = IXP_AUDIO_OUT_PHY_PRIMARY_CODEC | 1318 IXP_AUDIO_OUT_PHY_READ | 1319 IXP_AUDIO_OUT_PHY_EN | 1320 ((unsigned)reg << IXP_AUDIO_OUT_PHY_ADDR_SHIFT); 1321 PUT32(IXP_AUDIO_OUT_PHY_ADDR_DATA, value); 1322 1323 if (audioixp_codec_sync(statep) != DDI_SUCCESS) 1324 return (0xffff); 1325 1326 for (int i = 0; i < 300; i++) { 1327 result = GET32(IXP_AUDIO_IN_PHY_ADDR_DATA); 1328 if (result & IXP_AUDIO_IN_PHY_READY) { 1329 return (result >> IXP_AUDIO_IN_PHY_DATA_SHIFT); 1330 } 1331 drv_usecwait(10); 1332 } 1333 1334 done: 1335 audio_dev_warn(statep->adev, "time out reading codec reg %d", reg); 1336 return (0xffff); 1337 } 1338 1339 /* 1340 * audioixp_wr97() 1341 * 1342 * Description: 1343 * Set the specific AC97 Codec register. 1344 * 1345 * Arguments: 1346 * void *arg The device's state structure 1347 * uint8_t reg AC97 register number 1348 * uint16_t data The data want to be set 1349 */ 1350 static void 1351 audioixp_wr97(void *arg, uint8_t reg, uint16_t data) 1352 { 1353 audioixp_state_t *statep = arg; 1354 uint32_t value; 1355 1356 if (audioixp_codec_sync(statep) != DDI_SUCCESS) { 1357 return; 1358 } 1359 1360 value = IXP_AUDIO_OUT_PHY_PRIMARY_CODEC | 1361 IXP_AUDIO_OUT_PHY_WRITE | 1362 IXP_AUDIO_OUT_PHY_EN | 1363 ((unsigned)reg << IXP_AUDIO_OUT_PHY_ADDR_SHIFT) | 1364 ((unsigned)data << IXP_AUDIO_OUT_PHY_DATA_SHIFT); 1365 PUT32(IXP_AUDIO_OUT_PHY_ADDR_DATA, value); 1366 1367 (void) audioixp_rd97(statep, reg); 1368 } 1369 1370 /* 1371 * audioixp_reset_ac97() 1372 * 1373 * Description: 1374 * Reset AC97 Codec register. 1375 * 1376 * Arguments: 1377 * audioixp_state_t *state The device's state structure 1378 * 1379 * Returns: 1380 * DDI_SUCCESS Reset the codec successfully 1381 * DDI_FAILURE Failed to reset the codec 1382 */ 1383 static int 1384 audioixp_reset_ac97(audioixp_state_t *statep) 1385 { 1386 uint32_t cmd; 1387 int i; 1388 1389 CLR32(IXP_AUDIO_CMD, IXP_AUDIO_CMD_POWER_DOWN); 1390 drv_usecwait(10); 1391 1392 /* register reset */ 1393 SET32(IXP_AUDIO_CMD, IXP_AUDIO_CMD_AC_SOFT_RESET); 1394 /* force a read to flush caches */ 1395 (void) GET32(IXP_AUDIO_CMD); 1396 1397 drv_usecwait(10); 1398 CLR32(IXP_AUDIO_CMD, IXP_AUDIO_CMD_AC_SOFT_RESET); 1399 1400 /* cold reset */ 1401 for (i = 0; i < 300; i++) { 1402 cmd = GET32(IXP_AUDIO_CMD); 1403 if (cmd & IXP_AUDIO_CMD_AC_ACTIVE) { 1404 cmd |= IXP_AUDIO_CMD_AC_RESET | IXP_AUDIO_CMD_AC_SYNC; 1405 PUT32(IXP_AUDIO_CMD, cmd); 1406 return (DDI_SUCCESS); 1407 } 1408 cmd &= ~IXP_AUDIO_CMD_AC_RESET; 1409 cmd |= IXP_AUDIO_CMD_AC_SYNC; 1410 PUT32(IXP_AUDIO_CMD, cmd); 1411 (void) GET32(IXP_AUDIO_CMD); 1412 drv_usecwait(10); 1413 cmd |= IXP_AUDIO_CMD_AC_RESET; 1414 PUT32(IXP_AUDIO_CMD, cmd); 1415 drv_usecwait(10); 1416 } 1417 1418 audio_dev_warn(statep->adev, "AC'97 reset timed out"); 1419 return (DDI_FAILURE); 1420 } 1421 1422 /* 1423 * audioixp_chip_init() 1424 * 1425 * Description: 1426 * This routine initializes ATI IXP audio controller and the AC97 1427 * codec. 1428 * 1429 * Arguments: 1430 * audioixp_state_t *state The device's state structure 1431 * 1432 * Returns: 1433 * DDI_SUCCESS The hardware was initialized properly 1434 * DDI_FAILURE The hardware couldn't be initialized properly 1435 */ 1436 static int 1437 audioixp_chip_init(audioixp_state_t *statep) 1438 { 1439 /* 1440 * put the audio controller into quiet state, everything off 1441 */ 1442 CLR32(IXP_AUDIO_CMD, IXP_AUDIO_CMD_EN_OUT_DMA); 1443 CLR32(IXP_AUDIO_CMD, IXP_AUDIO_CMD_EN_IN_DMA); 1444 1445 /* AC97 reset */ 1446 if (audioixp_reset_ac97(statep) != DDI_SUCCESS) { 1447 audio_dev_warn(statep->adev, "AC97 codec reset failed"); 1448 return (DDI_FAILURE); 1449 } 1450 1451 if (audioixp_codec_ready(statep) != DDI_SUCCESS) { 1452 audio_dev_warn(statep->adev, "AC97 codec not ready"); 1453 return (DDI_FAILURE); 1454 } 1455 1456 /* enable interrupts */ 1457 PUT32(IXP_AUDIO_INT, 0xffffffff); 1458 PUT32( 1459 IXP_AUDIO_INT_EN, 1460 IXP_AUDIO_INT_EN_IN_DMA_OVERFLOW | 1461 IXP_AUDIO_INT_EN_STATUS | 1462 IXP_AUDIO_INT_EN_OUT_DMA_UNDERFLOW); 1463 return (DDI_SUCCESS); 1464 1465 } /* audioixp_chip_init() */ 1466 1467 /* 1468 * audioixp_attach() 1469 * 1470 * Description: 1471 * Attach an instance of the audioixp driver. This routine does 1472 * the device dependent attach tasks. 1473 * 1474 * Arguments: 1475 * dev_info_t *dip Pointer to the device's dev_info struct 1476 * ddi_attach_cmd_t cmd Attach command 1477 * 1478 * Returns: 1479 * DDI_SUCCESS The driver was initialized properly 1480 * DDI_FAILURE The driver couldn't be initialized properly 1481 */ 1482 static int 1483 audioixp_attach(dev_info_t *dip) 1484 { 1485 uint16_t cmdeg; 1486 audioixp_state_t *statep; 1487 audio_dev_t *adev; 1488 uint32_t devid; 1489 const char *name; 1490 const char *rev; 1491 1492 /* we don't support high level interrupts in the driver */ 1493 if (ddi_intr_hilevel(dip, 0) != 0) { 1494 cmn_err(CE_WARN, 1495 "!%s%d: unsupported high level interrupt", 1496 ddi_driver_name(dip), ddi_get_instance(dip)); 1497 return (DDI_FAILURE); 1498 } 1499 1500 /* allocate the soft state structure */ 1501 statep = kmem_zalloc(sizeof (*statep), KM_SLEEP); 1502 statep->dip = dip; 1503 ddi_set_driver_private(dip, statep); 1504 1505 if (ddi_get_iblock_cookie(dip, 0, &statep->iblock) != DDI_SUCCESS) { 1506 cmn_err(CE_WARN, 1507 "!%s%d: cannot get iblock cookie", 1508 ddi_driver_name(dip), ddi_get_instance(dip)); 1509 kmem_free(statep, sizeof (*statep)); 1510 return (DDI_FAILURE); 1511 } 1512 mutex_init(&statep->inst_lock, NULL, MUTEX_DRIVER, statep->iblock); 1513 1514 /* allocate framework audio device */ 1515 if ((adev = audio_dev_alloc(dip, 0)) == NULL) { 1516 cmn_err(CE_WARN, "!%s%d: unable to allocate audio dev", 1517 ddi_driver_name(dip), ddi_get_instance(dip)); 1518 goto error; 1519 } 1520 statep->adev = adev; 1521 1522 /* map in the registers */ 1523 if (audioixp_map_regs(statep) != DDI_SUCCESS) { 1524 audio_dev_warn(adev, "couldn't map registers"); 1525 goto error; 1526 } 1527 1528 /* set device information -- this could be smarter */ 1529 devid = ((pci_config_get16(statep->pcih, PCI_CONF_VENID)) << 16) | 1530 pci_config_get16(statep->pcih, PCI_CONF_DEVID); 1531 1532 name = "ATI AC'97"; 1533 switch (devid) { 1534 case IXP_PCI_ID_200: 1535 rev = "IXP150"; 1536 break; 1537 case IXP_PCI_ID_300: 1538 rev = "SB300"; 1539 break; 1540 case IXP_PCI_ID_400: 1541 if (pci_config_get8(statep->pcih, PCI_CONF_REVID) & 0x80) { 1542 rev = "SB450"; 1543 } else { 1544 rev = "SB400"; 1545 } 1546 break; 1547 case IXP_PCI_ID_SB600: 1548 rev = "SB600"; 1549 break; 1550 default: 1551 rev = "Unknown"; 1552 break; 1553 } 1554 audio_dev_set_description(adev, name); 1555 audio_dev_set_version(adev, rev); 1556 1557 /* set PCI command register */ 1558 cmdeg = pci_config_get16(statep->pcih, PCI_CONF_COMM); 1559 pci_config_put16(statep->pcih, PCI_CONF_COMM, 1560 cmdeg | PCI_COMM_IO | PCI_COMM_MAE); 1561 1562 statep->ac97 = ac97_alloc(dip, audioixp_rd97, audioixp_wr97, statep); 1563 if (statep->ac97 == NULL) { 1564 audio_dev_warn(adev, "failed to allocate ac97 handle"); 1565 goto error; 1566 } 1567 1568 /* allocate port structures */ 1569 if ((audioixp_alloc_port(statep, IXP_PLAY) != DDI_SUCCESS) || 1570 (audioixp_alloc_port(statep, IXP_REC) != DDI_SUCCESS)) { 1571 goto error; 1572 } 1573 1574 /* 1575 * If we have locked in a stereo configuration, then don't expose 1576 * multichannel-specific AC'97 codec controls. 1577 */ 1578 if (statep->play_port->nchan == 2) { 1579 int i; 1580 ac97_ctrl_t *ctrl; 1581 const char *name; 1582 1583 for (i = 0; (name = audioixp_remove_ac97[i]) != NULL; i++) { 1584 ctrl = ac97_control_find(statep->ac97, name); 1585 if (ctrl != NULL) { 1586 ac97_control_unregister(ctrl); 1587 } 1588 } 1589 } 1590 1591 /* set up kernel statistics */ 1592 if ((statep->ksp = kstat_create(IXP_NAME, ddi_get_instance(dip), 1593 IXP_NAME, "controller", KSTAT_TYPE_INTR, 1, 1594 KSTAT_FLAG_PERSISTENT)) != NULL) { 1595 kstat_install(statep->ksp); 1596 } 1597 1598 1599 if (audioixp_chip_init(statep) != DDI_SUCCESS) { 1600 audio_dev_warn(statep->adev, "failed to init chip"); 1601 goto error; 1602 } 1603 1604 /* initialize the AC'97 part */ 1605 if (ac97_init(statep->ac97, adev) != DDI_SUCCESS) { 1606 audio_dev_warn(adev, "ac'97 initialization failed"); 1607 goto error; 1608 } 1609 1610 /* set up the interrupt handler */ 1611 if (ddi_add_intr(dip, 0, &statep->iblock, NULL, audioixp_intr, 1612 (caddr_t)statep) != DDI_SUCCESS) { 1613 audio_dev_warn(adev, "bad interrupt specification"); 1614 } 1615 statep->intr_added = B_TRUE; 1616 1617 if (audio_dev_register(adev) != DDI_SUCCESS) { 1618 audio_dev_warn(adev, "unable to register with framework"); 1619 goto error; 1620 } 1621 1622 ddi_report_dev(dip); 1623 1624 return (DDI_SUCCESS); 1625 1626 error: 1627 audioixp_destroy(statep); 1628 return (DDI_FAILURE); 1629 } 1630 1631 /* 1632 * audioixp_detach() 1633 * 1634 * Description: 1635 * Detach an instance of the audioixp driver. 1636 * 1637 * Arguments: 1638 * dev_info_t *dip Pointer to the device's dev_info struct 1639 * 1640 * Returns: 1641 * DDI_SUCCESS The driver was detached 1642 * DDI_FAILURE The driver couldn't be detached 1643 */ 1644 static int 1645 audioixp_detach(dev_info_t *dip) 1646 { 1647 audioixp_state_t *statep; 1648 1649 statep = ddi_get_driver_private(dip); 1650 1651 if (audio_dev_unregister(statep->adev) != DDI_SUCCESS) { 1652 return (DDI_FAILURE); 1653 } 1654 1655 audioixp_destroy(statep); 1656 return (DDI_SUCCESS); 1657 } 1658 1659 /* 1660 * audioixp_destroy() 1661 * 1662 * Description: 1663 * This routine releases all resources held by the device instance, 1664 * as part of either detach or a failure in attach. 1665 * 1666 * Arguments: 1667 * audioixp_state_t *state The device soft state. 1668 */ 1669 void 1670 audioixp_destroy(audioixp_state_t *statep) 1671 { 1672 if (!statep->suspended) { 1673 PUT32(IXP_AUDIO_INT, GET32(IXP_AUDIO_INT)); 1674 PUT32(IXP_AUDIO_INT_EN, 0); 1675 1676 /* 1677 * put the audio controller into quiet state, everything off 1678 */ 1679 CLR32(IXP_AUDIO_CMD, IXP_AUDIO_CMD_EN_OUT_DMA); 1680 CLR32(IXP_AUDIO_CMD, IXP_AUDIO_CMD_EN_IN_DMA); 1681 } 1682 1683 if (statep->intr_added) { 1684 ddi_remove_intr(statep->dip, 0, statep->iblock); 1685 } 1686 if (statep->ksp) { 1687 kstat_delete(statep->ksp); 1688 } 1689 1690 audioixp_free_port(statep->play_port); 1691 audioixp_free_port(statep->rec_port); 1692 1693 audioixp_unmap_regs(statep); 1694 1695 if (statep->ac97) { 1696 ac97_free(statep->ac97); 1697 } 1698 1699 if (statep->adev) { 1700 audio_dev_free(statep->adev); 1701 } 1702 1703 mutex_destroy(&statep->inst_lock); 1704 kmem_free(statep, sizeof (*statep)); 1705 } 1706