blob: ddaa8a1bb00b35292a6f26296ea5c93963d54d75 [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
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080015static int codec_detect(u8 *base)
Stefan Reinauer8e073822012-04-04 00:07:22 +020016{
17 u8 reg8;
18
Angel Pons7f839f62020-12-05 19:02:14 +010019 if (azalia_exit_reset(base) < 0)
Stefan Reinauer8e073822012-04-04 00:07:22 +020020 goto no_codec;
21
22 /* Write back the value once reset bit is set. */
Elyes HAOUAS6ea24ff2020-08-11 09:21:24 +020023 write16(base + HDA_GCAP_REG, read16(base + HDA_GCAP_REG));
Stefan Reinauer8e073822012-04-04 00:07:22 +020024
Elyes HAOUAS6ea24ff2020-08-11 09:21:24 +020025 /* Read in Codec location (BAR + 0xe)[2..0] */
Elyes HAOUAS11178bd2020-08-03 15:34:46 +020026 reg8 = read8(base + HDA_STATESTS_REG);
Stefan Reinauer8e073822012-04-04 00:07:22 +020027 reg8 &= 0x0f;
28 if (!reg8)
29 goto no_codec;
30
31 return reg8;
32
33no_codec:
Angel Pons2e0053b2020-12-05 19:06:55 +010034 /* Codec not found, put HDA back in reset */
35 azalia_enter_reset(base);
Stefan Reinauer8e073822012-04-04 00:07:22 +020036 printk(BIOS_DEBUG, "Azalia: No codec!\n");
37 return 0;
38}
39
Stefan Reinauer8e073822012-04-04 00:07:22 +020040static void azalia_init(struct device *dev)
41{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080042 u8 *base;
Stefan Reinauer8e073822012-04-04 00:07:22 +020043 struct resource *res;
44 u32 codec_mask;
Stefan Reinauer8e073822012-04-04 00:07:22 +020045 u32 reg32;
46
Angel Ponsf32ae102021-11-03 13:07:14 +010047 res = probe_resource(dev, PCI_BASE_ADDRESS_0);
Stefan Reinauer8e073822012-04-04 00:07:22 +020048 if (!res)
49 return;
50
Martin Roth26f97f92021-10-01 14:53:22 -060051 // NOTE this will break as soon as the Azalia gets a bar above 4G.
Elyes HAOUAS6ea24ff2020-08-11 09:21:24 +020052 // Is there anything we can do about it?
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080053 base = res2mmio(res, 0, 0);
Patrick Rudolphb50b6a52020-08-20 16:50:01 +020054 printk(BIOS_DEBUG, "Azalia: base = %p\n", base);
Stefan Reinauer8e073822012-04-04 00:07:22 +020055
Patrick Rudolph4f8b1082019-07-14 11:54:58 +020056 if (RCBA32(CIR31) & (1 << 31)) {
Kyösti Mälkkifd98c652013-07-26 08:50:53 +030057 reg32 = pci_read_config32(dev, 0x120);
Stefan Reinauer8e073822012-04-04 00:07:22 +020058 reg32 &= 0xf8ffff01;
Stefan Reinauer15ba2bc2012-11-14 12:25:15 -080059 reg32 |= (1 << 24); // 2 << 24 for server
Patrick Rudolph4f8b1082019-07-14 11:54:58 +020060 reg32 |= RCBA32(CIR31) & 0xfe;
Kyösti Mälkkifd98c652013-07-26 08:50:53 +030061 pci_write_config32(dev, 0x120, reg32);
Stefan Reinauer8e073822012-04-04 00:07:22 +020062
Angel Ponsc803f652020-06-07 22:09:01 +020063 pci_or_config16(dev, 0x78, 1 << 11);
Stefan Reinauer8e073822012-04-04 00:07:22 +020064 } else
65 printk(BIOS_DEBUG, "Azalia: V1CTL disabled.\n");
66
Angel Ponsc803f652020-06-07 22:09:01 +020067 pci_and_config32(dev, 0x114, ~0xfe);
Stefan Reinauer8e073822012-04-04 00:07:22 +020068
69 // Set VCi enable bit
Angel Ponsc803f652020-06-07 22:09:01 +020070 pci_or_config32(dev, 0x120, 1 << 31);
Stefan Reinauer8e073822012-04-04 00:07:22 +020071
72 // Enable HDMI codec:
Angel Ponsc803f652020-06-07 22:09:01 +020073 pci_or_config32(dev, 0xc4, 1 << 1);
Stefan Reinauer8e073822012-04-04 00:07:22 +020074
Angel Ponsc803f652020-06-07 22:09:01 +020075 pci_or_config8(dev, 0x43, 1 << 6);
Stefan Reinauer8e073822012-04-04 00:07:22 +020076
77 /* Additional programming steps */
Angel Ponsc803f652020-06-07 22:09:01 +020078 pci_or_config32(dev, 0xc4, 1 << 13);
Stefan Reinauer8e073822012-04-04 00:07:22 +020079
Angel Ponsc803f652020-06-07 22:09:01 +020080 pci_or_config32(dev, 0xc4, 1 << 10);
Stefan Reinauer8e073822012-04-04 00:07:22 +020081
Angel Ponsc803f652020-06-07 22:09:01 +020082 pci_and_config32(dev, 0xd0, ~(1 << 31));
Stefan Reinauer8e073822012-04-04 00:07:22 +020083
Stefan Reinauer15ba2bc2012-11-14 12:25:15 -080084 if (dev->device == 0x1e20) {
85 /* Additional step on Panther Point */
Angel Ponsc803f652020-06-07 22:09:01 +020086 pci_or_config32(dev, 0xc4, 1 << 17);
Stefan Reinauer15ba2bc2012-11-14 12:25:15 -080087 }
88
Stefan Reinauer8e073822012-04-04 00:07:22 +020089 /* Set Bus Master */
Elyes HAOUAS729c0692020-04-28 19:50:44 +020090 pci_or_config16(dev, PCI_COMMAND, PCI_COMMAND_MASTER);
Stefan Reinauer8e073822012-04-04 00:07:22 +020091
92 pci_write_config8(dev, 0x3c, 0x0a); // unused?
93
94 /* Codec Initialization Programming Sequence */
Stefan Reinauer15ba2bc2012-11-14 12:25:15 -080095
96 /* Take controller out of reset */
Elyes HAOUAS11178bd2020-08-03 15:34:46 +020097 reg32 = read32(base + HDA_GCTL_REG);
98 reg32 |= HDA_GCTL_CRST;
99 write32(base + HDA_GCTL_REG, reg32);
Stefan Reinauer15ba2bc2012-11-14 12:25:15 -0800100 /* Wait 1ms */
101 udelay(1000);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200102
Angel Ponsc803f652020-06-07 22:09:01 +0200103 // Select Azalia mode. This needs to be controlled via devicetree.cb
104 pci_or_config8(dev, 0x40, 1); // Audio Control
Stefan Reinauer8e073822012-04-04 00:07:22 +0200105
Angel Ponsc803f652020-06-07 22:09:01 +0200106 // Docking not supported
107 pci_and_config8(dev, 0x4d, (u8)~(1 << 7)); // Docking Status
Stefan Reinauer8e073822012-04-04 00:07:22 +0200108
109 codec_mask = codec_detect(base);
110
111 if (codec_mask) {
112 printk(BIOS_DEBUG, "Azalia: codec_mask = %02x\n", codec_mask);
Angel Ponsaae6b552021-11-10 18:10:38 +0100113 azalia_codecs_init(base, codec_mask);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200114 }
115
116 /* Enable dynamic clock gating */
Angel Ponsc803f652020-06-07 22:09:01 +0200117 pci_update_config8(dev, 0x43, ~0x07, (1 << 2) | (1 << 0));
Stefan Reinauer8e073822012-04-04 00:07:22 +0200118}
119
Aaron Durbinaa090cb2017-09-13 16:01:52 -0600120static const char *azalia_acpi_name(const struct device *dev)
Patrick Rudolph604f6982017-06-07 09:46:52 +0200121{
122 return "HDEF";
123}
124
Felix Helda56ff902023-11-16 14:20:40 +0100125struct device_operations bd82x6x_azalia_ops = {
Stefan Reinauer8e073822012-04-04 00:07:22 +0200126 .read_resources = pci_dev_read_resources,
127 .set_resources = pci_dev_set_resources,
128 .enable_resources = pci_dev_enable_resources,
129 .init = azalia_init,
Angel Pons1fc0edd2020-05-31 00:03:28 +0200130 .ops_pci = &pci_dev_ops_pci,
Patrick Rudolph604f6982017-06-07 09:46:52 +0200131 .acpi_name = azalia_acpi_name,
Stefan Reinauer8e073822012-04-04 00:07:22 +0200132};