Home | History | Annotate | Line # | Download | only in sbus
if_le.c revision 1.4
      1 /*	$NetBSD: if_le.c,v 1.4 1998/08/15 10:51:19 mycroft 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) 1996
     42  *	The President and Fellows of Harvard College. All rights reserved.
     43  * Copyright (c) 1992, 1993
     44  *	The Regents of the University of California.  All rights reserved.
     45  *
     46  * This code is derived from software contributed to Berkeley by
     47  * Ralph Campbell and Rick Macklem.
     48  *
     49  * Redistribution and use in source and binary forms, with or without
     50  * modification, are permitted provided that the following conditions
     51  * are met:
     52  * 1. Redistributions of source code must retain the above copyright
     53  *    notice, this list of conditions and the following disclaimer.
     54  * 2. Redistributions in binary form must reproduce the above copyright
     55  *    notice, this list of conditions and the following disclaimer in the
     56  *    documentation and/or other materials provided with the distribution.
     57  * 3. All advertising materials mentioning features or use of this software
     58  *    must display the following acknowledgement:
     59  *	This product includes software developed by Aaron Brown and
     60  *	Harvard University.
     61  *	This product includes software developed by the University of
     62  *	California, Berkeley and its contributors.
     63  * 4. Neither the name of the University nor the names of its contributors
     64  *    may be used to endorse or promote products derived from this software
     65  *    without specific prior written permission.
     66  *
     67  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     68  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     69  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     70  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     71  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     72  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     73  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     74  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     75  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     76  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     77  * SUCH DAMAGE.
     78  *
     79  *	@(#)if_le.c	8.2 (Berkeley) 11/16/93
     80  */
     81 
     82 #include "opt_inet.h"
     83 #include "bpfilter.h"
     84 
     85 #include <sys/param.h>
     86 #include <sys/systm.h>
     87 #include <sys/mbuf.h>
     88 #include <sys/syslog.h>
     89 #include <sys/socket.h>
     90 #include <sys/device.h>
     91 #include <sys/malloc.h>
     92 
     93 #include <net/if.h>
     94 #include <net/if_ether.h>
     95 #include <net/if_media.h>
     96 
     97 #ifdef INET
     98 #include <netinet/in.h>
     99 #include <netinet/if_inarp.h>
    100 #endif
    101 
    102 #include <machine/autoconf.h>
    103 #include <machine/cpu.h>
    104 
    105 #include <dev/sbus/sbusvar.h>
    106 #include <dev/sbus/lebuffervar.h>	/*XXX*/
    107 
    108 #include <dev/ic/lancereg.h>
    109 #include <dev/ic/lancevar.h>
    110 #include <dev/ic/am7990reg.h>
    111 #include <dev/ic/am7990var.h>
    112 
    113 /*
    114  * LANCE registers.
    115  */
    116 struct lereg1 {
    117 	volatile u_int16_t	ler1_rdp;	/* data port */
    118 	volatile u_int16_t	ler1_rap;	/* register select port */
    119 };
    120 
    121 struct	le_softc {
    122 	struct	am7990_softc sc_am7990;	/* glue to MI code */
    123 	struct	sbusdev sc_sd;		/* sbus device */
    124 	bus_space_tag_t	sc_bustag;
    125 	bus_dma_tag_t	sc_dmatag;
    126 	struct	lereg1 *sc_r1;		/* LANCE registers */
    127 };
    128 
    129 #define MEMSIZE 0x4000		/* LANCE memory size */
    130 
    131 int	lematch_sbus __P((struct device *, struct cfdata *, void *));
    132 void	leattach_sbus __P((struct device *, struct device *, void *));
    133 
    134 /*
    135  * Media types supported.
    136  */
    137 static int lemedia[] = {
    138 	IFM_ETHER|IFM_10_5,
    139 };
    140 #define NLEMEDIA	(sizeof(lemedia) / sizeof(lemedia[0]))
    141 
    142 
    143 struct cfattach le_sbus_ca = {
    144 	sizeof(struct le_softc), lematch_sbus, leattach_sbus
    145 };
    146 
    147 extern struct cfdriver le_cd;
    148 
    149 #if defined(_KERNEL) && !defined(_LKM)
    150 #include "opt_ddb.h"
    151 #endif
    152 
    153 #ifdef DDB
    154 #define	integrate
    155 #define hide
    156 #else
    157 #define	integrate	static __inline
    158 #define hide		static
    159 #endif
    160 
    161 static void lewrcsr __P((struct lance_softc *, u_int16_t, u_int16_t));
    162 static u_int16_t lerdcsr __P((struct lance_softc *, u_int16_t));
    163 
    164 static void
    165 lewrcsr(sc, port, val)
    166 	struct lance_softc *sc;
    167 	u_int16_t port, val;
    168 {
    169 	struct lereg1 *ler1 = ((struct le_softc *)sc)->sc_r1;
    170 
    171 	ler1->ler1_rap = port;
    172 	ler1->ler1_rdp = val;
    173 
    174 #if defined(SUN4M)
    175 	/*
    176 	 * We need to flush the Sbus->Mbus write buffers. This can most
    177 	 * easily be accomplished by reading back the register that we
    178 	 * just wrote (thanks to Chris Torek for this solution).
    179 	 */
    180 	if (CPU_ISSUN4M) {
    181 		volatile u_int16_t discard;
    182 		discard = ler1->ler1_rdp;
    183 	}
    184 #endif
    185 }
    186 
    187 static u_int16_t
    188 lerdcsr(sc, port)
    189 	struct lance_softc *sc;
    190 	u_int16_t port;
    191 {
    192 	struct lereg1 *ler1 = ((struct le_softc *)sc)->sc_r1;
    193 	u_int16_t val;
    194 
    195 	ler1->ler1_rap = port;
    196 	val = ler1->ler1_rdp;
    197 	return (val);
    198 }
    199 
    200 
    201 int
    202 lematch_sbus(parent, cf, aux)
    203 	struct device *parent;
    204 	struct cfdata *cf;
    205 	void *aux;
    206 {
    207 	struct sbus_attach_args *sa = aux;
    208 
    209 	return (strcmp(cf->cf_driver->cd_name, sa->sa_name) == 0);
    210 }
    211 
    212 #define SAME_LANCE(bp, sa) \
    213 	((bp->val[0] == sa->sa_slot && bp->val[1] == sa->sa_offset) || \
    214 	 (bp->val[0] == -1 && bp->val[1] == sc->sc_dev.dv_unit))
    215 
    216 void
    217 leattach_sbus(parent, self, aux)
    218 	struct device *parent, *self;
    219 	void *aux;
    220 {
    221 	struct sbus_attach_args *sa = aux;
    222 	struct le_softc *lesc = (struct le_softc *)self;
    223 	struct lance_softc *sc = &lesc->sc_am7990.lsc;
    224 	struct sbusdev *sd;
    225 	bus_space_handle_t bh;
    226 	/* XXX the following declarations should be elsewhere */
    227 	extern void myetheraddr __P((u_char *));
    228 
    229 
    230 	lesc->sc_bustag = sa->sa_bustag;
    231 	lesc->sc_dmatag = sa->sa_dmatag;
    232 
    233 	if (sbus_bus_map(sa->sa_bustag,
    234 			 sa->sa_slot,
    235 			 sa->sa_offset,
    236 			 sizeof(struct lereg1),
    237 			 BUS_SPACE_MAP_LINEAR,
    238 			 0, &bh) != 0) {
    239 		printf("%s @ sbus: cannot map registers\n", self->dv_xname);
    240 		return;
    241 	}
    242 	lesc->sc_r1 = (struct lereg1 *)bh;
    243 
    244 	/*
    245 	 * Look for an "unallocated" lebuffer and pair it with
    246 	 * this `le' device on the assumption that we're on
    247 	 * a pre-historic ROM that doesn't establish le<=>lebuffer
    248 	 * parent-child relationships.
    249 	 */
    250 	for (sd = ((struct sbus_softc *)parent)->sc_sbdev; sd != NULL;
    251 	     sd = sd->sd_bchain) {
    252 
    253 		struct lebuf_softc *lebuf = (struct lebuf_softc *)sd->sd_dev;
    254 
    255 		if (strncmp("lebuffer", sd->sd_dev->dv_xname, 8) != 0)
    256 			continue;
    257 
    258 		if (lebuf->attached != 0)
    259 			continue;
    260 
    261 		sc->sc_mem = lebuf->sc_buffer;
    262 		sc->sc_memsize = lebuf->sc_bufsiz;
    263 		sc->sc_addr = 0; /* Lance view is offset by buffer location */
    264 		lebuf->attached = 1;
    265 
    266 		/* That old black magic... */
    267 		sc->sc_conf3 = getpropint(sa->sa_node,
    268 					  "busmaster-regval",
    269 					  LE_C3_BSWP | LE_C3_ACON | LE_C3_BCON);
    270 		break;
    271 	}
    272 
    273 	lesc->sc_sd.sd_reset = (void *)lance_reset;
    274 	sbus_establish(&lesc->sc_sd, &sc->sc_dev);
    275 
    276 	if (sa->sa_bp != NULL && strcmp(sa->sa_bp->name, le_cd.cd_name) == 0 &&
    277 	    SAME_LANCE(sa->sa_bp, sa))
    278 		sa->sa_bp->dev = &sc->sc_dev;
    279 
    280 	if (sc->sc_mem == 0) {
    281 		bus_dma_segment_t seg;
    282 		int rseg, error;
    283 
    284 		error = bus_dmamem_alloc(lesc->sc_dmatag, MEMSIZE, NBPG, 0,
    285 					 &seg, 1, &rseg,
    286 					 BUS_DMA_NOWAIT | BUS_DMA_24BIT);
    287 		if (error) {
    288 			printf("if_le: DMA buffer alloc error %d\n", error);
    289 			return;
    290 		}
    291 		error = bus_dmamem_map(lesc->sc_dmatag, &seg, rseg, MEMSIZE,
    292 				       (caddr_t *)&sc->sc_mem,
    293 				       BUS_DMA_NOWAIT|BUS_DMA_COHERENT);
    294 		if (error) {
    295 			printf("if_le: DMA buffer map error %d\n", error);
    296 			return;
    297 		}
    298 
    299 		sc->sc_addr = seg.ds_addr & 0xffffff;
    300 #if defined (SUN4M)
    301 		if ((sc->sc_addr & 0xffffff) >=
    302 		    (sc->sc_addr & 0xffffff) + MEMSIZE)
    303 			panic("if_le: Lance buffer crosses 16MB boundary");
    304 #endif
    305 		sc->sc_memsize = MEMSIZE;
    306 		sc->sc_conf3 = LE_C3_BSWP | LE_C3_ACON | LE_C3_BCON;
    307 	}
    308 
    309 	myetheraddr(sc->sc_enaddr);
    310 
    311 	sc->sc_supmedia = lemedia;
    312 	sc->sc_nsupmedia = NLEMEDIA;
    313 	sc->sc_defaultmedia = lemedia[0];
    314 
    315 	sc->sc_copytodesc = lance_copytobuf_contig;
    316 	sc->sc_copyfromdesc = lance_copyfrombuf_contig;
    317 	sc->sc_copytobuf = lance_copytobuf_contig;
    318 	sc->sc_copyfrombuf = lance_copyfrombuf_contig;
    319 	sc->sc_zerobuf = lance_zerobuf_contig;
    320 
    321 	sc->sc_rdcsr = lerdcsr;
    322 	sc->sc_wrcsr = lewrcsr;
    323 
    324 	am7990_config(&lesc->sc_am7990);
    325 
    326 	(void)bus_intr_establish(lesc->sc_bustag, sa->sa_pri, 0,
    327 				 am7990_intr, sc);
    328 }
    329