blob: 45c588a1654f4b9bbcb7535ead242176fbae580a [file] [log] [blame]
Angel Ponsc3f58f62020-04-05 15:46:41 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Aaron Durbine18d68f2013-10-24 00:05:31 -05002
3#include <stdint.h>
4#include <arch/io.h>
Kyösti Mälkki13f66502019-03-03 08:01:05 +02005#include <device/mmio.h>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +02006#include <device/pci_ops.h>
Furquan Shaikh76cedd22020-05-02 10:24:23 -07007#include <acpi/acpi.h>
Kyösti Mälkki0c1dd9c2020-06-17 23:37:49 +03008#include <acpi/acpi_gnvs.h>
Aaron Durbin4177db52014-02-05 14:55:26 -06009#include <bootstate.h>
Shawn Nematbakhsh51d787a2014-01-16 17:52:21 -080010#include <cbmem.h>
Aaron Durbind7bc23a2013-10-29 16:37:10 -050011#include <console/console.h>
Aaron Durbin4177db52014-02-05 14:55:26 -060012#include <cpu/x86/smm.h>
Aaron Durbine18d68f2013-10-24 00:05:31 -050013#include <device/device.h>
14#include <device/pci.h>
15#include <device/pci_ids.h>
Shawn Nematbakhsh51d787a2014-01-16 17:52:21 -080016#include <pc80/mc146818rtc.h>
Kein Yuan35110232014-02-22 12:26:55 -080017#include <drivers/uart/uart8250reg.h>
Elyes HAOUASa1e22b82019-03-18 22:49:36 +010018#include <string.h>
Aaron Durbine18d68f2013-10-24 00:05:31 -050019
Julius Werner18ea2d32014-10-07 16:42:17 -070020#include <soc/iomap.h>
21#include <soc/irq.h>
22#include <soc/lpc.h>
23#include <soc/nvs.h>
24#include <soc/pci_devs.h>
Angel Ponsb5320b22020-07-07 18:27:30 +020025#include <soc/pm.h>
Julius Werner18ea2d32014-10-07 16:42:17 -070026#include <soc/ramstage.h>
27#include <soc/spi.h>
Shawn Nematbakhsh51d787a2014-01-16 17:52:21 -080028#include "chip.h"
Furquan Shaikh76cedd22020-05-02 10:24:23 -070029#include <acpi/acpigen.h>
Aaron Durbine18d68f2013-10-24 00:05:31 -050030
Angel Ponsc4d4b542020-07-07 19:00:07 +020031static inline void add_mmio_resource(struct device *dev, int i, unsigned long addr,
32 unsigned long size)
Aaron Durbine18d68f2013-10-24 00:05:31 -050033{
34 mmio_resource(dev, i, addr >> 10, size >> 10);
35}
36
Elyes HAOUAS17a3ceb2018-05-22 10:42:28 +020037static void sc_add_mmio_resources(struct device *dev)
Aaron Durbine18d68f2013-10-24 00:05:31 -050038{
Duncan Laurie7fbe20b2013-11-04 17:00:22 -080039 add_mmio_resource(dev, 0xfeb, ABORT_BASE_ADDRESS, ABORT_BASE_SIZE);
40 add_mmio_resource(dev, PBASE, PMC_BASE_ADDRESS, PMC_BASE_SIZE);
41 add_mmio_resource(dev, IOBASE, IO_BASE_ADDRESS, IO_BASE_SIZE);
42 add_mmio_resource(dev, IBASE, ILB_BASE_ADDRESS, ILB_BASE_SIZE);
43 add_mmio_resource(dev, SBASE, SPI_BASE_ADDRESS, SPI_BASE_SIZE);
44 add_mmio_resource(dev, MPBASE, MPHY_BASE_ADDRESS, MPHY_BASE_SIZE);
45 add_mmio_resource(dev, PUBASE, PUNIT_BASE_ADDRESS, PUNIT_BASE_SIZE);
46 add_mmio_resource(dev, RCBA, RCBA_BASE_ADDRESS, RCBA_BASE_SIZE);
Aaron Durbine18d68f2013-10-24 00:05:31 -050047}
48
49/* Default IO range claimed by the LPC device. The upper bound is exclusive. */
50#define LPC_DEFAULT_IO_RANGE_LOWER 0
51#define LPC_DEFAULT_IO_RANGE_UPPER 0x1000
52
53static inline int io_range_in_default(int base, int size)
54{
55 /* Does it start above the range? */
56 if (base >= LPC_DEFAULT_IO_RANGE_UPPER)
57 return 0;
58
59 /* Is it entirely contained? */
Angel Ponsc4d4b542020-07-07 19:00:07 +020060 if (base >= LPC_DEFAULT_IO_RANGE_LOWER && (base + size) < LPC_DEFAULT_IO_RANGE_UPPER)
Aaron Durbine18d68f2013-10-24 00:05:31 -050061 return 1;
62
Angel Pons26b49cc2020-07-07 17:17:51 +020063 /* This will return not in range for partial overlaps */
Aaron Durbine18d68f2013-10-24 00:05:31 -050064 return 0;
65}
66
67/*
68 * Note: this function assumes there is no overlap with the default LPC device's
69 * claimed range: LPC_DEFAULT_IO_RANGE_LOWER -> LPC_DEFAULT_IO_RANGE_UPPER.
70 */
Angel Ponsc4d4b542020-07-07 19:00:07 +020071static void sc_add_io_resource(struct device *dev, int base, int size, int index)
Aaron Durbine18d68f2013-10-24 00:05:31 -050072{
73 struct resource *res;
74
75 if (io_range_in_default(base, size))
76 return;
77
78 res = new_resource(dev, index);
79 res->base = base;
80 res->size = size;
81 res->flags = IORESOURCE_IO | IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
82}
83
Elyes HAOUAS17a3ceb2018-05-22 10:42:28 +020084static void sc_add_io_resources(struct device *dev)
Aaron Durbine18d68f2013-10-24 00:05:31 -050085{
86 struct resource *res;
87
88 /* Add the default claimed IO range for the LPC device. */
89 res = new_resource(dev, 0);
90 res->base = LPC_DEFAULT_IO_RANGE_LOWER;
91 res->size = LPC_DEFAULT_IO_RANGE_UPPER - LPC_DEFAULT_IO_RANGE_LOWER;
92 res->flags = IORESOURCE_IO | IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
93
94 /* GPIO */
Frans Hendriks802f43d2018-10-29 14:17:16 +010095 sc_add_io_resource(dev, GPIO_BASE_ADDRESS, GPIO_BASE_SIZE, GBASE);
Aaron Durbine18d68f2013-10-24 00:05:31 -050096
97 /* ACPI */
Frans Hendriks802f43d2018-10-29 14:17:16 +010098 sc_add_io_resource(dev, ACPI_BASE_ADDRESS, ACPI_BASE_SIZE, ABASE);
Aaron Durbine18d68f2013-10-24 00:05:31 -050099}
100
Elyes HAOUAS17a3ceb2018-05-22 10:42:28 +0200101static void sc_read_resources(struct device *dev)
Aaron Durbine18d68f2013-10-24 00:05:31 -0500102{
103 /* Get the normal PCI resources of this device. */
104 pci_dev_read_resources(dev);
105
106 /* Add non-standard MMIO resources. */
107 sc_add_mmio_resources(dev);
108
109 /* Add IO resources. */
110 sc_add_io_resources(dev);
111}
112
Kein Yuan35110232014-02-22 12:26:55 -0800113/*
114 * The UART hardware loses power while in suspend. Because of this the kernel
115 * can hang because it doesn't re-initialize serial ports it is using for
116 * consoles at resume time. The following function configures the UART
117 * if the hardware is enabled though it may not be the correct baud rate
118 * or configuration. This is definitely a hack, but it helps the kernel
119 * along.
120 */
Elyes HAOUAS17a3ceb2018-05-22 10:42:28 +0200121static void com1_configure_resume(struct device *dev)
Kein Yuan35110232014-02-22 12:26:55 -0800122{
123 const uint16_t port = 0x3f8;
124
Martin Roth99a3bba2014-12-07 14:57:26 -0700125 /* Is the UART I/O port enabled? */
Kein Yuan35110232014-02-22 12:26:55 -0800126 if (!(pci_read_config32(dev, UART_CONT) & 1))
127 return;
128
129 /* Disable interrupts */
130 outb(0x0, port + UART8250_IER);
131
132 /* Enable FIFOs */
133 outb(UART8250_FCR_FIFO_EN, port + UART8250_FCR);
134
135 /* assert DTR and RTS so the other end is happy */
136 outb(UART8250_MCR_DTR | UART8250_MCR_RTS, port + UART8250_MCR);
137
138 /* DLAB on */
139 outb(UART8250_LCR_DLAB | 3, port + UART8250_LCR);
140
141 /* Set Baud Rate Divisor. 1 ==> 115200 Baud */
142 outb(1, port + UART8250_DLL);
143 outb(0, port + UART8250_DLM);
144
145 /* Set to 3 for 8N1 */
146 outb(3, port + UART8250_LCR);
147}
148
Elyes HAOUAS17a3ceb2018-05-22 10:42:28 +0200149static void sc_init(struct device *dev)
Aaron Durbin3bde3d72013-11-04 21:45:52 -0600150{
151 int i;
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800152 u8 *pr_base = (u8 *)(ILB_BASE_ADDRESS + 0x08);
Alexander Couzens316170e2015-11-24 09:46:18 +0100153 u16 *ir_base = (u16 *)(ILB_BASE_ADDRESS + 0x20);
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800154 u32 *gen_pmcon1 = (u32 *)(PMC_BASE_ADDRESS + GEN_PMCON1);
155 u32 *actl = (u32 *)(ILB_BASE_ADDRESS + ACTL);
Aaron Durbin3bde3d72013-11-04 21:45:52 -0600156 const struct baytrail_irq_route *ir = &global_baytrail_irq_route;
Kyösti Mälkki8950cfb2019-07-13 22:16:25 +0300157 struct soc_intel_baytrail_config *config = config_of(dev);
Aaron Durbin3bde3d72013-11-04 21:45:52 -0600158
159 /* Set up the PIRQ PIC routing based on static config. */
160 for (i = 0; i < NUM_PIRQS; i++) {
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800161 write8(pr_base + i, ir->pic[i]);
Aaron Durbin3bde3d72013-11-04 21:45:52 -0600162 }
163 /* Set up the per device PIRQ routing base on static config. */
164 for (i = 0; i < NUM_IR_DEVS; i++) {
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800165 write16(ir_base + i, ir->pcidev[i]);
Aaron Durbin3bde3d72013-11-04 21:45:52 -0600166 }
Aaron Durbin1af36632013-11-07 10:42:16 -0600167
168 /* Route SCI to IRQ9 */
169 write32(actl, (read32(actl) & ~SCIS_MASK) | SCIS_IRQ9);
Shawn Nematbakhsh51d787a2014-01-16 17:52:21 -0800170
Angel Ponsc4d4b542020-07-07 19:00:07 +0200171 cmos_init(rtc_failure());
Shawn Nematbakhsh51d787a2014-01-16 17:52:21 -0800172
173 if (config->disable_slp_x_stretch_sus_fail) {
174 printk(BIOS_DEBUG, "Disabling slp_x stretching.\n");
Angel Ponsc4d4b542020-07-07 19:00:07 +0200175 write32(gen_pmcon1, read32(gen_pmcon1) | DIS_SLP_X_STRCH_SUS_UP);
176
Shawn Nematbakhsh51d787a2014-01-16 17:52:21 -0800177 } else {
Angel Ponsc4d4b542020-07-07 19:00:07 +0200178 write32(gen_pmcon1, read32(gen_pmcon1) & ~DIS_SLP_X_STRCH_SUS_UP);
Shawn Nematbakhsh51d787a2014-01-16 17:52:21 -0800179 }
Kein Yuan35110232014-02-22 12:26:55 -0800180
Kyösti Mälkki9e94dbf2015-01-08 20:03:18 +0200181 if (acpi_is_wakeup_s3())
Kein Yuan35110232014-02-22 12:26:55 -0800182 com1_configure_resume(dev);
Aaron Durbin3bde3d72013-11-04 21:45:52 -0600183}
184
Aaron Durbind7bc23a2013-10-29 16:37:10 -0500185/*
186 * Common code for the south cluster devices.
187 */
188
Martin Roth99a3bba2014-12-07 14:57:26 -0700189/* Set bit in function disable register to hide this device. */
Elyes HAOUAS17a3ceb2018-05-22 10:42:28 +0200190static void sc_disable_devfn(struct device *dev)
Aaron Durbind7bc23a2013-10-29 16:37:10 -0500191{
Angel Ponse80d17f2020-07-07 17:25:38 +0200192 void *func_dis = (void *)(PMC_BASE_ADDRESS + FUNC_DIS);
193 void *func_dis2 = (void *)(PMC_BASE_ADDRESS + FUNC_DIS2);
Angel Ponsc4d4b542020-07-07 19:00:07 +0200194 uint32_t mask = 0;
Aaron Durbind7bc23a2013-10-29 16:37:10 -0500195 uint32_t mask2 = 0;
196
197 switch (dev->path.pci.devfn) {
Mate Kukri45b51e02020-07-03 14:44:49 +0200198 case PCI_DEVFN(MMC_DEV, MMC_FUNC):
199 mask |= MMC_DIS;
200 break;
Aaron Durbind7bc23a2013-10-29 16:37:10 -0500201 case PCI_DEVFN(SDIO_DEV, SDIO_FUNC):
202 mask |= SDIO_DIS;
203 break;
204 case PCI_DEVFN(SD_DEV, SD_FUNC):
205 mask |= SD_DIS;
206 break;
207 case PCI_DEVFN(SATA_DEV, SATA_FUNC):
208 mask |= SATA_DIS;
209 break;
210 case PCI_DEVFN(XHCI_DEV, XHCI_FUNC):
211 mask |= XHCI_DIS;
212 /* Disable super speed PHY when XHCI is not available. */
213 mask2 |= USH_SS_PHY_DIS;
214 break;
215 case PCI_DEVFN(LPE_DEV, LPE_FUNC):
216 mask |= LPE_DIS;
217 break;
Mate Kukri45b51e02020-07-03 14:44:49 +0200218 case PCI_DEVFN(MMC45_DEV, MMC45_FUNC):
219 mask |= MMC45_DIS;
Aaron Durbind7bc23a2013-10-29 16:37:10 -0500220 break;
221 case PCI_DEVFN(SIO_DMA1_DEV, SIO_DMA1_FUNC):
222 mask |= SIO_DMA1_DIS;
223 break;
224 case PCI_DEVFN(I2C1_DEV, I2C1_FUNC):
225 mask |= I2C1_DIS;
226 break;
227 case PCI_DEVFN(I2C2_DEV, I2C2_FUNC):
228 mask |= I2C1_DIS;
229 break;
230 case PCI_DEVFN(I2C3_DEV, I2C3_FUNC):
231 mask |= I2C3_DIS;
232 break;
233 case PCI_DEVFN(I2C4_DEV, I2C4_FUNC):
234 mask |= I2C4_DIS;
235 break;
236 case PCI_DEVFN(I2C5_DEV, I2C5_FUNC):
237 mask |= I2C5_DIS;
238 break;
239 case PCI_DEVFN(I2C6_DEV, I2C6_FUNC):
240 mask |= I2C6_DIS;
241 break;
242 case PCI_DEVFN(I2C7_DEV, I2C7_FUNC):
243 mask |= I2C7_DIS;
244 break;
245 case PCI_DEVFN(TXE_DEV, TXE_FUNC):
246 mask |= TXE_DIS;
247 break;
248 case PCI_DEVFN(HDA_DEV, HDA_FUNC):
249 mask |= HDA_DIS;
250 break;
251 case PCI_DEVFN(PCIE_PORT1_DEV, PCIE_PORT1_FUNC):
252 mask |= PCIE_PORT1_DIS;
253 break;
254 case PCI_DEVFN(PCIE_PORT2_DEV, PCIE_PORT2_FUNC):
255 mask |= PCIE_PORT2_DIS;
256 break;
257 case PCI_DEVFN(PCIE_PORT3_DEV, PCIE_PORT3_FUNC):
258 mask |= PCIE_PORT3_DIS;
259 break;
260 case PCI_DEVFN(PCIE_PORT4_DEV, PCIE_PORT4_FUNC):
261 mask |= PCIE_PORT4_DIS;
262 break;
263 case PCI_DEVFN(EHCI_DEV, EHCI_FUNC):
264 mask |= EHCI_DIS;
265 break;
266 case PCI_DEVFN(SIO_DMA2_DEV, SIO_DMA2_FUNC):
267 mask |= SIO_DMA2_DIS;
268 break;
269 case PCI_DEVFN(PWM1_DEV, PWM1_FUNC):
270 mask |= PWM1_DIS;
271 break;
272 case PCI_DEVFN(PWM2_DEV, PWM2_FUNC):
273 mask |= PWM2_DIS;
274 break;
275 case PCI_DEVFN(HSUART1_DEV, HSUART1_FUNC):
276 mask |= HSUART1_DIS;
277 break;
278 case PCI_DEVFN(HSUART2_DEV, HSUART2_FUNC):
279 mask |= HSUART2_DIS;
280 break;
281 case PCI_DEVFN(SPI_DEV, SPI_FUNC):
282 mask |= SPI_DIS;
283 break;
284 case PCI_DEVFN(SMBUS_DEV, SMBUS_FUNC):
285 mask2 |= SMBUS_DIS;
286 break;
287 }
288
289 if (mask != 0) {
290 write32(func_dis, read32(func_dis) | mask);
Angel Pons26b49cc2020-07-07 17:17:51 +0200291 /* Ensure posted write hits */
Aaron Durbind7bc23a2013-10-29 16:37:10 -0500292 read32(func_dis);
293 }
294
295 if (mask2 != 0) {
296 write32(func_dis2, read32(func_dis2) | mask2);
Angel Pons26b49cc2020-07-07 17:17:51 +0200297 /* Ensure posted write hits */
Aaron Durbind7bc23a2013-10-29 16:37:10 -0500298 read32(func_dis2);
299 }
300}
301
Elyes HAOUAS17a3ceb2018-05-22 10:42:28 +0200302static inline void set_d3hot_bits(struct device *dev, int offset)
Aaron Durbind7bc23a2013-10-29 16:37:10 -0500303{
304 uint32_t reg8;
305 printk(BIOS_DEBUG, "Power management CAP offset 0x%x.\n", offset);
306 reg8 = pci_read_config8(dev, offset + 4);
307 reg8 |= 0x3;
308 pci_write_config8(dev, offset + 4, reg8);
309}
310
Angel Pons26b49cc2020-07-07 17:17:51 +0200311/*
312 * Parts of the audio subsystem are powered by the HDA device. Thus, one cannot put HDA into
313 * D3Hot. Instead, perform this workaround to make some of the audio paths work for LPE audio.
314 */
Elyes HAOUAS17a3ceb2018-05-22 10:42:28 +0200315static void hda_work_around(struct device *dev)
Aaron Durbin46ab8cd2013-10-30 17:07:46 -0500316{
Angel Ponse80d17f2020-07-07 17:25:38 +0200317 void *gctl = (void *)(TEMP_BASE_ADDRESS + 0x8);
Aaron Durbin46ab8cd2013-10-30 17:07:46 -0500318
319 /* Need to set magic register 0x43 to 0xd7 in config space. */
320 pci_write_config8(dev, 0x43, 0xd7);
321
Angel Pons26b49cc2020-07-07 17:17:51 +0200322 /*
323 * Need to set bit 0 of GCTL to take the device out of reset.
324 * However, that requires setting up the 64-bit BAR.
325 */
Aaron Durbin46ab8cd2013-10-30 17:07:46 -0500326 pci_write_config32(dev, PCI_BASE_ADDRESS_0, TEMP_BASE_ADDRESS);
327 pci_write_config32(dev, PCI_BASE_ADDRESS_1, 0);
Elyes HAOUAS6468d87d2020-04-29 10:22:42 +0200328 pci_write_config16(dev, PCI_COMMAND, PCI_COMMAND_MEMORY);
Aaron Durbin46ab8cd2013-10-30 17:07:46 -0500329 write32(gctl, read32(gctl) | 0x1);
Elyes HAOUAS6468d87d2020-04-29 10:22:42 +0200330 pci_write_config16(dev, PCI_COMMAND, 0);
Aaron Durbin46ab8cd2013-10-30 17:07:46 -0500331 pci_write_config32(dev, PCI_BASE_ADDRESS_0, 0);
332}
333
Elyes HAOUAS17a3ceb2018-05-22 10:42:28 +0200334static int place_device_in_d3hot(struct device *dev)
Aaron Durbind7bc23a2013-10-29 16:37:10 -0500335{
Martin Roth57e89092019-10-23 21:45:23 -0600336 unsigned int offset;
Aaron Durbind7bc23a2013-10-29 16:37:10 -0500337
Angel Pons26b49cc2020-07-07 17:17:51 +0200338 /*
339 * Parts of the HDA block are used for LPE audio as well.
340 * Therefore assume the HDA will never be put into D3Hot.
341 */
Aaron Durbin46ab8cd2013-10-30 17:07:46 -0500342 if (dev->path.pci.devfn == PCI_DEVFN(HDA_DEV, HDA_FUNC)) {
343 hda_work_around(dev);
344 return 0;
345 }
346
Aaron Durbind7bc23a2013-10-29 16:37:10 -0500347 offset = pci_find_capability(dev, PCI_CAP_ID_PM);
348
349 if (offset != 0) {
350 set_d3hot_bits(dev, offset);
351 return 0;
352 }
353
Angel Pons26b49cc2020-07-07 17:17:51 +0200354 /*
355 * For some reason some of the devices don't have the capability pointer set correctly.
356 * Work around this by hard coding the offset.
357 */
Aaron Durbind7bc23a2013-10-29 16:37:10 -0500358 switch (dev->path.pci.devfn) {
Mate Kukri45b51e02020-07-03 14:44:49 +0200359 case PCI_DEVFN(MMC_DEV, MMC_FUNC):
360 offset = 0x80;
361 break;
Aaron Durbind7bc23a2013-10-29 16:37:10 -0500362 case PCI_DEVFN(SDIO_DEV, SDIO_FUNC):
363 offset = 0x80;
364 break;
365 case PCI_DEVFN(SD_DEV, SD_FUNC):
366 offset = 0x80;
367 break;
Mate Kukri45b51e02020-07-03 14:44:49 +0200368 case PCI_DEVFN(MMC45_DEV, MMC45_FUNC):
Aaron Durbind7bc23a2013-10-29 16:37:10 -0500369 offset = 0x80;
370 break;
371 case PCI_DEVFN(LPE_DEV, LPE_FUNC):
372 offset = 0x80;
373 break;
374 case PCI_DEVFN(SIO_DMA1_DEV, SIO_DMA1_FUNC):
375 offset = 0x80;
376 break;
377 case PCI_DEVFN(I2C1_DEV, I2C1_FUNC):
378 offset = 0x80;
379 break;
380 case PCI_DEVFN(I2C2_DEV, I2C2_FUNC):
381 offset = 0x80;
382 break;
383 case PCI_DEVFN(I2C3_DEV, I2C3_FUNC):
384 offset = 0x80;
385 break;
386 case PCI_DEVFN(I2C4_DEV, I2C4_FUNC):
387 offset = 0x80;
388 break;
389 case PCI_DEVFN(I2C5_DEV, I2C5_FUNC):
390 offset = 0x80;
391 break;
392 case PCI_DEVFN(I2C6_DEV, I2C6_FUNC):
393 offset = 0x80;
394 break;
395 case PCI_DEVFN(I2C7_DEV, I2C7_FUNC):
396 offset = 0x80;
397 break;
398 case PCI_DEVFN(SIO_DMA2_DEV, SIO_DMA2_FUNC):
399 offset = 0x80;
400 break;
401 case PCI_DEVFN(PWM1_DEV, PWM1_FUNC):
402 offset = 0x80;
403 break;
404 case PCI_DEVFN(PWM2_DEV, PWM2_FUNC):
405 offset = 0x80;
406 break;
407 case PCI_DEVFN(HSUART1_DEV, HSUART1_FUNC):
408 offset = 0x80;
409 break;
410 case PCI_DEVFN(HSUART2_DEV, HSUART2_FUNC):
411 offset = 0x80;
412 break;
413 case PCI_DEVFN(SPI_DEV, SPI_FUNC):
414 offset = 0x80;
415 break;
416 case PCI_DEVFN(SATA_DEV, SATA_FUNC):
417 offset = 0x70;
418 break;
419 case PCI_DEVFN(XHCI_DEV, XHCI_FUNC):
420 offset = 0x70;
421 break;
422 case PCI_DEVFN(EHCI_DEV, EHCI_FUNC):
423 offset = 0x70;
424 break;
425 case PCI_DEVFN(HDA_DEV, HDA_FUNC):
426 offset = 0x50;
427 break;
428 case PCI_DEVFN(SMBUS_DEV, SMBUS_FUNC):
429 offset = 0x50;
430 break;
431 case PCI_DEVFN(TXE_DEV, TXE_FUNC):
Aaron Durbin1eae3ee2013-10-30 17:08:59 -0500432 /* TXE cannot be placed in D3Hot. */
433 return 0;
Aaron Durbind7bc23a2013-10-29 16:37:10 -0500434 case PCI_DEVFN(PCIE_PORT1_DEV, PCIE_PORT1_FUNC):
435 offset = 0xa0;
436 break;
437 case PCI_DEVFN(PCIE_PORT2_DEV, PCIE_PORT2_FUNC):
438 offset = 0xa0;
439 break;
440 case PCI_DEVFN(PCIE_PORT3_DEV, PCIE_PORT3_FUNC):
441 offset = 0xa0;
442 break;
443 case PCI_DEVFN(PCIE_PORT4_DEV, PCIE_PORT4_FUNC):
444 offset = 0xa0;
445 break;
446 }
447
448 if (offset != 0) {
449 set_d3hot_bits(dev, offset);
450 return 0;
451 }
452
453 return -1;
454}
455
456/* Common PCI device function disable. */
Elyes HAOUAS17a3ceb2018-05-22 10:42:28 +0200457void southcluster_enable_dev(struct device *dev)
Aaron Durbind7bc23a2013-10-29 16:37:10 -0500458{
Elyes HAOUAS6468d87d2020-04-29 10:22:42 +0200459 uint16_t reg16;
Aaron Durbind7bc23a2013-10-29 16:37:10 -0500460
461 if (!dev->enabled) {
462 int slot = PCI_SLOT(dev->path.pci.devfn);
463 int func = PCI_FUNC(dev->path.pci.devfn);
464 printk(BIOS_DEBUG, "%s: Disabling device: %02x.%01x\n",
465 dev_path(dev), slot, func);
466
467 /* Ensure memory, io, and bus master are all disabled */
Elyes HAOUAS6468d87d2020-04-29 10:22:42 +0200468 reg16 = pci_read_config16(dev, PCI_COMMAND);
Angel Ponsc4d4b542020-07-07 19:00:07 +0200469 reg16 &= ~(PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY | PCI_COMMAND_IO);
Elyes HAOUAS6468d87d2020-04-29 10:22:42 +0200470 pci_write_config16(dev, PCI_COMMAND, reg16);
Aaron Durbind7bc23a2013-10-29 16:37:10 -0500471
472 /* Place device in D3Hot */
473 if (place_device_in_d3hot(dev) < 0) {
474 printk(BIOS_WARNING,
475 "Could not place %02x.%01x into D3Hot. "
476 "Keeping device visible.\n", slot, func);
477 return;
478 }
479 /* Disable this device if possible */
480 sc_disable_devfn(dev);
481 } else {
482 /* Enable SERR */
Elyes HAOUAS6468d87d2020-04-29 10:22:42 +0200483 pci_or_config16(dev, PCI_COMMAND, PCI_COMMAND_SERR);
Aaron Durbind7bc23a2013-10-29 16:37:10 -0500484 }
485}
486
Furquan Shaikh338fd9a2020-04-24 22:57:05 -0700487static void southcluster_inject_dsdt(const struct device *device)
Vladimir Serbinenko7fb149d2014-10-08 22:56:27 +0200488{
Kyösti Mälkki0c1dd9c2020-06-17 23:37:49 +0300489 struct global_nvs *gnvs;
Vladimir Serbinenko7fb149d2014-10-08 22:56:27 +0200490
491 gnvs = cbmem_find(CBMEM_ID_ACPI_GNVS);
492 if (!gnvs) {
493 gnvs = cbmem_add(CBMEM_ID_ACPI_GNVS, sizeof (*gnvs));
494 if (gnvs)
495 memset(gnvs, 0, sizeof(*gnvs));
496 }
497
498 if (gnvs) {
499 acpi_create_gnvs(gnvs);
Vladimir Serbinenko7fb149d2014-10-08 22:56:27 +0200500 /* And tell SMI about it */
Kyösti Mälkkic3c55212020-06-17 10:34:26 +0300501 apm_control(APM_CNT_GNVS_UPDATE);
Vladimir Serbinenko7fb149d2014-10-08 22:56:27 +0200502
503 /* Add it to DSDT. */
504 acpigen_write_scope("\\");
505 acpigen_write_name_dword("NVSA", (u32) gnvs);
506 acpigen_pop_len();
507 }
508}
509
510
Aaron Durbine18d68f2013-10-24 00:05:31 -0500511static struct device_operations device_ops = {
512 .read_resources = sc_read_resources,
513 .set_resources = pci_dev_set_resources,
Nico Huber68680dd2020-03-31 17:34:52 +0200514 .acpi_inject_dsdt = southcluster_inject_dsdt,
Angel Ponsc4d4b542020-07-07 19:00:07 +0200515 .write_acpi_tables = acpi_write_hpet,
Aaron Durbin3bde3d72013-11-04 21:45:52 -0600516 .init = sc_init,
Aaron Durbind7bc23a2013-10-29 16:37:10 -0500517 .enable = southcluster_enable_dev,
Nico Huber51b75ae2019-03-14 16:02:05 +0100518 .scan_bus = scan_static_bus,
Aaron Durbine18d68f2013-10-24 00:05:31 -0500519 .ops_pci = &soc_pci_ops,
520};
521
522static const struct pci_driver southcluster __pci_driver = {
523 .ops = &device_ops,
524 .vendor = PCI_VENDOR_ID_INTEL,
525 .device = LPC_DEVID,
526};
Aaron Durbin4177db52014-02-05 14:55:26 -0600527
Aaron Durbin64031672018-04-21 14:45:32 -0600528int __weak mainboard_get_spi_config(struct spi_config *cfg)
Aaron Durbin4177db52014-02-05 14:55:26 -0600529{
530 return -1;
531}
532
533static void finalize_chipset(void *unused)
534{
Angel Ponse80d17f2020-07-07 17:25:38 +0200535 void *bcr = (void *)(SPI_BASE_ADDRESS + BCR);
536 void *gcs = (void *)(RCBA_BASE_ADDRESS + GCS);
537 void *gen_pmcon2 = (void *)(PMC_BASE_ADDRESS + GEN_PMCON2);
538 void *etr = (void *)(PMC_BASE_ADDRESS + ETR);
Angel Ponsc4d4b542020-07-07 19:00:07 +0200539 uint8_t *spi = (uint8_t *)SPI_BASE_ADDRESS;
Aaron Durbin4177db52014-02-05 14:55:26 -0600540 struct spi_config cfg;
541
Angel Pons26b49cc2020-07-07 17:17:51 +0200542 /* Set the lock enable on the BIOS control register */
Aaron Durbin4177db52014-02-05 14:55:26 -0600543 write32(bcr, read32(bcr) | BCR_LE);
544
Angel Pons26b49cc2020-07-07 17:17:51 +0200545 /* Set BIOS lock down bit controlling boot block size and swapping */
Aaron Durbin4177db52014-02-05 14:55:26 -0600546 write32(gcs, read32(gcs) | BILD);
547
Angel Pons26b49cc2020-07-07 17:17:51 +0200548 /* Lock sleep stretching policy and set SMI lock */
Aaron Durbin4177db52014-02-05 14:55:26 -0600549 write32(gen_pmcon2, read32(gen_pmcon2) | SLPSX_STR_POL_LOCK | SMI_LOCK);
550
Angel Pons26b49cc2020-07-07 17:17:51 +0200551 /* Set the CF9 lock */
Aaron Durbin4177db52014-02-05 14:55:26 -0600552 write32(etr, read32(etr) | CF9LOCK);
553
554 if (mainboard_get_spi_config(&cfg) < 0) {
555 printk(BIOS_DEBUG, "No SPI lockdown configuration.\n");
556 } else {
557 write16(spi + PREOP, cfg.preop);
558 write16(spi + OPTYPE, cfg.optype);
559 write32(spi + OPMENU0, cfg.opmenu[0]);
560 write32(spi + OPMENU1, cfg.opmenu[1]);
561 write16(spi + HSFSTS, read16(spi + HSFSTS) | FLOCKDN);
562 write32(spi + UVSCC, cfg.uvscc);
563 write32(spi + LVSCC, cfg.lvscc | VCL);
564 }
Aaron Durbin4177db52014-02-05 14:55:26 -0600565}
566
Aaron Durbin9ef9d852015-03-16 17:30:09 -0500567BOOT_STATE_INIT_ENTRY(BS_OS_RESUME, BS_ON_ENTRY, finalize_chipset, NULL);
568BOOT_STATE_INIT_ENTRY(BS_PAYLOAD_LOAD, BS_ON_EXIT, finalize_chipset, NULL);