From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=185.246.85.4 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796382425; b=JHrGnL7UQQm2cWiB03o/eDl5K/iUUV2VXTRL9nDnmwTbT5o3w+uBdZIe9ZFfgXdvK9Gu HbEhQD4bS+5n3+QpVJ517o5pJpd5vOuRShvAhnwv7fs1A+X1OJKZFSkuMesn/s5FPcHkW u+n5U7rAxIDhjtVDiyroYIWcNTpFtoxzEMAgS/OWDCbfid00AHI9wDv3fKtPtdqqPbEGd fTKkQl7cDoqNvqPNHVEPWKgPsm3QxWxLlDpMLlxXjBi++pq4OI56uppAjaBqhjVyLppLB sbkToH8SIx9T1+AwavrD+ioTC969ISNm8ynXmNSZVum1jlxmh/IFxUU9zSOhLaoXIRA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796382425; h=DMARC-Filter:Received:Received:Received:DKIM-Signature:From:To:Cc: Subject:Date:Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding:X-Last-TLS-Session-Version; bh=uG0iiwzVBc8cBg4Lc7arl6djnP6qGY7pNO7sn0/p+zY=; b=I2UketizEqyjpvPj3AM37xGk61oN1J5HtSmI5Lf+q5085OD+pOwZ8tymHg7E6s0PmRnt jkRTRgNthgM1dDehWgCQ0q1yHrJnCNgNIaQinT8cRcywwoRYuV2QNlKr4+QCKcUCm2U+q hYZHoAfIGLUd29Wx0iPogdeIcN07KwX95bCk3mWN7hY0nSY9ChYahrl5PZMMwtNvcZnem C3dRbHISNZgH/7gMA0VurEGqtW/FOLEFPkocGafOUO6tvnrM3hJRMErh8fRDGWqCh3u59 DVtHKiHAmxj/J9L00dRXuSy4xdTeNqCZXIhIe0rUuTUIrPEciw6g3A4vVzoWKr6TP/Q== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=bootlin.com; spf=pass smtp.mailfrom=bootlin.com; dkim=pass header.d=bootlin.com header.i=@bootlin.com header.a=rsa-sha256 header.s=dkim header.b="jQiO9/7j"; arc=none smtp.remote-ip=185.246.85.4 Received: from smtpout-03.galae.net (smtpout-03.galae.net [185.246.85.4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2BB0D606A32D; Fri, 4 Dec 2026 11:07:05 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 2BB0D606A32D Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bootlin.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.a=rsa-sha256 header.s=dkim header.b=jQiO9/7j Received: from smtpout-01.galae.net (smtpout-01.galae.net [212.83.139.233]) by smtpout-03.galae.net (Postfix) with ESMTPS id C189E4E4185D; Fri, 4 Dec 2026 11:07:03 +0000 (UTC) Received: from mail.galae.net (mail.galae.net [212.83.136.155]) by smtpout-01.galae.net (Postfix) with ESMTPS id 8EDFD603C4; Fri, 4 Dec 2026 11:07:03 +0000 (UTC) Received: from [127.0.0.1] (localhost [127.0.0.1]) by localhost (Mailerdaemon) with ESMTPSA id 5CE4311EA193F; Fri, 04 Dec 2026 12:06:52 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=dkim; t=1796382420; h=from:subject:date:message-id:to:cc:mime-version: content-transfer-encoding:in-reply-to:references; bh=uG0iiwzVBc8cBg4Lc7arl6djnP6qGY7pNO7sn0/p+zY=; b=jQiO9/7jBhHJxcBO81q+vZ/yXbe6YNgkWIlAjbO84YB3s6Tt3e9oNpfldyLtuCPwFt0KK3 2iM8wIlaWef190PdgRMgwfg2DB06WE8EEosbeDDUmagt4pCOj8PUGuCW5ecOT53zyk+8Ai /8JFj1YfVpoF5TK1yk7C/LZgl55Hb/nS/DsSOxuTg47Y5JHxlRJIqKUYCwtRg1HUMfm3Yg gPQxvmkr8URk3j5Iu4VGOHkLSNABKWmArXIlf4jbeP+6EJLnc0vrK5kbgWSjfqnBGaaYCl kC6GE2lFSdBooKY0zOcLfm4ZQSG6KUq6BBeW4plZ2t7KYPQz/wJg+VffCKad1g== From: Maxime Chevallier To: davem@davemloft.net, Andrew Lunn , Jakub Kicinski , Eric Dumazet , Willem de Bruijn , Paolo Abeni , Russell King , =?UTF-8?q?Marek=20Beh=C3=BAn?= , Sebastian Hesselbarth , Gregory Clement , Heiner Kallweit Cc: Maxime Chevallier , 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 , Krzysztof Kozlowski , Rob Herring , Christophe Leroy , Herve Codina , Florian Fainelli , Vladimir Oltean , =?UTF-8?q?K=C3=B6ry=20Maincent?= , Oleksij Rempel , =?UTF-8?q?Nicol=C3=B2=20Veronese?= , Simon Horman , marcin.s.wojtas@gmail.com, Daniel Golle , Dimitri Fedrau , Frank Wunderlich , Pietro Ameruoso , Aleksei Sviridkin Subject: [PATCH net-next 02/12] net: phy: Introduce phy_mux, a MII multiplexer representation Date: Fri, 4 Dec 2026 12:06:11 +0100 Message-ID: <20261204110622.1106492-3-maxime.chevallier@bootlin.com> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261204110622.1106492-1-maxime.chevallier@bootlin.com> References: <20261204110622.1106492-1-maxime.chevallier@bootlin.com> X-Mailing-List: netdev@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Last-TLS-Session-Version: TLSv1.3 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 --- 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 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 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 +#include +#include +#include +#include +#include +#include +#include + +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 + +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