Home | History | Annotate | Line # | Download | only in ic
ne2000.c revision 1.35.2.1
      1 /*	$NetBSD: ne2000.c,v 1.35.2.1 2001/08/25 06:16:16 thorpej 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 Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9  * NASA Ames Research Center.
     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 /*
     41  * Device driver for National Semiconductor DS8390/WD83C690 based ethernet
     42  * adapters.
     43  *
     44  * Copyright (c) 1994, 1995 Charles M. Hannum.  All rights reserved.
     45  *
     46  * Copyright (C) 1993, David Greenman.  This software may be used, modified,
     47  * copied, distributed, and sold, in both source and binary form provided that
     48  * the above copyright and these terms are retained.  Under no circumstances is
     49  * the author responsible for the proper functioning of this software, nor does
     50  * the author assume any responsibility for damages incurred with its use.
     51  */
     52 
     53 /*
     54  * Common code shared by all NE2000-compatible Ethernet interfaces.
     55  */
     56 
     57 #include "opt_ipkdb.h"
     58 
     59 #include <sys/param.h>
     60 #include <sys/systm.h>
     61 #include <sys/device.h>
     62 #include <sys/socket.h>
     63 #include <sys/mbuf.h>
     64 #include <sys/syslog.h>
     65 
     66 #include <net/if.h>
     67 #include <net/if_dl.h>
     68 #include <net/if_types.h>
     69 #include <net/if_media.h>
     70 
     71 #include <net/if_ether.h>
     72 
     73 #include <machine/bswap.h>
     74 #include <machine/bus.h>
     75 
     76 #ifndef __BUS_SPACE_HAS_STREAM_METHODS
     77 #define	bus_space_write_stream_2	bus_space_write_2
     78 #define	bus_space_write_multi_stream_2	bus_space_write_multi_2
     79 #define	bus_space_read_multi_stream_2	bus_space_read_multi_2
     80 #endif /* __BUS_SPACE_HAS_STREAM_METHODS */
     81 
     82 #ifdef IPKDB_NE
     83 #include <ipkdb/ipkdb.h>
     84 #endif
     85 
     86 #include <dev/ic/dp8390reg.h>
     87 #include <dev/ic/dp8390var.h>
     88 
     89 #include <dev/ic/ne2000reg.h>
     90 #include <dev/ic/ne2000var.h>
     91 
     92 #include <dev/ic/ax88190reg.h>
     93 
     94 #if BYTE_ORDER == BIG_ENDIAN
     95 #include <machine/bswap.h>
     96 #endif
     97 
     98 int	ne2000_write_mbuf __P((struct dp8390_softc *, struct mbuf *, int));
     99 int	ne2000_ring_copy __P((struct dp8390_softc *, int, caddr_t, u_short));
    100 void	ne2000_read_hdr __P((struct dp8390_softc *, int, struct dp8390_ring *));
    101 int	ne2000_test_mem __P((struct dp8390_softc *));
    102 
    103 void	ne2000_writemem __P((bus_space_tag_t, bus_space_handle_t,
    104 	    bus_space_tag_t, bus_space_handle_t, u_int8_t *, int, size_t,
    105 	    int, int));
    106 void	ne2000_readmem __P((bus_space_tag_t, bus_space_handle_t,
    107 	    bus_space_tag_t, bus_space_handle_t, int, u_int8_t *, size_t, int));
    108 
    109 int
    110 ne2000_attach(nsc, myea)
    111 	struct ne2000_softc *nsc;
    112 	u_int8_t *myea;
    113 {
    114 	struct dp8390_softc *dsc = &nsc->sc_dp8390;
    115 	bus_space_tag_t nict = dsc->sc_regt;
    116 	bus_space_handle_t nich = dsc->sc_regh;
    117 	bus_space_tag_t asict = nsc->sc_asict;
    118 	bus_space_handle_t asich = nsc->sc_asich;
    119 	u_int8_t romdata[16];
    120 	int memsize, i, useword;
    121 
    122 	/*
    123 	 * Detect it again unless caller specified it; this gives us
    124 	 * the memory size.
    125 	 */
    126 	if (nsc->sc_type == 0) {
    127 		nsc->sc_type = ne2000_detect(nict, nich, asict, asich);
    128 		if (nsc->sc_type == 0) {
    129 			printf("%s: where did the card go?\n",
    130 			    dsc->sc_dev.dv_xname);
    131 			return (1);
    132 		}
    133 	}
    134 
    135 	useword = NE2000_USE_WORD(nsc);
    136 
    137 	dsc->cr_proto = ED_CR_RD2;
    138 	if (nsc->sc_type == NE2000_TYPE_AX88190) {
    139 		dsc->rcr_proto = ED_RCR_INTT;
    140 		dsc->sc_flags |= DP8390_DO_AX88190_WORKAROUND;
    141 	} else
    142 		dsc->rcr_proto = 0;
    143 
    144 	/*
    145 	 * DCR gets:
    146 	 *
    147 	 *	FIFO threshold to 8, No auto-init Remote DMA,
    148 	 *	byte order=80x86.
    149 	 *
    150 	 * NE1000 gets byte-wide DMA, NE2000 gets word-wide DMA.
    151 	 */
    152 	dsc->dcr_reg = ED_DCR_FT1 | ED_DCR_LS | (useword ? ED_DCR_WTS : 0);
    153 
    154 	dsc->test_mem = ne2000_test_mem;
    155 	dsc->ring_copy = ne2000_ring_copy;
    156 	dsc->write_mbuf = ne2000_write_mbuf;
    157 	dsc->read_hdr = ne2000_read_hdr;
    158 
    159 	/* Registers are linear. */
    160 	for (i = 0; i < 16; i++)
    161 		dsc->sc_reg_map[i] = i;
    162 
    163 	/*
    164 	 * 8k of memory for NE1000, 16k for NE2000 and 24k for the
    165 	 * card uses DL10019.
    166 	 */
    167 	switch (nsc->sc_type) {
    168 	case NE2000_TYPE_NE1000:
    169 		memsize = 8192;
    170 		break;
    171 	case NE2000_TYPE_NE2000:
    172 	case NE2000_TYPE_AX88190:		/* XXX really? */
    173 		memsize = 8192 * 2;
    174 		break;
    175 	case NE2000_TYPE_DL10019:
    176 	case NE2000_TYPE_DL10022:
    177 		memsize = 8192 * 3;
    178 		break;
    179 	}
    180 
    181 	/*
    182 	 * NIC memory doens't start at zero on an NE board.
    183 	 * The start address is tied to the bus width.
    184 	 * (It happens to be computed the same way as mem size.)
    185 	 */
    186 	dsc->mem_start = memsize;
    187 
    188 #ifdef GWETHER
    189 	{
    190 		int x, mstart = 0;
    191 		int8_t pbuf0[ED_PAGE_SIZE], pbuf[ED_PAGE_SIZE],
    192 		    tbuf[ED_PAGE_SIZE];
    193 
    194 		for (i = 0; i < ED_PAGE_SIZE; i++)
    195 			pbuf0[i] = 0;
    196 
    197 		/* Search for the start of RAM. */
    198 		for (x = 1; x < 256; x++) {
    199 			ne2000_writemem(nict, nich, asict, asich, pbuf0,
    200 			    x << ED_PAGE_SHIFT, ED_PAGE_SIZE, useword, 0);
    201 			ne2000_readmem(nict, nich, asict, asich,
    202 			    x << ED_PAGE_SHIFT, tbuf, ED_PAGE_SIZE, useword);
    203 			if (memcmp(pbuf0, tbuf, ED_PAGE_SIZE) == 0) {
    204 				for (i = 0; i < ED_PAGE_SIZE; i++)
    205 					pbuf[i] = 255 - x;
    206 				ne2000_writemem(nict, nich, asict, asich,
    207 				    pbuf, x << ED_PAGE_SHIFT, ED_PAGE_SIZE,
    208 				    useword, 0);
    209 				ne2000_readmem(nict, nich, asict, asich,
    210 				    x << ED_PAGE_SHIFT, tbuf, ED_PAGE_SIZE,
    211 				    useword);
    212 				if (memcmp(pbuf, tbuf, ED_PAGE_SIZE) == 0) {
    213 					mstart = x << ED_PAGE_SHIFT;
    214 					memsize = ED_PAGE_SIZE;
    215 					break;
    216 				}
    217 			}
    218 		}
    219 
    220 		if (mstart == 0) {
    221 			printf("%s: cannot find start of RAM\n",
    222 			    dsc->sc_dev.dv_xname);
    223 			return (1);
    224 		}
    225 
    226 		/* Search for the end of RAM. */
    227 		for (++x; x < 256; x++) {
    228 			ne2000_writemem(nict, nich, asict, asich, pbuf0,
    229 			    x << ED_PAGE_SHIFT, ED_PAGE_SIZE, useword, 0);
    230 			ne2000_readmem(nict, nich, asict, asich,
    231 			    x << ED_PAGE_SHIFT, tbuf, ED_PAGE_SIZE, useword);
    232 			if (memcmp(pbuf0, tbuf, ED_PAGE_SIZE) == 0) {
    233 				for (i = 0; i < ED_PAGE_SIZE; i++)
    234 					pbuf[i] = 255 - x;
    235 				ne2000_writemem(nict, nich, asict, asich,
    236 				    pbuf, x << ED_PAGE_SHIFT, ED_PAGE_SIZE,
    237 				    useword, 0);
    238 				ne2000_readmem(nict, nich, asict, asich,
    239 				    x << ED_PAGE_SHIFT, tbuf, ED_PAGE_SIZE,
    240 				    useword);
    241 				if (memcmp(pbuf, tbuf, ED_PAGE_SIZE) == 0)
    242 					memsize += ED_PAGE_SIZE;
    243 				else
    244 					break;
    245 			} else
    246 				break;
    247 		}
    248 
    249 		printf("%s: RAM start 0x%x, size %d\n",
    250 		    dsc->sc_dev.dv_xname, mstart, memsize);
    251 
    252 		dsc->mem_start = mstart;
    253 	}
    254 #endif /* GWETHER */
    255 
    256 	dsc->mem_size = memsize;
    257 
    258 	if (myea == NULL) {
    259 		/* Read the station address. */
    260 		if (nsc->sc_type == NE2000_TYPE_AX88190) {
    261 			/* Select page 0 registers. */
    262 			NIC_BARRIER(nict, nich);
    263 			bus_space_write_1(nict, nich, ED_P0_CR,
    264 			    ED_CR_RD2 | ED_CR_PAGE_0 | ED_CR_STA);
    265 			NIC_BARRIER(nict, nich);
    266 			/* Select word transfer. */
    267 			bus_space_write_1(nict, nich, ED_P0_DCR, ED_DCR_WTS);
    268 			NIC_BARRIER(nict, nich);
    269 			ne2000_readmem(nict, nich, asict, asich,
    270 			    AX88190_NODEID_OFFSET, dsc->sc_enaddr,
    271 			    ETHER_ADDR_LEN, useword);
    272 		} else {
    273 			ne2000_readmem(nict, nich, asict, asich, 0, romdata,
    274 			    sizeof(romdata), useword);
    275 			for (i = 0; i < ETHER_ADDR_LEN; i++)
    276 				dsc->sc_enaddr[i] =
    277 				    romdata[i * (useword ? 2 : 1)];
    278 		}
    279 	} else
    280 		memcpy(dsc->sc_enaddr, myea, sizeof(dsc->sc_enaddr));
    281 
    282 	/* Clear any pending interrupts that might have occurred above. */
    283 	NIC_BARRIER(nict, nich);
    284 	bus_space_write_1(nict, nich, ED_P0_ISR, 0xff);
    285 	NIC_BARRIER(nict, nich);
    286 
    287 	if (dsc->sc_media_init == NULL)
    288 		dsc->sc_media_init = dp8390_media_init;
    289 
    290 	if (dp8390_config(dsc)) {
    291 		printf("%s: setup failed\n", dsc->sc_dev.dv_xname);
    292 		return (1);
    293 	}
    294 
    295 	/*
    296 	 * We need to compute mem_ring a bit differently; override the
    297 	 * value set up in dp8390_config().
    298 	 */
    299 	dsc->mem_ring =
    300 	    dsc->mem_start + ((dsc->txb_cnt * ED_TXBUF_SIZE) << ED_PAGE_SHIFT);
    301 
    302 	return (0);
    303 }
    304 
    305 /*
    306  * Detect an NE-2000 or compatible.  Returns a model code.
    307  */
    308 int
    309 ne2000_detect(nict, nich, asict, asich)
    310 	bus_space_tag_t nict;
    311 	bus_space_handle_t nich;
    312 	bus_space_tag_t asict;
    313 	bus_space_handle_t asich;
    314 {
    315 	static u_int8_t test_pattern[32] = "THIS is A memory TEST pattern";
    316 	u_int8_t test_buffer[32], tmp;
    317 	int i, rv = 0;
    318 
    319 	/* Reset the board. */
    320 #ifdef GWETHER
    321 	bus_space_write_1(asict, asich, NE2000_ASIC_RESET, 0);
    322 	bus_space_barrier(nict, nich, 0, NE2000_NPORTS,
    323 			  BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
    324 	delay(200);
    325 #endif /* GWETHER */
    326 	tmp = bus_space_read_1(asict, asich, NE2000_ASIC_RESET);
    327 	bus_space_barrier(nict, nich, 0, NE2000_NPORTS,
    328 			  BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
    329 	delay(10000);
    330 
    331 	/*
    332 	 * I don't know if this is necessary; probably cruft leftover from
    333 	 * Clarkson packet driver code. Doesn't do a thing on the boards I've
    334 	 * tested. -DG [note that a outb(0x84, 0) seems to work here, and is
    335 	 * non-invasive...but some boards don't seem to reset and I don't have
    336 	 * complete documentation on what the 'right' thing to do is...so we do
    337 	 * the invasive thing for now.  Yuck.]
    338 	 */
    339 	bus_space_write_1(asict, asich, NE2000_ASIC_RESET, tmp);
    340 	bus_space_barrier(nict, nich, 0, NE2000_NPORTS,
    341 			  BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
    342 	delay(5000);
    343 
    344 	/*
    345 	 * This is needed because some NE clones apparently don't reset the
    346 	 * NIC properly (or the NIC chip doesn't reset fully on power-up).
    347 	 * XXX - this makes the probe invasive!  Done against my better
    348 	 * judgement.  -DLG
    349 	 */
    350 	bus_space_write_1(nict, nich, ED_P0_CR,
    351 	    ED_CR_RD2 | ED_CR_PAGE_0 | ED_CR_STP);
    352 	NIC_BARRIER(nict, nich);
    353 
    354 	delay(5000);
    355 
    356 	/*
    357 	 * Generic probe routine for testing for the existance of a DS8390.
    358 	 * Must be performed  after the NIC has just been reset.  This
    359 	 * works by looking at certain register values that are guaranteed
    360 	 * to be initialized a certain way after power-up or reset.
    361 	 *
    362 	 * Specifically:
    363 	 *
    364 	 *	Register		reset bits	set bits
    365 	 *	--------		----------	--------
    366 	 *	CR			TXP, STA	RD2, STP
    367 	 *	ISR					RST
    368 	 *	IMR			<all>
    369 	 *	DCR					LAS
    370 	 *	TCR			LB1, LB0
    371 	 *
    372 	 * We only look at CR and ISR, however, since looking at the others
    373 	 * would require changing register pages, which would be intrusive
    374 	 * if this isn't an 8390.
    375 	 */
    376 
    377 	tmp = bus_space_read_1(nict, nich, ED_P0_CR);
    378 	if ((tmp & (ED_CR_RD2 | ED_CR_TXP | ED_CR_STA | ED_CR_STP)) !=
    379 	    (ED_CR_RD2 | ED_CR_STP))
    380 		goto out;
    381 
    382 	tmp = bus_space_read_1(nict, nich, ED_P0_ISR);
    383 	if ((tmp & ED_ISR_RST) != ED_ISR_RST)
    384 		goto out;
    385 
    386 	bus_space_write_1(nict, nich,
    387 	    ED_P0_CR, ED_CR_RD2 | ED_CR_PAGE_0 | ED_CR_STA);
    388 	NIC_BARRIER(nict, nich);
    389 
    390 	for (i = 0; i < 100; i++) {
    391 		if ((bus_space_read_1(nict, nich, ED_P0_ISR) & ED_ISR_RST) ==
    392 		    ED_ISR_RST) {
    393 			/* Ack the reset bit. */
    394 			bus_space_write_1(nict, nich, ED_P0_ISR, ED_ISR_RST);
    395 			NIC_BARRIER(nict, nich);
    396 			break;
    397 		}
    398 		delay(100);
    399 	}
    400 
    401 #if 0
    402 	/* XXX */
    403 	if (i == 100)
    404 		goto out;
    405 #endif
    406 
    407 	/*
    408 	 * Test the ability to read and write to the NIC memory.  This has
    409 	 * the side effect of determining if this is an NE1000 or an NE2000.
    410 	 */
    411 
    412 	/*
    413 	 * This prevents packets from being stored in the NIC memory when
    414 	 * the readmem routine turns on the start bit in the CR.
    415 	 */
    416 	bus_space_write_1(nict, nich, ED_P0_RCR, ED_RCR_MON);
    417 	NIC_BARRIER(nict, nich);
    418 
    419 	/* Temporarily initialize DCR for byte operations. */
    420 	bus_space_write_1(nict, nich, ED_P0_DCR, ED_DCR_FT1 | ED_DCR_LS);
    421 
    422 	bus_space_write_1(nict, nich, ED_P0_PSTART, 8192 >> ED_PAGE_SHIFT);
    423 	bus_space_write_1(nict, nich, ED_P0_PSTOP, 16384 >> ED_PAGE_SHIFT);
    424 
    425 	/*
    426 	 * Write a test pattern in byte mode.  If this fails, then there
    427 	 * probably isn't any memory at 8k - which likely means that the
    428 	 * board is an NE2000.
    429 	 */
    430 	ne2000_writemem(nict, nich, asict, asich, test_pattern, 8192,
    431 	    sizeof(test_pattern), 0, 1);
    432 	ne2000_readmem(nict, nich, asict, asich, 8192, test_buffer,
    433 	    sizeof(test_buffer), 0);
    434 
    435 	if (memcmp(test_pattern, test_buffer, sizeof(test_pattern))) {
    436 		/* not an NE1000 - try NE2000 */
    437 		bus_space_write_1(nict, nich, ED_P0_DCR,
    438 		    ED_DCR_WTS | ED_DCR_FT1 | ED_DCR_LS);
    439 		bus_space_write_1(nict, nich, ED_P0_PSTART,
    440 		    16384 >> ED_PAGE_SHIFT);
    441 		bus_space_write_1(nict, nich, ED_P0_PSTOP,
    442 		    32768 >> ED_PAGE_SHIFT);
    443 
    444 		/*
    445 		 * Write the test pattern in word mode.  If this also fails,
    446 		 * then we don't know what this board is.
    447 		 */
    448 		ne2000_writemem(nict, nich, asict, asich, test_pattern, 16384,
    449 		    sizeof(test_pattern), 1, 0);
    450 		ne2000_readmem(nict, nich, asict, asich, 16384, test_buffer,
    451 		    sizeof(test_buffer), 1);
    452 
    453 		if (memcmp(test_pattern, test_buffer, sizeof(test_pattern)))
    454 			goto out;	/* not an NE2000 either */
    455 
    456 		rv = NE2000_TYPE_NE2000;
    457 	} else {
    458 		/* We're an NE1000. */
    459 		rv = NE2000_TYPE_NE1000;
    460 	}
    461 
    462 	/* Clear any pending interrupts that might have occurred above. */
    463 	NIC_BARRIER(nict, nich);
    464 	bus_space_write_1(nict, nich, ED_P0_ISR, 0xff);
    465 
    466  out:
    467 	return (rv);
    468 }
    469 
    470 /*
    471  * Write an mbuf chain to the destination NIC memory address using programmed
    472  * I/O.
    473  */
    474 int
    475 ne2000_write_mbuf(sc, m, buf)
    476 	struct dp8390_softc *sc;
    477 	struct mbuf *m;
    478 	int buf;
    479 {
    480 	struct ne2000_softc *nsc = (struct ne2000_softc *)sc;
    481 	bus_space_tag_t nict = sc->sc_regt;
    482 	bus_space_handle_t nich = sc->sc_regh;
    483 	bus_space_tag_t asict = nsc->sc_asict;
    484 	bus_space_handle_t asich = nsc->sc_asich;
    485 	int savelen;
    486 	int maxwait = 100;	/* about 120us */
    487 
    488 	savelen = m->m_pkthdr.len;
    489 
    490 	/* Select page 0 registers. */
    491 	NIC_BARRIER(nict, nich);
    492 	bus_space_write_1(nict, nich, ED_P0_CR,
    493 	    ED_CR_RD2 | ED_CR_PAGE_0 | ED_CR_STA);
    494 	NIC_BARRIER(nict, nich);
    495 
    496 	/* Reset remote DMA complete flag. */
    497 	bus_space_write_1(nict, nich, ED_P0_ISR, ED_ISR_RDC);
    498 	NIC_BARRIER(nict, nich);
    499 
    500 	/* Set up DMA byte count. */
    501 	bus_space_write_1(nict, nich, ED_P0_RBCR0, savelen);
    502 	bus_space_write_1(nict, nich, ED_P0_RBCR1, savelen >> 8);
    503 
    504 	/* Set up destination address in NIC mem. */
    505 	bus_space_write_1(nict, nich, ED_P0_RSAR0, buf);
    506 	bus_space_write_1(nict, nich, ED_P0_RSAR1, buf >> 8);
    507 
    508 	/* Set remote DMA write. */
    509 	NIC_BARRIER(nict, nich);
    510 	bus_space_write_1(nict, nich,
    511 	    ED_P0_CR, ED_CR_RD1 | ED_CR_PAGE_0 | ED_CR_STA);
    512 	NIC_BARRIER(nict, nich);
    513 
    514 	/*
    515 	 * Transfer the mbuf chain to the NIC memory.  NE2000 cards
    516 	 * require that data be transferred as words, and only words,
    517 	 * so that case requires some extra code to patch over odd-length
    518 	 * mbufs.
    519 	 */
    520 	if (nsc->sc_type == NE2000_TYPE_NE1000) {
    521 		/* NE1000s are easy. */
    522 		for (; m != 0; m = m->m_next) {
    523 			if (m->m_len) {
    524 				bus_space_write_multi_1(asict, asich,
    525 				    NE2000_ASIC_DATA, mtod(m, u_int8_t *),
    526 				    m->m_len);
    527 			}
    528 		}
    529 	} else {
    530 		/* NE2000s are a bit trickier. */
    531 		u_int8_t *data, savebyte[2];
    532 		int l, leftover;
    533 #ifdef DIAGNOSTIC
    534 		u_int8_t *lim;
    535 #endif
    536 		/* Start out with no leftover data. */
    537 		leftover = 0;
    538 		savebyte[0] = savebyte[1] = 0;
    539 
    540 		for (; m != 0; m = m->m_next) {
    541 			l = m->m_len;
    542 			if (l == 0)
    543 				continue;
    544 			data = mtod(m, u_int8_t *);
    545 #ifdef DIAGNOSTIC
    546 			lim = data + l;
    547 #endif
    548 			while (l > 0) {
    549 				if (leftover) {
    550 					/*
    551 					 * Data left over (from mbuf or
    552 					 * realignment).  Buffer the next
    553 					 * byte, and write it and the
    554 					 * leftover data out.
    555 					 */
    556 					savebyte[1] = *data++;
    557 					l--;
    558 					bus_space_write_stream_2(asict, asich,
    559 					    NE2000_ASIC_DATA,
    560 					    *(u_int16_t *)savebyte);
    561 					leftover = 0;
    562 				} else if (BUS_SPACE_ALIGNED_POINTER(data,
    563 					   u_int16_t) == 0) {
    564 					/*
    565 					 * Unaligned data; buffer the next
    566 					 * byte.
    567 					 */
    568 					savebyte[0] = *data++;
    569 					l--;
    570 					leftover = 1;
    571 				} else {
    572 					/*
    573 					 * Aligned data; output contiguous
    574 					 * words as much as we can, then
    575 					 * buffer the remaining byte, if any.
    576 					 */
    577 					leftover = l & 1;
    578 					l &= ~1;
    579 					bus_space_write_multi_stream_2(asict,
    580 					    asich, NE2000_ASIC_DATA,
    581 					    (u_int16_t *)data, l >> 1);
    582 					data += l;
    583 					if (leftover)
    584 						savebyte[0] = *data++;
    585 					l = 0;
    586 				}
    587 			}
    588 			if (l < 0)
    589 				panic("ne2000_write_mbuf: negative len");
    590 #ifdef DIAGNOSTIC
    591 			if (data != lim)
    592 				panic("ne2000_write_mbuf: data != lim");
    593 #endif
    594 		}
    595 		if (leftover) {
    596 			savebyte[1] = 0;
    597 			bus_space_write_stream_2(asict, asich, NE2000_ASIC_DATA,
    598 			    *(u_int16_t *)savebyte);
    599 		}
    600 	}
    601 	NIC_BARRIER(nict, nich);
    602 
    603 	/*
    604 	 * Wait for remote DMA to complete.  This is necessary because on the
    605 	 * transmit side, data is handled internally by the NIC in bursts, and
    606 	 * we can't start another remote DMA until this one completes.  Not
    607 	 * waiting causes really bad things to happen - like the NIC wedging
    608 	 * the bus.
    609 	 */
    610 	while (((bus_space_read_1(nict, nich, ED_P0_ISR) & ED_ISR_RDC) !=
    611 	    ED_ISR_RDC) && --maxwait) {
    612 		bus_space_read_1(nict, nich, ED_P0_CRDA1);
    613 		bus_space_read_1(nict, nich, ED_P0_CRDA0);
    614 		NIC_BARRIER(nict, nich);
    615 		DELAY(1);
    616 	}
    617 
    618 	if (maxwait == 0) {
    619 		log(LOG_WARNING,
    620 		    "%s: remote transmit DMA failed to complete\n",
    621 		    sc->sc_dev.dv_xname);
    622 		dp8390_reset(sc);
    623 	}
    624 
    625 	return (savelen);
    626 }
    627 
    628 /*
    629  * Given a source and destination address, copy 'amout' of a packet from
    630  * the ring buffer into a linear destination buffer.  Takes into account
    631  * ring-wrap.
    632  */
    633 int
    634 ne2000_ring_copy(sc, src, dst, amount)
    635 	struct dp8390_softc *sc;
    636 	int src;
    637 	caddr_t dst;
    638 	u_short amount;
    639 {
    640 	struct ne2000_softc *nsc = (struct ne2000_softc *)sc;
    641 	bus_space_tag_t nict = sc->sc_regt;
    642 	bus_space_handle_t nich = sc->sc_regh;
    643 	bus_space_tag_t asict = nsc->sc_asict;
    644 	bus_space_handle_t asich = nsc->sc_asich;
    645 	u_short tmp_amount;
    646 	int useword = NE2000_USE_WORD(nsc);
    647 
    648 	/* Does copy wrap to lower addr in ring buffer? */
    649 	if (src + amount > sc->mem_end) {
    650 		tmp_amount = sc->mem_end - src;
    651 
    652 		/* Copy amount up to end of NIC memory. */
    653 		ne2000_readmem(nict, nich, asict, asich, src,
    654 		    (u_int8_t *)dst, tmp_amount, useword);
    655 
    656 		amount -= tmp_amount;
    657 		src = sc->mem_ring;
    658 		dst += tmp_amount;
    659 	}
    660 
    661 	ne2000_readmem(nict, nich, asict, asich, src, (u_int8_t *)dst,
    662 	    amount, useword);
    663 
    664 	return (src + amount);
    665 }
    666 
    667 void
    668 ne2000_read_hdr(sc, buf, hdr)
    669 	struct dp8390_softc *sc;
    670 	int buf;
    671 	struct dp8390_ring *hdr;
    672 {
    673 	struct ne2000_softc *nsc = (struct ne2000_softc *)sc;
    674 
    675 	ne2000_readmem(sc->sc_regt, sc->sc_regh, nsc->sc_asict, nsc->sc_asich,
    676 	    buf, (u_int8_t *)hdr, sizeof(struct dp8390_ring),
    677 	    NE2000_USE_WORD(nsc));
    678 #if BYTE_ORDER == BIG_ENDIAN
    679 	hdr->count = bswap16(hdr->count);
    680 #endif
    681 }
    682 
    683 int
    684 ne2000_test_mem(sc)
    685 	struct dp8390_softc *sc;
    686 {
    687 
    688 	/* Noop. */
    689 	return (0);
    690 }
    691 
    692 /*
    693  * Given a NIC memory source address and a host memory destination address,
    694  * copy 'amount' from NIC to host using programmed i/o.  The 'amount' is
    695  * rounded up to a word - ok as long as mbufs are word sized.
    696  */
    697 void
    698 ne2000_readmem(nict, nich, asict, asich, src, dst, amount, useword)
    699 	bus_space_tag_t nict;
    700 	bus_space_handle_t nich;
    701 	bus_space_tag_t asict;
    702 	bus_space_handle_t asich;
    703 	int src;
    704 	u_int8_t *dst;
    705 	size_t amount;
    706 	int useword;
    707 {
    708 
    709 	/* Select page 0 registers. */
    710 	NIC_BARRIER(nict, nich);
    711 	bus_space_write_1(nict, nich, ED_P0_CR,
    712 	    ED_CR_RD2 | ED_CR_PAGE_0 | ED_CR_STA);
    713 	NIC_BARRIER(nict, nich);
    714 
    715 	/* Round up to a word. */
    716 	if (amount & 1)
    717 		++amount;
    718 
    719 	/* Set up DMA byte count. */
    720 	bus_space_write_1(nict, nich, ED_P0_RBCR0, amount);
    721 	bus_space_write_1(nict, nich, ED_P0_RBCR1, amount >> 8);
    722 
    723 	/* Set up source address in NIC mem. */
    724 	bus_space_write_1(nict, nich, ED_P0_RSAR0, src);
    725 	bus_space_write_1(nict, nich, ED_P0_RSAR1, src >> 8);
    726 
    727 	NIC_BARRIER(nict, nich);
    728 	bus_space_write_1(nict, nich, ED_P0_CR,
    729 	    ED_CR_RD0 | ED_CR_PAGE_0 | ED_CR_STA);
    730 
    731 	bus_space_barrier(nict, nich, 0, NE2000_NPORTS,
    732 			  BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
    733 	if (useword)
    734 		bus_space_read_multi_stream_2(asict, asich, NE2000_ASIC_DATA,
    735 		    (u_int16_t *)dst, amount >> 1);
    736 	else
    737 		bus_space_read_multi_1(asict, asich, NE2000_ASIC_DATA,
    738 		    dst, amount);
    739 }
    740 
    741 /*
    742  * Stripped down routine for writing a linear buffer to NIC memory.  Only
    743  * used in the probe routine to test the memory.  'len' must be even.
    744  */
    745 void
    746 ne2000_writemem(nict, nich, asict, asich, src, dst, len, useword, quiet)
    747 	bus_space_tag_t nict;
    748 	bus_space_handle_t nich;
    749 	bus_space_tag_t asict;
    750 	bus_space_handle_t asich;
    751 	u_int8_t *src;
    752 	int dst;
    753 	size_t len;
    754 	int useword;
    755 	int quiet;
    756 {
    757 	int maxwait = 100;	/* about 120us */
    758 
    759 	/* Select page 0 registers. */
    760 	NIC_BARRIER(nict, nich);
    761 	bus_space_write_1(nict, nich, ED_P0_CR,
    762 	    ED_CR_RD2 | ED_CR_PAGE_0 | ED_CR_STA);
    763 	NIC_BARRIER(nict, nich);
    764 
    765 	/* Reset remote DMA complete flag. */
    766 	bus_space_write_1(nict, nich, ED_P0_ISR, ED_ISR_RDC);
    767 	NIC_BARRIER(nict, nich);
    768 
    769 	/* Set up DMA byte count. */
    770 	bus_space_write_1(nict, nich, ED_P0_RBCR0, len);
    771 	bus_space_write_1(nict, nich, ED_P0_RBCR1, len >> 8);
    772 
    773 	/* Set up destination address in NIC mem. */
    774 	bus_space_write_1(nict, nich, ED_P0_RSAR0, dst);
    775 	bus_space_write_1(nict, nich, ED_P0_RSAR1, dst >> 8);
    776 
    777 	/* Set remote DMA write. */
    778 	NIC_BARRIER(nict, nich);
    779 	bus_space_write_1(nict, nich, ED_P0_CR,
    780 	    ED_CR_RD1 | ED_CR_PAGE_0 | ED_CR_STA);
    781 
    782 	bus_space_barrier(nict, nich, 0, NE2000_NPORTS,
    783 			  BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
    784 	if (useword)
    785 		bus_space_write_multi_stream_2(asict, asich, NE2000_ASIC_DATA,
    786 		    (u_int16_t *)src, len >> 1);
    787 	else
    788 		bus_space_write_multi_1(asict, asich, NE2000_ASIC_DATA,
    789 		    src, len);
    790 
    791 	/*
    792 	 * Wait for remote DMA to complete.  This is necessary because on the
    793 	 * transmit side, data is handled internally by the NIC in bursts, and
    794 	 * we can't start another remote DMA until this one completes.  Not
    795 	 * waiting causes really bad things to happen - like the NIC wedging
    796 	 * the bus.
    797 	 */
    798 	bus_space_barrier(nict, nich, 0, NE2000_NPORTS,
    799 			  BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
    800 	while (((bus_space_read_1(nict, nich, ED_P0_ISR) & ED_ISR_RDC) !=
    801 	    ED_ISR_RDC) && --maxwait)
    802 		DELAY(1);
    803 
    804 	if (!quiet && maxwait == 0)
    805 		printf("ne2000_writemem: failed to complete\n");
    806 }
    807 
    808 int
    809 ne2000_detach(sc, flags)
    810 	struct ne2000_softc *sc;
    811 	int flags;
    812 {
    813 
    814 	return (dp8390_detach(&sc->sc_dp8390, flags));
    815 }
    816 
    817 #ifdef IPKDB_NE
    818 /*
    819  * This code is essentially the same as ne2000_attach above.
    820  */
    821 int
    822 ne2000_ipkdb_attach(kip)
    823 	struct ipkdb_if *kip;
    824 {
    825 	struct ne2000_softc *np = kip->port;
    826 	struct dp8390_softc *dp = &np->sc_dp8390;
    827 	bus_space_tag_t nict = dp->sc_regt;
    828 	bus_space_handle_t nich = dp->sc_regh;
    829 	int i, useword;
    830 
    831 #ifdef GWETHER
    832 	/* Not supported (yet?) */
    833 	return -1;
    834 #endif
    835 
    836 	if (np->sc_type == 0)
    837 		np->sc_type = ne2000_detect(nict, nich,
    838 			np->sc_asict, np->sc_asich);
    839 	if (np->sc_type == 0)
    840 		return -1;
    841 
    842 	useword = NE2000_USE_WORD(np);
    843 
    844 	dp->cr_proto = ED_CR_RD2;
    845 	dp->dcr_reg = ED_DCR_FT1 | ED_DCR_LS | (useword ? ED_DCR_WTS : 0);
    846 	dp->rcr_proto = 0;
    847 
    848 	dp->test_mem = ne2000_test_mem;
    849 	dp->ring_copy = ne2000_ring_copy;
    850 	dp->write_mbuf = ne2000_write_mbuf;
    851 	dp->read_hdr = ne2000_read_hdr;
    852 
    853 	for (i = 0; i < 16; i++)
    854 		dp->sc_reg_map[i] = i;
    855 
    856 	switch (np->sc_type) {
    857 	case NE2000_TYPE_NE1000:
    858 		dp->mem_start = dp->mem_size = 8192;
    859 		kip->name = "ne1000";
    860 		break;
    861 	case NE2000_TYPE_NE2000:
    862 		dp->mem_start = dp->mem_size = 8192 * 2;
    863 		kip->name = "ne2000";
    864 		break;
    865 	case NE2000_TYPE_DL10019:
    866 	case NE2000_TYPE_DL10022:
    867 		dp->mem_start = dp->mem_size = 8192 * 3;
    868 		kip->name = (np->sc_type == NE2000_TYPE_DL10019) ?
    869 		    "dl10022" : "dl10019";
    870 		break;
    871 	case NE2000_TYPE_AX88190:
    872 		dp->rcr_proto = ED_RCR_INTT;
    873 		dp->sc_flags |= DP8390_DO_AX88190_WORKAROUND;
    874 		dp->mem_start = dp->mem_size = 8192 * 2;
    875 		kip->name = "ax88190";
    876 		break;
    877 	}
    878 
    879 	if (dp8390_ipkdb_attach(kip))
    880 		return -1;
    881 
    882 	dp->mem_ring = dp->mem_start
    883 		+ ((dp->txb_cnt * ED_TXBUF_SIZE) << ED_PAGE_SHIFT);
    884 
    885 	if (!(kip->flags & IPKDB_MYHW)) {
    886 		char romdata[16];
    887 
    888 		/* Read the station address. */
    889 		if (np->sc_type == NE2000_TYPE_AX88190) {
    890 			/* Select page 0 registers. */
    891 			NIC_BARRIER(nict, nich);
    892 			bus_space_write_1(nict, nich, ED_P0_CR,
    893 				ED_CR_RD2 | ED_CR_PAGE_0 | ED_CR_STA);
    894 			NIC_BARRIER(nict, nich);
    895 			/* Select word transfer */
    896 			bus_space_write_1(nict, nich, ED_P0_DCR, ED_DCR_WTS);
    897 			ne2000_readmem(nict, nich, np->sc_asict, np->sc_asich,
    898 				AX88190_NODEID_OFFSET, kip->myenetaddr,
    899 				ETHER_ADDR_LEN, useword);
    900 		} else {
    901 			ne2000_readmem(nict, nich, np->sc_asict, np->sc_asich,
    902 				0, romdata, sizeof romdata, useword);
    903 			useword = useword ? 2 : 1;
    904 			for (i = 0; i < ETHER_ADDR_LEN; i++)
    905 				kip->myenetaddr[i] = romdata[i * useword];
    906 		}
    907 		kip->flags |= IPKDB_MYHW;
    908 
    909 	}
    910 	dp8390_stop(dp);
    911 
    912 	return 0;
    913 }
    914 #endif
    915