blob: ae8ed9b15cb1fc7d35472c9301b6002dd2d1a9b1 [file] [log] [blame]
Angel Pons182dbde2020-04-02 23:49:05 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Stefan Reinauer8e073822012-04-04 00:07:22 +02002
3#include <console/console.h>
4#include <delay.h>
5#include <device/device.h>
6#include <device/pci.h>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +02007#include <device/pci_ops.h>
Bill XIE8c57d092017-08-25 22:07:12 +08008#include <string.h>
Stefan Reinauer8e073822012-04-04 00:07:22 +02009
Kyösti Mälkki12b121c2019-08-18 16:33:39 +030010#include "chip.h"
11#include "pch.h"
12
Stefan Reinauer8e073822012-04-04 00:07:22 +020013int pch_silicon_revision(void)
14{
Felix Held82bd0c32016-08-13 23:27:15 +020015 static int pch_revision_id = -1;
Marc Jones783f2262013-02-11 14:36:35 -070016
Antonello Dettoridac82402016-09-02 09:14:39 +020017#ifdef __SIMPLE_DEVICE__
Kyösti Mälkkic70eed12018-05-22 02:18:00 +030018 pci_devfn_t dev = PCI_DEV(0, 0x1f, 0);
Marc Jones783f2262013-02-11 14:36:35 -070019#else
Kyösti Mälkkic70eed12018-05-22 02:18:00 +030020 struct device *dev = pcidev_on_root(0x1f, 0);
Marc Jones783f2262013-02-11 14:36:35 -070021#endif
22
Stefan Reinauer8e073822012-04-04 00:07:22 +020023 if (pch_revision_id < 0)
Marc Jones783f2262013-02-11 14:36:35 -070024 pch_revision_id = pci_read_config8(dev, PCI_REVISION_ID);
Stefan Reinauer8e073822012-04-04 00:07:22 +020025 return pch_revision_id;
26}
27
Duncan Laurieb9fe01c2012-04-27 10:30:51 -070028int pch_silicon_type(void)
29{
Felix Held82bd0c32016-08-13 23:27:15 +020030 static int pch_type = -1;
Marc Jones783f2262013-02-11 14:36:35 -070031
Antonello Dettoridac82402016-09-02 09:14:39 +020032#ifdef __SIMPLE_DEVICE__
Kyösti Mälkkic70eed12018-05-22 02:18:00 +030033 pci_devfn_t dev = PCI_DEV(0, 0x1f, 0);
Marc Jones783f2262013-02-11 14:36:35 -070034#else
Kyösti Mälkkic70eed12018-05-22 02:18:00 +030035 struct device *dev = pcidev_on_root(0x1f, 0);
Marc Jones783f2262013-02-11 14:36:35 -070036#endif
37
Duncan Laurieb9fe01c2012-04-27 10:30:51 -070038 if (pch_type < 0)
Marc Jones783f2262013-02-11 14:36:35 -070039 pch_type = pci_read_config8(dev, PCI_DEVICE_ID + 1);
Duncan Laurieb9fe01c2012-04-27 10:30:51 -070040 return pch_type;
41}
42
Angel Ponsd703c5b2020-08-10 15:25:26 +020043static int pch_silicon_supported(int type, int rev)
Duncan Laurieb9fe01c2012-04-27 10:30:51 -070044{
45 int cur_type = pch_silicon_type();
46 int cur_rev = pch_silicon_revision();
47
48 switch (type) {
49 case PCH_TYPE_CPT:
50 /* CougarPoint minimum revision */
51 if (cur_type == PCH_TYPE_CPT && cur_rev >= rev)
52 return 1;
53 /* PantherPoint any revision */
54 if (cur_type == PCH_TYPE_PPT)
55 return 1;
56 break;
57
58 case PCH_TYPE_PPT:
59 /* PantherPoint minimum revision */
60 if (cur_type == PCH_TYPE_PPT && cur_rev >= rev)
61 return 1;
62 break;
63 }
64
65 return 0;
66}
67
Stefan Reinauer8e073822012-04-04 00:07:22 +020068#define IOBP_RETRY 1000
69static inline int iobp_poll(void)
70{
Martin Rothff744bf2019-10-23 21:46:03 -060071 unsigned int try = IOBP_RETRY;
Stefan Reinauer8e073822012-04-04 00:07:22 +020072 u32 data;
73
74 while (try--) {
75 data = RCBA32(IOBPS);
76 if ((data & 1) == 0)
77 return 1;
78 udelay(10);
79 }
80
81 printk(BIOS_ERR, "IOBP timeout\n");
82 return 0;
83}
84
85void pch_iobp_update(u32 address, u32 andvalue, u32 orvalue)
86{
87 u32 data;
88
89 /* Set the address */
90 RCBA32(IOBPIRI) = address;
91
92 /* READ OPCODE */
Duncan Laurieb9fe01c2012-04-27 10:30:51 -070093 if (pch_silicon_supported(PCH_TYPE_CPT, PCH_STEP_B0))
Stefan Reinauer8e073822012-04-04 00:07:22 +020094 RCBA32(IOBPS) = IOBPS_RW_BX;
95 else
96 RCBA32(IOBPS) = IOBPS_READ_AX;
97 if (!iobp_poll())
98 return;
99
100 /* Read IOBP data */
101 data = RCBA32(IOBPD);
102 if (!iobp_poll())
103 return;
104
105 /* Check for successful transaction */
106 if ((RCBA32(IOBPS) & 0x6) != 0) {
107 printk(BIOS_ERR, "IOBP read 0x%08x failed\n", address);
108 return;
109 }
110
111 /* Update the data */
112 data &= andvalue;
113 data |= orvalue;
114
115 /* WRITE OPCODE */
Duncan Laurieb9fe01c2012-04-27 10:30:51 -0700116 if (pch_silicon_supported(PCH_TYPE_CPT, PCH_STEP_B0))
Stefan Reinauer8e073822012-04-04 00:07:22 +0200117 RCBA32(IOBPS) = IOBPS_RW_BX;
118 else
119 RCBA32(IOBPS) = IOBPS_WRITE_AX;
120 if (!iobp_poll())
121 return;
122
123 /* Write IOBP data */
124 RCBA32(IOBPD) = data;
125 if (!iobp_poll())
126 return;
127}
128
Kyösti Mälkki21d6a272019-11-05 18:50:38 +0200129#ifndef __SIMPLE_DEVICE__
Frans Hendrikse6bf51f2019-05-01 10:48:31 +0200130/* Set bit in function disable register to hide this device */
Martin Rothff744bf2019-10-23 21:46:03 -0600131static void pch_hide_devfn(unsigned int devfn)
Marc Jones783f2262013-02-11 14:36:35 -0700132{
133 switch (devfn) {
Patrick Rudolph403f4332019-07-14 17:43:52 +0200134 case PCI_DEVFN(20, 0): /* xHCI */
135 if (pch_silicon_type() == PCH_TYPE_PPT) {
136 /* on CPT this bit is reserved */
137 RCBA32_OR(FD, PCH_DISABLE_XHCI);
138 }
139 break;
Marc Jones783f2262013-02-11 14:36:35 -0700140 case PCI_DEVFN(22, 0): /* MEI #1 */
141 RCBA32_OR(FD2, PCH_DISABLE_MEI1);
142 break;
143 case PCI_DEVFN(22, 1): /* MEI #2 */
144 RCBA32_OR(FD2, PCH_DISABLE_MEI2);
145 break;
146 case PCI_DEVFN(22, 2): /* IDE-R */
147 RCBA32_OR(FD2, PCH_DISABLE_IDER);
148 break;
149 case PCI_DEVFN(22, 3): /* KT */
150 RCBA32_OR(FD2, PCH_DISABLE_KT);
151 break;
152 case PCI_DEVFN(25, 0): /* Gigabit Ethernet */
Nico Huber6760e0b2019-11-17 02:34:53 +0100153 /* BUC is already handled in `early_pch.c`. */
Marc Jones783f2262013-02-11 14:36:35 -0700154 break;
155 case PCI_DEVFN(26, 0): /* EHCI #2 */
156 RCBA32_OR(FD, PCH_DISABLE_EHCI2);
157 break;
158 case PCI_DEVFN(27, 0): /* HD Audio Controller */
159 RCBA32_OR(FD, PCH_DISABLE_HD_AUDIO);
160 break;
161 case PCI_DEVFN(28, 0): /* PCI Express Root Port 1 */
162 case PCI_DEVFN(28, 1): /* PCI Express Root Port 2 */
163 case PCI_DEVFN(28, 2): /* PCI Express Root Port 3 */
164 case PCI_DEVFN(28, 3): /* PCI Express Root Port 4 */
165 case PCI_DEVFN(28, 4): /* PCI Express Root Port 5 */
166 case PCI_DEVFN(28, 5): /* PCI Express Root Port 6 */
167 case PCI_DEVFN(28, 6): /* PCI Express Root Port 7 */
168 case PCI_DEVFN(28, 7): /* PCI Express Root Port 8 */
169 RCBA32_OR(FD, PCH_DISABLE_PCIE(PCI_FUNC(devfn)));
170 break;
171 case PCI_DEVFN(29, 0): /* EHCI #1 */
172 RCBA32_OR(FD, PCH_DISABLE_EHCI1);
173 break;
174 case PCI_DEVFN(30, 0): /* PCI-to-PCI Bridge */
175 RCBA32_OR(FD, PCH_DISABLE_P2P);
176 break;
177 case PCI_DEVFN(31, 0): /* LPC */
178 RCBA32_OR(FD, PCH_DISABLE_LPC);
179 break;
180 case PCI_DEVFN(31, 2): /* SATA #1 */
181 RCBA32_OR(FD, PCH_DISABLE_SATA1);
182 break;
183 case PCI_DEVFN(31, 3): /* SMBUS */
184 RCBA32_OR(FD, PCH_DISABLE_SMBUS);
185 break;
186 case PCI_DEVFN(31, 5): /* SATA #22 */
187 RCBA32_OR(FD, PCH_DISABLE_SATA2);
188 break;
189 case PCI_DEVFN(31, 6): /* Thermal Subsystem */
190 RCBA32_OR(FD, PCH_DISABLE_THERMAL);
191 break;
192 }
193}
194
Stefan Reinauer8e073822012-04-04 00:07:22 +0200195/* Check if any port in set X to X+3 is enabled */
Elyes HAOUASdc035282018-09-18 13:28:49 +0200196static int pch_pcie_check_set_enabled(struct device *dev)
Stefan Reinauer8e073822012-04-04 00:07:22 +0200197{
Elyes HAOUASdc035282018-09-18 13:28:49 +0200198 struct device *port;
Stefan Reinauer8e073822012-04-04 00:07:22 +0200199 int port_func;
200 int dev_func = PCI_FUNC(dev->path.pci.devfn);
201
202 printk(BIOS_DEBUG, "%s: check set enabled\n", dev_path(dev));
203
204 /* Go through static device tree list of devices
205 * because enumeration is still in progress */
206 for (port = all_devices; port; port = port->next) {
207 /* Only care about PCIe root ports */
208 if (PCI_SLOT(port->path.pci.devfn) !=
209 PCI_SLOT(dev->path.pci.devfn))
210 continue;
211
212 /* Check if port is in range and enabled */
213 port_func = PCI_FUNC(port->path.pci.devfn);
214 if (port_func >= dev_func &&
215 port_func < (dev_func + 4) &&
216 port->enabled)
217 return 1;
218 }
219
220 /* None of the ports in this set are enabled */
221 return 0;
222}
223
Duncan Laurieb9fe01c2012-04-27 10:30:51 -0700224/* RPFN is a write-once register so keep a copy until it is written */
225static u32 new_rpfn;
226
227/* Swap function numbers assigned to two PCIe Root Ports */
228static void pch_pcie_function_swap(u8 old_fn, u8 new_fn)
Stefan Reinauer8e073822012-04-04 00:07:22 +0200229{
Duncan Laurieb9fe01c2012-04-27 10:30:51 -0700230 u32 old_rpfn = new_rpfn;
231
232 printk(BIOS_DEBUG, "PCH: Remap PCIe function %d to %d\n",
233 old_fn, new_fn);
234
235 new_rpfn &= ~(RPFN_FNMASK(old_fn) | RPFN_FNMASK(new_fn));
236
237 /* Old function set to new function and disabled */
238 new_rpfn |= RPFN_FNSET(old_fn, RPFN_FNGET(old_rpfn, new_fn));
239 new_rpfn |= RPFN_FNSET(new_fn, RPFN_FNGET(old_rpfn, old_fn));
240}
241
242/* Update devicetree with new Root Port function number assignment */
Bill XIE8c57d092017-08-25 22:07:12 +0800243static void pch_pcie_devicetree_update(
244 struct southbridge_intel_bd82x6x_config *config)
Duncan Laurieb9fe01c2012-04-27 10:30:51 -0700245{
Elyes HAOUASdc035282018-09-18 13:28:49 +0200246 struct device *dev;
Duncan Laurieb9fe01c2012-04-27 10:30:51 -0700247
Bill XIE8c57d092017-08-25 22:07:12 +0800248 /*
249 * hotplug map should also be updated along with their
250 * corresponding port
251 */
252 u8 new_hotplug_map[sizeof(config->pcie_hotplug_map)];
253
254 /*
255 * Slots that didn't move need the hotplug setting copied too,
256 * so "new_hotplug_map" is initialized with the values of the old map.
257 */
258 memcpy(new_hotplug_map, config->pcie_hotplug_map,
259 sizeof(new_hotplug_map));
260
Duncan Laurieb9fe01c2012-04-27 10:30:51 -0700261 /* Update the function numbers in the static devicetree */
262 for (dev = all_devices; dev; dev = dev->next) {
263 u8 new_devfn;
264
265 /* Only care about PCH PCIe root ports */
266 if (PCI_SLOT(dev->path.pci.devfn) !=
267 PCH_PCIE_DEV_SLOT)
268 continue;
269
270 /* Determine the new devfn for this port */
271 new_devfn = PCI_DEVFN(PCH_PCIE_DEV_SLOT,
272 RPFN_FNGET(new_rpfn,
273 PCI_FUNC(dev->path.pci.devfn)));
274
275 if (dev->path.pci.devfn != new_devfn) {
276 printk(BIOS_DEBUG,
277 "PCH: PCIe map %02x.%1x -> %02x.%1x\n",
278 PCI_SLOT(dev->path.pci.devfn),
279 PCI_FUNC(dev->path.pci.devfn),
280 PCI_SLOT(new_devfn), PCI_FUNC(new_devfn));
281
Bill XIE8c57d092017-08-25 22:07:12 +0800282 /*
283 * Copy the flag to its new position along with
284 * the corresponding port
285 */
286 new_hotplug_map[PCI_FUNC(new_devfn)] =
287 config->pcie_hotplug_map
288 [PCI_FUNC(dev->path.pci.devfn)];
289
Duncan Laurieb9fe01c2012-04-27 10:30:51 -0700290 dev->path.pci.devfn = new_devfn;
291 }
292 }
Bill XIE8c57d092017-08-25 22:07:12 +0800293
294 /* Copy the updated map back to its place */
295 memcpy(config->pcie_hotplug_map, new_hotplug_map,
296 sizeof(new_hotplug_map));
Duncan Laurieb9fe01c2012-04-27 10:30:51 -0700297}
298
299/* Special handling for PCIe Root Port devices */
Elyes HAOUASdc035282018-09-18 13:28:49 +0200300static void pch_pcie_enable(struct device *dev)
Duncan Laurieb9fe01c2012-04-27 10:30:51 -0700301{
302 struct southbridge_intel_bd82x6x_config *config = dev->chip_info;
Stefan Reinauer8e073822012-04-04 00:07:22 +0200303
Bill XIE8c57d092017-08-25 22:07:12 +0800304 if (!config)
305 return;
306
Duncan Laurieb9fe01c2012-04-27 10:30:51 -0700307 /*
308 * Save a copy of the Root Port Function Number map when
309 * starting to walk the list of PCIe Root Ports so it can
310 * be updated locally and written out when the last port
311 * has been processed.
312 */
313 if (PCI_FUNC(dev->path.pci.devfn) == 0) {
314 new_rpfn = RCBA32(RPFN);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200315
316 /*
Duncan Laurieb9fe01c2012-04-27 10:30:51 -0700317 * Enable Root Port coalescing if the first port is disabled
318 * or the other devices will not be enumerated by the OS.
319 */
320 if (!dev->enabled)
Angel Ponsaf4bd562021-12-28 13:05:56 +0100321 config->pcie_port_coalesce = true;
Duncan Laurieb9fe01c2012-04-27 10:30:51 -0700322
323 if (config->pcie_port_coalesce)
324 printk(BIOS_INFO,
325 "PCH: PCIe Root Port coalescing is enabled\n");
326 }
327
328 if (!dev->enabled) {
Elyes Haouas8b8ada62022-11-22 17:36:02 +0100329 printk(BIOS_DEBUG, "%s: Disabling device\n", dev_path(dev));
Marc Jonesef6b08c2012-06-15 23:03:15 -0600330
Duncan Laurieb9fe01c2012-04-27 10:30:51 -0700331 /*
332 * PCIE Power Savings for PantherPoint and CougarPoint/B1+
Stefan Reinauer8e073822012-04-04 00:07:22 +0200333 *
334 * If PCIe 0-3 disabled set Function 0 0xE2[0] = 1
335 * If PCIe 4-7 disabled set Function 4 0xE2[0] = 1
336 *
Elyes HAOUAS79ccc692020-02-24 13:43:39 +0100337 * This check is done here instead of PCIe driver
338 * because the PCIe driver enable() handler is not
Stefan Reinauer8e073822012-04-04 00:07:22 +0200339 * called unless the device is enabled.
340 */
Duncan Laurieb9fe01c2012-04-27 10:30:51 -0700341 if ((PCI_FUNC(dev->path.pci.devfn) == 0 ||
Stefan Reinauer8e073822012-04-04 00:07:22 +0200342 PCI_FUNC(dev->path.pci.devfn) == 4)) {
Duncan Laurieb9fe01c2012-04-27 10:30:51 -0700343 /* Handle workaround for PPT and CPT/B1+ */
344 if (pch_silicon_supported(PCH_TYPE_CPT, PCH_STEP_B1) &&
345 !pch_pcie_check_set_enabled(dev)) {
Angel Ponsc803f652020-06-07 22:09:01 +0200346 pci_or_config8(dev, 0xe2, 1);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200347 }
Duncan Laurieb9fe01c2012-04-27 10:30:51 -0700348
349 /*
350 * Enable Clock Gating for shared PCIe resources
351 * before disabling this particular port.
352 */
353 pci_write_config8(dev, 0xe1, 0x3c);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200354 }
355
356 /* Ensure memory, io, and bus master are all disabled */
Angel Ponsc803f652020-06-07 22:09:01 +0200357 pci_and_config16(dev, PCI_COMMAND,
358 ~(PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY | PCI_COMMAND_IO));
Duncan Laurieb9fe01c2012-04-27 10:30:51 -0700359
360 /* Do not claim downstream transactions for PCIe ports */
361 new_rpfn |= RPFN_HIDE(PCI_FUNC(dev->path.pci.devfn));
362
363 /* Hide this device if possible */
364 pch_hide_devfn(dev->path.pci.devfn);
365 } else {
366 int fn;
367
368 /*
369 * Check if there is a lower disabled port to swap with this
370 * port in order to maintain linear order starting at zero.
371 */
372 if (config->pcie_port_coalesce) {
373 for (fn=0; fn < PCI_FUNC(dev->path.pci.devfn); fn++) {
374 if (!(new_rpfn & RPFN_HIDE(fn)))
375 continue;
376
377 /* Swap places with this function */
378 pch_pcie_function_swap(
379 PCI_FUNC(dev->path.pci.devfn), fn);
380 break;
381 }
382 }
383
384 /* Enable SERR */
Elyes HAOUAS729c0692020-04-28 19:50:44 +0200385 pci_or_config16(dev, PCI_COMMAND, PCI_COMMAND_SERR);
Duncan Laurieb9fe01c2012-04-27 10:30:51 -0700386 }
387
388 /*
389 * When processing the last PCIe root port we can now
390 * update the Root Port Function Number and Hide register.
391 */
392 if (PCI_FUNC(dev->path.pci.devfn) == 7) {
393 printk(BIOS_SPEW, "PCH: RPFN 0x%08x -> 0x%08x\n",
394 RCBA32(RPFN), new_rpfn);
395 RCBA32(RPFN) = new_rpfn;
396
397 /* Update static devictree with new function numbers */
398 if (config->pcie_port_coalesce)
Bill XIE8c57d092017-08-25 22:07:12 +0800399 pch_pcie_devicetree_update(config);
Duncan Laurieb9fe01c2012-04-27 10:30:51 -0700400 }
401}
402
Elyes HAOUASdc035282018-09-18 13:28:49 +0200403void pch_enable(struct device *dev)
Duncan Laurieb9fe01c2012-04-27 10:30:51 -0700404{
Duncan Laurieb9fe01c2012-04-27 10:30:51 -0700405 /* PCH PCIe Root Ports get special handling */
406 if (PCI_SLOT(dev->path.pci.devfn) == PCH_PCIE_DEV_SLOT)
407 return pch_pcie_enable(dev);
408
409 if (!dev->enabled) {
Elyes Haouas8b8ada62022-11-22 17:36:02 +0100410 printk(BIOS_DEBUG, "%s: Disabling device\n", dev_path(dev));
Duncan Laurieb9fe01c2012-04-27 10:30:51 -0700411
412 /* Ensure memory, io, and bus master are all disabled */
Angel Ponsc803f652020-06-07 22:09:01 +0200413 pci_and_config16(dev, PCI_COMMAND,
414 ~(PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY | PCI_COMMAND_IO));
Stefan Reinauer8e073822012-04-04 00:07:22 +0200415
416 /* Hide this device if possible */
417 pch_hide_devfn(dev->path.pci.devfn);
418 } else {
419 /* Enable SERR */
Elyes HAOUAS729c0692020-04-28 19:50:44 +0200420 pci_or_config16(dev, PCI_COMMAND, PCI_COMMAND_SERR);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200421 }
422}
423
424struct chip_operations southbridge_intel_bd82x6x_ops = {
Stefan Reinauer9ca1c0a2012-07-25 16:10:36 -0700425 CHIP_NAME("Intel Series 6/7 (Cougar Point/Panther Point) Southbridge")
Stefan Reinauer8e073822012-04-04 00:07:22 +0200426 .enable_dev = pch_enable,
427};
Marc Jones783f2262013-02-11 14:36:35 -0700428#endif