Angel Pons | 182dbde | 2020-04-02 23:49:05 +0200 | [diff] [blame^] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
| 2 | /* This file is part of the coreboot project. */ |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 3 | |
| 4 | #include <console/console.h> |
| 5 | #include <device/device.h> |
| 6 | #include <device/pci.h> |
| 7 | #include <device/pci_ids.h> |
Stefan Reinauer | 54309d6 | 2009-01-20 22:53:10 +0000 | [diff] [blame] | 8 | #include <arch/io.h> |
Kyösti Mälkki | f1b58b7 | 2019-03-01 13:43:02 +0200 | [diff] [blame] | 9 | #include <device/pci_ops.h> |
Stefan Reinauer | 54309d6 | 2009-01-20 22:53:10 +0000 | [diff] [blame] | 10 | #include <delay.h> |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 11 | #include "i82801gx.h" |
| 12 | |
Stefan Reinauer | 54309d6 | 2009-01-20 22:53:10 +0000 | [diff] [blame] | 13 | #define NAMBAR 0x10 |
| 14 | #define MASTER_VOL 0x02 |
| 15 | #define PAGING 0x24 |
| 16 | #define EXT_AUDIO 0x28 |
| 17 | #define FUNC_SEL 0x66 |
| 18 | #define INFO_IO 0x68 |
| 19 | #define CONNECTOR 0x6a |
| 20 | #define VENDOR_ID1 0x7c |
| 21 | #define VENDOR_ID2 0x7e |
| 22 | #define SEC_VENDOR_ID1 0xfc |
| 23 | #define SEC_VENDOR_ID2 0xfe |
| 24 | |
| 25 | #define NABMBAR 0x14 |
| 26 | #define GLOB_CNT 0x2c |
| 27 | #define GLOB_STA 0x30 |
| 28 | #define CAS 0x34 |
| 29 | |
| 30 | #define MMBAR 0x10 |
| 31 | #define EXT_MODEM_ID1 0x3c |
| 32 | #define EXT_MODEM_ID2 0xbc |
| 33 | |
| 34 | #define MBAR 0x14 |
| 35 | #define SEC_CODEC 0x40 |
| 36 | |
| 37 | |
| 38 | /* FIXME. This table is probably mainboard specific */ |
| 39 | static u16 ac97_function[16*2][4] = { |
| 40 | { (1 << 5), (2 << 11), (1 << 10), (3 << 13) }, |
| 41 | { (1 << 5), (2 << 11), (1 << 10), (3 << 13) }, |
| 42 | { (1 << 5), (2 << 11), (1 << 10), (3 << 13) }, |
| 43 | { (1 << 5), (2 << 11), (1 << 10), (3 << 13) }, |
| 44 | { (1 << 5), (2 << 11), (1 << 10), (3 << 13) }, |
| 45 | { (1 << 5), (2 << 11), (1 << 10), (3 << 13) }, |
| 46 | { (1 << 5), (2 << 11), (1 << 10), (3 << 13) }, |
| 47 | { (1 << 5), (2 << 11), (1 << 10), (3 << 13) }, |
| 48 | { (1 << 5), (2 << 11), (1 << 10), (3 << 13) }, |
| 49 | { (1 << 5), (2 << 11), (1 << 10), (3 << 13) }, |
| 50 | { (1 << 5), (2 << 11), (1 << 10), (3 << 13) }, |
| 51 | { (1 << 5), (2 << 11), (1 << 10), (3 << 13) }, |
| 52 | { (1 << 5), (2 << 11), (1 << 10), (3 << 13) }, |
| 53 | { (1 << 5), (2 << 11), (1 << 10), (3 << 13) }, |
| 54 | { (1 << 5), (2 << 11), (1 << 10), (3 << 13) }, |
| 55 | { (1 << 5), (2 << 11), (1 << 10), (3 << 13) }, |
| 56 | { (1 << 5), (2 << 11), (1 << 10), (3 << 13) }, |
| 57 | { (1 << 5), (2 << 11), (1 << 10), (3 << 13) }, |
| 58 | { (1 << 5), (2 << 11), (1 << 10), (3 << 13) }, |
| 59 | { (1 << 5), (2 << 11), (1 << 10), (3 << 13) }, |
| 60 | { (1 << 5), (2 << 11), (1 << 10), (3 << 13) }, |
| 61 | { (1 << 5), (2 << 11), (1 << 10), (3 << 13) }, |
| 62 | { (1 << 5), (2 << 11), (1 << 10), (3 << 13) }, |
| 63 | { (1 << 5), (2 << 11), (1 << 10), (3 << 13) }, |
| 64 | { (1 << 5), (2 << 11), (1 << 10), (3 << 13) }, |
| 65 | { (1 << 5), (2 << 11), (1 << 10), (3 << 13) }, |
| 66 | { (1 << 5), (2 << 11), (1 << 10), (3 << 13) }, |
| 67 | { (1 << 5), (2 << 11), (1 << 10), (3 << 13) }, |
| 68 | { (1 << 5), (2 << 11), (1 << 10), (3 << 13) }, |
| 69 | { (1 << 5), (2 << 11), (1 << 10), (3 << 13) }, |
| 70 | { (1 << 5), (2 << 11), (1 << 10), (3 << 13) }, |
| 71 | { (1 << 5), (2 << 11), (1 << 10), (3 << 13) } |
| 72 | }; |
| 73 | |
| 74 | static u16 nabmbar; |
| 75 | static u16 nambar; |
| 76 | |
| 77 | static int ac97_semaphore(void) |
| 78 | { |
| 79 | int timeout; |
| 80 | u8 reg8; |
| 81 | |
| 82 | timeout = 0xffff; |
| 83 | do { |
| 84 | reg8 = inb(nabmbar + CAS); |
| 85 | timeout--; |
| 86 | } while ((reg8 & 1) && timeout); |
Arthur Heymans | 3f111b0 | 2017-03-09 12:02:52 +0100 | [diff] [blame] | 87 | if (!timeout) |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 88 | printk(BIOS_DEBUG, "Timeout!\n"); |
Stefan Reinauer | 54309d6 | 2009-01-20 22:53:10 +0000 | [diff] [blame] | 89 | |
| 90 | return (!timeout); |
| 91 | } |
| 92 | |
| 93 | static void init_cnr(void) |
| 94 | { |
| 95 | // TODO |
| 96 | } |
| 97 | |
| 98 | static void program_sigid(struct device *dev, u32 id) |
| 99 | { |
| 100 | pci_write_config32(dev, 0x2c, id); |
| 101 | } |
| 102 | |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 103 | static void ac97_audio_init(struct device *dev) |
| 104 | { |
Stefan Reinauer | 54309d6 | 2009-01-20 22:53:10 +0000 | [diff] [blame] | 105 | u16 reg16; |
| 106 | u32 reg32; |
| 107 | int i; |
| 108 | |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 109 | printk(BIOS_DEBUG, "Initializing AC'97 Audio.\n"); |
Stefan Reinauer | 54309d6 | 2009-01-20 22:53:10 +0000 | [diff] [blame] | 110 | |
| 111 | /* top 16 bits are zero, so don't read them */ |
| 112 | nabmbar = pci_read_config16(dev, NABMBAR) & 0xfffe; |
| 113 | nambar = pci_read_config16(dev, NAMBAR) & 0xfffe; |
| 114 | |
| 115 | reg16 = inw(nabmbar + GLOB_CNT); |
| 116 | reg16 |= (1 << 1); /* Remove AC_RESET# */ |
| 117 | outw(reg16, nabmbar + GLOB_CNT); |
| 118 | |
| 119 | /* Wait 600ms. Ouch. */ |
| 120 | udelay(600 * 1000); |
| 121 | |
| 122 | init_cnr(); |
| 123 | |
| 124 | /* Detect Primary AC'97 Codec */ |
| 125 | reg32 = inl(nabmbar + GLOB_STA); |
| 126 | if ((reg32 & ((1 << 28) | (1 << 9) | (1 << 8))) == 0) { |
| 127 | /* Primary Codec not found */ |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 128 | printk(BIOS_DEBUG, "No primary codec. Disabling AC'97 Audio.\n"); |
Stefan Reinauer | 54309d6 | 2009-01-20 22:53:10 +0000 | [diff] [blame] | 129 | return; |
| 130 | } |
Stefan Reinauer | 109ab31 | 2009-08-12 16:08:05 +0000 | [diff] [blame] | 131 | |
Stefan Reinauer | 54309d6 | 2009-01-20 22:53:10 +0000 | [diff] [blame] | 132 | ac97_semaphore(); |
| 133 | |
| 134 | /* Detect if codec is programmable */ |
| 135 | outw(0x8000, nambar + MASTER_VOL); |
| 136 | ac97_semaphore(); |
| 137 | if (inw(nambar + MASTER_VOL) != 0x8000) { |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 138 | printk(BIOS_DEBUG, "Codec not programmable. Disabling AC'97 Audio.\n"); |
Stefan Reinauer | 54309d6 | 2009-01-20 22:53:10 +0000 | [diff] [blame] | 139 | return; |
| 140 | } |
| 141 | |
| 142 | /* Program Vendor IDs */ |
| 143 | reg32 = inw(nambar + VENDOR_ID1); |
| 144 | reg32 <<= 16; |
| 145 | reg32 |= (u16)inw(nambar + VENDOR_ID2); |
| 146 | |
| 147 | program_sigid(dev, reg32); |
| 148 | |
| 149 | /* Is Codec AC'97 2.3 compliant? */ |
| 150 | reg16 = inw(nambar + EXT_AUDIO); |
| 151 | /* [11:10] = 10b -> AC'97 2.3 */ |
| 152 | if ((reg16 & 0x0c00) != 0x0800) { |
| 153 | /* No 2.3 Codec. We're done */ |
| 154 | return; |
| 155 | } |
| 156 | |
| 157 | /* Select Page 1 */ |
| 158 | reg16 = inw(nambar + PAGING); |
| 159 | reg16 &= 0xfff0; |
| 160 | reg16 |= 0x0001; |
| 161 | outw(reg16, nambar + PAGING); |
| 162 | |
| 163 | for (i = 0x0a * 2; i > 0; i--) { |
| 164 | outw(i, nambar + FUNC_SEL); |
| 165 | |
| 166 | /* Function could not be selected. Next one */ |
| 167 | if (inw(nambar + FUNC_SEL) != i) |
| 168 | continue; |
| 169 | |
| 170 | reg16 = inw(nambar + INFO_IO); |
| 171 | |
| 172 | /* Function Information present? */ |
| 173 | if (!(reg16 & (1 << 0))) |
| 174 | continue; |
| 175 | |
| 176 | /* Function Information valid? */ |
| 177 | if (!(reg16 & (1 << 4))) |
| 178 | continue; |
| 179 | |
| 180 | /* Program Buffer Delay [9:5] */ |
| 181 | reg16 &= 0x03e0; |
| 182 | reg16 |= ac97_function[i][0]; |
| 183 | |
| 184 | /* Program Gain [15:11] */ |
| 185 | reg16 |= ac97_function[i][1]; |
| 186 | |
| 187 | /* Program Inversion [10] */ |
| 188 | reg16 |= ac97_function[i][2]; |
| 189 | |
| 190 | outw(reg16, nambar + INFO_IO); |
| 191 | |
| 192 | /* Program Connector / Jack Location */ |
| 193 | reg16 = inw(nambar + CONNECTOR); |
| 194 | reg16 &= 0x1fff; |
| 195 | reg16 |= ac97_function[i][3]; |
| 196 | outw(reg16, nambar + CONNECTOR); |
| 197 | } |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | static void ac97_modem_init(struct device *dev) |
| 201 | { |
Stefan Reinauer | 54309d6 | 2009-01-20 22:53:10 +0000 | [diff] [blame] | 202 | u16 reg16; |
| 203 | u32 reg32; |
| 204 | u16 mmbar, mbar; |
| 205 | |
| 206 | mmbar = pci_read_config16(dev, MMBAR) & 0xfffe; |
| 207 | mbar = pci_read_config16(dev, MBAR) & 0xfffe; |
| 208 | |
| 209 | reg16 = inw(mmbar + EXT_MODEM_ID1); |
Arthur Heymans | 3f111b0 | 2017-03-09 12:02:52 +0100 | [diff] [blame] | 210 | if ((reg16 & 0xc000) != 0xc000) { |
Stefan Reinauer | 54309d6 | 2009-01-20 22:53:10 +0000 | [diff] [blame] | 211 | if (reg16 & (1 << 0)) { |
| 212 | reg32 = inw(mmbar + VENDOR_ID2); |
| 213 | reg32 <<= 16; |
| 214 | reg32 |= (u16)inw(mmbar + VENDOR_ID1); |
| 215 | program_sigid(dev, reg32); |
| 216 | return; |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | /* Secondary codec? */ |
| 221 | reg16 = inw(mbar + SEC_CODEC); |
| 222 | if ((reg16 & (1 << 9)) == 0) |
| 223 | return; |
| 224 | |
| 225 | reg16 = inw(mmbar + EXT_MODEM_ID2); |
| 226 | if ((reg16 & 0xc000) == 0x4000) { |
| 227 | if (reg16 & (1 << 0)) { |
| 228 | reg32 = inw(mmbar + SEC_VENDOR_ID2); |
| 229 | reg32 <<= 16; |
| 230 | reg32 |= (u16)inw(mmbar + SEC_VENDOR_ID1); |
| 231 | program_sigid(dev, reg32); |
| 232 | return; |
| 233 | } |
| 234 | } |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 235 | } |
| 236 | |
Stefan Reinauer | 573f7d4 | 2009-07-21 21:50:34 +0000 | [diff] [blame] | 237 | static struct pci_operations ac97_pci_ops = { |
Subrata Banik | 4a0f071 | 2019-03-20 14:29:47 +0530 | [diff] [blame] | 238 | .set_subsystem = pci_dev_set_subsystem, |
Stefan Reinauer | 573f7d4 | 2009-07-21 21:50:34 +0000 | [diff] [blame] | 239 | }; |
| 240 | |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 241 | static struct device_operations ac97_audio_ops = { |
| 242 | .read_resources = pci_dev_read_resources, |
| 243 | .set_resources = pci_dev_set_resources, |
| 244 | .enable_resources = pci_dev_enable_resources, |
| 245 | .init = ac97_audio_init, |
| 246 | .scan_bus = 0, |
| 247 | .enable = i82801gx_enable, |
Stefan Reinauer | 573f7d4 | 2009-07-21 21:50:34 +0000 | [diff] [blame] | 248 | .ops_pci = &ac97_pci_ops, |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 249 | }; |
| 250 | |
| 251 | static struct device_operations ac97_modem_ops = { |
| 252 | .read_resources = pci_dev_read_resources, |
| 253 | .set_resources = pci_dev_set_resources, |
| 254 | .enable_resources = pci_dev_enable_resources, |
| 255 | .init = ac97_modem_init, |
| 256 | .scan_bus = 0, |
| 257 | .enable = i82801gx_enable, |
Stefan Reinauer | 573f7d4 | 2009-07-21 21:50:34 +0000 | [diff] [blame] | 258 | .ops_pci = &ac97_pci_ops, |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 259 | }; |
| 260 | |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 261 | /* 82801GB/GR/GDH/GBM/GHM (ICH7/ICH7R/ICH7DH/ICH7-M/ICH7-M DH) */ |
Uwe Hermann | bddc693 | 2008-10-29 13:51:31 +0000 | [diff] [blame] | 262 | /* Note: 82801GU (ICH7-U) doesn't have AC97 audio. */ |
| 263 | static const struct pci_driver i82801gx_ac97_audio __pci_driver = { |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 264 | .ops = &ac97_audio_ops, |
| 265 | .vendor = PCI_VENDOR_ID_INTEL, |
Uwe Hermann | 5d7a1c8 | 2008-10-31 18:41:09 +0000 | [diff] [blame] | 266 | .device = 0x27de, |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 267 | }; |
| 268 | |
Uwe Hermann | bddc693 | 2008-10-29 13:51:31 +0000 | [diff] [blame] | 269 | /* 82801GB/GR/GDH/GBM/GHM (ICH7/ICH7R/ICH7DH/ICH7-M/ICH7-M DH) */ |
| 270 | /* Note: 82801GU (ICH7-U) doesn't have AC97 modem. */ |
| 271 | static const struct pci_driver i82801gx_ac97_modem __pci_driver = { |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 272 | .ops = &ac97_modem_ops, |
| 273 | .vendor = PCI_VENDOR_ID_INTEL, |
Uwe Hermann | 5d7a1c8 | 2008-10-31 18:41:09 +0000 | [diff] [blame] | 274 | .device = 0x27dd, |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 275 | }; |