Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 37a3f94b | 2015-11-29 13:17:49 -0700 | [diff] [blame] | 2 | /* |
| 3 | * PCI autoconfiguration library |
| 4 | * |
| 5 | * Author: Matt Porter <mporter@mvista.com> |
| 6 | * |
| 7 | * Copyright 2000 MontaVista Software Inc. |
Maciej W. Rozycki | 93008ab | 2021-11-20 23:03:30 +0000 | [diff] [blame] | 8 | * Copyright (c) 2021 Maciej W. Rozycki <macro@orcam.me.uk> |
Simon Glass | 37a3f94b | 2015-11-29 13:17:49 -0700 | [diff] [blame] | 9 | */ |
| 10 | |
Tom Rini | dec7ea0 | 2024-05-20 13:35:03 -0600 | [diff] [blame] | 11 | #include <config.h> |
Simon Glass | 4af3dc1 | 2016-01-18 20:19:16 -0700 | [diff] [blame] | 12 | #include <dm.h> |
Simon Glass | 37a3f94b | 2015-11-29 13:17:49 -0700 | [diff] [blame] | 13 | #include <errno.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 14 | #include <log.h> |
Simon Glass | 37a3f94b | 2015-11-29 13:17:49 -0700 | [diff] [blame] | 15 | #include <pci.h> |
Maciej W. Rozycki | 93008ab | 2021-11-20 23:03:30 +0000 | [diff] [blame] | 16 | #include <time.h> |
Vladimir Oltean | 2363f8a | 2021-09-17 15:11:21 +0300 | [diff] [blame] | 17 | #include "pci_internal.h" |
Simon Glass | 37a3f94b | 2015-11-29 13:17:49 -0700 | [diff] [blame] | 18 | |
Tom Rini | 56af659 | 2022-11-16 13:10:33 -0500 | [diff] [blame] | 19 | /* 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 Glass | 37a3f94b | 2015-11-29 13:17:49 -0700 | [diff] [blame] | 22 | #endif |
| 23 | |
Pali Rohár | 8597402 | 2021-10-07 14:50:59 +0200 | [diff] [blame] | 24 | static void dm_pciauto_setup_device(struct udevice *dev, |
Stefan Roese | ede8909 | 2021-01-12 12:03:43 +0100 | [diff] [blame] | 25 | struct pci_region *mem, |
| 26 | struct pci_region *prefetch, |
| 27 | struct pci_region *io) |
Simon Glass | 37a3f94b | 2015-11-29 13:17:49 -0700 | [diff] [blame] | 28 | { |
| 29 | u32 bar_response; |
| 30 | pci_size_t bar_size; |
| 31 | u16 cmdstat = 0; |
| 32 | int bar, bar_nr = 0; |
Pali Rohár | 8597402 | 2021-10-07 14:50:59 +0200 | [diff] [blame] | 33 | int bars_num; |
Simon Glass | 37a3f94b | 2015-11-29 13:17:49 -0700 | [diff] [blame] | 34 | u8 header_type; |
| 35 | int rom_addr; |
| 36 | pci_addr_t bar_value; |
Bin Meng | d143ddb | 2016-02-17 23:14:47 -0800 | [diff] [blame] | 37 | struct pci_region *bar_res = NULL; |
Simon Glass | 37a3f94b | 2015-11-29 13:17:49 -0700 | [diff] [blame] | 38 | 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ár | 8597402 | 2021-10-07 14:50:59 +0200 | [diff] [blame] | 45 | 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 Glass | 37a3f94b | 2015-11-29 13:17:49 -0700 | [diff] [blame] | 65 | for (bar = PCI_BASE_ADDRESS_0; |
| 66 | bar < PCI_BASE_ADDRESS_0 + (bars_num * 4); bar += 4) { |
Simon Glass | bb99abf | 2019-09-25 08:56:16 -0600 | [diff] [blame] | 67 | int ret = 0; |
| 68 | |
Simon Glass | 37a3f94b | 2015-11-29 13:17:49 -0700 | [diff] [blame] | 69 | /* Tickle the BAR and get the response */ |
Stefan Roese | ede8909 | 2021-01-12 12:03:43 +0100 | [diff] [blame] | 70 | dm_pci_write_config32(dev, bar, 0xffffffff); |
Simon Glass | 37a3f94b | 2015-11-29 13:17:49 -0700 | [diff] [blame] | 71 | dm_pci_read_config32(dev, bar, &bar_response); |
| 72 | |
Phil Sutter | b70ae13 | 2021-01-03 23:06:45 +0100 | [diff] [blame] | 73 | /* If BAR is not implemented (or invalid) go to the next BAR */ |
| 74 | if (!bar_response || bar_response == 0xffffffff) |
Simon Glass | 37a3f94b | 2015-11-29 13:17:49 -0700 | [diff] [blame] | 75 | 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 Sutter | b70ae13 | 2021-01-03 23:06:45 +0100 | [diff] [blame] | 81 | bar_size = bar_response & PCI_BASE_ADDRESS_IO_MASK; |
| 82 | bar_size &= ~(bar_size - 1); |
| 83 | |
Stefan Roese | ede8909 | 2021-01-12 12:03:43 +0100 | [diff] [blame] | 84 | bar_res = io; |
Simon Glass | 37a3f94b | 2015-11-29 13:17:49 -0700 | [diff] [blame] | 85 | |
| 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 Roese | ede8909 | 2021-01-12 12:03:43 +0100 | [diff] [blame] | 94 | dm_pci_write_config32(dev, bar + 4, 0xffffffff); |
Simon Glass | 37a3f94b | 2015-11-29 13:17:49 -0700 | [diff] [blame] | 95 | 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 Roese | ede8909 | 2021-01-12 12:03:43 +0100 | [diff] [blame] | 103 | found_mem64 = 1; |
Simon Glass | 37a3f94b | 2015-11-29 13:17:49 -0700 | [diff] [blame] | 104 | } else { |
| 105 | bar_size = (u32)(~(bar_response & |
| 106 | PCI_BASE_ADDRESS_MEM_MASK) + 1); |
| 107 | } |
Stefan Roese | ede8909 | 2021-01-12 12:03:43 +0100 | [diff] [blame] | 108 | |
| 109 | if (prefetch && |
Patrick Rudolph | e86539d | 2025-02-26 14:56:42 +0100 | [diff] [blame] | 110 | (bar_response & PCI_BASE_ADDRESS_MEM_PREFETCH) && |
| 111 | (found_mem64 || prefetch->bus_lower < 0x100000000ULL)) |
Stefan Roese | ede8909 | 2021-01-12 12:03:43 +0100 | [diff] [blame] | 112 | bar_res = prefetch; |
| 113 | else |
| 114 | bar_res = mem; |
Simon Glass | 37a3f94b | 2015-11-29 13:17:49 -0700 | [diff] [blame] | 115 | |
Phil Sutter | 8f90273 | 2021-03-03 01:57:35 +0100 | [diff] [blame] | 116 | debug("PCI Autoconfig: BAR %d, %s%s, size=0x%llx, ", |
Simon Glass | 37a3f94b | 2015-11-29 13:17:49 -0700 | [diff] [blame] | 117 | bar_nr, bar_res == prefetch ? "Prf" : "Mem", |
Phil Sutter | 8f90273 | 2021-03-03 01:57:35 +0100 | [diff] [blame] | 118 | found_mem64 ? "64" : "", |
Simon Glass | 37a3f94b | 2015-11-29 13:17:49 -0700 | [diff] [blame] | 119 | (unsigned long long)bar_size); |
| 120 | } |
| 121 | |
Stefan Roese | ede8909 | 2021-01-12 12:03:43 +0100 | [diff] [blame] | 122 | 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 Glass | 37a3f94b | 2015-11-29 13:17:49 -0700 | [diff] [blame] | 128 | /* 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 Roese | ede8909 | 2021-01-12 12:03:43 +0100 | [diff] [blame] | 155 | /* Configure the expansion ROM address */ |
Pali Rohár | 39a1d97 | 2021-10-07 14:50:57 +0200 | [diff] [blame] | 156 | if (header_type == PCI_HEADER_TYPE_NORMAL || |
| 157 | header_type == PCI_HEADER_TYPE_BRIDGE) { |
Stefan Roese | ede8909 | 2021-01-12 12:03:43 +0100 | [diff] [blame] | 158 | 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 Glass | 37a3f94b | 2015-11-29 13:17:49 -0700 | [diff] [blame] | 169 | } |
Stefan Roese | ede8909 | 2021-01-12 12:03:43 +0100 | [diff] [blame] | 170 | cmdstat |= PCI_COMMAND_MEMORY; |
| 171 | debug("\n"); |
Simon Glass | 37a3f94b | 2015-11-29 13:17:49 -0700 | [diff] [blame] | 172 | } |
| 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 Rini | 56af659 | 2022-11-16 13:10:33 -0500 | [diff] [blame] | 182 | CFG_SYS_PCI_CACHE_LINE_SIZE); |
Simon Glass | 37a3f94b | 2015-11-29 13:17:49 -0700 | [diff] [blame] | 183 | dm_pci_write_config8(dev, PCI_LATENCY_TIMER, 0x80); |
| 184 | } |
| 185 | |
Maciej W. Rozycki | 93008ab | 2021-11-20 23:03:30 +0000 | [diff] [blame] | 186 | /* |
| 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 | */ |
| 198 | static 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 | */ |
| 283 | static 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 Glass | 37a3f94b | 2015-11-29 13:17:49 -0700 | [diff] [blame] | 348 | void 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ár | 36e8fa0 | 2021-09-10 13:33:35 +0200 | [diff] [blame] | 354 | u8 io_32; |
Simon Glass | 4af3dc1 | 2016-01-18 20:19:16 -0700 | [diff] [blame] | 355 | struct udevice *ctlr = pci_get_controller(dev); |
| 356 | struct pci_controller *ctlr_hose = dev_get_uclass_priv(ctlr); |
Maciej W. Rozycki | 93008ab | 2021-11-20 23:03:30 +0000 | [diff] [blame] | 357 | int pcie_off; |
Simon Glass | 37a3f94b | 2015-11-29 13:17:49 -0700 | [diff] [blame] | 358 | |
| 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ár | e7a8e70 | 2021-11-25 11:30:58 +0100 | [diff] [blame] | 366 | dm_pci_read_config8(dev, PCI_IO_BASE, &io_32); |
Pali Rohár | 36e8fa0 | 2021-09-10 13:33:35 +0200 | [diff] [blame] | 367 | io_32 &= PCI_IO_RANGE_TYPE_MASK; |
Simon Glass | 37a3f94b | 2015-11-29 13:17:49 -0700 | [diff] [blame] | 368 | |
| 369 | /* Configure bus number registers */ |
| 370 | dm_pci_write_config8(dev, PCI_PRIMARY_BUS, |
Simon Glass | 75e534b | 2020-12-16 21:20:07 -0700 | [diff] [blame] | 371 | 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 Glass | 37a3f94b | 2015-11-29 13:17:49 -0700 | [diff] [blame] | 373 | 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ár | 36e8fa0 | 2021-09-10 13:33:35 +0200 | [diff] [blame] | 384 | ((pci_mem->bus_lower & 0xfff00000) >> 16) & |
| 385 | PCI_MEMORY_RANGE_MASK); |
Simon Glass | 37a3f94b | 2015-11-29 13:17:49 -0700 | [diff] [blame] | 386 | |
| 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ár | 36e8fa0 | 2021-09-10 13:33:35 +0200 | [diff] [blame] | 399 | (((pci_prefetch->bus_lower & 0xfff00000) >> 16) & |
| 400 | PCI_PREF_RANGE_MASK) | prefechable_64); |
Simon Glass | 37a3f94b | 2015-11-29 13:17:49 -0700 | [diff] [blame] | 401 | 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ár | 5a52671 | 2021-11-25 11:34:37 +0100 | [diff] [blame] | 412 | dm_pci_write_config16(dev, PCI_PREF_MEMORY_BASE, 0xfff0 | |
Pali Rohár | 36e8fa0 | 2021-09-10 13:33:35 +0200 | [diff] [blame] | 413 | prefechable_64); |
| 414 | dm_pci_write_config16(dev, PCI_PREF_MEMORY_LIMIT, 0x0 | |
| 415 | prefechable_64); |
Simon Glass | 37a3f94b | 2015-11-29 13:17:49 -0700 | [diff] [blame] | 416 | 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ár | 36e8fa0 | 2021-09-10 13:33:35 +0200 | [diff] [blame] | 427 | (((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 Glass | 37a3f94b | 2015-11-29 13:17:49 -0700 | [diff] [blame] | 431 | (pci_io->bus_lower & 0xffff0000) >> 16); |
| 432 | |
| 433 | cmdstat |= PCI_COMMAND_IO; |
Pali Rohár | 597acfb | 2021-11-25 11:32:43 +0100 | [diff] [blame] | 434 | } 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 Glass | 37a3f94b | 2015-11-29 13:17:49 -0700 | [diff] [blame] | 442 | } |
| 443 | |
Maciej W. Rozycki | 93008ab | 2021-11-20 23:03:30 +0000 | [diff] [blame] | 444 | /* 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 Glass | 37a3f94b | 2015-11-29 13:17:49 -0700 | [diff] [blame] | 449 | /* Enable memory and I/O accesses, enable bus master */ |
| 450 | dm_pci_write_config16(dev, PCI_COMMAND, cmdstat | PCI_COMMAND_MASTER); |
| 451 | } |
| 452 | |
| 453 | void 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 Glass | 4af3dc1 | 2016-01-18 20:19:16 -0700 | [diff] [blame] | 458 | struct udevice *ctlr = pci_get_controller(dev); |
| 459 | struct pci_controller *ctlr_hose = dev_get_uclass_priv(ctlr); |
Simon Glass | 37a3f94b | 2015-11-29 13:17:49 -0700 | [diff] [blame] | 460 | |
| 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 Glass | 75e534b | 2020-12-16 21:20:07 -0700 | [diff] [blame] | 466 | dm_pci_write_config8(dev, PCI_SUBORDINATE_BUS, sub_bus - dev_seq(ctlr)); |
Simon Glass | 37a3f94b | 2015-11-29 13:17:49 -0700 | [diff] [blame] | 467 | |
| 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ár | 36e8fa0 | 2021-09-10 13:33:35 +0200 | [diff] [blame] | 473 | ((pci_mem->bus_lower - 1) >> 16) & |
| 474 | PCI_MEMORY_RANGE_MASK); |
Simon Glass | 37a3f94b | 2015-11-29 13:17:49 -0700 | [diff] [blame] | 475 | } |
| 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ár | 36e8fa0 | 2021-09-10 13:33:35 +0200 | [diff] [blame] | 488 | (((pci_prefetch->bus_lower - 1) >> 16) & |
| 489 | PCI_PREF_RANGE_MASK) | prefechable_64); |
Simon Glass | 37a3f94b | 2015-11-29 13:17:49 -0700 | [diff] [blame] | 490 | 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ár | 36e8fa0 | 2021-09-10 13:33:35 +0200 | [diff] [blame] | 500 | 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 Glass | 37a3f94b | 2015-11-29 13:17:49 -0700 | [diff] [blame] | 506 | /* 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ár | 36e8fa0 | 2021-09-10 13:33:35 +0200 | [diff] [blame] | 510 | ((((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 Glass | 37a3f94b | 2015-11-29 13:17:49 -0700 | [diff] [blame] | 514 | ((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 | */ |
| 522 | int 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 Glass | 4af3dc1 | 2016-01-18 20:19:16 -0700 | [diff] [blame] | 529 | struct udevice *ctlr = pci_get_controller(dev); |
| 530 | struct pci_controller *ctlr_hose = dev_get_uclass_priv(ctlr); |
Simon Glass | be70610 | 2020-12-16 21:20:18 -0700 | [diff] [blame] | 531 | int ret; |
Simon Glass | 37a3f94b | 2015-11-29 13:17:49 -0700 | [diff] [blame] | 532 | |
Simon Glass | 37a3f94b | 2015-11-29 13:17:49 -0700 | [diff] [blame] | 533 | 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ár | 8597402 | 2021-10-07 14:50:59 +0200 | [diff] [blame] | 544 | dm_pciauto_setup_device(dev, pci_mem, pci_prefetch, pci_io); |
Simon Glass | 37a3f94b | 2015-11-29 13:17:49 -0700 | [diff] [blame] | 545 | |
Simon Glass | be70610 | 2020-12-16 21:20:18 -0700 | [diff] [blame] | 546 | ret = dm_pci_hose_probe_bus(dev); |
| 547 | if (ret < 0) |
| 548 | return log_msg_ret("probe", ret); |
| 549 | sub_bus = ret; |
Simon Glass | 37a3f94b | 2015-11-29 13:17:49 -0700 | [diff] [blame] | 550 | 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ár | 8597402 | 2021-10-07 14:50:59 +0200 | [diff] [blame] | 557 | dm_pciauto_setup_device(dev, pci_mem, pci_prefetch, pci_io); |
Simon Glass | 37a3f94b | 2015-11-29 13:17:49 -0700 | [diff] [blame] | 558 | |
| 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 Rini | d179823 | 2021-05-14 21:34:17 -0400 | [diff] [blame] | 570 | #if defined(CONFIG_ARCH_MPC834X) |
Simon Glass | 37a3f94b | 2015-11-29 13:17:49 -0700 | [diff] [blame] | 571 | 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 Roese | ede8909 | 2021-01-12 12:03:43 +0100 | [diff] [blame] | 580 | hose->pci_prefetch, hose->pci_io); |
Simon Glass | 37a3f94b | 2015-11-29 13:17:49 -0700 | [diff] [blame] | 581 | break; |
| 582 | #endif |
| 583 | |
Simon Glass | 37a3f94b | 2015-11-29 13:17:49 -0700 | [diff] [blame] | 584 | default: |
Pali Rohár | 8597402 | 2021-10-07 14:50:59 +0200 | [diff] [blame] | 585 | dm_pciauto_setup_device(dev, pci_mem, pci_prefetch, pci_io); |
Simon Glass | 37a3f94b | 2015-11-29 13:17:49 -0700 | [diff] [blame] | 586 | break; |
| 587 | } |
| 588 | |
| 589 | return sub_bus; |
| 590 | } |