blob: 28f4b6ad155a70ec3a97ac926fdb8e57eddba1da [file] [log] [blame]
Aaron Durbin76c37002012-10-30 09:03:43 -05001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2008-2009 coresystems GmbH
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; version 2 of
9 * 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_ids.h>
26#include "pch.h"
27
Aaron Durbin60f82082013-06-19 13:28:04 -050028/* Check if any port in set X to X+3 is enabled */
29static int pch_pcie_check_set_enabled(device_t dev)
30{
31 device_t port;
32 int port_func;
33 int dev_func = PCI_FUNC(dev->path.pci.devfn);
34
35 printk(BIOS_DEBUG, "%s: check set enabled\n", dev_path(dev));
36
37 /* Go through static device tree list of devices
38 * because enumeration is still in progress */
39 for (port = all_devices; port; port = port->next) {
40 /* Only care about PCIe root ports */
41 if (PCI_SLOT(port->path.pci.devfn) !=
42 PCI_SLOT(dev->path.pci.devfn))
43 continue;
44
45 /* Check if port is in range and enabled */
46 port_func = PCI_FUNC(port->path.pci.devfn);
47 if (port_func >= dev_func &&
48 port_func < (dev_func + 4) &&
49 port->enabled)
50 return 1;
51 }
52
53 /* None of the ports in this set are enabled */
54 return 0;
55}
56
57/* RPFN is a write-once register so keep a copy until it is written */
58static u32 new_rpfn;
59
60/* Swap function numbers assigned to two PCIe Root Ports */
61static void pch_pcie_function_swap(u8 old_fn, u8 new_fn)
62{
63 u32 old_rpfn = new_rpfn;
64
65 printk(BIOS_DEBUG, "PCH: Remap PCIe function %d to %d\n",
66 old_fn, new_fn);
67
68 new_rpfn &= ~(RPFN_FNMASK(old_fn) | RPFN_FNMASK(new_fn));
69
70 /* Old function set to new function and disabled */
71 new_rpfn |= RPFN_FNSET(old_fn, RPFN_FNGET(old_rpfn, new_fn));
72 new_rpfn |= RPFN_FNSET(new_fn, RPFN_FNGET(old_rpfn, old_fn));
73}
74
75/* Update devicetree with new Root Port function number assignment */
76static void pch_pcie_devicetree_update(void)
77{
78 device_t dev;
79
80 /* Update the function numbers in the static devicetree */
81 for (dev = all_devices; dev; dev = dev->next) {
82 u8 new_devfn;
83
84 /* Only care about PCH PCIe root ports */
85 if (PCI_SLOT(dev->path.pci.devfn) !=
86 PCH_PCIE_DEV_SLOT)
87 continue;
88
89 /* Determine the new devfn for this port */
90 new_devfn = PCI_DEVFN(PCH_PCIE_DEV_SLOT,
91 RPFN_FNGET(new_rpfn,
92 PCI_FUNC(dev->path.pci.devfn)));
93
94 if (dev->path.pci.devfn != new_devfn) {
95 printk(BIOS_DEBUG,
96 "PCH: PCIe map %02x.%1x -> %02x.%1x\n",
97 PCI_SLOT(dev->path.pci.devfn),
98 PCI_FUNC(dev->path.pci.devfn),
99 PCI_SLOT(new_devfn), PCI_FUNC(new_devfn));
100
101 dev->path.pci.devfn = new_devfn;
102 }
103 }
104}
105
106/* Special handling for PCIe Root Port devices */
107void pch_pcie_enable_dev(device_t dev)
108{
109 struct southbridge_intel_lynxpoint_config *config = dev->chip_info;
110 u32 reg32;
111
112 /*
113 * Save a copy of the Root Port Function Number map when
114 * starting to walk the list of PCIe Root Ports so it can
115 * be updated locally and written out when the last port
116 * has been processed.
117 */
118 if (PCI_FUNC(dev->path.pci.devfn) == 0) {
119 new_rpfn = RCBA32(RPFN);
120
121 /*
122 * Enable Root Port coalescing if the first port is disabled
123 * or the other devices will not be enumerated by the OS.
124 */
125 if (!dev->enabled)
126 config->pcie_port_coalesce = 1;
127
128 if (config->pcie_port_coalesce)
129 printk(BIOS_INFO,
130 "PCH: PCIe Root Port coalescing is enabled\n");
131 }
132
133 if (!dev->enabled) {
134 printk(BIOS_DEBUG, "%s: Disabling device\n", dev_path(dev));
135
136 /*
137 * PCIE Power Savings for PantherPoint and CougarPoint/B1+
138 *
139 * If PCIe 0-3 disabled set Function 0 0xE2[0] = 1
140 * If PCIe 4-7 disabled set Function 4 0xE2[0] = 1
141 *
142 * This check is done here instead of pcie driver
143 * because the pcie driver enable() handler is not
144 * called unless the device is enabled.
145 */
146 if ((PCI_FUNC(dev->path.pci.devfn) == 0 ||
147 PCI_FUNC(dev->path.pci.devfn) == 4)) {
148 /* Handle workaround for PPT and CPT/B1+ */
149 if (!pch_pcie_check_set_enabled(dev)) {
150 u8 reg8 = pci_read_config8(dev, 0xe2);
151 reg8 |= 1;
152 pci_write_config8(dev, 0xe2, reg8);
153 }
154
155 /*
156 * Enable Clock Gating for shared PCIe resources
157 * before disabling this particular port.
158 */
159 pci_write_config8(dev, 0xe1, 0x3c);
160 }
161
162 /* Ensure memory, io, and bus master are all disabled */
163 reg32 = pci_read_config32(dev, PCI_COMMAND);
164 reg32 &= ~(PCI_COMMAND_MASTER |
165 PCI_COMMAND_MEMORY | PCI_COMMAND_IO);
166 pci_write_config32(dev, PCI_COMMAND, reg32);
167
168 /* Do not claim downstream transactions for PCIe ports */
169 new_rpfn |= RPFN_HIDE(PCI_FUNC(dev->path.pci.devfn));
170
171 /* Disable this device if possible */
172 pch_disable_devfn(dev);
173 } else {
174 int fn;
175
176 /*
177 * Check if there is a lower disabled port to swap with this
178 * port in order to maintain linear order starting at zero.
179 */
180 if (config->pcie_port_coalesce) {
181 for (fn=0; fn < PCI_FUNC(dev->path.pci.devfn); fn++) {
182 if (!(new_rpfn & RPFN_HIDE(fn)))
183 continue;
184
185 /* Swap places with this function */
186 pch_pcie_function_swap(
187 PCI_FUNC(dev->path.pci.devfn), fn);
188 break;
189 }
190 }
191
192 /* Enable SERR */
193 reg32 = pci_read_config32(dev, PCI_COMMAND);
194 reg32 |= PCI_COMMAND_SERR;
195 pci_write_config32(dev, PCI_COMMAND, reg32);
196 }
197
198 /*
199 * When processing the last PCIe root port we can now
200 * update the Root Port Function Number and Hide register.
201 */
202 if (PCI_FUNC(dev->path.pci.devfn) == 7) {
203 printk(BIOS_SPEW, "PCH: RPFN 0x%08x -> 0x%08x\n",
204 RCBA32(RPFN), new_rpfn);
205 RCBA32(RPFN) = new_rpfn;
206
207 /* Update static devictree with new function numbers */
208 if (config->pcie_port_coalesce)
209 pch_pcie_devicetree_update();
210 }
211}
212
Aaron Durbin76c37002012-10-30 09:03:43 -0500213static void pch_pcie_pm_early(struct device *dev)
214{
215/* RPC has been moved. It is in PCI config space now. */
216#if 0
217 u16 link_width_p0, link_width_p4;
218 u8 slot_power_limit = 10; /* 10W for x1 */
219 u32 reg32;
220 u8 reg8;
221
222 reg32 = RCBA32(RPC);
223
224 /* Port 0-3 link aggregation from PCIEPCS1[1:0] soft strap */
225 switch (reg32 & 3) {
226 case 3:
227 link_width_p0 = 4;
228 break;
229 case 1:
230 case 2:
231 link_width_p0 = 2;
232 break;
233 case 0:
234 default:
235 link_width_p0 = 1;
236 }
237
238 /* Port 4-7 link aggregation from PCIEPCS2[1:0] soft strap */
239 switch ((reg32 >> 2) & 3) {
240 case 3:
241 link_width_p4 = 4;
242 break;
243 case 1:
244 case 2:
245 link_width_p4 = 2;
246 break;
247 case 0:
248 default:
249 link_width_p4 = 1;
250 }
251
252 /* Enable dynamic clock gating where needed */
253 reg8 = pci_read_config8(dev, 0xe1);
254 switch (PCI_FUNC(dev->path.pci.devfn)) {
255 case 0: /* Port 0 */
256 if (link_width_p0 == 4)
257 slot_power_limit = 40; /* 40W for x4 */
258 else if (link_width_p0 == 2)
259 slot_power_limit = 20; /* 20W for x2 */
260 reg8 |= 0x3f;
261 break;
262 case 4: /* Port 4 */
263 if (link_width_p4 == 4)
264 slot_power_limit = 40; /* 40W for x4 */
265 else if (link_width_p4 == 2)
266 slot_power_limit = 20; /* 20W for x2 */
267 reg8 |= 0x3f;
268 break;
269 case 1: /* Port 1 only if Port 0 is x1 */
270 if (link_width_p0 == 1)
271 reg8 |= 0x3;
272 break;
273 case 2: /* Port 2 only if Port 0 is x1 or x2 */
274 case 3: /* Port 3 only if Port 0 is x1 or x2 */
275 if (link_width_p0 <= 2)
276 reg8 |= 0x3;
277 break;
278 case 5: /* Port 5 only if Port 4 is x1 */
279 if (link_width_p4 == 1)
280 reg8 |= 0x3;
281 break;
282 case 6: /* Port 7 only if Port 4 is x1 or x2 */
283 case 7: /* Port 7 only if Port 4 is x1 or x2 */
284 if (link_width_p4 <= 2)
285 reg8 |= 0x3;
286 break;
287 }
288 pci_write_config8(dev, 0xe1, reg8);
289
290 /* Set 0xE8[0] = 1 */
291 reg32 = pci_read_config32(dev, 0xe8);
292 reg32 |= 1;
293 pci_write_config32(dev, 0xe8, reg32);
294
295 /* Adjust Common Clock exit latency */
296 reg32 = pci_read_config32(dev, 0xd8);
297 reg32 &= ~(1 << 17);
298 reg32 |= (1 << 16) | (1 << 15);
299 reg32 &= ~(1 << 31); /* Disable PME# SCI for native PME handling */
300 pci_write_config32(dev, 0xd8, reg32);
301
302 /* Adjust ASPM L1 exit latency */
303 reg32 = pci_read_config32(dev, 0x4c);
304 reg32 &= ~((1 << 17) | (1 << 16) | (1 << 15));
305 if (RCBA32(0x2320) & (1 << 16)) {
306 /* If RCBA+2320[15]=1 set ASPM L1 to 8-16us */
307 reg32 |= (1 << 17);
308 } else {
309 /* Else set ASPM L1 to 2-4us */
310 reg32 |= (1 << 16);
311 }
312 pci_write_config32(dev, 0x4c, reg32);
313
314 /* Set slot power limit as configured above */
315 reg32 = pci_read_config32(dev, 0x54);
316 reg32 &= ~((1 << 15) | (1 << 16)); /* 16:15 = Slot power scale */
317 reg32 &= ~(0xff << 7); /* 14:7 = Slot power limit */
318 reg32 |= (slot_power_limit << 7);
319 pci_write_config32(dev, 0x54, reg32);
320#endif
321}
322
323static void pch_pcie_pm_late(struct device *dev)
324{
325 enum aspm_type apmc;
326 u32 reg32;
327
328 /* Set 0x314 = 0x743a361b */
Kyösti Mälkki386b3e62013-07-26 08:52:49 +0300329 pci_write_config32(dev, 0x314, 0x743a361b);
Aaron Durbin76c37002012-10-30 09:03:43 -0500330
331 /* Set 0x318[31:16] = 0x1414 */
Kyösti Mälkki386b3e62013-07-26 08:52:49 +0300332 reg32 = pci_read_config32(dev, 0x318);
Aaron Durbin76c37002012-10-30 09:03:43 -0500333 reg32 &= 0x0000ffff;
334 reg32 |= 0x14140000;
Kyösti Mälkki386b3e62013-07-26 08:52:49 +0300335 pci_write_config32(dev, 0x318, reg32);
Aaron Durbin76c37002012-10-30 09:03:43 -0500336
337 /* Set 0x324[5] = 1 */
Kyösti Mälkki386b3e62013-07-26 08:52:49 +0300338 reg32 = pci_read_config32(dev, 0x324);
Aaron Durbin76c37002012-10-30 09:03:43 -0500339 reg32 |= (1 << 5);
Kyösti Mälkki386b3e62013-07-26 08:52:49 +0300340 pci_write_config32(dev, 0x324, reg32);
Aaron Durbin76c37002012-10-30 09:03:43 -0500341
342 /* Set 0x330[7:0] = 0x40 */
Kyösti Mälkki386b3e62013-07-26 08:52:49 +0300343 reg32 = pci_read_config32(dev, 0x330);
Aaron Durbin76c37002012-10-30 09:03:43 -0500344 reg32 &= ~(0xff);
345 reg32 |= 0x40;
Kyösti Mälkki386b3e62013-07-26 08:52:49 +0300346 pci_write_config32(dev, 0x330, reg32);
Aaron Durbin76c37002012-10-30 09:03:43 -0500347
348 /* Set 0x33C[24:0] = 0x854c74 */
Kyösti Mälkki386b3e62013-07-26 08:52:49 +0300349 reg32 = pci_read_config32(dev, 0x33c);
Aaron Durbin76c37002012-10-30 09:03:43 -0500350 reg32 &= 0xff000000;
351 reg32 |= 0x00854c74;
Kyösti Mälkki386b3e62013-07-26 08:52:49 +0300352 pci_write_config32(dev, 0x33c, reg32);
Aaron Durbin76c37002012-10-30 09:03:43 -0500353
354 /* No IO-APIC, Disable EOI forwarding */
355 reg32 = pci_read_config32(dev, 0xd4);
356 reg32 |= (1 << 1);
357 pci_write_config32(dev, 0xd4, reg32);
358
359 /* Get configured ASPM state */
360 apmc = pci_read_config32(dev, 0x50) & 3;
361
362 /* If both L0s and L1 enabled then set root port 0xE8[1]=1 */
363 if (apmc == PCIE_ASPM_BOTH) {
364 reg32 = pci_read_config32(dev, 0xe8);
365 reg32 |= (1 << 1);
366 pci_write_config32(dev, 0xe8, reg32);
367 }
368}
369
370static void pci_init(struct device *dev)
371{
372 u16 reg16;
373 u32 reg32;
374
375 printk(BIOS_DEBUG, "Initializing PCH PCIe bridge.\n");
376
377 /* Enable Bus Master */
378 reg32 = pci_read_config32(dev, PCI_COMMAND);
379 reg32 |= PCI_COMMAND_MASTER;
380 pci_write_config32(dev, PCI_COMMAND, reg32);
381
382 /* Set Cache Line Size to 0x10 */
383 // This has no effect but the OS might expect it
384 pci_write_config8(dev, 0x0c, 0x10);
385
386 reg16 = pci_read_config16(dev, 0x3e);
387 reg16 &= ~(1 << 0); /* disable parity error response */
388 // reg16 &= ~(1 << 1); /* disable SERR */
389 reg16 |= (1 << 2); /* ISA enable */
390 pci_write_config16(dev, 0x3e, reg16);
391
392#ifdef EVEN_MORE_DEBUG
393 reg32 = pci_read_config32(dev, 0x20);
394 printk(BIOS_SPEW, " MBL = 0x%08x\n", reg32);
395 reg32 = pci_read_config32(dev, 0x24);
396 printk(BIOS_SPEW, " PMBL = 0x%08x\n", reg32);
397 reg32 = pci_read_config32(dev, 0x28);
398 printk(BIOS_SPEW, " PMBU32 = 0x%08x\n", reg32);
399 reg32 = pci_read_config32(dev, 0x2c);
400 printk(BIOS_SPEW, " PMLU32 = 0x%08x\n", reg32);
401#endif
402
403 /* Clear errors in status registers */
404 reg16 = pci_read_config16(dev, 0x06);
405 //reg16 |= 0xf900;
406 pci_write_config16(dev, 0x06, reg16);
407
408 reg16 = pci_read_config16(dev, 0x1e);
409 //reg16 |= 0xf900;
410 pci_write_config16(dev, 0x1e, reg16);
411
412 /* Power Management init after enumeration */
413 pch_pcie_pm_late(dev);
414}
415
416static void pch_pcie_enable(device_t dev)
417{
418 /* Power Management init before enumeration */
419 pch_pcie_pm_early(dev);
420}
421
422static void pcie_set_subsystem(device_t dev, unsigned vendor, unsigned device)
423{
424 /* NOTE: This is not the default position! */
425 if (!vendor || !device) {
426 pci_write_config32(dev, 0x94,
427 pci_read_config32(dev, 0));
428 } else {
429 pci_write_config32(dev, 0x94,
430 ((device & 0xffff) << 16) | (vendor & 0xffff));
431 }
432}
433
434static struct pci_operations pci_ops = {
435 .set_subsystem = pcie_set_subsystem,
436};
437
438static struct device_operations device_ops = {
439 .read_resources = pci_bus_read_resources,
440 .set_resources = pci_dev_set_resources,
441 .enable_resources = pci_bus_enable_resources,
442 .init = pci_init,
443 .enable = pch_pcie_enable,
444 .scan_bus = pciexp_scan_bridge,
445 .ops_pci = &pci_ops,
446};
447
Duncan Laurie74c0d052012-12-17 11:31:40 -0800448static const unsigned short pci_device_ids[] = {
449 /* Lynxpoint Mobile */
450 0x8c10, 0x8c12, 0x8c14, 0x8c16, 0x8c18, 0x8c1a, 0x8c1c, 0x8c1e,
451 /* Lynxpoint Low Power */
452 0x9c10, 0x9c12, 0x9c14, 0x9c16, 0x9c18, 0x9c1a,
453 0
454};
Aaron Durbin76c37002012-10-30 09:03:43 -0500455
456static const struct pci_driver pch_pcie __pci_driver = {
457 .ops = &device_ops,
458 .vendor = PCI_VENDOR_ID_INTEL,
459 .devices = pci_device_ids,
460};