Home | History | Annotate | Line # | Download | only in tc
if_le_ioasic.c revision 1.12
      1 /*	$NetBSD: if_le_ioasic.c,v 1.12 1998/07/21 17:36:07 drochner Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1996 Carnegie-Mellon University.
      5  * All rights reserved.
      6  *
      7  * Author: Chris G. Demetriou
      8  *
      9  * Permission to use, copy, modify and distribute this software and
     10  * its documentation is hereby granted, provided that both the copyright
     11  * notice and this permission notice appear in all copies of the
     12  * software, derivative works or modified versions, and any portions
     13  * thereof, and that both notices appear in supporting documentation.
     14  *
     15  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
     16  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
     17  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
     18  *
     19  * Carnegie Mellon requests users of this software to return to
     20  *
     21  *  Software Distribution Coordinator  or  Software.Distribution (at) CS.CMU.EDU
     22  *  School of Computer Science
     23  *  Carnegie Mellon University
     24  *  Pittsburgh PA 15213-3890
     25  *
     26  * any improvements or extensions that they make and grant Carnegie the
     27  * rights to redistribute these changes.
     28  */
     29 
     30 /*
     31  * LANCE on DEC IOCTL ASIC.
     32  */
     33 
     34 #include <sys/cdefs.h>			/* RCS ID &  macro defns */
     35 __KERNEL_RCSID(0, "$NetBSD: if_le_ioasic.c,v 1.12 1998/07/21 17:36:07 drochner Exp $");
     36 
     37 #include "opt_inet.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 
     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 <dev/ic/lancereg.h>
     56 #include <dev/ic/lancevar.h>
     57 #include <dev/ic/am7990reg.h>
     58 #include <dev/ic/am7990var.h>
     59 
     60 #include <dev/tc/if_levar.h>
     61 #include <dev/tc/tcvar.h>
     62 #include <dev/tc/ioasicvar.h>
     63 
     64 extern caddr_t le_iomem;
     65 
     66 int	le_ioasic_match __P((struct device *, struct cfdata *, void *));
     67 void	le_ioasic_attach __P((struct device *, struct device *, void *));
     68 
     69 #if defined(_KERNEL) && !defined(_LKM)
     70 #include "opt_ddb.h"
     71 #endif
     72 
     73 #ifdef DDB
     74 #define	integrate
     75 #define hide
     76 #else
     77 #define	integrate	static __inline
     78 #define hide		static
     79 #endif
     80 
     81 hide void le_ioasic_copytobuf_gap2 __P((struct lance_softc *, void *,
     82 	    int, int));
     83 hide void le_ioasic_copyfrombuf_gap2 __P((struct lance_softc *, void *,
     84 	    int, int));
     85 
     86 hide void le_ioasic_copytobuf_gap16 __P((struct lance_softc *, void *,
     87 	    int, int));
     88 hide void le_ioasic_copyfrombuf_gap16 __P((struct lance_softc *, void *,
     89 	    int, int));
     90 hide void le_ioasic_zerobuf_gap16 __P((struct lance_softc *, int, int));
     91 
     92 struct cfattach le_ioasic_ca = {
     93 	sizeof(struct le_softc), le_ioasic_match, le_ioasic_attach
     94 };
     95 
     96 int
     97 le_ioasic_match(parent, match, aux)
     98 	struct device *parent;
     99 	struct cfdata *match;
    100 	void *aux;
    101 {
    102 	struct ioasicdev_attach_args *d = aux;
    103 
    104 	if (!ioasic_submatch(match, aux))
    105 		return (0);
    106 	if (strncmp("lance", d->iada_modname, TC_ROM_LLEN))
    107 		return (0);
    108 
    109 	return (1);
    110 }
    111 
    112 void
    113 le_ioasic_attach(parent, self, aux)
    114 	struct device *parent, *self;
    115 	void *aux;
    116 {
    117 	struct ioasicdev_attach_args *d = aux;
    118 	register struct le_softc *lesc = (void *)self;
    119 	register struct lance_softc *sc = &lesc->sc_am7990.lsc;
    120 
    121 	lesc->sc_r1 = (struct lereg1 *)
    122 		TC_DENSE_TO_SPARSE(TC_PHYS_TO_UNCACHED(d->iada_addr));
    123 	sc->sc_mem = (void *)TC_PHYS_TO_UNCACHED(le_iomem);
    124 
    125 	sc->sc_copytodesc = le_ioasic_copytobuf_gap2;
    126 	sc->sc_copyfromdesc = le_ioasic_copyfrombuf_gap2;
    127 	sc->sc_copytobuf = le_ioasic_copytobuf_gap16;
    128 	sc->sc_copyfrombuf = le_ioasic_copyfrombuf_gap16;
    129 	sc->sc_zerobuf = le_ioasic_zerobuf_gap16;
    130 
    131 	if (le_iomem == 0) {
    132 		printf("%s: DMA area not set up\n", sc->sc_dev.dv_xname);
    133 		return;
    134 	}
    135 
    136 #ifndef __alpha__
    137 	ioasic_lance_dma_setup(le_iomem);	/* XXX more thought */
    138 #endif
    139 
    140 	dec_le_common_attach(&lesc->sc_am7990, ioasic_lance_ether_address());
    141 
    142 	ioasic_intr_establish(parent, d->iada_cookie, TC_IPL_NET,
    143 	    am7990_intr, sc);
    144 }
    145 
    146 /*
    147  * Special memory access functions needed by ioasic-attached LANCE
    148  * chips.
    149  */
    150 
    151 /*
    152  * gap2: two bytes of data followed by two bytes of pad.
    153  *
    154  * Buffers must be 4-byte aligned.  The code doesn't worry about
    155  * doing an extra byte.
    156  */
    157 
    158 void
    159 le_ioasic_copytobuf_gap2(sc, fromv, boff, len)
    160 	struct lance_softc *sc;
    161 	void *fromv;
    162 	int boff;
    163 	register int len;
    164 {
    165 	volatile caddr_t buf = sc->sc_mem;
    166 	register caddr_t from = fromv;
    167 	register volatile u_int16_t *bptr;
    168 
    169 	if (boff & 0x1) {
    170 		/* handle unaligned first byte */
    171 		bptr = ((volatile u_int16_t *)buf) + (boff - 1);
    172 		*bptr = (*from++ << 8) | (*bptr & 0xff);
    173 		bptr += 2;
    174 		len--;
    175 	} else
    176 		bptr = ((volatile u_int16_t *)buf) + boff;
    177 	while (len > 1) {
    178 		*bptr = (from[1] << 8) | (from[0] & 0xff);
    179 		bptr += 2;
    180 		from += 2;
    181 		len -= 2;
    182 	}
    183 	if (len == 1)
    184 		*bptr = (u_int16_t)*from;
    185 }
    186 
    187 void
    188 le_ioasic_copyfrombuf_gap2(sc, tov, boff, len)
    189 	struct lance_softc *sc;
    190 	void *tov;
    191 	int boff, len;
    192 {
    193 	volatile caddr_t buf = sc->sc_mem;
    194 	register caddr_t to = tov;
    195 	register volatile u_int16_t *bptr;
    196 	register u_int16_t tmp;
    197 
    198 	if (boff & 0x1) {
    199 		/* handle unaligned first byte */
    200 		bptr = ((volatile u_int16_t *)buf) + (boff - 1);
    201 		*to++ = (*bptr >> 8) & 0xff;
    202 		bptr += 2;
    203 		len--;
    204 	} else
    205 		bptr = ((volatile u_int16_t *)buf) + boff;
    206 	while (len > 1) {
    207 		tmp = *bptr;
    208 		*to++ = tmp & 0xff;
    209 		*to++ = (tmp >> 8) & 0xff;
    210 		bptr += 2;
    211 		len -= 2;
    212 	}
    213 	if (len == 1)
    214 		*to = *bptr & 0xff;
    215 }
    216 
    217 /*
    218  * gap16: 16 bytes of data followed by 16 bytes of pad.
    219  *
    220  * Buffers must be 32-byte aligned.
    221  */
    222 
    223 void
    224 le_ioasic_copytobuf_gap16(sc, fromv, boff, len)
    225 	struct lance_softc *sc;
    226 	void *fromv;
    227 	int boff;
    228 	register int len;
    229 {
    230 	volatile caddr_t buf = sc->sc_mem;
    231 	register caddr_t from = fromv;
    232 	register caddr_t bptr;
    233 
    234 	bptr = buf + ((boff << 1) & ~0x1f);
    235 	boff &= 0xf;
    236 
    237 	/*
    238 	 * Dispose of boff so destination of subsequent copies is
    239 	 * 16-byte aligned.
    240 	 */
    241 	if (boff) {
    242 		register int xfer;
    243 		xfer = min(len, 16 - boff);
    244 		bcopy(from, bptr + boff, xfer);
    245 		from += xfer;
    246 		bptr += 32;
    247 		len -= xfer;
    248 	}
    249 
    250 	/* Destination of  copies is now 16-byte aligned. */
    251 	if (len >= 16)
    252 		switch ((u_long)from & (sizeof(u_int32_t) -1)) {
    253 		case 2:
    254 			/*  Ethernet headers make this the dominant case. */
    255 		do {
    256 			register u_int32_t *dst = (u_int32_t*)bptr;
    257 			register u_int16_t t0;
    258 			register u_int32_t t1,  t2, t3, t4;
    259 
    260 			/* read from odd-16-bit-aligned, cached src */
    261 			t0 = *(u_int16_t*)from;
    262 			t1 = *(u_int32_t*)(from+2);
    263 			t2 = *(u_int32_t*)(from+6);
    264 			t3 = *(u_int32_t*)(from+10);
    265 			t4 = *(u_int16_t*)(from+14);
    266 
    267 			/* DMA buffer is uncached on mips */
    268 			dst[0] =         t0 |  (t1 << 16);
    269 			dst[1] = (t1 >> 16) |  (t2 << 16);
    270 			dst[2] = (t2 >> 16) |  (t3 << 16);
    271 			dst[3] = (t3 >> 16) |  (t4 << 16);
    272 
    273 			from += 16;
    274 			bptr += 32;
    275 			len -= 16;
    276 		} while (len >= 16);
    277 		break;
    278 
    279 		case 0:
    280 		do {
    281 			register u_int32_t *src = (u_int32_t*)from;
    282 			register u_int32_t *dst = (u_int32_t*)bptr;
    283 			register u_int32_t t0, t1, t2, t3;
    284 
    285 			t0 = src[0]; t1 = src[1]; t2 = src[2]; t3 = src[3];
    286 			dst[0] = t0; dst[1] = t1; dst[2] = t2; dst[3] = t3;
    287 
    288 			from += 16;
    289 			bptr += 32;
    290 			len -= 16;
    291 		} while (len >= 16);
    292 		break;
    293 
    294 		default:
    295 		/* Does odd-aligned case ever happen? */
    296 		do {
    297 			bcopy(from, bptr, 16);
    298 			from += 16;
    299 			bptr += 32;
    300 			len -= 16;
    301 		} while (len >= 16);
    302 		break;
    303 	}
    304 	if (len)
    305 		bcopy(from, bptr, len);
    306 }
    307 
    308 void
    309 le_ioasic_copyfrombuf_gap16(sc, tov, boff, len)
    310 	struct lance_softc *sc;
    311 	void *tov;
    312 	int boff, len;
    313 {
    314 	volatile caddr_t buf = sc->sc_mem;
    315 	register caddr_t to = tov;
    316 	register caddr_t bptr;
    317 
    318 	bptr = buf + ((boff << 1) & ~0x1f);
    319 	boff &= 0xf;
    320 
    321 	/* Dispose of boff. source of copy is subsequently 16-byte aligned. */
    322 	if (boff) {
    323 		register int xfer;
    324 		xfer = min(len, 16 - boff);
    325 		bcopy(bptr+boff, to, xfer);
    326 		to += xfer;
    327 		bptr += 32;
    328 		len -= xfer;
    329 	}
    330 	if (len >= 16)
    331 	switch ((u_long)to & (sizeof(u_int32_t) -1)) {
    332 	case 2:
    333 		/*
    334 		 * to is aligned to an odd 16-bit boundary.  Ethernet headers
    335 		 * make this the dominant case (98% or more).
    336 		 */
    337 		do {
    338 			register u_int32_t *src = (u_int32_t*)bptr;
    339 			register u_int32_t t0, t1, t2, t3;
    340 
    341 			/* read from uncached aligned DMA buf */
    342 			t0 = src[0]; t1 = src[1]; t2 = src[2]; t3 = src[3];
    343 
    344 			/* write to odd-16-bit-word aligned dst */
    345 			*(u_int16_t *) (to+0)  = (u_short)  t0;
    346 			*(u_int32_t *) (to+2)  = (t0 >> 16) |  (t1 << 16);
    347 			*(u_int32_t *) (to+6)  = (t1 >> 16) |  (t2 << 16);
    348 			*(u_int32_t *) (to+10) = (t2 >> 16) |  (t3 << 16);
    349 			*(u_int16_t *) (to+14) = (t3 >> 16);
    350 			bptr += 32;
    351 			to += 16;
    352 			len -= 16;
    353 		} while (len > 16);
    354 		break;
    355 	case 0:
    356 		/* 32-bit aligned aligned copy. Rare. */
    357 		do {
    358 			register u_int32_t *src = (u_int32_t*)bptr;
    359 			register u_int32_t *dst = (u_int32_t*)to;
    360 			register u_int32_t t0, t1, t2, t3;
    361 
    362 			t0 = src[0]; t1 = src[1]; t2 = src[2]; t3 = src[3];
    363 			dst[0] = t0; dst[1] = t1; dst[2] = t2; dst[3] = t3;
    364 			to += 16;
    365 			bptr += 32;
    366 			len -= 16;
    367 		} while (len  > 16);
    368 		break;
    369 
    370 	/* XXX Does odd-byte-aligned case ever happen? */
    371 	default:
    372 		do {
    373 			bcopy(bptr, to, 16);
    374 			to += 16;
    375 			bptr += 32;
    376 			len -= 16;
    377 		} while (len  > 16);
    378 		break;
    379 	}
    380 	if (len)
    381 		bcopy(bptr, to, len);
    382 }
    383 
    384 void
    385 le_ioasic_zerobuf_gap16(sc, boff, len)
    386 	struct lance_softc *sc;
    387 	int boff, len;
    388 {
    389 	volatile caddr_t buf = sc->sc_mem;
    390 	register caddr_t bptr;
    391 	register int xfer;
    392 
    393 	bptr = buf + ((boff << 1) & ~0x1f);
    394 	boff &= 0xf;
    395 	xfer = min(len, 16 - boff);
    396 	while (len > 0) {
    397 		bzero(bptr + boff, xfer);
    398 		bptr += 32;
    399 		boff = 0;
    400 		len -= xfer;
    401 		xfer = min(len, 16);
    402 	}
    403 }
    404