diff mbox series

[net-next,3/3] net: phy: add support for PHY package MMD read/write

Message ID 20231126003748.9600-3-ansuelsmth@gmail.com (mailing list archive)
State Superseded
Delegated to: Netdev Maintainers
Headers show
Series [net-next,1/3] net: phy: extend PHY package API to support multiple global address | expand

Checks

Context Check Description
netdev/series_format warning Series does not have a cover letter
netdev/codegen success Generated files up to date
netdev/tree_selection success Clearly marked for net-next
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 1565 this patch: 1565
netdev/cc_maintainers success CCed 4 of 4 maintainers
netdev/build_clang success Errors and warnings before: 1158 this patch: 1158
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 1602 this patch: 1602
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 80 lines checked
netdev/build_clang_rust success No Rust files in patch. Skipping build
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0

Commit Message

Christian Marangi Nov. 26, 2023, 12:37 a.m. UTC
Some PHY in PHY package may require to read/write MMD regs to correctly
configure the PHY package.

Add support for these additional required function in both lock and no
lock variant.

Signed-off-by: Christian Marangi <ansuelsmth@gmail.com>
---
 include/linux/phy.h | 74 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 74 insertions(+)

Comments

Florian Fainelli Nov. 26, 2023, 12:52 a.m. UTC | #1
On 11/25/2023 4:37 PM, Christian Marangi wrote:
> Some PHY in PHY package may require to read/write MMD regs to correctly
> configure the PHY package.
> 
> Add support for these additional required function in both lock and no
> lock variant.
> 
> Signed-off-by: Christian Marangi <ansuelsmth@gmail.com>
> ---
>   include/linux/phy.h | 74 +++++++++++++++++++++++++++++++++++++++++++++
>   1 file changed, 74 insertions(+)
> 
> diff --git a/include/linux/phy.h b/include/linux/phy.h
> index 984bca9a82f4..1799133c8387 100644
> --- a/include/linux/phy.h
> +++ b/include/linux/phy.h
> @@ -2067,6 +2067,80 @@ static inline int __phy_package_write(struct phy_device *phydev,
>   	return __mdiobus_write(phydev->mdio.bus, addr, regnum, val);
>   }
>   
> +static inline int phy_package_read_mmd(struct phy_device *phydev,
> +				       unsigned int addr_offset, int devad,
> +				       u32 regnum)
> +{
> +	struct phy_package_shared *shared = phydev->shared;
> +	struct mii_bus *bus = phydev->mdio.bus;
> +	int addr, val;
> +
> +	if (!shared || shared->base_addr + addr_offset > PHY_MAX_ADDR)
> +		return -EIO;

You might be off by one here, should not that >= PHY_MAX_ADDR here and 
below?
Christian Marangi Nov. 26, 2023, 12:54 a.m. UTC | #2
On Sat, Nov 25, 2023 at 04:52:19PM -0800, Florian Fainelli wrote:
> 
> 
> On 11/25/2023 4:37 PM, Christian Marangi wrote:
> > Some PHY in PHY package may require to read/write MMD regs to correctly
> > configure the PHY package.
> > 
> > Add support for these additional required function in both lock and no
> > lock variant.
> > 
> > Signed-off-by: Christian Marangi <ansuelsmth@gmail.com>
> > ---
> >   include/linux/phy.h | 74 +++++++++++++++++++++++++++++++++++++++++++++
> >   1 file changed, 74 insertions(+)
> > 
> > diff --git a/include/linux/phy.h b/include/linux/phy.h
> > index 984bca9a82f4..1799133c8387 100644
> > --- a/include/linux/phy.h
> > +++ b/include/linux/phy.h
> > @@ -2067,6 +2067,80 @@ static inline int __phy_package_write(struct phy_device *phydev,
> >   	return __mdiobus_write(phydev->mdio.bus, addr, regnum, val);
> >   }
> > +static inline int phy_package_read_mmd(struct phy_device *phydev,
> > +				       unsigned int addr_offset, int devad,
> > +				       u32 regnum)
> > +{
> > +	struct phy_package_shared *shared = phydev->shared;
> > +	struct mii_bus *bus = phydev->mdio.bus;
> > +	int addr, val;
> > +
> > +	if (!shared || shared->base_addr + addr_offset > PHY_MAX_ADDR)
> > +		return -EIO;
> 
> You might be off by one here, should not that >= PHY_MAX_ADDR here and
> below?

Thanks for the review. Yes PHY_MAX_ADDR is 32 so I should use >=.

(interesting choice to use 32 instead of 31 as MAX, guess an old mistake)
Florian Fainelli Nov. 26, 2023, 1 a.m. UTC | #3
On 11/25/2023 4:54 PM, Christian Marangi wrote:
> On Sat, Nov 25, 2023 at 04:52:19PM -0800, Florian Fainelli wrote:
>>
>>
>> On 11/25/2023 4:37 PM, Christian Marangi wrote:
>>> Some PHY in PHY package may require to read/write MMD regs to correctly
>>> configure the PHY package.
>>>
>>> Add support for these additional required function in both lock and no
>>> lock variant.
>>>
>>> Signed-off-by: Christian Marangi <ansuelsmth@gmail.com>
>>> ---
>>>    include/linux/phy.h | 74 +++++++++++++++++++++++++++++++++++++++++++++
>>>    1 file changed, 74 insertions(+)
>>>
>>> diff --git a/include/linux/phy.h b/include/linux/phy.h
>>> index 984bca9a82f4..1799133c8387 100644
>>> --- a/include/linux/phy.h
>>> +++ b/include/linux/phy.h
>>> @@ -2067,6 +2067,80 @@ static inline int __phy_package_write(struct phy_device *phydev,
>>>    	return __mdiobus_write(phydev->mdio.bus, addr, regnum, val);
>>>    }
>>> +static inline int phy_package_read_mmd(struct phy_device *phydev,
>>> +				       unsigned int addr_offset, int devad,
>>> +				       u32 regnum)
>>> +{
>>> +	struct phy_package_shared *shared = phydev->shared;
>>> +	struct mii_bus *bus = phydev->mdio.bus;
>>> +	int addr, val;
>>> +
>>> +	if (!shared || shared->base_addr + addr_offset > PHY_MAX_ADDR)
>>> +		return -EIO;
>>
>> You might be off by one here, should not that >= PHY_MAX_ADDR here and
>> below?
> 
> Thanks for the review. Yes PHY_MAX_ADDR is 32 so I should use >=.
> 
> (interesting choice to use 32 instead of 31 as MAX, guess an old mistake)

It has historically been used as an iterator upper bound, hence the 32.
Andrew Lunn Nov. 26, 2023, 6:14 p.m. UTC | #4
On Sun, Nov 26, 2023 at 01:37:48AM +0100, Christian Marangi wrote:
> Some PHY in PHY package may require to read/write MMD regs to correctly
> configure the PHY package.
> 
> Add support for these additional required function in both lock and no
> lock variant.

You are assuming the PHY only supports C45 over C22. But what about
those PHYs which have native C45? And maybe don't have C22 at all?

You should refactor the code of __phy_read_mmd() into a helper and use
it here.

   Andrew
Christian Marangi Nov. 26, 2023, 6:24 p.m. UTC | #5
On Sun, Nov 26, 2023 at 07:14:52PM +0100, Andrew Lunn wrote:
> On Sun, Nov 26, 2023 at 01:37:48AM +0100, Christian Marangi wrote:
> > Some PHY in PHY package may require to read/write MMD regs to correctly
> > configure the PHY package.
> > 
> > Add support for these additional required function in both lock and no
> > lock variant.
> 
> You are assuming the PHY only supports C45 over C22. But what about
> those PHYs which have native C45? And maybe don't have C22 at all?
> 
> You should refactor the code of __phy_read_mmd() into a helper and use
> it here.
>

Have to be honest here and chose the ""easy"" way. Was a little scared
by all the complexity with mmd with C45 and C22... The idea was to add
C22 support for now and if used by others extend the function. But I
guess with the generic approach we are taking it's better to handle it
now. Will update this in v2.
diff mbox series

Patch

diff --git a/include/linux/phy.h b/include/linux/phy.h
index 984bca9a82f4..1799133c8387 100644
--- a/include/linux/phy.h
+++ b/include/linux/phy.h
@@ -2067,6 +2067,80 @@  static inline int __phy_package_write(struct phy_device *phydev,
 	return __mdiobus_write(phydev->mdio.bus, addr, regnum, val);
 }
 
+static inline int phy_package_read_mmd(struct phy_device *phydev,
+				       unsigned int addr_offset, int devad,
+				       u32 regnum)
+{
+	struct phy_package_shared *shared = phydev->shared;
+	struct mii_bus *bus = phydev->mdio.bus;
+	int addr, val;
+
+	if (!shared || shared->base_addr + addr_offset > PHY_MAX_ADDR)
+		return -EIO;
+
+	addr = shared->base_addr + addr_offset;
+
+	phy_lock_mdio_bus(phydev);
+	mmd_phy_indirect(bus, addr, devad, regnum);
+	val = __mdiobus_read(bus, addr, MII_MMD_DATA);
+	phy_unlock_mdio_bus(phydev);
+
+	return val;
+}
+
+static inline int __phy_package_read_mmd(struct phy_device *phydev,
+					 unsigned int addr_offset, int devad,
+					 u32 regnum)
+{
+	struct phy_package_shared *shared = phydev->shared;
+	struct mii_bus *bus = phydev->mdio.bus;
+	int addr;
+
+	if (!shared || shared->base_addr + addr_offset > PHY_MAX_ADDR)
+		return -EIO;
+
+	addr = shared->base_addr + addr_offset;
+	mmd_phy_indirect(bus, addr, devad, regnum);
+	return __mdiobus_read(bus, addr, MII_MMD_DATA);
+}
+
+static inline int phy_package_write_mmd(struct phy_device *phydev,
+					unsigned int addr_offset, int devad,
+					u32 regnum, u16 val)
+{
+	struct phy_package_shared *shared = phydev->shared;
+	struct mii_bus *bus = phydev->mdio.bus;
+	int addr, ret;
+
+	if (!shared || shared->base_addr + addr_offset > PHY_MAX_ADDR)
+		return -EIO;
+
+	addr = shared->base_addr + addr_offset;
+
+	phy_lock_mdio_bus(phydev);
+	mmd_phy_indirect(bus, addr, devad, regnum);
+	ret = __mdiobus_write(bus, addr, MII_MMD_DATA, val);
+	phy_unlock_mdio_bus(phydev);
+
+	return ret;
+}
+
+static inline int __phy_package_write_mmd(struct phy_device *phydev,
+					  unsigned int addr_offset, int devad,
+					  u32 regnum, u16 val)
+{
+	struct phy_package_shared *shared = phydev->shared;
+	struct mii_bus *bus = phydev->mdio.bus;
+	int addr;
+
+	if (!shared || shared->base_addr + addr_offset > PHY_MAX_ADDR)
+		return -EIO;
+
+	addr = shared->base_addr + addr_offset;
+	mmd_phy_indirect(bus, addr, devad, regnum);
+	return __mdiobus_write(bus, addr, MII_MMD_DATA, val);
+}
+
 static inline bool __phy_package_set_once(struct phy_device *phydev,
 					  unsigned int b)
 {