Home | History | Annotate | Line # | Download | only in isa
if_le_isa.c revision 1.4
      1 /*	$NetBSD: if_le_isa.c,v 1.4 1996/10/10 22:05:04 christos Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1995 Charles M. Hannum.  All rights reserved.
      5  * Copyright (c) 1992, 1993
      6  *	The Regents of the University of California.  All rights reserved.
      7  *
      8  * This code is derived from software contributed to Berkeley by
      9  * Ralph Campbell and Rick Macklem.
     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 University of
     22  *	California, Berkeley and its contributors.
     23  * 4. Neither the name of the University nor the names of its contributors
     24  *    may be used to endorse or promote products derived from this software
     25  *    without specific prior written permission.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     28  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     29  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     30  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     31  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     32  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     33  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     34  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     35  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     36  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     37  * SUCH DAMAGE.
     38  *
     39  *	@(#)if_le.c	8.2 (Berkeley) 11/16/93
     40  */
     41 
     42 #include "bpfilter.h"
     43 
     44 #include <sys/param.h>
     45 #include <sys/systm.h>
     46 #include <sys/mbuf.h>
     47 #include <sys/syslog.h>
     48 #include <sys/socket.h>
     49 #include <sys/device.h>
     50 
     51 #include <net/if.h>
     52 
     53 #ifdef INET
     54 #include <netinet/in.h>
     55 #include <netinet/if_ether.h>
     56 #endif
     57 
     58 #include <vm/vm.h>
     59 
     60 #include <machine/cpu.h>
     61 #include <machine/intr.h>
     62 #include <machine/pio.h>
     63 
     64 #include <dev/isa/isareg.h>
     65 #include <dev/isa/isavar.h>
     66 #include <dev/isa/isadmavar.h>
     67 #include <i386/isa/isa_machdep.h>
     68 
     69 #include <dev/ic/am7990reg.h>
     70 #include <dev/ic/am7990var.h>
     71 
     72 #include <dev/isa/if_levar.h>
     73 
     74 static char *card_type[] =
     75     { "unknown", "BICC Isolan", "NE2100", "DEPCA", "PCnet-ISA" };
     76 
     77 int le_isa_probe __P((struct device *, void *, void *));
     78 void le_isa_attach __P((struct device *, struct device *, void *));
     79 
     80 struct cfattach le_isa_ca = {
     81 	sizeof(struct le_softc), le_isa_probe, le_isa_attach
     82 };
     83 
     84 int depca_isa_probe __P((struct le_softc *, struct isa_attach_args *));
     85 int ne2100_isa_probe __P((struct le_softc *, struct isa_attach_args *));
     86 int bicc_isa_probe __P((struct le_softc *, struct isa_attach_args *));
     87 int lance_isa_probe __P((struct am7990_softc *));
     88 
     89 int le_isa_intredge __P((void *));
     90 
     91 hide void le_isa_wrcsr __P((struct am7990_softc *, u_int16_t, u_int16_t));
     92 hide u_int16_t le_isa_rdcsr __P((struct am7990_softc *, u_int16_t));
     93 
     94 hide void
     95 le_isa_wrcsr(sc, port, val)
     96 	struct am7990_softc *sc;
     97 	u_int16_t port, val;
     98 {
     99 
    100 	outw(((struct le_softc *)sc)->sc_rap, port);
    101 	outw(((struct le_softc *)sc)->sc_rdp, val);
    102 }
    103 
    104 hide u_int16_t
    105 le_isa_rdcsr(sc, port)
    106 	struct am7990_softc *sc;
    107 	u_int16_t port;
    108 {
    109 	u_int16_t val;
    110 
    111 	outw(((struct le_softc *)sc)->sc_rap, port);
    112 	val = inw(((struct le_softc *)sc)->sc_rdp);
    113 	return (val);
    114 }
    115 
    116 int
    117 le_isa_probe(parent, match, aux)
    118 	struct device *parent;
    119 	void *match, *aux;
    120 {
    121 	struct le_softc *lesc = match;
    122 	struct isa_attach_args *ia = aux;
    123 
    124 	if (bicc_isa_probe(lesc, ia))
    125 		return (1);
    126 	if (ne2100_isa_probe(lesc, ia))
    127 		return (1);
    128 	if (depca_isa_probe(lesc, ia))
    129 		return (1);
    130 
    131 	return (0);
    132 }
    133 
    134 int
    135 depca_isa_probe(lesc, ia)
    136 	struct le_softc *lesc;
    137 	struct isa_attach_args *ia;
    138 {
    139 	struct am7990_softc *sc = &lesc->sc_am7990;
    140 	int iobase = ia->ia_iobase, port;
    141 #if 0
    142 	u_long sum, rom_sum;
    143 	u_char x;
    144 #endif
    145 	int i;
    146 
    147 	lesc->sc_rap = iobase + DEPCA_RAP;
    148 	lesc->sc_rdp = iobase + DEPCA_RDP;
    149 	lesc->sc_card = DEPCA;
    150 
    151 	if (lance_isa_probe(sc) == 0)
    152 		return 0;
    153 
    154 	outb(iobase + DEPCA_CSR, DEPCA_CSR_DUM);
    155 
    156 	/*
    157 	 * Extract the physical MAC address from the ROM.
    158 	 *
    159 	 * The address PROM is 32 bytes wide, and we access it through
    160 	 * a single I/O port.  On each read, it rotates to the next
    161 	 * position.  We find the ethernet address by looking for a
    162 	 * particular sequence of bytes (0xff, 0x00, 0x55, 0xaa, 0xff,
    163 	 * 0x00, 0x55, 0xaa), and then reading the next 8 bytes (the
    164 	 * ethernet address and a checksum).
    165 	 *
    166 	 * It appears that the PROM can be at one of two locations, so
    167 	 * we just try both.
    168 	 */
    169 	port = iobase + DEPCA_ADP;
    170 	for (i = 0; i < 32; i++)
    171 		if (inb(port) == 0xff && inb(port) == 0x00 &&
    172 		    inb(port) == 0x55 && inb(port) == 0xaa &&
    173 		    inb(port) == 0xff && inb(port) == 0x00 &&
    174 		    inb(port) == 0x55 && inb(port) == 0xaa)
    175 			goto found;
    176 	port = iobase + DEPCA_ADP + 1;
    177 	for (i = 0; i < 32; i++)
    178 		if (inb(port) == 0xff && inb(port) == 0x00 &&
    179 		    inb(port) == 0x55 && inb(port) == 0xaa &&
    180 		    inb(port) == 0xff && inb(port) == 0x00 &&
    181 		    inb(port) == 0x55 && inb(port) == 0xaa)
    182 			goto found;
    183 	kprintf("%s: address not found\n", sc->sc_dev.dv_xname);
    184 	return 0;
    185 
    186 found:
    187 	for (i = 0; i < sizeof(sc->sc_arpcom.ac_enaddr); i++)
    188 		sc->sc_arpcom.ac_enaddr[i] = inb(port);
    189 
    190 #if 0
    191 	sum =
    192 	    (sc->sc_arpcom.ac_enaddr[0] <<  2) +
    193 	    (sc->sc_arpcom.ac_enaddr[1] << 10) +
    194 	    (sc->sc_arpcom.ac_enaddr[2] <<  1) +
    195 	    (sc->sc_arpcom.ac_enaddr[3] <<  9) +
    196 	    (sc->sc_arpcom.ac_enaddr[4] <<  0) +
    197 	    (sc->sc_arpcom.ac_enaddr[5] <<  8);
    198 	sum = (sum & 0xffff) + (sum >> 16);
    199 	sum = (sum & 0xffff) + (sum >> 16);
    200 
    201 	rom_sum = inb(port);
    202 	rom_sum |= inb(port) << 8;
    203 
    204 	if (sum != rom_sum) {
    205 		kprintf("%s: checksum mismatch; calculated %04x != read %04x",
    206 		    sc->sc_dev.dv_xname, sum, rom_sum);
    207 		return 0;
    208 	}
    209 #endif
    210 
    211 	outb(iobase + DEPCA_CSR, DEPCA_CSR_NORMAL);
    212 
    213 	ia->ia_iosize = 16;
    214 	ia->ia_drq = DRQUNK;
    215 	return 1;
    216 }
    217 
    218 int
    219 ne2100_isa_probe(lesc, ia)
    220 	struct le_softc *lesc;
    221 	struct isa_attach_args *ia;
    222 {
    223 	struct am7990_softc *sc = &lesc->sc_am7990;
    224 	int iobase = ia->ia_iobase;
    225 	int i;
    226 
    227 	lesc->sc_rap = iobase + NE2100_RAP;
    228 	lesc->sc_rdp = iobase + NE2100_RDP;
    229 	lesc->sc_card = NE2100;
    230 
    231 	if (lance_isa_probe(sc) == 0)
    232 		return 0;
    233 
    234 	/*
    235 	 * Extract the physical MAC address from the ROM.
    236 	 */
    237 	for (i = 0; i < sizeof(sc->sc_arpcom.ac_enaddr); i++)
    238 		sc->sc_arpcom.ac_enaddr[i] = inb(iobase + i);
    239 
    240 	ia->ia_iosize = 24;
    241 	return 1;
    242 }
    243 
    244 int
    245 bicc_isa_probe(lesc, ia)
    246 	struct le_softc *lesc;
    247 	struct isa_attach_args *ia;
    248 {
    249 	struct am7990_softc *sc = &lesc->sc_am7990;
    250 	int iobase = ia->ia_iobase;
    251 	int i;
    252 
    253 	lesc->sc_rap = iobase + BICC_RAP;
    254 	lesc->sc_rdp = iobase + BICC_RDP;
    255 	lesc->sc_card = BICC;
    256 
    257 	if (lance_isa_probe(sc) == 0)
    258 		return 0;
    259 
    260 	/*
    261 	 * Extract the physical MAC address from the ROM.
    262 	 */
    263 	for (i = 0; i < sizeof(sc->sc_arpcom.ac_enaddr); i++)
    264 		sc->sc_arpcom.ac_enaddr[i] = inb(iobase + i * 2);
    265 
    266 	ia->ia_iosize = 16;
    267 	return 1;
    268 }
    269 
    270 /*
    271  * Determine which chip is present on the card.
    272  */
    273 int
    274 lance_isa_probe(sc)
    275 	struct am7990_softc *sc;
    276 {
    277 
    278 	/* Stop the LANCE chip and put it in a known state. */
    279 	le_isa_wrcsr(sc, LE_CSR0, LE_C0_STOP);
    280 	delay(100);
    281 
    282 	if (le_isa_rdcsr(sc, LE_CSR0) != LE_C0_STOP)
    283 		return 0;
    284 
    285 	le_isa_wrcsr(sc, LE_CSR3, sc->sc_conf3);
    286 	return 1;
    287 }
    288 
    289 void
    290 le_isa_attach(parent, self, aux)
    291 	struct device *parent, *self;
    292 	void *aux;
    293 {
    294 	struct le_softc *lesc = (void *)self;
    295 	struct am7990_softc *sc = &lesc->sc_am7990;
    296 	struct isa_attach_args *ia = aux;
    297 
    298 	kprintf(": %s Ethernet\n", card_type[lesc->sc_card]);
    299 
    300 	if (lesc->sc_card == DEPCA) {
    301 		u_char *mem, val;
    302 		int i;
    303 
    304 		mem = sc->sc_mem = ISA_HOLE_VADDR(ia->ia_maddr);
    305 
    306 		val = 0xff;
    307 		for (;;) {
    308 			for (i = 0; i < ia->ia_msize; i++)
    309 				mem[i] = val;
    310 			for (i = 0; i < ia->ia_msize; i++)
    311 				if (mem[i] != val) {
    312 					kprintf("%s: failed to clear memory\n",
    313 					    sc->sc_dev.dv_xname);
    314 					return;
    315 				}
    316 			if (val == 0x00)
    317 				break;
    318 			val -= 0x55;
    319 		}
    320 
    321 		sc->sc_conf3 = LE_C3_ACON;
    322 		sc->sc_addr = 0;
    323 		sc->sc_memsize = ia->ia_msize;
    324 	} else {
    325 		sc->sc_mem = malloc(16384, M_DEVBUF, M_NOWAIT);
    326 		if (sc->sc_mem == 0) {
    327 			kprintf("%s: couldn't allocate memory for card\n",
    328 			    sc->sc_dev.dv_xname);
    329 			return;
    330 		}
    331 
    332 		sc->sc_conf3 = 0;
    333 		sc->sc_addr = kvtop(sc->sc_mem);
    334 		sc->sc_memsize = 16384;
    335 	}
    336 
    337 	sc->sc_copytodesc = am7990_copytobuf_contig;
    338 	sc->sc_copyfromdesc = am7990_copyfrombuf_contig;
    339 	sc->sc_copytobuf = am7990_copytobuf_contig;
    340 	sc->sc_copyfrombuf = am7990_copyfrombuf_contig;
    341 	sc->sc_zerobuf = am7990_zerobuf_contig;
    342 
    343 	sc->sc_rdcsr = le_isa_rdcsr;
    344 	sc->sc_wrcsr = le_isa_wrcsr;
    345 	sc->sc_hwinit = NULL;
    346 
    347 	kprintf("%s", sc->sc_dev.dv_xname);
    348 	am7990_config(sc);
    349 
    350 	if (ia->ia_drq != DRQUNK)
    351 		isa_dmacascade(ia->ia_drq);
    352 
    353 	lesc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq, IST_EDGE,
    354 	    IPL_NET, le_isa_intredge, sc);
    355 }
    356 
    357 /*
    358  * Controller interrupt.
    359  */
    360 int
    361 le_isa_intredge(arg)
    362 	void *arg;
    363 {
    364 
    365 	if (am7990_intr(arg) == 0)
    366 		return (0);
    367 	for (;;)
    368 		if (am7990_intr(arg) == 0)
    369 			return (1);
    370 }
    371