blob: e68e31a8227a7fabb087224067b4832512b63504 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass37a3f94b2015-11-29 13:17:49 -07002/*
3 * PCI autoconfiguration library
4 *
5 * Author: Matt Porter <mporter@mvista.com>
6 *
7 * Copyright 2000 MontaVista Software Inc.
Maciej W. Rozycki93008ab2021-11-20 23:03:30 +00008 * Copyright (c) 2021 Maciej W. Rozycki <macro@orcam.me.uk>
Simon Glass37a3f94b2015-11-29 13:17:49 -07009 */
10
Tom Rinidec7ea02024-05-20 13:35:03 -060011#include <config.h>
Simon Glass4af3dc12016-01-18 20:19:16 -070012#include <dm.h>
Simon Glass37a3f94b2015-11-29 13:17:49 -070013#include <errno.h>
Simon Glass0f2af882020-05-10 11:40:05 -060014#include <log.h>
Simon Glass37a3f94b2015-11-29 13:17:49 -070015#include <pci.h>
Maciej W. Rozycki93008ab2021-11-20 23:03:30 +000016#include <time.h>
Vladimir Oltean2363f8a2021-09-17 15:11:21 +030017#include "pci_internal.h"
Simon Glass37a3f94b2015-11-29 13:17:49 -070018
Tom Rini56af6592022-11-16 13:10:33 -050019/* the user can define CFG_SYS_PCI_CACHE_LINE_SIZE to avoid problems */
20#ifndef CFG_SYS_PCI_CACHE_LINE_SIZE
21#define CFG_SYS_PCI_CACHE_LINE_SIZE 8
Simon Glass37a3f94b2015-11-29 13:17:49 -070022#endif
23
Pali Rohár85974022021-10-07 14:50:59 +020024static void dm_pciauto_setup_device(struct udevice *dev,
Stefan Roeseede89092021-01-12 12:03:43 +010025 struct pci_region *mem,
26 struct pci_region *prefetch,
27 struct pci_region *io)
Simon Glass37a3f94b2015-11-29 13:17:49 -070028{
29 u32 bar_response;
30 pci_size_t bar_size;
31 u16 cmdstat = 0;
32 int bar, bar_nr = 0;
Pali Rohár85974022021-10-07 14:50:59 +020033 int bars_num;
Simon Glass37a3f94b2015-11-29 13:17:49 -070034 u8 header_type;
35 int rom_addr;
36 pci_addr_t bar_value;
Bin Mengd143ddb2016-02-17 23:14:47 -080037 struct pci_region *bar_res = NULL;
Simon Glass37a3f94b2015-11-29 13:17:49 -070038 int found_mem64 = 0;
39 u16 class;
40
41 dm_pci_read_config16(dev, PCI_COMMAND, &cmdstat);
42 cmdstat = (cmdstat & ~(PCI_COMMAND_IO | PCI_COMMAND_MEMORY)) |
43 PCI_COMMAND_MASTER;
44
Pali Rohár85974022021-10-07 14:50:59 +020045 dm_pci_read_config8(dev, PCI_HEADER_TYPE, &header_type);
46 header_type &= 0x7f;
47
48 switch (header_type) {
49 case PCI_HEADER_TYPE_NORMAL:
50 bars_num = 6;
51 break;
52 case PCI_HEADER_TYPE_BRIDGE:
53 bars_num = 2;
54 break;
55 case PCI_HEADER_TYPE_CARDBUS:
56 /* CardBus header does not have any BAR */
57 bars_num = 0;
58 break;
59 default:
60 /* Skip configuring BARs for unknown header types */
61 bars_num = 0;
62 break;
63 }
64
Simon Glass37a3f94b2015-11-29 13:17:49 -070065 for (bar = PCI_BASE_ADDRESS_0;
66 bar < PCI_BASE_ADDRESS_0 + (bars_num * 4); bar += 4) {
Simon Glassbb99abf2019-09-25 08:56:16 -060067 int ret = 0;
68
Simon Glass37a3f94b2015-11-29 13:17:49 -070069 /* Tickle the BAR and get the response */
Stefan Roeseede89092021-01-12 12:03:43 +010070 dm_pci_write_config32(dev, bar, 0xffffffff);
Simon Glass37a3f94b2015-11-29 13:17:49 -070071 dm_pci_read_config32(dev, bar, &bar_response);
72
Phil Sutterb70ae132021-01-03 23:06:45 +010073 /* If BAR is not implemented (or invalid) go to the next BAR */
74 if (!bar_response || bar_response == 0xffffffff)
Simon Glass37a3f94b2015-11-29 13:17:49 -070075 continue;
76
77 found_mem64 = 0;
78
79 /* Check the BAR type and set our address mask */
80 if (bar_response & PCI_BASE_ADDRESS_SPACE) {
Phil Sutterb70ae132021-01-03 23:06:45 +010081 bar_size = bar_response & PCI_BASE_ADDRESS_IO_MASK;
82 bar_size &= ~(bar_size - 1);
83
Stefan Roeseede89092021-01-12 12:03:43 +010084 bar_res = io;
Simon Glass37a3f94b2015-11-29 13:17:49 -070085
86 debug("PCI Autoconfig: BAR %d, I/O, size=0x%llx, ",
87 bar_nr, (unsigned long long)bar_size);
88 } else {
89 if ((bar_response & PCI_BASE_ADDRESS_MEM_TYPE_MASK) ==
90 PCI_BASE_ADDRESS_MEM_TYPE_64) {
91 u32 bar_response_upper;
92 u64 bar64;
93
Stefan Roeseede89092021-01-12 12:03:43 +010094 dm_pci_write_config32(dev, bar + 4, 0xffffffff);
Simon Glass37a3f94b2015-11-29 13:17:49 -070095 dm_pci_read_config32(dev, bar + 4,
96 &bar_response_upper);
97
98 bar64 = ((u64)bar_response_upper << 32) |
99 bar_response;
100
101 bar_size = ~(bar64 & PCI_BASE_ADDRESS_MEM_MASK)
102 + 1;
Stefan Roeseede89092021-01-12 12:03:43 +0100103 found_mem64 = 1;
Simon Glass37a3f94b2015-11-29 13:17:49 -0700104 } else {
105 bar_size = (u32)(~(bar_response &
106 PCI_BASE_ADDRESS_MEM_MASK) + 1);
107 }
Stefan Roeseede89092021-01-12 12:03:43 +0100108
109 if (prefetch &&
Patrick Rudolphe86539d2025-02-26 14:56:42 +0100110 (bar_response & PCI_BASE_ADDRESS_MEM_PREFETCH) &&
111 (found_mem64 || prefetch->bus_lower < 0x100000000ULL))
Stefan Roeseede89092021-01-12 12:03:43 +0100112 bar_res = prefetch;
113 else
114 bar_res = mem;
Simon Glass37a3f94b2015-11-29 13:17:49 -0700115
Phil Sutter8f902732021-03-03 01:57:35 +0100116 debug("PCI Autoconfig: BAR %d, %s%s, size=0x%llx, ",
Simon Glass37a3f94b2015-11-29 13:17:49 -0700117 bar_nr, bar_res == prefetch ? "Prf" : "Mem",
Phil Sutter8f902732021-03-03 01:57:35 +0100118 found_mem64 ? "64" : "",
Simon Glass37a3f94b2015-11-29 13:17:49 -0700119 (unsigned long long)bar_size);
120 }
121
Stefan Roeseede89092021-01-12 12:03:43 +0100122 ret = pciauto_region_allocate(bar_res, bar_size,
123 &bar_value, found_mem64);
124 if (ret)
125 printf("PCI: Failed autoconfig bar %x\n", bar);
126
127 if (!ret) {
Simon Glass37a3f94b2015-11-29 13:17:49 -0700128 /* Write it out and update our limit */
129 dm_pci_write_config32(dev, bar, (u32)bar_value);
130
131 if (found_mem64) {
132 bar += 4;
133#ifdef CONFIG_SYS_PCI_64BIT
134 dm_pci_write_config32(dev, bar,
135 (u32)(bar_value >> 32));
136#else
137 /*
138 * If we are a 64-bit decoder then increment to
139 * the upper 32 bits of the bar and force it to
140 * locate in the lower 4GB of memory.
141 */
142 dm_pci_write_config32(dev, bar, 0x00000000);
143#endif
144 }
145 }
146
147 cmdstat |= (bar_response & PCI_BASE_ADDRESS_SPACE) ?
148 PCI_COMMAND_IO : PCI_COMMAND_MEMORY;
149
150 debug("\n");
151
152 bar_nr++;
153 }
154
Stefan Roeseede89092021-01-12 12:03:43 +0100155 /* Configure the expansion ROM address */
Pali Rohár39a1d972021-10-07 14:50:57 +0200156 if (header_type == PCI_HEADER_TYPE_NORMAL ||
157 header_type == PCI_HEADER_TYPE_BRIDGE) {
Stefan Roeseede89092021-01-12 12:03:43 +0100158 rom_addr = (header_type == PCI_HEADER_TYPE_NORMAL) ?
159 PCI_ROM_ADDRESS : PCI_ROM_ADDRESS1;
160 dm_pci_write_config32(dev, rom_addr, 0xfffffffe);
161 dm_pci_read_config32(dev, rom_addr, &bar_response);
162 if (bar_response) {
163 bar_size = -(bar_response & ~1);
164 debug("PCI Autoconfig: ROM, size=%#x, ",
165 (unsigned int)bar_size);
166 if (pciauto_region_allocate(mem, bar_size, &bar_value,
167 false) == 0) {
168 dm_pci_write_config32(dev, rom_addr, bar_value);
Simon Glass37a3f94b2015-11-29 13:17:49 -0700169 }
Stefan Roeseede89092021-01-12 12:03:43 +0100170 cmdstat |= PCI_COMMAND_MEMORY;
171 debug("\n");
Simon Glass37a3f94b2015-11-29 13:17:49 -0700172 }
173 }
174
175 /* PCI_COMMAND_IO must be set for VGA device */
176 dm_pci_read_config16(dev, PCI_CLASS_DEVICE, &class);
177 if (class == PCI_CLASS_DISPLAY_VGA)
178 cmdstat |= PCI_COMMAND_IO;
179
180 dm_pci_write_config16(dev, PCI_COMMAND, cmdstat);
181 dm_pci_write_config8(dev, PCI_CACHE_LINE_SIZE,
Tom Rini56af6592022-11-16 13:10:33 -0500182 CFG_SYS_PCI_CACHE_LINE_SIZE);
Simon Glass37a3f94b2015-11-29 13:17:49 -0700183 dm_pci_write_config8(dev, PCI_LATENCY_TIMER, 0x80);
184}
185
Maciej W. Rozycki93008ab2021-11-20 23:03:30 +0000186/*
187 * Check if the link of a downstream PCIe port operates correctly.
188 *
189 * For that check if the optional Data Link Layer Link Active status gets
190 * on within a 200ms period or failing that wait until the completion of
191 * that period and check if link training has shown the completed status
192 * continuously throughout the second half of that period.
193 *
194 * Observation with the ASMedia ASM2824 Gen 3 switch indicates it takes
195 * 11-44ms to indicate the Data Link Layer Link Active status at 2.5GT/s,
196 * though it may take a couple of link training iterations.
197 */
198static bool dm_pciauto_exp_link_stable(struct udevice *dev, int pcie_off)
199{
200 u64 loops = 0, trcount = 0, ntrcount = 0, flips = 0;
201 bool dllla, lnktr, plnktr;
202 u16 exp_lnksta;
203 pci_dev_t bdf;
204 u64 end;
205
206 dm_pci_read_config16(dev, pcie_off + PCI_EXP_LNKSTA, &exp_lnksta);
207 plnktr = !!(exp_lnksta & PCI_EXP_LNKSTA_LT);
208
209 end = get_ticks() + usec_to_tick(200000);
210 do {
211 dm_pci_read_config16(dev, pcie_off + PCI_EXP_LNKSTA,
212 &exp_lnksta);
213 dllla = !!(exp_lnksta & PCI_EXP_LNKSTA_DLLLA);
214 lnktr = !!(exp_lnksta & PCI_EXP_LNKSTA_LT);
215
216 flips += plnktr ^ lnktr;
217 if (lnktr) {
218 ntrcount = 0;
219 trcount++;
220 } else {
221 ntrcount++;
222 }
223 loops++;
224
225 plnktr = lnktr;
226 } while (!dllla && get_ticks() < end);
227
228 bdf = dm_pci_get_bdf(dev);
229 debug("PCI Autoconfig: %02x.%02x.%02x: Fixup link: DL active: %u; "
230 "%3llu flips, %6llu loops of which %6llu while training, "
231 "final %6llu stable\n",
232 PCI_BUS(bdf), PCI_DEV(bdf), PCI_FUNC(bdf),
233 (unsigned int)dllla,
234 (unsigned long long)flips, (unsigned long long)loops,
235 (unsigned long long)trcount, (unsigned long long)ntrcount);
236
237 return dllla || ntrcount >= loops / 2;
238}
239
240/*
241 * Retrain the link of a downstream PCIe port by hand if necessary.
242 *
243 * This is needed at least where a downstream port of the ASMedia ASM2824
244 * Gen 3 switch is wired to the upstream port of the Pericom PI7C9X2G304
245 * Gen 2 switch, and observed with the Delock Riser Card PCI Express x1 >
246 * 2 x PCIe x1 device, P/N 41433, plugged into the SiFive HiFive Unmatched
247 * board.
248 *
249 * In such a configuration the switches are supposed to negotiate the link
250 * speed of preferably 5.0GT/s, falling back to 2.5GT/s. However the link
251 * continues switching between the two speeds indefinitely and the data
252 * link layer never reaches the active state, with link training reported
253 * repeatedly active ~84% of the time. Forcing the target link speed to
254 * 2.5GT/s with the upstream ASM2824 device makes the two switches talk to
255 * each other correctly however. And more interestingly retraining with a
256 * higher target link speed afterwards lets the two successfully negotiate
257 * 5.0GT/s.
258 *
259 * As this can potentially happen with any device and is cheap in the case
260 * of correctly operating hardware, let's do it for all downstream ports,
261 * for root complexes, PCIe switches and PCI/PCI-X to PCIe bridges.
262 *
263 * First check if automatic link training may have failed to complete, as
264 * indicated by the optional Data Link Layer Link Active status being off
265 * and the Link Bandwidth Management Status indicating that hardware has
266 * changed the link speed or width in an attempt to correct unreliable
267 * link operation. If this is the case, then check if the link operates
268 * correctly by seeing whether it is being trained excessively. If it is,
269 * then conclude the link is broken.
270 *
271 * In that case restrict the speed to 2.5GT/s, observing that the Target
272 * Link Speed field is sticky and therefore the link will stay restricted
273 * even after a device reset is later made by an OS that is unaware of the
274 * problem. With the speed restricted request that the link be retrained
275 * and check again if the link operates correctly. If not, then set the
276 * Target Link Speed back to the original value.
277 *
278 * This requires the presence of the Link Control 2 register, so make sure
279 * the PCI Express Capability Version is at least 2. Also don't try, for
280 * obvious reasons, to limit the speed if 2.5GT/s is the only link speed
281 * supported.
282 */
283static void dm_pciauto_exp_fixup_link(struct udevice *dev, int pcie_off)
284{
285 u16 exp_lnksta, exp_lnkctl, exp_lnkctl2;
286 u16 exp_flags, exp_type, exp_version;
287 u32 exp_lnkcap;
288 pci_dev_t bdf;
289
290 dm_pci_read_config16(dev, pcie_off + PCI_EXP_FLAGS, &exp_flags);
291 exp_version = exp_flags & PCI_EXP_FLAGS_VERS;
292 if (exp_version < 2)
293 return;
294
295 exp_type = (exp_flags & PCI_EXP_FLAGS_TYPE) >> 4;
296 switch (exp_type) {
297 case PCI_EXP_TYPE_ROOT_PORT:
298 case PCI_EXP_TYPE_DOWNSTREAM:
299 case PCI_EXP_TYPE_PCIE_BRIDGE:
300 break;
301 default:
302 return;
303 }
304
305 dm_pci_read_config32(dev, pcie_off + PCI_EXP_LNKCAP, &exp_lnkcap);
306 if ((exp_lnkcap & PCI_EXP_LNKCAP_SLS) <= PCI_EXP_LNKCAP_SLS_2_5GB)
307 return;
308
309 dm_pci_read_config16(dev, pcie_off + PCI_EXP_LNKSTA, &exp_lnksta);
310 if ((exp_lnksta & (PCI_EXP_LNKSTA_LBMS | PCI_EXP_LNKSTA_DLLLA)) !=
311 PCI_EXP_LNKSTA_LBMS)
312 return;
313
314 if (dm_pciauto_exp_link_stable(dev, pcie_off))
315 return;
316
317 bdf = dm_pci_get_bdf(dev);
318 printf("PCI Autoconfig: %02x.%02x.%02x: "
319 "Downstream link non-functional\n",
320 PCI_BUS(bdf), PCI_DEV(bdf), PCI_FUNC(bdf));
321 printf("PCI Autoconfig: %02x.%02x.%02x: "
322 "Retrying with speed restricted to 2.5GT/s...\n",
323 PCI_BUS(bdf), PCI_DEV(bdf), PCI_FUNC(bdf));
324
325 dm_pci_read_config16(dev, pcie_off + PCI_EXP_LNKCTL, &exp_lnkctl);
326 dm_pci_read_config16(dev, pcie_off + PCI_EXP_LNKCTL2, &exp_lnkctl2);
327
328 dm_pci_write_config16(dev, pcie_off + PCI_EXP_LNKCTL2,
329 (exp_lnkctl2 & ~PCI_EXP_LNKCTL2_TLS) |
330 PCI_EXP_LNKCTL2_TLS_2_5GT);
331 dm_pci_write_config16(dev, pcie_off + PCI_EXP_LNKCTL,
332 exp_lnkctl | PCI_EXP_LNKCTL_RL);
333
334 if (dm_pciauto_exp_link_stable(dev, pcie_off)) {
335 printf("PCI Autoconfig: %02x.%02x.%02x: Succeeded!\n",
336 PCI_BUS(bdf), PCI_DEV(bdf), PCI_FUNC(bdf));
337 } else {
338 printf("PCI Autoconfig: %02x.%02x.%02x: Failed!\n",
339 PCI_BUS(bdf), PCI_DEV(bdf), PCI_FUNC(bdf));
340
341 dm_pci_write_config16(dev, pcie_off + PCI_EXP_LNKCTL2,
342 exp_lnkctl2);
343 dm_pci_write_config16(dev, pcie_off + PCI_EXP_LNKCTL,
344 exp_lnkctl | PCI_EXP_LNKCTL_RL);
345 }
346}
347
Simon Glass37a3f94b2015-11-29 13:17:49 -0700348void dm_pciauto_prescan_setup_bridge(struct udevice *dev, int sub_bus)
349{
350 struct pci_region *pci_mem;
351 struct pci_region *pci_prefetch;
352 struct pci_region *pci_io;
353 u16 cmdstat, prefechable_64;
Pali Rohár36e8fa02021-09-10 13:33:35 +0200354 u8 io_32;
Simon Glass4af3dc12016-01-18 20:19:16 -0700355 struct udevice *ctlr = pci_get_controller(dev);
356 struct pci_controller *ctlr_hose = dev_get_uclass_priv(ctlr);
Maciej W. Rozycki93008ab2021-11-20 23:03:30 +0000357 int pcie_off;
Simon Glass37a3f94b2015-11-29 13:17:49 -0700358
359 pci_mem = ctlr_hose->pci_mem;
360 pci_prefetch = ctlr_hose->pci_prefetch;
361 pci_io = ctlr_hose->pci_io;
362
363 dm_pci_read_config16(dev, PCI_COMMAND, &cmdstat);
364 dm_pci_read_config16(dev, PCI_PREF_MEMORY_BASE, &prefechable_64);
365 prefechable_64 &= PCI_PREF_RANGE_TYPE_MASK;
Pali Roháre7a8e702021-11-25 11:30:58 +0100366 dm_pci_read_config8(dev, PCI_IO_BASE, &io_32);
Pali Rohár36e8fa02021-09-10 13:33:35 +0200367 io_32 &= PCI_IO_RANGE_TYPE_MASK;
Simon Glass37a3f94b2015-11-29 13:17:49 -0700368
369 /* Configure bus number registers */
370 dm_pci_write_config8(dev, PCI_PRIMARY_BUS,
Simon Glass75e534b2020-12-16 21:20:07 -0700371 PCI_BUS(dm_pci_get_bdf(dev)) - dev_seq(ctlr));
372 dm_pci_write_config8(dev, PCI_SECONDARY_BUS, sub_bus - dev_seq(ctlr));
Simon Glass37a3f94b2015-11-29 13:17:49 -0700373 dm_pci_write_config8(dev, PCI_SUBORDINATE_BUS, 0xff);
374
375 if (pci_mem) {
376 /* Round memory allocator to 1MB boundary */
377 pciauto_region_align(pci_mem, 0x100000);
378
379 /*
380 * Set up memory and I/O filter limits, assume 32-bit
381 * I/O space
382 */
383 dm_pci_write_config16(dev, PCI_MEMORY_BASE,
Pali Rohár36e8fa02021-09-10 13:33:35 +0200384 ((pci_mem->bus_lower & 0xfff00000) >> 16) &
385 PCI_MEMORY_RANGE_MASK);
Simon Glass37a3f94b2015-11-29 13:17:49 -0700386
387 cmdstat |= PCI_COMMAND_MEMORY;
388 }
389
390 if (pci_prefetch) {
391 /* Round memory allocator to 1MB boundary */
392 pciauto_region_align(pci_prefetch, 0x100000);
393
394 /*
395 * Set up memory and I/O filter limits, assume 32-bit
396 * I/O space
397 */
398 dm_pci_write_config16(dev, PCI_PREF_MEMORY_BASE,
Pali Rohár36e8fa02021-09-10 13:33:35 +0200399 (((pci_prefetch->bus_lower & 0xfff00000) >> 16) &
400 PCI_PREF_RANGE_MASK) | prefechable_64);
Simon Glass37a3f94b2015-11-29 13:17:49 -0700401 if (prefechable_64 == PCI_PREF_RANGE_TYPE_64)
402#ifdef CONFIG_SYS_PCI_64BIT
403 dm_pci_write_config32(dev, PCI_PREF_BASE_UPPER32,
404 pci_prefetch->bus_lower >> 32);
405#else
406 dm_pci_write_config32(dev, PCI_PREF_BASE_UPPER32, 0x0);
407#endif
408
409 cmdstat |= PCI_COMMAND_MEMORY;
410 } else {
411 /* We don't support prefetchable memory for now, so disable */
Pali Rohár5a526712021-11-25 11:34:37 +0100412 dm_pci_write_config16(dev, PCI_PREF_MEMORY_BASE, 0xfff0 |
Pali Rohár36e8fa02021-09-10 13:33:35 +0200413 prefechable_64);
414 dm_pci_write_config16(dev, PCI_PREF_MEMORY_LIMIT, 0x0 |
415 prefechable_64);
Simon Glass37a3f94b2015-11-29 13:17:49 -0700416 if (prefechable_64 == PCI_PREF_RANGE_TYPE_64) {
417 dm_pci_write_config16(dev, PCI_PREF_BASE_UPPER32, 0x0);
418 dm_pci_write_config16(dev, PCI_PREF_LIMIT_UPPER32, 0x0);
419 }
420 }
421
422 if (pci_io) {
423 /* Round I/O allocator to 4KB boundary */
424 pciauto_region_align(pci_io, 0x1000);
425
426 dm_pci_write_config8(dev, PCI_IO_BASE,
Pali Rohár36e8fa02021-09-10 13:33:35 +0200427 (((pci_io->bus_lower & 0x0000f000) >> 8) &
428 PCI_IO_RANGE_MASK) | io_32);
429 if (io_32 == PCI_IO_RANGE_TYPE_32)
430 dm_pci_write_config16(dev, PCI_IO_BASE_UPPER16,
Simon Glass37a3f94b2015-11-29 13:17:49 -0700431 (pci_io->bus_lower & 0xffff0000) >> 16);
432
433 cmdstat |= PCI_COMMAND_IO;
Pali Rohár597acfb2021-11-25 11:32:43 +0100434 } else {
435 /* Disable I/O if unsupported */
436 dm_pci_write_config8(dev, PCI_IO_BASE, 0xf0 | io_32);
437 dm_pci_write_config8(dev, PCI_IO_LIMIT, 0x0 | io_32);
438 if (io_32 == PCI_IO_RANGE_TYPE_32) {
439 dm_pci_write_config16(dev, PCI_IO_BASE_UPPER16, 0x0);
440 dm_pci_write_config16(dev, PCI_IO_LIMIT_UPPER16, 0x0);
441 }
Simon Glass37a3f94b2015-11-29 13:17:49 -0700442 }
443
Maciej W. Rozycki93008ab2021-11-20 23:03:30 +0000444 /* For PCIe devices see if we need to retrain the link by hand */
445 pcie_off = dm_pci_find_capability(dev, PCI_CAP_ID_EXP);
446 if (pcie_off)
447 dm_pciauto_exp_fixup_link(dev, pcie_off);
448
Simon Glass37a3f94b2015-11-29 13:17:49 -0700449 /* Enable memory and I/O accesses, enable bus master */
450 dm_pci_write_config16(dev, PCI_COMMAND, cmdstat | PCI_COMMAND_MASTER);
451}
452
453void dm_pciauto_postscan_setup_bridge(struct udevice *dev, int sub_bus)
454{
455 struct pci_region *pci_mem;
456 struct pci_region *pci_prefetch;
457 struct pci_region *pci_io;
Simon Glass4af3dc12016-01-18 20:19:16 -0700458 struct udevice *ctlr = pci_get_controller(dev);
459 struct pci_controller *ctlr_hose = dev_get_uclass_priv(ctlr);
Simon Glass37a3f94b2015-11-29 13:17:49 -0700460
461 pci_mem = ctlr_hose->pci_mem;
462 pci_prefetch = ctlr_hose->pci_prefetch;
463 pci_io = ctlr_hose->pci_io;
464
465 /* Configure bus number registers */
Simon Glass75e534b2020-12-16 21:20:07 -0700466 dm_pci_write_config8(dev, PCI_SUBORDINATE_BUS, sub_bus - dev_seq(ctlr));
Simon Glass37a3f94b2015-11-29 13:17:49 -0700467
468 if (pci_mem) {
469 /* Round memory allocator to 1MB boundary */
470 pciauto_region_align(pci_mem, 0x100000);
471
472 dm_pci_write_config16(dev, PCI_MEMORY_LIMIT,
Pali Rohár36e8fa02021-09-10 13:33:35 +0200473 ((pci_mem->bus_lower - 1) >> 16) &
474 PCI_MEMORY_RANGE_MASK);
Simon Glass37a3f94b2015-11-29 13:17:49 -0700475 }
476
477 if (pci_prefetch) {
478 u16 prefechable_64;
479
480 dm_pci_read_config16(dev, PCI_PREF_MEMORY_LIMIT,
481 &prefechable_64);
482 prefechable_64 &= PCI_PREF_RANGE_TYPE_MASK;
483
484 /* Round memory allocator to 1MB boundary */
485 pciauto_region_align(pci_prefetch, 0x100000);
486
487 dm_pci_write_config16(dev, PCI_PREF_MEMORY_LIMIT,
Pali Rohár36e8fa02021-09-10 13:33:35 +0200488 (((pci_prefetch->bus_lower - 1) >> 16) &
489 PCI_PREF_RANGE_MASK) | prefechable_64);
Simon Glass37a3f94b2015-11-29 13:17:49 -0700490 if (prefechable_64 == PCI_PREF_RANGE_TYPE_64)
491#ifdef CONFIG_SYS_PCI_64BIT
492 dm_pci_write_config32(dev, PCI_PREF_LIMIT_UPPER32,
493 (pci_prefetch->bus_lower - 1) >> 32);
494#else
495 dm_pci_write_config32(dev, PCI_PREF_LIMIT_UPPER32, 0x0);
496#endif
497 }
498
499 if (pci_io) {
Pali Rohár36e8fa02021-09-10 13:33:35 +0200500 u8 io_32;
501
502 dm_pci_read_config8(dev, PCI_IO_LIMIT,
503 &io_32);
504 io_32 &= PCI_IO_RANGE_TYPE_MASK;
505
Simon Glass37a3f94b2015-11-29 13:17:49 -0700506 /* Round I/O allocator to 4KB boundary */
507 pciauto_region_align(pci_io, 0x1000);
508
509 dm_pci_write_config8(dev, PCI_IO_LIMIT,
Pali Rohár36e8fa02021-09-10 13:33:35 +0200510 ((((pci_io->bus_lower - 1) & 0x0000f000) >> 8) &
511 PCI_IO_RANGE_MASK) | io_32);
512 if (io_32 == PCI_IO_RANGE_TYPE_32)
513 dm_pci_write_config16(dev, PCI_IO_LIMIT_UPPER16,
Simon Glass37a3f94b2015-11-29 13:17:49 -0700514 ((pci_io->bus_lower - 1) & 0xffff0000) >> 16);
515 }
516}
517
518/*
519 * HJF: Changed this to return int. I think this is required
520 * to get the correct result when scanning bridges
521 */
522int dm_pciauto_config_device(struct udevice *dev)
523{
524 struct pci_region *pci_mem;
525 struct pci_region *pci_prefetch;
526 struct pci_region *pci_io;
527 unsigned int sub_bus = PCI_BUS(dm_pci_get_bdf(dev));
528 unsigned short class;
Simon Glass4af3dc12016-01-18 20:19:16 -0700529 struct udevice *ctlr = pci_get_controller(dev);
530 struct pci_controller *ctlr_hose = dev_get_uclass_priv(ctlr);
Simon Glassbe706102020-12-16 21:20:18 -0700531 int ret;
Simon Glass37a3f94b2015-11-29 13:17:49 -0700532
Simon Glass37a3f94b2015-11-29 13:17:49 -0700533 pci_mem = ctlr_hose->pci_mem;
534 pci_prefetch = ctlr_hose->pci_prefetch;
535 pci_io = ctlr_hose->pci_io;
536
537 dm_pci_read_config16(dev, PCI_CLASS_DEVICE, &class);
538
539 switch (class) {
540 case PCI_CLASS_BRIDGE_PCI:
541 debug("PCI Autoconfig: Found P2P bridge, device %d\n",
542 PCI_DEV(dm_pci_get_bdf(dev)));
543
Pali Rohár85974022021-10-07 14:50:59 +0200544 dm_pciauto_setup_device(dev, pci_mem, pci_prefetch, pci_io);
Simon Glass37a3f94b2015-11-29 13:17:49 -0700545
Simon Glassbe706102020-12-16 21:20:18 -0700546 ret = dm_pci_hose_probe_bus(dev);
547 if (ret < 0)
548 return log_msg_ret("probe", ret);
549 sub_bus = ret;
Simon Glass37a3f94b2015-11-29 13:17:49 -0700550 break;
551
552 case PCI_CLASS_BRIDGE_CARDBUS:
553 /*
554 * just do a minimal setup of the bridge,
555 * let the OS take care of the rest
556 */
Pali Rohár85974022021-10-07 14:50:59 +0200557 dm_pciauto_setup_device(dev, pci_mem, pci_prefetch, pci_io);
Simon Glass37a3f94b2015-11-29 13:17:49 -0700558
559 debug("PCI Autoconfig: Found P2CardBus bridge, device %d\n",
560 PCI_DEV(dm_pci_get_bdf(dev)));
561
562 break;
563
564#if defined(CONFIG_PCIAUTO_SKIP_HOST_BRIDGE)
565 case PCI_CLASS_BRIDGE_OTHER:
566 debug("PCI Autoconfig: Skipping bridge device %d\n",
567 PCI_DEV(dm_pci_get_bdf(dev)));
568 break;
569#endif
Tom Rinid1798232021-05-14 21:34:17 -0400570#if defined(CONFIG_ARCH_MPC834X)
Simon Glass37a3f94b2015-11-29 13:17:49 -0700571 case PCI_CLASS_BRIDGE_OTHER:
572 /*
573 * The host/PCI bridge 1 seems broken in 8349 - it presents
574 * itself as 'PCI_CLASS_BRIDGE_OTHER' and appears as an _agent_
575 * device claiming resources io/mem/irq.. we only allow for
576 * the PIMMR window to be allocated (BAR0 - 1MB size)
577 */
578 debug("PCI Autoconfig: Broken bridge found, only minimal config\n");
579 dm_pciauto_setup_device(dev, 0, hose->pci_mem,
Stefan Roeseede89092021-01-12 12:03:43 +0100580 hose->pci_prefetch, hose->pci_io);
Simon Glass37a3f94b2015-11-29 13:17:49 -0700581 break;
582#endif
583
Simon Glass37a3f94b2015-11-29 13:17:49 -0700584 default:
Pali Rohár85974022021-10-07 14:50:59 +0200585 dm_pciauto_setup_device(dev, pci_mem, pci_prefetch, pci_io);
Simon Glass37a3f94b2015-11-29 13:17:49 -0700586 break;
587 }
588
589 return sub_bus;
590}