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>
64 #include <sys/sysctl.h>
66 #include <sys/clock.h>
67 #include <sys/timetc.h>
68 #include <sys/interrupt.h>
70 #include <dev/ofw/openfirm.h>
72 #include <machine/clock.h>
73 #include <machine/cpu.h>
74 #include <machine/intr.h>
75 #include <machine/md_var.h>
78 * Initially we assume a processor with a bus frequency of 12.5 MHz.
81 u_long ns_per_tick = 80;
82 static u_long ticks_per_sec = 12500000;
83 static long ticks_per_intr;
84 static volatile u_long lasttb;
86 #define DIFF19041970 2082844800
88 static int clockinitted = 0;
90 static timecounter_get_t decr_get_timecount;
92 static struct timecounter decr_timecounter = {
93 decr_get_timecount, /* get_timecount */
95 ~0u, /* counter_mask */
97 "decrementer" /* name */
101 inittodr(time_t base)
112 * If we can't read from RTC, use the fs time.
114 phandle = OF_finddevice("rtc");
116 OF_package_to_path(phandle, rtcpath, sizeof(rtcpath));
117 ihandle = OF_open(rtcpath);
119 if (OF_call_method("read-rtc", ihandle,
121 printf("RTC call method error\n");
123 ts.tv_sec = rtcsecs - DIFF19041970;
138 ts.tv_sec = rtc_time - DIFF19041970;
140 deltat = ts.tv_sec - base;
144 if (deltat < 2 * SECDAY) {
149 printf("WARNING: clock %s %d days",
150 ts.tv_sec < base ? "lost" : "gained", (int)(deltat / SECDAY));
152 printf(" -- CHECK AND RESET THE DATE!\n");
156 * Similar to the above
165 decr_intr(struct trapframe *frame)
172 * Check whether we are initialized.
178 * Based on the actual time delay since the last decrementer reload,
179 * we arrange for earlier interrupt next time.
181 __asm ("mftb %0; mfdec %1" : "=r"(tb), "=r"(tick));
182 for (nticks = 0; tick < 0; nticks++)
183 tick += ticks_per_intr;
186 * lasttb is used during microtime. Set it to the virtual
187 * start of this tick interval.
189 lasttb = tb + tick - ticks_per_intr;
191 nticks += tickspending;
195 * Reenable interrupts
199 mtmsr(msr | PSL_EE | PSL_RI);
202 * Do standard timer interrupt stuff.
203 * Do softclock stuff only on the last iteration.
206 while (--nticks > 0) {
207 hardclock(TRAPF_USERMODE(frame), TRAPF_PC(frame));
210 hardclock(TRAPF_USERMODE(frame), TRAPF_PC(frame));
216 int qhandle, phandle;
223 * Get this info during autoconf? XXX
225 for (qhandle = OF_peer(0); qhandle; qhandle = phandle) {
226 if (OF_getprop(qhandle, "device_type", name, sizeof name) >= 0
227 && !strcmp(name, "cpu")
228 && OF_getprop(qhandle, "timebase-frequency",
229 &ticks_per_sec, sizeof ticks_per_sec) >= 0) {
231 * Should check for correct CPU here? XXX
234 mtmsr(msr & ~(PSL_EE|PSL_RI));
236 ns_per_tick = 1000000000 / ticks_per_sec;
237 ticks_per_intr = ticks_per_sec / hz;
238 __asm __volatile ("mftb %0" : "=r"(lasttb));
239 mtdec(ticks_per_intr);
245 if ((phandle = OF_child(qhandle)))
248 if ((phandle = OF_peer(qhandle)))
250 qhandle = OF_parent(qhandle);
254 panic("no cpu node");
260 decr_timecounter.tc_frequency = ticks_per_sec;
261 tc_init(&decr_timecounter);
264 static __inline u_quad_t
270 __asm ("1: mftbu %0; mftb %0+1; mftbu %1; cmpw 0,%0,%1; bne 1b"
271 : "=r"(tb), "=r"(scratch));
276 decr_get_timecount(struct timecounter *tc)
282 * Wait for about n microseconds (at least!).
290 ttb = tb + (n * 1000 + ns_per_tick - 1) / ns_per_tick;
299 cpu_startprofclock(void)
306 cpu_stopprofclock(void)
311 * XXX Needed by syscons
314 sysbeep(int pitch, int period)