1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
|
#include <sys/param.h>
#include <sys/conf.h>
#include <sys/ioctl.h>
#include <sys/proc.h>
#include <sys/tty.h>
#include <sys/uio.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/device.h>
#include <machine/cpu.h>
#include <machine/autoconf.h>
#include <mvme88k/dev/nvramreg.h>
#include <mvme88k/dev/pcctworeg.h>
struct nvramsoftc {
struct device sc_dev;
caddr_t sc_paddr;
caddr_t sc_vaddr;
int sc_size;
volatile struct clockreg *sc_clockreg;
};
int nvrammatch __P((struct device *, void *, void *));
void nvramattach __P((struct device *, struct device *, void *));
struct cfattach nvram_ca = {
sizeof(struct nvramsoftc), nvrammatch, nvramattach
};
struct cfdriver nvram_cd = {
NULL, "nvram", DV_DULL, 0
};
/* ARGSUSED */
int
nvrammatch(struct device *parent, void *self, void *aux)
{
struct confargs *ca = aux;
struct cfdata *cf = self;
caddr_t base;
int ret;
#if 0
if (cputyp != CPU_167 && cputyp != CPU_166
#ifdef CPU_187
&& cputyp != CPU_187
#endif
)
{
return 0;
}
#endif /* 0 */
if (cputyp != CPU_187) {
return 0;
}
/*
* If bus or name do not match, fail.
*/
if (ca->ca_bustype != BUS_PCCTWO ||
strcmp(cf->cf_driver->cd_name, "nvram")) {
return 0;
}
/* 3 locators base, size, ipl */
base = (caddr_t)cf->cf_loc[0];
if (badpaddr(base, 1) == -1) {
return 0;
}
/*
* Tell our parent our requirements.
*/
ca->ca_paddr = (caddr_t)cf->cf_loc[0];
ca->ca_size = NVRAMSIZE;
ca->ca_ipl = 0;
return 1;
}
/* ARGSUSED */
void
nvramattach(struct device *parent, struct device *self, void *aux)
{
struct nvramsoftc *sc = (struct nvramsoftc *)self;
struct confargs *ca = aux;
sc->sc_clockreg = (struct clockreg *)(ca->ca_vaddr + NVRAM_TOD_OFF);
printf(": MK48T08\n");
}
#if 0
/*
* Write en/dis-able clock registers. We coordinate so that several
* writers can run simultaneously.
*/
void
clk_wenable(onoff)
int onoff;
{
register int s;
register vm_prot_t prot;/* nonzero => change prot */
static int writers;
s = splhigh();
if (onoff)
prot = writers++ == 0 ? VM_PROT_READ|VM_PROT_WRITE : 0;
else
prot = --writers == 0 ? VM_PROT_READ : 0;
splx(s);
if (prot)
pmap_changeprot(pmap_kernel(), (vm_offset_t)clockreg, prot, 1);
}
#endif /* 0 */
/*
* BCD to hex and hex to BCD.
*/
#define FROMBCD(x) (((x) >> 4) * 10 + ((x) & 0xf))
#define TOBCD(x) (((x) / 10 * 16) + ((x) % 10))
#define SECDAY (24 * 60 * 60)
#define SECYR (SECDAY * 365)
#define LEAPYEAR(y) (((y) % 4 == 0) && ((y) % 100) != 0 || ((y) % 400) == 0)
/*
* This code is defunct after 2068.
* Will Unix still be here then??
*/
const short dayyr[12] =
{ 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
chiptotime(int sec, int min, int hour, int day, int mon, int year)
{
register int days, yr;
sec = FROMBCD(sec);
min = FROMBCD(min);
hour = FROMBCD(hour);
day = FROMBCD(day);
mon = FROMBCD(mon);
year = FROMBCD(year) + YEAR0;
/* simple sanity checks */
if (year < 70 || mon < 1 || mon > 12 || day < 1 || day > 31)
return (0);
days = 0;
for (yr = 70; yr < year; yr++)
days += LEAPYEAR(yr) ? 366 : 365;
days += dayyr[mon - 1] + day - 1;
if (LEAPYEAR(yr) && mon > 2)
days++;
/* now have days since Jan 1, 1970; the rest is easy... */
return (days * SECDAY + hour * 3600 + min * 60 + sec);
}
struct chiptime {
int sec;
int min;
int hour;
int wday;
int day;
int mon;
int year;
};
timetochip(struct chiptime *c)
{
register int t, t2, t3, now = time.tv_sec;
/* compute the year */
t2 = now / SECDAY;
t3 = (t2 + 2) % 7; /* day of week */
c->wday = TOBCD(t3 + 1);
t = 69;
while (t2 >= 0) { /* whittle off years */
t3 = t2;
t++;
t2 -= LEAPYEAR(t) ? 366 : 365;
}
c->year = t;
/* t3 = month + day; separate */
t = LEAPYEAR(t);
for (t2 = 1; t2 < 12; t2++)
if (t3 < dayyr[t2] + (t && t2 > 1))
break;
/* t2 is month */
c->mon = t2;
c->day = t3 - dayyr[t2 - 1] + 1;
if (t && t2 > 2)
c->day--;
/* the rest is easy */
t = now % SECDAY;
c->hour = t / 3600;
t %= 3600;
c->min = t / 60;
c->sec = t % 60;
c->sec = TOBCD(c->sec);
c->min = TOBCD(c->min);
c->hour = TOBCD(c->hour);
c->day = TOBCD(c->day);
c->mon = TOBCD(c->mon);
c->year = TOBCD(c->year - YEAR0);
}
/*
* Set up the system's time, given a `reasonable' time value.
*/
void
inittodr(time_t base)
{
register struct nvramsoftc *sc;
register volatile struct clockreg *cl;
int sec, min, hour, day, mon, year;
int badbase = 0, waszero = base == 0;
sc = (struct nvramsoftc *)nvram_cd.cd_devs[0];
cl = sc->sc_clockreg;
if (base < 5 * SECYR) {
/*
* If base is 0, assume filesystem time is just unknown
* in stead of preposterous. Don't bark.
*/
if (base != 0)
printf("WARNING: preposterous time in file system\n");
/* not going to use it anyway, if the chip is readable */
base = 21*SECYR + 186*SECDAY + SECDAY/2;
badbase = 1;
}
cl->cl_csr |= CLK_READ; /* enable read (stop time) */
sec = cl->cl_sec;
min = cl->cl_min;
hour = cl->cl_hour;
day = cl->cl_mday;
mon = cl->cl_month;
year = cl->cl_year;
cl->cl_csr &= ~CLK_READ; /* time wears on */
time.tv_sec = chiptotime(sec, min, hour, day, mon, year);
if (time.tv_sec == 0) {
printf("WARNING: bad date in battery clock");
/*
* Believe the time in the file system for lack of
* anything better, resetting the clock.
*/
time.tv_sec = base;
if (!badbase)
resettodr();
} else {
int deltat = time.tv_sec - base;
if (deltat < 0)
deltat = -deltat;
if (waszero || deltat < 2 * SECDAY)
return;
printf("WARNING: clock %s %d days",
time.tv_sec < base ? "lost" : "gained", deltat / SECDAY);
}
printf(" -- CHECK AND RESET THE DATE!\n");
}
/*
* Reset the clock based on the current time.
* Used when the current clock is preposterous, when the time is changed,
* and when rebooting. Do nothing if the time is not yet known, e.g.,
* when crashing during autoconfig.
*/
void
resettodr()
{
register struct nvramsoftc *sc;
register volatile struct clockreg *cl;
struct chiptime c;
sc = (struct nvramsoftc *)nvram_cd.cd_devs[0];
if (!time.tv_sec || (cl = sc->sc_clockreg) == NULL)
return;
timetochip(&c);
cl->cl_csr |= CLK_WRITE; /* enable write */
cl->cl_sec = c.sec;
cl->cl_min = c.min;
cl->cl_hour = c.hour;
cl->cl_wday = c.wday;
cl->cl_mday = c.day;
cl->cl_month = c.mon;
cl->cl_year = c.year;
cl->cl_csr &= ~CLK_WRITE; /* load them up */
}
|