[PATCH 1/4] lib: sbi: Add timer tick to time conversion helper

Prajwal N prajwal.n at oss.qualcomm.com
Fri Oct 2 01:30:37 PDT 2026


Add helper function to convert timer tick delta to a specified time
unit using the corresponding unit frequency.
Add a wrapper to convert the timer ticks to microsecond.

The generic helper allows the SBI watchdog and other timer users to
share the same conversion logic and can be extended to support
additional time units.

Signed-off-by: Prajwal N <prajwal.n at oss.qualcomm.com>
---
 include/sbi/sbi_timer.h | 12 ++++++++++++
 lib/sbi/sbi_timer.c     |  5 +++++
 2 files changed, 17 insertions(+)

diff --git a/include/sbi/sbi_timer.h b/include/sbi/sbi_timer.h
index 17d6c1dc..3e1243b2 100644
--- a/include/sbi/sbi_timer.h
+++ b/include/sbi/sbi_timer.h
@@ -12,6 +12,9 @@
 
 #include <sbi/sbi_list.h>
 
+/** Macro for seconds to microseconds conversion */
+#define SEC_TO_MICROSEC		1000000ULL
+
 /** Timer event re-start details */
 struct sbi_timer_event_restart {
 	/** Flag indicating whether event re-start is required */
@@ -88,6 +91,15 @@ struct sbi_timer_device {
 
 struct sbi_scratch;
 
+/** Compute timer value in given unit sec from timer tick delta */
+u64 sbi_timer_convert_ticks(ulong from_ticks, u64 unit_freq);
+
+/** Compute microseconds from timer tick delta */
+static inline u64 sbi_timer_ticks_to_usec(ulong ticks)
+{
+	return sbi_timer_convert_ticks(ticks, SEC_TO_MICROSEC);
+}
+
 /** Compute timer value delta based on arbitary units */
 u64 sbi_timer_compute_delta(ulong units, u64 unit_freq);
 
diff --git a/lib/sbi/sbi_timer.c b/lib/sbi/sbi_timer.c
index 78aaa32e..aeb860eb 100644
--- a/lib/sbi/sbi_timer.c
+++ b/lib/sbi/sbi_timer.c
@@ -58,6 +58,11 @@ static void nop_delay_fn(void *opaque)
 	cpu_relax();
 }
 
+u64 sbi_timer_convert_ticks(ulong from_ticks, u64 unit_freq)
+{
+	return (from_ticks * unit_freq) / ((u64)timer_dev->timer_freq);
+}
+
 u64 sbi_timer_compute_delta(ulong units, u64 unit_freq)
 {
 	u64 delta;
-- 
2.43.0




More information about the opensbi mailing list