Home | History | Annotate | Line # | Download | only in isa
if_le_isa.c revision 1.6
      1 /*	$NetBSD: if_le_isa.c,v 1.6 1996/12/02 05:44:17 thorpej 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/bus.h>
     63 
     64 #include <dev/isa/isareg.h>
     65 #include <dev/isa/isavar.h>
     66 #include <dev/isa/isadmavar.h>
     67 
     68 #include <dev/ic/am7990reg.h>
     69 #include <dev/ic/am7990var.h>
     70 
     71 #include <dev/isa/if_levar.h>
     72 
     73 static char *card_type[] =
     74     { "unknown", "BICC Isolan", "NE2100", "DEPCA", "PCnet-ISA" };
     75 
     76 int le_isa_probe __P((struct device *, void *, void *));
     77 void le_isa_attach __P((struct device *, struct device *, void *));
     78 
     79 struct cfattach le_isa_ca = {
     80 	sizeof(struct le_softc), le_isa_probe, le_isa_attach
     81 };
     82 
     83 int depca_isa_probe __P((struct le_softc *, struct isa_attach_args *));
     84 int ne2100_isa_probe __P((struct le_softc *, struct isa_attach_args *));
     85 int bicc_isa_probe __P((struct le_softc *, struct isa_attach_args *));
     86 int lance_isa_probe __P((struct am7990_softc *));
     87 
     88 int le_isa_intredge __P((void *));
     89 
     90 hide void le_isa_wrcsr __P((struct am7990_softc *, u_int16_t, u_int16_t));
     91 hide u_int16_t le_isa_rdcsr __P((struct am7990_softc *, u_int16_t));
     92 
     93 void	depca_copytobuf __P((struct am7990_softc *, void *, int, int));
     94 void	depca_copyfrombuf __P((struct am7990_softc *, void *, int, int));
     95 void	depca_zerobuf __P((struct am7990_softc *, int, int));
     96 
     97 hide void
     98 le_isa_wrcsr(sc, port, val)
     99 	struct am7990_softc *sc;
    100 	u_int16_t port, val;
    101 {
    102 	struct le_softc *lesc = (struct le_softc *)sc;
    103 	bus_space_tag_t iot = lesc->sc_iot;
    104 	bus_space_handle_t ioh = lesc->sc_ioh;
    105 
    106 	bus_space_write_2(iot, ioh, lesc->sc_rap, port);
    107 	bus_space_write_2(iot, ioh, lesc->sc_rdp, val);
    108 }
    109 
    110 hide u_int16_t
    111 le_isa_rdcsr(sc, port)
    112 	struct am7990_softc *sc;
    113 	u_int16_t port;
    114 {
    115 	struct le_softc *lesc = (struct le_softc *)sc;
    116 	bus_space_tag_t iot = lesc->sc_iot;
    117 	bus_space_handle_t ioh = lesc->sc_ioh;
    118 	u_int16_t val;
    119 
    120 	bus_space_write_2(iot, ioh, lesc->sc_rap, port);
    121 	val = bus_space_read_2(iot, ioh, lesc->sc_rdp);
    122 	return (val);
    123 }
    124 
    125 int
    126 le_isa_probe(parent, match, aux)
    127 	struct device *parent;
    128 	void *match, *aux;
    129 {
    130 	struct le_softc *lesc = match;
    131 	struct isa_attach_args *ia = aux;
    132 
    133 	if (bicc_isa_probe(lesc, ia))
    134 		return (1);
    135 	if (ne2100_isa_probe(lesc, ia))
    136 		return (1);
    137 	if (depca_isa_probe(lesc, ia))
    138 		return (1);
    139 
    140 	return (0);
    141 }
    142 
    143 int
    144 depca_isa_probe(lesc, ia)
    145 	struct le_softc *lesc;
    146 	struct isa_attach_args *ia;
    147 {
    148 	struct am7990_softc *sc = &lesc->sc_am7990;
    149 	int iobase = ia->ia_iobase, port;
    150 	bus_space_tag_t iot = ia->ia_iot;
    151 	bus_space_handle_t ioh;
    152 	bus_space_handle_t memh;
    153 #if 0
    154 	u_int32_t sum, rom_sum;
    155 	u_int8_t x;
    156 #endif
    157 	int i;
    158 
    159 	/* Map i/o space. */
    160 	if (bus_space_map(iot, iobase, 16, 0, &ioh))
    161 		return 0;
    162 
    163 	if (ia->ia_maddr == MADDRUNK || ia->ia_msize == -1)
    164 		goto bad;
    165 
    166 	/* Map card RAM. */
    167 	if (bus_space_map(ia->ia_memt, ia->ia_maddr, ia->ia_msize,
    168 	    0, &memh))
    169 		goto bad;
    170 
    171 	/* Just needed to check mapability; don't need it anymore. */
    172 	bus_space_unmap(ia->ia_memt, ia->ia_maddr, ia->ia_msize);
    173 
    174 	lesc->sc_rap = DEPCA_RAP;
    175 	lesc->sc_rdp = DEPCA_RDP;
    176 	lesc->sc_card = DEPCA;
    177 
    178 	if (lance_isa_probe(sc) == 0)
    179 		goto bad;
    180 
    181 	bus_space_write_1(iot, ioh, DEPCA_CSR, DEPCA_CSR_DUM);
    182 
    183 	/*
    184 	 * Extract the physical MAC address from the ROM.
    185 	 *
    186 	 * The address PROM is 32 bytes wide, and we access it through
    187 	 * a single I/O port.  On each read, it rotates to the next
    188 	 * position.  We find the ethernet address by looking for a
    189 	 * particular sequence of bytes (0xff, 0x00, 0x55, 0xaa, 0xff,
    190 	 * 0x00, 0x55, 0xaa), and then reading the next 8 bytes (the
    191 	 * ethernet address and a checksum).
    192 	 *
    193 	 * It appears that the PROM can be at one of two locations, so
    194 	 * we just try both.
    195 	 */
    196 	port = DEPCA_ADP;
    197 	for (i = 0; i < 32; i++)
    198 		if (bus_space_read_1(iot, ioh, port) == 0xff &&
    199 		    bus_space_read_1(iot, ioh, port) == 0x00 &&
    200 		    bus_space_read_1(iot, ioh, port) == 0x55 &&
    201 		    bus_space_read_1(iot, ioh, port) == 0xaa &&
    202 		    bus_space_read_1(iot, ioh, port) == 0xff &&
    203 		    bus_space_read_1(iot, ioh, port) == 0x00 &&
    204 		    bus_space_read_1(iot, ioh, port) == 0x55 &&
    205 		    bus_space_read_1(iot, ioh, port) == 0xaa)
    206 			goto found;
    207 	port = DEPCA_ADP + 1;
    208 	for (i = 0; i < 32; i++)
    209 		if (bus_space_read_1(iot, ioh, port) == 0xff &&
    210 		    bus_space_read_1(iot, ioh, port) == 0x00 &&
    211 		    bus_space_read_1(iot, ioh, port) == 0x55 &&
    212 		    bus_space_read_1(iot, ioh, port) == 0xaa &&
    213 		    bus_space_read_1(iot, ioh, port) == 0xff &&
    214 		    bus_space_read_1(iot, ioh, port) == 0x00 &&
    215 		    bus_space_read_1(iot, ioh, port) == 0x55 &&
    216 		    bus_space_read_1(iot, ioh, port) == 0xaa)
    217 			goto found;
    218 	printf("%s: address not found\n", sc->sc_dev.dv_xname);
    219 	goto bad;
    220 
    221 found:
    222 	for (i = 0; i < sizeof(sc->sc_arpcom.ac_enaddr); i++)
    223 		sc->sc_arpcom.ac_enaddr[i] = bus_space_read_1(iot, ioh, port);
    224 
    225 #if 0
    226 	sum =
    227 	    (sc->sc_arpcom.ac_enaddr[0] <<  2) +
    228 	    (sc->sc_arpcom.ac_enaddr[1] << 10) +
    229 	    (sc->sc_arpcom.ac_enaddr[2] <<  1) +
    230 	    (sc->sc_arpcom.ac_enaddr[3] <<  9) +
    231 	    (sc->sc_arpcom.ac_enaddr[4] <<  0) +
    232 	    (sc->sc_arpcom.ac_enaddr[5] <<  8);
    233 	sum = (sum & 0xffff) + (sum >> 16);
    234 	sum = (sum & 0xffff) + (sum >> 16);
    235 
    236 	rom_sum = bus_space_read_1(iot, ioh, port);
    237 	rom_sum |= bus_space_read_1(iot, ioh, port) << 8;
    238 
    239 	if (sum != rom_sum) {
    240 		printf("%s: checksum mismatch; calculated %04x != read %04x",
    241 		    sc->sc_dev.dv_xname, sum, rom_sum);
    242 		goto bad;
    243 	}
    244 #endif
    245 
    246 	bus_space_write_1(iot, ioh, DEPCA_CSR, DEPCA_CSR_NORMAL);
    247 
    248 	/*
    249 	 * XXX INDIRECT BROKENNESS!
    250 	 * XXX Should always unmap, and re-map in if_le_isa_attach().
    251 	 */
    252 	lesc->sc_iot = iot;
    253 	lesc->sc_ioh = ioh;
    254 
    255 	ia->ia_iosize = 16;
    256 	ia->ia_drq = DRQUNK;
    257 	return 1;
    258 
    259  bad:
    260 	bus_space_unmap(iot, ioh, 16);
    261 	return 0;
    262 }
    263 
    264 int
    265 ne2100_isa_probe(lesc, ia)
    266 	struct le_softc *lesc;
    267 	struct isa_attach_args *ia;
    268 {
    269 	struct am7990_softc *sc = &lesc->sc_am7990;
    270 	int iobase = ia->ia_iobase;
    271 	bus_space_tag_t iot = ia->ia_iot;
    272 	bus_space_handle_t ioh;
    273 	int i;
    274 
    275 	/* Map i/o space. */
    276 	if (bus_space_map(iot, iobase, 24, 0, &ioh))
    277 		return 0;
    278 
    279 	lesc->sc_rap = NE2100_RAP;
    280 	lesc->sc_rdp = NE2100_RDP;
    281 	lesc->sc_card = NE2100;
    282 
    283 	if (lance_isa_probe(sc) == 0) {
    284 		bus_space_unmap(iot, ioh, 24);
    285 		return 0;
    286 	}
    287 
    288 	/*
    289 	 * Extract the physical MAC address from the ROM.
    290 	 */
    291 	for (i = 0; i < sizeof(sc->sc_arpcom.ac_enaddr); i++)
    292 		sc->sc_arpcom.ac_enaddr[i] = bus_space_read_1(iot, ioh, i);
    293 
    294 	/*
    295 	 * XXX INDIRECT BROKENNESS!
    296 	 * XXX Should always unmap, and re-map in if_le_isa_attach().
    297 	 */
    298 	lesc->sc_iot = iot;
    299 	lesc->sc_ioh = ioh;
    300 
    301 	ia->ia_iosize = 24;
    302 	return 1;
    303 }
    304 
    305 int
    306 bicc_isa_probe(lesc, ia)
    307 	struct le_softc *lesc;
    308 	struct isa_attach_args *ia;
    309 {
    310 	struct am7990_softc *sc = &lesc->sc_am7990;
    311 	int iobase = ia->ia_iobase;
    312 	bus_space_tag_t iot = ia->ia_iot;
    313 	bus_space_handle_t ioh;
    314 	int i;
    315 
    316 	/* Map i/o space. */
    317 	if (bus_space_map(iot, iobase, 16, 0, &ioh))
    318 		return 0;
    319 
    320 	lesc->sc_rap = BICC_RAP;
    321 	lesc->sc_rdp = BICC_RDP;
    322 	lesc->sc_card = BICC;
    323 
    324 	if (lance_isa_probe(sc) == 0) {
    325 		bus_space_unmap(iot, ioh, 16);
    326 		return 0;
    327 	}
    328 
    329 	/*
    330 	 * Extract the physical MAC address from the ROM.
    331 	 */
    332 	for (i = 0; i < sizeof(sc->sc_arpcom.ac_enaddr); i++)
    333 		sc->sc_arpcom.ac_enaddr[i] = bus_space_read_1(iot, ioh, i * 2);
    334 
    335 	/*
    336 	 * XXX INDIRECT BROKENNESS!
    337 	 * XXX Should always unmap, and re-map in if_le_isa_attach().
    338 	 */
    339 	lesc->sc_iot = iot;
    340 	lesc->sc_ioh = ioh;
    341 
    342 	ia->ia_iosize = 16;
    343 	return 1;
    344 }
    345 
    346 /*
    347  * Determine which chip is present on the card.
    348  */
    349 int
    350 lance_isa_probe(sc)
    351 	struct am7990_softc *sc;
    352 {
    353 
    354 	/* Stop the LANCE chip and put it in a known state. */
    355 	le_isa_wrcsr(sc, LE_CSR0, LE_C0_STOP);
    356 	delay(100);
    357 
    358 	if (le_isa_rdcsr(sc, LE_CSR0) != LE_C0_STOP)
    359 		return 0;
    360 
    361 	le_isa_wrcsr(sc, LE_CSR3, sc->sc_conf3);
    362 	return 1;
    363 }
    364 
    365 void
    366 le_isa_attach(parent, self, aux)
    367 	struct device *parent, *self;
    368 	void *aux;
    369 {
    370 	struct le_softc *lesc = (void *)self;
    371 	struct am7990_softc *sc = &lesc->sc_am7990;
    372 	struct isa_attach_args *ia = aux;
    373 	bus_space_tag_t iot = ia->ia_iot;
    374 	bus_space_tag_t memt = ia->ia_memt;
    375 	bus_space_handle_t memh;
    376 
    377 	printf(": %s Ethernet\n", card_type[lesc->sc_card]);
    378 
    379 	lesc->sc_iot = iot;
    380 	lesc->sc_memt = memt;
    381 
    382 	/* XXX SHOULD RE-MAP I/O SPACE HERE. */
    383 
    384 	if (lesc->sc_card == DEPCA) {
    385 		u_char val;
    386 		int i;
    387 
    388 		/* Map shared memory. */
    389 		if (bus_space_map(memt, ia->ia_maddr, ia->ia_msize,
    390 		    0, &memh))
    391 			panic("le_isa_attach: can't map shared memory");
    392 
    393 		lesc->sc_memh = memh;
    394 
    395 		val = 0xff;
    396 		for (;;) {
    397 #if 0	/* XXX !! */
    398 			bus_space_set_region_1(memt, memh, 0, val);
    399 #else
    400 			for (i = 0; i < ia->ia_msize; i++)
    401 				bus_space_write_1(memt, memh, i, val);
    402 #endif
    403 			for (i = 0; i < ia->ia_msize; i++)
    404 				if (bus_space_read_1(memt, memh, 1) != val) {
    405 					printf("%s: failed to clear memory\n",
    406 					    sc->sc_dev.dv_xname);
    407 					return;
    408 				}
    409 			if (val == 0x00)
    410 				break;
    411 			val -= 0x55;
    412 		}
    413 
    414 		sc->sc_conf3 = LE_C3_ACON;
    415 		sc->sc_mem = 0;			/* Not used. */
    416 		sc->sc_addr = 0;
    417 		sc->sc_memsize = ia->ia_msize;
    418 	} else {
    419 		sc->sc_mem = malloc(16384, M_DEVBUF, M_NOWAIT);
    420 		if (sc->sc_mem == 0) {
    421 			printf("%s: couldn't allocate memory for card\n",
    422 			    sc->sc_dev.dv_xname);
    423 			return;
    424 		}
    425 
    426 		sc->sc_conf3 = 0;
    427 		sc->sc_addr = kvtop(sc->sc_mem);	/* XXX !! */
    428 		sc->sc_memsize = 16384;
    429 	}
    430 
    431 	if (lesc->sc_card == DEPCA) {
    432 		sc->sc_copytodesc = depca_copytobuf;
    433 		sc->sc_copyfromdesc = depca_copyfrombuf;
    434 		sc->sc_copytobuf = depca_copytobuf;
    435 		sc->sc_copyfrombuf = depca_copyfrombuf;
    436 		sc->sc_zerobuf = depca_zerobuf;
    437 	} else {
    438 		sc->sc_copytodesc = am7990_copytobuf_contig;
    439 		sc->sc_copyfromdesc = am7990_copyfrombuf_contig;
    440 		sc->sc_copytobuf = am7990_copytobuf_contig;
    441 		sc->sc_copyfrombuf = am7990_copyfrombuf_contig;
    442 		sc->sc_zerobuf = am7990_zerobuf_contig;
    443 	}
    444 
    445 	sc->sc_rdcsr = le_isa_rdcsr;
    446 	sc->sc_wrcsr = le_isa_wrcsr;
    447 	sc->sc_hwinit = NULL;
    448 
    449 	printf("%s", sc->sc_dev.dv_xname);
    450 	am7990_config(sc);
    451 
    452 	if (ia->ia_drq != DRQUNK)
    453 		isa_dmacascade(ia->ia_drq);
    454 
    455 	lesc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq, IST_EDGE,
    456 	    IPL_NET, le_isa_intredge, sc);
    457 }
    458 
    459 /*
    460  * Controller interrupt.
    461  */
    462 int
    463 le_isa_intredge(arg)
    464 	void *arg;
    465 {
    466 
    467 	if (am7990_intr(arg) == 0)
    468 		return (0);
    469 	for (;;)
    470 		if (am7990_intr(arg) == 0)
    471 			return (1);
    472 }
    473 
    474 /*
    475  * DEPCA shared memory access functions.
    476  */
    477 
    478 void
    479 depca_copytobuf(sc, from, boff, len)
    480 	struct am7990_softc *sc;
    481 	void *from;
    482 	int boff, len;
    483 {
    484 	struct le_softc *lesc = (struct le_softc *)sc;
    485 
    486 	bus_space_write_region_1(lesc->sc_memt, lesc->sc_memh, boff,
    487 	    from, len);
    488 }
    489 
    490 void
    491 depca_copyfrombuf(sc, to, boff, len)
    492 	struct am7990_softc *sc;
    493 	void *to;
    494 	int boff, len;
    495 {
    496 	struct le_softc *lesc = (struct le_softc *)sc;
    497 
    498 	bus_space_read_region_1(lesc->sc_memt, lesc->sc_memh, boff,
    499 	    to, len);
    500 }
    501 
    502 void
    503 depca_zerobuf(sc, boff, len)
    504 	struct am7990_softc *sc;
    505 	int boff, len;
    506 {
    507 	struct le_softc *lesc = (struct le_softc *)sc;
    508 
    509 #if 0	/* XXX !! */
    510 	bus_space_set_region_1(lesc->sc_memt, lesc->sc_memh, boff,
    511 	    0x00, len);
    512 #else
    513 	for (; len != 0; boff++, len--)
    514 		bus_space_write_1(lesc->sc_memt, lesc->sc_memh, boff, 0x00);
    515 #endif
    516 }
    517