clock.c revision 110388
1/*
2 * Copyright (C) 1995, 1996 Wolfgang Solfrank.
3 * Copyright (C) 1995, 1996 TooLs GmbH.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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.
19 *
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.
30 *
31 *	$NetBSD: clock.c,v 1.9 2000/01/19 02:52:19 msaitoh Exp $
32 */
33/*
34 * Copyright (C) 2001 Benno Rice.
35 * All rights reserved.
36 *
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
39 * are met:
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.
45 *
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.
56 */
57
58#ifndef lint
59static const char rcsid[] =
60  "$FreeBSD: head/sys/powerpc/aim/clock.c 110388 2003-02-05 12:33:49Z benno $";
61#endif /* not lint */
62
63#include <sys/param.h>
64#include <sys/systm.h>
65#include <sys/kernel.h>
66#include <sys/sysctl.h>
67#include <sys/bus.h>
68#include <sys/timetc.h>
69#include <sys/interrupt.h>
70
71#include <dev/ofw/openfirm.h>
72
73#include <machine/clock.h>
74#include <machine/cpu.h>
75#include <machine/intr.h>
76
77#if 0 /* XXX */
78#include "adb.h"
79#else
80#define	NADB	0
81#endif
82
83/*
84 * Initially we assume a processor with a bus frequency of 12.5 MHz.
85 */
86u_int			tickspending;
87u_long			ns_per_tick = 80;
88static u_long		ticks_per_sec = 12500000;
89static long		ticks_per_intr;
90static volatile u_long	lasttb;
91
92#define	SECDAY		86400
93#define	DIFF19041970	2082844800
94
95#if NADB > 0
96extern int adb_read_date_time(int *);
97extern int adb_set_date_time(int);
98#endif
99
100static int		clockinitted = 0;
101
102static timecounter_get_t	decr_get_timecount;
103
104static struct timecounter	decr_timecounter = {
105	decr_get_timecount,	/* get_timecount */
106	0,			/* no poll_pps */
107	~0u,			/* counter_mask */
108	0,			/* frequency */
109	"decrementer"		/* name */
110};
111
112void
113inittodr(time_t base)
114{
115	time_t		deltat;
116	u_int		rtc_time;
117	struct timespec	ts;
118	phandle_t	phandle;
119	ihandle_t	ihandle;
120	char		rtcpath[128];
121	u_int		rtcsecs;
122
123	/*
124	 * If we can't read from RTC, use the fs time.
125	 */
126	phandle = OF_finddevice("rtc");
127	if (phandle != -1) {
128		OF_package_to_path(phandle, rtcpath, sizeof(rtcpath));
129		ihandle = OF_open(rtcpath);
130		if (ihandle != -1) {
131			if (OF_call_method("read-rtc", ihandle,
132			    0, 1, &rtcsecs))
133				printf("RTC call method error\n");
134			else {
135				ts.tv_sec = rtcsecs - DIFF19041970;
136				ts.tv_nsec = 0;
137				tc_setclock(&ts);
138				return;
139			}
140		}
141	}
142
143#if NADB > 0
144	if (adb_read_date_time(&rtc_time) < 0)
145#endif
146	{
147		ts.tv_sec = base;
148		ts.tv_nsec = 0;
149		tc_setclock(&ts);
150		return;
151	}
152	clockinitted = 1;
153	ts.tv_sec = rtc_time - DIFF19041970;
154
155	deltat = ts.tv_sec - base;
156	if (deltat < 0) {
157		deltat = -deltat;
158	}
159	if (deltat < 2 * SECDAY) {
160		tc_setclock(&ts);
161		return;
162	}
163
164	printf("WARNING: clock %s %d days",
165	    ts.tv_sec < base ? "lost" : "gained", (int)(deltat / SECDAY));
166
167	printf(" -- CHECK AND RESET THE DATE!\n");
168}
169
170/*
171 * Similar to the above
172 */
173void
174resettodr()
175{
176#if NADB > 0
177	u_int	rtc_time;
178
179	if (clockinitted) {
180		rtc_time = time.tv_sec + DIFF19041970;
181		adb_set_date_time(rtc_time);
182	}
183#endif
184}
185
186void
187decr_intr(struct clockframe *frame)
188{
189	u_long		tb;
190	long		tick;
191	int		nticks;
192	register_t	msr;
193
194	/*
195	 * Check whether we are initialized.
196	 */
197	if (!ticks_per_intr)
198		return;
199
200	/*
201	 * Based on the actual time delay since the last decrementer reload,
202	 * we arrange for earlier interrupt next time.
203	 */
204	__asm ("mftb %0; mfdec %1" : "=r"(tb), "=r"(tick));
205	for (nticks = 0; tick < 0; nticks++)
206		tick += ticks_per_intr;
207	mtdec(tick);
208	/*
209	 * lasttb is used during microtime. Set it to the virtual
210	 * start of this tick interval.
211	 */
212	lasttb = tb + tick - ticks_per_intr;
213
214	nticks += tickspending;
215	tickspending = 0;
216
217	/*
218	 * Reenable interrupts
219	 */
220#if 0
221	msr = mfmsr();
222	mtmsr(msr | PSL_EE | PSL_RI);
223#endif
224	/*
225	 * Do standard timer interrupt stuff.
226	 * Do softclock stuff only on the last iteration.
227	 */
228#if 0
229	while (--nticks > 0) {
230		hardclock(frame);
231	}
232#endif
233	hardclock(frame);
234}
235
236void
237cpu_initclocks(void)
238{
239
240	return;
241}
242
243void
244decr_init(void)
245{
246	int qhandle, phandle;
247	char name[32];
248	unsigned int msr;
249
250	phandle = 0;
251
252	/*
253	 * Get this info during autoconf?				XXX
254	 */
255	for (qhandle = OF_peer(0); qhandle; qhandle = phandle) {
256		if (OF_getprop(qhandle, "device_type", name, sizeof name) >= 0
257		    && !strcmp(name, "cpu")
258		    && OF_getprop(qhandle, "timebase-frequency",
259				  &ticks_per_sec, sizeof ticks_per_sec) >= 0) {
260			/*
261			 * Should check for correct CPU here?		XXX
262			 */
263			msr = mfmsr();
264			mtmsr(msr & ~(PSL_EE|PSL_RI));
265
266			decr_timecounter.tc_frequency = ticks_per_sec;
267			tc_init(&decr_timecounter);
268
269			ns_per_tick = 1000000000 / ticks_per_sec;
270			ticks_per_intr = ticks_per_sec / hz;
271			__asm __volatile ("mftb %0" : "=r"(lasttb));
272			mtdec(ticks_per_intr);
273
274			mtmsr(msr);
275
276			break;
277		}
278		if ((phandle = OF_child(qhandle)))
279			continue;
280		while (qhandle) {
281			if ((phandle = OF_peer(qhandle)))
282				break;
283			qhandle = OF_parent(qhandle);
284		}
285	}
286	if (!phandle)
287		panic("no cpu node");
288}
289
290static __inline u_quad_t
291mftb(void)
292{
293	u_long		scratch;
294	u_quad_t	tb;
295
296	__asm ("1: mftbu %0; mftb %0+1; mftbu %1; cmpw 0,%0,%1; bne 1b"
297	      : "=r"(tb), "=r"(scratch));
298	return tb;
299}
300
301static unsigned
302decr_get_timecount(struct timecounter *tc)
303{
304	return mftb();
305}
306
307/*
308 * Wait for about n microseconds (at least!).
309 */
310void
311delay(int n)
312{
313	u_quad_t	tb, ttb;
314
315	tb = mftb();
316	ttb = tb + (n * 1000 + ns_per_tick - 1) / ns_per_tick;
317	while (tb < ttb)
318		tb = mftb();
319}
320
321/*
322 * Nothing to do.
323 */
324void
325cpu_startprofclock(void)
326{
327
328	/* Do nothing */
329}
330
331void
332cpu_stopprofclock(void)
333{
334}
335