blob: 5230ace8ef7eb3c8994c7844408345aa87d1db80 [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
Stefan Reinauer54309d62009-01-20 22:53:10 +000037/* FIXME. This table is probably mainboard specific */
38static u16 ac97_function[16*2][4] = {
39 { (1 << 5), (2 << 11), (1 << 10), (3 << 13) },
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};
72
73static u16 nabmbar;
74static u16 nambar;
75
76static int ac97_semaphore(void)
77{
78 int timeout;
79 u8 reg8;
80
81 timeout = 0xffff;
82 do {
83 reg8 = inb(nabmbar + CAS);
84 timeout--;
85 } while ((reg8 & 1) && timeout);
Arthur Heymans3f111b02017-03-09 12:02:52 +010086 if (!timeout)
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +000087 printk(BIOS_DEBUG, "Timeout!\n");
Stefan Reinauer54309d62009-01-20 22:53:10 +000088
89 return (!timeout);
90}
91
92static void init_cnr(void)
93{
94 // TODO
95}
96
97static void program_sigid(struct device *dev, u32 id)
98{
Elyes HAOUASae22fe22020-05-21 09:04:16 +020099 pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID, id);
Stefan Reinauer54309d62009-01-20 22:53:10 +0000100}
101
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000102static void ac97_audio_init(struct device *dev)
103{
Stefan Reinauer54309d62009-01-20 22:53:10 +0000104 u16 reg16;
105 u32 reg32;
106 int i;
107
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000108 printk(BIOS_DEBUG, "Initializing AC'97 Audio.\n");
Stefan Reinauer54309d62009-01-20 22:53:10 +0000109
110 /* top 16 bits are zero, so don't read them */
111 nabmbar = pci_read_config16(dev, NABMBAR) & 0xfffe;
112 nambar = pci_read_config16(dev, NAMBAR) & 0xfffe;
113
114 reg16 = inw(nabmbar + GLOB_CNT);
115 reg16 |= (1 << 1); /* Remove AC_RESET# */
116 outw(reg16, nabmbar + GLOB_CNT);
117
118 /* Wait 600ms. Ouch. */
119 udelay(600 * 1000);
120
121 init_cnr();
122
123 /* Detect Primary AC'97 Codec */
124 reg32 = inl(nabmbar + GLOB_STA);
125 if ((reg32 & ((1 << 28) | (1 << 9) | (1 << 8))) == 0) {
126 /* Primary Codec not found */
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000127 printk(BIOS_DEBUG, "No primary codec. Disabling AC'97 Audio.\n");
Stefan Reinauer54309d62009-01-20 22:53:10 +0000128 return;
129 }
Stefan Reinauer109ab312009-08-12 16:08:05 +0000130
Stefan Reinauer54309d62009-01-20 22:53:10 +0000131 ac97_semaphore();
132
133 /* Detect if codec is programmable */
134 outw(0x8000, nambar + MASTER_VOL);
135 ac97_semaphore();
136 if (inw(nambar + MASTER_VOL) != 0x8000) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000137 printk(BIOS_DEBUG, "Codec not programmable. Disabling AC'97 Audio.\n");
Stefan Reinauer54309d62009-01-20 22:53:10 +0000138 return;
139 }
140
141 /* Program Vendor IDs */
142 reg32 = inw(nambar + VENDOR_ID1);
143 reg32 <<= 16;
144 reg32 |= (u16)inw(nambar + VENDOR_ID2);
145
146 program_sigid(dev, reg32);
147
148 /* Is Codec AC'97 2.3 compliant? */
149 reg16 = inw(nambar + EXT_AUDIO);
150 /* [11:10] = 10b -> AC'97 2.3 */
151 if ((reg16 & 0x0c00) != 0x0800) {
152 /* No 2.3 Codec. We're done */
153 return;
154 }
155
156 /* Select Page 1 */
157 reg16 = inw(nambar + PAGING);
158 reg16 &= 0xfff0;
159 reg16 |= 0x0001;
160 outw(reg16, nambar + PAGING);
161
162 for (i = 0x0a * 2; i > 0; i--) {
163 outw(i, nambar + FUNC_SEL);
164
165 /* Function could not be selected. Next one */
166 if (inw(nambar + FUNC_SEL) != i)
167 continue;
168
169 reg16 = inw(nambar + INFO_IO);
170
171 /* Function Information present? */
172 if (!(reg16 & (1 << 0)))
173 continue;
174
175 /* Function Information valid? */
176 if (!(reg16 & (1 << 4)))
177 continue;
178
179 /* Program Buffer Delay [9:5] */
180 reg16 &= 0x03e0;
181 reg16 |= ac97_function[i][0];
182
183 /* Program Gain [15:11] */
184 reg16 |= ac97_function[i][1];
185
186 /* Program Inversion [10] */
187 reg16 |= ac97_function[i][2];
188
189 outw(reg16, nambar + INFO_IO);
190
191 /* Program Connector / Jack Location */
192 reg16 = inw(nambar + CONNECTOR);
193 reg16 &= 0x1fff;
194 reg16 |= ac97_function[i][3];
195 outw(reg16, nambar + CONNECTOR);
196 }
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000197}
198
199static void ac97_modem_init(struct device *dev)
200{
Stefan Reinauer54309d62009-01-20 22:53:10 +0000201 u16 reg16;
202 u32 reg32;
203 u16 mmbar, mbar;
204
205 mmbar = pci_read_config16(dev, MMBAR) & 0xfffe;
206 mbar = pci_read_config16(dev, MBAR) & 0xfffe;
207
208 reg16 = inw(mmbar + EXT_MODEM_ID1);
Arthur Heymans3f111b02017-03-09 12:02:52 +0100209 if ((reg16 & 0xc000) != 0xc000) {
Stefan Reinauer54309d62009-01-20 22:53:10 +0000210 if (reg16 & (1 << 0)) {
211 reg32 = inw(mmbar + VENDOR_ID2);
212 reg32 <<= 16;
213 reg32 |= (u16)inw(mmbar + VENDOR_ID1);
214 program_sigid(dev, reg32);
215 return;
216 }
217 }
218
219 /* Secondary codec? */
220 reg16 = inw(mbar + SEC_CODEC);
221 if ((reg16 & (1 << 9)) == 0)
222 return;
223
224 reg16 = inw(mmbar + EXT_MODEM_ID2);
225 if ((reg16 & 0xc000) == 0x4000) {
226 if (reg16 & (1 << 0)) {
227 reg32 = inw(mmbar + SEC_VENDOR_ID2);
228 reg32 <<= 16;
229 reg32 |= (u16)inw(mmbar + SEC_VENDOR_ID1);
230 program_sigid(dev, reg32);
231 return;
232 }
233 }
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000234}
235
236static struct device_operations ac97_audio_ops = {
237 .read_resources = pci_dev_read_resources,
238 .set_resources = pci_dev_set_resources,
239 .enable_resources = pci_dev_enable_resources,
240 .init = ac97_audio_init,
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000241 .enable = i82801gx_enable,
Angel Pons1fc0edd2020-05-31 00:03:28 +0200242 .ops_pci = &pci_dev_ops_pci,
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000243};
244
245static 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 .init = ac97_modem_init,
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000250 .enable = i82801gx_enable,
Angel Pons1fc0edd2020-05-31 00:03:28 +0200251 .ops_pci = &pci_dev_ops_pci,
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000252};
253
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000254/* 82801GB/GR/GDH/GBM/GHM (ICH7/ICH7R/ICH7DH/ICH7-M/ICH7-M DH) */
Uwe Hermannbddc6932008-10-29 13:51:31 +0000255/* Note: 82801GU (ICH7-U) doesn't have AC97 audio. */
256static const struct pci_driver i82801gx_ac97_audio __pci_driver = {
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000257 .ops = &ac97_audio_ops,
258 .vendor = PCI_VENDOR_ID_INTEL,
Uwe Hermann5d7a1c82008-10-31 18:41:09 +0000259 .device = 0x27de,
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000260};
261
Uwe Hermannbddc6932008-10-29 13:51:31 +0000262/* 82801GB/GR/GDH/GBM/GHM (ICH7/ICH7R/ICH7DH/ICH7-M/ICH7-M DH) */
263/* Note: 82801GU (ICH7-U) doesn't have AC97 modem. */
264static const struct pci_driver i82801gx_ac97_modem __pci_driver = {
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000265 .ops = &ac97_modem_ops,
266 .vendor = PCI_VENDOR_ID_INTEL,
Uwe Hermann5d7a1c82008-10-31 18:41:09 +0000267 .device = 0x27dd,
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000268};