plan 9 kernel history: overview | file list | diff list

1997/0403/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; 
}; 
1997/0327    
static QLock rtclock;	/* mutex on clock operations */ 
1993/0916    
 
 
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    
 
	Nvsize = 4096, 
1997/0403    
 
	Nclock=		6, 
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/0403    
static uchar statusB; 
 
static void 
getstatusB(void) 
{ 
	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; 
		statusB = *(uchar*)Rtcdata; 
		break; 
	} 
} 
 
1997/0327    
static Chan* 
1993/0916    
rtcattach(char *spec) 
{ 
1997/0403    
	getstatusB(); 
1993/0916    
	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); 
} 
 
static uchar 
bcd2binary(int reg) 
{ 
	uchar x; 
1994/0225    
 
1993/0916    
	x = (*(uchar*)Rtcindex)&~0x7f; 
	*(uchar*)Rtcindex = x|reg; 
	x = *(uchar*)Rtcdata; 
	return (x&0xf) + 10*(x>>4); 
} 
 
1997/0403    
#define GETBCD(o) ((clock[o]&0xf) + 10*(clock[o]>>4)) 
 
static void 
dumprtcstatus(void) 
{ 
	int i, x; 
	uchar status[4], r; 
 
	for(i = 0; i < 10000; i++){ 
		x = (*(uchar*)Rtcindex)&~0x7f; 
		*(uchar*)Rtcindex = x|Status; 
		r = *(uchar*)Rtcdata; 
		if(r & 0x80) 
			continue; 
 
		status[0] = r; 
		*(uchar*)Rtcindex = x|StatusB; 
		status[1] = *(uchar*)Rtcdata; 
		*(uchar*)Rtcindex = x|StatusC; 
		status[2] = *(uchar*)Rtcdata; 
		*(uchar*)Rtcindex = x|StatusD; 
		status[3] = *(uchar*)Rtcdata; 
 
		*(uchar*)Rtcindex = x|Status; 
		r = *(uchar*)Rtcdata; 
		if((r & 0x80) == 0) 
			break; 
	} 
 
	print("RTC: %uX %uX %uX %uX\n", status[0], status[1], status[2], status[3]); 
} 
 
1997/0401    
static long	  
_rtctime(void) 
1993/0916    
{ 
	Rtc rtc; 
1997/0401    
	int i, x; 
1997/0403    
	uchar clock[Nclock], r; 
	int busy; 
1993/0916    
 
1997/0403    
	/* don't do the read until the clock is no longer busy */ 
	busy = 0; 
1997/0401    
	for(i = 0; i < 10000; i++){ 
		x = (*(uchar*)Rtcindex)&~0x7f; 
		*(uchar*)Rtcindex = x|Status; 
1997/0403    
		r = *(uchar*)Rtcdata; 
		if(r & 0x80){ 
			busy++; 
1997/0401    
			continue; 
1997/0403    
		} 
1993/0916    
 
1997/0403    
		/* read clock values */ 
		*(uchar*)Rtcindex = x|Seconds; 
		clock[0] = *(uchar*)Rtcdata; 
		*(uchar*)Rtcindex = x|Minutes; 
		clock[1] = *(uchar*)Rtcdata; 
		*(uchar*)Rtcindex = x|Hours; 
		clock[2] = *(uchar*)Rtcdata; 
		*(uchar*)Rtcindex = x|Mday; 
		clock[3] = *(uchar*)Rtcdata; 
		*(uchar*)Rtcindex = x|Month; 
		clock[4] = *(uchar*)Rtcdata; 
		*(uchar*)Rtcindex = x|Year; 
		clock[5] = *(uchar*)Rtcdata; 
1997/0401    
 
		*(uchar*)Rtcindex = x|Status; 
1997/0403    
		r = *(uchar*)Rtcdata; 
		if((r & 0x80) == 0) 
1997/0401    
			break; 
	} 
 
1997/0403    
	if(statusB & 0x04){ 
		rtc.sec = clock[0]; 
		rtc.min = clock[1]; 
		rtc.hour = clock[2]; 
		rtc.mday = clock[3]; 
		rtc.mon = clock[4]; 
		rtc.year = clock[5]; 
	} 
	else{ 
		/* 
		 *  convert from BCD 
		 */ 
		rtc.sec = GETBCD(0); 
		rtc.min = GETBCD(1); 
		rtc.hour = GETBCD(2); 
		rtc.mday = GETBCD(3); 
		rtc.mon = GETBCD(4); 
		rtc.year = GETBCD(5); 
	} 
 
	/* 
	 *  the world starts jan 1 1970 
	 */ 
	rtc.year += 1970; 
 
1993/0916    
	return rtc2sec(&rtc); 
} 
 
1997/0401    
static Lock rtlock; 
 
long 
rtctime(void) 
{ 
1997/0403    
#define notdef 
#ifdef notdef 
1997/0401    
	int i; 
	long t, ot; 
 
	ilock(&rtlock); 
 
	/* 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; 
	} 
	iunlock(&rtlock); 
 
1997/0403    
	if(i == 100) print("we are boofheads\n"); 
 
1997/0401    
	return t; 
1997/0403    
#else 
	extern ulong boottime; 
 
	return boottime+TK2SEC(MACHP(0)->ticks); 
#endif /* notdef */ 
1997/0401    
} 
 
1997/0327    
static long	  
1993/0916    
rtcread(Chan *c, void *buf, long n, ulong offset) 
{ 
1997/0401    
	ulong t; 
1993/0916    
	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/0401    
		qlock(&rtclock); 
1993/0916    
		t = rtctime(); 
1997/0403    
dumprtcstatus(); 
1997/0401    
		qunlock(&rtclock); 
1993/0916    
		n = readnum(offset, buf, n, t, 12); 
		return n; 
	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; 
} 
 
static void 
binary2bcd(int reg, uchar val) 
{ 
	uchar x; 
1997/0403    
 
1993/0916    
	x = (*(uchar*)Rtcindex)&~0x7f; 
	*(uchar*)Rtcindex = x|reg; 
1997/0403    
	if(statusB & 0x04) 
		*(uchar*)Rtcdata = val; 
	else 
		*(uchar*)Rtcdata = (val % 10) | (((val / 10) % 10)<<4); 
1993/0916    
} 
 
 
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; 
	int s; 
 
	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); 
	 
		/* 
		 *  convert to bcd 
		 */ 
		s = splhi(); 
		binary2bcd(Seconds, rtc.sec); 
		binary2bcd(Minutes, rtc.min); 
		binary2bcd(Hours, rtc.hour); 
		binary2bcd(Mday, rtc.mday); 
		binary2bcd(Month, rtc.mon); 
1997/0401    
		binary2bcd(Year, rtc.year-1970); 
1993/0916    
		splx(s); 
 
		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 = { 
	devreset, 
	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; 
} 


source code copyright © 1990-2005 Lucent Technologies; see license
Plan 9 distribution
comments to russ cox (rsc@swtch.com)