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