Home | History | Annotate | Line # | Download | only in sbus
if_le_lebuffer.c revision 1.23
      1 /*	$NetBSD: if_le_lebuffer.c,v 1.23 2008/04/04 12:25:07 tsutsui 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_lebuffer.c,v 1.23 2008/04/04 12:25:07 tsutsui 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 #ifdef INET
     59 #include <netinet/in.h>
     60 #include <netinet/if_inarp.h>
     61 #endif
     62 
     63 #include <sys/bus.h>
     64 #include <sys/intr.h>
     65 #include <machine/autoconf.h>
     66 
     67 #include <dev/sbus/sbusvar.h>
     68 #include <dev/sbus/lebuffervar.h>
     69 
     70 #include <dev/ic/lancereg.h>
     71 #include <dev/ic/lancevar.h>
     72 #include <dev/ic/am7990reg.h>
     73 #include <dev/ic/am7990var.h>
     74 
     75 #include "ioconf.h"
     76 
     77 /*
     78  * LANCE registers.
     79  */
     80 #define LEREG1_RDP	0	/* Register Data port */
     81 #define LEREG1_RAP	2	/* Register Address port */
     82 
     83 struct	le_softc {
     84 	struct	am7990_softc	sc_am7990;	/* glue to MI code */
     85 	struct	sbusdev		sc_sd;		/* sbus device */
     86 	bus_space_tag_t		sc_bustag;
     87 	bus_dma_tag_t		sc_dmatag;
     88 	bus_space_handle_t	sc_reg;		/* LANCE registers */
     89 };
     90 
     91 
     92 int	lematch_lebuffer(device_t, cfdata_t, void *);
     93 void	leattach_lebuffer(device_t, device_t, void *);
     94 
     95 /*
     96  * Media types supported.
     97  */
     98 static int lemedia[] = {
     99 	IFM_ETHER|IFM_10_T,
    100 };
    101 #define NLEMEDIA	__arraycount(lemedia)
    102 
    103 CFATTACH_DECL_NEW(le_lebuffer, sizeof(struct le_softc),
    104     lematch_lebuffer, leattach_lebuffer, NULL, NULL);
    105 
    106 #if defined(_KERNEL_OPT)
    107 #include "opt_ddb.h"
    108 #endif
    109 
    110 static void lewrcsr(struct lance_softc *, uint16_t, uint16_t);
    111 static uint16_t lerdcsr(struct lance_softc *, uint16_t);
    112 
    113 static void
    114 lewrcsr(struct lance_softc *sc, uint16_t port, uint16_t val)
    115 {
    116 	struct le_softc *lesc = (struct le_softc *)sc;
    117 	bus_space_tag_t t = lesc->sc_bustag;
    118 	bus_space_handle_t h = lesc->sc_reg;
    119 
    120 	bus_space_write_2(t, h, LEREG1_RAP, port);
    121 	bus_space_write_2(t, h, LEREG1_RDP, val);
    122 
    123 #if defined(SUN4M)
    124 	/*
    125 	 * We need to flush the Sbus->Mbus write buffers. This can most
    126 	 * easily be accomplished by reading back the register that we
    127 	 * just wrote (thanks to Chris Torek for this solution).
    128 	 */
    129 	(void)bus_space_read_2(t, h, LEREG1_RDP);
    130 #endif
    131 }
    132 
    133 static uint16_t
    134 lerdcsr(struct lance_softc *sc, uint16_t port)
    135 {
    136 	struct le_softc *lesc = (struct le_softc *)sc;
    137 	bus_space_tag_t t = lesc->sc_bustag;
    138 	bus_space_handle_t h = lesc->sc_reg;
    139 
    140 	bus_space_write_2(t, h, LEREG1_RAP, port);
    141 	return (bus_space_read_2(t, h, LEREG1_RDP));
    142 }
    143 
    144 int
    145 lematch_lebuffer(device_t parent, cfdata_t cf, void *aux)
    146 {
    147 	struct sbus_attach_args *sa = aux;
    148 
    149 	return (strcmp(cf->cf_name, sa->sa_name) == 0);
    150 }
    151 
    152 
    153 void
    154 leattach_lebuffer(device_t parent, device_t self, void *aux)
    155 {
    156 	struct le_softc *lesc = device_private(self);
    157 	struct lance_softc *sc = &lesc->sc_am7990.lsc;
    158 	struct lebuf_softc *lebuf = device_private(parent);
    159 	struct sbus_attach_args *sa = aux;
    160 
    161 	sc->sc_dev = self;
    162 	lesc->sc_bustag = sa->sa_bustag;
    163 	lesc->sc_dmatag = sa->sa_dmatag;
    164 
    165 	if (sbus_bus_map(sa->sa_bustag,
    166 			 sa->sa_slot,
    167 			 sa->sa_offset,
    168 			 sa->sa_size,
    169 			 0, &lesc->sc_reg)) {
    170 		aprint_error(": cannot map registers\n");
    171 		return;
    172 	}
    173 
    174 	sc->sc_mem = lebuf->sc_buffer;
    175 	sc->sc_memsize = lebuf->sc_bufsiz;
    176 	sc->sc_addr = 0; /* Lance view is offset by buffer location */
    177 	lebuf->attached = 1;
    178 
    179 	/* That old black magic... */
    180 	sc->sc_conf3 = prom_getpropint(sa->sa_node, "busmaster-regval",
    181 				  LE_C3_BSWP | LE_C3_ACON | LE_C3_BCON);
    182 
    183 	/* Assume SBus is grandparent */
    184 	lesc->sc_sd.sd_reset = (void *)lance_reset;
    185 	sbus_establish(&lesc->sc_sd, parent);
    186 
    187 	sc->sc_supmedia = lemedia;
    188 	sc->sc_nsupmedia = NLEMEDIA;
    189 	sc->sc_defaultmedia = lemedia[0];
    190 
    191 	prom_getether(sa->sa_node, sc->sc_enaddr);
    192 
    193 	sc->sc_copytodesc = lance_copytobuf_contig;
    194 	sc->sc_copyfromdesc = lance_copyfrombuf_contig;
    195 	sc->sc_copytobuf = lance_copytobuf_contig;
    196 	sc->sc_copyfrombuf = lance_copyfrombuf_contig;
    197 	sc->sc_zerobuf = lance_zerobuf_contig;
    198 
    199 	sc->sc_rdcsr = lerdcsr;
    200 	sc->sc_wrcsr = lewrcsr;
    201 
    202 	am7990_config(&lesc->sc_am7990);
    203 
    204 	/* Establish interrupt handler */
    205 	if (sa->sa_nintr != 0)
    206 		(void)bus_intr_establish(lesc->sc_bustag, sa->sa_pri,
    207 					 IPL_NET, am7990_intr, sc);
    208 }
    209