4a17d4645344bb440ab425810af36b58f751684f
[openwrt/staging/ansuel.git] /
1 From 028672bd1d73cf65249a420c1de75e8d2acd2f6a Mon Sep 17 00:00:00 2001
2 From: Christian Marangi <ansuelsmth@gmail.com>
3 Date: Fri, 15 Dec 2023 14:15:33 +0100
4 Subject: [PATCH 3/4] net: phy: restructure __phy_write/read_mmd to helper and
5 phydev user
6
7 Restructure phy_write_mmd and phy_read_mmd to implement generic helper
8 for direct mdiobus access for mmd and use these helper for phydev user.
9
10 This is needed in preparation of PHY package API that requires generic
11 access to the mdiobus and are deatched from phydev struct but instead
12 access them based on PHY package base_addr and offsets.
13
14 Signed-off-by: Christian Marangi <ansuelsmth@gmail.com>
15 Reviewed-by: Russell King (Oracle) <rmk+kernel@armlinux.org.uk>
16 Signed-off-by: David S. Miller <davem@davemloft.net>
17 ---
18 drivers/net/phy/phy-core.c | 64 ++++++++++++++++++--------------------
19 1 file changed, 30 insertions(+), 34 deletions(-)
20
21 --- a/drivers/net/phy/phy-core.c
22 +++ b/drivers/net/phy/phy-core.c
23 @@ -528,6 +528,28 @@ static void mmd_phy_indirect(struct mii_
24 devad | MII_MMD_CTRL_NOINCR);
25 }
26
27 +static int mmd_phy_read(struct mii_bus *bus, int phy_addr, bool is_c45,
28 + int devad, u32 regnum)
29 +{
30 + if (is_c45)
31 + return __mdiobus_c45_read(bus, phy_addr, devad, regnum);
32 +
33 + mmd_phy_indirect(bus, phy_addr, devad, regnum);
34 + /* Read the content of the MMD's selected register */
35 + return __mdiobus_read(bus, phy_addr, MII_MMD_DATA);
36 +}
37 +
38 +static int mmd_phy_write(struct mii_bus *bus, int phy_addr, bool is_c45,
39 + int devad, u32 regnum, u16 val)
40 +{
41 + if (is_c45)
42 + return __mdiobus_c45_write(bus, phy_addr, devad, regnum, val);
43 +
44 + mmd_phy_indirect(bus, phy_addr, devad, regnum);
45 + /* Write the data into MMD's selected register */
46 + return __mdiobus_write(bus, phy_addr, MII_MMD_DATA, val);
47 +}
48 +
49 /**
50 * __phy_read_mmd - Convenience function for reading a register
51 * from an MMD on a given PHY.
52 @@ -539,26 +561,14 @@ static void mmd_phy_indirect(struct mii_
53 */
54 int __phy_read_mmd(struct phy_device *phydev, int devad, u32 regnum)
55 {
56 - int val;
57 -
58 if (regnum > (u16)~0 || devad > 32)
59 return -EINVAL;
60
61 - if (phydev->drv && phydev->drv->read_mmd) {
62 - val = phydev->drv->read_mmd(phydev, devad, regnum);
63 - } else if (phydev->is_c45) {
64 - val = __mdiobus_c45_read(phydev->mdio.bus, phydev->mdio.addr,
65 - devad, regnum);
66 - } else {
67 - struct mii_bus *bus = phydev->mdio.bus;
68 - int phy_addr = phydev->mdio.addr;
69 -
70 - mmd_phy_indirect(bus, phy_addr, devad, regnum);
71 -
72 - /* Read the content of the MMD's selected register */
73 - val = __mdiobus_read(bus, phy_addr, MII_MMD_DATA);
74 - }
75 - return val;
76 + if (phydev->drv && phydev->drv->read_mmd)
77 + return phydev->drv->read_mmd(phydev, devad, regnum);
78 +
79 + return mmd_phy_read(phydev->mdio.bus, phydev->mdio.addr,
80 + phydev->is_c45, devad, regnum);
81 }
82 EXPORT_SYMBOL(__phy_read_mmd);
83
84 @@ -595,28 +605,14 @@ EXPORT_SYMBOL(phy_read_mmd);
85 */
86 int __phy_write_mmd(struct phy_device *phydev, int devad, u32 regnum, u16 val)
87 {
88 - int ret;
89 -
90 if (regnum > (u16)~0 || devad > 32)
91 return -EINVAL;
92
93 - if (phydev->drv && phydev->drv->write_mmd) {
94 - ret = phydev->drv->write_mmd(phydev, devad, regnum, val);
95 - } else if (phydev->is_c45) {
96 - ret = __mdiobus_c45_write(phydev->mdio.bus, phydev->mdio.addr,
97 - devad, regnum, val);
98 - } else {
99 - struct mii_bus *bus = phydev->mdio.bus;
100 - int phy_addr = phydev->mdio.addr;
101 + if (phydev->drv && phydev->drv->write_mmd)
102 + return phydev->drv->write_mmd(phydev, devad, regnum, val);
103
104 - mmd_phy_indirect(bus, phy_addr, devad, regnum);
105 -
106 - /* Write the data into MMD's selected register */
107 - __mdiobus_write(bus, phy_addr, MII_MMD_DATA, val);
108 -
109 - ret = 0;
110 - }
111 - return ret;
112 + return mmd_phy_write(phydev->mdio.bus, phydev->mdio.addr,
113 + phydev->is_c45, devad, regnum, val);
114 }
115 EXPORT_SYMBOL(__phy_write_mmd);
116