revert between 56095 -> 55830 in arch
[AROS.git] / workbench / devs / AHI / Drivers / SB128 / misc.c
blob10acbfbd4c5bb43f5710cf3b5514ba5889dacd42
1 /*
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.
14 All Rights Reserved.
18 #include <exec/memory.h>
20 #ifdef __AROS__
21 #include <aros/debug.h>
22 #endif
24 #include <proto/exec.h>
25 #include <proto/dos.h>
27 #include "library.h"
28 #include "regs.h"
29 #include "interrupt.h"
30 #include "misc.h"
32 #include "pci_wrapper.h"
34 #ifdef __AROS__
35 #define DebugPrintF bug
36 INTGW(static, void, playbackinterrupt, PlaybackInterrupt);
37 INTGW(static, void, recordinterrupt, RecordInterrupt);
38 INTGW(static, ULONG, cardinterrupt, CardInterrupt);
39 #endif
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);
50 #endif
52 void micro_delay(unsigned int val)
54 struct timerequest* TimerIO = NULL;
55 struct MsgPort * replymp;
57 replymp = (struct MsgPort *) CreateMsgPort();
58 if (!replymp)
60 DebugPrintF("SB128: Couldn't create reply port\n");
61 return;
64 TimerIO = (struct timerequest *)CreateIORequest(replymp, sizeof(struct timerequest));
66 if (TimerIO == NULL)
68 DebugPrintF("SB128: Out of memory.\n");
69 return;
72 if (OpenDevice((CONST_STRPTR) "timer.device", UNIT_MICROHZ, (struct IORequest *)TimerIO, 0) != 0)
74 DebugPrintF("SB128: Unable to open 'timer.device'.\n");
75 return;
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);
84 TimerIO = NULL;
86 if (replymp)
88 DeleteMsgPort(replymp);
92 unsigned long src_ready(struct SB128_DATA *card)
94 unsigned int i;
95 unsigned long r;
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))
102 return r;
103 //micro_delay(1);
106 DebugPrintF("SB128: SRC Ready timeout.\n");
107 return 0;
110 void src_write(struct SB128_DATA *card, unsigned short addr, unsigned short data)
112 unsigned long r;
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);
119 r = r | data;
120 pci_outl(r | SRC_WE, SB128_SRC, card);
121 //micro_delay(1);
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.
130 unsigned long r;
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. */
141 //micro_delay(1);
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)
151 char ak4531_L1 = 0;
152 char ak4531_R1 = 0;
153 char ak4531_L2 = 0;
154 char ak4531_R2 = 0;
156 char input_right = 0;
157 char input_left = 0;
159 short left_vol = 0;
160 short right_vol = 0;
162 float steps = 0;
164 if (reg == AC97_RECORD_SELECT)
166 /* Change input select settings */
167 input_right = val;
168 input_left = val >> 8;
170 /* Translate as appropriate */
171 switch (input_left) {
173 case 0:
174 /* Mic */
175 ak4531_L2 = 0x80;
176 break;
178 case 1:
179 /* CD */
180 ak4531_L1 = 0x04;
181 break;
183 case 2:
184 /* Video */
185 break;
187 case 3:
188 /* Aux */
189 ak4531_L2 = 0x10;
190 break;
192 case 4:
193 /* Line in */
194 ak4531_L1 = 0x10;
195 break;
197 case 5:
198 /* Stereo Mix (all) */
199 ak4531_L1 = 0x14;
200 ak4531_L2 = 0xF4;
201 break;
203 case 6:
204 /* Mono Mix */
205 break;
207 case 7:
208 /* Phone */
209 ak4531_L2 = 0x60;
210 break;
212 default:
213 /* Shouldn't happen */
214 DebugPrintF("SB128: Unsupported Record Input command\n");
217 switch (input_right) {
219 case 0:
220 /* Mic */
221 ak4531_R2 = 0x80;
222 break;
224 case 1:
225 /* CD */
226 ak4531_R1 = 0x02;
227 break;
229 case 2:
230 /* Video */
231 break;
233 case 3:
234 /* Aux */
235 ak4531_R2 = 0x08;
236 break;
238 case 4:
239 /* Line in */
240 ak4531_R1 = 0x08;
241 break;
243 case 5:
244 /* Stereo Mix (all) */
245 ak4531_R1 = 0x0A;
246 ak4531_R2 = 0xEC;
247 break;
249 case 6:
250 /* Mono Mix */
251 break;
253 case 7:
254 /* Phone */
255 ak4531_R2 = 0x60;
256 break;
258 default:
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);
269 return;
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 */
275 if (val & AC97_MUTE)
277 /* Using a muted volume */
278 right_vol = (AK4531_MUTE | 0x6);
279 left_vol = (AK4531_MUTE | 0x6);
281 else
283 /* Strip bits */
284 right_vol = (val & 0x1F);
285 left_vol = (val >> 8);
287 /* Convert right volume */
288 if (right_vol < 0x8)
290 steps = 0x8 - right_vol;
291 steps = steps * 1.5;
292 steps = steps / 2;
293 right_vol = (int) (steps + 0.5);
294 right_vol = 0x6 - right_vol;
296 else if (right_vol > 0x8)
298 steps = right_vol - 0x8;
299 steps = steps * 1.5;
300 steps = steps / 2;
301 right_vol = (int) (steps + 0.5);
302 right_vol = 0x6 + right_vol;
304 else if (right_vol == 0x8)
306 /* No attentuation, no mute */
307 right_vol = 0x6;
310 /* Convert left volume */
311 if (left_vol < 0x8)
313 steps = 0x8 - left_vol;
314 steps = steps * 1.5;
315 steps = steps / 2;
316 left_vol = (int) (steps + 0.5);
317 left_vol = 0x6 - left_vol;
319 else if (left_vol > 0x8)
321 steps = left_vol - 0x8;
322 steps = steps * 1.5;
323 steps = steps / 2;
324 left_vol = (int) (steps + 0.5);
325 left_vol = 0x6 + left_vol;
327 else if (left_vol == 0x8)
329 /* No attentuation, no mute */
330 left_vol = 0x6;
335 /* Write adjusted volume to appropriate place */
336 /* Un-mute, and disable output, if an input muted */
337 switch (reg) {
339 case AC97_PHONE_VOL:
340 codec_write(card, AK4531_PHONE_VOL_L, right_vol);
341 codec_write(card, AK4531_PHONE_VOL_R, right_vol);
342 break;
344 case AC97_MIC_VOL:
345 codec_write(card, AK4531_MIC_VOL, right_vol);
346 break;
348 case AC97_LINEIN_VOL:
349 if ((left_vol & AK4531_MUTE) && (right_vol & AK4531_MUTE))
351 left_vol = 0x6;
352 right_vol = 0x6;
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);
357 else
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);
365 break;
367 case AC97_CD_VOL:
368 if ((left_vol & AK4531_MUTE) && (right_vol & AK4531_MUTE))
370 left_vol = 0x6;
371 right_vol = 0x6;
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);
376 else
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);
384 break;
386 case AC97_AUX_VOL:
387 if ((left_vol & AK4531_MUTE) && (right_vol & AK4531_MUTE))
389 left_vol = 0x6;
390 right_vol = 0x6;
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);
395 else
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);
403 break;
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);
408 break;
410 default:
411 DebugPrintF("SB128: Invalid value for Volume Set\n");
414 return;
420 void codec_write(struct SB128_DATA *card, unsigned short reg, unsigned short val)
422 unsigned long i, r;
424 /* Take hold of the hardware semaphore */
425 //ObtainSemaphore(&card->sb128_semaphore);
427 if(card->es1370)
429 for (i = 0; i < 10; i++)
431 if (!(pci_inl(SB128_STATUS, card) & CODEC_CSTAT ))
432 goto es1370_ok1;
433 Delay(1);
435 DebugPrintF("SB128: Couldn't write to ak4531!\n");
436 return;
438 es1370_ok1:
439 pci_outw(((unsigned char)reg << ES1370_CODEC_ADD_SHIFT) | (unsigned char)val, ES1370_SB128_CODEC, card);
440 micro_delay(100);
442 else
445 /* Check for WIP. */
446 for (i = 0; i < 0x1000; i++)
448 if (!(pci_inl(SB128_CODEC, card) & CODEC_WIP ))
449 goto ok1;
451 DebugPrintF("SB128: Couldn't write to ac97! (1)\n");
452 //ReleaseSemaphore(&card->sb128_semaphore);
453 return;
455 ok1:
456 /* Get copy of SRC register when it's not busy. */
457 r = src_ready(card);
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)
465 break;
466 //micro_delay(1);
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)
473 break;
474 //micro_delay(1);
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
481 the codec write. */
482 //micro_delay(1);
484 /* Restore SRC register. */
485 src_ready(card);
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);
494 return;
498 DebugPrintF("SB128: Couldn't write to ac97! (2)\n");
501 //ReleaseSemaphore(&card->sb128_semaphore);
502 return;
505 unsigned short codec_read(struct SB128_DATA *card, unsigned short reg)
507 unsigned long i, r;
508 unsigned short val;
510 if(card->es1370)
511 return 0;
513 //ObtainSemaphore(&card->sb128_semaphore);
515 /* Check for WIP. */
516 for (i = 0; i < 0x1000; i++) {
517 if (!((pci_inl(SB128_CODEC, card)) & CODEC_WIP ))
518 goto ok1;
520 DebugPrintF("SB128: Couldn't read from ac97! (1)\n");
521 // ReleaseSemaphore(&card->sb128_semaphore);
522 return 0;
524 ok1:
526 /* Get copy of SRC register when it's not busy. */
527 r = src_ready(card);
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)
537 break;
538 //micro_delay(1);
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)
545 break;
546 //micro_delay(1);
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. */
553 //micro_delay(1);
555 /* Restore SRC register. */
556 src_ready(card);
557 pci_outl(r, SB128_SRC, card);
559 /* Check for WIP. */
560 for (i = 0; i < 0x1000; i++) {
561 if (!((pci_inl(SB128_CODEC, card)) & CODEC_WIP))
562 goto ok2;
564 DebugPrintF("SB128: Couldn't read from ac97 (2)!\n");
565 // ReleaseSemaphore(&card->sb128_semaphore);
566 return 0;
568 ok2:
570 /* Wait for RDY. */
571 //micro_delay(1);
572 for (i = 0; i < 0x1000; i++) {
573 if (!((pci_inl(SB128_CODEC, card)) & CODEC_RDY))
574 goto ok3;
576 DebugPrintF("SB128: Couldn't read from ac97 (3)!\n");
577 // ReleaseSemaphore(&card->sb128_semaphore);
578 return 0;
580 ok3:
581 //micro_delay(5);
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);
587 return val;
590 void rate_set_adc(struct SB128_DATA *card, unsigned long rate)
592 unsigned long n, truncm, freq;
594 //ObtainSemaphore(&card->sb128_semaphore);
596 if (rate > 48000)
597 rate = 48000;
598 if (rate < 4000)
599 rate = 4000;
601 if (card->es1370)
603 pci_outl(((pci_inl(SB128_CONTROL, card) & ~DAC2_DIV_MASK) | (DAC2_SRTODIV(rate) << DAC2_DIV_SHIFT)), SB128_CONTROL, card);
605 else
608 /* This is completely undocumented */
609 n = rate / 3000;
610 if ((1 << n) & ((1 << 15) | (1 << 13) | (1 << 11) | (1 << 9)))
611 n--;
612 truncm = (21 * n - 1) | 1;
613 freq = ((48000UL << 15) / rate) * n;
615 if (rate >= 24000)
617 if (truncm > 239)
618 truncm = 239;
619 src_write(card, SRC_ADC + SRC_TRUNC, (((239 - truncm) >> 1) << 9) | (n << 4));
621 else
623 if (truncm > 119)
624 truncm = 119;
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);
645 if (rate > 48000)
646 rate = 48000;
647 if (rate < 4000)
648 rate = 4000;
650 if(card->es1370)
652 pci_outl(((pci_inl(SB128_CONTROL, card) & ~DAC2_DIV_MASK) | (DAC2_SRTODIV(rate) << DAC2_DIV_SHIFT)), SB128_CONTROL, card);
654 else
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
682 time. */
684 struct SB128_DATA*
685 AllocDriverData( struct PCIDevice * dev,
686 struct DriverBase* AHIsubBase )
688 struct SB128_DATA* card;
689 UWORD command_word;
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 );
696 if( card == NULL )
698 Req( "Unable to allocate driver structure." );
699 return NULL;
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;
707 #ifdef __AROS__
708 card->interrupt.is_Code = (void(*)(void))&cardinterrupt;
709 #else
710 card->interrupt.is_Code = (void(*)(void))CardInterrupt;
711 #endif
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;
717 #ifdef __AROS__
718 card->playback_interrupt.is_Code = (void(*)(void))&playbackinterrupt;
719 #else
720 card->playback_interrupt.is_Code = (void(*)(void))PlaybackInterrupt;
721 #endif
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;
727 #ifdef __AROS__
728 card->record_interrupt.is_Code = (void(*)(void))&recordinterrupt;
729 #else
730 card->record_interrupt.is_Code = (void(*)(void))RecordInterrupt;
731 #endif
732 card->record_interrupt.is_Data = (APTR) card;
734 card->pci_dev = dev;
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.");
758 return NULL;
761 ahi_pci_add_intserver(&card->interrupt, dev);
762 card->interrupt_added = TRUE;
765 card->card_initialized = TRUE;
766 card->input = 0;
767 card->output = 0;
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);
775 #endif
777 return card;
781 /******************************************************************************
782 ** DriverData deallocation ****************************************************
783 ******************************************************************************/
785 /* And this code used to be in _AHIsub_FreeAudio(). */
787 void
788 FreeDriverData( struct SB128_DATA* card,
789 struct DriverBase* AHIsubBase )
791 if( card != NULL )
793 if( card->pci_dev != NULL )
795 if( card->card_initialized )
797 card_cleanup( card );
800 if( card->pci_master_enabled )
802 UWORD cmd;
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);
815 FreeVec( card );
820 int card_init(struct SB128_DATA *card)
822 struct PCIDevice *dev = (struct PCIDevice *) card->pci_dev;
823 unsigned short cod;
824 unsigned int i;
826 /* Check if the card is an original ES1370 - different code needed */
827 if (inw_config(2, dev) == 0x5000)
828 card->es1370 = TRUE;
829 else
830 card->es1370 = FALSE;
832 /* Different init sequence for the ES1370 */
833 if (card->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 */
866 /* Always on */
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");
882 else
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);
892 Delay(1);
894 /* Assert the AC97 reset, and wait 20ms */
895 pci_outl(CODEC_RESET, SB128_CONTROL, card);
896 Delay(1);
897 /* De-assert delay, and wait 20ms */
898 pci_outl(0x00, SB128_CONTROL, card);
899 Delay(1);
901 DebugPrintF("SB128: Did AC97 reset.\n");
903 /* Disable the Sample Rate Converter (SRC) */
904 src_ready(card);
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 */
930 src_ready(card);
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);
971 return 0;
975 void card_cleanup(struct SB128_DATA *card)
981 /******************************************************************************
982 ** Misc. **********************************************************************
983 ******************************************************************************/
985 void
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 );
996 void
997 RestoreMixerState( struct SB128_DATA* card )
999 if(card->es1370)
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);
1008 else
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 );
1018 void
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 );
1032 else
1034 if(card->es1370)
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 );
1051 else
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 );
1072 Fixed
1073 Linear2MixerGain( Fixed linear,
1074 UWORD* bits )
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 */
1110 0 /* -oo dB */
1113 int v = 0;
1114 while( linear < gain[ v ] )
1116 ++v;
1119 if( v == 32 )
1121 *bits = 0x8000; /* Mute */
1123 else
1125 *bits = ( v << 8 ) | v;
1127 return gain[ v ];
1130 Fixed
1131 Linear2RecordGain( Fixed linear,
1132 UWORD* bits )
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 */
1152 0 /* -oo dB */
1155 int v = 0;
1157 while( linear < gain[ v ] )
1159 ++v;
1162 if( v == 16 )
1164 *bits = 0x8000; /* Mute */
1166 else
1168 *bits = ( ( 15 - v ) << 8 ) | ( 15 - v );
1171 return gain[ v ];
1175 ULONG
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)
1185 void* address;
1186 unsigned long a;
1188 D(bug("[SB128]: %s()\n", __PRETTY_FUNCTION__);)
1190 address = (void *) AllocVec(size + boundary, (
1191 #if defined(__AROS__) && (__WORDSIZE==64)
1192 MEMF_31BIT |
1193 #endif
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;
1208 return address;
1212 void pci_free_consistent(void* addr)
1214 D(bug("[SB128]: %s()\n", __PRETTY_FUNCTION__);)
1216 FreeVec(addr);
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
1225 unsigned long ctrl;
1227 ctrl = pci_inl(SB128_CONTROL, card);
1228 ctrl &= ( ~(CTRL_DAC2_EN) & ~(CTRL_ADC_EN) );
1230 /* Stop */
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);
1236 return 0UL;
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 );
1251 #endif