blob: 7f5e1face6f789ac8e066226f074e2f8920f6867 [file] [log] [blame]
Angel Pons182dbde2020-04-02 23:49:05 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Aaron Durbin76c37002012-10-30 09:03:43 -05002
Tristan Corrick05b75242018-12-06 22:47:42 +13003#include <assert.h>
4#include <commonlib/helpers.h>
Aaron Durbin76c37002012-10-30 09:03:43 -05005#include <console/console.h>
6#include <device/device.h>
7#include <device/pci.h>
8#include <device/pciexp.h>
9#include <device/pci_ids.h>
Patrick Rudolphe56189c2018-04-18 10:11:59 +020010#include <device/pci_ops.h>
Angel Pons2178b722020-05-31 00:55:35 +020011#include "iobp.h"
Aaron Durbin76c37002012-10-30 09:03:43 -050012#include "pch.h"
Patrick Rudolph273a8dc2016-02-06 18:07:59 +010013#include <southbridge/intel/common/gpio.h>
Tristan Corrick05b75242018-12-06 22:47:42 +130014#include <stdint.h>
Kyösti Mälkki12b121c2019-08-18 16:33:39 +030015#include "chip.h"
Aaron Durbin76c37002012-10-30 09:03:43 -050016
Aaron Durbinc0254e62013-06-20 01:20:30 -050017#define MAX_NUM_ROOT_PORTS 8
Aaron Durbinc0254e62013-06-20 01:20:30 -050018
19struct root_port_config {
20 /* RPFN is a write-once register so keep a copy until it is written */
21 u32 orig_rpfn;
22 u32 new_rpfn;
23 u32 pin_ownership;
24 u32 strpfusecfg1;
25 u32 strpfusecfg2;
26 u32 strpfusecfg3;
Stefan Reinauerab365af2013-12-03 12:13:26 -080027 u32 b0d28f0_32c;
28 u32 b0d28f4_32c;
29 u32 b0d28f5_32c;
Angel Ponsaf4bd562021-12-28 13:05:56 +010030 bool coalesce;
Aaron Durbinc0254e62013-06-20 01:20:30 -050031 int gbe_port;
32 int num_ports;
Elyes HAOUAS7a5f7712018-06-08 17:20:38 +020033 struct device *ports[MAX_NUM_ROOT_PORTS];
Aaron Durbinc0254e62013-06-20 01:20:30 -050034};
35
36static struct root_port_config rpc;
37
38static inline int max_root_ports(void)
Aaron Durbin60f82082013-06-19 13:28:04 -050039{
Felix Singer43b7f412022-03-07 04:34:52 +010040 if (pch_is_lp() || pch_silicon_id() == PCI_DID_INTEL_LPT_H81)
Tristan Corrickd3f01b22018-12-06 22:46:58 +130041 return 6;
42
43 return 8;
Aaron Durbin60f82082013-06-19 13:28:04 -050044}
45
Elyes HAOUAS7a5f7712018-06-08 17:20:38 +020046static inline int root_port_is_first(struct device *dev)
Aaron Durbin60f82082013-06-19 13:28:04 -050047{
Aaron Durbinc0254e62013-06-20 01:20:30 -050048 return PCI_FUNC(dev->path.pci.devfn) == 0;
49}
Aaron Durbin60f82082013-06-19 13:28:04 -050050
Elyes HAOUAS7a5f7712018-06-08 17:20:38 +020051static inline int root_port_is_last(struct device *dev)
Aaron Durbinc0254e62013-06-20 01:20:30 -050052{
53 return PCI_FUNC(dev->path.pci.devfn) == (rpc.num_ports - 1);
54}
Aaron Durbin60f82082013-06-19 13:28:04 -050055
Aaron Durbinc0254e62013-06-20 01:20:30 -050056/* Root ports are numbered 1..N in the documentation. */
Elyes HAOUAS7a5f7712018-06-08 17:20:38 +020057static inline int root_port_number(struct device *dev)
Aaron Durbinc0254e62013-06-20 01:20:30 -050058{
59 return PCI_FUNC(dev->path.pci.devfn) + 1;
60}
Aaron Durbin60f82082013-06-19 13:28:04 -050061
Tristan Corrick05b75242018-12-06 22:47:42 +130062static bool is_rp_enabled(int rp)
63{
64 ASSERT(rp > 0 && rp <= ARRAY_SIZE(rpc.ports));
65
66 if (rpc.ports[rp - 1] == NULL)
67 return false;
68
69 return rpc.ports[rp - 1]->enabled;
70}
71
Aaron Durbinc0254e62013-06-20 01:20:30 -050072static void root_port_config_update_gbe_port(void)
73{
74 /* Is the Gbe Port enabled? */
75 if (!((rpc.strpfusecfg1 >> 19) & 1))
76 return;
77
78 if (pch_is_lp()) {
79 switch ((rpc.strpfusecfg1 >> 16) & 0x7) {
80 case 0:
81 rpc.gbe_port = 3;
82 break;
83 case 1:
84 rpc.gbe_port = 4;
85 break;
86 case 2:
87 case 3:
88 case 4:
89 case 5:
90 /* Lanes 0-4 of Root Port 5. */
91 rpc.gbe_port = 5;
92 break;
93 default:
94 printk(BIOS_DEBUG, "Invalid GbE Port Selection.\n");
95 }
96 } else {
97 /* Non-LP has 1:1 mapping with root ports. */
98 rpc.gbe_port = ((rpc.strpfusecfg1 >> 16) & 0x7) + 1;
99 }
100}
101
Angel Pons12404e02020-08-04 00:26:45 +0200102static void update_num_ports(void)
103{
104 /*
105 * According to existing code in 'root_port_check_disable()', which does
106 * not agree with the confusing information on the datasheets, the last
107 * visible function depends on the strapped root port width as follows:
108 *
109 * +-----+----+----+----+----+
110 * | RPC | #5 | #6 | #7 | #8 |
111 * +-----+----+----+----+----+
112 * | 0 | x1 | x1 | x1 | x1 |
113 * | 1 | x2 | | x1 | x1 |
114 * | 2 | x2 | | x2 | |
115 * | 3 | x4 | | | |
116 * +-----+----+----+----+----+
117 */
118 switch ((rpc.strpfusecfg2 >> 14) & 0x3) {
119 case 0:
120 case 1:
121 break;
122 case 2:
123 rpc.num_ports = MIN(rpc.num_ports, 7);
124 break;
125 case 3:
126 rpc.num_ports = MIN(rpc.num_ports, 5);
127 break;
128 }
129
130 printk(BIOS_DEBUG, "Adjusted number of PCIe root ports to %d as per strpfusecfg2\n",
131 rpc.num_ports);
132}
133
Elyes HAOUAS7a5f7712018-06-08 17:20:38 +0200134static void root_port_init_config(struct device *dev)
Aaron Durbinc0254e62013-06-20 01:20:30 -0500135{
136 int rp;
137
138 if (root_port_is_first(dev)) {
139 rpc.orig_rpfn = RCBA32(RPFN);
140 rpc.new_rpfn = rpc.orig_rpfn;
141 rpc.num_ports = max_root_ports();
142 rpc.gbe_port = -1;
143
144 rpc.pin_ownership = pci_read_config32(dev, 0x410);
145 root_port_config_update_gbe_port();
146
147 if (dev->chip_info != NULL) {
148 struct southbridge_intel_lynxpoint_config *config;
149
150 config = dev->chip_info;
151 rpc.coalesce = config->pcie_port_coalesce;
152 }
153 }
154
155 rp = root_port_number(dev);
156 if (rp > rpc.num_ports) {
157 printk(BIOS_ERR, "Found Root Port %d, expecting %d\n",
158 rp, rpc.num_ports);
159 return;
160 }
161
162 /* Read the fuse configuration and pin ownership. */
163 switch (rp) {
164 case 1:
165 rpc.strpfusecfg1 = pci_read_config32(dev, 0xfc);
Stefan Reinauerab365af2013-12-03 12:13:26 -0800166 rpc.b0d28f0_32c = pci_read_config32(dev, 0x32c);
Aaron Durbinc0254e62013-06-20 01:20:30 -0500167 break;
168 case 5:
169 rpc.strpfusecfg2 = pci_read_config32(dev, 0xfc);
Stefan Reinauerab365af2013-12-03 12:13:26 -0800170 rpc.b0d28f4_32c = pci_read_config32(dev, 0x32c);
Angel Pons12404e02020-08-04 00:26:45 +0200171
172 if (!pch_is_lp())
173 update_num_ports();
174
Aaron Durbinc0254e62013-06-20 01:20:30 -0500175 break;
176 case 6:
Stefan Reinauerab365af2013-12-03 12:13:26 -0800177 rpc.b0d28f5_32c = pci_read_config32(dev, 0x32c);
Aaron Durbinc0254e62013-06-20 01:20:30 -0500178 rpc.strpfusecfg3 = pci_read_config32(dev, 0xfc);
179 break;
180 default:
181 break;
182 }
183
184 /* Cache pci device. */
185 rpc.ports[rp - 1] = dev;
Aaron Durbin60f82082013-06-19 13:28:04 -0500186}
187
188/* Update devicetree with new Root Port function number assignment */
Aaron Durbinc0254e62013-06-20 01:20:30 -0500189static void pch_pcie_device_set_func(int index, int pci_func)
Aaron Durbin60f82082013-06-19 13:28:04 -0500190{
Elyes HAOUAS7a5f7712018-06-08 17:20:38 +0200191 struct device *dev;
Martin Rothff744bf2019-10-23 21:46:03 -0600192 unsigned int new_devfn;
Aaron Durbin60f82082013-06-19 13:28:04 -0500193
Aaron Durbinc0254e62013-06-20 01:20:30 -0500194 dev = rpc.ports[index];
Aaron Durbin60f82082013-06-19 13:28:04 -0500195
Aaron Durbinc0254e62013-06-20 01:20:30 -0500196 /* Set the new PCI function field for this Root Port. */
197 rpc.new_rpfn &= ~RPFN_FNMASK(index);
198 rpc.new_rpfn |= RPFN_FNSET(index, pci_func);
Aaron Durbin60f82082013-06-19 13:28:04 -0500199
Aaron Durbinc0254e62013-06-20 01:20:30 -0500200 /* Determine the new devfn for this port */
201 new_devfn = PCI_DEVFN(PCH_PCIE_DEV_SLOT, pci_func);
Aaron Durbin60f82082013-06-19 13:28:04 -0500202
Tristan Corrick05b75242018-12-06 22:47:42 +1300203 if (dev && dev->path.pci.devfn != new_devfn) {
Aaron Durbinc0254e62013-06-20 01:20:30 -0500204 printk(BIOS_DEBUG,
205 "PCH: PCIe map %02x.%1x -> %02x.%1x\n",
206 PCI_SLOT(dev->path.pci.devfn),
207 PCI_FUNC(dev->path.pci.devfn),
208 PCI_SLOT(new_devfn), PCI_FUNC(new_devfn));
Aaron Durbin60f82082013-06-19 13:28:04 -0500209
Aaron Durbinc0254e62013-06-20 01:20:30 -0500210 dev->path.pci.devfn = new_devfn;
Aaron Durbin60f82082013-06-19 13:28:04 -0500211 }
212}
213
Aaron Durbin1c4289d2013-06-21 14:06:11 -0500214static void pcie_enable_clock_gating(void)
215{
216 int i;
217 int is_lp;
218 int enabled_ports;
219
220 is_lp = pch_is_lp();
221 enabled_ports = 0;
222
223 for (i = 0; i < rpc.num_ports; i++) {
Elyes HAOUAS7a5f7712018-06-08 17:20:38 +0200224 struct device *dev;
Aaron Durbin1c4289d2013-06-21 14:06:11 -0500225 int rp;
226
227 dev = rpc.ports[i];
Tristan Corrick05b75242018-12-06 22:47:42 +1300228 if (!dev)
229 continue;
230
Aaron Durbin1c4289d2013-06-21 14:06:11 -0500231 rp = root_port_number(dev);
232
Tristan Corrick05b75242018-12-06 22:47:42 +1300233 if (!is_rp_enabled(rp)) {
Aaron Durbin1c4289d2013-06-21 14:06:11 -0500234 /* Configure shared resource clock gating. */
235 if (rp == 1 || rp == 5 || (rp == 6 && is_lp))
Angel Ponsbf9bc502020-06-08 00:12:43 +0200236 pci_or_config8(dev, 0xe1, 0x3c);
Aaron Durbin1c4289d2013-06-21 14:06:11 -0500237
238 if (!is_lp) {
Tristan Corrick05b75242018-12-06 22:47:42 +1300239 if (rp == 1 && !is_rp_enabled(2) &&
240 !is_rp_enabled(3) && !is_rp_enabled(4)) {
Angel Ponsbf9bc502020-06-08 00:12:43 +0200241 pci_or_config8(dev, 0xe2, 1);
242 pci_or_config8(dev, 0xe1, 1 << 7);
Aaron Durbin1c4289d2013-06-21 14:06:11 -0500243 }
Tristan Corrick05b75242018-12-06 22:47:42 +1300244 if (rp == 5 && !is_rp_enabled(6) &&
245 !is_rp_enabled(7) && !is_rp_enabled(8)) {
Angel Ponsbf9bc502020-06-08 00:12:43 +0200246 pci_or_config8(dev, 0xe2, 1);
247 pci_or_config8(dev, 0xe1, 1 << 7);
Aaron Durbin1c4289d2013-06-21 14:06:11 -0500248 }
249 continue;
250 }
251
Angel Ponsbf9bc502020-06-08 00:12:43 +0200252 pci_or_config8(dev, 0xe2, 3 << 4);
253 pci_or_config32(dev, 0x420, 1 << 31);
Aaron Durbin1c4289d2013-06-21 14:06:11 -0500254
255 /* Per-Port CLKREQ# handling. */
256 if (is_lp && gpio_is_native(18 + rp - 1))
Angel Ponsbf9bc502020-06-08 00:12:43 +0200257 pci_or_config32(dev, 0x420, 3 << 29);
Aaron Durbin1c4289d2013-06-21 14:06:11 -0500258
259 /* Enable static clock gating. */
Tristan Corrick05b75242018-12-06 22:47:42 +1300260 if (rp == 1 && !is_rp_enabled(2) &&
261 !is_rp_enabled(3) && !is_rp_enabled(4)) {
Angel Ponsbf9bc502020-06-08 00:12:43 +0200262 pci_or_config8(dev, 0xe2, 1);
263 pci_or_config8(dev, 0xe1, 1 << 7);
Aaron Durbin1c4289d2013-06-21 14:06:11 -0500264 } else if (rp == 5 || rp == 6) {
Angel Ponsbf9bc502020-06-08 00:12:43 +0200265 pci_or_config8(dev, 0xe2, 1);
266 pci_or_config8(dev, 0xe1, 1 << 7);
Aaron Durbin1c4289d2013-06-21 14:06:11 -0500267 }
268 continue;
269 }
270
271 enabled_ports++;
272
273 /* Enable dynamic clock gating. */
Angel Ponsbf9bc502020-06-08 00:12:43 +0200274 pci_or_config8(dev, 0xe1, 0x03);
Aaron Durbin1c4289d2013-06-21 14:06:11 -0500275
276 if (is_lp) {
Angel Ponsbf9bc502020-06-08 00:12:43 +0200277 pci_or_config8(dev, 0xe2, 1 << 6);
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300278 pci_update_config8(dev, 0xe8, ~(3 << 2), (2 << 2));
Aaron Durbin1c4289d2013-06-21 14:06:11 -0500279 }
280
281 /* Update PECR1 register. */
Angel Ponsbf9bc502020-06-08 00:12:43 +0200282 pci_or_config8(dev, 0xe8, 1);
Aaron Durbin1c4289d2013-06-21 14:06:11 -0500283
Angel Pons72f4dda2020-09-25 00:23:20 +0200284 pci_or_config8(dev, 0x324, 1 << 5);
Aaron Durbin1c4289d2013-06-21 14:06:11 -0500285
286 /* Per-Port CLKREQ# handling. */
287 if (is_lp && gpio_is_native(18 + rp - 1))
Angel Ponsbf9bc502020-06-08 00:12:43 +0200288 pci_or_config32(dev, 0x420, 3 << 29);
Aaron Durbin1c4289d2013-06-21 14:06:11 -0500289
290 /* Configure shared resource clock gating. */
291 if (rp == 1 || rp == 5 || (rp == 6 && is_lp))
Angel Ponsbf9bc502020-06-08 00:12:43 +0200292 pci_or_config8(dev, 0xe1, 0x3c);
Aaron Durbin1c4289d2013-06-21 14:06:11 -0500293 }
294
Tristan Corrick05b75242018-12-06 22:47:42 +1300295 if (!enabled_ports && is_lp && rpc.ports[0])
Angel Ponsbf9bc502020-06-08 00:12:43 +0200296 pci_or_config8(rpc.ports[0], 0xe1, 1 << 6);
Aaron Durbin1c4289d2013-06-21 14:06:11 -0500297}
298
Aaron Durbinc0254e62013-06-20 01:20:30 -0500299static void root_port_commit_config(void)
Aaron Durbin60f82082013-06-19 13:28:04 -0500300{
Aaron Durbinc0254e62013-06-20 01:20:30 -0500301 int i;
Aaron Durbin60f82082013-06-19 13:28:04 -0500302
Aaron Durbinc0254e62013-06-20 01:20:30 -0500303 /* If the first root port is disabled the coalesce ports. */
Tristan Corrick05b75242018-12-06 22:47:42 +1300304 if (!is_rp_enabled(1))
Angel Ponsaf4bd562021-12-28 13:05:56 +0100305 rpc.coalesce = true;
Aaron Durbin60f82082013-06-19 13:28:04 -0500306
Aaron Durbin1c4289d2013-06-21 14:06:11 -0500307 /* Perform clock gating configuration. */
308 pcie_enable_clock_gating();
309
Aaron Durbinc0254e62013-06-20 01:20:30 -0500310 for (i = 0; i < rpc.num_ports; i++) {
Elyes HAOUAS7a5f7712018-06-08 17:20:38 +0200311 struct device *dev;
Aaron Durbin60f82082013-06-19 13:28:04 -0500312
Aaron Durbinc0254e62013-06-20 01:20:30 -0500313 dev = rpc.ports[i];
Aaron Durbin60f82082013-06-19 13:28:04 -0500314
Aaron Durbinc0254e62013-06-20 01:20:30 -0500315 if (dev == NULL) {
316 printk(BIOS_ERR, "Root Port %d device is NULL?\n", i+1);
317 continue;
Aaron Durbin60f82082013-06-19 13:28:04 -0500318 }
319
Aaron Durbinc0254e62013-06-20 01:20:30 -0500320 if (dev->enabled)
321 continue;
322
Elyes Haouas8b8ada62022-11-22 17:36:02 +0100323 printk(BIOS_DEBUG, "%s: Disabling device\n", dev_path(dev));
Aaron Durbinc0254e62013-06-20 01:20:30 -0500324
Aaron Durbin60f82082013-06-19 13:28:04 -0500325 /* Ensure memory, io, and bus master are all disabled */
Angel Ponsbf9bc502020-06-08 00:12:43 +0200326 pci_and_config16(dev, PCI_COMMAND,
327 ~(PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY | PCI_COMMAND_IO));
Aaron Durbin60f82082013-06-19 13:28:04 -0500328
Aaron Durbin60f82082013-06-19 13:28:04 -0500329 /* Disable this device if possible */
330 pch_disable_devfn(dev);
Aaron Durbin60f82082013-06-19 13:28:04 -0500331 }
332
Aaron Durbinc0254e62013-06-20 01:20:30 -0500333 if (rpc.coalesce) {
334 int current_func;
Aaron Durbin60f82082013-06-19 13:28:04 -0500335
Aaron Durbinc0254e62013-06-20 01:20:30 -0500336 /* For all Root Ports N enabled ports get assigned the lower
337 * PCI function number. The disabled ones get upper PCI
338 * function numbers. */
339 current_func = 0;
340 for (i = 0; i < rpc.num_ports; i++) {
Tristan Corrick05b75242018-12-06 22:47:42 +1300341 if (!is_rp_enabled(i + 1))
Aaron Durbinc0254e62013-06-20 01:20:30 -0500342 continue;
343 pch_pcie_device_set_func(i, current_func);
344 current_func++;
345 }
346
347 /* Allocate the disabled devices' PCI function number. */
348 for (i = 0; i < rpc.num_ports; i++) {
Tristan Corrick05b75242018-12-06 22:47:42 +1300349 if (is_rp_enabled(i + 1))
Aaron Durbinc0254e62013-06-20 01:20:30 -0500350 continue;
351 pch_pcie_device_set_func(i, current_func);
352 current_func++;
353 }
354 }
355
356 printk(BIOS_SPEW, "PCH: RPFN 0x%08x -> 0x%08x\n",
357 rpc.orig_rpfn, rpc.new_rpfn);
358 RCBA32(RPFN) = rpc.new_rpfn;
359}
360
Elyes HAOUAS7a5f7712018-06-08 17:20:38 +0200361static void root_port_mark_disable(struct device *dev)
Aaron Durbinc0254e62013-06-20 01:20:30 -0500362{
363 /* Mark device as disabled. */
364 dev->enabled = 0;
365 /* Mark device to be hidden. */
366 rpc.new_rpfn |= RPFN_HIDE(PCI_FUNC(dev->path.pci.devfn));
367}
368
Elyes HAOUAS7a5f7712018-06-08 17:20:38 +0200369static void root_port_check_disable(struct device *dev)
Aaron Durbinc0254e62013-06-20 01:20:30 -0500370{
371 int rp;
372 int is_lp;
373
374 /* Device already disabled. */
375 if (!dev->enabled) {
376 root_port_mark_disable(dev);
377 return;
378 }
379
380 rp = root_port_number(dev);
381
382 /* Is the GbE port mapped to this Root Port? */
383 if (rp == rpc.gbe_port) {
384 root_port_mark_disable(dev);
385 return;
386 }
387
388 is_lp = pch_is_lp();
389
390 /* Check Root Port Configuration. */
391 switch (rp) {
392 case 2:
393 /* Root Port 2 is disabled for all lane configurations
394 * but config 00b (4x1 links). */
395 if ((rpc.strpfusecfg1 >> 14) & 0x3) {
396 root_port_mark_disable(dev);
397 return;
398 }
399 break;
400 case 3:
401 /* Root Port 3 is disabled in config 11b (1x4 links). */
402 if (((rpc.strpfusecfg1 >> 14) & 0x3) == 0x3) {
403 root_port_mark_disable(dev);
404 return;
405 }
406 break;
407 case 4:
408 /* Root Port 4 is disabled in configs 11b (1x4 links)
409 * and 10b (2x2 links). */
410 if ((rpc.strpfusecfg1 >> 14) & 0x2) {
411 root_port_mark_disable(dev);
412 return;
413 }
414 break;
415 case 6:
416 if (is_lp)
417 break;
418 /* Root Port 6 is disabled for all lane configurations
419 * but config 00b (4x1 links). */
420 if ((rpc.strpfusecfg2 >> 14) & 0x3) {
421 root_port_mark_disable(dev);
422 return;
423 }
424 break;
425 case 7:
426 if (is_lp)
427 break;
Tristan Corrickbaa4c072018-12-06 22:47:21 +1300428 /* Root Port 7 is disabled in config 11b (1x4 links). */
Aaron Durbinc0254e62013-06-20 01:20:30 -0500429 if (((rpc.strpfusecfg2 >> 14) & 0x3) == 0x3) {
430 root_port_mark_disable(dev);
431 return;
432 }
433 break;
434 case 8:
435 if (is_lp)
436 break;
437 /* Root Port 8 is disabled in configs 11b (1x4 links)
438 * and 10b (2x2 links). */
439 if ((rpc.strpfusecfg2 >> 14) & 0x2) {
440 root_port_mark_disable(dev);
441 return;
442 }
443 break;
444 }
445
446 /* Check Pin Ownership. */
447 if (is_lp) {
448 switch (rp) {
449 case 1:
450 /* Bit 0 is Root Port 1 ownership. */
451 if ((rpc.pin_ownership & 0x1) == 0) {
452 root_port_mark_disable(dev);
453 return;
454 }
455 break;
456 case 2:
457 /* Bit 2 is Root Port 2 ownership. */
458 if ((rpc.pin_ownership & 0x4) == 0) {
459 root_port_mark_disable(dev);
460 return;
461 }
462 break;
463 case 6:
464 /* Bits 7:4 are Root Port 6 pin-lane ownership. */
465 if ((rpc.pin_ownership & 0xf0) == 0) {
466 root_port_mark_disable(dev);
467 return;
468 }
469 break;
470 }
471 } else {
472 switch (rp) {
473 case 1:
474 /* Bits 4 and 0 are Root Port 1 ownership. */
475 if ((rpc.pin_ownership & 0x11) == 0) {
476 root_port_mark_disable(dev);
477 return;
478 }
479 break;
480 case 2:
481 /* Bits 5 and 2 are Root Port 2 ownership. */
482 if ((rpc.pin_ownership & 0x24) == 0) {
483 root_port_mark_disable(dev);
484 return;
485 }
486 break;
487 }
Aaron Durbin60f82082013-06-19 13:28:04 -0500488 }
489}
490
Stefan Reinauerab365af2013-12-03 12:13:26 -0800491static void pcie_add_0x0202000_iobp(u32 reg)
492{
493 u32 reg32;
Aaron Durbin76c37002012-10-30 09:03:43 -0500494
Stefan Reinauerab365af2013-12-03 12:13:26 -0800495 reg32 = pch_iobp_read(reg);
496 reg32 += (0x2 << 16) | (0x2 << 8);
497 pch_iobp_write(reg, reg32);
498}
Aaron Durbin76c37002012-10-30 09:03:43 -0500499
Stefan Reinauerab365af2013-12-03 12:13:26 -0800500static void pch_pcie_early(struct device *dev)
501{
Duncan Laurie249a03b2013-08-09 09:06:41 -0700502 struct southbridge_intel_lynxpoint_config *config = dev->chip_info;
Angel Pons2aaf7c02020-09-24 18:03:18 +0200503 int do_aspm = 0;
504 int rp = root_port_number(dev);
505 int is_lp = pch_is_lp();
Aaron Durbin76c37002012-10-30 09:03:43 -0500506
Stefan Reinauerab365af2013-12-03 12:13:26 -0800507 if (is_lp) {
508 switch (rp) {
509 case 1:
510 case 2:
511 case 3:
512 case 4:
Angel Pons2aaf7c02020-09-24 18:03:18 +0200513 /*
514 * Bits 31:28 of b0d28f0 0x32c register correspond to
515 * Root Ports 4:1.
516 */
Stefan Reinauerab365af2013-12-03 12:13:26 -0800517 do_aspm = !!(rpc.b0d28f0_32c & (1 << (28 + rp - 1)));
518 break;
519 case 5:
Angel Pons2aaf7c02020-09-24 18:03:18 +0200520 /*
521 * Bit 28 of b0d28f4 0x32c register correspond to
522 * Root Ports 4:1.
523 */
Stefan Reinauerab365af2013-12-03 12:13:26 -0800524 do_aspm = !!(rpc.b0d28f4_32c & (1 << 28));
525 break;
526 case 6:
Angel Pons2aaf7c02020-09-24 18:03:18 +0200527 /*
528 * Bit 28 of b0d28f5 0x32c register correspond to
529 * Root Ports 4:1.
530 */
Stefan Reinauerab365af2013-12-03 12:13:26 -0800531 do_aspm = !!(rpc.b0d28f5_32c & (1 << 28));
532 break;
533 }
534 } else {
535 switch (rp) {
536 case 1:
537 case 2:
538 case 3:
539 case 4:
Angel Pons2aaf7c02020-09-24 18:03:18 +0200540 /*
541 * Bits 31:28 of b0d28f0 0x32c register correspond to
542 * Root Ports 4:1.
543 */
Stefan Reinauerab365af2013-12-03 12:13:26 -0800544 do_aspm = !!(rpc.b0d28f0_32c & (1 << (28 + rp - 1)));
545 break;
546 case 5:
547 case 6:
548 case 7:
549 case 8:
Angel Pons2aaf7c02020-09-24 18:03:18 +0200550 /*
551 * Bits 31:28 of b0d28f4 0x32c register correspond to
552 * Root Ports 8:5.
553 */
Stefan Reinauerab365af2013-12-03 12:13:26 -0800554 do_aspm = !!(rpc.b0d28f4_32c & (1 << (28 + rp - 5)));
555 break;
556 }
Aaron Durbin76c37002012-10-30 09:03:43 -0500557 }
Stefan Reinauerab365af2013-12-03 12:13:26 -0800558
Duncan Laurie249a03b2013-08-09 09:06:41 -0700559 /* Allow ASPM to be forced on in devicetree */
560 if (config && (config->pcie_port_force_aspm & (1 << (rp - 1))))
561 do_aspm = 1;
562
563 printk(BIOS_DEBUG, "PCIe Root Port %d ASPM is %sabled\n",
564 rp, do_aspm ? "en" : "dis");
565
Stefan Reinauerab365af2013-12-03 12:13:26 -0800566 if (do_aspm) {
567 /* Set ASPM bits in MPC2 register. */
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300568 pci_update_config32(dev, 0xd4, ~(0x3 << 2), (1 << 4) | (0x2 << 2));
Stefan Reinauerab365af2013-12-03 12:13:26 -0800569
570 /* Set unique clock exit latency in MPC register. */
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300571 pci_update_config32(dev, 0xd8, ~(0x7 << 18), (0x7 << 18));
Stefan Reinauerab365af2013-12-03 12:13:26 -0800572
573 /* Set L1 exit latency in LCAP register. */
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300574 pci_update_config32(dev, 0x4c, ~(0x7 << 15), (0x4 << 15));
Stefan Reinauerab365af2013-12-03 12:13:26 -0800575
576 if (is_lp) {
577 switch (rp) {
578 case 1:
579 pcie_add_0x0202000_iobp(0xe9002440);
580 break;
581 case 2:
582 pcie_add_0x0202000_iobp(0xe9002640);
583 break;
584 case 3:
585 pcie_add_0x0202000_iobp(0xe9000840);
586 break;
587 case 4:
588 pcie_add_0x0202000_iobp(0xe9000a40);
589 break;
590 case 5:
591 pcie_add_0x0202000_iobp(0xe9000c40);
592 pcie_add_0x0202000_iobp(0xe9000e40);
593 pcie_add_0x0202000_iobp(0xe9001040);
594 pcie_add_0x0202000_iobp(0xe9001240);
595 break;
596 case 6:
597 /* Update IOBP based on lane ownership. */
598 if (rpc.pin_ownership & (1 << 4))
599 pcie_add_0x0202000_iobp(0xea002040);
600 if (rpc.pin_ownership & (1 << 5))
601 pcie_add_0x0202000_iobp(0xea002240);
602 if (rpc.pin_ownership & (1 << 6))
603 pcie_add_0x0202000_iobp(0xea002440);
604 if (rpc.pin_ownership & (1 << 7))
605 pcie_add_0x0202000_iobp(0xea002640);
606 break;
607 }
608 } else {
609 switch (rp) {
610 case 1:
611 if ((rpc.pin_ownership & 0x3) == 1)
612 pcie_add_0x0202000_iobp(0xe9002e40);
613 else
614 pcie_add_0x0202000_iobp(0xea002040);
615 break;
616 case 2:
617 if ((rpc.pin_ownership & 0xc) == 0x4)
618 pcie_add_0x0202000_iobp(0xe9002c40);
619 else
620 pcie_add_0x0202000_iobp(0xea002240);
621 break;
622 case 3:
623 pcie_add_0x0202000_iobp(0xe9002a40);
624 break;
625 case 4:
626 pcie_add_0x0202000_iobp(0xe9002840);
627 break;
628 case 5:
629 pcie_add_0x0202000_iobp(0xe9002640);
630 break;
631 case 6:
632 pcie_add_0x0202000_iobp(0xe9002440);
633 break;
634 case 7:
635 pcie_add_0x0202000_iobp(0xe9002240);
636 break;
637 case 8:
638 pcie_add_0x0202000_iobp(0xe9002040);
639 break;
640 }
641 }
642
Angel Ponsbf9bc502020-06-08 00:12:43 +0200643 pci_and_config32(dev, 0x338, ~(1 << 26));
Stefan Reinauerab365af2013-12-03 12:13:26 -0800644 }
645
Angel Ponsa575759c2020-11-05 11:35:54 +0100646 /* Enable LTR in Root Port. Disable OBFF. */
647 pci_update_config32(dev, 0x64, ~(3 << 18), 1 << 11);
648 pci_update_config16(dev, 0x68, ~(3 << 13), 1 << 10);
Stefan Reinauerab365af2013-12-03 12:13:26 -0800649
Angel Pons2aaf7c02020-09-24 18:03:18 +0200650 pci_update_config32(dev, 0x318, ~(0xffff << 16), (0x1414 << 16));
Stefan Reinauerab365af2013-12-03 12:13:26 -0800651
652 /* Set L1 exit latency in LCAP register. */
653 if (!do_aspm && (pci_read_config8(dev, 0xf5) & 0x1))
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300654 pci_update_config32(dev, 0x4c, ~(0x7 << 15), (0x4 << 15));
Stefan Reinauerab365af2013-12-03 12:13:26 -0800655 else
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300656 pci_update_config32(dev, 0x4c, ~(0x7 << 15), (0x2 << 15));
Stefan Reinauerab365af2013-12-03 12:13:26 -0800657
Angel Ponsbf9bc502020-06-08 00:12:43 +0200658 pci_update_config32(dev, 0x314, 0, 0x743a361b);
Stefan Reinauerab365af2013-12-03 12:13:26 -0800659
660 /* Set Common Clock Exit Latency in MPC register. */
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300661 pci_update_config32(dev, 0xd8, ~(0x7 << 15), (0x3 << 15));
Stefan Reinauerab365af2013-12-03 12:13:26 -0800662
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300663 pci_update_config32(dev, 0x33c, ~0x00ffffff, 0x854c74);
Stefan Reinauerab365af2013-12-03 12:13:26 -0800664
Matt DeVillier1aeccd12020-10-07 13:18:55 -0500665 /* Set Invalid Receive Range Check Enable in MPC register. */
Angel Ponsbf9bc502020-06-08 00:12:43 +0200666 pci_or_config32(dev, 0xd8, 1 << 25);
Stefan Reinauerab365af2013-12-03 12:13:26 -0800667
Angel Ponsbf9bc502020-06-08 00:12:43 +0200668 pci_and_config8(dev, 0xf5, 0x3f);
Stefan Reinauerab365af2013-12-03 12:13:26 -0800669
670 if (rp == 1 || rp == 5 || (is_lp && rp == 6))
Angel Ponsbf9bc502020-06-08 00:12:43 +0200671 pci_and_config8(dev, 0xf7, ~0x0c);
Stefan Reinauerab365af2013-12-03 12:13:26 -0800672
673 /* Set EOI forwarding disable. */
Angel Ponsbf9bc502020-06-08 00:12:43 +0200674 pci_or_config32(dev, 0xd4, 1 << 1);
Stefan Reinauerab365af2013-12-03 12:13:26 -0800675
Matt DeVillier7f633532020-10-07 13:11:58 -0500676 /* Set AER Extended Cap ID to 01h and Next Cap Pointer to 200h. */
677 if (CONFIG(PCIEXP_AER))
678 pci_update_config32(dev, 0x100, ~0xfffff, (1 << 29) | 0x10001);
679 else
680 pci_update_config32(dev, 0x100, ~0xfffff, (1 << 29));
Stefan Reinauerab365af2013-12-03 12:13:26 -0800681
Matt DeVillier54e1f592020-10-07 13:17:09 -0500682 /* Set L1 Sub-State Cap ID to 1Eh and Next Cap Pointer to None. */
683 if (CONFIG(PCIEXP_L1_SUB_STATE))
684 pci_update_config32(dev, 0x200, ~0xfffff, 0x001e);
685 else
686 pci_update_config32(dev, 0x200, ~0xfffff, 0);
687
Stefan Reinauerab365af2013-12-03 12:13:26 -0800688 if (is_lp)
Angel Ponsbf9bc502020-06-08 00:12:43 +0200689 pci_or_config32(dev, 0x100, 1 << 29);
Stefan Reinauerab365af2013-12-03 12:13:26 -0800690
691 /* Read and write back write-once capability registers. */
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300692 pci_update_config32(dev, 0x34, ~0, 0);
693 pci_update_config32(dev, 0x40, ~0, 0);
694 pci_update_config32(dev, 0x80, ~0, 0);
695 pci_update_config32(dev, 0x90, ~0, 0);
Aaron Durbin76c37002012-10-30 09:03:43 -0500696}
697
Angel Pons2aaf7c02020-09-24 18:03:18 +0200698static void pch_pcie_init(struct device *dev)
Aaron Durbin76c37002012-10-30 09:03:43 -0500699{
Aaron Durbin76c37002012-10-30 09:03:43 -0500700 printk(BIOS_DEBUG, "Initializing PCH PCIe bridge.\n");
701
Aaron Durbinc0254e62013-06-20 01:20:30 -0500702 /* Enable SERR */
Elyes HAOUAS73ae0762020-04-28 10:13:05 +0200703 pci_or_config16(dev, PCI_COMMAND, PCI_COMMAND_SERR);
Aaron Durbinc0254e62013-06-20 01:20:30 -0500704
Aaron Durbin76c37002012-10-30 09:03:43 -0500705 /* Enable Bus Master */
Elyes HAOUAS73ae0762020-04-28 10:13:05 +0200706 pci_or_config16(dev, PCI_COMMAND, PCI_COMMAND_MASTER);
Aaron Durbin76c37002012-10-30 09:03:43 -0500707
708 /* Set Cache Line Size to 0x10 */
709 // This has no effect but the OS might expect it
710 pci_write_config8(dev, 0x0c, 0x10);
711
Angel Ponsbf9bc502020-06-08 00:12:43 +0200712 pci_and_config16(dev, PCI_BRIDGE_CONTROL, ~PCI_BRIDGE_CTL_PARITY);
Aaron Durbin76c37002012-10-30 09:03:43 -0500713
Aaron Durbin76c37002012-10-30 09:03:43 -0500714 /* Clear errors in status registers */
Angel Ponsbf9bc502020-06-08 00:12:43 +0200715 pci_update_config16(dev, 0x06, ~0, 0);
716 pci_update_config16(dev, 0x1e, ~0, 0);
Aaron Durbin76c37002012-10-30 09:03:43 -0500717}
718
Elyes HAOUAS7a5f7712018-06-08 17:20:38 +0200719static void pch_pcie_enable(struct device *dev)
Aaron Durbin76c37002012-10-30 09:03:43 -0500720{
Aaron Durbinc0254e62013-06-20 01:20:30 -0500721 /* Add this device to the root port config structure. */
722 root_port_init_config(dev);
723
724 /* Check to see if this Root Port should be disabled. */
725 root_port_check_disable(dev);
726
Aaron Durbin76c37002012-10-30 09:03:43 -0500727 /* Power Management init before enumeration */
Aaron Durbinc0254e62013-06-20 01:20:30 -0500728 if (dev->enabled)
Stefan Reinauerab365af2013-12-03 12:13:26 -0800729 pch_pcie_early(dev);
Aaron Durbinc0254e62013-06-20 01:20:30 -0500730
731 /*
732 * When processing the last PCIe root port we can now
733 * update the Root Port Function Number and Hide register.
734 */
735 if (root_port_is_last(dev))
736 root_port_commit_config();
Aaron Durbin76c37002012-10-30 09:03:43 -0500737}
738
Aaron Durbin76c37002012-10-30 09:03:43 -0500739static struct device_operations device_ops = {
740 .read_resources = pci_bus_read_resources,
741 .set_resources = pci_dev_set_resources,
742 .enable_resources = pci_bus_enable_resources,
Angel Pons2aaf7c02020-09-24 18:03:18 +0200743 .init = pch_pcie_init,
Aaron Durbin76c37002012-10-30 09:03:43 -0500744 .enable = pch_pcie_enable,
745 .scan_bus = pciexp_scan_bridge,
Angel Pons1fc0edd2020-05-31 00:03:28 +0200746 .ops_pci = &pci_dev_ops_pci,
Aaron Durbin76c37002012-10-30 09:03:43 -0500747};
748
Duncan Laurie74c0d052012-12-17 11:31:40 -0800749static const unsigned short pci_device_ids[] = {
Felix Singer43b7f412022-03-07 04:34:52 +0100750 PCI_DID_INTEL_LPT_H_PCIE_RP1,
751 PCI_DID_INTEL_LPT_H_PCIE_RP2,
752 PCI_DID_INTEL_LPT_H_PCIE_RP3,
753 PCI_DID_INTEL_LPT_H_PCIE_RP4,
754 PCI_DID_INTEL_LPT_H_PCIE_RP5,
755 PCI_DID_INTEL_LPT_H_PCIE_RP6,
756 PCI_DID_INTEL_LPT_H_PCIE_RP7,
757 PCI_DID_INTEL_LPT_H_PCIE_RP8,
758 PCI_DID_INTEL_LPT_LP_PCIE_RP1,
759 PCI_DID_INTEL_LPT_LP_PCIE_RP2,
760 PCI_DID_INTEL_LPT_LP_PCIE_RP3,
761 PCI_DID_INTEL_LPT_LP_PCIE_RP4,
762 PCI_DID_INTEL_LPT_LP_PCIE_RP5,
763 PCI_DID_INTEL_LPT_LP_PCIE_RP6,
Duncan Laurie74c0d052012-12-17 11:31:40 -0800764 0
765};
Aaron Durbin76c37002012-10-30 09:03:43 -0500766
767static const struct pci_driver pch_pcie __pci_driver = {
768 .ops = &device_ops,
Felix Singer43b7f412022-03-07 04:34:52 +0100769 .vendor = PCI_VID_INTEL,
Aaron Durbin76c37002012-10-30 09:03:43 -0500770 .devices = pci_device_ids,
771};