blob: fd0852828b43de55703998899f54db5213774599 [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 HAOUAS6ea24ff2020-08-11 09:21:24 +020044/*
45 * Wait 50usec for the codec to indicate it is ready.
46 * No response would imply that the codec is non-operative.
Stefan Reinauer8e073822012-04-04 00:07:22 +020047 */
48
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080049static int wait_for_ready(u8 *base)
Stefan Reinauer8e073822012-04-04 00:07:22 +020050{
Stefan Reinauer15ba2bc2012-11-14 12:25:15 -080051 /* Use a 1msec timeout */
Stefan Reinauer15ba2bc2012-11-14 12:25:15 -080052 int timeout = 1000;
Stefan Reinauer8e073822012-04-04 00:07:22 +020053
Elyes HAOUASba28e8d2016-08-31 19:22:16 +020054 while (timeout--) {
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080055 u32 reg32 = read32(base + HDA_ICII_REG);
Stefan Reinauer8e073822012-04-04 00:07:22 +020056 if (!(reg32 & HDA_ICII_BUSY))
57 return 0;
58 udelay(1);
59 }
60
61 return -1;
62}
63
Elyes HAOUAS6ea24ff2020-08-11 09:21:24 +020064/*
65 * Wait 50usec for the codec to indicate that it accepted the previous command.
66 * No response would imply that the code is non-operative.
Stefan Reinauer8e073822012-04-04 00:07:22 +020067 */
68
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080069static int wait_for_valid(u8 *base)
Stefan Reinauer8e073822012-04-04 00:07:22 +020070{
71 u32 reg32;
Elyes HAOUAS6ea24ff2020-08-11 09:21:24 +020072 /* Use a 1msec timeout */
73 int timeout = 1000;
Stefan Reinauer8e073822012-04-04 00:07:22 +020074
75 /* Send the verb to the codec */
76 reg32 = read32(base + HDA_ICII_REG);
77 reg32 |= HDA_ICII_BUSY | HDA_ICII_VALID;
78 write32(base + HDA_ICII_REG, reg32);
79
Elyes HAOUASba28e8d2016-08-31 19:22:16 +020080 while (timeout--) {
Stefan Reinauer8e073822012-04-04 00:07:22 +020081 reg32 = read32(base + HDA_ICII_REG);
Elyes HAOUAS6ea24ff2020-08-11 09:21:24 +020082 if ((reg32 & (HDA_ICII_VALID | HDA_ICII_BUSY)) == HDA_ICII_VALID)
Stefan Reinauer8e073822012-04-04 00:07:22 +020083 return 0;
84 udelay(1);
85 }
86
87 return -1;
88}
89
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080090static void codec_init(struct device *dev, u8 *base, int addr)
Stefan Reinauer8e073822012-04-04 00:07:22 +020091{
92 u32 reg32;
93 const u32 *verb;
94 u32 verb_size;
95 int i;
96
97 printk(BIOS_DEBUG, "Azalia: Initializing codec #%d\n", addr);
98
99 /* 1 */
Angel Pons554713e2020-10-24 23:23:07 +0200100 if (wait_for_ready(base) < 0) {
Stefan Reinauer8e073822012-04-04 00:07:22 +0200101 printk(BIOS_DEBUG, " codec not ready.\n");
102 return;
103 }
104
105 reg32 = (addr << 28) | 0x000f0000;
Elyes HAOUAS11178bd2020-08-03 15:34:46 +0200106 write32(base + HDA_IC_REG, reg32);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200107
Angel Pons554713e2020-10-24 23:23:07 +0200108 if (wait_for_valid(base) < 0) {
Stefan Reinauer8e073822012-04-04 00:07:22 +0200109 printk(BIOS_DEBUG, " codec not valid.\n");
110 return;
111 }
112
Stefan Reinauer8e073822012-04-04 00:07:22 +0200113 /* 2 */
Elyes HAOUAS6ea24ff2020-08-11 09:21:24 +0200114 reg32 = read32(base + HDA_IR_REG);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200115 printk(BIOS_DEBUG, "Azalia: codec viddid: %08x\n", reg32);
Angel Ponsd425ddd2020-12-05 18:22:58 +0100116 verb_size = azalia_find_verb(cim_verb_data, cim_verb_data_size, reg32, &verb);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200117
118 if (!verb_size) {
119 printk(BIOS_DEBUG, "Azalia: No verb!\n");
120 return;
121 }
122 printk(BIOS_DEBUG, "Azalia: verb_size: %d\n", verb_size);
123
124 /* 3 */
125 for (i = 0; i < verb_size; i++) {
Angel Pons554713e2020-10-24 23:23:07 +0200126 if (wait_for_ready(base) < 0)
Stefan Reinauer8e073822012-04-04 00:07:22 +0200127 return;
128
Elyes HAOUAS11178bd2020-08-03 15:34:46 +0200129 write32(base + HDA_IC_REG, verb[i]);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200130
Angel Pons554713e2020-10-24 23:23:07 +0200131 if (wait_for_valid(base) < 0)
Stefan Reinauer8e073822012-04-04 00:07:22 +0200132 return;
133 }
134 printk(BIOS_DEBUG, "Azalia: verb loaded.\n");
135}
136
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800137static void codecs_init(struct device *dev, u8 *base, u32 codec_mask)
Stefan Reinauer8e073822012-04-04 00:07:22 +0200138{
139 int i;
140 for (i = 3; i >= 0; i--) {
141 if (codec_mask & (1 << i))
142 codec_init(dev, base, i);
143 }
Dylan Reidb98d0782012-04-27 11:37:33 -0700144
145 for (i = 0; i < pc_beep_verbs_size; i++) {
Angel Pons554713e2020-10-24 23:23:07 +0200146 if (wait_for_ready(base) < 0)
Dylan Reidb98d0782012-04-27 11:37:33 -0700147 return;
148
Elyes HAOUAS11178bd2020-08-03 15:34:46 +0200149 write32(base + HDA_IC_REG, pc_beep_verbs[i]);
Dylan Reidb98d0782012-04-27 11:37:33 -0700150
Angel Pons554713e2020-10-24 23:23:07 +0200151 if (wait_for_valid(base) < 0)
Dylan Reidb98d0782012-04-27 11:37:33 -0700152 return;
153 }
Stefan Reinauer8e073822012-04-04 00:07:22 +0200154}
155
156static void azalia_init(struct device *dev)
157{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800158 u8 *base;
Stefan Reinauer8e073822012-04-04 00:07:22 +0200159 struct resource *res;
160 u32 codec_mask;
Stefan Reinauer8e073822012-04-04 00:07:22 +0200161 u32 reg32;
162
Stefan Reinauer8e073822012-04-04 00:07:22 +0200163 res = find_resource(dev, PCI_BASE_ADDRESS_0);
164 if (!res)
165 return;
166
Elyes HAOUAS6ea24ff2020-08-11 09:21:24 +0200167 // NOTE this will break as soon as the Azalia get's a bar above 4G.
168 // Is there anything we can do about it?
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800169 base = res2mmio(res, 0, 0);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200170 printk(BIOS_DEBUG, "Azalia: base = %08x\n", (u32)base);
171
Patrick Rudolph4f8b1082019-07-14 11:54:58 +0200172 if (RCBA32(CIR31) & (1 << 31)) {
Kyösti Mälkkifd98c652013-07-26 08:50:53 +0300173 reg32 = pci_read_config32(dev, 0x120);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200174 reg32 &= 0xf8ffff01;
Stefan Reinauer15ba2bc2012-11-14 12:25:15 -0800175 reg32 |= (1 << 24); // 2 << 24 for server
Patrick Rudolph4f8b1082019-07-14 11:54:58 +0200176 reg32 |= RCBA32(CIR31) & 0xfe;
Kyösti Mälkkifd98c652013-07-26 08:50:53 +0300177 pci_write_config32(dev, 0x120, reg32);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200178
Angel Ponsc803f652020-06-07 22:09:01 +0200179 pci_or_config16(dev, 0x78, 1 << 11);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200180 } else
181 printk(BIOS_DEBUG, "Azalia: V1CTL disabled.\n");
182
Angel Ponsc803f652020-06-07 22:09:01 +0200183 pci_and_config32(dev, 0x114, ~0xfe);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200184
185 // Set VCi enable bit
Angel Ponsc803f652020-06-07 22:09:01 +0200186 pci_or_config32(dev, 0x120, 1 << 31);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200187
188 // Enable HDMI codec:
Angel Ponsc803f652020-06-07 22:09:01 +0200189 pci_or_config32(dev, 0xc4, 1 << 1);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200190
Angel Ponsc803f652020-06-07 22:09:01 +0200191 pci_or_config8(dev, 0x43, 1 << 6);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200192
193 /* Additional programming steps */
Angel Ponsc803f652020-06-07 22:09:01 +0200194 pci_or_config32(dev, 0xc4, 1 << 13);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200195
Angel Ponsc803f652020-06-07 22:09:01 +0200196 pci_or_config32(dev, 0xc4, 1 << 10);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200197
Angel Ponsc803f652020-06-07 22:09:01 +0200198 pci_and_config32(dev, 0xd0, ~(1 << 31));
Stefan Reinauer8e073822012-04-04 00:07:22 +0200199
Stefan Reinauer15ba2bc2012-11-14 12:25:15 -0800200 if (dev->device == 0x1e20) {
201 /* Additional step on Panther Point */
Angel Ponsc803f652020-06-07 22:09:01 +0200202 pci_or_config32(dev, 0xc4, 1 << 17);
Stefan Reinauer15ba2bc2012-11-14 12:25:15 -0800203 }
204
Stefan Reinauer8e073822012-04-04 00:07:22 +0200205 /* Set Bus Master */
Elyes HAOUAS729c0692020-04-28 19:50:44 +0200206 pci_or_config16(dev, PCI_COMMAND, PCI_COMMAND_MASTER);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200207
208 pci_write_config8(dev, 0x3c, 0x0a); // unused?
209
210 /* Codec Initialization Programming Sequence */
Stefan Reinauer15ba2bc2012-11-14 12:25:15 -0800211
212 /* Take controller out of reset */
Elyes HAOUAS11178bd2020-08-03 15:34:46 +0200213 reg32 = read32(base + HDA_GCTL_REG);
214 reg32 |= HDA_GCTL_CRST;
215 write32(base + HDA_GCTL_REG, reg32);
Stefan Reinauer15ba2bc2012-11-14 12:25:15 -0800216 /* Wait 1ms */
217 udelay(1000);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200218
Angel Ponsc803f652020-06-07 22:09:01 +0200219 // Select Azalia mode. This needs to be controlled via devicetree.cb
220 pci_or_config8(dev, 0x40, 1); // Audio Control
Stefan Reinauer8e073822012-04-04 00:07:22 +0200221
Angel Ponsc803f652020-06-07 22:09:01 +0200222 // Docking not supported
223 pci_and_config8(dev, 0x4d, (u8)~(1 << 7)); // Docking Status
Stefan Reinauer8e073822012-04-04 00:07:22 +0200224
225 codec_mask = codec_detect(base);
226
227 if (codec_mask) {
228 printk(BIOS_DEBUG, "Azalia: codec_mask = %02x\n", codec_mask);
229 codecs_init(dev, base, codec_mask);
230 }
231
232 /* Enable dynamic clock gating */
Angel Ponsc803f652020-06-07 22:09:01 +0200233 pci_update_config8(dev, 0x43, ~0x07, (1 << 2) | (1 << 0));
Stefan Reinauer8e073822012-04-04 00:07:22 +0200234}
235
Aaron Durbinaa090cb2017-09-13 16:01:52 -0600236static const char *azalia_acpi_name(const struct device *dev)
Patrick Rudolph604f6982017-06-07 09:46:52 +0200237{
238 return "HDEF";
239}
240
Stefan Reinauer8e073822012-04-04 00:07:22 +0200241static struct device_operations azalia_ops = {
242 .read_resources = pci_dev_read_resources,
243 .set_resources = pci_dev_set_resources,
244 .enable_resources = pci_dev_enable_resources,
245 .init = azalia_init,
Angel Pons1fc0edd2020-05-31 00:03:28 +0200246 .ops_pci = &pci_dev_ops_pci,
Patrick Rudolph604f6982017-06-07 09:46:52 +0200247 .acpi_name = azalia_acpi_name,
Stefan Reinauer8e073822012-04-04 00:07:22 +0200248};
249
Stefan Reinauer9a380ab2012-06-22 13:16:11 -0700250static const unsigned short pci_device_ids[] = { 0x1c20, 0x1e20, 0 };
Stefan Reinauer8e073822012-04-04 00:07:22 +0200251
Stefan Reinauer9a380ab2012-06-22 13:16:11 -0700252static const struct pci_driver pch_azalia __pci_driver = {
253 .ops = &azalia_ops,
254 .vendor = PCI_VENDOR_ID_INTEL,
255 .devices = pci_device_ids,
Stefan Reinauer8e073822012-04-04 00:07:22 +0200256};