From: "Frédéric Mahé" <frederic.mahe@cirad.fr>
To: "Lorenzo Pieralisi" <lpieralisi@kernel.org>,
"Krzysztof Wilczyński" <kwilczynski@kernel.org>,
"Manivannan Sadhasivam" <mani@kernel.org>,
"Bjorn Helgaas" <bhelgaas@google.com>
Cc: "Frédéric Mahé" <frederic.mahe@cirad.fr>,
"Rob Herring" <robh@kernel.org>, "Yixun Lan" <dlan@kernel.org>,
"Troy Mitchell" <troy.mitchell@linux.spacemit.com>,
"Alex Elder" <elder@riscstar.com>,
"Encrow Thorne" <encrow.thorne@linux.spacemit.com>,
"Aurelien Jarno" <aurelien@aurel32.net>,
linux-pci@vger.kernel.org, spacemit@lists.linux.dev,
linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org
Subject: [PATCH] PCI: spacemit-k1: Request the Gen2 speed change once the link is up
Date: Tue, 17 Nov 2026 09:55:06 +0100 [thread overview]
Message-ID: <20261117085508.83388-1-frederic.mahe@cirad.fr> (raw)
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
next reply other threads:[~2026-11-17 8:55 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-17 8:55 Frédéric Mahé [this message]
2026-11-17 21:42 ` Aurelien Jarno
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261117085508.83388-1-frederic.mahe@cirad.fr \
--to=frederic.mahe@cirad.fr \
--cc=aurelien@aurel32.net \
--cc=bhelgaas@google.com \
--cc=dlan@kernel.org \
--cc=elder@riscstar.com \
--cc=encrow.thorne@linux.spacemit.com \
--cc=kwilczynski@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-pci@vger.kernel.org \
--cc=linux-riscv@lists.infradead.org \
--cc=lpieralisi@kernel.org \
--cc=mani@kernel.org \
--cc=robh@kernel.org \
--cc=spacemit@lists.linux.dev \
--cc=troy.mitchell@linux.spacemit.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®