From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (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 3A20A606A691 for ; Fri, 4 Dec 2026 11:07:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=IBYdFeMW; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.a=rsa-sha256 header.s=dkim header.b=jQiO9/7j DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Unsubscribe-Post: List-Owner; bh=uG0iiwzVBc8cBg4Lc7arl6djnP6qGY7pNO7sn0/p+zY=; b=IBYdFeMWOaRWdt Uz7GRw3R+VD+jrU57qdnlqDsCYyEioidg4j6cWba0AaRQ0BzPWjOmjEA2yZQyQ5siZR4WASrlAO1u d7UeF4okQAU73JrSBUCuXF9RoU/ToQ+dSv3pE5MvJmYX/Qjyeo5juS8cwhyD1xgtpKbUSJuqUS6Ga 73qgvSUhftSEB0j/kCTihXznQwYtFCRmPOoNoRaysKYJHtVMwixt8Nex3Cuc/+m1H5e4kyhCPQ/CF ybwY5++P8ckGBWVxHXowiCR1Kt/TAg9JGjZiHfs8FeCmKTUMpZshG6ALj88/E4T59IqhTqcSMiW+o LFPl+kF8DbcD1mtE4MLQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.100.1 #2 (Red Hat Linux)) id 1xZR8V-000000004J2-3nUW; Fri, 04 Dec 2026 11:07:35 +0000 Received: from smtpout-03.galae.net ([185.246.85.4]) by bombadil.infradead.org with esmtps (Exim 4.100.1 #2 (Red Hat Linux)) id 1xZR85-000000004EA-1KXT for linux-arm-kernel@lists.infradead.org; Fri, 04 Dec 2026 11:07:19 +0000 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-Archived-By: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Last-TLS-Session-Version: TLSv1.3 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.9.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20261204_030705_667748_079A1FE7 X-CRM114-Status: GOOD ( 30.94 ) X-Spam-Score: -2.1 (--) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" X-Spam-Status: No, score=-19.3 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=4.0.1 X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 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