blob: ba20a6aae6e2be1dedb2ce4057deec49e63a3afd [file] [log] [blame]
Angel Pons182dbde2020-04-02 23:49:05 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Arthur Heymans7b9c1392017-04-09 20:40:39 +02002
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>
Arthur Heymans7b9c1392017-04-09 20:40:39 +02009#include <delay.h>
10#include <device/azalia_device.h>
Kyösti Mälkki12b121c2019-08-18 16:33:39 +030011#include "chip.h"
Arthur Heymans349e0852017-04-09 20:48:37 +020012#include "i82801jx.h"
Arthur Heymans7b9c1392017-04-09 20:40:39 +020013
14#define HDA_ICII_REG 0x68
15#define HDA_ICII_BUSY (1 << 0)
16#define HDA_ICII_VALID (1 << 1)
17
Arthur Heymans7b9c1392017-04-09 20:40:39 +020018static int set_bits(void *port, u32 mask, u32 val)
19{
20 u32 reg32;
21 int count;
22
23 /* Write (val & mask) to port */
24 val &= mask;
25 reg32 = read32(port);
26 reg32 &= ~mask;
27 reg32 |= val;
28 write32(port, reg32);
29
Angel Pons7a2864b2020-06-21 13:29:28 +020030 /* Wait for readback of register to match what was just written to it */
Arthur Heymans7b9c1392017-04-09 20:40:39 +020031 count = 50;
32 do {
33 /* Wait 1ms based on BKDG wait time */
34 mdelay(1);
35 reg32 = read32(port);
36 reg32 &= mask;
37 } while ((reg32 != val) && --count);
38
39 /* Timeout occurred */
40 if (!count)
41 return -1;
42 return 0;
43}
44
45static int codec_detect(u8 *base)
46{
47 u32 reg32;
48
49 /* Set Bit0 to 0 to enter reset state (BAR + 0x8)[0] */
50 if (set_bits(base + 0x08, 1, 0) == -1)
51 goto no_codec;
52
53 /* Set Bit 0 to 1 to exit reset state (BAR + 0x8)[0] */
54 if (set_bits(base + 0x08, 1, 1) == -1)
55 goto no_codec;
56
57 /* Read in Codec location (BAR + 0xe)[2..0]*/
58 reg32 = read32(base + 0xe);
59 reg32 &= 0x0f;
60 if (!reg32)
61 goto no_codec;
62
63 return reg32;
64
65no_codec:
66 /* Codec Not found */
67 /* Put HDA back in reset (BAR + 0x8) [0] */
68 set_bits(base + 0x08, 1, 0);
69 printk(BIOS_DEBUG, "Azalia: No codec!\n");
70 return 0;
71}
72
Elyes HAOUASe414a4e2019-01-03 10:40:43 +010073static u32 find_verb(struct device *dev, u32 viddid, const u32 **verb)
Arthur Heymans7b9c1392017-04-09 20:40:39 +020074{
Angel Pons7a2864b2020-06-21 13:29:28 +020075 int idx = 0;
Arthur Heymans7b9c1392017-04-09 20:40:39 +020076
77 while (idx < (cim_verb_data_size / sizeof(u32))) {
78 u32 verb_size = 4 * cim_verb_data[idx+2]; // in u32
79 if (cim_verb_data[idx] != viddid) {
80 idx += verb_size + 3; // skip verb + header
81 continue;
82 }
83 *verb = &cim_verb_data[idx+3];
84 return verb_size;
85 }
86
87 /* Not all codecs need to load another verb */
88 return 0;
89}
90
91/**
92 * Wait 50usec for the codec to indicate it is ready
93 * no response would imply that the codec is non-operative
94 */
95
96static int wait_for_ready(u8 *base)
97{
Angel Pons7a2864b2020-06-21 13:29:28 +020098 /* Use a 50 usec timeout - the Linux kernel uses the same duration */
Arthur Heymans7b9c1392017-04-09 20:40:39 +020099 int timeout = 50;
100
101 while (timeout--) {
102 u32 reg32 = read32(base + HDA_ICII_REG);
103 if (!(reg32 & HDA_ICII_BUSY))
104 return 0;
105 udelay(1);
106 }
107
108 return -1;
109}
110
111/**
Angel Pons7a2864b2020-06-21 13:29:28 +0200112 * Wait 50usec for the codec to indicate that it accepted the previous command.
113 * No response would imply that the code is non-operative.
Arthur Heymans7b9c1392017-04-09 20:40:39 +0200114 */
115
116static int wait_for_valid(u8 *base)
117{
118 u32 reg32;
119
120 /* Send the verb to the codec */
121 reg32 = read32(base + 0x68);
122 reg32 |= (1 << 0) | (1 << 1);
123 write32(base + 0x68, reg32);
124
Angel Pons7a2864b2020-06-21 13:29:28 +0200125 /* Use a 50 usec timeout - the Linux kernel uses the same duration */
Arthur Heymans7b9c1392017-04-09 20:40:39 +0200126
127 int timeout = 50;
128 while (timeout--) {
129 reg32 = read32(base + HDA_ICII_REG);
Angel Pons7a2864b2020-06-21 13:29:28 +0200130 if ((reg32 & (HDA_ICII_VALID | HDA_ICII_BUSY)) == HDA_ICII_VALID)
Arthur Heymans7b9c1392017-04-09 20:40:39 +0200131 return 0;
132 udelay(1);
133 }
134
135 return -1;
136}
137
138static void codec_init(struct device *dev, u8 *base, int addr)
139{
140 u32 reg32;
141 const u32 *verb;
142 u32 verb_size;
143 int i;
144
Angel Ponsaaa8ab72020-06-21 15:33:24 +0200145 printk(BIOS_DEBUG, "Azalia: Initializing codec #%d\n", addr);
Arthur Heymans7b9c1392017-04-09 20:40:39 +0200146
147 /* 1 */
148 if (wait_for_ready(base) == -1)
149 return;
150
151 reg32 = (addr << 28) | 0x000f0000;
152 write32(base + 0x60, reg32);
153
154 if (wait_for_valid(base) == -1)
155 return;
156
157 reg32 = read32(base + 0x64);
158
159 /* 2 */
160 printk(BIOS_DEBUG, "Azalia: codec viddid: %08x\n", reg32);
161 verb_size = find_verb(dev, reg32, &verb);
162
163 if (!verb_size) {
164 printk(BIOS_DEBUG, "Azalia: No verb!\n");
165 return;
166 }
167 printk(BIOS_DEBUG, "Azalia: verb_size: %d\n", verb_size);
168
169 /* 3 */
170 for (i = 0; i < verb_size; i++) {
171 if (wait_for_ready(base) == -1)
172 return;
173
174 write32(base + 0x60, verb[i]);
175
176 if (wait_for_valid(base) == -1)
177 return;
178 }
179 printk(BIOS_DEBUG, "Azalia: verb loaded.\n");
180}
181
182static void codecs_init(struct device *dev, u8 *base, u32 codec_mask)
183{
184 int i;
185 for (i = 2; i >= 0; i--) {
186 if (codec_mask & (1 << i))
187 codec_init(dev, base, i);
188 }
189
190 for (i = 0; i < pc_beep_verbs_size; i++) {
191 if (wait_for_ready(base) == -1)
192 return;
193
194 write32(base + 0x60, pc_beep_verbs[i]);
195
196 if (wait_for_valid(base) == -1)
197 return;
198 }
199}
200
201static void azalia_init(struct device *dev)
202{
203 u8 *base;
204 struct resource *res;
205 u32 codec_mask;
Arthur Heymans7b9c1392017-04-09 20:40:39 +0200206
207 // ESD
Angel Pons2048cb42020-06-08 02:09:33 +0200208 pci_update_config32(dev, 0x134, ~0x00ff0000, 2 << 16);
Arthur Heymans7b9c1392017-04-09 20:40:39 +0200209
210 // Link1 description
Angel Pons2048cb42020-06-08 02:09:33 +0200211 pci_update_config32(dev, 0x140, ~0x00ff0000, 2 << 16);
Arthur Heymans7b9c1392017-04-09 20:40:39 +0200212
213 // Port VC0 Resource Control Register
Angel Pons2048cb42020-06-08 02:09:33 +0200214 pci_update_config32(dev, 0x114, ~0x000000ff, 1);
Arthur Heymans7b9c1392017-04-09 20:40:39 +0200215
216 // VCi traffic class
Angel Pons7a2864b2020-06-21 13:29:28 +0200217 pci_or_config8(dev, 0x44, 7 << 0); // TC7
Arthur Heymans7b9c1392017-04-09 20:40:39 +0200218
219 // VCi Resource Control
Angel Pons2048cb42020-06-08 02:09:33 +0200220 pci_or_config32(dev, 0x120, (1 << 31) | (1 << 24) | (0x80 << 0)); /* VCi ID and map */
Arthur Heymans7b9c1392017-04-09 20:40:39 +0200221
222 /* Set Bus Master */
Elyes HAOUASca4ff252020-04-28 10:29:11 +0200223 pci_or_config16(dev, PCI_COMMAND, PCI_COMMAND_MASTER);
Arthur Heymans7b9c1392017-04-09 20:40:39 +0200224
Angel Pons2048cb42020-06-08 02:09:33 +0200225 // Docking not supported
226 pci_and_config8(dev, 0x4d, (u8)~(1 << 7)); // Docking Status
Arthur Heymans7b9c1392017-04-09 20:40:39 +0200227
228 /* Lock some R/WO bits by writing their current value. */
Angel Pons2048cb42020-06-08 02:09:33 +0200229 pci_update_config32(dev, 0x74, ~0, 0);
Arthur Heymans7b9c1392017-04-09 20:40:39 +0200230
231 res = find_resource(dev, 0x10);
232 if (!res)
233 return;
234
235 // NOTE this will break as soon as the Azalia get's a bar above
236 // 4G. Is there anything we can do about it?
237 base = res2mmio(res, 0, 0);
Angel Pons7a2864b2020-06-21 13:29:28 +0200238 printk(BIOS_DEBUG, "Azalia: base = %p\n", base);
Arthur Heymans7b9c1392017-04-09 20:40:39 +0200239 codec_mask = codec_detect(base);
240
241 if (codec_mask) {
242 printk(BIOS_DEBUG, "Azalia: codec_mask = %02x\n", codec_mask);
243 codecs_init(dev, base, codec_mask);
244 }
245}
246
Arthur Heymans7b9c1392017-04-09 20:40:39 +0200247static struct device_operations azalia_ops = {
248 .read_resources = pci_dev_read_resources,
249 .set_resources = pci_dev_set_resources,
250 .enable_resources = pci_dev_enable_resources,
251 .init = azalia_init,
Angel Pons1fc0edd2020-05-31 00:03:28 +0200252 .ops_pci = &pci_dev_ops_pci,
Arthur Heymans7b9c1392017-04-09 20:40:39 +0200253};
254
Arthur Heymans349e0852017-04-09 20:48:37 +0200255static const unsigned short pci_device_ids[] = {
256 0x3a3e,
257 0x3a6e,
258 0
259};
260
261static const struct pci_driver i82801jx_azalia __pci_driver = {
Arthur Heymans7b9c1392017-04-09 20:40:39 +0200262 .ops = &azalia_ops,
263 .vendor = PCI_VENDOR_ID_INTEL,
Arthur Heymans349e0852017-04-09 20:48:37 +0200264 .devices = pci_device_ids,
Arthur Heymans7b9c1392017-04-09 20:40:39 +0200265};