blob: b91151aee40109ee70b808947d6f4608d94c4f75 [file] [log] [blame]
Angel Pons182dbde2020-04-02 23:49:05 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Stefan Reinauer8e073822012-04-04 00:07:22 +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>
Stefan Reinauer8e073822012-04-04 00:07:22 +02009#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
12#include "chip.h"
Stefan Reinauer8e073822012-04-04 00:07:22 +020013#include "pch.h"
14
Stefan Reinauer8e073822012-04-04 00:07:22 +020015typedef struct southbridge_intel_bd82x6x_config config_t;
16
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080017static int set_bits(void *port, u32 mask, u32 val)
Stefan Reinauer8e073822012-04-04 00:07:22 +020018{
19 u32 reg32;
20 int count;
21
22 /* Write (val & mask) to port */
23 val &= mask;
24 reg32 = read32(port);
25 reg32 &= ~mask;
26 reg32 |= val;
27 write32(port, reg32);
28
Elyes HAOUAS6ea24ff2020-08-11 09:21:24 +020029 /* Wait for readback of register to match what was just written to it */
Stefan Reinauer8e073822012-04-04 00:07:22 +020030 count = 50;
31 do {
32 /* Wait 1ms based on BKDG wait time */
33 mdelay(1);
34 reg32 = read32(port);
35 reg32 &= mask;
36 } while ((reg32 != val) && --count);
37
38 /* Timeout occurred */
39 if (!count)
40 return -1;
41 return 0;
42}
43
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080044static int codec_detect(u8 *base)
Stefan Reinauer8e073822012-04-04 00:07:22 +020045{
46 u8 reg8;
47
48 /* Set Bit 0 to 1 to exit reset state (BAR + 0x8)[0] */
Angel Pons554713e2020-10-24 23:23:07 +020049 if (set_bits(base + HDA_GCTL_REG, 1, HDA_GCTL_CRST) < 0)
Stefan Reinauer8e073822012-04-04 00:07:22 +020050 goto no_codec;
51
52 /* Write back the value once reset bit is set. */
Elyes HAOUAS6ea24ff2020-08-11 09:21:24 +020053 write16(base + HDA_GCAP_REG, read16(base + HDA_GCAP_REG));
Stefan Reinauer8e073822012-04-04 00:07:22 +020054
Elyes HAOUAS6ea24ff2020-08-11 09:21:24 +020055 /* Read in Codec location (BAR + 0xe)[2..0] */
Elyes HAOUAS11178bd2020-08-03 15:34:46 +020056 reg8 = read8(base + HDA_STATESTS_REG);
Stefan Reinauer8e073822012-04-04 00:07:22 +020057 reg8 &= 0x0f;
58 if (!reg8)
59 goto no_codec;
60
61 return reg8;
62
63no_codec:
64 /* Codec Not found */
65 /* Put HDA back in reset (BAR + 0x8) [0] */
Elyes HAOUAS11178bd2020-08-03 15:34:46 +020066 set_bits(base + HDA_GCTL_REG, 1, 0);
Stefan Reinauer8e073822012-04-04 00:07:22 +020067 printk(BIOS_DEBUG, "Azalia: No codec!\n");
68 return 0;
69}
70
Elyes HAOUASe414a4e2019-01-03 10:40:43 +010071static u32 find_verb(struct device *dev, u32 viddid, const u32 **verb)
Stefan Reinauer8e073822012-04-04 00:07:22 +020072{
Elyes HAOUAS6ea24ff2020-08-11 09:21:24 +020073 int idx = 0;
Stefan Reinauer8e073822012-04-04 00:07:22 +020074
75 while (idx < (cim_verb_data_size / sizeof(u32))) {
Elyes HAOUAS6ea24ff2020-08-11 09:21:24 +020076 u32 verb_size = 4 * cim_verb_data[idx + 2]; // in u32
Stefan Reinauer8e073822012-04-04 00:07:22 +020077 if (cim_verb_data[idx] != viddid) {
Elyes HAOUAS6ea24ff2020-08-11 09:21:24 +020078 idx += verb_size + 3; // skip verb + header
Stefan Reinauer8e073822012-04-04 00:07:22 +020079 continue;
80 }
Elyes HAOUAS6ea24ff2020-08-11 09:21:24 +020081 *verb = &cim_verb_data[idx + 3];
Stefan Reinauer8e073822012-04-04 00:07:22 +020082 return verb_size;
83 }
84
85 /* Not all codecs need to load another verb */
86 return 0;
87}
88
Elyes HAOUAS6ea24ff2020-08-11 09:21:24 +020089/*
90 * Wait 50usec for the codec to indicate it is ready.
91 * No response would imply that the codec is non-operative.
Stefan Reinauer8e073822012-04-04 00:07:22 +020092 */
93
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080094static int wait_for_ready(u8 *base)
Stefan Reinauer8e073822012-04-04 00:07:22 +020095{
Stefan Reinauer15ba2bc2012-11-14 12:25:15 -080096 /* Use a 1msec timeout */
Stefan Reinauer15ba2bc2012-11-14 12:25:15 -080097 int timeout = 1000;
Stefan Reinauer8e073822012-04-04 00:07:22 +020098
Elyes HAOUASba28e8d2016-08-31 19:22:16 +020099 while (timeout--) {
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800100 u32 reg32 = read32(base + HDA_ICII_REG);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200101 if (!(reg32 & HDA_ICII_BUSY))
102 return 0;
103 udelay(1);
104 }
105
106 return -1;
107}
108
Elyes HAOUAS6ea24ff2020-08-11 09:21:24 +0200109/*
110 * Wait 50usec for the codec to indicate that it accepted the previous command.
111 * No response would imply that the code is non-operative.
Stefan Reinauer8e073822012-04-04 00:07:22 +0200112 */
113
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800114static int wait_for_valid(u8 *base)
Stefan Reinauer8e073822012-04-04 00:07:22 +0200115{
116 u32 reg32;
Elyes HAOUAS6ea24ff2020-08-11 09:21:24 +0200117 /* Use a 1msec timeout */
118 int timeout = 1000;
Stefan Reinauer8e073822012-04-04 00:07:22 +0200119
120 /* Send the verb to the codec */
121 reg32 = read32(base + HDA_ICII_REG);
122 reg32 |= HDA_ICII_BUSY | HDA_ICII_VALID;
123 write32(base + HDA_ICII_REG, reg32);
124
Elyes HAOUASba28e8d2016-08-31 19:22:16 +0200125 while (timeout--) {
Stefan Reinauer8e073822012-04-04 00:07:22 +0200126 reg32 = read32(base + HDA_ICII_REG);
Elyes HAOUAS6ea24ff2020-08-11 09:21:24 +0200127 if ((reg32 & (HDA_ICII_VALID | HDA_ICII_BUSY)) == HDA_ICII_VALID)
Stefan Reinauer8e073822012-04-04 00:07:22 +0200128 return 0;
129 udelay(1);
130 }
131
132 return -1;
133}
134
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800135static void codec_init(struct device *dev, u8 *base, int addr)
Stefan Reinauer8e073822012-04-04 00:07:22 +0200136{
137 u32 reg32;
138 const u32 *verb;
139 u32 verb_size;
140 int i;
141
142 printk(BIOS_DEBUG, "Azalia: Initializing codec #%d\n", addr);
143
144 /* 1 */
Angel Pons554713e2020-10-24 23:23:07 +0200145 if (wait_for_ready(base) < 0) {
Stefan Reinauer8e073822012-04-04 00:07:22 +0200146 printk(BIOS_DEBUG, " codec not ready.\n");
147 return;
148 }
149
150 reg32 = (addr << 28) | 0x000f0000;
Elyes HAOUAS11178bd2020-08-03 15:34:46 +0200151 write32(base + HDA_IC_REG, reg32);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200152
Angel Pons554713e2020-10-24 23:23:07 +0200153 if (wait_for_valid(base) < 0) {
Stefan Reinauer8e073822012-04-04 00:07:22 +0200154 printk(BIOS_DEBUG, " codec not valid.\n");
155 return;
156 }
157
Stefan Reinauer8e073822012-04-04 00:07:22 +0200158 /* 2 */
Elyes HAOUAS6ea24ff2020-08-11 09:21:24 +0200159 reg32 = read32(base + HDA_IR_REG);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200160 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++) {
Angel Pons554713e2020-10-24 23:23:07 +0200171 if (wait_for_ready(base) < 0)
Stefan Reinauer8e073822012-04-04 00:07:22 +0200172 return;
173
Elyes HAOUAS11178bd2020-08-03 15:34:46 +0200174 write32(base + HDA_IC_REG, verb[i]);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200175
Angel Pons554713e2020-10-24 23:23:07 +0200176 if (wait_for_valid(base) < 0)
Stefan Reinauer8e073822012-04-04 00:07:22 +0200177 return;
178 }
179 printk(BIOS_DEBUG, "Azalia: verb loaded.\n");
180}
181
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800182static void codecs_init(struct device *dev, u8 *base, u32 codec_mask)
Stefan Reinauer8e073822012-04-04 00:07:22 +0200183{
184 int i;
185 for (i = 3; i >= 0; i--) {
186 if (codec_mask & (1 << i))
187 codec_init(dev, base, i);
188 }
Dylan Reidb98d0782012-04-27 11:37:33 -0700189
190 for (i = 0; i < pc_beep_verbs_size; i++) {
Angel Pons554713e2020-10-24 23:23:07 +0200191 if (wait_for_ready(base) < 0)
Dylan Reidb98d0782012-04-27 11:37:33 -0700192 return;
193
Elyes HAOUAS11178bd2020-08-03 15:34:46 +0200194 write32(base + HDA_IC_REG, pc_beep_verbs[i]);
Dylan Reidb98d0782012-04-27 11:37:33 -0700195
Angel Pons554713e2020-10-24 23:23:07 +0200196 if (wait_for_valid(base) < 0)
Dylan Reidb98d0782012-04-27 11:37:33 -0700197 return;
198 }
Stefan Reinauer8e073822012-04-04 00:07:22 +0200199}
200
201static void azalia_init(struct device *dev)
202{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800203 u8 *base;
Stefan Reinauer8e073822012-04-04 00:07:22 +0200204 struct resource *res;
205 u32 codec_mask;
Stefan Reinauer8e073822012-04-04 00:07:22 +0200206 u32 reg32;
207
Stefan Reinauer8e073822012-04-04 00:07:22 +0200208 res = find_resource(dev, PCI_BASE_ADDRESS_0);
209 if (!res)
210 return;
211
Elyes HAOUAS6ea24ff2020-08-11 09:21:24 +0200212 // NOTE this will break as soon as the Azalia get's a bar above 4G.
213 // Is there anything we can do about it?
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800214 base = res2mmio(res, 0, 0);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200215 printk(BIOS_DEBUG, "Azalia: base = %08x\n", (u32)base);
216
Patrick Rudolph4f8b1082019-07-14 11:54:58 +0200217 if (RCBA32(CIR31) & (1 << 31)) {
Kyösti Mälkkifd98c652013-07-26 08:50:53 +0300218 reg32 = pci_read_config32(dev, 0x120);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200219 reg32 &= 0xf8ffff01;
Stefan Reinauer15ba2bc2012-11-14 12:25:15 -0800220 reg32 |= (1 << 24); // 2 << 24 for server
Patrick Rudolph4f8b1082019-07-14 11:54:58 +0200221 reg32 |= RCBA32(CIR31) & 0xfe;
Kyösti Mälkkifd98c652013-07-26 08:50:53 +0300222 pci_write_config32(dev, 0x120, reg32);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200223
Angel Ponsc803f652020-06-07 22:09:01 +0200224 pci_or_config16(dev, 0x78, 1 << 11);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200225 } else
226 printk(BIOS_DEBUG, "Azalia: V1CTL disabled.\n");
227
Angel Ponsc803f652020-06-07 22:09:01 +0200228 pci_and_config32(dev, 0x114, ~0xfe);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200229
230 // Set VCi enable bit
Angel Ponsc803f652020-06-07 22:09:01 +0200231 pci_or_config32(dev, 0x120, 1 << 31);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200232
233 // Enable HDMI codec:
Angel Ponsc803f652020-06-07 22:09:01 +0200234 pci_or_config32(dev, 0xc4, 1 << 1);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200235
Angel Ponsc803f652020-06-07 22:09:01 +0200236 pci_or_config8(dev, 0x43, 1 << 6);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200237
238 /* Additional programming steps */
Angel Ponsc803f652020-06-07 22:09:01 +0200239 pci_or_config32(dev, 0xc4, 1 << 13);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200240
Angel Ponsc803f652020-06-07 22:09:01 +0200241 pci_or_config32(dev, 0xc4, 1 << 10);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200242
Angel Ponsc803f652020-06-07 22:09:01 +0200243 pci_and_config32(dev, 0xd0, ~(1 << 31));
Stefan Reinauer8e073822012-04-04 00:07:22 +0200244
Stefan Reinauer15ba2bc2012-11-14 12:25:15 -0800245 if (dev->device == 0x1e20) {
246 /* Additional step on Panther Point */
Angel Ponsc803f652020-06-07 22:09:01 +0200247 pci_or_config32(dev, 0xc4, 1 << 17);
Stefan Reinauer15ba2bc2012-11-14 12:25:15 -0800248 }
249
Stefan Reinauer8e073822012-04-04 00:07:22 +0200250 /* Set Bus Master */
Elyes HAOUAS729c0692020-04-28 19:50:44 +0200251 pci_or_config16(dev, PCI_COMMAND, PCI_COMMAND_MASTER);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200252
253 pci_write_config8(dev, 0x3c, 0x0a); // unused?
254
255 /* Codec Initialization Programming Sequence */
Stefan Reinauer15ba2bc2012-11-14 12:25:15 -0800256
257 /* Take controller out of reset */
Elyes HAOUAS11178bd2020-08-03 15:34:46 +0200258 reg32 = read32(base + HDA_GCTL_REG);
259 reg32 |= HDA_GCTL_CRST;
260 write32(base + HDA_GCTL_REG, reg32);
Stefan Reinauer15ba2bc2012-11-14 12:25:15 -0800261 /* Wait 1ms */
262 udelay(1000);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200263
Angel Ponsc803f652020-06-07 22:09:01 +0200264 // Select Azalia mode. This needs to be controlled via devicetree.cb
265 pci_or_config8(dev, 0x40, 1); // Audio Control
Stefan Reinauer8e073822012-04-04 00:07:22 +0200266
Angel Ponsc803f652020-06-07 22:09:01 +0200267 // Docking not supported
268 pci_and_config8(dev, 0x4d, (u8)~(1 << 7)); // Docking Status
Stefan Reinauer8e073822012-04-04 00:07:22 +0200269
270 codec_mask = codec_detect(base);
271
272 if (codec_mask) {
273 printk(BIOS_DEBUG, "Azalia: codec_mask = %02x\n", codec_mask);
274 codecs_init(dev, base, codec_mask);
275 }
276
277 /* Enable dynamic clock gating */
Angel Ponsc803f652020-06-07 22:09:01 +0200278 pci_update_config8(dev, 0x43, ~0x07, (1 << 2) | (1 << 0));
Stefan Reinauer8e073822012-04-04 00:07:22 +0200279}
280
Aaron Durbinaa090cb2017-09-13 16:01:52 -0600281static const char *azalia_acpi_name(const struct device *dev)
Patrick Rudolph604f6982017-06-07 09:46:52 +0200282{
283 return "HDEF";
284}
285
Stefan Reinauer8e073822012-04-04 00:07:22 +0200286static struct device_operations azalia_ops = {
287 .read_resources = pci_dev_read_resources,
288 .set_resources = pci_dev_set_resources,
289 .enable_resources = pci_dev_enable_resources,
290 .init = azalia_init,
Angel Pons1fc0edd2020-05-31 00:03:28 +0200291 .ops_pci = &pci_dev_ops_pci,
Patrick Rudolph604f6982017-06-07 09:46:52 +0200292 .acpi_name = azalia_acpi_name,
Stefan Reinauer8e073822012-04-04 00:07:22 +0200293};
294
Stefan Reinauer9a380ab2012-06-22 13:16:11 -0700295static const unsigned short pci_device_ids[] = { 0x1c20, 0x1e20, 0 };
Stefan Reinauer8e073822012-04-04 00:07:22 +0200296
Stefan Reinauer9a380ab2012-06-22 13:16:11 -0700297static const struct pci_driver pch_azalia __pci_driver = {
298 .ops = &azalia_ops,
299 .vendor = PCI_VENDOR_ID_INTEL,
300 .devices = pci_device_ids,
Stefan Reinauer8e073822012-04-04 00:07:22 +0200301};