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

1999/0612/mpc/devrtc.c (diff list | history)

mpc/devrtc.c on 1999/0121
1999/0121    
#include	"u.h" 
#include	"../port/lib.h" 
#include	"mem.h" 
#include	"dat.h" 
#include	"fns.h" 
#include	"../port/error.h" 
 
#include	"io.h" 
 
enum{ 
	Qrtc = 1, 
1999/0608    
	Qnvram, 
1999/0612    
	Qnvram2, 
1999/0121    
 
	/* sccr */ 
	RTDIV=	1<<24, 
	RTSEL=	1<<23, 
 
	/* rtcsc */ 
	RTE=	1<<0, 
	R38K=	1<<4, 
1999/0608    
 
	Nvoff=		4*1024,	/* where usable nvram lives */ 
	Nvsize=		4*1024, 
1999/0612    
	Nvoff2=		8*1024,	/* where usable nvram lives */ 
	Nvsize2=	4*1024, 
1999/0121    
}; 
 
static	QLock	rtclock;		/* mutex on clock operations */ 
1999/0608    
static Lock nvrtlock; 
1999/0121    
 
1999/0608    
 
1999/0121    
static Dirtab rtcdir[]={ 
	"rtc",		{Qrtc, 0},	12,	0666, 
1999/0608    
	"nvram",	{Qnvram, 0},	Nvsize,	0664, 
1999/0612    
	"nvram2",	{Qnvram2, 0},	Nvsize,	0664, 
1999/0121    
}; 
#define	NRTC	(sizeof(rtcdir)/sizeof(rtcdir[0])) 
 
static void 
rtcreset(void) 
{ 
	IMM *io; 
	int n; 
 
	io = m->iomem; 
	io->rtcsck = KEEP_ALIVE_KEY; 
	n = (RTClevel<<8)|RTE; 
	if(m->oscclk == 5) 
		n |= R38K; 
	io->rtcsc = n; 
	io->rtcsck = ~KEEP_ALIVE_KEY; 
print("sccr=#%8.8lux plprcr=#%8.8lux\n", io->sccr, io->plprcr); 
} 
 
static Chan* 
rtcattach(char *spec) 
{ 
	return devattach('r', spec); 
} 
 
static int	  
rtcwalk(Chan *c, char *name) 
{ 
	return devwalk(c, name, rtcdir, NRTC, devgen); 
} 
 
static void	  
rtcstat(Chan *c, char *dp) 
{ 
	devstat(c, dp, rtcdir, NRTC, devgen); 
} 
 
static Chan* 
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; 
1999/0608    
	case Qnvram: 
1999/0612    
	case Qnvram2: 
1999/0608    
		if(strcmp(up->user, eve)!=0) 
1999/0121    
			error(Eperm); 
		break; 
	} 
	return devopen(c, omode, rtcdir, NRTC, devgen); 
} 
 
static void	  
rtcclose(Chan*) 
{ 
} 
 
static long	  
rtcread(Chan *c, void *buf, long n, vlong offset) 
{ 
	ulong t; 
 
	if(c->qid.path & CHDIR) 
		return devdirread(c, buf, n, rtcdir, NRTC, devgen); 
 
	switch(c->qid.path){ 
	case Qrtc: 
		t = m->iomem->rtc; 
		n = readnum(offset, buf, n, t, 12); 
		return n; 
1999/0608    
	case Qnvram: 
		if(offset >= Nvsize) 
			return 0; 
		t = offset; 
		if(t + n > Nvsize) 
			n = Nvsize - t; 
		ilock(&nvrtlock); 
		memmove(buf, (uchar*)(NVRAMMEM + Nvoff + t), n); 
		iunlock(&nvrtlock); 
		return n; 
1999/0612    
	case Qnvram2: 
		if(offset >= Nvsize2) 
			return 0; 
		t = offset; 
		if(t + n > Nvsize2) 
			n = Nvsize2 - t; 
		ilock(&nvrtlock); 
		memmove(buf, (uchar*)(NVRAMMEM + Nvoff2 + t), n); 
		iunlock(&nvrtlock); 
		return n; 
1999/0121    
	} 
	error(Egreg); 
	return 0;		/* not reached */ 
} 
 
static long	  
rtcwrite(Chan *c, void *buf, long n, vlong offset) 
{ 
	ulong secs; 
	char *cp, *ep; 
	IMM *io; 
1999/0608    
	ulong t; 
1999/0121    
 
	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); 
		/* 
		 * set it 
		 */ 
		io = ioplock(); 
		io->rtck = KEEP_ALIVE_KEY; 
		io->rtc = secs; 
		io->rtck = ~KEEP_ALIVE_KEY; 
		iopunlock(); 
		return n; 
1999/0608    
	case Qnvram: 
		if(offset >= Nvsize) 
			return 0; 
		t = offset; 
		if(t + n > Nvsize) 
			n = Nvsize - t; 
		ilock(&nvrtlock); 
		memmove((uchar*)(NVRAMMEM + Nvoff + offset), buf, n); 
1999/0612    
		iunlock(&nvrtlock); 
		return n; 
	case Qnvram2: 
		if(offset >= Nvsize2) 
			return 0; 
		t = offset; 
		if(t + n > Nvsize2) 
			n = Nvsize2 - t; 
		ilock(&nvrtlock); 
		memmove((uchar*)(NVRAMMEM + Nvoff2 + offset), buf, n); 
1999/0608    
		iunlock(&nvrtlock); 
		return n; 
1999/0121    
	} 
	error(Egreg); 
	return 0;		/* not reached */ 
} 
 
long 
rtctime(void) 
{ 
	return m->iomem->rtc; 
} 
 
Dev rtcdevtab = { 
	'r', 
	"rtc", 
 
	rtcreset, 
	devinit, 
	rtcattach, 
	devclone, 
	rtcwalk, 
	rtcstat, 
	rtcopen, 
	devcreate, 
	rtcclose, 
	rtcread, 
	devbread, 
	rtcwrite, 
	devbwrite, 
	devremove, 
	devwstat, 
}; 


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