2 * Copyright (C) 2006 InnoTek Systemberatung GmbH
4 * This file is part of VirtualBox Open Source Edition (OSE), as
5 * available from http://www.virtualbox.org. This file is free software;
6 * you can redistribute it and/or modify it under the terms of the GNU
7 * General Public License as published by the Free Software Foundation,
8 * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
9 * distribution. VirtualBox OSE is distributed in the hope that it will
10 * be useful, but WITHOUT ANY WARRANTY of any kind.
12 * If you received this file as part of a commercial VirtualBox
13 * distribution, then only the terms of your commercial VirtualBox
14 * license agreement apply instead of the previous paragraph.
16 * Contributions after 2012-01-13 are licensed under the terms of the
17 * GNU GPL, version 2 or (at your option) any later version.
22 #include "audio/audio.h"
28 AC97_Master_Volume_Mute
= 0x02,
29 AC97_Headphone_Volume_Mute
= 0x04,
30 AC97_Master_Volume_Mono_Mute
= 0x06,
31 AC97_Master_Tone_RL
= 0x08,
32 AC97_PC_BEEP_Volume_Mute
= 0x0A,
33 AC97_Phone_Volume_Mute
= 0x0C,
34 AC97_Mic_Volume_Mute
= 0x0E,
35 AC97_Line_In_Volume_Mute
= 0x10,
36 AC97_CD_Volume_Mute
= 0x12,
37 AC97_Video_Volume_Mute
= 0x14,
38 AC97_Aux_Volume_Mute
= 0x16,
39 AC97_PCM_Out_Volume_Mute
= 0x18,
40 AC97_Record_Select
= 0x1A,
41 AC97_Record_Gain_Mute
= 0x1C,
42 AC97_Record_Gain_Mic_Mute
= 0x1E,
43 AC97_General_Purpose
= 0x20,
44 AC97_3D_Control
= 0x22,
45 AC97_AC_97_RESERVED
= 0x24,
46 AC97_Powerdown_Ctrl_Stat
= 0x26,
47 AC97_Extended_Audio_ID
= 0x28,
48 AC97_Extended_Audio_Ctrl_Stat
= 0x2A,
49 AC97_PCM_Front_DAC_Rate
= 0x2C,
50 AC97_PCM_Surround_DAC_Rate
= 0x2E,
51 AC97_PCM_LFE_DAC_Rate
= 0x30,
52 AC97_PCM_LR_ADC_Rate
= 0x32,
53 AC97_MIC_ADC_Rate
= 0x34,
54 AC97_6Ch_Vol_C_LFE_Mute
= 0x36,
55 AC97_6Ch_Vol_L_R_Surround_Mute
= 0x38,
56 AC97_Vendor_Reserved
= 0x58,
57 AC97_Vendor_ID1
= 0x7c,
58 AC97_Vendor_ID2
= 0x7e
62 #define SR_FIFOE 16 /* rwc */
63 #define SR_BCIS 8 /* rwc */
64 #define SR_LVBCI 4 /* rwc */
65 #define SR_CELV 2 /* ro */
66 #define SR_DCH 1 /* ro */
67 #define SR_VALID_MASK ((1 << 5) - 1)
68 #define SR_WCLEAR_MASK (SR_FIFOE | SR_BCIS | SR_LVBCI)
69 #define SR_RO_MASK (SR_DCH | SR_CELV)
70 #define SR_INT_MASK (SR_FIFOE | SR_BCIS | SR_LVBCI)
72 #define CR_IOCE 16 /* rw */
73 #define CR_FEIE 8 /* rw */
74 #define CR_LVBIE 4 /* rw */
75 #define CR_RR 2 /* rw */
76 #define CR_RPBM 1 /* rw */
77 #define CR_VALID_MASK ((1 << 5) - 1)
78 #define CR_DONT_CLEAR_MASK (CR_IOCE | CR_FEIE | CR_LVBIE)
80 #define GC_WR 4 /* rw */
81 #define GC_CR 2 /* rw */
82 #define GC_VALID_MASK ((1 << 6) - 1)
84 #define GS_MD3 (1<<17) /* rw */
85 #define GS_AD3 (1<<16) /* rw */
86 #define GS_RCS (1<<15) /* rwc */
87 #define GS_B3S12 (1<<14) /* ro */
88 #define GS_B2S12 (1<<13) /* ro */
89 #define GS_B1S12 (1<<12) /* ro */
90 #define GS_S1R1 (1<<11) /* rwc */
91 #define GS_S0R1 (1<<10) /* rwc */
92 #define GS_S1CR (1<<9) /* ro */
93 #define GS_S0CR (1<<8) /* ro */
94 #define GS_MINT (1<<7) /* ro */
95 #define GS_POINT (1<<6) /* ro */
96 #define GS_PIINT (1<<5) /* ro */
97 #define GS_RSRVD ((1<<4)|(1<<3))
98 #define GS_MOINT (1<<2) /* ro */
99 #define GS_MIINT (1<<1) /* ro */
100 #define GS_GSCI 1 /* rwc */
101 #define GS_RO_MASK (GS_B3S12| \
112 #define GS_VALID_MASK ((1 << 18) - 1)
113 #define GS_WCLEAR_MASK (GS_RCS|GS_S1R1|GS_S0R1|GS_GSCI)
115 #define BD_IOC (1<<31)
116 #define BD_BUP (1<<30)
121 #define VOL_MASK 0x1f
122 #define MUTE_SHIFT 15
141 typedef struct AC97BusMasterRegs
{
142 uint32_t bdbar
; /* rw 0 */
143 uint8_t civ
; /* ro 0 */
144 uint8_t lvi
; /* rw 0 */
145 uint16_t sr
; /* rw 1 */
146 uint16_t picb
; /* ro 0 */
147 uint8_t piv
; /* ro 0 */
148 uint8_t cr
; /* rw 0 */
149 unsigned int bd_valid
;
153 typedef struct AC97LinkState
{
156 uint32_t use_broken_id
;
161 AC97BusMasterRegs bm_regs
[3];
162 uint8_t mixer_data
[256];
164 SWVoiceOut
*voice_po
;
167 uint8_t silence
[128];
170 MemoryRegion io_nabm
;
179 #define dolog(...) AUD_log ("ac97", __VA_ARGS__)
184 #define MKREGS(prefix, start) \
186 prefix ## _BDBAR = start, \
187 prefix ## _CIV = start + 4, \
188 prefix ## _LVI = start + 5, \
189 prefix ## _SR = start + 6, \
190 prefix ## _PICB = start + 8, \
191 prefix ## _PIV = start + 10, \
192 prefix ## _CR = start + 11 \
202 MKREGS (PI
, PI_INDEX
* 16);
203 MKREGS (PO
, PO_INDEX
* 16);
204 MKREGS (MC
, MC_INDEX
* 16);
212 #define GET_BM(index) (((index) >> 4) & 3)
214 static void po_callback (void *opaque
, int free
);
215 static void pi_callback (void *opaque
, int avail
);
216 static void mc_callback (void *opaque
, int avail
);
218 static void warm_reset (AC97LinkState
*s
)
223 static void cold_reset (AC97LinkState
* s
)
228 static void fetch_bd (AC97LinkState
*s
, AC97BusMasterRegs
*r
)
232 pci_dma_read (&s
->dev
, r
->bdbar
+ r
->civ
* 8, b
, 8);
234 r
->bd
.addr
= le32_to_cpu (*(uint32_t *) &b
[0]) & ~3;
235 r
->bd
.ctl_len
= le32_to_cpu (*(uint32_t *) &b
[4]);
236 r
->picb
= r
->bd
.ctl_len
& 0xffff;
237 dolog ("bd %2d addr=%#x ctl=%#06x len=%#x(%d bytes)\n",
238 r
->civ
, r
->bd
.addr
, r
->bd
.ctl_len
>> 16,
239 r
->bd
.ctl_len
& 0xffff,
240 (r
->bd
.ctl_len
& 0xffff) << 1);
243 static void update_sr (AC97LinkState
*s
, AC97BusMasterRegs
*r
, uint32_t new_sr
)
247 uint32_t new_mask
= new_sr
& SR_INT_MASK
;
248 uint32_t old_mask
= r
->sr
& SR_INT_MASK
;
249 uint32_t masks
[] = {GS_PIINT
, GS_POINT
, GS_MINT
};
251 if (new_mask
^ old_mask
) {
252 /** @todo is IRQ deasserted when only one of status bits is cleared? */
258 if ((new_mask
& SR_LVBCI
) && (r
->cr
& CR_LVBIE
)) {
262 if ((new_mask
& SR_BCIS
) && (r
->cr
& CR_IOCE
)) {
271 dolog ("IOC%d LVB%d sr=%#x event=%d level=%d\n",
272 r
->sr
& SR_BCIS
, r
->sr
& SR_LVBCI
,
280 s
->glob_sta
|= masks
[r
- s
->bm_regs
];
281 dolog ("set irq level=1\n");
282 qemu_set_irq (s
->dev
.irq
[0], 1);
285 s
->glob_sta
&= ~masks
[r
- s
->bm_regs
];
286 dolog ("set irq level=0\n");
287 qemu_set_irq (s
->dev
.irq
[0], 0);
291 static void voice_set_active (AC97LinkState
*s
, int bm_index
, int on
)
295 AUD_set_active_in (s
->voice_pi
, on
);
299 AUD_set_active_out (s
->voice_po
, on
);
303 AUD_set_active_in (s
->voice_mc
, on
);
307 AUD_log ("ac97", "invalid bm_index(%d) in voice_set_active", bm_index
);
312 static void reset_bm_regs (AC97LinkState
*s
, AC97BusMasterRegs
*r
)
314 dolog ("reset_bm_regs\n");
318 /** todo do we need to do that? */
319 update_sr (s
, r
, SR_DCH
);
322 r
->cr
= r
->cr
& CR_DONT_CLEAR_MASK
;
325 voice_set_active (s
, r
- s
->bm_regs
, 0);
326 memset (s
->silence
, 0, sizeof (s
->silence
));
329 static void mixer_store (AC97LinkState
*s
, uint32_t i
, uint16_t v
)
331 if (i
+ 2 > sizeof (s
->mixer_data
)) {
332 dolog ("mixer_store: index %d out of bounds %zd\n",
333 i
, sizeof (s
->mixer_data
));
337 s
->mixer_data
[i
+ 0] = v
& 0xff;
338 s
->mixer_data
[i
+ 1] = v
>> 8;
341 static uint16_t mixer_load (AC97LinkState
*s
, uint32_t i
)
343 uint16_t val
= 0xffff;
345 if (i
+ 2 > sizeof (s
->mixer_data
)) {
346 dolog ("mixer_store: index %d out of bounds %zd\n",
347 i
, sizeof (s
->mixer_data
));
350 val
= s
->mixer_data
[i
+ 0] | (s
->mixer_data
[i
+ 1] << 8);
356 static void open_voice (AC97LinkState
*s
, int index
, int freq
)
358 struct audsettings as
;
362 as
.fmt
= AUD_FMT_S16
;
366 s
->invalid_freq
[index
] = 0;
369 s
->voice_pi
= AUD_open_in (
380 s
->voice_po
= AUD_open_out (
391 s
->voice_mc
= AUD_open_in (
403 s
->invalid_freq
[index
] = freq
;
406 AUD_close_in (&s
->card
, s
->voice_pi
);
411 AUD_close_out (&s
->card
, s
->voice_po
);
416 AUD_close_in (&s
->card
, s
->voice_mc
);
423 static void reset_voices (AC97LinkState
*s
, uint8_t active
[LAST_INDEX
])
427 freq
= mixer_load (s
, AC97_PCM_LR_ADC_Rate
);
428 open_voice (s
, PI_INDEX
, freq
);
429 AUD_set_active_in (s
->voice_pi
, active
[PI_INDEX
]);
431 freq
= mixer_load (s
, AC97_PCM_Front_DAC_Rate
);
432 open_voice (s
, PO_INDEX
, freq
);
433 AUD_set_active_out (s
->voice_po
, active
[PO_INDEX
]);
435 freq
= mixer_load (s
, AC97_MIC_ADC_Rate
);
436 open_voice (s
, MC_INDEX
, freq
);
437 AUD_set_active_in (s
->voice_mc
, active
[MC_INDEX
]);
441 static void set_volume (AC97LinkState
*s
, int index
,
442 audmixerctl_t mt
, uint32_t val
)
444 int mute
= (val
>> MUTE_SHIFT
) & 1;
445 uint8_t rvol
= VOL_MASK
- (val
& VOL_MASK
);
446 uint8_t lvol
= VOL_MASK
- ((val
>> 8) & VOL_MASK
);
447 rvol
= 255 * rvol
/ VOL_MASK
;
448 lvol
= 255 * lvol
/ VOL_MASK
;
451 if (index
== AC97_Master_Volume_Mute
) {
452 AUD_set_volume_out (s
->voice_po
, mute
, lvol
, rvol
);
455 AUD_set_volume (mt
, &mute
, &lvol
, &rvol
);
458 AUD_set_volume (mt
, &mute
, &lvol
, &rvol
);
461 rvol
= VOL_MASK
- ((VOL_MASK
* rvol
) / 255);
462 lvol
= VOL_MASK
- ((VOL_MASK
* lvol
) / 255);
463 mixer_store (s
, index
, val
);
466 static audrecsource_t
ac97_to_aud_record_source (uint8_t i
)
476 return AUD_REC_VIDEO
;
482 return AUD_REC_LINE_IN
;
485 return AUD_REC_PHONE
;
488 dolog ("Unknown record source %d, using MIC\n", i
);
493 static uint8_t aud_to_ac97_record_source (audrecsource_t rs
)
508 case AUD_REC_LINE_IN
:
515 dolog ("Unknown audio recording source %d using MIC\n", rs
);
520 static void record_select (AC97LinkState
*s
, uint32_t val
)
522 uint8_t rs
= val
& REC_MASK
;
523 uint8_t ls
= (val
>> 8) & REC_MASK
;
524 audrecsource_t ars
= ac97_to_aud_record_source (rs
);
525 audrecsource_t als
= ac97_to_aud_record_source (ls
);
526 AUD_set_record_source (&als
, &ars
);
527 rs
= aud_to_ac97_record_source (ars
);
528 ls
= aud_to_ac97_record_source (als
);
529 mixer_store (s
, AC97_Record_Select
, rs
| (ls
<< 8));
533 static void mixer_reset (AC97LinkState
*s
)
535 uint8_t active
[LAST_INDEX
];
537 dolog ("mixer_reset\n");
538 memset (s
->mixer_data
, 0, sizeof (s
->mixer_data
));
539 memset (active
, 0, sizeof (active
));
540 mixer_store (s
, AC97_Reset
, 0x0000); /* 6940 */
541 mixer_store (s
, AC97_Master_Volume_Mono_Mute
, 0x8000);
542 mixer_store (s
, AC97_PC_BEEP_Volume_Mute
, 0x0000);
544 mixer_store (s
, AC97_Phone_Volume_Mute
, 0x8008);
545 mixer_store (s
, AC97_Mic_Volume_Mute
, 0x8008);
546 mixer_store (s
, AC97_CD_Volume_Mute
, 0x8808);
547 mixer_store (s
, AC97_Aux_Volume_Mute
, 0x8808);
548 mixer_store (s
, AC97_Record_Gain_Mic_Mute
, 0x8000);
549 mixer_store (s
, AC97_General_Purpose
, 0x0000);
550 mixer_store (s
, AC97_3D_Control
, 0x0000);
551 mixer_store (s
, AC97_Powerdown_Ctrl_Stat
, 0x000f);
554 * Sigmatel 9700 (STAC9700)
556 mixer_store (s
, AC97_Vendor_ID1
, 0x8384);
557 mixer_store (s
, AC97_Vendor_ID2
, 0x7600); /* 7608 */
559 mixer_store (s
, AC97_Extended_Audio_ID
, 0x0809);
560 mixer_store (s
, AC97_Extended_Audio_Ctrl_Stat
, 0x0009);
561 mixer_store (s
, AC97_PCM_Front_DAC_Rate
, 0xbb80);
562 mixer_store (s
, AC97_PCM_Surround_DAC_Rate
, 0xbb80);
563 mixer_store (s
, AC97_PCM_LFE_DAC_Rate
, 0xbb80);
564 mixer_store (s
, AC97_PCM_LR_ADC_Rate
, 0xbb80);
565 mixer_store (s
, AC97_MIC_ADC_Rate
, 0xbb80);
568 record_select (s
, 0);
569 set_volume (s
, AC97_Master_Volume_Mute
, AUD_MIXER_VOLUME
, 0x8000);
570 set_volume (s
, AC97_PCM_Out_Volume_Mute
, AUD_MIXER_PCM
, 0x8808);
571 set_volume (s
, AC97_Line_In_Volume_Mute
, AUD_MIXER_LINE_IN
, 0x8808);
573 reset_voices (s
, active
);
580 static uint32_t nam_readb (void *opaque
, uint32_t addr
)
582 AC97LinkState
*s
= opaque
;
583 dolog ("U nam readb %#x\n", addr
);
588 static uint32_t nam_readw (void *opaque
, uint32_t addr
)
590 AC97LinkState
*s
= opaque
;
592 uint32_t index
= addr
;
594 val
= mixer_load (s
, index
);
598 static uint32_t nam_readl (void *opaque
, uint32_t addr
)
600 AC97LinkState
*s
= opaque
;
601 dolog ("U nam readl %#x\n", addr
);
610 static void nam_writeb (void *opaque
, uint32_t addr
, uint32_t val
)
612 AC97LinkState
*s
= opaque
;
613 dolog ("U nam writeb %#x <- %#x\n", addr
, val
);
617 static void nam_writew (void *opaque
, uint32_t addr
, uint32_t val
)
619 AC97LinkState
*s
= opaque
;
620 uint32_t index
= addr
;
626 case AC97_Powerdown_Ctrl_Stat
:
628 val
|= mixer_load (s
, index
) & 0xf;
629 mixer_store (s
, index
, val
);
632 case AC97_Master_Volume_Mute
:
633 set_volume (s
, index
, AUD_MIXER_VOLUME
, val
);
635 case AC97_PCM_Out_Volume_Mute
:
636 set_volume (s
, index
, AUD_MIXER_PCM
, val
);
638 case AC97_Line_In_Volume_Mute
:
639 set_volume (s
, index
, AUD_MIXER_LINE_IN
, val
);
641 case AC97_Record_Select
:
642 record_select (s
, val
);
645 case AC97_Vendor_ID1
:
646 case AC97_Vendor_ID2
:
647 dolog ("Attempt to write vendor ID to %#x\n", val
);
649 case AC97_Extended_Audio_ID
:
650 dolog ("Attempt to write extended audio ID to %#x\n", val
);
652 case AC97_Extended_Audio_Ctrl_Stat
:
653 if (!(val
& EACS_VRA
)) {
654 mixer_store (s
, AC97_PCM_Front_DAC_Rate
, 0xbb80);
655 mixer_store (s
, AC97_PCM_LR_ADC_Rate
, 0xbb80);
656 open_voice (s
, PI_INDEX
, 48000);
657 open_voice (s
, PO_INDEX
, 48000);
659 if (!(val
& EACS_VRM
)) {
660 mixer_store (s
, AC97_MIC_ADC_Rate
, 0xbb80);
661 open_voice (s
, MC_INDEX
, 48000);
663 dolog ("Setting extended audio control to %#x\n", val
);
664 mixer_store (s
, AC97_Extended_Audio_Ctrl_Stat
, val
);
666 case AC97_PCM_Front_DAC_Rate
:
667 if (mixer_load (s
, AC97_Extended_Audio_Ctrl_Stat
) & EACS_VRA
) {
668 mixer_store (s
, index
, val
);
669 dolog ("Set front DAC rate to %d\n", val
);
670 open_voice (s
, PO_INDEX
, val
);
673 dolog ("Attempt to set front DAC rate to %d, "
674 "but VRA is not set\n",
678 case AC97_MIC_ADC_Rate
:
679 if (mixer_load (s
, AC97_Extended_Audio_Ctrl_Stat
) & EACS_VRM
) {
680 mixer_store (s
, index
, val
);
681 dolog ("Set MIC ADC rate to %d\n", val
);
682 open_voice (s
, MC_INDEX
, val
);
685 dolog ("Attempt to set MIC ADC rate to %d, "
686 "but VRM is not set\n",
690 case AC97_PCM_LR_ADC_Rate
:
691 if (mixer_load (s
, AC97_Extended_Audio_Ctrl_Stat
) & EACS_VRA
) {
692 mixer_store (s
, index
, val
);
693 dolog ("Set front LR ADC rate to %d\n", val
);
694 open_voice (s
, PI_INDEX
, val
);
697 dolog ("Attempt to set LR ADC rate to %d, but VRA is not set\n",
702 dolog ("U nam writew %#x <- %#x\n", addr
, val
);
703 mixer_store (s
, index
, val
);
708 static void nam_writel (void *opaque
, uint32_t addr
, uint32_t val
)
710 AC97LinkState
*s
= opaque
;
711 dolog ("U nam writel %#x <- %#x\n", addr
, val
);
716 * Native audio bus master
719 static uint32_t nabm_readb (void *opaque
, uint32_t addr
)
721 AC97LinkState
*s
= opaque
;
722 AC97BusMasterRegs
*r
= NULL
;
723 uint32_t index
= addr
;
728 dolog ("CAS %d\n", s
->cas
);
735 r
= &s
->bm_regs
[GET_BM (index
)];
737 dolog ("CIV[%d] -> %#x\n", GET_BM (index
), val
);
742 r
= &s
->bm_regs
[GET_BM (index
)];
744 dolog ("LVI[%d] -> %#x\n", GET_BM (index
), val
);
749 r
= &s
->bm_regs
[GET_BM (index
)];
751 dolog ("PIV[%d] -> %#x\n", GET_BM (index
), val
);
756 r
= &s
->bm_regs
[GET_BM (index
)];
758 dolog ("CR[%d] -> %#x\n", GET_BM (index
), val
);
763 r
= &s
->bm_regs
[GET_BM (index
)];
765 dolog ("SRb[%d] -> %#x\n", GET_BM (index
), val
);
768 dolog ("U nabm readb %#x -> %#x\n", addr
, val
);
774 static uint32_t nabm_readw (void *opaque
, uint32_t addr
)
776 AC97LinkState
*s
= opaque
;
777 AC97BusMasterRegs
*r
= NULL
;
778 uint32_t index
= addr
;
785 r
= &s
->bm_regs
[GET_BM (index
)];
787 dolog ("SR[%d] -> %#x\n", GET_BM (index
), val
);
792 r
= &s
->bm_regs
[GET_BM (index
)];
794 dolog ("PICB[%d] -> %#x\n", GET_BM (index
), val
);
797 dolog ("U nabm readw %#x -> %#x\n", addr
, val
);
803 static uint32_t nabm_readl (void *opaque
, uint32_t addr
)
805 AC97LinkState
*s
= opaque
;
806 AC97BusMasterRegs
*r
= NULL
;
807 uint32_t index
= addr
;
814 r
= &s
->bm_regs
[GET_BM (index
)];
816 dolog ("BMADDR[%d] -> %#x\n", GET_BM (index
), val
);
821 r
= &s
->bm_regs
[GET_BM (index
)];
822 val
= r
->civ
| (r
->lvi
<< 8) | (r
->sr
<< 16);
823 dolog ("CIV LVI SR[%d] -> %#x, %#x, %#x\n", GET_BM (index
),
824 r
->civ
, r
->lvi
, r
->sr
);
829 r
= &s
->bm_regs
[GET_BM (index
)];
830 val
= r
->picb
| (r
->piv
<< 16) | (r
->cr
<< 24);
831 dolog ("PICB PIV CR[%d] -> %#x %#x %#x %#x\n", GET_BM (index
),
832 val
, r
->picb
, r
->piv
, r
->cr
);
836 dolog ("glob_cnt -> %#x\n", val
);
839 val
= s
->glob_sta
| GS_S0CR
;
840 dolog ("glob_sta -> %#x\n", val
);
843 dolog ("U nabm readl %#x -> %#x\n", addr
, val
);
850 * Native audio bus master
853 static void nabm_writeb (void *opaque
, uint32_t addr
, uint32_t val
)
855 AC97LinkState
*s
= opaque
;
856 AC97BusMasterRegs
*r
= NULL
;
857 uint32_t index
= addr
;
862 r
= &s
->bm_regs
[GET_BM (index
)];
863 if ((r
->cr
& CR_RPBM
) && (r
->sr
& SR_DCH
)) {
864 r
->sr
&= ~(SR_DCH
| SR_CELV
);
866 r
->piv
= (r
->piv
+ 1) % 32;
870 dolog ("LVI[%d] <- %#x\n", GET_BM (index
), val
);
875 r
= &s
->bm_regs
[GET_BM (index
)];
877 reset_bm_regs (s
, r
);
880 r
->cr
= val
& CR_VALID_MASK
;
881 if (!(r
->cr
& CR_RPBM
)) {
882 voice_set_active (s
, r
- s
->bm_regs
, 0);
887 r
->piv
= (r
->piv
+ 1) % 32;
890 voice_set_active (s
, r
- s
->bm_regs
, 1);
893 dolog ("CR[%d] <- %#x (cr %#x)\n", GET_BM (index
), val
, r
->cr
);
898 r
= &s
->bm_regs
[GET_BM (index
)];
899 r
->sr
|= val
& ~(SR_RO_MASK
| SR_WCLEAR_MASK
);
900 update_sr (s
, r
, r
->sr
& ~(val
& SR_WCLEAR_MASK
));
901 dolog ("SR[%d] <- %#x (sr %#x)\n", GET_BM (index
), val
, r
->sr
);
904 dolog ("U nabm writeb %#x <- %#x\n", addr
, val
);
909 static void nabm_writew (void *opaque
, uint32_t addr
, uint32_t val
)
911 AC97LinkState
*s
= opaque
;
912 AC97BusMasterRegs
*r
= NULL
;
913 uint32_t index
= addr
;
918 r
= &s
->bm_regs
[GET_BM (index
)];
919 r
->sr
|= val
& ~(SR_RO_MASK
| SR_WCLEAR_MASK
);
920 update_sr (s
, r
, r
->sr
& ~(val
& SR_WCLEAR_MASK
));
921 dolog ("SR[%d] <- %#x (sr %#x)\n", GET_BM (index
), val
, r
->sr
);
924 dolog ("U nabm writew %#x <- %#x\n", addr
, val
);
929 static void nabm_writel (void *opaque
, uint32_t addr
, uint32_t val
)
931 AC97LinkState
*s
= opaque
;
932 AC97BusMasterRegs
*r
= NULL
;
933 uint32_t index
= addr
;
938 r
= &s
->bm_regs
[GET_BM (index
)];
940 dolog ("BDBAR[%d] <- %#x (bdbar %#x)\n",
941 GET_BM (index
), val
, r
->bdbar
);
948 if (!(val
& (GC_WR
| GC_CR
)))
949 s
->glob_cnt
= val
& GC_VALID_MASK
;
950 dolog ("glob_cnt <- %#x (glob_cnt %#x)\n", val
, s
->glob_cnt
);
953 s
->glob_sta
&= ~(val
& GS_WCLEAR_MASK
);
954 s
->glob_sta
|= (val
& ~(GS_WCLEAR_MASK
| GS_RO_MASK
)) & GS_VALID_MASK
;
955 dolog ("glob_sta <- %#x (glob_sta %#x)\n", val
, s
->glob_sta
);
958 dolog ("U nabm writel %#x <- %#x\n", addr
, val
);
963 static int write_audio (AC97LinkState
*s
, AC97BusMasterRegs
*r
,
966 uint8_t tmpbuf
[4096];
967 uint32_t addr
= r
->bd
.addr
;
968 uint32_t temp
= r
->picb
<< 1;
969 uint32_t written
= 0;
971 temp
= audio_MIN (temp
, max
);
980 to_copy
= audio_MIN (temp
, sizeof (tmpbuf
));
981 pci_dma_read (&s
->dev
, addr
, tmpbuf
, to_copy
);
982 copied
= AUD_write (s
->voice_po
, tmpbuf
, to_copy
);
983 dolog ("write_audio max=%x to_copy=%x copied=%x\n",
984 max
, to_copy
, copied
);
1000 s
->last_samp
= *(uint32_t *) &tmpbuf
[to_copy
- 4];
1008 static void write_bup (AC97LinkState
*s
, int elapsed
)
1010 dolog ("write_bup\n");
1011 if (!(s
->bup_flag
& BUP_SET
)) {
1012 if (s
->bup_flag
& BUP_LAST
) {
1014 uint8_t *p
= s
->silence
;
1015 for (i
= 0; i
< sizeof (s
->silence
) / 4; i
++, p
+= 4) {
1016 *(uint32_t *) p
= s
->last_samp
;
1020 memset (s
->silence
, 0, sizeof (s
->silence
));
1022 s
->bup_flag
|= BUP_SET
;
1026 int temp
= audio_MIN (elapsed
, sizeof (s
->silence
));
1028 int copied
= AUD_write (s
->voice_po
, s
->silence
, temp
);
1037 static int read_audio (AC97LinkState
*s
, AC97BusMasterRegs
*r
,
1040 uint8_t tmpbuf
[4096];
1041 uint32_t addr
= r
->bd
.addr
;
1042 uint32_t temp
= r
->picb
<< 1;
1045 SWVoiceIn
*voice
= (r
- s
->bm_regs
) == MC_INDEX
? s
->voice_mc
: s
->voice_pi
;
1047 temp
= audio_MIN (temp
, max
);
1056 to_copy
= audio_MIN (temp
, sizeof (tmpbuf
));
1057 acquired
= AUD_read (voice
, tmpbuf
, to_copy
);
1062 pci_dma_write (&s
->dev
, addr
, tmpbuf
, acquired
);
1072 static void transfer_audio (AC97LinkState
*s
, int index
, int elapsed
)
1074 AC97BusMasterRegs
*r
= &s
->bm_regs
[index
];
1077 if (s
->invalid_freq
[index
]) {
1078 AUD_log ("ac97", "attempt to use voice %d with invalid frequency %d\n",
1079 index
, s
->invalid_freq
[index
]);
1083 if (r
->sr
& SR_DCH
) {
1084 if (r
->cr
& CR_RPBM
) {
1087 write_bup (s
, elapsed
);
1094 while ((elapsed
>> 1) && !stop
) {
1098 dolog ("invalid bd\n");
1103 dolog ("fresh bd %d is empty %#x %#x\n",
1104 r
->civ
, r
->bd
.addr
, r
->bd
.ctl_len
);
1105 if (r
->civ
== r
->lvi
) {
1106 r
->sr
|= SR_DCH
; /* CELV? */
1112 r
->piv
= (r
->piv
+ 1) % 32;
1119 temp
= write_audio (s
, r
, elapsed
, &stop
);
1121 r
->picb
-= (temp
>> 1);
1126 temp
= read_audio (s
, r
, elapsed
, &stop
);
1128 r
->picb
-= (temp
>> 1);
1133 uint32_t new_sr
= r
->sr
& ~SR_CELV
;
1135 if (r
->bd
.ctl_len
& BD_IOC
) {
1139 if (r
->civ
== r
->lvi
) {
1140 dolog ("Underrun civ (%d) == lvi (%d)\n", r
->civ
, r
->lvi
);
1142 new_sr
|= SR_LVBCI
| SR_DCH
| SR_CELV
;
1144 s
->bup_flag
= (r
->bd
.ctl_len
& BD_BUP
) ? BUP_LAST
: 0;
1148 r
->piv
= (r
->piv
+ 1) % 32;
1152 update_sr (s
, r
, new_sr
);
1157 static void pi_callback (void *opaque
, int avail
)
1159 transfer_audio (opaque
, PI_INDEX
, avail
);
1162 static void mc_callback (void *opaque
, int avail
)
1164 transfer_audio (opaque
, MC_INDEX
, avail
);
1167 static void po_callback (void *opaque
, int free
)
1169 transfer_audio (opaque
, PO_INDEX
, free
);
1172 static const VMStateDescription vmstate_ac97_bm_regs
= {
1173 .name
= "ac97_bm_regs",
1175 .minimum_version_id
= 1,
1176 .minimum_version_id_old
= 1,
1177 .fields
= (VMStateField
[]) {
1178 VMSTATE_UINT32(bdbar
, AC97BusMasterRegs
),
1179 VMSTATE_UINT8(civ
, AC97BusMasterRegs
),
1180 VMSTATE_UINT8(lvi
, AC97BusMasterRegs
),
1181 VMSTATE_UINT16(sr
, AC97BusMasterRegs
),
1182 VMSTATE_UINT16(picb
, AC97BusMasterRegs
),
1183 VMSTATE_UINT8(piv
, AC97BusMasterRegs
),
1184 VMSTATE_UINT8(cr
, AC97BusMasterRegs
),
1185 VMSTATE_UINT32(bd_valid
, AC97BusMasterRegs
),
1186 VMSTATE_UINT32(bd
.addr
, AC97BusMasterRegs
),
1187 VMSTATE_UINT32(bd
.ctl_len
, AC97BusMasterRegs
),
1188 VMSTATE_END_OF_LIST()
1192 static int ac97_post_load (void *opaque
, int version_id
)
1194 uint8_t active
[LAST_INDEX
];
1195 AC97LinkState
*s
= opaque
;
1198 record_select (s
, mixer_load (s
, AC97_Record_Select
));
1199 #define V_(a, b) set_volume (s, a, b, mixer_load (s, a))
1200 V_ (AC97_Master_Volume_Mute
, AUD_MIXER_VOLUME
);
1201 V_ (AC97_PCM_Out_Volume_Mute
, AUD_MIXER_PCM
);
1202 V_ (AC97_Line_In_Volume_Mute
, AUD_MIXER_LINE_IN
);
1205 active
[PI_INDEX
] = !!(s
->bm_regs
[PI_INDEX
].cr
& CR_RPBM
);
1206 active
[PO_INDEX
] = !!(s
->bm_regs
[PO_INDEX
].cr
& CR_RPBM
);
1207 active
[MC_INDEX
] = !!(s
->bm_regs
[MC_INDEX
].cr
& CR_RPBM
);
1208 reset_voices (s
, active
);
1215 static bool is_version_2 (void *opaque
, int version_id
)
1217 return version_id
== 2;
1220 static const VMStateDescription vmstate_ac97
= {
1223 .minimum_version_id
= 2,
1224 .minimum_version_id_old
= 2,
1225 .post_load
= ac97_post_load
,
1226 .fields
= (VMStateField
[]) {
1227 VMSTATE_PCI_DEVICE(dev
, AC97LinkState
),
1228 VMSTATE_UINT32(glob_cnt
, AC97LinkState
),
1229 VMSTATE_UINT32(glob_sta
, AC97LinkState
),
1230 VMSTATE_UINT32(cas
, AC97LinkState
),
1231 VMSTATE_STRUCT_ARRAY(bm_regs
, AC97LinkState
, 3, 1,
1232 vmstate_ac97_bm_regs
, AC97BusMasterRegs
),
1233 VMSTATE_BUFFER(mixer_data
, AC97LinkState
),
1234 VMSTATE_UNUSED_TEST(is_version_2
, 3),
1235 VMSTATE_END_OF_LIST()
1239 static const MemoryRegionPortio nam_portio
[] = {
1240 { 0, 256 * 1, 1, .read
= nam_readb
, },
1241 { 0, 256 * 2, 2, .read
= nam_readw
, },
1242 { 0, 256 * 4, 4, .read
= nam_readl
, },
1243 { 0, 256 * 1, 1, .write
= nam_writeb
, },
1244 { 0, 256 * 2, 2, .write
= nam_writew
, },
1245 { 0, 256 * 4, 4, .write
= nam_writel
, },
1246 PORTIO_END_OF_LIST(),
1249 static const MemoryRegionOps ac97_io_nam_ops
= {
1250 .old_portio
= nam_portio
,
1253 static const MemoryRegionPortio nabm_portio
[] = {
1254 { 0, 64 * 1, 1, .read
= nabm_readb
, },
1255 { 0, 64 * 2, 2, .read
= nabm_readw
, },
1256 { 0, 64 * 4, 4, .read
= nabm_readl
, },
1257 { 0, 64 * 1, 1, .write
= nabm_writeb
, },
1258 { 0, 64 * 2, 2, .write
= nabm_writew
, },
1259 { 0, 64 * 4, 4, .write
= nabm_writel
, },
1260 PORTIO_END_OF_LIST()
1263 static const MemoryRegionOps ac97_io_nabm_ops
= {
1264 .old_portio
= nabm_portio
,
1267 static void ac97_on_reset (void *opaque
)
1269 AC97LinkState
*s
= opaque
;
1271 reset_bm_regs (s
, &s
->bm_regs
[0]);
1272 reset_bm_regs (s
, &s
->bm_regs
[1]);
1273 reset_bm_regs (s
, &s
->bm_regs
[2]);
1276 * Reset the mixer too. The Windows XP driver seems to rely on
1277 * this. At least it wants to read the vendor id before it resets
1278 * the codec manually.
1283 static int ac97_initfn (PCIDevice
*dev
)
1285 AC97LinkState
*s
= DO_UPCAST (AC97LinkState
, dev
, dev
);
1286 uint8_t *c
= s
->dev
.config
;
1288 /* TODO: no need to override */
1289 c
[PCI_COMMAND
] = 0x00; /* pcicmd pci command rw, ro */
1290 c
[PCI_COMMAND
+ 1] = 0x00;
1293 c
[PCI_STATUS
] = PCI_STATUS_FAST_BACK
; /* pcists pci status rwc, ro */
1294 c
[PCI_STATUS
+ 1] = PCI_STATUS_DEVSEL_MEDIUM
>> 8;
1296 c
[PCI_CLASS_PROG
] = 0x00; /* pi programming interface ro */
1298 /* TODO set when bar is registered. no need to override. */
1299 /* nabmar native audio mixer base address rw */
1300 c
[PCI_BASE_ADDRESS_0
] = PCI_BASE_ADDRESS_SPACE_IO
;
1301 c
[PCI_BASE_ADDRESS_0
+ 1] = 0x00;
1302 c
[PCI_BASE_ADDRESS_0
+ 2] = 0x00;
1303 c
[PCI_BASE_ADDRESS_0
+ 3] = 0x00;
1305 /* TODO set when bar is registered. no need to override. */
1306 /* nabmbar native audio bus mastering base address rw */
1307 c
[PCI_BASE_ADDRESS_0
+ 4] = PCI_BASE_ADDRESS_SPACE_IO
;
1308 c
[PCI_BASE_ADDRESS_0
+ 5] = 0x00;
1309 c
[PCI_BASE_ADDRESS_0
+ 6] = 0x00;
1310 c
[PCI_BASE_ADDRESS_0
+ 7] = 0x00;
1312 if (s
->use_broken_id
) {
1313 c
[PCI_SUBSYSTEM_VENDOR_ID
] = 0x86;
1314 c
[PCI_SUBSYSTEM_VENDOR_ID
+ 1] = 0x80;
1315 c
[PCI_SUBSYSTEM_ID
] = 0x00;
1316 c
[PCI_SUBSYSTEM_ID
+ 1] = 0x00;
1319 c
[PCI_INTERRUPT_LINE
] = 0x00; /* intr_ln interrupt line rw */
1320 c
[PCI_INTERRUPT_PIN
] = 0x01; /* intr_pn interrupt pin ro */
1322 memory_region_init_io (&s
->io_nam
, &ac97_io_nam_ops
, s
, "ac97-nam", 1024);
1323 memory_region_init_io (&s
->io_nabm
, &ac97_io_nabm_ops
, s
, "ac97-nabm", 256);
1324 pci_register_bar (&s
->dev
, 0, PCI_BASE_ADDRESS_SPACE_IO
, &s
->io_nam
);
1325 pci_register_bar (&s
->dev
, 1, PCI_BASE_ADDRESS_SPACE_IO
, &s
->io_nabm
);
1326 qemu_register_reset (ac97_on_reset
, s
);
1327 AUD_register_card ("ac97", &s
->card
);
1332 static int ac97_exitfn (PCIDevice
*dev
)
1334 AC97LinkState
*s
= DO_UPCAST (AC97LinkState
, dev
, dev
);
1336 memory_region_destroy (&s
->io_nam
);
1337 memory_region_destroy (&s
->io_nabm
);
1341 int ac97_init (PCIBus
*bus
)
1343 pci_create_simple (bus
, -1, "AC97");
1347 static PCIDeviceInfo ac97_info
= {
1348 .qdev
.name
= "AC97",
1349 .qdev
.desc
= "Intel 82801AA AC97 Audio",
1350 .qdev
.size
= sizeof (AC97LinkState
),
1351 .qdev
.vmsd
= &vmstate_ac97
,
1352 .init
= ac97_initfn
,
1353 .exit
= ac97_exitfn
,
1354 .vendor_id
= PCI_VENDOR_ID_INTEL
,
1355 .device_id
= PCI_DEVICE_ID_INTEL_82801AA_5
,
1357 .class_id
= PCI_CLASS_MULTIMEDIA_AUDIO
,
1358 .qdev
.props
= (Property
[]) {
1359 DEFINE_PROP_UINT32("use_broken_id", AC97LinkState
, use_broken_id
, 0),
1360 DEFINE_PROP_END_OF_LIST(),
1364 static void ac97_register (void)
1366 pci_qdev_register (&ac97_info
);
1368 device_init (ac97_register
);