blob: dceb03183eb52336d15e759191ce62a6d5cff260 [file] [log] [blame]
Angel Pons0612b272020-04-05 15:46:56 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Shawn Nematbakhsh51d787a2014-01-16 17:52:21 -08002
3#include <console/console.h>
Elyes Haouas8ed58352022-10-22 22:17:28 +02004#include <delay.h>
Elyes HAOUAS899d5bde2020-08-03 15:00:46 +02005#include <device/azalia_device.h>
Kyösti Mälkki13f66502019-03-03 08:01:05 +02006#include <device/mmio.h>
Elyes Haouas8ed58352022-10-22 22:17:28 +02007#include <stdint.h>
Elyes HAOUAS899d5bde2020-08-03 15:00:46 +02008
Duncan Laurief0aaa292014-04-22 10:48:29 -07009#include "hda_verb.h"
Shawn Nematbakhsh51d787a2014-01-16 17:52:21 -080010
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080011int hda_codec_detect(u8 *base)
Shawn Nematbakhsh51d787a2014-01-16 17:52:21 -080012{
13 u8 reg8;
14
15 /* Set Bit 0 to 1 to exit reset state (BAR + 0x8)[0] */
Angel Pons7f839f62020-12-05 19:02:14 +010016 if (azalia_exit_reset(base) < 0)
Shawn Nematbakhsh51d787a2014-01-16 17:52:21 -080017 goto no_codec;
18
19 /* Write back the value once reset bit is set. */
20 write16(base + HDA_GCAP_REG, read16(base + HDA_GCAP_REG));
21
Angel Ponsaa566ad2021-03-18 17:08:09 +010022 /*
23 * Clear the "State Change Status Register" STATESTS bits
Kein Yuand3b40bf2014-02-11 17:40:31 -080024 * for each of the "SDIN Stat Change Status Flag"
Lee Leahy0946ec32015-04-20 15:24:54 -070025 */
Kein Yuand3b40bf2014-02-11 17:40:31 -080026 write8(base + HDA_STATESTS_REG, 0xf);
27
28 /* Turn off the link and poll RESET# bit until it reads back as 0 */
Angel Pons2e0053b2020-12-05 19:06:55 +010029 if (azalia_enter_reset(base) < 0)
Kein Yuand3b40bf2014-02-11 17:40:31 -080030 goto no_codec;
31
32 /* Turn on the link and poll RESET# bit until it reads back as 1 */
Angel Pons7f839f62020-12-05 19:02:14 +010033 if (azalia_exit_reset(base) < 0)
Kein Yuand3b40bf2014-02-11 17:40:31 -080034 goto no_codec;
35
Angel Ponsaa566ad2021-03-18 17:08:09 +010036 /* Read in Codec location (BAR + 0xe)[2..0] */
Shawn Nematbakhsh51d787a2014-01-16 17:52:21 -080037 reg8 = read8(base + HDA_STATESTS_REG);
38 reg8 &= 0x0f;
39 if (!reg8)
40 goto no_codec;
41
42 return reg8;
43
44no_codec:
Angel Pons2e0053b2020-12-05 19:06:55 +010045 /* Codec not found, put HDA back in reset */
46 azalia_enter_reset(base);
Shawn Nematbakhsh51d787a2014-01-16 17:52:21 -080047 printk(BIOS_DEBUG, "HDA: No codec!\n");
48 return 0;
49}
50
Subrata Banik5885ffe2019-11-14 11:08:51 +053051/*
Elyes HAOUASa3022052020-08-11 16:47:47 +020052 * Wait 50usec for the codec to indicate it is ready.
53 * No response would imply that the codec is non-operative.
Shawn Nematbakhsh51d787a2014-01-16 17:52:21 -080054 */
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080055static int hda_wait_for_ready(u8 *base)
Shawn Nematbakhsh51d787a2014-01-16 17:52:21 -080056{
Elyes HAOUASa3022052020-08-11 16:47:47 +020057 /* Use a 50 usec timeout - the Linux kernel uses the same duration */
Shawn Nematbakhsh51d787a2014-01-16 17:52:21 -080058 int timeout = 50;
59
Elyes HAOUAS4a83f1c2016-08-25 21:07:59 +020060 while (timeout--) {
Elyes HAOUASa3022052020-08-11 16:47:47 +020061 u32 reg32 = read32(base + HDA_ICII_REG);
Shawn Nematbakhsh51d787a2014-01-16 17:52:21 -080062 if (!(reg32 & HDA_ICII_BUSY))
63 return 0;
64 udelay(1);
65 }
66
67 return -1;
68}
69
Subrata Banik5885ffe2019-11-14 11:08:51 +053070/*
Elyes HAOUASa3022052020-08-11 16:47:47 +020071 * Wait 50usec for the codec to indicate that it accepted the previous command.
72 * No response would imply that the code is non-operative.
Shawn Nematbakhsh51d787a2014-01-16 17:52:21 -080073 */
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080074static int hda_wait_for_valid(u8 *base)
Shawn Nematbakhsh51d787a2014-01-16 17:52:21 -080075{
76 u32 reg32;
Elyes HAOUASa3022052020-08-11 16:47:47 +020077 /* Use a 50 usec timeout - the Linux kernel uses the same duration */
78 int timeout = 50;
Shawn Nematbakhsh51d787a2014-01-16 17:52:21 -080079
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);
84
Elyes HAOUAS4a83f1c2016-08-25 21:07:59 +020085 while (timeout--) {
Shawn Nematbakhsh51d787a2014-01-16 17:52:21 -080086 reg32 = read32(base + HDA_ICII_REG);
Elyes HAOUASa3022052020-08-11 16:47:47 +020087 if ((reg32 & (HDA_ICII_VALID | HDA_ICII_BUSY)) == HDA_ICII_VALID)
Shawn Nematbakhsh51d787a2014-01-16 17:52:21 -080088 return 0;
89 udelay(1);
90 }
91
92 return -1;
93}
94
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080095int hda_codec_write(u8 *base, u32 size, const u32 *data)
Shawn Nematbakhsh51d787a2014-01-16 17:52:21 -080096{
97 int i;
98
99 for (i = 0; i < size; i++) {
100 if (hda_wait_for_ready(base) < 0)
101 return -1;
102
103 write32(base + HDA_IC_REG, data[i]);
104
105 if (hda_wait_for_valid(base) < 0)
106 return -1;
107 }
108
109 return 0;
110}
111
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800112int hda_codec_init(u8 *base, int addr, int verb_size, const u32 *verb_data)
Shawn Nematbakhsh51d787a2014-01-16 17:52:21 -0800113{
114 const u32 *verb;
115 u32 reg32, size;
116 int rc;
117
118 printk(BIOS_DEBUG, "HDA: Initializing codec #%d\n", addr);
119
120 if (!verb_size || !verb_data) {
121 printk(BIOS_DEBUG, "HDA: No verb list!\n");
122 return -1;
123 }
124
125 /* 1 */
126 if (hda_wait_for_ready(base) < 0) {
127 printk(BIOS_DEBUG, " codec not ready.\n");
128 return -1;
129 }
130
131 reg32 = (addr << 28) | 0x000f0000;
132 write32(base + HDA_IC_REG, reg32);
133
134 if (hda_wait_for_valid(base) < 0) {
135 printk(BIOS_DEBUG, " codec not valid.\n");
136 return -1;
137 }
138
139 /* 2 */
140 reg32 = read32(base + HDA_IR_REG);
141 printk(BIOS_DEBUG, "HDA: codec viddid: %08x\n", reg32);
142
Angel Ponsd3f70282020-12-05 18:28:33 +0100143 size = azalia_find_verb(verb_data, verb_size, reg32, &verb);
Shawn Nematbakhsh51d787a2014-01-16 17:52:21 -0800144 if (!size) {
145 printk(BIOS_DEBUG, "HDA: No verb table entry found\n");
146 return -1;
147 }
148
149 /* 3 */
150 rc = hda_codec_write(base, size, verb);
151
152 if (rc < 0)
153 printk(BIOS_DEBUG, "HDA: verb not loaded\n");
154 else
155 printk(BIOS_DEBUG, "HDA: verb loaded.\n");
156
157 return rc;
158}