Home | History | Annotate | Line # | Download | only in pcmcia
if_xi.c revision 1.67
      1  1.67    cegger /*	$NetBSD: if_xi.c,v 1.67 2009/05/12 14:42:18 cegger Exp $ */
      2   1.1  gmcgarry /*	OpenBSD: if_xe.c,v 1.9 1999/09/16 11:28:42 niklas Exp 	*/
      3   1.5   thorpej 
      4   1.5   thorpej /*
      5  1.39   mycroft  * Copyright (c) 2004 Charles M. Hannum.  All rights reserved.
      6  1.39   mycroft  *
      7  1.39   mycroft  * Redistribution and use in source and binary forms, with or without
      8  1.39   mycroft  * modification, are permitted provided that the following conditions
      9  1.39   mycroft  * are met:
     10  1.39   mycroft  * 1. Redistributions of source code must retain the above copyright
     11  1.39   mycroft  *    notice, this list of conditions and the following disclaimer.
     12  1.39   mycroft  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.39   mycroft  *    notice, this list of conditions and the following disclaimer in the
     14  1.39   mycroft  *    documentation and/or other materials provided with the distribution.
     15  1.39   mycroft  * 3. All advertising materials mentioning features or use of this software
     16  1.39   mycroft  *    must display the following acknowledgement:
     17  1.39   mycroft  *      This product includes software developed by Charles M. Hannum.
     18  1.39   mycroft  * 4. The name of the author may not be used to endorse or promote products
     19  1.39   mycroft  *    derived from this software without specific prior written permission.
     20   1.5   thorpej  */
     21   1.1  gmcgarry 
     22   1.1  gmcgarry /*
     23   1.1  gmcgarry  * Copyright (c) 1999 Niklas Hallqvist, Brandon Creighton, Job de Haas
     24   1.1  gmcgarry  * All rights reserved.
     25   1.1  gmcgarry  *
     26   1.1  gmcgarry  * Redistribution and use in source and binary forms, with or without
     27   1.1  gmcgarry  * modification, are permitted provided that the following conditions
     28   1.1  gmcgarry  * are met:
     29   1.1  gmcgarry  * 1. Redistributions of source code must retain the above copyright
     30   1.1  gmcgarry  *    notice, this list of conditions and the following disclaimer.
     31   1.1  gmcgarry  * 2. Redistributions in binary form must reproduce the above copyright
     32   1.1  gmcgarry  *    notice, this list of conditions and the following disclaimer in the
     33   1.1  gmcgarry  *    documentation and/or other materials provided with the distribution.
     34   1.1  gmcgarry  * 3. All advertising materials mentioning features or use of this software
     35   1.1  gmcgarry  *    must display the following acknowledgement:
     36   1.1  gmcgarry  *	This product includes software developed by Niklas Hallqvist,
     37   1.1  gmcgarry  *	Brandon Creighton and Job de Haas.
     38   1.1  gmcgarry  * 4. The name of the author may not be used to endorse or promote products
     39   1.1  gmcgarry  *    derived from this software without specific prior written permission
     40   1.1  gmcgarry  *
     41   1.1  gmcgarry  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     42   1.1  gmcgarry  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     43   1.1  gmcgarry  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     44   1.1  gmcgarry  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     45   1.1  gmcgarry  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     46   1.1  gmcgarry  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     47   1.1  gmcgarry  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     48   1.1  gmcgarry  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     49   1.1  gmcgarry  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     50   1.1  gmcgarry  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     51   1.1  gmcgarry  */
     52   1.1  gmcgarry 
     53   1.1  gmcgarry /*
     54   1.1  gmcgarry  * A driver for Xircom CreditCard PCMCIA Ethernet adapters.
     55   1.1  gmcgarry  */
     56   1.1  gmcgarry 
     57  1.18     lukem #include <sys/cdefs.h>
     58  1.67    cegger __KERNEL_RCSID(0, "$NetBSD: if_xi.c,v 1.67 2009/05/12 14:42:18 cegger Exp $");
     59   1.1  gmcgarry 
     60   1.1  gmcgarry #include "opt_inet.h"
     61  1.31    martin #include "opt_ipx.h"
     62   1.1  gmcgarry #include "bpfilter.h"
     63   1.1  gmcgarry 
     64   1.1  gmcgarry #include <sys/param.h>
     65   1.1  gmcgarry #include <sys/systm.h>
     66   1.1  gmcgarry #include <sys/device.h>
     67   1.1  gmcgarry #include <sys/ioctl.h>
     68   1.1  gmcgarry #include <sys/mbuf.h>
     69   1.1  gmcgarry #include <sys/malloc.h>
     70   1.1  gmcgarry #include <sys/socket.h>
     71  1.47   mycroft #include <sys/kernel.h>
     72  1.47   mycroft #include <sys/proc.h>
     73   1.1  gmcgarry 
     74   1.1  gmcgarry #include <net/if.h>
     75   1.1  gmcgarry #include <net/if_dl.h>
     76   1.1  gmcgarry #include <net/if_media.h>
     77   1.1  gmcgarry #include <net/if_types.h>
     78   1.1  gmcgarry #include <net/if_ether.h>
     79   1.1  gmcgarry 
     80   1.1  gmcgarry #ifdef INET
     81   1.1  gmcgarry #include <netinet/in.h>
     82   1.1  gmcgarry #include <netinet/in_systm.h>
     83   1.1  gmcgarry #include <netinet/in_var.h>
     84   1.1  gmcgarry #include <netinet/ip.h>
     85   1.1  gmcgarry #include <netinet/if_inarp.h>
     86   1.1  gmcgarry #endif
     87   1.1  gmcgarry 
     88   1.1  gmcgarry #ifdef IPX
     89   1.1  gmcgarry #include <netipx/ipx.h>
     90   1.1  gmcgarry #include <netipx/ipx_if.h>
     91   1.1  gmcgarry #endif
     92   1.1  gmcgarry 
     93   1.1  gmcgarry 
     94   1.1  gmcgarry #if NBPFILTER > 0
     95   1.1  gmcgarry #include <net/bpf.h>
     96   1.1  gmcgarry #include <net/bpfdesc.h>
     97   1.1  gmcgarry #endif
     98   1.1  gmcgarry 
     99   1.1  gmcgarry /*
    100   1.1  gmcgarry  * Maximum number of bytes to read per interrupt.  Linux recommends
    101   1.1  gmcgarry  * somewhere between 2000-22000.
    102   1.1  gmcgarry  * XXX This is currently a hard maximum.
    103   1.1  gmcgarry  */
    104   1.1  gmcgarry #define MAX_BYTES_INTR 12000
    105   1.1  gmcgarry 
    106   1.1  gmcgarry #include <dev/mii/mii.h>
    107   1.1  gmcgarry #include <dev/mii/miivar.h>
    108   1.1  gmcgarry 
    109   1.1  gmcgarry #include <dev/pcmcia/pcmciareg.h>
    110   1.1  gmcgarry #include <dev/pcmcia/pcmciavar.h>
    111   1.1  gmcgarry #include <dev/pcmcia/pcmciadevs.h>
    112   1.1  gmcgarry 
    113   1.1  gmcgarry #include <dev/pcmcia/if_xireg.h>
    114  1.39   mycroft #include <dev/pcmcia/if_xivar.h>
    115   1.1  gmcgarry 
    116   1.1  gmcgarry #ifdef __GNUC__
    117  1.54     perry #define INLINE	inline
    118   1.1  gmcgarry #else
    119   1.1  gmcgarry #define INLINE
    120   1.1  gmcgarry #endif	/* __GNUC__ */
    121   1.1  gmcgarry 
    122  1.39   mycroft #define	XIDEBUG
    123  1.40   mycroft #define	XIDEBUG_VALUE	0
    124  1.35   mycroft 
    125   1.1  gmcgarry #ifdef XIDEBUG
    126   1.1  gmcgarry #define DPRINTF(cat, x) if (xidebug & (cat)) printf x
    127   1.1  gmcgarry 
    128  1.39   mycroft #define XID_CONFIG	0x01
    129  1.39   mycroft #define XID_MII		0x02
    130  1.39   mycroft #define XID_INTR	0x04
    131  1.39   mycroft #define XID_FIFO	0x08
    132  1.39   mycroft #define	XID_MCAST	0x10
    133   1.1  gmcgarry 
    134   1.1  gmcgarry #ifdef XIDEBUG_VALUE
    135   1.1  gmcgarry int xidebug = XIDEBUG_VALUE;
    136   1.1  gmcgarry #else
    137   1.1  gmcgarry int xidebug = 0;
    138   1.1  gmcgarry #endif
    139   1.1  gmcgarry #else
    140   1.1  gmcgarry #define DPRINTF(cat, x) (void)0
    141   1.1  gmcgarry #endif
    142   1.1  gmcgarry 
    143  1.39   mycroft #define STATIC
    144   1.1  gmcgarry 
    145  1.51     perry STATIC int xi_enable(struct xi_softc *);
    146  1.51     perry STATIC void xi_disable(struct xi_softc *);
    147  1.51     perry STATIC void xi_cycle_power(struct xi_softc *);
    148  1.60  christos STATIC int xi_ether_ioctl(struct ifnet *, u_long cmd, void *);
    149  1.51     perry STATIC void xi_full_reset(struct xi_softc *);
    150  1.51     perry STATIC void xi_init(struct xi_softc *);
    151  1.60  christos STATIC int xi_ioctl(struct ifnet *, u_long, void *);
    152  1.67    cegger STATIC int xi_mdi_read(device_t, int, int);
    153  1.67    cegger STATIC void xi_mdi_write(device_t, int, int, int);
    154  1.51     perry STATIC int xi_mediachange(struct ifnet *);
    155  1.51     perry STATIC u_int16_t xi_get(struct xi_softc *);
    156  1.51     perry STATIC void xi_reset(struct xi_softc *);
    157  1.51     perry STATIC void xi_set_address(struct xi_softc *);
    158  1.51     perry STATIC void xi_start(struct ifnet *);
    159  1.67    cegger STATIC void xi_statchg(device_t);
    160  1.51     perry STATIC void xi_stop(struct xi_softc *);
    161  1.51     perry STATIC void xi_watchdog(struct ifnet *);
    162   1.3  gmcgarry 
    163  1.39   mycroft void
    164  1.66       dsl xi_attach(struct xi_softc *sc, u_int8_t *myea)
    165   1.1  gmcgarry {
    166   1.1  gmcgarry 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    167   1.1  gmcgarry 
    168  1.39   mycroft #if 0
    169   1.1  gmcgarry 	/*
    170  1.11  gmcgarry 	 * Configuration as advised by DINGO documentation.
    171   1.1  gmcgarry 	 * Dingo has some extra configuration registers in the CCR space.
    172   1.1  gmcgarry 	 */
    173  1.39   mycroft 	if (sc->sc_chipset >= XI_CHIPSET_DINGO) {
    174   1.1  gmcgarry 		struct pcmcia_mem_handle pcmh;
    175   1.1  gmcgarry 		int ccr_window;
    176  1.30    martin 		bus_size_t ccr_offset;
    177   1.1  gmcgarry 
    178  1.11  gmcgarry 		/* get access to the DINGO CCR space */
    179   1.1  gmcgarry 		if (pcmcia_mem_alloc(psc->sc_pf, PCMCIA_CCR_SIZE_DINGO,
    180   1.1  gmcgarry 			&pcmh)) {
    181   1.2  gmcgarry 			DPRINTF(XID_CONFIG, ("xi: bad mem alloc\n"));
    182   1.1  gmcgarry 			goto fail;
    183   1.1  gmcgarry 		}
    184   1.1  gmcgarry 		if (pcmcia_mem_map(psc->sc_pf, PCMCIA_MEM_ATTR,
    185   1.1  gmcgarry 			psc->sc_pf->ccr_base, PCMCIA_CCR_SIZE_DINGO,
    186   1.1  gmcgarry 			&pcmh, &ccr_offset, &ccr_window)) {
    187   1.2  gmcgarry 			DPRINTF(XID_CONFIG, ("xi: bad mem map\n"));
    188   1.1  gmcgarry 			pcmcia_mem_free(psc->sc_pf, &pcmh);
    189   1.1  gmcgarry 			goto fail;
    190   1.1  gmcgarry 		}
    191   1.1  gmcgarry 
    192  1.11  gmcgarry 		/* enable the second function - usually modem */
    193   1.1  gmcgarry 		bus_space_write_1(pcmh.memt, pcmh.memh,
    194   1.1  gmcgarry 		    ccr_offset + PCMCIA_CCR_DCOR0, PCMCIA_CCR_DCOR0_SFINT);
    195   1.1  gmcgarry 		bus_space_write_1(pcmh.memt, pcmh.memh,
    196   1.1  gmcgarry 		    ccr_offset + PCMCIA_CCR_DCOR1,
    197   1.1  gmcgarry 		    PCMCIA_CCR_DCOR1_FORCE_LEVIREQ | PCMCIA_CCR_DCOR1_D6);
    198   1.1  gmcgarry 		bus_space_write_1(pcmh.memt, pcmh.memh,
    199   1.1  gmcgarry 		    ccr_offset + PCMCIA_CCR_DCOR2, 0);
    200   1.1  gmcgarry 		bus_space_write_1(pcmh.memt, pcmh.memh,
    201   1.1  gmcgarry 		    ccr_offset + PCMCIA_CCR_DCOR3, 0);
    202   1.1  gmcgarry 		bus_space_write_1(pcmh.memt, pcmh.memh,
    203   1.1  gmcgarry 		    ccr_offset + PCMCIA_CCR_DCOR4, 0);
    204   1.1  gmcgarry 
    205   1.1  gmcgarry 		/* We don't need them anymore and can free them (I think). */
    206   1.1  gmcgarry 		pcmcia_mem_unmap(psc->sc_pf, ccr_window);
    207   1.1  gmcgarry 		pcmcia_mem_free(psc->sc_pf, &pcmh);
    208   1.1  gmcgarry 	}
    209  1.39   mycroft #endif
    210  1.11  gmcgarry 
    211  1.39   mycroft 	/* Reset and initialize the card. */
    212  1.39   mycroft 	xi_full_reset(sc);
    213   1.1  gmcgarry 
    214  1.64    cegger 	printf("%s: MAC address %s\n", device_xname(&sc->sc_dev), ether_sprintf(myea));
    215   1.1  gmcgarry 
    216   1.1  gmcgarry 	ifp = &sc->sc_ethercom.ec_if;
    217  1.39   mycroft 	/* Initialize the ifnet structure. */
    218  1.64    cegger 	strlcpy(ifp->if_xname, device_xname(&sc->sc_dev), IFNAMSIZ);
    219   1.1  gmcgarry 	ifp->if_softc = sc;
    220   1.1  gmcgarry 	ifp->if_start = xi_start;
    221   1.1  gmcgarry 	ifp->if_ioctl = xi_ioctl;
    222   1.1  gmcgarry 	ifp->if_watchdog = xi_watchdog;
    223   1.1  gmcgarry 	ifp->if_flags =
    224   1.1  gmcgarry 	    IFF_BROADCAST | IFF_NOTRAILERS | IFF_SIMPLEX | IFF_MULTICAST;
    225   1.7   thorpej 	IFQ_SET_READY(&ifp->if_snd);
    226   1.1  gmcgarry 
    227  1.39   mycroft 	/* 802.1q capability */
    228  1.39   mycroft 	sc->sc_ethercom.ec_capabilities |= ETHERCAP_VLAN_MTU;
    229  1.39   mycroft 
    230  1.39   mycroft 	/* Attach the interface. */
    231  1.39   mycroft 	if_attach(ifp);
    232  1.39   mycroft 	ether_ifattach(ifp, myea);
    233   1.1  gmcgarry 
    234   1.1  gmcgarry 	/*
    235   1.1  gmcgarry 	 * Initialize our media structures and probe the MII.
    236   1.1  gmcgarry 	 */
    237   1.1  gmcgarry 	sc->sc_mii.mii_ifp = ifp;
    238   1.1  gmcgarry 	sc->sc_mii.mii_readreg = xi_mdi_read;
    239   1.1  gmcgarry 	sc->sc_mii.mii_writereg = xi_mdi_write;
    240   1.1  gmcgarry 	sc->sc_mii.mii_statchg = xi_statchg;
    241  1.63    dyoung 	sc->sc_ethercom.ec_mii = &sc->sc_mii;
    242   1.1  gmcgarry 	ifmedia_init(&sc->sc_mii.mii_media, 0, xi_mediachange,
    243  1.63    dyoung 	    ether_mediastatus);
    244   1.1  gmcgarry 	DPRINTF(XID_MII | XID_CONFIG,
    245   1.2  gmcgarry 	    ("xi: bmsr %x\n", xi_mdi_read(&sc->sc_dev, 0, 1)));
    246  1.39   mycroft 
    247  1.39   mycroft 	mii_attach(&sc->sc_dev, &sc->sc_mii, 0xffffffff, MII_PHY_ANY,
    248   1.1  gmcgarry 		MII_OFFSET_ANY, 0);
    249   1.1  gmcgarry 	if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL)
    250   1.1  gmcgarry 		ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER | IFM_AUTO, 0,
    251   1.1  gmcgarry 		    NULL);
    252   1.1  gmcgarry 	ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER | IFM_AUTO);
    253   1.1  gmcgarry 
    254  1.11  gmcgarry #if NRND > 0
    255  1.64    cegger 	rnd_attach_source(&sc->sc_rnd_source, device_xname(&sc->sc_dev), RND_TYPE_NET, 0);
    256  1.11  gmcgarry #endif
    257   1.1  gmcgarry }
    258   1.1  gmcgarry 
    259   1.1  gmcgarry int
    260  1.67    cegger xi_detach(device_t self, int flags)
    261   1.1  gmcgarry {
    262  1.39   mycroft 	struct xi_softc *sc = (void *)self;
    263   1.1  gmcgarry 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    264   1.1  gmcgarry 
    265  1.39   mycroft 	DPRINTF(XID_CONFIG, ("xi_detach()\n"));
    266   1.1  gmcgarry 
    267  1.42   mycroft 	xi_disable(sc);
    268   1.1  gmcgarry 
    269  1.11  gmcgarry #if NRND > 0
    270  1.39   mycroft 	rnd_detach_source(&sc->sc_rnd_source);
    271  1.11  gmcgarry #endif
    272   1.1  gmcgarry 
    273  1.39   mycroft 	mii_detach(&sc->sc_mii, MII_PHY_ANY, MII_OFFSET_ANY);
    274  1.39   mycroft 	ifmedia_delete_instance(&sc->sc_mii.mii_media, IFM_INST_ANY);
    275  1.39   mycroft 	ether_ifdetach(ifp);
    276  1.39   mycroft 	if_detach(ifp);
    277   1.1  gmcgarry 
    278   1.1  gmcgarry 	return 0;
    279   1.1  gmcgarry }
    280   1.1  gmcgarry 
    281   1.1  gmcgarry int
    282  1.67    cegger xi_activate(device_t self, enum devact act)
    283  1.39   mycroft {
    284  1.39   mycroft 	struct xi_softc *sc = (void *)self;
    285  1.39   mycroft 	int s, rv = 0;
    286   1.1  gmcgarry 
    287  1.39   mycroft 	DPRINTF(XID_CONFIG, ("xi_activate()\n"));
    288   1.1  gmcgarry 
    289   1.1  gmcgarry 	s = splnet();
    290   1.1  gmcgarry 	switch (act) {
    291   1.1  gmcgarry 	case DVACT_ACTIVATE:
    292   1.1  gmcgarry 		rv = EOPNOTSUPP;
    293   1.1  gmcgarry 		break;
    294   1.1  gmcgarry 
    295   1.1  gmcgarry 	case DVACT_DEACTIVATE:
    296   1.1  gmcgarry 		if_deactivate(&sc->sc_ethercom.ec_if);
    297   1.1  gmcgarry 		break;
    298   1.1  gmcgarry 	}
    299   1.1  gmcgarry 	splx(s);
    300   1.1  gmcgarry 	return (rv);
    301   1.1  gmcgarry }
    302   1.1  gmcgarry 
    303   1.3  gmcgarry int
    304  1.66       dsl xi_intr(void *arg)
    305   1.1  gmcgarry {
    306   1.1  gmcgarry 	struct xi_softc *sc = arg;
    307   1.1  gmcgarry 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    308  1.45   mycroft 	u_int8_t esr, rsr, isr, rx_status;
    309  1.33   mycroft 	u_int16_t tx_status, recvcount = 0, tempint;
    310   1.1  gmcgarry 
    311   1.2  gmcgarry 	DPRINTF(XID_CONFIG, ("xi_intr()\n"));
    312   1.1  gmcgarry 
    313  1.39   mycroft 	if (sc->sc_enabled == 0 ||
    314  1.55   thorpej 	    !device_is_active(&sc->sc_dev))
    315   1.1  gmcgarry 		return (0);
    316   1.1  gmcgarry 
    317   1.1  gmcgarry 	ifp->if_timer = 0;	/* turn watchdog timer off */
    318   1.1  gmcgarry 
    319  1.45   mycroft 	PAGE(sc, 0);
    320  1.39   mycroft 	if (sc->sc_chipset >= XI_CHIPSET_MOHAWK) {
    321   1.1  gmcgarry 		/* Disable interrupt (Linux does it). */
    322  1.48   mycroft 		bus_space_write_1(sc->sc_bst, sc->sc_bsh, CR, 0);
    323   1.1  gmcgarry 	}
    324   1.1  gmcgarry 
    325  1.48   mycroft 	esr = bus_space_read_1(sc->sc_bst, sc->sc_bsh, ESR);
    326  1.48   mycroft 	isr = bus_space_read_1(sc->sc_bst, sc->sc_bsh, ISR0);
    327  1.48   mycroft 	rsr = bus_space_read_1(sc->sc_bst, sc->sc_bsh, RSR);
    328  1.52     perry 
    329   1.1  gmcgarry 	/* Check to see if card has been ejected. */
    330   1.1  gmcgarry 	if (isr == 0xff) {
    331   1.1  gmcgarry #ifdef DIAGNOSTIC
    332  1.64    cegger 		printf("%s: interrupt for dead card\n", device_xname(&sc->sc_dev));
    333   1.1  gmcgarry #endif
    334   1.1  gmcgarry 		goto end;
    335   1.1  gmcgarry 	}
    336  1.39   mycroft 	DPRINTF(XID_INTR, ("xi: isr=%02x\n", isr));
    337   1.1  gmcgarry 
    338  1.39   mycroft 	PAGE(sc, 0x40);
    339   1.1  gmcgarry 	rx_status =
    340  1.48   mycroft 	    bus_space_read_1(sc->sc_bst, sc->sc_bsh, RXST0);
    341  1.48   mycroft 	bus_space_write_1(sc->sc_bst, sc->sc_bsh, RXST0, ~rx_status & 0xff);
    342   1.1  gmcgarry 	tx_status =
    343  1.48   mycroft 	    bus_space_read_1(sc->sc_bst, sc->sc_bsh, TXST0);
    344  1.23    martin 	tx_status |=
    345  1.48   mycroft 	    bus_space_read_1(sc->sc_bst, sc->sc_bsh, TXST1) << 8;
    346  1.48   mycroft 	bus_space_write_1(sc->sc_bst, sc->sc_bsh, TXST0, 0);
    347  1.48   mycroft 	bus_space_write_1(sc->sc_bst, sc->sc_bsh, TXST1, 0);
    348  1.39   mycroft 	DPRINTF(XID_INTR, ("xi: rx_status=%02x tx_status=%04x\n", rx_status,
    349  1.39   mycroft 	    tx_status));
    350   1.1  gmcgarry 
    351   1.1  gmcgarry 	PAGE(sc, 0);
    352   1.1  gmcgarry 	while (esr & FULL_PKT_RCV) {
    353   1.1  gmcgarry 		if (!(rsr & RSR_RX_OK))
    354   1.1  gmcgarry 			break;
    355   1.1  gmcgarry 
    356   1.1  gmcgarry 		/* Compare bytes read this interrupt to hard maximum. */
    357   1.1  gmcgarry 		if (recvcount > MAX_BYTES_INTR) {
    358   1.1  gmcgarry 			DPRINTF(XID_INTR,
    359   1.2  gmcgarry 			    ("xi: too many bytes this interrupt\n"));
    360   1.1  gmcgarry 			ifp->if_iqdrops++;
    361   1.1  gmcgarry 			/* Drop packet. */
    362  1.48   mycroft 			bus_space_write_2(sc->sc_bst, sc->sc_bsh, DO0,
    363  1.48   mycroft 			    DO_SKIP_RX_PKT);
    364   1.1  gmcgarry 		}
    365   1.1  gmcgarry 		tempint = xi_get(sc);	/* XXX doesn't check the error! */
    366   1.1  gmcgarry 		recvcount += tempint;
    367   1.1  gmcgarry 		ifp->if_ibytes += tempint;
    368  1.48   mycroft 		esr = bus_space_read_1(sc->sc_bst, sc->sc_bsh, ESR);
    369  1.48   mycroft 		rsr = bus_space_read_1(sc->sc_bst, sc->sc_bsh, RSR);
    370   1.1  gmcgarry 	}
    371  1.52     perry 
    372   1.1  gmcgarry 	/* Packet too long? */
    373   1.1  gmcgarry 	if (rsr & RSR_TOO_LONG) {
    374   1.1  gmcgarry 		ifp->if_ierrors++;
    375   1.2  gmcgarry 		DPRINTF(XID_INTR, ("xi: packet too long\n"));
    376   1.1  gmcgarry 	}
    377   1.1  gmcgarry 
    378   1.1  gmcgarry 	/* CRC error? */
    379   1.1  gmcgarry 	if (rsr & RSR_CRCERR) {
    380   1.1  gmcgarry 		ifp->if_ierrors++;
    381   1.2  gmcgarry 		DPRINTF(XID_INTR, ("xi: CRC error detected\n"));
    382   1.1  gmcgarry 	}
    383   1.1  gmcgarry 
    384   1.1  gmcgarry 	/* Alignment error? */
    385   1.1  gmcgarry 	if (rsr & RSR_ALIGNERR) {
    386   1.1  gmcgarry 		ifp->if_ierrors++;
    387   1.2  gmcgarry 		DPRINTF(XID_INTR, ("xi: alignment error detected\n"));
    388   1.1  gmcgarry 	}
    389   1.1  gmcgarry 
    390   1.1  gmcgarry 	/* Check for rx overrun. */
    391   1.1  gmcgarry 	if (rx_status & RX_OVERRUN) {
    392  1.23    martin 		ifp->if_ierrors++;
    393  1.48   mycroft 		bus_space_write_1(sc->sc_bst, sc->sc_bsh, CR, CLR_RX_OVERRUN);
    394   1.2  gmcgarry 		DPRINTF(XID_INTR, ("xi: overrun cleared\n"));
    395   1.1  gmcgarry 	}
    396  1.52     perry 
    397   1.1  gmcgarry 	/* Try to start more packets transmitting. */
    398   1.7   thorpej 	if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
    399   1.1  gmcgarry 		xi_start(ifp);
    400   1.1  gmcgarry 
    401   1.1  gmcgarry 	/* Detected excessive collisions? */
    402   1.1  gmcgarry 	if ((tx_status & EXCESSIVE_COLL) && ifp->if_opackets > 0) {
    403   1.2  gmcgarry 		DPRINTF(XID_INTR, ("xi: excessive collisions\n"));
    404  1.48   mycroft 		bus_space_write_1(sc->sc_bst, sc->sc_bsh, CR, RESTART_TX);
    405   1.1  gmcgarry 		ifp->if_oerrors++;
    406   1.1  gmcgarry 	}
    407  1.52     perry 
    408   1.1  gmcgarry 	if ((tx_status & TX_ABORT) && ifp->if_opackets > 0)
    409   1.1  gmcgarry 		ifp->if_oerrors++;
    410   1.1  gmcgarry 
    411  1.33   mycroft 	/* have handled the interrupt */
    412  1.52     perry #if NRND > 0
    413  1.52     perry 	rnd_add_uint32(&sc->sc_rnd_source, tx_status);
    414  1.33   mycroft #endif
    415  1.33   mycroft 
    416   1.1  gmcgarry end:
    417   1.1  gmcgarry 	/* Reenable interrupts. */
    418  1.45   mycroft 	PAGE(sc, 0);
    419  1.48   mycroft 	bus_space_write_1(sc->sc_bst, sc->sc_bsh, CR, ENABLE_INT);
    420  1.11  gmcgarry 
    421   1.1  gmcgarry 	return (1);
    422   1.1  gmcgarry }
    423   1.1  gmcgarry 
    424   1.1  gmcgarry /*
    425   1.1  gmcgarry  * Pull a packet from the card into an mbuf chain.
    426   1.1  gmcgarry  */
    427  1.39   mycroft STATIC u_int16_t
    428  1.66       dsl xi_get(struct xi_softc *sc)
    429   1.1  gmcgarry {
    430   1.1  gmcgarry 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    431   1.1  gmcgarry 	struct mbuf *top, **mp, *m;
    432   1.1  gmcgarry 	u_int16_t pktlen, len, recvcount = 0;
    433   1.1  gmcgarry 	u_int8_t *data;
    434  1.52     perry 
    435   1.2  gmcgarry 	DPRINTF(XID_CONFIG, ("xi_get()\n"));
    436   1.1  gmcgarry 
    437   1.1  gmcgarry 	PAGE(sc, 0);
    438   1.1  gmcgarry 	pktlen =
    439  1.48   mycroft 	    bus_space_read_2(sc->sc_bst, sc->sc_bsh, RBC0) & RBC_COUNT_MASK;
    440   1.1  gmcgarry 
    441   1.1  gmcgarry 	DPRINTF(XID_CONFIG, ("xi_get: pktlen=%d\n", pktlen));
    442   1.1  gmcgarry 
    443   1.1  gmcgarry 	if (pktlen == 0) {
    444   1.1  gmcgarry 		/*
    445   1.1  gmcgarry 		 * XXX At least one CE2 sets RBC0 == 0 occasionally, and only
    446   1.1  gmcgarry 		 * when MPE is set.  It is not known why.
    447   1.1  gmcgarry 		 */
    448   1.1  gmcgarry 		return (0);
    449   1.1  gmcgarry 	}
    450   1.1  gmcgarry 
    451   1.1  gmcgarry 	/* XXX should this be incremented now ? */
    452   1.1  gmcgarry 	recvcount += pktlen;
    453   1.1  gmcgarry 
    454   1.1  gmcgarry 	MGETHDR(m, M_DONTWAIT, MT_DATA);
    455  1.56  christos 	if (m == NULL)
    456   1.1  gmcgarry 		return (recvcount);
    457   1.1  gmcgarry 	m->m_pkthdr.rcvif = ifp;
    458   1.1  gmcgarry 	m->m_pkthdr.len = pktlen;
    459   1.1  gmcgarry 	len = MHLEN;
    460  1.56  christos 	top = NULL;
    461   1.1  gmcgarry 	mp = &top;
    462  1.52     perry 
    463   1.1  gmcgarry 	while (pktlen > 0) {
    464   1.1  gmcgarry 		if (top) {
    465   1.1  gmcgarry 			MGET(m, M_DONTWAIT, MT_DATA);
    466  1.56  christos 			if (m == NULL) {
    467   1.1  gmcgarry 				m_freem(top);
    468   1.1  gmcgarry 				return (recvcount);
    469   1.1  gmcgarry 			}
    470   1.1  gmcgarry 			len = MLEN;
    471   1.1  gmcgarry 		}
    472   1.1  gmcgarry 		if (pktlen >= MINCLSIZE) {
    473   1.1  gmcgarry 			MCLGET(m, M_DONTWAIT);
    474   1.1  gmcgarry 			if (!(m->m_flags & M_EXT)) {
    475   1.1  gmcgarry 				m_freem(m);
    476   1.1  gmcgarry 				m_freem(top);
    477   1.1  gmcgarry 				return (recvcount);
    478   1.1  gmcgarry 			}
    479   1.1  gmcgarry 			len = MCLBYTES;
    480   1.1  gmcgarry 		}
    481  1.56  christos 		if (top == NULL) {
    482  1.60  christos 			char *newdata = (char *)ALIGN(m->m_data +
    483   1.1  gmcgarry 			    sizeof(struct ether_header)) -
    484   1.1  gmcgarry 			    sizeof(struct ether_header);
    485   1.1  gmcgarry 			len -= newdata - m->m_data;
    486   1.1  gmcgarry 			m->m_data = newdata;
    487   1.1  gmcgarry 		}
    488   1.1  gmcgarry 		len = min(pktlen, len);
    489   1.1  gmcgarry 		data = mtod(m, u_int8_t *);
    490   1.1  gmcgarry 		if (len > 1) {
    491   1.1  gmcgarry 		        len &= ~1;
    492  1.48   mycroft 			bus_space_read_multi_2(sc->sc_bst, sc->sc_bsh, EDP,
    493  1.48   mycroft 			    (u_int16_t *)data, len>>1);
    494   1.1  gmcgarry 		} else
    495  1.48   mycroft 			*data = bus_space_read_1(sc->sc_bst, sc->sc_bsh, EDP);
    496   1.1  gmcgarry 		m->m_len = len;
    497   1.1  gmcgarry 		pktlen -= len;
    498   1.1  gmcgarry 		*mp = m;
    499   1.1  gmcgarry 		mp = &m->m_next;
    500   1.1  gmcgarry 	}
    501   1.1  gmcgarry 
    502   1.1  gmcgarry 	/* Skip Rx packet. */
    503  1.48   mycroft 	bus_space_write_2(sc->sc_bst, sc->sc_bsh, DO0, DO_SKIP_RX_PKT);
    504  1.50   thorpej 
    505  1.56  christos 	if (top == NULL)
    506  1.56  christos 		return recvcount;
    507  1.56  christos 
    508  1.50   thorpej 	/* Trim the CRC off the end of the packet. */
    509  1.50   thorpej 	m_adj(top, -ETHER_CRC_LEN);
    510  1.50   thorpej 
    511   1.1  gmcgarry 	ifp->if_ipackets++;
    512  1.52     perry 
    513   1.1  gmcgarry #if NBPFILTER > 0
    514   1.1  gmcgarry 	if (ifp->if_bpf)
    515   1.1  gmcgarry 		bpf_mtap(ifp->if_bpf, top);
    516   1.1  gmcgarry #endif
    517  1.52     perry 
    518   1.1  gmcgarry 	(*ifp->if_input)(ifp, top);
    519   1.1  gmcgarry 	return (recvcount);
    520   1.1  gmcgarry }
    521   1.1  gmcgarry 
    522   1.1  gmcgarry /*
    523   1.1  gmcgarry  * Serial management for the MII.
    524   1.1  gmcgarry  * The DELAY's below stem from the fact that the maximum frequency
    525   1.1  gmcgarry  * acceptable on the MDC pin is 2.5 MHz and fast processors can easily
    526   1.1  gmcgarry  * go much faster than that.
    527   1.1  gmcgarry  */
    528   1.1  gmcgarry 
    529   1.1  gmcgarry /* Let the MII serial management be idle for one period. */
    530  1.51     perry static INLINE void xi_mdi_idle(struct xi_softc *);
    531   1.1  gmcgarry static INLINE void
    532  1.66       dsl xi_mdi_idle(struct xi_softc *sc)
    533   1.1  gmcgarry {
    534   1.1  gmcgarry 	bus_space_tag_t bst = sc->sc_bst;
    535   1.1  gmcgarry 	bus_space_handle_t bsh = sc->sc_bsh;
    536   1.1  gmcgarry 
    537   1.1  gmcgarry 	/* Drive MDC low... */
    538  1.48   mycroft 	bus_space_write_1(bst, bsh, GP2, MDC_LOW);
    539   1.1  gmcgarry 	DELAY(1);
    540   1.1  gmcgarry 
    541   1.1  gmcgarry 	/* and high again. */
    542  1.48   mycroft 	bus_space_write_1(bst, bsh, GP2, MDC_HIGH);
    543   1.1  gmcgarry 	DELAY(1);
    544   1.1  gmcgarry }
    545   1.1  gmcgarry 
    546   1.1  gmcgarry /* Pulse out one bit of data. */
    547  1.51     perry static INLINE void xi_mdi_pulse(struct xi_softc *, int);
    548   1.1  gmcgarry static INLINE void
    549  1.66       dsl xi_mdi_pulse(struct xi_softc *sc, int data)
    550   1.1  gmcgarry {
    551   1.1  gmcgarry 	bus_space_tag_t bst = sc->sc_bst;
    552   1.1  gmcgarry 	bus_space_handle_t bsh = sc->sc_bsh;
    553   1.1  gmcgarry 	u_int8_t bit = data ? MDIO_HIGH : MDIO_LOW;
    554   1.1  gmcgarry 
    555   1.1  gmcgarry 	/* First latch the data bit MDIO with clock bit MDC low...*/
    556  1.48   mycroft 	bus_space_write_1(bst, bsh, GP2, bit | MDC_LOW);
    557   1.1  gmcgarry 	DELAY(1);
    558   1.1  gmcgarry 
    559   1.1  gmcgarry 	/* then raise the clock again, preserving the data bit. */
    560  1.48   mycroft 	bus_space_write_1(bst, bsh, GP2, bit | MDC_HIGH);
    561   1.1  gmcgarry 	DELAY(1);
    562   1.1  gmcgarry }
    563   1.1  gmcgarry 
    564   1.1  gmcgarry /* Probe one bit of data. */
    565  1.51     perry static INLINE int xi_mdi_probe(struct xi_softc *sc);
    566   1.1  gmcgarry static INLINE int
    567  1.66       dsl xi_mdi_probe(struct xi_softc *sc)
    568   1.1  gmcgarry {
    569   1.1  gmcgarry 	bus_space_tag_t bst = sc->sc_bst;
    570   1.1  gmcgarry 	bus_space_handle_t bsh = sc->sc_bsh;
    571   1.1  gmcgarry 	u_int8_t x;
    572   1.1  gmcgarry 
    573   1.1  gmcgarry 	/* Pull clock bit MDCK low... */
    574  1.48   mycroft 	bus_space_write_1(bst, bsh, GP2, MDC_LOW);
    575   1.1  gmcgarry 	DELAY(1);
    576   1.1  gmcgarry 
    577   1.1  gmcgarry 	/* Read data and drive clock high again. */
    578  1.48   mycroft 	x = bus_space_read_1(bst, bsh, GP2);
    579  1.48   mycroft 	bus_space_write_1(bst, bsh, GP2, MDC_HIGH);
    580   1.1  gmcgarry 	DELAY(1);
    581   1.1  gmcgarry 
    582  1.39   mycroft 	return (x & MDIO);
    583   1.1  gmcgarry }
    584   1.1  gmcgarry 
    585   1.1  gmcgarry /* Pulse out a sequence of data bits. */
    586  1.51     perry static INLINE void xi_mdi_pulse_bits(struct xi_softc *, u_int32_t, int);
    587   1.1  gmcgarry static INLINE void
    588  1.66       dsl xi_mdi_pulse_bits(struct xi_softc *sc, u_int32_t data, int len)
    589   1.1  gmcgarry {
    590   1.1  gmcgarry 	u_int32_t mask;
    591   1.1  gmcgarry 
    592   1.1  gmcgarry 	for (mask = 1 << (len - 1); mask; mask >>= 1)
    593   1.1  gmcgarry 		xi_mdi_pulse(sc, data & mask);
    594   1.1  gmcgarry }
    595   1.1  gmcgarry 
    596   1.1  gmcgarry /* Read a PHY register. */
    597  1.39   mycroft STATIC int
    598  1.67    cegger xi_mdi_read(device_t self, int phy, int reg)
    599   1.1  gmcgarry {
    600   1.1  gmcgarry 	struct xi_softc *sc = (struct xi_softc *)self;
    601   1.1  gmcgarry 	int i;
    602   1.1  gmcgarry 	u_int32_t mask;
    603   1.1  gmcgarry 	u_int32_t data = 0;
    604   1.1  gmcgarry 
    605   1.1  gmcgarry 	PAGE(sc, 2);
    606   1.1  gmcgarry 	for (i = 0; i < 32; i++)	/* Synchronize. */
    607   1.1  gmcgarry 		xi_mdi_pulse(sc, 1);
    608   1.1  gmcgarry 	xi_mdi_pulse_bits(sc, 0x06, 4); /* Start + Read opcode */
    609   1.1  gmcgarry 	xi_mdi_pulse_bits(sc, phy, 5);	/* PHY address */
    610   1.1  gmcgarry 	xi_mdi_pulse_bits(sc, reg, 5);	/* PHY register */
    611   1.1  gmcgarry 	xi_mdi_idle(sc);		/* Turn around. */
    612   1.1  gmcgarry 	xi_mdi_probe(sc);		/* Drop initial zero bit. */
    613   1.1  gmcgarry 
    614   1.1  gmcgarry 	for (mask = 1 << 15; mask; mask >>= 1) {
    615   1.1  gmcgarry 		if (xi_mdi_probe(sc))
    616   1.1  gmcgarry 			data |= mask;
    617   1.1  gmcgarry 	}
    618   1.1  gmcgarry 	xi_mdi_idle(sc);
    619   1.1  gmcgarry 
    620   1.1  gmcgarry 	DPRINTF(XID_MII,
    621   1.1  gmcgarry 	    ("xi_mdi_read: phy %d reg %d -> %x\n", phy, reg, data));
    622   1.1  gmcgarry 
    623   1.1  gmcgarry 	return (data);
    624   1.1  gmcgarry }
    625   1.1  gmcgarry 
    626   1.1  gmcgarry /* Write a PHY register. */
    627  1.39   mycroft STATIC void
    628  1.67    cegger xi_mdi_write(device_t self, int phy, int reg, int value)
    629   1.1  gmcgarry {
    630   1.1  gmcgarry 	struct xi_softc *sc = (struct xi_softc *)self;
    631   1.1  gmcgarry 	int i;
    632   1.1  gmcgarry 
    633   1.1  gmcgarry 	PAGE(sc, 2);
    634   1.1  gmcgarry 	for (i = 0; i < 32; i++)	/* Synchronize. */
    635   1.1  gmcgarry 		xi_mdi_pulse(sc, 1);
    636   1.1  gmcgarry 	xi_mdi_pulse_bits(sc, 0x05, 4); /* Start + Write opcode */
    637   1.1  gmcgarry 	xi_mdi_pulse_bits(sc, phy, 5);	/* PHY address */
    638   1.1  gmcgarry 	xi_mdi_pulse_bits(sc, reg, 5);	/* PHY register */
    639   1.1  gmcgarry 	xi_mdi_pulse_bits(sc, 0x02, 2); /* Turn around. */
    640   1.1  gmcgarry 	xi_mdi_pulse_bits(sc, value, 16);	/* Write the data */
    641   1.1  gmcgarry 	xi_mdi_idle(sc);		/* Idle away. */
    642   1.1  gmcgarry 
    643   1.1  gmcgarry 	DPRINTF(XID_MII,
    644   1.1  gmcgarry 	    ("xi_mdi_write: phy %d reg %d val %x\n", phy, reg, value));
    645   1.1  gmcgarry }
    646   1.1  gmcgarry 
    647  1.39   mycroft STATIC void
    648  1.67    cegger xi_statchg(device_t self)
    649   1.1  gmcgarry {
    650   1.1  gmcgarry 	/* XXX Update ifp->if_baudrate */
    651   1.1  gmcgarry }
    652   1.1  gmcgarry 
    653   1.1  gmcgarry /*
    654   1.1  gmcgarry  * Change media according to request.
    655   1.1  gmcgarry  */
    656  1.39   mycroft STATIC int
    657  1.66       dsl xi_mediachange(struct ifnet *ifp)
    658   1.1  gmcgarry {
    659  1.42   mycroft 	int s;
    660  1.42   mycroft 
    661   1.2  gmcgarry 	DPRINTF(XID_CONFIG, ("xi_mediachange()\n"));
    662   1.1  gmcgarry 
    663  1.42   mycroft 	if (ifp->if_flags & IFF_UP) {
    664  1.42   mycroft 		s = splnet();
    665   1.1  gmcgarry 		xi_init(ifp->if_softc);
    666  1.42   mycroft 		splx(s);
    667  1.42   mycroft 	}
    668   1.1  gmcgarry 	return (0);
    669   1.1  gmcgarry }
    670   1.1  gmcgarry 
    671  1.39   mycroft STATIC void
    672  1.66       dsl xi_reset(struct xi_softc *sc)
    673   1.1  gmcgarry {
    674   1.1  gmcgarry 	int s;
    675   1.1  gmcgarry 
    676   1.2  gmcgarry 	DPRINTF(XID_CONFIG, ("xi_reset()\n"));
    677   1.1  gmcgarry 
    678   1.1  gmcgarry 	s = splnet();
    679   1.1  gmcgarry 	xi_stop(sc);
    680   1.1  gmcgarry 	xi_init(sc);
    681   1.1  gmcgarry 	splx(s);
    682   1.1  gmcgarry }
    683   1.1  gmcgarry 
    684  1.39   mycroft STATIC void
    685  1.66       dsl xi_watchdog(struct ifnet *ifp)
    686   1.1  gmcgarry {
    687   1.1  gmcgarry 	struct xi_softc *sc = ifp->if_softc;
    688   1.1  gmcgarry 
    689  1.64    cegger 	printf("%s: device timeout\n", device_xname(&sc->sc_dev));
    690   1.1  gmcgarry 	++ifp->if_oerrors;
    691   1.1  gmcgarry 
    692   1.1  gmcgarry 	xi_reset(sc);
    693   1.1  gmcgarry }
    694   1.1  gmcgarry 
    695  1.39   mycroft STATIC void
    696  1.66       dsl xi_stop(register struct xi_softc *sc)
    697   1.1  gmcgarry {
    698  1.39   mycroft 	bus_space_tag_t bst = sc->sc_bst;
    699  1.39   mycroft 	bus_space_handle_t bsh = sc->sc_bsh;
    700  1.39   mycroft 
    701   1.2  gmcgarry 	DPRINTF(XID_CONFIG, ("xi_stop()\n"));
    702   1.1  gmcgarry 
    703  1.44   mycroft 	PAGE(sc, 0x40);
    704  1.48   mycroft 	bus_space_write_1(bst, bsh, CMD0, DISABLE_RX);
    705  1.44   mycroft 
    706   1.1  gmcgarry 	/* Disable interrupts. */
    707   1.1  gmcgarry 	PAGE(sc, 0);
    708  1.48   mycroft 	bus_space_write_1(bst, bsh, CR, 0);
    709   1.1  gmcgarry 
    710   1.1  gmcgarry 	PAGE(sc, 1);
    711  1.48   mycroft 	bus_space_write_1(bst, bsh, IMR0, 0);
    712  1.52     perry 
    713   1.1  gmcgarry 	/* Cancel watchdog timer. */
    714   1.1  gmcgarry 	sc->sc_ethercom.ec_if.if_timer = 0;
    715   1.1  gmcgarry }
    716   1.1  gmcgarry 
    717  1.42   mycroft STATIC int
    718  1.66       dsl xi_enable(struct xi_softc *sc)
    719  1.42   mycroft {
    720  1.42   mycroft 	int error;
    721  1.42   mycroft 
    722  1.42   mycroft 	if (!sc->sc_enabled) {
    723  1.42   mycroft 		error = (*sc->sc_enable)(sc);
    724  1.42   mycroft 		if (error)
    725  1.42   mycroft 			return (error);
    726  1.42   mycroft 		sc->sc_enabled = 1;
    727  1.42   mycroft 		xi_full_reset(sc);
    728  1.42   mycroft 	}
    729  1.42   mycroft 	return (0);
    730  1.42   mycroft }
    731  1.42   mycroft 
    732  1.42   mycroft STATIC void
    733  1.66       dsl xi_disable(struct xi_softc *sc)
    734  1.42   mycroft {
    735  1.42   mycroft 
    736  1.42   mycroft 	if (sc->sc_enabled) {
    737  1.42   mycroft 		sc->sc_enabled = 0;
    738  1.42   mycroft 		(*sc->sc_disable)(sc);
    739  1.42   mycroft 	}
    740  1.42   mycroft }
    741  1.42   mycroft 
    742  1.39   mycroft STATIC void
    743  1.66       dsl xi_init(struct xi_softc *sc)
    744   1.1  gmcgarry {
    745   1.1  gmcgarry 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    746  1.39   mycroft 	bus_space_tag_t bst = sc->sc_bst;
    747  1.39   mycroft 	bus_space_handle_t bsh = sc->sc_bsh;
    748   1.1  gmcgarry 
    749   1.2  gmcgarry 	DPRINTF(XID_CONFIG, ("xi_init()\n"));
    750   1.1  gmcgarry 
    751  1.39   mycroft 	/* Setup the ethernet interrupt mask. */
    752  1.39   mycroft 	PAGE(sc, 1);
    753  1.48   mycroft 	bus_space_write_1(bst, bsh, IMR0,
    754  1.39   mycroft 	    ISR_TX_OFLOW | ISR_PKT_TX | ISR_MAC_INT | /* ISR_RX_EARLY | */
    755  1.39   mycroft 	    ISR_RX_FULL | ISR_RX_PKT_REJ | ISR_FORCED_INT);
    756  1.39   mycroft 	if (sc->sc_chipset < XI_CHIPSET_DINGO) {
    757  1.39   mycroft 		/* XXX What is this?  Not for Dingo at least. */
    758  1.39   mycroft 		/* Unmask TX underrun detection */
    759  1.48   mycroft 		bus_space_write_1(bst, bsh, IMR1, 1);
    760  1.39   mycroft 	}
    761  1.39   mycroft 
    762  1.39   mycroft 	/* Enable interrupts. */
    763  1.39   mycroft 	PAGE(sc, 0);
    764  1.48   mycroft 	bus_space_write_1(bst, bsh, CR, ENABLE_INT);
    765  1.39   mycroft 
    766  1.44   mycroft 	xi_set_address(sc);
    767  1.44   mycroft 
    768  1.44   mycroft 	PAGE(sc, 0x40);
    769  1.48   mycroft 	bus_space_write_1(bst, bsh, CMD0, ENABLE_RX | ONLINE);
    770  1.44   mycroft 
    771  1.44   mycroft 	PAGE(sc, 0);
    772  1.44   mycroft 
    773   1.1  gmcgarry 	/* Set current media. */
    774   1.1  gmcgarry 	mii_mediachg(&sc->sc_mii);
    775   1.1  gmcgarry 
    776   1.1  gmcgarry 	ifp->if_flags |= IFF_RUNNING;
    777   1.1  gmcgarry 	ifp->if_flags &= ~IFF_OACTIVE;
    778  1.39   mycroft 
    779  1.42   mycroft 	xi_start(ifp);
    780   1.1  gmcgarry }
    781   1.1  gmcgarry 
    782   1.1  gmcgarry /*
    783   1.1  gmcgarry  * Start outputting on the interface.
    784   1.1  gmcgarry  * Always called as splnet().
    785   1.1  gmcgarry  */
    786  1.39   mycroft STATIC void
    787  1.66       dsl xi_start(struct ifnet *ifp)
    788   1.1  gmcgarry {
    789   1.1  gmcgarry 	struct xi_softc *sc = ifp->if_softc;
    790   1.1  gmcgarry 	bus_space_tag_t bst = sc->sc_bst;
    791   1.1  gmcgarry 	bus_space_handle_t bsh = sc->sc_bsh;
    792   1.1  gmcgarry 	unsigned int s, len, pad = 0;
    793   1.1  gmcgarry 	struct mbuf *m0, *m;
    794   1.1  gmcgarry 	u_int16_t space;
    795   1.1  gmcgarry 
    796   1.2  gmcgarry 	DPRINTF(XID_CONFIG, ("xi_start()\n"));
    797   1.1  gmcgarry 
    798   1.1  gmcgarry 	/* Don't transmit if interface is busy or not running. */
    799   1.1  gmcgarry 	if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING) {
    800   1.2  gmcgarry 		DPRINTF(XID_CONFIG, ("xi: interface busy or not running\n"));
    801   1.1  gmcgarry 		return;
    802   1.1  gmcgarry 	}
    803   1.1  gmcgarry 
    804   1.1  gmcgarry 	/* Peek at the next packet. */
    805   1.7   thorpej 	IFQ_POLL(&ifp->if_snd, m0);
    806   1.1  gmcgarry 	if (m0 == 0)
    807   1.1  gmcgarry 		return;
    808   1.1  gmcgarry 
    809   1.1  gmcgarry 	/* We need to use m->m_pkthdr.len, so require the header. */
    810   1.1  gmcgarry 	if (!(m0->m_flags & M_PKTHDR))
    811   1.1  gmcgarry 		panic("xi_start: no header mbuf");
    812   1.1  gmcgarry 
    813   1.1  gmcgarry 	len = m0->m_pkthdr.len;
    814   1.1  gmcgarry 
    815  1.39   mycroft #if 1
    816   1.1  gmcgarry 	/* Pad to ETHER_MIN_LEN - ETHER_CRC_LEN. */
    817   1.1  gmcgarry 	if (len < ETHER_MIN_LEN - ETHER_CRC_LEN)
    818   1.1  gmcgarry 		pad = ETHER_MIN_LEN - ETHER_CRC_LEN - len;
    819  1.39   mycroft #else
    820  1.39   mycroft 	pad = 0;
    821  1.39   mycroft #endif
    822   1.1  gmcgarry 
    823   1.1  gmcgarry 	PAGE(sc, 0);
    824  1.39   mycroft 
    825  1.48   mycroft 	bus_space_write_2(bst, bsh, TRS, (u_int16_t)len + pad + 2);
    826  1.48   mycroft 	space = bus_space_read_2(bst, bsh, TSO) & 0x7fff;
    827   1.1  gmcgarry 	if (len + pad + 2 > space) {
    828   1.1  gmcgarry 		DPRINTF(XID_FIFO,
    829   1.2  gmcgarry 		    ("xi: not enough space in output FIFO (%d > %d)\n",
    830   1.2  gmcgarry 		    len + pad + 2, space));
    831   1.1  gmcgarry 		return;
    832   1.1  gmcgarry 	}
    833   1.1  gmcgarry 
    834   1.7   thorpej 	IFQ_DEQUEUE(&ifp->if_snd, m0);
    835   1.1  gmcgarry 
    836   1.1  gmcgarry #if NBPFILTER > 0
    837   1.1  gmcgarry 	if (ifp->if_bpf)
    838   1.1  gmcgarry 		bpf_mtap(ifp->if_bpf, m0);
    839   1.1  gmcgarry #endif
    840   1.1  gmcgarry 
    841   1.1  gmcgarry 	/*
    842   1.1  gmcgarry 	 * Do the output at splhigh() so that an interrupt from another device
    843   1.1  gmcgarry 	 * won't cause a FIFO underrun.
    844   1.1  gmcgarry 	 */
    845   1.1  gmcgarry 	s = splhigh();
    846   1.1  gmcgarry 
    847  1.48   mycroft 	bus_space_write_2(bst, bsh, EDP, (u_int16_t)len + pad);
    848   1.1  gmcgarry 	for (m = m0; m; ) {
    849   1.1  gmcgarry 		if (m->m_len > 1)
    850  1.48   mycroft 			bus_space_write_multi_2(bst, bsh, EDP,
    851  1.21  takemura 			    mtod(m, u_int16_t *), m->m_len>>1);
    852  1.39   mycroft 		if (m->m_len & 1) {
    853  1.39   mycroft 			DPRINTF(XID_CONFIG, ("xi: XXX odd!\n"));
    854  1.48   mycroft 			bus_space_write_1(bst, bsh, EDP,
    855   1.1  gmcgarry 			    *(mtod(m, u_int8_t *) + m->m_len - 1));
    856  1.39   mycroft 		}
    857   1.1  gmcgarry 		MFREE(m, m0);
    858   1.1  gmcgarry 		m = m0;
    859   1.1  gmcgarry 	}
    860  1.39   mycroft 	DPRINTF(XID_CONFIG, ("xi: len=%d pad=%d total=%d\n", len, pad, len+pad+4));
    861  1.39   mycroft 	if (sc->sc_chipset >= XI_CHIPSET_MOHAWK)
    862  1.48   mycroft 		bus_space_write_1(bst, bsh, CR, TX_PKT | ENABLE_INT);
    863   1.1  gmcgarry 	else {
    864   1.1  gmcgarry 		for (; pad > 1; pad -= 2)
    865  1.48   mycroft 			bus_space_write_2(bst, bsh, EDP, 0);
    866   1.1  gmcgarry 		if (pad == 1)
    867  1.48   mycroft 			bus_space_write_1(bst, bsh, EDP, 0);
    868   1.1  gmcgarry 	}
    869   1.1  gmcgarry 
    870   1.1  gmcgarry 	splx(s);
    871   1.1  gmcgarry 
    872   1.1  gmcgarry 	ifp->if_timer = 5;
    873   1.1  gmcgarry 	++ifp->if_opackets;
    874   1.1  gmcgarry }
    875   1.1  gmcgarry 
    876  1.39   mycroft STATIC int
    877  1.66       dsl xi_ether_ioctl(struct ifnet *ifp, u_long cmd, void *data)
    878   1.1  gmcgarry {
    879   1.1  gmcgarry 	struct ifaddr *ifa = (struct ifaddr *)data;
    880   1.1  gmcgarry 	struct xi_softc *sc = ifp->if_softc;
    881  1.42   mycroft 	int error;
    882   1.1  gmcgarry 
    883   1.2  gmcgarry 	DPRINTF(XID_CONFIG, ("xi_ether_ioctl()\n"));
    884   1.1  gmcgarry 
    885   1.1  gmcgarry 	switch (cmd) {
    886  1.65    dyoung 	case SIOCINITIFADDR:
    887  1.42   mycroft 		if ((error = xi_enable(sc)) != 0)
    888  1.42   mycroft 			break;
    889  1.42   mycroft 
    890   1.1  gmcgarry 		ifp->if_flags |= IFF_UP;
    891   1.1  gmcgarry 
    892  1.65    dyoung 		xi_init(sc);
    893   1.1  gmcgarry 		switch (ifa->ifa_addr->sa_family) {
    894   1.1  gmcgarry #ifdef INET
    895   1.1  gmcgarry 		case AF_INET:
    896   1.1  gmcgarry 			arp_ifinit(ifp, ifa);
    897   1.1  gmcgarry 			break;
    898   1.1  gmcgarry #endif	/* INET */
    899   1.1  gmcgarry 
    900   1.1  gmcgarry 
    901   1.1  gmcgarry 		default:
    902   1.1  gmcgarry 			break;
    903   1.1  gmcgarry 		}
    904   1.1  gmcgarry 		break;
    905   1.1  gmcgarry 
    906   1.1  gmcgarry 	default:
    907   1.1  gmcgarry 		return (EINVAL);
    908   1.1  gmcgarry 	}
    909   1.1  gmcgarry 
    910   1.1  gmcgarry 	return (0);
    911   1.1  gmcgarry }
    912   1.1  gmcgarry 
    913  1.39   mycroft STATIC int
    914  1.65    dyoung xi_ioctl(struct ifnet *ifp, u_long cmd, void *data)
    915   1.1  gmcgarry {
    916  1.39   mycroft 	struct xi_softc *sc = ifp->if_softc;
    917   1.1  gmcgarry 	int s, error = 0;
    918   1.1  gmcgarry 
    919   1.2  gmcgarry 	DPRINTF(XID_CONFIG, ("xi_ioctl()\n"));
    920   1.1  gmcgarry 
    921   1.8   thorpej 	s = splnet();
    922   1.1  gmcgarry 
    923  1.39   mycroft 	switch (cmd) {
    924  1.65    dyoung 	case SIOCINITIFADDR:
    925  1.39   mycroft 		error = xi_ether_ioctl(ifp, cmd, data);
    926   1.1  gmcgarry 		break;
    927   1.1  gmcgarry 
    928   1.1  gmcgarry 	case SIOCSIFFLAGS:
    929  1.65    dyoung 		if ((error = ifioctl_common(ifp, cmd, data)) != 0)
    930  1.65    dyoung 			break;
    931  1.65    dyoung 		/* XXX re-use ether_ioctl() */
    932  1.65    dyoung 		switch (ifp->if_flags & (IFF_UP|IFF_RUNNING)) {
    933  1.65    dyoung 		case IFF_RUNNING:
    934  1.39   mycroft 			/*
    935  1.39   mycroft 			 * If interface is marked down and it is running,
    936  1.39   mycroft 			 * stop it.
    937  1.39   mycroft 			 */
    938  1.39   mycroft 			xi_stop(sc);
    939  1.39   mycroft 			ifp->if_flags &= ~IFF_RUNNING;
    940  1.42   mycroft 			xi_disable(sc);
    941  1.65    dyoung 			break;
    942  1.65    dyoung 		case IFF_UP:
    943  1.39   mycroft 			/*
    944  1.39   mycroft 			 * If interface is marked up and it is stopped,
    945  1.39   mycroft 			 * start it.
    946  1.39   mycroft 			 */
    947  1.42   mycroft 			if ((error = xi_enable(sc)) != 0)
    948  1.42   mycroft 				break;
    949   1.1  gmcgarry 			xi_init(sc);
    950  1.65    dyoung 			break;
    951  1.65    dyoung 		case IFF_UP|IFF_RUNNING:
    952  1.39   mycroft 			/*
    953  1.39   mycroft 			 * Reset the interface to pick up changes in any
    954  1.39   mycroft 			 * other flags that affect hardware registers.
    955  1.39   mycroft 			 */
    956  1.42   mycroft 			xi_set_address(sc);
    957  1.65    dyoung 			break;
    958  1.65    dyoung 		case 0:
    959  1.65    dyoung 			break;
    960   1.1  gmcgarry 		}
    961   1.1  gmcgarry 		break;
    962   1.1  gmcgarry 
    963   1.1  gmcgarry 	case SIOCADDMULTI:
    964   1.1  gmcgarry 	case SIOCDELMULTI:
    965  1.39   mycroft 		if (sc->sc_enabled == 0) {
    966  1.39   mycroft 			error = EIO;
    967  1.39   mycroft 			break;
    968  1.39   mycroft 		}
    969  1.63    dyoung 		/*FALLTHROUGH*/
    970  1.63    dyoung 	case SIOCSIFMEDIA:
    971  1.63    dyoung 	case SIOCGIFMEDIA:
    972  1.62    dyoung 		if ((error = ether_ioctl(ifp, cmd, data)) == ENETRESET) {
    973   1.1  gmcgarry 			/*
    974   1.1  gmcgarry 			 * Multicast list has changed; set the hardware
    975   1.1  gmcgarry 			 * filter accordingly.
    976   1.1  gmcgarry 			 */
    977  1.49   thorpej 			if (ifp->if_flags & IFF_RUNNING)
    978  1.49   thorpej 				xi_set_address(sc);
    979   1.1  gmcgarry 			error = 0;
    980   1.1  gmcgarry 		}
    981   1.1  gmcgarry 		break;
    982   1.1  gmcgarry 
    983   1.1  gmcgarry 	default:
    984  1.65    dyoung 		error = ether_ioctl(ifp, cmd, data);
    985  1.39   mycroft 		break;
    986   1.1  gmcgarry 	}
    987  1.39   mycroft 
    988   1.1  gmcgarry 	splx(s);
    989   1.1  gmcgarry 	return (error);
    990   1.1  gmcgarry }
    991   1.1  gmcgarry 
    992  1.39   mycroft STATIC void
    993  1.66       dsl xi_set_address(struct xi_softc *sc)
    994   1.1  gmcgarry {
    995   1.1  gmcgarry 	bus_space_tag_t bst = sc->sc_bst;
    996   1.1  gmcgarry 	bus_space_handle_t bsh = sc->sc_bsh;
    997   1.1  gmcgarry 	struct ethercom *ether = &sc->sc_ethercom;
    998  1.11  gmcgarry 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    999  1.11  gmcgarry 	struct ether_multistep step;
   1000   1.1  gmcgarry 	struct ether_multi *enm;
   1001  1.39   mycroft 	int page, num;
   1002  1.11  gmcgarry 	int i;
   1003  1.39   mycroft 	u_int8_t x;
   1004  1.61    dyoung 	const u_int8_t *enaddr;
   1005  1.39   mycroft 	u_int8_t indaddr[64];
   1006   1.1  gmcgarry 
   1007   1.2  gmcgarry 	DPRINTF(XID_CONFIG, ("xi_set_address()\n"));
   1008   1.1  gmcgarry 
   1009  1.61    dyoung 	enaddr = (const u_int8_t *)CLLADDR(ifp->if_sadl);
   1010  1.39   mycroft 	if (sc->sc_chipset >= XI_CHIPSET_MOHAWK)
   1011  1.39   mycroft 		for (i = 0; i < 6; i++)
   1012  1.39   mycroft 			indaddr[i] = enaddr[5 - i];
   1013  1.39   mycroft 	else
   1014  1.39   mycroft 		for (i = 0; i < 6; i++)
   1015  1.39   mycroft 			indaddr[i] = enaddr[i];
   1016  1.39   mycroft 	num = 1;
   1017  1.39   mycroft 
   1018  1.39   mycroft 	if (ether->ec_multicnt > 9) {
   1019  1.39   mycroft 		ifp->if_flags |= IFF_ALLMULTI;
   1020  1.39   mycroft 		goto done;
   1021   1.1  gmcgarry 	}
   1022  1.11  gmcgarry 
   1023  1.39   mycroft 	ETHER_FIRST_MULTI(step, ether, enm);
   1024  1.39   mycroft 	for (; enm; num++) {
   1025  1.39   mycroft 		if (memcmp(enm->enm_addrlo, enm->enm_addrhi,
   1026  1.39   mycroft 		    sizeof(enm->enm_addrlo)) != 0) {
   1027  1.39   mycroft 			/*
   1028  1.39   mycroft 			 * The multicast address is really a range;
   1029  1.39   mycroft 			 * it's easier just to accept all multicasts.
   1030  1.39   mycroft 			 * XXX should we be setting IFF_ALLMULTI here?
   1031  1.39   mycroft 			 */
   1032  1.39   mycroft 			ifp->if_flags |= IFF_ALLMULTI;
   1033  1.39   mycroft 			goto done;
   1034   1.1  gmcgarry 		}
   1035  1.39   mycroft 		if (sc->sc_chipset >= XI_CHIPSET_MOHAWK)
   1036  1.39   mycroft 			for (i = 0; i < 6; i++)
   1037  1.39   mycroft 				indaddr[num * 6 + i] = enm->enm_addrlo[5 - i];
   1038  1.39   mycroft 		else
   1039  1.39   mycroft 			for (i = 0; i < 6; i++)
   1040  1.39   mycroft 				indaddr[num * 6 + i] = enm->enm_addrlo[i];
   1041  1.39   mycroft 		ETHER_NEXT_MULTI(step, enm);
   1042  1.39   mycroft 	}
   1043  1.39   mycroft 	ifp->if_flags &= ~IFF_ALLMULTI;
   1044   1.1  gmcgarry 
   1045  1.39   mycroft done:
   1046  1.39   mycroft 	if (num < 10)
   1047  1.39   mycroft 		memset(&indaddr[num * 6], 0xff, 6 * (10 - num));
   1048  1.11  gmcgarry 
   1049  1.39   mycroft 	for (page = 0; page < 8; page++) {
   1050  1.39   mycroft #ifdef XIDEBUG
   1051  1.39   mycroft 		if (xidebug & XID_MCAST) {
   1052  1.46   mycroft 			printf("page %d before:", page);
   1053  1.39   mycroft 			for (i = 0; i < 8; i++)
   1054  1.39   mycroft 				printf(" %02x", indaddr[page * 8 + i]);
   1055  1.11  gmcgarry 			printf("\n");
   1056   1.1  gmcgarry 		}
   1057  1.11  gmcgarry #endif
   1058  1.39   mycroft 
   1059  1.39   mycroft 		PAGE(sc, 0x50 + page);
   1060  1.48   mycroft 		bus_space_write_region_1(bst, bsh, IA, &indaddr[page * 8],
   1061  1.48   mycroft 		    page == 7 ? 4 : 8);
   1062  1.46   mycroft 		/*
   1063  1.46   mycroft 		 * XXX
   1064  1.46   mycroft 		 * Without this delay, the address registers on my CE2 get
   1065  1.46   mycroft 		 * trashed the first and I have to cycle it.  I have no idea
   1066  1.46   mycroft 		 * why.  - mycroft, 2004/08/09
   1067  1.46   mycroft 		 */
   1068  1.46   mycroft 		DELAY(50);
   1069  1.46   mycroft 
   1070  1.46   mycroft #ifdef XIDEBUG
   1071  1.46   mycroft 		if (xidebug & XID_MCAST) {
   1072  1.48   mycroft 			bus_space_read_region_1(bst, bsh, IA,
   1073  1.46   mycroft 			    &indaddr[page * 8], page == 7 ? 4 : 8);
   1074  1.46   mycroft 			printf("page %d after: ", page);
   1075  1.46   mycroft 			for (i = 0; i < 8; i++)
   1076  1.46   mycroft 				printf(" %02x", indaddr[page * 8 + i]);
   1077  1.46   mycroft 			printf("\n");
   1078  1.46   mycroft 		}
   1079  1.46   mycroft #endif
   1080   1.1  gmcgarry 	}
   1081  1.39   mycroft 
   1082  1.39   mycroft 	PAGE(sc, 0x42);
   1083  1.39   mycroft 	x = SWC1_IND_ADDR;
   1084  1.39   mycroft 	if (ifp->if_flags & IFF_PROMISC)
   1085  1.39   mycroft 		x |= SWC1_PROMISC;
   1086  1.44   mycroft 	if (ifp->if_flags & (IFF_ALLMULTI|IFF_PROMISC))
   1087  1.39   mycroft 		x |= SWC1_MCAST_PROM;
   1088  1.39   mycroft 	if (!LIST_FIRST(&sc->sc_mii.mii_phys))
   1089  1.39   mycroft 		x |= SWC1_AUTO_MEDIA;
   1090  1.48   mycroft 	bus_space_write_1(sc->sc_bst, sc->sc_bsh, SWC1, x);
   1091   1.1  gmcgarry }
   1092   1.1  gmcgarry 
   1093  1.39   mycroft STATIC void
   1094  1.66       dsl xi_cycle_power(struct xi_softc *sc)
   1095   1.1  gmcgarry {
   1096   1.1  gmcgarry 	bus_space_tag_t bst = sc->sc_bst;
   1097   1.1  gmcgarry 	bus_space_handle_t bsh = sc->sc_bsh;
   1098   1.1  gmcgarry 
   1099   1.2  gmcgarry 	DPRINTF(XID_CONFIG, ("xi_cycle_power()\n"));
   1100   1.1  gmcgarry 
   1101   1.1  gmcgarry 	PAGE(sc, 4);
   1102   1.1  gmcgarry 	DELAY(1);
   1103  1.48   mycroft 	bus_space_write_1(bst, bsh, GP1, 0);
   1104  1.47   mycroft 	tsleep(&xi_cycle_power, PWAIT, "xipwr1", hz * 40 / 1000);
   1105  1.39   mycroft 	if (sc->sc_chipset >= XI_CHIPSET_MOHAWK)
   1106  1.48   mycroft 		bus_space_write_1(bst, bsh, GP1, POWER_UP);
   1107   1.1  gmcgarry 	else
   1108   1.1  gmcgarry 		/* XXX What is bit 2 (aka AIC)? */
   1109  1.48   mycroft 		bus_space_write_1(bst, bsh, GP1, POWER_UP | 4);
   1110  1.47   mycroft 	tsleep(&xi_cycle_power, PWAIT, "xipwr2", hz * 20 / 1000);
   1111   1.1  gmcgarry }
   1112   1.1  gmcgarry 
   1113  1.39   mycroft STATIC void
   1114  1.66       dsl xi_full_reset(struct xi_softc *sc)
   1115   1.1  gmcgarry {
   1116   1.1  gmcgarry 	bus_space_tag_t bst = sc->sc_bst;
   1117   1.1  gmcgarry 	bus_space_handle_t bsh = sc->sc_bsh;
   1118  1.39   mycroft 	u_int8_t x;
   1119   1.1  gmcgarry 
   1120   1.2  gmcgarry 	DPRINTF(XID_CONFIG, ("xi_full_reset()\n"));
   1121   1.1  gmcgarry 
   1122   1.1  gmcgarry 	/* Do an as extensive reset as possible on all functions. */
   1123   1.1  gmcgarry 	xi_cycle_power(sc);
   1124  1.48   mycroft 	bus_space_write_1(bst, bsh, CR, SOFT_RESET);
   1125  1.47   mycroft 	tsleep(&xi_full_reset, PWAIT, "xirst1", hz * 20 / 1000);
   1126  1.48   mycroft 	bus_space_write_1(bst, bsh, CR, 0);
   1127  1.47   mycroft 	tsleep(&xi_full_reset, PWAIT, "xirst2", hz * 20 / 1000);
   1128  1.39   mycroft 	PAGE(sc, 4);
   1129  1.39   mycroft 	if (sc->sc_chipset >= XI_CHIPSET_MOHAWK) {
   1130   1.1  gmcgarry 		/*
   1131   1.1  gmcgarry 		 * Drive GP1 low to power up ML6692 and GP2 high to power up
   1132  1.29   tsutsui 		 * the 10MHz chip.  XXX What chip is that?  The phy?
   1133   1.1  gmcgarry 		 */
   1134  1.48   mycroft 		bus_space_write_1(bst, bsh, GP0, GP1_OUT | GP2_OUT | GP2_WR);
   1135   1.1  gmcgarry 	}
   1136  1.47   mycroft 	tsleep(&xi_full_reset, PWAIT, "xirst3", hz * 500 / 1000);
   1137   1.1  gmcgarry 
   1138   1.1  gmcgarry 	/* Get revision information.  XXX Symbolic constants. */
   1139  1.48   mycroft 	sc->sc_rev = bus_space_read_1(bst, bsh, BV) &
   1140  1.39   mycroft 	    ((sc->sc_chipset >= XI_CHIPSET_MOHAWK) ? 0x70 : 0x30) >> 4;
   1141  1.39   mycroft 	DPRINTF(XID_CONFIG, ("xi: rev=%02x\n", sc->sc_rev));
   1142   1.1  gmcgarry 
   1143   1.1  gmcgarry 	/* Media selection.  XXX Maybe manual overriding too? */
   1144  1.39   mycroft 	if (sc->sc_chipset < XI_CHIPSET_MOHAWK) {
   1145   1.1  gmcgarry 		/*
   1146   1.1  gmcgarry 		 * XXX I have no idea what this really does, it is from the
   1147   1.1  gmcgarry 		 * Linux driver.
   1148   1.1  gmcgarry 		 */
   1149  1.48   mycroft 		bus_space_write_1(bst, bsh, GP0, GP1_OUT);
   1150   1.1  gmcgarry 	}
   1151  1.47   mycroft 	tsleep(&xi_full_reset, PWAIT, "xirst4", hz * 40 / 1000);
   1152   1.1  gmcgarry 
   1153   1.1  gmcgarry 	/*
   1154   1.1  gmcgarry 	 * Disable source insertion.
   1155   1.1  gmcgarry 	 * XXX Dingo does not have this bit, but Linux does it unconditionally.
   1156   1.1  gmcgarry 	 */
   1157  1.39   mycroft 	if (sc->sc_chipset < XI_CHIPSET_DINGO) {
   1158   1.1  gmcgarry 		PAGE(sc, 0x42);
   1159  1.48   mycroft 		bus_space_write_1(bst, bsh, SWC0, 0x20);
   1160   1.1  gmcgarry 	}
   1161   1.1  gmcgarry 
   1162   1.1  gmcgarry 	/* Set the local memory dividing line. */
   1163   1.1  gmcgarry 	if (sc->sc_rev != 1) {
   1164   1.1  gmcgarry 		PAGE(sc, 2);
   1165   1.1  gmcgarry 		/* XXX Symbolic constant preferrable. */
   1166  1.48   mycroft 		bus_space_write_2(bst, bsh, RBS0, 0x2000);
   1167   1.1  gmcgarry 	}
   1168   1.1  gmcgarry 
   1169   1.1  gmcgarry 	/*
   1170   1.1  gmcgarry 	 * Apparently the receive byte pointer can be bad after a reset, so
   1171   1.1  gmcgarry 	 * we hardwire it correctly.
   1172   1.1  gmcgarry 	 */
   1173   1.1  gmcgarry 	PAGE(sc, 0);
   1174  1.48   mycroft 	bus_space_write_2(bst, bsh, DO0, DO_CHG_OFFSET);
   1175   1.1  gmcgarry 
   1176   1.1  gmcgarry 	/* Setup ethernet MAC registers. XXX Symbolic constants. */
   1177   1.1  gmcgarry 	PAGE(sc, 0x40);
   1178  1.48   mycroft 	bus_space_write_1(bst, bsh, RX0MSK,
   1179   1.1  gmcgarry 	    PKT_TOO_LONG | CRC_ERR | RX_OVERRUN | RX_ABORT | RX_OK);
   1180  1.48   mycroft 	bus_space_write_1(bst, bsh, TX0MSK,
   1181   1.1  gmcgarry 	    CARRIER_LOST | EXCESSIVE_COLL | TX_UNDERRUN | LATE_COLLISION |
   1182   1.1  gmcgarry 	    SQE | TX_ABORT | TX_OK);
   1183  1.39   mycroft 	if (sc->sc_chipset < XI_CHIPSET_DINGO)
   1184   1.1  gmcgarry 		/* XXX From Linux, dunno what 0xb0 means. */
   1185  1.48   mycroft 		bus_space_write_1(bst, bsh, TX1MSK, 0xb0);
   1186  1.48   mycroft 	bus_space_write_1(bst, bsh, RXST0, 0);
   1187  1.48   mycroft 	bus_space_write_1(bst, bsh, TXST0, 0);
   1188  1.48   mycroft 	bus_space_write_1(bst, bsh, TXST1, 0);
   1189   1.1  gmcgarry 
   1190  1.39   mycroft 	PAGE(sc, 2);
   1191  1.39   mycroft 
   1192   1.1  gmcgarry 	/* Enable MII function if available. */
   1193  1.39   mycroft 	x = 0;
   1194  1.39   mycroft 	if (LIST_FIRST(&sc->sc_mii.mii_phys))
   1195  1.39   mycroft 		x |= SELECT_MII;
   1196  1.48   mycroft 	bus_space_write_1(bst, bsh, MSR, x);
   1197  1.47   mycroft 	tsleep(&xi_full_reset, PWAIT, "xirst5", hz * 20 / 1000);
   1198   1.1  gmcgarry 
   1199   1.1  gmcgarry 	/* Configure the LED registers. */
   1200   1.1  gmcgarry 	/* XXX This is not good for 10base2. */
   1201  1.48   mycroft 	bus_space_write_1(bst, bsh, LED,
   1202  1.41   mycroft 	    (LED_TX_ACT << LED1_SHIFT) | (LED_10MB_LINK << LED0_SHIFT));
   1203  1.41   mycroft 	if (sc->sc_chipset >= XI_CHIPSET_DINGO)
   1204  1.48   mycroft 		bus_space_write_1(bst, bsh, LED3, LED_100MB_LINK << LED3_SHIFT);
   1205   1.1  gmcgarry 
   1206   1.1  gmcgarry 	/*
   1207   1.1  gmcgarry 	 * The Linux driver says this:
   1208   1.1  gmcgarry 	 * We should switch back to page 0 to avoid a bug in revision 0
   1209   1.1  gmcgarry 	 * where regs with offset below 8 can't be read after an access
   1210   1.1  gmcgarry 	 * to the MAC registers.
   1211   1.1  gmcgarry 	 */
   1212   1.1  gmcgarry 	PAGE(sc, 0);
   1213   1.1  gmcgarry }
   1214