diff --git a/sys/riscv/riscv/timer.c b/sys/riscv/riscv/timer.c index 93d0cefbc3a5..5ee5ca221584 100644 --- a/sys/riscv/riscv/timer.c +++ b/sys/riscv/riscv/timer.c @@ -61,7 +61,10 @@ __FBSDID("$FreeBSD$"); #include #include -#define DEFAULT_FREQ 10000000 +#include +#include +#include +#include #define TIMER_COUNTS 0x00 #define TIMER_MTIMECMP(cpu) (cpu * 8) @@ -156,6 +159,32 @@ riscv_timer_intr(void *arg) return (FILTER_HANDLED); } +static int +riscv_timer_get_timebase(device_t dev, uint32_t *freq) +{ + phandle_t node; + int len; + + node = OF_finddevice("/cpus"); + if (node == -1) { + if (bootverbose) + device_printf(dev, "Can't find cpus node.\n"); + return (ENXIO); + } + + len = OF_getproplen(node, "timebase-frequency"); + if (len != 4) { + if (bootverbose) + device_printf(dev, + "Can't find timebase-frequency property.\n"); + return (ENXIO); + } + + OF_getencprop(node, "timebase-frequency", freq, len); + + return (0); +} + static int riscv_timer_probe(device_t dev) { @@ -176,10 +205,9 @@ riscv_timer_attach(device_t dev) return (ENXIO); if (device_get_unit(dev) != 0) - return ENXIO; + return (ENXIO); - sc->clkfreq = DEFAULT_FREQ; - if (sc->clkfreq == 0) { + if (riscv_timer_get_timebase(dev, &sc->clkfreq) != 0) { device_printf(dev, "No clock frequency specified\n"); return (ENXIO); }