2 * This file is part of the coreboot project.
4 * Copyright (C) 2008-2009 coresystems GmbH
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 #include <console/console.h>
22 #include <device/device.h>
23 #include <device/pci.h>
24 #include <device/pci_ids.h>
30 #define MASTER_VOL 0x02
32 #define EXT_AUDIO 0x28
35 #define CONNECTOR 0x6a
36 #define VENDOR_ID1 0x7c
37 #define VENDOR_ID2 0x7e
38 #define SEC_VENDOR_ID1 0xfc
39 #define SEC_VENDOR_ID2 0xfe
47 #define EXT_MODEM_ID1 0x3c
48 #define EXT_MODEM_ID2 0xbc
51 #define SEC_CODEC 0x40
54 /* FIXME. This table is probably mainboard specific */
55 static u16 ac97_function[16*2][4] = {
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) },
72 { (1 << 5), (2 << 11), (1 << 10), (3 << 13) },
73 { (1 << 5), (2 << 11), (1 << 10), (3 << 13) },
74 { (1 << 5), (2 << 11), (1 << 10), (3 << 13) },
75 { (1 << 5), (2 << 11), (1 << 10), (3 << 13) },
76 { (1 << 5), (2 << 11), (1 << 10), (3 << 13) },
77 { (1 << 5), (2 << 11), (1 << 10), (3 << 13) },
78 { (1 << 5), (2 << 11), (1 << 10), (3 << 13) },
79 { (1 << 5), (2 << 11), (1 << 10), (3 << 13) },
80 { (1 << 5), (2 << 11), (1 << 10), (3 << 13) },
81 { (1 << 5), (2 << 11), (1 << 10), (3 << 13) },
82 { (1 << 5), (2 << 11), (1 << 10), (3 << 13) },
83 { (1 << 5), (2 << 11), (1 << 10), (3 << 13) },
84 { (1 << 5), (2 << 11), (1 << 10), (3 << 13) },
85 { (1 << 5), (2 << 11), (1 << 10), (3 << 13) },
86 { (1 << 5), (2 << 11), (1 << 10), (3 << 13) },
87 { (1 << 5), (2 << 11), (1 << 10), (3 << 13) }
93 static int ac97_semaphore(void)
100 reg8 = inb(nabmbar + CAS);
102 } while ((reg8 & 1) && timeout);
104 printk_debug("Timeout!\n");
110 static void init_cnr(void)
115 static void program_sigid(struct device *dev, u32 id)
117 pci_write_config32(dev, 0x2c, id);
120 static void ac97_audio_init(struct device *dev)
127 printk_debug("Initializing AC'97 Audio.\n");
129 /* top 16 bits are zero, so don't read them */
130 nabmbar = pci_read_config16(dev, NABMBAR) & 0xfffe;
131 nambar = pci_read_config16(dev, NAMBAR) & 0xfffe;
133 reg16 = inw(nabmbar + GLOB_CNT);
134 reg16 |= (1 << 1); /* Remove AC_RESET# */
135 outw(reg16, nabmbar + GLOB_CNT);
137 /* Wait 600ms. Ouch. */
142 /* Detect Primary AC'97 Codec */
143 reg32 = inl(nabmbar + GLOB_STA);
144 if ((reg32 & ((1 << 28) | (1 << 9) | (1 << 8))) == 0) {
145 /* Primary Codec not found */
146 printk_debug("No primary codec. Disabling AC'97 Audio.\n");
152 /* Detect if codec is programmable */
153 outw(0x8000, nambar + MASTER_VOL);
155 if (inw(nambar + MASTER_VOL) != 0x8000) {
156 printk_debug("Codec not programmable. Disabling AC'97 Audio.\n");
160 /* Program Vendor IDs */
161 reg32 = inw(nambar + VENDOR_ID1);
163 reg32 |= (u16)inw(nambar + VENDOR_ID2);
165 program_sigid(dev, reg32);
167 /* Is Codec AC'97 2.3 compliant? */
168 reg16 = inw(nambar + EXT_AUDIO);
169 /* [11:10] = 10b -> AC'97 2.3 */
170 if ((reg16 & 0x0c00) != 0x0800) {
171 /* No 2.3 Codec. We're done */
176 reg16 = inw(nambar + PAGING);
179 outw(reg16, nambar + PAGING);
181 for (i = 0x0a * 2; i > 0; i--) {
182 outw(i, nambar + FUNC_SEL);
184 /* Function could not be selected. Next one */
185 if (inw(nambar + FUNC_SEL) != i)
188 reg16 = inw(nambar + INFO_IO);
190 /* Function Information present? */
191 if (!(reg16 & (1 << 0)))
194 /* Function Information valid? */
195 if (!(reg16 & (1 << 4)))
198 /* Program Buffer Delay [9:5] */
200 reg16 |= ac97_function[i][0];
202 /* Program Gain [15:11] */
203 reg16 |= ac97_function[i][1];
205 /* Program Inversion [10] */
206 reg16 |= ac97_function[i][2];
208 outw(reg16, nambar + INFO_IO);
210 /* Program Connector / Jack Location */
211 reg16 = inw(nambar + CONNECTOR);
213 reg16 |= ac97_function[i][3];
214 outw(reg16, nambar + CONNECTOR);
218 static void ac97_modem_init(struct device *dev)
224 mmbar = pci_read_config16(dev, MMBAR) & 0xfffe;
225 mbar = pci_read_config16(dev, MBAR) & 0xfffe;
227 reg16 = inw(mmbar + EXT_MODEM_ID1);
228 if ((reg16 & 0xc000) != 0xc000 ) {
229 if (reg16 & (1 << 0)) {
230 reg32 = inw(mmbar + VENDOR_ID2);
232 reg32 |= (u16)inw(mmbar + VENDOR_ID1);
233 program_sigid(dev, reg32);
238 /* Secondary codec? */
239 reg16 = inw(mbar + SEC_CODEC);
240 if ((reg16 & (1 << 9)) == 0)
243 reg16 = inw(mmbar + EXT_MODEM_ID2);
244 if ((reg16 & 0xc000) == 0x4000) {
245 if (reg16 & (1 << 0)) {
246 reg32 = inw(mmbar + SEC_VENDOR_ID2);
248 reg32 |= (u16)inw(mmbar + SEC_VENDOR_ID1);
249 program_sigid(dev, reg32);
255 static struct device_operations ac97_audio_ops = {
256 .read_resources = pci_dev_read_resources,
257 .set_resources = pci_dev_set_resources,
258 .enable_resources = pci_dev_enable_resources,
259 .init = ac97_audio_init,
261 .enable = i82801gx_enable,
264 static struct device_operations ac97_modem_ops = {
265 .read_resources = pci_dev_read_resources,
266 .set_resources = pci_dev_set_resources,
267 .enable_resources = pci_dev_enable_resources,
268 .init = ac97_modem_init,
270 .enable = i82801gx_enable,
273 /* 82801GB/GR/GDH/GBM/GHM (ICH7/ICH7R/ICH7DH/ICH7-M/ICH7-M DH) */
274 /* Note: 82801GU (ICH7-U) doesn't have AC97 audio. */
275 static const struct pci_driver i82801gx_ac97_audio __pci_driver = {
276 .ops = &ac97_audio_ops,
277 .vendor = PCI_VENDOR_ID_INTEL,
281 /* 82801GB/GR/GDH/GBM/GHM (ICH7/ICH7R/ICH7DH/ICH7-M/ICH7-M DH) */
282 /* Note: 82801GU (ICH7-U) doesn't have AC97 modem. */
283 static const struct pci_driver i82801gx_ac97_modem __pci_driver = {
284 .ops = &ac97_modem_ops,
285 .vendor = PCI_VENDOR_ID_INTEL,