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