Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
/*      $NetBSD: en.c,v 1.20 2023/02/12 08:25:09 tsutsui Exp $        */
/*
 * Copyright (c) 1996 Rolf Grossmann
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *      This product includes software developed by Rolf Grossmann.
 * 4. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/param.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <next68k/dev/enreg.h>
#include <next68k/next68k/nextrom.h>
#include "enreg.h"
#include "dmareg.h"
#include "samachdep.h"

#include <stand.h>
#include <netif.h>
#include <net.h>
#include <nfs.h>

#include <lib/libkern/libkern.h>

#define	MON(type, off) (*(type *)((u_int) (mg) + off))

#define PRINTF(x) printf x;
#ifdef EN_DEBUG
#define DPRINTF(x) printf x;
#else
#define DPRINTF(x)
#endif

#define	EN_TIMEOUT	2000000
#define EN_RETRIES	10

int en_match(struct netif *, void *);
int en_probe(struct netif *, void *);
void en_init(struct iodesc *, void *);
int en_get(struct iodesc *, void *, size_t, saseconds_t);
int en_put(struct iodesc *, void *, size_t);
void en_end(struct netif *);

static int en_wait_for_intr(int);

struct netif_stats en_stats;

struct netif_dif en_ifs[] = {
/* dif_unit	dif_nsel	dif_stats	dif_private     */
{  0,		1,		&en_stats,	NULL,	},
};

struct netif_driver en_driver = {
	"en",
	en_match, en_probe, en_init, en_get, en_put, en_end,
	en_ifs, sizeof(en_ifs) / sizeof(en_ifs[0])
};

/* ### int netdev_sock;
static int open_count; */

char dma_buffer1[MAX_DMASIZE+DMA_ENDALIGNMENT],
     dma_buffer2[MAX_DMASIZE+DMA_ENDALIGNMENT],
     *dma_buffers[2];
int next_dma_buffer;


int
en_match(struct netif *nif, void *machdep_hint)
{
	/* we always match, because every NeXT has an ethernet interface
	 * and we've checked the unit numbers before we even started this
	 * search.
	 * ### now that open is generic, we should check the params!
	 */
	return 1;
}

int
en_probe(struct netif *nif, void *machdep_hint)
{
	/* we also always probe ok, see en_match. */
	return 0;
}

void
en_init(struct iodesc *desc, void *machdep_hint)
{
	volatile struct en_regs *er;
	volatile u_int *bmap_chip;
	int i;

	DPRINTF(("en_init\n"));

	er = (struct en_regs *)P_ENET;
	bmap_chip = (u_int *)P_BMAP;

	dma_buffers[0] = DMA_ALIGN(char *, dma_buffer1);
	dma_buffers[1] = DMA_ALIGN(char *, dma_buffer2);

	er->reset = EN_RST_RESET;
/* 	if (turbo) */
/* 		er->reset = 0; */

	er->txmask = 0;
	er->txstat = 0xff;
	if (turbo)
		er->txmode = 0 | EN_TMD_COLLSHIFT;
	else
		er->txmode = EN_TMD_LB_DISABLE;

	/* setup for bnc/tp */
	if (!turbo) {
		DPRINTF (("en_media: %s\n",
			  (bmap_chip[13] & 0x20000000) ? "BNC" : "TP"));
		if (!(bmap_chip[13] & 0x20000000)) {
			bmap_chip[12] |= 0x90000000;
			bmap_chip[13] |= (0x80000000|0x10000000); /* TP */
		}
	}

/* 	if (turbo) { */
/* 		er->txmode |= EN_TMD_COLLSHIFT; */
/* 	} else { */
/* 		er->txmode &= ~EN_TMD_LB_DISABLE; /\* ZZZ *\/ */
/* 	} */

	er->rxmask = 0;
	er->rxstat = 0xff;
	if (turbo)
		er->rxmode = EN_RMD_TEST | EN_RMD_RECV_NORMAL;
	else
		er->rxmode = EN_RMD_RECV_NORMAL;
	for (i=0; i<6; i++)
	  er->addr[i] = desc->myea[i] = MON(char *,MG_clientetheraddr)[i];

	DPRINTF(("ethernet addr (%x:%x:%x:%x:%x:%x)\n",
			desc->myea[0],desc->myea[1],desc->myea[2],
			desc->myea[3],desc->myea[4],desc->myea[5]));

/* 	if (!turbo) */
		er->reset = 0;
}

#if 0
/* ### remove this when things work! */
#define XCHR(x) hexdigits[(x) & 0xf]
void
dump_pkt(unsigned char *pkt, size_t len)
{
	size_t i, j;

	printf("0000: ");
	for(i=0; i<len; i++) {
		printf("%c%c ", XCHR(pkt[i]>>4), XCHR(pkt[i]));
		if ((i+1) % 16 == 0) {
			printf("  %c", '"');
			for(j=0; j<16; j++)
				printf("%c", pkt[i-15+j]>=32 && pkt[i-15+j]<127?pkt[i-15+j]:'.');
			printf("%c\n%c%c%c%c: ", '"', XCHR((i+1)>>12),
				XCHR((i+1)>>8), XCHR((i+1)>>4), XCHR(i+1));
		}
	}
	printf("\n");
}
#endif

int
en_put(struct iodesc *desc, void *pkt, size_t len)
{
	volatile struct en_regs *er;
	volatile struct dma_dev *txdma;
	int state, txs;
	int retries;

	DPRINTF(("en_put: %d bytes at 0x%lx\n", len, (unsigned long)pkt));
#if 0
	dump_pkt(pkt,len);
#endif

	er = (struct en_regs *)P_ENET;
	txdma = (struct dma_dev *)P_ENETX_CSR;

	DPRINTF(("en_put: txdma->dd_csr = %x\n",txdma->dd_csr));

	if (len > 1600) {
		errno = EINVAL;
		return -1;
	}

	if (!turbo) {
		while ((er->txstat & EN_TXS_READY) == 0)
			printf("en: tx not ready\n");
	}

	for (retries = 0; retries < EN_RETRIES; retries++) {
		er->txstat = 0xff;
		memcpy(dma_buffers[0], pkt, len);
		txdma->dd_csr = (turbo ? DMACSR_INITBUFTURBO : DMACSR_INITBUF) |
			DMACSR_RESET | DMACSR_WRITE;
		txdma->dd_csr = 0;
		txdma->dd_next/* _initbuf */ = dma_buffers[0];
		txdma->dd_start = (turbo ? dma_buffers[0] : 0);
		txdma->dd_limit = ENDMA_ENDALIGN(char *,  dma_buffers[0]+len);
		txdma->dd_stop = 0;
		txdma->dd_csr = DMACSR_SETENABLE;
		if (turbo)
			er->txmode |= 0x80;

		while (1) {
			if (en_wait_for_intr(ENETX_DMA_INTR)) {
				printf("en_put: timed out\n");
				errno = EIO;
				return -1;
			}

			state = txdma->dd_csr &
				(DMACSR_BUSEXC | DMACSR_COMPLETE
				 | DMACSR_SUPDATE | DMACSR_ENABLE);

#if 01
			DPRINTF(("en_put: DMA state = 0x%x.\n", state));
#endif
			if (state & (DMACSR_COMPLETE|DMACSR_BUSEXC)) {
				txdma->dd_csr = DMACSR_RESET | DMACSR_CLRCOMPLETE;
				break;
			}
		}

		txs = er->txstat;

#if 01
		DPRINTF(("en_put: done txstat=%x.\n", txs));
#endif

#define EN_TXS_ERROR (EN_TXS_SHORTED | EN_TXS_UNDERFLOW | EN_TXS_PARERR)
		if ((state & DMACSR_COMPLETE) == 0 ||
		    (txs & EN_TXS_ERROR) != 0) {
			errno = EIO;
			return -1;
		}
		if ((txs & EN_TXS_COLLERR) == 0)
			return len;		/* success */
	}

	errno = EIO;		/* too many retries */
	return -1;
}

int
en_get(struct iodesc *desc, void *pkt, size_t len, saseconds_t timeout)
{
	volatile struct en_regs *er;
	volatile struct dma_dev *rxdma;
#if 0
	volatile struct dma_dev *txdma;
#endif
	int state, rxs;
	size_t rlen;
	char *gotpkt;

	rxdma = (struct dma_dev *)P_ENETR_CSR;
#if 0
	txdma = (struct dma_dev *)P_ENETX_CSR;
#endif
	er = (struct en_regs *)P_ENET;

	DPRINTF(("en_get: rxdma->dd_csr = %x\n",rxdma->dd_csr));

	er->rxstat = 0xff;

	/* this is mouse's code now ... still doesn't work :( */
	/* The previous comment is now a lie, this does work
	 * Darrin B Jewell <jewell@mit.edu>  Sat Jan 24 21:44:56 1998
	 */

	rxdma->dd_csr = 0;
	rxdma->dd_csr = (turbo ? DMACSR_INITBUFTURBO : DMACSR_INITBUF) |
		DMACSR_READ | DMACSR_RESET;

	if (!turbo) {
		rxdma->dd_saved_next = 0;
		rxdma->dd_saved_limit = 0;
		rxdma->dd_saved_start = 0;
		rxdma->dd_saved_stop = 0;
	} else {
		rxdma->dd_saved_next = dma_buffers[0];
	}

	rxdma->dd_next = dma_buffers[0];
	rxdma->dd_limit = DMA_ENDALIGN(char *, dma_buffers[0]+MAX_DMASIZE);
#if 0
	if (turbo) {
		/* !!! not a typo: txdma */
		txdma->dd_stop = dma_buffers[0];
	}
#endif
	rxdma->dd_start = 0;
	rxdma->dd_stop = 0;
	rxdma->dd_csr = DMACSR_SETENABLE | DMACSR_READ;
	if (turbo)
		er->rxmode = EN_RMD_TEST | EN_RMD_RECV_NORMAL;
	else
		er->rxmode = EN_RMD_RECV_NORMAL;

#if 01
	DPRINTF(("en_get: blocking on rcv DMA\n"));
#endif

	while (1) {
		if (en_wait_for_intr(ENETR_DMA_INTR))	/* ### use timeout? */
			return 0;

		state = rxdma->dd_csr &
			(DMACSR_BUSEXC | DMACSR_COMPLETE
			 | DMACSR_SUPDATE | DMACSR_ENABLE);
		DPRINTF(("en_get: DMA state = 0x%x.\n", state));
		if ((state & DMACSR_ENABLE) == 0) {
			rxdma->dd_csr = DMACSR_RESET | DMACSR_CLRCOMPLETE;
			break;
		}

		if (state & DMACSR_COMPLETE) {
			PRINTF(("en_get: ending DMA sequence\n"));
			rxdma->dd_csr = DMACSR_CLRCOMPLETE;
		}
	}

	rxs = er->rxstat;

	if ((state & DMACSR_COMPLETE) == 0 ||
	    (rxs & EN_RX_OK) == 0) {
		errno = EIO;
		return -1;	/* receive failed */
	}

	if (turbo) {
		gotpkt = rxdma->dd_saved_next;
		rlen = rxdma->dd_next - rxdma->dd_saved_next;
	} else {
		gotpkt = rxdma->dd_saved_next;
		rlen = rxdma->dd_next - rxdma->dd_saved_next;
	}

	if (gotpkt != dma_buffers[0]) {
		printf("Unexpected received packet location\n");
		printf("DEBUG: rxstat=%x.\n", rxs);
		printf("DEBUG: gotpkt = 0x%lx, rlen = %d, len = %d\n",(u_long)gotpkt,rlen,len);
		printf("DEBUG: rxdma->dd_csr = 0x%lx\n",(u_long)rxdma->dd_csr);
		printf("DEBUG: rxdma->dd_saved_next = 0x%lx\n",(u_long)rxdma->dd_saved_next);
		printf("DEBUG: rxdma->dd_saved_limit = 0x%lx\n",(u_long)rxdma->dd_saved_limit);
		printf("DEBUG: rxdma->dd_saved_start = 0x%lx\n",(u_long)rxdma->dd_saved_start);
		printf("DEBUG: rxdma->dd_saved_stop = 0x%lx\n",(u_long)rxdma->dd_saved_stop);
		printf("DEBUG: rxdma->dd_next = 0x%lx\n",(u_long)rxdma->dd_next);
		printf("DEBUG: rxdma->dd_limit = 0x%lx\n",(u_long)rxdma->dd_limit);
		printf("DEBUG: rxdma->dd_start = 0x%lx\n",(u_long)rxdma->dd_start);
		printf("DEBUG: rxdma->dd_stop = 0x%lx\n",(u_long)rxdma->dd_stop);
		printf("DEBUG: rxdma->dd_next_initbuf = 0x%lx\n",(u_long)rxdma->dd_next_initbuf);
	}

#if 0
dump_pkt(gotpkt, rlen < 255 ? rlen : 128);
#endif

	DPRINTF(("en_get: done rxstat=%x.\n", rxs));

	if (rlen > len) {
		DPRINTF(("en_get: buffer too small. want %d, got %d\n",
			 len, rlen));
		rlen = len;
	}

	memcpy(pkt, gotpkt, rlen);

#if 0
	printf("DEBUG: gotpkt = 0x%lx, pkt = 0x%lx, rlen = %d\n",
			(u_long)gotpkt,(u_long)pkt,rlen);
	dump_pkt(gotpkt, rlen < 255 ? rlen : 128);
	dump_pkt(pkt, rlen < 255 ? rlen : 128);
#endif

	return rlen;
}


void
en_end(struct netif *a)
{
	DPRINTF(("en_end: WARNING not doing anything\n"));
}

#if 0

#define MKPANIC(ret,name,args) \
	ret name args { panic(#name ## ": not implemented.\n"); }

MKPANIC(void, en_end, (struct netif *a));

/* device functions */

int
enopen(struct open_file *f, char count, char lun, char part)
{
	int error;

	DPRINTF(("open: en(%d,%d,%d)\n", count, lun, part));

	if (count != 0 || lun != 0 || part != 0)
		return EUNIT;	/* there can be exactly one ethernet */

	if (open_count == 0) {
		/* Find network interface. */
		if ((netdev_sock = netif_open(NULL)) < 0)
			return errno;
		if ((error = mountroot(netdev_sock)) != 0) {
			if (open_count == 0)
				netif_close(netdev_sock);
			return error;
		}
	}
	open_count++;
	f->f_devdata = NULL; /* ### nfs_root_node ?! */
	return 0;
}

int
enclose(struct open_file *f)
{
	if (open_count > 0)
		if (--open_count == 0)
			netif_close(netdev_sock);
	f->f_devdata = NULL;
	return 0;
}

int
enstrategy(void *devdata, int rw, daddr_t dblk,
	   size_t size, void *buf, size_t *rsize)
{
	return ENXIO;		/* wrong access method */
}

/* private function */

static int
mountroot(int sock)
{
	/* Mount the root directory from a boot server */
#if 0
	struct in_addr in = {
		0xc2793418
	};
	u_char *res;

	res = arpwhohas(socktodesc(sock), in);
	panic("arpwhohas returned %s", res);
#endif
	/* 1. use bootp. This does most of the work for us. */
	bootp(sock);

	if (myip.s_addr == 0 || rootip.s_addr == 0 || rootpath[0] == '\0')
		return ETIMEDOUT;

	printf("Using IP address: %s\n", inet_ntoa(myip));
	printf("root addr=%s path=%s\n", inet_ntoa(rootip), rootpath);

	/* 2. mount. */
	if (nfs_mount(sock, rootip, rootpath) < 0)
		return errno;

	return 0;
}
#endif

static int
en_wait_for_intr(int flag)
{
	volatile int *intrstat = MON(volatile int *, MG_intrstat);

	int count;

	for (count = 0; count < EN_TIMEOUT; count++)
		if (*intrstat & flag)
			return 0;

	return -1;
}