blob: b86891bddf5617cd5ce9d05a9b73c65e879f712d [file] [log] [blame]
Stefan Reinauerdebb11f2008-10-29 04:46:52 +00001/*
2 * This file is part of the coreboot project.
3 *
Stefan Reinauer54309d62009-01-20 22:53:10 +00004 * Copyright (C) 2008-2009 coresystems GmbH
Stefan Reinauerdebb11f2008-10-29 04:46:52 +00005 *
Stefan Reinauera8e11682009-03-11 14:54:18 +00006 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; version 2 of
9 * the License.
Stefan Reinauerdebb11f2008-10-29 04:46:52 +000010 *
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.
Stefan Reinauerdebb11f2008-10-29 04:46:52 +000015 */
16
17#include <console/console.h>
18#include <device/device.h>
19#include <device/pci.h>
20#include <device/pci_ids.h>
Stefan Reinauer54309d62009-01-20 22:53:10 +000021#include <arch/io.h>
22#include <delay.h>
Stefan Reinauerdebb11f2008-10-29 04:46:52 +000023#include "i82801gx.h"
24
Stefan Reinauer54309d62009-01-20 22:53:10 +000025#define NAMBAR 0x10
26#define MASTER_VOL 0x02
27#define PAGING 0x24
28#define EXT_AUDIO 0x28
29#define FUNC_SEL 0x66
30#define INFO_IO 0x68
31#define CONNECTOR 0x6a
32#define VENDOR_ID1 0x7c
33#define VENDOR_ID2 0x7e
34#define SEC_VENDOR_ID1 0xfc
35#define SEC_VENDOR_ID2 0xfe
36
37#define NABMBAR 0x14
38#define GLOB_CNT 0x2c
39#define GLOB_STA 0x30
40#define CAS 0x34
41
42#define MMBAR 0x10
43#define EXT_MODEM_ID1 0x3c
44#define EXT_MODEM_ID2 0xbc
45
46#define MBAR 0x14
47#define SEC_CODEC 0x40
48
49
50/* FIXME. This table is probably mainboard specific */
51static u16 ac97_function[16*2][4] = {
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) },
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};
85
86static u16 nabmbar;
87static u16 nambar;
88
89static int ac97_semaphore(void)
90{
91 int timeout;
92 u8 reg8;
93
94 timeout = 0xffff;
95 do {
96 reg8 = inb(nabmbar + CAS);
97 timeout--;
98 } while ((reg8 & 1) && timeout);
Arthur Heymans3f111b02017-03-09 12:02:52 +010099 if (!timeout)
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000100 printk(BIOS_DEBUG, "Timeout!\n");
Stefan Reinauer54309d62009-01-20 22:53:10 +0000101
102 return (!timeout);
103}
104
105static void init_cnr(void)
106{
107 // TODO
108}
109
110static void program_sigid(struct device *dev, u32 id)
111{
112 pci_write_config32(dev, 0x2c, id);
113}
114
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000115static void ac97_audio_init(struct device *dev)
116{
Stefan Reinauer54309d62009-01-20 22:53:10 +0000117 u16 reg16;
118 u32 reg32;
119 int i;
120
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000121 printk(BIOS_DEBUG, "Initializing AC'97 Audio.\n");
Stefan Reinauer54309d62009-01-20 22:53:10 +0000122
123 /* top 16 bits are zero, so don't read them */
124 nabmbar = pci_read_config16(dev, NABMBAR) & 0xfffe;
125 nambar = pci_read_config16(dev, NAMBAR) & 0xfffe;
126
127 reg16 = inw(nabmbar + GLOB_CNT);
128 reg16 |= (1 << 1); /* Remove AC_RESET# */
129 outw(reg16, nabmbar + GLOB_CNT);
130
131 /* Wait 600ms. Ouch. */
132 udelay(600 * 1000);
133
134 init_cnr();
135
136 /* Detect Primary AC'97 Codec */
137 reg32 = inl(nabmbar + GLOB_STA);
138 if ((reg32 & ((1 << 28) | (1 << 9) | (1 << 8))) == 0) {
139 /* Primary Codec not found */
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000140 printk(BIOS_DEBUG, "No primary codec. Disabling AC'97 Audio.\n");
Stefan Reinauer54309d62009-01-20 22:53:10 +0000141 return;
142 }
Stefan Reinauer109ab312009-08-12 16:08:05 +0000143
Stefan Reinauer54309d62009-01-20 22:53:10 +0000144 ac97_semaphore();
145
146 /* Detect if codec is programmable */
147 outw(0x8000, nambar + MASTER_VOL);
148 ac97_semaphore();
149 if (inw(nambar + MASTER_VOL) != 0x8000) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000150 printk(BIOS_DEBUG, "Codec not programmable. Disabling AC'97 Audio.\n");
Stefan Reinauer54309d62009-01-20 22:53:10 +0000151 return;
152 }
153
154 /* Program Vendor IDs */
155 reg32 = inw(nambar + VENDOR_ID1);
156 reg32 <<= 16;
157 reg32 |= (u16)inw(nambar + VENDOR_ID2);
158
159 program_sigid(dev, reg32);
160
161 /* Is Codec AC'97 2.3 compliant? */
162 reg16 = inw(nambar + EXT_AUDIO);
163 /* [11:10] = 10b -> AC'97 2.3 */
164 if ((reg16 & 0x0c00) != 0x0800) {
165 /* No 2.3 Codec. We're done */
166 return;
167 }
168
169 /* Select Page 1 */
170 reg16 = inw(nambar + PAGING);
171 reg16 &= 0xfff0;
172 reg16 |= 0x0001;
173 outw(reg16, nambar + PAGING);
174
175 for (i = 0x0a * 2; i > 0; i--) {
176 outw(i, nambar + FUNC_SEL);
177
178 /* Function could not be selected. Next one */
179 if (inw(nambar + FUNC_SEL) != i)
180 continue;
181
182 reg16 = inw(nambar + INFO_IO);
183
184 /* Function Information present? */
185 if (!(reg16 & (1 << 0)))
186 continue;
187
188 /* Function Information valid? */
189 if (!(reg16 & (1 << 4)))
190 continue;
191
192 /* Program Buffer Delay [9:5] */
193 reg16 &= 0x03e0;
194 reg16 |= ac97_function[i][0];
195
196 /* Program Gain [15:11] */
197 reg16 |= ac97_function[i][1];
198
199 /* Program Inversion [10] */
200 reg16 |= ac97_function[i][2];
201
202 outw(reg16, nambar + INFO_IO);
203
204 /* Program Connector / Jack Location */
205 reg16 = inw(nambar + CONNECTOR);
206 reg16 &= 0x1fff;
207 reg16 |= ac97_function[i][3];
208 outw(reg16, nambar + CONNECTOR);
209 }
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000210}
211
212static void ac97_modem_init(struct device *dev)
213{
Stefan Reinauer54309d62009-01-20 22:53:10 +0000214 u16 reg16;
215 u32 reg32;
216 u16 mmbar, mbar;
217
218 mmbar = pci_read_config16(dev, MMBAR) & 0xfffe;
219 mbar = pci_read_config16(dev, MBAR) & 0xfffe;
220
221 reg16 = inw(mmbar + EXT_MODEM_ID1);
Arthur Heymans3f111b02017-03-09 12:02:52 +0100222 if ((reg16 & 0xc000) != 0xc000) {
Stefan Reinauer54309d62009-01-20 22:53:10 +0000223 if (reg16 & (1 << 0)) {
224 reg32 = inw(mmbar + VENDOR_ID2);
225 reg32 <<= 16;
226 reg32 |= (u16)inw(mmbar + VENDOR_ID1);
227 program_sigid(dev, reg32);
228 return;
229 }
230 }
231
232 /* Secondary codec? */
233 reg16 = inw(mbar + SEC_CODEC);
234 if ((reg16 & (1 << 9)) == 0)
235 return;
236
237 reg16 = inw(mmbar + EXT_MODEM_ID2);
238 if ((reg16 & 0xc000) == 0x4000) {
239 if (reg16 & (1 << 0)) {
240 reg32 = inw(mmbar + SEC_VENDOR_ID2);
241 reg32 <<= 16;
242 reg32 |= (u16)inw(mmbar + SEC_VENDOR_ID1);
243 program_sigid(dev, reg32);
244 return;
245 }
246 }
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000247}
248
Arthur Heymans3f111b02017-03-09 12:02:52 +0100249static void ac97_set_subsystem(device_t dev, unsigned int vendor,
250 unsigned int device)
Stefan Reinauer573f7d42009-07-21 21:50:34 +0000251{
252 if (!vendor || !device) {
253 pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID,
254 pci_read_config32(dev, PCI_VENDOR_ID));
255 } else {
256 pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID,
257 ((device & 0xffff) << 16) | (vendor & 0xffff));
258 }
259}
260
261static struct pci_operations ac97_pci_ops = {
262 .set_subsystem = ac97_set_subsystem,
263};
264
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000265static struct device_operations ac97_audio_ops = {
266 .read_resources = pci_dev_read_resources,
267 .set_resources = pci_dev_set_resources,
268 .enable_resources = pci_dev_enable_resources,
269 .init = ac97_audio_init,
270 .scan_bus = 0,
271 .enable = i82801gx_enable,
Stefan Reinauer573f7d42009-07-21 21:50:34 +0000272 .ops_pci = &ac97_pci_ops,
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000273};
274
275static struct device_operations ac97_modem_ops = {
276 .read_resources = pci_dev_read_resources,
277 .set_resources = pci_dev_set_resources,
278 .enable_resources = pci_dev_enable_resources,
279 .init = ac97_modem_init,
280 .scan_bus = 0,
281 .enable = i82801gx_enable,
Stefan Reinauer573f7d42009-07-21 21:50:34 +0000282 .ops_pci = &ac97_pci_ops,
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000283};
284
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000285/* 82801GB/GR/GDH/GBM/GHM (ICH7/ICH7R/ICH7DH/ICH7-M/ICH7-M DH) */
Uwe Hermannbddc6932008-10-29 13:51:31 +0000286/* Note: 82801GU (ICH7-U) doesn't have AC97 audio. */
287static const struct pci_driver i82801gx_ac97_audio __pci_driver = {
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000288 .ops = &ac97_audio_ops,
289 .vendor = PCI_VENDOR_ID_INTEL,
Uwe Hermann5d7a1c82008-10-31 18:41:09 +0000290 .device = 0x27de,
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000291};
292
Uwe Hermannbddc6932008-10-29 13:51:31 +0000293/* 82801GB/GR/GDH/GBM/GHM (ICH7/ICH7R/ICH7DH/ICH7-M/ICH7-M DH) */
294/* Note: 82801GU (ICH7-U) doesn't have AC97 modem. */
295static const struct pci_driver i82801gx_ac97_modem __pci_driver = {
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000296 .ops = &ac97_modem_ops,
297 .vendor = PCI_VENDOR_ID_INTEL,
Uwe Hermann5d7a1c82008-10-31 18:41:09 +0000298 .device = 0x27dd,
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000299};