Home | History | Annotate | Line # | Download | only in sbus
if_le_lebuffer.c revision 1.3
      1 /*	$NetBSD: if_le_lebuffer.c,v 1.3 1998/08/29 20:32:11 pk 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 
     41 #include "opt_inet.h"
     42 #include "bpfilter.h"
     43 
     44 #include <sys/param.h>
     45 #include <sys/systm.h>
     46 #include <sys/mbuf.h>
     47 #include <sys/syslog.h>
     48 #include <sys/socket.h>
     49 #include <sys/device.h>
     50 #include <sys/malloc.h>
     51 
     52 #include <net/if.h>
     53 #include <net/if_ether.h>
     54 #include <net/if_media.h>
     55 
     56 #ifdef INET
     57 #include <netinet/in.h>
     58 #include <netinet/if_inarp.h>
     59 #endif
     60 
     61 #include <machine/autoconf.h>
     62 #include <machine/cpu.h>
     63 
     64 #include <dev/sbus/sbusvar.h>
     65 #include <dev/sbus/lebuffervar.h>
     66 
     67 #include <dev/ic/lancereg.h>
     68 #include <dev/ic/lancevar.h>
     69 #include <dev/ic/am7990reg.h>
     70 #include <dev/ic/am7990var.h>
     71 
     72 /*
     73  * LANCE registers.
     74  */
     75 #define LEREG1_RDP	0	/* Register Data port */
     76 #define LEREG1_RAP	2	/* Register Address port */
     77 
     78 struct	le_softc {
     79 	struct	am7990_softc	sc_am7990;	/* glue to MI code */
     80 	struct	sbusdev		sc_sd;		/* sbus device */
     81 	bus_space_tag_t		sc_bustag;
     82 	bus_dma_tag_t		sc_dmatag;
     83 	bus_space_handle_t	sc_reg;		/* LANCE registers */
     84 };
     85 
     86 
     87 int	lematch_lebuffer __P((struct device *, struct cfdata *, void *));
     88 void	leattach_lebuffer __P((struct device *, struct device *, void *));
     89 
     90 /*
     91  * Media types supported.
     92  */
     93 static int lemedia[] = {
     94 	IFM_ETHER|IFM_10_T,
     95 };
     96 #define NLEMEDIA	(sizeof(lemedia) / sizeof(lemedia[0]))
     97 
     98 struct cfattach le_lebuffer_ca = {
     99 	sizeof(struct le_softc), lematch_lebuffer, leattach_lebuffer
    100 };
    101 
    102 extern struct cfdriver le_cd;
    103 
    104 #if defined(_KERNEL) && !defined(_LKM)
    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 int
    155 lematch_lebuffer(parent, cf, aux)
    156 	struct device *parent;
    157 	struct cfdata *cf;
    158 	void *aux;
    159 {
    160 	struct sbus_attach_args *sa = aux;
    161 
    162 	return (strcmp(cf->cf_driver->cd_name, sa->sa_name) == 0);
    163 }
    164 
    165 
    166 #define SAME_LANCE(bp, sa) \
    167 	(bp->val[0] == sa->sa_slot && bp->val[1] == sa->sa_offset)
    168 
    169 
    170 void
    171 leattach_lebuffer(parent, self, aux)
    172 	struct device *parent, *self;
    173 	void *aux;
    174 {
    175 	struct sbus_attach_args *sa = aux;
    176 	struct le_softc *lesc = (struct le_softc *)self;
    177 	struct lance_softc *sc = &lesc->sc_am7990.lsc;
    178 	struct lebuf_softc *lebuf = (struct lebuf_softc *)parent;
    179 	/* XXX the following declarations should be elsewhere */
    180 	extern void myetheraddr __P((u_char *));
    181 
    182 	lesc->sc_bustag = sa->sa_bustag;
    183 	lesc->sc_dmatag = sa->sa_dmatag;
    184 
    185 	if (bus_space_map2(sa->sa_bustag,
    186 			   sa->sa_slot,
    187 			   sa->sa_offset,
    188 			   sa->sa_size,
    189 			   BUS_SPACE_MAP_LINEAR,
    190 			   0, &lesc->sc_reg)) {
    191 		printf("%s @ lebuffer: cannot map registers\n", self->dv_xname);
    192 		return;
    193 	}
    194 
    195 	sc->sc_mem = lebuf->sc_buffer;
    196 	sc->sc_memsize = lebuf->sc_bufsiz;
    197 	sc->sc_addr = 0; /* Lance view is offset by buffer location */
    198 	lebuf->attached = 1;
    199 
    200 	/* That old black magic... */
    201 	sc->sc_conf3 = getpropint(sa->sa_node, "busmaster-regval",
    202 				  LE_C3_BSWP | LE_C3_ACON | LE_C3_BCON);
    203 
    204 	/* Assume SBus is grandparent */
    205 	lesc->sc_sd.sd_reset = (void *)lance_reset;
    206 	sbus_establish(&lesc->sc_sd, parent);
    207 
    208 	if (sa->sa_bp != NULL && strcmp(sa->sa_bp->name, le_cd.cd_name) == 0 &&
    209 	    SAME_LANCE(sa->sa_bp, sa))
    210 		sa->sa_bp->dev = &sc->sc_dev;
    211 
    212 	sc->sc_supmedia = lemedia;
    213 	sc->sc_nsupmedia = NLEMEDIA;
    214 	sc->sc_defaultmedia = lemedia[0];
    215 
    216 	myetheraddr(sc->sc_enaddr);
    217 
    218 	sc->sc_copytodesc = lance_copytobuf_contig;
    219 	sc->sc_copyfromdesc = lance_copyfrombuf_contig;
    220 	sc->sc_copytobuf = lance_copytobuf_contig;
    221 	sc->sc_copyfrombuf = lance_copyfrombuf_contig;
    222 	sc->sc_zerobuf = lance_zerobuf_contig;
    223 
    224 	sc->sc_rdcsr = lerdcsr;
    225 	sc->sc_wrcsr = lewrcsr;
    226 
    227 	am7990_config(&lesc->sc_am7990);
    228 
    229 	(void)bus_intr_establish(lesc->sc_bustag, sa->sa_pri, 0,
    230 				 am7990_intr, sc);
    231 }
    232