Skip to contentSkip to conversation

arm: imx: initialize the system counter earlier on i.MX7

PATCH In review Emanuele Ghidoli 2 participants 3 messages Jul 30 – Aug 3

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

PATCH 1/1 arm: imx: initialize the system counter earlier on i.MX7

From: Emanuele Ghidoli <[email protected]>

imx_gpcv2_init() calls udelay(), which through schedule() reaches cyclic_run(). Since commit 9c1b13b3fd27 ("cyclic: reduce get_timer_us() calls inside hlist_for_each_entry_safe()") get_timer_us() is called even when no cyclic function is registered, so tick_to_time_us() divides by a still zero CNTFRQ and __div0() hangs the board before the console is up. The requested delay was a no-op as well, since us_to_tick() reads a CNTFRQ of zero.

Initialize the system counter before imx_gpcv2_init(), and move the "already initialized" guard from timer_get_boot_us() into timer_init() so that the later timer_init() from the generic init sequence is a no-op. Where the system counter timer_init() is not compiled in, the weak definition is empty and the calling it is harmless.

Fixes b059837850e4 ("imx: mx7: add gpc initialization for low power mode")
Fixes 9c1b13b3fd27 ("cyclic: reduce get_timer_us() calls inside hlist_for_each_entry_safe()")
---
 arch/arm/mach-imx/mx7/soc.c    | 6 ++++++
 arch/arm/mach-imx/syscounter.c | 6 ++++--
 2 files changed, 10 insertions(+), 2 deletions(-)
arch/arm/mach-imx/mx7/soc.c +6 -0
index e504c1fd52a0..8a858dea5187 100644
@@ -331,6 +331,12 @@ int arch_cpu_init(void)
 
 	init_snvs();
 
+	/*
+	 * imx_gpcv2_init() needs to do a delay.
+	 * Timer must be initialized before calling a delay function.
+	 */
+	timer_init();
+
 	imx_gpcv2_init();
 
 	enable_ca7_smp();
arch/arm/mach-imx/syscounter.c +4 -2
index 96fe2c7c17bb..86311d0cc07d 100644
@@ -65,6 +65,9 @@ int timer_init(void)
 	struct sctr_regs *sctr = (struct sctr_regs *)SCTR_BASE_ADDR;
 	unsigned long val, freq;
 
+	if (gd->arch.timer_rate_hz)
+		return 0;
+
 	freq = CFG_SC_TIMER_CLK;
 	asm volatile("mcr p15, 0, %0, c14, c0, 0" : : "r" (freq));
 
@@ -103,8 +106,7 @@ ulong get_timer(ulong base)
 
 ulong timer_get_boot_us(void)
 {
-	if (!gd->arch.timer_rate_hz)
-		timer_init();
+	timer_init();
 
 	return tick_to_time(get_ticks());
 }
Signature · 1 line
2.43.0
#1 lore raw

On 7/30/26 11:35 AM, Emanuele Ghidoli wrote:

Hello Emanuele,

Quoted 54 lines

imx_gpcv2_init() calls udelay(), which through schedule() reaches cyclic_run(). Since commit 9c1b13b3fd27 ("cyclic: reduce get_timer_us() calls inside hlist_for_each_entry_safe()") get_timer_us() is called even when no cyclic function is registered, so tick_to_time_us() divides by a still zero CNTFRQ and __div0() hangs the board before the console is up. The requested delay was a no-op as well, since us_to_tick() reads a CNTFRQ of zero.

Initialize the system counter before imx_gpcv2_init(), and move the "already initialized" guard from timer_get_boot_us() into timer_init() so that the later timer_init() from the generic init sequence is a no-op. Where the system counter timer_init() is not compiled in, the weak definition is empty and the calling it is harmless.

Fixes b059837850e4 ("imx: mx7: add gpc initialization for low power mode")
Fixes 9c1b13b3fd27 ("cyclic: reduce get_timer_us() calls inside hlist_for_each_entry_safe()")
---
  arch/arm/mach-imx/mx7/soc.c    | 6 ++++++
  arch/arm/mach-imx/syscounter.c | 6 ++++--
  2 files changed, 10 insertions(+), 2 deletions(-)
arch/arm/mach-imx/mx7/soc.c +6 -0
index e504c1fd52a0..8a858dea5187 100644
@@ -331,6 +331,12 @@ int arch_cpu_init(void)
 
  	init_snvs();
 
+	/*
+	 * imx_gpcv2_init() needs to do a delay.
+	 * Timer must be initialized before calling a delay function.
+	 */
+	timer_init();
+
  	imx_gpcv2_init();
 
  	enable_ca7_smp();
arch/arm/mach-imx/syscounter.c +3 -0
index 96fe2c7c17bb..86311d0cc07d 100644
@@ -65,6 +65,9 @@ int timer_init(void)
  	struct sctr_regs *sctr = (struct sctr_regs *)SCTR_BASE_ADDR;
  	unsigned long val, freq;
 
+	if (gd->arch.timer_rate_hz)
+		return 0;
+
  	freq = CFG_SC_TIMER_CLK;
  	asm volatile("mcr p15, 0, %0, c14, c0, 0" : : "r" (freq));
 

arch/arm/mach-imx/syscounter.c ulong get_timer(ulong base) · line 112

Quoted 9 lines
arch/arm/mach-imx/syscounter.c +1 -2
@@ -103,8 +106,7 @@ ulong get_timer(ulong base)
 
  ulong timer_get_boot_us(void)
  {
-	if (!gd->arch.timer_rate_hz)
-		timer_init();
+	timer_init();
 
  	return tick_to_time(get_ticks());
  }

+CC Peng

The patch looks OK, thank you!

(Could you also send one for the MX6 ?)

Reviewed-by Stefan Roese on v1 1/1
#2 lore raw
In reply to Emanuele Ghidoli
Quoted 16 lines · Emanuele Ghidoli

From: Emanuele Ghidoli <[email protected]>

imx_gpcv2_init() calls udelay(), which through schedule() reaches cyclic_run(). Since commit 9c1b13b3fd27 ("cyclic: reduce get_timer_us() calls inside hlist_for_each_entry_safe()") get_timer_us() is called even when no cyclic function is registered, so tick_to_time_us() divides by a still zero CNTFRQ and __div0() hangs the board before the console is up. The requested delay was a no-op as well, since us_to_tick() reads a CNTFRQ of zero.

Initialize the system counter before imx_gpcv2_init(), and move the "already initialized" guard from timer_get_boot_us() into timer_init() so that the later timer_init() from the generic init sequence is a no-op. Where the system counter timer_init() is not compiled in, the weak definition is empty and the calling it is harmless.

Quoted 3 lines
Fixes b059837850e4 ("imx: mx7: add gpc initialization for low power mode")
Fixes 9c1b13b3fd27 ("cyclic: reduce get_timer_us() calls inside hlist_for_each_entry_safe()")

Thanks,
Stefan

Quoted 46 lines
---
  arch/arm/mach-imx/mx7/soc.c    | 6 ++++++
  arch/arm/mach-imx/syscounter.c | 6 ++++--
  2 files changed, 10 insertions(+), 2 deletions(-)
arch/arm/mach-imx/mx7/soc.c +6 -0
index e504c1fd52a0..8a858dea5187 100644
@@ -331,6 +331,12 @@ int arch_cpu_init(void)
 
  	init_snvs();
 
+	/*
+	 * imx_gpcv2_init() needs to do a delay.
+	 * Timer must be initialized before calling a delay function.
+	 */
+	timer_init();
+
  	imx_gpcv2_init();
 
  	enable_ca7_smp();
arch/arm/mach-imx/syscounter.c +4 -2
index 96fe2c7c17bb..86311d0cc07d 100644
@@ -65,6 +65,9 @@ int timer_init(void)
  	struct sctr_regs *sctr = (struct sctr_regs *)SCTR_BASE_ADDR;
  	unsigned long val, freq;
 
+	if (gd->arch.timer_rate_hz)
+		return 0;
+
  	freq = CFG_SC_TIMER_CLK;
  	asm volatile("mcr p15, 0, %0, c14, c0, 0" : : "r" (freq));
 
@@ -103,8 +106,7 @@ ulong get_timer(ulong base)
 
  ulong timer_get_boot_us(void)
  {
-	if (!gd->arch.timer_rate_hz)
-		timer_init();
+	timer_init();
 
  	return tick_to_time(get_ticks());
  }