Home | History | Annotate | Line # | Download | only in isapnp
if_le_isapnp.c revision 1.18
      1 /*	$NetBSD: if_le_isapnp.c,v 1.18 2000/11/14 18:46:36 thorpej Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1997 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9  * 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) 1997 Jonathan Stone <jonathan (at) NetBSD.org> and
     42  * Matthias Drochner. All rights reserved.
     43  *
     44  * Redistribution and use in source and binary forms, with or without
     45  * modification, are permitted provided that the following conditions
     46  * are met:
     47  * 1. Redistributions of source code must retain the above copyright
     48  *    notice, this list of conditions and the following disclaimer.
     49  * 2. Redistributions in binary form must reproduce the above copyright
     50  *    notice, this list of conditions and the following disclaimer in the
     51  *    documentation and/or other materials provided with the distribution.
     52  * 3. All advertising materials mentioning features or use of this software
     53  *    must display the following acknowledgement:
     54  *      This product includes software developed by Jonathan Stone.
     55  * 4. The name of the author may not be used to endorse or promote products
     56  *    derived from this software without specific prior written permission.
     57  *
     58  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     59  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     60  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     61  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     62  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     63  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     64  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     65  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     66  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     67  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     68  */
     69 
     70 #include "opt_inet.h"
     71 #include "opt_ns.h"
     72 #include "bpfilter.h"
     73 
     74 #include <sys/param.h>
     75 #include <sys/systm.h>
     76 #include <sys/mbuf.h>
     77 #include <sys/socket.h>
     78 #include <sys/ioctl.h>
     79 #include <sys/errno.h>
     80 #include <sys/syslog.h>
     81 #include <sys/select.h>
     82 #include <sys/device.h>
     83 
     84 #include <uvm/uvm_extern.h>
     85 
     86 #include <net/if.h>
     87 #include <net/if_dl.h>
     88 #include <net/if_ether.h>
     89 #include <net/if_media.h>
     90 
     91 #ifdef INET
     92 #include <netinet/in.h>
     93 #include <netinet/in_systm.h>
     94 #include <netinet/in_var.h>
     95 #include <netinet/ip.h>
     96 #endif
     97 
     98 #ifdef NS
     99 #include <netns/ns.h>
    100 #include <netns/ns_if.h>
    101 #endif
    102 
    103 #if NBPFILTER > 0
    104 #include <net/bpf.h>
    105 #include <net/bpfdesc.h>
    106 #endif
    107 
    108 #include <machine/cpu.h>
    109 #include <machine/bus.h>
    110 #include <machine/intr.h>
    111 
    112 #include <dev/isa/isavar.h>
    113 #include <dev/isa/isadmavar.h>
    114 
    115 #include <dev/isapnp/isapnpreg.h>
    116 #include <dev/isapnp/isapnpvar.h>
    117 #include <dev/isapnp/isapnpdevs.h>
    118 
    119 #include <dev/ic/lancereg.h>
    120 #include <dev/ic/lancevar.h>
    121 #include <dev/ic/am7990reg.h>
    122 #include <dev/ic/am7990var.h>
    123 #include <dev/isapnp/if_levar.h>
    124 
    125 int le_isapnp_match __P((struct device *, struct cfdata *, void *));
    126 void le_isapnp_attach __P((struct device *, struct device *, void *));
    127 
    128 struct cfattach le_isapnp_ca = {
    129 	sizeof(struct le_softc), le_isapnp_match, le_isapnp_attach
    130 };
    131 
    132 int	le_isapnp_intredge __P((void *));
    133 static void le_isapnp_wrcsr __P((struct lance_softc *, u_int16_t, u_int16_t));
    134 static u_int16_t le_isapnp_rdcsr __P((struct lance_softc *, u_int16_t));
    135 
    136 
    137 #define	LE_ISAPNP_MEMSIZE	16384
    138 
    139 static void
    140 le_isapnp_wrcsr(sc, port, val)
    141 	struct lance_softc *sc;
    142 	u_int16_t port, val;
    143 {
    144 	struct le_softc *lesc = (struct le_softc *)sc;
    145 	bus_space_tag_t iot = lesc->sc_iot;
    146 	bus_space_handle_t ioh = lesc->sc_ioh;
    147 
    148 	bus_space_write_2(iot, ioh, lesc->sc_rap, port);
    149 	bus_space_write_2(iot, ioh, lesc->sc_rdp, val);
    150 }
    151 
    152 static u_int16_t
    153 le_isapnp_rdcsr(sc, port)
    154 	struct lance_softc *sc;
    155 	u_int16_t port;
    156 {
    157 	struct le_softc *lesc = (struct le_softc *)sc;
    158 	bus_space_tag_t iot = lesc->sc_iot;
    159 	bus_space_handle_t ioh = lesc->sc_ioh;
    160 	u_int16_t val;
    161 
    162 	bus_space_write_2(iot, ioh, lesc->sc_rap, port);
    163 	val = bus_space_read_2(iot, ioh, lesc->sc_rdp);
    164 	return (val);
    165 }
    166 
    167 int
    168 le_isapnp_match(parent, match, aux)
    169 	struct device *parent;
    170 	struct cfdata *match;
    171 	void *aux;
    172 {
    173 	int pri, variant;
    174 
    175 	pri = isapnp_devmatch(aux, &isapnp_le_devinfo, &variant);
    176 	if (pri && variant > 0)
    177 		pri = 0;
    178 	return (pri);
    179 }
    180 
    181 void
    182 le_isapnp_attach(parent, self, aux)
    183 	struct device *parent, *self;
    184 	void *aux;
    185 {
    186 	struct le_softc *lesc = (void *)self;
    187 	struct lance_softc *sc = &lesc->sc_am7990.lsc;
    188 	struct isapnp_attach_args *ipa = aux;
    189 	bus_space_tag_t iot;
    190 	bus_space_handle_t ioh;
    191 	bus_dma_tag_t dmat;
    192 	bus_dma_segment_t seg;
    193 	int i, rseg, error;
    194 
    195 	if (isapnp_config(ipa->ipa_iot, ipa->ipa_memt, ipa)) {
    196 		printf("%s: error in region allocation\n",
    197 		    sc->sc_dev.dv_xname);
    198 		return;
    199 	}
    200 
    201 	lesc->sc_iot = iot = ipa->ipa_iot;
    202 	lesc->sc_ioh = ioh = ipa->ipa_io[0].h;
    203 	lesc->sc_dmat = dmat = ipa->ipa_dmat;
    204 
    205 	lesc->sc_rap = PCNET_RAP;
    206 	lesc->sc_rdp = PCNET_RDP;
    207 
    208 	/*
    209 	 * Extract the physical MAC address from the ROM.
    210 	 */
    211 	for (i = 0; i < sizeof(sc->sc_enaddr); i++)
    212 		sc->sc_enaddr[i] = bus_space_read_1(iot, ioh, PCNET_SAPROM+i);
    213 
    214 	/*
    215 	 * Allocate a DMA area for the card.
    216 	 */
    217 	if (bus_dmamem_alloc(dmat, LE_ISAPNP_MEMSIZE, PAGE_SIZE, 0, &seg, 1,
    218 	    &rseg, BUS_DMA_NOWAIT)) {
    219 		printf("%s: couldn't allocate memory for card\n",
    220 		    sc->sc_dev.dv_xname);
    221 		return;
    222 	}
    223 	if (bus_dmamem_map(dmat, &seg, rseg, LE_ISAPNP_MEMSIZE,
    224 	    (caddr_t *)&sc->sc_mem, BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) {
    225 		printf("%s: couldn't map memory for card\n",
    226 		    sc->sc_dev.dv_xname);
    227 		return;
    228 	}
    229 
    230 	/*
    231 	 * Create and load the DMA map for the DMA area.
    232 	 */
    233 	if (bus_dmamap_create(dmat, LE_ISAPNP_MEMSIZE, 1,
    234 	    LE_ISAPNP_MEMSIZE, 0, BUS_DMA_NOWAIT, &lesc->sc_dmam)) {
    235 		printf("%s: couldn't create DMA map\n",
    236 		    sc->sc_dev.dv_xname);
    237 		bus_dmamem_free(dmat, &seg, rseg);
    238 		return;
    239 	}
    240 	if (bus_dmamap_load(dmat, lesc->sc_dmam,
    241 	    sc->sc_mem, LE_ISAPNP_MEMSIZE, NULL, BUS_DMA_NOWAIT)) {
    242 		printf("%s: coundn't load DMA map\n",
    243 		    sc->sc_dev.dv_xname);
    244 		bus_dmamem_free(dmat, &seg, rseg);
    245 		return;
    246 	}
    247 
    248 	sc->sc_conf3 = 0;
    249 	sc->sc_addr = lesc->sc_dmam->dm_segs[0].ds_addr;
    250 	sc->sc_memsize = LE_ISAPNP_MEMSIZE;
    251 
    252 	sc->sc_copytodesc = lance_copytobuf_contig;
    253 	sc->sc_copyfromdesc = lance_copyfrombuf_contig;
    254 	sc->sc_copytobuf = lance_copytobuf_contig;
    255 	sc->sc_copyfrombuf = lance_copyfrombuf_contig;
    256 	sc->sc_zerobuf = lance_zerobuf_contig;
    257 
    258 	sc->sc_rdcsr = le_isapnp_rdcsr;
    259 	sc->sc_wrcsr = le_isapnp_wrcsr;
    260 	sc->sc_hwinit = NULL;
    261 
    262 	if (ipa->ipa_ndrq > 0) {
    263 		if ((error = isa_dmacascade(ipa->ipa_ic,
    264 		    ipa->ipa_drq[0].num)) != 0) {
    265 			printf("%s: unable to cascade DRQ, error = %d\n",
    266 			    sc->sc_dev.dv_xname, error);
    267 			return;
    268 		}
    269 	}
    270 
    271 	lesc->sc_ih = isa_intr_establish(ipa->ipa_ic, ipa->ipa_irq[0].num,
    272 	    ipa->ipa_irq[0].type, IPL_NET, le_isapnp_intredge, sc);
    273 
    274 	printf("%s: %s %s\n", sc->sc_dev.dv_xname, ipa->ipa_devident,
    275 	    ipa->ipa_devclass);
    276 	am7990_config(&lesc->sc_am7990);
    277 }
    278 
    279 
    280 /*
    281  * Controller interrupt.
    282  */
    283 int
    284 le_isapnp_intredge(arg)
    285 	void *arg;
    286 {
    287 
    288 	if (am7990_intr(arg) == 0)
    289 		return (0);
    290 	for (;;)
    291 		if (am7990_intr(arg) == 0)
    292 			return (1);
    293 }
    294