[PATCH v3 7/8] ARM64: enable PBL_CLOCKSOURCE compatibility

Stefan Kerkmann <[email protected]>
Newsgroups org.infradead.lists.barebox
Message-ID <20260817-feature-pbl-get-time-ns-v3-7-9874c1438855@pengutronix.de>
All ARMv8-A cores implement the 64bit wide generic timer CNTPCT[1]. If
the architected timer driver is enabled the barebox PBL automatically is
PBL_CLOCKSOURCE compatible.

The setup of the PBL clocksource is implemented in the newly added PBL
constructors which are called at the end of setup_c. There is one catch
though:

The CNTFRQ_EL0 register[2] must be programmed by the bootrom, barebox or
the tf-a before the `arm_arch_timer_init` call. A later
re-initialization is also possible by calling `arm_arch_timer_init`
again.

[1]: See "ARM Architecture Reference Manual for A-profile architecture
(rev L.a)", chapter D12 "The Generic Timer in AArch64 state"
[2]: See "ARM Architecture Reference Manual for A-profile architecture
(rev L.a)", chapter D12.1.2.1 "Initializing and reading the system
effective frequency"

Signed-off-by: Stefan Kerkmann <[email protected]>
---
 arch/arm/lib64/Makefile      |  3 ++-
 arch/arm/lib64/clocksource.c | 10 ++++++++++
 arch/arm/lib64/delay.c       | 19 -------------------
 drivers/clocksource/Kconfig  |  1 +
 4 files changed, 13 insertions(+), 20 deletions(-)

diff --git a/arch/arm/lib64/Makefile b/arch/arm/lib64/Makefile
index f28cf81ab3..218de68a54 100644
--- a/arch/arm/lib64/Makefile
+++ b/arch/arm/lib64/Makefile
@@ -10,4 +10,5 @@ obj-pbl-y   += runtime-offset.o
 obj-pbl-y   += setjmp.o
 obj-pbl-y   += reloc.o
 obj-y += io.o
-pbl-y	+= div0.o delay.o
+pbl-$(CONFIG_CLOCKSOURCE_ARM_ARCHITECTED_TIMER)	+= clocksource.o
+pbl-y	+= div0.o
diff --git a/arch/arm/lib64/clocksource.c b/arch/arm/lib64/clocksource.c
new file mode 100644
index 0000000000..f992be09b8
--- /dev/null
+++ b/arch/arm/lib64/clocksource.c
@@ -0,0 +1,10 @@
+// SPDX-License-Identifier: GPL-2.0-only
+
+#include <debug_ll.h>
+#include <asm/hardware/arm_architected_timer.h>
+
+__attribute__((constructor)) static void init_arch_clock(void)
+{
+	if (arm_arch_timer_init(0))
+		puts_ll("Failed to setup architected timer\n");
+}
diff --git a/arch/arm/lib64/delay.c b/arch/arm/lib64/delay.c
deleted file mode 100644
index 78eab33f8d..0000000000
--- a/arch/arm/lib64/delay.c
+++ /dev/null
@@ -1,19 +0,0 @@
-// SPDX-License-Identifier: GPL-2.0-only
-
-#include <asm/system.h>
-#include <clock.h>
-#include <common.h>
-
-void udelay(unsigned long us)
-{
-	unsigned long cntfrq = get_cntfrq();
-	unsigned long ticks = (us * cntfrq) / 1000000;
-	unsigned long start = get_cntpct();
-
-	while ((long)(start + ticks - get_cntpct()) > 0);
-}
-
-void mdelay(unsigned long ms)
-{
-	udelay(ms * 1000);
-}
diff --git a/drivers/clocksource/Kconfig b/drivers/clocksource/Kconfig
index 5ee83d2b38..6cf68d8be4 100644
--- a/drivers/clocksource/Kconfig
+++ b/drivers/clocksource/Kconfig
@@ -79,6 +79,7 @@ config CLOCKSOURCE_ARM_ARCHITECTED_TIMER
 	bool "ARM architected timer clock source" if COMPILE_TEST
 	default y
 	depends on ARM && (CPU_64v8 || CPU_V7)
+	select PBL_CLOCKSOURCE if CPU_64v8
 
 config CLOCKSOURCE_ARM_GLOBAL_TIMER
 	bool "ARM global timer clock source" if COMPILE_TEST

-- 
2.47.3
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.