3 The contents of this file are subject to the AROS Public License Version 1.1 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
4 http://www.aros.org/license.html
6 Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF
7 ANY KIND, either express or implied. See the License for the specific language governing rights and
8 limitations under the License.
10 The Original Code is (C) Copyright 2004-2011 Ross Vumbaca.
12 The Initial Developer of the Original Code is Ross Vumbaca.
18 #include <exec/memory.h>
21 #include <aros/debug.h>
24 #include <proto/exec.h>
25 #include <proto/dos.h>
29 #include "interrupt.h"
32 #include "pci_wrapper.h"
35 #define DebugPrintF bug
36 INTGW(static, void, playbackinterrupt
, PlaybackInterrupt
);
37 INTGW(static, void, recordinterrupt
, RecordInterrupt
);
38 INTGW(static, ULONG
, cardinterrupt
, CardInterrupt
);
41 /* Global in Card.c */
42 extern const UWORD InputBits
[];
44 /* Public functions in main.c */
45 int card_init(struct SB128_DATA
*card
);
46 void card_cleanup(struct SB128_DATA
*card
);
48 #if !defined(__AROS__)
49 void AddResetHandler(struct SB128_DATA
*card
);
52 void micro_delay(unsigned int val
)
54 struct timerequest
* TimerIO
= NULL
;
55 struct MsgPort
* replymp
;
57 replymp
= (struct MsgPort
*) CreateMsgPort();
60 DebugPrintF("SB128: Couldn't create reply port\n");
64 TimerIO
= (struct timerequest
*)CreateIORequest(replymp
, sizeof(struct timerequest
));
68 DebugPrintF("SB128: Out of memory.\n");
72 if (OpenDevice((CONST_STRPTR
) "timer.device", UNIT_MICROHZ
, (struct IORequest
*)TimerIO
, 0) != 0)
74 DebugPrintF("SB128: Unable to open 'timer.device'.\n");
78 TimerIO
->tr_node
.io_Command
= TR_ADDREQUEST
;
79 TimerIO
->tr_time
.tv_secs
= 0;
80 TimerIO
->tr_time
.tv_micro
= val
;
81 DoIO((struct IORequest
*)TimerIO
);
82 CloseDevice((struct IORequest
*)TimerIO
);
83 DeleteIORequest((struct IORequest
*)TimerIO
);
88 DeleteMsgPort(replymp
);
92 unsigned long src_ready(struct SB128_DATA
*card
)
97 /* Wait for the busy bit to become invalid, and then return the contents
98 of the SRC register. */
99 for (i
= 0; i
< 0x1000; i
++)
101 if (!((r
= pci_inl(SB128_SRC
, card
)) & SRC_BUSY
))
106 DebugPrintF("SB128: SRC Ready timeout.\n");
110 void src_write(struct SB128_DATA
*card
, unsigned short addr
, unsigned short data
)
114 // ObtainSemaphore(&card->sb128_semaphore);
116 /* Get copy of SRC register when it's not busy, add address and data, write back. */
117 r
= src_ready(card
) & (SRC_DISABLE
| SRC_DIS_DAC2
| SRC_DIS_ADC
);
118 r
= r
| (addr
<< SRC_ADDR_SHIFT
);
120 pci_outl(r
| SRC_WE
, SB128_SRC
, card
);
123 // ReleaseSemaphore(&card->sb128_semaphore);
126 unsigned short src_read(struct SB128_DATA
*card
, unsigned short addr
)
128 //There may be ES137x bugs that require accomodating in this section.
132 // ObtainSemaphore(&card->sb128_semaphore);
134 /* Get copy of SRC register when it's not busy, add address, write back,
135 wait for ready, then read back value. */
136 r
= src_ready(card
) & (SRC_DISABLE
| SRC_DIS_DAC2
| SRC_DIS_ADC
);
137 r
= r
| (addr
<< SRC_ADDR_SHIFT
);
138 pci_outl(r
, SB128_SRC
, card
);
140 /* Give the chip a chance to set the busy bit. */
143 // ReleaseSemaphore(&card->sb128_semaphore);
145 return (src_ready(card
) & 0xFFFF);
148 /* Translate AC97 commands to AK4531 commands, and write to the AK4531 codec */
149 void ak4531_ac97_write(struct SB128_DATA
*card
, unsigned short reg
, unsigned short val
)
156 char input_right
= 0;
164 if (reg
== AC97_RECORD_SELECT
)
166 /* Change input select settings */
168 input_left
= val
>> 8;
170 /* Translate as appropriate */
171 switch (input_left
) {
198 /* Stereo Mix (all) */
213 /* Shouldn't happen */
214 DebugPrintF("SB128: Unsupported Record Input command\n");
217 switch (input_right
) {
244 /* Stereo Mix (all) */
259 /* Shouldn't happen */
260 DebugPrintF("SB128: Unsupported Record Input command\n");
263 /* Write input values to AK4531 */
265 codec_write(card
, AK4531_INPUT_MUX_L_1
, ak4531_L1
);
266 codec_write(card
, AK4531_INPUT_MUX_R_1
, ak4531_R1
);
267 codec_write(card
, AK4531_INPUT_MUX_L_2
, ak4531_L2
);
268 codec_write(card
, AK4531_INPUT_MUX_R_2
, ak4531_R2
);
272 if (reg
== AC97_PHONE_VOL
|| AC97_MIC_VOL
|| AC97_LINEIN_VOL
|| AC97_CD_VOL
|| AC97_AUX_VOL
|| AC97_PCMOUT_VOL
)
274 /* Adjust PCM (from card) Input gain */
277 /* Using a muted volume */
278 right_vol
= (AK4531_MUTE
| 0x6);
279 left_vol
= (AK4531_MUTE
| 0x6);
284 right_vol
= (val
& 0x1F);
285 left_vol
= (val
>> 8);
287 /* Convert right volume */
290 steps
= 0x8 - right_vol
;
293 right_vol
= (int) (steps
+ 0.5);
294 right_vol
= 0x6 - right_vol
;
296 else if (right_vol
> 0x8)
298 steps
= right_vol
- 0x8;
301 right_vol
= (int) (steps
+ 0.5);
302 right_vol
= 0x6 + right_vol
;
304 else if (right_vol
== 0x8)
306 /* No attentuation, no mute */
310 /* Convert left volume */
313 steps
= 0x8 - left_vol
;
316 left_vol
= (int) (steps
+ 0.5);
317 left_vol
= 0x6 - left_vol
;
319 else if (left_vol
> 0x8)
321 steps
= left_vol
- 0x8;
324 left_vol
= (int) (steps
+ 0.5);
325 left_vol
= 0x6 + left_vol
;
327 else if (left_vol
== 0x8)
329 /* No attentuation, no mute */
335 /* Write adjusted volume to appropriate place */
336 /* Un-mute, and disable output, if an input muted */
340 codec_write(card
, AK4531_PHONE_VOL_L
, right_vol
);
341 codec_write(card
, AK4531_PHONE_VOL_R
, right_vol
);
345 codec_write(card
, AK4531_MIC_VOL
, right_vol
);
348 case AC97_LINEIN_VOL
:
349 if ((left_vol
& AK4531_MUTE
) && (right_vol
& AK4531_MUTE
))
353 /* Disable on OUTPUT mux */
354 card
->ak4531_output_1
= (card
->ak4531_output_1
& ~(AK4531_OUTPUT_LINE
));
355 codec_write(card
, AK4531_OUTPUT_MUX_1
, card
->ak4531_output_1
);
359 codec_write(card
, AK4531_LINEIN_VOL_L
, left_vol
);
360 codec_write(card
, AK4531_LINEIN_VOL_R
, right_vol
);
361 /* Re-enable on OUTPUT mux */
362 card
->ak4531_output_1
= (card
->ak4531_output_1
| AK4531_OUTPUT_LINE
);
363 codec_write(card
, AK4531_OUTPUT_MUX_1
, card
->ak4531_output_1
);
368 if ((left_vol
& AK4531_MUTE
) && (right_vol
& AK4531_MUTE
))
372 /* Disable on OUTPUT mux */
373 card
->ak4531_output_1
= (card
->ak4531_output_1
& ~(AK4531_OUTPUT_CD
));
374 codec_write(card
, AK4531_OUTPUT_MUX_1
, card
->ak4531_output_1
);
378 codec_write(card
, AK4531_CD_VOL_L
, left_vol
);
379 codec_write(card
, AK4531_CD_VOL_R
, right_vol
);
380 /* Re-enable on OUTPUT mux */
381 card
->ak4531_output_1
= (card
->ak4531_output_1
| AK4531_OUTPUT_CD
);
382 codec_write(card
, AK4531_OUTPUT_MUX_1
, card
->ak4531_output_1
);
387 if ((left_vol
& AK4531_MUTE
) && (right_vol
& AK4531_MUTE
))
391 /* Disable on OUTPUT mux */
392 card
->ak4531_output_2
= (card
->ak4531_output_2
& ~(AK4531_OUTPUT_AUX
));
393 codec_write(card
, AK4531_OUTPUT_MUX_2
, card
->ak4531_output_2
);
397 codec_write(card
, AK4531_AUX_VOL_L
, left_vol
);
398 codec_write(card
, AK4531_AUX_VOL_R
, right_vol
);
399 /* Re-enable on OUTPUT mux */
400 card
->ak4531_output_2
= (card
->ak4531_output_2
| AK4531_OUTPUT_AUX
);
401 codec_write(card
, AK4531_OUTPUT_MUX_2
, card
->ak4531_output_2
);
405 case AC97_PCMOUT_VOL
:
406 codec_write(card
, AK4531_PCMOUT_VOL_L
, left_vol
);
407 codec_write(card
, AK4531_PCMOUT_VOL_R
, right_vol
);
411 DebugPrintF("SB128: Invalid value for Volume Set\n");
420 void codec_write(struct SB128_DATA
*card
, unsigned short reg
, unsigned short val
)
424 /* Take hold of the hardware semaphore */
425 //ObtainSemaphore(&card->sb128_semaphore);
429 for (i
= 0; i
< 10; i
++)
431 if (!(pci_inl(SB128_STATUS
, card
) & CODEC_CSTAT
))
435 DebugPrintF("SB128: Couldn't write to ak4531!\n");
439 pci_outw(((unsigned char)reg
<< ES1370_CODEC_ADD_SHIFT
) | (unsigned char)val
, ES1370_SB128_CODEC
, card
);
446 for (i
= 0; i
< 0x1000; i
++)
448 if (!(pci_inl(SB128_CODEC
, card
) & CODEC_WIP
))
451 DebugPrintF("SB128: Couldn't write to ac97! (1)\n");
452 //ReleaseSemaphore(&card->sb128_semaphore);
456 /* Get copy of SRC register when it's not busy. */
458 /* Enable "SRC State Data", an undocumented feature! */
459 pci_outl((r
& (SRC_DISABLE
| SRC_DIS_DAC2
| SRC_DIS_ADC
)) | 0x00010000, SB128_SRC
, card
);
461 /* Wait for "state 0", to avoid "transition states". */
462 for (i
= 0; i
< 0x1000; i
++)
464 if ((pci_inl(SB128_SRC
, card
) & 0x00870000) == 0x00)
469 /* Now wait for an undocumented bit to be set (and the SRC to be NOT busy) */
470 for (i
= 0; i
< 0x1000; i
++)
472 if ((pci_inl(SB128_SRC
, card
) & 0x00870000) == 0x00010000)
477 /* Write out the value to the codec now. */
478 pci_outl((((reg
<< CODEC_ADD_SHIFT
) & CODEC_ADD_MASK
) | val
), SB128_CODEC
, card
);
480 /* Delay to make sure the chip had time to set the WIP after
484 /* Restore SRC register. */
486 pci_outl(r
, SB128_SRC
, card
);
488 /* Check for WIP before returning. */
489 for (i
= 0; i
< 0x1000; i
++)
491 if (!(pci_inl(SB128_CODEC
, card
) & CODEC_WIP
))
493 //ReleaseSemaphore(&card->sb128_semaphore);
498 DebugPrintF("SB128: Couldn't write to ac97! (2)\n");
501 //ReleaseSemaphore(&card->sb128_semaphore);
505 unsigned short codec_read(struct SB128_DATA
*card
, unsigned short reg
)
513 //ObtainSemaphore(&card->sb128_semaphore);
516 for (i
= 0; i
< 0x1000; i
++) {
517 if (!((pci_inl(SB128_CODEC
, card
)) & CODEC_WIP
))
520 DebugPrintF("SB128: Couldn't read from ac97! (1)\n");
521 // ReleaseSemaphore(&card->sb128_semaphore);
526 /* Get copy of SRC register when it's not busy. */
529 /* Enable "SRC State Data", an undocumented feature! */
530 pci_outl((r
& (SRC_DISABLE
| SRC_DIS_DAC1
| SRC_DIS_DAC2
| SRC_DIS_ADC
)) | 0x00010000, SB128_SRC
, card
);
532 /* Wait for "state 0", to avoid "transition states".
533 Seen in open code. */
534 for (i
= 0; i
< 0x1000; i
++)
536 if ((pci_inl(SB128_SRC
, card
) & 0x00870000) == 0x00)
541 /* Now wait for an undocumented bit to be set (and the SRC to be NOT busy) */
542 for (i
= 0; i
< 0x1000; i
++)
544 if ((pci_inl(SB128_SRC
, card
) & 0x00870000) == 0x00010000)
549 /* Write the read request to the chip now */
550 pci_outl((((reg
<< CODEC_ADD_SHIFT
) & CODEC_ADD_MASK
) | CODEC_READ
), SB128_CODEC
, card
);
552 /* Give the chip time to respond to our read request. */
555 /* Restore SRC register. */
557 pci_outl(r
, SB128_SRC
, card
);
560 for (i
= 0; i
< 0x1000; i
++) {
561 if (!((pci_inl(SB128_CODEC
, card
)) & CODEC_WIP
))
564 DebugPrintF("SB128: Couldn't read from ac97 (2)!\n");
565 // ReleaseSemaphore(&card->sb128_semaphore);
572 for (i
= 0; i
< 0x1000; i
++) {
573 if (!((pci_inl(SB128_CODEC
, card
)) & CODEC_RDY
))
576 DebugPrintF("SB128: Couldn't read from ac97 (3)!\n");
577 // ReleaseSemaphore(&card->sb128_semaphore);
582 Delay(1); //A delay here is crucial, remove this if you use micro_delay()
583 val
= pci_inl(SB128_CODEC
, card
);
585 // ReleaseSemaphore(&card->sb128_semaphore);
590 void rate_set_adc(struct SB128_DATA
*card
, unsigned long rate
)
592 unsigned long n
, truncm
, freq
;
594 //ObtainSemaphore(&card->sb128_semaphore);
603 pci_outl(((pci_inl(SB128_CONTROL
, card
) & ~DAC2_DIV_MASK
) | (DAC2_SRTODIV(rate
) << DAC2_DIV_SHIFT
)), SB128_CONTROL
, card
);
608 /* This is completely undocumented */
610 if ((1 << n
) & ((1 << 15) | (1 << 13) | (1 << 11) | (1 << 9)))
612 truncm
= (21 * n
- 1) | 1;
613 freq
= ((48000UL << 15) / rate
) * n
;
619 src_write(card
, SRC_ADC
+ SRC_TRUNC
, (((239 - truncm
) >> 1) << 9) | (n
<< 4));
625 src_write(card
, SRC_ADC
+ SRC_TRUNC
, 0x8000 | (((119 - truncm
) >> 1) << 9) | (n
<< 4));
627 src_write(card
, SRC_ADC
+ SRC_INT
,
628 (src_read(card
, SRC_ADC
+ SRC_INT
) & 0x00FF) | ((freq
>> 5) & 0xFC00));
629 src_write(card
, SRC_ADC
+ SRC_VF
, freq
& 0x7FFF);
630 src_write(card
, SRC_VOL_ADC
, n
<< 8);
631 src_write(card
, SRC_VOL_ADC
+ 1, n
<< 8);
635 //ReleaseSemaphore(&card->sb128_semaphore);
639 void rate_set_dac2(struct SB128_DATA
*card
, unsigned long rate
)
641 unsigned long freq
, r
;
643 //ObtainSemaphore(&card->sb128_semaphore);
652 pci_outl(((pci_inl(SB128_CONTROL
, card
) & ~DAC2_DIV_MASK
) | (DAC2_SRTODIV(rate
) << DAC2_DIV_SHIFT
)), SB128_CONTROL
, card
);
657 freq
= ((rate
<< 15 ) + 1500) / 3000;
659 /* Get copy of SRC register when it's not busy, clear, preserve the disable bits, write back. */
660 r
= src_ready(card
) & (SRC_DISABLE
| SRC_DIS_DAC1
| SRC_DIS_DAC2
| SRC_DIS_ADC
);
661 pci_outl(r
, SB128_SRC
, card
);
663 /* This is completely undocumented */
664 src_write(card
, SRC_DAC2
+ SRC_INT
,
665 (src_read(card
, SRC_DAC2
+ SRC_INT
) & 0x00FF) | ((freq
>> 5) & 0xFC00));
666 src_write(card
, SRC_DAC2
+ SRC_VF
, freq
& 0x7FFF);
667 r
= (src_ready(card
) & (SRC_DISABLE
| SRC_DIS_DAC1
| SRC_DIS_ADC
));
668 pci_outl(r
, SB128_SRC
, card
);
672 //ReleaseSemaphore(&card->sb128_semaphore);
676 /******************************************************************************
677 ** DriverData allocation ******************************************************
678 ******************************************************************************/
680 /* This code used to be in _AHIsub_AllocAudio(), but since we're now
681 handling CAMD support too, it needs to be done at driver loading
685 AllocDriverData( struct PCIDevice
* dev
,
686 struct DriverBase
* AHIsubBase
)
688 struct SB128_DATA
* card
;
691 D(bug("[SB128]: %s()\n", __PRETTY_FUNCTION__
);)
693 // FIXME: This should be non-cachable, DMA-able memory
694 card
= AllocVec( sizeof( *card
), MEMF_PUBLIC
| MEMF_CLEAR
);
698 Req( "Unable to allocate driver structure." );
702 card
->ahisubbase
= AHIsubBase
;
704 card
->interrupt
.is_Node
.ln_Type
= IRQTYPE
;
705 card
->interrupt
.is_Node
.ln_Pri
= 0;
706 card
->interrupt
.is_Node
.ln_Name
= (STRPTR
) LibName
;
708 card
->interrupt
.is_Code
= (void(*)(void))&cardinterrupt
;
710 card
->interrupt
.is_Code
= (void(*)(void))CardInterrupt
;
712 card
->interrupt
.is_Data
= (APTR
) card
;
714 card
->playback_interrupt
.is_Node
.ln_Type
= IRQTYPE
;
715 card
->playback_interrupt
.is_Node
.ln_Pri
= 0;
716 card
->playback_interrupt
.is_Node
.ln_Name
= (STRPTR
) LibName
;
718 card
->playback_interrupt
.is_Code
= (void(*)(void))&playbackinterrupt
;
720 card
->playback_interrupt
.is_Code
= (void(*)(void))PlaybackInterrupt
;
722 card
->playback_interrupt
.is_Data
= (APTR
) card
;
724 card
->record_interrupt
.is_Node
.ln_Type
= IRQTYPE
;
725 card
->record_interrupt
.is_Node
.ln_Pri
= 0;
726 card
->record_interrupt
.is_Node
.ln_Name
= (STRPTR
) LibName
;
728 card
->record_interrupt
.is_Code
= (void(*)(void))&recordinterrupt
;
730 card
->record_interrupt
.is_Code
= (void(*)(void))RecordInterrupt
;
732 card
->record_interrupt
.is_Data
= (APTR
) card
;
736 command_word
= inw_config( PCI_COMMAND
, dev
);
737 command_word
|= PCI_COMMAND_IO
| PCI_COMMAND_MEMORY
| PCI_COMMAND_MASTER
;
738 outw_config( PCI_COMMAND
, command_word
, dev
);
740 card
->pci_master_enabled
= TRUE
;
742 card
->iobase
= ahi_pci_get_base_address(0, dev
);
743 card
->length
= ~( ahi_pci_get_base_size(0, dev
) & PCI_BASE_ADDRESS_IO_MASK
);
744 card
->irq
= ahi_pci_get_irq(dev
);
745 card
->chiprev
= inb_config(PCI_REVISION_ID
, dev
);
746 card
->model
= inw_config(PCI_SUBSYSTEM_ID
, dev
);
748 D(bug("[SB128]: %s: iobase = 0x%p, len = %d\n", __PRETTY_FUNCTION__
, card
->iobase
, card
->length
);)
750 /* Initialise hardware access Semaphore */
751 InitSemaphore(&card
->sb128_semaphore
);
754 /* Initialize chip */
755 if( card_init( card
) < 0 )
757 DebugPrintF("SB128: Unable to initialize Card subsystem.");
761 ahi_pci_add_intserver(&card
->interrupt
, dev
);
762 card
->interrupt_added
= TRUE
;
765 card
->card_initialized
= TRUE
;
768 card
->monitor_volume
= Linear2MixerGain( 0, &card
->monitor_volume_bits
);
769 card
->input_gain
= Linear2RecordGain( 0x10000, &card
->input_gain_bits
);
770 card
->output_volume
= Linear2MixerGain( 0x10000, &card
->output_volume_bits
);
771 SaveMixerState(card
);
773 #if !defined(__AROS__)
774 AddResetHandler(card
);
781 /******************************************************************************
782 ** DriverData deallocation ****************************************************
783 ******************************************************************************/
785 /* And this code used to be in _AHIsub_FreeAudio(). */
788 FreeDriverData( struct SB128_DATA
* card
,
789 struct DriverBase
* AHIsubBase
)
793 if( card
->pci_dev
!= NULL
)
795 if( card
->card_initialized
)
797 card_cleanup( card
);
800 if( card
->pci_master_enabled
)
804 cmd
= inw_config(PCI_COMMAND
, (struct PCIDevice
* ) card
->pci_dev
);
805 cmd
&= ~( PCI_COMMAND_IO
| PCI_COMMAND_MEMORY
| PCI_COMMAND_MASTER
);
806 outw_config(PCI_COMMAND
, cmd
, (struct PCIDevice
* ) card
->pci_dev
);
810 if( card
->interrupt_added
)
812 ahi_pci_rem_intserver(&card
->interrupt
, card
->pci_dev
);
820 int card_init(struct SB128_DATA
*card
)
822 struct PCIDevice
*dev
= (struct PCIDevice
*) card
->pci_dev
;
826 /* Check if the card is an original ES1370 - different code needed */
827 if (inw_config(2, dev
) == 0x5000)
830 card
->es1370
= FALSE
;
832 /* Different init sequence for the ES1370 */
835 /* Enable CODEC access, set DAC sample rate to 44100 */
836 pci_outl(CTRL_CDC_EN
| (DAC2_SRTODIV(44100) << DAC2_DIV_SHIFT
), SB128_CONTROL
, card
);
837 pci_outl(0x00, SB128_SCON
, card
);
838 DebugPrintF("SB128: Did RATE init\n");
840 /* CODEC initialisation */
841 codec_write(card
, AK4531_RESET
, 0x03); /* Enable CODEC */
842 codec_write(card
, AK4531_CLOCK_SEL
, 0x00); /* CODEC ADC and DAC use PLL */
843 codec_write(card
, AK4531_RECORD_SELECT
, 0x00); /* CODEC ADC set to use mixer for input */
844 codec_write(card
, AK4531_RECORD_GAIN_MIC
, 0x00); /* Mic gain set to 0 dB */
846 /* Volume initialisation */
847 codec_write(card
, AK4531_MASTER_VOL_L
, 0x00); /* No attentuation */
848 codec_write(card
, AK4531_MASTER_VOL_R
, 0x00);
849 codec_write(card
, AK4531_MASTER_VOL_MONO
, 0x00);
851 /* Analogue mixer input gain registers */
852 codec_write(card
, AK4531_PHONE_VOL_L
, AK4531_MUTE
| 0x06);
853 codec_write(card
, AK4531_PHONE_VOL_R
, AK4531_MUTE
| 0x06);
854 codec_write(card
, AK4531_MIC_VOL
, AK4531_MUTE
| 0x06);
855 codec_write(card
, AK4531_LINEIN_VOL_L
, AK4531_MUTE
| 0x06);
856 codec_write(card
, AK4531_LINEIN_VOL_R
, AK4531_MUTE
| 0x06);
857 codec_write(card
, AK4531_CD_VOL_L
, 0x06);
858 codec_write(card
, AK4531_CD_VOL_R
, 0x06);
859 codec_write(card
, AK4531_AUX_VOL_L
, 0x06);
860 codec_write(card
, AK4531_AUX_VOL_R
, 0x06);
861 codec_write(card
, AK4531_PCMOUT_VOL_L
, 0x06);
862 codec_write(card
, AK4531_PCMOUT_VOL_R
, 0x06);
864 /* Mixer registers */
867 codec_write(card
, AK4531_OUTPUT_MUX_1
, 0x1F);
868 codec_write(card
, AK4531_OUTPUT_MUX_2
, 0x3F);
870 /* Store value of OUTPUT MUX registers */
871 card
->ak4531_output_1
= 0x1F;
872 card
->ak4531_output_2
= 0x3F;
874 /* Analogous to "Record Select", only TMIC and Phone enabled here */
875 codec_write(card
, AK4531_INPUT_MUX_L_1
, 0x00);
876 codec_write(card
, AK4531_INPUT_MUX_R_1
, 0x00);
877 codec_write(card
, AK4531_INPUT_MUX_L_2
, 0x80);
878 codec_write(card
, AK4531_INPUT_MUX_R_2
, 0x80);
880 DebugPrintF("SB128: Did VOLUME init\n");
884 /* Basic clear of everything */
885 pci_outl(0x00, SB128_CONTROL
, card
);
886 pci_outl(0x00, SB128_SCON
, card
);
887 pci_outl(0x00, SB128_LEGACY
, card
);
889 /* Magical CT5880 AC97 enable bit plus 20ms delay
890 (Gotta love the undocumented stuff) */
891 pci_outl(0x20000000, SB128_STATUS
, card
);
894 /* Assert the AC97 reset, and wait 20ms */
895 pci_outl(CODEC_RESET
, SB128_CONTROL
, card
);
897 /* De-assert delay, and wait 20ms */
898 pci_outl(0x00, SB128_CONTROL
, card
);
901 DebugPrintF("SB128: Did AC97 reset.\n");
903 /* Disable the Sample Rate Converter (SRC) */
905 pci_outl(SRC_DISABLE
, SB128_SRC
, card
);
906 /* Clear the SRC RAM */
907 for (i
= 0; i
< 0x80; i
++)
908 src_write(card
, i
, 0);
910 DebugPrintF("SB128: Did SRC wipe.\n");
912 /* Perform basic configuration of the SRC, not well documented! */
913 src_write(card
, SRC_DAC1
+ SRC_TRUNC
, 0x100);
914 src_write(card
, SRC_DAC1
+ SRC_INT
, 0x4000);
915 src_write(card
, SRC_DAC2
+ SRC_TRUNC
, 0x100);
916 src_write(card
, SRC_DAC2
+ SRC_INT
, 0x4000);
917 src_write(card
, SRC_VOL_ADC
, 0x1000);
918 src_write(card
, SRC_VOL_ADC
+ 1, 0x1000);
919 src_write(card
, SRC_VOL_DAC1
, 0x1000);
920 src_write(card
, SRC_VOL_DAC1
+ 1, 0x1000);
921 src_write(card
, SRC_VOL_DAC2
, 0x1000);
922 src_write(card
, SRC_VOL_DAC2
+ 1, 0x1000);
924 DebugPrintF("SB128: Did SRC init.\n");
926 rate_set_adc(card
, 44100);
927 rate_set_dac2(card
, 44100);
929 /* Re-enable the SRC */
931 pci_outl(0, SB128_SRC
, card
);
933 card
->currentPlayFreq
= 9;
934 card
->currentRecFreq
= 9;
936 DebugPrintF("SB128: Did RATE init.\n");
938 /* Initialise registers of AC97 to default */
939 codec_write(card
, AC97_RESET
, 0x00);
941 /* Initialise volumes of AC97 */
942 codec_write(card
, AC97_MASTER_VOL_STEREO
, 0x0000 ); /* no attenuation */
943 codec_write(card
, AC97_AUXOUT_VOL
, 0x0000 ); /* volume of the rear output */
944 codec_write(card
, AC97_MASTER_VOL_MONO
, 0x0000 );
945 codec_write(card
, AC97_MASTER_TONE
, 0x0f0f ); /* bass/treble control (if present) */
947 codec_write(card
, AC97_RECORD_SELECT
, 0);
948 codec_write(card
, AC97_RECORD_GAIN
, 0x0000 ); /* 0 dB gain */
950 /* Analogue mixer input gain registers */
951 codec_write(card
, AC97_PHONE_VOL
, AC97_MUTE
| 0x0008 );
952 codec_write(card
, AC97_MIC_VOL
, AC97_MUTE
| 0x0008 );
953 codec_write(card
, AC97_LINEIN_VOL
, AC97_MUTE
| 0x0808 );
954 codec_write(card
, AC97_CD_VOL
, 0x0808 );
955 codec_write(card
, AC97_VIDEO_VOL
, AC97_MUTE
| 0x0808 );
956 codec_write(card
, AC97_AUX_VOL
, 0x0808 );
957 codec_write(card
, AC97_PCMOUT_VOL
, 0x0808 );
959 DebugPrintF("SB128: Did VOLUME init.\n");
961 cod
= codec_read(card
, AC97_RESET
);
962 DebugPrintF("SB128: AC97 capabilities = %x\n", cod
);
964 cod
= codec_read(card
, AC97_VENDOR_ID0
);
965 DebugPrintF("SB128: AC97_VENDOR_ID0 = %x\n", cod
);
967 cod
= codec_read(card
, AC97_VENDOR_ID1
);
968 DebugPrintF("SB128: AC97_VENDOR_ID1 = %x\n", cod
);
975 void card_cleanup(struct SB128_DATA
*card
)
981 /******************************************************************************
982 ** Misc. **********************************************************************
983 ******************************************************************************/
986 SaveMixerState( struct SB128_DATA
* card
)
988 card
->ac97_mic
= codec_read( card
, AC97_MIC_VOL
);
989 card
->ac97_cd
= codec_read( card
, AC97_CD_VOL
);
990 card
->ac97_aux
= codec_read( card
, AC97_AUX_VOL
);
991 card
->ac97_linein
= codec_read( card
, AC97_LINEIN_VOL
);
992 card
->ac97_phone
= codec_read( card
, AC97_PHONE_VOL
);
997 RestoreMixerState( struct SB128_DATA
* card
)
1001 /* Not possible to save the state, so restore all volumes to mid levels */
1002 ak4531_ac97_write(card
, AC97_MIC_VOL
, 0x0808);
1003 ak4531_ac97_write(card
, AC97_CD_VOL
, 0x0808);
1004 ak4531_ac97_write(card
, AC97_AUX_VOL
, 0x0808);
1005 ak4531_ac97_write(card
, AC97_LINEIN_VOL
, 0x0808);
1006 ak4531_ac97_write(card
, AC97_PHONE_VOL
, 0x0808);
1010 codec_write(card
, AC97_MIC_VOL
, card
->ac97_mic
);
1011 codec_write(card
, AC97_CD_VOL
, card
->ac97_cd
);
1012 codec_write(card
, AC97_AUX_VOL
, card
->ac97_aux
);
1013 codec_write(card
, AC97_LINEIN_VOL
, card
->ac97_linein
);
1014 codec_write(card
, AC97_PHONE_VOL
, card
->ac97_phone
);
1019 UpdateMonitorMixer( struct SB128_DATA
* card
)
1021 int i
= InputBits
[ card
->input
];
1022 UWORD m
= card
->monitor_volume_bits
& 0x801f;
1023 UWORD s
= card
->monitor_volume_bits
;
1024 UWORD mm
= AC97_MUTE
| 0x0008;
1025 UWORD sm
= AC97_MUTE
| 0x0808;
1027 if( i
== AC97_RECMUX_STEREO_MIX
)
1029 /* Use the original mixer settings */
1030 RestoreMixerState( card
);
1036 ak4531_ac97_write(card
, AC97_MIC_VOL
,
1037 i
== AC97_RECMUX_MIC
? m
: mm
);
1039 ak4531_ac97_write(card
, AC97_CD_VOL
,
1040 i
== AC97_RECMUX_CD
? s
: sm
);
1042 ak4531_ac97_write(card
, AC97_AUX_VOL
,
1043 i
== AC97_RECMUX_AUX
? s
: sm
);
1045 ak4531_ac97_write(card
, AC97_LINEIN_VOL
,
1046 i
== AC97_RECMUX_LINE
? s
: sm
);
1048 ak4531_ac97_write(card
, AC97_PHONE_VOL
,
1049 i
== AC97_RECMUX_PHONE
? m
: mm
);
1053 codec_write(card
, AC97_MIC_VOL
,
1054 i
== AC97_RECMUX_MIC
? m
: mm
);
1056 codec_write(card
, AC97_CD_VOL
,
1057 i
== AC97_RECMUX_CD
? s
: sm
);
1059 codec_write(card
, AC97_AUX_VOL
,
1060 i
== AC97_RECMUX_AUX
? s
: sm
);
1062 codec_write(card
, AC97_LINEIN_VOL
,
1063 i
== AC97_RECMUX_LINE
? s
: sm
);
1065 codec_write(card
, AC97_PHONE_VOL
,
1066 i
== AC97_RECMUX_PHONE
? m
: mm
);
1073 Linear2MixerGain( Fixed linear
,
1076 static const Fixed gain
[ 33 ] =
1078 260904, /* +12.0 dB */
1079 219523, /* +10.5 dB */
1080 184706, /* +9.0 dB */
1081 155410, /* +7.5 dB */
1082 130762, /* +6.0 dB */
1083 110022, /* +4.5 dB */
1084 92572, /* +3.0 dB */
1085 77890, /* +1.5 dB */
1086 65536, /* ±0.0 dB */
1087 55142, /* -1.5 dB */
1088 46396, /* -3.0 dB */
1089 39037, /* -4.5 dB */
1090 32846, /* -6.0 dB */
1091 27636, /* -7.5 dB */
1092 23253, /* -9.0 dB */
1093 19565, /* -10.5 dB */
1094 16462, /* -12.0 dB */
1095 13851, /* -13.5 dB */
1096 11654, /* -15.0 dB */
1097 9806, /* -16.5 dB */
1098 8250, /* -18.0 dB */
1099 6942, /* -19.5 dB */
1100 5841, /* -21.0 dB */
1101 4915, /* -22.5 dB */
1102 4135, /* -24.0 dB */
1103 3479, /* -25.5 dB */
1104 2927, /* -27.0 dB */
1105 2463, /* -28.5 dB */
1106 2072, /* -30.0 dB */
1107 1744, /* -31.5 dB */
1108 1467, /* -33.0 dB */
1109 1234, /* -34.5 dB */
1114 while( linear
< gain
[ v
] )
1121 *bits
= 0x8000; /* Mute */
1125 *bits
= ( v
<< 8 ) | v
;
1131 Linear2RecordGain( Fixed linear
,
1134 static const Fixed gain
[ 17 ] =
1136 873937, /* +22.5 dB */
1137 735326, /* +21.0 dB */
1138 618700, /* +19.5 dB */
1139 520571, /* +18.0 dB */
1140 438006, /* +16.5 dB */
1141 368536, /* +15.0 dB */
1142 310084, /* +13.5 dB */
1143 260904, /* +12.0 dB */
1144 219523, /* +10.5 dB */
1145 184706, /* +9.0 dB */
1146 155410, /* +7.5 dB */
1147 130762, /* +6.0 dB */
1148 110022, /* +4.5 dB */
1149 92572, /* +3.0 dB */
1150 77890, /* +1.5 dB */
1151 65536, /* ±0.0 dB */
1157 while( linear
< gain
[ v
] )
1164 *bits
= 0x8000; /* Mute */
1168 *bits
= ( ( 15 - v
) << 8 ) | ( 15 - v
);
1176 SamplerateToLinearPitch( ULONG samplingrate
)
1178 samplingrate
= (samplingrate
<< 8) / 375;
1179 return (samplingrate
>> 1) + (samplingrate
& 1);
1183 void *pci_alloc_consistent(size_t size
, APTR
*NonAlignedAddress
, unsigned int boundary
)
1188 D(bug("[SB128]: %s()\n", __PRETTY_FUNCTION__
);)
1190 address
= (void *) AllocVec(size
+ boundary
, (
1191 #if defined(__AROS__) && (__WORDSIZE==64)
1194 MEMF_PUBLIC
| MEMF_CLEAR
));
1196 if (address
!= NULL
)
1198 a
= (unsigned long) address
;
1199 a
= (a
+ boundary
- 1) & ~(boundary
- 1);
1200 address
= (void *) a
;
1203 if (NonAlignedAddress
)
1205 *NonAlignedAddress
= address
;
1212 void pci_free_consistent(void* addr
)
1214 D(bug("[SB128]: %s()\n", __PRETTY_FUNCTION__
);)
1219 #if !defined(__AROS__)
1220 static ULONG
ResetHandler(struct ExceptionContext
*ctx
, struct ExecBase
*pExecBase
, struct SB128_DATA
*card
)
1222 struct PCIDevice
*dev
= card
->pci_dev
;
1224 //Stop SB128 interrupts and playback/recording
1227 ctrl
= pci_inl(SB128_CONTROL
, card
);
1228 ctrl
&= ( ~(CTRL_DAC2_EN
) & ~(CTRL_ADC_EN
) );
1231 pci_outl(ctrl
, SB128_CONTROL
, card
);
1233 /* Clear and mask interrupts */
1234 pci_outl((pci_inl(SB128_SCON
, card
) & SB128_IRQ_MASK
), SB128_SCON
, card
);
1239 void AddResetHandler(struct SB128_DATA
*card
)
1241 static struct Interrupt interrupt
;
1243 interrupt
.is_Code
= (void (*)())ResetHandler
;
1244 interrupt
.is_Data
= (APTR
) card
;
1245 interrupt
.is_Node
.ln_Pri
= 0;
1246 interrupt
.is_Node
.ln_Type
= NT_EXTINTERRUPT
;
1247 interrupt
.is_Node
.ln_Name
= "SB128 Reset Handler";
1249 AddResetCallback( &interrupt
);