blob: 0cbf3c6880d96ea19203874d586a9164ed53204e [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.
Stefan Reinauer8e073822012-04-04 00:07:22 +020016 */
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>
Arthur Heymansd2d2aef2018-01-16 14:19:37 +010026#include <southbridge/intel/common/rcba.h>
Stefan Reinauer8e073822012-04-04 00:07:22 +020027#include "pch.h"
28
29#define HDA_ICII_REG 0x68
Andrew Wuae8d0692013-08-02 19:29:17 +080030#define HDA_ICII_BUSY (1 << 0)
31#define HDA_ICII_VALID (1 << 1)
Stefan Reinauer8e073822012-04-04 00:07:22 +020032
33typedef struct southbridge_intel_bd82x6x_config config_t;
34
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080035static int set_bits(void *port, u32 mask, u32 val)
Stefan Reinauer8e073822012-04-04 00:07:22 +020036{
37 u32 reg32;
38 int count;
39
40 /* Write (val & mask) to port */
41 val &= mask;
42 reg32 = read32(port);
43 reg32 &= ~mask;
44 reg32 |= val;
45 write32(port, reg32);
46
47 /* Wait for readback of register to
48 * match what was just written to it
49 */
50 count = 50;
51 do {
52 /* Wait 1ms based on BKDG wait time */
53 mdelay(1);
54 reg32 = read32(port);
55 reg32 &= mask;
56 } while ((reg32 != val) && --count);
57
58 /* Timeout occurred */
59 if (!count)
60 return -1;
61 return 0;
62}
63
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080064static int codec_detect(u8 *base)
Stefan Reinauer8e073822012-04-04 00:07:22 +020065{
66 u8 reg8;
67
68 /* Set Bit 0 to 1 to exit reset state (BAR + 0x8)[0] */
69 if (set_bits(base + 0x08, 1, 1) == -1)
70 goto no_codec;
71
72 /* Write back the value once reset bit is set. */
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080073 write16(base + 0x0,
74 read16(base + 0x0));
Stefan Reinauer8e073822012-04-04 00:07:22 +020075
76 /* Read in Codec location (BAR + 0xe)[2..0]*/
77 reg8 = read8(base + 0xe);
78 reg8 &= 0x0f;
79 if (!reg8)
80 goto no_codec;
81
82 return reg8;
83
84no_codec:
85 /* Codec Not found */
86 /* Put HDA back in reset (BAR + 0x8) [0] */
87 set_bits(base + 0x08, 1, 0);
88 printk(BIOS_DEBUG, "Azalia: No codec!\n");
89 return 0;
90}
91
Stefan Reinauer8e073822012-04-04 00:07:22 +020092static u32 find_verb(struct device *dev, u32 viddid, const u32 ** verb)
93{
94 int idx=0;
95
96 while (idx < (cim_verb_data_size / sizeof(u32))) {
97 u32 verb_size = 4 * cim_verb_data[idx+2]; // in u32
98 if (cim_verb_data[idx] != viddid) {
99 idx += verb_size + 3; // skip verb + header
100 continue;
101 }
102 *verb = &cim_verb_data[idx+3];
103 return verb_size;
104 }
105
106 /* Not all codecs need to load another verb */
107 return 0;
108}
109
110/**
111 * Wait 50usec for the codec to indicate it is ready
112 * no response would imply that the codec is non-operative
113 */
114
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800115static int wait_for_ready(u8 *base)
Stefan Reinauer8e073822012-04-04 00:07:22 +0200116{
Stefan Reinauer15ba2bc2012-11-14 12:25:15 -0800117 /* Use a 1msec timeout */
Stefan Reinauer8e073822012-04-04 00:07:22 +0200118
Stefan Reinauer15ba2bc2012-11-14 12:25:15 -0800119 int timeout = 1000;
Stefan Reinauer8e073822012-04-04 00:07:22 +0200120
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);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200123 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)
Stefan Reinauer8e073822012-04-04 00:07:22 +0200138{
139 u32 reg32;
140
141 /* Send the verb to the codec */
142 reg32 = read32(base + HDA_ICII_REG);
143 reg32 |= HDA_ICII_BUSY | HDA_ICII_VALID;
144 write32(base + HDA_ICII_REG, reg32);
145
Stefan Reinauer15ba2bc2012-11-14 12:25:15 -0800146 /* Use a 1msec timeout */
Stefan Reinauer8e073822012-04-04 00:07:22 +0200147
Stefan Reinauer15ba2bc2012-11-14 12:25:15 -0800148 int timeout = 1000;
Elyes HAOUASba28e8d2016-08-31 19:22:16 +0200149 while (timeout--) {
Stefan Reinauer8e073822012-04-04 00:07:22 +0200150 reg32 = read32(base + HDA_ICII_REG);
151 if ((reg32 & (HDA_ICII_VALID | HDA_ICII_BUSY)) ==
152 HDA_ICII_VALID)
153 return 0;
154 udelay(1);
155 }
156
157 return -1;
158}
159
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800160static void codec_init(struct device *dev, u8 *base, int addr)
Stefan Reinauer8e073822012-04-04 00:07:22 +0200161{
162 u32 reg32;
163 const u32 *verb;
164 u32 verb_size;
165 int i;
166
167 printk(BIOS_DEBUG, "Azalia: Initializing codec #%d\n", addr);
168
169 /* 1 */
170 if (wait_for_ready(base) == -1) {
171 printk(BIOS_DEBUG, " codec not ready.\n");
172 return;
173 }
174
175 reg32 = (addr << 28) | 0x000f0000;
176 write32(base + 0x60, reg32);
177
178 if (wait_for_valid(base) == -1) {
179 printk(BIOS_DEBUG, " codec not valid.\n");
180 return;
181 }
182
183 reg32 = read32(base + 0x64);
184
185 /* 2 */
186 printk(BIOS_DEBUG, "Azalia: codec viddid: %08x\n", reg32);
187 verb_size = find_verb(dev, reg32, &verb);
188
189 if (!verb_size) {
190 printk(BIOS_DEBUG, "Azalia: No verb!\n");
191 return;
192 }
193 printk(BIOS_DEBUG, "Azalia: verb_size: %d\n", verb_size);
194
195 /* 3 */
196 for (i = 0; i < verb_size; i++) {
197 if (wait_for_ready(base) == -1)
198 return;
199
200 write32(base + 0x60, verb[i]);
201
202 if (wait_for_valid(base) == -1)
203 return;
204 }
205 printk(BIOS_DEBUG, "Azalia: verb loaded.\n");
206}
207
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800208static void codecs_init(struct device *dev, u8 *base, u32 codec_mask)
Stefan Reinauer8e073822012-04-04 00:07:22 +0200209{
210 int i;
211 for (i = 3; i >= 0; i--) {
212 if (codec_mask & (1 << i))
213 codec_init(dev, base, i);
214 }
Dylan Reidb98d0782012-04-27 11:37:33 -0700215
216 for (i = 0; i < pc_beep_verbs_size; i++) {
217 if (wait_for_ready(base) == -1)
218 return;
219
220 write32(base + 0x60, pc_beep_verbs[i]);
221
222 if (wait_for_valid(base) == -1)
223 return;
224 }
Stefan Reinauer8e073822012-04-04 00:07:22 +0200225}
226
227static void azalia_init(struct device *dev)
228{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800229 u8 *base;
Stefan Reinauer8e073822012-04-04 00:07:22 +0200230 struct resource *res;
231 u32 codec_mask;
232 u8 reg8;
233 u16 reg16;
234 u32 reg32;
235
236 /* Find base address */
237 res = find_resource(dev, PCI_BASE_ADDRESS_0);
238 if (!res)
239 return;
240
241 // NOTE this will break as soon as the Azalia get's a bar above
242 // 4G. Is there anything we can do about it?
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800243 base = res2mmio(res, 0, 0);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200244 printk(BIOS_DEBUG, "Azalia: base = %08x\n", (u32)base);
245
246 if (RCBA32(0x2030) & (1 << 31)) {
Kyösti Mälkkifd98c652013-07-26 08:50:53 +0300247 reg32 = pci_read_config32(dev, 0x120);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200248 reg32 &= 0xf8ffff01;
Stefan Reinauer15ba2bc2012-11-14 12:25:15 -0800249 reg32 |= (1 << 24); // 2 << 24 for server
Stefan Reinauer8e073822012-04-04 00:07:22 +0200250 reg32 |= RCBA32(0x2030) & 0xfe;
Kyösti Mälkkifd98c652013-07-26 08:50:53 +0300251 pci_write_config32(dev, 0x120, reg32);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200252
Kyösti Mälkkifd98c652013-07-26 08:50:53 +0300253 reg16 = pci_read_config16(dev, 0x78);
Stefan Reinauer15ba2bc2012-11-14 12:25:15 -0800254 reg16 |= (1 << 11);
Kyösti Mälkkifd98c652013-07-26 08:50:53 +0300255 pci_write_config16(dev, 0x78, reg16);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200256 } else
257 printk(BIOS_DEBUG, "Azalia: V1CTL disabled.\n");
258
Kyösti Mälkkifd98c652013-07-26 08:50:53 +0300259 reg32 = pci_read_config32(dev, 0x114);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200260 reg32 &= ~0xfe;
Kyösti Mälkkifd98c652013-07-26 08:50:53 +0300261 pci_write_config32(dev, 0x114, reg32);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200262
263 // Set VCi enable bit
Kyösti Mälkkifd98c652013-07-26 08:50:53 +0300264 reg32 = pci_read_config32(dev, 0x120);
Stefan Reinauer15ba2bc2012-11-14 12:25:15 -0800265 reg32 |= (1 << 31);
Kyösti Mälkkifd98c652013-07-26 08:50:53 +0300266 pci_write_config32(dev, 0x120, reg32);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200267
268 // Enable HDMI codec:
269 reg32 = pci_read_config32(dev, 0xc4);
270 reg32 |= (1 << 1);
271 pci_write_config32(dev, 0xc4, reg32);
272
273 reg8 = pci_read_config8(dev, 0x43);
274 reg8 |= (1 << 6);
275 pci_write_config8(dev, 0x43, reg8);
276
277 /* Additional programming steps */
278 reg32 = pci_read_config32(dev, 0xc4);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200279 reg32 |= (1 << 13);
280 pci_write_config32(dev, 0xc4, reg32);
281
282 reg32 = pci_read_config32(dev, 0xc4);
283 reg32 |= (1 << 10);
284 pci_write_config32(dev, 0xc4, reg32);
285
286 reg32 = pci_read_config32(dev, 0xd0);
287 reg32 &= ~(1 << 31);
288 pci_write_config32(dev, 0xd0, reg32);
289
Stefan Reinauer15ba2bc2012-11-14 12:25:15 -0800290 if (dev->device == 0x1e20) {
291 /* Additional step on Panther Point */
292 reg32 = pci_read_config32(dev, 0xc4);
293 reg32 |= (1 << 17);
294 pci_write_config32(dev, 0xc4, reg32);
295 }
296
Stefan Reinauer8e073822012-04-04 00:07:22 +0200297 /* Set Bus Master */
298 reg32 = pci_read_config32(dev, PCI_COMMAND);
299 pci_write_config32(dev, PCI_COMMAND, reg32 | PCI_COMMAND_MASTER);
300
301 pci_write_config8(dev, 0x3c, 0x0a); // unused?
302
303 /* Codec Initialization Programming Sequence */
Stefan Reinauer15ba2bc2012-11-14 12:25:15 -0800304
305 /* Take controller out of reset */
Stefan Reinauer8e073822012-04-04 00:07:22 +0200306 reg32 = read32(base + 0x08);
307 reg32 |= (1 << 0);
308 write32(base + 0x08, reg32);
Stefan Reinauer15ba2bc2012-11-14 12:25:15 -0800309 /* Wait 1ms */
310 udelay(1000);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200311
312 //
313 reg8 = pci_read_config8(dev, 0x40); // Audio Control
314 reg8 |= 1; // Select Azalia mode. This needs to be controlled via devicetree.cb
315 pci_write_config8(dev, 0x40, reg8);
316
317 reg8 = pci_read_config8(dev, 0x4d); // Docking Status
318 reg8 &= ~(1 << 7); // Docking not supported
319 pci_write_config8(dev, 0x4d, reg8);
320
321 codec_mask = codec_detect(base);
322
323 if (codec_mask) {
324 printk(BIOS_DEBUG, "Azalia: codec_mask = %02x\n", codec_mask);
325 codecs_init(dev, base, codec_mask);
326 }
327
328 /* Enable dynamic clock gating */
329 reg8 = pci_read_config8(dev, 0x43);
330 reg8 &= ~0x7;
331 reg8 |= (1 << 2) | (1 << 0);
332 pci_write_config8(dev, 0x43, reg8);
333}
334
Aaron Durbinaa090cb2017-09-13 16:01:52 -0600335static const char *azalia_acpi_name(const struct device *dev)
Patrick Rudolph604f6982017-06-07 09:46:52 +0200336{
337 return "HDEF";
338}
339
Elyes HAOUAS4aec3402018-05-25 08:29:27 +0200340static void azalia_set_subsystem(struct device *dev, unsigned vendor,
341 unsigned device)
Stefan Reinauer8e073822012-04-04 00:07:22 +0200342{
343 if (!vendor || !device) {
344 pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID,
345 pci_read_config32(dev, PCI_VENDOR_ID));
346 } else {
347 pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID,
348 ((device & 0xffff) << 16) | (vendor & 0xffff));
349 }
350}
351
352static struct pci_operations azalia_pci_ops = {
353 .set_subsystem = azalia_set_subsystem,
354};
355
356static struct device_operations azalia_ops = {
357 .read_resources = pci_dev_read_resources,
358 .set_resources = pci_dev_set_resources,
359 .enable_resources = pci_dev_enable_resources,
360 .init = azalia_init,
361 .scan_bus = 0,
362 .ops_pci = &azalia_pci_ops,
Patrick Rudolph604f6982017-06-07 09:46:52 +0200363 .acpi_name = azalia_acpi_name,
Stefan Reinauer8e073822012-04-04 00:07:22 +0200364};
365
Stefan Reinauer9a380ab2012-06-22 13:16:11 -0700366static const unsigned short pci_device_ids[] = { 0x1c20, 0x1e20, 0 };
Stefan Reinauer8e073822012-04-04 00:07:22 +0200367
Stefan Reinauer9a380ab2012-06-22 13:16:11 -0700368static const struct pci_driver pch_azalia __pci_driver = {
369 .ops = &azalia_ops,
370 .vendor = PCI_VENDOR_ID_INTEL,
371 .devices = pci_device_ids,
Stefan Reinauer8e073822012-04-04 00:07:22 +0200372};