## diffname power/devrtc.c 1990/1204
## diff -e /dev/null /n/bootesdump/1990/1204/sys/src/9/mips/devrtc.c
0a
#include "u.h"
#include "lib.h"
#include "mem.h"
#include "dat.h"
#include "fns.h"
#include "errno.h"
#include "devtab.h"
#include "io.h"
typedef struct Rtc Rtc;
struct Rtc
{
QLock;
int sec;
int min;
int hour;
int mday;
int mon;
int year;
};
Rtc rtc;
Dirtab rtcdir[]={
"rtc", {1}, 0, 0600,
};
static uchar pattern[] =
{
0xc5, 0x3a, 0xa3, 0x5c, 0xc5, 0x3a, 0xa3, 0x5c
};
/*
* issue pattern recognition bits to nv ram to address the
* real time clock
*/
rtcpattern(void)
{
uchar *nv;
uchar ch;
int i, j;
nv = RTC;
/*
* read the pattern sequence pointer to reset it
*/
ch = *nv;
/*
* stuff the pattern recognition codes one bit at
* a time into *nv.
*/
for(i = 0; i < sizeof(pattern); i++){
ch = pattern[i];
for (j = 0; j < 8; j++){
*nv = ch & 0x1;
ch >>= 1;
}
}
}
void
rtcreset(void)
{
}
void
rtcinit(void)
{
}
Chan*
rtcattach(char *spec)
{
return devattach('r', spec);
}
Chan*
rtcclone(Chan *c, Chan *nc)
{
return devclone(c, nc);
}
int
rtcwalk(Chan *c, char *name)
{
if(c->qid.path != CHDIR)
return 0;
if(strcmp(name, "rtc") == 0){
c->qid.path = 1;
return 1;
}
return 0;
}
void
rtcstat(Chan *c, char *dp)
{
devstat(c, dp, rtcdir, 1, devgen);
}
Chan*
rtcopen(Chan *c, int omode)
{
c->mode = openmode(omode);
c->flag |= COPEN;
c->offset = 0;
return c;
}
void
rtccreate(Chan *c, char *name, int omode, ulong perm)
{
error(Eperm);
}
void
rtcclose(Chan *c)
{
}
#define GETBCD(o) ((clock[o]&0xf) + 10*(clock[o]>>4))
long
rtcread(Chan *c, void *buf, long n)
{
int i,j;
uchar ch;
uchar *nv;
uchar clock[8];
char atime[64];
if(c->offset!=0)
error(Ebadarg);
nv = RTC;
/*
* set up the pattern for the clock
*/
qlock(&rtc);
rtcpattern();
/*
* read out the clock one bit at a time
*/
for (i = 0; i < 8; i++){
ch = 0;
for (j = 0; j < 8; j++)
ch |= ((*nv & 0x1) << j);
clock[i] = ch;
}
qunlock(&rtc);
rtc.sec = GETBCD(1);
rtc.min = GETBCD(2);
rtc.hour = GETBCD(3);
rtc.mday = GETBCD(5);
rtc.mon = GETBCD(6);
rtc.year = GETBCD(7);
/*
* the world starts jan 1 1970
*/
if(rtc.year < 70)
rtc.year += 2000;
else
rtc.year += 1900;
sprint(atime, "%.2d:%.2d:%.2d %d/%d/%d", rtc.hour, rtc.min, rtc.sec,
rtc.mon, rtc.mday, rtc.year);
i = strlen(atime);
if(c->offset >= i)
return 0;
if(c->offset + n > i)
n = i - c->offset;
strncpy(buf, &atime[c->offset], n);
return n;
}
static int perm[] =
{
3, 2, 1, 6, 5, 7
};
long
rtcwrite(Chan *c, void *buf, long n)
{
int i,j;
uchar ch;
uchar clock[8];
uchar *nv;
char *cp;
if(c->offset!=0)
error(Ebadarg);
/*
* parse (most any separator will do)
*/
ch = 0;
j = 0;
clock[0] = clock[4] = 0;
cp = buf;
for(i = 0; i < n; i++){
switch(*cp){
case ':': case ' ': case '\t': case '/': case '-':
clock[perm[j++]] = ch;
ch = 0;
break;
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
ch = (ch<<4) | (*cp - '0');
break;
default:
error(Ebadarg);
}
cp++;
}
clock[perm[j++]] = ch;
if(j != 6)
error(Ebadarg);
/*
* set up the pattern for the clock
*/
qlock(&rtc);
rtcpattern();
/*
* write the clock one bit at a time
*/
nv = RTC;
for (i = 0; i < 8; i++){
ch = clock[i];
for (j = 0; j < 8; j++){
*nv = ch & 1;
ch >>= 1;
}
}
qunlock(&rtc);
return n;
}
void
rtcremove(Chan *c)
{
error(Eperm);
}
void
rtcwstat(Chan *c, char *dp)
{
error(Eperm);
}
#define SEC2MIN 60
#define SEC2HOUR (60*SEC2MIN)
#define SEC2DAY (24*SEC2HOUR)
#define SEC2YR (365*SEC2DAY)
static char dmsize[12] =
{
31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
};
/*
* compute seconds since Jan 1 1970
*/
ulong
rtc2sec(void)
{
ulong secs;
int i;
for(i = 1970; i < rtc.year; i++)
secs += dysize(i);
for(i = 0; i < rtc.mon-1; i++)
secs += dmsize[i] * SEC2DAY;
if(dysize(rtc.year)==366 && rtc.mon>2)
secs += SEC2DAY;
secs += (rtc.mday-1) * SEC2DAY;
secs += rtc.hour * SEC2HOUR;
secs += rtc.min * SEC2MIN;
secs += rtc.sec;
return secs;
}
/*
* compute rtc from seconds since Jan 1 1970
*/
sec2rtc(ulong secs)
{
int d0, d1;
long hms, day;
/*
* break initial number into days
*/
hms = secs % SEC2DAY;
day = secs / SEC2DAY;
if(hms < 0) {
hms += SEC2DAY;
day -= 1;
}
/*
* generate hours:minutes:seconds
*/
rtc.sec = hms % SEC2MIN;
d1 = hms / SEC2MIN;
rtc.min = d1 % SEC2MIN;
d1 /= SEC2MIN;
rtc.hour = d1;
/*
* year number
*/
if(day >= 0)
for(d1 = 70; day >= dysize(d1); d1++)
day -= dysize(d1);
else
for (d1 = 70; day < 0; d1--)
day += dysize(d1-1);
rtc.year = d1;
/*
* generate month
*/
if(dysize(d1) == 366)
dmsize[1] = 29;
for(d1 = 0; d0 >= dmsize[d1]; d1++)
d0 -= dmsize[d1];
dmsize[1] = 28;
rtc.mday = d0 + 1;
rtc.mon = d1;
return;
}
/*
* days to year
*/
static
dysize(int y)
{
if((y%4) == 0)
return 366;
return 365;
}
.
## diffname power/devrtc.c 1990/12041
## diff -e /n/bootesdump/1990/1204/sys/src/9/mips/devrtc.c /n/bootesdump/1990/12041/sys/src/9/mips/devrtc.c
340,352d
331,337c
d2m = yrsize(rtc->year);
for(d = 1; day >= d2m[d]; d++)
day -= d2m[d];
rtc->mday = day + 1;
rtc->mon = d;
.
324,326c
for (d = 1970; day < 0; d--)
day += *yrsize(d-1);
rtc->year = d;
.
321,322c
for(d = 1970; day >= *yrsize(d); d++)
day -= *yrsize(d);
.
311,315c
rtc->sec = hms % 60;
d = hms / 60;
rtc->min = d % 60;
d /= 60;
rtc->hour = d;
.
296a
int *d2m;
.
295c
int d;
.
293c
void
sec2rtc(ulong secs, Rtc *rtc)
.
277,286c
secs = 0;
/*
* seconds per year
*/
for(i = 1970; i < rtc->year; i++){
d2m = yrsize(i);
secs += d2m[0] * SEC2DAY;
}
/*
* seconds per month
*/
d2m = yrsize(rtc->year);
for(i = 1; i < rtc->mon; i++)
secs += d2m[i] * SEC2DAY;
secs += (rtc->mday-1) * SEC2DAY;
secs += rtc->hour * SEC2HOUR;
secs += rtc->min * SEC2MIN;
secs += rtc->sec;
.
275a
int *d2m;
.
272c
rtc2sec(Rtc *rtc)
.
268a
* return the days/month for the given year
*/
int *
yrsize(int yr)
{
if((yr % 4) == 0)
return ldmsize;
else
return dmsize;
}
/*
.
266a
static int ldmsize[] =
{
366, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
};
.
265c
365, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
.
263c
/*
* days per month plus days/year
*/
static int dmsize[] =
.
258,261c
#define SEC2MIN 60L
#define SEC2HOUR (60L*SEC2MIN)
#define SEC2DAY (24L*SEC2HOUR)
.
242c
qunlock(&rtclock);
.
235,236c
for (i = 0; i < Nbcd; i++){
ch = bcdclock[i];
.
228c
qlock(&rtclock);
.
225a
* convert to bcd
*/
sec2rtc(secs, &rtc);
bcdclock[0] = bcdclock[4] = 0;
PUTBCD(rtc.sec, 1);
PUTBCD(rtc.min, 2);
PUTBCD(rtc.hour, 3);
PUTBCD(rtc.mday, 5);
PUTBCD(rtc.mon, 6);
PUTBCD(rtc.year, 7);
/*
.
221,223c
secs = strtoul(cp, 0, 0);
.
212,218d
202,210c
cp = ep = buf;
ep += n;
while(cp < ep){
if(*cp>='0' && *cp<='9')
.
200c
* read the time
.
194c
char *cp, *ep;
.
192c
uchar bcdclock[Nbcd];
.
189a
Rtc rtc;
ulong secs;
.
182,185c
#define PUTBCD(n,o) bcdclock[o] = (n % 10) | (((n / 10) % 10)<<4)
.
169,179c
return readnum(c->offset, buf, n, rtc2sec(&rtc), 12);
.
153c
qunlock(&rtclock);
/*
* see if the clock oscillator is on
*/
if(bcdclock[4] & 0x20)
return 0; /* nope, time is bogus */
/*
* convert from BCD
*/
.
151c
bcdclock[i] = ch;
.
147c
for (i = 0; i < Nbcd; i++){
.
141c
qlock(&rtclock);
.
134,135d
132a
Rtc rtc;
.
131c
uchar bcdclock[Nbcd];
.
123c
#define GETBCD(o) ((bcdclock[o]&0xf) + 10*(bcdclock[o]>>4))
.
105a
if(c->qid.path==1 && (omode&(OWRITE|OTRUNC))){
if(strcmp(u->p->pgrp->user, "bootes")) /* BUG */
error(Eperm);
}
.
54c
for(i = 0; i < Nbcd; i++){
.
32a
ulong rtc2sec(Rtc*);
void sec2rtc(ulong, Rtc*);
int *yrsize(int);
.
24c
QLock rtclock; /* mutex on clock operations */
static Dirtab rtcdir[]={
.
22d
14d
11c
typedef struct Rtc Rtc;
enum {
Nbcd = 8 /* number of bcd bytes in the clock */
};
.
## diffname power/devrtc.c 1990/1212
## diff -e /n/bootesdump/1990/12041/sys/src/9/mips/devrtc.c /n/bootesdump/1990/1212/sys/src/9/mips/devrtc.c
146a
if(c->qid.path & CHDIR)
return devdirread(c, buf, n, rtcdir, 1, devgen);
.
30c
"rtc", {1, 0}, 0, 0600,
.
## diffname power/devrtc.c 1990/1214
## diff -e /n/bootesdump/1990/1212/sys/src/9/mips/devrtc.c /n/bootesdump/1990/1214/sys/src/9/mips/devrtc.c
57a
USED(ch);
.
## diffname power/devrtc.c 1991/0411
## diff -e /n/bootesdump/1991/0201/sys/src/9/mips/devrtc.c /n/bootesdump/1991/0411/sys/src/9/power/devrtc.c
211c
if(offset!=0)
.
201c
rtcwrite(Chan *c, void *buf, long n, ulong offset)
.
195c
return readnum(offset, buf, n, rtc2sec(&rtc), 12);
.
140c
rtcread(Chan *c, void *buf, long n, ulong offset)
.
## diffname power/devrtc.c 1991/0419
## diff -e /n/bootesdump/1991/0411/sys/src/9/power/devrtc.c /n/bootesdump/1991/0419/sys/src/9/power/devrtc.c
106a
Chan*
rtcclwalk(Chan *c, char *name)
{
return devclwalk(c, name);
}
.
## diffname power/devrtc.c 1991/0427
## diff -e /n/bootesdump/1991/0419/sys/src/9/power/devrtc.c /n/bootesdump/1991/0427/sys/src/9/power/devrtc.c
107,112d
## diffname power/devrtc.c 1991/0621
## diff -e /n/bootesdump/1991/0427/sys/src/9/power/devrtc.c /n/bootesdump/1991/0621/sys/src/9/power/devrtc.c
195c
long
rtcread(Chan *c, void *buf, long n, ulong offset)
{
ulong t, ot;
if(c->qid.path & CHDIR)
return devdirread(c, buf, n, rtcdir, NRTC, devgen);
qlock(&rtclock);
t = rtctime();
do{
ot = t;
t = rtctime(); /* make sure there's no skew */
}while(t != ot);
qunlock(&rtclock);
n = readnum(offset, buf, n, t, 12);
return n;
.
193a
return rtc2sec(&rtc);
}
.
157d
149,151d
140c
rtctime(void)
.
120,123c
return devopen(c, omode, rtcdir, NRTC, devgen);
.
110c
devstat(c, dp, rtcdir, NRTC, devgen);
.
98,104c
return devwalk(c, name, rtcdir, NRTC, devgen);
.
28a
#define NRTC 1
.
## diffname power/devrtc.c 1991/0924
## diff -e /n/bootesdump/1991/0621/sys/src/9/power/devrtc.c /n/bootesdump/1991/0924/sys/src/9/power/devrtc.c
262,276d
254c
nv = &((Nvram*)NVRAM)[NVRTC].val;
.
248d
238,243c
PUTBCD(rtc->sec, 1);
PUTBCD(rtc->min, 2);
PUTBCD(rtc->hour, 3);
PUTBCD(rtc->mday, 5);
PUTBCD(rtc->mon, 6);
PUTBCD(rtc->year, 7);
.
218,236d
216d
210,212c
int i, j;
.
207,208c
void
setrtc(Rtc *rtc)
.
185,204d
157d
141c
nv = &((Nvram*)NVRAM)[NVRTC].val;
.
128a
long
rtcread(Chan *c, void *buf, long n, ulong offset)
{
Nvram *nv;
char *p;
ulong t, ot;
int i;
if(c->qid.path & CHDIR)
return devdirread(c, buf, n, rtcdir, NRTC, devgen);
switch(c->qid.path){
case Qrtc:
qlock(&rtclock);
t = rtctime();
do{
ot = t;
t = rtctime(); /* make sure there's no skew */
}while(t != ot);
qunlock(&rtclock);
n = readnum(offset, buf, n, t, 12);
return n;
case Qnvram:
if(offset > NVLEN)
return 0;
if(n > NVLEN - offset)
n = NVLEN - offset;
nv = (Nvram *)NVRAM;
p = buf;
qlock(&rtclock);
for(i = 0; i < n; i++)
p[i] = nv[i+offset].val;
qunlock(&rtclock);
return n;
}
error(Egreg);
}
long
rtcwrite(Chan *c, void *buf, long n, ulong offset)
{
Nvram *nv;
Rtc rtc;
ulong secs;
char *cp, *ep;
int i;
switch(c->qid.path){
case Qrtc:
if(offset!=0)
error(Ebadarg);
/*
* read the time
*/
cp = ep = buf;
ep += n;
while(cp < ep){
if(*cp>='0' && *cp<='9')
break;
cp++;
}
secs = strtoul(cp, 0, 0);
/*
* convert to bcd
*/
sec2rtc(secs, &rtc);
/*
* write it
*/
qlock(&rtclock);
setrtc(&rtc);
qunlock(&rtclock);
return n;
case Qnvram:
if(offset > NVLEN)
return 0;
if(n > NVLEN - offset)
n = NVLEN - offset;
nv = (Nvram *)NVRAM;
qlock(&rtclock);
for(i = 0; i < n; i++)
nv[i+offset].val = ((char*)buf)[i];
nvcksum();
qunlock(&rtclock);
return n;
}
error(Egreg);
}
void
rtcremove(Chan *c)
{
error(Eperm);
}
void
rtcwstat(Chan *c, char *dp)
{
error(Eperm);
}
void
nvcksum(void)
{
Nvram *nv;
uchar cksum;
int i;
/*
* Seed the checksum so all-zeroes (all-ones) nvram doesn't have a zero
* (all-ones) checksum.
*/
cksum = 0xa5;
nv = (Nvram *)NVRAM;
for(i = 0; i < NVLEN; i++){
cksum ^= nv[i].val;
cksum = (cksum << 1) | ((cksum >> 7) & 1);
}
nv[NVCKSUM].val = cksum;
}
/*
* issue pattern recognition bits to nv ram to address the
* real time clock
*/
void
rtcpattern(void)
{
uchar *nv;
uchar ch;
int i, j;
nv = &((Nvram*)NVRAM)[NVRTC].val;
/*
* read the pattern sequence pointer to reset it
*/
ch = *nv;
USED(ch);
/*
* stuff the pattern recognition codes one bit at
* a time into *nv.
*/
for(i = 0; i < Nbcd; i++){
ch = pattern[i];
for (j = 0; j < 8; j++){
*nv = ch & 0x1;
ch >>= 1;
}
}
}
.
43,73d
39,41c
ulong rtc2sec(Rtc*);
void sec2rtc(ulong, Rtc*);
int *yrsize(int);
void setrtc(Rtc*);
long rtctime(void);
void nvcksum(void);
.
32a
#define NRTC (sizeof(rtcdir)/sizeof(rtcdir[0]))
.
31c
"rtc", {Qrtc, 0}, 0, 0644,
/* "nvram", {Qnvram, 0}, 0, 0644,/**/
.
29d
27c
QLock rtclock; /* mutex on nvram operations */
.
16a
struct Nvram{
uchar val;
uchar pad[7];
};
.
13,14c
/*
* length of nvram is 2048
* the last addresses are reserved by convention for
* a checksum and setting the real time clock
*/
enum{
NVLEN = 2046,
NVCKSUM = 2046,
NVRTC = 2047,
Nbcd = 8, /* number of bcd bytes in the clock */
Qrtc = 1,
Qnvram,
.
11a
typedef struct Nvram Nvram;
.
## diffname power/devrtc.c 1991/10302
## diff -e /n/bootesdump/1991/0924/sys/src/9/power/devrtc.c /n/bootesdump/1991/10302/sys/src/9/power/devrtc.c
353c
for(i = 0; i < Nbcd; i++){
.
293c
for(i = 0; i < Nbcd; i++){
.
100,103c
if(c->qid.path==Qrtc && (omode&(OWRITE|OTRUNC)))
if(strcmp(u->p->pgrp->user, "bootes") != 0) /* BUG */
error(Eperm);
if(c->qid.path == Qnvram)
if(strcmp(u->p->pgrp->user, "bootes") != 0) /* BUG */
error(Eperm);
.
47c
"nvram", {Qnvram, 0}, 0, 0644,
.
## diffname power/devrtc.c 1991/1105
## diff -e /n/bootesdump/1991/10302/sys/src/9/power/devrtc.c /n/bootesdump/1991/1105/sys/src/9/power/devrtc.c
104c
if(strcmp(u->p->user, "bootes") != 0) /* BUG */
.
101c
if(strcmp(u->p->user, "bootes") != 0) /* BUG */
.
## diffname power/devrtc.c 1991/1112
## diff -e /n/bootesdump/1991/1105/sys/src/9/power/devrtc.c /n/bootesdump/1991/1112/sys/src/9/power/devrtc.c
100,105d
47c
"nvram", {Qnvram, 0}, 0, 0600,
.
## diffname power/devrtc.c 1991/1214
## diff -e /n/bootesdump/1991/1112/sys/src/9/power/devrtc.c /n/bootesdump/1991/1214/sys/src/9/power/devrtc.c
211a
USED(c);
.
205a
USED(c);
.
111a
USED(c);
.
105a
USED(c);
.
## diffname power/devrtc.c 1992/0111
## diff -e /n/bootesdump/1991/1214/sys/src/9/power/devrtc.c /n/bootesdump/1992/0111/sys/src/9/power/devrtc.c
6c
#include "../port/error.h"
.
## diffname power/devrtc.c 1992/0321
## diff -e /n/bootesdump/1992/0111/sys/src/9/power/devrtc.c /n/bootesdump/1992/0321/sys/src/9/power/devrtc.c
2c
#include "../port/lib.h"
.
## diffname power/devrtc.c 1992/0520
## diff -e /n/bootesdump/1992/0321/sys/src/9/power/devrtc.c /n/bootesdump/1992/0520/sys/src/9/power/devrtc.c
202a
return -1; /* never reached */
.
151a
return -1; /* never reached */
.
## diffname power/devrtc.c 1992/0711
## diff -e /n/bootesdump/1992/0520/sys/src/9/power/devrtc.c /n/bootesdump/1992/0711/sys/src/9/power/devrtc.c
282d
217a
USED(dp);
.
106a
USED(name);
USED(omode);
USED(perm);
.
## diffname power/devrtc.c 1992/0819
## diff -e /n/bootesdump/1992/0711/sys/src/9/power/devrtc.c /n/bootesdump/1992/0819/sys/src/9/power/devrtc.c
99a
omode = openmode(omode);
switch(c->qid.path){
case Qrtc:
if(omode == OREAD)
break;
/* fall through */
case Qnvram:
if(strcmp(u->p->user, eve)!=0 || !cpuserver)
error(Eperm);
}
.
## diffname power/devrtc.c 1992/0902
## diff -e /n/bootesdump/1992/0819/sys/src/9/power/devrtc.c /n/bootesdump/1992/0902/sys/src/9/power/devrtc.c
103,105c
if(strcmp(u->p->user, eve)!=0 && omode!=OREAD)
error(Eperm);
break;
.
## diffname power/devrtc.c 1993/0501
## diff -e /n/bootesdump/1992/0902/sys/src/9/power/devrtc.c /n/fornaxdump/1993/0501/sys/src/brazil/power/devrtc.c
107c
if(strcmp(up->user, eve)!=0 || !cpuserver)
.
103c
if(strcmp(up->user, eve)!=0 && omode!=OREAD)
.
## diffname power/devrtc.c 1995/0108
## diff -e /n/fornaxdump/1993/0501/sys/src/brazil/power/devrtc.c /n/fornaxdump/1995/0108/sys/src/brazil/power/devrtc.c
217a
}
long
rtcbwrite(Chan *c, Block *bp, ulong offset)
{
return devbwrite(c, bp, offset);
.
167a
Block*
rtcbread(Chan *c, long n, ulong offset)
{
return devbread(c, n, offset);
}
.
## diffname power/devrtc.c 1996/0223
## diff -e /n/fornaxdump/1995/0108/sys/src/brazil/power/devrtc.c /n/fornaxdump/1996/0223/sys/src/brazil/power/devrtc.c
7d
## diffname power/devrtc.c 1997/0327 # deleted
## diff -e /n/fornaxdump/1996/0223/sys/src/brazil/power/devrtc.c /n/emeliedump/1997/0327/sys/src/brazil/power/devrtc.c
1,501d
|