blob: bef88abea4eb9880dbf6d890352b382e68b5226d [file] [log] [blame]
Stefan Reinauer8e073822012-04-04 00:07:22 +02001/*
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) 2011 The ChromiumOS Authors. All rights reserved.
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
Stefan Reinauer8e073822012-04-04 00:07:22 +020020 */
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>
Stefan Reinauer8e073822012-04-04 00:07:22 +020030#include "pch.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)
Stefan Reinauer8e073822012-04-04 00:07:22 +020035
36typedef struct southbridge_intel_bd82x6x_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 u8 reg8;
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 /* Write back the value once reset bit is set. */
76 write16(base + 0x0, read16(base + 0x0));
77
78 /* Read in Codec location (BAR + 0xe)[2..0]*/
79 reg8 = read8(base + 0xe);
80 reg8 &= 0x0f;
81 if (!reg8)
82 goto no_codec;
83
84 return reg8;
85
86no_codec:
87 /* Codec Not found */
88 /* Put HDA back in reset (BAR + 0x8) [0] */
89 set_bits(base + 0x08, 1, 0);
90 printk(BIOS_DEBUG, "Azalia: No codec!\n");
91 return 0;
92}
93
Stefan Reinauer8e073822012-04-04 00:07:22 +020094static u32 find_verb(struct device *dev, u32 viddid, const u32 ** verb)
95{
96 int idx=0;
97
98 while (idx < (cim_verb_data_size / sizeof(u32))) {
99 u32 verb_size = 4 * cim_verb_data[idx+2]; // in u32
100 if (cim_verb_data[idx] != viddid) {
101 idx += verb_size + 3; // skip verb + header
102 continue;
103 }
104 *verb = &cim_verb_data[idx+3];
105 return verb_size;
106 }
107
108 /* Not all codecs need to load another verb */
109 return 0;
110}
111
112/**
113 * Wait 50usec for the codec to indicate it is ready
114 * no response would imply that the codec is non-operative
115 */
116
117static int wait_for_ready(u32 base)
118{
Stefan Reinauer15ba2bc2012-11-14 12:25:15 -0800119 /* Use a 1msec timeout */
Stefan Reinauer8e073822012-04-04 00:07:22 +0200120
Stefan Reinauer15ba2bc2012-11-14 12:25:15 -0800121 int timeout = 1000;
Stefan Reinauer8e073822012-04-04 00:07:22 +0200122
123 while(timeout--) {
124 u32 reg32 = read32(base + HDA_ICII_REG);
125 if (!(reg32 & HDA_ICII_BUSY))
126 return 0;
127 udelay(1);
128 }
129
130 return -1;
131}
132
133/**
134 * Wait 50usec for the codec to indicate that it accepted
135 * the previous command. No response would imply that the code
136 * is non-operative
137 */
138
139static int wait_for_valid(u32 base)
140{
141 u32 reg32;
142
143 /* Send the verb to the codec */
144 reg32 = read32(base + HDA_ICII_REG);
145 reg32 |= HDA_ICII_BUSY | HDA_ICII_VALID;
146 write32(base + HDA_ICII_REG, reg32);
147
Stefan Reinauer15ba2bc2012-11-14 12:25:15 -0800148 /* Use a 1msec timeout */
Stefan Reinauer8e073822012-04-04 00:07:22 +0200149
Stefan Reinauer15ba2bc2012-11-14 12:25:15 -0800150 int timeout = 1000;
Stefan Reinauer8e073822012-04-04 00:07:22 +0200151 while(timeout--) {
152 reg32 = read32(base + HDA_ICII_REG);
153 if ((reg32 & (HDA_ICII_VALID | HDA_ICII_BUSY)) ==
154 HDA_ICII_VALID)
155 return 0;
156 udelay(1);
157 }
158
159 return -1;
160}
161
162static void codec_init(struct device *dev, u32 base, int addr)
163{
164 u32 reg32;
165 const u32 *verb;
166 u32 verb_size;
167 int i;
168
169 printk(BIOS_DEBUG, "Azalia: Initializing codec #%d\n", addr);
170
171 /* 1 */
172 if (wait_for_ready(base) == -1) {
173 printk(BIOS_DEBUG, " codec not ready.\n");
174 return;
175 }
176
177 reg32 = (addr << 28) | 0x000f0000;
178 write32(base + 0x60, reg32);
179
180 if (wait_for_valid(base) == -1) {
181 printk(BIOS_DEBUG, " codec not valid.\n");
182 return;
183 }
184
185 reg32 = read32(base + 0x64);
186
187 /* 2 */
188 printk(BIOS_DEBUG, "Azalia: codec viddid: %08x\n", reg32);
189 verb_size = find_verb(dev, reg32, &verb);
190
191 if (!verb_size) {
192 printk(BIOS_DEBUG, "Azalia: No verb!\n");
193 return;
194 }
195 printk(BIOS_DEBUG, "Azalia: verb_size: %d\n", verb_size);
196
197 /* 3 */
198 for (i = 0; i < verb_size; i++) {
199 if (wait_for_ready(base) == -1)
200 return;
201
202 write32(base + 0x60, verb[i]);
203
204 if (wait_for_valid(base) == -1)
205 return;
206 }
207 printk(BIOS_DEBUG, "Azalia: verb loaded.\n");
208}
209
210static void codecs_init(struct device *dev, u32 base, u32 codec_mask)
211{
212 int i;
213 for (i = 3; i >= 0; i--) {
214 if (codec_mask & (1 << i))
215 codec_init(dev, base, i);
216 }
Dylan Reidb98d0782012-04-27 11:37:33 -0700217
218 for (i = 0; i < pc_beep_verbs_size; i++) {
219 if (wait_for_ready(base) == -1)
220 return;
221
222 write32(base + 0x60, pc_beep_verbs[i]);
223
224 if (wait_for_valid(base) == -1)
225 return;
226 }
Stefan Reinauer8e073822012-04-04 00:07:22 +0200227}
228
229static void azalia_init(struct device *dev)
230{
231 u32 base;
232 struct resource *res;
233 u32 codec_mask;
234 u8 reg8;
235 u16 reg16;
236 u32 reg32;
237
238 /* Find base address */
239 res = find_resource(dev, PCI_BASE_ADDRESS_0);
240 if (!res)
241 return;
242
243 // NOTE this will break as soon as the Azalia get's a bar above
244 // 4G. Is there anything we can do about it?
245 base = (u32)res->base;
246 printk(BIOS_DEBUG, "Azalia: base = %08x\n", (u32)base);
247
248 if (RCBA32(0x2030) & (1 << 31)) {
Kyösti Mälkkifd98c652013-07-26 08:50:53 +0300249 reg32 = pci_read_config32(dev, 0x120);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200250 reg32 &= 0xf8ffff01;
Stefan Reinauer15ba2bc2012-11-14 12:25:15 -0800251 reg32 |= (1 << 24); // 2 << 24 for server
Stefan Reinauer8e073822012-04-04 00:07:22 +0200252 reg32 |= RCBA32(0x2030) & 0xfe;
Kyösti Mälkkifd98c652013-07-26 08:50:53 +0300253 pci_write_config32(dev, 0x120, reg32);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200254
Kyösti Mälkkifd98c652013-07-26 08:50:53 +0300255 reg16 = pci_read_config16(dev, 0x78);
Stefan Reinauer15ba2bc2012-11-14 12:25:15 -0800256 reg16 |= (1 << 11);
Kyösti Mälkkifd98c652013-07-26 08:50:53 +0300257 pci_write_config16(dev, 0x78, reg16);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200258 } else
259 printk(BIOS_DEBUG, "Azalia: V1CTL disabled.\n");
260
Kyösti Mälkkifd98c652013-07-26 08:50:53 +0300261 reg32 = pci_read_config32(dev, 0x114);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200262 reg32 &= ~0xfe;
Kyösti Mälkkifd98c652013-07-26 08:50:53 +0300263 pci_write_config32(dev, 0x114, reg32);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200264
265 // Set VCi enable bit
Kyösti Mälkkifd98c652013-07-26 08:50:53 +0300266 reg32 = pci_read_config32(dev, 0x120);
Stefan Reinauer15ba2bc2012-11-14 12:25:15 -0800267 reg32 |= (1 << 31);
Kyösti Mälkkifd98c652013-07-26 08:50:53 +0300268 pci_write_config32(dev, 0x120, reg32);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200269
270 // Enable HDMI codec:
271 reg32 = pci_read_config32(dev, 0xc4);
272 reg32 |= (1 << 1);
273 pci_write_config32(dev, 0xc4, reg32);
274
275 reg8 = pci_read_config8(dev, 0x43);
276 reg8 |= (1 << 6);
277 pci_write_config8(dev, 0x43, reg8);
278
279 /* Additional programming steps */
280 reg32 = pci_read_config32(dev, 0xc4);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200281 reg32 |= (1 << 13);
282 pci_write_config32(dev, 0xc4, reg32);
283
284 reg32 = pci_read_config32(dev, 0xc4);
285 reg32 |= (1 << 10);
286 pci_write_config32(dev, 0xc4, reg32);
287
288 reg32 = pci_read_config32(dev, 0xd0);
289 reg32 &= ~(1 << 31);
290 pci_write_config32(dev, 0xd0, reg32);
291
Stefan Reinauer15ba2bc2012-11-14 12:25:15 -0800292 if (dev->device == 0x1e20) {
293 /* Additional step on Panther Point */
294 reg32 = pci_read_config32(dev, 0xc4);
295 reg32 |= (1 << 17);
296 pci_write_config32(dev, 0xc4, reg32);
297 }
298
Stefan Reinauer8e073822012-04-04 00:07:22 +0200299 /* Set Bus Master */
300 reg32 = pci_read_config32(dev, PCI_COMMAND);
301 pci_write_config32(dev, PCI_COMMAND, reg32 | PCI_COMMAND_MASTER);
302
303 pci_write_config8(dev, 0x3c, 0x0a); // unused?
304
305 /* Codec Initialization Programming Sequence */
Stefan Reinauer15ba2bc2012-11-14 12:25:15 -0800306
307 /* Take controller out of reset */
Stefan Reinauer8e073822012-04-04 00:07:22 +0200308 reg32 = read32(base + 0x08);
309 reg32 |= (1 << 0);
310 write32(base + 0x08, reg32);
Stefan Reinauer15ba2bc2012-11-14 12:25:15 -0800311 /* Wait 1ms */
312 udelay(1000);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200313
314 //
315 reg8 = pci_read_config8(dev, 0x40); // Audio Control
316 reg8 |= 1; // Select Azalia mode. This needs to be controlled via devicetree.cb
317 pci_write_config8(dev, 0x40, reg8);
318
319 reg8 = pci_read_config8(dev, 0x4d); // Docking Status
320 reg8 &= ~(1 << 7); // Docking not supported
321 pci_write_config8(dev, 0x4d, reg8);
322
323 codec_mask = codec_detect(base);
324
325 if (codec_mask) {
326 printk(BIOS_DEBUG, "Azalia: codec_mask = %02x\n", codec_mask);
327 codecs_init(dev, base, codec_mask);
328 }
329
330 /* Enable dynamic clock gating */
331 reg8 = pci_read_config8(dev, 0x43);
332 reg8 &= ~0x7;
333 reg8 |= (1 << 2) | (1 << 0);
334 pci_write_config8(dev, 0x43, reg8);
335}
336
337static void azalia_set_subsystem(device_t dev, unsigned vendor, unsigned device)
338{
339 if (!vendor || !device) {
340 pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID,
341 pci_read_config32(dev, PCI_VENDOR_ID));
342 } else {
343 pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID,
344 ((device & 0xffff) << 16) | (vendor & 0xffff));
345 }
346}
347
348static struct pci_operations azalia_pci_ops = {
349 .set_subsystem = azalia_set_subsystem,
350};
351
352static struct device_operations azalia_ops = {
353 .read_resources = pci_dev_read_resources,
354 .set_resources = pci_dev_set_resources,
355 .enable_resources = pci_dev_enable_resources,
356 .init = azalia_init,
357 .scan_bus = 0,
358 .ops_pci = &azalia_pci_ops,
359};
360
Stefan Reinauer9a380ab2012-06-22 13:16:11 -0700361static const unsigned short pci_device_ids[] = { 0x1c20, 0x1e20, 0 };
Stefan Reinauer8e073822012-04-04 00:07:22 +0200362
Stefan Reinauer9a380ab2012-06-22 13:16:11 -0700363static const struct pci_driver pch_azalia __pci_driver = {
364 .ops = &azalia_ops,
365 .vendor = PCI_VENDOR_ID_INTEL,
366 .devices = pci_device_ids,
Stefan Reinauer8e073822012-04-04 00:07:22 +0200367};