summaryrefslogtreecommitdiff
path: root/os/boot/rpcg/etherscc.c
blob: 1e47d473faf1a81c12bb77380f0fda54dc488873 (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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/*
 * SCCn ethernet
 */

#include "u.h"
#include "lib.h"
#include "mem.h"
#include "dat.h"
#include "fns.h"
#include "io.h"

#include "etherif.h"

enum {
	Nrdre		= 32,	/* receive descriptor ring entries */
	Ntdre		= 4,	/* transmit descriptor ring entries */

	Rbsize		= ETHERMAXTU+4,		/* ring buffer size (+4 for CRC) */
	Bufsize		= (Rbsize+7)&~7,	/* aligned */
};

enum {
	/* ether-specific Rx BD bits */
	RxMiss=		1<<8,
	RxeLG=		1<<5,
	RxeNO=		1<<4,
	RxeSH=		1<<3,
	RxeCR=		1<<2,
	RxeOV=		1<<1,
	RxeCL=		1<<0,
	RxError=		(RxeLG|RxeNO|RxeSH|RxeCR|RxeOV|RxeCL),	/* various error flags */

	/* ether-specific Tx BD bits */
	TxPad=		1<<14,	/* pad short frames */
	TxTC=		1<<10,	/* transmit CRC */
	TxeDEF=		1<<9,
	TxeHB=		1<<8,
	TxeLC=		1<<7,
	TxeRL=		1<<6,
	TxeUN=		1<<1,
	TxeCSL=		1<<0,

	/* scce */
	RXB=	1<<0,
	TXB=	1<<1,
	BSY=		1<<2,
	RXF=		1<<3,
	TXE=		1<<4,

	/* gsmrl */
	ENR=	1<<5,
	ENT=	1<<4,

	/* port A */
	RXD1=	SIBIT(15),
	TXD1=	SIBIT(14),

	/* port B */
	RTS1=	IBIT(19),

	/* port C */
	CTS1=	SIBIT(11),
	CD1=	SIBIT(10),
};

typedef struct Etherparam Etherparam;
struct Etherparam {
	SCCparam;
	ulong	c_pres;		/* preset CRC */
	ulong	c_mask;		/* constant mask for CRC */
	ulong	crcec;		/* CRC error counter */
	ulong	alec;		/* alighnment error counter */
	ulong	disfc;		/* discard frame counter */
	ushort	pads;		/* short frame PAD characters */
	ushort	ret_lim;	/* retry limit threshold */
	ushort	ret_cnt;	/* retry limit counter */
	ushort	mflr;		/* maximum frame length reg */
	ushort	minflr;		/* minimum frame length reg */
	ushort	maxd1;		/* maximum DMA1 length reg */
	ushort	maxd2;		/* maximum DMA2 length reg */
	ushort	maxd;		/* rx max DMA */
	ushort	dma_cnt;	/* rx dma counter */
	ushort	max_b;		/* max bd byte count */
	ushort	gaddr[4];		/* group address filter */
	ulong	tbuf0_data0;	/* save area 0 - current frm */
	ulong	tbuf0_data1;	/* save area 1 - current frm */
	ulong	tbuf0_rba0;
	ulong	tbuf0_crc;
	ushort	tbuf0_bcnt;
	ushort	paddr[3];	/* physical address LSB to MSB increasing */
	ushort	p_per;		/* persistence */
	ushort	rfbd_ptr;	/* rx first bd pointer */
	ushort	tfbd_ptr;	/* tx first bd pointer */
	ushort	tlbd_ptr;	/* tx last bd pointer */
	ulong	tbuf1_data0;	/* save area 0 - next frame */
	ulong	tbuf1_data1;	/* save area 1 - next frame */
	ulong	tbuf1_rba0;
	ulong	tbuf1_crc;
	ushort	tbuf1_bcnt;
	ushort	tx_len;		/* tx frame length counter */
	ushort	iaddr[4];		/* individual address filter*/
	ushort	boff_cnt;	/* back-off counter */
	ushort	taddr[3];	/* temp address */
};

typedef struct {
	SCC*	scc;
	int	port;
	int	cpm;

	BD*	rdr;				/* receive descriptor ring */
	void*	rrb;				/* receive ring buffers */
	int	rdrx;				/* index into rdr */

	BD*	tdr;				/* transmit descriptor ring */
	void*	trb;				/* transmit ring buffers */
	int	tdrx;				/* index into tdr */
} Mot;
static Mot mot[MaxEther];

static	int	sccid[] = {-1, SCC1ID, SCC2ID, SCC3ID, SCC4ID};
static	int	sccparam[] = {-1, SCC1P, SCC2P, SCC3P, SCC4P};
static	int	sccreg[] = {-1, 0xA00, 0xA20, 0xA40, 0xA60};
static	int	sccirq[] = {-1, 0x1E, 0x1D, 0x1C, 0x1B};

static void
attach(Ctlr *ctlr)
{
	mot[ctlr->ctlrno].scc->gsmrl |= ENR|ENT;
	eieio();
}

static void
transmit(Ctlr *ctlr)
{
	int len;
	Mot *motp;
	Block *b;
	BD *tdre;

	motp = &mot[ctlr->ctlrno];
	while(((tdre = &motp->tdr[motp->tdrx])->status & BDReady) == 0){
		b = qget(ctlr->oq);
		if(b == 0)
			break;

		/*
		 * Copy the packet to the transmit buffer.
		 */
		len = BLEN(b);
		memmove(KADDR(tdre->addr), b->rp, len);
	
		/*
		 * Give ownership of the descriptor to the chip, increment the
		 * software ring descriptor pointer and tell the chip to poll.
		 */
		tdre->length = len;
		eieio();
		tdre->status = (tdre->status & BDWrap) | BDReady|TxPad|BDInt|BDLast|TxTC;
		eieio();
		motp->scc->todr = 1<<15;	/* transmit now */
		eieio();
		motp->tdrx = NEXT(motp->tdrx, Ntdre);

		freeb(b);
	
	}
}

static void
interrupt(Ureg*, void *ap)
{
	int len, events, status;
	Mot *motp;
	BD *rdre;
	Block *b;
	Ctlr *ctlr;

	ctlr = ap;
	motp = &mot[ctlr->ctlrno];

	/*
	 * Acknowledge all interrupts and whine about those that shouldn't
	 * happen.
	 */
	events = motp->scc->scce;
	eieio();
	motp->scc->scce = events;
	eieio();
	if(events & (TXE|BSY|RXB))
		print("ETHER.SCC#%d: scce = 0x%uX\n", ctlr->ctlrno, events);
	//print(" %ux|", events);
	/*
	 * Receiver interrupt: run round the descriptor ring logging
	 * errors and passing valid receive data up to the higher levels
	 * until we encounter a descriptor still owned by the chip.
	 */
	if(events & (RXF|RXB) || 1){
		rdre = &motp->rdr[motp->rdrx];
		while(((status = rdre->status) & BDEmpty) == 0){
			if(status & RxError || (status & (BDFirst|BDLast)) != (BDFirst|BDLast)){
				//if(status & RxBuff)
				//	ctlr->buffs++;
				if(status & (1<<2))
					ctlr->crcs++;
				if(status & (1<<1))
					ctlr->overflows++;
				//print("eth rx: %ux\n", status);
				if(status & RxError)
					print("~");
				else if((status & BDLast) == 0)
					print("@");
			}
			else{
				/*
				 * We have a packet. Read it into the next
				 * free ring buffer, if any.
				 */
				len = rdre->length-4;
				if((b = iallocb(len)) != 0){
					memmove(b->wp, KADDR(rdre->addr), len);
					b->wp += len;
					etheriq(ctlr, b, 1);
				}
			}

			/*
			 * Finished with this descriptor, reinitialise it,
			 * give it back to the chip, then on to the next...
			 */
			rdre->length = 0;
			rdre->status = (rdre->status & BDWrap) | BDEmpty | BDInt;
			eieio();

			motp->rdrx = NEXT(motp->rdrx, Nrdre);
			rdre = &motp->rdr[motp->rdrx];
		}
	}

	/*
	 * Transmitter interrupt: handle anything queued for a free descriptor.
	 */
	if(events & TXB)
		transmit(ctlr);
	if(events & TXE)
		cpmop(RestartTx, motp->cpm, 0);
}

static void
ringinit(Mot* motp)
{
	int i, x;

	/*
	 * Initialise the receive and transmit buffer rings. The ring
	 * entries must be aligned on 16-byte boundaries.
	 */
	if(motp->rdr == 0)
		motp->rdr = bdalloc(Nrdre);
	if(motp->rrb == 0)
		motp->rrb = ialloc(Nrdre*Bufsize, 0);
	x = PADDR(motp->rrb);
	for(i = 0; i < Nrdre; i++){
		motp->rdr[i].length = 0;
		motp->rdr[i].addr = x;
		motp->rdr[i].status = BDEmpty|BDInt;
		x += Bufsize;
	}
	motp->rdr[i-1].status |= BDWrap;
	motp->rdrx = 0;

	if(motp->tdr == 0)
		motp->tdr = bdalloc(Ntdre);
	if(motp->trb == 0)
		motp->trb = ialloc(Ntdre*Bufsize, 0);
	x = PADDR(motp->trb);
	for(i = 0; i < Ntdre; i++){
		motp->tdr[i].addr = x;
		motp->tdr[i].length = 0;
		motp->tdr[i].status = TxPad|BDInt|BDLast|TxTC;
		x += Bufsize;
	}
	motp->tdr[i-1].status |= BDWrap;
	motp->tdrx = 0;
}

/*
 * This follows the MPC823 user guide: section16.9.23.7's initialisation sequence,
 * except that it sets the right bits for the MPC823ADS board when SCC2 is used,
 * and those for the 860/821 development board for SCC1.
 */
static void
sccsetup(Mot *ctlr, SCC *scc, uchar *ea)
{
	int i, rcs, tcs, w;
	Etherparam *p;
	IMM *io;


	i = 2*(ctlr->port-1);
	io = ioplock();
	w = (TXD1|RXD1)<<i;	/* TXDn and RXDn in port A */
	io->papar |= w;	/* enable TXDn and RXDn pins */
	io->padir &= ~w;
	io->paodr &= ~w;	/* not open drain */

	w = (CD1|CTS1)<<i;	/* CLSN and RENA: CDn and CTSn in port C */
	io->pcpar &= ~w;	/* enable CLSN (CTSn) and RENA (CDn) */
	io->pcdir &= ~w;
	io->pcso |= w;
	iopunlock();

	/* clocks and transceiver control: details depend on the board's wiring */
	archetherenable(ctlr->cpm, &rcs, &tcs);

	sccnmsi(ctlr->port, rcs, tcs);	/* connect the clocks */

	p = (Etherparam*)KADDR(sccparam[ctlr->port]);
	memset(p, 0, sizeof(*p));
	p->rfcr = 0x18;
	p->tfcr = 0x18;
	p->mrblr = Bufsize;
	p->rbase = PADDR(ctlr->rdr);
	p->tbase = PADDR(ctlr->tdr);

	cpmop(InitRxTx, ctlr->cpm, 0);

	p->c_pres = ~0;
	p->c_mask = 0xDEBB20E3;
	p->crcec = 0;
	p->alec = 0;
	p->disfc = 0;
	p->pads = 0x8888;
	p->ret_lim = 0xF;
	p->mflr = Rbsize;
	p->minflr = ETHERMINTU+4;
	p->maxd1 = Bufsize;
	p->maxd2 = Bufsize;
	p->p_per = 0;	/* only moderate aggression */

	for(i=0; i<Eaddrlen; i+=2)
		p->paddr[2-i/2] = (ea[i+1]<<8)|ea[i];	/* it's not the obvious byte order */

	scc->psmr = (2<<10)|(5<<1);	/* 32-bit CRC, ignore 22 bits before SFD */
	scc->dsr = 0xd555;
	scc->gsmrh = 0;	/* normal operation */
	scc->gsmrl = (1<<28)|(4<<21)|(1<<19)|0xC;	/* transmit clock invert, 48 bit preamble, repetitive 10 preamble, ethernet */
	eieio();
	scc->scce = ~0;	/* clear all events */
	eieio();
	scc->sccm = TXE | RXF | TXB;	/* enable interrupts */
	eieio();

	io = ioplock();
	w = RTS1<<(ctlr->port-1);	/* enable TENA pin (RTSn) */
	io->pbpar |= w;
	io->pbdir |= w;
	iopunlock();

	/* gsmrl enable is deferred until attach */
}

/*
 * Prepare the SCCx ethernet for booting.
 */
int
sccethreset(Ctlr* ctlr)
{
	uchar ea[Eaddrlen];
	Mot *motp;
	SCC *scc;
	char line[50], def[50];

	/*
	 * Since there's no EPROM, insist that the configuration entry
	 * (see conf.c and flash.c) holds the Ethernet address.
	 */
	memset(ea, 0, Eaddrlen);
	if(memcmp(ea, ctlr->card.ea, Eaddrlen) == 0){
		print("no preset Ether address\n");
		for(;;){
			strcpy(def, "00108bf12900");	/* valid MAC address to be used only for initial configuration */
			if(getstr("ether MAC address", line, sizeof(line), def) < 0)
				return -1;
			if(parseether(ctlr->card.ea, line) >= 0 || ctlr->card.ea[0] == 0xFF)
				break;
			print("invalid MAC address\n");
		}
	}

	scc = IOREGS(sccreg[ctlr->card.port], SCC);
	ctlr->card.irq = VectorCPIC+sccirq[ctlr->card.port];

	motp = &mot[ctlr->ctlrno];
	motp->scc = scc;
	motp->port = ctlr->card.port;
	motp->cpm = sccid[ctlr->card.port];

	ringinit(motp);

	sccsetup(motp, scc, ctlr->card.ea);

	/* enable is deferred until attach */

	ctlr->card.reset = sccethreset;
	ctlr->card.attach = attach;
	ctlr->card.transmit = transmit;
	ctlr->card.intr = interrupt;

	return 0;
}