blob: c41db9481308c05e9370409309d04eafa3f2dc0d [file] [log] [blame]
Angel Ponsc3f58f62020-04-05 15:46:41 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Aaron Durbin97651c52013-11-01 14:36:03 -05002
Kyösti Mälkki13f66502019-03-03 08:01:05 +02003#include <device/mmio.h>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +02004#include <device/pci_ops.h>
Kyösti Mälkki5daa1d32020-06-14 12:01:58 +03005#include <acpi/acpi_gnvs.h>
Aaron Durbin97651c52013-11-01 14:36:03 -05006#include <console/console.h>
7#include <device/device.h>
8#include <device/pci.h>
9#include <device/pci_ids.h>
Shawn Nematbakhsh51d787a2014-01-16 17:52:21 -080010#include <reg_script.h>
Aaron Durbin97651c52013-11-01 14:36:03 -050011
Julius Werner18ea2d32014-10-07 16:42:17 -070012#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 Ponsb5320b22020-07-07 18:27:30 +020018#include <soc/pm.h>
Julius Werner18ea2d32014-10-07 16:42:17 -070019#include <soc/ramstage.h>
Aaron Durbin8cbf47f2013-12-04 11:03:20 -060020#include "chip.h"
21
22
Aaron Durbinf4fe3c32013-12-09 12:52:37 -060023/* 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 Nematbakhsh51d787a2014-01-16 17:52:21 -080027#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 Roth57e89092019-10-23 21:45:23 -060032static void assign_device_nvs(struct device *dev, u32 *field, unsigned int index)
Shawn Nematbakhsh51d787a2014-01-16 17:52:21 -080033{
34 struct resource *res;
35
36 res = find_resource(dev, index);
37 if (res)
38 *field = res->base;
39}
40
Elyes HAOUAS17a3ceb2018-05-22 10:42:28 +020041static void lpe_enable_acpi_mode(struct device *dev)
Shawn Nematbakhsh51d787a2014-01-16 17:52:21 -080042{
43 static const struct reg_script ops[] = {
44 /* Disable PCI interrupt, enable Memory and Bus Master */
Elyes HAOUASd2bbc682020-04-29 10:12:33 +020045 REG_PCI_OR16(PCI_COMMAND,
Shawn Nematbakhsh51d787a2014-01-16 17:52:21 -080046 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älkki0c1dd9c2020-06-17 23:37:49 +030053 struct global_nvs *gnvs;
Shawn Nematbakhsh51d787a2014-01-16 17:52:21 -080054
55 /* Find ACPI NVS to update BARs */
Kyösti Mälkki5daa1d32020-06-14 12:01:58 +030056 gnvs = acpi_get_gnvs();
57 if (!gnvs)
Shawn Nematbakhsh51d787a2014-01-16 17:52:21 -080058 return;
Shawn Nematbakhsh51d787a2014-01-16 17:52:21 -080059
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 Durbinf4fe3c32013-12-09 12:52:37 -060071
Elyes HAOUAS17a3ceb2018-05-22 10:42:28 +020072static void setup_codec_clock(struct device *dev)
Aaron Durbin8cbf47f2013-12-04 11:03:20 -060073{
74 uint32_t reg;
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080075 u32 *clk_reg;
Aaron Durbin8cbf47f2013-12-04 11:03:20 -060076 struct soc_intel_baytrail_config *config;
77 const char *freq_str;
78
Kyösti Mälkki8950cfb2019-07-13 22:16:25 +030079 config = config_of(dev);
Aaron Durbin8cbf47f2013-12-04 11:03:20 -060080 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 Herbertbde6d302014-12-24 18:43:20 -0800104 clk_reg = (u32 *)(PMC_BASE_ADDRESS + PLT_CLK_CTL_0);
105 clk_reg += config->lpe_codec_clk_num;
Aaron Durbin8cbf47f2013-12-04 11:03:20 -0600106
107 write32(clk_reg, (read32(clk_reg) & ~0x7) | reg);
108}
Aaron Durbin97651c52013-11-01 14:36:03 -0500109
Elyes HAOUAS17a3ceb2018-05-22 10:42:28 +0200110static void lpe_stash_firmware_info(struct device *dev)
Shawn Nematbakhsh51d787a2014-01-16 17:52:21 -0800111{
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 Herbertbde6d302014-12-24 18:43:20 -0800129 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 Nematbakhsh51d787a2014-01-16 17:52:21 -0800133 }
134}
135
Elyes HAOUAS17a3ceb2018-05-22 10:42:28 +0200136static void lpe_init(struct device *dev)
Aaron Durbin97651c52013-11-01 14:36:03 -0500137{
Kyösti Mälkki8950cfb2019-07-13 22:16:25 +0300138 struct soc_intel_baytrail_config *config = config_of(dev);
Shawn Nematbakhsh51d787a2014-01-16 17:52:21 -0800139
140 lpe_stash_firmware_info(dev);
141
Aaron Durbin8cbf47f2013-12-04 11:03:20 -0600142 setup_codec_clock(dev);
Shawn Nematbakhsh51d787a2014-01-16 17:52:21 -0800143
144 if (config->lpe_acpi_mode)
145 lpe_enable_acpi_mode(dev);
Aaron Durbin97651c52013-11-01 14:36:03 -0500146}
147
Elyes HAOUAS17a3ceb2018-05-22 10:42:28 +0200148static void lpe_read_resources(struct device *dev)
Aaron Durbinf4fe3c32013-12-09 12:52:37 -0600149{
150 pci_dev_read_resources(dev);
151
Shawn Nematbakhsh51d787a2014-01-16 17:52:21 -0800152 reserved_ram_resource(dev, FIRMWARE_PCI_REG_BASE,
Aaron Durbinf4fe3c32013-12-09 12:52:37 -0600153 FIRMWARE_PHYS_BASE >> 10,
154 FIRMWARE_PHYS_LENGTH >> 10);
155}
156
Aaron Durbin97651c52013-11-01 14:36:03 -0500157static const struct device_operations device_ops = {
Aaron Durbinf4fe3c32013-12-09 12:52:37 -0600158 .read_resources = lpe_read_resources,
Shawn Nematbakhsh51d787a2014-01-16 17:52:21 -0800159 .set_resources = pci_dev_set_resources,
Aaron Durbin4334c872013-12-05 11:12:15 -0600160 .enable_resources = pci_dev_enable_resources,
Aaron Durbin97651c52013-11-01 14:36:03 -0500161 .init = lpe_init,
Aaron Durbin97651c52013-11-01 14:36:03 -0500162 .ops_pci = &soc_pci_ops,
163};
164
165static const struct pci_driver southcluster __pci_driver = {
166 .ops = &device_ops,
167 .vendor = PCI_VENDOR_ID_INTEL,
168 .device = LPE_DEVID,
169};