blob: 4e94f121c0b1d7b13a7da43e628fc2b29bbdc845 [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>
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 <stddef.h>
15#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 static const uint32_t high_bit = (1UL << 31);
236
Aaron Durbin1c4289d2013-06-21 14:06:11 -0500237 /* Configure shared resource clock gating. */
238 if (rp == 1 || rp == 5 || (rp == 6 && is_lp))
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300239 pci_update_config8(dev, 0xe1, 0xc3, 0x3c);
Aaron Durbin1c4289d2013-06-21 14:06:11 -0500240
241 if (!is_lp) {
Tristan Corrick05b75242018-12-06 22:47:42 +1300242 if (rp == 1 && !is_rp_enabled(2) &&
243 !is_rp_enabled(3) && !is_rp_enabled(4)) {
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300244 pci_update_config8(dev, 0xe2, ~1, 1);
245 pci_update_config8(dev, 0xe1, 0x7f, 0x80);
Aaron Durbin1c4289d2013-06-21 14:06:11 -0500246 }
Tristan Corrick05b75242018-12-06 22:47:42 +1300247 if (rp == 5 && !is_rp_enabled(6) &&
248 !is_rp_enabled(7) && !is_rp_enabled(8)) {
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300249 pci_update_config8(dev, 0xe2, ~1, 1);
250 pci_update_config8(dev, 0xe1, 0x7f, 0x80);
Aaron Durbin1c4289d2013-06-21 14:06:11 -0500251 }
252 continue;
253 }
254
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300255 pci_update_config8(dev, 0xe2, ~(3 << 4), (3 << 4));
Ryan Salsamendi3f2fe182017-07-04 13:14:16 -0700256 pci_update_config32(dev, 0x420, ~high_bit, high_bit);
Aaron Durbin1c4289d2013-06-21 14:06:11 -0500257
258 /* Per-Port CLKREQ# handling. */
259 if (is_lp && gpio_is_native(18 + rp - 1))
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300260 pci_update_config32(dev, 0x420, ~0, (3 << 29));
Aaron Durbin1c4289d2013-06-21 14:06:11 -0500261
262 /* Enable static clock gating. */
Tristan Corrick05b75242018-12-06 22:47:42 +1300263 if (rp == 1 && !is_rp_enabled(2) &&
264 !is_rp_enabled(3) && !is_rp_enabled(4)) {
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300265 pci_update_config8(dev, 0xe2, ~1, 1);
266 pci_update_config8(dev, 0xe1, 0x7f, 0x80);
Aaron Durbin1c4289d2013-06-21 14:06:11 -0500267 } else if (rp == 5 || rp == 6) {
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300268 pci_update_config8(dev, 0xe2, ~1, 1);
269 pci_update_config8(dev, 0xe1, 0x7f, 0x80);
Aaron Durbin1c4289d2013-06-21 14:06:11 -0500270 }
271 continue;
272 }
273
274 enabled_ports++;
275
276 /* Enable dynamic clock gating. */
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300277 pci_update_config8(dev, 0xe1, 0xfc, 0x03);
Aaron Durbin1c4289d2013-06-21 14:06:11 -0500278
279 if (is_lp) {
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300280 pci_update_config8(dev, 0xe2, ~(1 << 6), (1 << 6));
281 pci_update_config8(dev, 0xe8, ~(3 << 2), (2 << 2));
Aaron Durbin1c4289d2013-06-21 14:06:11 -0500282 }
283
284 /* Update PECR1 register. */
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300285 pci_update_config8(dev, 0xe8, ~0, 1);
Aaron Durbin1c4289d2013-06-21 14:06:11 -0500286
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))
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300291 pci_update_config32(dev, 0x420, ~0, (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))
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300295 pci_update_config8(dev, 0xe1, 0xc3, 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])
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300299 pci_update_config8(rpc.ports[0], 0xe1, ~(1 << 6), (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;
Elyes HAOUAS73ae0762020-04-28 10:13:05 +0200315 u16 reg16;
Aaron Durbin60f82082013-06-19 13:28:04 -0500316
Aaron Durbinc0254e62013-06-20 01:20:30 -0500317 dev = rpc.ports[i];
Aaron Durbin60f82082013-06-19 13:28:04 -0500318
Aaron Durbinc0254e62013-06-20 01:20:30 -0500319 if (dev == NULL) {
320 printk(BIOS_ERR, "Root Port %d device is NULL?\n", i+1);
321 continue;
Aaron Durbin60f82082013-06-19 13:28:04 -0500322 }
323
Aaron Durbinc0254e62013-06-20 01:20:30 -0500324 if (dev->enabled)
325 continue;
326
327 printk(BIOS_DEBUG, "%s: Disabling device\n", dev_path(dev));
328
Aaron Durbin60f82082013-06-19 13:28:04 -0500329 /* Ensure memory, io, and bus master are all disabled */
Elyes HAOUAS73ae0762020-04-28 10:13:05 +0200330 reg16 = pci_read_config16(dev, PCI_COMMAND);
331 reg16 &= ~(PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY | PCI_COMMAND_IO);
332 pci_write_config16(dev, PCI_COMMAND, reg16);
Aaron Durbin60f82082013-06-19 13:28:04 -0500333
Aaron Durbin60f82082013-06-19 13:28:04 -0500334 /* Disable this device if possible */
335 pch_disable_devfn(dev);
Aaron Durbin60f82082013-06-19 13:28:04 -0500336 }
337
Aaron Durbinc0254e62013-06-20 01:20:30 -0500338 if (rpc.coalesce) {
339 int current_func;
Aaron Durbin60f82082013-06-19 13:28:04 -0500340
Aaron Durbinc0254e62013-06-20 01:20:30 -0500341 /* For all Root Ports N enabled ports get assigned the lower
342 * PCI function number. The disabled ones get upper PCI
343 * function numbers. */
344 current_func = 0;
345 for (i = 0; i < rpc.num_ports; i++) {
Tristan Corrick05b75242018-12-06 22:47:42 +1300346 if (!is_rp_enabled(i + 1))
Aaron Durbinc0254e62013-06-20 01:20:30 -0500347 continue;
348 pch_pcie_device_set_func(i, current_func);
349 current_func++;
350 }
351
352 /* Allocate the disabled devices' PCI function number. */
353 for (i = 0; i < rpc.num_ports; i++) {
Tristan Corrick05b75242018-12-06 22:47:42 +1300354 if (is_rp_enabled(i + 1))
Aaron Durbinc0254e62013-06-20 01:20:30 -0500355 continue;
356 pch_pcie_device_set_func(i, current_func);
357 current_func++;
358 }
359 }
360
361 printk(BIOS_SPEW, "PCH: RPFN 0x%08x -> 0x%08x\n",
362 rpc.orig_rpfn, rpc.new_rpfn);
363 RCBA32(RPFN) = rpc.new_rpfn;
364}
365
Elyes HAOUAS7a5f7712018-06-08 17:20:38 +0200366static void root_port_mark_disable(struct device *dev)
Aaron Durbinc0254e62013-06-20 01:20:30 -0500367{
368 /* Mark device as disabled. */
369 dev->enabled = 0;
370 /* Mark device to be hidden. */
371 rpc.new_rpfn |= RPFN_HIDE(PCI_FUNC(dev->path.pci.devfn));
372}
373
Elyes HAOUAS7a5f7712018-06-08 17:20:38 +0200374static void root_port_check_disable(struct device *dev)
Aaron Durbinc0254e62013-06-20 01:20:30 -0500375{
376 int rp;
377 int is_lp;
378
379 /* Device already disabled. */
380 if (!dev->enabled) {
381 root_port_mark_disable(dev);
382 return;
383 }
384
385 rp = root_port_number(dev);
386
387 /* Is the GbE port mapped to this Root Port? */
388 if (rp == rpc.gbe_port) {
389 root_port_mark_disable(dev);
390 return;
391 }
392
393 is_lp = pch_is_lp();
394
395 /* Check Root Port Configuration. */
396 switch (rp) {
397 case 2:
398 /* Root Port 2 is disabled for all lane configurations
399 * but config 00b (4x1 links). */
400 if ((rpc.strpfusecfg1 >> 14) & 0x3) {
401 root_port_mark_disable(dev);
402 return;
403 }
404 break;
405 case 3:
406 /* Root Port 3 is disabled in config 11b (1x4 links). */
407 if (((rpc.strpfusecfg1 >> 14) & 0x3) == 0x3) {
408 root_port_mark_disable(dev);
409 return;
410 }
411 break;
412 case 4:
413 /* Root Port 4 is disabled in configs 11b (1x4 links)
414 * and 10b (2x2 links). */
415 if ((rpc.strpfusecfg1 >> 14) & 0x2) {
416 root_port_mark_disable(dev);
417 return;
418 }
419 break;
420 case 6:
421 if (is_lp)
422 break;
423 /* Root Port 6 is disabled for all lane configurations
424 * but config 00b (4x1 links). */
425 if ((rpc.strpfusecfg2 >> 14) & 0x3) {
426 root_port_mark_disable(dev);
427 return;
428 }
429 break;
430 case 7:
431 if (is_lp)
432 break;
Tristan Corrickbaa4c072018-12-06 22:47:21 +1300433 /* Root Port 7 is disabled in config 11b (1x4 links). */
Aaron Durbinc0254e62013-06-20 01:20:30 -0500434 if (((rpc.strpfusecfg2 >> 14) & 0x3) == 0x3) {
435 root_port_mark_disable(dev);
436 return;
437 }
438 break;
439 case 8:
440 if (is_lp)
441 break;
442 /* Root Port 8 is disabled in configs 11b (1x4 links)
443 * and 10b (2x2 links). */
444 if ((rpc.strpfusecfg2 >> 14) & 0x2) {
445 root_port_mark_disable(dev);
446 return;
447 }
448 break;
449 }
450
451 /* Check Pin Ownership. */
452 if (is_lp) {
453 switch (rp) {
454 case 1:
455 /* Bit 0 is Root Port 1 ownership. */
456 if ((rpc.pin_ownership & 0x1) == 0) {
457 root_port_mark_disable(dev);
458 return;
459 }
460 break;
461 case 2:
462 /* Bit 2 is Root Port 2 ownership. */
463 if ((rpc.pin_ownership & 0x4) == 0) {
464 root_port_mark_disable(dev);
465 return;
466 }
467 break;
468 case 6:
469 /* Bits 7:4 are Root Port 6 pin-lane ownership. */
470 if ((rpc.pin_ownership & 0xf0) == 0) {
471 root_port_mark_disable(dev);
472 return;
473 }
474 break;
475 }
476 } else {
477 switch (rp) {
478 case 1:
479 /* Bits 4 and 0 are Root Port 1 ownership. */
480 if ((rpc.pin_ownership & 0x11) == 0) {
481 root_port_mark_disable(dev);
482 return;
483 }
484 break;
485 case 2:
486 /* Bits 5 and 2 are Root Port 2 ownership. */
487 if ((rpc.pin_ownership & 0x24) == 0) {
488 root_port_mark_disable(dev);
489 return;
490 }
491 break;
492 }
Aaron Durbin60f82082013-06-19 13:28:04 -0500493 }
494}
495
Stefan Reinauerab365af2013-12-03 12:13:26 -0800496static void pcie_add_0x0202000_iobp(u32 reg)
497{
498 u32 reg32;
Aaron Durbin76c37002012-10-30 09:03:43 -0500499
Stefan Reinauerab365af2013-12-03 12:13:26 -0800500 reg32 = pch_iobp_read(reg);
501 reg32 += (0x2 << 16) | (0x2 << 8);
502 pch_iobp_write(reg, reg32);
503}
Aaron Durbin76c37002012-10-30 09:03:43 -0500504
Stefan Reinauerab365af2013-12-03 12:13:26 -0800505static void pch_pcie_early(struct device *dev)
506{
507 int rp;
508 int do_aspm;
509 int is_lp;
Duncan Laurie249a03b2013-08-09 09:06:41 -0700510 struct southbridge_intel_lynxpoint_config *config = dev->chip_info;
Aaron Durbin76c37002012-10-30 09:03:43 -0500511
Stefan Reinauerab365af2013-12-03 12:13:26 -0800512 rp = root_port_number(dev);
513 do_aspm = 0;
514 is_lp = pch_is_lp();
Aaron Durbin76c37002012-10-30 09:03:43 -0500515
Stefan Reinauerab365af2013-12-03 12:13:26 -0800516 if (is_lp) {
517 switch (rp) {
518 case 1:
519 case 2:
520 case 3:
521 case 4:
522 /* Bits 31:28 of b0d28f0 0x32c register correspnd to
523 * Root Ports 4:1. */
524 do_aspm = !!(rpc.b0d28f0_32c & (1 << (28 + rp - 1)));
525 break;
526 case 5:
527 /* Bit 28 of b0d28f4 0x32c register correspnd to
528 * Root Ports 4:1. */
529 do_aspm = !!(rpc.b0d28f4_32c & (1 << 28));
530 break;
531 case 6:
532 /* Bit 28 of b0d28f5 0x32c register correspnd to
533 * Root Ports 4:1. */
534 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:
543 /* Bits 31:28 of b0d28f0 0x32c register correspnd to
544 * Root Ports 4:1. */
545 do_aspm = !!(rpc.b0d28f0_32c & (1 << (28 + rp - 1)));
546 break;
547 case 5:
548 case 6:
549 case 7:
550 case 8:
551 /* Bit 31:28 of b0d28f4 0x32c register correspnd to
552 * Root Ports 8:5. */
553 do_aspm = !!(rpc.b0d28f4_32c & (1 << (28 + rp - 5)));
554 break;
555 }
Aaron Durbin76c37002012-10-30 09:03:43 -0500556 }
Stefan Reinauerab365af2013-12-03 12:13:26 -0800557
Duncan Laurie249a03b2013-08-09 09:06:41 -0700558 /* Allow ASPM to be forced on in devicetree */
559 if (config && (config->pcie_port_force_aspm & (1 << (rp - 1))))
560 do_aspm = 1;
561
562 printk(BIOS_DEBUG, "PCIe Root Port %d ASPM is %sabled\n",
563 rp, do_aspm ? "en" : "dis");
564
Stefan Reinauerab365af2013-12-03 12:13:26 -0800565 if (do_aspm) {
566 /* Set ASPM bits in MPC2 register. */
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300567 pci_update_config32(dev, 0xd4, ~(0x3 << 2), (1 << 4) | (0x2 << 2));
Stefan Reinauerab365af2013-12-03 12:13:26 -0800568
569 /* Set unique clock exit latency in MPC register. */
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300570 pci_update_config32(dev, 0xd8, ~(0x7 << 18), (0x7 << 18));
Stefan Reinauerab365af2013-12-03 12:13:26 -0800571
572 /* Set L1 exit latency in LCAP register. */
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300573 pci_update_config32(dev, 0x4c, ~(0x7 << 15), (0x4 << 15));
Stefan Reinauerab365af2013-12-03 12:13:26 -0800574
575 if (is_lp) {
576 switch (rp) {
577 case 1:
578 pcie_add_0x0202000_iobp(0xe9002440);
579 break;
580 case 2:
581 pcie_add_0x0202000_iobp(0xe9002640);
582 break;
583 case 3:
584 pcie_add_0x0202000_iobp(0xe9000840);
585 break;
586 case 4:
587 pcie_add_0x0202000_iobp(0xe9000a40);
588 break;
589 case 5:
590 pcie_add_0x0202000_iobp(0xe9000c40);
591 pcie_add_0x0202000_iobp(0xe9000e40);
592 pcie_add_0x0202000_iobp(0xe9001040);
593 pcie_add_0x0202000_iobp(0xe9001240);
594 break;
595 case 6:
596 /* Update IOBP based on lane ownership. */
597 if (rpc.pin_ownership & (1 << 4))
598 pcie_add_0x0202000_iobp(0xea002040);
599 if (rpc.pin_ownership & (1 << 5))
600 pcie_add_0x0202000_iobp(0xea002240);
601 if (rpc.pin_ownership & (1 << 6))
602 pcie_add_0x0202000_iobp(0xea002440);
603 if (rpc.pin_ownership & (1 << 7))
604 pcie_add_0x0202000_iobp(0xea002640);
605 break;
606 }
607 } else {
608 switch (rp) {
609 case 1:
610 if ((rpc.pin_ownership & 0x3) == 1)
611 pcie_add_0x0202000_iobp(0xe9002e40);
612 else
613 pcie_add_0x0202000_iobp(0xea002040);
614 break;
615 case 2:
616 if ((rpc.pin_ownership & 0xc) == 0x4)
617 pcie_add_0x0202000_iobp(0xe9002c40);
618 else
619 pcie_add_0x0202000_iobp(0xea002240);
620 break;
621 case 3:
622 pcie_add_0x0202000_iobp(0xe9002a40);
623 break;
624 case 4:
625 pcie_add_0x0202000_iobp(0xe9002840);
626 break;
627 case 5:
628 pcie_add_0x0202000_iobp(0xe9002640);
629 break;
630 case 6:
631 pcie_add_0x0202000_iobp(0xe9002440);
632 break;
633 case 7:
634 pcie_add_0x0202000_iobp(0xe9002240);
635 break;
636 case 8:
637 pcie_add_0x0202000_iobp(0xe9002040);
638 break;
639 }
640 }
641
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300642 pci_update_config32(dev, 0x338, ~(1 << 26), 0);
Stefan Reinauerab365af2013-12-03 12:13:26 -0800643 }
644
645 /* Enable LTR in Root Port. */
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300646 pci_update_config32(dev, 0x64, ~(1 << 11), (1 << 11));
647 pci_update_config32(dev, 0x68, ~(1 << 10), (1 << 10));
Stefan Reinauerab365af2013-12-03 12:13:26 -0800648
Ryan Salsamendi0d9b3602017-06-30 17:15:57 -0700649 pci_update_config32(dev, 0x318, ~(0xffffUL << 16), (0x1414UL << 16));
Stefan Reinauerab365af2013-12-03 12:13:26 -0800650
651 /* Set L1 exit latency in LCAP register. */
652 if (!do_aspm && (pci_read_config8(dev, 0xf5) & 0x1))
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300653 pci_update_config32(dev, 0x4c, ~(0x7 << 15), (0x4 << 15));
Stefan Reinauerab365af2013-12-03 12:13:26 -0800654 else
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300655 pci_update_config32(dev, 0x4c, ~(0x7 << 15), (0x2 << 15));
Stefan Reinauerab365af2013-12-03 12:13:26 -0800656
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300657 pci_update_config32(dev, 0x314, 0x0, 0x743a361b);
Stefan Reinauerab365af2013-12-03 12:13:26 -0800658
659 /* Set Common Clock Exit Latency in MPC register. */
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300660 pci_update_config32(dev, 0xd8, ~(0x7 << 15), (0x3 << 15));
Stefan Reinauerab365af2013-12-03 12:13:26 -0800661
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300662 pci_update_config32(dev, 0x33c, ~0x00ffffff, 0x854c74);
Stefan Reinauerab365af2013-12-03 12:13:26 -0800663
Stefan Reinauerab365af2013-12-03 12:13:26 -0800664 /* Set Invalid Recieve Range Check Enable in MPC register. */
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300665 pci_update_config32(dev, 0xd8, ~0, (1 << 25));
Stefan Reinauerab365af2013-12-03 12:13:26 -0800666
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300667 pci_update_config8(dev, 0xf5, 0x3f, 0);
Stefan Reinauerab365af2013-12-03 12:13:26 -0800668
669 if (rp == 1 || rp == 5 || (is_lp && rp == 6))
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300670 pci_update_config8(dev, 0xf7, ~0xc, 0);
Stefan Reinauerab365af2013-12-03 12:13:26 -0800671
672 /* Set EOI forwarding disable. */
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300673 pci_update_config32(dev, 0xd4, ~0, (1 << 1));
Stefan Reinauerab365af2013-12-03 12:13:26 -0800674
675 /* Set something involving advanced error reporting. */
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300676 pci_update_config32(dev, 0x100, ~((1 << 20) - 1), 0x10001);
Stefan Reinauerab365af2013-12-03 12:13:26 -0800677
678 if (is_lp)
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300679 pci_update_config32(dev, 0x100, ~0, (1 << 29));
Stefan Reinauerab365af2013-12-03 12:13:26 -0800680
681 /* Read and write back write-once capability registers. */
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300682 pci_update_config32(dev, 0x34, ~0, 0);
683 pci_update_config32(dev, 0x40, ~0, 0);
684 pci_update_config32(dev, 0x80, ~0, 0);
685 pci_update_config32(dev, 0x90, ~0, 0);
Aaron Durbin76c37002012-10-30 09:03:43 -0500686}
687
688static void pci_init(struct device *dev)
689{
690 u16 reg16;
Aaron Durbin76c37002012-10-30 09:03:43 -0500691
692 printk(BIOS_DEBUG, "Initializing PCH PCIe bridge.\n");
693
Aaron Durbinc0254e62013-06-20 01:20:30 -0500694 /* Enable SERR */
Elyes HAOUAS73ae0762020-04-28 10:13:05 +0200695 pci_or_config16(dev, PCI_COMMAND, PCI_COMMAND_SERR);
Aaron Durbinc0254e62013-06-20 01:20:30 -0500696
Aaron Durbin76c37002012-10-30 09:03:43 -0500697 /* Enable Bus Master */
Elyes HAOUAS73ae0762020-04-28 10:13:05 +0200698 pci_or_config16(dev, PCI_COMMAND, PCI_COMMAND_MASTER);
Aaron Durbin76c37002012-10-30 09:03:43 -0500699
700 /* Set Cache Line Size to 0x10 */
701 // This has no effect but the OS might expect it
702 pci_write_config8(dev, 0x0c, 0x10);
703
Kyösti Mälkkidf128a52019-09-21 18:35:37 +0300704 reg16 = pci_read_config16(dev, PCI_BRIDGE_CONTROL);
705 reg16 &= ~PCI_BRIDGE_CTL_PARITY;
Kyösti Mälkkidf128a52019-09-21 18:35:37 +0300706 pci_write_config16(dev, PCI_BRIDGE_CONTROL, reg16);
Aaron Durbin76c37002012-10-30 09:03:43 -0500707
Aaron Durbin76c37002012-10-30 09:03:43 -0500708 /* Clear errors in status registers */
709 reg16 = pci_read_config16(dev, 0x06);
Aaron Durbin76c37002012-10-30 09:03:43 -0500710 pci_write_config16(dev, 0x06, reg16);
Aaron Durbin76c37002012-10-30 09:03:43 -0500711 reg16 = pci_read_config16(dev, 0x1e);
Aaron Durbin76c37002012-10-30 09:03:43 -0500712 pci_write_config16(dev, 0x1e, reg16);
Aaron Durbin76c37002012-10-30 09:03:43 -0500713}
714
Elyes HAOUAS7a5f7712018-06-08 17:20:38 +0200715static void pch_pcie_enable(struct device *dev)
Aaron Durbin76c37002012-10-30 09:03:43 -0500716{
Aaron Durbinc0254e62013-06-20 01:20:30 -0500717 /* Add this device to the root port config structure. */
718 root_port_init_config(dev);
719
720 /* Check to see if this Root Port should be disabled. */
721 root_port_check_disable(dev);
722
Aaron Durbin76c37002012-10-30 09:03:43 -0500723 /* Power Management init before enumeration */
Aaron Durbinc0254e62013-06-20 01:20:30 -0500724 if (dev->enabled)
Stefan Reinauerab365af2013-12-03 12:13:26 -0800725 pch_pcie_early(dev);
Aaron Durbinc0254e62013-06-20 01:20:30 -0500726
727 /*
728 * When processing the last PCIe root port we can now
729 * update the Root Port Function Number and Hide register.
730 */
731 if (root_port_is_last(dev))
732 root_port_commit_config();
Aaron Durbin76c37002012-10-30 09:03:43 -0500733}
734
Aaron Durbin76c37002012-10-30 09:03:43 -0500735static struct device_operations device_ops = {
736 .read_resources = pci_bus_read_resources,
737 .set_resources = pci_dev_set_resources,
738 .enable_resources = pci_bus_enable_resources,
739 .init = pci_init,
740 .enable = pch_pcie_enable,
741 .scan_bus = pciexp_scan_bridge,
Angel Pons1fc0edd2020-05-31 00:03:28 +0200742 .ops_pci = &pci_dev_ops_pci,
Aaron Durbin76c37002012-10-30 09:03:43 -0500743};
744
Duncan Laurie74c0d052012-12-17 11:31:40 -0800745static const unsigned short pci_device_ids[] = {
746 /* Lynxpoint Mobile */
747 0x8c10, 0x8c12, 0x8c14, 0x8c16, 0x8c18, 0x8c1a, 0x8c1c, 0x8c1e,
748 /* Lynxpoint Low Power */
749 0x9c10, 0x9c12, 0x9c14, 0x9c16, 0x9c18, 0x9c1a,
750 0
751};
Aaron Durbin76c37002012-10-30 09:03:43 -0500752
753static const struct pci_driver pch_pcie __pci_driver = {
754 .ops = &device_ops,
755 .vendor = PCI_VENDOR_ID_INTEL,
756 .devices = pci_device_ids,
757};