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 <proto/expansion.h>
23 #include <proto/dos.h>
25 #include <devices/timer.h>
27 #include "library_card.h"
29 #include "interrupt.h"
31 #include "DriverData.h"
33 /* Global in Card.c */
34 extern const UWORD InputBits
[];
35 extern struct DOSIFace
*IDOS
;
37 //extern struct timerequest *TimerIO;
38 extern struct MsgPort
* replymp
;
40 /* Public functions in main.c */
41 int card_init(struct CardData
*card
);
42 void card_cleanup(struct CardData
*card
);
44 void AddResetHandler(struct CardData
*card
);
46 void MicroDelay(unsigned int val
)
48 struct Device
* TimerBase
= NULL
;
49 struct TimeRequest
* TimerIO
= NULL
;
50 struct MsgPort
* replymp
;
52 replymp
= (struct MsgPort
*) IExec
->CreatePort(NULL
, 0);
55 IExec
->DebugPrintF("SB128: Couldn't create reply port\n");
59 TimerIO
= (struct TimeRequest
*)IExec
->CreateIORequest(replymp
, sizeof(struct TimeRequest
));
63 IExec
->DebugPrintF("SB128: Out of memory.\n");
67 if (IExec
->OpenDevice("timer.device", UNIT_MICROHZ
, (struct IORequest
*)TimerIO
, 0) != 0)
69 IExec
->DebugPrintF("SB128: Unable to open 'timer.device'.\n");
74 TimerBase
= (struct Device
*)TimerIO
->Request
.io_Device
;
79 TimerIO
->Request
.io_Command
= TR_ADDREQUEST
;
80 TimerIO
->Time
.Seconds
= 0;
81 TimerIO
->Time
.Microseconds
= val
;
82 IExec
->DoIO((struct IORequest
*)TimerIO
);
83 IExec
->DeleteIORequest((struct IORequest
*)TimerIO
);
85 IExec
->CloseDevice((struct IORequest
*)TimerIO
);
86 IExec
->DeletePort(replymp
);
90 unsigned long src_ready(struct CardData
*card
)
92 struct PCIDevice
*dev
= (struct PCIDevice
*) card
->pci_dev
;
96 /* Wait for the busy bit to become invalid, and then return the contents
97 of the SRC register. */
98 for (i
= 0; i
< 0x1000; i
++)
100 if (!((r
= dev
->InLong(card
->iobase
+ SB128_SRC
)) & SRC_BUSY
))
105 IExec
->DebugPrintF("SB128: SRC Ready timeout.\n");
109 void src_write(struct CardData
*card
, unsigned short addr
, unsigned short data
)
111 struct PCIDevice
*dev
= (struct PCIDevice
*) card
->pci_dev
;
114 // IExec->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 dev
->OutLong(card
->iobase
+ SB128_SRC
, r
| SRC_WE
);
123 // IExec->ReleaseSemaphore(&card->sb128_semaphore);
126 unsigned short src_read(struct CardData
*card
, unsigned short addr
)
128 struct PCIDevice
*dev
= (struct PCIDevice
*) card
->pci_dev
;
130 //There may be ES137x bugs that require accomodating in this section.
134 // IExec->ObtainSemaphore(&card->sb128_semaphore);
136 /* Get copy of SRC register when it's not busy, add address, write back,
137 wait for ready, then read back value. */
138 r
= src_ready(card
) & (SRC_DISABLE
| SRC_DIS_DAC2
| SRC_DIS_ADC
);
139 r
= r
| (addr
<< SRC_ADDR_SHIFT
);
140 dev
->OutLong(card
->iobase
+ SB128_SRC
, r
);
142 /* Give the chip a chance to set the busy bit. */
145 // IExec->ReleaseSemaphore(&card->sb128_semaphore);
147 return (src_ready(card
) & 0xFFFF);
150 /* Translate AC97 commands to AK4531 commands, and write to the AK4531 codec */
151 void ak4531_ac97_write(struct CardData
*card
, unsigned short reg
, unsigned short val
)
158 char input_right
= 0;
166 if (reg
== AC97_RECORD_SELECT
)
168 /* Change input select settings */
170 input_left
= val
>> 8;
172 /* Translate as appropriate */
173 switch (input_left
) {
200 /* Stereo Mix (all) */
215 /* Shouldn't happen */
216 IExec
->DebugPrintF("SB128: Unsupported Record Input command\n");
219 switch (input_right
) {
246 /* Stereo Mix (all) */
261 /* Shouldn't happen */
262 IExec
->DebugPrintF("SB128: Unsupported Record Input command\n");
265 /* Write input values to AK4531 */
267 codec_write(card
, AK4531_INPUT_MUX_L_1
, ak4531_L1
);
268 codec_write(card
, AK4531_INPUT_MUX_R_1
, ak4531_R1
);
269 codec_write(card
, AK4531_INPUT_MUX_L_2
, ak4531_L2
);
270 codec_write(card
, AK4531_INPUT_MUX_R_2
, ak4531_R2
);
274 if (reg
== AC97_PHONE_VOL
|| AC97_MIC_VOL
|| AC97_LINEIN_VOL
|| AC97_CD_VOL
|| AC97_AUX_VOL
|| AC97_PCMOUT_VOL
)
276 /* Adjust PCM (from card) Input gain */
279 /* Using a muted volume */
280 right_vol
= (AK4531_MUTE
| 0x6);
281 left_vol
= (AK4531_MUTE
| 0x6);
286 right_vol
= (val
& 0x1F);
287 left_vol
= (val
>> 8);
289 /* Convert right volume */
292 steps
= 0x8 - right_vol
;
295 right_vol
= (int) (steps
+ 0.5);
296 right_vol
= 0x6 - right_vol
;
298 else if (right_vol
> 0x8)
300 steps
= right_vol
- 0x8;
303 right_vol
= (int) (steps
+ 0.5);
304 right_vol
= 0x6 + right_vol
;
306 else if (right_vol
== 0x8)
308 /* No attentuation, no mute */
312 /* Convert left volume */
315 steps
= 0x8 - left_vol
;
318 left_vol
= (int) (steps
+ 0.5);
319 left_vol
= 0x6 - left_vol
;
321 else if (left_vol
> 0x8)
323 steps
= left_vol
- 0x8;
326 left_vol
= (int) (steps
+ 0.5);
327 left_vol
= 0x6 + left_vol
;
329 else if (left_vol
== 0x8)
331 /* No attentuation, no mute */
337 /* Write adjusted volume to appropriate place */
338 /* Un-mute, and disable output, if an input muted */
342 codec_write(card
, AK4531_PHONE_VOL_L
, right_vol
);
343 codec_write(card
, AK4531_PHONE_VOL_R
, right_vol
);
347 codec_write(card
, AK4531_MIC_VOL
, right_vol
);
350 case AC97_LINEIN_VOL
:
351 if ((left_vol
& AK4531_MUTE
) && (right_vol
& AK4531_MUTE
))
355 /* Disable on OUTPUT mux */
356 card
->ak4531_output_1
= (card
->ak4531_output_1
& ~(AK4531_OUTPUT_LINE
));
357 codec_write(card
, AK4531_OUTPUT_MUX_1
, card
->ak4531_output_1
);
361 codec_write(card
, AK4531_LINEIN_VOL_L
, left_vol
);
362 codec_write(card
, AK4531_LINEIN_VOL_R
, right_vol
);
363 /* Re-enable on OUTPUT mux */
364 card
->ak4531_output_1
= (card
->ak4531_output_1
| AK4531_OUTPUT_LINE
);
365 codec_write(card
, AK4531_OUTPUT_MUX_1
, card
->ak4531_output_1
);
370 if ((left_vol
& AK4531_MUTE
) && (right_vol
& AK4531_MUTE
))
374 /* Disable on OUTPUT mux */
375 card
->ak4531_output_1
= (card
->ak4531_output_1
& ~(AK4531_OUTPUT_CD
));
376 codec_write(card
, AK4531_OUTPUT_MUX_1
, card
->ak4531_output_1
);
380 codec_write(card
, AK4531_CD_VOL_L
, left_vol
);
381 codec_write(card
, AK4531_CD_VOL_R
, right_vol
);
382 /* Re-enable on OUTPUT mux */
383 card
->ak4531_output_1
= (card
->ak4531_output_1
| AK4531_OUTPUT_CD
);
384 codec_write(card
, AK4531_OUTPUT_MUX_1
, card
->ak4531_output_1
);
389 if ((left_vol
& AK4531_MUTE
) && (right_vol
& AK4531_MUTE
))
393 /* Disable on OUTPUT mux */
394 card
->ak4531_output_2
= (card
->ak4531_output_2
& ~(AK4531_OUTPUT_AUX
));
395 codec_write(card
, AK4531_OUTPUT_MUX_2
, card
->ak4531_output_2
);
399 codec_write(card
, AK4531_AUX_VOL_L
, left_vol
);
400 codec_write(card
, AK4531_AUX_VOL_R
, right_vol
);
401 /* Re-enable on OUTPUT mux */
402 card
->ak4531_output_2
= (card
->ak4531_output_2
| AK4531_OUTPUT_AUX
);
403 codec_write(card
, AK4531_OUTPUT_MUX_2
, card
->ak4531_output_2
);
407 case AC97_PCMOUT_VOL
:
408 codec_write(card
, AK4531_PCMOUT_VOL_L
, left_vol
);
409 codec_write(card
, AK4531_PCMOUT_VOL_R
, right_vol
);
413 IExec
->DebugPrintF("SB128: Invalid value for Volume Set\n");
422 void codec_write(struct CardData
*card
, unsigned short reg
, unsigned short val
)
424 struct PCIDevice
*dev
= (struct PCIDevice
*) card
->pci_dev
;
427 /* Take hold of the hardware semaphore */
428 //IExec->ObtainSemaphore(&card->sb128_semaphore);
432 for (i
= 0; i
< 10; i
++)
434 if (!(dev
->InLong(card
->iobase
+ SB128_STATUS
) & CODEC_CSTAT
))
438 IExec
->DebugPrintF("SB128: Couldn't write to ak4531!\n");
442 dev
->OutWord(card
->iobase
+ ES1370_SB128_CODEC
, ((unsigned char)reg
<< ES1370_CODEC_ADD_SHIFT
) | (unsigned char)val
);
449 for (i
= 0; i
< 0x1000; i
++)
451 if (!(dev
->InLong(card
->iobase
+ SB128_CODEC
) & CODEC_WIP
))
454 IExec
->DebugPrintF("SB128: Couldn't write to ac97! (1)\n");
455 //IExec->ReleaseSemaphore(&card->sb128_semaphore);
459 /* Get copy of SRC register when it's not busy. */
461 /* Enable "SRC State Data", an undocumented feature! */
462 dev
->OutLong(card
->iobase
+ SB128_SRC
, (r
& (SRC_DISABLE
| SRC_DIS_DAC2
|
463 SRC_DIS_ADC
)) | 0x00010000);
465 /* Wait for "state 0", to avoid "transition states". */
466 for (i
= 0; i
< 0x1000; i
++)
468 if ((dev
->InLong(card
->iobase
+ SB128_SRC
) & 0x00870000) == 0x00)
473 /* Now wait for an undocumented bit to be set (and the SRC to be NOT busy) */
474 for (i
= 0; i
< 0x1000; i
++)
476 if ((dev
->InLong(card
->iobase
+ SB128_SRC
) & 0x00870000) == 0x00010000)
481 /* Write out the value to the codec now. */
482 dev
->OutLong(card
->iobase
+ SB128_CODEC
, (((reg
<< CODEC_ADD_SHIFT
) & CODEC_ADD_MASK
) | val
));
484 /* Delay to make sure the chip had time to set the WIP after
488 /* Restore SRC register. */
490 dev
->OutLong(card
->iobase
+ SB128_SRC
, r
);
492 /* Check for WIP before returning. */
493 for (i
= 0; i
< 0x1000; i
++)
495 if (!(dev
->InLong(card
->iobase
+ SB128_CODEC
) & CODEC_WIP
))
497 //IExec->ReleaseSemaphore(&card->sb128_semaphore);
502 IExec
->DebugPrintF("SB128: Couldn't write to ac97! (2)\n");
505 //IExec->ReleaseSemaphore(&card->sb128_semaphore);
509 unsigned short codec_read(struct CardData
*card
, unsigned short reg
)
511 struct PCIDevice
*dev
= (struct PCIDevice
*) card
->pci_dev
;
518 //IExec->ObtainSemaphore(&card->sb128_semaphore);
521 for (i
= 0; i
< 0x1000; i
++) {
522 if (!((dev
->InLong(card
->iobase
+ SB128_CODEC
)) & CODEC_WIP
))
525 IExec
->DebugPrintF("SB128: Couldn't read from ac97! (1)\n");
526 // IExec->ReleaseSemaphore(&card->sb128_semaphore);
531 /* Get copy of SRC register when it's not busy. */
534 /* Enable "SRC State Data", an undocumented feature! */
535 dev
->OutLong(card
->iobase
+ SB128_SRC
, (r
& (SRC_DISABLE
| SRC_DIS_DAC1
| SRC_DIS_DAC2
|
536 SRC_DIS_ADC
)) | 0x00010000);
538 /* Wait for "state 0", to avoid "transition states".
539 Seen in open code. */
540 for (i
= 0; i
< 0x1000; i
++)
542 if ((dev
->InLong(card
->iobase
+ SB128_SRC
) & 0x00870000) == 0x00)
547 /* Now wait for an undocumented bit to be set (and the SRC to be NOT busy) */
548 for (i
= 0; i
< 0x1000; i
++)
550 if ((dev
->InLong(card
->iobase
+ SB128_SRC
) & 0x00870000) == 0x00010000)
555 /* Write the read request to the chip now */
556 dev
->OutLong(card
->iobase
+ SB128_CODEC
, (((reg
<< CODEC_ADD_SHIFT
) & CODEC_ADD_MASK
) | CODEC_READ
));
558 /* Give the chip time to respond to our read request. */
561 /* Restore SRC register. */
563 dev
->OutLong(card
->iobase
+ SB128_SRC
, r
);
566 for (i
= 0; i
< 0x1000; i
++) {
567 if (!((dev
->InLong(card
->iobase
+ SB128_CODEC
)) & CODEC_WIP
))
570 IExec
->DebugPrintF("SB128: Couldn't read from ac97 (2)!\n");
571 // IExec->ReleaseSemaphore(&card->sb128_semaphore);
578 for (i
= 0; i
< 0x1000; i
++) {
579 if (!((dev
->InLong(card
->iobase
+ SB128_CODEC
)) & CODEC_RDY
))
582 IExec
->DebugPrintF("SB128: Couldn't read from ac97 (3)!\n");
583 // IExec->ReleaseSemaphore(&card->sb128_semaphore);
588 IDOS
->Delay(1); //A delay here is crucial, remove this if you use MicroDelay()
589 val
= dev
->InLong(card
->iobase
+ SB128_CODEC
);
591 // IExec->ReleaseSemaphore(&card->sb128_semaphore);
596 void rate_set_adc(struct CardData
*card
, unsigned long rate
)
598 struct PCIDevice
*dev
= (struct PCIDevice
*) card
->pci_dev
;
600 unsigned long n
, truncm
, freq
;
602 //IExec->ObtainSemaphore(&card->sb128_semaphore);
611 dev
->OutLong(card
->iobase
+ SB128_CONTROL
, ((dev
->InLong(card
->iobase
+ SB128_CONTROL
) & ~DAC2_DIV_MASK
) | (DAC2_SRTODIV(rate
) << DAC2_DIV_SHIFT
)));
616 /* This is completely undocumented */
618 if ((1 << n
) & ((1 << 15) | (1 << 13) | (1 << 11) | (1 << 9)))
620 truncm
= (21 * n
- 1) | 1;
621 freq
= ((48000UL << 15) / rate
) * n
;
627 src_write(card
, SRC_ADC
+ SRC_TRUNC
, (((239 - truncm
) >> 1) << 9) | (n
<< 4));
633 src_write(card
, SRC_ADC
+ SRC_TRUNC
, 0x8000 | (((119 - truncm
) >> 1) << 9) | (n
<< 4));
635 src_write(card
, SRC_ADC
+ SRC_INT
,
636 (src_read(card
, SRC_ADC
+ SRC_INT
) & 0x00FF) | ((freq
>> 5) & 0xFC00));
637 src_write(card
, SRC_ADC
+ SRC_VF
, freq
& 0x7FFF);
638 src_write(card
, SRC_VOL_ADC
, n
<< 8);
639 src_write(card
, SRC_VOL_ADC
+ 1, n
<< 8);
643 //IExec->ReleaseSemaphore(&card->sb128_semaphore);
647 void rate_set_dac2(struct CardData
*card
, unsigned long rate
)
649 struct PCIDevice
*dev
= (struct PCIDevice
*) card
->pci_dev
;
651 unsigned long freq
, r
;
653 //IExec->ObtainSemaphore(&card->sb128_semaphore);
662 dev
->OutLong(card
->iobase
+ SB128_CONTROL
, ((dev
->InLong(card
->iobase
+ SB128_CONTROL
) & ~DAC2_DIV_MASK
) | (DAC2_SRTODIV(rate
) << DAC2_DIV_SHIFT
)));
667 freq
= ((rate
<< 15 ) + 1500) / 3000;
669 /* Get copy of SRC register when it's not busy, clear, preserve the disable bits, write back. */
670 r
= src_ready(card
) & (SRC_DISABLE
| SRC_DIS_DAC1
| SRC_DIS_DAC2
| SRC_DIS_ADC
);
671 dev
->OutLong(card
->iobase
+ SB128_SRC
, r
);
673 /* This is completely undocumented */
674 src_write(card
, SRC_DAC2
+ SRC_INT
,
675 (src_read(card
, SRC_DAC2
+ SRC_INT
) & 0x00FF) | ((freq
>> 5) & 0xFC00));
676 src_write(card
, SRC_DAC2
+ SRC_VF
, freq
& 0x7FFF);
677 r
= (src_ready(card
) & (SRC_DISABLE
| SRC_DIS_DAC1
| SRC_DIS_ADC
));
678 dev
->OutLong(card
->iobase
+ SB128_SRC
, r
);
682 //IExec->ReleaseSemaphore(&card->sb128_semaphore);
686 /******************************************************************************
687 ** DriverData allocation ******************************************************
688 ******************************************************************************/
690 /* This code used to be in _AHIsub_AllocAudio(), but since we're now
691 handling CAMD support too, it needs to be done at driver loading
695 AllocDriverData( struct PCIDevice
* dev
,
696 struct DriverBase
* AHIsubBase
)
698 struct CardBase
* CardBase
= (struct CardBase
*) AHIsubBase
;
699 struct CardData
* card
;
704 // FIXME: This should be non-cachable, DMA-able memory
705 card
= IExec
->AllocVec( sizeof( *card
), MEMF_PUBLIC
| MEMF_CLEAR
);
709 Req( "Unable to allocate driver structure." );
713 card
->ahisubbase
= AHIsubBase
;
715 card
->interrupt
.is_Node
.ln_Type
= NT_EXTINTERRUPT
;
716 card
->interrupt
.is_Node
.ln_Pri
= 0;
717 card
->interrupt
.is_Node
.ln_Name
= (STRPTR
) LibName
;
718 card
->interrupt
.is_Code
= (void(*)(void)) CardInterrupt
;
719 card
->interrupt
.is_Data
= (APTR
) card
;
721 card
->playback_interrupt
.is_Node
.ln_Type
= NT_INTERRUPT
;
722 card
->playback_interrupt
.is_Node
.ln_Pri
= 0;
723 card
->playback_interrupt
.is_Node
.ln_Name
= (STRPTR
) LibName
;
724 card
->playback_interrupt
.is_Code
= PlaybackInterrupt
;
725 card
->playback_interrupt
.is_Data
= (APTR
) card
;
727 card
->record_interrupt
.is_Node
.ln_Type
= NT_INTERRUPT
;
728 card
->record_interrupt
.is_Node
.ln_Pri
= 0;
729 card
->record_interrupt
.is_Node
.ln_Name
= (STRPTR
) LibName
;
730 card
->record_interrupt
.is_Code
= RecordInterrupt
;
731 card
->record_interrupt
.is_Data
= (APTR
) card
;
735 command_word
= dev
->ReadConfigWord( PCI_COMMAND
);
736 command_word
|= PCI_COMMAND_IO
| PCI_COMMAND_MEMORY
| PCI_COMMAND_MASTER
;
737 dev
->WriteConfigWord( PCI_COMMAND
, command_word
);
739 card
->pci_master_enabled
= TRUE
;
741 card
->iobase
= dev
->GetResourceRange(0)->BaseAddress
;
742 card
->length
= ~( dev
->GetResourceRange(0)->Size
& PCI_BASE_ADDRESS_IO_MASK
);
743 card
->irq
= dev
->MapInterrupt();
744 card
->chiprev
= dev
->ReadConfigByte( PCI_REVISION_ID
);
745 card
->model
= dev
->ReadConfigWord( PCI_SUBSYSTEM_ID
);
747 IExec
->DebugPrintF("SB128: Device = %x, Vendor = %x, Revision = %x\n", dev
->ReadConfigWord(PCI_DEVICE_ID
),
748 dev
->ReadConfigWord(PCI_VENDOR_ID
), dev
->ReadConfigByte(PCI_REVISION_ID
));
751 /* Initialise hardware access Semaphore */
752 IExec
->InitSemaphore(&card
->sb128_semaphore
);
755 /* Initialize chip */
756 if( card_init( card
) < 0 )
758 IExec
->DebugPrintF("SB128: Unable to initialize Card subsystem.");
763 res
= IExec
->AddIntServer(dev
->MapInterrupt(), &card
->interrupt
);
764 card
->interrupt_added
= TRUE
;
767 card
->card_initialized
= TRUE
;
770 card
->monitor_volume
= Linear2MixerGain( 0, &card
->monitor_volume_bits
);
771 card
->input_gain
= Linear2RecordGain( 0x10000, &card
->input_gain_bits
);
772 card
->output_volume
= Linear2MixerGain( 0x10000, &card
->output_volume_bits
);
773 SaveMixerState(card
);
775 AddResetHandler(card
);
781 /******************************************************************************
782 ** DriverData deallocation ****************************************************
783 ******************************************************************************/
785 /* And this code used to be in _AHIsub_FreeAudio(). */
788 FreeDriverData( struct CardData
* 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
= ((struct PCIDevice
* ) card
->pci_dev
)->ReadConfigWord( PCI_COMMAND
);
805 cmd
&= ~( PCI_COMMAND_IO
| PCI_COMMAND_MEMORY
| PCI_COMMAND_MASTER
);
806 ((struct PCIDevice
* ) card
->pci_dev
)->WriteConfigWord( PCI_COMMAND
, cmd
);
810 if( card
->interrupt_added
)
812 IExec
->RemIntServer(((struct PCIDevice
* ) card
->pci_dev
)->MapInterrupt(), &card
->interrupt
);
815 IExec
->FreeVec( card
);
820 int card_init(struct CardData
*card
)
822 struct PCIDevice
*dev
= (struct PCIDevice
*) card
->pci_dev
;
826 /* Check if the card is an original ES1370 - different code needed */
827 if (dev
->ReadConfigWord(2) == 0x5000)
830 card
->es1370
= FALSE
;
832 /* Different init sequence for the ES1370 */
835 /* Enable CODEC access, set DAC sample rate to 44100 */
836 dev
->OutLong(card
->iobase
+ SB128_CONTROL
, CTRL_CDC_EN
| (DAC2_SRTODIV(44100) << DAC2_DIV_SHIFT
));
837 dev
->OutLong(card
->iobase
+ SB128_SCON
, 0x00);
838 IExec
->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 IExec
->DebugPrintF("SB128: Did VOLUME init\n");
884 /* Basic clear of everything */
885 dev
->OutLong(card
->iobase
+ SB128_CONTROL
, 0x00);
886 dev
->OutLong(card
->iobase
+ SB128_SCON
, 0x00);
887 dev
->OutLong(card
->iobase
+ SB128_LEGACY
, 0x00);
889 /* Magical CT5880 AC97 enable bit plus 20ms delay
890 (Gotta love the undocumented stuff) */
891 dev
->OutLong(card
->iobase
+ SB128_STATUS
, 0x20000000);
894 /* Assert the AC97 reset, and wait 20ms */
895 dev
->OutLong(card
->iobase
+ SB128_CONTROL
, CODEC_RESET
);
897 /* De-assert delay, and wait 20ms */
898 dev
->OutLong(card
->iobase
+ SB128_CONTROL
, 0x00);
901 IExec
->DebugPrintF("SB128: Did AC97 reset.\n");
903 /* Disable the Sample Rate Converter (SRC) */
905 dev
->OutLong(card
->iobase
+ SB128_SRC
, SRC_DISABLE
);
906 /* Clear the SRC RAM */
907 for (i
= 0; i
< 0x80; i
++)
908 src_write(card
, i
, 0);
910 IExec
->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 IExec
->DebugPrintF("SB128: Did SRC init.\n");
926 rate_set_adc(card
, 44100);
927 rate_set_dac2(card
, 44100);
929 /* Re-enable the SRC */
931 dev
->OutLong(card
->iobase
+ SB128_SRC
, 0);
933 card
->currentPlayFreq
= 9;
934 card
->currentRecFreq
= 9;
936 IExec
->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 IExec
->DebugPrintF("SB128: Did VOLUME init.\n");
961 cod
= codec_read(card
, AC97_RESET
);
962 IExec
->DebugPrintF("SB128: AC97 capabilities = %x\n", cod
);
964 cod
= codec_read(card
, AC97_VENDOR_ID0
);
965 IExec
->DebugPrintF("SB128: AC97_VENDOR_ID0 = %x\n", cod
);
967 cod
= codec_read(card
, AC97_VENDOR_ID1
);
968 IExec
->DebugPrintF("SB128: AC97_VENDOR_ID1 = %x\n", cod
);
975 void card_cleanup(struct CardData
*card
)
981 /******************************************************************************
982 ** Misc. **********************************************************************
983 ******************************************************************************/
986 SaveMixerState( struct CardData
* 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 CardData
* 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 CardData
* 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
)
1188 if (IExec
->OpenResource("newmemory.resource"))
1190 address
= IExec
->AllocVecTags(size
, AVT_Type
, MEMF_SHARED
, AVT_Contiguous
, TRUE
, AVT_Lock
, TRUE
,
1191 AVT_PhysicalAlignment
, CACHELINE_SIZE
, AVT_Clear
, 0, TAG_DONE
);
1193 *NonAlignedAddress
= address
;
1197 address
= IExec
->AllocVec(size
+ CACHELINE_SIZE
, MEMF_PUBLIC
| MEMF_CLEAR
);
1199 *NonAlignedAddress
= address
;
1201 if( address
!= NULL
)
1203 a
= (unsigned long) address
;
1204 a
= (a
+ CACHELINE_SIZE
- 1) & ~(CACHELINE_SIZE
- 1);
1205 address
= (void *) a
;
1214 void pci_free_consistent(void* addr
)
1216 IExec
->FreeVec(addr
);
1219 static ULONG
ResetHandler(struct ExceptionContext
*ctx
, struct ExecBase
*pExecBase
, struct CardData
*card
)
1221 struct PCIDevice
*dev
= card
->pci_dev
;
1223 //Stop SB128 interrupts and playback/recording
1226 ctrl
= dev
->InLong(card
->iobase
+ SB128_CONTROL
);
1227 ctrl
&= ( ~(CTRL_DAC2_EN
) & ~(CTRL_ADC_EN
) );
1230 dev
->OutLong(card
->iobase
+ SB128_CONTROL
, ctrl
);
1232 /* Clear and mask interrupts */
1233 dev
->OutLong(card
->iobase
+ SB128_SCON
, (dev
->InLong(card
->iobase
+ SB128_SCON
)) & SB128_IRQ_MASK
);
1238 void AddResetHandler(struct CardData
*card
)
1240 static struct Interrupt interrupt
;
1242 interrupt
.is_Code
= (void (*)())ResetHandler
;
1243 interrupt
.is_Data
= (APTR
) card
;
1244 interrupt
.is_Node
.ln_Pri
= 0;
1245 interrupt
.is_Node
.ln_Type
= NT_EXTINTERRUPT
;
1246 interrupt
.is_Node
.ln_Name
= "SB128 Reset Handler";
1248 IExec
->AddResetCallback( &interrupt
);