Skip to contentSkip to conversation

Timer based delay loop

PATCH New Anup Patel 3 participants 16 messages Sep 25, 2021 – Sep 27, 2021

lore
Share & tools
lore

Anup Patel

author
#0 lore raw

v3 cover Timer based delay loop

This series adds timer based delay loop so that drivers and platform support can have precise delays in their implementations.

These patches can be found in mtimer_freq_v3 branch at:
https://github.com/avpatel/opensbi.git

Changes since v2:
- Renamed few more defines in PATCH2

Changes since v1:
- Renamed few defines in PATCH2
- Added a warning in PATCH4

Anup Patel (5):
lib: utils/fdt: Add fdt_parse_timebase_frequency() function
lib: sbi: Add timer frequency to struct sbi_timer_device
lib: sbi: Print timer frequency at boot time
lib: sbi: Add generic timer delay loop function
lib: utils/reset: use sbi_timer_mdelay() in gpio reset driver

 include/sbi/sbi_timer.h                 | 19 ++++++++++++++
 include/sbi_utils/fdt/fdt_helper.h      |  2 ++
 include/sbi_utils/timer/aclint_mtimer.h |  1 +
 lib/sbi/sbi_init.c                      |  5 ++--
 lib/sbi/sbi_timer.c                     | 34 +++++++++++++++++++++++++
 lib/utils/fdt/fdt_helper.c              | 21 +++++++++++++++
 lib/utils/reset/fdt_reset_gpio.c        | 13 +++-------
 lib/utils/timer/aclint_mtimer.c         |  3 +++
 lib/utils/timer/fdt_timer_mtimer.c      |  4 +++
 platform/fpga/ariane/platform.c         |  2 ++
 platform/fpga/openpiton/platform.c      |  7 +++++
 platform/kendryte/k210/platform.c       |  1 +
 platform/kendryte/k210/platform.h       |  2 +-
 platform/nuclei/ux600/platform.c        |  1 +
 platform/template/platform.c            |  2 ++
 15 files changed, 104 insertions(+), 13 deletions(-)
Signature · 1 line
2.25.1
author
#1 lore raw

PATCH v3 1/5 lib: utils/fdt: Add fdt_parse_timebase_frequency() function

We add fdt_parse_timebase_frequency() function which can be used by ACLINT mtimer driver and platform code to get timebase frequency.

---
 include/sbi_utils/fdt/fdt_helper.h |  2 ++
 lib/utils/fdt/fdt_helper.c         | 21 +++++++++++++++++++++
 2 files changed, 23 insertions(+)
include/sbi_utils/fdt/fdt_helper.h +2 -0
index 2246254..5cb7340 100644
@@ -50,6 +50,8 @@ int fdt_parse_hart_id(void *fdt, int cpu_offset, u32 *hartid);
 
 int fdt_parse_max_hart_id(void *fdt, u32 *max_hartid);
 
+int fdt_parse_timebase_frequency(void *fdt, unsigned long *freq);
+
 int fdt_parse_gaisler_uart_node(void *fdt, int nodeoffset,
 				struct platform_uart_data *uart);
 
lib/utils/fdt/fdt_helper.c +21 -0
index 5628337..5bf4021 100644
@@ -269,6 +269,27 @@ int fdt_parse_max_hart_id(void *fdt, u32 *max_hartid)
 	return 0;
 }
 
+int fdt_parse_timebase_frequency(void *fdt, unsigned long *freq)
+{
+	const fdt32_t *val;
+	int len, cpus_offset;
+
+	if (!fdt || !freq)
+		return SBI_EINVAL;
+
+	cpus_offset = fdt_path_offset(fdt, "/cpus");
+	if (cpus_offset < 0)
+		return cpus_offset;
+
+	val = fdt_getprop(fdt, cpus_offset, "timebase-frequency", &len);
+	if (len > 0 && val)
+		*freq = fdt32_to_cpu(*val);
+	else
+		return SBI_ENOENT;
+
+	return 0;
+}
+
 int fdt_parse_gaisler_uart_node(void *fdt, int nodeoffset,
 				struct platform_uart_data *uart)
 {
Signature · 1 line
2.25.1
#2 lore raw

PATCH v3 1/5 lib: utils/fdt: Add fdt_parse_timebase_frequency() function

? 2021-09-25???? 10:07 +0530?Anup Patel???

Quoted 5 lines

We add fdt_parse_timebase_frequency() function which can be used by ACLINT mtimer driver and platform code to get timebase frequency.

Quoted 58 lines

---
?include/sbi_utils/fdt/fdt_helper.h |? 2 ++ ?lib/utils/fdt/fdt_helper.c???????? | 21 +++++++++++++++++++++ ?2 files changed, 23 insertions(+)

include/sbi_utils/fdt/fdt_helper.h +0 -0
index 2246254..5cb7340 100644
@@ -50,6 +50,8 @@ int fdt_parse_hart_id(void *fdt, int cpu_offset,
 u32 *hartid);

?
?int fdt_parse_max_hart_id(void *fdt, u32 *max_hartid); ?

include/sbi_utils/fdt/fdt_helper.h +2 -0
+int fdt_parse_timebase_frequency(void *fdt, unsigned long *freq);
+

?int fdt_parse_gaisler_uart_node(void *fdt, int nodeoffset, ????????????????????????????????struct platform_uart_data *uart); ?

lib/utils/fdt/fdt_helper.c +0 -0
index 5628337..5bf4021 100644
@@ -269,6 +269,27 @@ int fdt_parse_max_hart_id(void *fdt, u32
 *max_hartid)
 ????????return 0;
 ?}

?

lib/utils/fdt/fdt_helper.c +13 -0
+int fdt_parse_timebase_frequency(void *fdt, unsigned long *freq)
+{
+???????const fdt32_t *val;
+???????int len, cpus_offset;
+
+???????if (!fdt || !freq)
+???????????????return SBI_EINVAL;
+
+???????cpus_offset = fdt_path_offset(fdt, "/cpus");
+???????if (cpus_offset < 0)
+???????????????return cpus_offset;
+
+???????val = fdt_getprop(fdt, cpus_offset, "timebase-frequency",

&len);
+???????if (len > 0 && val)
+???????????????*freq = fdt32_to_cpu(*val);
+???????else
+???????????????return SBI_ENOENT;

lib/utils/fdt/fdt_helper.c +4 -0
+
+???????return 0;
+}
+

?int fdt_parse_gaisler_uart_node(void *fdt, int nodeoffset, ????????????????????????????????struct platform_uart_data *uart) ?{

Signature · 1 line
2.25.1
Applied
#3 lore raw

PATCH v3 1/5 lib: utils/fdt: Add fdt_parse_timebase_frequency() function

Quoted 8 lines · Xiang W

? 2021-09-25???? 10:07 +0530?Anup Patel???

Quoted 5 lines

We add fdt_parse_timebase_frequency() function which can be used by ACLINT mtimer driver and platform code to get timebase frequency.

Applied this patch to the riscv/opensbi repo

Regards,
Anup

Quoted 60 lines
Quoted 58 lines
---
 include/sbi_utils/fdt/fdt_helper.h |  2 ++
 lib/utils/fdt/fdt_helper.c         | 21 +++++++++++++++++++++
 2 files changed, 23 insertions(+)
include/sbi_utils/fdt/fdt_helper.h +2 -0
index 2246254..5cb7340 100644
@@ -50,6 +50,8 @@ int fdt_parse_hart_id(void *fdt, int cpu_offset,
 u32 *hartid);
 
 int fdt_parse_max_hart_id(void *fdt, u32 *max_hartid);
 
+int fdt_parse_timebase_frequency(void *fdt, unsigned long *freq);
+
 int fdt_parse_gaisler_uart_node(void *fdt, int nodeoffset,
                                struct platform_uart_data *uart);
lib/utils/fdt/fdt_helper.c +21 -0
index 5628337..5bf4021 100644
@@ -269,6 +269,27 @@ int fdt_parse_max_hart_id(void *fdt, u32
 *max_hartid)
        return 0;
 }
 
+int fdt_parse_timebase_frequency(void *fdt, unsigned long *freq)
+{
+       const fdt32_t *val;
+       int len, cpus_offset;
+
+       if (!fdt || !freq)
+               return SBI_EINVAL;
+
+       cpus_offset = fdt_path_offset(fdt, "/cpus");
+       if (cpus_offset < 0)
+               return cpus_offset;
+
+       val = fdt_getprop(fdt, cpus_offset, "timebase-frequency",
 &len);
+       if (len > 0 && val)
+               *freq = fdt32_to_cpu(*val);
+       else
+               return SBI_ENOENT;
+
+       return 0;
+}
+
 int fdt_parse_gaisler_uart_node(void *fdt, int nodeoffset,

struct platform_uart_data *uart)
{
--
2.25.1

author
#4 lore raw

PATCH v3 2/5 lib: sbi: Add timer frequency to struct sbi_timer_device

In reply to Anup Patel

Generic mdelay() and udelay() functions can be provided by the sbi_timer framework if timer frequency is available in the timer instance provided by the platform support or timer driver.

This patch adds timer frequency (timer_freq) member in the struct sbi_timer_device for above purpose.

---
 include/sbi/sbi_timer.h                 | 3 +++
 include/sbi_utils/timer/aclint_mtimer.h | 1 +
 lib/utils/timer/aclint_mtimer.c         | 3 +++
 lib/utils/timer/fdt_timer_mtimer.c      | 4 ++++
 platform/fpga/ariane/platform.c         | 2 ++
 platform/fpga/openpiton/platform.c      | 7 +++++++
 platform/kendryte/k210/platform.c       | 1 +
 platform/kendryte/k210/platform.h       | 2 +-
 platform/nuclei/ux600/platform.c        | 1 +
 platform/template/platform.c            | 2 ++
 10 files changed, 25 insertions(+), 1 deletion(-)
include/sbi/sbi_timer.h +3 -0
index 1ba6da0..211e83d 100644
@@ -17,6 +17,9 @@ struct sbi_timer_device {
 	/** Name of the timer operations */
 	char name[32];
 
+	/** Frequency of timer in HZ */
+	unsigned long timer_freq;
+
 	/** Get free-running timer value */
 	u64 (*timer_value)(void);
 
include/sbi_utils/timer/aclint_mtimer.h +1 -0
index a9fe445..f02cc62 100644
@@ -24,6 +24,7 @@
 
 struct aclint_mtimer_data {
 	/* Public details */
+	unsigned long mtime_freq;
 	unsigned long mtime_addr;
 	unsigned long mtime_size;
 	unsigned long mtimecmp_addr;
lib/utils/timer/aclint_mtimer.c +3 -0
index d612b12..62e87f7 100644
@@ -186,6 +186,8 @@ int aclint_mtimer_cold_init(struct aclint_mtimer_data *mt,
 	    (mt->first_hartid >= SBI_HARTMASK_MAX_BITS) ||
 	    (mt->hart_count > ACLINT_MTIMER_MAX_HARTS))
 		return SBI_EINVAL;
+	if (reference && mt->mtime_freq != reference->mtime_freq)
+		return SBI_EINVAL;
 
 	/* Initialize private data */
 	aclint_mtimer_set_reference(mt, reference);
@@ -227,6 +229,7 @@ int aclint_mtimer_cold_init(struct aclint_mtimer_data *mt,
 			return rc;
 	}
 
+	mtimer.timer_freq = mt->mtime_freq;
 	sbi_timer_set_device(&mtimer);
 
 	return 0;
lib/utils/timer/fdt_timer_mtimer.c +4 -0
index 4eafffa..1ad8508 100644
@@ -38,6 +38,10 @@ static int timer_mtimer_cold_init(void *fdt, int nodeoff,
 	mt->has_64bit_mmio = true;
 	mt->has_shared_mtime = false;
 
+	rc = fdt_parse_timebase_frequency(fdt, &mt->mtime_freq);
+	if (rc)
+		return rc;
+
 	if (match->data) { /* SiFive CLINT */
 		/* Set CLINT addresses */
 		mt->mtimecmp_addr = addr[0] + ACLINT_DEFAULT_MTIMECMP_OFFSET;
platform/fpga/ariane/platform.c +2 -0
index 58a46c0..5acc446 100644
@@ -26,6 +26,7 @@
 #define ARIANE_PLIC_NUM_SOURCES			3
 #define ARIANE_HART_COUNT			1
 #define ARIANE_CLINT_ADDR			0x2000000
+#define ARIANE_ACLINT_MTIMER_FREQ		1000000
 #define ARIANE_ACLINT_MSWI_ADDR			(ARIANE_CLINT_ADDR + \
 						 CLINT_MSWI_OFFSET)
 #define ARIANE_ACLINT_MTIMER_ADDR		(ARIANE_CLINT_ADDR + \
@@ -44,6 +45,7 @@ static struct aclint_mswi_data mswi = {
 };
 
 static struct aclint_mtimer_data mtimer = {
+	.mtime_freq = ARIANE_ACLINT_MTIMER_FREQ,
 	.mtime_addr = ARIANE_ACLINT_MTIMER_ADDR +
 		      ACLINT_DEFAULT_MTIME_OFFSET,
 	.mtime_size = ACLINT_DEFAULT_MTIME_SIZE,
platform/fpga/openpiton/platform.c +7 -0
index a9bfa99..924053e 100644
@@ -26,6 +26,7 @@
 #define OPENPITON_DEFAULT_PLIC_NUM_SOURCES	2
 #define OPENPITON_DEFAULT_HART_COUNT		3
 #define OPENPITON_DEFAULT_CLINT_ADDR		0xfff1020000
+#define OPENPITON_DEFAULT_ACLINT_MTIMER_FREQ	1000000
 #define OPENPITON_DEFAULT_ACLINT_MSWI_ADDR	\
 		(OPENPITON_DEFAULT_CLINT_ADDR + CLINT_MSWI_OFFSET)
 #define OPENPITON_DEFAULT_ACLINT_MTIMER_ADDR	\
@@ -49,6 +50,7 @@ static struct aclint_mswi_data mswi = {
 };
 
 static struct aclint_mtimer_data mtimer = {
+	.mtime_freq = OPENPITON_DEFAULT_ACLINT_MTIMER_FREQ,
 	.mtime_addr = OPENPITON_DEFAULT_ACLINT_MTIMER_ADDR +
 		      ACLINT_DEFAULT_MTIME_OFFSET,
 	.mtime_size = ACLINT_DEFAULT_MTIME_SIZE,
@@ -68,6 +70,7 @@ static int openpiton_early_init(bool cold_boot)
 	void *fdt;
 	struct platform_uart_data uart_data;
 	struct plic_data plic_data;
+	unsigned long aclint_freq;
 	uint64_t clint_addr;
 	int rc;
 
@@ -83,6 +86,10 @@ static int openpiton_early_init(bool cold_boot)
 	if (!rc)
 		plic = plic_data;
 
+	rc = fdt_parse_timebase_frequency(fdt, &aclint_freq);
+	if (!rc)
+		mtimer.mtime_freq = aclint_freq;
+
 	rc = fdt_parse_compat_addr(fdt, &clint_addr, "riscv,clint0");
 	if (!rc) {
 		mswi.addr = clint_addr;
platform/kendryte/k210/platform.c +1 -0
index ee4c223..35cec5f 100644
@@ -42,6 +42,7 @@ static struct aclint_mswi_data mswi = {
 };
 
 static struct aclint_mtimer_data mtimer = {
+	.mtime_freq = K210_ACLINT_MTIMER_FREQ,
 	.mtime_addr = K210_ACLINT_MTIMER_ADDR +
 		      ACLINT_DEFAULT_MTIME_OFFSET,
 	.mtime_size = ACLINT_DEFAULT_MTIME_SIZE,
platform/kendryte/k210/platform.h +1 -1
index 0a32530..be52aa3 100644
@@ -14,7 +14,7 @@
 #define K210_HART_COUNT		2
 
 #define K210_UART_BAUDRATE	115200
-
+#define K210_ACLINT_MTIMER_FREQ	7800000
 #define K210_CLK0_FREQ		26000000UL
 #define K210_PLIC_NUM_SOURCES	65
 
platform/nuclei/ux600/platform.c +1 -0
index ab0becc..6bef4c4 100644
@@ -74,6 +74,7 @@ static struct aclint_mswi_data mswi = {
 };
 
 static struct aclint_mtimer_data mtimer = {
+	.mtime_freq = UX600_TIMER_FREQ,
 	.mtime_addr = UX600_ACLINT_MTIMER_ADDR +
 		      ACLINT_DEFAULT_MTIME_OFFSET,
 	.mtime_size = ACLINT_DEFAULT_MTIME_SIZE,
platform/template/platform.c +2 -0
index 4528822..d6806e6 100644
@@ -22,6 +22,7 @@
 #define PLATFORM_PLIC_NUM_SOURCES	128
 #define PLATFORM_HART_COUNT		4
 #define PLATFORM_CLINT_ADDR		0x2000000
+#define PLATFORM_ACLINT_MTIMER_FREQ	10000000
 #define PLATFORM_ACLINT_MSWI_ADDR	(PLATFORM_CLINT_ADDR + \
 					 CLINT_MSWI_OFFSET)
 #define PLATFORM_ACLINT_MTIMER_ADDR	(PLATFORM_CLINT_ADDR + \
@@ -43,6 +44,7 @@ static struct aclint_mswi_data mswi = {
 };
 
 static struct aclint_mtimer_data mtimer = {
+	.mtime_freq = PLATFORM_ACLINT_MTIMER_FREQ,
 	.mtime_addr = PLATFORM_ACLINT_MTIMER_ADDR +
 		      ACLINT_DEFAULT_MTIME_OFFSET,
 	.mtime_size = ACLINT_DEFAULT_MTIME_SIZE,
Signature · 1 line
2.25.1
#5 lore raw

PATCH v3 2/5 lib: sbi: Add timer frequency to struct sbi_timer_device

? 2021-09-25???? 10:07 +0530?Anup Patel???

Quoted 9 lines

Generic mdelay() and udelay() functions can be provided by the sbi_timer framework if timer frequency is available in the timer instance provided by the platform support or timer driver.

This patch adds timer frequency (timer_freq) member in the struct sbi_timer_device for above purpose.

Quoted 210 lines

---

?include/sbi/sbi_timer.h???????????????? | 3 +++
?include/sbi_utils/timer/aclint_mtimer.h | 1 +
?lib/utils/timer/aclint_mtimer.c???????? | 3 +++
?lib/utils/timer/fdt_timer_mtimer.c????? | 4 ++++
?platform/fpga/ariane/platform.c???????? | 2 ++
?platform/fpga/openpiton/platform.c????? | 7 +++++++
?platform/kendryte/k210/platform.c?????? | 1 +
?platform/kendryte/k210/platform.h?????? | 2 +-
?platform/nuclei/ux600/platform.c??????? | 1 +
?platform/template/platform.c??????????? | 2 ++

?10 files changed, 25 insertions(+), 1 deletion(-)

include/sbi/sbi_timer.h +0 -0
index 1ba6da0..211e83d 100644
@@ -17,6 +17,9 @@ struct sbi_timer_device {

????????/** Name of the timer operations */
????????char name[32];
?
+???????/** Frequency of timer in HZ */
+???????unsigned long timer_freq;
+
????????/** Get free-running timer value */
????????u64 (*timer_value)(void);
?

include/sbi_utils/timer/aclint_mtimer.h +0 -0
index a9fe445..f02cc62 100644
@@ -24,6 +24,7 @@

?
?struct aclint_mtimer_data {
????????/* Public details */
+???????unsigned long mtime_freq;
????????unsigned long mtime_addr;
????????unsigned long mtime_size;
????????unsigned long mtimecmp_addr;

lib/utils/timer/aclint_mtimer.c +0 -0
index d612b12..62e87f7 100644
@@ -186,6 +186,8 @@ int aclint_mtimer_cold_init(struct
 aclint_mtimer_data *mt,

??????????? (mt->first_hartid >= SBI_HARTMASK_MAX_BITS) || ??????????? (mt->hart_count > ACLINT_MTIMER_MAX_HARTS)) ????????????????return SBI_EINVAL;
+???????if (reference && mt->mtime_freq != reference->mtime_freq) +???????????????return SBI_EINVAL;
?
????????/* Initialize private data */ ????????aclint_mtimer_set_reference(mt, reference);

lib/utils/timer/aclint_mtimer.c +0 -0
@@ -227,6 +229,7 @@ int aclint_mtimer_cold_init(struct
 aclint_mtimer_data *mt,
 ????????????????????????return rc;
 ????????}

?
+???????mtimer.timer_freq = mt->mtime_freq;
????????sbi_timer_set_device(&mtimer);
?
????????return 0;

lib/utils/timer/fdt_timer_mtimer.c +0 -0
index 4eafffa..1ad8508 100644
@@ -38,6 +38,10 @@ static int timer_mtimer_cold_init(void *fdt, int
 nodeoff,
 ????????mt->has_64bit_mmio = true;
 ????????mt->has_shared_mtime = false;

?
+???????rc = fdt_parse_timebase_frequency(fdt, &mt->mtime_freq); +???????if (rc)
+???????????????return rc;
+
????????if (match->data) { /* SiFive CLINT */ ????????????????/* Set CLINT addresses */ ????????????????mt->mtimecmp_addr = addr[0] + ACLINT_DEFAULT_MTIMECMP_OFFSET;

platform/fpga/ariane/platform.c +0 -0
index 58a46c0..5acc446 100644
@@ -26,6 +26,7 @@

?#define ARIANE_PLIC_NUM_SOURCES????????????????????????3 ?#define ARIANE_HART_COUNT??????????????????????1
?#define ARIANE_CLINT_ADDR??????????????????????0x2000000 +#define ARIANE_ACLINT_MTIMER_FREQ??????????????1000000 ?#define ARIANE_ACLINT_MSWI_ADDR????????????????????????(ARIANE_CLINT_ADDR + \
???????????????????????????????????????????????? CLINT_MSWI_OFFSET) ?#define ARIANE_ACLINT_MTIMER_ADDR??????????????(ARIANE_CLINT_ADDR + \

platform/fpga/ariane/platform.c +0 -0
@@ -44,6 +45,7 @@ static struct aclint_mswi_data mswi = {
 ?};

?
?static struct aclint_mtimer_data mtimer = { +???????.mtime_freq = ARIANE_ACLINT_MTIMER_FREQ, ????????.mtime_addr = ARIANE_ACLINT_MTIMER_ADDR + ????????????????????? ACLINT_DEFAULT_MTIME_OFFSET, ????????.mtime_size = ACLINT_DEFAULT_MTIME_SIZE,

platform/fpga/openpiton/platform.c +0 -0
index a9bfa99..924053e 100644
@@ -26,6 +26,7 @@

?#define OPENPITON_DEFAULT_PLIC_NUM_SOURCES?????2 ?#define OPENPITON_DEFAULT_HART_COUNT???????????3 ?#define OPENPITON_DEFAULT_CLINT_ADDR???????????0xfff1020000 +#define OPENPITON_DEFAULT_ACLINT_MTIMER_FREQ???1000000 ?#define OPENPITON_DEFAULT_ACLINT_MSWI_ADDR?????\
????????????????(OPENPITON_DEFAULT_CLINT_ADDR + CLINT_MSWI_OFFSET) ?#define OPENPITON_DEFAULT_ACLINT_MTIMER_ADDR???\

platform/fpga/openpiton/platform.c +0 -0
@@ -49,6 +50,7 @@ static struct aclint_mswi_data mswi = {
 ?};

?
?static struct aclint_mtimer_data mtimer = { +???????.mtime_freq = OPENPITON_DEFAULT_ACLINT_MTIMER_FREQ, ????????.mtime_addr = OPENPITON_DEFAULT_ACLINT_MTIMER_ADDR + ????????????????????? ACLINT_DEFAULT_MTIME_OFFSET, ????????.mtime_size = ACLINT_DEFAULT_MTIME_SIZE,

platform/fpga/openpiton/platform.c +1 -0
@@ -68,6 +70,7 @@ static int openpiton_early_init(bool cold_boot)
 ????????void *fdt;
 ????????struct platform_uart_data uart_data;
 ????????struct plic_data plic_data;
+???????unsigned long aclint_freq;
 ????????uint64_t clint_addr;
 ????????int rc;

?

platform/fpga/openpiton/platform.c +0 -0
@@ -83,6 +86,10 @@ static int openpiton_early_init(bool cold_boot)
 ????????if (!rc)
 ????????????????plic = plic_data;

?
+???????rc = fdt_parse_timebase_frequency(fdt, &aclint_freq); +???????if (!rc)
+???????????????mtimer.mtime_freq = aclint_freq;
+
????????rc = fdt_parse_compat_addr(fdt, &clint_addr, "riscv,clint0"); ????????if (!rc) {
????????????????mswi.addr = clint_addr;

platform/kendryte/k210/platform.c +0 -0
index ee4c223..35cec5f 100644
@@ -42,6 +42,7 @@ static struct aclint_mswi_data mswi = {
 ?};

?
?static struct aclint_mtimer_data mtimer = { +???????.mtime_freq = K210_ACLINT_MTIMER_FREQ, ????????.mtime_addr = K210_ACLINT_MTIMER_ADDR + ????????????????????? ACLINT_DEFAULT_MTIME_OFFSET, ????????.mtime_size = ACLINT_DEFAULT_MTIME_SIZE,

platform/kendryte/k210/platform.h +0 -0
index 0a32530..be52aa3 100644
@@ -14,7 +14,7 @@

?#define K210_HART_COUNT????????????????2
?
?#define K210_UART_BAUDRATE?????115200

platform/kendryte/k210/platform.h +1 -1
-
+#define K210_ACLINT_MTIMER_FREQ????????7800000

?#define K210_CLK0_FREQ?????????26000000UL
?#define K210_PLIC_NUM_SOURCES??65
?

platform/nuclei/ux600/platform.c +0 -0
index ab0becc..6bef4c4 100644
@@ -74,6 +74,7 @@ static struct aclint_mswi_data mswi = {
 ?};

?
?static struct aclint_mtimer_data mtimer = { +???????.mtime_freq = UX600_TIMER_FREQ, ????????.mtime_addr = UX600_ACLINT_MTIMER_ADDR + ????????????????????? ACLINT_DEFAULT_MTIME_OFFSET, ????????.mtime_size = ACLINT_DEFAULT_MTIME_SIZE,

platform/template/platform.c +0 -0
index 4528822..d6806e6 100644
@@ -22,6 +22,7 @@

?#define PLATFORM_PLIC_NUM_SOURCES??????128
?#define PLATFORM_HART_COUNT????????????4
?#define PLATFORM_CLINT_ADDR????????????0x2000000 +#define PLATFORM_ACLINT_MTIMER_FREQ????10000000 ?#define PLATFORM_ACLINT_MSWI_ADDR??????(PLATFORM_CLINT_ADDR + \
???????????????????????????????????????? CLINT_MSWI_OFFSET) ?#define PLATFORM_ACLINT_MTIMER_ADDR????(PLATFORM_CLINT_ADDR + \

platform/template/platform.c +0 -0
@@ -43,6 +44,7 @@ static struct aclint_mswi_data mswi = {
 ?};

?
?static struct aclint_mtimer_data mtimer = { +???????.mtime_freq = PLATFORM_ACLINT_MTIMER_FREQ, ????????.mtime_addr = PLATFORM_ACLINT_MTIMER_ADDR + ????????????????????? ACLINT_DEFAULT_MTIME_OFFSET, ????????.mtime_size = ACLINT_DEFAULT_MTIME_SIZE,

Signature · 1 line
2.25.1
Applied
#6 lore raw

PATCH v3 2/5 lib: sbi: Add timer frequency to struct sbi_timer_device

Quoted 6 lines · Xiang W

? 2021-09-25???? 10:07 +0530?Anup Patel???

Quoted 4 lines

Generic mdelay() and udelay() functions can be provided by the sbi_timer framework if timer frequency is available in the timer instance provided by the platform support or timer driver.

Quoted 6 lines
Quoted 5 lines

This patch adds timer frequency (timer_freq) member in the struct sbi_timer_device for above purpose.

Applied this patch to the riscv/opensbi repo

Regards,
Anup

Quoted 212 lines
Quoted 210 lines
---
 include/sbi/sbi_timer.h                 | 3 +++
 include/sbi_utils/timer/aclint_mtimer.h | 1 +
 lib/utils/timer/aclint_mtimer.c         | 3 +++
 lib/utils/timer/fdt_timer_mtimer.c      | 4 ++++
 platform/fpga/ariane/platform.c         | 2 ++
 platform/fpga/openpiton/platform.c      | 7 +++++++
 platform/kendryte/k210/platform.c       | 1 +
 platform/kendryte/k210/platform.h       | 2 +-
 platform/nuclei/ux600/platform.c        | 1 +
 platform/template/platform.c            | 2 ++
 10 files changed, 25 insertions(+), 1 deletion(-)
include/sbi/sbi_timer.h +3 -0
index 1ba6da0..211e83d 100644
@@ -17,6 +17,9 @@ struct sbi_timer_device {
        /** Name of the timer operations */
        char name[32];
 
+       /** Frequency of timer in HZ */
+       unsigned long timer_freq;
+
        /** Get free-running timer value */
        u64 (*timer_value)(void);
 
include/sbi_utils/timer/aclint_mtimer.h +1 -0
index a9fe445..f02cc62 100644
@@ -24,6 +24,7 @@
 
 struct aclint_mtimer_data {
        /* Public details */
+       unsigned long mtime_freq;
        unsigned long mtime_addr;
        unsigned long mtime_size;
        unsigned long mtimecmp_addr;
lib/utils/timer/aclint_mtimer.c +2 -0
index d612b12..62e87f7 100644
@@ -186,6 +186,8 @@ int aclint_mtimer_cold_init(struct
 aclint_mtimer_data *mt,
            (mt->first_hartid >= SBI_HARTMASK_MAX_BITS) ||
            (mt->hart_count > ACLINT_MTIMER_MAX_HARTS))
                return SBI_EINVAL;
+       if (reference && mt->mtime_freq != reference->mtime_freq)
+               return SBI_EINVAL;
 
        /* Initialize private data */
        aclint_mtimer_set_reference(mt, reference);
lib/utils/timer/aclint_mtimer.c +1 -0
@@ -227,6 +229,7 @@ int aclint_mtimer_cold_init(struct
 aclint_mtimer_data *mt,
                        return rc;
        }
 
+       mtimer.timer_freq = mt->mtime_freq;
        sbi_timer_set_device(&mtimer);
 
        return 0;
lib/utils/timer/fdt_timer_mtimer.c +4 -0
index 4eafffa..1ad8508 100644
@@ -38,6 +38,10 @@ static int timer_mtimer_cold_init(void *fdt, int
 nodeoff,
        mt->has_64bit_mmio = true;
        mt->has_shared_mtime = false;
 
+       rc = fdt_parse_timebase_frequency(fdt, &mt->mtime_freq);
+       if (rc)
+               return rc;
+
        if (match->data) { /* SiFive CLINT */
                /* Set CLINT addresses */
                mt->mtimecmp_addr = addr[0] +

ACLINT_DEFAULT_MTIMECMP_OFFSET;

platform/fpga/ariane/platform.c +1 -0
index 58a46c0..5acc446 100644
@@ -26,6 +26,7 @@
 #define ARIANE_PLIC_NUM_SOURCES                        3
 #define ARIANE_HART_COUNT                      1
 #define ARIANE_CLINT_ADDR                      0x2000000
+#define ARIANE_ACLINT_MTIMER_FREQ              1000000
 #define
 ARIANE_ACLINT_MSWI_ADDR                        (ARIANE_CLINT_ADDR + \
                                                 CLINT_MSWI_OFFSET)

#define ARIANE_ACLINT_MTIMER_ADDR (ARIANE_CLINT_ADDR +
\

platform/fpga/ariane/platform.c +1 -0
@@ -44,6 +45,7 @@ static struct aclint_mswi_data mswi = {
 };
 
 static struct aclint_mtimer_data mtimer = {
+       .mtime_freq = ARIANE_ACLINT_MTIMER_FREQ,
        .mtime_addr = ARIANE_ACLINT_MTIMER_ADDR +
                      ACLINT_DEFAULT_MTIME_OFFSET,
        .mtime_size = ACLINT_DEFAULT_MTIME_SIZE,
platform/fpga/openpiton/platform.c +7 -0
index a9bfa99..924053e 100644
@@ -26,6 +26,7 @@
 #define OPENPITON_DEFAULT_PLIC_NUM_SOURCES     2
 #define OPENPITON_DEFAULT_HART_COUNT           3
 #define OPENPITON_DEFAULT_CLINT_ADDR           0xfff1020000
+#define OPENPITON_DEFAULT_ACLINT_MTIMER_FREQ   1000000
 #define OPENPITON_DEFAULT_ACLINT_MSWI_ADDR     \
                (OPENPITON_DEFAULT_CLINT_ADDR + CLINT_MSWI_OFFSET)
 #define OPENPITON_DEFAULT_ACLINT_MTIMER_ADDR   \
@@ -49,6 +50,7 @@ static struct aclint_mswi_data mswi = {
 };
 
 static struct aclint_mtimer_data mtimer = {
+       .mtime_freq = OPENPITON_DEFAULT_ACLINT_MTIMER_FREQ,
        .mtime_addr = OPENPITON_DEFAULT_ACLINT_MTIMER_ADDR +
                      ACLINT_DEFAULT_MTIME_OFFSET,
        .mtime_size = ACLINT_DEFAULT_MTIME_SIZE,
@@ -68,6 +70,7 @@ static int openpiton_early_init(bool cold_boot)
        void *fdt;
        struct platform_uart_data uart_data;
        struct plic_data plic_data;
+       unsigned long aclint_freq;
        uint64_t clint_addr;
        int rc;
 
@@ -83,6 +86,10 @@ static int openpiton_early_init(bool cold_boot)
        if (!rc)
                plic = plic_data;
 
+       rc = fdt_parse_timebase_frequency(fdt, &aclint_freq);
+       if (!rc)
+               mtimer.mtime_freq = aclint_freq;
+
        rc = fdt_parse_compat_addr(fdt, &clint_addr, "riscv,clint0");
        if (!rc) {
                mswi.addr = clint_addr;
platform/kendryte/k210/platform.c +1 -0
index ee4c223..35cec5f 100644
@@ -42,6 +42,7 @@ static struct aclint_mswi_data mswi = {
 };
 
 static struct aclint_mtimer_data mtimer = {
+       .mtime_freq = K210_ACLINT_MTIMER_FREQ,
        .mtime_addr = K210_ACLINT_MTIMER_ADDR +
                      ACLINT_DEFAULT_MTIME_OFFSET,
        .mtime_size = ACLINT_DEFAULT_MTIME_SIZE,
platform/kendryte/k210/platform.h +1 -1
index 0a32530..be52aa3 100644
@@ -14,7 +14,7 @@
 #define K210_HART_COUNT                2
 
 #define K210_UART_BAUDRATE     115200
-
+#define K210_ACLINT_MTIMER_FREQ        7800000
 #define K210_CLK0_FREQ         26000000UL
 #define K210_PLIC_NUM_SOURCES  65
 
platform/nuclei/ux600/platform.c +1 -0
index ab0becc..6bef4c4 100644
@@ -74,6 +74,7 @@ static struct aclint_mswi_data mswi = {
 };
 
 static struct aclint_mtimer_data mtimer = {
+       .mtime_freq = UX600_TIMER_FREQ,
        .mtime_addr = UX600_ACLINT_MTIMER_ADDR +
                      ACLINT_DEFAULT_MTIME_OFFSET,
        .mtime_size = ACLINT_DEFAULT_MTIME_SIZE,
platform/template/platform.c +2 -1
index 4528822..d6806e6 100644
@@ -22,6 +22,7 @@
 #define PLATFORM_PLIC_NUM_SOURCES      128
 #define PLATFORM_HART_COUNT            4
 #define PLATFORM_CLINT_ADDR            0x2000000
+#define PLATFORM_ACLINT_MTIMER_FREQ    10000000
 #define PLATFORM_ACLINT_MSWI_ADDR      (PLATFORM_CLINT_ADDR + \
                                         CLINT_MSWI_OFFSET)
 #define PLATFORM_ACLINT_MTIMER_ADDR    (PLATFORM_CLINT_ADDR + \
@@ -43,6 +44,7 @@ static struct aclint_mswi_data mswi = {
 };
 
 static struct aclint_mtimer_data mtimer = {
+       .mtime_freq = PLATFORM_ACLINT_MTIMER_FREQ,
        .mtime_addr = PLATFORM_ACLINT_MTIMER_ADDR +
                      ACLINT_DEFAULT_MTIME_OFFSET,
        .mtime_size = ACLINT_DEFAULT_MTIME_SIZE,
--

2.25.1

author
#7 lore raw

PATCH v3 3/5 lib: sbi: Print timer frequency at boot time

In reply to Anup Patel

We now have frequency in timer device instance provided by platform so let's print timer frequency as part of the boot prints.

---
 lib/sbi/sbi_init.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
lib/sbi/sbi_init.c +3 -2
index 6ef8742..f0eb365 100644
@@ -75,8 +75,9 @@ static void sbi_boot_print_general(struct sbi_scratch *scratch)
 	sbi_printf("Platform IPI Device       : %s\n",
 		   (idev) ? idev->name : "---");
 	tdev = sbi_timer_get_device();
-	sbi_printf("Platform Timer Device     : %s\n",
-		   (tdev) ? tdev->name : "---");
+	sbi_printf("Platform Timer Device     : %s @ %luHz\n",
+		   (tdev) ? tdev->name : "---",
+		   (tdev) ? tdev->timer_freq : 0);
 	cdev = sbi_console_get_device();
 	sbi_printf("Platform Console Device   : %s\n",
 		   (cdev) ? cdev->name : "---");
Signature · 1 line
2.25.1
#8 lore raw

PATCH v3 3/5 lib: sbi: Print timer frequency at boot time

? 2021-09-25???? 10:07 +0530?Anup Patel???

Quoted 5 lines

We now have frequency in timer device instance provided by platform so let's print timer frequency as part of the boot prints.

Quoted 25 lines

---
?lib/sbi/sbi_init.c | 5 +++--
?1 file changed, 3 insertions(+), 2 deletions(-)

lib/sbi/sbi_init.c +3 -2
index 6ef8742..f0eb365 100644
@@ -75,8 +75,9 @@ static void sbi_boot_print_general(struct
 sbi_scratch *scratch)
 ????????sbi_printf("Platform IPI Device?????? : %s\n",
 ?????????????????? (idev) ? idev->name : "---");
 ????????tdev = sbi_timer_get_device();
-???????sbi_printf("Platform Timer Device???? : %s\n",
-????????????????? (tdev) ? tdev->name : "---");
+???????sbi_printf("Platform Timer Device???? : %s @ %luHz\n",
+????????????????? (tdev) ? tdev->name : "---",
+????????????????? (tdev) ? tdev->timer_freq : 0);
 ????????cdev = sbi_console_get_device();
 ????????sbi_printf("Platform Console Device?? : %s\n",

?????????????????? (cdev) ? cdev->name : "---");

Signature · 1 line
2.25.1
Applied
#9 lore raw

PATCH v3 3/5 lib: sbi: Print timer frequency at boot time

Quoted 8 lines · Xiang W

? 2021-09-25???? 10:07 +0530?Anup Patel???

Quoted 5 lines

We now have frequency in timer device instance provided by platform so let's print timer frequency as part of the boot prints.

Applied this patch to the riscv/opensbi repo

Regards,
Anup

Quoted 27 lines
Quoted 25 lines
---
 lib/sbi/sbi_init.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
lib/sbi/sbi_init.c +3 -2
index 6ef8742..f0eb365 100644
@@ -75,8 +75,9 @@ static void sbi_boot_print_general(struct
 sbi_scratch *scratch)
        sbi_printf("Platform IPI Device       : %s\n",
                   (idev) ? idev->name : "---");
        tdev = sbi_timer_get_device();
-       sbi_printf("Platform Timer Device     : %s\n",
-                  (tdev) ? tdev->name : "---");
+       sbi_printf("Platform Timer Device     : %s @ %luHz\n",
+                  (tdev) ? tdev->name : "---",
+                  (tdev) ? tdev->timer_freq : 0);
        cdev = sbi_console_get_device();
        sbi_printf("Platform Console Device   : %s\n",
                   (cdev) ? cdev->name : "---");

--
2.25.1

author
#10 lore raw

PATCH v3 4/5 lib: sbi: Add generic timer delay loop function

In reply to Anup Patel

We now have frequency of the timer device provided by the platform support so we can emulate desired delay using a loop where the number loop iterations are based on timer frequency.

This patch provides sbi_timer_delay_loop() for above purpose.

---
 include/sbi/sbi_timer.h | 16 ++++++++++++++++
 lib/sbi/sbi_timer.c     | 34 ++++++++++++++++++++++++++++++++++
 2 files changed, 50 insertions(+)
include/sbi/sbi_timer.h +16 -0
index 211e83d..63ef1af 100644
@@ -32,6 +32,22 @@ struct sbi_timer_device {
 
 struct sbi_scratch;
 
+/** Generic delay loop of desired granularity */
+void sbi_timer_delay_loop(ulong units, u64 unit_freq,
+			  void (*delay_fn)(void *), void *opaque);
+
+/** Provide delay in terms of milliseconds */
+static inline void sbi_timer_mdelay(ulong msecs)
+{
+	sbi_timer_delay_loop(msecs, 1000, NULL, NULL);
+}
+
+/** Provide delay in terms of microseconds */
+static inline void sbi_timer_udelay(ulong usecs)
+{
+	sbi_timer_delay_loop(usecs, 1000000, NULL, NULL);
+}
+
 /** Get timer value for current HART */
 u64 sbi_timer_value(void);
 
lib/sbi/sbi_timer.c +34 -0
index 2759501..acdba92 100644
@@ -8,7 +8,9 @@
  */
 
 #include <sbi/riscv_asm.h>
+#include <sbi/riscv_barrier.h>
 #include <sbi/riscv_encoding.h>
+#include <sbi/sbi_console.h>
 #include <sbi/sbi_error.h>
 #include <sbi/sbi_hart.h>
 #include <sbi/sbi_platform.h>
@@ -47,6 +49,38 @@ static u64 get_platform_ticks(void)
 	return timer_dev->timer_value();
 }
 
+static void nop_delay_fn(void *opaque)
+{
+	cpu_relax();
+}
+
+void sbi_timer_delay_loop(ulong units, u64 unit_freq,
+			  void (*delay_fn)(void *), void *opaque)
+{
+	u64 start_val, delta;
+
+	/* Do nothing if we don't have timer device */
+	if (!timer_dev || !get_time_val) {
+		sbi_printf("%s: called without timer device\n", __func__);
+		return;
+	}
+
+	/* Save starting timer value */
+	start_val = get_time_val();
+
+	/* Compute desired timer value delta */
+	delta = ((u64)timer_dev->timer_freq * (u64)units);
+	delta = delta / unit_freq;
+
+	/* Use NOP delay function if delay function not available */
+	if (!delay_fn)
+		delay_fn = nop_delay_fn;
+
+	/* Busy loop until desired timer value delta reached */
+	while ((get_time_val() - start_val) < delta)
+		delay_fn(opaque);
+}
+
 u64 sbi_timer_value(void)
 {
 	if (get_time_val)
Signature · 1 line
2.25.1
#11 lore raw

PATCH v3 4/5 lib: sbi: Add generic timer delay loop function

? 2021-09-25???? 10:07 +0530?Anup Patel???

Quoted 8 lines

We now have frequency of the timer device provided by the platform support so we can emulate desired delay using a loop where the number loop iterations are based on timer frequency.

This patch provides sbi_timer_delay_loop() for above purpose.

Quoted 90 lines

---
?include/sbi/sbi_timer.h | 16 ++++++++++++++++ ?lib/sbi/sbi_timer.c???? | 34 ++++++++++++++++++++++++++++++++++ ?2 files changed, 50 insertions(+)

include/sbi/sbi_timer.h +0 -0
index 211e83d..63ef1af 100644
@@ -32,6 +32,22 @@ struct sbi_timer_device {

?
?struct sbi_scratch;
?

include/sbi/sbi_timer.h +16 -0
+/** Generic delay loop of desired granularity */
+void sbi_timer_delay_loop(ulong units, u64 unit_freq,
+???????????????????????? void (*delay_fn)(void *), void *opaque);
+
+/** Provide delay in terms of milliseconds */
+static inline void sbi_timer_mdelay(ulong msecs)
+{
+???????sbi_timer_delay_loop(msecs, 1000, NULL, NULL);
+}
+
+/** Provide delay in terms of microseconds */
+static inline void sbi_timer_udelay(ulong usecs)
+{
+???????sbi_timer_delay_loop(usecs, 1000000, NULL, NULL);
+}
+

?/** Get timer value for current HART */
?u64 sbi_timer_value(void);
?

lib/sbi/sbi_timer.c +0 -0
index 2759501..acdba92 100644
@@ -8,7 +8,9 @@

? */
?
?#include <sbi/riscv_asm.h>
+#include <sbi/riscv_barrier.h>
?#include <sbi/riscv_encoding.h>
+#include <sbi/sbi_console.h>
?#include <sbi/sbi_error.h>
?#include <sbi/sbi_hart.h>
?#include <sbi/sbi_platform.h>

lib/sbi/sbi_timer.c +0 -0
@@ -47,6 +49,38 @@ static u64 get_platform_ticks(void)
 ????????return timer_dev->timer_value();
 ?}

?

lib/sbi/sbi_timer.c +13 -0
+static void nop_delay_fn(void *opaque)
+{
+???????cpu_relax();
+}
+
+void sbi_timer_delay_loop(ulong units, u64 unit_freq,
+???????????????????????? void (*delay_fn)(void *), void *opaque)
+{
+???????u64 start_val, delta;
+
+???????/* Do nothing if we don't have timer device */
+???????if (!timer_dev || !get_time_val) {
+???????????????sbi_printf("%s: called without timer device\n",

__func__);
+???????????????return;
+???????}

lib/sbi/sbi_timer.c +17 -0
+
+???????/* Save starting timer value */
+???????start_val = get_time_val();
+
+???????/* Compute desired timer value delta */
+???????delta = ((u64)timer_dev->timer_freq * (u64)units);
+???????delta = delta / unit_freq;
+
+???????/* Use NOP delay function if delay function not available */
+???????if (!delay_fn)
+???????????????delay_fn = nop_delay_fn;
+
+???????/* Busy loop until desired timer value delta reached */
+???????while ((get_time_val() - start_val) < delta)
+???????????????delay_fn(opaque);
+}
+

?u64 sbi_timer_value(void)
?{
????????if (get_time_val)

Signature · 1 line
2.25.1
Applied
#12 lore raw

PATCH v3 4/5 lib: sbi: Add generic timer delay loop function

Quoted 11 lines · Xiang W

? 2021-09-25???? 10:07 +0530?Anup Patel???

Quoted 8 lines

We now have frequency of the timer device provided by the platform support so we can emulate desired delay using a loop where the number loop iterations are based on timer frequency.

This patch provides sbi_timer_delay_loop() for above purpose.

Applied this patch to the riscv/opensbi repo

Regards,
Anup

Quoted 92 lines
Quoted 90 lines
---
 include/sbi/sbi_timer.h | 16 ++++++++++++++++
 lib/sbi/sbi_timer.c     | 34 ++++++++++++++++++++++++++++++++++
 2 files changed, 50 insertions(+)
include/sbi/sbi_timer.h +16 -0
index 211e83d..63ef1af 100644
@@ -32,6 +32,22 @@ struct sbi_timer_device {
 
 struct sbi_scratch;
 
+/** Generic delay loop of desired granularity */
+void sbi_timer_delay_loop(ulong units, u64 unit_freq,
+                         void (*delay_fn)(void *), void *opaque);
+
+/** Provide delay in terms of milliseconds */
+static inline void sbi_timer_mdelay(ulong msecs)
+{
+       sbi_timer_delay_loop(msecs, 1000, NULL, NULL);
+}
+
+/** Provide delay in terms of microseconds */
+static inline void sbi_timer_udelay(ulong usecs)
+{
+       sbi_timer_delay_loop(usecs, 1000000, NULL, NULL);
+}
+
 /** Get timer value for current HART */
 u64 sbi_timer_value(void);
 
lib/sbi/sbi_timer.c +34 -0
index 2759501..acdba92 100644
@@ -8,7 +8,9 @@
  */
 
 #include <sbi/riscv_asm.h>
+#include <sbi/riscv_barrier.h>
 #include <sbi/riscv_encoding.h>
+#include <sbi/sbi_console.h>
 #include <sbi/sbi_error.h>
 #include <sbi/sbi_hart.h>
 #include <sbi/sbi_platform.h>
@@ -47,6 +49,38 @@ static u64 get_platform_ticks(void)
        return timer_dev->timer_value();
 }
 
+static void nop_delay_fn(void *opaque)
+{
+       cpu_relax();
+}
+
+void sbi_timer_delay_loop(ulong units, u64 unit_freq,
+                         void (*delay_fn)(void *), void *opaque)
+{
+       u64 start_val, delta;
+
+       /* Do nothing if we don't have timer device */
+       if (!timer_dev || !get_time_val) {
+               sbi_printf("%s: called without timer device\n",
 __func__);
+               return;
+       }
+
+       /* Save starting timer value */
+       start_val = get_time_val();
+
+       /* Compute desired timer value delta */
+       delta = ((u64)timer_dev->timer_freq * (u64)units);
+       delta = delta / unit_freq;
+
+       /* Use NOP delay function if delay function not available */
+       if (!delay_fn)
+               delay_fn = nop_delay_fn;
+
+       /* Busy loop until desired timer value delta reached */
+       while ((get_time_val() - start_val) < delta)
+               delay_fn(opaque);
+}
+
 u64 sbi_timer_value(void)
 {

if (get_time_val)
--
2.25.1

author
#13 lore raw

PATCH v3 5/5 lib: utils/reset: use sbi_timer_mdelay() in gpio reset driver

In reply to Anup Patel

We should use sbi_timer_mdelay() instead of custom gpio_mdelay() in the gpio reset driver.

---
 lib/utils/reset/fdt_reset_gpio.c | 13 +++----------
 1 file changed, 3 insertions(+), 10 deletions(-)
lib/utils/reset/fdt_reset_gpio.c +3 -10
index 77e308a..30127f5 100644
@@ -14,6 +14,7 @@
 #include <sbi/sbi_ecall_interface.h>
 #include <sbi/sbi_hart.h>
 #include <sbi/sbi_system.h>
+#include <sbi/sbi_timer.h>
 #include <sbi_utils/fdt/fdt_helper.h>
 #include <sbi_utils/gpio/fdt_gpio.h>
 #include <sbi_utils/reset/fdt_reset.h>
@@ -34,14 +35,6 @@ static struct gpio_reset restart = {
 	.inactive_delay = 100
 };
 
-/* Custom mdelay function until we have a generic mdelay() API */
-static void gpio_mdelay(unsigned long msecs)
-{
-	volatile int i;
-	while (msecs--)
-		for (i = 0; i < 100000; i++) ;
-}
-
 static int gpio_system_reset_check(u32 type, u32 reason)
 {
 	switch (type) {
@@ -76,11 +69,11 @@ static void gpio_system_reset(u32 type, u32 reason)
 
 		/* drive it active, also inactive->active edge */
 		gpio_direction_output(&reset->pin, 1);
-		gpio_mdelay(reset->active_delay);
+		sbi_timer_mdelay(reset->active_delay);
 
 		/* drive inactive, also active->inactive edge */
 		gpio_set(&reset->pin, 0);
-		gpio_mdelay(reset->inactive_delay);
+		sbi_timer_mdelay(reset->inactive_delay);
 
 		/* drive it active, also inactive->active edge */
 		gpio_set(&reset->pin, 1);
Signature · 1 line
2.25.1
#14 lore raw

PATCH v3 5/5 lib: utils/reset: use sbi_timer_mdelay() in gpio reset driver

? 2021-09-25???? 10:07 +0530?Anup Patel???

Quoted 5 lines

We should use sbi_timer_mdelay() instead of custom gpio_mdelay() in the gpio reset driver.

Quoted 51 lines

---
?lib/utils/reset/fdt_reset_gpio.c | 13 +++---------- ?1 file changed, 3 insertions(+), 10 deletions(-)

lib/utils/reset/fdt_reset_gpio.c +0 -0
index 77e308a..30127f5 100644
@@ -14,6 +14,7 @@

?#include <sbi/sbi_ecall_interface.h>
?#include <sbi/sbi_hart.h>
?#include <sbi/sbi_system.h>
+#include <sbi/sbi_timer.h>
?#include <sbi_utils/fdt/fdt_helper.h>
?#include <sbi_utils/gpio/fdt_gpio.h>
?#include <sbi_utils/reset/fdt_reset.h>

lib/utils/reset/fdt_reset_gpio.c +0 -0
@@ -34,14 +35,6 @@ static struct gpio_reset restart = {

????????.inactive_delay = 100
?};
?

lib/utils/reset/fdt_reset_gpio.c +0 -8
-/* Custom mdelay function until we have a generic mdelay() API */
-static void gpio_mdelay(unsigned long msecs)
-{
-???????volatile int i;
-???????while (msecs--)
-???????????????for (i = 0; i < 100000; i++) ;
-}
-

?static int gpio_system_reset_check(u32 type, u32 reason) ?{
????????switch (type) {

lib/utils/reset/fdt_reset_gpio.c +0 -0
@@ -76,11 +69,11 @@ static void gpio_system_reset(u32 type, u32
 reason)

?
????????????????/* drive it active, also inactive->active edge */ ????????????????gpio_direction_output(&reset->pin, 1); -???????????????gpio_mdelay(reset->active_delay); +???????????????sbi_timer_mdelay(reset->active_delay); ?
????????????????/* drive inactive, also active->inactive edge */ ????????????????gpio_set(&reset->pin, 0); -???????????????gpio_mdelay(reset->inactive_delay); +???????????????sbi_timer_mdelay(reset->inactive_delay); ?
????????????????/* drive it active, also inactive->active edge */ ????????????????gpio_set(&reset->pin, 1);

Signature · 1 line
2.25.1
Applied
#15 lore raw

PATCH v3 5/5 lib: utils/reset: use sbi_timer_mdelay() in gpio reset driver

Quoted 8 lines · Xiang W

? 2021-09-25???? 10:07 +0530?Anup Patel???

Quoted 5 lines

We should use sbi_timer_mdelay() instead of custom gpio_mdelay() in the gpio reset driver.

Applied this patch to the riscv/opensbi repo

Regards,
Anup

Quoted 53 lines
Quoted 51 lines
---
 lib/utils/reset/fdt_reset_gpio.c | 13 +++----------
 1 file changed, 3 insertions(+), 10 deletions(-)
lib/utils/reset/fdt_reset_gpio.c +3 -10
index 77e308a..30127f5 100644
@@ -14,6 +14,7 @@
 #include <sbi/sbi_ecall_interface.h>
 #include <sbi/sbi_hart.h>
 #include <sbi/sbi_system.h>
+#include <sbi/sbi_timer.h>
 #include <sbi_utils/fdt/fdt_helper.h>
 #include <sbi_utils/gpio/fdt_gpio.h>
 #include <sbi_utils/reset/fdt_reset.h>
@@ -34,14 +35,6 @@ static struct gpio_reset restart = {
        .inactive_delay = 100
 };
 
-/* Custom mdelay function until we have a generic mdelay() API */
-static void gpio_mdelay(unsigned long msecs)
-{
-       volatile int i;
-       while (msecs--)
-               for (i = 0; i < 100000; i++) ;
-}
-
 static int gpio_system_reset_check(u32 type, u32 reason)
 {
        switch (type) {
@@ -76,11 +69,11 @@ static void gpio_system_reset(u32 type, u32
 reason)
 
                /* drive it active, also inactive->active edge */
                gpio_direction_output(&reset->pin, 1);
-               gpio_mdelay(reset->active_delay);
+               sbi_timer_mdelay(reset->active_delay);
 
                /* drive inactive, also active->inactive edge */
                gpio_set(&reset->pin, 0);
-               gpio_mdelay(reset->inactive_delay);
+               sbi_timer_mdelay(reset->inactive_delay);
 
                /* drive it active, also inactive->active edge */
                gpio_set(&reset->pin, 1);

--
2.25.1