Home | History | Annotate | Line # | Download | only in sbus
if_le.c revision 1.2.2.1
      1  1.2.2.1  eeh /*	$NetBSD: if_le.c,v 1.2.2.1 1998/08/08 03:06:50 eeh Exp $	*/
      2      1.1   pk 
      3      1.1   pk /*-
      4      1.1   pk  * Copyright (c) 1997 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.1   pk  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9      1.1   pk  * NASA Ames Research Center.
     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  * 3. All advertising materials mentioning features or use of this software
     20      1.1   pk  *    must display the following acknowledgement:
     21      1.1   pk  *	This product includes software developed by the NetBSD
     22      1.1   pk  *	Foundation, Inc. and its contributors.
     23      1.1   pk  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24      1.1   pk  *    contributors may be used to endorse or promote products derived
     25      1.1   pk  *    from this software without specific prior written permission.
     26      1.1   pk  *
     27      1.1   pk  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28      1.1   pk  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29      1.1   pk  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30      1.1   pk  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31      1.1   pk  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32      1.1   pk  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33      1.1   pk  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34      1.1   pk  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35      1.1   pk  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36      1.1   pk  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37      1.1   pk  * POSSIBILITY OF SUCH DAMAGE.
     38      1.1   pk  */
     39      1.1   pk 
     40      1.1   pk /*-
     41      1.1   pk  * Copyright (c) 1996
     42      1.1   pk  *	The President and Fellows of Harvard College. All rights reserved.
     43      1.1   pk  * Copyright (c) 1995 Charles M. Hannum.  All rights reserved.
     44      1.1   pk  * Copyright (c) 1992, 1993
     45      1.1   pk  *	The Regents of the University of California.  All rights reserved.
     46      1.1   pk  *
     47      1.1   pk  * This code is derived from software contributed to Berkeley by
     48      1.1   pk  * Ralph Campbell and Rick Macklem.
     49      1.1   pk  *
     50      1.1   pk  * Redistribution and use in source and binary forms, with or without
     51      1.1   pk  * modification, are permitted provided that the following conditions
     52      1.1   pk  * are met:
     53      1.1   pk  * 1. Redistributions of source code must retain the above copyright
     54      1.1   pk  *    notice, this list of conditions and the following disclaimer.
     55      1.1   pk  * 2. Redistributions in binary form must reproduce the above copyright
     56      1.1   pk  *    notice, this list of conditions and the following disclaimer in the
     57      1.1   pk  *    documentation and/or other materials provided with the distribution.
     58      1.1   pk  * 3. All advertising materials mentioning features or use of this software
     59      1.1   pk  *    must display the following acknowledgement:
     60      1.1   pk  *	This product includes software developed by Aaron Brown and
     61      1.1   pk  *	Harvard University.
     62      1.1   pk  *	This product includes software developed by the University of
     63      1.1   pk  *	California, Berkeley and its contributors.
     64      1.1   pk  * 4. Neither the name of the University nor the names of its contributors
     65      1.1   pk  *    may be used to endorse or promote products derived from this software
     66      1.1   pk  *    without specific prior written permission.
     67      1.1   pk  *
     68      1.1   pk  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     69      1.1   pk  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     70      1.1   pk  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     71      1.1   pk  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     72      1.1   pk  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     73      1.1   pk  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     74      1.1   pk  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     75      1.1   pk  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     76      1.1   pk  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     77      1.1   pk  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     78      1.1   pk  * SUCH DAMAGE.
     79      1.1   pk  *
     80      1.1   pk  *	@(#)if_le.c	8.2 (Berkeley) 11/16/93
     81      1.1   pk  */
     82      1.1   pk 
     83      1.1   pk #include "opt_inet.h"
     84      1.1   pk #include "bpfilter.h"
     85      1.1   pk 
     86      1.1   pk #include <sys/param.h>
     87      1.1   pk #include <sys/systm.h>
     88      1.1   pk #include <sys/mbuf.h>
     89      1.1   pk #include <sys/syslog.h>
     90      1.1   pk #include <sys/socket.h>
     91      1.1   pk #include <sys/device.h>
     92      1.1   pk #include <sys/malloc.h>
     93      1.1   pk 
     94      1.1   pk #include <net/if.h>
     95      1.1   pk #include <net/if_ether.h>
     96      1.1   pk #include <net/if_media.h>
     97      1.1   pk 
     98      1.1   pk #ifdef INET
     99      1.1   pk #include <netinet/in.h>
    100      1.1   pk #include <netinet/if_inarp.h>
    101      1.1   pk #endif
    102      1.1   pk 
    103      1.1   pk #include <machine/autoconf.h>
    104      1.1   pk #include <machine/cpu.h>
    105      1.1   pk 
    106      1.1   pk #include <dev/sbus/sbusvar.h>
    107      1.1   pk #include <dev/sbus/lebuffervar.h>	/*XXX*/
    108      1.1   pk 
    109      1.1   pk #include <dev/ic/lancereg.h>
    110      1.1   pk #include <dev/ic/lancevar.h>
    111      1.1   pk #include <dev/ic/am7990reg.h>
    112      1.1   pk #include <dev/ic/am7990var.h>
    113      1.1   pk 
    114      1.1   pk /*
    115      1.1   pk  * LANCE registers.
    116      1.1   pk  */
    117      1.1   pk struct lereg1 {
    118      1.1   pk 	volatile u_int16_t	ler1_rdp;	/* data port */
    119      1.1   pk 	volatile u_int16_t	ler1_rap;	/* register select port */
    120      1.1   pk };
    121      1.1   pk 
    122      1.1   pk struct	le_softc {
    123      1.1   pk 	struct	am7990_softc sc_am7990;	/* glue to MI code */
    124      1.1   pk 	struct	sbusdev sc_sd;		/* sbus device */
    125      1.1   pk 	bus_space_tag_t	sc_bustag;
    126      1.1   pk 	bus_dma_tag_t	sc_dmatag;
    127      1.1   pk 	struct	lereg1 *sc_r1;		/* LANCE registers */
    128      1.1   pk };
    129      1.1   pk 
    130      1.1   pk #define MEMSIZE 0x4000		/* LANCE memory size */
    131      1.1   pk 
    132      1.1   pk int	lematch_sbus __P((struct device *, struct cfdata *, void *));
    133      1.1   pk void	leattach_sbus __P((struct device *, struct device *, void *));
    134      1.1   pk 
    135      1.1   pk /*
    136      1.1   pk  * Media types supported.
    137      1.1   pk  */
    138      1.1   pk static int lemedia[] = {
    139      1.2   pk 	IFM_ETHER|IFM_10_5,
    140      1.1   pk };
    141      1.1   pk #define NLEMEDIA	(sizeof(lemedia) / sizeof(lemedia[0]))
    142      1.1   pk 
    143      1.1   pk 
    144      1.1   pk struct cfattach le_sbus_ca = {
    145      1.1   pk 	sizeof(struct le_softc), lematch_sbus, leattach_sbus
    146      1.1   pk };
    147      1.1   pk 
    148      1.1   pk extern struct cfdriver le_cd;
    149      1.1   pk 
    150      1.1   pk #if defined(_KERNEL) && !defined(_LKM)
    151      1.1   pk #include "opt_ddb.h"
    152      1.1   pk #endif
    153      1.1   pk 
    154      1.1   pk #ifdef DDB
    155      1.1   pk #define	integrate
    156      1.1   pk #define hide
    157      1.1   pk #else
    158      1.1   pk #define	integrate	static __inline
    159      1.1   pk #define hide		static
    160      1.1   pk #endif
    161      1.1   pk 
    162      1.1   pk static void lewrcsr __P((struct lance_softc *, u_int16_t, u_int16_t));
    163      1.1   pk static u_int16_t lerdcsr __P((struct lance_softc *, u_int16_t));
    164      1.1   pk 
    165      1.1   pk static void
    166      1.1   pk lewrcsr(sc, port, val)
    167      1.1   pk 	struct lance_softc *sc;
    168      1.1   pk 	u_int16_t port, val;
    169      1.1   pk {
    170      1.1   pk 	struct lereg1 *ler1 = ((struct le_softc *)sc)->sc_r1;
    171      1.1   pk 
    172      1.1   pk 	ler1->ler1_rap = port;
    173      1.1   pk 	ler1->ler1_rdp = val;
    174      1.1   pk 
    175      1.1   pk #if defined(SUN4M)
    176      1.1   pk 	/*
    177      1.1   pk 	 * We need to flush the Sbus->Mbus write buffers. This can most
    178      1.1   pk 	 * easily be accomplished by reading back the register that we
    179      1.1   pk 	 * just wrote (thanks to Chris Torek for this solution).
    180      1.1   pk 	 */
    181      1.1   pk 	if (CPU_ISSUN4M) {
    182      1.1   pk 		volatile u_int16_t discard;
    183      1.1   pk 		discard = ler1->ler1_rdp;
    184      1.1   pk 	}
    185      1.1   pk #endif
    186      1.1   pk }
    187      1.1   pk 
    188      1.1   pk static u_int16_t
    189      1.1   pk lerdcsr(sc, port)
    190      1.1   pk 	struct lance_softc *sc;
    191      1.1   pk 	u_int16_t port;
    192      1.1   pk {
    193      1.1   pk 	struct lereg1 *ler1 = ((struct le_softc *)sc)->sc_r1;
    194      1.1   pk 	u_int16_t val;
    195      1.1   pk 
    196      1.1   pk 	ler1->ler1_rap = port;
    197      1.1   pk 	val = ler1->ler1_rdp;
    198      1.1   pk 	return (val);
    199      1.1   pk }
    200      1.1   pk 
    201      1.1   pk 
    202      1.1   pk int
    203      1.1   pk lematch_sbus(parent, cf, aux)
    204      1.1   pk 	struct device *parent;
    205      1.1   pk 	struct cfdata *cf;
    206      1.1   pk 	void *aux;
    207      1.1   pk {
    208      1.1   pk 	struct sbus_attach_args *sa = aux;
    209      1.1   pk 
    210      1.1   pk 	return (strcmp(cf->cf_driver->cd_name, sa->sa_name) == 0);
    211      1.1   pk }
    212      1.1   pk 
    213      1.1   pk #define SAME_LANCE(bp, sa) \
    214      1.1   pk 	((bp->val[0] == sa->sa_slot && bp->val[1] == sa->sa_offset) || \
    215      1.1   pk 	 (bp->val[0] == -1 && bp->val[1] == sc->sc_dev.dv_unit))
    216      1.1   pk 
    217      1.1   pk void
    218      1.1   pk leattach_sbus(parent, self, aux)
    219      1.1   pk 	struct device *parent, *self;
    220      1.1   pk 	void *aux;
    221      1.1   pk {
    222      1.1   pk 	struct sbus_attach_args *sa = aux;
    223      1.1   pk 	struct le_softc *lesc = (struct le_softc *)self;
    224      1.1   pk 	struct lance_softc *sc = &lesc->sc_am7990.lsc;
    225      1.1   pk 	struct sbusdev *sd;
    226      1.1   pk 	bus_space_handle_t bh;
    227      1.1   pk 	/* XXX the following declarations should be elsewhere */
    228      1.1   pk 	extern void myetheraddr __P((u_char *));
    229      1.1   pk 
    230      1.1   pk 
    231      1.1   pk 	lesc->sc_bustag = sa->sa_bustag;
    232      1.1   pk 	lesc->sc_dmatag = sa->sa_dmatag;
    233      1.1   pk 
    234      1.1   pk 	if (sbus_bus_map(sa->sa_bustag,
    235      1.1   pk 			 sa->sa_slot,
    236      1.1   pk 			 sa->sa_offset,
    237      1.1   pk 			 sizeof(struct lereg1),
    238      1.1   pk 			 BUS_SPACE_MAP_LINEAR,
    239      1.1   pk 			 0, &bh) != 0) {
    240      1.1   pk 		printf("%s @ sbus: cannot map registers\n", self->dv_xname);
    241      1.1   pk 		return;
    242      1.1   pk 	}
    243      1.1   pk 	lesc->sc_r1 = (struct lereg1 *)bh;
    244      1.1   pk 
    245      1.1   pk 	/*
    246      1.1   pk 	 * Look for an "unallocated" lebuffer and pair it with
    247      1.1   pk 	 * this `le' device on the assumption that we're on
    248      1.1   pk 	 * a pre-historic ROM that doesn't establish le<=>lebuffer
    249      1.1   pk 	 * parent-child relationships.
    250      1.1   pk 	 */
    251      1.1   pk 	for (sd = ((struct sbus_softc *)parent)->sc_sbdev; sd != NULL;
    252      1.1   pk 	     sd = sd->sd_bchain) {
    253      1.1   pk 
    254      1.1   pk 		struct lebuf_softc *lebuf = (struct lebuf_softc *)sd->sd_dev;
    255      1.1   pk 
    256      1.1   pk 		if (strncmp("lebuffer", sd->sd_dev->dv_xname, 8) != 0)
    257      1.1   pk 			continue;
    258      1.1   pk 
    259      1.1   pk 		if (lebuf->attached != 0)
    260      1.1   pk 			continue;
    261      1.1   pk 
    262      1.1   pk 		sc->sc_mem = lebuf->sc_buffer;
    263      1.1   pk 		sc->sc_memsize = lebuf->sc_bufsiz;
    264      1.1   pk 		sc->sc_addr = 0; /* Lance view is offset by buffer location */
    265      1.1   pk 		lebuf->attached = 1;
    266      1.1   pk 
    267      1.1   pk 		/* That old black magic... */
    268      1.1   pk 		sc->sc_conf3 = getpropint(sa->sa_node,
    269      1.1   pk 					  "busmaster-regval",
    270      1.1   pk 					  LE_C3_BSWP | LE_C3_ACON | LE_C3_BCON);
    271      1.1   pk 		break;
    272      1.1   pk 	}
    273      1.1   pk 
    274      1.1   pk 	lesc->sc_sd.sd_reset = (void *)lance_reset;
    275      1.1   pk 	sbus_establish(&lesc->sc_sd, &sc->sc_dev);
    276      1.1   pk 
    277      1.1   pk 	if (sa->sa_bp != NULL && strcmp(sa->sa_bp->name, le_cd.cd_name) == 0 &&
    278      1.1   pk 	    SAME_LANCE(sa->sa_bp, sa))
    279      1.1   pk 		sa->sa_bp->dev = &sc->sc_dev;
    280      1.1   pk 
    281      1.1   pk 	if (sc->sc_mem == 0) {
    282      1.1   pk 		bus_dma_segment_t seg;
    283      1.1   pk 		int rseg, error;
    284      1.1   pk 
    285  1.2.2.1  eeh 		error = bus_dmamem_alloc(lesc->sc_dmatag, MEMSIZE, NBPG, 0,
    286  1.2.2.1  eeh 					 &seg, 1, &rseg,
    287  1.2.2.1  eeh 					 BUS_DMA_NOWAIT | BUS_DMA_24BIT);
    288      1.1   pk 		if (error) {
    289      1.1   pk 			printf("if_le: DMA buffer alloc error %d\n", error);
    290      1.1   pk 			return;
    291      1.1   pk 		}
    292  1.2.2.1  eeh 		error = bus_dmamem_map(lesc->sc_dmatag, &seg, rseg, MEMSIZE,
    293      1.1   pk 				       (caddr_t *)&sc->sc_mem,
    294  1.2.2.1  eeh 				       BUS_DMA_NOWAIT|BUS_DMA_COHERENT);
    295      1.1   pk 		if (error) {
    296      1.1   pk 			printf("if_le: DMA buffer map error %d\n", error);
    297      1.1   pk 			return;
    298      1.1   pk 		}
    299      1.1   pk 
    300      1.1   pk 		sc->sc_addr = seg.ds_addr & 0xffffff;
    301      1.1   pk #if defined (SUN4M)
    302      1.1   pk 		if ((sc->sc_addr & 0xffffff) >=
    303      1.1   pk 		    (sc->sc_addr & 0xffffff) + MEMSIZE)
    304      1.1   pk 			panic("if_le: Lance buffer crosses 16MB boundary");
    305      1.1   pk #endif
    306      1.1   pk 		sc->sc_memsize = MEMSIZE;
    307      1.1   pk 		sc->sc_conf3 = LE_C3_BSWP | LE_C3_ACON | LE_C3_BCON;
    308      1.1   pk 	}
    309      1.1   pk 
    310      1.1   pk 	myetheraddr(sc->sc_enaddr);
    311      1.1   pk 
    312      1.1   pk 	sc->sc_supmedia = lemedia;
    313      1.1   pk 	sc->sc_nsupmedia = NLEMEDIA;
    314      1.1   pk 	sc->sc_defaultmedia = lemedia[0];
    315      1.1   pk 
    316      1.1   pk 	sc->sc_copytodesc = lance_copytobuf_contig;
    317      1.1   pk 	sc->sc_copyfromdesc = lance_copyfrombuf_contig;
    318      1.1   pk 	sc->sc_copytobuf = lance_copytobuf_contig;
    319      1.1   pk 	sc->sc_copyfrombuf = lance_copyfrombuf_contig;
    320      1.1   pk 	sc->sc_zerobuf = lance_zerobuf_contig;
    321      1.1   pk 
    322      1.1   pk 	sc->sc_rdcsr = lerdcsr;
    323      1.1   pk 	sc->sc_wrcsr = lewrcsr;
    324      1.1   pk 
    325      1.1   pk 	am7990_config(&lesc->sc_am7990);
    326      1.1   pk 
    327      1.1   pk 	(void)bus_intr_establish(lesc->sc_bustag, sa->sa_pri, 0,
    328      1.1   pk 				 am7990_intr, sc);
    329      1.1   pk }
    330