Home | History | Annotate | Line # | Download | only in sbus
if_le.c revision 1.39
      1 /*	$NetBSD: if_le.c,v 1.39 2009/09/17 16:28:12 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  *
     20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30  * POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 #include <sys/cdefs.h>
     34 __KERNEL_RCSID(0, "$NetBSD: if_le.c,v 1.39 2009/09/17 16:28:12 tsutsui Exp $");
     35 
     36 #include "opt_inet.h"
     37 #include "bpfilter.h"
     38 
     39 #include <sys/param.h>
     40 #include <sys/systm.h>
     41 #include <sys/mbuf.h>
     42 #include <sys/syslog.h>
     43 #include <sys/socket.h>
     44 #include <sys/device.h>
     45 #include <sys/malloc.h>
     46 
     47 #include <net/if.h>
     48 #include <net/if_ether.h>
     49 #include <net/if_media.h>
     50 
     51 #include <sys/bus.h>
     52 #include <sys/intr.h>
     53 #include <machine/autoconf.h>
     54 
     55 #include <dev/sbus/sbusvar.h>
     56 #include <dev/sbus/lebuffervar.h>	/*XXX*/
     57 
     58 #include <dev/ic/lancereg.h>
     59 #include <dev/ic/lancevar.h>
     60 #include <dev/ic/am7990reg.h>
     61 #include <dev/ic/am7990var.h>
     62 
     63 #include "ioconf.h"
     64 
     65 /*
     66  * LANCE registers.
     67  */
     68 #define LEREG1_RDP	0	/* Register Data port */
     69 #define LEREG1_RAP	2	/* Register Address port */
     70 
     71 struct	le_softc {
     72 	struct	am7990_softc	sc_am7990;	/* glue to MI code */
     73 	bus_space_tag_t		sc_bustag;
     74 	bus_dma_tag_t		sc_dmatag;
     75 	bus_dmamap_t		sc_dmamap;
     76 	bus_space_handle_t	sc_reg;
     77 };
     78 
     79 #define MEMSIZE 0x4000		/* LANCE memory size */
     80 
     81 int	lematch_sbus(device_t, cfdata_t, void *);
     82 void	leattach_sbus(device_t, device_t, void *);
     83 
     84 /*
     85  * Media types supported.
     86  */
     87 static int lemedia[] = {
     88 	IFM_ETHER|IFM_10_5,
     89 };
     90 #define NLEMEDIA	__arraycount(lemedia)
     91 
     92 CFATTACH_DECL_NEW(le_sbus, sizeof(struct le_softc),
     93     lematch_sbus, leattach_sbus, NULL, NULL);
     94 
     95 #if defined(_KERNEL_OPT)
     96 #include "opt_ddb.h"
     97 #endif
     98 
     99 static void lewrcsr(struct lance_softc *, uint16_t, uint16_t);
    100 static uint16_t lerdcsr(struct lance_softc *, uint16_t);
    101 
    102 static void
    103 lewrcsr(struct lance_softc *sc, uint16_t port, uint16_t val)
    104 {
    105 	struct le_softc *lesc = (struct le_softc *)sc;
    106 	bus_space_tag_t t = lesc->sc_bustag;
    107 	bus_space_handle_t h = lesc->sc_reg;
    108 
    109 	bus_space_write_2(t, h, LEREG1_RAP, port);
    110 	bus_space_write_2(t, h, LEREG1_RDP, val);
    111 
    112 #if defined(SUN4M)
    113 	/*
    114 	 * We need to flush the Sbus->Mbus write buffers. This can most
    115 	 * easily be accomplished by reading back the register that we
    116 	 * just wrote (thanks to Chris Torek for this solution).
    117 	 */
    118 	(void)bus_space_read_2(t, h, LEREG1_RDP);
    119 #endif
    120 }
    121 
    122 static uint16_t
    123 lerdcsr(struct lance_softc *sc, uint16_t port)
    124 {
    125 	struct le_softc *lesc = (struct le_softc *)sc;
    126 	bus_space_tag_t t = lesc->sc_bustag;
    127 	bus_space_handle_t h = lesc->sc_reg;
    128 
    129 	bus_space_write_2(t, h, LEREG1_RAP, port);
    130 	return (bus_space_read_2(t, h, LEREG1_RDP));
    131 }
    132 
    133 
    134 int
    135 lematch_sbus(device_t parent, cfdata_t cf, void *aux)
    136 {
    137 	struct sbus_attach_args *sa = aux;
    138 
    139 	return (strcmp(cf->cf_name, sa->sa_name) == 0);
    140 }
    141 
    142 void
    143 leattach_sbus(device_t parent, device_t self, void *aux)
    144 {
    145 	struct le_softc *lesc = device_private(self);
    146 	struct lance_softc *sc = &lesc->sc_am7990.lsc;
    147 	struct sbus_attach_args *sa = aux;
    148 	bus_dma_tag_t dmatag;
    149 	cfdriver_t lebufcd;
    150 
    151 	sc->sc_dev = self;
    152 	lesc->sc_bustag = sa->sa_bustag;
    153 	lesc->sc_dmatag = dmatag = sa->sa_dmatag;
    154 
    155 	if (sbus_bus_map(sa->sa_bustag,
    156 			 sa->sa_slot,
    157 			 sa->sa_offset,
    158 			 sa->sa_size,
    159 			 0, &lesc->sc_reg) != 0) {
    160 		aprint_error(": cannot map registers\n");
    161 		return;
    162 	}
    163 
    164 	/*
    165 	 * Look for an "unallocated" lebuffer and pair it with
    166 	 * this `le' device on the assumption that we're on
    167 	 * a pre-historic ROM that doesn't establish le<=>lebuffer
    168 	 * parent-child relationships.
    169 	 */
    170 	lebufcd = config_cfdriver_lookup("lebuffer");
    171 	if (lebufcd != NULL) {
    172 		int unit;
    173 
    174 		/* Check all possible lebuffer units */
    175 		for (unit = 0; unit < lebufcd->cd_ndevs; unit++) {
    176 			device_t lebufdev;
    177 			struct lebuf_softc *lebufsc;
    178 
    179 			/* Check if unit is valid */
    180 			lebufdev = device_lookup(lebufcd, unit);
    181 			if (lebufdev == NULL)
    182 				continue;
    183 
    184 			/* Check if we have a common sbus parent */
    185 			if (parent != device_parent(lebufdev))
    186 				continue;
    187 			lebufsc = device_private(lebufdev);
    188 
    189 			/*
    190 			 * Check if this lebuffer unit is attached
    191 			 * but unused by its child, if_le_lebuffer.
    192 			 * XXX: this won't work if lebuffer is configured
    193 			 *      but not le at lebuffer?
    194 			 */
    195 			if (lebufsc->sc_buffer == 0 || lebufsc->attached != 0)
    196 				continue;
    197 
    198 			/* Assume this lebuffer is my pair */
    199 			sc->sc_mem = lebufsc->sc_buffer;
    200 			sc->sc_memsize = lebufsc->sc_bufsiz;
    201 
    202 			/* Lance view is offset by buffer location */
    203 			sc->sc_addr = 0;
    204 
    205 			/* Denote it */
    206 			aprint_normal(" (%s)", device_xname(lebufdev));
    207 			lebufsc->attached = 1;
    208 
    209 			/* That old black magic... */
    210 			sc->sc_conf3 = prom_getpropint(sa->sa_node,
    211 			    "busmaster-regval",
    212 			    LE_C3_BSWP | LE_C3_ACON | LE_C3_BCON);
    213 			break;
    214 		}
    215 	}
    216 
    217 	if (sc->sc_mem == 0) {
    218 		bus_dma_segment_t seg;
    219 		int rseg, error;
    220 
    221 #ifndef BUS_DMA_24BIT
    222 /* XXX - This flag is not defined on all archs */
    223 #define BUS_DMA_24BIT	0
    224 #endif
    225 		/* Get a DMA handle */
    226 		if ((error = bus_dmamap_create(dmatag, MEMSIZE, 1, MEMSIZE, 0,
    227 						BUS_DMA_NOWAIT|BUS_DMA_24BIT,
    228 						&lesc->sc_dmamap)) != 0) {
    229 			aprint_error(": DMA map create error %d\n", error);
    230 			return;
    231 		}
    232 
    233 		/* Allocate DMA buffer */
    234 		if ((error = bus_dmamem_alloc(dmatag, MEMSIZE, 0, 0,
    235 					 &seg, 1, &rseg,
    236 					 BUS_DMA_NOWAIT|BUS_DMA_24BIT)) != 0){
    237 			aprint_error(": DMA buffer allocation error %d\n",
    238 			    error);
    239 			return;
    240 		}
    241 
    242 		/* Map DMA buffer into kernel space */
    243 		if ((error = bus_dmamem_map(dmatag, &seg, rseg, MEMSIZE,
    244 				       (void **)&sc->sc_mem,
    245 				       BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) != 0) {
    246 			aprint_error(": DMA buffer map error %d\n", error);
    247 			bus_dmamem_free(lesc->sc_dmatag, &seg, rseg);
    248 			return;
    249 		}
    250 
    251 		/* Load DMA buffer */
    252 		if ((error = bus_dmamap_load(dmatag, lesc->sc_dmamap,
    253 		    sc->sc_mem, MEMSIZE, NULL, BUS_DMA_NOWAIT)) != 0) {
    254 			aprint_error(": DMA buffer map load error %d\n", error);
    255 			bus_dmamem_free(dmatag, &seg, rseg);
    256 			bus_dmamem_unmap(dmatag, sc->sc_mem, MEMSIZE);
    257 			return;
    258 		}
    259 
    260 		sc->sc_addr = lesc->sc_dmamap->dm_segs[0].ds_addr & 0xffffff;
    261 		sc->sc_memsize = MEMSIZE;
    262 		sc->sc_conf3 = LE_C3_BSWP | LE_C3_ACON | LE_C3_BCON;
    263 	}
    264 
    265 	prom_getether(sa->sa_node, sc->sc_enaddr);
    266 
    267 	sc->sc_supmedia = lemedia;
    268 	sc->sc_nsupmedia = NLEMEDIA;
    269 	sc->sc_defaultmedia = lemedia[0];
    270 
    271 	sc->sc_copytodesc = lance_copytobuf_contig;
    272 	sc->sc_copyfromdesc = lance_copyfrombuf_contig;
    273 	sc->sc_copytobuf = lance_copytobuf_contig;
    274 	sc->sc_copyfrombuf = lance_copyfrombuf_contig;
    275 	sc->sc_zerobuf = lance_zerobuf_contig;
    276 
    277 	sc->sc_rdcsr = lerdcsr;
    278 	sc->sc_wrcsr = lewrcsr;
    279 
    280 	am7990_config(&lesc->sc_am7990);
    281 
    282 	/* Establish interrupt handler */
    283 	if (sa->sa_nintr != 0)
    284 		(void)bus_intr_establish(lesc->sc_bustag, sa->sa_pri,
    285 					 IPL_NET, am7990_intr, sc);
    286 }
    287