Angel Pons | 182dbde | 2020-04-02 23:49:05 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Arthur Heymans | 7b9c139 | 2017-04-09 20:40:39 +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> |
Arthur Heymans | 7b9c139 | 2017-04-09 20:40:39 +0200 | [diff] [blame] | 9 | #include <delay.h> |
| 10 | #include <device/azalia_device.h> |
Kyösti Mälkki | 12b121c | 2019-08-18 16:33:39 +0300 | [diff] [blame] | 11 | #include "chip.h" |
Arthur Heymans | 349e085 | 2017-04-09 20:48:37 +0200 | [diff] [blame] | 12 | #include "i82801jx.h" |
Arthur Heymans | 7b9c139 | 2017-04-09 20:40:39 +0200 | [diff] [blame] | 13 | |
Arthur Heymans | 7b9c139 | 2017-04-09 20:40:39 +0200 | [diff] [blame] | 14 | static int codec_detect(u8 *base) |
| 15 | { |
| 16 | u32 reg32; |
| 17 | |
Elyes HAOUAS | 6ea24ff | 2020-08-11 09:21:24 +0200 | [diff] [blame] | 18 | /* Set Bit 0 to 0 to enter reset state (BAR + 0x8)[0] */ |
Angel Pons | 61dd836 | 2020-12-05 18:02:32 +0100 | [diff] [blame] | 19 | if (azalia_set_bits(base + HDA_GCTL_REG, HDA_GCTL_CRST, 0) < 0) |
Arthur Heymans | 7b9c139 | 2017-04-09 20:40:39 +0200 | [diff] [blame] | 20 | goto no_codec; |
| 21 | |
Angel Pons | 7f839f6 | 2020-12-05 19:02:14 +0100 | [diff] [blame] | 22 | if (azalia_exit_reset(base) < 0) |
Arthur Heymans | 7b9c139 | 2017-04-09 20:40:39 +0200 | [diff] [blame] | 23 | goto no_codec; |
| 24 | |
Elyes HAOUAS | 6ea24ff | 2020-08-11 09:21:24 +0200 | [diff] [blame] | 25 | /* Read in Codec location (BAR + 0xe)[2..0] */ |
Elyes HAOUAS | 388c88b | 2020-08-03 15:36:20 +0200 | [diff] [blame] | 26 | reg32 = read32(base + HDA_STATESTS_REG); |
Arthur Heymans | 7b9c139 | 2017-04-09 20:40:39 +0200 | [diff] [blame] | 27 | reg32 &= 0x0f; |
| 28 | if (!reg32) |
| 29 | goto no_codec; |
| 30 | |
| 31 | return reg32; |
| 32 | |
| 33 | no_codec: |
| 34 | /* Codec Not found */ |
| 35 | /* Put HDA back in reset (BAR + 0x8) [0] */ |
Angel Pons | 61dd836 | 2020-12-05 18:02:32 +0100 | [diff] [blame] | 36 | azalia_set_bits(base + HDA_GCTL_REG, 1, 0); |
Arthur Heymans | 7b9c139 | 2017-04-09 20:40:39 +0200 | [diff] [blame] | 37 | printk(BIOS_DEBUG, "Azalia: No codec!\n"); |
| 38 | return 0; |
| 39 | } |
| 40 | |
Elyes HAOUAS | 6ea24ff | 2020-08-11 09:21:24 +0200 | [diff] [blame] | 41 | /* |
| 42 | * Wait 50usec for the codec to indicate it is ready. |
| 43 | * No response would imply that the codec is non-operative. |
Arthur Heymans | 7b9c139 | 2017-04-09 20:40:39 +0200 | [diff] [blame] | 44 | */ |
| 45 | |
| 46 | static int wait_for_ready(u8 *base) |
| 47 | { |
Angel Pons | 7a2864b | 2020-06-21 13:29:28 +0200 | [diff] [blame] | 48 | /* Use a 50 usec timeout - the Linux kernel uses the same duration */ |
Arthur Heymans | 7b9c139 | 2017-04-09 20:40:39 +0200 | [diff] [blame] | 49 | int timeout = 50; |
| 50 | |
| 51 | while (timeout--) { |
| 52 | u32 reg32 = read32(base + HDA_ICII_REG); |
| 53 | if (!(reg32 & HDA_ICII_BUSY)) |
| 54 | return 0; |
| 55 | udelay(1); |
| 56 | } |
| 57 | |
| 58 | return -1; |
| 59 | } |
| 60 | |
Elyes HAOUAS | 6ea24ff | 2020-08-11 09:21:24 +0200 | [diff] [blame] | 61 | /* |
| 62 | * Wait 50usec for the codec to indicate that it accepted the previous command. |
| 63 | * No response would imply that the code is non-operative. |
Arthur Heymans | 7b9c139 | 2017-04-09 20:40:39 +0200 | [diff] [blame] | 64 | */ |
| 65 | |
| 66 | static int wait_for_valid(u8 *base) |
| 67 | { |
| 68 | u32 reg32; |
Elyes HAOUAS | 6ea24ff | 2020-08-11 09:21:24 +0200 | [diff] [blame] | 69 | /* Use a 50 usec timeout - the Linux kernel uses the same duration */ |
| 70 | int timeout = 50; |
Arthur Heymans | 7b9c139 | 2017-04-09 20:40:39 +0200 | [diff] [blame] | 71 | |
| 72 | /* Send the verb to the codec */ |
Elyes HAOUAS | 388c88b | 2020-08-03 15:36:20 +0200 | [diff] [blame] | 73 | reg32 = read32(base + HDA_ICII_REG); |
| 74 | reg32 |= HDA_ICII_BUSY | HDA_ICII_VALID; |
| 75 | write32(base + HDA_ICII_REG, reg32); |
Arthur Heymans | 7b9c139 | 2017-04-09 20:40:39 +0200 | [diff] [blame] | 76 | |
Arthur Heymans | 7b9c139 | 2017-04-09 20:40:39 +0200 | [diff] [blame] | 77 | while (timeout--) { |
| 78 | reg32 = read32(base + HDA_ICII_REG); |
Angel Pons | 7a2864b | 2020-06-21 13:29:28 +0200 | [diff] [blame] | 79 | if ((reg32 & (HDA_ICII_VALID | HDA_ICII_BUSY)) == HDA_ICII_VALID) |
Arthur Heymans | 7b9c139 | 2017-04-09 20:40:39 +0200 | [diff] [blame] | 80 | return 0; |
| 81 | udelay(1); |
| 82 | } |
| 83 | |
| 84 | return -1; |
| 85 | } |
| 86 | |
| 87 | static void codec_init(struct device *dev, u8 *base, int addr) |
| 88 | { |
| 89 | u32 reg32; |
| 90 | const u32 *verb; |
| 91 | u32 verb_size; |
| 92 | int i; |
| 93 | |
Angel Pons | aaa8ab7 | 2020-06-21 15:33:24 +0200 | [diff] [blame] | 94 | printk(BIOS_DEBUG, "Azalia: Initializing codec #%d\n", addr); |
Arthur Heymans | 7b9c139 | 2017-04-09 20:40:39 +0200 | [diff] [blame] | 95 | |
| 96 | /* 1 */ |
Angel Pons | 554713e | 2020-10-24 23:23:07 +0200 | [diff] [blame] | 97 | if (wait_for_ready(base) < 0) { |
Elyes HAOUAS | 6ea24ff | 2020-08-11 09:21:24 +0200 | [diff] [blame] | 98 | printk(BIOS_DEBUG, " codec not ready.\n"); |
Arthur Heymans | 7b9c139 | 2017-04-09 20:40:39 +0200 | [diff] [blame] | 99 | return; |
Elyes HAOUAS | 6ea24ff | 2020-08-11 09:21:24 +0200 | [diff] [blame] | 100 | } |
Arthur Heymans | 7b9c139 | 2017-04-09 20:40:39 +0200 | [diff] [blame] | 101 | |
| 102 | reg32 = (addr << 28) | 0x000f0000; |
Elyes HAOUAS | 388c88b | 2020-08-03 15:36:20 +0200 | [diff] [blame] | 103 | write32(base + HDA_IC_REG, reg32); |
Arthur Heymans | 7b9c139 | 2017-04-09 20:40:39 +0200 | [diff] [blame] | 104 | |
Angel Pons | 554713e | 2020-10-24 23:23:07 +0200 | [diff] [blame] | 105 | if (wait_for_valid(base) < 0) { |
Elyes HAOUAS | 6ea24ff | 2020-08-11 09:21:24 +0200 | [diff] [blame] | 106 | printk(BIOS_DEBUG, " codec not valid.\n"); |
Arthur Heymans | 7b9c139 | 2017-04-09 20:40:39 +0200 | [diff] [blame] | 107 | return; |
Elyes HAOUAS | 6ea24ff | 2020-08-11 09:21:24 +0200 | [diff] [blame] | 108 | } |
Arthur Heymans | 7b9c139 | 2017-04-09 20:40:39 +0200 | [diff] [blame] | 109 | |
| 110 | /* 2 */ |
Elyes HAOUAS | 6ea24ff | 2020-08-11 09:21:24 +0200 | [diff] [blame] | 111 | reg32 = read32(base + HDA_IR_REG); |
Arthur Heymans | 7b9c139 | 2017-04-09 20:40:39 +0200 | [diff] [blame] | 112 | printk(BIOS_DEBUG, "Azalia: codec viddid: %08x\n", reg32); |
Angel Pons | d425ddd | 2020-12-05 18:22:58 +0100 | [diff] [blame] | 113 | verb_size = azalia_find_verb(cim_verb_data, cim_verb_data_size, reg32, &verb); |
Arthur Heymans | 7b9c139 | 2017-04-09 20:40:39 +0200 | [diff] [blame] | 114 | |
| 115 | if (!verb_size) { |
| 116 | printk(BIOS_DEBUG, "Azalia: No verb!\n"); |
| 117 | return; |
| 118 | } |
| 119 | printk(BIOS_DEBUG, "Azalia: verb_size: %d\n", verb_size); |
| 120 | |
| 121 | /* 3 */ |
| 122 | for (i = 0; i < verb_size; i++) { |
Angel Pons | 554713e | 2020-10-24 23:23:07 +0200 | [diff] [blame] | 123 | if (wait_for_ready(base) < 0) |
Arthur Heymans | 7b9c139 | 2017-04-09 20:40:39 +0200 | [diff] [blame] | 124 | return; |
| 125 | |
Elyes HAOUAS | 388c88b | 2020-08-03 15:36:20 +0200 | [diff] [blame] | 126 | write32(base + HDA_IC_REG, verb[i]); |
Arthur Heymans | 7b9c139 | 2017-04-09 20:40:39 +0200 | [diff] [blame] | 127 | |
Angel Pons | 554713e | 2020-10-24 23:23:07 +0200 | [diff] [blame] | 128 | if (wait_for_valid(base) < 0) |
Arthur Heymans | 7b9c139 | 2017-04-09 20:40:39 +0200 | [diff] [blame] | 129 | return; |
| 130 | } |
| 131 | printk(BIOS_DEBUG, "Azalia: verb loaded.\n"); |
| 132 | } |
| 133 | |
| 134 | static void codecs_init(struct device *dev, u8 *base, u32 codec_mask) |
| 135 | { |
| 136 | int i; |
Elyes HAOUAS | 6ea24ff | 2020-08-11 09:21:24 +0200 | [diff] [blame] | 137 | |
Arthur Heymans | 7b9c139 | 2017-04-09 20:40:39 +0200 | [diff] [blame] | 138 | for (i = 2; i >= 0; i--) { |
| 139 | if (codec_mask & (1 << i)) |
| 140 | codec_init(dev, base, i); |
| 141 | } |
| 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) |
Arthur Heymans | 7b9c139 | 2017-04-09 20:40:39 +0200 | [diff] [blame] | 145 | return; |
| 146 | |
Elyes HAOUAS | 388c88b | 2020-08-03 15:36:20 +0200 | [diff] [blame] | 147 | write32(base + HDA_IC_REG, pc_beep_verbs[i]); |
Arthur Heymans | 7b9c139 | 2017-04-09 20:40:39 +0200 | [diff] [blame] | 148 | |
Angel Pons | 554713e | 2020-10-24 23:23:07 +0200 | [diff] [blame] | 149 | if (wait_for_valid(base) < 0) |
Arthur Heymans | 7b9c139 | 2017-04-09 20:40:39 +0200 | [diff] [blame] | 150 | return; |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | static void azalia_init(struct device *dev) |
| 155 | { |
| 156 | u8 *base; |
| 157 | struct resource *res; |
| 158 | u32 codec_mask; |
Arthur Heymans | 7b9c139 | 2017-04-09 20:40:39 +0200 | [diff] [blame] | 159 | |
| 160 | // ESD |
Angel Pons | 2048cb4 | 2020-06-08 02:09:33 +0200 | [diff] [blame] | 161 | pci_update_config32(dev, 0x134, ~0x00ff0000, 2 << 16); |
Arthur Heymans | 7b9c139 | 2017-04-09 20:40:39 +0200 | [diff] [blame] | 162 | |
| 163 | // Link1 description |
Angel Pons | 2048cb4 | 2020-06-08 02:09:33 +0200 | [diff] [blame] | 164 | pci_update_config32(dev, 0x140, ~0x00ff0000, 2 << 16); |
Arthur Heymans | 7b9c139 | 2017-04-09 20:40:39 +0200 | [diff] [blame] | 165 | |
| 166 | // Port VC0 Resource Control Register |
Angel Pons | 2048cb4 | 2020-06-08 02:09:33 +0200 | [diff] [blame] | 167 | pci_update_config32(dev, 0x114, ~0x000000ff, 1); |
Arthur Heymans | 7b9c139 | 2017-04-09 20:40:39 +0200 | [diff] [blame] | 168 | |
| 169 | // VCi traffic class |
Angel Pons | 7a2864b | 2020-06-21 13:29:28 +0200 | [diff] [blame] | 170 | pci_or_config8(dev, 0x44, 7 << 0); // TC7 |
Arthur Heymans | 7b9c139 | 2017-04-09 20:40:39 +0200 | [diff] [blame] | 171 | |
| 172 | // VCi Resource Control |
Angel Pons | 2048cb4 | 2020-06-08 02:09:33 +0200 | [diff] [blame] | 173 | pci_or_config32(dev, 0x120, (1 << 31) | (1 << 24) | (0x80 << 0)); /* VCi ID and map */ |
Arthur Heymans | 7b9c139 | 2017-04-09 20:40:39 +0200 | [diff] [blame] | 174 | |
| 175 | /* Set Bus Master */ |
Elyes HAOUAS | ca4ff25 | 2020-04-28 10:29:11 +0200 | [diff] [blame] | 176 | pci_or_config16(dev, PCI_COMMAND, PCI_COMMAND_MASTER); |
Arthur Heymans | 7b9c139 | 2017-04-09 20:40:39 +0200 | [diff] [blame] | 177 | |
Angel Pons | 2048cb4 | 2020-06-08 02:09:33 +0200 | [diff] [blame] | 178 | // Docking not supported |
| 179 | pci_and_config8(dev, 0x4d, (u8)~(1 << 7)); // Docking Status |
Arthur Heymans | 7b9c139 | 2017-04-09 20:40:39 +0200 | [diff] [blame] | 180 | |
| 181 | /* Lock some R/WO bits by writing their current value. */ |
Angel Pons | 2048cb4 | 2020-06-08 02:09:33 +0200 | [diff] [blame] | 182 | pci_update_config32(dev, 0x74, ~0, 0); |
Arthur Heymans | 7b9c139 | 2017-04-09 20:40:39 +0200 | [diff] [blame] | 183 | |
Elyes HAOUAS | 6ea24ff | 2020-08-11 09:21:24 +0200 | [diff] [blame] | 184 | res = find_resource(dev, PCI_BASE_ADDRESS_0); |
Arthur Heymans | 7b9c139 | 2017-04-09 20:40:39 +0200 | [diff] [blame] | 185 | if (!res) |
| 186 | return; |
| 187 | |
Elyes HAOUAS | 6ea24ff | 2020-08-11 09:21:24 +0200 | [diff] [blame] | 188 | // NOTE this will break as soon as the Azalia get's a bar above 4G. |
| 189 | // Is there anything we can do about it? |
Arthur Heymans | 7b9c139 | 2017-04-09 20:40:39 +0200 | [diff] [blame] | 190 | base = res2mmio(res, 0, 0); |
Angel Pons | 7a2864b | 2020-06-21 13:29:28 +0200 | [diff] [blame] | 191 | printk(BIOS_DEBUG, "Azalia: base = %p\n", base); |
Arthur Heymans | 7b9c139 | 2017-04-09 20:40:39 +0200 | [diff] [blame] | 192 | codec_mask = codec_detect(base); |
| 193 | |
| 194 | if (codec_mask) { |
| 195 | printk(BIOS_DEBUG, "Azalia: codec_mask = %02x\n", codec_mask); |
| 196 | codecs_init(dev, base, codec_mask); |
| 197 | } |
| 198 | } |
| 199 | |
Arthur Heymans | 7b9c139 | 2017-04-09 20:40:39 +0200 | [diff] [blame] | 200 | static struct device_operations azalia_ops = { |
| 201 | .read_resources = pci_dev_read_resources, |
| 202 | .set_resources = pci_dev_set_resources, |
| 203 | .enable_resources = pci_dev_enable_resources, |
| 204 | .init = azalia_init, |
Angel Pons | 1fc0edd | 2020-05-31 00:03:28 +0200 | [diff] [blame] | 205 | .ops_pci = &pci_dev_ops_pci, |
Arthur Heymans | 7b9c139 | 2017-04-09 20:40:39 +0200 | [diff] [blame] | 206 | }; |
| 207 | |
Arthur Heymans | 349e085 | 2017-04-09 20:48:37 +0200 | [diff] [blame] | 208 | static const unsigned short pci_device_ids[] = { |
| 209 | 0x3a3e, |
| 210 | 0x3a6e, |
| 211 | 0 |
| 212 | }; |
| 213 | |
| 214 | static const struct pci_driver i82801jx_azalia __pci_driver = { |
Arthur Heymans | 7b9c139 | 2017-04-09 20:40:39 +0200 | [diff] [blame] | 215 | .ops = &azalia_ops, |
| 216 | .vendor = PCI_VENDOR_ID_INTEL, |
Arthur Heymans | 349e085 | 2017-04-09 20:48:37 +0200 | [diff] [blame] | 217 | .devices = pci_device_ids, |
Arthur Heymans | 7b9c139 | 2017-04-09 20:40:39 +0200 | [diff] [blame] | 218 | }; |