blob: 03ac736793b8ecc125e69cbd538eac0be62a2835 [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>
Kyösti Mälkki4abc7312021-01-12 17:46:30 +020015#include <soc/device_nvs.h>
Julius Werner18ea2d32014-10-07 16:42:17 -070016#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
Angel Pons26b49cc2020-07-07 17:17:51 +020022/*
23 * The LPE audio devices needs 1MiB of memory reserved aligned to a 512MiB
24 * address. Just take 1MiB @ 512MiB.
25 */
Aaron Durbinf4fe3c32013-12-09 12:52:37 -060026#define FIRMWARE_PHYS_BASE (512 << 20)
27#define FIRMWARE_PHYS_LENGTH (1 << 20)
Shawn Nematbakhsh51d787a2014-01-16 17:52:21 -080028#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 Roth57e89092019-10-23 21:45:23 -060033static void assign_device_nvs(struct device *dev, u32 *field, unsigned int index)
Shawn Nematbakhsh51d787a2014-01-16 17:52:21 -080034{
35 struct resource *res;
36
Angel Ponsc1bfbe02021-11-03 13:18:53 +010037 res = probe_resource(dev, index);
Shawn Nematbakhsh51d787a2014-01-16 17:52:21 -080038 if (res)
39 *field = res->base;
40}
41
Elyes HAOUAS17a3ceb2018-05-22 10:42:28 +020042static void lpe_enable_acpi_mode(struct device *dev)
Shawn Nematbakhsh51d787a2014-01-16 17:52:21 -080043{
44 static const struct reg_script ops[] = {
45 /* Disable PCI interrupt, enable Memory and Bus Master */
Elyes HAOUASd2bbc682020-04-29 10:12:33 +020046 REG_PCI_OR16(PCI_COMMAND,
Angel Pons12baf202020-07-07 17:51:17 +020047 PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER | PCI_COMMAND_INT_DISABLE),
48
Shawn Nematbakhsh51d787a2014-01-16 17:52:21 -080049 /* Enable ACPI mode */
50 REG_IOSF_OR(IOSF_PORT_0x58, LPE_PCICFGCTR1,
Angel Pons12baf202020-07-07 17:51:17 +020051 LPE_PCICFGCTR1_PCI_CFG_DIS | LPE_PCICFGCTR1_ACPI_INT_EN),
52
Shawn Nematbakhsh51d787a2014-01-16 17:52:21 -080053 REG_SCRIPT_END
54 };
Kyösti Mälkki4abc7312021-01-12 17:46:30 +020055 struct device_nvs *dev_nvs = acpi_get_device_nvs();
Shawn Nematbakhsh51d787a2014-01-16 17:52:21 -080056
57 /* Save BAR0, BAR1, and firmware base to ACPI NVS */
Kyösti Mälkki4abc7312021-01-12 17:46:30 +020058 assign_device_nvs(dev, &dev_nvs->lpe_bar0, PCI_BASE_ADDRESS_0);
59 assign_device_nvs(dev, &dev_nvs->lpe_bar1, PCI_BASE_ADDRESS_1);
60 assign_device_nvs(dev, &dev_nvs->lpe_fw, FIRMWARE_PCI_REG_BASE);
Shawn Nematbakhsh51d787a2014-01-16 17:52:21 -080061
62 /* Device is enabled in ACPI mode */
Kyösti Mälkki4abc7312021-01-12 17:46:30 +020063 dev_nvs->lpe_en = 1;
Shawn Nematbakhsh51d787a2014-01-16 17:52:21 -080064
65 /* Put device in ACPI mode */
66 reg_script_run_on_dev(dev, ops);
67}
Aaron Durbinf4fe3c32013-12-09 12:52:37 -060068
Elyes HAOUAS17a3ceb2018-05-22 10:42:28 +020069static void setup_codec_clock(struct device *dev)
Aaron Durbin8cbf47f2013-12-04 11:03:20 -060070{
71 uint32_t reg;
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080072 u32 *clk_reg;
Aaron Durbin8cbf47f2013-12-04 11:03:20 -060073 struct soc_intel_baytrail_config *config;
74 const char *freq_str;
75
Kyösti Mälkki8950cfb2019-07-13 22:16:25 +030076 config = config_of(dev);
Aaron Durbin8cbf47f2013-12-04 11:03:20 -060077 switch (config->lpe_codec_clk_freq) {
78 case 19:
79 freq_str = "19.2";
80 reg = CLK_FREQ_19P2MHZ;
81 break;
Angel Pons26b49cc2020-07-07 17:17:51 +020082
Aaron Durbin8cbf47f2013-12-04 11:03:20 -060083 case 25:
84 freq_str = "25";
85 reg = CLK_FREQ_25MHZ;
86 break;
Angel Pons26b49cc2020-07-07 17:17:51 +020087
Aaron Durbin8cbf47f2013-12-04 11:03:20 -060088 default:
89 printk(BIOS_DEBUG, "LPE codec clock not required.\n");
90 return;
91 }
92
93 /* Default to always running. */
94 reg |= CLK_CTL_ON;
95
96 if (config->lpe_codec_clk_num < 0 || config->lpe_codec_clk_num > 5) {
97 printk(BIOS_DEBUG, "Invalid LPE codec clock number.\n");
98 return;
99 }
100
101 printk(BIOS_DEBUG, "LPE Audio codec clock set to %sMHz.\n", freq_str);
102
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800103 clk_reg = (u32 *)(PMC_BASE_ADDRESS + PLT_CLK_CTL_0);
104 clk_reg += config->lpe_codec_clk_num;
Aaron Durbin8cbf47f2013-12-04 11:03:20 -0600105
106 write32(clk_reg, (read32(clk_reg) & ~0x7) | reg);
107}
Aaron Durbin97651c52013-11-01 14:36:03 -0500108
Elyes HAOUAS17a3ceb2018-05-22 10:42:28 +0200109static void lpe_stash_firmware_info(struct device *dev)
Shawn Nematbakhsh51d787a2014-01-16 17:52:21 -0800110{
111 struct resource *res;
112 struct resource *mmio;
113 const struct pattrs *pattrs = pattrs_get();
114
Angel Ponsc1bfbe02021-11-03 13:18:53 +0100115 res = probe_resource(dev, FIRMWARE_PCI_REG_BASE);
Shawn Nematbakhsh51d787a2014-01-16 17:52:21 -0800116 if (res == NULL) {
117 printk(BIOS_DEBUG, "LPE Firmware memory not found.\n");
118 return;
119 }
120
121 /* Continue using old way of informing firmware address / size. */
Angel Pons12baf202020-07-07 17:51:17 +0200122 pci_write_config32(dev, FIRMWARE_PCI_REG_BASE, res->base);
Shawn Nematbakhsh51d787a2014-01-16 17:52:21 -0800123 pci_write_config32(dev, FIRMWARE_PCI_REG_LENGTH, res->size);
124
125 /* C0 and later steppings use an offset in the MMIO space. */
126 if (pattrs->stepping >= STEP_C0) {
127 mmio = find_resource(dev, PCI_BASE_ADDRESS_0);
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800128 write32((u32 *)(uintptr_t)(mmio->base + FIRMWARE_REG_BASE_C0),
129 res->base);
130 write32((u32 *)(uintptr_t)(mmio->base + FIRMWARE_REG_LENGTH_C0),
131 res->size);
Shawn Nematbakhsh51d787a2014-01-16 17:52:21 -0800132 }
133}
134
Elyes HAOUAS17a3ceb2018-05-22 10:42:28 +0200135static void lpe_init(struct device *dev)
Aaron Durbin97651c52013-11-01 14:36:03 -0500136{
Kyösti Mälkki8950cfb2019-07-13 22:16:25 +0300137 struct soc_intel_baytrail_config *config = config_of(dev);
Shawn Nematbakhsh51d787a2014-01-16 17:52:21 -0800138
139 lpe_stash_firmware_info(dev);
Aaron Durbin8cbf47f2013-12-04 11:03:20 -0600140 setup_codec_clock(dev);
Shawn Nematbakhsh51d787a2014-01-16 17:52:21 -0800141
142 if (config->lpe_acpi_mode)
143 lpe_enable_acpi_mode(dev);
Aaron Durbin97651c52013-11-01 14:36:03 -0500144}
145
Elyes HAOUAS17a3ceb2018-05-22 10:42:28 +0200146static void lpe_read_resources(struct device *dev)
Aaron Durbinf4fe3c32013-12-09 12:52:37 -0600147{
148 pci_dev_read_resources(dev);
149
Shawn Nematbakhsh51d787a2014-01-16 17:52:21 -0800150 reserved_ram_resource(dev, FIRMWARE_PCI_REG_BASE,
Aaron Durbinf4fe3c32013-12-09 12:52:37 -0600151 FIRMWARE_PHYS_BASE >> 10,
152 FIRMWARE_PHYS_LENGTH >> 10);
153}
154
Aaron Durbin97651c52013-11-01 14:36:03 -0500155static const struct device_operations device_ops = {
Aaron Durbinf4fe3c32013-12-09 12:52:37 -0600156 .read_resources = lpe_read_resources,
Shawn Nematbakhsh51d787a2014-01-16 17:52:21 -0800157 .set_resources = pci_dev_set_resources,
Aaron Durbin4334c872013-12-05 11:12:15 -0600158 .enable_resources = pci_dev_enable_resources,
Aaron Durbin97651c52013-11-01 14:36:03 -0500159 .init = lpe_init,
Aaron Durbin97651c52013-11-01 14:36:03 -0500160 .ops_pci = &soc_pci_ops,
161};
162
163static const struct pci_driver southcluster __pci_driver = {
164 .ops = &device_ops,
Felix Singer43b7f412022-03-07 04:34:52 +0100165 .vendor = PCI_VID_INTEL,
Aaron Durbin97651c52013-11-01 14:36:03 -0500166 .device = LPE_DEVID,
167};