blob: 2830f0033842a275c932b68e4e534adce58828be [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>
Shawn Nematbakhsh51d787a2014-01-16 17:52:21 -080017#include <cbmem.h>
Aaron Durbin97651c52013-11-01 14:36:03 -050018#include <console/console.h>
19#include <device/device.h>
20#include <device/pci.h>
21#include <device/pci_ids.h>
Shawn Nematbakhsh51d787a2014-01-16 17:52:21 -080022#include <reg_script.h>
Aaron Durbin97651c52013-11-01 14:36:03 -050023
Julius Werner18ea2d32014-10-07 16:42:17 -070024#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>
30#include <soc/pmc.h>
31#include <soc/ramstage.h>
Aaron Durbin8cbf47f2013-12-04 11:03:20 -060032#include "chip.h"
33
34
Aaron Durbinf4fe3c32013-12-09 12:52:37 -060035/* The LPE audio devices needs 1MiB of memory reserved aligned to a 512MiB
36 * address. Just take 1MiB @ 512MiB. */
37#define FIRMWARE_PHYS_BASE (512 << 20)
38#define FIRMWARE_PHYS_LENGTH (1 << 20)
Shawn Nematbakhsh51d787a2014-01-16 17:52:21 -080039#define FIRMWARE_PCI_REG_BASE 0xa8
40#define FIRMWARE_PCI_REG_LENGTH 0xac
41#define FIRMWARE_REG_BASE_C0 0x144000
42#define FIRMWARE_REG_LENGTH_C0 (FIRMWARE_REG_BASE_C0 + 4)
43
Elyes HAOUAS17a3ceb2018-05-22 10:42:28 +020044static void assign_device_nvs(struct device *dev, u32 *field, unsigned index)
Shawn Nematbakhsh51d787a2014-01-16 17:52:21 -080045{
46 struct resource *res;
47
48 res = find_resource(dev, index);
49 if (res)
50 *field = res->base;
51}
52
Elyes HAOUAS17a3ceb2018-05-22 10:42:28 +020053static void lpe_enable_acpi_mode(struct device *dev)
Shawn Nematbakhsh51d787a2014-01-16 17:52:21 -080054{
55 static const struct reg_script ops[] = {
56 /* Disable PCI interrupt, enable Memory and Bus Master */
57 REG_PCI_OR32(PCI_COMMAND,
58 PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER | (1<<10)),
59 /* Enable ACPI mode */
60 REG_IOSF_OR(IOSF_PORT_0x58, LPE_PCICFGCTR1,
61 LPE_PCICFGCTR1_PCI_CFG_DIS |
62 LPE_PCICFGCTR1_ACPI_INT_EN),
63 REG_SCRIPT_END
64 };
65 global_nvs_t *gnvs;
66
67 /* Find ACPI NVS to update BARs */
68 gnvs = (global_nvs_t *)cbmem_find(CBMEM_ID_ACPI_GNVS);
69 if (!gnvs) {
70 printk(BIOS_ERR, "Unable to locate Global NVS\n");
71 return;
72 }
73
74 /* Save BAR0, BAR1, and firmware base to ACPI NVS */
75 assign_device_nvs(dev, &gnvs->dev.lpe_bar0, PCI_BASE_ADDRESS_0);
76 assign_device_nvs(dev, &gnvs->dev.lpe_bar1, PCI_BASE_ADDRESS_1);
77 assign_device_nvs(dev, &gnvs->dev.lpe_fw, FIRMWARE_PCI_REG_BASE);
78
79 /* Device is enabled in ACPI mode */
80 gnvs->dev.lpe_en = 1;
81
82 /* Put device in ACPI mode */
83 reg_script_run_on_dev(dev, ops);
84}
Aaron Durbinf4fe3c32013-12-09 12:52:37 -060085
Elyes HAOUAS17a3ceb2018-05-22 10:42:28 +020086static void setup_codec_clock(struct device *dev)
Aaron Durbin8cbf47f2013-12-04 11:03:20 -060087{
88 uint32_t reg;
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080089 u32 *clk_reg;
Aaron Durbin8cbf47f2013-12-04 11:03:20 -060090 struct soc_intel_baytrail_config *config;
91 const char *freq_str;
92
93 config = dev->chip_info;
94 switch (config->lpe_codec_clk_freq) {
95 case 19:
96 freq_str = "19.2";
97 reg = CLK_FREQ_19P2MHZ;
98 break;
99 case 25:
100 freq_str = "25";
101 reg = CLK_FREQ_25MHZ;
102 break;
103 default:
104 printk(BIOS_DEBUG, "LPE codec clock not required.\n");
105 return;
106 }
107
108 /* Default to always running. */
109 reg |= CLK_CTL_ON;
110
111 if (config->lpe_codec_clk_num < 0 || config->lpe_codec_clk_num > 5) {
112 printk(BIOS_DEBUG, "Invalid LPE codec clock number.\n");
113 return;
114 }
115
116 printk(BIOS_DEBUG, "LPE Audio codec clock set to %sMHz.\n", freq_str);
117
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800118 clk_reg = (u32 *)(PMC_BASE_ADDRESS + PLT_CLK_CTL_0);
119 clk_reg += config->lpe_codec_clk_num;
Aaron Durbin8cbf47f2013-12-04 11:03:20 -0600120
121 write32(clk_reg, (read32(clk_reg) & ~0x7) | reg);
122}
Aaron Durbin97651c52013-11-01 14:36:03 -0500123
Elyes HAOUAS17a3ceb2018-05-22 10:42:28 +0200124static void lpe_stash_firmware_info(struct device *dev)
Shawn Nematbakhsh51d787a2014-01-16 17:52:21 -0800125{
126 struct resource *res;
127 struct resource *mmio;
128 const struct pattrs *pattrs = pattrs_get();
129
130 res = find_resource(dev, FIRMWARE_PCI_REG_BASE);
131 if (res == NULL) {
132 printk(BIOS_DEBUG, "LPE Firmware memory not found.\n");
133 return;
134 }
135
136 /* Continue using old way of informing firmware address / size. */
137 pci_write_config32(dev, FIRMWARE_PCI_REG_BASE, res->base);
138 pci_write_config32(dev, FIRMWARE_PCI_REG_LENGTH, res->size);
139
140 /* C0 and later steppings use an offset in the MMIO space. */
141 if (pattrs->stepping >= STEP_C0) {
142 mmio = find_resource(dev, PCI_BASE_ADDRESS_0);
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800143 write32((u32 *)(uintptr_t)(mmio->base + FIRMWARE_REG_BASE_C0),
144 res->base);
145 write32((u32 *)(uintptr_t)(mmio->base + FIRMWARE_REG_LENGTH_C0),
146 res->size);
Shawn Nematbakhsh51d787a2014-01-16 17:52:21 -0800147 }
148}
149
Elyes HAOUAS17a3ceb2018-05-22 10:42:28 +0200150static void lpe_init(struct device *dev)
Aaron Durbin97651c52013-11-01 14:36:03 -0500151{
Shawn Nematbakhsh51d787a2014-01-16 17:52:21 -0800152 struct soc_intel_baytrail_config *config = dev->chip_info;
153
154 lpe_stash_firmware_info(dev);
155
Aaron Durbin8cbf47f2013-12-04 11:03:20 -0600156 setup_codec_clock(dev);
Shawn Nematbakhsh51d787a2014-01-16 17:52:21 -0800157
158 if (config->lpe_acpi_mode)
159 lpe_enable_acpi_mode(dev);
Aaron Durbin97651c52013-11-01 14:36:03 -0500160}
161
Elyes HAOUAS17a3ceb2018-05-22 10:42:28 +0200162static void lpe_read_resources(struct device *dev)
Aaron Durbinf4fe3c32013-12-09 12:52:37 -0600163{
164 pci_dev_read_resources(dev);
165
Shawn Nematbakhsh51d787a2014-01-16 17:52:21 -0800166 reserved_ram_resource(dev, FIRMWARE_PCI_REG_BASE,
Aaron Durbinf4fe3c32013-12-09 12:52:37 -0600167 FIRMWARE_PHYS_BASE >> 10,
168 FIRMWARE_PHYS_LENGTH >> 10);
169}
170
Aaron Durbin97651c52013-11-01 14:36:03 -0500171static const struct device_operations device_ops = {
Aaron Durbinf4fe3c32013-12-09 12:52:37 -0600172 .read_resources = lpe_read_resources,
Shawn Nematbakhsh51d787a2014-01-16 17:52:21 -0800173 .set_resources = pci_dev_set_resources,
Aaron Durbin4334c872013-12-05 11:12:15 -0600174 .enable_resources = pci_dev_enable_resources,
Aaron Durbin97651c52013-11-01 14:36:03 -0500175 .init = lpe_init,
176 .enable = NULL,
177 .scan_bus = NULL,
178 .ops_pci = &soc_pci_ops,
179};
180
181static const struct pci_driver southcluster __pci_driver = {
182 .ops = &device_ops,
183 .vendor = PCI_VENDOR_ID_INTEL,
184 .device = LPE_DEVID,
185};