blob: dd817b9b08d359e673159fa297c01b8641cde5cd [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.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
Paul Menzela46a7122013-02-23 18:37:27 +010019 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Patrick Georgie72a8a32012-11-06 11:05:09 +010020 */
21
22#include <console/console.h>
23#include <device/device.h>
24#include <device/pci.h>
25#include <device/pci_ids.h>
26#include <device/pci_ops.h>
27#include <arch/io.h>
28#include <delay.h>
Vladimir Serbinenko75c83872014-09-05 01:01:31 +020029#include <device/azalia_device.h>
Patrick Georgie72a8a32012-11-06 11:05:09 +010030#include "i82801ix.h"
31
32#define HDA_ICII_REG 0x68
Andrew Wuae8d0692013-08-02 19:29:17 +080033#define HDA_ICII_BUSY (1 << 0)
34#define HDA_ICII_VALID (1 << 1)
Patrick Georgie72a8a32012-11-06 11:05:09 +010035
36typedef struct southbridge_intel_i82801ix_config config_t;
37
38static int set_bits(u32 port, u32 mask, u32 val)
39{
40 u32 reg32;
41 int count;
42
43 /* Write (val & mask) to port */
44 val &= mask;
45 reg32 = read32(port);
46 reg32 &= ~mask;
47 reg32 |= val;
48 write32(port, reg32);
49
50 /* Wait for readback of register to
51 * match what was just written to it
52 */
53 count = 50;
54 do {
55 /* Wait 1ms based on BKDG wait time */
56 mdelay(1);
57 reg32 = read32(port);
58 reg32 &= mask;
59 } while ((reg32 != val) && --count);
60
61 /* Timeout occurred */
62 if (!count)
63 return -1;
64 return 0;
65}
66
67static int codec_detect(u32 base)
68{
69 u32 reg32;
70
71 /* Set Bit0 to 0 to enter reset state (BAR + 0x8)[0] */
72 if (set_bits(base + 0x08, 1, 0) == -1)
73 goto no_codec;
74
75 /* Set Bit 0 to 1 to exit reset state (BAR + 0x8)[0] */
76 if (set_bits(base + 0x08, 1, 1) == -1)
77 goto no_codec;
78
79 /* Read in Codec location (BAR + 0xe)[2..0]*/
80 reg32 = read32(base + 0xe);
81 reg32 &= 0x0f;
82 if (!reg32)
83 goto no_codec;
84
85 return reg32;
86
87no_codec:
88 /* Codec Not found */
89 /* Put HDA back in reset (BAR + 0x8) [0] */
90 set_bits(base + 0x08, 1, 0);
91 printk(BIOS_DEBUG, "Azalia: No codec!\n");
92 return 0;
93}
94
Patrick Georgie72a8a32012-11-06 11:05:09 +010095static u32 find_verb(struct device *dev, u32 viddid, const u32 ** verb)
96{
97 int idx=0;
98
99 while (idx < (cim_verb_data_size / sizeof(u32))) {
100 u32 verb_size = 4 * cim_verb_data[idx+2]; // in u32
101 if (cim_verb_data[idx] != viddid) {
102 idx += verb_size + 3; // skip verb + header
103 continue;
104 }
105 *verb = &cim_verb_data[idx+3];
106 return verb_size;
107 }
108
109 /* Not all codecs need to load another verb */
110 return 0;
111}
112
113/**
114 * Wait 50usec for the codec to indicate it is ready
115 * no response would imply that the codec is non-operative
116 */
117
118static int wait_for_ready(u32 base)
119{
120 /* Use a 50 usec timeout - the Linux kernel uses the
121 * same duration */
122
123 int timeout = 50;
124
125 while(timeout--) {
126 u32 reg32 = read32(base + HDA_ICII_REG);
127 if (!(reg32 & HDA_ICII_BUSY))
128 return 0;
129 udelay(1);
130 }
131
132 return -1;
133}
134
135/**
136 * Wait 50usec for the codec to indicate that it accepted
137 * the previous command. No response would imply that the code
138 * is non-operative
139 */
140
141static int wait_for_valid(u32 base)
142{
143 u32 reg32;
144
145 /* Send the verb to the codec */
146 reg32 = read32(base + 0x68);
147 reg32 |= (1 << 0) | (1 << 1);
148 write32(base + 0x68, reg32);
149
150 /* Use a 50 usec timeout - the Linux kernel uses the
151 * same duration */
152
153 int timeout = 50;
154 while(timeout--) {
155 reg32 = read32(base + HDA_ICII_REG);
156 if ((reg32 & (HDA_ICII_VALID | HDA_ICII_BUSY)) ==
157 HDA_ICII_VALID)
158 return 0;
159 udelay(1);
160 }
161
162 return -1;
163}
164
165static void codec_init(struct device *dev, u32 base, int addr)
166{
167 u32 reg32;
168 const u32 *verb;
169 u32 verb_size;
170 int i;
171
172 printk(BIOS_DEBUG, "HD Audio: Initializing codec #%d\n", addr);
173
174 /* 1 */
175 if (wait_for_ready(base) == -1)
176 return;
177
178 reg32 = (addr << 28) | 0x000f0000;
179 write32(base + 0x60, reg32);
180
181 if (wait_for_valid(base) == -1)
182 return;
183
184 reg32 = read32(base + 0x64);
185
186 /* 2 */
187 printk(BIOS_DEBUG, "Azalia: codec viddid: %08x\n", reg32);
188 verb_size = find_verb(dev, reg32, &verb);
189
190 if (!verb_size) {
191 printk(BIOS_DEBUG, "Azalia: No verb!\n");
192 return;
193 }
194 printk(BIOS_DEBUG, "Azalia: verb_size: %d\n", verb_size);
195
196 /* 3 */
197 for (i = 0; i < verb_size; i++) {
198 if (wait_for_ready(base) == -1)
199 return;
200
201 write32(base + 0x60, verb[i]);
202
203 if (wait_for_valid(base) == -1)
204 return;
205 }
206 printk(BIOS_DEBUG, "Azalia: verb loaded.\n");
207}
208
209static void codecs_init(struct device *dev, u32 base, u32 codec_mask)
210{
211 int i;
212 for (i = 2; i >= 0; i--) {
213 if (codec_mask & (1 << i))
214 codec_init(dev, base, i);
215 }
216
217 for (i = 0; i < pc_beep_verbs_size; i++) {
218 if (wait_for_ready(base) == -1)
219 return;
220
221 write32(base + 0x60, pc_beep_verbs[i]);
222
223 if (wait_for_valid(base) == -1)
224 return;
225 }
226}
227
228static void azalia_init(struct device *dev)
229{
230 u32 base;
231 struct resource *res;
232 u32 codec_mask;
233 u8 reg8;
234 u32 reg32;
235
Patrick Georgie72a8a32012-11-06 11:05:09 +0100236 // ESD
Kyösti Mälkki9b143e12013-07-26 08:35:09 +0300237 reg32 = pci_read_config32(dev, 0x134);
Patrick Georgie72a8a32012-11-06 11:05:09 +0100238 reg32 &= 0xff00ffff;
239 reg32 |= (2 << 16);
Kyösti Mälkki9b143e12013-07-26 08:35:09 +0300240 pci_write_config32(dev, 0x134, reg32);
Patrick Georgie72a8a32012-11-06 11:05:09 +0100241
242 // Link1 description
Kyösti Mälkki9b143e12013-07-26 08:35:09 +0300243 reg32 = pci_read_config32(dev, 0x140);
Patrick Georgie72a8a32012-11-06 11:05:09 +0100244 reg32 &= 0xff00ffff;
245 reg32 |= (2 << 16);
Kyösti Mälkki9b143e12013-07-26 08:35:09 +0300246 pci_write_config32(dev, 0x140, reg32);
Patrick Georgie72a8a32012-11-06 11:05:09 +0100247
248 // Port VC0 Resource Control Register
Kyösti Mälkki9b143e12013-07-26 08:35:09 +0300249 reg32 = pci_read_config32(dev, 0x114);
Patrick Georgie72a8a32012-11-06 11:05:09 +0100250 reg32 &= 0xffffff00;
251 reg32 |= 1;
Kyösti Mälkki9b143e12013-07-26 08:35:09 +0300252 pci_write_config32(dev, 0x114, reg32);
Patrick Georgie72a8a32012-11-06 11:05:09 +0100253
254 // VCi traffic class
Kyösti Mälkki9b143e12013-07-26 08:35:09 +0300255 reg8 = pci_read_config8(dev, 0x44);
Patrick Georgie72a8a32012-11-06 11:05:09 +0100256 reg8 |= (7 << 0); // TC7
Kyösti Mälkki9b143e12013-07-26 08:35:09 +0300257 pci_write_config8(dev, 0x44, reg8);
Patrick Georgie72a8a32012-11-06 11:05:09 +0100258
259 // VCi Resource Control
Kyösti Mälkki9b143e12013-07-26 08:35:09 +0300260 reg32 = pci_read_config32(dev, 0x120);
Patrick Georgie72a8a32012-11-06 11:05:09 +0100261 reg32 |= (1 << 31);
262 reg32 |= (1 << 24); // VCi ID
263 reg32 |= (0x80 << 0); // VCi map
Kyösti Mälkki9b143e12013-07-26 08:35:09 +0300264 pci_write_config32(dev, 0x120, reg32);
Patrick Georgie72a8a32012-11-06 11:05:09 +0100265
266 /* Set Bus Master */
267 reg32 = pci_read_config32(dev, PCI_COMMAND);
268 pci_write_config32(dev, PCI_COMMAND, reg32 | PCI_COMMAND_MASTER);
269
270 reg8 = pci_read_config8(dev, 0x4d); // Docking Status
271 reg8 &= ~(1 << 7); // Docking not supported
272 pci_write_config8(dev, 0x4d, reg8);
273
274 /* Lock some R/WO bits by writing their current value. */
275 reg32 = pci_read_config32(dev, 0x74);
276 pci_write_config32(dev, 0x74, reg32);
277
278 res = find_resource(dev, 0x10);
279 if (!res)
280 return;
281
282 // NOTE this will break as soon as the Azalia get's a bar above
283 // 4G. Is there anything we can do about it?
284 base = (u32)res->base;
285 printk(BIOS_DEBUG, "Azalia: base = %08x\n", (u32)base);
286 codec_mask = codec_detect(base);
287
288 if (codec_mask) {
289 printk(BIOS_DEBUG, "Azalia: codec_mask = %02x\n", codec_mask);
290 codecs_init(dev, base, codec_mask);
291 }
292}
293
294static void azalia_set_subsystem(device_t dev, unsigned vendor, unsigned device)
295{
296 if (!vendor || !device) {
297 pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID,
298 pci_read_config32(dev, PCI_VENDOR_ID));
299 } else {
300 pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID,
301 ((device & 0xffff) << 16) | (vendor & 0xffff));
302 }
303}
304
305static struct pci_operations azalia_pci_ops = {
306 .set_subsystem = azalia_set_subsystem,
307};
308
309static struct device_operations azalia_ops = {
310 .read_resources = pci_dev_read_resources,
311 .set_resources = pci_dev_set_resources,
312 .enable_resources = pci_dev_enable_resources,
313 .init = azalia_init,
314 .scan_bus = 0,
315 .ops_pci = &azalia_pci_ops,
316};
317
318/* ICH9DH/ICH9DO/ICH9R/ICH9/ICH9M-E/ICH9M */
319static const struct pci_driver i82801ix_azalia __pci_driver = {
320 .ops = &azalia_ops,
321 .vendor = PCI_VENDOR_ID_INTEL,
322 .device = 0x293e,
323};