blob: 06a47bcfd4587373b7c1e46950b8d01170c45cb2 [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
Elyes HAOUASb13fac32018-05-24 22:29:44 +020048static void sc_add_mmio_resources(struct device *dev)
Lee Leahy77ff0b12015-05-05 15:07:29 -070049{
Kyösti Mälkki5a55a452021-06-24 20:49:05 +030050 mmio_range(dev, 0xfeb, ABORT_BASE_ADDRESS, ABORT_BASE_SIZE);
51 mmio_range(dev, PBASE, PMC_BASE_ADDRESS, PMC_BASE_SIZE);
52 mmio_range(dev, IOBASE, IO_BASE_ADDRESS, IO_BASE_SIZE);
53 mmio_range(dev, IBASE, ILB_BASE_ADDRESS, ILB_BASE_SIZE);
54 mmio_range(dev, SBASE, SPI_BASE_ADDRESS, SPI_BASE_SIZE);
55 mmio_range(dev, MPBASE, MPHY_BASE_ADDRESS, MPHY_BASE_SIZE);
56 mmio_range(dev, PUBASE, PUNIT_BASE_ADDRESS, PUNIT_BASE_SIZE);
57 mmio_range(dev, RCBA, RCBA_BASE_ADDRESS, RCBA_BASE_SIZE);
58 mmio_range(dev, 0xfff, 0xffffffff - (CONFIG_COREBOOT_ROMSIZE_KB * KiB) + 1,
59 CONFIG_COREBOOT_ROMSIZE_KB * KiB); /* BIOS ROM */
Angel Ponsaee7ab22020-03-19 00:31:58 +010060
Kyösti Mälkki5a55a452021-06-24 20:49:05 +030061 mmio_range(dev, 0xfec, IO_APIC_ADDR, 0x00001000); /* IOAPIC */
Lee Leahy77ff0b12015-05-05 15:07:29 -070062}
63
64/* Default IO range claimed by the LPC device. The upper bound is exclusive. */
65#define LPC_DEFAULT_IO_RANGE_LOWER 0
66#define LPC_DEFAULT_IO_RANGE_UPPER 0x1000
67
Frans Hendriks2c630172019-04-02 15:06:29 +020068static void sc_enable_serial_irqs(struct device *dev)
69{
70 u8 *ilb_base = (u8 *)(pci_read_config32(dev, IBASE) & ~0xF);
71
72 printk(BIOS_SPEW, "Enable serial irq\n");
73 write32(ilb_base + ILB_OIC, read32(ilb_base + ILB_OIC) | SIRQEN);
74 write8(ilb_base + SCNT, read8(ilb_base + SCNT) | SCNT_MODE);
75}
76
Frans Hendriks93484132018-12-10 12:38:16 +010077/*
Angel Ponsaee7ab22020-03-19 00:31:58 +010078 * Write PCI config space IRQ assignments. PCI devices have the INT_LINE (0x3c) and INT_PIN
79 * (0x3d) registers which report interrupt routing information to operating systems and drivers.
80 * The INT_PIN register is generally read only and reports which interrupt pin A - D it uses.
81 * The INT_LINE register is configurable and reports which IRQ (generally the PIC IRQs 1 - 15)
82 * it will use. This needs to take interrupt pin swizzling on devices that are downstream on
83 * a PCI bridge into account.
Frans Hendriks93484132018-12-10 12:38:16 +010084 *
Angel Ponsaee7ab22020-03-19 00:31:58 +010085 * This function will loop through all enabled PCI devices and program the INT_LINE register
86 * with the correct PIC IRQ number for the INT_PIN that it uses. It then configures each
87 * interrupt in the PIC to be level triggered.
Frans Hendriks93484132018-12-10 12:38:16 +010088 */
89static void write_pci_config_irqs(void)
90{
91 struct device *irq_dev;
92 struct device *targ_dev;
93 uint8_t int_line = 0;
94 uint8_t original_int_pin = 0;
95 uint8_t new_int_pin = 0;
96 uint16_t current_bdf = 0;
97 uint16_t parent_bdf = 0;
98 uint8_t pirq = 0;
99 uint8_t device_num = 0;
100 const struct soc_irq_route *ir = &global_soc_irq_route;
101
102 if (ir == NULL) {
Julius Wernere9665952022-01-21 17:06:20 -0800103 printk(BIOS_WARNING, "Can't write PCI IRQ assignments "
Angel Ponsaee7ab22020-03-19 00:31:58 +0100104 "because 'global_braswell_irq_route' structure does not exist\n");
Frans Hendriks93484132018-12-10 12:38:16 +0100105 return;
106 }
107
108 /*
Angel Ponsaee7ab22020-03-19 00:31:58 +0100109 * Loop through all enabled devices and program their INT_LINE, INT_PIN registers from
110 * values taken from the Interrupt Route registers in the ILB
Frans Hendriks93484132018-12-10 12:38:16 +0100111 */
112 printk(BIOS_DEBUG, "PCI_CFG IRQ: Write PIRQ assignments\n");
113 for (irq_dev = all_devices; irq_dev; irq_dev = irq_dev->next) {
Frans Hendriks93484132018-12-10 12:38:16 +0100114 if ((irq_dev->path.type != DEVICE_PATH_PCI) ||
115 (!irq_dev->enabled))
116 continue;
117
118 current_bdf = irq_dev->path.pci.devfn |
Arthur Heymans7fcd4d52023-08-24 15:12:19 +0200119 irq_dev->upstream->secondary << 8;
Frans Hendriks93484132018-12-10 12:38:16 +0100120
121 /*
122 * Step 1: Get the INT_PIN and device structure to look for
123 * in the pirq_data table defined in the mainboard directory.
124 */
125 targ_dev = NULL;
126 new_int_pin = get_pci_irq_pins(irq_dev, &targ_dev);
127 if (targ_dev == NULL || new_int_pin < 1)
128 continue;
129
130 /* Get the original INT_PIN for record keeping */
131 original_int_pin = pci_read_config8(irq_dev, PCI_INTERRUPT_PIN);
132
133 parent_bdf = targ_dev->path.pci.devfn
Arthur Heymans7fcd4d52023-08-24 15:12:19 +0200134 | targ_dev->upstream->secondary << 8;
Frans Hendriks93484132018-12-10 12:38:16 +0100135 device_num = PCI_SLOT(parent_bdf);
136
137 if (ir->pcidev[device_num] == 0) {
Julius Wernere9665952022-01-21 17:06:20 -0800138 printk(BIOS_WARNING, "PCI Device %d does not have an IRQ entry, "
Angel Ponsaee7ab22020-03-19 00:31:58 +0100139 "skipping it\n", device_num);
Frans Hendriks93484132018-12-10 12:38:16 +0100140 continue;
141 }
142
143 /* Find the PIRQ that is attached to the INT_PIN */
144 pirq = (ir->pcidev[device_num] >> ((new_int_pin - 1) * 4))
145 & 0x7;
146
147 /* Get the INT_LINE this device/function will use */
148 int_line = ir->pic[pirq];
149
150 if (int_line != PIRQ_PIC_IRQDISABLE) {
151 /* Set this IRQ to level triggered */
Angel Ponsaee7ab22020-03-19 00:31:58 +0100152 i8259_configure_irq_trigger(int_line, IRQ_LEVEL_TRIGGERED);
153
Frans Hendriks93484132018-12-10 12:38:16 +0100154 /* Set the Interrupt Line register */
Angel Ponsaee7ab22020-03-19 00:31:58 +0100155 pci_write_config8(irq_dev, PCI_INTERRUPT_LINE, int_line);
Frans Hendriks93484132018-12-10 12:38:16 +0100156 } else {
Angel Ponsaee7ab22020-03-19 00:31:58 +0100157 /* Set the Interrupt line register as 'unknown' or 'unused' */
158 pci_write_config8(irq_dev, PCI_INTERRUPT_LINE, PIRQ_PIC_UNKNOWN_UNUSED);
Frans Hendriks93484132018-12-10 12:38:16 +0100159 }
160
Angel Ponsaee7ab22020-03-19 00:31:58 +0100161 printk(BIOS_SPEW, "\tINT_PIN\t\t: %d (%s)\n", original_int_pin,
162 pin_to_str(original_int_pin));
163
Frans Hendriks93484132018-12-10 12:38:16 +0100164 if (parent_bdf != current_bdf)
Angel Ponsaee7ab22020-03-19 00:31:58 +0100165 printk(BIOS_SPEW, "\tSwizzled to\t: %d (%s)\n", new_int_pin,
166 pin_to_str(new_int_pin));
167
168 printk(BIOS_SPEW, "\tPIRQ\t\t: %c\n\tINT_LINE\t: 0x%X (IRQ %d)\n",
169 'A' + pirq, int_line, int_line);
Frans Hendriks93484132018-12-10 12:38:16 +0100170 }
171 printk(BIOS_DEBUG, "PCI_CFG IRQ: Finished writing PIRQ assignments\n");
172}
173
Lee Leahy77ff0b12015-05-05 15:07:29 -0700174static inline int io_range_in_default(int base, int size)
175{
176 /* Does it start above the range? */
177 if (base >= LPC_DEFAULT_IO_RANGE_UPPER)
178 return 0;
179
180 /* Is it entirely contained? */
Angel Ponsaee7ab22020-03-19 00:31:58 +0100181 if (base >= LPC_DEFAULT_IO_RANGE_LOWER && (base + size) < LPC_DEFAULT_IO_RANGE_UPPER)
Lee Leahy77ff0b12015-05-05 15:07:29 -0700182 return 1;
183
Angel Ponsaee7ab22020-03-19 00:31:58 +0100184 /* This will return not in range for partial overlaps */
Lee Leahy77ff0b12015-05-05 15:07:29 -0700185 return 0;
186}
187
188/*
189 * Note: this function assumes there is no overlap with the default LPC device's
190 * claimed range: LPC_DEFAULT_IO_RANGE_LOWER -> LPC_DEFAULT_IO_RANGE_UPPER.
191 */
Angel Ponsaee7ab22020-03-19 00:31:58 +0100192static void sc_add_io_resource(struct device *dev, int base, int size, int index)
Lee Leahy77ff0b12015-05-05 15:07:29 -0700193{
194 struct resource *res;
195
196 if (io_range_in_default(base, size))
197 return;
198
199 res = new_resource(dev, index);
200 res->base = base;
201 res->size = size;
202 res->flags = IORESOURCE_IO | IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
203}
204
Elyes HAOUASb13fac32018-05-24 22:29:44 +0200205static void sc_add_io_resources(struct device *dev)
Lee Leahy77ff0b12015-05-05 15:07:29 -0700206{
207 struct resource *res;
208
209 /* Add the default claimed IO range for the LPC device. */
210 res = new_resource(dev, 0);
211 res->base = LPC_DEFAULT_IO_RANGE_LOWER;
212 res->size = LPC_DEFAULT_IO_RANGE_UPPER - LPC_DEFAULT_IO_RANGE_LOWER;
213 res->flags = IORESOURCE_IO | IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
214
215 /* GPIO */
Frans Hendriks4b2c12f2018-11-22 07:52:38 +0100216 sc_add_io_resource(dev, GPIO_BASE_ADDRESS, GPIO_BASE_SIZE, GBASE);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700217
218 /* ACPI */
Frans Hendriks4b2c12f2018-11-22 07:52:38 +0100219 sc_add_io_resource(dev, ACPI_BASE_ADDRESS, ACPI_BASE_SIZE, ABASE);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700220}
221
Elyes HAOUASb13fac32018-05-24 22:29:44 +0200222static void sc_read_resources(struct device *dev)
Lee Leahy77ff0b12015-05-05 15:07:29 -0700223{
224 /* Get the normal PCI resources of this device. */
225 pci_dev_read_resources(dev);
226
227 /* Add non-standard MMIO resources. */
228 sc_add_mmio_resources(dev);
229
230 /* Add IO resources. */
231 sc_add_io_resources(dev);
232}
233
Elyes HAOUASb13fac32018-05-24 22:29:44 +0200234static void sc_init(struct device *dev)
Lee Leahy77ff0b12015-05-05 15:07:29 -0700235{
236 int i;
Frans Hendriksb55cd542019-03-06 14:45:12 +0100237 const unsigned long ilb_base = ILB_BASE_ADDRESS;
Angel Ponsaee7ab22020-03-19 00:31:58 +0100238 const unsigned long pr_base = ILB_BASE_ADDRESS + 0x08;
239 const unsigned long ir_base = ILB_BASE_ADDRESS + 0x20;
240
Lee Leahy32471722015-04-20 15:20:28 -0700241 void *gen_pmcon1 = (void *)(PMC_BASE_ADDRESS + GEN_PMCON1);
Lee Leahy32471722015-04-20 15:20:28 -0700242 const struct soc_irq_route *ir = &global_soc_irq_route;
Kyösti Mälkki8950cfb2019-07-13 22:16:25 +0300243 struct soc_intel_braswell_config *config = config_of(dev);
Lee Leahy32471722015-04-20 15:20:28 -0700244
Frans Hendriksb55cd542019-03-06 14:45:12 +0100245 /* Set the value for PCI command register. */
246 pci_write_config16(dev, PCI_COMMAND,
Angel Ponsaee7ab22020-03-19 00:31:58 +0100247 PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER | PCI_COMMAND_SPECIAL);
Frans Hendriksb55cd542019-03-06 14:45:12 +0100248
249 /* Use IRQ9 for SCI Interrupt */
250 write32((void *)(ilb_base + ACTL), 0);
251
Frans Hendriks255f35c2018-12-11 15:21:47 +0100252 isa_dma_init();
253
Frans Hendriks2c630172019-04-02 15:06:29 +0200254 sc_enable_serial_irqs(dev);
255
Lee Leahy77ff0b12015-05-05 15:07:29 -0700256 /* Set up the PIRQ PIC routing based on static config. */
Lee Leahy32471722015-04-20 15:20:28 -0700257 for (i = 0; i < NUM_PIRQS; i++)
Angel Ponsaee7ab22020-03-19 00:31:58 +0100258 write8((void *)(pr_base + i*sizeof(ir->pic[i])), ir->pic[i]);
Lee Leahy32471722015-04-20 15:20:28 -0700259
Lee Leahy77ff0b12015-05-05 15:07:29 -0700260 /* Set up the per device PIRQ routing base on static config. */
Lee Leahy32471722015-04-20 15:20:28 -0700261 for (i = 0; i < NUM_IR_DEVS; i++)
Angel Ponsaee7ab22020-03-19 00:31:58 +0100262 write16((void *)(ir_base + i*sizeof(ir->pcidev[i])), ir->pcidev[i]);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700263
Frans Hendriks93484132018-12-10 12:38:16 +0100264 /* Interrupt 9 should be level triggered (SCI) */
265 i8259_configure_irq_trigger(9, 1);
266
267 for (i = 0; i < NUM_PIRQS; i++) {
268 if (ir->pic[i])
269 i8259_configure_irq_trigger(ir->pic[i], 1);
270 }
Lee Leahy77ff0b12015-05-05 15:07:29 -0700271
Lee Leahy77ff0b12015-05-05 15:07:29 -0700272 if (config->disable_slp_x_stretch_sus_fail) {
273 printk(BIOS_DEBUG, "Disabling slp_x stretching.\n");
Angel Ponsaee7ab22020-03-19 00:31:58 +0100274 write32(gen_pmcon1, read32(gen_pmcon1) | DIS_SLP_X_STRCH_SUS_UP);
275
Lee Leahy77ff0b12015-05-05 15:07:29 -0700276 } else {
Angel Ponsaee7ab22020-03-19 00:31:58 +0100277 write32(gen_pmcon1, read32(gen_pmcon1) & ~DIS_SLP_X_STRCH_SUS_UP);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700278 }
279
Frans Hendriks93484132018-12-10 12:38:16 +0100280 /* Write IRQ assignments to PCI config space */
281 write_pci_config_irqs();
282
283 /* Initialize i8259 pic */
284 setup_i8259();
285
Frans Hendriksbd5233e2018-12-05 15:24:48 +0100286 /* Initialize i8254 timers */
287 setup_i8254();
Frans Hendriks2c630172019-04-02 15:06:29 +0200288
289 sc_set_serial_irqs_mode(dev, config->serirq_mode);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700290}
291
292/*
293 * Common code for the south cluster devices.
294 */
295
Frans Hendrikse6bf51f2019-05-01 10:48:31 +0200296/* Set bit in function disable register to hide this device. */
Elyes HAOUASb13fac32018-05-24 22:29:44 +0200297static void sc_disable_devfn(struct device *dev)
Lee Leahy77ff0b12015-05-05 15:07:29 -0700298{
Angel Ponsaee7ab22020-03-19 00:31:58 +0100299 void *func_dis = (void *)(PMC_BASE_ADDRESS + FUNC_DIS);
Lee Leahy32471722015-04-20 15:20:28 -0700300 void *func_dis2 = (void *)(PMC_BASE_ADDRESS + FUNC_DIS2);
Angel Ponsaee7ab22020-03-19 00:31:58 +0100301 uint32_t mask = 0;
Lee Leahy77ff0b12015-05-05 15:07:29 -0700302 uint32_t mask2 = 0;
303
Lee Leahy32471722015-04-20 15:20:28 -0700304#define SET_DIS_MASK(name_) \
305 case PCI_DEVFN(name_ ## _DEV, name_ ## _FUNC): \
306 mask |= name_ ## _DIS
Angel Ponsaee7ab22020-03-19 00:31:58 +0100307
Lee Leahy32471722015-04-20 15:20:28 -0700308#define SET_DIS_MASK2(name_) \
309 case PCI_DEVFN(name_ ## _DEV, name_ ## _FUNC): \
310 mask2 |= name_ ## _DIS
311
Lee Leahy77ff0b12015-05-05 15:07:29 -0700312 switch (dev->path.pci.devfn) {
Lee Leahy32471722015-04-20 15:20:28 -0700313 SET_DIS_MASK(SDIO);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700314 break;
Lee Leahy32471722015-04-20 15:20:28 -0700315 SET_DIS_MASK(SD);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700316 break;
Lee Leahy32471722015-04-20 15:20:28 -0700317 SET_DIS_MASK(SATA);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700318 break;
Lee Leahy32471722015-04-20 15:20:28 -0700319 SET_DIS_MASK(XHCI);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700320 /* Disable super speed PHY when XHCI is not available. */
321 mask2 |= USH_SS_PHY_DIS;
322 break;
Lee Leahy32471722015-04-20 15:20:28 -0700323 SET_DIS_MASK(LPE);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700324 break;
Lee Leahy32471722015-04-20 15:20:28 -0700325 SET_DIS_MASK(MMC);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700326 break;
Lee Leahy32471722015-04-20 15:20:28 -0700327 SET_DIS_MASK(SIO_DMA1);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700328 break;
Lee Leahy32471722015-04-20 15:20:28 -0700329 SET_DIS_MASK(I2C1);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700330 break;
Lee Leahy32471722015-04-20 15:20:28 -0700331 SET_DIS_MASK(I2C2);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700332 break;
Lee Leahy32471722015-04-20 15:20:28 -0700333 SET_DIS_MASK(I2C3);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700334 break;
Lee Leahy32471722015-04-20 15:20:28 -0700335 SET_DIS_MASK(I2C4);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700336 break;
Lee Leahy32471722015-04-20 15:20:28 -0700337 SET_DIS_MASK(I2C5);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700338 break;
Lee Leahy32471722015-04-20 15:20:28 -0700339 SET_DIS_MASK(I2C6);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700340 break;
Lee Leahy32471722015-04-20 15:20:28 -0700341 SET_DIS_MASK(I2C7);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700342 break;
Lee Leahy32471722015-04-20 15:20:28 -0700343 SET_DIS_MASK(TXE);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700344 break;
Lee Leahy32471722015-04-20 15:20:28 -0700345 SET_DIS_MASK(HDA);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700346 break;
Lee Leahy32471722015-04-20 15:20:28 -0700347 SET_DIS_MASK(PCIE_PORT1);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700348 break;
Lee Leahy32471722015-04-20 15:20:28 -0700349 SET_DIS_MASK(PCIE_PORT2);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700350 break;
Lee Leahy32471722015-04-20 15:20:28 -0700351 SET_DIS_MASK(PCIE_PORT3);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700352 break;
Lee Leahy32471722015-04-20 15:20:28 -0700353 SET_DIS_MASK(PCIE_PORT4);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700354 break;
Lee Leahy32471722015-04-20 15:20:28 -0700355 SET_DIS_MASK(SIO_DMA2);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700356 break;
Lee Leahy32471722015-04-20 15:20:28 -0700357 SET_DIS_MASK(PWM1);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700358 break;
Lee Leahy32471722015-04-20 15:20:28 -0700359 SET_DIS_MASK(PWM2);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700360 break;
Lee Leahy32471722015-04-20 15:20:28 -0700361 SET_DIS_MASK(HSUART1);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700362 break;
Lee Leahy32471722015-04-20 15:20:28 -0700363 SET_DIS_MASK(HSUART2);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700364 break;
Lee Leahy32471722015-04-20 15:20:28 -0700365 SET_DIS_MASK(SPI);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700366 break;
Lee Leahy32471722015-04-20 15:20:28 -0700367 SET_DIS_MASK2(SMBUS);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700368 break;
369 }
370
371 if (mask != 0) {
372 write32(func_dis, read32(func_dis) | mask);
Angel Ponsaee7ab22020-03-19 00:31:58 +0100373 /* Ensure posted write hits */
Lee Leahy77ff0b12015-05-05 15:07:29 -0700374 read32(func_dis);
375 }
376
377 if (mask2 != 0) {
378 write32(func_dis2, read32(func_dis2) | mask2);
Angel Ponsaee7ab22020-03-19 00:31:58 +0100379 /* Ensure posted write hits */
Lee Leahy77ff0b12015-05-05 15:07:29 -0700380 read32(func_dis2);
381 }
382}
383
Elyes HAOUASb13fac32018-05-24 22:29:44 +0200384static inline void set_d3hot_bits(struct device *dev, int offset)
Lee Leahy77ff0b12015-05-05 15:07:29 -0700385{
386 uint32_t reg8;
387 printk(BIOS_DEBUG, "Power management CAP offset 0x%x.\n", offset);
388 reg8 = pci_read_config8(dev, offset + 4);
389 reg8 |= 0x3;
390 pci_write_config8(dev, offset + 4, reg8);
391}
392
Lee Leahy32471722015-04-20 15:20:28 -0700393/*
Angel Ponsaee7ab22020-03-19 00:31:58 +0100394 * Parts of the audio subsystem are powered by the HDA device. Thus, one cannot put HDA into
395 * D3Hot. Instead, perform this workaround to make some of the audio paths work for LPE audio.
Lee Leahy32471722015-04-20 15:20:28 -0700396 */
Elyes HAOUASb13fac32018-05-24 22:29:44 +0200397static void hda_work_around(struct device *dev)
Lee Leahy77ff0b12015-05-05 15:07:29 -0700398{
Lee Leahy32471722015-04-20 15:20:28 -0700399 void *gctl = (void *)(TEMP_BASE_ADDRESS + 0x8);
400
Lee Leahy77ff0b12015-05-05 15:07:29 -0700401 /* Need to set magic register 0x43 to 0xd7 in config space. */
402 pci_write_config8(dev, 0x43, 0xd7);
403
Lee Leahy32471722015-04-20 15:20:28 -0700404 /*
Angel Ponsaee7ab22020-03-19 00:31:58 +0100405 * Need to set bit 0 of GCTL to take the device out of reset.
406 * However, that requires setting up the 64-bit BAR.
Lee Leahy32471722015-04-20 15:20:28 -0700407 */
Lee Leahy77ff0b12015-05-05 15:07:29 -0700408 pci_write_config32(dev, PCI_BASE_ADDRESS_0, TEMP_BASE_ADDRESS);
409 pci_write_config32(dev, PCI_BASE_ADDRESS_1, 0);
Elyes HAOUAS066e61f2020-04-29 10:28:20 +0200410 pci_write_config16(dev, PCI_COMMAND, PCI_COMMAND_MEMORY);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700411 write32(gctl, read32(gctl) | 0x1);
Elyes HAOUAS066e61f2020-04-29 10:28:20 +0200412 pci_write_config16(dev, PCI_COMMAND, 0);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700413 pci_write_config32(dev, PCI_BASE_ADDRESS_0, 0);
414}
415
Elyes HAOUASb13fac32018-05-24 22:29:44 +0200416static int place_device_in_d3hot(struct device *dev)
Lee Leahy77ff0b12015-05-05 15:07:29 -0700417{
Lee Leahy1072e7d2017-03-16 17:35:32 -0700418 unsigned int offset;
Lee Leahy77ff0b12015-05-05 15:07:29 -0700419
Lee Leahy32471722015-04-20 15:20:28 -0700420 /*
421 * Parts of the HDA block are used for LPE audio as well.
422 * Therefore assume the HDA will never be put into D3Hot.
423 */
Lee Leahy77ff0b12015-05-05 15:07:29 -0700424 if (dev->path.pci.devfn == PCI_DEVFN(HDA_DEV, HDA_FUNC)) {
425 hda_work_around(dev);
426 return 0;
427 }
428
429 offset = pci_find_capability(dev, PCI_CAP_ID_PM);
430
431 if (offset != 0) {
432 set_d3hot_bits(dev, offset);
433 return 0;
434 }
435
Lee Leahy32471722015-04-20 15:20:28 -0700436 /*
Angel Ponsaee7ab22020-03-19 00:31:58 +0100437 * For some reason some of the devices don't have the capability pointer set correctly.
438 * Work around this by hard coding the offset.
Lee Leahy32471722015-04-20 15:20:28 -0700439 */
440#define DEV_CASE(name_) \
441 case PCI_DEVFN(name_ ## _DEV, name_ ## _FUNC)
442
Lee Leahy77ff0b12015-05-05 15:07:29 -0700443 switch (dev->path.pci.devfn) {
Lee Leahy32471722015-04-20 15:20:28 -0700444 DEV_CASE(SDIO) :
445 DEV_CASE(SD) :
446 DEV_CASE(MMC) :
447 DEV_CASE(LPE) :
448 DEV_CASE(SIO_DMA1) :
449 DEV_CASE(I2C1) :
450 DEV_CASE(I2C2) :
451 DEV_CASE(I2C3) :
452 DEV_CASE(I2C4) :
453 DEV_CASE(I2C5) :
454 DEV_CASE(I2C6) :
455 DEV_CASE(I2C7) :
456 DEV_CASE(SIO_DMA2) :
457 DEV_CASE(PWM1) :
458 DEV_CASE(PWM2) :
459 DEV_CASE(HSUART1) :
460 DEV_CASE(HSUART2) :
461 DEV_CASE(SPI) :
Lee Leahy77ff0b12015-05-05 15:07:29 -0700462 offset = 0x80;
463 break;
Lee Leahy32471722015-04-20 15:20:28 -0700464 DEV_CASE(SATA) :
465 DEV_CASE(XHCI) :
Lee Leahy77ff0b12015-05-05 15:07:29 -0700466 offset = 0x70;
467 break;
Lee Leahy32471722015-04-20 15:20:28 -0700468 DEV_CASE(HDA) :
469 DEV_CASE(SMBUS) :
Lee Leahy77ff0b12015-05-05 15:07:29 -0700470 offset = 0x50;
471 break;
Lee Leahy32471722015-04-20 15:20:28 -0700472 DEV_CASE(TXE) :
Lee Leahy77ff0b12015-05-05 15:07:29 -0700473 /* TXE cannot be placed in D3Hot. */
474 return 0;
Lee Leahy32471722015-04-20 15:20:28 -0700475 DEV_CASE(PCIE_PORT1) :
476 DEV_CASE(PCIE_PORT2) :
477 DEV_CASE(PCIE_PORT3) :
478 DEV_CASE(PCIE_PORT4) :
Lee Leahy77ff0b12015-05-05 15:07:29 -0700479 offset = 0xa0;
480 break;
481 }
482
483 if (offset != 0) {
484 set_d3hot_bits(dev, offset);
485 return 0;
486 }
487
488 return -1;
489}
490
491/* Common PCI device function disable. */
Elyes HAOUASb13fac32018-05-24 22:29:44 +0200492void southcluster_enable_dev(struct device *dev)
Lee Leahy77ff0b12015-05-05 15:07:29 -0700493{
Elyes HAOUAS066e61f2020-04-29 10:28:20 +0200494 uint16_t reg16;
Lee Leahy77ff0b12015-05-05 15:07:29 -0700495
496 if (!dev->enabled) {
497 int slot = PCI_SLOT(dev->path.pci.devfn);
498 int func = PCI_FUNC(dev->path.pci.devfn);
499 printk(BIOS_DEBUG, "%s: Disabling device: %02x.%01x\n",
500 dev_path(dev), slot, func);
501
502 /* Ensure memory, io, and bus master are all disabled */
Elyes HAOUAS066e61f2020-04-29 10:28:20 +0200503 reg16 = pci_read_config16(dev, PCI_COMMAND);
504 reg16 &= ~(PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY | PCI_COMMAND_IO);
505 pci_write_config16(dev, PCI_COMMAND, reg16);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700506
507 /* Place device in D3Hot */
508 if (place_device_in_d3hot(dev) < 0) {
509 printk(BIOS_WARNING,
510 "Could not place %02x.%01x into D3Hot. "
511 "Keeping device visible.\n", slot, func);
512 return;
513 }
514 /* Disable this device if possible */
515 sc_disable_devfn(dev);
516 } else {
517 /* Enable SERR */
Elyes HAOUAS066e61f2020-04-29 10:28:20 +0200518 pci_or_config16(dev, PCI_COMMAND, PCI_COMMAND_SERR);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700519 }
520}
521
522static struct device_operations device_ops = {
Angel Ponsaee7ab22020-03-19 00:31:58 +0100523 .read_resources = sc_read_resources,
524 .set_resources = pci_dev_set_resources,
Angel Ponsaee7ab22020-03-19 00:31:58 +0100525 .write_acpi_tables = southcluster_write_acpi_tables,
526 .init = sc_init,
527 .enable = southcluster_enable_dev,
528 .scan_bus = scan_static_bus,
529 .ops_pci = &soc_pci_ops,
Lee Leahy77ff0b12015-05-05 15:07:29 -0700530};
531
532static const struct pci_driver southcluster __pci_driver = {
533 .ops = &device_ops,
Felix Singer43b7f412022-03-07 04:34:52 +0100534 .vendor = PCI_VID_INTEL,
Lee Leahy77ff0b12015-05-05 15:07:29 -0700535 .device = LPC_DEVID,
536};
537
Lee Leahy77ff0b12015-05-05 15:07:29 -0700538static void finalize_chipset(void *unused)
539{
Lee Leahy32471722015-04-20 15:20:28 -0700540 void *bcr = (void *)(SPI_BASE_ADDRESS + BCR);
541 void *gcs = (void *)(RCBA_BASE_ADDRESS + GCS);
542 void *gen_pmcon2 = (void *)(PMC_BASE_ADDRESS + GEN_PMCON2);
543 void *etr = (void *)(PMC_BASE_ADDRESS + ETR);
544 uint8_t *spi = (uint8_t *)SPI_BASE_ADDRESS;
Angel Ponsaee7ab22020-03-19 00:31:58 +0100545
Arthur Heymans68b6eb72019-10-13 23:26:36 +0200546 struct vscc_config cfg;
Lee Leahy77ff0b12015-05-05 15:07:29 -0700547
Angel Ponsaee7ab22020-03-19 00:31:58 +0100548 /* Set the lock enable on the BIOS control register */
Lee Leahy77ff0b12015-05-05 15:07:29 -0700549 write32(bcr, read32(bcr) | BCR_LE);
550
Angel Ponsaee7ab22020-03-19 00:31:58 +0100551 /* Set BIOS lock down bit controlling boot block size and swapping */
Lee Leahy77ff0b12015-05-05 15:07:29 -0700552 write32(gcs, read32(gcs) | BILD);
553
Angel Ponsaee7ab22020-03-19 00:31:58 +0100554 /* Lock sleep stretching policy and set SMI lock */
Lee Leahy77ff0b12015-05-05 15:07:29 -0700555 write32(gen_pmcon2, read32(gen_pmcon2) | SLPSX_STR_POL_LOCK | SMI_LOCK);
556
Angel Ponsaee7ab22020-03-19 00:31:58 +0100557 /* Set the CF9 lock */
Lee Leahy77ff0b12015-05-05 15:07:29 -0700558 write32(etr, read32(etr) | CF9LOCK);
559
Arthur Heymans68b6eb72019-10-13 23:26:36 +0200560 spi_finalize_ops();
561 write16(spi + HSFSTS, read16(spi + HSFSTS) | FLOCKDN);
562
563 if (mainboard_get_spi_vscc_config(&cfg) < 0) {
564 printk(BIOS_DEBUG, "No SPI VSCC configuration.\n");
Lee Leahy77ff0b12015-05-05 15:07:29 -0700565 } else {
Lee Leahy77ff0b12015-05-05 15:07:29 -0700566 write32(spi + UVSCC, cfg.uvscc);
567 write32(spi + LVSCC, cfg.lvscc | VCL);
568 }
Lee Leahy77ff0b12015-05-05 15:07:29 -0700569}
570
Hannah Williams2cfdde72015-04-15 19:48:07 -0700571BOOT_STATE_INIT_ENTRY(BS_POST_DEVICE, BS_ON_EXIT, finalize_chipset, NULL);