Angel Pons | 182dbde | 2020-04-02 23:49:05 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Stefan Reinauer | 679c9f9 | 2009-01-20 22:54:59 +0000 | [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 | 679c9f9 | 2009-01-20 22:54:59 +0000 | [diff] [blame] | 9 | #include <delay.h> |
Vladimir Serbinenko | 75c8387 | 2014-09-05 01:01:31 +0200 | [diff] [blame] | 10 | #include <device/azalia_device.h> |
Arthur Heymans | 028b94b | 2021-07-02 10:06:55 +0200 | [diff] [blame] | 11 | #include <stdint.h> |
Kyösti Mälkki | 12b121c | 2019-08-18 16:33:39 +0300 | [diff] [blame] | 12 | #include "chip.h" |
Stefan Reinauer | 679c9f9 | 2009-01-20 22:54:59 +0000 | [diff] [blame] | 13 | #include "i82801gx.h" |
| 14 | |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 15 | static int codec_detect(u8 *base) |
Stefan Reinauer | 679c9f9 | 2009-01-20 22:54:59 +0000 | [diff] [blame] | 16 | { |
Stefan Reinauer | a8e1168 | 2009-03-11 14:54:18 +0000 | [diff] [blame] | 17 | u32 reg32; |
Stefan Reinauer | 679c9f9 | 2009-01-20 22:54:59 +0000 | [diff] [blame] | 18 | |
Angel Pons | 2e0053b | 2020-12-05 19:06:55 +0100 | [diff] [blame] | 19 | if (azalia_enter_reset(base) < 0) |
Stefan Reinauer | a8e1168 | 2009-03-11 14:54:18 +0000 | [diff] [blame] | 20 | goto no_codec; |
Stefan Reinauer | 679c9f9 | 2009-01-20 22:54:59 +0000 | [diff] [blame] | 21 | |
Angel Pons | 7f839f6 | 2020-12-05 19:02:14 +0100 | [diff] [blame] | 22 | if (azalia_exit_reset(base) < 0) |
Stefan Reinauer | a8e1168 | 2009-03-11 14:54:18 +0000 | [diff] [blame] | 23 | goto no_codec; |
Stefan Reinauer | 679c9f9 | 2009-01-20 22:54:59 +0000 | [diff] [blame] | 24 | |
Elyes HAOUAS | 6ea24ff | 2020-08-11 09:21:24 +0200 | [diff] [blame] | 25 | /* Read in Codec location (BAR + 0xe)[2..0] */ |
Elyes HAOUAS | f1da909 | 2020-08-03 15:35:16 +0200 | [diff] [blame] | 26 | reg32 = read32(base + HDA_STATESTS_REG); |
Stefan Reinauer | a8e1168 | 2009-03-11 14:54:18 +0000 | [diff] [blame] | 27 | reg32 &= 0x0f; |
| 28 | if (!reg32) |
| 29 | goto no_codec; |
Stefan Reinauer | 109ab31 | 2009-08-12 16:08:05 +0000 | [diff] [blame] | 30 | |
Stefan Reinauer | a8e1168 | 2009-03-11 14:54:18 +0000 | [diff] [blame] | 31 | return reg32; |
| 32 | |
| 33 | no_codec: |
Angel Pons | 2e0053b | 2020-12-05 19:06:55 +0100 | [diff] [blame] | 34 | /* Codec not found, put HDA back in reset */ |
| 35 | azalia_enter_reset(base); |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 36 | printk(BIOS_DEBUG, "Azalia: No codec!\n"); |
Stefan Reinauer | a8e1168 | 2009-03-11 14:54:18 +0000 | [diff] [blame] | 37 | return 0; |
Stefan Reinauer | 679c9f9 | 2009-01-20 22:54:59 +0000 | [diff] [blame] | 38 | } |
| 39 | |
Stefan Reinauer | 679c9f9 | 2009-01-20 22:54:59 +0000 | [diff] [blame] | 40 | static void azalia_init(struct device *dev) |
| 41 | { |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 42 | u8 *base; |
Stefan Reinauer | 679c9f9 | 2009-01-20 22:54:59 +0000 | [diff] [blame] | 43 | struct resource *res; |
| 44 | u32 codec_mask; |
Stefan Reinauer | a8e1168 | 2009-03-11 14:54:18 +0000 | [diff] [blame] | 45 | u8 reg8; |
Stefan Reinauer | 679c9f9 | 2009-01-20 22:54:59 +0000 | [diff] [blame] | 46 | |
Stefan Reinauer | a8e1168 | 2009-03-11 14:54:18 +0000 | [diff] [blame] | 47 | // ESD |
Angel Pons | d19332c | 2020-06-08 12:32:54 +0200 | [diff] [blame] | 48 | pci_update_config32(dev, 0x134, ~(0xff << 16), 2 << 16); |
Stefan Reinauer | a8e1168 | 2009-03-11 14:54:18 +0000 | [diff] [blame] | 49 | |
| 50 | // Link1 description |
Angel Pons | d19332c | 2020-06-08 12:32:54 +0200 | [diff] [blame] | 51 | pci_update_config32(dev, 0x140, ~(0xff << 16), 2 << 16); |
Stefan Reinauer | a8e1168 | 2009-03-11 14:54:18 +0000 | [diff] [blame] | 52 | |
| 53 | // Port VC0 Resource Control Register |
Angel Pons | d19332c | 2020-06-08 12:32:54 +0200 | [diff] [blame] | 54 | pci_update_config32(dev, 0x114, ~(0xff << 0), 1); |
Stefan Reinauer | a8e1168 | 2009-03-11 14:54:18 +0000 | [diff] [blame] | 55 | |
| 56 | // VCi traffic class |
Angel Pons | d19332c | 2020-06-08 12:32:54 +0200 | [diff] [blame] | 57 | pci_or_config8(dev, 0x44, 7 << 0); // TC7 |
Stefan Reinauer | a8e1168 | 2009-03-11 14:54:18 +0000 | [diff] [blame] | 58 | |
| 59 | // VCi Resource Control |
Angel Pons | d19332c | 2020-06-08 12:32:54 +0200 | [diff] [blame] | 60 | pci_or_config32(dev, 0x120, (1 << 31) | (1 << 24) | (0x80 << 0)); /* VCi ID and map */ |
Stefan Reinauer | a8e1168 | 2009-03-11 14:54:18 +0000 | [diff] [blame] | 61 | |
| 62 | /* Set Bus Master */ |
Elyes HAOUAS | 1234925 | 2020-04-27 05:08:26 +0200 | [diff] [blame] | 63 | pci_or_config16(dev, PCI_COMMAND, PCI_COMMAND_MASTER); |
Stefan Reinauer | a8e1168 | 2009-03-11 14:54:18 +0000 | [diff] [blame] | 64 | |
| 65 | pci_write_config8(dev, 0x3c, 0x0a); // unused? |
| 66 | |
| 67 | // TODO Actually check if we're AC97 or HDA instead of hardcoding this |
Stefan Reinauer | 38f147e | 2010-02-08 12:20:50 +0000 | [diff] [blame] | 68 | // here, in devicetree.cb and/or romstage.c. |
Stefan Reinauer | a8e1168 | 2009-03-11 14:54:18 +0000 | [diff] [blame] | 69 | reg8 = pci_read_config8(dev, 0x40); |
| 70 | reg8 |= (1 << 3); // Clear Clock Detect Bit |
| 71 | pci_write_config8(dev, 0x40, reg8); |
| 72 | reg8 &= ~(1 << 3); // Keep CLKDETCLR from clearing the bit over and over |
| 73 | pci_write_config8(dev, 0x40, reg8); |
| 74 | reg8 |= (1 << 2); // Enable clock detection |
| 75 | pci_write_config8(dev, 0x40, reg8); |
| 76 | mdelay(1); |
| 77 | reg8 = pci_read_config8(dev, 0x40); |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 78 | printk(BIOS_DEBUG, "Azalia: codec type: %s\n", (reg8 & (1 << 1))?"Azalia":"AC97"); |
Stefan Reinauer | a8e1168 | 2009-03-11 14:54:18 +0000 | [diff] [blame] | 79 | |
Angel Pons | d19332c | 2020-06-08 12:32:54 +0200 | [diff] [blame] | 80 | // Select Azalia mode. This needs to be controlled via devicetree.cb |
| 81 | pci_or_config8(dev, 0x40, 1); // Audio Control |
Stefan Reinauer | a8e1168 | 2009-03-11 14:54:18 +0000 | [diff] [blame] | 82 | |
Angel Pons | d19332c | 2020-06-08 12:32:54 +0200 | [diff] [blame] | 83 | // Docking not supported |
| 84 | pci_and_config8(dev, 0x4d, (u8)~(1 << 7)); // Docking Status |
Stefan Reinauer | 679c9f9 | 2009-01-20 22:54:59 +0000 | [diff] [blame] | 85 | |
Angel Pons | f32ae10 | 2021-11-03 13:07:14 +0100 | [diff] [blame] | 86 | res = probe_resource(dev, PCI_BASE_ADDRESS_0); |
Stefan Reinauer | 679c9f9 | 2009-01-20 22:54:59 +0000 | [diff] [blame] | 87 | if (!res) |
| 88 | return; |
| 89 | |
Martin Roth | 26f97f9 | 2021-10-01 14:53:22 -0600 | [diff] [blame] | 90 | // 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] | 91 | // Is there anything we can do about it? |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 92 | base = res2mmio(res, 0, 0); |
Arthur Heymans | 028b94b | 2021-07-02 10:06:55 +0200 | [diff] [blame] | 93 | printk(BIOS_DEBUG, "Azalia: base = %08x\n", (u32)(uintptr_t)base); |
Stefan Reinauer | 679c9f9 | 2009-01-20 22:54:59 +0000 | [diff] [blame] | 94 | codec_mask = codec_detect(base); |
| 95 | |
| 96 | if (codec_mask) { |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 97 | printk(BIOS_DEBUG, "Azalia: codec_mask = %02x\n", codec_mask); |
Angel Pons | 4e94822 | 2021-11-10 17:26:15 +0100 | [diff] [blame] | 98 | azalia_codecs_init(dev, base, codec_mask); |
Stefan Reinauer | 679c9f9 | 2009-01-20 22:54:59 +0000 | [diff] [blame] | 99 | } |
| 100 | } |
| 101 | |
| 102 | static struct device_operations azalia_ops = { |
| 103 | .read_resources = pci_dev_read_resources, |
| 104 | .set_resources = pci_dev_set_resources, |
| 105 | .enable_resources = pci_dev_enable_resources, |
| 106 | .init = azalia_init, |
Stefan Reinauer | 679c9f9 | 2009-01-20 22:54:59 +0000 | [diff] [blame] | 107 | .enable = i82801gx_enable, |
Angel Pons | 1fc0edd | 2020-05-31 00:03:28 +0200 | [diff] [blame] | 108 | .ops_pci = &pci_dev_ops_pci, |
Stefan Reinauer | 679c9f9 | 2009-01-20 22:54:59 +0000 | [diff] [blame] | 109 | }; |
| 110 | |
| 111 | /* 82801GB/GR/GDH/GBM/GHM (ICH7/ICH7R/ICH7DH/ICH7-M/ICH7-M DH) */ |
| 112 | static const struct pci_driver i82801gx_azalia __pci_driver = { |
| 113 | .ops = &azalia_ops, |
| 114 | .vendor = PCI_VENDOR_ID_INTEL, |
| 115 | .device = 0x27d8, |
| 116 | }; |