blob: 3fd5ea4f10d847715481268b95a65b4c089509d7 [file] [log] [blame]
Duncan Lauriec88c54c2014-04-30 16:36:13 -07001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2008-2009 coresystems GmbH
5 * Copyright (C) 2014 Google Inc.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; version 2 of 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.
Duncan Lauriec88c54c2014-04-30 16:36:13 -070015 */
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_def.h>
22#include <device/pci_ids.h>
Patrick Rudolphe56189c2018-04-18 10:11:59 +020023#include <device/pci_ops.h>
Julius Werner4ee4bd52014-10-20 13:46:39 -070024#include <soc/gpio.h>
25#include <soc/lpc.h>
26#include <soc/iobp.h>
27#include <soc/pch.h>
28#include <soc/pci_devs.h>
29#include <soc/rcba.h>
30#include <soc/intel/broadwell/chip.h>
31#include <soc/cpu.h>
Wenkai Du83067612014-12-05 14:00:26 -080032#include <delay.h>
Duncan Lauriec88c54c2014-04-30 16:36:13 -070033
Duncan Lauriec88c54c2014-04-30 16:36:13 -070034/* Low Power variant has 6 root ports. */
35#define NUM_ROOT_PORTS 6
36
37struct root_port_config {
38 /* RPFN is a write-once register so keep a copy until it is written */
39 u32 orig_rpfn;
40 u32 new_rpfn;
41 u32 pin_ownership;
42 u32 strpfusecfg1;
43 u32 strpfusecfg2;
44 u32 strpfusecfg3;
45 u32 b0d28f0_32c;
46 u32 b0d28f4_32c;
47 u32 b0d28f5_32c;
48 int coalesce;
49 int gbe_port;
50 int num_ports;
Elyes HAOUAS040aff22018-05-27 16:30:36 +020051 struct device *ports[NUM_ROOT_PORTS];
Duncan Lauriec88c54c2014-04-30 16:36:13 -070052};
53
54static struct root_port_config rpc;
55
Elyes HAOUAS040aff22018-05-27 16:30:36 +020056static inline int root_port_is_first(struct device *dev)
Duncan Lauriec88c54c2014-04-30 16:36:13 -070057{
58 return PCI_FUNC(dev->path.pci.devfn) == 0;
59}
60
Elyes HAOUAS040aff22018-05-27 16:30:36 +020061static inline int root_port_is_last(struct device *dev)
Duncan Lauriec88c54c2014-04-30 16:36:13 -070062{
63 return PCI_FUNC(dev->path.pci.devfn) == (rpc.num_ports - 1);
64}
65
66/* Root ports are numbered 1..N in the documentation. */
Elyes HAOUAS040aff22018-05-27 16:30:36 +020067static inline int root_port_number(struct device *dev)
Duncan Lauriec88c54c2014-04-30 16:36:13 -070068{
69 return PCI_FUNC(dev->path.pci.devfn) + 1;
70}
71
72static void root_port_config_update_gbe_port(void)
73{
74 /* Is the Gbe Port enabled? */
75 if (!((rpc.strpfusecfg1 >> 19) & 1))
76 return;
77
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}
96
Elyes HAOUAS040aff22018-05-27 16:30:36 +020097static void pcie_iosf_port_grant_count(struct device *dev)
Kenji Chen87d4a202014-09-24 01:18:26 +080098{
99 u8 update_val;
Patrick Georgie8f2ef52016-07-29 18:53:34 +0200100 u32 rpcd = (pci_read_config32(dev, 0xfc) >> 14) & 0x3;
Kenji Chen87d4a202014-09-24 01:18:26 +0800101
102 switch (rpcd) {
103 case 1:
104 case 3:
105 update_val = 0x02;
106 break;
107 case 2:
108 update_val = 0x22;
109 break;
110 default:
111 update_val = 0x00;
112 break;
113 }
114
115 RCBA32(0x103C) = (RCBA32(0x103C) & (~0xff)) | update_val;
116}
117
Elyes HAOUAS040aff22018-05-27 16:30:36 +0200118static void root_port_init_config(struct device *dev)
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700119{
120 int rp;
Martin Roth2b2ff7f2015-12-18 10:46:59 -0700121 u32 data = 0;
Kenji Chene383feb2014-09-26 03:14:57 +0800122 u8 resp, id;
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700123
124 if (root_port_is_first(dev)) {
125 rpc.orig_rpfn = RCBA32(RPFN);
126 rpc.new_rpfn = rpc.orig_rpfn;
127 rpc.num_ports = NUM_ROOT_PORTS;
128 rpc.gbe_port = -1;
Kenji Chen87d4a202014-09-24 01:18:26 +0800129 /* RP0 f5[3:0] = 0101b*/
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300130 pci_update_config8(dev, 0xf5, ~0xa, 0x5);
Kenji Chen87d4a202014-09-24 01:18:26 +0800131
132 pcie_iosf_port_grant_count(dev);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700133
134 rpc.pin_ownership = pci_read_config32(dev, 0x410);
135 root_port_config_update_gbe_port();
136
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300137 pci_update_config8(dev, 0xe2, ~(3 << 4), (3 << 4));
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700138 if (dev->chip_info != NULL) {
139 config_t *config = dev->chip_info;
140 rpc.coalesce = config->pcie_port_coalesce;
141 }
142 }
143
144 rp = root_port_number(dev);
145 if (rp > rpc.num_ports) {
146 printk(BIOS_ERR, "Found Root Port %d, expecting %d\n",
147 rp, rpc.num_ports);
148 return;
149 }
150
151 /* Read the fuse configuration and pin ownership. */
152 switch (rp) {
153 case 1:
154 rpc.strpfusecfg1 = pci_read_config32(dev, 0xfc);
155 rpc.b0d28f0_32c = pci_read_config32(dev, 0x32c);
156 break;
157 case 5:
158 rpc.strpfusecfg2 = pci_read_config32(dev, 0xfc);
159 rpc.b0d28f4_32c = pci_read_config32(dev, 0x32c);
160 break;
161 case 6:
162 rpc.b0d28f5_32c = pci_read_config32(dev, 0x32c);
163 rpc.strpfusecfg3 = pci_read_config32(dev, 0xfc);
164 break;
165 default:
166 break;
167 }
168
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300169 pci_update_config32(dev, 0x418, 0, 0x02000430);
Kenji Chene383feb2014-09-26 03:14:57 +0800170
Kenji Chene383feb2014-09-26 03:14:57 +0800171 if (root_port_is_first(dev)) {
Kenji Chene8f36642014-10-04 02:59:06 +0800172 /*
173 * set RP0 PCICFG E2h[5:4] = 11b and E1h[6] = 1
174 * before configuring ASPM
175 */
Kenji Chene383feb2014-09-26 03:14:57 +0800176 id = 0xe0 + (u8)(RCBA32(RPFN) & 0x07);
177 pch_iobp_exec(0xE00000E0, IOBP_PCICFG_READ, id, &data, &resp);
Kenji Chene8f36642014-10-04 02:59:06 +0800178 data |= ((0x30 << 16) | (0x40 << 8));
Kenji Chene383feb2014-09-26 03:14:57 +0800179 pch_iobp_exec(0xE00000E0, IOBP_PCICFG_WRITE, id, &data, &resp);
180 }
181
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700182 /* Cache pci device. */
183 rpc.ports[rp - 1] = dev;
184}
185
186/* Update devicetree with new Root Port function number assignment */
187static void pch_pcie_device_set_func(int index, int pci_func)
188{
Elyes HAOUAS040aff22018-05-27 16:30:36 +0200189 struct device *dev;
Lee Leahy23602df2017-03-16 19:00:37 -0700190 unsigned int new_devfn;
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700191
192 dev = rpc.ports[index];
193
194 /* Set the new PCI function field for this Root Port. */
195 rpc.new_rpfn &= ~RPFN_FNMASK(index);
196 rpc.new_rpfn |= RPFN_FNSET(index, pci_func);
197
198 /* Determine the new devfn for this port */
199 new_devfn = PCI_DEVFN(PCH_DEV_SLOT_PCIE, pci_func);
200
201 if (dev->path.pci.devfn != new_devfn) {
202 printk(BIOS_DEBUG,
203 "PCH: PCIe map %02x.%1x -> %02x.%1x\n",
204 PCI_SLOT(dev->path.pci.devfn),
205 PCI_FUNC(dev->path.pci.devfn),
206 PCI_SLOT(new_devfn), PCI_FUNC(new_devfn));
207
208 dev->path.pci.devfn = new_devfn;
209 }
210}
211
212static void pcie_enable_clock_gating(void)
213{
214 int i;
215 int enabled_ports = 0;
Kane Chen4fef5a22014-08-27 15:21:32 -0700216 int is_broadwell = !!(cpu_family_model() == BROADWELL_FAMILY_ULT);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700217
218 for (i = 0; i < rpc.num_ports; i++) {
Elyes HAOUAS040aff22018-05-27 16:30:36 +0200219 struct device *dev;
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700220 int rp;
221
222 dev = rpc.ports[i];
223 rp = root_port_number(dev);
224
225 if (!dev->enabled) {
226 /* Configure shared resource clock gating. */
227 if (rp == 1 || rp == 5 || rp == 6)
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300228 pci_update_config8(dev, 0xe1, 0xc3, 0x3c);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700229
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300230 pci_update_config8(dev, 0xe2, ~(3 << 4), (3 << 4));
231 pci_update_config32(dev, 0x420, ~(1 << 31), (1 << 31));
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700232
233 /* Per-Port CLKREQ# handling. */
234 if (gpio_is_native(18 + rp - 1))
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300235 pci_update_config32(dev, 0x420, ~0, (3 << 29));
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700236
237 /* Enable static clock gating. */
238 if (rp == 1 && !rpc.ports[1]->enabled &&
239 !rpc.ports[2]->enabled && !rpc.ports[3]->enabled) {
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300240 pci_update_config8(dev, 0xe2, ~1, 1);
241 pci_update_config8(dev, 0xe1, 0x7f, 0x80);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700242 } else if (rp == 5 || rp == 6) {
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300243 pci_update_config8(dev, 0xe2, ~1, 1);
244 pci_update_config8(dev, 0xe1, 0x7f, 0x80);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700245 }
246 continue;
247 }
248
249 enabled_ports++;
250
251 /* Enable dynamic clock gating. */
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300252 pci_update_config8(dev, 0xe1, 0xfc, 0x03);
253 pci_update_config8(dev, 0xe2, ~(1 << 6), (1 << 6));
254 pci_update_config8(dev, 0xe8, ~(3 << 2), (2 << 2));
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700255
256 /* Update PECR1 register. */
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300257 pci_update_config8(dev, 0xe8, ~0, 3);
Kane Chen4fef5a22014-08-27 15:21:32 -0700258 if (is_broadwell) {
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300259 pci_update_config32(dev, 0x324, ~((1 << 5) | (1 << 14)),
Kane Chen4fef5a22014-08-27 15:21:32 -0700260 ((1 << 5) | (1 << 14)));
261 } else {
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300262 pci_update_config32(dev, 0x324, ~(1 << 5), (1 << 5));
Kane Chen4fef5a22014-08-27 15:21:32 -0700263 }
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700264 /* Per-Port CLKREQ# handling. */
265 if (gpio_is_native(18 + rp - 1))
Kenji Chene8f36642014-10-04 02:59:06 +0800266 /*
267 * In addition to D28Fx PCICFG 420h[30:29] = 11b,
268 * set 420h[17] = 0b and 420[0] = 1b for L1 SubState.
269 */
Lee Leahy6ef51922017-03-17 10:56:08 -0700270 pci_update_config32(dev, 0x420, ~0x20000,
271 (3 << 29) | 1);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700272
273 /* Configure shared resource clock gating. */
274 if (rp == 1 || rp == 5 || rp == 6)
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300275 pci_update_config8(dev, 0xe1, 0xc3, 0x3c);
Duncan Laurie446fb8e2014-08-08 09:59:43 -0700276
277 /* CLKREQ# VR Idle Enable */
278 RCBA32_OR(0x2b1c, (1 << (16 + i)));
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700279 }
280
281 if (!enabled_ports)
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300282 pci_update_config8(rpc.ports[0], 0xe1, ~(1 << 6), (1 << 6));
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700283}
284
285static void root_port_commit_config(void)
286{
287 int i;
288
289 /* If the first root port is disabled the coalesce ports. */
290 if (!rpc.ports[0]->enabled)
291 rpc.coalesce = 1;
292
293 /* Perform clock gating configuration. */
294 pcie_enable_clock_gating();
295
296 for (i = 0; i < rpc.num_ports; i++) {
Elyes HAOUAS040aff22018-05-27 16:30:36 +0200297 struct device *dev;
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700298 u32 reg32;
Wenkai Du83067612014-12-05 14:00:26 -0800299 int n = 0;
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700300
301 dev = rpc.ports[i];
302
303 if (dev == NULL) {
304 printk(BIOS_ERR, "Root Port %d device is NULL?\n", i+1);
305 continue;
306 }
307
308 if (dev->enabled)
309 continue;
310
311 printk(BIOS_DEBUG, "%s: Disabling device\n", dev_path(dev));
312
Wenkai Du83067612014-12-05 14:00:26 -0800313 /* 8.2 Configuration of PCI Express Root Ports */
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300314 pci_update_config32(dev, 0x338, ~(1 << 26), 1 << 26);
Wenkai Du83067612014-12-05 14:00:26 -0800315
316 do {
317 reg32 = pci_read_config32(dev, 0x328);
318 n++;
Duncan Lauriecad2b7b2015-01-14 17:30:20 -0800319 if (((reg32 & 0xff000000) == 0x01000000) || (n > 50))
Wenkai Du83067612014-12-05 14:00:26 -0800320 break;
321 udelay(100);
322 } while (1);
323
Duncan Lauriecad2b7b2015-01-14 17:30:20 -0800324 if (n > 50)
Wenkai Du83067612014-12-05 14:00:26 -0800325 printk(BIOS_DEBUG, "%s: Timeout waiting for 328h\n",
326 dev_path(dev));
327
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300328 pci_update_config32(dev, 0x408, ~(1 << 27), 1 << 27);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700329
330 /* Disable this device if possible */
331 pch_disable_devfn(dev);
332 }
333
334 if (rpc.coalesce) {
335 int current_func;
336
337 /* For all Root Ports N enabled ports get assigned the lower
338 * PCI function number. The disabled ones get upper PCI
339 * function numbers. */
340 current_func = 0;
341 for (i = 0; i < rpc.num_ports; i++) {
342 if (!rpc.ports[i]->enabled)
343 continue;
344 pch_pcie_device_set_func(i, current_func);
345 current_func++;
346 }
347
348 /* Allocate the disabled devices' PCI function number. */
349 for (i = 0; i < rpc.num_ports; i++) {
350 if (rpc.ports[i]->enabled)
351 continue;
352 pch_pcie_device_set_func(i, current_func);
353 current_func++;
354 }
355 }
356
357 printk(BIOS_SPEW, "PCH: RPFN 0x%08x -> 0x%08x\n",
358 rpc.orig_rpfn, rpc.new_rpfn);
359 RCBA32(RPFN) = rpc.new_rpfn;
360}
361
Elyes HAOUAS040aff22018-05-27 16:30:36 +0200362static void root_port_mark_disable(struct device *dev)
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700363{
364 /* Mark device as disabled. */
365 dev->enabled = 0;
366 /* Mark device to be hidden. */
367 rpc.new_rpfn |= RPFN_HIDE(PCI_FUNC(dev->path.pci.devfn));
368}
369
Elyes HAOUAS040aff22018-05-27 16:30:36 +0200370static void root_port_check_disable(struct device *dev)
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700371{
372 int rp;
373
374 /* Device already disabled. */
375 if (!dev->enabled) {
376 root_port_mark_disable(dev);
377 return;
378 }
379
380 rp = root_port_number(dev);
381
382 /* Is the GbE port mapped to this Root Port? */
383 if (rp == rpc.gbe_port) {
384 root_port_mark_disable(dev);
385 return;
386 }
387
388 /* Check Root Port Configuration. */
389 switch (rp) {
Lee Leahy6ef51922017-03-17 10:56:08 -0700390 case 2:
391 /* Root Port 2 is disabled for all lane configurations
392 * but config 00b (4x1 links). */
393 if ((rpc.strpfusecfg1 >> 14) & 0x3) {
394 root_port_mark_disable(dev);
395 return;
396 }
397 break;
398 case 3:
399 /* Root Port 3 is disabled in config 11b (1x4 links). */
400 if (((rpc.strpfusecfg1 >> 14) & 0x3) == 0x3) {
401 root_port_mark_disable(dev);
402 return;
403 }
404 break;
405 case 4:
406 /* Root Port 4 is disabled in configs 11b (1x4 links)
407 * and 10b (2x2 links). */
408 if ((rpc.strpfusecfg1 >> 14) & 0x2) {
409 root_port_mark_disable(dev);
410 return;
411 }
412 break;
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700413 }
414
415 /* Check Pin Ownership. */
416 switch (rp) {
417 case 1:
418 /* Bit 0 is Root Port 1 ownership. */
419 if ((rpc.pin_ownership & 0x1) == 0) {
420 root_port_mark_disable(dev);
421 return;
422 }
423 break;
424 case 2:
425 /* Bit 2 is Root Port 2 ownership. */
426 if ((rpc.pin_ownership & 0x4) == 0) {
427 root_port_mark_disable(dev);
428 return;
429 }
430 break;
431 case 6:
432 /* Bits 7:4 are Root Port 6 pin-lane ownership. */
433 if ((rpc.pin_ownership & 0xf0) == 0) {
434 root_port_mark_disable(dev);
435 return;
436 }
437 break;
438 }
439}
440
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700441static void pcie_add_0x0202000_iobp(u32 reg)
442{
443 u32 reg32;
444
445 reg32 = pch_iobp_read(reg);
446 reg32 += (0x2 << 16) | (0x2 << 8);
447 pch_iobp_write(reg, reg32);
448}
449
450static void pch_pcie_early(struct device *dev)
451{
452 config_t *config = dev->chip_info;
453 int do_aspm = 0;
454 int rp = root_port_number(dev);
455
456 switch (rp) {
457 case 1:
458 case 2:
459 case 3:
460 case 4:
461 /*
Martin Rothde7ed6f2014-12-07 14:58:18 -0700462 * Bits 31:28 of b0d28f0 0x32c register correspond to
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700463 * Root Ports 4:1.
464 */
465 do_aspm = !!(rpc.b0d28f0_32c & (1 << (28 + rp - 1)));
466 break;
467 case 5:
468 /*
Martin Rothde7ed6f2014-12-07 14:58:18 -0700469 * Bit 28 of b0d28f4 0x32c register correspond to
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700470 * Root Ports 4:1.
471 */
472 do_aspm = !!(rpc.b0d28f4_32c & (1 << 28));
473 break;
474 case 6:
475 /*
Martin Rothde7ed6f2014-12-07 14:58:18 -0700476 * Bit 28 of b0d28f5 0x32c register correspond to
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700477 * Root Ports 4:1.
478 */
479 do_aspm = !!(rpc.b0d28f5_32c & (1 << 28));
480 break;
481 }
482
483 /* Allow ASPM to be forced on in devicetree */
484 if (config && (config->pcie_port_force_aspm & (1 << (rp - 1))))
485 do_aspm = 1;
486
487 printk(BIOS_DEBUG, "PCIe Root Port %d ASPM is %sabled\n",
488 rp, do_aspm ? "en" : "dis");
489
490 if (do_aspm) {
491 /* Set ASPM bits in MPC2 register. */
Lee Leahy6ef51922017-03-17 10:56:08 -0700492 pci_update_config32(dev, 0xd4, ~(0x3 << 2),
493 (1 << 4) | (0x2 << 2));
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700494
495 /* Set unique clock exit latency in MPC register. */
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300496 pci_update_config32(dev, 0xd8, ~(0x7 << 18), (0x7 << 18));
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700497
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700498 switch (rp) {
499 case 1:
500 pcie_add_0x0202000_iobp(0xe9002440);
501 break;
502 case 2:
503 pcie_add_0x0202000_iobp(0xe9002640);
504 break;
505 case 3:
506 pcie_add_0x0202000_iobp(0xe9000840);
507 break;
508 case 4:
509 pcie_add_0x0202000_iobp(0xe9000a40);
510 break;
511 case 5:
512 pcie_add_0x0202000_iobp(0xe9000c40);
513 pcie_add_0x0202000_iobp(0xe9000e40);
514 pcie_add_0x0202000_iobp(0xe9001040);
515 pcie_add_0x0202000_iobp(0xe9001240);
516 break;
517 case 6:
518 /* Update IOBP based on lane ownership. */
519 if (rpc.pin_ownership & (1 << 4))
520 pcie_add_0x0202000_iobp(0xea002040);
521 if (rpc.pin_ownership & (1 << 5))
522 pcie_add_0x0202000_iobp(0xea002240);
523 if (rpc.pin_ownership & (1 << 6))
524 pcie_add_0x0202000_iobp(0xea002440);
525 if (rpc.pin_ownership & (1 << 7))
526 pcie_add_0x0202000_iobp(0xea002640);
527 break;
528 }
529
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300530 pci_update_config32(dev, 0x338, ~(1 << 26), 0);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700531 }
532
Kenji Chenc373f502014-09-26 02:48:16 +0800533 /* Enable LTR in Root Port. Disable OBFF. */
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300534 pci_update_config32(dev, 0x64, ~(1 << 11) & ~(3 << 18), (1 << 11));
535 pci_update_config32(dev, 0x68, ~(1 << 10), (1 << 10));
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700536
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300537 pci_update_config32(dev, 0x318, ~(0xffff << 16), (0x1414 << 16));
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700538
539 /* Set L1 exit latency in LCAP register. */
540 if (!do_aspm && (pci_read_config8(dev, 0xf5) & 0x1))
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300541 pci_update_config32(dev, 0x4c, ~(0x7 << 15), (0x4 << 15));
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700542 else
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300543 pci_update_config32(dev, 0x4c, ~(0x7 << 15), (0x2 << 15));
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700544
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300545 pci_update_config32(dev, 0x314, 0x0, 0x743a361b);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700546
547 /* Set Common Clock Exit Latency in MPC register. */
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300548 pci_update_config32(dev, 0xd8, ~(0x7 << 15), (0x3 << 15));
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700549
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300550 pci_update_config32(dev, 0x33c, ~0x00ffffff, 0x854d74);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700551
Martin Rothde7ed6f2014-12-07 14:58:18 -0700552 /* Set Invalid Receive Range Check Enable in MPC register. */
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300553 pci_update_config32(dev, 0xd8, ~0, (1 << 25));
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700554
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300555 pci_update_config8(dev, 0xf5, 0x0f, 0);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700556
Kenji Chen94fea492014-09-30 14:17:35 +0800557 /* Set AER Extended Cap ID to 01h and Next Cap Pointer to 200h. */
Youness Alaoui71616782018-05-04 15:34:06 -0400558 if (IS_ENABLED(CONFIG_PCIEXP_AER))
559 pci_update_config32(dev, 0x100, ~(1 << 29) & ~0xfffff,
560 (1 << 29) | 0x10001);
561 else
562 pci_update_config32(dev, 0x100, ~(1 << 29) & ~0xfffff,
563 (1 << 29));
Kenji Chen8ef55ee2014-09-25 21:34:42 +0800564
Kenji Chen94fea492014-09-30 14:17:35 +0800565 /* Set L1 Sub-State Cap ID to 1Eh and Next Cap Pointer to None. */
Youness Alaoui1f64b012018-05-04 15:33:54 -0400566 if (IS_ENABLED(CONFIG_PCIEXP_L1_SUB_STATE))
567 pci_update_config32(dev, 0x200, ~0xfffff, 0x001e);
568 else
569 pci_update_config32(dev, 0x200, ~0xfffff, 0);
Kenji Chen94fea492014-09-30 14:17:35 +0800570
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300571 pci_update_config32(dev, 0x320, ~(3 << 20) & ~(7 << 6),
Kenji Chenc373f502014-09-26 02:48:16 +0800572 (1 << 20) | (3 << 6));
573 /* Enable Relaxed Order from Root Port. */
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300574 pci_update_config32(dev, 0x320, ~(3 << 23), (3 << 23));
Kenji Chenc373f502014-09-26 02:48:16 +0800575
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700576 if (rp == 1 || rp == 5 || rp == 6)
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300577 pci_update_config8(dev, 0xf7, ~0xc, 0);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700578
579 /* Set EOI forwarding disable. */
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300580 pci_update_config32(dev, 0xd4, ~0, (1 << 1));
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700581
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700582 /* Read and write back write-once capability registers. */
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300583 pci_update_config32(dev, 0x34, ~0, 0);
584 pci_update_config32(dev, 0x40, ~0, 0);
585 pci_update_config32(dev, 0x80, ~0, 0);
586 pci_update_config32(dev, 0x90, ~0, 0);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700587}
588
589static void pch_pcie_init(struct device *dev)
590{
591 u16 reg16;
592 u32 reg32;
593
594 printk(BIOS_DEBUG, "Initializing PCH PCIe bridge.\n");
595
596 /* Enable SERR */
597 reg32 = pci_read_config32(dev, PCI_COMMAND);
598 reg32 |= PCI_COMMAND_SERR;
599 pci_write_config32(dev, PCI_COMMAND, reg32);
600
601 /* Enable Bus Master */
602 reg32 = pci_read_config32(dev, PCI_COMMAND);
603 reg32 |= PCI_COMMAND_MASTER;
604 pci_write_config32(dev, PCI_COMMAND, reg32);
605
606 /* Set Cache Line Size to 0x10 */
607 pci_write_config8(dev, 0x0c, 0x10);
608
609 reg16 = pci_read_config16(dev, 0x3e);
610 reg16 &= ~(1 << 0); /* disable parity error response */
611 reg16 |= (1 << 2); /* ISA enable */
612 pci_write_config16(dev, 0x3e, reg16);
613
614#ifdef EVEN_MORE_DEBUG
615 reg32 = pci_read_config32(dev, 0x20);
616 printk(BIOS_SPEW, " MBL = 0x%08x\n", reg32);
617 reg32 = pci_read_config32(dev, 0x24);
618 printk(BIOS_SPEW, " PMBL = 0x%08x\n", reg32);
619 reg32 = pci_read_config32(dev, 0x28);
620 printk(BIOS_SPEW, " PMBU32 = 0x%08x\n", reg32);
621 reg32 = pci_read_config32(dev, 0x2c);
622 printk(BIOS_SPEW, " PMLU32 = 0x%08x\n", reg32);
623#endif
624
625 /* Clear errors in status registers */
626 reg16 = pci_read_config16(dev, 0x06);
627 pci_write_config16(dev, 0x06, reg16);
628 reg16 = pci_read_config16(dev, 0x1e);
629 pci_write_config16(dev, 0x1e, reg16);
630}
631
Elyes HAOUAS040aff22018-05-27 16:30:36 +0200632static void pch_pcie_enable(struct device *dev)
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700633{
634 /* Add this device to the root port config structure. */
635 root_port_init_config(dev);
636
637 /* Check to see if this Root Port should be disabled. */
638 root_port_check_disable(dev);
639
640 /* Power Management init before enumeration */
641 if (dev->enabled)
642 pch_pcie_early(dev);
643
644 /*
645 * When processing the last PCIe root port we can now
646 * update the Root Port Function Number and Hide register.
647 */
648 if (root_port_is_last(dev))
649 root_port_commit_config();
650}
651
Elyes HAOUAS040aff22018-05-27 16:30:36 +0200652static void pcie_set_subsystem(struct device *dev, unsigned int vendor,
Lee Leahy23602df2017-03-16 19:00:37 -0700653 unsigned int device)
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700654{
655 /* NOTE: This is not the default position! */
656 if (!vendor || !device)
657 pci_write_config32(dev, 0x94, pci_read_config32(dev, 0));
658 else
659 pci_write_config32(dev, 0x94, (device << 16) | vendor);
660}
661
Elyes HAOUAS040aff22018-05-27 16:30:36 +0200662static void pcie_set_L1_ss_max_latency(struct device *dev, unsigned int off)
Kenji Chenb71d9b82014-10-10 03:08:15 +0800663{
664 /* Set max snoop and non-snoop latency for Broadwell */
Kyösti Mälkkib4a45dc2013-07-26 08:53:59 +0300665 pci_write_config32(dev, off, 0x10031003);
Kenji Chenb71d9b82014-10-10 03:08:15 +0800666}
667
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700668static struct pci_operations pcie_ops = {
669 .set_subsystem = pcie_set_subsystem,
Kenji Chenb71d9b82014-10-10 03:08:15 +0800670 .set_L1_ss_latency = pcie_set_L1_ss_max_latency,
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700671};
672
673static struct device_operations device_ops = {
674 .read_resources = pci_bus_read_resources,
675 .set_resources = pci_dev_set_resources,
676 .enable_resources = pci_bus_enable_resources,
677 .init = pch_pcie_init,
678 .enable = pch_pcie_enable,
679 .scan_bus = pciexp_scan_bridge,
680 .ops_pci = &pcie_ops,
681};
682
683static const unsigned short pcie_device_ids[] = {
684 /* Lynxpoint-LP */
685 0x9c10, 0x9c12, 0x9c14, 0x9c16, 0x9c18, 0x9c1a,
686 /* WildcatPoint */
687 0x9c90, 0x9c92, 0x9c94, 0x9c96, 0x9c98, 0x9c9a, 0x2448,
688 0
689};
690
691static const struct pci_driver pch_pcie __pci_driver = {
692 .ops = &device_ops,
693 .vendor = PCI_VENDOR_ID_INTEL,
694 .devices = pcie_device_ids,
695};