Home | History | Annotate | Line # | Download | only in sbus
if_le_ledma.c revision 1.1
      1 /*	$NetBSD: if_le_ledma.c,v 1.1 1998/08/29 20:32:11 pk 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_long			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 le_softc *lesc = (struct le_softc *)sc;
    174 	u_int32_t csr;
    175 
    176 	csr = L64854_GCSR(lesc->sc_dma);
    177 	csr |= E_TP_AUI;
    178 	L64854_SCSR(lesc->sc_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 le_softc *lesc = (struct le_softc *)sc;
    187 	u_int32_t csr;
    188 
    189 	csr = L64854_GCSR(lesc->sc_dma);
    190 	csr &= ~E_TP_AUI;
    191 	L64854_SCSR(lesc->sc_dma, csr);
    192 
    193 	delay(20000);	/* must not touch le for 20ms */
    194 }
    195 
    196 int
    197 lemediachange(sc)
    198 	struct lance_softc *sc;
    199 {
    200 	struct ifmedia *ifm = &sc->sc_media;
    201 
    202 	if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
    203 		return (EINVAL);
    204 
    205 	/*
    206 	 * Switch to the selected media.  If autoselect is
    207 	 * set, we don't really have to do anything.  We'll
    208 	 * switch to the other media when we detect loss of
    209 	 * carrier.
    210 	 */
    211 	switch (IFM_SUBTYPE(ifm->ifm_media)) {
    212 	case IFM_10_T:
    213 		lesetutp(sc);
    214 		break;
    215 
    216 	case IFM_10_5:
    217 		lesetaui(sc);
    218 		break;
    219 
    220 	case IFM_AUTO:
    221 		break;
    222 
    223 	default:
    224 		return (EINVAL);
    225 	}
    226 
    227 	return (0);
    228 }
    229 
    230 void
    231 lemediastatus(sc, ifmr)
    232 	struct lance_softc *sc;
    233 	struct ifmediareq *ifmr;
    234 {
    235 	struct le_softc *lesc = (struct le_softc *)sc;
    236 
    237 	/*
    238 	 * Notify the world which media we're currently using.
    239 	 */
    240 	if (L64854_GCSR(lesc->sc_dma) & E_TP_AUI)
    241 		ifmr->ifm_active = IFM_ETHER|IFM_10_T;
    242 	else
    243 		ifmr->ifm_active = IFM_ETHER|IFM_10_5;
    244 }
    245 
    246 hide void
    247 lehwreset(sc)
    248 	struct lance_softc *sc;
    249 {
    250 	struct le_softc *lesc = (struct le_softc *)sc;
    251 	struct lsi64854_softc *dma = lesc->sc_dma;
    252 	u_int32_t csr;
    253 	u_long aui_bit;
    254 
    255 	/*
    256 	 * Reset DMA channel.
    257 	 */
    258 	csr = L64854_GCSR(dma);
    259 	aui_bit = csr & E_TP_AUI;
    260 	DMA_RESET(dma);
    261 
    262 	/* Write bits 24-31 of Lance address */
    263 	bus_space_write_4(dma->sc_bustag, dma->sc_regs, L64854_REG_ENBAR,
    264 			  lesc->sc_laddr & 0xff000000);
    265 
    266 	DMA_ENINTR(dma);
    267 
    268 	/*
    269 	 * Disable E-cache invalidates on chip writes.
    270 	 * Retain previous cable selection bit.
    271 	 */
    272 	csr = L64854_GCSR(dma);
    273 	csr |= (E_DSBL_WR_INVAL | aui_bit);
    274 	L64854_SCSR(dma, csr);
    275 }
    276 
    277 hide void
    278 lehwinit(sc)
    279 	struct lance_softc *sc;
    280 {
    281 
    282 	/*
    283 	 * Make sure we're using the currently-enabled media type.
    284 	 * XXX Actually, this is probably unnecessary, now.
    285 	 */
    286 	switch (IFM_SUBTYPE(sc->sc_media.ifm_cur->ifm_media)) {
    287 	case IFM_10_T:
    288 		lesetutp(sc);
    289 		break;
    290 
    291 	case IFM_10_5:
    292 		lesetaui(sc);
    293 		break;
    294 	}
    295 }
    296 
    297 hide void
    298 lenocarrier(sc)
    299 	struct lance_softc *sc;
    300 {
    301 	struct le_softc *lesc = (struct le_softc *)sc;
    302 
    303 	/*
    304 	 * Check if the user has requested a certain cable type, and
    305 	 * if so, honor that request.
    306 	 */
    307 	printf("%s: lost carrier on ", sc->sc_dev.dv_xname);
    308 
    309 	if (L64854_GCSR(lesc->sc_dma) & E_TP_AUI) {
    310 		printf("UTP port");
    311 		switch (IFM_SUBTYPE(sc->sc_media.ifm_media)) {
    312 		case IFM_10_5:
    313 		case IFM_AUTO:
    314 			printf(", switching to AUI port");
    315 			lesetaui(sc);
    316 		}
    317 	} else {
    318 		printf("AUI port");
    319 		switch (IFM_SUBTYPE(sc->sc_media.ifm_media)) {
    320 		case IFM_10_T:
    321 		case IFM_AUTO:
    322 			printf(", switching to UTP port");
    323 			lesetutp(sc);
    324 		}
    325 	}
    326 	printf("\n");
    327 }
    328 
    329 int
    330 lematch_ledma(parent, cf, aux)
    331 	struct device *parent;
    332 	struct cfdata *cf;
    333 	void *aux;
    334 {
    335 	struct sbus_attach_args *sa = aux;
    336 
    337 	return (strcmp(cf->cf_driver->cd_name, sa->sa_name) == 0);
    338 }
    339 
    340 
    341 #define SAME_LANCE(bp, sa) \
    342 	(bp->val[0] == sa->sa_slot && bp->val[1] == sa->sa_offset)
    343 
    344 void
    345 leattach_ledma(parent, self, aux)
    346 	struct device *parent, *self;
    347 	void *aux;
    348 {
    349 	struct sbus_attach_args *sa = aux;
    350 	struct le_softc *lesc = (struct le_softc *)self;
    351 	struct lsi64854_softc *lsi = (struct lsi64854_softc *)parent;
    352 	struct lance_softc *sc = &lesc->sc_am7990.lsc;
    353 	bus_dma_segment_t seg;
    354 	int rseg, error;
    355 	/* XXX the following declarations should be elsewhere */
    356 	extern void myetheraddr __P((u_char *));
    357 
    358 	lesc->sc_bustag = sa->sa_bustag;
    359 
    360 	/* Establish link to `ledma' device */
    361 	lesc->sc_dma = lsi;
    362 	lesc->sc_dma->sc_le = lesc;
    363 
    364 	/* Map device registers */
    365 	if (bus_space_map2(sa->sa_bustag,
    366 			   sa->sa_slot,
    367 			   sa->sa_offset,
    368 			   sa->sa_size,
    369 			   BUS_SPACE_MAP_LINEAR,
    370 			   0, &lesc->sc_reg) != 0) {
    371 		printf("%s @ ledma: cannot map registers\n", self->dv_xname);
    372 		return;
    373 	}
    374 
    375 	/* Allocate buffer memory */
    376 	sc->sc_memsize = MEMSIZE;
    377 	error = bus_dmamem_alloc(sa->sa_dmatag, MEMSIZE, NBPG, LEDMA_BOUNDARY,
    378 				 &seg, 1, &rseg, BUS_DMA_NOWAIT);
    379 	if (error) {
    380 		printf("%s @ ledma: DMA buffer alloc error %d\n",
    381 			self->dv_xname, error);
    382 		return;
    383 	}
    384 	error = bus_dmamem_map(sa->sa_dmatag, &seg, rseg, MEMSIZE,
    385 			       (caddr_t *)&sc->sc_mem,
    386 			       BUS_DMA_NOWAIT|BUS_DMA_COHERENT);
    387 	if (error) {
    388 		printf("%s @ ledma: DMA buffer map error %d\n",
    389 			self->dv_xname, error);
    390 		bus_dmamem_free(sa->sa_dmatag, &seg, rseg);
    391 		return;
    392 	}
    393 
    394 	sc->sc_addr = seg.ds_addr & 0xffffff;
    395 	sc->sc_conf3 = LE_C3_BSWP | LE_C3_ACON | LE_C3_BCON;
    396 
    397 	lesc->sc_laddr = seg.ds_addr;
    398 
    399 	/* Assume SBus is grandparent */
    400 	lesc->sc_sd.sd_reset = (void *)lance_reset;
    401 	sbus_establish(&lesc->sc_sd, parent);
    402 
    403 	if (sa->sa_bp != NULL && strcmp(sa->sa_bp->name, le_cd.cd_name) == 0 &&
    404 	    SAME_LANCE(sa->sa_bp, sa))
    405 		sa->sa_bp->dev = &sc->sc_dev;
    406 
    407 	sc->sc_mediachange = lemediachange;
    408 	sc->sc_mediastatus = lemediastatus;
    409 	sc->sc_supmedia = lemedia;
    410 	sc->sc_nsupmedia = NLEMEDIA;
    411 	sc->sc_defaultmedia = IFM_ETHER|IFM_AUTO;
    412 
    413 	myetheraddr(sc->sc_enaddr);
    414 
    415 	sc->sc_copytodesc = lance_copytobuf_contig;
    416 	sc->sc_copyfromdesc = lance_copyfrombuf_contig;
    417 	sc->sc_copytobuf = lance_copytobuf_contig;
    418 	sc->sc_copyfrombuf = lance_copyfrombuf_contig;
    419 	sc->sc_zerobuf = lance_zerobuf_contig;
    420 
    421 	sc->sc_rdcsr = lerdcsr;
    422 	sc->sc_wrcsr = lewrcsr;
    423 	sc->sc_hwinit = lehwinit;
    424 	sc->sc_nocarrier = lenocarrier;
    425 	sc->sc_hwreset = lehwreset;
    426 
    427 	(void)bus_intr_establish(sa->sa_bustag, sa->sa_pri, 0,
    428 				 am7990_intr, sc);
    429 
    430 	am7990_config(&lesc->sc_am7990);
    431 
    432 	/* now initialize DMA */
    433 	lehwreset(sc);
    434 }
    435