[PATCH v3 1/2] pcie-xilinx: Wait for link-up status during initialization

Mike Looijmans
2025-04-09T15:07:41Z

When the driver loads, the transceiver may still be in the state of
setting up a link. Wait for that to complete before continuing. This
fixes that the PCIe core does not work when loading the PL bitstream
from userspace. There's only milliseconds between the FPGA boot and the
core initializing in that case, and the link won't be up yet. The design
only worked when the FPGA was programmed in the bootloader, as that will
give the system hundreds of milliseconds to boot.

As the PCIe spec allows up to 100 ms time to establish a link, we'll
allow up to 200ms before giving up.

Signed-off-by: Mike Looijmans <mike.looijmans@topic.nl>
---

(no changes since v2)

Changes in v2:
Split into "reset GPIO" and "wait for link" patches
Add timeout explanation

drivers/pci/controller/pcie-xilinx.c | 16 +++++++++++++++-
1 file changed, 15 insertions(+), 1 deletion(-)

diff --git a/drivers/pci/controller/pcie-xilinx.c b/drivers/pci/controller/pcie-xilinx.c
index 0b534f73a942..2e59b91f43e0 100644
--- a/drivers/pci/controller/pcie-xilinx.c
+++ b/drivers/pci/controller/pcie-xilinx.c
@@ -15,6 +15,7 @@
#include <linux/irqdomain.h>
#include <linux/kernel.h>
#include <linux/init.h>
+#include <linux/iopoll.h>
#include <linux/msi.h>
#include <linux/of_address.h>
#include <linux/of_pci.h>
@@ -126,6 +127,19 @@ static inline bool xilinx_pcie_link_up(struct xilinx_pcie *pcie)
XILINX_PCIE_REG_PSCR_LNKUP) ? 1 : 0;
}

+static int xilinx_pci_wait_link_up(struct xilinx_pcie *pcie)
+{
+ u32 val;
+
+ /*
+ * PCIe r6.0, sec 6.6.1 provides 100ms timeout. Since this is FPGA
+ * fabric, we're more lenient and allow 200 ms for link training.
+ */
+ return readl_poll_timeout(pcie->reg_base + XILINX_PCIE_REG_PSCR, val,
+ (val & XILINX_PCIE_REG_PSCR_LNKUP), 2 * USEC_PER_MSEC,
+ 200 * USEC_PER_MSEC);
+}
+
/**
* xilinx_pcie_clear_err_interrupts - Clear Error Interrupts
* @pcie: PCIe port information
@@ -493,7 +507,7 @@ static void xilinx_pcie_init_port(struct xilinx_pcie *pcie)
{
struct device *dev = pcie->dev;

- if (xilinx_pcie_link_up(pcie))
+ if (!xilinx_pci_wait_link_up(pcie))
dev_info(dev, "PCIe Link is UP\n");
else
dev_info(dev, "PCIe Link is DOWN\n");
--
2.43.0