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 * audiots Audio Driver 29 * 30 * This Audio Driver controls the T2 audio core in the ALI M1553 31 * southbridge chip. This chip supports multiple play streams, but just 32 * a single record stream. It also supports wave table synthesis and 33 * hardware MIDI and joystick ports. Unfortunately the MIDI ports are 34 * not available because their pins have been re-assigned to expose 35 * interrupts. We also aren't going to do anything with the joystick 36 * ports. The audio core controls an AC-97 V2.1 Codec. 37 * 38 * The DMA engine uses a single buffer which is large enough to hold 39 * two interrupts worth of data. When it gets to the mid point an 40 * interrupt is generated and data is either sent (for record) or 41 * requested and put in that half of the buffer (for play). When the 42 * second half is played we do the same, but the audio core loops the 43 * pointer back to the beginning. 44 * 45 * The audio core has a bug in silicon that doesn't let it read the AC-97 46 * Codec's register. T2 has provided an algorithm that attempts to read the 47 * the Codec several times. This is probably heuristic and thus isn't 48 * absolutely guaranteed to work. However we do have to place a limit on 49 * the looping, otherwise when we read a valid 0x00 we would never exit 50 * the loop. Unfortunately there is also a problem with writing the AC-97 51 * Codec's registers as well. Thus we read it back to verify the write. 52 * 53 * The AC'97 common code provides shadow state for AC'97 registers for us, 54 * so we only need to read those registers during early startup (primarily 55 * to determine codec id and capabilities.) 56 * 57 * We don't save any of the audio controller registers during normal 58 * operation. When we need to save register state we only have to save 59 * the aram and eram. The rest of the controller state is never modified 60 * from the initial programming. Thus restoring the controller state 61 * can be done from audiots_chip_init() as well. 62 * 63 * 64 * WARNING: The SME birdsnest platform uses a PCI bridge chip between the 65 * CPU and the southbridge containing the audio core. There is 66 * a bug in silicon that causes a bogus parity error. With the mixer 67 * reimplementation project, Bug 4374774, the audio driver is always 68 * set to the best precision and number of channels. Thus when turning 69 * the mixer on and off the only thing that changes is the sample rate. 70 * This change in programming doesn't trigger the silicon error. 71 * Thus the supported channels must always be 2 and the precision 72 * must always be 16-bits. This will keep any future change in the 73 * mixer from exposing this bug. 74 * 75 * Due to a hardware bug, system power management is not supported by this 76 * driver. 77 * 78 * CAUTION: If audio controller state is changed outside of aram 79 * and eram then that information must be saved and restored 80 * during power management shutdown and bringup. 81 * 82 * NOTE: The AC-97 Codec's reset pin is set to PCI reset, so we 83 * can't power down the Codec all the way. 84 * 85 * NOTE: This driver depends on the drv/audio and misc/ac97 86 * modules being loaded first. 87 * 88 * NOTE: Don't OR the ap_stop register to stop a play or record. This 89 * will just stop all active channels because a read of ap_stop 90 * returns ap_start. Just set the ap_stop register with the 91 * channels you want to stop. The same goes for ap_start. 92 * 93 * NOTE: There is a hardware problem with P2 rev motherboards. After 94 * prolonged use, reading the AC97 register will always return 95 * busy. The AC97 register is now useless. Consequently, we are no 96 * longer able to program the Codec. This work around disables 97 * audio when this state is detected. It's not great, but its 98 * better than having audio blasting out at 100% all the time. 99 * 100 * NOTE: Power Management testing has also exposed this AC97 timeout 101 * problem. Management has decided this is too risky for customers 102 * and hence they want power management support removed from the 103 * audio subsystem. All PM support is now removed. 104 */ 105 106 #include <sys/modctl.h> 107 #include <sys/kmem.h> 108 #include <sys/pci.h> 109 #include <sys/ddi.h> 110 #include <sys/sunddi.h> 111 #include <sys/debug.h> 112 #include <sys/note.h> 113 #include <sys/audio/audio_driver.h> 114 #include <sys/audio/ac97.h> 115 #include "audiots.h" 116 117 /* 118 * Module linkage routines for the kernel 119 */ 120 static int audiots_attach(dev_info_t *, ddi_attach_cmd_t); 121 static int audiots_detach(dev_info_t *, ddi_detach_cmd_t); 122 static int audiots_quiesce(dev_info_t *); 123 124 /* 125 * Entry point routine prototypes 126 */ 127 static int audiots_open(void *, int, unsigned *, unsigned *, caddr_t *); 128 static void audiots_close(void *); 129 static int audiots_start(void *); 130 static void audiots_stop(void *); 131 static int audiots_format(void *); 132 static int audiots_channels(void *); 133 static int audiots_rate(void *); 134 static void audiots_chinfo(void *, int, unsigned *, unsigned *); 135 static uint64_t audiots_count(void *); 136 static void audiots_sync(void *, unsigned); 137 static size_t audiots_qlen(void *); 138 139 static audio_engine_ops_t audiots_engine_ops = { 140 AUDIO_ENGINE_VERSION, 141 audiots_open, 142 audiots_close, 143 audiots_start, 144 audiots_stop, 145 audiots_count, 146 audiots_format, 147 audiots_channels, 148 audiots_rate, 149 audiots_sync, 150 audiots_qlen, 151 audiots_chinfo 152 }; 153 154 /* 155 * Local Routine Prototypes 156 */ 157 static void audiots_power_up(audiots_state_t *); 158 static void audiots_chip_init(audiots_state_t *); 159 static uint16_t audiots_get_ac97(void *, uint8_t); 160 static void audiots_set_ac97(void *, uint8_t, uint16_t); 161 static int audiots_init_state(audiots_state_t *, dev_info_t *); 162 static uint_t audiots_intr(caddr_t); 163 static int audiots_map_regs(dev_info_t *, audiots_state_t *); 164 static void audiots_update_port(audiots_port_t *); 165 static void audiots_start_port(audiots_port_t *); 166 static void audiots_stop_port(audiots_port_t *); 167 static uint16_t audiots_read_ac97(audiots_state_t *, int); 168 static void audiots_stop_everything(audiots_state_t *); 169 static void audiots_destroy(audiots_state_t *); 170 static int audiots_alloc_port(audiots_state_t *, int); 171 static void audiots_reset_port(audiots_port_t *); 172 173 /* 174 * Global variables, but viewable only by this file. 175 */ 176 177 /* anchor for soft state structures */ 178 static void *audiots_statep; 179 180 /* driver name, so we don't have to call ddi_driver_name() or hard code strs */ 181 static char *audiots_name = TS_NAME; 182 183 /* 184 * DDI Structures 185 */ 186 187 /* Device operations structure */ 188 static struct dev_ops audiots_dev_ops = { 189 DEVO_REV, /* devo_rev */ 190 0, /* devo_refcnt */ 191 NULL, /* devo_getinfo */ 192 nulldev, /* devo_identify - obsolete */ 193 nulldev, /* devo_probe */ 194 audiots_attach, /* devo_attach */ 195 audiots_detach, /* devo_detach */ 196 nodev, /* devo_reset */ 197 NULL, /* devo_cb_ops */ 198 NULL, /* devo_bus_ops */ 199 NULL, /* devo_power */ 200 audiots_quiesce, /* devo_quiesce */ 201 }; 202 203 /* Linkage structure for loadable drivers */ 204 static struct modldrv audiots_modldrv = { 205 &mod_driverops, /* drv_modops */ 206 TS_MOD_NAME, /* drv_linkinfo */ 207 &audiots_dev_ops /* drv_dev_ops */ 208 }; 209 210 /* Module linkage structure */ 211 static struct modlinkage audiots_modlinkage = { 212 MODREV_1, /* ml_rev */ 213 (void *)&audiots_modldrv, /* ml_linkage */ 214 NULL /* NULL terminates the list */ 215 }; 216 217 218 /* 219 * NOTE: Grover OBP v4.0.166 and rev G of the ALI Southbridge chip force the 220 * audiots driver to use the upper 2 GB DMA address range. However to maintain 221 * backwards compatibility with older systems/OBP, we're going to try the full 222 * 4 GB DMA range. 223 * 224 * Eventually, this will be set back to using the proper high 2 GB DMA range. 225 */ 226 227 /* Device attribute structure - full 4 gig address range */ 228 static ddi_dma_attr_t audiots_attr = { 229 DMA_ATTR_VERSION, /* version */ 230 0x0000000000000000LL, /* dlim_addr_lo */ 231 0x00000000ffffffffLL, /* dlim_addr_hi */ 232 0x0000000000003fffLL, /* DMA counter register - 16 bits */ 233 0x0000000000000008LL, /* DMA address alignment, 64-bit */ 234 0x0000007f, /* 1 through 64 byte burst sizes */ 235 0x00000001, /* min effective DMA size */ 236 0x0000000000003fffLL, /* maximum transfer size, 16k */ 237 0x000000000000ffffLL, /* segment boundary, 64k */ 238 0x00000001, /* s/g list length, no s/g */ 239 0x00000001, /* granularity of device, don't care */ 240 0 /* DMA flags */ 241 }; 242 243 static ddi_device_acc_attr_t ts_acc_attr = { 244 DDI_DEVICE_ATTR_V0, 245 DDI_NEVERSWAP_ACC, 246 DDI_STRICTORDER_ACC 247 }; 248 249 static ddi_device_acc_attr_t ts_regs_attr = { 250 DDI_DEVICE_ATTR_V0, 251 DDI_STRUCTURE_LE_ACC, 252 DDI_STRICTORDER_ACC 253 }; 254 255 /* 256 * _init() 257 * 258 * Description: 259 * Driver initialization, called when driver is first loaded. 260 * This is how access is initially given to all the static structures. 261 * 262 * Arguments: 263 * None 264 * 265 * Returns: 266 * ddi_soft_state_init() status, see ddi_soft_state_init(9f), or 267 * mod_install() status, see mod_install(9f) 268 */ 269 int 270 _init(void) 271 { 272 int error; 273 274 audio_init_ops(&audiots_dev_ops, TS_NAME); 275 276 /* initialize the soft state */ 277 if ((error = ddi_soft_state_init(&audiots_statep, 278 sizeof (audiots_state_t), 1)) != 0) { 279 audio_fini_ops(&audiots_dev_ops); 280 return (error); 281 } 282 283 if ((error = mod_install(&audiots_modlinkage)) != 0) { 284 audio_fini_ops(&audiots_dev_ops); 285 ddi_soft_state_fini(&audiots_statep); 286 } 287 288 return (error); 289 } 290 291 /* 292 * _fini() 293 * 294 * Description: 295 * Module de-initialization, called when the driver is to be unloaded. 296 * 297 * Arguments: 298 * None 299 * 300 * Returns: 301 * mod_remove() status, see mod_remove(9f) 302 */ 303 int 304 _fini(void) 305 { 306 int error; 307 308 if ((error = mod_remove(&audiots_modlinkage)) != 0) { 309 return (error); 310 } 311 312 /* free the soft state internal structures */ 313 ddi_soft_state_fini(&audiots_statep); 314 315 /* clean up ops */ 316 audio_fini_ops(&audiots_dev_ops); 317 318 return (0); 319 } 320 321 /* 322 * _info() 323 * 324 * Description: 325 * Module information, returns infomation about the driver. 326 * 327 * Arguments: 328 * modinfo *modinfop Pointer to the opaque modinfo structure 329 * 330 * Returns: 331 * mod_info() status, see mod_info(9f) 332 */ 333 int 334 _info(struct modinfo *modinfop) 335 { 336 int error; 337 338 error = mod_info(&audiots_modlinkage, modinfop); 339 340 return (error); 341 } 342 343 344 /* 345 * audiots_attach() 346 * 347 * Description: 348 * Attach an instance of the audiots driver. This routine does the 349 * device dependent attach tasks. 350 * 351 * Arguments: 352 * dev_info_t *dip Pointer to the device's dev_info struct 353 * ddi_attach_cmd_t cmd Attach command 354 * 355 * Returns: 356 * DDI_SUCCESS The driver was initialized properly 357 * DDI_FAILURE The driver couldn't be initialized properly 358 */ 359 static int 360 audiots_attach(dev_info_t *dip, ddi_attach_cmd_t cmd) 361 { 362 audiots_state_t *state; 363 int instance; 364 365 instance = ddi_get_instance(dip); 366 367 switch (cmd) { 368 case DDI_ATTACH: 369 break; 370 case DDI_RESUME: 371 372 /* we've already allocated the state structure so get ptr */ 373 if ((state = ddi_get_soft_state(audiots_statep, instance)) == 374 NULL) { 375 /* this will probably panic */ 376 cmn_err(CE_WARN, 377 "!%s%d: RESUME get soft state failed", 378 audiots_name, instance); 379 return (DDI_FAILURE); 380 } 381 382 ASSERT(dip == state->ts_dip); 383 384 /* suspend/resume resets the chip, so we have no more faults */ 385 if (state->ts_flags & TS_AUDIO_READ_FAILED) { 386 ddi_dev_report_fault(state->ts_dip, 387 DDI_SERVICE_RESTORED, 388 DDI_DEVICE_FAULT, 389 "check port, gain, balance, and mute settings"); 390 /* and clear the fault state flags */ 391 state->ts_flags &= 392 ~(TS_AUDIO_READ_FAILED|TS_READ_FAILURE_PRINTED); 393 } 394 395 audiots_power_up(state); 396 audiots_chip_init(state); 397 ac97_resume(state->ts_ac97); 398 399 mutex_enter(&state->ts_lock); 400 /* 401 * Initialize/reset ports. Done under the lock, to 402 * avoid race with interrupt service routine. 403 */ 404 state->ts_suspended = B_FALSE; 405 for (int i = 0; i < TS_NUM_PORTS; i++) { 406 audiots_port_t *port = state->ts_ports[i]; 407 if (port != NULL) { 408 /* relocate any streams properly */ 409 if (port->tp_engine) 410 audio_engine_reset(port->tp_engine); 411 412 /* do a hardware reset on the port */ 413 audiots_reset_port(port); 414 if (port->tp_started) { 415 audiots_start_port(port); 416 } else { 417 audiots_stop_port(port); 418 } 419 } 420 } 421 mutex_exit(&state->ts_lock); 422 423 return (DDI_SUCCESS); 424 425 default: 426 cmn_err(CE_WARN, "!%s%d: attach() unknown command: 0x%x", 427 audiots_name, instance, cmd); 428 return (DDI_FAILURE); 429 } 430 431 /* before we do anything make sure that we haven't had a h/w failure */ 432 if (ddi_get_devstate(dip) == DDI_DEVSTATE_DOWN) { 433 cmn_err(CE_WARN, "%s%d: The audio hardware has " 434 "been disabled.", audiots_name, instance); 435 cmn_err(CE_CONT, "Please reboot to restore audio."); 436 return (DDI_FAILURE); 437 } 438 439 /* we don't support high level interrupts in this driver */ 440 if (ddi_intr_hilevel(dip, 0) != 0) { 441 cmn_err(CE_WARN, "!%s%d: unsupported high level interrupt", 442 audiots_name, instance); 443 return (DDI_FAILURE); 444 } 445 446 /* allocate the state structure */ 447 if (ddi_soft_state_zalloc(audiots_statep, instance) == DDI_FAILURE) { 448 cmn_err(CE_WARN, "!%s%d: soft state allocate failed", 449 audiots_name, instance); 450 return (DDI_FAILURE); 451 } 452 453 /* 454 * WARNING: From here on all errors require that we free memory, 455 * including the state structure. 456 */ 457 458 /* get the state structure - cannot fail */ 459 state = ddi_get_soft_state(audiots_statep, instance); 460 ASSERT(state != NULL); 461 462 if ((state->ts_adev = audio_dev_alloc(dip, 0)) == NULL) { 463 cmn_err(CE_WARN, "unable to allocate audio dev"); 464 goto error; 465 } 466 467 /* map in the registers, allocate DMA buffers, etc. */ 468 if (audiots_map_regs(dip, state) == DDI_FAILURE) { 469 audio_dev_warn(state->ts_adev, "unable to map registers"); 470 goto error; 471 } 472 473 /* initialize the audio state structures */ 474 if (audiots_init_state(state, dip) == DDI_FAILURE) { 475 audio_dev_warn(state->ts_adev, "init state structure failed"); 476 goto error; 477 } 478 479 /* power up */ 480 audiots_power_up(state); 481 482 /* initialize the audio controller */ 483 audiots_chip_init(state); 484 485 /* initialize the AC-97 Codec */ 486 if (ac97_init(state->ts_ac97, state->ts_adev) != 0) { 487 goto error; 488 } 489 490 /* put the engine interrupts into a known state -- all off */ 491 ddi_put32(state->ts_acch, &state->ts_regs->aud_regs.ap_ainten, 492 TS_ALL_DMA_OFF); 493 494 /* call the framework attach routine */ 495 if (audio_dev_register(state->ts_adev) != DDI_SUCCESS) { 496 audio_dev_warn(state->ts_adev, "unable to register audio"); 497 goto error; 498 } 499 500 /* set up kernel statistics */ 501 state->ts_ksp = kstat_create(TS_NAME, instance, TS_NAME, 502 "controller", KSTAT_TYPE_INTR, 1, KSTAT_FLAG_PERSISTENT); 503 if (state->ts_ksp != NULL) { 504 kstat_install(state->ts_ksp); 505 } 506 507 /* set up the interrupt handler */ 508 if (ddi_add_intr(dip, 0, NULL, NULL, audiots_intr, 509 (caddr_t)state) != DDI_SUCCESS) { 510 audio_dev_warn(state->ts_adev, 511 "failed to register interrupt handler"); 512 goto error; 513 } 514 state->ts_flags |= TS_INTR_INSTALLED; 515 516 /* everything worked out, so report the device */ 517 ddi_report_dev(dip); 518 519 return (DDI_SUCCESS); 520 521 error: 522 audiots_destroy(state); 523 return (DDI_FAILURE); 524 } 525 526 /* 527 * audiots_detach() 528 * 529 * Description: 530 * Detach an instance of the audiots driver. 531 * 532 * Arguments: 533 * dev_info_t *dip Pointer to the device's dev_info struct 534 * ddi_detach_cmd_t cmd Detach command 535 * 536 * Returns: 537 * DDI_SUCCESS The driver was detached 538 * DDI_FAILURE The driver couldn't be detached 539 */ 540 static int 541 audiots_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 542 { 543 audiots_state_t *state; 544 int instance; 545 546 instance = ddi_get_instance(dip); 547 548 /* get the state structure */ 549 if ((state = ddi_get_soft_state(audiots_statep, instance)) == NULL) { 550 cmn_err(CE_WARN, "!%s%d: detach get soft state failed", 551 audiots_name, instance); 552 return (DDI_FAILURE); 553 } 554 555 switch (cmd) { 556 case DDI_DETACH: 557 break; 558 case DDI_SUSPEND: 559 560 ac97_suspend(state->ts_ac97); 561 562 mutex_enter(&state->ts_lock); 563 564 state->ts_suspended = B_TRUE; /* stop new ops */ 565 566 /* we may already be powered down, so only save state if up */ 567 568 /* stop playing and recording */ 569 (void) audiots_stop_everything(state); 570 571 mutex_exit(&state->ts_lock); 572 573 return (DDI_SUCCESS); 574 575 default: 576 return (DDI_FAILURE); 577 } 578 579 /* attempt to unregister from the framework first */ 580 if (audio_dev_unregister(state->ts_adev) != DDI_SUCCESS) { 581 return (DDI_FAILURE); 582 } 583 584 audiots_destroy(state); 585 586 return (DDI_SUCCESS); 587 588 } 589 590 /* 591 * audiots_quiesce() 592 * 593 * Description: 594 * Quiesce an instance of the audiots driver. Stops all DMA and 595 * interrupts. 596 * 597 * Arguments: 598 * dev_info_t *dip Pointer to the device's dev_info struct 599 * 600 * Returns: 601 * DDI_SUCCESS The driver was quiesced 602 * DDI_SUCCESS The driver was NOT quiesced 603 */ 604 static int 605 audiots_quiesce(dev_info_t *dip) 606 { 607 audiots_state_t *state; 608 int instance; 609 610 instance = ddi_get_instance(dip); 611 612 /* get the state structure */ 613 if ((state = ddi_get_soft_state(audiots_statep, instance)) == NULL) { 614 return (DDI_FAILURE); 615 } 616 617 audiots_stop_everything(state); 618 619 return (DDI_SUCCESS); 620 } 621 622 /* 623 * audiots_power_up() 624 * 625 * Description 626 * Ensure that the device is running in PCI power state D0. 627 */ 628 static void 629 audiots_power_up(audiots_state_t *state) 630 { 631 ddi_acc_handle_t pcih = state->ts_pcih; 632 uint8_t ptr; 633 uint16_t pmcsr; 634 635 if ((pci_config_get16(pcih, PCI_CONF_STAT) & PCI_STAT_CAP) == 0) { 636 /* does not implement PCI capabilities -- no PM */ 637 return; 638 } 639 640 ptr = pci_config_get8(pcih, PCI_CONF_CAP_PTR); 641 for (;;) { 642 if (ptr == PCI_CAP_NEXT_PTR_NULL) { 643 /* PM capability not found */ 644 return; 645 } 646 if (pci_config_get8(pcih, ptr + PCI_CAP_ID) == PCI_CAP_ID_PM) { 647 /* found it */ 648 break; 649 } 650 ptr = pci_config_get8(pcih, ptr + PCI_CAP_NEXT_PTR); 651 } 652 653 /* if we got here, then got valid PMCSR pointer */ 654 ptr += PCI_PMCSR; 655 656 /* check to see if we are already in state D0 */ 657 pmcsr = pci_config_get16(pcih, ptr); 658 if ((pmcsr & PCI_PMCSR_STATE_MASK) != PCI_PMCSR_D0) { 659 660 /* D3hot (or any other state) -> D0 */ 661 pmcsr &= ~PCI_PMCSR_STATE_MASK; 662 pmcsr |= PCI_PMCSR_D0; 663 pci_config_put16(pcih, ptr, pmcsr); 664 } 665 666 /* 667 * Wait for it to power up - PCI spec says 10 ms is enough. 668 * We double it. Note that no locks are held when this routine 669 * is called, so we can sleep (we are in attach context only). 670 * 671 * We do this delay even if already powerd up, just to make 672 * sure we aren't seeing something that *just* transitioned 673 * into D0 state. 674 */ 675 delay(drv_usectohz(TS_20MS)); 676 677 /* clear PME# flag */ 678 pmcsr = pci_config_get16(pcih, ptr); 679 pci_config_put16(pcih, ptr, pmcsr | PCI_PMCSR_PME_STAT); 680 } 681 682 /* 683 * audiots_chip_init() 684 * 685 * Description: 686 * Initialize the audio core. 687 * 688 * Arguments: 689 * audiots_state_t *state The device's state structure 690 * 691 * Returns: 692 * void 693 */ 694 static void 695 audiots_chip_init(audiots_state_t *state) 696 { 697 ddi_acc_handle_t handle = state->ts_acch; 698 audiots_regs_t *regs = state->ts_regs; 699 int str; 700 701 /* start with all interrupts & dma channels disabled */ 702 ddi_put32(handle, ®s->aud_regs.ap_stop, TS_ALL_DMA_ENGINES); 703 ddi_put32(handle, ®s->aud_regs.ap_ainten, TS_ALL_DMA_OFF); 704 705 /* set global music and wave volume to 0dB */ 706 ddi_put32(handle, ®s->aud_regs.ap_volume, 0x0); 707 708 /* enable end interrupts for all channels. */ 709 ddi_put32(handle, ®s->aud_regs.ap_cir_gc, AP_CIR_GC_ENDLP_IE); 710 711 /* for each stream, set gain and vol settings */ 712 for (str = 0; str < TS_MAX_HW_CHANNELS; str++) { 713 /* 714 * Set volume to all off, 1st left and then right. 715 * These are never changed, so we don't have to save them. 716 */ 717 ddi_put16(handle, 718 ®s->aud_ram[str].eram.eram_gvsel_pan_vol, 719 (ERAM_WAVE_VOL|ERAM_PAN_LEFT|ERAM_PAN_0dB| 720 ERAM_VOL_MAX_ATTEN)); 721 ddi_put16(handle, 722 ®s->aud_ram[str].eram.eram_gvsel_pan_vol, 723 (ERAM_WAVE_VOL|ERAM_PAN_RIGHT|ERAM_PAN_0dB| 724 ERAM_VOL_MAX_ATTEN)); 725 726 /* 727 * The envelope engine *MUST* remain in still mode (off). 728 * Otherwise bad things like gain randomly disappearing might 729 * happen. See bug #4332773. 730 */ 731 732 ddi_put32(handle, ®s->aud_ram[str].eram.eram_ebuf1, 733 ERAM_EBUF_STILL); 734 ddi_put32(handle, ®s->aud_ram[str].eram.eram_ebuf2, 735 ERAM_EBUF_STILL); 736 737 /* program the initial eram and aram rate */ 738 ddi_put16(handle, ®s->aud_ram[str].aram.aram_delta, 739 1 << TS_SRC_SHIFT); 740 ddi_put16(handle, ®s->aud_ram[str].eram.eram_ctrl_ec, 741 ERAM_16_BITS | ERAM_STEREO | ERAM_LOOP_MODE | 742 ERAM_SIGNED_PCM); 743 } 744 745 /* program channel 31 for record */ 746 OR_SET_WORD(handle, &state->ts_regs->aud_regs.ap_global_control, 747 (AP_CLOGAL_CTRL_E_PCMIN_CH31|AP_CLOGAL_CTRL_PCM_OUT_AC97| 748 AP_CLOGAL_CTRL_MMC_FROM_MIXER|AP_CLOGAL_CTRL_PCM_OUT_TO_AC97)); 749 750 /* do a warm reset, which powers up the Codec */ 751 OR_SET_WORD(handle, &state->ts_regs->aud_regs.ap_sctrl, 752 AP_SCTRL_WRST_CODEC); 753 drv_usecwait(2); 754 AND_SET_WORD(handle, &state->ts_regs->aud_regs.ap_sctrl, 755 ~AP_SCTRL_WRST_CODEC); 756 757 /* do a warm reset via the Codec, yes, I'm being paranoid! */ 758 audiots_set_ac97(state, AC97_RESET_REGISTER, 0); 759 760 /* Make sure the Codec is powered up. */ 761 int i = TS_WAIT_CNT; 762 while ((audiots_get_ac97(state, AC97_POWERDOWN_CTRL_STAT_REGISTER) & 763 PCSR_POWERD_UP) != PCSR_POWERD_UP && i--) { 764 drv_usecwait(1); 765 } 766 767 } 768 769 /* 770 * audiots_get_ac97() 771 * 772 * Description: 773 * Get the value in the specified AC-97 Codec register. There is a 774 * bug in silicon which forces us to do multiple reads of the Codec's 775 * register. This algorithm was provided by T2 and is heuristic in 776 * nature. Unfortunately we have no guarantees that the real answer 777 * isn't 0x0000, which is what we get when a read fails. So we loop 778 * TS_LOOP_CNT times before we give up. We just have to hope this is 779 * sufficient to give us the correct value. 780 * 781 * Arguments: 782 * audiots_state_t *state The device's state structure 783 * int reg AC-97 register number 784 * 785 * Returns: 786 * unsigned short The value in the specified register 787 */ 788 static uint16_t 789 audiots_get_ac97(void *arg, uint8_t reg) 790 { 791 audiots_state_t *state = arg; 792 ddi_acc_handle_t handle = state->ts_acch; 793 uint16_t *data; 794 int count; 795 int delay; 796 uint16_t first; 797 uint16_t next; 798 799 if (state->ts_revid == AC_REV_ID1) { 800 data = &state->ts_regs->aud_regs.ap_acrd_35D_data; 801 } else { 802 data = &state->ts_regs->aud_regs.ap_acrdwr_data; 803 } 804 805 /* make sure the register is good */ 806 reg &= AP_ACRD_INDEX_MASK; 807 for (count = TS_LOOP_CNT; count--; ) { 808 if ((first = audiots_read_ac97(state, reg)) != 0) { 809 next = first; 810 break; 811 } 812 813 delay = TS_DELAY_CNT; 814 while (delay--) { 815 (void) ddi_get16(handle, data); 816 } 817 818 if ((next = audiots_read_ac97(state, reg)) != 0) { 819 break; 820 } 821 } 822 823 /* 824 * Arggg, if you let the next read happen too soon then it fails. 825 * 12 usec fails, 13 usec succeeds. So set it to 20 for safety. 826 */ 827 drv_usecwait(TS_20US); 828 829 return (next); 830 831 } 832 833 /* 834 * audiots_init_state() 835 * 836 * Description: 837 * This routine initializes the audio driver's state structure. 838 * This includes reading the properties. 839 * 840 * CAUTION: This routine cannot allocate resources, unless it frees 841 * them before returning for an error. Also, error_destroy: 842 * in audiots_attach() would need to be fixed as well. 843 * 844 * NOTE: birdsnest supports CD ROM input. We check for the cdrom 845 * property. If there we turn it on. 846 * 847 * Arguments: 848 * audiots_state_t *state The device's state structure 849 * dev_info_t *dip Pointer to the device's dev_info struct 850 * 851 * Returns: 852 * DDI_SUCCESS State structure initialized 853 * DDI_FAILURE State structure not initialized 854 */ 855 static int 856 audiots_init_state(audiots_state_t *state, dev_info_t *dip) 857 { 858 state->ts_ac97 = ac97_alloc(dip, audiots_get_ac97, 859 audiots_set_ac97, state); 860 861 if (state->ts_ac97 == NULL) { 862 return (DDI_FAILURE); 863 } 864 865 /* save the device info pointer */ 866 state->ts_dip = dip; 867 868 /* get the iblock cookie needed for interrupt context */ 869 if (ddi_get_iblock_cookie(dip, 0, &state->ts_iblock) != DDI_SUCCESS) { 870 audio_dev_warn(state->ts_adev, 871 "cannot get iblock cookie"); 872 return (DDI_FAILURE); 873 } 874 875 /* initialize the state mutexes and condition variables */ 876 mutex_init(&state->ts_lock, NULL, MUTEX_DRIVER, state->ts_iblock); 877 state->ts_flags |= TS_MUTEX_INIT; 878 879 for (int i = 0; i < TS_NUM_PORTS; i++) { 880 if (audiots_alloc_port(state, i) != DDI_SUCCESS) { 881 return (DDI_FAILURE); 882 } 883 } 884 /* init power management state */ 885 state->ts_suspended = B_FALSE; 886 887 return (DDI_SUCCESS); 888 889 } 890 891 /* 892 * audiots_intr() 893 * 894 * Description: 895 * Interrupt service routine for both play and record. For play we 896 * get the next buffers worth of audio. For record we send it on to 897 * the mixer. 898 * 899 * NOTE: This device needs to make sure any PIO access required to clear 900 * its interrupt has made it out on the PCI bus before returning from its 901 * interrupt handler so that the interrupt has been deasserted. This is 902 * done by rereading the address engine interrupt register. 903 * 904 * Arguments: 905 * caddr_t T Pointer to the interrupting device's state 906 * structure 907 * 908 * Returns: 909 * DDI_INTR_CLAIMED Interrupt claimed and processed 910 * DDI_INTR_UNCLAIMED Interrupt not claimed, and thus ignored 911 */ 912 static uint_t 913 audiots_intr(caddr_t T) 914 { 915 audiots_state_t *state = (void *)T; 916 audiots_regs_t *regs = state->ts_regs; 917 ddi_acc_handle_t handle = state->ts_acch; 918 uint32_t interrupts; 919 920 mutex_enter(&state->ts_lock); 921 922 if (state->ts_suspended) { 923 mutex_exit(&state->ts_lock); 924 return (DDI_INTR_UNCLAIMED); 925 } 926 927 interrupts = ddi_get32(handle, ®s->aud_regs.ap_aint); 928 if (interrupts == 0) { 929 mutex_exit(&state->ts_lock); 930 /* no interrupts to process, so it's not us */ 931 return (DDI_INTR_UNCLAIMED); 932 } 933 934 /* 935 * Clear the interrupts to acknowledge. Also, reread the 936 * interrupt reg to ensure that PIO write has completed. 937 */ 938 ddi_put32(handle, ®s->aud_regs.ap_aint, interrupts); 939 (void) ddi_get32(handle, ®s->aud_regs.ap_aint); 940 941 /* update the kernel interrupt statistics */ 942 if (state->ts_ksp) { 943 TS_KIOP(state)->intrs[KSTAT_INTR_HARD]++; 944 } 945 946 mutex_exit(&state->ts_lock); 947 948 for (int i = 0; i < TS_NUM_PORTS; i++) { 949 audiots_port_t *port = state->ts_ports[i]; 950 951 if (((interrupts & port->tp_int_mask) == 0) || 952 (!port->tp_started)) 953 continue; 954 955 if (i == TS_INPUT_PORT) { 956 audio_engine_produce(port->tp_engine); 957 } else { 958 audio_engine_consume(port->tp_engine); 959 } 960 } 961 962 return (DDI_INTR_CLAIMED); 963 964 } 965 966 /* 967 * audiots_map_regs() 968 * 969 * Description: 970 * This routine maps the registers in. 971 * 972 * Once the config space registers are mapped in we determine if the 973 * audio core may be power managed. It should, but if it doesn't, 974 * then trying to may cause the core to hang. 975 * 976 * CAUTION: Make sure all errors call audio_dev_warn(). 977 * 978 * Arguments: 979 * dev_info_t *dip Pointer to the device's devinfo 980 * audiots_state_t *state The device's state structure 981 * Returns: 982 * DDI_SUCCESS Registers successfully mapped 983 * DDI_FAILURE Registers not successfully mapped 984 */ 985 static int 986 audiots_map_regs(dev_info_t *dip, audiots_state_t *state) 987 { 988 char rev[16]; 989 char *name; 990 991 /* map in the registers, the config and memory mapped registers */ 992 if (pci_config_setup(dip, &state->ts_pcih) != DDI_SUCCESS) { 993 audio_dev_warn(state->ts_adev, 994 "unable to map PCI configuration space"); 995 return (DDI_FAILURE); 996 } 997 998 /* Read the Audio Controller's vendor, device, and revision IDs */ 999 state->ts_devid = 1000 (pci_config_get16(state->ts_pcih, PCI_CONF_VENID) << 16) | 1001 pci_config_get16(state->ts_pcih, PCI_CONF_DEVID); 1002 state->ts_revid = pci_config_get8(state->ts_pcih, PCI_CONF_REVID); 1003 1004 if (ddi_regs_map_setup(dip, TS_MEM_MAPPED_REGS, 1005 (caddr_t *)&state->ts_regs, 0, 0, &ts_regs_attr, &state->ts_acch) != 1006 DDI_SUCCESS) { 1007 audio_dev_warn(state->ts_adev, 1008 "unable to map PCI device registers"); 1009 return (DDI_FAILURE); 1010 } 1011 1012 switch (state->ts_devid) { 1013 case 0x10b95451: 1014 name = "ALI M5451"; 1015 break; 1016 default: 1017 name = "audiots"; 1018 break; 1019 } 1020 (void) snprintf(rev, sizeof (rev), "Rev %x", state->ts_revid); 1021 audio_dev_set_description(state->ts_adev, name); 1022 audio_dev_set_version(state->ts_adev, rev); 1023 1024 return (DDI_SUCCESS); 1025 } 1026 1027 /* 1028 * audiots_alloc_port() 1029 * 1030 * Description: 1031 * This routine allocates the DMA handles and the memory for the 1032 * DMA engines to use. It then binds each of the buffers to its 1033 * respective handle, getting a DMA cookie. 1034 * 1035 * NOTE: All of the ddi_dma_... routines sleep if they cannot get 1036 * memory. This means these calls should always succeed. 1037 * 1038 * NOTE: ddi_dma_alloc_handle() attempts to use the full 4 GB DMA address 1039 * range. This is to work around Southbridge rev E/G OBP issues. 1040 * (See Grover OBP note above) 1041 * 1042 * CAUTION: Make sure all errors call audio_dev_warn(). 1043 * 1044 * Arguments: 1045 * audiots_port_t *state The port structure for a device stream 1046 * int num The port number 1047 * 1048 * Returns: 1049 * DDI_SUCCESS DMA resources mapped 1050 * DDI_FAILURE DMA resources not successfully mapped 1051 */ 1052 int 1053 audiots_alloc_port(audiots_state_t *state, int num) 1054 { 1055 audiots_port_t *port; 1056 dev_info_t *dip = state->ts_dip; 1057 audio_dev_t *adev = state->ts_adev; 1058 char *prop; 1059 int dir; 1060 unsigned caps; 1061 ddi_dma_cookie_t cookie; 1062 unsigned count; 1063 int rc; 1064 ddi_acc_handle_t regsh = state->ts_acch; 1065 uint32_t *gcptr = &state->ts_regs->aud_regs.ap_cir_gc; 1066 char *namestr; 1067 1068 port = kmem_zalloc(sizeof (*port), KM_SLEEP); 1069 state->ts_ports[num] = port; 1070 port->tp_num = num; 1071 port->tp_state = state; 1072 port->tp_started = B_FALSE; 1073 port->tp_rate = 48000; 1074 1075 if (num == TS_INPUT_PORT) { 1076 prop = "record-interrupts"; 1077 dir = DDI_DMA_READ; 1078 caps = ENGINE_INPUT_CAP; 1079 port->tp_dma_stream = 31; 1080 port->tp_int_stream = 2; 1081 port->tp_sync_dir = DDI_DMA_SYNC_FORKERNEL; 1082 } else { 1083 prop = "play-interrupts"; 1084 dir = DDI_DMA_WRITE; 1085 caps = ENGINE_OUTPUT_CAP; 1086 port->tp_dma_stream = 0; 1087 port->tp_int_stream = 1; 1088 port->tp_sync_dir = DDI_DMA_SYNC_FORDEV; 1089 } 1090 port->tp_int_mask = (1U << port->tp_int_stream); 1091 port->tp_dma_mask = (1U << port->tp_dma_stream); 1092 1093 /* get the number of interrupts per second */ 1094 port->tp_intrs = ddi_prop_get_int(DDI_DEV_T_ANY, dip, 1095 DDI_PROP_DONTPASS, prop, TS_INTS); 1096 1097 /* make sure the values are good */ 1098 if (port->tp_intrs < TS_MIN_INTS) { 1099 audio_dev_warn(adev, "%s too low, %d, resetting to %d", 1100 prop, port->tp_intrs, TS_INTS); 1101 port->tp_intrs = TS_INTS; 1102 } else if (port->tp_intrs > TS_MAX_INTS) { 1103 audio_dev_warn(adev, "%s too high, %d, resetting to %d", 1104 prop, port->tp_intrs, TS_INTS); 1105 port->tp_intrs = TS_INTS; 1106 } 1107 1108 /* 1109 * Now allocate space. We configure for the worst case. The 1110 * worst (biggest) case is 48000 kHz, at 4 bytes per frame 1111 * (16-bit stereo), with the lowest interrupt frequency. We 1112 * need two fragments though, and each half has to be rounded 1113 * up to allow for alignment considerations. 1114 */ 1115 1116 /* allocate dma handle */ 1117 rc = ddi_dma_alloc_handle(dip, &audiots_attr, DDI_DMA_SLEEP, 1118 NULL, &port->tp_dmah); 1119 if (rc != DDI_SUCCESS) { 1120 audio_dev_warn(adev, "ddi_dma_alloc_handle failed: %d", rc); 1121 return (DDI_FAILURE); 1122 } 1123 /* allocate DMA buffer */ 1124 rc = ddi_dma_mem_alloc(port->tp_dmah, TS_BUFSZ, &ts_acc_attr, 1125 DDI_DMA_CONSISTENT, DDI_DMA_SLEEP, NULL, &port->tp_kaddr, 1126 &port->tp_size, &port->tp_acch); 1127 if (rc == DDI_FAILURE) { 1128 audio_dev_warn(adev, "dma_mem_alloc failed"); 1129 return (DDI_FAILURE); 1130 } 1131 1132 /* bind DMA buffer */ 1133 rc = ddi_dma_addr_bind_handle(port->tp_dmah, NULL, 1134 port->tp_kaddr, port->tp_size, dir|DDI_DMA_CONSISTENT, 1135 DDI_DMA_SLEEP, NULL, &cookie, &count); 1136 if (rc != DDI_DMA_MAPPED) { 1137 audio_dev_warn(adev, 1138 "ddi_dma_addr_bind_handle failed: %d", rc); 1139 return (DDI_FAILURE); 1140 } 1141 ASSERT(count == 1); 1142 1143 port->tp_paddr = cookie.dmac_address; 1144 if ((unsigned)port->tp_paddr & 0x80000000U) { 1145 ddi_put32(regsh, gcptr, 1146 ddi_get32(regsh, gcptr) | AP_CIR_GC_SYS_MEM_4G_ENABLE); 1147 } else { 1148 ddi_put32(regsh, gcptr, 1149 ddi_get32(regsh, gcptr) & ~(AP_CIR_GC_SYS_MEM_4G_ENABLE)); 1150 } 1151 port->tp_engine = audio_engine_alloc(&audiots_engine_ops, caps); 1152 if (port->tp_engine == NULL) { 1153 audio_dev_warn(adev, "audio_engine_alloc failed"); 1154 return (DDI_FAILURE); 1155 } 1156 1157 audio_engine_set_private(port->tp_engine, port); 1158 audio_dev_add_engine(adev, port->tp_engine); 1159 1160 state->ts_swapped = B_FALSE; 1161 1162 /* 1163 * SPARCLE platform specific hack. For reasons that I can't 1164 * seem to fathom, the SPARCLE platform only gets the 1165 * endianness correct when transferring whole 32-bit words and 1166 * using little endian mapping. That isn't compatible with 1167 * the audio framework's access mode, so we have to set up 1168 * explicit swapping of the left and right channels. 1169 * 1170 * The real mystery here is why this is required for Tadpole 1171 * SPARCLE, but not for any other standard Sun platforms that 1172 * I've tested. 1173 * 1174 * "swap_channels" property can be used in driver.conf to 1175 * force the left/right as well. 1176 */ 1177 rc = ddi_prop_lookup_string(DDI_DEV_T_ANY, ddi_root_node(), 0, 1178 "name", &namestr); 1179 if (rc == DDI_PROP_SUCCESS) { 1180 if (strcmp(namestr, "TAD,SPARCLE") == 0) { 1181 state->ts_swapped = B_TRUE; 1182 } 1183 ddi_prop_free(namestr); 1184 } 1185 1186 state->ts_swapped = ddi_prop_get_int(DDI_DEV_T_ANY, dip, 1187 DDI_PROP_NOTPROM | DDI_PROP_DONTPASS, "swap_channels", 1188 state->ts_swapped); 1189 1190 return (DDI_SUCCESS); 1191 } 1192 1193 /* 1194 * audiots_read_ac97() 1195 * 1196 * Description: 1197 * This routine actually reads the AC-97 Codec's register. It may 1198 * be called several times to succeed. 1199 * 1200 * NOTE: 1201 * Revision M1535D B1-C of the ALI SouthBridge includes a workaround for 1202 * the broken busy flag. Resetting the busy flag requires a software tweak 1203 * to go with the worked around hardware. When we detect failure, we make 1204 * 10 attempts to reset the chip before we fail. This should reset the new 1205 * SB systems. On all SB systems, this will increse the read delay 1206 * slightly, but shouldn't bother it otherwise. 1207 * 1208 * Arguments: 1209 * audiots_state_t *state The device's state structure 1210 * int reg AC-97 register number 1211 * 1212 * Returns: 1213 * unsigned short The value in the specified register 1214 */ 1215 static uint16_t 1216 audiots_read_ac97(audiots_state_t *state, int reg) 1217 { 1218 ddi_acc_handle_t acch = state->ts_acch; 1219 uint16_t *addr; 1220 uint16_t *data; 1221 uint32_t *stimer = &state->ts_regs->aud_regs.ap_stimer; 1222 uint32_t chk1; 1223 uint32_t chk2; 1224 int resets = 0; 1225 int i; 1226 1227 if (state->ts_revid == AC_REV_ID1) { 1228 addr = &state->ts_regs->aud_regs.ap_acrd_35D_reg; 1229 data = &state->ts_regs->aud_regs.ap_acrd_35D_data; 1230 } else { 1231 addr = &state->ts_regs->aud_regs.ap_acrdwr_reg; 1232 data = &state->ts_regs->aud_regs.ap_acrdwr_data; 1233 } 1234 1235 first_read: 1236 /* wait for ready to send read request */ 1237 for (i = 0; i < TS_READ_TRIES; i++) { 1238 if (!(ddi_get16(acch, addr) & AP_ACRD_R_READ_BUSY)) { 1239 break; 1240 } 1241 /* don't beat on the bus */ 1242 drv_usecwait(1); 1243 } 1244 if (i >= TS_READ_TRIES) { 1245 if (resets < TS_RESET_TRIES) { 1246 /* Attempt to reset */ 1247 drv_usecwait(TS_20US); 1248 ddi_put16(acch, addr, TS_SB_RESET); 1249 resets++; 1250 goto first_read; 1251 } else { 1252 state->ts_flags |= TS_AUDIO_READ_FAILED; 1253 if (!(state->ts_flags & TS_READ_FAILURE_PRINTED)) { 1254 ddi_dev_report_fault(state->ts_dip, 1255 DDI_SERVICE_LOST, DDI_DEVICE_FAULT, 1256 "Unable to communicate with AC97 CODEC"); 1257 audio_dev_warn(state->ts_adev, 1258 "The audio AC97 register has timed out."); 1259 audio_dev_warn(state->ts_adev, 1260 "Audio is now disabled."); 1261 audio_dev_warn(state->ts_adev, 1262 "Please reboot to restore audio."); 1263 1264 /* Don't flood the console */ 1265 state->ts_flags |= TS_READ_FAILURE_PRINTED; 1266 } 1267 } 1268 return (0); 1269 } 1270 1271 /* program the register to read */ 1272 ddi_put16(acch, addr, (reg|AP_ACRD_W_PRIMARY_CODEC| 1273 AP_ACRD_W_READ_MIXER_REG|AP_ACRD_W_AUDIO_READ_REQ& 1274 (~AP_ACWR_W_SELECT_WRITE))); 1275 1276 /* hardware bug work around */ 1277 chk1 = ddi_get32(acch, stimer); 1278 chk2 = ddi_get32(acch, stimer); 1279 i = TS_WAIT_CNT; 1280 while (chk1 == chk2 && i) { 1281 chk2 = ddi_get32(acch, stimer); 1282 i--; 1283 } 1284 OR_SET_SHORT(acch, addr, AP_ACRD_W_READ_MIXER_REG); 1285 resets = 0; 1286 1287 second_read: 1288 /* wait again for read to send read request */ 1289 for (i = 0; i < TS_READ_TRIES; i++) { 1290 if (!(ddi_get16(acch, addr) & AP_ACRD_R_READ_BUSY)) { 1291 break; 1292 } 1293 /* don't beat on the bus */ 1294 drv_usecwait(1); 1295 } 1296 if (i >= TS_READ_TRIES) { 1297 if (resets < TS_RESET_TRIES) { 1298 /* Attempt to reset */ 1299 drv_usecwait(TS_20US); 1300 ddi_put16(acch, addr, TS_SB_RESET); 1301 resets++; 1302 goto second_read; 1303 } else { 1304 state->ts_flags |= TS_AUDIO_READ_FAILED; 1305 if (!(state->ts_flags & TS_READ_FAILURE_PRINTED)) { 1306 ddi_dev_report_fault(state->ts_dip, 1307 DDI_SERVICE_LOST, DDI_DEVICE_FAULT, 1308 "Unable to communicate with AC97 CODEC"); 1309 audio_dev_warn(state->ts_adev, 1310 "The audio AC97 register has timed out."); 1311 audio_dev_warn(state->ts_adev, 1312 "Audio is now disabled."); 1313 audio_dev_warn(state->ts_adev, 1314 "Please reboot to restore audio."); 1315 1316 /* Don't flood the console */ 1317 state->ts_flags |= TS_READ_FAILURE_PRINTED; 1318 } 1319 } 1320 return (0); 1321 } 1322 1323 return (ddi_get16(acch, data)); 1324 1325 } /* audiots_read_ac97() */ 1326 1327 /* 1328 * audiots_set_ac97() 1329 * 1330 * Description: 1331 * Set the value in the specified AC-97 Codec register. Just like 1332 * reading the AC-97 Codec, it is possible there is a problem writing 1333 * it as well. So we loop. 1334 * 1335 * Arguments: 1336 * audiots_state_t *state The device's state structure 1337 * int reg AC-97 register number 1338 * uint16_t value The value to write 1339 * 1340 * Returns: 1341 * void 1342 */ 1343 static void 1344 audiots_set_ac97(void *arg, uint8_t reg8, uint16_t data) 1345 { 1346 audiots_state_t *state = arg; 1347 ddi_acc_handle_t handle = state->ts_acch; 1348 uint16_t *data_addr = &state->ts_regs->aud_regs.ap_acrdwr_data; 1349 uint16_t *reg_addr = &state->ts_regs->aud_regs.ap_acrdwr_reg; 1350 int count; 1351 int i; 1352 uint16_t tmp_short; 1353 uint16_t reg = reg8; 1354 1355 reg &= AP_ACWR_INDEX_MASK; 1356 1357 /* Don't touch the reserved bits on the pre 35D+ SouthBridge */ 1358 if (state->ts_revid == AC_REV_ID1) { 1359 reg |= AP_ACWR_W_PRIMARY_CODEC|AP_ACWR_W_WRITE_MIXER_REG; 1360 } else { 1361 reg |= AP_ACWR_W_PRIMARY_CODEC|AP_ACWR_W_WRITE_MIXER_REG| 1362 AP_ACWR_W_SELECT_WRITE; 1363 } 1364 1365 for (count = TS_LOOP_CNT; count--; ) { 1366 /* wait for ready to write */ 1367 for (i = 0; i < TS_WAIT_CNT; i++) { 1368 if (!(ddi_get16(handle, reg_addr) & 1369 AP_ACWR_R_WRITE_BUSY)) { 1370 /* ready to write */ 1371 ddi_put16(handle, reg_addr, reg); 1372 1373 /* Write the data */ 1374 ddi_put16(handle, data_addr, data); 1375 break; 1376 } 1377 } 1378 if (i >= TS_WAIT_CNT) { 1379 /* try again */ 1380 continue; 1381 } 1382 1383 /* wait for write to complete */ 1384 for (i = 0; i < TS_WAIT_CNT; i++) { 1385 if (!(ddi_get16(handle, reg_addr) & 1386 AP_ACWR_R_WRITE_BUSY)) { 1387 /* done writing */ 1388 break; 1389 } 1390 } 1391 1392 /* verify the value written */ 1393 tmp_short = audiots_get_ac97(state, reg8); 1394 if (data == tmp_short) { 1395 /* successfully loaded, so we can return */ 1396 return; 1397 } 1398 } 1399 1400 } /* audiots_set_ac97() */ 1401 1402 /* 1403 * audiots_reset_port() 1404 * 1405 * Description: 1406 * Initializes the hardware for a DMA engine. 1407 * We only support stereo 16-bit linear PCM (signed native endian). 1408 * 1409 * The audio core uses a single DMA buffer which is divided into two 1410 * halves. An interrupt is generated when the middle of the buffer has 1411 * been reached and at the end. The audio core resets the pointer back 1412 * to the beginning automatically. After the interrupt the driver clears 1413 * the buffer and asks the mixer for more audio samples. If there aren't 1414 * enough then silence is played out. 1415 * 1416 * Arguments: 1417 * audiots_port_t *port The DMA engine to reset 1418 * 1419 * Returns: 1420 * void 1421 */ 1422 static void 1423 audiots_reset_port(audiots_port_t *port) 1424 { 1425 audiots_state_t *state = port->tp_state; 1426 ddi_acc_handle_t handle = state->ts_acch; 1427 audiots_regs_t *regs = state->ts_regs; 1428 audiots_aram_t *aram; 1429 audiots_eram_t *eram; 1430 unsigned delta; 1431 uint16_t ctrl; 1432 uint16_t gvsel; 1433 uint16_t eso; 1434 1435 if (state->ts_suspended) 1436 return; 1437 1438 port->tp_cso = 0; 1439 1440 gvsel = ERAM_WAVE_VOL | ERAM_PAN_0dB | ERAM_VOL_DEFAULT; 1441 ctrl = ERAM_16_BITS | ERAM_STEREO | ERAM_LOOP_MODE | ERAM_SIGNED_PCM; 1442 for (int i = 0; i < 2; i++) { 1443 1444 delta = (port->tp_rate << TS_SRC_SHIFT) / TS_RATE; 1445 1446 if (i == 0) { 1447 /* first do the DMA stream */ 1448 aram = ®s->aud_ram[port->tp_dma_stream].aram; 1449 eram = ®s->aud_ram[port->tp_dma_stream].eram; 1450 if (port->tp_num == TS_INPUT_PORT) { 1451 delta = (TS_RATE << TS_SRC_SHIFT) / 1452 port->tp_rate; 1453 } 1454 eso = port->tp_nframes - 1; 1455 } else { 1456 /* else do the interrupt stream */ 1457 aram = ®s->aud_ram[port->tp_int_stream].aram; 1458 eram = ®s->aud_ram[port->tp_int_stream].eram; 1459 /* interrupt stream is silent */ 1460 gvsel |= ERAM_VOL_MAX_ATTEN; 1461 eso = port->tp_fragfr - 1; 1462 } 1463 1464 /* program the sample rate */ 1465 ddi_put16(handle, &aram->aram_delta, (uint16_t)delta); 1466 1467 /* program the precision, number of channels and loop mode */ 1468 ddi_put16(handle, &eram->eram_ctrl_ec, ctrl); 1469 1470 /* program the volume settings */ 1471 ddi_put16(handle, &eram->eram_gvsel_pan_vol, gvsel); 1472 1473 /* set ALPHA and FMS to 0 */ 1474 ddi_put16(handle, &aram->aram_alpha_fms, 0x0); 1475 1476 /* set CSO to 0 */ 1477 ddi_put16(handle, &aram->aram_cso, 0x0); 1478 1479 /* set LBA */ 1480 ddi_put32(handle, &aram->aram_cptr_lba, 1481 port->tp_paddr & ARAM_LBA_MASK); 1482 1483 /* set ESO */ 1484 ddi_put16(handle, &aram->aram_eso, eso); 1485 } 1486 1487 /* stop the DMA & interrupt engines */ 1488 ddi_put32(handle, ®s->aud_regs.ap_stop, 1489 port->tp_int_mask | port->tp_dma_mask); 1490 1491 /* enable interrupts */ 1492 OR_SET_WORD(handle, ®s->aud_regs.ap_ainten, port->tp_int_mask); 1493 } 1494 1495 /* 1496 * audiots_open() 1497 * 1498 * Description: 1499 * Opens a DMA engine for use. Will also ensure the device is powered 1500 * up if not already done so. 1501 * 1502 * Arguments: 1503 * void *arg The DMA engine to set up 1504 * int flag Open flags 1505 * unsigned *fragfrp Receives number of frames per fragment 1506 * unsigned *nfragsp Receives number of fragments 1507 * caddr_t *bufp Receives kernel data buffer 1508 * 1509 * Returns: 1510 * 0 on success 1511 * errno on failure 1512 */ 1513 static int 1514 audiots_open(void *arg, int flag, 1515 unsigned *fragfrp, unsigned *nfragsp, caddr_t *bufp) 1516 { 1517 audiots_port_t *port = arg; 1518 unsigned nfrag; 1519 1520 _NOTE(ARGUNUSED(flag)); 1521 1522 /* 1523 * Round up - we have to have a sample that is a whole number 1524 * of 64-bit words. Since our frames are 4 bytes wide, we 1525 * just need an even number of frames. 1526 */ 1527 port->tp_fragfr = port->tp_rate / port->tp_intrs; 1528 port->tp_fragfr = (port->tp_fragfr + 1) & ~1; 1529 nfrag = port->tp_size / (port->tp_fragfr * TS_FRAMESZ); 1530 port->tp_nframes = nfrag * port->tp_fragfr; 1531 port->tp_started = B_FALSE; 1532 port->tp_count = 0; 1533 port->tp_cso = 0; 1534 *fragfrp = port->tp_fragfr; 1535 *nfragsp = nfrag; 1536 *bufp = port->tp_kaddr; 1537 1538 /* 1539 * This should always be true because we used a worst case 1540 * assumption when calculating the port->tp_size. 1541 */ 1542 ASSERT((port->tp_fragfr * nfrag) <= port->tp_size); 1543 1544 mutex_enter(&port->tp_state->ts_lock); 1545 audiots_reset_port(port); 1546 mutex_exit(&port->tp_state->ts_lock); 1547 1548 return (0); 1549 } 1550 1551 /* 1552 * audiots_close() 1553 * 1554 * Description: 1555 * Closes an audio DMA engine that was previously opened. Since 1556 * nobody is using it, we take this opportunity to possibly power 1557 * down the entire device. 1558 * 1559 * Arguments: 1560 * void *arg The DMA engine to shut down 1561 * 1562 * Returns: 1563 * void 1564 */ 1565 static void 1566 audiots_close(void *arg) 1567 { 1568 audiots_port_t *port = arg; 1569 audiots_state_t *state = port->tp_state; 1570 1571 mutex_enter(&state->ts_lock); 1572 audiots_stop_port(port); 1573 port->tp_started = B_FALSE; 1574 mutex_exit(&state->ts_lock); 1575 } 1576 1577 /* 1578 * audiots_stop() 1579 * 1580 * Description: 1581 * This is called by the framework to stop a port that is 1582 * transferring data. 1583 * 1584 * Arguments: 1585 * void *arg The DMA engine to stop 1586 * 1587 * Returns: 1588 * void 1589 */ 1590 static void 1591 audiots_stop(void *arg) 1592 { 1593 audiots_port_t *port = arg; 1594 audiots_state_t *state = port->tp_state; 1595 1596 mutex_enter(&state->ts_lock); 1597 if (port->tp_started) { 1598 audiots_stop_port(port); 1599 } 1600 port->tp_started = B_FALSE; 1601 mutex_exit(&state->ts_lock); 1602 } 1603 1604 /* 1605 * audiots_start() 1606 * 1607 * Description: 1608 * This is called by the framework to start a port transferring data. 1609 * 1610 * Arguments: 1611 * void *arg The DMA engine to start 1612 * 1613 * Returns: 1614 * 0 on success (never fails, errno if it did) 1615 */ 1616 static int 1617 audiots_start(void *arg) 1618 { 1619 audiots_port_t *port = arg; 1620 audiots_state_t *state = port->tp_state; 1621 1622 mutex_enter(&state->ts_lock); 1623 if (!port->tp_started) { 1624 audiots_start_port(port); 1625 port->tp_started = B_TRUE; 1626 } 1627 mutex_exit(&state->ts_lock); 1628 return (0); 1629 } 1630 1631 /* 1632 * audiots_chinfo() 1633 * 1634 * Description: 1635 * This is called by the framework to query the channel offsets 1636 * and ordering. 1637 * 1638 * Arguments: 1639 * void *arg The DMA engine to query 1640 * int chan Channel number. 1641 * unsigned *offset Starting offset of channel. 1642 * unsigned *incr Increment (in samples) between frames. 1643 * 1644 * Returns: 1645 * 0 indicating rate array is range instead of enumeration 1646 */ 1647 1648 static void 1649 audiots_chinfo(void *arg, int chan, unsigned *offset, unsigned *incr) 1650 { 1651 audiots_port_t *port = arg; 1652 1653 /* if channels are swapped (SPARCLE), then deal with it */ 1654 if (port->tp_state->ts_swapped) { 1655 *offset = (chan ? 0 : 1); 1656 } else { 1657 *offset = chan; 1658 } 1659 *incr = 2; 1660 } 1661 1662 /* 1663 * audiots_format() 1664 * 1665 * Description: 1666 * Called by the framework to query the format for the device. 1667 * 1668 * Arguments: 1669 * void *arg The DMA engine to query 1670 * 1671 * Returns: 1672 * AUDIO_FORMAT_S16_LE. 1673 */ 1674 static int 1675 audiots_format(void *arg) 1676 { 1677 _NOTE(ARGUNUSED(arg)); 1678 1679 return (AUDIO_FORMAT_S16_LE); 1680 } 1681 1682 1683 /* 1684 * audiots_channels() 1685 * 1686 * Description: 1687 * Called by the framework to query the channnels for the device. 1688 * 1689 * Arguments: 1690 * void *arg The DMA engine to query 1691 * 1692 * Returns: 1693 * 2 (Stereo). 1694 */ 1695 static int 1696 audiots_channels(void *arg) 1697 { 1698 _NOTE(ARGUNUSED(arg)); 1699 1700 return (2); 1701 } 1702 1703 /* 1704 * audiots_rate() 1705 * 1706 * Description: 1707 * Called by the framework to query the sample rates for the device. 1708 * 1709 * Arguments: 1710 * void *arg The DMA engine to query 1711 * 1712 * Returns: 1713 * Sample rate in HZ (always 48000). 1714 */ 1715 static int 1716 audiots_rate(void *arg) 1717 { 1718 audiots_port_t *port = arg; 1719 1720 return (port->tp_rate); 1721 } 1722 1723 /* 1724 * audiots_count() 1725 * 1726 * Description: 1727 * This is called by the framework to get the engine's frame counter 1728 * 1729 * Arguments: 1730 * void *arg The DMA engine to query 1731 * 1732 * Returns: 1733 * frame count for current engine 1734 */ 1735 static uint64_t 1736 audiots_count(void *arg) 1737 { 1738 audiots_port_t *port = arg; 1739 audiots_state_t *state = port->tp_state; 1740 uint64_t val; 1741 1742 mutex_enter(&state->ts_lock); 1743 audiots_update_port(port); 1744 1745 val = port->tp_count; 1746 mutex_exit(&state->ts_lock); 1747 return (val); 1748 } 1749 1750 /* 1751 * audiots_sync() 1752 * 1753 * Description: 1754 * This is called by the framework to synchronize DMA caches. 1755 * We also leverage this do some endian swapping, because on SPARC 1756 * the chip accesses the DMA region using 32-bit little-endian 1757 * accesses. Its not enough to just use the framework's sample 1758 * conversion logic, because the channels will also be backwards. 1759 * 1760 * Arguments: 1761 * void *arg The DMA engine to sync 1762 * 1763 * Returns: 1764 * void 1765 */ 1766 static void 1767 audiots_sync(void *arg, unsigned nframes) 1768 { 1769 audiots_port_t *port = arg; 1770 _NOTE(ARGUNUSED(nframes)); 1771 1772 (void) ddi_dma_sync(port->tp_dmah, 0, 0, port->tp_sync_dir); 1773 } 1774 1775 /* 1776 * audiots_qlen() 1777 * 1778 * Description: 1779 * This is called by the framework to determine on-device queue length. 1780 * 1781 * Arguments: 1782 * void *arg The DMA engine to query 1783 * 1784 * Returns: 1785 * hardware queue length not reported by count (0 for this device) 1786 */ 1787 static size_t 1788 audiots_qlen(void *arg) 1789 { 1790 _NOTE(ARGUNUSED(arg)); 1791 return (0); 1792 } 1793 1794 /* 1795 * audiots_start_port() 1796 * 1797 * Description: 1798 * The audio core uses a single DMA buffer which is divided into two 1799 * halves. An interrupt is generated when the middle of the buffer has 1800 * been reached and at the end. The audio core resets the pointer back 1801 * to the beginning automatically. After the interrupt the driver clears 1802 * the buffer and asks the mixer for more audio samples. If there aren't 1803 * enough then silence is played out. 1804 * 1805 * Arguments: 1806 * audiots_port_t *port The DMA engine to start up 1807 * 1808 * Returns: 1809 * void 1810 */ 1811 static void 1812 audiots_start_port(audiots_port_t *port) 1813 { 1814 audiots_state_t *state = port->tp_state; 1815 audiots_regs_t *regs = state->ts_regs; 1816 ddi_acc_handle_t handle = state->ts_acch; 1817 1818 ASSERT(mutex_owned(&state->ts_lock)); 1819 1820 /* if suspended then do nothing else */ 1821 if (state->ts_suspended) { 1822 return; 1823 } 1824 1825 /* make sure it starts playing */ 1826 ddi_put32(handle, ®s->aud_regs.ap_start, 1827 port->tp_dma_mask | port->tp_int_mask); 1828 1829 ASSERT(mutex_owned(&state->ts_lock)); 1830 } 1831 1832 /* 1833 * audiots_stop_port() 1834 * 1835 * Description: 1836 * This routine stops a DMA engine. 1837 * 1838 * Arguments: 1839 * audiots_port_t *port The port to stop 1840 * 1841 * Returns: 1842 * void 1843 */ 1844 static void 1845 audiots_stop_port(audiots_port_t *port) 1846 { 1847 audiots_state_t *state = port->tp_state; 1848 1849 ASSERT(mutex_owned(&state->ts_lock)); 1850 1851 if (state->ts_suspended) 1852 return; 1853 1854 ddi_put32(state->ts_acch, &state->ts_regs->aud_regs.ap_stop, 1855 port->tp_int_mask | port->tp_dma_mask); 1856 1857 ASSERT(mutex_owned(&state->ts_lock)); 1858 } 1859 1860 /* 1861 * audiots_update_port() 1862 * 1863 * Description: 1864 * This routine updates the ports frame counter from hardware, and 1865 * gracefully handles wraps. 1866 * 1867 * Arguments: 1868 * audiots_port_t *port The port to stop 1869 * 1870 * Returns: 1871 * void 1872 */ 1873 static void 1874 audiots_update_port(audiots_port_t *port) 1875 { 1876 audiots_state_t *state = port->tp_state; 1877 1878 uint16_t cso; 1879 unsigned n; 1880 1881 ASSERT(mutex_owned(&state->ts_lock)); 1882 1883 if (state->ts_suspended) 1884 return; 1885 1886 cso = ddi_get16(state->ts_acch, 1887 &state->ts_regs->aud_ram[port->tp_dma_stream].aram.aram_cso); 1888 1889 n = (cso >= port->tp_cso) ? 1890 cso - port->tp_cso : 1891 cso + port->tp_nframes - port->tp_cso; 1892 1893 port->tp_cso = cso; 1894 port->tp_count += n; 1895 } 1896 1897 /* 1898 * audiots_stop_everything() 1899 * 1900 * Description: 1901 * This routine disables the address engine interrupt for all 32 DMA 1902 * engines. Just to be sure, it then explicitly issues a stop command to 1903 * the address engine and envelope engines for all 32 channels. 1904 * 1905 * NOTE: 1906 * 1907 * There is a hardware bug that generates a spurious interrupt 1908 * when the DMA engines are stopped. It's not consistent - it 1909 * happens every 1 out of 6 stops or so. It will show up as a 1910 * record interrupt. The problem is that once the driver is 1911 * detached or if the system goes into low power mode, nobody 1912 * will service that interrupt. The system will eventually become 1913 * unusable. 1914 * 1915 * Arguments: 1916 * audiots_state_t *state The device's state structure 1917 * 1918 * Returns: 1919 * void 1920 */ 1921 static void 1922 audiots_stop_everything(audiots_state_t *state) 1923 { 1924 if (state->ts_acch == NULL) 1925 return; 1926 1927 ddi_put32(state->ts_acch, &state->ts_regs->aud_regs.ap_ainten, 1928 TS_ALL_DMA_OFF); 1929 1930 ddi_put32(state->ts_acch, &state->ts_regs->aud_regs.ap_stop, 1931 TS_ALL_DMA_ENGINES); 1932 1933 ddi_put32(state->ts_acch, &state->ts_regs->aud_regs.ap_aint, 1934 TS_ALL_DMA_ENGINES); 1935 } 1936 1937 /* 1938 * audiots_free_port() 1939 * 1940 * Description: 1941 * This routine unbinds the DMA cookies, frees the DMA buffers, 1942 * deallocates the DMA handles. 1943 * 1944 * Arguments: 1945 * audiots_port_t *port The port structure for a device stream. 1946 * 1947 * Returns: 1948 * None 1949 */ 1950 void 1951 audiots_free_port(audiots_port_t *port) 1952 { 1953 if (port == NULL) 1954 return; 1955 1956 if (port->tp_engine) { 1957 audio_dev_remove_engine(port->tp_state->ts_adev, 1958 port->tp_engine); 1959 audio_engine_free(port->tp_engine); 1960 } 1961 if (port->tp_paddr) { 1962 (void) ddi_dma_unbind_handle(port->tp_dmah); 1963 } 1964 if (port->tp_acch) { 1965 ddi_dma_mem_free(&port->tp_acch); 1966 } 1967 if (port->tp_dmah) { 1968 ddi_dma_free_handle(&port->tp_dmah); 1969 } 1970 kmem_free(port, sizeof (*port)); 1971 } 1972 1973 /* 1974 * audiots_destroy() 1975 * 1976 * Description: 1977 * This routine releases all resources held by the device instance, 1978 * as part of either detach or a failure in attach. 1979 * 1980 * Arguments: 1981 * audiots_state_t *state The device soft state. 1982 * 1983 * Returns: 1984 * None 1985 */ 1986 void 1987 audiots_destroy(audiots_state_t *state) 1988 { 1989 audiots_stop_everything(state); 1990 1991 if (state->ts_flags & TS_INTR_INSTALLED) 1992 ddi_remove_intr(state->ts_dip, 0, NULL); 1993 1994 if (state->ts_ksp) 1995 kstat_delete(state->ts_ksp); 1996 1997 for (int i = 0; i < TS_NUM_PORTS; i++) 1998 audiots_free_port(state->ts_ports[i]); 1999 2000 if (state->ts_acch) 2001 ddi_regs_map_free(&state->ts_acch); 2002 2003 if (state->ts_pcih) 2004 pci_config_teardown(&state->ts_pcih); 2005 2006 if (state->ts_ac97) 2007 ac97_free(state->ts_ac97); 2008 2009 if (state->ts_adev) 2010 audio_dev_free(state->ts_adev); 2011 2012 if (state->ts_flags & TS_MUTEX_INIT) { 2013 mutex_destroy(&state->ts_lock); 2014 } 2015 2016 ddi_soft_state_free(audiots_statep, ddi_get_instance(state->ts_dip)); 2017 } 2018