coreboot
coreboot is an Open Source project aimed at replacing the proprietary BIOS found in most computers.
azalia.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 
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>
8 #include <device/mmio.h>
9 #include <delay.h>
10 #include <device/azalia_device.h>
11 #include <stdint.h>
12 #include "chip.h"
13 #include "i82801gx.h"
14 
15 static int codec_detect(u8 *base)
16 {
17  u32 reg32;
18 
19  if (azalia_enter_reset(base) < 0)
20  goto no_codec;
21 
22  if (azalia_exit_reset(base) < 0)
23  goto no_codec;
24 
25  /* Read in Codec location (BAR + 0xe)[2..0] */
26  reg32 = read32(base + HDA_STATESTS_REG);
27  reg32 &= 0x0f;
28  if (!reg32)
29  goto no_codec;
30 
31  return reg32;
32 
33 no_codec:
34  /* Codec not found, put HDA back in reset */
36  printk(BIOS_DEBUG, "Azalia: No codec!\n");
37  return 0;
38 }
39 
40 static void azalia_init(struct device *dev)
41 {
42  u8 *base;
43  struct resource *res;
44  u32 codec_mask;
45  u8 reg8;
46 
47  // ESD
48  pci_update_config32(dev, 0x134, ~(0xff << 16), 2 << 16);
49 
50  // Link1 description
51  pci_update_config32(dev, 0x140, ~(0xff << 16), 2 << 16);
52 
53  // Port VC0 Resource Control Register
54  pci_update_config32(dev, 0x114, ~(0xff << 0), 1);
55 
56  // VCi traffic class
57  pci_or_config8(dev, 0x44, 7 << 0); // TC7
58 
59  // VCi Resource Control
60  pci_or_config32(dev, 0x120, (1 << 31) | (1 << 24) | (0x80 << 0)); /* VCi ID and map */
61 
62  /* Set Bus Master */
64 
65  pci_write_config8(dev, 0x3c, 0x0a); // unused?
66 
67  // TODO Actually check if we're AC97 or HDA instead of hardcoding this
68  // here, in devicetree.cb and/or romstage.c.
69  reg8 = pci_read_config8(dev, 0x40);
70  reg8 |= (1 << 3); // Clear Clock Detect Bit
71  pci_write_config8(dev, 0x40, reg8);
72  reg8 &= ~(1 << 3); // Keep CLKDETCLR from clearing the bit over and over
73  pci_write_config8(dev, 0x40, reg8);
74  reg8 |= (1 << 2); // Enable clock detection
75  pci_write_config8(dev, 0x40, reg8);
76  mdelay(1);
77  reg8 = pci_read_config8(dev, 0x40);
78  printk(BIOS_DEBUG, "Azalia: codec type: %s\n", (reg8 & (1 << 1))?"Azalia":"AC97");
79 
80  // Select Azalia mode. This needs to be controlled via devicetree.cb
81  pci_or_config8(dev, 0x40, 1); // Audio Control
82 
83  // Docking not supported
84  pci_and_config8(dev, 0x4d, (u8)~(1 << 7)); // Docking Status
85 
87  if (!res)
88  return;
89 
90  // NOTE this will break as soon as the Azalia gets a bar above 4G.
91  // Is there anything we can do about it?
92  base = res2mmio(res, 0, 0);
93  printk(BIOS_DEBUG, "Azalia: base = %08x\n", (u32)(uintptr_t)base);
94  codec_mask = codec_detect(base);
95 
96  if (codec_mask) {
97  printk(BIOS_DEBUG, "Azalia: codec_mask = %02x\n", codec_mask);
98  azalia_codecs_init(base, codec_mask);
99  }
100 }
101 
102 static struct device_operations azalia_ops = {
104  .set_resources = pci_dev_set_resources,
105  .enable_resources = pci_dev_enable_resources,
106  .init = azalia_init,
107  .enable = i82801gx_enable,
108  .ops_pci = &pci_dev_ops_pci,
109 };
110 
111 /* 82801GB/GR/GDH/GBM/GHM (ICH7/ICH7R/ICH7DH/ICH7-M/ICH7-M DH) */
112 static const struct pci_driver i82801gx_azalia __pci_driver = {
113  .ops = &azalia_ops,
114  .vendor = PCI_VID_INTEL,
115  .device = 0x27d8,
116 };
static uint32_t read32(const void *addr)
Definition: mmio.h:22
int azalia_enter_reset(u8 *base)
Definition: azalia_device.c:38
void azalia_codecs_init(u8 *base, u16 codec_mask)
int azalia_exit_reset(u8 *base)
Definition: azalia_device.c:44
#define HDA_STATESTS_REG
Definition: azalia_device.h:14
#define printk(level,...)
Definition: stdlib.h:16
void mdelay(unsigned int msecs)
Definition: delay.c:2
struct resource * probe_resource(const struct device *dev, unsigned int index)
See if a resource structure already exists for a given index.
Definition: device_util.c:323
static int codec_detect(u8 *base)
Definition: azalia.c:15
static const struct pci_driver i82801gx_azalia __pci_driver
Definition: azalia.c:112
static void azalia_init(struct device *dev)
Definition: azalia.c:40
static struct device_operations azalia_ops
Definition: azalia.c:102
void i82801gx_enable(struct device *dev)
Definition: i82801gx.c:54
static __always_inline void pci_or_config32(const struct device *dev, u16 reg, u32 ormask)
Definition: pci_ops.h:191
static __always_inline void pci_and_config8(const struct device *dev, u16 reg, u8 andmask)
Definition: pci_ops.h:136
static __always_inline void pci_update_config32(const struct device *dev, u16 reg, u32 mask, u32 or)
Definition: pci_ops.h:120
static __always_inline void pci_or_config16(const struct device *dev, u16 reg, u16 ormask)
Definition: pci_ops.h:180
static __always_inline void pci_or_config8(const struct device *dev, u16 reg, u8 ormask)
Definition: pci_ops.h:169
static __always_inline u8 pci_read_config8(const struct device *dev, u16 reg)
Definition: pci_ops.h:46
static __always_inline void pci_write_config8(const struct device *dev, u16 reg, u8 val)
Definition: pci_ops.h:64
#define BIOS_DEBUG
BIOS_DEBUG - Verbose output.
Definition: loglevel.h:128
#define PCI_COMMAND_MASTER
Definition: pci_def.h:13
#define PCI_BASE_ADDRESS_0
Definition: pci_def.h:63
#define PCI_COMMAND
Definition: pci_def.h:10
void pci_dev_enable_resources(struct device *dev)
Definition: pci_device.c:721
void pci_dev_read_resources(struct device *dev)
Definition: pci_device.c:534
struct pci_operations pci_dev_ops_pci
Default device operation for PCI devices.
Definition: pci_device.c:911
void pci_dev_set_resources(struct device *dev)
Definition: pci_device.c:691
#define PCI_VID_INTEL
Definition: pci_ids.h:2157
static void * res2mmio(const struct resource *res, unsigned long offset, unsigned long mask)
Definition: resource.h:87
uintptr_t base
Definition: uart.c:17
uint32_t u32
Definition: stdint.h:51
unsigned long uintptr_t
Definition: stdint.h:21
uint8_t u8
Definition: stdint.h:45
void(* read_resources)(struct device *dev)
Definition: device.h:39
Definition: device.h:107