blob: 2b9b24579b3174b34fc9838f72ef965c61550b19 [file] [log] [blame]
Stefan Reinauer679c9f92009-01-20 22:54:59 +00001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2008 Advanced Micro Devices, Inc.
5 * Copyright (C) 2008-2009 coresystems GmbH
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; version 2 of 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.
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 <device/pci_ops.h>
26#include <arch/io.h>
27#include <delay.h>
28#include "i82801gx.h"
29
30#define HDA_ICII_REG 0x68
31#define HDA_ICII_BUSY (1 << 0)
32#define HDA_ICII_VALID (1 << 1)
33
Stefan Reinauera8e11682009-03-11 14:54:18 +000034typedef struct southbridge_intel_i82801gx_config config_t;
35
Stefan Reinauer679c9f92009-01-20 22:54:59 +000036static int set_bits(u8 * port, u32 mask, u32 val)
37{
Stefan Reinauera8e11682009-03-11 14:54:18 +000038 u32 reg32;
Stefan Reinauer679c9f92009-01-20 22:54:59 +000039 int count;
40
Stefan Reinauera8e11682009-03-11 14:54:18 +000041 /* Write (val & mask) to port */
Stefan Reinauer679c9f92009-01-20 22:54:59 +000042 val &= mask;
Stefan Reinauer9fe4d792010-01-16 17:53:38 +000043 reg32 = read32(port);
Stefan Reinauera8e11682009-03-11 14:54:18 +000044 reg32 &= ~mask;
45 reg32 |= val;
Stefan Reinauer9fe4d792010-01-16 17:53:38 +000046 write32(port, reg32);
Stefan Reinauer679c9f92009-01-20 22:54:59 +000047
Stefan Reinauer109ab312009-08-12 16:08:05 +000048 /* Wait for readback of register to
49 * match what was just written to it
Stefan Reinauera8e11682009-03-11 14:54:18 +000050 */
Stefan Reinauer679c9f92009-01-20 22:54:59 +000051 count = 50;
52 do {
Stefan Reinauera8e11682009-03-11 14:54:18 +000053 /* Wait 1ms based on BKDG wait time */
54 mdelay(1);
Stefan Reinauer9fe4d792010-01-16 17:53:38 +000055 reg32 = read32(port);
Stefan Reinauera8e11682009-03-11 14:54:18 +000056 reg32 &= mask;
57 } while ((reg32 != val) && --count);
Stefan Reinauer679c9f92009-01-20 22:54:59 +000058
Stefan Reinauera8e11682009-03-11 14:54:18 +000059 /* Timeout occured */
Stefan Reinauer679c9f92009-01-20 22:54:59 +000060 if (!count)
61 return -1;
Stefan Reinauer679c9f92009-01-20 22:54:59 +000062 return 0;
63}
64
65static int codec_detect(u8 * base)
66{
Stefan Reinauera8e11682009-03-11 14:54:18 +000067 u32 reg32;
Stefan Reinauer679c9f92009-01-20 22:54:59 +000068
Stefan Reinauera8e11682009-03-11 14:54:18 +000069 /* Set Bit0 to 0 to enter reset state (BAR + 0x8)[0] */
Stefan Reinauer109ab312009-08-12 16:08:05 +000070 if (set_bits(base + 0x08, 1, 0) == -1)
Stefan Reinauera8e11682009-03-11 14:54:18 +000071 goto no_codec;
Stefan Reinauer679c9f92009-01-20 22:54:59 +000072
Stefan Reinauera8e11682009-03-11 14:54:18 +000073 /* Set Bit 0 to 1 to exit reset state (BAR + 0x8)[0] */
Stefan Reinauer109ab312009-08-12 16:08:05 +000074 if (set_bits(base + 0x08, 1, 1) == -1)
Stefan Reinauera8e11682009-03-11 14:54:18 +000075 goto no_codec;
Stefan Reinauer679c9f92009-01-20 22:54:59 +000076
Stefan Reinauera8e11682009-03-11 14:54:18 +000077 /* Read in Codec location (BAR + 0xe)[2..0]*/
Stefan Reinauer9fe4d792010-01-16 17:53:38 +000078 reg32 = read32(base + 0xe);
Stefan Reinauera8e11682009-03-11 14:54:18 +000079 reg32 &= 0x0f;
80 if (!reg32)
81 goto no_codec;
Stefan Reinauer109ab312009-08-12 16:08:05 +000082
Stefan Reinauera8e11682009-03-11 14:54:18 +000083 return reg32;
84
85no_codec:
86 /* Codec Not found */
87 /* Put HDA back in reset (BAR + 0x8) [0] */
Stefan Reinauer679c9f92009-01-20 22:54:59 +000088 set_bits(base + 0x08, 1, 0);
Stefan Reinauera8e11682009-03-11 14:54:18 +000089 printk_debug("Azalia: No codec!\n");
90 return 0;
Stefan Reinauer679c9f92009-01-20 22:54:59 +000091}
92
Stefan Reinaueraca6ec62009-10-26 17:12:21 +000093u32 * cim_verb_data = NULL;
94u32 cim_verb_data_size = 0;
Stefan Reinauer679c9f92009-01-20 22:54:59 +000095
Stefan Reinaueraca6ec62009-10-26 17:12:21 +000096static u32 find_verb(struct device *dev, u32 viddid, u32 ** verb)
Stefan Reinauer679c9f92009-01-20 22:54:59 +000097{
Stefan Reinaueraca6ec62009-10-26 17:12:21 +000098 int idx=0;
99
100 while (idx < (cim_verb_data_size / sizeof(u32))) {
101 u32 verb_size = 4 * cim_verb_data[idx+2]; // in u32
102 if (cim_verb_data[idx] != viddid) {
103 idx += verb_size + 3; // skip verb + header
104 continue;
105 }
106 *verb = &cim_verb_data[idx+3];
107 return verb_size;
Stefan Reinauera8e11682009-03-11 14:54:18 +0000108 }
109
Stefan Reinaueraca6ec62009-10-26 17:12:21 +0000110 /* Not all codecs need to load another verb */
111 return 0;
Stefan Reinauer679c9f92009-01-20 22:54:59 +0000112}
113
114/**
115 * Wait 50usec for for the codec to indicate it is ready
116 * no response would imply that the codec is non-operative
117 */
118
119static int wait_for_ready(u8 *base)
120{
121 /* Use a 50 usec timeout - the Linux kernel uses the
122 * same duration */
123
124 int timeout = 50;
125
126 while(timeout--) {
Stefan Reinauer9fe4d792010-01-16 17:53:38 +0000127 u32 reg32 = read32(base + HDA_ICII_REG);
Stefan Reinauera8e11682009-03-11 14:54:18 +0000128 if (!(reg32 & HDA_ICII_BUSY))
Stefan Reinauer679c9f92009-01-20 22:54:59 +0000129 return 0;
130 udelay(1);
131 }
132
133 return -1;
134}
135
136/**
137 * Wait 50usec for for the codec to indicate that it accepted
138 * the previous command. No response would imply that the code
139 * is non-operative
140 */
141
142static int wait_for_valid(u8 *base)
143{
Stefan Reinaueraca6ec62009-10-26 17:12:21 +0000144 u32 reg32;
145
146 /* Send the verb to the codec */
Stefan Reinauer9fe4d792010-01-16 17:53:38 +0000147 reg32 = read32(base + 0x68);
Stefan Reinaueraca6ec62009-10-26 17:12:21 +0000148 reg32 |= (1 << 0) | (1 << 1);
Stefan Reinauer9fe4d792010-01-16 17:53:38 +0000149 write32(base + 0x68, reg32);
Stefan Reinaueraca6ec62009-10-26 17:12:21 +0000150
Stefan Reinauer679c9f92009-01-20 22:54:59 +0000151 /* Use a 50 usec timeout - the Linux kernel uses the
152 * same duration */
153
154 int timeout = 50;
155 while(timeout--) {
Stefan Reinauer9fe4d792010-01-16 17:53:38 +0000156 reg32 = read32(base + HDA_ICII_REG);
Stefan Reinauera8e11682009-03-11 14:54:18 +0000157 if ((reg32 & (HDA_ICII_VALID | HDA_ICII_BUSY)) ==
Stefan Reinauer679c9f92009-01-20 22:54:59 +0000158 HDA_ICII_VALID)
159 return 0;
160 udelay(1);
161 }
162
Stefan Reinaueraca6ec62009-10-26 17:12:21 +0000163 return -1;
Stefan Reinauer679c9f92009-01-20 22:54:59 +0000164}
165
Stefan Reinauera8e11682009-03-11 14:54:18 +0000166static void codec_init(struct device *dev, u8 * base, int addr)
Stefan Reinauer679c9f92009-01-20 22:54:59 +0000167{
Stefan Reinauera8e11682009-03-11 14:54:18 +0000168 u32 reg32;
Stefan Reinauer679c9f92009-01-20 22:54:59 +0000169 u32 *verb;
170 u32 verb_size;
171 int i;
172
Stefan Reinaueraca6ec62009-10-26 17:12:21 +0000173 printk_debug("Azalia: Initializing codec #%d\n", addr);
174
Stefan Reinauer679c9f92009-01-20 22:54:59 +0000175 /* 1 */
176 if (wait_for_ready(base) == -1)
177 return;
178
Stefan Reinauera8e11682009-03-11 14:54:18 +0000179 reg32 = (addr << 28) | 0x000f0000;
Stefan Reinauer9fe4d792010-01-16 17:53:38 +0000180 write32(base + 0x60, reg32);
Stefan Reinauer679c9f92009-01-20 22:54:59 +0000181
182 if (wait_for_valid(base) == -1)
183 return;
184
Stefan Reinauer9fe4d792010-01-16 17:53:38 +0000185 reg32 = read32(base + 0x64);
Stefan Reinauer679c9f92009-01-20 22:54:59 +0000186
187 /* 2 */
Stefan Reinauera8e11682009-03-11 14:54:18 +0000188 printk_debug("Azalia: codec viddid: %08x\n", reg32);
189 verb_size = find_verb(dev, reg32, &verb);
Stefan Reinauer679c9f92009-01-20 22:54:59 +0000190
191 if (!verb_size) {
Stefan Reinauera8e11682009-03-11 14:54:18 +0000192 printk_debug("Azalia: No verb!\n");
Stefan Reinauer679c9f92009-01-20 22:54:59 +0000193 return;
194 }
Stefan Reinauera8e11682009-03-11 14:54:18 +0000195 printk_debug("Azalia: verb_size: %d\n", verb_size);
Stefan Reinaueraca6ec62009-10-26 17:12:21 +0000196
Stefan Reinauer679c9f92009-01-20 22:54:59 +0000197 /* 3 */
198 for (i = 0; i < verb_size; i++) {
199 if (wait_for_ready(base) == -1)
200 return;
201
Stefan Reinauer9fe4d792010-01-16 17:53:38 +0000202 write32(base + 0x60, verb[i]);
Stefan Reinauer679c9f92009-01-20 22:54:59 +0000203
204 if (wait_for_valid(base) == -1)
205 return;
206 }
Stefan Reinauera8e11682009-03-11 14:54:18 +0000207 printk_debug("Azalia: verb loaded.\n");
Stefan Reinauer679c9f92009-01-20 22:54:59 +0000208}
209
Stefan Reinauera8e11682009-03-11 14:54:18 +0000210static void codecs_init(struct device *dev, u8 * base, u32 codec_mask)
Stefan Reinauer679c9f92009-01-20 22:54:59 +0000211{
212 int i;
213 for (i = 2; i >= 0; i--) {
214 if (codec_mask & (1 << i))
Stefan Reinauera8e11682009-03-11 14:54:18 +0000215 codec_init(dev, base, i);
Stefan Reinauer679c9f92009-01-20 22:54:59 +0000216 }
217}
218
219static void azalia_init(struct device *dev)
220{
221 u8 *base;
222 struct resource *res;
223 u32 codec_mask;
Stefan Reinauera8e11682009-03-11 14:54:18 +0000224 u8 reg8;
225 u32 reg32;
Stefan Reinauer679c9f92009-01-20 22:54:59 +0000226
Stefan Reinauer08670622009-06-30 15:17:49 +0000227#if CONFIG_MMCONF_SUPPORT
Stefan Reinauera8e11682009-03-11 14:54:18 +0000228 // ESD
229 reg32 = pci_mmio_read_config32(dev, 0x134);
230 reg32 &= 0xff00ffff;
231 reg32 |= (2 << 16);
232 pci_mmio_write_config32(dev, 0x134, reg32);
233
234 // Link1 description
235 reg32 = pci_mmio_read_config32(dev, 0x140);
236 reg32 &= 0xff00ffff;
237 reg32 |= (2 << 16);
238 pci_mmio_write_config32(dev, 0x140, reg32);
239
240 // Port VC0 Resource Control Register
241 reg32 = pci_mmio_read_config32(dev, 0x114);
242 reg32 &= 0xffffff00;
243 reg32 |= 1;
244 pci_mmio_write_config32(dev, 0x114, reg32);
245
246 // VCi traffic class
247 reg8 = pci_mmio_read_config8(dev, 0x44);
248 reg8 |= (7 << 0); // TC7
249 pci_mmio_write_config8(dev, 0x44, reg8);
250
251 // VCi Resource Control
252 reg32 = pci_mmio_read_config32(dev, 0x120);
Stefan Reinauer109ab312009-08-12 16:08:05 +0000253 reg32 |= (1 << 31);
Stefan Reinauera8e11682009-03-11 14:54:18 +0000254 reg32 |= (1 << 24); // VCi ID
255 reg32 |= (0x80 << 0); // VCi map
256 pci_mmio_write_config32(dev, 0x120, reg32);
257#else
Stefan Reinauer08670622009-06-30 15:17:49 +0000258#error ICH7 Azalia required CONFIG_MMCONF_SUPPORT
Stefan Reinauera8e11682009-03-11 14:54:18 +0000259#endif
260
261 /* Set Bus Master */
262 reg32 = pci_read_config32(dev, PCI_COMMAND);
263 pci_write_config32(dev, PCI_COMMAND, reg32 | PCI_COMMAND_MASTER);
264
265 pci_write_config8(dev, 0x3c, 0x0a); // unused?
266
267 // TODO Actually check if we're AC97 or HDA instead of hardcoding this
Stefan Reinauer38f147e2010-02-08 12:20:50 +0000268 // here, in devicetree.cb and/or romstage.c.
Stefan Reinauera8e11682009-03-11 14:54:18 +0000269 reg8 = pci_read_config8(dev, 0x40);
270 reg8 |= (1 << 3); // Clear Clock Detect Bit
271 pci_write_config8(dev, 0x40, reg8);
272 reg8 &= ~(1 << 3); // Keep CLKDETCLR from clearing the bit over and over
273 pci_write_config8(dev, 0x40, reg8);
274 reg8 |= (1 << 2); // Enable clock detection
275 pci_write_config8(dev, 0x40, reg8);
276 mdelay(1);
277 reg8 = pci_read_config8(dev, 0x40);
278 printk_debug("Azalia: codec type: %s\n", (reg8 & (1 << 1))?"Azalia":"AC97");
279
280 //
281 reg8 = pci_read_config8(dev, 0x40); // Audio Control
Stefan Reinauer38f147e2010-02-08 12:20:50 +0000282 reg8 |= 1; // Select Azalia mode. This needs to be controlled via devicetree.cb
Stefan Reinauera8e11682009-03-11 14:54:18 +0000283 pci_write_config8(dev, 0x40, reg8);
284
285 reg8 = pci_read_config8(dev, 0x4d); // Docking Status
286 reg8 &= ~(1 << 7); // Docking not supported
287 pci_write_config8(dev, 0x4d, reg8);
288#if 0
Stefan Reinauer679c9f92009-01-20 22:54:59 +0000289 /* Set routing pin */
290 pci_write_config32(dev, 0xf8, 0x0);
291 pci_write_config8(dev, 0xfc, 0xAA);
292
293 /* Set INTA */
294 pci_write_config8(dev, 0x63, 0x0);
295
296 /* Enable azalia, disable ac97 */
297 // pm_iowrite(0x59, 0xB);
Stefan Reinauera8e11682009-03-11 14:54:18 +0000298#endif
Stefan Reinauer679c9f92009-01-20 22:54:59 +0000299
300 res = find_resource(dev, 0x10);
301 if (!res)
302 return;
303
Stefan Reinauera8e11682009-03-11 14:54:18 +0000304 // NOTE this will break as soon as the Azalia get's a bar above
305 // 4G. Is there anything we can do about it?
Stefan Reinauer679c9f92009-01-20 22:54:59 +0000306 base = (u8 *) ((u32)res->base);
Stefan Reinauera8e11682009-03-11 14:54:18 +0000307 printk_debug("Azalia: base = %08x\n", (u32)base);
Stefan Reinauer679c9f92009-01-20 22:54:59 +0000308 codec_mask = codec_detect(base);
309
310 if (codec_mask) {
Stefan Reinauera8e11682009-03-11 14:54:18 +0000311 printk_debug("Azalia: codec_mask = %02x\n", codec_mask);
312 codecs_init(dev, base, codec_mask);
Stefan Reinauer679c9f92009-01-20 22:54:59 +0000313 }
314}
315
Stefan Reinauera8e11682009-03-11 14:54:18 +0000316static void azalia_set_subsystem(device_t dev, unsigned vendor, unsigned device)
317{
318 if (!vendor || !device) {
319 pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID,
320 pci_read_config32(dev, PCI_VENDOR_ID));
321 } else {
322 pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID,
323 ((device & 0xffff) << 16) | (vendor & 0xffff));
324 }
325}
326
327static struct pci_operations azalia_pci_ops = {
328 .set_subsystem = azalia_set_subsystem,
329};
330
Stefan Reinauer679c9f92009-01-20 22:54:59 +0000331static struct device_operations azalia_ops = {
332 .read_resources = pci_dev_read_resources,
333 .set_resources = pci_dev_set_resources,
334 .enable_resources = pci_dev_enable_resources,
335 .init = azalia_init,
336 .scan_bus = 0,
337 .enable = i82801gx_enable,
Stefan Reinauera8e11682009-03-11 14:54:18 +0000338 .ops_pci = &azalia_pci_ops,
Stefan Reinauer679c9f92009-01-20 22:54:59 +0000339};
340
341/* 82801GB/GR/GDH/GBM/GHM (ICH7/ICH7R/ICH7DH/ICH7-M/ICH7-M DH) */
342static const struct pci_driver i82801gx_azalia __pci_driver = {
343 .ops = &azalia_ops,
344 .vendor = PCI_VENDOR_ID_INTEL,
345 .device = 0x27d8,
346};
347