[PATCH v2 1/3] ARM64: lib64: pbl: implement get_time_ns and is_timeout

Stefan Kerkmann <[email protected]>
Newsgroups org.infradead.lists.barebox
Message-ID <20260811-feature-pbl-get-time-ns-v2-1-754ee021b19d@pengutronix.de>
All ARMv8-A cores implement the 64bit wide generic timer CNTPCT[1], thus
we can use it to implement the get_time_ns and is_timeout helpers which
in turn enable the whole read_poll_timeout class of functions in the
PBL. As it is guaranteed that the timer will not wrap for 40 years no
overflow handling is necessary.

[1]: See "ARM Architecture Reference Manual for A-profile architecture
(rev L.a)", chapter D12 "The Generic Timer in AArch64 state"

Signed-off-by: Stefan Kerkmann <[email protected]>
---
 arch/arm/lib64/delay.c | 17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)

diff --git a/arch/arm/lib64/delay.c b/arch/arm/lib64/delay.c
index 78eab33f8d..00b20c5db1 100644
--- a/arch/arm/lib64/delay.c
+++ b/arch/arm/lib64/delay.c
@@ -7,7 +7,7 @@
 void udelay(unsigned long us)
 {
 	unsigned long cntfrq = get_cntfrq();
-	unsigned long ticks = (us * cntfrq) / 1000000;
+	unsigned long ticks = (us * cntfrq) / MSECOND;
 	unsigned long start = get_cntpct();
 
 	while ((long)(start + ticks - get_cntpct()) > 0);
@@ -15,5 +15,18 @@ void udelay(unsigned long us)
 
 void mdelay(unsigned long ms)
 {
-	udelay(ms * 1000);
+	udelay(ms * USECOND);
+}
+
+uint64_t get_time_ns(void)
+{
+	return get_cntpct() * SECOND / get_cntfrq();
+}
+
+int is_timeout(uint64_t start, uint64_t time_offset_ns)
+{
+	if ((int64_t)(start + time_offset_ns - get_time_ns()) < 0)
+		return 1;
+	else
+		return 0;
 }

-- 
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.