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