Skip to contentSkip to conversation

clocksource: rtl-otto: Change driver to use __raw reads and writes

PATCH Superseded Rustam Adilov 3 participants 10 messages May 11 – Jun 20 Clocksource, clockevent drivers

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 clocksource: rtl-otto: Change driver to use __raw reads and writes

As it stands, the driver uses ioread32 and iowrite32 for register access and it works fine. However this stops working when the SWAP_IO_SPACE config is enabled as this drivers expects ioread32 and iowrite32 to be in native endian (that is big endian for currently supported SoCs). RTL9607C is a big endian MIPS SoC that has identical timer as the already supported chips but needs to have SWAP_IO_SPACE to have a functioning little endian USB host.

Fix this by replacing all instances of ioread32 and iowrite32 with __raw_readl and __raw_writel variants. Since they essentially do the same register access, this shouldn't affect anything on other machines.

---
 drivers/clocksource/timer-rtl-otto.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
drivers/clocksource/timer-rtl-otto.c +7 -7
index 6113d2fdd4de..bcb449ef0481 100644
@@ -56,37 +56,37 @@ struct rttm_cs {
 /* Simple internal register functions */
 static inline unsigned int rttm_get_counter(void __iomem *base)
 {
-	return ioread32(base + RTTM_CNT);
+	return __raw_readl(base + RTTM_CNT);
 }
 
 static inline void rttm_set_period(void __iomem *base, unsigned int period)
 {
-	iowrite32(period, base + RTTM_DATA);
+	__raw_writel(period, base + RTTM_DATA);
 }
 
 static inline void rttm_disable_timer(void __iomem *base)
 {
-	iowrite32(0, base + RTTM_CTRL);
+	__raw_writel(0, base + RTTM_CTRL);
 }
 
 static inline void rttm_enable_timer(void __iomem *base, u32 mode, u32 divisor)
 {
-	iowrite32(RTTM_CTRL_ENABLE | mode | divisor, base + RTTM_CTRL);
+	__raw_writel(RTTM_CTRL_ENABLE | mode | divisor, base + RTTM_CTRL);
 }
 
 static inline void rttm_ack_irq(void __iomem *base)
 {
-	iowrite32(ioread32(base + RTTM_INT) | RTTM_INT_PENDING, base + RTTM_INT);
+	__raw_writel(__raw_readl(base + RTTM_INT) | RTTM_INT_PENDING, base + RTTM_INT);
 }
 
 static inline void rttm_enable_irq(void __iomem *base)
 {
-	iowrite32(RTTM_INT_ENABLE, base + RTTM_INT);
+	__raw_writel(RTTM_INT_ENABLE, base + RTTM_INT);
 }
 
 static inline void rttm_disable_irq(void __iomem *base)
 {
-	iowrite32(0, base + RTTM_INT);
+	__raw_writel(0, base + RTTM_INT);
 }
 
 /* Aggregated control functions for kernel clock framework */
Signature · 1 line
2.54.0
#1 lore raw
Quoted 57 lines · Rustam Adilov

As it stands, the driver uses ioread32 and iowrite32 for register access and it works fine. However this stops working when the SWAP_IO_SPACE config is enabled as this drivers expects ioread32 and iowrite32 to be in native endian (that is big endian for currently supported SoCs). RTL9607C is a big endian MIPS SoC that has identical timer as the already supported chips but needs to have SWAP_IO_SPACE to have a functioning little endian USB host.

Fix this by replacing all instances of ioread32 and iowrite32 with __raw_readl and __raw_writel variants. Since they essentially do the same register access, this shouldn't affect anything on other machines.

---
 drivers/clocksource/timer-rtl-otto.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
drivers/clocksource/timer-rtl-otto.c +5 -6
index 6113d2fdd4de..bcb449ef0481 100644
@@ -56,37 +56,37 @@ struct rttm_cs {
 /* Simple internal register functions */
 static inline unsigned int rttm_get_counter(void __iomem *base)
 {
-	return ioread32(base + RTTM_CNT);
+	return __raw_readl(base + RTTM_CNT);
 }
 
 static inline void rttm_set_period(void __iomem *base, unsigned int period)
 {
-	iowrite32(period, base + RTTM_DATA);
+	__raw_writel(period, base + RTTM_DATA);
 }
 
 static inline void rttm_disable_timer(void __iomem *base)
 {
-	iowrite32(0, base + RTTM_CTRL);
+	__raw_writel(0, base + RTTM_CTRL);
 }
 
 static inline void rttm_enable_timer(void __iomem *base, u32 mode, u32 divisor)
 {
-	iowrite32(RTTM_CTRL_ENABLE | mode | divisor, base + RTTM_CTRL);
+	__raw_writel(RTTM_CTRL_ENABLE | mode | divisor, base + RTTM_CTRL);
 }
 
 static inline void rttm_ack_irq(void __iomem *base)
 {
-	iowrite32(ioread32(base + RTTM_INT) | RTTM_INT_PENDING, base + RTTM_INT);
+	__raw_writel(__raw_readl(base + RTTM_INT) | RTTM_INT_PENDING, base + RTTM_INT);
 }
 
 static inline void rttm_enable_irq(void __iomem *base)
 {
-	iowrite32(RTTM_INT_ENABLE, base + RTTM_INT);

drivers/clocksource/timer-rtl-otto.c struct rttm_cs { · line 92

Quoted 10 lines
drivers/clocksource/timer-rtl-otto.c +2 -1
+	__raw_writel(RTTM_INT_ENABLE, base + RTTM_INT);
 }
 
 static inline void rttm_disable_irq(void __iomem *base)
 {
-	iowrite32(0, base + RTTM_INT);
+	__raw_writel(0, base + RTTM_INT);
 }
 
 /* Aggregated control functions for kernel clock framework */

It appears the get_maintainer.pl script didn't catch the Chris Packham, presumably because i didn't touch the device bindings file. Added them to CC for review.

Reviewed-by
#2 lore raw

Hi Rustam,

Quoted 67 lines · Rustam Adilov
Quoted 66 lines · Rustam Adilov

As it stands, the driver uses ioread32 and iowrite32 for register access and it works fine. However this stops working when the SWAP_IO_SPACE config is enabled as this drivers expects ioread32 and iowrite32 to be in native endian (that is big endian for currently supported SoCs). RTL9607C is a big endian MIPS SoC that has identical timer as the already supported chips but needs to have SWAP_IO_SPACE to have a functioning little endian USB host.

Fix this by replacing all instances of ioread32 and iowrite32 with __raw_readl and __raw_writel variants. Since they essentially do the same register access, this shouldn't affect anything on other machines.

---
  drivers/clocksource/timer-rtl-otto.c | 14 +++++++-------
  1 file changed, 7 insertions(+), 7 deletions(-)
drivers/clocksource/timer-rtl-otto.c +7 -7
index 6113d2fdd4de..bcb449ef0481 100644
@@ -56,37 +56,37 @@ struct rttm_cs {
  /* Simple internal register functions */
  static inline unsigned int rttm_get_counter(void __iomem *base)
  {
-	return ioread32(base + RTTM_CNT);
+	return __raw_readl(base + RTTM_CNT);
  }
 
  static inline void rttm_set_period(void __iomem *base, unsigned int period)
  {
-	iowrite32(period, base + RTTM_DATA);
+	__raw_writel(period, base + RTTM_DATA);
  }
 
  static inline void rttm_disable_timer(void __iomem *base)
  {
-	iowrite32(0, base + RTTM_CTRL);
+	__raw_writel(0, base + RTTM_CTRL);
  }
 
  static inline void rttm_enable_timer(void __iomem *base, u32 mode, u32 divisor)
  {
-	iowrite32(RTTM_CTRL_ENABLE | mode | divisor, base + RTTM_CTRL);
+	__raw_writel(RTTM_CTRL_ENABLE | mode | divisor, base + RTTM_CTRL);
  }
 
  static inline void rttm_ack_irq(void __iomem *base)
  {
-	iowrite32(ioread32(base + RTTM_INT) | RTTM_INT_PENDING, base + RTTM_INT);
+	__raw_writel(__raw_readl(base + RTTM_INT) | RTTM_INT_PENDING, base + RTTM_INT);
  }
 
  static inline void rttm_enable_irq(void __iomem *base)
  {
-	iowrite32(RTTM_INT_ENABLE, base + RTTM_INT);
+	__raw_writel(RTTM_INT_ENABLE, base + RTTM_INT);
  }
 
  static inline void rttm_disable_irq(void __iomem *base)
  {
-	iowrite32(0, base + RTTM_INT);
+	__raw_writel(0, base + RTTM_INT);
  }
 
Quoted 3 lines

drivers/clocksource/timer-rtl-otto.c struct rttm_cs { · line 92

Quoted 1 line
drivers/clocksource/timer-rtl-otto.c +0 -0
  /* Aggregated control functions for kernel clock framework */

It appears the get_maintainer.pl script didn't catch the Chris Packham, presumably because i didn't touch the device bindings file. Added them to CC for review.

No problem.  Has --git been dropped by default from get_maintainer.pl? It doesn't actually help as even when explicitly using it that doesn't seem to get me (initial commit ignored?), it does find Markus who I've added to the Cc.

#3 lore raw
Quoted 10 lines

Von: Chris Packham <[email protected]>
Gesendet: Mittwoch, 13. Mai 2026 23:05
Betreff: Re: [PATCH] clocksource: rtl-otto: Change driver to use __raw reads and writes
...

Quoted 6 lines

Fix this by replacing all instances of ioread32 and iowrite32 with __raw_readl and __raw_writel variants. Since they essentially do the same register access, this shouldn't affect anything on other machines.

IIRC the current functions use memory barriers while the new ones do not. Timers are critical and cost me a lot of time in the past [1]. We nearly lost support for several devices last year. So I like to have at least some stability confirmation from downstream.

Markus

[1] https://lkml.org/lkml/2025/8/4/240

#4 lore raw

Hi Markus,

Quoted 9 lines · On 2026-05-14 20:50, [email protected] wrote:
Quoted 9 lines

Von: Chris Packham <[email protected]>
Gesendet: Mittwoch, 13. Mai 2026 23:05
Betreff: Re: [PATCH] clocksource: rtl-otto: Change driver to use __raw reads and writes
...

Quoted 5 lines

Fix this by replacing all instances of ioread32 and iowrite32 with __raw_readl and __raw_writel variants. Since they essentially do the same register access, this shouldn't affect anything on other machines.

Quoted 7 lines
Quoted 1 line
Quoted 1 line

IIRC the current functions use memory barriers while the new ones do not. Timers are critical and cost me a lot of time in the past [1]. We nearly lost support for several devices last year. So I like to have at least some stability confirmation from downstream.

As far as i can see, if you untangle the the BUILDIO_MEM(l, u32) in [1] you should see they do have barriers as denoted by barrier argument being set. So that means, in MIPS, both readl/writel are the same as their __raw variants. And both ioread32/iowrite32 are using readl/writel under the hood so...

[1] - https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/arch/mips/include/asm/io.h

Quoted 3 lines

Markus

[1] https://lkml.org/lkml/2025/8/4/240

Best,
Rustam

#5 lore raw
Quoted 4 lines

Von: Rustam Adilov <[email protected]>
Gesendet: Freitag, 15. Mai 2026 16:33
An: [email protected]
Betreff: Re: AW: [PATCH] clocksource: rtl-otto: Change driver to use __raw

reads and writes

Quoted 9 lines

...

Quoted 5 lines

IIRC the current functions use memory barriers while the new ones do not. Timers are critical and cost me a lot of time in the past [1]. We nearly lost support for several devices last year. So I like to have at least some stability confirmation from downstream.

As far as i can see, if you untangle the the BUILDIO_MEM(l, u32) in [1] you should see they do have barriers as denoted by barrier argument being

set.

Quoted 1 line

So that means, in MIPS, both readl/writel are the same as their __raw

variants.

Quoted 1 line

And both ioread32/iowrite32 are using readl/writel under the hood so...

Hi,

will the __relaxed functions serve your IO_SWAP scenario too?

Markus

#6 lore raw
Quoted 16 lines · On 2026-05-15 17:18, [email protected] wrote:
Quoted 4 lines

Von: Rustam Adilov <[email protected]>
Gesendet: Freitag, 15. Mai 2026 16:33
An: [email protected]
Betreff: Re: AW: [PATCH] clocksource: rtl-otto: Change driver to use __raw

reads and writes

Quoted 9 lines

...

Quoted 5 lines

IIRC the current functions use memory barriers while the new ones do not. Timers are critical and cost me a lot of time in the past [1]. We nearly lost support for several devices last year. So I like to have at least some stability confirmation from downstream.

As far as i can see, if you untangle the the BUILDIO_MEM(l, u32) in [1] you should see they do have barriers as denoted by barrier argument being

set.

Quoted 1 line

So that means, in MIPS, both readl/writel are the same as their __raw

Quoted 8 lines

variants.

Quoted 1 line

And both ioread32/iowrite32 are using readl/writel under the hood so...

Hi,

will the __relaxed functions serve your IO_SWAP scenario too?

Markus

Not really because the readl_relaxed and readl appear to be both mapped to ioswabl which changes behavior as per IO_SWAP config [1].

[1] - https://elixir.bootlin.com/linux/v7.0.6/source/arch/mips/include/asm/io.h#L49

#7 lore raw
Quoted 4 lines

Von: Rustam Adilov <[email protected]>
Gesendet: Freitag, 15. Mai 2026 20:21
An: [email protected]
Betreff: Re: AW: AW: [PATCH] clocksource: rtl-otto: Change driver to use

__raw reads and writes

Quoted 6 lines · On 2026-05-15 17:18, [email protected] wrote:
Quoted 4 lines
Quoted 4 lines

Von: Rustam Adilov <[email protected]>
Gesendet: Freitag, 15. Mai 2026 16:33
An: [email protected]
Betreff: Re: AW: [PATCH] clocksource: rtl-otto: Change driver to use

__raw

Quoted 10 lines
Quoted 10 lines

reads and writes

Quoted 9 lines

...

Quoted 5 lines

IIRC the current functions use memory barriers while the new ones do not. Timers are critical and cost me a lot of time in the past [1]. We nearly lost support for several devices last year. So I like to have at least some stability confirmation from downstream.

As far as i can see, if you untangle the the BUILDIO_MEM(l, u32) in [1] you should see they do have barriers as denoted by barrier argument

being

Quoted 5 lines
Quoted 5 lines

set.

Quoted 1 line

So that means, in MIPS, both readl/writel are the same as their __raw

variants.

Quoted 1 line

And both ioread32/iowrite32 are using readl/writel under the hood so...

Quoted 7 lines
Quoted 5 lines

Hi,

will the __relaxed functions serve your IO_SWAP scenario too?

Markus

Not really because the readl_relaxed and readl appear to be both mapped to

ioswabl

Quoted 1 line

which changes behavior as per IO_SWAP config [1].

My two cents regarding the real issue behind this discussion. I'm still convinced that CONFIG_SWAP_IO_SPACE is not the right way to get an USB driver working. Rewriting every other driver around that just feels wrong.

Coming back to this. If it produces the same assembler code for the big endian Realtek Otto platform then I'm ok with this. I expect an immediate downstream backport if it gets accepted upstream.

Markus

#8 lore raw
Quoted 34 lines · On 2026-05-15 18:39, [email protected] wrote:
Quoted 4 lines

Von: Rustam Adilov <[email protected]>
Gesendet: Freitag, 15. Mai 2026 20:21
An: [email protected]
Betreff: Re: AW: AW: [PATCH] clocksource: rtl-otto: Change driver to use

__raw reads and writes

Quoted 6 lines · On 2026-05-15 17:18, [email protected] wrote:
Quoted 4 lines
Quoted 4 lines

Von: Rustam Adilov <[email protected]>
Gesendet: Freitag, 15. Mai 2026 16:33
An: [email protected]
Betreff: Re: AW: [PATCH] clocksource: rtl-otto: Change driver to use

__raw

Quoted 10 lines
Quoted 10 lines

reads and writes

Quoted 9 lines

...

Quoted 5 lines

IIRC the current functions use memory barriers while the new ones do not. Timers are critical and cost me a lot of time in the past [1]. We nearly lost support for several devices last year. So I like to have at least some stability confirmation from downstream.

As far as i can see, if you untangle the the BUILDIO_MEM(l, u32) in [1] you should see they do have barriers as denoted by barrier argument

being

Quoted 11 lines
Quoted 10 lines

set.

Quoted 1 line

So that means, in MIPS, both readl/writel are the same as their __raw

variants.

Quoted 1 line

And both ioread32/iowrite32 are using readl/writel under the hood so...

Hi,

will the __relaxed functions serve your IO_SWAP scenario too?

Markus

Quoted 8 lines
Quoted 1 line

Not really because the readl_relaxed and readl appear to be both mapped to

ioswabl

Quoted 1 line

which changes behavior as per IO_SWAP config [1].

My two cents regarding the real issue behind this discussion. I'm still convinced that CONFIG_SWAP_IO_SPACE is not the right way to get an USB driver working. Rewriting every other driver around that just feels wrong.

I mean, it is either this or hacking up the general usb reads and writes functions with this atrociousness:

static inline unsigned int ehci_readl(const struct ehci_hcd *ehci,
__u32 __iomem *regs)
{
+#if defined(CONFIG_RTK_MIPS_SOC)
+ return (le32_to_cpu((*(volatile unsigned long *)(regs))));
+#else
#ifdef CONFIG_USB_EHCI_BIG_ENDIAN_MMIO
return ehci_big_endian_mmio(ehci) ?
readl_be(regs) :
@@ -746,6 +749,7 @@
#else
return readl(regs);
#endif
+#endif
}

static inline void ehci_writel(const struct ehci_hcd *ehci,
const unsigned int val, __u32 __iomem *regs)
{
+#if defined(CONFIG_RTK_MIPS_SOC)
+ ((*(volatile unsigned long *)(regs))=cpu_to_le32(val));
+#else
#ifdef CONFIG_USB_EHCI_BIG_ENDIAN_MMIO
ehci_big_endian_mmio(ehci) ?
writel_be(val, regs) :
@@ -773,6 +780,7 @@
else
writel(val, regs);
#endif
+#endif
}

And i don't think anyone upstream is gonna be okay with it. Obviously that's straight from Realtek code and we can slightly improve it but the underlying issue is still gonna be there.

With all that, I believe CONFIG_SWAP_IO_SPACE is a good long term solution to this endian mess and multiple big endian MIPS SoCs use it too like Lantiq and Broadcom. The happy side effect is rewriting existing drivers, which i'll call as a good thing personally.

Quoted 6 lines

Coming back to this. If it produces the same assembler code for the big endian Realtek Otto platform then I'm ok with this. I expect an immediate downstream backport if it gets accepted upstream.

Markus

Best,
Rustam