xref: /netbsd/sys/arch/next68k/stand/boot/en.c (revision c4a72b64)
1 /*      $NetBSD: en.c,v 1.8 2002/09/11 13:15:59 mycroft 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 __P((struct netif *nif, void *machdep_hint));
62 int en_probe __P((struct netif *nif, void *machdep_hint));
63 void en_init __P((struct iodesc *desc, void *machdep_hint));
64 int en_get __P((struct iodesc *a, void *b, size_t c, time_t d));
65 int en_put __P((struct iodesc *a, void *b, size_t c));
66 void en_end __P((struct netif *a));
67 
68 /* ### static int mountroot __P((int sock)); */
69 static int en_wait_for_intr __P((int flag));
70 
71 struct netif_stats en_stats;
72 
73 struct netif_dif en_ifs[] = {
74 /* dif_unit	dif_nsel	dif_stats	dif_private     */
75 {  0,		1,		&en_stats,	NULL,	},
76 };
77 
78 struct netif_driver en_driver = {
79 	"en",
80 	en_match, en_probe, en_init, en_get, en_put, en_end,
81 	en_ifs, NENTS(en_ifs)
82 };
83 
84 extern int turbo;
85 
86 /* ### int netdev_sock;
87 static int open_count; */
88 
89 char dma_buffer1[MAX_DMASIZE+DMA_ENDALIGNMENT],
90      dma_buffer2[MAX_DMASIZE+DMA_ENDALIGNMENT],
91      *dma_buffers[2];
92 int next_dma_buffer;
93 
94 
95 int
96 en_match(struct netif *nif, void *machdep_hint)
97 {
98 	/* we always match, because every NeXT has an ethernet interface
99 	 * and we've checked the unit numbers before we even started this
100 	 * search.
101 	 * ### now that open is generic, we should check the params!
102 	 */
103 	return 1;
104 }
105 
106 int
107 en_probe(struct netif *nif, void *machdep_hint)
108 {
109 	/* we also always probe ok, see en_match. */
110 	return 0;
111 }
112 
113 void
114 en_init(struct iodesc *desc, void *machdep_hint)
115 {
116 	volatile struct en_regs *er;
117 	volatile u_int *bmap_chip;
118 	int i;
119 
120 	DPRINTF(("en_init\n"));
121 
122 	er = (struct en_regs *)P_ENET;
123 	bmap_chip = (u_int *)P_BMAP;
124 
125 	dma_buffers[0] = DMA_ALIGN(char *, dma_buffer1);
126 	dma_buffers[1] = DMA_ALIGN(char *, dma_buffer2);
127 
128 	er->reset = EN_RST_RESET;
129 /* 	if (turbo) */
130 /* 		er->reset = 0; */
131 
132 	er->txmask = 0;
133 	er->txstat = 0xff;
134 	if (turbo)
135 		er->txmode = 0 | EN_TMD_COLLSHIFT;
136 	else
137 		er->txmode = EN_TMD_LB_DISABLE;
138 
139 	/* setup for bnc/tp */
140 	if (!turbo) {
141 		DPRINTF (("en_media: %s\n",
142 			  (bmap_chip[13] & 0x20000000) ? "BNC" : "TP"));
143 		if (!(bmap_chip[13] & 0x20000000)) {
144 			bmap_chip[12] |= 0x90000000;
145 			bmap_chip[13] |= (0x80000000|0x10000000); /* TP */
146 		}
147 	}
148 
149 /* 	if (turbo) { */
150 /* 		er->txmode |= EN_TMD_COLLSHIFT; */
151 /* 	} else { */
152 /* 		er->txmode &= ~EN_TMD_LB_DISABLE; /\* ZZZ *\/ */
153 /* 	} */
154 
155 	er->rxmask = 0;
156 	er->rxstat = 0xff;
157 	if (turbo)
158 		er->rxmode = EN_RMD_TEST | EN_RMD_RECV_NORMAL;
159 	else
160 		er->rxmode = EN_RMD_RECV_NORMAL;
161 	for (i=0; i<6; i++)
162 	  er->addr[i] = desc->myea[i] = MON(char *,MG_clientetheraddr)[i];
163 
164 	DPRINTF(("ethernet addr (%x:%x:%x:%x:%x:%x)\n",
165 			desc->myea[0],desc->myea[1],desc->myea[2],
166 			desc->myea[3],desc->myea[4],desc->myea[5]));
167 
168 /* 	if (!turbo) */
169 		er->reset = 0;
170 }
171 
172 #if 0
173 /* ### remove this when things work! */
174 #define XCHR(x) "0123456789abcdef"[(x) & 0xf]
175 void
176 dump_pkt(unsigned char *pkt, size_t len)
177 {
178 	size_t i, j;
179 
180 	printf("0000: ");
181 	for(i=0; i<len; i++) {
182 		printf("%c%c ", XCHR(pkt[i]>>4), XCHR(pkt[i]));
183 		if ((i+1) % 16 == 0) {
184 			printf("  %c", '"');
185 			for(j=0; j<16; j++)
186 				printf("%c", pkt[i-15+j]>=32 && pkt[i-15+j]<127?pkt[i-15+j]:'.');
187 			printf("%c\n%c%c%c%c: ", '"', XCHR((i+1)>>12),
188 				XCHR((i+1)>>8), XCHR((i+1)>>4), XCHR(i+1));
189 		}
190 	}
191 	printf("\n");
192 }
193 #endif
194 
195 int
196 en_put(struct iodesc *desc, void *pkt, size_t len)
197 {
198 	volatile struct en_regs *er;
199 	volatile struct dma_dev *txdma;
200 	int state, txs;
201 	int retries;
202 
203 	DPRINTF(("en_put: %d bytes at 0x%lx\n", len, (unsigned long)pkt));
204 #if 0
205 	dump_pkt(pkt,len);
206 #endif
207 
208 	er = (struct en_regs *)P_ENET;
209 	txdma = (struct dma_dev *)P_ENETX_CSR;
210 
211 	DPRINTF(("en_put: txdma->dd_csr = %x\n",txdma->dd_csr));
212 
213 	if (len > 1600) {
214 		errno = EINVAL;
215 		return -1;
216 	}
217 
218 	if (!turbo) {
219 		while ((er->txstat & EN_TXS_READY) == 0)
220 			printf("en: tx not ready\n");
221 	}
222 
223 	for (retries = 0; retries < EN_RETRIES; retries++) {
224 		er->txstat = 0xff;
225 		bcopy(pkt, dma_buffers[0], len);
226 		txdma->dd_csr = (turbo ? DMACSR_INITBUFTURBO : DMACSR_INITBUF) |
227 			DMACSR_RESET | DMACSR_WRITE;
228 		txdma->dd_csr = 0;
229 		txdma->dd_next/* _initbuf */ = dma_buffers[0];
230 		txdma->dd_start = (turbo ? dma_buffers[0] : 0);
231 		txdma->dd_limit = ENDMA_ENDALIGN(char *,  dma_buffers[0]+len);
232 		txdma->dd_stop = 0;
233 		txdma->dd_csr = DMACSR_SETENABLE;
234 		if (turbo)
235 			er->txmode |= 0x80;
236 
237 		while(1) {
238 			if (en_wait_for_intr(ENETX_DMA_INTR)) {
239 				printf("en_put: timed out\n");
240 				errno = EIO;
241 				return -1;
242 			}
243 
244 			state = txdma->dd_csr &
245 				(DMACSR_BUSEXC | DMACSR_COMPLETE
246 				 | DMACSR_SUPDATE | DMACSR_ENABLE);
247 
248 #if 01
249 			DPRINTF(("en_put: dma state = 0x%x.\n", state));
250 #endif
251 			if (state & (DMACSR_COMPLETE|DMACSR_BUSEXC))
252 				txdma->dd_csr = DMACSR_RESET | DMACSR_CLRCOMPLETE;
253 				break;
254 		}
255 
256 		txs = er->txstat;
257 
258 #if 01
259 		DPRINTF(("en_put: done txstat=%x.\n", txs));
260 #endif
261 
262 #define EN_TXS_ERROR (EN_TXS_SHORTED | EN_TXS_UNDERFLOW | EN_TXS_PARERR)
263 		if ((state & DMACSR_COMPLETE) == 0 ||
264 		    (txs & EN_TXS_ERROR) != 0) {
265 			errno = EIO;
266 			return -1;
267 		}
268 		if ((txs & EN_TXS_COLLERR) == 0)
269 			return len;		/* success */
270 	}
271 
272 	errno = EIO;		/* too many retries */
273 	return -1;
274 }
275 
276 int
277 en_get(struct iodesc *desc, void *pkt, size_t len, time_t timeout)
278 {
279 	volatile struct en_regs *er;
280 	volatile struct dma_dev *rxdma;
281 	volatile struct dma_dev *txdma;
282 	int state, rxs;
283 	size_t rlen;
284 	char *gotpkt;
285 
286 	rxdma = (struct dma_dev *)P_ENETR_CSR;
287 	txdma = (struct dma_dev *)P_ENETX_CSR;
288 	er = (struct en_regs *)P_ENET;
289 
290 	DPRINTF(("en_get: rxdma->dd_csr = %x\n",rxdma->dd_csr));
291 
292 	er->rxstat = 0xff;
293 
294 	/* this is mouse's code now ... still doesn't work :( */
295 	/* The previous comment is now a lie, this does work
296 	 * Darrin B Jewell <jewell@mit.edu>  Sat Jan 24 21:44:56 1998
297 	 */
298 
299 	rxdma->dd_csr = 0;
300 	rxdma->dd_csr = (turbo ? DMACSR_INITBUFTURBO : DMACSR_INITBUF) |
301 		DMACSR_READ | DMACSR_RESET;
302 
303 	if (!turbo) {
304 		rxdma->dd_saved_next = 0;
305 		rxdma->dd_saved_limit = 0;
306 		rxdma->dd_saved_start = 0;
307 		rxdma->dd_saved_stop = 0;
308 	} else {
309 		rxdma->dd_saved_next = dma_buffers[0];
310 	}
311 
312 	rxdma->dd_next = dma_buffers[0];
313 	rxdma->dd_limit = DMA_ENDALIGN(char *, dma_buffers[0]+MAX_DMASIZE);
314 #if 0
315 	if (turbo) {
316 		/* !!! not a typo: txdma */
317 		txdma->dd_stop = dma_buffers[0];
318 	}
319 #endif
320 	rxdma->dd_start = 0;
321 	rxdma->dd_stop = 0;
322 	rxdma->dd_csr = DMACSR_SETENABLE | DMACSR_READ;
323 	if (turbo)
324 		er->rxmode = EN_RMD_TEST | EN_RMD_RECV_NORMAL;
325 	else
326 		er->rxmode = EN_RMD_RECV_NORMAL;
327 
328 #if 01
329 	DPRINTF(("en_get: blocking on rcv dma\n"));
330 #endif
331 
332 	while(1) {
333 		if (en_wait_for_intr(ENETR_DMA_INTR))	/* ### use timeout? */
334 			return 0;
335 
336 		state = rxdma->dd_csr &
337 			(DMACSR_BUSEXC | DMACSR_COMPLETE
338 			 | DMACSR_SUPDATE | DMACSR_ENABLE);
339 		DPRINTF(("en_get: dma state = 0x%x.\n", state));
340 		if ((state & DMACSR_ENABLE) == 0) {
341 			rxdma->dd_csr = DMACSR_RESET | DMACSR_CLRCOMPLETE;
342 			break;
343 		}
344 
345 		if (state & DMACSR_COMPLETE) {
346 			PRINTF(("en_get: ending dma sequence\n"));
347 			rxdma->dd_csr = DMACSR_CLRCOMPLETE;
348 		}
349 
350 	}
351 
352 	rxs = er->rxstat;
353 
354 	if ((state & DMACSR_COMPLETE) == 0 ||
355 	    (rxs & EN_RX_OK) == 0) {
356 		errno = EIO;
357 		return -1;	/* receive failed */
358 	}
359 
360 	if (turbo) {
361 		gotpkt = rxdma->dd_saved_next;
362 		rlen = rxdma->dd_next - rxdma->dd_saved_next;
363 	} else {
364 		gotpkt = rxdma->dd_saved_next;
365 		rlen = rxdma->dd_next - rxdma->dd_saved_next;
366 	}
367 
368 	if (gotpkt != dma_buffers[0]) {
369 		printf("Unexpected received packet location\n");
370 		printf("DEBUG: rxstat=%x.\n", rxs);
371 		printf("DEBUG: gotpkt = 0x%lx, rlen = %d, len = %d\n",(u_long)gotpkt,rlen,len);
372 		printf("DEBUG: rxdma->dd_csr = 0x%lx\n",(u_long)rxdma->dd_csr);
373 		printf("DEBUG: rxdma->dd_saved_next = 0x%lx\n",(u_long)rxdma->dd_saved_next);
374 		printf("DEBUG: rxdma->dd_saved_limit = 0x%lx\n",(u_long)rxdma->dd_saved_limit);
375 		printf("DEBUG: rxdma->dd_saved_start = 0x%lx\n",(u_long)rxdma->dd_saved_start);
376 		printf("DEBUG: rxdma->dd_saved_stop = 0x%lx\n",(u_long)rxdma->dd_saved_stop);
377 		printf("DEBUG: rxdma->dd_next = 0x%lx\n",(u_long)rxdma->dd_next);
378 		printf("DEBUG: rxdma->dd_limit = 0x%lx\n",(u_long)rxdma->dd_limit);
379 		printf("DEBUG: rxdma->dd_start = 0x%lx\n",(u_long)rxdma->dd_start);
380 		printf("DEBUG: rxdma->dd_stop = 0x%lx\n",(u_long)rxdma->dd_stop);
381 		printf("DEBUG: rxdma->dd_next_initbuf = 0x%lx\n",(u_long)rxdma->dd_next_initbuf);
382 	}
383 
384 #if 0
385 dump_pkt(gotpkt, rlen < 255 ? rlen : 128);
386 #endif
387 
388  DPRINTF(("en_get: done rxstat=%x.\n", rxs));
389 
390 	if (rlen > len) {
391 		DPRINTF(("en_get: buffer too small. want %d, got %d\n",
392 			 len, rlen));
393 		rlen = len;
394 	}
395 
396 
397 	bcopy(gotpkt, pkt, rlen);
398 
399 #if 0
400 	printf("DEBUG: gotpkt = 0x%lx, pkt = 0x%lx, rlen = %d\n",
401 			(u_long)gotpkt,(u_long)pkt,rlen);
402 	dump_pkt(gotpkt, rlen < 255 ? rlen : 128);
403 	dump_pkt(pkt, rlen < 255 ? rlen : 128);
404 #endif
405 
406 	return rlen;
407 }
408 
409 
410 void
411 en_end(struct netif *a)
412 {
413   DPRINTF(("en_end: WARNING not doing anything\n"));
414 }
415 
416 #if 0
417 
418 #define MKPANIC(ret,name,args) \
419 	ret name args { panic(#name ## ": not implemented.\n"); }
420 
421 MKPANIC(void, en_end, (struct netif *a));
422 
423 /* device functions */
424 
425 int
426 enopen(struct open_file *f, char count, char lun, char part)
427 {
428 	int error;
429 
430 	DPRINTF(("open: en(%d,%d,%d)\n", count, lun, part));
431 
432 	if (count != 0 || lun != 0 || part != 0)
433 		return EUNIT;	/* there can be exactly one ethernet */
434 
435 	if (open_count == 0) {
436 		/* Find network interface. */
437 		if ((netdev_sock = netif_open(NULL)) < 0)
438 			return errno;
439 		if ((error = mountroot(netdev_sock)) != 0) {
440 			if (open_count == 0)
441 				netif_close(netdev_sock);
442 			return error;
443 		}
444 	}
445 	open_count++;
446 	f->f_devdata = NULL; /* ### nfs_root_node ?! */
447 	return 0;
448 }
449 
450 int
451 enclose(struct open_file *f)
452 {
453 	if (open_count > 0)
454 		if (--open_count == 0)
455 			netif_close(netdev_sock);
456 	f->f_devdata = NULL;
457 	return 0;
458 }
459 
460 int
461 enstrategy(void *devdata, int rw, daddr_t dblk,
462 	   size_t size, void *buf, size_t *rsize)
463 {
464 	return ENXIO;		/* wrong access method */
465 }
466 
467 /* private function */
468 
469 static int
470 mountroot(int sock)
471 {
472 	/* Mount the root directory from a boot server */
473 #if 0
474 	struct in_addr in = {
475 		0xc2793418
476 	};
477 	u_char *res;
478 
479 	res = arpwhohas(socktodesc(sock), in);
480 	panic("arpwhohas returned %s", res);
481 #endif
482 	/* 1. use bootp. This does most of the work for us. */
483 	bootp(sock);
484 
485 	if (myip.s_addr == 0 || rootip.s_addr == 0 || rootpath[0] == '\0')
486 		return ETIMEDOUT;
487 
488 	printf("Using IP address: %s\n", inet_ntoa(myip));
489 	printf("root addr=%s path=%s\n", inet_ntoa(rootip), rootpath);
490 
491 	/* 2. mount. */
492 	if (nfs_mount(sock, rootip, rootpath) < 0)
493 		return errno;
494 
495 	return 0;
496 }
497 #endif
498 
499 static int
500 en_wait_for_intr(int flag)
501 {
502   volatile int *intrstat = MON(volatile int *,MG_intrstat);
503 
504 	int count;
505 
506 	for(count = 0; count < EN_TIMEOUT; count++)
507 		if (*intrstat & flag)
508 			return 0;
509 
510 	return -1;
511 }
512