Angel Pons | c74dae9 | 2020-04-02 23:48:16 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
| 2 | /* This file is part of the coreboot project. */ |
Andrew Wu | b7bb70d | 2013-08-12 20:07:47 +0800 | [diff] [blame] | 3 | |
| 4 | #include <console/console.h> |
| 5 | #include <device/device.h> |
| 6 | #include <device/pci.h> |
Andrew Wu | b7bb70d | 2013-08-12 20:07:47 +0800 | [diff] [blame] | 7 | #include <device/azalia_device.h> |
Kyösti Mälkki | 13f6650 | 2019-03-03 08:01:05 +0200 | [diff] [blame] | 8 | #include <device/mmio.h> |
Andrew Wu | b7bb70d | 2013-08-12 20:07:47 +0800 | [diff] [blame] | 9 | #include <delay.h> |
| 10 | |
| 11 | #define HDA_ICII_REG 0x68 |
| 12 | #define HDA_ICII_BUSY (1 << 0) |
| 13 | #define HDA_ICII_VALID (1 << 1) |
| 14 | |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 15 | static int set_bits(void *port, u32 mask, u32 val) |
Andrew Wu | b7bb70d | 2013-08-12 20:07:47 +0800 | [diff] [blame] | 16 | { |
| 17 | u32 reg32; |
| 18 | int count; |
| 19 | |
| 20 | /* Write (val & mask) to port */ |
| 21 | val &= mask; |
| 22 | reg32 = read32(port); |
| 23 | reg32 &= ~mask; |
| 24 | reg32 |= val; |
| 25 | write32(port, reg32); |
| 26 | |
| 27 | /* Wait for readback of register to |
| 28 | * match what was just written to it |
| 29 | */ |
| 30 | count = 50; |
| 31 | do { |
| 32 | /* Wait 1ms based on BKDG wait time */ |
| 33 | mdelay(1); |
| 34 | reg32 = read32(port); |
| 35 | reg32 &= mask; |
| 36 | } while ((reg32 != val) && --count); |
| 37 | |
| 38 | /* Timeout occurred */ |
| 39 | if (!count) |
| 40 | return -1; |
| 41 | return 0; |
| 42 | } |
| 43 | |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 44 | static int codec_detect(u8 *base) |
Andrew Wu | b7bb70d | 2013-08-12 20:07:47 +0800 | [diff] [blame] | 45 | { |
| 46 | u32 reg32; |
| 47 | int count; |
| 48 | |
| 49 | /* Set Bit 0 to 1 to exit reset state (BAR + 0x8)[0] */ |
| 50 | if (set_bits(base + 0x08, 1, 1) == -1) |
| 51 | goto no_codec; |
| 52 | |
| 53 | /* clear STATESTS bits (BAR + 0xE)[2:0] */ |
| 54 | reg32 = read32(base + 0x0E); |
| 55 | reg32 |= 7; |
| 56 | write32(base + 0x0E, reg32); |
| 57 | |
| 58 | /* Wait for readback of register to |
| 59 | * match what was just written to it |
| 60 | */ |
| 61 | count = 50; |
| 62 | do { |
| 63 | /* Wait 1ms based on BKDG wait time */ |
| 64 | mdelay(1); |
| 65 | reg32 = read32(base + 0x0E); |
| 66 | } while ((reg32 != 0) && --count); |
Martin Roth | 2ed0aa2 | 2016-01-05 20:58:58 -0700 | [diff] [blame] | 67 | /* Timeout occurred */ |
Andrew Wu | b7bb70d | 2013-08-12 20:07:47 +0800 | [diff] [blame] | 68 | if (!count) |
| 69 | goto no_codec; |
| 70 | |
| 71 | /* Set Bit0 to 0 to enter reset state (BAR + 0x8)[0] */ |
| 72 | if (set_bits(base + 0x08, 1, 0) == -1) |
| 73 | goto no_codec; |
| 74 | |
| 75 | /* Set Bit 0 to 1 to exit reset state (BAR + 0x8)[0] */ |
| 76 | if (set_bits(base + 0x08, 1, 1) == -1) |
| 77 | goto no_codec; |
| 78 | |
| 79 | /* Read in Codec location (BAR + 0xe)[2..0] */ |
| 80 | reg32 = read32(base + 0xe); |
| 81 | reg32 &= 0x0f; |
| 82 | if (!reg32) |
| 83 | goto no_codec; |
| 84 | |
| 85 | return reg32; |
| 86 | |
| 87 | no_codec: |
| 88 | /* Codec Not found */ |
| 89 | /* Put HDA back in reset (BAR + 0x8) [0] */ |
| 90 | set_bits(base + 0x08, 1, 0); |
| 91 | printk(BIOS_DEBUG, "azalia_audio: No codec!\n"); |
| 92 | return 0; |
| 93 | } |
| 94 | |
Elyes HAOUAS | e414a4e | 2019-01-03 10:40:43 +0100 | [diff] [blame] | 95 | static u32 find_verb(struct device *dev, u32 viddid, const u32 **verb) |
Andrew Wu | b7bb70d | 2013-08-12 20:07:47 +0800 | [diff] [blame] | 96 | { |
| 97 | printk(BIOS_DEBUG, "azalia_audio: dev=%s\n", dev_path(dev)); |
| 98 | printk(BIOS_DEBUG, "azalia_audio: Reading viddid=%x\n", viddid); |
| 99 | |
| 100 | int idx = 0; |
| 101 | |
| 102 | while (idx < (cim_verb_data_size / sizeof(u32))) { |
| 103 | u32 verb_size = 4 * cim_verb_data[idx + 2]; // in u32 |
| 104 | if (cim_verb_data[idx] != viddid) { |
| 105 | idx += verb_size + 3; // skip verb + header |
| 106 | continue; |
| 107 | } |
| 108 | *verb = &cim_verb_data[idx + 3]; |
| 109 | return verb_size; |
| 110 | } |
| 111 | |
| 112 | /* Not all codecs need to load another verb */ |
| 113 | return 0; |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Wait 50usec for the codec to indicate it is ready |
| 118 | * no response would imply that the codec is non-operative |
| 119 | */ |
| 120 | |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 121 | static int wait_for_ready(u8 *base) |
Andrew Wu | b7bb70d | 2013-08-12 20:07:47 +0800 | [diff] [blame] | 122 | { |
| 123 | /* Use a 50 usec timeout - the Linux kernel uses the |
| 124 | * same duration */ |
| 125 | |
| 126 | int timeout = 50; |
| 127 | |
| 128 | while (timeout--) { |
| 129 | u32 reg32 = read32(base + HDA_ICII_REG); |
| 130 | if (!(reg32 & HDA_ICII_BUSY)) |
| 131 | return 0; |
| 132 | udelay(1); |
| 133 | } |
| 134 | |
| 135 | return -1; |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Wait 50usec for the codec to indicate that it accepted |
| 140 | * the previous command. No response would imply that the code |
| 141 | * is non-operative |
| 142 | */ |
| 143 | |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 144 | static int wait_for_valid(u8 *base) |
Andrew Wu | b7bb70d | 2013-08-12 20:07:47 +0800 | [diff] [blame] | 145 | { |
| 146 | /* Use a 50 usec timeout - the Linux kernel uses the |
| 147 | * same duration */ |
| 148 | |
| 149 | int timeout = 25; |
| 150 | |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 151 | write32(base + HDA_ICII_REG, |
| 152 | HDA_ICII_VALID | HDA_ICII_BUSY); |
Andrew Wu | b7bb70d | 2013-08-12 20:07:47 +0800 | [diff] [blame] | 153 | while (timeout--) { |
| 154 | udelay(1); |
| 155 | } |
| 156 | timeout = 50; |
| 157 | while (timeout--) { |
| 158 | u32 reg32 = read32(base + HDA_ICII_REG); |
| 159 | if ((reg32 & (HDA_ICII_VALID | HDA_ICII_BUSY)) == |
| 160 | HDA_ICII_VALID) |
| 161 | return 0; |
| 162 | udelay(1); |
| 163 | } |
| 164 | |
| 165 | return -1; |
| 166 | } |
| 167 | |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 168 | static void codec_init(struct device *dev, u8 *base, int addr) |
Andrew Wu | b7bb70d | 2013-08-12 20:07:47 +0800 | [diff] [blame] | 169 | { |
| 170 | u32 reg32; |
| 171 | const u32 *verb; |
| 172 | u32 verb_size; |
| 173 | int i; |
| 174 | |
| 175 | printk(BIOS_DEBUG, "azalia_audio: Initializing codec #%d\n", addr); |
| 176 | |
| 177 | /* 1 */ |
| 178 | if (wait_for_ready(base) == -1) |
| 179 | return; |
| 180 | |
| 181 | reg32 = (addr << 28) | 0x000f0000; |
| 182 | write32(base + 0x60, reg32); |
| 183 | |
| 184 | if (wait_for_valid(base) == -1) |
| 185 | return; |
| 186 | |
| 187 | reg32 = read32(base + 0x64); |
| 188 | |
| 189 | /* 2 */ |
| 190 | printk(BIOS_DEBUG, "azalia_audio: codec viddid: %08x\n", reg32); |
| 191 | verb_size = find_verb(dev, reg32, &verb); |
| 192 | |
| 193 | if (!verb_size) { |
| 194 | printk(BIOS_DEBUG, "azalia_audio: No verb!\n"); |
| 195 | return; |
| 196 | } |
| 197 | printk(BIOS_DEBUG, "azalia_audio: verb_size: %d\n", verb_size); |
| 198 | |
| 199 | /* 3 */ |
| 200 | for (i = 0; i < verb_size; i++) { |
| 201 | if (wait_for_ready(base) == -1) |
| 202 | return; |
| 203 | |
| 204 | write32(base + 0x60, verb[i]); |
| 205 | |
| 206 | if (wait_for_valid(base) == -1) |
| 207 | return; |
| 208 | } |
| 209 | printk(BIOS_DEBUG, "azalia_audio: verb loaded.\n"); |
| 210 | } |
| 211 | |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 212 | static void codecs_init(struct device *dev, u8 *base, u32 codec_mask) |
Andrew Wu | b7bb70d | 2013-08-12 20:07:47 +0800 | [diff] [blame] | 213 | { |
| 214 | int i; |
| 215 | |
| 216 | for (i = 2; i >= 0; i--) { |
| 217 | if (codec_mask & (1 << i)) |
| 218 | codec_init(dev, base, i); |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | void azalia_audio_init(struct device *dev) |
| 223 | { |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 224 | u8 *base; |
Andrew Wu | b7bb70d | 2013-08-12 20:07:47 +0800 | [diff] [blame] | 225 | struct resource *res; |
| 226 | u32 codec_mask; |
| 227 | |
| 228 | res = find_resource(dev, 0x10); |
| 229 | if (!res) |
| 230 | return; |
| 231 | |
| 232 | // NOTE this will break as soon as the azalia_audio get's a bar above |
| 233 | // 4G. Is there anything we can do about it? |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 234 | base = res2mmio(res, 0, 0); |
| 235 | printk(BIOS_DEBUG, "azalia_audio: base = %p\n", base); |
Andrew Wu | b7bb70d | 2013-08-12 20:07:47 +0800 | [diff] [blame] | 236 | codec_mask = codec_detect(base); |
| 237 | |
| 238 | if (codec_mask) { |
| 239 | printk(BIOS_DEBUG, "azalia_audio: codec_mask = %02x\n", |
| 240 | codec_mask); |
| 241 | codecs_init(dev, base, codec_mask); |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | struct pci_operations azalia_audio_pci_ops = { |
| 246 | .set_subsystem = pci_dev_set_subsystem, |
| 247 | }; |
| 248 | |
| 249 | struct device_operations default_azalia_audio_ops = { |
| 250 | .read_resources = pci_dev_read_resources, |
| 251 | .set_resources = pci_dev_set_resources, |
| 252 | .enable_resources = pci_dev_enable_resources, |
| 253 | .init = azalia_audio_init, |
Andrew Wu | b7bb70d | 2013-08-12 20:07:47 +0800 | [diff] [blame] | 254 | .ops_pci = &azalia_audio_pci_ops, |
| 255 | }; |