blob: eab8e792a31df30f772e49788645dd732c21cc7e [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
Angel Pons7f839f62020-12-05 19:02:14 +010021 if (azalia_exit_reset(base) < 0)
Stefan Reinauer8e073822012-04-04 00:07:22 +020022 goto no_codec;
23
24 /* Write back the value once reset bit is set. */
Elyes HAOUAS6ea24ff2020-08-11 09:21:24 +020025 write16(base + HDA_GCAP_REG, read16(base + HDA_GCAP_REG));
Stefan Reinauer8e073822012-04-04 00:07:22 +020026
Elyes HAOUAS6ea24ff2020-08-11 09:21:24 +020027 /* Read in Codec location (BAR + 0xe)[2..0] */
Elyes HAOUAS11178bd2020-08-03 15:34:46 +020028 reg8 = read8(base + HDA_STATESTS_REG);
Stefan Reinauer8e073822012-04-04 00:07:22 +020029 reg8 &= 0x0f;
30 if (!reg8)
31 goto no_codec;
32
33 return reg8;
34
35no_codec:
36 /* Codec Not found */
37 /* Put HDA back in reset (BAR + 0x8) [0] */
Angel Pons61dd8362020-12-05 18:02:32 +010038 azalia_set_bits(base + HDA_GCTL_REG, 1, 0);
Stefan Reinauer8e073822012-04-04 00:07:22 +020039 printk(BIOS_DEBUG, "Azalia: No codec!\n");
40 return 0;
41}
42
Elyes HAOUAS6ea24ff2020-08-11 09:21:24 +020043/*
44 * Wait 50usec for the codec to indicate it is ready.
45 * No response would imply that the codec is non-operative.
Stefan Reinauer8e073822012-04-04 00:07:22 +020046 */
47
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080048static int wait_for_ready(u8 *base)
Stefan Reinauer8e073822012-04-04 00:07:22 +020049{
Stefan Reinauer15ba2bc2012-11-14 12:25:15 -080050 /* Use a 1msec timeout */
Stefan Reinauer15ba2bc2012-11-14 12:25:15 -080051 int timeout = 1000;
Stefan Reinauer8e073822012-04-04 00:07:22 +020052
Elyes HAOUASba28e8d2016-08-31 19:22:16 +020053 while (timeout--) {
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080054 u32 reg32 = read32(base + HDA_ICII_REG);
Stefan Reinauer8e073822012-04-04 00:07:22 +020055 if (!(reg32 & HDA_ICII_BUSY))
56 return 0;
57 udelay(1);
58 }
59
60 return -1;
61}
62
Elyes HAOUAS6ea24ff2020-08-11 09:21:24 +020063/*
64 * Wait 50usec for the codec to indicate that it accepted the previous command.
65 * No response would imply that the code is non-operative.
Stefan Reinauer8e073822012-04-04 00:07:22 +020066 */
67
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080068static int wait_for_valid(u8 *base)
Stefan Reinauer8e073822012-04-04 00:07:22 +020069{
70 u32 reg32;
Elyes HAOUAS6ea24ff2020-08-11 09:21:24 +020071 /* Use a 1msec timeout */
72 int timeout = 1000;
Stefan Reinauer8e073822012-04-04 00:07:22 +020073
74 /* Send the verb to the codec */
75 reg32 = read32(base + HDA_ICII_REG);
76 reg32 |= HDA_ICII_BUSY | HDA_ICII_VALID;
77 write32(base + HDA_ICII_REG, reg32);
78
Elyes HAOUASba28e8d2016-08-31 19:22:16 +020079 while (timeout--) {
Stefan Reinauer8e073822012-04-04 00:07:22 +020080 reg32 = read32(base + HDA_ICII_REG);
Elyes HAOUAS6ea24ff2020-08-11 09:21:24 +020081 if ((reg32 & (HDA_ICII_VALID | HDA_ICII_BUSY)) == HDA_ICII_VALID)
Stefan Reinauer8e073822012-04-04 00:07:22 +020082 return 0;
83 udelay(1);
84 }
85
86 return -1;
87}
88
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080089static void codec_init(struct device *dev, u8 *base, int addr)
Stefan Reinauer8e073822012-04-04 00:07:22 +020090{
91 u32 reg32;
92 const u32 *verb;
93 u32 verb_size;
94 int i;
95
96 printk(BIOS_DEBUG, "Azalia: Initializing codec #%d\n", addr);
97
98 /* 1 */
Angel Pons554713e2020-10-24 23:23:07 +020099 if (wait_for_ready(base) < 0) {
Stefan Reinauer8e073822012-04-04 00:07:22 +0200100 printk(BIOS_DEBUG, " codec not ready.\n");
101 return;
102 }
103
104 reg32 = (addr << 28) | 0x000f0000;
Elyes HAOUAS11178bd2020-08-03 15:34:46 +0200105 write32(base + HDA_IC_REG, reg32);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200106
Angel Pons554713e2020-10-24 23:23:07 +0200107 if (wait_for_valid(base) < 0) {
Stefan Reinauer8e073822012-04-04 00:07:22 +0200108 printk(BIOS_DEBUG, " codec not valid.\n");
109 return;
110 }
111
Stefan Reinauer8e073822012-04-04 00:07:22 +0200112 /* 2 */
Elyes HAOUAS6ea24ff2020-08-11 09:21:24 +0200113 reg32 = read32(base + HDA_IR_REG);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200114 printk(BIOS_DEBUG, "Azalia: codec viddid: %08x\n", reg32);
Angel Ponsd425ddd2020-12-05 18:22:58 +0100115 verb_size = azalia_find_verb(cim_verb_data, cim_verb_data_size, reg32, &verb);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200116
117 if (!verb_size) {
118 printk(BIOS_DEBUG, "Azalia: No verb!\n");
119 return;
120 }
121 printk(BIOS_DEBUG, "Azalia: verb_size: %d\n", verb_size);
122
123 /* 3 */
124 for (i = 0; i < verb_size; i++) {
Angel Pons554713e2020-10-24 23:23:07 +0200125 if (wait_for_ready(base) < 0)
Stefan Reinauer8e073822012-04-04 00:07:22 +0200126 return;
127
Elyes HAOUAS11178bd2020-08-03 15:34:46 +0200128 write32(base + HDA_IC_REG, verb[i]);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200129
Angel Pons554713e2020-10-24 23:23:07 +0200130 if (wait_for_valid(base) < 0)
Stefan Reinauer8e073822012-04-04 00:07:22 +0200131 return;
132 }
133 printk(BIOS_DEBUG, "Azalia: verb loaded.\n");
134}
135
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800136static void codecs_init(struct device *dev, u8 *base, u32 codec_mask)
Stefan Reinauer8e073822012-04-04 00:07:22 +0200137{
138 int i;
139 for (i = 3; i >= 0; i--) {
140 if (codec_mask & (1 << i))
141 codec_init(dev, base, i);
142 }
Dylan Reidb98d0782012-04-27 11:37:33 -0700143
144 for (i = 0; i < pc_beep_verbs_size; i++) {
Angel Pons554713e2020-10-24 23:23:07 +0200145 if (wait_for_ready(base) < 0)
Dylan Reidb98d0782012-04-27 11:37:33 -0700146 return;
147
Elyes HAOUAS11178bd2020-08-03 15:34:46 +0200148 write32(base + HDA_IC_REG, pc_beep_verbs[i]);
Dylan Reidb98d0782012-04-27 11:37:33 -0700149
Angel Pons554713e2020-10-24 23:23:07 +0200150 if (wait_for_valid(base) < 0)
Dylan Reidb98d0782012-04-27 11:37:33 -0700151 return;
152 }
Stefan Reinauer8e073822012-04-04 00:07:22 +0200153}
154
155static void azalia_init(struct device *dev)
156{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800157 u8 *base;
Stefan Reinauer8e073822012-04-04 00:07:22 +0200158 struct resource *res;
159 u32 codec_mask;
Stefan Reinauer8e073822012-04-04 00:07:22 +0200160 u32 reg32;
161
Stefan Reinauer8e073822012-04-04 00:07:22 +0200162 res = find_resource(dev, PCI_BASE_ADDRESS_0);
163 if (!res)
164 return;
165
Elyes HAOUAS6ea24ff2020-08-11 09:21:24 +0200166 // NOTE this will break as soon as the Azalia get's a bar above 4G.
167 // Is there anything we can do about it?
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800168 base = res2mmio(res, 0, 0);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200169 printk(BIOS_DEBUG, "Azalia: base = %08x\n", (u32)base);
170
Patrick Rudolph4f8b1082019-07-14 11:54:58 +0200171 if (RCBA32(CIR31) & (1 << 31)) {
Kyösti Mälkkifd98c652013-07-26 08:50:53 +0300172 reg32 = pci_read_config32(dev, 0x120);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200173 reg32 &= 0xf8ffff01;
Stefan Reinauer15ba2bc2012-11-14 12:25:15 -0800174 reg32 |= (1 << 24); // 2 << 24 for server
Patrick Rudolph4f8b1082019-07-14 11:54:58 +0200175 reg32 |= RCBA32(CIR31) & 0xfe;
Kyösti Mälkkifd98c652013-07-26 08:50:53 +0300176 pci_write_config32(dev, 0x120, reg32);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200177
Angel Ponsc803f652020-06-07 22:09:01 +0200178 pci_or_config16(dev, 0x78, 1 << 11);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200179 } else
180 printk(BIOS_DEBUG, "Azalia: V1CTL disabled.\n");
181
Angel Ponsc803f652020-06-07 22:09:01 +0200182 pci_and_config32(dev, 0x114, ~0xfe);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200183
184 // Set VCi enable bit
Angel Ponsc803f652020-06-07 22:09:01 +0200185 pci_or_config32(dev, 0x120, 1 << 31);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200186
187 // Enable HDMI codec:
Angel Ponsc803f652020-06-07 22:09:01 +0200188 pci_or_config32(dev, 0xc4, 1 << 1);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200189
Angel Ponsc803f652020-06-07 22:09:01 +0200190 pci_or_config8(dev, 0x43, 1 << 6);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200191
192 /* Additional programming steps */
Angel Ponsc803f652020-06-07 22:09:01 +0200193 pci_or_config32(dev, 0xc4, 1 << 13);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200194
Angel Ponsc803f652020-06-07 22:09:01 +0200195 pci_or_config32(dev, 0xc4, 1 << 10);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200196
Angel Ponsc803f652020-06-07 22:09:01 +0200197 pci_and_config32(dev, 0xd0, ~(1 << 31));
Stefan Reinauer8e073822012-04-04 00:07:22 +0200198
Stefan Reinauer15ba2bc2012-11-14 12:25:15 -0800199 if (dev->device == 0x1e20) {
200 /* Additional step on Panther Point */
Angel Ponsc803f652020-06-07 22:09:01 +0200201 pci_or_config32(dev, 0xc4, 1 << 17);
Stefan Reinauer15ba2bc2012-11-14 12:25:15 -0800202 }
203
Stefan Reinauer8e073822012-04-04 00:07:22 +0200204 /* Set Bus Master */
Elyes HAOUAS729c0692020-04-28 19:50:44 +0200205 pci_or_config16(dev, PCI_COMMAND, PCI_COMMAND_MASTER);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200206
207 pci_write_config8(dev, 0x3c, 0x0a); // unused?
208
209 /* Codec Initialization Programming Sequence */
Stefan Reinauer15ba2bc2012-11-14 12:25:15 -0800210
211 /* Take controller out of reset */
Elyes HAOUAS11178bd2020-08-03 15:34:46 +0200212 reg32 = read32(base + HDA_GCTL_REG);
213 reg32 |= HDA_GCTL_CRST;
214 write32(base + HDA_GCTL_REG, reg32);
Stefan Reinauer15ba2bc2012-11-14 12:25:15 -0800215 /* Wait 1ms */
216 udelay(1000);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200217
Angel Ponsc803f652020-06-07 22:09:01 +0200218 // Select Azalia mode. This needs to be controlled via devicetree.cb
219 pci_or_config8(dev, 0x40, 1); // Audio Control
Stefan Reinauer8e073822012-04-04 00:07:22 +0200220
Angel Ponsc803f652020-06-07 22:09:01 +0200221 // Docking not supported
222 pci_and_config8(dev, 0x4d, (u8)~(1 << 7)); // Docking Status
Stefan Reinauer8e073822012-04-04 00:07:22 +0200223
224 codec_mask = codec_detect(base);
225
226 if (codec_mask) {
227 printk(BIOS_DEBUG, "Azalia: codec_mask = %02x\n", codec_mask);
228 codecs_init(dev, base, codec_mask);
229 }
230
231 /* Enable dynamic clock gating */
Angel Ponsc803f652020-06-07 22:09:01 +0200232 pci_update_config8(dev, 0x43, ~0x07, (1 << 2) | (1 << 0));
Stefan Reinauer8e073822012-04-04 00:07:22 +0200233}
234
Aaron Durbinaa090cb2017-09-13 16:01:52 -0600235static const char *azalia_acpi_name(const struct device *dev)
Patrick Rudolph604f6982017-06-07 09:46:52 +0200236{
237 return "HDEF";
238}
239
Stefan Reinauer8e073822012-04-04 00:07:22 +0200240static struct device_operations azalia_ops = {
241 .read_resources = pci_dev_read_resources,
242 .set_resources = pci_dev_set_resources,
243 .enable_resources = pci_dev_enable_resources,
244 .init = azalia_init,
Angel Pons1fc0edd2020-05-31 00:03:28 +0200245 .ops_pci = &pci_dev_ops_pci,
Patrick Rudolph604f6982017-06-07 09:46:52 +0200246 .acpi_name = azalia_acpi_name,
Stefan Reinauer8e073822012-04-04 00:07:22 +0200247};
248
Stefan Reinauer9a380ab2012-06-22 13:16:11 -0700249static const unsigned short pci_device_ids[] = { 0x1c20, 0x1e20, 0 };
Stefan Reinauer8e073822012-04-04 00:07:22 +0200250
Stefan Reinauer9a380ab2012-06-22 13:16:11 -0700251static const struct pci_driver pch_azalia __pci_driver = {
252 .ops = &azalia_ops,
253 .vendor = PCI_VENDOR_ID_INTEL,
254 .devices = pci_device_ids,
Stefan Reinauer8e073822012-04-04 00:07:22 +0200255};