blob: 01ee06895f9773ebd6e5a7887a87333b9ebe88f7 [file] [log] [blame]
Angel Ponsf94ac9a2020-04-05 15:46:48 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Duncan Lauriec88c54c2014-04-30 16:36:13 -07002
3#include <console/console.h>
4#include <device/device.h>
5#include <device/pci.h>
6#include <device/pciexp.h>
7#include <device/pci_def.h>
8#include <device/pci_ids.h>
Patrick Rudolphe56189c2018-04-18 10:11:59 +02009#include <device/pci_ops.h>
Julius Werner4ee4bd52014-10-20 13:46:39 -070010#include <soc/gpio.h>
11#include <soc/lpc.h>
12#include <soc/iobp.h>
13#include <soc/pch.h>
14#include <soc/pci_devs.h>
15#include <soc/rcba.h>
16#include <soc/intel/broadwell/chip.h>
17#include <soc/cpu.h>
Wenkai Du83067612014-12-05 14:00:26 -080018#include <delay.h>
Duncan Lauriec88c54c2014-04-30 16:36:13 -070019
Duncan Lauriec88c54c2014-04-30 16:36:13 -070020/* Low Power variant has 6 root ports. */
Angel Pons2ead3632020-09-24 16:50:05 +020021#define MAX_NUM_ROOT_PORTS 6
Duncan Lauriec88c54c2014-04-30 16:36:13 -070022
23struct root_port_config {
24 /* RPFN is a write-once register so keep a copy until it is written */
25 u32 orig_rpfn;
26 u32 new_rpfn;
27 u32 pin_ownership;
28 u32 strpfusecfg1;
29 u32 strpfusecfg2;
30 u32 strpfusecfg3;
31 u32 b0d28f0_32c;
32 u32 b0d28f4_32c;
33 u32 b0d28f5_32c;
34 int coalesce;
35 int gbe_port;
36 int num_ports;
Angel Pons2ead3632020-09-24 16:50:05 +020037 struct device *ports[MAX_NUM_ROOT_PORTS];
Duncan Lauriec88c54c2014-04-30 16:36:13 -070038};
39
40static struct root_port_config rpc;
41
Elyes HAOUAS040aff22018-05-27 16:30:36 +020042static inline int root_port_is_first(struct device *dev)
Duncan Lauriec88c54c2014-04-30 16:36:13 -070043{
44 return PCI_FUNC(dev->path.pci.devfn) == 0;
45}
46
Elyes HAOUAS040aff22018-05-27 16:30:36 +020047static inline int root_port_is_last(struct device *dev)
Duncan Lauriec88c54c2014-04-30 16:36:13 -070048{
49 return PCI_FUNC(dev->path.pci.devfn) == (rpc.num_ports - 1);
50}
51
52/* Root ports are numbered 1..N in the documentation. */
Elyes HAOUAS040aff22018-05-27 16:30:36 +020053static inline int root_port_number(struct device *dev)
Duncan Lauriec88c54c2014-04-30 16:36:13 -070054{
55 return PCI_FUNC(dev->path.pci.devfn) + 1;
56}
57
58static void root_port_config_update_gbe_port(void)
59{
60 /* Is the Gbe Port enabled? */
61 if (!((rpc.strpfusecfg1 >> 19) & 1))
62 return;
63
64 switch ((rpc.strpfusecfg1 >> 16) & 0x7) {
65 case 0:
66 rpc.gbe_port = 3;
67 break;
68 case 1:
69 rpc.gbe_port = 4;
70 break;
71 case 2:
72 case 3:
73 case 4:
74 case 5:
75 /* Lanes 0-4 of Root Port 5. */
76 rpc.gbe_port = 5;
77 break;
78 default:
79 printk(BIOS_DEBUG, "Invalid GbE Port Selection.\n");
80 }
81}
82
Elyes HAOUAS040aff22018-05-27 16:30:36 +020083static void pcie_iosf_port_grant_count(struct device *dev)
Kenji Chen87d4a202014-09-24 01:18:26 +080084{
85 u8 update_val;
Patrick Georgie8f2ef52016-07-29 18:53:34 +020086 u32 rpcd = (pci_read_config32(dev, 0xfc) >> 14) & 0x3;
Kenji Chen87d4a202014-09-24 01:18:26 +080087
88 switch (rpcd) {
89 case 1:
90 case 3:
91 update_val = 0x02;
92 break;
93 case 2:
94 update_val = 0x22;
95 break;
96 default:
97 update_val = 0x00;
98 break;
99 }
100
101 RCBA32(0x103C) = (RCBA32(0x103C) & (~0xff)) | update_val;
102}
103
Elyes HAOUAS040aff22018-05-27 16:30:36 +0200104static void root_port_init_config(struct device *dev)
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700105{
106 int rp;
Martin Roth2b2ff7f2015-12-18 10:46:59 -0700107 u32 data = 0;
Kenji Chene383feb2014-09-26 03:14:57 +0800108 u8 resp, id;
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700109
110 if (root_port_is_first(dev)) {
111 rpc.orig_rpfn = RCBA32(RPFN);
112 rpc.new_rpfn = rpc.orig_rpfn;
Angel Pons2ead3632020-09-24 16:50:05 +0200113 rpc.num_ports = MAX_NUM_ROOT_PORTS;
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700114 rpc.gbe_port = -1;
Kenji Chen87d4a202014-09-24 01:18:26 +0800115 /* RP0 f5[3:0] = 0101b*/
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300116 pci_update_config8(dev, 0xf5, ~0xa, 0x5);
Kenji Chen87d4a202014-09-24 01:18:26 +0800117
118 pcie_iosf_port_grant_count(dev);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700119
120 rpc.pin_ownership = pci_read_config32(dev, 0x410);
121 root_port_config_update_gbe_port();
122
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300123 pci_update_config8(dev, 0xe2, ~(3 << 4), (3 << 4));
Kyösti Mälkki8950cfb2019-07-13 22:16:25 +0300124 config_t *config = config_of(dev);
125 rpc.coalesce = config->pcie_port_coalesce;
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700126 }
127
128 rp = root_port_number(dev);
129 if (rp > rpc.num_ports) {
130 printk(BIOS_ERR, "Found Root Port %d, expecting %d\n",
131 rp, rpc.num_ports);
132 return;
133 }
134
135 /* Read the fuse configuration and pin ownership. */
136 switch (rp) {
137 case 1:
138 rpc.strpfusecfg1 = pci_read_config32(dev, 0xfc);
139 rpc.b0d28f0_32c = pci_read_config32(dev, 0x32c);
140 break;
141 case 5:
142 rpc.strpfusecfg2 = pci_read_config32(dev, 0xfc);
143 rpc.b0d28f4_32c = pci_read_config32(dev, 0x32c);
144 break;
145 case 6:
146 rpc.b0d28f5_32c = pci_read_config32(dev, 0x32c);
147 rpc.strpfusecfg3 = pci_read_config32(dev, 0xfc);
148 break;
149 default:
150 break;
151 }
152
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300153 pci_update_config32(dev, 0x418, 0, 0x02000430);
Kenji Chene383feb2014-09-26 03:14:57 +0800154
Kenji Chene383feb2014-09-26 03:14:57 +0800155 if (root_port_is_first(dev)) {
Kenji Chene8f36642014-10-04 02:59:06 +0800156 /*
157 * set RP0 PCICFG E2h[5:4] = 11b and E1h[6] = 1
158 * before configuring ASPM
159 */
Kenji Chene383feb2014-09-26 03:14:57 +0800160 id = 0xe0 + (u8)(RCBA32(RPFN) & 0x07);
161 pch_iobp_exec(0xE00000E0, IOBP_PCICFG_READ, id, &data, &resp);
Kenji Chene8f36642014-10-04 02:59:06 +0800162 data |= ((0x30 << 16) | (0x40 << 8));
Kenji Chene383feb2014-09-26 03:14:57 +0800163 pch_iobp_exec(0xE00000E0, IOBP_PCICFG_WRITE, id, &data, &resp);
164 }
165
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700166 /* Cache pci device. */
167 rpc.ports[rp - 1] = dev;
168}
169
170/* Update devicetree with new Root Port function number assignment */
171static void pch_pcie_device_set_func(int index, int pci_func)
172{
Elyes HAOUAS040aff22018-05-27 16:30:36 +0200173 struct device *dev;
Lee Leahy23602df2017-03-16 19:00:37 -0700174 unsigned int new_devfn;
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700175
176 dev = rpc.ports[index];
177
178 /* Set the new PCI function field for this Root Port. */
179 rpc.new_rpfn &= ~RPFN_FNMASK(index);
180 rpc.new_rpfn |= RPFN_FNSET(index, pci_func);
181
182 /* Determine the new devfn for this port */
183 new_devfn = PCI_DEVFN(PCH_DEV_SLOT_PCIE, pci_func);
184
185 if (dev->path.pci.devfn != new_devfn) {
186 printk(BIOS_DEBUG,
187 "PCH: PCIe map %02x.%1x -> %02x.%1x\n",
188 PCI_SLOT(dev->path.pci.devfn),
189 PCI_FUNC(dev->path.pci.devfn),
190 PCI_SLOT(new_devfn), PCI_FUNC(new_devfn));
191
192 dev->path.pci.devfn = new_devfn;
193 }
194}
195
196static void pcie_enable_clock_gating(void)
197{
198 int i;
199 int enabled_ports = 0;
Kane Chen4fef5a22014-08-27 15:21:32 -0700200 int is_broadwell = !!(cpu_family_model() == BROADWELL_FAMILY_ULT);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700201
202 for (i = 0; i < rpc.num_ports; i++) {
Elyes HAOUAS040aff22018-05-27 16:30:36 +0200203 struct device *dev;
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700204 int rp;
205
206 dev = rpc.ports[i];
207 rp = root_port_number(dev);
208
209 if (!dev->enabled) {
210 /* Configure shared resource clock gating. */
211 if (rp == 1 || rp == 5 || rp == 6)
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300212 pci_update_config8(dev, 0xe1, 0xc3, 0x3c);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700213
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300214 pci_update_config8(dev, 0xe2, ~(3 << 4), (3 << 4));
215 pci_update_config32(dev, 0x420, ~(1 << 31), (1 << 31));
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700216
217 /* Per-Port CLKREQ# handling. */
218 if (gpio_is_native(18 + rp - 1))
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300219 pci_update_config32(dev, 0x420, ~0, (3 << 29));
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700220
221 /* Enable static clock gating. */
222 if (rp == 1 && !rpc.ports[1]->enabled &&
223 !rpc.ports[2]->enabled && !rpc.ports[3]->enabled) {
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300224 pci_update_config8(dev, 0xe2, ~1, 1);
225 pci_update_config8(dev, 0xe1, 0x7f, 0x80);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700226 } else if (rp == 5 || rp == 6) {
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300227 pci_update_config8(dev, 0xe2, ~1, 1);
228 pci_update_config8(dev, 0xe1, 0x7f, 0x80);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700229 }
230 continue;
231 }
232
233 enabled_ports++;
234
235 /* Enable dynamic clock gating. */
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300236 pci_update_config8(dev, 0xe1, 0xfc, 0x03);
237 pci_update_config8(dev, 0xe2, ~(1 << 6), (1 << 6));
238 pci_update_config8(dev, 0xe8, ~(3 << 2), (2 << 2));
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700239
240 /* Update PECR1 register. */
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300241 pci_update_config8(dev, 0xe8, ~0, 3);
Kane Chen4fef5a22014-08-27 15:21:32 -0700242 if (is_broadwell) {
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300243 pci_update_config32(dev, 0x324, ~((1 << 5) | (1 << 14)),
Kane Chen4fef5a22014-08-27 15:21:32 -0700244 ((1 << 5) | (1 << 14)));
245 } else {
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300246 pci_update_config32(dev, 0x324, ~(1 << 5), (1 << 5));
Kane Chen4fef5a22014-08-27 15:21:32 -0700247 }
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700248 /* Per-Port CLKREQ# handling. */
249 if (gpio_is_native(18 + rp - 1))
Kenji Chene8f36642014-10-04 02:59:06 +0800250 /*
251 * In addition to D28Fx PCICFG 420h[30:29] = 11b,
252 * set 420h[17] = 0b and 420[0] = 1b for L1 SubState.
253 */
Lee Leahy6ef51922017-03-17 10:56:08 -0700254 pci_update_config32(dev, 0x420, ~0x20000,
255 (3 << 29) | 1);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700256
257 /* Configure shared resource clock gating. */
258 if (rp == 1 || rp == 5 || rp == 6)
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300259 pci_update_config8(dev, 0xe1, 0xc3, 0x3c);
Duncan Laurie446fb8e2014-08-08 09:59:43 -0700260
261 /* CLKREQ# VR Idle Enable */
262 RCBA32_OR(0x2b1c, (1 << (16 + i)));
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700263 }
264
265 if (!enabled_ports)
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300266 pci_update_config8(rpc.ports[0], 0xe1, ~(1 << 6), (1 << 6));
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700267}
268
269static void root_port_commit_config(void)
270{
271 int i;
272
273 /* If the first root port is disabled the coalesce ports. */
274 if (!rpc.ports[0]->enabled)
275 rpc.coalesce = 1;
276
277 /* Perform clock gating configuration. */
278 pcie_enable_clock_gating();
279
280 for (i = 0; i < rpc.num_ports; i++) {
Elyes HAOUAS040aff22018-05-27 16:30:36 +0200281 struct device *dev;
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700282 u32 reg32;
Wenkai Du83067612014-12-05 14:00:26 -0800283 int n = 0;
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700284
285 dev = rpc.ports[i];
286
287 if (dev == NULL) {
288 printk(BIOS_ERR, "Root Port %d device is NULL?\n", i+1);
289 continue;
290 }
291
292 if (dev->enabled)
293 continue;
294
295 printk(BIOS_DEBUG, "%s: Disabling device\n", dev_path(dev));
296
Wenkai Du83067612014-12-05 14:00:26 -0800297 /* 8.2 Configuration of PCI Express Root Ports */
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300298 pci_update_config32(dev, 0x338, ~(1 << 26), 1 << 26);
Wenkai Du83067612014-12-05 14:00:26 -0800299
300 do {
301 reg32 = pci_read_config32(dev, 0x328);
302 n++;
Duncan Lauriecad2b7b2015-01-14 17:30:20 -0800303 if (((reg32 & 0xff000000) == 0x01000000) || (n > 50))
Wenkai Du83067612014-12-05 14:00:26 -0800304 break;
305 udelay(100);
306 } while (1);
307
Duncan Lauriecad2b7b2015-01-14 17:30:20 -0800308 if (n > 50)
Wenkai Du83067612014-12-05 14:00:26 -0800309 printk(BIOS_DEBUG, "%s: Timeout waiting for 328h\n",
310 dev_path(dev));
311
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300312 pci_update_config32(dev, 0x408, ~(1 << 27), 1 << 27);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700313
314 /* Disable this device if possible */
315 pch_disable_devfn(dev);
316 }
317
318 if (rpc.coalesce) {
319 int current_func;
320
321 /* For all Root Ports N enabled ports get assigned the lower
322 * PCI function number. The disabled ones get upper PCI
323 * function numbers. */
324 current_func = 0;
325 for (i = 0; i < rpc.num_ports; i++) {
326 if (!rpc.ports[i]->enabled)
327 continue;
328 pch_pcie_device_set_func(i, current_func);
329 current_func++;
330 }
331
332 /* Allocate the disabled devices' PCI function number. */
333 for (i = 0; i < rpc.num_ports; i++) {
334 if (rpc.ports[i]->enabled)
335 continue;
336 pch_pcie_device_set_func(i, current_func);
337 current_func++;
338 }
339 }
340
341 printk(BIOS_SPEW, "PCH: RPFN 0x%08x -> 0x%08x\n",
342 rpc.orig_rpfn, rpc.new_rpfn);
343 RCBA32(RPFN) = rpc.new_rpfn;
344}
345
Elyes HAOUAS040aff22018-05-27 16:30:36 +0200346static void root_port_mark_disable(struct device *dev)
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700347{
348 /* Mark device as disabled. */
349 dev->enabled = 0;
350 /* Mark device to be hidden. */
351 rpc.new_rpfn |= RPFN_HIDE(PCI_FUNC(dev->path.pci.devfn));
352}
353
Elyes HAOUAS040aff22018-05-27 16:30:36 +0200354static void root_port_check_disable(struct device *dev)
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700355{
356 int rp;
357
358 /* Device already disabled. */
359 if (!dev->enabled) {
360 root_port_mark_disable(dev);
361 return;
362 }
363
364 rp = root_port_number(dev);
365
366 /* Is the GbE port mapped to this Root Port? */
367 if (rp == rpc.gbe_port) {
368 root_port_mark_disable(dev);
369 return;
370 }
371
372 /* Check Root Port Configuration. */
373 switch (rp) {
Lee Leahy6ef51922017-03-17 10:56:08 -0700374 case 2:
375 /* Root Port 2 is disabled for all lane configurations
376 * but config 00b (4x1 links). */
377 if ((rpc.strpfusecfg1 >> 14) & 0x3) {
378 root_port_mark_disable(dev);
379 return;
380 }
381 break;
382 case 3:
383 /* Root Port 3 is disabled in config 11b (1x4 links). */
384 if (((rpc.strpfusecfg1 >> 14) & 0x3) == 0x3) {
385 root_port_mark_disable(dev);
386 return;
387 }
388 break;
389 case 4:
390 /* Root Port 4 is disabled in configs 11b (1x4 links)
391 * and 10b (2x2 links). */
392 if ((rpc.strpfusecfg1 >> 14) & 0x2) {
393 root_port_mark_disable(dev);
394 return;
395 }
396 break;
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700397 }
398
399 /* Check Pin Ownership. */
400 switch (rp) {
401 case 1:
402 /* Bit 0 is Root Port 1 ownership. */
403 if ((rpc.pin_ownership & 0x1) == 0) {
404 root_port_mark_disable(dev);
405 return;
406 }
407 break;
408 case 2:
409 /* Bit 2 is Root Port 2 ownership. */
410 if ((rpc.pin_ownership & 0x4) == 0) {
411 root_port_mark_disable(dev);
412 return;
413 }
414 break;
415 case 6:
416 /* Bits 7:4 are Root Port 6 pin-lane ownership. */
417 if ((rpc.pin_ownership & 0xf0) == 0) {
418 root_port_mark_disable(dev);
419 return;
420 }
421 break;
422 }
423}
424
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700425static void pcie_add_0x0202000_iobp(u32 reg)
426{
427 u32 reg32;
428
429 reg32 = pch_iobp_read(reg);
430 reg32 += (0x2 << 16) | (0x2 << 8);
431 pch_iobp_write(reg, reg32);
432}
433
434static void pch_pcie_early(struct device *dev)
435{
Kyösti Mälkki8950cfb2019-07-13 22:16:25 +0300436 config_t *config = config_of(dev);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700437 int do_aspm = 0;
438 int rp = root_port_number(dev);
439
440 switch (rp) {
441 case 1:
442 case 2:
443 case 3:
444 case 4:
445 /*
Martin Rothde7ed6f2014-12-07 14:58:18 -0700446 * Bits 31:28 of b0d28f0 0x32c register correspond to
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700447 * Root Ports 4:1.
448 */
449 do_aspm = !!(rpc.b0d28f0_32c & (1 << (28 + rp - 1)));
450 break;
451 case 5:
452 /*
Martin Rothde7ed6f2014-12-07 14:58:18 -0700453 * Bit 28 of b0d28f4 0x32c register correspond to
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700454 * Root Ports 4:1.
455 */
456 do_aspm = !!(rpc.b0d28f4_32c & (1 << 28));
457 break;
458 case 6:
459 /*
Martin Rothde7ed6f2014-12-07 14:58:18 -0700460 * Bit 28 of b0d28f5 0x32c register correspond to
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700461 * Root Ports 4:1.
462 */
463 do_aspm = !!(rpc.b0d28f5_32c & (1 << 28));
464 break;
465 }
466
467 /* Allow ASPM to be forced on in devicetree */
Kyösti Mälkki8950cfb2019-07-13 22:16:25 +0300468 if ((config->pcie_port_force_aspm & (1 << (rp - 1))))
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700469 do_aspm = 1;
470
471 printk(BIOS_DEBUG, "PCIe Root Port %d ASPM is %sabled\n",
472 rp, do_aspm ? "en" : "dis");
473
474 if (do_aspm) {
475 /* Set ASPM bits in MPC2 register. */
Angel Pons2ead3632020-09-24 16:50:05 +0200476 pci_update_config32(dev, 0xd4, ~(0x3 << 2), (1 << 4) | (0x2 << 2));
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700477
478 /* Set unique clock exit latency in MPC register. */
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300479 pci_update_config32(dev, 0xd8, ~(0x7 << 18), (0x7 << 18));
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700480
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700481 switch (rp) {
482 case 1:
483 pcie_add_0x0202000_iobp(0xe9002440);
484 break;
485 case 2:
486 pcie_add_0x0202000_iobp(0xe9002640);
487 break;
488 case 3:
489 pcie_add_0x0202000_iobp(0xe9000840);
490 break;
491 case 4:
492 pcie_add_0x0202000_iobp(0xe9000a40);
493 break;
494 case 5:
495 pcie_add_0x0202000_iobp(0xe9000c40);
496 pcie_add_0x0202000_iobp(0xe9000e40);
497 pcie_add_0x0202000_iobp(0xe9001040);
498 pcie_add_0x0202000_iobp(0xe9001240);
499 break;
500 case 6:
501 /* Update IOBP based on lane ownership. */
502 if (rpc.pin_ownership & (1 << 4))
503 pcie_add_0x0202000_iobp(0xea002040);
504 if (rpc.pin_ownership & (1 << 5))
505 pcie_add_0x0202000_iobp(0xea002240);
506 if (rpc.pin_ownership & (1 << 6))
507 pcie_add_0x0202000_iobp(0xea002440);
508 if (rpc.pin_ownership & (1 << 7))
509 pcie_add_0x0202000_iobp(0xea002640);
510 break;
511 }
512
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300513 pci_update_config32(dev, 0x338, ~(1 << 26), 0);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700514 }
515
Kenji Chenc373f502014-09-26 02:48:16 +0800516 /* Enable LTR in Root Port. Disable OBFF. */
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300517 pci_update_config32(dev, 0x64, ~(1 << 11) & ~(3 << 18), (1 << 11));
518 pci_update_config32(dev, 0x68, ~(1 << 10), (1 << 10));
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700519
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300520 pci_update_config32(dev, 0x318, ~(0xffff << 16), (0x1414 << 16));
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700521
522 /* Set L1 exit latency in LCAP register. */
523 if (!do_aspm && (pci_read_config8(dev, 0xf5) & 0x1))
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300524 pci_update_config32(dev, 0x4c, ~(0x7 << 15), (0x4 << 15));
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700525 else
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300526 pci_update_config32(dev, 0x4c, ~(0x7 << 15), (0x2 << 15));
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700527
Angel Pons2ead3632020-09-24 16:50:05 +0200528 pci_update_config32(dev, 0x314, 0, 0x743a361b);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700529
530 /* Set Common Clock Exit Latency in MPC register. */
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300531 pci_update_config32(dev, 0xd8, ~(0x7 << 15), (0x3 << 15));
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700532
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300533 pci_update_config32(dev, 0x33c, ~0x00ffffff, 0x854d74);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700534
Martin Rothde7ed6f2014-12-07 14:58:18 -0700535 /* Set Invalid Receive Range Check Enable in MPC register. */
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300536 pci_update_config32(dev, 0xd8, ~0, (1 << 25));
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700537
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300538 pci_update_config8(dev, 0xf5, 0x0f, 0);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700539
Kenji Chen94fea492014-09-30 14:17:35 +0800540 /* Set AER Extended Cap ID to 01h and Next Cap Pointer to 200h. */
Julius Wernercd49cce2019-03-05 16:53:33 -0800541 if (CONFIG(PCIEXP_AER))
Youness Alaoui71616782018-05-04 15:34:06 -0400542 pci_update_config32(dev, 0x100, ~(1 << 29) & ~0xfffff,
543 (1 << 29) | 0x10001);
544 else
545 pci_update_config32(dev, 0x100, ~(1 << 29) & ~0xfffff,
546 (1 << 29));
Kenji Chen8ef55ee2014-09-25 21:34:42 +0800547
Kenji Chen94fea492014-09-30 14:17:35 +0800548 /* Set L1 Sub-State Cap ID to 1Eh and Next Cap Pointer to None. */
Julius Wernercd49cce2019-03-05 16:53:33 -0800549 if (CONFIG(PCIEXP_L1_SUB_STATE))
Youness Alaoui1f64b012018-05-04 15:33:54 -0400550 pci_update_config32(dev, 0x200, ~0xfffff, 0x001e);
551 else
552 pci_update_config32(dev, 0x200, ~0xfffff, 0);
Kenji Chen94fea492014-09-30 14:17:35 +0800553
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300554 pci_update_config32(dev, 0x320, ~(3 << 20) & ~(7 << 6),
Kenji Chenc373f502014-09-26 02:48:16 +0800555 (1 << 20) | (3 << 6));
556 /* Enable Relaxed Order from Root Port. */
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300557 pci_update_config32(dev, 0x320, ~(3 << 23), (3 << 23));
Kenji Chenc373f502014-09-26 02:48:16 +0800558
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700559 if (rp == 1 || rp == 5 || rp == 6)
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300560 pci_update_config8(dev, 0xf7, ~0xc, 0);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700561
562 /* Set EOI forwarding disable. */
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300563 pci_update_config32(dev, 0xd4, ~0, (1 << 1));
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700564
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700565 /* Read and write back write-once capability registers. */
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300566 pci_update_config32(dev, 0x34, ~0, 0);
567 pci_update_config32(dev, 0x40, ~0, 0);
568 pci_update_config32(dev, 0x80, ~0, 0);
569 pci_update_config32(dev, 0x90, ~0, 0);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700570}
571
572static void pch_pcie_init(struct device *dev)
573{
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700574 printk(BIOS_DEBUG, "Initializing PCH PCIe bridge.\n");
575
576 /* Enable SERR */
Elyes HAOUASb887adf2020-04-29 10:42:34 +0200577 pci_or_config16(dev, PCI_COMMAND, PCI_COMMAND_SERR);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700578
579 /* Enable Bus Master */
Elyes HAOUASb887adf2020-04-29 10:42:34 +0200580 pci_or_config16(dev, PCI_COMMAND, PCI_COMMAND_MASTER);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700581
582 /* Set Cache Line Size to 0x10 */
583 pci_write_config8(dev, 0x0c, 0x10);
584
Angel Pons2ead3632020-09-24 16:50:05 +0200585 pci_and_config16(dev, PCI_BRIDGE_CONTROL, ~PCI_BRIDGE_CTL_PARITY);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700586
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700587 /* Clear errors in status registers */
Angel Pons2ead3632020-09-24 16:50:05 +0200588 pci_update_config16(dev, 0x06, ~0, 0);
589 pci_update_config16(dev, 0x1e, ~0, 0);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700590}
591
Elyes HAOUAS040aff22018-05-27 16:30:36 +0200592static void pch_pcie_enable(struct device *dev)
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700593{
594 /* Add this device to the root port config structure. */
595 root_port_init_config(dev);
596
597 /* Check to see if this Root Port should be disabled. */
598 root_port_check_disable(dev);
599
600 /* Power Management init before enumeration */
601 if (dev->enabled)
602 pch_pcie_early(dev);
603
604 /*
605 * When processing the last PCIe root port we can now
606 * update the Root Port Function Number and Hide register.
607 */
608 if (root_port_is_last(dev))
609 root_port_commit_config();
610}
611
Elyes HAOUAS040aff22018-05-27 16:30:36 +0200612static void pcie_set_L1_ss_max_latency(struct device *dev, unsigned int off)
Kenji Chenb71d9b82014-10-10 03:08:15 +0800613{
614 /* Set max snoop and non-snoop latency for Broadwell */
Subrata Baniked6996f2019-03-25 21:49:39 +0530615 pci_write_config32(dev, off,
616 PCIE_LTR_MAX_NO_SNOOP_LATENCY_3146US << 16 |
617 PCIE_LTR_MAX_SNOOP_LATENCY_3146US);
Kenji Chenb71d9b82014-10-10 03:08:15 +0800618}
619
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700620static struct pci_operations pcie_ops = {
Subrata Banik15ccbf02019-03-20 15:09:44 +0530621 .set_subsystem = pci_dev_set_subsystem,
Kenji Chenb71d9b82014-10-10 03:08:15 +0800622 .set_L1_ss_latency = pcie_set_L1_ss_max_latency,
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700623};
624
625static struct device_operations device_ops = {
626 .read_resources = pci_bus_read_resources,
627 .set_resources = pci_dev_set_resources,
628 .enable_resources = pci_bus_enable_resources,
629 .init = pch_pcie_init,
630 .enable = pch_pcie_enable,
631 .scan_bus = pciexp_scan_bridge,
632 .ops_pci = &pcie_ops,
633};
634
635static const unsigned short pcie_device_ids[] = {
636 /* Lynxpoint-LP */
637 0x9c10, 0x9c12, 0x9c14, 0x9c16, 0x9c18, 0x9c1a,
638 /* WildcatPoint */
639 0x9c90, 0x9c92, 0x9c94, 0x9c96, 0x9c98, 0x9c9a, 0x2448,
640 0
641};
642
643static const struct pci_driver pch_pcie __pci_driver = {
644 .ops = &device_ops,
645 .vendor = PCI_VENDOR_ID_INTEL,
646 .devices = pcie_device_ids,
647};