If CONFIG_TIMER_EARLY is selected and the timer driver implements
timer_early_get_count() and timer_early_get_rate(), check first
if the virtual root driver is running, when it is initialized yet
use the virtual timer driver instead. When the virtual root driver
doesn't exists fallback to the timer_early_get_count() and
timer_early_get_rate().

Signed-off-by: Johannes Krottmayer <krj...@gmail.com>
Cc: Wolfgang Denk <w...@denx.de>
Cc: Thomas Chou <tho...@wytron.com.tw>
Cc: Rick Chen <r...@andestech.com>
Cc: Leo <ycli...@andestech.com>
Cc: Bin Meng <bmeng...@gmail.com>
Cc: Sean Anderson <sean...@gmail.com>
Cc: Anup Patel <a...@brainfault.org>
Cc: Thomas Chou <tho...@wytron.com.tw>
---
 lib/time.c | 42 +++++++++++++++++++++++++++++++++---------
 1 file changed, 33 insertions(+), 9 deletions(-)

diff --git a/lib/time.c b/lib/time.c
index 96074b84af..b5c9760042 100644
--- a/lib/time.c
+++ b/lib/time.c
@@ -8,6 +8,7 @@
 #include <clock_legacy.h>
 #include <bootstage.h>
 #include <dm.h>
+#include <dm/root.h>
 #include <errno.h>
 #include <init.h>
 #include <spl.h>
@@ -66,16 +67,28 @@ extern unsigned long __weak timer_read_counter(void);
 #if CONFIG_IS_ENABLED(TIMER)
 ulong notrace get_tbclk(void)
 {
-       if (!gd->timer) {
+       int ret;
+
 #ifdef CONFIG_TIMER_EARLY
+
+       /*
+        * Check if the virtual root driver does not yet exist,
+        * if not fallback to timer_early_get_rate().
+        */
+       if (gd->dm_root == NULL)
                return timer_early_get_rate();
-#else
-               int ret;
 
+#endif /* CONFIG_TIMER_EARLY */
+
+       /*
+        * Check if the virtual timer driver does not yet exist,
+        * if not initialize the driver.
+        */
+       if (!gd->timer) {
                ret = dm_timer_init();
+
                if (ret)
-                       return ret;
-#endif
+                       panic("Could not initialize timer (err %d)\n", ret);
        }
 
        return timer_get_rate(gd->timer);
@@ -86,19 +99,30 @@ uint64_t notrace get_ticks(void)
        u64 count;
        int ret;
 
-       if (!gd->timer) {
 #ifdef CONFIG_TIMER_EARLY
+
+       /*
+        * Check if the virtual root driver does not yet exist,
+        * if not fallback to timer_early_get_rate().
+        */
+       if (gd->dm_root == NULL)
                return timer_early_get_count();
-#else
-               int ret;
 
+#endif /* CONFIG_TIMER_EARLY */
+
+       /*
+        * Check if the virtual timer driver does not yet exist,
+        * if not initialize the driver.
+        */
+       if (!gd->timer) {
                ret = dm_timer_init();
+
                if (ret)
                        panic("Could not initialize timer (err %d)\n", ret);
-#endif
        }
 
        ret = timer_get_count(gd->timer, &count);
+
        if (ret) {
                if (spl_phase() > PHASE_TPL)
                        panic("Could not read count from timer (err %d)\n",
-- 
2.34.1

Reply via email to