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