blob: d506057d6177748fad38c47656b9d287d266f6d7 [file] [log] [blame]
Angel Ponsf94ac9a2020-04-05 15:46:48 +02001/* SPDX-License-Identifier: GPL-2.0-only */
2/* This file is part of the coreboot project. */
Duncan Lauriec88c54c2014-04-30 16:36:13 -07003
4#include <console/console.h>
5#include <device/device.h>
6#include <device/pci.h>
7#include <device/pciexp.h>
8#include <device/pci_def.h>
9#include <device/pci_ids.h>
Patrick Rudolphe56189c2018-04-18 10:11:59 +020010#include <device/pci_ops.h>
Julius Werner4ee4bd52014-10-20 13:46:39 -070011#include <soc/gpio.h>
12#include <soc/lpc.h>
13#include <soc/iobp.h>
14#include <soc/pch.h>
15#include <soc/pci_devs.h>
16#include <soc/rcba.h>
17#include <soc/intel/broadwell/chip.h>
18#include <soc/cpu.h>
Wenkai Du83067612014-12-05 14:00:26 -080019#include <delay.h>
Duncan Lauriec88c54c2014-04-30 16:36:13 -070020
Duncan Lauriec88c54c2014-04-30 16:36:13 -070021/* Low Power variant has 6 root ports. */
22#define NUM_ROOT_PORTS 6
23
24struct root_port_config {
25 /* RPFN is a write-once register so keep a copy until it is written */
26 u32 orig_rpfn;
27 u32 new_rpfn;
28 u32 pin_ownership;
29 u32 strpfusecfg1;
30 u32 strpfusecfg2;
31 u32 strpfusecfg3;
32 u32 b0d28f0_32c;
33 u32 b0d28f4_32c;
34 u32 b0d28f5_32c;
35 int coalesce;
36 int gbe_port;
37 int num_ports;
Elyes HAOUAS040aff22018-05-27 16:30:36 +020038 struct device *ports[NUM_ROOT_PORTS];
Duncan Lauriec88c54c2014-04-30 16:36:13 -070039};
40
41static struct root_port_config rpc;
42
Elyes HAOUAS040aff22018-05-27 16:30:36 +020043static inline int root_port_is_first(struct device *dev)
Duncan Lauriec88c54c2014-04-30 16:36:13 -070044{
45 return PCI_FUNC(dev->path.pci.devfn) == 0;
46}
47
Elyes HAOUAS040aff22018-05-27 16:30:36 +020048static inline int root_port_is_last(struct device *dev)
Duncan Lauriec88c54c2014-04-30 16:36:13 -070049{
50 return PCI_FUNC(dev->path.pci.devfn) == (rpc.num_ports - 1);
51}
52
53/* Root ports are numbered 1..N in the documentation. */
Elyes HAOUAS040aff22018-05-27 16:30:36 +020054static inline int root_port_number(struct device *dev)
Duncan Lauriec88c54c2014-04-30 16:36:13 -070055{
56 return PCI_FUNC(dev->path.pci.devfn) + 1;
57}
58
59static void root_port_config_update_gbe_port(void)
60{
61 /* Is the Gbe Port enabled? */
62 if (!((rpc.strpfusecfg1 >> 19) & 1))
63 return;
64
65 switch ((rpc.strpfusecfg1 >> 16) & 0x7) {
66 case 0:
67 rpc.gbe_port = 3;
68 break;
69 case 1:
70 rpc.gbe_port = 4;
71 break;
72 case 2:
73 case 3:
74 case 4:
75 case 5:
76 /* Lanes 0-4 of Root Port 5. */
77 rpc.gbe_port = 5;
78 break;
79 default:
80 printk(BIOS_DEBUG, "Invalid GbE Port Selection.\n");
81 }
82}
83
Elyes HAOUAS040aff22018-05-27 16:30:36 +020084static void pcie_iosf_port_grant_count(struct device *dev)
Kenji Chen87d4a202014-09-24 01:18:26 +080085{
86 u8 update_val;
Patrick Georgie8f2ef52016-07-29 18:53:34 +020087 u32 rpcd = (pci_read_config32(dev, 0xfc) >> 14) & 0x3;
Kenji Chen87d4a202014-09-24 01:18:26 +080088
89 switch (rpcd) {
90 case 1:
91 case 3:
92 update_val = 0x02;
93 break;
94 case 2:
95 update_val = 0x22;
96 break;
97 default:
98 update_val = 0x00;
99 break;
100 }
101
102 RCBA32(0x103C) = (RCBA32(0x103C) & (~0xff)) | update_val;
103}
104
Elyes HAOUAS040aff22018-05-27 16:30:36 +0200105static void root_port_init_config(struct device *dev)
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700106{
107 int rp;
Martin Roth2b2ff7f2015-12-18 10:46:59 -0700108 u32 data = 0;
Kenji Chene383feb2014-09-26 03:14:57 +0800109 u8 resp, id;
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700110
111 if (root_port_is_first(dev)) {
112 rpc.orig_rpfn = RCBA32(RPFN);
113 rpc.new_rpfn = rpc.orig_rpfn;
114 rpc.num_ports = NUM_ROOT_PORTS;
115 rpc.gbe_port = -1;
Kenji Chen87d4a202014-09-24 01:18:26 +0800116 /* RP0 f5[3:0] = 0101b*/
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300117 pci_update_config8(dev, 0xf5, ~0xa, 0x5);
Kenji Chen87d4a202014-09-24 01:18:26 +0800118
119 pcie_iosf_port_grant_count(dev);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700120
121 rpc.pin_ownership = pci_read_config32(dev, 0x410);
122 root_port_config_update_gbe_port();
123
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300124 pci_update_config8(dev, 0xe2, ~(3 << 4), (3 << 4));
Kyösti Mälkki8950cfb2019-07-13 22:16:25 +0300125 config_t *config = config_of(dev);
126 rpc.coalesce = config->pcie_port_coalesce;
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700127 }
128
129 rp = root_port_number(dev);
130 if (rp > rpc.num_ports) {
131 printk(BIOS_ERR, "Found Root Port %d, expecting %d\n",
132 rp, rpc.num_ports);
133 return;
134 }
135
136 /* Read the fuse configuration and pin ownership. */
137 switch (rp) {
138 case 1:
139 rpc.strpfusecfg1 = pci_read_config32(dev, 0xfc);
140 rpc.b0d28f0_32c = pci_read_config32(dev, 0x32c);
141 break;
142 case 5:
143 rpc.strpfusecfg2 = pci_read_config32(dev, 0xfc);
144 rpc.b0d28f4_32c = pci_read_config32(dev, 0x32c);
145 break;
146 case 6:
147 rpc.b0d28f5_32c = pci_read_config32(dev, 0x32c);
148 rpc.strpfusecfg3 = pci_read_config32(dev, 0xfc);
149 break;
150 default:
151 break;
152 }
153
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300154 pci_update_config32(dev, 0x418, 0, 0x02000430);
Kenji Chene383feb2014-09-26 03:14:57 +0800155
Kenji Chene383feb2014-09-26 03:14:57 +0800156 if (root_port_is_first(dev)) {
Kenji Chene8f36642014-10-04 02:59:06 +0800157 /*
158 * set RP0 PCICFG E2h[5:4] = 11b and E1h[6] = 1
159 * before configuring ASPM
160 */
Kenji Chene383feb2014-09-26 03:14:57 +0800161 id = 0xe0 + (u8)(RCBA32(RPFN) & 0x07);
162 pch_iobp_exec(0xE00000E0, IOBP_PCICFG_READ, id, &data, &resp);
Kenji Chene8f36642014-10-04 02:59:06 +0800163 data |= ((0x30 << 16) | (0x40 << 8));
Kenji Chene383feb2014-09-26 03:14:57 +0800164 pch_iobp_exec(0xE00000E0, IOBP_PCICFG_WRITE, id, &data, &resp);
165 }
166
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700167 /* Cache pci device. */
168 rpc.ports[rp - 1] = dev;
169}
170
171/* Update devicetree with new Root Port function number assignment */
172static void pch_pcie_device_set_func(int index, int pci_func)
173{
Elyes HAOUAS040aff22018-05-27 16:30:36 +0200174 struct device *dev;
Lee Leahy23602df2017-03-16 19:00:37 -0700175 unsigned int new_devfn;
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700176
177 dev = rpc.ports[index];
178
179 /* Set the new PCI function field for this Root Port. */
180 rpc.new_rpfn &= ~RPFN_FNMASK(index);
181 rpc.new_rpfn |= RPFN_FNSET(index, pci_func);
182
183 /* Determine the new devfn for this port */
184 new_devfn = PCI_DEVFN(PCH_DEV_SLOT_PCIE, pci_func);
185
186 if (dev->path.pci.devfn != new_devfn) {
187 printk(BIOS_DEBUG,
188 "PCH: PCIe map %02x.%1x -> %02x.%1x\n",
189 PCI_SLOT(dev->path.pci.devfn),
190 PCI_FUNC(dev->path.pci.devfn),
191 PCI_SLOT(new_devfn), PCI_FUNC(new_devfn));
192
193 dev->path.pci.devfn = new_devfn;
194 }
195}
196
197static void pcie_enable_clock_gating(void)
198{
199 int i;
200 int enabled_ports = 0;
Kane Chen4fef5a22014-08-27 15:21:32 -0700201 int is_broadwell = !!(cpu_family_model() == BROADWELL_FAMILY_ULT);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700202
203 for (i = 0; i < rpc.num_ports; i++) {
Elyes HAOUAS040aff22018-05-27 16:30:36 +0200204 struct device *dev;
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700205 int rp;
206
207 dev = rpc.ports[i];
208 rp = root_port_number(dev);
209
210 if (!dev->enabled) {
211 /* Configure shared resource clock gating. */
212 if (rp == 1 || rp == 5 || rp == 6)
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300213 pci_update_config8(dev, 0xe1, 0xc3, 0x3c);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700214
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300215 pci_update_config8(dev, 0xe2, ~(3 << 4), (3 << 4));
216 pci_update_config32(dev, 0x420, ~(1 << 31), (1 << 31));
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700217
218 /* Per-Port CLKREQ# handling. */
219 if (gpio_is_native(18 + rp - 1))
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300220 pci_update_config32(dev, 0x420, ~0, (3 << 29));
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700221
222 /* Enable static clock gating. */
223 if (rp == 1 && !rpc.ports[1]->enabled &&
224 !rpc.ports[2]->enabled && !rpc.ports[3]->enabled) {
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300225 pci_update_config8(dev, 0xe2, ~1, 1);
226 pci_update_config8(dev, 0xe1, 0x7f, 0x80);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700227 } else if (rp == 5 || rp == 6) {
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);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700230 }
231 continue;
232 }
233
234 enabled_ports++;
235
236 /* Enable dynamic clock gating. */
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300237 pci_update_config8(dev, 0xe1, 0xfc, 0x03);
238 pci_update_config8(dev, 0xe2, ~(1 << 6), (1 << 6));
239 pci_update_config8(dev, 0xe8, ~(3 << 2), (2 << 2));
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700240
241 /* Update PECR1 register. */
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300242 pci_update_config8(dev, 0xe8, ~0, 3);
Kane Chen4fef5a22014-08-27 15:21:32 -0700243 if (is_broadwell) {
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300244 pci_update_config32(dev, 0x324, ~((1 << 5) | (1 << 14)),
Kane Chen4fef5a22014-08-27 15:21:32 -0700245 ((1 << 5) | (1 << 14)));
246 } else {
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300247 pci_update_config32(dev, 0x324, ~(1 << 5), (1 << 5));
Kane Chen4fef5a22014-08-27 15:21:32 -0700248 }
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700249 /* Per-Port CLKREQ# handling. */
250 if (gpio_is_native(18 + rp - 1))
Kenji Chene8f36642014-10-04 02:59:06 +0800251 /*
252 * In addition to D28Fx PCICFG 420h[30:29] = 11b,
253 * set 420h[17] = 0b and 420[0] = 1b for L1 SubState.
254 */
Lee Leahy6ef51922017-03-17 10:56:08 -0700255 pci_update_config32(dev, 0x420, ~0x20000,
256 (3 << 29) | 1);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700257
258 /* Configure shared resource clock gating. */
259 if (rp == 1 || rp == 5 || rp == 6)
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300260 pci_update_config8(dev, 0xe1, 0xc3, 0x3c);
Duncan Laurie446fb8e2014-08-08 09:59:43 -0700261
262 /* CLKREQ# VR Idle Enable */
263 RCBA32_OR(0x2b1c, (1 << (16 + i)));
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700264 }
265
266 if (!enabled_ports)
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300267 pci_update_config8(rpc.ports[0], 0xe1, ~(1 << 6), (1 << 6));
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700268}
269
270static void root_port_commit_config(void)
271{
272 int i;
273
274 /* If the first root port is disabled the coalesce ports. */
275 if (!rpc.ports[0]->enabled)
276 rpc.coalesce = 1;
277
278 /* Perform clock gating configuration. */
279 pcie_enable_clock_gating();
280
281 for (i = 0; i < rpc.num_ports; i++) {
Elyes HAOUAS040aff22018-05-27 16:30:36 +0200282 struct device *dev;
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700283 u32 reg32;
Wenkai Du83067612014-12-05 14:00:26 -0800284 int n = 0;
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700285
286 dev = rpc.ports[i];
287
288 if (dev == NULL) {
289 printk(BIOS_ERR, "Root Port %d device is NULL?\n", i+1);
290 continue;
291 }
292
293 if (dev->enabled)
294 continue;
295
296 printk(BIOS_DEBUG, "%s: Disabling device\n", dev_path(dev));
297
Wenkai Du83067612014-12-05 14:00:26 -0800298 /* 8.2 Configuration of PCI Express Root Ports */
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300299 pci_update_config32(dev, 0x338, ~(1 << 26), 1 << 26);
Wenkai Du83067612014-12-05 14:00:26 -0800300
301 do {
302 reg32 = pci_read_config32(dev, 0x328);
303 n++;
Duncan Lauriecad2b7b2015-01-14 17:30:20 -0800304 if (((reg32 & 0xff000000) == 0x01000000) || (n > 50))
Wenkai Du83067612014-12-05 14:00:26 -0800305 break;
306 udelay(100);
307 } while (1);
308
Duncan Lauriecad2b7b2015-01-14 17:30:20 -0800309 if (n > 50)
Wenkai Du83067612014-12-05 14:00:26 -0800310 printk(BIOS_DEBUG, "%s: Timeout waiting for 328h\n",
311 dev_path(dev));
312
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300313 pci_update_config32(dev, 0x408, ~(1 << 27), 1 << 27);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700314
315 /* Disable this device if possible */
316 pch_disable_devfn(dev);
317 }
318
319 if (rpc.coalesce) {
320 int current_func;
321
322 /* For all Root Ports N enabled ports get assigned the lower
323 * PCI function number. The disabled ones get upper PCI
324 * function numbers. */
325 current_func = 0;
326 for (i = 0; i < rpc.num_ports; i++) {
327 if (!rpc.ports[i]->enabled)
328 continue;
329 pch_pcie_device_set_func(i, current_func);
330 current_func++;
331 }
332
333 /* Allocate the disabled devices' PCI function number. */
334 for (i = 0; i < rpc.num_ports; i++) {
335 if (rpc.ports[i]->enabled)
336 continue;
337 pch_pcie_device_set_func(i, current_func);
338 current_func++;
339 }
340 }
341
342 printk(BIOS_SPEW, "PCH: RPFN 0x%08x -> 0x%08x\n",
343 rpc.orig_rpfn, rpc.new_rpfn);
344 RCBA32(RPFN) = rpc.new_rpfn;
345}
346
Elyes HAOUAS040aff22018-05-27 16:30:36 +0200347static void root_port_mark_disable(struct device *dev)
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700348{
349 /* Mark device as disabled. */
350 dev->enabled = 0;
351 /* Mark device to be hidden. */
352 rpc.new_rpfn |= RPFN_HIDE(PCI_FUNC(dev->path.pci.devfn));
353}
354
Elyes HAOUAS040aff22018-05-27 16:30:36 +0200355static void root_port_check_disable(struct device *dev)
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700356{
357 int rp;
358
359 /* Device already disabled. */
360 if (!dev->enabled) {
361 root_port_mark_disable(dev);
362 return;
363 }
364
365 rp = root_port_number(dev);
366
367 /* Is the GbE port mapped to this Root Port? */
368 if (rp == rpc.gbe_port) {
369 root_port_mark_disable(dev);
370 return;
371 }
372
373 /* Check Root Port Configuration. */
374 switch (rp) {
Lee Leahy6ef51922017-03-17 10:56:08 -0700375 case 2:
376 /* Root Port 2 is disabled for all lane configurations
377 * but config 00b (4x1 links). */
378 if ((rpc.strpfusecfg1 >> 14) & 0x3) {
379 root_port_mark_disable(dev);
380 return;
381 }
382 break;
383 case 3:
384 /* Root Port 3 is disabled in config 11b (1x4 links). */
385 if (((rpc.strpfusecfg1 >> 14) & 0x3) == 0x3) {
386 root_port_mark_disable(dev);
387 return;
388 }
389 break;
390 case 4:
391 /* Root Port 4 is disabled in configs 11b (1x4 links)
392 * and 10b (2x2 links). */
393 if ((rpc.strpfusecfg1 >> 14) & 0x2) {
394 root_port_mark_disable(dev);
395 return;
396 }
397 break;
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700398 }
399
400 /* Check Pin Ownership. */
401 switch (rp) {
402 case 1:
403 /* Bit 0 is Root Port 1 ownership. */
404 if ((rpc.pin_ownership & 0x1) == 0) {
405 root_port_mark_disable(dev);
406 return;
407 }
408 break;
409 case 2:
410 /* Bit 2 is Root Port 2 ownership. */
411 if ((rpc.pin_ownership & 0x4) == 0) {
412 root_port_mark_disable(dev);
413 return;
414 }
415 break;
416 case 6:
417 /* Bits 7:4 are Root Port 6 pin-lane ownership. */
418 if ((rpc.pin_ownership & 0xf0) == 0) {
419 root_port_mark_disable(dev);
420 return;
421 }
422 break;
423 }
424}
425
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700426static void pcie_add_0x0202000_iobp(u32 reg)
427{
428 u32 reg32;
429
430 reg32 = pch_iobp_read(reg);
431 reg32 += (0x2 << 16) | (0x2 << 8);
432 pch_iobp_write(reg, reg32);
433}
434
435static void pch_pcie_early(struct device *dev)
436{
Kyösti Mälkki8950cfb2019-07-13 22:16:25 +0300437 config_t *config = config_of(dev);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700438 int do_aspm = 0;
439 int rp = root_port_number(dev);
440
441 switch (rp) {
442 case 1:
443 case 2:
444 case 3:
445 case 4:
446 /*
Martin Rothde7ed6f2014-12-07 14:58:18 -0700447 * Bits 31:28 of b0d28f0 0x32c register correspond to
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700448 * Root Ports 4:1.
449 */
450 do_aspm = !!(rpc.b0d28f0_32c & (1 << (28 + rp - 1)));
451 break;
452 case 5:
453 /*
Martin Rothde7ed6f2014-12-07 14:58:18 -0700454 * Bit 28 of b0d28f4 0x32c register correspond to
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700455 * Root Ports 4:1.
456 */
457 do_aspm = !!(rpc.b0d28f4_32c & (1 << 28));
458 break;
459 case 6:
460 /*
Martin Rothde7ed6f2014-12-07 14:58:18 -0700461 * Bit 28 of b0d28f5 0x32c register correspond to
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700462 * Root Ports 4:1.
463 */
464 do_aspm = !!(rpc.b0d28f5_32c & (1 << 28));
465 break;
466 }
467
468 /* Allow ASPM to be forced on in devicetree */
Kyösti Mälkki8950cfb2019-07-13 22:16:25 +0300469 if ((config->pcie_port_force_aspm & (1 << (rp - 1))))
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700470 do_aspm = 1;
471
472 printk(BIOS_DEBUG, "PCIe Root Port %d ASPM is %sabled\n",
473 rp, do_aspm ? "en" : "dis");
474
475 if (do_aspm) {
476 /* Set ASPM bits in MPC2 register. */
Lee Leahy6ef51922017-03-17 10:56:08 -0700477 pci_update_config32(dev, 0xd4, ~(0x3 << 2),
478 (1 << 4) | (0x2 << 2));
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700479
480 /* Set unique clock exit latency in MPC register. */
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300481 pci_update_config32(dev, 0xd8, ~(0x7 << 18), (0x7 << 18));
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700482
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700483 switch (rp) {
484 case 1:
485 pcie_add_0x0202000_iobp(0xe9002440);
486 break;
487 case 2:
488 pcie_add_0x0202000_iobp(0xe9002640);
489 break;
490 case 3:
491 pcie_add_0x0202000_iobp(0xe9000840);
492 break;
493 case 4:
494 pcie_add_0x0202000_iobp(0xe9000a40);
495 break;
496 case 5:
497 pcie_add_0x0202000_iobp(0xe9000c40);
498 pcie_add_0x0202000_iobp(0xe9000e40);
499 pcie_add_0x0202000_iobp(0xe9001040);
500 pcie_add_0x0202000_iobp(0xe9001240);
501 break;
502 case 6:
503 /* Update IOBP based on lane ownership. */
504 if (rpc.pin_ownership & (1 << 4))
505 pcie_add_0x0202000_iobp(0xea002040);
506 if (rpc.pin_ownership & (1 << 5))
507 pcie_add_0x0202000_iobp(0xea002240);
508 if (rpc.pin_ownership & (1 << 6))
509 pcie_add_0x0202000_iobp(0xea002440);
510 if (rpc.pin_ownership & (1 << 7))
511 pcie_add_0x0202000_iobp(0xea002640);
512 break;
513 }
514
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300515 pci_update_config32(dev, 0x338, ~(1 << 26), 0);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700516 }
517
Kenji Chenc373f502014-09-26 02:48:16 +0800518 /* Enable LTR in Root Port. Disable OBFF. */
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300519 pci_update_config32(dev, 0x64, ~(1 << 11) & ~(3 << 18), (1 << 11));
520 pci_update_config32(dev, 0x68, ~(1 << 10), (1 << 10));
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700521
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300522 pci_update_config32(dev, 0x318, ~(0xffff << 16), (0x1414 << 16));
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700523
524 /* Set L1 exit latency in LCAP register. */
525 if (!do_aspm && (pci_read_config8(dev, 0xf5) & 0x1))
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300526 pci_update_config32(dev, 0x4c, ~(0x7 << 15), (0x4 << 15));
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700527 else
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300528 pci_update_config32(dev, 0x4c, ~(0x7 << 15), (0x2 << 15));
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700529
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300530 pci_update_config32(dev, 0x314, 0x0, 0x743a361b);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700531
532 /* Set Common Clock Exit Latency in MPC register. */
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300533 pci_update_config32(dev, 0xd8, ~(0x7 << 15), (0x3 << 15));
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700534
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300535 pci_update_config32(dev, 0x33c, ~0x00ffffff, 0x854d74);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700536
Martin Rothde7ed6f2014-12-07 14:58:18 -0700537 /* Set Invalid Receive Range Check Enable in MPC register. */
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300538 pci_update_config32(dev, 0xd8, ~0, (1 << 25));
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700539
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300540 pci_update_config8(dev, 0xf5, 0x0f, 0);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700541
Kenji Chen94fea492014-09-30 14:17:35 +0800542 /* Set AER Extended Cap ID to 01h and Next Cap Pointer to 200h. */
Julius Wernercd49cce2019-03-05 16:53:33 -0800543 if (CONFIG(PCIEXP_AER))
Youness Alaoui71616782018-05-04 15:34:06 -0400544 pci_update_config32(dev, 0x100, ~(1 << 29) & ~0xfffff,
545 (1 << 29) | 0x10001);
546 else
547 pci_update_config32(dev, 0x100, ~(1 << 29) & ~0xfffff,
548 (1 << 29));
Kenji Chen8ef55ee2014-09-25 21:34:42 +0800549
Kenji Chen94fea492014-09-30 14:17:35 +0800550 /* Set L1 Sub-State Cap ID to 1Eh and Next Cap Pointer to None. */
Julius Wernercd49cce2019-03-05 16:53:33 -0800551 if (CONFIG(PCIEXP_L1_SUB_STATE))
Youness Alaoui1f64b012018-05-04 15:33:54 -0400552 pci_update_config32(dev, 0x200, ~0xfffff, 0x001e);
553 else
554 pci_update_config32(dev, 0x200, ~0xfffff, 0);
Kenji Chen94fea492014-09-30 14:17:35 +0800555
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300556 pci_update_config32(dev, 0x320, ~(3 << 20) & ~(7 << 6),
Kenji Chenc373f502014-09-26 02:48:16 +0800557 (1 << 20) | (3 << 6));
558 /* Enable Relaxed Order from Root Port. */
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300559 pci_update_config32(dev, 0x320, ~(3 << 23), (3 << 23));
Kenji Chenc373f502014-09-26 02:48:16 +0800560
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700561 if (rp == 1 || rp == 5 || rp == 6)
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300562 pci_update_config8(dev, 0xf7, ~0xc, 0);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700563
564 /* Set EOI forwarding disable. */
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300565 pci_update_config32(dev, 0xd4, ~0, (1 << 1));
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700566
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700567 /* Read and write back write-once capability registers. */
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300568 pci_update_config32(dev, 0x34, ~0, 0);
569 pci_update_config32(dev, 0x40, ~0, 0);
570 pci_update_config32(dev, 0x80, ~0, 0);
571 pci_update_config32(dev, 0x90, ~0, 0);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700572}
573
574static void pch_pcie_init(struct device *dev)
575{
576 u16 reg16;
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700577
578 printk(BIOS_DEBUG, "Initializing PCH PCIe bridge.\n");
579
580 /* Enable SERR */
Elyes HAOUASb887adf2020-04-29 10:42:34 +0200581 pci_or_config16(dev, PCI_COMMAND, PCI_COMMAND_SERR);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700582
583 /* Enable Bus Master */
Elyes HAOUASb887adf2020-04-29 10:42:34 +0200584 pci_or_config16(dev, PCI_COMMAND, PCI_COMMAND_MASTER);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700585
586 /* Set Cache Line Size to 0x10 */
587 pci_write_config8(dev, 0x0c, 0x10);
588
Kyösti Mälkkidf128a52019-09-21 18:35:37 +0300589 reg16 = pci_read_config16(dev, PCI_BRIDGE_CONTROL);
590 reg16 &= ~PCI_BRIDGE_CTL_PARITY;
591 reg16 |= PCI_BRIDGE_CTL_NO_ISA;
592 pci_write_config16(dev, PCI_BRIDGE_CONTROL, reg16);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700593
594#ifdef EVEN_MORE_DEBUG
Elyes HAOUASb887adf2020-04-29 10:42:34 +0200595 u32 reg32;
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700596 reg32 = pci_read_config32(dev, 0x20);
597 printk(BIOS_SPEW, " MBL = 0x%08x\n", reg32);
598 reg32 = pci_read_config32(dev, 0x24);
599 printk(BIOS_SPEW, " PMBL = 0x%08x\n", reg32);
600 reg32 = pci_read_config32(dev, 0x28);
601 printk(BIOS_SPEW, " PMBU32 = 0x%08x\n", reg32);
602 reg32 = pci_read_config32(dev, 0x2c);
603 printk(BIOS_SPEW, " PMLU32 = 0x%08x\n", reg32);
604#endif
605
606 /* Clear errors in status registers */
607 reg16 = pci_read_config16(dev, 0x06);
608 pci_write_config16(dev, 0x06, reg16);
609 reg16 = pci_read_config16(dev, 0x1e);
610 pci_write_config16(dev, 0x1e, reg16);
611}
612
Elyes HAOUAS040aff22018-05-27 16:30:36 +0200613static void pch_pcie_enable(struct device *dev)
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700614{
615 /* Add this device to the root port config structure. */
616 root_port_init_config(dev);
617
618 /* Check to see if this Root Port should be disabled. */
619 root_port_check_disable(dev);
620
621 /* Power Management init before enumeration */
622 if (dev->enabled)
623 pch_pcie_early(dev);
624
625 /*
626 * When processing the last PCIe root port we can now
627 * update the Root Port Function Number and Hide register.
628 */
629 if (root_port_is_last(dev))
630 root_port_commit_config();
631}
632
Elyes HAOUAS040aff22018-05-27 16:30:36 +0200633static void pcie_set_L1_ss_max_latency(struct device *dev, unsigned int off)
Kenji Chenb71d9b82014-10-10 03:08:15 +0800634{
635 /* Set max snoop and non-snoop latency for Broadwell */
Subrata Baniked6996f2019-03-25 21:49:39 +0530636 pci_write_config32(dev, off,
637 PCIE_LTR_MAX_NO_SNOOP_LATENCY_3146US << 16 |
638 PCIE_LTR_MAX_SNOOP_LATENCY_3146US);
Kenji Chenb71d9b82014-10-10 03:08:15 +0800639}
640
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700641static struct pci_operations pcie_ops = {
Subrata Banik15ccbf02019-03-20 15:09:44 +0530642 .set_subsystem = pci_dev_set_subsystem,
Kenji Chenb71d9b82014-10-10 03:08:15 +0800643 .set_L1_ss_latency = pcie_set_L1_ss_max_latency,
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700644};
645
646static struct device_operations device_ops = {
647 .read_resources = pci_bus_read_resources,
648 .set_resources = pci_dev_set_resources,
649 .enable_resources = pci_bus_enable_resources,
650 .init = pch_pcie_init,
651 .enable = pch_pcie_enable,
652 .scan_bus = pciexp_scan_bridge,
653 .ops_pci = &pcie_ops,
654};
655
656static const unsigned short pcie_device_ids[] = {
657 /* Lynxpoint-LP */
658 0x9c10, 0x9c12, 0x9c14, 0x9c16, 0x9c18, 0x9c1a,
659 /* WildcatPoint */
660 0x9c90, 0x9c92, 0x9c94, 0x9c96, 0x9c98, 0x9c9a, 0x2448,
661 0
662};
663
664static const struct pci_driver pch_pcie __pci_driver = {
665 .ops = &device_ops,
666 .vendor = PCI_VENDOR_ID_INTEL,
667 .devices = pcie_device_ids,
668};