clock.c revision 1.41.6.1.4.1 1 /* $NetBSD: clock.c,v 1.41.6.1.4.1 2010/04/21 00:33:52 matt Exp $ */
2
3 /*
4 * Copyright (c) 1982, 1990 The Regents of the University of California.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * the Systems Programming Group of the University of Utah Computer
9 * Science Department.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
35 * from: Utah $Hdr: clock.c 1.18 91/01/21$
36 *
37 * @(#)clock.c 7.6 (Berkeley) 5/7/91
38 */
39 /*
40 * Copyright (c) 1988 University of Utah.
41 *
42 * This code is derived from software contributed to Berkeley by
43 * the Systems Programming Group of the University of Utah Computer
44 * Science Department.
45 *
46 * Redistribution and use in source and binary forms, with or without
47 * modification, are permitted provided that the following conditions
48 * are met:
49 * 1. Redistributions of source code must retain the above copyright
50 * notice, this list of conditions and the following disclaimer.
51 * 2. Redistributions in binary form must reproduce the above copyright
52 * notice, this list of conditions and the following disclaimer in the
53 * documentation and/or other materials provided with the distribution.
54 * 3. All advertising materials mentioning features or use of this software
55 * must display the following acknowledgement:
56 * This product includes software developed by the University of
57 * California, Berkeley and its contributors.
58 * 4. Neither the name of the University nor the names of its contributors
59 * may be used to endorse or promote products derived from this software
60 * without specific prior written permission.
61 *
62 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
63 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
64 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
65 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
66 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
67 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
68 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
69 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
70 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
71 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
72 * SUCH DAMAGE.
73 *
74 * from: Utah $Hdr: clock.c 1.18 91/01/21$
75 *
76 * @(#)clock.c 7.6 (Berkeley) 5/7/91
77 */
78
79 #include <sys/cdefs.h>
80 __KERNEL_RCSID(0, "$NetBSD: clock.c,v 1.41.6.1.4.1 2010/04/21 00:33:52 matt Exp $");
81
82 #include <sys/param.h>
83 #include <sys/kernel.h>
84 #include <sys/systm.h>
85 #include <sys/device.h>
86 #include <sys/uio.h>
87 #include <sys/conf.h>
88 #include <sys/proc.h>
89 #include <sys/event.h>
90 #include <sys/timetc.h>
91
92 #include <dev/clock_subr.h>
93
94 #include <machine/psl.h>
95 #include <machine/cpu.h>
96 #include <machine/iomap.h>
97 #include <machine/mfp.h>
98 #include <atari/dev/clockreg.h>
99 #include <atari/atari/device.h>
100
101 #if defined(GPROF) && defined(PROFTIMER)
102 #include <machine/profile.h>
103 #endif
104
105 static int atari_rtc_get(todr_chip_handle_t, struct clock_ymdhms *);
106 static int atari_rtc_set(todr_chip_handle_t, struct clock_ymdhms *);
107
108 /*
109 * The MFP clock runs at 2457600Hz. We use a {system,stat,prof}clock divider
110 * of 200. Therefore the timer runs at an effective rate of:
111 * 2457600/200 = 12288Hz.
112 */
113 #define CLOCK_HZ 12288
114
115 static u_int clk_getcounter(struct timecounter *);
116
117 static struct timecounter clk_timecounter = {
118 clk_getcounter, /* get_timecount */
119 0, /* no poll_pps */
120 ~0u, /* counter_mask */
121 CLOCK_HZ, /* frequency */
122 "clock", /* name, overriden later */
123 100, /* quality */
124 NULL, /* prev */
125 NULL, /* next */
126 };
127
128 /*
129 * Machine-dependent clock routines.
130 *
131 * Inittodr initializes the time of day hardware which provides
132 * date functions.
133 *
134 * Resettodr restores the time of day hardware after a time change.
135 */
136
137 struct clock_softc {
138 struct device sc_dev;
139 int sc_flags;
140 };
141
142 /*
143 * 'sc_flags' state info. Only used by the rtc-device functions.
144 */
145 #define RTC_OPEN 1
146
147 dev_type_open(rtcopen);
148 dev_type_close(rtcclose);
149 dev_type_read(rtcread);
150 dev_type_write(rtcwrite);
151
152 static void clockattach __P((struct device *, struct device *, void *));
153 static int clockmatch __P((struct device *, struct cfdata *, void *));
154
155 CFATTACH_DECL(clock, sizeof(struct clock_softc),
156 clockmatch, clockattach, NULL, NULL);
157
158 extern struct cfdriver clock_cd;
159
160 const struct cdevsw rtc_cdevsw = {
161 rtcopen, rtcclose, rtcread, rtcwrite, noioctl,
162 nostop, notty, nopoll, nommap, nokqfilter,
163 };
164
165 void statintr __P((struct clockframe));
166
167 static int twodigits __P((char *, int));
168
169 static int divisor; /* Systemclock divisor */
170
171 /*
172 * Statistics and profile clock intervals and variances. Variance must
173 * be a power of 2. Since this gives us an even number, not an odd number,
174 * we discard one case and compensate. That is, a variance of 64 would
175 * give us offsets in [0..63]. Instead, we take offsets in [1..63].
176 * This is symmetric around the point 32, or statvar/2, and thus averages
177 * to that value (assuming uniform random numbers).
178 */
179 #ifdef STATCLOCK
180 static int statvar = 32; /* {stat,prof}clock variance */
181 static int statmin; /* statclock divisor - variance/2 */
182 static int profmin; /* profclock divisor - variance/2 */
183 static int clk2min; /* current, from above choices */
184 #endif
185
186 int
187 clockmatch(pdp, cfp, auxp)
188 struct device *pdp;
189 struct cfdata *cfp;
190 void *auxp;
191 {
192 if (!atari_realconfig) {
193 /*
194 * Initialize Timer-B in the ST-MFP. This timer is used by
195 * the 'delay' function below. This timer is setup to be
196 * continueously counting from 255 back to zero at a
197 * frequency of 614400Hz. We do this *early* in the
198 * initialisation process.
199 */
200 MFP->mf_tbcr = 0; /* Stop timer */
201 MFP->mf_iera &= ~IA_TIMB; /* Disable timer interrupts */
202 MFP->mf_tbdr = 0;
203 MFP->mf_tbcr = T_Q004; /* Start timer */
204
205 return 0;
206 }
207 if(!strcmp("clock", auxp))
208 return(1);
209 return(0);
210 }
211
212 /*
213 * Start the real-time clock.
214 */
215 void clockattach(pdp, dp, auxp)
216 struct device *pdp, *dp;
217 void *auxp;
218 {
219 struct clock_softc *sc = (void *)dp;
220 static struct todr_chip_handle tch;
221
222 tch.todr_gettime_ymdhms = atari_rtc_get;
223 tch.todr_settime_ymdhms = atari_rtc_set;
224 tch.todr_setwen = NULL;
225
226 todr_attach(&tch);
227
228 sc->sc_flags = 0;
229
230 /*
231 * Initialize Timer-A in the ST-MFP. We use a divisor of 200.
232 * The MFP clock runs at 2457600Hz. Therefore the timer runs
233 * at an effective rate of: 2457600/200 = 12288Hz. The
234 * following expression works for 48, 64 or 96 hz.
235 */
236 divisor = CLOCK_HZ/hz;
237 MFP->mf_tacr = 0; /* Stop timer */
238 MFP->mf_iera &= ~IA_TIMA; /* Disable timer interrupts */
239 MFP->mf_tadr = divisor; /* Set divisor */
240
241 clk_timecounter.tc_frequency = CLOCK_HZ;
242
243 if (hz != 48 && hz != 64 && hz != 96) { /* XXX */
244 printf (": illegal value %d for systemclock, reset to %d\n\t",
245 hz, 64);
246 hz = 64;
247 }
248 printf(": system hz %d timer-A divisor 200/%d\n", hz, divisor);
249 tc_init(&clk_timecounter);
250
251 #ifdef STATCLOCK
252 if ((stathz == 0) || (stathz > hz) || (CLOCK_HZ % stathz))
253 stathz = hz;
254 if ((profhz == 0) || (profhz > (hz << 1)) || (CLOCK_HZ % profhz))
255 profhz = hz << 1;
256
257 MFP->mf_tcdcr &= 0x7; /* Stop timer */
258 MFP->mf_ierb &= ~IB_TIMC; /* Disable timer inter. */
259 MFP->mf_tcdr = CLOCK_HZ/stathz; /* Set divisor */
260
261 statmin = (CLOCK_HZ/stathz) - (statvar >> 1);
262 profmin = (CLOCK_HZ/profhz) - (statvar >> 1);
263 clk2min = statmin;
264 #endif /* STATCLOCK */
265
266 }
267
268 void cpu_initclocks()
269 {
270 MFP->mf_tacr = T_Q200; /* Start timer */
271 MFP->mf_ipra = (u_int8_t)~IA_TIMA;/* Clear pending interrupts */
272 MFP->mf_iera |= IA_TIMA; /* Enable timer interrupts */
273 MFP->mf_imra |= IA_TIMA; /* ..... */
274
275 #ifdef STATCLOCK
276 MFP->mf_tcdcr = (MFP->mf_tcdcr & 0x7) | (T_Q200<<4); /* Start */
277 MFP->mf_iprb = (u_int8_t)~IB_TIMC;/* Clear pending interrupts */
278 MFP->mf_ierb |= IB_TIMC; /* Enable timer interrupts */
279 MFP->mf_imrb |= IB_TIMC; /* ..... */
280 #endif /* STATCLOCK */
281 }
282
283 void
284 setstatclockrate(newhz)
285 int newhz;
286 {
287 #ifdef STATCLOCK
288 if (newhz == stathz)
289 clk2min = statmin;
290 else clk2min = profmin;
291 #endif /* STATCLOCK */
292 }
293
294 #ifdef STATCLOCK
295 void
296 statintr(frame)
297 struct clockframe frame;
298 {
299 register int var, r;
300
301 var = statvar - 1;
302 do {
303 r = random() & var;
304 } while(r == 0);
305
306 /*
307 * Note that we are always lagging behind as the new divisor
308 * value will not be loaded until the next interrupt. This
309 * shouldn't disturb the median frequency (I think ;-) ) as
310 * only the value used when switching frequencies is used
311 * twice. This shouldn't happen very often.
312 */
313 MFP->mf_tcdr = clk2min + r;
314
315 statclock(&frame);
316 }
317 #endif /* STATCLOCK */
318
319 static u_int
320 clk_getcounter(struct timecounter *tc)
321 {
322 uint32_t delta, count, cur_hardclock;
323 uint8_t ipra, tadr;
324 int s;
325 static uint32_t lastcount;
326
327 s = splhigh();
328 cur_hardclock = hardclock_ticks;
329 ipra = MFP->mf_ipra;
330 tadr = MFP->mf_tadr;
331 delta = divisor - tadr;
332
333 if (ipra & IA_TIMA)
334 delta += divisor;
335 splx(s);
336
337 count = (divisor * cur_hardclock) + delta;
338 if ((int32_t)(count - lastcount) < 0) {
339 /* XXX wrapped; maybe hardclock() is blocked more than 2/HZ */
340 count = lastcount + 1;
341 }
342 lastcount = count;
343
344 return count;
345 }
346
347 #define TIMB_FREQ 614400
348 #define TIMB_LIMIT 256
349
350 /*
351 * Wait "n" microseconds.
352 * Relies on MFP-Timer B counting down from TIMB_LIMIT at TIMB_FREQ Hz.
353 * Note: timer had better have been programmed before this is first used!
354 */
355 void
356 delay(unsigned int n)
357 {
358 int ticks, otick, remaining;
359
360 /*
361 * Read the counter first, so that the rest of the setup overhead is
362 * counted.
363 */
364 otick = MFP->mf_tbdr;
365
366 if (n <= UINT_MAX / TIMB_FREQ) {
367 /*
368 * For unsigned arithmetic, division can be replaced with
369 * multiplication with the inverse and a shift.
370 */
371 remaining = n * TIMB_FREQ / 1000000;
372 } else {
373 /* This is a very long delay.
374 * Being slow here doesn't matter.
375 */
376 remaining = (unsigned long long) n * TIMB_FREQ / 1000000;
377 }
378
379 while(remaining > 0) {
380 ticks = MFP->mf_tbdr;
381 if(ticks > otick)
382 remaining -= TIMB_LIMIT - (ticks - otick);
383 else
384 remaining -= otick - ticks;
385 otick = ticks;
386 }
387 }
388
389 #ifdef GPROF
390 /*
391 * profclock() is expanded in line in lev6intr() unless profiling kernel.
392 * Assumes it is called with clock interrupts blocked.
393 */
394 profclock(pc, ps)
395 void *pc;
396 int ps;
397 {
398 /*
399 * Came from user mode.
400 * If this process is being profiled record the tick.
401 */
402 if (USERMODE(ps)) {
403 if (p->p_stats.p_prof.pr_scale)
404 addupc(pc, &curproc->p_stats.p_prof, 1);
405 }
406 /*
407 * Came from kernel (supervisor) mode.
408 * If we are profiling the kernel, record the tick.
409 */
410 else if (profiling < 2) {
411 register int s = pc - s_lowpc;
412
413 if (s < s_textsize)
414 kcount[s / (HISTFRACTION * sizeof (*kcount))]++;
415 }
416 /*
417 * Kernel profiling was on but has been disabled.
418 * Mark as no longer profiling kernel and if all profiling done,
419 * disable the clock.
420 */
421 if (profiling && (profon & PRF_KERNEL)) {
422 profon &= ~PRF_KERNEL;
423 if (profon == PRF_NONE)
424 stopprofclock();
425 }
426 }
427 #endif
428
429 /***********************************************************************
430 * Real Time Clock support *
431 ***********************************************************************/
432
433 u_int mc146818_read(rtc, regno)
434 void *rtc;
435 u_int regno;
436 {
437 ((struct rtc *)rtc)->rtc_regno = regno;
438 return(((struct rtc *)rtc)->rtc_data & 0377);
439 }
440
441 void mc146818_write(rtc, regno, value)
442 void *rtc;
443 u_int regno, value;
444 {
445 ((struct rtc *)rtc)->rtc_regno = regno;
446 ((struct rtc *)rtc)->rtc_data = value;
447 }
448
449 static int
450 atari_rtc_get(todr_chip_handle_t todr, struct clock_ymdhms *dtp)
451 {
452 int sps;
453 mc_todregs clkregs;
454 u_int regb;
455
456 sps = splhigh();
457 regb = mc146818_read(RTC, MC_REGB);
458 MC146818_GETTOD(RTC, &clkregs);
459 splx(sps);
460
461 regb &= MC_REGB_24HR|MC_REGB_BINARY;
462 if (regb != (MC_REGB_24HR|MC_REGB_BINARY)) {
463 printf("Error: Nonstandard RealTimeClock Configuration -"
464 " value ignored\n"
465 " A write to /dev/rtc will correct this.\n");
466 return(0);
467 }
468 if(clkregs[MC_SEC] > 59)
469 return -1;
470 if(clkregs[MC_MIN] > 59)
471 return -1;
472 if(clkregs[MC_HOUR] > 23)
473 return -1;
474 if(range_test(clkregs[MC_DOM], 1, 31))
475 return -1;
476 if (range_test(clkregs[MC_MONTH], 1, 12))
477 return -1;
478 if(clkregs[MC_YEAR] > 99)
479 return -1;
480
481 dtp->dt_year = clkregs[MC_YEAR] + GEMSTARTOFTIME;
482 dtp->dt_mon = clkregs[MC_MONTH];
483 dtp->dt_day = clkregs[MC_DOM];
484 dtp->dt_hour = clkregs[MC_HOUR];
485 dtp->dt_min = clkregs[MC_MIN];
486 dtp->dt_sec = clkregs[MC_SEC];
487
488 return 0;
489 }
490
491 static int
492 atari_rtc_set(todr_chip_handle_t todr, struct clock_ymdhms *dtp)
493 {
494 int s;
495 mc_todregs clkregs;
496
497 clkregs[MC_YEAR] = dtp->dt_year - GEMSTARTOFTIME;
498 clkregs[MC_MONTH] = dtp->dt_mon;
499 clkregs[MC_DOM] = dtp->dt_day;
500 clkregs[MC_HOUR] = dtp->dt_hour;
501 clkregs[MC_MIN] = dtp->dt_min;
502 clkregs[MC_SEC] = dtp->dt_sec;
503
504 s = splclock();
505 MC146818_PUTTOD(RTC, &clkregs);
506 splx(s);
507
508 return 0;
509 }
510
511 /***********************************************************************
512 * RTC-device support *
513 ***********************************************************************/
514 int
515 rtcopen(dev, flag, mode, l)
516 dev_t dev;
517 int flag, mode;
518 struct lwp *l;
519 {
520 int unit = minor(dev);
521 struct clock_softc *sc;
522
523 sc = device_lookup_private(&clock_cd, unit);
524 if (sc == NULL)
525 return ENXIO;
526 if (sc->sc_flags & RTC_OPEN)
527 return EBUSY;
528
529 sc->sc_flags = RTC_OPEN;
530 return 0;
531 }
532
533 int
534 rtcclose(dev, flag, mode, l)
535 dev_t dev;
536 int flag;
537 int mode;
538 struct lwp *l;
539 {
540 int unit = minor(dev);
541 struct clock_softc *sc = device_lookup_private(&clock_cd, unit);
542
543 sc->sc_flags = 0;
544 return 0;
545 }
546
547 int
548 rtcread(dev, uio, flags)
549 dev_t dev;
550 struct uio *uio;
551 int flags;
552 {
553 struct clock_softc *sc;
554 mc_todregs clkregs;
555 int s, length;
556 char buffer[16];
557
558 sc = device_lookup_private(&clock_cd, minor(dev));
559
560 s = splhigh();
561 MC146818_GETTOD(RTC, &clkregs);
562 splx(s);
563
564 sprintf(buffer, "%4d%02d%02d%02d%02d.%02d\n",
565 clkregs[MC_YEAR] + GEMSTARTOFTIME,
566 clkregs[MC_MONTH], clkregs[MC_DOM],
567 clkregs[MC_HOUR], clkregs[MC_MIN], clkregs[MC_SEC]);
568
569 if (uio->uio_offset > strlen(buffer))
570 return 0;
571
572 length = strlen(buffer) - uio->uio_offset;
573 if (length > uio->uio_resid)
574 length = uio->uio_resid;
575
576 return(uiomove((void *)buffer, length, uio));
577 }
578
579 static int
580 twodigits(buffer, pos)
581 char *buffer;
582 int pos;
583 {
584 int result = 0;
585
586 if (buffer[pos] >= '0' && buffer[pos] <= '9')
587 result = (buffer[pos] - '0') * 10;
588 if (buffer[pos+1] >= '0' && buffer[pos+1] <= '9')
589 result += (buffer[pos+1] - '0');
590 return(result);
591 }
592
593 int
594 rtcwrite(dev, uio, flags)
595 dev_t dev;
596 struct uio *uio;
597 int flags;
598 {
599 mc_todregs clkregs;
600 int s, length, error;
601 char buffer[16];
602
603 /*
604 * We require atomic updates!
605 */
606 length = uio->uio_resid;
607 if (uio->uio_offset || (length != sizeof(buffer)
608 && length != sizeof(buffer - 1)))
609 return(EINVAL);
610
611 if ((error = uiomove((void *)buffer, sizeof(buffer), uio)))
612 return(error);
613
614 if (length == sizeof(buffer) && buffer[sizeof(buffer) - 1] != '\n')
615 return(EINVAL);
616
617 s = splclock();
618 mc146818_write(RTC, MC_REGB,
619 mc146818_read(RTC, MC_REGB) | MC_REGB_24HR | MC_REGB_BINARY);
620 MC146818_GETTOD(RTC, &clkregs);
621 splx(s);
622
623 clkregs[MC_SEC] = twodigits(buffer, 13);
624 clkregs[MC_MIN] = twodigits(buffer, 10);
625 clkregs[MC_HOUR] = twodigits(buffer, 8);
626 clkregs[MC_DOM] = twodigits(buffer, 6);
627 clkregs[MC_MONTH] = twodigits(buffer, 4);
628 s = twodigits(buffer, 0) * 100 + twodigits(buffer, 2);
629 clkregs[MC_YEAR] = s - GEMSTARTOFTIME;
630
631 s = splclock();
632 MC146818_PUTTOD(RTC, &clkregs);
633 splx(s);
634
635 return(0);
636 }
637