Home | History | Annotate | Line # | Download | only in ic
ne2000.c revision 1.74
      1  1.74   rkujawa /*	$NetBSD: ne2000.c,v 1.74 2013/08/11 12:34:16 rkujawa Exp $	*/
      2   1.2   thorpej 
      3   1.2   thorpej /*-
      4   1.8   thorpej  * Copyright (c) 1997, 1998 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  *
     20   1.2   thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21   1.2   thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22   1.2   thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23   1.2   thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24   1.2   thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25   1.2   thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26   1.2   thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27   1.2   thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28   1.2   thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29   1.2   thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30   1.2   thorpej  * POSSIBILITY OF SUCH DAMAGE.
     31   1.2   thorpej  */
     32   1.2   thorpej 
     33   1.2   thorpej /*
     34   1.2   thorpej  * Device driver for National Semiconductor DS8390/WD83C690 based ethernet
     35   1.2   thorpej  * adapters.
     36   1.2   thorpej  *
     37   1.2   thorpej  * Copyright (c) 1994, 1995 Charles M. Hannum.  All rights reserved.
     38   1.2   thorpej  *
     39   1.2   thorpej  * Copyright (C) 1993, David Greenman.  This software may be used, modified,
     40   1.2   thorpej  * copied, distributed, and sold, in both source and binary form provided that
     41   1.2   thorpej  * the above copyright and these terms are retained.  Under no circumstances is
     42   1.2   thorpej  * the author responsible for the proper functioning of this software, nor does
     43   1.2   thorpej  * the author assume any responsibility for damages incurred with its use.
     44   1.2   thorpej  */
     45   1.2   thorpej 
     46   1.2   thorpej /*
     47   1.2   thorpej  * Common code shared by all NE2000-compatible Ethernet interfaces.
     48   1.2   thorpej  */
     49  1.37     lukem 
     50  1.37     lukem #include <sys/cdefs.h>
     51  1.74   rkujawa __KERNEL_RCSID(0, "$NetBSD: ne2000.c,v 1.74 2013/08/11 12:34:16 rkujawa Exp $");
     52   1.2   thorpej 
     53  1.29        ws #include "opt_ipkdb.h"
     54  1.29        ws 
     55  1.68   tsutsui #include "rtl80x9.h"
     56  1.68   tsutsui 
     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.5   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.48       dsl #include <sys/bswap.h>
     72  1.55        ad #include <sys/bus.h>
     73   1.2   thorpej 
     74  1.11  sakamoto #ifndef __BUS_SPACE_HAS_STREAM_METHODS
     75  1.10  sakamoto #define	bus_space_write_stream_2	bus_space_write_2
     76  1.10  sakamoto #define	bus_space_write_multi_stream_2	bus_space_write_multi_2
     77  1.10  sakamoto #define	bus_space_read_multi_stream_2	bus_space_read_multi_2
     78  1.11  sakamoto #endif /* __BUS_SPACE_HAS_STREAM_METHODS */
     79  1.10  sakamoto 
     80  1.29        ws #ifdef IPKDB_NE
     81  1.29        ws #include <ipkdb/ipkdb.h>
     82  1.29        ws #endif
     83  1.29        ws 
     84   1.2   thorpej #include <dev/ic/dp8390reg.h>
     85   1.2   thorpej #include <dev/ic/dp8390var.h>
     86   1.2   thorpej 
     87   1.2   thorpej #include <dev/ic/ne2000reg.h>
     88   1.2   thorpej #include <dev/ic/ne2000var.h>
     89  1.17    mjacob 
     90  1.68   tsutsui #include <dev/ic/rtl80x9reg.h>
     91  1.68   tsutsui #include <dev/ic/rtl80x9var.h>
     92  1.68   tsutsui 
     93  1.36     enami #include <dev/ic/ax88190reg.h>
     94  1.36     enami 
     95  1.70   tsutsui static int	ne2000_write_mbuf(struct dp8390_softc *, struct mbuf *, int);
     96  1.70   tsutsui static int	ne2000_ring_copy(struct dp8390_softc *, int, void *, u_short);
     97  1.70   tsutsui static void	ne2000_read_hdr(struct dp8390_softc *, int,
     98  1.70   tsutsui 		    struct dp8390_ring *);
     99  1.70   tsutsui static int	ne2000_test_mem(struct dp8390_softc *);
    100  1.70   tsutsui 
    101  1.70   tsutsui static void	ne2000_writemem(bus_space_tag_t, bus_space_handle_t,
    102  1.70   tsutsui 		    bus_space_tag_t, bus_space_handle_t, const uint8_t *, int,
    103  1.70   tsutsui 		    size_t, int, int);
    104  1.70   tsutsui static void	ne2000_readmem(bus_space_tag_t, bus_space_handle_t,
    105  1.70   tsutsui 		    bus_space_tag_t, bus_space_handle_t, int, uint8_t *,
    106  1.70   tsutsui 		    size_t, int);
    107   1.2   thorpej 
    108  1.68   tsutsui #ifdef NE2000_DETECT_8BIT
    109  1.68   tsutsui static bool	ne2000_detect_8bit(bus_space_tag_t, bus_space_handle_t,
    110  1.68   tsutsui 		    bus_space_tag_t, bus_space_handle_t);
    111  1.68   tsutsui #endif
    112  1.68   tsutsui 
    113  1.44   mycroft #define	ASIC_BARRIER(asict, asich) \
    114  1.44   mycroft 	bus_space_barrier((asict), (asich), 0, 0x10, \
    115  1.44   mycroft 	    BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE)
    116  1.44   mycroft 
    117  1.26     enami int
    118  1.69   tsutsui ne2000_attach(struct ne2000_softc *nsc, uint8_t *myea)
    119   1.2   thorpej {
    120   1.2   thorpej 	struct dp8390_softc *dsc = &nsc->sc_dp8390;
    121   1.2   thorpej 	bus_space_tag_t nict = dsc->sc_regt;
    122   1.2   thorpej 	bus_space_handle_t nich = dsc->sc_regh;
    123   1.2   thorpej 	bus_space_tag_t asict = nsc->sc_asict;
    124   1.2   thorpej 	bus_space_handle_t asich = nsc->sc_asich;
    125  1.69   tsutsui 	uint8_t romdata[16];
    126  1.68   tsutsui 	int memstart, memsize, i, useword;
    127   1.2   thorpej 
    128   1.2   thorpej 	/*
    129  1.19     enami 	 * Detect it again unless caller specified it; this gives us
    130  1.19     enami 	 * the memory size.
    131   1.2   thorpej 	 */
    132  1.42   mycroft 	if (nsc->sc_type == NE2000_TYPE_UNKNOWN)
    133  1.19     enami 		nsc->sc_type = ne2000_detect(nict, nich, asict, asich);
    134  1.42   mycroft 
    135  1.42   mycroft 	/*
    136  1.42   mycroft 	 * 8k of memory for NE1000, 16k for NE2000 and 24k for the
    137  1.42   mycroft 	 * card uses DL10019.
    138  1.42   mycroft 	 */
    139  1.42   mycroft 	switch (nsc->sc_type) {
    140  1.42   mycroft 	case NE2000_TYPE_UNKNOWN:
    141  1.42   mycroft 	default:
    142  1.57      cube 		aprint_error_dev(dsc->sc_dev, "where did the card go?\n");
    143  1.69   tsutsui 		return 1;
    144  1.42   mycroft 	case NE2000_TYPE_NE1000:
    145  1.68   tsutsui 		memstart = 8192;
    146  1.42   mycroft 		memsize = 8192;
    147  1.42   mycroft 		useword = 0;
    148  1.42   mycroft 		break;
    149  1.42   mycroft 	case NE2000_TYPE_NE2000:
    150  1.42   mycroft 	case NE2000_TYPE_AX88190:		/* XXX really? */
    151  1.42   mycroft 	case NE2000_TYPE_AX88790:
    152  1.74   rkujawa 	case NE2000_TYPE_AX88796:
    153  1.68   tsutsui #if NRTL80X9 > 0
    154  1.68   tsutsui 	case NE2000_TYPE_RTL8019:
    155  1.68   tsutsui #endif
    156  1.68   tsutsui 		memstart = 16384;
    157  1.68   tsutsui 		memsize = 16384;
    158  1.42   mycroft 		useword = 1;
    159  1.74   rkujawa 
    160  1.68   tsutsui 		if (
    161  1.68   tsutsui #ifdef NE2000_DETECT_8BIT
    162  1.68   tsutsui 		    ne2000_detect_8bit(nict, nich, asict, asich) ||
    163  1.68   tsutsui #endif
    164  1.68   tsutsui 		    (nsc->sc_quirk & NE2000_QUIRK_8BIT) != 0) {
    165  1.68   tsutsui 			/* in 8 bit mode, only 8KB memory can be used */
    166  1.68   tsutsui 			memsize = 8192;
    167  1.68   tsutsui 			useword = 0;
    168  1.68   tsutsui 		}
    169  1.42   mycroft 		break;
    170  1.42   mycroft 	case NE2000_TYPE_DL10019:
    171  1.42   mycroft 	case NE2000_TYPE_DL10022:
    172  1.68   tsutsui 		memstart = 8192 * 3;
    173  1.42   mycroft 		memsize = 8192 * 3;
    174  1.42   mycroft 		useword = 1;
    175  1.42   mycroft 		break;
    176   1.2   thorpej 	}
    177   1.2   thorpej 
    178  1.42   mycroft 	nsc->sc_useword = useword;
    179  1.68   tsutsui #if NRTL80X9 > 0
    180  1.68   tsutsui 	if (nsc->sc_type == NE2000_TYPE_RTL8019) {
    181  1.68   tsutsui 		dsc->init_card = rtl80x9_init_card;
    182  1.68   tsutsui 		dsc->sc_media_init = rtl80x9_media_init;
    183  1.68   tsutsui 		dsc->sc_mediachange = rtl80x9_mediachange;
    184  1.68   tsutsui 		dsc->sc_mediastatus = rtl80x9_mediastatus;
    185  1.68   tsutsui 	}
    186  1.68   tsutsui #endif
    187   1.2   thorpej 
    188   1.2   thorpej 	dsc->cr_proto = ED_CR_RD2;
    189  1.39  christos 	if (nsc->sc_type == NE2000_TYPE_AX88190 ||
    190  1.39  christos 	    nsc->sc_type == NE2000_TYPE_AX88790) {
    191  1.27     enami 		dsc->rcr_proto = ED_RCR_INTT;
    192  1.27     enami 		dsc->sc_flags |= DP8390_DO_AX88190_WORKAROUND;
    193  1.27     enami 	} else
    194  1.27     enami 		dsc->rcr_proto = 0;
    195   1.2   thorpej 
    196   1.2   thorpej 	/*
    197   1.2   thorpej 	 * DCR gets:
    198   1.2   thorpej 	 *
    199   1.2   thorpej 	 *	FIFO threshold to 8, No auto-init Remote DMA,
    200   1.2   thorpej 	 *	byte order=80x86.
    201   1.2   thorpej 	 *
    202   1.2   thorpej 	 * NE1000 gets byte-wide DMA, NE2000 gets word-wide DMA.
    203   1.2   thorpej 	 */
    204  1.68   tsutsui 	dsc->dcr_reg = ED_DCR_FT1 | ED_DCR_LS | (useword ? ED_DCR_WTS : 0);
    205   1.2   thorpej 
    206   1.2   thorpej 	dsc->test_mem = ne2000_test_mem;
    207   1.2   thorpej 	dsc->ring_copy = ne2000_ring_copy;
    208   1.2   thorpej 	dsc->write_mbuf = ne2000_write_mbuf;
    209   1.2   thorpej 	dsc->read_hdr = ne2000_read_hdr;
    210   1.2   thorpej 
    211   1.2   thorpej 	/* Registers are linear. */
    212   1.2   thorpej 	for (i = 0; i < 16; i++)
    213   1.2   thorpej 		dsc->sc_reg_map[i] = i;
    214   1.2   thorpej 
    215   1.2   thorpej 	/*
    216   1.2   thorpej 	 * NIC memory doens't start at zero on an NE board.
    217   1.2   thorpej 	 * The start address is tied to the bus width.
    218   1.2   thorpej 	 */
    219   1.2   thorpej #ifdef GWETHER
    220   1.2   thorpej 	{
    221  1.68   tsutsui 		int x;
    222   1.2   thorpej 		int8_t pbuf0[ED_PAGE_SIZE], pbuf[ED_PAGE_SIZE],
    223   1.2   thorpej 		    tbuf[ED_PAGE_SIZE];
    224   1.2   thorpej 
    225  1.68   tsutsui 		memstart = 0;
    226   1.2   thorpej 		for (i = 0; i < ED_PAGE_SIZE; i++)
    227   1.2   thorpej 			pbuf0[i] = 0;
    228   1.2   thorpej 
    229   1.2   thorpej 		/* Search for the start of RAM. */
    230   1.2   thorpej 		for (x = 1; x < 256; x++) {
    231   1.2   thorpej 			ne2000_writemem(nict, nich, asict, asich, pbuf0,
    232  1.15   thorpej 			    x << ED_PAGE_SHIFT, ED_PAGE_SIZE, useword, 0);
    233   1.2   thorpej 			ne2000_readmem(nict, nich, asict, asich,
    234   1.2   thorpej 			    x << ED_PAGE_SHIFT, tbuf, ED_PAGE_SIZE, useword);
    235  1.34   thorpej 			if (memcmp(pbuf0, tbuf, ED_PAGE_SIZE) == 0) {
    236   1.2   thorpej 				for (i = 0; i < ED_PAGE_SIZE; i++)
    237   1.2   thorpej 					pbuf[i] = 255 - x;
    238   1.2   thorpej 				ne2000_writemem(nict, nich, asict, asich,
    239   1.2   thorpej 				    pbuf, x << ED_PAGE_SHIFT, ED_PAGE_SIZE,
    240  1.15   thorpej 				    useword, 0);
    241   1.2   thorpej 				ne2000_readmem(nict, nich, asict, asich,
    242   1.2   thorpej 				    x << ED_PAGE_SHIFT, tbuf, ED_PAGE_SIZE,
    243   1.2   thorpej 				    useword);
    244  1.34   thorpej 				if (memcmp(pbuf, tbuf, ED_PAGE_SIZE) == 0) {
    245  1.68   tsutsui 					memstart = x << ED_PAGE_SHIFT;
    246   1.2   thorpej 					memsize = ED_PAGE_SIZE;
    247   1.2   thorpej 					break;
    248   1.2   thorpej 				}
    249   1.2   thorpej 			}
    250   1.2   thorpej 		}
    251   1.2   thorpej 
    252  1.68   tsutsui 		if (memstart == 0) {
    253  1.73       chs 			aprint_error_dev(dsc->sc_dev,
    254  1.69   tsutsui 			    "cannot find start of RAM\n");
    255  1.69   tsutsui 			return 1;
    256   1.2   thorpej 		}
    257   1.2   thorpej 
    258   1.2   thorpej 		/* Search for the end of RAM. */
    259   1.2   thorpej 		for (++x; x < 256; x++) {
    260   1.2   thorpej 			ne2000_writemem(nict, nich, asict, asich, pbuf0,
    261  1.15   thorpej 			    x << ED_PAGE_SHIFT, ED_PAGE_SIZE, useword, 0);
    262   1.2   thorpej 			ne2000_readmem(nict, nich, asict, asich,
    263   1.2   thorpej 			    x << ED_PAGE_SHIFT, tbuf, ED_PAGE_SIZE, useword);
    264  1.34   thorpej 			if (memcmp(pbuf0, tbuf, ED_PAGE_SIZE) == 0) {
    265   1.2   thorpej 				for (i = 0; i < ED_PAGE_SIZE; i++)
    266   1.2   thorpej 					pbuf[i] = 255 - x;
    267   1.2   thorpej 				ne2000_writemem(nict, nich, asict, asich,
    268   1.2   thorpej 				    pbuf, x << ED_PAGE_SHIFT, ED_PAGE_SIZE,
    269  1.15   thorpej 				    useword, 0);
    270   1.2   thorpej 				ne2000_readmem(nict, nich, asict, asich,
    271  1.13   msaitoh 				    x << ED_PAGE_SHIFT, tbuf, ED_PAGE_SIZE,
    272   1.2   thorpej 				    useword);
    273  1.34   thorpej 				if (memcmp(pbuf, tbuf, ED_PAGE_SIZE) == 0)
    274   1.2   thorpej 					memsize += ED_PAGE_SIZE;
    275   1.2   thorpej 				else
    276   1.2   thorpej 					break;
    277   1.2   thorpej 			} else
    278   1.2   thorpej 				break;
    279   1.2   thorpej 		}
    280   1.2   thorpej 
    281   1.2   thorpej 		printf("%s: RAM start 0x%x, size %d\n",
    282  1.73       chs 		    device_xname(dsc->sc_dev), memstart, memsize);
    283   1.2   thorpej 	}
    284   1.2   thorpej #endif /* GWETHER */
    285  1.68   tsutsui 	dsc->mem_start = memstart;
    286   1.2   thorpej 
    287   1.2   thorpej 	dsc->mem_size = memsize;
    288   1.2   thorpej 
    289   1.2   thorpej 	if (myea == NULL) {
    290   1.2   thorpej 		/* Read the station address. */
    291  1.39  christos 		if (nsc->sc_type == NE2000_TYPE_AX88190 ||
    292  1.74   rkujawa 		    nsc->sc_type == NE2000_TYPE_AX88790 ||
    293  1.74   rkujawa 		    nsc->sc_type == NE2000_TYPE_AX88796) {
    294  1.27     enami 			/* Select page 0 registers. */
    295  1.28        ws 			NIC_BARRIER(nict, nich);
    296  1.27     enami 			bus_space_write_1(nict, nich, ED_P0_CR,
    297  1.27     enami 			    ED_CR_RD2 | ED_CR_PAGE_0 | ED_CR_STA);
    298  1.28        ws 			NIC_BARRIER(nict, nich);
    299  1.27     enami 			/* Select word transfer. */
    300  1.62    nonaka 			bus_space_write_1(nict, nich, ED_P0_DCR,
    301  1.68   tsutsui 			    useword ? ED_DCR_WTS : 0);
    302  1.28        ws 			NIC_BARRIER(nict, nich);
    303  1.27     enami 			ne2000_readmem(nict, nich, asict, asich,
    304  1.36     enami 			    AX88190_NODEID_OFFSET, dsc->sc_enaddr,
    305  1.27     enami 			    ETHER_ADDR_LEN, useword);
    306  1.27     enami 		} else {
    307  1.68   tsutsui 			bool ne1000 = (nsc->sc_type == NE2000_TYPE_NE1000);
    308  1.68   tsutsui 
    309  1.27     enami 			ne2000_readmem(nict, nich, asict, asich, 0, romdata,
    310  1.27     enami 			    sizeof(romdata), useword);
    311  1.27     enami 			for (i = 0; i < ETHER_ADDR_LEN; i++)
    312  1.27     enami 				dsc->sc_enaddr[i] =
    313  1.68   tsutsui 				    romdata[i * (ne1000 ? 1 : 2)];
    314  1.27     enami 		}
    315   1.2   thorpej 	} else
    316  1.35   thorpej 		memcpy(dsc->sc_enaddr, myea, sizeof(dsc->sc_enaddr));
    317   1.2   thorpej 
    318   1.2   thorpej 	/* Clear any pending interrupts that might have occurred above. */
    319  1.28        ws 	NIC_BARRIER(nict, nich);
    320   1.2   thorpej 	bus_space_write_1(nict, nich, ED_P0_ISR, 0xff);
    321  1.28        ws 	NIC_BARRIER(nict, nich);
    322   1.2   thorpej 
    323  1.32   thorpej 	if (dsc->sc_media_init == NULL)
    324  1.32   thorpej 		dsc->sc_media_init = dp8390_media_init;
    325  1.32   thorpej 
    326  1.32   thorpej 	if (dp8390_config(dsc)) {
    327  1.57      cube 		aprint_error_dev(dsc->sc_dev, "setup failed\n");
    328  1.69   tsutsui 		return 1;
    329   1.2   thorpej 	}
    330   1.2   thorpej 
    331  1.69   tsutsui 	return 0;
    332   1.2   thorpej }
    333   1.2   thorpej 
    334   1.2   thorpej /*
    335   1.2   thorpej  * Detect an NE-2000 or compatible.  Returns a model code.
    336   1.2   thorpej  */
    337   1.2   thorpej int
    338  1.69   tsutsui ne2000_detect(bus_space_tag_t nict, bus_space_handle_t nich,
    339  1.69   tsutsui     bus_space_tag_t asict, bus_space_handle_t asich)
    340   1.2   thorpej {
    341  1.70   tsutsui 	const uint8_t test_pattern[32] = "THIS is A memory TEST pattern";
    342  1.69   tsutsui 	uint8_t test_buffer[32], tmp;
    343  1.56  dholland 	int i, rv = NE2000_TYPE_UNKNOWN;
    344  1.68   tsutsui 	int useword;
    345   1.2   thorpej 
    346   1.2   thorpej 	/* Reset the board. */
    347   1.2   thorpej #ifdef GWETHER
    348   1.2   thorpej 	bus_space_write_1(asict, asich, NE2000_ASIC_RESET, 0);
    349  1.44   mycroft 	ASIC_BARRIER(asict, asich);
    350   1.2   thorpej 	delay(200);
    351   1.2   thorpej #endif /* GWETHER */
    352   1.2   thorpej 	tmp = bus_space_read_1(asict, asich, NE2000_ASIC_RESET);
    353  1.44   mycroft 	ASIC_BARRIER(asict, asich);
    354   1.2   thorpej 	delay(10000);
    355   1.2   thorpej 
    356   1.2   thorpej 	/*
    357   1.2   thorpej 	 * I don't know if this is necessary; probably cruft leftover from
    358   1.2   thorpej 	 * Clarkson packet driver code. Doesn't do a thing on the boards I've
    359   1.2   thorpej 	 * tested. -DG [note that a outb(0x84, 0) seems to work here, and is
    360   1.2   thorpej 	 * non-invasive...but some boards don't seem to reset and I don't have
    361   1.2   thorpej 	 * complete documentation on what the 'right' thing to do is...so we do
    362   1.2   thorpej 	 * the invasive thing for now.  Yuck.]
    363   1.2   thorpej 	 */
    364   1.2   thorpej 	bus_space_write_1(asict, asich, NE2000_ASIC_RESET, tmp);
    365  1.44   mycroft 	ASIC_BARRIER(asict, asich);
    366   1.2   thorpej 	delay(5000);
    367   1.2   thorpej 
    368   1.2   thorpej 	/*
    369   1.2   thorpej 	 * This is needed because some NE clones apparently don't reset the
    370   1.2   thorpej 	 * NIC properly (or the NIC chip doesn't reset fully on power-up).
    371   1.2   thorpej 	 * XXX - this makes the probe invasive!  Done against my better
    372   1.2   thorpej 	 * judgement.  -DLG
    373   1.2   thorpej 	 */
    374   1.2   thorpej 	bus_space_write_1(nict, nich, ED_P0_CR,
    375   1.2   thorpej 	    ED_CR_RD2 | ED_CR_PAGE_0 | ED_CR_STP);
    376  1.28        ws 	NIC_BARRIER(nict, nich);
    377   1.2   thorpej 
    378   1.2   thorpej 	delay(5000);
    379   1.2   thorpej 
    380   1.4   thorpej 	/*
    381  1.41       wiz 	 * Generic probe routine for testing for the existence of a DS8390.
    382   1.4   thorpej 	 * Must be performed  after the NIC has just been reset.  This
    383   1.4   thorpej 	 * works by looking at certain register values that are guaranteed
    384   1.4   thorpej 	 * to be initialized a certain way after power-up or reset.
    385   1.4   thorpej 	 *
    386   1.4   thorpej 	 * Specifically:
    387   1.4   thorpej 	 *
    388   1.4   thorpej 	 *	Register		reset bits	set bits
    389   1.4   thorpej 	 *	--------		----------	--------
    390   1.4   thorpej 	 *	CR			TXP, STA	RD2, STP
    391   1.4   thorpej 	 *	ISR					RST
    392   1.4   thorpej 	 *	IMR			<all>
    393   1.4   thorpej 	 *	DCR					LAS
    394   1.4   thorpej 	 *	TCR			LB1, LB0
    395   1.4   thorpej 	 *
    396   1.4   thorpej 	 * We only look at CR and ISR, however, since looking at the others
    397   1.4   thorpej 	 * would require changing register pages, which would be intrusive
    398   1.4   thorpej 	 * if this isn't an 8390.
    399   1.4   thorpej 	 */
    400   1.4   thorpej 
    401   1.2   thorpej 	tmp = bus_space_read_1(nict, nich, ED_P0_CR);
    402   1.2   thorpej 	if ((tmp & (ED_CR_RD2 | ED_CR_TXP | ED_CR_STA | ED_CR_STP)) !=
    403   1.2   thorpej 	    (ED_CR_RD2 | ED_CR_STP))
    404   1.4   thorpej 		goto out;
    405   1.4   thorpej 
    406   1.4   thorpej 	tmp = bus_space_read_1(nict, nich, ED_P0_ISR);
    407   1.4   thorpej 	if ((tmp & ED_ISR_RST) != ED_ISR_RST)
    408   1.2   thorpej 		goto out;
    409   1.2   thorpej 
    410   1.2   thorpej 	bus_space_write_1(nict, nich,
    411   1.2   thorpej 	    ED_P0_CR, ED_CR_RD2 | ED_CR_PAGE_0 | ED_CR_STA);
    412  1.28        ws 	NIC_BARRIER(nict, nich);
    413   1.2   thorpej 
    414   1.2   thorpej 	for (i = 0; i < 100; i++) {
    415   1.2   thorpej 		if ((bus_space_read_1(nict, nich, ED_P0_ISR) & ED_ISR_RST) ==
    416   1.2   thorpej 		    ED_ISR_RST) {
    417   1.2   thorpej 			/* Ack the reset bit. */
    418   1.2   thorpej 			bus_space_write_1(nict, nich, ED_P0_ISR, ED_ISR_RST);
    419  1.28        ws 			NIC_BARRIER(nict, nich);
    420   1.2   thorpej 			break;
    421   1.2   thorpej 		}
    422   1.2   thorpej 		delay(100);
    423   1.2   thorpej 	}
    424   1.2   thorpej 
    425   1.2   thorpej #if 0
    426   1.2   thorpej 	/* XXX */
    427   1.2   thorpej 	if (i == 100)
    428   1.2   thorpej 		goto out;
    429   1.2   thorpej #endif
    430   1.2   thorpej 
    431   1.2   thorpej 	/*
    432   1.2   thorpej 	 * Test the ability to read and write to the NIC memory.  This has
    433   1.2   thorpej 	 * the side effect of determining if this is an NE1000 or an NE2000.
    434   1.2   thorpej 	 */
    435   1.2   thorpej 
    436   1.2   thorpej 	/*
    437   1.2   thorpej 	 * This prevents packets from being stored in the NIC memory when
    438   1.2   thorpej 	 * the readmem routine turns on the start bit in the CR.
    439   1.2   thorpej 	 */
    440   1.2   thorpej 	bus_space_write_1(nict, nich, ED_P0_RCR, ED_RCR_MON);
    441  1.28        ws 	NIC_BARRIER(nict, nich);
    442   1.2   thorpej 
    443   1.2   thorpej 	/* Temporarily initialize DCR for byte operations. */
    444   1.2   thorpej 	bus_space_write_1(nict, nich, ED_P0_DCR, ED_DCR_FT1 | ED_DCR_LS);
    445   1.2   thorpej 
    446   1.2   thorpej 	bus_space_write_1(nict, nich, ED_P0_PSTART, 8192 >> ED_PAGE_SHIFT);
    447   1.2   thorpej 	bus_space_write_1(nict, nich, ED_P0_PSTOP, 16384 >> ED_PAGE_SHIFT);
    448   1.2   thorpej 
    449   1.2   thorpej 	/*
    450   1.2   thorpej 	 * Write a test pattern in byte mode.  If this fails, then there
    451   1.2   thorpej 	 * probably isn't any memory at 8k - which likely means that the
    452   1.2   thorpej 	 * board is an NE2000.
    453   1.2   thorpej 	 */
    454   1.2   thorpej 	ne2000_writemem(nict, nich, asict, asich, test_pattern, 8192,
    455  1.15   thorpej 	    sizeof(test_pattern), 0, 1);
    456   1.2   thorpej 	ne2000_readmem(nict, nich, asict, asich, 8192, test_buffer,
    457   1.2   thorpej 	    sizeof(test_buffer), 0);
    458   1.2   thorpej 
    459  1.68   tsutsui 	if (memcmp(test_pattern, test_buffer, sizeof(test_pattern)) == 0) {
    460   1.2   thorpej 		/* We're an NE1000. */
    461   1.2   thorpej 		rv = NE2000_TYPE_NE1000;
    462  1.68   tsutsui 		goto out;
    463  1.68   tsutsui 	}
    464  1.68   tsutsui 
    465  1.68   tsutsui 	/* not an NE1000 - try NE2000 */
    466  1.68   tsutsui 
    467  1.68   tsutsui 	/* try 16 bit mode first */
    468  1.68   tsutsui 	useword = 1;
    469  1.68   tsutsui 
    470  1.68   tsutsui #ifdef NE2000_DETECT_8BIT
    471  1.68   tsutsui 	/*
    472  1.68   tsutsui 	 * Check bus type in EEPROM first because some NE2000 compatible wedges
    473  1.68   tsutsui 	 * on 16 bit DMA access if the chip is configured in 8 bit mode.
    474  1.68   tsutsui 	 */
    475  1.68   tsutsui 	if (ne2000_detect_8bit(nict, nich, asict, asich))
    476  1.68   tsutsui 		useword = 0;
    477  1.68   tsutsui #endif
    478  1.68   tsutsui  again:
    479  1.68   tsutsui 	bus_space_write_1(nict, nich, ED_P0_DCR, ED_DCR_FT1 | ED_DCR_LS |
    480  1.68   tsutsui 	    (useword ? ED_DCR_WTS : 0));
    481  1.68   tsutsui 	bus_space_write_1(nict, nich, ED_P0_PSTART, 16384 >> ED_PAGE_SHIFT);
    482  1.68   tsutsui 	bus_space_write_1(nict, nich, ED_P0_PSTOP,
    483  1.68   tsutsui 	    (16384 + (useword ? 16384 : 8192)) >> ED_PAGE_SHIFT);
    484  1.68   tsutsui 
    485  1.68   tsutsui 	/*
    486  1.68   tsutsui 	 * Write the test pattern in word mode.  If this also fails,
    487  1.68   tsutsui 	 * then we don't know what this board is.
    488  1.68   tsutsui 	 */
    489  1.68   tsutsui 	ne2000_writemem(nict, nich, asict, asich, test_pattern, 16384,
    490  1.72   tsutsui 	    sizeof(test_pattern), useword, 1);
    491  1.68   tsutsui 	ne2000_readmem(nict, nich, asict, asich, 16384, test_buffer,
    492  1.68   tsutsui 	    sizeof(test_buffer), useword);
    493  1.68   tsutsui 
    494  1.68   tsutsui 	if (memcmp(test_pattern, test_buffer, sizeof(test_pattern)) != 0) {
    495  1.68   tsutsui 		if (useword == 1) {
    496  1.68   tsutsui 			/* try 8 bit mode */
    497  1.68   tsutsui 			useword = 0;
    498  1.68   tsutsui 			goto again;
    499  1.68   tsutsui 		}
    500  1.68   tsutsui 		return NE2000_TYPE_UNKNOWN;	/* not an NE2000 either */
    501   1.2   thorpej 	}
    502   1.2   thorpej 
    503  1.68   tsutsui 	rv = NE2000_TYPE_NE2000;
    504  1.68   tsutsui 
    505  1.68   tsutsui #if NRTL80X9 > 0
    506  1.68   tsutsui 	/* Check for a Realtek RTL8019. */
    507  1.68   tsutsui 	if (bus_space_read_1(nict, nich, NERTL_RTL0_8019ID0) == RTL0_8019ID0 &&
    508  1.68   tsutsui 	    bus_space_read_1(nict, nich, NERTL_RTL0_8019ID1) == RTL0_8019ID1)
    509  1.68   tsutsui 		rv = NE2000_TYPE_RTL8019;
    510  1.68   tsutsui #endif
    511  1.68   tsutsui 
    512  1.68   tsutsui  out:
    513   1.2   thorpej 	/* Clear any pending interrupts that might have occurred above. */
    514  1.28        ws 	NIC_BARRIER(nict, nich);
    515   1.2   thorpej 	bus_space_write_1(nict, nich, ED_P0_ISR, 0xff);
    516   1.2   thorpej 
    517  1.69   tsutsui 	return rv;
    518   1.2   thorpej }
    519   1.2   thorpej 
    520  1.68   tsutsui #ifdef NE2000_DETECT_8BIT
    521  1.68   tsutsui static bool
    522  1.68   tsutsui ne2000_detect_8bit(bus_space_tag_t nict, bus_space_handle_t nich,
    523  1.68   tsutsui     bus_space_tag_t asict, bus_space_handle_t asich)
    524  1.68   tsutsui {
    525  1.68   tsutsui 	bool is8bit;
    526  1.68   tsutsui 	uint8_t romdata[32];
    527  1.68   tsutsui 
    528  1.68   tsutsui 	is8bit = false;
    529  1.68   tsutsui 
    530  1.68   tsutsui 	/* Set DCR for 8 bit DMA. */
    531  1.68   tsutsui 	bus_space_write_1(nict, nich, ED_P0_DCR, ED_DCR_FT1 | ED_DCR_LS);
    532  1.68   tsutsui 	/* Read PROM area. */
    533  1.68   tsutsui 	ne2000_readmem(nict, nich, asict, asich, 0, romdata,
    534  1.68   tsutsui 	    sizeof(romdata), 0);
    535  1.68   tsutsui 	if (romdata[28] == 'B' && romdata[30] == 'B') {
    536  1.68   tsutsui 		/* 'B' (0x42) in 8 bit mode, 'W' (0x57) in 16 bit mode */
    537  1.68   tsutsui 		is8bit = true;
    538  1.68   tsutsui 	}
    539  1.68   tsutsui 	if (!is8bit) {
    540  1.68   tsutsui 		/* not in 8 bit mode; put back DCR setting for 16 bit DMA */
    541  1.68   tsutsui 		bus_space_write_1(nict, nich, ED_P0_DCR,
    542  1.68   tsutsui 		    ED_DCR_FT1 | ED_DCR_LS | ED_DCR_WTS);
    543  1.68   tsutsui 	}
    544  1.68   tsutsui 
    545  1.68   tsutsui 	return is8bit;
    546  1.68   tsutsui }
    547  1.68   tsutsui #endif
    548  1.68   tsutsui 
    549   1.2   thorpej /*
    550   1.2   thorpej  * Write an mbuf chain to the destination NIC memory address using programmed
    551   1.2   thorpej  * I/O.
    552   1.2   thorpej  */
    553   1.2   thorpej int
    554  1.60       dsl ne2000_write_mbuf(struct dp8390_softc *sc, struct mbuf *m, int buf)
    555   1.2   thorpej {
    556   1.2   thorpej 	struct ne2000_softc *nsc = (struct ne2000_softc *)sc;
    557   1.2   thorpej 	bus_space_tag_t nict = sc->sc_regt;
    558   1.2   thorpej 	bus_space_handle_t nich = sc->sc_regh;
    559   1.2   thorpej 	bus_space_tag_t asict = nsc->sc_asict;
    560   1.2   thorpej 	bus_space_handle_t asich = nsc->sc_asich;
    561  1.38    bouyer 	int savelen, padlen;
    562   1.2   thorpej 	int maxwait = 100;	/* about 120us */
    563   1.2   thorpej 
    564   1.2   thorpej 	savelen = m->m_pkthdr.len;
    565  1.38    bouyer 	if (savelen < ETHER_MIN_LEN - ETHER_CRC_LEN) {
    566  1.38    bouyer 		padlen = ETHER_MIN_LEN - ETHER_CRC_LEN - savelen;
    567  1.38    bouyer 		savelen = ETHER_MIN_LEN - ETHER_CRC_LEN;
    568  1.38    bouyer 	} else
    569  1.38    bouyer 		padlen = 0;
    570  1.38    bouyer 
    571   1.2   thorpej 
    572   1.2   thorpej 	/* Select page 0 registers. */
    573  1.28        ws 	NIC_BARRIER(nict, nich);
    574   1.3     enami 	bus_space_write_1(nict, nich, ED_P0_CR,
    575   1.3     enami 	    ED_CR_RD2 | ED_CR_PAGE_0 | ED_CR_STA);
    576  1.28        ws 	NIC_BARRIER(nict, nich);
    577   1.2   thorpej 
    578   1.2   thorpej 	/* Reset remote DMA complete flag. */
    579   1.3     enami 	bus_space_write_1(nict, nich, ED_P0_ISR, ED_ISR_RDC);
    580  1.28        ws 	NIC_BARRIER(nict, nich);
    581   1.2   thorpej 
    582   1.2   thorpej 	/* Set up DMA byte count. */
    583   1.3     enami 	bus_space_write_1(nict, nich, ED_P0_RBCR0, savelen);
    584   1.3     enami 	bus_space_write_1(nict, nich, ED_P0_RBCR1, savelen >> 8);
    585   1.2   thorpej 
    586   1.2   thorpej 	/* Set up destination address in NIC mem. */
    587   1.3     enami 	bus_space_write_1(nict, nich, ED_P0_RSAR0, buf);
    588   1.3     enami 	bus_space_write_1(nict, nich, ED_P0_RSAR1, buf >> 8);
    589   1.2   thorpej 
    590   1.2   thorpej 	/* Set remote DMA write. */
    591  1.28        ws 	NIC_BARRIER(nict, nich);
    592   1.3     enami 	bus_space_write_1(nict, nich,
    593   1.3     enami 	    ED_P0_CR, ED_CR_RD1 | ED_CR_PAGE_0 | ED_CR_STA);
    594  1.28        ws 	NIC_BARRIER(nict, nich);
    595   1.2   thorpej 
    596   1.2   thorpej 	/*
    597   1.2   thorpej 	 * Transfer the mbuf chain to the NIC memory.  NE2000 cards
    598   1.2   thorpej 	 * require that data be transferred as words, and only words,
    599   1.2   thorpej 	 * so that case requires some extra code to patch over odd-length
    600   1.2   thorpej 	 * mbufs.
    601   1.2   thorpej 	 */
    602  1.68   tsutsui 	if (nsc->sc_useword == 0) {
    603  1.68   tsutsui 		/* byte ops are easy. */
    604  1.69   tsutsui 		for (; m != NULL; m = m->m_next) {
    605   1.2   thorpej 			if (m->m_len) {
    606   1.2   thorpej 				bus_space_write_multi_1(asict, asich,
    607  1.69   tsutsui 				    NE2000_ASIC_DATA, mtod(m, uint8_t *),
    608   1.2   thorpej 				    m->m_len);
    609   1.2   thorpej 			}
    610   1.2   thorpej 		}
    611  1.38    bouyer 		if (padlen) {
    612  1.38    bouyer 			for(; padlen > 0; padlen--)
    613  1.38    bouyer 				bus_space_write_1(asict, asich,
    614  1.38    bouyer 				    NE2000_ASIC_DATA, 0);
    615  1.38    bouyer 		}
    616   1.2   thorpej 	} else {
    617  1.68   tsutsui 		/* word ops are a bit trickier. */
    618  1.69   tsutsui 		uint8_t *data, savebyte[2];
    619  1.12   thorpej 		int l, leftover;
    620  1.12   thorpej #ifdef DIAGNOSTIC
    621  1.69   tsutsui 		uint8_t *lim;
    622  1.12   thorpej #endif
    623  1.12   thorpej 		/* Start out with no leftover data. */
    624  1.12   thorpej 		leftover = 0;
    625  1.12   thorpej 		savebyte[0] = savebyte[1] = 0;
    626   1.2   thorpej 
    627  1.69   tsutsui 		for (; m != NULL; m = m->m_next) {
    628   1.2   thorpej 			l = m->m_len;
    629   1.2   thorpej 			if (l == 0)
    630   1.2   thorpej 				continue;
    631  1.69   tsutsui 			data = mtod(m, uint8_t *);
    632  1.12   thorpej #ifdef DIAGNOSTIC
    633  1.12   thorpej 			lim = data + l;
    634  1.12   thorpej #endif
    635  1.12   thorpej 			while (l > 0) {
    636  1.12   thorpej 				if (leftover) {
    637  1.12   thorpej 					/*
    638  1.12   thorpej 					 * Data left over (from mbuf or
    639  1.12   thorpej 					 * realignment).  Buffer the next
    640  1.12   thorpej 					 * byte, and write it and the
    641  1.12   thorpej 					 * leftover data out.
    642  1.12   thorpej 					 */
    643  1.12   thorpej 					savebyte[1] = *data++;
    644  1.12   thorpej 					l--;
    645  1.12   thorpej 					bus_space_write_stream_2(asict, asich,
    646  1.12   thorpej 					    NE2000_ASIC_DATA,
    647  1.69   tsutsui 					    *(uint16_t *)savebyte);
    648  1.12   thorpej 					leftover = 0;
    649  1.18  drochner 				} else if (BUS_SPACE_ALIGNED_POINTER(data,
    650  1.69   tsutsui 					   uint16_t) == 0) {
    651  1.12   thorpej 					/*
    652  1.12   thorpej 					 * Unaligned data; buffer the next
    653  1.12   thorpej 					 * byte.
    654  1.12   thorpej 					 */
    655  1.12   thorpej 					savebyte[0] = *data++;
    656  1.12   thorpej 					l--;
    657  1.12   thorpej 					leftover = 1;
    658  1.12   thorpej 				} else {
    659  1.12   thorpej 					/*
    660  1.12   thorpej 					 * Aligned data; output contiguous
    661  1.12   thorpej 					 * words as much as we can, then
    662  1.12   thorpej 					 * buffer the remaining byte, if any.
    663  1.12   thorpej 					 */
    664  1.12   thorpej 					leftover = l & 1;
    665  1.12   thorpej 					l &= ~1;
    666  1.12   thorpej 					bus_space_write_multi_stream_2(asict,
    667  1.12   thorpej 					    asich, NE2000_ASIC_DATA,
    668  1.69   tsutsui 					    (uint16_t *)data, l >> 1);
    669  1.12   thorpej 					data += l;
    670  1.12   thorpej 					if (leftover)
    671  1.12   thorpej 						savebyte[0] = *data++;
    672  1.12   thorpej 					l = 0;
    673  1.12   thorpej 				}
    674   1.2   thorpej 			}
    675  1.12   thorpej 			if (l < 0)
    676  1.12   thorpej 				panic("ne2000_write_mbuf: negative len");
    677  1.12   thorpej #ifdef DIAGNOSTIC
    678  1.12   thorpej 			if (data != lim)
    679  1.12   thorpej 				panic("ne2000_write_mbuf: data != lim");
    680  1.12   thorpej #endif
    681   1.2   thorpej 		}
    682  1.12   thorpej 		if (leftover) {
    683   1.2   thorpej 			savebyte[1] = 0;
    684  1.10  sakamoto 			bus_space_write_stream_2(asict, asich, NE2000_ASIC_DATA,
    685  1.69   tsutsui 			    *(uint16_t *)savebyte);
    686  1.38    bouyer 		}
    687  1.38    bouyer 		if (padlen) {
    688  1.43   mycroft 			for(; padlen > 1; padlen -= 2)
    689  1.38    bouyer 				bus_space_write_stream_2(asict, asich,
    690  1.38    bouyer 				    NE2000_ASIC_DATA, 0);
    691   1.2   thorpej 		}
    692   1.2   thorpej 	}
    693  1.28        ws 	NIC_BARRIER(nict, nich);
    694   1.2   thorpej 
    695  1.74   rkujawa 	/* some AX88796 doesn't seem to have remote DMA complete */
    696  1.46       bsh 	if (sc->sc_flags & DP8390_NO_REMOTE_DMA_COMPLETE)
    697  1.69   tsutsui 		return savelen;
    698  1.46       bsh 
    699   1.2   thorpej 	/*
    700   1.2   thorpej 	 * Wait for remote DMA to complete.  This is necessary because on the
    701   1.2   thorpej 	 * transmit side, data is handled internally by the NIC in bursts, and
    702   1.2   thorpej 	 * we can't start another remote DMA until this one completes.  Not
    703   1.2   thorpej 	 * waiting causes really bad things to happen - like the NIC wedging
    704   1.2   thorpej 	 * the bus.
    705   1.2   thorpej 	 */
    706   1.2   thorpej 	while (((bus_space_read_1(nict, nich, ED_P0_ISR) & ED_ISR_RDC) !=
    707  1.28        ws 	    ED_ISR_RDC) && --maxwait) {
    708  1.49  christos 		(void)bus_space_read_1(nict, nich, ED_P0_CRDA1);
    709  1.49  christos 		(void)bus_space_read_1(nict, nich, ED_P0_CRDA0);
    710  1.28        ws 		NIC_BARRIER(nict, nich);
    711  1.28        ws 		DELAY(1);
    712  1.28        ws 	}
    713   1.2   thorpej 
    714   1.2   thorpej 	if (maxwait == 0) {
    715   1.2   thorpej 		log(LOG_WARNING,
    716   1.2   thorpej 		    "%s: remote transmit DMA failed to complete\n",
    717  1.57      cube 		    device_xname(sc->sc_dev));
    718   1.2   thorpej 		dp8390_reset(sc);
    719   1.2   thorpej 	}
    720   1.2   thorpej 
    721  1.69   tsutsui 	return savelen;
    722   1.2   thorpej }
    723   1.2   thorpej 
    724   1.2   thorpej /*
    725   1.2   thorpej  * Given a source and destination address, copy 'amout' of a packet from
    726   1.2   thorpej  * the ring buffer into a linear destination buffer.  Takes into account
    727   1.2   thorpej  * ring-wrap.
    728   1.2   thorpej  */
    729   1.2   thorpej int
    730  1.60       dsl ne2000_ring_copy(struct dp8390_softc *sc, int src, void *dstv, u_short amount)
    731   1.2   thorpej {
    732  1.54  christos 	char *dst = dstv;
    733   1.2   thorpej 	struct ne2000_softc *nsc = (struct ne2000_softc *)sc;
    734   1.2   thorpej 	bus_space_tag_t nict = sc->sc_regt;
    735   1.2   thorpej 	bus_space_handle_t nich = sc->sc_regh;
    736   1.2   thorpej 	bus_space_tag_t asict = nsc->sc_asict;
    737   1.2   thorpej 	bus_space_handle_t asich = nsc->sc_asich;
    738   1.2   thorpej 	u_short tmp_amount;
    739  1.42   mycroft 	int useword = nsc->sc_useword;
    740   1.2   thorpej 
    741   1.2   thorpej 	/* Does copy wrap to lower addr in ring buffer? */
    742   1.2   thorpej 	if (src + amount > sc->mem_end) {
    743   1.2   thorpej 		tmp_amount = sc->mem_end - src;
    744   1.2   thorpej 
    745   1.2   thorpej 		/* Copy amount up to end of NIC memory. */
    746   1.2   thorpej 		ne2000_readmem(nict, nich, asict, asich, src,
    747  1.69   tsutsui 		    (uint8_t *)dst, tmp_amount, useword);
    748   1.2   thorpej 
    749   1.2   thorpej 		amount -= tmp_amount;
    750   1.2   thorpej 		src = sc->mem_ring;
    751   1.2   thorpej 		dst += tmp_amount;
    752   1.2   thorpej 	}
    753   1.2   thorpej 
    754  1.69   tsutsui 	ne2000_readmem(nict, nich, asict, asich, src, (uint8_t *)dst,
    755   1.2   thorpej 	    amount, useword);
    756   1.2   thorpej 
    757  1.69   tsutsui 	return src + amount;
    758   1.2   thorpej }
    759   1.2   thorpej 
    760   1.2   thorpej void
    761  1.60       dsl ne2000_read_hdr(struct dp8390_softc *sc, int buf, struct dp8390_ring *hdr)
    762   1.2   thorpej {
    763   1.2   thorpej 	struct ne2000_softc *nsc = (struct ne2000_softc *)sc;
    764   1.2   thorpej 
    765   1.2   thorpej 	ne2000_readmem(sc->sc_regt, sc->sc_regh, nsc->sc_asict, nsc->sc_asich,
    766  1.69   tsutsui 	    buf, (uint8_t *)hdr, sizeof(struct dp8390_ring),
    767  1.42   mycroft 	    nsc->sc_useword);
    768   1.7   thorpej #if BYTE_ORDER == BIG_ENDIAN
    769   1.7   thorpej 	hdr->count = bswap16(hdr->count);
    770   1.7   thorpej #endif
    771   1.2   thorpej }
    772   1.2   thorpej 
    773   1.2   thorpej int
    774  1.52  christos ne2000_test_mem(struct dp8390_softc *sc)
    775   1.2   thorpej {
    776   1.2   thorpej 
    777   1.2   thorpej 	/* Noop. */
    778  1.69   tsutsui 	return 0;
    779   1.2   thorpej }
    780   1.2   thorpej 
    781   1.2   thorpej /*
    782   1.2   thorpej  * Given a NIC memory source address and a host memory destination address,
    783   1.2   thorpej  * copy 'amount' from NIC to host using programmed i/o.  The 'amount' is
    784   1.2   thorpej  * rounded up to a word - ok as long as mbufs are word sized.
    785   1.2   thorpej  */
    786   1.2   thorpej void
    787  1.69   tsutsui ne2000_readmem(bus_space_tag_t nict, bus_space_handle_t nich,
    788  1.69   tsutsui     bus_space_tag_t asict, bus_space_handle_t asich,
    789  1.69   tsutsui     int src, uint8_t *dst, size_t amount, int useword)
    790   1.2   thorpej {
    791   1.2   thorpej 
    792   1.2   thorpej 	/* Select page 0 registers. */
    793  1.28        ws 	NIC_BARRIER(nict, nich);
    794   1.2   thorpej 	bus_space_write_1(nict, nich, ED_P0_CR,
    795   1.2   thorpej 	    ED_CR_RD2 | ED_CR_PAGE_0 | ED_CR_STA);
    796  1.28        ws 	NIC_BARRIER(nict, nich);
    797   1.2   thorpej 
    798   1.2   thorpej 	/* Round up to a word. */
    799  1.66   tsutsui 	amount = roundup2(amount, sizeof(uint16_t));
    800   1.2   thorpej 
    801   1.2   thorpej 	/* Set up DMA byte count. */
    802   1.2   thorpej 	bus_space_write_1(nict, nich, ED_P0_RBCR0, amount);
    803   1.2   thorpej 	bus_space_write_1(nict, nich, ED_P0_RBCR1, amount >> 8);
    804   1.2   thorpej 
    805   1.2   thorpej 	/* Set up source address in NIC mem. */
    806   1.2   thorpej 	bus_space_write_1(nict, nich, ED_P0_RSAR0, src);
    807   1.2   thorpej 	bus_space_write_1(nict, nich, ED_P0_RSAR1, src >> 8);
    808   1.2   thorpej 
    809  1.28        ws 	NIC_BARRIER(nict, nich);
    810   1.2   thorpej 	bus_space_write_1(nict, nich, ED_P0_CR,
    811   1.2   thorpej 	    ED_CR_RD0 | ED_CR_PAGE_0 | ED_CR_STA);
    812   1.2   thorpej 
    813  1.44   mycroft 	ASIC_BARRIER(asict, asich);
    814   1.2   thorpej 	if (useword)
    815  1.10  sakamoto 		bus_space_read_multi_stream_2(asict, asich, NE2000_ASIC_DATA,
    816  1.69   tsutsui 		    (uint16_t *)dst, amount >> 1);
    817   1.2   thorpej 	else
    818   1.2   thorpej 		bus_space_read_multi_1(asict, asich, NE2000_ASIC_DATA,
    819   1.2   thorpej 		    dst, amount);
    820   1.2   thorpej }
    821   1.2   thorpej 
    822   1.2   thorpej /*
    823   1.2   thorpej  * Stripped down routine for writing a linear buffer to NIC memory.  Only
    824   1.2   thorpej  * used in the probe routine to test the memory.  'len' must be even.
    825   1.2   thorpej  */
    826   1.2   thorpej void
    827  1.69   tsutsui ne2000_writemem(bus_space_tag_t nict, bus_space_handle_t nich,
    828  1.69   tsutsui     bus_space_tag_t asict, bus_space_handle_t asich,
    829  1.70   tsutsui     const uint8_t *src, int dst, size_t len, int useword, int quiet)
    830   1.2   thorpej {
    831   1.2   thorpej 	int maxwait = 100;	/* about 120us */
    832   1.2   thorpej 
    833   1.2   thorpej 	/* Select page 0 registers. */
    834  1.28        ws 	NIC_BARRIER(nict, nich);
    835   1.2   thorpej 	bus_space_write_1(nict, nich, ED_P0_CR,
    836   1.2   thorpej 	    ED_CR_RD2 | ED_CR_PAGE_0 | ED_CR_STA);
    837  1.28        ws 	NIC_BARRIER(nict, nich);
    838   1.2   thorpej 
    839   1.2   thorpej 	/* Reset remote DMA complete flag. */
    840   1.2   thorpej 	bus_space_write_1(nict, nich, ED_P0_ISR, ED_ISR_RDC);
    841  1.28        ws 	NIC_BARRIER(nict, nich);
    842   1.2   thorpej 
    843   1.2   thorpej 	/* Set up DMA byte count. */
    844   1.2   thorpej 	bus_space_write_1(nict, nich, ED_P0_RBCR0, len);
    845   1.2   thorpej 	bus_space_write_1(nict, nich, ED_P0_RBCR1, len >> 8);
    846   1.2   thorpej 
    847   1.2   thorpej 	/* Set up destination address in NIC mem. */
    848   1.2   thorpej 	bus_space_write_1(nict, nich, ED_P0_RSAR0, dst);
    849   1.2   thorpej 	bus_space_write_1(nict, nich, ED_P0_RSAR1, dst >> 8);
    850   1.2   thorpej 
    851   1.2   thorpej 	/* Set remote DMA write. */
    852  1.28        ws 	NIC_BARRIER(nict, nich);
    853   1.2   thorpej 	bus_space_write_1(nict, nich, ED_P0_CR,
    854   1.2   thorpej 	    ED_CR_RD1 | ED_CR_PAGE_0 | ED_CR_STA);
    855  1.44   mycroft 	NIC_BARRIER(nict, nich);
    856   1.2   thorpej 
    857  1.44   mycroft 	ASIC_BARRIER(asict, asich);
    858   1.2   thorpej 	if (useword)
    859  1.10  sakamoto 		bus_space_write_multi_stream_2(asict, asich, NE2000_ASIC_DATA,
    860  1.70   tsutsui 		    (const uint16_t *)src, len >> 1);
    861   1.2   thorpej 	else
    862   1.2   thorpej 		bus_space_write_multi_1(asict, asich, NE2000_ASIC_DATA,
    863   1.2   thorpej 		    src, len);
    864  1.44   mycroft 	ASIC_BARRIER(asict, asich);
    865   1.2   thorpej 
    866   1.2   thorpej 	/*
    867   1.2   thorpej 	 * Wait for remote DMA to complete.  This is necessary because on the
    868   1.2   thorpej 	 * transmit side, data is handled internally by the NIC in bursts, and
    869   1.2   thorpej 	 * we can't start another remote DMA until this one completes.  Not
    870   1.2   thorpej 	 * waiting causes really bad things to happen - like the NIC wedging
    871   1.2   thorpej 	 * the bus.
    872   1.2   thorpej 	 */
    873   1.2   thorpej 	while (((bus_space_read_1(nict, nich, ED_P0_ISR) & ED_ISR_RDC) !=
    874  1.28        ws 	    ED_ISR_RDC) && --maxwait)
    875  1.28        ws 		DELAY(1);
    876   1.2   thorpej 
    877  1.15   thorpej 	if (!quiet && maxwait == 0)
    878   1.2   thorpej 		printf("ne2000_writemem: failed to complete\n");
    879  1.24    itojun }
    880  1.24    itojun 
    881  1.24    itojun int
    882  1.60       dsl ne2000_detach(struct ne2000_softc *sc, int flags)
    883  1.24    itojun {
    884  1.26     enami 
    885  1.69   tsutsui 	return dp8390_detach(&sc->sc_dp8390, flags);
    886   1.2   thorpej }
    887  1.29        ws 
    888  1.29        ws #ifdef IPKDB_NE
    889  1.29        ws /*
    890  1.29        ws  * This code is essentially the same as ne2000_attach above.
    891  1.29        ws  */
    892  1.29        ws int
    893  1.60       dsl ne2000_ipkdb_attach(struct ipkdb_if *kip)
    894  1.29        ws {
    895  1.29        ws 	struct ne2000_softc *np = kip->port;
    896  1.29        ws 	struct dp8390_softc *dp = &np->sc_dp8390;
    897  1.29        ws 	bus_space_tag_t nict = dp->sc_regt;
    898  1.29        ws 	bus_space_handle_t nich = dp->sc_regh;
    899  1.68   tsutsui 	bus_space_tag_t asict = np->sc_asict;
    900  1.68   tsutsui 	bus_space_handle_t asich = np->sc_asich;
    901  1.68   tsutsui 	int i, useword;
    902  1.29        ws 
    903  1.29        ws #ifdef GWETHER
    904  1.29        ws 	/* Not supported (yet?) */
    905  1.29        ws 	return -1;
    906  1.29        ws #endif
    907  1.29        ws 
    908  1.62    nonaka 	if (np->sc_type == NE2000_TYPE_UNKNOWN)
    909  1.68   tsutsui 		np->sc_type = ne2000_detect(nict, nich, asict, asich);
    910  1.62    nonaka 	if (np->sc_type == NE2000_TYPE_UNKNOWN)
    911  1.29        ws 		return -1;
    912  1.29        ws 
    913  1.29        ws 	switch (np->sc_type) {
    914  1.29        ws 	case NE2000_TYPE_NE1000:
    915  1.68   tsutsui 		dp->mem_start = 8192;
    916  1.68   tsutsui 		dp->mem_size = 8192;
    917  1.68   tsutsui 		useword = 0;
    918  1.29        ws 		kip->name = "ne1000";
    919  1.29        ws 		break;
    920  1.29        ws 	case NE2000_TYPE_NE2000:
    921  1.68   tsutsui 	case NE2000_TYPE_AX88190:
    922  1.68   tsutsui 	case NE2000_TYPE_AX88790:
    923  1.74   rkujawa 	case NE2000_TYPE_AX88796:
    924  1.68   tsutsui #if NRTL80X9 > 0
    925  1.68   tsutsui 	case NE2000_TYPE_RTL8019:
    926  1.68   tsutsui #endif
    927  1.68   tsutsui 		dp->mem_start = 16384;
    928  1.68   tsutsui 		dp->mem_size = 16384;
    929  1.68   tsutsui 		useword = 1;
    930  1.68   tsutsui 		if (
    931  1.68   tsutsui #ifdef NE2000_DETECT_8BIT
    932  1.68   tsutsui 		    ne2000_detect_8bit(nict, nich, asict, asich) ||
    933  1.68   tsutsui #endif
    934  1.68   tsutsui 		    (np->sc_quirk & NE2000_QUIRK_8BIT) != 0) {
    935  1.68   tsutsui 			/* in 8 bit mode, only 8KB memory can be used */
    936  1.68   tsutsui 			dp->mem_size = 8192;
    937  1.68   tsutsui 			useword = 0;
    938  1.68   tsutsui 		}
    939  1.68   tsutsui 		kip->name =
    940  1.68   tsutsui 		    (np->sc_type == NE2000_TYPE_AX88190 ||
    941  1.68   tsutsui 		     np->sc_type == NE2000_TYPE_AX88790) ?
    942  1.68   tsutsui 		    "ax88190" : "ne2000";
    943  1.29        ws 		break;
    944  1.29        ws 	case NE2000_TYPE_DL10019:
    945  1.33   thorpej 	case NE2000_TYPE_DL10022:
    946  1.68   tsutsui 		dp->mem_start = 8192 * 3;
    947  1.68   tsutsui 		dp->mem_size = 8192 * 3;
    948  1.68   tsutsui 		useword = 1;
    949  1.33   thorpej 		kip->name = (np->sc_type == NE2000_TYPE_DL10019) ?
    950  1.33   thorpej 		    "dl10022" : "dl10019";
    951  1.29        ws 		break;
    952  1.53      cube 	default:
    953  1.53      cube 		return -1;
    954  1.53      cube 		break;
    955  1.29        ws 	}
    956  1.29        ws 
    957  1.68   tsutsui 	np->sc_useword = useword;
    958  1.68   tsutsui #if NRTL80X9 > 0
    959  1.68   tsutsui 	if (np->sc_type == NE2000_TYPE_RTL8019) {
    960  1.68   tsutsui 		dp->init_card = rtl80x9_init_card;
    961  1.68   tsutsui 		dp->sc_media_init = rtl80x9_media_init;
    962  1.68   tsutsui 		dp->sc_mediachange = rtl80x9_mediachange;
    963  1.68   tsutsui 		dp->sc_mediastatus = rtl80x9_mediastatus;
    964  1.68   tsutsui 	}
    965  1.68   tsutsui #endif
    966  1.68   tsutsui 
    967  1.68   tsutsui 	dp->cr_proto = ED_CR_RD2;
    968  1.68   tsutsui 	if (np->sc_type == NE2000_TYPE_AX88190 ||
    969  1.68   tsutsui 	    np->sc_type == NE2000_TYPE_AX88790) {
    970  1.68   tsutsui 		dp->rcr_proto = ED_RCR_INTT;
    971  1.68   tsutsui 		dp->sc_flags |= DP8390_DO_AX88190_WORKAROUND;
    972  1.68   tsutsui 	} else
    973  1.68   tsutsui 		dp->rcr_proto = 0;
    974  1.68   tsutsui 	dp->dcr_reg = ED_DCR_FT1 | ED_DCR_LS | (useword ? ED_DCR_WTS : 0);
    975  1.68   tsutsui 
    976  1.68   tsutsui 	dp->test_mem = ne2000_test_mem;
    977  1.68   tsutsui 	dp->ring_copy = ne2000_ring_copy;
    978  1.68   tsutsui 	dp->write_mbuf = ne2000_write_mbuf;
    979  1.68   tsutsui 	dp->read_hdr = ne2000_read_hdr;
    980  1.68   tsutsui 
    981  1.68   tsutsui 	for (i = 0; i < 16; i++)
    982  1.68   tsutsui 		dp->sc_reg_map[i] = i;
    983  1.68   tsutsui 
    984  1.29        ws 	if (dp8390_ipkdb_attach(kip))
    985  1.29        ws 		return -1;
    986  1.29        ws 
    987  1.29        ws 	if (!(kip->flags & IPKDB_MYHW)) {
    988  1.29        ws 		char romdata[16];
    989  1.29        ws 
    990  1.29        ws 		/* Read the station address. */
    991  1.39  christos 		if (np->sc_type == NE2000_TYPE_AX88190 ||
    992  1.74   rkujawa 		    np->sc_type == NE2000_TYPE_AX88790 ||
    993  1.74   rkujawa 		    np->sc_type == NE2000_TYPE_AX88796) {
    994  1.29        ws 			/* Select page 0 registers. */
    995  1.29        ws 			NIC_BARRIER(nict, nich);
    996  1.29        ws 			bus_space_write_1(nict, nich, ED_P0_CR,
    997  1.29        ws 				ED_CR_RD2 | ED_CR_PAGE_0 | ED_CR_STA);
    998  1.29        ws 			NIC_BARRIER(nict, nich);
    999  1.29        ws 			/* Select word transfer */
   1000  1.62    nonaka 			bus_space_write_1(nict, nich, ED_P0_DCR,
   1001  1.68   tsutsui 			    useword ? ED_DCR_WTS : 0);
   1002  1.68   tsutsui 			ne2000_readmem(nict, nich, asict, asich,
   1003  1.36     enami 				AX88190_NODEID_OFFSET, kip->myenetaddr,
   1004  1.29        ws 				ETHER_ADDR_LEN, useword);
   1005  1.29        ws 		} else {
   1006  1.68   tsutsui 			bool ne1000 = (np->sc_type == NE2000_TYPE_NE1000);
   1007  1.68   tsutsui 
   1008  1.68   tsutsui 			ne2000_readmem(nict, nich, asict, asich,
   1009  1.29        ws 				0, romdata, sizeof romdata, useword);
   1010  1.29        ws 			for (i = 0; i < ETHER_ADDR_LEN; i++)
   1011  1.68   tsutsui 				kip->myenetaddr[i] =
   1012  1.68   tsutsui 				    romdata[i * (ne1000 ? 1 : 2)];
   1013  1.29        ws 		}
   1014  1.29        ws 		kip->flags |= IPKDB_MYHW;
   1015  1.29        ws 
   1016  1.29        ws 	}
   1017  1.29        ws 	dp8390_stop(dp);
   1018  1.29        ws 
   1019  1.29        ws 	return 0;
   1020  1.29        ws }
   1021  1.29        ws #endif
   1022  1.50     peter 
   1023  1.61       uwe bool
   1024  1.67    dyoung ne2000_suspend(device_t self, const pmf_qual_t *qual)
   1025  1.61       uwe {
   1026  1.61       uwe 	struct ne2000_softc *sc = device_private(self);
   1027  1.61       uwe 	struct dp8390_softc *dsc = &sc->sc_dp8390;
   1028  1.61       uwe 	int s;
   1029  1.61       uwe 
   1030  1.61       uwe 	s = splnet();
   1031  1.61       uwe 
   1032  1.61       uwe 	dp8390_stop(dsc);
   1033  1.61       uwe 	dp8390_disable(dsc);
   1034  1.61       uwe 
   1035  1.61       uwe 	splx(s);
   1036  1.61       uwe 	return true;
   1037  1.61       uwe }
   1038  1.61       uwe 
   1039  1.61       uwe bool
   1040  1.67    dyoung ne2000_resume(device_t self, const pmf_qual_t *qual)
   1041  1.61       uwe {
   1042  1.61       uwe 	struct ne2000_softc *sc = device_private(self);
   1043  1.61       uwe 	struct dp8390_softc *dsc = &sc->sc_dp8390;
   1044  1.61       uwe 	struct ifnet *ifp = &dsc->sc_ec.ec_if;
   1045  1.61       uwe 	int s;
   1046  1.61       uwe 
   1047  1.61       uwe 	s = splnet();
   1048  1.61       uwe 
   1049  1.61       uwe 	if (ifp->if_flags & IFF_UP) {
   1050  1.61       uwe 		if (dp8390_enable(dsc) == 0)
   1051  1.61       uwe 			dp8390_init(dsc);
   1052  1.61       uwe 	}
   1053  1.61       uwe 
   1054  1.61       uwe 	splx(s);
   1055  1.61       uwe 	return true;
   1056  1.61       uwe }
   1057