Home | History | Annotate | Line # | Download | only in sbus
if_le_ledma.c revision 1.4
      1 /*	$NetBSD: if_le_ledma.c,v 1.4 1998/09/06 22:43:16 eeh 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 "opt_inet.h"
     41 #include "bpfilter.h"
     42 
     43 #include <sys/param.h>
     44 #include <sys/systm.h>
     45 #include <sys/mbuf.h>
     46 #include <sys/syslog.h>
     47 #include <sys/socket.h>
     48 #include <sys/device.h>
     49 #include <sys/malloc.h>
     50 
     51 #include <net/if.h>
     52 #include <net/if_ether.h>
     53 #include <net/if_media.h>
     54 
     55 #ifdef INET
     56 #include <netinet/in.h>
     57 #include <netinet/if_inarp.h>
     58 #endif
     59 
     60 #include <machine/autoconf.h>
     61 #include <machine/cpu.h>
     62 
     63 #include <dev/sbus/sbusvar.h>
     64 
     65 #include <dev/ic/lsi64854reg.h>
     66 #include <dev/ic/lsi64854var.h>
     67 
     68 #include <dev/ic/lancereg.h>
     69 #include <dev/ic/lancevar.h>
     70 #include <dev/ic/am7990reg.h>
     71 #include <dev/ic/am7990var.h>
     72 
     73 /*
     74  * LANCE registers.
     75  */
     76 #define LEREG1_RDP	0	/* Register Data port */
     77 #define LEREG1_RAP	2	/* Register Address port */
     78 
     79 struct	le_softc {
     80 	struct	am7990_softc	sc_am7990;	/* glue to MI code */
     81 	struct	sbusdev		sc_sd;		/* sbus device */
     82 	bus_space_tag_t		sc_bustag;
     83 	bus_space_handle_t	sc_reg;		/* LANCE registers */
     84 	struct	lsi64854_softc	*sc_dma;	/* pointer to my dma */
     85 	u_int			sc_laddr;	/* LANCE DMA address */
     86 };
     87 
     88 #define MEMSIZE		(16*1024)	/* LANCE memory size */
     89 #define LEDMA_BOUNDARY	(16*1024*1024)	/* must not cross 16MB boundary */
     90 
     91 int	lematch_ledma __P((struct device *, struct cfdata *, void *));
     92 void	leattach_ledma __P((struct device *, struct device *, void *));
     93 
     94 /*
     95  * Media types supported by the Sun4m.
     96  */
     97 static int lemedia[] = {
     98 	IFM_ETHER|IFM_10_T,
     99 	IFM_ETHER|IFM_10_5,
    100 	IFM_ETHER|IFM_AUTO,
    101 };
    102 #define NLEMEDIA	(sizeof(lemedia) / sizeof(lemedia[0]))
    103 
    104 void	lesetutp __P((struct lance_softc *));
    105 void	lesetaui __P((struct lance_softc *));
    106 
    107 int	lemediachange __P((struct lance_softc *));
    108 void	lemediastatus __P((struct lance_softc *, struct ifmediareq *));
    109 
    110 struct cfattach le_ledma_ca = {
    111 	sizeof(struct le_softc), lematch_ledma, leattach_ledma
    112 };
    113 
    114 extern struct cfdriver le_cd;
    115 
    116 #if defined(_KERNEL) && !defined(_LKM)
    117 #include "opt_ddb.h"
    118 #endif
    119 
    120 #ifdef DDB
    121 #define	integrate
    122 #define hide
    123 #else
    124 #define	integrate	static __inline
    125 #define hide		static
    126 #endif
    127 
    128 static void lewrcsr __P((struct lance_softc *, u_int16_t, u_int16_t));
    129 static u_int16_t lerdcsr __P((struct lance_softc *, u_int16_t));
    130 hide void lehwreset __P((struct lance_softc *));
    131 hide void lehwinit __P((struct lance_softc *));
    132 hide void lenocarrier __P((struct lance_softc *));
    133 
    134 static void
    135 lewrcsr(sc, port, val)
    136 	struct lance_softc *sc;
    137 	u_int16_t port, val;
    138 {
    139 	struct le_softc *lesc = (struct le_softc *)sc;
    140 
    141 	bus_space_write_2(lesc->sc_bustag, lesc->sc_reg, LEREG1_RAP, port);
    142 	bus_space_write_2(lesc->sc_bustag, lesc->sc_reg, LEREG1_RDP, val);
    143 
    144 #if defined(SUN4M)
    145 	/*
    146 	 * We need to flush the Sbus->Mbus write buffers. This can most
    147 	 * easily be accomplished by reading back the register that we
    148 	 * just wrote (thanks to Chris Torek for this solution).
    149 	 */
    150 	if (CPU_ISSUN4M) {
    151 		volatile u_int16_t discard;
    152 		discard = bus_space_read_2(lesc->sc_bustag, lesc->sc_reg,
    153 					   LEREG1_RDP);
    154 	}
    155 #endif
    156 }
    157 
    158 static u_int16_t
    159 lerdcsr(sc, port)
    160 	struct lance_softc *sc;
    161 	u_int16_t port;
    162 {
    163 	struct le_softc *lesc = (struct le_softc *)sc;
    164 
    165 	bus_space_write_2(lesc->sc_bustag, lesc->sc_reg, LEREG1_RAP, port);
    166 	return (bus_space_read_2(lesc->sc_bustag, lesc->sc_reg, LEREG1_RDP));
    167 }
    168 
    169 void
    170 lesetutp(sc)
    171 	struct lance_softc *sc;
    172 {
    173 	struct lsi64854_softc *dma = ((struct le_softc *)sc)->sc_dma;
    174 	u_int32_t csr;
    175 
    176 	csr = L64854_GCSR(dma);
    177 	csr |= E_TP_AUI;
    178 	L64854_SCSR(dma, csr);
    179 	delay(20000);	/* must not touch le for 20ms */
    180 }
    181 
    182 void
    183 lesetaui(sc)
    184 	struct lance_softc *sc;
    185 {
    186 	struct lsi64854_softc *dma = ((struct le_softc *)sc)->sc_dma;
    187 	u_int32_t csr;
    188 
    189 	csr = L64854_GCSR(dma);
    190 	csr &= ~E_TP_AUI;
    191 	L64854_SCSR(dma, csr);
    192 	delay(20000);	/* must not touch le for 20ms */
    193 }
    194 
    195 int
    196 lemediachange(sc)
    197 	struct lance_softc *sc;
    198 {
    199 	struct ifmedia *ifm = &sc->sc_media;
    200 
    201 	if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
    202 		return (EINVAL);
    203 
    204 	/*
    205 	 * Switch to the selected media.  If autoselect is
    206 	 * set, we don't really have to do anything.  We'll
    207 	 * switch to the other media when we detect loss of
    208 	 * carrier.
    209 	 */
    210 	switch (IFM_SUBTYPE(ifm->ifm_media)) {
    211 	case IFM_10_T:
    212 		lesetutp(sc);
    213 		break;
    214 
    215 	case IFM_10_5:
    216 		lesetaui(sc);
    217 		break;
    218 
    219 	case IFM_AUTO:
    220 		break;
    221 
    222 	default:
    223 		return (EINVAL);
    224 	}
    225 
    226 	return (0);
    227 }
    228 
    229 void
    230 lemediastatus(sc, ifmr)
    231 	struct lance_softc *sc;
    232 	struct ifmediareq *ifmr;
    233 {
    234 	struct lsi64854_softc *dma = ((struct le_softc *)sc)->sc_dma;
    235 
    236 	/*
    237 	 * Notify the world which media we're currently using.
    238 	 */
    239 	if (L64854_GCSR(dma) & E_TP_AUI)
    240 		ifmr->ifm_active = IFM_ETHER|IFM_10_T;
    241 	else
    242 		ifmr->ifm_active = IFM_ETHER|IFM_10_5;
    243 }
    244 
    245 hide void
    246 lehwreset(sc)
    247 	struct lance_softc *sc;
    248 {
    249 	struct le_softc *lesc = (struct le_softc *)sc;
    250 	struct lsi64854_softc *dma = lesc->sc_dma;
    251 	u_int32_t csr;
    252 	u_int aui_bit;
    253 
    254 	/*
    255 	 * Reset DMA channel.
    256 	 */
    257 	csr = L64854_GCSR(dma);
    258 	aui_bit = csr & E_TP_AUI;
    259 	DMA_RESET(dma);
    260 
    261 	/* Write bits 24-31 of Lance address */
    262 	bus_space_write_4(dma->sc_bustag, dma->sc_regs, L64854_REG_ENBAR,
    263 			  lesc->sc_laddr & 0xff000000);
    264 
    265 	DMA_ENINTR(dma);
    266 
    267 	/*
    268 	 * Disable E-cache invalidates on chip writes.
    269 	 * Retain previous cable selection bit.
    270 	 */
    271 	csr = L64854_GCSR(dma);
    272 	csr |= (E_DSBL_WR_INVAL | aui_bit);
    273 	L64854_SCSR(dma, csr);
    274 }
    275 
    276 hide void
    277 lehwinit(sc)
    278 	struct lance_softc *sc;
    279 {
    280 
    281 	/*
    282 	 * Make sure we're using the currently-enabled media type.
    283 	 * XXX Actually, this is probably unnecessary, now.
    284 	 */
    285 	switch (IFM_SUBTYPE(sc->sc_media.ifm_cur->ifm_media)) {
    286 	case IFM_10_T:
    287 		lesetutp(sc);
    288 		break;
    289 
    290 	case IFM_10_5:
    291 		lesetaui(sc);
    292 		break;
    293 	}
    294 }
    295 
    296 hide void
    297 lenocarrier(sc)
    298 	struct lance_softc *sc;
    299 {
    300 	struct le_softc *lesc = (struct le_softc *)sc;
    301 
    302 	/*
    303 	 * Check if the user has requested a certain cable type, and
    304 	 * if so, honor that request.
    305 	 */
    306 	printf("%s: lost carrier on ", sc->sc_dev.dv_xname);
    307 
    308 	if (L64854_GCSR(lesc->sc_dma) & E_TP_AUI) {
    309 		printf("UTP port");
    310 		switch (IFM_SUBTYPE(sc->sc_media.ifm_media)) {
    311 		case IFM_10_5:
    312 		case IFM_AUTO:
    313 			printf(", switching to AUI port");
    314 			lesetaui(sc);
    315 		}
    316 	} else {
    317 		printf("AUI port");
    318 		switch (IFM_SUBTYPE(sc->sc_media.ifm_media)) {
    319 		case IFM_10_T:
    320 		case IFM_AUTO:
    321 			printf(", switching to UTP port");
    322 			lesetutp(sc);
    323 		}
    324 	}
    325 	printf("\n");
    326 }
    327 
    328 int
    329 lematch_ledma(parent, cf, aux)
    330 	struct device *parent;
    331 	struct cfdata *cf;
    332 	void *aux;
    333 {
    334 	struct sbus_attach_args *sa = aux;
    335 
    336 	return (strcmp(cf->cf_driver->cd_name, sa->sa_name) == 0);
    337 }
    338 
    339 
    340 #define SAME_LANCE(bp, sa) \
    341 	(bp->val[0] == sa->sa_slot && bp->val[1] == sa->sa_offset)
    342 
    343 void
    344 leattach_ledma(parent, self, aux)
    345 	struct device *parent, *self;
    346 	void *aux;
    347 {
    348 	struct sbus_attach_args *sa = aux;
    349 	struct le_softc *lesc = (struct le_softc *)self;
    350 	struct lsi64854_softc *lsi = (struct lsi64854_softc *)parent;
    351 	struct lance_softc *sc = &lesc->sc_am7990.lsc;
    352 	bus_dma_segment_t seg;
    353 	int rseg, error;
    354 	/* XXX the following declarations should be elsewhere */
    355 	extern void myetheraddr __P((u_char *));
    356 
    357 	lesc->sc_bustag = sa->sa_bustag;
    358 
    359 	/* Establish link to `ledma' device */
    360 	lesc->sc_dma = lsi;
    361 	lesc->sc_dma->sc_client = lesc;
    362 
    363 	/* Map device registers */
    364 	if (bus_space_map2(sa->sa_bustag,
    365 			   sa->sa_slot,
    366 			   sa->sa_offset,
    367 			   sa->sa_size,
    368 			   BUS_SPACE_MAP_LINEAR,
    369 			   0, &lesc->sc_reg) != 0) {
    370 		printf("%s @ ledma: cannot map registers\n", self->dv_xname);
    371 		return;
    372 	}
    373 
    374 	/* Allocate buffer memory */
    375 	sc->sc_memsize = MEMSIZE;
    376 	error = bus_dmamem_alloc(sa->sa_dmatag, MEMSIZE, NBPG, LEDMA_BOUNDARY,
    377 				 &seg, 1, &rseg, BUS_DMA_NOWAIT);
    378 	if (error) {
    379 		printf("%s @ ledma: DMA buffer alloc error %d\n",
    380 			self->dv_xname, error);
    381 		return;
    382 	}
    383 	error = bus_dmamem_map(sa->sa_dmatag, &seg, rseg, MEMSIZE,
    384 			       (caddr_t *)&sc->sc_mem,
    385 			       BUS_DMA_NOWAIT|BUS_DMA_COHERENT);
    386 	if (error) {
    387 		printf("%s @ ledma: DMA buffer map error %d\n",
    388 			self->dv_xname, error);
    389 		bus_dmamem_free(sa->sa_dmatag, &seg, rseg);
    390 		return;
    391 	}
    392 
    393 	sc->sc_addr = seg.ds_addr & 0xffffff;
    394 	sc->sc_conf3 = LE_C3_BSWP | LE_C3_ACON | LE_C3_BCON;
    395 
    396 	lesc->sc_laddr = seg.ds_addr;
    397 
    398 	/* Assume SBus is grandparent */
    399 	lesc->sc_sd.sd_reset = (void *)lance_reset;
    400 	sbus_establish(&lesc->sc_sd, parent);
    401 
    402 	if (sa->sa_bp != NULL && strcmp(sa->sa_bp->name, le_cd.cd_name) == 0 &&
    403 	    SAME_LANCE(sa->sa_bp, sa))
    404 		sa->sa_bp->dev = &sc->sc_dev;
    405 
    406 	sc->sc_mediachange = lemediachange;
    407 	sc->sc_mediastatus = lemediastatus;
    408 	sc->sc_supmedia = lemedia;
    409 	sc->sc_nsupmedia = NLEMEDIA;
    410 	sc->sc_defaultmedia = IFM_ETHER|IFM_AUTO;
    411 
    412 	myetheraddr(sc->sc_enaddr);
    413 
    414 	sc->sc_copytodesc = lance_copytobuf_contig;
    415 	sc->sc_copyfromdesc = lance_copyfrombuf_contig;
    416 	sc->sc_copytobuf = lance_copytobuf_contig;
    417 	sc->sc_copyfrombuf = lance_copyfrombuf_contig;
    418 	sc->sc_zerobuf = lance_zerobuf_contig;
    419 
    420 	sc->sc_rdcsr = lerdcsr;
    421 	sc->sc_wrcsr = lewrcsr;
    422 	sc->sc_hwinit = lehwinit;
    423 	sc->sc_nocarrier = lenocarrier;
    424 	sc->sc_hwreset = lehwreset;
    425 
    426 	(void)bus_intr_establish(sa->sa_bustag, sa->sa_pri, 0,
    427 				 am7990_intr, sc);
    428 
    429 	am7990_config(&lesc->sc_am7990);
    430 
    431 	/* now initialize DMA */
    432 	lehwreset(sc);
    433 }
    434