Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [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 The ChromiumOS Authors. All rights reserved. |
| 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. |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [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> |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [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 | |
| 27 | #include "chip.h" |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 28 | #include "pch.h" |
| 29 | |
| 30 | #define HDA_ICII_REG 0x68 |
Andrew Wu | ae8d069 | 2013-08-02 19:29:17 +0800 | [diff] [blame] | 31 | #define HDA_ICII_BUSY (1 << 0) |
| 32 | #define HDA_ICII_VALID (1 << 1) |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 33 | |
| 34 | typedef struct southbridge_intel_bd82x6x_config config_t; |
| 35 | |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 36 | static int set_bits(void *port, u32 mask, u32 val) |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 37 | { |
| 38 | u32 reg32; |
| 39 | int count; |
| 40 | |
| 41 | /* Write (val & mask) to port */ |
| 42 | val &= mask; |
| 43 | reg32 = read32(port); |
| 44 | reg32 &= ~mask; |
| 45 | reg32 |= val; |
| 46 | write32(port, reg32); |
| 47 | |
| 48 | /* Wait for readback of register to |
| 49 | * match what was just written to it |
| 50 | */ |
| 51 | count = 50; |
| 52 | do { |
| 53 | /* Wait 1ms based on BKDG wait time */ |
| 54 | mdelay(1); |
| 55 | reg32 = read32(port); |
| 56 | reg32 &= mask; |
| 57 | } while ((reg32 != val) && --count); |
| 58 | |
| 59 | /* Timeout occurred */ |
| 60 | if (!count) |
| 61 | return -1; |
| 62 | return 0; |
| 63 | } |
| 64 | |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 65 | static int codec_detect(u8 *base) |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 66 | { |
| 67 | u8 reg8; |
| 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 | /* Write back the value once reset bit is set. */ |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 74 | write16(base + 0x0, |
| 75 | read16(base + 0x0)); |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 76 | |
| 77 | /* Read in Codec location (BAR + 0xe)[2..0]*/ |
| 78 | reg8 = read8(base + 0xe); |
| 79 | reg8 &= 0x0f; |
| 80 | if (!reg8) |
| 81 | goto no_codec; |
| 82 | |
| 83 | return reg8; |
| 84 | |
| 85 | no_codec: |
| 86 | /* Codec Not found */ |
| 87 | /* Put HDA back in reset (BAR + 0x8) [0] */ |
| 88 | set_bits(base + 0x08, 1, 0); |
| 89 | printk(BIOS_DEBUG, "Azalia: No codec!\n"); |
| 90 | return 0; |
| 91 | } |
| 92 | |
Elyes HAOUAS | e414a4e | 2019-01-03 10:40:43 +0100 | [diff] [blame] | 93 | static u32 find_verb(struct device *dev, u32 viddid, const u32 **verb) |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 94 | { |
| 95 | int idx=0; |
| 96 | |
| 97 | while (idx < (cim_verb_data_size / sizeof(u32))) { |
| 98 | u32 verb_size = 4 * cim_verb_data[idx+2]; // in u32 |
| 99 | if (cim_verb_data[idx] != viddid) { |
| 100 | idx += verb_size + 3; // skip verb + header |
| 101 | continue; |
| 102 | } |
| 103 | *verb = &cim_verb_data[idx+3]; |
| 104 | return verb_size; |
| 105 | } |
| 106 | |
| 107 | /* Not all codecs need to load another verb */ |
| 108 | return 0; |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Wait 50usec for the codec to indicate it is ready |
| 113 | * no response would imply that the codec is non-operative |
| 114 | */ |
| 115 | |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 116 | static int wait_for_ready(u8 *base) |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 117 | { |
Stefan Reinauer | 15ba2bc | 2012-11-14 12:25:15 -0800 | [diff] [blame] | 118 | /* Use a 1msec timeout */ |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 119 | |
Stefan Reinauer | 15ba2bc | 2012-11-14 12:25:15 -0800 | [diff] [blame] | 120 | int timeout = 1000; |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 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); |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [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) |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 139 | { |
| 140 | u32 reg32; |
| 141 | |
| 142 | /* Send the verb to the codec */ |
| 143 | reg32 = read32(base + HDA_ICII_REG); |
| 144 | reg32 |= HDA_ICII_BUSY | HDA_ICII_VALID; |
| 145 | write32(base + HDA_ICII_REG, reg32); |
| 146 | |
Stefan Reinauer | 15ba2bc | 2012-11-14 12:25:15 -0800 | [diff] [blame] | 147 | /* Use a 1msec timeout */ |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 148 | |
Stefan Reinauer | 15ba2bc | 2012-11-14 12:25:15 -0800 | [diff] [blame] | 149 | int timeout = 1000; |
Elyes HAOUAS | ba28e8d | 2016-08-31 19:22:16 +0200 | [diff] [blame] | 150 | while (timeout--) { |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 151 | reg32 = read32(base + HDA_ICII_REG); |
| 152 | if ((reg32 & (HDA_ICII_VALID | HDA_ICII_BUSY)) == |
| 153 | HDA_ICII_VALID) |
| 154 | return 0; |
| 155 | udelay(1); |
| 156 | } |
| 157 | |
| 158 | return -1; |
| 159 | } |
| 160 | |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 161 | static void codec_init(struct device *dev, u8 *base, int addr) |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 162 | { |
| 163 | u32 reg32; |
| 164 | const u32 *verb; |
| 165 | u32 verb_size; |
| 166 | int i; |
| 167 | |
| 168 | printk(BIOS_DEBUG, "Azalia: Initializing codec #%d\n", addr); |
| 169 | |
| 170 | /* 1 */ |
| 171 | if (wait_for_ready(base) == -1) { |
| 172 | printk(BIOS_DEBUG, " codec not ready.\n"); |
| 173 | return; |
| 174 | } |
| 175 | |
| 176 | reg32 = (addr << 28) | 0x000f0000; |
| 177 | write32(base + 0x60, reg32); |
| 178 | |
| 179 | if (wait_for_valid(base) == -1) { |
| 180 | printk(BIOS_DEBUG, " codec not valid.\n"); |
| 181 | return; |
| 182 | } |
| 183 | |
| 184 | reg32 = read32(base + 0x64); |
| 185 | |
| 186 | /* 2 */ |
| 187 | printk(BIOS_DEBUG, "Azalia: codec viddid: %08x\n", reg32); |
| 188 | verb_size = find_verb(dev, reg32, &verb); |
| 189 | |
| 190 | if (!verb_size) { |
| 191 | printk(BIOS_DEBUG, "Azalia: No verb!\n"); |
| 192 | return; |
| 193 | } |
| 194 | printk(BIOS_DEBUG, "Azalia: verb_size: %d\n", verb_size); |
| 195 | |
| 196 | /* 3 */ |
| 197 | for (i = 0; i < verb_size; i++) { |
| 198 | if (wait_for_ready(base) == -1) |
| 199 | return; |
| 200 | |
| 201 | write32(base + 0x60, verb[i]); |
| 202 | |
| 203 | if (wait_for_valid(base) == -1) |
| 204 | return; |
| 205 | } |
| 206 | printk(BIOS_DEBUG, "Azalia: verb loaded.\n"); |
| 207 | } |
| 208 | |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 209 | static void codecs_init(struct device *dev, u8 *base, u32 codec_mask) |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 210 | { |
| 211 | int i; |
| 212 | for (i = 3; i >= 0; i--) { |
| 213 | if (codec_mask & (1 << i)) |
| 214 | codec_init(dev, base, i); |
| 215 | } |
Dylan Reid | b98d078 | 2012-04-27 11:37:33 -0700 | [diff] [blame] | 216 | |
| 217 | for (i = 0; i < pc_beep_verbs_size; i++) { |
| 218 | if (wait_for_ready(base) == -1) |
| 219 | return; |
| 220 | |
| 221 | write32(base + 0x60, pc_beep_verbs[i]); |
| 222 | |
| 223 | if (wait_for_valid(base) == -1) |
| 224 | return; |
| 225 | } |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | static void azalia_init(struct device *dev) |
| 229 | { |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 230 | u8 *base; |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 231 | struct resource *res; |
| 232 | u32 codec_mask; |
| 233 | u8 reg8; |
| 234 | u16 reg16; |
| 235 | u32 reg32; |
| 236 | |
| 237 | /* Find base address */ |
| 238 | res = find_resource(dev, PCI_BASE_ADDRESS_0); |
| 239 | if (!res) |
| 240 | return; |
| 241 | |
| 242 | // NOTE this will break as soon as the Azalia get's a bar above |
| 243 | // 4G. Is there anything we can do about it? |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 244 | base = res2mmio(res, 0, 0); |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 245 | printk(BIOS_DEBUG, "Azalia: base = %08x\n", (u32)base); |
| 246 | |
Patrick Rudolph | 4f8b108 | 2019-07-14 11:54:58 +0200 | [diff] [blame] | 247 | if (RCBA32(CIR31) & (1 << 31)) { |
Kyösti Mälkki | fd98c65 | 2013-07-26 08:50:53 +0300 | [diff] [blame] | 248 | reg32 = pci_read_config32(dev, 0x120); |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 249 | reg32 &= 0xf8ffff01; |
Stefan Reinauer | 15ba2bc | 2012-11-14 12:25:15 -0800 | [diff] [blame] | 250 | reg32 |= (1 << 24); // 2 << 24 for server |
Patrick Rudolph | 4f8b108 | 2019-07-14 11:54:58 +0200 | [diff] [blame] | 251 | reg32 |= RCBA32(CIR31) & 0xfe; |
Kyösti Mälkki | fd98c65 | 2013-07-26 08:50:53 +0300 | [diff] [blame] | 252 | pci_write_config32(dev, 0x120, reg32); |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 253 | |
Kyösti Mälkki | fd98c65 | 2013-07-26 08:50:53 +0300 | [diff] [blame] | 254 | reg16 = pci_read_config16(dev, 0x78); |
Stefan Reinauer | 15ba2bc | 2012-11-14 12:25:15 -0800 | [diff] [blame] | 255 | reg16 |= (1 << 11); |
Kyösti Mälkki | fd98c65 | 2013-07-26 08:50:53 +0300 | [diff] [blame] | 256 | pci_write_config16(dev, 0x78, reg16); |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 257 | } else |
| 258 | printk(BIOS_DEBUG, "Azalia: V1CTL disabled.\n"); |
| 259 | |
Kyösti Mälkki | fd98c65 | 2013-07-26 08:50:53 +0300 | [diff] [blame] | 260 | reg32 = pci_read_config32(dev, 0x114); |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 261 | reg32 &= ~0xfe; |
Kyösti Mälkki | fd98c65 | 2013-07-26 08:50:53 +0300 | [diff] [blame] | 262 | pci_write_config32(dev, 0x114, reg32); |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 263 | |
| 264 | // Set VCi enable bit |
Kyösti Mälkki | fd98c65 | 2013-07-26 08:50:53 +0300 | [diff] [blame] | 265 | reg32 = pci_read_config32(dev, 0x120); |
Stefan Reinauer | 15ba2bc | 2012-11-14 12:25:15 -0800 | [diff] [blame] | 266 | reg32 |= (1 << 31); |
Kyösti Mälkki | fd98c65 | 2013-07-26 08:50:53 +0300 | [diff] [blame] | 267 | pci_write_config32(dev, 0x120, reg32); |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 268 | |
| 269 | // Enable HDMI codec: |
| 270 | reg32 = pci_read_config32(dev, 0xc4); |
| 271 | reg32 |= (1 << 1); |
| 272 | pci_write_config32(dev, 0xc4, reg32); |
| 273 | |
| 274 | reg8 = pci_read_config8(dev, 0x43); |
| 275 | reg8 |= (1 << 6); |
| 276 | pci_write_config8(dev, 0x43, reg8); |
| 277 | |
| 278 | /* Additional programming steps */ |
| 279 | reg32 = pci_read_config32(dev, 0xc4); |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 280 | reg32 |= (1 << 13); |
| 281 | pci_write_config32(dev, 0xc4, reg32); |
| 282 | |
| 283 | reg32 = pci_read_config32(dev, 0xc4); |
| 284 | reg32 |= (1 << 10); |
| 285 | pci_write_config32(dev, 0xc4, reg32); |
| 286 | |
| 287 | reg32 = pci_read_config32(dev, 0xd0); |
| 288 | reg32 &= ~(1 << 31); |
| 289 | pci_write_config32(dev, 0xd0, reg32); |
| 290 | |
Stefan Reinauer | 15ba2bc | 2012-11-14 12:25:15 -0800 | [diff] [blame] | 291 | if (dev->device == 0x1e20) { |
| 292 | /* Additional step on Panther Point */ |
| 293 | reg32 = pci_read_config32(dev, 0xc4); |
| 294 | reg32 |= (1 << 17); |
| 295 | pci_write_config32(dev, 0xc4, reg32); |
| 296 | } |
| 297 | |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 298 | /* Set Bus Master */ |
| 299 | reg32 = pci_read_config32(dev, PCI_COMMAND); |
| 300 | pci_write_config32(dev, PCI_COMMAND, reg32 | PCI_COMMAND_MASTER); |
| 301 | |
| 302 | pci_write_config8(dev, 0x3c, 0x0a); // unused? |
| 303 | |
| 304 | /* Codec Initialization Programming Sequence */ |
Stefan Reinauer | 15ba2bc | 2012-11-14 12:25:15 -0800 | [diff] [blame] | 305 | |
| 306 | /* Take controller out of reset */ |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 307 | reg32 = read32(base + 0x08); |
| 308 | reg32 |= (1 << 0); |
| 309 | write32(base + 0x08, reg32); |
Stefan Reinauer | 15ba2bc | 2012-11-14 12:25:15 -0800 | [diff] [blame] | 310 | /* Wait 1ms */ |
| 311 | udelay(1000); |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 312 | |
| 313 | // |
| 314 | reg8 = pci_read_config8(dev, 0x40); // Audio Control |
| 315 | reg8 |= 1; // Select Azalia mode. This needs to be controlled via devicetree.cb |
| 316 | pci_write_config8(dev, 0x40, reg8); |
| 317 | |
| 318 | reg8 = pci_read_config8(dev, 0x4d); // Docking Status |
| 319 | reg8 &= ~(1 << 7); // Docking not supported |
| 320 | pci_write_config8(dev, 0x4d, reg8); |
| 321 | |
| 322 | codec_mask = codec_detect(base); |
| 323 | |
| 324 | if (codec_mask) { |
| 325 | printk(BIOS_DEBUG, "Azalia: codec_mask = %02x\n", codec_mask); |
| 326 | codecs_init(dev, base, codec_mask); |
| 327 | } |
| 328 | |
| 329 | /* Enable dynamic clock gating */ |
| 330 | reg8 = pci_read_config8(dev, 0x43); |
| 331 | reg8 &= ~0x7; |
| 332 | reg8 |= (1 << 2) | (1 << 0); |
| 333 | pci_write_config8(dev, 0x43, reg8); |
| 334 | } |
| 335 | |
Aaron Durbin | aa090cb | 2017-09-13 16:01:52 -0600 | [diff] [blame] | 336 | static const char *azalia_acpi_name(const struct device *dev) |
Patrick Rudolph | 604f698 | 2017-06-07 09:46:52 +0200 | [diff] [blame] | 337 | { |
| 338 | return "HDEF"; |
| 339 | } |
| 340 | |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 341 | static struct pci_operations azalia_pci_ops = { |
Subrata Banik | 4a0f071 | 2019-03-20 14:29:47 +0530 | [diff] [blame] | 342 | .set_subsystem = pci_dev_set_subsystem, |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 343 | }; |
| 344 | |
| 345 | static struct device_operations azalia_ops = { |
| 346 | .read_resources = pci_dev_read_resources, |
| 347 | .set_resources = pci_dev_set_resources, |
| 348 | .enable_resources = pci_dev_enable_resources, |
| 349 | .init = azalia_init, |
| 350 | .scan_bus = 0, |
| 351 | .ops_pci = &azalia_pci_ops, |
Patrick Rudolph | 604f698 | 2017-06-07 09:46:52 +0200 | [diff] [blame] | 352 | .acpi_name = azalia_acpi_name, |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 353 | }; |
| 354 | |
Stefan Reinauer | 9a380ab | 2012-06-22 13:16:11 -0700 | [diff] [blame] | 355 | static const unsigned short pci_device_ids[] = { 0x1c20, 0x1e20, 0 }; |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 356 | |
Stefan Reinauer | 9a380ab | 2012-06-22 13:16:11 -0700 | [diff] [blame] | 357 | static const struct pci_driver pch_azalia __pci_driver = { |
| 358 | .ops = &azalia_ops, |
| 359 | .vendor = PCI_VENDOR_ID_INTEL, |
| 360 | .devices = pci_device_ids, |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 361 | }; |