Shawn Nematbakhsh | 51d787a | 2014-01-16 17:52:21 -0800 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
| 4 | * Copyright (C) 2008 Advanced Micro Devices, Inc. |
| 5 | * Copyright (C) 2008-2009 coresystems GmbH |
| 6 | * Copyright (C) 2011 Google Inc. |
| 7 | * |
| 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. |
| 11 | * |
| 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. |
Shawn Nematbakhsh | 51d787a | 2014-01-16 17:52:21 -0800 | [diff] [blame] | 16 | */ |
| 17 | |
| 18 | #include <console/console.h> |
| 19 | #include <arch/io.h> |
| 20 | #include <delay.h> |
Duncan Laurie | f0aaa29 | 2014-04-22 10:48:29 -0700 | [diff] [blame] | 21 | #include "hda_verb.h" |
Shawn Nematbakhsh | 51d787a | 2014-01-16 17:52:21 -0800 | [diff] [blame] | 22 | |
| 23 | /** |
| 24 | * Set bits in a register and wait for status |
| 25 | */ |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 26 | static int set_bits(void *port, u32 mask, u32 val) |
Shawn Nematbakhsh | 51d787a | 2014-01-16 17:52:21 -0800 | [diff] [blame] | 27 | { |
| 28 | u32 reg32; |
| 29 | int count; |
| 30 | |
| 31 | /* Write (val & mask) to port */ |
| 32 | val &= mask; |
| 33 | reg32 = read32(port); |
| 34 | reg32 &= ~mask; |
| 35 | reg32 |= val; |
| 36 | write32(port, reg32); |
| 37 | |
| 38 | /* Wait for readback of register to |
| 39 | * match what was just written to it |
| 40 | */ |
| 41 | count = 50; |
| 42 | do { |
| 43 | /* Wait 1ms based on BKDG wait time */ |
| 44 | mdelay(1); |
| 45 | reg32 = read32(port); |
| 46 | reg32 &= mask; |
| 47 | } while ((reg32 != val) && --count); |
| 48 | |
| 49 | /* Timeout occurred */ |
| 50 | if (!count) |
| 51 | return -1; |
| 52 | return 0; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Probe for supported codecs |
| 57 | */ |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 58 | int hda_codec_detect(u8 *base) |
Shawn Nematbakhsh | 51d787a | 2014-01-16 17:52:21 -0800 | [diff] [blame] | 59 | { |
| 60 | u8 reg8; |
| 61 | |
| 62 | /* Set Bit 0 to 1 to exit reset state (BAR + 0x8)[0] */ |
| 63 | if (set_bits(base + HDA_GCTL_REG, HDA_GCTL_CRST, HDA_GCTL_CRST) < 0) |
| 64 | goto no_codec; |
| 65 | |
| 66 | /* Write back the value once reset bit is set. */ |
| 67 | write16(base + HDA_GCAP_REG, read16(base + HDA_GCAP_REG)); |
| 68 | |
Kein Yuan | d3b40bf | 2014-02-11 17:40:31 -0800 | [diff] [blame] | 69 | /* Clear the "State Change Status Register" STATESTS bits |
| 70 | * for each of the "SDIN Stat Change Status Flag" |
Lee Leahy | 0946ec3 | 2015-04-20 15:24:54 -0700 | [diff] [blame] | 71 | */ |
Kein Yuan | d3b40bf | 2014-02-11 17:40:31 -0800 | [diff] [blame] | 72 | write8(base + HDA_STATESTS_REG, 0xf); |
| 73 | |
| 74 | /* Turn off the link and poll RESET# bit until it reads back as 0 */ |
| 75 | if (set_bits(base + HDA_GCTL_REG, HDA_GCTL_CRST, ~HDA_GCTL_CRST) < 0) |
| 76 | goto no_codec; |
| 77 | |
| 78 | /* Turn on the link and poll RESET# bit until it reads back as 1 */ |
| 79 | if (set_bits(base + HDA_GCTL_REG, HDA_GCTL_CRST, HDA_GCTL_CRST) < 0) |
| 80 | goto no_codec; |
| 81 | |
Shawn Nematbakhsh | 51d787a | 2014-01-16 17:52:21 -0800 | [diff] [blame] | 82 | /* Read in Codec location (BAR + 0xe)[2..0]*/ |
| 83 | reg8 = read8(base + HDA_STATESTS_REG); |
| 84 | reg8 &= 0x0f; |
| 85 | if (!reg8) |
| 86 | goto no_codec; |
| 87 | |
| 88 | return reg8; |
| 89 | |
| 90 | no_codec: |
| 91 | /* Codec Not found */ |
| 92 | /* Put HDA back in reset (BAR + 0x8) [0] */ |
| 93 | set_bits(base + HDA_GCTL_REG, HDA_GCTL_CRST, 0); |
| 94 | printk(BIOS_DEBUG, "HDA: No codec!\n"); |
| 95 | return 0; |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Wait 50usec for the codec to indicate it is ready |
| 100 | * no response would imply that the codec is non-operative |
| 101 | */ |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 102 | static int hda_wait_for_ready(u8 *base) |
Shawn Nematbakhsh | 51d787a | 2014-01-16 17:52:21 -0800 | [diff] [blame] | 103 | { |
| 104 | /* Use a 50 usec timeout - the Linux kernel uses the |
| 105 | * same duration */ |
| 106 | |
| 107 | int timeout = 50; |
| 108 | |
| 109 | while(timeout--) { |
| 110 | u32 reg32 = read32(base + HDA_ICII_REG); |
| 111 | if (!(reg32 & HDA_ICII_BUSY)) |
| 112 | return 0; |
| 113 | udelay(1); |
| 114 | } |
| 115 | |
| 116 | return -1; |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Wait 50usec for the codec to indicate that it accepted |
| 121 | * the previous command. No response would imply that the code |
| 122 | * is non-operative |
| 123 | */ |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 124 | static int hda_wait_for_valid(u8 *base) |
Shawn Nematbakhsh | 51d787a | 2014-01-16 17:52:21 -0800 | [diff] [blame] | 125 | { |
| 126 | u32 reg32; |
| 127 | |
| 128 | /* Send the verb to the codec */ |
| 129 | reg32 = read32(base + HDA_ICII_REG); |
| 130 | reg32 |= HDA_ICII_BUSY | HDA_ICII_VALID; |
| 131 | write32(base + HDA_ICII_REG, reg32); |
| 132 | |
| 133 | /* Use a 50 usec timeout - the Linux kernel uses the |
| 134 | * same duration */ |
| 135 | |
| 136 | int timeout = 50; |
| 137 | while(timeout--) { |
| 138 | reg32 = read32(base + HDA_ICII_REG); |
| 139 | if ((reg32 & (HDA_ICII_VALID | HDA_ICII_BUSY)) == |
| 140 | HDA_ICII_VALID) |
| 141 | return 0; |
| 142 | udelay(1); |
| 143 | } |
| 144 | |
| 145 | return -1; |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Find a specific entry within a verb table |
| 150 | * |
Martin Roth | 5f066b2 | 2015-01-04 16:47:39 -0700 | [diff] [blame] | 151 | * @param verb_table_bytes: verb table size in bytes |
| 152 | * @param verb_table_data: verb table data |
| 153 | * @param viddid: vendor/device to search for |
| 154 | * @param verb: pointer to entry within table |
Shawn Nematbakhsh | 51d787a | 2014-01-16 17:52:21 -0800 | [diff] [blame] | 155 | * |
| 156 | * Returns size of the entry within the verb table, |
| 157 | * Returns 0 if the entry is not found |
| 158 | * |
| 159 | * The HDA verb table is composed of dwords. A set of 4 dwords is |
| 160 | * grouped together to form a "jack" descriptor. |
| 161 | * Bits 31:28 - Codec Address |
| 162 | * Bits 27:20 - NID |
| 163 | * Bits 19:8 - Verb ID |
| 164 | * Bits 7:0 - Payload |
| 165 | * |
| 166 | * coreboot groups different codec verb tables into a single table |
| 167 | * and prefixes each with a specific header consisting of 3 |
| 168 | * dword entries: |
| 169 | * 1 - Codec Vendor/Device ID |
| 170 | * 2 - Subsystem ID |
| 171 | * 3 - Number of jacks (groups of 4 dwords) for this codec |
| 172 | */ |
| 173 | static u32 hda_find_verb(u32 verb_table_bytes, |
| 174 | const u32 *verb_table_data, |
| 175 | u32 viddid, const u32 ** verb) |
| 176 | { |
| 177 | int idx=0; |
| 178 | |
| 179 | while (idx < (verb_table_bytes / sizeof(u32))) { |
| 180 | u32 verb_size = 4 * verb_table_data[idx+2]; // in u32 |
| 181 | if (verb_table_data[idx] != viddid) { |
| 182 | idx += verb_size + 3; // skip verb + header |
| 183 | continue; |
| 184 | } |
| 185 | *verb = &verb_table_data[idx+3]; |
| 186 | return verb_size; |
| 187 | } |
| 188 | |
| 189 | /* Not all codecs need to load another verb */ |
| 190 | return 0; |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * Write a supplied verb table |
| 195 | */ |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 196 | int hda_codec_write(u8 *base, u32 size, const u32 *data) |
Shawn Nematbakhsh | 51d787a | 2014-01-16 17:52:21 -0800 | [diff] [blame] | 197 | { |
| 198 | int i; |
| 199 | |
| 200 | for (i = 0; i < size; i++) { |
| 201 | if (hda_wait_for_ready(base) < 0) |
| 202 | return -1; |
| 203 | |
| 204 | write32(base + HDA_IC_REG, data[i]); |
| 205 | |
| 206 | if (hda_wait_for_valid(base) < 0) |
| 207 | return -1; |
| 208 | } |
| 209 | |
| 210 | return 0; |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * Initialize codec, then find the verb table and write it |
| 215 | */ |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 216 | int hda_codec_init(u8 *base, int addr, int verb_size, const u32 *verb_data) |
Shawn Nematbakhsh | 51d787a | 2014-01-16 17:52:21 -0800 | [diff] [blame] | 217 | { |
| 218 | const u32 *verb; |
| 219 | u32 reg32, size; |
| 220 | int rc; |
| 221 | |
| 222 | printk(BIOS_DEBUG, "HDA: Initializing codec #%d\n", addr); |
| 223 | |
| 224 | if (!verb_size || !verb_data) { |
| 225 | printk(BIOS_DEBUG, "HDA: No verb list!\n"); |
| 226 | return -1; |
| 227 | } |
| 228 | |
| 229 | /* 1 */ |
| 230 | if (hda_wait_for_ready(base) < 0) { |
| 231 | printk(BIOS_DEBUG, " codec not ready.\n"); |
| 232 | return -1; |
| 233 | } |
| 234 | |
| 235 | reg32 = (addr << 28) | 0x000f0000; |
| 236 | write32(base + HDA_IC_REG, reg32); |
| 237 | |
| 238 | if (hda_wait_for_valid(base) < 0) { |
| 239 | printk(BIOS_DEBUG, " codec not valid.\n"); |
| 240 | return -1; |
| 241 | } |
| 242 | |
| 243 | /* 2 */ |
| 244 | reg32 = read32(base + HDA_IR_REG); |
| 245 | printk(BIOS_DEBUG, "HDA: codec viddid: %08x\n", reg32); |
| 246 | |
| 247 | size = hda_find_verb(verb_size, verb_data, reg32, &verb); |
| 248 | if (!size) { |
| 249 | printk(BIOS_DEBUG, "HDA: No verb table entry found\n"); |
| 250 | return -1; |
| 251 | } |
| 252 | |
| 253 | /* 3 */ |
| 254 | rc = hda_codec_write(base, size, verb); |
| 255 | |
| 256 | if (rc < 0) |
| 257 | printk(BIOS_DEBUG, "HDA: verb not loaded\n"); |
| 258 | else |
| 259 | printk(BIOS_DEBUG, "HDA: verb loaded.\n"); |
| 260 | |
| 261 | return rc; |
| 262 | } |