blob: 0e0ccd6b1afc7f75c6c6a3b1426d93dd128eca12 [file] [log] [blame]
Angel Pons182dbde2020-04-02 23:49:05 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Patrick Georgie72a8a32012-11-06 11:05:09 +01002
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>
Patrick Georgie72a8a32012-11-06 11:05:09 +01009#include <delay.h>
Vladimir Serbinenko75c83872014-09-05 01:01:31 +020010#include <device/azalia_device.h>
Kyösti Mälkki12b121c2019-08-18 16:33:39 +030011#include "chip.h"
Patrick Georgie72a8a32012-11-06 11:05:09 +010012#include "i82801ix.h"
13
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080014static int set_bits(void *port, u32 mask, u32 val)
Patrick Georgie72a8a32012-11-06 11:05:09 +010015{
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
Angel Ponsc9d63332020-06-21 15:38:29 +020026 /* Wait for readback of register to match what was just written to it */
Patrick Georgie72a8a32012-11-06 11:05:09 +010027 count = 50;
28 do {
29 /* Wait 1ms based on BKDG wait time */
30 mdelay(1);
31 reg32 = read32(port);
32 reg32 &= mask;
33 } while ((reg32 != val) && --count);
34
35 /* Timeout occurred */
36 if (!count)
37 return -1;
38 return 0;
39}
40
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080041static int codec_detect(u8 *base)
Patrick Georgie72a8a32012-11-06 11:05:09 +010042{
43 u32 reg32;
44
Elyes HAOUAS6ea24ff2020-08-11 09:21:24 +020045 /* Set Bit 0 to 0 to enter reset state (BAR + 0x8)[0] */
Angel Pons554713e2020-10-24 23:23:07 +020046 if (set_bits(base + HDA_GCTL_REG, 1, 0) < 0)
Patrick Georgie72a8a32012-11-06 11:05:09 +010047 goto no_codec;
48
49 /* Set Bit 0 to 1 to exit reset state (BAR + 0x8)[0] */
Angel Pons554713e2020-10-24 23:23:07 +020050 if (set_bits(base + HDA_GCTL_REG, 1, HDA_GCTL_CRST) < 0)
Patrick Georgie72a8a32012-11-06 11:05:09 +010051 goto no_codec;
52
Elyes HAOUAS6ea24ff2020-08-11 09:21:24 +020053 /* Read in Codec location (BAR + 0xe)[2..0] */
Elyes HAOUASe5954ba2020-08-03 15:35:47 +020054 reg32 = read32(base + HDA_STATESTS_REG);
Patrick Georgie72a8a32012-11-06 11:05:09 +010055 reg32 &= 0x0f;
56 if (!reg32)
57 goto no_codec;
58
59 return reg32;
60
61no_codec:
62 /* Codec Not found */
63 /* Put HDA back in reset (BAR + 0x8) [0] */
Elyes HAOUASe5954ba2020-08-03 15:35:47 +020064 set_bits(base + HDA_GCTL_REG, 1, 0);
Patrick Georgie72a8a32012-11-06 11:05:09 +010065 printk(BIOS_DEBUG, "Azalia: No codec!\n");
66 return 0;
67}
68
Elyes HAOUASe414a4e2019-01-03 10:40:43 +010069static u32 find_verb(struct device *dev, u32 viddid, const u32 **verb)
Patrick Georgie72a8a32012-11-06 11:05:09 +010070{
Angel Ponsc9d63332020-06-21 15:38:29 +020071 int idx = 0;
Patrick Georgie72a8a32012-11-06 11:05:09 +010072
73 while (idx < (cim_verb_data_size / sizeof(u32))) {
Elyes HAOUAS6ea24ff2020-08-11 09:21:24 +020074 u32 verb_size = 4 * cim_verb_data[idx + 2]; // in u32
Patrick Georgie72a8a32012-11-06 11:05:09 +010075 if (cim_verb_data[idx] != viddid) {
Elyes HAOUAS6ea24ff2020-08-11 09:21:24 +020076 idx += verb_size + 3; // skip verb + header
Patrick Georgie72a8a32012-11-06 11:05:09 +010077 continue;
78 }
Elyes HAOUAS6ea24ff2020-08-11 09:21:24 +020079 *verb = &cim_verb_data[idx + 3];
Patrick Georgie72a8a32012-11-06 11:05:09 +010080 return verb_size;
81 }
82
83 /* Not all codecs need to load another verb */
84 return 0;
85}
86
Elyes HAOUAS6ea24ff2020-08-11 09:21:24 +020087/*
88 * Wait 50usec for the codec to indicate it is ready.
89 * No response would imply that the codec is non-operative.
Patrick Georgie72a8a32012-11-06 11:05:09 +010090 */
91
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080092static int wait_for_ready(u8 *base)
Patrick Georgie72a8a32012-11-06 11:05:09 +010093{
Angel Ponsc9d63332020-06-21 15:38:29 +020094 /* Use a 50 usec timeout - the Linux kernel uses the same duration */
Patrick Georgie72a8a32012-11-06 11:05:09 +010095 int timeout = 50;
96
Elyes HAOUASba28e8d2016-08-31 19:22:16 +020097 while (timeout--) {
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080098 u32 reg32 = read32(base + HDA_ICII_REG);
Patrick Georgie72a8a32012-11-06 11:05:09 +010099 if (!(reg32 & HDA_ICII_BUSY))
100 return 0;
101 udelay(1);
102 }
103
104 return -1;
105}
106
Elyes HAOUAS6ea24ff2020-08-11 09:21:24 +0200107/*
108 * Wait 50usec for the codec to indicate that it accepted the previous command.
109 * No response would imply that the code is non-operative.
Patrick Georgie72a8a32012-11-06 11:05:09 +0100110 */
111
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800112static int wait_for_valid(u8 *base)
Patrick Georgie72a8a32012-11-06 11:05:09 +0100113{
114 u32 reg32;
Elyes HAOUAS6ea24ff2020-08-11 09:21:24 +0200115 /* Use a 50 usec timeout - the Linux kernel uses the same duration */
116 int timeout = 50;
Patrick Georgie72a8a32012-11-06 11:05:09 +0100117
118 /* Send the verb to the codec */
Elyes HAOUASe5954ba2020-08-03 15:35:47 +0200119 reg32 = read32(base + HDA_ICII_REG);
120 reg32 |= HDA_ICII_BUSY | HDA_ICII_VALID;
121 write32(base + HDA_ICII_REG, reg32);
Patrick Georgie72a8a32012-11-06 11:05:09 +0100122
Elyes HAOUASba28e8d2016-08-31 19:22:16 +0200123 while (timeout--) {
Patrick Georgie72a8a32012-11-06 11:05:09 +0100124 reg32 = read32(base + HDA_ICII_REG);
Angel Ponsc9d63332020-06-21 15:38:29 +0200125 if ((reg32 & (HDA_ICII_VALID | HDA_ICII_BUSY)) == HDA_ICII_VALID)
Patrick Georgie72a8a32012-11-06 11:05:09 +0100126 return 0;
127 udelay(1);
128 }
129
130 return -1;
131}
132
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800133static void codec_init(struct device *dev, u8 *base, int addr)
Patrick Georgie72a8a32012-11-06 11:05:09 +0100134{
135 u32 reg32;
136 const u32 *verb;
137 u32 verb_size;
138 int i;
139
Angel Ponsaaa8ab72020-06-21 15:33:24 +0200140 printk(BIOS_DEBUG, "Azalia: Initializing codec #%d\n", addr);
Patrick Georgie72a8a32012-11-06 11:05:09 +0100141
142 /* 1 */
Angel Pons554713e2020-10-24 23:23:07 +0200143 if (wait_for_ready(base) < 0) {
Elyes HAOUAS6ea24ff2020-08-11 09:21:24 +0200144 printk(BIOS_DEBUG, " codec not ready.\n");
Patrick Georgie72a8a32012-11-06 11:05:09 +0100145 return;
Elyes HAOUAS6ea24ff2020-08-11 09:21:24 +0200146 }
Patrick Georgie72a8a32012-11-06 11:05:09 +0100147
148 reg32 = (addr << 28) | 0x000f0000;
Elyes HAOUASe5954ba2020-08-03 15:35:47 +0200149 write32(base + HDA_IC_REG, reg32);
Patrick Georgie72a8a32012-11-06 11:05:09 +0100150
Angel Pons554713e2020-10-24 23:23:07 +0200151 if (wait_for_valid(base) < 0) {
Elyes HAOUAS6ea24ff2020-08-11 09:21:24 +0200152 printk(BIOS_DEBUG, " codec not valid.\n");
Patrick Georgie72a8a32012-11-06 11:05:09 +0100153 return;
Elyes HAOUAS6ea24ff2020-08-11 09:21:24 +0200154 }
Patrick Georgie72a8a32012-11-06 11:05:09 +0100155
156 /* 2 */
Elyes HAOUAS6ea24ff2020-08-11 09:21:24 +0200157 reg32 = read32(base + HDA_IR_REG);
Patrick Georgie72a8a32012-11-06 11:05:09 +0100158 printk(BIOS_DEBUG, "Azalia: codec viddid: %08x\n", reg32);
159 verb_size = find_verb(dev, reg32, &verb);
160
161 if (!verb_size) {
162 printk(BIOS_DEBUG, "Azalia: No verb!\n");
163 return;
164 }
165 printk(BIOS_DEBUG, "Azalia: verb_size: %d\n", verb_size);
166
167 /* 3 */
168 for (i = 0; i < verb_size; i++) {
Angel Pons554713e2020-10-24 23:23:07 +0200169 if (wait_for_ready(base) < 0)
Patrick Georgie72a8a32012-11-06 11:05:09 +0100170 return;
171
Elyes HAOUASe5954ba2020-08-03 15:35:47 +0200172 write32(base + HDA_IC_REG, verb[i]);
Patrick Georgie72a8a32012-11-06 11:05:09 +0100173
Angel Pons554713e2020-10-24 23:23:07 +0200174 if (wait_for_valid(base) < 0)
Patrick Georgie72a8a32012-11-06 11:05:09 +0100175 return;
176 }
177 printk(BIOS_DEBUG, "Azalia: verb loaded.\n");
178}
179
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800180static void codecs_init(struct device *dev, u8 *base, u32 codec_mask)
Patrick Georgie72a8a32012-11-06 11:05:09 +0100181{
182 int i;
Elyes HAOUAS6ea24ff2020-08-11 09:21:24 +0200183
Patrick Georgie72a8a32012-11-06 11:05:09 +0100184 for (i = 2; i >= 0; i--) {
185 if (codec_mask & (1 << i))
186 codec_init(dev, base, i);
187 }
188
189 for (i = 0; i < pc_beep_verbs_size; i++) {
Angel Pons554713e2020-10-24 23:23:07 +0200190 if (wait_for_ready(base) < 0)
Patrick Georgie72a8a32012-11-06 11:05:09 +0100191 return;
192
Elyes HAOUASe5954ba2020-08-03 15:35:47 +0200193 write32(base + HDA_IC_REG, pc_beep_verbs[i]);
Patrick Georgie72a8a32012-11-06 11:05:09 +0100194
Angel Pons554713e2020-10-24 23:23:07 +0200195 if (wait_for_valid(base) < 0)
Patrick Georgie72a8a32012-11-06 11:05:09 +0100196 return;
197 }
198}
199
200static void azalia_init(struct device *dev)
201{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800202 u8 *base;
Patrick Georgie72a8a32012-11-06 11:05:09 +0100203 struct resource *res;
204 u32 codec_mask;
Patrick Georgie72a8a32012-11-06 11:05:09 +0100205
Patrick Georgie72a8a32012-11-06 11:05:09 +0100206 // ESD
Angel Pons67406472020-06-08 11:13:42 +0200207 pci_update_config32(dev, 0x134, ~0x00ff0000, 2 << 16);
Patrick Georgie72a8a32012-11-06 11:05:09 +0100208
209 // Link1 description
Angel Pons67406472020-06-08 11:13:42 +0200210 pci_update_config32(dev, 0x140, ~0x00ff0000, 2 << 16);
Patrick Georgie72a8a32012-11-06 11:05:09 +0100211
212 // Port VC0 Resource Control Register
Angel Pons67406472020-06-08 11:13:42 +0200213 pci_update_config32(dev, 0x114, ~0x000000ff, 1);
Patrick Georgie72a8a32012-11-06 11:05:09 +0100214
215 // VCi traffic class
Angel Pons67406472020-06-08 11:13:42 +0200216 pci_or_config8(dev, 0x44, 7 << 0); // TC7
Patrick Georgie72a8a32012-11-06 11:05:09 +0100217
218 // VCi Resource Control
Angel Pons67406472020-06-08 11:13:42 +0200219 pci_or_config32(dev, 0x120, (1 << 31) | (1 << 24) | (0x80 << 0)); /* VCi ID and map */
Patrick Georgie72a8a32012-11-06 11:05:09 +0100220
221 /* Set Bus Master */
Elyes HAOUASb9d2e222020-04-28 10:25:12 +0200222 pci_or_config16(dev, PCI_COMMAND, PCI_COMMAND_MASTER);
Patrick Georgie72a8a32012-11-06 11:05:09 +0100223
Angel Pons67406472020-06-08 11:13:42 +0200224 // Docking not supported
225 pci_and_config8(dev, 0x4d, (u8)~(1 << 7)); // Docking Status
Patrick Georgie72a8a32012-11-06 11:05:09 +0100226
227 /* Lock some R/WO bits by writing their current value. */
Angel Pons67406472020-06-08 11:13:42 +0200228 pci_update_config32(dev, 0x74, ~0, 0);
Patrick Georgie72a8a32012-11-06 11:05:09 +0100229
Elyes HAOUAS6ea24ff2020-08-11 09:21:24 +0200230 res = find_resource(dev, PCI_BASE_ADDRESS_0);
Patrick Georgie72a8a32012-11-06 11:05:09 +0100231 if (!res)
232 return;
233
Elyes HAOUAS6ea24ff2020-08-11 09:21:24 +0200234 // NOTE this will break as soon as the Azalia get's a bar above 4G.
235 // Is there anything we can do about it?
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800236 base = res2mmio(res, 0, 0);
Patrick Rudolph4af2add2018-11-26 15:56:11 +0100237 printk(BIOS_DEBUG, "Azalia: base = %p\n", base);
Patrick Georgie72a8a32012-11-06 11:05:09 +0100238 codec_mask = codec_detect(base);
239
240 if (codec_mask) {
241 printk(BIOS_DEBUG, "Azalia: codec_mask = %02x\n", codec_mask);
242 codecs_init(dev, base, codec_mask);
243 }
244}
245
Patrick Georgie72a8a32012-11-06 11:05:09 +0100246static struct device_operations azalia_ops = {
247 .read_resources = pci_dev_read_resources,
248 .set_resources = pci_dev_set_resources,
249 .enable_resources = pci_dev_enable_resources,
250 .init = azalia_init,
Angel Pons1fc0edd2020-05-31 00:03:28 +0200251 .ops_pci = &pci_dev_ops_pci,
Patrick Georgie72a8a32012-11-06 11:05:09 +0100252};
253
254/* ICH9DH/ICH9DO/ICH9R/ICH9/ICH9M-E/ICH9M */
255static const struct pci_driver i82801ix_azalia __pci_driver = {
256 .ops = &azalia_ops,
257 .vendor = PCI_VENDOR_ID_INTEL,
Felix Singer7f8b0cd82019-11-10 11:04:08 +0100258 .device = PCI_DEVICE_ID_INTEL_82801IB_HD_AUDIO,
Patrick Georgie72a8a32012-11-06 11:05:09 +0100259};