Linux-ARM-Kernel Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Maxime Chevallier <maxime.chevallier@bootlin.com>
To: davem@davemloft.net, "Andrew Lunn" <andrew+netdev@lunn.ch>,
	"Jakub Kicinski" <kuba@kernel.org>,
	"Eric Dumazet" <edumazet@kernel.org>,
	"Willem de Bruijn" <willemb@google.com>,
	"Paolo Abeni" <pabeni@redhat.com>,
	"Russell King" <linux@armlinux.org.uk>,
	"Marek Behún" <kabel@kernel.org>,
	"Sebastian Hesselbarth" <sebastian.hesselbarth@gmail.com>,
	"Gregory Clement" <gregory.clement@bootlin.com>,
	"Heiner Kallweit" <hkallweit1@gmail.com>
Cc: "Maxime Chevallier" <maxime.chevallier@bootlin.com>,
	netdev@vger.kernel.org, linux-kernel@vger.kernel.org,
	linux-arm-kernel@lists.infradead.org,
	thomas.petazzoni@bootlin.com, devicetree@vger.kernel.org,
	"Conor Dooley" <conor+dt@kernel.org>,
	"Krzysztof Kozlowski" <krzk+dt@kernel.org>,
	"Rob Herring" <robh@kernel.org>,
	"Christophe Leroy" <christophe.leroy@csgroup.eu>,
	"Herve Codina" <herve.codina@bootlin.com>,
	"Florian Fainelli" <f.fainelli@gmail.com>,
	"Vladimir Oltean" <vladimir.oltean@nxp.com>,
	"Köry Maincent" <kory.maincent@bootlin.com>,
	"Oleksij Rempel" <o.rempel@pengutronix.de>,
	"Nicolò Veronese" <nicveronese@gmail.com>,
	"Simon Horman" <horms@kernel.org>,
	marcin.s.wojtas@gmail.com, "Daniel Golle" <daniel@makrotopia.org>,
	"Dimitri Fedrau" <dimitri.fedrau@liebherr.com>,
	"Frank Wunderlich" <frank.wunderlich@linux.dev>,
	"Pietro Ameruoso" <p.ameruoso@live.it>,
	"Aleksei Sviridkin" <f@lex.la>
Subject: [PATCH net-next 02/12] net: phy: Introduce phy_mux, a MII multiplexer representation
Date: Fri,  4 Dec 2026 12:06:11 +0100	[thread overview]
Message-ID: <20261204110622.1106492-3-maxime.chevallier@bootlin.com> (raw)
In-Reply-To: <20261204110622.1106492-1-maxime.chevallier@bootlin.com>

The implementation of combo-port and multi-PHY interfaces can be done
either by having the MAC's MII interface muxed to 2 different
destinations, or by having a PHY with 2 outputs.

Let's introduce a framework to deal with the first case: muxing the MII
bus. Here again, we can mux it with different techniques:

 - A Mux block in the SoC, after the MAC, MMIO-mapped like we have on
   banana Pi R4 for example

 - A gpio-driven Mux, can be some high speed analog switch for example.
   Here again, we have 2 options:
    - The switch can be controlled by Linux
    - The switch is controlled through another means, that's the case
      for Turris Omnia for example, the control pin is tied to the SFP
      cage's mod_def0 pin that indicates if a module is present

 - No mux at all, the mux is just the MII going to 2 PHYs, one of them
   needing to isolate its MII.

The plan is to support all these cases, with the uAPI interface being
phy_port as it'll also allow supporting phy-driven muxes.

Introduce the framework for the muxes, named phy_mux, for now without
users, they'll be added in the next commits.

The mux is represented in DT, and is parsed using the fwnode logic.

A mux has multiple "ports" represented by "struct phy_mux_port", that
correspond to the PHY-side MII busses, only one of these can be active
at any given time.

Signed-off-by: Maxime Chevallier <maxime.chevallier@bootlin.com>
---
 MAINTAINERS               |   2 +
 drivers/net/phy/Kconfig   |   8 ++
 drivers/net/phy/Makefile  |   1 +
 drivers/net/phy/phy_mux.c | 189 ++++++++++++++++++++++++++++++++++++++
 include/linux/phy_mux.h   |  44 +++++++++
 5 files changed, 244 insertions(+)
 create mode 100644 drivers/net/phy/phy_mux.c
 create mode 100644 include/linux/phy_mux.h

diff --git a/MAINTAINERS b/MAINTAINERS
index 8685ed44840a..b2b02a1dbd3a 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -10086,6 +10086,8 @@ M:	Maxime Chevallier <maxime.chevallier@bootlin.com>
 L:	netdev@vger.kernel.org
 S:	Maintained
 F:	Documentation/devicetree/bindings/net/mii-mux*.yaml
+F:	drivers/net/phy/phy_mux.c
+F:	include/linux/phy_mux.h
 
 EXEC & BINFMT API, ELF
 M:	Kees Cook <kees@kernel.org>
diff --git a/drivers/net/phy/Kconfig b/drivers/net/phy/Kconfig
index 30e9d22c8d1a..d8a0131d2aa2 100644
--- a/drivers/net/phy/Kconfig
+++ b/drivers/net/phy/Kconfig
@@ -21,6 +21,14 @@ menuconfig PHYLIB
 
 if PHYLIB
 
+config PHY_MUX
+	bool "PHY multiplexing support"
+	help
+	  Some boards route several PHYs or SFP cages to the same MAC
+	  through a multiplexer, only one of them being usable at a
+	  time. This adds the infrastructure to represent such multiplexers
+	  and to select which port carries the link.
+
 config SWPHY
 	bool
 
diff --git a/drivers/net/phy/Makefile b/drivers/net/phy/Makefile
index 579066a638f0..b3ec2933fec4 100644
--- a/drivers/net/phy/Makefile
+++ b/drivers/net/phy/Makefile
@@ -5,6 +5,7 @@ libphy-y			:= phy.o phy-c45.o phy-core.o phy_device.o \
 				   linkmode.o phy_link_topology.o \
 				   phy_caps.o mdio_bus_provider.o phy_port.o \
 				   mdio_device.o
+libphy-$(CONFIG_PHY_MUX)	+= phy_mux.o
 
 ifdef CONFIG_PHYLIB
 # built-in whenever PHYLIB is built-in or module
diff --git a/drivers/net/phy/phy_mux.c b/drivers/net/phy/phy_mux.c
new file mode 100644
index 000000000000..e259eb4e271a
--- /dev/null
+++ b/drivers/net/phy/phy_mux.c
@@ -0,0 +1,189 @@
+// SPDX-License-Identifier: GPL-2.0+
+
+#include <linux/device.h>
+#include <linux/list.h>
+#include <linux/mutex.h>
+#include <linux/phy.h>
+#include <linux/phy_mux.h>
+#include <linux/property.h>
+#include <linux/slab.h>
+#include <linux/xarray.h>
+
+static LIST_HEAD(phy_muxes);
+static DEFINE_MUTEX(phy_mux_mutex);
+
+struct phy_mux {
+	struct list_head node;
+	struct device *dev;
+
+	struct xarray ports;
+	int n_ports;
+	int max_n_ports;
+
+};
+
+/**
+ * phy_mux_alloc() - Allocate a mux
+ * @dev: The mux device
+ * @max_n_ports: How many ports the mux accepts
+ *
+ * The mux is freed with phy_mux_free().
+ *
+ * Returns: the mux, or an ERR_PTR.
+ */
+struct phy_mux *phy_mux_alloc(struct device *dev, int max_n_ports)
+{
+	struct phy_mux *mux;
+
+	mux = kzalloc_obj(*mux);
+	if (!mux)
+		return ERR_PTR(-ENOMEM);
+
+	xa_init_flags(&mux->ports, XA_FLAGS_ALLOC1);
+
+	mux->dev = dev;
+	mux->max_n_ports = max_n_ports;
+
+	return mux;
+}
+EXPORT_SYMBOL_GPL(phy_mux_alloc);
+
+static void phy_mux_port_destroy(struct phy_mux_port *mux_port)
+{
+	switch (mux_port->type) {
+	case PHY_MUX_PORT_PHY:
+		fwnode_handle_put(mux_port->fwnode);
+		break;
+	}
+
+	kfree(mux_port);
+}
+
+/**
+ * phy_mux_register_port() - Add a port to a mux
+ * @mux: The mux, not registered yet
+ * @port: The port, allocated with kzalloc(). The mux owns it from then on
+ *	  and frees it in phy_mux_free().
+ *
+ * Returns: 0 on success, -EBUSY if the mux is full, or a negative error.
+ */
+static int phy_mux_register_port(struct phy_mux *mux,
+				 struct phy_mux_port *port)
+{
+	int ret;
+
+	if (mux->n_ports >= mux->max_n_ports)
+		return -EBUSY;
+
+	ret = xa_alloc(&mux->ports, &port->id, port, xa_limit_16b, GFP_KERNEL);
+	if (ret)
+		return ret;
+
+	mux->n_ports++;
+
+	return 0;
+}
+
+/**
+ * phy_mux_fwnode_add_ports() - Create the ports described under a fwnode
+ * @mux: The mux
+ * @fwnode: The mux device's fwnode, each child being a port with a
+ *	    phy-handle and a phy-mode
+ *
+ * The PHYs themselves are only looked up when the mux gets attached.
+ *
+ * Returns: 0 on success or a negative error.
+ */
+int phy_mux_fwnode_add_ports(struct phy_mux *mux,
+			     const struct fwnode_handle *fwnode)
+{
+	struct fwnode_handle *child, *phy_fwnode;
+	struct phy_mux_port *port;
+	int ret;
+
+	fwnode_for_each_available_child_node(fwnode, child) {
+		phy_fwnode = fwnode_get_phy_node(child);
+		if (IS_ERR(phy_fwnode)) {
+			ret = PTR_ERR(phy_fwnode);
+			goto err;
+		}
+
+		port = kzalloc_obj(*port);
+		if (!port) {
+			fwnode_handle_put(phy_fwnode);
+			ret = -ENOMEM;
+			goto err;
+		}
+
+		port->type = PHY_MUX_PORT_PHY;
+		port->fwnode = phy_fwnode;
+		port->mux = mux;
+
+		ret = fwnode_get_phy_mode(child);
+		if (ret < 0) {
+			phy_mux_port_destroy(port);
+			goto err;
+		}
+		port->interface = ret;
+
+		ret = phy_mux_register_port(mux, port);
+		if (ret) {
+			phy_mux_port_destroy(port);
+			goto err;
+		}
+	}
+
+	return 0;
+
+err:
+	fwnode_handle_put(child);
+	return ret;
+}
+EXPORT_SYMBOL_GPL(phy_mux_fwnode_add_ports);
+
+/**
+ * phy_mux_register() - Make the mux available to its parent
+ * @mux: The mux, with all its ports added
+ *
+ * Returns: 0 on success or a negative error.
+ */
+int phy_mux_register(struct phy_mux *mux)
+{
+	mutex_lock(&phy_mux_mutex);
+	list_add(&mux->node, &phy_muxes);
+	mutex_unlock(&phy_mux_mutex);
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(phy_mux_register);
+
+/**
+ * phy_mux_unregister() - Make the mux unavailable to its parent
+ * @mux: The mux, registered
+ *
+ * The mux still has to be freed with phy_mux_free().
+ */
+void phy_mux_unregister(struct phy_mux *mux)
+{
+	mutex_lock(&phy_mux_mutex);
+	list_del(&mux->node);
+	mutex_unlock(&phy_mux_mutex);
+}
+EXPORT_SYMBOL_GPL(phy_mux_unregister);
+
+/**
+ * phy_mux_free() - Free a mux and its ports
+ * @mux: The mux, not registered
+ */
+void phy_mux_free(struct phy_mux *mux)
+{
+	struct phy_mux_port *port;
+	unsigned long id;
+
+	xa_for_each(&mux->ports, id, port)
+		phy_mux_port_destroy(port);
+
+	xa_destroy(&mux->ports);
+	kfree(mux);
+}
+EXPORT_SYMBOL_GPL(phy_mux_free);
diff --git a/include/linux/phy_mux.h b/include/linux/phy_mux.h
new file mode 100644
index 000000000000..3dde6dd57faf
--- /dev/null
+++ b/include/linux/phy_mux.h
@@ -0,0 +1,44 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+
+#ifndef __PHY_MUX_H
+#define __PHY_MUX_H
+
+#include <linux/phy.h>
+
+struct device;
+struct fwnode_handle;
+struct phy_mux;
+struct phy_mux_port;
+
+enum phy_mux_port_type {
+	PHY_MUX_PORT_PHY,
+};
+
+/**
+ * struct phy_mux_port - One of the multiplexed channels of a MII multiplexer
+ * @id: Identifier of the port, unique within the mux
+ * @mux: The mux this port belongs to
+ * @type: What sits behind that port
+ * @phydev: For PHY_MUX_PORT_PHY, the PHY device behind the port, while the
+ *	    mux is attached
+ * @fwnode: For PHY_MUX_PORT_PHY, the firmware node of that PHY
+ * @interface: The MII mode the link runs in when this port is selected
+ */
+struct phy_mux_port {
+	u32 id;
+	struct phy_mux *mux;
+	enum phy_mux_port_type type;
+	struct phy_device *phydev;
+	struct fwnode_handle *fwnode;
+
+	phy_interface_t interface;
+};
+
+struct phy_mux *phy_mux_alloc(struct device *dev, int max_n_ports);
+int phy_mux_fwnode_add_ports(struct phy_mux *mux,
+			     const struct fwnode_handle *fwnode);
+int phy_mux_register(struct phy_mux *mux);
+void phy_mux_unregister(struct phy_mux *mux);
+void phy_mux_free(struct phy_mux *mux);
+
+#endif /* __PHY_MUX_H */
-- 
2.56.1



  parent reply	other threads:[~2026-12-04 11:07 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-04 11:06 [PATCH net-next 00/12] net: phy: Introduce MII multiplexers Maxime Chevallier
2026-12-04 11:06 ` [PATCH net-next 01/12] dt-bindings: net: Add Ethernet " Maxime Chevallier
2026-12-04 11:06 ` Maxime Chevallier [this message]
2026-12-04 11:06 ` [PATCH net-next 03/12] net: phy: Don't create the sysfs links for PHYs behind a mux Maxime Chevallier
2026-12-04 11:06 ` [PATCH net-next 04/12] net: phy: phy_mux: Allow attaching a mux to a net_device Maxime Chevallier
2026-12-04 11:06 ` [PATCH net-next 05/12] net: phy: phy_mux: Add port selection logic Maxime Chevallier
2026-12-04 11:06 ` [PATCH net-next 06/12] net: phy: phy_mux: Support SFP cage ports Maxime Chevallier
2026-12-04 11:06 ` [PATCH net-next 07/12] net: phylink: Factor out phylink_clear_phydev() Maxime Chevallier
2026-12-04 11:06 ` [PATCH net-next 08/12] net: phy: phy_mux: Add phy_mux_set_mac_managed_pm() Maxime Chevallier
2026-12-04 11:06 ` [PATCH net-next 09/12] net: phylink: Support PHYs reached through a phy_mux Maxime Chevallier
2026-12-04 11:06 ` [PATCH net-next 10/12] net: phylink: Support an SFP cage behind " Maxime Chevallier
2026-12-04 11:06 ` [PATCH net-next 11/12] net: phy: mux: Add the mii-mux-auto driver Maxime Chevallier
2026-12-04 11:06 ` [PATCH net-next 12/12] ARM: dts: turris-omnia: Describe the WAN port multiplexer Maxime Chevallier

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=20261204110622.1106492-3-maxime.chevallier@bootlin.com \
    --to=maxime.chevallier@bootlin.com \
    --cc=andrew+netdev@lunn.ch \
    --cc=christophe.leroy@csgroup.eu \
    --cc=conor+dt@kernel.org \
    --cc=daniel@makrotopia.org \
    --cc=davem@davemloft.net \
    --cc=devicetree@vger.kernel.org \
    --cc=dimitri.fedrau@liebherr.com \
    --cc=edumazet@kernel.org \
    --cc=f.fainelli@gmail.com \
    --cc=f@lex.la \
    --cc=frank.wunderlich@linux.dev \
    --cc=gregory.clement@bootlin.com \
    --cc=herve.codina@bootlin.com \
    --cc=hkallweit1@gmail.com \
    --cc=horms@kernel.org \
    --cc=kabel@kernel.org \
    --cc=kory.maincent@bootlin.com \
    --cc=krzk+dt@kernel.org \
    --cc=kuba@kernel.org \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@armlinux.org.uk \
    --cc=marcin.s.wojtas@gmail.com \
    --cc=netdev@vger.kernel.org \
    --cc=nicveronese@gmail.com \
    --cc=o.rempel@pengutronix.de \
    --cc=p.ameruoso@live.it \
    --cc=pabeni@redhat.com \
    --cc=robh@kernel.org \
    --cc=sebastian.hesselbarth@gmail.com \
    --cc=thomas.petazzoni@bootlin.com \
    --cc=vladimir.oltean@nxp.com \
    --cc=willemb@google.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