blob: 206894d8a4094c2df30adb1c7f47cba94d428182 [file] [log] [blame]
Angel Pons182dbde2020-04-02 23:49:05 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Stefan Reinauer8e073822012-04-04 00:07:22 +02002
3#include <console/console.h>
4#include <device/device.h>
5#include <device/pci.h>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +02006#include <device/pci_ops.h>
Stefan Reinauer8e073822012-04-04 00:07:22 +02007#include <device/pciexp.h>
8#include <device/pci_ids.h>
Vladimir Serbinenko36fa5b82014-10-28 23:43:20 +01009#include <southbridge/intel/common/pciehp.h>
Patrick Rudolph604f6982017-06-07 09:46:52 +020010#include <assert.h>
Kyösti Mälkki12b121c2019-08-18 16:33:39 +030011
12#include "chip.h"
Stefan Reinauer8e073822012-04-04 00:07:22 +020013#include "pch.h"
14
Angel Pons7333ea92020-06-08 15:24:32 +020015static const char *pch_pcie_acpi_name(const struct device *dev)
16{
17 ASSERT(dev);
18
19 if (PCI_SLOT(dev->path.pci.devfn) == 0x1c) {
20 static const char *names[] = { "RP01",
21 "RP02",
22 "RP03",
23 "RP04",
24 "RP05",
25 "RP06",
26 "RP07",
27 "RP08"};
28
29 return names[PCI_FUNC(dev->path.pci.devfn)];
30 }
31
32 return NULL;
33}
34
Patrick Rudolphbd7a7fd2023-10-02 07:18:18 +020035static bool pci_is_hotplugable(struct device *dev)
36{
37 struct southbridge_intel_bd82x6x_config *config = dev->chip_info;
38
39 return config && config->pcie_hotplug_map[PCI_FUNC(dev->path.pci.devfn)];
40}
41
Stefan Reinauer8e073822012-04-04 00:07:22 +020042static void pch_pcie_pm_early(struct device *dev)
43{
44 u16 link_width_p0, link_width_p4;
45 u8 slot_power_limit = 10; /* 10W for x1 */
46 u32 reg32;
47 u8 reg8;
48
Duncan Laurie4aca5d72012-04-27 10:58:22 -070049 reg32 = RCBA32(RPC);
50
51 /* Port 0-3 link aggregation from PCIEPCS1[1:0] soft strap */
52 switch (reg32 & 3) {
53 case 3:
54 link_width_p0 = 4;
55 break;
56 case 1:
57 case 2:
58 link_width_p0 = 2;
59 break;
60 case 0:
61 default:
62 link_width_p0 = 1;
63 }
64
65 /* Port 4-7 link aggregation from PCIEPCS2[1:0] soft strap */
66 switch ((reg32 >> 2) & 3) {
67 case 3:
68 link_width_p4 = 4;
69 break;
70 case 1:
71 case 2:
72 link_width_p4 = 2;
73 break;
74 case 0:
75 default:
76 link_width_p4 = 1;
77 }
Stefan Reinauer8e073822012-04-04 00:07:22 +020078
79 /* Enable dynamic clock gating where needed */
80 reg8 = pci_read_config8(dev, 0xe1);
81 switch (PCI_FUNC(dev->path.pci.devfn)) {
82 case 0: /* Port 0 */
83 if (link_width_p0 == 4)
84 slot_power_limit = 40; /* 40W for x4 */
85 else if (link_width_p0 == 2)
86 slot_power_limit = 20; /* 20W for x2 */
Duncan Laurie4aca5d72012-04-27 10:58:22 -070087 reg8 |= 0x3f;
88 break;
Stefan Reinauer8e073822012-04-04 00:07:22 +020089 case 4: /* Port 4 */
90 if (link_width_p4 == 4)
91 slot_power_limit = 40; /* 40W for x4 */
92 else if (link_width_p4 == 2)
93 slot_power_limit = 20; /* 20W for x2 */
94 reg8 |= 0x3f;
95 break;
96 case 1: /* Port 1 only if Port 0 is x1 */
97 if (link_width_p0 == 1)
98 reg8 |= 0x3;
99 break;
100 case 2: /* Port 2 only if Port 0 is x1 or x2 */
101 case 3: /* Port 3 only if Port 0 is x1 or x2 */
102 if (link_width_p0 <= 2)
103 reg8 |= 0x3;
104 break;
105 case 5: /* Port 5 only if Port 4 is x1 */
106 if (link_width_p4 == 1)
107 reg8 |= 0x3;
108 break;
109 case 6: /* Port 7 only if Port 4 is x1 or x2 */
110 case 7: /* Port 7 only if Port 4 is x1 or x2 */
111 if (link_width_p4 <= 2)
112 reg8 |= 0x3;
113 break;
114 }
115 pci_write_config8(dev, 0xe1, reg8);
116
117 /* Set 0xE8[0] = 1 */
Angel Ponsc803f652020-06-07 22:09:01 +0200118 pci_or_config32(dev, 0xe8, 1);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200119
120 /* Adjust Common Clock exit latency */
121 reg32 = pci_read_config32(dev, 0xd8);
122 reg32 &= ~(1 << 17);
123 reg32 |= (1 << 16) | (1 << 15);
124 reg32 &= ~(1 << 31); /* Disable PME# SCI for native PME handling */
125 pci_write_config32(dev, 0xd8, reg32);
126
127 /* Adjust ASPM L1 exit latency */
128 reg32 = pci_read_config32(dev, 0x4c);
129 reg32 &= ~((1 << 17) | (1 << 16) | (1 << 15));
Patrick Rudolph4f8b1082019-07-14 11:54:58 +0200130 if (RCBA32(CIR9) & (1 << 16)) {
Stefan Reinauer8e073822012-04-04 00:07:22 +0200131 /* If RCBA+2320[15]=1 set ASPM L1 to 8-16us */
132 reg32 |= (1 << 17);
133 } else {
134 /* Else set ASPM L1 to 2-4us */
135 reg32 |= (1 << 16);
136 }
137 pci_write_config32(dev, 0x4c, reg32);
138
139 /* Set slot power limit as configured above */
140 reg32 = pci_read_config32(dev, 0x54);
141 reg32 &= ~((1 << 15) | (1 << 16)); /* 16:15 = Slot power scale */
142 reg32 &= ~(0xff << 7); /* 14:7 = Slot power limit */
143 reg32 |= (slot_power_limit << 7);
144 pci_write_config32(dev, 0x54, reg32);
145}
146
147static void pch_pcie_pm_late(struct device *dev)
148{
Marc Jones4adc8cd2012-10-31 16:24:37 -0600149 struct southbridge_intel_bd82x6x_config *config = dev->chip_info;
150 enum aspm_type apmc = 0;
Stefan Reinauer8e073822012-04-04 00:07:22 +0200151
152 /* Set 0x314 = 0x743a361b */
Kyösti Mälkkifd98c652013-07-26 08:50:53 +0300153 pci_write_config32(dev, 0x314, 0x743a361b);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200154
155 /* Set 0x318[31:16] = 0x1414 */
Angel Ponsc803f652020-06-07 22:09:01 +0200156 pci_update_config32(dev, 0x318, 0x0000ffff, 0x14140000);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200157
158 /* Set 0x324[5] = 1 */
Angel Ponsc803f652020-06-07 22:09:01 +0200159 pci_or_config32(dev, 0x324, 1 << 5);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200160
161 /* Set 0x330[7:0] = 0x40 */
Angel Ponsc803f652020-06-07 22:09:01 +0200162 pci_update_config32(dev, 0x330, ~0xff, 0x40);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200163
164 /* Set 0x33C[24:0] = 0x854c74 */
Angel Ponsc803f652020-06-07 22:09:01 +0200165 pci_update_config32(dev, 0x33c, 0xff000000, 0x00854c74);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200166
167 /* No IO-APIC, Disable EOI forwarding */
Angel Ponsc803f652020-06-07 22:09:01 +0200168 pci_or_config32(dev, 0xd4, 1 << 1);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200169
Marc Jones4adc8cd2012-10-31 16:24:37 -0600170 /* Check for a rootport ASPM override */
Angel Pons02396842021-06-05 12:34:23 +0200171 apmc = config->pcie_aspm[PCI_FUNC(dev->path.pci.devfn)];
Marc Jones4adc8cd2012-10-31 16:24:37 -0600172
173 /* Setup the override or get the real ASPM setting */
174 if (apmc) {
Angel Ponsc803f652020-06-07 22:09:01 +0200175 pci_or_config32(dev, 0xd4, (apmc << 2) | (1 << 4));
176
Marc Jones4adc8cd2012-10-31 16:24:37 -0600177 } else {
178 apmc = pci_read_config32(dev, 0x50) & 3;
179 }
Stefan Reinauer8e073822012-04-04 00:07:22 +0200180
181 /* If both L0s and L1 enabled then set root port 0xE8[1]=1 */
Angel Ponsc803f652020-06-07 22:09:01 +0200182 if (apmc == PCIE_ASPM_BOTH)
183 pci_or_config32(dev, 0xe8, 1 << 1);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200184}
185
186static void pci_init(struct device *dev)
187{
188 u16 reg16;
Stefan Reinauer8e073822012-04-04 00:07:22 +0200189
190 printk(BIOS_DEBUG, "Initializing PCH PCIe bridge.\n");
191
192 /* Enable Bus Master */
Elyes HAOUAS729c0692020-04-28 19:50:44 +0200193 pci_or_config16(dev, PCI_COMMAND, PCI_COMMAND_MASTER);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200194
195 /* Set Cache Line Size to 0x10 */
196 // This has no effect but the OS might expect it
197 pci_write_config8(dev, 0x0c, 0x10);
198
Angel Ponsb82b4312020-07-23 23:32:46 +0200199 pci_and_config16(dev, PCI_BRIDGE_CONTROL, ~PCI_BRIDGE_CTL_PARITY);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200200
Angel Ponsc803f652020-06-07 22:09:01 +0200201 /* Clear errors in status registers. FIXME: Do something? */
Stefan Reinauer8e073822012-04-04 00:07:22 +0200202 reg16 = pci_read_config16(dev, 0x06);
203 //reg16 |= 0xf900;
204 pci_write_config16(dev, 0x06, reg16);
205
206 reg16 = pci_read_config16(dev, 0x1e);
207 //reg16 |= 0xf900;
208 pci_write_config16(dev, 0x1e, reg16);
Vladimir Serbinenko36fa5b82014-10-28 23:43:20 +0100209
210 /* Enable expresscard hotplug events. */
Patrick Rudolphbd7a7fd2023-10-02 07:18:18 +0200211 if (pci_is_hotplugable(dev)) {
Angel Ponsc803f652020-06-07 22:09:01 +0200212 pci_or_config32(dev, 0xd8, 1 << 30);
Vladimir Serbinenko36fa5b82014-10-28 23:43:20 +0100213 pci_write_config16(dev, 0x42, 0x142);
214 }
Stefan Reinauer8e073822012-04-04 00:07:22 +0200215}
216
Elyes HAOUAS4aec3402018-05-25 08:29:27 +0200217static void pch_pcie_enable(struct device *dev)
Stefan Reinauer8e073822012-04-04 00:07:22 +0200218{
219 /* Power Management init before enumeration */
220 pch_pcie_pm_early(dev);
221}
222
Elyes HAOUAS4aec3402018-05-25 08:29:27 +0200223static void pch_pciexp_scan_bridge(struct device *dev)
Marc Jones4adc8cd2012-10-31 16:24:37 -0600224{
Patrick Rudolphbd7a7fd2023-10-02 07:18:18 +0200225 if (CONFIG(PCIEXP_HOTPLUG) && pci_is_hotplugable(dev)) {
Arthur Heymansa560c712021-02-24 22:27:44 +0100226 pciexp_hotplug_scan_bridge(dev);
227 } else {
228 /* Normal PCIe Scan */
229 pciexp_scan_bridge(dev);
Vladimir Serbinenko36fa5b82014-10-28 23:43:20 +0100230 }
231
Marc Jones4adc8cd2012-10-31 16:24:37 -0600232 /* Late Power Management init after bridge device enumeration */
233 pch_pcie_pm_late(dev);
Marc Jones4adc8cd2012-10-31 16:24:37 -0600234}
235
Stefan Reinauer8e073822012-04-04 00:07:22 +0200236static struct device_operations device_ops = {
237 .read_resources = pci_bus_read_resources,
238 .set_resources = pci_dev_set_resources,
239 .enable_resources = pci_bus_enable_resources,
240 .init = pci_init,
241 .enable = pch_pcie_enable,
Marc Jones4adc8cd2012-10-31 16:24:37 -0600242 .scan_bus = pch_pciexp_scan_bridge,
Patrick Rudolph604f6982017-06-07 09:46:52 +0200243 .acpi_name = pch_pcie_acpi_name,
Angel Pons1fc0edd2020-05-31 00:03:28 +0200244 .ops_pci = &pci_dev_ops_pci,
Stefan Reinauer8e073822012-04-04 00:07:22 +0200245};
246
Stefan Reinauer9a380ab2012-06-22 13:16:11 -0700247static const unsigned short pci_device_ids[] = { 0x1c10, 0x1c12, 0x1c14, 0x1c16,
248 0x1c18, 0x1c1a, 0x1c1c, 0x1c1e,
249 0x1e10, 0x1e12, 0x1e14, 0x1e16,
250 0x1e18, 0x1e1a, 0x1e1c, 0x1e1e,
251 0 };
Stefan Reinauer8e073822012-04-04 00:07:22 +0200252
Stefan Reinauer9a380ab2012-06-22 13:16:11 -0700253static const struct pci_driver pch_pcie __pci_driver = {
254 .ops = &device_ops,
Felix Singer43b7f412022-03-07 04:34:52 +0100255 .vendor = PCI_VID_INTEL,
Stefan Reinauer9a380ab2012-06-22 13:16:11 -0700256 .devices = pci_device_ids,
Stefan Reinauer8e073822012-04-04 00:07:22 +0200257};