Home | History | Annotate | Line # | Download | only in dev
if_le_obio.c revision 1.19
      1 /*	$NetBSD: if_le_obio.c,v 1.19 2003/07/15 00:04:55 lukem 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; Jason R. Thorpe of the Numerical Aerospace
      9  * Simulation Facility, NASA Ames Research Center; Paul Kranenburg.
     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 #include <sys/cdefs.h>
     41 __KERNEL_RCSID(0, "$NetBSD: if_le_obio.c,v 1.19 2003/07/15 00:04:55 lukem Exp $");
     42 
     43 #include "opt_inet.h"
     44 #include "bpfilter.h"
     45 
     46 #include <sys/param.h>
     47 #include <sys/systm.h>
     48 #include <sys/device.h>
     49 #include <sys/malloc.h>
     50 #include <sys/socket.h>
     51 
     52 #include <uvm/uvm_extern.h>
     53 
     54 #include <net/if.h>
     55 #include <net/if_ether.h>
     56 #include <net/if_media.h>
     57 
     58 #include <machine/bus.h>
     59 #include <machine/intr.h>
     60 #include <machine/autoconf.h>
     61 
     62 #include <dev/ic/lancereg.h>
     63 #include <dev/ic/lancevar.h>
     64 #include <dev/ic/am7990reg.h>
     65 #include <dev/ic/am7990var.h>
     66 
     67 /*
     68  * LANCE registers.
     69  */
     70 #define LEREG1_RDP	0	/* Register Data port */
     71 #define LEREG1_RAP	2	/* Register Address port */
     72 
     73 struct	le_softc {
     74 	struct	am7990_softc	sc_am7990;	/* glue to MI code */
     75 	bus_space_tag_t		sc_bustag;
     76 	bus_dma_tag_t		sc_dmatag;
     77 	bus_dmamap_t		sc_dmamap;
     78 	bus_space_handle_t	sc_reg;		/* LANCE registers */
     79 };
     80 
     81 #define MEMSIZE 0x4000		/* LANCE memory size */
     82 
     83 int	lematch_obio __P((struct device *, struct cfdata *, void *));
     84 void	leattach_obio __P((struct device *, struct device *, void *));
     85 
     86 /*
     87  * Media types supported.
     88  */
     89 static int lemedia[] = {
     90 	IFM_ETHER|IFM_10_T,
     91 };
     92 #define NLEMEDIA	(sizeof(lemedia) / sizeof(lemedia[0]))
     93 
     94 CFATTACH_DECL(le_obio, sizeof(struct le_softc),
     95     lematch_obio, leattach_obio, NULL, NULL);
     96 
     97 extern struct cfdriver le_cd;
     98 
     99 #if defined(_KERNEL_OPT)
    100 #include "opt_ddb.h"
    101 #endif
    102 
    103 #ifdef DDB
    104 #define	integrate
    105 #define hide
    106 #else
    107 #define	integrate	static __inline
    108 #define hide		static
    109 #endif
    110 
    111 static void lewrcsr __P((struct lance_softc *, u_int16_t, u_int16_t));
    112 static u_int16_t lerdcsr __P((struct lance_softc *, u_int16_t));
    113 
    114 static void
    115 lewrcsr(sc, port, val)
    116 	struct lance_softc *sc;
    117 	u_int16_t port, val;
    118 {
    119 	struct le_softc *lesc = (struct le_softc *)sc;
    120 
    121 	bus_space_write_2(lesc->sc_bustag, lesc->sc_reg, LEREG1_RAP, port);
    122 	bus_space_write_2(lesc->sc_bustag, lesc->sc_reg, LEREG1_RDP, val);
    123 }
    124 
    125 static u_int16_t
    126 lerdcsr(sc, port)
    127 	struct lance_softc *sc;
    128 	u_int16_t port;
    129 {
    130 	struct le_softc *lesc = (struct le_softc *)sc;
    131 
    132 	bus_space_write_2(lesc->sc_bustag, lesc->sc_reg, LEREG1_RAP, port);
    133 	return (bus_space_read_2(lesc->sc_bustag, lesc->sc_reg, LEREG1_RDP));
    134 }
    135 
    136 int
    137 lematch_obio(parent, cf, aux)
    138 	struct device *parent;
    139 	struct cfdata *cf;
    140 	void *aux;
    141 {
    142 	union obio_attach_args *uoba = aux;
    143 	struct obio4_attach_args *oba;
    144 
    145 	if (uoba->uoba_isobio4 == 0)
    146 		return (0);
    147 
    148 	oba = &uoba->uoba_oba4;
    149 	return (bus_space_probe(oba->oba_bustag, oba->oba_paddr,
    150 				2,	/* probe size */
    151 				0,	/* offset */
    152 				0,	/* flags */
    153 				NULL, NULL));
    154 }
    155 
    156 void
    157 leattach_obio(parent, self, aux)
    158 	struct device *parent, *self;
    159 	void *aux;
    160 {
    161 	union obio_attach_args *uoba = aux;
    162 	struct obio4_attach_args *oba = &uoba->uoba_oba4;
    163 	struct le_softc *lesc = (struct le_softc *)self;
    164 	struct lance_softc *sc = &lesc->sc_am7990.lsc;
    165 	bus_dma_segment_t seg;
    166 	bus_dma_tag_t dmatag;
    167 	int rseg;
    168 	int error;
    169 	/* XXX the following declarations should be elsewhere */
    170 	extern void myetheraddr __P((u_char *));
    171 
    172 	lesc->sc_bustag = oba->oba_bustag;
    173 	lesc->sc_dmatag = dmatag = oba->oba_dmatag;
    174 
    175 	if (bus_space_map(oba->oba_bustag, oba->oba_paddr,
    176 			  2 * sizeof(u_int16_t),
    177 			  0, &lesc->sc_reg) != 0) {
    178 		printf("%s @ obio: cannot map registers\n", self->dv_xname);
    179 		return;
    180 	}
    181 
    182 	/* Get a DMA handle */
    183 	if ((error = bus_dmamap_create(dmatag, MEMSIZE, 1, MEMSIZE, 0,
    184 					BUS_DMA_NOWAIT|BUS_DMA_24BIT,
    185 					&lesc->sc_dmamap)) != 0) {
    186 		printf("%s: DMA map create error %d\n",
    187 			self->dv_xname, error);
    188 		return;
    189 	}
    190 
    191 	/* Allocate DMA buffer */
    192 	if ((error = bus_dmamem_alloc(dmatag, MEMSIZE, PAGE_SIZE, 0,
    193 			     &seg, 1, &rseg,
    194 			     BUS_DMA_NOWAIT | BUS_DMA_24BIT)) != 0) {
    195 		printf("%s: DMA memory allocation error %d\n",
    196 			self->dv_xname, error);
    197 		return;
    198 	}
    199 	/* Map DMA buffer into kernel space */
    200 	if ((error = bus_dmamem_map(dmatag, &seg, rseg, MEMSIZE,
    201 			   (caddr_t *)&sc->sc_mem,
    202 			   BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) != 0) {
    203 		printf("%s: DMA memory map error %d\n", self->dv_xname, error);
    204 		bus_dmamem_free(lesc->sc_dmatag, &seg, rseg);
    205 		return;
    206 	}
    207 	/* Load DMA buffer */
    208 	if ((error = bus_dmamap_load(dmatag, lesc->sc_dmamap,
    209 				     sc->sc_mem, MEMSIZE, NULL,
    210 				     BUS_DMA_NOWAIT)) != 0) {
    211 		printf("%s: DMA buffer map load error %d\n",
    212 			self->dv_xname, error);
    213 		bus_dmamem_unmap(dmatag, (caddr_t)sc->sc_mem, MEMSIZE);
    214 		bus_dmamem_free(dmatag, &seg, rseg);
    215 		return;
    216 	}
    217 
    218 	sc->sc_addr = lesc->sc_dmamap->dm_segs[0].ds_addr & 0xffffff;
    219 	sc->sc_memsize = MEMSIZE;
    220 	sc->sc_conf3 = LE_C3_BSWP | LE_C3_ACON | LE_C3_BCON;
    221 
    222 	sc->sc_supmedia = lemedia;
    223 	sc->sc_nsupmedia = NLEMEDIA;
    224 	sc->sc_defaultmedia = lemedia[0];
    225 
    226 	myetheraddr(sc->sc_enaddr);
    227 
    228 	sc->sc_copytodesc = lance_copytobuf_contig;
    229 	sc->sc_copyfromdesc = lance_copyfrombuf_contig;
    230 	sc->sc_copytobuf = lance_copytobuf_contig;
    231 	sc->sc_copyfrombuf = lance_copyfrombuf_contig;
    232 	sc->sc_zerobuf = lance_zerobuf_contig;
    233 
    234 	sc->sc_rdcsr = lerdcsr;
    235 	sc->sc_wrcsr = lewrcsr;
    236 
    237 	am7990_config(&lesc->sc_am7990);
    238 
    239 	/* Install interrupt */
    240 	(void)bus_intr_establish(lesc->sc_bustag, oba->oba_pri, IPL_NET,
    241 				 am7990_intr, sc);
    242 }
    243