clock.c revision 1.49 1 /* $NetBSD: clock.c,v 1.49 2009/07/07 16:16:18 tsutsui 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.49 2009/07/07 16:16:18 tsutsui 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/dev/clockvar.h>
100 #include <atari/atari/device.h>
101
102 #if defined(GPROF) && defined(PROFTIMER)
103 #include <machine/profile.h>
104 #endif
105
106 static int atari_rtc_get(todr_chip_handle_t, struct clock_ymdhms *);
107 static int atari_rtc_set(todr_chip_handle_t, struct clock_ymdhms *);
108
109 /*
110 * The MFP clock runs at 2457600Hz. We use a {system,stat,prof}clock divider
111 * of 200. Therefore the timer runs at an effective rate of:
112 * 2457600/200 = 12288Hz.
113 */
114 #define CLOCK_HZ 12288
115
116 static u_int clk_getcounter(struct timecounter *);
117
118 static struct timecounter clk_timecounter = {
119 clk_getcounter, /* get_timecount */
120 0, /* no poll_pps */
121 ~0u, /* counter_mask */
122 CLOCK_HZ, /* frequency */
123 "clock", /* name, overriden later */
124 100, /* quality */
125 NULL, /* prev */
126 NULL, /* next */
127 };
128
129 /*
130 * Machine-dependent clock routines.
131 *
132 * Inittodr initializes the time of day hardware which provides
133 * date functions.
134 *
135 * Resettodr restores the time of day hardware after a time change.
136 */
137
138 struct clock_softc {
139 struct device sc_dev;
140 int sc_flags;
141 };
142
143 /*
144 * 'sc_flags' state info. Only used by the rtc-device functions.
145 */
146 #define RTC_OPEN 1
147
148 dev_type_open(rtcopen);
149 dev_type_close(rtcclose);
150 dev_type_read(rtcread);
151 dev_type_write(rtcwrite);
152
153 static void clockattach(struct device *, struct device *, void *);
154 static int clockmatch(struct device *, struct cfdata *, void *);
155
156 CFATTACH_DECL(clock, sizeof(struct clock_softc),
157 clockmatch, clockattach, NULL, NULL);
158
159 extern struct cfdriver clock_cd;
160
161 const struct cdevsw rtc_cdevsw = {
162 rtcopen, rtcclose, rtcread, rtcwrite, noioctl,
163 nostop, notty, nopoll, nommap, nokqfilter,
164 };
165
166 void statintr(struct clockframe);
167
168 static int twodigits(char *, int);
169
170 static int divisor; /* Systemclock divisor */
171
172 /*
173 * Statistics and profile clock intervals and variances. Variance must
174 * be a power of 2. Since this gives us an even number, not an odd number,
175 * we discard one case and compensate. That is, a variance of 64 would
176 * give us offsets in [0..63]. Instead, we take offsets in [1..63].
177 * This is symmetric around the point 32, or statvar/2, and thus averages
178 * to that value (assuming uniform random numbers).
179 */
180 #ifdef STATCLOCK
181 static int statvar = 32; /* {stat,prof}clock variance */
182 static int statmin; /* statclock divisor - variance/2 */
183 static int profmin; /* profclock divisor - variance/2 */
184 static int clk2min; /* current, from above choices */
185 #endif
186
187 int
188 clockmatch(struct device *pdp, struct cfdata *cfp, void *auxp)
189 {
190
191 if (!strcmp("clock", auxp))
192 return 1;
193 return 0;
194 }
195
196 /*
197 * Start the real-time clock.
198 */
199 void clockattach(struct device *pdp, struct device *dp, void *auxp)
200 {
201
202 struct clock_softc *sc = (void *)dp;
203 static struct todr_chip_handle tch;
204
205 tch.todr_gettime_ymdhms = atari_rtc_get;
206 tch.todr_settime_ymdhms = atari_rtc_set;
207 tch.todr_setwen = NULL;
208
209 todr_attach(&tch);
210
211 sc->sc_flags = 0;
212
213 /*
214 * Initialize Timer-A in the ST-MFP. We use a divisor of 200.
215 * The MFP clock runs at 2457600Hz. Therefore the timer runs
216 * at an effective rate of: 2457600/200 = 12288Hz. The
217 * following expression works for 48, 64 or 96 hz.
218 */
219 divisor = CLOCK_HZ/hz;
220 MFP->mf_tacr = 0; /* Stop timer */
221 MFP->mf_iera &= ~IA_TIMA; /* Disable timer interrupts */
222 MFP->mf_tadr = divisor; /* Set divisor */
223
224 clk_timecounter.tc_frequency = CLOCK_HZ;
225
226 if (hz != 48 && hz != 64 && hz != 96) { /* XXX */
227 printf (": illegal value %d for systemclock, reset to %d\n\t",
228 hz, 64);
229 hz = 64;
230 }
231 printf(": system hz %d timer-A divisor 200/%d\n", hz, divisor);
232 tc_init(&clk_timecounter);
233
234 #ifdef STATCLOCK
235 if ((stathz == 0) || (stathz > hz) || (CLOCK_HZ % stathz))
236 stathz = hz;
237 if ((profhz == 0) || (profhz > (hz << 1)) || (CLOCK_HZ % profhz))
238 profhz = hz << 1;
239
240 MFP->mf_tcdcr &= 0x7; /* Stop timer */
241 MFP->mf_ierb &= ~IB_TIMC; /* Disable timer inter. */
242 MFP->mf_tcdr = CLOCK_HZ/stathz; /* Set divisor */
243
244 statmin = (CLOCK_HZ/stathz) - (statvar >> 1);
245 profmin = (CLOCK_HZ/profhz) - (statvar >> 1);
246 clk2min = statmin;
247 #endif /* STATCLOCK */
248 }
249
250 void cpu_initclocks(void)
251 {
252
253 MFP->mf_tacr = T_Q200; /* Start timer */
254 MFP->mf_ipra = (u_int8_t)~IA_TIMA;/* Clear pending interrupts */
255 MFP->mf_iera |= IA_TIMA; /* Enable timer interrupts */
256 MFP->mf_imra |= IA_TIMA; /* ..... */
257
258 #ifdef STATCLOCK
259 MFP->mf_tcdcr = (MFP->mf_tcdcr & 0x7) | (T_Q200<<4); /* Start */
260 MFP->mf_iprb = (u_int8_t)~IB_TIMC;/* Clear pending interrupts */
261 MFP->mf_ierb |= IB_TIMC; /* Enable timer interrupts */
262 MFP->mf_imrb |= IB_TIMC; /* ..... */
263 #endif /* STATCLOCK */
264 }
265
266 void
267 setstatclockrate(int newhz)
268 {
269
270 #ifdef STATCLOCK
271 if (newhz == stathz)
272 clk2min = statmin;
273 else clk2min = profmin;
274 #endif /* STATCLOCK */
275 }
276
277 #ifdef STATCLOCK
278 void
279 statintr(struct clockframe frame)
280 {
281 register int var, r;
282
283 var = statvar - 1;
284 do {
285 r = random() & var;
286 } while (r == 0);
287
288 /*
289 * Note that we are always lagging behind as the new divisor
290 * value will not be loaded until the next interrupt. This
291 * shouldn't disturb the median frequency (I think ;-) ) as
292 * only the value used when switching frequencies is used
293 * twice. This shouldn't happen very often.
294 */
295 MFP->mf_tcdr = clk2min + r;
296
297 statclock(&frame);
298 }
299 #endif /* STATCLOCK */
300
301 static u_int
302 clk_getcounter(struct timecounter *tc)
303 {
304 uint32_t delta, count, cur_hardclock;
305 uint8_t ipra, tadr;
306 int s;
307 static uint32_t lastcount;
308
309 s = splhigh();
310 cur_hardclock = hardclock_ticks;
311 ipra = MFP->mf_ipra;
312 tadr = MFP->mf_tadr;
313 delta = divisor - tadr;
314
315 if (ipra & IA_TIMA)
316 delta += divisor;
317 splx(s);
318
319 count = (divisor * cur_hardclock) + delta;
320 if ((int32_t)(count - lastcount) < 0) {
321 /* XXX wrapped; maybe hardclock() is blocked more than 2/HZ */
322 count = lastcount + 1;
323 }
324 lastcount = count;
325
326 return count;
327 }
328
329 #define TIMB_FREQ 614400
330 #define TIMB_LIMIT 256
331
332 void
333 init_delay(void)
334 {
335
336 /*
337 * Initialize Timer-B in the ST-MFP. This timer is used by
338 * the 'delay' function below. This timer is setup to be
339 * continueously counting from 255 back to zero at a
340 * frequency of 614400Hz. We do this *early* in the
341 * initialisation process.
342 */
343 MFP->mf_tbcr = 0; /* Stop timer */
344 MFP->mf_iera &= ~IA_TIMB; /* Disable timer interrupts */
345 MFP->mf_tbdr = 0;
346 MFP->mf_tbcr = T_Q004; /* Start timer */
347 }
348
349 /*
350 * Wait "n" microseconds.
351 * Relies on MFP-Timer B counting down from TIMB_LIMIT at TIMB_FREQ Hz.
352 * Note: timer had better have been programmed before this is first used!
353 */
354 void
355 delay(unsigned int n)
356 {
357 int ticks, otick, remaining;
358
359 /*
360 * Read the counter first, so that the rest of the setup overhead is
361 * counted.
362 */
363 otick = MFP->mf_tbdr;
364
365 if (n <= UINT_MAX / TIMB_FREQ) {
366 /*
367 * For unsigned arithmetic, division can be replaced with
368 * multiplication with the inverse and a shift.
369 */
370 remaining = n * TIMB_FREQ / 1000000;
371 } else {
372 /* This is a very long delay.
373 * Being slow here doesn't matter.
374 */
375 remaining = (unsigned long long) n * TIMB_FREQ / 1000000;
376 }
377
378 while (remaining > 0) {
379 ticks = MFP->mf_tbdr;
380 if (ticks > otick)
381 remaining -= TIMB_LIMIT - (ticks - otick);
382 else
383 remaining -= otick - ticks;
384 otick = ticks;
385 }
386 }
387
388 #ifdef GPROF
389 /*
390 * profclock() is expanded in line in lev6intr() unless profiling kernel.
391 * Assumes it is called with clock interrupts blocked.
392 */
393 profclock(void *pc, int ps)
394 {
395
396 /*
397 * Came from user mode.
398 * If this process is being profiled record the tick.
399 */
400 if (USERMODE(ps)) {
401 if (p->p_stats.p_prof.pr_scale)
402 addupc(pc, &curproc->p_stats.p_prof, 1);
403 }
404 /*
405 * Came from kernel (supervisor) mode.
406 * If we are profiling the kernel, record the tick.
407 */
408 else if (profiling < 2) {
409 register int s = pc - s_lowpc;
410
411 if (s < s_textsize)
412 kcount[s / (HISTFRACTION * sizeof(*kcount))]++;
413 }
414 /*
415 * Kernel profiling was on but has been disabled.
416 * Mark as no longer profiling kernel and if all profiling done,
417 * disable the clock.
418 */
419 if (profiling && (profon & PRF_KERNEL)) {
420 profon &= ~PRF_KERNEL;
421 if (profon == PRF_NONE)
422 stopprofclock();
423 }
424 }
425 #endif
426
427 /***********************************************************************
428 * Real Time Clock support *
429 ***********************************************************************/
430
431 u_int mc146818_read(void *rtc, u_int regno)
432 {
433
434 ((struct rtc *)rtc)->rtc_regno = regno;
435 return ((struct rtc *)rtc)->rtc_data & 0377;
436 }
437
438 void mc146818_write(void *rtc, u_int regno, u_int value)
439 {
440
441 ((struct rtc *)rtc)->rtc_regno = regno;
442 ((struct rtc *)rtc)->rtc_data = value;
443 }
444
445 static int
446 atari_rtc_get(todr_chip_handle_t todr, struct clock_ymdhms *dtp)
447 {
448 int sps;
449 mc_todregs clkregs;
450 u_int regb;
451
452 sps = splhigh();
453 regb = mc146818_read(RTC, MC_REGB);
454 MC146818_GETTOD(RTC, &clkregs);
455 splx(sps);
456
457 regb &= MC_REGB_24HR|MC_REGB_BINARY;
458 if (regb != (MC_REGB_24HR|MC_REGB_BINARY)) {
459 printf("Error: Nonstandard RealTimeClock Configuration -"
460 " value ignored\n"
461 " A write to /dev/rtc will correct this.\n");
462 return 0;
463 }
464 if (clkregs[MC_SEC] > 59)
465 return -1;
466 if (clkregs[MC_MIN] > 59)
467 return -1;
468 if (clkregs[MC_HOUR] > 23)
469 return -1;
470 if (range_test(clkregs[MC_DOM], 1, 31))
471 return -1;
472 if (range_test(clkregs[MC_MONTH], 1, 12))
473 return -1;
474 if (clkregs[MC_YEAR] > 99)
475 return -1;
476
477 dtp->dt_year = clkregs[MC_YEAR] + GEMSTARTOFTIME;
478 dtp->dt_mon = clkregs[MC_MONTH];
479 dtp->dt_day = clkregs[MC_DOM];
480 dtp->dt_hour = clkregs[MC_HOUR];
481 dtp->dt_min = clkregs[MC_MIN];
482 dtp->dt_sec = clkregs[MC_SEC];
483
484 return 0;
485 }
486
487 static int
488 atari_rtc_set(todr_chip_handle_t todr, struct clock_ymdhms *dtp)
489 {
490 int s;
491 mc_todregs clkregs;
492
493 clkregs[MC_YEAR] = dtp->dt_year - GEMSTARTOFTIME;
494 clkregs[MC_MONTH] = dtp->dt_mon;
495 clkregs[MC_DOM] = dtp->dt_day;
496 clkregs[MC_HOUR] = dtp->dt_hour;
497 clkregs[MC_MIN] = dtp->dt_min;
498 clkregs[MC_SEC] = dtp->dt_sec;
499
500 s = splclock();
501 MC146818_PUTTOD(RTC, &clkregs);
502 splx(s);
503
504 return 0;
505 }
506
507 /***********************************************************************
508 * RTC-device support *
509 ***********************************************************************/
510 int
511 rtcopen(dev_t dev, int flag, int mode, struct lwp *l)
512 {
513 int unit = minor(dev);
514 struct clock_softc *sc;
515
516 sc = device_lookup_private(&clock_cd, unit);
517 if (sc == NULL)
518 return ENXIO;
519 if (sc->sc_flags & RTC_OPEN)
520 return EBUSY;
521
522 sc->sc_flags = RTC_OPEN;
523 return 0;
524 }
525
526 int
527 rtcclose(dev_t dev, int flag, int mode, struct lwp *l)
528 {
529 int unit = minor(dev);
530 struct clock_softc *sc = device_lookup_private(&clock_cd, unit);
531
532 sc->sc_flags = 0;
533 return 0;
534 }
535
536 int
537 rtcread(dev_t dev, struct uio *uio, int flags)
538 {
539 struct clock_softc *sc;
540 mc_todregs clkregs;
541 int s, length;
542 char buffer[16];
543
544 sc = device_lookup_private(&clock_cd, minor(dev));
545
546 s = splhigh();
547 MC146818_GETTOD(RTC, &clkregs);
548 splx(s);
549
550 sprintf(buffer, "%4d%02d%02d%02d%02d.%02d\n",
551 clkregs[MC_YEAR] + GEMSTARTOFTIME,
552 clkregs[MC_MONTH], clkregs[MC_DOM],
553 clkregs[MC_HOUR], clkregs[MC_MIN], clkregs[MC_SEC]);
554
555 if (uio->uio_offset > strlen(buffer))
556 return 0;
557
558 length = strlen(buffer) - uio->uio_offset;
559 if (length > uio->uio_resid)
560 length = uio->uio_resid;
561
562 return uiomove((void *)buffer, length, uio);
563 }
564
565 static int
566 twodigits(char *buffer, int pos)
567 {
568 int result = 0;
569
570 if (buffer[pos] >= '0' && buffer[pos] <= '9')
571 result = (buffer[pos] - '0') * 10;
572 if (buffer[pos+1] >= '0' && buffer[pos+1] <= '9')
573 result += (buffer[pos+1] - '0');
574 return result;
575 }
576
577 int
578 rtcwrite(dev_t dev, struct uio *uio, int flags)
579 {
580 mc_todregs clkregs;
581 int s, length, error;
582 char buffer[16];
583
584 /*
585 * We require atomic updates!
586 */
587 length = uio->uio_resid;
588 if (uio->uio_offset || (length != sizeof(buffer)
589 && length != sizeof(buffer - 1)))
590 return EINVAL;
591
592 if ((error = uiomove((void *)buffer, sizeof(buffer), uio)))
593 return error;
594
595 if (length == sizeof(buffer) && buffer[sizeof(buffer) - 1] != '\n')
596 return EINVAL;
597
598 s = splclock();
599 mc146818_write(RTC, MC_REGB,
600 mc146818_read(RTC, MC_REGB) | MC_REGB_24HR | MC_REGB_BINARY);
601 MC146818_GETTOD(RTC, &clkregs);
602 splx(s);
603
604 clkregs[MC_SEC] = twodigits(buffer, 13);
605 clkregs[MC_MIN] = twodigits(buffer, 10);
606 clkregs[MC_HOUR] = twodigits(buffer, 8);
607 clkregs[MC_DOM] = twodigits(buffer, 6);
608 clkregs[MC_MONTH] = twodigits(buffer, 4);
609 s = twodigits(buffer, 0) * 100 + twodigits(buffer, 2);
610 clkregs[MC_YEAR] = s - GEMSTARTOFTIME;
611
612 s = splclock();
613 MC146818_PUTTOD(RTC, &clkregs);
614 splx(s);
615
616 return 0;
617 }
618