~kris/9p

9hist

ref: e7e01d6c0e78d81dff4b17ffbc6e97848a462fbe 9hist/mpc/devrtc.c -rw-r--r-- 6.1 KiB
e7e01d6c — David du Colombier Plan 9 from Bell Labs 2001-02-15 25 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#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,
	Qnvram,
    Qeeprom,
	Qnvram2,
	Qled,

	/* sccr */
	RTDIV=	1<<24,
	RTSEL=	1<<23,

	/* rtcsc */
	RTE=	1<<0,
	R38K=	1<<4,

	Nvoff=		0,	/* where usable EEPROM lives */
	Nvsize=		256,/*128x16=256x8*/
	Nvoff2=		8*1024,	/* where second usable nvram lives */
	Nvsize2=	4*1024,
};

static	QLock	rtclock;		/* mutex on clock operations */
static Lock nvrtlock;
static Lock eepromlock;

static Dirtab rtcdir[]={
	"rtc",		{Qrtc, 0},	12,	0666,
	"eeprom",	{Qeeprom, 0},	Nvsize,	0664,
	"nvram2",	{Qnvram2, 0},	Nvsize,	0664,
	"led",	{Qled, 0},	0,	0664,
};
#define	NRTC	(sizeof(rtcdir)/sizeof(rtcdir[0]))

extern void spiread(ulong addr, uchar *buf);
extern void spiwrite(ulong addr, uchar *buf);
extern void spiresult(void);
extern void spienwrite(void);
extern void spidiswrite(void);
extern void spiwriteall(uchar *buf);

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:
	case Qled:
		if(strcmp(up->user, eve)!=0 && omode!=OREAD)
			error(Eperm);
		break;
	case Qeeprom:
    //case Qnvram:
	case Qnvram2:
		if(strcmp(up->user, eve)!=0)
			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,readaddr;
    uchar spibuf[2];
    int  index;
    
	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;
	case Qeeprom:
    //case Qnvram:
		if(offset >= Nvsize)
			return 0;
		t = offset;
		if(t + n > Nvsize)
			n = Nvsize - t;
		ilock(&eepromlock);
        
        /* read from eeprom through spi, two bytes a time*/

        index=0;
        if(t%2!=0){
          print("not starting from an even address\n");
          readaddr=(t-1)/2;
          spiread(readaddr,spibuf);
          memmove((uchar*)buf+index,(uchar *)spibuf+1,1);
          index++;
          readaddr++;
        }
        else
          readaddr=t/2;
        
        while(index<n){
            spiread(readaddr,spibuf);
            memmove((uchar*)buf+index,spibuf,1);
            if(index+1<n)
               memmove((uchar*)buf+index+1,(uchar*)spibuf+1,1);
            readaddr++;
            index+=2;
        }
        
/*        print("result of reading is \n");
        for(index=0;index<n;index++)
          print(" %ux ",((uchar*)buf)[index]);
        print("\n");
*/
		iunlock(&eepromlock);
		return n;
	case Qnvram2:
        print("ram 2\n");
		return 0;
		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;
	case Qled:
		if(offset >= 5)
			return 0;
		t = offset;
		if(t + n > 5)
			n = 5 - t;
		memmove(buf, "kitty" + t, n);
		return n;
	}
	error(Egreg);
	return 0;		/* not reached */
}

static long	 
rtcwrite(Chan *c, void *buf, long n, vlong offset)
{
	ulong secs;
	char *cp, *ep;
	IMM *io;
	ulong t,readaddr,writeaddr;
    int index;
    uchar spibuf[2];

	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;
	case Qeeprom:
    //case Qnvram:
		print("write %d to eeprom\n",n);
		if(offset >= Nvsize)
			return 0;
		t = offset;
		if(t + n > Nvsize)
			n = Nvsize - t;

		ilock(&eepromlock);
        index=0;
        spienwrite();
        /*if the writing addr starts at an odd address, we need to read in
          the higher byte first, combined it with the lower byte,
          and then write them out
        */
        if(t%2!=0){
          print("not starting from an even address\n");
          readaddr=(t-1)/2;
          spiread(readaddr,spibuf);
          memmove((uchar*)spibuf+1,(uchar*)buf+index,1);
          //spienwrite();
          spiwrite(readaddr,spibuf);
          //spidiswrite();
          index++;
          writeaddr=readaddr+1;
        }
        else
          writeaddr=t/2;

        while(index<n){
           if(index+1==n){
             //spienwrite();
             spiread(writeaddr,spibuf);
             //spidiswrite();
           }
           else
             memmove((uchar*)spibuf+1,(uchar*)buf+index+1,1);
           
           memmove((uchar*)spibuf,(uchar*)buf+index,1);
          // spienwrite();
           spiwrite(writeaddr,spibuf);
          // spidiswrite();
           writeaddr++;
           index+=2;
        }
          
        spidiswrite();
		//memmove((uchar*)(NVRAMMEM + Nvoff + offset), buf, n);
		iunlock(&eepromlock);
		return n;

	case Qnvram2:
        memmove((uchar*)spibuf,(uchar*)buf,2);
        spiwriteall(spibuf);
		return 0;
		if(offset >= Nvsize2)
			return 0;
		t = offset;
		if(t + n > Nvsize2)
			n = Nvsize2 - t;
		ilock(&nvrtlock);
		memmove((uchar*)(NVRAMMEM + Nvoff2 + offset), buf, n);
		iunlock(&nvrtlock);
		return n;
	case Qled:
		if(strncmp(buf, "on", 2) == 0)
			powerupled();
		else if(strncmp(buf, "off", 3) == 0)
			powerdownled();
		else
			error("unknown command");
		return n;
	}
	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,
};