Home | History | Annotate | Line # | Download | only in dev
if_le.c revision 1.51
      1 /*	$NetBSD: if_le.c,v 1.51 2002/10/02 05:15:52 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. All advertising materials mentioning features or use of this software
     55  *    must display the following acknowledgement:
     56  *	This product includes software developed by the University of
     57  *	California, Berkeley and its contributors.
     58  * 4. Neither the name of the University nor the names of its contributors
     59  *    may be used to endorse or promote products derived from this software
     60  *    without specific prior written permission.
     61  *
     62  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     63  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     64  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     65  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     66  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     67  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     68  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     69  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     70  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     71  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     72  * SUCH DAMAGE.
     73  *
     74  *	@(#)if_le.c	8.2 (Berkeley) 11/16/93
     75  */
     76 
     77 #include <sys/cdefs.h>
     78 __KERNEL_RCSID(0, "$NetBSD: if_le.c,v 1.51 2002/10/02 05:15:52 thorpej Exp $");
     79 
     80 #include "opt_inet.h"
     81 #include "bpfilter.h"
     82 
     83 #include <sys/param.h>
     84 #include <sys/systm.h>
     85 #include <sys/mbuf.h>
     86 #include <sys/syslog.h>
     87 #include <sys/socket.h>
     88 #include <sys/device.h>
     89 
     90 #include <net/if.h>
     91 #include <net/if_ether.h>
     92 #include <net/if_media.h>
     93 
     94 #ifdef INET
     95 #include <netinet/in.h>
     96 #include <netinet/if_inarp.h>
     97 #endif
     98 
     99 #include <machine/autoconf.h>
    100 #include <machine/cpu.h>
    101 #include <machine/intr.h>
    102 
    103 #include <dev/ic/lancereg.h>
    104 #include <dev/ic/lancevar.h>
    105 #include <dev/ic/am7990reg.h>
    106 #include <dev/ic/am7990var.h>
    107 
    108 #include <hp300/dev/dioreg.h>
    109 #include <hp300/dev/diovar.h>
    110 #include <hp300/dev/diodevs.h>
    111 #include <hp300/dev/if_lereg.h>
    112 #include <hp300/dev/if_levar.h>
    113 
    114 #include "opt_useleds.h"
    115 
    116 #ifdef USELEDS
    117 #include <hp300/hp300/leds.h>
    118 #endif
    119 
    120 int	lematch __P((struct device *, struct cfdata *, void *));
    121 void	leattach __P((struct device *, struct device *, void *));
    122 
    123 CFATTACH_DECL(le, sizeof(struct le_softc),
    124     lematch, leattach, NULL, NULL);
    125 
    126 int	leintr __P((void *));
    127 
    128 #if defined(_KERNEL_OPT)
    129 #include "opt_ddb.h"
    130 #endif
    131 
    132 #ifdef DDB
    133 #define	integrate
    134 #define hide
    135 #else
    136 #define	integrate	static __inline
    137 #define hide		static
    138 #endif
    139 
    140 hide void le_copytobuf __P((struct lance_softc *, void *, int, int));
    141 hide void le_copyfrombuf __P((struct lance_softc *, void *, int, int));
    142 hide void le_zerobuf __P((struct lance_softc *, int, int));
    143 
    144 /* offsets for:	   ID,   REGS,    MEM,  NVRAM */
    145 int	lestd[] = { 0, 0x4000, 0x8000, 0xC008 };
    146 
    147 hide void lewrcsr __P((struct lance_softc *, u_int16_t, u_int16_t));
    148 hide u_int16_t lerdcsr __P((struct lance_softc *, u_int16_t));
    149 
    150 hide void
    151 lewrcsr(sc, port, val)
    152 	struct lance_softc *sc;
    153 	u_int16_t port, val;
    154 {
    155 	struct le_softc *lesc = (struct le_softc *)sc;
    156 	bus_space_tag_t bst = lesc->sc_bst;
    157 	bus_space_handle_t bsh0 = lesc->sc_bsh0;
    158 	bus_space_handle_t bsh1 = lesc->sc_bsh1;
    159 
    160 	do {
    161 		bus_space_write_2(bst, bsh1, LER1_RAP, port);
    162 	} while ((bus_space_read_1(bst, bsh0, LER0_STATUS) & LE_ACK) == 0);
    163 	do {
    164 		bus_space_write_2(bst, bsh1, LER1_RDP, val);
    165 	} while ((bus_space_read_1(bst, bsh0, LER0_STATUS) & LE_ACK) == 0);
    166 }
    167 
    168 hide u_int16_t
    169 lerdcsr(sc, port)
    170 	struct lance_softc *sc;
    171 	u_int16_t port;
    172 {
    173 	struct le_softc *lesc = (struct le_softc *)sc;
    174 	bus_space_tag_t bst = lesc->sc_bst;
    175 	bus_space_handle_t bsh0 = lesc->sc_bsh0;
    176 	bus_space_handle_t bsh1 = lesc->sc_bsh1;
    177 	u_int16_t val;
    178 
    179 	do {
    180 		bus_space_write_2(bst, bsh1, LER1_RAP, port);
    181 	} while ((bus_space_read_1(bst, bsh0, LER0_STATUS) & LE_ACK) == 0);
    182 	do {
    183 		val = bus_space_read_2(bst, bsh1, LER1_RDP);
    184 	} while ((bus_space_read_1(bst, bsh0, LER0_STATUS) & LE_ACK) == 0);
    185 
    186 	return (val);
    187 }
    188 
    189 int
    190 lematch(parent, match, aux)
    191 	struct device *parent;
    192 	struct cfdata *match;
    193 	void *aux;
    194 {
    195 	struct dio_attach_args *da = aux;
    196 
    197 	if (da->da_id == DIO_DEVICE_ID_LAN)
    198 		return (1);
    199 	return (0);
    200 }
    201 
    202 /*
    203  * Interface exists: make available by filling in network interface
    204  * record.  System will initialize the interface when it is ready
    205  * to accept packets.
    206  */
    207 void
    208 leattach(parent, self, aux)
    209 	struct device *parent, *self;
    210 	void *aux;
    211 {
    212 	struct dio_attach_args *da = aux;
    213 	struct le_softc *lesc = (struct le_softc *)self;
    214 	struct lance_softc *sc = &lesc->sc_am7990.lsc;
    215 	bus_space_tag_t bst = da->da_bst;
    216 	bus_space_handle_t bsh0, bsh1, bsh2;
    217 	bus_size_t offset;
    218 	int i, ipl;
    219 
    220 	if (bus_space_map(bst, (bus_addr_t)dio_scodetopa(da->da_scode),
    221 	    da->da_size, 0, &bsh0)) {
    222 		printf("\n%s: can't map LANCE registers\n",
    223 		    sc->sc_dev.dv_xname);
    224 		return;
    225 	}
    226 
    227 	if (bus_space_subregion(bst, bsh0, lestd[1], LER1_SIZE, &bsh1)) {
    228 		printf("\n%s: can't subregion LANCE space\n",
    229 		    sc->sc_dev.dv_xname);
    230 		return;
    231 	}
    232 
    233 	if (bus_space_subregion(bst, bsh0, lestd[2], LE_BUFSIZE, &bsh2)) {
    234 		printf("\n%s: can't subregion buffer space\n",
    235 		    sc->sc_dev.dv_xname);
    236 		return;
    237 	}
    238 
    239 	lesc->sc_bst = bst;
    240 	lesc->sc_bsh0 = bsh0;
    241 	lesc->sc_bsh1 = bsh1;
    242 	lesc->sc_bsh2 = bsh2;
    243 
    244 	bus_space_write_1(bst, bsh0, LER0_ID, 0xff);
    245 	DELAY(100);
    246 
    247 	ipl = DIO_IPL((caddr_t)bsh0);			/* XXX */
    248 	printf(" ipl %d", ipl);
    249 
    250 	sc->sc_conf3 = LE_C3_BSWP;
    251 	sc->sc_addr = 0;
    252 	sc->sc_memsize = LE_BUFSIZE;
    253 
    254 	/*
    255 	 * Read the ethernet address off the board, one nibble at a time.
    256 	 */
    257 	offset = lestd[3];
    258 	for (i = 0; i < sizeof(sc->sc_enaddr); i++) {
    259 		sc->sc_enaddr[i] =
    260 		    (bus_space_read_1(bst, bsh0, ++offset) & 0xf) << 4;
    261 		offset++;
    262 		sc->sc_enaddr[i] |=
    263 		    (bus_space_read_1(bst, bsh0, ++offset) & 0xf);
    264 		offset++;
    265 	}
    266 
    267 	sc->sc_copytodesc = le_copytobuf;
    268 	sc->sc_copyfromdesc = le_copyfrombuf;
    269 	sc->sc_copytobuf = le_copytobuf;
    270 	sc->sc_copyfrombuf = le_copyfrombuf;
    271 	sc->sc_zerobuf = le_zerobuf;
    272 
    273 	sc->sc_rdcsr = lerdcsr;
    274 	sc->sc_wrcsr = lewrcsr;
    275 	sc->sc_hwinit = NULL;
    276 
    277 	am7990_config(&lesc->sc_am7990);
    278 
    279 	/* Establish the interrupt handler. */
    280 	(void) dio_intr_establish(leintr, sc, ipl, IPL_NET);
    281 	bus_space_write_1(bst, bsh0, LER0_STATUS, LE_IE);
    282 }
    283 
    284 int
    285 leintr(arg)
    286 	void *arg;
    287 {
    288 	struct lance_softc *sc = arg;
    289 #ifdef USELEDS
    290 	u_int16_t isr;
    291 
    292 	isr = lerdcsr(sc, LE_CSR0);
    293 
    294 	if ((isr & LE_C0_INTR) == 0)
    295 		return (0);
    296 
    297 	if (isr & LE_C0_RINT)
    298 		ledcontrol(0, 0, LED_LANRCV);
    299 
    300 	if (isr & LE_C0_TINT)
    301 		ledcontrol(0, 0, LED_LANXMT);
    302 #endif /* USELEDS */
    303 
    304 	return (am7990_intr(sc));
    305 }
    306 
    307 hide void
    308 le_copytobuf(sc, from, boff, len)
    309 	struct lance_softc *sc;
    310 	void *from;
    311 	int boff, len;
    312 {
    313 	struct le_softc *lesc = (struct le_softc *)sc;
    314 
    315 	bus_space_write_region_1(lesc->sc_bst, lesc->sc_bsh2, boff, from, len);
    316 }
    317 
    318 hide void
    319 le_copyfrombuf(sc, to, boff, len)
    320 	struct lance_softc *sc;
    321 	void *to;
    322 	int boff, len;
    323 {
    324 	struct le_softc *lesc = (struct le_softc *)sc;
    325 
    326 	bus_space_read_region_1(lesc->sc_bst, lesc->sc_bsh2, boff, to, len);
    327 }
    328 
    329 hide void
    330 le_zerobuf(sc, boff, len)
    331 	struct lance_softc *sc;
    332 	int boff, len;
    333 {
    334 	struct le_softc *lesc = (struct le_softc *)sc;
    335 
    336 	bus_space_set_region_1(lesc->sc_bst, lesc->sc_bsh2, boff, 0, len);
    337 }
    338