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