blob: ab370f95d7a78a538b3a2459a740e6f592a8ca94 [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))) {
Angel Ponsf23c6a82020-12-05 18:32:05 +0100118 /* Header contains the number of jacks, aka groups of 4 dwords */
119 u32 verb_size = 4 * verb_table[idx + 2];
Angel Ponsd425ddd2020-12-05 18:22:58 +0100120 if (verb_table[idx] != viddid) {
Andrew Wub7bb70d2013-08-12 20:07:47 +0800121 idx += verb_size + 3; // skip verb + header
122 continue;
123 }
Angel Ponsd425ddd2020-12-05 18:22:58 +0100124 *verb = &verb_table[idx + 3];
Andrew Wub7bb70d2013-08-12 20:07:47 +0800125 return verb_size;
126 }
127
128 /* Not all codecs need to load another verb */
129 return 0;
130}
131
Elyes HAOUAS6ea24ff2020-08-11 09:21:24 +0200132/*
133 * Wait 50usec for the codec to indicate it is ready.
134 * No response would imply that the codec is non-operative.
Andrew Wub7bb70d2013-08-12 20:07:47 +0800135 */
136
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800137static int wait_for_ready(u8 *base)
Andrew Wub7bb70d2013-08-12 20:07:47 +0800138{
Elyes HAOUAS6ea24ff2020-08-11 09:21:24 +0200139 /* Use a 50 usec timeout - the Linux kernel uses the same duration */
Andrew Wub7bb70d2013-08-12 20:07:47 +0800140 int timeout = 50;
141
142 while (timeout--) {
143 u32 reg32 = read32(base + HDA_ICII_REG);
144 if (!(reg32 & HDA_ICII_BUSY))
145 return 0;
146 udelay(1);
147 }
148
149 return -1;
150}
151
Elyes HAOUAS6ea24ff2020-08-11 09:21:24 +0200152/*
153 * Wait 50usec for the codec to indicate that it accepted the previous command.
154 * No response would imply that the code is non-operative.
Andrew Wub7bb70d2013-08-12 20:07:47 +0800155 */
156
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800157static int wait_for_valid(u8 *base)
Andrew Wub7bb70d2013-08-12 20:07:47 +0800158{
Elyes HAOUAS6ea24ff2020-08-11 09:21:24 +0200159 u32 reg32;
160 /* Use a 50 usec timeout - the Linux kernel uses the same duration */
Andrew Wub7bb70d2013-08-12 20:07:47 +0800161 int timeout = 25;
162
Elyes HAOUAS6ea24ff2020-08-11 09:21:24 +0200163 /* Send the verb to the codec */
164 reg32 = read32(base + HDA_ICII_REG);
165 reg32 |= HDA_ICII_BUSY | HDA_ICII_VALID;
166 write32(base + HDA_ICII_REG, reg32);
167
Andrew Wub7bb70d2013-08-12 20:07:47 +0800168 while (timeout--) {
169 udelay(1);
170 }
171 timeout = 50;
172 while (timeout--) {
Elyes HAOUAS6ea24ff2020-08-11 09:21:24 +0200173 reg32 = read32(base + HDA_ICII_REG);
174 if ((reg32 & (HDA_ICII_VALID | HDA_ICII_BUSY)) == HDA_ICII_VALID)
Andrew Wub7bb70d2013-08-12 20:07:47 +0800175 return 0;
176 udelay(1);
177 }
178
179 return -1;
180}
181
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800182static void codec_init(struct device *dev, u8 *base, int addr)
Andrew Wub7bb70d2013-08-12 20:07:47 +0800183{
184 u32 reg32;
185 const u32 *verb;
186 u32 verb_size;
187 int i;
188
189 printk(BIOS_DEBUG, "azalia_audio: Initializing codec #%d\n", addr);
190
191 /* 1 */
Angel Pons554713e2020-10-24 23:23:07 +0200192 if (wait_for_ready(base) < 0) {
Elyes HAOUAS6ea24ff2020-08-11 09:21:24 +0200193 printk(BIOS_DEBUG, " codec not ready.\n");
Andrew Wub7bb70d2013-08-12 20:07:47 +0800194 return;
Elyes HAOUAS6ea24ff2020-08-11 09:21:24 +0200195 }
Andrew Wub7bb70d2013-08-12 20:07:47 +0800196
197 reg32 = (addr << 28) | 0x000f0000;
Elyes HAOUASd8c47992020-08-03 15:31:39 +0200198 write32(base + HDA_IC_REG, reg32);
Andrew Wub7bb70d2013-08-12 20:07:47 +0800199
Angel Pons554713e2020-10-24 23:23:07 +0200200 if (wait_for_valid(base) < 0) {
Elyes HAOUAS6ea24ff2020-08-11 09:21:24 +0200201 printk(BIOS_DEBUG, " codec not valid.\n");
Andrew Wub7bb70d2013-08-12 20:07:47 +0800202 return;
Elyes HAOUAS6ea24ff2020-08-11 09:21:24 +0200203 }
Andrew Wub7bb70d2013-08-12 20:07:47 +0800204
205 /* 2 */
Elyes HAOUAS6ea24ff2020-08-11 09:21:24 +0200206 reg32 = read32(base + HDA_IR_REG);
Andrew Wub7bb70d2013-08-12 20:07:47 +0800207 printk(BIOS_DEBUG, "azalia_audio: codec viddid: %08x\n", reg32);
Angel Ponsd425ddd2020-12-05 18:22:58 +0100208 verb_size = azalia_find_verb(cim_verb_data, cim_verb_data_size, reg32, &verb);
Andrew Wub7bb70d2013-08-12 20:07:47 +0800209
210 if (!verb_size) {
211 printk(BIOS_DEBUG, "azalia_audio: No verb!\n");
212 return;
213 }
214 printk(BIOS_DEBUG, "azalia_audio: verb_size: %d\n", verb_size);
215
216 /* 3 */
217 for (i = 0; i < verb_size; i++) {
Angel Pons554713e2020-10-24 23:23:07 +0200218 if (wait_for_ready(base) < 0)
Andrew Wub7bb70d2013-08-12 20:07:47 +0800219 return;
220
Elyes HAOUASd8c47992020-08-03 15:31:39 +0200221 write32(base + HDA_IC_REG, verb[i]);
Andrew Wub7bb70d2013-08-12 20:07:47 +0800222
Angel Pons554713e2020-10-24 23:23:07 +0200223 if (wait_for_valid(base) < 0)
Andrew Wub7bb70d2013-08-12 20:07:47 +0800224 return;
225 }
226 printk(BIOS_DEBUG, "azalia_audio: verb loaded.\n");
227}
228
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800229static void codecs_init(struct device *dev, u8 *base, u32 codec_mask)
Andrew Wub7bb70d2013-08-12 20:07:47 +0800230{
231 int i;
232
233 for (i = 2; i >= 0; i--) {
234 if (codec_mask & (1 << i))
235 codec_init(dev, base, i);
236 }
237}
238
239void azalia_audio_init(struct device *dev)
240{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800241 u8 *base;
Andrew Wub7bb70d2013-08-12 20:07:47 +0800242 struct resource *res;
243 u32 codec_mask;
244
Elyes HAOUAS6ea24ff2020-08-11 09:21:24 +0200245 res = find_resource(dev, PCI_BASE_ADDRESS_0);
Andrew Wub7bb70d2013-08-12 20:07:47 +0800246 if (!res)
247 return;
248
Elyes HAOUAS6ea24ff2020-08-11 09:21:24 +0200249 // NOTE this will break as soon as the azalia_audio get's a bar above 4G.
250 // Is there anything we can do about it?
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800251 base = res2mmio(res, 0, 0);
252 printk(BIOS_DEBUG, "azalia_audio: base = %p\n", base);
Andrew Wub7bb70d2013-08-12 20:07:47 +0800253 codec_mask = codec_detect(base);
254
255 if (codec_mask) {
Elyes HAOUAS6ea24ff2020-08-11 09:21:24 +0200256 printk(BIOS_DEBUG, "azalia_audio: codec_mask = %02x\n", codec_mask);
Andrew Wub7bb70d2013-08-12 20:07:47 +0800257 codecs_init(dev, base, codec_mask);
258 }
259}
260
Andrew Wub7bb70d2013-08-12 20:07:47 +0800261struct device_operations default_azalia_audio_ops = {
262 .read_resources = pci_dev_read_resources,
263 .set_resources = pci_dev_set_resources,
264 .enable_resources = pci_dev_enable_resources,
265 .init = azalia_audio_init,
Angel Pons1fc0edd2020-05-31 00:03:28 +0200266 .ops_pci = &pci_dev_ops_pci,
Andrew Wub7bb70d2013-08-12 20:07:47 +0800267};