xref: /netbsd-src/sys/arch/next68k/stand/boot/en.c (revision 946379e7b37692fc43f68eb0d1c10daa0a7f3b6c)
1 /*      $NetBSD: en.c,v 1.18 2015/01/23 03:36:58 chs Exp $        */
2 /*
3  * Copyright (c) 1996 Rolf Grossmann
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. All advertising materials mentioning features or use of this software
15  *    must display the following acknowledgement:
16  *      This product includes software developed by Rolf Grossmann.
17  * 4. The name of the author may not be used to endorse or promote products
18  *    derived from this software without specific prior written permission
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/param.h>
33 #include <sys/types.h>
34 #include <netinet/in.h>
35 #include <netinet/in_systm.h>
36 #include <next68k/dev/enreg.h>
37 #include <next68k/next68k/nextrom.h>
38 #include "enreg.h"
39 #include "dmareg.h"
40 
41 #include <stand.h>
42 #include <netif.h>
43 #include <net.h>
44 #include <nfs.h>
45 
46 #include <lib/libkern/libkern.h>
47 
48 extern char *mg;
49 #define	MON(type, off) (*(type *)((u_int) (mg) + off))
50 
51 #define PRINTF(x) printf x;
52 #ifdef EN_DEBUG
53 #define DPRINTF(x) printf x;
54 #else
55 #define DPRINTF(x)
56 #endif
57 
58 #define	EN_TIMEOUT	2000000
59 #define EN_RETRIES	10
60 
61 int en_match(struct netif *, void *);
62 int en_probe(struct netif *, void *);
63 void en_init(struct iodesc *, void *);
64 int en_get(struct iodesc *, void *, size_t, saseconds_t);
65 int en_put(struct iodesc *, void *, size_t);
66 void en_end(struct netif *);
67 
68 static int en_wait_for_intr(int);
69 
70 struct netif_stats en_stats;
71 
72 struct netif_dif en_ifs[] = {
73 /* dif_unit	dif_nsel	dif_stats	dif_private     */
74 {  0,		1,		&en_stats,	NULL,	},
75 };
76 
77 struct netif_driver en_driver = {
78 	"en",
79 	en_match, en_probe, en_init, en_get, en_put, en_end,
80 	en_ifs, sizeof(en_ifs) / sizeof(en_ifs[0])
81 };
82 
83 extern int turbo;
84 
85 /* ### int netdev_sock;
86 static int open_count; */
87 
88 char dma_buffer1[MAX_DMASIZE+DMA_ENDALIGNMENT],
89      dma_buffer2[MAX_DMASIZE+DMA_ENDALIGNMENT],
90      *dma_buffers[2];
91 int next_dma_buffer;
92 
93 
94 int
95 en_match(struct netif *nif, void *machdep_hint)
96 {
97 	/* we always match, because every NeXT has an ethernet interface
98 	 * and we've checked the unit numbers before we even started this
99 	 * search.
100 	 * ### now that open is generic, we should check the params!
101 	 */
102 	return 1;
103 }
104 
105 int
106 en_probe(struct netif *nif, void *machdep_hint)
107 {
108 	/* we also always probe ok, see en_match. */
109 	return 0;
110 }
111 
112 void
113 en_init(struct iodesc *desc, void *machdep_hint)
114 {
115 	volatile struct en_regs *er;
116 	volatile u_int *bmap_chip;
117 	int i;
118 
119 	DPRINTF(("en_init\n"));
120 
121 	er = (struct en_regs *)P_ENET;
122 	bmap_chip = (u_int *)P_BMAP;
123 
124 	dma_buffers[0] = DMA_ALIGN(char *, dma_buffer1);
125 	dma_buffers[1] = DMA_ALIGN(char *, dma_buffer2);
126 
127 	er->reset = EN_RST_RESET;
128 /* 	if (turbo) */
129 /* 		er->reset = 0; */
130 
131 	er->txmask = 0;
132 	er->txstat = 0xff;
133 	if (turbo)
134 		er->txmode = 0 | EN_TMD_COLLSHIFT;
135 	else
136 		er->txmode = EN_TMD_LB_DISABLE;
137 
138 	/* setup for bnc/tp */
139 	if (!turbo) {
140 		DPRINTF (("en_media: %s\n",
141 			  (bmap_chip[13] & 0x20000000) ? "BNC" : "TP"));
142 		if (!(bmap_chip[13] & 0x20000000)) {
143 			bmap_chip[12] |= 0x90000000;
144 			bmap_chip[13] |= (0x80000000|0x10000000); /* TP */
145 		}
146 	}
147 
148 /* 	if (turbo) { */
149 /* 		er->txmode |= EN_TMD_COLLSHIFT; */
150 /* 	} else { */
151 /* 		er->txmode &= ~EN_TMD_LB_DISABLE; /\* ZZZ *\/ */
152 /* 	} */
153 
154 	er->rxmask = 0;
155 	er->rxstat = 0xff;
156 	if (turbo)
157 		er->rxmode = EN_RMD_TEST | EN_RMD_RECV_NORMAL;
158 	else
159 		er->rxmode = EN_RMD_RECV_NORMAL;
160 	for (i=0; i<6; i++)
161 	  er->addr[i] = desc->myea[i] = MON(char *,MG_clientetheraddr)[i];
162 
163 	DPRINTF(("ethernet addr (%x:%x:%x:%x:%x:%x)\n",
164 			desc->myea[0],desc->myea[1],desc->myea[2],
165 			desc->myea[3],desc->myea[4],desc->myea[5]));
166 
167 /* 	if (!turbo) */
168 		er->reset = 0;
169 }
170 
171 #if 0
172 /* ### remove this when things work! */
173 #define XCHR(x) hexdigits[(x) & 0xf]
174 void
175 dump_pkt(unsigned char *pkt, size_t len)
176 {
177 	size_t i, j;
178 
179 	printf("0000: ");
180 	for(i=0; i<len; i++) {
181 		printf("%c%c ", XCHR(pkt[i]>>4), XCHR(pkt[i]));
182 		if ((i+1) % 16 == 0) {
183 			printf("  %c", '"');
184 			for(j=0; j<16; j++)
185 				printf("%c", pkt[i-15+j]>=32 && pkt[i-15+j]<127?pkt[i-15+j]:'.');
186 			printf("%c\n%c%c%c%c: ", '"', XCHR((i+1)>>12),
187 				XCHR((i+1)>>8), XCHR((i+1)>>4), XCHR(i+1));
188 		}
189 	}
190 	printf("\n");
191 }
192 #endif
193 
194 int
195 en_put(struct iodesc *desc, void *pkt, size_t len)
196 {
197 	volatile struct en_regs *er;
198 	volatile struct dma_dev *txdma;
199 	int state, txs;
200 	int retries;
201 
202 	DPRINTF(("en_put: %d bytes at 0x%lx\n", len, (unsigned long)pkt));
203 #if 0
204 	dump_pkt(pkt,len);
205 #endif
206 
207 	er = (struct en_regs *)P_ENET;
208 	txdma = (struct dma_dev *)P_ENETX_CSR;
209 
210 	DPRINTF(("en_put: txdma->dd_csr = %x\n",txdma->dd_csr));
211 
212 	if (len > 1600) {
213 		errno = EINVAL;
214 		return -1;
215 	}
216 
217 	if (!turbo) {
218 		while ((er->txstat & EN_TXS_READY) == 0)
219 			printf("en: tx not ready\n");
220 	}
221 
222 	for (retries = 0; retries < EN_RETRIES; retries++) {
223 		er->txstat = 0xff;
224 		memcpy(dma_buffers[0], pkt, len);
225 		txdma->dd_csr = (turbo ? DMACSR_INITBUFTURBO : DMACSR_INITBUF) |
226 			DMACSR_RESET | DMACSR_WRITE;
227 		txdma->dd_csr = 0;
228 		txdma->dd_next/* _initbuf */ = dma_buffers[0];
229 		txdma->dd_start = (turbo ? dma_buffers[0] : 0);
230 		txdma->dd_limit = ENDMA_ENDALIGN(char *,  dma_buffers[0]+len);
231 		txdma->dd_stop = 0;
232 		txdma->dd_csr = DMACSR_SETENABLE;
233 		if (turbo)
234 			er->txmode |= 0x80;
235 
236 		while (1) {
237 			if (en_wait_for_intr(ENETX_DMA_INTR)) {
238 				printf("en_put: timed out\n");
239 				errno = EIO;
240 				return -1;
241 			}
242 
243 			state = txdma->dd_csr &
244 				(DMACSR_BUSEXC | DMACSR_COMPLETE
245 				 | DMACSR_SUPDATE | DMACSR_ENABLE);
246 
247 #if 01
248 			DPRINTF(("en_put: DMA state = 0x%x.\n", state));
249 #endif
250 			if (state & (DMACSR_COMPLETE|DMACSR_BUSEXC))
251 				txdma->dd_csr = DMACSR_RESET | DMACSR_CLRCOMPLETE;
252 				break;
253 		}
254 
255 		txs = er->txstat;
256 
257 #if 01
258 		DPRINTF(("en_put: done txstat=%x.\n", txs));
259 #endif
260 
261 #define EN_TXS_ERROR (EN_TXS_SHORTED | EN_TXS_UNDERFLOW | EN_TXS_PARERR)
262 		if ((state & DMACSR_COMPLETE) == 0 ||
263 		    (txs & EN_TXS_ERROR) != 0) {
264 			errno = EIO;
265 			return -1;
266 		}
267 		if ((txs & EN_TXS_COLLERR) == 0)
268 			return len;		/* success */
269 	}
270 
271 	errno = EIO;		/* too many retries */
272 	return -1;
273 }
274 
275 int
276 en_get(struct iodesc *desc, void *pkt, size_t len, saseconds_t timeout)
277 {
278 	volatile struct en_regs *er;
279 	volatile struct dma_dev *rxdma;
280 #if 0
281 	volatile struct dma_dev *txdma;
282 #endif
283 	int state, rxs;
284 	size_t rlen;
285 	char *gotpkt;
286 
287 	rxdma = (struct dma_dev *)P_ENETR_CSR;
288 #if 0
289 	txdma = (struct dma_dev *)P_ENETX_CSR;
290 #endif
291 	er = (struct en_regs *)P_ENET;
292 
293 	DPRINTF(("en_get: rxdma->dd_csr = %x\n",rxdma->dd_csr));
294 
295 	er->rxstat = 0xff;
296 
297 	/* this is mouse's code now ... still doesn't work :( */
298 	/* The previous comment is now a lie, this does work
299 	 * Darrin B Jewell <jewell@mit.edu>  Sat Jan 24 21:44:56 1998
300 	 */
301 
302 	rxdma->dd_csr = 0;
303 	rxdma->dd_csr = (turbo ? DMACSR_INITBUFTURBO : DMACSR_INITBUF) |
304 		DMACSR_READ | DMACSR_RESET;
305 
306 	if (!turbo) {
307 		rxdma->dd_saved_next = 0;
308 		rxdma->dd_saved_limit = 0;
309 		rxdma->dd_saved_start = 0;
310 		rxdma->dd_saved_stop = 0;
311 	} else {
312 		rxdma->dd_saved_next = dma_buffers[0];
313 	}
314 
315 	rxdma->dd_next = dma_buffers[0];
316 	rxdma->dd_limit = DMA_ENDALIGN(char *, dma_buffers[0]+MAX_DMASIZE);
317 #if 0
318 	if (turbo) {
319 		/* !!! not a typo: txdma */
320 		txdma->dd_stop = dma_buffers[0];
321 	}
322 #endif
323 	rxdma->dd_start = 0;
324 	rxdma->dd_stop = 0;
325 	rxdma->dd_csr = DMACSR_SETENABLE | DMACSR_READ;
326 	if (turbo)
327 		er->rxmode = EN_RMD_TEST | EN_RMD_RECV_NORMAL;
328 	else
329 		er->rxmode = EN_RMD_RECV_NORMAL;
330 
331 #if 01
332 	DPRINTF(("en_get: blocking on rcv DMA\n"));
333 #endif
334 
335 	while (1) {
336 		if (en_wait_for_intr(ENETR_DMA_INTR))	/* ### use timeout? */
337 			return 0;
338 
339 		state = rxdma->dd_csr &
340 			(DMACSR_BUSEXC | DMACSR_COMPLETE
341 			 | DMACSR_SUPDATE | DMACSR_ENABLE);
342 		DPRINTF(("en_get: DMA state = 0x%x.\n", state));
343 		if ((state & DMACSR_ENABLE) == 0) {
344 			rxdma->dd_csr = DMACSR_RESET | DMACSR_CLRCOMPLETE;
345 			break;
346 		}
347 
348 		if (state & DMACSR_COMPLETE) {
349 			PRINTF(("en_get: ending DMA sequence\n"));
350 			rxdma->dd_csr = DMACSR_CLRCOMPLETE;
351 		}
352 	}
353 
354 	rxs = er->rxstat;
355 
356 	if ((state & DMACSR_COMPLETE) == 0 ||
357 	    (rxs & EN_RX_OK) == 0) {
358 		errno = EIO;
359 		return -1;	/* receive failed */
360 	}
361 
362 	if (turbo) {
363 		gotpkt = rxdma->dd_saved_next;
364 		rlen = rxdma->dd_next - rxdma->dd_saved_next;
365 	} else {
366 		gotpkt = rxdma->dd_saved_next;
367 		rlen = rxdma->dd_next - rxdma->dd_saved_next;
368 	}
369 
370 	if (gotpkt != dma_buffers[0]) {
371 		printf("Unexpected received packet location\n");
372 		printf("DEBUG: rxstat=%x.\n", rxs);
373 		printf("DEBUG: gotpkt = 0x%lx, rlen = %d, len = %d\n",(u_long)gotpkt,rlen,len);
374 		printf("DEBUG: rxdma->dd_csr = 0x%lx\n",(u_long)rxdma->dd_csr);
375 		printf("DEBUG: rxdma->dd_saved_next = 0x%lx\n",(u_long)rxdma->dd_saved_next);
376 		printf("DEBUG: rxdma->dd_saved_limit = 0x%lx\n",(u_long)rxdma->dd_saved_limit);
377 		printf("DEBUG: rxdma->dd_saved_start = 0x%lx\n",(u_long)rxdma->dd_saved_start);
378 		printf("DEBUG: rxdma->dd_saved_stop = 0x%lx\n",(u_long)rxdma->dd_saved_stop);
379 		printf("DEBUG: rxdma->dd_next = 0x%lx\n",(u_long)rxdma->dd_next);
380 		printf("DEBUG: rxdma->dd_limit = 0x%lx\n",(u_long)rxdma->dd_limit);
381 		printf("DEBUG: rxdma->dd_start = 0x%lx\n",(u_long)rxdma->dd_start);
382 		printf("DEBUG: rxdma->dd_stop = 0x%lx\n",(u_long)rxdma->dd_stop);
383 		printf("DEBUG: rxdma->dd_next_initbuf = 0x%lx\n",(u_long)rxdma->dd_next_initbuf);
384 	}
385 
386 #if 0
387 dump_pkt(gotpkt, rlen < 255 ? rlen : 128);
388 #endif
389 
390 	DPRINTF(("en_get: done rxstat=%x.\n", rxs));
391 
392 	if (rlen > len) {
393 		DPRINTF(("en_get: buffer too small. want %d, got %d\n",
394 			 len, rlen));
395 		rlen = len;
396 	}
397 
398 	memcpy(pkt, gotpkt, rlen);
399 
400 #if 0
401 	printf("DEBUG: gotpkt = 0x%lx, pkt = 0x%lx, rlen = %d\n",
402 			(u_long)gotpkt,(u_long)pkt,rlen);
403 	dump_pkt(gotpkt, rlen < 255 ? rlen : 128);
404 	dump_pkt(pkt, rlen < 255 ? rlen : 128);
405 #endif
406 
407 	return rlen;
408 }
409 
410 
411 void
412 en_end(struct netif *a)
413 {
414 	DPRINTF(("en_end: WARNING not doing anything\n"));
415 }
416 
417 #if 0
418 
419 #define MKPANIC(ret,name,args) \
420 	ret name args { panic(#name ## ": not implemented.\n"); }
421 
422 MKPANIC(void, en_end, (struct netif *a));
423 
424 /* device functions */
425 
426 int
427 enopen(struct open_file *f, char count, char lun, char part)
428 {
429 	int error;
430 
431 	DPRINTF(("open: en(%d,%d,%d)\n", count, lun, part));
432 
433 	if (count != 0 || lun != 0 || part != 0)
434 		return EUNIT;	/* there can be exactly one ethernet */
435 
436 	if (open_count == 0) {
437 		/* Find network interface. */
438 		if ((netdev_sock = netif_open(NULL)) < 0)
439 			return errno;
440 		if ((error = mountroot(netdev_sock)) != 0) {
441 			if (open_count == 0)
442 				netif_close(netdev_sock);
443 			return error;
444 		}
445 	}
446 	open_count++;
447 	f->f_devdata = NULL; /* ### nfs_root_node ?! */
448 	return 0;
449 }
450 
451 int
452 enclose(struct open_file *f)
453 {
454 	if (open_count > 0)
455 		if (--open_count == 0)
456 			netif_close(netdev_sock);
457 	f->f_devdata = NULL;
458 	return 0;
459 }
460 
461 int
462 enstrategy(void *devdata, int rw, daddr_t dblk,
463 	   size_t size, void *buf, size_t *rsize)
464 {
465 	return ENXIO;		/* wrong access method */
466 }
467 
468 /* private function */
469 
470 static int
471 mountroot(int sock)
472 {
473 	/* Mount the root directory from a boot server */
474 #if 0
475 	struct in_addr in = {
476 		0xc2793418
477 	};
478 	u_char *res;
479 
480 	res = arpwhohas(socktodesc(sock), in);
481 	panic("arpwhohas returned %s", res);
482 #endif
483 	/* 1. use bootp. This does most of the work for us. */
484 	bootp(sock);
485 
486 	if (myip.s_addr == 0 || rootip.s_addr == 0 || rootpath[0] == '\0')
487 		return ETIMEDOUT;
488 
489 	printf("Using IP address: %s\n", inet_ntoa(myip));
490 	printf("root addr=%s path=%s\n", inet_ntoa(rootip), rootpath);
491 
492 	/* 2. mount. */
493 	if (nfs_mount(sock, rootip, rootpath) < 0)
494 		return errno;
495 
496 	return 0;
497 }
498 #endif
499 
500 static int
501 en_wait_for_intr(int flag)
502 {
503 	volatile int *intrstat = MON(volatile int *, MG_intrstat);
504 
505 	int count;
506 
507 	for (count = 0; count < EN_TIMEOUT; count++)
508 		if (*intrstat & flag)
509 			return 0;
510 
511 	return -1;
512 }
513