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