blob: eb75b1200dd8b4bf60b041b92cb987a86474a40f [file] [log] [blame]
Angel Pons182dbde2020-04-02 23:49:05 +02001/* SPDX-License-Identifier: GPL-2.0-only */
2/* This file is part of the coreboot project. */
Vladimir Serbinenko888d5592013-11-13 17:53:38 +01003
4#include <console/console.h>
5#include <device/device.h>
6#include <device/pci.h>
7#include <device/pci_ids.h>
8#include <device/pci_ops.h>
Kyösti Mälkki13f66502019-03-03 08:01:05 +02009#include <device/mmio.h>
Vladimir Serbinenko888d5592013-11-13 17:53:38 +010010#include <delay.h>
Vladimir Serbinenko75c83872014-09-05 01:01:31 +020011#include <device/azalia_device.h>
Vladimir Serbinenko888d5592013-11-13 17:53:38 +010012#include "pch.h"
13
14#define HDA_ICII_REG 0x68
15#define HDA_ICII_BUSY (1 << 0)
16#define HDA_ICII_VALID (1 << 1)
17
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080018static int set_bits(void *port, u32 mask, u32 val)
Vladimir Serbinenko888d5592013-11-13 17:53:38 +010019{
20 u32 reg32;
21 int count;
22
23 /* Write (val & mask) to port */
24 val &= mask;
25 reg32 = read32(port);
26 reg32 &= ~mask;
27 reg32 |= val;
28 write32(port, reg32);
29
30 /* Wait for readback of register to
31 * match what was just written to it
32 */
33 count = 50;
34 do {
35 /* Wait 1ms based on BKDG wait time */
36 mdelay(1);
37 reg32 = read32(port);
38 reg32 &= mask;
39 } while ((reg32 != val) && --count);
40
41 /* Timeout occurred */
42 if (!count)
43 return -1;
44 return 0;
45}
46
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080047static int codec_detect(u8 *base)
Vladimir Serbinenko888d5592013-11-13 17:53:38 +010048{
49 u8 reg8;
50
51 /* Set Bit 0 to 1 to exit reset state (BAR + 0x8)[0] */
52 if (set_bits(base + 0x08, 1, 1) == -1)
53 goto no_codec;
54
55 /* Write back the value once reset bit is set. */
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080056 write16(base + 0x0,
57 read16(base + 0x0));
Vladimir Serbinenko888d5592013-11-13 17:53:38 +010058
59 /* Read in Codec location (BAR + 0xe)[2..0]*/
60 reg8 = read8(base + 0xe);
61 reg8 &= 0x0f;
62 if (!reg8)
63 goto no_codec;
64
65 return reg8;
66
67no_codec:
68 /* Codec Not found */
69 /* Put HDA back in reset (BAR + 0x8) [0] */
70 set_bits(base + 0x08, 1, 0);
71 printk(BIOS_DEBUG, "Azalia: No codec!\n");
72 return 0;
73}
74
Elyes HAOUASe414a4e2019-01-03 10:40:43 +010075static u32 find_verb(struct device *dev, u32 viddid, const u32 **verb)
Vladimir Serbinenko888d5592013-11-13 17:53:38 +010076{
77 int idx=0;
78
79 while (idx < (cim_verb_data_size / sizeof(u32))) {
80 u32 verb_size = 4 * cim_verb_data[idx+2]; // in u32
81 if (cim_verb_data[idx] != viddid) {
82 idx += verb_size + 3; // skip verb + header
83 continue;
84 }
85 *verb = &cim_verb_data[idx+3];
86 return verb_size;
87 }
88
89 /* Not all codecs need to load another verb */
90 return 0;
91}
92
93/**
94 * Wait 50usec for the codec to indicate it is ready
95 * no response would imply that the codec is non-operative
96 */
97
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080098static int wait_for_ready(u8 *base)
Vladimir Serbinenko888d5592013-11-13 17:53:38 +010099{
100 /* Use a 1msec timeout */
101
102 int timeout = 1000;
103
Elyes HAOUASba28e8d2016-08-31 19:22:16 +0200104 while (timeout--) {
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800105 u32 reg32 = read32(base + HDA_ICII_REG);
Vladimir Serbinenko888d5592013-11-13 17:53:38 +0100106 if (!(reg32 & HDA_ICII_BUSY))
107 return 0;
108 udelay(1);
109 }
110
111 return -1;
112}
113
114/**
115 * Wait 50usec for the codec to indicate that it accepted
116 * the previous command. No response would imply that the code
117 * is non-operative
118 */
119
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800120static int wait_for_valid(u8 *base)
Vladimir Serbinenko888d5592013-11-13 17:53:38 +0100121{
122 u32 reg32;
123
124 /* Send the verb to the codec */
125 reg32 = read32(base + HDA_ICII_REG);
126 reg32 |= HDA_ICII_BUSY | HDA_ICII_VALID;
127 write32(base + HDA_ICII_REG, reg32);
128
129 /* Use a 1msec timeout */
130
131 int timeout = 1000;
Elyes HAOUASba28e8d2016-08-31 19:22:16 +0200132 while (timeout--) {
Vladimir Serbinenko888d5592013-11-13 17:53:38 +0100133 reg32 = read32(base + HDA_ICII_REG);
134 if ((reg32 & (HDA_ICII_VALID | HDA_ICII_BUSY)) ==
135 HDA_ICII_VALID)
136 return 0;
137 udelay(1);
138 }
139
140 return -1;
141}
142
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800143static void codec_init(struct device *dev, u8 *base, int addr)
Vladimir Serbinenko888d5592013-11-13 17:53:38 +0100144{
145 u32 reg32;
146 const u32 *verb;
147 u32 verb_size;
148 int i;
149
150 printk(BIOS_DEBUG, "Azalia: Initializing codec #%d\n", addr);
151
152 /* 1 */
153 if (wait_for_ready(base) == -1) {
154 printk(BIOS_DEBUG, " codec not ready.\n");
155 return;
156 }
157
158 reg32 = (addr << 28) | 0x000f0000;
159 write32(base + 0x60, reg32);
160
161 if (wait_for_valid(base) == -1) {
162 printk(BIOS_DEBUG, " codec not valid.\n");
163 return;
164 }
165
166 reg32 = read32(base + 0x64);
167
168 /* 2 */
169 printk(BIOS_DEBUG, "Azalia: codec viddid: %08x\n", reg32);
170 verb_size = find_verb(dev, reg32, &verb);
171
172 if (!verb_size) {
173 printk(BIOS_DEBUG, "Azalia: No verb!\n");
174 return;
175 }
176 printk(BIOS_DEBUG, "Azalia: verb_size: %d\n", verb_size);
177
178 /* 3 */
179 for (i = 0; i < verb_size; i++) {
180 if (wait_for_ready(base) == -1)
181 return;
182
183 write32(base + 0x60, verb[i]);
184
185 if (wait_for_valid(base) == -1)
186 return;
187 }
188 printk(BIOS_DEBUG, "Azalia: verb loaded.\n");
189}
190
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800191static void codecs_init(struct device *dev, u8 *base, u32 codec_mask)
Vladimir Serbinenko888d5592013-11-13 17:53:38 +0100192{
193 int i;
194 for (i = 3; i >= 0; i--) {
195 if (codec_mask & (1 << i))
196 codec_init(dev, base, i);
197 }
198
199 for (i = 0; i < pc_beep_verbs_size; i++) {
200 if (wait_for_ready(base) == -1)
201 return;
202
203 write32(base + 0x60, pc_beep_verbs[i]);
204
205 if (wait_for_valid(base) == -1)
206 return;
207 }
208}
209
210static void azalia_init(struct device *dev)
211{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800212 u8 *base;
Vladimir Serbinenko888d5592013-11-13 17:53:38 +0100213 struct resource *res;
214 u32 codec_mask;
215 u8 reg8;
216 u16 reg16;
217 u32 reg32;
218
219 /* Find base address */
220 res = find_resource(dev, PCI_BASE_ADDRESS_0);
221 if (!res)
222 return;
223
224 // NOTE this will break as soon as the Azalia get's a bar above
225 // 4G. Is there anything we can do about it?
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800226 base = res2mmio(res, 0, 0);
Vladimir Serbinenko888d5592013-11-13 17:53:38 +0100227 printk(BIOS_DEBUG, "Azalia: base = %08x\n", (u32)base);
228
229 if (RCBA32(0x2030) & (1 << 31)) {
230 reg32 = pci_read_config32(dev, 0x120);
231 reg32 &= 0xf8ffff01;
232 reg32 |= (1 << 24); // 2 << 24 for server
233 reg32 |= RCBA32(0x2030) & 0xfe;
234 pci_write_config32(dev, 0x120, reg32);
235
236 reg16 = pci_read_config16(dev, 0x78);
237 reg16 |= (1 << 11);
238 pci_write_config16(dev, 0x78, reg16);
239 } else
240 printk(BIOS_DEBUG, "Azalia: V1CTL disabled.\n");
241
242 reg32 = pci_read_config32(dev, 0x114);
243 reg32 &= ~0xfe;
244 pci_write_config32(dev, 0x114, reg32);
245
246 // Set VCi enable bit
247 reg32 = pci_read_config32(dev, 0x120);
248 reg32 |= (1 << 31);
249 pci_write_config32(dev, 0x120, reg32);
250
251 // Enable HDMI codec:
252 reg32 = pci_read_config32(dev, 0xc4);
253 reg32 |= (1 << 1);
254 pci_write_config32(dev, 0xc4, reg32);
255
256 reg8 = pci_read_config8(dev, 0x43);
257 reg8 |= (1 << 6);
258 pci_write_config8(dev, 0x43, reg8);
259
Vladimir Serbinenko888d5592013-11-13 17:53:38 +0100260 reg32 = pci_read_config32(dev, 0xd0);
261 reg32 &= ~(1 << 31);
262 pci_write_config32(dev, 0xd0, reg32);
263
Vladimir Serbinenko888d5592013-11-13 17:53:38 +0100264 /* Set Bus Master */
265 reg32 = pci_read_config32(dev, PCI_COMMAND);
266 pci_write_config32(dev, PCI_COMMAND, reg32 | PCI_COMMAND_MASTER);
267
268 pci_write_config8(dev, 0x3c, 0x0a); // unused?
269
270 /* Codec Initialization Programming Sequence */
271
272 /* Take controller out of reset */
273 reg32 = read32(base + 0x08);
274 reg32 |= (1 << 0);
275 write32(base + 0x08, reg32);
276 /* Wait 1ms */
277 udelay(1000);
278
279 //
280 reg8 = pci_read_config8(dev, 0x40); // Audio Control
281 reg8 |= 1; // Select Azalia mode. This needs to be controlled via devicetree.cb
282 pci_write_config8(dev, 0x40, reg8);
283
284 reg8 = pci_read_config8(dev, 0x4d); // Docking Status
285 reg8 &= ~(1 << 7); // Docking not supported
286 pci_write_config8(dev, 0x4d, reg8);
287
288 codec_mask = codec_detect(base);
289
290 if (codec_mask) {
291 printk(BIOS_DEBUG, "Azalia: codec_mask = %02x\n", codec_mask);
292 codecs_init(dev, base, codec_mask);
293 }
294
295 /* Enable dynamic clock gating */
296 reg8 = pci_read_config8(dev, 0x43);
297 reg8 &= ~0x7;
298 reg8 |= (1 << 2) | (1 << 0);
299 pci_write_config8(dev, 0x43, reg8);
300}
301
Vladimir Serbinenko888d5592013-11-13 17:53:38 +0100302static struct pci_operations azalia_pci_ops = {
Subrata Banik4a0f0712019-03-20 14:29:47 +0530303 .set_subsystem = pci_dev_set_subsystem,
Vladimir Serbinenko888d5592013-11-13 17:53:38 +0100304};
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,
Vladimir Serbinenko888d5592013-11-13 17:53:38 +0100311 .ops_pci = &azalia_pci_ops,
312};
313
Felix Singer838fbc72019-11-21 21:23:32 +0100314static const unsigned short pci_device_ids[] = {
315 0x1c20,
316 0x1e20,
317 PCI_DID_INTEL_IBEXPEAK_AUDIO,
318 0
319};
Vladimir Serbinenko888d5592013-11-13 17:53:38 +0100320
321static const struct pci_driver pch_azalia __pci_driver = {
322 .ops = &azalia_ops,
323 .vendor = PCI_VENDOR_ID_INTEL,
324 .devices = pci_device_ids,
325};