blob: 85e698c535aeb0730bb1fb06387f4e6acbc8c20b [file] [log] [blame]
Lee Leahy77ff0b12015-05-05 15:07:29 -07001/*
2 * This file is part of the coreboot project.
3 *
Lee Leahy77ff0b12015-05-05 15:07:29 -07004 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; version 2 of the License.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
Lee Leahy77ff0b12015-05-05 15:07:29 -070013 */
14
Kyösti Mälkki13f66502019-03-03 08:01:05 +020015#include <device/mmio.h>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +020016#include <device/pci_ops.h>
Lee Leahy77ff0b12015-05-05 15:07:29 -070017#include <cbmem.h>
18#include <console/console.h>
19#include <device/device.h>
20#include <device/pci.h>
21#include <device/pci_ids.h>
22#include <reg_script.h>
23
24#include <soc/iomap.h>
25#include <soc/iosf.h>
26#include <soc/lpc.h>
27#include <soc/nvs.h>
28#include <soc/pattrs.h>
29#include <soc/pci_devs.h>
Lee Leahy32471722015-04-20 15:20:28 -070030#include <soc/pm.h>
Lee Leahy77ff0b12015-05-05 15:07:29 -070031#include <soc/ramstage.h>
32#include "chip.h"
33
Lee Leahy32471722015-04-20 15:20:28 -070034/*
35 * The LPE audio devices needs 1MiB of memory reserved aligned to a 512MiB
36 * address. Just take 1MiB @ 512MiB.
37 */
Lee Leahy77ff0b12015-05-05 15:07:29 -070038#define FIRMWARE_PHYS_BASE (512 << 20)
Matt DeVillier83ef07a2018-01-21 16:37:24 -060039#define FIRMWARE_PHYS_LENGTH (2 << 20)
Lee Leahy77ff0b12015-05-05 15:07:29 -070040#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
Elyes HAOUASb13fac32018-05-24 22:29:44 +020045static void assign_device_nvs(struct device *dev, u32 *field,
46 unsigned int index)
Lee Leahy77ff0b12015-05-05 15:07:29 -070047{
48 struct resource *res;
49
50 res = find_resource(dev, index);
51 if (res)
52 *field = res->base;
53}
54
Elyes HAOUASb13fac32018-05-24 22:29:44 +020055static void lpe_enable_acpi_mode(struct device *dev)
Lee Leahy77ff0b12015-05-05 15:07:29 -070056{
57 static const struct reg_script ops[] = {
58 /* Disable PCI interrupt, enable Memory and Bus Master */
59 REG_PCI_OR32(PCI_COMMAND,
Angel Ponsaee7ab22020-03-19 00:31:58 +010060 PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER | PCI_COMMAND_INT_DISABLE),
61
Lee Leahy77ff0b12015-05-05 15:07:29 -070062 /* Enable ACPI mode */
63 REG_IOSF_OR(IOSF_PORT_0x58, LPE_PCICFGCTR1,
Angel Ponsaee7ab22020-03-19 00:31:58 +010064 LPE_PCICFGCTR1_PCI_CFG_DIS | LPE_PCICFGCTR1_ACPI_INT_EN),
65
Lee Leahy77ff0b12015-05-05 15:07:29 -070066 REG_SCRIPT_END
67 };
68 global_nvs_t *gnvs;
69
70 /* Find ACPI NVS to update BARs */
Lee Leahy32471722015-04-20 15:20:28 -070071 gnvs = cbmem_find(CBMEM_ID_ACPI_GNVS);
Lee Leahy77ff0b12015-05-05 15:07:29 -070072 if (!gnvs) {
73 printk(BIOS_ERR, "Unable to locate Global NVS\n");
74 return;
75 }
76
77 /* Save BAR0, BAR1, and firmware base to ACPI NVS */
78 assign_device_nvs(dev, &gnvs->dev.lpe_bar0, PCI_BASE_ADDRESS_0);
Matt DeVillier5d6ab452018-01-17 19:39:52 -060079 assign_device_nvs(dev, &gnvs->dev.lpe_bar1, PCI_BASE_ADDRESS_2);
Lee Leahy77ff0b12015-05-05 15:07:29 -070080 assign_device_nvs(dev, &gnvs->dev.lpe_fw, FIRMWARE_PCI_REG_BASE);
81
82 /* Device is enabled in ACPI mode */
83 gnvs->dev.lpe_en = 1;
84
85 /* Put device in ACPI mode */
86 reg_script_run_on_dev(dev, ops);
87}
88
Elyes HAOUASb13fac32018-05-24 22:29:44 +020089static void setup_codec_clock(struct device *dev)
Lee Leahy77ff0b12015-05-05 15:07:29 -070090{
91 uint32_t reg;
92 u32 *clk_reg;
Lee Leahy32471722015-04-20 15:20:28 -070093 struct soc_intel_braswell_config *config;
Lee Leahy77ff0b12015-05-05 15:07:29 -070094 const char *freq_str;
95
Kyösti Mälkki8950cfb2019-07-13 22:16:25 +030096 config = config_of(dev);
fdurairxaff502e2015-08-21 15:36:53 -070097 switch (config->lpe_codec_clk_src) {
98 case LPE_CLK_SRC_XTAL:
99 /* XTAL driven bit2=0 */
100 freq_str = "19.2MHz External Crystal";
101 reg = CLK_SRC_XTAL;
Lee Leahy77ff0b12015-05-05 15:07:29 -0700102 break;
Angel Ponsaee7ab22020-03-19 00:31:58 +0100103
fdurairxaff502e2015-08-21 15:36:53 -0700104 case LPE_CLK_SRC_PLL:
105 /* PLL driven bit2=1 */
106 freq_str = "19.2MHz PLL";
107 reg = CLK_SRC_PLL;
Lee Leahy77ff0b12015-05-05 15:07:29 -0700108 break;
Angel Ponsaee7ab22020-03-19 00:31:58 +0100109
Lee Leahy77ff0b12015-05-05 15:07:29 -0700110 default:
fdurairxaff502e2015-08-21 15:36:53 -0700111 reg = CLK_SRC_XTAL;
112 printk(BIOS_DEBUG, "LPE codec clock default to using Crystal\n");
Lee Leahy77ff0b12015-05-05 15:07:29 -0700113 return;
114 }
115
116 /* Default to always running. */
117 reg |= CLK_CTL_ON;
118
Lee Leahy77ff0b12015-05-05 15:07:29 -0700119
120 printk(BIOS_DEBUG, "LPE Audio codec clock set to %sMHz.\n", freq_str);
121
Angel Ponsaee7ab22020-03-19 00:31:58 +0100122 clk_reg = (u32 *)(PMC_BASE_ADDRESS + PLT_CLK_CTL_0);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700123
124 write32(clk_reg, (read32(clk_reg) & ~0x7) | reg);
125}
126
Elyes HAOUASb13fac32018-05-24 22:29:44 +0200127static void lpe_stash_firmware_info(struct device *dev)
Lee Leahy77ff0b12015-05-05 15:07:29 -0700128{
129 struct resource *res;
130 struct resource *mmio;
Lee Leahy77ff0b12015-05-05 15:07:29 -0700131
132 res = find_resource(dev, FIRMWARE_PCI_REG_BASE);
133 if (res == NULL) {
134 printk(BIOS_DEBUG, "LPE Firmware memory not found.\n");
135 return;
136 }
Lee Leahy32471722015-04-20 15:20:28 -0700137 printk(BIOS_DEBUG, "LPE FW Resource: 0x%08x\n", (u32) res->base);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700138
139 /* Continue using old way of informing firmware address / size. */
Angel Ponsaee7ab22020-03-19 00:31:58 +0100140 pci_write_config32(dev, FIRMWARE_PCI_REG_BASE, res->base);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700141 pci_write_config32(dev, FIRMWARE_PCI_REG_LENGTH, res->size);
142
Lee Leahy32471722015-04-20 15:20:28 -0700143 /* Also put the address in MMIO space like on C0 BTM */
144 mmio = find_resource(dev, PCI_BASE_ADDRESS_0);
Angel Ponsaee7ab22020-03-19 00:31:58 +0100145 write32((void *)(uintptr_t)(mmio->base + FIRMWARE_REG_BASE_C0), res->base);
146 write32((void *)(uintptr_t)(mmio->base + FIRMWARE_REG_LENGTH_C0), res->size);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700147}
148
Lee Leahy32471722015-04-20 15:20:28 -0700149
Elyes HAOUASb13fac32018-05-24 22:29:44 +0200150static void lpe_init(struct device *dev)
Lee Leahy77ff0b12015-05-05 15:07:29 -0700151{
Kyösti Mälkki8950cfb2019-07-13 22:16:25 +0300152 struct soc_intel_braswell_config *config = config_of(dev);
Lee Leahy32471722015-04-20 15:20:28 -0700153
Angel Ponsaee7ab22020-03-19 00:31:58 +0100154 printk(BIOS_SPEW, "%s/%s (%s)\n", __FILE__, __func__, dev_name(dev));
Lee Leahy77ff0b12015-05-05 15:07:29 -0700155
156 lpe_stash_firmware_info(dev);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700157 setup_codec_clock(dev);
158
159 if (config->lpe_acpi_mode)
160 lpe_enable_acpi_mode(dev);
161}
162
Elyes HAOUASb13fac32018-05-24 22:29:44 +0200163static void lpe_read_resources(struct device *dev)
Lee Leahy77ff0b12015-05-05 15:07:29 -0700164{
Matt DeVillier5d6ab452018-01-17 19:39:52 -0600165 struct resource *res;
Lee Leahy77ff0b12015-05-05 15:07:29 -0700166 pci_dev_read_resources(dev);
167
Matt DeVillier5d6ab452018-01-17 19:39:52 -0600168 /*
Angel Ponsaee7ab22020-03-19 00:31:58 +0100169 * Allocate the BAR1 resource at index 2 to fulfill the Windows driver
Matt DeVillier5d6ab452018-01-17 19:39:52 -0600170 * interface requirements even though the PCI device has only one BAR
171 */
172 res = new_resource(dev, PCI_BASE_ADDRESS_2);
173 res->base = 0;
174 res->size = 0x1000;
175 res->limit = 0xffffffff;
176 res->gran = 12;
177 res->align = 12;
178 res->flags = IORESOURCE_MEM;
179
Angel Ponsaee7ab22020-03-19 00:31:58 +0100180 reserved_ram_resource(dev, FIRMWARE_PCI_REG_BASE, FIRMWARE_PHYS_BASE >> 10,
Lee Leahy77ff0b12015-05-05 15:07:29 -0700181 FIRMWARE_PHYS_LENGTH >> 10);
182}
183
Elyes HAOUASb13fac32018-05-24 22:29:44 +0200184static void lpe_set_resources(struct device *dev)
Matt DeVillier5d6ab452018-01-17 19:39:52 -0600185{
186 struct resource *res;
187
188 res = find_resource(dev, PCI_BASE_ADDRESS_2);
189 if (res != NULL)
190 res->flags |= IORESOURCE_STORED;
191
192 pci_dev_set_resources(dev);
193}
194
Lee Leahy77ff0b12015-05-05 15:07:29 -0700195static const struct device_operations device_ops = {
196 .read_resources = lpe_read_resources,
Matt DeVillier5d6ab452018-01-17 19:39:52 -0600197 .set_resources = lpe_set_resources,
Lee Leahy77ff0b12015-05-05 15:07:29 -0700198 .enable_resources = pci_dev_enable_resources,
199 .init = lpe_init,
200 .enable = NULL,
201 .scan_bus = NULL,
202 .ops_pci = &soc_pci_ops,
203};
204
205static const struct pci_driver southcluster __pci_driver = {
206 .ops = &device_ops,
207 .vendor = PCI_VENDOR_ID_INTEL,
208 .device = LPE_DEVID,
209};