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