Angel Pons | 182dbde | 2020-04-02 23:49:05 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [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> |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [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 | |
| 12 | #include "chip.h" |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 13 | #include "pch.h" |
| 14 | |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 15 | typedef struct southbridge_intel_bd82x6x_config config_t; |
| 16 | |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 17 | static int codec_detect(u8 *base) |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 18 | { |
| 19 | u8 reg8; |
| 20 | |
Angel Pons | 7f839f6 | 2020-12-05 19:02:14 +0100 | [diff] [blame] | 21 | if (azalia_exit_reset(base) < 0) |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 22 | goto no_codec; |
| 23 | |
| 24 | /* Write back the value once reset bit is set. */ |
Elyes HAOUAS | 6ea24ff | 2020-08-11 09:21:24 +0200 | [diff] [blame] | 25 | write16(base + HDA_GCAP_REG, read16(base + HDA_GCAP_REG)); |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 26 | |
Elyes HAOUAS | 6ea24ff | 2020-08-11 09:21:24 +0200 | [diff] [blame] | 27 | /* Read in Codec location (BAR + 0xe)[2..0] */ |
Elyes HAOUAS | 11178bd | 2020-08-03 15:34:46 +0200 | [diff] [blame] | 28 | reg8 = read8(base + HDA_STATESTS_REG); |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 29 | reg8 &= 0x0f; |
| 30 | if (!reg8) |
| 31 | goto no_codec; |
| 32 | |
| 33 | return reg8; |
| 34 | |
| 35 | no_codec: |
Angel Pons | 2e0053b | 2020-12-05 19:06:55 +0100 | [diff] [blame] | 36 | /* Codec not found, put HDA back in reset */ |
| 37 | azalia_enter_reset(base); |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 38 | printk(BIOS_DEBUG, "Azalia: No codec!\n"); |
| 39 | return 0; |
| 40 | } |
| 41 | |
Elyes HAOUAS | 6ea24ff | 2020-08-11 09:21:24 +0200 | [diff] [blame] | 42 | /* |
| 43 | * Wait 50usec for the codec to indicate it is ready. |
| 44 | * No response would imply that the codec is non-operative. |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 45 | */ |
| 46 | |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 47 | static int wait_for_ready(u8 *base) |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 48 | { |
Stefan Reinauer | 15ba2bc | 2012-11-14 12:25:15 -0800 | [diff] [blame] | 49 | /* Use a 1msec timeout */ |
Stefan Reinauer | 15ba2bc | 2012-11-14 12:25:15 -0800 | [diff] [blame] | 50 | int timeout = 1000; |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 51 | |
Elyes HAOUAS | ba28e8d | 2016-08-31 19:22:16 +0200 | [diff] [blame] | 52 | while (timeout--) { |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 53 | u32 reg32 = read32(base + HDA_ICII_REG); |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 54 | if (!(reg32 & HDA_ICII_BUSY)) |
| 55 | return 0; |
| 56 | udelay(1); |
| 57 | } |
| 58 | |
| 59 | return -1; |
| 60 | } |
| 61 | |
Elyes HAOUAS | 6ea24ff | 2020-08-11 09:21:24 +0200 | [diff] [blame] | 62 | /* |
| 63 | * Wait 50usec for the codec to indicate that it accepted the previous command. |
| 64 | * No response would imply that the code is non-operative. |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 65 | */ |
| 66 | |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 67 | static int wait_for_valid(u8 *base) |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 68 | { |
| 69 | u32 reg32; |
Elyes HAOUAS | 6ea24ff | 2020-08-11 09:21:24 +0200 | [diff] [blame] | 70 | /* Use a 1msec timeout */ |
| 71 | int timeout = 1000; |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 72 | |
| 73 | /* Send the verb to the codec */ |
| 74 | reg32 = read32(base + HDA_ICII_REG); |
| 75 | reg32 |= HDA_ICII_BUSY | HDA_ICII_VALID; |
| 76 | write32(base + HDA_ICII_REG, reg32); |
| 77 | |
Elyes HAOUAS | ba28e8d | 2016-08-31 19:22:16 +0200 | [diff] [blame] | 78 | while (timeout--) { |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 79 | reg32 = read32(base + HDA_ICII_REG); |
Elyes HAOUAS | 6ea24ff | 2020-08-11 09:21:24 +0200 | [diff] [blame] | 80 | if ((reg32 & (HDA_ICII_VALID | HDA_ICII_BUSY)) == HDA_ICII_VALID) |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 81 | return 0; |
| 82 | udelay(1); |
| 83 | } |
| 84 | |
| 85 | return -1; |
| 86 | } |
| 87 | |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 88 | static void codec_init(struct device *dev, u8 *base, int addr) |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 89 | { |
| 90 | u32 reg32; |
| 91 | const u32 *verb; |
| 92 | u32 verb_size; |
| 93 | int i; |
| 94 | |
| 95 | printk(BIOS_DEBUG, "Azalia: Initializing codec #%d\n", addr); |
| 96 | |
| 97 | /* 1 */ |
Angel Pons | 554713e | 2020-10-24 23:23:07 +0200 | [diff] [blame] | 98 | if (wait_for_ready(base) < 0) { |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 99 | printk(BIOS_DEBUG, " codec not ready.\n"); |
| 100 | return; |
| 101 | } |
| 102 | |
| 103 | reg32 = (addr << 28) | 0x000f0000; |
Elyes HAOUAS | 11178bd | 2020-08-03 15:34:46 +0200 | [diff] [blame] | 104 | write32(base + HDA_IC_REG, reg32); |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 105 | |
Angel Pons | 554713e | 2020-10-24 23:23:07 +0200 | [diff] [blame] | 106 | if (wait_for_valid(base) < 0) { |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 107 | printk(BIOS_DEBUG, " codec not valid.\n"); |
| 108 | return; |
| 109 | } |
| 110 | |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 111 | /* 2 */ |
Elyes HAOUAS | 6ea24ff | 2020-08-11 09:21:24 +0200 | [diff] [blame] | 112 | reg32 = read32(base + HDA_IR_REG); |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 113 | printk(BIOS_DEBUG, "Azalia: codec viddid: %08x\n", reg32); |
Angel Pons | d425ddd | 2020-12-05 18:22:58 +0100 | [diff] [blame] | 114 | verb_size = azalia_find_verb(cim_verb_data, cim_verb_data_size, reg32, &verb); |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 115 | |
| 116 | if (!verb_size) { |
| 117 | printk(BIOS_DEBUG, "Azalia: No verb!\n"); |
| 118 | return; |
| 119 | } |
| 120 | printk(BIOS_DEBUG, "Azalia: verb_size: %d\n", verb_size); |
| 121 | |
| 122 | /* 3 */ |
| 123 | for (i = 0; i < verb_size; i++) { |
Angel Pons | 554713e | 2020-10-24 23:23:07 +0200 | [diff] [blame] | 124 | if (wait_for_ready(base) < 0) |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 125 | return; |
| 126 | |
Elyes HAOUAS | 11178bd | 2020-08-03 15:34:46 +0200 | [diff] [blame] | 127 | write32(base + HDA_IC_REG, verb[i]); |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 128 | |
Angel Pons | 554713e | 2020-10-24 23:23:07 +0200 | [diff] [blame] | 129 | if (wait_for_valid(base) < 0) |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 130 | return; |
| 131 | } |
| 132 | printk(BIOS_DEBUG, "Azalia: verb loaded.\n"); |
| 133 | } |
| 134 | |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 135 | static void codecs_init(struct device *dev, u8 *base, u32 codec_mask) |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 136 | { |
| 137 | int i; |
| 138 | for (i = 3; i >= 0; i--) { |
| 139 | if (codec_mask & (1 << i)) |
| 140 | codec_init(dev, base, i); |
| 141 | } |
Dylan Reid | b98d078 | 2012-04-27 11:37:33 -0700 | [diff] [blame] | 142 | |
| 143 | for (i = 0; i < pc_beep_verbs_size; i++) { |
Angel Pons | 554713e | 2020-10-24 23:23:07 +0200 | [diff] [blame] | 144 | if (wait_for_ready(base) < 0) |
Dylan Reid | b98d078 | 2012-04-27 11:37:33 -0700 | [diff] [blame] | 145 | return; |
| 146 | |
Elyes HAOUAS | 11178bd | 2020-08-03 15:34:46 +0200 | [diff] [blame] | 147 | write32(base + HDA_IC_REG, pc_beep_verbs[i]); |
Dylan Reid | b98d078 | 2012-04-27 11:37:33 -0700 | [diff] [blame] | 148 | |
Angel Pons | 554713e | 2020-10-24 23:23:07 +0200 | [diff] [blame] | 149 | if (wait_for_valid(base) < 0) |
Dylan Reid | b98d078 | 2012-04-27 11:37:33 -0700 | [diff] [blame] | 150 | return; |
| 151 | } |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | static void azalia_init(struct device *dev) |
| 155 | { |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 156 | u8 *base; |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 157 | struct resource *res; |
| 158 | u32 codec_mask; |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 159 | u32 reg32; |
| 160 | |
Angel Pons | f32ae10 | 2021-11-03 13:07:14 +0100 | [diff] [blame^] | 161 | res = probe_resource(dev, PCI_BASE_ADDRESS_0); |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 162 | if (!res) |
| 163 | return; |
| 164 | |
Martin Roth | 26f97f9 | 2021-10-01 14:53:22 -0600 | [diff] [blame] | 165 | // NOTE this will break as soon as the Azalia gets a bar above 4G. |
Elyes HAOUAS | 6ea24ff | 2020-08-11 09:21:24 +0200 | [diff] [blame] | 166 | // Is there anything we can do about it? |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 167 | base = res2mmio(res, 0, 0); |
Patrick Rudolph | b50b6a5 | 2020-08-20 16:50:01 +0200 | [diff] [blame] | 168 | printk(BIOS_DEBUG, "Azalia: base = %p\n", base); |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 169 | |
Patrick Rudolph | 4f8b108 | 2019-07-14 11:54:58 +0200 | [diff] [blame] | 170 | if (RCBA32(CIR31) & (1 << 31)) { |
Kyösti Mälkki | fd98c65 | 2013-07-26 08:50:53 +0300 | [diff] [blame] | 171 | reg32 = pci_read_config32(dev, 0x120); |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 172 | reg32 &= 0xf8ffff01; |
Stefan Reinauer | 15ba2bc | 2012-11-14 12:25:15 -0800 | [diff] [blame] | 173 | reg32 |= (1 << 24); // 2 << 24 for server |
Patrick Rudolph | 4f8b108 | 2019-07-14 11:54:58 +0200 | [diff] [blame] | 174 | reg32 |= RCBA32(CIR31) & 0xfe; |
Kyösti Mälkki | fd98c65 | 2013-07-26 08:50:53 +0300 | [diff] [blame] | 175 | pci_write_config32(dev, 0x120, reg32); |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 176 | |
Angel Pons | c803f65 | 2020-06-07 22:09:01 +0200 | [diff] [blame] | 177 | pci_or_config16(dev, 0x78, 1 << 11); |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 178 | } else |
| 179 | printk(BIOS_DEBUG, "Azalia: V1CTL disabled.\n"); |
| 180 | |
Angel Pons | c803f65 | 2020-06-07 22:09:01 +0200 | [diff] [blame] | 181 | pci_and_config32(dev, 0x114, ~0xfe); |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 182 | |
| 183 | // Set VCi enable bit |
Angel Pons | c803f65 | 2020-06-07 22:09:01 +0200 | [diff] [blame] | 184 | pci_or_config32(dev, 0x120, 1 << 31); |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 185 | |
| 186 | // Enable HDMI codec: |
Angel Pons | c803f65 | 2020-06-07 22:09:01 +0200 | [diff] [blame] | 187 | pci_or_config32(dev, 0xc4, 1 << 1); |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 188 | |
Angel Pons | c803f65 | 2020-06-07 22:09:01 +0200 | [diff] [blame] | 189 | pci_or_config8(dev, 0x43, 1 << 6); |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 190 | |
| 191 | /* Additional programming steps */ |
Angel Pons | c803f65 | 2020-06-07 22:09:01 +0200 | [diff] [blame] | 192 | pci_or_config32(dev, 0xc4, 1 << 13); |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 193 | |
Angel Pons | c803f65 | 2020-06-07 22:09:01 +0200 | [diff] [blame] | 194 | pci_or_config32(dev, 0xc4, 1 << 10); |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 195 | |
Angel Pons | c803f65 | 2020-06-07 22:09:01 +0200 | [diff] [blame] | 196 | pci_and_config32(dev, 0xd0, ~(1 << 31)); |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 197 | |
Stefan Reinauer | 15ba2bc | 2012-11-14 12:25:15 -0800 | [diff] [blame] | 198 | if (dev->device == 0x1e20) { |
| 199 | /* Additional step on Panther Point */ |
Angel Pons | c803f65 | 2020-06-07 22:09:01 +0200 | [diff] [blame] | 200 | pci_or_config32(dev, 0xc4, 1 << 17); |
Stefan Reinauer | 15ba2bc | 2012-11-14 12:25:15 -0800 | [diff] [blame] | 201 | } |
| 202 | |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 203 | /* Set Bus Master */ |
Elyes HAOUAS | 729c069 | 2020-04-28 19:50:44 +0200 | [diff] [blame] | 204 | pci_or_config16(dev, PCI_COMMAND, PCI_COMMAND_MASTER); |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 205 | |
| 206 | pci_write_config8(dev, 0x3c, 0x0a); // unused? |
| 207 | |
| 208 | /* Codec Initialization Programming Sequence */ |
Stefan Reinauer | 15ba2bc | 2012-11-14 12:25:15 -0800 | [diff] [blame] | 209 | |
| 210 | /* Take controller out of reset */ |
Elyes HAOUAS | 11178bd | 2020-08-03 15:34:46 +0200 | [diff] [blame] | 211 | reg32 = read32(base + HDA_GCTL_REG); |
| 212 | reg32 |= HDA_GCTL_CRST; |
| 213 | write32(base + HDA_GCTL_REG, reg32); |
Stefan Reinauer | 15ba2bc | 2012-11-14 12:25:15 -0800 | [diff] [blame] | 214 | /* Wait 1ms */ |
| 215 | udelay(1000); |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 216 | |
Angel Pons | c803f65 | 2020-06-07 22:09:01 +0200 | [diff] [blame] | 217 | // Select Azalia mode. This needs to be controlled via devicetree.cb |
| 218 | pci_or_config8(dev, 0x40, 1); // Audio Control |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 219 | |
Angel Pons | c803f65 | 2020-06-07 22:09:01 +0200 | [diff] [blame] | 220 | // Docking not supported |
| 221 | pci_and_config8(dev, 0x4d, (u8)~(1 << 7)); // Docking Status |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 222 | |
| 223 | codec_mask = codec_detect(base); |
| 224 | |
| 225 | if (codec_mask) { |
| 226 | printk(BIOS_DEBUG, "Azalia: codec_mask = %02x\n", codec_mask); |
| 227 | codecs_init(dev, base, codec_mask); |
| 228 | } |
| 229 | |
| 230 | /* Enable dynamic clock gating */ |
Angel Pons | c803f65 | 2020-06-07 22:09:01 +0200 | [diff] [blame] | 231 | pci_update_config8(dev, 0x43, ~0x07, (1 << 2) | (1 << 0)); |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 232 | } |
| 233 | |
Aaron Durbin | aa090cb | 2017-09-13 16:01:52 -0600 | [diff] [blame] | 234 | static const char *azalia_acpi_name(const struct device *dev) |
Patrick Rudolph | 604f698 | 2017-06-07 09:46:52 +0200 | [diff] [blame] | 235 | { |
| 236 | return "HDEF"; |
| 237 | } |
| 238 | |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 239 | static struct device_operations azalia_ops = { |
| 240 | .read_resources = pci_dev_read_resources, |
| 241 | .set_resources = pci_dev_set_resources, |
| 242 | .enable_resources = pci_dev_enable_resources, |
| 243 | .init = azalia_init, |
Angel Pons | 1fc0edd | 2020-05-31 00:03:28 +0200 | [diff] [blame] | 244 | .ops_pci = &pci_dev_ops_pci, |
Patrick Rudolph | 604f698 | 2017-06-07 09:46:52 +0200 | [diff] [blame] | 245 | .acpi_name = azalia_acpi_name, |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 246 | }; |
| 247 | |
Stefan Reinauer | 9a380ab | 2012-06-22 13:16:11 -0700 | [diff] [blame] | 248 | static const unsigned short pci_device_ids[] = { 0x1c20, 0x1e20, 0 }; |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 249 | |
Stefan Reinauer | 9a380ab | 2012-06-22 13:16:11 -0700 | [diff] [blame] | 250 | static const struct pci_driver pch_azalia __pci_driver = { |
| 251 | .ops = &azalia_ops, |
| 252 | .vendor = PCI_VENDOR_ID_INTEL, |
| 253 | .devices = pci_device_ids, |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 254 | }; |