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 2009 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 27 /* 28 * audio810 Audio Driver 29 * 30 * The driver is primarily targeted at providing audio support for the 31 * W1100z and W2100z systems, which use the AMD 8111 audio core and 32 * the Realtek ALC 655 codec. The ALC 655 chip supports only fixed 48k 33 * sample rate. However, the audio core of AMD 8111 is completely 34 * compatible to the Intel ICHx chips (Intel 8x0 chipsets), so the 35 * driver can work for the ICHx. We only support the 48k maximum 36 * rate, since we only have a single PCM out channel. 37 * 38 * The AMD 8111 audio core, as an AC'97 controller, has independent 39 * channels for PCM in, PCM out, mic in, modem in, and modem out. 40 * The AC'97 controller is a PCI bus master with scatter/gather 41 * support. Each channel has a DMA engine. Currently, we use only 42 * the PCM in and PCM out channels. Each DMA engine uses one buffer 43 * descriptor list. And the buffer descriptor list is an array of up 44 * to 32 entries, each of which describes a data buffer. Each entry 45 * contains a pointer to a data buffer, control bits, and the length 46 * of the buffer being pointed to, where the length is expressed as 47 * the number of samples. This, combined with the 16-bit sample size, 48 * gives the actual physical length of the buffer. 49 * 50 * A workaround for the AD1980 and AD1985 codec: 51 * Most vendors connect the surr-out of the codecs to the line-out jack. 52 * So far we haven't found which vendors don't do that. So we assume that 53 * all vendors swap the surr-out and the line-out outputs. So we need swap 54 * the two outputs. But we still internally process the 55 * "ad198x-swap-output" property. If someday some vendors do not swap the 56 * outputs, we would set "ad198x-swap-output = 0" in the 57 * /kernel/drv/audio810.conf file, and unload and reload the audio810 58 * driver (or reboot). 59 * 60 * NOTE: 61 * This driver depends on the drv/audio and misc/ac97 62 * modules being loaded first. 63 */ 64 #include <sys/types.h> 65 #include <sys/modctl.h> 66 #include <sys/kmem.h> 67 #include <sys/conf.h> 68 #include <sys/ddi.h> 69 #include <sys/sunddi.h> 70 #include <sys/pci.h> 71 #include <sys/note.h> 72 #include <sys/audio/audio_driver.h> 73 #include <sys/audio/ac97.h> 74 #include "audio810.h" 75 76 /* 77 * Module linkage routines for the kernel 78 */ 79 static int audio810_ddi_attach(dev_info_t *, ddi_attach_cmd_t); 80 static int audio810_ddi_detach(dev_info_t *, ddi_detach_cmd_t); 81 static int audio810_ddi_quiesce(dev_info_t *); 82 83 /* 84 * Entry point routine prototypes 85 */ 86 static int audio810_open(void *, int, unsigned *, unsigned *, caddr_t *); 87 static void audio810_close(void *); 88 static int audio810_start(void *); 89 static void audio810_stop(void *); 90 static int audio810_format(void *); 91 static int audio810_channels(void *); 92 static int audio810_rate(void *); 93 static uint64_t audio810_count(void *); 94 static void audio810_sync(void *, unsigned); 95 static unsigned audio810_playahead(void *); 96 97 static audio_engine_ops_t audio810_engine_ops = { 98 AUDIO_ENGINE_VERSION, 99 audio810_open, 100 audio810_close, 101 audio810_start, 102 audio810_stop, 103 audio810_count, 104 audio810_format, 105 audio810_channels, 106 audio810_rate, 107 audio810_sync, 108 NULL, 109 NULL, 110 audio810_playahead 111 }; 112 113 /* 114 * interrupt handler 115 */ 116 static uint_t audio810_intr(caddr_t); 117 118 /* 119 * Local Routine Prototypes 120 */ 121 static int audio810_attach(dev_info_t *); 122 static int audio810_resume(dev_info_t *); 123 static int audio810_detach(dev_info_t *); 124 static int audio810_suspend(dev_info_t *); 125 126 static int audio810_alloc_port(audio810_state_t *, int, uint8_t); 127 static void audio810_start_port(audio810_port_t *); 128 static void audio810_stop_port(audio810_port_t *); 129 static void audio810_reset_port(audio810_port_t *); 130 static void audio810_update_port(audio810_port_t *); 131 static int audio810_codec_sync(audio810_state_t *); 132 static void audio810_write_ac97(void *, uint8_t, uint16_t); 133 static uint16_t audio810_read_ac97(void *, uint8_t); 134 static int audio810_map_regs(dev_info_t *, audio810_state_t *); 135 static void audio810_unmap_regs(audio810_state_t *); 136 static void audio810_stop_dma(audio810_state_t *); 137 static int audio810_chip_init(audio810_state_t *); 138 static void audio810_destroy(audio810_state_t *); 139 140 /* 141 * Global variables, but used only by this file. 142 */ 143 144 /* driver name, so we don't have to call ddi_driver_name() or hard code strs */ 145 static char *audio810_name = I810_NAME; 146 147 148 /* 149 * DDI Structures 150 */ 151 152 /* Device operations structure */ 153 static struct dev_ops audio810_dev_ops = { 154 DEVO_REV, /* devo_rev */ 155 0, /* devo_refcnt */ 156 NULL, /* devo_getinfo */ 157 nulldev, /* devo_identify - obsolete */ 158 nulldev, /* devo_probe */ 159 audio810_ddi_attach, /* devo_attach */ 160 audio810_ddi_detach, /* devo_detach */ 161 nodev, /* devo_reset */ 162 NULL, /* devi_cb_ops */ 163 NULL, /* devo_bus_ops */ 164 NULL, /* devo_power */ 165 audio810_ddi_quiesce, /* devo_quiesce */ 166 }; 167 168 /* Linkage structure for loadable drivers */ 169 static struct modldrv audio810_modldrv = { 170 &mod_driverops, /* drv_modops */ 171 I810_MOD_NAME, /* drv_linkinfo */ 172 &audio810_dev_ops, /* drv_dev_ops */ 173 }; 174 175 /* Module linkage structure */ 176 static struct modlinkage audio810_modlinkage = { 177 MODREV_1, /* ml_rev */ 178 (void *)&audio810_modldrv, /* ml_linkage */ 179 NULL /* NULL terminates the list */ 180 }; 181 182 /* 183 * device access attributes for register mapping 184 */ 185 static struct ddi_device_acc_attr dev_attr = { 186 DDI_DEVICE_ATTR_V0, 187 DDI_STRUCTURE_LE_ACC, 188 DDI_STRICTORDER_ACC 189 }; 190 191 static struct ddi_device_acc_attr buf_attr = { 192 DDI_DEVICE_ATTR_V0, 193 DDI_STRUCTURE_LE_ACC, 194 DDI_STRICTORDER_ACC 195 }; 196 197 /* 198 * DMA attributes of buffer descriptor list 199 */ 200 static ddi_dma_attr_t bdlist_dma_attr = { 201 DMA_ATTR_V0, /* version */ 202 0, /* addr_lo */ 203 0xffffffff, /* addr_hi */ 204 0x0000ffff, /* count_max */ 205 8, /* align, BDL must be aligned on a 8-byte boundary */ 206 0x3c, /* burstsize */ 207 8, /* minxfer, set to the size of a BDlist entry */ 208 0x0000ffff, /* maxxfer */ 209 0x00000fff, /* seg, set to the RAM pagesize of intel platform */ 210 1, /* sgllen, there's no scatter-gather list */ 211 8, /* granular, set to the value of minxfer */ 212 0 /* flags, use virtual address */ 213 }; 214 215 /* 216 * DMA attributes of buffers to be used to receive/send audio data 217 */ 218 static ddi_dma_attr_t sample_buf_dma_attr = { 219 DMA_ATTR_V0, 220 0, /* addr_lo */ 221 0xffffffff, /* addr_hi */ 222 0x0001ffff, /* count_max */ 223 4, /* align, data buffer is aligned on a 4-byte boundary */ 224 0x3c, /* burstsize */ 225 4, /* minxfer, set to the size of a sample data */ 226 0x0001ffff, /* maxxfer */ 227 0x0001ffff, /* seg */ 228 1, /* sgllen, no scatter-gather */ 229 4, /* granular, set to the value of minxfer */ 230 0, /* flags, use virtual address */ 231 }; 232 233 /* 234 * _init() 235 * 236 * Description: 237 * Driver initialization, called when driver is first loaded. 238 * This is how access is initially given to all the static structures. 239 * 240 * Arguments: 241 * None 242 * 243 * Returns: 244 * mod_install() status, see mod_install(9f) 245 */ 246 int 247 _init(void) 248 { 249 int error; 250 251 audio_init_ops(&audio810_dev_ops, I810_NAME); 252 253 if ((error = mod_install(&audio810_modlinkage)) != 0) { 254 audio_fini_ops(&audio810_dev_ops); 255 } 256 257 return (error); 258 } 259 260 /* 261 * _fini() 262 * 263 * Description: 264 * Module de-initialization, called when the driver is to be unloaded. 265 * 266 * Arguments: 267 * None 268 * 269 * Returns: 270 * mod_remove() status, see mod_remove(9f) 271 */ 272 int 273 _fini(void) 274 { 275 int error; 276 277 if ((error = mod_remove(&audio810_modlinkage)) != 0) { 278 return (error); 279 } 280 281 /* clean up ops */ 282 audio_fini_ops(&audio810_dev_ops); 283 284 return (0); 285 } 286 287 /* 288 * _info() 289 * 290 * Description: 291 * Module information, returns information about the driver. 292 * 293 * Arguments: 294 * modinfo *modinfop Pointer to the opaque modinfo structure 295 * 296 * Returns: 297 * mod_info() status, see mod_info(9f) 298 */ 299 int 300 _info(struct modinfo *modinfop) 301 { 302 return (mod_info(&audio810_modlinkage, modinfop)); 303 } 304 305 306 /* ******************* Driver Entry Points ********************************* */ 307 308 /* 309 * audio810_ddi_attach() 310 * 311 * Description: 312 * Implements the DDI attach(9e) entry point. 313 * 314 * Arguments: 315 * dev_info_t *dip Pointer to the device's dev_info struct 316 * ddi_attach_cmd_t cmd Attach command 317 * 318 * Returns: 319 * DDI_SUCCESS The driver was initialized properly 320 * DDI_FAILURE The driver couldn't be initialized properly 321 */ 322 static int 323 audio810_ddi_attach(dev_info_t *dip, ddi_attach_cmd_t cmd) 324 { 325 switch (cmd) { 326 case DDI_ATTACH: 327 return (audio810_attach(dip)); 328 329 case DDI_RESUME: 330 return (audio810_resume(dip)); 331 } 332 return (DDI_FAILURE); 333 } 334 335 /* 336 * audio810_ddi_detach() 337 * 338 * Description: 339 * Implements the detach(9e) entry point. 340 * 341 * Arguments: 342 * dev_info_t *dip Pointer to the device's dev_info struct 343 * ddi_detach_cmd_t cmd Detach command 344 * 345 * Returns: 346 * DDI_SUCCESS The driver was detached 347 * DDI_FAILURE The driver couldn't be detached 348 */ 349 static int 350 audio810_ddi_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 351 { 352 switch (cmd) { 353 case DDI_DETACH: 354 return (audio810_detach(dip)); 355 356 case DDI_SUSPEND: 357 return (audio810_suspend(dip)); 358 } 359 return (DDI_FAILURE); 360 } 361 362 /* 363 * audio810_ddi_quiesce() 364 * 365 * Description: 366 * Implements the quiesce(9e) entry point. 367 * 368 * Arguments: 369 * dev_info_t *dip Pointer to the device's dev_info struct 370 * 371 * Returns: 372 * DDI_SUCCESS The driver was quiesced 373 * DDI_FAILURE The driver couldn't be quiesced 374 */ 375 static int 376 audio810_ddi_quiesce(dev_info_t *dip) 377 { 378 audio810_state_t *statep; 379 380 if ((statep = ddi_get_driver_private(dip)) == NULL) 381 return (DDI_FAILURE); 382 383 audio810_stop_dma(statep); 384 return (DDI_SUCCESS); 385 } 386 387 /* 388 * audio810_intr() 389 * 390 * Description: 391 * Interrupt service routine for both play and record. For play we 392 * get the next buffers worth of audio. For record we send it on to 393 * the mixer. 394 * 395 * Each of buffer descriptor has a field IOC(interrupt on completion) 396 * When both this and the IOC bit of correspondent dma control register 397 * is set, it means that the controller should issue an interrupt upon 398 * completion of this buffer. 399 * (AMD 8111 hypertransport I/O hub data sheet. 3.8.3 page 71) 400 * 401 * Arguments: 402 * caddr_t arg Pointer to the interrupting device's state 403 * structure 404 * 405 * Returns: 406 * DDI_INTR_CLAIMED Interrupt claimed and processed 407 * DDI_INTR_UNCLAIMED Interrupt not claimed, and thus ignored 408 */ 409 static uint_t 410 audio810_intr(caddr_t arg) 411 { 412 audio810_state_t *statep; 413 uint16_t gsr; 414 415 statep = (void *)arg; 416 mutex_enter(&statep->inst_lock); 417 418 if (statep->suspended) { 419 mutex_exit(&statep->inst_lock); 420 return (DDI_INTR_UNCLAIMED); 421 } 422 423 gsr = I810_BM_GET32(I810_REG_GSR); 424 425 /* check if device is interrupting */ 426 if ((gsr & I810_GSR_USE_INTR) == 0) { 427 mutex_exit(&statep->inst_lock); 428 return (DDI_INTR_UNCLAIMED); 429 } 430 431 for (int pnum = 0; pnum < I810_NUM_PORTS; pnum++) { 432 audio810_port_t *port; 433 uint8_t regoff, index; 434 435 port = statep->ports[pnum]; 436 if (port == NULL) { 437 continue; 438 } 439 regoff = port->regoff; 440 441 if (!(I810_BM_GET8(port->stsoff) & I810_BM_SR_BCIS)) 442 continue; 443 444 /* update the LVI -- we just set it to the current value - 1 */ 445 index = I810_BM_GET8(regoff + I810_OFFSET_CIV); 446 index = (index - 1) % I810_BD_NUMS; 447 448 I810_BM_PUT8(regoff + I810_OFFSET_LVI, index); 449 450 /* clear any interrupt */ 451 I810_BM_PUT8(port->stsoff, 452 I810_BM_SR_LVBCI | I810_BM_SR_BCIS | I810_BM_SR_FIFOE); 453 } 454 455 /* update the kernel interrupt statistics */ 456 if (statep->ksp) { 457 I810_KIOP(statep)->intrs[KSTAT_INTR_HARD]++; 458 } 459 460 mutex_exit(&statep->inst_lock); 461 462 /* notify the framework */ 463 if (gsr & I810_GSR_INTR_PIN) { 464 audio_engine_produce(statep->ports[I810_PCM_IN]->engine); 465 } 466 if (gsr & I810_GSR_INTR_POUT) { 467 audio_engine_consume(statep->ports[I810_PCM_OUT]->engine); 468 } 469 470 return (DDI_INTR_CLAIMED); 471 } 472 473 /* 474 * audio810_open() 475 * 476 * Description: 477 * Opens a DMA engine for use. 478 * 479 * Arguments: 480 * void *arg The DMA engine to set up 481 * int flag Open flags 482 * unsigned *fragfrp Receives number of frames per fragment 483 * unsigned *nfragsp Receives number of fragments 484 * caddr_t *bufp Receives kernel data buffer 485 * 486 * Returns: 487 * 0 on success 488 * errno on failure 489 */ 490 static int 491 audio810_open(void *arg, int flag, unsigned *fragfrp, unsigned *nfragsp, 492 caddr_t *bufp) 493 { 494 audio810_port_t *port = arg; 495 496 _NOTE(ARGUNUSED(flag)); 497 498 port->started = B_FALSE; 499 port->count = 0; 500 *fragfrp = port->fragfr; 501 *nfragsp = port->nfrag; 502 *bufp = port->samp_kaddr; 503 504 mutex_enter(&port->statep->inst_lock); 505 audio810_reset_port(port); 506 mutex_exit(&port->statep->inst_lock); 507 508 return (0); 509 } 510 511 /* 512 * audio810_close() 513 * 514 * Description: 515 * Closes an audio DMA engine that was previously opened. Since 516 * nobody is using it, we take this opportunity to possibly power 517 * down the entire device. 518 * 519 * Arguments: 520 * void *arg The DMA engine to shut down 521 */ 522 static void 523 audio810_close(void *arg) 524 { 525 audio810_port_t *port = arg; 526 audio810_state_t *statep = port->statep; 527 528 mutex_enter(&statep->inst_lock); 529 audio810_stop_port(port); 530 port->started = B_FALSE; 531 mutex_exit(&statep->inst_lock); 532 } 533 534 /* 535 * audio810_stop() 536 * 537 * Description: 538 * This is called by the framework to stop a port that is 539 * transferring data. 540 * 541 * Arguments: 542 * void *arg The DMA engine to stop 543 */ 544 static void 545 audio810_stop(void *arg) 546 { 547 audio810_port_t *port = arg; 548 audio810_state_t *statep = port->statep; 549 550 mutex_enter(&statep->inst_lock); 551 if (port->started) { 552 audio810_stop_port(port); 553 } 554 port->started = B_FALSE; 555 mutex_exit(&statep->inst_lock); 556 } 557 558 /* 559 * audio810_start() 560 * 561 * Description: 562 * This is called by the framework to start a port transferring data. 563 * 564 * Arguments: 565 * void *arg The DMA engine to start 566 * 567 * Returns: 568 * 0 on success (never fails, errno if it did) 569 */ 570 static int 571 audio810_start(void *arg) 572 { 573 audio810_port_t *port = arg; 574 audio810_state_t *statep = port->statep; 575 576 mutex_enter(&statep->inst_lock); 577 if (!port->started) { 578 audio810_start_port(port); 579 port->started = B_TRUE; 580 } 581 mutex_exit(&statep->inst_lock); 582 return (0); 583 } 584 585 /* 586 * audio810_format() 587 * 588 * Description: 589 * This is called by the framework to query the format of the device. 590 * 591 * Arguments: 592 * void *arg The DMA engine to query 593 * 594 * Returns: 595 * Format of the device (fixed at AUDIO_FORMAT_S16_LE) 596 */ 597 static int 598 audio810_format(void *arg) 599 { 600 _NOTE(ARGUNUSED(arg)); 601 602 return (AUDIO_FORMAT_S16_LE); 603 } 604 605 /* 606 * audio810_channels() 607 * 608 * Description: 609 * This is called by the framework to query the num channels of 610 * the device. 611 * 612 * Arguments: 613 * void *arg The DMA engine to query 614 * 615 * Returns: 616 * 0 number of channels for device 617 */ 618 static int 619 audio810_channels(void *arg) 620 { 621 audio810_port_t *port = arg; 622 623 return (port->nchan); 624 } 625 626 /* 627 * audio810_rate() 628 * 629 * Description: 630 * This is called by the framework to query the rate of the device. 631 * 632 * Arguments: 633 * void *arg The DMA engine to query 634 * 635 * Returns: 636 * Rate of device (fixed at 48000 Hz) 637 */ 638 static int 639 audio810_rate(void *arg) 640 { 641 _NOTE(ARGUNUSED(arg)); 642 643 return (48000); 644 } 645 646 /* 647 * audio810_count() 648 * 649 * Description: 650 * This is called by the framework to get the engine's frame counter 651 * 652 * Arguments: 653 * void *arg The DMA engine to query 654 * 655 * Returns: 656 * frame count for current engine 657 */ 658 static uint64_t 659 audio810_count(void *arg) 660 { 661 audio810_port_t *port = arg; 662 audio810_state_t *statep = port->statep; 663 uint64_t val; 664 uint16_t picb; 665 uint64_t count; 666 uint8_t nchan; 667 668 mutex_enter(&statep->inst_lock); 669 audio810_update_port(port); 670 count = port->count; 671 picb = port->picb; 672 nchan = port->nchan; 673 mutex_exit(&statep->inst_lock); 674 675 if (statep->quirk == QUIRK_SIS7012) { 676 val = count + picb / (2 * nchan); 677 } else { 678 val = count + (picb / nchan); 679 } 680 681 return (val); 682 } 683 684 /* 685 * audio810_sync() 686 * 687 * Description: 688 * This is called by the framework to synchronize DMA caches. 689 * 690 * Arguments: 691 * void *arg The DMA engine to sync 692 */ 693 static void 694 audio810_sync(void *arg, unsigned nframes) 695 { 696 audio810_port_t *port = arg; 697 _NOTE(ARGUNUSED(nframes)); 698 699 (void) ddi_dma_sync(port->samp_dmah, 0, 0, port->sync_dir); 700 } 701 702 /* 703 * audio810_playahead() 704 * 705 * Description: 706 * This is called by the framework to determine how much data it 707 * should queue up. We desire a deeper playahead than most to 708 * allow for virtualized devices which have less "regular" 709 * interrupt scheduling. 710 * 711 * Arguments: 712 * void *arg The DMA engine to query 713 * 714 * Returns: 715 * Play ahead in frames (4 fragments). 716 */ 717 static unsigned 718 audio810_playahead(void *arg) 719 { 720 audio810_port_t *port = arg; 721 722 return (4 * port->fragfr); 723 } 724 725 726 727 /* *********************** Local Routines *************************** */ 728 729 /* 730 * audio810_start_port() 731 * 732 * Description: 733 * This routine starts the DMA engine. 734 * 735 * Arguments: 736 * audio810_port_t *port Port of DMA engine to start. 737 */ 738 static void 739 audio810_start_port(audio810_port_t *port) 740 { 741 audio810_state_t *statep = port->statep; 742 uint8_t cr; 743 744 ASSERT(mutex_owned(&statep->inst_lock)); 745 746 /* if suspended, then do nothing else */ 747 if (statep->suspended) { 748 return; 749 } 750 751 cr = I810_BM_GET8(port->regoff + I810_OFFSET_CR); 752 cr |= I810_BM_CR_IOCE; 753 I810_BM_PUT8(port->regoff + I810_OFFSET_CR, cr); 754 cr |= I810_BM_CR_RUN; 755 I810_BM_PUT8(port->regoff + I810_OFFSET_CR, cr); 756 } 757 758 /* 759 * audio810_stop_port() 760 * 761 * Description: 762 * This routine stops the DMA engine. 763 * 764 * Arguments: 765 * audio810_port_t *port Port of DMA engine to stop. 766 */ 767 static void 768 audio810_stop_port(audio810_port_t *port) 769 { 770 audio810_state_t *statep = port->statep; 771 uint8_t cr; 772 773 ASSERT(mutex_owned(&statep->inst_lock)); 774 775 /* if suspended, then do nothing else */ 776 if (statep->suspended) { 777 return; 778 } 779 780 cr = I810_BM_GET8(port->regoff + I810_OFFSET_CR); 781 cr &= ~I810_BM_CR_RUN; 782 I810_BM_PUT8(port->regoff + I810_OFFSET_CR, cr); 783 } 784 785 /* 786 * audio810_reset_port() 787 * 788 * Description: 789 * This routine resets the DMA engine pareparing it for work. 790 * 791 * Arguments: 792 * audio810_port_t *port Port of DMA engine to reset. 793 */ 794 static void 795 audio810_reset_port(audio810_port_t *port) 796 { 797 audio810_state_t *statep = port->statep; 798 uint32_t gcr; 799 800 ASSERT(mutex_owned(&statep->inst_lock)); 801 802 port->civ = 0; 803 port->picb = 0; 804 805 if (statep->suspended) 806 return; 807 808 /* 809 * Make sure we put once in stereo, to ensure we always start from 810 * front left. 811 */ 812 if (port->num == I810_PCM_OUT) { 813 814 if (statep->quirk == QUIRK_SIS7012) { 815 /* 816 * SiS 7012 needs its own special multichannel config. 817 */ 818 gcr = I810_BM_GET32(I810_REG_GCR); 819 gcr &= ~I810_GCR_SIS_CHANNELS_MASK; 820 I810_BM_PUT32(I810_REG_GCR, gcr); 821 delay(drv_usectohz(50000)); /* 50 msec */ 822 823 switch (statep->maxch) { 824 case 2: 825 gcr |= I810_GCR_SIS_2_CHANNELS; 826 break; 827 case 4: 828 gcr |= I810_GCR_SIS_4_CHANNELS; 829 break; 830 case 6: 831 gcr |= I810_GCR_SIS_6_CHANNELS; 832 break; 833 } 834 I810_BM_PUT32(I810_REG_GCR, gcr); 835 delay(drv_usectohz(50000)); /* 50 msec */ 836 837 /* 838 * SiS 7012 has special unmute bit. 839 */ 840 I810_BM_PUT8(I810_REG_SISCTL, I810_SISCTL_UNMUTE); 841 842 } else { 843 844 /* 845 * All other devices work the same. 846 */ 847 gcr = I810_BM_GET32(I810_REG_GCR); 848 gcr &= ~I810_GCR_CHANNELS_MASK; 849 850 I810_BM_PUT32(I810_REG_GCR, gcr); 851 delay(drv_usectohz(50000)); /* 50 msec */ 852 853 switch (statep->maxch) { 854 case 2: 855 gcr |= I810_GCR_2_CHANNELS; 856 break; 857 case 4: 858 gcr |= I810_GCR_4_CHANNELS; 859 break; 860 case 6: 861 gcr |= I810_GCR_6_CHANNELS; 862 break; 863 } 864 I810_BM_PUT32(I810_REG_GCR, gcr); 865 delay(drv_usectohz(50000)); /* 50 msec */ 866 } 867 } 868 869 /* 870 * Perform full reset of the engine, but leave it turned off. 871 */ 872 I810_BM_PUT8(port->regoff + I810_OFFSET_CR, 0); 873 I810_BM_PUT8(port->regoff + I810_OFFSET_CR, I810_BM_CR_RST); 874 875 /* program the offset of the BD list */ 876 I810_BM_PUT32(port->regoff + I810_OFFSET_BD_BASE, port->bdl_paddr); 877 878 /* we set the last index to the full count -- all buffers are valid */ 879 I810_BM_PUT8(port->regoff + I810_OFFSET_LVI, I810_BD_NUMS - 1); 880 } 881 882 /* 883 * audio810_update_port() 884 * 885 * Description: 886 * This routine updates the ports frame counter from hardware, and 887 * gracefully handles wraps. 888 * 889 * Arguments: 890 * audio810_port_t *port The port to update. 891 */ 892 static void 893 audio810_update_port(audio810_port_t *port) 894 { 895 audio810_state_t *statep = port->statep; 896 uint8_t regoff = port->regoff; 897 uint8_t civ; 898 uint16_t picb; 899 unsigned n; 900 901 if (statep->suspended) { 902 civ = 0; 903 picb = 0; 904 } else { 905 /* 906 * We read the position counters, but we're careful to avoid 907 * the situation where the position counter resets at the end 908 * of a buffer. 909 */ 910 for (int i = 0; i < 2; i++) { 911 civ = I810_BM_GET8(regoff + I810_OFFSET_CIV); 912 picb = I810_BM_GET16(port->picboff); 913 if (I810_BM_GET8(regoff + I810_OFFSET_CIV) == civ) { 914 /* 915 * Chip did not start a new index, 916 * so the picb is valid. 917 */ 918 break; 919 } 920 } 921 922 if (civ >= port->civ) { 923 n = civ - port->civ; 924 } else { 925 n = civ + (I810_BD_NUMS - port->civ); 926 } 927 port->count += (n * port->fragfr); 928 } 929 port->civ = civ; 930 port->picb = picb; 931 } 932 933 /* 934 * audio810_attach() 935 * 936 * Description: 937 * Attach an instance of the audio810 driver. This routine does the 938 * device dependent attach tasks, and registers with the audio framework. 939 * 940 * Arguments: 941 * dev_info_t *dip Pointer to the device's dev_info struct 942 * ddi_attach_cmd_t cmd Attach command 943 * 944 * Returns: 945 * DDI_SUCCESS The driver was initialized properly 946 * DDI_FAILURE The driver couldn't be initialized properly 947 */ 948 static int 949 audio810_attach(dev_info_t *dip) 950 { 951 uint16_t cmdreg; 952 audio810_state_t *statep; 953 audio_dev_t *adev; 954 ddi_acc_handle_t pcih; 955 uint32_t devid; 956 uint32_t gsr; 957 const char *name; 958 const char *vers; 959 uint8_t nch; 960 int maxch; 961 962 /* we don't support high level interrupts in the driver */ 963 if (ddi_intr_hilevel(dip, 0) != 0) { 964 cmn_err(CE_WARN, "!%s: unsupported high level interrupt", 965 audio810_name); 966 return (DDI_FAILURE); 967 } 968 969 /* allocate the soft state structure */ 970 statep = kmem_zalloc(sizeof (*statep), KM_SLEEP); 971 ddi_set_driver_private(dip, statep); 972 973 /* get iblock cookie information */ 974 if (ddi_get_iblock_cookie(dip, 0, &statep->iblock) != DDI_SUCCESS) { 975 cmn_err(CE_WARN, "!%s: cannot get iblock cookie", 976 audio810_name); 977 kmem_free(statep, sizeof (*statep)); 978 return (DDI_FAILURE); 979 } 980 mutex_init(&statep->inst_lock, NULL, MUTEX_DRIVER, statep->iblock); 981 mutex_init(&statep->ac_lock, NULL, MUTEX_DRIVER, statep->iblock); 982 983 if ((adev = audio_dev_alloc(dip, 0)) == NULL) { 984 cmn_err(CE_WARN, "!%s: unable to allocate audio dev", 985 audio810_name); 986 goto error; 987 } 988 statep->adev = adev; 989 statep->dip = dip; 990 991 /* map in the registers, allocate DMA buffers, etc. */ 992 if (audio810_map_regs(dip, statep) != DDI_SUCCESS) { 993 audio_dev_warn(adev, "couldn't map registers"); 994 goto error; 995 } 996 997 /* set PCI command register */ 998 if (pci_config_setup(dip, &pcih) != DDI_SUCCESS) { 999 audio_dev_warn(adev, "pci conf mapping failed"); 1000 goto error; 1001 } 1002 cmdreg = pci_config_get16(pcih, PCI_CONF_COMM); 1003 pci_config_put16(pcih, PCI_CONF_COMM, 1004 cmdreg | PCI_COMM_IO | PCI_COMM_MAE | PCI_COMM_ME); 1005 devid = pci_config_get16(pcih, PCI_CONF_VENID); 1006 devid <<= 16; 1007 devid |= pci_config_get16(pcih, PCI_CONF_DEVID); 1008 pci_config_teardown(&pcih); 1009 1010 name = "Unknown AC'97"; 1011 vers = ""; 1012 1013 statep->quirk = QUIRK_NONE; 1014 switch (devid) { 1015 case 0x80862415: 1016 name = "Intel AC'97"; 1017 vers = "ICH"; 1018 break; 1019 case 0x80862425: 1020 name = "Intel AC'97"; 1021 vers = "ICH0"; 1022 break; 1023 case 0x80867195: 1024 name = "Intel AC'97"; 1025 vers = "440MX"; 1026 break; 1027 case 0x80862445: 1028 name = "Intel AC'97"; 1029 vers = "ICH2"; 1030 break; 1031 case 0x80862485: 1032 name = "Intel AC'97"; 1033 vers = "ICH3"; 1034 break; 1035 case 0x808624C5: 1036 name = "Intel AC'97"; 1037 vers = "ICH4"; 1038 break; 1039 case 0x808624D5: 1040 name = "Intel AC'97"; 1041 vers = "ICH5"; 1042 break; 1043 case 0x8086266E: 1044 name = "Intel AC'97"; 1045 vers = "ICH6"; 1046 break; 1047 case 0x808627DE: 1048 name = "Intel AC'97"; 1049 vers = "ICH7"; 1050 break; 1051 case 0x808625A6: 1052 name = "Intel AC'97"; 1053 vers = "6300ESB"; 1054 break; 1055 case 0x80862698: 1056 name = "Intel AC'97"; 1057 vers = "ESB2"; 1058 break; 1059 case 0x10397012: 1060 name = "SiS AC'97"; 1061 vers = "7012"; 1062 statep->quirk = QUIRK_SIS7012; 1063 break; 1064 case 0x10de01b1: /* nForce */ 1065 name = "NVIDIA AC'97"; 1066 vers = "MCP1"; 1067 break; 1068 case 0x10de006a: /* nForce 2 */ 1069 name = "NVIDIA AC'97"; 1070 vers = "MCP2"; 1071 break; 1072 case 0x10de00da: /* nForce 3 */ 1073 name = "NVIDIA AC'97"; 1074 vers = "MCP3"; 1075 break; 1076 case 0x10de00ea: 1077 name = "NVIDIA AC'97"; 1078 vers = "CK8S"; 1079 break; 1080 case 0x10de0059: 1081 name = "NVIDIA AC'97"; 1082 vers = "CK804"; 1083 break; 1084 case 0x10de008a: 1085 name = "NVIDIA AC'97"; 1086 vers = "CK8"; 1087 break; 1088 case 0x10de003a: /* nForce 4 */ 1089 name = "NVIDIA AC'97"; 1090 vers = "MCP4"; 1091 break; 1092 case 0x10de026b: 1093 name = "NVIDIA AC'97"; 1094 vers = "MCP51"; 1095 break; 1096 case 0x1022746d: 1097 name = "AMD AC'97"; 1098 vers = "8111"; 1099 break; 1100 case 0x10227445: 1101 name = "AMD AC'97"; 1102 vers = "AMD768"; 1103 break; 1104 } 1105 /* set device information */ 1106 audio_dev_set_description(adev, name); 1107 audio_dev_set_version(adev, vers); 1108 1109 /* initialize audio controller and AC97 codec */ 1110 if (audio810_chip_init(statep) != DDI_SUCCESS) { 1111 audio_dev_warn(adev, "failed to init chip"); 1112 goto error; 1113 } 1114 1115 /* allocate ac97 handle */ 1116 statep->ac97 = ac97_alloc(dip, audio810_read_ac97, audio810_write_ac97, 1117 statep); 1118 if (statep->ac97 == NULL) { 1119 audio_dev_warn(adev, "failed to allocate ac97 handle"); 1120 goto error; 1121 } 1122 1123 /* initialize the AC'97 part */ 1124 if (ac97_init(statep->ac97, adev) != DDI_SUCCESS) { 1125 audio_dev_warn(adev, "ac'97 initialization failed"); 1126 goto error; 1127 } 1128 1129 /* 1130 * Override "max-channels" property to prevent configuration 1131 * of 4 or 6 (or possibly even 8!) channel audio. The default 1132 * is to support as many channels as the hardware can do. 1133 * 1134 * (Hmmm... perhaps this should be driven in the common 1135 * framework. The framework could even offer simplistic upmix 1136 * and downmix for various standard configs.) 1137 */ 1138 maxch = ddi_prop_get_int(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, 1139 "max-channels", ac97_num_channels(statep->ac97)); 1140 if (maxch < 2) { 1141 maxch = 2; 1142 } 1143 1144 gsr = I810_BM_GET32(I810_REG_GSR); 1145 if (gsr & I810_GSR_CAP6CH) { 1146 nch = 6; 1147 } else if (gsr & I810_GSR_CAP4CH) { 1148 nch = 4; 1149 } else { 1150 nch = 2; 1151 } 1152 1153 statep->maxch = (uint8_t)min(nch, maxch); 1154 statep->maxch &= ~1; 1155 1156 /* allocate port structures */ 1157 if ((audio810_alloc_port(statep, I810_PCM_OUT, statep->maxch) != 1158 DDI_SUCCESS) || 1159 (audio810_alloc_port(statep, I810_PCM_IN, 2) != DDI_SUCCESS)) { 1160 goto error; 1161 } 1162 1163 /* set up kernel statistics */ 1164 if ((statep->ksp = kstat_create(I810_NAME, ddi_get_instance(dip), 1165 I810_NAME, "controller", KSTAT_TYPE_INTR, 1, 1166 KSTAT_FLAG_PERSISTENT)) != NULL) { 1167 kstat_install(statep->ksp); 1168 } 1169 1170 /* set up the interrupt handler */ 1171 if (ddi_add_intr(dip, 0, &statep->iblock, 1172 NULL, audio810_intr, (caddr_t)statep) != DDI_SUCCESS) { 1173 audio_dev_warn(adev, "bad interrupt specification"); 1174 goto error; 1175 } 1176 statep->intr_added = B_TRUE; 1177 1178 if (audio_dev_register(adev) != DDI_SUCCESS) { 1179 audio_dev_warn(adev, "unable to register with framework"); 1180 goto error; 1181 } 1182 1183 ddi_report_dev(dip); 1184 1185 return (DDI_SUCCESS); 1186 1187 error: 1188 audio810_destroy(statep); 1189 1190 return (DDI_FAILURE); 1191 } 1192 1193 1194 /* 1195 * audio810_resume() 1196 * 1197 * Description: 1198 * Resume operation of the device after sleeping or hibernating. 1199 * Note that this should never fail, even if hardware goes wonky, 1200 * because the current PM framework will panic if it does. 1201 * 1202 * Arguments: 1203 * dev_info_t *dip Pointer to the device's dev_info struct 1204 * 1205 * Returns: 1206 * DDI_SUCCESS The driver was resumed. 1207 */ 1208 static int 1209 audio810_resume(dev_info_t *dip) 1210 { 1211 audio810_state_t *statep; 1212 audio_dev_t *adev; 1213 1214 /* this should always be valid */ 1215 statep = ddi_get_driver_private(dip); 1216 adev = statep->adev; 1217 1218 ASSERT(statep != NULL); 1219 ASSERT(dip == statep->dip); 1220 1221 /* Restore the audio810 chip's state */ 1222 if (audio810_chip_init(statep) != DDI_SUCCESS) { 1223 /* 1224 * Note that PM gurus say we should return 1225 * success here. Failure of audio shouldn't 1226 * be considered FATAL to the system. The 1227 * upshot is that audio will not progress. 1228 */ 1229 audio_dev_warn(adev, "DDI_RESUME failed to init chip"); 1230 return (DDI_SUCCESS); 1231 } 1232 1233 /* allow ac97 operations again */ 1234 ac97_resume(statep->ac97); 1235 1236 mutex_enter(&statep->inst_lock); 1237 1238 ASSERT(statep->suspended); 1239 statep->suspended = B_FALSE; 1240 1241 for (int i = 0; i < I810_NUM_PORTS; i++) { 1242 1243 audio810_port_t *port = statep->ports[i]; 1244 1245 if (port != NULL) { 1246 /* reset framework DMA engine buffer */ 1247 if (port->engine != NULL) { 1248 audio_engine_reset(port->engine); 1249 } 1250 1251 /* reset and initialize hardware ports */ 1252 audio810_reset_port(port); 1253 if (port->started) { 1254 audio810_start_port(port); 1255 } else { 1256 audio810_stop_port(port); 1257 } 1258 } 1259 } 1260 mutex_exit(&statep->inst_lock); 1261 1262 return (DDI_SUCCESS); 1263 } 1264 1265 /* 1266 * audio810_detach() 1267 * 1268 * Description: 1269 * Detach an instance of the audio810 driver. 1270 * 1271 * Arguments: 1272 * dev_info_t *dip Pointer to the device's dev_info struct 1273 * 1274 * Returns: 1275 * DDI_SUCCESS The driver was detached 1276 * DDI_FAILURE The driver couldn't be detached 1277 */ 1278 static int 1279 audio810_detach(dev_info_t *dip) 1280 { 1281 audio810_state_t *statep; 1282 1283 statep = ddi_get_driver_private(dip); 1284 ASSERT(statep != NULL); 1285 1286 /* don't detach us if we are still in use */ 1287 if (audio_dev_unregister(statep->adev) != DDI_SUCCESS) { 1288 return (DDI_FAILURE); 1289 } 1290 1291 audio810_destroy(statep); 1292 return (DDI_SUCCESS); 1293 } 1294 1295 /* 1296 * audio810_suspend() 1297 * 1298 * Description: 1299 * Suspend an instance of the audio810 driver, in preparation for 1300 * sleep or hibernation. 1301 * 1302 * Arguments: 1303 * dev_info_t *dip Pointer to the device's dev_info struct 1304 * 1305 * Returns: 1306 * DDI_SUCCESS The driver was suspended 1307 */ 1308 static int 1309 audio810_suspend(dev_info_t *dip) 1310 { 1311 audio810_state_t *statep; 1312 1313 statep = ddi_get_driver_private(dip); 1314 ASSERT(statep != NULL); 1315 1316 ac97_suspend(statep->ac97); 1317 1318 mutex_enter(&statep->inst_lock); 1319 1320 ASSERT(statep->suspended == B_FALSE); 1321 1322 statep->suspended = B_TRUE; /* stop new ops */ 1323 1324 /* stop DMA engines */ 1325 audio810_stop_dma(statep); 1326 1327 mutex_exit(&statep->inst_lock); 1328 1329 return (DDI_SUCCESS); 1330 } 1331 1332 /* 1333 * audio810_alloc_port() 1334 * 1335 * Description: 1336 * This routine allocates the DMA handles and the memory for the 1337 * DMA engines to use. It also configures the BDL lists properly 1338 * for use. 1339 * 1340 * Arguments: 1341 * dev_info_t *dip Pointer to the device's devinfo 1342 * 1343 * Returns: 1344 * DDI_SUCCESS Registers successfully mapped 1345 * DDI_FAILURE Registers not successfully mapped 1346 */ 1347 static int 1348 audio810_alloc_port(audio810_state_t *statep, int num, uint8_t nchan) 1349 { 1350 ddi_dma_cookie_t cookie; 1351 uint_t count; 1352 int dir; 1353 unsigned caps; 1354 char *prop; 1355 char *nfprop; 1356 audio_dev_t *adev; 1357 audio810_port_t *port; 1358 uint32_t paddr; 1359 int rc; 1360 dev_info_t *dip; 1361 i810_bd_entry_t *bdentry; 1362 1363 adev = statep->adev; 1364 dip = statep->dip; 1365 1366 port = kmem_zalloc(sizeof (*port), KM_SLEEP); 1367 statep->ports[num] = port; 1368 port->statep = statep; 1369 port->started = B_FALSE; 1370 port->nchan = nchan; 1371 port->num = num; 1372 1373 switch (num) { 1374 case I810_PCM_IN: 1375 prop = "record-interrupts"; 1376 nfprop = "record-fragments"; 1377 dir = DDI_DMA_READ; 1378 caps = ENGINE_INPUT_CAP; 1379 port->sync_dir = DDI_DMA_SYNC_FORKERNEL; 1380 port->regoff = I810_BASE_PCM_IN; 1381 break; 1382 case I810_PCM_OUT: 1383 prop = "play-interrupts"; 1384 nfprop = "play-fragments"; 1385 dir = DDI_DMA_WRITE; 1386 caps = ENGINE_OUTPUT_CAP; 1387 port->sync_dir = DDI_DMA_SYNC_FORDEV; 1388 port->regoff = I810_BASE_PCM_OUT; 1389 break; 1390 default: 1391 audio_dev_warn(adev, "bad port number (%d)!", num); 1392 return (DDI_FAILURE); 1393 } 1394 1395 /* 1396 * SiS 7012 swaps status and picb registers. 1397 */ 1398 if (statep->quirk == QUIRK_SIS7012) { 1399 port->stsoff = port->regoff + I810_OFFSET_PICB; 1400 port->picboff = port->regoff + I810_OFFSET_SR; 1401 } else { 1402 port->stsoff = port->regoff + I810_OFFSET_SR; 1403 port->picboff = port->regoff + I810_OFFSET_PICB; 1404 } 1405 1406 port->intrs = ddi_prop_get_int(DDI_DEV_T_ANY, dip, 1407 DDI_PROP_DONTPASS, prop, I810_INTS); 1408 1409 /* make sure the values are good */ 1410 if (port->intrs < I810_MIN_INTS) { 1411 audio_dev_warn(adev, "%s too low, %d, resetting to %d", 1412 prop, port->intrs, I810_INTS); 1413 port->intrs = I810_INTS; 1414 } else if (port->intrs > I810_MAX_INTS) { 1415 audio_dev_warn(adev, "%s too high, %d, resetting to %d", 1416 prop, port->intrs, I810_INTS); 1417 port->intrs = I810_INTS; 1418 } 1419 1420 port->nfrag = ddi_prop_get_int(DDI_DEV_T_ANY, dip, 1421 DDI_PROP_DONTPASS, nfprop, I810_NFRAGS); 1422 1423 /* 1424 * Note that fragments must divide evenly into I810_BD_NUMS (32). 1425 * We also insist that the value be larger than our "playahead". 1426 */ 1427 if (port->nfrag <= 8) { 1428 port->nfrag = 8; 1429 } else if (port->nfrag <= 16) { 1430 port->nfrag = 16; 1431 } else { 1432 port->nfrag = I810_BD_NUMS; 1433 } 1434 1435 /* 1436 * Figure out how much space we need. Sample rate is 48kHz, and 1437 * we need to store 32 chunks. (Note that this means that low 1438 * interrupt frequencies will require more RAM. We could probably 1439 * do some cleverness to use a shorter BD list.) 1440 */ 1441 port->fragfr = 48000 / port->intrs; 1442 port->fragfr = I810_ROUNDUP(port->fragfr, I810_MOD_SIZE); 1443 port->fragsz = port->fragfr * port->nchan * 2; 1444 port->samp_size = port->fragsz * port->nfrag; 1445 1446 /* allocate dma handle */ 1447 rc = ddi_dma_alloc_handle(dip, &sample_buf_dma_attr, DDI_DMA_SLEEP, 1448 NULL, &port->samp_dmah); 1449 if (rc != DDI_SUCCESS) { 1450 audio_dev_warn(adev, "ddi_dma_alloc_handle failed: %d", rc); 1451 return (DDI_FAILURE); 1452 } 1453 /* allocate DMA buffer */ 1454 rc = ddi_dma_mem_alloc(port->samp_dmah, port->samp_size, &buf_attr, 1455 DDI_DMA_CONSISTENT, DDI_DMA_SLEEP, NULL, &port->samp_kaddr, 1456 &port->samp_size, &port->samp_acch); 1457 if (rc == DDI_FAILURE) { 1458 audio_dev_warn(adev, "dma_mem_alloc (%d) failed: %d", 1459 port->samp_size, rc); 1460 return (DDI_FAILURE); 1461 } 1462 1463 /* bind DMA buffer */ 1464 rc = ddi_dma_addr_bind_handle(port->samp_dmah, NULL, 1465 port->samp_kaddr, port->samp_size, dir|DDI_DMA_CONSISTENT, 1466 DDI_DMA_SLEEP, NULL, &cookie, &count); 1467 if ((rc != DDI_DMA_MAPPED) || (count != 1)) { 1468 audio_dev_warn(adev, 1469 "ddi_dma_addr_bind_handle failed: %d", rc); 1470 return (DDI_FAILURE); 1471 } 1472 port->samp_paddr = cookie.dmac_address; 1473 1474 /* 1475 * now, from here we allocate DMA memory for buffer descriptor list. 1476 * we allocate adjacent DMA memory for all DMA engines. 1477 */ 1478 rc = ddi_dma_alloc_handle(dip, &bdlist_dma_attr, DDI_DMA_SLEEP, 1479 NULL, &port->bdl_dmah); 1480 if (rc != DDI_SUCCESS) { 1481 audio_dev_warn(adev, "ddi_dma_alloc_handle(bdlist) failed"); 1482 return (DDI_FAILURE); 1483 } 1484 1485 /* 1486 * we allocate all buffer descriptors lists in continuous dma memory. 1487 */ 1488 port->bdl_size = sizeof (i810_bd_entry_t) * I810_BD_NUMS; 1489 rc = ddi_dma_mem_alloc(port->bdl_dmah, port->bdl_size, 1490 &dev_attr, DDI_DMA_CONSISTENT, DDI_DMA_SLEEP, NULL, 1491 &port->bdl_kaddr, &port->bdl_size, &port->bdl_acch); 1492 if (rc != DDI_SUCCESS) { 1493 audio_dev_warn(adev, "ddi_dma_mem_alloc(bdlist) failed"); 1494 return (DDI_FAILURE); 1495 } 1496 1497 rc = ddi_dma_addr_bind_handle(port->bdl_dmah, NULL, port->bdl_kaddr, 1498 port->bdl_size, DDI_DMA_WRITE|DDI_DMA_CONSISTENT, DDI_DMA_SLEEP, 1499 NULL, &cookie, &count); 1500 if ((rc != DDI_DMA_MAPPED) || (count != 1)) { 1501 audio_dev_warn(adev, "addr_bind_handle failed"); 1502 return (DDI_FAILURE); 1503 } 1504 port->bdl_paddr = cookie.dmac_address; 1505 1506 /* 1507 * Wire up the BD list. 1508 */ 1509 paddr = port->samp_paddr; 1510 bdentry = (void *)port->bdl_kaddr; 1511 for (int i = 0; i < I810_BD_NUMS; i++) { 1512 1513 /* set base address of buffer */ 1514 ddi_put32(port->bdl_acch, &bdentry->buf_base, paddr); 1515 /* 1516 * SiS 7012 counts samples in bytes, all other count 1517 * in words. 1518 */ 1519 ddi_put16(port->bdl_acch, &bdentry->buf_len, 1520 statep->quirk == QUIRK_SIS7012 ? port->fragsz : 1521 port->fragsz / 2); 1522 ddi_put16(port->bdl_acch, &bdentry->buf_cmd, 1523 BUF_CMD_IOC | BUF_CMD_BUP); 1524 paddr += port->fragsz; 1525 if ((i % port->nfrag) == (port->nfrag - 1)) { 1526 /* handle wrap */ 1527 paddr = port->samp_paddr; 1528 } 1529 bdentry++; 1530 } 1531 (void) ddi_dma_sync(port->bdl_dmah, 0, 0, DDI_DMA_SYNC_FORDEV); 1532 1533 port->engine = audio_engine_alloc(&audio810_engine_ops, caps); 1534 if (port->engine == NULL) { 1535 audio_dev_warn(adev, "audio_engine_alloc failed"); 1536 return (DDI_FAILURE); 1537 } 1538 1539 audio_engine_set_private(port->engine, port); 1540 audio_dev_add_engine(adev, port->engine); 1541 1542 return (DDI_SUCCESS); 1543 } 1544 1545 /* 1546 * audio810_free_port() 1547 * 1548 * Description: 1549 * This routine unbinds the DMA cookies, frees the DMA buffers, 1550 * deallocates the DMA handles. 1551 * 1552 * Arguments: 1553 * audio810_port_t *port The port structure for a DMA engine. 1554 */ 1555 static void 1556 audio810_free_port(audio810_port_t *port) 1557 { 1558 if (port == NULL) 1559 return; 1560 1561 if (port->engine) { 1562 audio_dev_remove_engine(port->statep->adev, port->engine); 1563 audio_engine_free(port->engine); 1564 } 1565 if (port->bdl_paddr) { 1566 (void) ddi_dma_unbind_handle(port->bdl_dmah); 1567 } 1568 if (port->bdl_acch) { 1569 ddi_dma_mem_free(&port->bdl_acch); 1570 } 1571 if (port->bdl_dmah) { 1572 ddi_dma_free_handle(&port->bdl_dmah); 1573 } 1574 if (port->samp_paddr) { 1575 (void) ddi_dma_unbind_handle(port->samp_dmah); 1576 } 1577 if (port->samp_acch) { 1578 ddi_dma_mem_free(&port->samp_acch); 1579 } 1580 if (port->samp_dmah) { 1581 ddi_dma_free_handle(&port->samp_dmah); 1582 } 1583 kmem_free(port, sizeof (*port)); 1584 } 1585 1586 /* 1587 * audio810_map_regs() 1588 * 1589 * Description: 1590 * The registers are mapped in. 1591 * 1592 * Arguments: 1593 * dev_info_t *dip Pointer to the device's devinfo 1594 * 1595 * Returns: 1596 * DDI_SUCCESS Registers successfully mapped 1597 * DDI_FAILURE Registers not successfully mapped 1598 */ 1599 static int 1600 audio810_map_regs(dev_info_t *dip, audio810_state_t *statep) 1601 { 1602 uint_t nregs = 0; 1603 int *regs_list; 1604 int i; 1605 int pciBar1 = 0; 1606 int pciBar2 = 0; 1607 int pciBar3 = 0; 1608 int pciBar4 = 0; 1609 1610 /* check the "reg" property to get the length of memory-mapped I/O */ 1611 if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, 1612 "reg", (int **)®s_list, &nregs) != DDI_PROP_SUCCESS) { 1613 audio_dev_warn(statep->adev, "inquire regs property failed"); 1614 goto error; 1615 } 1616 /* 1617 * Some hardwares, such as Intel ICH0/ICH and AMD 8111, use PCI 0x10 1618 * and 0x14 BAR separately for native audio mixer BAR and native bus 1619 * mastering BAR. More advanced hardwares, such as Intel ICH4 and ICH5, 1620 * support PCI memory BAR, via PCI 0x18 and 0x1C BAR, that allows for 1621 * higher performance access to the controller register. All features 1622 * can be accessed via this BAR making the I/O BAR (PCI 0x10 and 0x14 1623 * BAR) capabilities obsolete. However, these controller maintain the 1624 * I/O BAR capability to allow for the reuse of legacy code maintaining 1625 * backward compatibility. The I/O BAR is disabled unless system BIOS 1626 * enables the simultaneous backward compatible capability on the 0x41 1627 * register. 1628 * 1629 * When I/O BAR is enabled, the value of "reg" property should be like 1630 * this, 1631 * phys_hi phys_mid phys_lo size_hi size_lo 1632 * -------------------------------------------------------- 1633 * 0000fd00 00000000 00000000 00000000 00000000 1634 * 0100fd10 00000000 00000000 00000000 00000100 1635 * 0100fd14 00000000 00000000 00000000 00000040 1636 * 0200fd18 00000000 00000000 00000000 00000200 1637 * 0200fd1c 00000000 00000000 00000000 00000100 1638 * 1639 * When I/O BAR is disabled, the "reg" property of the device node does 1640 * not consist of the description for the I/O BAR. The following example 1641 * illustrates the vaule of "reg" property, 1642 * 1643 * phys_hi phys_mid phys_lo size_hi size_lo 1644 * -------------------------------------------------------- 1645 * 0000fd00 00000000 00000000 00000000 00000000 1646 * 0200fd18 00000000 00000000 00000000 00000200 1647 * 0200fd1c 00000000 00000000 00000000 00000100 1648 * 1649 * If the hardware has memory-mapped I/O access, first try to use 1650 * this facility, otherwise we will try I/O access. 1651 */ 1652 for (i = 1; i < nregs/I810_INTS_PER_REG_PROP; i++) { 1653 switch (regs_list[I810_INTS_PER_REG_PROP * i] & 0x000000ff) { 1654 case 0x10: 1655 pciBar1 = i; 1656 break; 1657 case 0x14: 1658 pciBar2 = i; 1659 break; 1660 case 0x18: 1661 pciBar3 = i; 1662 break; 1663 case 0x1c: 1664 pciBar4 = i; 1665 break; 1666 default: /* we don't care others */ 1667 break; 1668 } 1669 } 1670 1671 if ((pciBar3 != 0) && (pciBar4 != 0)) { 1672 /* map audio mixer registers */ 1673 if ((ddi_regs_map_setup(dip, pciBar3, &statep->am_regs_base, 0, 1674 0, &dev_attr, &statep->am_regs_handle)) != DDI_SUCCESS) { 1675 audio_dev_warn(statep->adev, 1676 "memory am mapping failed"); 1677 goto error; 1678 } 1679 1680 /* map bus master register */ 1681 if ((ddi_regs_map_setup(dip, pciBar4, &statep->bm_regs_base, 0, 1682 0, &dev_attr, &statep->bm_regs_handle)) != DDI_SUCCESS) { 1683 audio_dev_warn(statep->adev, 1684 "memory bm mapping failed"); 1685 goto error; 1686 } 1687 1688 } else if ((pciBar1 != 0) && (pciBar2 != 0)) { 1689 /* map audio mixer registers */ 1690 if ((ddi_regs_map_setup(dip, pciBar1, &statep->am_regs_base, 0, 1691 0, &dev_attr, &statep->am_regs_handle)) != DDI_SUCCESS) { 1692 audio_dev_warn(statep->adev, "I/O am mapping failed"); 1693 goto error; 1694 } 1695 1696 /* map bus master register */ 1697 if ((ddi_regs_map_setup(dip, pciBar2, &statep->bm_regs_base, 0, 1698 0, &dev_attr, &statep->bm_regs_handle)) != DDI_SUCCESS) { 1699 audio_dev_warn(statep->adev, "I/O bm mapping failed"); 1700 goto error; 1701 } 1702 } else { 1703 audio_dev_warn(statep->adev, "map_regs() pci BAR error"); 1704 goto error; 1705 } 1706 1707 ddi_prop_free(regs_list); 1708 1709 return (DDI_SUCCESS); 1710 1711 error: 1712 if (nregs > 0) { 1713 ddi_prop_free(regs_list); 1714 } 1715 audio810_unmap_regs(statep); 1716 1717 return (DDI_FAILURE); 1718 } 1719 1720 /* 1721 * audio810_unmap_regs() 1722 * 1723 * Description: 1724 * This routine unmaps control registers. 1725 * 1726 * Arguments: 1727 * audio810_state_t *state The device's state structure 1728 */ 1729 static void 1730 audio810_unmap_regs(audio810_state_t *statep) 1731 { 1732 if (statep->bm_regs_handle) { 1733 ddi_regs_map_free(&statep->bm_regs_handle); 1734 } 1735 1736 if (statep->am_regs_handle) { 1737 ddi_regs_map_free(&statep->am_regs_handle); 1738 } 1739 } 1740 1741 /* 1742 * audio810_chip_init() 1743 * 1744 * Description: 1745 * This routine initializes the AMD 8111 audio controller. 1746 * codec. 1747 * 1748 * Arguments: 1749 * audio810_state_t *state The device's state structure 1750 * 1751 * Returns: 1752 * DDI_SUCCESS The hardware was initialized properly 1753 * DDI_FAILURE The hardware couldn't be initialized properly 1754 */ 1755 static int 1756 audio810_chip_init(audio810_state_t *statep) 1757 { 1758 uint32_t gcr; 1759 uint32_t gsr; 1760 uint32_t codec_ready; 1761 int loop; 1762 clock_t ticks; 1763 1764 gcr = I810_BM_GET32(I810_REG_GCR); 1765 ticks = drv_usectohz(100); 1766 1767 /* 1768 * Clear the channels bits for now. We'll set them later in 1769 * reset port. 1770 */ 1771 if (statep->quirk == QUIRK_SIS7012) { 1772 gcr &= ~(I810_GCR_ACLINK_OFF | I810_GCR_SIS_CHANNELS_MASK); 1773 } else { 1774 gcr &= ~(I810_GCR_ACLINK_OFF | I810_GCR_CHANNELS_MASK); 1775 } 1776 1777 /* 1778 * Datasheet(ICH5, document number of Intel: 252751-001): 1779 * 3.6.5.5(page 37) 1780 * if reset bit(bit1) is "0", driver must set it 1781 * to "1" to de-assert the AC_RESET# signal in AC 1782 * link, thus completing a cold reset. But if the 1783 * bit is "1", then a warm reset is required. 1784 */ 1785 gcr |= (gcr & I810_GCR_COLD_RST) == 0 ? 1786 I810_GCR_COLD_RST:I810_GCR_WARM_RST; 1787 I810_BM_PUT32(I810_REG_GCR, gcr); 1788 1789 /* according AC'97 spec, wait for codec reset */ 1790 for (loop = 6000; --loop >= 0; ) { 1791 delay(ticks); 1792 gcr = I810_BM_GET32(I810_REG_GCR); 1793 if ((gcr & I810_GCR_WARM_RST) == 0) { 1794 break; 1795 } 1796 } 1797 1798 /* codec reset failed */ 1799 if (loop < 0) { 1800 audio_dev_warn(statep->adev, "Failed to reset codec"); 1801 return (DDI_FAILURE); 1802 } 1803 1804 /* 1805 * Wait for codec ready. The hardware can provide the state of 1806 * codec ready bit on SDATA_IN[0], SDATA_IN[1] or SDATA_IN[2] 1807 */ 1808 codec_ready = 1809 I810_GSR_PRI_READY | I810_GSR_SEC_READY | I810_GSR_TRI_READY; 1810 for (loop = 7000; --loop >= 0; ) { 1811 delay(ticks); 1812 gsr = I810_BM_GET32(I810_REG_GSR); 1813 if ((gsr & codec_ready) != 0) { 1814 break; 1815 } 1816 } 1817 if (loop < 0) { 1818 audio_dev_warn(statep->adev, "No codec ready signal received"); 1819 return (DDI_FAILURE); 1820 } 1821 1822 /* 1823 * put the audio controller into quiet state, everything off 1824 */ 1825 audio810_stop_dma(statep); 1826 1827 return (DDI_SUCCESS); 1828 } 1829 1830 /* 1831 * audio810_stop_dma() 1832 * 1833 * Description: 1834 * This routine is used to put each DMA engine into the quiet state. 1835 * 1836 * Arguments: 1837 * audio810_state_t *state The device's state structure 1838 */ 1839 static void 1840 audio810_stop_dma(audio810_state_t *statep) 1841 { 1842 if (statep->bm_regs_handle == NULL) { 1843 return; 1844 } 1845 /* pause bus master (needed for the following reset register) */ 1846 I810_BM_PUT8(I810_BASE_PCM_IN + I810_OFFSET_CR, 0x0); 1847 I810_BM_PUT8(I810_BASE_PCM_OUT + I810_OFFSET_CR, 0x0); 1848 I810_BM_PUT8(I810_BASE_MIC + I810_OFFSET_CR, 0x0); 1849 1850 /* and then reset the bus master registers for a three DMA engines */ 1851 I810_BM_PUT8(I810_BASE_PCM_IN + I810_OFFSET_CR, I810_BM_CR_RST); 1852 I810_BM_PUT8(I810_BASE_PCM_OUT + I810_OFFSET_CR, I810_BM_CR_RST); 1853 I810_BM_PUT8(I810_BASE_MIC + I810_OFFSET_CR, I810_BM_CR_RST); 1854 } 1855 1856 1857 /* 1858 * audio810_codec_sync() 1859 * 1860 * Description: 1861 * Serialize access to the AC97 audio mixer registers. 1862 * 1863 * Arguments: 1864 * audio810_state_t *state The device's state structure 1865 * 1866 * Returns: 1867 * DDI_SUCCESS Ready for an I/O access to the codec 1868 * DDI_FAILURE An I/O access is currently in progress, can't 1869 * perform another I/O access. 1870 */ 1871 static int 1872 audio810_codec_sync(audio810_state_t *statep) 1873 { 1874 int i; 1875 uint16_t casr; 1876 1877 for (i = 0; i < 300; i++) { 1878 casr = I810_BM_GET8(I810_REG_CASR); 1879 if ((casr & 1) == 0) { 1880 return (DDI_SUCCESS); 1881 } 1882 drv_usecwait(10); 1883 } 1884 1885 return (DDI_FAILURE); 1886 } 1887 1888 /* 1889 * audio810_write_ac97() 1890 * 1891 * Description: 1892 * Set the specific AC97 Codec register. 1893 * 1894 * Arguments: 1895 * void *arg The device's state structure 1896 * uint8_t reg AC97 register number 1897 * uint16_t data The data want to be set 1898 */ 1899 static void 1900 audio810_write_ac97(void *arg, uint8_t reg, uint16_t data) 1901 { 1902 audio810_state_t *statep = arg; 1903 1904 mutex_enter(&statep->ac_lock); 1905 if (audio810_codec_sync(statep) == DDI_SUCCESS) { 1906 I810_AM_PUT16(reg, data); 1907 } 1908 mutex_exit(&statep->ac_lock); 1909 1910 (void) audio810_read_ac97(statep, reg); 1911 } 1912 1913 /* 1914 * audio810_read_ac97() 1915 * 1916 * Description: 1917 * Get the specific AC97 Codec register. 1918 * 1919 * Arguments: 1920 * void *arg The device's state structure 1921 * uint8_t reg AC97 register number 1922 * 1923 * Returns: 1924 * The register value. 1925 */ 1926 static uint16_t 1927 audio810_read_ac97(void *arg, uint8_t reg) 1928 { 1929 audio810_state_t *statep = arg; 1930 uint16_t val = 0xffff; 1931 1932 mutex_enter(&statep->ac_lock); 1933 if (audio810_codec_sync(statep) == DDI_SUCCESS) { 1934 val = I810_AM_GET16(reg); 1935 } 1936 mutex_exit(&statep->ac_lock); 1937 return (val); 1938 } 1939 1940 /* 1941 * audio810_destroy() 1942 * 1943 * Description: 1944 * This routine releases all resources held by the device instance, 1945 * as part of either detach or a failure in attach. 1946 * 1947 * Arguments: 1948 * audio810_state_t *state The device soft state. 1949 */ 1950 void 1951 audio810_destroy(audio810_state_t *statep) 1952 { 1953 /* stop DMA engines */ 1954 audio810_stop_dma(statep); 1955 1956 if (statep->intr_added) { 1957 ddi_remove_intr(statep->dip, 0, NULL); 1958 } 1959 1960 if (statep->ksp) { 1961 kstat_delete(statep->ksp); 1962 } 1963 1964 for (int i = 0; i < I810_NUM_PORTS; i++) { 1965 audio810_free_port(statep->ports[i]); 1966 } 1967 1968 audio810_unmap_regs(statep); 1969 1970 if (statep->ac97) 1971 ac97_free(statep->ac97); 1972 1973 if (statep->adev) 1974 audio_dev_free(statep->adev); 1975 1976 mutex_destroy(&statep->inst_lock); 1977 mutex_destroy(&statep->ac_lock); 1978 kmem_free(statep, sizeof (*statep)); 1979 } 1980