blob: caa37103bb02ecd11db27fdff0af8ccfab5a9356 [file] [log] [blame]
Aaron Durbin97651c52013-11-01 14:36:03 -05001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2013 Google Inc.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
Aaron Durbin97651c52013-11-01 14:36:03 -050014 */
15
Aaron Durbin8cbf47f2013-12-04 11:03:20 -060016#include <arch/io.h>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +020017#include <device/pci_ops.h>
Shawn Nematbakhsh51d787a2014-01-16 17:52:21 -080018#include <cbmem.h>
Aaron Durbin97651c52013-11-01 14:36:03 -050019#include <console/console.h>
20#include <device/device.h>
21#include <device/pci.h>
22#include <device/pci_ids.h>
Shawn Nematbakhsh51d787a2014-01-16 17:52:21 -080023#include <reg_script.h>
Aaron Durbin97651c52013-11-01 14:36:03 -050024
Julius Werner18ea2d32014-10-07 16:42:17 -070025#include <soc/iomap.h>
26#include <soc/iosf.h>
27#include <soc/lpc.h>
28#include <soc/nvs.h>
29#include <soc/pattrs.h>
30#include <soc/pci_devs.h>
31#include <soc/pmc.h>
32#include <soc/ramstage.h>
Aaron Durbin8cbf47f2013-12-04 11:03:20 -060033#include "chip.h"
34
35
Aaron Durbinf4fe3c32013-12-09 12:52:37 -060036/* The LPE audio devices needs 1MiB of memory reserved aligned to a 512MiB
37 * address. Just take 1MiB @ 512MiB. */
38#define FIRMWARE_PHYS_BASE (512 << 20)
39#define FIRMWARE_PHYS_LENGTH (1 << 20)
Shawn Nematbakhsh51d787a2014-01-16 17:52:21 -080040#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 HAOUAS17a3ceb2018-05-22 10:42:28 +020045static void assign_device_nvs(struct device *dev, u32 *field, unsigned index)
Shawn Nematbakhsh51d787a2014-01-16 17:52:21 -080046{
47 struct resource *res;
48
49 res = find_resource(dev, index);
50 if (res)
51 *field = res->base;
52}
53
Elyes HAOUAS17a3ceb2018-05-22 10:42:28 +020054static void lpe_enable_acpi_mode(struct device *dev)
Shawn Nematbakhsh51d787a2014-01-16 17:52:21 -080055{
56 static const struct reg_script ops[] = {
57 /* Disable PCI interrupt, enable Memory and Bus Master */
58 REG_PCI_OR32(PCI_COMMAND,
59 PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER | (1<<10)),
60 /* Enable ACPI mode */
61 REG_IOSF_OR(IOSF_PORT_0x58, LPE_PCICFGCTR1,
62 LPE_PCICFGCTR1_PCI_CFG_DIS |
63 LPE_PCICFGCTR1_ACPI_INT_EN),
64 REG_SCRIPT_END
65 };
66 global_nvs_t *gnvs;
67
68 /* Find ACPI NVS to update BARs */
69 gnvs = (global_nvs_t *)cbmem_find(CBMEM_ID_ACPI_GNVS);
70 if (!gnvs) {
71 printk(BIOS_ERR, "Unable to locate Global NVS\n");
72 return;
73 }
74
75 /* Save BAR0, BAR1, and firmware base to ACPI NVS */
76 assign_device_nvs(dev, &gnvs->dev.lpe_bar0, PCI_BASE_ADDRESS_0);
77 assign_device_nvs(dev, &gnvs->dev.lpe_bar1, PCI_BASE_ADDRESS_1);
78 assign_device_nvs(dev, &gnvs->dev.lpe_fw, FIRMWARE_PCI_REG_BASE);
79
80 /* Device is enabled in ACPI mode */
81 gnvs->dev.lpe_en = 1;
82
83 /* Put device in ACPI mode */
84 reg_script_run_on_dev(dev, ops);
85}
Aaron Durbinf4fe3c32013-12-09 12:52:37 -060086
Elyes HAOUAS17a3ceb2018-05-22 10:42:28 +020087static void setup_codec_clock(struct device *dev)
Aaron Durbin8cbf47f2013-12-04 11:03:20 -060088{
89 uint32_t reg;
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080090 u32 *clk_reg;
Aaron Durbin8cbf47f2013-12-04 11:03:20 -060091 struct soc_intel_baytrail_config *config;
92 const char *freq_str;
93
94 config = dev->chip_info;
95 switch (config->lpe_codec_clk_freq) {
96 case 19:
97 freq_str = "19.2";
98 reg = CLK_FREQ_19P2MHZ;
99 break;
100 case 25:
101 freq_str = "25";
102 reg = CLK_FREQ_25MHZ;
103 break;
104 default:
105 printk(BIOS_DEBUG, "LPE codec clock not required.\n");
106 return;
107 }
108
109 /* Default to always running. */
110 reg |= CLK_CTL_ON;
111
112 if (config->lpe_codec_clk_num < 0 || config->lpe_codec_clk_num > 5) {
113 printk(BIOS_DEBUG, "Invalid LPE codec clock number.\n");
114 return;
115 }
116
117 printk(BIOS_DEBUG, "LPE Audio codec clock set to %sMHz.\n", freq_str);
118
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800119 clk_reg = (u32 *)(PMC_BASE_ADDRESS + PLT_CLK_CTL_0);
120 clk_reg += config->lpe_codec_clk_num;
Aaron Durbin8cbf47f2013-12-04 11:03:20 -0600121
122 write32(clk_reg, (read32(clk_reg) & ~0x7) | reg);
123}
Aaron Durbin97651c52013-11-01 14:36:03 -0500124
Elyes HAOUAS17a3ceb2018-05-22 10:42:28 +0200125static void lpe_stash_firmware_info(struct device *dev)
Shawn Nematbakhsh51d787a2014-01-16 17:52:21 -0800126{
127 struct resource *res;
128 struct resource *mmio;
129 const struct pattrs *pattrs = pattrs_get();
130
131 res = find_resource(dev, FIRMWARE_PCI_REG_BASE);
132 if (res == NULL) {
133 printk(BIOS_DEBUG, "LPE Firmware memory not found.\n");
134 return;
135 }
136
137 /* Continue using old way of informing firmware address / size. */
138 pci_write_config32(dev, FIRMWARE_PCI_REG_BASE, res->base);
139 pci_write_config32(dev, FIRMWARE_PCI_REG_LENGTH, res->size);
140
141 /* C0 and later steppings use an offset in the MMIO space. */
142 if (pattrs->stepping >= STEP_C0) {
143 mmio = find_resource(dev, PCI_BASE_ADDRESS_0);
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800144 write32((u32 *)(uintptr_t)(mmio->base + FIRMWARE_REG_BASE_C0),
145 res->base);
146 write32((u32 *)(uintptr_t)(mmio->base + FIRMWARE_REG_LENGTH_C0),
147 res->size);
Shawn Nematbakhsh51d787a2014-01-16 17:52:21 -0800148 }
149}
150
Elyes HAOUAS17a3ceb2018-05-22 10:42:28 +0200151static void lpe_init(struct device *dev)
Aaron Durbin97651c52013-11-01 14:36:03 -0500152{
Shawn Nematbakhsh51d787a2014-01-16 17:52:21 -0800153 struct soc_intel_baytrail_config *config = dev->chip_info;
154
155 lpe_stash_firmware_info(dev);
156
Aaron Durbin8cbf47f2013-12-04 11:03:20 -0600157 setup_codec_clock(dev);
Shawn Nematbakhsh51d787a2014-01-16 17:52:21 -0800158
159 if (config->lpe_acpi_mode)
160 lpe_enable_acpi_mode(dev);
Aaron Durbin97651c52013-11-01 14:36:03 -0500161}
162
Elyes HAOUAS17a3ceb2018-05-22 10:42:28 +0200163static void lpe_read_resources(struct device *dev)
Aaron Durbinf4fe3c32013-12-09 12:52:37 -0600164{
165 pci_dev_read_resources(dev);
166
Shawn Nematbakhsh51d787a2014-01-16 17:52:21 -0800167 reserved_ram_resource(dev, FIRMWARE_PCI_REG_BASE,
Aaron Durbinf4fe3c32013-12-09 12:52:37 -0600168 FIRMWARE_PHYS_BASE >> 10,
169 FIRMWARE_PHYS_LENGTH >> 10);
170}
171
Aaron Durbin97651c52013-11-01 14:36:03 -0500172static const struct device_operations device_ops = {
Aaron Durbinf4fe3c32013-12-09 12:52:37 -0600173 .read_resources = lpe_read_resources,
Shawn Nematbakhsh51d787a2014-01-16 17:52:21 -0800174 .set_resources = pci_dev_set_resources,
Aaron Durbin4334c872013-12-05 11:12:15 -0600175 .enable_resources = pci_dev_enable_resources,
Aaron Durbin97651c52013-11-01 14:36:03 -0500176 .init = lpe_init,
177 .enable = NULL,
178 .scan_bus = NULL,
179 .ops_pci = &soc_pci_ops,
180};
181
182static const struct pci_driver southcluster __pci_driver = {
183 .ops = &device_ops,
184 .vendor = PCI_VENDOR_ID_INTEL,
185 .device = LPE_DEVID,
186};