mb/system76/cml-u/dt: Make use of chipset devicetree
[coreboot.git] / src / soc / intel / common / hda_verb.c
blobdceb03183eb52336d15e759191ce62a6d5cff260
1 /* SPDX-License-Identifier: GPL-2.0-only */
3 #include <console/console.h>
4 #include <delay.h>
5 #include <device/azalia_device.h>
6 #include <device/mmio.h>
7 #include <stdint.h>
9 #include "hda_verb.h"
11 int hda_codec_detect(u8 *base)
13 u8 reg8;
15 /* Set Bit 0 to 1 to exit reset state (BAR + 0x8)[0] */
16 if (azalia_exit_reset(base) < 0)
17 goto no_codec;
19 /* Write back the value once reset bit is set. */
20 write16(base + HDA_GCAP_REG, read16(base + HDA_GCAP_REG));
23 * Clear the "State Change Status Register" STATESTS bits
24 * for each of the "SDIN Stat Change Status Flag"
26 write8(base + HDA_STATESTS_REG, 0xf);
28 /* Turn off the link and poll RESET# bit until it reads back as 0 */
29 if (azalia_enter_reset(base) < 0)
30 goto no_codec;
32 /* Turn on the link and poll RESET# bit until it reads back as 1 */
33 if (azalia_exit_reset(base) < 0)
34 goto no_codec;
36 /* Read in Codec location (BAR + 0xe)[2..0] */
37 reg8 = read8(base + HDA_STATESTS_REG);
38 reg8 &= 0x0f;
39 if (!reg8)
40 goto no_codec;
42 return reg8;
44 no_codec:
45 /* Codec not found, put HDA back in reset */
46 azalia_enter_reset(base);
47 printk(BIOS_DEBUG, "HDA: No codec!\n");
48 return 0;
52 * Wait 50usec for the codec to indicate it is ready.
53 * No response would imply that the codec is non-operative.
55 static int hda_wait_for_ready(u8 *base)
57 /* Use a 50 usec timeout - the Linux kernel uses the same duration */
58 int timeout = 50;
60 while (timeout--) {
61 u32 reg32 = read32(base + HDA_ICII_REG);
62 if (!(reg32 & HDA_ICII_BUSY))
63 return 0;
64 udelay(1);
67 return -1;
71 * Wait 50usec for the codec to indicate that it accepted the previous command.
72 * No response would imply that the code is non-operative.
74 static int hda_wait_for_valid(u8 *base)
76 u32 reg32;
77 /* Use a 50 usec timeout - the Linux kernel uses the same duration */
78 int timeout = 50;
80 /* Send the verb to the codec */
81 reg32 = read32(base + HDA_ICII_REG);
82 reg32 |= HDA_ICII_BUSY | HDA_ICII_VALID;
83 write32(base + HDA_ICII_REG, reg32);
85 while (timeout--) {
86 reg32 = read32(base + HDA_ICII_REG);
87 if ((reg32 & (HDA_ICII_VALID | HDA_ICII_BUSY)) == HDA_ICII_VALID)
88 return 0;
89 udelay(1);
92 return -1;
95 int hda_codec_write(u8 *base, u32 size, const u32 *data)
97 int i;
99 for (i = 0; i < size; i++) {
100 if (hda_wait_for_ready(base) < 0)
101 return -1;
103 write32(base + HDA_IC_REG, data[i]);
105 if (hda_wait_for_valid(base) < 0)
106 return -1;
109 return 0;
112 int hda_codec_init(u8 *base, int addr, int verb_size, const u32 *verb_data)
114 const u32 *verb;
115 u32 reg32, size;
116 int rc;
118 printk(BIOS_DEBUG, "HDA: Initializing codec #%d\n", addr);
120 if (!verb_size || !verb_data) {
121 printk(BIOS_DEBUG, "HDA: No verb list!\n");
122 return -1;
125 /* 1 */
126 if (hda_wait_for_ready(base) < 0) {
127 printk(BIOS_DEBUG, " codec not ready.\n");
128 return -1;
131 reg32 = (addr << 28) | 0x000f0000;
132 write32(base + HDA_IC_REG, reg32);
134 if (hda_wait_for_valid(base) < 0) {
135 printk(BIOS_DEBUG, " codec not valid.\n");
136 return -1;
139 /* 2 */
140 reg32 = read32(base + HDA_IR_REG);
141 printk(BIOS_DEBUG, "HDA: codec viddid: %08x\n", reg32);
143 size = azalia_find_verb(verb_data, verb_size, reg32, &verb);
144 if (!size) {
145 printk(BIOS_DEBUG, "HDA: No verb table entry found\n");
146 return -1;
149 /* 3 */
150 rc = hda_codec_write(base, size, verb);
152 if (rc < 0)
153 printk(BIOS_DEBUG, "HDA: verb not loaded\n");
154 else
155 printk(BIOS_DEBUG, "HDA: verb loaded.\n");
157 return rc;