enable the new framebuffer resizing code.
[AROS.git] / workbench / devs / AHI / Drivers / SB128 / misc.c
bloba4873edaa49ab68e2c411be1d6a7ca1785d41157
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;
450 micro_delay(1);
452 DebugPrintF("[SB128] Couldn't write to ac97! (1)\n");
453 //ReleaseSemaphore(&card->sb128_semaphore);
454 return;
456 ok1:
457 /* Get copy of SRC register when it's not busy. */
458 r = src_ready(card);
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)
466 break;
467 micro_delay(1);
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)
474 break;
475 micro_delay(1);
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
482 the codec write. */
483 //micro_delay(1);
485 /* Restore SRC register. */
486 src_ready(card);
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);
495 return;
497 micro_delay(1);
500 DebugPrintF("[SB128] Couldn't write to ac97! (2)\n");
503 //ReleaseSemaphore(&card->sb128_semaphore);
504 return;
507 unsigned short codec_read(struct SB128_DATA *card, unsigned short reg)
509 unsigned long i, r;
510 unsigned short val;
512 if(card->es1370)
513 return 0;
515 //ObtainSemaphore(&card->sb128_semaphore);
517 /* Check for WIP. */
518 for (i = 0; i < 0x1000; i++) {
519 if (!((pci_inl(SB128_CODEC, card)) & CODEC_WIP ))
520 goto ok1;
521 micro_delay(1);
523 DebugPrintF("[SB128] Couldn't read from ac97! (1)\n");
524 // ReleaseSemaphore(&card->sb128_semaphore);
525 return 0;
527 ok1:
529 /* Get copy of SRC register when it's not busy. */
530 r = src_ready(card);
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)
540 break;
541 micro_delay(1);
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)
548 break;
549 micro_delay(1);
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. */
556 //micro_delay(1);
558 /* Restore SRC register. */
559 src_ready(card);
560 pci_outl(r, SB128_SRC, card);
562 /* Check for WIP. */
563 for (i = 0; i < 0x1000; i++) {
564 if (!((pci_inl(SB128_CODEC, card)) & CODEC_WIP))
565 goto ok2;
566 micro_delay(1);
568 DebugPrintF("[SB128] Couldn't read from ac97 (2)!\n");
569 // ReleaseSemaphore(&card->sb128_semaphore);
570 return 0;
572 ok2:
574 /* Wait for RDY. */
575 //micro_delay(1);
576 for (i = 0; i < 0x1000; i++) {
577 if (!((pci_inl(SB128_CODEC, card)) & CODEC_RDY))
578 goto ok3;
579 micro_delay(1);
581 DebugPrintF("[SB128] Couldn't read from ac97 (3)!\n");
582 // ReleaseSemaphore(&card->sb128_semaphore);
583 return 0;
585 ok3:
586 //micro_delay(5);
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);
592 return val;
595 void rate_set_adc(struct SB128_DATA *card, unsigned long rate)
597 unsigned long n, truncm, freq;
599 //ObtainSemaphore(&card->sb128_semaphore);
601 if (rate > 48000)
602 rate = 48000;
603 if (rate < 4000)
604 rate = 4000;
606 if (card->es1370)
608 pci_outl(((pci_inl(SB128_CONTROL, card) & ~DAC2_DIV_MASK) | (DAC2_SRTODIV(rate) << DAC2_DIV_SHIFT)), SB128_CONTROL, card);
610 else
613 /* This is completely undocumented */
614 n = rate / 3000;
615 if ((1 << n) & ((1 << 15) | (1 << 13) | (1 << 11) | (1 << 9)))
616 n--;
617 truncm = (21 * n - 1) | 1;
618 freq = ((48000UL << 15) / rate) * n;
620 if (rate >= 24000)
622 if (truncm > 239)
623 truncm = 239;
624 src_write(card, SRC_ADC + SRC_TRUNC, (((239 - truncm) >> 1) << 9) | (n << 4));
626 else
628 if (truncm > 119)
629 truncm = 119;
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);
650 if (rate > 48000)
651 rate = 48000;
652 if (rate < 4000)
653 rate = 4000;
655 if(card->es1370)
657 pci_outl(((pci_inl(SB128_CONTROL, card) & ~DAC2_DIV_MASK) | (DAC2_SRTODIV(rate) << DAC2_DIV_SHIFT)), SB128_CONTROL, card);
659 else
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
687 time. */
689 struct SB128_DATA*
690 AllocDriverData( struct PCIDevice * dev,
691 struct DriverBase* AHIsubBase )
693 struct SB128_DATA* card;
694 UWORD command_word;
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 );
701 if( card == NULL )
703 Req( "Unable to allocate driver structure." );
704 return NULL;
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;
712 #ifdef __AROS__
713 card->interrupt.is_Code = (void(*)(void))&cardinterrupt;
714 #else
715 card->interrupt.is_Code = (void(*)(void))CardInterrupt;
716 #endif
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;
722 #ifdef __AROS__
723 card->playback_interrupt.is_Code = (void(*)(void))&playbackinterrupt;
724 #else
725 card->playback_interrupt.is_Code = (void(*)(void))PlaybackInterrupt;
726 #endif
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;
732 #ifdef __AROS__
733 card->record_interrupt.is_Code = (void(*)(void))&recordinterrupt;
734 #else
735 card->record_interrupt.is_Code = (void(*)(void))RecordInterrupt;
736 #endif
737 card->record_interrupt.is_Data = (APTR) card;
739 card->pci_dev = dev;
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.");
763 return NULL;
766 ahi_pci_add_intserver(&card->interrupt, dev);
767 card->interrupt_added = TRUE;
770 card->card_initialized = TRUE;
771 card->input = 0;
772 card->output = 0;
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);
780 #endif
782 return card;
786 /******************************************************************************
787 ** DriverData deallocation ****************************************************
788 ******************************************************************************/
790 /* And this code used to be in _AHIsub_FreeAudio(). */
792 void
793 FreeDriverData( struct SB128_DATA* card,
794 struct DriverBase* AHIsubBase )
796 if( card != NULL )
798 if( card->pci_dev != NULL )
800 if( card->card_initialized )
802 card_cleanup( card );
805 if( card->pci_master_enabled )
807 UWORD cmd;
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);
820 FreeVec( card );
825 int card_init(struct SB128_DATA *card)
827 struct PCIDevice *dev = (struct PCIDevice *) card->pci_dev;
828 unsigned short cod;
829 unsigned int i;
831 /* Check if the card is an original ES1370 - different code needed */
832 if (inw_config(2, dev) == 0x5000)
833 card->es1370 = TRUE;
834 else
835 card->es1370 = FALSE;
837 /* Different init sequence for the ES1370 */
838 if (card->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 */
871 /* Always on */
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");
887 else
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);
897 Delay(1);
899 /* Assert the AC97 reset, and wait 20ms */
900 pci_outl(CODEC_RESET, SB128_CONTROL, card);
901 Delay(1);
902 /* De-assert delay, and wait 20ms */
903 pci_outl(0x00, SB128_CONTROL, card);
904 Delay(1);
906 DebugPrintF("[SB128] Did AC97 reset.\n");
908 /* Disable the Sample Rate Converter (SRC) */
909 src_ready(card);
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 */
935 src_ready(card);
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);
976 return 0;
980 void card_cleanup(struct SB128_DATA *card)
986 /******************************************************************************
987 ** Misc. **********************************************************************
988 ******************************************************************************/
990 void
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 );
1001 void
1002 RestoreMixerState( struct SB128_DATA* card )
1004 if(card->es1370)
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);
1013 else
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 );
1023 void
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 );
1037 else
1039 if(card->es1370)
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 );
1056 else
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 );
1077 Fixed
1078 Linear2MixerGain( Fixed linear,
1079 UWORD* bits )
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 */
1115 0 /* -oo dB */
1118 int v = 0;
1119 while( linear < gain[ v ] )
1121 ++v;
1124 if( v == 32 )
1126 *bits = 0x8000; /* Mute */
1128 else
1130 *bits = ( v << 8 ) | v;
1132 return gain[ v ];
1135 Fixed
1136 Linear2RecordGain( Fixed linear,
1137 UWORD* bits )
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 */
1157 0 /* -oo dB */
1160 int v = 0;
1162 while( linear < gain[ v ] )
1164 ++v;
1167 if( v == 16 )
1169 *bits = 0x8000; /* Mute */
1171 else
1173 *bits = ( ( 15 - v ) << 8 ) | ( 15 - v );
1176 return gain[ v ];
1180 ULONG
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)
1190 void* address;
1191 unsigned long a;
1193 D(bug("[SB128]: %s()\n", __PRETTY_FUNCTION__);)
1195 address = (void *) AllocVec(size + boundary, (
1196 #if defined(__AROS__) && (__WORDSIZE==64)
1197 MEMF_31BIT |
1198 #endif
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;
1213 return address;
1217 void pci_free_consistent(void* addr)
1219 D(bug("[SB128]: %s()\n", __PRETTY_FUNCTION__);)
1221 FreeVec(addr);
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
1230 unsigned long ctrl;
1232 ctrl = pci_inl(SB128_CONTROL, card);
1233 ctrl &= ( ~(CTRL_DAC2_EN) & ~(CTRL_ADC_EN) );
1235 /* Stop */
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);
1241 return 0UL;
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 );
1256 #endif