2 * This file is part of the coreboot project.
4 * Copyright (C) 2008 Advanced Micro Devices, Inc.
5 * Copyright (C) 2008-2009 coresystems GmbH
6 * Copyright (C) 2011 Google Inc.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; version 2 of the License.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
18 #include <console/console.h>
25 * Set bits in a register and wait for status
27 static int set_bits(void *port
, u32 mask
, u32 val
)
32 /* Write (val & mask) to port */
39 /* Wait for readback of register to
40 * match what was just written to it
44 /* Wait 1ms based on BKDG wait time */
48 } while ((reg32
!= val
) && --count
);
50 /* Timeout occurred */
57 * Probe for supported codecs
59 int hda_codec_detect(u8
*base
)
63 /* Set Bit 0 to 1 to exit reset state (BAR + 0x8)[0] */
64 if (set_bits(base
+ HDA_GCTL_REG
, HDA_GCTL_CRST
, HDA_GCTL_CRST
) < 0)
67 /* Write back the value once reset bit is set. */
68 write16(base
+ HDA_GCAP_REG
, read16(base
+ HDA_GCAP_REG
));
70 /* Read in Codec location (BAR + 0xe)[2..0]*/
71 reg8
= read8(base
+ HDA_STATESTS_REG
);
80 /* Put HDA back in reset (BAR + 0x8) [0] */
81 set_bits(base
+ HDA_GCTL_REG
, HDA_GCTL_CRST
, 0);
82 printk(BIOS_DEBUG
, "HDA: No codec!\n");
87 * Wait 50usec for the codec to indicate it is ready
88 * no response would imply that the codec is non-operative
90 static int hda_wait_for_ready(u8
*base
)
92 /* Use a 50 usec timeout - the Linux kernel uses the
98 u32 reg32
= read32(base
+ HDA_ICII_REG
);
99 if (!(reg32
& HDA_ICII_BUSY
))
108 * Wait 50usec for the codec to indicate that it accepted
109 * the previous command. No response would imply that the code
112 static int hda_wait_for_valid(u8
*base
)
116 /* Send the verb to the codec */
117 reg32
= read32(base
+ HDA_ICII_REG
);
118 reg32
|= HDA_ICII_BUSY
| HDA_ICII_VALID
;
119 write32(base
+ HDA_ICII_REG
, reg32
);
121 /* Use a 50 usec timeout - the Linux kernel uses the
126 reg32
= read32(base
+ HDA_ICII_REG
);
127 if ((reg32
& (HDA_ICII_VALID
| HDA_ICII_BUSY
)) ==
137 * Find a specific entry within a verb table
139 * @param verb_table_bytes: verb table size in bytes
140 * @param verb_table_data: verb table data
141 * @param viddid: vendor/device to search for
142 * @param **verb: pointer to entry within table
144 * Returns size of the entry within the verb table,
145 * Returns 0 if the entry is not found
147 * The HDA verb table is composed of dwords. A set of 4 dwords is
148 * grouped together to form a "jack" descriptor.
149 * Bits 31:28 - Codec Address
151 * Bits 19:8 - Verb ID
154 * coreboot groups different codec verb tables into a single table
155 * and prefixes each with a specific header consisting of 3
157 * 1 - Codec Vendor/Device ID
159 * 3 - Number of jacks (groups of 4 dwords) for this codec
161 static u32
hda_find_verb(u32 verb_table_bytes
,
162 const u32
*verb_table_data
,
163 u32 viddid
, const u32
**verb
)
167 while (idx
< (verb_table_bytes
/ sizeof(u32
))) {
168 u32 verb_size
= 4 * verb_table_data
[idx
+2]; // in u32
169 if (verb_table_data
[idx
] != viddid
) {
170 idx
+= verb_size
+ 3; // skip verb + header
173 *verb
= &verb_table_data
[idx
+3];
177 /* Not all codecs need to load another verb */
182 * Write a supplied verb table
184 int hda_codec_write(u8
*base
, u32 size
, const u32
*data
)
188 for (i
= 0; i
< size
; i
++) {
189 if (hda_wait_for_ready(base
) < 0)
192 write32(base
+ HDA_IC_REG
, data
[i
]);
194 if (hda_wait_for_valid(base
) < 0)
202 * Initialize codec, then find the verb table and write it
204 int hda_codec_init(u8
*base
, int addr
, int verb_size
, const u32
*verb_data
)
210 printk(BIOS_DEBUG
, "HDA: Initializing codec #%d\n", addr
);
212 if (!verb_size
|| !verb_data
) {
213 printk(BIOS_DEBUG
, "HDA: No verb list!\n");
218 if (hda_wait_for_ready(base
) < 0) {
219 printk(BIOS_DEBUG
, " codec not ready.\n");
223 reg32
= (addr
<< 28) | 0x000f0000;
224 write32(base
+ HDA_IC_REG
, reg32
);
226 if (hda_wait_for_valid(base
) < 0) {
227 printk(BIOS_DEBUG
, " codec not valid.\n");
232 reg32
= read32(base
+ HDA_IR_REG
);
233 printk(BIOS_DEBUG
, "HDA: codec viddid: %08x\n", reg32
);
235 size
= hda_find_verb(verb_size
, verb_data
, reg32
, &verb
);
237 printk(BIOS_DEBUG
, "HDA: No verb table entry found\n");
242 rc
= hda_codec_write(base
, size
, verb
);
245 printk(BIOS_DEBUG
, "HDA: verb not loaded\n");
247 printk(BIOS_DEBUG
, "HDA: verb loaded.\n");