blob: cc6e3761305d8c25f83759af0b40160b82ed01e5 [file] [log] [blame]
Angel Pons182dbde2020-04-02 23:49:05 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Stefan Reinauerdebb11f2008-10-29 04:46:52 +00002
3#include <console/console.h>
4#include <device/device.h>
5#include <device/pci.h>
6#include <device/pci_ids.h>
Elyes HAOUASae22fe22020-05-21 09:04:16 +02007#include <device/pci_def.h>
Stefan Reinauer54309d62009-01-20 22:53:10 +00008#include <arch/io.h>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +02009#include <device/pci_ops.h>
Stefan Reinauer54309d62009-01-20 22:53:10 +000010#include <delay.h>
Stefan Reinauerdebb11f2008-10-29 04:46:52 +000011#include "i82801gx.h"
12
Stefan Reinauer54309d62009-01-20 22:53:10 +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
24
25#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);
Arthur Heymans3f111b02017-03-09 12:02:52 +010087 if (!timeout)
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +000088 printk(BIOS_DEBUG, "Timeout!\n");
Stefan Reinauer54309d62009-01-20 22:53:10 +000089
90 return (!timeout);
91}
92
93static void init_cnr(void)
94{
95 // TODO
96}
97
98static void program_sigid(struct device *dev, u32 id)
99{
Elyes HAOUASae22fe22020-05-21 09:04:16 +0200100 pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID, id);
Stefan Reinauer54309d62009-01-20 22:53:10 +0000101}
102
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000103static void ac97_audio_init(struct device *dev)
104{
Stefan Reinauer54309d62009-01-20 22:53:10 +0000105 u16 reg16;
106 u32 reg32;
107 int i;
108
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000109 printk(BIOS_DEBUG, "Initializing AC'97 Audio.\n");
Stefan Reinauer54309d62009-01-20 22:53:10 +0000110
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;
114
115 reg16 = inw(nabmbar + GLOB_CNT);
116 reg16 |= (1 << 1); /* Remove AC_RESET# */
117 outw(reg16, nabmbar + GLOB_CNT);
118
119 /* Wait 600ms. Ouch. */
120 udelay(600 * 1000);
121
122 init_cnr();
123
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 */
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000128 printk(BIOS_DEBUG, "No primary codec. Disabling AC'97 Audio.\n");
Stefan Reinauer54309d62009-01-20 22:53:10 +0000129 return;
130 }
Stefan Reinauer109ab312009-08-12 16:08:05 +0000131
Stefan Reinauer54309d62009-01-20 22:53:10 +0000132 ac97_semaphore();
133
134 /* Detect if codec is programmable */
135 outw(0x8000, nambar + MASTER_VOL);
136 ac97_semaphore();
137 if (inw(nambar + MASTER_VOL) != 0x8000) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000138 printk(BIOS_DEBUG, "Codec not programmable. Disabling AC'97 Audio.\n");
Stefan Reinauer54309d62009-01-20 22:53:10 +0000139 return;
140 }
141
142 /* Program Vendor IDs */
143 reg32 = inw(nambar + VENDOR_ID1);
144 reg32 <<= 16;
145 reg32 |= (u16)inw(nambar + VENDOR_ID2);
146
147 program_sigid(dev, reg32);
148
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 */
154 return;
155 }
156
157 /* Select Page 1 */
158 reg16 = inw(nambar + PAGING);
159 reg16 &= 0xfff0;
160 reg16 |= 0x0001;
161 outw(reg16, nambar + PAGING);
162
163 for (i = 0x0a * 2; i > 0; i--) {
164 outw(i, nambar + FUNC_SEL);
165
166 /* Function could not be selected. Next one */
167 if (inw(nambar + FUNC_SEL) != i)
168 continue;
169
170 reg16 = inw(nambar + INFO_IO);
171
172 /* Function Information present? */
173 if (!(reg16 & (1 << 0)))
174 continue;
175
176 /* Function Information valid? */
177 if (!(reg16 & (1 << 4)))
178 continue;
179
180 /* Program Buffer Delay [9:5] */
181 reg16 &= 0x03e0;
182 reg16 |= ac97_function[i][0];
183
184 /* Program Gain [15:11] */
185 reg16 |= ac97_function[i][1];
186
187 /* Program Inversion [10] */
188 reg16 |= ac97_function[i][2];
189
190 outw(reg16, nambar + INFO_IO);
191
192 /* Program Connector / Jack Location */
193 reg16 = inw(nambar + CONNECTOR);
194 reg16 &= 0x1fff;
195 reg16 |= ac97_function[i][3];
196 outw(reg16, nambar + CONNECTOR);
197 }
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000198}
199
200static void ac97_modem_init(struct device *dev)
201{
Stefan Reinauer54309d62009-01-20 22:53:10 +0000202 u16 reg16;
203 u32 reg32;
204 u16 mmbar, mbar;
205
206 mmbar = pci_read_config16(dev, MMBAR) & 0xfffe;
207 mbar = pci_read_config16(dev, MBAR) & 0xfffe;
208
209 reg16 = inw(mmbar + EXT_MODEM_ID1);
Arthur Heymans3f111b02017-03-09 12:02:52 +0100210 if ((reg16 & 0xc000) != 0xc000) {
Stefan Reinauer54309d62009-01-20 22:53:10 +0000211 if (reg16 & (1 << 0)) {
212 reg32 = inw(mmbar + VENDOR_ID2);
213 reg32 <<= 16;
214 reg32 |= (u16)inw(mmbar + VENDOR_ID1);
215 program_sigid(dev, reg32);
216 return;
217 }
218 }
219
220 /* Secondary codec? */
221 reg16 = inw(mbar + SEC_CODEC);
222 if ((reg16 & (1 << 9)) == 0)
223 return;
224
225 reg16 = inw(mmbar + EXT_MODEM_ID2);
226 if ((reg16 & 0xc000) == 0x4000) {
227 if (reg16 & (1 << 0)) {
228 reg32 = inw(mmbar + SEC_VENDOR_ID2);
229 reg32 <<= 16;
230 reg32 |= (u16)inw(mmbar + SEC_VENDOR_ID1);
231 program_sigid(dev, reg32);
232 return;
233 }
234 }
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000235}
236
237static 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 .init = ac97_audio_init,
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000242 .enable = i82801gx_enable,
Angel Pons1fc0edd2020-05-31 00:03:28 +0200243 .ops_pci = &pci_dev_ops_pci,
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000244};
245
246static struct device_operations ac97_modem_ops = {
247 .read_resources = pci_dev_read_resources,
248 .set_resources = pci_dev_set_resources,
249 .enable_resources = pci_dev_enable_resources,
250 .init = ac97_modem_init,
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000251 .enable = i82801gx_enable,
Angel Pons1fc0edd2020-05-31 00:03:28 +0200252 .ops_pci = &pci_dev_ops_pci,
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000253};
254
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000255/* 82801GB/GR/GDH/GBM/GHM (ICH7/ICH7R/ICH7DH/ICH7-M/ICH7-M DH) */
Uwe Hermannbddc6932008-10-29 13:51:31 +0000256/* Note: 82801GU (ICH7-U) doesn't have AC97 audio. */
257static const struct pci_driver i82801gx_ac97_audio __pci_driver = {
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000258 .ops = &ac97_audio_ops,
259 .vendor = PCI_VENDOR_ID_INTEL,
Uwe Hermann5d7a1c82008-10-31 18:41:09 +0000260 .device = 0x27de,
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000261};
262
Uwe Hermannbddc6932008-10-29 13:51:31 +0000263/* 82801GB/GR/GDH/GBM/GHM (ICH7/ICH7R/ICH7DH/ICH7-M/ICH7-M DH) */
264/* Note: 82801GU (ICH7-U) doesn't have AC97 modem. */
265static const struct pci_driver i82801gx_ac97_modem __pci_driver = {
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000266 .ops = &ac97_modem_ops,
267 .vendor = PCI_VENDOR_ID_INTEL,
Uwe Hermann5d7a1c82008-10-31 18:41:09 +0000268 .device = 0x27dd,
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000269};