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. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
Paul Menzel | a46a712 | 2013-02-23 18:37:27 +0100 | [diff] [blame] | 19 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | #include <console/console.h> |
| 23 | #include <device/device.h> |
| 24 | #include <device/pci.h> |
| 25 | #include <device/pci_ids.h> |
| 26 | #include <device/pci_ops.h> |
| 27 | #include <arch/io.h> |
| 28 | #include <delay.h> |
| 29 | #include "pch.h" |
| 30 | |
| 31 | #define HDA_ICII_REG 0x68 |
Andrew Wu | ae8d069 | 2013-08-02 19:29:17 +0800 | [diff] [blame] | 32 | #define HDA_ICII_BUSY (1 << 0) |
| 33 | #define HDA_ICII_VALID (1 << 1) |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 34 | |
| 35 | typedef struct southbridge_intel_bd82x6x_config config_t; |
| 36 | |
| 37 | static int set_bits(u32 port, u32 mask, u32 val) |
| 38 | { |
| 39 | u32 reg32; |
| 40 | int count; |
| 41 | |
| 42 | /* Write (val & mask) to port */ |
| 43 | val &= mask; |
| 44 | reg32 = read32(port); |
| 45 | reg32 &= ~mask; |
| 46 | reg32 |= val; |
| 47 | write32(port, reg32); |
| 48 | |
| 49 | /* Wait for readback of register to |
| 50 | * match what was just written to it |
| 51 | */ |
| 52 | count = 50; |
| 53 | do { |
| 54 | /* Wait 1ms based on BKDG wait time */ |
| 55 | mdelay(1); |
| 56 | reg32 = read32(port); |
| 57 | reg32 &= mask; |
| 58 | } while ((reg32 != val) && --count); |
| 59 | |
| 60 | /* Timeout occurred */ |
| 61 | if (!count) |
| 62 | return -1; |
| 63 | return 0; |
| 64 | } |
| 65 | |
| 66 | static int codec_detect(u32 base) |
| 67 | { |
| 68 | u8 reg8; |
| 69 | |
| 70 | /* Set Bit 0 to 1 to exit reset state (BAR + 0x8)[0] */ |
| 71 | if (set_bits(base + 0x08, 1, 1) == -1) |
| 72 | goto no_codec; |
| 73 | |
| 74 | /* Write back the value once reset bit is set. */ |
| 75 | write16(base + 0x0, read16(base + 0x0)); |
| 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 | |
| 93 | const u32 * cim_verb_data = NULL; |
| 94 | u32 cim_verb_data_size = 0; |
Dylan Reid | b98d078 | 2012-04-27 11:37:33 -0700 | [diff] [blame] | 95 | const u32 * pc_beep_verbs = NULL; |
| 96 | u32 pc_beep_verbs_size = 0; |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 97 | |
| 98 | static u32 find_verb(struct device *dev, u32 viddid, const u32 ** verb) |
| 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 | |
| 121 | static int wait_for_ready(u32 base) |
| 122 | { |
Stefan Reinauer | 15ba2bc | 2012-11-14 12:25:15 -0800 | [diff] [blame] | 123 | /* Use a 1msec timeout */ |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 124 | |
Stefan Reinauer | 15ba2bc | 2012-11-14 12:25:15 -0800 | [diff] [blame] | 125 | int timeout = 1000; |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 126 | |
| 127 | while(timeout--) { |
| 128 | u32 reg32 = read32(base + HDA_ICII_REG); |
| 129 | if (!(reg32 & HDA_ICII_BUSY)) |
| 130 | return 0; |
| 131 | udelay(1); |
| 132 | } |
| 133 | |
| 134 | return -1; |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Wait 50usec for the codec to indicate that it accepted |
| 139 | * the previous command. No response would imply that the code |
| 140 | * is non-operative |
| 141 | */ |
| 142 | |
| 143 | static int wait_for_valid(u32 base) |
| 144 | { |
| 145 | u32 reg32; |
| 146 | |
| 147 | /* Send the verb to the codec */ |
| 148 | reg32 = read32(base + HDA_ICII_REG); |
| 149 | reg32 |= HDA_ICII_BUSY | HDA_ICII_VALID; |
| 150 | write32(base + HDA_ICII_REG, reg32); |
| 151 | |
Stefan Reinauer | 15ba2bc | 2012-11-14 12:25:15 -0800 | [diff] [blame] | 152 | /* Use a 1msec timeout */ |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 153 | |
Stefan Reinauer | 15ba2bc | 2012-11-14 12:25:15 -0800 | [diff] [blame] | 154 | int timeout = 1000; |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 155 | while(timeout--) { |
| 156 | reg32 = read32(base + HDA_ICII_REG); |
| 157 | if ((reg32 & (HDA_ICII_VALID | HDA_ICII_BUSY)) == |
| 158 | HDA_ICII_VALID) |
| 159 | return 0; |
| 160 | udelay(1); |
| 161 | } |
| 162 | |
| 163 | return -1; |
| 164 | } |
| 165 | |
| 166 | static void codec_init(struct device *dev, u32 base, int addr) |
| 167 | { |
| 168 | u32 reg32; |
| 169 | const u32 *verb; |
| 170 | u32 verb_size; |
| 171 | int i; |
| 172 | |
| 173 | printk(BIOS_DEBUG, "Azalia: Initializing codec #%d\n", addr); |
| 174 | |
| 175 | /* 1 */ |
| 176 | if (wait_for_ready(base) == -1) { |
| 177 | printk(BIOS_DEBUG, " codec not ready.\n"); |
| 178 | return; |
| 179 | } |
| 180 | |
| 181 | reg32 = (addr << 28) | 0x000f0000; |
| 182 | write32(base + 0x60, reg32); |
| 183 | |
| 184 | if (wait_for_valid(base) == -1) { |
| 185 | printk(BIOS_DEBUG, " codec not valid.\n"); |
| 186 | return; |
| 187 | } |
| 188 | |
| 189 | reg32 = read32(base + 0x64); |
| 190 | |
| 191 | /* 2 */ |
| 192 | printk(BIOS_DEBUG, "Azalia: codec viddid: %08x\n", reg32); |
| 193 | verb_size = find_verb(dev, reg32, &verb); |
| 194 | |
| 195 | if (!verb_size) { |
| 196 | printk(BIOS_DEBUG, "Azalia: No verb!\n"); |
| 197 | return; |
| 198 | } |
| 199 | printk(BIOS_DEBUG, "Azalia: verb_size: %d\n", verb_size); |
| 200 | |
| 201 | /* 3 */ |
| 202 | for (i = 0; i < verb_size; i++) { |
| 203 | if (wait_for_ready(base) == -1) |
| 204 | return; |
| 205 | |
| 206 | write32(base + 0x60, verb[i]); |
| 207 | |
| 208 | if (wait_for_valid(base) == -1) |
| 209 | return; |
| 210 | } |
| 211 | printk(BIOS_DEBUG, "Azalia: verb loaded.\n"); |
| 212 | } |
| 213 | |
| 214 | static void codecs_init(struct device *dev, u32 base, u32 codec_mask) |
| 215 | { |
| 216 | int i; |
| 217 | for (i = 3; i >= 0; i--) { |
| 218 | if (codec_mask & (1 << i)) |
| 219 | codec_init(dev, base, i); |
| 220 | } |
Dylan Reid | b98d078 | 2012-04-27 11:37:33 -0700 | [diff] [blame] | 221 | |
| 222 | for (i = 0; i < pc_beep_verbs_size; i++) { |
| 223 | if (wait_for_ready(base) == -1) |
| 224 | return; |
| 225 | |
| 226 | write32(base + 0x60, pc_beep_verbs[i]); |
| 227 | |
| 228 | if (wait_for_valid(base) == -1) |
| 229 | return; |
| 230 | } |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | static void azalia_init(struct device *dev) |
| 234 | { |
| 235 | u32 base; |
| 236 | struct resource *res; |
| 237 | u32 codec_mask; |
| 238 | u8 reg8; |
| 239 | u16 reg16; |
| 240 | u32 reg32; |
| 241 | |
| 242 | /* Find base address */ |
| 243 | res = find_resource(dev, PCI_BASE_ADDRESS_0); |
| 244 | if (!res) |
| 245 | return; |
| 246 | |
| 247 | // NOTE this will break as soon as the Azalia get's a bar above |
| 248 | // 4G. Is there anything we can do about it? |
| 249 | base = (u32)res->base; |
| 250 | printk(BIOS_DEBUG, "Azalia: base = %08x\n", (u32)base); |
| 251 | |
| 252 | if (RCBA32(0x2030) & (1 << 31)) { |
Kyösti Mälkki | fd98c65 | 2013-07-26 08:50:53 +0300 | [diff] [blame] | 253 | reg32 = pci_read_config32(dev, 0x120); |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 254 | reg32 &= 0xf8ffff01; |
Stefan Reinauer | 15ba2bc | 2012-11-14 12:25:15 -0800 | [diff] [blame] | 255 | reg32 |= (1 << 24); // 2 << 24 for server |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 256 | reg32 |= RCBA32(0x2030) & 0xfe; |
Kyösti Mälkki | fd98c65 | 2013-07-26 08:50:53 +0300 | [diff] [blame] | 257 | pci_write_config32(dev, 0x120, reg32); |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 258 | |
Kyösti Mälkki | fd98c65 | 2013-07-26 08:50:53 +0300 | [diff] [blame] | 259 | reg16 = pci_read_config16(dev, 0x78); |
Stefan Reinauer | 15ba2bc | 2012-11-14 12:25:15 -0800 | [diff] [blame] | 260 | reg16 |= (1 << 11); |
Kyösti Mälkki | fd98c65 | 2013-07-26 08:50:53 +0300 | [diff] [blame] | 261 | pci_write_config16(dev, 0x78, reg16); |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 262 | } else |
| 263 | printk(BIOS_DEBUG, "Azalia: V1CTL disabled.\n"); |
| 264 | |
Kyösti Mälkki | fd98c65 | 2013-07-26 08:50:53 +0300 | [diff] [blame] | 265 | reg32 = pci_read_config32(dev, 0x114); |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 266 | reg32 &= ~0xfe; |
Kyösti Mälkki | fd98c65 | 2013-07-26 08:50:53 +0300 | [diff] [blame] | 267 | pci_write_config32(dev, 0x114, reg32); |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 268 | |
| 269 | // Set VCi enable bit |
Kyösti Mälkki | fd98c65 | 2013-07-26 08:50:53 +0300 | [diff] [blame] | 270 | reg32 = pci_read_config32(dev, 0x120); |
Stefan Reinauer | 15ba2bc | 2012-11-14 12:25:15 -0800 | [diff] [blame] | 271 | reg32 |= (1 << 31); |
Kyösti Mälkki | fd98c65 | 2013-07-26 08:50:53 +0300 | [diff] [blame] | 272 | pci_write_config32(dev, 0x120, reg32); |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 273 | |
| 274 | // Enable HDMI codec: |
| 275 | reg32 = pci_read_config32(dev, 0xc4); |
| 276 | reg32 |= (1 << 1); |
| 277 | pci_write_config32(dev, 0xc4, reg32); |
| 278 | |
| 279 | reg8 = pci_read_config8(dev, 0x43); |
| 280 | reg8 |= (1 << 6); |
| 281 | pci_write_config8(dev, 0x43, reg8); |
| 282 | |
| 283 | /* Additional programming steps */ |
| 284 | reg32 = pci_read_config32(dev, 0xc4); |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 285 | reg32 |= (1 << 13); |
| 286 | pci_write_config32(dev, 0xc4, reg32); |
| 287 | |
| 288 | reg32 = pci_read_config32(dev, 0xc4); |
| 289 | reg32 |= (1 << 10); |
| 290 | pci_write_config32(dev, 0xc4, reg32); |
| 291 | |
| 292 | reg32 = pci_read_config32(dev, 0xd0); |
| 293 | reg32 &= ~(1 << 31); |
| 294 | pci_write_config32(dev, 0xd0, reg32); |
| 295 | |
Stefan Reinauer | 15ba2bc | 2012-11-14 12:25:15 -0800 | [diff] [blame] | 296 | if (dev->device == 0x1e20) { |
| 297 | /* Additional step on Panther Point */ |
| 298 | reg32 = pci_read_config32(dev, 0xc4); |
| 299 | reg32 |= (1 << 17); |
| 300 | pci_write_config32(dev, 0xc4, reg32); |
| 301 | } |
| 302 | |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 303 | /* Set Bus Master */ |
| 304 | reg32 = pci_read_config32(dev, PCI_COMMAND); |
| 305 | pci_write_config32(dev, PCI_COMMAND, reg32 | PCI_COMMAND_MASTER); |
| 306 | |
| 307 | pci_write_config8(dev, 0x3c, 0x0a); // unused? |
| 308 | |
| 309 | /* Codec Initialization Programming Sequence */ |
Stefan Reinauer | 15ba2bc | 2012-11-14 12:25:15 -0800 | [diff] [blame] | 310 | |
| 311 | /* Take controller out of reset */ |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 312 | reg32 = read32(base + 0x08); |
| 313 | reg32 |= (1 << 0); |
| 314 | write32(base + 0x08, reg32); |
Stefan Reinauer | 15ba2bc | 2012-11-14 12:25:15 -0800 | [diff] [blame] | 315 | /* Wait 1ms */ |
| 316 | udelay(1000); |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 317 | |
| 318 | // |
| 319 | reg8 = pci_read_config8(dev, 0x40); // Audio Control |
| 320 | reg8 |= 1; // Select Azalia mode. This needs to be controlled via devicetree.cb |
| 321 | pci_write_config8(dev, 0x40, reg8); |
| 322 | |
| 323 | reg8 = pci_read_config8(dev, 0x4d); // Docking Status |
| 324 | reg8 &= ~(1 << 7); // Docking not supported |
| 325 | pci_write_config8(dev, 0x4d, reg8); |
| 326 | |
| 327 | codec_mask = codec_detect(base); |
| 328 | |
| 329 | if (codec_mask) { |
| 330 | printk(BIOS_DEBUG, "Azalia: codec_mask = %02x\n", codec_mask); |
| 331 | codecs_init(dev, base, codec_mask); |
| 332 | } |
| 333 | |
| 334 | /* Enable dynamic clock gating */ |
| 335 | reg8 = pci_read_config8(dev, 0x43); |
| 336 | reg8 &= ~0x7; |
| 337 | reg8 |= (1 << 2) | (1 << 0); |
| 338 | pci_write_config8(dev, 0x43, reg8); |
| 339 | } |
| 340 | |
| 341 | static void azalia_set_subsystem(device_t dev, unsigned vendor, unsigned device) |
| 342 | { |
| 343 | if (!vendor || !device) { |
| 344 | pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID, |
| 345 | pci_read_config32(dev, PCI_VENDOR_ID)); |
| 346 | } else { |
| 347 | pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID, |
| 348 | ((device & 0xffff) << 16) | (vendor & 0xffff)); |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | static struct pci_operations azalia_pci_ops = { |
| 353 | .set_subsystem = azalia_set_subsystem, |
| 354 | }; |
| 355 | |
| 356 | static struct device_operations azalia_ops = { |
| 357 | .read_resources = pci_dev_read_resources, |
| 358 | .set_resources = pci_dev_set_resources, |
| 359 | .enable_resources = pci_dev_enable_resources, |
| 360 | .init = azalia_init, |
| 361 | .scan_bus = 0, |
| 362 | .ops_pci = &azalia_pci_ops, |
| 363 | }; |
| 364 | |
Stefan Reinauer | 9a380ab | 2012-06-22 13:16:11 -0700 | [diff] [blame] | 365 | static const unsigned short pci_device_ids[] = { 0x1c20, 0x1e20, 0 }; |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 366 | |
Stefan Reinauer | 9a380ab | 2012-06-22 13:16:11 -0700 | [diff] [blame] | 367 | static const struct pci_driver pch_azalia __pci_driver = { |
| 368 | .ops = &azalia_ops, |
| 369 | .vendor = PCI_VENDOR_ID_INTEL, |
| 370 | .devices = pci_device_ids, |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 371 | }; |