Home | History | Annotate | Line # | Download | only in isa
if_le_isa.c revision 1.46
      1 /*	$NetBSD: if_le_isa.c,v 1.46 2008/04/04 12:25:07 tsutsui Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1997, 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 of the Numerical Aerospace
      9  * Simulation Facility, NASA Ames Research Center.
     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 NetBSD
     22  *	Foundation, Inc. and its contributors.
     23  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  *    contributors may be used to endorse or promote products derived
     25  *    from this software without specific prior written permission.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  * POSSIBILITY OF SUCH DAMAGE.
     38  */
     39 
     40 /*-
     41  * Copyright (c) 1992, 1993
     42  *	The Regents of the University of California.  All rights reserved.
     43  *
     44  * This code is derived from software contributed to Berkeley by
     45  * Ralph Campbell and Rick Macklem.
     46  *
     47  * Redistribution and use in source and binary forms, with or without
     48  * modification, are permitted provided that the following conditions
     49  * are met:
     50  * 1. Redistributions of source code must retain the above copyright
     51  *    notice, this list of conditions and the following disclaimer.
     52  * 2. Redistributions in binary form must reproduce the above copyright
     53  *    notice, this list of conditions and the following disclaimer in the
     54  *    documentation and/or other materials provided with the distribution.
     55  * 3. Neither the name of the University nor the names of its contributors
     56  *    may be used to endorse or promote products derived from this software
     57  *    without specific prior written permission.
     58  *
     59  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     60  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     61  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     62  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     63  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     64  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     65  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     66  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     67  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     68  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     69  * SUCH DAMAGE.
     70  *
     71  *	@(#)if_le.c	8.2 (Berkeley) 11/16/93
     72  */
     73 
     74 #include <sys/cdefs.h>
     75 __KERNEL_RCSID(0, "$NetBSD: if_le_isa.c,v 1.46 2008/04/04 12:25:07 tsutsui Exp $");
     76 
     77 #include <sys/param.h>
     78 #include <sys/systm.h>
     79 #include <sys/mbuf.h>
     80 #include <sys/syslog.h>
     81 #include <sys/socket.h>
     82 #include <sys/device.h>
     83 
     84 #include <uvm/uvm_extern.h>
     85 
     86 #include <net/if.h>
     87 #include <net/if_ether.h>
     88 #include <net/if_media.h>
     89 
     90 #include <sys/cpu.h>
     91 #include <sys/intr.h>
     92 #include <sys/bus.h>
     93 
     94 #include <dev/isa/isareg.h>
     95 #include <dev/isa/isavar.h>
     96 #include <dev/isa/isadmavar.h>
     97 
     98 #include <dev/ic/lancereg.h>
     99 #include <dev/ic/lancevar.h>
    100 #include <dev/ic/am7990reg.h>
    101 #include <dev/ic/am7990var.h>
    102 
    103 #include <dev/isa/if_levar.h>
    104 
    105 int ne2100_isa_probe(device_t, cfdata_t, void *);
    106 int bicc_isa_probe(device_t, cfdata_t, void *);
    107 void le_dummyattach(device_t, device_t, void *);
    108 int le_dummyprobe(device_t, cfdata_t, void *);
    109 void le_ne2100_attach(device_t, device_t, void *);
    110 void le_bicc_attach(device_t, device_t, void *);
    111 
    112 CFATTACH_DECL_NEW(nele, 0,
    113     ne2100_isa_probe, le_dummyattach, NULL, NULL);
    114 
    115 CFATTACH_DECL_NEW(le_nele, sizeof(struct le_softc),
    116     le_dummyprobe, le_ne2100_attach, NULL, NULL);
    117 
    118 CFATTACH_DECL_NEW(bicc, 0,
    119     bicc_isa_probe, le_dummyattach, NULL, NULL);
    120 
    121 CFATTACH_DECL_NEW(le_bicc, sizeof(struct le_softc),
    122     le_dummyprobe, le_bicc_attach, NULL, NULL);
    123 
    124 struct le_isa_params {
    125 	const char *name;
    126 	int iosize, rap, rdp;
    127 	int macstart, macstride;
    128 } ne2100_params = {
    129 	"NE2100",
    130 	24, NE2100_RAP, NE2100_RDP,
    131 	0, 1
    132 }, bicc_params = {
    133 	"BICC Isolan",
    134 	16, BICC_RAP, BICC_RDP,
    135 	0, 2
    136 };
    137 
    138 int lance_isa_probe(struct isa_attach_args *, struct le_isa_params *, int);
    139 void le_isa_attach(struct device *, struct le_softc *,
    140     struct isa_attach_args *, struct le_isa_params *);
    141 
    142 int le_isa_intredge(void *);
    143 
    144 #if defined(_KERNEL_OPT)
    145 #include "opt_ddb.h"
    146 #endif
    147 
    148 #ifdef DDB
    149 #define	integrate
    150 #define hide
    151 #else
    152 #define	integrate	static inline
    153 #define hide		static
    154 #endif
    155 
    156 hide void le_isa_wrcsr(struct lance_softc *, uint16_t, uint16_t);
    157 hide uint16_t le_isa_rdcsr(struct lance_softc *, uint16_t);
    158 
    159 #define	LE_ISA_MEMSIZE	16384
    160 
    161 hide void
    162 le_isa_wrcsr(struct lance_softc *sc, uint16_t port, uint16_t val)
    163 {
    164 	struct le_softc *lesc = (struct le_softc *)sc;
    165 	bus_space_tag_t iot = lesc->sc_iot;
    166 	bus_space_handle_t ioh = lesc->sc_ioh;
    167 
    168 	bus_space_write_2(iot, ioh, lesc->sc_rap, port);
    169 	bus_space_write_2(iot, ioh, lesc->sc_rdp, val);
    170 }
    171 
    172 hide uint16_t
    173 le_isa_rdcsr(struct lance_softc *sc, uint16_t port)
    174 {
    175 	struct le_softc *lesc = (struct le_softc *)sc;
    176 	bus_space_tag_t iot = lesc->sc_iot;
    177 	bus_space_handle_t ioh = lesc->sc_ioh;
    178 	uint16_t val;
    179 
    180 	bus_space_write_2(iot, ioh, lesc->sc_rap, port);
    181 	val = bus_space_read_2(iot, ioh, lesc->sc_rdp);
    182 	return (val);
    183 }
    184 
    185 int
    186 ne2100_isa_probe(device_t parent, cfdata_t cf, void *aux)
    187 {
    188 
    189 	return (lance_isa_probe(aux, &ne2100_params, cf->cf_flags));
    190 }
    191 
    192 int
    193 bicc_isa_probe(device_t parent, cfdata_t cf, void *aux)
    194 {
    195 
    196 	return (lance_isa_probe(aux, &bicc_params, cf->cf_flags));
    197 }
    198 
    199 /*
    200  * Determine which chip is present on the card.
    201  */
    202 int
    203 lance_isa_probe(struct isa_attach_args *ia, struct le_isa_params *p, int flags)
    204 {
    205 	bus_space_tag_t iot = ia->ia_iot;
    206 	bus_space_handle_t ioh;
    207 	int rap, rdp;
    208 	int rv = 0;
    209 
    210 	if (ia->ia_nio < 1)
    211 		return (0);
    212 	if (ia->ia_nirq < 1)
    213 		return (0);
    214 	if (ia->ia_ndrq < 1)
    215 		return (0);
    216 
    217 	if (ISA_DIRECT_CONFIG(ia))
    218 		return (0);
    219 
    220 	/* Disallow wildcarded i/o address. */
    221 	if (ia->ia_io[0].ir_addr == ISA_UNKNOWN_PORT)
    222 		return (0);
    223 	if (ia->ia_irq[0].ir_irq == ISA_UNKNOWN_IRQ)
    224 		return (0);
    225 	if ((flags & LANCEISA_FLAG_LOCALBUS) == 0 &&
    226 	    ia->ia_drq[0].ir_drq == ISA_UNKNOWN_DRQ)
    227 		return (0);
    228 
    229 	/* Map i/o space. */
    230 	if (bus_space_map(iot, ia->ia_io[0].ir_addr, p->iosize, 0, &ioh))
    231 		return (0);
    232 
    233 	rap = p->rap;
    234 	rdp = p->rdp;
    235 
    236 	/* Stop the LANCE chip and put it in a known state. */
    237 	bus_space_write_2(iot, ioh, rap, LE_CSR0);
    238 	bus_space_write_2(iot, ioh, rdp, LE_C0_STOP);
    239 	delay(100);
    240 
    241 	bus_space_write_2(iot, ioh, rap, LE_CSR0);
    242 	if (bus_space_read_2(iot, ioh, rdp) != LE_C0_STOP)
    243 		goto bad;
    244 
    245 	bus_space_write_2(iot, ioh, rap, LE_CSR3);
    246 	bus_space_write_2(iot, ioh, rdp, 0);
    247 
    248 	ia->ia_nio = 1;
    249 	ia->ia_io[0].ir_size = p->iosize;
    250 
    251 	ia->ia_nirq = 1;
    252 
    253 	if ((flags & LANCEISA_FLAG_LOCALBUS) != 0 &&
    254 	    ia->ia_drq[0].ir_drq == ISA_UNKNOWN_DRQ)
    255 	    ia->ia_ndrq = 0;
    256 	else
    257 	    ia->ia_ndrq = 1;
    258 
    259 	ia->ia_niomem = 0;
    260 
    261 	rv = 1;
    262 
    263 bad:
    264 	bus_space_unmap(iot, ioh, p->iosize);
    265 	return (rv);
    266 }
    267 
    268 void
    269 le_dummyattach(device_t parent, device_t self, void *aux)
    270 {
    271 
    272 	aprint_normal("\n");
    273 
    274 	config_found(self, aux, 0);
    275 }
    276 
    277 int
    278 le_dummyprobe(device_t parent, cfdata_t cf, void *aux)
    279 {
    280 
    281 	return (1);
    282 }
    283 
    284 void
    285 le_ne2100_attach(device_t parent, device_t self, void *aux)
    286 {
    287 	struct le_softc *lesc = device_private(self);
    288 	struct lance_softc *sc = &lesc->sc_am7990.lsc;
    289 
    290 	sc->sc_dev = self;
    291 	le_isa_attach(parent, lesc, aux, &ne2100_params);
    292 }
    293 
    294 void
    295 le_bicc_attach(device_t parent, device_t self, void *aux)
    296 {
    297 	struct le_softc *lesc = device_private(self);
    298 	struct lance_softc *sc = &lesc->sc_am7990.lsc;
    299 
    300 	sc->sc_dev = self;
    301 	le_isa_attach(parent, lesc, aux, &bicc_params);
    302 }
    303 
    304 void
    305 le_isa_attach(struct device *parent, struct le_softc *lesc,
    306     struct isa_attach_args *ia, struct le_isa_params *p)
    307 {
    308 	struct lance_softc *sc = &lesc->sc_am7990.lsc;
    309 	bus_space_tag_t iot = ia->ia_iot;
    310 	bus_space_handle_t ioh;
    311 	bus_dma_tag_t dmat = ia->ia_dmat;
    312 	bus_dma_segment_t seg;
    313 	int i, rseg, error;
    314 
    315 	aprint_normal(": %s Ethernet\n", p->name);
    316 
    317 	if (bus_space_map(iot, ia->ia_io[0].ir_addr, p->iosize, 0, &ioh))
    318 		panic("%s: can't map io", device_xname(sc->sc_dev));
    319 
    320 	/*
    321 	 * Extract the physical MAC address from the ROM.
    322 	 */
    323 	for (i = 0; i < sizeof(sc->sc_enaddr); i++)
    324 		sc->sc_enaddr[i] =
    325 		    bus_space_read_1(iot, ioh, p->macstart + i * p->macstride);
    326 
    327 	lesc->sc_iot = iot;
    328 	lesc->sc_ioh = ioh;
    329 	lesc->sc_dmat = dmat;
    330 	lesc->sc_rap = p->rap;
    331 	lesc->sc_rdp = p->rdp;
    332 
    333 	/*
    334 	 * Allocate a DMA area for the card.
    335 	 */
    336 	if (bus_dmamem_alloc(dmat, LE_ISA_MEMSIZE, PAGE_SIZE, 0, &seg, 1,
    337 			     &rseg, BUS_DMA_NOWAIT)) {
    338 		aprint_error_dev(sc->sc_dev,
    339 		    "couldn't allocate memory for card\n");
    340 		return;
    341 	}
    342 	if (bus_dmamem_map(dmat, &seg, rseg, LE_ISA_MEMSIZE,
    343 			   (void **)&sc->sc_mem,
    344 			   BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) {
    345 		aprint_error_dev(sc->sc_dev, "couldn't map memory for card\n");
    346 		return;
    347 	}
    348 
    349 	/*
    350 	 * Create and load the DMA map for the DMA area.
    351 	 */
    352 	if (bus_dmamap_create(dmat, LE_ISA_MEMSIZE, 1,
    353 			LE_ISA_MEMSIZE, 0, BUS_DMA_NOWAIT, &lesc->sc_dmam)) {
    354 		aprint_error_dev(sc->sc_dev, "couldn't create DMA map\n");
    355 		bus_dmamem_free(dmat, &seg, rseg);
    356 		return;
    357 	}
    358 	if (bus_dmamap_load(dmat, lesc->sc_dmam,
    359 			sc->sc_mem, LE_ISA_MEMSIZE, NULL, BUS_DMA_NOWAIT)) {
    360 		aprint_error_dev(sc->sc_dev, "coundn't load DMA map\n");
    361 		bus_dmamem_free(dmat, &seg, rseg);
    362 		return;
    363 	}
    364 
    365 	sc->sc_conf3 = 0;
    366 	sc->sc_addr = lesc->sc_dmam->dm_segs[0].ds_addr;
    367 	sc->sc_memsize = LE_ISA_MEMSIZE;
    368 
    369 	sc->sc_copytodesc = lance_copytobuf_contig;
    370 	sc->sc_copyfromdesc = lance_copyfrombuf_contig;
    371 	sc->sc_copytobuf = lance_copytobuf_contig;
    372 	sc->sc_copyfrombuf = lance_copyfrombuf_contig;
    373 	sc->sc_zerobuf = lance_zerobuf_contig;
    374 
    375 	sc->sc_rdcsr = le_isa_rdcsr;
    376 	sc->sc_wrcsr = le_isa_wrcsr;
    377 	sc->sc_hwinit = NULL;
    378 
    379 	if (ia->ia_ndrq > 0) {
    380 		if ((error = isa_dmacascade(ia->ia_ic,
    381 					    ia->ia_drq[0].ir_drq)) != 0) {
    382 			aprint_error_dev(sc->sc_dev,
    383 			    "unable to cascade DRQ, error = %d\n", error);
    384 			return;
    385 		}
    386 	}
    387 
    388 	lesc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq[0].ir_irq,
    389 	    IST_EDGE, IPL_NET, le_isa_intredge, sc);
    390 
    391 	aprint_normal("%s", device_xname(sc->sc_dev));
    392 	am7990_config(&lesc->sc_am7990);
    393 }
    394 
    395 /*
    396  * Controller interrupt.
    397  */
    398 int
    399 le_isa_intredge(void *arg)
    400 {
    401 
    402 	if (am7990_intr(arg) == 0)
    403 		return (0);
    404 	for (;;)
    405 		if (am7990_intr(arg) == 0)
    406 			return (1);
    407 }
    408