Angel Pons | c3f58f6 | 2020-04-05 15:46:41 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Aaron Durbin | 97651c5 | 2013-11-01 14:36:03 -0500 | [diff] [blame] | 2 | |
Kyösti Mälkki | 13f6650 | 2019-03-03 08:01:05 +0200 | [diff] [blame] | 3 | #include <device/mmio.h> |
Kyösti Mälkki | f1b58b7 | 2019-03-01 13:43:02 +0200 | [diff] [blame] | 4 | #include <device/pci_ops.h> |
Kyösti Mälkki | 5daa1d3 | 2020-06-14 12:01:58 +0300 | [diff] [blame] | 5 | #include <acpi/acpi_gnvs.h> |
Aaron Durbin | 97651c5 | 2013-11-01 14:36:03 -0500 | [diff] [blame] | 6 | #include <console/console.h> |
| 7 | #include <device/device.h> |
| 8 | #include <device/pci.h> |
| 9 | #include <device/pci_ids.h> |
Shawn Nematbakhsh | 51d787a | 2014-01-16 17:52:21 -0800 | [diff] [blame] | 10 | #include <reg_script.h> |
Aaron Durbin | 97651c5 | 2013-11-01 14:36:03 -0500 | [diff] [blame] | 11 | |
Julius Werner | 18ea2d3 | 2014-10-07 16:42:17 -0700 | [diff] [blame] | 12 | #include <soc/iomap.h> |
| 13 | #include <soc/iosf.h> |
| 14 | #include <soc/lpc.h> |
| 15 | #include <soc/nvs.h> |
| 16 | #include <soc/pattrs.h> |
| 17 | #include <soc/pci_devs.h> |
Angel Pons | b5320b2 | 2020-07-07 18:27:30 +0200 | [diff] [blame] | 18 | #include <soc/pm.h> |
Julius Werner | 18ea2d3 | 2014-10-07 16:42:17 -0700 | [diff] [blame] | 19 | #include <soc/ramstage.h> |
Aaron Durbin | 8cbf47f | 2013-12-04 11:03:20 -0600 | [diff] [blame] | 20 | #include "chip.h" |
| 21 | |
Angel Pons | 26b49cc | 2020-07-07 17:17:51 +0200 | [diff] [blame] | 22 | /* |
| 23 | * The LPE audio devices needs 1MiB of memory reserved aligned to a 512MiB |
| 24 | * address. Just take 1MiB @ 512MiB. |
| 25 | */ |
Aaron Durbin | f4fe3c3 | 2013-12-09 12:52:37 -0600 | [diff] [blame] | 26 | #define FIRMWARE_PHYS_BASE (512 << 20) |
| 27 | #define FIRMWARE_PHYS_LENGTH (1 << 20) |
Shawn Nematbakhsh | 51d787a | 2014-01-16 17:52:21 -0800 | [diff] [blame] | 28 | #define FIRMWARE_PCI_REG_BASE 0xa8 |
| 29 | #define FIRMWARE_PCI_REG_LENGTH 0xac |
| 30 | #define FIRMWARE_REG_BASE_C0 0x144000 |
| 31 | #define FIRMWARE_REG_LENGTH_C0 (FIRMWARE_REG_BASE_C0 + 4) |
| 32 | |
Martin Roth | 57e8909 | 2019-10-23 21:45:23 -0600 | [diff] [blame] | 33 | static void assign_device_nvs(struct device *dev, u32 *field, unsigned int index) |
Shawn Nematbakhsh | 51d787a | 2014-01-16 17:52:21 -0800 | [diff] [blame] | 34 | { |
| 35 | struct resource *res; |
| 36 | |
| 37 | res = find_resource(dev, index); |
| 38 | if (res) |
| 39 | *field = res->base; |
| 40 | } |
| 41 | |
Elyes HAOUAS | 17a3ceb | 2018-05-22 10:42:28 +0200 | [diff] [blame] | 42 | static void lpe_enable_acpi_mode(struct device *dev) |
Shawn Nematbakhsh | 51d787a | 2014-01-16 17:52:21 -0800 | [diff] [blame] | 43 | { |
| 44 | static const struct reg_script ops[] = { |
| 45 | /* Disable PCI interrupt, enable Memory and Bus Master */ |
Elyes HAOUAS | d2bbc68 | 2020-04-29 10:12:33 +0200 | [diff] [blame] | 46 | REG_PCI_OR16(PCI_COMMAND, |
Shawn Nematbakhsh | 51d787a | 2014-01-16 17:52:21 -0800 | [diff] [blame] | 47 | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER | (1<<10)), |
| 48 | /* Enable ACPI mode */ |
| 49 | REG_IOSF_OR(IOSF_PORT_0x58, LPE_PCICFGCTR1, |
| 50 | LPE_PCICFGCTR1_PCI_CFG_DIS | |
| 51 | LPE_PCICFGCTR1_ACPI_INT_EN), |
| 52 | REG_SCRIPT_END |
| 53 | }; |
Kyösti Mälkki | 0c1dd9c | 2020-06-17 23:37:49 +0300 | [diff] [blame] | 54 | struct global_nvs *gnvs; |
Shawn Nematbakhsh | 51d787a | 2014-01-16 17:52:21 -0800 | [diff] [blame] | 55 | |
| 56 | /* Find ACPI NVS to update BARs */ |
Kyösti Mälkki | 5daa1d3 | 2020-06-14 12:01:58 +0300 | [diff] [blame] | 57 | gnvs = acpi_get_gnvs(); |
| 58 | if (!gnvs) |
Shawn Nematbakhsh | 51d787a | 2014-01-16 17:52:21 -0800 | [diff] [blame] | 59 | return; |
Shawn Nematbakhsh | 51d787a | 2014-01-16 17:52:21 -0800 | [diff] [blame] | 60 | |
| 61 | /* Save BAR0, BAR1, and firmware base to ACPI NVS */ |
| 62 | assign_device_nvs(dev, &gnvs->dev.lpe_bar0, PCI_BASE_ADDRESS_0); |
| 63 | assign_device_nvs(dev, &gnvs->dev.lpe_bar1, PCI_BASE_ADDRESS_1); |
| 64 | assign_device_nvs(dev, &gnvs->dev.lpe_fw, FIRMWARE_PCI_REG_BASE); |
| 65 | |
| 66 | /* Device is enabled in ACPI mode */ |
| 67 | gnvs->dev.lpe_en = 1; |
| 68 | |
| 69 | /* Put device in ACPI mode */ |
| 70 | reg_script_run_on_dev(dev, ops); |
| 71 | } |
Aaron Durbin | f4fe3c3 | 2013-12-09 12:52:37 -0600 | [diff] [blame] | 72 | |
Elyes HAOUAS | 17a3ceb | 2018-05-22 10:42:28 +0200 | [diff] [blame] | 73 | static void setup_codec_clock(struct device *dev) |
Aaron Durbin | 8cbf47f | 2013-12-04 11:03:20 -0600 | [diff] [blame] | 74 | { |
| 75 | uint32_t reg; |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 76 | u32 *clk_reg; |
Aaron Durbin | 8cbf47f | 2013-12-04 11:03:20 -0600 | [diff] [blame] | 77 | struct soc_intel_baytrail_config *config; |
| 78 | const char *freq_str; |
| 79 | |
Kyösti Mälkki | 8950cfb | 2019-07-13 22:16:25 +0300 | [diff] [blame] | 80 | config = config_of(dev); |
Aaron Durbin | 8cbf47f | 2013-12-04 11:03:20 -0600 | [diff] [blame] | 81 | switch (config->lpe_codec_clk_freq) { |
| 82 | case 19: |
| 83 | freq_str = "19.2"; |
| 84 | reg = CLK_FREQ_19P2MHZ; |
| 85 | break; |
Angel Pons | 26b49cc | 2020-07-07 17:17:51 +0200 | [diff] [blame] | 86 | |
Aaron Durbin | 8cbf47f | 2013-12-04 11:03:20 -0600 | [diff] [blame] | 87 | case 25: |
| 88 | freq_str = "25"; |
| 89 | reg = CLK_FREQ_25MHZ; |
| 90 | break; |
Angel Pons | 26b49cc | 2020-07-07 17:17:51 +0200 | [diff] [blame] | 91 | |
Aaron Durbin | 8cbf47f | 2013-12-04 11:03:20 -0600 | [diff] [blame] | 92 | default: |
| 93 | printk(BIOS_DEBUG, "LPE codec clock not required.\n"); |
| 94 | return; |
| 95 | } |
| 96 | |
| 97 | /* Default to always running. */ |
| 98 | reg |= CLK_CTL_ON; |
| 99 | |
| 100 | if (config->lpe_codec_clk_num < 0 || config->lpe_codec_clk_num > 5) { |
| 101 | printk(BIOS_DEBUG, "Invalid LPE codec clock number.\n"); |
| 102 | return; |
| 103 | } |
| 104 | |
| 105 | printk(BIOS_DEBUG, "LPE Audio codec clock set to %sMHz.\n", freq_str); |
| 106 | |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 107 | clk_reg = (u32 *)(PMC_BASE_ADDRESS + PLT_CLK_CTL_0); |
| 108 | clk_reg += config->lpe_codec_clk_num; |
Aaron Durbin | 8cbf47f | 2013-12-04 11:03:20 -0600 | [diff] [blame] | 109 | |
| 110 | write32(clk_reg, (read32(clk_reg) & ~0x7) | reg); |
| 111 | } |
Aaron Durbin | 97651c5 | 2013-11-01 14:36:03 -0500 | [diff] [blame] | 112 | |
Elyes HAOUAS | 17a3ceb | 2018-05-22 10:42:28 +0200 | [diff] [blame] | 113 | static void lpe_stash_firmware_info(struct device *dev) |
Shawn Nematbakhsh | 51d787a | 2014-01-16 17:52:21 -0800 | [diff] [blame] | 114 | { |
| 115 | struct resource *res; |
| 116 | struct resource *mmio; |
| 117 | const struct pattrs *pattrs = pattrs_get(); |
| 118 | |
| 119 | res = find_resource(dev, FIRMWARE_PCI_REG_BASE); |
| 120 | if (res == NULL) { |
| 121 | printk(BIOS_DEBUG, "LPE Firmware memory not found.\n"); |
| 122 | return; |
| 123 | } |
| 124 | |
| 125 | /* Continue using old way of informing firmware address / size. */ |
| 126 | pci_write_config32(dev, FIRMWARE_PCI_REG_BASE, res->base); |
| 127 | pci_write_config32(dev, FIRMWARE_PCI_REG_LENGTH, res->size); |
| 128 | |
| 129 | /* C0 and later steppings use an offset in the MMIO space. */ |
| 130 | if (pattrs->stepping >= STEP_C0) { |
| 131 | mmio = find_resource(dev, PCI_BASE_ADDRESS_0); |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 132 | write32((u32 *)(uintptr_t)(mmio->base + FIRMWARE_REG_BASE_C0), |
| 133 | res->base); |
| 134 | write32((u32 *)(uintptr_t)(mmio->base + FIRMWARE_REG_LENGTH_C0), |
| 135 | res->size); |
Shawn Nematbakhsh | 51d787a | 2014-01-16 17:52:21 -0800 | [diff] [blame] | 136 | } |
| 137 | } |
| 138 | |
Elyes HAOUAS | 17a3ceb | 2018-05-22 10:42:28 +0200 | [diff] [blame] | 139 | static void lpe_init(struct device *dev) |
Aaron Durbin | 97651c5 | 2013-11-01 14:36:03 -0500 | [diff] [blame] | 140 | { |
Kyösti Mälkki | 8950cfb | 2019-07-13 22:16:25 +0300 | [diff] [blame] | 141 | struct soc_intel_baytrail_config *config = config_of(dev); |
Shawn Nematbakhsh | 51d787a | 2014-01-16 17:52:21 -0800 | [diff] [blame] | 142 | |
| 143 | lpe_stash_firmware_info(dev); |
Aaron Durbin | 8cbf47f | 2013-12-04 11:03:20 -0600 | [diff] [blame] | 144 | setup_codec_clock(dev); |
Shawn Nematbakhsh | 51d787a | 2014-01-16 17:52:21 -0800 | [diff] [blame] | 145 | |
| 146 | if (config->lpe_acpi_mode) |
| 147 | lpe_enable_acpi_mode(dev); |
Aaron Durbin | 97651c5 | 2013-11-01 14:36:03 -0500 | [diff] [blame] | 148 | } |
| 149 | |
Elyes HAOUAS | 17a3ceb | 2018-05-22 10:42:28 +0200 | [diff] [blame] | 150 | static void lpe_read_resources(struct device *dev) |
Aaron Durbin | f4fe3c3 | 2013-12-09 12:52:37 -0600 | [diff] [blame] | 151 | { |
| 152 | pci_dev_read_resources(dev); |
| 153 | |
Shawn Nematbakhsh | 51d787a | 2014-01-16 17:52:21 -0800 | [diff] [blame] | 154 | reserved_ram_resource(dev, FIRMWARE_PCI_REG_BASE, |
Aaron Durbin | f4fe3c3 | 2013-12-09 12:52:37 -0600 | [diff] [blame] | 155 | FIRMWARE_PHYS_BASE >> 10, |
| 156 | FIRMWARE_PHYS_LENGTH >> 10); |
| 157 | } |
| 158 | |
Aaron Durbin | 97651c5 | 2013-11-01 14:36:03 -0500 | [diff] [blame] | 159 | static const struct device_operations device_ops = { |
Aaron Durbin | f4fe3c3 | 2013-12-09 12:52:37 -0600 | [diff] [blame] | 160 | .read_resources = lpe_read_resources, |
Shawn Nematbakhsh | 51d787a | 2014-01-16 17:52:21 -0800 | [diff] [blame] | 161 | .set_resources = pci_dev_set_resources, |
Aaron Durbin | 4334c87 | 2013-12-05 11:12:15 -0600 | [diff] [blame] | 162 | .enable_resources = pci_dev_enable_resources, |
Aaron Durbin | 97651c5 | 2013-11-01 14:36:03 -0500 | [diff] [blame] | 163 | .init = lpe_init, |
Aaron Durbin | 97651c5 | 2013-11-01 14:36:03 -0500 | [diff] [blame] | 164 | .ops_pci = &soc_pci_ops, |
| 165 | }; |
| 166 | |
| 167 | static const struct pci_driver southcluster __pci_driver = { |
| 168 | .ops = &device_ops, |
| 169 | .vendor = PCI_VENDOR_ID_INTEL, |
| 170 | .device = LPE_DEVID, |
| 171 | }; |