blob: 0ede943ceb2fbd97191ec26ecb96c9d72d32588b [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>
Kyösti Mälkkidf128a52019-09-21 18:35:37 +03008#include <device/pci_def.h>
Aaron Durbin76c37002012-10-30 09:03:43 -05009#include <device/pciexp.h>
10#include <device/pci_ids.h>
Patrick Rudolphe56189c2018-04-18 10:11:59 +020011#include <device/pci_ops.h>
Angel Pons2178b722020-05-31 00:55:35 +020012#include "iobp.h"
Aaron Durbin76c37002012-10-30 09:03:43 -050013#include "pch.h"
Patrick Rudolph273a8dc2016-02-06 18:07:59 +010014#include <southbridge/intel/common/gpio.h>
Tristan Corrick05b75242018-12-06 22:47:42 +130015#include <stdint.h>
Kyösti Mälkki12b121c2019-08-18 16:33:39 +030016#include "chip.h"
Aaron Durbin76c37002012-10-30 09:03:43 -050017
Aaron Durbinc0254e62013-06-20 01:20:30 -050018#define MAX_NUM_ROOT_PORTS 8
Aaron Durbinc0254e62013-06-20 01:20:30 -050019
20struct root_port_config {
21 /* RPFN is a write-once register so keep a copy until it is written */
22 u32 orig_rpfn;
23 u32 new_rpfn;
24 u32 pin_ownership;
25 u32 strpfusecfg1;
26 u32 strpfusecfg2;
27 u32 strpfusecfg3;
Stefan Reinauerab365af2013-12-03 12:13:26 -080028 u32 b0d28f0_32c;
29 u32 b0d28f4_32c;
30 u32 b0d28f5_32c;
Aaron Durbinc0254e62013-06-20 01:20:30 -050031 int coalesce;
32 int gbe_port;
33 int num_ports;
Elyes HAOUAS7a5f7712018-06-08 17:20:38 +020034 struct device *ports[MAX_NUM_ROOT_PORTS];
Aaron Durbinc0254e62013-06-20 01:20:30 -050035};
36
37static struct root_port_config rpc;
38
39static inline int max_root_ports(void)
Aaron Durbin60f82082013-06-19 13:28:04 -050040{
Tristan Corrickd3f01b22018-12-06 22:46:58 +130041 if (pch_is_lp() || pch_silicon_id() == PCI_DEVICE_ID_INTEL_LPT_H81)
42 return 6;
43
44 return 8;
Aaron Durbin60f82082013-06-19 13:28:04 -050045}
46
Elyes HAOUAS7a5f7712018-06-08 17:20:38 +020047static inline int root_port_is_first(struct device *dev)
Aaron Durbin60f82082013-06-19 13:28:04 -050048{
Aaron Durbinc0254e62013-06-20 01:20:30 -050049 return PCI_FUNC(dev->path.pci.devfn) == 0;
50}
Aaron Durbin60f82082013-06-19 13:28:04 -050051
Elyes HAOUAS7a5f7712018-06-08 17:20:38 +020052static inline int root_port_is_last(struct device *dev)
Aaron Durbinc0254e62013-06-20 01:20:30 -050053{
54 return PCI_FUNC(dev->path.pci.devfn) == (rpc.num_ports - 1);
55}
Aaron Durbin60f82082013-06-19 13:28:04 -050056
Aaron Durbinc0254e62013-06-20 01:20:30 -050057/* Root ports are numbered 1..N in the documentation. */
Elyes HAOUAS7a5f7712018-06-08 17:20:38 +020058static inline int root_port_number(struct device *dev)
Aaron Durbinc0254e62013-06-20 01:20:30 -050059{
60 return PCI_FUNC(dev->path.pci.devfn) + 1;
61}
Aaron Durbin60f82082013-06-19 13:28:04 -050062
Tristan Corrick05b75242018-12-06 22:47:42 +130063static bool is_rp_enabled(int rp)
64{
65 ASSERT(rp > 0 && rp <= ARRAY_SIZE(rpc.ports));
66
67 if (rpc.ports[rp - 1] == NULL)
68 return false;
69
70 return rpc.ports[rp - 1]->enabled;
71}
72
Aaron Durbinc0254e62013-06-20 01:20:30 -050073static void root_port_config_update_gbe_port(void)
74{
75 /* Is the Gbe Port enabled? */
76 if (!((rpc.strpfusecfg1 >> 19) & 1))
77 return;
78
79 if (pch_is_lp()) {
80 switch ((rpc.strpfusecfg1 >> 16) & 0x7) {
81 case 0:
82 rpc.gbe_port = 3;
83 break;
84 case 1:
85 rpc.gbe_port = 4;
86 break;
87 case 2:
88 case 3:
89 case 4:
90 case 5:
91 /* Lanes 0-4 of Root Port 5. */
92 rpc.gbe_port = 5;
93 break;
94 default:
95 printk(BIOS_DEBUG, "Invalid GbE Port Selection.\n");
96 }
97 } else {
98 /* Non-LP has 1:1 mapping with root ports. */
99 rpc.gbe_port = ((rpc.strpfusecfg1 >> 16) & 0x7) + 1;
100 }
101}
102
Angel Pons12404e02020-08-04 00:26:45 +0200103static void update_num_ports(void)
104{
105 /*
106 * According to existing code in 'root_port_check_disable()', which does
107 * not agree with the confusing information on the datasheets, the last
108 * visible function depends on the strapped root port width as follows:
109 *
110 * +-----+----+----+----+----+
111 * | RPC | #5 | #6 | #7 | #8 |
112 * +-----+----+----+----+----+
113 * | 0 | x1 | x1 | x1 | x1 |
114 * | 1 | x2 | | x1 | x1 |
115 * | 2 | x2 | | x2 | |
116 * | 3 | x4 | | | |
117 * +-----+----+----+----+----+
118 */
119 switch ((rpc.strpfusecfg2 >> 14) & 0x3) {
120 case 0:
121 case 1:
122 break;
123 case 2:
124 rpc.num_ports = MIN(rpc.num_ports, 7);
125 break;
126 case 3:
127 rpc.num_ports = MIN(rpc.num_ports, 5);
128 break;
129 }
130
131 printk(BIOS_DEBUG, "Adjusted number of PCIe root ports to %d as per strpfusecfg2\n",
132 rpc.num_ports);
133}
134
Elyes HAOUAS7a5f7712018-06-08 17:20:38 +0200135static void root_port_init_config(struct device *dev)
Aaron Durbinc0254e62013-06-20 01:20:30 -0500136{
137 int rp;
138
139 if (root_port_is_first(dev)) {
140 rpc.orig_rpfn = RCBA32(RPFN);
141 rpc.new_rpfn = rpc.orig_rpfn;
142 rpc.num_ports = max_root_ports();
143 rpc.gbe_port = -1;
144
145 rpc.pin_ownership = pci_read_config32(dev, 0x410);
146 root_port_config_update_gbe_port();
147
148 if (dev->chip_info != NULL) {
149 struct southbridge_intel_lynxpoint_config *config;
150
151 config = dev->chip_info;
152 rpc.coalesce = config->pcie_port_coalesce;
153 }
154 }
155
156 rp = root_port_number(dev);
157 if (rp > rpc.num_ports) {
158 printk(BIOS_ERR, "Found Root Port %d, expecting %d\n",
159 rp, rpc.num_ports);
160 return;
161 }
162
163 /* Read the fuse configuration and pin ownership. */
164 switch (rp) {
165 case 1:
166 rpc.strpfusecfg1 = pci_read_config32(dev, 0xfc);
Stefan Reinauerab365af2013-12-03 12:13:26 -0800167 rpc.b0d28f0_32c = pci_read_config32(dev, 0x32c);
Aaron Durbinc0254e62013-06-20 01:20:30 -0500168 break;
169 case 5:
170 rpc.strpfusecfg2 = pci_read_config32(dev, 0xfc);
Stefan Reinauerab365af2013-12-03 12:13:26 -0800171 rpc.b0d28f4_32c = pci_read_config32(dev, 0x32c);
Angel Pons12404e02020-08-04 00:26:45 +0200172
173 if (!pch_is_lp())
174 update_num_ports();
175
Aaron Durbinc0254e62013-06-20 01:20:30 -0500176 break;
177 case 6:
Stefan Reinauerab365af2013-12-03 12:13:26 -0800178 rpc.b0d28f5_32c = pci_read_config32(dev, 0x32c);
Aaron Durbinc0254e62013-06-20 01:20:30 -0500179 rpc.strpfusecfg3 = pci_read_config32(dev, 0xfc);
180 break;
181 default:
182 break;
183 }
184
185 /* Cache pci device. */
186 rpc.ports[rp - 1] = dev;
Aaron Durbin60f82082013-06-19 13:28:04 -0500187}
188
189/* Update devicetree with new Root Port function number assignment */
Aaron Durbinc0254e62013-06-20 01:20:30 -0500190static void pch_pcie_device_set_func(int index, int pci_func)
Aaron Durbin60f82082013-06-19 13:28:04 -0500191{
Elyes HAOUAS7a5f7712018-06-08 17:20:38 +0200192 struct device *dev;
Martin Rothff744bf2019-10-23 21:46:03 -0600193 unsigned int new_devfn;
Aaron Durbin60f82082013-06-19 13:28:04 -0500194
Aaron Durbinc0254e62013-06-20 01:20:30 -0500195 dev = rpc.ports[index];
Aaron Durbin60f82082013-06-19 13:28:04 -0500196
Aaron Durbinc0254e62013-06-20 01:20:30 -0500197 /* Set the new PCI function field for this Root Port. */
198 rpc.new_rpfn &= ~RPFN_FNMASK(index);
199 rpc.new_rpfn |= RPFN_FNSET(index, pci_func);
Aaron Durbin60f82082013-06-19 13:28:04 -0500200
Aaron Durbinc0254e62013-06-20 01:20:30 -0500201 /* Determine the new devfn for this port */
202 new_devfn = PCI_DEVFN(PCH_PCIE_DEV_SLOT, pci_func);
Aaron Durbin60f82082013-06-19 13:28:04 -0500203
Tristan Corrick05b75242018-12-06 22:47:42 +1300204 if (dev && dev->path.pci.devfn != new_devfn) {
Aaron Durbinc0254e62013-06-20 01:20:30 -0500205 printk(BIOS_DEBUG,
206 "PCH: PCIe map %02x.%1x -> %02x.%1x\n",
207 PCI_SLOT(dev->path.pci.devfn),
208 PCI_FUNC(dev->path.pci.devfn),
209 PCI_SLOT(new_devfn), PCI_FUNC(new_devfn));
Aaron Durbin60f82082013-06-19 13:28:04 -0500210
Aaron Durbinc0254e62013-06-20 01:20:30 -0500211 dev->path.pci.devfn = new_devfn;
Aaron Durbin60f82082013-06-19 13:28:04 -0500212 }
213}
214
Aaron Durbin1c4289d2013-06-21 14:06:11 -0500215static void pcie_enable_clock_gating(void)
216{
217 int i;
218 int is_lp;
219 int enabled_ports;
220
221 is_lp = pch_is_lp();
222 enabled_ports = 0;
223
224 for (i = 0; i < rpc.num_ports; i++) {
Elyes HAOUAS7a5f7712018-06-08 17:20:38 +0200225 struct device *dev;
Aaron Durbin1c4289d2013-06-21 14:06:11 -0500226 int rp;
227
228 dev = rpc.ports[i];
Tristan Corrick05b75242018-12-06 22:47:42 +1300229 if (!dev)
230 continue;
231
Aaron Durbin1c4289d2013-06-21 14:06:11 -0500232 rp = root_port_number(dev);
233
Tristan Corrick05b75242018-12-06 22:47:42 +1300234 if (!is_rp_enabled(rp)) {
Ryan Salsamendi3f2fe182017-07-04 13:14:16 -0700235
Aaron Durbin1c4289d2013-06-21 14:06:11 -0500236 /* Configure shared resource clock gating. */
237 if (rp == 1 || rp == 5 || (rp == 6 && is_lp))
Angel Ponsbf9bc502020-06-08 00:12:43 +0200238 pci_or_config8(dev, 0xe1, 0x3c);
Aaron Durbin1c4289d2013-06-21 14:06:11 -0500239
240 if (!is_lp) {
Tristan Corrick05b75242018-12-06 22:47:42 +1300241 if (rp == 1 && !is_rp_enabled(2) &&
242 !is_rp_enabled(3) && !is_rp_enabled(4)) {
Angel Ponsbf9bc502020-06-08 00:12:43 +0200243 pci_or_config8(dev, 0xe2, 1);
244 pci_or_config8(dev, 0xe1, 1 << 7);
Aaron Durbin1c4289d2013-06-21 14:06:11 -0500245 }
Tristan Corrick05b75242018-12-06 22:47:42 +1300246 if (rp == 5 && !is_rp_enabled(6) &&
247 !is_rp_enabled(7) && !is_rp_enabled(8)) {
Angel Ponsbf9bc502020-06-08 00:12:43 +0200248 pci_or_config8(dev, 0xe2, 1);
249 pci_or_config8(dev, 0xe1, 1 << 7);
Aaron Durbin1c4289d2013-06-21 14:06:11 -0500250 }
251 continue;
252 }
253
Angel Ponsbf9bc502020-06-08 00:12:43 +0200254 pci_or_config8(dev, 0xe2, 3 << 4);
255 pci_or_config32(dev, 0x420, 1 << 31);
Aaron Durbin1c4289d2013-06-21 14:06:11 -0500256
257 /* Per-Port CLKREQ# handling. */
258 if (is_lp && gpio_is_native(18 + rp - 1))
Angel Ponsbf9bc502020-06-08 00:12:43 +0200259 pci_or_config32(dev, 0x420, 3 << 29);
Aaron Durbin1c4289d2013-06-21 14:06:11 -0500260
261 /* Enable static clock gating. */
Tristan Corrick05b75242018-12-06 22:47:42 +1300262 if (rp == 1 && !is_rp_enabled(2) &&
263 !is_rp_enabled(3) && !is_rp_enabled(4)) {
Angel Ponsbf9bc502020-06-08 00:12:43 +0200264 pci_or_config8(dev, 0xe2, 1);
265 pci_or_config8(dev, 0xe1, 1 << 7);
Aaron Durbin1c4289d2013-06-21 14:06:11 -0500266 } else if (rp == 5 || rp == 6) {
Angel Ponsbf9bc502020-06-08 00:12:43 +0200267 pci_or_config8(dev, 0xe2, 1);
268 pci_or_config8(dev, 0xe1, 1 << 7);
Aaron Durbin1c4289d2013-06-21 14:06:11 -0500269 }
270 continue;
271 }
272
273 enabled_ports++;
274
275 /* Enable dynamic clock gating. */
Angel Ponsbf9bc502020-06-08 00:12:43 +0200276 pci_or_config8(dev, 0xe1, 0x03);
Aaron Durbin1c4289d2013-06-21 14:06:11 -0500277
278 if (is_lp) {
Angel Ponsbf9bc502020-06-08 00:12:43 +0200279 pci_or_config8(dev, 0xe2, 1 << 6);
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300280 pci_update_config8(dev, 0xe8, ~(3 << 2), (2 << 2));
Aaron Durbin1c4289d2013-06-21 14:06:11 -0500281 }
282
283 /* Update PECR1 register. */
Angel Ponsbf9bc502020-06-08 00:12:43 +0200284 pci_or_config8(dev, 0xe8, 1);
Aaron Durbin1c4289d2013-06-21 14:06:11 -0500285
Angel Ponsbf9bc502020-06-08 00:12:43 +0200286 /* FIXME: Are we supposed to update this register with a constant boolean? */
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300287 pci_update_config8(dev, 0x324, ~(1 << 5), (1 < 5));
Aaron Durbin1c4289d2013-06-21 14:06:11 -0500288
289 /* Per-Port CLKREQ# handling. */
290 if (is_lp && gpio_is_native(18 + rp - 1))
Angel Ponsbf9bc502020-06-08 00:12:43 +0200291 pci_or_config32(dev, 0x420, 3 << 29);
Aaron Durbin1c4289d2013-06-21 14:06:11 -0500292
293 /* Configure shared resource clock gating. */
294 if (rp == 1 || rp == 5 || (rp == 6 && is_lp))
Angel Ponsbf9bc502020-06-08 00:12:43 +0200295 pci_or_config8(dev, 0xe1, 0x3c);
Aaron Durbin1c4289d2013-06-21 14:06:11 -0500296 }
297
Tristan Corrick05b75242018-12-06 22:47:42 +1300298 if (!enabled_ports && is_lp && rpc.ports[0])
Angel Ponsbf9bc502020-06-08 00:12:43 +0200299 pci_or_config8(rpc.ports[0], 0xe1, 1 << 6);
Aaron Durbin1c4289d2013-06-21 14:06:11 -0500300}
301
Aaron Durbinc0254e62013-06-20 01:20:30 -0500302static void root_port_commit_config(void)
Aaron Durbin60f82082013-06-19 13:28:04 -0500303{
Aaron Durbinc0254e62013-06-20 01:20:30 -0500304 int i;
Aaron Durbin60f82082013-06-19 13:28:04 -0500305
Aaron Durbinc0254e62013-06-20 01:20:30 -0500306 /* If the first root port is disabled the coalesce ports. */
Tristan Corrick05b75242018-12-06 22:47:42 +1300307 if (!is_rp_enabled(1))
Aaron Durbinc0254e62013-06-20 01:20:30 -0500308 rpc.coalesce = 1;
Aaron Durbin60f82082013-06-19 13:28:04 -0500309
Aaron Durbin1c4289d2013-06-21 14:06:11 -0500310 /* Perform clock gating configuration. */
311 pcie_enable_clock_gating();
312
Aaron Durbinc0254e62013-06-20 01:20:30 -0500313 for (i = 0; i < rpc.num_ports; i++) {
Elyes HAOUAS7a5f7712018-06-08 17:20:38 +0200314 struct device *dev;
Aaron Durbin60f82082013-06-19 13:28:04 -0500315
Aaron Durbinc0254e62013-06-20 01:20:30 -0500316 dev = rpc.ports[i];
Aaron Durbin60f82082013-06-19 13:28:04 -0500317
Aaron Durbinc0254e62013-06-20 01:20:30 -0500318 if (dev == NULL) {
319 printk(BIOS_ERR, "Root Port %d device is NULL?\n", i+1);
320 continue;
Aaron Durbin60f82082013-06-19 13:28:04 -0500321 }
322
Aaron Durbinc0254e62013-06-20 01:20:30 -0500323 if (dev->enabled)
324 continue;
325
326 printk(BIOS_DEBUG, "%s: Disabling device\n", dev_path(dev));
327
Aaron Durbin60f82082013-06-19 13:28:04 -0500328 /* Ensure memory, io, and bus master are all disabled */
Angel Ponsbf9bc502020-06-08 00:12:43 +0200329 pci_and_config16(dev, PCI_COMMAND,
330 ~(PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY | PCI_COMMAND_IO));
Aaron Durbin60f82082013-06-19 13:28:04 -0500331
Aaron Durbin60f82082013-06-19 13:28:04 -0500332 /* Disable this device if possible */
333 pch_disable_devfn(dev);
Aaron Durbin60f82082013-06-19 13:28:04 -0500334 }
335
Aaron Durbinc0254e62013-06-20 01:20:30 -0500336 if (rpc.coalesce) {
337 int current_func;
Aaron Durbin60f82082013-06-19 13:28:04 -0500338
Aaron Durbinc0254e62013-06-20 01:20:30 -0500339 /* For all Root Ports N enabled ports get assigned the lower
340 * PCI function number. The disabled ones get upper PCI
341 * function numbers. */
342 current_func = 0;
343 for (i = 0; i < rpc.num_ports; i++) {
Tristan Corrick05b75242018-12-06 22:47:42 +1300344 if (!is_rp_enabled(i + 1))
Aaron Durbinc0254e62013-06-20 01:20:30 -0500345 continue;
346 pch_pcie_device_set_func(i, current_func);
347 current_func++;
348 }
349
350 /* Allocate the disabled devices' PCI function number. */
351 for (i = 0; i < rpc.num_ports; i++) {
Tristan Corrick05b75242018-12-06 22:47:42 +1300352 if (is_rp_enabled(i + 1))
Aaron Durbinc0254e62013-06-20 01:20:30 -0500353 continue;
354 pch_pcie_device_set_func(i, current_func);
355 current_func++;
356 }
357 }
358
359 printk(BIOS_SPEW, "PCH: RPFN 0x%08x -> 0x%08x\n",
360 rpc.orig_rpfn, rpc.new_rpfn);
361 RCBA32(RPFN) = rpc.new_rpfn;
362}
363
Elyes HAOUAS7a5f7712018-06-08 17:20:38 +0200364static void root_port_mark_disable(struct device *dev)
Aaron Durbinc0254e62013-06-20 01:20:30 -0500365{
366 /* Mark device as disabled. */
367 dev->enabled = 0;
368 /* Mark device to be hidden. */
369 rpc.new_rpfn |= RPFN_HIDE(PCI_FUNC(dev->path.pci.devfn));
370}
371
Elyes HAOUAS7a5f7712018-06-08 17:20:38 +0200372static void root_port_check_disable(struct device *dev)
Aaron Durbinc0254e62013-06-20 01:20:30 -0500373{
374 int rp;
375 int is_lp;
376
377 /* Device already disabled. */
378 if (!dev->enabled) {
379 root_port_mark_disable(dev);
380 return;
381 }
382
383 rp = root_port_number(dev);
384
385 /* Is the GbE port mapped to this Root Port? */
386 if (rp == rpc.gbe_port) {
387 root_port_mark_disable(dev);
388 return;
389 }
390
391 is_lp = pch_is_lp();
392
393 /* Check Root Port Configuration. */
394 switch (rp) {
395 case 2:
396 /* Root Port 2 is disabled for all lane configurations
397 * but config 00b (4x1 links). */
398 if ((rpc.strpfusecfg1 >> 14) & 0x3) {
399 root_port_mark_disable(dev);
400 return;
401 }
402 break;
403 case 3:
404 /* Root Port 3 is disabled in config 11b (1x4 links). */
405 if (((rpc.strpfusecfg1 >> 14) & 0x3) == 0x3) {
406 root_port_mark_disable(dev);
407 return;
408 }
409 break;
410 case 4:
411 /* Root Port 4 is disabled in configs 11b (1x4 links)
412 * and 10b (2x2 links). */
413 if ((rpc.strpfusecfg1 >> 14) & 0x2) {
414 root_port_mark_disable(dev);
415 return;
416 }
417 break;
418 case 6:
419 if (is_lp)
420 break;
421 /* Root Port 6 is disabled for all lane configurations
422 * but config 00b (4x1 links). */
423 if ((rpc.strpfusecfg2 >> 14) & 0x3) {
424 root_port_mark_disable(dev);
425 return;
426 }
427 break;
428 case 7:
429 if (is_lp)
430 break;
Tristan Corrickbaa4c072018-12-06 22:47:21 +1300431 /* Root Port 7 is disabled in config 11b (1x4 links). */
Aaron Durbinc0254e62013-06-20 01:20:30 -0500432 if (((rpc.strpfusecfg2 >> 14) & 0x3) == 0x3) {
433 root_port_mark_disable(dev);
434 return;
435 }
436 break;
437 case 8:
438 if (is_lp)
439 break;
440 /* Root Port 8 is disabled in configs 11b (1x4 links)
441 * and 10b (2x2 links). */
442 if ((rpc.strpfusecfg2 >> 14) & 0x2) {
443 root_port_mark_disable(dev);
444 return;
445 }
446 break;
447 }
448
449 /* Check Pin Ownership. */
450 if (is_lp) {
451 switch (rp) {
452 case 1:
453 /* Bit 0 is Root Port 1 ownership. */
454 if ((rpc.pin_ownership & 0x1) == 0) {
455 root_port_mark_disable(dev);
456 return;
457 }
458 break;
459 case 2:
460 /* Bit 2 is Root Port 2 ownership. */
461 if ((rpc.pin_ownership & 0x4) == 0) {
462 root_port_mark_disable(dev);
463 return;
464 }
465 break;
466 case 6:
467 /* Bits 7:4 are Root Port 6 pin-lane ownership. */
468 if ((rpc.pin_ownership & 0xf0) == 0) {
469 root_port_mark_disable(dev);
470 return;
471 }
472 break;
473 }
474 } else {
475 switch (rp) {
476 case 1:
477 /* Bits 4 and 0 are Root Port 1 ownership. */
478 if ((rpc.pin_ownership & 0x11) == 0) {
479 root_port_mark_disable(dev);
480 return;
481 }
482 break;
483 case 2:
484 /* Bits 5 and 2 are Root Port 2 ownership. */
485 if ((rpc.pin_ownership & 0x24) == 0) {
486 root_port_mark_disable(dev);
487 return;
488 }
489 break;
490 }
Aaron Durbin60f82082013-06-19 13:28:04 -0500491 }
492}
493
Stefan Reinauerab365af2013-12-03 12:13:26 -0800494static void pcie_add_0x0202000_iobp(u32 reg)
495{
496 u32 reg32;
Aaron Durbin76c37002012-10-30 09:03:43 -0500497
Stefan Reinauerab365af2013-12-03 12:13:26 -0800498 reg32 = pch_iobp_read(reg);
499 reg32 += (0x2 << 16) | (0x2 << 8);
500 pch_iobp_write(reg, reg32);
501}
Aaron Durbin76c37002012-10-30 09:03:43 -0500502
Stefan Reinauerab365af2013-12-03 12:13:26 -0800503static void pch_pcie_early(struct device *dev)
504{
505 int rp;
506 int do_aspm;
507 int is_lp;
Duncan Laurie249a03b2013-08-09 09:06:41 -0700508 struct southbridge_intel_lynxpoint_config *config = dev->chip_info;
Aaron Durbin76c37002012-10-30 09:03:43 -0500509
Stefan Reinauerab365af2013-12-03 12:13:26 -0800510 rp = root_port_number(dev);
511 do_aspm = 0;
512 is_lp = pch_is_lp();
Aaron Durbin76c37002012-10-30 09:03:43 -0500513
Stefan Reinauerab365af2013-12-03 12:13:26 -0800514 if (is_lp) {
515 switch (rp) {
516 case 1:
517 case 2:
518 case 3:
519 case 4:
520 /* Bits 31:28 of b0d28f0 0x32c register correspnd to
521 * Root Ports 4:1. */
522 do_aspm = !!(rpc.b0d28f0_32c & (1 << (28 + rp - 1)));
523 break;
524 case 5:
525 /* Bit 28 of b0d28f4 0x32c register correspnd to
526 * Root Ports 4:1. */
527 do_aspm = !!(rpc.b0d28f4_32c & (1 << 28));
528 break;
529 case 6:
530 /* Bit 28 of b0d28f5 0x32c register correspnd to
531 * Root Ports 4:1. */
532 do_aspm = !!(rpc.b0d28f5_32c & (1 << 28));
533 break;
534 }
535 } else {
536 switch (rp) {
537 case 1:
538 case 2:
539 case 3:
540 case 4:
541 /* Bits 31:28 of b0d28f0 0x32c register correspnd to
542 * Root Ports 4:1. */
543 do_aspm = !!(rpc.b0d28f0_32c & (1 << (28 + rp - 1)));
544 break;
545 case 5:
546 case 6:
547 case 7:
548 case 8:
549 /* Bit 31:28 of b0d28f4 0x32c register correspnd to
550 * Root Ports 8:5. */
551 do_aspm = !!(rpc.b0d28f4_32c & (1 << (28 + rp - 5)));
552 break;
553 }
Aaron Durbin76c37002012-10-30 09:03:43 -0500554 }
Stefan Reinauerab365af2013-12-03 12:13:26 -0800555
Duncan Laurie249a03b2013-08-09 09:06:41 -0700556 /* Allow ASPM to be forced on in devicetree */
557 if (config && (config->pcie_port_force_aspm & (1 << (rp - 1))))
558 do_aspm = 1;
559
560 printk(BIOS_DEBUG, "PCIe Root Port %d ASPM is %sabled\n",
561 rp, do_aspm ? "en" : "dis");
562
Stefan Reinauerab365af2013-12-03 12:13:26 -0800563 if (do_aspm) {
564 /* Set ASPM bits in MPC2 register. */
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300565 pci_update_config32(dev, 0xd4, ~(0x3 << 2), (1 << 4) | (0x2 << 2));
Stefan Reinauerab365af2013-12-03 12:13:26 -0800566
567 /* Set unique clock exit latency in MPC register. */
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300568 pci_update_config32(dev, 0xd8, ~(0x7 << 18), (0x7 << 18));
Stefan Reinauerab365af2013-12-03 12:13:26 -0800569
570 /* Set L1 exit latency in LCAP register. */
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300571 pci_update_config32(dev, 0x4c, ~(0x7 << 15), (0x4 << 15));
Stefan Reinauerab365af2013-12-03 12:13:26 -0800572
573 if (is_lp) {
574 switch (rp) {
575 case 1:
576 pcie_add_0x0202000_iobp(0xe9002440);
577 break;
578 case 2:
579 pcie_add_0x0202000_iobp(0xe9002640);
580 break;
581 case 3:
582 pcie_add_0x0202000_iobp(0xe9000840);
583 break;
584 case 4:
585 pcie_add_0x0202000_iobp(0xe9000a40);
586 break;
587 case 5:
588 pcie_add_0x0202000_iobp(0xe9000c40);
589 pcie_add_0x0202000_iobp(0xe9000e40);
590 pcie_add_0x0202000_iobp(0xe9001040);
591 pcie_add_0x0202000_iobp(0xe9001240);
592 break;
593 case 6:
594 /* Update IOBP based on lane ownership. */
595 if (rpc.pin_ownership & (1 << 4))
596 pcie_add_0x0202000_iobp(0xea002040);
597 if (rpc.pin_ownership & (1 << 5))
598 pcie_add_0x0202000_iobp(0xea002240);
599 if (rpc.pin_ownership & (1 << 6))
600 pcie_add_0x0202000_iobp(0xea002440);
601 if (rpc.pin_ownership & (1 << 7))
602 pcie_add_0x0202000_iobp(0xea002640);
603 break;
604 }
605 } else {
606 switch (rp) {
607 case 1:
608 if ((rpc.pin_ownership & 0x3) == 1)
609 pcie_add_0x0202000_iobp(0xe9002e40);
610 else
611 pcie_add_0x0202000_iobp(0xea002040);
612 break;
613 case 2:
614 if ((rpc.pin_ownership & 0xc) == 0x4)
615 pcie_add_0x0202000_iobp(0xe9002c40);
616 else
617 pcie_add_0x0202000_iobp(0xea002240);
618 break;
619 case 3:
620 pcie_add_0x0202000_iobp(0xe9002a40);
621 break;
622 case 4:
623 pcie_add_0x0202000_iobp(0xe9002840);
624 break;
625 case 5:
626 pcie_add_0x0202000_iobp(0xe9002640);
627 break;
628 case 6:
629 pcie_add_0x0202000_iobp(0xe9002440);
630 break;
631 case 7:
632 pcie_add_0x0202000_iobp(0xe9002240);
633 break;
634 case 8:
635 pcie_add_0x0202000_iobp(0xe9002040);
636 break;
637 }
638 }
639
Angel Ponsbf9bc502020-06-08 00:12:43 +0200640 pci_and_config32(dev, 0x338, ~(1 << 26));
Stefan Reinauerab365af2013-12-03 12:13:26 -0800641 }
642
643 /* Enable LTR in Root Port. */
Angel Ponsbf9bc502020-06-08 00:12:43 +0200644 pci_or_config32(dev, 0x64, 1 << 11);
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300645 pci_update_config32(dev, 0x68, ~(1 << 10), (1 << 10));
Stefan Reinauerab365af2013-12-03 12:13:26 -0800646
Ryan Salsamendi0d9b3602017-06-30 17:15:57 -0700647 pci_update_config32(dev, 0x318, ~(0xffffUL << 16), (0x1414UL << 16));
Stefan Reinauerab365af2013-12-03 12:13:26 -0800648
649 /* Set L1 exit latency in LCAP register. */
650 if (!do_aspm && (pci_read_config8(dev, 0xf5) & 0x1))
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300651 pci_update_config32(dev, 0x4c, ~(0x7 << 15), (0x4 << 15));
Stefan Reinauerab365af2013-12-03 12:13:26 -0800652 else
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300653 pci_update_config32(dev, 0x4c, ~(0x7 << 15), (0x2 << 15));
Stefan Reinauerab365af2013-12-03 12:13:26 -0800654
Angel Ponsbf9bc502020-06-08 00:12:43 +0200655 pci_update_config32(dev, 0x314, 0, 0x743a361b);
Stefan Reinauerab365af2013-12-03 12:13:26 -0800656
657 /* Set Common Clock Exit Latency in MPC register. */
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300658 pci_update_config32(dev, 0xd8, ~(0x7 << 15), (0x3 << 15));
Stefan Reinauerab365af2013-12-03 12:13:26 -0800659
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300660 pci_update_config32(dev, 0x33c, ~0x00ffffff, 0x854c74);
Stefan Reinauerab365af2013-12-03 12:13:26 -0800661
Matt DeVillier1aeccd12020-10-07 13:18:55 -0500662 /* Set Invalid Receive Range Check Enable in MPC register. */
Angel Ponsbf9bc502020-06-08 00:12:43 +0200663 pci_or_config32(dev, 0xd8, 1 << 25);
Stefan Reinauerab365af2013-12-03 12:13:26 -0800664
Angel Ponsbf9bc502020-06-08 00:12:43 +0200665 pci_and_config8(dev, 0xf5, 0x3f);
Stefan Reinauerab365af2013-12-03 12:13:26 -0800666
667 if (rp == 1 || rp == 5 || (is_lp && rp == 6))
Angel Ponsbf9bc502020-06-08 00:12:43 +0200668 pci_and_config8(dev, 0xf7, ~0x0c);
Stefan Reinauerab365af2013-12-03 12:13:26 -0800669
670 /* Set EOI forwarding disable. */
Angel Ponsbf9bc502020-06-08 00:12:43 +0200671 pci_or_config32(dev, 0xd4, 1 << 1);
Stefan Reinauerab365af2013-12-03 12:13:26 -0800672
Matt DeVillier7f633532020-10-07 13:11:58 -0500673 /* Set AER Extended Cap ID to 01h and Next Cap Pointer to 200h. */
674 if (CONFIG(PCIEXP_AER))
675 pci_update_config32(dev, 0x100, ~0xfffff, (1 << 29) | 0x10001);
676 else
677 pci_update_config32(dev, 0x100, ~0xfffff, (1 << 29));
Stefan Reinauerab365af2013-12-03 12:13:26 -0800678
Matt DeVillier54e1f592020-10-07 13:17:09 -0500679 /* Set L1 Sub-State Cap ID to 1Eh and Next Cap Pointer to None. */
680 if (CONFIG(PCIEXP_L1_SUB_STATE))
681 pci_update_config32(dev, 0x200, ~0xfffff, 0x001e);
682 else
683 pci_update_config32(dev, 0x200, ~0xfffff, 0);
684
Stefan Reinauerab365af2013-12-03 12:13:26 -0800685 if (is_lp)
Angel Ponsbf9bc502020-06-08 00:12:43 +0200686 pci_or_config32(dev, 0x100, 1 << 29);
Stefan Reinauerab365af2013-12-03 12:13:26 -0800687
688 /* Read and write back write-once capability registers. */
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300689 pci_update_config32(dev, 0x34, ~0, 0);
690 pci_update_config32(dev, 0x40, ~0, 0);
691 pci_update_config32(dev, 0x80, ~0, 0);
692 pci_update_config32(dev, 0x90, ~0, 0);
Aaron Durbin76c37002012-10-30 09:03:43 -0500693}
694
695static void pci_init(struct device *dev)
696{
Aaron Durbin76c37002012-10-30 09:03:43 -0500697 printk(BIOS_DEBUG, "Initializing PCH PCIe bridge.\n");
698
Aaron Durbinc0254e62013-06-20 01:20:30 -0500699 /* Enable SERR */
Elyes HAOUAS73ae0762020-04-28 10:13:05 +0200700 pci_or_config16(dev, PCI_COMMAND, PCI_COMMAND_SERR);
Aaron Durbinc0254e62013-06-20 01:20:30 -0500701
Aaron Durbin76c37002012-10-30 09:03:43 -0500702 /* Enable Bus Master */
Elyes HAOUAS73ae0762020-04-28 10:13:05 +0200703 pci_or_config16(dev, PCI_COMMAND, PCI_COMMAND_MASTER);
Aaron Durbin76c37002012-10-30 09:03:43 -0500704
705 /* Set Cache Line Size to 0x10 */
706 // This has no effect but the OS might expect it
707 pci_write_config8(dev, 0x0c, 0x10);
708
Angel Ponsbf9bc502020-06-08 00:12:43 +0200709 pci_and_config16(dev, PCI_BRIDGE_CONTROL, ~PCI_BRIDGE_CTL_PARITY);
Aaron Durbin76c37002012-10-30 09:03:43 -0500710
Aaron Durbin76c37002012-10-30 09:03:43 -0500711 /* Clear errors in status registers */
Angel Ponsbf9bc502020-06-08 00:12:43 +0200712 pci_update_config16(dev, 0x06, ~0, 0);
713 pci_update_config16(dev, 0x1e, ~0, 0);
Aaron Durbin76c37002012-10-30 09:03:43 -0500714}
715
Elyes HAOUAS7a5f7712018-06-08 17:20:38 +0200716static void pch_pcie_enable(struct device *dev)
Aaron Durbin76c37002012-10-30 09:03:43 -0500717{
Aaron Durbinc0254e62013-06-20 01:20:30 -0500718 /* Add this device to the root port config structure. */
719 root_port_init_config(dev);
720
721 /* Check to see if this Root Port should be disabled. */
722 root_port_check_disable(dev);
723
Aaron Durbin76c37002012-10-30 09:03:43 -0500724 /* Power Management init before enumeration */
Aaron Durbinc0254e62013-06-20 01:20:30 -0500725 if (dev->enabled)
Stefan Reinauerab365af2013-12-03 12:13:26 -0800726 pch_pcie_early(dev);
Aaron Durbinc0254e62013-06-20 01:20:30 -0500727
728 /*
729 * When processing the last PCIe root port we can now
730 * update the Root Port Function Number and Hide register.
731 */
732 if (root_port_is_last(dev))
733 root_port_commit_config();
Aaron Durbin76c37002012-10-30 09:03:43 -0500734}
735
Aaron Durbin76c37002012-10-30 09:03:43 -0500736static struct device_operations device_ops = {
737 .read_resources = pci_bus_read_resources,
738 .set_resources = pci_dev_set_resources,
739 .enable_resources = pci_bus_enable_resources,
740 .init = pci_init,
741 .enable = pch_pcie_enable,
742 .scan_bus = pciexp_scan_bridge,
Angel Pons1fc0edd2020-05-31 00:03:28 +0200743 .ops_pci = &pci_dev_ops_pci,
Aaron Durbin76c37002012-10-30 09:03:43 -0500744};
745
Duncan Laurie74c0d052012-12-17 11:31:40 -0800746static const unsigned short pci_device_ids[] = {
747 /* Lynxpoint Mobile */
748 0x8c10, 0x8c12, 0x8c14, 0x8c16, 0x8c18, 0x8c1a, 0x8c1c, 0x8c1e,
749 /* Lynxpoint Low Power */
750 0x9c10, 0x9c12, 0x9c14, 0x9c16, 0x9c18, 0x9c1a,
751 0
752};
Aaron Durbin76c37002012-10-30 09:03:43 -0500753
754static const struct pci_driver pch_pcie __pci_driver = {
755 .ops = &device_ops,
756 .vendor = PCI_VENDOR_ID_INTEL,
757 .devices = pci_device_ids,
758};