blob: b4cee4633c6457005bc4bf07acbe83b5da2e122b [file] [log] [blame]
Patrick Georgie72a8a32012-11-06 11:05:09 +01001/*
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 * Copyright (C) 2012 secunet Security Networks AG
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; version 2 of the License.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
Patrick Georgie72a8a32012-11-06 11:05:09 +010016 */
17
18#include <console/console.h>
19#include <device/device.h>
20#include <device/pci.h>
21#include <device/pci_ids.h>
22#include <device/pci_ops.h>
23#include <arch/io.h>
24#include <delay.h>
Vladimir Serbinenko75c83872014-09-05 01:01:31 +020025#include <device/azalia_device.h>
Patrick Georgie72a8a32012-11-06 11:05:09 +010026#include "i82801ix.h"
27
28#define HDA_ICII_REG 0x68
Andrew Wuae8d0692013-08-02 19:29:17 +080029#define HDA_ICII_BUSY (1 << 0)
30#define HDA_ICII_VALID (1 << 1)
Patrick Georgie72a8a32012-11-06 11:05:09 +010031
32typedef struct southbridge_intel_i82801ix_config config_t;
33
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080034static int set_bits(void *port, u32 mask, u32 val)
Patrick Georgie72a8a32012-11-06 11:05:09 +010035{
36 u32 reg32;
37 int count;
38
39 /* Write (val & mask) to port */
40 val &= mask;
41 reg32 = read32(port);
42 reg32 &= ~mask;
43 reg32 |= val;
44 write32(port, reg32);
45
46 /* Wait for readback of register to
47 * match what was just written to it
48 */
49 count = 50;
50 do {
51 /* Wait 1ms based on BKDG wait time */
52 mdelay(1);
53 reg32 = read32(port);
54 reg32 &= mask;
55 } while ((reg32 != val) && --count);
56
57 /* Timeout occurred */
58 if (!count)
59 return -1;
60 return 0;
61}
62
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080063static int codec_detect(u8 *base)
Patrick Georgie72a8a32012-11-06 11:05:09 +010064{
65 u32 reg32;
66
67 /* Set Bit0 to 0 to enter reset state (BAR + 0x8)[0] */
68 if (set_bits(base + 0x08, 1, 0) == -1)
69 goto no_codec;
70
71 /* Set Bit 0 to 1 to exit reset state (BAR + 0x8)[0] */
72 if (set_bits(base + 0x08, 1, 1) == -1)
73 goto no_codec;
74
75 /* Read in Codec location (BAR + 0xe)[2..0]*/
76 reg32 = read32(base + 0xe);
77 reg32 &= 0x0f;
78 if (!reg32)
79 goto no_codec;
80
81 return reg32;
82
83no_codec:
84 /* Codec Not found */
85 /* Put HDA back in reset (BAR + 0x8) [0] */
86 set_bits(base + 0x08, 1, 0);
87 printk(BIOS_DEBUG, "Azalia: No codec!\n");
88 return 0;
89}
90
Patrick Georgie72a8a32012-11-06 11:05:09 +010091static u32 find_verb(struct device *dev, u32 viddid, const u32 ** verb)
92{
93 int idx=0;
94
95 while (idx < (cim_verb_data_size / sizeof(u32))) {
96 u32 verb_size = 4 * cim_verb_data[idx+2]; // in u32
97 if (cim_verb_data[idx] != viddid) {
98 idx += verb_size + 3; // skip verb + header
99 continue;
100 }
101 *verb = &cim_verb_data[idx+3];
102 return verb_size;
103 }
104
105 /* Not all codecs need to load another verb */
106 return 0;
107}
108
109/**
110 * Wait 50usec for the codec to indicate it is ready
111 * no response would imply that the codec is non-operative
112 */
113
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800114static int wait_for_ready(u8 *base)
Patrick Georgie72a8a32012-11-06 11:05:09 +0100115{
116 /* Use a 50 usec timeout - the Linux kernel uses the
117 * same duration */
118
119 int timeout = 50;
120
Elyes HAOUASba28e8d2016-08-31 19:22:16 +0200121 while (timeout--) {
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800122 u32 reg32 = read32(base + HDA_ICII_REG);
Patrick Georgie72a8a32012-11-06 11:05:09 +0100123 if (!(reg32 & HDA_ICII_BUSY))
124 return 0;
125 udelay(1);
126 }
127
128 return -1;
129}
130
131/**
132 * Wait 50usec for the codec to indicate that it accepted
133 * the previous command. No response would imply that the code
134 * is non-operative
135 */
136
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800137static int wait_for_valid(u8 *base)
Patrick Georgie72a8a32012-11-06 11:05:09 +0100138{
139 u32 reg32;
140
141 /* Send the verb to the codec */
142 reg32 = read32(base + 0x68);
143 reg32 |= (1 << 0) | (1 << 1);
144 write32(base + 0x68, reg32);
145
146 /* Use a 50 usec timeout - the Linux kernel uses the
147 * same duration */
148
149 int timeout = 50;
Elyes HAOUASba28e8d2016-08-31 19:22:16 +0200150 while (timeout--) {
Patrick Georgie72a8a32012-11-06 11:05:09 +0100151 reg32 = read32(base + HDA_ICII_REG);
152 if ((reg32 & (HDA_ICII_VALID | HDA_ICII_BUSY)) ==
153 HDA_ICII_VALID)
154 return 0;
155 udelay(1);
156 }
157
158 return -1;
159}
160
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800161static void codec_init(struct device *dev, u8 *base, int addr)
Patrick Georgie72a8a32012-11-06 11:05:09 +0100162{
163 u32 reg32;
164 const u32 *verb;
165 u32 verb_size;
166 int i;
167
168 printk(BIOS_DEBUG, "HD Audio: Initializing codec #%d\n", addr);
169
170 /* 1 */
171 if (wait_for_ready(base) == -1)
172 return;
173
174 reg32 = (addr << 28) | 0x000f0000;
175 write32(base + 0x60, reg32);
176
177 if (wait_for_valid(base) == -1)
178 return;
179
180 reg32 = read32(base + 0x64);
181
182 /* 2 */
183 printk(BIOS_DEBUG, "Azalia: codec viddid: %08x\n", reg32);
184 verb_size = find_verb(dev, reg32, &verb);
185
186 if (!verb_size) {
187 printk(BIOS_DEBUG, "Azalia: No verb!\n");
188 return;
189 }
190 printk(BIOS_DEBUG, "Azalia: verb_size: %d\n", verb_size);
191
192 /* 3 */
193 for (i = 0; i < verb_size; i++) {
194 if (wait_for_ready(base) == -1)
195 return;
196
197 write32(base + 0x60, verb[i]);
198
199 if (wait_for_valid(base) == -1)
200 return;
201 }
202 printk(BIOS_DEBUG, "Azalia: verb loaded.\n");
203}
204
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800205static void codecs_init(struct device *dev, u8 *base, u32 codec_mask)
Patrick Georgie72a8a32012-11-06 11:05:09 +0100206{
207 int i;
208 for (i = 2; i >= 0; i--) {
209 if (codec_mask & (1 << i))
210 codec_init(dev, base, i);
211 }
212
213 for (i = 0; i < pc_beep_verbs_size; i++) {
214 if (wait_for_ready(base) == -1)
215 return;
216
217 write32(base + 0x60, pc_beep_verbs[i]);
218
219 if (wait_for_valid(base) == -1)
220 return;
221 }
222}
223
224static void azalia_init(struct device *dev)
225{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800226 u8 *base;
Patrick Georgie72a8a32012-11-06 11:05:09 +0100227 struct resource *res;
228 u32 codec_mask;
229 u8 reg8;
230 u32 reg32;
231
Patrick Georgie72a8a32012-11-06 11:05:09 +0100232 // ESD
Kyösti Mälkki9b143e12013-07-26 08:35:09 +0300233 reg32 = pci_read_config32(dev, 0x134);
Patrick Georgie72a8a32012-11-06 11:05:09 +0100234 reg32 &= 0xff00ffff;
235 reg32 |= (2 << 16);
Kyösti Mälkki9b143e12013-07-26 08:35:09 +0300236 pci_write_config32(dev, 0x134, reg32);
Patrick Georgie72a8a32012-11-06 11:05:09 +0100237
238 // Link1 description
Kyösti Mälkki9b143e12013-07-26 08:35:09 +0300239 reg32 = pci_read_config32(dev, 0x140);
Patrick Georgie72a8a32012-11-06 11:05:09 +0100240 reg32 &= 0xff00ffff;
241 reg32 |= (2 << 16);
Kyösti Mälkki9b143e12013-07-26 08:35:09 +0300242 pci_write_config32(dev, 0x140, reg32);
Patrick Georgie72a8a32012-11-06 11:05:09 +0100243
244 // Port VC0 Resource Control Register
Kyösti Mälkki9b143e12013-07-26 08:35:09 +0300245 reg32 = pci_read_config32(dev, 0x114);
Patrick Georgie72a8a32012-11-06 11:05:09 +0100246 reg32 &= 0xffffff00;
247 reg32 |= 1;
Kyösti Mälkki9b143e12013-07-26 08:35:09 +0300248 pci_write_config32(dev, 0x114, reg32);
Patrick Georgie72a8a32012-11-06 11:05:09 +0100249
250 // VCi traffic class
Kyösti Mälkki9b143e12013-07-26 08:35:09 +0300251 reg8 = pci_read_config8(dev, 0x44);
Patrick Georgie72a8a32012-11-06 11:05:09 +0100252 reg8 |= (7 << 0); // TC7
Kyösti Mälkki9b143e12013-07-26 08:35:09 +0300253 pci_write_config8(dev, 0x44, reg8);
Patrick Georgie72a8a32012-11-06 11:05:09 +0100254
255 // VCi Resource Control
Kyösti Mälkki9b143e12013-07-26 08:35:09 +0300256 reg32 = pci_read_config32(dev, 0x120);
Patrick Georgie72a8a32012-11-06 11:05:09 +0100257 reg32 |= (1 << 31);
258 reg32 |= (1 << 24); // VCi ID
259 reg32 |= (0x80 << 0); // VCi map
Kyösti Mälkki9b143e12013-07-26 08:35:09 +0300260 pci_write_config32(dev, 0x120, reg32);
Patrick Georgie72a8a32012-11-06 11:05:09 +0100261
262 /* Set Bus Master */
263 reg32 = pci_read_config32(dev, PCI_COMMAND);
264 pci_write_config32(dev, PCI_COMMAND, reg32 | PCI_COMMAND_MASTER);
265
266 reg8 = pci_read_config8(dev, 0x4d); // Docking Status
267 reg8 &= ~(1 << 7); // Docking not supported
268 pci_write_config8(dev, 0x4d, reg8);
269
270 /* Lock some R/WO bits by writing their current value. */
271 reg32 = pci_read_config32(dev, 0x74);
272 pci_write_config32(dev, 0x74, reg32);
273
274 res = find_resource(dev, 0x10);
275 if (!res)
276 return;
277
278 // NOTE this will break as soon as the Azalia get's a bar above
279 // 4G. Is there anything we can do about it?
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800280 base = res2mmio(res, 0, 0);
Patrick Rudolph4af2add2018-11-26 15:56:11 +0100281 printk(BIOS_DEBUG, "Azalia: base = %p\n", base);
Patrick Georgie72a8a32012-11-06 11:05:09 +0100282 codec_mask = codec_detect(base);
283
284 if (codec_mask) {
285 printk(BIOS_DEBUG, "Azalia: codec_mask = %02x\n", codec_mask);
286 codecs_init(dev, base, codec_mask);
287 }
288}
289
Elyes HAOUAS8aa50732018-05-13 13:34:58 +0200290static void azalia_set_subsystem(struct device *dev, unsigned vendor,
291 unsigned device)
Patrick Georgie72a8a32012-11-06 11:05:09 +0100292{
293 if (!vendor || !device) {
294 pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID,
295 pci_read_config32(dev, PCI_VENDOR_ID));
296 } else {
297 pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID,
298 ((device & 0xffff) << 16) | (vendor & 0xffff));
299 }
300}
301
302static struct pci_operations azalia_pci_ops = {
303 .set_subsystem = azalia_set_subsystem,
304};
305
306static struct device_operations azalia_ops = {
307 .read_resources = pci_dev_read_resources,
308 .set_resources = pci_dev_set_resources,
309 .enable_resources = pci_dev_enable_resources,
310 .init = azalia_init,
311 .scan_bus = 0,
312 .ops_pci = &azalia_pci_ops,
313};
314
315/* ICH9DH/ICH9DO/ICH9R/ICH9/ICH9M-E/ICH9M */
316static const struct pci_driver i82801ix_azalia __pci_driver = {
317 .ops = &azalia_ops,
318 .vendor = PCI_VENDOR_ID_INTEL,
319 .device = 0x293e,
320};