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