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 | |
Vladimir Serbinenko | 888d559 | 2013-11-13 17:53:38 +0100 | [diff] [blame] | 38 | static void azalia_init(struct device *dev) |
| 39 | { |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 40 | u8 *base; |
Vladimir Serbinenko | 888d559 | 2013-11-13 17:53:38 +0100 | [diff] [blame] | 41 | struct resource *res; |
| 42 | u32 codec_mask; |
Vladimir Serbinenko | 888d559 | 2013-11-13 17:53:38 +0100 | [diff] [blame] | 43 | u32 reg32; |
| 44 | |
| 45 | /* Find base address */ |
Angel Pons | f32ae10 | 2021-11-03 13:07:14 +0100 | [diff] [blame] | 46 | res = probe_resource(dev, PCI_BASE_ADDRESS_0); |
Vladimir Serbinenko | 888d559 | 2013-11-13 17:53:38 +0100 | [diff] [blame] | 47 | if (!res) |
| 48 | return; |
| 49 | |
Martin Roth | 26f97f9 | 2021-10-01 14:53:22 -0600 | [diff] [blame] | 50 | // 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] | 51 | // Is there anything we can do about it? |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 52 | base = res2mmio(res, 0, 0); |
Patrick Rudolph | 819c206 | 2019-11-29 19:27:37 +0100 | [diff] [blame] | 53 | printk(BIOS_DEBUG, "Azalia: base = %p\n", base); |
Vladimir Serbinenko | 888d559 | 2013-11-13 17:53:38 +0100 | [diff] [blame] | 54 | |
| 55 | if (RCBA32(0x2030) & (1 << 31)) { |
| 56 | reg32 = pci_read_config32(dev, 0x120); |
| 57 | reg32 &= 0xf8ffff01; |
| 58 | reg32 |= (1 << 24); // 2 << 24 for server |
| 59 | reg32 |= RCBA32(0x2030) & 0xfe; |
| 60 | pci_write_config32(dev, 0x120, reg32); |
| 61 | |
Elyes HAOUAS | f91538c | 2022-02-07 08:00:59 +0100 | [diff] [blame] | 62 | pci_or_config16(dev, 0x78, 1 << 11); |
Vladimir Serbinenko | 888d559 | 2013-11-13 17:53:38 +0100 | [diff] [blame] | 63 | } else |
| 64 | printk(BIOS_DEBUG, "Azalia: V1CTL disabled.\n"); |
| 65 | |
Elyes HAOUAS | f91538c | 2022-02-07 08:00:59 +0100 | [diff] [blame] | 66 | pci_and_config32(dev, 0x114, ~0xfe); |
Vladimir Serbinenko | 888d559 | 2013-11-13 17:53:38 +0100 | [diff] [blame] | 67 | |
| 68 | // Set VCi enable bit |
Elyes HAOUAS | f91538c | 2022-02-07 08:00:59 +0100 | [diff] [blame] | 69 | pci_or_config32(dev, 0x120, 1 << 31); |
Vladimir Serbinenko | 888d559 | 2013-11-13 17:53:38 +0100 | [diff] [blame] | 70 | |
| 71 | // Enable HDMI codec: |
Elyes HAOUAS | f91538c | 2022-02-07 08:00:59 +0100 | [diff] [blame] | 72 | pci_or_config32(dev, 0xc4, 1 << 1); |
Vladimir Serbinenko | 888d559 | 2013-11-13 17:53:38 +0100 | [diff] [blame] | 73 | |
Elyes HAOUAS | f91538c | 2022-02-07 08:00:59 +0100 | [diff] [blame] | 74 | pci_or_config8(dev, 0x43, 1 << 6); |
Vladimir Serbinenko | 888d559 | 2013-11-13 17:53:38 +0100 | [diff] [blame] | 75 | |
Elyes HAOUAS | f91538c | 2022-02-07 08:00:59 +0100 | [diff] [blame] | 76 | pci_and_config32(dev, 0xd0, ~(1 << 31)); |
Vladimir Serbinenko | 888d559 | 2013-11-13 17:53:38 +0100 | [diff] [blame] | 77 | |
Vladimir Serbinenko | 888d559 | 2013-11-13 17:53:38 +0100 | [diff] [blame] | 78 | /* Set Bus Master */ |
Elyes HAOUAS | 8b6dfde | 2020-04-28 09:58:21 +0200 | [diff] [blame] | 79 | pci_or_config16(dev, PCI_COMMAND, PCI_COMMAND_MASTER); |
Vladimir Serbinenko | 888d559 | 2013-11-13 17:53:38 +0100 | [diff] [blame] | 80 | |
| 81 | pci_write_config8(dev, 0x3c, 0x0a); // unused? |
| 82 | |
| 83 | /* Codec Initialization Programming Sequence */ |
| 84 | |
| 85 | /* Take controller out of reset */ |
Elyes HAOUAS | 59236d5 | 2020-08-03 15:36:52 +0200 | [diff] [blame] | 86 | reg32 = read32(base + HDA_GCTL_REG); |
| 87 | reg32 |= HDA_GCTL_CRST; |
| 88 | write32(base + HDA_GCTL_REG, reg32); |
Vladimir Serbinenko | 888d559 | 2013-11-13 17:53:38 +0100 | [diff] [blame] | 89 | /* Wait 1ms */ |
| 90 | udelay(1000); |
| 91 | |
Elyes HAOUAS | f91538c | 2022-02-07 08:00:59 +0100 | [diff] [blame] | 92 | // Select Azalia mode. This needs to be controlled via devicetree.cb |
| 93 | pci_or_config8(dev, 0x40, 1); // Audio Control |
Vladimir Serbinenko | 888d559 | 2013-11-13 17:53:38 +0100 | [diff] [blame] | 94 | |
Elyes HAOUAS | f91538c | 2022-02-07 08:00:59 +0100 | [diff] [blame] | 95 | // Docking not supported |
| 96 | pci_and_config8(dev, 0x4d, (u8)~(1 << 7)); // Docking Status |
Vladimir Serbinenko | 888d559 | 2013-11-13 17:53:38 +0100 | [diff] [blame] | 97 | |
| 98 | codec_mask = codec_detect(base); |
| 99 | |
| 100 | if (codec_mask) { |
| 101 | printk(BIOS_DEBUG, "Azalia: codec_mask = %02x\n", codec_mask); |
Angel Pons | aae6b55 | 2021-11-10 18:10:38 +0100 | [diff] [blame] | 102 | azalia_codecs_init(base, codec_mask); |
Vladimir Serbinenko | 888d559 | 2013-11-13 17:53:38 +0100 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | /* Enable dynamic clock gating */ |
Elyes HAOUAS | f91538c | 2022-02-07 08:00:59 +0100 | [diff] [blame] | 106 | pci_update_config8(dev, 0x43, ~0x07, (1 << 2) | (1 << 0)); |
Vladimir Serbinenko | 888d559 | 2013-11-13 17:53:38 +0100 | [diff] [blame] | 107 | } |
| 108 | |
Vladimir Serbinenko | 888d559 | 2013-11-13 17:53:38 +0100 | [diff] [blame] | 109 | static struct device_operations azalia_ops = { |
| 110 | .read_resources = pci_dev_read_resources, |
| 111 | .set_resources = pci_dev_set_resources, |
| 112 | .enable_resources = pci_dev_enable_resources, |
| 113 | .init = azalia_init, |
Angel Pons | 1fc0edd | 2020-05-31 00:03:28 +0200 | [diff] [blame] | 114 | .ops_pci = &pci_dev_ops_pci, |
Vladimir Serbinenko | 888d559 | 2013-11-13 17:53:38 +0100 | [diff] [blame] | 115 | }; |
| 116 | |
Felix Singer | 838fbc7 | 2019-11-21 21:23:32 +0100 | [diff] [blame] | 117 | static const unsigned short pci_device_ids[] = { |
| 118 | 0x1c20, |
| 119 | 0x1e20, |
| 120 | PCI_DID_INTEL_IBEXPEAK_AUDIO, |
| 121 | 0 |
| 122 | }; |
Vladimir Serbinenko | 888d559 | 2013-11-13 17:53:38 +0100 | [diff] [blame] | 123 | |
| 124 | static const struct pci_driver pch_azalia __pci_driver = { |
| 125 | .ops = &azalia_ops, |
Felix Singer | 43b7f41 | 2022-03-07 04:34:52 +0100 | [diff] [blame] | 126 | .vendor = PCI_VID_INTEL, |
Vladimir Serbinenko | 888d559 | 2013-11-13 17:53:38 +0100 | [diff] [blame] | 127 | .devices = pci_device_ids, |
| 128 | }; |