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