Home | History | Annotate | Line # | Download | only in dev
if_le.c revision 1.56
      1 /*	$NetBSD: if_le.c,v 1.56 2004/08/28 17:37:01 thorpej Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 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.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the NetBSD
     21  *	Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*-
     40  * Copyright (c) 1992, 1993
     41  *	The Regents of the University of California.  All rights reserved.
     42  *
     43  * This code is derived from software contributed to Berkeley by
     44  * Ralph Campbell and Rick Macklem.
     45  *
     46  * Redistribution and use in source and binary forms, with or without
     47  * modification, are permitted provided that the following conditions
     48  * are met:
     49  * 1. Redistributions of source code must retain the above copyright
     50  *    notice, this list of conditions and the following disclaimer.
     51  * 2. Redistributions in binary form must reproduce the above copyright
     52  *    notice, this list of conditions and the following disclaimer in the
     53  *    documentation and/or other materials provided with the distribution.
     54  * 3. Neither the name of the University nor the names of its contributors
     55  *    may be used to endorse or promote products derived from this software
     56  *    without specific prior written permission.
     57  *
     58  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     59  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     60  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     61  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     62  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     63  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     64  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     65  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     66  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     67  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     68  * SUCH DAMAGE.
     69  *
     70  *	@(#)if_le.c	8.2 (Berkeley) 11/16/93
     71  */
     72 
     73 #include <sys/cdefs.h>
     74 __KERNEL_RCSID(0, "$NetBSD: if_le.c,v 1.56 2004/08/28 17:37:01 thorpej Exp $");
     75 
     76 #include "opt_inet.h"
     77 #include "bpfilter.h"
     78 
     79 #include <sys/param.h>
     80 #include <sys/systm.h>
     81 #include <sys/device.h>
     82 
     83 #include <net/if.h>
     84 #include <net/if_ether.h>
     85 #include <net/if_media.h>
     86 
     87 #include <dev/ic/lancereg.h>
     88 #include <dev/ic/lancevar.h>
     89 #include <dev/ic/am7990var.h>
     90 
     91 #include <hp300/dev/diovar.h>
     92 #include <hp300/dev/diodevs.h>
     93 #include <hp300/dev/if_lereg.h>
     94 
     95 #include "opt_useleds.h"
     96 
     97 #ifdef USELEDS
     98 #include <hp300/hp300/leds.h>
     99 #endif
    100 
    101 struct  le_softc {
    102 	struct  am7990_softc sc_am7990; /* glue to MI code */
    103 
    104 	bus_space_tag_t sc_bst;
    105 
    106 	bus_space_handle_t sc_bsh0;	/* DIO registers */
    107 	bus_space_handle_t sc_bsh1;	/* LANCE registers */
    108 	bus_space_handle_t sc_bsh2;	/* buffer area */
    109 };
    110 
    111 static int	lematch(struct device *, struct cfdata *, void *);
    112 static void	leattach(struct device *, struct device *, void *);
    113 
    114 CFATTACH_DECL(le, sizeof(struct le_softc),
    115     lematch, leattach, NULL, NULL);
    116 
    117 static int	leintr(void *);
    118 
    119 #if defined(_KERNEL_OPT)
    120 #include "opt_ddb.h"
    121 #endif
    122 
    123 static void	le_copytobuf(struct lance_softc *, void *, int, int);
    124 static void	le_copyfrombuf(struct lance_softc *, void *, int, int);
    125 static void	le_zerobuf(struct lance_softc *, int, int);
    126 
    127 /* offsets for:	   ID,   REGS,    MEM,  NVRAM */
    128 static const int lestd[] = { 0, 0x4000, 0x8000, 0xC008 };
    129 
    130 static void
    131 lewrcsr(struct lance_softc *sc, u_int16_t port, u_int16_t val)
    132 {
    133 	struct le_softc *lesc = (struct le_softc *)sc;
    134 	bus_space_tag_t bst = lesc->sc_bst;
    135 	bus_space_handle_t bsh0 = lesc->sc_bsh0;
    136 	bus_space_handle_t bsh1 = lesc->sc_bsh1;
    137 
    138 	do {
    139 		bus_space_write_2(bst, bsh1, LER1_RAP, port);
    140 	} while ((bus_space_read_1(bst, bsh0, LER0_STATUS) & LE_ACK) == 0);
    141 	do {
    142 		bus_space_write_2(bst, bsh1, LER1_RDP, val);
    143 	} while ((bus_space_read_1(bst, bsh0, LER0_STATUS) & LE_ACK) == 0);
    144 }
    145 
    146 static u_int16_t
    147 lerdcsr(struct lance_softc *sc, u_int16_t port)
    148 {
    149 	struct le_softc *lesc = (struct le_softc *)sc;
    150 	bus_space_tag_t bst = lesc->sc_bst;
    151 	bus_space_handle_t bsh0 = lesc->sc_bsh0;
    152 	bus_space_handle_t bsh1 = lesc->sc_bsh1;
    153 	u_int16_t val;
    154 
    155 	do {
    156 		bus_space_write_2(bst, bsh1, LER1_RAP, port);
    157 	} while ((bus_space_read_1(bst, bsh0, LER0_STATUS) & LE_ACK) == 0);
    158 	do {
    159 		val = bus_space_read_2(bst, bsh1, LER1_RDP);
    160 	} while ((bus_space_read_1(bst, bsh0, LER0_STATUS) & LE_ACK) == 0);
    161 
    162 	return (val);
    163 }
    164 
    165 static int
    166 lematch(struct device *parent, struct cfdata *match, void *aux)
    167 {
    168 	struct dio_attach_args *da = aux;
    169 
    170 	if (da->da_id == DIO_DEVICE_ID_LAN)
    171 		return (1);
    172 	return (0);
    173 }
    174 
    175 /*
    176  * Interface exists: make available by filling in network interface
    177  * record.  System will initialize the interface when it is ready
    178  * to accept packets.
    179  */
    180 static void
    181 leattach(struct device *parent, struct device *self, void *aux)
    182 {
    183 	struct dio_attach_args *da = aux;
    184 	struct le_softc *lesc = (struct le_softc *)self;
    185 	struct lance_softc *sc = &lesc->sc_am7990.lsc;
    186 	bus_space_tag_t bst = da->da_bst;
    187 	bus_space_handle_t bsh0, bsh1, bsh2;
    188 	bus_size_t offset;
    189 	int i;
    190 
    191 	if (bus_space_map(bst, (bus_addr_t)dio_scodetopa(da->da_scode),
    192 	    da->da_size, 0, &bsh0)) {
    193 		printf("\n%s: can't map LANCE registers\n",
    194 		    sc->sc_dev.dv_xname);
    195 		return;
    196 	}
    197 
    198 	if (bus_space_subregion(bst, bsh0, lestd[1], LER1_SIZE, &bsh1)) {
    199 		printf("\n%s: can't subregion LANCE space\n",
    200 		    sc->sc_dev.dv_xname);
    201 		return;
    202 	}
    203 
    204 	if (bus_space_subregion(bst, bsh0, lestd[2], LE_BUFSIZE, &bsh2)) {
    205 		printf("\n%s: can't subregion buffer space\n",
    206 		    sc->sc_dev.dv_xname);
    207 		return;
    208 	}
    209 
    210 	lesc->sc_bst = bst;
    211 	lesc->sc_bsh0 = bsh0;
    212 	lesc->sc_bsh1 = bsh1;
    213 	lesc->sc_bsh2 = bsh2;
    214 
    215 	bus_space_write_1(bst, bsh0, LER0_ID, 0xff);
    216 	DELAY(100);
    217 
    218 	sc->sc_conf3 = LE_C3_BSWP;
    219 	sc->sc_addr = 0;
    220 	sc->sc_memsize = LE_BUFSIZE;
    221 
    222 	/*
    223 	 * Read the ethernet address off the board, one nibble at a time.
    224 	 */
    225 	offset = lestd[3];
    226 	for (i = 0; i < sizeof(sc->sc_enaddr); i++) {
    227 		sc->sc_enaddr[i] =
    228 		    (bus_space_read_1(bst, bsh0, ++offset) & 0xf) << 4;
    229 		offset++;
    230 		sc->sc_enaddr[i] |=
    231 		    (bus_space_read_1(bst, bsh0, ++offset) & 0xf);
    232 		offset++;
    233 	}
    234 
    235 	sc->sc_copytodesc = le_copytobuf;
    236 	sc->sc_copyfromdesc = le_copyfrombuf;
    237 	sc->sc_copytobuf = le_copytobuf;
    238 	sc->sc_copyfrombuf = le_copyfrombuf;
    239 	sc->sc_zerobuf = le_zerobuf;
    240 
    241 	sc->sc_rdcsr = lerdcsr;
    242 	sc->sc_wrcsr = lewrcsr;
    243 	sc->sc_hwinit = NULL;
    244 
    245 	am7990_config(&lesc->sc_am7990);
    246 
    247 	/* Establish the interrupt handler. */
    248 	(void) dio_intr_establish(leintr, sc, da->da_ipl, IPL_NET);
    249 	bus_space_write_1(bst, bsh0, LER0_STATUS, LE_IE);
    250 }
    251 
    252 static int
    253 leintr(void *arg)
    254 {
    255 	struct lance_softc *sc = arg;
    256 #ifdef USELEDS
    257 	u_int16_t isr;
    258 
    259 	isr = lerdcsr(sc, LE_CSR0);
    260 
    261 	if ((isr & LE_C0_INTR) == 0)
    262 		return (0);
    263 
    264 	if (isr & LE_C0_RINT)
    265 		ledcontrol(0, 0, LED_LANRCV);
    266 
    267 	if (isr & LE_C0_TINT)
    268 		ledcontrol(0, 0, LED_LANXMT);
    269 #endif /* USELEDS */
    270 
    271 	return (am7990_intr(sc));
    272 }
    273 
    274 static void
    275 le_copytobuf(struct lance_softc *sc, void *from, int boff, int len)
    276 {
    277 	struct le_softc *lesc = (struct le_softc *)sc;
    278 
    279 	bus_space_write_region_1(lesc->sc_bst, lesc->sc_bsh2, boff, from, len);
    280 }
    281 
    282 static void
    283 le_copyfrombuf(struct lance_softc *sc, void *to, int boff, int len)
    284 {
    285 	struct le_softc *lesc = (struct le_softc *)sc;
    286 
    287 	bus_space_read_region_1(lesc->sc_bst, lesc->sc_bsh2, boff, to, len);
    288 }
    289 
    290 static void
    291 le_zerobuf(struct lance_softc *sc, int boff, int len)
    292 {
    293 	struct le_softc *lesc = (struct le_softc *)sc;
    294 
    295 	bus_space_set_region_1(lesc->sc_bst, lesc->sc_bsh2, boff, 0, len);
    296 }
    297