Skip to contentSkip to conversation

usb: dwc2: regression on MyBook Live Duo / Canyonlands since 4.3.0-rc4

PATCH Christian Lamparter 6 participants 32 messages May 7, 2016 – May 19, 2016

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

Hello,

I've been looking in getting the MyBook Live Duo's USB OTG port to function. The SoC is a APM82181. Which has a PowerPC 464 core and related to the supported canyonlands architecture in arch/powerpc/.=

Currently in -next the dwc2 module doesn't load:=20

dwc2 4bff80000.usbotg: dwc2_core_reset() HANG! AHB Idle GRSTCTL=3D80 dwc2 4bff80000.usbotg: Bad value for GSNPSID: 0x0a29544f

Looking at the Bad GSNPSID value: 0x0a29544f. It is obvious that this is an endian problem. git finds this patch:

commit 95c8bc3609440af5e4a4f760b8680caea7424396

Author: Antti Sepp=E4l=E4 <[email protected]>
Date:   Thu Aug 20 21:41:07 2015 +0300

usb: dwc2: Use platform endianness when accessing registers

This patch is necessary to access dwc2 registers correctly on big-e=
ndian
systems such as the mips based SoCs made by Lantiq. Then dwc2 can b=
e
used to replace ifx-hcd driver for Lantiq platforms found e.g. in OpenWrt.
=20

    The patch was autogenerated with the following commands:
    $EDITOR core.h
    sed -i "s/\<readl\>/dwc2_readl/g" *.c hcd.h hw.h
    sed -i "s/\<writel\>/dwc2_writel/g" *.c hcd.h hw.h

=20
Some files were then hand-edited to fix checkpatch.pl warnings abou=
t
too long lines.

which unfortunately, broke the USB-OTG port on the MyBook Live Duo. Reverting to the readl / writel:

---=20

drivers/usb/dwc2/core.h +0 -0
index 3c58d63..c021c1f 100644
@@ -66,7 +66,7 @@

=20
static inline u32 dwc2_readl(const void __iomem *addr)
{
-=09u32 value =3D __raw_readl(addr);
+=09u32 value =3D readl(addr);
=20
=09/* In order to preserve endianness __raw_* operation is used. There=
fore
=09 * a barrier is needed to ensure IO access is not re-ordered across=

@@ -78,7 +78,7 @@ static inline u32 dwc2_readl(const void __iomem *addr= )
=20
static inline void dwc2_writel(u32 value, void __iomem *addr) {
-=09__raw_writel(value, addr);
+=09writel(value, addr);
=20
=09/*
=09 * In order to preserve endianness __raw_* operation is used. There=
fore

---

restores the dwc-otg port to full working order:
dwc2 4bff80000.usbotg: Specified GNPTXFDEP=3D1024 > 256 dwc2 4bff80000.usbotg: EPs: 3, shared fifos, 2042 entries in SPRAM dwc2 4bff80000.usbotg: DWC OTG Controller
dwc2 4bff80000.usbotg: new USB bus registered, assigned bus number 1 dwc2 4bff80000.usbotg: irq 33, io mem 0x00000000
hub 1-0:1.0: USB hub found
hub 1-0:1.0: 1 port detected

root@mbl:~# usb 1-1: new high-speed USB device number 2 using dwc2

So, what to do?

Regards,
Christian

#1 lore raw
Quoted 8 lines · Christian Lamparter via Linuxppc-dev

Hello,

I've been looking in getting the MyBook Live Duo's USB OTG port to function. The SoC is a APM82181. Which has a PowerPC 464 core and related to the supported canyonlands architecture in arch/powerpc/.

Currently in -next the dwc2 module doesn't load:

Smells like the APM implementation is little endian. You might need to use a flag to indicate what endian to use instead and set it appropriately based on some DT properties.

Quoted 79 lines

dwc2 4bff80000.usbotg: dwc2_core_reset() HANG! AHB Idle GRSTCTL=80 dwc2 4bff80000.usbotg: Bad value for GSNPSID: 0x0a29544f

Looking at the Bad GSNPSID value: 0x0a29544f. It is obvious that this is an endian problem. git finds this patch:

commit 95c8bc3609440af5e4a4f760b8680caea7424396
Author: Antti Seppälä <[email protected]>
Date:   Thu Aug 20 21:41:07 2015 +0300

    usb: dwc2: Use platform endianness when accessing registers

    This patch is necessary to access dwc2 registers correctly on big-endian
    systems such as the mips based SoCs made by Lantiq. Then dwc2 can be
    used to replace ifx-hcd driver for Lantiq platforms found e.g. in OpenWrt.

    The patch was autogenerated with the following commands:
    $EDITOR core.h
    sed -i "s/\<readl\>/dwc2_readl/g" *.c hcd.h hw.h
    sed -i "s/\<writel\>/dwc2_writel/g" *.c hcd.h hw.h

    Some files were then hand-edited to fix checkpatch.pl warnings about
    too long lines.

which unfortunately, broke the USB-OTG port on the MyBook Live Duo. Reverting to the readl / writel:

---

drivers/usb/dwc2/core.h +0 -0
index 3c58d63..c021c1f 100644
@@ -66,7 +66,7 @@


 static inline u32 dwc2_readl(const void __iomem *addr) {

drivers/usb/dwc2/core.h +1 -1
-	u32 value = __raw_readl(addr);
+	u32 value = readl(addr);


  /* In order to preserve endianness __raw_* operation is used. Therefore
   * a barrier is needed to ensure IO access is not re-ordered across

drivers/usb/dwc2/core.h +0 -0
@@ -78,7 +78,7 @@ static inline u32 dwc2_readl(const void __iomem
 *addr)


 static inline void dwc2_writel(u32 value, void __iomem *addr) {

drivers/usb/dwc2/core.h +1 -1
-	__raw_writel(value, addr);
+	writel(value, addr);


  /*
   * In order to preserve endianness __raw_* operation is used. Therefore

---

restores the dwc-otg port to full working order:
dwc2 4bff80000.usbotg: Specified GNPTXFDEP=1024 > 256 dwc2 4bff80000.usbotg: EPs: 3, shared fifos, 2042 entries in SPRAM dwc2 4bff80000.usbotg: DWC OTG Controller
dwc2 4bff80000.usbotg: new USB bus registered, assigned bus number 1 dwc2 4bff80000.usbotg: irq 33, io mem 0x00000000
hub 1-0:1.0: USB hub found
hub 1-0:1.0: 1 port detected

root@mbl:~# usb 1-1: new high-speed USB device number 2 using dwc2

So, what to do?

Regards,
Christian
_______________________________________________
Linuxppc-dev mailing list
[email protected]
https://lists.ozlabs.org/listinfo/linuxppc-dev

#2 lore raw
Quoted 1 line · Benjamin Herrenschmidt

On Sun, 2016-05-08 at 00:54 +0200, Christian Lamparter via Linuxppc-d=

ev=20

Quoted 9 lines
Quoted 6 lines · wrote:

I've been looking in getting the MyBook Live Duo's USB OTG port to function. The SoC is a APM82181. Which has a PowerPC 464 core and related to the supported canyonlands architecture in arch/powerpc/.
=20
Currently in -next the dwc2 module doesn't load:=20

=20
Smells like the APM implementation is little endian. You might need t=

o

Quoted 2 lines

use a flag to indicate what endian to use instead and set it appropriately based on some DT properties.

I tried. As per common-properties[0], I added little-endian; but it has=
no
effect. I looked in dwc2_driver_probe and found no way of specifying th= e
endian of the device. It all comes down to the dwc2_readl & dwc2_writel=

accessors. These - sadly - have been hardwired to use __raw_readl and __raw_writel. So, it's always "native-endian". While common-properties says little-endian should be preferred.

Quoted 1 line
Quoted 1 line

dwc2 4bff80000.usbotg: dwc2_core_reset() HANG! AHB Idle GRSTCTL=3D8=

0

Quoted 6 lines
Quoted 6 lines

dwc2 4bff80000.usbotg: Bad value for GSNPSID: 0x0a29544f =20
Looking at the Bad GSNPSID value: 0x0a29544f. It is obvious that this is an endian problem. git finds this patch:
=20
commit 95c8bc3609440af5e4a4f760b8680caea7424396

Quoted 8 lines
Quoted 8 lines
Author: Antti Sepp=E4l=E4 <[email protected]>
Date:   Thu Aug 20 21:41:07 2015 +0300

=20
usb: dwc2: Use platform endianness when accessing registers
=20
This patch is necessary to access dwc2 registers correctly on
big-endian
systems such as the mips based SoCs made by Lantiq. Then dwc2 c=

an

Quoted 2 lines
Quoted 2 lines

be
used to replace ifx-hcd driver for Lantiq platforms found e.g. =

in

Quoted 43 lines
Quoted 43 lines

OpenWrt.
=20

    The patch was autogenerated with the following commands:
    $EDITOR core.h
    sed -i "s/\<readl\>/dwc2_readl/g" *.c hcd.h hw.h
    sed -i "s/\<writel\>/dwc2_writel/g" *.c hcd.h hw.h

=20
Some files were then hand-edited to fix checkpatch.pl warnings
about
too long lines.
=20
which unfortunately, broke the USB-OTG port on the MyBook Live Duo.=

Reverting to the readl / writel:
=20
---=20

drivers/usb/dwc2/core.h +0 -0
index 3c58d63..c021c1f 100644
@@ -66,7 +66,7 @@

=20
static inline u32 dwc2_readl(const void __iomem *addr)
{
-=09u32 value =3D __raw_readl(addr);
+=09u32 value =3D readl(addr);
=20
=09/* In order to preserve endianness __raw_* operation is
used. Therefore
=09 * a barrier is needed to ensure IO access is not re-ordered=20=

across

drivers/usb/dwc2/core.h +0 -0
@@ -78,7 +78,7 @@ static inline u32 dwc2_readl(const void __iomem
 *addr)

=20
static inline void dwc2_writel(u32 value, void __iomem *addr) {
-=09__raw_writel(value, addr);
+=09writel(value, addr);
=20
=09/*
=09 * In order to preserve endianness __raw_* operation is
used. Therefore

Quoted 8 lines
Quoted 8 lines

=20
---
=20
restores the dwc-otg port to full working order:
dwc2 4bff80000.usbotg: Specified GNPTXFDEP=3D1024 > 256 dwc2 4bff80000.usbotg: EPs: 3, shared fifos, 2042 entries in SPRAM dwc2 4bff80000.usbotg: DWC OTG Controller
dwc2 4bff80000.usbotg: new USB bus registered, assigned bus number =

1

Quoted 6 lines
Quoted 6 lines

dwc2 4bff80000.usbotg: irq 33, io mem 0x00000000
hub 1-0:1.0: USB hub found
hub 1-0:1.0: 1 port detected

root@mbl:~# usb 1-1: new high-speed USB device number 2 using dwc2
=20
So, what to do?
     ^^^

Regards,
Christian

[0] <http://lxr.free-electrons.com/source/Documentation/devicetree/bind=
ings/common-properties.txt>

#3 lore raw
Quoted 14 lines · Christian Lamparter
Quoted 9 lines · Benjamin Herrenschmidt
Quoted 6 lines · Christian Lamparter via Linuxppc-dev

I've been looking in getting the MyBook Live Duo's USB OTG port to function. The SoC is a APM82181. Which has a PowerPC 464 core and related to the supported canyonlands architecture in arch/powerpc/.

Quoted 4 lines
Quoted 1 line

Currently in -next the dwc2 module doesn't load:

Smells like the APM implementation is little endian. You might need to use a flag to indicate what endian to use instead and set it appropriately based on some DT properties.

Quoted 6 lines

I tried. As per common-properties[0], I added little-endian; but it has no effect. I looked in dwc2_driver_probe and found no way of specifying the endian of the device. It all comes down to the dwc2_readl & dwc2_writel accessors. These - sadly - have been hardwired to use __raw_readl and __raw_writel. So, it's always "native-endian". While common-properties says little-endian should be preferred.

Right, I meant, you should produce a patch adding a runtime test inside those functions based on a device-tree property, a bit like we do for some of the HCDs like OHCI, EHCI etc...

Cheers,
Ben.

Quoted 85 lines
Quoted 73 lines
Quoted 73 lines

dwc2 4bff80000.usbotg: dwc2_core_reset() HANG! AHB Idle GRSTCTL=80 dwc2 4bff80000.usbotg: Bad value for GSNPSID: 0x0a29544f

Looking at the Bad GSNPSID value: 0x0a29544f. It is obvious that this is an endian problem. git finds this patch:

commit 95c8bc3609440af5e4a4f760b8680caea7424396
Author: Antti Seppälä <[email protected]>
Date:   Thu Aug 20 21:41:07 2015 +0300

    usb: dwc2: Use platform endianness when accessing registers

    This patch is necessary to access dwc2 registers correctly on big-endian
    systems such as the mips based SoCs made by Lantiq. Then dwc2 can be
    used to replace ifx-hcd driver for Lantiq platforms found e.g. in OpenWrt.

    The patch was autogenerated with the following commands:
    $EDITOR core.h
    sed -i "s/\/dwc2_readl/g" *.c hcd.h hw.h
    sed -i "s/\/dwc2_writel/g" *.c hcd.h hw.h

    Some files were then hand-edited to fix checkpatch.pl warnings about
    too long lines.

which unfortunately, broke the USB-OTG port on the MyBook Live Duo. Reverting to the readl / writel:

---

drivers/usb/dwc2/core.h +0 -0
index 3c58d63..c021c1f 100644
@@ -66,7 +66,7 @@


 static inline u32 dwc2_readl(const void __iomem *addr) {

drivers/usb/dwc2/core.h +1 -1
-	u32 value = __raw_readl(addr);
+	u32 value = readl(addr);


  /* In order to preserve endianness __raw_* operation is used. Therefore
   * a barrier is needed to ensure IO access is not re-ordered across

drivers/usb/dwc2/core.h +0 -0
@@ -78,7 +78,7 @@ static inline u32 dwc2_readl(const void __iomem
 *addr)


 static inline void dwc2_writel(u32 value, void __iomem *addr) {

drivers/usb/dwc2/core.h +1 -1
-	__raw_writel(value, addr);
+	writel(value, addr);


  /*
   * In order to preserve endianness __raw_* operation is used. Therefore

---

restores the dwc-otg port to full working order:
dwc2 4bff80000.usbotg: Specified GNPTXFDEP=1024 > 256 dwc2 4bff80000.usbotg: EPs: 3, shared fifos, 2042 entries in SPRAM dwc2 4bff80000.usbotg: DWC OTG Controller
dwc2 4bff80000.usbotg: new USB bus registered, assigned bus number 1 dwc2 4bff80000.usbotg: irq 33, io mem 0x00000000
hub 1-0:1.0: USB hub found
hub 1-0:1.0: 1 port detected

root@mbl:~# usb 1-1: new high-speed USB device number 2 using dwc2
Quoted 1 line
Quoted 1 line

So, what to do?

     ^^^

Regards,
Christian

[0]

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html

#4 lore raw
Quoted 22 lines · On Monday 09 May 2016 10:23:22 Benjamin Herrenschmidt wrote:
Quoted 14 lines · Christian Lamparter
Quoted 9 lines · Benjamin Herrenschmidt
Quoted 6 lines · Christian Lamparter via Linuxppc-dev

I've been looking in getting the MyBook Live Duo's USB OTG port to function. The SoC is a APM82181. Which has a PowerPC 464 core and related to the supported canyonlands architecture in arch/powerpc/.

Quoted 4 lines
Quoted 1 line

Currently in -next the dwc2 module doesn't load:

Smells like the APM implementation is little endian. You might need to use a flag to indicate what endian to use instead and set it appropriately based on some DT properties.

Quoted 6 lines

I tried. As per common-properties[0], I added little-endian; but it has no effect. I looked in dwc2_driver_probe and found no way of specifying the endian of the device. It all comes down to the dwc2_readl & dwc2_writel accessors. These - sadly - have been hardwired to use __raw_readl and __raw_writel. So, it's always "native-endian". While common-properties says little-endian should be preferred.

Quoted 5 lines

Right, I meant, you should produce a patch adding a runtime test inside those functions based on a device-tree property, a bit like we do for some of the HCDs like OHCI, EHCI etc...

The patch that caused the problem had multiple issues:

- it broke big-endian ARM kernels: any machine that was working correctly with a little-endian kernel is no longer using byteswaps on big-endian kernels, which clearly breaks them.
- On PowerPC the same thing must be true: if it was working before, using big-endian kernels is now broken. Unlike ARM, 32-bit PowerPC usually uses big-endian kernels, so they are likely all broken.
- The barrier for dwc2_writel is on the wrong side of the __raw_writel(), so the MMIO no longer synchronizes with DMA operations.
- On architectures that require specific CPU instructions for MMIO access, using the __raw_ variant may turn this into a pointer dereference that does not have the same effect as the readl/writel.

I think we can simply make this set of accessors architecture-dependent (MIPS vs. the rest of the world) to revert ARM and PowerPC back to the working version.

drivers/usb/dwc2/core.h +35 -6
index 3c58d633ce80..1f8ed149a40f 100644
@@ -64,12 +64,24 @@
 	DWC2_TRACE_SCHEDULER_VB(pr_fmt("%s: SCH: " fmt),		\
 				dev_name(hsotg->dev), ##__VA_ARGS__)
 
+
+#ifdef CONFIG_MIPS
+/*
+ * There are some MIPS machines that can run in either big-endian
+ * or little-endian mode and that use the dwc2 register without
+ * a byteswap in both ways.
+ * Unlike other architectures, MIPS does not require a barrier
+ * before the __raw_writel() to synchronize with DMA but does
+ * require the barrier after the writel() to serialize a series
+ * of writes. This set of operations was added specifically for
+ * MIPS and should only be used there.
+ */
 static inline u32 dwc2_readl(const void __iomem *addr)
 {
 	u32 value = __raw_readl(addr);
 
-	/* In order to preserve endianness __raw_* operation is used. Therefore
-	 * a barrier is needed to ensure IO access is not re-ordered across
+	/* in order to preserve endianness __raw_* operation is used. therefore
+	 * a barrier is needed to ensure io access is not re-ordered across
 	 * reads or writes
 	 */
 	mb();
@@ -81,15 +93,32 @@ static inline void dwc2_writel(u32 value, void __iomem *addr)
 	__raw_writel(value, addr);
 
 	/*
-	 * In order to preserve endianness __raw_* operation is used. Therefore
-	 * a barrier is needed to ensure IO access is not re-ordered across
+	 * in order to preserve endianness __raw_* operation is used. therefore
+	 * a barrier is needed to ensure io access is not re-ordered across
 	 * reads or writes
 	 */
 	mb();
-#ifdef DWC2_LOG_WRITES
-	pr_info("INFO:: wrote %08x to %p\n", value, addr);
+#ifdef dwc2_log_writes
+	pr_info("info:: wrote %08x to %p\n", value, addr);
 #endif
 }
+#else
+/* Normal architectures just use readl/write */
+static inline u32 dwc2_readl(const void __iomem *addr)
+{
+	u32 value = readl(addr);
+	return value;
+}
+
+static inline void dwc2_writel(u32 value, void __iomem *addr)
+{
+	writel(value, addr);
+
+#ifdef dwc2_log_writes
+	pr_info("info:: wrote %08x to %p\n", value, addr);
+#endif
+}
+#endif
 
 /* Maximum number of Endpoints/HostChannels */
 #define MAX_EPS_CHANNELS	16
#5 lore raw

Hi,

Quoted 43 lines · Arnd Bergmann
Quoted 22 lines · On Monday 09 May 2016 10:23:22 Benjamin Herrenschmidt wrote:
Quoted 14 lines · Christian Lamparter
Quoted 9 lines · Benjamin Herrenschmidt
Quoted 6 lines · Christian Lamparter via Linuxppc-dev

I've been looking in getting the MyBook Live Duo's USB OTG port to function. The SoC is a APM82181. Which has a PowerPC 464 core and related to the supported canyonlands architecture in arch/powerpc/.

Quoted 4 lines
Quoted 1 line

Currently in -next the dwc2 module doesn't load:

Smells like the APM implementation is little endian. You might need to use a flag to indicate what endian to use instead and set it appropriately based on some DT properties.

Quoted 6 lines

I tried. As per common-properties[0], I added little-endian; but it has no effect. I looked in dwc2_driver_probe and found no way of specifying the endian of the device. It all comes down to the dwc2_readl & dwc2_writel accessors. These - sadly - have been hardwired to use __raw_readl and __raw_writel. So, it's always "native-endian". While common-properties says little-endian should be preferred.

Quoted 5 lines

Right, I meant, you should produce a patch adding a runtime test inside those functions based on a device-tree property, a bit like we do for some of the HCDs like OHCI, EHCI etc...

The patch that caused the problem had multiple issues:

- it broke big-endian ARM kernels: any machine that was working correctly with a little-endian kernel is no longer using byteswaps on big-endian kernels, which clearly breaks them.
- On PowerPC the same thing must be true: if it was working before, using big-endian kernels is now broken. Unlike ARM, 32-bit PowerPC usually uses big-endian kernels, so they are likely all broken.
- The barrier for dwc2_writel is on the wrong side of the __raw_writel(), so the MMIO no longer synchronizes with DMA operations.
- On architectures that require specific CPU instructions for MMIO access, using the __raw_ variant may turn this into a pointer dereference that does not have the same effect as the readl/writel.

Quoted 3 lines

I think we can simply make this set of accessors architecture-dependent (MIPS vs. the rest of the world) to revert ARM and PowerPC back to the working version.

and patch all drivers similarly? Shouldn't arch/mips itself deal with it and hide it from drivers ?

Signature · 1 line
balbi
#6 lore raw
Quoted 18 lines · On Monday 09 May 2016 13:39:50 Felipe Balbi wrote:
Quoted 17 lines · Arnd Bergmann
Quoted 1 line · On Monday 09 May 2016 10:23:22 Benjamin Herrenschmidt wrote:

On Sun, 2016-05-08 at 13:44 +0200, Christian Lamparter wrote:

The patch that caused the problem had multiple issues:

- it broke big-endian ARM kernels: any machine that was working correctly with a little-endian kernel is no longer using byteswaps on big-endian kernels, which clearly breaks them.
- On PowerPC the same thing must be true: if it was working before, using big-endian kernels is now broken. Unlike ARM, 32-bit PowerPC usually uses big-endian kernels, so they are likely all broken.
- The barrier for dwc2_writel is on the wrong side of the __raw_writel(), so the MMIO no longer synchronizes with DMA operations.
- On architectures that require specific CPU instructions for MMIO access, using the __raw_ variant may turn this into a pointer dereference that does not have the same effect as the readl/writel.

Quoted 7 lines
Quoted 3 lines

I think we can simply make this set of accessors architecture-dependent (MIPS vs. the rest of the world) to revert ARM and PowerPC back to the working version.

and patch all drivers similarly? Shouldn't arch/mips itself deal with it and hide it from drivers ?

Unfortunately, I don't see any way this could be done in MIPS specific
code: There is typically a byteswap between the internal bus and the PCI bus on big-endian MIPS systems, so the PCI MMIO ends up being little-endian, which matches the expected behavior of readl/writel. However, drivers for non-PCI devices often use the same readl/writel accessors because that is how it's done on ARMv6/ARMv7.

Doing it hardcoded by architecture is just the simplest way to deal with it, working on the assumption that nothing actually needs the runtime detection that Ben suggested. Detecting the endianess of the device is probably the best future-proof solution, but it's also considerably more work to do in the driver, and comes with a tiny runtime overhead.

Arnd

#7 lore raw

Uh, Thanks for the participation!

Quoted 34 lines · Arnd Bergmann
Quoted 18 lines · On Monday 09 May 2016 13:39:50 Felipe Balbi wrote:
Quoted 17 lines · Arnd Bergmann
Quoted 1 line · On Monday 09 May 2016 10:23:22 Benjamin Herrenschmidt wrote:

On Sun, 2016-05-08 at 13:44 +0200, Christian Lamparter wrote:

The patch that caused the problem had multiple issues:

- it broke big-endian ARM kernels: any machine that was working correctly with a little-endian kernel is no longer using byteswaps on big-endian kernels, which clearly breaks them.
- On PowerPC the same thing must be true: if it was working before, using big-endian kernels is now broken. Unlike ARM, 32-bit PowerPC usually uses big-endian kernels, so they are likely all broken.
- The barrier for dwc2_writel is on the wrong side of the __raw_writel(), so the MMIO no longer synchronizes with DMA operations.
- On architectures that require specific CPU instructions for MMIO access, using the __raw_ variant may turn this into a pointer dereference that does not have the same effect as the readl/writel.

Quoted 7 lines
Quoted 3 lines

I think we can simply make this set of accessors architecture-dependent (MIPS vs. the rest of the world) to revert ARM and PowerPC back to the working version.

and patch all drivers similarly? Shouldn't arch/mips itself deal with it and hide it from drivers ?

Unfortunately, I don't see any way this could be done in MIPS specific
code: There is typically a byteswap between the internal bus and the PCI bus on big-endian MIPS systems, so the PCI MMIO ends up being little-endian, which matches the expected behavior of readl/writel. However, drivers for non-PCI devices often use the same readl/writel accessors because that is how it's done on ARMv6/ARMv7.

Quoted 7 lines

Doing it hardcoded by architecture is just the simplest way to deal with it, working on the assumption that nothing actually needs the runtime detection that Ben suggested. Detecting the endianess of the device is probably the best future-proof solution, but it's also considerably more work to do in the driver, and comes with a tiny runtime overhead.

Ok, just to have it on the table. I went ahead and implemented the "Detect Endian".

I looked in the DWC USB OTG's Databook documents v3.30a (If someone wants them too, PM me). If I read the Application Interface Feature list on page 30 correctly. The endianess is selectable by a "pin".... That said I don't know which one is it in the APM82181 or any other arch. I looked around for configuration registers and stuff but unlike DesignWare's AHB DMA Controller, there's no Bit in the "User HW Config Registers" that would tell us if it was configured as big-endian or little-endian at the moment.

One way out would be to detect the endianess automatically by looking at the values in the GSNPSID register. This is a read-only register containing the release number of the core being used. The "upper" 16-bits of it are hardcoded to 0x4f45 (The comment in dwc2_get_hwparams [1] has it backwards but not the code below).

I ran into the following issues:
- gadget.c uses ioread32_rep [0] & iowrite32_rep [1]. This is interesting because both of these functions actually use the __raw_io* on powerpc. This is because powerpc uses the default defines of include/asm-generic/io.h [2].

Ideally, this should be done by sth like a writesl_be or writesl(e) function. But I found none so for now: Let's make a ugly hack:
to_correct_endian that will work for testing, but will be replaced.

- dwc2_readl, dwc2_writel. I have no insight in the craziness that's going on with MIPS and the memory barrier. But it got me thinking rather than CONFIG_MIPS, isn't there a umbrella "ARCH_HAS_NEED_MEMORY_BARRIER_FOR_MMIO" config symbol?

- is_little_endian (do we want a separate is_big_endian?) Also, do we want to be able to overwrite the detection code if the endian setting was set in the device tree?. For now it always does auto detection (see dwc2_detect_endiannes() ).

( - 80 character per line issues, is it possible to drop the
hsotg->reg + REGISTER from the dwc2_readl/writel since we pass the hsotg now anyway and do the reg + REGISTER calculation in the accessor? I played around with macros as most functions calling the accessors have the hsotg variable anyway )

Regards,
Christian

[0] <http://lxr.free-electrons.com/source/drivers/usb/dwc2/gadget.c#L1488>
[1] <http://lxr.free-electrons.com/source/drivers/usb/dwc2/gadget.c#L462>
[2] <http://lxr.free-electrons.com/source/include/asm-generic/io.h#L315>

---

drivers/usb/dwc2/core.c +108 -99
index 4135a5f..478379a 100644
@@ -71,16 +71,16 @@ static int dwc2_backup_global_registers(struct dwc2_hsotg *hsotg)
 	/* Backup global regs */
 	gr = &hsotg->gr_backup;
 
-	gr->gotgctl = dwc2_readl(hsotg->regs + GOTGCTL);
-	gr->gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
-	gr->gahbcfg = dwc2_readl(hsotg->regs + GAHBCFG);
-	gr->gusbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
-	gr->grxfsiz = dwc2_readl(hsotg->regs + GRXFSIZ);
-	gr->gnptxfsiz = dwc2_readl(hsotg->regs + GNPTXFSIZ);
-	gr->hptxfsiz = dwc2_readl(hsotg->regs + HPTXFSIZ);
-	gr->gdfifocfg = dwc2_readl(hsotg->regs + GDFIFOCFG);
+	gr->gotgctl = dwc2_readl(hsotg, hsotg->regs + GOTGCTL);
+	gr->gintmsk = dwc2_readl(hsotg, hsotg->regs + GINTMSK);
+	gr->gahbcfg = dwc2_readl(hsotg, hsotg->regs + GAHBCFG);
+	gr->gusbcfg = dwc2_readl(hsotg, hsotg->regs + GUSBCFG);
+	gr->grxfsiz = dwc2_readl(hsotg, hsotg->regs + GRXFSIZ);
+	gr->gnptxfsiz = dwc2_readl(hsotg, hsotg->regs + GNPTXFSIZ);
+	gr->hptxfsiz = dwc2_readl(hsotg, hsotg->regs + HPTXFSIZ);
+	gr->gdfifocfg = dwc2_readl(hsotg, hsotg->regs + GDFIFOCFG);
 	for (i = 0; i < MAX_EPS_CHANNELS; i++)
-		gr->dtxfsiz[i] = dwc2_readl(hsotg->regs + DPTXFSIZN(i));
+		gr->dtxfsiz[i] = dwc2_readl(hsotg, hsotg->regs + DPTXFSIZN(i));
 
 	gr->valid = true;
 	return 0;
@@ -109,17 +109,17 @@ static int dwc2_restore_global_registers(struct dwc2_hsotg *hsotg)
 	}
 	gr->valid = false;
 
-	dwc2_writel(0xffffffff, hsotg->regs + GINTSTS);
-	dwc2_writel(gr->gotgctl, hsotg->regs + GOTGCTL);
-	dwc2_writel(gr->gintmsk, hsotg->regs + GINTMSK);
-	dwc2_writel(gr->gusbcfg, hsotg->regs + GUSBCFG);
-	dwc2_writel(gr->gahbcfg, hsotg->regs + GAHBCFG);
-	dwc2_writel(gr->grxfsiz, hsotg->regs + GRXFSIZ);
-	dwc2_writel(gr->gnptxfsiz, hsotg->regs + GNPTXFSIZ);
-	dwc2_writel(gr->hptxfsiz, hsotg->regs + HPTXFSIZ);
-	dwc2_writel(gr->gdfifocfg, hsotg->regs + GDFIFOCFG);
+	dwc2_writel(hsotg, 0xffffffff, hsotg->regs + GINTSTS);
+	dwc2_writel(hsotg, gr->gotgctl, hsotg->regs + GOTGCTL);
+	dwc2_writel(hsotg, gr->gintmsk, hsotg->regs + GINTMSK);
+	dwc2_writel(hsotg, gr->gusbcfg, hsotg->regs + GUSBCFG);
+	dwc2_writel(hsotg, gr->gahbcfg, hsotg->regs + GAHBCFG);
+	dwc2_writel(hsotg, gr->grxfsiz, hsotg->regs + GRXFSIZ);
+	dwc2_writel(hsotg, gr->gnptxfsiz, hsotg->regs + GNPTXFSIZ);
+	dwc2_writel(hsotg, gr->hptxfsiz, hsotg->regs + HPTXFSIZ);
+	dwc2_writel(hsotg, gr->gdfifocfg, hsotg->regs + GDFIFOCFG);
 	for (i = 0; i < MAX_EPS_CHANNELS; i++)
-		dwc2_writel(gr->dtxfsiz[i], hsotg->regs + DPTXFSIZN(i));
+		dwc2_writel(hsotg, gr->dtxfsiz[i], hsotg->regs + DPTXFSIZN(i));
 
 	return 0;
 }
@@ -138,17 +138,17 @@ int dwc2_exit_hibernation(struct dwc2_hsotg *hsotg, bool restore)
 	if (!hsotg->core_params->hibernation)
 		return -ENOTSUPP;
 
-	pcgcctl = dwc2_readl(hsotg->regs + PCGCTL);
+	pcgcctl = dwc2_readl(hsotg, hsotg->regs + PCGCTL);
 	pcgcctl &= ~PCGCTL_STOPPCLK;
-	dwc2_writel(pcgcctl, hsotg->regs + PCGCTL);
+	dwc2_writel(hsotg, pcgcctl, hsotg->regs + PCGCTL);
 
-	pcgcctl = dwc2_readl(hsotg->regs + PCGCTL);
+	pcgcctl = dwc2_readl(hsotg, hsotg->regs + PCGCTL);
 	pcgcctl &= ~PCGCTL_PWRCLMP;
-	dwc2_writel(pcgcctl, hsotg->regs + PCGCTL);
+	dwc2_writel(hsotg, pcgcctl, hsotg->regs + PCGCTL);
 
-	pcgcctl = dwc2_readl(hsotg->regs + PCGCTL);
+	pcgcctl = dwc2_readl(hsotg, hsotg->regs + PCGCTL);
 	pcgcctl &= ~PCGCTL_RSTPDWNMODULE;
-	dwc2_writel(pcgcctl, hsotg->regs + PCGCTL);
+	dwc2_writel(hsotg, pcgcctl, hsotg->regs + PCGCTL);
 
 	udelay(100);
 	if (restore) {
@@ -219,21 +219,21 @@ int dwc2_enter_hibernation(struct dwc2_hsotg *hsotg)
 	 * Clear any pending interrupts since dwc2 will not be able to
 	 * clear them after entering hibernation.
 	 */
-	dwc2_writel(0xffffffff, hsotg->regs + GINTSTS);
+	dwc2_writel(hsotg, 0xffffffff, hsotg->regs + GINTSTS);
 
 	/* Put the controller in low power state */
-	pcgcctl = dwc2_readl(hsotg->regs + PCGCTL);
+	pcgcctl = dwc2_readl(hsotg, hsotg->regs + PCGCTL);
 
 	pcgcctl |= PCGCTL_PWRCLMP;
-	dwc2_writel(pcgcctl, hsotg->regs + PCGCTL);
+	dwc2_writel(hsotg, pcgcctl, hsotg->regs + PCGCTL);
 	ndelay(20);
 
 	pcgcctl |= PCGCTL_RSTPDWNMODULE;
-	dwc2_writel(pcgcctl, hsotg->regs + PCGCTL);
+	dwc2_writel(hsotg, pcgcctl, hsotg->regs + PCGCTL);
 	ndelay(20);
 
 	pcgcctl |= PCGCTL_STOPPCLK;
-	dwc2_writel(pcgcctl, hsotg->regs + PCGCTL);
+	dwc2_writel(hsotg, pcgcctl, hsotg->regs + PCGCTL);
 
 	return ret;
 }
@@ -250,12 +250,12 @@ int dwc2_core_reset(struct dwc2_hsotg *hsotg)
 	dev_vdbg(hsotg->dev, "%s()\n", __func__);
 
 	/* Core Soft Reset */
-	greset = dwc2_readl(hsotg->regs + GRSTCTL);
+	greset = dwc2_readl(hsotg, hsotg->regs + GRSTCTL);
 	greset |= GRSTCTL_CSFTRST;
-	dwc2_writel(greset, hsotg->regs + GRSTCTL);
+	dwc2_writel(hsotg, greset, hsotg->regs + GRSTCTL);
 	do {
 		udelay(1);
-		greset = dwc2_readl(hsotg->regs + GRSTCTL);
+		greset = dwc2_readl(hsotg, hsotg->regs + GRSTCTL);
 		if (++count > 50) {
 			dev_warn(hsotg->dev,
 				 "%s() HANG! Soft Reset GRSTCTL=%0x\n",
@@ -268,7 +268,7 @@ int dwc2_core_reset(struct dwc2_hsotg *hsotg)
 	count = 0;
 	do {
 		udelay(1);
-		greset = dwc2_readl(hsotg->regs + GRSTCTL);
+		greset = dwc2_readl(hsotg, hsotg->regs + GRSTCTL);
 		if (++count > 50) {
 			dev_warn(hsotg->dev,
 				 "%s() HANG! AHB Idle GRSTCTL=%0x\n",
@@ -328,14 +328,14 @@ static bool dwc2_force_mode(struct dwc2_hsotg *hsotg, bool host)
 	if (WARN_ON(!host && hsotg->dr_mode == USB_DR_MODE_HOST))
 		return false;
 
-	gusbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
+	gusbcfg = dwc2_readl(hsotg, hsotg->regs + GUSBCFG);
 
 	set = host ? GUSBCFG_FORCEHOSTMODE : GUSBCFG_FORCEDEVMODE;
 	clear = host ? GUSBCFG_FORCEDEVMODE : GUSBCFG_FORCEHOSTMODE;
 
 	gusbcfg &= ~clear;
 	gusbcfg |= set;
-	dwc2_writel(gusbcfg, hsotg->regs + GUSBCFG);
+	dwc2_writel(hsotg, gusbcfg, hsotg->regs + GUSBCFG);
 
 	msleep(25);
 	return true;
@@ -348,10 +348,10 @@ static void dwc2_clear_force_mode(struct dwc2_hsotg *hsotg)
 {
 	u32 gusbcfg;
 
-	gusbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
+	gusbcfg = dwc2_readl(hsotg, hsotg->regs + GUSBCFG);
 	gusbcfg &= ~GUSBCFG_FORCEHOSTMODE;
 	gusbcfg &= ~GUSBCFG_FORCEDEVMODE;
-	dwc2_writel(gusbcfg, hsotg->regs + GUSBCFG);
+	dwc2_writel(hsotg, gusbcfg, hsotg->regs + GUSBCFG);
 
 	/*
 	 * NOTE: This long sleep is _very_ important, otherwise the core will
@@ -424,56 +424,56 @@ void dwc2_dump_host_registers(struct dwc2_hsotg *hsotg)
 	dev_dbg(hsotg->dev, "Host Global Registers\n");
 	addr = hsotg->regs + HCFG;
 	dev_dbg(hsotg->dev, "HCFG	 @0x%08lX : 0x%08X\n",
-		(unsigned long)addr, dwc2_readl(addr));
+		(unsigned long)addr, dwc2_readl(hsotg, addr));
 	addr = hsotg->regs + HFIR;
 	dev_dbg(hsotg->dev, "HFIR	 @0x%08lX : 0x%08X\n",
-		(unsigned long)addr, dwc2_readl(addr));
+		(unsigned long)addr, dwc2_readl(hsotg, addr));
 	addr = hsotg->regs + HFNUM;
 	dev_dbg(hsotg->dev, "HFNUM	 @0x%08lX : 0x%08X\n",
-		(unsigned long)addr, dwc2_readl(addr));
+		(unsigned long)addr, dwc2_readl(hsotg, addr));
 	addr = hsotg->regs + HPTXSTS;
 	dev_dbg(hsotg->dev, "HPTXSTS	 @0x%08lX : 0x%08X\n",
-		(unsigned long)addr, dwc2_readl(addr));
+		(unsigned long)addr, dwc2_readl(hsotg, addr));
 	addr = hsotg->regs + HAINT;
 	dev_dbg(hsotg->dev, "HAINT	 @0x%08lX : 0x%08X\n",
-		(unsigned long)addr, dwc2_readl(addr));
+		(unsigned long)addr, dwc2_readl(hsotg, addr));
 	addr = hsotg->regs + HAINTMSK;
 	dev_dbg(hsotg->dev, "HAINTMSK	 @0x%08lX : 0x%08X\n",
-		(unsigned long)addr, dwc2_readl(addr));
+		(unsigned long)addr, dwc2_readl(hsotg, addr));
 	if (hsotg->core_params->dma_desc_enable > 0) {
 		addr = hsotg->regs + HFLBADDR;
 		dev_dbg(hsotg->dev, "HFLBADDR @0x%08lX : 0x%08X\n",
-			(unsigned long)addr, dwc2_readl(addr));
+			(unsigned long)addr, dwc2_readl(hsotg, addr));
 	}
 
 	addr = hsotg->regs + HPRT0;
 	dev_dbg(hsotg->dev, "HPRT0	 @0x%08lX : 0x%08X\n",
-		(unsigned long)addr, dwc2_readl(addr));
+		(unsigned long)addr, dwc2_readl(hsotg, addr));
 
 	for (i = 0; i < hsotg->core_params->host_channels; i++) {
 		dev_dbg(hsotg->dev, "Host Channel %d Specific Registers\n", i);
 		addr = hsotg->regs + HCCHAR(i);
 		dev_dbg(hsotg->dev, "HCCHAR	 @0x%08lX : 0x%08X\n",
-			(unsigned long)addr, dwc2_readl(addr));
+			(unsigned long)addr, dwc2_readl(hsotg, addr));
 		addr = hsotg->regs + HCSPLT(i);
 		dev_dbg(hsotg->dev, "HCSPLT	 @0x%08lX : 0x%08X\n",
-			(unsigned long)addr, dwc2_readl(addr));
+			(unsigned long)addr, dwc2_readl(hsotg, addr));
 		addr = hsotg->regs + HCINT(i);
 		dev_dbg(hsotg->dev, "HCINT	 @0x%08lX : 0x%08X\n",
-			(unsigned long)addr, dwc2_readl(addr));
+			(unsigned long)addr, dwc2_readl(hsotg, addr));
 		addr = hsotg->regs + HCINTMSK(i);
 		dev_dbg(hsotg->dev, "HCINTMSK	 @0x%08lX : 0x%08X\n",
-			(unsigned long)addr, dwc2_readl(addr));
+			(unsigned long)addr, dwc2_readl(hsotg, addr));
 		addr = hsotg->regs + HCTSIZ(i);
 		dev_dbg(hsotg->dev, "HCTSIZ	 @0x%08lX : 0x%08X\n",
-			(unsigned long)addr, dwc2_readl(addr));
+			(unsigned long)addr, dwc2_readl(hsotg, addr));
 		addr = hsotg->regs + HCDMA(i);
 		dev_dbg(hsotg->dev, "HCDMA	 @0x%08lX : 0x%08X\n",
-			(unsigned long)addr, dwc2_readl(addr));
+			(unsigned long)addr, dwc2_readl(hsotg, addr));
 		if (hsotg->core_params->dma_desc_enable > 0) {
 			addr = hsotg->regs + HCDMAB(i);
 			dev_dbg(hsotg->dev, "HCDMAB	 @0x%08lX : 0x%08X\n",
-				(unsigned long)addr, dwc2_readl(addr));
+				(unsigned long)addr, dwc2_readl(hsotg, addr));
 		}
 	}
 #endif
@@ -495,80 +495,80 @@ void dwc2_dump_global_registers(struct dwc2_hsotg *hsotg)
 	dev_dbg(hsotg->dev, "Core Global Registers\n");
 	addr = hsotg->regs + GOTGCTL;
 	dev_dbg(hsotg->dev, "GOTGCTL	 @0x%08lX : 0x%08X\n",
-		(unsigned long)addr, dwc2_readl(addr));
+		(unsigned long)addr, dwc2_readl(hsotg, addr));
 	addr = hsotg->regs + GOTGINT;
 	dev_dbg(hsotg->dev, "GOTGINT	 @0x%08lX : 0x%08X\n",
-		(unsigned long)addr, dwc2_readl(addr));
+		(unsigned long)addr, dwc2_readl(hsotg, addr));
 	addr = hsotg->regs + GAHBCFG;
 	dev_dbg(hsotg->dev, "GAHBCFG	 @0x%08lX : 0x%08X\n",
-		(unsigned long)addr, dwc2_readl(addr));
+		(unsigned long)addr, dwc2_readl(hsotg, addr));
 	addr = hsotg->regs + GUSBCFG;
 	dev_dbg(hsotg->dev, "GUSBCFG	 @0x%08lX : 0x%08X\n",
-		(unsigned long)addr, dwc2_readl(addr));
+		(unsigned long)addr, dwc2_readl(hsotg, addr));
 	addr = hsotg->regs + GRSTCTL;
 	dev_dbg(hsotg->dev, "GRSTCTL	 @0x%08lX : 0x%08X\n",
-		(unsigned long)addr, dwc2_readl(addr));
+		(unsigned long)addr, dwc2_readl(hsotg, addr));
 	addr = hsotg->regs + GINTSTS;
 	dev_dbg(hsotg->dev, "GINTSTS	 @0x%08lX : 0x%08X\n",
-		(unsigned long)addr, dwc2_readl(addr));
+		(unsigned long)addr, dwc2_readl(hsotg, addr));
 	addr = hsotg->regs + GINTMSK;
 	dev_dbg(hsotg->dev, "GINTMSK	 @0x%08lX : 0x%08X\n",
-		(unsigned long)addr, dwc2_readl(addr));
+		(unsigned long)addr, dwc2_readl(hsotg, addr));
 	addr = hsotg->regs + GRXSTSR;
 	dev_dbg(hsotg->dev, "GRXSTSR	 @0x%08lX : 0x%08X\n",
-		(unsigned long)addr, dwc2_readl(addr));
+		(unsigned long)addr, dwc2_readl(hsotg, addr));
 	addr = hsotg->regs + GRXFSIZ;
 	dev_dbg(hsotg->dev, "GRXFSIZ	 @0x%08lX : 0x%08X\n",
-		(unsigned long)addr, dwc2_readl(addr));
+		(unsigned long)addr, dwc2_readl(hsotg, addr));
 	addr = hsotg->regs + GNPTXFSIZ;
 	dev_dbg(hsotg->dev, "GNPTXFSIZ	 @0x%08lX : 0x%08X\n",
-		(unsigned long)addr, dwc2_readl(addr));
+		(unsigned long)addr, dwc2_readl(hsotg, addr));
 	addr = hsotg->regs + GNPTXSTS;
 	dev_dbg(hsotg->dev, "GNPTXSTS	 @0x%08lX : 0x%08X\n",
-		(unsigned long)addr, dwc2_readl(addr));
+		(unsigned long)addr, dwc2_readl(hsotg, addr));
 	addr = hsotg->regs + GI2CCTL;
 	dev_dbg(hsotg->dev, "GI2CCTL	 @0x%08lX : 0x%08X\n",
-		(unsigned long)addr, dwc2_readl(addr));
+		(unsigned long)addr, dwc2_readl(hsotg, addr));
 	addr = hsotg->regs + GPVNDCTL;
 	dev_dbg(hsotg->dev, "GPVNDCTL	 @0x%08lX : 0x%08X\n",
-		(unsigned long)addr, dwc2_readl(addr));
+		(unsigned long)addr, dwc2_readl(hsotg, addr));
 	addr = hsotg->regs + GGPIO;
 	dev_dbg(hsotg->dev, "GGPIO	 @0x%08lX : 0x%08X\n",
-		(unsigned long)addr, dwc2_readl(addr));
+		(unsigned long)addr, dwc2_readl(hsotg, addr));
 	addr = hsotg->regs + GUID;
 	dev_dbg(hsotg->dev, "GUID	 @0x%08lX : 0x%08X\n",
-		(unsigned long)addr, dwc2_readl(addr));
+		(unsigned long)addr, dwc2_readl(hsotg, addr));
 	addr = hsotg->regs + GSNPSID;
 	dev_dbg(hsotg->dev, "GSNPSID	 @0x%08lX : 0x%08X\n",
-		(unsigned long)addr, dwc2_readl(addr));
+		(unsigned long)addr, dwc2_readl(hsotg, addr));
 	addr = hsotg->regs + GHWCFG1;
 	dev_dbg(hsotg->dev, "GHWCFG1	 @0x%08lX : 0x%08X\n",
-		(unsigned long)addr, dwc2_readl(addr));
+		(unsigned long)addr, dwc2_readl(hsotg, addr));
 	addr = hsotg->regs + GHWCFG2;
 	dev_dbg(hsotg->dev, "GHWCFG2	 @0x%08lX : 0x%08X\n",
-		(unsigned long)addr, dwc2_readl(addr));
+		(unsigned long)addr, dwc2_readl(hsotg, addr));
 	addr = hsotg->regs + GHWCFG3;
 	dev_dbg(hsotg->dev, "GHWCFG3	 @0x%08lX : 0x%08X\n",
-		(unsigned long)addr, dwc2_readl(addr));
+		(unsigned long)addr, dwc2_readl(hsotg, addr));
 	addr = hsotg->regs + GHWCFG4;
 	dev_dbg(hsotg->dev, "GHWCFG4	 @0x%08lX : 0x%08X\n",
-		(unsigned long)addr, dwc2_readl(addr));
+		(unsigned long)addr, dwc2_readl(hsotg, addr));
 	addr = hsotg->regs + GLPMCFG;
 	dev_dbg(hsotg->dev, "GLPMCFG	 @0x%08lX : 0x%08X\n",
-		(unsigned long)addr, dwc2_readl(addr));
+		(unsigned long)addr, dwc2_readl(hsotg, addr));
 	addr = hsotg->regs + GPWRDN;
 	dev_dbg(hsotg->dev, "GPWRDN	 @0x%08lX : 0x%08X\n",
-		(unsigned long)addr, dwc2_readl(addr));
+		(unsigned long)addr, dwc2_readl(hsotg, addr));
 	addr = hsotg->regs + GDFIFOCFG;
 	dev_dbg(hsotg->dev, "GDFIFOCFG	 @0x%08lX : 0x%08X\n",
-		(unsigned long)addr, dwc2_readl(addr));
+		(unsigned long)addr, dwc2_readl(hsotg, addr));
 	addr = hsotg->regs + HPTXFSIZ;
 	dev_dbg(hsotg->dev, "HPTXFSIZ	 @0x%08lX : 0x%08X\n",
-		(unsigned long)addr, dwc2_readl(addr));
+		(unsigned long)addr, dwc2_readl(hsotg, addr));
 
 	addr = hsotg->regs + PCGCTL;
 	dev_dbg(hsotg->dev, "PCGCTL	 @0x%08lX : 0x%08X\n",
-		(unsigned long)addr, dwc2_readl(addr));
+		(unsigned long)addr, dwc2_readl(hsotg, addr));
 #endif
 }
 
@@ -587,15 +587,15 @@ void dwc2_flush_tx_fifo(struct dwc2_hsotg *hsotg, const int num)
 
 	greset = GRSTCTL_TXFFLSH;
 	greset |= num << GRSTCTL_TXFNUM_SHIFT & GRSTCTL_TXFNUM_MASK;
-	dwc2_writel(greset, hsotg->regs + GRSTCTL);
+	dwc2_writel(hsotg, greset, hsotg->regs + GRSTCTL);
 
 	do {
-		greset = dwc2_readl(hsotg->regs + GRSTCTL);
+		greset = dwc2_readl(hsotg, hsotg->regs + GRSTCTL);
 		if (++count > 10000) {
 			dev_warn(hsotg->dev,
 				 "%s() HANG! GRSTCTL=%0x GNPTXSTS=0x%08x\n",
 				 __func__, greset,
-				 dwc2_readl(hsotg->regs + GNPTXSTS));
+				 dwc2_readl(hsotg, hsotg->regs + GNPTXSTS));
 			break;
 		}
 		udelay(1);
@@ -618,10 +618,10 @@ void dwc2_flush_rx_fifo(struct dwc2_hsotg *hsotg)
 	dev_vdbg(hsotg->dev, "%s()\n", __func__);
 
 	greset = GRSTCTL_RXFFLSH;
-	dwc2_writel(greset, hsotg->regs + GRSTCTL);
+	dwc2_writel(hsotg, greset, hsotg->regs + GRSTCTL);
 
 	do {
-		greset = dwc2_readl(hsotg->regs + GRSTCTL);
+		greset = dwc2_readl(hsotg, hsotg->regs + GRSTCTL);
 		if (++count > 10000) {
 			dev_warn(hsotg->dev, "%s() HANG! GRSTCTL=%0x\n",
 				 __func__, greset);
@@ -1358,8 +1358,8 @@ static void dwc2_get_host_hwparams(struct dwc2_hsotg *hsotg)
 
 	forced = dwc2_force_mode_if_needed(hsotg, true);
 
-	gnptxfsiz = dwc2_readl(hsotg->regs + GNPTXFSIZ);
-	hptxfsiz = dwc2_readl(hsotg->regs + HPTXFSIZ);
+	gnptxfsiz = dwc2_readl(hsotg, hsotg->regs + GNPTXFSIZ);
+	hptxfsiz = dwc2_readl(hsotg, hsotg->regs + HPTXFSIZ);
 	dev_dbg(hsotg->dev, "gnptxfsiz=%08x\n", gnptxfsiz);
 	dev_dbg(hsotg->dev, "hptxfsiz=%08x\n", hptxfsiz);
 
@@ -1388,7 +1388,7 @@ static void dwc2_get_dev_hwparams(struct dwc2_hsotg *hsotg)
 
 	forced = dwc2_force_mode_if_needed(hsotg, false);
 
-	gnptxfsiz = dwc2_readl(hsotg->regs + GNPTXFSIZ);
+	gnptxfsiz = dwc2_readl(hsotg, hsotg->regs + GNPTXFSIZ);
 	dev_dbg(hsotg->dev, "gnptxfsiz=%08x\n", gnptxfsiz);
 
 	if (forced)
@@ -1398,6 +1398,15 @@ static void dwc2_get_dev_hwparams(struct dwc2_hsotg *hsotg)
 				       FIFOSIZE_DEPTH_SHIFT;
 }
 
+void dwc2_detect_endiannes(struct dwc2_hsotg *hsotg)
+{
+	u32 sid;
+
+	sid = __raw_readl(hsotg->regs + GSNPSID);
+	if ((le32_to_cpu(sid) & 0xffff0000) == 0x4f540000)
+		hsotg->is_little_endian = 1;
+}
+
 /**
  * During device initialization, read various hardware configuration
  * registers and interpret the contents.
@@ -1412,10 +1421,10 @@ int dwc2_get_hwparams(struct dwc2_hsotg *hsotg)
 	/*
 	 * Attempt to ensure this device is really a DWC_otg Controller.
 	 * Read and verify the GSNPSID register contents. The value should be
-	 * 0x45f42xxx or 0x45f43xxx, which corresponds to either "OT2" or "OT3",
+	 * 0x4f542xxx or 0x4f543xxx, which corresponds to either "OT2" or "OT3",
 	 * as in "OTG version 2.xx" or "OTG version 3.xx".
 	 */
-	hw->snpsid = dwc2_readl(hsotg->regs + GSNPSID);
+	hw->snpsid = dwc2_readl(hsotg, hsotg->regs + GSNPSID);
 	if ((hw->snpsid & 0xfffff000) != 0x4f542000 &&
 	    (hw->snpsid & 0xfffff000) != 0x4f543000) {
 		dev_err(hsotg->dev, "Bad value for GSNPSID: 0x%08x\n",
@@ -1427,11 +1436,11 @@ int dwc2_get_hwparams(struct dwc2_hsotg *hsotg)
 		hw->snpsid >> 12 & 0xf, hw->snpsid >> 8 & 0xf,
 		hw->snpsid >> 4 & 0xf, hw->snpsid & 0xf, hw->snpsid);
 
-	hwcfg1 = dwc2_readl(hsotg->regs + GHWCFG1);
-	hwcfg2 = dwc2_readl(hsotg->regs + GHWCFG2);
-	hwcfg3 = dwc2_readl(hsotg->regs + GHWCFG3);
-	hwcfg4 = dwc2_readl(hsotg->regs + GHWCFG4);
-	grxfsiz = dwc2_readl(hsotg->regs + GRXFSIZ);
+	hwcfg1 = dwc2_readl(hsotg, hsotg->regs + GHWCFG1);
+	hwcfg2 = dwc2_readl(hsotg, hsotg->regs + GHWCFG2);
+	hwcfg3 = dwc2_readl(hsotg, hsotg->regs + GHWCFG3);
+	hwcfg4 = dwc2_readl(hsotg, hsotg->regs + GHWCFG4);
+	grxfsiz = dwc2_readl(hsotg, hsotg->regs + GRXFSIZ);
 
 	dev_dbg(hsotg->dev, "hwcfg1=%08x\n", hwcfg1);
 	dev_dbg(hsotg->dev, "hwcfg2=%08x\n", hwcfg2);
@@ -1571,7 +1580,7 @@ u16 dwc2_get_otg_version(struct dwc2_hsotg *hsotg)
 
 bool dwc2_is_controller_alive(struct dwc2_hsotg *hsotg)
 {
-	if (dwc2_readl(hsotg->regs + GSNPSID) == 0xffffffff)
+	if (dwc2_readl(hsotg, hsotg->regs + GSNPSID) == 0xffffffff)
 		return false;
 	else
 		return true;
@@ -1585,10 +1594,10 @@ bool dwc2_is_controller_alive(struct dwc2_hsotg *hsotg)
  */
 void dwc2_enable_global_interrupts(struct dwc2_hsotg *hsotg)
 {
-	u32 ahbcfg = dwc2_readl(hsotg->regs + GAHBCFG);
+	u32 ahbcfg = dwc2_readl(hsotg, hsotg->regs + GAHBCFG);
 
 	ahbcfg |= GAHBCFG_GLBL_INTR_EN;
-	dwc2_writel(ahbcfg, hsotg->regs + GAHBCFG);
+	dwc2_writel(hsotg, ahbcfg, hsotg->regs + GAHBCFG);
 }
 
 /**
@@ -1599,16 +1608,16 @@ void dwc2_enable_global_interrupts(struct dwc2_hsotg *hsotg)
  */
 void dwc2_disable_global_interrupts(struct dwc2_hsotg *hsotg)
 {
-	u32 ahbcfg = dwc2_readl(hsotg->regs + GAHBCFG);
+	u32 ahbcfg = dwc2_readl(hsotg, hsotg->regs + GAHBCFG);
 
 	ahbcfg &= ~GAHBCFG_GLBL_INTR_EN;
-	dwc2_writel(ahbcfg, hsotg->regs + GAHBCFG);
+	dwc2_writel(hsotg, ahbcfg, hsotg->regs + GAHBCFG);
 }
 
 /* Returns the controller's GHWCFG2.OTG_MODE. */
 unsigned dwc2_op_mode(struct dwc2_hsotg *hsotg)
 {
-	u32 ghwcfg2 = dwc2_readl(hsotg->regs + GHWCFG2);
+	u32 ghwcfg2 = dwc2_readl(hsotg, hsotg->regs + GHWCFG2);
 
 	return (ghwcfg2 & GHWCFG2_OP_MODE_MASK) >>
 		GHWCFG2_OP_MODE_SHIFT;
drivers/usb/dwc2/core.h +34 -29

96 lines not on this page · raw message

drivers/usb/dwc2/core_intr.c +39 -35

217 lines not on this page · raw message

drivers/usb/dwc2/debugfs.c +27 -26

108 lines not on this page · raw message

drivers/usb/dwc2/gadget.c +207 -174

1,064 lines not on this page · raw message

drivers/usb/dwc2/hcd.c +194 -187

1,057 lines not on this page · raw message

drivers/usb/dwc2/hcd.h +6 -6
index 7758bfb..c48bbee 100644

Lines of this file are not shown. View the raw message.

drivers/usb/dwc2/hcd_ddma.c +5 -5
index 0e1d42b..93261b1 100644

Lines of this file are not shown. View the raw message.

drivers/usb/dwc2/hcd_intr.c +49 -46

95 lines not on this page · raw message

drivers/usb/dwc2/hcd_queue.c +5 -5
index b5c7793..c4c6e84 100644

Lines of this file are not shown. View the raw message.

drivers/usb/dwc2/platform.c +2 -0
index fc6f525..7050f2d 100644

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

Part of the diff is not shown here. View the raw message.

#8 lore raw
Quoted 56 lines · On Monday 09 May 2016 21:06:07 Christian Lamparter wrote:

Uh, Thanks for the participation!

Quoted 34 lines · Arnd Bergmann
Quoted 18 lines · On Monday 09 May 2016 13:39:50 Felipe Balbi wrote:
Quoted 17 lines · Arnd Bergmann
Quoted 1 line · On Monday 09 May 2016 10:23:22 Benjamin Herrenschmidt wrote:

On Sun, 2016-05-08 at 13:44 +0200, Christian Lamparter wrote:

The patch that caused the problem had multiple issues:

- it broke big-endian ARM kernels: any machine that was working correctly with a little-endian kernel is no longer using byteswaps on big-endian kernels, which clearly breaks them.
- On PowerPC the same thing must be true: if it was working before, using big-endian kernels is now broken. Unlike ARM, 32-bit PowerPC usually uses big-endian kernels, so they are likely all broken.
- The barrier for dwc2_writel is on the wrong side of the __raw_writel(), so the MMIO no longer synchronizes with DMA operations.
- On architectures that require specific CPU instructions for MMIO access, using the __raw_ variant may turn this into a pointer dereference that does not have the same effect as the readl/writel.

Quoted 7 lines
Quoted 3 lines

I think we can simply make this set of accessors architecture-dependent (MIPS vs. the rest of the world) to revert ARM and PowerPC back to the working version.

and patch all drivers similarly? Shouldn't arch/mips itself deal with it and hide it from drivers ?

Unfortunately, I don't see any way this could be done in MIPS specific
code: There is typically a byteswap between the internal bus and the PCI bus on big-endian MIPS systems, so the PCI MMIO ends up being little-endian, which matches the expected behavior of readl/writel. However, drivers for non-PCI devices often use the same readl/writel accessors because that is how it's done on ARMv6/ARMv7.

Quoted 7 lines

Doing it hardcoded by architecture is just the simplest way to deal with it, working on the assumption that nothing actually needs the runtime detection that Ben suggested. Detecting the endianess of the device is probably the best future-proof solution, but it's also considerably more work to do in the driver, and comes with a tiny runtime overhead.

Ok, just to have it on the table. I went ahead and implemented the "Detect Endian".

I looked in the DWC USB OTG's Databook documents v3.30a (If someone wants them too, PM me). If I read the Application Interface Feature list on page 30 correctly. The endianess is selectable by a "pin".... That said I don't know which one is it in the APM82181 or any other arch. I looked around for configuration registers and stuff but unlike DesignWare's AHB DMA Controller, there's no Bit in the "User HW Config Registers" that would tell us if it was configured as big-endian or little-endian at the moment.

Quoted 5 lines

One way out would be to detect the endianess automatically by looking at the values in the GSNPSID register. This is a read-only register containing the release number of the core being used. The "upper" 16-bits of it are hardcoded to 0x4f45 (The comment in dwc2_get_hwparams [1] has it backwards but not the code below).

Good, that should work.

Quoted 9 lines

I ran into the following issues:
- gadget.c uses ioread32_rep [0] & iowrite32_rep [1]. This is interesting because both of these functions actually use the __raw_io* on powerpc. This is because powerpc uses the default defines of include/asm-generic/io.h [2].

Ideally, this should be done by sth like a writesl_be or writesl(e) function. But I found none so for now: Let's make a ugly hack:
to_correct_endian that will work for testing, but will be replaced.

You must have gotten this wrong: writesl() and the other variants are used to copy byte streams, which are always in the correct endianess, i.e. copying them one byte at a time should result in the same in-memory representation as copying them four bytes at a time. You should never need an additional swap in here.

Quoted 4 lines

- is_little_endian (do we want a separate is_big_endian?) Also, do we want to be able to overwrite the detection code if the endian setting was set in the device tree?. For now it always does auto detection (see dwc2_detect_endiannes() ).

I'd say that detecting endianess from the register is the safest approach. We can also parse the standard DT properties to do the same, but they can easily be stale, especially when the driver has already gotten this wrong for some users.

Quoted 6 lines

( - 80 character per line issues, is it possible to drop the
hsotg->reg + REGISTER from the dwc2_readl/writel since we pass the hsotg now anyway and do the reg + REGISTER calculation in the accessor? I played around with macros as most functions calling the accessors have the hsotg variable anyway )

I would definitely recommend doing this. If necessary there can be an extra set of functions, but this is what a lot of drivers do when you pass the device structure to an MMIO accessor.

drivers/usb/dwc2/core.c static void dwc2_get_dev_hwparams(struct dwc2_hsotg *hsotg) · line 1408

Quoted 12 lines

[0] <http://lxr.free-electrons.com/source/drivers/usb/dwc2/gadget.c#L1488>
[1] <http://lxr.free-electrons.com/source/drivers/usb/dwc2/gadget.c#L462>
[2] <http://lxr.free-electrons.com/source/include/asm-generic/io.h#L315>

drivers/usb/dwc2/core.c +8 -0
+void dwc2_detect_endiannes(struct dwc2_hsotg *hsotg)
+{
+	u32 sid;
+
+	sid = __raw_readl(hsotg->regs + GSNPSID);
+	if ((le32_to_cpu(sid) & 0xffff0000) == 0x4f540000)
+		hsotg->is_little_endian = 1;
+}

__raw_readl() might not do what you want here, I'd always use readl() for the detection without the extra le32_to_cpu().

Quoted 15 lines
drivers/usb/dwc2/core.h +15 -0
+static inline u32 dwc2_readl(struct dwc2_hsotg *hsotg,
+			     void __iomem *addr)
+{
+	u32 value;
+
+	if (hsotg->is_little_endian)
+		value = ioread32(addr);
+	else
+		value = ioread32be(addr);
+
+	return value;
+}
+
+static inline void dwc2_writel(struct dwc2_hsotg *hsotg,
+			       u32 value, void __iomem *addr)

drivers/usb/dwc2/core.h enum dwc2_halt_status { · line 985

Quoted 10 lines
drivers/usb/dwc2/core.h +10 -0
+{
+	if (hsotg->is_little_endian)
+		iowrite32(value, addr);
+	else
+		iowrite32be(value, addr);
+
+#ifdef DWC2_LOG_WRITES
+	pr_info("INFO:: wrote %08x to %p\n", value, addr);
+#endif
+}

In terms of micro-optimizing this, it may be better to use readl/writel instead of ioread32/iowrite32: On most architectures they are the same, but notably on x86, ioread32/iowrite32 is slightly slower because of the indirection.

We probably only need to worry about the big-endian registers on architectures that have an efficient ioread32be/iowrite32be.

Quoted 8 lines
drivers/usb/dwc2/gadget.c +4 -0
@@ -292,6 +294,19 @@ static void dwc2_hsotg_unmap_dma(struct dwc2_hsotg *hsotg,
 	usb_gadget_unmap_request(&hsotg->gadget, req, hs_ep->dir_in);
 }
 
+static void to_correct_endian(struct dwc2_hsotg *hsotg, u32 *data, size_t len)
+{
+	size_t i;
+

drivers/usb/dwc2/gadget.c static void dwc2_hsotg_unmap_dma(struct dwc2_hsotg *hsotg, · line 310

Quoted 10 lines
drivers/usb/dwc2/gadget.c +9 -0
+	if (hsotg->is_little_endian) {
+		for (i = 0; i < len; i++)
+			data[i] = cpu_to_le32(data[i]);
+	} else {
+		for (i = 0; i < len; i++)
+			data[i] = cpu_to_be32(data[i]);
+	}
+}
+
 /**

My guess is that this leaves the big-endian powerpc machines broken, you already have the correct byte stream using readsl/writesl or ioread32_rep/iowrite32_rep.

If there is any location in the code that accesses the FIFO registers readl/writel or a loop of those, that code should be changed to use readsl/writesl so you don't have to double-swap the data.

Arnd

#9 lore raw
In reply to Christian Lamparter
Quoted 10 lines · Christian Lamparter via Linuxppc-dev

I ran into the following issues:
        - gadget.c uses ioread32_rep [0] & iowrite32_rep [1]. This is interesting because both of these functions actually use the __raw_io* on powerpc. This is because powerpc uses the default defines of include/asm-generic/io.h [2].

          Ideally, this should be done by sth like a writesl_be or writesl(e) function. But I found none so for now: Let's make a ugly hack:
          to_correct_endian that will work for testing, but will be replaced.

No. The _rep variants always must use native endian. If for some reason your device requires something different then the device is more broken than I thought. IE. even with a BE core and an LE device, we use non- byeswap _rep versions, this isn't just because we use "defaults" on powerpc for example, it's necessary to work.

Here too, I could suggest you google for my talk on the subject :-) But if you think closely about it, you'll figure out that FIFOs don't have an endian per-se, thus what matters is which byte is first in ascending address order, and that byte must land in memory in the first address as well.

Interpreting the resulting data might require endian swaps, but actually transferring to/from the fifo doesn't.

Quoted 9 lines

   - dwc2_readl, dwc2_writel. I have no insight in the craziness that's going on with MIPS and the memory barrier. But it got me thinking rather than CONFIG_MIPS, isn't there a umbrella "ARCH_HAS_NEED_MEMORY_BARRIER_FOR_MMIO" config symbol?

        - is_little_endian (do we want a separate is_big_endian?) Also, do we want to be able to overwrite the detection code if the endian setting was set in the device tree?. For now it always does auto detection (see dwc2_detect_endiannes() ).

If auto-detect always work, no need to bother with the device-tree. A single flag is enough, either is_big or is_little, doesn't matter.

"readl" should always be little, readl_be should always be big, though the latter isn't supported on all archs. However the new accessors in iomap.h should provide a "be" variant on all archs so switching to these might be a good idea.

Quoted 6 lines

        ( - 80 character per line issues, is it possible to drop the hsotg->reg + REGISTER from the dwc2_readl/writel since we pass the hsotg now anyway and do the reg + REGISTER calculation in the accessor? I played around with macros as most functions calling the accessors have the hsotg variable anyway )

Or just use a temp variable, the compiler should deal with it the same way.

Ben.

Quoted 2 lines

Regards,
Christian

#10 lore raw
In reply to Arnd Bergmann
Quoted 4 lines · Arnd Bergmann

Unfortunately, I don't see any way this could be done in MIPS specific
code: There is typically a byteswap between the internal bus and the PCI bus on big-endian MIPS systems, so the PCI MMIO ends up being little-endian,

Ugh ... not exactly, re-watch my talk on the matter :-) While there is a specific lane wiring to preserve byte addresss, in the end it's the end device itself that is either BE or LE. Regardless of any "bus endianness".

Quoted 3 lines

which matches the expected behavior of readl/writel. However, drivers for non-PCI devices often use the same readl/writel accessors because that is how it's done on ARMv6/ARMv7.

Even then, you can have on-SoC (non-PCI) devices that also have a different endianness from the main CPU. How does it work on ARM for example ? The device endianness should be fixed, regardless of the endianness of the core, no ?

Quoted 3 lines

Doing it hardcoded by architecture is just the simplest way to deal with it, working on the assumption that nothing actually needs the runtime detection that Ben suggested.

No, it's not an archicture problem. It's a problem specific to that one SoC that the device was synthetized to be a certain endian while it was synthetized differently on another SoC... that also happens to be a different architecture. But doesn't have to.

For example, we had in the past cases of both LE and BE EHCI implementations on the same architecture (PowerPC).

Quoted 4 lines

Detecting the endianess of the
device is probably the best future-proof solution, but it's also considerably more work to do in the driver, and comes with a tiny runtime overhead.

The runtime overhead is probably non-measurable compared with the cost of the actual MMIOs.

Cheers,
Ben.

#11 lore raw
Quoted 10 lines · On Tuesday 10 May 2016 08:37:52 Benjamin Herrenschmidt wrote:
Quoted 4 lines · Arnd Bergmann

Unfortunately, I don't see any way this could be done in MIPS specific
code: There is typically a byteswap between the internal bus and the PCI bus on big-endian MIPS systems, so the PCI MMIO ends up being little-endian,

Ugh ... not exactly, re-watch my talk on the matter :-) While there is a specific lane wiring to preserve byte addresss, in the end it's the end device itself that is either BE or LE. Regardless of any "bus endianness".

I found your slides on

http://www.linuxplumbersconf.org/2012/wp-content/uploads/2012/09/2012-lpc-ref-big-little-endian-herrenschmidt.odp

but there are at least two more twists that you completely missed here:

- Some architectures (e.g. ARMv5 "BE32" mode in IXP4xx, surely some others) do not implement big-endian mode by wiring up the data lines between the bus and the CPU differently between big- and little-endian mode like powerpc and armv7 "BE8" do, but instead they swizzle the *address* lines on 8-bit and 16-bit addresses. The effect of that is that normal RAM accesses work as expected both ways, and devices that are accessed using 32-bit MMIO ops never need any byteswap (you actually get "native endian") while MMIO with 8 and 16 bit width does something completely unexpected and touches the wrong register. Having an explicit byteswap on the PCI host bridge gets you the expected addresses again for 8-bit cycles but it also means that readl()/writel() again need to swap the data.

- Some other architectures (e.g. Broadcom MIPS) apparently are even fancier and use a strapping pin on the SoC flips the endianess of the CPU core at the same time as all the peripheral MMIO registers, with the intention of never requiring any byte swaps. I believe they are implemented careful enough to actually get this right, but it confuses the heck out of Linux drivers that don't expect this.

Quoted 8 lines
Quoted 3 lines

which matches the expected behavior of readl/writel. However, drivers for non-PCI devices often use the same readl/writel accessors because that is how it's done on ARMv6/ARMv7.

Even then, you can have on-SoC (non-PCI) devices that also have a different endianness from the main CPU. How does it work on ARM for example ? The device endianness should be fixed, regardless of the endianness of the core, no ?

ARMv6/v7 is uses BE8 mode like powerpc: each peripheral is fixed-endian and you have to know what it is. Only Freescale managed to put identical IP blocks on various (powerpc-derived) SoCs and have a subset of them treat the access as little-endian while others remain big-endian, so all those drivers now require runtime detection.

Quoted 11 lines
Quoted 3 lines

Doing it hardcoded by architecture is just the simplest way to deal with it, working on the assumption that nothing actually needs the runtime detection that Ben suggested.

No, it's not an archicture problem. It's a problem specific to that one SoC that the device was synthetized to be a certain endian while it was synthetized differently on another SoC... that also happens to be a different architecture. But doesn't have to.

For example, we had in the past cases of both LE and BE EHCI implementations on the same architecture (PowerPC).

I understand this, but from what I see in this history of this particular driver, all ARM and PowerPC implementations chose to use LE registers for DWC2 because the normal approach for these is to not mess with endianess, while presumably all MIPS users of the same block wired up the endian-select line of the IP block to match that of the CPU core, again because it's what you are expected to do on a MIPS based SoC.

So hardcoding it per architecture would make an assumption based on the mindset of the SoC designers rather than strict technical differences, and that can fail as soon as someone does things differently on any of them (see the Freescale example), but I still think it's the easiest workaround for backporting to stable kernels. A revert of the original patch would be even easier, but that would break the one big-endian MIPS machine we know about.

Quoted 7 lines
Quoted 4 lines

Detecting the endianess of the
device is probably the best future-proof solution, but it's also considerably more work to do in the driver, and comes with a tiny runtime overhead.

The runtime overhead is probably non-measurable compared with the cost of the actual MMIOs.

Right. The code size increase is probably measurable (but still small), the runtime overhead is not.

Arnd

author Tested-by
#12 lore raw
Quoted 85 lines · Arnd Bergmann
Quoted 10 lines · On Tuesday 10 May 2016 08:37:52 Benjamin Herrenschmidt wrote:
Quoted 4 lines · Arnd Bergmann

Unfortunately, I don't see any way this could be done in MIPS specific
code: There is typically a byteswap between the internal bus and the PCI bus on big-endian MIPS systems, so the PCI MMIO ends up being little-endian,

Ugh ... not exactly, re-watch my talk on the matter :-) While there is a specific lane wiring to preserve byte addresss, in the end it's the end device itself that is either BE or LE. Regardless of any "bus endianness".

I found your slides on

http://www.linuxplumbersconf.org/2012/wp-content/uploads/2012/09/2012-lpc-ref-big-little-endian-herrenschmidt.odp

but there are at least two more twists that you completely missed here:

- Some architectures (e.g. ARMv5 "BE32" mode in IXP4xx, surely some others) do not implement big-endian mode by wiring up the data lines between the bus and the CPU differently between big- and little-endian mode like powerpc and armv7 "BE8" do, but instead they swizzle the *address* lines on 8-bit and 16-bit addresses. The effect of that is that normal RAM accesses work as expected both ways, and devices that are accessed using 32-bit MMIO ops never need any byteswap (you actually get "native endian") while MMIO with 8 and 16 bit width does something completely unexpected and touches the wrong register. Having an explicit byteswap on the PCI host bridge gets you the expected addresses again for 8-bit cycles but it also means that readl()/writel() again need to swap the data.

- Some other architectures (e.g. Broadcom MIPS) apparently are even fancier and use a strapping pin on the SoC flips the endianess of the CPU core at the same time as all the peripheral MMIO registers, with the intention of never requiring any byte swaps. I believe they are implemented careful enough to actually get this right, but it confuses the heck out of Linux drivers that don't expect this.

Quoted 8 lines
Quoted 3 lines

which matches the expected behavior of readl/writel. However, drivers for non-PCI devices often use the same readl/writel accessors because that is how it's done on ARMv6/ARMv7.

Even then, you can have on-SoC (non-PCI) devices that also have a different endianness from the main CPU. How does it work on ARM for example ? The device endianness should be fixed, regardless of the endianness of the core, no ?

ARMv6/v7 is uses BE8 mode like powerpc: each peripheral is fixed-endian and you have to know what it is. Only Freescale managed to put identical IP blocks on various (powerpc-derived) SoCs and have a subset of them treat the access as little-endian while others remain big-endian, so all those drivers now require runtime detection.

Quoted 11 lines
Quoted 3 lines

Doing it hardcoded by architecture is just the simplest way to deal with it, working on the assumption that nothing actually needs the runtime detection that Ben suggested.

No, it's not an archicture problem. It's a problem specific to that one SoC that the device was synthetized to be a certain endian while it was synthetized differently on another SoC... that also happens to be a different architecture. But doesn't have to.

For example, we had in the past cases of both LE and BE EHCI implementations on the same architecture (PowerPC).

I understand this, but from what I see in this history of this particular driver, all ARM and PowerPC implementations chose to use LE registers for DWC2 because the normal approach for these is to not mess with endianess, while presumably all MIPS users of the same block wired up the endian-select line of the IP block to match that of the CPU core, again because it's what you are expected to do on a MIPS based SoC.

So hardcoding it per architecture would make an assumption based on the mindset of the SoC designers rather than strict technical differences, and that can fail as soon as someone does things differently on any of them (see the Freescale example), but I still think it's the easiest workaround for backporting to stable kernels. A revert of the original patch would be even easier, but that would break the one big-endian MIPS machine we know about.

Quoted 5 lines
Quoted 4 lines

Detecting the endianess of the
device is probably the best future-proof solution, but it's also considerably more work to do in the driver, and comes with a tiny runtime overhead.

Quoted 5 lines
Quoted 2 lines

The runtime overhead is probably non-measurable compared with the cost of the actual MMIOs.

Right. The code size increase is probably measurable (but still small), the runtime overhead is not.

Ok, so no rebuts or complains have been posted.

I've tested the patch you made in: https://lkml.org/lkml/2016/5/9/354
and it works:

So, how do we go from here? There is are two small issues with the original patch (#ifdef DWC2_LOG_WRITES got converted to lower case:
#ifdef dwc2_log_writes) and I guess a proper subject would be nice.

Arnd, can you please respin and post it (cc'd stable as well)? So this is can be picked up? Or what's your plan?

Regards,
Christian

#13 lore raw
Quoted 16 lines · On Thursday 12 May 2016 11:58:18 Christian Lamparter wrote:
Quoted 10 lines
Quoted 7 lines
Quoted 4 lines

Detecting the endianess of the
device is probably the best future-proof solution, but it's also considerably more work to do in the driver, and comes with a tiny runtime overhead.

The runtime overhead is probably non-measurable compared with the cost of the actual MMIOs.

Right. The code size increase is probably measurable (but still small), the runtime overhead is not.

Ok, so no rebuts or complains have been posted.

I've tested the patch you made in: https://lkml.org/lkml/2016/5/9/354
and it works:

Quoted 8 lines

So, how do we go from here? There is are two small issues with the original patch (#ifdef DWC2_LOG_WRITES got converted to lower case:
#ifdef dwc2_log_writes) and I guess a proper subject would be nice.

Arnd, can you please respin and post it (cc'd stable as well)? So this is can be picked up? Or what's your plan?

(I just realized my reply was stuck in my outbox, so the patch went out first)

If I recall correctly, the rough consensus was to go with your longer patch in the future (fixed up for the comments that BenH and I sent), and I'd suggest basing it on top of a fixed version of my patch.

Felipe just had another idea, to change the endianess of the dwc2 block by setting a registers (if that exists). That would indeed be preferable, then we can just revert the broken change that went into 4.4 and backport that fix instead.

Arnd

#14 lore raw
Quoted 26 lines · Arnd Bergmann
Quoted 16 lines · On Thursday 12 May 2016 11:58:18 Christian Lamparter wrote:
Quoted 10 lines
Quoted 7 lines
Quoted 4 lines

Detecting the endianess of the
device is probably the best future-proof solution, but it's also considerably more work to do in the driver, and comes with a tiny runtime overhead.

The runtime overhead is probably non-measurable compared with the cost of the actual MMIOs.

Right. The code size increase is probably measurable (but still small), the runtime overhead is not.

Ok, so no rebuts or complains have been posted.

I've tested the patch you made in: https://lkml.org/lkml/2016/5/9/354
and it works:

Quoted 8 lines

So, how do we go from here? There is are two small issues with the original patch (#ifdef DWC2_LOG_WRITES got converted to lower case:
#ifdef dwc2_log_writes) and I guess a proper subject would be nice.

Arnd, can you please respin and post it (cc'd stable as well)? So this is can be picked up? Or what's your plan?

Quoted 7 lines

(I just realized my reply was stuck in my outbox, so the patch went out first)

If I recall correctly, the rough consensus was to go with your longer patch in the future (fixed up for the comments that BenH and I sent), and I'd suggest basing it on top of a fixed version of my patch.

Well, but it comes with the "overhead"! So this was just as I said:
"Let's look at it and see if it's any good"... And I think it isn't since the usb/host/ehci people also opted for #ifdef CONFIG_BIG_ENDIAN archs etc...

Quoted 4 lines

Felipe just had another idea, to change the endianess of the dwc2 block by setting a registers (if that exists). That would indeed be preferable, then we can just revert the broken change that went into 4.4 and backport that fix instead.

Just a quick reply. I have the docs for the thing. There's something like that in GAHBCFG at Bit 24... BUT it only switches the endiannes for the DMA descriptors (which is not always used, there are devices with PIO only)! It doesn't deal with the MMIO access at all.

The pin that would select which endian the device uses is probably connected to a DCR or GPIO but I don't know which or where so this is more or less useless. (Or the selectable endianness was dropped during synth).

Regards,
Christian

#15 lore raw
Quoted 30 lines · Christian Lamparter
Quoted 29 lines · Arnd Bergmann
Quoted 16 lines · On Thursday 12 May 2016 11:58:18 Christian Lamparter wrote:
Quoted 10 lines
Quoted 7 lines
Quoted 4 lines

Detecting the endianess of the
device is probably the best future-proof solution, but it's also considerably more work to do in the driver, and comes with a tiny runtime overhead.

The runtime overhead is probably non-measurable compared with the cost of the actual MMIOs.

Right. The code size increase is probably measurable (but still small), the runtime overhead is not.

Ok, so no rebuts or complains have been posted.

I've tested the patch you made in: https://lkml.org/lkml/2016/5/9/354
and it works:

Quoted 8 lines

So, how do we go from here? There is are two small issues with the original patch (#ifdef DWC2_LOG_WRITES got converted to lower case:
#ifdef dwc2_log_writes) and I guess a proper subject would be nice.

Arnd, can you please respin and post it (cc'd stable as well)? So this is can be picked up? Or what's your plan?

(I just realized my reply was stuck in my outbox, so the patch went out first)

Quoted 8 lines
Quoted 4 lines

If I recall correctly, the rough consensus was to go with your longer patch in the future (fixed up for the comments that BenH and I sent), and I'd suggest basing it on top of a fixed version of my patch.

Well, but it comes with the "overhead"! So this was just as I said:
"Let's look at it and see if it's any good"... And I think it isn't since the usb/host/ehci people also opted for #ifdef CONFIG_BIG_ENDIAN archs etc...

I slightly prefer the more general patch for future kernel versions. The overhead will probably be negligible, but we can perform some testing to make sure.

Can you resubmit with all gathered feedback?

Quoted 9 lines
Quoted 5 lines

Felipe just had another idea, to change the endianess of the dwc2 block by setting a registers (if that exists). That would indeed be preferable, then we can just revert the broken change that went into 4.4 and backport that fix instead.

Just a quick reply. I have the docs for the thing. There's something like that in GAHBCFG at Bit 24... BUT it only switches the endiannes for the DMA descriptors (which is not always used, there are devices with PIO only)! It doesn't deal with the MMIO access at all.

That's correct. It only affects descriptor endianness for DMA descriptor mode of operation.

Regards,
John

#16 lore raw
Quoted 40 lines · John Youn
Quoted 30 lines · Christian Lamparter
Quoted 29 lines · Arnd Bergmann
Quoted 16 lines · On Thursday 12 May 2016 11:58:18 Christian Lamparter wrote:
Quoted 10 lines
Quoted 7 lines
Quoted 4 lines

Detecting the endianess of the
device is probably the best future-proof solution, but it's also considerably more work to do in the driver, and comes with a tiny runtime overhead.

The runtime overhead is probably non-measurable compared with the cost of the actual MMIOs.

Right. The code size increase is probably measurable (but still small), the runtime overhead is not.

Ok, so no rebuts or complains have been posted.

I've tested the patch you made in: https://lkml.org/lkml/2016/5/9/354
and it works:

Quoted 8 lines

So, how do we go from here? There is are two small issues with the original patch (#ifdef DWC2_LOG_WRITES got converted to lower case:
#ifdef dwc2_log_writes) and I guess a proper subject would be nice.

Arnd, can you please respin and post it (cc'd stable as well)? So this is can be picked up? Or what's your plan?

(I just realized my reply was stuck in my outbox, so the patch went out first)

Quoted 8 lines
Quoted 4 lines

If I recall correctly, the rough consensus was to go with your longer patch in the future (fixed up for the comments that BenH and I sent), and I'd suggest basing it on top of a fixed version of my patch.

Well, but it comes with the "overhead"! So this was just as I said:
"Let's look at it and see if it's any good"... And I think it isn't since the usb/host/ehci people also opted for #ifdef CONFIG_BIG_ENDIAN archs etc...

Quoted 5 lines

I slightly prefer the more general patch for future kernel versions. The overhead will probably be negligible, but we can perform some testing to make sure.

Can you resubmit with all gathered feedback?

Yes I think I can do that. But I would really like to get the regression out of the way. So for that: I back Arnd's patch. It explains the problem much better and doesn't kill MIPS like the revert I was doing in my initial post to the MLs. Also, another bonus: his patch is suited to port to stable.

The auto-detection approach is not that easy to get right, given all the stuff that's going on with BE8, LE4, ... So can we have your "blessing" for Arnd's patch for now? since that way, I can base my patch on top of his work about the issues of endiannes? (Just say: ACK :) )

Arnd: do you have a version with the #ifdef lower/uppercase fix? Or should I give it a try (and fail in a different way ;) )

Quoted 11 lines
Quoted 8 lines
Quoted 4 lines

Felipe just had another idea, to change the endianess of the dwc2 block by setting a registers (if that exists). That would indeed be preferable, then we can just revert the broken change that went into 4.4 and backport that fix instead.

Just a quick reply. I have the docs for the thing. There's something like that in GAHBCFG at Bit 24... BUT it only switches the endiannes for the DMA descriptors (which is not always used, there are devices with PIO only)! It doesn't deal with the MMIO access at all.

That's correct. It only affects descriptor endianness for DMA descriptor mode of operation.

Ok. The funny thing is that for the MyBook Live Duo this setting might be important since the PLB_DMA engine is not part of the DWC library... Instead it's from IBM and operates in: Big Endian :-D.

Regards,
Christian

#17 lore raw
Quoted 24 lines · On Thursday 12 May 2016 22:39:36 Christian Lamparter wrote:
Quoted 12 lines · John Youn
Quoted 10 lines · Christian Lamparter
Quoted 5 lines · Arnd Bergmann

If I recall correctly, the rough consensus was to go with your longer patch in the future (fixed up for the comments that BenH and I sent), and I'd suggest basing it on top of a fixed version of my patch.

Well, but it comes with the "overhead"! So this was just as I said:
"Let's look at it and see if it's any good"... And I think it isn't since the usb/host/ehci people also opted for #ifdef CONFIG_BIG_ENDIAN archs etc...

Quoted 5 lines

I slightly prefer the more general patch for future kernel versions. The overhead will probably be negligible, but we can perform some testing to make sure.

Can you resubmit with all gathered feedback?

Yes I think I can do that. But I would really like to get the regression out of the way. So for that: I back Arnd's patch. It explains the problem much better and doesn't kill MIPS like the revert I was doing in my initial post to the MLs. Also, another bonus: his patch is suited to port to stable.

Quoted 8 lines

The auto-detection approach is not that easy to get right, given all the stuff that's going on with BE8, LE4, ... So can we have your "blessing" for Arnd's patch for now? since that way, I can base my patch on top of his work about the issues of endiannes? (Just say: ACK )

Arnd: do you have a version with the #ifdef lower/uppercase fix? Or should I give it a try (and fail in a different way )

I've already fixed it up locally, will send the latest version so it's out there, whether Felipe takes it or not.

Arnd

#18 lore raw
In reply to Christian Lamparter
Quoted 52 lines · Christian Lamparter
Quoted 40 lines · John Youn
Quoted 30 lines · Christian Lamparter
Quoted 29 lines · Arnd Bergmann
Quoted 16 lines · On Thursday 12 May 2016 11:58:18 Christian Lamparter wrote:
Quoted 10 lines
Quoted 7 lines
Quoted 4 lines

Detecting the endianess of the
device is probably the best future-proof solution, but it's also considerably more work to do in the driver, and comes with a tiny runtime overhead.

The runtime overhead is probably non-measurable compared with the cost of the actual MMIOs.

Right. The code size increase is probably measurable (but still small), the runtime overhead is not.

Ok, so no rebuts or complains have been posted.

I've tested the patch you made in: https://lkml.org/lkml/2016/5/9/354
and it works:

Quoted 8 lines

So, how do we go from here? There is are two small issues with the original patch (#ifdef DWC2_LOG_WRITES got converted to lower case:
#ifdef dwc2_log_writes) and I guess a proper subject would be nice.

Arnd, can you please respin and post it (cc'd stable as well)? So this is can be picked up? Or what's your plan?

(I just realized my reply was stuck in my outbox, so the patch went out first)

Quoted 8 lines
Quoted 4 lines

If I recall correctly, the rough consensus was to go with your longer patch in the future (fixed up for the comments that BenH and I sent), and I'd suggest basing it on top of a fixed version of my patch.

Well, but it comes with the "overhead"! So this was just as I said:
"Let's look at it and see if it's any good"... And I think it isn't since the usb/host/ehci people also opted for #ifdef CONFIG_BIG_ENDIAN archs etc...

Quoted 5 lines

I slightly prefer the more general patch for future kernel versions. The overhead will probably be negligible, but we can perform some testing to make sure.

Can you resubmit with all gathered feedback?

Yes I think I can do that. But I would really like to get the regression out of the way. So for that: I back Arnd's patch. It explains the problem much better and doesn't kill MIPS like the revert I was doing in my initial post to the MLs. Also, another bonus: his patch is suited to port to stable.

Quoted 6 lines

The auto-detection approach is not that easy to get right, given all the stuff that's going on with BE8, LE4, ... So can we have your "blessing" for Arnd's patch for now? since that way, I can base my patch on top of his work about the issues of endiannes? (Just say: ACK :) )

I agree Arnd's patch is best for stable. We can also apply it to mainline until we get the autodection working as well.

Unless Felipe has objections.

Quoted 12 lines

Arnd: do you have a version with the #ifdef lower/uppercase fix? Or should I give it a try (and fail in a different way ;) )

Quoted 9 lines
Quoted 8 lines
Quoted 4 lines

Felipe just had another idea, to change the endianess of the dwc2 block by setting a registers (if that exists). That would indeed be preferable, then we can just revert the broken change that went into 4.4 and backport that fix instead.

Just a quick reply. I have the docs for the thing. There's something like that in GAHBCFG at Bit 24... BUT it only switches the endiannes for the DMA descriptors (which is not always used, there are devices with PIO only)! It doesn't deal with the MMIO access at all.

Quoted 7 lines
Quoted 2 lines

That's correct. It only affects descriptor endianness for DMA descriptor mode of operation.

Ok. The funny thing is that for the MyBook Live Duo this setting might be important since the PLB_DMA engine is not part of the DWC library... Instead it's from IBM and operates in: Big Endian :-D.

Are you sure the controller is using descriptor DMA? It's more likely using buffer DMA which this setting doesn't affect. DWC2 doesn't support Descriptor DMA in device mode on mainline yet. If it's a host then it might be.

Regards,
John

#19 lore raw
In reply to John Youn
Quoted 40 lines · John Youn
Quoted 30 lines · Christian Lamparter
Quoted 29 lines · Arnd Bergmann
Quoted 16 lines · On Thursday 12 May 2016 11:58:18 Christian Lamparter wrote:
Quoted 10 lines
Quoted 7 lines
Quoted 4 lines

Detecting the endianess of the
device is probably the best future-proof solution, but it's also considerably more work to do in the driver, and comes with a tiny runtime overhead.

The runtime overhead is probably non-measurable compared with the cost of the actual MMIOs.

Right. The code size increase is probably measurable (but still small), the runtime overhead is not.

Ok, so no rebuts or complains have been posted.

I've tested the patch you made in: https://lkml.org/lkml/2016/5/9/354
and it works:

Quoted 8 lines

So, how do we go from here? There is are two small issues with the original patch (#ifdef DWC2_LOG_WRITES got converted to lower case:
#ifdef dwc2_log_writes) and I guess a proper subject would be nice.

Arnd, can you please respin and post it (cc'd stable as well)? So this is can be picked up? Or what's your plan?

(I just realized my reply was stuck in my outbox, so the patch went out first)

Quoted 8 lines
Quoted 4 lines

If I recall correctly, the rough consensus was to go with your longer patch in the future (fixed up for the comments that BenH and I sent), and I'd suggest basing it on top of a fixed version of my patch.

Well, but it comes with the "overhead"! So this was just as I said:
"Let's look at it and see if it's any good"... And I think it isn't since the usb/host/ehci people also opted for #ifdef CONFIG_BIG_ENDIAN archs etc...

Quoted 5 lines

I slightly prefer the more general patch for future kernel versions. The overhead will probably be negligible, but we can perform some testing to make sure.

Can you resubmit with all gathered feedback?

Yes, here are the changes.

I've tested it on my MyBook Live Duo. The usbotg comes right up:

[12610.540004] dwc2 4bff80000.usbotg: USB bus 1 deregistered
[12612.513934] dwc2 4bff80000.usbotg: Specified GNPTXFDEP=1024 > 256
[12612.518756] dwc2 4bff80000.usbotg: EPs: 3, shared fifos, 2042 entries in SPRAM
[12612.530112] dwc2 4bff80000.usbotg: DWC OTG Controller
[12612.533948] dwc2 4bff80000.usbotg: new USB bus registered, assigned bus number 1
[12612.540083] dwc2 4bff80000.usbotg: irq 33, io mem 0x00000000

John: Can you run some perf test with it?

I've based this on:

commit 6ea2fffc9057a67df1994d85a7c085d899eaa25a

Author: Arnd Bergmann <[email protected]>
Date:   Fri May 13 15:52:27 2016 +0200

usb: dwc2: fix regression on big-endian PowerPC/ARM systems

so naturally, it needs to be applied first. Most of the conversion work was done by the attached coccinelle semantic patches.

I had to edit the __bic32 and __orr32 helpers by hand. As well as some debugfs code and stuff in gadget.c.

here's the diffstat for everything. It's quite a lot:

 core.c      |  221 ++++++++++++++++++---------------
 core.h      |  134 +++++++++++---------
 core_intr.c |   70 +++++-----
 debugfs.c   |   55 +++-----
 gadget.c    |  396 +++++++++++++++++++++++++++++-------------------------------
 hcd.c       |  382 ++++++++++++++++++++++++++++-----------------------------
 hcd.h       |   10 -
 hcd_ddma.c  |   10 -
 hcd_intr.c  |   93 ++++++--------
 hcd_queue.c |   10 -
 platform.c  |    4 
 11 files changed, 709 insertions(+), 676 deletions(-)

So, Felipe: what's your plan?

---
@@
struct dwc2_hsotg *hsotg;
expression addr;
@@

-dwc2_readl(hsotg->regs + addr)
+dwc2_readl(hsotg, addr)

@@
struct dwc2_hsotg *hsotg;
expression addr, value;
@@

-dwc2_writel(value, hsotg->regs + addr)
+dwc2_writel(hsotg, value, addr)

@@
struct dwc2_hsotg *hsotg;
expression addr, port;
@@
addr = hsotg->regs + port;
...
-dwc2_readl(addr)
+dwc2_readl(hsotg, port)

@@
struct dwc2_hsotg *hsotg;
expression addr, val;
@@

-__orr32(hsotg->regs + addr, val)
+__orr32(hsotg, addr, val)

@@
struct dwc2_hsotg *hsotg;
expression addr, val;
@@

-__bic32(hsotg->regs + addr, val)
+__bic32(hsotg, addr, val)

@@
expression port;
identifier regs;
@@
-dwc2_readl(regs + port)
+dwc2_readl(hsotg, port)
---

drivers/usb/dwc2/core.c +122 -99
index 4135a5f..f773146 100644
@@ -71,16 +71,16 @@ static int dwc2_backup_global_registers(struct dwc2_hsotg *hsotg)
 	/* Backup global regs */
 	gr = &hsotg->gr_backup;
 
-	gr->gotgctl = dwc2_readl(hsotg->regs + GOTGCTL);
-	gr->gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
-	gr->gahbcfg = dwc2_readl(hsotg->regs + GAHBCFG);
-	gr->gusbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
-	gr->grxfsiz = dwc2_readl(hsotg->regs + GRXFSIZ);
-	gr->gnptxfsiz = dwc2_readl(hsotg->regs + GNPTXFSIZ);
-	gr->hptxfsiz = dwc2_readl(hsotg->regs + HPTXFSIZ);
-	gr->gdfifocfg = dwc2_readl(hsotg->regs + GDFIFOCFG);
+	gr->gotgctl = dwc2_readl(hsotg, GOTGCTL);
+	gr->gintmsk = dwc2_readl(hsotg, GINTMSK);
+	gr->gahbcfg = dwc2_readl(hsotg, GAHBCFG);
+	gr->gusbcfg = dwc2_readl(hsotg, GUSBCFG);
+	gr->grxfsiz = dwc2_readl(hsotg, GRXFSIZ);
+	gr->gnptxfsiz = dwc2_readl(hsotg, GNPTXFSIZ);
+	gr->hptxfsiz = dwc2_readl(hsotg, HPTXFSIZ);
+	gr->gdfifocfg = dwc2_readl(hsotg, GDFIFOCFG);
 	for (i = 0; i < MAX_EPS_CHANNELS; i++)
-		gr->dtxfsiz[i] = dwc2_readl(hsotg->regs + DPTXFSIZN(i));
+		gr->dtxfsiz[i] = dwc2_readl(hsotg, DPTXFSIZN(i));
 
 	gr->valid = true;
 	return 0;
@@ -109,17 +109,17 @@ static int dwc2_restore_global_registers(struct dwc2_hsotg *hsotg)
 	}
 	gr->valid = false;
 
-	dwc2_writel(0xffffffff, hsotg->regs + GINTSTS);
-	dwc2_writel(gr->gotgctl, hsotg->regs + GOTGCTL);
-	dwc2_writel(gr->gintmsk, hsotg->regs + GINTMSK);
-	dwc2_writel(gr->gusbcfg, hsotg->regs + GUSBCFG);
-	dwc2_writel(gr->gahbcfg, hsotg->regs + GAHBCFG);
-	dwc2_writel(gr->grxfsiz, hsotg->regs + GRXFSIZ);
-	dwc2_writel(gr->gnptxfsiz, hsotg->regs + GNPTXFSIZ);
-	dwc2_writel(gr->hptxfsiz, hsotg->regs + HPTXFSIZ);
-	dwc2_writel(gr->gdfifocfg, hsotg->regs + GDFIFOCFG);
+	dwc2_writel(hsotg, 0xffffffff, GINTSTS);
+	dwc2_writel(hsotg, gr->gotgctl, GOTGCTL);
+	dwc2_writel(hsotg, gr->gintmsk, GINTMSK);
+	dwc2_writel(hsotg, gr->gusbcfg, GUSBCFG);
+	dwc2_writel(hsotg, gr->gahbcfg, GAHBCFG);
+	dwc2_writel(hsotg, gr->grxfsiz, GRXFSIZ);
+	dwc2_writel(hsotg, gr->gnptxfsiz, GNPTXFSIZ);
+	dwc2_writel(hsotg, gr->hptxfsiz, HPTXFSIZ);
+	dwc2_writel(hsotg, gr->gdfifocfg, GDFIFOCFG);
 	for (i = 0; i < MAX_EPS_CHANNELS; i++)
-		dwc2_writel(gr->dtxfsiz[i], hsotg->regs + DPTXFSIZN(i));
+		dwc2_writel(hsotg, gr->dtxfsiz[i], DPTXFSIZN(i));
 
 	return 0;
 }
@@ -138,17 +138,17 @@ int dwc2_exit_hibernation(struct dwc2_hsotg *hsotg, bool restore)
 	if (!hsotg->core_params->hibernation)
 		return -ENOTSUPP;
 
-	pcgcctl = dwc2_readl(hsotg->regs + PCGCTL);
+	pcgcctl = dwc2_readl(hsotg, PCGCTL);
 	pcgcctl &= ~PCGCTL_STOPPCLK;
-	dwc2_writel(pcgcctl, hsotg->regs + PCGCTL);
+	dwc2_writel(hsotg, pcgcctl, PCGCTL);
 
-	pcgcctl = dwc2_readl(hsotg->regs + PCGCTL);
+	pcgcctl = dwc2_readl(hsotg, PCGCTL);
 	pcgcctl &= ~PCGCTL_PWRCLMP;
-	dwc2_writel(pcgcctl, hsotg->regs + PCGCTL);
+	dwc2_writel(hsotg, pcgcctl, PCGCTL);
 
-	pcgcctl = dwc2_readl(hsotg->regs + PCGCTL);
+	pcgcctl = dwc2_readl(hsotg, PCGCTL);
 	pcgcctl &= ~PCGCTL_RSTPDWNMODULE;
-	dwc2_writel(pcgcctl, hsotg->regs + PCGCTL);
+	dwc2_writel(hsotg, pcgcctl, PCGCTL);
 
 	udelay(100);
 	if (restore) {
@@ -219,21 +219,21 @@ int dwc2_enter_hibernation(struct dwc2_hsotg *hsotg)
 	 * Clear any pending interrupts since dwc2 will not be able to
 	 * clear them after entering hibernation.
 	 */
-	dwc2_writel(0xffffffff, hsotg->regs + GINTSTS);
+	dwc2_writel(hsotg, 0xffffffff, GINTSTS);
 
 	/* Put the controller in low power state */
-	pcgcctl = dwc2_readl(hsotg->regs + PCGCTL);
+	pcgcctl = dwc2_readl(hsotg, PCGCTL);
 
 	pcgcctl |= PCGCTL_PWRCLMP;
-	dwc2_writel(pcgcctl, hsotg->regs + PCGCTL);
+	dwc2_writel(hsotg, pcgcctl, PCGCTL);
 	ndelay(20);
 
 	pcgcctl |= PCGCTL_RSTPDWNMODULE;
-	dwc2_writel(pcgcctl, hsotg->regs + PCGCTL);
+	dwc2_writel(hsotg, pcgcctl, PCGCTL);
 	ndelay(20);
 
 	pcgcctl |= PCGCTL_STOPPCLK;
-	dwc2_writel(pcgcctl, hsotg->regs + PCGCTL);
+	dwc2_writel(hsotg, pcgcctl, PCGCTL);
 
 	return ret;
 }
@@ -250,12 +250,12 @@ int dwc2_core_reset(struct dwc2_hsotg *hsotg)
 	dev_vdbg(hsotg->dev, "%s()\n", __func__);
 
 	/* Core Soft Reset */
-	greset = dwc2_readl(hsotg->regs + GRSTCTL);
+	greset = dwc2_readl(hsotg, GRSTCTL);
 	greset |= GRSTCTL_CSFTRST;
-	dwc2_writel(greset, hsotg->regs + GRSTCTL);
+	dwc2_writel(hsotg, greset, GRSTCTL);
 	do {
 		udelay(1);
-		greset = dwc2_readl(hsotg->regs + GRSTCTL);
+		greset = dwc2_readl(hsotg, GRSTCTL);
 		if (++count > 50) {
 			dev_warn(hsotg->dev,
 				 "%s() HANG! Soft Reset GRSTCTL=%0x\n",
@@ -268,7 +268,7 @@ int dwc2_core_reset(struct dwc2_hsotg *hsotg)
 	count = 0;
 	do {
 		udelay(1);
-		greset = dwc2_readl(hsotg->regs + GRSTCTL);
+		greset = dwc2_readl(hsotg, GRSTCTL);
 		if (++count > 50) {
 			dev_warn(hsotg->dev,
 				 "%s() HANG! AHB Idle GRSTCTL=%0x\n",
@@ -328,14 +328,14 @@ static bool dwc2_force_mode(struct dwc2_hsotg *hsotg, bool host)
 	if (WARN_ON(!host && hsotg->dr_mode == USB_DR_MODE_HOST))
 		return false;
 
-	gusbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
+	gusbcfg = dwc2_readl(hsotg, GUSBCFG);
 
 	set = host ? GUSBCFG_FORCEHOSTMODE : GUSBCFG_FORCEDEVMODE;
 	clear = host ? GUSBCFG_FORCEDEVMODE : GUSBCFG_FORCEHOSTMODE;
 
 	gusbcfg &= ~clear;
 	gusbcfg |= set;
-	dwc2_writel(gusbcfg, hsotg->regs + GUSBCFG);
+	dwc2_writel(hsotg, gusbcfg, GUSBCFG);
 
 	msleep(25);
 	return true;
@@ -348,10 +348,10 @@ static void dwc2_clear_force_mode(struct dwc2_hsotg *hsotg)
 {
 	u32 gusbcfg;
 
-	gusbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
+	gusbcfg = dwc2_readl(hsotg, GUSBCFG);
 	gusbcfg &= ~GUSBCFG_FORCEHOSTMODE;
 	gusbcfg &= ~GUSBCFG_FORCEDEVMODE;
-	dwc2_writel(gusbcfg, hsotg->regs + GUSBCFG);
+	dwc2_writel(hsotg, gusbcfg, GUSBCFG);
 
 	/*
 	 * NOTE: This long sleep is _very_ important, otherwise the core will
@@ -424,56 +424,57 @@ void dwc2_dump_host_registers(struct dwc2_hsotg *hsotg)
 	dev_dbg(hsotg->dev, "Host Global Registers\n");
 	addr = hsotg->regs + HCFG;
 	dev_dbg(hsotg->dev, "HCFG	 @0x%08lX : 0x%08X\n",
-		(unsigned long)addr, dwc2_readl(addr));
+		(unsigned long)addr, dwc2_readl(hsotg, HCFG));
 	addr = hsotg->regs + HFIR;
 	dev_dbg(hsotg->dev, "HFIR	 @0x%08lX : 0x%08X\n",
-		(unsigned long)addr, dwc2_readl(addr));
+		(unsigned long)addr, dwc2_readl(hsotg, HFIR));
 	addr = hsotg->regs + HFNUM;
 	dev_dbg(hsotg->dev, "HFNUM	 @0x%08lX : 0x%08X\n",
-		(unsigned long)addr, dwc2_readl(addr));
+		(unsigned long)addr, dwc2_readl(hsotg, HFNUM));
 	addr = hsotg->regs + HPTXSTS;
 	dev_dbg(hsotg->dev, "HPTXSTS	 @0x%08lX : 0x%08X\n",
-		(unsigned long)addr, dwc2_readl(addr));
+		(unsigned long)addr, dwc2_readl(hsotg, HPTXSTS));
 	addr = hsotg->regs + HAINT;
 	dev_dbg(hsotg->dev, "HAINT	 @0x%08lX : 0x%08X\n",
-		(unsigned long)addr, dwc2_readl(addr));
+		(unsigned long)addr, dwc2_readl(hsotg, HAINT));
 	addr = hsotg->regs + HAINTMSK;
 	dev_dbg(hsotg->dev, "HAINTMSK	 @0x%08lX : 0x%08X\n",
-		(unsigned long)addr, dwc2_readl(addr));
+		(unsigned long)addr, dwc2_readl(hsotg, HAINTMSK));
 	if (hsotg->core_params->dma_desc_enable > 0) {
 		addr = hsotg->regs + HFLBADDR;
 		dev_dbg(hsotg->dev, "HFLBADDR @0x%08lX : 0x%08X\n",
-			(unsigned long)addr, dwc2_readl(addr));
+			(unsigned long)addr, dwc2_readl(hsotg, HFLBADDR));
 	}
 
 	addr = hsotg->regs + HPRT0;
 	dev_dbg(hsotg->dev, "HPRT0	 @0x%08lX : 0x%08X\n",
-		(unsigned long)addr, dwc2_readl(addr));
+		(unsigned long)addr, dwc2_readl(hsotg, HPRT0));
 
 	for (i = 0; i < hsotg->core_params->host_channels; i++) {
 		dev_dbg(hsotg->dev, "Host Channel %d Specific Registers\n", i);
 		addr = hsotg->regs + HCCHAR(i);
 		dev_dbg(hsotg->dev, "HCCHAR	 @0x%08lX : 0x%08X\n",
-			(unsigned long)addr, dwc2_readl(addr));
+			(unsigned long)addr, dwc2_readl(hsotg, HCCHAR(i)));
 		addr = hsotg->regs + HCSPLT(i);
 		dev_dbg(hsotg->dev, "HCSPLT	 @0x%08lX : 0x%08X\n",
-			(unsigned long)addr, dwc2_readl(addr));
+			(unsigned long)addr, dwc2_readl(hsotg, HCSPLT(i)));
 		addr = hsotg->regs + HCINT(i);
 		dev_dbg(hsotg->dev, "HCINT	 @0x%08lX : 0x%08X\n",
-			(unsigned long)addr, dwc2_readl(addr));
+			(unsigned long)addr, dwc2_readl(hsotg, HCINT(i)));
 		addr = hsotg->regs + HCINTMSK(i);
 		dev_dbg(hsotg->dev, "HCINTMSK	 @0x%08lX : 0x%08X\n",
-			(unsigned long)addr, dwc2_readl(addr));
+			(unsigned long)addr, dwc2_readl(hsotg, HCINTMSK(i)));
 		addr = hsotg->regs + HCTSIZ(i);
 		dev_dbg(hsotg->dev, "HCTSIZ	 @0x%08lX : 0x%08X\n",
-			(unsigned long)addr, dwc2_readl(addr));
+			(unsigned long)addr, dwc2_readl(hsotg, HCTSIZ(i)));
 		addr = hsotg->regs + HCDMA(i);
 		dev_dbg(hsotg->dev, "HCDMA	 @0x%08lX : 0x%08X\n",
-			(unsigned long)addr, dwc2_readl(addr));
+			(unsigned long)addr, dwc2_readl(hsotg, HCDMA(i)));
 		if (hsotg->core_params->dma_desc_enable > 0) {
 			addr = hsotg->regs + HCDMAB(i);
 			dev_dbg(hsotg->dev, "HCDMAB	 @0x%08lX : 0x%08X\n",
-				(unsigned long)addr, dwc2_readl(addr));
+				(unsigned long)addr,
+				dwc2_readl(hsotg, HCDMAB(i)));
 		}
 	}
 #endif
@@ -495,80 +496,80 @@ void dwc2_dump_global_registers(struct dwc2_hsotg *hsotg)
 	dev_dbg(hsotg->dev, "Core Global Registers\n");
 	addr = hsotg->regs + GOTGCTL;
 	dev_dbg(hsotg->dev, "GOTGCTL	 @0x%08lX : 0x%08X\n",
-		(unsigned long)addr, dwc2_readl(addr));
+		(unsigned long)addr, dwc2_readl(hsotg, GOTGCTL));
 	addr = hsotg->regs + GOTGINT;
 	dev_dbg(hsotg->dev, "GOTGINT	 @0x%08lX : 0x%08X\n",
-		(unsigned long)addr, dwc2_readl(addr));
+		(unsigned long)addr, dwc2_readl(hsotg, GOTGINT));
 	addr = hsotg->regs + GAHBCFG;
 	dev_dbg(hsotg->dev, "GAHBCFG	 @0x%08lX : 0x%08X\n",
-		(unsigned long)addr, dwc2_readl(addr));
+		(unsigned long)addr, dwc2_readl(hsotg, GAHBCFG));
 	addr = hsotg->regs + GUSBCFG;
 	dev_dbg(hsotg->dev, "GUSBCFG	 @0x%08lX : 0x%08X\n",
-		(unsigned long)addr, dwc2_readl(addr));
+		(unsigned long)addr, dwc2_readl(hsotg, GUSBCFG));
 	addr = hsotg->regs + GRSTCTL;
 	dev_dbg(hsotg->dev, "GRSTCTL	 @0x%08lX : 0x%08X\n",
-		(unsigned long)addr, dwc2_readl(addr));
+		(unsigned long)addr, dwc2_readl(hsotg, GRSTCTL));
 	addr = hsotg->regs + GINTSTS;
 	dev_dbg(hsotg->dev, "GINTSTS	 @0x%08lX : 0x%08X\n",
-		(unsigned long)addr, dwc2_readl(addr));
+		(unsigned long)addr, dwc2_readl(hsotg, GINTSTS));
 	addr = hsotg->regs + GINTMSK;
 	dev_dbg(hsotg->dev, "GINTMSK	 @0x%08lX : 0x%08X\n",
-		(unsigned long)addr, dwc2_readl(addr));
+		(unsigned long)addr, dwc2_readl(hsotg, GINTMSK));
 	addr = hsotg->regs + GRXSTSR;
 	dev_dbg(hsotg->dev, "GRXSTSR	 @0x%08lX : 0x%08X\n",
-		(unsigned long)addr, dwc2_readl(addr));
+		(unsigned long)addr, dwc2_readl(hsotg, GRXSTSR));
 	addr = hsotg->regs + GRXFSIZ;
 	dev_dbg(hsotg->dev, "GRXFSIZ	 @0x%08lX : 0x%08X\n",
-		(unsigned long)addr, dwc2_readl(addr));
+		(unsigned long)addr, dwc2_readl(hsotg, GRXFSIZ));
 	addr = hsotg->regs + GNPTXFSIZ;
 	dev_dbg(hsotg->dev, "GNPTXFSIZ	 @0x%08lX : 0x%08X\n",
-		(unsigned long)addr, dwc2_readl(addr));
+		(unsigned long)addr, dwc2_readl(hsotg, GNPTXFSIZ));
 	addr = hsotg->regs + GNPTXSTS;
 	dev_dbg(hsotg->dev, "GNPTXSTS	 @0x%08lX : 0x%08X\n",
-		(unsigned long)addr, dwc2_readl(addr));
+		(unsigned long)addr, dwc2_readl(hsotg, GNPTXSTS));
 	addr = hsotg->regs + GI2CCTL;
 	dev_dbg(hsotg->dev, "GI2CCTL	 @0x%08lX : 0x%08X\n",
-		(unsigned long)addr, dwc2_readl(addr));
+		(unsigned long)addr, dwc2_readl(hsotg, GI2CCTL));
 	addr = hsotg->regs + GPVNDCTL;
 	dev_dbg(hsotg->dev, "GPVNDCTL	 @0x%08lX : 0x%08X\n",
-		(unsigned long)addr, dwc2_readl(addr));
+		(unsigned long)addr, dwc2_readl(hsotg, GPVNDCTL));
 	addr = hsotg->regs + GGPIO;
 	dev_dbg(hsotg->dev, "GGPIO	 @0x%08lX : 0x%08X\n",
-		(unsigned long)addr, dwc2_readl(addr));
+		(unsigned long)addr, dwc2_readl(hsotg, GGPIO));
 	addr = hsotg->regs + GUID;
 	dev_dbg(hsotg->dev, "GUID	 @0x%08lX : 0x%08X\n",
-		(unsigned long)addr, dwc2_readl(addr));
+		(unsigned long)addr, dwc2_readl(hsotg, GUID));
 	addr = hsotg->regs + GSNPSID;
 	dev_dbg(hsotg->dev, "GSNPSID	 @0x%08lX : 0x%08X\n",
-		(unsigned long)addr, dwc2_readl(addr));
+		(unsigned long)addr, dwc2_readl(hsotg, GSNPSID));
 	addr = hsotg->regs + GHWCFG1;
 	dev_dbg(hsotg->dev, "GHWCFG1	 @0x%08lX : 0x%08X\n",
-		(unsigned long)addr, dwc2_readl(addr));
+		(unsigned long)addr, dwc2_readl(hsotg, GHWCFG1));
 	addr = hsotg->regs + GHWCFG2;
 	dev_dbg(hsotg->dev, "GHWCFG2	 @0x%08lX : 0x%08X\n",
-		(unsigned long)addr, dwc2_readl(addr));
+		(unsigned long)addr, dwc2_readl(hsotg, GHWCFG2));
 	addr = hsotg->regs + GHWCFG3;
 	dev_dbg(hsotg->dev, "GHWCFG3	 @0x%08lX : 0x%08X\n",
-		(unsigned long)addr, dwc2_readl(addr));
+		(unsigned long)addr, dwc2_readl(hsotg, GHWCFG3));
 	addr = hsotg->regs + GHWCFG4;
 	dev_dbg(hsotg->dev, "GHWCFG4	 @0x%08lX : 0x%08X\n",
-		(unsigned long)addr, dwc2_readl(addr));
+		(unsigned long)addr, dwc2_readl(hsotg, GHWCFG4));
 	addr = hsotg->regs + GLPMCFG;
 	dev_dbg(hsotg->dev, "GLPMCFG	 @0x%08lX : 0x%08X\n",
-		(unsigned long)addr, dwc2_readl(addr));
+		(unsigned long)addr, dwc2_readl(hsotg, GLPMCFG));
 	addr = hsotg->regs + GPWRDN;
 	dev_dbg(hsotg->dev, "GPWRDN	 @0x%08lX : 0x%08X\n",
-		(unsigned long)addr, dwc2_readl(addr));
+		(unsigned long)addr, dwc2_readl(hsotg, GPWRDN));
 	addr = hsotg->regs + GDFIFOCFG;
 	dev_dbg(hsotg->dev, "GDFIFOCFG	 @0x%08lX : 0x%08X\n",
-		(unsigned long)addr, dwc2_readl(addr));
+		(unsigned long)addr, dwc2_readl(hsotg, GDFIFOCFG));
 	addr = hsotg->regs + HPTXFSIZ;
 	dev_dbg(hsotg->dev, "HPTXFSIZ	 @0x%08lX : 0x%08X\n",
-		(unsigned long)addr, dwc2_readl(addr));
+		(unsigned long)addr, dwc2_readl(hsotg, HPTXFSIZ));
 
 	addr = hsotg->regs + PCGCTL;
 	dev_dbg(hsotg->dev, "PCGCTL	 @0x%08lX : 0x%08X\n",
-		(unsigned long)addr, dwc2_readl(addr));
+		(unsigned long)addr, dwc2_readl(hsotg, PCGCTL));
 #endif
 }
 
@@ -587,15 +588,15 @@ void dwc2_flush_tx_fifo(struct dwc2_hsotg *hsotg, const int num)
 
 	greset = GRSTCTL_TXFFLSH;
 	greset |= num << GRSTCTL_TXFNUM_SHIFT & GRSTCTL_TXFNUM_MASK;
-	dwc2_writel(greset, hsotg->regs + GRSTCTL);
+	dwc2_writel(hsotg, greset, GRSTCTL);
 
 	do {
-		greset = dwc2_readl(hsotg->regs + GRSTCTL);
+		greset = dwc2_readl(hsotg, GRSTCTL);
 		if (++count > 10000) {
 			dev_warn(hsotg->dev,
 				 "%s() HANG! GRSTCTL=%0x GNPTXSTS=0x%08x\n",
 				 __func__, greset,
-				 dwc2_readl(hsotg->regs + GNPTXSTS));
+				 dwc2_readl(hsotg, GNPTXSTS));
 			break;
 		}
 		udelay(1);
@@ -618,10 +619,10 @@ void dwc2_flush_rx_fifo(struct dwc2_hsotg *hsotg)
 	dev_vdbg(hsotg->dev, "%s()\n", __func__);
 
 	greset = GRSTCTL_RXFFLSH;
-	dwc2_writel(greset, hsotg->regs + GRSTCTL);
+	dwc2_writel(hsotg, greset, GRSTCTL);
 
 	do {
-		greset = dwc2_readl(hsotg->regs + GRSTCTL);
+		greset = dwc2_readl(hsotg, GRSTCTL);
 		if (++count > 10000) {
 			dev_warn(hsotg->dev, "%s() HANG! GRSTCTL=%0x\n",
 				 __func__, greset);
@@ -1358,8 +1359,8 @@ static void dwc2_get_host_hwparams(struct dwc2_hsotg *hsotg)
 
 	forced = dwc2_force_mode_if_needed(hsotg, true);
 
-	gnptxfsiz = dwc2_readl(hsotg->regs + GNPTXFSIZ);
-	hptxfsiz = dwc2_readl(hsotg->regs + HPTXFSIZ);
+	gnptxfsiz = dwc2_readl(hsotg, GNPTXFSIZ);
+	hptxfsiz = dwc2_readl(hsotg, HPTXFSIZ);
 	dev_dbg(hsotg->dev, "gnptxfsiz=%08x\n", gnptxfsiz);
 	dev_dbg(hsotg->dev, "hptxfsiz=%08x\n", hptxfsiz);
 
@@ -1388,7 +1389,7 @@ static void dwc2_get_dev_hwparams(struct dwc2_hsotg *hsotg)
 
 	forced = dwc2_force_mode_if_needed(hsotg, false);
 
-	gnptxfsiz = dwc2_readl(hsotg->regs + GNPTXFSIZ);
+	gnptxfsiz = dwc2_readl(hsotg, GNPTXFSIZ);
 	dev_dbg(hsotg->dev, "gnptxfsiz=%08x\n", gnptxfsiz);
 
 	if (forced)
@@ -1398,6 +1399,28 @@ static void dwc2_get_dev_hwparams(struct dwc2_hsotg *hsotg)
 				       FIFOSIZE_DEPTH_SHIFT;
 }
 
+int dwc2_detect_endiannes(struct dwc2_hsotg *hsotg)
+{
+	u32 sid;
+
+	/*
+	 * try little endian first, as this is the default on most
+	 * platforms.
+	 */
+	hsotg->is_big_endian = 0;
+	sid = dwc2_readl(hsotg, GSNPSID);
+	if ((sid & 0xffff0000) == 0x4f540000)
+		return 0;
+
+	hsotg->is_big_endian = 1;
+	sid = dwc2_readl(hsotg, GSNPSID);
+	if ((sid & 0xffff0000) == 0x4f540000)
+		return 0;
+
+	dev_err(hsotg->dev, "Automatic endian detection failed");
+	return -EINVAL;
+}
+
 /**
  * During device initialization, read various hardware configuration
  * registers and interpret the contents.
@@ -1412,10 +1435,10 @@ int dwc2_get_hwparams(struct dwc2_hsotg *hsotg)
 	/*
 	 * Attempt to ensure this device is really a DWC_otg Controller.
 	 * Read and verify the GSNPSID register contents. The value should be
-	 * 0x45f42xxx or 0x45f43xxx, which corresponds to either "OT2" or "OT3",
+	 * 0x4f542xxx or 0x4f543xxx, which corresponds to either "OT2" or "OT3",
 	 * as in "OTG version 2.xx" or "OTG version 3.xx".
 	 */
-	hw->snpsid = dwc2_readl(hsotg->regs + GSNPSID);
+	hw->snpsid = dwc2_readl(hsotg, GSNPSID);
 	if ((hw->snpsid & 0xfffff000) != 0x4f542000 &&
 	    (hw->snpsid & 0xfffff000) != 0x4f543000) {
 		dev_err(hsotg->dev, "Bad value for GSNPSID: 0x%08x\n",
@@ -1427,11 +1450,11 @@ int dwc2_get_hwparams(struct dwc2_hsotg *hsotg)
 		hw->snpsid >> 12 & 0xf, hw->snpsid >> 8 & 0xf,
 		hw->snpsid >> 4 & 0xf, hw->snpsid & 0xf, hw->snpsid);
 
-	hwcfg1 = dwc2_readl(hsotg->regs + GHWCFG1);
-	hwcfg2 = dwc2_readl(hsotg->regs + GHWCFG2);
-	hwcfg3 = dwc2_readl(hsotg->regs + GHWCFG3);
-	hwcfg4 = dwc2_readl(hsotg->regs + GHWCFG4);
-	grxfsiz = dwc2_readl(hsotg->regs + GRXFSIZ);
+	hwcfg1 = dwc2_readl(hsotg, GHWCFG1);
+	hwcfg2 = dwc2_readl(hsotg, GHWCFG2);
+	hwcfg3 = dwc2_readl(hsotg, GHWCFG3);
+	hwcfg4 = dwc2_readl(hsotg, GHWCFG4);
+	grxfsiz = dwc2_readl(hsotg, GRXFSIZ);
 
 	dev_dbg(hsotg->dev, "hwcfg1=%08x\n", hwcfg1);
 	dev_dbg(hsotg->dev, "hwcfg2=%08x\n", hwcfg2);
@@ -1571,7 +1594,7 @@ u16 dwc2_get_otg_version(struct dwc2_hsotg *hsotg)
 
 bool dwc2_is_controller_alive(struct dwc2_hsotg *hsotg)
 {
-	if (dwc2_readl(hsotg->regs + GSNPSID) == 0xffffffff)
+	if (dwc2_readl(hsotg, GSNPSID) == 0xffffffff)
 		return false;
 	else
 		return true;
@@ -1585,10 +1608,10 @@ bool dwc2_is_controller_alive(struct dwc2_hsotg *hsotg)
  */
 void dwc2_enable_global_interrupts(struct dwc2_hsotg *hsotg)
 {
-	u32 ahbcfg = dwc2_readl(hsotg->regs + GAHBCFG);
+	u32 ahbcfg = dwc2_readl(hsotg, GAHBCFG);
 
 	ahbcfg |= GAHBCFG_GLBL_INTR_EN;
-	dwc2_writel(ahbcfg, hsotg->regs + GAHBCFG);
+	dwc2_writel(hsotg, ahbcfg, GAHBCFG);
 }
 
 /**
@@ -1599,16 +1622,16 @@ void dwc2_enable_global_interrupts(struct dwc2_hsotg *hsotg)
  */
 void dwc2_disable_global_interrupts(struct dwc2_hsotg *hsotg)
 {
-	u32 ahbcfg = dwc2_readl(hsotg->regs + GAHBCFG);
+	u32 ahbcfg = dwc2_readl(hsotg, GAHBCFG);
 
 	ahbcfg &= ~GAHBCFG_GLBL_INTR_EN;
-	dwc2_writel(ahbcfg, hsotg->regs + GAHBCFG);
+	dwc2_writel(hsotg, ahbcfg, GAHBCFG);
 }
 
 /* Returns the controller's GHWCFG2.OTG_MODE. */
 unsigned dwc2_op_mode(struct dwc2_hsotg *hsotg)
 {
-	u32 ghwcfg2 = dwc2_readl(hsotg->regs + GHWCFG2);
+	u32 ghwcfg2 = dwc2_readl(hsotg, GHWCFG2);
 
 	return (ghwcfg2 & GHWCFG2_OP_MODE_MASK) >>
 		GHWCFG2_OP_MODE_SHIFT;
drivers/usb/dwc2/core.h +78 -56

167 lines not on this page · raw message

drivers/usb/dwc2/core_intr.c +35 -35

213 lines not on this page · raw message

drivers/usb/dwc2/debugfs.c +26 -29

123 lines not on this page · raw message

drivers/usb/dwc2/gadget.c +195 -201

1,033 lines not on this page · raw message

drivers/usb/dwc2/hcd.c +188 -194

992 lines not on this page · raw message

drivers/usb/dwc2/hcd.h +5 -5
index 7758bfb..880549e 100644

Lines of this file are not shown. View the raw message.

drivers/usb/dwc2/hcd_ddma.c +5 -5
index 0e1d42b..8c329e7 100644

Lines of this file are not shown. View the raw message.

drivers/usb/dwc2/hcd_intr.c +46 -47

93 lines not on this page · raw message

drivers/usb/dwc2/hcd_queue.c +5 -5
index b5c7793..e6e0185 100644

Lines of this file are not shown. View the raw message.

drivers/usb/dwc2/platform.c +4 -0
index fc6f525..0ad6155 100644

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

Part of the diff is not shown here. View the raw message.

#20 lore raw
Quoted 9 lines · On Saturday 14 May 2016 15:11:34 Christian Lamparter wrote:
drivers/usb/dwc2/core.h +7 -0
+#ifdef CONFIG_MIPS
+/*
+ * There are some MIPS machines that can run in either big-endian
+ * or little-endian mode and that use the dwc2 register without
+ * a byteswap in both ways.
+ * Unlike other architectures, MIPS apparently does not require a
+ * barrier before the __raw_writel() to synchronize with DMA but does

drivers/usb/dwc2/core.h enum dwc2_halt_status { · line 977

Quoted 10 lines
drivers/usb/dwc2/core.h +10 -0
+ * require the barrier after the __raw_writel() to serialize a set of
+ * writes. This set of operations was added specifically for MIPS and
+ * should only be used there.
+ */
+static inline u32 dwc2_readl(struct dwc2_hsotg *hsotg,
+                            ptrdiff_t reg)
+{
+       const void __iomem *addr = hsotg->regs + reg;
+       u32 value = __raw_readl(addr);
+

I see you keep the special case for MIPS here, I'd vote for folding that back into the architecture-independent version and not treating MIPS any different from the others. With your endianness detection, MIPS should have no way of getting the byteorder wrong, and on MIPS the platform is responsible for adding the appropriate barriers to readl/writel.

Other than this, the patch looks good to me.

Arnd

#21 lore raw
In reply to Christian Lamparter
Quoted 65 lines · Christian Lamparter
Quoted 40 lines · John Youn
Quoted 30 lines · Christian Lamparter
Quoted 29 lines · Arnd Bergmann
Quoted 16 lines · On Thursday 12 May 2016 11:58:18 Christian Lamparter wrote:
Quoted 10 lines
Quoted 7 lines
Quoted 4 lines

Detecting the endianess of the
device is probably the best future-proof solution, but it's also considerably more work to do in the driver, and comes with a tiny runtime overhead.

The runtime overhead is probably non-measurable compared with the cost of the actual MMIOs.

Right. The code size increase is probably measurable (but still small), the runtime overhead is not.

Ok, so no rebuts or complains have been posted.

I've tested the patch you made in: https://lkml.org/lkml/2016/5/9/354
and it works:

Quoted 8 lines

So, how do we go from here? There is are two small issues with the original patch (#ifdef DWC2_LOG_WRITES got converted to lower case:
#ifdef dwc2_log_writes) and I guess a proper subject would be nice.

Arnd, can you please respin and post it (cc'd stable as well)? So this is can be picked up? Or what's your plan?

(I just realized my reply was stuck in my outbox, so the patch went out first)

Quoted 8 lines
Quoted 4 lines

If I recall correctly, the rough consensus was to go with your longer patch in the future (fixed up for the comments that BenH and I sent), and I'd suggest basing it on top of a fixed version of my patch.

Well, but it comes with the "overhead"! So this was just as I said:
"Let's look at it and see if it's any good"... And I think it isn't since the usb/host/ehci people also opted for #ifdef CONFIG_BIG_ENDIAN archs etc...

Quoted 5 lines

I slightly prefer the more general patch for future kernel versions. The overhead will probably be negligible, but we can perform some testing to make sure.

Can you resubmit with all gathered feedback?

Yes, here are the changes.

I've tested it on my MyBook Live Duo. The usbotg comes right up:

[12610.540004] dwc2 4bff80000.usbotg: USB bus 1 deregistered
[12612.513934] dwc2 4bff80000.usbotg: Specified GNPTXFDEP=1024 > 256
[12612.518756] dwc2 4bff80000.usbotg: EPs: 3, shared fifos, 2042 entries in SPRAM
[12612.530112] dwc2 4bff80000.usbotg: DWC OTG Controller
[12612.533948] dwc2 4bff80000.usbotg: new USB bus registered, assigned bus number 1
[12612.540083] dwc2 4bff80000.usbotg: irq 33, io mem 0x00000000

John: Can you run some perf test with it?

I've based this on:

commit 6ea2fffc9057a67df1994d85a7c085d899eaa25a

Author: Arnd Bergmann <[email protected]>
Date:   Fri May 13 15:52:27 2016 +0200
Quoted 9 lines

usb: dwc2: fix regression on big-endian PowerPC/ARM systems

so naturally, it needs to be applied first. Most of the conversion work was done by the attached coccinelle semantic patches.

I had to edit the __bic32 and __orr32 helpers by hand. As well as some debugfs code and stuff in gadget.c.

Thanks Christian.

I'll keep this in our internal tree and send it to Felipe later. This causes a bunch of conflicts that I have to fix up and I should do a bit of testing as well.

And since there is a patch that fixes the regression this is can wait.

Regards,
John

#22 lore raw
Quoted 78 lines · John Youn
Quoted 65 lines · Christian Lamparter
Quoted 40 lines · John Youn
Quoted 30 lines · Christian Lamparter
Quoted 29 lines · Arnd Bergmann
Quoted 16 lines · On Thursday 12 May 2016 11:58:18 Christian Lamparter wrote:
Quoted 10 lines
Quoted 7 lines
Quoted 4 lines

Detecting the endianess of the
device is probably the best future-proof solution, but it's also considerably more work to do in the driver, and comes with a tiny runtime overhead.

The runtime overhead is probably non-measurable compared with the cost of the actual MMIOs.

Right. The code size increase is probably measurable (but still small), the runtime overhead is not.

Ok, so no rebuts or complains have been posted.

I've tested the patch you made in: https://lkml.org/lkml/2016/5/9/354
and it works:

Quoted 8 lines

So, how do we go from here? There is are two small issues with the original patch (#ifdef DWC2_LOG_WRITES got converted to lower case:
#ifdef dwc2_log_writes) and I guess a proper subject would be nice.

Arnd, can you please respin and post it (cc'd stable as well)? So this is can be picked up? Or what's your plan?

(I just realized my reply was stuck in my outbox, so the patch went out first)

Quoted 8 lines
Quoted 4 lines

If I recall correctly, the rough consensus was to go with your longer patch in the future (fixed up for the comments that BenH and I sent), and I'd suggest basing it on top of a fixed version of my patch.

Well, but it comes with the "overhead"! So this was just as I said:
"Let's look at it and see if it's any good"... And I think it isn't since the usb/host/ehci people also opted for #ifdef CONFIG_BIG_ENDIAN archs etc...

Quoted 5 lines

I slightly prefer the more general patch for future kernel versions. The overhead will probably be negligible, but we can perform some testing to make sure.

Can you resubmit with all gathered feedback?

Yes, here are the changes.

I've tested it on my MyBook Live Duo. The usbotg comes right up:

[12610.540004] dwc2 4bff80000.usbotg: USB bus 1 deregistered
[12612.513934] dwc2 4bff80000.usbotg: Specified GNPTXFDEP=1024 > 256
[12612.518756] dwc2 4bff80000.usbotg: EPs: 3, shared fifos, 2042 entries in SPRAM
[12612.530112] dwc2 4bff80000.usbotg: DWC OTG Controller
[12612.533948] dwc2 4bff80000.usbotg: new USB bus registered, assigned bus number 1
[12612.540083] dwc2 4bff80000.usbotg: irq 33, io mem 0x00000000

John: Can you run some perf test with it?

I've based this on:

commit 6ea2fffc9057a67df1994d85a7c085d899eaa25a

Author: Arnd Bergmann <[email protected]>
Date:   Fri May 13 15:52:27 2016 +0200
Quoted 9 lines

usb: dwc2: fix regression on big-endian PowerPC/ARM systems

so naturally, it needs to be applied first. Most of the conversion work was done by the attached coccinelle semantic patches.

I had to edit the __bic32 and __orr32 helpers by hand. As well as some debugfs code and stuff in gadget.c.

Thanks Christian.

Quoted 8 lines

I'll keep this in our internal tree and send it to Felipe later. This causes a bunch of conflicts that I have to fix up and I should do a bit of testing as well.

And since there is a patch that fixes the regression this is can wait.

Regards,
John

---
Hey, that's really nice of you to do that :-D. Please keep me in the loop (Cc) for those then.

Yes, this needs definitely testing on all the affected ARCHs. I've attached a diff to a updated version of the patch. It drops the special MIPS case (as requested by Arnd).

BTW, I looked into the ioread32_rep and iowrite32_rep again. I'm not entirely convinced that the hardware FIFOs are actually endian neutral. But I can't verify it since my Western Digital My Book Live only supports the host configuration (forces host mode), so I don't know what a device in dual-mode or peripheral do here.

The reason why I think it was broken is because there's a PIO copy to and from the HCFIFO(x) in dwc2_hc_write_packet and dwc2_hc_read_packet access in the hcd.c file as well... And there, the code was using the dwc2_readl and dwc2_writel to access the data. I added special accessors for the FIFOS now:
dwc2_readl_rep and dwc2_writel_rep.

I went all the way and implemented the helpers to do unaligned access if necessary (not sure if adding likely branches is a good idea, as this could be either always true or false for a specific driver the whole time).

NB: it also fixes a "regs variable not used in dwc2_hsotg_dump" warning if DEBUG isn't selected.

NB2: If it you need a patch against a specific tree, please let me know.
---

drivers/usb/dwc2/core.h +100 -46
index 2fa57cd..69030bb 100644
@@ -42,6 +42,7 @@
 #include <linux/usb/gadget.h>
 #include <linux/usb/otg.h>
 #include <linux/usb/phy.h>
+#include <asm/unaligned.h>
 #include "hw.h"
 
 /*
@@ -958,50 +959,6 @@ enum dwc2_halt_status {
 	DWC2_HC_XFER_URB_DEQUEUE,
 };
 
-#ifdef CONFIG_MIPS
-/*
- * There are some MIPS machines that can run in either big-endian
- * or little-endian mode and that use the dwc2 register without
- * a byteswap in both ways.
- * Unlike other architectures, MIPS apparently does not require a
- * barrier before the __raw_writel() to synchronize with DMA but does
- * require the barrier after the __raw_writel() to serialize a set of
- * writes. This set of operations was added specifically for MIPS and
- * should only be used there.
- */
-static inline u32 dwc2_readl(struct dwc2_hsotg *hsotg,
-			     ptrdiff_t reg)
-{
-	const void __iomem *addr = hsotg->regs + reg;
-	u32 value = __raw_readl(addr);
-
-	/*
-	 * In order to preserve endianness __raw_* operation is used. Therefore
-	 * a barrier is needed to ensure IO access is not re-ordered across
-	 * reads or writes
-	 */
-	mb();
-	return value;
-}
-
-static inline void dwc2_writel(struct dwc2_hsotg *hsotg, u32 value,
-			       ptrdiff_t reg)
-{
-	const void __iomem *addr = hsotg->regs + reg;
-	__raw_writel(value, addr);
-
-	/*
-	 * In order to preserve endianness __raw_* operation is used. Therefore
-	 * a barrier is needed to ensure IO access is not re-ordered across
-	 * reads or writes
-	 */
-	mb();
-#ifdef DWC2_LOG_WRITES
-	pr_info("INFO:: wrote %08x to %p\n", value, addr);
-#endif
-}
-#else
-/* Normal architectures just use readl/write_be */
 static inline u32 dwc2_readl(struct dwc2_hsotg *hsotg,
 			     ptrdiff_t reg)
 {
@@ -1014,7 +971,8 @@ static inline u32 dwc2_readl(struct dwc2_hsotg *hsotg,
 }
 
 
-static inline void dwc2_writel(struct dwc2_hsotg *hsotg, u32 value,
+static inline void dwc2_writel(struct dwc2_hsotg *hsotg,
+			       const u32 value,
 			       ptrdiff_t reg)
 {
 	void __iomem *addr = hsotg->regs + reg;
@@ -1028,7 +986,103 @@ static inline void dwc2_writel(struct dwc2_hsotg *hsotg, u32 value,
 	pr_info("info:: wrote %08x to %p\n", value, addr);
 #endif
 }
-#endif
+
+static inline void dwc2_readl_rep(struct dwc2_hsotg *hsotg,
+				  ptrdiff_t reg,
+				  u32 *buf, const size_t len)
+{
+	void __iomem *addr = hsotg->regs + reg;
+	size_t i, remaining = len & ~4;
+
+	if (hsotg->is_big_endian) {
+		if (likely(IS_ALIGNED(*buf, 0x4))) {
+			for (i = len >> 2; i > 0; i--)
+				*buf++ = ioread32be(addr);
+		} else {
+			/* xfer_buf is not DWORD aligned */
+			for (i = len >> 2; i > 0; i--) {
+				u32 data = ioread32be(addr);
+
+				put_unaligned(data, buf);
+				buf++;
+			}
+		}
+	} else {
+		/* little-endian accessors */
+		if (likely(IS_ALIGNED(*buf, 0x4))) {
+			for (i = len >> 2; i > 0; i--)
+				*buf++ = ioread32(addr);
+
+		} else {
+			/* xfer_buf is not DWORD aligned */
+			for (i = len >> 2; i > 0; i--) {
+				u32 data = ioread32be(addr);
+
+				put_unaligned(data, buf);
+				buf++;
+			}
+		}
+	}
+
+	if (unlikely(remaining)) {
+		u32 data_u32;
+		u8 *buf_u8 = (u8 *) buf;
+		u8 *data_u8 = (u8 *) &data_u32;
+
+		data_u32 = dwc2_readl(hsotg, reg);
+
+		while (remaining--)
+			*buf_u8++ = *data_u8++;
+	}
+}
+
+static inline void dwc2_writel_rep(struct dwc2_hsotg *hsotg,
+				   const ptrdiff_t reg,
+				   const u32 *buf, const size_t len)
+{
+	void __iomem *addr = hsotg->regs + reg;
+	size_t i, remaining = len & ~4;
+
+	if (hsotg->is_big_endian) {
+		if (likely(IS_ALIGNED(*buf, 0x4))) {
+			for (i = len >> 2; i > 0; i--)
+				iowrite32be(*buf++, addr);
+		} else {
+			/* xfer_buf is not DWORD aligned */
+			for (i = len >> 2; i > 0; i--) {
+				u32 data = get_unaligned(buf);
+
+				iowrite32be(data, addr);
+				buf++;
+			}
+		}
+	} else {
+		/* little-endian accessors */
+		if (likely(IS_ALIGNED(*buf, 0x4))) {
+			for (i = len >> 2; i > 0; i--)
+				iowrite32(*buf++, addr);
+		} else {
+			/* xfer_buf is not DWORD aligned */
+			for (i = len >> 2; i > 0; i--) {
+				u32 data = get_unaligned(buf);
+
+				iowrite32(data, addr);
+				buf++;
+			}
+		}
+	}
+
+	if (unlikely(remaining)) {
+		u32 data_u32;
+		u8 *buf_u8 = (u8 *) buf;
+		u8 *data_u8 = (u8 *) &data_u32;
+
+		while (remaining--)
+			*data_u8++ = *buf_u8++;
+
+		dwc2_writel(hsotg, data_u32, reg);
+	}
+}
 
 extern int dwc2_detect_endiannes(struct dwc2_hsotg *hsotg);
 
drivers/usb/dwc2/gadget.c +7 -9
index 2c687d9..531b30f 100644
@@ -317,7 +317,7 @@ static int dwc2_hsotg_write_fifo(struct dwc2_hsotg *hsotg,
 	u32 gnptxsts = dwc2_readl(hsotg, GNPTXSTS);
 	int buf_pos = hs_req->req.actual;
 	int to_write = hs_ep->size_loaded;
-	void *data;
+	u32 *data;
 	int can_write;
 	int pkt_round;
 	int max_transfer;
@@ -457,10 +457,9 @@ static int dwc2_hsotg_write_fifo(struct dwc2_hsotg *hsotg,
 	if (periodic)
 		hs_ep->fifo_load += to_write;
 
-	to_write = DIV_ROUND_UP(to_write, 4);
 	data = hs_req->req.buf + buf_pos;
 
-	iowrite32_rep(hsotg->regs + EPFIFO(hs_ep->index), data, to_write);
+	dwc2_writel_rep(hsotg, EPFIFO(hs_ep->index), data, to_write);
 
 	return (to_write >= can_write) ? -ENOSPC : 0;
 }
@@ -1439,12 +1438,11 @@ static void dwc2_hsotg_rx_data(struct dwc2_hsotg *hsotg, int ep_idx, int size)
 {
 	struct dwc2_hsotg_ep *hs_ep = hsotg->eps_out[ep_idx];
 	struct dwc2_hsotg_req *hs_req = hs_ep->req;
-	void __iomem *fifo = hsotg->regs + EPFIFO(ep_idx);
+	u32 *data;
 	int to_read;
 	int max_req;
 	int read_ptr;
 
-
 	if (!hs_req) {
 		u32 epctl = dwc2_readl(hsotg, DOEPCTL(ep_idx));
 		int ptr;
@@ -1455,7 +1453,7 @@ static void dwc2_hsotg_rx_data(struct dwc2_hsotg *hsotg, int ep_idx, int size)
 
 		/* dump the data from the FIFO, we've nothing we can do */
 		for (ptr = 0; ptr < size; ptr += 4)
-			(void)dwc2_readl(hsotg, EPFIFO(ep_idx));
+			(void)__raw_readl(hsotg->regs + EPFIFO(ep_idx));
 
 		return;
 	}
@@ -1479,13 +1477,14 @@ static void dwc2_hsotg_rx_data(struct dwc2_hsotg *hsotg, int ep_idx, int size)
 
 	hs_ep->total_data += to_read;
 	hs_req->req.actual += to_read;
-	to_read = DIV_ROUND_UP(to_read, 4);
 
 	/*
 	 * note, we might over-write the buffer end by 3 bytes depending on
 	 * alignment of the data.
 	 */
-	ioread32_rep(fifo, hs_req->req.buf + read_ptr, to_read);
+	data = hs_req->req.buf + read_ptr;
+
+	dwc2_readl_rep(hsotg, EPFIFO(ep_idx), data, to_read);
 }
 
 /**
@@ -3411,7 +3416,6 @@ static void dwc2_hsotg_dump(struct dwc2_hsotg *hsotg)
 {
 #ifdef DEBUG
 	struct device *dev = hsotg->dev;
-	void __iomem *regs = hsotg->regs;
 	u32 val;
 	int idx;
 
drivers/usb/dwc2/hcd.c +2 -26
index dcd6338..8568ff4 100644
@@ -567,19 +567,10 @@ u32 dwc2_calc_frame_interval(struct dwc2_hsotg *hsotg)
 void dwc2_read_packet(struct dwc2_hsotg *hsotg, u8 *dest, u16 bytes)
 {
 	u32 *data_buf = (u32 *)dest;
-	int word_count = (bytes + 3) / 4;
-	int i;
-
-	/*
-	 * Todo: Account for the case where dest is not dword aligned. This
-	 * requires reading data from the FIFO into a u32 temp buffer, then
-	 * moving it into the data buffer.
-	 */
 
 	dev_vdbg(hsotg->dev, "%s(%p,%p,%d)\n", __func__, hsotg, dest, bytes);
 
-	for (i = 0; i < word_count; i++, data_buf++)
-		*data_buf = dwc2_readl(hsotg, HCFIFO(0));
+	dwc2_readl_rep(hsotg, HCFIFO(0), data_buf, bytes);
 }
 
 /**
@@ -1236,10 +1227,8 @@ static void dwc2_set_pid_isoc(struct dwc2_host_chan *chan)
 static void dwc2_hc_write_packet(struct dwc2_hsotg *hsotg,
 				 struct dwc2_host_chan *chan)
 {
-	u32 i;
 	u32 remaining_count;
 	u32 byte_count;
-	u32 dword_count;
 	u32 *data_buf = (u32 *)chan->xfer_buf;
 
 	if (dbg_hc(chan))
@@ -1251,20 +1240,7 @@ static void dwc2_hc_write_packet(struct dwc2_hsotg *hsotg,
 	else
 		byte_count = remaining_count;
 
-	dword_count = (byte_count + 3) / 4;
-
-	if (((unsigned long)data_buf & 0x3) == 0) {
-		/* xfer_buf is DWORD aligned */
-		for (i = 0; i < dword_count; i++, data_buf++)
-			dwc2_writel(hsotg, *data_buf, HCFIFO(chan->hc_num));
-	} else {
-		/* xfer_buf is not DWORD aligned */
-		for (i = 0; i < dword_count; i++, data_buf++) {
-			u32 data = data_buf[0] | data_buf[1] << 8 |
-				   data_buf[2] << 16 | data_buf[3] << 24;
-			dwc2_writel(hsotg, data, HCFIFO(chan->hc_num));
-		}
-	}
+	dwc2_writel_rep(hsotg, HCFIFO(chan->hc_num), data_buf, byte_count);
 
 	chan->xfer_count += byte_count;
 	chan->xfer_buf += byte_count;
#23 lore raw
Quoted 9 lines · On Wednesday 18 May 2016 21:14:55 Christian Lamparter wrote:
Quoted 1 line · John Youn

On 5/14/2016 6:11 AM, Christian Lamparter wrote:

Hey, that's really nice of you to do that :-D. Please keep me in the loop (Cc) for those then.

Yes, this needs definitely testing on all the affected ARCHs. I've attached a diff to a updated version of the patch. It drops the special MIPS case (as requested by Arnd).

Ok, thanks!

Quoted 5 lines

BTW, I looked into the ioread32_rep and iowrite32_rep again. I'm not entirely convinced that the hardware FIFOs are actually endian neutral. But I can't verify it since my Western Digital My Book Live only supports the host configuration (forces host mode), so I don't know what a device in dual-mode or peripheral do here.

I think it's highly unlikely that designware would have screwed up their part in such an unusual way, by intentionally adding a byte-reversal on something that is not connected to a 32-bit register.

Note that the reason why ioread32_rep() doesn't swap is not that the registers are endian neutral, but that they use endianess in the same way that the memory does: If you want to look at the first byte of a (theoretical) four-byte USB data packet, we read four bytes from the FIFO register using __raw_readl() (a pointer dereference) and store it to memory using a 32-bit write:

	*(u32 *)buffer = __raw_readl(FIFO);

Then we expect the first byte of the packet to be at the start:

	byte0 = *(u8*)buffer;

If you replace the __raw_readl() with ioread32(), it gets byteswapped on big-endian *CPUs*, and then written to memory without an extra swap. This means that now you get the wrong data depending on the kernel endianess configuration, and independent of the device endianess.

If the big-endian mode of the dwc2 block indeed contains a byteswap on the FIFO, that would mean not having to use ioread32_be() for the FIFO, but using

fifo_read32(void *buffer)

{
	u32 data = __raw_readl(FIFO_ADDRESS);
	if (big_endian_registers)
		data = bswap32(data);
	*(u32*)buffer = data;
}

so we byteswap the FIFO contents back, regardless of the CPU endianess. As I said, it's unlikely that the hardware is this broken, but not impossible.

Quoted 4 lines

The reason why I think it was broken is because there's a PIO copy to and from the HCFIFO(x) in dwc2_hc_write_packet and dwc2_hc_read_packet access in the hcd.c file as well... And there, the code was using the dwc2_readl and dwc2_writel to access the data.

Well, we know for a fact that those functions get endianess wrong, see dwc2_hc_write_packet:

        if (((unsigned long)data_buf & 0x3) == 0) {
                /* xfer_buf is DWORD aligned */
                for (i = 0; i < dword_count; i++, data_buf++)
                        dwc2_writel(*data_buf, data_fifo);
        } else {
                /* xfer_buf is not DWORD aligned */
                for (i = 0; i < dword_count; i++, data_buf++) {
                        u32 data = data_buf[0] | data_buf[1] << 8 |
                                   data_buf[2] << 16 | data_buf[3] << 24;
                        dwc2_writel(data, data_fifo);
                }
        }

On big-endian machines, unaligned case performs a byte swap while the aligned case does not. My best guess is that this function never got called on either the MIPS machine that first got the fix or your PowerPC machine.

Another possibility is that you are right that there is a byteswap on the FIFO register in big-endian mode, and that this function always gets unaligned buffers, so the byteswap here cancels out the byteswap on the FIFO when both the CPU and the device are big-endian.

Quoted 4 lines
drivers/usb/dwc2/hcd.c +0 -3
-	if (((unsigned long)data_buf & 0x3) == 0) {
-		/* xfer_buf is DWORD aligned */
-		for (i = 0; i < dword_count; i++, data_buf++)

drivers/usb/dwc2/hcd.c static void dwc2_hc_write_packet(struct dwc2_hsotg *hsotg, · line 1243

Quoted 10 lines
drivers/usb/dwc2/hcd.c +1 -9
-			dwc2_writel(hsotg, *data_buf, HCFIFO(chan->hc_num));
-	} else {
-		/* xfer_buf is not DWORD aligned */
-		for (i = 0; i < dword_count; i++, data_buf++) {
-			u32 data = data_buf[0] | data_buf[1] << 8 |
-				   data_buf[2] << 16 | data_buf[3] << 24;
-			dwc2_writel(hsotg, data, HCFIFO(chan->hc_num));
-		}
-	}
+	dwc2_writel_rep(hsotg, HCFIFO(chan->hc_num), data_buf, byte_count);

and here you are dropping the byteswap on big-endian.

Arnd

#24 lore raw
In reply to Christian Lamparter
Quoted 83 lines · Christian Lamparter
Quoted 82 lines · John Youn
Quoted 65 lines · Christian Lamparter
Quoted 40 lines · John Youn
Quoted 30 lines · Christian Lamparter
Quoted 29 lines · Arnd Bergmann
Quoted 16 lines · On Thursday 12 May 2016 11:58:18 Christian Lamparter wrote:
Quoted 10 lines
Quoted 7 lines
Quoted 4 lines

Detecting the endianess of the
device is probably the best future-proof solution, but it's also considerably more work to do in the driver, and comes with a tiny runtime overhead.

The runtime overhead is probably non-measurable compared with the cost of the actual MMIOs.

Right. The code size increase is probably measurable (but still small), the runtime overhead is not.

Ok, so no rebuts or complains have been posted.

I've tested the patch you made in: https://lkml.org/lkml/2016/5/9/354
and it works:

Quoted 8 lines

So, how do we go from here? There is are two small issues with the original patch (#ifdef DWC2_LOG_WRITES got converted to lower case:
#ifdef dwc2_log_writes) and I guess a proper subject would be nice.

Arnd, can you please respin and post it (cc'd stable as well)? So this is can be picked up? Or what's your plan?

(I just realized my reply was stuck in my outbox, so the patch went out first)

Quoted 8 lines
Quoted 4 lines

If I recall correctly, the rough consensus was to go with your longer patch in the future (fixed up for the comments that BenH and I sent), and I'd suggest basing it on top of a fixed version of my patch.

Well, but it comes with the "overhead"! So this was just as I said:
"Let's look at it and see if it's any good"... And I think it isn't since the usb/host/ehci people also opted for #ifdef CONFIG_BIG_ENDIAN archs etc...

Quoted 5 lines

I slightly prefer the more general patch for future kernel versions. The overhead will probably be negligible, but we can perform some testing to make sure.

Can you resubmit with all gathered feedback?

Yes, here are the changes.

I've tested it on my MyBook Live Duo. The usbotg comes right up:

[12610.540004] dwc2 4bff80000.usbotg: USB bus 1 deregistered
[12612.513934] dwc2 4bff80000.usbotg: Specified GNPTXFDEP=1024 > 256
[12612.518756] dwc2 4bff80000.usbotg: EPs: 3, shared fifos, 2042 entries in SPRAM
[12612.530112] dwc2 4bff80000.usbotg: DWC OTG Controller
[12612.533948] dwc2 4bff80000.usbotg: new USB bus registered, assigned bus number 1
[12612.540083] dwc2 4bff80000.usbotg: irq 33, io mem 0x00000000

John: Can you run some perf test with it?

I've based this on:

commit 6ea2fffc9057a67df1994d85a7c085d899eaa25a

Author: Arnd Bergmann <[email protected]>
Date:   Fri May 13 15:52:27 2016 +0200
Quoted 9 lines

usb: dwc2: fix regression on big-endian PowerPC/ARM systems

so naturally, it needs to be applied first. Most of the conversion work was done by the attached coccinelle semantic patches.

I had to edit the __bic32 and __orr32 helpers by hand. As well as some debugfs code and stuff in gadget.c.

Thanks Christian.

I'll keep this in our internal tree and send it to Felipe later. This causes a bunch of conflicts that I have to fix up and I should do a bit of testing as well.

Quoted 7 lines
Quoted 4 lines

And since there is a patch that fixes the regression this is can wait.

Regards,
John

---
Hey, that's really nice of you to do that :-D. Please keep me in the loop (Cc) for those then.

Sure no problem.

Quoted 11 lines

Yes, this needs definitely testing on all the affected ARCHs. I've attached a diff to a updated version of the patch. It drops the special MIPS case (as requested by Arnd).

BTW, I looked into the ioread32_rep and iowrite32_rep again. I'm not entirely convinced that the hardware FIFOs are actually endian neutral. But I can't verify it since my Western Digital My Book Live only supports the host configuration (forces host mode), so I don't know what a device in dual-mode or peripheral do here.

Quoted 7 lines

The reason why I think it was broken is because there's a PIO copy to and from the HCFIFO(x) in dwc2_hc_write_packet and dwc2_hc_read_packet access in the hcd.c file as well... And there, the code was using the dwc2_readl and dwc2_writel to access the data. I added special accessors for the FIFOS now:
dwc2_readl_rep and dwc2_writel_rep.

Hmmm, you could be right in that case. I'll have to check with the IP engineers and maybe try to run some tests on our platforms. So native access to the host FIFO will fail then? This platform is a BE CPU with the IP connected as LE, right?

Quoted 10 lines

I went all the way and implemented the helpers to do unaligned access if necessary (not sure if adding likely branches is a good idea, as this could be either always true or false for a specific driver the whole time).

NB: it also fixes a "regs variable not used in dwc2_hsotg_dump" warning if DEBUG isn't selected.

NB2: If it you need a patch against a specific tree, please let me know.

I can't provide you a tree to rebase on just yet. I'm hoping to get a few things queued for 4.8 and just apply this on top.

It would help if you could resend these as proper patches with a commit message and signed-off-by line, and the CONFIG_MIPS removal and compile warning fix merged in. And the fifo accessors in a separate patch. That way I can do any simple fix-ups if needed.

Regards,
John

#25 lore raw
In reply to Christian Lamparter
Quoted 5 lines · Christian Lamparter
Quoted 5 lines

http://www.linuxplumbersconf.org/2012/wp-content/uploads/2012/09/2012-lpc-ref-big-little-endian-herrenschmidt.odp

but there are at least two more twists that you completely missed here:

Quoted 12 lines
Quoted 4 lines

- Some architectures (e.g. ARMv5 "BE32" mode in IXP4xx, surely some others) do not implement big-endian mode by wiring up the data lines between the bus and the CPU differently between big- and little-endian mode like powerpc and armv7 "BE8" do, but instead they swizzle the *address* lines

Quoted 8 lines

  on 8-bit and 16-bit addresses. The effect of that is that normal RAM accesses work as expected both ways, and devices that are accessed using 32-bit MMIO ops never need any byteswap (you actually get "native endian") while MMIO with 8 and 16 bit width does something completely unexpected and touches the wrong register. Having an explicit byteswap on the PCI host bridge gets you the expected addresses again for 8-bit cycles but it also means that readl()/writel() again need to swap the data.

Right. Old PowerPC did that too and it's completely stupid. Thankfully most vendors grew a clue since then and this practice has slowly fallen into oblivion.

Quoted 6 lines
Quoted 6 lines

- Some other architectures (e.g. Broadcom MIPS) apparently are even fancier and use a strapping pin on the SoC flips the endianess of the CPU core at the same time as all the peripheral MMIO registers, with the intention of never requiring any byte swaps. I believe they are implemented careful enough to actually get this right, but it confuses the heck out of Linux drivers that don't expect this.

Right. Drivers like that will need an explicit test in the accessors.

Cheers,
Ben.

#26 lore raw
In reply to Felipe Balbi
Quoted 2 lines · Felipe Balbi

and patch all drivers similarly? Shouldn't arch/mips itself deal with it and hide it from drivers ?

Not sure what you mean, but we never had "endian neutral" accessors. It would be a bit of an endeavour and we already have so many accessors that adding more need a very strong justification.

Most IP blocks have a fixed endian...

Ben.

#27 lore raw
In reply to Arnd Bergmann
Quoted 5 lines · Arnd Bergmann


I think we can simply make this set of accessors architecture- dependent
(MIPS vs. the rest of the world) to revert ARM and PowerPC back to the working version.

Or use writel_be which mips seems to support...

Really, make it a BE vs. LE device test is a much better solution.

For now, since dwc2_readl() and writel don't take the device as an argument, you can make it a function of a compile time #define, or maybe a driver global, but the right way is really something like

	if (device_is_be())
		return readl_be(...)
	else
		return readl(...)

With the device_is_be() being temporarily set to true for MIPS for example, and later, a second pass, add the device argument and make it a device-flag initialized from the probe routine, possibly from the DT.

Cheers,
Ben.

Quoted 80 lines
drivers/usb/dwc2/core.h +0 -0
index 3c58d633ce80..1f8ed149a40f 100644
@@ -64,12 +64,24 @@
  	DWC2_TRACE_SCHEDULER_VB(pr_fmt("%s: SCH: " fmt),		
 \
  				dev_name(hsotg->dev), ##__VA_ARGS__)

drivers/usb/dwc2/core.h +12 -0
+
+#ifdef CONFIG_MIPS
+/*
+ * There are some MIPS machines that can run in either big-endian
+ * or little-endian mode and that use the dwc2 register without
+ * a byteswap in both ways.
+ * Unlike other architectures, MIPS does not require a barrier
+ * before the __raw_writel() to synchronize with DMA but does
+ * require the barrier after the writel() to serialize a series
+ * of writes. This set of operations was added specifically for
+ * MIPS and should only be used there.
+ */

 static inline u32 dwc2_readl(const void __iomem *addr) {
  u32 value = __raw_readl(addr);

drivers/usb/dwc2/core.h +0 -1
-	/* In order to preserve endianness __raw_* operation is

used. Therefore

drivers/usb/dwc2/core.h +0 -1
-	 * a barrier is needed to ensure IO access is not re-ordered 

across

drivers/usb/dwc2/core.h +1 -0
+	/* in order to preserve endianness __raw_* operation is

used. therefore

drivers/usb/dwc2/core.h +1 -0
+	 * a barrier is needed to ensure io access is not re-ordered 

across
   * reads or writes
   */
  mb();

drivers/usb/dwc2/core.h +0 -0
@@ -81,15 +93,32 @@ static inline void dwc2_writel(u32 value, void
 __iomem *addr)
  	__raw_writel(value, addr);


  /*

drivers/usb/dwc2/core.h +0 -1
-	 * In order to preserve endianness __raw_* operation is

used. Therefore

drivers/usb/dwc2/core.h +0 -1
-	 * a barrier is needed to ensure IO access is not re-ordered 

across

drivers/usb/dwc2/core.h +1 -0
+	 * in order to preserve endianness __raw_* operation is

used. therefore

drivers/usb/dwc2/core.h +1 -0
+	 * a barrier is needed to ensure io access is not re-ordered 

across
   * reads or writes
   */
  mb();

drivers/usb/dwc2/core.h +2 -2
-#ifdef DWC2_LOG_WRITES
-	pr_info("INFO:: wrote %08x to %p\n", value, addr);
+#ifdef dwc2_log_writes
+	pr_info("info:: wrote %08x to %p\n", value, addr);

 #endif
 }

drivers/usb/dwc2/core.h +17 -0
+#else
+/* Normal architectures just use readl/write */
+static inline u32 dwc2_readl(const void __iomem *addr)
+{
+	u32 value = readl(addr);
+	return value;
+}
+
+static inline void dwc2_writel(u32 value, void __iomem *addr)
+{
+	writel(value, addr);
+
+#ifdef dwc2_log_writes
+	pr_info("info:: wrote %08x to %p\n", value, addr);
+#endif
+}
+#endif


 /* Maximum number of Endpoints/HostChannels */
 #define MAX_EPS_CHANNELS 16

#28 lore raw
In reply to Arnd Bergmann
Quoted 29 lines · Arnd Bergmann
Quoted 22 lines · On Monday 09 May 2016 10:23:22 Benjamin Herrenschmidt wrote:
Quoted 14 lines · Christian Lamparter
Quoted 9 lines · Benjamin Herrenschmidt
Quoted 6 lines · Christian Lamparter via Linuxppc-dev

I've been looking in getting the MyBook Live Duo's USB OTG port to function. The SoC is a APM82181. Which has a PowerPC 464 core and related to the supported canyonlands architecture in arch/powerpc/.

Quoted 4 lines
Quoted 1 line

Currently in -next the dwc2 module doesn't load:

Smells like the APM implementation is little endian. You might need to use a flag to indicate what endian to use instead and set it appropriately based on some DT properties.

Quoted 6 lines

I tried. As per common-properties[0], I added little-endian; but it has no effect. I looked in dwc2_driver_probe and found no way of specifying the endian of the device. It all comes down to the dwc2_readl & dwc2_writel accessors. These - sadly - have been hardwired to use __raw_readl and __raw_writel. So, it's always "native-endian". While common-properties says little-endian should be preferred.

Quoted 5 lines

Right, I meant, you should produce a patch adding a runtime test inside those functions based on a device-tree property, a bit like we do for some of the HCDs like OHCI, EHCI etc...

Quoted 5 lines

The patch that caused the problem had multiple issues:

- it broke big-endian ARM kernels: any machine that was working correctly with a little-endian kernel is no longer using byteswaps on big-endian kernels, which clearly breaks them.

I'm a bit confused about how this is supposed to work. My understanding was that the readl() and writel() are defined as little endian. So byte-swapping was performed if the architecture is big endian. And the raw versions never swapped, always using the "native" endianness.

dwc2 is always treating the result of readl/writel as if it was read in native endian. So it needs to read the registers in big-endian on big-endian systems.

This was the premise on which this patch was made.

So for big endian systems, isn't what we want is to read in big-endian without any byteswapping to little-endian? But your saying this breaks big-endian ARM systems as well. Am I missing something?

The rest of the feedback makes sense. Just confused about this bit.

Regards,
John

#29 lore raw
Quoted 43 lines · On Monday 09 May 2016 13:22:48 John Youn wrote:
Quoted 29 lines · Arnd Bergmann
Quoted 22 lines · On Monday 09 May 2016 10:23:22 Benjamin Herrenschmidt wrote:
Quoted 14 lines · Christian Lamparter
Quoted 9 lines · Benjamin Herrenschmidt
Quoted 6 lines · Christian Lamparter via Linuxppc-dev

I've been looking in getting the MyBook Live Duo's USB OTG port to function. The SoC is a APM82181. Which has a PowerPC 464 core and related to the supported canyonlands architecture in arch/powerpc/.

Quoted 4 lines
Quoted 1 line

Currently in -next the dwc2 module doesn't load:

Smells like the APM implementation is little endian. You might need to use a flag to indicate what endian to use instead and set it appropriately based on some DT properties.

Quoted 6 lines

I tried. As per common-properties[0], I added little-endian; but it has no effect. I looked in dwc2_driver_probe and found no way of specifying the endian of the device. It all comes down to the dwc2_readl & dwc2_writel accessors. These - sadly - have been hardwired to use __raw_readl and __raw_writel. So, it's always "native-endian". While common-properties says little-endian should be preferred.

Quoted 5 lines

Right, I meant, you should produce a patch adding a runtime test inside those functions based on a device-tree property, a bit like we do for some of the HCDs like OHCI, EHCI etc...

Quoted 5 lines

The patch that caused the problem had multiple issues:

- it broke big-endian ARM kernels: any machine that was working correctly with a little-endian kernel is no longer using byteswaps on big-endian kernels, which clearly breaks them.

I'm a bit confused about how this is supposed to work. My understanding was that the readl() and writel() are defined as little endian. So byte-swapping was performed if the architecture is big endian. And the raw versions never swapped, always using the "native" endianness.

Quoted 3 lines

dwc2 is always treating the result of readl/writel as if it was read in native endian. So it needs to read the registers in big-endian on big-endian systems.

The hardware has no idea of what endianess the CPU uses at any given time, it's fixed by the SoC design, so there is no such thing as "native" endianess for a random IP block.

The readl/writel accessors accomodate for that by swapping the data on big-endian kernels, because most SoC designers tend to pick little-endian device registers by default.

Quoted 5 lines

This was the premise on which this patch was made.

So for big endian systems, isn't what we want is to read in big-endian without any byteswapping to little-endian? But your saying this breaks big-endian ARM systems as well. Am I missing something?

The systems are not a particular endianess, only the current state of the CPU is, and that may change independent of the way the hardware block got synthesized. We don't support swapping endianess at runtime in Linux, but the system normally doesn't care what we run.

The normal behavior is for the register contents to be read as little-endian, and then swapped on big-endian kernel builds to match what the kernel expects.

MIPS is a special case here, because the endianess of the CPU core is fixed in hardware (or using a strapping pin) and is often tied to the endianess of all the IP blocks. There are a couple of other architectures like this (e.g. ARM ixp4xx, but none of the modern ARM systems).

Arnd

#30 lore raw
Quoted 72 lines · Arnd Bergmann
Quoted 43 lines · On Monday 09 May 2016 13:22:48 John Youn wrote:
Quoted 29 lines · Arnd Bergmann
Quoted 22 lines · On Monday 09 May 2016 10:23:22 Benjamin Herrenschmidt wrote:
Quoted 14 lines · Christian Lamparter
Quoted 9 lines · Benjamin Herrenschmidt
Quoted 6 lines · Christian Lamparter via Linuxppc-dev

I've been looking in getting the MyBook Live Duo's USB OTG port to function. The SoC is a APM82181. Which has a PowerPC 464 core and related to the supported canyonlands architecture in arch/powerpc/.

Quoted 4 lines
Quoted 1 line

Currently in -next the dwc2 module doesn't load:

Smells like the APM implementation is little endian. You might need to use a flag to indicate what endian to use instead and set it appropriately based on some DT properties.

Quoted 6 lines

I tried. As per common-properties[0], I added little-endian; but it has no effect. I looked in dwc2_driver_probe and found no way of specifying the endian of the device. It all comes down to the dwc2_readl & dwc2_writel accessors. These - sadly - have been hardwired to use __raw_readl and __raw_writel. So, it's always "native-endian". While common-properties says little-endian should be preferred.

Quoted 5 lines

Right, I meant, you should produce a patch adding a runtime test inside those functions based on a device-tree property, a bit like we do for some of the HCDs like OHCI, EHCI etc...

Quoted 5 lines

The patch that caused the problem had multiple issues:

- it broke big-endian ARM kernels: any machine that was working correctly with a little-endian kernel is no longer using byteswaps on big-endian kernels, which clearly breaks them.

I'm a bit confused about how this is supposed to work. My understanding was that the readl() and writel() are defined as little endian. So byte-swapping was performed if the architecture is big endian. And the raw versions never swapped, always using the "native" endianness.

Quoted 3 lines

dwc2 is always treating the result of readl/writel as if it was read in native endian. So it needs to read the registers in big-endian on big-endian systems.

The hardware has no idea of what endianess the CPU uses at any given time, it's fixed by the SoC design, so there is no such thing as "native" endianess for a random IP block.

The readl/writel accessors accomodate for that by swapping the data on big-endian kernels, because most SoC designers tend to pick little-endian device registers by default.

Quoted 5 lines

This was the premise on which this patch was made.

So for big endian systems, isn't what we want is to read in big-endian without any byteswapping to little-endian? But your saying this breaks big-endian ARM systems as well. Am I missing something?

The systems are not a particular endianess, only the current state of the CPU is, and that may change independent of the way the hardware block got synthesized. We don't support swapping endianess at runtime in Linux, but the system normally doesn't care what we run.

The normal behavior is for the register contents to be read as little-endian, and then swapped on big-endian kernel builds to match what the kernel expects.

Quoted 5 lines

MIPS is a special case here, because the endianess of the CPU core is fixed in hardware (or using a strapping pin) and is often tied to the endianess of all the IP blocks. There are a couple of other architectures like this (e.g. ARM ixp4xx, but none of the modern ARM systems).

Ok thanks. What you're saying is clear now.

Is there a standard way to handle this? Must all drivers either check some endianness configuration or do a runtime check?

Regards,
John

#31 lore raw
Quoted 18 lines · On Monday 09 May 2016 14:11:23 John Youn wrote:
Quoted 10 lines · Arnd Bergmann

The systems are not a particular endianess, only the current state of the CPU is, and that may change independent of the way the hardware block got synthesized. We don't support swapping endianess at runtime in Linux, but the system normally doesn't care what we run.

The normal behavior is for the register contents to be read as little-endian, and then swapped on big-endian kernel builds to match what the kernel expects.

Quoted 5 lines

MIPS is a special case here, because the endianess of the CPU core is fixed in hardware (or using a strapping pin) and is often tied to the endianess of all the IP blocks. There are a couple of other architectures like this (e.g. ARM ixp4xx, but none of the modern ARM systems).

Quoted 4 lines

Ok thanks. What you're saying is clear now.

Is there a standard way to handle this? Must all drivers either check some endianness configuration or do a runtime check?

There are four common approaches:

1 hardcode a particular endianess because an IP block is always
used the same way

2 pick the right endianess at compile-time because we know
what we are building for (typically by CPU architecture, but there are some other ways)

3 do runtime configuration based on a DT property or platform data

4 do automatic runtime configuration based on well-known register
contents

Approach 1 is the most common, in particular as most IP blocks are not configurable and are not used on big-endian MIPS machines.

Approach 4 as implemented by Christian Lamparter's patch is the most reliable way, and probably makes most sense for DesignWare IP blocks in general, because they are configurable and may get used in all kinds of systems.

Approaches 2 and 3 are somewhat inferior because you have to rely on everyone else doing it the way you planned, but 2 is particularly easy and 3 is used when there is no obvious way to detect endianess of the device.

The patch I suggested would be approach 2, and as Felipe correctly mentioned, it is not ideal, but it may be more appropriate for backports to the 4.4-longterm, 4.5-stable and 4.6-stable kernels than the more elaborate patch we are now discussing.

Arnd