Skip to contentSkip to conversation

Endian fixes for SYSTEMPORT/SF2/MDIO

PATCH In review Florian Fainelli 2 participants 11 messages Aug 29, 2017

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

cover Endian fixes for SYSTEMPORT/SF2/MDIO

Hi David,

While trying an ARM BE kernel for kinks, the 3 drivers below started not working and the reasons why became pretty obvious because the register space remains LE (hardwired), except for Broadcom MIPS where it follows the CPU's native endian (let's call that a feature).

Thanks!

Florian Fainelli (4):
net: systemport: Use correct I/O accessors
net: dsa: bcm_sf2: Use correct I/O accessors
net: systemport: Set correct RSB endian bits based on host
net: phy: mdio-bcm-unimac: Use correct I/O accessors

 drivers/net/dsa/bcm_sf2.h                  | 12 +++++------
 drivers/net/ethernet/broadcom/bcmsysport.c | 21 ++++++++++++--------
 drivers/net/phy/mdio-bcm-unimac.c          | 32 ++++++++++++++++++++++++------
 3 files changed, 45 insertions(+), 20 deletions(-)
Signature · 1 line
1.9.1
#1 lore raw

PATCH 1/4 net: systemport: Use correct I/O accessors

The SYSTEMPORT driver currently uses __raw_{read,write}l which means native I/O endian. This works correctly for an ARM LE kernel (default) but fails miserably on an ARM BE (BE8) kernel where registers are kept little endian, so replace uses with {read,write}l_relaxed here which is what we want because this is all performance sensitive code.

---
 drivers/net/ethernet/broadcom/bcmsysport.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
drivers/net/ethernet/broadcom/bcmsysport.c +6 -6
index b3a21418f511..a7e84292af50 100644
@@ -32,13 +32,13 @@
 #define BCM_SYSPORT_IO_MACRO(name, offset) \
 static inline u32 name##_readl(struct bcm_sysport_priv *priv, u32 off)	\
 {									\
-	u32 reg = __raw_readl(priv->base + offset + off);		\
+	u32 reg = readl_relaxed(priv->base + offset + off);		\
 	return reg;							\
 }									\
 static inline void name##_writel(struct bcm_sysport_priv *priv,		\
 				  u32 val, u32 off)			\
 {									\
-	__raw_writel(val, priv->base + offset + off);			\
+	writel_relaxed(val, priv->base + offset + off);			\
 }									\
 
 BCM_SYSPORT_IO_MACRO(intrl2_0, SYS_PORT_INTRL2_0_OFFSET);
@@ -59,14 +59,14 @@ static inline u32 rdma_readl(struct bcm_sysport_priv *priv, u32 off)
 {
 	if (priv->is_lite && off >= RDMA_STATUS)
 		off += 4;
-	return __raw_readl(priv->base + SYS_PORT_RDMA_OFFSET + off);
+	return readl_relaxed(priv->base + SYS_PORT_RDMA_OFFSET + off);
 }
 
 static inline void rdma_writel(struct bcm_sysport_priv *priv, u32 val, u32 off)
 {
 	if (priv->is_lite && off >= RDMA_STATUS)
 		off += 4;
-	__raw_writel(val, priv->base + SYS_PORT_RDMA_OFFSET + off);
+	writel_relaxed(val, priv->base + SYS_PORT_RDMA_OFFSET + off);
 }
 
 static inline u32 tdma_control_bit(struct bcm_sysport_priv *priv, u32 bit)
@@ -110,10 +110,10 @@ static inline void dma_desc_set_addr(struct bcm_sysport_priv *priv,
 				     dma_addr_t addr)
 {
 #ifdef CONFIG_PHYS_ADDR_T_64BIT
-	__raw_writel(upper_32_bits(addr) & DESC_ADDR_HI_MASK,
+	writel_relaxed(upper_32_bits(addr) & DESC_ADDR_HI_MASK,
 		     d + DESC_ADDR_HI_STATUS_LEN);
 #endif
-	__raw_writel(lower_32_bits(addr), d + DESC_ADDR_LO);
+	writel_relaxed(lower_32_bits(addr), d + DESC_ADDR_LO);
 }
 
 static inline void tdma_port_write_desc_addr(struct bcm_sysport_priv *priv,
Signature · 1 line
1.9.1
#2 lore raw

PATCH 2/4 net: dsa: bcm_sf2: Use correct I/O accessors

In reply to Florian Fainelli

The Starfigther 2 driver currently uses __raw_{read,write}l which means native I/O endian. This works correctly for an ARM LE kernel (default) but fails miserably on an ARM BE (BE8) kernel where registers are kept little endian, so replace uses with {read,write}l_relaxed here which is what we want because this is all performance sensitive code.

---
 drivers/net/dsa/bcm_sf2.h | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
drivers/net/dsa/bcm_sf2.h +6 -6
index 7d3030e04f11..d9c96b281fc0 100644
@@ -130,12 +130,12 @@ static inline u32 bcm_sf2_mangle_addr(struct bcm_sf2_priv *priv, u32 off)
 #define SF2_IO_MACRO(name) \
 static inline u32 name##_readl(struct bcm_sf2_priv *priv, u32 off)	\
 {									\
-	return __raw_readl(priv->name + off);				\
+	return readl_relaxed(priv->name + off);				\
 }									\
 static inline void name##_writel(struct bcm_sf2_priv *priv,		\
 				  u32 val, u32 off)			\
 {									\
-	__raw_writel(val, priv->name + off);				\
+	writel_relaxed(val, priv->name + off);				\
 }									\
 
 /* Accesses to 64-bits register requires us to latch the hi/lo pairs
@@ -179,23 +179,23 @@ static inline u32 bcm_sf2_mangle_addr(struct bcm_sf2_priv *priv, u32 off)
 static inline u32 core_readl(struct bcm_sf2_priv *priv, u32 off)
 {
 	u32 tmp = bcm_sf2_mangle_addr(priv, off);
-	return __raw_readl(priv->core + tmp);
+	return readl_relaxed(priv->core + tmp);
 }
 
 static inline void core_writel(struct bcm_sf2_priv *priv, u32 val, u32 off)
 {
 	u32 tmp = bcm_sf2_mangle_addr(priv, off);
-	__raw_writel(val, priv->core + tmp);
+	writel_relaxed(val, priv->core + tmp);
 }
 
 static inline u32 reg_readl(struct bcm_sf2_priv *priv, u16 off)
 {
-	return __raw_readl(priv->reg + priv->reg_offsets[off]);
+	return readl_relaxed(priv->reg + priv->reg_offsets[off]);
 }
 
 static inline void reg_writel(struct bcm_sf2_priv *priv, u32 val, u16 off)
 {
-	__raw_writel(val, priv->reg + priv->reg_offsets[off]);
+	writel_relaxed(val, priv->reg + priv->reg_offsets[off]);
 }
 
 SF2_IO64_MACRO(core);
Signature · 1 line
1.9.1
#3 lore raw

PATCH 3/4 net: systemport: Set correct RSB endian bits based on host

In reply to Florian Fainelli

LE CPU:
* set RSB_SWAP0 (both SYSTEMPORT and SYSTEMPORT Lite)
* clear RSB_SWAP1 (SYSTEMPORT Lite only)

BE CPU:
* clear RSB_SWAP0 (both SYSTEMPORT and SYSTEMPORTE lite)
* set RSB_SWAP1 (SYSTEMPORT Lite only)

With these settings, we have the Receive Status Block always match the host endian and we do not need to perform any conversion.

---
 drivers/net/ethernet/broadcom/bcmsysport.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
drivers/net/ethernet/broadcom/bcmsysport.c +7 -2
index a7e84292af50..7c7558a6b720 100644
@@ -1762,9 +1762,14 @@ static void rbuf_init(struct bcm_sysport_priv *priv)
 	reg = rbuf_readl(priv, RBUF_CONTROL);
 	reg |= RBUF_4B_ALGN | RBUF_RSB_EN;
 	/* Set a correct RSB format on SYSTEMPORT Lite */
-	if (priv->is_lite) {
-		reg &= ~RBUF_RSB_SWAP1;
+	if (!IS_ENABLED(CONFIG_CPU_BIG_ENDIAN)) {
+		if (priv->is_lite)
+			reg &= ~RBUF_RSB_SWAP1;
 		reg |= RBUF_RSB_SWAP0;
+	} else {
+		if (priv->is_lite)
+			reg |= RBUF_RSB_SWAP1;
+		reg &= ~RBUF_RSB_SWAP0;
 	}
 	rbuf_writel(priv, reg, RBUF_CONTROL);
 }
Signature · 1 line
1.9.1
#4 lore raw

PATCH 4/4 net: phy: mdio-bcm-unimac: Use correct I/O accessors

In reply to Florian Fainelli

The driver currently uses __raw_{read,write}l which works for all platforms supported: Broadcom MIPS LE/BE (native endian), ARM LE (native endian) but not ARM BE (registers are still LE). Switch to using the proper accessors for all platforms and explain why Broadcom MIPS BE is special here, in doing so, we introduce a couple of helper functions to abstract these differences.

---
 drivers/net/phy/mdio-bcm-unimac.c | 32 ++++++++++++++++++++++++++------
 1 file changed, 26 insertions(+), 6 deletions(-)
drivers/net/phy/mdio-bcm-unimac.c +26 -6
index 73c5267a11fd..08e0647b85e2 100644
@@ -47,18 +47,38 @@ struct unimac_mdio_priv {
 	void			*wait_func_data;
 };
 
+static inline u32 unimac_mdio_readl(struct unimac_mdio_priv *priv, u32 offset)
+{
+	/* MIPS chips strapped for BE will automagically configure the
+	 * peripheral registers for CPU-native byte order.
+	 */
+	if (IS_ENABLED(CONFIG_MIPS) && IS_ENABLED(CONFIG_CPU_BIG_ENDIAN))
+		return __raw_readl(priv->base + offset);
+	else
+		return readl_relaxed(priv->base + offset);
+}
+
+static inline void unimac_mdio_writel(struct unimac_mdio_priv *priv, u32 val,
+				      u32 offset)
+{
+	if (IS_ENABLED(CONFIG_MIPS) && IS_ENABLED(CONFIG_CPU_BIG_ENDIAN))
+		__raw_writel(val, priv->base + offset);
+	else
+		writel_relaxed(val, priv->base + offset);
+}
+
 static inline void unimac_mdio_start(struct unimac_mdio_priv *priv)
 {
 	u32 reg;
 
-	reg = __raw_readl(priv->base + MDIO_CMD);
+	reg = unimac_mdio_readl(priv, MDIO_CMD);
 	reg |= MDIO_START_BUSY;
-	__raw_writel(reg, priv->base + MDIO_CMD);
+	unimac_mdio_writel(priv, reg, MDIO_CMD);
 }
 
 static inline unsigned int unimac_mdio_busy(struct unimac_mdio_priv *priv)
 {
-	return __raw_readl(priv->base + MDIO_CMD) & MDIO_START_BUSY;
+	return unimac_mdio_readl(priv, MDIO_CMD) & MDIO_START_BUSY;
 }
 
 static int unimac_mdio_poll(void *wait_func_data)
@@ -87,7 +107,7 @@ static int unimac_mdio_read(struct mii_bus *bus, int phy_id, int reg)
 
 	/* Prepare the read operation */
 	cmd = MDIO_RD | (phy_id << MDIO_PMD_SHIFT) | (reg << MDIO_REG_SHIFT);
-	__raw_writel(cmd, priv->base + MDIO_CMD);
+	unimac_mdio_writel(priv, cmd, MDIO_CMD);
 
 	/* Start MDIO transaction */
 	unimac_mdio_start(priv);
@@ -96,7 +116,7 @@ static int unimac_mdio_read(struct mii_bus *bus, int phy_id, int reg)
 	if (ret)
 		return ret;
 
-	cmd = __raw_readl(priv->base + MDIO_CMD);
+	cmd = unimac_mdio_readl(priv, MDIO_CMD);
 
 	/* Some broken devices are known not to release the line during
 	 * turn-around, e.g: Broadcom BCM53125 external switches, so check for
@@ -118,7 +138,7 @@ static int unimac_mdio_write(struct mii_bus *bus, int phy_id,
 	/* Prepare the write operation */
 	cmd = MDIO_WR | (phy_id << MDIO_PMD_SHIFT) |
 		(reg << MDIO_REG_SHIFT) | (0xffff & val);
-	__raw_writel(cmd, priv->base + MDIO_CMD);
+	unimac_mdio_writel(priv, cmd, MDIO_CMD);
 
 	unimac_mdio_start(priv);
 
Signature · 1 line
1.9.1
#5 lore raw
In reply to Florian Fainelli
Quoted 6 lines · Florian Fainelli

Hi David,

While trying an ARM BE kernel for kinks, the 3 drivers below started not working and the reasons why became pretty obvious because the register space remains LE (hardwired), except for Broadcom MIPS where it follows the CPU's native endian (let's call that a feature).

David, I found a minor problem in the RSB endian configuration for SYSTEMPOR Lite that uses two bits for that, I will repost shortly.

Quoted 14 lines

Thanks!

Florian Fainelli (4):
net: systemport: Use correct I/O accessors
net: dsa: bcm_sf2: Use correct I/O accessors
net: systemport: Set correct RSB endian bits based on host
net: phy: mdio-bcm-unimac: Use correct I/O accessors

 drivers/net/dsa/bcm_sf2.h                  | 12 +++++------
 drivers/net/ethernet/broadcom/bcmsysport.c | 21 ++++++++++++--------
 drivers/net/phy/mdio-bcm-unimac.c          | 32 ++++++++++++++++++++++++------
 3 files changed, 45 insertions(+), 20 deletions(-)
Signature · 1 line
Florian
maintainer
#6 lore raw
In reply to Florian Fainelli

From: Florian Fainelli <[email protected]>
Date: Tue, 29 Aug 2017 11:39:41 -0700

Quoted 4 lines

While trying an ARM BE kernel for kinks, the 3 drivers below started not working and the reasons why became pretty obvious because the register space remains LE (hardwired), except for Broadcom MIPS where it follows the CPU's native endian (let's call that a feature).

Series applied, thanks Florian.

#7 lore raw
Quoted 9 lines · David Miller

From: Florian Fainelli <[email protected]>
Date: Tue, 29 Aug 2017 11:39:41 -0700

Quoted 4 lines

While trying an ARM BE kernel for kinks, the 3 drivers below started not working and the reasons why became pretty obvious because the register space remains LE (hardwired), except for Broadcom MIPS where it follows the CPU's native endian (let's call that a feature).

Series applied, thanks Florian.

If you have not pushed yet, seems like not, can you check you applied v2 of this patch series, in particular this patch:

http://patchwork.ozlabs.org/patch/807296/

Thanks!

Signature · 1 line
Florian
maintainer
#8 lore raw

From: Florian Fainelli <[email protected]>
Date: Tue, 29 Aug 2017 14:45:30 -0700

Quoted 9 lines · David Miller
Quoted 8 lines

From: Florian Fainelli <[email protected]>
Date: Tue, 29 Aug 2017 11:39:41 -0700

Quoted 4 lines

While trying an ARM BE kernel for kinks, the 3 drivers below started not working and the reasons why became pretty obvious because the register space remains LE (hardwired), except for Broadcom MIPS where it follows the CPU's native endian (let's call that a feature).

Quoted 6 lines
Quoted 1 line

Series applied, thanks Florian.

If you have not pushed yet, seems like not, can you check you applied v2 of this patch series, in particular this patch:

http://patchwork.ozlabs.org/patch/807296/

If you didn't keep messing with the patchwork state of patches in my queue this confusion would not have happened.

I did happen to apply v2, but because of all of the confusion you keep creating, I used the header message from v1.

It's already pushed out so there is nothing we can do about it.

#9 lore raw
Quoted 19 lines · David Miller

From: Florian Fainelli <[email protected]>
Date: Tue, 29 Aug 2017 14:45:30 -0700

Quoted 9 lines · David Miller
Quoted 8 lines

From: Florian Fainelli <[email protected]>
Date: Tue, 29 Aug 2017 11:39:41 -0700

Quoted 4 lines

While trying an ARM BE kernel for kinks, the 3 drivers below started not working and the reasons why became pretty obvious because the register space remains LE (hardwired), except for Broadcom MIPS where it follows the CPU's native endian (let's call that a feature).

Quoted 6 lines
Quoted 1 line

Series applied, thanks Florian.

If you have not pushed yet, seems like not, can you check you applied v2 of this patch series, in particular this patch:

http://patchwork.ozlabs.org/patch/807296/

Quoted 8 lines

If you didn't keep messing with the patchwork state of patches in my queue this confusion would not have happened.

I did happen to apply v2, but because of all of the confusion you keep creating, I used the header message from v1.

It's already pushed out so there is nothing we can do about it.

It's all good, thanks! /me goes updating netdev-FAQ.txt to mention not touching patchwork.

Signature · 1 line
Florian
maintainer
#10 lore raw

From: Florian Fainelli <[email protected]>
Date: Tue, 29 Aug 2017 15:08:00 -0700

Quoted 23 lines · David Miller
Quoted 22 lines

From: Florian Fainelli <[email protected]>
Date: Tue, 29 Aug 2017 14:45:30 -0700

Quoted 9 lines · David Miller
Quoted 8 lines

From: Florian Fainelli <[email protected]>
Date: Tue, 29 Aug 2017 11:39:41 -0700

Quoted 4 lines

While trying an ARM BE kernel for kinks, the 3 drivers below started not working and the reasons why became pretty obvious because the register space remains LE (hardwired), except for Broadcom MIPS where it follows the CPU's native endian (let's call that a feature).

Quoted 6 lines
Quoted 1 line

Series applied, thanks Florian.

If you have not pushed yet, seems like not, can you check you applied v2 of this patch series, in particular this patch:

http://patchwork.ozlabs.org/patch/807296/

If you didn't keep messing with the patchwork state of patches in my queue this confusion would not have happened.

Quoted 8 lines
Quoted 5 lines

I did happen to apply v2, but because of all of the confusion you keep creating, I used the header message from v1.

It's already pushed out so there is nothing we can do about it.

It's all good, thanks! /me goes updating netdev-FAQ.txt to mention not touching patchwork.

Thank you.