Home | History | Annotate | Line # | Download | only in sbus
if_le_ledma.c revision 1.9
      1 /*	$NetBSD: if_le_ledma.c,v 1.9 2000/05/10 11:15:32 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_dmamap_t		sc_dmamap;
     84 	bus_space_handle_t	sc_reg;		/* LANCE registers */
     85 	struct	lsi64854_softc	*sc_dma;	/* pointer to my dma */
     86 	u_int			sc_laddr;	/* LANCE DMA address */
     87 };
     88 
     89 #define MEMSIZE		(16*1024)	/* LANCE memory size */
     90 #define LEDMA_BOUNDARY	(16*1024*1024)	/* must not cross 16MB boundary */
     91 
     92 int	lematch_ledma __P((struct device *, struct cfdata *, void *));
     93 void	leattach_ledma __P((struct device *, struct device *, void *));
     94 
     95 /*
     96  * Media types supported by the Sun4m.
     97  */
     98 static int lemedia[] = {
     99 	IFM_ETHER|IFM_10_T,
    100 	IFM_ETHER|IFM_10_5,
    101 	IFM_ETHER|IFM_AUTO,
    102 };
    103 #define NLEMEDIA	(sizeof(lemedia) / sizeof(lemedia[0]))
    104 
    105 void	lesetutp __P((struct lance_softc *));
    106 void	lesetaui __P((struct lance_softc *));
    107 
    108 int	lemediachange __P((struct lance_softc *));
    109 void	lemediastatus __P((struct lance_softc *, struct ifmediareq *));
    110 
    111 struct cfattach le_ledma_ca = {
    112 	sizeof(struct le_softc), lematch_ledma, leattach_ledma
    113 };
    114 
    115 extern struct cfdriver le_cd;
    116 
    117 #if defined(_KERNEL) && !defined(_LKM)
    118 #include "opt_ddb.h"
    119 #endif
    120 
    121 #ifdef DDB
    122 #define	integrate
    123 #define hide
    124 #else
    125 #define	integrate	static __inline
    126 #define hide		static
    127 #endif
    128 
    129 static void lewrcsr __P((struct lance_softc *, u_int16_t, u_int16_t));
    130 static u_int16_t lerdcsr __P((struct lance_softc *, u_int16_t));
    131 hide void lehwreset __P((struct lance_softc *));
    132 hide void lehwinit __P((struct lance_softc *));
    133 hide void lenocarrier __P((struct lance_softc *));
    134 
    135 static void
    136 lewrcsr(sc, port, val)
    137 	struct lance_softc *sc;
    138 	u_int16_t port, val;
    139 {
    140 	struct le_softc *lesc = (struct le_softc *)sc;
    141 
    142 	bus_space_write_2(lesc->sc_bustag, lesc->sc_reg, LEREG1_RAP, port);
    143 	bus_space_write_2(lesc->sc_bustag, lesc->sc_reg, LEREG1_RDP, val);
    144 
    145 #if defined(SUN4M)
    146 	/*
    147 	 * We need to flush the Sbus->Mbus write buffers. This can most
    148 	 * easily be accomplished by reading back the register that we
    149 	 * just wrote (thanks to Chris Torek for this solution).
    150 	 */
    151 	if (CPU_ISSUN4M) {
    152 		volatile u_int16_t discard;
    153 		discard = bus_space_read_2(lesc->sc_bustag, lesc->sc_reg,
    154 					   LEREG1_RDP);
    155 	}
    156 #endif
    157 }
    158 
    159 static u_int16_t
    160 lerdcsr(sc, port)
    161 	struct lance_softc *sc;
    162 	u_int16_t port;
    163 {
    164 	struct le_softc *lesc = (struct le_softc *)sc;
    165 
    166 	bus_space_write_2(lesc->sc_bustag, lesc->sc_reg, LEREG1_RAP, port);
    167 	return (bus_space_read_2(lesc->sc_bustag, lesc->sc_reg, LEREG1_RDP));
    168 }
    169 
    170 void
    171 lesetutp(sc)
    172 	struct lance_softc *sc;
    173 {
    174 	struct lsi64854_softc *dma = ((struct le_softc *)sc)->sc_dma;
    175 	u_int32_t csr;
    176 
    177 	csr = L64854_GCSR(dma);
    178 	csr |= E_TP_AUI;
    179 	L64854_SCSR(dma, csr);
    180 	delay(20000);	/* must not touch le for 20ms */
    181 }
    182 
    183 void
    184 lesetaui(sc)
    185 	struct lance_softc *sc;
    186 {
    187 	struct lsi64854_softc *dma = ((struct le_softc *)sc)->sc_dma;
    188 	u_int32_t csr;
    189 
    190 	csr = L64854_GCSR(dma);
    191 	csr &= ~E_TP_AUI;
    192 	L64854_SCSR(dma, csr);
    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 lsi64854_softc *dma = ((struct le_softc *)sc)->sc_dma;
    236 
    237 	/*
    238 	 * Notify the world which media we're currently using.
    239 	 */
    240 	if (L64854_GCSR(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_int 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 	delay(20000);	/* must not touch le for 20ms */
    276 }
    277 
    278 hide void
    279 lehwinit(sc)
    280 	struct lance_softc *sc;
    281 {
    282 
    283 	/*
    284 	 * Make sure we're using the currently-enabled media type.
    285 	 * XXX Actually, this is probably unnecessary, now.
    286 	 */
    287 	switch (IFM_SUBTYPE(sc->sc_media.ifm_cur->ifm_media)) {
    288 	case IFM_10_T:
    289 		lesetutp(sc);
    290 		break;
    291 
    292 	case IFM_10_5:
    293 		lesetaui(sc);
    294 		break;
    295 	}
    296 }
    297 
    298 hide void
    299 lenocarrier(sc)
    300 	struct lance_softc *sc;
    301 {
    302 	struct le_softc *lesc = (struct le_softc *)sc;
    303 
    304 	/*
    305 	 * Check if the user has requested a certain cable type, and
    306 	 * if so, honor that request.
    307 	 */
    308 	printf("%s: lost carrier on ", sc->sc_dev.dv_xname);
    309 
    310 	if (L64854_GCSR(lesc->sc_dma) & E_TP_AUI) {
    311 		printf("UTP port");
    312 		switch (IFM_SUBTYPE(sc->sc_media.ifm_media)) {
    313 		case IFM_10_5:
    314 		case IFM_AUTO:
    315 			printf(", switching to AUI port");
    316 			lesetaui(sc);
    317 		}
    318 	} else {
    319 		printf("AUI port");
    320 		switch (IFM_SUBTYPE(sc->sc_media.ifm_media)) {
    321 		case IFM_10_T:
    322 		case IFM_AUTO:
    323 			printf(", switching to UTP port");
    324 			lesetutp(sc);
    325 		}
    326 	}
    327 	printf("\n");
    328 }
    329 
    330 int
    331 lematch_ledma(parent, cf, aux)
    332 	struct device *parent;
    333 	struct cfdata *cf;
    334 	void *aux;
    335 {
    336 	struct sbus_attach_args *sa = aux;
    337 
    338 	return (strcmp(cf->cf_driver->cd_name, sa->sa_name) == 0);
    339 }
    340 
    341 
    342 void
    343 leattach_ledma(parent, self, aux)
    344 	struct device *parent, *self;
    345 	void *aux;
    346 {
    347 	struct sbus_attach_args *sa = aux;
    348 	struct le_softc *lesc = (struct le_softc *)self;
    349 	struct lsi64854_softc *lsi = (struct lsi64854_softc *)parent;
    350 	struct lance_softc *sc = &lesc->sc_am7990.lsc;
    351 	bus_dma_tag_t dmatag = sa->sa_dmatag;
    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 
    377 	/* Get a DMA handle */
    378 	if ((error = bus_dmamap_create(dmatag, MEMSIZE, 1, MEMSIZE,
    379 					LEDMA_BOUNDARY, BUS_DMA_NOWAIT,
    380 					&lesc->sc_dmamap)) != 0) {
    381 		printf("%s: DMA map create error %d\n", self->dv_xname, error);
    382 		return;
    383 	}
    384 
    385 	/* Allocate DMA buffer */
    386 	if ((error = bus_dmamem_alloc(dmatag, MEMSIZE, NBPG, LEDMA_BOUNDARY,
    387 				 &seg, 1, &rseg, BUS_DMA_NOWAIT)) != 0) {
    388 		printf("%s @ ledma: DMA buffer alloc error %d\n",
    389 			self->dv_xname, error);
    390 		return;
    391 	}
    392 
    393 	/* Load DMA buffer */
    394 	if ((error = bus_dmamap_load_raw(dmatag, lesc->sc_dmamap,
    395 				&seg, rseg, MEMSIZE, BUS_DMA_NOWAIT)) != 0) {
    396 		printf("%s: DMA buffer map load error %d\n",
    397 			self->dv_xname, error);
    398 		bus_dmamem_free(dmatag, &seg, rseg);
    399 		return;
    400 	}
    401 
    402 	/* Map DMA buffer into kernel space */
    403 	if ((error = bus_dmamem_map(dmatag, &seg, rseg, MEMSIZE,
    404 			       (caddr_t *)&sc->sc_mem,
    405 			       BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) != 0) {
    406 		printf("%s @ ledma: DMA buffer map error %d\n",
    407 			self->dv_xname, error);
    408 		bus_dmamap_unload(dmatag, lesc->sc_dmamap);
    409 		bus_dmamem_free(dmatag, &seg, rseg);
    410 		return;
    411 	}
    412 
    413 	lesc->sc_laddr = lesc->sc_dmamap->dm_segs[0].ds_addr;
    414 	sc->sc_addr = lesc->sc_laddr & 0xffffff;
    415 	sc->sc_conf3 = LE_C3_BSWP | LE_C3_ACON | LE_C3_BCON;
    416 
    417 
    418 	/* Assume SBus is grandparent */
    419 	lesc->sc_sd.sd_reset = (void *)lance_reset;
    420 	sbus_establish(&lesc->sc_sd, parent);
    421 
    422 	sc->sc_mediachange = lemediachange;
    423 	sc->sc_mediastatus = lemediastatus;
    424 	sc->sc_supmedia = lemedia;
    425 	sc->sc_nsupmedia = NLEMEDIA;
    426 	sc->sc_defaultmedia = IFM_ETHER|IFM_AUTO;
    427 
    428 	myetheraddr(sc->sc_enaddr);
    429 
    430 	sc->sc_copytodesc = lance_copytobuf_contig;
    431 	sc->sc_copyfromdesc = lance_copyfrombuf_contig;
    432 	sc->sc_copytobuf = lance_copytobuf_contig;
    433 	sc->sc_copyfrombuf = lance_copyfrombuf_contig;
    434 	sc->sc_zerobuf = lance_zerobuf_contig;
    435 
    436 	sc->sc_rdcsr = lerdcsr;
    437 	sc->sc_wrcsr = lewrcsr;
    438 	sc->sc_hwinit = lehwinit;
    439 	sc->sc_nocarrier = lenocarrier;
    440 	sc->sc_hwreset = lehwreset;
    441 
    442 	/* Establish interrupt handler */
    443 	if (sa->sa_nintr != 0)
    444 		(void)bus_intr_establish(sa->sa_bustag, sa->sa_pri, 0,
    445 					 am7990_intr, sc);
    446 
    447 	am7990_config(&lesc->sc_am7990);
    448 
    449 	/* now initialize DMA */
    450 	lehwreset(sc);
    451 }
    452