coreboot
coreboot is an Open Source project aimed at replacing the proprietary BIOS found in most computers.
hda.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/azalia_device.h>
6 #include <device/pci.h>
7 #include <device/pci_ids.h>
8 #include <device/pci_ops.h>
9 #include <device/mmio.h>
10 #include <soc/pch.h>
11 #include <soc/rcba.h>
13 
14 static void hda_pch_init(struct device *dev, u8 *base)
15 {
16  u8 reg8;
17  u16 reg16;
18  u32 reg32;
19 
20  if (RCBA32(0x2030) & (1 << 31)) {
21  reg32 = pci_read_config32(dev, 0x120);
22  reg32 &= 0xf8ffff01;
23  reg32 |= (1 << 25);
24  reg32 |= RCBA32(0x2030) & 0xfe;
25  pci_write_config32(dev, 0x120, reg32);
26  } else
27  printk(BIOS_DEBUG, "HDA: V1CTL disabled.\n");
28 
29  reg32 = pci_read_config32(dev, 0x114);
30  reg32 &= ~0xfe;
31  pci_write_config32(dev, 0x114, reg32);
32 
33  // Set VCi enable bit
34  if (pci_read_config32(dev, 0x120) & ((1 << 24) |
35  (1 << 25) | (1 << 26))) {
36  reg32 = pci_read_config32(dev, 0x120);
37  reg32 &= ~(1 << 31);
38  pci_write_config32(dev, 0x120, reg32);
39  }
40 
41  /* Additional programming steps */
42  reg32 = pci_read_config32(dev, 0xc4);
43  reg32 |= (1 << 24);
44  pci_write_config32(dev, 0xc4, reg32);
45 
46  reg8 = pci_read_config8(dev, 0x4d); // Docking Status
47  reg8 &= ~(1 << 7); // Docking not supported
48  pci_write_config8(dev, 0x4d, reg8);
49 
50  reg16 = read32(base + 0x0012);
51  reg16 |= (1 << 0);
52  write32(base + 0x0012, reg16);
53 
54  /* disable Auto Voltage Detector */
55  reg8 = pci_read_config8(dev, 0x42);
56  reg8 |= (1 << 2);
57  pci_write_config8(dev, 0x42, reg8);
58 }
59 
60 static void hda_init(struct device *dev)
61 {
62  u8 *base;
63  struct resource *res;
64  u32 codec_mask;
65 
66  /* Find base address */
68  if (!res)
69  return;
70 
71  base = res2mmio(res, 0, 0);
72  printk(BIOS_DEBUG, "HDA: base = %p\n", base);
73 
74  /* Set Bus Master */
76 
77  hda_pch_init(dev, base);
78 
79  codec_mask = hda_codec_detect(base);
80 
81  if (codec_mask) {
82  printk(BIOS_DEBUG, "HDA: codec_mask = %02x\n", codec_mask);
83  azalia_codecs_init(base, codec_mask);
84  }
85 }
86 
87 static void hda_enable(struct device *dev)
88 {
89  u16 reg16;
90  u8 reg8;
91 
92  reg8 = pci_read_config8(dev, 0x43);
93  reg8 |= 0x6f;
94  pci_write_config8(dev, 0x43, reg8);
95 
96  if (!dev->enabled) {
97  /* Route I/O buffers to ADSP function */
98  reg8 = pci_read_config8(dev, 0x42);
99  reg8 |= (1 << 7) | (1 << 6);
100  pci_write_config8(dev, 0x42, reg8);
101 
102  printk(BIOS_INFO, "HDA disabled, I/O buffers routed to ADSP\n");
103 
104  /* Ensure memory, io, and bus master are all disabled */
105  reg16 = pci_read_config16(dev, PCI_COMMAND);
106  reg16 &= ~(PCI_COMMAND_MASTER |
108  pci_write_config16(dev, PCI_COMMAND, reg16);
109 
110  /* Disable this device */
111  pch_disable_devfn(dev);
112  }
113 }
114 
115 static void hda_final(struct device *dev)
116 {
117  /* Set HDCFG.BCLD */
118  pci_or_config16(dev, 0x40, 1 << 1);
119 }
120 
121 static struct device_operations hda_ops = {
123  .set_resources = &pci_dev_set_resources,
124  .enable_resources = &pci_dev_enable_resources,
125  .init = &hda_init,
126  .enable = &hda_enable,
127  .final = &hda_final,
128  .ops_pci = &pci_dev_ops_pci,
129 };
130 
131 static const unsigned short pci_device_ids[] = {
132  0x9c20, /* LynxPoint-LP */
133  0x9ca0, /* WildcatPoint */
134  0
135 };
136 
137 static const struct pci_driver pch_hda __pci_driver = {
138  .ops = &hda_ops,
139  .vendor = PCI_VID_INTEL,
140  .devices = pci_device_ids,
141 };
static void write32(void *addr, uint32_t val)
Definition: mmio.h:40
static uint32_t read32(const void *addr)
Definition: mmio.h:22
void azalia_codecs_init(u8 *base, u16 codec_mask)
#define printk(level,...)
Definition: stdlib.h:16
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 __always_inline void pci_write_config32(const struct device *dev, u16 reg, u32 val)
Definition: pci_ops.h:76
static __always_inline void pci_or_config16(const struct device *dev, u16 reg, u16 ormask)
Definition: pci_ops.h:180
static __always_inline u16 pci_read_config16(const struct device *dev, u16 reg)
Definition: pci_ops.h:52
static __always_inline u32 pci_read_config32(const struct device *dev, u16 reg)
Definition: pci_ops.h:58
static __always_inline u8 pci_read_config8(const struct device *dev, u16 reg)
Definition: pci_ops.h:46
static __always_inline void pci_write_config16(const struct device *dev, u16 reg, u16 val)
Definition: pci_ops.h:70
static __always_inline void pci_write_config8(const struct device *dev, u16 reg, u8 val)
Definition: pci_ops.h:64
#define BIOS_INFO
BIOS_INFO - Expected events.
Definition: loglevel.h:113
#define BIOS_DEBUG
BIOS_DEBUG - Verbose output.
Definition: loglevel.h:128
#define PCI_COMMAND_IO
Definition: pci_def.h:11
#define PCI_COMMAND_MASTER
Definition: pci_def.h:13
#define PCI_COMMAND_MEMORY
Definition: pci_def.h:12
#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
void pch_disable_devfn(struct device *dev)
Definition: pch.c:77
static void hda_init(struct device *dev)
Definition: hda.c:60
static void hda_final(struct device *dev)
Definition: hda.c:115
static struct device_operations hda_ops
Definition: hda.c:121
static void hda_enable(struct device *dev)
Definition: hda.c:87
static void hda_pch_init(struct device *dev, u8 *base)
Definition: hda.c:14
static const unsigned short pci_device_ids[]
Definition: hda.c:131
static const struct pci_driver pch_hda __pci_driver
Definition: hda.c:137
int hda_codec_detect(u8 *base)
Definition: hda_verb.c:10
#define RCBA32(x)
Definition: rcba.h:14
uint32_t u32
Definition: stdint.h:51
uint16_t u16
Definition: stdint.h:48
uint8_t u8
Definition: stdint.h:45
void(* read_resources)(struct device *dev)
Definition: device.h:39
Definition: device.h:107
unsigned int enabled
Definition: device.h:122