Angel Pons | 182dbde | 2020-04-02 23:49:05 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Patrick Georgi | e72a8a3 | 2012-11-06 11:05:09 +0100 | [diff] [blame] | 2 | |
| 3 | #include <console/console.h> |
| 4 | #include <device/device.h> |
| 5 | #include <device/pci.h> |
| 6 | #include <device/pci_ids.h> |
| 7 | #include <device/pci_ops.h> |
Kyösti Mälkki | 13f6650 | 2019-03-03 08:01:05 +0200 | [diff] [blame] | 8 | #include <device/mmio.h> |
Patrick Georgi | e72a8a3 | 2012-11-06 11:05:09 +0100 | [diff] [blame] | 9 | #include <delay.h> |
Vladimir Serbinenko | 75c8387 | 2014-09-05 01:01:31 +0200 | [diff] [blame] | 10 | #include <device/azalia_device.h> |
Kyösti Mälkki | 12b121c | 2019-08-18 16:33:39 +0300 | [diff] [blame] | 11 | #include "chip.h" |
Patrick Georgi | e72a8a3 | 2012-11-06 11:05:09 +0100 | [diff] [blame] | 12 | #include "i82801ix.h" |
| 13 | |
| 14 | #define HDA_ICII_REG 0x68 |
Andrew Wu | ae8d069 | 2013-08-02 19:29:17 +0800 | [diff] [blame] | 15 | #define HDA_ICII_BUSY (1 << 0) |
| 16 | #define HDA_ICII_VALID (1 << 1) |
Patrick Georgi | e72a8a3 | 2012-11-06 11:05:09 +0100 | [diff] [blame] | 17 | |
| 18 | typedef struct southbridge_intel_i82801ix_config config_t; |
| 19 | |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 20 | static int set_bits(void *port, u32 mask, u32 val) |
Patrick Georgi | e72a8a3 | 2012-11-06 11:05:09 +0100 | [diff] [blame] | 21 | { |
| 22 | u32 reg32; |
| 23 | int count; |
| 24 | |
| 25 | /* Write (val & mask) to port */ |
| 26 | val &= mask; |
| 27 | reg32 = read32(port); |
| 28 | reg32 &= ~mask; |
| 29 | reg32 |= val; |
| 30 | write32(port, reg32); |
| 31 | |
Angel Pons | c9d6333 | 2020-06-21 15:38:29 +0200 | [diff] [blame^] | 32 | /* Wait for readback of register to match what was just written to it */ |
Patrick Georgi | e72a8a3 | 2012-11-06 11:05:09 +0100 | [diff] [blame] | 33 | count = 50; |
| 34 | do { |
| 35 | /* Wait 1ms based on BKDG wait time */ |
| 36 | mdelay(1); |
| 37 | reg32 = read32(port); |
| 38 | reg32 &= mask; |
| 39 | } while ((reg32 != val) && --count); |
| 40 | |
| 41 | /* Timeout occurred */ |
| 42 | if (!count) |
| 43 | return -1; |
| 44 | return 0; |
| 45 | } |
| 46 | |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 47 | static int codec_detect(u8 *base) |
Patrick Georgi | e72a8a3 | 2012-11-06 11:05:09 +0100 | [diff] [blame] | 48 | { |
| 49 | u32 reg32; |
| 50 | |
| 51 | /* Set Bit0 to 0 to enter reset state (BAR + 0x8)[0] */ |
| 52 | if (set_bits(base + 0x08, 1, 0) == -1) |
| 53 | goto no_codec; |
| 54 | |
| 55 | /* Set Bit 0 to 1 to exit reset state (BAR + 0x8)[0] */ |
| 56 | if (set_bits(base + 0x08, 1, 1) == -1) |
| 57 | goto no_codec; |
| 58 | |
| 59 | /* Read in Codec location (BAR + 0xe)[2..0]*/ |
| 60 | reg32 = read32(base + 0xe); |
| 61 | reg32 &= 0x0f; |
| 62 | if (!reg32) |
| 63 | goto no_codec; |
| 64 | |
| 65 | return reg32; |
| 66 | |
| 67 | no_codec: |
| 68 | /* Codec Not found */ |
| 69 | /* Put HDA back in reset (BAR + 0x8) [0] */ |
| 70 | set_bits(base + 0x08, 1, 0); |
| 71 | printk(BIOS_DEBUG, "Azalia: No codec!\n"); |
| 72 | return 0; |
| 73 | } |
| 74 | |
Elyes HAOUAS | e414a4e | 2019-01-03 10:40:43 +0100 | [diff] [blame] | 75 | static u32 find_verb(struct device *dev, u32 viddid, const u32 **verb) |
Patrick Georgi | e72a8a3 | 2012-11-06 11:05:09 +0100 | [diff] [blame] | 76 | { |
Angel Pons | c9d6333 | 2020-06-21 15:38:29 +0200 | [diff] [blame^] | 77 | int idx = 0; |
Patrick Georgi | e72a8a3 | 2012-11-06 11:05:09 +0100 | [diff] [blame] | 78 | |
| 79 | while (idx < (cim_verb_data_size / sizeof(u32))) { |
| 80 | u32 verb_size = 4 * cim_verb_data[idx+2]; // in u32 |
| 81 | if (cim_verb_data[idx] != viddid) { |
| 82 | idx += verb_size + 3; // skip verb + header |
| 83 | continue; |
| 84 | } |
| 85 | *verb = &cim_verb_data[idx+3]; |
| 86 | return verb_size; |
| 87 | } |
| 88 | |
| 89 | /* Not all codecs need to load another verb */ |
| 90 | return 0; |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Wait 50usec for the codec to indicate it is ready |
| 95 | * no response would imply that the codec is non-operative |
| 96 | */ |
| 97 | |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 98 | static int wait_for_ready(u8 *base) |
Patrick Georgi | e72a8a3 | 2012-11-06 11:05:09 +0100 | [diff] [blame] | 99 | { |
Angel Pons | c9d6333 | 2020-06-21 15:38:29 +0200 | [diff] [blame^] | 100 | /* Use a 50 usec timeout - the Linux kernel uses the same duration */ |
Patrick Georgi | e72a8a3 | 2012-11-06 11:05:09 +0100 | [diff] [blame] | 101 | int timeout = 50; |
| 102 | |
Elyes HAOUAS | ba28e8d | 2016-08-31 19:22:16 +0200 | [diff] [blame] | 103 | while (timeout--) { |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 104 | u32 reg32 = read32(base + HDA_ICII_REG); |
Patrick Georgi | e72a8a3 | 2012-11-06 11:05:09 +0100 | [diff] [blame] | 105 | if (!(reg32 & HDA_ICII_BUSY)) |
| 106 | return 0; |
| 107 | udelay(1); |
| 108 | } |
| 109 | |
| 110 | return -1; |
| 111 | } |
| 112 | |
| 113 | /** |
Angel Pons | c9d6333 | 2020-06-21 15:38:29 +0200 | [diff] [blame^] | 114 | * Wait 50usec for the codec to indicate that it accepted the previous command. |
| 115 | * No response would imply that the code is non-operative. |
Patrick Georgi | e72a8a3 | 2012-11-06 11:05:09 +0100 | [diff] [blame] | 116 | */ |
| 117 | |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 118 | static int wait_for_valid(u8 *base) |
Patrick Georgi | e72a8a3 | 2012-11-06 11:05:09 +0100 | [diff] [blame] | 119 | { |
| 120 | u32 reg32; |
| 121 | |
| 122 | /* Send the verb to the codec */ |
| 123 | reg32 = read32(base + 0x68); |
| 124 | reg32 |= (1 << 0) | (1 << 1); |
| 125 | write32(base + 0x68, reg32); |
| 126 | |
Angel Pons | c9d6333 | 2020-06-21 15:38:29 +0200 | [diff] [blame^] | 127 | /* Use a 50 usec timeout - the Linux kernel uses the same duration */ |
Patrick Georgi | e72a8a3 | 2012-11-06 11:05:09 +0100 | [diff] [blame] | 128 | |
| 129 | int timeout = 50; |
Elyes HAOUAS | ba28e8d | 2016-08-31 19:22:16 +0200 | [diff] [blame] | 130 | while (timeout--) { |
Patrick Georgi | e72a8a3 | 2012-11-06 11:05:09 +0100 | [diff] [blame] | 131 | reg32 = read32(base + HDA_ICII_REG); |
Angel Pons | c9d6333 | 2020-06-21 15:38:29 +0200 | [diff] [blame^] | 132 | if ((reg32 & (HDA_ICII_VALID | HDA_ICII_BUSY)) == HDA_ICII_VALID) |
Patrick Georgi | e72a8a3 | 2012-11-06 11:05:09 +0100 | [diff] [blame] | 133 | return 0; |
| 134 | udelay(1); |
| 135 | } |
| 136 | |
| 137 | return -1; |
| 138 | } |
| 139 | |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 140 | static void codec_init(struct device *dev, u8 *base, int addr) |
Patrick Georgi | e72a8a3 | 2012-11-06 11:05:09 +0100 | [diff] [blame] | 141 | { |
| 142 | u32 reg32; |
| 143 | const u32 *verb; |
| 144 | u32 verb_size; |
| 145 | int i; |
| 146 | |
| 147 | printk(BIOS_DEBUG, "HD Audio: Initializing codec #%d\n", addr); |
| 148 | |
| 149 | /* 1 */ |
| 150 | if (wait_for_ready(base) == -1) |
| 151 | return; |
| 152 | |
| 153 | reg32 = (addr << 28) | 0x000f0000; |
| 154 | write32(base + 0x60, reg32); |
| 155 | |
| 156 | if (wait_for_valid(base) == -1) |
| 157 | return; |
| 158 | |
| 159 | reg32 = read32(base + 0x64); |
| 160 | |
| 161 | /* 2 */ |
| 162 | printk(BIOS_DEBUG, "Azalia: codec viddid: %08x\n", reg32); |
| 163 | verb_size = find_verb(dev, reg32, &verb); |
| 164 | |
| 165 | if (!verb_size) { |
| 166 | printk(BIOS_DEBUG, "Azalia: No verb!\n"); |
| 167 | return; |
| 168 | } |
| 169 | printk(BIOS_DEBUG, "Azalia: verb_size: %d\n", verb_size); |
| 170 | |
| 171 | /* 3 */ |
| 172 | for (i = 0; i < verb_size; i++) { |
| 173 | if (wait_for_ready(base) == -1) |
| 174 | return; |
| 175 | |
| 176 | write32(base + 0x60, verb[i]); |
| 177 | |
| 178 | if (wait_for_valid(base) == -1) |
| 179 | return; |
| 180 | } |
| 181 | printk(BIOS_DEBUG, "Azalia: verb loaded.\n"); |
| 182 | } |
| 183 | |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 184 | static void codecs_init(struct device *dev, u8 *base, u32 codec_mask) |
Patrick Georgi | e72a8a3 | 2012-11-06 11:05:09 +0100 | [diff] [blame] | 185 | { |
| 186 | int i; |
| 187 | for (i = 2; i >= 0; i--) { |
| 188 | if (codec_mask & (1 << i)) |
| 189 | codec_init(dev, base, i); |
| 190 | } |
| 191 | |
| 192 | for (i = 0; i < pc_beep_verbs_size; i++) { |
| 193 | if (wait_for_ready(base) == -1) |
| 194 | return; |
| 195 | |
| 196 | write32(base + 0x60, pc_beep_verbs[i]); |
| 197 | |
| 198 | if (wait_for_valid(base) == -1) |
| 199 | return; |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | static void azalia_init(struct device *dev) |
| 204 | { |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 205 | u8 *base; |
Patrick Georgi | e72a8a3 | 2012-11-06 11:05:09 +0100 | [diff] [blame] | 206 | struct resource *res; |
| 207 | u32 codec_mask; |
Patrick Georgi | e72a8a3 | 2012-11-06 11:05:09 +0100 | [diff] [blame] | 208 | |
Patrick Georgi | e72a8a3 | 2012-11-06 11:05:09 +0100 | [diff] [blame] | 209 | // ESD |
Angel Pons | 6740647 | 2020-06-08 11:13:42 +0200 | [diff] [blame] | 210 | pci_update_config32(dev, 0x134, ~0x00ff0000, 2 << 16); |
Patrick Georgi | e72a8a3 | 2012-11-06 11:05:09 +0100 | [diff] [blame] | 211 | |
| 212 | // Link1 description |
Angel Pons | 6740647 | 2020-06-08 11:13:42 +0200 | [diff] [blame] | 213 | pci_update_config32(dev, 0x140, ~0x00ff0000, 2 << 16); |
Patrick Georgi | e72a8a3 | 2012-11-06 11:05:09 +0100 | [diff] [blame] | 214 | |
| 215 | // Port VC0 Resource Control Register |
Angel Pons | 6740647 | 2020-06-08 11:13:42 +0200 | [diff] [blame] | 216 | pci_update_config32(dev, 0x114, ~0x000000ff, 1); |
Patrick Georgi | e72a8a3 | 2012-11-06 11:05:09 +0100 | [diff] [blame] | 217 | |
| 218 | // VCi traffic class |
Angel Pons | 6740647 | 2020-06-08 11:13:42 +0200 | [diff] [blame] | 219 | pci_or_config8(dev, 0x44, 7 << 0); // TC7 |
Patrick Georgi | e72a8a3 | 2012-11-06 11:05:09 +0100 | [diff] [blame] | 220 | |
| 221 | // VCi Resource Control |
Angel Pons | 6740647 | 2020-06-08 11:13:42 +0200 | [diff] [blame] | 222 | pci_or_config32(dev, 0x120, (1 << 31) | (1 << 24) | (0x80 << 0)); /* VCi ID and map */ |
Patrick Georgi | e72a8a3 | 2012-11-06 11:05:09 +0100 | [diff] [blame] | 223 | |
| 224 | /* Set Bus Master */ |
Elyes HAOUAS | b9d2e22 | 2020-04-28 10:25:12 +0200 | [diff] [blame] | 225 | pci_or_config16(dev, PCI_COMMAND, PCI_COMMAND_MASTER); |
Patrick Georgi | e72a8a3 | 2012-11-06 11:05:09 +0100 | [diff] [blame] | 226 | |
Angel Pons | 6740647 | 2020-06-08 11:13:42 +0200 | [diff] [blame] | 227 | // Docking not supported |
| 228 | pci_and_config8(dev, 0x4d, (u8)~(1 << 7)); // Docking Status |
Patrick Georgi | e72a8a3 | 2012-11-06 11:05:09 +0100 | [diff] [blame] | 229 | |
| 230 | /* Lock some R/WO bits by writing their current value. */ |
Angel Pons | 6740647 | 2020-06-08 11:13:42 +0200 | [diff] [blame] | 231 | pci_update_config32(dev, 0x74, ~0, 0); |
Patrick Georgi | e72a8a3 | 2012-11-06 11:05:09 +0100 | [diff] [blame] | 232 | |
| 233 | res = find_resource(dev, 0x10); |
| 234 | if (!res) |
| 235 | return; |
| 236 | |
| 237 | // NOTE this will break as soon as the Azalia get's a bar above |
| 238 | // 4G. Is there anything we can do about it? |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 239 | base = res2mmio(res, 0, 0); |
Patrick Rudolph | 4af2add | 2018-11-26 15:56:11 +0100 | [diff] [blame] | 240 | printk(BIOS_DEBUG, "Azalia: base = %p\n", base); |
Patrick Georgi | e72a8a3 | 2012-11-06 11:05:09 +0100 | [diff] [blame] | 241 | codec_mask = codec_detect(base); |
| 242 | |
| 243 | if (codec_mask) { |
| 244 | printk(BIOS_DEBUG, "Azalia: codec_mask = %02x\n", codec_mask); |
| 245 | codecs_init(dev, base, codec_mask); |
| 246 | } |
| 247 | } |
| 248 | |
Patrick Georgi | e72a8a3 | 2012-11-06 11:05:09 +0100 | [diff] [blame] | 249 | static struct device_operations azalia_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_init, |
Angel Pons | 1fc0edd | 2020-05-31 00:03:28 +0200 | [diff] [blame] | 254 | .ops_pci = &pci_dev_ops_pci, |
Patrick Georgi | e72a8a3 | 2012-11-06 11:05:09 +0100 | [diff] [blame] | 255 | }; |
| 256 | |
| 257 | /* ICH9DH/ICH9DO/ICH9R/ICH9/ICH9M-E/ICH9M */ |
| 258 | static const struct pci_driver i82801ix_azalia __pci_driver = { |
| 259 | .ops = &azalia_ops, |
| 260 | .vendor = PCI_VENDOR_ID_INTEL, |
Felix Singer | 7f8b0cd8 | 2019-11-10 11:04:08 +0100 | [diff] [blame] | 261 | .device = PCI_DEVICE_ID_INTEL_82801IB_HD_AUDIO, |
Patrick Georgi | e72a8a3 | 2012-11-06 11:05:09 +0100 | [diff] [blame] | 262 | }; |