1 // SPDX-License-Identifier: GPL-2.0+ OR X11
5 * PCIe DM U-Boot driver for Freescale PowerPC SoCs
6 * Author: Hou Zhiqiang <Zhiqiang.Hou@nxp.com>
14 #include <asm/fsl_pci.h>
15 #include <asm/fsl_serdes.h>
18 #include <dm/device_compat.h>
20 LIST_HEAD(fsl_pcie_list);
22 static int fsl_pcie_link_up(struct fsl_pcie *pcie);
24 static int fsl_pcie_addr_valid(struct fsl_pcie *pcie, pci_dev_t bdf)
26 struct udevice *bus = pcie->bus;
31 if (PCI_BUS(bdf) < bus->seq)
34 if (PCI_BUS(bdf) > bus->seq && (!fsl_pcie_link_up(pcie) || pcie->mode))
37 if (PCI_BUS(bdf) == bus->seq && (PCI_DEV(bdf) > 0 || PCI_FUNC(bdf) > 0))
40 if (PCI_BUS(bdf) == (bus->seq + 1) && (PCI_DEV(bdf) > 0))
46 static int fsl_pcie_read_config(const struct udevice *bus, pci_dev_t bdf,
47 uint offset, ulong *valuep,
50 struct fsl_pcie *pcie = dev_get_priv(bus);
51 ccsr_fsl_pci_t *regs = pcie->regs;
54 if (fsl_pcie_addr_valid(pcie, bdf)) {
55 *valuep = pci_get_ff(size);
59 bdf = bdf - PCI_BDF(bus->seq, 0, 0);
60 val = bdf | (offset & 0xfc) | ((offset & 0xf00) << 16) | 0x80000000;
61 out_be32(®s->cfg_addr, val);
67 *valuep = in_8((u8 *)®s->cfg_data + (offset & 3));
70 *valuep = in_le16((u16 *)((u8 *)®s->cfg_data +
74 *valuep = in_le32(®s->cfg_data);
81 static int fsl_pcie_write_config(struct udevice *bus, pci_dev_t bdf,
82 uint offset, ulong value,
85 struct fsl_pcie *pcie = dev_get_priv(bus);
86 ccsr_fsl_pci_t *regs = pcie->regs;
92 if (fsl_pcie_addr_valid(pcie, bdf))
95 bdf = bdf - PCI_BDF(bus->seq, 0, 0);
96 val = bdf | (offset & 0xfc) | ((offset & 0xf00) << 16) | 0x80000000;
97 out_be32(®s->cfg_addr, val);
104 out_8((u8 *)®s->cfg_data + (offset & 3), val_8);
108 out_le16((u16 *)((u8 *)®s->cfg_data + (offset & 2)), val_16);
112 out_le32(®s->cfg_data, val_32);
119 static int fsl_pcie_hose_read_config(struct fsl_pcie *pcie, uint offset,
120 ulong *valuep, enum pci_size_t size)
123 struct udevice *bus = pcie->bus;
125 ret = fsl_pcie_read_config(bus, PCI_BDF(bus->seq, 0, 0),
126 offset, valuep, size);
131 static int fsl_pcie_hose_write_config(struct fsl_pcie *pcie, uint offset,
132 ulong value, enum pci_size_t size)
134 struct udevice *bus = pcie->bus;
136 return fsl_pcie_write_config(bus, PCI_BDF(bus->seq, 0, 0),
137 offset, value, size);
140 static int fsl_pcie_hose_read_config_byte(struct fsl_pcie *pcie, uint offset,
146 ret = fsl_pcie_hose_read_config(pcie, offset, &val, PCI_SIZE_8);
152 static int fsl_pcie_hose_read_config_word(struct fsl_pcie *pcie, uint offset,
158 ret = fsl_pcie_hose_read_config(pcie, offset, &val, PCI_SIZE_16);
164 static int fsl_pcie_hose_read_config_dword(struct fsl_pcie *pcie, uint offset,
170 ret = fsl_pcie_hose_read_config(pcie, offset, &val, PCI_SIZE_32);
176 static int fsl_pcie_hose_write_config_byte(struct fsl_pcie *pcie, uint offset,
179 return fsl_pcie_hose_write_config(pcie, offset, value, PCI_SIZE_8);
182 static int fsl_pcie_hose_write_config_word(struct fsl_pcie *pcie, uint offset,
185 return fsl_pcie_hose_write_config(pcie, offset, value, PCI_SIZE_16);
188 static int fsl_pcie_hose_write_config_dword(struct fsl_pcie *pcie, uint offset,
191 return fsl_pcie_hose_write_config(pcie, offset, value, PCI_SIZE_32);
194 static int fsl_pcie_link_up(struct fsl_pcie *pcie)
196 ccsr_fsl_pci_t *regs = pcie->regs;
199 if (pcie->block_rev >= PEX_IP_BLK_REV_3_0) {
200 ltssm = (in_be32(®s->pex_csr0)
201 & PEX_CSR0_LTSSM_MASK) >> PEX_CSR0_LTSSM_SHIFT;
202 return ltssm == LTSSM_L0_REV3;
205 fsl_pcie_hose_read_config_word(pcie, PCI_LTSSM, <ssm);
207 return ltssm == LTSSM_L0;
210 static bool fsl_pcie_is_agent(struct fsl_pcie *pcie)
214 fsl_pcie_hose_read_config_byte(pcie, PCI_HEADER_TYPE, &header_type);
216 return (header_type & 0x7f) == PCI_HEADER_TYPE_NORMAL;
219 static int fsl_pcie_setup_law(struct fsl_pcie *pcie)
221 struct pci_region *io, *mem, *pref;
223 pci_get_regions(pcie->bus, &io, &mem, &pref);
226 set_next_law(mem->phys_start,
227 law_size_bits(mem->size),
231 set_next_law(io->phys_start,
232 law_size_bits(io->size),
238 static void fsl_pcie_config_ready(struct fsl_pcie *pcie)
240 ccsr_fsl_pci_t *regs = pcie->regs;
242 if (pcie->block_rev >= PEX_IP_BLK_REV_3_0) {
243 setbits_be32(®s->config, FSL_PCIE_V3_CFG_RDY);
247 fsl_pcie_hose_write_config_byte(pcie, FSL_PCIE_CFG_RDY, 0x1);
250 static int fsl_pcie_setup_outbound_win(struct fsl_pcie *pcie, int idx,
251 int type, u64 phys, u64 bus_addr,
254 ccsr_fsl_pci_t *regs = pcie->regs;
255 pot_t *po = ®s->pot[idx];
261 out_be32(&po->powbar, phys >> 12);
262 out_be32(&po->potar, bus_addr >> 12);
263 #ifdef CONFIG_SYS_PCI_64BIT
264 out_be32(&po->potear, bus_addr >> 44);
266 out_be32(&po->potear, 0);
269 sz = (__ilog2_u64((u64)size) - 1);
272 if (type == PCI_REGION_IO)
273 war |= POWAR_IO_READ | POWAR_IO_WRITE;
275 war |= POWAR_MEM_READ | POWAR_MEM_WRITE;
277 out_be32(&po->powar, war);
282 static int fsl_pcie_setup_inbound_win(struct fsl_pcie *pcie, int idx,
283 bool pf, u64 phys, u64 bus_addr,
286 ccsr_fsl_pci_t *regs = pcie->regs;
287 pit_t *pi = ®s->pit[idx];
288 u32 sz = (__ilog2_u64(size) - 1);
289 u32 flag = PIWAR_LOCAL;
294 out_be32(&pi->pitar, phys >> 12);
295 out_be32(&pi->piwbar, bus_addr >> 12);
297 #ifdef CONFIG_SYS_PCI_64BIT
298 out_be32(&pi->piwbear, bus_addr >> 44);
300 out_be32(&pi->piwbear, 0);
303 #ifdef CONFIG_SYS_FSL_ERRATUM_A005434
307 flag |= PIWAR_EN | PIWAR_READ_SNOOP | PIWAR_WRITE_SNOOP;
310 out_be32(&pi->piwar, flag | sz);
315 static int fsl_pcie_setup_outbound_wins(struct fsl_pcie *pcie)
317 struct pci_region *io, *mem, *pref;
318 int idx = 1; /* skip 0 */
320 pci_get_regions(pcie->bus, &io, &mem, &pref);
323 /* ATU : OUTBOUND : IO */
324 fsl_pcie_setup_outbound_win(pcie, idx++,
331 /* ATU : OUTBOUND : MEM */
332 fsl_pcie_setup_outbound_win(pcie, idx++,
340 static int fsl_pcie_setup_inbound_wins(struct fsl_pcie *pcie)
342 phys_addr_t phys_start = CONFIG_SYS_PCI_MEMORY_PHYS;
343 pci_addr_t bus_start = CONFIG_SYS_PCI_MEMORY_BUS;
344 u64 sz = min((u64)gd->ram_size, (1ull << 32));
348 if (pcie->block_rev >= PEX_IP_BLK_REV_2_2)
353 pci_sz = 1ull << __ilog2_u64(sz);
355 dev_dbg(pcie->bus, "R0 bus_start: %llx phys_start: %llx size: %llx\n",
356 (u64)bus_start, (u64)phys_start, (u64)sz);
358 /* if we aren't an exact power of two match, pci_sz is smaller
359 * round it up to the next power of two. We report the actual
360 * size to pci region tracking.
363 sz = 2ull << __ilog2_u64(sz);
365 fsl_pcie_setup_inbound_win(pcie, idx--, true,
366 CONFIG_SYS_PCI_MEMORY_PHYS,
367 CONFIG_SYS_PCI_MEMORY_BUS, sz);
368 #if defined(CONFIG_PHYS_64BIT) && defined(CONFIG_SYS_PCI_64BIT)
370 * On 64-bit capable systems, set up a mapping for all of DRAM
371 * in high pci address space.
373 pci_sz = 1ull << __ilog2_u64(gd->ram_size);
374 /* round up to the next largest power of two */
375 if (gd->ram_size > pci_sz)
376 pci_sz = 1ull << (__ilog2_u64(gd->ram_size) + 1);
378 dev_dbg(pcie->bus, "R64 bus_start: %llx phys_start: %llx size: %llx\n",
379 (u64)CONFIG_SYS_PCI64_MEMORY_BUS,
380 (u64)CONFIG_SYS_PCI_MEMORY_PHYS, (u64)pci_sz);
382 fsl_pcie_setup_inbound_win(pcie, idx--, true,
383 CONFIG_SYS_PCI_MEMORY_PHYS,
384 CONFIG_SYS_PCI64_MEMORY_BUS, pci_sz);
390 static int fsl_pcie_init_atmu(struct fsl_pcie *pcie)
392 fsl_pcie_setup_outbound_wins(pcie);
393 fsl_pcie_setup_inbound_wins(pcie);
398 static int fsl_pcie_init_port(struct fsl_pcie *pcie)
400 ccsr_fsl_pci_t *regs = pcie->regs;
404 fsl_pcie_init_atmu(pcie);
406 #ifdef CONFIG_FSL_PCIE_DISABLE_ASPM
408 fsl_pcie_hose_read_config_dword(pcie, PCI_LCR, &val_32);
410 fsl_pcie_hose_write_config_dword(pcie, PCI_LCR, val_32);
414 #ifdef CONFIG_FSL_PCIE_RESET
418 if (pcie->block_rev >= PEX_IP_BLK_REV_3_0) {
419 /* assert PCIe reset */
420 setbits_be32(®s->pdb_stat, 0x08000000);
421 (void)in_be32(®s->pdb_stat);
423 /* clear PCIe reset */
424 clrbits_be32(®s->pdb_stat, 0x08000000);
426 for (i = 0; i < 100 && !fsl_pcie_link_up(pcie); i++)
429 fsl_pcie_hose_read_config_word(pcie, PCI_LTSSM, <ssm);
431 /* assert PCIe reset */
432 setbits_be32(®s->pdb_stat, 0x08000000);
433 (void)in_be32(®s->pdb_stat);
435 /* clear PCIe reset */
436 clrbits_be32(®s->pdb_stat, 0x08000000);
438 for (i = 0; i < 100 &&
439 !fsl_pcie_link_up(pcie); i++)
445 #ifdef CONFIG_SYS_P4080_ERRATUM_PCIE_A003
446 if (!fsl_pcie_link_up(pcie)) {
447 serdes_corenet_t *srds_regs;
449 srds_regs = (void *)CONFIG_SYS_FSL_CORENET_SERDES_ADDR;
450 val_32 = in_be32(&srds_regs->srdspccr0);
452 if ((val_32 >> 28) == 3) {
455 out_be32(&srds_regs->srdspccr0, 2 << 28);
456 setbits_be32(®s->pdb_stat, 0x08000000);
457 in_be32(®s->pdb_stat);
459 clrbits_be32(®s->pdb_stat, 0x08000000);
461 for (i = 0; i < 100 && !fsl_pcie_link_up(pcie); i++)
468 * The Read-Only Write Enable bit defaults to 1 instead of 0.
469 * Set to 0 to protect the read-only registers.
471 #ifdef CONFIG_SYS_FSL_ERRATUM_A007815
472 clrbits_be32(®s->dbi_ro_wr_en, 0x01);
476 * Enable All Error Interrupts except
477 * - Master abort (pci)
478 * - Master PERR (pci)
481 out_be32(®s->peer, ~0x20140);
483 /* set URR, FER, NFER (but not CER) */
484 fsl_pcie_hose_read_config_dword(pcie, PCI_DCR, &val_32);
486 fsl_pcie_hose_write_config_dword(pcie, PCI_DCR, val_32);
488 /* Clear all error indications */
489 out_be32(®s->pme_msg_det, 0xffffffff);
490 out_be32(®s->pme_msg_int_en, 0xffffffff);
491 out_be32(®s->pedr, 0xffffffff);
493 fsl_pcie_hose_read_config_word(pcie, PCI_DSR, &val_16);
495 fsl_pcie_hose_write_config_word(pcie, PCI_DSR, 0xffff);
497 fsl_pcie_hose_read_config_word(pcie, PCI_SEC_STATUS, &val_16);
499 fsl_pcie_hose_write_config_word(pcie, PCI_SEC_STATUS, 0xffff);
504 static int fsl_pcie_fixup_classcode(struct fsl_pcie *pcie)
506 ccsr_fsl_pci_t *regs = pcie->regs;
510 if (pcie->block_rev >= PEX_IP_BLK_REV_3_0) {
511 classcode_reg = PCI_CLASS_REVISION;
512 setbits_be32(®s->dbi_ro_wr_en, 0x01);
514 classcode_reg = CSR_CLASSCODE;
517 fsl_pcie_hose_read_config_dword(pcie, classcode_reg, &val);
519 val |= PCI_CLASS_BRIDGE_PCI << 16;
520 fsl_pcie_hose_write_config_dword(pcie, classcode_reg, val);
522 if (pcie->block_rev >= PEX_IP_BLK_REV_3_0)
523 clrbits_be32(®s->dbi_ro_wr_en, 0x01);
528 static int fsl_pcie_init_rc(struct fsl_pcie *pcie)
530 return fsl_pcie_fixup_classcode(pcie);
533 static int fsl_pcie_init_ep(struct fsl_pcie *pcie)
535 fsl_pcie_config_ready(pcie);
540 static int fsl_pcie_probe(struct udevice *dev)
542 struct fsl_pcie *pcie = dev_get_priv(dev);
543 ccsr_fsl_pci_t *regs = pcie->regs;
547 pcie->block_rev = in_be32(®s->block_rev1);
549 list_add(&pcie->list, &fsl_pcie_list);
550 pcie->enabled = is_serdes_configured(PCIE1 + pcie->idx);
551 if (!pcie->enabled) {
552 printf("PCIe%d: %s disabled\n", pcie->idx, dev->name);
556 fsl_pcie_setup_law(pcie);
558 pcie->mode = fsl_pcie_is_agent(pcie);
560 fsl_pcie_init_port(pcie);
562 printf("PCIe%d: %s ", pcie->idx, dev->name);
566 fsl_pcie_init_ep(pcie);
568 printf("Root Complex");
569 fsl_pcie_init_rc(pcie);
572 if (!fsl_pcie_link_up(pcie)) {
573 printf(": %s\n", pcie->mode ? "undetermined link" : "no link");
577 fsl_pcie_hose_read_config_word(pcie, PCI_LSR, &val_16);
578 printf(": x%d gen%d\n", (val_16 & 0x3f0) >> 4, (val_16 & 0xf));
583 static int fsl_pcie_ofdata_to_platdata(struct udevice *dev)
585 struct fsl_pcie *pcie = dev_get_priv(dev);
586 struct fsl_pcie_data *info;
589 pcie->regs = dev_remap_addr(dev);
591 pr_err("\"reg\" resource not found\n");
595 ret = dev_read_u32(dev, "law_trgt_if", &pcie->law_trgt_if);
597 pr_err("\"law_trgt_if\" not found\n");
601 info = (struct fsl_pcie_data *)dev_get_driver_data(dev);
603 pcie->idx = abs((u32)(dev_read_addr(dev) & info->block_offset_mask) -
604 info->block_offset) / info->stride;
609 static const struct dm_pci_ops fsl_pcie_ops = {
610 .read_config = fsl_pcie_read_config,
611 .write_config = fsl_pcie_write_config,
614 static struct fsl_pcie_data p1_p2_data = {
615 .block_offset = 0xa000,
616 .block_offset_mask = 0xffff,
620 static struct fsl_pcie_data p2041_data = {
621 .block_offset = 0x200000,
622 .block_offset_mask = 0x3fffff,
626 static struct fsl_pcie_data t2080_data = {
627 .block_offset = 0x240000,
628 .block_offset_mask = 0x3fffff,
632 static const struct udevice_id fsl_pcie_ids[] = {
633 { .compatible = "fsl,pcie-mpc8548", .data = (ulong)&p1_p2_data },
634 { .compatible = "fsl,pcie-p1_p2", .data = (ulong)&p1_p2_data },
635 { .compatible = "fsl,pcie-p2041", .data = (ulong)&p2041_data },
636 { .compatible = "fsl,pcie-p3041", .data = (ulong)&p2041_data },
637 { .compatible = "fsl,pcie-p4080", .data = (ulong)&p2041_data },
638 { .compatible = "fsl,pcie-p5040", .data = (ulong)&p2041_data },
639 { .compatible = "fsl,pcie-t102x", .data = (ulong)&t2080_data },
640 { .compatible = "fsl,pcie-t104x", .data = (ulong)&t2080_data },
641 { .compatible = "fsl,pcie-t2080", .data = (ulong)&t2080_data },
642 { .compatible = "fsl,pcie-t4240", .data = (ulong)&t2080_data },
646 U_BOOT_DRIVER(fsl_pcie) = {
649 .of_match = fsl_pcie_ids,
650 .ops = &fsl_pcie_ops,
651 .ofdata_to_platdata = fsl_pcie_ofdata_to_platdata,
652 .probe = fsl_pcie_probe,
653 .priv_auto_alloc_size = sizeof(struct fsl_pcie),