[PATCH 1/2] drivers: net: mdio-sun4i: Speedup mdio read and write

From: qianfanguijin
Date: Tue Sep 06 2022 - 03:57:00 EST


From: qianfan Zhao <qianfanguijin@xxxxxxx>

msleep(1) on my board takes about 30ms, and it is too long to accept.
Use read_poll_timeout to speedup.

Signed-off-by: qianfan Zhao <qianfanguijin@xxxxxxx>
---
drivers/net/mdio/mdio-sun4i.c | 29 ++++++++++++-----------------
1 file changed, 12 insertions(+), 17 deletions(-)

diff --git a/drivers/net/mdio/mdio-sun4i.c b/drivers/net/mdio/mdio-sun4i.c
index f798de3276dc..168e2a375535 100644
--- a/drivers/net/mdio/mdio-sun4i.c
+++ b/drivers/net/mdio/mdio-sun4i.c
@@ -26,8 +26,6 @@
#define EMAC_MAC_MIND_REG (0x10)
#define EMAC_MAC_SSRR_REG (0x14)

-#define MDIO_TIMEOUT (msecs_to_jiffies(100))
-
struct sun4i_mdio_data {
void __iomem *membase;
struct regulator *regulator;
@@ -36,8 +34,7 @@ struct sun4i_mdio_data {
static int sun4i_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
{
struct sun4i_mdio_data *data = bus->priv;
- unsigned long timeout_jiffies;
- int value;
+ int ret, tmp, value;

/* issue the phy address and reg */
writel((mii_id << 8) | regnum, data->membase + EMAC_MAC_MADR_REG);
@@ -45,12 +42,11 @@ static int sun4i_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
writel(0x1, data->membase + EMAC_MAC_MCMD_REG);

/* Wait read complete */
- timeout_jiffies = jiffies + MDIO_TIMEOUT;
- while (readl(data->membase + EMAC_MAC_MIND_REG) & 0x1) {
- if (time_is_before_jiffies(timeout_jiffies))
- return -ETIMEDOUT;
- msleep(1);
- }
+ ret = read_poll_timeout(readl, tmp, (tmp & 1) == 0,
+ 20, 10000, false,
+ data->membase + EMAC_MAC_MIND_REG);
+ if (ret < 0)
+ return ret;

/* push down the phy io line */
writel(0x0, data->membase + EMAC_MAC_MCMD_REG);
@@ -64,7 +60,7 @@ static int sun4i_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
u16 value)
{
struct sun4i_mdio_data *data = bus->priv;
- unsigned long timeout_jiffies;
+ int ret, tmp;

/* issue the phy address and reg */
writel((mii_id << 8) | regnum, data->membase + EMAC_MAC_MADR_REG);
@@ -72,12 +68,11 @@ static int sun4i_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
writel(0x1, data->membase + EMAC_MAC_MCMD_REG);

/* Wait read complete */
- timeout_jiffies = jiffies + MDIO_TIMEOUT;
- while (readl(data->membase + EMAC_MAC_MIND_REG) & 0x1) {
- if (time_is_before_jiffies(timeout_jiffies))
- return -ETIMEDOUT;
- msleep(1);
- }
+ ret = read_poll_timeout(readl, tmp, (tmp & 1) == 0,
+ 20, 10000, false,
+ data->membase + EMAC_MAC_MIND_REG);
+ if (ret < 0)
+ return ret;

/* push down the phy io line */
writel(0x0, data->membase + EMAC_MAC_MCMD_REG);
--
2.25.1