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