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