blob: 683715fd6bd9d31f2e9fb26382a2873d9ad1df57 [file] [log] [blame]
Angel Pons182dbde2020-04-02 23:49:05 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Vladimir Serbinenko888d5592013-11-13 17:53:38 +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>
Vladimir Serbinenko888d5592013-11-13 17:53:38 +01009#include <delay.h>
Vladimir Serbinenko75c83872014-09-05 01:01:31 +020010#include <device/azalia_device.h>
Vladimir Serbinenko888d5592013-11-13 17:53:38 +010011#include "pch.h"
12
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080013static int codec_detect(u8 *base)
Vladimir Serbinenko888d5592013-11-13 17:53:38 +010014{
15 u8 reg8;
16
Angel Pons7f839f62020-12-05 19:02:14 +010017 if (azalia_exit_reset(base) < 0)
Vladimir Serbinenko888d5592013-11-13 17:53:38 +010018 goto no_codec;
19
20 /* Write back the value once reset bit is set. */
Elyes HAOUAS6ea24ff2020-08-11 09:21:24 +020021 write16(base + HDA_GCAP_REG, read16(base + HDA_GCAP_REG));
Vladimir Serbinenko888d5592013-11-13 17:53:38 +010022
Elyes HAOUAS6ea24ff2020-08-11 09:21:24 +020023 /* Read in Codec location (BAR + 0xe)[2..0] */
Elyes HAOUAS59236d52020-08-03 15:36:52 +020024 reg8 = read8(base + HDA_STATESTS_REG);
Vladimir Serbinenko888d5592013-11-13 17:53:38 +010025 reg8 &= 0x0f;
26 if (!reg8)
27 goto no_codec;
28
29 return reg8;
30
31no_codec:
Angel Pons2e0053b2020-12-05 19:06:55 +010032 /* Codec not found, put HDA back in reset */
33 azalia_enter_reset(base);
Vladimir Serbinenko888d5592013-11-13 17:53:38 +010034 printk(BIOS_DEBUG, "Azalia: No codec!\n");
35 return 0;
36}
37
Elyes HAOUAS6ea24ff2020-08-11 09:21:24 +020038/*
39 * Wait 50usec for the codec to indicate it is ready.
40 * No response would imply that the codec is non-operative.
Vladimir Serbinenko888d5592013-11-13 17:53:38 +010041 */
42
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080043static int wait_for_ready(u8 *base)
Vladimir Serbinenko888d5592013-11-13 17:53:38 +010044{
45 /* Use a 1msec timeout */
Vladimir Serbinenko888d5592013-11-13 17:53:38 +010046 int timeout = 1000;
47
Elyes HAOUASba28e8d2016-08-31 19:22:16 +020048 while (timeout--) {
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080049 u32 reg32 = read32(base + HDA_ICII_REG);
Vladimir Serbinenko888d5592013-11-13 17:53:38 +010050 if (!(reg32 & HDA_ICII_BUSY))
51 return 0;
52 udelay(1);
53 }
54
55 return -1;
56}
57
Elyes HAOUAS6ea24ff2020-08-11 09:21:24 +020058/*
59 * Wait 50usec for the codec to indicate that it accepted the previous command.
60 * No response would imply that the code is non-operative.
Vladimir Serbinenko888d5592013-11-13 17:53:38 +010061 */
62
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080063static int wait_for_valid(u8 *base)
Vladimir Serbinenko888d5592013-11-13 17:53:38 +010064{
65 u32 reg32;
Elyes HAOUAS6ea24ff2020-08-11 09:21:24 +020066 /* Use a 1msec timeout */
67 int timeout = 1000;
Vladimir Serbinenko888d5592013-11-13 17:53:38 +010068
69 /* Send the verb to the codec */
70 reg32 = read32(base + HDA_ICII_REG);
71 reg32 |= HDA_ICII_BUSY | HDA_ICII_VALID;
72 write32(base + HDA_ICII_REG, reg32);
73
Elyes HAOUASba28e8d2016-08-31 19:22:16 +020074 while (timeout--) {
Vladimir Serbinenko888d5592013-11-13 17:53:38 +010075 reg32 = read32(base + HDA_ICII_REG);
Elyes HAOUAS6ea24ff2020-08-11 09:21:24 +020076 if ((reg32 & (HDA_ICII_VALID | HDA_ICII_BUSY)) == HDA_ICII_VALID)
Vladimir Serbinenko888d5592013-11-13 17:53:38 +010077 return 0;
78 udelay(1);
79 }
80
81 return -1;
82}
83
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080084static void codec_init(struct device *dev, u8 *base, int addr)
Vladimir Serbinenko888d5592013-11-13 17:53:38 +010085{
86 u32 reg32;
87 const u32 *verb;
88 u32 verb_size;
89 int i;
90
91 printk(BIOS_DEBUG, "Azalia: Initializing codec #%d\n", addr);
92
93 /* 1 */
Angel Pons554713e2020-10-24 23:23:07 +020094 if (wait_for_ready(base) < 0) {
Vladimir Serbinenko888d5592013-11-13 17:53:38 +010095 printk(BIOS_DEBUG, " codec not ready.\n");
96 return;
97 }
98
99 reg32 = (addr << 28) | 0x000f0000;
Elyes HAOUAS59236d52020-08-03 15:36:52 +0200100 write32(base + HDA_IC_REG, reg32);
Vladimir Serbinenko888d5592013-11-13 17:53:38 +0100101
Angel Pons554713e2020-10-24 23:23:07 +0200102 if (wait_for_valid(base) < 0) {
Vladimir Serbinenko888d5592013-11-13 17:53:38 +0100103 printk(BIOS_DEBUG, " codec not valid.\n");
104 return;
105 }
106
Vladimir Serbinenko888d5592013-11-13 17:53:38 +0100107 /* 2 */
Elyes HAOUAS6ea24ff2020-08-11 09:21:24 +0200108 reg32 = read32(base + HDA_IR_REG);
Vladimir Serbinenko888d5592013-11-13 17:53:38 +0100109 printk(BIOS_DEBUG, "Azalia: codec viddid: %08x\n", reg32);
Angel Ponsd425ddd2020-12-05 18:22:58 +0100110 verb_size = azalia_find_verb(cim_verb_data, cim_verb_data_size, reg32, &verb);
Vladimir Serbinenko888d5592013-11-13 17:53:38 +0100111
112 if (!verb_size) {
113 printk(BIOS_DEBUG, "Azalia: No verb!\n");
114 return;
115 }
116 printk(BIOS_DEBUG, "Azalia: verb_size: %d\n", verb_size);
117
118 /* 3 */
119 for (i = 0; i < verb_size; i++) {
Angel Pons554713e2020-10-24 23:23:07 +0200120 if (wait_for_ready(base) < 0)
Vladimir Serbinenko888d5592013-11-13 17:53:38 +0100121 return;
122
Elyes HAOUAS59236d52020-08-03 15:36:52 +0200123 write32(base + HDA_IC_REG, verb[i]);
Vladimir Serbinenko888d5592013-11-13 17:53:38 +0100124
Angel Pons554713e2020-10-24 23:23:07 +0200125 if (wait_for_valid(base) < 0)
Vladimir Serbinenko888d5592013-11-13 17:53:38 +0100126 return;
127 }
128 printk(BIOS_DEBUG, "Azalia: verb loaded.\n");
129}
130
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800131static void codecs_init(struct device *dev, u8 *base, u32 codec_mask)
Vladimir Serbinenko888d5592013-11-13 17:53:38 +0100132{
133 int i;
Elyes HAOUAS6ea24ff2020-08-11 09:21:24 +0200134
Vladimir Serbinenko888d5592013-11-13 17:53:38 +0100135 for (i = 3; i >= 0; i--) {
136 if (codec_mask & (1 << i))
137 codec_init(dev, base, i);
138 }
139
140 for (i = 0; i < pc_beep_verbs_size; i++) {
Angel Pons554713e2020-10-24 23:23:07 +0200141 if (wait_for_ready(base) < 0)
Vladimir Serbinenko888d5592013-11-13 17:53:38 +0100142 return;
143
Elyes HAOUAS59236d52020-08-03 15:36:52 +0200144 write32(base + HDA_IC_REG, pc_beep_verbs[i]);
Vladimir Serbinenko888d5592013-11-13 17:53:38 +0100145
Angel Pons554713e2020-10-24 23:23:07 +0200146 if (wait_for_valid(base) < 0)
Vladimir Serbinenko888d5592013-11-13 17:53:38 +0100147 return;
148 }
149}
150
151static void azalia_init(struct device *dev)
152{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800153 u8 *base;
Vladimir Serbinenko888d5592013-11-13 17:53:38 +0100154 struct resource *res;
155 u32 codec_mask;
156 u8 reg8;
157 u16 reg16;
158 u32 reg32;
159
160 /* Find base address */
161 res = find_resource(dev, PCI_BASE_ADDRESS_0);
162 if (!res)
163 return;
164
Martin Roth26f97f92021-10-01 14:53:22 -0600165 // NOTE this will break as soon as the Azalia gets a bar above 4G.
Elyes HAOUAS6ea24ff2020-08-11 09:21:24 +0200166 // Is there anything we can do about it?
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800167 base = res2mmio(res, 0, 0);
Patrick Rudolph819c2062019-11-29 19:27:37 +0100168 printk(BIOS_DEBUG, "Azalia: base = %p\n", base);
Vladimir Serbinenko888d5592013-11-13 17:53:38 +0100169
170 if (RCBA32(0x2030) & (1 << 31)) {
171 reg32 = pci_read_config32(dev, 0x120);
172 reg32 &= 0xf8ffff01;
173 reg32 |= (1 << 24); // 2 << 24 for server
174 reg32 |= RCBA32(0x2030) & 0xfe;
175 pci_write_config32(dev, 0x120, reg32);
176
177 reg16 = pci_read_config16(dev, 0x78);
178 reg16 |= (1 << 11);
179 pci_write_config16(dev, 0x78, reg16);
180 } else
181 printk(BIOS_DEBUG, "Azalia: V1CTL disabled.\n");
182
183 reg32 = pci_read_config32(dev, 0x114);
184 reg32 &= ~0xfe;
185 pci_write_config32(dev, 0x114, reg32);
186
187 // Set VCi enable bit
188 reg32 = pci_read_config32(dev, 0x120);
189 reg32 |= (1 << 31);
190 pci_write_config32(dev, 0x120, reg32);
191
192 // Enable HDMI codec:
193 reg32 = pci_read_config32(dev, 0xc4);
194 reg32 |= (1 << 1);
195 pci_write_config32(dev, 0xc4, reg32);
196
197 reg8 = pci_read_config8(dev, 0x43);
198 reg8 |= (1 << 6);
199 pci_write_config8(dev, 0x43, reg8);
200
Vladimir Serbinenko888d5592013-11-13 17:53:38 +0100201 reg32 = pci_read_config32(dev, 0xd0);
202 reg32 &= ~(1 << 31);
203 pci_write_config32(dev, 0xd0, reg32);
204
Vladimir Serbinenko888d5592013-11-13 17:53:38 +0100205 /* Set Bus Master */
Elyes HAOUAS8b6dfde2020-04-28 09:58:21 +0200206 pci_or_config16(dev, PCI_COMMAND, PCI_COMMAND_MASTER);
Vladimir Serbinenko888d5592013-11-13 17:53:38 +0100207
208 pci_write_config8(dev, 0x3c, 0x0a); // unused?
209
210 /* Codec Initialization Programming Sequence */
211
212 /* Take controller out of reset */
Elyes HAOUAS59236d52020-08-03 15:36:52 +0200213 reg32 = read32(base + HDA_GCTL_REG);
214 reg32 |= HDA_GCTL_CRST;
215 write32(base + HDA_GCTL_REG, reg32);
Vladimir Serbinenko888d5592013-11-13 17:53:38 +0100216 /* Wait 1ms */
217 udelay(1000);
218
219 //
220 reg8 = pci_read_config8(dev, 0x40); // Audio Control
221 reg8 |= 1; // Select Azalia mode. This needs to be controlled via devicetree.cb
222 pci_write_config8(dev, 0x40, reg8);
223
224 reg8 = pci_read_config8(dev, 0x4d); // Docking Status
225 reg8 &= ~(1 << 7); // Docking not supported
226 pci_write_config8(dev, 0x4d, reg8);
227
228 codec_mask = codec_detect(base);
229
230 if (codec_mask) {
231 printk(BIOS_DEBUG, "Azalia: codec_mask = %02x\n", codec_mask);
232 codecs_init(dev, base, codec_mask);
233 }
234
235 /* Enable dynamic clock gating */
236 reg8 = pci_read_config8(dev, 0x43);
237 reg8 &= ~0x7;
238 reg8 |= (1 << 2) | (1 << 0);
239 pci_write_config8(dev, 0x43, reg8);
240}
241
Vladimir Serbinenko888d5592013-11-13 17:53:38 +0100242static struct device_operations azalia_ops = {
243 .read_resources = pci_dev_read_resources,
244 .set_resources = pci_dev_set_resources,
245 .enable_resources = pci_dev_enable_resources,
246 .init = azalia_init,
Angel Pons1fc0edd2020-05-31 00:03:28 +0200247 .ops_pci = &pci_dev_ops_pci,
Vladimir Serbinenko888d5592013-11-13 17:53:38 +0100248};
249
Felix Singer838fbc72019-11-21 21:23:32 +0100250static const unsigned short pci_device_ids[] = {
251 0x1c20,
252 0x1e20,
253 PCI_DID_INTEL_IBEXPEAK_AUDIO,
254 0
255};
Vladimir Serbinenko888d5592013-11-13 17:53:38 +0100256
257static const struct pci_driver pch_azalia __pci_driver = {
258 .ops = &azalia_ops,
259 .vendor = PCI_VENDOR_ID_INTEL,
260 .devices = pci_device_ids,
261};