1 From eda80b249df7bbc7b3dd13907343a3e59bfc57fd Mon Sep 17 00:00:00 2001
2 From: Daniel Golle <daniel@makrotopia.org>
3 Date: Tue, 4 Jan 2022 12:06:22 +0000
4 Subject: [PATCH 1/3] net: ethernet: mtk_eth_soc: fix return values and
7 Instead of returning -1 (-EPERM) when MDIO bus is stuck busy
8 while writing or 0xffff if it happens while reading, return the
9 appropriate -ETIMEDOUT. Also fix return type to int instead of u32.
10 Refactor functions to use bitfield helpers instead of having various
11 masking and shifting constants in the code, which also results in the
12 register definitions in the header file being more obviously related
13 to what is stated in the MediaTek's Reference Manual.
15 Fixes: 656e705243fd0 ("net-next: mediatek: add support for MT7623 ethernet")
16 Signed-off-by: Daniel Golle <daniel@makrotopia.org>
17 Signed-off-by: David S. Miller <davem@davemloft.net>
19 drivers/net/ethernet/mediatek/mtk_eth_soc.c | 53 ++++++++++++---------
20 drivers/net/ethernet/mediatek/mtk_eth_soc.h | 16 +++++--
21 2 files changed, 41 insertions(+), 28 deletions(-)
23 --- a/drivers/net/ethernet/mediatek/mtk_eth_soc.c
24 +++ b/drivers/net/ethernet/mediatek/mtk_eth_soc.c
25 @@ -92,46 +92,53 @@ static int mtk_mdio_busy_wait(struct mtk
28 dev_err(eth->dev, "mdio: MDIO timeout\n");
33 -static u32 _mtk_mdio_write(struct mtk_eth *eth, u32 phy_addr,
34 - u32 phy_register, u32 write_data)
35 +static int _mtk_mdio_write(struct mtk_eth *eth, u32 phy_addr, u32 phy_reg,
38 - if (mtk_mdio_busy_wait(eth))
42 - write_data &= 0xffff;
44 - mtk_w32(eth, PHY_IAC_ACCESS | PHY_IAC_START | PHY_IAC_WRITE |
45 - (phy_register << PHY_IAC_REG_SHIFT) |
46 - (phy_addr << PHY_IAC_ADDR_SHIFT) | write_data,
47 + ret = mtk_mdio_busy_wait(eth);
51 + mtk_w32(eth, PHY_IAC_ACCESS |
54 + PHY_IAC_REG(phy_reg) |
55 + PHY_IAC_ADDR(phy_addr) |
56 + PHY_IAC_DATA(write_data),
59 - if (mtk_mdio_busy_wait(eth))
61 + ret = mtk_mdio_busy_wait(eth);
68 -static u32 _mtk_mdio_read(struct mtk_eth *eth, int phy_addr, int phy_reg)
69 +static int _mtk_mdio_read(struct mtk_eth *eth, u32 phy_addr, u32 phy_reg)
73 - if (mtk_mdio_busy_wait(eth))
77 - mtk_w32(eth, PHY_IAC_ACCESS | PHY_IAC_START | PHY_IAC_READ |
78 - (phy_reg << PHY_IAC_REG_SHIFT) |
79 - (phy_addr << PHY_IAC_ADDR_SHIFT),
80 + ret = mtk_mdio_busy_wait(eth);
84 + mtk_w32(eth, PHY_IAC_ACCESS |
86 + PHY_IAC_CMD_C22_READ |
87 + PHY_IAC_REG(phy_reg) |
88 + PHY_IAC_ADDR(phy_addr),
91 - if (mtk_mdio_busy_wait(eth))
94 - d = mtk_r32(eth, MTK_PHY_IAC) & 0xffff;
95 + ret = mtk_mdio_busy_wait(eth);
100 + return mtk_r32(eth, MTK_PHY_IAC) & PHY_IAC_DATA_MASK;
103 static int mtk_mdio_write(struct mii_bus *bus, int phy_addr,
104 --- a/drivers/net/ethernet/mediatek/mtk_eth_soc.h
105 +++ b/drivers/net/ethernet/mediatek/mtk_eth_soc.h
106 @@ -341,11 +341,17 @@
107 /* PHY Indirect Access Control registers */
108 #define MTK_PHY_IAC 0x10004
109 #define PHY_IAC_ACCESS BIT(31)
110 -#define PHY_IAC_READ BIT(19)
111 -#define PHY_IAC_WRITE BIT(18)
112 -#define PHY_IAC_START BIT(16)
113 -#define PHY_IAC_ADDR_SHIFT 20
114 -#define PHY_IAC_REG_SHIFT 25
115 +#define PHY_IAC_REG_MASK GENMASK(29, 25)
116 +#define PHY_IAC_REG(x) FIELD_PREP(PHY_IAC_REG_MASK, (x))
117 +#define PHY_IAC_ADDR_MASK GENMASK(24, 20)
118 +#define PHY_IAC_ADDR(x) FIELD_PREP(PHY_IAC_ADDR_MASK, (x))
119 +#define PHY_IAC_CMD_MASK GENMASK(19, 18)
120 +#define PHY_IAC_CMD_WRITE FIELD_PREP(PHY_IAC_CMD_MASK, 1)
121 +#define PHY_IAC_CMD_C22_READ FIELD_PREP(PHY_IAC_CMD_MASK, 2)
122 +#define PHY_IAC_START_MASK GENMASK(17, 16)
123 +#define PHY_IAC_START_C22 FIELD_PREP(PHY_IAC_START_MASK, 1)
124 +#define PHY_IAC_DATA_MASK GENMASK(15, 0)
125 +#define PHY_IAC_DATA(x) FIELD_PREP(PHY_IAC_DATA_MASK, (x))
126 #define PHY_IAC_TIMEOUT HZ
128 #define MTK_MAC_MISC 0x1000c