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