Update Kontron board
[coreboot.git] / src / southbridge / intel / i82801gx / i82801gx_ac97.c
1 /*
2  * 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 modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
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.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
19  */
20
21 #include <console/console.h>
22 #include <device/device.h>
23 #include <device/pci.h>
24 #include <device/pci_ids.h>
25 #include <arch/io.h>
26 #include <delay.h>
27 #include "i82801gx.h"
28
29 #define NAMBAR          0x10
30 #define   MASTER_VOL    0x02
31 #define   PAGING        0x24
32 #define   EXT_AUDIO     0x28
33 #define   FUNC_SEL      0x66
34 #define   INFO_IO       0x68
35 #define   CONNECTOR     0x6a
36 #define   VENDOR_ID1    0x7c
37 #define   VENDOR_ID2    0x7e
38 #define   SEC_VENDOR_ID1 0xfc
39 #define   SEC_VENDOR_ID2 0xfe
40
41 #define NABMBAR         0x14
42 #define   GLOB_CNT      0x2c
43 #define   GLOB_STA      0x30
44 #define   CAS           0x34
45
46 #define MMBAR           0x10
47 #define   EXT_MODEM_ID1 0x3c
48 #define   EXT_MODEM_ID2 0xbc
49
50 #define MBAR            0x14
51 #define   SEC_CODEC     0x40
52
53
54 /* FIXME. This table is probably mainboard specific */
55 static u16 ac97_function[16*2][4] = {
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         { (1 << 5), (2 << 11), (1 << 10), (3 << 13) },
86         { (1 << 5), (2 << 11), (1 << 10), (3 << 13) },
87         { (1 << 5), (2 << 11), (1 << 10), (3 << 13) }
88 };
89
90 static u16 nabmbar;
91 static u16 nambar;
92
93 static int ac97_semaphore(void)
94 {
95         int timeout;
96         u8 reg8;
97
98         timeout = 0xffff;
99         do {
100                 reg8 = inb(nabmbar + CAS);
101                 timeout--;
102         } while ((reg8 & 1) && timeout);
103         if (! timeout) {
104                 printk_debug("Timeout!\n");
105         }
106
107         return (!timeout);
108 }
109
110 static void init_cnr(void)
111 {
112         // TODO
113 }
114
115 static void program_sigid(struct device *dev, u32 id)
116 {
117         pci_write_config32(dev, 0x2c, id);
118 }
119
120 static void ac97_audio_init(struct device *dev)
121 {
122         u8 reg8;
123         u16 reg16;
124         u32 reg32;
125         int i;
126
127         printk_debug("Initializing AC'97 Audio.\n");
128
129         /* top 16 bits are zero, so don't read them */
130         nabmbar = pci_read_config16(dev, NABMBAR) & 0xfffe;
131         nambar = pci_read_config16(dev, NAMBAR) & 0xfffe;
132
133         reg16 = inw(nabmbar + GLOB_CNT);
134         reg16 |= (1 << 1); /* Remove AC_RESET# */
135         outw(reg16, nabmbar + GLOB_CNT);
136
137         /* Wait 600ms. Ouch. */
138         udelay(600 * 1000);
139
140         init_cnr();
141
142         /* Detect Primary AC'97 Codec */
143         reg32 = inl(nabmbar + GLOB_STA);
144         if ((reg32 & ((1 << 28) | (1 << 9) | (1 << 8))) == 0) {
145                 /* Primary Codec not found */
146                 printk_debug("No primary codec. Disabling AC'97 Audio.\n");
147                 return;
148         }
149         
150         ac97_semaphore();
151
152         /* Detect if codec is programmable */
153         outw(0x8000, nambar + MASTER_VOL);
154         ac97_semaphore();
155         if (inw(nambar + MASTER_VOL) != 0x8000) {
156                 printk_debug("Codec not programmable. Disabling AC'97 Audio.\n");
157                 return;
158         }
159
160         /* Program Vendor IDs */
161         reg32 = inw(nambar + VENDOR_ID1);
162         reg32 <<= 16;
163         reg32 |= (u16)inw(nambar + VENDOR_ID2);
164
165         program_sigid(dev, reg32);
166
167         /* Is Codec AC'97 2.3 compliant? */
168         reg16 = inw(nambar + EXT_AUDIO);
169         /* [11:10] = 10b -> AC'97 2.3 */
170         if ((reg16 & 0x0c00) != 0x0800) {
171                 /* No 2.3 Codec. We're done */
172                 return;
173         }
174
175         /* Select Page 1 */
176         reg16 = inw(nambar + PAGING);
177         reg16 &= 0xfff0;
178         reg16 |= 0x0001;
179         outw(reg16, nambar + PAGING);
180
181         for (i = 0x0a * 2; i > 0; i--) {
182                 outw(i, nambar + FUNC_SEL);
183
184                 /* Function could not be selected. Next one */
185                 if (inw(nambar + FUNC_SEL) != i)
186                         continue;
187
188                 reg16 = inw(nambar + INFO_IO);
189
190                 /* Function Information present? */
191                 if (!(reg16 & (1 << 0)))
192                         continue;
193
194                 /* Function Information valid? */
195                 if (!(reg16 & (1 << 4)))
196                         continue;
197
198                 /* Program Buffer Delay [9:5] */
199                 reg16 &= 0x03e0;
200                 reg16 |= ac97_function[i][0];
201
202                 /* Program Gain [15:11] */
203                 reg16 |= ac97_function[i][1];
204
205                 /* Program Inversion [10] */
206                 reg16 |= ac97_function[i][2];
207
208                 outw(reg16, nambar + INFO_IO);
209
210                 /* Program Connector / Jack Location */
211                 reg16 = inw(nambar + CONNECTOR);
212                 reg16 &= 0x1fff;
213                 reg16 |= ac97_function[i][3];
214                 outw(reg16, nambar + CONNECTOR);
215         }
216 }
217
218 static void ac97_modem_init(struct device *dev)
219 {
220         u16 reg16;
221         u32 reg32;
222         u16 mmbar, mbar;
223
224         mmbar = pci_read_config16(dev, MMBAR) & 0xfffe;
225         mbar = pci_read_config16(dev, MBAR) & 0xfffe;
226
227         reg16 = inw(mmbar + EXT_MODEM_ID1);
228         if ((reg16 & 0xc000) != 0xc000 ) {
229                 if (reg16 & (1 << 0)) {
230                         reg32 = inw(mmbar + VENDOR_ID2);
231                         reg32 <<= 16;
232                         reg32 |= (u16)inw(mmbar + VENDOR_ID1);
233                         program_sigid(dev, reg32);
234                         return;
235                 }
236         }
237
238         /* Secondary codec? */
239         reg16 = inw(mbar + SEC_CODEC);
240         if ((reg16 & (1 << 9)) == 0)
241                 return;
242
243         reg16 = inw(mmbar + EXT_MODEM_ID2);
244         if ((reg16 & 0xc000) == 0x4000) {
245                 if (reg16 & (1 << 0)) {
246                         reg32 = inw(mmbar + SEC_VENDOR_ID2);
247                         reg32 <<= 16;
248                         reg32 |= (u16)inw(mmbar + SEC_VENDOR_ID1);
249                         program_sigid(dev, reg32);
250                         return;
251                 }
252         }
253 }
254
255 static struct device_operations ac97_audio_ops = {
256         .read_resources         = pci_dev_read_resources,
257         .set_resources          = pci_dev_set_resources,
258         .enable_resources       = pci_dev_enable_resources,
259         .init                   = ac97_audio_init,
260         .scan_bus               = 0,
261         .enable                 = i82801gx_enable,
262 };
263
264 static struct device_operations ac97_modem_ops = {
265         .read_resources         = pci_dev_read_resources,
266         .set_resources          = pci_dev_set_resources,
267         .enable_resources       = pci_dev_enable_resources,
268         .init                   = ac97_modem_init,
269         .scan_bus               = 0,
270         .enable                 = i82801gx_enable,
271 };
272
273 /* 82801GB/GR/GDH/GBM/GHM (ICH7/ICH7R/ICH7DH/ICH7-M/ICH7-M DH) */
274 /* Note: 82801GU (ICH7-U) doesn't have AC97 audio. */
275 static const struct pci_driver i82801gx_ac97_audio __pci_driver = {
276         .ops    = &ac97_audio_ops,
277         .vendor = PCI_VENDOR_ID_INTEL,
278         .device = 0x27de,
279 };
280
281 /* 82801GB/GR/GDH/GBM/GHM (ICH7/ICH7R/ICH7DH/ICH7-M/ICH7-M DH) */
282 /* Note: 82801GU (ICH7-U) doesn't have AC97 modem. */
283 static const struct pci_driver i82801gx_ac97_modem __pci_driver = {
284         .ops    = &ac97_modem_ops,
285         .vendor = PCI_VENDOR_ID_INTEL,
286         .device = 0x27dd,
287 };