Skip to contentSkip to conversation

net: dsa: microchip: Further regmap cleanups

PATCH In review Marek Vasut 5 participants 16 messages Jun 27, 2019 – Jun 28, 2019

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

cover net: dsa: microchip: Further regmap cleanups

This patchset cleans up KSZ9477 switch driver by replacing various ad-hoc polling implementations and register RMW with regmap functions.

Each polling function is replaced separately to make it easier to review and possibly bisect, but maybe the patches can be squashed.

Cc Andrew Lunn <[email protected]>
Cc Florian Fainelli <[email protected]>
Cc Tristram Ha <[email protected]>
Cc Woojung Huh <[email protected]>

Marek Vasut (5):
net: dsa: microchip: Replace ad-hoc polling with regmap
net: dsa: microchip: Replace ksz9477_wait_vlan_ctrl_ready polling with
regmap
net: dsa: microchip: Replace ksz9477_wait_alu_ready polling with
regmap
net: dsa: microchip: Replace ksz9477_wait_alu_sta_ready polling with
regmap
net: dsa: microchip: Replace bit RMW with regmap

 drivers/net/dsa/microchip/ksz9477.c    | 128 +++++++++----------------
 drivers/net/dsa/microchip/ksz_common.h |  14 ---
 2 files changed, 47 insertions(+), 95 deletions(-)
Signature · 1 line
2.20.1
#1 lore raw

PATCH 1/5 net: dsa: microchip: Replace ad-hoc polling with regmap

Regmap provides polling function to poll for bits in a register, use in instead of reimplementing it.

Cc Andrew Lunn <[email protected]>
Cc Florian Fainelli <[email protected]>
Cc Tristram Ha <[email protected]>
Cc Woojung Huh <[email protected]>
---
 drivers/net/dsa/microchip/ksz9477.c    | 14 +++++---------
 drivers/net/dsa/microchip/ksz_common.h | 14 --------------
 2 files changed, 5 insertions(+), 23 deletions(-)
drivers/net/dsa/microchip/ksz9477.c +5 -9
index 8f13dcc05a10..ece25f38e02a 100644
@@ -263,12 +263,8 @@ static int ksz9477_reset_switch(struct ksz_device *dev)
 static void ksz9477_r_mib_cnt(struct ksz_device *dev, int port, u16 addr,
 			      u64 *cnt)
 {
-	struct ksz_poll_ctx ctx = {
-		.dev = dev,
-		.port = port,
-		.offset = REG_PORT_MIB_CTRL_STAT__4,
-	};
 	struct ksz_port *p = &dev->ports[port];
+	unsigned int val;
 	u32 data;
 	int ret;
 
@@ -278,11 +274,11 @@ static void ksz9477_r_mib_cnt(struct ksz_device *dev, int port, u16 addr,
 	data |= (addr << MIB_COUNTER_INDEX_S);
 	ksz_pwrite32(dev, port, REG_PORT_MIB_CTRL_STAT__4, data);
 
-	ret = readx_poll_timeout(ksz_pread32_poll, &ctx, data,
-				 !(data & MIB_COUNTER_READ), 10, 1000);
-
+	ret = regmap_read_poll_timeout(dev->regmap[2],
+			PORT_CTRL_ADDR(port, REG_PORT_MIB_CTRL_STAT__4),
+			val, !(val & MIB_COUNTER_READ), 10, 1000);
 	/* failed to read MIB. get out of loop */
-	if (ret < 0) {
+	if (ret) {
 		dev_dbg(dev->dev, "Failed to get MIB\n");
 		return;
 	}
drivers/net/dsa/microchip/ksz_common.h +0 -14
index 745318424f71..ee7096d8af07 100644
@@ -119,20 +119,6 @@ static inline void ksz_pwrite32(struct ksz_device *dev, int port, int offset,
 	ksz_write32(dev, dev->dev_ops->get_port_addr(port, offset), data);
 }
 
-struct ksz_poll_ctx {
-	struct ksz_device *dev;
-	int port;
-	int offset;
-};
-
-static inline u32 ksz_pread32_poll(struct ksz_poll_ctx *ctx)
-{
-	u32 data;
-
-	ksz_pread32(ctx->dev, ctx->port, ctx->offset, &data);
-	return data;
-}
-
 /* Regmap tables generation */
 #define KSZ_SPI_OP_RD		3
 #define KSZ_SPI_OP_WR		2
Signature · 1 line
2.20.1
Reviewed-by on v1 1/5
#3 lore raw

Re: [PATCH 1/5] net: dsa: microchip: Replace ad-hoc polling with regmap

In reply to Marek Vasut
Quoted 8 lines · Marek Vasut

Regmap provides polling function to poll for bits in a register, use in instead of reimplementing it.

Cc Andrew Lunn <[email protected]>
Cc Florian Fainelli <[email protected]>
Cc Tristram Ha <[email protected]>
Cc Woojung Huh <[email protected]>
Signature · 1 line
Florian
#4 lore raw

PATCH 2/5 net: dsa: microchip: Replace ksz9477_wait_vlan_ctrl_ready polling with regmap

In reply to Marek Vasut

Regmap provides polling function to poll for bits in a register. This function is another reimplementation of polling for bit being clear in a register. Replace this with regmap polling function. Moreover, inline the function parameters, as the function is never called with any other parameter values than this one.

Cc Andrew Lunn <[email protected]>
Cc Florian Fainelli <[email protected]>
Cc Tristram Ha <[email protected]>
Cc Woojung Huh <[email protected]>
---
 drivers/net/dsa/microchip/ksz9477.c | 26 ++++++++------------------
 1 file changed, 8 insertions(+), 18 deletions(-)
drivers/net/dsa/microchip/ksz9477.c +8 -18
index ece25f38e02a..0aab00bf23b9 100644
@@ -89,22 +89,12 @@ static void ksz9477_port_cfg32(struct ksz_device *dev, int port, int offset,
 			   bits, set ? bits : 0);
 }
 
-static int ksz9477_wait_vlan_ctrl_ready(struct ksz_device *dev, u32 waiton,
-					int timeout)
+static int ksz9477_wait_vlan_ctrl_ready(struct ksz_device *dev)
 {
-	u8 data;
-
-	do {
-		ksz_read8(dev, REG_SW_VLAN_CTRL, &data);
-		if (!(data & waiton))
-			break;
-		usleep_range(1, 10);
-	} while (timeout-- > 0);
-
-	if (timeout <= 0)
-		return -ETIMEDOUT;
+	unsigned int val;
 
-	return 0;
+	return regmap_read_poll_timeout(dev->regmap[0], REG_SW_VLAN_CTRL,
+					val, !(val & VLAN_START), 10, 1000);
 }
 
 static int ksz9477_get_vlan_table(struct ksz_device *dev, u16 vid,
@@ -118,8 +108,8 @@ static int ksz9477_get_vlan_table(struct ksz_device *dev, u16 vid,
 	ksz_write8(dev, REG_SW_VLAN_CTRL, VLAN_READ | VLAN_START);
 
 	/* wait to be cleared */
-	ret = ksz9477_wait_vlan_ctrl_ready(dev, VLAN_START, 1000);
-	if (ret < 0) {
+	ret = ksz9477_wait_vlan_ctrl_ready(dev);
+	if (ret) {
 		dev_dbg(dev->dev, "Failed to read vlan table\n");
 		goto exit;
 	}
@@ -151,8 +141,8 @@ static int ksz9477_set_vlan_table(struct ksz_device *dev, u16 vid,
 	ksz_write8(dev, REG_SW_VLAN_CTRL, VLAN_START | VLAN_WRITE);
 
 	/* wait to be cleared */
-	ret = ksz9477_wait_vlan_ctrl_ready(dev, VLAN_START, 1000);
-	if (ret < 0) {
+	ret = ksz9477_wait_vlan_ctrl_ready(dev);
+	if (ret) {
 		dev_dbg(dev->dev, "Failed to write vlan table\n");
 		goto exit;
 	}
Signature · 1 line
2.20.1
maintainer Reviewed-by on v1 2/5
#5 lore raw

Re: [PATCH 2/5] net: dsa: microchip: Replace ksz9477_wait_vlan_ctrl_ready polling with regmap

Quoted 7 lines · Marek Vasut

Regmap provides polling function to poll for bits in a register. This function is another reimplementation of polling for bit being clear in a register. Replace this with regmap polling function. Moreover, inline the function parameters, as the function is never called with any other parameter values than this one.

Andrew

Reviewed-by on v1 2/5
#6 lore raw

Re: [PATCH 2/5] net: dsa: microchip: Replace ksz9477_wait_vlan_ctrl_ready polling with regmap

In reply to Marek Vasut
Quoted 11 lines · Marek Vasut

Regmap provides polling function to poll for bits in a register. This function is another reimplementation of polling for bit being clear in a register. Replace this with regmap polling function. Moreover, inline the function parameters, as the function is never called with any other parameter values than this one.

Cc Andrew Lunn <[email protected]>
Cc Florian Fainelli <[email protected]>
Cc Tristram Ha <[email protected]>
Cc Woojung Huh <[email protected]>
Signature · 1 line
Florian
#7 lore raw

PATCH 3/5 net: dsa: microchip: Replace ksz9477_wait_alu_ready polling with regmap

In reply to Marek Vasut

Regmap provides polling function to poll for bits in a register. This function is another reimplementation of polling for bit being clear in a register. Replace this with regmap polling function. Moreover, inline the function parameters, as the function is never called with any other parameter values than this one.

Cc Andrew Lunn <[email protected]>
Cc Florian Fainelli <[email protected]>
Cc Tristram Ha <[email protected]>
Cc Woojung Huh <[email protected]>
---
 drivers/net/dsa/microchip/ksz9477.c | 34 ++++++++++-------------------
 1 file changed, 12 insertions(+), 22 deletions(-)
drivers/net/dsa/microchip/ksz9477.c +12 -22
index 0aab00bf23b9..e5b2f3e45db6 100644
@@ -176,22 +176,12 @@ static void ksz9477_write_table(struct ksz_device *dev, u32 *table)
 	ksz_write32(dev, REG_SW_ALU_VAL_D, table[3]);
 }
 
-static int ksz9477_wait_alu_ready(struct ksz_device *dev, u32 waiton,
-				  int timeout)
+static int ksz9477_wait_alu_ready(struct ksz_device *dev)
 {
-	u32 data;
-
-	do {
-		ksz_read32(dev, REG_SW_ALU_CTRL__4, &data);
-		if (!(data & waiton))
-			break;
-		usleep_range(1, 10);
-	} while (timeout-- > 0);
-
-	if (timeout <= 0)
-		return -ETIMEDOUT;
+	unsigned int val;
 
-	return 0;
+	return regmap_read_poll_timeout(dev->regmap[2], REG_SW_ALU_CTRL__4,
+					val, !(val & ALU_START), 10, 1000);
 }
 
 static int ksz9477_wait_alu_sta_ready(struct ksz_device *dev, u32 waiton,
@@ -633,8 +623,8 @@ static int ksz9477_port_fdb_add(struct dsa_switch *ds, int port,
 	ksz_write32(dev, REG_SW_ALU_CTRL__4, ALU_READ | ALU_START);
 
 	/* wait to be finished */
-	ret = ksz9477_wait_alu_ready(dev, ALU_START, 1000);
-	if (ret < 0) {
+	ret = ksz9477_wait_alu_ready(dev);
+	if (ret) {
 		dev_dbg(dev->dev, "Failed to read ALU\n");
 		goto exit;
 	}
@@ -657,8 +647,8 @@ static int ksz9477_port_fdb_add(struct dsa_switch *ds, int port,
 	ksz_write32(dev, REG_SW_ALU_CTRL__4, ALU_WRITE | ALU_START);
 
 	/* wait to be finished */
-	ret = ksz9477_wait_alu_ready(dev, ALU_START, 1000);
-	if (ret < 0)
+	ret = ksz9477_wait_alu_ready(dev);
+	if (ret)
 		dev_dbg(dev->dev, "Failed to write ALU\n");
 
 exit:
@@ -690,8 +680,8 @@ static int ksz9477_port_fdb_del(struct dsa_switch *ds, int port,
 	ksz_write32(dev, REG_SW_ALU_CTRL__4, ALU_READ | ALU_START);
 
 	/* wait to be finished */
-	ret = ksz9477_wait_alu_ready(dev, ALU_START, 1000);
-	if (ret < 0) {
+	ret = ksz9477_wait_alu_ready(dev);
+	if (ret) {
 		dev_dbg(dev->dev, "Failed to read ALU\n");
 		goto exit;
 	}
@@ -724,8 +714,8 @@ static int ksz9477_port_fdb_del(struct dsa_switch *ds, int port,
 	ksz_write32(dev, REG_SW_ALU_CTRL__4, ALU_WRITE | ALU_START);
 
 	/* wait to be finished */
-	ret = ksz9477_wait_alu_ready(dev, ALU_START, 1000);
-	if (ret < 0)
+	ret = ksz9477_wait_alu_ready(dev);
+	if (ret)
 		dev_dbg(dev->dev, "Failed to write ALU\n");
 
 exit:
Signature · 1 line
2.20.1
maintainer Reviewed-by on v1 3/5
#8 lore raw

Re: [PATCH 3/5] net: dsa: microchip: Replace ksz9477_wait_alu_ready polling with regmap

Quoted 7 lines · Marek Vasut

Regmap provides polling function to poll for bits in a register. This function is another reimplementation of polling for bit being clear in a register. Replace this with regmap polling function. Moreover, inline the function parameters, as the function is never called with any other parameter values than this one.

Andrew

#9 lore raw

PATCH 4/5 net: dsa: microchip: Replace ksz9477_wait_alu_sta_ready polling with regmap

In reply to Marek Vasut

Regmap provides polling function to poll for bits in a register. This function is another reimplementation of polling for bit being clear in a register. Replace this with regmap polling function. Moreover, inline the function parameters, as the function is never called with any other parameter values than this one.

Cc Andrew Lunn <[email protected]>
Cc Florian Fainelli <[email protected]>
Cc Tristram Ha <[email protected]>
Cc Woojung Huh <[email protected]>
---
 drivers/net/dsa/microchip/ksz9477.c | 32 +++++++++++------------------
 1 file changed, 12 insertions(+), 20 deletions(-)
drivers/net/dsa/microchip/ksz9477.c +12 -20
index e5b2f3e45db6..bfc44799854c 100644
@@ -184,22 +184,14 @@ static int ksz9477_wait_alu_ready(struct ksz_device *dev)
 					val, !(val & ALU_START), 10, 1000);
 }
 
-static int ksz9477_wait_alu_sta_ready(struct ksz_device *dev, u32 waiton,
-				      int timeout)
+static int ksz9477_wait_alu_sta_ready(struct ksz_device *dev)
 {
-	u32 data;
-
-	do {
-		ksz_read32(dev, REG_SW_ALU_STAT_CTRL__4, &data);
-		if (!(data & waiton))
-			break;
-		usleep_range(1, 10);
-	} while (timeout-- > 0);
-
-	if (timeout <= 0)
-		return -ETIMEDOUT;
+	unsigned int val;
 
-	return 0;
+	return regmap_read_poll_timeout(dev->regmap[2],
+					REG_SW_ALU_STAT_CTRL__4,
+					val, !(val & ALU_STAT_START),
+					10, 1000);
 }
 
 static int ksz9477_reset_switch(struct ksz_device *dev)
@@ -821,7 +813,7 @@ static void ksz9477_port_mdb_add(struct dsa_switch *ds, int port,
 		ksz_write32(dev, REG_SW_ALU_STAT_CTRL__4, data);
 
 		/* wait to be finished */
-		if (ksz9477_wait_alu_sta_ready(dev, ALU_STAT_START, 1000) < 0) {
+		if (ksz9477_wait_alu_sta_ready(dev)) {
 			dev_dbg(dev->dev, "Failed to read ALU STATIC\n");
 			goto exit;
 		}
@@ -862,7 +854,7 @@ static void ksz9477_port_mdb_add(struct dsa_switch *ds, int port,
 	ksz_write32(dev, REG_SW_ALU_STAT_CTRL__4, data);
 
 	/* wait to be finished */
-	if (ksz9477_wait_alu_sta_ready(dev, ALU_STAT_START, 1000) < 0)
+	if (ksz9477_wait_alu_sta_ready(dev))
 		dev_dbg(dev->dev, "Failed to read ALU STATIC\n");
 
 exit:
@@ -892,8 +884,8 @@ static int ksz9477_port_mdb_del(struct dsa_switch *ds, int port,
 		ksz_write32(dev, REG_SW_ALU_STAT_CTRL__4, data);
 
 		/* wait to be finished */
-		ret = ksz9477_wait_alu_sta_ready(dev, ALU_STAT_START, 1000);
-		if (ret < 0) {
+		ret = ksz9477_wait_alu_sta_ready(dev);
+		if (ret) {
 			dev_dbg(dev->dev, "Failed to read ALU STATIC\n");
 			goto exit;
 		}
@@ -934,8 +926,8 @@ static int ksz9477_port_mdb_del(struct dsa_switch *ds, int port,
 	ksz_write32(dev, REG_SW_ALU_STAT_CTRL__4, data);
 
 	/* wait to be finished */
-	ret = ksz9477_wait_alu_sta_ready(dev, ALU_STAT_START, 1000);
-	if (ret < 0)
+	ret = ksz9477_wait_alu_sta_ready(dev);
+	if (ret)
 		dev_dbg(dev->dev, "Failed to read ALU STATIC\n");
 
 exit:
Signature · 1 line
2.20.1
maintainer Reviewed-by on v1 4/5
#10 lore raw

Re: [PATCH 4/5] net: dsa: microchip: Replace ksz9477_wait_alu_sta_ready polling with regmap

Quoted 7 lines · Marek Vasut

Regmap provides polling function to poll for bits in a register. This function is another reimplementation of polling for bit being clear in a register. Replace this with regmap polling function. Moreover, inline the function parameters, as the function is never called with any other parameter values than this one.

Andrew

#11 lore raw

PATCH 5/5 net: dsa: microchip: Replace bit RMW with regmap

In reply to Marek Vasut

Regmap provides read-modify-write function to update bitfields in registers. Replace ad-hoc read-modify-write with regmap_update_bits() where applicable.

Cc Andrew Lunn <[email protected]>
Cc Florian Fainelli <[email protected]>
Cc Tristram Ha <[email protected]>
Cc Woojung Huh <[email protected]>
---
 drivers/net/dsa/microchip/ksz9477.c | 22 ++++++++++------------
 1 file changed, 10 insertions(+), 12 deletions(-)
drivers/net/dsa/microchip/ksz9477.c +10 -12
index bfc44799854c..a8c97f7a79b7 100644
@@ -197,16 +197,14 @@ static int ksz9477_wait_alu_sta_ready(struct ksz_device *dev)
 static int ksz9477_reset_switch(struct ksz_device *dev)
 {
 	u8 data8;
-	u16 data16;
 	u32 data32;
 
 	/* reset switch */
 	ksz_cfg(dev, REG_SW_OPERATION, SW_RESET, true);
 
 	/* turn off SPI DO Edge select */
-	ksz_read8(dev, REG_SW_GLOBAL_SERIAL_CTRL_0, &data8);
-	data8 &= ~SPI_AUTO_EDGE_DETECTION;
-	ksz_write8(dev, REG_SW_GLOBAL_SERIAL_CTRL_0, data8);
+	regmap_update_bits(dev->regmap[0], REG_SW_GLOBAL_SERIAL_CTRL_0,
+			   SPI_AUTO_EDGE_DETECTION, 0);
 
 	/* default configuration */
 	ksz_read8(dev, REG_SW_LUE_CTRL_1, &data8);
@@ -220,10 +218,10 @@ static int ksz9477_reset_switch(struct ksz_device *dev)
 	ksz_read32(dev, REG_SW_PORT_INT_STATUS__4, &data32);
 
 	/* set broadcast storm protection 10% rate */
-	ksz_read16(dev, REG_SW_MAC_CTRL_2, &data16);
-	data16 &= ~BROADCAST_STORM_RATE;
-	data16 |= (BROADCAST_STORM_VALUE * BROADCAST_STORM_PROT_RATE) / 100;
-	ksz_write16(dev, REG_SW_MAC_CTRL_2, data16);
+	regmap_update_bits(dev->regmap[1], REG_SW_MAC_CTRL_2,
+			   BROADCAST_STORM_RATE,
+			   (BROADCAST_STORM_VALUE *
+			   BROADCAST_STORM_PROT_RATE) / 100);
 
 	if (dev->synclko_125)
 		ksz_write8(dev, REG_SW_GLOBAL_OUTPUT_CTRL__1,
@@ -485,10 +483,10 @@ static void ksz9477_flush_dyn_mac_table(struct ksz_device *dev, int port)
 {
 	u8 data;
 
-	ksz_read8(dev, REG_SW_LUE_CTRL_2, &data);
-	data &= ~(SW_FLUSH_OPTION_M << SW_FLUSH_OPTION_S);
-	data |= (SW_FLUSH_OPTION_DYN_MAC << SW_FLUSH_OPTION_S);
-	ksz_write8(dev, REG_SW_LUE_CTRL_2, data);
+	regmap_update_bits(dev->regmap[0], REG_SW_LUE_CTRL_2,
+			   SW_FLUSH_OPTION_M << SW_FLUSH_OPTION_S,
+			   SW_FLUSH_OPTION_DYN_MAC << SW_FLUSH_OPTION_S);
+
 	if (port < dev->mib_port_cnt) {
 		/* flush individual port */
 		ksz_pread8(dev, port, P_STP_CTRL, &data);
Signature · 1 line
2.20.1
maintainer Reviewed-by on v1 5/5
#12 lore raw

Re: [PATCH 5/5] net: dsa: microchip: Replace bit RMW with regmap

Quoted 5 lines · Marek Vasut

Regmap provides read-modify-write function to update bitfields in registers. Replace ad-hoc read-modify-write with regmap_update_bits() where applicable.

Andrew

maintainer
#13 lore raw
In reply to Marek Vasut

From: Marek Vasut <[email protected]>
Date: Thu, 27 Jun 2019 23:55:51 +0200

Quoted 7 lines

This patchset cleans up KSZ9477 switch driver by replacing various ad-hoc polling implementations and register RMW with regmap functions.

Each polling function is replaced separately to make it easier to review and possibly bisect, but maybe the patches can be squashed.

Series applied, thanks.

#14 lore raw
In reply to Marek Vasut

Hi Marek,

Quoted 11 lines · Marek Vasut

This patchset cleans up KSZ9477 switch driver by replacing various ad-hoc polling implementations and register RMW with regmap functions.

Each polling function is replaced separately to make it easier to review and possibly bisect, but maybe the patches can be squashed.

Cc Andrew Lunn <[email protected]>
Cc Florian Fainelli <[email protected]>
Cc Tristram Ha <[email protected]>
Cc Woojung Huh <[email protected]>

Please copy me next time, as per the MAINTAINERS file.

Thanks,
Vivien

#15 lore raw
Quoted 9 lines · Vivien Didelot

Hi Marek,

Quoted 6 lines · Marek Vasut

This patchset cleans up KSZ9477 switch driver by replacing various ad-hoc polling implementations and register RMW with regmap functions.

Each polling function is replaced separately to make it easier to review and possibly bisect, but maybe the patches can be squashed.

Quoted 7 lines
Quoted 5 lines
Cc Andrew Lunn <[email protected]>
Cc Florian Fainelli <[email protected]>
Cc Tristram Ha <[email protected]>
Cc Woojung Huh <[email protected]>

Please copy me next time, as per the MAINTAINERS file.

Will do. I plan to submit KSZ8795 support next.

Signature · 2 lines
Best regards,
Marek Vasut