2020-04-02 23:49:05 +02:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0-only */
|
2009-01-20 23:54:59 +01:00
|
|
|
|
|
|
|
#include <console/console.h>
|
|
|
|
#include <device/device.h>
|
|
|
|
#include <device/pci.h>
|
|
|
|
#include <device/pci_ids.h>
|
|
|
|
#include <device/pci_ops.h>
|
2019-03-03 07:01:05 +01:00
|
|
|
#include <device/mmio.h>
|
2009-01-20 23:54:59 +01:00
|
|
|
#include <delay.h>
|
2014-09-05 01:01:31 +02:00
|
|
|
#include <device/azalia_device.h>
|
2019-08-18 15:33:39 +02:00
|
|
|
#include "chip.h"
|
2009-01-20 23:54:59 +01:00
|
|
|
#include "i82801gx.h"
|
|
|
|
|
2014-12-25 03:43:20 +01:00
|
|
|
static int set_bits(void *port, u32 mask, u32 val)
|
2009-01-20 23:54:59 +01:00
|
|
|
{
|
2009-03-11 15:54:18 +01:00
|
|
|
u32 reg32;
|
2009-01-20 23:54:59 +01:00
|
|
|
int count;
|
|
|
|
|
2009-03-11 15:54:18 +01:00
|
|
|
/* Write (val & mask) to port */
|
2009-01-20 23:54:59 +01:00
|
|
|
val &= mask;
|
2010-01-16 18:53:38 +01:00
|
|
|
reg32 = read32(port);
|
2009-03-11 15:54:18 +01:00
|
|
|
reg32 &= ~mask;
|
|
|
|
reg32 |= val;
|
2010-01-16 18:53:38 +01:00
|
|
|
write32(port, reg32);
|
2009-03-11 15:54:18 +01:00
|
|
|
|
2020-04-04 13:43:03 +02:00
|
|
|
/* Wait for readback of register to match what was just written to it */
|
2009-01-20 23:54:59 +01:00
|
|
|
count = 50;
|
|
|
|
do {
|
2009-03-11 15:54:18 +01:00
|
|
|
/* Wait 1ms based on BKDG wait time */
|
|
|
|
mdelay(1);
|
2010-01-16 18:53:38 +01:00
|
|
|
reg32 = read32(port);
|
2009-03-11 15:54:18 +01:00
|
|
|
reg32 &= mask;
|
|
|
|
} while ((reg32 != val) && --count);
|
2009-01-20 23:54:59 +01:00
|
|
|
|
2010-10-10 23:15:01 +02:00
|
|
|
/* Timeout occurred */
|
2009-01-20 23:54:59 +01:00
|
|
|
if (!count)
|
|
|
|
return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-12-25 03:43:20 +01:00
|
|
|
static int codec_detect(u8 *base)
|
2009-01-20 23:54:59 +01:00
|
|
|
{
|
2009-03-11 15:54:18 +01:00
|
|
|
u32 reg32;
|
|
|
|
|
2020-08-11 09:21:24 +02:00
|
|
|
/* Set Bit 0 to 0 to enter reset state (BAR + 0x8)[0] */
|
2020-08-03 15:35:16 +02:00
|
|
|
if (set_bits(base + HDA_GCTL_REG, 1, 0) == -1)
|
2009-03-11 15:54:18 +01:00
|
|
|
goto no_codec;
|
|
|
|
|
|
|
|
/* Set Bit 0 to 1 to exit reset state (BAR + 0x8)[0] */
|
2020-08-11 09:21:24 +02:00
|
|
|
if (set_bits(base + HDA_GCTL_REG, 1, HDA_GCTL_CRST) == -1)
|
2009-03-11 15:54:18 +01:00
|
|
|
goto no_codec;
|
|
|
|
|
2020-08-11 09:21:24 +02:00
|
|
|
/* Read in Codec location (BAR + 0xe)[2..0] */
|
2020-08-03 15:35:16 +02:00
|
|
|
reg32 = read32(base + HDA_STATESTS_REG);
|
2009-03-11 15:54:18 +01:00
|
|
|
reg32 &= 0x0f;
|
|
|
|
if (!reg32)
|
|
|
|
goto no_codec;
|
2009-08-12 18:08:05 +02:00
|
|
|
|
2009-03-11 15:54:18 +01:00
|
|
|
return reg32;
|
|
|
|
|
|
|
|
no_codec:
|
|
|
|
/* Codec Not found */
|
|
|
|
/* Put HDA back in reset (BAR + 0x8) [0] */
|
2020-08-03 15:35:16 +02:00
|
|
|
set_bits(base + HDA_GCTL_REG, 1, 0);
|
2010-03-22 12:42:32 +01:00
|
|
|
printk(BIOS_DEBUG, "Azalia: No codec!\n");
|
2009-03-11 15:54:18 +01:00
|
|
|
return 0;
|
2009-01-20 23:54:59 +01:00
|
|
|
}
|
|
|
|
|
2017-03-09 12:02:52 +01:00
|
|
|
static u32 find_verb(struct device *dev, u32 viddid, const u32 **verb)
|
2009-10-26 18:12:21 +01:00
|
|
|
{
|
2017-03-09 12:02:52 +01:00
|
|
|
int idx = 0;
|
2010-04-27 08:56:47 +02:00
|
|
|
|
2009-10-26 18:12:21 +01:00
|
|
|
while (idx < (cim_verb_data_size / sizeof(u32))) {
|
2020-08-11 09:21:24 +02:00
|
|
|
u32 verb_size = 4 * cim_verb_data[idx + 2]; // in u32
|
2009-10-26 18:12:21 +01:00
|
|
|
if (cim_verb_data[idx] != viddid) {
|
2020-08-11 09:21:24 +02:00
|
|
|
idx += verb_size + 3; // skip verb + header
|
2009-10-26 18:12:21 +01:00
|
|
|
continue;
|
|
|
|
}
|
2020-08-11 09:21:24 +02:00
|
|
|
*verb = &cim_verb_data[idx + 3];
|
2009-10-26 18:12:21 +01:00
|
|
|
return verb_size;
|
2009-03-11 15:54:18 +01:00
|
|
|
}
|
|
|
|
|
2009-10-26 18:12:21 +01:00
|
|
|
/* Not all codecs need to load another verb */
|
|
|
|
return 0;
|
2009-01-20 23:54:59 +01:00
|
|
|
}
|
|
|
|
|
2020-08-11 09:21:24 +02:00
|
|
|
/*
|
|
|
|
* Wait 50usec for the codec to indicate it is ready.
|
|
|
|
* No response would imply that the codec is non-operative.
|
2009-01-20 23:54:59 +01:00
|
|
|
*/
|
|
|
|
|
2014-12-25 03:43:20 +01:00
|
|
|
static int wait_for_ready(u8 *base)
|
2009-01-20 23:54:59 +01:00
|
|
|
{
|
2020-04-04 13:43:03 +02:00
|
|
|
/* Use a 50 usec timeout - the Linux kernel uses the same duration */
|
2009-01-20 23:54:59 +01:00
|
|
|
int timeout = 50;
|
|
|
|
|
2016-08-31 19:22:16 +02:00
|
|
|
while (timeout--) {
|
2014-12-25 03:43:20 +01:00
|
|
|
u32 reg32 = read32(base + HDA_ICII_REG);
|
2009-03-11 15:54:18 +01:00
|
|
|
if (!(reg32 & HDA_ICII_BUSY))
|
2009-01-20 23:54:59 +01:00
|
|
|
return 0;
|
|
|
|
udelay(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2020-08-11 09:21:24 +02:00
|
|
|
/*
|
|
|
|
* Wait 50usec for the codec to indicate that it accepted the previous command.
|
|
|
|
* No response would imply that the code is non-operative.
|
2009-01-20 23:54:59 +01:00
|
|
|
*/
|
|
|
|
|
2014-12-25 03:43:20 +01:00
|
|
|
static int wait_for_valid(u8 *base)
|
2009-01-20 23:54:59 +01:00
|
|
|
{
|
2009-10-26 18:12:21 +01:00
|
|
|
u32 reg32;
|
2020-08-11 09:21:24 +02:00
|
|
|
/* Use a 50 usec timeout - the Linux kernel uses the same duration */
|
|
|
|
int timeout = 50;
|
2009-10-26 18:12:21 +01:00
|
|
|
|
|
|
|
/* Send the verb to the codec */
|
2020-08-03 15:35:16 +02:00
|
|
|
reg32 = read32(base + HDA_ICII_REG);
|
|
|
|
reg32 |= HDA_ICII_BUSY | HDA_ICII_VALID;
|
|
|
|
write32(base + HDA_ICII_REG, reg32);
|
2009-10-26 18:12:21 +01:00
|
|
|
|
2016-08-31 19:22:16 +02:00
|
|
|
while (timeout--) {
|
2010-01-16 18:53:38 +01:00
|
|
|
reg32 = read32(base + HDA_ICII_REG);
|
2020-04-04 13:43:03 +02:00
|
|
|
if ((reg32 & (HDA_ICII_VALID | HDA_ICII_BUSY)) == HDA_ICII_VALID)
|
2009-01-20 23:54:59 +01:00
|
|
|
return 0;
|
|
|
|
udelay(1);
|
|
|
|
}
|
|
|
|
|
2009-10-26 18:12:21 +01:00
|
|
|
return -1;
|
2009-01-20 23:54:59 +01:00
|
|
|
}
|
|
|
|
|
2014-12-25 03:43:20 +01:00
|
|
|
static void codec_init(struct device *dev, u8 *base, int addr)
|
2009-01-20 23:54:59 +01:00
|
|
|
{
|
2009-03-11 15:54:18 +01:00
|
|
|
u32 reg32;
|
2010-06-05 12:03:08 +02:00
|
|
|
const u32 *verb;
|
2009-01-20 23:54:59 +01:00
|
|
|
u32 verb_size;
|
|
|
|
int i;
|
|
|
|
|
2010-03-22 12:42:32 +01:00
|
|
|
printk(BIOS_DEBUG, "Azalia: Initializing codec #%d\n", addr);
|
2009-10-26 18:12:21 +01:00
|
|
|
|
2009-01-20 23:54:59 +01:00
|
|
|
/* 1 */
|
2020-08-11 09:21:24 +02:00
|
|
|
if (wait_for_ready(base) == -1) {
|
|
|
|
printk(BIOS_DEBUG, " codec not ready.\n");
|
2009-01-20 23:54:59 +01:00
|
|
|
return;
|
2020-08-11 09:21:24 +02:00
|
|
|
}
|
2009-01-20 23:54:59 +01:00
|
|
|
|
2009-03-11 15:54:18 +01:00
|
|
|
reg32 = (addr << 28) | 0x000f0000;
|
2020-08-03 15:35:16 +02:00
|
|
|
write32(base + HDA_IC_REG, reg32);
|
2009-01-20 23:54:59 +01:00
|
|
|
|
2020-08-11 09:21:24 +02:00
|
|
|
if (wait_for_valid(base) == -1) {
|
|
|
|
printk(BIOS_DEBUG, " codec not valid.\n");
|
2009-01-20 23:54:59 +01:00
|
|
|
return;
|
2020-08-11 09:21:24 +02:00
|
|
|
}
|
2009-01-20 23:54:59 +01:00
|
|
|
|
|
|
|
/* 2 */
|
2020-08-11 09:21:24 +02:00
|
|
|
reg32 = read32(base + HDA_IR_REG);
|
2010-03-22 12:42:32 +01:00
|
|
|
printk(BIOS_DEBUG, "Azalia: codec viddid: %08x\n", reg32);
|
2009-03-11 15:54:18 +01:00
|
|
|
verb_size = find_verb(dev, reg32, &verb);
|
2009-01-20 23:54:59 +01:00
|
|
|
|
|
|
|
if (!verb_size) {
|
2010-03-22 12:42:32 +01:00
|
|
|
printk(BIOS_DEBUG, "Azalia: No verb!\n");
|
2009-01-20 23:54:59 +01:00
|
|
|
return;
|
|
|
|
}
|
2010-03-22 12:42:32 +01:00
|
|
|
printk(BIOS_DEBUG, "Azalia: verb_size: %d\n", verb_size);
|
2009-10-26 18:12:21 +01:00
|
|
|
|
2009-01-20 23:54:59 +01:00
|
|
|
/* 3 */
|
|
|
|
for (i = 0; i < verb_size; i++) {
|
|
|
|
if (wait_for_ready(base) == -1)
|
|
|
|
return;
|
|
|
|
|
2020-08-03 15:35:16 +02:00
|
|
|
write32(base + HDA_IC_REG, verb[i]);
|
2009-01-20 23:54:59 +01:00
|
|
|
|
|
|
|
if (wait_for_valid(base) == -1)
|
|
|
|
return;
|
|
|
|
}
|
2010-03-22 12:42:32 +01:00
|
|
|
printk(BIOS_DEBUG, "Azalia: verb loaded.\n");
|
2009-01-20 23:54:59 +01:00
|
|
|
}
|
|
|
|
|
2014-12-25 03:43:20 +01:00
|
|
|
static void codecs_init(struct device *dev, u8 *base, u32 codec_mask)
|
2009-01-20 23:54:59 +01:00
|
|
|
{
|
|
|
|
int i;
|
2020-08-11 09:21:24 +02:00
|
|
|
|
2009-01-20 23:54:59 +01:00
|
|
|
for (i = 2; i >= 0; i--) {
|
|
|
|
if (codec_mask & (1 << i))
|
2009-03-11 15:54:18 +01:00
|
|
|
codec_init(dev, base, i);
|
2009-01-20 23:54:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void azalia_init(struct device *dev)
|
|
|
|
{
|
2014-12-25 03:43:20 +01:00
|
|
|
u8 *base;
|
2009-01-20 23:54:59 +01:00
|
|
|
struct resource *res;
|
|
|
|
u32 codec_mask;
|
2009-03-11 15:54:18 +01:00
|
|
|
u8 reg8;
|
|
|
|
|
|
|
|
// ESD
|
2020-06-08 12:32:54 +02:00
|
|
|
pci_update_config32(dev, 0x134, ~(0xff << 16), 2 << 16);
|
2009-03-11 15:54:18 +01:00
|
|
|
|
|
|
|
// Link1 description
|
2020-06-08 12:32:54 +02:00
|
|
|
pci_update_config32(dev, 0x140, ~(0xff << 16), 2 << 16);
|
2009-03-11 15:54:18 +01:00
|
|
|
|
|
|
|
// Port VC0 Resource Control Register
|
2020-06-08 12:32:54 +02:00
|
|
|
pci_update_config32(dev, 0x114, ~(0xff << 0), 1);
|
2009-03-11 15:54:18 +01:00
|
|
|
|
|
|
|
// VCi traffic class
|
2020-06-08 12:32:54 +02:00
|
|
|
pci_or_config8(dev, 0x44, 7 << 0); // TC7
|
2009-03-11 15:54:18 +01:00
|
|
|
|
|
|
|
// VCi Resource Control
|
2020-06-08 12:32:54 +02:00
|
|
|
pci_or_config32(dev, 0x120, (1 << 31) | (1 << 24) | (0x80 << 0)); /* VCi ID and map */
|
2009-03-11 15:54:18 +01:00
|
|
|
|
|
|
|
/* Set Bus Master */
|
2020-04-27 05:08:26 +02:00
|
|
|
pci_or_config16(dev, PCI_COMMAND, PCI_COMMAND_MASTER);
|
2009-03-11 15:54:18 +01:00
|
|
|
|
|
|
|
pci_write_config8(dev, 0x3c, 0x0a); // unused?
|
|
|
|
|
|
|
|
// TODO Actually check if we're AC97 or HDA instead of hardcoding this
|
2010-02-08 13:20:50 +01:00
|
|
|
// here, in devicetree.cb and/or romstage.c.
|
2009-03-11 15:54:18 +01:00
|
|
|
reg8 = pci_read_config8(dev, 0x40);
|
|
|
|
reg8 |= (1 << 3); // Clear Clock Detect Bit
|
|
|
|
pci_write_config8(dev, 0x40, reg8);
|
|
|
|
reg8 &= ~(1 << 3); // Keep CLKDETCLR from clearing the bit over and over
|
|
|
|
pci_write_config8(dev, 0x40, reg8);
|
|
|
|
reg8 |= (1 << 2); // Enable clock detection
|
|
|
|
pci_write_config8(dev, 0x40, reg8);
|
|
|
|
mdelay(1);
|
|
|
|
reg8 = pci_read_config8(dev, 0x40);
|
2010-03-22 12:42:32 +01:00
|
|
|
printk(BIOS_DEBUG, "Azalia: codec type: %s\n", (reg8 & (1 << 1))?"Azalia":"AC97");
|
2009-03-11 15:54:18 +01:00
|
|
|
|
2020-06-08 12:32:54 +02:00
|
|
|
// Select Azalia mode. This needs to be controlled via devicetree.cb
|
|
|
|
pci_or_config8(dev, 0x40, 1); // Audio Control
|
2009-03-11 15:54:18 +01:00
|
|
|
|
2020-06-08 12:32:54 +02:00
|
|
|
// Docking not supported
|
|
|
|
pci_and_config8(dev, 0x4d, (u8)~(1 << 7)); // Docking Status
|
2009-01-20 23:54:59 +01:00
|
|
|
|
2020-08-11 09:21:24 +02:00
|
|
|
res = find_resource(dev, PCI_BASE_ADDRESS_0);
|
2009-01-20 23:54:59 +01:00
|
|
|
if (!res)
|
|
|
|
return;
|
|
|
|
|
2020-08-11 09:21:24 +02:00
|
|
|
// NOTE this will break as soon as the Azalia get's a bar above 4G.
|
|
|
|
// Is there anything we can do about it?
|
2014-12-25 03:43:20 +01:00
|
|
|
base = res2mmio(res, 0, 0);
|
2010-03-22 12:42:32 +01:00
|
|
|
printk(BIOS_DEBUG, "Azalia: base = %08x\n", (u32)base);
|
2009-01-20 23:54:59 +01:00
|
|
|
codec_mask = codec_detect(base);
|
|
|
|
|
|
|
|
if (codec_mask) {
|
2010-03-22 12:42:32 +01:00
|
|
|
printk(BIOS_DEBUG, "Azalia: codec_mask = %02x\n", codec_mask);
|
2009-03-11 15:54:18 +01:00
|
|
|
codecs_init(dev, base, codec_mask);
|
2009-01-20 23:54:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct device_operations azalia_ops = {
|
|
|
|
.read_resources = pci_dev_read_resources,
|
|
|
|
.set_resources = pci_dev_set_resources,
|
|
|
|
.enable_resources = pci_dev_enable_resources,
|
|
|
|
.init = azalia_init,
|
|
|
|
.enable = i82801gx_enable,
|
2020-05-31 00:03:28 +02:00
|
|
|
.ops_pci = &pci_dev_ops_pci,
|
2009-01-20 23:54:59 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/* 82801GB/GR/GDH/GBM/GHM (ICH7/ICH7R/ICH7DH/ICH7-M/ICH7-M DH) */
|
|
|
|
static const struct pci_driver i82801gx_azalia __pci_driver = {
|
|
|
|
.ops = &azalia_ops,
|
|
|
|
.vendor = PCI_VENDOR_ID_INTEL,
|
|
|
|
.device = 0x27d8,
|
|
|
|
};
|