Home | History | Annotate | Line # | Download | only in dev
if_le.c revision 1.35
      1 /*	$NetBSD: if_le.c,v 1.35 2002/01/26 13:40:56 aymeric 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 #include "bpfilter.h"
     83 
     84 #include <sys/param.h>
     85 #include <sys/systm.h>
     86 #include <sys/mbuf.h>
     87 #include <sys/syslog.h>
     88 #include <sys/socket.h>
     89 #include <sys/device.h>
     90 
     91 #include <net/if.h>
     92 #include <net/if_ether.h>
     93 #include <net/if_media.h>
     94 
     95 #ifdef INET
     96 #include <netinet/in.h>
     97 #include <netinet/if_inarp.h>
     98 #endif
     99 
    100 #include <machine/cpu.h>
    101 #include <machine/mtpr.h>
    102 
    103 #include <amiga/amiga/device.h>
    104 #include <amiga/amiga/isr.h>
    105 
    106 #include <dev/ic/lancereg.h>
    107 #include <dev/ic/lancevar.h>
    108 #include <dev/ic/am7990reg.h>
    109 #include <dev/ic/am7990var.h>
    110 
    111 #include <amiga/dev/zbusvar.h>
    112 #include <amiga/dev/if_levar.h>
    113 
    114 int le_zbus_match(struct device *, struct cfdata *, void *);
    115 void le_zbus_attach(struct device *, struct device *, void *);
    116 
    117 struct cfattach le_zbus_ca = {
    118 	sizeof(struct le_softc), le_zbus_match, le_zbus_attach
    119 };
    120 
    121 #if defined(_KERNEL_OPT)
    122 #include "opt_ddb.h"
    123 #endif
    124 
    125 #ifdef DDB
    126 #define	integrate
    127 #define hide
    128 #else
    129 #define	integrate	static __inline
    130 #define hide		static
    131 #endif
    132 
    133 hide void lepcnet_reset(struct lance_softc *);
    134 hide void lewrcsr(struct lance_softc *, u_int16_t, u_int16_t);
    135 hide u_int16_t lerdcsr(struct lance_softc *, u_int16_t);
    136 
    137 hide u_int16_t ariadne_swapreg(u_int16_t);
    138 hide void ariadne_wrcsr(struct lance_softc *, u_int16_t, u_int16_t);
    139 hide u_int16_t ariadne_rdcsr(struct lance_softc *, u_int16_t);
    140 hide void ariadne_wribcr(struct lance_softc *, u_int16_t, u_int16_t);
    141 integrate void ariadne_copytodesc_word(struct lance_softc *, void *, int, int);
    142 integrate void ariadne_copyfromdesc_word(struct lance_softc *, void *,
    143 				int, int);
    144 integrate void ariadne_copytobuf_word(struct lance_softc *, void *, int, int);
    145 integrate void ariadne_copyfrombuf_word(struct lance_softc *, void *, int, int);
    146 integrate void ariadne_zerobuf_word(struct lance_softc *, int, int);
    147 void ariadne_autoselect(struct lance_softc *, int);
    148 int ariadne_mediachange(struct lance_softc *);
    149 void ariadne_hwinit(struct lance_softc *);
    150 
    151 /*
    152  * Media types supported by the Ariadne.
    153  */
    154 int lemedia_ariadne[] = {
    155 	IFM_ETHER | IFM_10_T,
    156 	IFM_ETHER | IFM_10_2,
    157 	IFM_ETHER | IFM_AUTO,
    158 };
    159 #define NLEMEDIA_ARIADNE (sizeof(lemedia_ariadne) / sizeof(lemedia_ariadne[0]))
    160 
    161 
    162 hide u_int16_t
    163 ariadne_swapreg(u_int16_t val)
    164 {
    165 
    166 	return (((val & 0xff) << 8 ) | (( val >> 8) & 0xff));
    167 }
    168 
    169 hide void
    170 ariadne_wrcsr(struct lance_softc *sc, u_int16_t port, u_int16_t val)
    171 {
    172 	struct lereg1 *ler1 = ((struct le_softc *)sc)->sc_r1;
    173 
    174 	ler1->ler1_rap = ariadne_swapreg(port);
    175 	ler1->ler1_rdp = ariadne_swapreg(val);
    176 }
    177 
    178 hide u_int16_t
    179 ariadne_rdcsr(struct lance_softc *sc, u_int16_t port)
    180 {
    181 	struct lereg1 *ler1 = ((struct le_softc *)sc)->sc_r1;
    182 	u_int16_t val;
    183 
    184 	ler1->ler1_rap = ariadne_swapreg(port);
    185 	val = ariadne_swapreg(ler1->ler1_rdp);
    186 	return (val);
    187 }
    188 
    189 hide void
    190 ariadne_wribcr(struct lance_softc *sc, u_int16_t port, u_int16_t val)
    191 {
    192 	struct lereg1 *ler1 = ((struct le_softc *)sc)->sc_r1;
    193 
    194 	ler1->ler1_rap = ariadne_swapreg(port);
    195 	ler1->ler1_idp = ariadne_swapreg(val);
    196 }
    197 
    198 hide void
    199 lewrcsr(struct lance_softc *sc, u_int16_t port, u_int16_t val)
    200 {
    201 	struct lereg1 *ler1 = ((struct le_softc *)sc)->sc_r1;
    202 
    203 	ler1->ler1_rap = port;
    204 	ler1->ler1_rdp = val;
    205 }
    206 
    207 hide u_int16_t
    208 lerdcsr(struct lance_softc *sc, u_int16_t port)
    209 {
    210 	struct lereg1 *ler1 = ((struct le_softc *)sc)->sc_r1;
    211 	u_int16_t val;
    212 
    213 	ler1->ler1_rap = port;
    214 	val = ler1->ler1_rdp;
    215 	return (val);
    216 }
    217 
    218 hide void
    219 lepcnet_reset(struct lance_softc *sc)
    220 {
    221 	struct lereg1 *ler1 = ((struct le_softc *)sc)->sc_r1;
    222 	volatile int dummy;
    223 
    224 	dummy = ler1->ler1_reset;	/* Reset PCNet-ISA */
    225 }
    226 
    227 void
    228 ariadne_autoselect(struct lance_softc *sc, int on)
    229 {
    230 
    231 	/*
    232 	 * on = 0: autoselect disabled
    233 	 * on = 1: autoselect enabled
    234 	 */
    235 	if (on == 0)
    236 		ariadne_wribcr(sc, LE_BCR_MC, 0x0000);
    237 	else
    238 		ariadne_wribcr(sc, LE_BCR_MC, LE_MC_ASEL);
    239 }
    240 
    241 int
    242 ariadne_mediachange(struct lance_softc *sc)
    243 {
    244 	struct ifmedia *ifm = &sc->sc_media;
    245 
    246 	if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
    247 		return (EINVAL);
    248 
    249 	/*
    250 	 * Switch to the selected media.  If autoselect is
    251 	 * set, switch it on otherwise disable it.  We'll
    252 	 * switch to the other media when we detect loss of
    253 	 * carrier.
    254 	 */
    255 	switch (IFM_SUBTYPE(ifm->ifm_media)) {
    256 	    case IFM_10_T:
    257 		sc->sc_initmodemedia = 1;
    258 		lance_init(&sc->sc_ethercom.ec_if);
    259 		break;
    260 
    261 	    case IFM_10_2:
    262 		sc->sc_initmodemedia = 0;
    263 		lance_init(&sc->sc_ethercom.ec_if);
    264 		break;
    265 
    266 	    case IFM_AUTO:
    267 		sc->sc_initmodemedia = 2;
    268 		ariadne_hwinit(sc);
    269 		break;
    270 
    271 	    default:
    272 		return (EINVAL);
    273 	}
    274 
    275 	return (0);
    276 }
    277 
    278 void
    279 ariadne_hwinit(struct lance_softc *sc)
    280 {
    281 
    282 	/*
    283 	 * Re-program LEDs to match meaning used on the Ariadne board.
    284          */
    285 	ariadne_wribcr(sc, LE_BCR_LED1, 0x0090);
    286 	ariadne_wribcr(sc, LE_BCR_LED2, 0x0081);
    287 	ariadne_wribcr(sc, LE_BCR_LED3, 0x0084);
    288 
    289 	/*
    290 	 * Enabel/Disable auto selection
    291 	 */
    292 	if (sc->sc_initmodemedia == 2)
    293 		ariadne_autoselect(sc, 1);
    294 	else
    295 		ariadne_autoselect(sc, 0);
    296 }
    297 
    298 int
    299 le_zbus_match(struct device *parent, struct cfdata *cfp, void *aux)
    300 {
    301 	struct zbus_args *zap = aux;
    302 
    303 	/* Commodore ethernet card */
    304 	if (zap->manid == 514 && zap->prodid == 112)
    305 		return (1);
    306 
    307 	/* Ameristar ethernet card */
    308 	if (zap->manid == 1053 && zap->prodid == 1)
    309 		return (1);
    310 
    311 	/* Ariadne ethernet card */
    312 	if (zap->manid == 2167 && zap->prodid == 201)
    313 		return (1);
    314 
    315 	return (0);
    316 }
    317 
    318 void
    319 le_zbus_attach(struct device *parent, struct device *self, void *aux)
    320 {
    321 	struct le_softc *lesc = (struct le_softc *)self;
    322 	struct lance_softc *sc = &lesc->sc_am7990.lsc;
    323 	struct zbus_args *zap = aux;
    324 	u_long ser;
    325 
    326 	/* This has no effect on PCnet-ISA LANCE chips */
    327 	sc->sc_conf3 = LE_C3_BSWP;
    328 
    329 	/*
    330 	 * Manufacturer decides the 3 first bytes, i.e. ethernet vendor ID.
    331 	 */
    332 	switch (zap->manid) {
    333 	    case 514:
    334 		/* Commodore */
    335 		sc->sc_memsize = 32768;
    336 		sc->sc_enaddr[0] = 0x00;
    337 		sc->sc_enaddr[1] = 0x80;
    338 		sc->sc_enaddr[2] = 0x10;
    339 		lesc->sc_r1 = (struct lereg1 *)(0x4000 + (int)zap->va);
    340 		sc->sc_mem = (void *)(0x8000 + (int)zap->va);
    341 		sc->sc_addr = 0x8000;
    342 		sc->sc_copytodesc = lance_copytobuf_contig;
    343 		sc->sc_copyfromdesc = lance_copyfrombuf_contig;
    344 		sc->sc_copytobuf = lance_copytobuf_contig;
    345 		sc->sc_copyfrombuf = lance_copyfrombuf_contig;
    346 		sc->sc_zerobuf = lance_zerobuf_contig;
    347 		sc->sc_rdcsr = lerdcsr;
    348 		sc->sc_wrcsr = lewrcsr;
    349 		sc->sc_hwreset = NULL;
    350 		sc->sc_hwinit = NULL;
    351 		break;
    352 
    353 	    case 1053:
    354 		/* Ameristar */
    355 		sc->sc_memsize = 32768;
    356 		sc->sc_enaddr[0] = 0x00;
    357 		sc->sc_enaddr[1] = 0x00;
    358 		sc->sc_enaddr[2] = 0x9f;
    359 		lesc->sc_r1 = (struct lereg1 *)(0x4000 + (int)zap->va);
    360 		sc->sc_mem = (void *)(0x8000 + (int)zap->va);
    361 		sc->sc_addr = 0x8000;
    362 		sc->sc_copytodesc = lance_copytobuf_contig;
    363 		sc->sc_copyfromdesc = lance_copyfrombuf_contig;
    364 		sc->sc_copytobuf = lance_copytobuf_contig;
    365 		sc->sc_copyfrombuf = lance_copyfrombuf_contig;
    366 		sc->sc_zerobuf = lance_zerobuf_contig;
    367 		sc->sc_rdcsr = lerdcsr;
    368 		sc->sc_wrcsr = lewrcsr;
    369 		sc->sc_hwreset = NULL;
    370 		sc->sc_hwinit = NULL;
    371 		break;
    372 
    373 	    case 2167:
    374 		/* Village Tronic */
    375 		sc->sc_memsize = 32768;
    376 		sc->sc_enaddr[0] = 0x00;
    377 		sc->sc_enaddr[1] = 0x60;
    378 		sc->sc_enaddr[2] = 0x30;
    379 		lesc->sc_r1 = (struct lereg1 *)(0x0370 + (int)zap->va);
    380 		sc->sc_mem = (void *)(0x8000 + (int)zap->va);
    381 		sc->sc_addr = 0x8000;
    382 		sc->sc_copytodesc = ariadne_copytodesc_word;
    383 		sc->sc_copyfromdesc = ariadne_copyfromdesc_word;
    384 		sc->sc_copytobuf = ariadne_copytobuf_word;
    385 		sc->sc_copyfrombuf = ariadne_copyfrombuf_word;
    386 		sc->sc_zerobuf = ariadne_zerobuf_word;
    387 		sc->sc_rdcsr = ariadne_rdcsr;
    388 		sc->sc_wrcsr = ariadne_wrcsr;
    389 		sc->sc_hwreset = lepcnet_reset;
    390 		sc->sc_hwinit = ariadne_hwinit;
    391 		sc->sc_mediachange = ariadne_mediachange;
    392 		sc->sc_supmedia = lemedia_ariadne;
    393 		sc->sc_nsupmedia = NLEMEDIA_ARIADNE;
    394 		sc->sc_defaultmedia = IFM_ETHER | IFM_AUTO;
    395 		sc->sc_initmodemedia = 2;
    396 		break;
    397 
    398 	    default:
    399 		panic("le_zbus_attach: bad manid");
    400 	}
    401 
    402 	/*
    403 	 * Serial number for board is used as host ID.
    404 	 */
    405 	ser = (u_long)zap->serno;
    406 	sc->sc_enaddr[3] = (ser >> 16) & 0xff;
    407 	sc->sc_enaddr[4] = (ser >>  8) & 0xff;
    408 	sc->sc_enaddr[5] = (ser      ) & 0xff;
    409 
    410 	am7990_config(&lesc->sc_am7990);
    411 
    412 	lesc->sc_isr.isr_intr = am7990_intr;
    413 	lesc->sc_isr.isr_arg = sc;
    414 	lesc->sc_isr.isr_ipl = 2;
    415 	add_isr(&lesc->sc_isr);
    416 }
    417 
    418 
    419 integrate void
    420 ariadne_copytodesc_word(struct lance_softc *sc, void *from, int boff, int len)
    421 {
    422 	u_short *b1 = from;
    423 	volatile u_short *b2 = (u_short *)((u_char *)sc->sc_mem + boff);
    424 
    425 	for (len >>= 1; len > 0; len--)
    426 		*b2++ = ariadne_swapreg(*b1++);
    427 }
    428 
    429 integrate void
    430 ariadne_copyfromdesc_word(struct lance_softc *sc, void *to, int boff, int len)
    431 {
    432 	volatile u_short *b1 = (u_short *)((u_char *)sc->sc_mem + boff);
    433 	u_short *b2 = to;
    434 
    435 	for (len >>= 1; len > 0; len--)
    436 		*b2++ = ariadne_swapreg(*b1++);
    437 }
    438 
    439 #define	isodd(n)	((n) & 1)
    440 
    441 integrate void
    442 ariadne_copytobuf_word(struct lance_softc *sc, void *from, int boff, int len)
    443 {
    444 	u_char *a1 = from;
    445 	volatile u_char *a2 = (u_char *)sc->sc_mem + boff;
    446 	u_short *b1;
    447 	volatile u_short *b2;
    448 	int i;
    449 
    450 	if (len > 0 && isodd(boff)) {
    451 		/* adjust source pointer */
    452 		b1 = (u_short *)(a1 + 1);
    453 		/* compute aligned destination pointer */
    454 		b2 = (u_short *)(a2 + 1);
    455 		/* copy first unaligned byte to buf */
    456 		b2[-1] = (b2[-1] & 0xff00) | *a1;
    457 		--len;
    458 	} else {
    459 		/* destination is aligned or length is zero */
    460 		b1 = (u_short *)a1;
    461 		b2 = (u_short *)a2;
    462 	}
    463 
    464 	/* copy full words with aligned destination */
    465 	for (i = len >> 1; i > 0; i--)
    466 		*b2++ = *b1++;
    467 
    468 	/* copy remaining byte */
    469 	if (isodd(len))
    470 		*b2 = (*b2 & 0x00ff) | (*(u_char *)b1) << 8;
    471 }
    472 
    473 integrate void
    474 ariadne_copyfrombuf_word(struct lance_softc *sc, void *to, int boff, int len)
    475 {
    476 	volatile u_char *a1 = (u_char *)sc->sc_mem + boff;
    477 	u_char *a2 = to;
    478 	volatile u_short *b1;
    479 	u_short *b2;
    480 	int i;
    481 
    482 	if (len > 0 && isodd(boff)) {
    483 		/* compute aligned source pointer */
    484 		b1  = (u_short *)(a1 + 1);
    485 		/* adjust destination pointer (possibly unaligned) */
    486 		b2  = (u_short *)(a2 + 1);
    487 		/* copy first unaligned byte from buf */
    488 		*a2 = b1[-1];
    489 		--len;
    490 	} else {
    491 		/* source is aligned or length is zero */
    492 		b1 = (u_short *)a1;
    493 		b2 = (u_short *)a2;
    494 	}
    495 
    496 	/* copy full words with aligned source */
    497 	for (i = len >> 1; i > 0; i--)
    498 		*b2++ = *b1++;
    499 
    500 	/* copy remaining byte */
    501 	if (isodd(len))
    502 		*(u_char *)b2 = *b1 >> 8;
    503 }
    504 
    505 integrate void
    506 ariadne_zerobuf_word(struct lance_softc *sc, int boff, int len)
    507 {
    508 	volatile u_char *a1 = (u_char *)sc->sc_mem + boff;
    509 	volatile u_short *b1;
    510 	int i;
    511 
    512 	if (len > 0 && isodd(boff)) {
    513 		b1 = (u_short *)(a1 + 1);
    514 		b1[-1] &= 0xff00;
    515 		--len;
    516 	} else {
    517 		b1 = (u_short *)a1;
    518 	}
    519 
    520 	for (i = len >> 1; i > 0; i--)
    521 		*b1++ = 0;
    522 
    523 	if (isodd(len))
    524 		*b1 &= 0x00ff;
    525 }
    526