Home | History | Annotate | Line # | Download | only in sbus
if_le.c revision 1.19
      1 /*	$NetBSD: if_le.c,v 1.19 2001/11/13 06:58:17 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.c,v 1.19 2001/11/13 06:58:17 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/mbuf.h>
     49 #include <sys/syslog.h>
     50 #include <sys/socket.h>
     51 #include <sys/device.h>
     52 #include <sys/malloc.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/sbus/sbusvar.h>
     63 #include <dev/sbus/lebuffervar.h>	/*XXX*/
     64 
     65 #include <dev/ic/lancereg.h>
     66 #include <dev/ic/lancevar.h>
     67 #include <dev/ic/am7990reg.h>
     68 #include <dev/ic/am7990var.h>
     69 
     70 /*
     71  * LANCE registers.
     72  */
     73 #define LEREG1_RDP	0	/* Register Data port */
     74 #define LEREG1_RAP	2	/* Register Address port */
     75 
     76 struct	le_softc {
     77 	struct	am7990_softc	sc_am7990;	/* glue to MI code */
     78 	struct	sbusdev		sc_sd;		/* sbus device */
     79 	bus_space_tag_t		sc_bustag;
     80 	bus_dma_tag_t		sc_dmatag;
     81 	bus_dmamap_t		sc_dmamap;
     82 	bus_space_handle_t	sc_reg;
     83 };
     84 
     85 #define MEMSIZE 0x4000		/* LANCE memory size */
     86 
     87 int	lematch_sbus __P((struct device *, struct cfdata *, void *));
     88 void	leattach_sbus __P((struct device *, struct device *, void *));
     89 
     90 /*
     91  * Media types supported.
     92  */
     93 static int lemedia[] = {
     94 	IFM_ETHER|IFM_10_5,
     95 };
     96 #define NLEMEDIA	(sizeof(lemedia) / sizeof(lemedia[0]))
     97 
     98 struct cfattach le_sbus_ca = {
     99 	sizeof(struct le_softc), lematch_sbus, leattach_sbus
    100 };
    101 
    102 extern struct cfdriver le_cd;
    103 
    104 #if defined(_KERNEL_OPT)
    105 #include "opt_ddb.h"
    106 #endif
    107 
    108 #ifdef DDB
    109 #define	integrate
    110 #define hide
    111 #else
    112 #define	integrate	static __inline
    113 #define hide		static
    114 #endif
    115 
    116 static void lewrcsr __P((struct lance_softc *, u_int16_t, u_int16_t));
    117 static u_int16_t lerdcsr __P((struct lance_softc *, u_int16_t));
    118 
    119 static void
    120 lewrcsr(sc, port, val)
    121 	struct lance_softc *sc;
    122 	u_int16_t port, val;
    123 {
    124 	struct le_softc *lesc = (struct le_softc *)sc;
    125 
    126 	bus_space_write_2(lesc->sc_bustag, lesc->sc_reg, LEREG1_RAP, port);
    127 	bus_space_write_2(lesc->sc_bustag, lesc->sc_reg, LEREG1_RDP, val);
    128 
    129 #if defined(SUN4M)
    130 	/*
    131 	 * We need to flush the Sbus->Mbus write buffers. This can most
    132 	 * easily be accomplished by reading back the register that we
    133 	 * just wrote (thanks to Chris Torek for this solution).
    134 	 */
    135 	if (CPU_ISSUN4M) {
    136 		volatile u_int16_t discard;
    137 		discard = bus_space_read_2(lesc->sc_bustag, lesc->sc_reg,
    138 					   LEREG1_RDP);
    139 	}
    140 #endif
    141 }
    142 
    143 static u_int16_t
    144 lerdcsr(sc, port)
    145 	struct lance_softc *sc;
    146 	u_int16_t port;
    147 {
    148 	struct le_softc *lesc = (struct le_softc *)sc;
    149 
    150 	bus_space_write_2(lesc->sc_bustag, lesc->sc_reg, LEREG1_RAP, port);
    151 	return (bus_space_read_2(lesc->sc_bustag, lesc->sc_reg, LEREG1_RDP));
    152 }
    153 
    154 
    155 int
    156 lematch_sbus(parent, cf, aux)
    157 	struct device *parent;
    158 	struct cfdata *cf;
    159 	void *aux;
    160 {
    161 	struct sbus_attach_args *sa = aux;
    162 
    163 	return (strcmp(cf->cf_driver->cd_name, sa->sa_name) == 0);
    164 }
    165 
    166 void
    167 leattach_sbus(parent, self, aux)
    168 	struct device *parent, *self;
    169 	void *aux;
    170 {
    171 	struct sbus_attach_args *sa = aux;
    172 	struct le_softc *lesc = (struct le_softc *)self;
    173 	struct lance_softc *sc = &lesc->sc_am7990.lsc;
    174 	bus_dma_tag_t dmatag;
    175 	struct sbusdev *sd;
    176 	/* XXX the following declarations should be elsewhere */
    177 	extern void myetheraddr __P((u_char *));
    178 
    179 
    180 	lesc->sc_bustag = sa->sa_bustag;
    181 	lesc->sc_dmatag = dmatag = sa->sa_dmatag;
    182 
    183 	if (sbus_bus_map(sa->sa_bustag,
    184 			 sa->sa_slot,
    185 			 sa->sa_offset,
    186 			 sa->sa_size,
    187 			 BUS_SPACE_MAP_LINEAR,
    188 			 0, &lesc->sc_reg) != 0) {
    189 		printf("%s @ sbus: cannot map registers\n", self->dv_xname);
    190 		return;
    191 	}
    192 
    193 	/*
    194 	 * Look for an "unallocated" lebuffer and pair it with
    195 	 * this `le' device on the assumption that we're on
    196 	 * a pre-historic ROM that doesn't establish le<=>lebuffer
    197 	 * parent-child relationships.
    198 	 */
    199 	for (sd = ((struct sbus_softc *)parent)->sc_sbdev; sd != NULL;
    200 	     sd = sd->sd_bchain) {
    201 
    202 		struct lebuf_softc *lebuf = (struct lebuf_softc *)sd->sd_dev;
    203 
    204 		if (strncmp("lebuffer", sd->sd_dev->dv_xname, 8) != 0)
    205 			continue;
    206 
    207 		if (lebuf->attached != 0)
    208 			continue;
    209 
    210 		sc->sc_mem = lebuf->sc_buffer;
    211 		sc->sc_memsize = lebuf->sc_bufsiz;
    212 		sc->sc_addr = 0; /* Lance view is offset by buffer location */
    213 		lebuf->attached = 1;
    214 
    215 		/* That old black magic... */
    216 		sc->sc_conf3 = PROM_getpropint(sa->sa_node,
    217 					  "busmaster-regval",
    218 					  LE_C3_BSWP | LE_C3_ACON | LE_C3_BCON);
    219 		break;
    220 	}
    221 
    222 	lesc->sc_sd.sd_reset = (void *)lance_reset;
    223 	sbus_establish(&lesc->sc_sd, &sc->sc_dev);
    224 
    225 	if (sc->sc_mem == 0) {
    226 		bus_dma_segment_t seg;
    227 		int rseg, error;
    228 
    229 #ifndef BUS_DMA_24BIT
    230 /* XXX - This flag is not defined on all archs */
    231 #define BUS_DMA_24BIT	0
    232 #endif
    233 		/* Get a DMA handle */
    234 		if ((error = bus_dmamap_create(dmatag, MEMSIZE, 1, MEMSIZE, 0,
    235 						BUS_DMA_NOWAIT|BUS_DMA_24BIT,
    236 						&lesc->sc_dmamap)) != 0) {
    237 			printf("%s: DMA map create error %d\n",
    238 				self->dv_xname, error);
    239 			return;
    240 		}
    241 
    242 		/* Allocate DMA buffer */
    243 		if ((error = bus_dmamem_alloc(dmatag, MEMSIZE, 0, 0,
    244 					 &seg, 1, &rseg,
    245 					 BUS_DMA_NOWAIT|BUS_DMA_24BIT)) != 0){
    246 			printf("%s: DMA buffer allocation error %d\n",
    247 				self->dv_xname, error);
    248 			return;
    249 		}
    250 
    251 		/* Map DMA buffer into kernel space */
    252 		if ((error = bus_dmamem_map(dmatag, &seg, rseg, MEMSIZE,
    253 				       (caddr_t *)&sc->sc_mem,
    254 				       BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) != 0) {
    255 			printf("%s: DMA buffer map error %d\n",
    256 				self->dv_xname, error);
    257 			bus_dmamem_free(lesc->sc_dmatag, &seg, rseg);
    258 			return;
    259 		}
    260 
    261 		/* Load DMA buffer */
    262 		if ((error = bus_dmamap_load(dmatag, lesc->sc_dmamap, sc->sc_mem,
    263 		    MEMSIZE, NULL, BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) != 0) {
    264 			printf("%s: DMA buffer map load error %d\n",
    265 				self->dv_xname, error);
    266 			bus_dmamem_free(dmatag, &seg, rseg);
    267 			bus_dmamem_unmap(dmatag, sc->sc_mem, MEMSIZE);
    268 			return;
    269 		}
    270 
    271 		sc->sc_addr = lesc->sc_dmamap->dm_segs[0].ds_addr & 0xffffff;
    272 		sc->sc_memsize = MEMSIZE;
    273 		sc->sc_conf3 = LE_C3_BSWP | LE_C3_ACON | LE_C3_BCON;
    274 	}
    275 
    276 	myetheraddr(sc->sc_enaddr);
    277 
    278 	sc->sc_supmedia = lemedia;
    279 	sc->sc_nsupmedia = NLEMEDIA;
    280 	sc->sc_defaultmedia = lemedia[0];
    281 
    282 	sc->sc_copytodesc = lance_copytobuf_contig;
    283 	sc->sc_copyfromdesc = lance_copyfrombuf_contig;
    284 	sc->sc_copytobuf = lance_copytobuf_contig;
    285 	sc->sc_copyfrombuf = lance_copyfrombuf_contig;
    286 	sc->sc_zerobuf = lance_zerobuf_contig;
    287 
    288 	sc->sc_rdcsr = lerdcsr;
    289 	sc->sc_wrcsr = lewrcsr;
    290 
    291 	am7990_config(&lesc->sc_am7990);
    292 
    293 	/* Establish interrupt handler */
    294 	if (sa->sa_nintr != 0)
    295 		(void)bus_intr_establish(lesc->sc_bustag, sa->sa_pri,
    296 					 IPL_NET, 0, am7990_intr, sc);
    297 }
    298