blob: e46c27f7b03f1257aff4f5c2206c85fe7463e812 [file] [log] [blame]
Angel Ponsc74dae92020-04-02 23:48:16 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Andrew Wub7bb70d2013-08-12 20:07:47 +08002
3#include <console/console.h>
4#include <device/device.h>
5#include <device/pci.h>
Andrew Wub7bb70d2013-08-12 20:07:47 +08006#include <device/azalia_device.h>
Kyösti Mälkki13f66502019-03-03 08:01:05 +02007#include <device/mmio.h>
Andrew Wub7bb70d2013-08-12 20:07:47 +08008#include <delay.h>
9
Angel Pons61dd8362020-12-05 18:02:32 +010010int azalia_set_bits(void *port, u32 mask, u32 val)
Andrew Wub7bb70d2013-08-12 20:07:47 +080011{
12 u32 reg32;
13 int count;
14
15 /* Write (val & mask) to port */
16 val &= mask;
17 reg32 = read32(port);
18 reg32 &= ~mask;
19 reg32 |= val;
20 write32(port, reg32);
21
Elyes HAOUAS6ea24ff2020-08-11 09:21:24 +020022 /* Wait for readback of register to match what was just written to it */
Andrew Wub7bb70d2013-08-12 20:07:47 +080023 count = 50;
24 do {
25 /* Wait 1ms based on BKDG wait time */
26 mdelay(1);
27 reg32 = read32(port);
28 reg32 &= mask;
29 } while ((reg32 != val) && --count);
30
31 /* Timeout occurred */
32 if (!count)
33 return -1;
34 return 0;
35}
36
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080037static int codec_detect(u8 *base)
Andrew Wub7bb70d2013-08-12 20:07:47 +080038{
39 u32 reg32;
40 int count;
41
42 /* Set Bit 0 to 1 to exit reset state (BAR + 0x8)[0] */
Angel Pons61dd8362020-12-05 18:02:32 +010043 if (azalia_set_bits(base + HDA_GCTL_REG, 1, HDA_GCTL_CRST) < 0)
Andrew Wub7bb70d2013-08-12 20:07:47 +080044 goto no_codec;
45
Elyes HAOUAS6ea24ff2020-08-11 09:21:24 +020046 /* clear STATESTS bits (BAR + 0xe)[2:0] */
Elyes HAOUASd8c47992020-08-03 15:31:39 +020047 reg32 = read32(base + HDA_STATESTS_REG);
Andrew Wub7bb70d2013-08-12 20:07:47 +080048 reg32 |= 7;
Elyes HAOUASd8c47992020-08-03 15:31:39 +020049 write32(base + HDA_STATESTS_REG, reg32);
Andrew Wub7bb70d2013-08-12 20:07:47 +080050
51 /* Wait for readback of register to
52 * match what was just written to it
53 */
54 count = 50;
55 do {
56 /* Wait 1ms based on BKDG wait time */
57 mdelay(1);
Elyes HAOUASd8c47992020-08-03 15:31:39 +020058 reg32 = read32(base + HDA_STATESTS_REG);
Andrew Wub7bb70d2013-08-12 20:07:47 +080059 } while ((reg32 != 0) && --count);
Martin Roth2ed0aa22016-01-05 20:58:58 -070060 /* Timeout occurred */
Andrew Wub7bb70d2013-08-12 20:07:47 +080061 if (!count)
62 goto no_codec;
63
Elyes HAOUAS6ea24ff2020-08-11 09:21:24 +020064 /* Set Bit 0 to 0 to enter reset state (BAR + 0x8)[0] */
Angel Pons61dd8362020-12-05 18:02:32 +010065 if (azalia_set_bits(base + HDA_GCTL_REG, 1, 0) < 0)
Andrew Wub7bb70d2013-08-12 20:07:47 +080066 goto no_codec;
67
68 /* Set Bit 0 to 1 to exit reset state (BAR + 0x8)[0] */
Angel Pons61dd8362020-12-05 18:02:32 +010069 if (azalia_set_bits(base + HDA_GCTL_REG, 1, HDA_GCTL_CRST) < 0)
Andrew Wub7bb70d2013-08-12 20:07:47 +080070 goto no_codec;
71
72 /* Read in Codec location (BAR + 0xe)[2..0] */
Elyes HAOUASd8c47992020-08-03 15:31:39 +020073 reg32 = read32(base + HDA_STATESTS_REG);
Andrew Wub7bb70d2013-08-12 20:07:47 +080074 reg32 &= 0x0f;
75 if (!reg32)
76 goto no_codec;
77
78 return reg32;
79
80no_codec:
81 /* Codec Not found */
82 /* Put HDA back in reset (BAR + 0x8) [0] */
Angel Pons61dd8362020-12-05 18:02:32 +010083 azalia_set_bits(base + HDA_GCTL_REG, 1, 0);
Andrew Wub7bb70d2013-08-12 20:07:47 +080084 printk(BIOS_DEBUG, "azalia_audio: No codec!\n");
85 return 0;
86}
87
Angel Ponsd3f70282020-12-05 18:28:33 +010088/*
89 * Find a specific entry within a verb table
90 *
91 * @param verb_table: verb table data
92 * @param verb_table_bytes: verb table size in bytes
93 * @param viddid: vendor/device to search for
94 * @param verb: pointer to entry within table
95 *
96 * Returns size of the entry within the verb table,
97 * Returns 0 if the entry is not found
98 *
99 * The HDA verb table is composed of dwords. A set of 4 dwords is
100 * grouped together to form a "jack" descriptor.
101 * Bits 31:28 - Codec Address
102 * Bits 27:20 - NID
103 * Bits 19:8 - Verb ID
104 * Bits 7:0 - Payload
105 *
106 * coreboot groups different codec verb tables into a single table
107 * and prefixes each with a specific header consisting of 3
108 * dword entries:
109 * 1 - Codec Vendor/Device ID
110 * 2 - Subsystem ID
111 * 3 - Number of jacks (groups of 4 dwords) for this codec
112 */
Angel Ponsd425ddd2020-12-05 18:22:58 +0100113u32 azalia_find_verb(const u32 *verb_table, u32 verb_table_bytes, u32 viddid, const u32 **verb)
Andrew Wub7bb70d2013-08-12 20:07:47 +0800114{
Andrew Wub7bb70d2013-08-12 20:07:47 +0800115 int idx = 0;
116
Angel Ponsd425ddd2020-12-05 18:22:58 +0100117 while (idx < (verb_table_bytes / sizeof(u32))) {
118 u32 verb_size = 4 * verb_table[idx + 2]; // in u32
119 if (verb_table[idx] != viddid) {
Andrew Wub7bb70d2013-08-12 20:07:47 +0800120 idx += verb_size + 3; // skip verb + header
121 continue;
122 }
Angel Ponsd425ddd2020-12-05 18:22:58 +0100123 *verb = &verb_table[idx + 3];
Andrew Wub7bb70d2013-08-12 20:07:47 +0800124 return verb_size;
125 }
126
127 /* Not all codecs need to load another verb */
128 return 0;
129}
130
Elyes HAOUAS6ea24ff2020-08-11 09:21:24 +0200131/*
132 * Wait 50usec for the codec to indicate it is ready.
133 * No response would imply that the codec is non-operative.
Andrew Wub7bb70d2013-08-12 20:07:47 +0800134 */
135
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800136static int wait_for_ready(u8 *base)
Andrew Wub7bb70d2013-08-12 20:07:47 +0800137{
Elyes HAOUAS6ea24ff2020-08-11 09:21:24 +0200138 /* Use a 50 usec timeout - the Linux kernel uses the same duration */
Andrew Wub7bb70d2013-08-12 20:07:47 +0800139 int timeout = 50;
140
141 while (timeout--) {
142 u32 reg32 = read32(base + HDA_ICII_REG);
143 if (!(reg32 & HDA_ICII_BUSY))
144 return 0;
145 udelay(1);
146 }
147
148 return -1;
149}
150
Elyes HAOUAS6ea24ff2020-08-11 09:21:24 +0200151/*
152 * Wait 50usec for the codec to indicate that it accepted the previous command.
153 * No response would imply that the code is non-operative.
Andrew Wub7bb70d2013-08-12 20:07:47 +0800154 */
155
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800156static int wait_for_valid(u8 *base)
Andrew Wub7bb70d2013-08-12 20:07:47 +0800157{
Elyes HAOUAS6ea24ff2020-08-11 09:21:24 +0200158 u32 reg32;
159 /* Use a 50 usec timeout - the Linux kernel uses the same duration */
Andrew Wub7bb70d2013-08-12 20:07:47 +0800160 int timeout = 25;
161
Elyes HAOUAS6ea24ff2020-08-11 09:21:24 +0200162 /* Send the verb to the codec */
163 reg32 = read32(base + HDA_ICII_REG);
164 reg32 |= HDA_ICII_BUSY | HDA_ICII_VALID;
165 write32(base + HDA_ICII_REG, reg32);
166
Andrew Wub7bb70d2013-08-12 20:07:47 +0800167 while (timeout--) {
168 udelay(1);
169 }
170 timeout = 50;
171 while (timeout--) {
Elyes HAOUAS6ea24ff2020-08-11 09:21:24 +0200172 reg32 = read32(base + HDA_ICII_REG);
173 if ((reg32 & (HDA_ICII_VALID | HDA_ICII_BUSY)) == HDA_ICII_VALID)
Andrew Wub7bb70d2013-08-12 20:07:47 +0800174 return 0;
175 udelay(1);
176 }
177
178 return -1;
179}
180
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800181static void codec_init(struct device *dev, u8 *base, int addr)
Andrew Wub7bb70d2013-08-12 20:07:47 +0800182{
183 u32 reg32;
184 const u32 *verb;
185 u32 verb_size;
186 int i;
187
188 printk(BIOS_DEBUG, "azalia_audio: Initializing codec #%d\n", addr);
189
190 /* 1 */
Angel Pons554713e2020-10-24 23:23:07 +0200191 if (wait_for_ready(base) < 0) {
Elyes HAOUAS6ea24ff2020-08-11 09:21:24 +0200192 printk(BIOS_DEBUG, " codec not ready.\n");
Andrew Wub7bb70d2013-08-12 20:07:47 +0800193 return;
Elyes HAOUAS6ea24ff2020-08-11 09:21:24 +0200194 }
Andrew Wub7bb70d2013-08-12 20:07:47 +0800195
196 reg32 = (addr << 28) | 0x000f0000;
Elyes HAOUASd8c47992020-08-03 15:31:39 +0200197 write32(base + HDA_IC_REG, reg32);
Andrew Wub7bb70d2013-08-12 20:07:47 +0800198
Angel Pons554713e2020-10-24 23:23:07 +0200199 if (wait_for_valid(base) < 0) {
Elyes HAOUAS6ea24ff2020-08-11 09:21:24 +0200200 printk(BIOS_DEBUG, " codec not valid.\n");
Andrew Wub7bb70d2013-08-12 20:07:47 +0800201 return;
Elyes HAOUAS6ea24ff2020-08-11 09:21:24 +0200202 }
Andrew Wub7bb70d2013-08-12 20:07:47 +0800203
204 /* 2 */
Elyes HAOUAS6ea24ff2020-08-11 09:21:24 +0200205 reg32 = read32(base + HDA_IR_REG);
Andrew Wub7bb70d2013-08-12 20:07:47 +0800206 printk(BIOS_DEBUG, "azalia_audio: codec viddid: %08x\n", reg32);
Angel Ponsd425ddd2020-12-05 18:22:58 +0100207 verb_size = azalia_find_verb(cim_verb_data, cim_verb_data_size, reg32, &verb);
Andrew Wub7bb70d2013-08-12 20:07:47 +0800208
209 if (!verb_size) {
210 printk(BIOS_DEBUG, "azalia_audio: No verb!\n");
211 return;
212 }
213 printk(BIOS_DEBUG, "azalia_audio: verb_size: %d\n", verb_size);
214
215 /* 3 */
216 for (i = 0; i < verb_size; i++) {
Angel Pons554713e2020-10-24 23:23:07 +0200217 if (wait_for_ready(base) < 0)
Andrew Wub7bb70d2013-08-12 20:07:47 +0800218 return;
219
Elyes HAOUASd8c47992020-08-03 15:31:39 +0200220 write32(base + HDA_IC_REG, verb[i]);
Andrew Wub7bb70d2013-08-12 20:07:47 +0800221
Angel Pons554713e2020-10-24 23:23:07 +0200222 if (wait_for_valid(base) < 0)
Andrew Wub7bb70d2013-08-12 20:07:47 +0800223 return;
224 }
225 printk(BIOS_DEBUG, "azalia_audio: verb loaded.\n");
226}
227
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800228static void codecs_init(struct device *dev, u8 *base, u32 codec_mask)
Andrew Wub7bb70d2013-08-12 20:07:47 +0800229{
230 int i;
231
232 for (i = 2; i >= 0; i--) {
233 if (codec_mask & (1 << i))
234 codec_init(dev, base, i);
235 }
236}
237
238void azalia_audio_init(struct device *dev)
239{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800240 u8 *base;
Andrew Wub7bb70d2013-08-12 20:07:47 +0800241 struct resource *res;
242 u32 codec_mask;
243
Elyes HAOUAS6ea24ff2020-08-11 09:21:24 +0200244 res = find_resource(dev, PCI_BASE_ADDRESS_0);
Andrew Wub7bb70d2013-08-12 20:07:47 +0800245 if (!res)
246 return;
247
Elyes HAOUAS6ea24ff2020-08-11 09:21:24 +0200248 // NOTE this will break as soon as the azalia_audio get's a bar above 4G.
249 // Is there anything we can do about it?
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800250 base = res2mmio(res, 0, 0);
251 printk(BIOS_DEBUG, "azalia_audio: base = %p\n", base);
Andrew Wub7bb70d2013-08-12 20:07:47 +0800252 codec_mask = codec_detect(base);
253
254 if (codec_mask) {
Elyes HAOUAS6ea24ff2020-08-11 09:21:24 +0200255 printk(BIOS_DEBUG, "azalia_audio: codec_mask = %02x\n", codec_mask);
Andrew Wub7bb70d2013-08-12 20:07:47 +0800256 codecs_init(dev, base, codec_mask);
257 }
258}
259
Andrew Wub7bb70d2013-08-12 20:07:47 +0800260struct device_operations default_azalia_audio_ops = {
261 .read_resources = pci_dev_read_resources,
262 .set_resources = pci_dev_set_resources,
263 .enable_resources = pci_dev_enable_resources,
264 .init = azalia_audio_init,
Angel Pons1fc0edd2020-05-31 00:03:28 +0200265 .ops_pci = &pci_dev_ops_pci,
Andrew Wub7bb70d2013-08-12 20:07:47 +0800266};