| plan 9 kernel history: overview | file list | diff list |
1997/0404/carrera/devrtc.c (diff list | history)
| carrera/devrtc.c on 1993/0916 | ||
| 1993/0916 | #include "u.h" #include "../port/lib.h" #include "mem.h" #include "dat.h" #include "fns.h" #include "../port/error.h" #include "io.h" /* * real time clock and non-volatile ram */ typedef struct Rtc Rtc; struct Rtc { int sec; int min; int hour; int mday; int mon; int year; }; | |
| 1995/0108 | enum { | |
| 1993/0916 | Qrtc = 1, Qnvram, Seconds= 0x00, Minutes= 0x02, Hours= 0x04, Mday= 0x07, Month= 0x08, Year= 0x09, Status= 0x0A, | |
| 1997/0403 | StatusB= 0x0B, StatusC= 0x0C, StatusD= 0x0D, | |
| 1993/0916 | ||
| 1997/0404 | Update= 0x80, | |
| 1993/0916 | Nvsize = 4096, | |
| 1997/0403 | Nclock= 6, | |
| 1993/0916 | }; | |
| 1997/0404 | #define GETBCD(v) (((v) & 0x0F) + 10*((v)>>4)) #define PUTBCD(v) ((v) % 10)|((((v)/10) % 10)<<4) | |
| 1993/0916 | Dirtab rtcdir[]={ "nvram", {Qnvram, 0}, Nvsize, 0664, "rtc", {Qrtc, 0}, 0, 0664, }; | |
| 1997/0327 | static ulong rtc2sec(Rtc*); static void sec2rtc(ulong, Rtc*); | |
| 1993/0916 | ||
| 1997/0404 | static int isbinary; static Lock rtclock; | |
| 1997/0403 | static void | |
| 1997/0404 | rtcreset(void) | |
| 1997/0403 | { int i, x; uchar r; for(i = 0; i < 10000; i++){ x = (*(uchar*)Rtcindex)&~0x7f; *(uchar*)Rtcindex = x|Status; r = *(uchar*)Rtcdata; if(r & 0x80) continue; *(uchar*)Rtcindex = x|StatusB; | |
| 1997/0404 | isbinary = *(uchar*)Rtcdata & 0x04; | |
| 1997/0403 | break; } } | |
| 1997/0327 | static Chan* | |
| 1993/0916 | rtcattach(char *spec) { return devattach('r', spec); } | |
| 1997/0327 | static int | |
| 1993/0916 | rtcwalk(Chan *c, char *name) { | |
| 1997/0327 | return devwalk(c, name, rtcdir, nelem(rtcdir), devgen); | |
| 1993/0916 | } | |
| 1997/0327 | static void | |
| 1993/0916 | rtcstat(Chan *c, char *dp) { | |
| 1997/0327 | devstat(c, dp, rtcdir, nelem(rtcdir), devgen); | |
| 1993/0916 | } | |
| 1997/0327 | static Chan* | |
| 1993/0916 | rtcopen(Chan *c, int omode) { omode = openmode(omode); switch(c->qid.path){ case Qrtc: if(strcmp(up->user, eve)!=0 && omode!=OREAD) error(Eperm); break; case Qnvram: if(strcmp(up->user, eve)!=0) error(Eperm); } | |
| 1997/0327 | return devopen(c, omode, rtcdir, nelem(rtcdir), devgen); | |
| 1993/0916 | } | |
| 1997/0327 | static void | |
| 1993/0916 | rtcclose(Chan *c) { USED(c); } | |
| 1997/0401 | static long _rtctime(void) | |
| 1993/0916 | { Rtc rtc; | |
| 1997/0401 | int i, x; | |
| 1997/0404 | uchar r; | |
| 1993/0916 | ||
| 1997/0403 | /* don't do the read until the clock is no longer busy */ | |
| 1997/0401 | for(i = 0; i < 10000; i++){ x = (*(uchar*)Rtcindex)&~0x7f; *(uchar*)Rtcindex = x|Status; | |
| 1997/0403 | r = *(uchar*)Rtcdata; | |
| 1997/0404 | if(r & Update) | |
| 1997/0401 | continue; | |
| 1993/0916 | ||
| 1997/0403 | /* read clock values */ *(uchar*)Rtcindex = x|Seconds; | |
| 1997/0404 | rtc.sec = *(uchar*)Rtcdata; | |
| 1997/0403 | *(uchar*)Rtcindex = x|Minutes; | |
| 1997/0404 | rtc.min = *(uchar*)Rtcdata; | |
| 1997/0403 | *(uchar*)Rtcindex = x|Hours; | |
| 1997/0404 | rtc.hour = *(uchar*)Rtcdata; | |
| 1997/0403 | *(uchar*)Rtcindex = x|Mday; | |
| 1997/0404 | rtc.mday = *(uchar*)Rtcdata; | |
| 1997/0403 | *(uchar*)Rtcindex = x|Month; | |
| 1997/0404 | rtc.mon = *(uchar*)Rtcdata; | |
| 1997/0403 | *(uchar*)Rtcindex = x|Year; | |
| 1997/0404 | rtc.year = *(uchar*)Rtcdata; | |
| 1997/0401 | *(uchar*)Rtcindex = x|Status; | |
| 1997/0403 | r = *(uchar*)Rtcdata; | |
| 1997/0404 | if((r & Update) == 0) | |
| 1997/0401 | break; } | |
| 1997/0404 | if(!isbinary){ | |
| 1997/0403 | /* * convert from BCD */ | |
| 1997/0404 | rtc.sec = GETBCD(rtc.sec); rtc.min = GETBCD(rtc.min); rtc.hour = GETBCD(rtc.hour); rtc.mday = GETBCD(rtc.mday); rtc.mon = GETBCD(rtc.mon); rtc.year = GETBCD(rtc.year); | |
| 1997/0403 | } /* * the world starts jan 1 1970 */ rtc.year += 1970; | |
| 1993/0916 | return rtc2sec(&rtc); } | |
| 1997/0401 | long rtctime(void) { int i; long t, ot; | |
| 1997/0404 | ilock(&rtclock); | |
| 1997/0401 | /* loop till we get two reads in a row the same */ t = _rtctime(); for(i = 0; i < 100; i++){ ot = t; t = _rtctime(); if(ot == t) break; } | |
| 1997/0404 | iunlock(&rtclock); | |
| 1997/0401 | return t; } | |
| 1997/0327 | static long | |
| 1993/0916 | rtcread(Chan *c, void *buf, long n, ulong offset) { uchar *f, *to, *e; if(c->qid.path & CHDIR) | |
| 1997/0327 | return devdirread(c, buf, n, rtcdir, nelem(rtcdir), devgen); | |
| 1993/0916 | switch(c->qid.path){ case Qrtc: | |
| 1997/0404 | return readnum(offset, buf, n, rtctime(), 12); | |
| 1993/0916 | case Qnvram: if(offset > Nvsize) return -1; if(offset + n > Nvsize) n = Nvsize - offset; f = (uchar*)Nvram+offset; to = buf; e = f + n; while(f < e) *to++ = *f++; return n; } error(Ebadarg); return 0; } | |
| 1997/0327 | static long | |
| 1993/0916 | rtcwrite(Chan *c, void *buf, long n, ulong offset) { Rtc rtc; ulong secs; char *cp, *ep; uchar *f, *t, *e; | |
| 1997/0404 | uchar x; | |
| 1993/0916 | USED(c); switch(c->qid.path){ case Qrtc: /* * read the time */ cp = ep = buf; ep += n; while(cp < ep){ if(*cp>='0' && *cp<='9') break; cp++; } secs = strtoul(cp, 0, 0); sec2rtc(secs, &rtc); | |
| 1997/0404 | rtc.year -= 1970; | |
| 1993/0916 | ||
| 1997/0404 | if(!isbinary){ /* * convert to bcd */ rtc.sec = PUTBCD(rtc.sec); rtc.min = PUTBCD(rtc.min); rtc.hour = PUTBCD(rtc.hour); rtc.mday = PUTBCD(rtc.mday); rtc.mon = PUTBCD(rtc.mon); rtc.year = PUTBCD(rtc.year); } ilock(&rtclock); /* set clock values */ x = (*(uchar*)Rtcindex)&~0x7f; *(uchar*)Rtcindex = x|Seconds; *(uchar*)Rtcdata = rtc.sec; *(uchar*)Rtcindex = x|Minutes; *(uchar*)Rtcdata = rtc.min; *(uchar*)Rtcindex = x|Hours; *(uchar*)Rtcdata = rtc.hour; *(uchar*)Rtcindex = x|Mday; *(uchar*)Rtcdata = rtc.mday; *(uchar*)Rtcindex = x|Month; *(uchar*)Rtcdata = rtc.mon; *(uchar*)Rtcindex = x|Year; *(uchar*)Rtcdata = rtc.year; iunlock(&rtclock); | |
| 1993/0916 | return n; case Qnvram: if(offset > Nvsize) return -1; if(offset + n > Nvsize) n = Nvsize - offset; t = (uchar*)Nvram+offset; f = buf; e = f + n; while(f < e) *t++ = *f++; return n; } error(Ebadarg); return 0; | |
| 1995/0108 | } | |
| 1997/0327 | Dev rtcdevtab = { | |
| 1997/0404 | rtcreset, | |
| 1997/0327 | devinit, rtcattach, devclone, rtcwalk, rtcstat, rtcopen, devcreate, rtcclose, rtcread, devbread, rtcwrite, devbwrite, devremove, devwstat, }; | |
| 1993/0916 | #define SEC2MIN 60L #define SEC2HOUR (60L*SEC2MIN) #define SEC2DAY (24L*SEC2HOUR) /* * days per month plus days/year */ static int dmsize[] = { 365, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; static int ldmsize[] = { 366, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; /* * return the days/month for the given year */ | |
| 1997/0327 | static int* | |
| 1993/0916 | yrsize(int yr) { if((yr % 4) == 0) return ldmsize; else return dmsize; } /* * compute seconds since Jan 1 1970 */ | |
| 1997/0327 | static ulong | |
| 1993/0916 | rtc2sec(Rtc *rtc) { ulong secs; int i; int *d2m; 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; return secs; } /* * compute rtc from seconds since Jan 1 1970 */ | |
| 1997/0327 | static void | |
| 1993/0916 | sec2rtc(ulong secs, Rtc *rtc) { int d; long hms, day; int *d2m; /* * 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 % 60; d = hms / 60; rtc->min = d % 60; d /= 60; rtc->hour = d; /* * year number */ if(day >= 0) for(d = 1970; day >= *yrsize(d); d++) day -= *yrsize(d); else for (d = 1970; day < 0; d--) day += *yrsize(d-1); rtc->year = d; /* * generate month */ d2m = yrsize(rtc->year); for(d = 1; day >= d2m[d]; d++) day -= d2m[d]; rtc->mday = day + 1; rtc->mon = d; return; } | |