cleanups
[unix-history] / usr / src / sys / vax / vax / ka630.c
CommitLineData
9d2503c6 1/*
46072663 2 * @(#)ka630.c 7.2 (Berkeley) %G%
9d2503c6
BK
3 */
4#if defined(VAX630)
5/* ka630.c routines for the ka630 clock chip... */
6#include "param.h"
7#include "time.h"
8#include "kernel.h"
9#include "vmmac.h"
10
11#include "mtpr.h"
12#include "cpu.h"
13#include "clock.h"
14#include "pte.h"
15#include "ka630.h"
16
17/*
18 * These two fuctions handle the tod clock
19 * This code is defunct at the end of the century.
20 * Will Unix still be here then??
21 */
22
46072663
MK
23extern struct cldevice cldevice;
24extern struct ka630cpu ka630cpu;
9d2503c6
BK
25
26short dayyr[12] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, };
27/* Starts the tod clock. Called from clkstart... */
28ka630tod(base)
29 time_t base;
30{
31 register int tmp1, tmp2;
9d2503c6
BK
32 register struct cldevice *claddr = &cldevice;
33 struct ka630cpu *ka630addr = &ka630cpu;
34
35 /* Enable system page for registers */
46072663
MK
36 ioaccess(0x200b8000, &Clockmap[0], sizeof(struct cldevice));
37 ioaccess(0x20080000, &Ka630map[0], sizeof(struct ka630cpu));
38
9d2503c6
BK
39 /*
40 * Clear restart and boot in progress flags in the CPMBX. This has
41 * nothing to do with the clock except that it the CPMBX reg. is a
42 * byte in the clock's ram.
43 */
44 claddr->cpmbx=(u_short)((claddr->cpmbx&KA630CLK_LANG)|KA630CLK_REBOOT);
45 /*
46 * Enable memory parity error detection. again nothing to do with the
47 * tod clock except for being a convenient place.
48 */
49 ka630addr->ka630_mser = KA630MSER_PAREN;
50 claddr->csr1 = KA630CLK_SET;
51 while ((claddr->csr0 & KA630CLK_UIP) != 0)
52 ;
53 /* If the clock is valid, use it. */
54 if ((claddr->csr3 & KA630CLK_VRT) != 0 &&
55 (claddr->csr1 & KA630CLK_ENABLE) == KA630CLK_ENABLE) {
56 /* Convert yr,mon,day,hr,min,sec to sec past Jan.1, 1970. */
57 tmp2 = 0;
58 for (tmp1 = 70; tmp1 < claddr->yr; tmp1++) {
59 tmp2 += 365;
60 /* I just luv leap years... */
61 if (LEAPYEAR(tmp1))
62 tmp2++;
63 }
64 tmp2 += (dayyr[claddr->mon-1]+claddr->day-1);
65 if (LEAPYEAR(claddr->yr) && claddr->mon > 2)
66 tmp2++;
67 /* Finally got days past Jan. 1,1970. the rest is easy.. */
68 time.tv_sec = tmp2*SECDAY+claddr->hr*HRSEC+
69 claddr->min*MINSEC+claddr->sec;
70 tmp1 = claddr->csr2;
71 claddr->csr0 = KA630CLK_RATE;
72 claddr->csr1 = KA630CLK_ENABLE;
73 } else if (base < 5*SECYR) {
74 printf("WARNING: preposterous time in file system\n");
75 time.tv_sec = 6*SECYR+186*SECDAY+SECDAY/2;
76 ka630stod();
77 } else {
78 printf("WARNING: Time set via file system\n");
79 time.tv_sec = base;
80 ka630stod();
81 }
82}
83/* Set the time of day clock, called via. stime system call.. */
84ka630stod()
85{
86 register int tmp1, tmp3;
87 register struct cldevice *claddr = &cldevice;
88 long tmp2, tmp4;
89
90 claddr->csr1 = KA630CLK_SET;
91 while ((claddr->csr0 & KA630CLK_UIP) != 0)
92 ;
93 /* The reverse of above, sec. past Jan. 1,1970 to yr, mon... */
94 tmp2 = time.tv_sec/HRSEC;
95 tmp4 = tmp2 = tmp2/24;
96 tmp1 = 69;
97 while (tmp2 >= 0) {
98 tmp3 = tmp2;
99 tmp2 -= 365;
100 tmp1++;
101 if (LEAPYEAR(tmp1))
102 tmp2--;
103 }
104 /* Got the year... */
105 claddr->yr = tmp1;
106 tmp1 = -1;
107 do {
108 tmp2 = tmp3-dayyr[++tmp1];
109 if (LEAPYEAR(claddr->yr) && tmp1 > 1)
110 tmp2--;
111 } while (tmp2 >= 0);
112 /* Finally, got the rest... */
113 claddr->mon = tmp1;
114 claddr->day = tmp3-dayyr[tmp1-1]+1;
115 if (LEAPYEAR(claddr->yr) && tmp1 > 2)
116 claddr->day--;
117 tmp2 = time.tv_sec-(tmp4*SECDAY);
118 claddr->hr = tmp2/HRSEC;
119 tmp2 = tmp2%HRSEC;
120 claddr->min = tmp2/MINSEC;
121 tmp2 = tmp2%MINSEC;
122 claddr->sec = tmp2;
123 tmp1 = claddr->csr2;
124 tmp1 = claddr->csr3;
125 claddr->csr0 = KA630CLK_RATE;
126 claddr->csr1 = KA630CLK_ENABLE;
127}
128#endif