Home | History | Annotate | Line # | Download | only in pcmcia
if_xi.c revision 1.42
      1 /*	$NetBSD: if_xi.c,v 1.42 2004/08/08 07:25:20 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.42 2004/08/08 07:25:20 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, savedpage;
    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 	if (sc->sc_chipset >= XI_CHIPSET_MOHAWK) {
    336 		/* Disable interrupt (Linux does it). */
    337 		bus_space_write_1(sc->sc_bst, sc->sc_bsh, sc->sc_offset + CR,
    338 		    0);
    339 	}
    340 
    341 	savedpage =
    342 	    bus_space_read_1(sc->sc_bst, sc->sc_bsh, sc->sc_offset + PR);
    343 
    344 	PAGE(sc, 0);
    345 	esr = bus_space_read_1(sc->sc_bst, sc->sc_bsh, sc->sc_offset + ESR);
    346 	isr = bus_space_read_1(sc->sc_bst, sc->sc_bsh, sc->sc_offset + ISR0);
    347 	rsr = bus_space_read_1(sc->sc_bst, sc->sc_bsh, sc->sc_offset + RSR);
    348 
    349 	/* Check to see if card has been ejected. */
    350 	if (isr == 0xff) {
    351 #ifdef DIAGNOSTIC
    352 		printf("%s: interrupt for dead card\n", sc->sc_dev.dv_xname);
    353 #endif
    354 		goto end;
    355 	}
    356 	DPRINTF(XID_INTR, ("xi: isr=%02x\n", isr));
    357 
    358 	PAGE(sc, 0x40);
    359 	rx_status =
    360 	    bus_space_read_1(sc->sc_bst, sc->sc_bsh, sc->sc_offset + RXST0);
    361 	bus_space_write_1(sc->sc_bst, sc->sc_bsh, sc->sc_offset + RXST0,
    362 	    ~rx_status & 0xff);
    363 	tx_status =
    364 	    bus_space_read_1(sc->sc_bst, sc->sc_bsh, sc->sc_offset + TXST0);
    365 	tx_status |=
    366 	    bus_space_read_1(sc->sc_bst, sc->sc_bsh, sc->sc_offset + TXST1) << 8;
    367 	bus_space_write_1(sc->sc_bst, sc->sc_bsh, sc->sc_offset + TXST0,0);
    368 	bus_space_write_1(sc->sc_bst, sc->sc_bsh, sc->sc_offset + TXST1,0);
    369 	DPRINTF(XID_INTR, ("xi: rx_status=%02x tx_status=%04x\n", rx_status,
    370 	    tx_status));
    371 
    372 	PAGE(sc, 0);
    373 	while (esr & FULL_PKT_RCV) {
    374 		if (!(rsr & RSR_RX_OK))
    375 			break;
    376 
    377 		/* Compare bytes read this interrupt to hard maximum. */
    378 		if (recvcount > MAX_BYTES_INTR) {
    379 			DPRINTF(XID_INTR,
    380 			    ("xi: too many bytes this interrupt\n"));
    381 			ifp->if_iqdrops++;
    382 			/* Drop packet. */
    383 			bus_space_write_2(sc->sc_bst, sc->sc_bsh,
    384 			    sc->sc_offset + DO0, DO_SKIP_RX_PKT);
    385 		}
    386 		tempint = xi_get(sc);	/* XXX doesn't check the error! */
    387 		recvcount += tempint;
    388 		ifp->if_ibytes += tempint;
    389 		esr = bus_space_read_1(sc->sc_bst, sc->sc_bsh,
    390 		    sc->sc_offset + ESR);
    391 		rsr = bus_space_read_1(sc->sc_bst, sc->sc_bsh,
    392 		    sc->sc_offset + RSR);
    393 	}
    394 
    395 	/* Packet too long? */
    396 	if (rsr & RSR_TOO_LONG) {
    397 		ifp->if_ierrors++;
    398 		DPRINTF(XID_INTR, ("xi: packet too long\n"));
    399 	}
    400 
    401 	/* CRC error? */
    402 	if (rsr & RSR_CRCERR) {
    403 		ifp->if_ierrors++;
    404 		DPRINTF(XID_INTR, ("xi: CRC error detected\n"));
    405 	}
    406 
    407 	/* Alignment error? */
    408 	if (rsr & RSR_ALIGNERR) {
    409 		ifp->if_ierrors++;
    410 		DPRINTF(XID_INTR, ("xi: alignment error detected\n"));
    411 	}
    412 
    413 	/* Check for rx overrun. */
    414 	if (rx_status & RX_OVERRUN) {
    415 		ifp->if_ierrors++;
    416 		bus_space_write_1(sc->sc_bst, sc->sc_bsh, sc->sc_offset + CR,
    417 		    CLR_RX_OVERRUN);
    418 		DPRINTF(XID_INTR, ("xi: overrun cleared\n"));
    419 	}
    420 
    421 	/* Try to start more packets transmitting. */
    422 	if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
    423 		xi_start(ifp);
    424 
    425 	/* Detected excessive collisions? */
    426 	if ((tx_status & EXCESSIVE_COLL) && ifp->if_opackets > 0) {
    427 		DPRINTF(XID_INTR, ("xi: excessive collisions\n"));
    428 		bus_space_write_1(sc->sc_bst, sc->sc_bsh, sc->sc_offset + CR,
    429 		    RESTART_TX);
    430 		ifp->if_oerrors++;
    431 	}
    432 
    433 	if ((tx_status & TX_ABORT) && ifp->if_opackets > 0)
    434 		ifp->if_oerrors++;
    435 
    436 	/* have handled the interrupt */
    437 #if NRND > 0
    438 	rnd_add_uint32(&sc->sc_rnd_source, tx_status);
    439 #endif
    440 
    441 end:
    442 	/* Reenable interrupts. */
    443 	PAGE(sc, savedpage);
    444 	bus_space_write_1(sc->sc_bst, sc->sc_bsh, sc->sc_offset + CR,
    445 	    ENABLE_INT);
    446 
    447 	return (1);
    448 }
    449 
    450 /*
    451  * Pull a packet from the card into an mbuf chain.
    452  */
    453 STATIC u_int16_t
    454 xi_get(sc)
    455 	struct xi_softc *sc;
    456 {
    457 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    458 	struct mbuf *top, **mp, *m;
    459 	u_int16_t pktlen, len, recvcount = 0;
    460 	u_int8_t *data;
    461 
    462 	DPRINTF(XID_CONFIG, ("xi_get()\n"));
    463 
    464 	PAGE(sc, 0);
    465 	pktlen =
    466 	    bus_space_read_2(sc->sc_bst, sc->sc_bsh, sc->sc_offset + RBC0) &
    467 	    RBC_COUNT_MASK;
    468 
    469 	DPRINTF(XID_CONFIG, ("xi_get: pktlen=%d\n", pktlen));
    470 
    471 	if (pktlen == 0) {
    472 		/*
    473 		 * XXX At least one CE2 sets RBC0 == 0 occasionally, and only
    474 		 * when MPE is set.  It is not known why.
    475 		 */
    476 		return (0);
    477 	}
    478 
    479 	/* XXX should this be incremented now ? */
    480 	recvcount += pktlen;
    481 
    482 	MGETHDR(m, M_DONTWAIT, MT_DATA);
    483 	if (m == 0)
    484 		return (recvcount);
    485 	m->m_pkthdr.rcvif = ifp;
    486 	m->m_pkthdr.len = pktlen;
    487 	m->m_flags |= M_HASFCS;
    488 	len = MHLEN;
    489 	top = 0;
    490 	mp = &top;
    491 
    492 	while (pktlen > 0) {
    493 		if (top) {
    494 			MGET(m, M_DONTWAIT, MT_DATA);
    495 			if (m == 0) {
    496 				m_freem(top);
    497 				return (recvcount);
    498 			}
    499 			len = MLEN;
    500 		}
    501 		if (pktlen >= MINCLSIZE) {
    502 			MCLGET(m, M_DONTWAIT);
    503 			if (!(m->m_flags & M_EXT)) {
    504 				m_freem(m);
    505 				m_freem(top);
    506 				return (recvcount);
    507 			}
    508 			len = MCLBYTES;
    509 		}
    510 		if (!top) {
    511 			caddr_t newdata = (caddr_t)ALIGN(m->m_data +
    512 			    sizeof(struct ether_header)) -
    513 			    sizeof(struct ether_header);
    514 			len -= newdata - m->m_data;
    515 			m->m_data = newdata;
    516 		}
    517 		len = min(pktlen, len);
    518 		data = mtod(m, u_int8_t *);
    519 		if (len > 1) {
    520 		        len &= ~1;
    521 			bus_space_read_multi_2(sc->sc_bst, sc->sc_bsh,
    522 			    sc->sc_offset + EDP, (u_int16_t *)data, len>>1);
    523 		} else
    524 			*data = bus_space_read_1(sc->sc_bst, sc->sc_bsh,
    525 			    sc->sc_offset + EDP);
    526 		m->m_len = len;
    527 		pktlen -= len;
    528 		*mp = m;
    529 		mp = &m->m_next;
    530 	}
    531 
    532 	/* Skip Rx packet. */
    533 	bus_space_write_2(sc->sc_bst, sc->sc_bsh, sc->sc_offset + DO0,
    534 	    DO_SKIP_RX_PKT);
    535 
    536 	ifp->if_ipackets++;
    537 
    538 #if NBPFILTER > 0
    539 	if (ifp->if_bpf)
    540 		bpf_mtap(ifp->if_bpf, top);
    541 #endif
    542 
    543 	(*ifp->if_input)(ifp, top);
    544 	return (recvcount);
    545 }
    546 
    547 /*
    548  * Serial management for the MII.
    549  * The DELAY's below stem from the fact that the maximum frequency
    550  * acceptable on the MDC pin is 2.5 MHz and fast processors can easily
    551  * go much faster than that.
    552  */
    553 
    554 /* Let the MII serial management be idle for one period. */
    555 static INLINE void xi_mdi_idle __P((struct xi_softc *));
    556 static INLINE void
    557 xi_mdi_idle(sc)
    558 	struct xi_softc *sc;
    559 {
    560 	bus_space_tag_t bst = sc->sc_bst;
    561 	bus_space_handle_t bsh = sc->sc_bsh;
    562 	bus_size_t offset = sc->sc_offset;
    563 
    564 	/* Drive MDC low... */
    565 	bus_space_write_1(bst, bsh, offset + GP2, MDC_LOW);
    566 	DELAY(1);
    567 
    568 	/* and high again. */
    569 	bus_space_write_1(bst, bsh, offset + GP2, MDC_HIGH);
    570 	DELAY(1);
    571 }
    572 
    573 /* Pulse out one bit of data. */
    574 static INLINE void xi_mdi_pulse __P((struct xi_softc *, int));
    575 static INLINE void
    576 xi_mdi_pulse(sc, data)
    577 	struct xi_softc *sc;
    578 	int data;
    579 {
    580 	bus_space_tag_t bst = sc->sc_bst;
    581 	bus_space_handle_t bsh = sc->sc_bsh;
    582 	bus_size_t offset = sc->sc_offset;
    583 	u_int8_t bit = data ? MDIO_HIGH : MDIO_LOW;
    584 
    585 	/* First latch the data bit MDIO with clock bit MDC low...*/
    586 	bus_space_write_1(bst, bsh, offset + GP2, bit | MDC_LOW);
    587 	DELAY(1);
    588 
    589 	/* then raise the clock again, preserving the data bit. */
    590 	bus_space_write_1(bst, bsh, offset + GP2, bit | MDC_HIGH);
    591 	DELAY(1);
    592 }
    593 
    594 /* Probe one bit of data. */
    595 static INLINE int xi_mdi_probe __P((struct xi_softc *sc));
    596 static INLINE int
    597 xi_mdi_probe(sc)
    598 	struct xi_softc *sc;
    599 {
    600 	bus_space_tag_t bst = sc->sc_bst;
    601 	bus_space_handle_t bsh = sc->sc_bsh;
    602 	bus_size_t offset = sc->sc_offset;
    603 	u_int8_t x;
    604 
    605 	/* Pull clock bit MDCK low... */
    606 	bus_space_write_1(bst, bsh, offset + GP2, MDC_LOW);
    607 	DELAY(1);
    608 
    609 	/* Read data and drive clock high again. */
    610 	x = bus_space_read_1(bst, bsh, offset + GP2);
    611 	bus_space_write_1(bst, bsh, offset + GP2, MDC_HIGH);
    612 	DELAY(1);
    613 
    614 	return (x & MDIO);
    615 }
    616 
    617 /* Pulse out a sequence of data bits. */
    618 static INLINE void xi_mdi_pulse_bits __P((struct xi_softc *, u_int32_t, int));
    619 static INLINE void
    620 xi_mdi_pulse_bits(sc, data, len)
    621 	struct xi_softc *sc;
    622 	u_int32_t data;
    623 	int len;
    624 {
    625 	u_int32_t mask;
    626 
    627 	for (mask = 1 << (len - 1); mask; mask >>= 1)
    628 		xi_mdi_pulse(sc, data & mask);
    629 }
    630 
    631 /* Read a PHY register. */
    632 STATIC int
    633 xi_mdi_read(self, phy, reg)
    634 	struct device *self;
    635 	int phy;
    636 	int reg;
    637 {
    638 	struct xi_softc *sc = (struct xi_softc *)self;
    639 	int i;
    640 	u_int32_t mask;
    641 	u_int32_t data = 0;
    642 
    643 	PAGE(sc, 2);
    644 	for (i = 0; i < 32; i++)	/* Synchronize. */
    645 		xi_mdi_pulse(sc, 1);
    646 	xi_mdi_pulse_bits(sc, 0x06, 4); /* Start + Read opcode */
    647 	xi_mdi_pulse_bits(sc, phy, 5);	/* PHY address */
    648 	xi_mdi_pulse_bits(sc, reg, 5);	/* PHY register */
    649 	xi_mdi_idle(sc);		/* Turn around. */
    650 	xi_mdi_probe(sc);		/* Drop initial zero bit. */
    651 
    652 	for (mask = 1 << 15; mask; mask >>= 1) {
    653 		if (xi_mdi_probe(sc))
    654 			data |= mask;
    655 	}
    656 	xi_mdi_idle(sc);
    657 
    658 	DPRINTF(XID_MII,
    659 	    ("xi_mdi_read: phy %d reg %d -> %x\n", phy, reg, data));
    660 
    661 	return (data);
    662 }
    663 
    664 /* Write a PHY register. */
    665 STATIC void
    666 xi_mdi_write(self, phy, reg, value)
    667 	struct device *self;
    668 	int phy;
    669 	int reg;
    670 	int value;
    671 {
    672 	struct xi_softc *sc = (struct xi_softc *)self;
    673 	int i;
    674 
    675 	PAGE(sc, 2);
    676 	for (i = 0; i < 32; i++)	/* Synchronize. */
    677 		xi_mdi_pulse(sc, 1);
    678 	xi_mdi_pulse_bits(sc, 0x05, 4); /* Start + Write opcode */
    679 	xi_mdi_pulse_bits(sc, phy, 5);	/* PHY address */
    680 	xi_mdi_pulse_bits(sc, reg, 5);	/* PHY register */
    681 	xi_mdi_pulse_bits(sc, 0x02, 2); /* Turn around. */
    682 	xi_mdi_pulse_bits(sc, value, 16);	/* Write the data */
    683 	xi_mdi_idle(sc);		/* Idle away. */
    684 
    685 	DPRINTF(XID_MII,
    686 	    ("xi_mdi_write: phy %d reg %d val %x\n", phy, reg, value));
    687 }
    688 
    689 STATIC void
    690 xi_statchg(self)
    691 	struct device *self;
    692 {
    693 	/* XXX Update ifp->if_baudrate */
    694 }
    695 
    696 /*
    697  * Change media according to request.
    698  */
    699 STATIC int
    700 xi_mediachange(ifp)
    701 	struct ifnet *ifp;
    702 {
    703 	int s;
    704 
    705 	DPRINTF(XID_CONFIG, ("xi_mediachange()\n"));
    706 
    707 	if (ifp->if_flags & IFF_UP) {
    708 		s = splnet();
    709 		xi_init(ifp->if_softc);
    710 		splx(s);
    711 	}
    712 	return (0);
    713 }
    714 
    715 /*
    716  * Notify the world which media we're using.
    717  */
    718 STATIC void
    719 xi_mediastatus(ifp, ifmr)
    720 	struct ifnet *ifp;
    721 	struct ifmediareq *ifmr;
    722 {
    723 	struct xi_softc *sc = ifp->if_softc;
    724 
    725 	DPRINTF(XID_CONFIG, ("xi_mediastatus()\n"));
    726 
    727 	mii_pollstat(&sc->sc_mii);
    728 	ifmr->ifm_status = sc->sc_mii.mii_media_status;
    729 	ifmr->ifm_active = sc->sc_mii.mii_media_active;
    730 }
    731 
    732 STATIC void
    733 xi_reset(sc)
    734 	struct xi_softc *sc;
    735 {
    736 	int s;
    737 
    738 	DPRINTF(XID_CONFIG, ("xi_reset()\n"));
    739 
    740 	s = splnet();
    741 	xi_stop(sc);
    742 	xi_init(sc);
    743 	splx(s);
    744 }
    745 
    746 STATIC void
    747 xi_watchdog(ifp)
    748 	struct ifnet *ifp;
    749 {
    750 	struct xi_softc *sc = ifp->if_softc;
    751 
    752 	printf("%s: device timeout\n", sc->sc_dev.dv_xname);
    753 	++ifp->if_oerrors;
    754 
    755 	xi_reset(sc);
    756 }
    757 
    758 STATIC void
    759 xi_stop(sc)
    760 	register struct xi_softc *sc;
    761 {
    762 	bus_space_tag_t bst = sc->sc_bst;
    763 	bus_space_handle_t bsh = sc->sc_bsh;
    764 	bus_size_t offset = sc->sc_offset;
    765 
    766 	DPRINTF(XID_CONFIG, ("xi_stop()\n"));
    767 
    768 	/* Disable interrupts. */
    769 	PAGE(sc, 0);
    770 	bus_space_write_1(bst, bsh, offset + CR, 0);
    771 
    772 	PAGE(sc, 1);
    773 	bus_space_write_1(bst, bsh, offset + IMR0, 0);
    774 
    775 	/* Cancel watchdog timer. */
    776 	sc->sc_ethercom.ec_if.if_timer = 0;
    777 }
    778 
    779 STATIC int
    780 xi_enable(sc)
    781 	struct xi_softc *sc;
    782 {
    783 	int error;
    784 
    785 	if (!sc->sc_enabled) {
    786 		error = (*sc->sc_enable)(sc);
    787 		if (error)
    788 			return (error);
    789 		sc->sc_enabled = 1;
    790 		xi_full_reset(sc);
    791 	}
    792 	return (0);
    793 }
    794 
    795 STATIC void
    796 xi_disable(sc)
    797 	struct xi_softc *sc;
    798 {
    799 
    800 	if (sc->sc_enabled) {
    801 		sc->sc_enabled = 0;
    802 		(*sc->sc_disable)(sc);
    803 	}
    804 }
    805 
    806 STATIC void
    807 xi_init(sc)
    808 	struct xi_softc *sc;
    809 {
    810 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    811 	bus_space_tag_t bst = sc->sc_bst;
    812 	bus_space_handle_t bsh = sc->sc_bsh;
    813 	bus_size_t offset = sc->sc_offset;
    814 
    815 	DPRINTF(XID_CONFIG, ("xi_init()\n"));
    816 
    817 	xi_set_address(sc);
    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 	/* Set current media. */
    835 	mii_mediachg(&sc->sc_mii);
    836 
    837 	ifp->if_flags |= IFF_RUNNING;
    838 	ifp->if_flags &= ~IFF_OACTIVE;
    839 
    840 	xi_start(ifp);
    841 }
    842 
    843 /*
    844  * Start outputting on the interface.
    845  * Always called as splnet().
    846  */
    847 STATIC void
    848 xi_start(ifp)
    849 	struct ifnet *ifp;
    850 {
    851 	struct xi_softc *sc = ifp->if_softc;
    852 	bus_space_tag_t bst = sc->sc_bst;
    853 	bus_space_handle_t bsh = sc->sc_bsh;
    854 	bus_size_t offset = sc->sc_offset;
    855 	unsigned int s, len, pad = 0;
    856 	struct mbuf *m0, *m;
    857 	u_int16_t space;
    858 
    859 	DPRINTF(XID_CONFIG, ("xi_start()\n"));
    860 
    861 	/* Don't transmit if interface is busy or not running. */
    862 	if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING) {
    863 		DPRINTF(XID_CONFIG, ("xi: interface busy or not running\n"));
    864 		return;
    865 	}
    866 
    867 	/* Peek at the next packet. */
    868 	IFQ_POLL(&ifp->if_snd, m0);
    869 	if (m0 == 0)
    870 		return;
    871 
    872 	/* We need to use m->m_pkthdr.len, so require the header. */
    873 	if (!(m0->m_flags & M_PKTHDR))
    874 		panic("xi_start: no header mbuf");
    875 
    876 	len = m0->m_pkthdr.len;
    877 
    878 #if 1
    879 	/* Pad to ETHER_MIN_LEN - ETHER_CRC_LEN. */
    880 	if (len < ETHER_MIN_LEN - ETHER_CRC_LEN)
    881 		pad = ETHER_MIN_LEN - ETHER_CRC_LEN - len;
    882 #else
    883 	pad = 0;
    884 #endif
    885 
    886 	PAGE(sc, 0);
    887 
    888 	bus_space_write_2(bst, bsh, offset + TRS, (u_int16_t)len + pad + 2);
    889 	space = bus_space_read_2(bst, bsh, offset + TSO) & 0x7fff;
    890 	if (len + pad + 2 > space) {
    891 		DPRINTF(XID_FIFO,
    892 		    ("xi: not enough space in output FIFO (%d > %d)\n",
    893 		    len + pad + 2, space));
    894 		return;
    895 	}
    896 
    897 	IFQ_DEQUEUE(&ifp->if_snd, m0);
    898 
    899 #if NBPFILTER > 0
    900 	if (ifp->if_bpf)
    901 		bpf_mtap(ifp->if_bpf, m0);
    902 #endif
    903 
    904 	/*
    905 	 * Do the output at splhigh() so that an interrupt from another device
    906 	 * won't cause a FIFO underrun.
    907 	 */
    908 	s = splhigh();
    909 
    910 	bus_space_write_2(bst, bsh, offset + EDP, (u_int16_t)len + pad);
    911 	for (m = m0; m; ) {
    912 		if (m->m_len > 1)
    913 			bus_space_write_multi_2(bst, bsh, offset + EDP,
    914 			    mtod(m, u_int16_t *), m->m_len>>1);
    915 		if (m->m_len & 1) {
    916 			DPRINTF(XID_CONFIG, ("xi: XXX odd!\n"));
    917 			bus_space_write_1(bst, bsh, offset + EDP,
    918 			    *(mtod(m, u_int8_t *) + m->m_len - 1));
    919 		}
    920 		MFREE(m, m0);
    921 		m = m0;
    922 	}
    923 	DPRINTF(XID_CONFIG, ("xi: len=%d pad=%d total=%d\n", len, pad, len+pad+4));
    924 	if (sc->sc_chipset >= XI_CHIPSET_MOHAWK)
    925 		bus_space_write_1(bst, bsh, offset + CR, TX_PKT | ENABLE_INT);
    926 	else {
    927 		for (; pad > 1; pad -= 2)
    928 			bus_space_write_2(bst, bsh, offset + EDP, 0);
    929 		if (pad == 1)
    930 			bus_space_write_1(bst, bsh, offset + EDP, 0);
    931 	}
    932 
    933 	splx(s);
    934 
    935 	ifp->if_timer = 5;
    936 	++ifp->if_opackets;
    937 }
    938 
    939 STATIC int
    940 xi_ether_ioctl(ifp, cmd, data)
    941 	struct ifnet *ifp;
    942 	u_long cmd;
    943 	caddr_t data;
    944 {
    945 	struct ifaddr *ifa = (struct ifaddr *)data;
    946 	struct xi_softc *sc = ifp->if_softc;
    947 	int error;
    948 
    949 	DPRINTF(XID_CONFIG, ("xi_ether_ioctl()\n"));
    950 
    951 	switch (cmd) {
    952 	case SIOCSIFADDR:
    953 		if ((error = xi_enable(sc)) != 0)
    954 			break;
    955 
    956 		ifp->if_flags |= IFF_UP;
    957 
    958 		switch (ifa->ifa_addr->sa_family) {
    959 #ifdef INET
    960 		case AF_INET:
    961 			xi_init(sc);
    962 			arp_ifinit(ifp, ifa);
    963 			break;
    964 #endif	/* INET */
    965 
    966 #ifdef NS
    967 		case AF_NS:
    968 		{
    969 			struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
    970 
    971 			if (ns_nullhost(*ina))
    972 				ina->x_host = *(union ns_host *)
    973 					LLADDR(ifp->if_sadl);
    974 			else
    975 				memcpy(LLADDR(ifp->if_sadl), ina->x_host.c_host,
    976 					ifp->if_addrlen);
    977 			/* Set new address. */
    978 			xi_init(sc);
    979 			break;
    980 		}
    981 #endif  /* NS */
    982 
    983 		default:
    984 			xi_init(sc);
    985 			break;
    986 		}
    987 		break;
    988 
    989 	default:
    990 		return (EINVAL);
    991 	}
    992 
    993 	return (0);
    994 }
    995 
    996 STATIC int
    997 xi_ioctl(ifp, cmd, data)
    998 	struct ifnet *ifp;
    999 	u_long cmd;
   1000 	caddr_t data;
   1001 {
   1002 	struct xi_softc *sc = ifp->if_softc;
   1003 	struct ifreq *ifr = (struct ifreq *)data;
   1004 	int s, error = 0;
   1005 
   1006 	DPRINTF(XID_CONFIG, ("xi_ioctl()\n"));
   1007 
   1008 	s = splnet();
   1009 
   1010 	switch (cmd) {
   1011 	case SIOCSIFADDR:
   1012 		error = xi_ether_ioctl(ifp, cmd, data);
   1013 		break;
   1014 
   1015 	case SIOCSIFFLAGS:
   1016 		if ((ifp->if_flags & IFF_UP) == 0 &&
   1017 		    (ifp->if_flags & IFF_RUNNING) != 0) {
   1018 			/*
   1019 			 * If interface is marked down and it is running,
   1020 			 * stop it.
   1021 			 */
   1022 			xi_stop(sc);
   1023 			ifp->if_flags &= ~IFF_RUNNING;
   1024 			xi_disable(sc);
   1025 		} else if ((ifp->if_flags & IFF_UP) != 0 &&
   1026 			   (ifp->if_flags & IFF_RUNNING) == 0) {
   1027 			/*
   1028 			 * If interface is marked up and it is stopped,
   1029 			 * start it.
   1030 			 */
   1031 			if ((error = xi_enable(sc)) != 0)
   1032 				break;
   1033 			xi_init(sc);
   1034 		} else if ((ifp->if_flags & IFF_UP) != 0) {
   1035 			/*
   1036 			 * Reset the interface to pick up changes in any
   1037 			 * other flags that affect hardware registers.
   1038 			 */
   1039 			xi_set_address(sc);
   1040 		}
   1041 		break;
   1042 
   1043 	case SIOCADDMULTI:
   1044 	case SIOCDELMULTI:
   1045 		if (sc->sc_enabled == 0) {
   1046 			error = EIO;
   1047 			break;
   1048 		}
   1049 
   1050 		error = (cmd == SIOCADDMULTI) ?
   1051 		    ether_addmulti(ifr, &sc->sc_ethercom) :
   1052 		    ether_delmulti(ifr, &sc->sc_ethercom);
   1053 		if (error == ENETRESET) {
   1054 			/*
   1055 			 * Multicast list has changed; set the hardware
   1056 			 * filter accordingly.
   1057 			 */
   1058 			xi_set_address(sc);
   1059 			error = 0;
   1060 		}
   1061 		break;
   1062 
   1063 	case SIOCSIFMEDIA:
   1064 	case SIOCGIFMEDIA:
   1065 		error = ifmedia_ioctl(ifp, ifr, &sc->sc_mii.mii_media, cmd);
   1066 		break;
   1067 
   1068 	default:
   1069 		error = EINVAL;
   1070 		break;
   1071 	}
   1072 
   1073 	splx(s);
   1074 	return (error);
   1075 }
   1076 
   1077 STATIC void
   1078 xi_set_address(sc)
   1079 	struct xi_softc *sc;
   1080 {
   1081 	bus_space_tag_t bst = sc->sc_bst;
   1082 	bus_space_handle_t bsh = sc->sc_bsh;
   1083 	bus_size_t offset = sc->sc_offset;
   1084 	struct ethercom *ether = &sc->sc_ethercom;
   1085 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1086 	struct ether_multistep step;
   1087 	struct ether_multi *enm;
   1088 	int page, num;
   1089 	int i;
   1090 	u_int8_t x;
   1091 	u_int8_t *enaddr;
   1092 	u_int8_t indaddr[64];
   1093 
   1094 	DPRINTF(XID_CONFIG, ("xi_set_address()\n"));
   1095 
   1096 	enaddr = (u_int8_t *)LLADDR(ifp->if_sadl);
   1097 	if (sc->sc_chipset >= XI_CHIPSET_MOHAWK)
   1098 		for (i = 0; i < 6; i++)
   1099 			indaddr[i] = enaddr[5 - i];
   1100 	else
   1101 		for (i = 0; i < 6; i++)
   1102 			indaddr[i] = enaddr[i];
   1103 	num = 1;
   1104 
   1105 	if (ether->ec_multicnt > 9) {
   1106 		ifp->if_flags |= IFF_ALLMULTI;
   1107 		goto done;
   1108 	}
   1109 
   1110 	ETHER_FIRST_MULTI(step, ether, enm);
   1111 	for (; enm; num++) {
   1112 		if (memcmp(enm->enm_addrlo, enm->enm_addrhi,
   1113 		    sizeof(enm->enm_addrlo)) != 0) {
   1114 			/*
   1115 			 * The multicast address is really a range;
   1116 			 * it's easier just to accept all multicasts.
   1117 			 * XXX should we be setting IFF_ALLMULTI here?
   1118 			 */
   1119 			ifp->if_flags |= IFF_ALLMULTI;
   1120 			goto done;
   1121 		}
   1122 		if (sc->sc_chipset >= XI_CHIPSET_MOHAWK)
   1123 			for (i = 0; i < 6; i++)
   1124 				indaddr[num * 6 + i] = enm->enm_addrlo[5 - i];
   1125 		else
   1126 			for (i = 0; i < 6; i++)
   1127 				indaddr[num * 6 + i] = enm->enm_addrlo[i];
   1128 		ETHER_NEXT_MULTI(step, enm);
   1129 	}
   1130 	ifp->if_flags &= ~IFF_ALLMULTI;
   1131 
   1132 done:
   1133 	if (num < 10)
   1134 		memset(&indaddr[num * 6], 0xff, 6 * (10 - num));
   1135 
   1136 	for (page = 0; page < 8; page++) {
   1137 #ifdef XIDEBUG
   1138 		if (xidebug & XID_MCAST) {
   1139 			printf("page %d:", page);
   1140 			for (i = 0; i < 8; i++)
   1141 				printf(" %02x", indaddr[page * 8 + i]);
   1142 			printf("\n");
   1143 		}
   1144 #endif
   1145 
   1146 		PAGE(sc, 0x50 + page);
   1147 		bus_space_write_region_1(bst, bsh, offset + IA,
   1148 		    &indaddr[page * 8], 8);
   1149 	}
   1150 
   1151 	PAGE(sc, 0x42);
   1152 	x = SWC1_IND_ADDR;
   1153 	if (ifp->if_flags & IFF_PROMISC)
   1154 		x |= SWC1_PROMISC;
   1155 	if (ifp->if_flags & IFF_ALLMULTI)
   1156 		x |= SWC1_MCAST_PROM;
   1157 	if (!LIST_FIRST(&sc->sc_mii.mii_phys))
   1158 		x |= SWC1_AUTO_MEDIA;
   1159 	bus_space_write_1(sc->sc_bst, sc->sc_bsh, sc->sc_offset + SWC1, x);
   1160 
   1161 	PAGE(sc, 0);
   1162 }
   1163 
   1164 STATIC void
   1165 xi_cycle_power(sc)
   1166 	struct xi_softc *sc;
   1167 {
   1168 	bus_space_tag_t bst = sc->sc_bst;
   1169 	bus_space_handle_t bsh = sc->sc_bsh;
   1170 	bus_size_t offset = sc->sc_offset;
   1171 
   1172 	DPRINTF(XID_CONFIG, ("xi_cycle_power()\n"));
   1173 
   1174 	PAGE(sc, 4);
   1175 	DELAY(1);
   1176 	bus_space_write_1(bst, bsh, offset + GP1, 0);
   1177 	DELAY(40000);
   1178 	if (sc->sc_chipset >= XI_CHIPSET_MOHAWK)
   1179 		bus_space_write_1(bst, bsh, offset + GP1, POWER_UP);
   1180 	else
   1181 		/* XXX What is bit 2 (aka AIC)? */
   1182 		bus_space_write_1(bst, bsh, offset + GP1, POWER_UP | 4);
   1183 	DELAY(20000);
   1184 }
   1185 
   1186 STATIC void
   1187 xi_full_reset(sc)
   1188 	struct xi_softc *sc;
   1189 {
   1190 	bus_space_tag_t bst = sc->sc_bst;
   1191 	bus_space_handle_t bsh = sc->sc_bsh;
   1192 	bus_size_t offset = sc->sc_offset;
   1193 	u_int8_t x;
   1194 
   1195 	DPRINTF(XID_CONFIG, ("xi_full_reset()\n"));
   1196 
   1197 	/* Do an as extensive reset as possible on all functions. */
   1198 	xi_cycle_power(sc);
   1199 	bus_space_write_1(bst, bsh, offset + CR, SOFT_RESET);
   1200 	DELAY(20000);
   1201 	bus_space_write_1(bst, bsh, offset + CR, 0);
   1202 	DELAY(20000);
   1203 	PAGE(sc, 4);
   1204 	if (sc->sc_chipset >= XI_CHIPSET_MOHAWK) {
   1205 		/*
   1206 		 * Drive GP1 low to power up ML6692 and GP2 high to power up
   1207 		 * the 10MHz chip.  XXX What chip is that?  The phy?
   1208 		 */
   1209 		bus_space_write_1(bst, bsh, offset + GP0,
   1210 		    GP1_OUT | GP2_OUT | GP2_WR);
   1211 	}
   1212 	DELAY(500000);
   1213 
   1214 	/* Get revision information.  XXX Symbolic constants. */
   1215 	sc->sc_rev = bus_space_read_1(bst, bsh, offset + BV) &
   1216 	    ((sc->sc_chipset >= XI_CHIPSET_MOHAWK) ? 0x70 : 0x30) >> 4;
   1217 	DPRINTF(XID_CONFIG, ("xi: rev=%02x\n", sc->sc_rev));
   1218 
   1219 	/* Media selection.  XXX Maybe manual overriding too? */
   1220 	if (sc->sc_chipset < XI_CHIPSET_MOHAWK) {
   1221 		/*
   1222 		 * XXX I have no idea what this really does, it is from the
   1223 		 * Linux driver.
   1224 		 */
   1225 		bus_space_write_1(bst, bsh, offset + GP0, GP1_OUT);
   1226 	}
   1227 	DELAY(40000);
   1228 
   1229 	/*
   1230 	 * Disable source insertion.
   1231 	 * XXX Dingo does not have this bit, but Linux does it unconditionally.
   1232 	 */
   1233 	if (sc->sc_chipset < XI_CHIPSET_DINGO) {
   1234 		PAGE(sc, 0x42);
   1235 		bus_space_write_1(bst, bsh, offset + SWC0, 0x20);
   1236 	}
   1237 
   1238 	/* Set the local memory dividing line. */
   1239 	if (sc->sc_rev != 1) {
   1240 		PAGE(sc, 2);
   1241 		/* XXX Symbolic constant preferrable. */
   1242 		bus_space_write_2(bst, bsh, offset + RBS0, 0x2000);
   1243 	}
   1244 
   1245 	/*
   1246 	 * Apparently the receive byte pointer can be bad after a reset, so
   1247 	 * we hardwire it correctly.
   1248 	 */
   1249 	PAGE(sc, 0);
   1250 	bus_space_write_2(bst, bsh, offset + DO0, DO_CHG_OFFSET);
   1251 
   1252 	/* Setup ethernet MAC registers. XXX Symbolic constants. */
   1253 	PAGE(sc, 0x40);
   1254 	bus_space_write_1(bst, bsh, offset + RX0MSK,
   1255 	    PKT_TOO_LONG | CRC_ERR | RX_OVERRUN | RX_ABORT | RX_OK);
   1256 	bus_space_write_1(bst, bsh, offset + TX0MSK,
   1257 	    CARRIER_LOST | EXCESSIVE_COLL | TX_UNDERRUN | LATE_COLLISION |
   1258 	    SQE | TX_ABORT | TX_OK);
   1259 	if (sc->sc_chipset < XI_CHIPSET_DINGO)
   1260 		/* XXX From Linux, dunno what 0xb0 means. */
   1261 		bus_space_write_1(bst, bsh, offset + TX1MSK, 0xb0);
   1262 	bus_space_write_1(bst, bsh, offset + RXST0, 0);
   1263 	bus_space_write_1(bst, bsh, offset + TXST0, 0);
   1264 	bus_space_write_1(bst, bsh, offset + TXST1, 0);
   1265 
   1266 	PAGE(sc, 2);
   1267 
   1268 	/* Enable MII function if available. */
   1269 	x = 0;
   1270 	if (LIST_FIRST(&sc->sc_mii.mii_phys))
   1271 		x |= SELECT_MII;
   1272 	bus_space_write_1(bst, bsh, offset + MSR, x);
   1273 	DELAY(20000);
   1274 
   1275 	/* Configure the LED registers. */
   1276 	/* XXX This is not good for 10base2. */
   1277 	bus_space_write_1(bst, bsh, offset + LED,
   1278 	    (LED_TX_ACT << LED1_SHIFT) | (LED_10MB_LINK << LED0_SHIFT));
   1279 	if (sc->sc_chipset >= XI_CHIPSET_DINGO)
   1280 		bus_space_write_1(bst, bsh, offset + LED3,
   1281 		    LED_100MB_LINK << LED3_SHIFT);
   1282 
   1283 	/* Enable receiver and go online. */
   1284 	PAGE(sc, 0x40);
   1285 	bus_space_write_1(bst, bsh, offset + CMD0, ENABLE_RX | ONLINE);
   1286 
   1287 #if 0 /*XXXMYC*/
   1288 	/* XXX This is pure magic for me, found in the Linux driver. */
   1289 	if ((sc->sc_flags & (XIFLAGS_DINGO | XIFLAGS_MODEM)) == XIFLAGS_MODEM) {
   1290 		if ((bus_space_read_1(bst, bsh, offset + 0x10) & 0x01) == 0)
   1291 			/* Unmask the master interrupt bit. */
   1292 			bus_space_write_1(bst, bsh, offset + 0x10, 0x11);
   1293 	}
   1294 #endif
   1295 
   1296 	/*
   1297 	 * The Linux driver says this:
   1298 	 * We should switch back to page 0 to avoid a bug in revision 0
   1299 	 * where regs with offset below 8 can't be read after an access
   1300 	 * to the MAC registers.
   1301 	 */
   1302 	PAGE(sc, 0);
   1303 }
   1304