[PATCH 1/3] riscv: Add timer_get_us() for tracing

Pragnesh Patel pragnesh.patel at sifive.com
Mon Aug 24 16:44:27 CEST 2020


timer_get_us() will use timer_ops->get_count() function for timer counter.
For S-mode U-Boot, CSR_TIMEH and CSR_TIME will provide a timer counter and
For M-mode U-Boot, mtime register will provide the same.

Signed-off-by: Pragnesh Patel <pragnesh.patel at sifive.com>
---
 arch/riscv/lib/Makefile |  1 +
 arch/riscv/lib/timer.c  | 50 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 51 insertions(+)
 create mode 100644 arch/riscv/lib/timer.c

diff --git a/arch/riscv/lib/Makefile b/arch/riscv/lib/Makefile
index 10ac5b06d3..fbb68e583b 100644
--- a/arch/riscv/lib/Makefile
+++ b/arch/riscv/lib/Makefile
@@ -26,6 +26,7 @@ obj-y   += setjmp.o
 obj-$(CONFIG_$(SPL_)SMP) += smp.o
 obj-$(CONFIG_SPL_BUILD)	+= spl.o
 obj-y   += fdt_fixup.o
+obj-$(CONFIG_TIMER) += timer.o
 
 # For building EFI apps
 CFLAGS_$(EFI_CRT0) := $(CFLAGS_EFI)
diff --git a/arch/riscv/lib/timer.c b/arch/riscv/lib/timer.c
new file mode 100644
index 0000000000..3e423f2805
--- /dev/null
+++ b/arch/riscv/lib/timer.c
@@ -0,0 +1,50 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (C) 2020 SiFive, Inc
+ *
+ * Authors:
+ *   Pragnesh Patel <pragnesh.patel at sifive.com>
+ */
+
+#include <common.h>
+#include <dm.h>
+#include <timer.h>
+
+DECLARE_GLOBAL_DATA_PTR;
+
+static struct udevice *timer;
+
+ulong notrace timer_get_us(void)
+{
+	u64 count;
+	ulong rate;
+	int ret;
+
+	/**
+	 * gd->timer will become NULL in initr_dm(), so assign gd->timer
+	 * to other static global timer, so that timer_get_us() can use it.
+	 */
+	if (!timer && gd->timer)
+		timer = (struct udevice *)gd->timer;
+
+	if (timer) {
+		ret = timer_get_count(timer, &count);
+		if (ret)
+			return ret;
+
+		rate = timer_get_rate(timer);
+	}
+
+	return (ulong)count / rate;
+}
+
+int timer_init(void)
+{
+	int ret;
+
+	ret = dm_timer_init();
+	if (ret)
+		return ret;
+
+	return 0;
+}
-- 
2.17.1



More information about the U-Boot mailing list