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