blob: dca4e0a1104bd975dda7066a005cf9364cb75432 [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
10#define HDA_ICII_REG 0x68
11#define HDA_ICII_BUSY (1 << 0)
12#define HDA_ICII_VALID (1 << 1)
13
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080014static int set_bits(void *port, u32 mask, u32 val)
Andrew Wub7bb70d2013-08-12 20:07:47 +080015{
16 u32 reg32;
17 int count;
18
19 /* Write (val & mask) to port */
20 val &= mask;
21 reg32 = read32(port);
22 reg32 &= ~mask;
23 reg32 |= val;
24 write32(port, reg32);
25
26 /* Wait for readback of register to
27 * match what was just written to it
28 */
29 count = 50;
30 do {
31 /* Wait 1ms based on BKDG wait time */
32 mdelay(1);
33 reg32 = read32(port);
34 reg32 &= mask;
35 } while ((reg32 != val) && --count);
36
37 /* Timeout occurred */
38 if (!count)
39 return -1;
40 return 0;
41}
42
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080043static int codec_detect(u8 *base)
Andrew Wub7bb70d2013-08-12 20:07:47 +080044{
45 u32 reg32;
46 int count;
47
48 /* Set Bit 0 to 1 to exit reset state (BAR + 0x8)[0] */
49 if (set_bits(base + 0x08, 1, 1) == -1)
50 goto no_codec;
51
52 /* clear STATESTS bits (BAR + 0xE)[2:0] */
53 reg32 = read32(base + 0x0E);
54 reg32 |= 7;
55 write32(base + 0x0E, reg32);
56
57 /* Wait for readback of register to
58 * match what was just written to it
59 */
60 count = 50;
61 do {
62 /* Wait 1ms based on BKDG wait time */
63 mdelay(1);
64 reg32 = read32(base + 0x0E);
65 } while ((reg32 != 0) && --count);
Martin Roth2ed0aa22016-01-05 20:58:58 -070066 /* Timeout occurred */
Andrew Wub7bb70d2013-08-12 20:07:47 +080067 if (!count)
68 goto no_codec;
69
70 /* Set Bit0 to 0 to enter reset state (BAR + 0x8)[0] */
71 if (set_bits(base + 0x08, 1, 0) == -1)
72 goto no_codec;
73
74 /* Set Bit 0 to 1 to exit reset state (BAR + 0x8)[0] */
75 if (set_bits(base + 0x08, 1, 1) == -1)
76 goto no_codec;
77
78 /* Read in Codec location (BAR + 0xe)[2..0] */
79 reg32 = read32(base + 0xe);
80 reg32 &= 0x0f;
81 if (!reg32)
82 goto no_codec;
83
84 return reg32;
85
86no_codec:
87 /* Codec Not found */
88 /* Put HDA back in reset (BAR + 0x8) [0] */
89 set_bits(base + 0x08, 1, 0);
90 printk(BIOS_DEBUG, "azalia_audio: No codec!\n");
91 return 0;
92}
93
Elyes HAOUASe414a4e2019-01-03 10:40:43 +010094static u32 find_verb(struct device *dev, u32 viddid, const u32 **verb)
Andrew Wub7bb70d2013-08-12 20:07:47 +080095{
96 printk(BIOS_DEBUG, "azalia_audio: dev=%s\n", dev_path(dev));
97 printk(BIOS_DEBUG, "azalia_audio: Reading viddid=%x\n", viddid);
98
99 int idx = 0;
100
101 while (idx < (cim_verb_data_size / sizeof(u32))) {
102 u32 verb_size = 4 * cim_verb_data[idx + 2]; // in u32
103 if (cim_verb_data[idx] != viddid) {
104 idx += verb_size + 3; // skip verb + header
105 continue;
106 }
107 *verb = &cim_verb_data[idx + 3];
108 return verb_size;
109 }
110
111 /* Not all codecs need to load another verb */
112 return 0;
113}
114
115/**
116 * Wait 50usec for the codec to indicate it is ready
117 * no response would imply that the codec is non-operative
118 */
119
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800120static int wait_for_ready(u8 *base)
Andrew Wub7bb70d2013-08-12 20:07:47 +0800121{
122 /* Use a 50 usec timeout - the Linux kernel uses the
123 * same duration */
124
125 int timeout = 50;
126
127 while (timeout--) {
128 u32 reg32 = read32(base + HDA_ICII_REG);
129 if (!(reg32 & HDA_ICII_BUSY))
130 return 0;
131 udelay(1);
132 }
133
134 return -1;
135}
136
137/**
138 * Wait 50usec for the codec to indicate that it accepted
139 * the previous command. No response would imply that the code
140 * is non-operative
141 */
142
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800143static int wait_for_valid(u8 *base)
Andrew Wub7bb70d2013-08-12 20:07:47 +0800144{
145 /* Use a 50 usec timeout - the Linux kernel uses the
146 * same duration */
147
148 int timeout = 25;
149
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800150 write32(base + HDA_ICII_REG,
151 HDA_ICII_VALID | HDA_ICII_BUSY);
Andrew Wub7bb70d2013-08-12 20:07:47 +0800152 while (timeout--) {
153 udelay(1);
154 }
155 timeout = 50;
156 while (timeout--) {
157 u32 reg32 = read32(base + HDA_ICII_REG);
158 if ((reg32 & (HDA_ICII_VALID | HDA_ICII_BUSY)) ==
159 HDA_ICII_VALID)
160 return 0;
161 udelay(1);
162 }
163
164 return -1;
165}
166
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800167static void codec_init(struct device *dev, u8 *base, int addr)
Andrew Wub7bb70d2013-08-12 20:07:47 +0800168{
169 u32 reg32;
170 const u32 *verb;
171 u32 verb_size;
172 int i;
173
174 printk(BIOS_DEBUG, "azalia_audio: Initializing codec #%d\n", addr);
175
176 /* 1 */
177 if (wait_for_ready(base) == -1)
178 return;
179
180 reg32 = (addr << 28) | 0x000f0000;
181 write32(base + 0x60, reg32);
182
183 if (wait_for_valid(base) == -1)
184 return;
185
186 reg32 = read32(base + 0x64);
187
188 /* 2 */
189 printk(BIOS_DEBUG, "azalia_audio: codec viddid: %08x\n", reg32);
190 verb_size = find_verb(dev, reg32, &verb);
191
192 if (!verb_size) {
193 printk(BIOS_DEBUG, "azalia_audio: No verb!\n");
194 return;
195 }
196 printk(BIOS_DEBUG, "azalia_audio: verb_size: %d\n", verb_size);
197
198 /* 3 */
199 for (i = 0; i < verb_size; i++) {
200 if (wait_for_ready(base) == -1)
201 return;
202
203 write32(base + 0x60, verb[i]);
204
205 if (wait_for_valid(base) == -1)
206 return;
207 }
208 printk(BIOS_DEBUG, "azalia_audio: verb loaded.\n");
209}
210
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800211static void codecs_init(struct device *dev, u8 *base, u32 codec_mask)
Andrew Wub7bb70d2013-08-12 20:07:47 +0800212{
213 int i;
214
215 for (i = 2; i >= 0; i--) {
216 if (codec_mask & (1 << i))
217 codec_init(dev, base, i);
218 }
219}
220
221void azalia_audio_init(struct device *dev)
222{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800223 u8 *base;
Andrew Wub7bb70d2013-08-12 20:07:47 +0800224 struct resource *res;
225 u32 codec_mask;
226
227 res = find_resource(dev, 0x10);
228 if (!res)
229 return;
230
231 // NOTE this will break as soon as the azalia_audio get's a bar above
232 // 4G. Is there anything we can do about it?
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800233 base = res2mmio(res, 0, 0);
234 printk(BIOS_DEBUG, "azalia_audio: base = %p\n", base);
Andrew Wub7bb70d2013-08-12 20:07:47 +0800235 codec_mask = codec_detect(base);
236
237 if (codec_mask) {
238 printk(BIOS_DEBUG, "azalia_audio: codec_mask = %02x\n",
239 codec_mask);
240 codecs_init(dev, base, codec_mask);
241 }
242}
243
244struct pci_operations azalia_audio_pci_ops = {
245 .set_subsystem = pci_dev_set_subsystem,
246};
247
248struct device_operations default_azalia_audio_ops = {
249 .read_resources = pci_dev_read_resources,
250 .set_resources = pci_dev_set_resources,
251 .enable_resources = pci_dev_enable_resources,
252 .init = azalia_audio_init,
Andrew Wub7bb70d2013-08-12 20:07:47 +0800253 .ops_pci = &azalia_audio_pci_ops,
254};