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
))
452 DebugPrintF("[SB128] Couldn't write to ac97! (1)\n");
453 //ReleaseSemaphore(&card->sb128_semaphore);
457 /* Get copy of SRC register when it's not busy. */
459 /* Enable "SRC State Data", an undocumented feature! */
460 pci_outl((r
& (SRC_DISABLE
| SRC_DIS_DAC2
| SRC_DIS_ADC
)) | 0x00010000, SB128_SRC
, card
);
462 /* Wait for "state 0", to avoid "transition states". */
463 for (i
= 0; i
< 0x1000; i
++)
465 if ((pci_inl(SB128_SRC
, card
) & 0x00870000) == 0x00)
470 /* Now wait for an undocumented bit to be set (and the SRC to be NOT busy) */
471 for (i
= 0; i
< 0x1000; i
++)
473 if ((pci_inl(SB128_SRC
, card
) & 0x00870000) == 0x00010000)
478 /* Write out the value to the codec now. */
479 pci_outl((((reg
<< CODEC_ADD_SHIFT
) & CODEC_ADD_MASK
) | val
), SB128_CODEC
, card
);
481 /* Delay to make sure the chip had time to set the WIP after
485 /* Restore SRC register. */
487 pci_outl(r
, SB128_SRC
, card
);
489 /* Check for WIP before returning. */
490 for (i
= 0; i
< 0x1000; i
++)
492 if (!(pci_inl(SB128_CODEC
, card
) & CODEC_WIP
))
494 //ReleaseSemaphore(&card->sb128_semaphore);
500 DebugPrintF("[SB128] Couldn't write to ac97! (2)\n");
503 //ReleaseSemaphore(&card->sb128_semaphore);
507 unsigned short codec_read(struct SB128_DATA
*card
, unsigned short reg
)
515 //ObtainSemaphore(&card->sb128_semaphore);
518 for (i
= 0; i
< 0x1000; i
++) {
519 if (!((pci_inl(SB128_CODEC
, card
)) & CODEC_WIP
))
523 DebugPrintF("[SB128] Couldn't read from ac97! (1)\n");
524 // ReleaseSemaphore(&card->sb128_semaphore);
529 /* Get copy of SRC register when it's not busy. */
532 /* Enable "SRC State Data", an undocumented feature! */
533 pci_outl((r
& (SRC_DISABLE
| SRC_DIS_DAC1
| SRC_DIS_DAC2
| SRC_DIS_ADC
)) | 0x00010000, SB128_SRC
, card
);
535 /* Wait for "state 0", to avoid "transition states".
536 Seen in open code. */
537 for (i
= 0; i
< 0x1000; i
++)
539 if ((pci_inl(SB128_SRC
, card
) & 0x00870000) == 0x00)
544 /* Now wait for an undocumented bit to be set (and the SRC to be NOT busy) */
545 for (i
= 0; i
< 0x1000; i
++)
547 if ((pci_inl(SB128_SRC
, card
) & 0x00870000) == 0x00010000)
552 /* Write the read request to the chip now */
553 pci_outl((((reg
<< CODEC_ADD_SHIFT
) & CODEC_ADD_MASK
) | CODEC_READ
), SB128_CODEC
, card
);
555 /* Give the chip time to respond to our read request. */
558 /* Restore SRC register. */
560 pci_outl(r
, SB128_SRC
, card
);
563 for (i
= 0; i
< 0x1000; i
++) {
564 if (!((pci_inl(SB128_CODEC
, card
)) & CODEC_WIP
))
568 DebugPrintF("[SB128] Couldn't read from ac97 (2)!\n");
569 // ReleaseSemaphore(&card->sb128_semaphore);
576 for (i
= 0; i
< 0x1000; i
++) {
577 if (!((pci_inl(SB128_CODEC
, card
)) & CODEC_RDY
))
581 DebugPrintF("[SB128] Couldn't read from ac97 (3)!\n");
582 // ReleaseSemaphore(&card->sb128_semaphore);
587 Delay(1); //A delay here is crucial, remove this if you use micro_delay()
588 val
= pci_inl(SB128_CODEC
, card
);
590 // ReleaseSemaphore(&card->sb128_semaphore);
595 void rate_set_adc(struct SB128_DATA
*card
, unsigned long rate
)
597 unsigned long n
, truncm
, freq
;
599 //ObtainSemaphore(&card->sb128_semaphore);
608 pci_outl(((pci_inl(SB128_CONTROL
, card
) & ~DAC2_DIV_MASK
) | (DAC2_SRTODIV(rate
) << DAC2_DIV_SHIFT
)), SB128_CONTROL
, card
);
613 /* This is completely undocumented */
615 if ((1 << n
) & ((1 << 15) | (1 << 13) | (1 << 11) | (1 << 9)))
617 truncm
= (21 * n
- 1) | 1;
618 freq
= ((48000UL << 15) / rate
) * n
;
624 src_write(card
, SRC_ADC
+ SRC_TRUNC
, (((239 - truncm
) >> 1) << 9) | (n
<< 4));
630 src_write(card
, SRC_ADC
+ SRC_TRUNC
, 0x8000 | (((119 - truncm
) >> 1) << 9) | (n
<< 4));
632 src_write(card
, SRC_ADC
+ SRC_INT
,
633 (src_read(card
, SRC_ADC
+ SRC_INT
) & 0x00FF) | ((freq
>> 5) & 0xFC00));
634 src_write(card
, SRC_ADC
+ SRC_VF
, freq
& 0x7FFF);
635 src_write(card
, SRC_VOL_ADC
, n
<< 8);
636 src_write(card
, SRC_VOL_ADC
+ 1, n
<< 8);
640 //ReleaseSemaphore(&card->sb128_semaphore);
644 void rate_set_dac2(struct SB128_DATA
*card
, unsigned long rate
)
646 unsigned long freq
, r
;
648 //ObtainSemaphore(&card->sb128_semaphore);
657 pci_outl(((pci_inl(SB128_CONTROL
, card
) & ~DAC2_DIV_MASK
) | (DAC2_SRTODIV(rate
) << DAC2_DIV_SHIFT
)), SB128_CONTROL
, card
);
662 freq
= ((rate
<< 15 ) + 1500) / 3000;
664 /* Get copy of SRC register when it's not busy, clear, preserve the disable bits, write back. */
665 r
= src_ready(card
) & (SRC_DISABLE
| SRC_DIS_DAC1
| SRC_DIS_DAC2
| SRC_DIS_ADC
);
666 pci_outl(r
, SB128_SRC
, card
);
668 /* This is completely undocumented */
669 src_write(card
, SRC_DAC2
+ SRC_INT
,
670 (src_read(card
, SRC_DAC2
+ SRC_INT
) & 0x00FF) | ((freq
>> 5) & 0xFC00));
671 src_write(card
, SRC_DAC2
+ SRC_VF
, freq
& 0x7FFF);
672 r
= (src_ready(card
) & (SRC_DISABLE
| SRC_DIS_DAC1
| SRC_DIS_ADC
));
673 pci_outl(r
, SB128_SRC
, card
);
677 //ReleaseSemaphore(&card->sb128_semaphore);
681 /******************************************************************************
682 ** DriverData allocation ******************************************************
683 ******************************************************************************/
685 /* This code used to be in _AHIsub_AllocAudio(), but since we're now
686 handling CAMD support too, it needs to be done at driver loading
690 AllocDriverData( struct PCIDevice
* dev
,
691 struct DriverBase
* AHIsubBase
)
693 struct SB128_DATA
* card
;
696 D(bug("[SB128]: %s()\n", __PRETTY_FUNCTION__
);)
698 // FIXME: This should be non-cachable, DMA-able memory
699 card
= AllocVec( sizeof( *card
), MEMF_PUBLIC
| MEMF_CLEAR
);
703 Req( "Unable to allocate driver structure." );
707 card
->ahisubbase
= AHIsubBase
;
709 card
->interrupt
.is_Node
.ln_Type
= IRQTYPE
;
710 card
->interrupt
.is_Node
.ln_Pri
= 0;
711 card
->interrupt
.is_Node
.ln_Name
= (STRPTR
) LibName
;
713 card
->interrupt
.is_Code
= (void(*)(void))&cardinterrupt
;
715 card
->interrupt
.is_Code
= (void(*)(void))CardInterrupt
;
717 card
->interrupt
.is_Data
= (APTR
) card
;
719 card
->playback_interrupt
.is_Node
.ln_Type
= IRQTYPE
;
720 card
->playback_interrupt
.is_Node
.ln_Pri
= 0;
721 card
->playback_interrupt
.is_Node
.ln_Name
= (STRPTR
) LibName
;
723 card
->playback_interrupt
.is_Code
= (void(*)(void))&playbackinterrupt
;
725 card
->playback_interrupt
.is_Code
= (void(*)(void))PlaybackInterrupt
;
727 card
->playback_interrupt
.is_Data
= (APTR
) card
;
729 card
->record_interrupt
.is_Node
.ln_Type
= IRQTYPE
;
730 card
->record_interrupt
.is_Node
.ln_Pri
= 0;
731 card
->record_interrupt
.is_Node
.ln_Name
= (STRPTR
) LibName
;
733 card
->record_interrupt
.is_Code
= (void(*)(void))&recordinterrupt
;
735 card
->record_interrupt
.is_Code
= (void(*)(void))RecordInterrupt
;
737 card
->record_interrupt
.is_Data
= (APTR
) card
;
741 command_word
= inw_config( PCI_COMMAND
, dev
);
742 command_word
|= PCI_COMMAND_IO
| PCI_COMMAND_MEMORY
| PCI_COMMAND_MASTER
;
743 outw_config( PCI_COMMAND
, command_word
, dev
);
745 card
->pci_master_enabled
= TRUE
;
747 card
->iobase
= ahi_pci_get_base_address(0, dev
);
748 card
->length
= ~( ahi_pci_get_base_size(0, dev
) & PCI_BASE_ADDRESS_IO_MASK
);
749 card
->irq
= ahi_pci_get_irq(dev
);
750 card
->chiprev
= inb_config(PCI_REVISION_ID
, dev
);
751 card
->model
= inw_config(PCI_SUBSYSTEM_ID
, dev
);
753 D(bug("[SB128]: %s: iobase = 0x%p, len = %d\n", __PRETTY_FUNCTION__
, card
->iobase
, card
->length
);)
755 /* Initialise hardware access Semaphore */
756 InitSemaphore(&card
->sb128_semaphore
);
759 /* Initialize chip */
760 if( card_init( card
) < 0 )
762 DebugPrintF("[SB128] Unable to initialize Card subsystem.");
766 ahi_pci_add_intserver(&card
->interrupt
, dev
);
767 card
->interrupt_added
= TRUE
;
770 card
->card_initialized
= TRUE
;
773 card
->monitor_volume
= Linear2MixerGain( 0, &card
->monitor_volume_bits
);
774 card
->input_gain
= Linear2RecordGain( 0x10000, &card
->input_gain_bits
);
775 card
->output_volume
= Linear2MixerGain( 0x10000, &card
->output_volume_bits
);
776 SaveMixerState(card
);
778 #if !defined(__AROS__)
779 AddResetHandler(card
);
786 /******************************************************************************
787 ** DriverData deallocation ****************************************************
788 ******************************************************************************/
790 /* And this code used to be in _AHIsub_FreeAudio(). */
793 FreeDriverData( struct SB128_DATA
* card
,
794 struct DriverBase
* AHIsubBase
)
798 if( card
->pci_dev
!= NULL
)
800 if( card
->card_initialized
)
802 card_cleanup( card
);
805 if( card
->pci_master_enabled
)
809 cmd
= inw_config(PCI_COMMAND
, (struct PCIDevice
* ) card
->pci_dev
);
810 cmd
&= ~( PCI_COMMAND_IO
| PCI_COMMAND_MEMORY
| PCI_COMMAND_MASTER
);
811 outw_config(PCI_COMMAND
, cmd
, (struct PCIDevice
* ) card
->pci_dev
);
815 if( card
->interrupt_added
)
817 ahi_pci_rem_intserver(&card
->interrupt
, card
->pci_dev
);
825 int card_init(struct SB128_DATA
*card
)
827 struct PCIDevice
*dev
= (struct PCIDevice
*) card
->pci_dev
;
831 /* Check if the card is an original ES1370 - different code needed */
832 if (inw_config(2, dev
) == 0x5000)
835 card
->es1370
= FALSE
;
837 /* Different init sequence for the ES1370 */
840 /* Enable CODEC access, set DAC sample rate to 44100 */
841 pci_outl(CTRL_CDC_EN
| (DAC2_SRTODIV(44100) << DAC2_DIV_SHIFT
), SB128_CONTROL
, card
);
842 pci_outl(0x00, SB128_SCON
, card
);
843 DebugPrintF("[SB128] Did RATE init\n");
845 /* CODEC initialisation */
846 codec_write(card
, AK4531_RESET
, 0x03); /* Enable CODEC */
847 codec_write(card
, AK4531_CLOCK_SEL
, 0x00); /* CODEC ADC and DAC use PLL */
848 codec_write(card
, AK4531_RECORD_SELECT
, 0x00); /* CODEC ADC set to use mixer for input */
849 codec_write(card
, AK4531_RECORD_GAIN_MIC
, 0x00); /* Mic gain set to 0 dB */
851 /* Volume initialisation */
852 codec_write(card
, AK4531_MASTER_VOL_L
, 0x00); /* No attentuation */
853 codec_write(card
, AK4531_MASTER_VOL_R
, 0x00);
854 codec_write(card
, AK4531_MASTER_VOL_MONO
, 0x00);
856 /* Analogue mixer input gain registers */
857 codec_write(card
, AK4531_PHONE_VOL_L
, AK4531_MUTE
| 0x06);
858 codec_write(card
, AK4531_PHONE_VOL_R
, AK4531_MUTE
| 0x06);
859 codec_write(card
, AK4531_MIC_VOL
, AK4531_MUTE
| 0x06);
860 codec_write(card
, AK4531_LINEIN_VOL_L
, AK4531_MUTE
| 0x06);
861 codec_write(card
, AK4531_LINEIN_VOL_R
, AK4531_MUTE
| 0x06);
862 codec_write(card
, AK4531_CD_VOL_L
, 0x06);
863 codec_write(card
, AK4531_CD_VOL_R
, 0x06);
864 codec_write(card
, AK4531_AUX_VOL_L
, 0x06);
865 codec_write(card
, AK4531_AUX_VOL_R
, 0x06);
866 codec_write(card
, AK4531_PCMOUT_VOL_L
, 0x06);
867 codec_write(card
, AK4531_PCMOUT_VOL_R
, 0x06);
869 /* Mixer registers */
872 codec_write(card
, AK4531_OUTPUT_MUX_1
, 0x1F);
873 codec_write(card
, AK4531_OUTPUT_MUX_2
, 0x3F);
875 /* Store value of OUTPUT MUX registers */
876 card
->ak4531_output_1
= 0x1F;
877 card
->ak4531_output_2
= 0x3F;
879 /* Analogous to "Record Select", only TMIC and Phone enabled here */
880 codec_write(card
, AK4531_INPUT_MUX_L_1
, 0x00);
881 codec_write(card
, AK4531_INPUT_MUX_R_1
, 0x00);
882 codec_write(card
, AK4531_INPUT_MUX_L_2
, 0x80);
883 codec_write(card
, AK4531_INPUT_MUX_R_2
, 0x80);
885 DebugPrintF("[SB128] Did VOLUME init\n");
889 /* Basic clear of everything */
890 pci_outl(0x00, SB128_CONTROL
, card
);
891 pci_outl(0x00, SB128_SCON
, card
);
892 pci_outl(0x00, SB128_LEGACY
, card
);
894 /* Magical CT5880 AC97 enable bit plus 20ms delay
895 (Gotta love the undocumented stuff) */
896 pci_outl(0x20000000, SB128_STATUS
, card
);
899 /* Assert the AC97 reset, and wait 20ms */
900 pci_outl(CODEC_RESET
, SB128_CONTROL
, card
);
902 /* De-assert delay, and wait 20ms */
903 pci_outl(0x00, SB128_CONTROL
, card
);
906 DebugPrintF("[SB128] Did AC97 reset.\n");
908 /* Disable the Sample Rate Converter (SRC) */
910 pci_outl(SRC_DISABLE
, SB128_SRC
, card
);
911 /* Clear the SRC RAM */
912 for (i
= 0; i
< 0x80; i
++)
913 src_write(card
, i
, 0);
915 DebugPrintF("[SB128] Did SRC wipe.\n");
917 /* Perform basic configuration of the SRC, not well documented! */
918 src_write(card
, SRC_DAC1
+ SRC_TRUNC
, 0x100);
919 src_write(card
, SRC_DAC1
+ SRC_INT
, 0x4000);
920 src_write(card
, SRC_DAC2
+ SRC_TRUNC
, 0x100);
921 src_write(card
, SRC_DAC2
+ SRC_INT
, 0x4000);
922 src_write(card
, SRC_VOL_ADC
, 0x1000);
923 src_write(card
, SRC_VOL_ADC
+ 1, 0x1000);
924 src_write(card
, SRC_VOL_DAC1
, 0x1000);
925 src_write(card
, SRC_VOL_DAC1
+ 1, 0x1000);
926 src_write(card
, SRC_VOL_DAC2
, 0x1000);
927 src_write(card
, SRC_VOL_DAC2
+ 1, 0x1000);
929 DebugPrintF("[SB128] Did SRC init.\n");
931 rate_set_adc(card
, 44100);
932 rate_set_dac2(card
, 44100);
934 /* Re-enable the SRC */
936 pci_outl(0, SB128_SRC
, card
);
938 card
->currentPlayFreq
= 9;
939 card
->currentRecFreq
= 9;
941 DebugPrintF("[SB128] Did RATE init.\n");
943 /* Initialise registers of AC97 to default */
944 codec_write(card
, AC97_RESET
, 0x00);
946 /* Initialise volumes of AC97 */
947 codec_write(card
, AC97_MASTER_VOL_STEREO
, 0x0000 ); /* no attenuation */
948 codec_write(card
, AC97_AUXOUT_VOL
, 0x0000 ); /* volume of the rear output */
949 codec_write(card
, AC97_MASTER_VOL_MONO
, 0x0000 );
950 codec_write(card
, AC97_MASTER_TONE
, 0x0f0f ); /* bass/treble control (if present) */
952 codec_write(card
, AC97_RECORD_SELECT
, 0);
953 codec_write(card
, AC97_RECORD_GAIN
, 0x0000 ); /* 0 dB gain */
955 /* Analogue mixer input gain registers */
956 codec_write(card
, AC97_PHONE_VOL
, AC97_MUTE
| 0x0008 );
957 codec_write(card
, AC97_MIC_VOL
, AC97_MUTE
| 0x0008 );
958 codec_write(card
, AC97_LINEIN_VOL
, AC97_MUTE
| 0x0808 );
959 codec_write(card
, AC97_CD_VOL
, 0x0808 );
960 codec_write(card
, AC97_VIDEO_VOL
, AC97_MUTE
| 0x0808 );
961 codec_write(card
, AC97_AUX_VOL
, 0x0808 );
962 codec_write(card
, AC97_PCMOUT_VOL
, 0x0808 );
964 DebugPrintF("[SB128] Did VOLUME init.\n");
966 cod
= codec_read(card
, AC97_RESET
);
967 DebugPrintF("[SB128] AC97 capabilities = %x\n", cod
);
969 cod
= codec_read(card
, AC97_VENDOR_ID0
);
970 DebugPrintF("[SB128] AC97_VENDOR_ID0 = %x\n", cod
);
972 cod
= codec_read(card
, AC97_VENDOR_ID1
);
973 DebugPrintF("[SB128] AC97_VENDOR_ID1 = %x\n", cod
);
980 void card_cleanup(struct SB128_DATA
*card
)
986 /******************************************************************************
987 ** Misc. **********************************************************************
988 ******************************************************************************/
991 SaveMixerState( struct SB128_DATA
* card
)
993 card
->ac97_mic
= codec_read( card
, AC97_MIC_VOL
);
994 card
->ac97_cd
= codec_read( card
, AC97_CD_VOL
);
995 card
->ac97_aux
= codec_read( card
, AC97_AUX_VOL
);
996 card
->ac97_linein
= codec_read( card
, AC97_LINEIN_VOL
);
997 card
->ac97_phone
= codec_read( card
, AC97_PHONE_VOL
);
1002 RestoreMixerState( struct SB128_DATA
* card
)
1006 /* Not possible to save the state, so restore all volumes to mid levels */
1007 ak4531_ac97_write(card
, AC97_MIC_VOL
, 0x0808);
1008 ak4531_ac97_write(card
, AC97_CD_VOL
, 0x0808);
1009 ak4531_ac97_write(card
, AC97_AUX_VOL
, 0x0808);
1010 ak4531_ac97_write(card
, AC97_LINEIN_VOL
, 0x0808);
1011 ak4531_ac97_write(card
, AC97_PHONE_VOL
, 0x0808);
1015 codec_write(card
, AC97_MIC_VOL
, card
->ac97_mic
);
1016 codec_write(card
, AC97_CD_VOL
, card
->ac97_cd
);
1017 codec_write(card
, AC97_AUX_VOL
, card
->ac97_aux
);
1018 codec_write(card
, AC97_LINEIN_VOL
, card
->ac97_linein
);
1019 codec_write(card
, AC97_PHONE_VOL
, card
->ac97_phone
);
1024 UpdateMonitorMixer( struct SB128_DATA
* card
)
1026 int i
= InputBits
[ card
->input
];
1027 UWORD m
= card
->monitor_volume_bits
& 0x801f;
1028 UWORD s
= card
->monitor_volume_bits
;
1029 UWORD mm
= AC97_MUTE
| 0x0008;
1030 UWORD sm
= AC97_MUTE
| 0x0808;
1032 if( i
== AC97_RECMUX_STEREO_MIX
)
1034 /* Use the original mixer settings */
1035 RestoreMixerState( card
);
1041 ak4531_ac97_write(card
, AC97_MIC_VOL
,
1042 i
== AC97_RECMUX_MIC
? m
: mm
);
1044 ak4531_ac97_write(card
, AC97_CD_VOL
,
1045 i
== AC97_RECMUX_CD
? s
: sm
);
1047 ak4531_ac97_write(card
, AC97_AUX_VOL
,
1048 i
== AC97_RECMUX_AUX
? s
: sm
);
1050 ak4531_ac97_write(card
, AC97_LINEIN_VOL
,
1051 i
== AC97_RECMUX_LINE
? s
: sm
);
1053 ak4531_ac97_write(card
, AC97_PHONE_VOL
,
1054 i
== AC97_RECMUX_PHONE
? m
: mm
);
1058 codec_write(card
, AC97_MIC_VOL
,
1059 i
== AC97_RECMUX_MIC
? m
: mm
);
1061 codec_write(card
, AC97_CD_VOL
,
1062 i
== AC97_RECMUX_CD
? s
: sm
);
1064 codec_write(card
, AC97_AUX_VOL
,
1065 i
== AC97_RECMUX_AUX
? s
: sm
);
1067 codec_write(card
, AC97_LINEIN_VOL
,
1068 i
== AC97_RECMUX_LINE
? s
: sm
);
1070 codec_write(card
, AC97_PHONE_VOL
,
1071 i
== AC97_RECMUX_PHONE
? m
: mm
);
1078 Linear2MixerGain( Fixed linear
,
1081 static const Fixed gain
[ 33 ] =
1083 260904, /* +12.0 dB */
1084 219523, /* +10.5 dB */
1085 184706, /* +9.0 dB */
1086 155410, /* +7.5 dB */
1087 130762, /* +6.0 dB */
1088 110022, /* +4.5 dB */
1089 92572, /* +3.0 dB */
1090 77890, /* +1.5 dB */
1091 65536, /* ±0.0 dB */
1092 55142, /* -1.5 dB */
1093 46396, /* -3.0 dB */
1094 39037, /* -4.5 dB */
1095 32846, /* -6.0 dB */
1096 27636, /* -7.5 dB */
1097 23253, /* -9.0 dB */
1098 19565, /* -10.5 dB */
1099 16462, /* -12.0 dB */
1100 13851, /* -13.5 dB */
1101 11654, /* -15.0 dB */
1102 9806, /* -16.5 dB */
1103 8250, /* -18.0 dB */
1104 6942, /* -19.5 dB */
1105 5841, /* -21.0 dB */
1106 4915, /* -22.5 dB */
1107 4135, /* -24.0 dB */
1108 3479, /* -25.5 dB */
1109 2927, /* -27.0 dB */
1110 2463, /* -28.5 dB */
1111 2072, /* -30.0 dB */
1112 1744, /* -31.5 dB */
1113 1467, /* -33.0 dB */
1114 1234, /* -34.5 dB */
1119 while( linear
< gain
[ v
] )
1126 *bits
= 0x8000; /* Mute */
1130 *bits
= ( v
<< 8 ) | v
;
1136 Linear2RecordGain( Fixed linear
,
1139 static const Fixed gain
[ 17 ] =
1141 873937, /* +22.5 dB */
1142 735326, /* +21.0 dB */
1143 618700, /* +19.5 dB */
1144 520571, /* +18.0 dB */
1145 438006, /* +16.5 dB */
1146 368536, /* +15.0 dB */
1147 310084, /* +13.5 dB */
1148 260904, /* +12.0 dB */
1149 219523, /* +10.5 dB */
1150 184706, /* +9.0 dB */
1151 155410, /* +7.5 dB */
1152 130762, /* +6.0 dB */
1153 110022, /* +4.5 dB */
1154 92572, /* +3.0 dB */
1155 77890, /* +1.5 dB */
1156 65536, /* ±0.0 dB */
1162 while( linear
< gain
[ v
] )
1169 *bits
= 0x8000; /* Mute */
1173 *bits
= ( ( 15 - v
) << 8 ) | ( 15 - v
);
1181 SamplerateToLinearPitch( ULONG samplingrate
)
1183 samplingrate
= (samplingrate
<< 8) / 375;
1184 return (samplingrate
>> 1) + (samplingrate
& 1);
1188 void *pci_alloc_consistent(size_t size
, APTR
*NonAlignedAddress
, unsigned int boundary
)
1193 D(bug("[SB128]: %s()\n", __PRETTY_FUNCTION__
);)
1195 address
= (void *) AllocVec(size
+ boundary
, (
1196 #if defined(__AROS__) && (__WORDSIZE==64)
1199 MEMF_PUBLIC
| MEMF_CLEAR
));
1201 if (address
!= NULL
)
1203 a
= (unsigned long) address
;
1204 a
= (a
+ boundary
- 1) & ~(boundary
- 1);
1205 address
= (void *) a
;
1208 if (NonAlignedAddress
)
1210 *NonAlignedAddress
= address
;
1217 void pci_free_consistent(void* addr
)
1219 D(bug("[SB128]: %s()\n", __PRETTY_FUNCTION__
);)
1224 #if !defined(__AROS__)
1225 static ULONG
ResetHandler(struct ExceptionContext
*ctx
, struct ExecBase
*pExecBase
, struct SB128_DATA
*card
)
1227 struct PCIDevice
*dev
= card
->pci_dev
;
1229 //Stop SB128 interrupts and playback/recording
1232 ctrl
= pci_inl(SB128_CONTROL
, card
);
1233 ctrl
&= ( ~(CTRL_DAC2_EN
) & ~(CTRL_ADC_EN
) );
1236 pci_outl(ctrl
, SB128_CONTROL
, card
);
1238 /* Clear and mask interrupts */
1239 pci_outl((pci_inl(SB128_SCON
, card
) & SB128_IRQ_MASK
), SB128_SCON
, card
);
1244 void AddResetHandler(struct SB128_DATA
*card
)
1246 static struct Interrupt interrupt
;
1248 interrupt
.is_Code
= (void (*)())ResetHandler
;
1249 interrupt
.is_Data
= (APTR
) card
;
1250 interrupt
.is_Node
.ln_Pri
= 0;
1251 interrupt
.is_Node
.ln_Type
= NT_EXTINTERRUPT
;
1252 interrupt
.is_Node
.ln_Name
= "SB128 Reset Handler";
1254 AddResetCallback( &interrupt
);