2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2006-2007 Bruce M. Simpson.
5 * Copyright (c) 2003-2004 Juli Mallett.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * Simple driver for the 32-bit interval counter built in to all
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/sysctl.h>
42 #include <sys/kernel.h>
43 #include <sys/module.h>
45 #include <sys/power.h>
48 #include <sys/timeet.h>
49 #include <sys/timetc.h>
51 #include <machine/hwfunc.h>
52 #include <machine/clock.h>
53 #include <machine/locore.h>
54 #include <machine/md_var.h>
57 #include <machine/intr.h>
60 uint64_t counter_freq;
62 struct timecounter *platform_timecounter;
64 DPCPU_DEFINE_STATIC(uint32_t, cycles_per_tick);
65 static uint32_t cycles_per_usec;
67 DPCPU_DEFINE_STATIC(volatile uint32_t, counter_upper);
68 DPCPU_DEFINE_STATIC(volatile uint32_t, counter_lower_last);
69 DPCPU_DEFINE_STATIC(uint32_t, compare_ticks);
70 DPCPU_DEFINE_STATIC(uint32_t, lost_ticks);
74 struct resource *intr_res;
76 struct timecounter tc;
79 static struct clock_softc *softc;
84 static int clock_probe(device_t);
85 static void clock_identify(driver_t *, device_t);
86 static int clock_attach(device_t);
87 static unsigned counter_get_timecount(struct timecounter *tc);
90 mips_timer_early_init(uint64_t clock_hz)
92 /* Initialize clock early so that we can use DELAY sooner */
93 counter_freq = clock_hz;
94 cycles_per_usec = (clock_hz / (1000 * 1000));
98 platform_initclocks(void)
101 if (platform_timecounter != NULL)
102 tc_init(platform_timecounter);
110 uint32_t t_lower_last, t_upper;
113 * Disable preemption because we are working with cpu specific data.
118 * Note that even though preemption is disabled, interrupts are
119 * still enabled. In particular there is a race with clock_intr()
120 * reading the values of 'counter_upper' and 'counter_lower_last'.
122 * XXX this depends on clock_intr() being executed periodically
123 * so that 'counter_upper' and 'counter_lower_last' are not stale.
126 t_upper = DPCPU_GET(counter_upper);
127 t_lower_last = DPCPU_GET(counter_lower_last);
128 } while (t_upper != DPCPU_GET(counter_upper));
130 ticktock = mips_rd_count();
134 /* COUNT register wrapped around */
135 if (ticktock < t_lower_last)
138 ret = ((uint64_t)t_upper << 32) | ticktock;
143 mips_timer_init_params(uint64_t platform_counter_freq, int double_count)
147 * XXX: Do not use printf here: uart code 8250 may use DELAY so this
148 * function should be called before cninit.
150 counter_freq = platform_counter_freq;
152 * XXX: Some MIPS32 cores update the Count register only every two
154 * We know this because of status registers in CP0, make it automatic.
156 if (double_count != 0)
159 cycles_per_usec = counter_freq / (1 * 1000 * 1000);
160 set_cputicker(tick_ticker, counter_freq, 1);
164 sysctl_machdep_counter_freq(SYSCTL_HANDLER_ARGS)
172 error = sysctl_handle_64(oidp, &freq, sizeof(freq), req);
173 if (error == 0 && req->newptr != NULL) {
175 softc->et.et_frequency = counter_freq;
176 softc->tc.tc_frequency = counter_freq;
181 SYSCTL_PROC(_machdep, OID_AUTO, counter_freq,
182 CTLTYPE_U64 | CTLFLAG_RW | CTLFLAG_NEEDGIANT, NULL, 0,
183 sysctl_machdep_counter_freq, "QU",
184 "Timecounter frequency in Hz");
187 counter_get_timecount(struct timecounter *tc)
190 return (mips_rd_count());
194 * Wait for about n microseconds (at least!).
199 uint32_t cur, last, delta, usecs;
203 * This works by polling the timer and counting the number of
204 * microseconds that go by.
206 last = mips_rd_count();
210 cur = mips_rd_count();
212 /* Check to see if the timer has wrapped around. */
214 delta += cur + (0xffffffff - last) + 1;
220 if (delta >= cycles_per_usec) {
221 usecs += delta / cycles_per_usec;
222 delta %= cycles_per_usec;
229 clock_start(struct eventtimer *et, sbintime_t first, sbintime_t period)
231 uint32_t fdiv, div, next;
234 div = (et->et_frequency * period) >> 32;
238 fdiv = (et->et_frequency * first) >> 32;
241 DPCPU_SET(cycles_per_tick, div);
242 next = mips_rd_count() + fdiv;
243 DPCPU_SET(compare_ticks, next);
244 mips_wr_compare(next);
249 clock_stop(struct eventtimer *et)
252 DPCPU_SET(cycles_per_tick, 0);
253 mips_wr_compare(0xffffffff);
258 * Device section of file below
261 clock_intr(void *arg)
263 struct clock_softc *sc = (struct clock_softc *)arg;
264 uint32_t cycles_per_tick;
265 uint32_t count, compare_last, compare_next, lost_ticks;
267 cycles_per_tick = DPCPU_GET(cycles_per_tick);
269 * Set next clock edge.
271 count = mips_rd_count();
272 compare_last = DPCPU_GET(compare_ticks);
273 if (cycles_per_tick > 0) {
274 compare_next = count + cycles_per_tick;
275 DPCPU_SET(compare_ticks, compare_next);
276 mips_wr_compare(compare_next);
277 } else /* In one-shot mode timer should be stopped after the event. */
278 mips_wr_compare(0xffffffff);
280 /* COUNT register wrapped around */
281 if (count < DPCPU_GET(counter_lower_last)) {
282 DPCPU_SET(counter_upper, DPCPU_GET(counter_upper) + 1);
284 DPCPU_SET(counter_lower_last, count);
286 if (cycles_per_tick > 0) {
289 * Account for the "lost time" between when the timer interrupt
290 * fired and when 'clock_intr' actually started executing.
292 lost_ticks = DPCPU_GET(lost_ticks);
293 lost_ticks += count - compare_last;
296 * If the COUNT and COMPARE registers are no longer in sync
297 * then make up some reasonable value for the 'lost_ticks'.
299 * This could happen, for e.g., after we resume normal
300 * operations after exiting the debugger.
302 if (lost_ticks > 2 * cycles_per_tick)
303 lost_ticks = cycles_per_tick;
305 while (lost_ticks >= cycles_per_tick) {
306 if (sc->et.et_active)
307 sc->et.et_event_cb(&sc->et, sc->et.et_arg);
308 lost_ticks -= cycles_per_tick;
310 DPCPU_SET(lost_ticks, lost_ticks);
312 if (sc->et.et_active)
313 sc->et.et_event_cb(&sc->et, sc->et.et_arg);
314 return (FILTER_HANDLED);
318 clock_probe(device_t dev)
321 device_set_desc(dev, "Generic MIPS32 ticker");
322 return (BUS_PROBE_NOWILDCARD);
326 clock_identify(driver_t * drv, device_t parent)
329 BUS_ADD_CHILD(parent, 0, "clock", 0);
333 clock_attach(device_t dev)
335 struct clock_softc *sc;
340 if (device_get_unit(dev) != 0)
341 panic("can't attach more clocks");
343 softc = sc = device_get_softc(dev);
345 cpu_establish_hardintr("clock", clock_intr, NULL, sc, 5, INTR_TYPE_CLK,
349 sc->intr_res = bus_alloc_resource(dev,
350 SYS_RES_IRQ, &sc->intr_rid, 5, 5, 1, RF_ACTIVE);
351 if (sc->intr_res == NULL) {
352 device_printf(dev, "failed to allocate irq\n");
355 error = bus_setup_intr(dev, sc->intr_res, INTR_TYPE_CLK,
356 clock_intr, NULL, sc, &sc->intr_handler);
358 device_printf(dev, "bus_setup_intr returned %d\n", error);
363 sc->tc.tc_get_timecount = counter_get_timecount;
364 sc->tc.tc_counter_mask = 0xffffffff;
365 sc->tc.tc_frequency = counter_freq;
366 sc->tc.tc_name = "MIPS32";
367 sc->tc.tc_quality = 800;
370 sc->et.et_name = "MIPS32";
371 sc->et.et_flags = ET_FLAGS_PERIODIC | ET_FLAGS_ONESHOT |
373 sc->et.et_quality = 800;
374 sc->et.et_frequency = counter_freq;
375 sc->et.et_min_period = 0x00004000LLU; /* To be safe. */
376 sc->et.et_max_period = (0xfffffffeLLU << 32) / sc->et.et_frequency;
377 sc->et.et_start = clock_start;
378 sc->et.et_stop = clock_stop;
380 et_register(&sc->et);
384 static device_method_t clock_methods[] = {
385 /* Device interface */
386 DEVMETHOD(device_probe, clock_probe),
387 DEVMETHOD(device_identify, clock_identify),
388 DEVMETHOD(device_attach, clock_attach),
389 DEVMETHOD(device_detach, bus_generic_detach),
390 DEVMETHOD(device_shutdown, bus_generic_shutdown),
395 static driver_t clock_driver = {
398 sizeof(struct clock_softc),
401 static devclass_t clock_devclass;
403 DRIVER_MODULE(clock, nexus, clock_driver, clock_devclass, 0, 0);