blob: b0fda296650a03cce558e2a76a33ec7b6a5c17bd [file] [log] [blame]
Angel Pons182dbde2020-04-02 23:49:05 +02001/* SPDX-License-Identifier: GPL-2.0-only */
2/* This file is part of the coreboot project. */
Stefan Reinauer800379f2010-03-01 08:34:19 +00003
Ronald G. Minnich182615d2004-08-24 16:20:46 +00004#include <console/console.h>
5#include <device/device.h>
6#include <device/pci.h>
7#include <device/pci_ids.h>
Stefan Reinauer800379f2010-03-01 08:34:19 +00008#include <arch/io.h>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +02009#include <device/pci_ops.h>
Stefan Reinauer800379f2010-03-01 08:34:19 +000010#include <delay.h>
Stefan Reinauer138be832010-02-27 01:50:21 +000011#include "i82801dx.h"
Ronald G. Minnich182615d2004-08-24 16:20:46 +000012
Stefan Reinauer800379f2010-03-01 08:34:19 +000013#define NAMBAR 0x10
14#define MASTER_VOL 0x02
15#define PAGING 0x24
16#define EXT_AUDIO 0x28
17#define FUNC_SEL 0x66
18#define INFO_IO 0x68
19#define CONNECTOR 0x6a
20#define VENDOR_ID1 0x7c
21#define VENDOR_ID2 0x7e
22#define SEC_VENDOR_ID1 0xfc
23#define SEC_VENDOR_ID2 0xfe
Ronald G. Minnich182615d2004-08-24 16:20:46 +000024
Stefan Reinauer800379f2010-03-01 08:34:19 +000025#define NABMBAR 0x14
26#define GLOB_CNT 0x2c
27#define GLOB_STA 0x30
28#define CAS 0x34
29
30#define MMBAR 0x10
31#define EXT_MODEM_ID1 0x3c
32#define EXT_MODEM_ID2 0xbc
33
34#define MBAR 0x14
35#define SEC_CODEC 0x40
36
37
38/* FIXME. This table is probably mainboard specific */
39static 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) }
72};
73
74static u16 nabmbar;
75static u16 nambar;
76
77static int ac97_semaphore(void)
78{
79 int timeout;
80 u8 reg8;
81
82 timeout = 0xffff;
83 do {
84 reg8 = inb(nabmbar + CAS);
85 timeout--;
86 } while ((reg8 & 1) && timeout);
87 if (! timeout) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +000088 printk(BIOS_DEBUG, "Timeout!\n");
Stefan Reinauer800379f2010-03-01 08:34:19 +000089 }
90
91 return (!timeout);
92}
93
94static void init_cnr(void)
95{
96 // TODO
97}
98
99static void program_sigid(struct device *dev, u32 id)
100{
101 pci_write_config32(dev, 0x2c, id);
102}
103
104static void ac97_audio_init(struct device *dev)
105{
106 u16 reg16;
107 u32 reg32;
108 int i;
109
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000110 printk(BIOS_DEBUG, "Initializing AC'97 Audio.\n");
Stefan Reinauer800379f2010-03-01 08:34:19 +0000111
112 /* top 16 bits are zero, so don't read them */
113 nabmbar = pci_read_config16(dev, NABMBAR) & 0xfffe;
114 nambar = pci_read_config16(dev, NAMBAR) & 0xfffe;
115
116 reg16 = inw(nabmbar + GLOB_CNT);
117 reg16 |= (1 << 1); /* Remove AC_RESET# */
118 outw(reg16, nabmbar + GLOB_CNT);
119
120 /* Wait 600ms. Ouch. */
121 udelay(600 * 1000);
122
123 init_cnr();
124
125 /* Detect Primary AC'97 Codec */
126 reg32 = inl(nabmbar + GLOB_STA);
127 if ((reg32 & ((1 << 28) | (1 << 9) | (1 << 8))) == 0) {
128 /* Primary Codec not found */
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000129 printk(BIOS_DEBUG, "No primary codec. Disabling AC'97 Audio.\n");
Stefan Reinauer800379f2010-03-01 08:34:19 +0000130 return;
131 }
132
133 ac97_semaphore();
134
135 /* Detect if codec is programmable */
136 outw(0x8000, nambar + MASTER_VOL);
137 ac97_semaphore();
138 if (inw(nambar + MASTER_VOL) != 0x8000) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000139 printk(BIOS_DEBUG, "Codec not programmable. Disabling AC'97 Audio.\n");
Stefan Reinauer800379f2010-03-01 08:34:19 +0000140 return;
141 }
142
143 /* Program Vendor IDs */
144 reg32 = inw(nambar + VENDOR_ID1);
145 reg32 <<= 16;
146 reg32 |= (u16)inw(nambar + VENDOR_ID2);
147
148 program_sigid(dev, reg32);
149
150 /* Is Codec AC'97 2.3 compliant? */
151 reg16 = inw(nambar + EXT_AUDIO);
152 /* [11:10] = 10b -> AC'97 2.3 */
153 if ((reg16 & 0x0c00) != 0x0800) {
154 /* No 2.3 Codec. We're done */
155 return;
156 }
157
158 /* Select Page 1 */
159 reg16 = inw(nambar + PAGING);
160 reg16 &= 0xfff0;
161 reg16 |= 0x0001;
162 outw(reg16, nambar + PAGING);
163
164 for (i = 0x0a * 2; i > 0; i--) {
165 outw(i, nambar + FUNC_SEL);
166
167 /* Function could not be selected. Next one */
168 if (inw(nambar + FUNC_SEL) != i)
169 continue;
170
171 reg16 = inw(nambar + INFO_IO);
172
173 /* Function Information present? */
174 if (!(reg16 & (1 << 0)))
175 continue;
176
177 /* Function Information valid? */
178 if (!(reg16 & (1 << 4)))
179 continue;
180
181 /* Program Buffer Delay [9:5] */
182 reg16 &= 0x03e0;
183 reg16 |= ac97_function[i][0];
184
185 /* Program Gain [15:11] */
186 reg16 |= ac97_function[i][1];
187
188 /* Program Inversion [10] */
189 reg16 |= ac97_function[i][2];
190
191 outw(reg16, nambar + INFO_IO);
192
193 /* Program Connector / Jack Location */
194 reg16 = inw(nambar + CONNECTOR);
195 reg16 &= 0x1fff;
196 reg16 |= ac97_function[i][3];
197 outw(reg16, nambar + CONNECTOR);
198 }
199}
200
201static void ac97_modem_init(struct device *dev)
202{
203 u16 reg16;
204 u32 reg32;
205 u16 mmbar, mbar;
206
207 mmbar = pci_read_config16(dev, MMBAR) & 0xfffe;
208 mbar = pci_read_config16(dev, MBAR) & 0xfffe;
209
210 reg16 = inw(mmbar + EXT_MODEM_ID1);
Elyes HAOUAS9c5d4632018-04-26 22:21:21 +0200211 if ((reg16 & 0xc000) != 0xc000) {
Stefan Reinauer800379f2010-03-01 08:34:19 +0000212 if (reg16 & (1 << 0)) {
213 reg32 = inw(mmbar + VENDOR_ID2);
214 reg32 <<= 16;
215 reg32 |= (u16)inw(mmbar + VENDOR_ID1);
216 program_sigid(dev, reg32);
217 return;
218 }
219 }
220
221 /* Secondary codec? */
222 reg16 = inw(mbar + SEC_CODEC);
223 if ((reg16 & (1 << 9)) == 0)
224 return;
225
226 reg16 = inw(mmbar + EXT_MODEM_ID2);
227 if ((reg16 & 0xc000) == 0x4000) {
228 if (reg16 & (1 << 0)) {
229 reg32 = inw(mmbar + SEC_VENDOR_ID2);
230 reg32 <<= 16;
231 reg32 |= (u16)inw(mmbar + SEC_VENDOR_ID1);
232 program_sigid(dev, reg32);
233 return;
234 }
235 }
236}
237
238static struct device_operations ac97_audio_ops = {
Ronald G. Minnich182615d2004-08-24 16:20:46 +0000239 .read_resources = pci_dev_read_resources,
240 .set_resources = pci_dev_set_resources,
241 .enable_resources = pci_dev_enable_resources,
Stefan Reinauer138be832010-02-27 01:50:21 +0000242 .enable = i82801dx_enable,
Stefan Reinauer800379f2010-03-01 08:34:19 +0000243 .init = ac97_audio_init,
Ronald G. Minnich182615d2004-08-24 16:20:46 +0000244 .scan_bus = 0,
245};
246
Stefan Reinauer800379f2010-03-01 08:34:19 +0000247static struct device_operations ac97_modem_ops = {
Ronald G. Minnich182615d2004-08-24 16:20:46 +0000248 .read_resources = pci_dev_read_resources,
249 .set_resources = pci_dev_set_resources,
250 .enable_resources = pci_dev_enable_resources,
Stefan Reinauer138be832010-02-27 01:50:21 +0000251 .enable = i82801dx_enable,
Stefan Reinauer800379f2010-03-01 08:34:19 +0000252 .init = ac97_modem_init,
Ronald G. Minnich182615d2004-08-24 16:20:46 +0000253 .scan_bus = 0,
254};
255
Stefan Reinauer800379f2010-03-01 08:34:19 +0000256/* 82801DB/DBL/DBM (ICH4/ICH4-L/ICH4-M) */
257static const struct pci_driver i82801db_ac97_audio __pci_driver = {
258 .ops = &ac97_audio_ops,
259 .vendor = PCI_VENDOR_ID_INTEL,
260 .device = PCI_DEVICE_ID_INTEL_82801DB_AC97_AUDIO,
Ronald G. Minnich182615d2004-08-24 16:20:46 +0000261};
Stefan Reinauer800379f2010-03-01 08:34:19 +0000262
263static const struct pci_driver i82801db_ac97_modem __pci_driver = {
264 .ops = &ac97_modem_ops,
265 .vendor = PCI_VENDOR_ID_INTEL,
266 .device = PCI_DEVICE_ID_INTEL_82801DB_AC97_MODEM,
267};