Hou Zhiqiang | b89e3d9 | 2019-04-24 22:33:02 +0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ OR X11 |
| 2 | /* |
| 3 | * Copyright 2019 NXP |
| 4 | * |
| 5 | * PCIe DM U-Boot driver for Freescale PowerPC SoCs |
| 6 | * Author: Hou Zhiqiang <Zhiqiang.Hou@nxp.com> |
| 7 | */ |
| 8 | |
| 9 | #include <common.h> |
| 10 | #include <dm.h> |
| 11 | #include <malloc.h> |
| 12 | #include <mapmem.h> |
| 13 | #include <pci.h> |
| 14 | #include <asm/fsl_pci.h> |
| 15 | #include <asm/fsl_serdes.h> |
Simon Glass | 401d1c4 | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 16 | #include <asm/global_data.h> |
Hou Zhiqiang | b89e3d9 | 2019-04-24 22:33:02 +0800 | [diff] [blame] | 17 | #include <asm/io.h> |
Simon Glass | c05ed00 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 18 | #include <linux/delay.h> |
Hou Zhiqiang | b89e3d9 | 2019-04-24 22:33:02 +0800 | [diff] [blame] | 19 | #include "pcie_fsl.h" |
Simon Glass | 336d461 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 20 | #include <dm/device_compat.h> |
Hou Zhiqiang | b89e3d9 | 2019-04-24 22:33:02 +0800 | [diff] [blame] | 21 | |
| 22 | LIST_HEAD(fsl_pcie_list); |
| 23 | |
| 24 | static int fsl_pcie_link_up(struct fsl_pcie *pcie); |
| 25 | |
| 26 | static int fsl_pcie_addr_valid(struct fsl_pcie *pcie, pci_dev_t bdf) |
| 27 | { |
| 28 | struct udevice *bus = pcie->bus; |
| 29 | |
| 30 | if (!pcie->enabled) |
| 31 | return -ENXIO; |
| 32 | |
Simon Glass | 8b85dfc | 2020-12-16 21:20:07 -0700 | [diff] [blame] | 33 | if (PCI_BUS(bdf) < dev_seq(bus)) |
Hou Zhiqiang | b89e3d9 | 2019-04-24 22:33:02 +0800 | [diff] [blame] | 34 | return -EINVAL; |
| 35 | |
Simon Glass | 8b85dfc | 2020-12-16 21:20:07 -0700 | [diff] [blame] | 36 | if (PCI_BUS(bdf) > dev_seq(bus) && (!fsl_pcie_link_up(pcie) || pcie->mode)) |
Hou Zhiqiang | b89e3d9 | 2019-04-24 22:33:02 +0800 | [diff] [blame] | 37 | return -EINVAL; |
| 38 | |
Simon Glass | 8b85dfc | 2020-12-16 21:20:07 -0700 | [diff] [blame] | 39 | if (PCI_BUS(bdf) == dev_seq(bus) && (PCI_DEV(bdf) > 0 || PCI_FUNC(bdf) > 0)) |
Hou Zhiqiang | b89e3d9 | 2019-04-24 22:33:02 +0800 | [diff] [blame] | 40 | return -EINVAL; |
| 41 | |
Simon Glass | 8b85dfc | 2020-12-16 21:20:07 -0700 | [diff] [blame] | 42 | if (PCI_BUS(bdf) == (dev_seq(bus) + 1) && (PCI_DEV(bdf) > 0)) |
Hou Zhiqiang | b89e3d9 | 2019-04-24 22:33:02 +0800 | [diff] [blame] | 43 | return -EINVAL; |
| 44 | |
| 45 | return 0; |
| 46 | } |
| 47 | |
Simon Glass | c4e72c4 | 2020-01-27 08:49:37 -0700 | [diff] [blame] | 48 | static int fsl_pcie_read_config(const struct udevice *bus, pci_dev_t bdf, |
Hou Zhiqiang | b89e3d9 | 2019-04-24 22:33:02 +0800 | [diff] [blame] | 49 | uint offset, ulong *valuep, |
| 50 | enum pci_size_t size) |
| 51 | { |
| 52 | struct fsl_pcie *pcie = dev_get_priv(bus); |
| 53 | ccsr_fsl_pci_t *regs = pcie->regs; |
| 54 | u32 val; |
| 55 | |
| 56 | if (fsl_pcie_addr_valid(pcie, bdf)) { |
| 57 | *valuep = pci_get_ff(size); |
| 58 | return 0; |
| 59 | } |
| 60 | |
Pali Rohár | 0d734df | 2023-05-02 19:53:57 +0200 | [diff] [blame] | 61 | /* Skip Freescale PCIe controller's PEXCSRBAR register */ |
| 62 | if (PCI_BUS(bdf) - dev_seq(bus) == 0 && |
| 63 | PCI_DEV(bdf) == 0 && PCI_FUNC(bdf) == 0 && |
| 64 | (offset & ~3) == PCI_BASE_ADDRESS_0) { |
| 65 | *valuep = 0; |
| 66 | return 0; |
| 67 | } |
| 68 | |
Pali Rohár | f031f07 | 2021-11-26 11:42:47 +0100 | [diff] [blame] | 69 | val = PCI_CONF1_EXT_ADDRESS(PCI_BUS(bdf) - dev_seq(bus), |
| 70 | PCI_DEV(bdf), PCI_FUNC(bdf), |
| 71 | offset); |
Hou Zhiqiang | b89e3d9 | 2019-04-24 22:33:02 +0800 | [diff] [blame] | 72 | out_be32(®s->cfg_addr, val); |
| 73 | |
| 74 | sync(); |
| 75 | |
| 76 | switch (size) { |
| 77 | case PCI_SIZE_8: |
| 78 | *valuep = in_8((u8 *)®s->cfg_data + (offset & 3)); |
| 79 | break; |
| 80 | case PCI_SIZE_16: |
| 81 | *valuep = in_le16((u16 *)((u8 *)®s->cfg_data + |
| 82 | (offset & 2))); |
| 83 | break; |
| 84 | case PCI_SIZE_32: |
| 85 | *valuep = in_le32(®s->cfg_data); |
| 86 | break; |
| 87 | } |
| 88 | |
| 89 | return 0; |
| 90 | } |
| 91 | |
| 92 | static int fsl_pcie_write_config(struct udevice *bus, pci_dev_t bdf, |
| 93 | uint offset, ulong value, |
| 94 | enum pci_size_t size) |
| 95 | { |
| 96 | struct fsl_pcie *pcie = dev_get_priv(bus); |
| 97 | ccsr_fsl_pci_t *regs = pcie->regs; |
| 98 | u32 val; |
| 99 | u8 val_8; |
| 100 | u16 val_16; |
| 101 | u32 val_32; |
| 102 | |
| 103 | if (fsl_pcie_addr_valid(pcie, bdf)) |
| 104 | return 0; |
| 105 | |
Pali Rohár | 0d734df | 2023-05-02 19:53:57 +0200 | [diff] [blame] | 106 | /* Skip Freescale PCIe controller's PEXCSRBAR register */ |
| 107 | if (PCI_BUS(bdf) - dev_seq(bus) == 0 && |
| 108 | PCI_DEV(bdf) == 0 && PCI_FUNC(bdf) == 0 && |
| 109 | (offset & ~3) == PCI_BASE_ADDRESS_0) |
| 110 | return 0; |
| 111 | |
Pali Rohár | f031f07 | 2021-11-26 11:42:47 +0100 | [diff] [blame] | 112 | val = PCI_CONF1_EXT_ADDRESS(PCI_BUS(bdf) - dev_seq(bus), |
| 113 | PCI_DEV(bdf), PCI_FUNC(bdf), |
| 114 | offset); |
Hou Zhiqiang | b89e3d9 | 2019-04-24 22:33:02 +0800 | [diff] [blame] | 115 | out_be32(®s->cfg_addr, val); |
| 116 | |
| 117 | sync(); |
| 118 | |
| 119 | switch (size) { |
| 120 | case PCI_SIZE_8: |
| 121 | val_8 = value; |
| 122 | out_8((u8 *)®s->cfg_data + (offset & 3), val_8); |
| 123 | break; |
| 124 | case PCI_SIZE_16: |
| 125 | val_16 = value; |
| 126 | out_le16((u16 *)((u8 *)®s->cfg_data + (offset & 2)), val_16); |
| 127 | break; |
| 128 | case PCI_SIZE_32: |
| 129 | val_32 = value; |
| 130 | out_le32(®s->cfg_data, val_32); |
| 131 | break; |
| 132 | } |
| 133 | |
| 134 | return 0; |
| 135 | } |
| 136 | |
| 137 | static int fsl_pcie_hose_read_config(struct fsl_pcie *pcie, uint offset, |
| 138 | ulong *valuep, enum pci_size_t size) |
| 139 | { |
| 140 | int ret; |
| 141 | struct udevice *bus = pcie->bus; |
| 142 | |
Simon Glass | 8b85dfc | 2020-12-16 21:20:07 -0700 | [diff] [blame] | 143 | ret = fsl_pcie_read_config(bus, PCI_BDF(dev_seq(bus), 0, 0), |
Hou Zhiqiang | b89e3d9 | 2019-04-24 22:33:02 +0800 | [diff] [blame] | 144 | offset, valuep, size); |
| 145 | |
| 146 | return ret; |
| 147 | } |
| 148 | |
| 149 | static int fsl_pcie_hose_write_config(struct fsl_pcie *pcie, uint offset, |
| 150 | ulong value, enum pci_size_t size) |
| 151 | { |
| 152 | struct udevice *bus = pcie->bus; |
| 153 | |
Simon Glass | 8b85dfc | 2020-12-16 21:20:07 -0700 | [diff] [blame] | 154 | return fsl_pcie_write_config(bus, PCI_BDF(dev_seq(bus), 0, 0), |
Hou Zhiqiang | b89e3d9 | 2019-04-24 22:33:02 +0800 | [diff] [blame] | 155 | offset, value, size); |
| 156 | } |
| 157 | |
| 158 | static int fsl_pcie_hose_read_config_byte(struct fsl_pcie *pcie, uint offset, |
| 159 | u8 *valuep) |
| 160 | { |
| 161 | ulong val; |
| 162 | int ret; |
| 163 | |
| 164 | ret = fsl_pcie_hose_read_config(pcie, offset, &val, PCI_SIZE_8); |
| 165 | *valuep = val; |
| 166 | |
| 167 | return ret; |
| 168 | } |
| 169 | |
| 170 | static int fsl_pcie_hose_read_config_word(struct fsl_pcie *pcie, uint offset, |
| 171 | u16 *valuep) |
| 172 | { |
| 173 | ulong val; |
| 174 | int ret; |
| 175 | |
| 176 | ret = fsl_pcie_hose_read_config(pcie, offset, &val, PCI_SIZE_16); |
| 177 | *valuep = val; |
| 178 | |
| 179 | return ret; |
| 180 | } |
| 181 | |
| 182 | static int fsl_pcie_hose_read_config_dword(struct fsl_pcie *pcie, uint offset, |
| 183 | u32 *valuep) |
| 184 | { |
| 185 | ulong val; |
| 186 | int ret; |
| 187 | |
| 188 | ret = fsl_pcie_hose_read_config(pcie, offset, &val, PCI_SIZE_32); |
| 189 | *valuep = val; |
| 190 | |
| 191 | return ret; |
| 192 | } |
| 193 | |
| 194 | static int fsl_pcie_hose_write_config_byte(struct fsl_pcie *pcie, uint offset, |
| 195 | u8 value) |
| 196 | { |
| 197 | return fsl_pcie_hose_write_config(pcie, offset, value, PCI_SIZE_8); |
| 198 | } |
| 199 | |
| 200 | static int fsl_pcie_hose_write_config_word(struct fsl_pcie *pcie, uint offset, |
| 201 | u16 value) |
| 202 | { |
| 203 | return fsl_pcie_hose_write_config(pcie, offset, value, PCI_SIZE_16); |
| 204 | } |
| 205 | |
| 206 | static int fsl_pcie_hose_write_config_dword(struct fsl_pcie *pcie, uint offset, |
| 207 | u32 value) |
| 208 | { |
| 209 | return fsl_pcie_hose_write_config(pcie, offset, value, PCI_SIZE_32); |
| 210 | } |
| 211 | |
| 212 | static int fsl_pcie_link_up(struct fsl_pcie *pcie) |
| 213 | { |
| 214 | ccsr_fsl_pci_t *regs = pcie->regs; |
| 215 | u16 ltssm; |
| 216 | |
| 217 | if (pcie->block_rev >= PEX_IP_BLK_REV_3_0) { |
| 218 | ltssm = (in_be32(®s->pex_csr0) |
| 219 | & PEX_CSR0_LTSSM_MASK) >> PEX_CSR0_LTSSM_SHIFT; |
| 220 | return ltssm == LTSSM_L0_REV3; |
| 221 | } |
| 222 | |
| 223 | fsl_pcie_hose_read_config_word(pcie, PCI_LTSSM, <ssm); |
| 224 | |
| 225 | return ltssm == LTSSM_L0; |
| 226 | } |
| 227 | |
| 228 | static bool fsl_pcie_is_agent(struct fsl_pcie *pcie) |
| 229 | { |
| 230 | u8 header_type; |
| 231 | |
| 232 | fsl_pcie_hose_read_config_byte(pcie, PCI_HEADER_TYPE, &header_type); |
| 233 | |
| 234 | return (header_type & 0x7f) == PCI_HEADER_TYPE_NORMAL; |
| 235 | } |
| 236 | |
| 237 | static int fsl_pcie_setup_law(struct fsl_pcie *pcie) |
| 238 | { |
| 239 | struct pci_region *io, *mem, *pref; |
| 240 | |
| 241 | pci_get_regions(pcie->bus, &io, &mem, &pref); |
| 242 | |
| 243 | if (mem) |
| 244 | set_next_law(mem->phys_start, |
| 245 | law_size_bits(mem->size), |
| 246 | pcie->law_trgt_if); |
| 247 | |
| 248 | if (io) |
| 249 | set_next_law(io->phys_start, |
| 250 | law_size_bits(io->size), |
| 251 | pcie->law_trgt_if); |
| 252 | |
| 253 | return 0; |
| 254 | } |
| 255 | |
| 256 | static void fsl_pcie_config_ready(struct fsl_pcie *pcie) |
| 257 | { |
| 258 | ccsr_fsl_pci_t *regs = pcie->regs; |
| 259 | |
| 260 | if (pcie->block_rev >= PEX_IP_BLK_REV_3_0) { |
| 261 | setbits_be32(®s->config, FSL_PCIE_V3_CFG_RDY); |
| 262 | return; |
| 263 | } |
| 264 | |
| 265 | fsl_pcie_hose_write_config_byte(pcie, FSL_PCIE_CFG_RDY, 0x1); |
| 266 | } |
| 267 | |
| 268 | static int fsl_pcie_setup_outbound_win(struct fsl_pcie *pcie, int idx, |
| 269 | int type, u64 phys, u64 bus_addr, |
| 270 | pci_size_t size) |
| 271 | { |
| 272 | ccsr_fsl_pci_t *regs = pcie->regs; |
| 273 | pot_t *po = ®s->pot[idx]; |
| 274 | u32 war, sz; |
| 275 | |
| 276 | if (idx < 0) |
| 277 | return -EINVAL; |
| 278 | |
| 279 | out_be32(&po->powbar, phys >> 12); |
| 280 | out_be32(&po->potar, bus_addr >> 12); |
| 281 | #ifdef CONFIG_SYS_PCI_64BIT |
| 282 | out_be32(&po->potear, bus_addr >> 44); |
| 283 | #else |
| 284 | out_be32(&po->potear, 0); |
| 285 | #endif |
| 286 | |
| 287 | sz = (__ilog2_u64((u64)size) - 1); |
| 288 | war = POWAR_EN | sz; |
| 289 | |
| 290 | if (type == PCI_REGION_IO) |
| 291 | war |= POWAR_IO_READ | POWAR_IO_WRITE; |
| 292 | else |
| 293 | war |= POWAR_MEM_READ | POWAR_MEM_WRITE; |
| 294 | |
| 295 | out_be32(&po->powar, war); |
| 296 | |
| 297 | return 0; |
| 298 | } |
| 299 | |
| 300 | static int fsl_pcie_setup_inbound_win(struct fsl_pcie *pcie, int idx, |
| 301 | bool pf, u64 phys, u64 bus_addr, |
| 302 | pci_size_t size) |
| 303 | { |
| 304 | ccsr_fsl_pci_t *regs = pcie->regs; |
| 305 | pit_t *pi = ®s->pit[idx]; |
| 306 | u32 sz = (__ilog2_u64(size) - 1); |
| 307 | u32 flag = PIWAR_LOCAL; |
| 308 | |
| 309 | if (idx < 0) |
| 310 | return -EINVAL; |
| 311 | |
| 312 | out_be32(&pi->pitar, phys >> 12); |
| 313 | out_be32(&pi->piwbar, bus_addr >> 12); |
| 314 | |
| 315 | #ifdef CONFIG_SYS_PCI_64BIT |
| 316 | out_be32(&pi->piwbear, bus_addr >> 44); |
| 317 | #else |
| 318 | out_be32(&pi->piwbear, 0); |
| 319 | #endif |
| 320 | |
Hou Zhiqiang | adc983b | 2019-08-27 10:13:48 +0000 | [diff] [blame] | 321 | #ifdef CONFIG_SYS_FSL_ERRATUM_A005434 |
| 322 | flag = 0; |
| 323 | #endif |
Hou Zhiqiang | b89e3d9 | 2019-04-24 22:33:02 +0800 | [diff] [blame] | 324 | |
| 325 | flag |= PIWAR_EN | PIWAR_READ_SNOOP | PIWAR_WRITE_SNOOP; |
| 326 | if (pf) |
| 327 | flag |= PIWAR_PF; |
| 328 | out_be32(&pi->piwar, flag | sz); |
| 329 | |
| 330 | return 0; |
| 331 | } |
| 332 | |
| 333 | static int fsl_pcie_setup_outbound_wins(struct fsl_pcie *pcie) |
| 334 | { |
| 335 | struct pci_region *io, *mem, *pref; |
| 336 | int idx = 1; /* skip 0 */ |
| 337 | |
| 338 | pci_get_regions(pcie->bus, &io, &mem, &pref); |
| 339 | |
| 340 | if (io) |
| 341 | /* ATU : OUTBOUND : IO */ |
| 342 | fsl_pcie_setup_outbound_win(pcie, idx++, |
| 343 | PCI_REGION_IO, |
| 344 | io->phys_start, |
| 345 | io->bus_start, |
| 346 | io->size); |
| 347 | |
| 348 | if (mem) |
| 349 | /* ATU : OUTBOUND : MEM */ |
| 350 | fsl_pcie_setup_outbound_win(pcie, idx++, |
| 351 | PCI_REGION_MEM, |
| 352 | mem->phys_start, |
| 353 | mem->bus_start, |
| 354 | mem->size); |
| 355 | return 0; |
| 356 | } |
| 357 | |
| 358 | static int fsl_pcie_setup_inbound_wins(struct fsl_pcie *pcie) |
| 359 | { |
Tom Rini | ecc8d42 | 2022-11-16 13:10:33 -0500 | [diff] [blame] | 360 | phys_addr_t phys_start = CFG_SYS_PCI_MEMORY_PHYS; |
| 361 | pci_addr_t bus_start = CFG_SYS_PCI_MEMORY_BUS; |
Hou Zhiqiang | b89e3d9 | 2019-04-24 22:33:02 +0800 | [diff] [blame] | 362 | u64 sz = min((u64)gd->ram_size, (1ull << 32)); |
| 363 | pci_size_t pci_sz; |
| 364 | int idx; |
| 365 | |
| 366 | if (pcie->block_rev >= PEX_IP_BLK_REV_2_2) |
| 367 | idx = 2; |
| 368 | else |
| 369 | idx = 3; |
| 370 | |
| 371 | pci_sz = 1ull << __ilog2_u64(sz); |
| 372 | |
| 373 | dev_dbg(pcie->bus, "R0 bus_start: %llx phys_start: %llx size: %llx\n", |
| 374 | (u64)bus_start, (u64)phys_start, (u64)sz); |
| 375 | |
| 376 | /* if we aren't an exact power of two match, pci_sz is smaller |
| 377 | * round it up to the next power of two. We report the actual |
| 378 | * size to pci region tracking. |
| 379 | */ |
| 380 | if (pci_sz != sz) |
| 381 | sz = 2ull << __ilog2_u64(sz); |
| 382 | |
| 383 | fsl_pcie_setup_inbound_win(pcie, idx--, true, |
Tom Rini | ecc8d42 | 2022-11-16 13:10:33 -0500 | [diff] [blame] | 384 | CFG_SYS_PCI_MEMORY_PHYS, |
| 385 | CFG_SYS_PCI_MEMORY_BUS, sz); |
Hou Zhiqiang | b89e3d9 | 2019-04-24 22:33:02 +0800 | [diff] [blame] | 386 | #if defined(CONFIG_PHYS_64BIT) && defined(CONFIG_SYS_PCI_64BIT) |
| 387 | /* |
| 388 | * On 64-bit capable systems, set up a mapping for all of DRAM |
| 389 | * in high pci address space. |
| 390 | */ |
| 391 | pci_sz = 1ull << __ilog2_u64(gd->ram_size); |
| 392 | /* round up to the next largest power of two */ |
| 393 | if (gd->ram_size > pci_sz) |
| 394 | pci_sz = 1ull << (__ilog2_u64(gd->ram_size) + 1); |
| 395 | |
| 396 | dev_dbg(pcie->bus, "R64 bus_start: %llx phys_start: %llx size: %llx\n", |
Tom Rini | ecc8d42 | 2022-11-16 13:10:33 -0500 | [diff] [blame] | 397 | (u64)CFG_SYS_PCI64_MEMORY_BUS, |
| 398 | (u64)CFG_SYS_PCI_MEMORY_PHYS, (u64)pci_sz); |
Hou Zhiqiang | b89e3d9 | 2019-04-24 22:33:02 +0800 | [diff] [blame] | 399 | |
| 400 | fsl_pcie_setup_inbound_win(pcie, idx--, true, |
Tom Rini | ecc8d42 | 2022-11-16 13:10:33 -0500 | [diff] [blame] | 401 | CFG_SYS_PCI_MEMORY_PHYS, |
| 402 | CFG_SYS_PCI64_MEMORY_BUS, pci_sz); |
Hou Zhiqiang | b89e3d9 | 2019-04-24 22:33:02 +0800 | [diff] [blame] | 403 | #endif |
| 404 | |
| 405 | return 0; |
| 406 | } |
| 407 | |
| 408 | static int fsl_pcie_init_atmu(struct fsl_pcie *pcie) |
| 409 | { |
| 410 | fsl_pcie_setup_outbound_wins(pcie); |
| 411 | fsl_pcie_setup_inbound_wins(pcie); |
| 412 | |
| 413 | return 0; |
| 414 | } |
| 415 | |
Hou Zhiqiang | 0205beb | 2020-10-15 14:54:34 +0800 | [diff] [blame] | 416 | static void fsl_pcie_dbi_read_only_reg_write_enable(struct fsl_pcie *pcie, |
| 417 | bool enable) |
| 418 | { |
| 419 | u32 val; |
| 420 | |
| 421 | fsl_pcie_hose_read_config_dword(pcie, DBI_RO_WR_EN, &val); |
| 422 | if (enable) |
| 423 | val |= 1; |
| 424 | else |
| 425 | val &= ~1; |
| 426 | fsl_pcie_hose_write_config_dword(pcie, DBI_RO_WR_EN, val); |
| 427 | } |
| 428 | |
Hou Zhiqiang | b89e3d9 | 2019-04-24 22:33:02 +0800 | [diff] [blame] | 429 | static int fsl_pcie_init_port(struct fsl_pcie *pcie) |
| 430 | { |
| 431 | ccsr_fsl_pci_t *regs = pcie->regs; |
| 432 | u32 val_32; |
| 433 | u16 val_16; |
| 434 | |
| 435 | fsl_pcie_init_atmu(pcie); |
| 436 | |
Hou Zhiqiang | adc983b | 2019-08-27 10:13:48 +0000 | [diff] [blame] | 437 | #ifdef CONFIG_FSL_PCIE_DISABLE_ASPM |
| 438 | val_32 = 0; |
| 439 | fsl_pcie_hose_read_config_dword(pcie, PCI_LCR, &val_32); |
| 440 | val_32 &= ~0x03; |
| 441 | fsl_pcie_hose_write_config_dword(pcie, PCI_LCR, val_32); |
| 442 | udelay(1); |
| 443 | #endif |
Hou Zhiqiang | b89e3d9 | 2019-04-24 22:33:02 +0800 | [diff] [blame] | 444 | |
Hou Zhiqiang | adc983b | 2019-08-27 10:13:48 +0000 | [diff] [blame] | 445 | #ifdef CONFIG_FSL_PCIE_RESET |
| 446 | u16 ltssm; |
| 447 | int i; |
Hou Zhiqiang | b89e3d9 | 2019-04-24 22:33:02 +0800 | [diff] [blame] | 448 | |
Hou Zhiqiang | adc983b | 2019-08-27 10:13:48 +0000 | [diff] [blame] | 449 | if (pcie->block_rev >= PEX_IP_BLK_REV_3_0) { |
| 450 | /* assert PCIe reset */ |
| 451 | setbits_be32(®s->pdb_stat, 0x08000000); |
| 452 | (void)in_be32(®s->pdb_stat); |
| 453 | udelay(1000); |
| 454 | /* clear PCIe reset */ |
| 455 | clrbits_be32(®s->pdb_stat, 0x08000000); |
| 456 | asm("sync;isync"); |
| 457 | for (i = 0; i < 100 && !fsl_pcie_link_up(pcie); i++) |
| 458 | udelay(1000); |
| 459 | } else { |
| 460 | fsl_pcie_hose_read_config_word(pcie, PCI_LTSSM, <ssm); |
| 461 | if (ltssm == 1) { |
Hou Zhiqiang | b89e3d9 | 2019-04-24 22:33:02 +0800 | [diff] [blame] | 462 | /* assert PCIe reset */ |
| 463 | setbits_be32(®s->pdb_stat, 0x08000000); |
| 464 | (void)in_be32(®s->pdb_stat); |
Hou Zhiqiang | adc983b | 2019-08-27 10:13:48 +0000 | [diff] [blame] | 465 | udelay(100); |
Hou Zhiqiang | b89e3d9 | 2019-04-24 22:33:02 +0800 | [diff] [blame] | 466 | /* clear PCIe reset */ |
| 467 | clrbits_be32(®s->pdb_stat, 0x08000000); |
| 468 | asm("sync;isync"); |
Hou Zhiqiang | adc983b | 2019-08-27 10:13:48 +0000 | [diff] [blame] | 469 | for (i = 0; i < 100 && |
| 470 | !fsl_pcie_link_up(pcie); i++) |
Hou Zhiqiang | b89e3d9 | 2019-04-24 22:33:02 +0800 | [diff] [blame] | 471 | udelay(1000); |
Hou Zhiqiang | b89e3d9 | 2019-04-24 22:33:02 +0800 | [diff] [blame] | 472 | } |
| 473 | } |
Hou Zhiqiang | adc983b | 2019-08-27 10:13:48 +0000 | [diff] [blame] | 474 | #endif |
Hou Zhiqiang | b89e3d9 | 2019-04-24 22:33:02 +0800 | [diff] [blame] | 475 | |
Hou Zhiqiang | adc983b | 2019-08-27 10:13:48 +0000 | [diff] [blame] | 476 | #ifdef CONFIG_SYS_P4080_ERRATUM_PCIE_A003 |
| 477 | if (!fsl_pcie_link_up(pcie)) { |
Hou Zhiqiang | b89e3d9 | 2019-04-24 22:33:02 +0800 | [diff] [blame] | 478 | serdes_corenet_t *srds_regs; |
| 479 | |
Tom Rini | 6cc0454 | 2022-10-28 20:27:13 -0400 | [diff] [blame] | 480 | srds_regs = (void *)CFG_SYS_FSL_CORENET_SERDES_ADDR; |
Hou Zhiqiang | b89e3d9 | 2019-04-24 22:33:02 +0800 | [diff] [blame] | 481 | val_32 = in_be32(&srds_regs->srdspccr0); |
| 482 | |
| 483 | if ((val_32 >> 28) == 3) { |
| 484 | int i; |
| 485 | |
| 486 | out_be32(&srds_regs->srdspccr0, 2 << 28); |
| 487 | setbits_be32(®s->pdb_stat, 0x08000000); |
| 488 | in_be32(®s->pdb_stat); |
| 489 | udelay(100); |
| 490 | clrbits_be32(®s->pdb_stat, 0x08000000); |
| 491 | asm("sync;isync"); |
| 492 | for (i = 0; i < 100 && !fsl_pcie_link_up(pcie); i++) |
| 493 | udelay(1000); |
| 494 | } |
| 495 | } |
Hou Zhiqiang | adc983b | 2019-08-27 10:13:48 +0000 | [diff] [blame] | 496 | #endif |
Hou Zhiqiang | b89e3d9 | 2019-04-24 22:33:02 +0800 | [diff] [blame] | 497 | |
| 498 | /* |
| 499 | * The Read-Only Write Enable bit defaults to 1 instead of 0. |
| 500 | * Set to 0 to protect the read-only registers. |
| 501 | */ |
Hou Zhiqiang | adc983b | 2019-08-27 10:13:48 +0000 | [diff] [blame] | 502 | #ifdef CONFIG_SYS_FSL_ERRATUM_A007815 |
Hou Zhiqiang | 0205beb | 2020-10-15 14:54:34 +0800 | [diff] [blame] | 503 | fsl_pcie_dbi_read_only_reg_write_enable(pcie, false); |
Hou Zhiqiang | adc983b | 2019-08-27 10:13:48 +0000 | [diff] [blame] | 504 | #endif |
Hou Zhiqiang | b89e3d9 | 2019-04-24 22:33:02 +0800 | [diff] [blame] | 505 | |
| 506 | /* |
| 507 | * Enable All Error Interrupts except |
| 508 | * - Master abort (pci) |
| 509 | * - Master PERR (pci) |
| 510 | * - ICCA (PCIe) |
| 511 | */ |
| 512 | out_be32(®s->peer, ~0x20140); |
| 513 | |
| 514 | /* set URR, FER, NFER (but not CER) */ |
| 515 | fsl_pcie_hose_read_config_dword(pcie, PCI_DCR, &val_32); |
| 516 | val_32 |= 0xf000e; |
| 517 | fsl_pcie_hose_write_config_dword(pcie, PCI_DCR, val_32); |
| 518 | |
| 519 | /* Clear all error indications */ |
| 520 | out_be32(®s->pme_msg_det, 0xffffffff); |
| 521 | out_be32(®s->pme_msg_int_en, 0xffffffff); |
| 522 | out_be32(®s->pedr, 0xffffffff); |
| 523 | |
| 524 | fsl_pcie_hose_read_config_word(pcie, PCI_DSR, &val_16); |
| 525 | if (val_16) |
| 526 | fsl_pcie_hose_write_config_word(pcie, PCI_DSR, 0xffff); |
| 527 | |
| 528 | fsl_pcie_hose_read_config_word(pcie, PCI_SEC_STATUS, &val_16); |
| 529 | if (val_16) |
| 530 | fsl_pcie_hose_write_config_word(pcie, PCI_SEC_STATUS, 0xffff); |
| 531 | |
| 532 | return 0; |
| 533 | } |
| 534 | |
| 535 | static int fsl_pcie_fixup_classcode(struct fsl_pcie *pcie) |
| 536 | { |
Hou Zhiqiang | d18d06a | 2019-08-27 10:13:51 +0000 | [diff] [blame] | 537 | u32 classcode_reg; |
Hou Zhiqiang | b89e3d9 | 2019-04-24 22:33:02 +0800 | [diff] [blame] | 538 | u32 val; |
| 539 | |
Hou Zhiqiang | d18d06a | 2019-08-27 10:13:51 +0000 | [diff] [blame] | 540 | if (pcie->block_rev >= PEX_IP_BLK_REV_3_0) { |
| 541 | classcode_reg = PCI_CLASS_REVISION; |
Hou Zhiqiang | 0205beb | 2020-10-15 14:54:34 +0800 | [diff] [blame] | 542 | fsl_pcie_dbi_read_only_reg_write_enable(pcie, true); |
Hou Zhiqiang | d18d06a | 2019-08-27 10:13:51 +0000 | [diff] [blame] | 543 | } else { |
| 544 | classcode_reg = CSR_CLASSCODE; |
| 545 | } |
| 546 | |
| 547 | fsl_pcie_hose_read_config_dword(pcie, classcode_reg, &val); |
Hou Zhiqiang | b89e3d9 | 2019-04-24 22:33:02 +0800 | [diff] [blame] | 548 | val &= 0xff; |
Pali Rohár | d7b9040 | 2022-02-18 13:18:40 +0100 | [diff] [blame] | 549 | val |= PCI_CLASS_BRIDGE_PCI_NORMAL << 8; |
Hou Zhiqiang | d18d06a | 2019-08-27 10:13:51 +0000 | [diff] [blame] | 550 | fsl_pcie_hose_write_config_dword(pcie, classcode_reg, val); |
| 551 | |
| 552 | if (pcie->block_rev >= PEX_IP_BLK_REV_3_0) |
Hou Zhiqiang | 0205beb | 2020-10-15 14:54:34 +0800 | [diff] [blame] | 553 | fsl_pcie_dbi_read_only_reg_write_enable(pcie, false); |
Hou Zhiqiang | b89e3d9 | 2019-04-24 22:33:02 +0800 | [diff] [blame] | 554 | |
| 555 | return 0; |
| 556 | } |
| 557 | |
| 558 | static int fsl_pcie_init_rc(struct fsl_pcie *pcie) |
| 559 | { |
| 560 | return fsl_pcie_fixup_classcode(pcie); |
| 561 | } |
| 562 | |
| 563 | static int fsl_pcie_init_ep(struct fsl_pcie *pcie) |
| 564 | { |
| 565 | fsl_pcie_config_ready(pcie); |
| 566 | |
| 567 | return 0; |
| 568 | } |
| 569 | |
| 570 | static int fsl_pcie_probe(struct udevice *dev) |
| 571 | { |
| 572 | struct fsl_pcie *pcie = dev_get_priv(dev); |
| 573 | ccsr_fsl_pci_t *regs = pcie->regs; |
| 574 | u16 val_16; |
| 575 | |
| 576 | pcie->bus = dev; |
| 577 | pcie->block_rev = in_be32(®s->block_rev1); |
| 578 | |
| 579 | list_add(&pcie->list, &fsl_pcie_list); |
| 580 | pcie->enabled = is_serdes_configured(PCIE1 + pcie->idx); |
| 581 | if (!pcie->enabled) { |
| 582 | printf("PCIe%d: %s disabled\n", pcie->idx, dev->name); |
| 583 | return 0; |
| 584 | } |
| 585 | |
| 586 | fsl_pcie_setup_law(pcie); |
| 587 | |
| 588 | pcie->mode = fsl_pcie_is_agent(pcie); |
| 589 | |
| 590 | fsl_pcie_init_port(pcie); |
| 591 | |
| 592 | printf("PCIe%d: %s ", pcie->idx, dev->name); |
| 593 | |
| 594 | if (pcie->mode) { |
| 595 | printf("Endpoint"); |
| 596 | fsl_pcie_init_ep(pcie); |
| 597 | } else { |
| 598 | printf("Root Complex"); |
| 599 | fsl_pcie_init_rc(pcie); |
| 600 | } |
| 601 | |
| 602 | if (!fsl_pcie_link_up(pcie)) { |
| 603 | printf(": %s\n", pcie->mode ? "undetermined link" : "no link"); |
| 604 | return 0; |
| 605 | } |
| 606 | |
| 607 | fsl_pcie_hose_read_config_word(pcie, PCI_LSR, &val_16); |
| 608 | printf(": x%d gen%d\n", (val_16 & 0x3f0) >> 4, (val_16 & 0xf)); |
| 609 | |
| 610 | return 0; |
| 611 | } |
| 612 | |
Simon Glass | d1998a9 | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 613 | static int fsl_pcie_of_to_plat(struct udevice *dev) |
Hou Zhiqiang | b89e3d9 | 2019-04-24 22:33:02 +0800 | [diff] [blame] | 614 | { |
| 615 | struct fsl_pcie *pcie = dev_get_priv(dev); |
Hou Zhiqiang | fbcb2ff | 2019-08-27 10:13:54 +0000 | [diff] [blame] | 616 | struct fsl_pcie_data *info; |
Hou Zhiqiang | b89e3d9 | 2019-04-24 22:33:02 +0800 | [diff] [blame] | 617 | int ret; |
| 618 | |
| 619 | pcie->regs = dev_remap_addr(dev); |
| 620 | if (!pcie->regs) { |
| 621 | pr_err("\"reg\" resource not found\n"); |
| 622 | return -EINVAL; |
| 623 | } |
| 624 | |
| 625 | ret = dev_read_u32(dev, "law_trgt_if", &pcie->law_trgt_if); |
| 626 | if (ret < 0) { |
| 627 | pr_err("\"law_trgt_if\" not found\n"); |
| 628 | return ret; |
| 629 | } |
| 630 | |
Hou Zhiqiang | fbcb2ff | 2019-08-27 10:13:54 +0000 | [diff] [blame] | 631 | info = (struct fsl_pcie_data *)dev_get_driver_data(dev); |
| 632 | pcie->info = info; |
| 633 | pcie->idx = abs((u32)(dev_read_addr(dev) & info->block_offset_mask) - |
| 634 | info->block_offset) / info->stride; |
Hou Zhiqiang | b89e3d9 | 2019-04-24 22:33:02 +0800 | [diff] [blame] | 635 | |
| 636 | return 0; |
| 637 | } |
| 638 | |
| 639 | static const struct dm_pci_ops fsl_pcie_ops = { |
| 640 | .read_config = fsl_pcie_read_config, |
| 641 | .write_config = fsl_pcie_write_config, |
| 642 | }; |
| 643 | |
Hou Zhiqiang | ba82736 | 2019-08-27 11:04:01 +0000 | [diff] [blame] | 644 | static struct fsl_pcie_data p1_p2_data = { |
| 645 | .block_offset = 0xa000, |
| 646 | .block_offset_mask = 0xffff, |
| 647 | .stride = 0x1000, |
| 648 | }; |
| 649 | |
Hou Zhiqiang | 1a92802 | 2019-08-27 11:04:25 +0000 | [diff] [blame] | 650 | static struct fsl_pcie_data p2041_data = { |
| 651 | .block_offset = 0x200000, |
| 652 | .block_offset_mask = 0x3fffff, |
| 653 | .stride = 0x1000, |
| 654 | }; |
| 655 | |
Hou Zhiqiang | fbcb2ff | 2019-08-27 10:13:54 +0000 | [diff] [blame] | 656 | static struct fsl_pcie_data t2080_data = { |
| 657 | .block_offset = 0x240000, |
| 658 | .block_offset_mask = 0x3fffff, |
| 659 | .stride = 0x10000, |
| 660 | }; |
| 661 | |
Hou Zhiqiang | b89e3d9 | 2019-04-24 22:33:02 +0800 | [diff] [blame] | 662 | static const struct udevice_id fsl_pcie_ids[] = { |
Pali Rohár | 95f8dfe | 2022-04-14 22:52:03 +0200 | [diff] [blame] | 663 | { .compatible = "fsl,mpc8548-pcie", .data = (ulong)&p1_p2_data }, |
Hou Zhiqiang | ba82736 | 2019-08-27 11:04:01 +0000 | [diff] [blame] | 664 | { .compatible = "fsl,pcie-p1_p2", .data = (ulong)&p1_p2_data }, |
Hou Zhiqiang | 1a92802 | 2019-08-27 11:04:25 +0000 | [diff] [blame] | 665 | { .compatible = "fsl,pcie-p2041", .data = (ulong)&p2041_data }, |
Hou Zhiqiang | 096d5f8 | 2019-08-27 11:04:39 +0000 | [diff] [blame] | 666 | { .compatible = "fsl,pcie-p3041", .data = (ulong)&p2041_data }, |
Hou Zhiqiang | 7b7e4e1 | 2019-08-27 11:04:52 +0000 | [diff] [blame] | 667 | { .compatible = "fsl,pcie-p4080", .data = (ulong)&p2041_data }, |
Hou Zhiqiang | 5274459 | 2019-08-27 11:05:02 +0000 | [diff] [blame] | 668 | { .compatible = "fsl,pcie-p5040", .data = (ulong)&p2041_data }, |
Hou Zhiqiang | a8c79f6 | 2019-08-27 11:03:24 +0000 | [diff] [blame] | 669 | { .compatible = "fsl,pcie-t102x", .data = (ulong)&t2080_data }, |
Hou Zhiqiang | 4392ddb | 2019-08-27 11:03:44 +0000 | [diff] [blame] | 670 | { .compatible = "fsl,pcie-t104x", .data = (ulong)&t2080_data }, |
Hou Zhiqiang | fbcb2ff | 2019-08-27 10:13:54 +0000 | [diff] [blame] | 671 | { .compatible = "fsl,pcie-t2080", .data = (ulong)&t2080_data }, |
Hou Zhiqiang | 9acc038 | 2019-08-27 11:03:06 +0000 | [diff] [blame] | 672 | { .compatible = "fsl,pcie-t4240", .data = (ulong)&t2080_data }, |
Hou Zhiqiang | b89e3d9 | 2019-04-24 22:33:02 +0800 | [diff] [blame] | 673 | { } |
| 674 | }; |
| 675 | |
| 676 | U_BOOT_DRIVER(fsl_pcie) = { |
| 677 | .name = "fsl_pcie", |
| 678 | .id = UCLASS_PCI, |
| 679 | .of_match = fsl_pcie_ids, |
| 680 | .ops = &fsl_pcie_ops, |
Simon Glass | d1998a9 | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 681 | .of_to_plat = fsl_pcie_of_to_plat, |
Hou Zhiqiang | b89e3d9 | 2019-04-24 22:33:02 +0800 | [diff] [blame] | 682 | .probe = fsl_pcie_probe, |
Simon Glass | 41575d8 | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 683 | .priv_auto = sizeof(struct fsl_pcie), |
Hou Zhiqiang | b89e3d9 | 2019-04-24 22:33:02 +0800 | [diff] [blame] | 684 | }; |