netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Michael Walle <mwalle@kernel.org>
To: "Andrew Lunn" <andrew@lunn.ch>,
	"Heiner Kallweit" <hkallweit1@gmail.com>,
	"Russell King" <linux@armlinux.org.uk>,
	"David S. Miller" <davem@davemloft.net>,
	"Eric Dumazet" <edumazet@google.com>,
	"Jakub Kicinski" <kuba@kernel.org>,
	"Paolo Abeni" <pabeni@redhat.com>,
	"Yisen Zhuang" <yisen.zhuang@huawei.com>,
	"Salil Mehta" <salil.mehta@huawei.com>,
	"Florian Fainelli" <florian.fainelli@broadcom.com>,
	"Broadcom internal kernel review list"
	<bcm-kernel-feedback-list@broadcom.com>,
	"Marek Behún" <kabel@kernel.org>, "Xu Liang" <lxu@maxlinear.com>
Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org,
	 Simon Horman <simon.horman@corigine.com>,
	Michael Walle <mwalle@kernel.org>
Subject: [PATCH net-next v3 07/11] net: phy: introduce phy_mdiobus_read_mmd()
Date: Wed, 12 Jul 2023 17:07:07 +0200	[thread overview]
Message-ID: <20230620-feature-c45-over-c22-v3-7-9eb37edf7be0@kernel.org> (raw)
In-Reply-To: <20230620-feature-c45-over-c22-v3-0-9eb37edf7be0@kernel.org>

Factor out the low-level MDIO bus access code in
__phy_{read,write}_mmd() into individual helper functions. These can
then be used without a struct phy_device, which is needed in the PHY
probing code.

To decide which access - direct or indirect - is used, move away from
phy_has_c45_registers(). That function only indicates whether the PHY
has C45 registers, but not how they are accessed. Instead look at the
access_mode property.

Export a locked variant of the read for the PHY probing code.

Signed-off-by: Michael Walle <mwalle@kernel.org>
---
v3:
 - new patch
---
 drivers/net/phy/phy-core.c | 109 +++++++++++++++++++++++++++++++--------------
 include/linux/phy.h        |   3 ++
 2 files changed, 78 insertions(+), 34 deletions(-)

diff --git a/drivers/net/phy/phy-core.c b/drivers/net/phy/phy-core.c
index 598023610ee5..4863a85f8385 100644
--- a/drivers/net/phy/phy-core.c
+++ b/drivers/net/phy/phy-core.c
@@ -546,6 +546,73 @@ static int mmd_phy_indirect(struct mii_bus *bus, int phy_addr, int devad,
 	return check_rc ? ret : 0;
 }
 
+static int __phy_mdiobus_read_mmd(struct mii_bus *bus, int phy_addr,
+				  enum phy_access_mode access_mode,
+				  int devad, u32 regnum)
+{
+	switch (access_mode) {
+	case PHY_ACCESS_C45:
+		return __mdiobus_c45_read(bus, phy_addr, devad, regnum);
+	case PHY_ACCESS_C22:
+		/* ignore return value for legacy reasons */
+		mmd_phy_indirect(bus, phy_addr, devad, regnum, false);
+
+		/* Read the content of the MMD's selected register */
+		return __mdiobus_read(bus, phy_addr, MII_MMD_DATA);
+	default:
+		return -EOPNOTSUPP;
+	}
+}
+
+/**
+ * phy_mdiobus_read_mmd - low-level function for reading a register
+ *
+ * @bus: the target MII bus
+ * @phy_addr: PHY address on the MII bus
+ * @mode: Access mode of the PHY
+ * @devad: The target MMD (0..31)
+ * @regnum: The target register on the MMD (0..65535)
+ *
+ * Similar to phy_read_mmd() except that it can be used without a phydev and
+ * operates on the MII bus.
+ */
+int phy_mdiobus_read_mmd(struct mii_bus *bus, int phy_addr,
+			 enum phy_access_mode mode,
+			 int devad, u32 regnum)
+{
+	int ret;
+
+	if (regnum > (u16)~0 || devad > 32)
+		return -EINVAL;
+
+	mutex_lock(&bus->mdio_lock);
+	ret = __phy_mdiobus_read_mmd(bus, phy_addr, mode, devad, regnum);
+	mutex_unlock(&bus->mdio_lock);
+
+	return ret;
+}
+EXPORT_SYMBOL(phy_mdiobus_read_mmd);
+
+static int __phy_mdiobus_write_mmd(struct mii_bus *bus, int phy_addr,
+				   enum phy_access_mode mode,
+				   int devad, u32 regnum, u16 val)
+{
+	switch (mode) {
+	case PHY_ACCESS_C45:
+		return __mdiobus_c45_write(bus, phy_addr, devad, regnum, val);
+	case PHY_ACCESS_C22:
+		/* ignore return value for legacy reasons */
+		mmd_phy_indirect(bus, phy_addr, devad, regnum, false);
+
+		/* Write the data into MMD's selected register */
+		__mdiobus_write(bus, phy_addr, MII_MMD_DATA, val);
+
+		return 0;
+	default:
+		return -EOPNOTSUPP;
+	}
+}
+
 /**
  * __phy_read_mmd - Convenience function for reading a register
  * from an MMD on a given PHY.
@@ -557,26 +624,14 @@ static int mmd_phy_indirect(struct mii_bus *bus, int phy_addr, int devad,
  */
 int __phy_read_mmd(struct phy_device *phydev, int devad, u32 regnum)
 {
-	int val;
-
 	if (regnum > (u16)~0 || devad > 32)
 		return -EINVAL;
 
-	if (phydev->drv && phydev->drv->read_mmd) {
-		val = phydev->drv->read_mmd(phydev, devad, regnum);
-	} else if (phy_has_c45_registers(phydev)) {
-		val = __mdiobus_c45_read(phydev->mdio.bus, phydev->mdio.addr,
-					 devad, regnum);
-	} else {
-		struct mii_bus *bus = phydev->mdio.bus;
-		int phy_addr = phydev->mdio.addr;
-
-		mmd_phy_indirect(bus, phy_addr, devad, regnum, false);
+	if (phydev->drv && phydev->drv->read_mmd)
+		return phydev->drv->read_mmd(phydev, devad, regnum);
 
-		/* Read the content of the MMD's selected register */
-		val = __mdiobus_read(bus, phy_addr, MII_MMD_DATA);
-	}
-	return val;
+	return __phy_mdiobus_read_mmd(phydev->mdio.bus, phydev->mdio.addr,
+				      phydev->access_mode, devad, regnum);
 }
 EXPORT_SYMBOL(__phy_read_mmd);
 
@@ -613,28 +668,14 @@ EXPORT_SYMBOL(phy_read_mmd);
  */
 int __phy_write_mmd(struct phy_device *phydev, int devad, u32 regnum, u16 val)
 {
-	int ret;
-
 	if (regnum > (u16)~0 || devad > 32)
 		return -EINVAL;
 
-	if (phydev->drv && phydev->drv->write_mmd) {
-		ret = phydev->drv->write_mmd(phydev, devad, regnum, val);
-	} else if (phy_has_c45_registers(phydev)) {
-		ret = __mdiobus_c45_write(phydev->mdio.bus, phydev->mdio.addr,
-					  devad, regnum, val);
-	} else {
-		struct mii_bus *bus = phydev->mdio.bus;
-		int phy_addr = phydev->mdio.addr;
-
-		mmd_phy_indirect(bus, phy_addr, devad, regnum, false);
-
-		/* Write the data into MMD's selected register */
-		__mdiobus_write(bus, phy_addr, MII_MMD_DATA, val);
+	if (phydev->drv && phydev->drv->write_mmd)
+		return phydev->drv->write_mmd(phydev, devad, regnum, val);
 
-		ret = 0;
-	}
-	return ret;
+	return __phy_mdiobus_write_mmd(phydev->mdio.bus, phydev->mdio.addr,
+				       phydev->access_mode, devad, regnum, val);
 }
 EXPORT_SYMBOL(__phy_write_mmd);
 
diff --git a/include/linux/phy.h b/include/linux/phy.h
index cd67887a7289..a482696a17a6 100644
--- a/include/linux/phy.h
+++ b/include/linux/phy.h
@@ -1337,6 +1337,9 @@ int phy_read_mmd(struct phy_device *phydev, int devad, u32 regnum);
 	__ret; \
 })
 
+int phy_mdiobus_read_mmd(struct mii_bus *bus, int phy_addr,
+			 enum phy_access_mode mode,
+			 int devad, u32 regnum);
 /*
  * __phy_read_mmd - Convenience function for reading a register
  * from an MMD on a given PHY.

-- 
2.39.2


  parent reply	other threads:[~2023-07-12 15:07 UTC|newest]

Thread overview: 47+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-07-12 15:07 [PATCH net-next v3 00/11] net: phy: C45-over-C22 access Michael Walle
2023-07-12 15:07 ` [PATCH net-next v3 01/11] net: phy: get rid of redundant is_c45 information Michael Walle
2023-07-18 17:25   ` Andrew Lunn
2023-07-12 15:07 ` [PATCH net-next v3 02/11] net: phy: introduce phy_has_c45_registers() Michael Walle
2023-07-18 17:26   ` Andrew Lunn
2023-07-18 20:07   ` Andrew Lunn
2023-07-19  7:11     ` Michael Walle
2023-08-01 14:47       ` Michael Walle
2023-08-01 14:57         ` Russell King (Oracle)
2023-08-01 15:20           ` Michael Walle
2023-08-01 15:57             ` Russell King (Oracle)
2023-08-02 15:33               ` Michael Walle
2023-08-02 16:06                 ` Russell King (Oracle)
2023-08-02 17:11                   ` Michael Walle
2023-08-02 23:00                     ` Russell King (Oracle)
2023-08-02 16:15                 ` Andrew Lunn
2023-08-02 17:10                   ` Russell King (Oracle)
2023-08-02 22:21                     ` Andrew Lunn
2023-08-02 22:28                       ` Russell King (Oracle)
2023-09-05  8:22                         ` Michael Walle
2023-07-12 15:07 ` [PATCH net-next v3 03/11] net: phy: replace is_c45 with phy_accces_mode Michael Walle
2023-07-18 17:40   ` Andrew Lunn
2023-07-18 17:52     ` Russell King (Oracle)
2023-07-18 19:18       ` Andrew Lunn
2023-07-18 21:46         ` Russell King (Oracle)
2023-07-18 23:30           ` Andrew Lunn
2023-07-18 19:53     ` Michael Walle
2023-07-18 20:16       ` Andrew Lunn
2023-07-12 15:07 ` [PATCH net-next v3 04/11] net: phy: make the "prevent_c45_scan" a property of the MII bus Michael Walle
2023-07-18 23:31   ` Andrew Lunn
2023-07-12 15:07 ` [PATCH net-next v3 05/11] net: phy: print an info if a broken C45 bus is found Michael Walle
2023-07-18 23:32   ` Andrew Lunn
2023-07-12 15:07 ` [PATCH net-next v3 06/11] net: phy: add error checks in mmd_phy_indirect() Michael Walle
2023-07-18 23:34   ` Andrew Lunn
2023-07-12 15:07 ` Michael Walle [this message]
2023-07-18 23:54   ` [PATCH net-next v3 07/11] net: phy: introduce phy_mdiobus_read_mmd() Andrew Lunn
2023-07-19  7:21     ` Michael Walle
2023-07-12 15:07 ` [PATCH net-next v3 08/11] net: phy: add support for C45-over-C22 transfers Michael Walle
2023-07-13  8:56   ` Simon Horman
2023-07-13  9:00     ` Michael Walle
2023-07-13  9:19       ` Simon Horman
2023-07-12 15:07 ` [PATCH net-next v3 09/11] net: phy: introduce phy_promote_to_c45() Michael Walle
2023-07-13  8:56   ` Simon Horman
2023-07-12 15:07 ` [PATCH net-next v3 10/11] net: mdio: add C45-over-C22 fallback to fwnode_mdiobus_register_phy() Michael Walle
2023-07-19  0:03   ` Andrew Lunn
2023-07-19  7:32     ` Michael Walle
2023-07-12 15:07 ` [PATCH net-next v3 11/11] net: mdio: support C45-over-C22 when probed via OF Michael Walle

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=20230620-feature-c45-over-c22-v3-7-9eb37edf7be0@kernel.org \
    --to=mwalle@kernel.org \
    --cc=andrew@lunn.ch \
    --cc=bcm-kernel-feedback-list@broadcom.com \
    --cc=davem@davemloft.net \
    --cc=edumazet@google.com \
    --cc=florian.fainelli@broadcom.com \
    --cc=hkallweit1@gmail.com \
    --cc=kabel@kernel.org \
    --cc=kuba@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@armlinux.org.uk \
    --cc=lxu@maxlinear.com \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=salil.mehta@huawei.com \
    --cc=simon.horman@corigine.com \
    --cc=yisen.zhuang@huawei.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;
as well as URLs for NNTP newsgroup(s).