1 // SPDX-License-Identifier: GPL-2.0+ OR X11
5 * PCIe DM U-Boot driver for Freescale PowerPC SoCs
14 #include <asm/fsl_pci.h>
15 #include <asm/fsl_serdes.h>
16 #include <asm/global_data.h>
18 #include <linux/delay.h>
19 #include <linux/printk.h>
21 #include <dm/device_compat.h>
23 LIST_HEAD(fsl_pcie_list);
25 static int fsl_pcie_link_up(struct fsl_pcie *pcie);
27 static int fsl_pcie_addr_valid(struct fsl_pcie *pcie, pci_dev_t bdf)
29 struct udevice *bus = pcie->bus;
34 if (PCI_BUS(bdf) < dev_seq(bus))
37 if (PCI_BUS(bdf) > dev_seq(bus) && (!fsl_pcie_link_up(pcie) || pcie->mode))
40 if (PCI_BUS(bdf) == dev_seq(bus) && (PCI_DEV(bdf) > 0 || PCI_FUNC(bdf) > 0))
43 if (PCI_BUS(bdf) == (dev_seq(bus) + 1) && (PCI_DEV(bdf) > 0))
49 static int fsl_pcie_read_config(const struct udevice *bus, pci_dev_t bdf,
50 uint offset, ulong *valuep,
53 struct fsl_pcie *pcie = dev_get_priv(bus);
54 ccsr_fsl_pci_t *regs = pcie->regs;
57 if (fsl_pcie_addr_valid(pcie, bdf)) {
58 *valuep = pci_get_ff(size);
62 /* Skip Freescale PCIe controller's PEXCSRBAR register */
63 if (PCI_BUS(bdf) - dev_seq(bus) == 0 &&
64 PCI_DEV(bdf) == 0 && PCI_FUNC(bdf) == 0 &&
65 (offset & ~3) == PCI_BASE_ADDRESS_0) {
70 val = PCI_CONF1_EXT_ADDRESS(PCI_BUS(bdf) - dev_seq(bus),
71 PCI_DEV(bdf), PCI_FUNC(bdf),
73 out_be32(®s->cfg_addr, val);
79 *valuep = in_8((u8 *)®s->cfg_data + (offset & 3));
82 *valuep = in_le16((u16 *)((u8 *)®s->cfg_data +
86 *valuep = in_le32(®s->cfg_data);
93 static int fsl_pcie_write_config(struct udevice *bus, pci_dev_t bdf,
94 uint offset, ulong value,
97 struct fsl_pcie *pcie = dev_get_priv(bus);
98 ccsr_fsl_pci_t *regs = pcie->regs;
104 if (fsl_pcie_addr_valid(pcie, bdf))
107 /* Skip Freescale PCIe controller's PEXCSRBAR register */
108 if (PCI_BUS(bdf) - dev_seq(bus) == 0 &&
109 PCI_DEV(bdf) == 0 && PCI_FUNC(bdf) == 0 &&
110 (offset & ~3) == PCI_BASE_ADDRESS_0)
113 val = PCI_CONF1_EXT_ADDRESS(PCI_BUS(bdf) - dev_seq(bus),
114 PCI_DEV(bdf), PCI_FUNC(bdf),
116 out_be32(®s->cfg_addr, val);
123 out_8((u8 *)®s->cfg_data + (offset & 3), val_8);
127 out_le16((u16 *)((u8 *)®s->cfg_data + (offset & 2)), val_16);
131 out_le32(®s->cfg_data, val_32);
138 static int fsl_pcie_hose_read_config(struct fsl_pcie *pcie, uint offset,
139 ulong *valuep, enum pci_size_t size)
142 struct udevice *bus = pcie->bus;
144 ret = fsl_pcie_read_config(bus, PCI_BDF(dev_seq(bus), 0, 0),
145 offset, valuep, size);
150 static int fsl_pcie_hose_write_config(struct fsl_pcie *pcie, uint offset,
151 ulong value, enum pci_size_t size)
153 struct udevice *bus = pcie->bus;
155 return fsl_pcie_write_config(bus, PCI_BDF(dev_seq(bus), 0, 0),
156 offset, value, size);
159 static int fsl_pcie_hose_read_config_byte(struct fsl_pcie *pcie, uint offset,
165 ret = fsl_pcie_hose_read_config(pcie, offset, &val, PCI_SIZE_8);
171 static int fsl_pcie_hose_read_config_word(struct fsl_pcie *pcie, uint offset,
177 ret = fsl_pcie_hose_read_config(pcie, offset, &val, PCI_SIZE_16);
183 static int fsl_pcie_hose_read_config_dword(struct fsl_pcie *pcie, uint offset,
189 ret = fsl_pcie_hose_read_config(pcie, offset, &val, PCI_SIZE_32);
195 static int fsl_pcie_hose_write_config_byte(struct fsl_pcie *pcie, uint offset,
198 return fsl_pcie_hose_write_config(pcie, offset, value, PCI_SIZE_8);
201 static int fsl_pcie_hose_write_config_word(struct fsl_pcie *pcie, uint offset,
204 return fsl_pcie_hose_write_config(pcie, offset, value, PCI_SIZE_16);
207 static int fsl_pcie_hose_write_config_dword(struct fsl_pcie *pcie, uint offset,
210 return fsl_pcie_hose_write_config(pcie, offset, value, PCI_SIZE_32);
213 static int fsl_pcie_link_up(struct fsl_pcie *pcie)
215 ccsr_fsl_pci_t *regs = pcie->regs;
218 if (pcie->block_rev >= PEX_IP_BLK_REV_3_0) {
219 ltssm = (in_be32(®s->pex_csr0)
220 & PEX_CSR0_LTSSM_MASK) >> PEX_CSR0_LTSSM_SHIFT;
221 return ltssm == LTSSM_L0_REV3;
224 fsl_pcie_hose_read_config_word(pcie, PCI_LTSSM, <ssm);
226 return ltssm == LTSSM_L0;
229 static bool fsl_pcie_is_agent(struct fsl_pcie *pcie)
233 fsl_pcie_hose_read_config_byte(pcie, PCI_HEADER_TYPE, &header_type);
235 return (header_type & 0x7f) == PCI_HEADER_TYPE_NORMAL;
238 static int fsl_pcie_setup_law(struct fsl_pcie *pcie)
240 struct pci_region *io, *mem, *pref;
242 pci_get_regions(pcie->bus, &io, &mem, &pref);
245 set_next_law(mem->phys_start,
246 law_size_bits(mem->size),
250 set_next_law(io->phys_start,
251 law_size_bits(io->size),
257 static void fsl_pcie_config_ready(struct fsl_pcie *pcie)
259 ccsr_fsl_pci_t *regs = pcie->regs;
261 if (pcie->block_rev >= PEX_IP_BLK_REV_3_0) {
262 setbits_be32(®s->config, FSL_PCIE_V3_CFG_RDY);
266 fsl_pcie_hose_write_config_byte(pcie, FSL_PCIE_CFG_RDY, 0x1);
269 static int fsl_pcie_setup_outbound_win(struct fsl_pcie *pcie, int idx,
270 int type, u64 phys, u64 bus_addr,
273 ccsr_fsl_pci_t *regs = pcie->regs;
274 pot_t *po = ®s->pot[idx];
280 out_be32(&po->powbar, phys >> 12);
281 out_be32(&po->potar, bus_addr >> 12);
282 #ifdef CONFIG_SYS_PCI_64BIT
283 out_be32(&po->potear, bus_addr >> 44);
285 out_be32(&po->potear, 0);
288 sz = (__ilog2_u64((u64)size) - 1);
291 if (type == PCI_REGION_IO)
292 war |= POWAR_IO_READ | POWAR_IO_WRITE;
294 war |= POWAR_MEM_READ | POWAR_MEM_WRITE;
296 out_be32(&po->powar, war);
301 static int fsl_pcie_setup_inbound_win(struct fsl_pcie *pcie, int idx,
302 bool pf, u64 phys, u64 bus_addr,
305 ccsr_fsl_pci_t *regs = pcie->regs;
306 pit_t *pi = ®s->pit[idx];
307 u32 sz = (__ilog2_u64(size) - 1);
308 u32 flag = PIWAR_LOCAL;
313 out_be32(&pi->pitar, phys >> 12);
314 out_be32(&pi->piwbar, bus_addr >> 12);
316 #ifdef CONFIG_SYS_PCI_64BIT
317 out_be32(&pi->piwbear, bus_addr >> 44);
319 out_be32(&pi->piwbear, 0);
322 #ifdef CONFIG_SYS_FSL_ERRATUM_A005434
326 flag |= PIWAR_EN | PIWAR_READ_SNOOP | PIWAR_WRITE_SNOOP;
329 out_be32(&pi->piwar, flag | sz);
334 static int fsl_pcie_setup_outbound_wins(struct fsl_pcie *pcie)
336 struct pci_region *io, *mem, *pref;
337 int idx = 1; /* skip 0 */
339 pci_get_regions(pcie->bus, &io, &mem, &pref);
342 /* ATU : OUTBOUND : IO */
343 fsl_pcie_setup_outbound_win(pcie, idx++,
350 /* ATU : OUTBOUND : MEM */
351 fsl_pcie_setup_outbound_win(pcie, idx++,
359 static int fsl_pcie_setup_inbound_wins(struct fsl_pcie *pcie)
361 phys_addr_t phys_start = CFG_SYS_PCI_MEMORY_PHYS;
362 pci_addr_t bus_start = CFG_SYS_PCI_MEMORY_BUS;
363 u64 sz = min((u64)gd->ram_size, (1ull << 32));
367 if (pcie->block_rev >= PEX_IP_BLK_REV_2_2)
372 pci_sz = 1ull << __ilog2_u64(sz);
374 dev_dbg(pcie->bus, "R0 bus_start: %llx phys_start: %llx size: %llx\n",
375 (u64)bus_start, (u64)phys_start, (u64)sz);
377 /* if we aren't an exact power of two match, pci_sz is smaller
378 * round it up to the next power of two. We report the actual
379 * size to pci region tracking.
382 sz = 2ull << __ilog2_u64(sz);
384 fsl_pcie_setup_inbound_win(pcie, idx--, true,
385 CFG_SYS_PCI_MEMORY_PHYS,
386 CFG_SYS_PCI_MEMORY_BUS, sz);
387 #if defined(CONFIG_PHYS_64BIT) && defined(CONFIG_SYS_PCI_64BIT)
389 * On 64-bit capable systems, set up a mapping for all of DRAM
390 * in high pci address space.
392 pci_sz = 1ull << __ilog2_u64(gd->ram_size);
393 /* round up to the next largest power of two */
394 if (gd->ram_size > pci_sz)
395 pci_sz = 1ull << (__ilog2_u64(gd->ram_size) + 1);
397 dev_dbg(pcie->bus, "R64 bus_start: %llx phys_start: %llx size: %llx\n",
398 (u64)CFG_SYS_PCI64_MEMORY_BUS,
399 (u64)CFG_SYS_PCI_MEMORY_PHYS, (u64)pci_sz);
401 fsl_pcie_setup_inbound_win(pcie, idx--, true,
402 CFG_SYS_PCI_MEMORY_PHYS,
403 CFG_SYS_PCI64_MEMORY_BUS, pci_sz);
409 static int fsl_pcie_init_atmu(struct fsl_pcie *pcie)
411 fsl_pcie_setup_outbound_wins(pcie);
412 fsl_pcie_setup_inbound_wins(pcie);
417 static void fsl_pcie_dbi_read_only_reg_write_enable(struct fsl_pcie *pcie,
422 fsl_pcie_hose_read_config_dword(pcie, DBI_RO_WR_EN, &val);
427 fsl_pcie_hose_write_config_dword(pcie, DBI_RO_WR_EN, val);
430 static int fsl_pcie_init_port(struct fsl_pcie *pcie)
432 ccsr_fsl_pci_t *regs = pcie->regs;
436 fsl_pcie_init_atmu(pcie);
438 #ifdef CONFIG_FSL_PCIE_DISABLE_ASPM
440 fsl_pcie_hose_read_config_dword(pcie, PCI_LCR, &val_32);
442 fsl_pcie_hose_write_config_dword(pcie, PCI_LCR, val_32);
446 #ifdef CONFIG_FSL_PCIE_RESET
450 if (pcie->block_rev >= PEX_IP_BLK_REV_3_0) {
451 /* assert PCIe reset */
452 setbits_be32(®s->pdb_stat, 0x08000000);
453 (void)in_be32(®s->pdb_stat);
455 /* clear PCIe reset */
456 clrbits_be32(®s->pdb_stat, 0x08000000);
458 for (i = 0; i < 100 && !fsl_pcie_link_up(pcie); i++)
461 fsl_pcie_hose_read_config_word(pcie, PCI_LTSSM, <ssm);
463 /* assert PCIe reset */
464 setbits_be32(®s->pdb_stat, 0x08000000);
465 (void)in_be32(®s->pdb_stat);
467 /* clear PCIe reset */
468 clrbits_be32(®s->pdb_stat, 0x08000000);
470 for (i = 0; i < 100 &&
471 !fsl_pcie_link_up(pcie); i++)
477 #ifdef CONFIG_SYS_P4080_ERRATUM_PCIE_A003
478 if (!fsl_pcie_link_up(pcie)) {
479 serdes_corenet_t *srds_regs;
481 srds_regs = (void *)CFG_SYS_FSL_CORENET_SERDES_ADDR;
482 val_32 = in_be32(&srds_regs->srdspccr0);
484 if ((val_32 >> 28) == 3) {
487 out_be32(&srds_regs->srdspccr0, 2 << 28);
488 setbits_be32(®s->pdb_stat, 0x08000000);
489 in_be32(®s->pdb_stat);
491 clrbits_be32(®s->pdb_stat, 0x08000000);
493 for (i = 0; i < 100 && !fsl_pcie_link_up(pcie); i++)
500 * The Read-Only Write Enable bit defaults to 1 instead of 0.
501 * Set to 0 to protect the read-only registers.
503 #ifdef CONFIG_SYS_FSL_ERRATUM_A007815
504 fsl_pcie_dbi_read_only_reg_write_enable(pcie, false);
508 * Enable All Error Interrupts except
509 * - Master abort (pci)
510 * - Master PERR (pci)
513 out_be32(®s->peer, ~0x20140);
515 /* set URR, FER, NFER (but not CER) */
516 fsl_pcie_hose_read_config_dword(pcie, PCI_DCR, &val_32);
518 fsl_pcie_hose_write_config_dword(pcie, PCI_DCR, val_32);
520 /* Clear all error indications */
521 out_be32(®s->pme_msg_det, 0xffffffff);
522 out_be32(®s->pme_msg_int_en, 0xffffffff);
523 out_be32(®s->pedr, 0xffffffff);
525 fsl_pcie_hose_read_config_word(pcie, PCI_DSR, &val_16);
527 fsl_pcie_hose_write_config_word(pcie, PCI_DSR, 0xffff);
529 fsl_pcie_hose_read_config_word(pcie, PCI_SEC_STATUS, &val_16);
531 fsl_pcie_hose_write_config_word(pcie, PCI_SEC_STATUS, 0xffff);
536 static int fsl_pcie_fixup_classcode(struct fsl_pcie *pcie)
541 if (pcie->block_rev >= PEX_IP_BLK_REV_3_0) {
542 classcode_reg = PCI_CLASS_REVISION;
543 fsl_pcie_dbi_read_only_reg_write_enable(pcie, true);
545 classcode_reg = CSR_CLASSCODE;
548 fsl_pcie_hose_read_config_dword(pcie, classcode_reg, &val);
550 val |= PCI_CLASS_BRIDGE_PCI_NORMAL << 8;
551 fsl_pcie_hose_write_config_dword(pcie, classcode_reg, val);
553 if (pcie->block_rev >= PEX_IP_BLK_REV_3_0)
554 fsl_pcie_dbi_read_only_reg_write_enable(pcie, false);
559 static int fsl_pcie_init_rc(struct fsl_pcie *pcie)
561 return fsl_pcie_fixup_classcode(pcie);
564 static int fsl_pcie_init_ep(struct fsl_pcie *pcie)
566 fsl_pcie_config_ready(pcie);
571 static int fsl_pcie_probe(struct udevice *dev)
573 struct fsl_pcie *pcie = dev_get_priv(dev);
574 ccsr_fsl_pci_t *regs = pcie->regs;
578 pcie->block_rev = in_be32(®s->block_rev1);
580 list_add(&pcie->list, &fsl_pcie_list);
581 pcie->enabled = is_serdes_configured(PCIE1 + pcie->idx);
582 if (!pcie->enabled) {
583 printf("PCIe%d: %s disabled\n", pcie->idx, dev->name);
587 fsl_pcie_setup_law(pcie);
589 pcie->mode = fsl_pcie_is_agent(pcie);
591 fsl_pcie_init_port(pcie);
593 printf("PCIe%d: %s ", pcie->idx, dev->name);
597 fsl_pcie_init_ep(pcie);
599 printf("Root Complex");
600 fsl_pcie_init_rc(pcie);
603 if (!fsl_pcie_link_up(pcie)) {
604 printf(": %s\n", pcie->mode ? "undetermined link" : "no link");
608 fsl_pcie_hose_read_config_word(pcie, PCI_LSR, &val_16);
609 printf(": x%d gen%d\n", (val_16 & 0x3f0) >> 4, (val_16 & 0xf));
614 static int fsl_pcie_of_to_plat(struct udevice *dev)
616 struct fsl_pcie *pcie = dev_get_priv(dev);
617 struct fsl_pcie_data *info;
620 pcie->regs = dev_remap_addr(dev);
622 pr_err("\"reg\" resource not found\n");
626 ret = dev_read_u32(dev, "law_trgt_if", &pcie->law_trgt_if);
628 pr_err("\"law_trgt_if\" not found\n");
632 info = (struct fsl_pcie_data *)dev_get_driver_data(dev);
634 pcie->idx = abs((u32)(dev_read_addr(dev) & info->block_offset_mask) -
635 info->block_offset) / info->stride;
640 static const struct dm_pci_ops fsl_pcie_ops = {
641 .read_config = fsl_pcie_read_config,
642 .write_config = fsl_pcie_write_config,
645 static struct fsl_pcie_data p1_p2_data = {
646 .block_offset = 0xa000,
647 .block_offset_mask = 0xffff,
651 static struct fsl_pcie_data p2041_data = {
652 .block_offset = 0x200000,
653 .block_offset_mask = 0x3fffff,
657 static struct fsl_pcie_data t2080_data = {
658 .block_offset = 0x240000,
659 .block_offset_mask = 0x3fffff,
663 static const struct udevice_id fsl_pcie_ids[] = {
664 { .compatible = "fsl,mpc8548-pcie", .data = (ulong)&p1_p2_data },
665 { .compatible = "fsl,pcie-p1_p2", .data = (ulong)&p1_p2_data },
666 { .compatible = "fsl,pcie-p2041", .data = (ulong)&p2041_data },
667 { .compatible = "fsl,pcie-p3041", .data = (ulong)&p2041_data },
668 { .compatible = "fsl,pcie-p4080", .data = (ulong)&p2041_data },
669 { .compatible = "fsl,pcie-p5040", .data = (ulong)&p2041_data },
670 { .compatible = "fsl,pcie-t102x", .data = (ulong)&t2080_data },
671 { .compatible = "fsl,pcie-t104x", .data = (ulong)&t2080_data },
672 { .compatible = "fsl,pcie-t2080", .data = (ulong)&t2080_data },
673 { .compatible = "fsl,pcie-t4240", .data = (ulong)&t2080_data },
677 U_BOOT_DRIVER(fsl_pcie) = {
680 .of_match = fsl_pcie_ids,
681 .ops = &fsl_pcie_ops,
682 .of_to_plat = fsl_pcie_of_to_plat,
683 .probe = fsl_pcie_probe,
684 .priv_auto = sizeof(struct fsl_pcie),