From nobody Mon Feb 9 01:48:37 2026 Received: from metis.whiteo.stw.pengutronix.de (metis.whiteo.stw.pengutronix.de [185.203.201.7]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EB6901EDA34 for ; Mon, 5 May 2025 08:43:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.203.201.7 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746434632; cv=none; b=pz9d9KUSaRfKt1FfkCyvGkc1Hu/Qyi+4U37AYjIIYL+3bP0e/ksqHW+J7xj/J+COWd7+uIZl3SA6VGk576WFcFXajp7twCS1RdiPVtRBFMDrQ6r+Ni6BEiD2zkTSmCtrThQBOVYXbKFX9pfp1pj58lrXPALiNCV3uemFqn5FSvY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746434632; c=relaxed/simple; bh=PfDu6Fp8KyBjovfjyOpuBNf1DbqZ7wUZxTHJtarXAtU=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=eTCUfRfzl9j+0ef/FZw+jOVV25xiSEiUWxyM4ysWvOT1gVWen/X9qMB4paoeZZR2mBfwjM3LbHwsiI+U3Ix+waNt21Hi5VMp33Q2Cn4UN9ikaMu5qG5CcKfo9cP2IIH9dLJRphwcniQFdu4qmzIZks+AohN9ZQifqjua5W/1GVY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pengutronix.de; spf=pass smtp.mailfrom=pengutronix.de; arc=none smtp.client-ip=185.203.201.7 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pengutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pengutronix.de Received: from drehscheibe.grey.stw.pengutronix.de ([2a0a:edc0:0:c01:1d::a2]) by metis.whiteo.stw.pengutronix.de with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1uBrQN-0005W6-An; Mon, 05 May 2025 10:43:43 +0200 Received: from dude04.red.stw.pengutronix.de ([2a0a:edc0:0:1101:1d::ac]) by drehscheibe.grey.stw.pengutronix.de with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1uBrQM-001CSA-19; Mon, 05 May 2025 10:43:42 +0200 Received: from ore by dude04.red.stw.pengutronix.de with local (Exim 4.96) (envelope-from ) id 1uBrQM-003SPl-0s; Mon, 05 May 2025 10:43:42 +0200 From: Oleksij Rempel To: "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Woojung Huh , Andrew Lunn , Russell King , Thangaraj Samynathan , Rengarajan Sundararajan Cc: Oleksij Rempel , kernel@pengutronix.de, linux-kernel@vger.kernel.org, netdev@vger.kernel.org, UNGLinuxDriver@microchip.com, Phil Elwell , Maxime Chevallier , Simon Horman Subject: [PATCH net-next v8 1/7] net: usb: lan78xx: Improve error handling in PHY initialization Date: Mon, 5 May 2025 10:43:35 +0200 Message-Id: <20250505084341.824165-2-o.rempel@pengutronix.de> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20250505084341.824165-1-o.rempel@pengutronix.de> References: <20250505084341.824165-1-o.rempel@pengutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-SA-Exim-Connect-IP: 2a0a:edc0:0:c01:1d::a2 X-SA-Exim-Mail-From: ore@pengutronix.de X-SA-Exim-Scanned: No (on metis.whiteo.stw.pengutronix.de); SAEximRunCond expanded to false X-PTX-Original-Recipient: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Ensure that return values from `lan78xx_write_reg()`, `lan78xx_read_reg()`, and `phy_find_first()` are properly checked and propagated. Use `ERR_PTR(ret)` for error reporting in `lan7801_phy_init()` and replace `-EIO` with `-ENODEV` where appropriate to provide more accurate error codes. Signed-off-by: Oleksij Rempel Reviewed-by: Thangaraj Samynathan --- changes v6: - confirmed with maintainers that fixed_phy_register() leak is acceptable as soon as it is fixed within the patch set changes v5: - make sure lan7801_phy_init() caller is testing against IS_ERR instead of NULL. changes v4: - split the patch and move part of it before PHYlink migration --- drivers/net/usb/lan78xx.c | 47 ++++++++++++++++++++++++++------------- 1 file changed, 31 insertions(+), 16 deletions(-) diff --git a/drivers/net/usb/lan78xx.c b/drivers/net/usb/lan78xx.c index e4f1663b6204..19db18cf0504 100644 --- a/drivers/net/usb/lan78xx.c +++ b/drivers/net/usb/lan78xx.c @@ -2510,14 +2510,13 @@ static void lan78xx_remove_irq_domain(struct lan78x= x_net *dev) =20 static struct phy_device *lan7801_phy_init(struct lan78xx_net *dev) { - u32 buf; - int ret; struct fixed_phy_status fphy_status =3D { .link =3D 1, .speed =3D SPEED_1000, .duplex =3D DUPLEX_FULL, }; struct phy_device *phydev; + int ret; =20 phydev =3D phy_find_first(dev->mdiobus); if (!phydev) { @@ -2525,30 +2524,40 @@ static struct phy_device *lan7801_phy_init(struct l= an78xx_net *dev) phydev =3D fixed_phy_register(PHY_POLL, &fphy_status, NULL); if (IS_ERR(phydev)) { netdev_err(dev->net, "No PHY/fixed_PHY found\n"); - return NULL; + return ERR_PTR(-ENODEV); } netdev_dbg(dev->net, "Registered FIXED PHY\n"); dev->interface =3D PHY_INTERFACE_MODE_RGMII; ret =3D lan78xx_write_reg(dev, MAC_RGMII_ID, MAC_RGMII_ID_TXC_DELAY_EN_); + if (ret < 0) + return ERR_PTR(ret); + ret =3D lan78xx_write_reg(dev, RGMII_TX_BYP_DLL, 0x3D00); - ret =3D lan78xx_read_reg(dev, HW_CFG, &buf); - buf |=3D HW_CFG_CLK125_EN_; - buf |=3D HW_CFG_REFCLK25_EN_; - ret =3D lan78xx_write_reg(dev, HW_CFG, buf); + if (ret < 0) + return ERR_PTR(ret); + + ret =3D lan78xx_update_reg(dev, HW_CFG, HW_CFG_CLK125_EN_ | + HW_CFG_REFCLK25_EN_, + HW_CFG_CLK125_EN_ | HW_CFG_REFCLK25_EN_); + if (ret < 0) + return ERR_PTR(ret); } else { if (!phydev->drv) { netdev_err(dev->net, "no PHY driver found\n"); - return NULL; + return ERR_PTR(-EINVAL); } dev->interface =3D PHY_INTERFACE_MODE_RGMII_ID; /* The PHY driver is responsible to configure proper RGMII * interface delays. Disable RGMII delays on MAC side. */ - lan78xx_write_reg(dev, MAC_RGMII_ID, 0); + ret =3D lan78xx_write_reg(dev, MAC_RGMII_ID, 0); + if (ret < 0) + return ERR_PTR(ret); =20 phydev->is_internal =3D false; } + return phydev; } =20 @@ -2562,9 +2571,10 @@ static int lan78xx_phy_init(struct lan78xx_net *dev) switch (dev->chipid) { case ID_REV_CHIP_ID_7801_: phydev =3D lan7801_phy_init(dev); - if (!phydev) { - netdev_err(dev->net, "lan7801: PHY Init Failed"); - return -EIO; + if (IS_ERR(phydev)) { + netdev_err(dev->net, "lan7801: failed to init PHY: %pe\n", + phydev); + return PTR_ERR(phydev); } break; =20 @@ -2573,7 +2583,7 @@ static int lan78xx_phy_init(struct lan78xx_net *dev) phydev =3D phy_find_first(dev->mdiobus); if (!phydev) { netdev_err(dev->net, "no PHY found\n"); - return -EIO; + return -ENODEV; } phydev->is_internal =3D true; dev->interface =3D PHY_INTERFACE_MODE_GMII; @@ -2581,7 +2591,7 @@ static int lan78xx_phy_init(struct lan78xx_net *dev) =20 default: netdev_err(dev->net, "Unknown CHIP ID found\n"); - return -EIO; + return -ENODEV; } =20 /* if phyirq is not set, use polling mode in phylib */ @@ -2633,7 +2643,10 @@ static int lan78xx_phy_init(struct lan78xx_net *dev) sizeof(u32)); if (len >=3D 0) { /* Ensure the appropriate LEDs are enabled */ - lan78xx_read_reg(dev, HW_CFG, ®); + ret =3D lan78xx_read_reg(dev, HW_CFG, ®); + if (ret < 0) + return ret; + reg &=3D ~(HW_CFG_LED0_EN_ | HW_CFG_LED1_EN_ | HW_CFG_LED2_EN_ | @@ -2642,7 +2655,9 @@ static int lan78xx_phy_init(struct lan78xx_net *dev) (len > 1) * HW_CFG_LED1_EN_ | (len > 2) * HW_CFG_LED2_EN_ | (len > 3) * HW_CFG_LED3_EN_; - lan78xx_write_reg(dev, HW_CFG, reg); + ret =3D lan78xx_write_reg(dev, HW_CFG, reg); + if (ret < 0) + return ret; } } =20 --=20 2.39.5 From nobody Mon Feb 9 01:48:37 2026 Received: from metis.whiteo.stw.pengutronix.de (metis.whiteo.stw.pengutronix.de [185.203.201.7]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EB5B01DF73D for ; Mon, 5 May 2025 08:43:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.203.201.7 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746434630; cv=none; b=IgheLYMfqUlnrEMpbzvS7K/9fp8MLi3Ml2nq1oiuXtflkWReF1y2tW51o7v5PbxGpY+gHY2XeWtWLwyl7Frn3Obvn3wVQKacOu5kDQstKek+mgz8Md6MYe8TZWcNCR75tlos3iz473Ibj1m6lv4lRfpCiQBtuchnzw61mQBB/kU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746434630; c=relaxed/simple; bh=EKrdrdjRyLeoPTwBO7+5pjftSljEtQ9GICzVJzj1620=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=cz4wz5c9blUTUDSVPAzYhN7GVXSp8kAillgXXZZt0KJImnMCODrmFAVHzAZd1jDfji00Im6kPYVz5ka5/tlTNeQuPZTGSeukLshbCGw7oRX8iome2R3fAVfcY+5cmUoFYP5vZ2/s/kWOdOzf6EryJRx8sjtpchR5M1OqKOgkLoo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pengutronix.de; spf=pass smtp.mailfrom=pengutronix.de; arc=none smtp.client-ip=185.203.201.7 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pengutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pengutronix.de Received: from drehscheibe.grey.stw.pengutronix.de ([2a0a:edc0:0:c01:1d::a2]) by metis.whiteo.stw.pengutronix.de with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1uBrQN-0005W7-Al; Mon, 05 May 2025 10:43:43 +0200 Received: from dude04.red.stw.pengutronix.de ([2a0a:edc0:0:1101:1d::ac]) by drehscheibe.grey.stw.pengutronix.de with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1uBrQM-001CSB-1E; Mon, 05 May 2025 10:43:42 +0200 Received: from ore by dude04.red.stw.pengutronix.de with local (Exim 4.96) (envelope-from ) id 1uBrQM-003SPv-0x; Mon, 05 May 2025 10:43:42 +0200 From: Oleksij Rempel To: "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Woojung Huh , Andrew Lunn , Russell King , Thangaraj Samynathan , Rengarajan Sundararajan Cc: Oleksij Rempel , kernel@pengutronix.de, linux-kernel@vger.kernel.org, netdev@vger.kernel.org, UNGLinuxDriver@microchip.com, Phil Elwell , Maxime Chevallier , Simon Horman Subject: [PATCH net-next v8 2/7] net: usb: lan78xx: remove explicit check for missing PHY driver Date: Mon, 5 May 2025 10:43:36 +0200 Message-Id: <20250505084341.824165-3-o.rempel@pengutronix.de> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20250505084341.824165-1-o.rempel@pengutronix.de> References: <20250505084341.824165-1-o.rempel@pengutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-SA-Exim-Connect-IP: 2a0a:edc0:0:c01:1d::a2 X-SA-Exim-Mail-From: ore@pengutronix.de X-SA-Exim-Scanned: No (on metis.whiteo.stw.pengutronix.de); SAEximRunCond expanded to false X-PTX-Original-Recipient: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" RGMII timing correctness relies on the PHY providing internal delays. This is typically ensured via PHY driver, strap pins, or PCB layout. Explicitly checking for a PHY driver here is unnecessary and non-standard. This logic applies to all MACs, not just LAN78xx, and should be left to phylib, phylink, or platform configuration. Drop the check and rely on standard subsystem behavior. Signed-off-by: Oleksij Rempel Reviewed-by: Thangaraj Samynathan --- changes v6: - this patch is added in v6 --- drivers/net/usb/lan78xx.c | 4 ---- 1 file changed, 4 deletions(-) diff --git a/drivers/net/usb/lan78xx.c b/drivers/net/usb/lan78xx.c index 19db18cf0504..9c0658227bde 100644 --- a/drivers/net/usb/lan78xx.c +++ b/drivers/net/usb/lan78xx.c @@ -2543,10 +2543,6 @@ static struct phy_device *lan7801_phy_init(struct la= n78xx_net *dev) if (ret < 0) return ERR_PTR(ret); } else { - if (!phydev->drv) { - netdev_err(dev->net, "no PHY driver found\n"); - return ERR_PTR(-EINVAL); - } dev->interface =3D PHY_INTERFACE_MODE_RGMII_ID; /* The PHY driver is responsible to configure proper RGMII * interface delays. Disable RGMII delays on MAC side. --=20 2.39.5 From nobody Mon Feb 9 01:48:37 2026 Received: from metis.whiteo.stw.pengutronix.de (metis.whiteo.stw.pengutronix.de [185.203.201.7]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A35886D17 for ; Mon, 5 May 2025 08:43:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.203.201.7 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746434632; cv=none; b=MmqV3cUR7Plwouys+oLmcYMbHvYwdNdm037s3jZaQ/F9+PMKqOqNiBarky6Bd+EnKVYVaw+/BQaL8BLp63oDWqzvJvupnsqbMOtqyoD7Sg3Zfn/f+JXFp20zyISvd+t/x+Dcf4wi6nEzeO9naeXYnuEbeo2d7brQkA5CdPJucMg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746434632; c=relaxed/simple; bh=5URiGYR5zlAEfTrePIZ8K4h++6AoVHBDIfSU+U1wtuI=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Type; b=nZwqf/kB0DFkY3Ho4M9yjXiYVQLEkiTaUWUf18x+UohPVecUzYVZVeAAlXQrR43K9aTDki2E8r5pmNp55Mcd3GmXgyGV50VEKL8HyJqyol3WjD/oRQKmPuHbn0pO8QzKbgRpOm3aH0wFv3ZilzI7aGDePG8eJWvbqxPcBhoVCEs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pengutronix.de; spf=pass smtp.mailfrom=pengutronix.de; arc=none smtp.client-ip=185.203.201.7 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pengutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pengutronix.de Received: from drehscheibe.grey.stw.pengutronix.de ([2a0a:edc0:0:c01:1d::a2]) by metis.whiteo.stw.pengutronix.de with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1uBrQN-0005W8-Al; Mon, 05 May 2025 10:43:43 +0200 Received: from dude04.red.stw.pengutronix.de ([2a0a:edc0:0:1101:1d::ac]) by drehscheibe.grey.stw.pengutronix.de with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1uBrQM-001CSC-1M; Mon, 05 May 2025 10:43:42 +0200 Received: from ore by dude04.red.stw.pengutronix.de with local (Exim 4.96) (envelope-from ) id 1uBrQM-003SQ5-11; Mon, 05 May 2025 10:43:42 +0200 From: Oleksij Rempel To: "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Woojung Huh , Andrew Lunn , Russell King , Thangaraj Samynathan , Rengarajan Sundararajan Cc: Oleksij Rempel , kernel@pengutronix.de, linux-kernel@vger.kernel.org, netdev@vger.kernel.org, UNGLinuxDriver@microchip.com, Phil Elwell , Maxime Chevallier , Simon Horman Subject: [PATCH net-next v8 3/7] net: usb: lan78xx: refactor PHY init to separate detection and MAC configuration Date: Mon, 5 May 2025 10:43:37 +0200 Message-Id: <20250505084341.824165-4-o.rempel@pengutronix.de> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20250505084341.824165-1-o.rempel@pengutronix.de> References: <20250505084341.824165-1-o.rempel@pengutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-SA-Exim-Connect-IP: 2a0a:edc0:0:c01:1d::a2 X-SA-Exim-Mail-From: ore@pengutronix.de X-SA-Exim-Scanned: No (on metis.whiteo.stw.pengutronix.de); SAEximRunCond expanded to false X-PTX-Original-Recipient: linux-kernel@vger.kernel.org Split out PHY detection into lan78xx_get_phy() and MAC-side setup into lan78xx_mac_prepare_for_phy(), making the main lan78xx_phy_init() cleaner and easier to follow. This improves separation of concerns and prepares the code for a future transition to phylink. Fixed PHY registration and interface selection are now handled in lan78xx_get_phy(), while MAC-side delay configuration is done in lan78xx_mac_prepare_for_phy(). The fixed PHY fallback is preserved for setups like EVB-KSZ9897-1, where LAN7801 connects directly to a KSZ switch without a standard PHY or device tree support. No functional changes intended. Signed-off-by: Oleksij Rempel Reviewed-by: Thangaraj Samynathan --- changes v6: - this patch is added in v6 --- drivers/net/usb/lan78xx.c | 174 ++++++++++++++++++++++++++++---------- 1 file changed, 128 insertions(+), 46 deletions(-) diff --git a/drivers/net/usb/lan78xx.c b/drivers/net/usb/lan78xx.c index 9c0658227bde..7f1ecc415d53 100644 --- a/drivers/net/usb/lan78xx.c +++ b/drivers/net/usb/lan78xx.c @@ -2508,53 +2508,145 @@ static void lan78xx_remove_irq_domain(struct lan78= xx_net *dev) dev->domain_data.irqdomain =3D NULL; } =20 -static struct phy_device *lan7801_phy_init(struct lan78xx_net *dev) +/** + * lan78xx_register_fixed_phy() - Register a fallback fixed PHY + * @dev: LAN78xx device + * + * Registers a fixed PHY with 1 Gbps full duplex. This is used in special = cases + * like EVB-KSZ9897-1, where LAN7801 acts as a USB-to-Ethernet interface t= o a + * switch without a visible PHY. + * + * Return: pointer to the registered fixed PHY, or ERR_PTR() on error. + */ +static struct phy_device *lan78xx_register_fixed_phy(struct lan78xx_net *d= ev) { struct fixed_phy_status fphy_status =3D { .link =3D 1, .speed =3D SPEED_1000, .duplex =3D DUPLEX_FULL, }; + + netdev_info(dev->net, + "No PHY found on LAN7801 =E2=80=93 registering fixed PHY (e.g. EVB-K= SZ9897-1)\n"); + + return fixed_phy_register(PHY_POLL, &fphy_status, NULL); +} + +/** + * lan78xx_get_phy() - Probe or register PHY device and set interface mode + * @dev: LAN78xx device structure + * + * This function attempts to find a PHY on the MDIO bus. If no PHY is found + * and the chip is LAN7801, it registers a fixed PHY as fallback. It also + * sets dev->interface based on chip ID and detected PHY type. + * + * Return: a valid PHY device pointer, or ERR_PTR() on failure. + */ +static struct phy_device *lan78xx_get_phy(struct lan78xx_net *dev) +{ struct phy_device *phydev; - int ret; =20 + /* Attempt to locate a PHY on the MDIO bus */ phydev =3D phy_find_first(dev->mdiobus); - if (!phydev) { - netdev_dbg(dev->net, "PHY Not Found!! Registering Fixed PHY\n"); - phydev =3D fixed_phy_register(PHY_POLL, &fphy_status, NULL); - if (IS_ERR(phydev)) { - netdev_err(dev->net, "No PHY/fixed_PHY found\n"); - return ERR_PTR(-ENODEV); + + switch (dev->chipid) { + case ID_REV_CHIP_ID_7801_: + if (phydev) { + /* External RGMII PHY detected */ + dev->interface =3D PHY_INTERFACE_MODE_RGMII_ID; + phydev->is_internal =3D false; + + if (!phydev->drv) + netdev_warn(dev->net, + "PHY driver not found =E2=80=93 assuming RGMII delays are on PCB = or strapped for the PHY\n"); + + return phydev; } - netdev_dbg(dev->net, "Registered FIXED PHY\n"); + dev->interface =3D PHY_INTERFACE_MODE_RGMII; + /* No PHY found =E2=80=93 fallback to fixed PHY (e.g. KSZ switch board) = */ + return lan78xx_register_fixed_phy(dev); + + case ID_REV_CHIP_ID_7800_: + case ID_REV_CHIP_ID_7850_: + if (!phydev) + return ERR_PTR(-ENODEV); + + /* These use internal GMII-connected PHY */ + dev->interface =3D PHY_INTERFACE_MODE_GMII; + phydev->is_internal =3D true; + return phydev; + + default: + netdev_err(dev->net, "Unknown CHIP ID: 0x%08x\n", dev->chipid); + return ERR_PTR(-ENODEV); + } +} + +/** + * lan78xx_mac_prepare_for_phy() - Preconfigure MAC-side interface settings + * @dev: LAN78xx device + * + * Configure MAC-side registers according to dev->interface, which should = be + * set by lan78xx_get_phy(). + * + * - For PHY_INTERFACE_MODE_RGMII: + * Enable MAC-side TXC delay. This mode seems to be used in a special se= tup + * without a real PHY, likely on EVB-KSZ9897-1. In that design, LAN7801 = is + * connected to the KSZ9897 switch, and the link timing is expected to be + * hardwired (e.g. via strapping or board layout). No devicetree support= is + * assumed here. + * + * - For PHY_INTERFACE_MODE_RGMII_ID: + * Disable MAC-side delay and rely on the PHY driver to provide delay. + * + * - For GMII, no MAC-specific config is needed. + * + * Return: 0 on success or a negative error code. + */ +static int lan78xx_mac_prepare_for_phy(struct lan78xx_net *dev) +{ + int ret; + + switch (dev->interface) { + case PHY_INTERFACE_MODE_RGMII: + /* Enable MAC-side TX clock delay */ ret =3D lan78xx_write_reg(dev, MAC_RGMII_ID, MAC_RGMII_ID_TXC_DELAY_EN_); if (ret < 0) - return ERR_PTR(ret); + return ret; =20 ret =3D lan78xx_write_reg(dev, RGMII_TX_BYP_DLL, 0x3D00); if (ret < 0) - return ERR_PTR(ret); + return ret; =20 - ret =3D lan78xx_update_reg(dev, HW_CFG, HW_CFG_CLK125_EN_ | - HW_CFG_REFCLK25_EN_, + ret =3D lan78xx_update_reg(dev, HW_CFG, + HW_CFG_CLK125_EN_ | HW_CFG_REFCLK25_EN_, HW_CFG_CLK125_EN_ | HW_CFG_REFCLK25_EN_); if (ret < 0) - return ERR_PTR(ret); - } else { - dev->interface =3D PHY_INTERFACE_MODE_RGMII_ID; - /* The PHY driver is responsible to configure proper RGMII - * interface delays. Disable RGMII delays on MAC side. - */ + return ret; + + break; + + case PHY_INTERFACE_MODE_RGMII_ID: + /* Disable MAC-side TXC delay, PHY provides it */ ret =3D lan78xx_write_reg(dev, MAC_RGMII_ID, 0); if (ret < 0) - return ERR_PTR(ret); + return ret; =20 - phydev->is_internal =3D false; + break; + + case PHY_INTERFACE_MODE_GMII: + /* No MAC-specific configuration required */ + break; + + default: + netdev_warn(dev->net, "Unsupported interface mode: %d\n", + dev->interface); + break; } =20 - return phydev; + return 0; } =20 static int lan78xx_phy_init(struct lan78xx_net *dev) @@ -2564,31 +2656,13 @@ static int lan78xx_phy_init(struct lan78xx_net *dev) u32 mii_adv; struct phy_device *phydev; =20 - switch (dev->chipid) { - case ID_REV_CHIP_ID_7801_: - phydev =3D lan7801_phy_init(dev); - if (IS_ERR(phydev)) { - netdev_err(dev->net, "lan7801: failed to init PHY: %pe\n", - phydev); - return PTR_ERR(phydev); - } - break; - - case ID_REV_CHIP_ID_7800_: - case ID_REV_CHIP_ID_7850_: - phydev =3D phy_find_first(dev->mdiobus); - if (!phydev) { - netdev_err(dev->net, "no PHY found\n"); - return -ENODEV; - } - phydev->is_internal =3D true; - dev->interface =3D PHY_INTERFACE_MODE_GMII; - break; + phydev =3D lan78xx_get_phy(dev); + if (IS_ERR(phydev)) + return PTR_ERR(phydev); =20 - default: - netdev_err(dev->net, "Unknown CHIP ID found\n"); - return -ENODEV; - } + ret =3D lan78xx_mac_prepare_for_phy(dev); + if (ret < 0) + goto free_phy; =20 /* if phyirq is not set, use polling mode in phylib */ if (dev->domain_data.phyirq > 0) @@ -2662,6 +2736,14 @@ static int lan78xx_phy_init(struct lan78xx_net *dev) dev->fc_autoneg =3D phydev->autoneg; =20 return 0; + +free_phy: + if (phy_is_pseudo_fixed_link(phydev)) { + fixed_phy_unregister(phydev); + phy_device_free(phydev); + } + + return ret; } =20 static int lan78xx_set_rx_max_frame_length(struct lan78xx_net *dev, int si= ze) --=20 2.39.5 From nobody Mon Feb 9 01:48:37 2026 Received: from metis.whiteo.stw.pengutronix.de (metis.whiteo.stw.pengutronix.de [185.203.201.7]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6BBC81F4261 for ; Mon, 5 May 2025 08:43:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.203.201.7 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746434632; cv=none; b=lYVAILGFO98qt1KEuqr1++TowvRrnYHerN9MISrsb3mxcqzw4vgUJHp5xki3mSaBSMbnO/vFCd8rq21b6WiI7FEGjHxF3d+KNnsJfIDH+GjXpwRvmvGlwtlGHwZ6V7zWyiiHqneiQ9hwwr+2QDU6o9DHZKH3sudnTFfVRPQOkDo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746434632; c=relaxed/simple; bh=CywE26rgas3evbSjo68DaIxIX7NCR5wqWdWiofqIa3Y=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=IE1CivsIjtTM753yrYstlqlnz0PptHlaHFkvHpuVUibMjWkLr5ZGXOTxD/riLq57sNmWzJ267QkTwMr2IKhZFcswfgccCSaQtgoRE2vLQfGU1tyvGLqGtM8tNYLjiPP3WWmNFeAfsD4VTpbj5ejxgwmPdfhMnnAqRiJYc0UeJVg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pengutronix.de; spf=pass smtp.mailfrom=pengutronix.de; arc=none smtp.client-ip=185.203.201.7 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pengutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pengutronix.de Received: from drehscheibe.grey.stw.pengutronix.de ([2a0a:edc0:0:c01:1d::a2]) by metis.whiteo.stw.pengutronix.de with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1uBrQN-0005W9-Al; Mon, 05 May 2025 10:43:43 +0200 Received: from dude04.red.stw.pengutronix.de ([2a0a:edc0:0:1101:1d::ac]) by drehscheibe.grey.stw.pengutronix.de with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1uBrQM-001CSE-1N; Mon, 05 May 2025 10:43:42 +0200 Received: from ore by dude04.red.stw.pengutronix.de with local (Exim 4.96) (envelope-from ) id 1uBrQM-003SQF-15; Mon, 05 May 2025 10:43:42 +0200 From: Oleksij Rempel To: "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Woojung Huh , Andrew Lunn , Russell King , Thangaraj Samynathan , Rengarajan Sundararajan Cc: Oleksij Rempel , kernel@pengutronix.de, linux-kernel@vger.kernel.org, netdev@vger.kernel.org, UNGLinuxDriver@microchip.com, Phil Elwell , Maxime Chevallier , Simon Horman Subject: [PATCH net-next v8 4/7] net: usb: lan78xx: move LED DT configuration to helper Date: Mon, 5 May 2025 10:43:38 +0200 Message-Id: <20250505084341.824165-5-o.rempel@pengutronix.de> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20250505084341.824165-1-o.rempel@pengutronix.de> References: <20250505084341.824165-1-o.rempel@pengutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-SA-Exim-Connect-IP: 2a0a:edc0:0:c01:1d::a2 X-SA-Exim-Mail-From: ore@pengutronix.de X-SA-Exim-Scanned: No (on metis.whiteo.stw.pengutronix.de); SAEximRunCond expanded to false X-PTX-Original-Recipient: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Extract the LED enable logic based on the "microchip,led-modes" property into a new helper function lan78xx_configure_leds_from_dt(). This simplifies lan78xx_phy_init() and improves modularity. No functional changes intended. Signed-off-by: Oleksij Rempel Reviewed-by: Thangaraj Samynathan --- changes v6: - this patch is added in v6 --- drivers/net/usb/lan78xx.c | 72 +++++++++++++++++++++++++-------------- 1 file changed, 46 insertions(+), 26 deletions(-) diff --git a/drivers/net/usb/lan78xx.c b/drivers/net/usb/lan78xx.c index 7f1ecc415d53..07530eef82cb 100644 --- a/drivers/net/usb/lan78xx.c +++ b/drivers/net/usb/lan78xx.c @@ -2649,6 +2649,49 @@ static int lan78xx_mac_prepare_for_phy(struct lan78x= x_net *dev) return 0; } =20 +/** + * lan78xx_configure_leds_from_dt() - Configure LED enables based on DT + * @dev: LAN78xx device + * @phydev: PHY device (must be valid) + * + * Reads "microchip,led-modes" property from the PHY's DT node and enables + * the corresponding number of LEDs by writing to HW_CFG. + * + * This helper preserves the original logic, enabling up to 4 LEDs. + * If the property is not present, this function does nothing. + * + * Return: 0 on success or a negative error code. + */ +static int lan78xx_configure_leds_from_dt(struct lan78xx_net *dev, + struct phy_device *phydev) +{ + struct device_node *np =3D phydev->mdio.dev.of_node; + u32 reg; + int len, ret; + + if (!np) + return 0; + + len =3D of_property_count_elems_of_size(np, "microchip,led-modes", + sizeof(u32)); + if (len < 0) + return 0; + + ret =3D lan78xx_read_reg(dev, HW_CFG, ®); + if (ret < 0) + return ret; + + reg &=3D ~(HW_CFG_LED0_EN_ | HW_CFG_LED1_EN_ | + HW_CFG_LED2_EN_ | HW_CFG_LED3_EN_); + + reg |=3D (len > 0) * HW_CFG_LED0_EN_ | + (len > 1) * HW_CFG_LED1_EN_ | + (len > 2) * HW_CFG_LED2_EN_ | + (len > 3) * HW_CFG_LED3_EN_; + + return lan78xx_write_reg(dev, HW_CFG, reg); +} + static int lan78xx_phy_init(struct lan78xx_net *dev) { __ETHTOOL_DECLARE_LINK_MODE_MASK(fc) =3D { 0, }; @@ -2704,32 +2747,9 @@ static int lan78xx_phy_init(struct lan78xx_net *dev) =20 phy_support_eee(phydev); =20 - if (phydev->mdio.dev.of_node) { - u32 reg; - int len; - - len =3D of_property_count_elems_of_size(phydev->mdio.dev.of_node, - "microchip,led-modes", - sizeof(u32)); - if (len >=3D 0) { - /* Ensure the appropriate LEDs are enabled */ - ret =3D lan78xx_read_reg(dev, HW_CFG, ®); - if (ret < 0) - return ret; - - reg &=3D ~(HW_CFG_LED0_EN_ | - HW_CFG_LED1_EN_ | - HW_CFG_LED2_EN_ | - HW_CFG_LED3_EN_); - reg |=3D (len > 0) * HW_CFG_LED0_EN_ | - (len > 1) * HW_CFG_LED1_EN_ | - (len > 2) * HW_CFG_LED2_EN_ | - (len > 3) * HW_CFG_LED3_EN_; - ret =3D lan78xx_write_reg(dev, HW_CFG, reg); - if (ret < 0) - return ret; - } - } + ret =3D lan78xx_configure_leds_from_dt(dev, phydev); + if (ret) + goto free_phy; =20 genphy_config_aneg(phydev); =20 --=20 2.39.5 From nobody Mon Feb 9 01:48:37 2026 Received: from metis.whiteo.stw.pengutronix.de (metis.whiteo.stw.pengutronix.de [185.203.201.7]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4D2E61F4192 for ; Mon, 5 May 2025 08:43:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.203.201.7 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746434631; cv=none; b=EW1l67X1umJoH1Qwb44h50XqEIoLWTg0yHY/ubjOetnt07fwfhTVTUZ3/gLn86lmU3MqYlSXBCgN2nQhcRGqnHqWLVpp6fCznV6dvJSSjuK99ERQPL7OQRyuIQ2jVb+2rta9F17ZJ6RpOzuFsx2n8u+dnKL1rUTZo8vgG0bXhdQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746434631; c=relaxed/simple; bh=czQrwqSOCU4ro8+9cqoU25PAcGWoBynUwIf9q024yyM=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=ExI3/jO2kYLWYr5sIzQ7t5XDIn6R8Ol4VzX741VF8LNBRXqNw1TOxOG1i1++OFG71aZ1H0DcYXfSOE89L7HAGepTPxnwZfxwg+2FDo6qnsUGjB6Olnwtioj1Wm+ZVOxXvSJs4F4iBz6+LtsxeallkB96J4zYE240IqiXN24Ye2g= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pengutronix.de; spf=pass smtp.mailfrom=pengutronix.de; arc=none smtp.client-ip=185.203.201.7 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pengutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pengutronix.de Received: from drehscheibe.grey.stw.pengutronix.de ([2a0a:edc0:0:c01:1d::a2]) by metis.whiteo.stw.pengutronix.de with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1uBrQN-0005WB-Am; Mon, 05 May 2025 10:43:43 +0200 Received: from dude04.red.stw.pengutronix.de ([2a0a:edc0:0:1101:1d::ac]) by drehscheibe.grey.stw.pengutronix.de with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1uBrQM-001CSG-1X; Mon, 05 May 2025 10:43:42 +0200 Received: from ore by dude04.red.stw.pengutronix.de with local (Exim 4.96) (envelope-from ) id 1uBrQM-003SQP-19; Mon, 05 May 2025 10:43:42 +0200 From: Oleksij Rempel To: "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Woojung Huh , Andrew Lunn , Russell King , Thangaraj Samynathan , Rengarajan Sundararajan Cc: Oleksij Rempel , kernel@pengutronix.de, linux-kernel@vger.kernel.org, netdev@vger.kernel.org, UNGLinuxDriver@microchip.com, Phil Elwell , Maxime Chevallier , Simon Horman Subject: [PATCH net-next v8 5/7] net: usb: lan78xx: Extract PHY interrupt acknowledgment to helper Date: Mon, 5 May 2025 10:43:39 +0200 Message-Id: <20250505084341.824165-6-o.rempel@pengutronix.de> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20250505084341.824165-1-o.rempel@pengutronix.de> References: <20250505084341.824165-1-o.rempel@pengutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-SA-Exim-Connect-IP: 2a0a:edc0:0:c01:1d::a2 X-SA-Exim-Mail-From: ore@pengutronix.de X-SA-Exim-Scanned: No (on metis.whiteo.stw.pengutronix.de); SAEximRunCond expanded to false X-PTX-Original-Recipient: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Move the PHY interrupt acknowledgment logic from lan78xx_link_reset() to a new helper function lan78xx_phy_int_ack(). This simplifies the code and prepares for reusing the acknowledgment logic independently from the full link reset process, such as when using phylink. No functional change intended. Signed-off-by: Oleksij Rempel Reviewed-by: Thangaraj Samynathan --- changes v6: - this patch is added in v6 --- drivers/net/usb/lan78xx.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/drivers/net/usb/lan78xx.c b/drivers/net/usb/lan78xx.c index 07530eef82cb..de2b429e906e 100644 --- a/drivers/net/usb/lan78xx.c +++ b/drivers/net/usb/lan78xx.c @@ -1636,6 +1636,20 @@ static int lan78xx_mac_reset(struct lan78xx_net *dev) return ret; } =20 +/** + * lan78xx_phy_int_ack - Acknowledge PHY interrupt + * @dev: pointer to the LAN78xx device structure + * + * This function acknowledges the PHY interrupt by setting the + * INT_STS_PHY_INT_ bit in the interrupt status register (INT_STS). + * + * Return: 0 on success or a negative error code on failure. + */ +static int lan78xx_phy_int_ack(struct lan78xx_net *dev) +{ + return lan78xx_write_reg(dev, INT_STS, INT_STS_PHY_INT_); +} + static int lan78xx_link_reset(struct lan78xx_net *dev) { struct phy_device *phydev =3D dev->net->phydev; @@ -1644,7 +1658,7 @@ static int lan78xx_link_reset(struct lan78xx_net *dev) u32 buf; =20 /* clear LAN78xx interrupt status */ - ret =3D lan78xx_write_reg(dev, INT_STS, INT_STS_PHY_INT_); + ret =3D lan78xx_phy_int_ack(dev); if (unlikely(ret < 0)) return ret; =20 --=20 2.39.5 From nobody Mon Feb 9 01:48:37 2026 Received: from metis.whiteo.stw.pengutronix.de (metis.whiteo.stw.pengutronix.de [185.203.201.7]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EB6161E5218 for ; Mon, 5 May 2025 08:43:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.203.201.7 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746434632; cv=none; b=fRHvtDCHduKacf18d3j2jYyL+SiRdkPggiP6HhOSVZxjvjdyv0iWJB5kQIGAWIwMwmumpTZBHdX68lGh+xtCUe30hYuSfT2ZVl5QfWuTJyf4ThhxWLBoHIqkYEHgNRwSdz+yQwRYAT8M7hoT+/YpIHtEPz5b9imybagrrlULMQs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746434632; c=relaxed/simple; bh=yDBPrVSL+UwTdx3j7DqwG9mfpeNf/ZqzrDQtnxyuIIs=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=afQ5gPFyXWuqu5LKVeYwtG1zc4+cz0pWBDrEzXmNavwDnhAHJhcksLDC/uO9/R7fLoJy3Ag9mj50tZiv+7gqtfBnG6eU1s1O9G6ZGL2ctTq/V7EO9oyKxzjd3KpGpHo8y8yH2EGlY46UQBfYMRLPA2dPqfR9i0h0aaOPkQovjRQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pengutronix.de; spf=pass smtp.mailfrom=pengutronix.de; arc=none smtp.client-ip=185.203.201.7 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pengutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pengutronix.de Received: from drehscheibe.grey.stw.pengutronix.de ([2a0a:edc0:0:c01:1d::a2]) by metis.whiteo.stw.pengutronix.de with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1uBrQN-0005WA-Al; Mon, 05 May 2025 10:43:43 +0200 Received: from dude04.red.stw.pengutronix.de ([2a0a:edc0:0:1101:1d::ac]) by drehscheibe.grey.stw.pengutronix.de with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1uBrQM-001CSI-1X; Mon, 05 May 2025 10:43:42 +0200 Received: from ore by dude04.red.stw.pengutronix.de with local (Exim 4.96) (envelope-from ) id 1uBrQM-003SQa-1D; Mon, 05 May 2025 10:43:42 +0200 From: Oleksij Rempel To: "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Woojung Huh , Andrew Lunn , Russell King , Thangaraj Samynathan , Rengarajan Sundararajan Cc: Oleksij Rempel , kernel@pengutronix.de, linux-kernel@vger.kernel.org, netdev@vger.kernel.org, UNGLinuxDriver@microchip.com, Phil Elwell , Maxime Chevallier , Simon Horman Subject: [PATCH net-next v8 6/7] net: usb: lan78xx: Refactor USB link power configuration into helper Date: Mon, 5 May 2025 10:43:40 +0200 Message-Id: <20250505084341.824165-7-o.rempel@pengutronix.de> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20250505084341.824165-1-o.rempel@pengutronix.de> References: <20250505084341.824165-1-o.rempel@pengutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-SA-Exim-Connect-IP: 2a0a:edc0:0:c01:1d::a2 X-SA-Exim-Mail-From: ore@pengutronix.de X-SA-Exim-Scanned: No (on metis.whiteo.stw.pengutronix.de); SAEximRunCond expanded to false X-PTX-Original-Recipient: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Move the USB link power configuration logic from lan78xx_link_reset() to a new helper function lan78xx_configure_usb(). This simplifies the main link reset path and isolates USB-specific logic. The new function handles U1/U2 enablement based on Ethernet link speed, but only for SuperSpeed-capable devices (LAN7800 and LAN7801). LAN7850, a High-Speed-only device, is explicitly excluded. A warning is logged if SuperSpeed is reported unexpectedly for LAN7850. Add a forward declaration for lan78xx_configure_usb() as preparation for the upcoming phylink conversion, where it will also be used from the mac_link_up() callback. Open questions remain: - Why is the 1000 Mbps configuration split into two steps (U2 disable, then U1 enable), unlike the single-step config used for 10/100 Mbps? - U1/U2 behavior appears to depend on proper EEPROM configuration. There are known devices in the field without EEPROM. Should the driver enforce safe defaults in such cases? Due to lack of USB subsystem expertise, no changes were made to this logic beyond structural refactoring. Signed-off-by: Oleksij Rempel --- changes v6: - this patch is added in v6 --- drivers/net/usb/lan78xx.c | 90 +++++++++++++++++++++++++-------------- 1 file changed, 59 insertions(+), 31 deletions(-) diff --git a/drivers/net/usb/lan78xx.c b/drivers/net/usb/lan78xx.c index de2b429e906e..bff53324c70a 100644 --- a/drivers/net/usb/lan78xx.c +++ b/drivers/net/usb/lan78xx.c @@ -1650,12 +1650,13 @@ static int lan78xx_phy_int_ack(struct lan78xx_net *= dev) return lan78xx_write_reg(dev, INT_STS, INT_STS_PHY_INT_); } =20 +static int lan78xx_configure_usb(struct lan78xx_net *dev, int speed); + static int lan78xx_link_reset(struct lan78xx_net *dev) { struct phy_device *phydev =3D dev->net->phydev; struct ethtool_link_ksettings ecmd; int ladv, radv, ret, link; - u32 buf; =20 /* clear LAN78xx interrupt status */ ret =3D lan78xx_phy_int_ack(dev); @@ -1681,36 +1682,9 @@ static int lan78xx_link_reset(struct lan78xx_net *de= v) =20 phy_ethtool_ksettings_get(phydev, &ecmd); =20 - if (dev->udev->speed =3D=3D USB_SPEED_SUPER) { - if (ecmd.base.speed =3D=3D 1000) { - /* disable U2 */ - ret =3D lan78xx_read_reg(dev, USB_CFG1, &buf); - if (ret < 0) - return ret; - buf &=3D ~USB_CFG1_DEV_U2_INIT_EN_; - ret =3D lan78xx_write_reg(dev, USB_CFG1, buf); - if (ret < 0) - return ret; - /* enable U1 */ - ret =3D lan78xx_read_reg(dev, USB_CFG1, &buf); - if (ret < 0) - return ret; - buf |=3D USB_CFG1_DEV_U1_INIT_EN_; - ret =3D lan78xx_write_reg(dev, USB_CFG1, buf); - if (ret < 0) - return ret; - } else { - /* enable U1 & U2 */ - ret =3D lan78xx_read_reg(dev, USB_CFG1, &buf); - if (ret < 0) - return ret; - buf |=3D USB_CFG1_DEV_U2_INIT_EN_; - buf |=3D USB_CFG1_DEV_U1_INIT_EN_; - ret =3D lan78xx_write_reg(dev, USB_CFG1, buf); - if (ret < 0) - return ret; - } - } + ret =3D lan78xx_configure_usb(dev, ecmd.base.speed); + if (ret < 0) + return ret; =20 ladv =3D phy_read(phydev, MII_ADVERTISE); if (ladv < 0) @@ -2522,6 +2496,60 @@ static void lan78xx_remove_irq_domain(struct lan78xx= _net *dev) dev->domain_data.irqdomain =3D NULL; } =20 +/** + * lan78xx_configure_usb - Configure USB link power settings + * @dev: pointer to the LAN78xx device structure + * @speed: negotiated Ethernet link speed (in Mbps) + * + * This function configures U1/U2 link power management for SuperSpeed + * USB devices based on the current Ethernet link speed. It uses the + * USB_CFG1 register to enable or disable U1 and U2 low-power states. + * + * Note: Only LAN7800 and LAN7801 support SuperSpeed (USB 3.x). + * LAN7850 is a High-Speed-only (USB 2.0) device and is skipped. + * + * Return: 0 on success or a negative error code on failure. + */ +static int lan78xx_configure_usb(struct lan78xx_net *dev, int speed) +{ + u32 mask, val; + int ret; + + /* Only configure USB settings for SuperSpeed devices */ + if (dev->udev->speed !=3D USB_SPEED_SUPER) + return 0; + + /* LAN7850 does not support USB 3.x */ + if (dev->chipid =3D=3D ID_REV_CHIP_ID_7850_) { + netdev_warn_once(dev->net, "Unexpected SuperSpeed for LAN7850 (USB 2.0 o= nly)\n"); + return 0; + } + + switch (speed) { + case SPEED_1000: + /* Disable U2, enable U1 */ + ret =3D lan78xx_update_reg(dev, USB_CFG1, + USB_CFG1_DEV_U2_INIT_EN_, 0); + if (ret < 0) + return ret; + + return lan78xx_update_reg(dev, USB_CFG1, + USB_CFG1_DEV_U1_INIT_EN_, + USB_CFG1_DEV_U1_INIT_EN_); + + case SPEED_100: + case SPEED_10: + /* Enable both U1 and U2 */ + mask =3D USB_CFG1_DEV_U1_INIT_EN_ | USB_CFG1_DEV_U2_INIT_EN_; + val =3D mask; + return lan78xx_update_reg(dev, USB_CFG1, mask, val); + + default: + netdev_warn(dev->net, "Unsupported link speed: %d\n", speed); + return -EINVAL; + } +} + /** * lan78xx_register_fixed_phy() - Register a fallback fixed PHY * @dev: LAN78xx device --=20 2.39.5 From nobody Mon Feb 9 01:48:37 2026 Received: from metis.whiteo.stw.pengutronix.de (metis.whiteo.stw.pengutronix.de [185.203.201.7]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A35DE1A2393 for ; Mon, 5 May 2025 08:43:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.203.201.7 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746434631; cv=none; b=E1ZztBD1omB0Mfnx45eRikmaoxWffCVS2DlTdJ0vvKL8K9nV+3ctixSEEAhyQsptNJTw1klXOu4PuREYp6BRWko7VrWIT+pcnIGHUo2rYG4UCBFoERfJ1UxLU2PWFFGTay0Ly6fC1jC9aV5YOEZNmGEklhCYOXfvMN9DueBKQy0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746434631; c=relaxed/simple; bh=Ym/VO1lsf4cxLe2zwu26BSqCE0/7Tphe1tRXLpCeV9Q=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Type; b=pNfLnfFGN/hzZu6wL1/husacykM4ZZwus5S9o1wIws0hOReSLhjTenXEsZnOmJT99c8fms3XYiHPiBlOc0ZaM4CR3eXMpVsbaZhxnruwkhDv6wg15SCEyb/I/r5OShuvJUNbznQA43Swuk/M+p+HNcChv4BnjZ6YI8UQht4wOj4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pengutronix.de; spf=pass smtp.mailfrom=pengutronix.de; arc=none smtp.client-ip=185.203.201.7 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pengutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pengutronix.de Received: from drehscheibe.grey.stw.pengutronix.de ([2a0a:edc0:0:c01:1d::a2]) by metis.whiteo.stw.pengutronix.de with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1uBrQN-0005WC-Ak; Mon, 05 May 2025 10:43:43 +0200 Received: from dude04.red.stw.pengutronix.de ([2a0a:edc0:0:1101:1d::ac]) by drehscheibe.grey.stw.pengutronix.de with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1uBrQM-001CSK-1d; Mon, 05 May 2025 10:43:42 +0200 Received: from ore by dude04.red.stw.pengutronix.de with local (Exim 4.96) (envelope-from ) id 1uBrQM-003SQk-1H; Mon, 05 May 2025 10:43:42 +0200 From: Oleksij Rempel To: "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Woojung Huh , Andrew Lunn , Russell King , Thangaraj Samynathan , Rengarajan Sundararajan Cc: Oleksij Rempel , kernel@pengutronix.de, linux-kernel@vger.kernel.org, netdev@vger.kernel.org, UNGLinuxDriver@microchip.com, Phil Elwell , Maxime Chevallier , Simon Horman Subject: [PATCH net-next v8 7/7] net: usb: lan78xx: Extract flow control configuration to helper Date: Mon, 5 May 2025 10:43:41 +0200 Message-Id: <20250505084341.824165-8-o.rempel@pengutronix.de> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20250505084341.824165-1-o.rempel@pengutronix.de> References: <20250505084341.824165-1-o.rempel@pengutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-SA-Exim-Connect-IP: 2a0a:edc0:0:c01:1d::a2 X-SA-Exim-Mail-From: ore@pengutronix.de X-SA-Exim-Scanned: No (on metis.whiteo.stw.pengutronix.de); SAEximRunCond expanded to false X-PTX-Original-Recipient: linux-kernel@vger.kernel.org Move flow control register configuration from lan78xx_update_flowcontrol() into a new helper function lan78xx_configure_flowcontrol(). This separates hardware-specific programming from policy logic and simplifies the upcoming phylink integration. The values used in this initial version of lan78xx_configure_flowcontrol() are taken over as-is from the original implementation to avoid functional changes. While they may not be optimal for all USB and link speed combinations, they are known to work reliably. Optimization of pause time and thresholds based on runtime conditions can be done in a separate follow-up patch. The forward declaration of lan78xx_configure_flowcontrol() will also be removed later during the phylink conversion. Signed-off-by: Oleksij Rempel --- changes v6: - this patch is added in v6 --- drivers/net/usb/lan78xx.c | 105 +++++++++++++++++++++++++++++++------- 1 file changed, 87 insertions(+), 18 deletions(-) diff --git a/drivers/net/usb/lan78xx.c b/drivers/net/usb/lan78xx.c index bff53324c70a..58e3589e3b89 100644 --- a/drivers/net/usb/lan78xx.c +++ b/drivers/net/usb/lan78xx.c @@ -1554,10 +1554,12 @@ static void lan78xx_set_multicast(struct net_device= *netdev) schedule_work(&pdata->set_multicast); } =20 +static int lan78xx_configure_flowcontrol(struct lan78xx_net *dev, + bool tx_pause, bool rx_pause); + static int lan78xx_update_flowcontrol(struct lan78xx_net *dev, u8 duplex, u16 lcladv, u16 rmtadv) { - u32 flow =3D 0, fct_flow =3D 0; u8 cap; =20 if (dev->fc_autoneg) @@ -1565,27 +1567,13 @@ static int lan78xx_update_flowcontrol(struct lan78x= x_net *dev, u8 duplex, else cap =3D dev->fc_request_control; =20 - if (cap & FLOW_CTRL_TX) - flow |=3D (FLOW_CR_TX_FCEN_ | 0xFFFF); - - if (cap & FLOW_CTRL_RX) - flow |=3D FLOW_CR_RX_FCEN_; - - if (dev->udev->speed =3D=3D USB_SPEED_SUPER) - fct_flow =3D FLOW_CTRL_THRESHOLD(FLOW_ON_SS, FLOW_OFF_SS); - else if (dev->udev->speed =3D=3D USB_SPEED_HIGH) - fct_flow =3D FLOW_CTRL_THRESHOLD(FLOW_ON_HS, FLOW_OFF_HS); - netif_dbg(dev, link, dev->net, "rx pause %s, tx pause %s", (cap & FLOW_CTRL_RX ? "enabled" : "disabled"), (cap & FLOW_CTRL_TX ? "enabled" : "disabled")); =20 - lan78xx_write_reg(dev, FCT_FLOW, fct_flow); - - /* threshold value should be set before enabling flow */ - lan78xx_write_reg(dev, FLOW, flow); - - return 0; + return lan78xx_configure_flowcontrol(dev, + cap & FLOW_CTRL_TX, + cap & FLOW_CTRL_RX); } =20 static void lan78xx_rx_urb_submit_all(struct lan78xx_net *dev); @@ -2550,6 +2538,87 @@ static int lan78xx_configure_usb(struct lan78xx_net = *dev, int speed) } } =20 +/** + * lan78xx_configure_flowcontrol - Set MAC and FIFO flow control configura= tion + * @dev: pointer to the LAN78xx device structure + * @tx_pause: enable transmission of pause frames + * @rx_pause: enable reception of pause frames + * + * This function configures the LAN78xx flow control settings by writing + * to the FLOW and FCT_FLOW registers. The pause time is set to the + * maximum allowed value (65535 quanta). FIFO thresholds are selected + * based on USB speed. + * + * The Pause Time field is measured in units of 512-bit times (quanta): + * - At 1 Gbps: 1 quanta =3D 512 ns =E2=86=92 max ~33.6 ms pause + * - At 100 Mbps: 1 quanta =3D 5.12 =C2=B5s =E2=86=92 max ~335 ms pause + * - At 10 Mbps: 1 quanta =3D 51.2 =C2=B5s =E2=86=92 max ~3.3 s pause + * + * Flow control thresholds (FCT_FLOW) are used to trigger pause/resume: + * - RXUSED is the number of bytes used in the RX FIFO + * - Flow is turned ON when RXUSED =E2=89=A5 FLOW_ON threshold + * - Flow is turned OFF when RXUSED =E2=89=A4 FLOW_OFF threshold + * - Both thresholds are encoded in units of 512 bytes (rounded up) + * + * Thresholds differ by USB speed because available USB bandwidth + * affects how fast packets can be drained from the RX FIFO: + * - USB 3.x (SuperSpeed): + * FLOW_ON =3D 9216 bytes =E2=86=92 18 units + * FLOW_OFF =3D 4096 bytes =E2=86=92 8 units + * - USB 2.0 (High-Speed): + * FLOW_ON =3D 8704 bytes =E2=86=92 17 units + * FLOW_OFF =3D 1024 bytes =E2=86=92 2 units + * + * Note: The FCT_FLOW register must be configured before enabling TX pause + * (i.e., before setting FLOW_CR_TX_FCEN_), as required by the hardw= are. + * + * Return: 0 on success or a negative error code on failure. + */ +static int lan78xx_configure_flowcontrol(struct lan78xx_net *dev, + bool tx_pause, bool rx_pause) +{ + /* Use maximum pause time: 65535 quanta (512-bit times) */ + const u32 pause_time_quanta =3D 65535; + u32 fct_flow =3D 0; + u32 flow =3D 0; + int ret; + + /* Prepare MAC flow control bits */ + if (tx_pause) + flow |=3D FLOW_CR_TX_FCEN_ | pause_time_quanta; + + if (rx_pause) + flow |=3D FLOW_CR_RX_FCEN_; + + /* Select RX FIFO thresholds based on USB speed + * + * FCT_FLOW layout: + * bits [6:0] FLOW_ON threshold (RXUSED =E2=89=A5 ON =E2=86=92 assert= pause) + * bits [14:8] FLOW_OFF threshold (RXUSED =E2=89=A4 OFF =E2=86=92 deas= sert pause) + * thresholds are expressed in units of 512 bytes + */ + switch (dev->udev->speed) { + case USB_SPEED_SUPER: + fct_flow =3D FLOW_CTRL_THRESHOLD(FLOW_ON_SS, FLOW_OFF_SS); + break; + case USB_SPEED_HIGH: + fct_flow =3D FLOW_CTRL_THRESHOLD(FLOW_ON_HS, FLOW_OFF_HS); + break; + default: + netdev_warn(dev->net, "Unsupported USB speed: %d\n", + dev->udev->speed); + return -EINVAL; + } + + /* Step 1: Write FIFO thresholds before enabling pause frames */ + ret =3D lan78xx_write_reg(dev, FCT_FLOW, fct_flow); + if (ret < 0) + return ret; + + /* Step 2: Enable MAC pause functionality */ + return lan78xx_write_reg(dev, FLOW, flow); +} + /** * lan78xx_register_fixed_phy() - Register a fallback fixed PHY * @dev: LAN78xx device --=20 2.39.5