1 /* SPDX-License-Identifier: GPL-2.0-only */
3 #include <device/mmio.h>
4 #include <device/pci_ops.h>
6 #include <arch/ioapic.h>
9 #include <console/console.h>
10 #include <device/device.h>
11 #include <device/pci.h>
12 #include <device/pci_ids.h>
13 #include <intelblocks/lpc_lib.h>
14 #include <pc80/isa-dma.h>
15 #include <pc80/i8254.h>
16 #include <pc80/i8259.h>
18 #include <soc/iomap.h>
21 #include <soc/pci_devs.h>
23 #include <soc/ramstage.h>
25 #include <spi-generic.h>
27 #include <southbridge/intel/common/spi.h>
29 static void sc_set_serial_irqs_mode(struct device
*dev
, enum serirq_mode mode
)
31 u8
*ilb_base
= (u8
*)(pci_read_config32(dev
, IBASE
) & ~0xf);
34 case SERIRQ_CONTINUOUS
:
38 write32(ilb_base
+ ILB_OIC
, read32(ilb_base
+ ILB_OIC
) & ~SIRQEN
);
43 write8(ilb_base
+ SCNT
, read8(ilb_base
+ SCNT
) & ~SCNT_MODE
);
48 static void sc_add_mmio_resources(struct device
*dev
)
50 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 */
61 mmio_range(dev
, 0xfec, IO_APIC_ADDR
, 0x00001000); /* IOAPIC */
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
68 static void sc_enable_serial_irqs(struct device
*dev
)
70 u8
*ilb_base
= (u8
*)(pci_read_config32(dev
, IBASE
) & ~0xF);
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
);
78 * 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.
85 * 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.
89 static void write_pci_config_irqs(void)
91 struct device
*irq_dev
;
92 struct device
*targ_dev
;
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;
99 uint8_t device_num
= 0;
100 const struct soc_irq_route
*ir
= &global_soc_irq_route
;
103 printk(BIOS_WARNING
, "Can't write PCI IRQ assignments "
104 "because 'global_braswell_irq_route' structure does not exist\n");
109 * 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
112 printk(BIOS_DEBUG
, "PCI_CFG IRQ: Write PIRQ assignments\n");
113 for (irq_dev
= all_devices
; irq_dev
; irq_dev
= irq_dev
->next
) {
114 if ((irq_dev
->path
.type
!= DEVICE_PATH_PCI
) ||
118 current_bdf
= irq_dev
->path
.pci
.devfn
|
119 irq_dev
->upstream
->secondary
<< 8;
122 * Step 1: Get the INT_PIN and device structure to look for
123 * in the pirq_data table defined in the mainboard directory.
126 new_int_pin
= get_pci_irq_pins(irq_dev
, &targ_dev
);
127 if (targ_dev
== NULL
|| new_int_pin
< 1)
130 /* Get the original INT_PIN for record keeping */
131 original_int_pin
= pci_read_config8(irq_dev
, PCI_INTERRUPT_PIN
);
133 parent_bdf
= targ_dev
->path
.pci
.devfn
134 | targ_dev
->upstream
->secondary
<< 8;
135 device_num
= PCI_SLOT(parent_bdf
);
137 if (ir
->pcidev
[device_num
] == 0) {
138 printk(BIOS_WARNING
, "PCI Device %d does not have an IRQ entry, "
139 "skipping it\n", device_num
);
143 /* Find the PIRQ that is attached to the INT_PIN */
144 pirq
= (ir
->pcidev
[device_num
] >> ((new_int_pin
- 1) * 4))
147 /* Get the INT_LINE this device/function will use */
148 int_line
= ir
->pic
[pirq
];
150 if (int_line
!= PIRQ_PIC_IRQDISABLE
) {
151 /* Set this IRQ to level triggered */
152 i8259_configure_irq_trigger(int_line
, IRQ_LEVEL_TRIGGERED
);
154 /* Set the Interrupt Line register */
155 pci_write_config8(irq_dev
, PCI_INTERRUPT_LINE
, int_line
);
157 /* Set the Interrupt line register as 'unknown' or 'unused' */
158 pci_write_config8(irq_dev
, PCI_INTERRUPT_LINE
, PIRQ_PIC_UNKNOWN_UNUSED
);
161 printk(BIOS_SPEW
, "\tINT_PIN\t\t: %d (%s)\n", original_int_pin
,
162 pin_to_str(original_int_pin
));
164 if (parent_bdf
!= current_bdf
)
165 printk(BIOS_SPEW
, "\tSwizzled to\t: %d (%s)\n", new_int_pin
,
166 pin_to_str(new_int_pin
));
168 printk(BIOS_SPEW
, "\tPIRQ\t\t: %c\n\tINT_LINE\t: 0x%X (IRQ %d)\n",
169 'A' + pirq
, int_line
, int_line
);
171 printk(BIOS_DEBUG
, "PCI_CFG IRQ: Finished writing PIRQ assignments\n");
174 static inline int io_range_in_default(int base
, int size
)
176 /* Does it start above the range? */
177 if (base
>= LPC_DEFAULT_IO_RANGE_UPPER
)
180 /* Is it entirely contained? */
181 if (base
>= LPC_DEFAULT_IO_RANGE_LOWER
&& (base
+ size
) < LPC_DEFAULT_IO_RANGE_UPPER
)
184 /* This will return not in range for partial overlaps */
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.
192 static void sc_add_io_resource(struct device
*dev
, int base
, int size
, int index
)
194 struct resource
*res
;
196 if (io_range_in_default(base
, size
))
199 res
= new_resource(dev
, index
);
202 res
->flags
= IORESOURCE_IO
| IORESOURCE_ASSIGNED
| IORESOURCE_FIXED
;
205 static void sc_add_io_resources(struct device
*dev
)
207 struct resource
*res
;
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
;
216 sc_add_io_resource(dev
, GPIO_BASE_ADDRESS
, GPIO_BASE_SIZE
, GBASE
);
219 sc_add_io_resource(dev
, ACPI_BASE_ADDRESS
, ACPI_BASE_SIZE
, ABASE
);
222 static void sc_read_resources(struct device
*dev
)
224 /* Get the normal PCI resources of this device. */
225 pci_dev_read_resources(dev
);
227 /* Add non-standard MMIO resources. */
228 sc_add_mmio_resources(dev
);
230 /* Add IO resources. */
231 sc_add_io_resources(dev
);
234 static void sc_init(struct device
*dev
)
237 const unsigned long ilb_base
= ILB_BASE_ADDRESS
;
238 const unsigned long pr_base
= ILB_BASE_ADDRESS
+ 0x08;
239 const unsigned long ir_base
= ILB_BASE_ADDRESS
+ 0x20;
241 void *gen_pmcon1
= (void *)(PMC_BASE_ADDRESS
+ GEN_PMCON1
);
242 const struct soc_irq_route
*ir
= &global_soc_irq_route
;
243 struct soc_intel_braswell_config
*config
= config_of(dev
);
245 /* Set the value for PCI command register. */
246 pci_write_config16(dev
, PCI_COMMAND
,
247 PCI_COMMAND_IO
| PCI_COMMAND_MEMORY
| PCI_COMMAND_MASTER
| PCI_COMMAND_SPECIAL
);
249 /* Use IRQ9 for SCI Interrupt */
250 write32((void *)(ilb_base
+ ACTL
), 0);
254 sc_enable_serial_irqs(dev
);
256 /* Set up the PIRQ PIC routing based on static config. */
257 for (i
= 0; i
< NUM_PIRQS
; i
++)
258 write8((void *)(pr_base
+ i
*sizeof(ir
->pic
[i
])), ir
->pic
[i
]);
260 /* Set up the per device PIRQ routing base on static config. */
261 for (i
= 0; i
< NUM_IR_DEVS
; i
++)
262 write16((void *)(ir_base
+ i
*sizeof(ir
->pcidev
[i
])), ir
->pcidev
[i
]);
264 /* Interrupt 9 should be level triggered (SCI) */
265 i8259_configure_irq_trigger(9, 1);
267 for (i
= 0; i
< NUM_PIRQS
; i
++) {
269 i8259_configure_irq_trigger(ir
->pic
[i
], 1);
272 if (config
->disable_slp_x_stretch_sus_fail
) {
273 printk(BIOS_DEBUG
, "Disabling slp_x stretching.\n");
274 write32(gen_pmcon1
, read32(gen_pmcon1
) | DIS_SLP_X_STRCH_SUS_UP
);
277 write32(gen_pmcon1
, read32(gen_pmcon1
) & ~DIS_SLP_X_STRCH_SUS_UP
);
280 /* Write IRQ assignments to PCI config space */
281 write_pci_config_irqs();
283 /* Initialize i8259 pic */
286 /* Initialize i8254 timers */
289 sc_set_serial_irqs_mode(dev
, config
->serirq_mode
);
293 * Common code for the south cluster devices.
296 /* Set bit in function disable register to hide this device. */
297 static void sc_disable_devfn(struct device
*dev
)
299 void *func_dis
= (void *)(PMC_BASE_ADDRESS
+ FUNC_DIS
);
300 void *func_dis2
= (void *)(PMC_BASE_ADDRESS
+ FUNC_DIS2
);
304 #define SET_DIS_MASK(name_) \
305 case PCI_DEVFN(name_ ## _DEV, name_ ## _FUNC): \
306 mask |= name_ ## _DIS
308 #define SET_DIS_MASK2(name_) \
309 case PCI_DEVFN(name_ ## _DEV, name_ ## _FUNC): \
310 mask2 |= name_ ## _DIS
312 switch (dev
->path
.pci
.devfn
) {
320 /* Disable super speed PHY when XHCI is not available. */
321 mask2
|= USH_SS_PHY_DIS
;
327 SET_DIS_MASK(SIO_DMA1
);
347 SET_DIS_MASK(PCIE_PORT1
);
349 SET_DIS_MASK(PCIE_PORT2
);
351 SET_DIS_MASK(PCIE_PORT3
);
353 SET_DIS_MASK(PCIE_PORT4
);
355 SET_DIS_MASK(SIO_DMA2
);
361 SET_DIS_MASK(HSUART1
);
363 SET_DIS_MASK(HSUART2
);
367 SET_DIS_MASK2(SMBUS
);
372 write32(func_dis
, read32(func_dis
) | mask
);
373 /* Ensure posted write hits */
378 write32(func_dis2
, read32(func_dis2
) | mask2
);
379 /* Ensure posted write hits */
384 static inline void set_d3hot_bits(struct device
*dev
, int offset
)
387 printk(BIOS_DEBUG
, "Power management CAP offset 0x%x.\n", offset
);
388 reg8
= pci_read_config8(dev
, offset
+ 4);
390 pci_write_config8(dev
, offset
+ 4, reg8
);
394 * 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.
397 static void hda_work_around(struct device
*dev
)
399 void *gctl
= (void *)(TEMP_BASE_ADDRESS
+ 0x8);
401 /* Need to set magic register 0x43 to 0xd7 in config space. */
402 pci_write_config8(dev
, 0x43, 0xd7);
405 * Need to set bit 0 of GCTL to take the device out of reset.
406 * However, that requires setting up the 64-bit BAR.
408 pci_write_config32(dev
, PCI_BASE_ADDRESS_0
, TEMP_BASE_ADDRESS
);
409 pci_write_config32(dev
, PCI_BASE_ADDRESS_1
, 0);
410 pci_write_config16(dev
, PCI_COMMAND
, PCI_COMMAND_MEMORY
);
411 write32(gctl
, read32(gctl
) | 0x1);
412 pci_write_config16(dev
, PCI_COMMAND
, 0);
413 pci_write_config32(dev
, PCI_BASE_ADDRESS_0
, 0);
416 static int place_device_in_d3hot(struct device
*dev
)
421 * Parts of the HDA block are used for LPE audio as well.
422 * Therefore assume the HDA will never be put into D3Hot.
424 if (dev
->path
.pci
.devfn
== PCI_DEVFN(HDA_DEV
, HDA_FUNC
)) {
425 hda_work_around(dev
);
429 offset
= pci_find_capability(dev
, PCI_CAP_ID_PM
);
432 set_d3hot_bits(dev
, offset
);
437 * For some reason some of the devices don't have the capability pointer set correctly.
438 * Work around this by hard coding the offset.
440 #define DEV_CASE(name_) \
441 case PCI_DEVFN(name_ ## _DEV, name_ ## _FUNC)
443 switch (dev
->path
.pci
.devfn
) {
473 /* TXE cannot be placed in D3Hot. */
475 DEV_CASE(PCIE_PORT1
) :
476 DEV_CASE(PCIE_PORT2
) :
477 DEV_CASE(PCIE_PORT3
) :
478 DEV_CASE(PCIE_PORT4
) :
484 set_d3hot_bits(dev
, offset
);
491 /* Common PCI device function disable. */
492 void southcluster_enable_dev(struct device
*dev
)
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
);
502 /* Ensure memory, io, and bus master are all disabled */
503 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
);
507 /* Place device in D3Hot */
508 if (place_device_in_d3hot(dev
) < 0) {
510 "Could not place %02x.%01x into D3Hot. "
511 "Keeping device visible.\n", slot
, func
);
514 /* Disable this device if possible */
515 sc_disable_devfn(dev
);
518 pci_or_config16(dev
, PCI_COMMAND
, PCI_COMMAND_SERR
);
522 static struct device_operations device_ops
= {
523 .read_resources
= sc_read_resources
,
524 .set_resources
= pci_dev_set_resources
,
525 .write_acpi_tables
= southcluster_write_acpi_tables
,
527 .enable
= southcluster_enable_dev
,
528 .scan_bus
= scan_static_bus
,
529 .ops_pci
= &soc_pci_ops
,
532 static const struct pci_driver southcluster __pci_driver
= {
534 .vendor
= PCI_VID_INTEL
,
538 static void finalize_chipset(void *unused
)
540 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
;
546 struct vscc_config cfg
;
548 /* Set the lock enable on the BIOS control register */
549 write32(bcr
, read32(bcr
) | BCR_LE
);
551 /* Set BIOS lock down bit controlling boot block size and swapping */
552 write32(gcs
, read32(gcs
) | BILD
);
554 /* Lock sleep stretching policy and set SMI lock */
555 write32(gen_pmcon2
, read32(gen_pmcon2
) | SLPSX_STR_POL_LOCK
| SMI_LOCK
);
557 /* Set the CF9 lock */
558 write32(etr
, read32(etr
) | CF9LOCK
);
561 write16(spi
+ HSFSTS
, read16(spi
+ HSFSTS
) | FLOCKDN
);
563 if (mainboard_get_spi_vscc_config(&cfg
) < 0) {
564 printk(BIOS_DEBUG
, "No SPI VSCC configuration.\n");
566 write32(spi
+ UVSCC
, cfg
.uvscc
);
567 write32(spi
+ LVSCC
, cfg
.lvscc
| VCL
);
571 BOOT_STATE_INIT_ENTRY(BS_POST_DEVICE
, BS_ON_EXIT
, finalize_chipset
, NULL
);