clock.c revision 1.6 1 /* $NetBSD: clock.c,v 1.6 1995/12/01 19:51:53 leo Exp $ */
2
3 /*
4 * Copyright (c) 1988 University of Utah.
5 * Copyright (c) 1982, 1990 The Regents of the University of California.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to Berkeley by
9 * the Systems Programming Group of the University of Utah Computer
10 * Science Department.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the University of
23 * California, Berkeley and its contributors.
24 * 4. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * SUCH DAMAGE.
39 *
40 * from: Utah $Hdr: clock.c 1.18 91/01/21$
41 *
42 * @(#)clock.c 7.6 (Berkeley) 5/7/91
43 */
44
45 #include <sys/param.h>
46 #include <sys/kernel.h>
47 #include <sys/device.h>
48 #include <machine/psl.h>
49 #include <machine/cpu.h>
50 #include <machine/iomap.h>
51 #include <machine/mfp.h>
52 #include <atari/dev/clockreg.h>
53
54 #if defined(GPROF) && defined(PROFTIMER)
55 #include <machine/profile.h>
56 #endif
57
58 /*
59 * The MFP clock runs at 2457600Hz. We use a {system,stat,prof}clock divider
60 * of 200. Therefore the timer runs at an effective rate of:
61 * 2457600/200 = 12288Hz.
62 */
63 #define CLOCK_HZ 12288
64
65 /*
66 * Machine-dependent clock routines.
67 *
68 * Inittodr initializes the time of day hardware which provides
69 * date functions.
70 *
71 * Resettodr restores the time of day hardware after a time change.
72 */
73
74 int clockmatch __P((struct device *, struct cfdata *, void *));
75 void clockattach __P((struct device *, struct device *, void *));
76
77 struct cfdriver clockcd = {
78 NULL, "clock", (cfmatch_t)clockmatch, clockattach,
79 DV_DULL, sizeof(struct device), NULL, 0
80 };
81
82 static u_long gettod __P((void));
83 static int settod __P((u_long));
84
85 static int divisor; /* Systemclock divisor */
86
87 /*
88 * Statistics and profile clock intervals and variances. Variance must
89 * be a power of 2. Since this gives us an even number, not an odd number,
90 * we discard one case and compensate. That is, a variance of 64 would
91 * give us offsets in [0..63]. Instead, we take offsets in [1..63].
92 * This is symetric around the point 32, or statvar/2, and thus averages
93 * to that value (assuming uniform random numbers).
94 */
95 #ifdef STATCLOCK
96 static int statvar = 32; /* {stat,prof}clock variance */
97 static int statmin; /* statclock divisor - variance/2 */
98 static int profmin; /* profclock divisor - variance/2 */
99 static int clk2min; /* current, from above choises */
100 #endif
101
102 int
103 clockmatch(pdp, cfp, auxp)
104 struct device *pdp;
105 struct cfdata *cfp;
106 void *auxp;
107 {
108 if(!strcmp("clock", auxp))
109 return(1);
110 return(0);
111 }
112
113 /*
114 * Start the real-time clock.
115 */
116 void clockattach(pdp, dp, auxp)
117 struct device *pdp, *dp;
118 void *auxp;
119 {
120 /*
121 * Initialize Timer-A in the ST-MFP. We use a divisor of 200.
122 * The MFP clock runs at 2457600Hz. Therefore the timer runs
123 * at an effective rate of: 2457600/200 = 12288Hz. The
124 * following expression works for 48, 64 or 96 hz.
125 */
126 divisor = CLOCK_HZ/hz;
127 MFP->mf_tacr = 0; /* Stop timer */
128 MFP->mf_iera &= ~IA_TIMA; /* Disable timer interrupts */
129 MFP->mf_tadr = divisor; /* Set divisor */
130
131 if (hz != 48 && hz != 64 && hz != 96) { /* XXX */
132 printf (": illegal value %d for systemclock, reset to %d\n\t",
133 hz, 64);
134 hz = 64;
135 }
136 printf(": system hz %d timer-A divisor 200/%d\n", hz, divisor);
137
138 #ifdef STATCLOCK
139 if ((stathz == 0) || (stathz > hz) || (CLOCK_HZ % stathz))
140 stathz = hz;
141 if ((profhz == 0) || (profhz > (hz << 1)) || (CLOCK_HZ % profhz))
142 profhz = hz << 1;
143
144 MFP->mf_tcdcr &= 0x7; /* Stop timer */
145 MFP->mf_ierb &= ~IB_TIMC; /* Disable timer inter. */
146 MFP->mf_tcdr = CLOCK_HZ/stathz; /* Set divisor */
147
148 statmin = (CLOCK_HZ/stathz) - (statvar >> 1);
149 profmin = (CLOCK_HZ/profhz) - (statvar >> 1);
150 clk2min = statmin;
151 #endif /* STATCLOCK */
152
153 /*
154 * Initialize Timer-B in the ST-MFP. This timer is used by the 'delay'
155 * function below. This time is setup to be continueously counting from
156 * 255 back to zero at a frequency of 614400Hz.
157 */
158 MFP->mf_tbcr = 0; /* Stop timer */
159 MFP->mf_iera &= ~IA_TIMB; /* Disable timer interrupts */
160 MFP->mf_tbdr = 0;
161 MFP->mf_tbcr = T_Q004; /* Start timer */
162
163 }
164
165 void cpu_initclocks()
166 {
167 MFP->mf_tacr = T_Q200; /* Start timer */
168 MFP->mf_ipra &= ~IA_TIMA; /* Clear pending interrupts */
169 MFP->mf_iera |= IA_TIMA; /* Enable timer interrupts */
170 MFP->mf_imra |= IA_TIMA; /* ..... */
171
172 #ifdef STATCLOCK
173 MFP->mf_tcdcr = (MFP->mf_tcdcr & 0x7) | (T_Q200<<4); /* Start */
174 MFP->mf_iprb &= ~IB_TIMC; /* Clear pending interrupts */
175 MFP->mf_ierb |= IB_TIMC; /* Enable timer interrupts */
176 MFP->mf_imrb |= IB_TIMC; /* ..... */
177 #endif /* STATCLOCK */
178 }
179
180 setstatclockrate(newhz)
181 int newhz;
182 {
183 #ifdef STATCLOCK
184 if (newhz == stathz)
185 clk2min = statmin;
186 else clk2min = profmin;
187 #endif /* STATCLOCK */
188 }
189
190 #ifdef STATCLOCK
191 void
192 statintr(frame)
193 register struct clockframe *frame;
194 {
195 register int var, r;
196
197 var = statvar - 1;
198 do {
199 r = random() & var;
200 } while(r == 0);
201
202 /*
203 * Note that we are always lagging behind as the new divisor
204 * value will not be loaded until the next interrupt. This
205 * shouldn't disturb the median frequency (I think ;-) ) as
206 * only the value used when switching frequencies is used
207 * twice. This shouldn't happen very often.
208 */
209 MFP->mf_tcdr = clk2min + r;
210
211 statclock(frame);
212 }
213 #endif /* STATCLOCK */
214
215 /*
216 * Returns number of usec since last recorded clock "tick"
217 * (i.e. clock interrupt).
218 */
219 clkread()
220 {
221 u_int delta;
222
223 delta = ((divisor - MFP->mf_tadr) * tick) / divisor;
224 /*
225 * Account for pending clock interrupts
226 */
227 if(MFP->mf_iera & IA_TIMA)
228 return(delta + tick);
229 return(delta);
230 }
231
232 #define TIMB_FREQ 614400
233 #define TIMB_LIMIT 256
234
235 /*
236 * Wait "n" microseconds.
237 * Relies on MFP-Timer B counting down from TIMB_LIMIT at TIMB_FREQ Hz.
238 * Note: timer had better have been programmed before this is first used!
239 */
240 void delay(n)
241 int n;
242 {
243 int tick, otick;
244
245 /*
246 * Read the counter first, so that the rest of the setup overhead is
247 * counted.
248 */
249 otick = MFP->mf_tbdr;
250
251 /*
252 * Calculate ((n * TIMER_FREQ) / 1e6) using explicit assembler code so
253 * we can take advantage of the intermediate 64-bit quantity to prevent
254 * loss of significance.
255 */
256 n -= 5;
257 if(n < 0)
258 return;
259 {
260 u_int temp;
261
262 __asm __volatile ("mulul %2,%1:%0" : "=d" (n), "=d" (temp)
263 : "d" (TIMB_FREQ));
264 __asm __volatile ("divul %1,%2:%0" : "=d" (n)
265 : "d"(1000000),"d"(temp),"0"(n));
266 }
267
268 while(n > 0) {
269 tick = MFP->mf_tbdr;
270 if(tick > otick)
271 n -= TIMB_LIMIT - (tick - otick);
272 else n -= otick - tick;
273 otick = tick;
274 }
275 }
276
277 #ifdef GPROF
278 /*
279 * profclock() is expanded in line in lev6intr() unless profiling kernel.
280 * Assumes it is called with clock interrupts blocked.
281 */
282 profclock(pc, ps)
283 caddr_t pc;
284 int ps;
285 {
286 /*
287 * Came from user mode.
288 * If this process is being profiled record the tick.
289 */
290 if (USERMODE(ps)) {
291 if (p->p_stats.p_prof.pr_scale)
292 addupc(pc, &curproc->p_stats.p_prof, 1);
293 }
294 /*
295 * Came from kernel (supervisor) mode.
296 * If we are profiling the kernel, record the tick.
297 */
298 else if (profiling < 2) {
299 register int s = pc - s_lowpc;
300
301 if (s < s_textsize)
302 kcount[s / (HISTFRACTION * sizeof (*kcount))]++;
303 }
304 /*
305 * Kernel profiling was on but has been disabled.
306 * Mark as no longer profiling kernel and if all profiling done,
307 * disable the clock.
308 */
309 if (profiling && (profon & PRF_KERNEL)) {
310 profon &= ~PRF_KERNEL;
311 if (profon == PRF_NONE)
312 stopprofclock();
313 }
314 }
315 #endif
316
317 /*
318 * Initialize the time of day register, based on the time base which is, e.g.
319 * from a filesystem.
320 */
321 inittodr(base)
322 time_t base;
323 {
324 u_long timbuf = base; /* assume no battery clock exists */
325
326 timbuf = gettod();
327
328 if(timbuf < base) {
329 printf("WARNING: bad date in battery clock\n");
330 timbuf = base;
331 }
332
333 /* Battery clock does not store usec's, so forget about it. */
334 time.tv_sec = timbuf;
335 }
336
337 resettodr()
338 {
339 if(settod(time.tv_sec) == 1)
340 return;
341 printf("Cannot set battery backed clock\n");
342 }
343
344 static char dmsize[12] =
345 {
346 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
347 };
348
349 static char ldmsize[12] =
350 {
351 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
352 };
353
354 static u_long
355 gettod()
356 {
357 int i, sps;
358 u_long new_time = 0;
359 char *msize;
360 mc_todregs clkregs;
361
362 sps = splhigh();
363 MC146818_GETTOD(RTC, &clkregs);
364 splx(sps);
365
366 if(range_test(clkregs[MC_HOUR], 0, 23))
367 return(0);
368 if(range_test(clkregs[MC_DOM], 1, 31))
369 return(0);
370 if (range_test(clkregs[MC_MONTH], 1, 12))
371 return(0);
372 if(range_test(clkregs[MC_YEAR], 0, 2000 - GEMSTARTOFTIME))
373 return(0);
374 clkregs[MC_YEAR] += GEMSTARTOFTIME;
375
376 for(i = BSDSTARTOFTIME; i < clkregs[MC_YEAR]; i++) {
377 if(is_leap(i))
378 new_time += 366;
379 else new_time += 365;
380 }
381
382 msize = is_leap(clkregs[MC_YEAR]) ? ldmsize : dmsize;
383 for(i = 0; i < (clkregs[MC_MONTH] - 1); i++)
384 new_time += msize[i];
385 new_time += clkregs[MC_DOM] - 1;
386 new_time *= SECS_DAY;
387 new_time += (clkregs[MC_HOUR] * 3600) + (clkregs[MC_MIN] * 60);
388 return(new_time + clkregs[MC_SEC]);
389 }
390
391 static int
392 settod(newtime)
393 u_long newtime;
394 {
395 register long days, rem, year;
396 register char *ml;
397 int sps, sec, min, hour, month;
398 mc_todregs clkregs;
399
400 /* Number of days since Jan. 1 'BSDSTARTOFTIME' */
401 days = newtime / SECS_DAY;
402 rem = newtime % SECS_DAY;
403
404 /*
405 * Calculate sec, min, hour
406 */
407 hour = rem / SECS_HOUR;
408 rem %= SECS_HOUR;
409 min = rem / 60;
410 sec = rem % 60;
411
412 /*
413 * Figure out the year. Day in year is left in 'days'.
414 */
415 year = BSDSTARTOFTIME;
416 while(days >= (rem = is_leap(year) ? 366 : 365)) {
417 ++year;
418 days -= rem;
419 }
420
421 /*
422 * Determine the month
423 */
424 ml = is_leap(year) ? ldmsize : dmsize;
425 for(month = 0; days >= ml[month]; ++month)
426 days -= ml[month];
427
428 /*
429 * Now that everything is calculated, program the RTC
430 */
431 mc146818_write(RTC, MC_REGA, MC_BASE_32_KHz);
432 mc146818_write(RTC, MC_REGB, MC_REGB_24HR | MC_REGB_BINARY);
433 sps = splhigh();
434 MC146818_GETTOD(RTC, &clkregs);
435 clkregs[MC_SEC] = sec;
436 clkregs[MC_MIN] = min;
437 clkregs[MC_HOUR] = hour;
438 clkregs[MC_DOM] = days+1;
439 clkregs[MC_MONTH] = month+1;
440 clkregs[MC_YEAR] = year - GEMSTARTOFTIME;
441 MC146818_PUTTOD(RTC, &clkregs);
442 splx(sps);
443
444 return(1);
445 }
446