mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Stefan Kerkmann <s.kerkmann@pengutronix.de>
To: Sascha Hauer <s.hauer@pengutronix.de>,
	 BAREBOX <barebox@lists.infradead.org>
Cc: Stefan Kerkmann <s.kerkmann@pengutronix.de>
Subject: [PATCH 1/3] ARM64: lib64: pbl: implement get_time_ns and is_timeout
Date: Tue, 21 Jan 2025 17:49:26 +0100	[thread overview]
Message-ID: <20250121-feature-pbl-get-time-ns-v1-1-c3d493397846@pengutronix.de> (raw)
In-Reply-To: <20250121-feature-pbl-get-time-ns-v1-0-c3d493397846@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 <s.kerkmann@pengutronix.de>
---
 arch/arm/lib64/pbl.c | 17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)

diff --git a/arch/arm/lib64/pbl.c b/arch/arm/lib64/pbl.c
index 78eab33f8d68598554c07f347938cc84608c6716..00b20c5db1bd347ae4c4a3ce9eaa792fec3d07c9 100644
--- a/arch/arm/lib64/pbl.c
+++ b/arch/arm/lib64/pbl.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.39.5




  reply	other threads:[~2025-01-21 16:50 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-01-21 16:49 [PATCH 0/3] PBL: enable timeouts in read_poll_timeout macros Stefan Kerkmann
2025-01-21 16:49 ` Stefan Kerkmann [this message]
2025-01-21 16:49 ` [PATCH 2/3] ARM: lib32: pbl: implement get_time_ns and is_timeout Stefan Kerkmann
2025-01-21 16:49 ` [PATCH 3/3] pbl: introduce HAS_PBL_CLOCKSOURCE marker Stefan Kerkmann

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250121-feature-pbl-get-time-ns-v1-1-c3d493397846@pengutronix.de \
    --to=s.kerkmann@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    --cc=s.hauer@pengutronix.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox