blob: c3d9e13c86889c82f8443b2e51a7b79843ad2953 [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>
Julius Werner4ee4bd52014-10-20 13:46:39 -070023#include <soc/gpio.h>
24#include <soc/lpc.h>
25#include <soc/iobp.h>
26#include <soc/pch.h>
27#include <soc/pci_devs.h>
28#include <soc/rcba.h>
29#include <soc/intel/broadwell/chip.h>
30#include <soc/cpu.h>
Wenkai Du83067612014-12-05 14:00:26 -080031#include <delay.h>
Duncan Lauriec88c54c2014-04-30 16:36:13 -070032
Duncan Lauriec88c54c2014-04-30 16:36:13 -070033/* Low Power variant has 6 root ports. */
34#define NUM_ROOT_PORTS 6
35
36struct root_port_config {
37 /* RPFN is a write-once register so keep a copy until it is written */
38 u32 orig_rpfn;
39 u32 new_rpfn;
40 u32 pin_ownership;
41 u32 strpfusecfg1;
42 u32 strpfusecfg2;
43 u32 strpfusecfg3;
44 u32 b0d28f0_32c;
45 u32 b0d28f4_32c;
46 u32 b0d28f5_32c;
47 int coalesce;
48 int gbe_port;
49 int num_ports;
50 device_t ports[NUM_ROOT_PORTS];
51};
52
53static struct root_port_config rpc;
54
55static inline int root_port_is_first(device_t dev)
56{
57 return PCI_FUNC(dev->path.pci.devfn) == 0;
58}
59
60static inline int root_port_is_last(device_t dev)
61{
62 return PCI_FUNC(dev->path.pci.devfn) == (rpc.num_ports - 1);
63}
64
65/* Root ports are numbered 1..N in the documentation. */
66static inline int root_port_number(device_t dev)
67{
68 return PCI_FUNC(dev->path.pci.devfn) + 1;
69}
70
71static 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 switch ((rpc.strpfusecfg1 >> 16) & 0x7) {
78 case 0:
79 rpc.gbe_port = 3;
80 break;
81 case 1:
82 rpc.gbe_port = 4;
83 break;
84 case 2:
85 case 3:
86 case 4:
87 case 5:
88 /* Lanes 0-4 of Root Port 5. */
89 rpc.gbe_port = 5;
90 break;
91 default:
92 printk(BIOS_DEBUG, "Invalid GbE Port Selection.\n");
93 }
94}
95
Kenji Chen87d4a202014-09-24 01:18:26 +080096static void pcie_iosf_port_grant_count(device_t dev)
97{
98 u8 update_val;
Patrick Georgie8f2ef52016-07-29 18:53:34 +020099 u32 rpcd = (pci_read_config32(dev, 0xfc) >> 14) & 0x3;
Kenji Chen87d4a202014-09-24 01:18:26 +0800100
101 switch (rpcd) {
102 case 1:
103 case 3:
104 update_val = 0x02;
105 break;
106 case 2:
107 update_val = 0x22;
108 break;
109 default:
110 update_val = 0x00;
111 break;
112 }
113
114 RCBA32(0x103C) = (RCBA32(0x103C) & (~0xff)) | update_val;
115}
116
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700117static void root_port_init_config(device_t dev)
118{
119 int rp;
Martin Roth2b2ff7f2015-12-18 10:46:59 -0700120 u32 data = 0;
Kenji Chene383feb2014-09-26 03:14:57 +0800121 u8 resp, id;
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700122
123 if (root_port_is_first(dev)) {
124 rpc.orig_rpfn = RCBA32(RPFN);
125 rpc.new_rpfn = rpc.orig_rpfn;
126 rpc.num_ports = NUM_ROOT_PORTS;
127 rpc.gbe_port = -1;
Kenji Chen87d4a202014-09-24 01:18:26 +0800128 /* RP0 f5[3:0] = 0101b*/
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300129 pci_update_config8(dev, 0xf5, ~0xa, 0x5);
Kenji Chen87d4a202014-09-24 01:18:26 +0800130
131 pcie_iosf_port_grant_count(dev);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700132
133 rpc.pin_ownership = pci_read_config32(dev, 0x410);
134 root_port_config_update_gbe_port();
135
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300136 pci_update_config8(dev, 0xe2, ~(3 << 4), (3 << 4));
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700137 if (dev->chip_info != NULL) {
138 config_t *config = dev->chip_info;
139 rpc.coalesce = config->pcie_port_coalesce;
140 }
141 }
142
143 rp = root_port_number(dev);
144 if (rp > rpc.num_ports) {
145 printk(BIOS_ERR, "Found Root Port %d, expecting %d\n",
146 rp, rpc.num_ports);
147 return;
148 }
149
150 /* Read the fuse configuration and pin ownership. */
151 switch (rp) {
152 case 1:
153 rpc.strpfusecfg1 = pci_read_config32(dev, 0xfc);
154 rpc.b0d28f0_32c = pci_read_config32(dev, 0x32c);
155 break;
156 case 5:
157 rpc.strpfusecfg2 = pci_read_config32(dev, 0xfc);
158 rpc.b0d28f4_32c = pci_read_config32(dev, 0x32c);
159 break;
160 case 6:
161 rpc.b0d28f5_32c = pci_read_config32(dev, 0x32c);
162 rpc.strpfusecfg3 = pci_read_config32(dev, 0xfc);
163 break;
164 default:
165 break;
166 }
167
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300168 pci_update_config32(dev, 0x418, 0, 0x02000430);
Kenji Chene383feb2014-09-26 03:14:57 +0800169
Kenji Chene383feb2014-09-26 03:14:57 +0800170 if (root_port_is_first(dev)) {
Kenji Chene8f36642014-10-04 02:59:06 +0800171 /*
172 * set RP0 PCICFG E2h[5:4] = 11b and E1h[6] = 1
173 * before configuring ASPM
174 */
Kenji Chene383feb2014-09-26 03:14:57 +0800175 id = 0xe0 + (u8)(RCBA32(RPFN) & 0x07);
176 pch_iobp_exec(0xE00000E0, IOBP_PCICFG_READ, id, &data, &resp);
Kenji Chene8f36642014-10-04 02:59:06 +0800177 data |= ((0x30 << 16) | (0x40 << 8));
Kenji Chene383feb2014-09-26 03:14:57 +0800178 pch_iobp_exec(0xE00000E0, IOBP_PCICFG_WRITE, id, &data, &resp);
179 }
180
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700181 /* Cache pci device. */
182 rpc.ports[rp - 1] = dev;
183}
184
185/* Update devicetree with new Root Port function number assignment */
186static void pch_pcie_device_set_func(int index, int pci_func)
187{
188 device_t dev;
189 unsigned new_devfn;
190
191 dev = rpc.ports[index];
192
193 /* Set the new PCI function field for this Root Port. */
194 rpc.new_rpfn &= ~RPFN_FNMASK(index);
195 rpc.new_rpfn |= RPFN_FNSET(index, pci_func);
196
197 /* Determine the new devfn for this port */
198 new_devfn = PCI_DEVFN(PCH_DEV_SLOT_PCIE, pci_func);
199
200 if (dev->path.pci.devfn != new_devfn) {
201 printk(BIOS_DEBUG,
202 "PCH: PCIe map %02x.%1x -> %02x.%1x\n",
203 PCI_SLOT(dev->path.pci.devfn),
204 PCI_FUNC(dev->path.pci.devfn),
205 PCI_SLOT(new_devfn), PCI_FUNC(new_devfn));
206
207 dev->path.pci.devfn = new_devfn;
208 }
209}
210
211static void pcie_enable_clock_gating(void)
212{
213 int i;
214 int enabled_ports = 0;
Kane Chen4fef5a22014-08-27 15:21:32 -0700215 int is_broadwell = !!(cpu_family_model() == BROADWELL_FAMILY_ULT);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700216
217 for (i = 0; i < rpc.num_ports; i++) {
218 device_t dev;
219 int rp;
220
221 dev = rpc.ports[i];
222 rp = root_port_number(dev);
223
224 if (!dev->enabled) {
225 /* Configure shared resource clock gating. */
226 if (rp == 1 || rp == 5 || rp == 6)
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300227 pci_update_config8(dev, 0xe1, 0xc3, 0x3c);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700228
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300229 pci_update_config8(dev, 0xe2, ~(3 << 4), (3 << 4));
230 pci_update_config32(dev, 0x420, ~(1 << 31), (1 << 31));
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700231
232 /* Per-Port CLKREQ# handling. */
233 if (gpio_is_native(18 + rp - 1))
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300234 pci_update_config32(dev, 0x420, ~0, (3 << 29));
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700235
236 /* Enable static clock gating. */
237 if (rp == 1 && !rpc.ports[1]->enabled &&
238 !rpc.ports[2]->enabled && !rpc.ports[3]->enabled) {
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300239 pci_update_config8(dev, 0xe2, ~1, 1);
240 pci_update_config8(dev, 0xe1, 0x7f, 0x80);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700241 } else if (rp == 5 || rp == 6) {
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300242 pci_update_config8(dev, 0xe2, ~1, 1);
243 pci_update_config8(dev, 0xe1, 0x7f, 0x80);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700244 }
245 continue;
246 }
247
248 enabled_ports++;
249
250 /* Enable dynamic clock gating. */
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300251 pci_update_config8(dev, 0xe1, 0xfc, 0x03);
252 pci_update_config8(dev, 0xe2, ~(1 << 6), (1 << 6));
253 pci_update_config8(dev, 0xe8, ~(3 << 2), (2 << 2));
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700254
255 /* Update PECR1 register. */
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300256 pci_update_config8(dev, 0xe8, ~0, 3);
Kane Chen4fef5a22014-08-27 15:21:32 -0700257 if (is_broadwell) {
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300258 pci_update_config32(dev, 0x324, ~((1 << 5) | (1 << 14)),
Kane Chen4fef5a22014-08-27 15:21:32 -0700259 ((1 << 5) | (1 << 14)));
260 } else {
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300261 pci_update_config32(dev, 0x324, ~(1 << 5), (1 << 5));
Kane Chen4fef5a22014-08-27 15:21:32 -0700262 }
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700263 /* Per-Port CLKREQ# handling. */
264 if (gpio_is_native(18 + rp - 1))
Kenji Chene8f36642014-10-04 02:59:06 +0800265 /*
266 * In addition to D28Fx PCICFG 420h[30:29] = 11b,
267 * set 420h[17] = 0b and 420[0] = 1b for L1 SubState.
268 */
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300269 pci_update_config32(dev, 0x420, ~0x20000, (3 << 29) | 1);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700270
271 /* Configure shared resource clock gating. */
272 if (rp == 1 || rp == 5 || rp == 6)
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300273 pci_update_config8(dev, 0xe1, 0xc3, 0x3c);
Duncan Laurie446fb8e2014-08-08 09:59:43 -0700274
275 /* CLKREQ# VR Idle Enable */
276 RCBA32_OR(0x2b1c, (1 << (16 + i)));
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700277 }
278
279 if (!enabled_ports)
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300280 pci_update_config8(rpc.ports[0], 0xe1, ~(1 << 6), (1 << 6));
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700281}
282
283static void root_port_commit_config(void)
284{
285 int i;
286
287 /* If the first root port is disabled the coalesce ports. */
288 if (!rpc.ports[0]->enabled)
289 rpc.coalesce = 1;
290
291 /* Perform clock gating configuration. */
292 pcie_enable_clock_gating();
293
294 for (i = 0; i < rpc.num_ports; i++) {
295 device_t dev;
296 u32 reg32;
Wenkai Du83067612014-12-05 14:00:26 -0800297 int n = 0;
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700298
299 dev = rpc.ports[i];
300
301 if (dev == NULL) {
302 printk(BIOS_ERR, "Root Port %d device is NULL?\n", i+1);
303 continue;
304 }
305
306 if (dev->enabled)
307 continue;
308
309 printk(BIOS_DEBUG, "%s: Disabling device\n", dev_path(dev));
310
Wenkai Du83067612014-12-05 14:00:26 -0800311 /* 8.2 Configuration of PCI Express Root Ports */
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300312 pci_update_config32(dev, 0x338, ~(1 << 26), 1 << 26);
Wenkai Du83067612014-12-05 14:00:26 -0800313
314 do {
315 reg32 = pci_read_config32(dev, 0x328);
316 n++;
Duncan Lauriecad2b7b2015-01-14 17:30:20 -0800317 if (((reg32 & 0xff000000) == 0x01000000) || (n > 50))
Wenkai Du83067612014-12-05 14:00:26 -0800318 break;
319 udelay(100);
320 } while (1);
321
Duncan Lauriecad2b7b2015-01-14 17:30:20 -0800322 if (n > 50)
Wenkai Du83067612014-12-05 14:00:26 -0800323 printk(BIOS_DEBUG, "%s: Timeout waiting for 328h\n",
324 dev_path(dev));
325
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300326 pci_update_config32(dev, 0x408, ~(1 << 27), 1 << 27);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700327
328 /* Disable this device if possible */
329 pch_disable_devfn(dev);
330 }
331
332 if (rpc.coalesce) {
333 int current_func;
334
335 /* For all Root Ports N enabled ports get assigned the lower
336 * PCI function number. The disabled ones get upper PCI
337 * function numbers. */
338 current_func = 0;
339 for (i = 0; i < rpc.num_ports; i++) {
340 if (!rpc.ports[i]->enabled)
341 continue;
342 pch_pcie_device_set_func(i, current_func);
343 current_func++;
344 }
345
346 /* Allocate the disabled devices' PCI function number. */
347 for (i = 0; i < rpc.num_ports; i++) {
348 if (rpc.ports[i]->enabled)
349 continue;
350 pch_pcie_device_set_func(i, current_func);
351 current_func++;
352 }
353 }
354
355 printk(BIOS_SPEW, "PCH: RPFN 0x%08x -> 0x%08x\n",
356 rpc.orig_rpfn, rpc.new_rpfn);
357 RCBA32(RPFN) = rpc.new_rpfn;
358}
359
360static void root_port_mark_disable(device_t dev)
361{
362 /* Mark device as disabled. */
363 dev->enabled = 0;
364 /* Mark device to be hidden. */
365 rpc.new_rpfn |= RPFN_HIDE(PCI_FUNC(dev->path.pci.devfn));
366}
367
368static void root_port_check_disable(device_t dev)
369{
370 int rp;
371
372 /* Device already disabled. */
373 if (!dev->enabled) {
374 root_port_mark_disable(dev);
375 return;
376 }
377
378 rp = root_port_number(dev);
379
380 /* Is the GbE port mapped to this Root Port? */
381 if (rp == rpc.gbe_port) {
382 root_port_mark_disable(dev);
383 return;
384 }
385
386 /* Check Root Port Configuration. */
387 switch (rp) {
388 case 2:
389 /* Root Port 2 is disabled for all lane configurations
390 * but config 00b (4x1 links). */
391 if ((rpc.strpfusecfg1 >> 14) & 0x3) {
392 root_port_mark_disable(dev);
393 return;
394 }
395 break;
396 case 3:
397 /* Root Port 3 is disabled in config 11b (1x4 links). */
398 if (((rpc.strpfusecfg1 >> 14) & 0x3) == 0x3) {
399 root_port_mark_disable(dev);
400 return;
401 }
402 break;
403 case 4:
404 /* Root Port 4 is disabled in configs 11b (1x4 links)
405 * and 10b (2x2 links). */
406 if ((rpc.strpfusecfg1 >> 14) & 0x2) {
407 root_port_mark_disable(dev);
408 return;
409 }
410 break;
411 }
412
413 /* Check Pin Ownership. */
414 switch (rp) {
415 case 1:
416 /* Bit 0 is Root Port 1 ownership. */
417 if ((rpc.pin_ownership & 0x1) == 0) {
418 root_port_mark_disable(dev);
419 return;
420 }
421 break;
422 case 2:
423 /* Bit 2 is Root Port 2 ownership. */
424 if ((rpc.pin_ownership & 0x4) == 0) {
425 root_port_mark_disable(dev);
426 return;
427 }
428 break;
429 case 6:
430 /* Bits 7:4 are Root Port 6 pin-lane ownership. */
431 if ((rpc.pin_ownership & 0xf0) == 0) {
432 root_port_mark_disable(dev);
433 return;
434 }
435 break;
436 }
437}
438
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700439static void pcie_add_0x0202000_iobp(u32 reg)
440{
441 u32 reg32;
442
443 reg32 = pch_iobp_read(reg);
444 reg32 += (0x2 << 16) | (0x2 << 8);
445 pch_iobp_write(reg, reg32);
446}
447
448static void pch_pcie_early(struct device *dev)
449{
450 config_t *config = dev->chip_info;
451 int do_aspm = 0;
452 int rp = root_port_number(dev);
453
454 switch (rp) {
455 case 1:
456 case 2:
457 case 3:
458 case 4:
459 /*
Martin Rothde7ed6f2014-12-07 14:58:18 -0700460 * Bits 31:28 of b0d28f0 0x32c register correspond to
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700461 * Root Ports 4:1.
462 */
463 do_aspm = !!(rpc.b0d28f0_32c & (1 << (28 + rp - 1)));
464 break;
465 case 5:
466 /*
Martin Rothde7ed6f2014-12-07 14:58:18 -0700467 * Bit 28 of b0d28f4 0x32c register correspond to
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700468 * Root Ports 4:1.
469 */
470 do_aspm = !!(rpc.b0d28f4_32c & (1 << 28));
471 break;
472 case 6:
473 /*
Martin Rothde7ed6f2014-12-07 14:58:18 -0700474 * Bit 28 of b0d28f5 0x32c register correspond to
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700475 * Root Ports 4:1.
476 */
477 do_aspm = !!(rpc.b0d28f5_32c & (1 << 28));
478 break;
479 }
480
481 /* Allow ASPM to be forced on in devicetree */
482 if (config && (config->pcie_port_force_aspm & (1 << (rp - 1))))
483 do_aspm = 1;
484
485 printk(BIOS_DEBUG, "PCIe Root Port %d ASPM is %sabled\n",
486 rp, do_aspm ? "en" : "dis");
487
488 if (do_aspm) {
489 /* Set ASPM bits in MPC2 register. */
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300490 pci_update_config32(dev, 0xd4, ~(0x3 << 2), (1 << 4) | (0x2 << 2));
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700491
492 /* Set unique clock exit latency in MPC register. */
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300493 pci_update_config32(dev, 0xd8, ~(0x7 << 18), (0x7 << 18));
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700494
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700495 switch (rp) {
496 case 1:
497 pcie_add_0x0202000_iobp(0xe9002440);
498 break;
499 case 2:
500 pcie_add_0x0202000_iobp(0xe9002640);
501 break;
502 case 3:
503 pcie_add_0x0202000_iobp(0xe9000840);
504 break;
505 case 4:
506 pcie_add_0x0202000_iobp(0xe9000a40);
507 break;
508 case 5:
509 pcie_add_0x0202000_iobp(0xe9000c40);
510 pcie_add_0x0202000_iobp(0xe9000e40);
511 pcie_add_0x0202000_iobp(0xe9001040);
512 pcie_add_0x0202000_iobp(0xe9001240);
513 break;
514 case 6:
515 /* Update IOBP based on lane ownership. */
516 if (rpc.pin_ownership & (1 << 4))
517 pcie_add_0x0202000_iobp(0xea002040);
518 if (rpc.pin_ownership & (1 << 5))
519 pcie_add_0x0202000_iobp(0xea002240);
520 if (rpc.pin_ownership & (1 << 6))
521 pcie_add_0x0202000_iobp(0xea002440);
522 if (rpc.pin_ownership & (1 << 7))
523 pcie_add_0x0202000_iobp(0xea002640);
524 break;
525 }
526
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300527 pci_update_config32(dev, 0x338, ~(1 << 26), 0);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700528 }
529
Kenji Chenc373f502014-09-26 02:48:16 +0800530 /* Enable LTR in Root Port. Disable OBFF. */
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300531 pci_update_config32(dev, 0x64, ~(1 << 11) & ~(3 << 18), (1 << 11));
532 pci_update_config32(dev, 0x68, ~(1 << 10), (1 << 10));
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700533
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300534 pci_update_config32(dev, 0x318, ~(0xffff << 16), (0x1414 << 16));
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700535
536 /* Set L1 exit latency in LCAP register. */
537 if (!do_aspm && (pci_read_config8(dev, 0xf5) & 0x1))
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300538 pci_update_config32(dev, 0x4c, ~(0x7 << 15), (0x4 << 15));
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700539 else
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300540 pci_update_config32(dev, 0x4c, ~(0x7 << 15), (0x2 << 15));
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700541
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300542 pci_update_config32(dev, 0x314, 0x0, 0x743a361b);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700543
544 /* Set Common Clock Exit Latency in MPC register. */
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300545 pci_update_config32(dev, 0xd8, ~(0x7 << 15), (0x3 << 15));
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700546
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300547 pci_update_config32(dev, 0x33c, ~0x00ffffff, 0x854d74);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700548
Martin Rothde7ed6f2014-12-07 14:58:18 -0700549 /* Set Invalid Receive Range Check Enable in MPC register. */
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300550 pci_update_config32(dev, 0xd8, ~0, (1 << 25));
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700551
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300552 pci_update_config8(dev, 0xf5, 0x0f, 0);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700553
Kenji Chen94fea492014-09-30 14:17:35 +0800554 /* Set AER Extended Cap ID to 01h and Next Cap Pointer to 200h. */
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300555 pci_update_config32(dev, 0x100, ~(1 << 29) & ~0xfffff, (1 << 29) | 0x10001);
Kenji Chen8ef55ee2014-09-25 21:34:42 +0800556
Kenji Chen94fea492014-09-30 14:17:35 +0800557 /* Set L1 Sub-State Cap ID to 1Eh and Next Cap Pointer to None. */
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300558 pci_update_config32(dev, 0x200, ~0xffff, 0x001e);
Kenji Chen94fea492014-09-30 14:17:35 +0800559
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300560 pci_update_config32(dev, 0x320, ~(3 << 20) & ~(7 << 6),
Kenji Chenc373f502014-09-26 02:48:16 +0800561 (1 << 20) | (3 << 6));
562 /* Enable Relaxed Order from Root Port. */
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300563 pci_update_config32(dev, 0x320, ~(3 << 23), (3 << 23));
Kenji Chenc373f502014-09-26 02:48:16 +0800564
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700565 if (rp == 1 || rp == 5 || rp == 6)
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300566 pci_update_config8(dev, 0xf7, ~0xc, 0);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700567
568 /* Set EOI forwarding disable. */
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300569 pci_update_config32(dev, 0xd4, ~0, (1 << 1));
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700570
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700571 /* Read and write back write-once capability registers. */
Kyösti Mälkki48c389e2013-07-26 08:53:59 +0300572 pci_update_config32(dev, 0x34, ~0, 0);
573 pci_update_config32(dev, 0x40, ~0, 0);
574 pci_update_config32(dev, 0x80, ~0, 0);
575 pci_update_config32(dev, 0x90, ~0, 0);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700576}
577
578static void pch_pcie_init(struct device *dev)
579{
580 u16 reg16;
581 u32 reg32;
582
583 printk(BIOS_DEBUG, "Initializing PCH PCIe bridge.\n");
584
585 /* Enable SERR */
586 reg32 = pci_read_config32(dev, PCI_COMMAND);
587 reg32 |= PCI_COMMAND_SERR;
588 pci_write_config32(dev, PCI_COMMAND, reg32);
589
590 /* Enable Bus Master */
591 reg32 = pci_read_config32(dev, PCI_COMMAND);
592 reg32 |= PCI_COMMAND_MASTER;
593 pci_write_config32(dev, PCI_COMMAND, reg32);
594
595 /* Set Cache Line Size to 0x10 */
596 pci_write_config8(dev, 0x0c, 0x10);
597
598 reg16 = pci_read_config16(dev, 0x3e);
599 reg16 &= ~(1 << 0); /* disable parity error response */
600 reg16 |= (1 << 2); /* ISA enable */
601 pci_write_config16(dev, 0x3e, reg16);
602
603#ifdef EVEN_MORE_DEBUG
604 reg32 = pci_read_config32(dev, 0x20);
605 printk(BIOS_SPEW, " MBL = 0x%08x\n", reg32);
606 reg32 = pci_read_config32(dev, 0x24);
607 printk(BIOS_SPEW, " PMBL = 0x%08x\n", reg32);
608 reg32 = pci_read_config32(dev, 0x28);
609 printk(BIOS_SPEW, " PMBU32 = 0x%08x\n", reg32);
610 reg32 = pci_read_config32(dev, 0x2c);
611 printk(BIOS_SPEW, " PMLU32 = 0x%08x\n", reg32);
612#endif
613
614 /* Clear errors in status registers */
615 reg16 = pci_read_config16(dev, 0x06);
616 pci_write_config16(dev, 0x06, reg16);
617 reg16 = pci_read_config16(dev, 0x1e);
618 pci_write_config16(dev, 0x1e, reg16);
619}
620
621static void pch_pcie_enable(device_t dev)
622{
623 /* Add this device to the root port config structure. */
624 root_port_init_config(dev);
625
626 /* Check to see if this Root Port should be disabled. */
627 root_port_check_disable(dev);
628
629 /* Power Management init before enumeration */
630 if (dev->enabled)
631 pch_pcie_early(dev);
632
633 /*
634 * When processing the last PCIe root port we can now
635 * update the Root Port Function Number and Hide register.
636 */
637 if (root_port_is_last(dev))
638 root_port_commit_config();
639}
640
641static void pcie_set_subsystem(device_t dev, unsigned vendor, unsigned device)
642{
643 /* NOTE: This is not the default position! */
644 if (!vendor || !device)
645 pci_write_config32(dev, 0x94, pci_read_config32(dev, 0));
646 else
647 pci_write_config32(dev, 0x94, (device << 16) | vendor);
648}
649
Kenji Chenb71d9b82014-10-10 03:08:15 +0800650static void pcie_set_L1_ss_max_latency(device_t dev, unsigned int off)
651{
652 /* Set max snoop and non-snoop latency for Broadwell */
Kyösti Mälkkib4a45dc2013-07-26 08:53:59 +0300653 pci_write_config32(dev, off, 0x10031003);
Kenji Chenb71d9b82014-10-10 03:08:15 +0800654}
655
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700656static struct pci_operations pcie_ops = {
657 .set_subsystem = pcie_set_subsystem,
Kenji Chenb71d9b82014-10-10 03:08:15 +0800658 .set_L1_ss_latency = pcie_set_L1_ss_max_latency,
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700659};
660
661static struct device_operations device_ops = {
662 .read_resources = pci_bus_read_resources,
663 .set_resources = pci_dev_set_resources,
664 .enable_resources = pci_bus_enable_resources,
665 .init = pch_pcie_init,
666 .enable = pch_pcie_enable,
667 .scan_bus = pciexp_scan_bridge,
668 .ops_pci = &pcie_ops,
669};
670
671static const unsigned short pcie_device_ids[] = {
672 /* Lynxpoint-LP */
673 0x9c10, 0x9c12, 0x9c14, 0x9c16, 0x9c18, 0x9c1a,
674 /* WildcatPoint */
675 0x9c90, 0x9c92, 0x9c94, 0x9c96, 0x9c98, 0x9c9a, 0x2448,
676 0
677};
678
679static const struct pci_driver pch_pcie __pci_driver = {
680 .ops = &device_ops,
681 .vendor = PCI_VENDOR_ID_INTEL,
682 .devices = pcie_device_ids,
683};