Home | History | Annotate | Line # | Download | only in ic
ne2000.c revision 1.3.2.3
      1  1.3.2.3   mellon /*	$NetBSD: ne2000.c,v 1.3.2.3 1998/01/29 12:45:24 mellon Exp $	*/
      2      1.2  thorpej 
      3      1.2  thorpej /*-
      4      1.2  thorpej  * Copyright (c) 1997 The NetBSD Foundation, Inc.
      5      1.2  thorpej  * All rights reserved.
      6      1.2  thorpej  *
      7      1.2  thorpej  * This code is derived from software contributed to The NetBSD Foundation
      8      1.2  thorpej  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9      1.2  thorpej  * NASA Ames Research Center.
     10      1.2  thorpej  *
     11      1.2  thorpej  * Redistribution and use in source and binary forms, with or without
     12      1.2  thorpej  * modification, are permitted provided that the following conditions
     13      1.2  thorpej  * are met:
     14      1.2  thorpej  * 1. Redistributions of source code must retain the above copyright
     15      1.2  thorpej  *    notice, this list of conditions and the following disclaimer.
     16      1.2  thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     17      1.2  thorpej  *    notice, this list of conditions and the following disclaimer in the
     18      1.2  thorpej  *    documentation and/or other materials provided with the distribution.
     19      1.2  thorpej  * 3. All advertising materials mentioning features or use of this software
     20      1.2  thorpej  *    must display the following acknowledgement:
     21      1.2  thorpej  *	This product includes software developed by the NetBSD
     22      1.2  thorpej  *	Foundation, Inc. and its contributors.
     23      1.2  thorpej  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24      1.2  thorpej  *    contributors may be used to endorse or promote products derived
     25      1.2  thorpej  *    from this software without specific prior written permission.
     26      1.2  thorpej  *
     27      1.2  thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28      1.2  thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29      1.2  thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30      1.2  thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31      1.2  thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32      1.2  thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33      1.2  thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34      1.2  thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35      1.2  thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36      1.2  thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37      1.2  thorpej  * POSSIBILITY OF SUCH DAMAGE.
     38      1.2  thorpej  */
     39      1.2  thorpej 
     40      1.2  thorpej /*
     41      1.2  thorpej  * Device driver for National Semiconductor DS8390/WD83C690 based ethernet
     42      1.2  thorpej  * adapters.
     43      1.2  thorpej  *
     44      1.2  thorpej  * Copyright (c) 1994, 1995 Charles M. Hannum.  All rights reserved.
     45      1.2  thorpej  *
     46      1.2  thorpej  * Copyright (C) 1993, David Greenman.  This software may be used, modified,
     47      1.2  thorpej  * copied, distributed, and sold, in both source and binary form provided that
     48      1.2  thorpej  * the above copyright and these terms are retained.  Under no circumstances is
     49      1.2  thorpej  * the author responsible for the proper functioning of this software, nor does
     50      1.2  thorpej  * the author assume any responsibility for damages incurred with its use.
     51      1.2  thorpej  */
     52      1.2  thorpej 
     53      1.2  thorpej /*
     54      1.2  thorpej  * Common code shared by all NE2000-compatible Ethernet interfaces.
     55      1.2  thorpej  */
     56      1.2  thorpej 
     57      1.2  thorpej #include <sys/param.h>
     58      1.2  thorpej #include <sys/systm.h>
     59      1.2  thorpej #include <sys/device.h>
     60      1.2  thorpej #include <sys/socket.h>
     61      1.2  thorpej #include <sys/mbuf.h>
     62      1.2  thorpej #include <sys/syslog.h>
     63      1.2  thorpej 
     64      1.2  thorpej #include <net/if.h>
     65      1.2  thorpej #include <net/if_dl.h>
     66      1.2  thorpej #include <net/if_types.h>
     67  1.3.2.2  thorpej #include <net/if_media.h>
     68      1.2  thorpej 
     69      1.2  thorpej #include <net/if_ether.h>
     70      1.2  thorpej 
     71      1.2  thorpej #include <machine/bus.h>
     72      1.2  thorpej 
     73      1.2  thorpej #include <dev/ic/dp8390reg.h>
     74      1.2  thorpej #include <dev/ic/dp8390var.h>
     75      1.2  thorpej 
     76      1.2  thorpej #include <dev/ic/ne2000reg.h>
     77      1.2  thorpej #include <dev/ic/ne2000var.h>
     78      1.2  thorpej 
     79      1.2  thorpej struct cfdriver ne_cd = {
     80      1.2  thorpej 	NULL, "ne", DV_IFNET
     81      1.2  thorpej };
     82      1.2  thorpej 
     83      1.2  thorpej int	ne2000_write_mbuf __P((struct dp8390_softc *, struct mbuf *, int));
     84      1.2  thorpej int	ne2000_ring_copy __P((struct dp8390_softc *, int, caddr_t, u_short));
     85      1.2  thorpej void	ne2000_read_hdr __P((struct dp8390_softc *, int, struct dp8390_ring *));
     86      1.2  thorpej int	ne2000_test_mem __P((struct dp8390_softc *));
     87      1.2  thorpej 
     88      1.2  thorpej void	ne2000_writemem __P((bus_space_tag_t, bus_space_handle_t,
     89      1.2  thorpej 	    bus_space_tag_t, bus_space_handle_t, u_int8_t *, int, size_t, int));
     90      1.2  thorpej void	ne2000_readmem __P((bus_space_tag_t, bus_space_handle_t,
     91      1.2  thorpej 	    bus_space_tag_t, bus_space_handle_t, int, u_int8_t *, size_t, int));
     92      1.2  thorpej 
     93      1.2  thorpej void
     94      1.2  thorpej ne2000_attach(nsc, myea)
     95      1.2  thorpej 	struct ne2000_softc *nsc;
     96      1.2  thorpej 	u_int8_t *myea;
     97      1.2  thorpej {
     98      1.2  thorpej 	struct dp8390_softc *dsc = &nsc->sc_dp8390;
     99      1.2  thorpej 	bus_space_tag_t nict = dsc->sc_regt;
    100      1.2  thorpej 	bus_space_handle_t nich = dsc->sc_regh;
    101      1.2  thorpej 	bus_space_tag_t asict = nsc->sc_asict;
    102      1.2  thorpej 	bus_space_handle_t asich = nsc->sc_asich;
    103      1.2  thorpej 	u_int8_t romdata[16];
    104      1.2  thorpej 	int memsize, i, useword;
    105      1.2  thorpej 
    106      1.2  thorpej 	/*
    107      1.2  thorpej 	 * Detect it again; this gives us the memory size.
    108      1.2  thorpej 	 */
    109      1.2  thorpej 	nsc->sc_type = ne2000_detect(nict, nich, asict, asich);
    110      1.2  thorpej 	if (nsc->sc_type == 0) {
    111      1.2  thorpej 		printf("%s: where did the card go?\n", dsc->sc_dev.dv_xname);
    112      1.2  thorpej 		return;
    113      1.2  thorpej 	}
    114      1.2  thorpej 
    115      1.2  thorpej 	useword = (nsc->sc_type == NE2000_TYPE_NE2000);
    116      1.2  thorpej 
    117      1.2  thorpej 	dsc->cr_proto = ED_CR_RD2;
    118      1.2  thorpej 
    119      1.2  thorpej 	/*
    120      1.2  thorpej 	 * DCR gets:
    121      1.2  thorpej 	 *
    122      1.2  thorpej 	 *	FIFO threshold to 8, No auto-init Remote DMA,
    123      1.2  thorpej 	 *	byte order=80x86.
    124      1.2  thorpej 	 *
    125      1.2  thorpej 	 * NE1000 gets byte-wide DMA, NE2000 gets word-wide DMA.
    126      1.2  thorpej 	 */
    127      1.2  thorpej 	dsc->dcr_reg = ED_DCR_FT1 | ED_DCR_LS |
    128      1.2  thorpej 	    (nsc->sc_type == NE2000_TYPE_NE2000 ? ED_DCR_WTS : 0);
    129      1.2  thorpej 
    130      1.2  thorpej 	dsc->test_mem = ne2000_test_mem;
    131      1.2  thorpej 	dsc->ring_copy = ne2000_ring_copy;
    132      1.2  thorpej 	dsc->write_mbuf = ne2000_write_mbuf;
    133      1.2  thorpej 	dsc->read_hdr = ne2000_read_hdr;
    134      1.2  thorpej 
    135      1.2  thorpej 	/* Registers are linear. */
    136      1.2  thorpej 	for (i = 0; i < 16; i++)
    137      1.2  thorpej 		dsc->sc_reg_map[i] = i;
    138      1.2  thorpej 
    139      1.2  thorpej 	/*
    140      1.2  thorpej 	 * 8k of memory plus an additional 8k if an NE2000.
    141      1.2  thorpej 	 */
    142      1.2  thorpej 	memsize = 8192 + (nsc->sc_type == NE2000_TYPE_NE2000 ? 8192 : 0);
    143      1.2  thorpej 
    144      1.2  thorpej 	/*
    145      1.2  thorpej 	 * NIC memory doens't start at zero on an NE board.
    146      1.2  thorpej 	 * The start address is tied to the bus width.
    147      1.2  thorpej 	 * (It happens to be computed the same way as mem size.)
    148      1.2  thorpej 	 */
    149      1.2  thorpej 	dsc->mem_start = memsize;
    150      1.2  thorpej 
    151      1.2  thorpej #ifdef GWETHER
    152      1.2  thorpej 	{
    153      1.2  thorpej 		int x, mstart = 0;
    154      1.2  thorpej 		int8_t pbuf0[ED_PAGE_SIZE], pbuf[ED_PAGE_SIZE],
    155      1.2  thorpej 		    tbuf[ED_PAGE_SIZE];
    156      1.2  thorpej 
    157      1.2  thorpej 		for (i = 0; i < ED_PAGE_SIZE; i++)
    158      1.2  thorpej 			pbuf0[i] = 0;
    159      1.2  thorpej 
    160      1.2  thorpej 		/* Search for the start of RAM. */
    161      1.2  thorpej 		for (x = 1; x < 256; x++) {
    162      1.2  thorpej 			ne2000_writemem(nict, nich, asict, asich, pbuf0,
    163      1.2  thorpej 			    x << ED_PAGE_SHIFT, ED_PAGE_SIZE, useword);
    164      1.2  thorpej 			ne2000_readmem(nict, nich, asict, asich,
    165      1.2  thorpej 			    x << ED_PAGE_SHIFT, tbuf, ED_PAGE_SIZE, useword);
    166      1.2  thorpej 			if (bcmp(pbuf0, tbuf, ED_PAGE_SIZE) == 0) {
    167      1.2  thorpej 				for (i = 0; i < ED_PAGE_SIZE; i++)
    168      1.2  thorpej 					pbuf[i] = 255 - x;
    169      1.2  thorpej 				ne2000_writemem(nict, nich, asict, asich,
    170      1.2  thorpej 				    pbuf, x << ED_PAGE_SHIFT, ED_PAGE_SIZE,
    171      1.2  thorpej 				    useword);
    172      1.2  thorpej 				ne2000_readmem(nict, nich, asict, asich,
    173      1.2  thorpej 				    x << ED_PAGE_SHIFT, tbuf, ED_PAGE_SIZE,
    174      1.2  thorpej 				    useword);
    175      1.2  thorpej 				if (bcmp(pbuf, tbuf, ED_PAGE_SIZE) == 0) {
    176      1.2  thorpej 					mstart = x << ED_PAGE_SHIFT;
    177      1.2  thorpej 					memsize = ED_PAGE_SIZE;
    178      1.2  thorpej 					break;
    179      1.2  thorpej 				}
    180      1.2  thorpej 			}
    181      1.2  thorpej 		}
    182      1.2  thorpej 
    183      1.2  thorpej 		if (mstart == 0) {
    184      1.2  thorpej 			printf("%s: cannot find start of RAM\n",
    185      1.2  thorpej 			    dsc->sc_dev.dv_xname);
    186      1.2  thorpej 			return;
    187      1.2  thorpej 		}
    188      1.2  thorpej 
    189      1.2  thorpej 		/* Search for the end of RAM. */
    190      1.2  thorpej 		for (++x; x < 256; x++) {
    191      1.2  thorpej 			ne2000_writemem(nict, nich, asict, asich, pbuf0,
    192      1.2  thorpej 			    x << ED_PAGE_SHIFT, ED_PAGE_SIZE, useword);
    193      1.2  thorpej 			ne2000_readmem(nict, nich, asict, asich,
    194      1.2  thorpej 			    x << ED_PAGE_SHIFT, tbuf, ED_PAGE_SIZE, useword);
    195      1.2  thorpej 			if (bcmp(pbuf0, tbuf, ED_PAGE_SIZE) == 0) {
    196      1.2  thorpej 				for (i = 0; i < ED_PAGE_SIZE; i++)
    197      1.2  thorpej 					pbuf[i] = 255 - x;
    198      1.2  thorpej 				ne2000_writemem(nict, nich, asict, asich,
    199      1.2  thorpej 				    pbuf, x << ED_PAGE_SHIFT, ED_PAGE_SIZE,
    200      1.2  thorpej 				    useword);
    201      1.2  thorpej 				ne2000_readmem(nict, nich, asict, asich,
    202      1.2  thorpej 				    x << ED_PAGE_SHIFT, tbuf, ED_PAGE_SIZE
    203      1.2  thorpej 				    useword);
    204      1.2  thorpej 				if (bcmp(pbuf, tbuf, ED_PAGE_SIZE) == 0)
    205      1.2  thorpej 					memsize += ED_PAGE_SIZE;
    206      1.2  thorpej 				else
    207      1.2  thorpej 					break;
    208      1.2  thorpej 			} else
    209      1.2  thorpej 				break;
    210      1.2  thorpej 		}
    211      1.2  thorpej 
    212      1.2  thorpej 		printf("%s: RAM start 0x%x, size %d\n",
    213      1.2  thorpej 		    dsc->sc_dev.dv_xname, mstart, msize);
    214      1.2  thorpej 
    215      1.2  thorpej 		dsc->mem_start = mstart;
    216      1.2  thorpej 	}
    217      1.2  thorpej #endif /* GWETHER */
    218      1.2  thorpej 
    219      1.2  thorpej 	dsc->mem_size = memsize;
    220      1.2  thorpej 
    221      1.2  thorpej 	if (myea == NULL) {
    222      1.2  thorpej 		/* Read the station address. */
    223      1.2  thorpej 		ne2000_readmem(nict, nich, asict, asich, 0, romdata,
    224      1.2  thorpej 		    sizeof(romdata), useword);
    225      1.2  thorpej 		for (i = 0; i < ETHER_ADDR_LEN; i++)
    226      1.2  thorpej 			dsc->sc_enaddr[i] = romdata[i * (useword ? 2 : 1)];
    227      1.2  thorpej 	} else
    228      1.2  thorpej 		bcopy(myea, dsc->sc_enaddr, sizeof(dsc->sc_enaddr));
    229      1.2  thorpej 
    230      1.2  thorpej 	/* Clear any pending interrupts that might have occurred above. */
    231      1.2  thorpej 	bus_space_write_1(nict, nich, ED_P0_ISR, 0xff);
    232      1.2  thorpej 
    233  1.3.2.2  thorpej 	if (dp8390_config(dsc, NULL, 0, 0)) {
    234      1.2  thorpej 		printf("%s: setup failed\n", dsc->sc_dev.dv_xname);
    235      1.2  thorpej 		return;
    236      1.2  thorpej 	}
    237      1.2  thorpej 
    238      1.2  thorpej 	/*
    239      1.2  thorpej 	 * We need to compute mem_ring a bit differently; override the
    240      1.2  thorpej 	 * value set up in dp8390_config().
    241      1.2  thorpej 	 */
    242      1.2  thorpej 	dsc->mem_ring =
    243      1.2  thorpej 	    dsc->mem_start + ((dsc->txb_cnt * ED_TXBUF_SIZE) << ED_PAGE_SHIFT);
    244      1.2  thorpej }
    245      1.2  thorpej 
    246      1.2  thorpej /*
    247      1.2  thorpej  * Detect an NE-2000 or compatible.  Returns a model code.
    248      1.2  thorpej  */
    249      1.2  thorpej int
    250      1.2  thorpej ne2000_detect(nict, nich, asict, asich)
    251      1.2  thorpej 	bus_space_tag_t nict;
    252      1.2  thorpej 	bus_space_handle_t nich;
    253      1.2  thorpej 	bus_space_tag_t asict;
    254      1.2  thorpej 	bus_space_handle_t asich;
    255      1.2  thorpej {
    256      1.2  thorpej 	static u_int8_t test_pattern[32] = "THIS is A memory TEST pattern";
    257      1.2  thorpej 	u_int8_t test_buffer[32], tmp;
    258      1.2  thorpej 	int i, rv = 0;
    259      1.2  thorpej 
    260      1.2  thorpej 	/* Reset the board. */
    261      1.2  thorpej #ifdef GWETHER
    262      1.2  thorpej 	bus_space_write_1(asict, asich, NE2000_ASIC_RESET, 0);
    263      1.2  thorpej 	delay(200);
    264      1.2  thorpej #endif /* GWETHER */
    265      1.2  thorpej 	tmp = bus_space_read_1(asict, asich, NE2000_ASIC_RESET);
    266      1.2  thorpej 	delay(10000);
    267      1.2  thorpej 
    268      1.2  thorpej 	/*
    269      1.2  thorpej 	 * I don't know if this is necessary; probably cruft leftover from
    270      1.2  thorpej 	 * Clarkson packet driver code. Doesn't do a thing on the boards I've
    271      1.2  thorpej 	 * tested. -DG [note that a outb(0x84, 0) seems to work here, and is
    272      1.2  thorpej 	 * non-invasive...but some boards don't seem to reset and I don't have
    273      1.2  thorpej 	 * complete documentation on what the 'right' thing to do is...so we do
    274      1.2  thorpej 	 * the invasive thing for now.  Yuck.]
    275      1.2  thorpej 	 */
    276      1.2  thorpej 	bus_space_write_1(asict, asich, NE2000_ASIC_RESET, tmp);
    277      1.2  thorpej 	delay(5000);
    278      1.2  thorpej 
    279      1.2  thorpej 	/*
    280      1.2  thorpej 	 * This is needed because some NE clones apparently don't reset the
    281      1.2  thorpej 	 * NIC properly (or the NIC chip doesn't reset fully on power-up).
    282      1.2  thorpej 	 * XXX - this makes the probe invasive!  Done against my better
    283      1.2  thorpej 	 * judgement.  -DLG
    284      1.2  thorpej 	 */
    285      1.2  thorpej 	bus_space_write_1(nict, nich, ED_P0_CR,
    286      1.2  thorpej 	    ED_CR_RD2 | ED_CR_PAGE_0 | ED_CR_STP);
    287      1.2  thorpej 
    288      1.2  thorpej 	delay(5000);
    289      1.2  thorpej 
    290  1.3.2.1  thorpej 	/*
    291  1.3.2.1  thorpej 	 * Generic probe routine for testing for the existance of a DS8390.
    292  1.3.2.1  thorpej 	 * Must be performed  after the NIC has just been reset.  This
    293  1.3.2.1  thorpej 	 * works by looking at certain register values that are guaranteed
    294  1.3.2.1  thorpej 	 * to be initialized a certain way after power-up or reset.
    295  1.3.2.1  thorpej 	 *
    296  1.3.2.1  thorpej 	 * Specifically:
    297  1.3.2.1  thorpej 	 *
    298  1.3.2.1  thorpej 	 *	Register		reset bits	set bits
    299  1.3.2.1  thorpej 	 *	--------		----------	--------
    300  1.3.2.1  thorpej 	 *	CR			TXP, STA	RD2, STP
    301  1.3.2.1  thorpej 	 *	ISR					RST
    302  1.3.2.1  thorpej 	 *	IMR			<all>
    303  1.3.2.1  thorpej 	 *	DCR					LAS
    304  1.3.2.1  thorpej 	 *	TCR			LB1, LB0
    305  1.3.2.1  thorpej 	 *
    306  1.3.2.1  thorpej 	 * We only look at CR and ISR, however, since looking at the others
    307  1.3.2.1  thorpej 	 * would require changing register pages, which would be intrusive
    308  1.3.2.1  thorpej 	 * if this isn't an 8390.
    309  1.3.2.1  thorpej 	 */
    310  1.3.2.1  thorpej 
    311      1.2  thorpej 	tmp = bus_space_read_1(nict, nich, ED_P0_CR);
    312      1.2  thorpej 	if ((tmp & (ED_CR_RD2 | ED_CR_TXP | ED_CR_STA | ED_CR_STP)) !=
    313      1.2  thorpej 	    (ED_CR_RD2 | ED_CR_STP))
    314  1.3.2.1  thorpej 		goto out;
    315  1.3.2.1  thorpej 
    316  1.3.2.1  thorpej 	tmp = bus_space_read_1(nict, nich, ED_P0_ISR);
    317  1.3.2.1  thorpej 	if ((tmp & ED_ISR_RST) != ED_ISR_RST)
    318      1.2  thorpej 		goto out;
    319      1.2  thorpej 
    320      1.2  thorpej 	bus_space_write_1(nict, nich,
    321      1.2  thorpej 	    ED_P0_CR, ED_CR_RD2 | ED_CR_PAGE_0 | ED_CR_STA);
    322      1.2  thorpej 
    323      1.2  thorpej 	for (i = 0; i < 100; i++) {
    324      1.2  thorpej 		if ((bus_space_read_1(nict, nich, ED_P0_ISR) & ED_ISR_RST) ==
    325      1.2  thorpej 		    ED_ISR_RST) {
    326      1.2  thorpej 			/* Ack the reset bit. */
    327      1.2  thorpej 			bus_space_write_1(nict, nich, ED_P0_ISR, ED_ISR_RST);
    328      1.2  thorpej 			break;
    329      1.2  thorpej 		}
    330      1.2  thorpej 		delay(100);
    331      1.2  thorpej 	}
    332      1.2  thorpej 
    333      1.2  thorpej #if 0
    334      1.2  thorpej 	/* XXX */
    335      1.2  thorpej 	if (i == 100)
    336      1.2  thorpej 		goto out;
    337      1.2  thorpej #endif
    338      1.2  thorpej 
    339      1.2  thorpej 	/*
    340      1.2  thorpej 	 * Test the ability to read and write to the NIC memory.  This has
    341      1.2  thorpej 	 * the side effect of determining if this is an NE1000 or an NE2000.
    342      1.2  thorpej 	 */
    343      1.2  thorpej 
    344      1.2  thorpej 	/*
    345      1.2  thorpej 	 * This prevents packets from being stored in the NIC memory when
    346      1.2  thorpej 	 * the readmem routine turns on the start bit in the CR.
    347      1.2  thorpej 	 */
    348      1.2  thorpej 	bus_space_write_1(nict, nich, ED_P0_RCR, ED_RCR_MON);
    349      1.2  thorpej 
    350      1.2  thorpej 	/* Temporarily initialize DCR for byte operations. */
    351      1.2  thorpej 	bus_space_write_1(nict, nich, ED_P0_DCR, ED_DCR_FT1 | ED_DCR_LS);
    352      1.2  thorpej 
    353      1.2  thorpej 	bus_space_write_1(nict, nich, ED_P0_PSTART, 8192 >> ED_PAGE_SHIFT);
    354      1.2  thorpej 	bus_space_write_1(nict, nich, ED_P0_PSTOP, 16384 >> ED_PAGE_SHIFT);
    355      1.2  thorpej 
    356      1.2  thorpej 	/*
    357      1.2  thorpej 	 * Write a test pattern in byte mode.  If this fails, then there
    358      1.2  thorpej 	 * probably isn't any memory at 8k - which likely means that the
    359      1.2  thorpej 	 * board is an NE2000.
    360      1.2  thorpej 	 */
    361      1.2  thorpej 	ne2000_writemem(nict, nich, asict, asich, test_pattern, 8192,
    362      1.2  thorpej 	    sizeof(test_pattern), 0);
    363      1.2  thorpej 	ne2000_readmem(nict, nich, asict, asich, 8192, test_buffer,
    364      1.2  thorpej 	    sizeof(test_buffer), 0);
    365      1.2  thorpej 
    366      1.2  thorpej 	if (bcmp(test_pattern, test_buffer, sizeof(test_pattern))) {
    367      1.2  thorpej 		/* not an NE1000 - try NE2000 */
    368      1.2  thorpej 		bus_space_write_1(nict, nich, ED_P0_DCR,
    369      1.2  thorpej 		    ED_DCR_WTS | ED_DCR_FT1 | ED_DCR_LS);
    370      1.2  thorpej 		bus_space_write_1(nict, nich, ED_P0_PSTART,
    371      1.2  thorpej 		    16384 >> ED_PAGE_SHIFT);
    372      1.2  thorpej 		bus_space_write_1(nict, nich, ED_P0_PSTOP,
    373      1.2  thorpej 		    32768 >> ED_PAGE_SHIFT);
    374      1.2  thorpej 
    375      1.2  thorpej 		/*
    376      1.2  thorpej 		 * Write the test pattern in word mode.  If this also fails,
    377      1.2  thorpej 		 * then we don't know what this board is.
    378      1.2  thorpej 		 */
    379      1.2  thorpej 		ne2000_writemem(nict, nich, asict, asich, test_pattern, 16384,
    380      1.2  thorpej 		    sizeof(test_pattern), 1);
    381      1.2  thorpej 		ne2000_readmem(nict, nich, asict, asich, 16384, test_buffer,
    382      1.2  thorpej 		    sizeof(test_buffer), 1);
    383      1.2  thorpej 
    384      1.2  thorpej 		if (bcmp(test_pattern, test_buffer, sizeof(test_pattern)))
    385      1.2  thorpej 			goto out;	/* not an NE2000 either */
    386      1.2  thorpej 
    387      1.2  thorpej 		rv = NE2000_TYPE_NE2000;
    388      1.2  thorpej 	} else {
    389      1.2  thorpej 		/* We're an NE1000. */
    390      1.2  thorpej 		rv = NE2000_TYPE_NE1000;
    391      1.2  thorpej 	}
    392      1.2  thorpej 
    393      1.2  thorpej 	/* Clear any pending interrupts that might have occurred above. */
    394      1.2  thorpej 	bus_space_write_1(nict, nich, ED_P0_ISR, 0xff);
    395      1.2  thorpej 
    396      1.2  thorpej  out:
    397      1.2  thorpej 	return (rv);
    398      1.2  thorpej }
    399      1.2  thorpej 
    400      1.2  thorpej /*
    401      1.2  thorpej  * Write an mbuf chain to the destination NIC memory address using programmed
    402      1.2  thorpej  * I/O.
    403      1.2  thorpej  */
    404      1.2  thorpej int
    405      1.2  thorpej ne2000_write_mbuf(sc, m, buf)
    406      1.2  thorpej 	struct dp8390_softc *sc;
    407      1.2  thorpej 	struct mbuf *m;
    408      1.2  thorpej 	int buf;
    409      1.2  thorpej {
    410      1.2  thorpej 	struct ne2000_softc *nsc = (struct ne2000_softc *)sc;
    411      1.2  thorpej 	bus_space_tag_t nict = sc->sc_regt;
    412      1.2  thorpej 	bus_space_handle_t nich = sc->sc_regh;
    413      1.2  thorpej 	bus_space_tag_t asict = nsc->sc_asict;
    414      1.2  thorpej 	bus_space_handle_t asich = nsc->sc_asich;
    415      1.2  thorpej 	int savelen;
    416      1.2  thorpej 	int maxwait = 100;	/* about 120us */
    417      1.2  thorpej 
    418      1.2  thorpej 	savelen = m->m_pkthdr.len;
    419      1.2  thorpej 
    420      1.2  thorpej 	/* Select page 0 registers. */
    421      1.3    enami 	bus_space_write_1(nict, nich, ED_P0_CR,
    422      1.3    enami 	    ED_CR_RD2 | ED_CR_PAGE_0 | ED_CR_STA);
    423      1.2  thorpej 
    424      1.2  thorpej 	/* Reset remote DMA complete flag. */
    425      1.3    enami 	bus_space_write_1(nict, nich, ED_P0_ISR, ED_ISR_RDC);
    426      1.2  thorpej 
    427      1.2  thorpej 	/* Set up DMA byte count. */
    428      1.3    enami 	bus_space_write_1(nict, nich, ED_P0_RBCR0, savelen);
    429      1.3    enami 	bus_space_write_1(nict, nich, ED_P0_RBCR1, savelen >> 8);
    430      1.2  thorpej 
    431      1.2  thorpej 	/* Set up destination address in NIC mem. */
    432      1.3    enami 	bus_space_write_1(nict, nich, ED_P0_RSAR0, buf);
    433      1.3    enami 	bus_space_write_1(nict, nich, ED_P0_RSAR1, buf >> 8);
    434      1.2  thorpej 
    435      1.2  thorpej 	/* Set remote DMA write. */
    436      1.3    enami 	bus_space_write_1(nict, nich,
    437      1.3    enami 	    ED_P0_CR, ED_CR_RD1 | ED_CR_PAGE_0 | ED_CR_STA);
    438      1.2  thorpej 
    439      1.2  thorpej 	/*
    440      1.2  thorpej 	 * Transfer the mbuf chain to the NIC memory.  NE2000 cards
    441      1.2  thorpej 	 * require that data be transferred as words, and only words,
    442      1.2  thorpej 	 * so that case requires some extra code to patch over odd-length
    443      1.2  thorpej 	 * mbufs.
    444      1.2  thorpej 	 */
    445      1.2  thorpej 	if (nsc->sc_type == NE2000_TYPE_NE1000) {
    446      1.2  thorpej 		/* NE1000s are easy. */
    447      1.2  thorpej 		for (; m != 0; m = m->m_next) {
    448      1.2  thorpej 			if (m->m_len) {
    449      1.2  thorpej 				bus_space_write_multi_1(asict, asich,
    450      1.2  thorpej 				    NE2000_ASIC_DATA, mtod(m, u_int8_t *),
    451      1.2  thorpej 				    m->m_len);
    452      1.2  thorpej 			}
    453      1.2  thorpej 		}
    454      1.2  thorpej 	} else {
    455      1.2  thorpej 		/* NE2000s are a bit trickier. */
    456  1.3.2.3   mellon 		u_int8_t *data, *aligndata, savebyte[2];
    457      1.2  thorpej 		int l, wantbyte;
    458      1.2  thorpej 
    459      1.2  thorpej 		wantbyte = 0;
    460      1.2  thorpej 		for (; m != 0; m = m->m_next) {
    461      1.2  thorpej 			l = m->m_len;
    462      1.2  thorpej 			if (l == 0)
    463      1.2  thorpej 				continue;
    464      1.2  thorpej 			data = mtod(m, u_int8_t *);
    465  1.3.2.3   mellon 			aligndata = NULL;
    466  1.3.2.3   mellon 
    467      1.2  thorpej 			/* Finish the last word. */
    468      1.2  thorpej 			if (wantbyte) {
    469      1.2  thorpej 				savebyte[1] = *data;
    470      1.2  thorpej 				bus_space_write_2(asict, asich,
    471      1.2  thorpej 				    NE2000_ASIC_DATA, *(u_int16_t *)savebyte);
    472      1.2  thorpej 				data++;
    473      1.2  thorpej 				l--;
    474      1.2  thorpej 				wantbyte = 0;
    475      1.2  thorpej 			}
    476  1.3.2.3   mellon 
    477  1.3.2.3   mellon 			/*
    478  1.3.2.3   mellon 			 * If the previous mbuf was odd length, the
    479  1.3.2.3   mellon 			 * above check will have misaligned the rest
    480  1.3.2.3   mellon 			 * of the current mbuf.  If this is the case,
    481  1.3.2.3   mellon 			 * we need to use a temporary buffer.
    482  1.3.2.3   mellon 			 */
    483  1.3.2.3   mellon 			if (ALIGNED_POINTER(data, u_int16_t) == 0) {
    484  1.3.2.3   mellon 				aligndata = malloc(l, M_DEVBUF, M_NOWAIT);
    485  1.3.2.3   mellon 				if (aligndata == NULL)
    486  1.3.2.3   mellon 					panic("ne2000_write_mbuf: can't get "
    487  1.3.2.3   mellon 					    "temporary buffer"); /* XXX */
    488  1.3.2.3   mellon 				bcopy(data, aligndata, l);
    489  1.3.2.3   mellon 				data = aligndata;
    490  1.3.2.3   mellon 			}
    491  1.3.2.3   mellon 
    492      1.2  thorpej 			/* Output contiguous words. */
    493      1.2  thorpej 			if (l > 1) {
    494      1.2  thorpej 				bus_space_write_multi_2(asict, asich,
    495      1.2  thorpej 				    NE2000_ASIC_DATA, (u_int16_t *)data,
    496      1.2  thorpej 				    l >> 1);
    497      1.2  thorpej 			}
    498      1.2  thorpej 			/* Save last byte, if necessary. */
    499      1.2  thorpej 			if (l & 1) {
    500      1.2  thorpej 				data += l & ~1;
    501      1.2  thorpej 				savebyte[0] = *data;
    502      1.2  thorpej 				wantbyte = 1;
    503      1.2  thorpej 			}
    504  1.3.2.3   mellon 
    505  1.3.2.3   mellon 			if (aligndata != NULL)
    506  1.3.2.3   mellon 				free(aligndata, M_DEVBUF);
    507      1.2  thorpej 		}
    508      1.2  thorpej 
    509      1.2  thorpej 		if (wantbyte) {
    510      1.2  thorpej 			savebyte[1] = 0;
    511      1.2  thorpej 			bus_space_write_2(asict, asich, NE2000_ASIC_DATA,
    512      1.2  thorpej 			    *(u_int16_t *)savebyte);
    513      1.2  thorpej 		}
    514      1.2  thorpej 	}
    515      1.2  thorpej 
    516      1.2  thorpej 	/*
    517      1.2  thorpej 	 * Wait for remote DMA to complete.  This is necessary because on the
    518      1.2  thorpej 	 * transmit side, data is handled internally by the NIC in bursts, and
    519      1.2  thorpej 	 * we can't start another remote DMA until this one completes.  Not
    520      1.2  thorpej 	 * waiting causes really bad things to happen - like the NIC wedging
    521      1.2  thorpej 	 * the bus.
    522      1.2  thorpej 	 */
    523      1.2  thorpej 	while (((bus_space_read_1(nict, nich, ED_P0_ISR) & ED_ISR_RDC) !=
    524      1.2  thorpej 	    ED_ISR_RDC) && --maxwait);
    525      1.2  thorpej 
    526      1.2  thorpej 	if (maxwait == 0) {
    527      1.2  thorpej 		log(LOG_WARNING,
    528      1.2  thorpej 		    "%s: remote transmit DMA failed to complete\n",
    529      1.2  thorpej 		    sc->sc_dev.dv_xname);
    530      1.2  thorpej 		dp8390_reset(sc);
    531      1.2  thorpej 	}
    532      1.2  thorpej 
    533      1.2  thorpej 	return (savelen);
    534      1.2  thorpej }
    535      1.2  thorpej 
    536      1.2  thorpej /*
    537      1.2  thorpej  * Given a source and destination address, copy 'amout' of a packet from
    538      1.2  thorpej  * the ring buffer into a linear destination buffer.  Takes into account
    539      1.2  thorpej  * ring-wrap.
    540      1.2  thorpej  */
    541      1.2  thorpej int
    542      1.2  thorpej ne2000_ring_copy(sc, src, dst, amount)
    543      1.2  thorpej 	struct dp8390_softc *sc;
    544      1.2  thorpej 	int src;
    545      1.2  thorpej 	caddr_t dst;
    546      1.2  thorpej 	u_short amount;
    547      1.2  thorpej {
    548      1.2  thorpej 	struct ne2000_softc *nsc = (struct ne2000_softc *)sc;
    549      1.2  thorpej 	bus_space_tag_t nict = sc->sc_regt;
    550      1.2  thorpej 	bus_space_handle_t nich = sc->sc_regh;
    551      1.2  thorpej 	bus_space_tag_t asict = nsc->sc_asict;
    552      1.2  thorpej 	bus_space_handle_t asich = nsc->sc_asich;
    553      1.2  thorpej 	u_short tmp_amount;
    554      1.2  thorpej 	int useword = (nsc->sc_type == NE2000_TYPE_NE2000);
    555      1.2  thorpej 
    556      1.2  thorpej 	/* Does copy wrap to lower addr in ring buffer? */
    557      1.2  thorpej 	if (src + amount > sc->mem_end) {
    558      1.2  thorpej 		tmp_amount = sc->mem_end - src;
    559      1.2  thorpej 
    560      1.2  thorpej 		/* Copy amount up to end of NIC memory. */
    561      1.2  thorpej 		ne2000_readmem(nict, nich, asict, asich, src,
    562      1.2  thorpej 		    (u_int8_t *)dst, tmp_amount, useword);
    563      1.2  thorpej 
    564      1.2  thorpej 		amount -= tmp_amount;
    565      1.2  thorpej 		src = sc->mem_ring;
    566      1.2  thorpej 		dst += tmp_amount;
    567      1.2  thorpej 	}
    568      1.2  thorpej 
    569      1.2  thorpej 	ne2000_readmem(nict, nich, asict, asich, src, (u_int8_t *)dst,
    570      1.2  thorpej 	    amount, useword);
    571      1.2  thorpej 
    572      1.2  thorpej 	return (src + amount);
    573      1.2  thorpej }
    574      1.2  thorpej 
    575      1.2  thorpej void
    576      1.2  thorpej ne2000_read_hdr(sc, buf, hdr)
    577      1.2  thorpej 	struct dp8390_softc *sc;
    578      1.2  thorpej 	int buf;
    579      1.2  thorpej 	struct dp8390_ring *hdr;
    580      1.2  thorpej {
    581      1.2  thorpej 	struct ne2000_softc *nsc = (struct ne2000_softc *)sc;
    582      1.2  thorpej 
    583      1.2  thorpej 	ne2000_readmem(sc->sc_regt, sc->sc_regh, nsc->sc_asict, nsc->sc_asich,
    584      1.2  thorpej 	    buf, (u_int8_t *)hdr, sizeof(struct dp8390_ring),
    585      1.2  thorpej 	    (nsc->sc_type == NE2000_TYPE_NE2000));
    586  1.3.2.3   mellon #if BYTE_ORDER == BIG_ENDIAN
    587  1.3.2.3   mellon 	hdr->count = bswap16(hdr->count);
    588  1.3.2.3   mellon #endif
    589      1.2  thorpej }
    590      1.2  thorpej 
    591      1.2  thorpej int
    592      1.2  thorpej ne2000_test_mem(sc)
    593      1.2  thorpej 	struct dp8390_softc *sc;
    594      1.2  thorpej {
    595      1.2  thorpej 
    596      1.2  thorpej 	/* Noop. */
    597      1.2  thorpej 	return (0);
    598      1.2  thorpej }
    599      1.2  thorpej 
    600      1.2  thorpej /*
    601      1.2  thorpej  * Given a NIC memory source address and a host memory destination address,
    602      1.2  thorpej  * copy 'amount' from NIC to host using programmed i/o.  The 'amount' is
    603      1.2  thorpej  * rounded up to a word - ok as long as mbufs are word sized.
    604      1.2  thorpej  */
    605      1.2  thorpej void
    606      1.2  thorpej ne2000_readmem(nict, nich, asict, asich, src, dst, amount, useword)
    607      1.2  thorpej 	bus_space_tag_t nict;
    608      1.2  thorpej 	bus_space_handle_t nich;
    609      1.2  thorpej 	bus_space_tag_t asict;
    610      1.2  thorpej 	bus_space_handle_t asich;
    611      1.2  thorpej 	int src;
    612      1.2  thorpej 	u_int8_t *dst;
    613      1.2  thorpej 	size_t amount;
    614      1.2  thorpej 	int useword;
    615      1.2  thorpej {
    616      1.2  thorpej 
    617      1.2  thorpej 	/* Select page 0 registers. */
    618      1.2  thorpej 	bus_space_write_1(nict, nich, ED_P0_CR,
    619      1.2  thorpej 	    ED_CR_RD2 | ED_CR_PAGE_0 | ED_CR_STA);
    620      1.2  thorpej 
    621      1.2  thorpej 	/* Round up to a word. */
    622      1.2  thorpej 	if (amount & 1)
    623      1.2  thorpej 		++amount;
    624      1.2  thorpej 
    625      1.2  thorpej 	/* Set up DMA byte count. */
    626      1.2  thorpej 	bus_space_write_1(nict, nich, ED_P0_RBCR0, amount);
    627      1.2  thorpej 	bus_space_write_1(nict, nich, ED_P0_RBCR1, amount >> 8);
    628      1.2  thorpej 
    629      1.2  thorpej 	/* Set up source address in NIC mem. */
    630      1.2  thorpej 	bus_space_write_1(nict, nich, ED_P0_RSAR0, src);
    631      1.2  thorpej 	bus_space_write_1(nict, nich, ED_P0_RSAR1, src >> 8);
    632      1.2  thorpej 
    633      1.2  thorpej 	bus_space_write_1(nict, nich, ED_P0_CR,
    634      1.2  thorpej 	    ED_CR_RD0 | ED_CR_PAGE_0 | ED_CR_STA);
    635      1.2  thorpej 
    636      1.2  thorpej 	if (useword)
    637      1.2  thorpej 		bus_space_read_multi_2(asict, asich, NE2000_ASIC_DATA,
    638      1.2  thorpej 		    (u_int16_t *)dst, amount >> 1);
    639      1.2  thorpej 	else
    640      1.2  thorpej 		bus_space_read_multi_1(asict, asich, NE2000_ASIC_DATA,
    641      1.2  thorpej 		    dst, amount);
    642      1.2  thorpej }
    643      1.2  thorpej 
    644      1.2  thorpej /*
    645      1.2  thorpej  * Stripped down routine for writing a linear buffer to NIC memory.  Only
    646      1.2  thorpej  * used in the probe routine to test the memory.  'len' must be even.
    647      1.2  thorpej  */
    648      1.2  thorpej void
    649      1.2  thorpej ne2000_writemem(nict, nich, asict, asich, src, dst, len, useword)
    650      1.2  thorpej 	bus_space_tag_t nict;
    651      1.2  thorpej 	bus_space_handle_t nich;
    652      1.2  thorpej 	bus_space_tag_t asict;
    653      1.2  thorpej 	bus_space_handle_t asich;
    654      1.2  thorpej 	u_int8_t *src;
    655      1.2  thorpej 	int dst;
    656      1.2  thorpej 	size_t len;
    657      1.2  thorpej 	int useword;
    658      1.2  thorpej {
    659      1.2  thorpej 	int maxwait = 100;	/* about 120us */
    660      1.2  thorpej 
    661      1.2  thorpej 	/* Select page 0 registers. */
    662      1.2  thorpej 	bus_space_write_1(nict, nich, ED_P0_CR,
    663      1.2  thorpej 	    ED_CR_RD2 | ED_CR_PAGE_0 | ED_CR_STA);
    664      1.2  thorpej 
    665      1.2  thorpej 	/* Reset remote DMA complete flag. */
    666      1.2  thorpej 	bus_space_write_1(nict, nich, ED_P0_ISR, ED_ISR_RDC);
    667      1.2  thorpej 
    668      1.2  thorpej 	/* Set up DMA byte count. */
    669      1.2  thorpej 	bus_space_write_1(nict, nich, ED_P0_RBCR0, len);
    670      1.2  thorpej 	bus_space_write_1(nict, nich, ED_P0_RBCR1, len >> 8);
    671      1.2  thorpej 
    672      1.2  thorpej 	/* Set up destination address in NIC mem. */
    673      1.2  thorpej 	bus_space_write_1(nict, nich, ED_P0_RSAR0, dst);
    674      1.2  thorpej 	bus_space_write_1(nict, nich, ED_P0_RSAR1, dst >> 8);
    675      1.2  thorpej 
    676      1.2  thorpej 	/* Set remote DMA write. */
    677      1.2  thorpej 	bus_space_write_1(nict, nich, ED_P0_CR,
    678      1.2  thorpej 	    ED_CR_RD1 | ED_CR_PAGE_0 | ED_CR_STA);
    679      1.2  thorpej 
    680      1.2  thorpej 	if (useword)
    681      1.2  thorpej 		bus_space_write_multi_2(asict, asich, NE2000_ASIC_DATA,
    682      1.2  thorpej 		    (u_int16_t *)src, len >> 1);
    683      1.2  thorpej 	else
    684      1.2  thorpej 		bus_space_write_multi_1(asict, asich, NE2000_ASIC_DATA,
    685      1.2  thorpej 		    src, len);
    686      1.2  thorpej 
    687      1.2  thorpej 	/*
    688      1.2  thorpej 	 * Wait for remote DMA to complete.  This is necessary because on the
    689      1.2  thorpej 	 * transmit side, data is handled internally by the NIC in bursts, and
    690      1.2  thorpej 	 * we can't start another remote DMA until this one completes.  Not
    691      1.2  thorpej 	 * waiting causes really bad things to happen - like the NIC wedging
    692      1.2  thorpej 	 * the bus.
    693      1.2  thorpej 	 */
    694      1.2  thorpej 	while (((bus_space_read_1(nict, nich, ED_P0_ISR) & ED_ISR_RDC) !=
    695      1.2  thorpej 	    ED_ISR_RDC) && --maxwait);
    696      1.2  thorpej 
    697      1.2  thorpej 	if (maxwait == 0)
    698      1.2  thorpej 		printf("ne2000_writemem: failed to complete\n");
    699      1.2  thorpej }
    700