clock.c revision 1.15 1 /* $NetBSD: clock.c,v 1.15 1996/05/10 14:30:53 is 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 <sys/systm.h>
49 #include <machine/psl.h>
50 #include <machine/cpu.h>
51 #include <amiga/amiga/device.h>
52 #include <amiga/amiga/custom.h>
53 #include <amiga/amiga/cia.h>
54 #ifdef DRACO
55 #include <amiga/amiga/drcustom.h>
56 #endif
57 #include <amiga/dev/rtc.h>
58 #include <amiga/dev/zbusvar.h>
59
60 #if defined(PROF) && defined(PROFTIMER)
61 #include <sys/PROF.h>
62 #endif
63
64 /* the clocks run at NTSC: 715.909kHz or PAL: 709.379kHz.
65 We're using a 100 Hz clock. */
66
67 #define CLK_INTERVAL amiga_clk_interval
68 int amiga_clk_interval;
69 int eclockfreq;
70 struct CIA *clockcia;
71
72 /*
73 * Machine-dependent clock routines.
74 *
75 * Startrtclock restarts the real-time clock, which provides
76 * hardclock interrupts to kern_clock.c.
77 *
78 * Inittodr initializes the time of day hardware which provides
79 * date functions.
80 *
81 * Resettodr restores the time of day hardware after a time change.
82 *
83 * A note on the real-time clock:
84 * We actually load the clock with CLK_INTERVAL-1 instead of CLK_INTERVAL.
85 * This is because the counter decrements to zero after N+1 enabled clock
86 * periods where N is the value loaded into the counter.
87 */
88
89 int clockmatch __P((struct device *, void *, void *));
90 void clockattach __P((struct device *, struct device *, void *));
91 void cpu_initclocks __P((void));
92 void setmicspertick __P((void));
93
94 struct cfattach clock_ca = {
95 sizeof(struct device), clockmatch, clockattach
96 };
97
98 struct cfdriver clock_cd = {
99 NULL, "clock", DV_DULL, NULL, 0 };
100
101 int
102 clockmatch(pdp, match, auxp)
103 struct device *pdp;
104 void *match, *auxp;
105 {
106
107 if (matchname("clock", auxp)
108 #ifdef DRACO
109 && (is_draco() < 4)
110 #endif
111 )
112 return(1);
113 return(0);
114 }
115
116 /*
117 * Start the real-time clock.
118 */
119 void
120 clockattach(pdp, dp, auxp)
121 struct device *pdp, *dp;
122 void *auxp;
123 {
124 unsigned short interval;
125 char cia;
126
127 if (eclockfreq == 0)
128 eclockfreq = 715909; /* guess NTSC */
129
130 CLK_INTERVAL = (eclockfreq / 100);
131
132 #ifdef DRACO
133 if (is_draco()) {
134 clockcia = (struct CIA *)CIAAbase;
135 cia = 'A';
136 } else
137 #endif
138 {
139 clockcia = (struct CIA *)CIABbase;
140 cia = 'B';
141 }
142
143 printf(": CIA %c system hz %d hardware hz %d\n", cia, hz, eclockfreq);
144
145 /*
146 * stop timer A
147 */
148 clockcia->cra = clockcia->cra & 0xc0;
149 clockcia->icr = 1 << 0; /* disable timer A interrupt */
150 interval = clockcia->icr; /* and make sure it's clear */
151
152 /*
153 * load interval into registers.
154 * the clocks run at NTSC: 715.909kHz or PAL: 709.379kHz
155 * supprort for PAL WHEN?!?! XXX
156 */
157 interval = CLK_INTERVAL - 1;
158
159 /*
160 * order of setting is important !
161 */
162 clockcia->talo = interval & 0xff;
163 clockcia->tahi = interval >> 8;
164 }
165
166 void
167 cpu_initclocks()
168 {
169 /*
170 * enable interrupts for timer A
171 */
172 clockcia->icr = (1<<7) | (1<<0);
173
174 /*
175 * start timer A in continuous shot mode
176 */
177 clockcia->cra = (clockcia->cra & 0xc0) | 1;
178
179 /*
180 * and globally enable interrupts for ciab
181 */
182 #ifdef DRACO
183 if (is_draco()) /* we use cia a on DraCo */
184 *draco_intena |= DRIRQ_INT2;
185 else
186 #endif
187 custom.intena = INTF_SETCLR | INTF_EXTER;
188 }
189
190 void
191 setstatclockrate(hz)
192 int hz;
193 {
194 }
195
196 /*
197 * Returns number of usec since last recorded clock "tick"
198 * (i.e. clock interrupt).
199 */
200 u_long
201 clkread()
202 {
203 u_char hi, hi2, lo;
204 u_int interval;
205
206 hi = clockcia->tahi;
207 lo = clockcia->talo;
208 hi2 = clockcia->tahi;
209 if (hi != hi2) {
210 lo = clockcia->talo;
211 hi = hi2;
212 }
213
214 interval = (CLK_INTERVAL - 1) - ((hi<<8) | lo);
215
216 /*
217 * should read ICR and if there's an int pending, adjust interval.
218 * However, * since reading ICR clears the interrupt, we'd lose a
219 * hardclock int, and * this is not tolerable.
220 */
221
222 return((interval * tick) / CLK_INTERVAL);
223 }
224
225 u_int micspertick;
226
227 /*
228 * we set up as much of the CIAa as possible
229 * as all access to chip memory are very slow.
230 */
231 void
232 setmicspertick()
233 {
234 #ifdef DRACO
235 if (is_draco())
236 return; /* XXX */
237 #endif
238 micspertick = (1000000ULL << 20) / 715909;
239
240 /*
241 * disable interrupts (just in case.)
242 */
243 ciaa.icr = 0x3;
244
245 /*
246 * stop both timers if not already
247 */
248 ciaa.cra &= ~1;
249 ciaa.crb &= ~1;
250
251 /*
252 * set timer B in "count timer A underflows" mode
253 * set tiemr A in one-shot mode
254 */
255 ciaa.crb = (ciaa.crb & 0x80) | 0x48;
256 ciaa.cra = (ciaa.cra & 0xc0) | 0x08;
257 }
258
259 /*
260 * this function assumes that on any entry beyond the first
261 * the following condintions exist:
262 * Interrupts for Timers A and B are disabled.
263 * Timers A and B are stoped.
264 * Timers A and B are in one-shot mode with B counting timer A underflows
265 *
266 */
267 void
268 delay(mic)
269 int mic;
270 {
271 u_int temp;
272
273 #ifdef DRACO
274 if (is_draco()) {
275 DELAY(mic);
276 return;
277 }
278 #endif
279 if (micspertick == 0)
280 setmicspertick();
281
282 if (mic <= 1)
283 return;
284
285 /*
286 * basically this is going to do an integer
287 * usec / (1000000 / 715909) with no loss of
288 * precision
289 */
290 temp = mic >> 12;
291 asm("divul %3,%1:%0" : "=d" (temp) : "d" (mic >> 12), "0" (mic << 20),
292 "d" (micspertick));
293
294 if ((temp & 0xffff0000) > 0x10000) {
295 mic = (temp >> 16) - 1;
296 temp &= 0xffff;
297
298 /*
299 * set timer A in continous mode
300 */
301 ciaa.cra = (ciaa.cra & 0xc0) | 0x00;
302
303 /*
304 * latch/load/start "counts of timer A underflows" in B
305 */
306 ciaa.tblo = mic & 0xff;
307 ciaa.tbhi = mic >> 8;
308
309 /*
310 * timer A latches 0xffff
311 * and start it.
312 */
313 ciaa.talo = 0xff;
314 ciaa.tahi = 0xff;
315 ciaa.cra |= 1;
316
317 while (ciaa.crb & 1)
318 ;
319
320 /*
321 * stop timer A
322 */
323 ciaa.cra &= ~1;
324
325 /*
326 * set timer A in one shot mode
327 */
328 ciaa.cra = (ciaa.cra & 0xc0) | 0x08;
329 } else if ((temp & 0xffff0000) == 0x10000) {
330 temp &= 0xffff;
331
332 /*
333 * timer A is in one shot latch/load/start 1 full turn
334 */
335 ciaa.talo = 0xff;
336 ciaa.tahi = 0xff;
337 while (ciaa.cra & 1)
338 ;
339 }
340 if (temp < 1)
341 return;
342
343 /*
344 * temp is now residual ammount, latch/load/start it.
345 */
346 ciaa.talo = temp & 0xff;
347 ciaa.tahi = temp >> 8;
348 while (ciaa.cra & 1)
349 ;
350 }
351
352 /*
353 * Needs to be calibrated for use, its way off most of the time
354 */
355 void
356 DELAY(mic)
357 int mic;
358 {
359 u_long n;
360 short hpos;
361
362 #ifdef DRACO
363 if (is_draco()) {
364 while (--mic > 0)
365 n = *draco_intena;
366 return;
367 }
368 #endif
369 /*
370 * this function uses HSync pulses as base units. The custom chips
371 * display only deals with 31.6kHz/2 refresh, this gives us a
372 * resolution of 1/15800 s, which is ~63us (add some fuzz so we really
373 * wait awhile, even if using small timeouts)
374 */
375 n = mic/63 + 2;
376 do {
377 hpos = custom.vhposr & 0xff00;
378 while (hpos == (custom.vhposr & 0xff00))
379 ;
380 } while (n--);
381 }
382
383 #if notyet
384
385 /* implement this later. I'd suggest using both timers in CIA-A, they're
386 not yet used. */
387
388 #include "clock.h"
389 #if NCLOCK > 0
390 /*
391 * /dev/clock: mappable high resolution timer.
392 *
393 * This code implements a 32-bit recycling counter (with a 4 usec period)
394 * using timers 2 & 3 on the 6840 clock chip. The counter can be mapped
395 * RO into a user's address space to achieve low overhead (no system calls),
396 * high-precision timing.
397 *
398 * Note that timer 3 is also used for the high precision profiling timer
399 * (PROFTIMER code above). Care should be taken when both uses are
400 * configured as only a token effort is made to avoid conflicting use.
401 */
402 #include <sys/proc.h>
403 #include <sys/resourcevar.h>
404 #include <sys/ioctl.h>
405 #include <sys/malloc.h>
406 #include <vm/vm.h>
407 #include <amiga/amiga/clockioctl.h>
408 #include <sys/specdev.h>
409 #include <sys/vnode.h>
410 #include <sys/mman.h>
411
412 int clockon = 0; /* non-zero if high-res timer enabled */
413 #ifdef PROFTIMER
414 int profprocs = 0; /* # of procs using profiling timer */
415 #endif
416 #ifdef DEBUG
417 int clockdebug = 0;
418 #endif
419
420 /*ARGSUSED*/
421 clockopen(dev, flags)
422 dev_t dev;
423 {
424 #ifdef PROFTIMER
425 #ifdef PROF
426 /*
427 * Kernel profiling enabled, give up.
428 */
429 if (profiling)
430 return(EBUSY);
431 #endif
432 /*
433 * If any user processes are profiling, give up.
434 */
435 if (profprocs)
436 return(EBUSY);
437 #endif
438 if (!clockon) {
439 startclock();
440 clockon++;
441 }
442 return(0);
443 }
444
445 /*ARGSUSED*/
446 clockclose(dev, flags)
447 dev_t dev;
448 {
449 (void) clockunmmap(dev, (caddr_t)0, curproc); /* XXX */
450 stopclock();
451 clockon = 0;
452 return(0);
453 }
454
455 /*ARGSUSED*/
456 clockioctl(dev, cmd, data, flag, p)
457 dev_t dev;
458 u_long cmd;
459 caddr_t data;
460 struct proc *p;
461 {
462 int error = 0;
463
464 switch (cmd) {
465
466 case CLOCKMAP:
467 error = clockmmap(dev, (caddr_t *)data, p);
468 break;
469
470 case CLOCKUNMAP:
471 error = clockunmmap(dev, *(caddr_t *)data, p);
472 break;
473
474 case CLOCKGETRES:
475 *(int *)data = CLK_RESOLUTION;
476 break;
477
478 default:
479 error = EINVAL;
480 break;
481 }
482 return(error);
483 }
484
485 /*ARGSUSED*/
486 clockmap(dev, off, prot)
487 dev_t dev;
488 {
489 return((off + (INTIOBASE+CLKBASE+CLKSR-1)) >> PGSHIFT);
490 }
491
492 clockmmap(dev, addrp, p)
493 dev_t dev;
494 caddr_t *addrp;
495 struct proc *p;
496 {
497 int error;
498 struct vnode vn;
499 struct specinfo si;
500 int flags;
501
502 flags = MAP_FILE|MAP_SHARED;
503 if (*addrp)
504 flags |= MAP_FIXED;
505 else
506 *addrp = (caddr_t)0x1000000; /* XXX */
507 vn.v_type = VCHR; /* XXX */
508 vn.v_specinfo = &si; /* XXX */
509 vn.v_rdev = dev; /* XXX */
510 error = vm_mmap(&p->p_vmspace->vm_map, (vm_offset_t *)addrp,
511 PAGE_SIZE, VM_PROT_ALL, flags, (caddr_t)&vn, 0);
512 return(error);
513 }
514
515 clockunmmap(dev, addr, p)
516 dev_t dev;
517 caddr_t addr;
518 struct proc *p;
519 {
520 int rv;
521
522 if (addr == 0)
523 return(EINVAL); /* XXX: how do we deal with this? */
524 rv = vm_deallocate(p->p_vmspace->vm_map, (vm_offset_t)addr, PAGE_SIZE);
525 return(rv == KERN_SUCCESS ? 0 : EINVAL);
526 }
527
528 startclock()
529 {
530 register struct clkreg *clk = (struct clkreg *)clkstd[0];
531
532 clk->clk_msb2 = -1; clk->clk_lsb2 = -1;
533 clk->clk_msb3 = -1; clk->clk_lsb3 = -1;
534
535 clk->clk_cr2 = CLK_CR3;
536 clk->clk_cr3 = CLK_OENAB|CLK_8BIT;
537 clk->clk_cr2 = CLK_CR1;
538 clk->clk_cr1 = CLK_IENAB;
539 }
540
541 stopclock()
542 {
543 register struct clkreg *clk = (struct clkreg *)clkstd[0];
544
545 clk->clk_cr2 = CLK_CR3;
546 clk->clk_cr3 = 0;
547 clk->clk_cr2 = CLK_CR1;
548 clk->clk_cr1 = CLK_IENAB;
549 }
550 #endif
551
552 #endif
553
554
555 #ifdef PROFTIMER
556 /*
557 * This code allows the amiga kernel to use one of the extra timers on
558 * the clock chip for profiling, instead of the regular system timer.
559 * The advantage of this is that the profiling timer can be turned up to
560 * a higher interrupt rate, giving finer resolution timing. The profclock
561 * routine is called from the lev6intr in locore, and is a specialized
562 * routine that calls addupc. The overhead then is far less than if
563 * hardclock/softclock was called. Further, the context switch code in
564 * locore has been changed to turn the profile clock on/off when switching
565 * into/out of a process that is profiling (startprofclock/stopprofclock).
566 * This reduces the impact of the profiling clock on other users, and might
567 * possibly increase the accuracy of the profiling.
568 */
569 int profint = PRF_INTERVAL; /* Clock ticks between interrupts */
570 int profscale = 0; /* Scale factor from sys clock to prof clock */
571 char profon = 0; /* Is profiling clock on? */
572
573 /* profon values - do not change, locore.s assumes these values */
574 #define PRF_NONE 0x00
575 #define PRF_USER 0x01
576 #define PRF_KERNEL 0x80
577
578 initprofclock()
579 {
580 #if NCLOCK > 0
581 struct proc *p = curproc; /* XXX */
582
583 /*
584 * If the high-res timer is running, force profiling off.
585 * Unfortunately, this gets reflected back to the user not as
586 * an error but as a lack of results.
587 */
588 if (clockon) {
589 p->p_stats->p_prof.pr_scale = 0;
590 return;
591 }
592 /*
593 * Keep track of the number of user processes that are profiling
594 * by checking the scale value.
595 *
596 * XXX: this all assumes that the profiling code is well behaved;
597 * i.e. profil() is called once per process with pcscale non-zero
598 * to turn it on, and once with pcscale zero to turn it off.
599 * Also assumes you don't do any forks or execs. Oh well, there
600 * is always adb...
601 */
602 if (p->p_stats->p_prof.pr_scale)
603 profprocs++;
604 else
605 profprocs--;
606 #endif
607 /*
608 * The profile interrupt interval must be an even divisor
609 * of the CLK_INTERVAL so that scaling from a system clock
610 * tick to a profile clock tick is possible using integer math.
611 */
612 if (profint > CLK_INTERVAL || (CLK_INTERVAL % profint) != 0)
613 profint = CLK_INTERVAL;
614 profscale = CLK_INTERVAL / profint;
615 }
616
617 startprofclock()
618 {
619 unsigned short interval;
620
621 /* stop timer B */
622 clockcia->crb = clockcia->crb & 0xc0;
623
624 /* load interval into registers.
625 the clocks run at NTSC: 715.909kHz or PAL: 709.379kHz */
626
627 interval = profint - 1;
628
629 /* order of setting is important ! */
630 clockcia->tblo = interval & 0xff;
631 clockcia->tbhi = interval >> 8;
632
633 /* enable interrupts for timer B */
634 clockcia->icr = (1<<7) | (1<<1);
635
636 /* start timer B in continuous shot mode */
637 clockcia->crb = (clockcia->crb & 0xc0) | 1;
638 }
639
640 stopprofclock()
641 {
642 /* stop timer B */
643 clockcia->crb = clockcia->crb & 0xc0;
644 }
645
646 #ifdef PROF
647 /*
648 * profclock() is expanded in line in lev6intr() unless profiling kernel.
649 * Assumes it is called with clock interrupts blocked.
650 */
651 profclock(pc, ps)
652 caddr_t pc;
653 int ps;
654 {
655 /*
656 * Came from user mode.
657 * If this process is being profiled record the tick.
658 */
659 if (USERMODE(ps)) {
660 if (p->p_stats.p_prof.pr_scale)
661 addupc(pc, &curproc->p_stats.p_prof, 1);
662 }
663 /*
664 * Came from kernel (supervisor) mode.
665 * If we are profiling the kernel, record the tick.
666 */
667 else if (profiling < 2) {
668 register int s = pc - s_lowpc;
669
670 if (s < s_textsize)
671 kcount[s / (HISTFRACTION * sizeof (*kcount))]++;
672 }
673 /*
674 * Kernel profiling was on but has been disabled.
675 * Mark as no longer profiling kernel and if all profiling done,
676 * disable the clock.
677 */
678 if (profiling && (profon & PRF_KERNEL)) {
679 profon &= ~PRF_KERNEL;
680 if (profon == PRF_NONE)
681 stopprofclock();
682 }
683 }
684 #endif
685 #endif
686
687 /* this is a hook set by a clock driver for the configured realtime clock,
688 returning plain current unix-time */
689 long (*gettod) __P((void));
690 int (*settod) __P((long));
691 void *clockaddr;
692
693 long a3gettod __P((void));
694 long a2gettod __P((void));
695 int a3settod __P((long));
696 int a2settod __P((long));
697 int rtcinit __P((void));
698
699 /*
700 * Initialize the time of day register, based on the time base which is, e.g.
701 * from a filesystem.
702 */
703 void
704 inittodr(base)
705 time_t base;
706 {
707 u_long timbuf = base; /* assume no battery clock exists */
708
709 if (gettod == NULL && rtcinit() == 0)
710 printf("WARNING: no battery clock\n");
711 else
712 timbuf = gettod();
713
714 if (timbuf < base) {
715 printf("WARNING: bad date in battery clock\n");
716 timbuf = base;
717 }
718
719 /* Battery clock does not store usec's, so forget about it. */
720 time.tv_sec = timbuf;
721 }
722
723 void
724 resettodr()
725 {
726 if (settod && settod(time.tv_sec) == 0)
727 printf("Cannot set battery backed clock\n");
728 }
729
730 int
731 rtcinit()
732 {
733 clockaddr = (void *)ztwomap(0xdc0000);
734 #ifdef DRACO
735 if (is_draco()) {
736 /* XXX to be done */
737 gettod = (void *)0;
738 settod = (void *)0;
739 return 0;
740 } else
741 #endif
742 if (is_a3000() || is_a4000()) {
743 if (a3gettod() == 0)
744 return(0);
745 gettod = a3gettod;
746 settod = a3settod;
747 } else {
748 if (a2gettod() == 0)
749 return(0);
750 gettod = a2gettod;
751 settod = a2settod;
752 }
753 return(1);
754 }
755
756 static int month_days[12] = {
757 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
758 };
759
760 long
761 a3gettod()
762 {
763 struct rtclock3000 *rt;
764 int i, year, month, day, wday, hour, min, sec;
765 u_long tmp;
766
767 rt = clockaddr;
768
769 /* hold clock */
770 rt->control1 = A3CONTROL1_HOLD_CLOCK;
771
772 /* read it */
773 sec = rt->second1 * 10 + rt->second2;
774 min = rt->minute1 * 10 + rt->minute2;
775 hour = rt->hour1 * 10 + rt->hour2;
776 wday = rt->weekday;
777 day = rt->day1 * 10 + rt->day2;
778 month = rt->month1 * 10 + rt->month2;
779 year = rt->year1 * 10 + rt->year2 + 1900;
780
781 /* let it run again.. */
782 rt->control1 = A3CONTROL1_FREE_CLOCK;
783
784 if (range_test(hour, 0, 23))
785 return(0);
786 if (range_test(wday, 0, 6))
787 return(0);
788 if (range_test(day, 1, 31))
789 return(0);
790 if (range_test(month, 1, 12))
791 return(0);
792 if (range_test(year, STARTOFTIME, 2000))
793 return(0);
794
795 tmp = 0;
796
797 for (i = STARTOFTIME; i < year; i++)
798 tmp += days_in_year(i);
799 if (leapyear(year) && month > FEBRUARY)
800 tmp++;
801
802 for (i = 1; i < month; i++)
803 tmp += days_in_month(i);
804
805 tmp += (day - 1);
806 tmp = ((tmp * 24 + hour) * 60 + min) * 60 + sec;
807
808 return(tmp);
809 }
810
811 int
812 a3settod(tim)
813 long tim;
814 {
815 register int i;
816 register long hms, day;
817 u_char sec1, sec2;
818 u_char min1, min2;
819 u_char hour1, hour2;
820 /* u_char wday; */
821 u_char day1, day2;
822 u_char mon1, mon2;
823 u_char year1, year2;
824 struct rtclock3000 *rt;
825
826 rt = clockaddr;
827 /*
828 * there seem to be problems with the bitfield addressing
829 * currently used..
830 */
831
832 if (! rt)
833 return 0;
834
835 /* prepare values to be written to clock */
836 day = tim / SECDAY;
837 hms = tim % SECDAY;
838
839 hour2 = hms / 3600;
840 hour1 = hour2 / 10;
841 hour2 %= 10;
842
843 min2 = (hms % 3600) / 60;
844 min1 = min2 / 10;
845 min2 %= 10;
846
847
848 sec2 = (hms % 3600) % 60;
849 sec1 = sec2 / 10;
850 sec2 %= 10;
851
852 /* Number of years in days */
853 for (i = STARTOFTIME - 1900; day >= days_in_year(i); i++)
854 day -= days_in_year(i);
855 year1 = i / 10;
856 year2 = i % 10;
857
858 /* Number of months in days left */
859 if (leapyear(i))
860 days_in_month(FEBRUARY) = 29;
861 for (i = 1; day >= days_in_month(i); i++)
862 day -= days_in_month(i);
863 days_in_month(FEBRUARY) = 28;
864
865 mon1 = i / 10;
866 mon2 = i % 10;
867
868 /* Days are what is left over (+1) from all that. */
869 day ++;
870 day1 = day / 10;
871 day2 = day % 10;
872
873 rt->control1 = A3CONTROL1_HOLD_CLOCK;
874 rt->second1 = sec1;
875 rt->second2 = sec2;
876 rt->minute1 = min1;
877 rt->minute2 = min2;
878 rt->hour1 = hour1;
879 rt->hour2 = hour2;
880 /* rt->weekday = wday; */
881 rt->day1 = day1;
882 rt->day2 = day2;
883 rt->month1 = mon1;
884 rt->month2 = mon2;
885 rt->year1 = year1;
886 rt->year2 = year2;
887 rt->control1 = A3CONTROL1_FREE_CLOCK;
888
889 return 1;
890 }
891
892 long
893 a2gettod()
894 {
895 struct rtclock2000 *rt;
896 int i, year, month, day, hour, min, sec;
897 u_long tmp;
898
899 rt = clockaddr;
900
901 /*
902 * hold clock
903 */
904 rt->control1 |= A2CONTROL1_HOLD;
905 i = 0x1000;
906 while (rt->control1 & A2CONTROL1_BUSY && i--)
907 ;
908 if (rt->control1 & A2CONTROL1_BUSY)
909 return (0); /* Give up and say it's not there */
910
911 /*
912 * read it
913 */
914 sec = rt->second1 * 10 + rt->second2;
915 min = rt->minute1 * 10 + rt->minute2;
916 hour = (rt->hour1 & 3) * 10 + rt->hour2;
917 day = rt->day1 * 10 + rt->day2;
918 month = rt->month1 * 10 + rt->month2;
919 year = rt->year1 * 10 + rt->year2 + 1900;
920
921 if ((rt->control3 & A2CONTROL3_24HMODE) == 0) {
922 if ((rt->hour1 & A2HOUR1_PM) == 0 && hour == 12)
923 hour = 0;
924 else if ((rt->hour1 & A2HOUR1_PM) && hour != 12)
925 hour += 12;
926 }
927
928 /*
929 * release the clock
930 */
931 rt->control1 &= ~A2CONTROL1_HOLD;
932
933 if (range_test(hour, 0, 23))
934 return(0);
935 if (range_test(day, 1, 31))
936 return(0);
937 if (range_test(month, 1, 12))
938 return(0);
939 if (range_test(year, STARTOFTIME, 2000))
940 return(0);
941
942 tmp = 0;
943
944 for (i = STARTOFTIME; i < year; i++)
945 tmp += days_in_year(i);
946 if (leapyear(year) && month > FEBRUARY)
947 tmp++;
948
949 for (i = 1; i < month; i++)
950 tmp += days_in_month(i);
951
952 tmp += (day - 1);
953 tmp = ((tmp * 24 + hour) * 60 + min) * 60 + sec;
954
955 return(tmp);
956 }
957
958 /*
959 * there is some question as to whether this works
960 * I guess
961 */
962 int
963 a2settod(tim)
964 long tim;
965 {
966
967 int i;
968 long hms, day;
969 u_char sec1, sec2;
970 u_char min1, min2;
971 u_char hour1, hour2;
972 u_char day1, day2;
973 u_char mon1, mon2;
974 u_char year1, year2;
975 struct rtclock2000 *rt;
976
977 rt = clockaddr;
978 /*
979 * there seem to be problems with the bitfield addressing
980 * currently used..
981 *
982 * XXX Check out the above where we (hour1 & 3)
983 */
984 if (! rt)
985 return 0;
986
987 /* prepare values to be written to clock */
988 day = tim / SECDAY;
989 hms = tim % SECDAY;
990
991 hour2 = hms / 3600;
992 hour1 = hour2 / 10;
993 hour2 %= 10;
994
995 min2 = (hms % 3600) / 60;
996 min1 = min2 / 10;
997 min2 %= 10;
998
999
1000 sec2 = (hms % 3600) % 60;
1001 sec1 = sec2 / 10;
1002 sec2 %= 10;
1003
1004 /* Number of years in days */
1005 for (i = STARTOFTIME - 1900; day >= days_in_year(i); i++)
1006 day -= days_in_year(i);
1007 year1 = i / 10;
1008 year2 = i % 10;
1009
1010 /* Number of months in days left */
1011 if (leapyear(i))
1012 days_in_month(FEBRUARY) = 29;
1013 for (i = 1; day >= days_in_month(i); i++)
1014 day -= days_in_month(i);
1015 days_in_month(FEBRUARY) = 28;
1016
1017 mon1 = i / 10;
1018 mon2 = i % 10;
1019
1020 /* Days are what is left over (+1) from all that. */
1021 day ++;
1022 day1 = day / 10;
1023 day2 = day % 10;
1024
1025 /*
1026 * XXXX spin wait as with reading???
1027 */
1028 rt->control1 |= A2CONTROL1_HOLD;
1029 rt->second1 = sec1;
1030 rt->second2 = sec2;
1031 rt->minute1 = min1;
1032 rt->minute2 = min2;
1033 rt->hour1 = hour1;
1034 rt->hour2 = hour2;
1035 rt->day1 = day1;
1036 rt->day2 = day2;
1037 rt->month1 = mon1;
1038 rt->month2 = mon2;
1039 rt->year1 = year1;
1040 rt->year2 = year2;
1041 rt->control2 &= ~A2CONTROL1_HOLD;
1042
1043 return 1;
1044 }
1045