[PATCH RFC v4 09/15] phy: freescale: s32g: Add SerDes subsystem PHY
From: Jan Petrous via B4 Relay
Date: Wed Nov 25 2026 - 04:36:45 EST
From: Vincent Guittot <vincent.guittot@xxxxxxxxxx>
Each S32G SerDes subsystem is made of one PCIe controller, two XPCS and
a shared 2-lane combo PHY. The lanes carry PCIe and/or SGMII.
Add the SerDes subsystem driver: the PCIe PHY, the combo PHY bring-up
and the setup of the XPCS instances. A MAC gets its phylink PCS through
s32g_serdes_pcs_create(), which also creates a device link, so that the
MAC is unbound and suspended before the SerDes.
SS_RW_REG_0[SUBSYS_MODE] is derived at probe from the consumers' phys
references to the SerDes node, <lane type instance max-speed>: a PCIe
root port names its lanes, a MAC names its lane, the XPCS instance it
is wired to and the highest speed the lane is wired for, 1000 in this
series. The driver walks the tree for them, as the mux is programmed before
the reset is released, long before the consumers probe. Each supported
mode has a unique lane mux; a lane nobody references takes the function
it has in the first matching mode. The reference clock rate is
validated against the derived mode.
The SerDes provides one PHY for the PCIe link, whatever its width, and
one PHY per XPCS instance in use. The XPCS lane PHY has no operations:
the lane is brought up with the SerDes, and the MAC drives it through
the PCS, which it gets from s32g_serdes_pcs_create() with its own node.
After the subsystem reset is released, the PHY needs up to 5.2 ms to
become operational (MPLL_STATE). Poll it before any further PHY or XPCS
PMA access, in every mode.
Signed-off-by: Vincent Guittot <vincent.guittot@xxxxxxxxxx>
Co-developed-by: Ciprian Marian Costea <ciprianmarian.costea@xxxxxxxxxxx>
Signed-off-by: Ciprian Marian Costea <ciprianmarian.costea@xxxxxxxxxxx>
Co-developed-by: Alexandru-Catalin Ionita <alexandru-catalin.ionita@xxxxxxx>
Signed-off-by: Alexandru-Catalin Ionita <alexandru-catalin.ionita@xxxxxxx>
Co-developed-by: Ghennadi Procopciuc <ghennadi.procopciuc@xxxxxxx>
Signed-off-by: Ghennadi Procopciuc <ghennadi.procopciuc@xxxxxxx>
Co-developed-by: Ionut Vicovan <Ionut.Vicovan@xxxxxxx>
Signed-off-by: Ionut Vicovan <Ionut.Vicovan@xxxxxxx>
Co-developed-by: Bogdan Roman <bogdan-gabriel.roman@xxxxxxx>
Signed-off-by: Bogdan Roman <bogdan-gabriel.roman@xxxxxxx>
Co-developed-by: Jan Petrous (OSS) <jan.petrous@xxxxxxxxxxx>
Signed-off-by: Jan Petrous (OSS) <jan.petrous@xxxxxxxxxxx>
---
drivers/phy/freescale/Kconfig | 13 +
drivers/phy/freescale/Makefile | 1 +
drivers/phy/freescale/phy-nxp-s32g-serdes.c | 1132 +++++++++++++++++++++++++++
include/linux/phy/nxp-s32g-serdes.h | 25 +
4 files changed, 1171 insertions(+)
diff --git a/drivers/phy/freescale/Kconfig b/drivers/phy/freescale/Kconfig
index 7557b624be8f..a32249db5c8a 100644
--- a/drivers/phy/freescale/Kconfig
+++ b/drivers/phy/freescale/Kconfig
@@ -77,3 +77,16 @@ config PHY_FSL_LYNX_28G
found on NXP's Layerscape platforms such as LX2160A.
Used to change the protocol running on SerDes lanes at runtime.
Only useful for a restricted set of Ethernet protocols.
+
+config PHY_S32G_SERDES
+ tristate "NXP S32G SERDES support"
+ depends on ARCH_S32 || COMPILE_TEST
+ depends on NETDEVICES
+ select GENERIC_PHY
+ select PCS_XPCS
+ select PCS_NXP_S32G_XPCS
+ help
+ Enable this to support the SerDes subsystems of the NXP S32G2 and
+ S32G3 SoCs. Each one has a two-lane combo PHY shared between a PCIe
+ controller and two XPCS instances, used by the PCIe host driver
+ and, for SGMII, by the GMAC Ethernet driver.
diff --git a/drivers/phy/freescale/Makefile b/drivers/phy/freescale/Makefile
index 5b0e180d6972..1a0aec790917 100644
--- a/drivers/phy/freescale/Makefile
+++ b/drivers/phy/freescale/Makefile
@@ -8,3 +8,4 @@ obj-$(CONFIG_PHY_FSL_LYNX_CORE) += phy-fsl-lynx-core.o
obj-$(CONFIG_PHY_FSL_LYNX_10G) += phy-fsl-lynx-10g.o
obj-$(CONFIG_PHY_FSL_LYNX_28G) += phy-fsl-lynx-28g.o
obj-$(CONFIG_PHY_FSL_SAMSUNG_HDMI_PHY) += phy-fsl-samsung-hdmi.o
+obj-$(CONFIG_PHY_S32G_SERDES) += phy-nxp-s32g-serdes.o
diff --git a/drivers/phy/freescale/phy-nxp-s32g-serdes.c b/drivers/phy/freescale/phy-nxp-s32g-serdes.c
new file mode 100644
index 000000000000..07242cf3109e
--- /dev/null
+++ b/drivers/phy/freescale/phy-nxp-s32g-serdes.c
@@ -0,0 +1,1132 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * SerDes driver for S32G SoCs
+ *
+ * Copyright 2021-2026 NXP
+ */
+
+#include <dt-bindings/phy/phy.h>
+#include <linux/bitfield.h>
+#include <linux/clk.h>
+#include <linux/delay.h>
+#include <linux/iopoll.h>
+#include <linux/module.h>
+#include <linux/of_platform.h>
+#include <linux/pcs/pcs-nxp-s32g-xpcs.h>
+#include <linux/phy/nxp-s32g-serdes.h>
+#include <linux/phy/phy.h>
+#include <linux/platform_device.h>
+#include <linux/property.h>
+#include <linux/reset.h>
+#include <linux/units.h>
+
+#define S32G_SERDES_XPCS_MAX 2
+
+#define EXTERNAL_CLK_NAME "ext"
+#define INTERNAL_CLK_NAME "ref"
+
+/* Serdes Sub system registers */
+
+#define S32G_PCIE_PHY_GEN_CTRL 0x0
+#define REF_USE_PAD BIT(17)
+#define RX_SRIS_MODE BIT(9)
+
+#define S32G_PCIE_PHY_MPLLA_CTRL 0x10
+#define MPLL_STATE BIT(30)
+
+#define S32G_SS_RW_REG_0 0xF0
+#define SUBMODE_MASK GENMASK(2, 0)
+#define CLKEN_MASK BIT(23)
+#define PHY0_CR_PARA_SEL BIT(9)
+
+/* PCIe phy subsystem registers */
+
+#define S32G_PHY_REG_ADDR 0x0
+#define PHY_REG_EN BIT(31)
+
+#define S32G_PHY_REG_DATA 0x4
+
+#define S32G_PHY_REG_SETTLE_MIN_US 100
+#define S32G_PHY_REG_SETTLE_MAX_US 110
+
+#define RAWLANE0_DIG_PCS_XF_RX_EQ_DELTA_IQ_OVRD_IN 0x3019
+#define RAWLANE1_DIG_PCS_XF_RX_EQ_DELTA_IQ_OVRD_IN 0x3119
+#define RX_EQ_DELTA_IQ_OVRD_VAL GENMASK(3, 0)
+#define RX_EQ_DELTA_IQ_OVRD_EN BIT(4)
+
+#define S32G_PCIE_RX_EQ_DELTA_IQ 3
+
+#define S32G_SS_MODE_SETTLE_MIN_US 100
+#define S32G_SS_MODE_SETTLE_MAX_US 110
+
+/*
+ * PCIe reference clock architecture, the submode of
+ * phy_set_mode_ext(PHY_MODE_PCIE). Driver private, not PHY_MODE_PCIE_*.
+ */
+enum pcie_phy_mode {
+ CRNS = 0, /* Common Reference Clock, No Spread Spectrum */
+ CRSS = 1, /* Common Reference Clock, Spread Spectrum */
+ SRNS = 2, /* Separate Reference Clock, No Spread Spectrum */
+ SRIS = 3 /* Separate Reference Clock, Spread Spectrum */
+};
+
+/*
+ * phys = <&serdes lane type instance max-speed>; SS_RW_REG_0[SUBSYS_MODE]
+ * is derived from the consumers' references.
+ */
+#define S32G_SERDES_PHY_CELLS 4
+#define S32G_SERDES_LANE_SPEED_1G 1000
+#define S32G_SERDES_LANE_SPEED_2G5 2500
+
+enum s32g_lane_fn {
+ S32G_LANE_UNUSED = 0,
+ S32G_LANE_PCIE,
+ S32G_LANE_XPCS,
+};
+
+struct s32g_lane_desc {
+ enum s32g_lane_fn fn;
+ u32 xpcs_instance; /* valid iff fn == S32G_LANE_XPCS */
+};
+
+struct s32g_serdes_mode {
+ u32 submode; /* SS_RW_REG_0[SUBSYS_MODE] value */
+ struct s32g_lane_desc lane[2];
+ unsigned long refclk[2]; /* allowed rates in Hz, 0-terminated */
+};
+
+struct s32g_serdes_soc_data {
+ const struct s32g_serdes_mode *modes;
+ unsigned int n_modes;
+};
+
+#define S32G_LANE_IS_PCIE { .fn = S32G_LANE_PCIE }
+#define S32G_LANE_IS_XPCS(i) { .fn = S32G_LANE_XPCS, .xpcs_instance = (i) }
+
+#define S32G_REFCLK_100MHZ (100 * HZ_PER_MHZ)
+#define S32G_REFCLK_125MHZ (125 * HZ_PER_MHZ)
+
+/* S32G2/S32G3 1G modes. PCIe needs 100 MHz; mode 3 also takes 125 MHz. */
+static const struct s32g_serdes_mode s32g_serdes_modes[] = {
+ { 0, { S32G_LANE_IS_PCIE, S32G_LANE_IS_PCIE },
+ { S32G_REFCLK_100MHZ } },
+ { 1, { S32G_LANE_IS_PCIE, S32G_LANE_IS_XPCS(0) },
+ { S32G_REFCLK_100MHZ } },
+ { 2, { S32G_LANE_IS_PCIE, S32G_LANE_IS_XPCS(1) },
+ { S32G_REFCLK_100MHZ } },
+ { 3, { S32G_LANE_IS_XPCS(0), S32G_LANE_IS_XPCS(1) },
+ { S32G_REFCLK_100MHZ, S32G_REFCLK_125MHZ } },
+};
+
+/* S32G2 and S32G3 share the 1G table; the full tables differ per SoC. */
+static const struct s32g_serdes_soc_data s32g2_serdes_soc_data = {
+ .modes = s32g_serdes_modes,
+ .n_modes = ARRAY_SIZE(s32g_serdes_modes),
+};
+
+static const struct s32g_serdes_soc_data s32g3_serdes_soc_data = {
+ .modes = s32g_serdes_modes,
+ .n_modes = ARRAY_SIZE(s32g_serdes_modes),
+};
+
+struct s32g_serdes_ctrl {
+ void __iomem *ss_base;
+ struct reset_control *rst;
+ struct clk_bulk_data *clks;
+ int nclks;
+ u32 ss_mode;
+ unsigned long ref_clk_rate;
+ bool ext_clk;
+ /* Clocks on; cleared by suspend, never set by a failed init */
+ bool enabled;
+};
+
+struct s32g_pcie_ctrl {
+ void __iomem *phy_base;
+ struct reset_control *rst;
+ struct phy *phy;
+ enum pcie_phy_mode phy_mode;
+ bool powered_on;
+};
+
+struct s32g_xpcs_ctrl {
+ struct s32g_xpcs *phys[2]; /* indexed by XPCS instance */
+ void __iomem *base[2]; /* xpcs0 / xpcs1 register blocks */
+ struct phy *lane_phy[2]; /* generic PHY per XPCS instance */
+};
+
+struct s32g_serdes {
+ struct s32g_serdes_ctrl ctrl;
+ struct s32g_pcie_ctrl pcie;
+ struct s32g_xpcs_ctrl xpcs;
+ struct s32g_lane_desc lane[2];
+ const struct s32g_serdes_soc_data *soc_data;
+ struct device *dev;
+};
+
+/*
+ * MPLL_STATE sets within 3.4 ms (100 MHz reference) to 5.2 ms (125 MHz) of
+ * the reset release.
+ */
+#define S32G_SERDES_LOCK_TIMEOUT_US (10 * USEC_PER_MSEC)
+#define S32G_SERDES_LOCK_SLEEP_US 50
+
+static int s32g_serdes_wait_phy_operational(struct s32g_serdes *serdes)
+{
+ void __iomem *reg = serdes->ctrl.ss_base + S32G_PCIE_PHY_MPLLA_CTRL;
+ u32 val;
+ int ret;
+
+ ret = readl_poll_timeout(reg, val, (val & MPLL_STATE),
+ S32G_SERDES_LOCK_SLEEP_US,
+ S32G_SERDES_LOCK_TIMEOUT_US);
+ if (ret)
+ dev_err(serdes->dev, "SerDes PHY not operational\n");
+
+ return ret;
+}
+
+static int s32g_pcie_phy_power_on_common(struct s32g_serdes *serdes)
+{
+ struct s32g_serdes_ctrl *sctrl = &serdes->ctrl;
+ struct s32g_pcie_ctrl *pcie = &serdes->pcie;
+ u32 reg;
+ int ret;
+
+ reg = readl(sctrl->ss_base + S32G_PCIE_PHY_GEN_CTRL);
+
+ if (pcie->phy_mode == SRIS)
+ reg |= RX_SRIS_MODE;
+ else
+ reg &= ~RX_SRIS_MODE;
+
+ if (sctrl->ext_clk)
+ reg |= REF_USE_PAD;
+ else
+ reg &= ~REF_USE_PAD;
+
+ writel(reg, sctrl->ss_base + S32G_PCIE_PHY_GEN_CTRL);
+
+ ret = s32g_serdes_wait_phy_operational(serdes);
+ if (ret)
+ return ret;
+
+ reg = readl(sctrl->ss_base + S32G_SS_RW_REG_0);
+ reg |= PHY0_CR_PARA_SEL;
+ writel(reg, sctrl->ss_base + S32G_SS_RW_REG_0);
+
+ return 0;
+}
+
+static void s32g_pcie_phy_write(struct s32g_serdes *serdes, u32 reg, u32 val)
+{
+ writel(PHY_REG_EN, serdes->pcie.phy_base + S32G_PHY_REG_ADDR);
+ writel(reg | PHY_REG_EN, serdes->pcie.phy_base + S32G_PHY_REG_ADDR);
+ usleep_range(S32G_PHY_REG_SETTLE_MIN_US, S32G_PHY_REG_SETTLE_MAX_US);
+ writel(val, serdes->pcie.phy_base + S32G_PHY_REG_DATA);
+ usleep_range(S32G_PHY_REG_SETTLE_MIN_US, S32G_PHY_REG_SETTLE_MAX_US);
+ writel(0, serdes->pcie.phy_base + S32G_PHY_REG_ADDR);
+}
+
+static void s32g_pcie_phy_iq_ovrd(struct s32g_serdes *serdes, u32 reg)
+{
+ u32 val = FIELD_PREP(RX_EQ_DELTA_IQ_OVRD_VAL, S32G_PCIE_RX_EQ_DELTA_IQ);
+
+ s32g_pcie_phy_write(serdes, reg, val);
+ s32g_pcie_phy_write(serdes, reg, val | RX_EQ_DELTA_IQ_OVRD_EN);
+}
+
+static int s32g_pcie_phy_power_on(struct s32g_serdes *serdes)
+{
+ struct s32g_pcie_ctrl *pcie = &serdes->pcie;
+ struct s32g_serdes_ctrl *ctrl = &serdes->ctrl;
+ int ret;
+
+ ret = s32g_pcie_phy_power_on_common(serdes);
+ if (ret)
+ return ret;
+
+ s32g_pcie_phy_iq_ovrd(serdes, RAWLANE0_DIG_PCS_XF_RX_EQ_DELTA_IQ_OVRD_IN);
+ if (ctrl->ss_mode == 0)
+ s32g_pcie_phy_iq_ovrd(serdes,
+ RAWLANE1_DIG_PCS_XF_RX_EQ_DELTA_IQ_OVRD_IN);
+
+ pcie->powered_on = true;
+
+ return 0;
+}
+
+static int s32g_serdes_phy_power_on(struct phy *p)
+{
+ struct s32g_serdes *serdes = phy_get_drvdata(p);
+
+ return s32g_pcie_phy_power_on(serdes);
+}
+
+static int s32g_serdes_phy_power_off(struct phy *p)
+{
+ struct s32g_serdes *serdes = phy_get_drvdata(p);
+
+ serdes->pcie.powered_on = false;
+
+ return 0;
+}
+
+static bool is_pcie_phy_mode_valid(int mode)
+{
+ switch (mode) {
+ case CRNS:
+ case CRSS:
+ case SRNS:
+ case SRIS:
+ return true;
+ default:
+ return false;
+ }
+}
+
+static int s32g_serdes_phy_set_mode_ext(struct phy *p,
+ enum phy_mode mode, int submode)
+{
+ struct s32g_serdes *serdes = phy_get_drvdata(p);
+
+ if (mode != PHY_MODE_PCIE)
+ return -EINVAL;
+
+ if (!is_pcie_phy_mode_valid(submode))
+ return -EINVAL;
+
+ /*
+ * Spread spectrum clocking needs the external reference and cannot
+ * be combined with an SGMII lane.
+ */
+ if ((submode == CRSS || submode == SRIS) &&
+ (serdes->ctrl.ss_mode != 0 || !serdes->ctrl.ext_clk))
+ return -EINVAL;
+
+ /* The reference clock architecture is applied at power on */
+ if (serdes->pcie.powered_on)
+ return -EBUSY;
+
+ serdes->pcie.phy_mode = submode;
+
+ return 0;
+}
+
+static const struct phy_ops serdes_pcie_ops = {
+ .owner = THIS_MODULE,
+ .power_on = s32g_serdes_phy_power_on,
+ .power_off = s32g_serdes_phy_power_off,
+ .set_mode = s32g_serdes_phy_set_mode_ext,
+};
+
+static int s32g_serdes_xpcs_setup(struct s32g_serdes *serdes, u32 instance)
+{
+ struct s32g_serdes_ctrl *ctrl = &serdes->ctrl;
+ struct s32g_xpcs_ctrl *xpcs_ctrl = &serdes->xpcs;
+ enum s32g_xpcs_shared shared = S32G_NOT_SHARED;
+ struct device *dev = serdes->dev;
+ struct s32g_xpcs *xpcs;
+
+ if (ctrl->ss_mode == 1 || ctrl->ss_mode == 2)
+ shared = S32G_PCIE_XPCS_1G;
+
+ xpcs = s32g_xpcs_create(dev, instance, xpcs_ctrl->base[instance],
+ ctrl->ext_clk, ctrl->ref_clk_rate, shared);
+ if (IS_ERR(xpcs))
+ return PTR_ERR(xpcs);
+
+ xpcs_ctrl->phys[instance] = xpcs;
+
+ return 0;
+}
+
+static int s32g_serdes_init_xpcs(struct s32g_serdes *serdes)
+{
+ struct s32g_serdes_ctrl *ctrl = &serdes->ctrl;
+ struct s32g_xpcs_ctrl *xpcs = &serdes->xpcs;
+ struct s32g_xpcs *order[2];
+ size_t i;
+ int ret;
+
+ /*
+ * In mode 3, XPCS0 controls the PHY common signals, so the RM has
+ * XPCS1 configured and reset first.
+ */
+ switch (ctrl->ss_mode) {
+ case 0:
+ return 0;
+ case 1:
+ order[0] = xpcs->phys[0];
+ order[1] = NULL;
+ break;
+ case 2:
+ order[0] = xpcs->phys[1];
+ order[1] = NULL;
+ break;
+ case 3:
+ order[0] = xpcs->phys[1];
+ order[1] = xpcs->phys[0];
+ break;
+ default:
+ return -EINVAL;
+ }
+
+ for (i = 0; i < ARRAY_SIZE(order); i++) {
+ if (!order[i])
+ continue;
+
+ ret = s32g_xpcs_init_plls(order[i]);
+ if (ret)
+ return ret;
+ }
+
+ for (i = 0; i < ARRAY_SIZE(order); i++) {
+ if (!order[i])
+ continue;
+
+ s32g_xpcs_vreset(order[i]);
+ }
+
+ for (i = 0; i < ARRAY_SIZE(order); i++) {
+ if (!order[i])
+ continue;
+
+ ret = s32g_xpcs_wait_vreset(order[i]);
+ if (ret)
+ return ret;
+
+ ret = s32g_xpcs_reset_rx(order[i]);
+ if (ret)
+ return ret;
+
+ s32g_xpcs_disable_an(order[i]);
+ }
+
+ return 0;
+}
+
+static int s32g_serdes_assert_reset(struct s32g_serdes *serdes)
+{
+ struct device *dev = serdes->dev;
+ int ret;
+
+ ret = reset_control_assert(serdes->pcie.rst);
+ if (ret) {
+ dev_err(dev, "Failed to assert PCIE reset: %d\n", ret);
+ return ret;
+ }
+
+ ret = reset_control_assert(serdes->ctrl.rst);
+ if (ret) {
+ dev_err(dev, "Failed to assert SerDes reset: %d\n", ret);
+ return ret;
+ }
+
+ return 0;
+}
+
+static int s32g_serdes_deassert_reset(struct s32g_serdes *serdes)
+{
+ struct device *dev = serdes->dev;
+ int ret;
+
+ ret = reset_control_deassert(serdes->pcie.rst);
+ if (ret) {
+ dev_err(dev, "Failed to deassert PCIE reset: %d\n", ret);
+ return ret;
+ }
+
+ ret = reset_control_deassert(serdes->ctrl.rst);
+ if (ret) {
+ dev_err(dev, "Failed to deassert SerDes reset: %d\n", ret);
+ return ret;
+ }
+
+ return 0;
+}
+
+static bool s32g_serdes_lane_matches(const struct s32g_lane_desc *mode_lane,
+ const struct s32g_lane_desc *req_lane)
+{
+ /* A lane without a consumer takes the function of the first match */
+ if (req_lane->fn == S32G_LANE_UNUSED)
+ return true;
+
+ if (mode_lane->fn != req_lane->fn)
+ return false;
+
+ if (mode_lane->fn == S32G_LANE_XPCS)
+ return mode_lane->xpcs_instance == req_lane->xpcs_instance;
+
+ return true;
+}
+
+static const char *s32g_lane_fn_str(const struct s32g_lane_desc *lane)
+{
+ switch (lane->fn) {
+ case S32G_LANE_PCIE:
+ return "PCIe";
+ case S32G_LANE_XPCS:
+ return lane->xpcs_instance ? "XPCS1" : "XPCS0";
+ default:
+ return "unused";
+ }
+}
+
+static int s32g_serdes_derive_mode(struct s32g_serdes *serdes)
+{
+ const struct s32g_serdes_mode *match = NULL;
+ unsigned int i;
+ bool refclk_ok = false;
+
+ if (serdes->lane[0].fn == S32G_LANE_UNUSED &&
+ serdes->lane[1].fn == S32G_LANE_UNUSED)
+ return dev_err_probe(serdes->dev, -EINVAL,
+ "No consumer references this SerDes\n");
+
+ for (i = 0; i < serdes->soc_data->n_modes; i++) {
+ const struct s32g_serdes_mode *m = &serdes->soc_data->modes[i];
+
+ if (s32g_serdes_lane_matches(&m->lane[0], &serdes->lane[0]) &&
+ s32g_serdes_lane_matches(&m->lane[1], &serdes->lane[1])) {
+ match = m;
+ break;
+ }
+ }
+
+ if (!match)
+ return dev_err_probe(serdes->dev, -EINVAL,
+ "No SerDes mode matches the lane configuration\n");
+
+ for (i = 0; i < ARRAY_SIZE(match->refclk) && match->refclk[i]; i++) {
+ if (serdes->ctrl.ref_clk_rate == match->refclk[i]) {
+ refclk_ok = true;
+ break;
+ }
+ }
+
+ if (!refclk_ok)
+ return dev_err_probe(serdes->dev, -EINVAL,
+ "Reference clock %lu Hz invalid for mode %u\n",
+ serdes->ctrl.ref_clk_rate, match->submode);
+
+ serdes->ctrl.ss_mode = match->submode;
+
+ dev_info(serdes->dev,
+ "SerDes mode %u selected: lane0=%s lane1=%s, ref-clk %lu MHz (%s)\n",
+ match->submode,
+ s32g_lane_fn_str(&serdes->lane[0]),
+ s32g_lane_fn_str(&serdes->lane[1]),
+ serdes->ctrl.ref_clk_rate / HZ_PER_MHZ,
+ serdes->ctrl.ext_clk ? "external" : "internal");
+
+ return 0;
+}
+
+static int s32g_serdes_init(struct s32g_serdes *serdes)
+{
+ struct s32g_serdes_ctrl *ctrl = &serdes->ctrl;
+ u32 reg0;
+ int ret;
+
+ ret = clk_bulk_prepare_enable(ctrl->nclks, ctrl->clks);
+ if (ret) {
+ dev_err(serdes->dev, "Failed to enable SerDes clocks\n");
+ return ret;
+ }
+
+ ret = s32g_serdes_assert_reset(serdes);
+ if (ret)
+ goto disable_clks;
+
+ reg0 = readl(ctrl->ss_base + S32G_SS_RW_REG_0);
+ reg0 &= ~SUBMODE_MASK;
+ reg0 |= FIELD_PREP(SUBMODE_MASK, ctrl->ss_mode);
+ writel(reg0, ctrl->ss_base + S32G_SS_RW_REG_0);
+
+ reg0 = readl(ctrl->ss_base + S32G_SS_RW_REG_0);
+ if (ctrl->ext_clk)
+ reg0 &= ~CLKEN_MASK;
+ else
+ reg0 |= CLKEN_MASK;
+
+ writel(reg0, ctrl->ss_base + S32G_SS_RW_REG_0);
+
+ /*
+ * Route the reference before the XPCS bring-up: with the wrong one the
+ * PMA never powers up and VR_RST never clears.
+ */
+ reg0 = readl(ctrl->ss_base + S32G_PCIE_PHY_GEN_CTRL);
+ if (ctrl->ext_clk)
+ reg0 |= REF_USE_PAD;
+ else
+ reg0 &= ~REF_USE_PAD;
+ writel(reg0, ctrl->ss_base + S32G_PCIE_PHY_GEN_CTRL);
+
+ usleep_range(S32G_SS_MODE_SETTLE_MIN_US, S32G_SS_MODE_SETTLE_MAX_US);
+
+ ret = s32g_serdes_deassert_reset(serdes);
+ if (ret)
+ goto disable_clks;
+
+ ret = s32g_serdes_wait_phy_operational(serdes);
+ if (ret)
+ goto disable_clks;
+
+ ret = s32g_serdes_init_xpcs(serdes);
+ if (ret) {
+ dev_err(serdes->dev, "XPCS init failed\n");
+ goto disable_clks;
+ }
+
+ ctrl->enabled = true;
+
+ return 0;
+
+disable_clks:
+ s32g_serdes_assert_reset(serdes);
+ clk_bulk_disable_unprepare(serdes->ctrl.nclks,
+ serdes->ctrl.clks);
+
+ return ret;
+}
+
+static void s32g_serdes_disable(void *data)
+{
+ struct s32g_serdes *serdes = data;
+
+ s32g_serdes_assert_reset(serdes);
+
+ if (serdes->ctrl.enabled) {
+ clk_bulk_disable_unprepare(serdes->ctrl.nclks,
+ serdes->ctrl.clks);
+ serdes->ctrl.enabled = false;
+ }
+}
+
+static int s32g_serdes_get_ctrl_resources(struct platform_device *pdev,
+ struct s32g_serdes *serdes)
+{
+ struct s32g_serdes_ctrl *ctrl = &serdes->ctrl;
+ struct device *dev = &pdev->dev;
+ int ret, idx, i;
+
+ ctrl->ss_base = devm_platform_ioremap_resource_byname(pdev, "ss-pcie");
+ if (IS_ERR(ctrl->ss_base))
+ return dev_err_probe(dev, PTR_ERR(ctrl->ss_base),
+ "Failed to map 'ss-pcie'\n");
+
+ ctrl->rst = devm_reset_control_get_exclusive(dev, "serdes");
+ if (IS_ERR(ctrl->rst))
+ return dev_err_probe(dev, PTR_ERR(ctrl->rst),
+ "Failed to get 'serdes' reset control\n");
+
+ ctrl->nclks = devm_clk_bulk_get_all(dev, &ctrl->clks);
+ if (ctrl->nclks < 1) {
+ ret = ctrl->nclks ? : -EINVAL;
+ return dev_err_probe(dev, ret,
+ "Failed to get SerDes clocks\n");
+ }
+
+ idx = -1;
+ for (i = 0; i < ctrl->nclks; i++) {
+ if (!ctrl->clks[i].id)
+ continue;
+
+ if (!strcmp(ctrl->clks[i].id, EXTERNAL_CLK_NAME)) {
+ idx = i;
+ ctrl->ext_clk = true;
+ break;
+ }
+
+ if (!strcmp(ctrl->clks[i].id, INTERNAL_CLK_NAME))
+ idx = i;
+ }
+
+ if (idx < 0) {
+ dev_err(dev, "Failed to get Phy reference clock source\n");
+ return -EINVAL;
+ }
+
+ ctrl->ref_clk_rate = clk_get_rate(ctrl->clks[idx].clk);
+ if (!ctrl->ref_clk_rate) {
+ dev_err(dev, "Failed to get Phy reference clock rate\n");
+ return -EINVAL;
+ }
+
+ return 0;
+}
+
+static int s32g_serdes_get_pcie_resources(struct platform_device *pdev,
+ struct s32g_serdes *serdes)
+{
+ struct s32g_pcie_ctrl *pcie = &serdes->pcie;
+ struct device *dev = &pdev->dev;
+
+ pcie->phy_base = devm_platform_ioremap_resource_byname(pdev,
+ "pcie-phy");
+ if (IS_ERR(pcie->phy_base))
+ return dev_err_probe(dev, PTR_ERR(pcie->phy_base),
+ "Failed to map 'pcie-phy'\n");
+
+ pcie->rst = devm_reset_control_get_exclusive(dev, "pcie");
+ if (IS_ERR(pcie->rst))
+ return dev_err_probe(dev, PTR_ERR(pcie->rst),
+ "Failed to get 'pcie' reset control\n");
+
+ return 0;
+}
+
+static int s32g_serdes_get_xpcs_resources(struct platform_device *pdev,
+ struct s32g_serdes *serdes)
+{
+ struct s32g_xpcs_ctrl *xpcs = &serdes->xpcs;
+ struct device *dev = &pdev->dev;
+
+ xpcs->base[0] = devm_platform_ioremap_resource_byname(pdev, "xpcs0");
+ if (IS_ERR(xpcs->base[0]))
+ return dev_err_probe(dev, PTR_ERR(xpcs->base[0]),
+ "Failed to map 'xpcs0'\n");
+
+ xpcs->base[1] = devm_platform_ioremap_resource_byname(pdev, "xpcs1");
+ if (IS_ERR(xpcs->base[1]))
+ return dev_err_probe(dev, PTR_ERR(xpcs->base[1]),
+ "Failed to map 'xpcs1'\n");
+
+ return 0;
+}
+
+/* Walk up to the root: a consumer below a disabled node is disabled too */
+static bool s32g_serdes_consumer_available(struct device_node *np)
+{
+ struct device_node *parent;
+
+ for (np = of_node_get(np); np; np = parent) {
+ if (!of_device_is_available(np)) {
+ of_node_put(np);
+ return false;
+ }
+ parent = of_get_parent(np);
+ of_node_put(np);
+ }
+
+ return true;
+}
+
+static int s32g_serdes_add_ref(struct s32g_serdes *serdes,
+ struct device_node *consumer,
+ const struct of_phandle_args *args)
+{
+ struct device *dev = serdes->dev;
+ struct s32g_lane_desc *desc;
+ u32 lane, type, instance, speed;
+
+ if (args->args_count != S32G_SERDES_PHY_CELLS)
+ return dev_err_probe(dev, -EINVAL,
+ "%pOF: phys needs %d cells\n",
+ consumer, S32G_SERDES_PHY_CELLS);
+
+ lane = args->args[0];
+ type = args->args[1];
+ instance = args->args[2];
+ speed = args->args[3];
+
+ if (lane >= ARRAY_SIZE(serdes->lane))
+ return dev_err_probe(dev, -EINVAL, "%pOF: invalid lane %u\n",
+ consumer, lane);
+
+ desc = &serdes->lane[lane];
+ if (desc->fn != S32G_LANE_UNUSED)
+ return dev_err_probe(dev, -EINVAL,
+ "%pOF: lane %u referenced twice\n",
+ consumer, lane);
+
+ switch (type) {
+ case PHY_TYPE_PCIE:
+ if (instance || speed)
+ return dev_err_probe(dev, -EINVAL,
+ "%pOF: PCIe takes no instance or speed\n",
+ consumer);
+ desc->fn = S32G_LANE_PCIE;
+ return 0;
+ case PHY_TYPE_SGMII:
+ if (instance >= S32G_SERDES_XPCS_MAX)
+ return dev_err_probe(dev, -EINVAL,
+ "%pOF: invalid XPCS instance %u\n",
+ consumer, instance);
+ if (speed == S32G_SERDES_LANE_SPEED_2G5)
+ return dev_err_probe(dev, -EINVAL,
+ "%pOF: lane %u: 2.5G is not supported yet\n",
+ consumer, lane);
+ if (speed != S32G_SERDES_LANE_SPEED_1G)
+ return dev_err_probe(dev, -EINVAL,
+ "%pOF: lane %u: invalid speed %u\n",
+ consumer, lane, speed);
+ desc->fn = S32G_LANE_XPCS;
+ desc->xpcs_instance = instance;
+ return 0;
+ default:
+ return dev_err_probe(dev, -EINVAL,
+ "%pOF: unsupported PHY type %u\n",
+ consumer, type);
+ }
+}
+
+/*
+ * The consumers describe the lane routing through their phys references.
+ * SUBSYS_MODE is written before the reset release, long before they probe,
+ * so collect them here.
+ */
+static int s32g_serdes_scan_consumers(struct s32g_serdes *serdes)
+{
+ struct device_node *np = serdes->dev->of_node;
+ struct device_node *consumer;
+ struct of_phandle_args args;
+ int i, count, ret;
+
+ for_each_node_with_property(consumer, "phys") {
+ if (!s32g_serdes_consumer_available(consumer))
+ continue;
+
+ count = of_count_phandle_with_args(consumer, "phys",
+ "#phy-cells");
+ for (i = 0; i < count; i++) {
+ ret = of_parse_phandle_with_args(consumer, "phys",
+ "#phy-cells", i,
+ &args);
+ if (ret)
+ continue;
+
+ ret = 0;
+ if (args.np == np)
+ ret = s32g_serdes_add_ref(serdes, consumer,
+ &args);
+ of_node_put(args.np);
+ if (ret) {
+ of_node_put(consumer);
+ return ret;
+ }
+ }
+ }
+
+ return 0;
+}
+
+static const struct phy_ops serdes_xpcs_lane_ops = {
+ .owner = THIS_MODULE,
+};
+
+/*
+ * One PHY for the PCIe link, whatever its width, and one per XPCS instance
+ * in use. The XPCS lane PHY has no operations: the lane is brought up with
+ * the SerDes, and the MAC drives it through the PCS.
+ */
+static int s32g_serdes_create_phys(struct s32g_serdes *serdes)
+{
+ struct device *dev = serdes->dev;
+ struct phy *phy;
+ unsigned int i;
+
+ for (i = 0; i < ARRAY_SIZE(serdes->lane); i++) {
+ const struct s32g_lane_desc *lane = &serdes->lane[i];
+
+ switch (lane->fn) {
+ case S32G_LANE_PCIE:
+ if (serdes->pcie.phy)
+ break;
+
+ phy = devm_phy_create(dev, NULL, &serdes_pcie_ops);
+ if (IS_ERR(phy))
+ return PTR_ERR(phy);
+
+ phy_set_drvdata(phy, serdes);
+ phy->attrs.mode = PHY_MODE_PCIE;
+ serdes->pcie.phy = phy;
+ break;
+ case S32G_LANE_XPCS:
+ phy = devm_phy_create(dev, NULL, &serdes_xpcs_lane_ops);
+ if (IS_ERR(phy))
+ return PTR_ERR(phy);
+
+ phy_set_drvdata(phy, serdes);
+ serdes->xpcs.lane_phy[lane->xpcs_instance] = phy;
+ break;
+ default:
+ break;
+ }
+ }
+
+ return 0;
+}
+
+static struct phy *s32g_serdes_xlate(struct device *dev,
+ const struct of_phandle_args *args)
+{
+ struct s32g_serdes *serdes = dev_get_drvdata(dev);
+ const struct s32g_lane_desc *lane;
+ u32 instance;
+
+ if (args->args_count != S32G_SERDES_PHY_CELLS ||
+ args->args[0] >= ARRAY_SIZE(serdes->lane))
+ return ERR_PTR(-EINVAL);
+
+ lane = &serdes->lane[args->args[0]];
+ instance = args->args[2];
+
+ switch (args->args[1]) {
+ case PHY_TYPE_PCIE:
+ if (lane->fn != S32G_LANE_PCIE || !serdes->pcie.phy)
+ return ERR_PTR(-ENODEV);
+ return serdes->pcie.phy;
+ case PHY_TYPE_SGMII:
+ if (lane->fn != S32G_LANE_XPCS ||
+ instance >= S32G_SERDES_XPCS_MAX ||
+ lane->xpcs_instance != instance ||
+ !serdes->xpcs.lane_phy[instance])
+ return ERR_PTR(-ENODEV);
+ return serdes->xpcs.lane_phy[instance];
+ default:
+ return ERR_PTR(-EINVAL);
+ }
+}
+
+static int s32g_serdes_setup_xpcs_lanes(struct s32g_serdes *serdes)
+{
+ unsigned int i;
+ int ret;
+
+ for (i = 0; i < ARRAY_SIZE(serdes->lane); i++) {
+ if (serdes->lane[i].fn != S32G_LANE_XPCS)
+ continue;
+
+ ret = s32g_serdes_xpcs_setup(serdes,
+ serdes->lane[i].xpcs_instance);
+ if (ret)
+ return ret;
+ }
+
+ return 0;
+}
+
+static int s32g_serdes_probe(struct platform_device *pdev)
+{
+ struct device *dev = &pdev->dev;
+ const struct s32g_serdes_soc_data *soc_data;
+ struct phy_provider *phy_provider;
+ struct s32g_serdes *serdes;
+ int ret;
+
+ serdes = devm_kzalloc(dev, sizeof(*serdes), GFP_KERNEL);
+ if (!serdes)
+ return -ENOMEM;
+
+ serdes->dev = dev;
+
+ soc_data = device_get_match_data(dev);
+ if (!soc_data)
+ return -EINVAL;
+
+ serdes->soc_data = soc_data;
+
+ ret = s32g_serdes_get_ctrl_resources(pdev, serdes);
+ if (ret)
+ return ret;
+
+ ret = s32g_serdes_get_pcie_resources(pdev, serdes);
+ if (ret)
+ return ret;
+
+ ret = s32g_serdes_get_xpcs_resources(pdev, serdes);
+ if (ret)
+ return ret;
+
+ ret = s32g_serdes_scan_consumers(serdes);
+ if (ret)
+ return ret;
+
+ ret = s32g_serdes_derive_mode(serdes);
+ if (ret)
+ return ret;
+
+ ret = s32g_serdes_setup_xpcs_lanes(serdes);
+ if (ret)
+ return ret;
+
+ ret = s32g_serdes_init(serdes);
+ if (ret)
+ return ret;
+
+ ret = devm_add_action_or_reset(dev, s32g_serdes_disable, serdes);
+ if (ret)
+ return ret;
+
+ ret = s32g_serdes_create_phys(serdes);
+ if (ret)
+ return ret;
+
+ /*
+ * drvdata only now: s32g_serdes_pcs_create() looks it up before the
+ * provider exists and must defer until the PLLs are up.
+ */
+ platform_set_drvdata(pdev, serdes);
+
+ phy_provider = devm_of_phy_provider_register(dev, s32g_serdes_xlate);
+ if (IS_ERR(phy_provider))
+ return PTR_ERR(phy_provider);
+
+ return 0;
+}
+
+static int s32g_serdes_suspend(struct device *device)
+{
+ struct s32g_serdes *serdes = dev_get_drvdata(device);
+
+ if (serdes->ctrl.enabled) {
+ clk_bulk_disable_unprepare(serdes->ctrl.nclks,
+ serdes->ctrl.clks);
+ serdes->ctrl.enabled = false;
+ }
+
+ return 0;
+}
+
+static int s32g_serdes_resume(struct device *device)
+{
+ struct s32g_serdes *serdes = dev_get_drvdata(device);
+ struct s32g_pcie_ctrl *pcie = &serdes->pcie;
+ int ret;
+
+ ret = s32g_serdes_init(serdes);
+ if (ret) {
+ dev_err(device, "Failed to initialize\n");
+ return ret;
+ }
+
+ if (pcie->powered_on) {
+ ret = s32g_pcie_phy_power_on(serdes);
+ if (ret)
+ dev_err(device, "Failed to power-on PCIe phy\n");
+ }
+
+ return ret;
+}
+
+/**
+ * s32g_serdes_pcs_create() - get the phylink PCS behind a MAC's SerDes lane
+ * @dev: the MAC device, consumer of the device link
+ * @np: the MAC's node, carrying pcs-handle and the phys reference to the
+ * SerDes, <lane PHY_TYPE_SGMII instance max-speed>
+ *
+ * Return: the phylink_pcs of the XPCS instance the MAC references,
+ * -EPROBE_DEFER until the SerDes has probed, -ENODEV if the instance is not
+ * in use, or another ERR_PTR().
+ */
+struct phylink_pcs *s32g_serdes_pcs_create(struct device *dev,
+ struct device_node *np)
+{
+ struct platform_device *pdev;
+ struct device_node *pcs_np;
+ struct of_phandle_args args;
+ struct s32g_serdes *serdes;
+ struct device_link *link;
+ u32 instance = S32G_SERDES_XPCS_MAX;
+ int i, count;
+
+ pcs_np = of_parse_phandle(np, "pcs-handle", 0);
+ if (!pcs_np)
+ return ERR_PTR(-ENODEV);
+
+ /* The XPCS instance is in the MAC's phys reference to the SerDes */
+ count = of_count_phandle_with_args(np, "phys", "#phy-cells");
+ for (i = 0; i < count; i++) {
+ if (of_parse_phandle_with_args(np, "phys", "#phy-cells", i,
+ &args))
+ continue;
+
+ if (args.np == pcs_np &&
+ args.args_count == S32G_SERDES_PHY_CELLS &&
+ args.args[1] == PHY_TYPE_SGMII)
+ instance = args.args[2];
+ of_node_put(args.np);
+
+ if (instance < S32G_SERDES_XPCS_MAX)
+ break;
+ }
+
+ if (instance >= S32G_SERDES_XPCS_MAX) {
+ of_node_put(pcs_np);
+ return ERR_PTR(-EINVAL);
+ }
+
+ if (!of_device_is_available(pcs_np)) {
+ of_node_put(pcs_np);
+ return ERR_PTR(-ENODEV);
+ }
+
+ pdev = of_find_device_by_node(pcs_np);
+ of_node_put(pcs_np);
+ if (!pdev)
+ return ERR_PTR(-EPROBE_DEFER);
+
+ serdes = platform_get_drvdata(pdev);
+ if (!serdes) {
+ put_device(&pdev->dev);
+ return ERR_PTR(-EPROBE_DEFER);
+ }
+
+ /* Probe is complete: an instance not set up now never will be */
+ if (!serdes->xpcs.phys[instance]) {
+ put_device(&pdev->dev);
+ return ERR_PTR(-ENODEV);
+ }
+
+ link = device_link_add(dev, &pdev->dev, DL_FLAG_AUTOREMOVE_CONSUMER);
+ put_device(&pdev->dev);
+ if (!link)
+ return ERR_PTR(-EINVAL);
+
+ return s32g_xpcs_pcs(serdes->xpcs.phys[instance]);
+}
+EXPORT_SYMBOL_GPL(s32g_serdes_pcs_create);
+
+static const struct of_device_id s32g_serdes_match[] = {
+ {
+ .compatible = "nxp,s32g2-serdes",
+ .data = &s32g2_serdes_soc_data,
+ },
+ {
+ .compatible = "nxp,s32g3-serdes",
+ .data = &s32g3_serdes_soc_data,
+ },
+ { /* sentinel */ }
+};
+MODULE_DEVICE_TABLE(of, s32g_serdes_match);
+
+/*
+ * noirq: the SerDes must be back before the PCIe controller's noirq resume
+ * powers its PHY.
+ */
+static const struct dev_pm_ops s32g_serdes_pm_ops = {
+ NOIRQ_SYSTEM_SLEEP_PM_OPS(s32g_serdes_suspend,
+ s32g_serdes_resume)
+};
+
+static struct platform_driver s32g_serdes_driver = {
+ .probe = s32g_serdes_probe,
+ .driver = {
+ .name = "phy-s32g-serdes",
+ .of_match_table = s32g_serdes_match,
+ /*
+ * The PCIe controller holds the PHY through a stateless link;
+ * do not allow unbinding under it.
+ */
+ .suppress_bind_attrs = true,
+ .pm = pm_sleep_ptr(&s32g_serdes_pm_ops),
+ },
+};
+module_platform_driver(s32g_serdes_driver);
+
+MODULE_AUTHOR("Ghennadi Procopciuc <ghennadi.procopciuc@xxxxxxx>");
+MODULE_DESCRIPTION("NXP S32G SerDes driver");
+MODULE_LICENSE("GPL");
diff --git a/include/linux/phy/nxp-s32g-serdes.h b/include/linux/phy/nxp-s32g-serdes.h
new file mode 100644
index 000000000000..d14fcde1d933
--- /dev/null
+++ b/include/linux/phy/nxp-s32g-serdes.h
@@ -0,0 +1,25 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Copyright 2021-2026 NXP
+ */
+#ifndef PHY_NXP_S32G_SERDES_H
+#define PHY_NXP_S32G_SERDES_H
+
+#include <linux/err.h>
+
+struct device;
+struct device_node;
+struct phylink_pcs;
+
+#if IS_REACHABLE(CONFIG_PHY_S32G_SERDES)
+struct phylink_pcs *s32g_serdes_pcs_create(struct device *dev,
+ struct device_node *np);
+#else
+static inline struct phylink_pcs *
+s32g_serdes_pcs_create(struct device *dev, struct device_node *np)
+{
+ return ERR_PTR(-ENODEV);
+}
+#endif
+
+#endif /* PHY_NXP_S32G_SERDES_H */
--
2.55.0