blob: e5b0e8788c6de6d1022580cc2dbfd84dc5d4b115 [file] [log] [blame]
Angel Ponsc3f58f62020-04-05 15:46:41 +02001/* SPDX-License-Identifier: GPL-2.0-only */
2/* This file is part of the coreboot project. */
Aaron Durbin97651c52013-11-01 14:36:03 -05003
Kyösti Mälkki13f66502019-03-03 08:01:05 +02004#include <device/mmio.h>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +02005#include <device/pci_ops.h>
Shawn Nematbakhsh51d787a2014-01-16 17:52:21 -08006#include <cbmem.h>
Aaron Durbin97651c52013-11-01 14:36:03 -05007#include <console/console.h>
8#include <device/device.h>
9#include <device/pci.h>
10#include <device/pci_ids.h>
Shawn Nematbakhsh51d787a2014-01-16 17:52:21 -080011#include <reg_script.h>
Aaron Durbin97651c52013-11-01 14:36:03 -050012
Julius Werner18ea2d32014-10-07 16:42:17 -070013#include <soc/iomap.h>
14#include <soc/iosf.h>
15#include <soc/lpc.h>
16#include <soc/nvs.h>
17#include <soc/pattrs.h>
18#include <soc/pci_devs.h>
19#include <soc/pmc.h>
20#include <soc/ramstage.h>
Aaron Durbin8cbf47f2013-12-04 11:03:20 -060021#include "chip.h"
22
23
Aaron Durbinf4fe3c32013-12-09 12:52:37 -060024/* The LPE audio devices needs 1MiB of memory reserved aligned to a 512MiB
25 * address. Just take 1MiB @ 512MiB. */
26#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
37 res = find_resource(dev, index);
38 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,
Shawn Nematbakhsh51d787a2014-01-16 17:52:21 -080047 PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER | (1<<10)),
48 /* Enable ACPI mode */
49 REG_IOSF_OR(IOSF_PORT_0x58, LPE_PCICFGCTR1,
50 LPE_PCICFGCTR1_PCI_CFG_DIS |
51 LPE_PCICFGCTR1_ACPI_INT_EN),
52 REG_SCRIPT_END
53 };
54 global_nvs_t *gnvs;
55
56 /* Find ACPI NVS to update BARs */
57 gnvs = (global_nvs_t *)cbmem_find(CBMEM_ID_ACPI_GNVS);
58 if (!gnvs) {
59 printk(BIOS_ERR, "Unable to locate Global NVS\n");
60 return;
61 }
62
63 /* Save BAR0, BAR1, and firmware base to ACPI NVS */
64 assign_device_nvs(dev, &gnvs->dev.lpe_bar0, PCI_BASE_ADDRESS_0);
65 assign_device_nvs(dev, &gnvs->dev.lpe_bar1, PCI_BASE_ADDRESS_1);
66 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}
Aaron Durbinf4fe3c32013-12-09 12:52:37 -060074
Elyes HAOUAS17a3ceb2018-05-22 10:42:28 +020075static void setup_codec_clock(struct device *dev)
Aaron Durbin8cbf47f2013-12-04 11:03:20 -060076{
77 uint32_t reg;
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080078 u32 *clk_reg;
Aaron Durbin8cbf47f2013-12-04 11:03:20 -060079 struct soc_intel_baytrail_config *config;
80 const char *freq_str;
81
Kyösti Mälkki8950cfb2019-07-13 22:16:25 +030082 config = config_of(dev);
Aaron Durbin8cbf47f2013-12-04 11:03:20 -060083 switch (config->lpe_codec_clk_freq) {
84 case 19:
85 freq_str = "19.2";
86 reg = CLK_FREQ_19P2MHZ;
87 break;
88 case 25:
89 freq_str = "25";
90 reg = CLK_FREQ_25MHZ;
91 break;
92 default:
93 printk(BIOS_DEBUG, "LPE codec clock not required.\n");
94 return;
95 }
96
97 /* Default to always running. */
98 reg |= CLK_CTL_ON;
99
100 if (config->lpe_codec_clk_num < 0 || config->lpe_codec_clk_num > 5) {
101 printk(BIOS_DEBUG, "Invalid LPE codec clock number.\n");
102 return;
103 }
104
105 printk(BIOS_DEBUG, "LPE Audio codec clock set to %sMHz.\n", freq_str);
106
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800107 clk_reg = (u32 *)(PMC_BASE_ADDRESS + PLT_CLK_CTL_0);
108 clk_reg += config->lpe_codec_clk_num;
Aaron Durbin8cbf47f2013-12-04 11:03:20 -0600109
110 write32(clk_reg, (read32(clk_reg) & ~0x7) | reg);
111}
Aaron Durbin97651c52013-11-01 14:36:03 -0500112
Elyes HAOUAS17a3ceb2018-05-22 10:42:28 +0200113static void lpe_stash_firmware_info(struct device *dev)
Shawn Nematbakhsh51d787a2014-01-16 17:52:21 -0800114{
115 struct resource *res;
116 struct resource *mmio;
117 const struct pattrs *pattrs = pattrs_get();
118
119 res = find_resource(dev, FIRMWARE_PCI_REG_BASE);
120 if (res == NULL) {
121 printk(BIOS_DEBUG, "LPE Firmware memory not found.\n");
122 return;
123 }
124
125 /* Continue using old way of informing firmware address / size. */
126 pci_write_config32(dev, FIRMWARE_PCI_REG_BASE, res->base);
127 pci_write_config32(dev, FIRMWARE_PCI_REG_LENGTH, res->size);
128
129 /* C0 and later steppings use an offset in the MMIO space. */
130 if (pattrs->stepping >= STEP_C0) {
131 mmio = find_resource(dev, PCI_BASE_ADDRESS_0);
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800132 write32((u32 *)(uintptr_t)(mmio->base + FIRMWARE_REG_BASE_C0),
133 res->base);
134 write32((u32 *)(uintptr_t)(mmio->base + FIRMWARE_REG_LENGTH_C0),
135 res->size);
Shawn Nematbakhsh51d787a2014-01-16 17:52:21 -0800136 }
137}
138
Elyes HAOUAS17a3ceb2018-05-22 10:42:28 +0200139static void lpe_init(struct device *dev)
Aaron Durbin97651c52013-11-01 14:36:03 -0500140{
Kyösti Mälkki8950cfb2019-07-13 22:16:25 +0300141 struct soc_intel_baytrail_config *config = config_of(dev);
Shawn Nematbakhsh51d787a2014-01-16 17:52:21 -0800142
143 lpe_stash_firmware_info(dev);
144
Aaron Durbin8cbf47f2013-12-04 11:03:20 -0600145 setup_codec_clock(dev);
Shawn Nematbakhsh51d787a2014-01-16 17:52:21 -0800146
147 if (config->lpe_acpi_mode)
148 lpe_enable_acpi_mode(dev);
Aaron Durbin97651c52013-11-01 14:36:03 -0500149}
150
Elyes HAOUAS17a3ceb2018-05-22 10:42:28 +0200151static void lpe_read_resources(struct device *dev)
Aaron Durbinf4fe3c32013-12-09 12:52:37 -0600152{
153 pci_dev_read_resources(dev);
154
Shawn Nematbakhsh51d787a2014-01-16 17:52:21 -0800155 reserved_ram_resource(dev, FIRMWARE_PCI_REG_BASE,
Aaron Durbinf4fe3c32013-12-09 12:52:37 -0600156 FIRMWARE_PHYS_BASE >> 10,
157 FIRMWARE_PHYS_LENGTH >> 10);
158}
159
Aaron Durbin97651c52013-11-01 14:36:03 -0500160static const struct device_operations device_ops = {
Aaron Durbinf4fe3c32013-12-09 12:52:37 -0600161 .read_resources = lpe_read_resources,
Shawn Nematbakhsh51d787a2014-01-16 17:52:21 -0800162 .set_resources = pci_dev_set_resources,
Aaron Durbin4334c872013-12-05 11:12:15 -0600163 .enable_resources = pci_dev_enable_resources,
Aaron Durbin97651c52013-11-01 14:36:03 -0500164 .init = lpe_init,
Aaron Durbin97651c52013-11-01 14:36:03 -0500165 .ops_pci = &soc_pci_ops,
166};
167
168static const struct pci_driver southcluster __pci_driver = {
169 .ops = &device_ops,
170 .vendor = PCI_VENDOR_ID_INTEL,
171 .device = LPE_DEVID,
172};