2 * Copyright (C) 1995, 1996 Wolfgang Solfrank.
3 * Copyright (C) 1995, 1996 TooLs GmbH.
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.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by TooLs GmbH.
17 * 4. The name of TooLs GmbH may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 * $NetBSD: clock.c,v 1.9 2000/01/19 02:52:19 msaitoh Exp $
34 * Copyright (C) 2001 Benno Rice.
35 * All rights reserved.
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
46 * THIS SOFTWARE IS PROVIDED BY Benno Rice ``AS IS'' AND ANY EXPRESS OR
47 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
48 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
49 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
50 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
51 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
52 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
53 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
54 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
55 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
58 #include <sys/cdefs.h>
59 __FBSDID("$FreeBSD$");
61 #include <sys/param.h>
62 #include <sys/systm.h>
63 #include <sys/kernel.h>
65 #include <sys/interrupt.h>
67 #include <sys/sysctl.h>
68 #include <sys/timeet.h>
69 #include <sys/timetc.h>
71 #include <dev/ofw/openfirm.h>
73 #include <machine/clock.h>
74 #include <machine/cpu.h>
75 #include <machine/intr_machdep.h>
76 #include <machine/md_var.h>
77 #include <machine/smp.h>
80 * Initially we assume a processor with a bus frequency of 12.5 MHz.
82 static int initialized = 0;
83 static u_long ns_per_tick = 80;
84 static u_long ticks_per_sec = 12500000;
85 static u_long *decr_counts[MAXCPU];
87 static int decr_et_start(struct eventtimer *et,
88 sbintime_t first, sbintime_t period);
89 static int decr_et_stop(struct eventtimer *et);
90 static timecounter_get_t decr_get_timecount;
93 int mode; /* 0 - off, 1 - periodic, 2 - one-shot. */
94 int32_t div; /* Periodic divisor. */
96 static DPCPU_DEFINE(struct decr_state, decr_state);
98 static struct eventtimer decr_et;
99 static struct timecounter decr_tc = {
100 decr_get_timecount, /* get_timecount */
102 ~0u, /* counter_mask */
104 "timebase" /* name */
108 * Decrementer interrupt handler.
111 decr_intr(struct trapframe *frame)
113 struct decr_state *s = DPCPU_PTR(decr_state);
120 (*decr_counts[curcpu])++;
124 * Based on the actual time delay since the last decrementer
125 * reload, we arrange for earlier interrupt next time.
127 __asm ("mfdec %0" : "=r"(val));
133 } else if (s->mode == 2) {
138 while (nticks-- > 0) {
139 if (decr_et.et_active)
140 decr_et.et_event_cb(&decr_et, decr_et.et_arg);
145 * BSP early initialization.
154 * Check the BSP's timebase frequency. Sometimes we can't find the BSP, so fall
155 * back to the first CPU in this case.
157 if (platform_smp_get_bsp(&cpu) != 0)
158 platform_smp_first_cpu(&cpu);
159 ticks_per_sec = platform_timebase_freq(&cpu);
160 ns_per_tick = 1000000000 / ticks_per_sec;
162 set_cputicker(mftb, ticks_per_sec, 0);
163 snprintf(buf, sizeof(buf), "cpu%d:decrementer", curcpu);
164 intrcnt_add(buf, &decr_counts[curcpu]);
171 * AP early initialization.
178 snprintf(buf, sizeof(buf), "cpu%d:decrementer", curcpu);
179 intrcnt_add(buf, &decr_counts[curcpu]);
185 * Final initialization.
191 decr_tc.tc_frequency = ticks_per_sec;
193 decr_et.et_name = "decrementer";
194 decr_et.et_flags = ET_FLAGS_PERIODIC | ET_FLAGS_ONESHOT |
196 decr_et.et_quality = 1000;
197 decr_et.et_frequency = ticks_per_sec;
198 decr_et.et_min_period = (0x00000002LLU << 32) / ticks_per_sec;
199 decr_et.et_max_period = (0x7fffffffLLU << 32) / ticks_per_sec;
200 decr_et.et_start = decr_et_start;
201 decr_et.et_stop = decr_et_stop;
202 decr_et.et_priv = NULL;
203 et_register(&decr_et);
207 * Event timer start method.
210 decr_et_start(struct eventtimer *et,
211 sbintime_t first, sbintime_t period)
213 struct decr_state *s = DPCPU_PTR(decr_state);
218 s->div = (decr_et.et_frequency * period) >> 32;
224 fdiv = (decr_et.et_frequency * first) >> 32;
233 * Event timer stop method.
236 decr_et_stop(struct eventtimer *et)
238 struct decr_state *s = DPCPU_PTR(decr_state);
247 * Timecounter get method.
250 decr_get_timecount(struct timecounter *tc)
254 __asm __volatile("mftb %0" : "=r"(tb));
259 * Wait for about n microseconds (at least!).
267 ttb = tb + (n * 1000 + ns_per_tick - 1) / ns_per_tick;