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