blob: fde06b4aacc4c2b29a13595d0d041392894aa0fa [file] [log] [blame]
Angel Pons182dbde2020-04-02 23:49:05 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Stefan Reinauer679c9f92009-01-20 22:54:59 +00002
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 Reinauer679c9f92009-01-20 22:54:59 +00009#include <delay.h>
Vladimir Serbinenko75c83872014-09-05 01:01:31 +020010#include <device/azalia_device.h>
Arthur Heymans028b94b2021-07-02 10:06:55 +020011#include <stdint.h>
Kyösti Mälkki12b121c2019-08-18 16:33:39 +030012#include "chip.h"
Stefan Reinauer679c9f92009-01-20 22:54:59 +000013#include "i82801gx.h"
14
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080015static int codec_detect(u8 *base)
Stefan Reinauer679c9f92009-01-20 22:54:59 +000016{
Stefan Reinauera8e11682009-03-11 14:54:18 +000017 u32 reg32;
Stefan Reinauer679c9f92009-01-20 22:54:59 +000018
Angel Pons2e0053b2020-12-05 19:06:55 +010019 if (azalia_enter_reset(base) < 0)
Stefan Reinauera8e11682009-03-11 14:54:18 +000020 goto no_codec;
Stefan Reinauer679c9f92009-01-20 22:54:59 +000021
Angel Pons7f839f62020-12-05 19:02:14 +010022 if (azalia_exit_reset(base) < 0)
Stefan Reinauera8e11682009-03-11 14:54:18 +000023 goto no_codec;
Stefan Reinauer679c9f92009-01-20 22:54:59 +000024
Elyes HAOUAS6ea24ff2020-08-11 09:21:24 +020025 /* Read in Codec location (BAR + 0xe)[2..0] */
Elyes HAOUASf1da9092020-08-03 15:35:16 +020026 reg32 = read32(base + HDA_STATESTS_REG);
Stefan Reinauera8e11682009-03-11 14:54:18 +000027 reg32 &= 0x0f;
28 if (!reg32)
29 goto no_codec;
Stefan Reinauer109ab312009-08-12 16:08:05 +000030
Stefan Reinauera8e11682009-03-11 14:54:18 +000031 return reg32;
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 Reinauerc02b4fc2010-03-22 11:42:32 +000036 printk(BIOS_DEBUG, "Azalia: No codec!\n");
Stefan Reinauera8e11682009-03-11 14:54:18 +000037 return 0;
Stefan Reinauer679c9f92009-01-20 22:54:59 +000038}
39
Elyes HAOUAS6ea24ff2020-08-11 09:21:24 +020040/*
41 * Wait 50usec for the codec to indicate it is ready.
42 * No response would imply that the codec is non-operative.
Stefan Reinauer679c9f92009-01-20 22:54:59 +000043 */
44
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080045static int wait_for_ready(u8 *base)
Stefan Reinauer679c9f92009-01-20 22:54:59 +000046{
Elyes HAOUAS92646ea2020-04-04 13:43:03 +020047 /* Use a 50 usec timeout - the Linux kernel uses the same duration */
Stefan Reinauer679c9f92009-01-20 22:54:59 +000048 int timeout = 50;
49
Elyes HAOUASba28e8d2016-08-31 19:22:16 +020050 while (timeout--) {
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080051 u32 reg32 = read32(base + HDA_ICII_REG);
Stefan Reinauera8e11682009-03-11 14:54:18 +000052 if (!(reg32 & HDA_ICII_BUSY))
Stefan Reinauer679c9f92009-01-20 22:54:59 +000053 return 0;
54 udelay(1);
55 }
56
57 return -1;
58}
59
Elyes HAOUAS6ea24ff2020-08-11 09:21:24 +020060/*
61 * Wait 50usec for the codec to indicate that it accepted the previous command.
62 * No response would imply that the code is non-operative.
Stefan Reinauer679c9f92009-01-20 22:54:59 +000063 */
64
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080065static int wait_for_valid(u8 *base)
Stefan Reinauer679c9f92009-01-20 22:54:59 +000066{
Stefan Reinaueraca6ec62009-10-26 17:12:21 +000067 u32 reg32;
Elyes HAOUAS6ea24ff2020-08-11 09:21:24 +020068 /* Use a 50 usec timeout - the Linux kernel uses the same duration */
69 int timeout = 50;
Stefan Reinaueraca6ec62009-10-26 17:12:21 +000070
71 /* Send the verb to the codec */
Elyes HAOUASf1da9092020-08-03 15:35:16 +020072 reg32 = read32(base + HDA_ICII_REG);
73 reg32 |= HDA_ICII_BUSY | HDA_ICII_VALID;
74 write32(base + HDA_ICII_REG, reg32);
Stefan Reinaueraca6ec62009-10-26 17:12:21 +000075
Elyes HAOUASba28e8d2016-08-31 19:22:16 +020076 while (timeout--) {
Stefan Reinauer9fe4d792010-01-16 17:53:38 +000077 reg32 = read32(base + HDA_ICII_REG);
Elyes HAOUAS92646ea2020-04-04 13:43:03 +020078 if ((reg32 & (HDA_ICII_VALID | HDA_ICII_BUSY)) == HDA_ICII_VALID)
Stefan Reinauer679c9f92009-01-20 22:54:59 +000079 return 0;
80 udelay(1);
81 }
82
Stefan Reinaueraca6ec62009-10-26 17:12:21 +000083 return -1;
Stefan Reinauer679c9f92009-01-20 22:54:59 +000084}
85
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080086static void codec_init(struct device *dev, u8 *base, int addr)
Stefan Reinauer679c9f92009-01-20 22:54:59 +000087{
Stefan Reinauera8e11682009-03-11 14:54:18 +000088 u32 reg32;
Stefan Reinauerc4f1a772010-06-05 10:03:08 +000089 const u32 *verb;
Stefan Reinauer679c9f92009-01-20 22:54:59 +000090 u32 verb_size;
91 int i;
92
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +000093 printk(BIOS_DEBUG, "Azalia: Initializing codec #%d\n", addr);
Stefan Reinaueraca6ec62009-10-26 17:12:21 +000094
Stefan Reinauer679c9f92009-01-20 22:54:59 +000095 /* 1 */
Angel Pons554713e2020-10-24 23:23:07 +020096 if (wait_for_ready(base) < 0) {
Elyes HAOUAS6ea24ff2020-08-11 09:21:24 +020097 printk(BIOS_DEBUG, " codec not ready.\n");
Stefan Reinauer679c9f92009-01-20 22:54:59 +000098 return;
Elyes HAOUAS6ea24ff2020-08-11 09:21:24 +020099 }
Stefan Reinauer679c9f92009-01-20 22:54:59 +0000100
Stefan Reinauera8e11682009-03-11 14:54:18 +0000101 reg32 = (addr << 28) | 0x000f0000;
Elyes HAOUASf1da9092020-08-03 15:35:16 +0200102 write32(base + HDA_IC_REG, reg32);
Stefan Reinauer679c9f92009-01-20 22:54:59 +0000103
Angel Pons554713e2020-10-24 23:23:07 +0200104 if (wait_for_valid(base) < 0) {
Elyes HAOUAS6ea24ff2020-08-11 09:21:24 +0200105 printk(BIOS_DEBUG, " codec not valid.\n");
Stefan Reinauer679c9f92009-01-20 22:54:59 +0000106 return;
Elyes HAOUAS6ea24ff2020-08-11 09:21:24 +0200107 }
Stefan Reinauer679c9f92009-01-20 22:54:59 +0000108
109 /* 2 */
Elyes HAOUAS6ea24ff2020-08-11 09:21:24 +0200110 reg32 = read32(base + HDA_IR_REG);
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000111 printk(BIOS_DEBUG, "Azalia: codec viddid: %08x\n", reg32);
Angel Ponsd425ddd2020-12-05 18:22:58 +0100112 verb_size = azalia_find_verb(cim_verb_data, cim_verb_data_size, reg32, &verb);
Stefan Reinauer679c9f92009-01-20 22:54:59 +0000113
114 if (!verb_size) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000115 printk(BIOS_DEBUG, "Azalia: No verb!\n");
Stefan Reinauer679c9f92009-01-20 22:54:59 +0000116 return;
117 }
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000118 printk(BIOS_DEBUG, "Azalia: verb_size: %d\n", verb_size);
Stefan Reinaueraca6ec62009-10-26 17:12:21 +0000119
Stefan Reinauer679c9f92009-01-20 22:54:59 +0000120 /* 3 */
121 for (i = 0; i < verb_size; i++) {
Angel Pons554713e2020-10-24 23:23:07 +0200122 if (wait_for_ready(base) < 0)
Stefan Reinauer679c9f92009-01-20 22:54:59 +0000123 return;
124
Elyes HAOUASf1da9092020-08-03 15:35:16 +0200125 write32(base + HDA_IC_REG, verb[i]);
Stefan Reinauer679c9f92009-01-20 22:54:59 +0000126
Angel Pons554713e2020-10-24 23:23:07 +0200127 if (wait_for_valid(base) < 0)
Stefan Reinauer679c9f92009-01-20 22:54:59 +0000128 return;
129 }
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000130 printk(BIOS_DEBUG, "Azalia: verb loaded.\n");
Stefan Reinauer679c9f92009-01-20 22:54:59 +0000131}
132
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800133static void codecs_init(struct device *dev, u8 *base, u32 codec_mask)
Stefan Reinauer679c9f92009-01-20 22:54:59 +0000134{
135 int i;
Elyes HAOUAS6ea24ff2020-08-11 09:21:24 +0200136
Stefan Reinauer679c9f92009-01-20 22:54:59 +0000137 for (i = 2; i >= 0; i--) {
138 if (codec_mask & (1 << i))
Stefan Reinauera8e11682009-03-11 14:54:18 +0000139 codec_init(dev, base, i);
Stefan Reinauer679c9f92009-01-20 22:54:59 +0000140 }
141}
142
143static void azalia_init(struct device *dev)
144{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800145 u8 *base;
Stefan Reinauer679c9f92009-01-20 22:54:59 +0000146 struct resource *res;
147 u32 codec_mask;
Stefan Reinauera8e11682009-03-11 14:54:18 +0000148 u8 reg8;
Stefan Reinauer679c9f92009-01-20 22:54:59 +0000149
Stefan Reinauera8e11682009-03-11 14:54:18 +0000150 // ESD
Angel Ponsd19332c2020-06-08 12:32:54 +0200151 pci_update_config32(dev, 0x134, ~(0xff << 16), 2 << 16);
Stefan Reinauera8e11682009-03-11 14:54:18 +0000152
153 // Link1 description
Angel Ponsd19332c2020-06-08 12:32:54 +0200154 pci_update_config32(dev, 0x140, ~(0xff << 16), 2 << 16);
Stefan Reinauera8e11682009-03-11 14:54:18 +0000155
156 // Port VC0 Resource Control Register
Angel Ponsd19332c2020-06-08 12:32:54 +0200157 pci_update_config32(dev, 0x114, ~(0xff << 0), 1);
Stefan Reinauera8e11682009-03-11 14:54:18 +0000158
159 // VCi traffic class
Angel Ponsd19332c2020-06-08 12:32:54 +0200160 pci_or_config8(dev, 0x44, 7 << 0); // TC7
Stefan Reinauera8e11682009-03-11 14:54:18 +0000161
162 // VCi Resource Control
Angel Ponsd19332c2020-06-08 12:32:54 +0200163 pci_or_config32(dev, 0x120, (1 << 31) | (1 << 24) | (0x80 << 0)); /* VCi ID and map */
Stefan Reinauera8e11682009-03-11 14:54:18 +0000164
165 /* Set Bus Master */
Elyes HAOUAS12349252020-04-27 05:08:26 +0200166 pci_or_config16(dev, PCI_COMMAND, PCI_COMMAND_MASTER);
Stefan Reinauera8e11682009-03-11 14:54:18 +0000167
168 pci_write_config8(dev, 0x3c, 0x0a); // unused?
169
170 // TODO Actually check if we're AC97 or HDA instead of hardcoding this
Stefan Reinauer38f147e2010-02-08 12:20:50 +0000171 // here, in devicetree.cb and/or romstage.c.
Stefan Reinauera8e11682009-03-11 14:54:18 +0000172 reg8 = pci_read_config8(dev, 0x40);
173 reg8 |= (1 << 3); // Clear Clock Detect Bit
174 pci_write_config8(dev, 0x40, reg8);
175 reg8 &= ~(1 << 3); // Keep CLKDETCLR from clearing the bit over and over
176 pci_write_config8(dev, 0x40, reg8);
177 reg8 |= (1 << 2); // Enable clock detection
178 pci_write_config8(dev, 0x40, reg8);
179 mdelay(1);
180 reg8 = pci_read_config8(dev, 0x40);
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000181 printk(BIOS_DEBUG, "Azalia: codec type: %s\n", (reg8 & (1 << 1))?"Azalia":"AC97");
Stefan Reinauera8e11682009-03-11 14:54:18 +0000182
Angel Ponsd19332c2020-06-08 12:32:54 +0200183 // Select Azalia mode. This needs to be controlled via devicetree.cb
184 pci_or_config8(dev, 0x40, 1); // Audio Control
Stefan Reinauera8e11682009-03-11 14:54:18 +0000185
Angel Ponsd19332c2020-06-08 12:32:54 +0200186 // Docking not supported
187 pci_and_config8(dev, 0x4d, (u8)~(1 << 7)); // Docking Status
Stefan Reinauer679c9f92009-01-20 22:54:59 +0000188
Elyes HAOUAS6ea24ff2020-08-11 09:21:24 +0200189 res = find_resource(dev, PCI_BASE_ADDRESS_0);
Stefan Reinauer679c9f92009-01-20 22:54:59 +0000190 if (!res)
191 return;
192
Elyes HAOUAS6ea24ff2020-08-11 09:21:24 +0200193 // NOTE this will break as soon as the Azalia get's a bar above 4G.
194 // Is there anything we can do about it?
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800195 base = res2mmio(res, 0, 0);
Arthur Heymans028b94b2021-07-02 10:06:55 +0200196 printk(BIOS_DEBUG, "Azalia: base = %08x\n", (u32)(uintptr_t)base);
Stefan Reinauer679c9f92009-01-20 22:54:59 +0000197 codec_mask = codec_detect(base);
198
199 if (codec_mask) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000200 printk(BIOS_DEBUG, "Azalia: codec_mask = %02x\n", codec_mask);
Stefan Reinauera8e11682009-03-11 14:54:18 +0000201 codecs_init(dev, base, codec_mask);
Stefan Reinauer679c9f92009-01-20 22:54:59 +0000202 }
203}
204
205static struct device_operations azalia_ops = {
206 .read_resources = pci_dev_read_resources,
207 .set_resources = pci_dev_set_resources,
208 .enable_resources = pci_dev_enable_resources,
209 .init = azalia_init,
Stefan Reinauer679c9f92009-01-20 22:54:59 +0000210 .enable = i82801gx_enable,
Angel Pons1fc0edd2020-05-31 00:03:28 +0200211 .ops_pci = &pci_dev_ops_pci,
Stefan Reinauer679c9f92009-01-20 22:54:59 +0000212};
213
214/* 82801GB/GR/GDH/GBM/GHM (ICH7/ICH7R/ICH7DH/ICH7-M/ICH7-M DH) */
215static const struct pci_driver i82801gx_azalia __pci_driver = {
216 .ops = &azalia_ops,
217 .vendor = PCI_VENDOR_ID_INTEL,
218 .device = 0x27d8,
219};