xref: /netbsd-src/sys/arch/amiga/dev/if_le.c (revision 8ac07aec990b9d2e483062509d0a9fa5b4f57cf2)
1 /*	$NetBSD: if_le.c,v 1.42 2008/04/04 12:25:06 tsutsui Exp $ */
2 
3 /*-
4  * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Charles M. Hannum and by Jason R. Thorpe of the Numerical Aerospace
9  * Simulation Facility, NASA Ames Research Center.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *	This product includes software developed by the NetBSD
22  *	Foundation, Inc. and its contributors.
23  * 4. Neither the name of The NetBSD Foundation nor the names of its
24  *    contributors may be used to endorse or promote products derived
25  *    from this software without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37  * POSSIBILITY OF SUCH DAMAGE.
38  */
39 
40 /*-
41  * Copyright (c) 1997 Bernd Ernesti.  All rights reserved.
42  * Copyright (c) 1992, 1993
43  *	The Regents of the University of California.  All rights reserved.
44  *
45  * This code is derived from software contributed to Berkeley by
46  * Ralph Campbell and Rick Macklem.
47  *
48  * Redistribution and use in source and binary forms, with or without
49  * modification, are permitted provided that the following conditions
50  * are met:
51  * 1. Redistributions of source code must retain the above copyright
52  *    notice, this list of conditions and the following disclaimer.
53  * 2. Redistributions in binary form must reproduce the above copyright
54  *    notice, this list of conditions and the following disclaimer in the
55  *    documentation and/or other materials provided with the distribution.
56  * 3. All advertising materials mentioning features or use of this software
57  *    must display the following acknowledgement:
58  *	This product includes software developed for the NetBSD Project
59  *	by Bernd Ernesti.
60  *	This product includes software developed by the University of
61  *	California, Berkeley and its contributors.
62  * 4. Neither the name of the University nor the names of its contributors
63  *    may be used to endorse or promote products derived from this software
64  *    without specific prior written permission.
65  *
66  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
67  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
68  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
69  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
70  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
71  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
72  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
73  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
74  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
75  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
76  * SUCH DAMAGE.
77  *
78  *	@(#)if_le.c	8.2 (Berkeley) 11/16/93
79  */
80 
81 #include "opt_inet.h"
82 
83 #include <sys/cdefs.h>
84 __KERNEL_RCSID(0, "$NetBSD: if_le.c,v 1.42 2008/04/04 12:25:06 tsutsui Exp $");
85 
86 #include "bpfilter.h"
87 
88 #include <sys/param.h>
89 #include <sys/systm.h>
90 #include <sys/mbuf.h>
91 #include <sys/syslog.h>
92 #include <sys/socket.h>
93 #include <sys/device.h>
94 
95 #include <net/if.h>
96 #include <net/if_ether.h>
97 #include <net/if_media.h>
98 
99 #ifdef INET
100 #include <netinet/in.h>
101 #include <netinet/if_inarp.h>
102 #endif
103 
104 #include <machine/cpu.h>
105 #include <machine/mtpr.h>
106 
107 #include <amiga/amiga/device.h>
108 #include <amiga/amiga/isr.h>
109 
110 #include <dev/ic/lancereg.h>
111 #include <dev/ic/lancevar.h>
112 #include <dev/ic/am7990reg.h>
113 #include <dev/ic/am7990var.h>
114 
115 #include <amiga/dev/zbusvar.h>
116 #include <amiga/dev/if_levar.h>
117 
118 int le_zbus_match(device_t, cfdata_t, void *);
119 void le_zbus_attach(device_t, device_t, void *);
120 
121 CFATTACH_DECL_NEW(le_zbus, sizeof(struct le_softc),
122     le_zbus_match, le_zbus_attach, NULL, NULL);
123 
124 #if defined(_KERNEL_OPT)
125 #include "opt_ddb.h"
126 #endif
127 
128 #ifdef DDB
129 #define	integrate
130 #define hide
131 #else
132 #define	integrate	static inline
133 #define hide		static
134 #endif
135 
136 hide void lepcnet_reset(struct lance_softc *);
137 hide void lewrcsr(struct lance_softc *, u_int16_t, u_int16_t);
138 hide u_int16_t lerdcsr(struct lance_softc *, u_int16_t);
139 
140 hide u_int16_t ariadne_swapreg(u_int16_t);
141 hide void ariadne_wrcsr(struct lance_softc *, u_int16_t, u_int16_t);
142 hide u_int16_t ariadne_rdcsr(struct lance_softc *, u_int16_t);
143 hide void ariadne_wribcr(struct lance_softc *, u_int16_t, u_int16_t);
144 integrate void ariadne_copytodesc_word(struct lance_softc *, void *, int, int);
145 integrate void ariadne_copyfromdesc_word(struct lance_softc *, void *,
146 				int, int);
147 integrate void ariadne_copytobuf_word(struct lance_softc *, void *, int, int);
148 integrate void ariadne_copyfrombuf_word(struct lance_softc *, void *, int, int);
149 integrate void ariadne_zerobuf_word(struct lance_softc *, int, int);
150 void ariadne_autoselect(struct lance_softc *, int);
151 int ariadne_mediachange(struct lance_softc *);
152 void ariadne_hwinit(struct lance_softc *);
153 
154 /*
155  * Media types supported by the Ariadne.
156  */
157 int lemedia_ariadne[] = {
158 	IFM_ETHER | IFM_10_T,
159 	IFM_ETHER | IFM_10_2,
160 	IFM_ETHER | IFM_AUTO,
161 };
162 #define NLEMEDIA_ARIADNE __arraycount(lemedia_ariadne)
163 
164 
165 hide u_int16_t
166 ariadne_swapreg(u_int16_t val)
167 {
168 
169 	return (((val & 0xff) << 8 ) | (( val >> 8) & 0xff));
170 }
171 
172 hide void
173 ariadne_wrcsr(struct lance_softc *sc, u_int16_t port, u_int16_t val)
174 {
175 	struct lereg1 *ler1 = ((struct le_softc *)sc)->sc_r1;
176 
177 	ler1->ler1_rap = ariadne_swapreg(port);
178 	ler1->ler1_rdp = ariadne_swapreg(val);
179 }
180 
181 hide u_int16_t
182 ariadne_rdcsr(struct lance_softc *sc, u_int16_t port)
183 {
184 	struct lereg1 *ler1 = ((struct le_softc *)sc)->sc_r1;
185 	u_int16_t val;
186 
187 	ler1->ler1_rap = ariadne_swapreg(port);
188 	val = ariadne_swapreg(ler1->ler1_rdp);
189 	return (val);
190 }
191 
192 hide void
193 ariadne_wribcr(struct lance_softc *sc, u_int16_t port, u_int16_t val)
194 {
195 	struct lereg1 *ler1 = ((struct le_softc *)sc)->sc_r1;
196 
197 	ler1->ler1_rap = ariadne_swapreg(port);
198 	ler1->ler1_idp = ariadne_swapreg(val);
199 }
200 
201 hide void
202 lewrcsr(struct lance_softc *sc, u_int16_t port, u_int16_t val)
203 {
204 	struct lereg1 *ler1 = ((struct le_softc *)sc)->sc_r1;
205 
206 	ler1->ler1_rap = port;
207 	ler1->ler1_rdp = val;
208 }
209 
210 hide u_int16_t
211 lerdcsr(struct lance_softc *sc, u_int16_t port)
212 {
213 	struct lereg1 *ler1 = ((struct le_softc *)sc)->sc_r1;
214 	u_int16_t val;
215 
216 	ler1->ler1_rap = port;
217 	val = ler1->ler1_rdp;
218 	return (val);
219 }
220 
221 hide void
222 lepcnet_reset(struct lance_softc *sc)
223 {
224 	struct lereg1 *ler1 = ((struct le_softc *)sc)->sc_r1;
225 	volatile int dummy;
226 
227 	dummy = ler1->ler1_reset;	/* Reset PCNet-ISA */
228 }
229 
230 void
231 ariadne_autoselect(struct lance_softc *sc, int on)
232 {
233 
234 	/*
235 	 * on = 0: autoselect disabled
236 	 * on = 1: autoselect enabled
237 	 */
238 	if (on == 0)
239 		ariadne_wribcr(sc, LE_BCR_MC, 0x0000);
240 	else
241 		ariadne_wribcr(sc, LE_BCR_MC, LE_MC_ASEL);
242 }
243 
244 int
245 ariadne_mediachange(struct lance_softc *sc)
246 {
247 	struct ifmedia *ifm = &sc->sc_media;
248 
249 	if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
250 		return (EINVAL);
251 
252 	/*
253 	 * Switch to the selected media.  If autoselect is
254 	 * set, switch it on otherwise disable it.  We'll
255 	 * switch to the other media when we detect loss of
256 	 * carrier.
257 	 */
258 	switch (IFM_SUBTYPE(ifm->ifm_media)) {
259 	    case IFM_10_T:
260 		sc->sc_initmodemedia = 1;
261 		lance_init(&sc->sc_ethercom.ec_if);
262 		break;
263 
264 	    case IFM_10_2:
265 		sc->sc_initmodemedia = 0;
266 		lance_init(&sc->sc_ethercom.ec_if);
267 		break;
268 
269 	    case IFM_AUTO:
270 		sc->sc_initmodemedia = 2;
271 		ariadne_hwinit(sc);
272 		break;
273 
274 	    default:
275 		return (EINVAL);
276 	}
277 
278 	return (0);
279 }
280 
281 void
282 ariadne_hwinit(struct lance_softc *sc)
283 {
284 
285 	/*
286 	 * Re-program LEDs to match meaning used on the Ariadne board.
287          */
288 	ariadne_wribcr(sc, LE_BCR_LED1, 0x0090);
289 	ariadne_wribcr(sc, LE_BCR_LED2, 0x0081);
290 	ariadne_wribcr(sc, LE_BCR_LED3, 0x0084);
291 
292 	/*
293 	 * Enabel/Disable auto selection
294 	 */
295 	if (sc->sc_initmodemedia == 2)
296 		ariadne_autoselect(sc, 1);
297 	else
298 		ariadne_autoselect(sc, 0);
299 }
300 
301 int
302 le_zbus_match(device_t parent, cfdata_t cfp, void *aux)
303 {
304 	struct zbus_args *zap = aux;
305 
306 	/* Commodore ethernet card */
307 	if (zap->manid == 514 && zap->prodid == 112)
308 		return (1);
309 
310 	/* Ameristar ethernet card */
311 	if (zap->manid == 1053 && zap->prodid == 1)
312 		return (1);
313 
314 	/* Ariadne ethernet card */
315 	if (zap->manid == 2167 && zap->prodid == 201)
316 		return (1);
317 
318 	return (0);
319 }
320 
321 void
322 le_zbus_attach(device_t parent, device_t self, void *aux)
323 {
324 	struct le_softc *lesc = device_private(self);
325 	struct lance_softc *sc = &lesc->sc_am7990.lsc;
326 	struct zbus_args *zap = aux;
327 	u_long ser;
328 
329 	sc->sc_dev = self;
330 
331 	/* This has no effect on PCnet-ISA LANCE chips */
332 	sc->sc_conf3 = LE_C3_BSWP;
333 
334 	/*
335 	 * Manufacturer decides the 3 first bytes, i.e. ethernet vendor ID.
336 	 */
337 	switch (zap->manid) {
338 	    case 514:
339 		/* Commodore */
340 		sc->sc_memsize = 32768;
341 		sc->sc_enaddr[0] = 0x00;
342 		sc->sc_enaddr[1] = 0x80;
343 		sc->sc_enaddr[2] = 0x10;
344 		lesc->sc_r1 = (struct lereg1 *)(0x4000 + (int)zap->va);
345 		sc->sc_mem = (void *)(0x8000 + (int)zap->va);
346 		sc->sc_addr = 0x8000;
347 		sc->sc_copytodesc = lance_copytobuf_contig;
348 		sc->sc_copyfromdesc = lance_copyfrombuf_contig;
349 		sc->sc_copytobuf = lance_copytobuf_contig;
350 		sc->sc_copyfrombuf = lance_copyfrombuf_contig;
351 		sc->sc_zerobuf = lance_zerobuf_contig;
352 		sc->sc_rdcsr = lerdcsr;
353 		sc->sc_wrcsr = lewrcsr;
354 		sc->sc_hwreset = NULL;
355 		sc->sc_hwinit = NULL;
356 		break;
357 
358 	    case 1053:
359 		/* Ameristar */
360 		sc->sc_memsize = 32768;
361 		sc->sc_enaddr[0] = 0x00;
362 		sc->sc_enaddr[1] = 0x00;
363 		sc->sc_enaddr[2] = 0x9f;
364 		lesc->sc_r1 = (struct lereg1 *)(0x4000 + (int)zap->va);
365 		sc->sc_mem = (void *)(0x8000 + (int)zap->va);
366 		sc->sc_addr = 0x8000;
367 		sc->sc_copytodesc = lance_copytobuf_contig;
368 		sc->sc_copyfromdesc = lance_copyfrombuf_contig;
369 		sc->sc_copytobuf = lance_copytobuf_contig;
370 		sc->sc_copyfrombuf = lance_copyfrombuf_contig;
371 		sc->sc_zerobuf = lance_zerobuf_contig;
372 		sc->sc_rdcsr = lerdcsr;
373 		sc->sc_wrcsr = lewrcsr;
374 		sc->sc_hwreset = NULL;
375 		sc->sc_hwinit = NULL;
376 		break;
377 
378 	    case 2167:
379 		/* Village Tronic */
380 		sc->sc_memsize = 32768;
381 		sc->sc_enaddr[0] = 0x00;
382 		sc->sc_enaddr[1] = 0x60;
383 		sc->sc_enaddr[2] = 0x30;
384 		lesc->sc_r1 = (struct lereg1 *)(0x0370 + (int)zap->va);
385 		sc->sc_mem = (void *)(0x8000 + (int)zap->va);
386 		sc->sc_addr = 0x8000;
387 		sc->sc_copytodesc = ariadne_copytodesc_word;
388 		sc->sc_copyfromdesc = ariadne_copyfromdesc_word;
389 		sc->sc_copytobuf = ariadne_copytobuf_word;
390 		sc->sc_copyfrombuf = ariadne_copyfrombuf_word;
391 		sc->sc_zerobuf = ariadne_zerobuf_word;
392 		sc->sc_rdcsr = ariadne_rdcsr;
393 		sc->sc_wrcsr = ariadne_wrcsr;
394 		sc->sc_hwreset = lepcnet_reset;
395 		sc->sc_hwinit = ariadne_hwinit;
396 		sc->sc_mediachange = ariadne_mediachange;
397 		sc->sc_supmedia = lemedia_ariadne;
398 		sc->sc_nsupmedia = NLEMEDIA_ARIADNE;
399 		sc->sc_defaultmedia = IFM_ETHER | IFM_AUTO;
400 		sc->sc_initmodemedia = 2;
401 		break;
402 
403 	    default:
404 		panic("le_zbus_attach: bad manid");
405 	}
406 
407 	/*
408 	 * Serial number for board is used as host ID.
409 	 */
410 	ser = (u_long)zap->serno;
411 	sc->sc_enaddr[3] = (ser >> 16) & 0xff;
412 	sc->sc_enaddr[4] = (ser >>  8) & 0xff;
413 	sc->sc_enaddr[5] = (ser      ) & 0xff;
414 
415 	am7990_config(&lesc->sc_am7990);
416 
417 	lesc->sc_isr.isr_intr = am7990_intr;
418 	lesc->sc_isr.isr_arg = sc;
419 	lesc->sc_isr.isr_ipl = 2;
420 	add_isr(&lesc->sc_isr);
421 }
422 
423 
424 integrate void
425 ariadne_copytodesc_word(struct lance_softc *sc, void *from, int boff, int len)
426 {
427 	u_short *b1 = from;
428 	volatile u_short *b2 = (u_short *)((u_char *)sc->sc_mem + boff);
429 
430 	for (len >>= 1; len > 0; len--)
431 		*b2++ = ariadne_swapreg(*b1++);
432 }
433 
434 integrate void
435 ariadne_copyfromdesc_word(struct lance_softc *sc, void *to, int boff, int len)
436 {
437 	volatile u_short *b1 = (u_short *)((u_char *)sc->sc_mem + boff);
438 	u_short *b2 = to;
439 
440 	for (len >>= 1; len > 0; len--)
441 		*b2++ = ariadne_swapreg(*b1++);
442 }
443 
444 #define	isodd(n)	((n) & 1)
445 
446 integrate void
447 ariadne_copytobuf_word(struct lance_softc *sc, void *from, int boff, int len)
448 {
449 	u_char *a1 = from;
450 	volatile u_char *a2 = (u_char *)sc->sc_mem + boff;
451 	u_short *b1;
452 	volatile u_short *b2;
453 	int i;
454 
455 	if (len > 0 && isodd(boff)) {
456 		/* adjust source pointer */
457 		b1 = (u_short *)(a1 + 1);
458 		/* compute aligned destination pointer */
459 		b2 = (volatile u_short *)(a2 + 1);
460 		/* copy first unaligned byte to buf */
461 		b2[-1] = (b2[-1] & 0xff00) | *a1;
462 		--len;
463 	} else {
464 		/* destination is aligned or length is zero */
465 		b1 = (u_short *)a1;
466 		b2 = (volatile u_short *)a2;
467 	}
468 
469 	/* copy full words with aligned destination */
470 	for (i = len >> 1; i > 0; i--)
471 		*b2++ = *b1++;
472 
473 	/* copy remaining byte */
474 	if (isodd(len))
475 		*b2 = (*b2 & 0x00ff) | (*(u_char *)b1) << 8;
476 }
477 
478 integrate void
479 ariadne_copyfrombuf_word(struct lance_softc *sc, void *to, int boff, int len)
480 {
481 	volatile u_char *a1 = (u_char *)sc->sc_mem + boff;
482 	u_char *a2 = to;
483 	volatile u_short *b1;
484 	u_short *b2;
485 	int i;
486 
487 	if (len > 0 && isodd(boff)) {
488 		/* compute aligned source pointer */
489 		b1  = (volatile u_short *)(a1 + 1);
490 		/* adjust destination pointer (possibly unaligned) */
491 		b2  = (u_short *)(a2 + 1);
492 		/* copy first unaligned byte from buf */
493 		*a2 = b1[-1];
494 		--len;
495 	} else {
496 		/* source is aligned or length is zero */
497 		b1 = (volatile u_short *)a1;
498 		b2 = (u_short *)a2;
499 	}
500 
501 	/* copy full words with aligned source */
502 	for (i = len >> 1; i > 0; i--)
503 		*b2++ = *b1++;
504 
505 	/* copy remaining byte */
506 	if (isodd(len))
507 		*(u_char *)b2 = *b1 >> 8;
508 }
509 
510 integrate void
511 ariadne_zerobuf_word(struct lance_softc *sc, int boff, int len)
512 {
513 	volatile u_char *a1 = (u_char *)sc->sc_mem + boff;
514 	volatile u_short *b1;
515 	int i;
516 
517 	if (len > 0 && isodd(boff)) {
518 		b1 = (volatile u_short *)(a1 + 1);
519 		b1[-1] &= 0xff00;
520 		--len;
521 	} else {
522 		b1 = (volatile u_short *)a1;
523 	}
524 
525 	for (i = len >> 1; i > 0; i--)
526 		*b1++ = 0;
527 
528 	if (isodd(len))
529 		*b1 &= 0x00ff;
530 }
531