clock.c revision 1.24 1 /* $NetBSD: clock.c,v 1.24 2001/01/29 08:32:23 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/systm.h>
48 #include <sys/device.h>
49 #include <sys/uio.h>
50 #include <sys/conf.h>
51
52 #include <dev/clock_subr.h>
53
54 #include <machine/psl.h>
55 #include <machine/cpu.h>
56 #include <machine/iomap.h>
57 #include <machine/mfp.h>
58 #include <atari/dev/clockreg.h>
59 #include <atari/atari/device.h>
60
61 #if defined(GPROF) && defined(PROFTIMER)
62 #include <machine/profile.h>
63 #endif
64
65 /*
66 * The MFP clock runs at 2457600Hz. We use a {system,stat,prof}clock divider
67 * of 200. Therefore the timer runs at an effective rate of:
68 * 2457600/200 = 12288Hz.
69 */
70 #define CLOCK_HZ 12288
71
72 /*
73 * Machine-dependent clock routines.
74 *
75 * Inittodr initializes the time of day hardware which provides
76 * date functions.
77 *
78 * Resettodr restores the time of day hardware after a time change.
79 */
80
81 struct clock_softc {
82 struct device sc_dev;
83 int sc_flags;
84 };
85
86 /*
87 * 'sc_flags' state info. Only used by the rtc-device functions.
88 */
89 #define RTC_OPEN 1
90
91 /* {b,c}devsw[] function prototypes for rtc functions */
92 dev_type_open(rtcopen);
93 dev_type_close(rtcclose);
94 dev_type_read(rtcread);
95 dev_type_write(rtcwrite);
96
97 static void clockattach __P((struct device *, struct device *, void *));
98 static int clockmatch __P((struct device *, struct cfdata *, void *));
99
100 struct cfattach clock_ca = {
101 sizeof(struct clock_softc), clockmatch, clockattach
102 };
103
104 extern struct cfdriver clock_cd;
105
106 void statintr __P((struct clockframe));
107
108 static u_long gettod __P((void));
109 static int twodigits __P((char *, int));
110
111 static int divisor; /* Systemclock divisor */
112
113 /*
114 * Statistics and profile clock intervals and variances. Variance must
115 * be a power of 2. Since this gives us an even number, not an odd number,
116 * we discard one case and compensate. That is, a variance of 64 would
117 * give us offsets in [0..63]. Instead, we take offsets in [1..63].
118 * This is symetric around the point 32, or statvar/2, and thus averages
119 * to that value (assuming uniform random numbers).
120 */
121 #ifdef STATCLOCK
122 static int statvar = 32; /* {stat,prof}clock variance */
123 static int statmin; /* statclock divisor - variance/2 */
124 static int profmin; /* profclock divisor - variance/2 */
125 static int clk2min; /* current, from above choises */
126 #endif
127
128 int
129 clockmatch(pdp, cfp, auxp)
130 struct device *pdp;
131 struct cfdata *cfp;
132 void *auxp;
133 {
134 if (!atari_realconfig) {
135 /*
136 * Initialize Timer-B in the ST-MFP. This timer is used by
137 * the 'delay' function below. This timer is setup to be
138 * continueously counting from 255 back to zero at a
139 * frequency of 614400Hz. We do this *early* in the
140 * initialisation process.
141 */
142 MFP->mf_tbcr = 0; /* Stop timer */
143 MFP->mf_iera &= ~IA_TIMB; /* Disable timer interrupts */
144 MFP->mf_tbdr = 0;
145 MFP->mf_tbcr = T_Q004; /* Start timer */
146
147 /*
148 * Initialize the time structure
149 */
150 time.tv_sec = 0;
151 time.tv_usec = 0;
152
153 return 0;
154 }
155 if(!strcmp("clock", auxp))
156 return(1);
157 return(0);
158 }
159
160 /*
161 * Start the real-time clock.
162 */
163 void clockattach(pdp, dp, auxp)
164 struct device *pdp, *dp;
165 void *auxp;
166 {
167 struct clock_softc *sc = (void *)dp;
168
169 sc->sc_flags = 0;
170
171 /*
172 * Initialize Timer-A in the ST-MFP. We use a divisor of 200.
173 * The MFP clock runs at 2457600Hz. Therefore the timer runs
174 * at an effective rate of: 2457600/200 = 12288Hz. The
175 * following expression works for 48, 64 or 96 hz.
176 */
177 divisor = CLOCK_HZ/hz;
178 MFP->mf_tacr = 0; /* Stop timer */
179 MFP->mf_iera &= ~IA_TIMA; /* Disable timer interrupts */
180 MFP->mf_tadr = divisor; /* Set divisor */
181
182 if (hz != 48 && hz != 64 && hz != 96) { /* XXX */
183 printf (": illegal value %d for systemclock, reset to %d\n\t",
184 hz, 64);
185 hz = 64;
186 }
187 printf(": system hz %d timer-A divisor 200/%d\n", hz, divisor);
188
189 #ifdef STATCLOCK
190 if ((stathz == 0) || (stathz > hz) || (CLOCK_HZ % stathz))
191 stathz = hz;
192 if ((profhz == 0) || (profhz > (hz << 1)) || (CLOCK_HZ % profhz))
193 profhz = hz << 1;
194
195 MFP->mf_tcdcr &= 0x7; /* Stop timer */
196 MFP->mf_ierb &= ~IB_TIMC; /* Disable timer inter. */
197 MFP->mf_tcdr = CLOCK_HZ/stathz; /* Set divisor */
198
199 statmin = (CLOCK_HZ/stathz) - (statvar >> 1);
200 profmin = (CLOCK_HZ/profhz) - (statvar >> 1);
201 clk2min = statmin;
202 #endif /* STATCLOCK */
203
204 }
205
206 void cpu_initclocks()
207 {
208 MFP->mf_tacr = T_Q200; /* Start timer */
209 MFP->mf_ipra = (u_int8_t)~IA_TIMA;/* Clear pending interrupts */
210 MFP->mf_iera |= IA_TIMA; /* Enable timer interrupts */
211 MFP->mf_imra |= IA_TIMA; /* ..... */
212
213 #ifdef STATCLOCK
214 MFP->mf_tcdcr = (MFP->mf_tcdcr & 0x7) | (T_Q200<<4); /* Start */
215 MFP->mf_iprb = (u_int8_t)~IB_TIMC;/* Clear pending interrupts */
216 MFP->mf_ierb |= IB_TIMC; /* Enable timer interrupts */
217 MFP->mf_imrb |= IB_TIMC; /* ..... */
218 #endif /* STATCLOCK */
219 }
220
221 void
222 setstatclockrate(newhz)
223 int newhz;
224 {
225 #ifdef STATCLOCK
226 if (newhz == stathz)
227 clk2min = statmin;
228 else clk2min = profmin;
229 #endif /* STATCLOCK */
230 }
231
232 #ifdef STATCLOCK
233 void
234 statintr(frame)
235 struct clockframe frame;
236 {
237 register int var, r;
238
239 var = statvar - 1;
240 do {
241 r = random() & var;
242 } while(r == 0);
243
244 /*
245 * Note that we are always lagging behind as the new divisor
246 * value will not be loaded until the next interrupt. This
247 * shouldn't disturb the median frequency (I think ;-) ) as
248 * only the value used when switching frequencies is used
249 * twice. This shouldn't happen very often.
250 */
251 MFP->mf_tcdr = clk2min + r;
252
253 statclock(&frame);
254 }
255 #endif /* STATCLOCK */
256
257 /*
258 * Returns number of usec since last recorded clock "tick"
259 * (i.e. clock interrupt).
260 */
261 long
262 clkread()
263 {
264 u_int delta;
265 u_char ipra, tadr;
266
267 /*
268 * Note: Order is important!
269 * By reading 'ipra' before 'tadr' and caching the data, I try to avoid
270 * the situation that very low value in 'tadr' is read (== a big delta)
271 * while also acccounting for a full 'tick' because the counter went
272 * through zero during the calculations.
273 */
274 ipra = MFP->mf_ipra; tadr = MFP->mf_tadr;
275
276 delta = ((divisor - tadr) * tick) / divisor;
277 /*
278 * Account for pending clock interrupts
279 */
280 if(ipra & IA_TIMA)
281 return(delta + tick);
282 return(delta);
283 }
284
285 #define TIMB_FREQ 614400
286 #define TIMB_LIMIT 256
287
288 /*
289 * Wait "n" microseconds.
290 * Relies on MFP-Timer B counting down from TIMB_LIMIT at TIMB_FREQ Hz.
291 * Note: timer had better have been programmed before this is first used!
292 */
293 void
294 delay(n)
295 int n;
296 {
297 int tick, otick;
298
299 /*
300 * Read the counter first, so that the rest of the setup overhead is
301 * counted.
302 */
303 otick = MFP->mf_tbdr;
304
305 /*
306 * Calculate ((n * TIMER_FREQ) / 1e6) using explicit assembler code so
307 * we can take advantage of the intermediate 64-bit quantity to prevent
308 * loss of significance.
309 */
310 n -= 5;
311 if(n < 0)
312 return;
313 {
314 u_int temp;
315
316 __asm __volatile ("mulul %2,%1:%0" : "=d" (n), "=d" (temp)
317 : "d" (TIMB_FREQ), "d" (n));
318 __asm __volatile ("divul %1,%2:%0" : "=d" (n)
319 : "d"(1000000),"d"(temp),"0"(n));
320 }
321
322 while(n > 0) {
323 tick = MFP->mf_tbdr;
324 if(tick > otick)
325 n -= TIMB_LIMIT - (tick - otick);
326 else n -= otick - tick;
327 otick = tick;
328 }
329 }
330
331 #ifdef GPROF
332 /*
333 * profclock() is expanded in line in lev6intr() unless profiling kernel.
334 * Assumes it is called with clock interrupts blocked.
335 */
336 profclock(pc, ps)
337 caddr_t pc;
338 int ps;
339 {
340 /*
341 * Came from user mode.
342 * If this process is being profiled record the tick.
343 */
344 if (USERMODE(ps)) {
345 if (p->p_stats.p_prof.pr_scale)
346 addupc(pc, &curproc->p_stats.p_prof, 1);
347 }
348 /*
349 * Came from kernel (supervisor) mode.
350 * If we are profiling the kernel, record the tick.
351 */
352 else if (profiling < 2) {
353 register int s = pc - s_lowpc;
354
355 if (s < s_textsize)
356 kcount[s / (HISTFRACTION * sizeof (*kcount))]++;
357 }
358 /*
359 * Kernel profiling was on but has been disabled.
360 * Mark as no longer profiling kernel and if all profiling done,
361 * disable the clock.
362 */
363 if (profiling && (profon & PRF_KERNEL)) {
364 profon &= ~PRF_KERNEL;
365 if (profon == PRF_NONE)
366 stopprofclock();
367 }
368 }
369 #endif
370
371 /***********************************************************************
372 * Real Time Clock support *
373 ***********************************************************************/
374
375 u_int mc146818_read(rtc, regno)
376 void *rtc;
377 u_int regno;
378 {
379 ((struct rtc *)rtc)->rtc_regno = regno;
380 return(((struct rtc *)rtc)->rtc_data & 0377);
381 }
382
383 void mc146818_write(rtc, regno, value)
384 void *rtc;
385 u_int regno, value;
386 {
387 ((struct rtc *)rtc)->rtc_regno = regno;
388 ((struct rtc *)rtc)->rtc_data = value;
389 }
390
391 /*
392 * Initialize the time of day register, assuming the RTC runs in UTC.
393 * Since we've got the 'rtc' device, this functionality should be removed
394 * from the kernel. The only problem to be solved before that can happen
395 * is the possibility of init(1) providing a way (rc.boot?) to set
396 * the RTC before single-user mode is entered.
397 */
398 void
399 inittodr(base)
400 time_t base;
401 {
402 /* Battery clock does not store usec's, so forget about it. */
403 time.tv_sec = gettod();
404 time.tv_usec = 0;
405 }
406
407 /*
408 * Function turned into a No-op. Use /dev/rtc to update the RTC.
409 */
410 void
411 resettodr()
412 {
413 return;
414 }
415
416 static u_long
417 gettod()
418 {
419 int sps;
420 mc_todregs clkregs;
421 struct clock_ymdhms dt;
422
423 sps = splhigh();
424 MC146818_GETTOD(RTC, &clkregs);
425 splx(sps);
426
427 if(clkregs[MC_SEC] > 59)
428 return(0);
429 if(clkregs[MC_MIN] > 59)
430 return(0);
431 if(clkregs[MC_HOUR] > 23)
432 return(0);
433 if(range_test(clkregs[MC_DOM], 1, 31))
434 return(0);
435 if (range_test(clkregs[MC_MONTH], 1, 12))
436 return(0);
437 if(clkregs[MC_YEAR] > 99)
438 return(0);
439
440 dt.dt_year = clkregs[MC_YEAR] + GEMSTARTOFTIME;
441 dt.dt_mon = clkregs[MC_MONTH];
442 dt.dt_day = clkregs[MC_DOM];
443 dt.dt_hour = clkregs[MC_HOUR];
444 dt.dt_min = clkregs[MC_MIN];
445 dt.dt_sec = clkregs[MC_SEC];
446
447 return(clock_ymdhms_to_secs(&dt));
448 }
449 /***********************************************************************
450 * RTC-device support *
451 ***********************************************************************/
452 int
453 rtcopen(dev, flag, mode, p)
454 dev_t dev;
455 int flag, mode;
456 struct proc *p;
457 {
458 int unit = minor(dev);
459 struct clock_softc *sc;
460
461 if (unit >= clock_cd.cd_ndevs)
462 return ENXIO;
463 sc = clock_cd.cd_devs[unit];
464 if (!sc)
465 return ENXIO;
466 if (sc->sc_flags & RTC_OPEN)
467 return EBUSY;
468
469 sc->sc_flags = RTC_OPEN;
470 return 0;
471 }
472
473 int
474 rtcclose(dev, flag, mode, p)
475 dev_t dev;
476 int flag;
477 int mode;
478 struct proc *p;
479 {
480 int unit = minor(dev);
481 struct clock_softc *sc = clock_cd.cd_devs[unit];
482
483 sc->sc_flags = 0;
484 return 0;
485 }
486
487 int
488 rtcread(dev, uio, flags)
489 dev_t dev;
490 struct uio *uio;
491 int flags;
492 {
493 struct clock_softc *sc;
494 mc_todregs clkregs;
495 int s, length;
496 char buffer[16];
497
498 sc = clock_cd.cd_devs[minor(dev)];
499
500 s = splhigh();
501 MC146818_GETTOD(RTC, &clkregs);
502 splx(s);
503
504 sprintf(buffer, "%4d%02d%02d%02d%02d.%02d\n",
505 clkregs[MC_YEAR] + GEMSTARTOFTIME,
506 clkregs[MC_MONTH], clkregs[MC_DOM],
507 clkregs[MC_HOUR], clkregs[MC_MIN], clkregs[MC_SEC]);
508
509 if (uio->uio_offset > strlen(buffer))
510 return 0;
511
512 length = strlen(buffer) - uio->uio_offset;
513 if (length > uio->uio_resid)
514 length = uio->uio_resid;
515
516 return(uiomove((caddr_t)buffer, length, uio));
517 }
518
519 static int
520 twodigits(buffer, pos)
521 char *buffer;
522 int pos;
523 {
524 int result = 0;
525
526 if (buffer[pos] >= '0' && buffer[pos] <= '9')
527 result = (buffer[pos] - '0') * 10;
528 if (buffer[pos+1] >= '0' && buffer[pos+1] <= '9')
529 result += (buffer[pos+1] - '0');
530 return(result);
531 }
532
533 int
534 rtcwrite(dev, uio, flags)
535 dev_t dev;
536 struct uio *uio;
537 int flags;
538 {
539 mc_todregs clkregs;
540 int s, length, error;
541 char buffer[16];
542
543 /*
544 * We require atomic updates!
545 */
546 length = uio->uio_resid;
547 if (uio->uio_offset || (length != sizeof(buffer)
548 && length != sizeof(buffer - 1)))
549 return(EINVAL);
550
551 if ((error = uiomove((caddr_t)buffer, sizeof(buffer), uio)))
552 return(error);
553
554 if (length == sizeof(buffer) && buffer[sizeof(buffer) - 1] != '\n')
555 return(EINVAL);
556
557 s = splclock();
558 MC146818_GETTOD(RTC, &clkregs);
559 splx(s);
560
561 clkregs[MC_SEC] = twodigits(buffer, 13);
562 clkregs[MC_MIN] = twodigits(buffer, 10);
563 clkregs[MC_HOUR] = twodigits(buffer, 8);
564 clkregs[MC_DOM] = twodigits(buffer, 6);
565 clkregs[MC_MONTH] = twodigits(buffer, 4);
566 s = twodigits(buffer, 0) * 100 + twodigits(buffer, 2);
567 clkregs[MC_YEAR] = s - GEMSTARTOFTIME;
568
569 s = splclock();
570 MC146818_PUTTOD(RTC, &clkregs);
571 splx(s);
572
573 return(0);
574 }
575