]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/riscv/riscv/timer.c
Update lldb to release_39 branch r276489 and resolve immediate conflicts.
[FreeBSD/FreeBSD.git] / sys / riscv / riscv / timer.c
1 /*-
2  * Copyright (c) 2015-2016 Ruslan Bukin <br@bsdpad.com>
3  * All rights reserved.
4  *
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.
8  *
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).
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
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.
21  *
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
32  * SUCH DAMAGE.
33  */
34
35 /*
36  * RISC-V Timer
37  */
38
39 #include "opt_platform.h"
40
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
43
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/bus.h>
47 #include <sys/kernel.h>
48 #include <sys/module.h>
49 #include <sys/malloc.h>
50 #include <sys/rman.h>
51 #include <sys/timeet.h>
52 #include <sys/timetc.h>
53 #include <sys/watchdog.h>
54
55 #include <sys/proc.h>
56
57 #include <machine/bus.h>
58 #include <machine/cpu.h>
59 #include <machine/intr.h>
60 #include <machine/asm.h>
61 #include <machine/trap.h>
62 #include <machine/sbi.h>
63
64 #include <dev/fdt/fdt_common.h>
65 #include <dev/ofw/openfirm.h>
66 #include <dev/ofw/ofw_bus.h>
67 #include <dev/ofw/ofw_bus_subr.h>
68
69 #define DEFAULT_FREQ    1000000
70
71 #define TIMER_COUNTS            0x00
72 #define TIMER_MTIMECMP(cpu)     (0x08 + (cpu * 8))
73
74 #define READ8(_sc, _reg)        \
75         bus_space_read_8(_sc->bst, _sc->bsh, _reg)
76 #define WRITE8(_sc, _reg, _val) \
77         bus_space_write_8(_sc->bst, _sc->bsh, _reg, _val)
78
79 struct riscv_tmr_softc {
80         struct resource         *res[2];
81         bus_space_tag_t         bst;
82         bus_space_handle_t      bsh;
83         void                    *ih;
84         uint32_t                clkfreq;
85         struct eventtimer       et;
86 };
87
88 static struct riscv_tmr_softc *riscv_tmr_sc = NULL;
89
90 static struct resource_spec timer_spec[] = {
91         { SYS_RES_MEMORY,       0,      RF_ACTIVE },
92         { SYS_RES_IRQ,          0,      RF_ACTIVE },
93         { -1, 0 }
94 };
95
96 static timecounter_get_t riscv_tmr_get_timecount;
97
98 static struct timecounter riscv_tmr_timecount = {
99         .tc_name           = "RISC-V Timecounter",
100         .tc_get_timecount  = riscv_tmr_get_timecount,
101         .tc_poll_pps       = NULL,
102         .tc_counter_mask   = ~0u,
103         .tc_frequency      = 0,
104         .tc_quality        = 1000,
105 };
106
107 static long
108 get_counts(struct riscv_tmr_softc *sc)
109 {
110
111         return (READ8(sc, TIMER_COUNTS));
112 }
113
114 static unsigned
115 riscv_tmr_get_timecount(struct timecounter *tc)
116 {
117         struct riscv_tmr_softc *sc;
118
119         sc = tc->tc_priv;
120
121         return (get_counts(sc));
122 }
123
124 static int
125 riscv_tmr_start(struct eventtimer *et, sbintime_t first, sbintime_t period)
126 {
127         struct riscv_tmr_softc *sc;
128         uint64_t counts;
129         int cpu;
130
131         sc = (struct riscv_tmr_softc *)et->et_priv;
132
133         if (first != 0) {
134                 counts = ((uint32_t)et->et_frequency * first) >> 32;
135                 counts += READ8(sc, TIMER_COUNTS);
136                 cpu = PCPU_GET(cpuid);
137                 WRITE8(sc, TIMER_MTIMECMP(cpu), counts);
138                 csr_set(sie, SIE_STIE);
139                 sbi_set_timer(counts);
140
141                 return (0);
142         }
143
144         return (EINVAL);
145
146 }
147
148 static int
149 riscv_tmr_stop(struct eventtimer *et)
150 {
151         struct riscv_tmr_softc *sc;
152
153         sc = (struct riscv_tmr_softc *)et->et_priv;
154
155         /* TODO */
156
157         return (0);
158 }
159
160 static int
161 riscv_tmr_intr(void *arg)
162 {
163         struct riscv_tmr_softc *sc;
164
165         sc = (struct riscv_tmr_softc *)arg;
166
167         csr_clear(sip, SIP_STIP);
168
169         if (sc->et.et_active)
170                 sc->et.et_event_cb(&sc->et, sc->et.et_arg);
171
172         return (FILTER_HANDLED);
173 }
174
175 static int
176 riscv_tmr_fdt_probe(device_t dev)
177 {
178
179         if (!ofw_bus_status_okay(dev))
180                 return (ENXIO);
181
182         if (ofw_bus_is_compatible(dev, "riscv,timer")) {
183                 device_set_desc(dev, "RISC-V Timer");
184                 return (BUS_PROBE_DEFAULT);
185         }
186
187         return (ENXIO);
188 }
189
190 static int
191 riscv_tmr_attach(device_t dev)
192 {
193         struct riscv_tmr_softc *sc;
194         phandle_t node;
195         pcell_t clock;
196         int error;
197
198         sc = device_get_softc(dev);
199         if (riscv_tmr_sc)
200                 return (ENXIO);
201
202         /* Get the base clock frequency */
203         node = ofw_bus_get_node(dev);
204         if (node > 0) {
205                 error = OF_getprop(node, "clock-frequency", &clock,
206                     sizeof(clock));
207                 if (error > 0) {
208                         sc->clkfreq = fdt32_to_cpu(clock);
209                 }
210         }
211
212         if (sc->clkfreq == 0)
213                 sc->clkfreq = DEFAULT_FREQ;
214
215         if (sc->clkfreq == 0) {
216                 device_printf(dev, "No clock frequency specified\n");
217                 return (ENXIO);
218         }
219
220         if (bus_alloc_resources(dev, timer_spec, sc->res)) {
221                 device_printf(dev, "could not allocate resources\n");
222                 return (ENXIO);
223         }
224
225         /* Memory interface */
226         sc->bst = rman_get_bustag(sc->res[0]);
227         sc->bsh = rman_get_bushandle(sc->res[0]);
228
229         riscv_tmr_sc = sc;
230
231         /* Setup IRQs handler */
232         error = bus_setup_intr(dev, sc->res[1], INTR_TYPE_CLK,
233             riscv_tmr_intr, NULL, sc, &sc->ih);
234         if (error) {
235                 device_printf(dev, "Unable to alloc int resource.\n");
236                 return (ENXIO);
237         }
238
239         riscv_tmr_timecount.tc_frequency = sc->clkfreq;
240         riscv_tmr_timecount.tc_priv = sc;
241         tc_init(&riscv_tmr_timecount);
242
243         sc->et.et_name = "RISC-V Eventtimer";
244         sc->et.et_flags = ET_FLAGS_ONESHOT | ET_FLAGS_PERCPU;
245         sc->et.et_quality = 1000;
246
247         sc->et.et_frequency = sc->clkfreq;
248         sc->et.et_min_period = (0x00000002LLU << 32) / sc->et.et_frequency;
249         sc->et.et_max_period = (0xfffffffeLLU << 32) / sc->et.et_frequency;
250         sc->et.et_start = riscv_tmr_start;
251         sc->et.et_stop = riscv_tmr_stop;
252         sc->et.et_priv = sc;
253         et_register(&sc->et);
254
255         return (0);
256 }
257
258 static device_method_t riscv_tmr_fdt_methods[] = {
259         DEVMETHOD(device_probe,         riscv_tmr_fdt_probe),
260         DEVMETHOD(device_attach,        riscv_tmr_attach),
261         { 0, 0 }
262 };
263
264 static driver_t riscv_tmr_fdt_driver = {
265         "timer",
266         riscv_tmr_fdt_methods,
267         sizeof(struct riscv_tmr_softc),
268 };
269
270 static devclass_t riscv_tmr_fdt_devclass;
271
272 EARLY_DRIVER_MODULE(timer, simplebus, riscv_tmr_fdt_driver, riscv_tmr_fdt_devclass,
273     0, 0, BUS_PASS_TIMER + BUS_PASS_ORDER_MIDDLE);
274 EARLY_DRIVER_MODULE(timer, ofwbus, riscv_tmr_fdt_driver, riscv_tmr_fdt_devclass,
275     0, 0, BUS_PASS_TIMER + BUS_PASS_ORDER_MIDDLE);
276
277 void
278 DELAY(int usec)
279 {
280         int64_t counts, counts_per_usec;
281         uint64_t first, last;
282
283         /*
284          * Check the timers are setup, if not just
285          * use a for loop for the meantime
286          */
287         if (riscv_tmr_sc == NULL) {
288                 for (; usec > 0; usec--)
289                         for (counts = 200; counts > 0; counts--)
290                                 /*
291                                  * Prevent the compiler from optimizing
292                                  * out the loop
293                                  */
294                                 cpufunc_nullop();
295                 return;
296         }
297
298         /* Get the number of times to count */
299         counts_per_usec = ((riscv_tmr_timecount.tc_frequency / 1000000) + 1);
300
301         /*
302          * Clamp the timeout at a maximum value (about 32 seconds with
303          * a 66MHz clock). *Nobody* should be delay()ing for anywhere
304          * near that length of time and if they are, they should be hung
305          * out to dry.
306          */
307         if (usec >= (0x80000000U / counts_per_usec))
308                 counts = (0x80000000U / counts_per_usec) - 1;
309         else
310                 counts = usec * counts_per_usec;
311
312         first = get_counts(riscv_tmr_sc);
313
314         while (counts > 0) {
315                 last = get_counts(riscv_tmr_sc);
316                 counts -= (int64_t)(last - first);
317                 first = last;
318         }
319 }