Fix of MicroVAX clock chip code, so that time sets right after reboot.

Fix done by Ken Wellsch.
This commit is contained in:
ragge 1996-03-07 23:03:03 +00:00
parent 1c817d1c3c
commit 3f09ddf89f
1 changed files with 178 additions and 48 deletions

View File

@ -1,8 +1,11 @@
/* $NetBSD: uvaxII.c,v 1.6 1996/02/02 18:09:09 mycroft Exp $ */
/* $NetBSD: uvaxII.c,v 1.7 1996/03/07 23:03:03 ragge Exp $ */
/*-
* Copyright (c) 1988 The Regents of the University of California.
* All rights reserved.
* Copyright (c) 1994 Gordon W. Ross
* Copyright (c) 1993 Adam Glass
* Copyright (c) 1988 University of Utah.
* Copyright (c) 1982, 1988, 1990, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@ -52,8 +55,21 @@ struct uvaxIIcpu *uvaxIIcpu_ptr;
#if VAX630
struct ka630clock *ka630clk_ptr;
u_long ka630_clkread();
time_t ka630_clkread();
void ka630_clkwrite();
struct watclk {
u_short wat_sec;
u_short wat_min;
u_short wat_hour;
u_short wat_dow;
u_short wat_day;
u_short wat_month;
u_short wat_year;
};
void gmt_to_wat (time_t *tp, struct watclk *wt);
void wat_to_gmt (struct watclk *wt, time_t *tp);
#endif
/*
@ -73,8 +89,10 @@ uvaxII_conf(parent, self, aux)
case VAX_410:
strcpy(cpu_model,"MicroVAX 2000");
break;
default:
strcpy(cpu_model, "MicroVAX 78032/78132");
break;
};
strcpy(cpu_model, "MicroVAX 78032/78132");
printf(": %s\n", cpu_model);
}
@ -134,9 +152,9 @@ uvaxII_mchk(cmcf)
*/
u_long
uvaxII_gettodr(stopped_flag)
int *stopped_flag;
int *stopped_flag;
{
register u_long year_secs;
register time_t year_secs;
switch (cpu_type) {
#if VAX630
@ -153,9 +171,9 @@ uvaxII_gettodr(stopped_flag)
void
uvaxII_settodr(year_ticks)
u_long year_ticks;
time_t year_ticks;
{
register u_long year_secs;
register time_t year_secs;
year_secs = year_ticks / 100;
switch (cpu_type) {
@ -168,43 +186,46 @@ uvaxII_settodr(year_ticks)
}
#if VAX630
static short dayyr[12] = { 0,31,59,90,120,151,181,212,243,273,304,334 };
/* init system time from tod clock */
/* ARGSUSED */
u_long
time_t
ka630_clkread(stopped_flag)
int *stopped_flag;
int *stopped_flag;
{
register struct ka630clock *claddr = ka630clk_ptr;
register int days, yr;
register u_long year_secs;
struct watclk wt;
time_t year_secs;
*stopped_flag = 0;
claddr->csr1 = KA630CLK_SET;
while ((claddr->csr0 & KA630CLK_UIP) != 0)
;
wt.wat_sec = claddr->sec;
wt.wat_min = claddr->min;
wt.wat_hour = claddr->hr;
wt.wat_day = claddr->day;
wt.wat_month = claddr->mon;
wt.wat_year = claddr->yr;
/* If the clock is valid, use it. */
if ((claddr->csr3 & KA630CLK_VRT) != 0 &&
(claddr->csr1 & KA630CLK_ENABLE) == KA630CLK_ENABLE) {
/* simple sanity checks */
if (claddr->mon < 1 || claddr->mon > 12 ||
claddr->day < 1 || claddr->day > 31) {
if (wt.wat_month < 1 || wt.wat_month > 12 ||
wt.wat_day < 1 || wt.wat_day > 31) {
printf("WARNING: preposterous clock chip time.\n");
year_secs = 0;
} else {
days = dayyr[claddr->mon - 1] + claddr->day - 1;
year_secs = days * DAYSEC + claddr->hr * HRSEC +
claddr->min * MINSEC + claddr->sec;
}
claddr->yr = 70; /* any non-leap year */
#ifndef lint
{ volatile int t = claddr->csr2; } /* ??? */
#endif
} else
wat_to_gmt (&wt, &year_secs);
claddr->csr0 = KA630CLK_RATE;
claddr->csr1 = KA630CLK_ENABLE;
return (year_secs);
}
printf("WARNING: TOY clock invalid.\n");
return (0);
}
@ -212,40 +233,30 @@ ka630_clkread(stopped_flag)
/* Set the time of day clock, called via. stime system call.. */
void
ka630_clkwrite(year_secs)
u_long year_secs;
time_t year_secs;
{
register struct ka630clock *claddr = ka630clk_ptr;
register int t, t2;
struct watclk wt;
int s;
gmt_to_wat (&year_secs, &wt);
s = splhigh();
claddr->csr1 = KA630CLK_SET;
while ((claddr->csr0 & KA630CLK_UIP) != 0)
;
claddr->yr = 70; /* any non-leap year is ok */
claddr->sec = wt.wat_sec;
claddr->min = wt.wat_min;
claddr->hr = wt.wat_hour;
claddr->day = wt.wat_day;
claddr->mon = wt.wat_month;
claddr->yr = wt.wat_year;
/* t = month + day; separate */
t = year_secs % YEARSEC;
for (t2 = 1; t2 < 12; t2++)
if (t < dayyr[t2])
break;
/* t2 is month */
claddr->mon = t2;
claddr->day = t - dayyr[t2 - 1] + 1;
/* the rest is easy */
t = year_secs % DAYSEC;
claddr->hr = t / HRSEC;
t %= HRSEC;
claddr->min = t / MINSEC;
claddr->sec = t % MINSEC;
#ifndef lint
{ volatile int t = claddr->csr2; } /* ??? */
{ volatile int t = claddr->csr3; } /* ??? */
#endif
claddr->csr0 = KA630CLK_RATE;
claddr->csr1 = KA630CLK_ENABLE;
splx(s);
}
#endif
@ -293,3 +304,122 @@ uvaxII_steal_pages()
cpu_type = (((*UVAXIISID) >> 24) & 0xff) |
(cpu_type & 0xff000000);
}
#if VAX630
/*
* Generic routines to convert to or from a POSIX date
* (seconds since 1/1/1970) and yr/mo/day/hr/min/sec
* (These are derived from the sun3 clock chip code.)
*/
/*
* Machine dependent base year:
* Note: must be < 1970
*/
#define CLOCK_BASE_YEAR 1900
/* Traditional UNIX base year */
#define POSIX_BASE_YEAR 1970
#define FEBRUARY 2
#define SECDAY 86400L
#define SECYR (SECDAY * 365)
#define leapyear(year) ((year) % 4 == 0)
#define days_in_year(a) (leapyear(a) ? 366 : 365)
#define days_in_month(a) (month_days[(a) - 1])
static int month_days[12] = {
31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
};
void
gmt_to_wat(tp, wt)
time_t *tp;
struct watclk *wt;
{
register int i;
register long days, secs;
days = *tp / SECDAY;
secs = *tp % SECDAY;
/* Hours, minutes, seconds are easy */
wt->wat_hour = secs / 3600;
secs = secs % 3600;
wt->wat_min = secs / 60;
secs = secs % 60;
wt->wat_sec = secs;
/* Day of week (Note: 1/1/1970 was a Thursday) */
wt->wat_dow = (days + 4) % 7;
/* Number of years in days */
i = POSIX_BASE_YEAR;
while (days >= days_in_year(i)) {
days -= days_in_year(i);
i++;
}
wt->wat_year = i - CLOCK_BASE_YEAR;
/* Number of months in days left */
if (leapyear(i))
days_in_month(FEBRUARY) = 29;
for (i = 1; days >= days_in_month(i); i++)
days -= days_in_month(i);
days_in_month(FEBRUARY) = 28;
wt->wat_month = i;
/* Days are what is left over (+1) from all that. */
wt->wat_day = days + 1;
}
void
wat_to_gmt(wt, tp)
time_t *tp;
struct watclk *wt;
{
register int i;
register long tmp;
int year;
/*
* Hours are different for some reason. Makes no sense really.
*/
tmp = 0;
if (wt->wat_hour >= 24) goto out;
if (wt->wat_day > 31) goto out;
if (wt->wat_month > 12) goto out;
year = wt->wat_year + CLOCK_BASE_YEAR;
/*
* Compute days since start of time
* First from years, then from months.
*/
for (i = POSIX_BASE_YEAR; i < year; i++)
tmp += days_in_year(i);
if (leapyear(year) && wt->wat_month > FEBRUARY)
tmp++;
/* Months */
for (i = 1; i < wt->wat_month; i++)
tmp += days_in_month(i);
tmp += (wt->wat_day - 1);
/* Now do hours */
tmp = tmp * 24 + wt->wat_hour;
/* Now do minutes */
tmp = tmp * 60 + wt->wat_min;
/* Now do seconds */
tmp = tmp * 60 + wt->wat_sec;
out:
*tp = tmp;
}
#endif /* VAX630 */