Skip to content

Commit

Permalink
drivers: timer: Harmonize mtime-based RISC-V timers
Browse files Browse the repository at this point in the history
This commit replaces a bunch of ifdefs and bindings with a single
extensible binding, and makes all standard mtime system timers consistent.

Signed-off-by: Camille BAUD <[email protected]>
  • Loading branch information
VynDragon committed Jan 18, 2025
1 parent b7ec422 commit d638b22
Show file tree
Hide file tree
Showing 31 changed files with 152 additions and 206 deletions.
2 changes: 1 addition & 1 deletion boards/intel/niosv_g/niosv_g.dts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
};

&mtimer {
reg = <0x90000 0x10>;
reg = <0x90000 0x8 0x90008 0x8>;
};

&uart0 {
Expand Down
2 changes: 1 addition & 1 deletion boards/intel/niosv_m/niosv_m.dts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
};

&mtimer {
reg = <0x90000 0x10>;
reg = <0x90000 0x8 0x90008 0x8>;
};

&uart0 {
Expand Down
7 changes: 7 additions & 0 deletions boards/snps/nsim/arc_v/rmx1xx.dtsi
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@
interrupt-names = "soft0", "timer0";
};

mtimer: timer@200bff8 {
compatible = "riscv,mtime-timer";
interrupts-extended = <&cpu0_intc 7>;
reg = <0x200bff8 0x8 0x2004000 0x8>;
reg-names = "mtime", "mtimecmp";
};

uart0: serial@10000000{
compatible = "ns16550", "snps,dw-apb-uart";
reg = <0x10000000 0x400>;
Expand Down
9 changes: 2 additions & 7 deletions drivers/timer/Kconfig.riscv_machine
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,8 @@
config RISCV_MACHINE_TIMER
bool "RISCV Machine Timer"
default y
depends on DT_HAS_ANDESTECH_MACHINE_TIMER_ENABLED || \
DT_HAS_NEORV32_MACHINE_TIMER_ENABLED || \
DT_HAS_NUCLEI_SYSTIMER_ENABLED || \
DT_HAS_SIFIVE_CLINT0_ENABLED || \
DT_HAS_TELINK_MACHINE_TIMER_ENABLED || \
DT_HAS_LOWRISC_MACHINE_TIMER_ENABLED || \
DT_HAS_NIOSV_MACHINE_TIMER_ENABLED
depends on DT_HAS_RISCV_MTIME_TIMER_ENABLED || \
DT_HAS_NUCLEI_SYSTIMER_ENABLED
select TICKLESS_CAPABLE
select TIMER_HAS_64BIT_CYCLE_COUNTER
help
Expand Down
65 changes: 6 additions & 59 deletions drivers/timer/riscv_machine_timer.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/*
* Copyright (c) 2024 MASSDRIVER EI (massdriver.space)
* Copyright (c) 2018-2023 Intel Corporation
*
* SPDX-License-Identifier: Apache-2.0
Expand All @@ -13,65 +14,11 @@
#include <zephyr/spinlock.h>
#include <zephyr/irq.h>

/* andestech,machine-timer */
#if DT_HAS_COMPAT_STATUS_OKAY(andestech_machine_timer)
#define DT_DRV_COMPAT andestech_machine_timer

#define MTIME_REG DT_INST_REG_ADDR(0)
#define MTIMECMP_REG (DT_INST_REG_ADDR(0) + 8)
#define TIMER_IRQN DT_INST_IRQN(0)
/* neorv32-machine-timer */
#elif DT_HAS_COMPAT_STATUS_OKAY(neorv32_machine_timer)
#define DT_DRV_COMPAT neorv32_machine_timer

#define MTIME_REG DT_INST_REG_ADDR(0)
#define MTIMECMP_REG (DT_INST_REG_ADDR(0) + 8)
#define TIMER_IRQN DT_INST_IRQN(0)
/* nuclei,systimer */
#elif DT_HAS_COMPAT_STATUS_OKAY(nuclei_systimer)
#define DT_DRV_COMPAT nuclei_systimer

#define MTIME_REG DT_INST_REG_ADDR(0)
#define MTIMECMP_REG (DT_INST_REG_ADDR(0) + 8)
#define TIMER_IRQN DT_INST_IRQ_BY_IDX(0, 1, irq)
/* sifive,clint0 */
#elif DT_HAS_COMPAT_STATUS_OKAY(sifive_clint0)
#define DT_DRV_COMPAT sifive_clint0

#define MTIME_REG (DT_INST_REG_ADDR(0) + 0xbff8U)
#define MTIMECMP_REG (DT_INST_REG_ADDR(0) + 0x4000U)
#define TIMER_IRQN DT_INST_IRQ_BY_IDX(0, 1, irq)
/* telink,machine-timer */
#elif DT_HAS_COMPAT_STATUS_OKAY(telink_machine_timer)
#define DT_DRV_COMPAT telink_machine_timer

#define MTIME_REG DT_INST_REG_ADDR(0)
#define MTIMECMP_REG (DT_INST_REG_ADDR(0) + 8)
#define TIMER_IRQN DT_INST_IRQN(0)
/* lowrisc,machine-timer */
#elif DT_HAS_COMPAT_STATUS_OKAY(lowrisc_machine_timer)
#define DT_DRV_COMPAT lowrisc_machine_timer

#define MTIME_REG (DT_INST_REG_ADDR(0) + 0x110)
#define MTIMECMP_REG (DT_INST_REG_ADDR(0) + 0x118)
#define TIMER_IRQN DT_INST_IRQN(0)
/* niosv-machine-timer */
#elif DT_HAS_COMPAT_STATUS_OKAY(niosv_machine_timer)
#define DT_DRV_COMPAT niosv_machine_timer

#define MTIMECMP_REG DT_INST_REG_ADDR(0)
#define MTIME_REG (DT_INST_REG_ADDR(0) + 8)
#define TIMER_IRQN DT_INST_IRQN(0)
/* scr,machine-timer*/
#elif DT_HAS_COMPAT_STATUS_OKAY(scr_machine_timer)
#define DT_DRV_COMPAT scr_machine_timer
#define MTIMER_HAS_DIVIDER

#define MTIMEDIV_REG (DT_INST_REG_ADDR_U64(0) + 4)
#define MTIME_REG (DT_INST_REG_ADDR_U64(0) + 8)
#define MTIMECMP_REG (DT_INST_REG_ADDR_U64(0) + 16)
#define TIMER_IRQN DT_INST_IRQN(0)
#endif
#define DT_DRV_COMPAT riscv_mtime_timer

#define MTIME_REG DT_INST_REG_ADDR_BY_IDX(0, 0)
#define MTIMECMP_REG DT_INST_REG_ADDR_BY_IDX(0, 1)
#define TIMER_IRQN DT_INST_IRQN(0)

#define CYC_PER_TICK (uint32_t)(sys_clock_hw_cycles_per_sec() \
/ CONFIG_SYS_CLOCK_TICKS_PER_SEC)
Expand Down
File renamed without changes.
19 changes: 0 additions & 19 deletions dts/bindings/timer/andestech,machine-timer.yaml

This file was deleted.

19 changes: 0 additions & 19 deletions dts/bindings/timer/lowrisc,machine-timer.yaml

This file was deleted.

19 changes: 0 additions & 19 deletions dts/bindings/timer/neorv32-machine-timer.yaml

This file was deleted.

19 changes: 0 additions & 19 deletions dts/bindings/timer/niosv-machine-timer.yaml

This file was deleted.

8 changes: 1 addition & 7 deletions dts/bindings/timer/nuclei,systimer.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,9 @@ description: |
compatible: "nuclei,systimer"

include: base.yaml
include: "riscv,mtime-timer.yaml"

properties:
reg:
required: true

interrupts:
required: true

clk-divider:
type: int
description: |
Expand Down
14 changes: 14 additions & 0 deletions dts/bindings/timer/riscv,mtime-timer.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Copyright (c) 2025 MASSDRIVER EI (massdriver.space)
# SPDX-License-Identifier: Apache-2.0

description: RISC-V Standard mtime Timer.

compatible: "riscv,mtime-timer"

include: base.yaml

properties:
reg:
required: true
interrupts-extended:
required: true
8 changes: 0 additions & 8 deletions dts/bindings/timer/starfive,jh7100-clint.yaml

This file was deleted.

19 changes: 0 additions & 19 deletions dts/bindings/timer/telink,machine-timer.yaml

This file was deleted.

4 changes: 2 additions & 2 deletions dts/riscv/andes/andes_v5_ae350.dtsi
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,8 @@
};

mtimer: timer@e6000000 {
compatible = "andestech,machine-timer";
reg = <0xe6000000 0x10>;
compatible = "riscv,mtime-timer";
reg = <0xe6000000 0x8 0xe6000008 0x8>;
interrupts-extended = <&cpu0_intc 7 &cpu1_intc 7
&cpu2_intc 7 &cpu3_intc 7
&cpu4_intc 7 &cpu5_intc 7
Expand Down
7 changes: 7 additions & 0 deletions dts/riscv/efinix/sapphire_soc.dtsi
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@
reg = <0xf8b00000 0x10000>;
};

mtimer: timer@f8b0bff8 {
compatible = "riscv,mtime-timer";
interrupts-extended = <&hlic 7>;
reg = <0xf8b0bff8 0x8 0xf8b04000 0x8>;
reg-names = "mtime", "mtimecmp";
};

timer0: timer@e0002800 {
compatible = "efinix,sapphire-timer0";
reg = <0xe0002800 0x40>;
Expand Down
6 changes: 3 additions & 3 deletions dts/riscv/gd/gd32vf103.dtsi
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@
ranges;

systimer: timer@d1000000 {
compatible = "nuclei,systimer";
reg = <0xd1000000 0x10000>;
interrupts = <3 0>, <7 0>;
compatible = "nuclei,systimer", "riscv,mtime-timer";
reg = <0xd1000000 0x8 0xd1000008 0x8>;
interrupts-extended = <&eclic 7 0>;
clk-divider = <NUCLEI_SYSTIMER_DIVIDER_4>;
};

Expand Down
11 changes: 5 additions & 6 deletions dts/riscv/lowrisc/opentitan_earlgrey.dtsi
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,11 @@
reg = <0x10000000 0x10000>;
};

mtimer: timer@40100000 {
compatible = "lowrisc,machine-timer";
reg = <0x40100000 0x200>;
interrupts = <7 0>;
interrupt-parent = <&hlic>;
status = "disabled";
mtimer: timer@40100110 {
compatible = "riscv,mtime-timer";
reg = <0x40100110 0x8 0x40100118 0x8>;
reg-names = "mtime", "mtimecmp";
interrupts-extended = <&hlic 7>;
};

aontimer: aontimer@40470000 {
Expand Down
7 changes: 7 additions & 0 deletions dts/riscv/microchip/microchip-miv.dtsi
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@
reg = <0x44000000 0x10000>;
};

mtimer: timer@4400bff8 {
compatible = "riscv,mtime-timer";
interrupts-extended = <&hlic 7>;
reg = <0x4400bff8 0x8 0x44004000 0x8>;
reg-names = "mtime", "mtimecmp";
};

plic: interrupt-controller@40000000 {
compatible = "sifive,plic-1.0.0";
#address-cells = <0>;
Expand Down
11 changes: 11 additions & 0 deletions dts/riscv/microchip/mpfs.dtsi
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,17 @@
reg = <0x2000000 0x10000>;
};

mtimer: timer@200bff8 {
compatible = "riscv,mtime-timer";
interrupts-extended = <&hlic0 7
&hlic1 7
&hlic2 7
&hlic3 7
&hlic4 7>;
reg = <0x200bff8 0x8 0x2004000 0x8>;
reg-names = "mtime", "mtimecmp";
};

plic: interrupt-controller@c000000 {
compatible = "sifive,plic-1.0.0";
#interrupt-cells = <2>;
Expand Down
6 changes: 3 additions & 3 deletions dts/riscv/neorv32.dtsi
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@
ranges;

mtimer: timer@ffffff90 {
compatible = "neorv32-machine-timer";
reg = <0xffffff90 0x10>;
interrupts = <7>;
compatible = "riscv,mtime-timer";
reg = <0xffffff90 0x8 0xffffff98 0x8>;
interrupts-extended = <&intc 7>;
};

uart0: serial@ffffffa0 {
Expand Down
7 changes: 4 additions & 3 deletions dts/riscv/niosv/niosv-g.dtsi
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,10 @@
};

mtimer: machine-timer@90000 {
compatible = "niosv-machine-timer";
reg = <0x90000 0x10>;
interrupts = <7>;
compatible = "riscv,mtime-timer";
reg = <0x90000 0x8 0x90008 0x8>;
reg-names = "mtime", "mtimecmp";
interrupts-extended = <&intc 7>;
};

uart0: serial@90078 {
Expand Down
7 changes: 4 additions & 3 deletions dts/riscv/niosv/niosv-m.dtsi
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,10 @@
};

mtimer: machine-timer@90000 {
compatible = "niosv-machine-timer";
reg = <0x90000 0x10>;
interrupts = <7>;
compatible = "riscv,mtime-timer";
reg = <0x90000 0x8 0x90008 0x8>;
reg-names = "mtime", "mtimecmp";
interrupts-extended = <&intc 7>;
};

uart0: serial@90078 {
Expand Down
Loading

0 comments on commit d638b22

Please sign in to comment.