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>
17 #include <linux/delay.h>
19 #include <dm/device_compat.h>
21 LIST_HEAD(fsl_pcie_list);
23 static int fsl_pcie_link_up(struct fsl_pcie *pcie);
25 static int fsl_pcie_addr_valid(struct fsl_pcie *pcie, pci_dev_t bdf)
27 struct udevice *bus = pcie->bus;
32 if (PCI_BUS(bdf) < bus->seq)
35 if (PCI_BUS(bdf) > bus->seq && (!fsl_pcie_link_up(pcie) || pcie->mode))
38 if (PCI_BUS(bdf) == bus->seq && (PCI_DEV(bdf) > 0 || PCI_FUNC(bdf) > 0))
41 if (PCI_BUS(bdf) == (bus->seq + 1) && (PCI_DEV(bdf) > 0))
47 static int fsl_pcie_read_config(const struct udevice *bus, pci_dev_t bdf,
48 uint offset, ulong *valuep,
51 struct fsl_pcie *pcie = dev_get_priv(bus);
52 ccsr_fsl_pci_t *regs = pcie->regs;
55 if (fsl_pcie_addr_valid(pcie, bdf)) {
56 *valuep = pci_get_ff(size);
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);
68 *valuep = in_8((u8 *)®s->cfg_data + (offset & 3));
71 *valuep = in_le16((u16 *)((u8 *)®s->cfg_data +
75 *valuep = in_le32(®s->cfg_data);
82 static int fsl_pcie_write_config(struct udevice *bus, pci_dev_t bdf,
83 uint offset, ulong value,
86 struct fsl_pcie *pcie = dev_get_priv(bus);
87 ccsr_fsl_pci_t *regs = pcie->regs;
93 if (fsl_pcie_addr_valid(pcie, bdf))
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);
105 out_8((u8 *)®s->cfg_data + (offset & 3), val_8);
109 out_le16((u16 *)((u8 *)®s->cfg_data + (offset & 2)), val_16);
113 out_le32(®s->cfg_data, val_32);
120 static int fsl_pcie_hose_read_config(struct fsl_pcie *pcie, uint offset,
121 ulong *valuep, enum pci_size_t size)
124 struct udevice *bus = pcie->bus;
126 ret = fsl_pcie_read_config(bus, PCI_BDF(bus->seq, 0, 0),
127 offset, valuep, size);
132 static int fsl_pcie_hose_write_config(struct fsl_pcie *pcie, uint offset,
133 ulong value, enum pci_size_t size)
135 struct udevice *bus = pcie->bus;
137 return fsl_pcie_write_config(bus, PCI_BDF(bus->seq, 0, 0),
138 offset, value, size);
141 static int fsl_pcie_hose_read_config_byte(struct fsl_pcie *pcie, uint offset,
147 ret = fsl_pcie_hose_read_config(pcie, offset, &val, PCI_SIZE_8);
153 static int fsl_pcie_hose_read_config_word(struct fsl_pcie *pcie, uint offset,
159 ret = fsl_pcie_hose_read_config(pcie, offset, &val, PCI_SIZE_16);
165 static int fsl_pcie_hose_read_config_dword(struct fsl_pcie *pcie, uint offset,
171 ret = fsl_pcie_hose_read_config(pcie, offset, &val, PCI_SIZE_32);
177 static int fsl_pcie_hose_write_config_byte(struct fsl_pcie *pcie, uint offset,
180 return fsl_pcie_hose_write_config(pcie, offset, value, PCI_SIZE_8);
183 static int fsl_pcie_hose_write_config_word(struct fsl_pcie *pcie, uint offset,
186 return fsl_pcie_hose_write_config(pcie, offset, value, PCI_SIZE_16);
189 static int fsl_pcie_hose_write_config_dword(struct fsl_pcie *pcie, uint offset,
192 return fsl_pcie_hose_write_config(pcie, offset, value, PCI_SIZE_32);
195 static int fsl_pcie_link_up(struct fsl_pcie *pcie)
197 ccsr_fsl_pci_t *regs = pcie->regs;
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;
206 fsl_pcie_hose_read_config_word(pcie, PCI_LTSSM, <ssm);
208 return ltssm == LTSSM_L0;
211 static bool fsl_pcie_is_agent(struct fsl_pcie *pcie)
215 fsl_pcie_hose_read_config_byte(pcie, PCI_HEADER_TYPE, &header_type);
217 return (header_type & 0x7f) == PCI_HEADER_TYPE_NORMAL;
220 static int fsl_pcie_setup_law(struct fsl_pcie *pcie)
222 struct pci_region *io, *mem, *pref;
224 pci_get_regions(pcie->bus, &io, &mem, &pref);
227 set_next_law(mem->phys_start,
228 law_size_bits(mem->size),
232 set_next_law(io->phys_start,
233 law_size_bits(io->size),
239 static void fsl_pcie_config_ready(struct fsl_pcie *pcie)
241 ccsr_fsl_pci_t *regs = pcie->regs;
243 if (pcie->block_rev >= PEX_IP_BLK_REV_3_0) {
244 setbits_be32(®s->config, FSL_PCIE_V3_CFG_RDY);
248 fsl_pcie_hose_write_config_byte(pcie, FSL_PCIE_CFG_RDY, 0x1);
251 static int fsl_pcie_setup_outbound_win(struct fsl_pcie *pcie, int idx,
252 int type, u64 phys, u64 bus_addr,
255 ccsr_fsl_pci_t *regs = pcie->regs;
256 pot_t *po = ®s->pot[idx];
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);
267 out_be32(&po->potear, 0);
270 sz = (__ilog2_u64((u64)size) - 1);
273 if (type == PCI_REGION_IO)
274 war |= POWAR_IO_READ | POWAR_IO_WRITE;
276 war |= POWAR_MEM_READ | POWAR_MEM_WRITE;
278 out_be32(&po->powar, war);
283 static int fsl_pcie_setup_inbound_win(struct fsl_pcie *pcie, int idx,
284 bool pf, u64 phys, u64 bus_addr,
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;
295 out_be32(&pi->pitar, phys >> 12);
296 out_be32(&pi->piwbar, bus_addr >> 12);
298 #ifdef CONFIG_SYS_PCI_64BIT
299 out_be32(&pi->piwbear, bus_addr >> 44);
301 out_be32(&pi->piwbear, 0);
304 #ifdef CONFIG_SYS_FSL_ERRATUM_A005434
308 flag |= PIWAR_EN | PIWAR_READ_SNOOP | PIWAR_WRITE_SNOOP;
311 out_be32(&pi->piwar, flag | sz);
316 static int fsl_pcie_setup_outbound_wins(struct fsl_pcie *pcie)
318 struct pci_region *io, *mem, *pref;
319 int idx = 1; /* skip 0 */
321 pci_get_regions(pcie->bus, &io, &mem, &pref);
324 /* ATU : OUTBOUND : IO */
325 fsl_pcie_setup_outbound_win(pcie, idx++,
332 /* ATU : OUTBOUND : MEM */
333 fsl_pcie_setup_outbound_win(pcie, idx++,
341 static int fsl_pcie_setup_inbound_wins(struct fsl_pcie *pcie)
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));
349 if (pcie->block_rev >= PEX_IP_BLK_REV_2_2)
354 pci_sz = 1ull << __ilog2_u64(sz);
356 dev_dbg(pcie->bus, "R0 bus_start: %llx phys_start: %llx size: %llx\n",
357 (u64)bus_start, (u64)phys_start, (u64)sz);
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.
364 sz = 2ull << __ilog2_u64(sz);
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)
371 * On 64-bit capable systems, set up a mapping for all of DRAM
372 * in high pci address space.
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);
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);
383 fsl_pcie_setup_inbound_win(pcie, idx--, true,
384 CONFIG_SYS_PCI_MEMORY_PHYS,
385 CONFIG_SYS_PCI64_MEMORY_BUS, pci_sz);
391 static int fsl_pcie_init_atmu(struct fsl_pcie *pcie)
393 fsl_pcie_setup_outbound_wins(pcie);
394 fsl_pcie_setup_inbound_wins(pcie);
399 static int fsl_pcie_init_port(struct fsl_pcie *pcie)
401 ccsr_fsl_pci_t *regs = pcie->regs;
405 fsl_pcie_init_atmu(pcie);
407 #ifdef CONFIG_FSL_PCIE_DISABLE_ASPM
409 fsl_pcie_hose_read_config_dword(pcie, PCI_LCR, &val_32);
411 fsl_pcie_hose_write_config_dword(pcie, PCI_LCR, val_32);
415 #ifdef CONFIG_FSL_PCIE_RESET
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);
424 /* clear PCIe reset */
425 clrbits_be32(®s->pdb_stat, 0x08000000);
427 for (i = 0; i < 100 && !fsl_pcie_link_up(pcie); i++)
430 fsl_pcie_hose_read_config_word(pcie, PCI_LTSSM, <ssm);
432 /* assert PCIe reset */
433 setbits_be32(®s->pdb_stat, 0x08000000);
434 (void)in_be32(®s->pdb_stat);
436 /* clear PCIe reset */
437 clrbits_be32(®s->pdb_stat, 0x08000000);
439 for (i = 0; i < 100 &&
440 !fsl_pcie_link_up(pcie); i++)
446 #ifdef CONFIG_SYS_P4080_ERRATUM_PCIE_A003
447 if (!fsl_pcie_link_up(pcie)) {
448 serdes_corenet_t *srds_regs;
450 srds_regs = (void *)CONFIG_SYS_FSL_CORENET_SERDES_ADDR;
451 val_32 = in_be32(&srds_regs->srdspccr0);
453 if ((val_32 >> 28) == 3) {
456 out_be32(&srds_regs->srdspccr0, 2 << 28);
457 setbits_be32(®s->pdb_stat, 0x08000000);
458 in_be32(®s->pdb_stat);
460 clrbits_be32(®s->pdb_stat, 0x08000000);
462 for (i = 0; i < 100 && !fsl_pcie_link_up(pcie); i++)
469 * The Read-Only Write Enable bit defaults to 1 instead of 0.
470 * Set to 0 to protect the read-only registers.
472 #ifdef CONFIG_SYS_FSL_ERRATUM_A007815
473 clrbits_be32(®s->dbi_ro_wr_en, 0x01);
477 * Enable All Error Interrupts except
478 * - Master abort (pci)
479 * - Master PERR (pci)
482 out_be32(®s->peer, ~0x20140);
484 /* set URR, FER, NFER (but not CER) */
485 fsl_pcie_hose_read_config_dword(pcie, PCI_DCR, &val_32);
487 fsl_pcie_hose_write_config_dword(pcie, PCI_DCR, val_32);
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);
494 fsl_pcie_hose_read_config_word(pcie, PCI_DSR, &val_16);
496 fsl_pcie_hose_write_config_word(pcie, PCI_DSR, 0xffff);
498 fsl_pcie_hose_read_config_word(pcie, PCI_SEC_STATUS, &val_16);
500 fsl_pcie_hose_write_config_word(pcie, PCI_SEC_STATUS, 0xffff);
505 static int fsl_pcie_fixup_classcode(struct fsl_pcie *pcie)
507 ccsr_fsl_pci_t *regs = pcie->regs;
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);
515 classcode_reg = CSR_CLASSCODE;
518 fsl_pcie_hose_read_config_dword(pcie, classcode_reg, &val);
520 val |= PCI_CLASS_BRIDGE_PCI << 16;
521 fsl_pcie_hose_write_config_dword(pcie, classcode_reg, val);
523 if (pcie->block_rev >= PEX_IP_BLK_REV_3_0)
524 clrbits_be32(®s->dbi_ro_wr_en, 0x01);
529 static int fsl_pcie_init_rc(struct fsl_pcie *pcie)
531 return fsl_pcie_fixup_classcode(pcie);
534 static int fsl_pcie_init_ep(struct fsl_pcie *pcie)
536 fsl_pcie_config_ready(pcie);
541 static int fsl_pcie_probe(struct udevice *dev)
543 struct fsl_pcie *pcie = dev_get_priv(dev);
544 ccsr_fsl_pci_t *regs = pcie->regs;
548 pcie->block_rev = in_be32(®s->block_rev1);
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);
557 fsl_pcie_setup_law(pcie);
559 pcie->mode = fsl_pcie_is_agent(pcie);
561 fsl_pcie_init_port(pcie);
563 printf("PCIe%d: %s ", pcie->idx, dev->name);
567 fsl_pcie_init_ep(pcie);
569 printf("Root Complex");
570 fsl_pcie_init_rc(pcie);
573 if (!fsl_pcie_link_up(pcie)) {
574 printf(": %s\n", pcie->mode ? "undetermined link" : "no link");
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));
584 static int fsl_pcie_ofdata_to_platdata(struct udevice *dev)
586 struct fsl_pcie *pcie = dev_get_priv(dev);
587 struct fsl_pcie_data *info;
590 pcie->regs = dev_remap_addr(dev);
592 pr_err("\"reg\" resource not found\n");
596 ret = dev_read_u32(dev, "law_trgt_if", &pcie->law_trgt_if);
598 pr_err("\"law_trgt_if\" not found\n");
602 info = (struct fsl_pcie_data *)dev_get_driver_data(dev);
604 pcie->idx = abs((u32)(dev_read_addr(dev) & info->block_offset_mask) -
605 info->block_offset) / info->stride;
610 static const struct dm_pci_ops fsl_pcie_ops = {
611 .read_config = fsl_pcie_read_config,
612 .write_config = fsl_pcie_write_config,
615 static struct fsl_pcie_data p1_p2_data = {
616 .block_offset = 0xa000,
617 .block_offset_mask = 0xffff,
621 static struct fsl_pcie_data p2041_data = {
622 .block_offset = 0x200000,
623 .block_offset_mask = 0x3fffff,
627 static struct fsl_pcie_data t2080_data = {
628 .block_offset = 0x240000,
629 .block_offset_mask = 0x3fffff,
633 static const struct udevice_id fsl_pcie_ids[] = {
634 { .compatible = "fsl,pcie-mpc8548", .data = (ulong)&p1_p2_data },
635 { .compatible = "fsl,pcie-p1_p2", .data = (ulong)&p1_p2_data },
636 { .compatible = "fsl,pcie-p2041", .data = (ulong)&p2041_data },
637 { .compatible = "fsl,pcie-p3041", .data = (ulong)&p2041_data },
638 { .compatible = "fsl,pcie-p4080", .data = (ulong)&p2041_data },
639 { .compatible = "fsl,pcie-p5040", .data = (ulong)&p2041_data },
640 { .compatible = "fsl,pcie-t102x", .data = (ulong)&t2080_data },
641 { .compatible = "fsl,pcie-t104x", .data = (ulong)&t2080_data },
642 { .compatible = "fsl,pcie-t2080", .data = (ulong)&t2080_data },
643 { .compatible = "fsl,pcie-t4240", .data = (ulong)&t2080_data },
647 U_BOOT_DRIVER(fsl_pcie) = {
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),