blob: f9a7904408d77335b5370cc0e999c0ccaa163c47 [file] [log] [blame]
Ronald G. Minnich182615d2004-08-24 16:20:46 +00001/*
Stefan Reinauer800379f2010-03-01 08:34:19 +00002 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2008-2009 coresystems GmbH
5 *
6 * 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.
10 *
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.
Ronald G. Minnich182615d2004-08-24 16:20:46 +000015 */
Stefan Reinauer800379f2010-03-01 08:34:19 +000016
Ronald G. Minnich182615d2004-08-24 16:20:46 +000017#include <console/console.h>
18#include <device/device.h>
19#include <device/pci.h>
20#include <device/pci_ids.h>
Stefan Reinauer800379f2010-03-01 08:34:19 +000021#include <arch/io.h>
22#include <delay.h>
Stefan Reinauer138be832010-02-27 01:50:21 +000023#include "i82801dx.h"
Ronald G. Minnich182615d2004-08-24 16:20:46 +000024
Stefan Reinauer800379f2010-03-01 08:34:19 +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
Ronald G. Minnich182615d2004-08-24 16:20:46 +000036
Stefan Reinauer800379f2010-03-01 08:34:19 +000037#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);
99 if (! timeout) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000100 printk(BIOS_DEBUG, "Timeout!\n");
Stefan Reinauer800379f2010-03-01 08:34:19 +0000101 }
102
103 return (!timeout);
104}
105
106static void init_cnr(void)
107{
108 // TODO
109}
110
111static void program_sigid(struct device *dev, u32 id)
112{
113 pci_write_config32(dev, 0x2c, id);
114}
115
116static void ac97_audio_init(struct device *dev)
117{
118 u16 reg16;
119 u32 reg32;
120 int i;
121
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000122 printk(BIOS_DEBUG, "Initializing AC'97 Audio.\n");
Stefan Reinauer800379f2010-03-01 08:34:19 +0000123
124 /* top 16 bits are zero, so don't read them */
125 nabmbar = pci_read_config16(dev, NABMBAR) & 0xfffe;
126 nambar = pci_read_config16(dev, NAMBAR) & 0xfffe;
127
128 reg16 = inw(nabmbar + GLOB_CNT);
129 reg16 |= (1 << 1); /* Remove AC_RESET# */
130 outw(reg16, nabmbar + GLOB_CNT);
131
132 /* Wait 600ms. Ouch. */
133 udelay(600 * 1000);
134
135 init_cnr();
136
137 /* Detect Primary AC'97 Codec */
138 reg32 = inl(nabmbar + GLOB_STA);
139 if ((reg32 & ((1 << 28) | (1 << 9) | (1 << 8))) == 0) {
140 /* Primary Codec not found */
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000141 printk(BIOS_DEBUG, "No primary codec. Disabling AC'97 Audio.\n");
Stefan Reinauer800379f2010-03-01 08:34:19 +0000142 return;
143 }
144
145 ac97_semaphore();
146
147 /* Detect if codec is programmable */
148 outw(0x8000, nambar + MASTER_VOL);
149 ac97_semaphore();
150 if (inw(nambar + MASTER_VOL) != 0x8000) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000151 printk(BIOS_DEBUG, "Codec not programmable. Disabling AC'97 Audio.\n");
Stefan Reinauer800379f2010-03-01 08:34:19 +0000152 return;
153 }
154
155 /* Program Vendor IDs */
156 reg32 = inw(nambar + VENDOR_ID1);
157 reg32 <<= 16;
158 reg32 |= (u16)inw(nambar + VENDOR_ID2);
159
160 program_sigid(dev, reg32);
161
162 /* Is Codec AC'97 2.3 compliant? */
163 reg16 = inw(nambar + EXT_AUDIO);
164 /* [11:10] = 10b -> AC'97 2.3 */
165 if ((reg16 & 0x0c00) != 0x0800) {
166 /* No 2.3 Codec. We're done */
167 return;
168 }
169
170 /* Select Page 1 */
171 reg16 = inw(nambar + PAGING);
172 reg16 &= 0xfff0;
173 reg16 |= 0x0001;
174 outw(reg16, nambar + PAGING);
175
176 for (i = 0x0a * 2; i > 0; i--) {
177 outw(i, nambar + FUNC_SEL);
178
179 /* Function could not be selected. Next one */
180 if (inw(nambar + FUNC_SEL) != i)
181 continue;
182
183 reg16 = inw(nambar + INFO_IO);
184
185 /* Function Information present? */
186 if (!(reg16 & (1 << 0)))
187 continue;
188
189 /* Function Information valid? */
190 if (!(reg16 & (1 << 4)))
191 continue;
192
193 /* Program Buffer Delay [9:5] */
194 reg16 &= 0x03e0;
195 reg16 |= ac97_function[i][0];
196
197 /* Program Gain [15:11] */
198 reg16 |= ac97_function[i][1];
199
200 /* Program Inversion [10] */
201 reg16 |= ac97_function[i][2];
202
203 outw(reg16, nambar + INFO_IO);
204
205 /* Program Connector / Jack Location */
206 reg16 = inw(nambar + CONNECTOR);
207 reg16 &= 0x1fff;
208 reg16 |= ac97_function[i][3];
209 outw(reg16, nambar + CONNECTOR);
210 }
211}
212
213static void ac97_modem_init(struct device *dev)
214{
215 u16 reg16;
216 u32 reg32;
217 u16 mmbar, mbar;
218
219 mmbar = pci_read_config16(dev, MMBAR) & 0xfffe;
220 mbar = pci_read_config16(dev, MBAR) & 0xfffe;
221
222 reg16 = inw(mmbar + EXT_MODEM_ID1);
Elyes HAOUAS9c5d4632018-04-26 22:21:21 +0200223 if ((reg16 & 0xc000) != 0xc000) {
Stefan Reinauer800379f2010-03-01 08:34:19 +0000224 if (reg16 & (1 << 0)) {
225 reg32 = inw(mmbar + VENDOR_ID2);
226 reg32 <<= 16;
227 reg32 |= (u16)inw(mmbar + VENDOR_ID1);
228 program_sigid(dev, reg32);
229 return;
230 }
231 }
232
233 /* Secondary codec? */
234 reg16 = inw(mbar + SEC_CODEC);
235 if ((reg16 & (1 << 9)) == 0)
236 return;
237
238 reg16 = inw(mmbar + EXT_MODEM_ID2);
239 if ((reg16 & 0xc000) == 0x4000) {
240 if (reg16 & (1 << 0)) {
241 reg32 = inw(mmbar + SEC_VENDOR_ID2);
242 reg32 <<= 16;
243 reg32 |= (u16)inw(mmbar + SEC_VENDOR_ID1);
244 program_sigid(dev, reg32);
245 return;
246 }
247 }
248}
249
250static struct device_operations ac97_audio_ops = {
Ronald G. Minnich182615d2004-08-24 16:20:46 +0000251 .read_resources = pci_dev_read_resources,
252 .set_resources = pci_dev_set_resources,
253 .enable_resources = pci_dev_enable_resources,
Stefan Reinauer138be832010-02-27 01:50:21 +0000254 .enable = i82801dx_enable,
Stefan Reinauer800379f2010-03-01 08:34:19 +0000255 .init = ac97_audio_init,
Ronald G. Minnich182615d2004-08-24 16:20:46 +0000256 .scan_bus = 0,
257};
258
Stefan Reinauer800379f2010-03-01 08:34:19 +0000259static struct device_operations ac97_modem_ops = {
Ronald G. Minnich182615d2004-08-24 16:20:46 +0000260 .read_resources = pci_dev_read_resources,
261 .set_resources = pci_dev_set_resources,
262 .enable_resources = pci_dev_enable_resources,
Stefan Reinauer138be832010-02-27 01:50:21 +0000263 .enable = i82801dx_enable,
Stefan Reinauer800379f2010-03-01 08:34:19 +0000264 .init = ac97_modem_init,
Ronald G. Minnich182615d2004-08-24 16:20:46 +0000265 .scan_bus = 0,
266};
267
Stefan Reinauer800379f2010-03-01 08:34:19 +0000268/* 82801DB/DBL/DBM (ICH4/ICH4-L/ICH4-M) */
269static const struct pci_driver i82801db_ac97_audio __pci_driver = {
270 .ops = &ac97_audio_ops,
271 .vendor = PCI_VENDOR_ID_INTEL,
272 .device = PCI_DEVICE_ID_INTEL_82801DB_AC97_AUDIO,
Ronald G. Minnich182615d2004-08-24 16:20:46 +0000273};
Stefan Reinauer800379f2010-03-01 08:34:19 +0000274
275static const struct pci_driver i82801db_ac97_modem __pci_driver = {
276 .ops = &ac97_modem_ops,
277 .vendor = PCI_VENDOR_ID_INTEL,
278 .device = PCI_DEVICE_ID_INTEL_82801DB_AC97_MODEM,
279};