blob: 47d595c2a1428dad1d039275a1d7ff21d720035c [file] [log] [blame]
Angel Pons182dbde2020-04-02 23:49:05 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Vladimir Serbinenko888d5592013-11-13 17:53:38 +01002
3#include <console/console.h>
4#include <device/device.h>
5#include <device/pci.h>
6#include <device/pci_ids.h>
7#include <device/pci_ops.h>
Kyösti Mälkki13f66502019-03-03 08:01:05 +02008#include <device/mmio.h>
Vladimir Serbinenko888d5592013-11-13 17:53:38 +01009#include <delay.h>
Vladimir Serbinenko75c83872014-09-05 01:01:31 +020010#include <device/azalia_device.h>
Vladimir Serbinenko888d5592013-11-13 17:53:38 +010011#include "pch.h"
12
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080013static int set_bits(void *port, u32 mask, u32 val)
Vladimir Serbinenko888d5592013-11-13 17:53:38 +010014{
15 u32 reg32;
16 int count;
17
18 /* Write (val & mask) to port */
19 val &= mask;
20 reg32 = read32(port);
21 reg32 &= ~mask;
22 reg32 |= val;
23 write32(port, reg32);
24
25 /* Wait for readback of register to
26 * match what was just written to it
27 */
28 count = 50;
29 do {
30 /* Wait 1ms based on BKDG wait time */
31 mdelay(1);
32 reg32 = read32(port);
33 reg32 &= mask;
34 } while ((reg32 != val) && --count);
35
36 /* Timeout occurred */
37 if (!count)
38 return -1;
39 return 0;
40}
41
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080042static int codec_detect(u8 *base)
Vladimir Serbinenko888d5592013-11-13 17:53:38 +010043{
44 u8 reg8;
45
46 /* Set Bit 0 to 1 to exit reset state (BAR + 0x8)[0] */
Elyes HAOUAS59236d52020-08-03 15:36:52 +020047 if (set_bits(base + HDA_GCTL_REG, 1, 1) == -1)
Vladimir Serbinenko888d5592013-11-13 17:53:38 +010048 goto no_codec;
49
50 /* Write back the value once reset bit is set. */
Elyes HAOUAS59236d52020-08-03 15:36:52 +020051 write16(base + HDA_GCAP_REG,
52 read16(base + HDA_GCAP_REG));
Vladimir Serbinenko888d5592013-11-13 17:53:38 +010053
54 /* Read in Codec location (BAR + 0xe)[2..0]*/
Elyes HAOUAS59236d52020-08-03 15:36:52 +020055 reg8 = read8(base + HDA_STATESTS_REG);
Vladimir Serbinenko888d5592013-11-13 17:53:38 +010056 reg8 &= 0x0f;
57 if (!reg8)
58 goto no_codec;
59
60 return reg8;
61
62no_codec:
63 /* Codec Not found */
64 /* Put HDA back in reset (BAR + 0x8) [0] */
Elyes HAOUAS59236d52020-08-03 15:36:52 +020065 set_bits(base + HDA_GCTL_REG, 1, 0);
Vladimir Serbinenko888d5592013-11-13 17:53:38 +010066 printk(BIOS_DEBUG, "Azalia: No codec!\n");
67 return 0;
68}
69
Elyes HAOUASe414a4e2019-01-03 10:40:43 +010070static u32 find_verb(struct device *dev, u32 viddid, const u32 **verb)
Vladimir Serbinenko888d5592013-11-13 17:53:38 +010071{
72 int idx=0;
73
74 while (idx < (cim_verb_data_size / sizeof(u32))) {
75 u32 verb_size = 4 * cim_verb_data[idx+2]; // in u32
76 if (cim_verb_data[idx] != viddid) {
77 idx += verb_size + 3; // skip verb + header
78 continue;
79 }
80 *verb = &cim_verb_data[idx+3];
81 return verb_size;
82 }
83
84 /* Not all codecs need to load another verb */
85 return 0;
86}
87
88/**
89 * Wait 50usec for the codec to indicate it is ready
90 * no response would imply that the codec is non-operative
91 */
92
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080093static int wait_for_ready(u8 *base)
Vladimir Serbinenko888d5592013-11-13 17:53:38 +010094{
95 /* Use a 1msec timeout */
96
97 int timeout = 1000;
98
Elyes HAOUASba28e8d2016-08-31 19:22:16 +020099 while (timeout--) {
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800100 u32 reg32 = read32(base + HDA_ICII_REG);
Vladimir Serbinenko888d5592013-11-13 17:53:38 +0100101 if (!(reg32 & HDA_ICII_BUSY))
102 return 0;
103 udelay(1);
104 }
105
106 return -1;
107}
108
109/**
110 * Wait 50usec for the codec to indicate that it accepted
111 * the previous command. No response would imply that the code
112 * is non-operative
113 */
114
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800115static int wait_for_valid(u8 *base)
Vladimir Serbinenko888d5592013-11-13 17:53:38 +0100116{
117 u32 reg32;
118
119 /* Send the verb to the codec */
120 reg32 = read32(base + HDA_ICII_REG);
121 reg32 |= HDA_ICII_BUSY | HDA_ICII_VALID;
122 write32(base + HDA_ICII_REG, reg32);
123
124 /* Use a 1msec timeout */
125
126 int timeout = 1000;
Elyes HAOUASba28e8d2016-08-31 19:22:16 +0200127 while (timeout--) {
Vladimir Serbinenko888d5592013-11-13 17:53:38 +0100128 reg32 = read32(base + HDA_ICII_REG);
129 if ((reg32 & (HDA_ICII_VALID | HDA_ICII_BUSY)) ==
130 HDA_ICII_VALID)
131 return 0;
132 udelay(1);
133 }
134
135 return -1;
136}
137
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800138static void codec_init(struct device *dev, u8 *base, int addr)
Vladimir Serbinenko888d5592013-11-13 17:53:38 +0100139{
140 u32 reg32;
141 const u32 *verb;
142 u32 verb_size;
143 int i;
144
145 printk(BIOS_DEBUG, "Azalia: Initializing codec #%d\n", addr);
146
147 /* 1 */
148 if (wait_for_ready(base) == -1) {
149 printk(BIOS_DEBUG, " codec not ready.\n");
150 return;
151 }
152
153 reg32 = (addr << 28) | 0x000f0000;
Elyes HAOUAS59236d52020-08-03 15:36:52 +0200154 write32(base + HDA_IC_REG, reg32);
Vladimir Serbinenko888d5592013-11-13 17:53:38 +0100155
156 if (wait_for_valid(base) == -1) {
157 printk(BIOS_DEBUG, " codec not valid.\n");
158 return;
159 }
160
Elyes HAOUAS59236d52020-08-03 15:36:52 +0200161 reg32 = read32(base + HDA_IR_REG);
Vladimir Serbinenko888d5592013-11-13 17:53:38 +0100162
163 /* 2 */
164 printk(BIOS_DEBUG, "Azalia: codec viddid: %08x\n", reg32);
165 verb_size = find_verb(dev, reg32, &verb);
166
167 if (!verb_size) {
168 printk(BIOS_DEBUG, "Azalia: No verb!\n");
169 return;
170 }
171 printk(BIOS_DEBUG, "Azalia: verb_size: %d\n", verb_size);
172
173 /* 3 */
174 for (i = 0; i < verb_size; i++) {
175 if (wait_for_ready(base) == -1)
176 return;
177
Elyes HAOUAS59236d52020-08-03 15:36:52 +0200178 write32(base + HDA_IC_REG, verb[i]);
Vladimir Serbinenko888d5592013-11-13 17:53:38 +0100179
180 if (wait_for_valid(base) == -1)
181 return;
182 }
183 printk(BIOS_DEBUG, "Azalia: verb loaded.\n");
184}
185
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800186static void codecs_init(struct device *dev, u8 *base, u32 codec_mask)
Vladimir Serbinenko888d5592013-11-13 17:53:38 +0100187{
188 int i;
189 for (i = 3; i >= 0; i--) {
190 if (codec_mask & (1 << i))
191 codec_init(dev, base, i);
192 }
193
194 for (i = 0; i < pc_beep_verbs_size; i++) {
195 if (wait_for_ready(base) == -1)
196 return;
197
Elyes HAOUAS59236d52020-08-03 15:36:52 +0200198 write32(base + HDA_IC_REG, pc_beep_verbs[i]);
Vladimir Serbinenko888d5592013-11-13 17:53:38 +0100199
200 if (wait_for_valid(base) == -1)
201 return;
202 }
203}
204
205static void azalia_init(struct device *dev)
206{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800207 u8 *base;
Vladimir Serbinenko888d5592013-11-13 17:53:38 +0100208 struct resource *res;
209 u32 codec_mask;
210 u8 reg8;
211 u16 reg16;
212 u32 reg32;
213
214 /* Find base address */
215 res = find_resource(dev, PCI_BASE_ADDRESS_0);
216 if (!res)
217 return;
218
219 // NOTE this will break as soon as the Azalia get's a bar above
220 // 4G. Is there anything we can do about it?
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800221 base = res2mmio(res, 0, 0);
Vladimir Serbinenko888d5592013-11-13 17:53:38 +0100222 printk(BIOS_DEBUG, "Azalia: base = %08x\n", (u32)base);
223
224 if (RCBA32(0x2030) & (1 << 31)) {
225 reg32 = pci_read_config32(dev, 0x120);
226 reg32 &= 0xf8ffff01;
227 reg32 |= (1 << 24); // 2 << 24 for server
228 reg32 |= RCBA32(0x2030) & 0xfe;
229 pci_write_config32(dev, 0x120, reg32);
230
231 reg16 = pci_read_config16(dev, 0x78);
232 reg16 |= (1 << 11);
233 pci_write_config16(dev, 0x78, reg16);
234 } else
235 printk(BIOS_DEBUG, "Azalia: V1CTL disabled.\n");
236
237 reg32 = pci_read_config32(dev, 0x114);
238 reg32 &= ~0xfe;
239 pci_write_config32(dev, 0x114, reg32);
240
241 // Set VCi enable bit
242 reg32 = pci_read_config32(dev, 0x120);
243 reg32 |= (1 << 31);
244 pci_write_config32(dev, 0x120, reg32);
245
246 // Enable HDMI codec:
247 reg32 = pci_read_config32(dev, 0xc4);
248 reg32 |= (1 << 1);
249 pci_write_config32(dev, 0xc4, reg32);
250
251 reg8 = pci_read_config8(dev, 0x43);
252 reg8 |= (1 << 6);
253 pci_write_config8(dev, 0x43, reg8);
254
Vladimir Serbinenko888d5592013-11-13 17:53:38 +0100255 reg32 = pci_read_config32(dev, 0xd0);
256 reg32 &= ~(1 << 31);
257 pci_write_config32(dev, 0xd0, reg32);
258
Vladimir Serbinenko888d5592013-11-13 17:53:38 +0100259 /* Set Bus Master */
Elyes HAOUAS8b6dfde2020-04-28 09:58:21 +0200260 pci_or_config16(dev, PCI_COMMAND, PCI_COMMAND_MASTER);
Vladimir Serbinenko888d5592013-11-13 17:53:38 +0100261
262 pci_write_config8(dev, 0x3c, 0x0a); // unused?
263
264 /* Codec Initialization Programming Sequence */
265
266 /* Take controller out of reset */
Elyes HAOUAS59236d52020-08-03 15:36:52 +0200267 reg32 = read32(base + HDA_GCTL_REG);
268 reg32 |= HDA_GCTL_CRST;
269 write32(base + HDA_GCTL_REG, reg32);
Vladimir Serbinenko888d5592013-11-13 17:53:38 +0100270 /* Wait 1ms */
271 udelay(1000);
272
273 //
274 reg8 = pci_read_config8(dev, 0x40); // Audio Control
275 reg8 |= 1; // Select Azalia mode. This needs to be controlled via devicetree.cb
276 pci_write_config8(dev, 0x40, reg8);
277
278 reg8 = pci_read_config8(dev, 0x4d); // Docking Status
279 reg8 &= ~(1 << 7); // Docking not supported
280 pci_write_config8(dev, 0x4d, reg8);
281
282 codec_mask = codec_detect(base);
283
284 if (codec_mask) {
285 printk(BIOS_DEBUG, "Azalia: codec_mask = %02x\n", codec_mask);
286 codecs_init(dev, base, codec_mask);
287 }
288
289 /* Enable dynamic clock gating */
290 reg8 = pci_read_config8(dev, 0x43);
291 reg8 &= ~0x7;
292 reg8 |= (1 << 2) | (1 << 0);
293 pci_write_config8(dev, 0x43, reg8);
294}
295
Vladimir Serbinenko888d5592013-11-13 17:53:38 +0100296static struct device_operations azalia_ops = {
297 .read_resources = pci_dev_read_resources,
298 .set_resources = pci_dev_set_resources,
299 .enable_resources = pci_dev_enable_resources,
300 .init = azalia_init,
Angel Pons1fc0edd2020-05-31 00:03:28 +0200301 .ops_pci = &pci_dev_ops_pci,
Vladimir Serbinenko888d5592013-11-13 17:53:38 +0100302};
303
Felix Singer838fbc72019-11-21 21:23:32 +0100304static const unsigned short pci_device_ids[] = {
305 0x1c20,
306 0x1e20,
307 PCI_DID_INTEL_IBEXPEAK_AUDIO,
308 0
309};
Vladimir Serbinenko888d5592013-11-13 17:53:38 +0100310
311static const struct pci_driver pch_azalia __pci_driver = {
312 .ops = &azalia_ops,
313 .vendor = PCI_VENDOR_ID_INTEL,
314 .devices = pci_device_ids,
315};