blob: 25868640a4d2092d743cfdae50cd3c42a303126d [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>
Furquan Shaikh76cedd22020-05-02 10:24:23 -07005#include <acpi/acpi.h>
Frans Hendriksad5e0a82019-03-18 13:31:56 +01006#include <arch/ioapic.h>
Lee Leahy77ff0b12015-05-05 15:07:29 -07007#include <bootstate.h>
Lee Leahy32471722015-04-20 15:20:28 -07008#include "chip.h"
Lee Leahy77ff0b12015-05-05 15:07:29 -07009#include <console/console.h>
Lee Leahy77ff0b12015-05-05 15:07:29 -070010#include <device/device.h>
11#include <device/pci.h>
12#include <device/pci_ids.h>
Frans Hendriks2c630172019-04-02 15:06:29 +020013#include <intelblocks/lpc_lib.h>
Frans Hendriks255f35c2018-12-11 15:21:47 +010014#include <pc80/isa-dma.h>
Frans Hendriksbd5233e2018-12-05 15:24:48 +010015#include <pc80/i8254.h>
Frans Hendriks93484132018-12-10 12:38:16 +010016#include <pc80/i8259.h>
Lee Leahy2bc9cee2015-06-30 15:25:44 -070017#include <soc/acpi.h>
Lee Leahy77ff0b12015-05-05 15:07:29 -070018#include <soc/iomap.h>
19#include <soc/irq.h>
20#include <soc/lpc.h>
Lee Leahy77ff0b12015-05-05 15:07:29 -070021#include <soc/pci_devs.h>
Lee Leahy32471722015-04-20 15:20:28 -070022#include <soc/pm.h>
Lee Leahy77ff0b12015-05-05 15:07:29 -070023#include <soc/ramstage.h>
24#include <soc/spi.h>
Lee Leahy32471722015-04-20 15:20:28 -070025#include <spi-generic.h>
26#include <stdint.h>
Arthur Heymans68b6eb72019-10-13 23:26:36 +020027#include <southbridge/intel/common/spi.h>
Hannah Williams3fa80a92017-03-22 16:33:36 -070028
Frans Hendriks2c630172019-04-02 15:06:29 +020029static void sc_set_serial_irqs_mode(struct device *dev, enum serirq_mode mode)
Hannah Williams3fa80a92017-03-22 16:33:36 -070030{
Angel Ponsaee7ab22020-03-19 00:31:58 +010031 u8 *ilb_base = (u8 *)(pci_read_config32(dev, IBASE) & ~0xf);
Frans Hendriks2c630172019-04-02 15:06:29 +020032
33 switch (mode) {
34 case SERIRQ_CONTINUOUS:
35 break;
Angel Ponsaee7ab22020-03-19 00:31:58 +010036
Frans Hendriks2c630172019-04-02 15:06:29 +020037 case SERIRQ_OFF:
Angel Ponsaee7ab22020-03-19 00:31:58 +010038 write32(ilb_base + ILB_OIC, read32(ilb_base + ILB_OIC) & ~SIRQEN);
Frans Hendriks2c630172019-04-02 15:06:29 +020039 break;
Angel Ponsaee7ab22020-03-19 00:31:58 +010040
Frans Hendriks2c630172019-04-02 15:06:29 +020041 case SERIRQ_QUIET:
42 default:
43 write8(ilb_base + SCNT, read8(ilb_base + SCNT) & ~SCNT_MODE);
44 break;
45 }
Hannah Williams3fa80a92017-03-22 16:33:36 -070046}
Lee Leahy77ff0b12015-05-05 15:07:29 -070047
Angel Ponsaee7ab22020-03-19 00:31:58 +010048static inline void add_mmio_resource(struct device *dev, int i, unsigned long addr,
49 unsigned long size)
Lee Leahy77ff0b12015-05-05 15:07:29 -070050{
51 mmio_resource(dev, i, addr >> 10, size >> 10);
52}
53
Elyes HAOUASb13fac32018-05-24 22:29:44 +020054static void sc_add_mmio_resources(struct device *dev)
Lee Leahy77ff0b12015-05-05 15:07:29 -070055{
56 add_mmio_resource(dev, 0xfeb, ABORT_BASE_ADDRESS, ABORT_BASE_SIZE);
57 add_mmio_resource(dev, PBASE, PMC_BASE_ADDRESS, PMC_BASE_SIZE);
58 add_mmio_resource(dev, IOBASE, IO_BASE_ADDRESS, IO_BASE_SIZE);
59 add_mmio_resource(dev, IBASE, ILB_BASE_ADDRESS, ILB_BASE_SIZE);
60 add_mmio_resource(dev, SBASE, SPI_BASE_ADDRESS, SPI_BASE_SIZE);
61 add_mmio_resource(dev, MPBASE, MPHY_BASE_ADDRESS, MPHY_BASE_SIZE);
62 add_mmio_resource(dev, PUBASE, PUNIT_BASE_ADDRESS, PUNIT_BASE_SIZE);
63 add_mmio_resource(dev, RCBA, RCBA_BASE_ADDRESS, RCBA_BASE_SIZE);
Angel Ponsaee7ab22020-03-19 00:31:58 +010064 add_mmio_resource(dev, 0xfff, 0xffffffff - (CONFIG_COREBOOT_ROMSIZE_KB * KiB) + 1,
65 (CONFIG_COREBOOT_ROMSIZE_KB * KiB)); /* BIOS ROM */
66
Frans Hendriksad5e0a82019-03-18 13:31:56 +010067 add_mmio_resource(dev, 0xfec, IO_APIC_ADDR, 0x00001000); /* IOAPIC */
Lee Leahy77ff0b12015-05-05 15:07:29 -070068}
69
70/* Default IO range claimed by the LPC device. The upper bound is exclusive. */
71#define LPC_DEFAULT_IO_RANGE_LOWER 0
72#define LPC_DEFAULT_IO_RANGE_UPPER 0x1000
73
Frans Hendriks2c630172019-04-02 15:06:29 +020074static void sc_enable_serial_irqs(struct device *dev)
75{
76 u8 *ilb_base = (u8 *)(pci_read_config32(dev, IBASE) & ~0xF);
77
78 printk(BIOS_SPEW, "Enable serial irq\n");
79 write32(ilb_base + ILB_OIC, read32(ilb_base + ILB_OIC) | SIRQEN);
80 write8(ilb_base + SCNT, read8(ilb_base + SCNT) | SCNT_MODE);
81}
82
Frans Hendriks93484132018-12-10 12:38:16 +010083/*
Angel Ponsaee7ab22020-03-19 00:31:58 +010084 * Write PCI config space IRQ assignments. PCI devices have the INT_LINE (0x3c) and INT_PIN
85 * (0x3d) registers which report interrupt routing information to operating systems and drivers.
86 * The INT_PIN register is generally read only and reports which interrupt pin A - D it uses.
87 * The INT_LINE register is configurable and reports which IRQ (generally the PIC IRQs 1 - 15)
88 * it will use. This needs to take interrupt pin swizzling on devices that are downstream on
89 * a PCI bridge into account.
Frans Hendriks93484132018-12-10 12:38:16 +010090 *
Angel Ponsaee7ab22020-03-19 00:31:58 +010091 * This function will loop through all enabled PCI devices and program the INT_LINE register
92 * with the correct PIC IRQ number for the INT_PIN that it uses. It then configures each
93 * interrupt in the PIC to be level triggered.
Frans Hendriks93484132018-12-10 12:38:16 +010094 */
95static void write_pci_config_irqs(void)
96{
97 struct device *irq_dev;
98 struct device *targ_dev;
99 uint8_t int_line = 0;
100 uint8_t original_int_pin = 0;
101 uint8_t new_int_pin = 0;
102 uint16_t current_bdf = 0;
103 uint16_t parent_bdf = 0;
104 uint8_t pirq = 0;
105 uint8_t device_num = 0;
106 const struct soc_irq_route *ir = &global_soc_irq_route;
107
108 if (ir == NULL) {
Angel Ponsaee7ab22020-03-19 00:31:58 +0100109 printk(BIOS_WARNING, "Warning: Can't write PCI IRQ assignments "
110 "because 'global_braswell_irq_route' structure does not exist\n");
Frans Hendriks93484132018-12-10 12:38:16 +0100111 return;
112 }
113
114 /*
Angel Ponsaee7ab22020-03-19 00:31:58 +0100115 * Loop through all enabled devices and program their INT_LINE, INT_PIN registers from
116 * values taken from the Interrupt Route registers in the ILB
Frans Hendriks93484132018-12-10 12:38:16 +0100117 */
118 printk(BIOS_DEBUG, "PCI_CFG IRQ: Write PIRQ assignments\n");
119 for (irq_dev = all_devices; irq_dev; irq_dev = irq_dev->next) {
120
121 if ((irq_dev->path.type != DEVICE_PATH_PCI) ||
122 (!irq_dev->enabled))
123 continue;
124
125 current_bdf = irq_dev->path.pci.devfn |
126 irq_dev->bus->secondary << 8;
127
128 /*
129 * Step 1: Get the INT_PIN and device structure to look for
130 * in the pirq_data table defined in the mainboard directory.
131 */
132 targ_dev = NULL;
133 new_int_pin = get_pci_irq_pins(irq_dev, &targ_dev);
134 if (targ_dev == NULL || new_int_pin < 1)
135 continue;
136
137 /* Get the original INT_PIN for record keeping */
138 original_int_pin = pci_read_config8(irq_dev, PCI_INTERRUPT_PIN);
139
140 parent_bdf = targ_dev->path.pci.devfn
141 | targ_dev->bus->secondary << 8;
142 device_num = PCI_SLOT(parent_bdf);
143
144 if (ir->pcidev[device_num] == 0) {
145 printk(BIOS_WARNING,
Angel Ponsaee7ab22020-03-19 00:31:58 +0100146 "Warning: PCI Device %d does not have an IRQ entry, "
147 "skipping it\n", device_num);
Frans Hendriks93484132018-12-10 12:38:16 +0100148 continue;
149 }
150
151 /* Find the PIRQ that is attached to the INT_PIN */
152 pirq = (ir->pcidev[device_num] >> ((new_int_pin - 1) * 4))
153 & 0x7;
154
155 /* Get the INT_LINE this device/function will use */
156 int_line = ir->pic[pirq];
157
158 if (int_line != PIRQ_PIC_IRQDISABLE) {
159 /* Set this IRQ to level triggered */
Angel Ponsaee7ab22020-03-19 00:31:58 +0100160 i8259_configure_irq_trigger(int_line, IRQ_LEVEL_TRIGGERED);
161
Frans Hendriks93484132018-12-10 12:38:16 +0100162 /* Set the Interrupt Line register */
Angel Ponsaee7ab22020-03-19 00:31:58 +0100163 pci_write_config8(irq_dev, PCI_INTERRUPT_LINE, int_line);
Frans Hendriks93484132018-12-10 12:38:16 +0100164 } else {
Angel Ponsaee7ab22020-03-19 00:31:58 +0100165 /* Set the Interrupt line register as 'unknown' or 'unused' */
166 pci_write_config8(irq_dev, PCI_INTERRUPT_LINE, PIRQ_PIC_UNKNOWN_UNUSED);
Frans Hendriks93484132018-12-10 12:38:16 +0100167 }
168
Angel Ponsaee7ab22020-03-19 00:31:58 +0100169 printk(BIOS_SPEW, "\tINT_PIN\t\t: %d (%s)\n", original_int_pin,
170 pin_to_str(original_int_pin));
171
Frans Hendriks93484132018-12-10 12:38:16 +0100172 if (parent_bdf != current_bdf)
Angel Ponsaee7ab22020-03-19 00:31:58 +0100173 printk(BIOS_SPEW, "\tSwizzled to\t: %d (%s)\n", new_int_pin,
174 pin_to_str(new_int_pin));
175
176 printk(BIOS_SPEW, "\tPIRQ\t\t: %c\n\tINT_LINE\t: 0x%X (IRQ %d)\n",
177 'A' + pirq, int_line, int_line);
Frans Hendriks93484132018-12-10 12:38:16 +0100178 }
179 printk(BIOS_DEBUG, "PCI_CFG IRQ: Finished writing PIRQ assignments\n");
180}
181
Lee Leahy77ff0b12015-05-05 15:07:29 -0700182static inline int io_range_in_default(int base, int size)
183{
184 /* Does it start above the range? */
185 if (base >= LPC_DEFAULT_IO_RANGE_UPPER)
186 return 0;
187
188 /* Is it entirely contained? */
Angel Ponsaee7ab22020-03-19 00:31:58 +0100189 if (base >= LPC_DEFAULT_IO_RANGE_LOWER && (base + size) < LPC_DEFAULT_IO_RANGE_UPPER)
Lee Leahy77ff0b12015-05-05 15:07:29 -0700190 return 1;
191
Angel Ponsaee7ab22020-03-19 00:31:58 +0100192 /* This will return not in range for partial overlaps */
Lee Leahy77ff0b12015-05-05 15:07:29 -0700193 return 0;
194}
195
196/*
197 * Note: this function assumes there is no overlap with the default LPC device's
198 * claimed range: LPC_DEFAULT_IO_RANGE_LOWER -> LPC_DEFAULT_IO_RANGE_UPPER.
199 */
Angel Ponsaee7ab22020-03-19 00:31:58 +0100200static void sc_add_io_resource(struct device *dev, int base, int size, int index)
Lee Leahy77ff0b12015-05-05 15:07:29 -0700201{
202 struct resource *res;
203
204 if (io_range_in_default(base, size))
205 return;
206
207 res = new_resource(dev, index);
208 res->base = base;
209 res->size = size;
210 res->flags = IORESOURCE_IO | IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
211}
212
Elyes HAOUASb13fac32018-05-24 22:29:44 +0200213static void sc_add_io_resources(struct device *dev)
Lee Leahy77ff0b12015-05-05 15:07:29 -0700214{
215 struct resource *res;
216
217 /* Add the default claimed IO range for the LPC device. */
218 res = new_resource(dev, 0);
219 res->base = LPC_DEFAULT_IO_RANGE_LOWER;
220 res->size = LPC_DEFAULT_IO_RANGE_UPPER - LPC_DEFAULT_IO_RANGE_LOWER;
221 res->flags = IORESOURCE_IO | IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
222
223 /* GPIO */
Frans Hendriks4b2c12f2018-11-22 07:52:38 +0100224 sc_add_io_resource(dev, GPIO_BASE_ADDRESS, GPIO_BASE_SIZE, GBASE);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700225
226 /* ACPI */
Frans Hendriks4b2c12f2018-11-22 07:52:38 +0100227 sc_add_io_resource(dev, ACPI_BASE_ADDRESS, ACPI_BASE_SIZE, ABASE);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700228}
229
Elyes HAOUASb13fac32018-05-24 22:29:44 +0200230static void sc_read_resources(struct device *dev)
Lee Leahy77ff0b12015-05-05 15:07:29 -0700231{
232 /* Get the normal PCI resources of this device. */
233 pci_dev_read_resources(dev);
234
235 /* Add non-standard MMIO resources. */
236 sc_add_mmio_resources(dev);
237
238 /* Add IO resources. */
239 sc_add_io_resources(dev);
240}
241
Elyes HAOUASb13fac32018-05-24 22:29:44 +0200242static void sc_init(struct device *dev)
Lee Leahy77ff0b12015-05-05 15:07:29 -0700243{
244 int i;
Frans Hendriksb55cd542019-03-06 14:45:12 +0100245 const unsigned long ilb_base = ILB_BASE_ADDRESS;
Angel Ponsaee7ab22020-03-19 00:31:58 +0100246 const unsigned long pr_base = ILB_BASE_ADDRESS + 0x08;
247 const unsigned long ir_base = ILB_BASE_ADDRESS + 0x20;
248
Lee Leahy32471722015-04-20 15:20:28 -0700249 void *gen_pmcon1 = (void *)(PMC_BASE_ADDRESS + GEN_PMCON1);
Lee Leahy32471722015-04-20 15:20:28 -0700250 const struct soc_irq_route *ir = &global_soc_irq_route;
Kyösti Mälkki8950cfb2019-07-13 22:16:25 +0300251 struct soc_intel_braswell_config *config = config_of(dev);
Lee Leahy32471722015-04-20 15:20:28 -0700252
Frans Hendriksb55cd542019-03-06 14:45:12 +0100253 /* Set the value for PCI command register. */
254 pci_write_config16(dev, PCI_COMMAND,
Angel Ponsaee7ab22020-03-19 00:31:58 +0100255 PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER | PCI_COMMAND_SPECIAL);
Frans Hendriksb55cd542019-03-06 14:45:12 +0100256
257 /* Use IRQ9 for SCI Interrupt */
258 write32((void *)(ilb_base + ACTL), 0);
259
Frans Hendriks255f35c2018-12-11 15:21:47 +0100260 isa_dma_init();
261
Frans Hendriks2c630172019-04-02 15:06:29 +0200262 sc_enable_serial_irqs(dev);
263
Lee Leahy77ff0b12015-05-05 15:07:29 -0700264 /* Set up the PIRQ PIC routing based on static config. */
Lee Leahy32471722015-04-20 15:20:28 -0700265 for (i = 0; i < NUM_PIRQS; i++)
Angel Ponsaee7ab22020-03-19 00:31:58 +0100266 write8((void *)(pr_base + i*sizeof(ir->pic[i])), ir->pic[i]);
Lee Leahy32471722015-04-20 15:20:28 -0700267
Lee Leahy77ff0b12015-05-05 15:07:29 -0700268 /* Set up the per device PIRQ routing base on static config. */
Lee Leahy32471722015-04-20 15:20:28 -0700269 for (i = 0; i < NUM_IR_DEVS; i++)
Angel Ponsaee7ab22020-03-19 00:31:58 +0100270 write16((void *)(ir_base + i*sizeof(ir->pcidev[i])), ir->pcidev[i]);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700271
Frans Hendriks93484132018-12-10 12:38:16 +0100272 /* Interrupt 9 should be level triggered (SCI) */
273 i8259_configure_irq_trigger(9, 1);
274
275 for (i = 0; i < NUM_PIRQS; i++) {
276 if (ir->pic[i])
277 i8259_configure_irq_trigger(ir->pic[i], 1);
278 }
Lee Leahy77ff0b12015-05-05 15:07:29 -0700279
Lee Leahy77ff0b12015-05-05 15:07:29 -0700280 if (config->disable_slp_x_stretch_sus_fail) {
281 printk(BIOS_DEBUG, "Disabling slp_x stretching.\n");
Angel Ponsaee7ab22020-03-19 00:31:58 +0100282 write32(gen_pmcon1, read32(gen_pmcon1) | DIS_SLP_X_STRCH_SUS_UP);
283
Lee Leahy77ff0b12015-05-05 15:07:29 -0700284 } else {
Angel Ponsaee7ab22020-03-19 00:31:58 +0100285 write32(gen_pmcon1, read32(gen_pmcon1) & ~DIS_SLP_X_STRCH_SUS_UP);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700286 }
287
Frans Hendriks93484132018-12-10 12:38:16 +0100288 /* Write IRQ assignments to PCI config space */
289 write_pci_config_irqs();
290
291 /* Initialize i8259 pic */
292 setup_i8259();
293
Frans Hendriksbd5233e2018-12-05 15:24:48 +0100294 /* Initialize i8254 timers */
295 setup_i8254();
Frans Hendriks2c630172019-04-02 15:06:29 +0200296
297 sc_set_serial_irqs_mode(dev, config->serirq_mode);
298
Lee Leahy77ff0b12015-05-05 15:07:29 -0700299}
300
301/*
302 * Common code for the south cluster devices.
303 */
304
Frans Hendrikse6bf51f2019-05-01 10:48:31 +0200305/* Set bit in function disable register to hide this device. */
Elyes HAOUASb13fac32018-05-24 22:29:44 +0200306static void sc_disable_devfn(struct device *dev)
Lee Leahy77ff0b12015-05-05 15:07:29 -0700307{
Angel Ponsaee7ab22020-03-19 00:31:58 +0100308 void *func_dis = (void *)(PMC_BASE_ADDRESS + FUNC_DIS);
Lee Leahy32471722015-04-20 15:20:28 -0700309 void *func_dis2 = (void *)(PMC_BASE_ADDRESS + FUNC_DIS2);
Angel Ponsaee7ab22020-03-19 00:31:58 +0100310 uint32_t mask = 0;
Lee Leahy77ff0b12015-05-05 15:07:29 -0700311 uint32_t mask2 = 0;
312
Lee Leahy32471722015-04-20 15:20:28 -0700313#define SET_DIS_MASK(name_) \
314 case PCI_DEVFN(name_ ## _DEV, name_ ## _FUNC): \
315 mask |= name_ ## _DIS
Angel Ponsaee7ab22020-03-19 00:31:58 +0100316
Lee Leahy32471722015-04-20 15:20:28 -0700317#define SET_DIS_MASK2(name_) \
318 case PCI_DEVFN(name_ ## _DEV, name_ ## _FUNC): \
319 mask2 |= name_ ## _DIS
320
Lee Leahy77ff0b12015-05-05 15:07:29 -0700321 switch (dev->path.pci.devfn) {
Lee Leahy32471722015-04-20 15:20:28 -0700322 SET_DIS_MASK(SDIO);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700323 break;
Lee Leahy32471722015-04-20 15:20:28 -0700324 SET_DIS_MASK(SD);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700325 break;
Lee Leahy32471722015-04-20 15:20:28 -0700326 SET_DIS_MASK(SATA);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700327 break;
Lee Leahy32471722015-04-20 15:20:28 -0700328 SET_DIS_MASK(XHCI);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700329 /* Disable super speed PHY when XHCI is not available. */
330 mask2 |= USH_SS_PHY_DIS;
331 break;
Lee Leahy32471722015-04-20 15:20:28 -0700332 SET_DIS_MASK(LPE);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700333 break;
Lee Leahy32471722015-04-20 15:20:28 -0700334 SET_DIS_MASK(MMC);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700335 break;
Lee Leahy32471722015-04-20 15:20:28 -0700336 SET_DIS_MASK(SIO_DMA1);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700337 break;
Lee Leahy32471722015-04-20 15:20:28 -0700338 SET_DIS_MASK(I2C1);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700339 break;
Lee Leahy32471722015-04-20 15:20:28 -0700340 SET_DIS_MASK(I2C2);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700341 break;
Lee Leahy32471722015-04-20 15:20:28 -0700342 SET_DIS_MASK(I2C3);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700343 break;
Lee Leahy32471722015-04-20 15:20:28 -0700344 SET_DIS_MASK(I2C4);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700345 break;
Lee Leahy32471722015-04-20 15:20:28 -0700346 SET_DIS_MASK(I2C5);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700347 break;
Lee Leahy32471722015-04-20 15:20:28 -0700348 SET_DIS_MASK(I2C6);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700349 break;
Lee Leahy32471722015-04-20 15:20:28 -0700350 SET_DIS_MASK(I2C7);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700351 break;
Lee Leahy32471722015-04-20 15:20:28 -0700352 SET_DIS_MASK(TXE);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700353 break;
Lee Leahy32471722015-04-20 15:20:28 -0700354 SET_DIS_MASK(HDA);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700355 break;
Lee Leahy32471722015-04-20 15:20:28 -0700356 SET_DIS_MASK(PCIE_PORT1);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700357 break;
Lee Leahy32471722015-04-20 15:20:28 -0700358 SET_DIS_MASK(PCIE_PORT2);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700359 break;
Lee Leahy32471722015-04-20 15:20:28 -0700360 SET_DIS_MASK(PCIE_PORT3);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700361 break;
Lee Leahy32471722015-04-20 15:20:28 -0700362 SET_DIS_MASK(PCIE_PORT4);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700363 break;
Lee Leahy32471722015-04-20 15:20:28 -0700364 SET_DIS_MASK(SIO_DMA2);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700365 break;
Lee Leahy32471722015-04-20 15:20:28 -0700366 SET_DIS_MASK(PWM1);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700367 break;
Lee Leahy32471722015-04-20 15:20:28 -0700368 SET_DIS_MASK(PWM2);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700369 break;
Lee Leahy32471722015-04-20 15:20:28 -0700370 SET_DIS_MASK(HSUART1);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700371 break;
Lee Leahy32471722015-04-20 15:20:28 -0700372 SET_DIS_MASK(HSUART2);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700373 break;
Lee Leahy32471722015-04-20 15:20:28 -0700374 SET_DIS_MASK(SPI);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700375 break;
Lee Leahy32471722015-04-20 15:20:28 -0700376 SET_DIS_MASK2(SMBUS);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700377 break;
378 }
379
380 if (mask != 0) {
381 write32(func_dis, read32(func_dis) | mask);
Angel Ponsaee7ab22020-03-19 00:31:58 +0100382 /* Ensure posted write hits */
Lee Leahy77ff0b12015-05-05 15:07:29 -0700383 read32(func_dis);
384 }
385
386 if (mask2 != 0) {
387 write32(func_dis2, read32(func_dis2) | mask2);
Angel Ponsaee7ab22020-03-19 00:31:58 +0100388 /* Ensure posted write hits */
Lee Leahy77ff0b12015-05-05 15:07:29 -0700389 read32(func_dis2);
390 }
391}
392
Elyes HAOUASb13fac32018-05-24 22:29:44 +0200393static inline void set_d3hot_bits(struct device *dev, int offset)
Lee Leahy77ff0b12015-05-05 15:07:29 -0700394{
395 uint32_t reg8;
396 printk(BIOS_DEBUG, "Power management CAP offset 0x%x.\n", offset);
397 reg8 = pci_read_config8(dev, offset + 4);
398 reg8 |= 0x3;
399 pci_write_config8(dev, offset + 4, reg8);
400}
401
Lee Leahy32471722015-04-20 15:20:28 -0700402/*
Angel Ponsaee7ab22020-03-19 00:31:58 +0100403 * Parts of the audio subsystem are powered by the HDA device. Thus, one cannot put HDA into
404 * D3Hot. Instead, perform this workaround to make some of the audio paths work for LPE audio.
Lee Leahy32471722015-04-20 15:20:28 -0700405 */
Elyes HAOUASb13fac32018-05-24 22:29:44 +0200406static void hda_work_around(struct device *dev)
Lee Leahy77ff0b12015-05-05 15:07:29 -0700407{
Lee Leahy32471722015-04-20 15:20:28 -0700408 void *gctl = (void *)(TEMP_BASE_ADDRESS + 0x8);
409
Lee Leahy77ff0b12015-05-05 15:07:29 -0700410 /* Need to set magic register 0x43 to 0xd7 in config space. */
411 pci_write_config8(dev, 0x43, 0xd7);
412
Lee Leahy32471722015-04-20 15:20:28 -0700413 /*
Angel Ponsaee7ab22020-03-19 00:31:58 +0100414 * Need to set bit 0 of GCTL to take the device out of reset.
415 * However, that requires setting up the 64-bit BAR.
Lee Leahy32471722015-04-20 15:20:28 -0700416 */
Lee Leahy77ff0b12015-05-05 15:07:29 -0700417 pci_write_config32(dev, PCI_BASE_ADDRESS_0, TEMP_BASE_ADDRESS);
418 pci_write_config32(dev, PCI_BASE_ADDRESS_1, 0);
Elyes HAOUAS066e61f2020-04-29 10:28:20 +0200419 pci_write_config16(dev, PCI_COMMAND, PCI_COMMAND_MEMORY);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700420 write32(gctl, read32(gctl) | 0x1);
Elyes HAOUAS066e61f2020-04-29 10:28:20 +0200421 pci_write_config16(dev, PCI_COMMAND, 0);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700422 pci_write_config32(dev, PCI_BASE_ADDRESS_0, 0);
423}
424
Elyes HAOUASb13fac32018-05-24 22:29:44 +0200425static int place_device_in_d3hot(struct device *dev)
Lee Leahy77ff0b12015-05-05 15:07:29 -0700426{
Lee Leahy1072e7d2017-03-16 17:35:32 -0700427 unsigned int offset;
Lee Leahy77ff0b12015-05-05 15:07:29 -0700428
Lee Leahy32471722015-04-20 15:20:28 -0700429 /*
430 * Parts of the HDA block are used for LPE audio as well.
431 * Therefore assume the HDA will never be put into D3Hot.
432 */
Lee Leahy77ff0b12015-05-05 15:07:29 -0700433 if (dev->path.pci.devfn == PCI_DEVFN(HDA_DEV, HDA_FUNC)) {
434 hda_work_around(dev);
435 return 0;
436 }
437
438 offset = pci_find_capability(dev, PCI_CAP_ID_PM);
439
440 if (offset != 0) {
441 set_d3hot_bits(dev, offset);
442 return 0;
443 }
444
Lee Leahy32471722015-04-20 15:20:28 -0700445 /*
Angel Ponsaee7ab22020-03-19 00:31:58 +0100446 * For some reason some of the devices don't have the capability pointer set correctly.
447 * Work around this by hard coding the offset.
Lee Leahy32471722015-04-20 15:20:28 -0700448 */
449#define DEV_CASE(name_) \
450 case PCI_DEVFN(name_ ## _DEV, name_ ## _FUNC)
451
Lee Leahy77ff0b12015-05-05 15:07:29 -0700452 switch (dev->path.pci.devfn) {
Lee Leahy32471722015-04-20 15:20:28 -0700453 DEV_CASE(SDIO) :
454 DEV_CASE(SD) :
455 DEV_CASE(MMC) :
456 DEV_CASE(LPE) :
457 DEV_CASE(SIO_DMA1) :
458 DEV_CASE(I2C1) :
459 DEV_CASE(I2C2) :
460 DEV_CASE(I2C3) :
461 DEV_CASE(I2C4) :
462 DEV_CASE(I2C5) :
463 DEV_CASE(I2C6) :
464 DEV_CASE(I2C7) :
465 DEV_CASE(SIO_DMA2) :
466 DEV_CASE(PWM1) :
467 DEV_CASE(PWM2) :
468 DEV_CASE(HSUART1) :
469 DEV_CASE(HSUART2) :
470 DEV_CASE(SPI) :
Lee Leahy77ff0b12015-05-05 15:07:29 -0700471 offset = 0x80;
472 break;
Lee Leahy32471722015-04-20 15:20:28 -0700473 DEV_CASE(SATA) :
474 DEV_CASE(XHCI) :
Lee Leahy77ff0b12015-05-05 15:07:29 -0700475 offset = 0x70;
476 break;
Lee Leahy32471722015-04-20 15:20:28 -0700477 DEV_CASE(HDA) :
478 DEV_CASE(SMBUS) :
Lee Leahy77ff0b12015-05-05 15:07:29 -0700479 offset = 0x50;
480 break;
Lee Leahy32471722015-04-20 15:20:28 -0700481 DEV_CASE(TXE) :
Lee Leahy77ff0b12015-05-05 15:07:29 -0700482 /* TXE cannot be placed in D3Hot. */
483 return 0;
Lee Leahy32471722015-04-20 15:20:28 -0700484 DEV_CASE(PCIE_PORT1) :
485 DEV_CASE(PCIE_PORT2) :
486 DEV_CASE(PCIE_PORT3) :
487 DEV_CASE(PCIE_PORT4) :
Lee Leahy77ff0b12015-05-05 15:07:29 -0700488 offset = 0xa0;
489 break;
490 }
491
492 if (offset != 0) {
493 set_d3hot_bits(dev, offset);
494 return 0;
495 }
496
497 return -1;
498}
499
500/* Common PCI device function disable. */
Elyes HAOUASb13fac32018-05-24 22:29:44 +0200501void southcluster_enable_dev(struct device *dev)
Lee Leahy77ff0b12015-05-05 15:07:29 -0700502{
Elyes HAOUAS066e61f2020-04-29 10:28:20 +0200503 uint16_t reg16;
Lee Leahy77ff0b12015-05-05 15:07:29 -0700504
505 if (!dev->enabled) {
506 int slot = PCI_SLOT(dev->path.pci.devfn);
507 int func = PCI_FUNC(dev->path.pci.devfn);
508 printk(BIOS_DEBUG, "%s: Disabling device: %02x.%01x\n",
509 dev_path(dev), slot, func);
510
511 /* Ensure memory, io, and bus master are all disabled */
Elyes HAOUAS066e61f2020-04-29 10:28:20 +0200512 reg16 = pci_read_config16(dev, PCI_COMMAND);
513 reg16 &= ~(PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY | PCI_COMMAND_IO);
514 pci_write_config16(dev, PCI_COMMAND, reg16);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700515
516 /* Place device in D3Hot */
517 if (place_device_in_d3hot(dev) < 0) {
518 printk(BIOS_WARNING,
519 "Could not place %02x.%01x into D3Hot. "
520 "Keeping device visible.\n", slot, func);
521 return;
522 }
523 /* Disable this device if possible */
524 sc_disable_devfn(dev);
525 } else {
526 /* Enable SERR */
Elyes HAOUAS066e61f2020-04-29 10:28:20 +0200527 pci_or_config16(dev, PCI_COMMAND, PCI_COMMAND_SERR);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700528 }
529}
530
531static struct device_operations device_ops = {
Angel Ponsaee7ab22020-03-19 00:31:58 +0100532 .read_resources = sc_read_resources,
533 .set_resources = pci_dev_set_resources,
Angel Ponsaee7ab22020-03-19 00:31:58 +0100534 .write_acpi_tables = southcluster_write_acpi_tables,
535 .init = sc_init,
536 .enable = southcluster_enable_dev,
537 .scan_bus = scan_static_bus,
538 .ops_pci = &soc_pci_ops,
Lee Leahy77ff0b12015-05-05 15:07:29 -0700539};
540
541static const struct pci_driver southcluster __pci_driver = {
542 .ops = &device_ops,
543 .vendor = PCI_VENDOR_ID_INTEL,
544 .device = LPC_DEVID,
545};
546
Lee Leahy77ff0b12015-05-05 15:07:29 -0700547static void finalize_chipset(void *unused)
548{
Lee Leahy32471722015-04-20 15:20:28 -0700549 void *bcr = (void *)(SPI_BASE_ADDRESS + BCR);
550 void *gcs = (void *)(RCBA_BASE_ADDRESS + GCS);
551 void *gen_pmcon2 = (void *)(PMC_BASE_ADDRESS + GEN_PMCON2);
552 void *etr = (void *)(PMC_BASE_ADDRESS + ETR);
553 uint8_t *spi = (uint8_t *)SPI_BASE_ADDRESS;
Angel Ponsaee7ab22020-03-19 00:31:58 +0100554
Arthur Heymans68b6eb72019-10-13 23:26:36 +0200555 struct vscc_config cfg;
Lee Leahy77ff0b12015-05-05 15:07:29 -0700556
Angel Ponsaee7ab22020-03-19 00:31:58 +0100557 /* Set the lock enable on the BIOS control register */
Lee Leahy77ff0b12015-05-05 15:07:29 -0700558 write32(bcr, read32(bcr) | BCR_LE);
559
Angel Ponsaee7ab22020-03-19 00:31:58 +0100560 /* Set BIOS lock down bit controlling boot block size and swapping */
Lee Leahy77ff0b12015-05-05 15:07:29 -0700561 write32(gcs, read32(gcs) | BILD);
562
Angel Ponsaee7ab22020-03-19 00:31:58 +0100563 /* Lock sleep stretching policy and set SMI lock */
Lee Leahy77ff0b12015-05-05 15:07:29 -0700564 write32(gen_pmcon2, read32(gen_pmcon2) | SLPSX_STR_POL_LOCK | SMI_LOCK);
565
Angel Ponsaee7ab22020-03-19 00:31:58 +0100566 /* Set the CF9 lock */
Lee Leahy77ff0b12015-05-05 15:07:29 -0700567 write32(etr, read32(etr) | CF9LOCK);
568
Arthur Heymans68b6eb72019-10-13 23:26:36 +0200569 spi_finalize_ops();
570 write16(spi + HSFSTS, read16(spi + HSFSTS) | FLOCKDN);
571
572 if (mainboard_get_spi_vscc_config(&cfg) < 0) {
573 printk(BIOS_DEBUG, "No SPI VSCC configuration.\n");
Lee Leahy77ff0b12015-05-05 15:07:29 -0700574 } else {
Lee Leahy77ff0b12015-05-05 15:07:29 -0700575 write32(spi + UVSCC, cfg.uvscc);
576 write32(spi + LVSCC, cfg.lvscc | VCL);
577 }
Lee Leahy77ff0b12015-05-05 15:07:29 -0700578}
579
Hannah Williams2cfdde72015-04-15 19:48:07 -0700580BOOT_STATE_INIT_ENTRY(BS_POST_DEVICE, BS_ON_EXIT, finalize_chipset, NULL);