blob: a251a17a89fae07bfaf48d7326d4a1e9ed2c4b1d [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.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21#include <console/console.h>
22#include <device/device.h>
23#include <device/pci.h>
24#include <device/pciexp.h>
25#include <device/pci_def.h>
26#include <device/pci_ids.h>
27#include <broadwell/gpio.h>
28#include <broadwell/lpc.h>
29#include <broadwell/iobp.h>
30#include <broadwell/pch.h>
31#include <broadwell/pci_devs.h>
32#include <broadwell/rcba.h>
33#include <chip.h>
34
35static void pcie_update_cfg8(device_t dev, int reg, u8 mask, u8 or);
36static void pcie_update_cfg(device_t dev, int reg, u32 mask, u32 or);
37
38/* Low Power variant has 6 root ports. */
39#define NUM_ROOT_PORTS 6
40
41struct root_port_config {
42 /* RPFN is a write-once register so keep a copy until it is written */
43 u32 orig_rpfn;
44 u32 new_rpfn;
45 u32 pin_ownership;
46 u32 strpfusecfg1;
47 u32 strpfusecfg2;
48 u32 strpfusecfg3;
49 u32 b0d28f0_32c;
50 u32 b0d28f4_32c;
51 u32 b0d28f5_32c;
52 int coalesce;
53 int gbe_port;
54 int num_ports;
55 device_t ports[NUM_ROOT_PORTS];
56};
57
58static struct root_port_config rpc;
59
60static inline int root_port_is_first(device_t dev)
61{
62 return PCI_FUNC(dev->path.pci.devfn) == 0;
63}
64
65static inline int root_port_is_last(device_t dev)
66{
67 return PCI_FUNC(dev->path.pci.devfn) == (rpc.num_ports - 1);
68}
69
70/* Root ports are numbered 1..N in the documentation. */
71static inline int root_port_number(device_t dev)
72{
73 return PCI_FUNC(dev->path.pci.devfn) + 1;
74}
75
76static void root_port_config_update_gbe_port(void)
77{
78 /* Is the Gbe Port enabled? */
79 if (!((rpc.strpfusecfg1 >> 19) & 1))
80 return;
81
82 switch ((rpc.strpfusecfg1 >> 16) & 0x7) {
83 case 0:
84 rpc.gbe_port = 3;
85 break;
86 case 1:
87 rpc.gbe_port = 4;
88 break;
89 case 2:
90 case 3:
91 case 4:
92 case 5:
93 /* Lanes 0-4 of Root Port 5. */
94 rpc.gbe_port = 5;
95 break;
96 default:
97 printk(BIOS_DEBUG, "Invalid GbE Port Selection.\n");
98 }
99}
100
101static void root_port_init_config(device_t dev)
102{
103 int rp;
104
105 if (root_port_is_first(dev)) {
106 rpc.orig_rpfn = RCBA32(RPFN);
107 rpc.new_rpfn = rpc.orig_rpfn;
108 rpc.num_ports = NUM_ROOT_PORTS;
109 rpc.gbe_port = -1;
110
111 rpc.pin_ownership = pci_read_config32(dev, 0x410);
112 root_port_config_update_gbe_port();
113
114 if (dev->chip_info != NULL) {
115 config_t *config = dev->chip_info;
116 rpc.coalesce = config->pcie_port_coalesce;
117 }
118 }
119
120 rp = root_port_number(dev);
121 if (rp > rpc.num_ports) {
122 printk(BIOS_ERR, "Found Root Port %d, expecting %d\n",
123 rp, rpc.num_ports);
124 return;
125 }
126
127 /* Read the fuse configuration and pin ownership. */
128 switch (rp) {
129 case 1:
130 rpc.strpfusecfg1 = pci_read_config32(dev, 0xfc);
131 rpc.b0d28f0_32c = pci_read_config32(dev, 0x32c);
132 break;
133 case 5:
134 rpc.strpfusecfg2 = pci_read_config32(dev, 0xfc);
135 rpc.b0d28f4_32c = pci_read_config32(dev, 0x32c);
136 break;
137 case 6:
138 rpc.b0d28f5_32c = pci_read_config32(dev, 0x32c);
139 rpc.strpfusecfg3 = pci_read_config32(dev, 0xfc);
140 break;
141 default:
142 break;
143 }
144
145 /* Cache pci device. */
146 rpc.ports[rp - 1] = dev;
147}
148
149/* Update devicetree with new Root Port function number assignment */
150static void pch_pcie_device_set_func(int index, int pci_func)
151{
152 device_t dev;
153 unsigned new_devfn;
154
155 dev = rpc.ports[index];
156
157 /* Set the new PCI function field for this Root Port. */
158 rpc.new_rpfn &= ~RPFN_FNMASK(index);
159 rpc.new_rpfn |= RPFN_FNSET(index, pci_func);
160
161 /* Determine the new devfn for this port */
162 new_devfn = PCI_DEVFN(PCH_DEV_SLOT_PCIE, pci_func);
163
164 if (dev->path.pci.devfn != new_devfn) {
165 printk(BIOS_DEBUG,
166 "PCH: PCIe map %02x.%1x -> %02x.%1x\n",
167 PCI_SLOT(dev->path.pci.devfn),
168 PCI_FUNC(dev->path.pci.devfn),
169 PCI_SLOT(new_devfn), PCI_FUNC(new_devfn));
170
171 dev->path.pci.devfn = new_devfn;
172 }
173}
174
175static void pcie_enable_clock_gating(void)
176{
177 int i;
178 int enabled_ports = 0;
179
180 for (i = 0; i < rpc.num_ports; i++) {
181 device_t dev;
182 int rp;
183
184 dev = rpc.ports[i];
185 rp = root_port_number(dev);
186
187 if (!dev->enabled) {
188 /* Configure shared resource clock gating. */
189 if (rp == 1 || rp == 5 || rp == 6)
190 pcie_update_cfg8(dev, 0xe1, 0xc3, 0x3c);
191
192 pcie_update_cfg8(dev, 0xe2, ~(3 << 4), (3 << 4));
193 pcie_update_cfg(dev, 0x420, ~(1 << 31), (1 << 31));
194
195 /* Per-Port CLKREQ# handling. */
196 if (gpio_is_native(18 + rp - 1))
197 pcie_update_cfg(dev, 0x420, ~0, (3 << 29));
198
199 /* Enable static clock gating. */
200 if (rp == 1 && !rpc.ports[1]->enabled &&
201 !rpc.ports[2]->enabled && !rpc.ports[3]->enabled) {
202 pcie_update_cfg8(dev, 0xe2, ~1, 1);
203 pcie_update_cfg8(dev, 0xe1, 0x7f, 0x80);
204 } else if (rp == 5 || rp == 6) {
205 pcie_update_cfg8(dev, 0xe2, ~1, 1);
206 pcie_update_cfg8(dev, 0xe1, 0x7f, 0x80);
207 }
208 continue;
209 }
210
211 enabled_ports++;
212
213 /* Enable dynamic clock gating. */
214 pcie_update_cfg8(dev, 0xe1, 0xfc, 0x03);
215 pcie_update_cfg8(dev, 0xe2, ~(1 << 6), (1 << 6));
216 pcie_update_cfg8(dev, 0xe8, ~(3 << 2), (2 << 2));
217
218 /* Update PECR1 register. */
219 pcie_update_cfg8(dev, 0xe8, ~0, 1);
220 pcie_update_cfg8(dev, 0x324, ~(1 << 5), (1 < 5));
221
222 /* Per-Port CLKREQ# handling. */
223 if (gpio_is_native(18 + rp - 1))
224 pcie_update_cfg(dev, 0x420, ~0, (3 << 29));
225
226 /* Configure shared resource clock gating. */
227 if (rp == 1 || rp == 5 || rp == 6)
228 pcie_update_cfg8(dev, 0xe1, 0xc3, 0x3c);
Duncan Laurie446fb8e2014-08-08 09:59:43 -0700229
230 /* CLKREQ# VR Idle Enable */
231 RCBA32_OR(0x2b1c, (1 << (16 + i)));
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700232 }
233
234 if (!enabled_ports)
235 pcie_update_cfg8(rpc.ports[0], 0xe1, ~(1 << 6), (1 << 6));
236}
237
238static void root_port_commit_config(void)
239{
240 int i;
241
242 /* If the first root port is disabled the coalesce ports. */
243 if (!rpc.ports[0]->enabled)
244 rpc.coalesce = 1;
245
246 /* Perform clock gating configuration. */
247 pcie_enable_clock_gating();
248
249 for (i = 0; i < rpc.num_ports; i++) {
250 device_t dev;
251 u32 reg32;
252
253 dev = rpc.ports[i];
254
255 if (dev == NULL) {
256 printk(BIOS_ERR, "Root Port %d device is NULL?\n", i+1);
257 continue;
258 }
259
260 if (dev->enabled)
261 continue;
262
263 printk(BIOS_DEBUG, "%s: Disabling device\n", dev_path(dev));
264
265 /* Ensure memory, io, and bus master are all disabled */
266 reg32 = pci_read_config32(dev, PCI_COMMAND);
267 reg32 &= ~(PCI_COMMAND_MASTER |
268 PCI_COMMAND_MEMORY | PCI_COMMAND_IO);
269 pci_write_config32(dev, PCI_COMMAND, reg32);
270
271 /* Disable this device if possible */
272 pch_disable_devfn(dev);
273 }
274
275 if (rpc.coalesce) {
276 int current_func;
277
278 /* For all Root Ports N enabled ports get assigned the lower
279 * PCI function number. The disabled ones get upper PCI
280 * function numbers. */
281 current_func = 0;
282 for (i = 0; i < rpc.num_ports; i++) {
283 if (!rpc.ports[i]->enabled)
284 continue;
285 pch_pcie_device_set_func(i, current_func);
286 current_func++;
287 }
288
289 /* Allocate the disabled devices' PCI function number. */
290 for (i = 0; i < rpc.num_ports; i++) {
291 if (rpc.ports[i]->enabled)
292 continue;
293 pch_pcie_device_set_func(i, current_func);
294 current_func++;
295 }
296 }
297
298 printk(BIOS_SPEW, "PCH: RPFN 0x%08x -> 0x%08x\n",
299 rpc.orig_rpfn, rpc.new_rpfn);
300 RCBA32(RPFN) = rpc.new_rpfn;
301}
302
303static void root_port_mark_disable(device_t dev)
304{
305 /* Mark device as disabled. */
306 dev->enabled = 0;
307 /* Mark device to be hidden. */
308 rpc.new_rpfn |= RPFN_HIDE(PCI_FUNC(dev->path.pci.devfn));
309}
310
311static void root_port_check_disable(device_t dev)
312{
313 int rp;
314
315 /* Device already disabled. */
316 if (!dev->enabled) {
317 root_port_mark_disable(dev);
318 return;
319 }
320
321 rp = root_port_number(dev);
322
323 /* Is the GbE port mapped to this Root Port? */
324 if (rp == rpc.gbe_port) {
325 root_port_mark_disable(dev);
326 return;
327 }
328
329 /* Check Root Port Configuration. */
330 switch (rp) {
331 case 2:
332 /* Root Port 2 is disabled for all lane configurations
333 * but config 00b (4x1 links). */
334 if ((rpc.strpfusecfg1 >> 14) & 0x3) {
335 root_port_mark_disable(dev);
336 return;
337 }
338 break;
339 case 3:
340 /* Root Port 3 is disabled in config 11b (1x4 links). */
341 if (((rpc.strpfusecfg1 >> 14) & 0x3) == 0x3) {
342 root_port_mark_disable(dev);
343 return;
344 }
345 break;
346 case 4:
347 /* Root Port 4 is disabled in configs 11b (1x4 links)
348 * and 10b (2x2 links). */
349 if ((rpc.strpfusecfg1 >> 14) & 0x2) {
350 root_port_mark_disable(dev);
351 return;
352 }
353 break;
354 }
355
356 /* Check Pin Ownership. */
357 switch (rp) {
358 case 1:
359 /* Bit 0 is Root Port 1 ownership. */
360 if ((rpc.pin_ownership & 0x1) == 0) {
361 root_port_mark_disable(dev);
362 return;
363 }
364 break;
365 case 2:
366 /* Bit 2 is Root Port 2 ownership. */
367 if ((rpc.pin_ownership & 0x4) == 0) {
368 root_port_mark_disable(dev);
369 return;
370 }
371 break;
372 case 6:
373 /* Bits 7:4 are Root Port 6 pin-lane ownership. */
374 if ((rpc.pin_ownership & 0xf0) == 0) {
375 root_port_mark_disable(dev);
376 return;
377 }
378 break;
379 }
380}
381
382static void pcie_update_cfg8(device_t dev, int reg, u8 mask, u8 or)
383{
384 u8 reg8;
385
386 reg8 = pci_read_config8(dev, reg);
387 reg8 &= mask;
388 reg8 |= or;
389 pci_write_config8(dev, reg, reg8);
390}
391
392static void pcie_update_cfg(device_t dev, int reg, u32 mask, u32 or)
393{
394 u32 reg32;
395
396 reg32 = pci_read_config32(dev, reg);
397 reg32 &= mask;
398 reg32 |= or;
399 pci_write_config32(dev, reg, reg32);
400}
401
402static void pcie_add_0x0202000_iobp(u32 reg)
403{
404 u32 reg32;
405
406 reg32 = pch_iobp_read(reg);
407 reg32 += (0x2 << 16) | (0x2 << 8);
408 pch_iobp_write(reg, reg32);
409}
410
411static void pch_pcie_early(struct device *dev)
412{
413 config_t *config = dev->chip_info;
414 int do_aspm = 0;
415 int rp = root_port_number(dev);
416
417 switch (rp) {
418 case 1:
419 case 2:
420 case 3:
421 case 4:
422 /*
Martin Rothde7ed6f2014-12-07 14:58:18 -0700423 * Bits 31:28 of b0d28f0 0x32c register correspond to
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700424 * Root Ports 4:1.
425 */
426 do_aspm = !!(rpc.b0d28f0_32c & (1 << (28 + rp - 1)));
427 break;
428 case 5:
429 /*
Martin Rothde7ed6f2014-12-07 14:58:18 -0700430 * Bit 28 of b0d28f4 0x32c register correspond to
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700431 * Root Ports 4:1.
432 */
433 do_aspm = !!(rpc.b0d28f4_32c & (1 << 28));
434 break;
435 case 6:
436 /*
Martin Rothde7ed6f2014-12-07 14:58:18 -0700437 * Bit 28 of b0d28f5 0x32c register correspond to
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700438 * Root Ports 4:1.
439 */
440 do_aspm = !!(rpc.b0d28f5_32c & (1 << 28));
441 break;
442 }
443
444 /* Allow ASPM to be forced on in devicetree */
445 if (config && (config->pcie_port_force_aspm & (1 << (rp - 1))))
446 do_aspm = 1;
447
448 printk(BIOS_DEBUG, "PCIe Root Port %d ASPM is %sabled\n",
449 rp, do_aspm ? "en" : "dis");
450
451 if (do_aspm) {
452 /* Set ASPM bits in MPC2 register. */
453 pcie_update_cfg(dev, 0xd4, ~(0x3 << 2), (1 << 4) | (0x2 << 2));
454
455 /* Set unique clock exit latency in MPC register. */
456 pcie_update_cfg(dev, 0xd8, ~(0x7 << 18), (0x7 << 18));
457
458 /* Set L1 exit latency in LCAP register. */
459 pcie_update_cfg(dev, 0x4c, ~(0x7 << 15), (0x4 << 15));
460
461 switch (rp) {
462 case 1:
463 pcie_add_0x0202000_iobp(0xe9002440);
464 break;
465 case 2:
466 pcie_add_0x0202000_iobp(0xe9002640);
467 break;
468 case 3:
469 pcie_add_0x0202000_iobp(0xe9000840);
470 break;
471 case 4:
472 pcie_add_0x0202000_iobp(0xe9000a40);
473 break;
474 case 5:
475 pcie_add_0x0202000_iobp(0xe9000c40);
476 pcie_add_0x0202000_iobp(0xe9000e40);
477 pcie_add_0x0202000_iobp(0xe9001040);
478 pcie_add_0x0202000_iobp(0xe9001240);
479 break;
480 case 6:
481 /* Update IOBP based on lane ownership. */
482 if (rpc.pin_ownership & (1 << 4))
483 pcie_add_0x0202000_iobp(0xea002040);
484 if (rpc.pin_ownership & (1 << 5))
485 pcie_add_0x0202000_iobp(0xea002240);
486 if (rpc.pin_ownership & (1 << 6))
487 pcie_add_0x0202000_iobp(0xea002440);
488 if (rpc.pin_ownership & (1 << 7))
489 pcie_add_0x0202000_iobp(0xea002640);
490 break;
491 }
492
493 pcie_update_cfg(dev, 0x338, ~(1 << 26), 0);
494 }
495
496 /* Enable LTR in Root Port. */
497 pcie_update_cfg(dev, 0x64, ~(1 << 11), (1 << 11));
498 pcie_update_cfg(dev, 0x68, ~(1 << 10), (1 << 10));
499
500 pcie_update_cfg(dev, 0x318, ~(0xffff << 16), (0x1414 << 16));
501
502 /* Set L1 exit latency in LCAP register. */
503 if (!do_aspm && (pci_read_config8(dev, 0xf5) & 0x1))
504 pcie_update_cfg(dev, 0x4c, ~(0x7 << 15), (0x4 << 15));
505 else
506 pcie_update_cfg(dev, 0x4c, ~(0x7 << 15), (0x2 << 15));
507
508 pcie_update_cfg(dev, 0x314, 0x0, 0x743a361b);
509
510 /* Set Common Clock Exit Latency in MPC register. */
511 pcie_update_cfg(dev, 0xd8, ~(0x7 << 15), (0x3 << 15));
512
513 pcie_update_cfg(dev, 0x33c, ~0x00ffffff, 0x854c74);
514
Martin Rothde7ed6f2014-12-07 14:58:18 -0700515 /* Set Invalid Receive Range Check Enable in MPC register. */
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700516 pcie_update_cfg(dev, 0xd8, ~0, (1 << 25));
517
518 pcie_update_cfg8(dev, 0xf5, 0x3f, 0);
519
520 if (rp == 1 || rp == 5 || rp == 6)
521 pcie_update_cfg8(dev, 0xf7, ~0xc, 0);
522
523 /* Set EOI forwarding disable. */
524 pcie_update_cfg(dev, 0xd4, ~0, (1 << 1));
525
526 /* Set something involving advanced error reporting. */
527 pcie_update_cfg(dev, 0x100, ~((1 << 20) - 1), 0x10001);
528 pcie_update_cfg(dev, 0x100, ~0, (1 << 29));
529
530 /* Read and write back write-once capability registers. */
531 pcie_update_cfg(dev, 0x34, ~0, 0);
532 pcie_update_cfg(dev, 0x40, ~0, 0);
533 pcie_update_cfg(dev, 0x80, ~0, 0);
534 pcie_update_cfg(dev, 0x90, ~0, 0);
535}
536
537static void pch_pcie_init(struct device *dev)
538{
539 u16 reg16;
540 u32 reg32;
541
542 printk(BIOS_DEBUG, "Initializing PCH PCIe bridge.\n");
543
544 /* Enable SERR */
545 reg32 = pci_read_config32(dev, PCI_COMMAND);
546 reg32 |= PCI_COMMAND_SERR;
547 pci_write_config32(dev, PCI_COMMAND, reg32);
548
549 /* Enable Bus Master */
550 reg32 = pci_read_config32(dev, PCI_COMMAND);
551 reg32 |= PCI_COMMAND_MASTER;
552 pci_write_config32(dev, PCI_COMMAND, reg32);
553
554 /* Set Cache Line Size to 0x10 */
555 pci_write_config8(dev, 0x0c, 0x10);
556
557 reg16 = pci_read_config16(dev, 0x3e);
558 reg16 &= ~(1 << 0); /* disable parity error response */
559 reg16 |= (1 << 2); /* ISA enable */
560 pci_write_config16(dev, 0x3e, reg16);
561
562#ifdef EVEN_MORE_DEBUG
563 reg32 = pci_read_config32(dev, 0x20);
564 printk(BIOS_SPEW, " MBL = 0x%08x\n", reg32);
565 reg32 = pci_read_config32(dev, 0x24);
566 printk(BIOS_SPEW, " PMBL = 0x%08x\n", reg32);
567 reg32 = pci_read_config32(dev, 0x28);
568 printk(BIOS_SPEW, " PMBU32 = 0x%08x\n", reg32);
569 reg32 = pci_read_config32(dev, 0x2c);
570 printk(BIOS_SPEW, " PMLU32 = 0x%08x\n", reg32);
571#endif
572
573 /* Clear errors in status registers */
574 reg16 = pci_read_config16(dev, 0x06);
575 pci_write_config16(dev, 0x06, reg16);
576 reg16 = pci_read_config16(dev, 0x1e);
577 pci_write_config16(dev, 0x1e, reg16);
578}
579
580static void pch_pcie_enable(device_t dev)
581{
582 /* Add this device to the root port config structure. */
583 root_port_init_config(dev);
584
585 /* Check to see if this Root Port should be disabled. */
586 root_port_check_disable(dev);
587
588 /* Power Management init before enumeration */
589 if (dev->enabled)
590 pch_pcie_early(dev);
591
592 /*
593 * When processing the last PCIe root port we can now
594 * update the Root Port Function Number and Hide register.
595 */
596 if (root_port_is_last(dev))
597 root_port_commit_config();
598}
599
600static void pcie_set_subsystem(device_t dev, unsigned vendor, unsigned device)
601{
602 /* NOTE: This is not the default position! */
603 if (!vendor || !device)
604 pci_write_config32(dev, 0x94, pci_read_config32(dev, 0));
605 else
606 pci_write_config32(dev, 0x94, (device << 16) | vendor);
607}
608
609static struct pci_operations pcie_ops = {
610 .set_subsystem = pcie_set_subsystem,
611};
612
613static struct device_operations device_ops = {
614 .read_resources = pci_bus_read_resources,
615 .set_resources = pci_dev_set_resources,
616 .enable_resources = pci_bus_enable_resources,
617 .init = pch_pcie_init,
618 .enable = pch_pcie_enable,
619 .scan_bus = pciexp_scan_bridge,
620 .ops_pci = &pcie_ops,
621};
622
623static const unsigned short pcie_device_ids[] = {
624 /* Lynxpoint-LP */
625 0x9c10, 0x9c12, 0x9c14, 0x9c16, 0x9c18, 0x9c1a,
626 /* WildcatPoint */
627 0x9c90, 0x9c92, 0x9c94, 0x9c96, 0x9c98, 0x9c9a, 0x2448,
628 0
629};
630
631static const struct pci_driver pch_pcie __pci_driver = {
632 .ops = &device_ops,
633 .vendor = PCI_VENDOR_ID_INTEL,
634 .devices = pcie_device_ids,
635};