2 * Copyright (c) 2006-2007 Bruce M. Simpson.
3 * Copyright (c) 2003-2004 Juli Mallett.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * Simple driver for the 32-bit interval counter built in to all
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include "opt_cputype.h"
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/timetc.h>
50 #include <machine/hwfunc.h>
51 #include <machine/clock.h>
52 #include <machine/locore.h>
53 #include <machine/md_var.h>
55 uint64_t counter_freq;
57 struct timecounter *platform_timecounter;
59 static uint64_t cycles_per_tick;
60 static uint64_t cycles_per_usec;
61 static uint64_t cycles_per_hz, cycles_per_stathz, cycles_per_profhz;
63 static u_int32_t counter_upper = 0;
64 static u_int32_t counter_lower_last = 0;
70 uint32_t compare_ticks;
71 } __aligned(CACHE_LINE_SIZE);
73 static struct clk_ticks pcpu_ticks[MAXCPU];
78 static int clock_probe(device_t);
79 static void clock_identify(driver_t *, device_t);
80 static int clock_attach(device_t);
81 static unsigned counter_get_timecount(struct timecounter *tc);
83 static struct timecounter counter_timecounter = {
84 counter_get_timecount, /* get_timecount */
86 0xffffffffu, /* counter_mask */
89 800, /* quality (adjusted in code) */
93 mips_timer_early_init(uint64_t clock_hz)
95 /* Initialize clock early so that we can use DELAY sooner */
96 counter_freq = clock_hz;
97 cycles_per_usec = (clock_hz / (1000 * 1000));
101 platform_initclocks(void)
104 tc_init(&counter_timecounter);
106 if (platform_timecounter != NULL)
107 tc_init(platform_timecounter);
117 * XXX: MIPS64 platforms can read 64-bits of counter directly.
118 * Also: the tc code is supposed to cope with things wrapping
119 * from the time counter, so I'm not sure why all these hoops
120 * are even necessary.
122 ticktock = mips_rd_count();
124 if (ticktock < counter_lower_last)
126 counter_lower_last = ticktock;
129 ret = ((uint64_t) counter_upper << 32) | counter_lower_last;
134 mips_timer_init_params(uint64_t platform_counter_freq, int double_count)
141 * XXX: Do not use printf here: uart code 8250 may use DELAY so this
142 * function should be called before cninit.
144 counter_freq = platform_counter_freq;
146 * XXX: Some MIPS32 cores update the Count register only every two
148 * We know this because of status registers in CP0, make it automatic.
150 if (double_count != 0)
153 cycles_per_tick = counter_freq / 1000;
154 cycles_per_hz = counter_freq / hz;
155 cycles_per_stathz = counter_freq / stathz;
156 cycles_per_profhz = counter_freq / profhz;
157 cycles_per_usec = counter_freq / (1 * 1000 * 1000);
159 counter_timecounter.tc_frequency = counter_freq;
160 printf("hz=%d cyl_per_tick:%jd cyl_per_usec:%jd freq:%jd "
161 "cyl_per_hz:%jd cyl_per_stathz:%jd cyl_per_profhz:%jd\n",
169 set_cputicker(tick_ticker, counter_freq, 1);
173 sysctl_machdep_counter_freq(SYSCTL_HANDLER_ARGS)
178 if (counter_timecounter.tc_frequency == 0)
181 error = sysctl_handle_int(oidp, &freq, sizeof(freq), req);
182 if (error == 0 && req->newptr != NULL) {
184 counter_timecounter.tc_frequency = counter_freq;
189 SYSCTL_PROC(_machdep, OID_AUTO, counter_freq, CTLTYPE_QUAD | CTLFLAG_RW,
190 0, sizeof(u_int), sysctl_machdep_counter_freq, "IU",
191 "Timecounter frequency in Hz");
194 counter_get_timecount(struct timecounter *tc)
197 return (mips_rd_count());
202 cpu_startprofclock(void)
208 cpu_stopprofclock(void)
214 * Wait for about n microseconds (at least!).
219 uint32_t cur, last, delta, usecs;
222 * This works by polling the timer and counting the number of
223 * microseconds that go by.
225 last = mips_rd_count();
229 cur = mips_rd_count();
231 /* Check to see if the timer has wrapped around. */
233 delta += cur + (0xffffffff - last) + 1;
239 if (delta >= cycles_per_usec) {
240 usecs += delta / cycles_per_usec;
241 delta %= cycles_per_usec;
246 #if 0 /* TARGET_OCTEON */
247 int64_t wheel_run = 0;
249 void octeon_led_run_wheel();
253 * Device section of file below
256 clock_intr(void *arg)
258 struct clk_ticks *cpu_ticks;
259 struct trapframe *tf;
260 uint32_t count, compare, delta;
262 cpu_ticks = &pcpu_ticks[PCPU_GET(cpuid)];
265 * Set next clock edge.
267 count = mips_rd_count();
268 compare = cpu_ticks->compare_ticks;
269 cpu_ticks->compare_ticks = count + cycles_per_tick;
270 mips_wr_compare(cpu_ticks->compare_ticks);
272 if (count < counter_lower_last) {
274 counter_lower_last = count;
277 * Magic. Setting up with an arg of NULL means we get passed tf.
279 tf = (struct trapframe *)arg;
281 delta = cycles_per_tick;
284 * Account for the "lost time" between when the timer interrupt fired
285 * and when 'clock_intr' actually started executing.
287 delta += count - compare;
290 * If the COUNT and COMPARE registers are no longer in sync then make
291 * up some reasonable value for the 'delta'.
293 * This could happen, for e.g., after we resume normal operations after
294 * exiting the debugger.
296 if (delta > cycles_per_hz)
297 delta = cycles_per_hz;
300 * If the DTrace hooks are configured and a callback function
301 * has been registered, then call it to process the high speed
304 int cpu = PCPU_GET(cpuid);
305 if (cyclic_clock_func[cpu] != NULL)
306 (*cyclic_clock_func[cpu])(tf);
308 /* Fire hardclock at hz. */
309 cpu_ticks->hard_ticks += delta;
310 if (cpu_ticks->hard_ticks >= cycles_per_hz) {
311 cpu_ticks->hard_ticks -= cycles_per_hz;
312 if (PCPU_GET(cpuid) == 0)
313 hardclock(TRAPF_USERMODE(tf), tf->pc);
315 hardclock_cpu(TRAPF_USERMODE(tf));
318 /* Fire statclock at stathz. */
319 cpu_ticks->stat_ticks += delta;
320 if (cpu_ticks->stat_ticks >= cycles_per_stathz) {
321 cpu_ticks->stat_ticks -= cycles_per_stathz;
322 statclock(TRAPF_USERMODE(tf));
325 /* Fire profclock at profhz, but only when needed. */
326 cpu_ticks->prof_ticks += delta;
327 if (cpu_ticks->prof_ticks >= cycles_per_profhz) {
328 cpu_ticks->prof_ticks -= cycles_per_profhz;
330 profclock(TRAPF_USERMODE(tf), tf->pc);
333 #if 0 /* TARGET_OCTEON */
334 /* Run the FreeBSD display once every hz ticks */
335 wheel_run += cycles_per_tick;
336 if (wheel_run >= cycles_per_usec * 1000000ULL) {
338 octeon_led_run_wheel();
341 return (FILTER_HANDLED);
345 clock_probe(device_t dev)
348 if (device_get_unit(dev) != 0)
349 panic("can't attach more clocks");
351 device_set_desc(dev, "Generic MIPS32 ticker");
356 clock_identify(driver_t * drv, device_t parent)
359 BUS_ADD_CHILD(parent, 0, "clock", 0);
363 clock_attach(device_t dev)
365 struct resource *irq;
370 irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 5, 5, 1, RF_ACTIVE);
372 device_printf(dev, "failed to allocate irq\n");
375 error = bus_setup_intr(dev, irq, INTR_TYPE_CLK, clock_intr, NULL,
379 device_printf(dev, "bus_setup_intr returned %d\n", error);
383 mips_wr_compare(mips_rd_count() + counter_freq / hz);
387 static device_method_t clock_methods[] = {
388 /* Device interface */
389 DEVMETHOD(device_probe, clock_probe),
390 DEVMETHOD(device_identify, clock_identify),
391 DEVMETHOD(device_attach, clock_attach),
392 DEVMETHOD(device_detach, bus_generic_detach),
393 DEVMETHOD(device_shutdown, bus_generic_shutdown),
398 static driver_t clock_driver = {
399 "clock", clock_methods, 32
402 static devclass_t clock_devclass;
404 DRIVER_MODULE(clock, nexus, clock_driver, clock_devclass, 0, 0);