blob: 112c9d3fa31d5c50b22bd55865e01ae47bf31b55 [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{
Duncan Laurie249a03b2013-08-09 09:06:41 -0700505 struct southbridge_intel_lynxpoint_config *config = dev->chip_info;
Angel Pons2aaf7c02020-09-24 18:03:18 +0200506 int do_aspm = 0;
507 int rp = root_port_number(dev);
508 int is_lp = pch_is_lp();
Aaron Durbin76c37002012-10-30 09:03:43 -0500509
Stefan Reinauerab365af2013-12-03 12:13:26 -0800510 if (is_lp) {
511 switch (rp) {
512 case 1:
513 case 2:
514 case 3:
515 case 4:
Angel Pons2aaf7c02020-09-24 18:03:18 +0200516 /*
517 * Bits 31:28 of b0d28f0 0x32c register correspond to
518 * Root Ports 4:1.
519 */
Stefan Reinauerab365af2013-12-03 12:13:26 -0800520 do_aspm = !!(rpc.b0d28f0_32c & (1 << (28 + rp - 1)));
521 break;
522 case 5:
Angel Pons2aaf7c02020-09-24 18:03:18 +0200523 /*
524 * Bit 28 of b0d28f4 0x32c register correspond to
525 * Root Ports 4:1.
526 */
Stefan Reinauerab365af2013-12-03 12:13:26 -0800527 do_aspm = !!(rpc.b0d28f4_32c & (1 << 28));
528 break;
529 case 6:
Angel Pons2aaf7c02020-09-24 18:03:18 +0200530 /*
531 * Bit 28 of b0d28f5 0x32c register correspond to
532 * Root Ports 4:1.
533 */
Stefan Reinauerab365af2013-12-03 12:13:26 -0800534 do_aspm = !!(rpc.b0d28f5_32c & (1 << 28));
535 break;
536 }
537 } else {
538 switch (rp) {
539 case 1:
540 case 2:
541 case 3:
542 case 4:
Angel Pons2aaf7c02020-09-24 18:03:18 +0200543 /*
544 * Bits 31:28 of b0d28f0 0x32c register correspond to
545 * Root Ports 4:1.
546 */
Stefan Reinauerab365af2013-12-03 12:13:26 -0800547 do_aspm = !!(rpc.b0d28f0_32c & (1 << (28 + rp - 1)));
548 break;
549 case 5:
550 case 6:
551 case 7:
552 case 8:
Angel Pons2aaf7c02020-09-24 18:03:18 +0200553 /*
554 * Bits 31:28 of b0d28f4 0x32c register correspond to
555 * Root Ports 8:5.
556 */
Stefan Reinauerab365af2013-12-03 12:13:26 -0800557 do_aspm = !!(rpc.b0d28f4_32c & (1 << (28 + rp - 5)));
558 break;
559 }
Aaron Durbin76c37002012-10-30 09:03:43 -0500560 }
Stefan Reinauerab365af2013-12-03 12:13:26 -0800561
Duncan Laurie249a03b2013-08-09 09:06:41 -0700562 /* Allow ASPM to be forced on in devicetree */
563 if (config && (config->pcie_port_force_aspm & (1 << (rp - 1))))
564 do_aspm = 1;
565
566 printk(BIOS_DEBUG, "PCIe Root Port %d ASPM is %sabled\n",
567 rp, do_aspm ? "en" : "dis");
568
Stefan Reinauerab365af2013-12-03 12:13:26 -0800569 if (do_aspm) {
570 /* Set ASPM bits in MPC2 register. */
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300571 pci_update_config32(dev, 0xd4, ~(0x3 << 2), (1 << 4) | (0x2 << 2));
Stefan Reinauerab365af2013-12-03 12:13:26 -0800572
573 /* Set unique clock exit latency in MPC register. */
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300574 pci_update_config32(dev, 0xd8, ~(0x7 << 18), (0x7 << 18));
Stefan Reinauerab365af2013-12-03 12:13:26 -0800575
576 /* Set L1 exit latency in LCAP register. */
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300577 pci_update_config32(dev, 0x4c, ~(0x7 << 15), (0x4 << 15));
Stefan Reinauerab365af2013-12-03 12:13:26 -0800578
579 if (is_lp) {
580 switch (rp) {
581 case 1:
582 pcie_add_0x0202000_iobp(0xe9002440);
583 break;
584 case 2:
585 pcie_add_0x0202000_iobp(0xe9002640);
586 break;
587 case 3:
588 pcie_add_0x0202000_iobp(0xe9000840);
589 break;
590 case 4:
591 pcie_add_0x0202000_iobp(0xe9000a40);
592 break;
593 case 5:
594 pcie_add_0x0202000_iobp(0xe9000c40);
595 pcie_add_0x0202000_iobp(0xe9000e40);
596 pcie_add_0x0202000_iobp(0xe9001040);
597 pcie_add_0x0202000_iobp(0xe9001240);
598 break;
599 case 6:
600 /* Update IOBP based on lane ownership. */
601 if (rpc.pin_ownership & (1 << 4))
602 pcie_add_0x0202000_iobp(0xea002040);
603 if (rpc.pin_ownership & (1 << 5))
604 pcie_add_0x0202000_iobp(0xea002240);
605 if (rpc.pin_ownership & (1 << 6))
606 pcie_add_0x0202000_iobp(0xea002440);
607 if (rpc.pin_ownership & (1 << 7))
608 pcie_add_0x0202000_iobp(0xea002640);
609 break;
610 }
611 } else {
612 switch (rp) {
613 case 1:
614 if ((rpc.pin_ownership & 0x3) == 1)
615 pcie_add_0x0202000_iobp(0xe9002e40);
616 else
617 pcie_add_0x0202000_iobp(0xea002040);
618 break;
619 case 2:
620 if ((rpc.pin_ownership & 0xc) == 0x4)
621 pcie_add_0x0202000_iobp(0xe9002c40);
622 else
623 pcie_add_0x0202000_iobp(0xea002240);
624 break;
625 case 3:
626 pcie_add_0x0202000_iobp(0xe9002a40);
627 break;
628 case 4:
629 pcie_add_0x0202000_iobp(0xe9002840);
630 break;
631 case 5:
632 pcie_add_0x0202000_iobp(0xe9002640);
633 break;
634 case 6:
635 pcie_add_0x0202000_iobp(0xe9002440);
636 break;
637 case 7:
638 pcie_add_0x0202000_iobp(0xe9002240);
639 break;
640 case 8:
641 pcie_add_0x0202000_iobp(0xe9002040);
642 break;
643 }
644 }
645
Angel Ponsbf9bc502020-06-08 00:12:43 +0200646 pci_and_config32(dev, 0x338, ~(1 << 26));
Stefan Reinauerab365af2013-12-03 12:13:26 -0800647 }
648
649 /* Enable LTR in Root Port. */
Angel Ponsbf9bc502020-06-08 00:12:43 +0200650 pci_or_config32(dev, 0x64, 1 << 11);
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300651 pci_update_config32(dev, 0x68, ~(1 << 10), (1 << 10));
Stefan Reinauerab365af2013-12-03 12:13:26 -0800652
Angel Pons2aaf7c02020-09-24 18:03:18 +0200653 pci_update_config32(dev, 0x318, ~(0xffff << 16), (0x1414 << 16));
Stefan Reinauerab365af2013-12-03 12:13:26 -0800654
655 /* Set L1 exit latency in LCAP register. */
656 if (!do_aspm && (pci_read_config8(dev, 0xf5) & 0x1))
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300657 pci_update_config32(dev, 0x4c, ~(0x7 << 15), (0x4 << 15));
Stefan Reinauerab365af2013-12-03 12:13:26 -0800658 else
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300659 pci_update_config32(dev, 0x4c, ~(0x7 << 15), (0x2 << 15));
Stefan Reinauerab365af2013-12-03 12:13:26 -0800660
Angel Ponsbf9bc502020-06-08 00:12:43 +0200661 pci_update_config32(dev, 0x314, 0, 0x743a361b);
Stefan Reinauerab365af2013-12-03 12:13:26 -0800662
663 /* Set Common Clock Exit Latency in MPC register. */
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300664 pci_update_config32(dev, 0xd8, ~(0x7 << 15), (0x3 << 15));
Stefan Reinauerab365af2013-12-03 12:13:26 -0800665
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300666 pci_update_config32(dev, 0x33c, ~0x00ffffff, 0x854c74);
Stefan Reinauerab365af2013-12-03 12:13:26 -0800667
Matt DeVillier1aeccd12020-10-07 13:18:55 -0500668 /* Set Invalid Receive Range Check Enable in MPC register. */
Angel Ponsbf9bc502020-06-08 00:12:43 +0200669 pci_or_config32(dev, 0xd8, 1 << 25);
Stefan Reinauerab365af2013-12-03 12:13:26 -0800670
Angel Ponsbf9bc502020-06-08 00:12:43 +0200671 pci_and_config8(dev, 0xf5, 0x3f);
Stefan Reinauerab365af2013-12-03 12:13:26 -0800672
673 if (rp == 1 || rp == 5 || (is_lp && rp == 6))
Angel Ponsbf9bc502020-06-08 00:12:43 +0200674 pci_and_config8(dev, 0xf7, ~0x0c);
Stefan Reinauerab365af2013-12-03 12:13:26 -0800675
676 /* Set EOI forwarding disable. */
Angel Ponsbf9bc502020-06-08 00:12:43 +0200677 pci_or_config32(dev, 0xd4, 1 << 1);
Stefan Reinauerab365af2013-12-03 12:13:26 -0800678
Matt DeVillier7f633532020-10-07 13:11:58 -0500679 /* Set AER Extended Cap ID to 01h and Next Cap Pointer to 200h. */
680 if (CONFIG(PCIEXP_AER))
681 pci_update_config32(dev, 0x100, ~0xfffff, (1 << 29) | 0x10001);
682 else
683 pci_update_config32(dev, 0x100, ~0xfffff, (1 << 29));
Stefan Reinauerab365af2013-12-03 12:13:26 -0800684
Matt DeVillier54e1f592020-10-07 13:17:09 -0500685 /* Set L1 Sub-State Cap ID to 1Eh and Next Cap Pointer to None. */
686 if (CONFIG(PCIEXP_L1_SUB_STATE))
687 pci_update_config32(dev, 0x200, ~0xfffff, 0x001e);
688 else
689 pci_update_config32(dev, 0x200, ~0xfffff, 0);
690
Stefan Reinauerab365af2013-12-03 12:13:26 -0800691 if (is_lp)
Angel Ponsbf9bc502020-06-08 00:12:43 +0200692 pci_or_config32(dev, 0x100, 1 << 29);
Stefan Reinauerab365af2013-12-03 12:13:26 -0800693
694 /* Read and write back write-once capability registers. */
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300695 pci_update_config32(dev, 0x34, ~0, 0);
696 pci_update_config32(dev, 0x40, ~0, 0);
697 pci_update_config32(dev, 0x80, ~0, 0);
698 pci_update_config32(dev, 0x90, ~0, 0);
Aaron Durbin76c37002012-10-30 09:03:43 -0500699}
700
Angel Pons2aaf7c02020-09-24 18:03:18 +0200701static void pch_pcie_init(struct device *dev)
Aaron Durbin76c37002012-10-30 09:03:43 -0500702{
Aaron Durbin76c37002012-10-30 09:03:43 -0500703 printk(BIOS_DEBUG, "Initializing PCH PCIe bridge.\n");
704
Aaron Durbinc0254e62013-06-20 01:20:30 -0500705 /* Enable SERR */
Elyes HAOUAS73ae0762020-04-28 10:13:05 +0200706 pci_or_config16(dev, PCI_COMMAND, PCI_COMMAND_SERR);
Aaron Durbinc0254e62013-06-20 01:20:30 -0500707
Aaron Durbin76c37002012-10-30 09:03:43 -0500708 /* Enable Bus Master */
Elyes HAOUAS73ae0762020-04-28 10:13:05 +0200709 pci_or_config16(dev, PCI_COMMAND, PCI_COMMAND_MASTER);
Aaron Durbin76c37002012-10-30 09:03:43 -0500710
711 /* Set Cache Line Size to 0x10 */
712 // This has no effect but the OS might expect it
713 pci_write_config8(dev, 0x0c, 0x10);
714
Angel Ponsbf9bc502020-06-08 00:12:43 +0200715 pci_and_config16(dev, PCI_BRIDGE_CONTROL, ~PCI_BRIDGE_CTL_PARITY);
Aaron Durbin76c37002012-10-30 09:03:43 -0500716
Aaron Durbin76c37002012-10-30 09:03:43 -0500717 /* Clear errors in status registers */
Angel Ponsbf9bc502020-06-08 00:12:43 +0200718 pci_update_config16(dev, 0x06, ~0, 0);
719 pci_update_config16(dev, 0x1e, ~0, 0);
Aaron Durbin76c37002012-10-30 09:03:43 -0500720}
721
Elyes HAOUAS7a5f7712018-06-08 17:20:38 +0200722static void pch_pcie_enable(struct device *dev)
Aaron Durbin76c37002012-10-30 09:03:43 -0500723{
Aaron Durbinc0254e62013-06-20 01:20:30 -0500724 /* Add this device to the root port config structure. */
725 root_port_init_config(dev);
726
727 /* Check to see if this Root Port should be disabled. */
728 root_port_check_disable(dev);
729
Aaron Durbin76c37002012-10-30 09:03:43 -0500730 /* Power Management init before enumeration */
Aaron Durbinc0254e62013-06-20 01:20:30 -0500731 if (dev->enabled)
Stefan Reinauerab365af2013-12-03 12:13:26 -0800732 pch_pcie_early(dev);
Aaron Durbinc0254e62013-06-20 01:20:30 -0500733
734 /*
735 * When processing the last PCIe root port we can now
736 * update the Root Port Function Number and Hide register.
737 */
738 if (root_port_is_last(dev))
739 root_port_commit_config();
Aaron Durbin76c37002012-10-30 09:03:43 -0500740}
741
Aaron Durbin76c37002012-10-30 09:03:43 -0500742static struct device_operations device_ops = {
743 .read_resources = pci_bus_read_resources,
744 .set_resources = pci_dev_set_resources,
745 .enable_resources = pci_bus_enable_resources,
Angel Pons2aaf7c02020-09-24 18:03:18 +0200746 .init = pch_pcie_init,
Aaron Durbin76c37002012-10-30 09:03:43 -0500747 .enable = pch_pcie_enable,
748 .scan_bus = pciexp_scan_bridge,
Angel Pons1fc0edd2020-05-31 00:03:28 +0200749 .ops_pci = &pci_dev_ops_pci,
Aaron Durbin76c37002012-10-30 09:03:43 -0500750};
751
Duncan Laurie74c0d052012-12-17 11:31:40 -0800752static const unsigned short pci_device_ids[] = {
753 /* Lynxpoint Mobile */
754 0x8c10, 0x8c12, 0x8c14, 0x8c16, 0x8c18, 0x8c1a, 0x8c1c, 0x8c1e,
755 /* Lynxpoint Low Power */
756 0x9c10, 0x9c12, 0x9c14, 0x9c16, 0x9c18, 0x9c1a,
757 0
758};
Aaron Durbin76c37002012-10-30 09:03:43 -0500759
760static const struct pci_driver pch_pcie __pci_driver = {
761 .ops = &device_ops,
762 .vendor = PCI_VENDOR_ID_INTEL,
763 .devices = pci_device_ids,
764};