clock.c revision 1.7 1 /* $NetBSD: clock.c,v 1.7 1996/01/06 20:11:06 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 * Real Time Clock support *
319 ***********************************************************************/
320
321 u_int mc146818_read(rtc, regno)
322 void *rtc;
323 u_int regno;
324 {
325 ((struct rtc *)rtc)->rtc_regno = regno;
326 return(((struct rtc *)rtc)->rtc_data & 0377);
327 }
328
329 void mc146818_write(rtc, regno, value)
330 void *rtc;
331 u_int regno, value;
332 {
333 ((struct rtc *)rtc)->rtc_regno = regno;
334 ((struct rtc *)rtc)->rtc_data = value;
335 }
336
337 /*
338 * Initialize the time of day register, based on the time base which is, e.g.
339 * from a filesystem.
340 */
341 inittodr(base)
342 time_t base;
343 {
344 u_long timbuf = base; /* assume no battery clock exists */
345
346 timbuf = gettod();
347
348 if(timbuf < base) {
349 printf("WARNING: bad date in battery clock\n");
350 timbuf = base;
351 }
352
353 /* Battery clock does not store usec's, so forget about it. */
354 time.tv_sec = timbuf;
355 }
356
357 resettodr()
358 {
359 if(settod(time.tv_sec) == 1)
360 return;
361 printf("Cannot set battery backed clock\n");
362 }
363
364 static char dmsize[12] =
365 {
366 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
367 };
368
369 static char ldmsize[12] =
370 {
371 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
372 };
373
374 static u_long
375 gettod()
376 {
377 int i, sps;
378 u_long new_time = 0;
379 char *msize;
380 mc_todregs clkregs;
381
382 sps = splhigh();
383 MC146818_GETTOD(RTC, &clkregs);
384 splx(sps);
385
386 if(range_test(clkregs[MC_HOUR], 0, 23))
387 return(0);
388 if(range_test(clkregs[MC_DOM], 1, 31))
389 return(0);
390 if (range_test(clkregs[MC_MONTH], 1, 12))
391 return(0);
392 if(range_test(clkregs[MC_YEAR], 0, 2000 - GEMSTARTOFTIME))
393 return(0);
394 clkregs[MC_YEAR] += GEMSTARTOFTIME;
395
396 for(i = BSDSTARTOFTIME; i < clkregs[MC_YEAR]; i++) {
397 if(is_leap(i))
398 new_time += 366;
399 else new_time += 365;
400 }
401
402 msize = is_leap(clkregs[MC_YEAR]) ? ldmsize : dmsize;
403 for(i = 0; i < (clkregs[MC_MONTH] - 1); i++)
404 new_time += msize[i];
405 new_time += clkregs[MC_DOM] - 1;
406 new_time *= SECS_DAY;
407 new_time += (clkregs[MC_HOUR] * 3600) + (clkregs[MC_MIN] * 60);
408 return(new_time + clkregs[MC_SEC]);
409 }
410
411 static int
412 settod(newtime)
413 u_long newtime;
414 {
415 register long days, rem, year;
416 register char *ml;
417 int sps, sec, min, hour, month;
418 mc_todregs clkregs;
419
420 /* Number of days since Jan. 1 'BSDSTARTOFTIME' */
421 days = newtime / SECS_DAY;
422 rem = newtime % SECS_DAY;
423
424 /*
425 * Calculate sec, min, hour
426 */
427 hour = rem / SECS_HOUR;
428 rem %= SECS_HOUR;
429 min = rem / 60;
430 sec = rem % 60;
431
432 /*
433 * Figure out the year. Day in year is left in 'days'.
434 */
435 year = BSDSTARTOFTIME;
436 while(days >= (rem = is_leap(year) ? 366 : 365)) {
437 ++year;
438 days -= rem;
439 }
440
441 /*
442 * Determine the month
443 */
444 ml = is_leap(year) ? ldmsize : dmsize;
445 for(month = 0; days >= ml[month]; ++month)
446 days -= ml[month];
447
448 /*
449 * Now that everything is calculated, program the RTC
450 */
451 mc146818_write(RTC, MC_REGA, MC_BASE_32_KHz);
452 mc146818_write(RTC, MC_REGB, MC_REGB_24HR | MC_REGB_BINARY);
453 sps = splhigh();
454 MC146818_GETTOD(RTC, &clkregs);
455 clkregs[MC_SEC] = sec;
456 clkregs[MC_MIN] = min;
457 clkregs[MC_HOUR] = hour;
458 clkregs[MC_DOM] = days+1;
459 clkregs[MC_MONTH] = month+1;
460 clkregs[MC_YEAR] = year - GEMSTARTOFTIME;
461 MC146818_PUTTOD(RTC, &clkregs);
462 splx(sps);
463
464 return(1);
465 }
466