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