blob: 750b53395cd9d5bc5d4d0bd10546a0be24f7efb2 [file] [log] [blame]
Aaron Durbin76c37002012-10-30 09:03:43 -05001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2008-2009 coresystems GmbH
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; version 2 of
9 * the License.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
Aaron Durbin76c37002012-10-30 09:03:43 -050015 */
16
17#include <console/console.h>
18#include <device/device.h>
19#include <device/pci.h>
20#include <device/pciexp.h>
21#include <device/pci_ids.h>
Patrick Rudolphe56189c2018-04-18 10:11:59 +020022#include <device/pci_ops.h>
Aaron Durbin76c37002012-10-30 09:03:43 -050023#include "pch.h"
Patrick Rudolph273a8dc2016-02-06 18:07:59 +010024#include <southbridge/intel/common/gpio.h>
Aaron Durbin76c37002012-10-30 09:03:43 -050025
Aaron Durbinc0254e62013-06-20 01:20:30 -050026#define MAX_NUM_ROOT_PORTS 8
Aaron Durbinc0254e62013-06-20 01:20:30 -050027
28struct root_port_config {
29 /* RPFN is a write-once register so keep a copy until it is written */
30 u32 orig_rpfn;
31 u32 new_rpfn;
32 u32 pin_ownership;
33 u32 strpfusecfg1;
34 u32 strpfusecfg2;
35 u32 strpfusecfg3;
Stefan Reinauerab365af2013-12-03 12:13:26 -080036 u32 b0d28f0_32c;
37 u32 b0d28f4_32c;
38 u32 b0d28f5_32c;
Aaron Durbinc0254e62013-06-20 01:20:30 -050039 int coalesce;
40 int gbe_port;
41 int num_ports;
Elyes HAOUAS7a5f7712018-06-08 17:20:38 +020042 struct device *ports[MAX_NUM_ROOT_PORTS];
Aaron Durbinc0254e62013-06-20 01:20:30 -050043};
44
45static struct root_port_config rpc;
46
47static inline int max_root_ports(void)
Aaron Durbin60f82082013-06-19 13:28:04 -050048{
Tristan Corrickd3f01b22018-12-06 22:46:58 +130049 if (pch_is_lp() || pch_silicon_id() == PCI_DEVICE_ID_INTEL_LPT_H81)
50 return 6;
51
52 return 8;
Aaron Durbin60f82082013-06-19 13:28:04 -050053}
54
Elyes HAOUAS7a5f7712018-06-08 17:20:38 +020055static inline int root_port_is_first(struct device *dev)
Aaron Durbin60f82082013-06-19 13:28:04 -050056{
Aaron Durbinc0254e62013-06-20 01:20:30 -050057 return PCI_FUNC(dev->path.pci.devfn) == 0;
58}
Aaron Durbin60f82082013-06-19 13:28:04 -050059
Elyes HAOUAS7a5f7712018-06-08 17:20:38 +020060static inline int root_port_is_last(struct device *dev)
Aaron Durbinc0254e62013-06-20 01:20:30 -050061{
62 return PCI_FUNC(dev->path.pci.devfn) == (rpc.num_ports - 1);
63}
Aaron Durbin60f82082013-06-19 13:28:04 -050064
Aaron Durbinc0254e62013-06-20 01:20:30 -050065/* Root ports are numbered 1..N in the documentation. */
Elyes HAOUAS7a5f7712018-06-08 17:20:38 +020066static inline int root_port_number(struct device *dev)
Aaron Durbinc0254e62013-06-20 01:20:30 -050067{
68 return PCI_FUNC(dev->path.pci.devfn) + 1;
69}
Aaron Durbin60f82082013-06-19 13:28:04 -050070
Aaron Durbinc0254e62013-06-20 01:20:30 -050071static void root_port_config_update_gbe_port(void)
72{
73 /* Is the Gbe Port enabled? */
74 if (!((rpc.strpfusecfg1 >> 19) & 1))
75 return;
76
77 if (pch_is_lp()) {
78 switch ((rpc.strpfusecfg1 >> 16) & 0x7) {
79 case 0:
80 rpc.gbe_port = 3;
81 break;
82 case 1:
83 rpc.gbe_port = 4;
84 break;
85 case 2:
86 case 3:
87 case 4:
88 case 5:
89 /* Lanes 0-4 of Root Port 5. */
90 rpc.gbe_port = 5;
91 break;
92 default:
93 printk(BIOS_DEBUG, "Invalid GbE Port Selection.\n");
94 }
95 } else {
96 /* Non-LP has 1:1 mapping with root ports. */
97 rpc.gbe_port = ((rpc.strpfusecfg1 >> 16) & 0x7) + 1;
98 }
99}
100
Elyes HAOUAS7a5f7712018-06-08 17:20:38 +0200101static void root_port_init_config(struct device *dev)
Aaron Durbinc0254e62013-06-20 01:20:30 -0500102{
103 int rp;
104
105 if (root_port_is_first(dev)) {
106 rpc.orig_rpfn = RCBA32(RPFN);
107 rpc.new_rpfn = rpc.orig_rpfn;
108 rpc.num_ports = max_root_ports();
109 rpc.gbe_port = -1;
110
111 rpc.pin_ownership = pci_read_config32(dev, 0x410);
112 root_port_config_update_gbe_port();
113
114 if (dev->chip_info != NULL) {
115 struct southbridge_intel_lynxpoint_config *config;
116
117 config = dev->chip_info;
118 rpc.coalesce = config->pcie_port_coalesce;
119 }
120 }
121
122 rp = root_port_number(dev);
123 if (rp > rpc.num_ports) {
124 printk(BIOS_ERR, "Found Root Port %d, expecting %d\n",
125 rp, rpc.num_ports);
126 return;
127 }
128
129 /* Read the fuse configuration and pin ownership. */
130 switch (rp) {
131 case 1:
132 rpc.strpfusecfg1 = pci_read_config32(dev, 0xfc);
Stefan Reinauerab365af2013-12-03 12:13:26 -0800133 rpc.b0d28f0_32c = pci_read_config32(dev, 0x32c);
Aaron Durbinc0254e62013-06-20 01:20:30 -0500134 break;
135 case 5:
136 rpc.strpfusecfg2 = pci_read_config32(dev, 0xfc);
Stefan Reinauerab365af2013-12-03 12:13:26 -0800137 rpc.b0d28f4_32c = pci_read_config32(dev, 0x32c);
Aaron Durbinc0254e62013-06-20 01:20:30 -0500138 break;
139 case 6:
Stefan Reinauerab365af2013-12-03 12:13:26 -0800140 rpc.b0d28f5_32c = pci_read_config32(dev, 0x32c);
Aaron Durbinc0254e62013-06-20 01:20:30 -0500141 rpc.strpfusecfg3 = pci_read_config32(dev, 0xfc);
142 break;
143 default:
144 break;
145 }
146
147 /* Cache pci device. */
148 rpc.ports[rp - 1] = dev;
Aaron Durbin60f82082013-06-19 13:28:04 -0500149}
150
151/* Update devicetree with new Root Port function number assignment */
Aaron Durbinc0254e62013-06-20 01:20:30 -0500152static void pch_pcie_device_set_func(int index, int pci_func)
Aaron Durbin60f82082013-06-19 13:28:04 -0500153{
Elyes HAOUAS7a5f7712018-06-08 17:20:38 +0200154 struct device *dev;
Aaron Durbinc0254e62013-06-20 01:20:30 -0500155 unsigned new_devfn;
Aaron Durbin60f82082013-06-19 13:28:04 -0500156
Aaron Durbinc0254e62013-06-20 01:20:30 -0500157 dev = rpc.ports[index];
Aaron Durbin60f82082013-06-19 13:28:04 -0500158
Aaron Durbinc0254e62013-06-20 01:20:30 -0500159 /* Set the new PCI function field for this Root Port. */
160 rpc.new_rpfn &= ~RPFN_FNMASK(index);
161 rpc.new_rpfn |= RPFN_FNSET(index, pci_func);
Aaron Durbin60f82082013-06-19 13:28:04 -0500162
Aaron Durbinc0254e62013-06-20 01:20:30 -0500163 /* Determine the new devfn for this port */
164 new_devfn = PCI_DEVFN(PCH_PCIE_DEV_SLOT, pci_func);
Aaron Durbin60f82082013-06-19 13:28:04 -0500165
Aaron Durbinc0254e62013-06-20 01:20:30 -0500166 if (dev->path.pci.devfn != new_devfn) {
167 printk(BIOS_DEBUG,
168 "PCH: PCIe map %02x.%1x -> %02x.%1x\n",
169 PCI_SLOT(dev->path.pci.devfn),
170 PCI_FUNC(dev->path.pci.devfn),
171 PCI_SLOT(new_devfn), PCI_FUNC(new_devfn));
Aaron Durbin60f82082013-06-19 13:28:04 -0500172
Aaron Durbinc0254e62013-06-20 01:20:30 -0500173 dev->path.pci.devfn = new_devfn;
Aaron Durbin60f82082013-06-19 13:28:04 -0500174 }
175}
176
Aaron Durbin1c4289d2013-06-21 14:06:11 -0500177static void pcie_enable_clock_gating(void)
178{
179 int i;
180 int is_lp;
181 int enabled_ports;
182
183 is_lp = pch_is_lp();
184 enabled_ports = 0;
185
186 for (i = 0; i < rpc.num_ports; i++) {
Elyes HAOUAS7a5f7712018-06-08 17:20:38 +0200187 struct device *dev;
Aaron Durbin1c4289d2013-06-21 14:06:11 -0500188 int rp;
189
190 dev = rpc.ports[i];
191 rp = root_port_number(dev);
192
193 if (!dev->enabled) {
Ryan Salsamendi3f2fe182017-07-04 13:14:16 -0700194 static const uint32_t high_bit = (1UL << 31);
195
Aaron Durbin1c4289d2013-06-21 14:06:11 -0500196 /* Configure shared resource clock gating. */
197 if (rp == 1 || rp == 5 || (rp == 6 && is_lp))
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300198 pci_update_config8(dev, 0xe1, 0xc3, 0x3c);
Aaron Durbin1c4289d2013-06-21 14:06:11 -0500199
200 if (!is_lp) {
201 if (rp == 1 && !rpc.ports[1]->enabled &&
202 !rpc.ports[2]->enabled &&
203 !rpc.ports[3]->enabled) {
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300204 pci_update_config8(dev, 0xe2, ~1, 1);
205 pci_update_config8(dev, 0xe1, 0x7f, 0x80);
Aaron Durbin1c4289d2013-06-21 14:06:11 -0500206 }
207 if (rp == 5 && !rpc.ports[5]->enabled &&
Tristan Corrickd3f01b22018-12-06 22:46:58 +1300208 (rpc.ports[6] == NULL ||
209 !rpc.ports[6]->enabled) &&
210 (rpc.ports[7] == NULL ||
211 !rpc.ports[7]->enabled)) {
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300212 pci_update_config8(dev, 0xe2, ~1, 1);
213 pci_update_config8(dev, 0xe1, 0x7f, 0x80);
Aaron Durbin1c4289d2013-06-21 14:06:11 -0500214 }
215 continue;
216 }
217
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300218 pci_update_config8(dev, 0xe2, ~(3 << 4), (3 << 4));
Ryan Salsamendi3f2fe182017-07-04 13:14:16 -0700219 pci_update_config32(dev, 0x420, ~high_bit, high_bit);
Aaron Durbin1c4289d2013-06-21 14:06:11 -0500220
221 /* Per-Port CLKREQ# handling. */
222 if (is_lp && gpio_is_native(18 + rp - 1))
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300223 pci_update_config32(dev, 0x420, ~0, (3 << 29));
Aaron Durbin1c4289d2013-06-21 14:06:11 -0500224
225 /* Enable static clock gating. */
226 if (rp == 1 && !rpc.ports[1]->enabled &&
227 !rpc.ports[2]->enabled && !rpc.ports[3]->enabled) {
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300228 pci_update_config8(dev, 0xe2, ~1, 1);
229 pci_update_config8(dev, 0xe1, 0x7f, 0x80);
Aaron Durbin1c4289d2013-06-21 14:06:11 -0500230 } else if (rp == 5 || rp == 6) {
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300231 pci_update_config8(dev, 0xe2, ~1, 1);
232 pci_update_config8(dev, 0xe1, 0x7f, 0x80);
Aaron Durbin1c4289d2013-06-21 14:06:11 -0500233 }
234 continue;
235 }
236
237 enabled_ports++;
238
239 /* Enable dynamic clock gating. */
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300240 pci_update_config8(dev, 0xe1, 0xfc, 0x03);
Aaron Durbin1c4289d2013-06-21 14:06:11 -0500241
242 if (is_lp) {
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300243 pci_update_config8(dev, 0xe2, ~(1 << 6), (1 << 6));
244 pci_update_config8(dev, 0xe8, ~(3 << 2), (2 << 2));
Aaron Durbin1c4289d2013-06-21 14:06:11 -0500245 }
246
247 /* Update PECR1 register. */
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300248 pci_update_config8(dev, 0xe8, ~0, 1);
Aaron Durbin1c4289d2013-06-21 14:06:11 -0500249
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300250 pci_update_config8(dev, 0x324, ~(1 << 5), (1 < 5));
Aaron Durbin1c4289d2013-06-21 14:06:11 -0500251
252 /* Per-Port CLKREQ# handling. */
253 if (is_lp && gpio_is_native(18 + rp - 1))
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300254 pci_update_config32(dev, 0x420, ~0, (3 << 29));
Aaron Durbin1c4289d2013-06-21 14:06:11 -0500255
256 /* Configure shared resource clock gating. */
257 if (rp == 1 || rp == 5 || (rp == 6 && is_lp))
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300258 pci_update_config8(dev, 0xe1, 0xc3, 0x3c);
Aaron Durbin1c4289d2013-06-21 14:06:11 -0500259 }
260
261 if (!enabled_ports && is_lp)
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300262 pci_update_config8(rpc.ports[0], 0xe1, ~(1 << 6), (1 << 6));
Aaron Durbin1c4289d2013-06-21 14:06:11 -0500263}
264
Aaron Durbinc0254e62013-06-20 01:20:30 -0500265static void root_port_commit_config(void)
Aaron Durbin60f82082013-06-19 13:28:04 -0500266{
Aaron Durbinc0254e62013-06-20 01:20:30 -0500267 int i;
Aaron Durbin60f82082013-06-19 13:28:04 -0500268
Aaron Durbinc0254e62013-06-20 01:20:30 -0500269 /* If the first root port is disabled the coalesce ports. */
270 if (!rpc.ports[0]->enabled)
271 rpc.coalesce = 1;
Aaron Durbin60f82082013-06-19 13:28:04 -0500272
Aaron Durbin1c4289d2013-06-21 14:06:11 -0500273 /* Perform clock gating configuration. */
274 pcie_enable_clock_gating();
275
Aaron Durbinc0254e62013-06-20 01:20:30 -0500276 for (i = 0; i < rpc.num_ports; i++) {
Elyes HAOUAS7a5f7712018-06-08 17:20:38 +0200277 struct device *dev;
Aaron Durbinc0254e62013-06-20 01:20:30 -0500278 u32 reg32;
Aaron Durbin60f82082013-06-19 13:28:04 -0500279
Aaron Durbinc0254e62013-06-20 01:20:30 -0500280 dev = rpc.ports[i];
Aaron Durbin60f82082013-06-19 13:28:04 -0500281
Aaron Durbinc0254e62013-06-20 01:20:30 -0500282 if (dev == NULL) {
283 printk(BIOS_ERR, "Root Port %d device is NULL?\n", i+1);
284 continue;
Aaron Durbin60f82082013-06-19 13:28:04 -0500285 }
286
Aaron Durbinc0254e62013-06-20 01:20:30 -0500287 if (dev->enabled)
288 continue;
289
290 printk(BIOS_DEBUG, "%s: Disabling device\n", dev_path(dev));
291
Aaron Durbin60f82082013-06-19 13:28:04 -0500292 /* Ensure memory, io, and bus master are all disabled */
293 reg32 = pci_read_config32(dev, PCI_COMMAND);
294 reg32 &= ~(PCI_COMMAND_MASTER |
295 PCI_COMMAND_MEMORY | PCI_COMMAND_IO);
296 pci_write_config32(dev, PCI_COMMAND, reg32);
297
Aaron Durbin60f82082013-06-19 13:28:04 -0500298 /* Disable this device if possible */
299 pch_disable_devfn(dev);
Aaron Durbin60f82082013-06-19 13:28:04 -0500300 }
301
Aaron Durbinc0254e62013-06-20 01:20:30 -0500302 if (rpc.coalesce) {
303 int current_func;
Aaron Durbin60f82082013-06-19 13:28:04 -0500304
Aaron Durbinc0254e62013-06-20 01:20:30 -0500305 /* For all Root Ports N enabled ports get assigned the lower
306 * PCI function number. The disabled ones get upper PCI
307 * function numbers. */
308 current_func = 0;
309 for (i = 0; i < rpc.num_ports; i++) {
310 if (!rpc.ports[i]->enabled)
311 continue;
312 pch_pcie_device_set_func(i, current_func);
313 current_func++;
314 }
315
316 /* Allocate the disabled devices' PCI function number. */
317 for (i = 0; i < rpc.num_ports; i++) {
318 if (rpc.ports[i]->enabled)
319 continue;
320 pch_pcie_device_set_func(i, current_func);
321 current_func++;
322 }
323 }
324
325 printk(BIOS_SPEW, "PCH: RPFN 0x%08x -> 0x%08x\n",
326 rpc.orig_rpfn, rpc.new_rpfn);
327 RCBA32(RPFN) = rpc.new_rpfn;
328}
329
Elyes HAOUAS7a5f7712018-06-08 17:20:38 +0200330static void root_port_mark_disable(struct device *dev)
Aaron Durbinc0254e62013-06-20 01:20:30 -0500331{
332 /* Mark device as disabled. */
333 dev->enabled = 0;
334 /* Mark device to be hidden. */
335 rpc.new_rpfn |= RPFN_HIDE(PCI_FUNC(dev->path.pci.devfn));
336}
337
Elyes HAOUAS7a5f7712018-06-08 17:20:38 +0200338static void root_port_check_disable(struct device *dev)
Aaron Durbinc0254e62013-06-20 01:20:30 -0500339{
340 int rp;
341 int is_lp;
342
343 /* Device already disabled. */
344 if (!dev->enabled) {
345 root_port_mark_disable(dev);
346 return;
347 }
348
349 rp = root_port_number(dev);
350
351 /* Is the GbE port mapped to this Root Port? */
352 if (rp == rpc.gbe_port) {
353 root_port_mark_disable(dev);
354 return;
355 }
356
357 is_lp = pch_is_lp();
358
359 /* Check Root Port Configuration. */
360 switch (rp) {
361 case 2:
362 /* Root Port 2 is disabled for all lane configurations
363 * but config 00b (4x1 links). */
364 if ((rpc.strpfusecfg1 >> 14) & 0x3) {
365 root_port_mark_disable(dev);
366 return;
367 }
368 break;
369 case 3:
370 /* Root Port 3 is disabled in config 11b (1x4 links). */
371 if (((rpc.strpfusecfg1 >> 14) & 0x3) == 0x3) {
372 root_port_mark_disable(dev);
373 return;
374 }
375 break;
376 case 4:
377 /* Root Port 4 is disabled in configs 11b (1x4 links)
378 * and 10b (2x2 links). */
379 if ((rpc.strpfusecfg1 >> 14) & 0x2) {
380 root_port_mark_disable(dev);
381 return;
382 }
383 break;
384 case 6:
385 if (is_lp)
386 break;
387 /* Root Port 6 is disabled for all lane configurations
388 * but config 00b (4x1 links). */
389 if ((rpc.strpfusecfg2 >> 14) & 0x3) {
390 root_port_mark_disable(dev);
391 return;
392 }
393 break;
394 case 7:
395 if (is_lp)
396 break;
Tristan Corrickbaa4c072018-12-06 22:47:21 +1300397 /* Root Port 7 is disabled in config 11b (1x4 links). */
Aaron Durbinc0254e62013-06-20 01:20:30 -0500398 if (((rpc.strpfusecfg2 >> 14) & 0x3) == 0x3) {
399 root_port_mark_disable(dev);
400 return;
401 }
402 break;
403 case 8:
404 if (is_lp)
405 break;
406 /* Root Port 8 is disabled in configs 11b (1x4 links)
407 * and 10b (2x2 links). */
408 if ((rpc.strpfusecfg2 >> 14) & 0x2) {
409 root_port_mark_disable(dev);
410 return;
411 }
412 break;
413 }
414
415 /* Check Pin Ownership. */
416 if (is_lp) {
417 switch (rp) {
418 case 1:
419 /* Bit 0 is Root Port 1 ownership. */
420 if ((rpc.pin_ownership & 0x1) == 0) {
421 root_port_mark_disable(dev);
422 return;
423 }
424 break;
425 case 2:
426 /* Bit 2 is Root Port 2 ownership. */
427 if ((rpc.pin_ownership & 0x4) == 0) {
428 root_port_mark_disable(dev);
429 return;
430 }
431 break;
432 case 6:
433 /* Bits 7:4 are Root Port 6 pin-lane ownership. */
434 if ((rpc.pin_ownership & 0xf0) == 0) {
435 root_port_mark_disable(dev);
436 return;
437 }
438 break;
439 }
440 } else {
441 switch (rp) {
442 case 1:
443 /* Bits 4 and 0 are Root Port 1 ownership. */
444 if ((rpc.pin_ownership & 0x11) == 0) {
445 root_port_mark_disable(dev);
446 return;
447 }
448 break;
449 case 2:
450 /* Bits 5 and 2 are Root Port 2 ownership. */
451 if ((rpc.pin_ownership & 0x24) == 0) {
452 root_port_mark_disable(dev);
453 return;
454 }
455 break;
456 }
Aaron Durbin60f82082013-06-19 13:28:04 -0500457 }
458}
459
Stefan Reinauerab365af2013-12-03 12:13:26 -0800460static void pcie_add_0x0202000_iobp(u32 reg)
461{
462 u32 reg32;
Aaron Durbin76c37002012-10-30 09:03:43 -0500463
Stefan Reinauerab365af2013-12-03 12:13:26 -0800464 reg32 = pch_iobp_read(reg);
465 reg32 += (0x2 << 16) | (0x2 << 8);
466 pch_iobp_write(reg, reg32);
467}
Aaron Durbin76c37002012-10-30 09:03:43 -0500468
Stefan Reinauerab365af2013-12-03 12:13:26 -0800469static void pch_pcie_early(struct device *dev)
470{
471 int rp;
472 int do_aspm;
473 int is_lp;
Duncan Laurie249a03b2013-08-09 09:06:41 -0700474 struct southbridge_intel_lynxpoint_config *config = dev->chip_info;
Aaron Durbin76c37002012-10-30 09:03:43 -0500475
Stefan Reinauerab365af2013-12-03 12:13:26 -0800476 rp = root_port_number(dev);
477 do_aspm = 0;
478 is_lp = pch_is_lp();
Aaron Durbin76c37002012-10-30 09:03:43 -0500479
Stefan Reinauerab365af2013-12-03 12:13:26 -0800480 if (is_lp) {
481 switch (rp) {
482 case 1:
483 case 2:
484 case 3:
485 case 4:
486 /* Bits 31:28 of b0d28f0 0x32c register correspnd to
487 * Root Ports 4:1. */
488 do_aspm = !!(rpc.b0d28f0_32c & (1 << (28 + rp - 1)));
489 break;
490 case 5:
491 /* Bit 28 of b0d28f4 0x32c register correspnd to
492 * Root Ports 4:1. */
493 do_aspm = !!(rpc.b0d28f4_32c & (1 << 28));
494 break;
495 case 6:
496 /* Bit 28 of b0d28f5 0x32c register correspnd to
497 * Root Ports 4:1. */
498 do_aspm = !!(rpc.b0d28f5_32c & (1 << 28));
499 break;
500 }
501 } else {
502 switch (rp) {
503 case 1:
504 case 2:
505 case 3:
506 case 4:
507 /* Bits 31:28 of b0d28f0 0x32c register correspnd to
508 * Root Ports 4:1. */
509 do_aspm = !!(rpc.b0d28f0_32c & (1 << (28 + rp - 1)));
510 break;
511 case 5:
512 case 6:
513 case 7:
514 case 8:
515 /* Bit 31:28 of b0d28f4 0x32c register correspnd to
516 * Root Ports 8:5. */
517 do_aspm = !!(rpc.b0d28f4_32c & (1 << (28 + rp - 5)));
518 break;
519 }
Aaron Durbin76c37002012-10-30 09:03:43 -0500520 }
Stefan Reinauerab365af2013-12-03 12:13:26 -0800521
Duncan Laurie249a03b2013-08-09 09:06:41 -0700522 /* Allow ASPM to be forced on in devicetree */
523 if (config && (config->pcie_port_force_aspm & (1 << (rp - 1))))
524 do_aspm = 1;
525
526 printk(BIOS_DEBUG, "PCIe Root Port %d ASPM is %sabled\n",
527 rp, do_aspm ? "en" : "dis");
528
Stefan Reinauerab365af2013-12-03 12:13:26 -0800529 if (do_aspm) {
530 /* Set ASPM bits in MPC2 register. */
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300531 pci_update_config32(dev, 0xd4, ~(0x3 << 2), (1 << 4) | (0x2 << 2));
Stefan Reinauerab365af2013-12-03 12:13:26 -0800532
533 /* Set unique clock exit latency in MPC register. */
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300534 pci_update_config32(dev, 0xd8, ~(0x7 << 18), (0x7 << 18));
Stefan Reinauerab365af2013-12-03 12:13:26 -0800535
536 /* Set L1 exit latency in LCAP register. */
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300537 pci_update_config32(dev, 0x4c, ~(0x7 << 15), (0x4 << 15));
Stefan Reinauerab365af2013-12-03 12:13:26 -0800538
539 if (is_lp) {
540 switch (rp) {
541 case 1:
542 pcie_add_0x0202000_iobp(0xe9002440);
543 break;
544 case 2:
545 pcie_add_0x0202000_iobp(0xe9002640);
546 break;
547 case 3:
548 pcie_add_0x0202000_iobp(0xe9000840);
549 break;
550 case 4:
551 pcie_add_0x0202000_iobp(0xe9000a40);
552 break;
553 case 5:
554 pcie_add_0x0202000_iobp(0xe9000c40);
555 pcie_add_0x0202000_iobp(0xe9000e40);
556 pcie_add_0x0202000_iobp(0xe9001040);
557 pcie_add_0x0202000_iobp(0xe9001240);
558 break;
559 case 6:
560 /* Update IOBP based on lane ownership. */
561 if (rpc.pin_ownership & (1 << 4))
562 pcie_add_0x0202000_iobp(0xea002040);
563 if (rpc.pin_ownership & (1 << 5))
564 pcie_add_0x0202000_iobp(0xea002240);
565 if (rpc.pin_ownership & (1 << 6))
566 pcie_add_0x0202000_iobp(0xea002440);
567 if (rpc.pin_ownership & (1 << 7))
568 pcie_add_0x0202000_iobp(0xea002640);
569 break;
570 }
571 } else {
572 switch (rp) {
573 case 1:
574 if ((rpc.pin_ownership & 0x3) == 1)
575 pcie_add_0x0202000_iobp(0xe9002e40);
576 else
577 pcie_add_0x0202000_iobp(0xea002040);
578 break;
579 case 2:
580 if ((rpc.pin_ownership & 0xc) == 0x4)
581 pcie_add_0x0202000_iobp(0xe9002c40);
582 else
583 pcie_add_0x0202000_iobp(0xea002240);
584 break;
585 case 3:
586 pcie_add_0x0202000_iobp(0xe9002a40);
587 break;
588 case 4:
589 pcie_add_0x0202000_iobp(0xe9002840);
590 break;
591 case 5:
592 pcie_add_0x0202000_iobp(0xe9002640);
593 break;
594 case 6:
595 pcie_add_0x0202000_iobp(0xe9002440);
596 break;
597 case 7:
598 pcie_add_0x0202000_iobp(0xe9002240);
599 break;
600 case 8:
601 pcie_add_0x0202000_iobp(0xe9002040);
602 break;
603 }
604 }
605
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300606 pci_update_config32(dev, 0x338, ~(1 << 26), 0);
Stefan Reinauerab365af2013-12-03 12:13:26 -0800607 }
608
609 /* Enable LTR in Root Port. */
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300610 pci_update_config32(dev, 0x64, ~(1 << 11), (1 << 11));
611 pci_update_config32(dev, 0x68, ~(1 << 10), (1 << 10));
Stefan Reinauerab365af2013-12-03 12:13:26 -0800612
Ryan Salsamendi0d9b3602017-06-30 17:15:57 -0700613 pci_update_config32(dev, 0x318, ~(0xffffUL << 16), (0x1414UL << 16));
Stefan Reinauerab365af2013-12-03 12:13:26 -0800614
615 /* Set L1 exit latency in LCAP register. */
616 if (!do_aspm && (pci_read_config8(dev, 0xf5) & 0x1))
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300617 pci_update_config32(dev, 0x4c, ~(0x7 << 15), (0x4 << 15));
Stefan Reinauerab365af2013-12-03 12:13:26 -0800618 else
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300619 pci_update_config32(dev, 0x4c, ~(0x7 << 15), (0x2 << 15));
Stefan Reinauerab365af2013-12-03 12:13:26 -0800620
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300621 pci_update_config32(dev, 0x314, 0x0, 0x743a361b);
Stefan Reinauerab365af2013-12-03 12:13:26 -0800622
623 /* Set Common Clock Exit Latency in MPC register. */
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300624 pci_update_config32(dev, 0xd8, ~(0x7 << 15), (0x3 << 15));
Stefan Reinauerab365af2013-12-03 12:13:26 -0800625
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300626 pci_update_config32(dev, 0x33c, ~0x00ffffff, 0x854c74);
Stefan Reinauerab365af2013-12-03 12:13:26 -0800627
Stefan Reinauerab365af2013-12-03 12:13:26 -0800628 /* Set Invalid Recieve Range Check Enable in MPC register. */
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300629 pci_update_config32(dev, 0xd8, ~0, (1 << 25));
Stefan Reinauerab365af2013-12-03 12:13:26 -0800630
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300631 pci_update_config8(dev, 0xf5, 0x3f, 0);
Stefan Reinauerab365af2013-12-03 12:13:26 -0800632
633 if (rp == 1 || rp == 5 || (is_lp && rp == 6))
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300634 pci_update_config8(dev, 0xf7, ~0xc, 0);
Stefan Reinauerab365af2013-12-03 12:13:26 -0800635
636 /* Set EOI forwarding disable. */
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300637 pci_update_config32(dev, 0xd4, ~0, (1 << 1));
Stefan Reinauerab365af2013-12-03 12:13:26 -0800638
639 /* Set something involving advanced error reporting. */
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300640 pci_update_config32(dev, 0x100, ~((1 << 20) - 1), 0x10001);
Stefan Reinauerab365af2013-12-03 12:13:26 -0800641
642 if (is_lp)
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300643 pci_update_config32(dev, 0x100, ~0, (1 << 29));
Stefan Reinauerab365af2013-12-03 12:13:26 -0800644
645 /* Read and write back write-once capability registers. */
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300646 pci_update_config32(dev, 0x34, ~0, 0);
647 pci_update_config32(dev, 0x40, ~0, 0);
648 pci_update_config32(dev, 0x80, ~0, 0);
649 pci_update_config32(dev, 0x90, ~0, 0);
Aaron Durbin76c37002012-10-30 09:03:43 -0500650}
651
652static void pci_init(struct device *dev)
653{
654 u16 reg16;
655 u32 reg32;
656
657 printk(BIOS_DEBUG, "Initializing PCH PCIe bridge.\n");
658
Aaron Durbinc0254e62013-06-20 01:20:30 -0500659 /* Enable SERR */
660 reg32 = pci_read_config32(dev, PCI_COMMAND);
661 reg32 |= PCI_COMMAND_SERR;
662 pci_write_config32(dev, PCI_COMMAND, reg32);
663
Aaron Durbin76c37002012-10-30 09:03:43 -0500664 /* Enable Bus Master */
665 reg32 = pci_read_config32(dev, PCI_COMMAND);
666 reg32 |= PCI_COMMAND_MASTER;
667 pci_write_config32(dev, PCI_COMMAND, reg32);
668
669 /* Set Cache Line Size to 0x10 */
670 // This has no effect but the OS might expect it
671 pci_write_config8(dev, 0x0c, 0x10);
672
673 reg16 = pci_read_config16(dev, 0x3e);
674 reg16 &= ~(1 << 0); /* disable parity error response */
675 // reg16 &= ~(1 << 1); /* disable SERR */
676 reg16 |= (1 << 2); /* ISA enable */
677 pci_write_config16(dev, 0x3e, reg16);
678
679#ifdef EVEN_MORE_DEBUG
680 reg32 = pci_read_config32(dev, 0x20);
681 printk(BIOS_SPEW, " MBL = 0x%08x\n", reg32);
682 reg32 = pci_read_config32(dev, 0x24);
683 printk(BIOS_SPEW, " PMBL = 0x%08x\n", reg32);
684 reg32 = pci_read_config32(dev, 0x28);
685 printk(BIOS_SPEW, " PMBU32 = 0x%08x\n", reg32);
686 reg32 = pci_read_config32(dev, 0x2c);
687 printk(BIOS_SPEW, " PMLU32 = 0x%08x\n", reg32);
688#endif
689
690 /* Clear errors in status registers */
691 reg16 = pci_read_config16(dev, 0x06);
Aaron Durbin76c37002012-10-30 09:03:43 -0500692 pci_write_config16(dev, 0x06, reg16);
Aaron Durbin76c37002012-10-30 09:03:43 -0500693 reg16 = pci_read_config16(dev, 0x1e);
Aaron Durbin76c37002012-10-30 09:03:43 -0500694 pci_write_config16(dev, 0x1e, reg16);
Aaron Durbin76c37002012-10-30 09:03:43 -0500695}
696
Elyes HAOUAS7a5f7712018-06-08 17:20:38 +0200697static void pch_pcie_enable(struct device *dev)
Aaron Durbin76c37002012-10-30 09:03:43 -0500698{
Aaron Durbinc0254e62013-06-20 01:20:30 -0500699 /* Add this device to the root port config structure. */
700 root_port_init_config(dev);
701
702 /* Check to see if this Root Port should be disabled. */
703 root_port_check_disable(dev);
704
Aaron Durbin76c37002012-10-30 09:03:43 -0500705 /* Power Management init before enumeration */
Aaron Durbinc0254e62013-06-20 01:20:30 -0500706 if (dev->enabled)
Stefan Reinauerab365af2013-12-03 12:13:26 -0800707 pch_pcie_early(dev);
Aaron Durbinc0254e62013-06-20 01:20:30 -0500708
709 /*
710 * When processing the last PCIe root port we can now
711 * update the Root Port Function Number and Hide register.
712 */
713 if (root_port_is_last(dev))
714 root_port_commit_config();
Aaron Durbin76c37002012-10-30 09:03:43 -0500715}
716
Elyes HAOUAS7a5f7712018-06-08 17:20:38 +0200717static void pcie_set_subsystem(struct device *dev, unsigned vendor,
718 unsigned device)
Aaron Durbin76c37002012-10-30 09:03:43 -0500719{
720 /* NOTE: This is not the default position! */
721 if (!vendor || !device) {
722 pci_write_config32(dev, 0x94,
723 pci_read_config32(dev, 0));
724 } else {
725 pci_write_config32(dev, 0x94,
726 ((device & 0xffff) << 16) | (vendor & 0xffff));
727 }
728}
729
730static struct pci_operations pci_ops = {
731 .set_subsystem = pcie_set_subsystem,
732};
733
734static struct device_operations device_ops = {
735 .read_resources = pci_bus_read_resources,
736 .set_resources = pci_dev_set_resources,
737 .enable_resources = pci_bus_enable_resources,
738 .init = pci_init,
739 .enable = pch_pcie_enable,
740 .scan_bus = pciexp_scan_bridge,
741 .ops_pci = &pci_ops,
742};
743
Duncan Laurie74c0d052012-12-17 11:31:40 -0800744static const unsigned short pci_device_ids[] = {
745 /* Lynxpoint Mobile */
746 0x8c10, 0x8c12, 0x8c14, 0x8c16, 0x8c18, 0x8c1a, 0x8c1c, 0x8c1e,
747 /* Lynxpoint Low Power */
748 0x9c10, 0x9c12, 0x9c14, 0x9c16, 0x9c18, 0x9c1a,
749 0
750};
Aaron Durbin76c37002012-10-30 09:03:43 -0500751
752static const struct pci_driver pch_pcie __pci_driver = {
753 .ops = &device_ops,
754 .vendor = PCI_VENDOR_ID_INTEL,
755 .devices = pci_device_ids,
756};