blob: f3391de4433b03c8c6937f4969b5e06462cc0100 [file] [log] [blame]
Angel Ponsba38f372020-04-05 15:46:45 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Lee Leahy77ff0b12015-05-05 15:07:29 -07002
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>
Lee Leahy77ff0b12015-05-05 15:07:29 -07006#include <console/console.h>
7#include <device/device.h>
8#include <device/pci.h>
9#include <device/pci_ids.h>
10#include <reg_script.h>
11
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>
Lee Leahy32471722015-04-20 15:20:28 -070018#include <soc/pm.h>
Lee Leahy77ff0b12015-05-05 15:07:29 -070019#include <soc/ramstage.h>
20#include "chip.h"
21
Lee Leahy32471722015-04-20 15:20:28 -070022/*
23 * The LPE audio devices needs 1MiB of memory reserved aligned to a 512MiB
24 * address. Just take 1MiB @ 512MiB.
25 */
Lee Leahy77ff0b12015-05-05 15:07:29 -070026#define FIRMWARE_PHYS_BASE (512 << 20)
Matt DeVillier83ef07a2018-01-21 16:37:24 -060027#define FIRMWARE_PHYS_LENGTH (2 << 20)
Lee Leahy77ff0b12015-05-05 15:07:29 -070028#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
Elyes HAOUASb13fac32018-05-24 22:29:44 +020033static void assign_device_nvs(struct device *dev, u32 *field,
34 unsigned int index)
Lee Leahy77ff0b12015-05-05 15:07:29 -070035{
36 struct resource *res;
37
38 res = find_resource(dev, index);
39 if (res)
40 *field = res->base;
41}
42
Elyes HAOUASb13fac32018-05-24 22:29:44 +020043static void lpe_enable_acpi_mode(struct device *dev)
Lee Leahy77ff0b12015-05-05 15:07:29 -070044{
45 static const struct reg_script ops[] = {
46 /* Disable PCI interrupt, enable Memory and Bus Master */
Elyes HAOUAS066e61f2020-04-29 10:28:20 +020047 REG_PCI_OR16(PCI_COMMAND,
Angel Ponsaee7ab22020-03-19 00:31:58 +010048 PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER | PCI_COMMAND_INT_DISABLE),
49
Lee Leahy77ff0b12015-05-05 15:07:29 -070050 /* Enable ACPI mode */
51 REG_IOSF_OR(IOSF_PORT_0x58, LPE_PCICFGCTR1,
Angel Ponsaee7ab22020-03-19 00:31:58 +010052 LPE_PCICFGCTR1_PCI_CFG_DIS | LPE_PCICFGCTR1_ACPI_INT_EN),
53
Lee Leahy77ff0b12015-05-05 15:07:29 -070054 REG_SCRIPT_END
55 };
56 global_nvs_t *gnvs;
57
58 /* Find ACPI NVS to update BARs */
Kyösti Mälkki5daa1d32020-06-14 12:01:58 +030059 gnvs = acpi_get_gnvs();
60 if (!gnvs)
Lee Leahy77ff0b12015-05-05 15:07:29 -070061 return;
Lee Leahy77ff0b12015-05-05 15:07:29 -070062
63 /* Save BAR0, BAR1, and firmware base to ACPI NVS */
64 assign_device_nvs(dev, &gnvs->dev.lpe_bar0, PCI_BASE_ADDRESS_0);
Matt DeVillier5d6ab452018-01-17 19:39:52 -060065 assign_device_nvs(dev, &gnvs->dev.lpe_bar1, PCI_BASE_ADDRESS_2);
Lee Leahy77ff0b12015-05-05 15:07:29 -070066 assign_device_nvs(dev, &gnvs->dev.lpe_fw, FIRMWARE_PCI_REG_BASE);
67
68 /* Device is enabled in ACPI mode */
69 gnvs->dev.lpe_en = 1;
70
71 /* Put device in ACPI mode */
72 reg_script_run_on_dev(dev, ops);
73}
74
Elyes HAOUASb13fac32018-05-24 22:29:44 +020075static void setup_codec_clock(struct device *dev)
Lee Leahy77ff0b12015-05-05 15:07:29 -070076{
77 uint32_t reg;
78 u32 *clk_reg;
Lee Leahy32471722015-04-20 15:20:28 -070079 struct soc_intel_braswell_config *config;
Lee Leahy77ff0b12015-05-05 15:07:29 -070080 const char *freq_str;
81
Kyösti Mälkki8950cfb2019-07-13 22:16:25 +030082 config = config_of(dev);
fdurairxaff502e2015-08-21 15:36:53 -070083 switch (config->lpe_codec_clk_src) {
84 case LPE_CLK_SRC_XTAL:
85 /* XTAL driven bit2=0 */
86 freq_str = "19.2MHz External Crystal";
87 reg = CLK_SRC_XTAL;
Lee Leahy77ff0b12015-05-05 15:07:29 -070088 break;
Angel Ponsaee7ab22020-03-19 00:31:58 +010089
fdurairxaff502e2015-08-21 15:36:53 -070090 case LPE_CLK_SRC_PLL:
91 /* PLL driven bit2=1 */
92 freq_str = "19.2MHz PLL";
93 reg = CLK_SRC_PLL;
Lee Leahy77ff0b12015-05-05 15:07:29 -070094 break;
Angel Ponsaee7ab22020-03-19 00:31:58 +010095
Lee Leahy77ff0b12015-05-05 15:07:29 -070096 default:
fdurairxaff502e2015-08-21 15:36:53 -070097 reg = CLK_SRC_XTAL;
98 printk(BIOS_DEBUG, "LPE codec clock default to using Crystal\n");
Lee Leahy77ff0b12015-05-05 15:07:29 -070099 return;
100 }
101
102 /* Default to always running. */
103 reg |= CLK_CTL_ON;
104
Lee Leahy77ff0b12015-05-05 15:07:29 -0700105
106 printk(BIOS_DEBUG, "LPE Audio codec clock set to %sMHz.\n", freq_str);
107
Angel Ponsaee7ab22020-03-19 00:31:58 +0100108 clk_reg = (u32 *)(PMC_BASE_ADDRESS + PLT_CLK_CTL_0);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700109
110 write32(clk_reg, (read32(clk_reg) & ~0x7) | reg);
111}
112
Elyes HAOUASb13fac32018-05-24 22:29:44 +0200113static void lpe_stash_firmware_info(struct device *dev)
Lee Leahy77ff0b12015-05-05 15:07:29 -0700114{
115 struct resource *res;
116 struct resource *mmio;
Lee Leahy77ff0b12015-05-05 15:07:29 -0700117
118 res = find_resource(dev, FIRMWARE_PCI_REG_BASE);
119 if (res == NULL) {
120 printk(BIOS_DEBUG, "LPE Firmware memory not found.\n");
121 return;
122 }
Lee Leahy32471722015-04-20 15:20:28 -0700123 printk(BIOS_DEBUG, "LPE FW Resource: 0x%08x\n", (u32) res->base);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700124
125 /* Continue using old way of informing firmware address / size. */
Angel Ponsaee7ab22020-03-19 00:31:58 +0100126 pci_write_config32(dev, FIRMWARE_PCI_REG_BASE, res->base);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700127 pci_write_config32(dev, FIRMWARE_PCI_REG_LENGTH, res->size);
128
Lee Leahy32471722015-04-20 15:20:28 -0700129 /* Also put the address in MMIO space like on C0 BTM */
130 mmio = find_resource(dev, PCI_BASE_ADDRESS_0);
Angel Ponsaee7ab22020-03-19 00:31:58 +0100131 write32((void *)(uintptr_t)(mmio->base + FIRMWARE_REG_BASE_C0), res->base);
132 write32((void *)(uintptr_t)(mmio->base + FIRMWARE_REG_LENGTH_C0), res->size);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700133}
134
Lee Leahy32471722015-04-20 15:20:28 -0700135
Elyes HAOUASb13fac32018-05-24 22:29:44 +0200136static void lpe_init(struct device *dev)
Lee Leahy77ff0b12015-05-05 15:07:29 -0700137{
Kyösti Mälkki8950cfb2019-07-13 22:16:25 +0300138 struct soc_intel_braswell_config *config = config_of(dev);
Lee Leahy32471722015-04-20 15:20:28 -0700139
Angel Ponsaee7ab22020-03-19 00:31:58 +0100140 printk(BIOS_SPEW, "%s/%s (%s)\n", __FILE__, __func__, dev_name(dev));
Lee Leahy77ff0b12015-05-05 15:07:29 -0700141
142 lpe_stash_firmware_info(dev);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700143 setup_codec_clock(dev);
144
145 if (config->lpe_acpi_mode)
146 lpe_enable_acpi_mode(dev);
147}
148
Elyes HAOUASb13fac32018-05-24 22:29:44 +0200149static void lpe_read_resources(struct device *dev)
Lee Leahy77ff0b12015-05-05 15:07:29 -0700150{
Matt DeVillier5d6ab452018-01-17 19:39:52 -0600151 struct resource *res;
Lee Leahy77ff0b12015-05-05 15:07:29 -0700152 pci_dev_read_resources(dev);
153
Matt DeVillier5d6ab452018-01-17 19:39:52 -0600154 /*
Angel Ponsaee7ab22020-03-19 00:31:58 +0100155 * Allocate the BAR1 resource at index 2 to fulfill the Windows driver
Matt DeVillier5d6ab452018-01-17 19:39:52 -0600156 * interface requirements even though the PCI device has only one BAR
157 */
158 res = new_resource(dev, PCI_BASE_ADDRESS_2);
159 res->base = 0;
160 res->size = 0x1000;
161 res->limit = 0xffffffff;
162 res->gran = 12;
163 res->align = 12;
164 res->flags = IORESOURCE_MEM;
165
Angel Ponsaee7ab22020-03-19 00:31:58 +0100166 reserved_ram_resource(dev, FIRMWARE_PCI_REG_BASE, FIRMWARE_PHYS_BASE >> 10,
Lee Leahy77ff0b12015-05-05 15:07:29 -0700167 FIRMWARE_PHYS_LENGTH >> 10);
168}
169
Elyes HAOUASb13fac32018-05-24 22:29:44 +0200170static void lpe_set_resources(struct device *dev)
Matt DeVillier5d6ab452018-01-17 19:39:52 -0600171{
172 struct resource *res;
173
174 res = find_resource(dev, PCI_BASE_ADDRESS_2);
175 if (res != NULL)
176 res->flags |= IORESOURCE_STORED;
177
178 pci_dev_set_resources(dev);
179}
180
Lee Leahy77ff0b12015-05-05 15:07:29 -0700181static const struct device_operations device_ops = {
182 .read_resources = lpe_read_resources,
Matt DeVillier5d6ab452018-01-17 19:39:52 -0600183 .set_resources = lpe_set_resources,
Lee Leahy77ff0b12015-05-05 15:07:29 -0700184 .enable_resources = pci_dev_enable_resources,
185 .init = lpe_init,
Lee Leahy77ff0b12015-05-05 15:07:29 -0700186 .ops_pci = &soc_pci_ops,
187};
188
189static const struct pci_driver southcluster __pci_driver = {
190 .ops = &device_ops,
191 .vendor = PCI_VENDOR_ID_INTEL,
192 .device = LPE_DEVID,
193};