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