blob: 000790d9a635cd3db7b427dfbf19e3af3fbfd6c2 [file] [log] [blame]
Lee Leahy77ff0b12015-05-05 15:07:29 -07001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2008-2009 coresystems GmbH
5 * Copyright (C) 2013 Google Inc.
Lee Leahy32471722015-04-20 15:20:28 -07006 * Copyright (C) 2015 Intel Corp.
Frans Hendriksb55cd542019-03-06 14:45:12 +01007 * Copyright (C) 2018-2019 Eltan B.V.
Lee Leahy77ff0b12015-05-05 15:07:29 -07008 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; version 2 of the License.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
Lee Leahy77ff0b12015-05-05 15:07:29 -070017 */
18
Lee Leahy77ff0b12015-05-05 15:07:29 -070019#include <arch/io.h>
Kyösti Mälkki13f66502019-03-03 08:01:05 +020020#include <device/mmio.h>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +020021#include <device/pci_ops.h>
Lee Leahy77ff0b12015-05-05 15:07:29 -070022#include <arch/acpi.h>
Frans Hendriksad5e0a82019-03-18 13:31:56 +010023#include <arch/ioapic.h>
Lee Leahy77ff0b12015-05-05 15:07:29 -070024#include <bootstate.h>
Lee Leahy32471722015-04-20 15:20:28 -070025#include "chip.h"
Lee Leahy77ff0b12015-05-05 15:07:29 -070026#include <console/console.h>
27#include <cpu/x86/smm.h>
28#include <device/device.h>
29#include <device/pci.h>
30#include <device/pci_ids.h>
Frans Hendriks2c630172019-04-02 15:06:29 +020031#include <intelblocks/lpc_lib.h>
Frans Hendriks255f35c2018-12-11 15:21:47 +010032#include <pc80/isa-dma.h>
Frans Hendriksbd5233e2018-12-05 15:24:48 +010033#include <pc80/i8254.h>
Frans Hendriks93484132018-12-10 12:38:16 +010034#include <pc80/i8259.h>
Lee Leahy2bc9cee2015-06-30 15:25:44 -070035#include <soc/acpi.h>
Lee Leahy77ff0b12015-05-05 15:07:29 -070036#include <soc/iomap.h>
37#include <soc/irq.h>
38#include <soc/lpc.h>
Lee Leahy77ff0b12015-05-05 15:07:29 -070039#include <soc/pci_devs.h>
Lee Leahy32471722015-04-20 15:20:28 -070040#include <soc/pm.h>
Lee Leahy77ff0b12015-05-05 15:07:29 -070041#include <soc/ramstage.h>
42#include <soc/spi.h>
Lee Leahy32471722015-04-20 15:20:28 -070043#include <spi-generic.h>
44#include <stdint.h>
Hannah Williams3fa80a92017-03-22 16:33:36 -070045
Frans Hendriks2c630172019-04-02 15:06:29 +020046static void sc_set_serial_irqs_mode(struct device *dev, enum serirq_mode mode)
Hannah Williams3fa80a92017-03-22 16:33:36 -070047{
Frans Hendriks2c630172019-04-02 15:06:29 +020048 u8 *ilb_base = (u8 *)(pci_read_config32(dev, IBASE) & ~0xF);
49
50 switch (mode) {
51 case SERIRQ_CONTINUOUS:
52 break;
53 case SERIRQ_OFF:
54 write32(ilb_base + ILB_OIC, read32(ilb_base + ILB_OIC) &
55 ~SIRQEN);
56 break;
57 case SERIRQ_QUIET:
58 default:
59 write8(ilb_base + SCNT, read8(ilb_base + SCNT) & ~SCNT_MODE);
60 break;
61 }
Hannah Williams3fa80a92017-03-22 16:33:36 -070062}
Lee Leahy77ff0b12015-05-05 15:07:29 -070063
64static inline void
Elyes HAOUASb13fac32018-05-24 22:29:44 +020065add_mmio_resource(struct device *dev, int i, unsigned long addr,
66 unsigned long size)
Lee Leahy77ff0b12015-05-05 15:07:29 -070067{
Elyes HAOUASa342f392018-10-17 10:56:26 +020068 printk(BIOS_SPEW, "%s/%s (%s, 0x%016lx, 0x%016lx)\n",
Lee Leahy32471722015-04-20 15:20:28 -070069 __FILE__, __func__, dev_name(dev), addr, size);
Lee Leahy77ff0b12015-05-05 15:07:29 -070070 mmio_resource(dev, i, addr >> 10, size >> 10);
71}
72
Elyes HAOUASb13fac32018-05-24 22:29:44 +020073static void sc_add_mmio_resources(struct device *dev)
Lee Leahy77ff0b12015-05-05 15:07:29 -070074{
Elyes HAOUASa342f392018-10-17 10:56:26 +020075 printk(BIOS_SPEW, "%s/%s (%s)\n",
Lee Leahy32471722015-04-20 15:20:28 -070076 __FILE__, __func__, dev_name(dev));
Lee Leahy77ff0b12015-05-05 15:07:29 -070077 add_mmio_resource(dev, 0xfeb, ABORT_BASE_ADDRESS, ABORT_BASE_SIZE);
78 add_mmio_resource(dev, PBASE, PMC_BASE_ADDRESS, PMC_BASE_SIZE);
79 add_mmio_resource(dev, IOBASE, IO_BASE_ADDRESS, IO_BASE_SIZE);
80 add_mmio_resource(dev, IBASE, ILB_BASE_ADDRESS, ILB_BASE_SIZE);
81 add_mmio_resource(dev, SBASE, SPI_BASE_ADDRESS, SPI_BASE_SIZE);
82 add_mmio_resource(dev, MPBASE, MPHY_BASE_ADDRESS, MPHY_BASE_SIZE);
83 add_mmio_resource(dev, PUBASE, PUNIT_BASE_ADDRESS, PUNIT_BASE_SIZE);
84 add_mmio_resource(dev, RCBA, RCBA_BASE_ADDRESS, RCBA_BASE_SIZE);
Frans Hendriksad5e0a82019-03-18 13:31:56 +010085 add_mmio_resource(dev, 0xfff,
86 0xffffffff - (CONFIG_COREBOOT_ROMSIZE_KB*KiB) + 1,
87 (CONFIG_COREBOOT_ROMSIZE_KB*KiB)); /* BIOS ROM */
88 add_mmio_resource(dev, 0xfec, IO_APIC_ADDR, 0x00001000); /* IOAPIC */
Lee Leahy77ff0b12015-05-05 15:07:29 -070089}
90
91/* Default IO range claimed by the LPC device. The upper bound is exclusive. */
92#define LPC_DEFAULT_IO_RANGE_LOWER 0
93#define LPC_DEFAULT_IO_RANGE_UPPER 0x1000
94
Frans Hendriks2c630172019-04-02 15:06:29 +020095static void sc_enable_serial_irqs(struct device *dev)
96{
97 u8 *ilb_base = (u8 *)(pci_read_config32(dev, IBASE) & ~0xF);
98
99 printk(BIOS_SPEW, "Enable serial irq\n");
100 write32(ilb_base + ILB_OIC, read32(ilb_base + ILB_OIC) | SIRQEN);
101 write8(ilb_base + SCNT, read8(ilb_base + SCNT) | SCNT_MODE);
102}
103
Frans Hendriks93484132018-12-10 12:38:16 +0100104/*
105 * Write PCI config space IRQ assignments. PCI devices have the INT_LINE
106 * (0x3C) and INT_PIN (0x3D) registers which report interrupt routing
107 * information to operating systems and drivers. The INT_PIN register is
108 * generally read only and reports which interrupt pin A - D it uses. The
109 * INT_LINE register is configurable and reports which IRQ (generally the
110 * PIC IRQs 1 - 15) it will use. This needs to take interrupt pin swizzling
111 * on devices that are downstream on a PCI bridge into account.
112 *
113 * This function will loop through all enabled PCI devices and program the
114 * INT_LINE register with the correct PIC IRQ number for the INT_PIN that it
115 * uses. It then configures each interrupt in the pic to be level triggered.
116 */
117static void write_pci_config_irqs(void)
118{
119 struct device *irq_dev;
120 struct device *targ_dev;
121 uint8_t int_line = 0;
122 uint8_t original_int_pin = 0;
123 uint8_t new_int_pin = 0;
124 uint16_t current_bdf = 0;
125 uint16_t parent_bdf = 0;
126 uint8_t pirq = 0;
127 uint8_t device_num = 0;
128 const struct soc_irq_route *ir = &global_soc_irq_route;
129
130 if (ir == NULL) {
131 printk(BIOS_WARNING, "Warning: Can't write PCI IRQ assignments"
132 " because 'global_braswell_irq_route' structure does"
133 " not exist\n");
134 return;
135 }
136
137 /*
138 * Loop through all enabled devices and program their
139 * INT_LINE, INT_PIN registers from values taken from
140 * the Interrupt Route registers in the ILB
141 */
142 printk(BIOS_DEBUG, "PCI_CFG IRQ: Write PIRQ assignments\n");
143 for (irq_dev = all_devices; irq_dev; irq_dev = irq_dev->next) {
144
145 if ((irq_dev->path.type != DEVICE_PATH_PCI) ||
146 (!irq_dev->enabled))
147 continue;
148
149 current_bdf = irq_dev->path.pci.devfn |
150 irq_dev->bus->secondary << 8;
151
152 /*
153 * Step 1: Get the INT_PIN and device structure to look for
154 * in the pirq_data table defined in the mainboard directory.
155 */
156 targ_dev = NULL;
157 new_int_pin = get_pci_irq_pins(irq_dev, &targ_dev);
158 if (targ_dev == NULL || new_int_pin < 1)
159 continue;
160
161 /* Get the original INT_PIN for record keeping */
162 original_int_pin = pci_read_config8(irq_dev, PCI_INTERRUPT_PIN);
163
164 parent_bdf = targ_dev->path.pci.devfn
165 | targ_dev->bus->secondary << 8;
166 device_num = PCI_SLOT(parent_bdf);
167
168 if (ir->pcidev[device_num] == 0) {
169 printk(BIOS_WARNING,
170 "Warning: PCI Device %d does not have an IRQ "
171 "entry, skipping it\n", device_num);
172 continue;
173 }
174
175 /* Find the PIRQ that is attached to the INT_PIN */
176 pirq = (ir->pcidev[device_num] >> ((new_int_pin - 1) * 4))
177 & 0x7;
178
179 /* Get the INT_LINE this device/function will use */
180 int_line = ir->pic[pirq];
181
182 if (int_line != PIRQ_PIC_IRQDISABLE) {
183 /* Set this IRQ to level triggered */
184 i8259_configure_irq_trigger(int_line,
185 IRQ_LEVEL_TRIGGERED);
186 /* Set the Interrupt Line register */
187 pci_write_config8(irq_dev, PCI_INTERRUPT_LINE,
188 int_line);
189 } else {
190 /*
191 * Set the Interrupt line register as 'unknown' or
192 * 'unused'
193 */
194 pci_write_config8(irq_dev, PCI_INTERRUPT_LINE,
195 PIRQ_PIC_UNKNOWN_UNUSED);
196 }
197
198 printk(BIOS_SPEW, "\tINT_PIN\t\t: %d (%s)\n",
199 original_int_pin, pin_to_str(original_int_pin));
200 if (parent_bdf != current_bdf)
201 printk(BIOS_SPEW, "\tSwizzled to\t: %d (%s)\n",
202 new_int_pin, pin_to_str(new_int_pin));
203 printk(BIOS_SPEW, "\tPIRQ\t\t: %c\n"
204 "\tINT_LINE\t: 0x%X (IRQ %d)\n",
205 'A' + pirq, int_line, int_line);
206 }
207 printk(BIOS_DEBUG, "PCI_CFG IRQ: Finished writing PIRQ assignments\n");
208}
209
Lee Leahy77ff0b12015-05-05 15:07:29 -0700210static inline int io_range_in_default(int base, int size)
211{
212 /* Does it start above the range? */
213 if (base >= LPC_DEFAULT_IO_RANGE_UPPER)
214 return 0;
215
216 /* Is it entirely contained? */
217 if (base >= LPC_DEFAULT_IO_RANGE_LOWER &&
218 (base + size) < LPC_DEFAULT_IO_RANGE_UPPER)
219 return 1;
220
221 /* This will return not in range for partial overlaps. */
222 return 0;
223}
224
225/*
226 * Note: this function assumes there is no overlap with the default LPC device's
227 * claimed range: LPC_DEFAULT_IO_RANGE_LOWER -> LPC_DEFAULT_IO_RANGE_UPPER.
228 */
Elyes HAOUASb13fac32018-05-24 22:29:44 +0200229static void sc_add_io_resource(struct device *dev, int base, int size,
230 int index)
Lee Leahy77ff0b12015-05-05 15:07:29 -0700231{
232 struct resource *res;
233
Elyes HAOUASa342f392018-10-17 10:56:26 +0200234 printk(BIOS_SPEW, "%s/%s (%s, 0x%08x, 0x%08x, 0x%08x)\n",
Lee Leahy32471722015-04-20 15:20:28 -0700235 __FILE__, __func__, dev_name(dev), base, size, index);
236
Lee Leahy77ff0b12015-05-05 15:07:29 -0700237 if (io_range_in_default(base, size))
238 return;
239
240 res = new_resource(dev, index);
241 res->base = base;
242 res->size = size;
243 res->flags = IORESOURCE_IO | IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
244}
245
Elyes HAOUASb13fac32018-05-24 22:29:44 +0200246static void sc_add_io_resources(struct device *dev)
Lee Leahy77ff0b12015-05-05 15:07:29 -0700247{
248 struct resource *res;
249
Elyes HAOUASa342f392018-10-17 10:56:26 +0200250 printk(BIOS_SPEW, "%s/%s (%s)\n",
Lee Leahy32471722015-04-20 15:20:28 -0700251 __FILE__, __func__, dev_name(dev));
252
Lee Leahy77ff0b12015-05-05 15:07:29 -0700253 /* Add the default claimed IO range for the LPC device. */
254 res = new_resource(dev, 0);
255 res->base = LPC_DEFAULT_IO_RANGE_LOWER;
256 res->size = LPC_DEFAULT_IO_RANGE_UPPER - LPC_DEFAULT_IO_RANGE_LOWER;
257 res->flags = IORESOURCE_IO | IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
258
259 /* GPIO */
Frans Hendriks4b2c12f2018-11-22 07:52:38 +0100260 sc_add_io_resource(dev, GPIO_BASE_ADDRESS, GPIO_BASE_SIZE, GBASE);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700261
262 /* ACPI */
Frans Hendriks4b2c12f2018-11-22 07:52:38 +0100263 sc_add_io_resource(dev, ACPI_BASE_ADDRESS, ACPI_BASE_SIZE, ABASE);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700264}
265
Elyes HAOUASb13fac32018-05-24 22:29:44 +0200266static void sc_read_resources(struct device *dev)
Lee Leahy77ff0b12015-05-05 15:07:29 -0700267{
Elyes HAOUASa342f392018-10-17 10:56:26 +0200268 printk(BIOS_SPEW, "%s/%s (%s)\n",
Lee Leahy32471722015-04-20 15:20:28 -0700269 __FILE__, __func__, dev_name(dev));
270
Lee Leahy77ff0b12015-05-05 15:07:29 -0700271 /* Get the normal PCI resources of this device. */
272 pci_dev_read_resources(dev);
273
274 /* Add non-standard MMIO resources. */
275 sc_add_mmio_resources(dev);
276
277 /* Add IO resources. */
278 sc_add_io_resources(dev);
279}
280
Elyes HAOUASb13fac32018-05-24 22:29:44 +0200281static void sc_init(struct device *dev)
Lee Leahy77ff0b12015-05-05 15:07:29 -0700282{
283 int i;
Lee Leahy32471722015-04-20 15:20:28 -0700284 const unsigned long pr_base = ILB_BASE_ADDRESS + 0x08;
285 const unsigned long ir_base = ILB_BASE_ADDRESS + 0x20;
Frans Hendriksb55cd542019-03-06 14:45:12 +0100286 const unsigned long ilb_base = ILB_BASE_ADDRESS;
Lee Leahy32471722015-04-20 15:20:28 -0700287 void *gen_pmcon1 = (void *)(PMC_BASE_ADDRESS + GEN_PMCON1);
Lee Leahy32471722015-04-20 15:20:28 -0700288 const struct soc_irq_route *ir = &global_soc_irq_route;
289 struct soc_intel_braswell_config *config = dev->chip_info;
290
Elyes HAOUASa342f392018-10-17 10:56:26 +0200291 printk(BIOS_SPEW, "%s/%s (%s)\n",
Lee Leahy32471722015-04-20 15:20:28 -0700292 __FILE__, __func__, dev_name(dev));
Lee Leahy77ff0b12015-05-05 15:07:29 -0700293
Frans Hendriksb55cd542019-03-06 14:45:12 +0100294 /* Set the value for PCI command register. */
295 pci_write_config16(dev, PCI_COMMAND,
296 PCI_COMMAND_IO | PCI_COMMAND_MEMORY |
297 PCI_COMMAND_MASTER | PCI_COMMAND_SPECIAL);
298
299 /* Use IRQ9 for SCI Interrupt */
300 write32((void *)(ilb_base + ACTL), 0);
301
Frans Hendriks255f35c2018-12-11 15:21:47 +0100302 isa_dma_init();
303
Frans Hendriks2c630172019-04-02 15:06:29 +0200304 sc_enable_serial_irqs(dev);
305
Lee Leahy77ff0b12015-05-05 15:07:29 -0700306 /* Set up the PIRQ PIC routing based on static config. */
Lee Leahy32471722015-04-20 15:20:28 -0700307 for (i = 0; i < NUM_PIRQS; i++)
308 write8((void *)(pr_base + i*sizeof(ir->pic[i])),
309 ir->pic[i]);
310
Lee Leahy77ff0b12015-05-05 15:07:29 -0700311 /* Set up the per device PIRQ routing base on static config. */
Lee Leahy32471722015-04-20 15:20:28 -0700312 for (i = 0; i < NUM_IR_DEVS; i++)
313 write16((void *)(ir_base + i*sizeof(ir->pcidev[i])),
314 ir->pcidev[i]);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700315
Frans Hendriks93484132018-12-10 12:38:16 +0100316 /* Interrupt 9 should be level triggered (SCI) */
317 i8259_configure_irq_trigger(9, 1);
318
319 for (i = 0; i < NUM_PIRQS; i++) {
320 if (ir->pic[i])
321 i8259_configure_irq_trigger(ir->pic[i], 1);
322 }
Lee Leahy77ff0b12015-05-05 15:07:29 -0700323
Lee Leahy77ff0b12015-05-05 15:07:29 -0700324 if (config->disable_slp_x_stretch_sus_fail) {
325 printk(BIOS_DEBUG, "Disabling slp_x stretching.\n");
326 write32(gen_pmcon1,
327 read32(gen_pmcon1) | DIS_SLP_X_STRCH_SUS_UP);
328 } else {
329 write32(gen_pmcon1,
330 read32(gen_pmcon1) & ~DIS_SLP_X_STRCH_SUS_UP);
331 }
332
Frans Hendriks93484132018-12-10 12:38:16 +0100333 /* Write IRQ assignments to PCI config space */
334 write_pci_config_irqs();
335
336 /* Initialize i8259 pic */
337 setup_i8259();
338
Frans Hendriksbd5233e2018-12-05 15:24:48 +0100339 /* Initialize i8254 timers */
340 setup_i8254();
Frans Hendriks2c630172019-04-02 15:06:29 +0200341
342 sc_set_serial_irqs_mode(dev, config->serirq_mode);
343
Lee Leahy77ff0b12015-05-05 15:07:29 -0700344}
345
346/*
347 * Common code for the south cluster devices.
348 */
349
Lee Leahy32471722015-04-20 15:20:28 -0700350/* Set bit in function disble register to hide this device. */
Elyes HAOUASb13fac32018-05-24 22:29:44 +0200351static void sc_disable_devfn(struct device *dev)
Lee Leahy77ff0b12015-05-05 15:07:29 -0700352{
Lee Leahy32471722015-04-20 15:20:28 -0700353 void *func_dis = (void *)(PMC_BASE_ADDRESS + FUNC_DIS);
354 void *func_dis2 = (void *)(PMC_BASE_ADDRESS + FUNC_DIS2);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700355 uint32_t mask = 0;
356 uint32_t mask2 = 0;
357
Elyes HAOUASa342f392018-10-17 10:56:26 +0200358 printk(BIOS_SPEW, "%s/%s (%s)\n",
Lee Leahy32471722015-04-20 15:20:28 -0700359 __FILE__, __func__, dev_name(dev));
360
361#define SET_DIS_MASK(name_) \
362 case PCI_DEVFN(name_ ## _DEV, name_ ## _FUNC): \
363 mask |= name_ ## _DIS
364#define SET_DIS_MASK2(name_) \
365 case PCI_DEVFN(name_ ## _DEV, name_ ## _FUNC): \
366 mask2 |= name_ ## _DIS
367
Lee Leahy77ff0b12015-05-05 15:07:29 -0700368 switch (dev->path.pci.devfn) {
Lee Leahy32471722015-04-20 15:20:28 -0700369 SET_DIS_MASK(SDIO);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700370 break;
Lee Leahy32471722015-04-20 15:20:28 -0700371 SET_DIS_MASK(SD);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700372 break;
Lee Leahy32471722015-04-20 15:20:28 -0700373 SET_DIS_MASK(SATA);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700374 break;
Lee Leahy32471722015-04-20 15:20:28 -0700375 SET_DIS_MASK(XHCI);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700376 /* Disable super speed PHY when XHCI is not available. */
377 mask2 |= USH_SS_PHY_DIS;
378 break;
Lee Leahy32471722015-04-20 15:20:28 -0700379 SET_DIS_MASK(LPE);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700380 break;
Lee Leahy32471722015-04-20 15:20:28 -0700381 SET_DIS_MASK(MMC);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700382 break;
Lee Leahy32471722015-04-20 15:20:28 -0700383 SET_DIS_MASK(SIO_DMA1);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700384 break;
Lee Leahy32471722015-04-20 15:20:28 -0700385 SET_DIS_MASK(I2C1);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700386 break;
Lee Leahy32471722015-04-20 15:20:28 -0700387 SET_DIS_MASK(I2C2);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700388 break;
Lee Leahy32471722015-04-20 15:20:28 -0700389 SET_DIS_MASK(I2C3);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700390 break;
Lee Leahy32471722015-04-20 15:20:28 -0700391 SET_DIS_MASK(I2C4);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700392 break;
Lee Leahy32471722015-04-20 15:20:28 -0700393 SET_DIS_MASK(I2C5);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700394 break;
Lee Leahy32471722015-04-20 15:20:28 -0700395 SET_DIS_MASK(I2C6);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700396 break;
Lee Leahy32471722015-04-20 15:20:28 -0700397 SET_DIS_MASK(I2C7);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700398 break;
Lee Leahy32471722015-04-20 15:20:28 -0700399 SET_DIS_MASK(TXE);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700400 break;
Lee Leahy32471722015-04-20 15:20:28 -0700401 SET_DIS_MASK(HDA);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700402 break;
Lee Leahy32471722015-04-20 15:20:28 -0700403 SET_DIS_MASK(PCIE_PORT1);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700404 break;
Lee Leahy32471722015-04-20 15:20:28 -0700405 SET_DIS_MASK(PCIE_PORT2);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700406 break;
Lee Leahy32471722015-04-20 15:20:28 -0700407 SET_DIS_MASK(PCIE_PORT3);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700408 break;
Lee Leahy32471722015-04-20 15:20:28 -0700409 SET_DIS_MASK(PCIE_PORT4);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700410 break;
Lee Leahy32471722015-04-20 15:20:28 -0700411 SET_DIS_MASK(SIO_DMA2);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700412 break;
Lee Leahy32471722015-04-20 15:20:28 -0700413 SET_DIS_MASK(PWM1);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700414 break;
Lee Leahy32471722015-04-20 15:20:28 -0700415 SET_DIS_MASK(PWM2);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700416 break;
Lee Leahy32471722015-04-20 15:20:28 -0700417 SET_DIS_MASK(HSUART1);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700418 break;
Lee Leahy32471722015-04-20 15:20:28 -0700419 SET_DIS_MASK(HSUART2);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700420 break;
Lee Leahy32471722015-04-20 15:20:28 -0700421 SET_DIS_MASK(SPI);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700422 break;
Lee Leahy32471722015-04-20 15:20:28 -0700423 SET_DIS_MASK2(SMBUS);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700424 break;
425 }
426
427 if (mask != 0) {
428 write32(func_dis, read32(func_dis) | mask);
429 /* Ensure posted write hits. */
430 read32(func_dis);
431 }
432
433 if (mask2 != 0) {
434 write32(func_dis2, read32(func_dis2) | mask2);
435 /* Ensure posted write hits. */
436 read32(func_dis2);
437 }
438}
439
Elyes HAOUASb13fac32018-05-24 22:29:44 +0200440static inline void set_d3hot_bits(struct device *dev, int offset)
Lee Leahy77ff0b12015-05-05 15:07:29 -0700441{
442 uint32_t reg8;
Lee Leahy32471722015-04-20 15:20:28 -0700443
Elyes HAOUASa342f392018-10-17 10:56:26 +0200444 printk(BIOS_SPEW, "%s/%s (%s, 0x%08x)\n",
Lee Leahy32471722015-04-20 15:20:28 -0700445 __FILE__, __func__, dev_name(dev), offset);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700446 printk(BIOS_DEBUG, "Power management CAP offset 0x%x.\n", offset);
447 reg8 = pci_read_config8(dev, offset + 4);
448 reg8 |= 0x3;
449 pci_write_config8(dev, offset + 4, reg8);
450}
451
Lee Leahy32471722015-04-20 15:20:28 -0700452/*
453 * Parts of the audio subsystem are powered by the HDA device. Therefore, one
Lee Leahy77ff0b12015-05-05 15:07:29 -0700454 * cannot put HDA into D3Hot. Instead perform this workaround to make some of
Lee Leahy32471722015-04-20 15:20:28 -0700455 * the audio paths work for LPE audio.
456 */
Elyes HAOUASb13fac32018-05-24 22:29:44 +0200457static void hda_work_around(struct device *dev)
Lee Leahy77ff0b12015-05-05 15:07:29 -0700458{
Lee Leahy32471722015-04-20 15:20:28 -0700459 void *gctl = (void *)(TEMP_BASE_ADDRESS + 0x8);
460
Elyes HAOUASa342f392018-10-17 10:56:26 +0200461 printk(BIOS_SPEW, "%s/%s (%s)\n",
Lee Leahy32471722015-04-20 15:20:28 -0700462 __FILE__, __func__, dev_name(dev));
Lee Leahy77ff0b12015-05-05 15:07:29 -0700463
464 /* Need to set magic register 0x43 to 0xd7 in config space. */
465 pci_write_config8(dev, 0x43, 0xd7);
466
Lee Leahy32471722015-04-20 15:20:28 -0700467 /*
468 * Need to set bit 0 of GCTL to take the device out of reset. However,
469 * that requires setting up the 64-bit BAR.
470 */
Lee Leahy77ff0b12015-05-05 15:07:29 -0700471 pci_write_config32(dev, PCI_BASE_ADDRESS_0, TEMP_BASE_ADDRESS);
472 pci_write_config32(dev, PCI_BASE_ADDRESS_1, 0);
473 pci_write_config8(dev, PCI_COMMAND, PCI_COMMAND_MEMORY);
474 write32(gctl, read32(gctl) | 0x1);
475 pci_write_config8(dev, PCI_COMMAND, 0);
476 pci_write_config32(dev, PCI_BASE_ADDRESS_0, 0);
477}
478
Elyes HAOUASb13fac32018-05-24 22:29:44 +0200479static int place_device_in_d3hot(struct device *dev)
Lee Leahy77ff0b12015-05-05 15:07:29 -0700480{
Lee Leahy1072e7d2017-03-16 17:35:32 -0700481 unsigned int offset;
Lee Leahy77ff0b12015-05-05 15:07:29 -0700482
Elyes HAOUASa342f392018-10-17 10:56:26 +0200483 printk(BIOS_SPEW, "%s/%s (%s)\n",
Lee Leahy32471722015-04-20 15:20:28 -0700484 __FILE__, __func__, dev_name(dev));
485
486 /*
487 * Parts of the HDA block are used for LPE audio as well.
488 * Therefore assume the HDA will never be put into D3Hot.
489 */
Lee Leahy77ff0b12015-05-05 15:07:29 -0700490 if (dev->path.pci.devfn == PCI_DEVFN(HDA_DEV, HDA_FUNC)) {
491 hda_work_around(dev);
492 return 0;
493 }
494
495 offset = pci_find_capability(dev, PCI_CAP_ID_PM);
496
497 if (offset != 0) {
498 set_d3hot_bits(dev, offset);
499 return 0;
500 }
501
Lee Leahy32471722015-04-20 15:20:28 -0700502 /*
503 * For some reason some of the devices don't have the capability
504 * pointer set correctly. Work around this by hard coding the offset.
505 */
506#define DEV_CASE(name_) \
507 case PCI_DEVFN(name_ ## _DEV, name_ ## _FUNC)
508
Lee Leahy77ff0b12015-05-05 15:07:29 -0700509 switch (dev->path.pci.devfn) {
Lee Leahy32471722015-04-20 15:20:28 -0700510 DEV_CASE(SDIO) :
511 DEV_CASE(SD) :
512 DEV_CASE(MMC) :
513 DEV_CASE(LPE) :
514 DEV_CASE(SIO_DMA1) :
515 DEV_CASE(I2C1) :
516 DEV_CASE(I2C2) :
517 DEV_CASE(I2C3) :
518 DEV_CASE(I2C4) :
519 DEV_CASE(I2C5) :
520 DEV_CASE(I2C6) :
521 DEV_CASE(I2C7) :
522 DEV_CASE(SIO_DMA2) :
523 DEV_CASE(PWM1) :
524 DEV_CASE(PWM2) :
525 DEV_CASE(HSUART1) :
526 DEV_CASE(HSUART2) :
527 DEV_CASE(SPI) :
Lee Leahy77ff0b12015-05-05 15:07:29 -0700528 offset = 0x80;
529 break;
Lee Leahy32471722015-04-20 15:20:28 -0700530 DEV_CASE(SATA) :
531 DEV_CASE(XHCI) :
Lee Leahy77ff0b12015-05-05 15:07:29 -0700532 offset = 0x70;
533 break;
Lee Leahy32471722015-04-20 15:20:28 -0700534 DEV_CASE(HDA) :
535 DEV_CASE(SMBUS) :
Lee Leahy77ff0b12015-05-05 15:07:29 -0700536 offset = 0x50;
537 break;
Lee Leahy32471722015-04-20 15:20:28 -0700538 DEV_CASE(TXE) :
Lee Leahy77ff0b12015-05-05 15:07:29 -0700539 /* TXE cannot be placed in D3Hot. */
540 return 0;
Lee Leahy32471722015-04-20 15:20:28 -0700541 DEV_CASE(PCIE_PORT1) :
542 DEV_CASE(PCIE_PORT2) :
543 DEV_CASE(PCIE_PORT3) :
544 DEV_CASE(PCIE_PORT4) :
Lee Leahy77ff0b12015-05-05 15:07:29 -0700545 offset = 0xa0;
546 break;
547 }
548
549 if (offset != 0) {
550 set_d3hot_bits(dev, offset);
551 return 0;
552 }
553
554 return -1;
555}
556
557/* Common PCI device function disable. */
Elyes HAOUASb13fac32018-05-24 22:29:44 +0200558void southcluster_enable_dev(struct device *dev)
Lee Leahy77ff0b12015-05-05 15:07:29 -0700559{
560 uint32_t reg32;
561
Elyes HAOUASa342f392018-10-17 10:56:26 +0200562 printk(BIOS_SPEW, "%s/%s (%s)\n",
Lee Leahy32471722015-04-20 15:20:28 -0700563 __FILE__, __func__, dev_name(dev));
Lee Leahy77ff0b12015-05-05 15:07:29 -0700564 if (!dev->enabled) {
565 int slot = PCI_SLOT(dev->path.pci.devfn);
566 int func = PCI_FUNC(dev->path.pci.devfn);
567 printk(BIOS_DEBUG, "%s: Disabling device: %02x.%01x\n",
568 dev_path(dev), slot, func);
569
570 /* Ensure memory, io, and bus master are all disabled */
571 reg32 = pci_read_config32(dev, PCI_COMMAND);
572 reg32 &= ~(PCI_COMMAND_MASTER |
573 PCI_COMMAND_MEMORY | PCI_COMMAND_IO);
574 pci_write_config32(dev, PCI_COMMAND, reg32);
575
576 /* Place device in D3Hot */
577 if (place_device_in_d3hot(dev) < 0) {
578 printk(BIOS_WARNING,
579 "Could not place %02x.%01x into D3Hot. "
580 "Keeping device visible.\n", slot, func);
581 return;
582 }
583 /* Disable this device if possible */
584 sc_disable_devfn(dev);
585 } else {
586 /* Enable SERR */
587 reg32 = pci_read_config32(dev, PCI_COMMAND);
588 reg32 |= PCI_COMMAND_SERR;
589 pci_write_config32(dev, PCI_COMMAND, reg32);
590 }
591}
592
593static struct device_operations device_ops = {
594 .read_resources = sc_read_resources,
595 .set_resources = pci_dev_set_resources,
596 .enable_resources = NULL,
Lee Leahy2bc9cee2015-06-30 15:25:44 -0700597 .acpi_inject_dsdt_generator = southcluster_inject_dsdt,
598 .write_acpi_tables = southcluster_write_acpi_tables,
Lee Leahy77ff0b12015-05-05 15:07:29 -0700599 .init = sc_init,
600 .enable = southcluster_enable_dev,
Lee Leahy32471722015-04-20 15:20:28 -0700601 .scan_bus = scan_lpc_bus,
Lee Leahy77ff0b12015-05-05 15:07:29 -0700602 .ops_pci = &soc_pci_ops,
603};
604
605static const struct pci_driver southcluster __pci_driver = {
606 .ops = &device_ops,
607 .vendor = PCI_VENDOR_ID_INTEL,
608 .device = LPC_DEVID,
609};
610
Aaron Durbin64031672018-04-21 14:45:32 -0600611int __weak mainboard_get_spi_config(struct spi_config *cfg)
Lee Leahy77ff0b12015-05-05 15:07:29 -0700612{
Elyes HAOUASa342f392018-10-17 10:56:26 +0200613 printk(BIOS_SPEW, "%s/%s (0x%p)\n",
Lee Leahy32471722015-04-20 15:20:28 -0700614 __FILE__, __func__, (void *)cfg);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700615 return -1;
616}
617
618static void finalize_chipset(void *unused)
619{
Lee Leahy32471722015-04-20 15:20:28 -0700620 void *bcr = (void *)(SPI_BASE_ADDRESS + BCR);
621 void *gcs = (void *)(RCBA_BASE_ADDRESS + GCS);
622 void *gen_pmcon2 = (void *)(PMC_BASE_ADDRESS + GEN_PMCON2);
623 void *etr = (void *)(PMC_BASE_ADDRESS + ETR);
624 uint8_t *spi = (uint8_t *)SPI_BASE_ADDRESS;
Lee Leahy77ff0b12015-05-05 15:07:29 -0700625 struct spi_config cfg;
626
Elyes HAOUASa342f392018-10-17 10:56:26 +0200627 printk(BIOS_SPEW, "%s/%s (0x%p)\n",
Lee Leahy32471722015-04-20 15:20:28 -0700628 __FILE__, __func__, unused);
629
Lee Leahy77ff0b12015-05-05 15:07:29 -0700630 /* Set the lock enable on the BIOS control register. */
631 write32(bcr, read32(bcr) | BCR_LE);
632
633 /* Set BIOS lock down bit controlling boot block size and swapping. */
634 write32(gcs, read32(gcs) | BILD);
635
636 /* Lock sleep stretching policy and set SMI lock. */
637 write32(gen_pmcon2, read32(gen_pmcon2) | SLPSX_STR_POL_LOCK | SMI_LOCK);
638
639 /* Set the CF9 lock. */
640 write32(etr, read32(etr) | CF9LOCK);
641
642 if (mainboard_get_spi_config(&cfg) < 0) {
643 printk(BIOS_DEBUG, "No SPI lockdown configuration.\n");
644 } else {
645 write16(spi + PREOP, cfg.preop);
646 write16(spi + OPTYPE, cfg.optype);
647 write32(spi + OPMENU0, cfg.opmenu[0]);
648 write32(spi + OPMENU1, cfg.opmenu[1]);
649 write16(spi + HSFSTS, read16(spi + HSFSTS) | FLOCKDN);
650 write32(spi + UVSCC, cfg.uvscc);
651 write32(spi + LVSCC, cfg.lvscc | VCL);
652 }
Lee Leahy32471722015-04-20 15:20:28 -0700653 spi_init();
Lee Leahy77ff0b12015-05-05 15:07:29 -0700654
655 printk(BIOS_DEBUG, "Finalizing SMM.\n");
656 outb(APM_CNT_FINALIZE, APM_CNT);
657}
658
Hannah Williams2cfdde72015-04-15 19:48:07 -0700659BOOT_STATE_INIT_ENTRY(BS_POST_DEVICE, BS_ON_EXIT, finalize_chipset, NULL);