2 * Copyright (c) 2015-2017 Ruslan Bukin <br@bsdpad.com>
5 * Portions of this software were developed by SRI International and the
6 * University of Cambridge Computer Laboratory under DARPA/AFRL contract
7 * FA8750-10-C-0237 ("CTSRD"), as part of the DARPA CRASH research programme.
9 * Portions of this software were developed by the University of Cambridge
10 * Computer Laboratory as part of the CTSRD Project, with support from the
11 * UK Higher Education Innovation Fund (HEIF).
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 #include "opt_platform.h"
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
44 #include <sys/param.h>
45 #include <sys/systm.h>
47 #include <sys/kernel.h>
48 #include <sys/module.h>
49 #include <sys/timeet.h>
50 #include <sys/timetc.h>
52 #include <sys/watchdog.h>
54 #include <machine/cpufunc.h>
55 #include <machine/intr.h>
56 #include <machine/md_var.h>
57 #include <machine/sbi.h>
59 #include <dev/ofw/openfirm.h>
61 struct riscv_timer_softc {
66 static struct riscv_timer_softc *riscv_timer_sc = NULL;
68 static timecounter_get_t riscv_timer_tc_get_timecount;
69 static timecounter_fill_vdso_timehands_t riscv_timer_tc_fill_vdso_timehands;
71 static struct timecounter riscv_timer_timecount = {
72 .tc_name = "RISC-V Timecounter",
73 .tc_get_timecount = riscv_timer_tc_get_timecount,
75 .tc_counter_mask = ~0u,
78 .tc_fill_vdso_timehands = riscv_timer_tc_fill_vdso_timehands,
81 static inline uint64_t
89 set_timecmp(uint64_t timecmp)
93 csr_write(stimecmp, timecmp);
95 sbi_set_timer(timecmp);
99 riscv_timer_tc_get_timecount(struct timecounter *tc __unused)
102 return (get_timecount());
106 riscv_timer_tc_fill_vdso_timehands(struct vdso_timehands *vdso_th,
107 struct timecounter *tc)
109 vdso_th->th_algo = VDSO_TH_ALGO_RISCV_RDTIME;
110 bzero(vdso_th->th_res, sizeof(vdso_th->th_res));
115 riscv_timer_et_start(struct eventtimer *et, sbintime_t first, sbintime_t period)
120 counts = ((uint32_t)et->et_frequency * first) >> 32;
121 set_timecmp(get_timecount() + counts);
122 csr_set(sie, SIE_STIE);
131 riscv_timer_et_stop(struct eventtimer *et)
134 /* Disable timer interrupts. */
135 csr_clear(sie, SIE_STIE);
141 riscv_timer_intr(void *arg)
143 struct riscv_timer_softc *sc;
145 sc = (struct riscv_timer_softc *)arg;
147 csr_clear(sip, SIP_STIP);
149 if (sc->et.et_active)
150 sc->et.et_event_cb(&sc->et, sc->et.et_arg);
152 return (FILTER_HANDLED);
156 riscv_timer_get_timebase(device_t dev, uint32_t *freq)
161 node = OF_finddevice("/cpus");
164 device_printf(dev, "Can't find cpus node.\n");
168 len = OF_getproplen(node, "timebase-frequency");
172 "Can't find timebase-frequency property.\n");
176 OF_getencprop(node, "timebase-frequency", freq, len);
182 riscv_timer_probe(device_t dev)
185 device_set_desc(dev, "RISC-V Timer");
187 return (BUS_PROBE_DEFAULT);
191 riscv_timer_attach(device_t dev)
193 struct riscv_timer_softc *sc;
196 sc = device_get_softc(dev);
197 if (riscv_timer_sc != NULL)
200 if (device_get_unit(dev) != 0)
203 if (riscv_timer_get_timebase(dev, &sc->clkfreq) != 0) {
204 device_printf(dev, "No clock frequency specified\n");
210 /* Setup IRQs handler */
211 error = riscv_setup_intr(device_get_nameunit(dev), riscv_timer_intr,
212 NULL, sc, IRQ_TIMER_SUPERVISOR, INTR_TYPE_CLK, &sc->ih);
214 device_printf(dev, "Unable to alloc int resource.\n");
218 riscv_timer_timecount.tc_frequency = sc->clkfreq;
219 riscv_timer_timecount.tc_priv = sc;
220 tc_init(&riscv_timer_timecount);
222 sc->et.et_name = "RISC-V Eventtimer";
223 sc->et.et_flags = ET_FLAGS_ONESHOT | ET_FLAGS_PERCPU;
224 sc->et.et_quality = 1000;
226 sc->et.et_frequency = sc->clkfreq;
227 sc->et.et_min_period = (0x00000002LLU << 32) / sc->et.et_frequency;
228 sc->et.et_max_period = (0xfffffffeLLU << 32) / sc->et.et_frequency;
229 sc->et.et_start = riscv_timer_et_start;
230 sc->et.et_stop = riscv_timer_et_stop;
232 et_register(&sc->et);
234 set_cputicker(get_timecount, sc->clkfreq, false);
239 static device_method_t riscv_timer_methods[] = {
240 DEVMETHOD(device_probe, riscv_timer_probe),
241 DEVMETHOD(device_attach, riscv_timer_attach),
245 static driver_t riscv_timer_driver = {
248 sizeof(struct riscv_timer_softc),
251 EARLY_DRIVER_MODULE(timer, nexus, riscv_timer_driver, 0, 0,
252 BUS_PASS_TIMER + BUS_PASS_ORDER_MIDDLE);
257 int64_t counts, counts_per_usec;
258 uint64_t first, last;
261 * Check the timers are setup, if not just
262 * use a for loop for the meantime
264 if (riscv_timer_sc == NULL) {
265 for (; usec > 0; usec--)
266 for (counts = 200; counts > 0; counts--)
268 * Prevent the compiler from optimizing
276 /* Get the number of times to count */
277 counts_per_usec = ((riscv_timer_timecount.tc_frequency / 1000000) + 1);
280 * Clamp the timeout at a maximum value (about 32 seconds with
281 * a 66MHz clock). *Nobody* should be delay()ing for anywhere
282 * near that length of time and if they are, they should be hung
285 if (usec >= (0x80000000U / counts_per_usec))
286 counts = (0x80000000U / counts_per_usec) - 1;
288 counts = usec * counts_per_usec;
290 first = get_timecount();
293 last = get_timecount();
294 counts -= (int64_t)(last - first);