summaryrefslogtreecommitdiff
path: root/os/ipengine/devfpga.c
blob: 41d1a44581c3f3a2f17415d7d2c792239f5ab893 (plain)
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
#include	"u.h"
#include	"../port/lib.h"
#include	"mem.h"
#include	"dat.h"
#include	"fns.h"
#include	"../port/error.h"

#include	"io.h"
#include	"archipe.h"

enum {
	FPGASIZE = 8*1024*1024,
	FPGATMR = 2-1,	/* BCLK timer number (mapped to origin 0) */
	TIMERSH = FPGATMR*4,	/* timer field shift */

	COM3=	IBIT(1)|IBIT(2),	/* sccr: clock output disabled */

	ConfDone = 1<<1,
	nStatus = 1<<0,
};

/*
 * provisional FPGA interface for simple development work;
 * for more complex things, use this to load the device then have a
 * purpose-built device driver or module
 */

enum{
	Qdir,
	Qmemb,
	Qmemw,
	Qprog,
	Qctl,
	Qclk,
	Qstatus,
};

static struct {
	QLock;
	int	clkspeed;
} fpga;

static void resetfpga(void);
static void	startfpga(int);
static int endfpga(void);
static int fpgastatus(void);
static void powerfpga(int);
static void vclkenable(int);
static void vclkset(char*, char*, char*, char*);
static void memmovew(ushort*, ushort*, long);

static Dirtab fpgadir[]={
	".",			{Qdir, 0, QTDIR},	0,	0555,
	"fpgamemb",		{Qmemb, 0},	FPGASIZE,	0666,
	"fpgamemw",		{Qmemw, 0},	FPGASIZE, 0666,
	"fpgaprog",	{Qprog, 0},	0,	0222,
	"fpgastatus",	{Qstatus, 0},	0,	0444,
	"fpgactl",		{Qctl, 0},		0,	0666,
	"fpgaclk",		{Qclk, 0},		0,	0666,
};

static char Eodd[] = "odd count or offset";

static void
fpgareset(void)
{
	powerfpga(0);
}

static Chan*
fpgaattach(char *spec)
{
	return devattach('G', spec);
}

static Walkqid*
fpgawalk(Chan *c, Chan *nc, char **name, int nname)
{
	return devwalk(c, nc, name, nname, fpgadir, nelem(fpgadir), devgen);
}

static int
fpgastat(Chan *c, uchar *dp, int n)
{
	return devstat(c, dp, n, fpgadir, nelem(fpgadir), devgen);
}

static Chan*
fpgaopen(Chan *c, int omode)
{
	return devopen(c, omode, fpgadir, nelem(fpgadir), devgen);
}

static void
fpgaclose(Chan*)
{
}

static long	 
fpgaread(Chan *c, void *buf, long n, vlong offset)
{
	int v;
	char stat[32], *p;

	if(c->qid.type & QTDIR)
		return devdirread(c, buf, n, fpgadir, nelem(fpgadir), devgen);

	switch((ulong)c->qid.path){
	case Qmemb:
		if(offset >= FPGASIZE)
			return 0;
		if(offset+n >= FPGASIZE)
			n = FPGASIZE-offset;
		memmove(buf, KADDR(FPGAMEM+offset), n);
		return n;
	case Qmemw:
		if((n | offset) & 1)
			error(Eodd);
		if(offset >= FPGASIZE)
			return 0;
		if(offset+n >= FPGASIZE)
			n = FPGASIZE-offset;
		memmovew((ushort*)buf, (ushort*)KADDR(FPGAMEM+offset), n);
		return n;
	case Qstatus:
		v = fpgastatus();
		p = seprint(stat, stat+sizeof(stat), "%sconfig", v&ConfDone?"":"!");
		seprint(p, stat+sizeof(stat), " %sstatus\n", v&nStatus?"":"!");
		return readstr(offset, buf, n, stat);
	case Qclk:
		return readnum(offset, buf, n, fpga.clkspeed, NUMSIZE);
	case Qctl:
	case Qprog:
		return 0;
	}
	error(Egreg);
	return 0;		/* not reached */
}

static long	 
fpgawrite(Chan *c, void *buf, long n, vlong offset)
{
	int i, j, v;
	ulong w;
	Cmdbuf *cb;
	ulong *cfg;
	uchar *cp;

	switch((ulong)c->qid.path){
	case Qmemb:
		if(offset >= FPGASIZE)
			return 0;
		if(offset+n >= FPGASIZE)
			n = FPGASIZE-offset;
		memmove(KADDR(FPGAMEM+offset), buf, n);
		return n;
	case Qmemw:
		if((n | offset) & 1)
			error(Eodd);
		if(offset >= FPGASIZE)
			return 0;
		if(offset+n >= FPGASIZE)
			n = FPGASIZE-offset;
		memmovew((ushort*)KADDR(FPGAMEM+offset), (ushort*)buf, n);
		return n;
	case Qctl:
		cb = parsecmd(buf, n);
		if(waserror()){
			free(cb);
			nexterror();
		}
		if(cb->nf < 1)
			error(Ebadarg);
		if(strcmp(cb->f[0], "reset") == 0)
			resetfpga();
		else if(strcmp(cb->f[0], "bclk") == 0){
			v = 48;
			if(cb->nf > 1)
				v = strtoul(cb->f[1], nil, 0);
			if(v <= 0 || 48%v != 0)
				error(Ebadarg);
			startfpga(48/v-1);
		}else if(strcmp(cb->f[0], "vclk") == 0){
			if(cb->nf == 5){	/* vclk n m v r */
				vclkenable(1);
				vclkset(cb->f[1], cb->f[2], cb->f[3], cb->f[4]);
			}else
				vclkenable(cb->nf < 2 || strcmp(cb->f[1], "on") == 0);
		}else if(strcmp(cb->f[0], "power") == 0)
			powerfpga(cb->nf < 2 || strcmp(cb->f[1], "off") != 0);
		else
			error(Ebadarg);
		poperror();
		free(cb);
		return n;
	case Qprog:
		qlock(&fpga);
		if(waserror()){
			qunlock(&fpga);
			nexterror();
		}
		powerfpga(1);
		resetfpga();
		cfg = KADDR(FPGACR);
		cp = buf;
		for(i=0; i<n; i++){
			w = cp[i];
			for(j=0; j<8; j++){
				*cfg = w&1;
				w >>= 1;
			}
		}
		for(j=0; j<50; j++)	/* Altera note says at least 10 clock cycles, but microblaster uses 50 */
			*cfg = 0;
		v = fpgastatus();
		if(v != (nStatus|ConfDone)){
			snprint(up->genbuf, sizeof(up->genbuf), "error loading fpga: status %d", v);
			error(up->genbuf);
		}
		poperror();
		qunlock(&fpga);
		return n;
	}
	error(Egreg);
	return 0;		/* not reached */
}

/*
 * PDN seems to control power to the FPGA subsystem
 * but it is not documented nor is its scope clear (PLL as well?).
 * It will not run without it.
 */
static void
powerfpga(int on)
{
	IMM *io;

	io = ioplock();
	if(io->sccr & COM3){
		io->sccrk = KEEP_ALIVE_KEY;
		io->sccr &= ~ COM3;	/* FPGA designs can use the clock */
		io->sccrk = ~KEEP_ALIVE_KEY;
	}
	io->pcpar &= ~PDN;
	io->pcdir |= PDN;
	if(on)
		io->pcdat &= ~PDN;	
	else
		io->pcdat |= PDN;
	iopunlock();
}

static void
resetfpga(void)
{
	IMM *io;

	io = ioplock();
	io->pcpar &= ~nCONFIG;
	io->pcdir |= nCONFIG;
	io->pcdat &= ~nCONFIG;
	microdelay(200);
	io->pcdat |= nCONFIG;
	iopunlock();
}

static int
fpgastatus(void)
{
	/* isolate status bits IP_B0 and IP_B1 */
	return (m->iomem->pipr>>14) & (ConfDone|nStatus);
}

static void
startfpga(int scale)
{
	IMM *io;

	io = ioplock();
	io->tgcr &= ~(0xF<<TIMERSH);
	io->tmr2 = ((scale&0xFF)<<8) | 0x2A;
	io->tcn2 = 0;
	io->trr2 = 0;
	io->ter2 = 0xFFFF;
	io->tgcr |= 0x1<<TIMERSH;
	io->padir |= BCLK;
	io->papar |= BCLK;
	iopunlock();
}

static void
vclkenable(int i)
{
	IMM *io;

	io = ioplock();
	io->padir &= ~VCLK;
	io->papar &= ~VCLK;
	io->pbdir |= EnableVCLK;
	io->pbpar &= ~EnableVCLK;
	if(i)
		io->pbdat |= EnableVCLK;
	else
		io->pbdat &= ~EnableVCLK;
	iopunlock();
}

static void
vclkin(ulong *clk, int v)
{
	int i;

	for(i=0; i<7; i++)
		*clk = (v>>i) & 1;
}

static void
vclkset(char *ns, char *ms, char *vs, char *rs)
{
	int n, m, v, r;
	ulong *clk;

	clk = KADDR(CLOCKCR);
	n = strtol(ns, nil, 0);
	m = strtol(ms, nil, 0);
	v = strtol(vs, nil, 0);
	r = strtol(rs, nil, 0);
	if(n < 3 || n > 127 || m < 3 || m > 127 || v != 1 && v != 8 ||
	   r != 1 && r != 2 && r != 4 && r != 8)
		error(Ebadarg);
	vclkenable(0);
	vclkin(clk, n);
	vclkin(clk, m);
	*clk = (v==0) & 1;
	*clk = 1; *clk = 1;
	*clk = r == 2 || r == 8;
	*clk = r == 4 || r == 8;
	*clk = 1;	/* clock out */
	*clk = 0;	/* disable clk/x */
	*clk = 1; *clk = 0; *clk = 1;
	*clk = 0; *clk = 0; *clk = 0;
	vclkenable(1);
}

/*
 * copy data aligned on 16-bit word boundaries.
 */
static void
memmovew(ushort *to, ushort *from, long count)
{
	int n;

	if(count <= 0)
		return;
	count >>= 1;
	n = (count+7) >> 3;
	switch(count&7) {	/* Duff's device */
	case 0: do {	*to++ = *from++;
	case 7:		*to++ = *from++;
	case 6:		*to++ = *from++;
	case 5:		*to++ = *from++;
	case 4:		*to++ = *from++;
	case 3:		*to++ = *from++;
	case 2:		*to++ = *from++;
	case 1:		*to++ = *from++;
		} while(--n > 0);
	}
}

Dev fpgadevtab = {
	'G',
	"fpga",

	fpgareset,
	devinit,
	devshutdown,
	fpgaattach,
	fpgawalk,
	fpgastat,
	fpgaopen,
	devcreate,
	fpgaclose,
	fpgaread,
	devbread,
	fpgawrite,
	devbwrite,
	devremove,
	devwstat,
};