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