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 MUTE_SHIFT 15
140 typedef struct AC97BusMasterRegs
{
141 uint32_t bdbar
; /* rw 0 */
142 uint8_t civ
; /* ro 0 */
143 uint8_t lvi
; /* rw 0 */
144 uint16_t sr
; /* rw 1 */
145 uint16_t picb
; /* ro 0 */
146 uint8_t piv
; /* ro 0 */
147 uint8_t cr
; /* rw 0 */
148 unsigned int bd_valid
;
152 typedef struct AC97LinkState
{
155 uint32_t use_broken_id
;
160 AC97BusMasterRegs bm_regs
[3];
161 uint8_t mixer_data
[256];
163 SWVoiceOut
*voice_po
;
166 uint8_t silence
[128];
169 MemoryRegion io_nabm
;
178 #define dolog(...) AUD_log ("ac97", __VA_ARGS__)
183 #define MKREGS(prefix, start) \
185 prefix ## _BDBAR = start, \
186 prefix ## _CIV = start + 4, \
187 prefix ## _LVI = start + 5, \
188 prefix ## _SR = start + 6, \
189 prefix ## _PICB = start + 8, \
190 prefix ## _PIV = start + 10, \
191 prefix ## _CR = start + 11 \
201 MKREGS (PI
, PI_INDEX
* 16);
202 MKREGS (PO
, PO_INDEX
* 16);
203 MKREGS (MC
, MC_INDEX
* 16);
211 #define GET_BM(index) (((index) >> 4) & 3)
213 static void po_callback (void *opaque
, int free
);
214 static void pi_callback (void *opaque
, int avail
);
215 static void mc_callback (void *opaque
, int avail
);
217 static void warm_reset (AC97LinkState
*s
)
222 static void cold_reset (AC97LinkState
* s
)
227 static void fetch_bd (AC97LinkState
*s
, AC97BusMasterRegs
*r
)
231 pci_dma_read (&s
->dev
, r
->bdbar
+ r
->civ
* 8, b
, 8);
233 r
->bd
.addr
= le32_to_cpu (*(uint32_t *) &b
[0]) & ~3;
234 r
->bd
.ctl_len
= le32_to_cpu (*(uint32_t *) &b
[4]);
235 r
->picb
= r
->bd
.ctl_len
& 0xffff;
236 dolog ("bd %2d addr=%#x ctl=%#06x len=%#x(%d bytes)\n",
237 r
->civ
, r
->bd
.addr
, r
->bd
.ctl_len
>> 16,
238 r
->bd
.ctl_len
& 0xffff,
239 (r
->bd
.ctl_len
& 0xffff) << 1);
242 static void update_sr (AC97LinkState
*s
, AC97BusMasterRegs
*r
, uint32_t new_sr
)
246 uint32_t new_mask
= new_sr
& SR_INT_MASK
;
247 uint32_t old_mask
= r
->sr
& SR_INT_MASK
;
248 uint32_t masks
[] = {GS_PIINT
, GS_POINT
, GS_MINT
};
250 if (new_mask
^ old_mask
) {
251 /** @todo is IRQ deasserted when only one of status bits is cleared? */
257 if ((new_mask
& SR_LVBCI
) && (r
->cr
& CR_LVBIE
)) {
261 if ((new_mask
& SR_BCIS
) && (r
->cr
& CR_IOCE
)) {
270 dolog ("IOC%d LVB%d sr=%#x event=%d level=%d\n",
271 r
->sr
& SR_BCIS
, r
->sr
& SR_LVBCI
,
279 s
->glob_sta
|= masks
[r
- s
->bm_regs
];
280 dolog ("set irq level=1\n");
281 qemu_set_irq (s
->dev
.irq
[0], 1);
284 s
->glob_sta
&= ~masks
[r
- s
->bm_regs
];
285 dolog ("set irq level=0\n");
286 qemu_set_irq (s
->dev
.irq
[0], 0);
290 static void voice_set_active (AC97LinkState
*s
, int bm_index
, int on
)
294 AUD_set_active_in (s
->voice_pi
, on
);
298 AUD_set_active_out (s
->voice_po
, on
);
302 AUD_set_active_in (s
->voice_mc
, on
);
306 AUD_log ("ac97", "invalid bm_index(%d) in voice_set_active", bm_index
);
311 static void reset_bm_regs (AC97LinkState
*s
, AC97BusMasterRegs
*r
)
313 dolog ("reset_bm_regs\n");
317 /** todo do we need to do that? */
318 update_sr (s
, r
, SR_DCH
);
321 r
->cr
= r
->cr
& CR_DONT_CLEAR_MASK
;
324 voice_set_active (s
, r
- s
->bm_regs
, 0);
325 memset (s
->silence
, 0, sizeof (s
->silence
));
328 static void mixer_store (AC97LinkState
*s
, uint32_t i
, uint16_t v
)
330 if (i
+ 2 > sizeof (s
->mixer_data
)) {
331 dolog ("mixer_store: index %d out of bounds %zd\n",
332 i
, sizeof (s
->mixer_data
));
336 s
->mixer_data
[i
+ 0] = v
& 0xff;
337 s
->mixer_data
[i
+ 1] = v
>> 8;
340 static uint16_t mixer_load (AC97LinkState
*s
, uint32_t i
)
342 uint16_t val
= 0xffff;
344 if (i
+ 2 > sizeof (s
->mixer_data
)) {
345 dolog ("mixer_store: index %d out of bounds %zd\n",
346 i
, sizeof (s
->mixer_data
));
349 val
= s
->mixer_data
[i
+ 0] | (s
->mixer_data
[i
+ 1] << 8);
355 static void open_voice (AC97LinkState
*s
, int index
, int freq
)
357 struct audsettings as
;
361 as
.fmt
= AUD_FMT_S16
;
365 s
->invalid_freq
[index
] = 0;
368 s
->voice_pi
= AUD_open_in (
379 s
->voice_po
= AUD_open_out (
390 s
->voice_mc
= AUD_open_in (
402 s
->invalid_freq
[index
] = freq
;
405 AUD_close_in (&s
->card
, s
->voice_pi
);
410 AUD_close_out (&s
->card
, s
->voice_po
);
415 AUD_close_in (&s
->card
, s
->voice_mc
);
422 static void reset_voices (AC97LinkState
*s
, uint8_t active
[LAST_INDEX
])
426 freq
= mixer_load (s
, AC97_PCM_LR_ADC_Rate
);
427 open_voice (s
, PI_INDEX
, freq
);
428 AUD_set_active_in (s
->voice_pi
, active
[PI_INDEX
]);
430 freq
= mixer_load (s
, AC97_PCM_Front_DAC_Rate
);
431 open_voice (s
, PO_INDEX
, freq
);
432 AUD_set_active_out (s
->voice_po
, active
[PO_INDEX
]);
434 freq
= mixer_load (s
, AC97_MIC_ADC_Rate
);
435 open_voice (s
, MC_INDEX
, freq
);
436 AUD_set_active_in (s
->voice_mc
, active
[MC_INDEX
]);
439 static void get_volume (uint16_t vol
, uint16_t mask
, int inverse
,
440 int *mute
, uint8_t *lvol
, uint8_t *rvol
)
442 *mute
= (vol
>> MUTE_SHIFT
) & 1;
443 *rvol
= (255 * (vol
& mask
)) / mask
;
444 *lvol
= (255 * ((vol
>> 8) & mask
)) / mask
;
452 static void update_combined_volume_out (AC97LinkState
*s
)
454 uint8_t lvol
, rvol
, plvol
, prvol
;
457 get_volume (mixer_load (s
, AC97_Master_Volume_Mute
), 0x3f, 1,
458 &mute
, &lvol
, &rvol
);
459 /* FIXME: should be 1f according to spec */
460 get_volume (mixer_load (s
, AC97_PCM_Out_Volume_Mute
), 0x3f, 1,
461 &pmute
, &plvol
, &prvol
);
464 lvol
= (lvol
* plvol
) / 255;
465 rvol
= (rvol
* prvol
) / 255;
467 AUD_set_volume_out (s
->voice_po
, mute
, lvol
, rvol
);
470 static void update_volume_in (AC97LinkState
*s
)
475 get_volume (mixer_load (s
, AC97_Record_Gain_Mute
), 0x0f, 0,
476 &mute
, &lvol
, &rvol
);
478 AUD_set_volume_in (s
->voice_pi
, mute
, lvol
, rvol
);
481 static void set_volume (AC97LinkState
*s
, int index
, uint32_t val
)
483 mixer_store (s
, index
, val
);
484 if (index
== AC97_Master_Volume_Mute
|| index
== AC97_PCM_Out_Volume_Mute
) {
485 update_combined_volume_out (s
);
486 } else if (index
== AC97_Record_Gain_Mute
) {
487 update_volume_in (s
);
491 static void record_select (AC97LinkState
*s
, uint32_t val
)
493 uint8_t rs
= val
& REC_MASK
;
494 uint8_t ls
= (val
>> 8) & REC_MASK
;
495 mixer_store (s
, AC97_Record_Select
, rs
| (ls
<< 8));
498 static void mixer_reset (AC97LinkState
*s
)
500 uint8_t active
[LAST_INDEX
];
502 dolog ("mixer_reset\n");
503 memset (s
->mixer_data
, 0, sizeof (s
->mixer_data
));
504 memset (active
, 0, sizeof (active
));
505 mixer_store (s
, AC97_Reset
, 0x0000); /* 6940 */
506 mixer_store (s
, AC97_Master_Volume_Mono_Mute
, 0x8000);
507 mixer_store (s
, AC97_PC_BEEP_Volume_Mute
, 0x0000);
509 mixer_store (s
, AC97_Phone_Volume_Mute
, 0x8008);
510 mixer_store (s
, AC97_Mic_Volume_Mute
, 0x8008);
511 mixer_store (s
, AC97_CD_Volume_Mute
, 0x8808);
512 mixer_store (s
, AC97_Aux_Volume_Mute
, 0x8808);
513 mixer_store (s
, AC97_Record_Gain_Mic_Mute
, 0x8000);
514 mixer_store (s
, AC97_General_Purpose
, 0x0000);
515 mixer_store (s
, AC97_3D_Control
, 0x0000);
516 mixer_store (s
, AC97_Powerdown_Ctrl_Stat
, 0x000f);
519 * Sigmatel 9700 (STAC9700)
521 mixer_store (s
, AC97_Vendor_ID1
, 0x8384);
522 mixer_store (s
, AC97_Vendor_ID2
, 0x7600); /* 7608 */
524 mixer_store (s
, AC97_Extended_Audio_ID
, 0x0809);
525 mixer_store (s
, AC97_Extended_Audio_Ctrl_Stat
, 0x0009);
526 mixer_store (s
, AC97_PCM_Front_DAC_Rate
, 0xbb80);
527 mixer_store (s
, AC97_PCM_Surround_DAC_Rate
, 0xbb80);
528 mixer_store (s
, AC97_PCM_LFE_DAC_Rate
, 0xbb80);
529 mixer_store (s
, AC97_PCM_LR_ADC_Rate
, 0xbb80);
530 mixer_store (s
, AC97_MIC_ADC_Rate
, 0xbb80);
532 record_select (s
, 0);
533 set_volume (s
, AC97_Master_Volume_Mute
, 0x8000);
534 set_volume (s
, AC97_PCM_Out_Volume_Mute
, 0x8808);
535 set_volume (s
, AC97_Line_In_Volume_Mute
, 0x8808);
537 reset_voices (s
, active
);
544 static uint32_t nam_readb (void *opaque
, uint32_t addr
)
546 AC97LinkState
*s
= opaque
;
547 dolog ("U nam readb %#x\n", addr
);
552 static uint32_t nam_readw (void *opaque
, uint32_t addr
)
554 AC97LinkState
*s
= opaque
;
556 uint32_t index
= addr
;
558 val
= mixer_load (s
, index
);
562 static uint32_t nam_readl (void *opaque
, uint32_t addr
)
564 AC97LinkState
*s
= opaque
;
565 dolog ("U nam readl %#x\n", addr
);
574 static void nam_writeb (void *opaque
, uint32_t addr
, uint32_t val
)
576 AC97LinkState
*s
= opaque
;
577 dolog ("U nam writeb %#x <- %#x\n", addr
, val
);
581 static void nam_writew (void *opaque
, uint32_t addr
, uint32_t val
)
583 AC97LinkState
*s
= opaque
;
584 uint32_t index
= addr
;
590 case AC97_Powerdown_Ctrl_Stat
:
592 val
|= mixer_load (s
, index
) & 0xf;
593 mixer_store (s
, index
, val
);
595 case AC97_PCM_Out_Volume_Mute
:
596 case AC97_Master_Volume_Mute
:
597 case AC97_Record_Gain_Mute
:
598 case AC97_Line_In_Volume_Mute
:
599 set_volume (s
, index
, val
);
601 case AC97_Record_Select
:
602 record_select (s
, val
);
604 case AC97_Vendor_ID1
:
605 case AC97_Vendor_ID2
:
606 dolog ("Attempt to write vendor ID to %#x\n", val
);
608 case AC97_Extended_Audio_ID
:
609 dolog ("Attempt to write extended audio ID to %#x\n", val
);
611 case AC97_Extended_Audio_Ctrl_Stat
:
612 if (!(val
& EACS_VRA
)) {
613 mixer_store (s
, AC97_PCM_Front_DAC_Rate
, 0xbb80);
614 mixer_store (s
, AC97_PCM_LR_ADC_Rate
, 0xbb80);
615 open_voice (s
, PI_INDEX
, 48000);
616 open_voice (s
, PO_INDEX
, 48000);
618 if (!(val
& EACS_VRM
)) {
619 mixer_store (s
, AC97_MIC_ADC_Rate
, 0xbb80);
620 open_voice (s
, MC_INDEX
, 48000);
622 dolog ("Setting extended audio control to %#x\n", val
);
623 mixer_store (s
, AC97_Extended_Audio_Ctrl_Stat
, val
);
625 case AC97_PCM_Front_DAC_Rate
:
626 if (mixer_load (s
, AC97_Extended_Audio_Ctrl_Stat
) & EACS_VRA
) {
627 mixer_store (s
, index
, val
);
628 dolog ("Set front DAC rate to %d\n", val
);
629 open_voice (s
, PO_INDEX
, val
);
632 dolog ("Attempt to set front DAC rate to %d, "
633 "but VRA is not set\n",
637 case AC97_MIC_ADC_Rate
:
638 if (mixer_load (s
, AC97_Extended_Audio_Ctrl_Stat
) & EACS_VRM
) {
639 mixer_store (s
, index
, val
);
640 dolog ("Set MIC ADC rate to %d\n", val
);
641 open_voice (s
, MC_INDEX
, val
);
644 dolog ("Attempt to set MIC ADC rate to %d, "
645 "but VRM is not set\n",
649 case AC97_PCM_LR_ADC_Rate
:
650 if (mixer_load (s
, AC97_Extended_Audio_Ctrl_Stat
) & EACS_VRA
) {
651 mixer_store (s
, index
, val
);
652 dolog ("Set front LR ADC rate to %d\n", val
);
653 open_voice (s
, PI_INDEX
, val
);
656 dolog ("Attempt to set LR ADC rate to %d, but VRA is not set\n",
661 dolog ("U nam writew %#x <- %#x\n", addr
, val
);
662 mixer_store (s
, index
, val
);
667 static void nam_writel (void *opaque
, uint32_t addr
, uint32_t val
)
669 AC97LinkState
*s
= opaque
;
670 dolog ("U nam writel %#x <- %#x\n", addr
, val
);
675 * Native audio bus master
678 static uint32_t nabm_readb (void *opaque
, uint32_t addr
)
680 AC97LinkState
*s
= opaque
;
681 AC97BusMasterRegs
*r
= NULL
;
682 uint32_t index
= addr
;
687 dolog ("CAS %d\n", s
->cas
);
694 r
= &s
->bm_regs
[GET_BM (index
)];
696 dolog ("CIV[%d] -> %#x\n", GET_BM (index
), val
);
701 r
= &s
->bm_regs
[GET_BM (index
)];
703 dolog ("LVI[%d] -> %#x\n", GET_BM (index
), val
);
708 r
= &s
->bm_regs
[GET_BM (index
)];
710 dolog ("PIV[%d] -> %#x\n", GET_BM (index
), val
);
715 r
= &s
->bm_regs
[GET_BM (index
)];
717 dolog ("CR[%d] -> %#x\n", GET_BM (index
), val
);
722 r
= &s
->bm_regs
[GET_BM (index
)];
724 dolog ("SRb[%d] -> %#x\n", GET_BM (index
), val
);
727 dolog ("U nabm readb %#x -> %#x\n", addr
, val
);
733 static uint32_t nabm_readw (void *opaque
, uint32_t addr
)
735 AC97LinkState
*s
= opaque
;
736 AC97BusMasterRegs
*r
= NULL
;
737 uint32_t index
= addr
;
744 r
= &s
->bm_regs
[GET_BM (index
)];
746 dolog ("SR[%d] -> %#x\n", GET_BM (index
), val
);
751 r
= &s
->bm_regs
[GET_BM (index
)];
753 dolog ("PICB[%d] -> %#x\n", GET_BM (index
), val
);
756 dolog ("U nabm readw %#x -> %#x\n", addr
, val
);
762 static uint32_t nabm_readl (void *opaque
, uint32_t addr
)
764 AC97LinkState
*s
= opaque
;
765 AC97BusMasterRegs
*r
= NULL
;
766 uint32_t index
= addr
;
773 r
= &s
->bm_regs
[GET_BM (index
)];
775 dolog ("BMADDR[%d] -> %#x\n", GET_BM (index
), val
);
780 r
= &s
->bm_regs
[GET_BM (index
)];
781 val
= r
->civ
| (r
->lvi
<< 8) | (r
->sr
<< 16);
782 dolog ("CIV LVI SR[%d] -> %#x, %#x, %#x\n", GET_BM (index
),
783 r
->civ
, r
->lvi
, r
->sr
);
788 r
= &s
->bm_regs
[GET_BM (index
)];
789 val
= r
->picb
| (r
->piv
<< 16) | (r
->cr
<< 24);
790 dolog ("PICB PIV CR[%d] -> %#x %#x %#x %#x\n", GET_BM (index
),
791 val
, r
->picb
, r
->piv
, r
->cr
);
795 dolog ("glob_cnt -> %#x\n", val
);
798 val
= s
->glob_sta
| GS_S0CR
;
799 dolog ("glob_sta -> %#x\n", val
);
802 dolog ("U nabm readl %#x -> %#x\n", addr
, val
);
809 * Native audio bus master
812 static void nabm_writeb (void *opaque
, uint32_t addr
, uint32_t val
)
814 AC97LinkState
*s
= opaque
;
815 AC97BusMasterRegs
*r
= NULL
;
816 uint32_t index
= addr
;
821 r
= &s
->bm_regs
[GET_BM (index
)];
822 if ((r
->cr
& CR_RPBM
) && (r
->sr
& SR_DCH
)) {
823 r
->sr
&= ~(SR_DCH
| SR_CELV
);
825 r
->piv
= (r
->piv
+ 1) % 32;
829 dolog ("LVI[%d] <- %#x\n", GET_BM (index
), val
);
834 r
= &s
->bm_regs
[GET_BM (index
)];
836 reset_bm_regs (s
, r
);
839 r
->cr
= val
& CR_VALID_MASK
;
840 if (!(r
->cr
& CR_RPBM
)) {
841 voice_set_active (s
, r
- s
->bm_regs
, 0);
846 r
->piv
= (r
->piv
+ 1) % 32;
849 voice_set_active (s
, r
- s
->bm_regs
, 1);
852 dolog ("CR[%d] <- %#x (cr %#x)\n", GET_BM (index
), val
, r
->cr
);
857 r
= &s
->bm_regs
[GET_BM (index
)];
858 r
->sr
|= val
& ~(SR_RO_MASK
| SR_WCLEAR_MASK
);
859 update_sr (s
, r
, r
->sr
& ~(val
& SR_WCLEAR_MASK
));
860 dolog ("SR[%d] <- %#x (sr %#x)\n", GET_BM (index
), val
, r
->sr
);
863 dolog ("U nabm writeb %#x <- %#x\n", addr
, val
);
868 static void nabm_writew (void *opaque
, uint32_t addr
, uint32_t val
)
870 AC97LinkState
*s
= opaque
;
871 AC97BusMasterRegs
*r
= NULL
;
872 uint32_t index
= addr
;
877 r
= &s
->bm_regs
[GET_BM (index
)];
878 r
->sr
|= val
& ~(SR_RO_MASK
| SR_WCLEAR_MASK
);
879 update_sr (s
, r
, r
->sr
& ~(val
& SR_WCLEAR_MASK
));
880 dolog ("SR[%d] <- %#x (sr %#x)\n", GET_BM (index
), val
, r
->sr
);
883 dolog ("U nabm writew %#x <- %#x\n", addr
, val
);
888 static void nabm_writel (void *opaque
, uint32_t addr
, uint32_t val
)
890 AC97LinkState
*s
= opaque
;
891 AC97BusMasterRegs
*r
= NULL
;
892 uint32_t index
= addr
;
897 r
= &s
->bm_regs
[GET_BM (index
)];
899 dolog ("BDBAR[%d] <- %#x (bdbar %#x)\n",
900 GET_BM (index
), val
, r
->bdbar
);
907 if (!(val
& (GC_WR
| GC_CR
)))
908 s
->glob_cnt
= val
& GC_VALID_MASK
;
909 dolog ("glob_cnt <- %#x (glob_cnt %#x)\n", val
, s
->glob_cnt
);
912 s
->glob_sta
&= ~(val
& GS_WCLEAR_MASK
);
913 s
->glob_sta
|= (val
& ~(GS_WCLEAR_MASK
| GS_RO_MASK
)) & GS_VALID_MASK
;
914 dolog ("glob_sta <- %#x (glob_sta %#x)\n", val
, s
->glob_sta
);
917 dolog ("U nabm writel %#x <- %#x\n", addr
, val
);
922 static int write_audio (AC97LinkState
*s
, AC97BusMasterRegs
*r
,
925 uint8_t tmpbuf
[4096];
926 uint32_t addr
= r
->bd
.addr
;
927 uint32_t temp
= r
->picb
<< 1;
928 uint32_t written
= 0;
930 temp
= audio_MIN (temp
, max
);
939 to_copy
= audio_MIN (temp
, sizeof (tmpbuf
));
940 pci_dma_read (&s
->dev
, addr
, tmpbuf
, to_copy
);
941 copied
= AUD_write (s
->voice_po
, tmpbuf
, to_copy
);
942 dolog ("write_audio max=%x to_copy=%x copied=%x\n",
943 max
, to_copy
, copied
);
959 s
->last_samp
= *(uint32_t *) &tmpbuf
[to_copy
- 4];
967 static void write_bup (AC97LinkState
*s
, int elapsed
)
969 dolog ("write_bup\n");
970 if (!(s
->bup_flag
& BUP_SET
)) {
971 if (s
->bup_flag
& BUP_LAST
) {
973 uint8_t *p
= s
->silence
;
974 for (i
= 0; i
< sizeof (s
->silence
) / 4; i
++, p
+= 4) {
975 *(uint32_t *) p
= s
->last_samp
;
979 memset (s
->silence
, 0, sizeof (s
->silence
));
981 s
->bup_flag
|= BUP_SET
;
985 int temp
= audio_MIN (elapsed
, sizeof (s
->silence
));
987 int copied
= AUD_write (s
->voice_po
, s
->silence
, temp
);
996 static int read_audio (AC97LinkState
*s
, AC97BusMasterRegs
*r
,
999 uint8_t tmpbuf
[4096];
1000 uint32_t addr
= r
->bd
.addr
;
1001 uint32_t temp
= r
->picb
<< 1;
1004 SWVoiceIn
*voice
= (r
- s
->bm_regs
) == MC_INDEX
? s
->voice_mc
: s
->voice_pi
;
1006 temp
= audio_MIN (temp
, max
);
1015 to_copy
= audio_MIN (temp
, sizeof (tmpbuf
));
1016 acquired
= AUD_read (voice
, tmpbuf
, to_copy
);
1021 pci_dma_write (&s
->dev
, addr
, tmpbuf
, acquired
);
1031 static void transfer_audio (AC97LinkState
*s
, int index
, int elapsed
)
1033 AC97BusMasterRegs
*r
= &s
->bm_regs
[index
];
1036 if (s
->invalid_freq
[index
]) {
1037 AUD_log ("ac97", "attempt to use voice %d with invalid frequency %d\n",
1038 index
, s
->invalid_freq
[index
]);
1042 if (r
->sr
& SR_DCH
) {
1043 if (r
->cr
& CR_RPBM
) {
1046 write_bup (s
, elapsed
);
1053 while ((elapsed
>> 1) && !stop
) {
1057 dolog ("invalid bd\n");
1062 dolog ("fresh bd %d is empty %#x %#x\n",
1063 r
->civ
, r
->bd
.addr
, r
->bd
.ctl_len
);
1064 if (r
->civ
== r
->lvi
) {
1065 r
->sr
|= SR_DCH
; /* CELV? */
1071 r
->piv
= (r
->piv
+ 1) % 32;
1078 temp
= write_audio (s
, r
, elapsed
, &stop
);
1080 r
->picb
-= (temp
>> 1);
1085 temp
= read_audio (s
, r
, elapsed
, &stop
);
1087 r
->picb
-= (temp
>> 1);
1092 uint32_t new_sr
= r
->sr
& ~SR_CELV
;
1094 if (r
->bd
.ctl_len
& BD_IOC
) {
1098 if (r
->civ
== r
->lvi
) {
1099 dolog ("Underrun civ (%d) == lvi (%d)\n", r
->civ
, r
->lvi
);
1101 new_sr
|= SR_LVBCI
| SR_DCH
| SR_CELV
;
1103 s
->bup_flag
= (r
->bd
.ctl_len
& BD_BUP
) ? BUP_LAST
: 0;
1107 r
->piv
= (r
->piv
+ 1) % 32;
1111 update_sr (s
, r
, new_sr
);
1116 static void pi_callback (void *opaque
, int avail
)
1118 transfer_audio (opaque
, PI_INDEX
, avail
);
1121 static void mc_callback (void *opaque
, int avail
)
1123 transfer_audio (opaque
, MC_INDEX
, avail
);
1126 static void po_callback (void *opaque
, int free
)
1128 transfer_audio (opaque
, PO_INDEX
, free
);
1131 static const VMStateDescription vmstate_ac97_bm_regs
= {
1132 .name
= "ac97_bm_regs",
1134 .minimum_version_id
= 1,
1135 .minimum_version_id_old
= 1,
1136 .fields
= (VMStateField
[]) {
1137 VMSTATE_UINT32 (bdbar
, AC97BusMasterRegs
),
1138 VMSTATE_UINT8 (civ
, AC97BusMasterRegs
),
1139 VMSTATE_UINT8 (lvi
, AC97BusMasterRegs
),
1140 VMSTATE_UINT16 (sr
, AC97BusMasterRegs
),
1141 VMSTATE_UINT16 (picb
, AC97BusMasterRegs
),
1142 VMSTATE_UINT8 (piv
, AC97BusMasterRegs
),
1143 VMSTATE_UINT8 (cr
, AC97BusMasterRegs
),
1144 VMSTATE_UINT32 (bd_valid
, AC97BusMasterRegs
),
1145 VMSTATE_UINT32 (bd
.addr
, AC97BusMasterRegs
),
1146 VMSTATE_UINT32 (bd
.ctl_len
, AC97BusMasterRegs
),
1147 VMSTATE_END_OF_LIST ()
1151 static int ac97_post_load (void *opaque
, int version_id
)
1153 uint8_t active
[LAST_INDEX
];
1154 AC97LinkState
*s
= opaque
;
1156 record_select (s
, mixer_load (s
, AC97_Record_Select
));
1157 set_volume (s
, AC97_Master_Volume_Mute
,
1158 mixer_load (s
, AC97_Master_Volume_Mute
));
1159 set_volume (s
, AC97_PCM_Out_Volume_Mute
,
1160 mixer_load (s
, AC97_PCM_Out_Volume_Mute
));
1161 set_volume (s
, AC97_Line_In_Volume_Mute
,
1162 mixer_load (s
, AC97_Line_In_Volume_Mute
));
1164 active
[PI_INDEX
] = !!(s
->bm_regs
[PI_INDEX
].cr
& CR_RPBM
);
1165 active
[PO_INDEX
] = !!(s
->bm_regs
[PO_INDEX
].cr
& CR_RPBM
);
1166 active
[MC_INDEX
] = !!(s
->bm_regs
[MC_INDEX
].cr
& CR_RPBM
);
1167 reset_voices (s
, active
);
1174 static bool is_version_2 (void *opaque
, int version_id
)
1176 return version_id
== 2;
1179 static const VMStateDescription vmstate_ac97
= {
1182 .minimum_version_id
= 2,
1183 .minimum_version_id_old
= 2,
1184 .post_load
= ac97_post_load
,
1185 .fields
= (VMStateField
[]) {
1186 VMSTATE_PCI_DEVICE (dev
, AC97LinkState
),
1187 VMSTATE_UINT32 (glob_cnt
, AC97LinkState
),
1188 VMSTATE_UINT32 (glob_sta
, AC97LinkState
),
1189 VMSTATE_UINT32 (cas
, AC97LinkState
),
1190 VMSTATE_STRUCT_ARRAY (bm_regs
, AC97LinkState
, 3, 1,
1191 vmstate_ac97_bm_regs
, AC97BusMasterRegs
),
1192 VMSTATE_BUFFER (mixer_data
, AC97LinkState
),
1193 VMSTATE_UNUSED_TEST (is_version_2
, 3),
1194 VMSTATE_END_OF_LIST ()
1198 static const MemoryRegionPortio nam_portio
[] = {
1199 { 0, 256 * 1, 1, .read
= nam_readb
, },
1200 { 0, 256 * 2, 2, .read
= nam_readw
, },
1201 { 0, 256 * 4, 4, .read
= nam_readl
, },
1202 { 0, 256 * 1, 1, .write
= nam_writeb
, },
1203 { 0, 256 * 2, 2, .write
= nam_writew
, },
1204 { 0, 256 * 4, 4, .write
= nam_writel
, },
1205 PORTIO_END_OF_LIST (),
1208 static const MemoryRegionOps ac97_io_nam_ops
= {
1209 .old_portio
= nam_portio
,
1212 static const MemoryRegionPortio nabm_portio
[] = {
1213 { 0, 64 * 1, 1, .read
= nabm_readb
, },
1214 { 0, 64 * 2, 2, .read
= nabm_readw
, },
1215 { 0, 64 * 4, 4, .read
= nabm_readl
, },
1216 { 0, 64 * 1, 1, .write
= nabm_writeb
, },
1217 { 0, 64 * 2, 2, .write
= nabm_writew
, },
1218 { 0, 64 * 4, 4, .write
= nabm_writel
, },
1219 PORTIO_END_OF_LIST ()
1222 static const MemoryRegionOps ac97_io_nabm_ops
= {
1223 .old_portio
= nabm_portio
,
1226 static void ac97_on_reset (void *opaque
)
1228 AC97LinkState
*s
= opaque
;
1230 reset_bm_regs (s
, &s
->bm_regs
[0]);
1231 reset_bm_regs (s
, &s
->bm_regs
[1]);
1232 reset_bm_regs (s
, &s
->bm_regs
[2]);
1235 * Reset the mixer too. The Windows XP driver seems to rely on
1236 * this. At least it wants to read the vendor id before it resets
1237 * the codec manually.
1242 static int ac97_initfn (PCIDevice
*dev
)
1244 AC97LinkState
*s
= DO_UPCAST (AC97LinkState
, dev
, dev
);
1245 uint8_t *c
= s
->dev
.config
;
1247 /* TODO: no need to override */
1248 c
[PCI_COMMAND
] = 0x00; /* pcicmd pci command rw, ro */
1249 c
[PCI_COMMAND
+ 1] = 0x00;
1252 c
[PCI_STATUS
] = PCI_STATUS_FAST_BACK
; /* pcists pci status rwc, ro */
1253 c
[PCI_STATUS
+ 1] = PCI_STATUS_DEVSEL_MEDIUM
>> 8;
1255 c
[PCI_CLASS_PROG
] = 0x00; /* pi programming interface ro */
1257 /* TODO set when bar is registered. no need to override. */
1258 /* nabmar native audio mixer base address rw */
1259 c
[PCI_BASE_ADDRESS_0
] = PCI_BASE_ADDRESS_SPACE_IO
;
1260 c
[PCI_BASE_ADDRESS_0
+ 1] = 0x00;
1261 c
[PCI_BASE_ADDRESS_0
+ 2] = 0x00;
1262 c
[PCI_BASE_ADDRESS_0
+ 3] = 0x00;
1264 /* TODO set when bar is registered. no need to override. */
1265 /* nabmbar native audio bus mastering base address rw */
1266 c
[PCI_BASE_ADDRESS_0
+ 4] = PCI_BASE_ADDRESS_SPACE_IO
;
1267 c
[PCI_BASE_ADDRESS_0
+ 5] = 0x00;
1268 c
[PCI_BASE_ADDRESS_0
+ 6] = 0x00;
1269 c
[PCI_BASE_ADDRESS_0
+ 7] = 0x00;
1271 if (s
->use_broken_id
) {
1272 c
[PCI_SUBSYSTEM_VENDOR_ID
] = 0x86;
1273 c
[PCI_SUBSYSTEM_VENDOR_ID
+ 1] = 0x80;
1274 c
[PCI_SUBSYSTEM_ID
] = 0x00;
1275 c
[PCI_SUBSYSTEM_ID
+ 1] = 0x00;
1278 c
[PCI_INTERRUPT_LINE
] = 0x00; /* intr_ln interrupt line rw */
1279 c
[PCI_INTERRUPT_PIN
] = 0x01; /* intr_pn interrupt pin ro */
1281 memory_region_init_io (&s
->io_nam
, &ac97_io_nam_ops
, s
, "ac97-nam", 1024);
1282 memory_region_init_io (&s
->io_nabm
, &ac97_io_nabm_ops
, s
, "ac97-nabm", 256);
1283 pci_register_bar (&s
->dev
, 0, PCI_BASE_ADDRESS_SPACE_IO
, &s
->io_nam
);
1284 pci_register_bar (&s
->dev
, 1, PCI_BASE_ADDRESS_SPACE_IO
, &s
->io_nabm
);
1285 qemu_register_reset (ac97_on_reset
, s
);
1286 AUD_register_card ("ac97", &s
->card
);
1291 static int ac97_exitfn (PCIDevice
*dev
)
1293 AC97LinkState
*s
= DO_UPCAST (AC97LinkState
, dev
, dev
);
1295 memory_region_destroy (&s
->io_nam
);
1296 memory_region_destroy (&s
->io_nabm
);
1300 int ac97_init (PCIBus
*bus
)
1302 pci_create_simple (bus
, -1, "AC97");
1306 static Property ac97_properties
[] = {
1307 DEFINE_PROP_UINT32 ("use_broken_id", AC97LinkState
, use_broken_id
, 0),
1308 DEFINE_PROP_END_OF_LIST (),
1311 static void ac97_class_init (ObjectClass
*klass
, void *data
)
1313 DeviceClass
*dc
= DEVICE_CLASS (klass
);
1314 PCIDeviceClass
*k
= PCI_DEVICE_CLASS (klass
);
1316 k
->init
= ac97_initfn
;
1317 k
->exit
= ac97_exitfn
;
1318 k
->vendor_id
= PCI_VENDOR_ID_INTEL
;
1319 k
->device_id
= PCI_DEVICE_ID_INTEL_82801AA_5
;
1321 k
->class_id
= PCI_CLASS_MULTIMEDIA_AUDIO
;
1322 dc
->desc
= "Intel 82801AA AC97 Audio";
1323 dc
->vmsd
= &vmstate_ac97
;
1324 dc
->props
= ac97_properties
;
1327 static TypeInfo ac97_info
= {
1329 .parent
= TYPE_PCI_DEVICE
,
1330 .instance_size
= sizeof (AC97LinkState
),
1331 .class_init
= ac97_class_init
,
1334 static void ac97_register_types (void)
1336 type_register_static (&ac97_info
);
1339 type_init (ac97_register_types
)