Linux-RISC-V Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] PCI: spacemit-k1: Request the Gen2 speed change once the link is up
@ 2026-11-17  8:55 Frédéric Mahé
  2026-11-17 21:42 ` Aurelien Jarno
  0 siblings, 1 reply; 2+ messages in thread
From: Frédéric Mahé @ 2026-11-17  8:55 UTC (permalink / raw)
  To: Lorenzo Pieralisi, Krzysztof Wilczyński,
	Manivannan Sadhasivam, Bjorn Helgaas
  Cc: Frédéric Mahé, Rob Herring, Yixun Lan,
	Troy Mitchell, Alex Elder, Encrow Thorne, Aurelien Jarno,
	linux-pci, spacemit, linux-riscv, linux-kernel

On a Banana Pi BPI-F3, an NVMe SSD in the M.2 slot (pcie1) always comes
up at 2.5 GT/s, although the root port and the drive both support
5 GT/s. The Directed Speed Change that dw_pcie_setup_rc() requests
before the LTSSM is enabled has no effect on K1. Setting Retrain Link
in the root port's Link Control from userspace brings the link to
5 GT/s, so the link itself is fine.

Once the link is up, request the speed change again, wait for the
controller to clear SPEED_CHANGE (as pci-imx6 does), then briefly for
Link Status to report the new speed. A port with nothing behind it is
skipped after 100 ms.

With an SK hynix BC511, the link now trains at 5 GT/s x2 and fio
sequential read goes from 397 to 575-640 MB/s. Data verified with fio
crc32c. The empty pcie2 port behaves as before.

This change was developed with the help of Claude (Anthropic); I
reviewed and tested it on hardware.

Signed-off-by: Frédéric Mahé <frederic.mahe@cirad.fr>
---
The K1 PCIe cover letter says all ports run at 5 GT/s, so maybe this
depends on the drive or the firmware; on this board the link always came
up at 2.5 GT/s before this patch. Tested on top of v7.4-rc1 with "PCI:
spacemit: Ignore PERST# input in RC mode on K1" (applies cleanly without
it too).

 drivers/pci/controller/dwc/pcie-spacemit-k1.c | 48 +++++++++++++++++++
 1 file changed, 48 insertions(+)

diff --git a/drivers/pci/controller/dwc/pcie-spacemit-k1.c b/drivers/pci/controller/dwc/pcie-spacemit-k1.c
index 92a71ea90..4acc14efa 100644
--- a/drivers/pci/controller/dwc/pcie-spacemit-k1.c
+++ b/drivers/pci/controller/dwc/pcie-spacemit-k1.c
@@ -11,6 +11,7 @@
 #include <linux/device.h>
 #include <linux/err.h>
 #include <linux/gfp.h>
+#include <linux/iopoll.h>
 #include <linux/mfd/syscon.h>
 #include <linux/of.h>
 #include <linux/phy/phy.h>
@@ -209,6 +210,51 @@ static bool k1_pcie_link_up(struct dw_pcie *pci)
 	return (val & RDLH_LINK_UP) && (val & SMLH_LINK_UP);
 }
 
+/*
+ * The Directed Speed Change requested by dw_pcie_setup_rc() before the
+ * link is started does not take effect on K1, and the link stays at
+ * 2.5 GT/s.  Request it again once the link is up.
+ */
+static void k1_pcie_speed_change(struct dw_pcie *pci)
+{
+	u8 offset = dw_pcie_find_capability(pci, PCI_CAP_ID_EXP);
+	bool up;
+	u16 sta;
+	u32 val;
+	int ret;
+
+	if (pci->max_link_speed < 2)
+		return;
+
+	/* Nothing to do if there is no device behind the port */
+	ret = read_poll_timeout(k1_pcie_link_up, up, up, 1000,
+				100 * USEC_PER_MSEC, false, pci);
+	if (ret)
+		return;
+
+	val = dw_pcie_readl_dbi(pci, LINK_WIDTH_SPEED_CTRL);
+	dw_pcie_writel_dbi(pci, LINK_WIDTH_SPEED_CTRL, val & ~SPEED_CHANGE);
+	dw_pcie_writel_dbi(pci, LINK_WIDTH_SPEED_CTRL, val | SPEED_CHANGE);
+
+	/* The controller clears SPEED_CHANGE when the speed change is done */
+	ret = read_poll_timeout(dw_pcie_readl_dbi, val, !(val & SPEED_CHANGE),
+				1000, 100 * USEC_PER_MSEC, false,
+				pci, LINK_WIDTH_SPEED_CTRL);
+	if (ret) {
+		dev_warn(pci->dev, "link speed change timed out\n");
+		return;
+	}
+
+	/*
+	 * LnkSta lags behind SPEED_CHANGE; wait a little for the new speed
+	 * (stays at 2.5 GT/s if the device is Gen1 only).
+	 */
+	read_poll_timeout(dw_pcie_readw_dbi, sta,
+			  FIELD_GET(PCI_EXP_LNKSTA_CLS, sta) > 1,
+			  1000, 20 * USEC_PER_MSEC, false,
+			  pci, offset + PCI_EXP_LNKSTA);
+}
+
 static int k1_pcie_start_link(struct dw_pcie *pci)
 {
 	struct k1_pcie *k1 = to_k1_pcie(pci);
@@ -226,6 +272,8 @@ static int k1_pcie_start_link(struct dw_pcie *pci)
 	val |= PCIE_INTERRUPT_EN;
 	writel_relaxed(val, k1->link + K1_PHY_AHB_IRQ_EN);
 
+	k1_pcie_speed_change(pci);
+
 	return 0;
 }
 

base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
-- 
2.47.3


_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-17 21:42 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-17  8:55 [PATCH] PCI: spacemit-k1: Request the Gen2 speed change once the link is up Frédéric Mahé
2026-11-17 21:42 ` Aurelien Jarno

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox