blob: cbbc370252fc5e13c7b84eff5f64226e89d2a8b5 [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>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +020022#include <device/pci_ops.h>
Stefan Reinauer800379f2010-03-01 08:34:19 +000023#include <delay.h>
Stefan Reinauer138be832010-02-27 01:50:21 +000024#include "i82801dx.h"
Ronald G. Minnich182615d2004-08-24 16:20:46 +000025
Stefan Reinauer800379f2010-03-01 08:34:19 +000026#define NAMBAR 0x10
27#define MASTER_VOL 0x02
28#define PAGING 0x24
29#define EXT_AUDIO 0x28
30#define FUNC_SEL 0x66
31#define INFO_IO 0x68
32#define CONNECTOR 0x6a
33#define VENDOR_ID1 0x7c
34#define VENDOR_ID2 0x7e
35#define SEC_VENDOR_ID1 0xfc
36#define SEC_VENDOR_ID2 0xfe
Ronald G. Minnich182615d2004-08-24 16:20:46 +000037
Stefan Reinauer800379f2010-03-01 08:34:19 +000038#define NABMBAR 0x14
39#define GLOB_CNT 0x2c
40#define GLOB_STA 0x30
41#define CAS 0x34
42
43#define MMBAR 0x10
44#define EXT_MODEM_ID1 0x3c
45#define EXT_MODEM_ID2 0xbc
46
47#define MBAR 0x14
48#define SEC_CODEC 0x40
49
50
51/* FIXME. This table is probably mainboard specific */
52static u16 ac97_function[16*2][4] = {
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 { (1 << 5), (2 << 11), (1 << 10), (3 << 13) }
85};
86
87static u16 nabmbar;
88static u16 nambar;
89
90static int ac97_semaphore(void)
91{
92 int timeout;
93 u8 reg8;
94
95 timeout = 0xffff;
96 do {
97 reg8 = inb(nabmbar + CAS);
98 timeout--;
99 } while ((reg8 & 1) && timeout);
100 if (! timeout) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000101 printk(BIOS_DEBUG, "Timeout!\n");
Stefan Reinauer800379f2010-03-01 08:34:19 +0000102 }
103
104 return (!timeout);
105}
106
107static void init_cnr(void)
108{
109 // TODO
110}
111
112static void program_sigid(struct device *dev, u32 id)
113{
114 pci_write_config32(dev, 0x2c, id);
115}
116
117static void ac97_audio_init(struct device *dev)
118{
119 u16 reg16;
120 u32 reg32;
121 int i;
122
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000123 printk(BIOS_DEBUG, "Initializing AC'97 Audio.\n");
Stefan Reinauer800379f2010-03-01 08:34:19 +0000124
125 /* top 16 bits are zero, so don't read them */
126 nabmbar = pci_read_config16(dev, NABMBAR) & 0xfffe;
127 nambar = pci_read_config16(dev, NAMBAR) & 0xfffe;
128
129 reg16 = inw(nabmbar + GLOB_CNT);
130 reg16 |= (1 << 1); /* Remove AC_RESET# */
131 outw(reg16, nabmbar + GLOB_CNT);
132
133 /* Wait 600ms. Ouch. */
134 udelay(600 * 1000);
135
136 init_cnr();
137
138 /* Detect Primary AC'97 Codec */
139 reg32 = inl(nabmbar + GLOB_STA);
140 if ((reg32 & ((1 << 28) | (1 << 9) | (1 << 8))) == 0) {
141 /* Primary Codec not found */
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000142 printk(BIOS_DEBUG, "No primary codec. Disabling AC'97 Audio.\n");
Stefan Reinauer800379f2010-03-01 08:34:19 +0000143 return;
144 }
145
146 ac97_semaphore();
147
148 /* Detect if codec is programmable */
149 outw(0x8000, nambar + MASTER_VOL);
150 ac97_semaphore();
151 if (inw(nambar + MASTER_VOL) != 0x8000) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000152 printk(BIOS_DEBUG, "Codec not programmable. Disabling AC'97 Audio.\n");
Stefan Reinauer800379f2010-03-01 08:34:19 +0000153 return;
154 }
155
156 /* Program Vendor IDs */
157 reg32 = inw(nambar + VENDOR_ID1);
158 reg32 <<= 16;
159 reg32 |= (u16)inw(nambar + VENDOR_ID2);
160
161 program_sigid(dev, reg32);
162
163 /* Is Codec AC'97 2.3 compliant? */
164 reg16 = inw(nambar + EXT_AUDIO);
165 /* [11:10] = 10b -> AC'97 2.3 */
166 if ((reg16 & 0x0c00) != 0x0800) {
167 /* No 2.3 Codec. We're done */
168 return;
169 }
170
171 /* Select Page 1 */
172 reg16 = inw(nambar + PAGING);
173 reg16 &= 0xfff0;
174 reg16 |= 0x0001;
175 outw(reg16, nambar + PAGING);
176
177 for (i = 0x0a * 2; i > 0; i--) {
178 outw(i, nambar + FUNC_SEL);
179
180 /* Function could not be selected. Next one */
181 if (inw(nambar + FUNC_SEL) != i)
182 continue;
183
184 reg16 = inw(nambar + INFO_IO);
185
186 /* Function Information present? */
187 if (!(reg16 & (1 << 0)))
188 continue;
189
190 /* Function Information valid? */
191 if (!(reg16 & (1 << 4)))
192 continue;
193
194 /* Program Buffer Delay [9:5] */
195 reg16 &= 0x03e0;
196 reg16 |= ac97_function[i][0];
197
198 /* Program Gain [15:11] */
199 reg16 |= ac97_function[i][1];
200
201 /* Program Inversion [10] */
202 reg16 |= ac97_function[i][2];
203
204 outw(reg16, nambar + INFO_IO);
205
206 /* Program Connector / Jack Location */
207 reg16 = inw(nambar + CONNECTOR);
208 reg16 &= 0x1fff;
209 reg16 |= ac97_function[i][3];
210 outw(reg16, nambar + CONNECTOR);
211 }
212}
213
214static void ac97_modem_init(struct device *dev)
215{
216 u16 reg16;
217 u32 reg32;
218 u16 mmbar, mbar;
219
220 mmbar = pci_read_config16(dev, MMBAR) & 0xfffe;
221 mbar = pci_read_config16(dev, MBAR) & 0xfffe;
222
223 reg16 = inw(mmbar + EXT_MODEM_ID1);
Elyes HAOUAS9c5d4632018-04-26 22:21:21 +0200224 if ((reg16 & 0xc000) != 0xc000) {
Stefan Reinauer800379f2010-03-01 08:34:19 +0000225 if (reg16 & (1 << 0)) {
226 reg32 = inw(mmbar + VENDOR_ID2);
227 reg32 <<= 16;
228 reg32 |= (u16)inw(mmbar + VENDOR_ID1);
229 program_sigid(dev, reg32);
230 return;
231 }
232 }
233
234 /* Secondary codec? */
235 reg16 = inw(mbar + SEC_CODEC);
236 if ((reg16 & (1 << 9)) == 0)
237 return;
238
239 reg16 = inw(mmbar + EXT_MODEM_ID2);
240 if ((reg16 & 0xc000) == 0x4000) {
241 if (reg16 & (1 << 0)) {
242 reg32 = inw(mmbar + SEC_VENDOR_ID2);
243 reg32 <<= 16;
244 reg32 |= (u16)inw(mmbar + SEC_VENDOR_ID1);
245 program_sigid(dev, reg32);
246 return;
247 }
248 }
249}
250
251static struct device_operations ac97_audio_ops = {
Ronald G. Minnich182615d2004-08-24 16:20:46 +0000252 .read_resources = pci_dev_read_resources,
253 .set_resources = pci_dev_set_resources,
254 .enable_resources = pci_dev_enable_resources,
Stefan Reinauer138be832010-02-27 01:50:21 +0000255 .enable = i82801dx_enable,
Stefan Reinauer800379f2010-03-01 08:34:19 +0000256 .init = ac97_audio_init,
Ronald G. Minnich182615d2004-08-24 16:20:46 +0000257 .scan_bus = 0,
258};
259
Stefan Reinauer800379f2010-03-01 08:34:19 +0000260static struct device_operations ac97_modem_ops = {
Ronald G. Minnich182615d2004-08-24 16:20:46 +0000261 .read_resources = pci_dev_read_resources,
262 .set_resources = pci_dev_set_resources,
263 .enable_resources = pci_dev_enable_resources,
Stefan Reinauer138be832010-02-27 01:50:21 +0000264 .enable = i82801dx_enable,
Stefan Reinauer800379f2010-03-01 08:34:19 +0000265 .init = ac97_modem_init,
Ronald G. Minnich182615d2004-08-24 16:20:46 +0000266 .scan_bus = 0,
267};
268
Stefan Reinauer800379f2010-03-01 08:34:19 +0000269/* 82801DB/DBL/DBM (ICH4/ICH4-L/ICH4-M) */
270static const struct pci_driver i82801db_ac97_audio __pci_driver = {
271 .ops = &ac97_audio_ops,
272 .vendor = PCI_VENDOR_ID_INTEL,
273 .device = PCI_DEVICE_ID_INTEL_82801DB_AC97_AUDIO,
Ronald G. Minnich182615d2004-08-24 16:20:46 +0000274};
Stefan Reinauer800379f2010-03-01 08:34:19 +0000275
276static const struct pci_driver i82801db_ac97_modem __pci_driver = {
277 .ops = &ac97_modem_ops,
278 .vendor = PCI_VENDOR_ID_INTEL,
279 .device = PCI_DEVICE_ID_INTEL_82801DB_AC97_MODEM,
280};