delay: timeout detection support
[project/bcm63xx/atf.git] / include / drivers / delay_timer.h
index 4e44a5ee1065da46910684a1f671a28d6a279e91..e5044cc6e17e8b86383090447111b2d3c1f7ed33 100644 (file)
@@ -1,14 +1,18 @@
 /*
- * Copyright (c) 2015, ARM Limited and Contributors. All rights reserved.
+ * Copyright (c) 2015-2019, ARM Limited and Contributors. All rights reserved.
+ * Copyright (c) 2019, Linaro Limited
  *
  * SPDX-License-Identifier: BSD-3-Clause
  */
 
-#ifndef __DELAY_TIMER_H__
-#define __DELAY_TIMER_H__
+#ifndef DELAY_TIMER_H
+#define DELAY_TIMER_H
 
+#include <stdbool.h>
 #include <stdint.h>
 
+#include <arch_helpers.h>
+
 /********************************************************************
  * A simple timer driver providing synchronous delay functionality.
  * The driver must be initialized with a structure that provides a
@@ -23,9 +27,27 @@ typedef struct timer_ops {
        uint32_t clk_div;
 } timer_ops_t;
 
+static inline uint64_t timeout_cnt_us2cnt(uint32_t us)
+{
+       return ((uint64_t)us * (uint64_t)read_cntfrq_el0()) / 1000000ULL;
+}
+
+static inline uint64_t timeout_init_us(uint32_t us)
+{
+       uint64_t cnt = timeout_cnt_us2cnt(us);
+
+       cnt += read_cntfrq_el0();
+
+       return cnt;
+}
+
+static inline bool timeout_elapsed(uint64_t expire_cnt)
+{
+       return read_cntpct_el0() > expire_cnt;
+}
+
 void mdelay(uint32_t msec);
 void udelay(uint32_t usec);
-void timer_init(const timer_ops_t *ops);
-
+void timer_init(const timer_ops_t *ops_ptr);
 
-#endif /* __DELAY_TIMER_H__ */
+#endif /* DELAY_TIMER_H */