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