1 /* SPDX-License-Identifier: GPL-2.0-only */
4 #include <console/console.h>
6 #include <device/device.h>
7 #include <device/pci.h>
8 #include <device/pci_ids.h>
9 #include <device/pci_ops.h>
15 #define MASTER_VOL 0x02
17 #define EXT_AUDIO 0x28
20 #define CONNECTOR 0x6a
21 #define VENDOR_ID1 0x7c
22 #define VENDOR_ID2 0x7e
23 #define SEC_VENDOR_ID1 0xfc
24 #define SEC_VENDOR_ID2 0xfe
32 #define EXT_MODEM_ID1 0x3c
33 #define EXT_MODEM_ID2 0xbc
36 #define SEC_CODEC 0x40
38 /* FIXME. This table is probably mainboard specific */
39 static u16 ac97_function
[16*2][4] = {
40 { (1 << 5), (2 << 11), (1 << 10), (3 << 13) },
41 { (1 << 5), (2 << 11), (1 << 10), (3 << 13) },
42 { (1 << 5), (2 << 11), (1 << 10), (3 << 13) },
43 { (1 << 5), (2 << 11), (1 << 10), (3 << 13) },
44 { (1 << 5), (2 << 11), (1 << 10), (3 << 13) },
45 { (1 << 5), (2 << 11), (1 << 10), (3 << 13) },
46 { (1 << 5), (2 << 11), (1 << 10), (3 << 13) },
47 { (1 << 5), (2 << 11), (1 << 10), (3 << 13) },
48 { (1 << 5), (2 << 11), (1 << 10), (3 << 13) },
49 { (1 << 5), (2 << 11), (1 << 10), (3 << 13) },
50 { (1 << 5), (2 << 11), (1 << 10), (3 << 13) },
51 { (1 << 5), (2 << 11), (1 << 10), (3 << 13) },
52 { (1 << 5), (2 << 11), (1 << 10), (3 << 13) },
53 { (1 << 5), (2 << 11), (1 << 10), (3 << 13) },
54 { (1 << 5), (2 << 11), (1 << 10), (3 << 13) },
55 { (1 << 5), (2 << 11), (1 << 10), (3 << 13) },
56 { (1 << 5), (2 << 11), (1 << 10), (3 << 13) },
57 { (1 << 5), (2 << 11), (1 << 10), (3 << 13) },
58 { (1 << 5), (2 << 11), (1 << 10), (3 << 13) },
59 { (1 << 5), (2 << 11), (1 << 10), (3 << 13) },
60 { (1 << 5), (2 << 11), (1 << 10), (3 << 13) },
61 { (1 << 5), (2 << 11), (1 << 10), (3 << 13) },
62 { (1 << 5), (2 << 11), (1 << 10), (3 << 13) },
63 { (1 << 5), (2 << 11), (1 << 10), (3 << 13) },
64 { (1 << 5), (2 << 11), (1 << 10), (3 << 13) },
65 { (1 << 5), (2 << 11), (1 << 10), (3 << 13) },
66 { (1 << 5), (2 << 11), (1 << 10), (3 << 13) },
67 { (1 << 5), (2 << 11), (1 << 10), (3 << 13) },
68 { (1 << 5), (2 << 11), (1 << 10), (3 << 13) },
69 { (1 << 5), (2 << 11), (1 << 10), (3 << 13) },
70 { (1 << 5), (2 << 11), (1 << 10), (3 << 13) },
71 { (1 << 5), (2 << 11), (1 << 10), (3 << 13) }
77 static int ac97_semaphore(void)
84 reg8
= inb(nabmbar
+ CAS
);
86 } while ((reg8
& 1) && timeout
);
88 printk(BIOS_DEBUG
, "Timeout!\n");
93 static void init_cnr(void)
98 static void program_sigid(struct device
*dev
, u32 id
)
100 pci_write_config32(dev
, 0x2c, id
);
103 static void ac97_audio_init(struct device
*dev
)
109 printk(BIOS_DEBUG
, "Initializing AC'97 Audio.\n");
111 /* top 16 bits are zero, so don't read them */
112 nabmbar
= pci_read_config16(dev
, NABMBAR
) & 0xfffe;
113 nambar
= pci_read_config16(dev
, NAMBAR
) & 0xfffe;
115 reg16
= inw(nabmbar
+ GLOB_CNT
);
116 reg16
|= (1 << 1); /* Remove AC_RESET# */
117 outw(reg16
, nabmbar
+ GLOB_CNT
);
119 /* Wait 600ms. Ouch. */
124 /* Detect Primary AC'97 Codec */
125 reg32
= inl(nabmbar
+ GLOB_STA
);
126 if ((reg32
& ((1 << 28) | (1 << 9) | (1 << 8))) == 0) {
127 /* Primary Codec not found */
128 printk(BIOS_DEBUG
, "No primary codec. Disabling AC'97 Audio.\n");
134 /* Detect if codec is programmable */
135 outw(0x8000, nambar
+ MASTER_VOL
);
137 if (inw(nambar
+ MASTER_VOL
) != 0x8000) {
138 printk(BIOS_DEBUG
, "Codec not programmable. Disabling AC'97 Audio.\n");
142 /* Program Vendor IDs */
143 reg32
= inw(nambar
+ VENDOR_ID1
);
145 reg32
|= (u16
)inw(nambar
+ VENDOR_ID2
);
147 program_sigid(dev
, reg32
);
149 /* Is Codec AC'97 2.3 compliant? */
150 reg16
= inw(nambar
+ EXT_AUDIO
);
151 /* [11:10] = 10b -> AC'97 2.3 */
152 if ((reg16
& 0x0c00) != 0x0800) {
153 /* No 2.3 Codec. We're done */
158 reg16
= inw(nambar
+ PAGING
);
161 outw(reg16
, nambar
+ PAGING
);
163 for (i
= 0x0a * 2; i
> 0; i
--) {
164 outw(i
, nambar
+ FUNC_SEL
);
166 /* Function could not be selected. Next one */
167 if (inw(nambar
+ FUNC_SEL
) != i
)
170 reg16
= inw(nambar
+ INFO_IO
);
172 /* Function Information present? */
173 if (!(reg16
& (1 << 0)))
176 /* Function Information valid? */
177 if (!(reg16
& (1 << 4)))
180 /* Program Buffer Delay [9:5] */
182 reg16
|= ac97_function
[i
][0];
184 /* Program Gain [15:11] */
185 reg16
|= ac97_function
[i
][1];
187 /* Program Inversion [10] */
188 reg16
|= ac97_function
[i
][2];
190 outw(reg16
, nambar
+ INFO_IO
);
192 /* Program Connector / Jack Location */
193 reg16
= inw(nambar
+ CONNECTOR
);
195 reg16
|= ac97_function
[i
][3];
196 outw(reg16
, nambar
+ CONNECTOR
);
200 static void ac97_modem_init(struct device
*dev
)
206 mmbar
= pci_read_config16(dev
, MMBAR
) & 0xfffe;
207 mbar
= pci_read_config16(dev
, MBAR
) & 0xfffe;
209 reg16
= inw(mmbar
+ EXT_MODEM_ID1
);
210 if ((reg16
& 0xc000) != 0xc000) {
211 if (reg16
& (1 << 0)) {
212 reg32
= inw(mmbar
+ VENDOR_ID2
);
214 reg32
|= (u16
)inw(mmbar
+ VENDOR_ID1
);
215 program_sigid(dev
, reg32
);
220 /* Secondary codec? */
221 reg16
= inw(mbar
+ SEC_CODEC
);
222 if ((reg16
& (1 << 9)) == 0)
225 reg16
= inw(mmbar
+ EXT_MODEM_ID2
);
226 if ((reg16
& 0xc000) == 0x4000) {
227 if (reg16
& (1 << 0)) {
228 reg32
= inw(mmbar
+ SEC_VENDOR_ID2
);
230 reg32
|= (u16
)inw(mmbar
+ SEC_VENDOR_ID1
);
231 program_sigid(dev
, reg32
);
237 static struct device_operations ac97_audio_ops
= {
238 .read_resources
= pci_dev_read_resources
,
239 .set_resources
= pci_dev_set_resources
,
240 .enable_resources
= pci_dev_enable_resources
,
241 .enable
= i82801dx_enable
,
242 .init
= ac97_audio_init
,
245 static struct device_operations ac97_modem_ops
= {
246 .read_resources
= pci_dev_read_resources
,
247 .set_resources
= pci_dev_set_resources
,
248 .enable_resources
= pci_dev_enable_resources
,
249 .enable
= i82801dx_enable
,
250 .init
= ac97_modem_init
,
253 /* 82801DB/DBL/DBM (ICH4/ICH4-L/ICH4-M) */
254 static const struct pci_driver i82801db_ac97_audio __pci_driver
= {
255 .ops
= &ac97_audio_ops
,
256 .vendor
= PCI_VID_INTEL
,
257 .device
= PCI_DID_INTEL_82801DB_AC97_AUDIO
,
260 static const struct pci_driver i82801db_ac97_modem __pci_driver
= {
261 .ops
= &ac97_modem_ops
,
262 .vendor
= PCI_VID_INTEL
,
263 .device
= PCI_DID_INTEL_82801DB_AC97_MODEM
,