[PATCH v7 2/2] clocksource/drivers/timer-clint: Add T-Head C9xx clint
From: Jesse Taube
Date: Mon May 25 2026 - 14:22:17 EST
From: Jisheng Zhang <jszhang@xxxxxxxxxx>
To use the T-HEAD C9xx clint in RISCV-M NOMMU env, we need to take
care two points:
1.The mtimecmp in T-Head C9xx clint only supports 32bit read/write,
implement such support.
2. As pointed out by commit ca7810aecdba ("lib: utils/timer: mtimer:
add a quirk for lacking mtime register") of opensbi:
"T-Head developers surely have a different understanding of time CSR and
CLINT's mtime register with SiFive ones, that they did not implement
the mtime register at all -- as shown in openC906 source code, their
time CSR value is just exposed at the top of their processor IP block
and expects an external continuous counter, which makes it not
overrideable, and thus mtime register is not implemented, even not for
reading. However, if CLINTEE is not enabled in T-Head's MXSTATUS
extended CSR, these systems still rely on the mtimecmp registers to
generate timer interrupts. This makes it necessary to implement T-Head
C9xx CLINT support in OpenSBI MTIMER driver, which skips implementing
reading mtime register and falls back to default code that reads time
CSR."
So, we need to fall back to read time CSR instead of mtime register
using RISCV_ISA_EXT_ZICNTR.
Signed-off-by: Jisheng Zhang <jszhang@xxxxxxxxxx>
Signed-off-by: Jesse Taube <jtaubepe@xxxxxxxxxx>
---
Treat this as a completely new patch, as it is mostly rewritten.
Original:
https://lore.kernel.org/all/20240410142347.964-3-jszhang@xxxxxxxxxx/
V6 -> V7:
- New commit
- Split csr and c900 parts into different commits
- c900_clint_clock_next_event use csr_get_cycles64
over accedental use of clint_get_cycles64
---
drivers/clocksource/timer-clint.c | 25 +++++++++++++++++++++++++
1 file changed, 25 insertions(+)
diff --git a/drivers/clocksource/timer-clint.c b/drivers/clocksource/timer-clint.c
index 18f7bf7d26c6..62fe28df73fb 100644
--- a/drivers/clocksource/timer-clint.c
+++ b/drivers/clocksource/timer-clint.c
@@ -39,6 +39,7 @@ static u64 __iomem *clint_timer_cmp;
static u64 __iomem *clint_timer_val;
static unsigned long clint_timer_freq;
static unsigned int clint_timer_irq;
+static bool is_c900_clint;
#ifdef CONFIG_RISCV_M_MODE
u64 __iomem *clint_time_val;
@@ -154,6 +155,19 @@ static int clint_clock_next_event(unsigned long delta,
return 0;
}
+static int c900_clint_clock_next_event(unsigned long delta,
+ struct clock_event_device *ce)
+{
+ void __iomem *r = clint_timer_cmp +
+ cpuid_to_hartid_map(smp_processor_id());
+ u64 val = csr_get_cycles64() + delta;
+
+ csr_set(CSR_IE, IE_TIE);
+ writel_relaxed(val, r);
+ writel_relaxed(val >> 32, r + 4);
+ return 0;
+}
+
static DEFINE_PER_CPU(struct clock_event_device, clint_clock_event) = {
.name = "clint_clockevent",
.features = CLOCK_EVT_FEAT_ONESHOT,
@@ -168,6 +182,9 @@ static int clint_timer_starting_cpu(unsigned int cpu)
if (!riscv_has_extension_likely(RISCV_ISA_EXT_ZICNTR))
ce->set_next_event = clint_clock_next_event;
+ if (is_c900_clint)
+ ce->set_next_event = c900_clint_clock_next_event;
+
ce->cpumask = cpumask_of(cpu);
clockevents_config_and_register(ce, clint_timer_freq, 100, ULONG_MAX);
@@ -319,8 +336,16 @@ static int __init clint_timer_init(struct device_node *np)
static int __init clint_timer_init_dt(struct device_node *np)
{
+ is_c900_clint = false;
+ return clint_timer_init(np);
+}
+
+static int __init c900_clint_timer_init_dt(struct device_node *np)
+{
+ is_c900_clint = true;
return clint_timer_init(np);
}
TIMER_OF_DECLARE(clint_timer, "riscv,clint0", clint_timer_init_dt);
TIMER_OF_DECLARE(clint_timer1, "sifive,clint0", clint_timer_init_dt);
+TIMER_OF_DECLARE(clint_timer2, "thead,c900-clint", c900_clint_timer_init_dt);
--
2.54.0