blob: fe5cc2ea1d95bbfbe34a3f741ab2867aad9b5c97 [file] [log] [blame]
Vladimir Serbinenko888d5592013-11-13 17:53:38 +01001/*
2 * This file is part of the coreboot project.
3 *
Vladimir Serbinenko888d5592013-11-13 17:53:38 +01004 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; version 2 of the License.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
Vladimir Serbinenko888d5592013-11-13 17:53:38 +010013 */
14
15#include <console/console.h>
16#include <device/device.h>
17#include <device/pci.h>
18#include <device/pci_ids.h>
19#include <device/pci_ops.h>
Kyösti Mälkki13f66502019-03-03 08:01:05 +020020#include <device/mmio.h>
Vladimir Serbinenko888d5592013-11-13 17:53:38 +010021#include <delay.h>
Vladimir Serbinenko75c83872014-09-05 01:01:31 +020022#include <device/azalia_device.h>
Vladimir Serbinenko888d5592013-11-13 17:53:38 +010023#include "pch.h"
24
25#define HDA_ICII_REG 0x68
26#define HDA_ICII_BUSY (1 << 0)
27#define HDA_ICII_VALID (1 << 1)
28
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080029static int set_bits(void *port, u32 mask, u32 val)
Vladimir Serbinenko888d5592013-11-13 17:53:38 +010030{
31 u32 reg32;
32 int count;
33
34 /* Write (val & mask) to port */
35 val &= mask;
36 reg32 = read32(port);
37 reg32 &= ~mask;
38 reg32 |= val;
39 write32(port, reg32);
40
41 /* Wait for readback of register to
42 * match what was just written to it
43 */
44 count = 50;
45 do {
46 /* Wait 1ms based on BKDG wait time */
47 mdelay(1);
48 reg32 = read32(port);
49 reg32 &= mask;
50 } while ((reg32 != val) && --count);
51
52 /* Timeout occurred */
53 if (!count)
54 return -1;
55 return 0;
56}
57
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080058static int codec_detect(u8 *base)
Vladimir Serbinenko888d5592013-11-13 17:53:38 +010059{
60 u8 reg8;
61
62 /* Set Bit 0 to 1 to exit reset state (BAR + 0x8)[0] */
63 if (set_bits(base + 0x08, 1, 1) == -1)
64 goto no_codec;
65
66 /* Write back the value once reset bit is set. */
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080067 write16(base + 0x0,
68 read16(base + 0x0));
Vladimir Serbinenko888d5592013-11-13 17:53:38 +010069
70 /* Read in Codec location (BAR + 0xe)[2..0]*/
71 reg8 = read8(base + 0xe);
72 reg8 &= 0x0f;
73 if (!reg8)
74 goto no_codec;
75
76 return reg8;
77
78no_codec:
79 /* Codec Not found */
80 /* Put HDA back in reset (BAR + 0x8) [0] */
81 set_bits(base + 0x08, 1, 0);
82 printk(BIOS_DEBUG, "Azalia: No codec!\n");
83 return 0;
84}
85
Elyes HAOUASe414a4e2019-01-03 10:40:43 +010086static u32 find_verb(struct device *dev, u32 viddid, const u32 **verb)
Vladimir Serbinenko888d5592013-11-13 17:53:38 +010087{
88 int idx=0;
89
90 while (idx < (cim_verb_data_size / sizeof(u32))) {
91 u32 verb_size = 4 * cim_verb_data[idx+2]; // in u32
92 if (cim_verb_data[idx] != viddid) {
93 idx += verb_size + 3; // skip verb + header
94 continue;
95 }
96 *verb = &cim_verb_data[idx+3];
97 return verb_size;
98 }
99
100 /* Not all codecs need to load another verb */
101 return 0;
102}
103
104/**
105 * Wait 50usec for the codec to indicate it is ready
106 * no response would imply that the codec is non-operative
107 */
108
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800109static int wait_for_ready(u8 *base)
Vladimir Serbinenko888d5592013-11-13 17:53:38 +0100110{
111 /* Use a 1msec timeout */
112
113 int timeout = 1000;
114
Elyes HAOUASba28e8d2016-08-31 19:22:16 +0200115 while (timeout--) {
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800116 u32 reg32 = read32(base + HDA_ICII_REG);
Vladimir Serbinenko888d5592013-11-13 17:53:38 +0100117 if (!(reg32 & HDA_ICII_BUSY))
118 return 0;
119 udelay(1);
120 }
121
122 return -1;
123}
124
125/**
126 * Wait 50usec for the codec to indicate that it accepted
127 * the previous command. No response would imply that the code
128 * is non-operative
129 */
130
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800131static int wait_for_valid(u8 *base)
Vladimir Serbinenko888d5592013-11-13 17:53:38 +0100132{
133 u32 reg32;
134
135 /* Send the verb to the codec */
136 reg32 = read32(base + HDA_ICII_REG);
137 reg32 |= HDA_ICII_BUSY | HDA_ICII_VALID;
138 write32(base + HDA_ICII_REG, reg32);
139
140 /* Use a 1msec timeout */
141
142 int timeout = 1000;
Elyes HAOUASba28e8d2016-08-31 19:22:16 +0200143 while (timeout--) {
Vladimir Serbinenko888d5592013-11-13 17:53:38 +0100144 reg32 = read32(base + HDA_ICII_REG);
145 if ((reg32 & (HDA_ICII_VALID | HDA_ICII_BUSY)) ==
146 HDA_ICII_VALID)
147 return 0;
148 udelay(1);
149 }
150
151 return -1;
152}
153
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800154static void codec_init(struct device *dev, u8 *base, int addr)
Vladimir Serbinenko888d5592013-11-13 17:53:38 +0100155{
156 u32 reg32;
157 const u32 *verb;
158 u32 verb_size;
159 int i;
160
161 printk(BIOS_DEBUG, "Azalia: Initializing codec #%d\n", addr);
162
163 /* 1 */
164 if (wait_for_ready(base) == -1) {
165 printk(BIOS_DEBUG, " codec not ready.\n");
166 return;
167 }
168
169 reg32 = (addr << 28) | 0x000f0000;
170 write32(base + 0x60, reg32);
171
172 if (wait_for_valid(base) == -1) {
173 printk(BIOS_DEBUG, " codec not valid.\n");
174 return;
175 }
176
177 reg32 = read32(base + 0x64);
178
179 /* 2 */
180 printk(BIOS_DEBUG, "Azalia: codec viddid: %08x\n", reg32);
181 verb_size = find_verb(dev, reg32, &verb);
182
183 if (!verb_size) {
184 printk(BIOS_DEBUG, "Azalia: No verb!\n");
185 return;
186 }
187 printk(BIOS_DEBUG, "Azalia: verb_size: %d\n", verb_size);
188
189 /* 3 */
190 for (i = 0; i < verb_size; i++) {
191 if (wait_for_ready(base) == -1)
192 return;
193
194 write32(base + 0x60, verb[i]);
195
196 if (wait_for_valid(base) == -1)
197 return;
198 }
199 printk(BIOS_DEBUG, "Azalia: verb loaded.\n");
200}
201
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800202static void codecs_init(struct device *dev, u8 *base, u32 codec_mask)
Vladimir Serbinenko888d5592013-11-13 17:53:38 +0100203{
204 int i;
205 for (i = 3; i >= 0; i--) {
206 if (codec_mask & (1 << i))
207 codec_init(dev, base, i);
208 }
209
210 for (i = 0; i < pc_beep_verbs_size; i++) {
211 if (wait_for_ready(base) == -1)
212 return;
213
214 write32(base + 0x60, pc_beep_verbs[i]);
215
216 if (wait_for_valid(base) == -1)
217 return;
218 }
219}
220
221static void azalia_init(struct device *dev)
222{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800223 u8 *base;
Vladimir Serbinenko888d5592013-11-13 17:53:38 +0100224 struct resource *res;
225 u32 codec_mask;
226 u8 reg8;
227 u16 reg16;
228 u32 reg32;
229
230 /* Find base address */
231 res = find_resource(dev, PCI_BASE_ADDRESS_0);
232 if (!res)
233 return;
234
235 // NOTE this will break as soon as the Azalia get's a bar above
236 // 4G. Is there anything we can do about it?
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800237 base = res2mmio(res, 0, 0);
Vladimir Serbinenko888d5592013-11-13 17:53:38 +0100238 printk(BIOS_DEBUG, "Azalia: base = %08x\n", (u32)base);
239
240 if (RCBA32(0x2030) & (1 << 31)) {
241 reg32 = pci_read_config32(dev, 0x120);
242 reg32 &= 0xf8ffff01;
243 reg32 |= (1 << 24); // 2 << 24 for server
244 reg32 |= RCBA32(0x2030) & 0xfe;
245 pci_write_config32(dev, 0x120, reg32);
246
247 reg16 = pci_read_config16(dev, 0x78);
248 reg16 |= (1 << 11);
249 pci_write_config16(dev, 0x78, reg16);
250 } else
251 printk(BIOS_DEBUG, "Azalia: V1CTL disabled.\n");
252
253 reg32 = pci_read_config32(dev, 0x114);
254 reg32 &= ~0xfe;
255 pci_write_config32(dev, 0x114, reg32);
256
257 // Set VCi enable bit
258 reg32 = pci_read_config32(dev, 0x120);
259 reg32 |= (1 << 31);
260 pci_write_config32(dev, 0x120, reg32);
261
262 // Enable HDMI codec:
263 reg32 = pci_read_config32(dev, 0xc4);
264 reg32 |= (1 << 1);
265 pci_write_config32(dev, 0xc4, reg32);
266
267 reg8 = pci_read_config8(dev, 0x43);
268 reg8 |= (1 << 6);
269 pci_write_config8(dev, 0x43, reg8);
270
Vladimir Serbinenko888d5592013-11-13 17:53:38 +0100271 reg32 = pci_read_config32(dev, 0xd0);
272 reg32 &= ~(1 << 31);
273 pci_write_config32(dev, 0xd0, reg32);
274
Vladimir Serbinenko888d5592013-11-13 17:53:38 +0100275 /* Set Bus Master */
276 reg32 = pci_read_config32(dev, PCI_COMMAND);
277 pci_write_config32(dev, PCI_COMMAND, reg32 | PCI_COMMAND_MASTER);
278
279 pci_write_config8(dev, 0x3c, 0x0a); // unused?
280
281 /* Codec Initialization Programming Sequence */
282
283 /* Take controller out of reset */
284 reg32 = read32(base + 0x08);
285 reg32 |= (1 << 0);
286 write32(base + 0x08, reg32);
287 /* Wait 1ms */
288 udelay(1000);
289
290 //
291 reg8 = pci_read_config8(dev, 0x40); // Audio Control
292 reg8 |= 1; // Select Azalia mode. This needs to be controlled via devicetree.cb
293 pci_write_config8(dev, 0x40, reg8);
294
295 reg8 = pci_read_config8(dev, 0x4d); // Docking Status
296 reg8 &= ~(1 << 7); // Docking not supported
297 pci_write_config8(dev, 0x4d, reg8);
298
299 codec_mask = codec_detect(base);
300
301 if (codec_mask) {
302 printk(BIOS_DEBUG, "Azalia: codec_mask = %02x\n", codec_mask);
303 codecs_init(dev, base, codec_mask);
304 }
305
306 /* Enable dynamic clock gating */
307 reg8 = pci_read_config8(dev, 0x43);
308 reg8 &= ~0x7;
309 reg8 |= (1 << 2) | (1 << 0);
310 pci_write_config8(dev, 0x43, reg8);
311}
312
Vladimir Serbinenko888d5592013-11-13 17:53:38 +0100313static struct pci_operations azalia_pci_ops = {
Subrata Banik4a0f0712019-03-20 14:29:47 +0530314 .set_subsystem = pci_dev_set_subsystem,
Vladimir Serbinenko888d5592013-11-13 17:53:38 +0100315};
316
317static struct device_operations azalia_ops = {
318 .read_resources = pci_dev_read_resources,
319 .set_resources = pci_dev_set_resources,
320 .enable_resources = pci_dev_enable_resources,
321 .init = azalia_init,
322 .scan_bus = 0,
323 .ops_pci = &azalia_pci_ops,
324};
325
Felix Singer838fbc72019-11-21 21:23:32 +0100326static const unsigned short pci_device_ids[] = {
327 0x1c20,
328 0x1e20,
329 PCI_DID_INTEL_IBEXPEAK_AUDIO,
330 0
331};
Vladimir Serbinenko888d5592013-11-13 17:53:38 +0100332
333static const struct pci_driver pch_azalia __pci_driver = {
334 .ops = &azalia_ops,
335 .vendor = PCI_VENDOR_ID_INTEL,
336 .devices = pci_device_ids,
337};