Arthur Heymans | 7b9c139 | 2017-04-09 20:40:39 +0200 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
Arthur Heymans | 7b9c139 | 2017-04-09 20:40:39 +0200 | [diff] [blame] | 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; version 2 of the License. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | */ |
| 14 | |
| 15 | #include <console/console.h> |
| 16 | #include <device/device.h> |
| 17 | #include <device/pci.h> |
| 18 | #include <device/pci_ids.h> |
| 19 | #include <device/pci_ops.h> |
Kyösti Mälkki | 13f6650 | 2019-03-03 08:01:05 +0200 | [diff] [blame] | 20 | #include <device/mmio.h> |
Arthur Heymans | 7b9c139 | 2017-04-09 20:40:39 +0200 | [diff] [blame] | 21 | #include <delay.h> |
| 22 | #include <device/azalia_device.h> |
Kyösti Mälkki | 12b121c | 2019-08-18 16:33:39 +0300 | [diff] [blame] | 23 | #include "chip.h" |
Arthur Heymans | 349e085 | 2017-04-09 20:48:37 +0200 | [diff] [blame] | 24 | #include "i82801jx.h" |
Arthur Heymans | 7b9c139 | 2017-04-09 20:40:39 +0200 | [diff] [blame] | 25 | |
| 26 | #define HDA_ICII_REG 0x68 |
| 27 | #define HDA_ICII_BUSY (1 << 0) |
| 28 | #define HDA_ICII_VALID (1 << 1) |
| 29 | |
Arthur Heymans | 349e085 | 2017-04-09 20:48:37 +0200 | [diff] [blame] | 30 | typedef struct southbridge_intel_i82801jx_config config_t; |
Arthur Heymans | 7b9c139 | 2017-04-09 20:40:39 +0200 | [diff] [blame] | 31 | |
| 32 | static int set_bits(void *port, u32 mask, u32 val) |
| 33 | { |
| 34 | u32 reg32; |
| 35 | int count; |
| 36 | |
| 37 | /* Write (val & mask) to port */ |
| 38 | val &= mask; |
| 39 | reg32 = read32(port); |
| 40 | reg32 &= ~mask; |
| 41 | reg32 |= val; |
| 42 | write32(port, reg32); |
| 43 | |
| 44 | /* Wait for readback of register to |
| 45 | * match what was just written to it |
| 46 | */ |
| 47 | count = 50; |
| 48 | do { |
| 49 | /* Wait 1ms based on BKDG wait time */ |
| 50 | mdelay(1); |
| 51 | reg32 = read32(port); |
| 52 | reg32 &= mask; |
| 53 | } while ((reg32 != val) && --count); |
| 54 | |
| 55 | /* Timeout occurred */ |
| 56 | if (!count) |
| 57 | return -1; |
| 58 | return 0; |
| 59 | } |
| 60 | |
| 61 | static int codec_detect(u8 *base) |
| 62 | { |
| 63 | u32 reg32; |
| 64 | |
| 65 | /* Set Bit0 to 0 to enter reset state (BAR + 0x8)[0] */ |
| 66 | if (set_bits(base + 0x08, 1, 0) == -1) |
| 67 | goto no_codec; |
| 68 | |
| 69 | /* Set Bit 0 to 1 to exit reset state (BAR + 0x8)[0] */ |
| 70 | if (set_bits(base + 0x08, 1, 1) == -1) |
| 71 | goto no_codec; |
| 72 | |
| 73 | /* Read in Codec location (BAR + 0xe)[2..0]*/ |
| 74 | reg32 = read32(base + 0xe); |
| 75 | reg32 &= 0x0f; |
| 76 | if (!reg32) |
| 77 | goto no_codec; |
| 78 | |
| 79 | return reg32; |
| 80 | |
| 81 | no_codec: |
| 82 | /* Codec Not found */ |
| 83 | /* Put HDA back in reset (BAR + 0x8) [0] */ |
| 84 | set_bits(base + 0x08, 1, 0); |
| 85 | printk(BIOS_DEBUG, "Azalia: No codec!\n"); |
| 86 | return 0; |
| 87 | } |
| 88 | |
Elyes HAOUAS | e414a4e | 2019-01-03 10:40:43 +0100 | [diff] [blame] | 89 | static u32 find_verb(struct device *dev, u32 viddid, const u32 **verb) |
Arthur Heymans | 7b9c139 | 2017-04-09 20:40:39 +0200 | [diff] [blame] | 90 | { |
| 91 | int idx=0; |
| 92 | |
| 93 | while (idx < (cim_verb_data_size / sizeof(u32))) { |
| 94 | u32 verb_size = 4 * cim_verb_data[idx+2]; // in u32 |
| 95 | if (cim_verb_data[idx] != viddid) { |
| 96 | idx += verb_size + 3; // skip verb + header |
| 97 | continue; |
| 98 | } |
| 99 | *verb = &cim_verb_data[idx+3]; |
| 100 | return verb_size; |
| 101 | } |
| 102 | |
| 103 | /* Not all codecs need to load another verb */ |
| 104 | return 0; |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Wait 50usec for the codec to indicate it is ready |
| 109 | * no response would imply that the codec is non-operative |
| 110 | */ |
| 111 | |
| 112 | static int wait_for_ready(u8 *base) |
| 113 | { |
| 114 | /* Use a 50 usec timeout - the Linux kernel uses the |
| 115 | * same duration */ |
| 116 | |
| 117 | int timeout = 50; |
| 118 | |
| 119 | while (timeout--) { |
| 120 | u32 reg32 = read32(base + HDA_ICII_REG); |
| 121 | if (!(reg32 & HDA_ICII_BUSY)) |
| 122 | return 0; |
| 123 | udelay(1); |
| 124 | } |
| 125 | |
| 126 | return -1; |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Wait 50usec for the codec to indicate that it accepted |
| 131 | * the previous command. No response would imply that the code |
| 132 | * is non-operative |
| 133 | */ |
| 134 | |
| 135 | static int wait_for_valid(u8 *base) |
| 136 | { |
| 137 | u32 reg32; |
| 138 | |
| 139 | /* Send the verb to the codec */ |
| 140 | reg32 = read32(base + 0x68); |
| 141 | reg32 |= (1 << 0) | (1 << 1); |
| 142 | write32(base + 0x68, reg32); |
| 143 | |
| 144 | /* Use a 50 usec timeout - the Linux kernel uses the |
| 145 | * same duration */ |
| 146 | |
| 147 | int timeout = 50; |
| 148 | while (timeout--) { |
| 149 | reg32 = read32(base + HDA_ICII_REG); |
| 150 | if ((reg32 & (HDA_ICII_VALID | HDA_ICII_BUSY)) == |
| 151 | HDA_ICII_VALID) |
| 152 | return 0; |
| 153 | udelay(1); |
| 154 | } |
| 155 | |
| 156 | return -1; |
| 157 | } |
| 158 | |
| 159 | static void codec_init(struct device *dev, u8 *base, int addr) |
| 160 | { |
| 161 | u32 reg32; |
| 162 | const u32 *verb; |
| 163 | u32 verb_size; |
| 164 | int i; |
| 165 | |
| 166 | printk(BIOS_DEBUG, "HD Audio: Initializing codec #%d\n", addr); |
| 167 | |
| 168 | /* 1 */ |
| 169 | if (wait_for_ready(base) == -1) |
| 170 | return; |
| 171 | |
| 172 | reg32 = (addr << 28) | 0x000f0000; |
| 173 | write32(base + 0x60, reg32); |
| 174 | |
| 175 | if (wait_for_valid(base) == -1) |
| 176 | return; |
| 177 | |
| 178 | reg32 = read32(base + 0x64); |
| 179 | |
| 180 | /* 2 */ |
| 181 | printk(BIOS_DEBUG, "Azalia: codec viddid: %08x\n", reg32); |
| 182 | verb_size = find_verb(dev, reg32, &verb); |
| 183 | |
| 184 | if (!verb_size) { |
| 185 | printk(BIOS_DEBUG, "Azalia: No verb!\n"); |
| 186 | return; |
| 187 | } |
| 188 | printk(BIOS_DEBUG, "Azalia: verb_size: %d\n", verb_size); |
| 189 | |
| 190 | /* 3 */ |
| 191 | for (i = 0; i < verb_size; i++) { |
| 192 | if (wait_for_ready(base) == -1) |
| 193 | return; |
| 194 | |
| 195 | write32(base + 0x60, verb[i]); |
| 196 | |
| 197 | if (wait_for_valid(base) == -1) |
| 198 | return; |
| 199 | } |
| 200 | printk(BIOS_DEBUG, "Azalia: verb loaded.\n"); |
| 201 | } |
| 202 | |
| 203 | static void codecs_init(struct device *dev, u8 *base, u32 codec_mask) |
| 204 | { |
| 205 | int i; |
| 206 | for (i = 2; i >= 0; i--) { |
| 207 | if (codec_mask & (1 << i)) |
| 208 | codec_init(dev, base, i); |
| 209 | } |
| 210 | |
| 211 | for (i = 0; i < pc_beep_verbs_size; i++) { |
| 212 | if (wait_for_ready(base) == -1) |
| 213 | return; |
| 214 | |
| 215 | write32(base + 0x60, pc_beep_verbs[i]); |
| 216 | |
| 217 | if (wait_for_valid(base) == -1) |
| 218 | return; |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | static void azalia_init(struct device *dev) |
| 223 | { |
| 224 | u8 *base; |
| 225 | struct resource *res; |
| 226 | u32 codec_mask; |
| 227 | u8 reg8; |
| 228 | u32 reg32; |
| 229 | |
| 230 | // ESD |
| 231 | reg32 = pci_read_config32(dev, 0x134); |
| 232 | reg32 &= 0xff00ffff; |
| 233 | reg32 |= (2 << 16); |
| 234 | pci_write_config32(dev, 0x134, reg32); |
| 235 | |
| 236 | // Link1 description |
| 237 | reg32 = pci_read_config32(dev, 0x140); |
| 238 | reg32 &= 0xff00ffff; |
| 239 | reg32 |= (2 << 16); |
| 240 | pci_write_config32(dev, 0x140, reg32); |
| 241 | |
| 242 | // Port VC0 Resource Control Register |
| 243 | reg32 = pci_read_config32(dev, 0x114); |
| 244 | reg32 &= 0xffffff00; |
| 245 | reg32 |= 1; |
| 246 | pci_write_config32(dev, 0x114, reg32); |
| 247 | |
| 248 | // VCi traffic class |
| 249 | reg8 = pci_read_config8(dev, 0x44); |
| 250 | reg8 |= (7 << 0); // TC7 |
| 251 | pci_write_config8(dev, 0x44, reg8); |
| 252 | |
| 253 | // VCi Resource Control |
| 254 | reg32 = pci_read_config32(dev, 0x120); |
| 255 | reg32 |= (1 << 31); |
| 256 | reg32 |= (1 << 24); // VCi ID |
| 257 | reg32 |= (0x80 << 0); // VCi map |
| 258 | pci_write_config32(dev, 0x120, reg32); |
| 259 | |
| 260 | /* Set Bus Master */ |
| 261 | reg32 = pci_read_config32(dev, PCI_COMMAND); |
| 262 | pci_write_config32(dev, PCI_COMMAND, reg32 | PCI_COMMAND_MASTER); |
| 263 | |
| 264 | reg8 = pci_read_config8(dev, 0x4d); // Docking Status |
| 265 | reg8 &= ~(1 << 7); // Docking not supported |
| 266 | pci_write_config8(dev, 0x4d, reg8); |
| 267 | |
| 268 | /* Lock some R/WO bits by writing their current value. */ |
| 269 | reg32 = pci_read_config32(dev, 0x74); |
| 270 | pci_write_config32(dev, 0x74, reg32); |
| 271 | |
| 272 | res = find_resource(dev, 0x10); |
| 273 | if (!res) |
| 274 | return; |
| 275 | |
| 276 | // NOTE this will break as soon as the Azalia get's a bar above |
| 277 | // 4G. Is there anything we can do about it? |
| 278 | base = res2mmio(res, 0, 0); |
| 279 | printk(BIOS_DEBUG, "Azalia: base = %08x\n", (u32)base); |
| 280 | codec_mask = codec_detect(base); |
| 281 | |
| 282 | if (codec_mask) { |
| 283 | printk(BIOS_DEBUG, "Azalia: codec_mask = %02x\n", codec_mask); |
| 284 | codecs_init(dev, base, codec_mask); |
| 285 | } |
| 286 | } |
| 287 | |
Arthur Heymans | 7b9c139 | 2017-04-09 20:40:39 +0200 | [diff] [blame] | 288 | static struct pci_operations azalia_pci_ops = { |
Subrata Banik | 4a0f071 | 2019-03-20 14:29:47 +0530 | [diff] [blame] | 289 | .set_subsystem = pci_dev_set_subsystem, |
Arthur Heymans | 7b9c139 | 2017-04-09 20:40:39 +0200 | [diff] [blame] | 290 | }; |
| 291 | |
| 292 | static struct device_operations azalia_ops = { |
| 293 | .read_resources = pci_dev_read_resources, |
| 294 | .set_resources = pci_dev_set_resources, |
| 295 | .enable_resources = pci_dev_enable_resources, |
| 296 | .init = azalia_init, |
| 297 | .scan_bus = 0, |
| 298 | .ops_pci = &azalia_pci_ops, |
| 299 | }; |
| 300 | |
Arthur Heymans | 349e085 | 2017-04-09 20:48:37 +0200 | [diff] [blame] | 301 | static const unsigned short pci_device_ids[] = { |
| 302 | 0x3a3e, |
| 303 | 0x3a6e, |
| 304 | 0 |
| 305 | }; |
| 306 | |
| 307 | static const struct pci_driver i82801jx_azalia __pci_driver = { |
Arthur Heymans | 7b9c139 | 2017-04-09 20:40:39 +0200 | [diff] [blame] | 308 | .ops = &azalia_ops, |
| 309 | .vendor = PCI_VENDOR_ID_INTEL, |
Arthur Heymans | 349e085 | 2017-04-09 20:48:37 +0200 | [diff] [blame] | 310 | .devices = pci_device_ids, |
Arthur Heymans | 7b9c139 | 2017-04-09 20:40:39 +0200 | [diff] [blame] | 311 | }; |