1 /* SPDX-License-Identifier: GPL-2.0-only */
3 #include <console/console.h>
5 #include <device/azalia_device.h>
6 #include <device/mmio.h>
11 int hda_codec_detect(u8
*base
)
15 /* Set Bit 0 to 1 to exit reset state (BAR + 0x8)[0] */
16 if (azalia_exit_reset(base
) < 0)
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)
32 /* Turn on the link and poll RESET# bit until it reads back as 1 */
33 if (azalia_exit_reset(base
) < 0)
36 /* Read in Codec location (BAR + 0xe)[2..0] */
37 reg8
= read8(base
+ HDA_STATESTS_REG
);
45 /* Codec not found, put HDA back in reset */
46 azalia_enter_reset(base
);
47 printk(BIOS_DEBUG
, "HDA: No codec!\n");
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 */
61 u32 reg32
= read32(base
+ HDA_ICII_REG
);
62 if (!(reg32
& HDA_ICII_BUSY
))
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
)
77 /* Use a 50 usec timeout - the Linux kernel uses the same duration */
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
);
86 reg32
= read32(base
+ HDA_ICII_REG
);
87 if ((reg32
& (HDA_ICII_VALID
| HDA_ICII_BUSY
)) == HDA_ICII_VALID
)
95 int hda_codec_write(u8
*base
, u32 size
, const u32
*data
)
99 for (i
= 0; i
< size
; i
++) {
100 if (hda_wait_for_ready(base
) < 0)
103 write32(base
+ HDA_IC_REG
, data
[i
]);
105 if (hda_wait_for_valid(base
) < 0)
112 int hda_codec_init(u8
*base
, int addr
, int verb_size
, const u32
*verb_data
)
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");
126 if (hda_wait_for_ready(base
) < 0) {
127 printk(BIOS_DEBUG
, " codec not ready.\n");
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");
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
);
145 printk(BIOS_DEBUG
, "HDA: No verb table entry found\n");
150 rc
= hda_codec_write(base
, size
, verb
);
153 printk(BIOS_DEBUG
, "HDA: verb not loaded\n");
155 printk(BIOS_DEBUG
, "HDA: verb loaded.\n");