blob: 1435866100593740bcdaed0ef8d895bc0d26be18 [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 Reinauera8e11682009-03-11 14:54:18 +000043 reg32 = readl(port);
44 reg32 &= ~mask;
45 reg32 |= val;
46 writel(reg32, port);
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);
55 reg32 = readl(port);
56 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]*/
78 reg32 = readl(base + 0xe);
79 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
93static u32 cim_verb_data[] = {
94 0x00172000,
95 0x00172100,
96 0x001722EC,
97 0x00172310,
98
99 /* Pin Complex (NID 0x12) */
100 0x01271CF0,
101 0x01271D11,
102 0x01271E11,
103 0x01271F41,
104 /* Pin Complex (NID 0x14) */
105 0x01471C10,
106 0x01471D01,
107 0x01471E13,
108 0x01471F99,
109 /* Pin Complex (NID 0x15) */
110 0x01571C20,
111 0x01571D40,
112 0x01571E21,
113 0x01571F01,
114 /* Pin Complex (NID 0x16) */
115 0x01671CF0,
116 0x01671D11,
117 0x01671E11,
118 0x01671F41,
119 /* Pin Complex (NID 0x18) */
120 0x01871C30,
121 0x01871D98,
122 0x01871EA1,
123 0x01871F01,
124 /* Pin Complex (NID 0x19) */
125 0x01971C31,
126 0x01971D09,
127 0x01971EA3,
128 0x01971F99,
129 /* Pin Complex (NID 0x1A) */
130 0x01A71C3F,
131 0x01A71D98,
132 0x01A71EA1,
133 0x01A71F02,
134 /* Pin Complex (NID 0x1B) */
135 0x01B71C1F,
136 0x01B71D40,
137 0x01B71E21,
138 0x01B71F02,
139 /* Pin Complex (NID 0x1C) */
140 0x01C71CF0,
141 0x01C71D11,
142 0x01C71E11,
143 0x01C71F41,
144 /* Pin Complex (NID 0x1D) */
145 0x01D71CF0,
146 0x01D71D11,
147 0x01D71E11,
148 0x01D71F41,
149 /* Pin Complex (NID 0x1E) */
150 0x01E71CF0,
151 0x01E71D11,
152 0x01E71E11,
153 0x01E71F41,
154 /* Pin Complex (NID 0x1F) */
155 0x01F71CF0,
156 0x01F71D11,
157 0x01F71E11,
158 0x01F71F41,
159};
160
Stefan Reinauera8e11682009-03-11 14:54:18 +0000161static unsigned find_verb(struct device *dev, u32 viddid, u32 ** verb)
Stefan Reinauer679c9f92009-01-20 22:54:59 +0000162{
Stefan Reinauera8e11682009-03-11 14:54:18 +0000163 config_t *config = dev->chip_info;
Stefan Reinauer109ab312009-08-12 16:08:05 +0000164
Stefan Reinauera8e11682009-03-11 14:54:18 +0000165 if (config == NULL) {
166 printk_err("\ni82801gx_azalia: Not mentioned in mainboard's Config.lb!\n");
Stefan Reinauer679c9f92009-01-20 22:54:59 +0000167 return 0;
Stefan Reinauera8e11682009-03-11 14:54:18 +0000168 }
169
170 printk_debug("Azalia: dev=%s\n", dev_path(dev));
171 printk_debug("Azalia: Default viddid=%x\n", (u32)config->hda_viddid);
172 printk_debug("Azalia: Reading viddid=%x\n", viddid);
173
174 if (viddid != config->hda_viddid)
Stefan Reinauer679c9f92009-01-20 22:54:59 +0000175 return 0;
Stefan Reinauera8e11682009-03-11 14:54:18 +0000176
Stefan Reinauer679c9f92009-01-20 22:54:59 +0000177 *verb = (u32 *) cim_verb_data;
Stefan Reinauera8e11682009-03-11 14:54:18 +0000178
Stefan Reinauer679c9f92009-01-20 22:54:59 +0000179 return sizeof(cim_verb_data) / sizeof(u32);
180}
181
182/**
183 * Wait 50usec for for the codec to indicate it is ready
184 * no response would imply that the codec is non-operative
185 */
186
187static int wait_for_ready(u8 *base)
188{
189 /* Use a 50 usec timeout - the Linux kernel uses the
190 * same duration */
191
192 int timeout = 50;
193
194 while(timeout--) {
Stefan Reinauera8e11682009-03-11 14:54:18 +0000195 u32 reg32 = readl(base + HDA_ICII_REG);
196 if (!(reg32 & HDA_ICII_BUSY))
Stefan Reinauer679c9f92009-01-20 22:54:59 +0000197 return 0;
198 udelay(1);
199 }
200
201 return -1;
202}
203
204/**
205 * Wait 50usec for for the codec to indicate that it accepted
206 * the previous command. No response would imply that the code
207 * is non-operative
208 */
209
210static int wait_for_valid(u8 *base)
211{
212 /* Use a 50 usec timeout - the Linux kernel uses the
213 * same duration */
214
215 int timeout = 50;
216 while(timeout--) {
Stefan Reinauera8e11682009-03-11 14:54:18 +0000217 u32 reg32 = readl(base + HDA_ICII_REG);
218 if ((reg32 & (HDA_ICII_VALID | HDA_ICII_BUSY)) ==
Stefan Reinauer679c9f92009-01-20 22:54:59 +0000219 HDA_ICII_VALID)
220 return 0;
221 udelay(1);
222 }
223
224 return 1;
225}
226
Stefan Reinauera8e11682009-03-11 14:54:18 +0000227static void codec_init(struct device *dev, u8 * base, int addr)
Stefan Reinauer679c9f92009-01-20 22:54:59 +0000228{
Stefan Reinauera8e11682009-03-11 14:54:18 +0000229 u32 reg32;
Stefan Reinauer679c9f92009-01-20 22:54:59 +0000230 u32 *verb;
231 u32 verb_size;
232 int i;
233
234 /* 1 */
235 if (wait_for_ready(base) == -1)
236 return;
237
Stefan Reinauera8e11682009-03-11 14:54:18 +0000238 reg32 = (addr << 28) | 0x000f0000;
239 writel(reg32, base + 0x60);
Stefan Reinauer679c9f92009-01-20 22:54:59 +0000240
241 if (wait_for_valid(base) == -1)
242 return;
243
Stefan Reinauera8e11682009-03-11 14:54:18 +0000244 reg32 = readl(base + 0x64);
Stefan Reinauer679c9f92009-01-20 22:54:59 +0000245
246 /* 2 */
Stefan Reinauera8e11682009-03-11 14:54:18 +0000247 printk_debug("Azalia: codec viddid: %08x\n", reg32);
248 verb_size = find_verb(dev, reg32, &verb);
Stefan Reinauer679c9f92009-01-20 22:54:59 +0000249
250 if (!verb_size) {
Stefan Reinauera8e11682009-03-11 14:54:18 +0000251 printk_debug("Azalia: No verb!\n");
Stefan Reinauer679c9f92009-01-20 22:54:59 +0000252 return;
253 }
254
Stefan Reinauera8e11682009-03-11 14:54:18 +0000255 printk_debug("Azalia: verb_size: %d\n", verb_size);
Stefan Reinauer679c9f92009-01-20 22:54:59 +0000256 /* 3 */
257 for (i = 0; i < verb_size; i++) {
258 if (wait_for_ready(base) == -1)
259 return;
260
261 writel(verb[i], base + 0x60);
262
263 if (wait_for_valid(base) == -1)
264 return;
265 }
Stefan Reinauera8e11682009-03-11 14:54:18 +0000266 printk_debug("Azalia: verb loaded.\n");
Stefan Reinauer679c9f92009-01-20 22:54:59 +0000267}
268
Stefan Reinauera8e11682009-03-11 14:54:18 +0000269static void codecs_init(struct device *dev, u8 * base, u32 codec_mask)
Stefan Reinauer679c9f92009-01-20 22:54:59 +0000270{
271 int i;
272 for (i = 2; i >= 0; i--) {
273 if (codec_mask & (1 << i))
Stefan Reinauera8e11682009-03-11 14:54:18 +0000274 codec_init(dev, base, i);
Stefan Reinauer679c9f92009-01-20 22:54:59 +0000275 }
276}
277
278static void azalia_init(struct device *dev)
279{
280 u8 *base;
281 struct resource *res;
282 u32 codec_mask;
Stefan Reinauera8e11682009-03-11 14:54:18 +0000283 u8 reg8;
284 u32 reg32;
Stefan Reinauer679c9f92009-01-20 22:54:59 +0000285
Stefan Reinauer08670622009-06-30 15:17:49 +0000286#if CONFIG_MMCONF_SUPPORT
Stefan Reinauera8e11682009-03-11 14:54:18 +0000287 // ESD
288 reg32 = pci_mmio_read_config32(dev, 0x134);
289 reg32 &= 0xff00ffff;
290 reg32 |= (2 << 16);
291 pci_mmio_write_config32(dev, 0x134, reg32);
292
293 // Link1 description
294 reg32 = pci_mmio_read_config32(dev, 0x140);
295 reg32 &= 0xff00ffff;
296 reg32 |= (2 << 16);
297 pci_mmio_write_config32(dev, 0x140, reg32);
298
299 // Port VC0 Resource Control Register
300 reg32 = pci_mmio_read_config32(dev, 0x114);
301 reg32 &= 0xffffff00;
302 reg32 |= 1;
303 pci_mmio_write_config32(dev, 0x114, reg32);
304
305 // VCi traffic class
306 reg8 = pci_mmio_read_config8(dev, 0x44);
307 reg8 |= (7 << 0); // TC7
308 pci_mmio_write_config8(dev, 0x44, reg8);
309
310 // VCi Resource Control
311 reg32 = pci_mmio_read_config32(dev, 0x120);
Stefan Reinauer109ab312009-08-12 16:08:05 +0000312 reg32 |= (1 << 31);
Stefan Reinauera8e11682009-03-11 14:54:18 +0000313 reg32 |= (1 << 24); // VCi ID
314 reg32 |= (0x80 << 0); // VCi map
315 pci_mmio_write_config32(dev, 0x120, reg32);
316#else
Stefan Reinauer08670622009-06-30 15:17:49 +0000317#error ICH7 Azalia required CONFIG_MMCONF_SUPPORT
Stefan Reinauera8e11682009-03-11 14:54:18 +0000318#endif
319
320 /* Set Bus Master */
321 reg32 = pci_read_config32(dev, PCI_COMMAND);
322 pci_write_config32(dev, PCI_COMMAND, reg32 | PCI_COMMAND_MASTER);
323
324 pci_write_config8(dev, 0x3c, 0x0a); // unused?
325
326 // TODO Actually check if we're AC97 or HDA instead of hardcoding this
327 // here, in Config.lb and/or auto.c.
328 reg8 = pci_read_config8(dev, 0x40);
329 reg8 |= (1 << 3); // Clear Clock Detect Bit
330 pci_write_config8(dev, 0x40, reg8);
331 reg8 &= ~(1 << 3); // Keep CLKDETCLR from clearing the bit over and over
332 pci_write_config8(dev, 0x40, reg8);
333 reg8 |= (1 << 2); // Enable clock detection
334 pci_write_config8(dev, 0x40, reg8);
335 mdelay(1);
336 reg8 = pci_read_config8(dev, 0x40);
337 printk_debug("Azalia: codec type: %s\n", (reg8 & (1 << 1))?"Azalia":"AC97");
338
339 //
340 reg8 = pci_read_config8(dev, 0x40); // Audio Control
341 reg8 |= 1; // Select Azalia mode. This needs to be controlled via Config.lb
342 pci_write_config8(dev, 0x40, reg8);
343
344 reg8 = pci_read_config8(dev, 0x4d); // Docking Status
345 reg8 &= ~(1 << 7); // Docking not supported
346 pci_write_config8(dev, 0x4d, reg8);
347#if 0
Stefan Reinauer679c9f92009-01-20 22:54:59 +0000348 /* Set routing pin */
349 pci_write_config32(dev, 0xf8, 0x0);
350 pci_write_config8(dev, 0xfc, 0xAA);
351
352 /* Set INTA */
353 pci_write_config8(dev, 0x63, 0x0);
354
355 /* Enable azalia, disable ac97 */
356 // pm_iowrite(0x59, 0xB);
Stefan Reinauera8e11682009-03-11 14:54:18 +0000357#endif
Stefan Reinauer679c9f92009-01-20 22:54:59 +0000358
359 res = find_resource(dev, 0x10);
360 if (!res)
361 return;
362
Stefan Reinauera8e11682009-03-11 14:54:18 +0000363 // NOTE this will break as soon as the Azalia get's a bar above
364 // 4G. Is there anything we can do about it?
Stefan Reinauer679c9f92009-01-20 22:54:59 +0000365 base = (u8 *) ((u32)res->base);
Stefan Reinauera8e11682009-03-11 14:54:18 +0000366 printk_debug("Azalia: base = %08x\n", (u32)base);
Stefan Reinauer679c9f92009-01-20 22:54:59 +0000367 codec_mask = codec_detect(base);
368
369 if (codec_mask) {
Stefan Reinauera8e11682009-03-11 14:54:18 +0000370 printk_debug("Azalia: codec_mask = %02x\n", codec_mask);
371 codecs_init(dev, base, codec_mask);
Stefan Reinauer679c9f92009-01-20 22:54:59 +0000372 }
373}
374
Stefan Reinauera8e11682009-03-11 14:54:18 +0000375static void azalia_set_subsystem(device_t dev, unsigned vendor, unsigned device)
376{
377 if (!vendor || !device) {
378 pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID,
379 pci_read_config32(dev, PCI_VENDOR_ID));
380 } else {
381 pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID,
382 ((device & 0xffff) << 16) | (vendor & 0xffff));
383 }
384}
385
386static struct pci_operations azalia_pci_ops = {
387 .set_subsystem = azalia_set_subsystem,
388};
389
Stefan Reinauer679c9f92009-01-20 22:54:59 +0000390static struct device_operations azalia_ops = {
391 .read_resources = pci_dev_read_resources,
392 .set_resources = pci_dev_set_resources,
393 .enable_resources = pci_dev_enable_resources,
394 .init = azalia_init,
395 .scan_bus = 0,
396 .enable = i82801gx_enable,
Stefan Reinauera8e11682009-03-11 14:54:18 +0000397 .ops_pci = &azalia_pci_ops,
Stefan Reinauer679c9f92009-01-20 22:54:59 +0000398};
399
400/* 82801GB/GR/GDH/GBM/GHM (ICH7/ICH7R/ICH7DH/ICH7-M/ICH7-M DH) */
401static const struct pci_driver i82801gx_azalia __pci_driver = {
402 .ops = &azalia_ops,
403 .vendor = PCI_VENDOR_ID_INTEL,
404 .device = 0x27d8,
405};
406