blob: 5e810c4cc0f7fc00aa12d8bf83f432705a136053 [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 codec_detect(u8 *base)
Stefan Reinauer8e073822012-04-04 00:07:22 +020018{
19 u8 reg8;
20
21 /* Set Bit 0 to 1 to exit reset state (BAR + 0x8)[0] */
Angel Pons61dd8362020-12-05 18:02:32 +010022 if (azalia_set_bits(base + HDA_GCTL_REG, HDA_GCTL_CRST, HDA_GCTL_CRST) < 0)
Stefan Reinauer8e073822012-04-04 00:07:22 +020023 goto no_codec;
24
25 /* Write back the value once reset bit is set. */
Elyes HAOUAS6ea24ff2020-08-11 09:21:24 +020026 write16(base + HDA_GCAP_REG, read16(base + HDA_GCAP_REG));
Stefan Reinauer8e073822012-04-04 00:07:22 +020027
Elyes HAOUAS6ea24ff2020-08-11 09:21:24 +020028 /* Read in Codec location (BAR + 0xe)[2..0] */
Elyes HAOUAS11178bd2020-08-03 15:34:46 +020029 reg8 = read8(base + HDA_STATESTS_REG);
Stefan Reinauer8e073822012-04-04 00:07:22 +020030 reg8 &= 0x0f;
31 if (!reg8)
32 goto no_codec;
33
34 return reg8;
35
36no_codec:
37 /* Codec Not found */
38 /* Put HDA back in reset (BAR + 0x8) [0] */
Angel Pons61dd8362020-12-05 18:02:32 +010039 azalia_set_bits(base + HDA_GCTL_REG, 1, 0);
Stefan Reinauer8e073822012-04-04 00:07:22 +020040 printk(BIOS_DEBUG, "Azalia: No codec!\n");
41 return 0;
42}
43
Elyes HAOUASe414a4e2019-01-03 10:40:43 +010044static u32 find_verb(struct device *dev, u32 viddid, const u32 **verb)
Stefan Reinauer8e073822012-04-04 00:07:22 +020045{
Elyes HAOUAS6ea24ff2020-08-11 09:21:24 +020046 int idx = 0;
Stefan Reinauer8e073822012-04-04 00:07:22 +020047
48 while (idx < (cim_verb_data_size / sizeof(u32))) {
Elyes HAOUAS6ea24ff2020-08-11 09:21:24 +020049 u32 verb_size = 4 * cim_verb_data[idx + 2]; // in u32
Stefan Reinauer8e073822012-04-04 00:07:22 +020050 if (cim_verb_data[idx] != viddid) {
Elyes HAOUAS6ea24ff2020-08-11 09:21:24 +020051 idx += verb_size + 3; // skip verb + header
Stefan Reinauer8e073822012-04-04 00:07:22 +020052 continue;
53 }
Elyes HAOUAS6ea24ff2020-08-11 09:21:24 +020054 *verb = &cim_verb_data[idx + 3];
Stefan Reinauer8e073822012-04-04 00:07:22 +020055 return verb_size;
56 }
57
58 /* Not all codecs need to load another verb */
59 return 0;
60}
61
Elyes HAOUAS6ea24ff2020-08-11 09:21:24 +020062/*
63 * Wait 50usec for the codec to indicate it is ready.
64 * No response would imply that the codec is non-operative.
Stefan Reinauer8e073822012-04-04 00:07:22 +020065 */
66
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080067static int wait_for_ready(u8 *base)
Stefan Reinauer8e073822012-04-04 00:07:22 +020068{
Stefan Reinauer15ba2bc2012-11-14 12:25:15 -080069 /* Use a 1msec timeout */
Stefan Reinauer15ba2bc2012-11-14 12:25:15 -080070 int timeout = 1000;
Stefan Reinauer8e073822012-04-04 00:07:22 +020071
Elyes HAOUASba28e8d2016-08-31 19:22:16 +020072 while (timeout--) {
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080073 u32 reg32 = read32(base + HDA_ICII_REG);
Stefan Reinauer8e073822012-04-04 00:07:22 +020074 if (!(reg32 & HDA_ICII_BUSY))
75 return 0;
76 udelay(1);
77 }
78
79 return -1;
80}
81
Elyes HAOUAS6ea24ff2020-08-11 09:21:24 +020082/*
83 * Wait 50usec for the codec to indicate that it accepted the previous command.
84 * No response would imply that the code is non-operative.
Stefan Reinauer8e073822012-04-04 00:07:22 +020085 */
86
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080087static int wait_for_valid(u8 *base)
Stefan Reinauer8e073822012-04-04 00:07:22 +020088{
89 u32 reg32;
Elyes HAOUAS6ea24ff2020-08-11 09:21:24 +020090 /* Use a 1msec timeout */
91 int timeout = 1000;
Stefan Reinauer8e073822012-04-04 00:07:22 +020092
93 /* Send the verb to the codec */
94 reg32 = read32(base + HDA_ICII_REG);
95 reg32 |= HDA_ICII_BUSY | HDA_ICII_VALID;
96 write32(base + HDA_ICII_REG, reg32);
97
Elyes HAOUASba28e8d2016-08-31 19:22:16 +020098 while (timeout--) {
Stefan Reinauer8e073822012-04-04 00:07:22 +020099 reg32 = read32(base + HDA_ICII_REG);
Elyes HAOUAS6ea24ff2020-08-11 09:21:24 +0200100 if ((reg32 & (HDA_ICII_VALID | HDA_ICII_BUSY)) == HDA_ICII_VALID)
Stefan Reinauer8e073822012-04-04 00:07:22 +0200101 return 0;
102 udelay(1);
103 }
104
105 return -1;
106}
107
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800108static void codec_init(struct device *dev, u8 *base, int addr)
Stefan Reinauer8e073822012-04-04 00:07:22 +0200109{
110 u32 reg32;
111 const u32 *verb;
112 u32 verb_size;
113 int i;
114
115 printk(BIOS_DEBUG, "Azalia: Initializing codec #%d\n", addr);
116
117 /* 1 */
Angel Pons554713e2020-10-24 23:23:07 +0200118 if (wait_for_ready(base) < 0) {
Stefan Reinauer8e073822012-04-04 00:07:22 +0200119 printk(BIOS_DEBUG, " codec not ready.\n");
120 return;
121 }
122
123 reg32 = (addr << 28) | 0x000f0000;
Elyes HAOUAS11178bd2020-08-03 15:34:46 +0200124 write32(base + HDA_IC_REG, reg32);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200125
Angel Pons554713e2020-10-24 23:23:07 +0200126 if (wait_for_valid(base) < 0) {
Stefan Reinauer8e073822012-04-04 00:07:22 +0200127 printk(BIOS_DEBUG, " codec not valid.\n");
128 return;
129 }
130
Stefan Reinauer8e073822012-04-04 00:07:22 +0200131 /* 2 */
Elyes HAOUAS6ea24ff2020-08-11 09:21:24 +0200132 reg32 = read32(base + HDA_IR_REG);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200133 printk(BIOS_DEBUG, "Azalia: codec viddid: %08x\n", reg32);
134 verb_size = find_verb(dev, reg32, &verb);
135
136 if (!verb_size) {
137 printk(BIOS_DEBUG, "Azalia: No verb!\n");
138 return;
139 }
140 printk(BIOS_DEBUG, "Azalia: verb_size: %d\n", verb_size);
141
142 /* 3 */
143 for (i = 0; i < verb_size; i++) {
Angel Pons554713e2020-10-24 23:23:07 +0200144 if (wait_for_ready(base) < 0)
Stefan Reinauer8e073822012-04-04 00:07:22 +0200145 return;
146
Elyes HAOUAS11178bd2020-08-03 15:34:46 +0200147 write32(base + HDA_IC_REG, verb[i]);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200148
Angel Pons554713e2020-10-24 23:23:07 +0200149 if (wait_for_valid(base) < 0)
Stefan Reinauer8e073822012-04-04 00:07:22 +0200150 return;
151 }
152 printk(BIOS_DEBUG, "Azalia: verb loaded.\n");
153}
154
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800155static void codecs_init(struct device *dev, u8 *base, u32 codec_mask)
Stefan Reinauer8e073822012-04-04 00:07:22 +0200156{
157 int i;
158 for (i = 3; i >= 0; i--) {
159 if (codec_mask & (1 << i))
160 codec_init(dev, base, i);
161 }
Dylan Reidb98d0782012-04-27 11:37:33 -0700162
163 for (i = 0; i < pc_beep_verbs_size; i++) {
Angel Pons554713e2020-10-24 23:23:07 +0200164 if (wait_for_ready(base) < 0)
Dylan Reidb98d0782012-04-27 11:37:33 -0700165 return;
166
Elyes HAOUAS11178bd2020-08-03 15:34:46 +0200167 write32(base + HDA_IC_REG, pc_beep_verbs[i]);
Dylan Reidb98d0782012-04-27 11:37:33 -0700168
Angel Pons554713e2020-10-24 23:23:07 +0200169 if (wait_for_valid(base) < 0)
Dylan Reidb98d0782012-04-27 11:37:33 -0700170 return;
171 }
Stefan Reinauer8e073822012-04-04 00:07:22 +0200172}
173
174static void azalia_init(struct device *dev)
175{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800176 u8 *base;
Stefan Reinauer8e073822012-04-04 00:07:22 +0200177 struct resource *res;
178 u32 codec_mask;
Stefan Reinauer8e073822012-04-04 00:07:22 +0200179 u32 reg32;
180
Stefan Reinauer8e073822012-04-04 00:07:22 +0200181 res = find_resource(dev, PCI_BASE_ADDRESS_0);
182 if (!res)
183 return;
184
Elyes HAOUAS6ea24ff2020-08-11 09:21:24 +0200185 // NOTE this will break as soon as the Azalia get's a bar above 4G.
186 // Is there anything we can do about it?
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800187 base = res2mmio(res, 0, 0);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200188 printk(BIOS_DEBUG, "Azalia: base = %08x\n", (u32)base);
189
Patrick Rudolph4f8b1082019-07-14 11:54:58 +0200190 if (RCBA32(CIR31) & (1 << 31)) {
Kyösti Mälkkifd98c652013-07-26 08:50:53 +0300191 reg32 = pci_read_config32(dev, 0x120);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200192 reg32 &= 0xf8ffff01;
Stefan Reinauer15ba2bc2012-11-14 12:25:15 -0800193 reg32 |= (1 << 24); // 2 << 24 for server
Patrick Rudolph4f8b1082019-07-14 11:54:58 +0200194 reg32 |= RCBA32(CIR31) & 0xfe;
Kyösti Mälkkifd98c652013-07-26 08:50:53 +0300195 pci_write_config32(dev, 0x120, reg32);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200196
Angel Ponsc803f652020-06-07 22:09:01 +0200197 pci_or_config16(dev, 0x78, 1 << 11);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200198 } else
199 printk(BIOS_DEBUG, "Azalia: V1CTL disabled.\n");
200
Angel Ponsc803f652020-06-07 22:09:01 +0200201 pci_and_config32(dev, 0x114, ~0xfe);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200202
203 // Set VCi enable bit
Angel Ponsc803f652020-06-07 22:09:01 +0200204 pci_or_config32(dev, 0x120, 1 << 31);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200205
206 // Enable HDMI codec:
Angel Ponsc803f652020-06-07 22:09:01 +0200207 pci_or_config32(dev, 0xc4, 1 << 1);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200208
Angel Ponsc803f652020-06-07 22:09:01 +0200209 pci_or_config8(dev, 0x43, 1 << 6);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200210
211 /* Additional programming steps */
Angel Ponsc803f652020-06-07 22:09:01 +0200212 pci_or_config32(dev, 0xc4, 1 << 13);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200213
Angel Ponsc803f652020-06-07 22:09:01 +0200214 pci_or_config32(dev, 0xc4, 1 << 10);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200215
Angel Ponsc803f652020-06-07 22:09:01 +0200216 pci_and_config32(dev, 0xd0, ~(1 << 31));
Stefan Reinauer8e073822012-04-04 00:07:22 +0200217
Stefan Reinauer15ba2bc2012-11-14 12:25:15 -0800218 if (dev->device == 0x1e20) {
219 /* Additional step on Panther Point */
Angel Ponsc803f652020-06-07 22:09:01 +0200220 pci_or_config32(dev, 0xc4, 1 << 17);
Stefan Reinauer15ba2bc2012-11-14 12:25:15 -0800221 }
222
Stefan Reinauer8e073822012-04-04 00:07:22 +0200223 /* Set Bus Master */
Elyes HAOUAS729c0692020-04-28 19:50:44 +0200224 pci_or_config16(dev, PCI_COMMAND, PCI_COMMAND_MASTER);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200225
226 pci_write_config8(dev, 0x3c, 0x0a); // unused?
227
228 /* Codec Initialization Programming Sequence */
Stefan Reinauer15ba2bc2012-11-14 12:25:15 -0800229
230 /* Take controller out of reset */
Elyes HAOUAS11178bd2020-08-03 15:34:46 +0200231 reg32 = read32(base + HDA_GCTL_REG);
232 reg32 |= HDA_GCTL_CRST;
233 write32(base + HDA_GCTL_REG, reg32);
Stefan Reinauer15ba2bc2012-11-14 12:25:15 -0800234 /* Wait 1ms */
235 udelay(1000);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200236
Angel Ponsc803f652020-06-07 22:09:01 +0200237 // Select Azalia mode. This needs to be controlled via devicetree.cb
238 pci_or_config8(dev, 0x40, 1); // Audio Control
Stefan Reinauer8e073822012-04-04 00:07:22 +0200239
Angel Ponsc803f652020-06-07 22:09:01 +0200240 // Docking not supported
241 pci_and_config8(dev, 0x4d, (u8)~(1 << 7)); // Docking Status
Stefan Reinauer8e073822012-04-04 00:07:22 +0200242
243 codec_mask = codec_detect(base);
244
245 if (codec_mask) {
246 printk(BIOS_DEBUG, "Azalia: codec_mask = %02x\n", codec_mask);
247 codecs_init(dev, base, codec_mask);
248 }
249
250 /* Enable dynamic clock gating */
Angel Ponsc803f652020-06-07 22:09:01 +0200251 pci_update_config8(dev, 0x43, ~0x07, (1 << 2) | (1 << 0));
Stefan Reinauer8e073822012-04-04 00:07:22 +0200252}
253
Aaron Durbinaa090cb2017-09-13 16:01:52 -0600254static const char *azalia_acpi_name(const struct device *dev)
Patrick Rudolph604f6982017-06-07 09:46:52 +0200255{
256 return "HDEF";
257}
258
Stefan Reinauer8e073822012-04-04 00:07:22 +0200259static struct device_operations azalia_ops = {
260 .read_resources = pci_dev_read_resources,
261 .set_resources = pci_dev_set_resources,
262 .enable_resources = pci_dev_enable_resources,
263 .init = azalia_init,
Angel Pons1fc0edd2020-05-31 00:03:28 +0200264 .ops_pci = &pci_dev_ops_pci,
Patrick Rudolph604f6982017-06-07 09:46:52 +0200265 .acpi_name = azalia_acpi_name,
Stefan Reinauer8e073822012-04-04 00:07:22 +0200266};
267
Stefan Reinauer9a380ab2012-06-22 13:16:11 -0700268static const unsigned short pci_device_ids[] = { 0x1c20, 0x1e20, 0 };
Stefan Reinauer8e073822012-04-04 00:07:22 +0200269
Stefan Reinauer9a380ab2012-06-22 13:16:11 -0700270static const struct pci_driver pch_azalia __pci_driver = {
271 .ops = &azalia_ops,
272 .vendor = PCI_VENDOR_ID_INTEL,
273 .devices = pci_device_ids,
Stefan Reinauer8e073822012-04-04 00:07:22 +0200274};