blob: 744fe7d6a65a59c7d0985de4da4f6c7b8ec42edb [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>
Kyösti Mälkki13f66502019-03-03 08:01:05 +020023#include <device/mmio.h>
Stefan Reinauer8e073822012-04-04 00:07:22 +020024#include <delay.h>
Vladimir Serbinenko75c83872014-09-05 01:01:31 +020025#include <device/azalia_device.h>
Kyösti Mälkki12b121c2019-08-18 16:33:39 +030026
27#include "chip.h"
Stefan Reinauer8e073822012-04-04 00:07:22 +020028#include "pch.h"
29
30#define HDA_ICII_REG 0x68
Andrew Wuae8d0692013-08-02 19:29:17 +080031#define HDA_ICII_BUSY (1 << 0)
32#define HDA_ICII_VALID (1 << 1)
Stefan Reinauer8e073822012-04-04 00:07:22 +020033
34typedef struct southbridge_intel_bd82x6x_config config_t;
35
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080036static int set_bits(void *port, u32 mask, u32 val)
Stefan Reinauer8e073822012-04-04 00:07:22 +020037{
38 u32 reg32;
39 int count;
40
41 /* Write (val & mask) to port */
42 val &= mask;
43 reg32 = read32(port);
44 reg32 &= ~mask;
45 reg32 |= val;
46 write32(port, reg32);
47
48 /* Wait for readback of register to
49 * match what was just written to it
50 */
51 count = 50;
52 do {
53 /* Wait 1ms based on BKDG wait time */
54 mdelay(1);
55 reg32 = read32(port);
56 reg32 &= mask;
57 } while ((reg32 != val) && --count);
58
59 /* Timeout occurred */
60 if (!count)
61 return -1;
62 return 0;
63}
64
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080065static int codec_detect(u8 *base)
Stefan Reinauer8e073822012-04-04 00:07:22 +020066{
67 u8 reg8;
68
69 /* Set Bit 0 to 1 to exit reset state (BAR + 0x8)[0] */
70 if (set_bits(base + 0x08, 1, 1) == -1)
71 goto no_codec;
72
73 /* Write back the value once reset bit is set. */
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080074 write16(base + 0x0,
75 read16(base + 0x0));
Stefan Reinauer8e073822012-04-04 00:07:22 +020076
77 /* Read in Codec location (BAR + 0xe)[2..0]*/
78 reg8 = read8(base + 0xe);
79 reg8 &= 0x0f;
80 if (!reg8)
81 goto no_codec;
82
83 return reg8;
84
85no_codec:
86 /* Codec Not found */
87 /* Put HDA back in reset (BAR + 0x8) [0] */
88 set_bits(base + 0x08, 1, 0);
89 printk(BIOS_DEBUG, "Azalia: No codec!\n");
90 return 0;
91}
92
Elyes HAOUASe414a4e2019-01-03 10:40:43 +010093static u32 find_verb(struct device *dev, u32 viddid, const u32 **verb)
Stefan Reinauer8e073822012-04-04 00:07:22 +020094{
95 int idx=0;
96
97 while (idx < (cim_verb_data_size / sizeof(u32))) {
98 u32 verb_size = 4 * cim_verb_data[idx+2]; // in u32
99 if (cim_verb_data[idx] != viddid) {
100 idx += verb_size + 3; // skip verb + header
101 continue;
102 }
103 *verb = &cim_verb_data[idx+3];
104 return verb_size;
105 }
106
107 /* Not all codecs need to load another verb */
108 return 0;
109}
110
111/**
112 * Wait 50usec for the codec to indicate it is ready
113 * no response would imply that the codec is non-operative
114 */
115
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800116static int wait_for_ready(u8 *base)
Stefan Reinauer8e073822012-04-04 00:07:22 +0200117{
Stefan Reinauer15ba2bc2012-11-14 12:25:15 -0800118 /* Use a 1msec timeout */
Stefan Reinauer8e073822012-04-04 00:07:22 +0200119
Stefan Reinauer15ba2bc2012-11-14 12:25:15 -0800120 int timeout = 1000;
Stefan Reinauer8e073822012-04-04 00:07:22 +0200121
Elyes HAOUASba28e8d2016-08-31 19:22:16 +0200122 while (timeout--) {
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800123 u32 reg32 = read32(base + HDA_ICII_REG);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200124 if (!(reg32 & HDA_ICII_BUSY))
125 return 0;
126 udelay(1);
127 }
128
129 return -1;
130}
131
132/**
133 * Wait 50usec for the codec to indicate that it accepted
134 * the previous command. No response would imply that the code
135 * is non-operative
136 */
137
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800138static int wait_for_valid(u8 *base)
Stefan Reinauer8e073822012-04-04 00:07:22 +0200139{
140 u32 reg32;
141
142 /* Send the verb to the codec */
143 reg32 = read32(base + HDA_ICII_REG);
144 reg32 |= HDA_ICII_BUSY | HDA_ICII_VALID;
145 write32(base + HDA_ICII_REG, reg32);
146
Stefan Reinauer15ba2bc2012-11-14 12:25:15 -0800147 /* Use a 1msec timeout */
Stefan Reinauer8e073822012-04-04 00:07:22 +0200148
Stefan Reinauer15ba2bc2012-11-14 12:25:15 -0800149 int timeout = 1000;
Elyes HAOUASba28e8d2016-08-31 19:22:16 +0200150 while (timeout--) {
Stefan Reinauer8e073822012-04-04 00:07:22 +0200151 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)
Stefan Reinauer8e073822012-04-04 00:07:22 +0200162{
163 u32 reg32;
164 const u32 *verb;
165 u32 verb_size;
166 int i;
167
168 printk(BIOS_DEBUG, "Azalia: Initializing codec #%d\n", addr);
169
170 /* 1 */
171 if (wait_for_ready(base) == -1) {
172 printk(BIOS_DEBUG, " codec not ready.\n");
173 return;
174 }
175
176 reg32 = (addr << 28) | 0x000f0000;
177 write32(base + 0x60, reg32);
178
179 if (wait_for_valid(base) == -1) {
180 printk(BIOS_DEBUG, " codec not valid.\n");
181 return;
182 }
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
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800209static void codecs_init(struct device *dev, u8 *base, u32 codec_mask)
Stefan Reinauer8e073822012-04-04 00:07:22 +0200210{
211 int i;
212 for (i = 3; i >= 0; i--) {
213 if (codec_mask & (1 << i))
214 codec_init(dev, base, i);
215 }
Dylan Reidb98d0782012-04-27 11:37:33 -0700216
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 }
Stefan Reinauer8e073822012-04-04 00:07:22 +0200226}
227
228static void azalia_init(struct device *dev)
229{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800230 u8 *base;
Stefan Reinauer8e073822012-04-04 00:07:22 +0200231 struct resource *res;
232 u32 codec_mask;
233 u8 reg8;
234 u16 reg16;
235 u32 reg32;
236
237 /* Find base address */
238 res = find_resource(dev, PCI_BASE_ADDRESS_0);
239 if (!res)
240 return;
241
242 // NOTE this will break as soon as the Azalia get's a bar above
243 // 4G. Is there anything we can do about it?
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800244 base = res2mmio(res, 0, 0);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200245 printk(BIOS_DEBUG, "Azalia: base = %08x\n", (u32)base);
246
Patrick Rudolph4f8b1082019-07-14 11:54:58 +0200247 if (RCBA32(CIR31) & (1 << 31)) {
Kyösti Mälkkifd98c652013-07-26 08:50:53 +0300248 reg32 = pci_read_config32(dev, 0x120);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200249 reg32 &= 0xf8ffff01;
Stefan Reinauer15ba2bc2012-11-14 12:25:15 -0800250 reg32 |= (1 << 24); // 2 << 24 for server
Patrick Rudolph4f8b1082019-07-14 11:54:58 +0200251 reg32 |= RCBA32(CIR31) & 0xfe;
Kyösti Mälkkifd98c652013-07-26 08:50:53 +0300252 pci_write_config32(dev, 0x120, reg32);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200253
Kyösti Mälkkifd98c652013-07-26 08:50:53 +0300254 reg16 = pci_read_config16(dev, 0x78);
Stefan Reinauer15ba2bc2012-11-14 12:25:15 -0800255 reg16 |= (1 << 11);
Kyösti Mälkkifd98c652013-07-26 08:50:53 +0300256 pci_write_config16(dev, 0x78, reg16);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200257 } else
258 printk(BIOS_DEBUG, "Azalia: V1CTL disabled.\n");
259
Kyösti Mälkkifd98c652013-07-26 08:50:53 +0300260 reg32 = pci_read_config32(dev, 0x114);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200261 reg32 &= ~0xfe;
Kyösti Mälkkifd98c652013-07-26 08:50:53 +0300262 pci_write_config32(dev, 0x114, reg32);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200263
264 // Set VCi enable bit
Kyösti Mälkkifd98c652013-07-26 08:50:53 +0300265 reg32 = pci_read_config32(dev, 0x120);
Stefan Reinauer15ba2bc2012-11-14 12:25:15 -0800266 reg32 |= (1 << 31);
Kyösti Mälkkifd98c652013-07-26 08:50:53 +0300267 pci_write_config32(dev, 0x120, reg32);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200268
269 // Enable HDMI codec:
270 reg32 = pci_read_config32(dev, 0xc4);
271 reg32 |= (1 << 1);
272 pci_write_config32(dev, 0xc4, reg32);
273
274 reg8 = pci_read_config8(dev, 0x43);
275 reg8 |= (1 << 6);
276 pci_write_config8(dev, 0x43, reg8);
277
278 /* Additional programming steps */
279 reg32 = pci_read_config32(dev, 0xc4);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200280 reg32 |= (1 << 13);
281 pci_write_config32(dev, 0xc4, reg32);
282
283 reg32 = pci_read_config32(dev, 0xc4);
284 reg32 |= (1 << 10);
285 pci_write_config32(dev, 0xc4, reg32);
286
287 reg32 = pci_read_config32(dev, 0xd0);
288 reg32 &= ~(1 << 31);
289 pci_write_config32(dev, 0xd0, reg32);
290
Stefan Reinauer15ba2bc2012-11-14 12:25:15 -0800291 if (dev->device == 0x1e20) {
292 /* Additional step on Panther Point */
293 reg32 = pci_read_config32(dev, 0xc4);
294 reg32 |= (1 << 17);
295 pci_write_config32(dev, 0xc4, reg32);
296 }
297
Stefan Reinauer8e073822012-04-04 00:07:22 +0200298 /* Set Bus Master */
299 reg32 = pci_read_config32(dev, PCI_COMMAND);
300 pci_write_config32(dev, PCI_COMMAND, reg32 | PCI_COMMAND_MASTER);
301
302 pci_write_config8(dev, 0x3c, 0x0a); // unused?
303
304 /* Codec Initialization Programming Sequence */
Stefan Reinauer15ba2bc2012-11-14 12:25:15 -0800305
306 /* Take controller out of reset */
Stefan Reinauer8e073822012-04-04 00:07:22 +0200307 reg32 = read32(base + 0x08);
308 reg32 |= (1 << 0);
309 write32(base + 0x08, reg32);
Stefan Reinauer15ba2bc2012-11-14 12:25:15 -0800310 /* Wait 1ms */
311 udelay(1000);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200312
313 //
314 reg8 = pci_read_config8(dev, 0x40); // Audio Control
315 reg8 |= 1; // Select Azalia mode. This needs to be controlled via devicetree.cb
316 pci_write_config8(dev, 0x40, reg8);
317
318 reg8 = pci_read_config8(dev, 0x4d); // Docking Status
319 reg8 &= ~(1 << 7); // Docking not supported
320 pci_write_config8(dev, 0x4d, reg8);
321
322 codec_mask = codec_detect(base);
323
324 if (codec_mask) {
325 printk(BIOS_DEBUG, "Azalia: codec_mask = %02x\n", codec_mask);
326 codecs_init(dev, base, codec_mask);
327 }
328
329 /* Enable dynamic clock gating */
330 reg8 = pci_read_config8(dev, 0x43);
331 reg8 &= ~0x7;
332 reg8 |= (1 << 2) | (1 << 0);
333 pci_write_config8(dev, 0x43, reg8);
334}
335
Aaron Durbinaa090cb2017-09-13 16:01:52 -0600336static const char *azalia_acpi_name(const struct device *dev)
Patrick Rudolph604f6982017-06-07 09:46:52 +0200337{
338 return "HDEF";
339}
340
Stefan Reinauer8e073822012-04-04 00:07:22 +0200341static struct pci_operations azalia_pci_ops = {
Subrata Banik4a0f0712019-03-20 14:29:47 +0530342 .set_subsystem = pci_dev_set_subsystem,
Stefan Reinauer8e073822012-04-04 00:07:22 +0200343};
344
345static struct device_operations azalia_ops = {
346 .read_resources = pci_dev_read_resources,
347 .set_resources = pci_dev_set_resources,
348 .enable_resources = pci_dev_enable_resources,
349 .init = azalia_init,
350 .scan_bus = 0,
351 .ops_pci = &azalia_pci_ops,
Patrick Rudolph604f6982017-06-07 09:46:52 +0200352 .acpi_name = azalia_acpi_name,
Stefan Reinauer8e073822012-04-04 00:07:22 +0200353};
354
Stefan Reinauer9a380ab2012-06-22 13:16:11 -0700355static const unsigned short pci_device_ids[] = { 0x1c20, 0x1e20, 0 };
Stefan Reinauer8e073822012-04-04 00:07:22 +0200356
Stefan Reinauer9a380ab2012-06-22 13:16:11 -0700357static const struct pci_driver pch_azalia __pci_driver = {
358 .ops = &azalia_ops,
359 .vendor = PCI_VENDOR_ID_INTEL,
360 .devices = pci_device_ids,
Stefan Reinauer8e073822012-04-04 00:07:22 +0200361};