Debian Codegraph @

src/device/azalia_device.c

raw symbols SCIP
1 /* SPDX-License-Identifier: GPL-2.0-only */
2
3 #include <console/console.h>
4 #include <device/device.h>
5 #include <device/pci.h>
6 #include <device/azalia_device.h>
7 #include <device/mmio.h>
8 #include <delay.h>
9 #include <timer.h>
10 #include <types.h>
11
12 static enum cb_err azalia_set_bits(void *port, u32 mask, u32 val)
13 {
14 struct stopwatch sw;
15 u32 reg32;
16
17 clrsetbits32(port, mask, val);
18
19 /* Wait for readback of register to match what was just written to it */
20 stopwatch_init_msecs_expire(&sw, 50);
21 do {
22 /* Wait 1ms based on BKDG wait time */
23 mdelay(1);
24 reg32 = read32(port);
25 reg32 &= mask;
26 } while ((reg32 != val) && !stopwatch_expired(&sw));
27
28 /* Timeout occurred */
29 if (stopwatch_expired(&sw))
30 return CB_ERR;
31 return CB_SUCCESS;
32 }
33
34 enum cb_err azalia_enter_reset(u8 *base)
35 {
36 /* Set bit 0 to 0 to enter reset state (BAR + 0x8)[0] */
37 return azalia_set_bits(base + HDA_GCTL_REG, HDA_GCTL_CRST, 0);
38 }
39
40 enum cb_err azalia_exit_reset(u8 *base)
41 {
42 /* Set bit 0 to 1 to exit reset state (BAR + 0x8)[0] */
43 return azalia_set_bits(base + HDA_GCTL_REG, HDA_GCTL_CRST, HDA_GCTL_CRST);
44 }
45
46 static u16 codec_detect(u8 *base)
47 {
48 struct stopwatch sw;
49 u16 reg16;
50
51 if (azalia_exit_reset(base) != CB_SUCCESS)
52 goto no_codec;
53
54 /*
55 * In the HD Audio Specification Rev. 1.0a, every bitfield in the GCAP
56 * register is RO (Read Only). However, it is known that in some Intel
57 * PCHs (e.g 6-series and 7-series, documents 324645 and 326776), some
58 * of the bitfields in the GCAP register are R/WO (Read / Write Once).
59 * GCAP is RO on 5-series PCHs; 8-series and 9-series PCHs have a lock
60 * bit for GCAP elsewhere.
61 *
62 * Lock GCAP by reading GCAP and writing back the same value. This has
63 * no effect on platforms that implement GCAP as a RO register or lock
64 * GCAP through a different mechanism.
65 */
66 write16(base + HDA_GCAP_REG, read16(base + HDA_GCAP_REG));
67
68 /* clear STATESTS bits (BAR + 0x0e)[14:0] */
69 setbits16(base + HDA_STATESTS_REG, 0x7fff);
70
71 /* Wait for readback of register to
72 * match what was just written to it
73 */
74 stopwatch_init_msecs_expire(&sw, 50);
75 do {
76 /* Wait 1ms based on BKDG wait time */
77 mdelay(1);
78 reg16 = read16(base + HDA_STATESTS_REG);
79 } while ((reg16 != 0) && !stopwatch_expired(&sw));
80
81 /* Timeout occurred */
82 if (stopwatch_expired(&sw))
83 goto no_codec;
84
85 if (azalia_enter_reset(base) != CB_SUCCESS)
86 goto no_codec;
87
88 if (azalia_exit_reset(base) != CB_SUCCESS)
89 goto no_codec;
90
91 /* Read in Codec location (BAR + 0x0e)[14:0] */
92 reg16 = read16(base + HDA_STATESTS_REG);
93 reg16 &= 0x7fff;
94 if (!reg16)
95 goto no_codec;
96
97 return reg16;
98
99 no_codec:
100 /* Codec Not found */
101 azalia_enter_reset(base);
102 printk(BIOS_DEBUG, "azalia_audio: no codec!\n");
103 return 0;
104 }
105
106 /*
107 * Find a specific entry within a verb table
108 *
109 * @param verb_table: verb table data
110 * @param verb_table_bytes: verb table size in bytes
111 * @param viddid: vendor/device to search for
112 * @param verb: pointer to entry within table
113 *
114 * Returns size of the entry within the verb table,
115 * Returns 0 if the entry is not found
116 *
117 * The HDA verb table is composed of dwords. A set of 4 dwords is
118 * grouped together to form a "jack" descriptor.
119 * Bits 31:28 - Codec Address
120 * Bits 27:20 - NID
121 * Bits 19:8 - Verb ID
122 * Bits 7:0 - Payload
123 *
124 * coreboot groups different codec verb tables into a single table
125 * and prefixes each with a specific header consisting of 3
126 * dword entries:
127 * 1 - Codec Vendor/Device ID
128 * 2 - Subsystem ID
129 * 3 - Number of jacks (groups of 4 dwords) for this codec
130 */
131 u32 azalia_find_verb(const u32 *verb_table, u32 verb_table_bytes, u32 viddid, const u32 **verb)
132 {
133 int idx = 0;
134
135 while (idx < (verb_table_bytes / sizeof(u32))) {
136 /* Header contains the number of jacks, aka groups of 4 dwords */
137 u32 verb_size = 4 * verb_table[idx + 2];
138 if (verb_table[idx] != viddid) {
139 idx += verb_size + 3; // skip verb + header
140 continue;
141 }
142 *verb = &verb_table[idx + 3];
143 return verb_size;
144 }
145
146 /* Not all codecs need to load another verb */
147 return 0;
148 }
149
150 /*
151 * Wait 50usec for the controller to indicate it is ready.
152 */
153 static int wait_for_ready(u8 *base)
154 {
155 struct stopwatch sw;
156 /* Use a 50 usec timeout - the Linux kernel uses the same duration */
157 stopwatch_init_usecs_expire(&sw, 50);
158
159 while (!stopwatch_expired(&sw)) {
160 u32 reg32 = read32(base + HDA_ICII_REG);
161 if (!(reg32 & HDA_ICII_BUSY))
162 return 0;
163 udelay(1);
164 }
165
166 return -1;
167 }
168
169 /*
170 * Wait for the codec to indicate that it accepted the previous command.
171 * No response would imply that the codec is non-operative.
172 */
173 static int wait_for_valid(u8 *base)
174 {
175 struct stopwatch sw;
176 u32 reg32;
177
178 /* Send the verb to the codec */
179 setbits32(base + HDA_ICII_REG, HDA_ICII_BUSY | HDA_ICII_VALID);
180
181 /*
182 * The timeout is never reached when the codec is functioning properly.
183 * Using a small timeout value can result in spurious errors with some
184 * codecs, e.g. a codec that is slow to respond but operates correctly.
185 * When a codec is non-operative, the timeout is only reached once per
186 * verb table, thus the impact on booting time is relatively small. So,
187 * use a reasonably long enough timeout to cover all possible cases.
188 */
189 stopwatch_init_msecs_expire(&sw, 1);
190 while (!stopwatch_expired(&sw)) {
191 reg32 = read32(base + HDA_ICII_REG);
192 if ((reg32 & (HDA_ICII_VALID | HDA_ICII_BUSY)) == HDA_ICII_VALID)
193 return 0;
194 udelay(1);
195 }
196
197 /*
198 * HDA spec 1.0a "3.4.3 Offset 68h: Immediate Command Status"
199 * tells us to clear the busy bit explicitly, then poll until
200 * the controller is ready.
201 */
202 write32(base + HDA_ICII_REG, 0);
203 if (wait_for_ready(base) < 0) {
204 printk(BIOS_WARNING, "azalia_audio: controller is unresponsive.\n");
205 return -2;
206 }
207 return -1;
208 }
209
210 static int azalia_write_verb(u8 *base, u32 verb)
211 {
212 if (wait_for_ready(base) < 0)
213 return -1;
214
215 write32(base + HDA_IC_REG, verb);
216
217 return wait_for_valid(base);
218 }
219
220 int azalia_program_verb_table(u8 *base, const u32 *verbs, u32 verb_size)
221 {
222 if (!verbs)
223 return 0;
224
225 for (u32 i = 0; i < verb_size; i++) {
226 if (azalia_write_verb(base, verbs[i]) < 0)
227 return -1;
228 }
229 return 0;
230 }
231
232 __weak void mainboard_azalia_program_runtime_verbs(u8 *base, u32 viddid)
233 {
234 }
235
236 void azalia_codec_init(u8 *base, int addr, const u32 *verb_table, u32 verb_table_bytes)
237 {
238 if (azalia_write_verb(base, AZALIA_VERB_GET_VENDOR_ID(addr)) < 0)
239 return;
240 u32 viddid = read32(base + HDA_IR_REG);
241
242 const u32 *verb;
243 u32 verb_size;
244
245 printk(BIOS_DEBUG, "azalia_audio: initializing codec #%d...\n", addr);
246 printk(BIOS_DEBUG, "azalia_audio: - vendor/device id: 0x%08x\n", viddid);
247
248 verb_size = azalia_find_verb(verb_table, verb_table_bytes, viddid, &verb);
249
250 if (verb_size == 0) {
251 printk(BIOS_DEBUG, "azalia_audio: - no verb!\n");
252 return;
253 }
254 printk(BIOS_DEBUG, "azalia_audio: - verb size: %u\n", verb_size);
255
256 if (azalia_program_verb_table(base, verb, verb_size) < 0)
257 printk(BIOS_DEBUG, "azalia_audio: - verb not loaded\n");
258 else
259 printk(BIOS_DEBUG, "azalia_audio: - verb loaded\n");
260
261 mainboard_azalia_program_runtime_verbs(base, viddid);
262 }
263
264 void azalia_codecs_init(u8 *base, u16 codec_mask)
265 {
266 for (int i = AZALIA_MAX_CODECS - 1; i >= 0; i--) {
267 if (codec_mask & BIT(i))
268 azalia_codec_init(base, i, cim_verb_data, cim_verb_data_size);
269 }
270
271 azalia_program_verb_table(base, pc_beep_verbs, pc_beep_verbs_size);
272 }
273
274 void azalia_audio_init(struct device *dev)
275 {
276 u8 *base;
277 struct resource *res;
278 u16 codec_mask;
279
280 res = probe_resource(dev, PCI_BASE_ADDRESS_0);
281 if (!res)
282 return;
283
284 // NOTE this will break as soon as the azalia_audio gets a bar above 4G.
285 // Is there anything we can do about it?
286 base = res2mmio(res, 0, 0);
287 printk(BIOS_DEBUG, "azalia_audio: base = %p\n", base);
288 codec_mask = codec_detect(base);
289
290 if (codec_mask) {
291 printk(BIOS_DEBUG, "azalia_audio: codec_mask = 0x%02x\n", codec_mask);
292 azalia_codecs_init(base, codec_mask);
293 }
294 }
295
296 struct device_operations default_azalia_audio_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_audio_init,
301 .ops_pci = &pci_dev_ops_pci,
302 };