Home | History | Annotate | Line # | Download | only in ic
lan9118.c revision 1.9
      1 /*	$NetBSD: lan9118.c,v 1.9 2009/11/29 10:17:01 kiyohara Exp $	*/
      2 /*
      3  * Copyright (c) 2008 KIYOHARA Takashi
      4  * All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  *
     15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     18  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
     19  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     20  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     21  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     23  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
     24  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     25  * POSSIBILITY OF SUCH DAMAGE.
     26  */
     27 #include <sys/cdefs.h>
     28 __KERNEL_RCSID(0, "$NetBSD: lan9118.c,v 1.9 2009/11/29 10:17:01 kiyohara Exp $");
     29 
     30 /*
     31  * The LAN9118 Family
     32  *
     33  * * The LAN9118 is targeted for 32-bit applications requiring high
     34  *   performance, and provides the highest level of performance possible for
     35  *   a non-PCI 10/100 Ethernet controller.
     36  *
     37  * * The LAN9117 is designed to provide the highest level of performance
     38  *   possible for 16-bit applications. It also has an external MII interface,
     39  *   which can be used to attach an external PHY.
     40  *
     41  * * The LAN9116 and LAN9115 are designed for performance-sensitive
     42  *   applications with less intensive performance requirements. The LAN9116
     43  *   is for 32-bit host processors, while the LAN9115 is for 16-bit
     44  *   applications, which may also require an external PHY. Both devices
     45  *   deliver superior levels of performance.
     46  */
     47 
     48 #include "bpfilter.h"
     49 #include "rnd.h"
     50 
     51 #include <sys/param.h>
     52 #include <sys/callout.h>
     53 #include <sys/device.h>
     54 #include <sys/errno.h>
     55 #include <sys/bus.h>
     56 #include <sys/ioctl.h>
     57 #include <sys/kernel.h>
     58 #include <sys/proc.h>
     59 #include <sys/systm.h>
     60 
     61 #include <net/if.h>
     62 #include <net/if_ether.h>
     63 #include <net/if_media.h>
     64 
     65 #include <dev/mii/mii.h>
     66 #include <dev/mii/miivar.h>
     67 
     68 #if NBPFILTER > 0
     69 #include <net/bpf.h>
     70 #endif
     71 #if NRND > 0
     72 #include <sys/rnd.h>
     73 #endif
     74 
     75 #include <dev/ic/lan9118reg.h>
     76 #include <dev/ic/lan9118var.h>
     77 
     78 
     79 #ifdef SMSH_DEBUG
     80 #define DPRINTF(x)	if (smsh_debug) printf x
     81 #define DPRINTFN(n,x)	if (smsh_debug >= (n)) printf x
     82 int smsh_debug = SMSH_DEBUG;
     83 #else
     84 #define DPRINTF(x)
     85 #define DPRINTFN(n,x)
     86 #endif
     87 
     88 
     89 static void lan9118_start(struct ifnet *);
     90 static int lan9118_ioctl(struct ifnet *, u_long, void *);
     91 static int lan9118_init(struct ifnet *);
     92 static void lan9118_stop(struct ifnet *, int);
     93 static void lan9118_watchdog(struct ifnet *);
     94 
     95 static int lan9118_ifm_change(struct ifnet *);
     96 static void lan9118_ifm_status(struct ifnet *, struct ifmediareq *);
     97 
     98 static int lan9118_miibus_readreg(device_t, int, int);
     99 static void lan9118_miibus_writereg(device_t, int, int, int);
    100 static void lan9118_miibus_statchg(device_t);
    101 
    102 static uint16_t lan9118_mii_readreg(struct lan9118_softc *, int, int);
    103 static void lan9118_mii_writereg(struct lan9118_softc *, int, int, uint16_t);
    104 static uint32_t lan9118_mac_readreg(struct lan9118_softc *, int);
    105 static void lan9118_mac_writereg(struct lan9118_softc *, int, uint32_t);
    106 
    107 static void lan9118_set_filter(struct lan9118_softc *);
    108 static void lan9118_rxintr(struct lan9118_softc *);
    109 static void lan9118_txintr(struct lan9118_softc *);
    110 
    111 static void lan9118_tick(void *);
    112 
    113 /* This values refer from Linux's smc911x.c */
    114 static uint32_t afc_cfg[] = {
    115 	/* 0 */ 0x00000000,
    116 	/* 1 */ 0x00000000,
    117 	/* 2 */ 0x008c4600 | LAN9118_AFC_CFG_BACK_DUR(10) |
    118 		LAN9118_AFC_CFG_FCMULT | LAN9118_AFC_CFG_FCBRD |
    119 		LAN9118_AFC_CFG_FCADD | LAN9118_AFC_CFG_FCANY,
    120 	/* 3 */ 0x00824100 | LAN9118_AFC_CFG_BACK_DUR(9) |
    121 		LAN9118_AFC_CFG_FCMULT | LAN9118_AFC_CFG_FCBRD |
    122 		LAN9118_AFC_CFG_FCADD | LAN9118_AFC_CFG_FCANY,
    123 	/* 4 */ 0x00783c00 | LAN9118_AFC_CFG_BACK_DUR(9) |
    124 		LAN9118_AFC_CFG_FCMULT | LAN9118_AFC_CFG_FCBRD |
    125 		LAN9118_AFC_CFG_FCADD | LAN9118_AFC_CFG_FCANY,
    126 	/* 5 */ 0x006e3700 | LAN9118_AFC_CFG_BACK_DUR(8) |
    127 		LAN9118_AFC_CFG_FCMULT | LAN9118_AFC_CFG_FCBRD |
    128 		LAN9118_AFC_CFG_FCADD | LAN9118_AFC_CFG_FCANY,
    129 	/* 6 */ 0x00643200 | LAN9118_AFC_CFG_BACK_DUR(8) |
    130 		LAN9118_AFC_CFG_FCMULT | LAN9118_AFC_CFG_FCBRD |
    131 		LAN9118_AFC_CFG_FCADD | LAN9118_AFC_CFG_FCANY,
    132 	/* 7 */ 0x005a2d00 | LAN9118_AFC_CFG_BACK_DUR(7) |
    133 		LAN9118_AFC_CFG_FCMULT | LAN9118_AFC_CFG_FCBRD |
    134 		LAN9118_AFC_CFG_FCADD | LAN9118_AFC_CFG_FCANY,
    135 	/* 8 */ 0x00502800 | LAN9118_AFC_CFG_BACK_DUR(7) |
    136 		LAN9118_AFC_CFG_FCMULT | LAN9118_AFC_CFG_FCBRD |
    137 		LAN9118_AFC_CFG_FCADD | LAN9118_AFC_CFG_FCANY,
    138 	/* 9 */ 0x00462300 | LAN9118_AFC_CFG_BACK_DUR(6) |
    139 		LAN9118_AFC_CFG_FCMULT | LAN9118_AFC_CFG_FCBRD |
    140 		LAN9118_AFC_CFG_FCADD | LAN9118_AFC_CFG_FCANY,
    141 	/* a */ 0x003c1e00 | LAN9118_AFC_CFG_BACK_DUR(6) |
    142 		LAN9118_AFC_CFG_FCMULT | LAN9118_AFC_CFG_FCBRD |
    143 		LAN9118_AFC_CFG_FCADD | LAN9118_AFC_CFG_FCANY,
    144 	/* b */ 0x00321900 | LAN9118_AFC_CFG_BACK_DUR(5) |
    145 		LAN9118_AFC_CFG_FCMULT | LAN9118_AFC_CFG_FCBRD |
    146 		LAN9118_AFC_CFG_FCADD | LAN9118_AFC_CFG_FCANY,
    147 	/* c */ 0x00241200 | LAN9118_AFC_CFG_BACK_DUR(4) |
    148 		LAN9118_AFC_CFG_FCMULT | LAN9118_AFC_CFG_FCBRD |
    149 		LAN9118_AFC_CFG_FCADD | LAN9118_AFC_CFG_FCANY,
    150 	/* d */ 0x00150700 | LAN9118_AFC_CFG_BACK_DUR(3) |
    151 		LAN9118_AFC_CFG_FCMULT | LAN9118_AFC_CFG_FCBRD |
    152 		LAN9118_AFC_CFG_FCADD | LAN9118_AFC_CFG_FCANY,
    153 	/* e */ 0x00060300 | LAN9118_AFC_CFG_BACK_DUR(2) |
    154 		LAN9118_AFC_CFG_FCMULT | LAN9118_AFC_CFG_FCBRD |
    155 		LAN9118_AFC_CFG_FCADD | LAN9118_AFC_CFG_FCANY,
    156 	/* f */ 0x00000000,
    157 };
    158 
    159 
    160 int
    161 lan9118_attach(struct lan9118_softc *sc)
    162 {
    163 	struct ifnet *ifp = &sc->sc_ec.ec_if;
    164 	uint32_t val;
    165 	int timo, i;
    166 
    167 	if (sc->sc_flags & LAN9118_FLAGS_SWAP)
    168 		/* byte swap mode */
    169 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, LAN9118_WORD_SWAP,
    170 		    0xffffffff);
    171 	val = bus_space_read_4(sc->sc_iot, sc->sc_ioh, LAN9118_BYTE_TEST);
    172 	if (val != LAN9118_BYTE_TEST_VALUE) {
    173 		aprint_error(": failed to detect chip\n");
    174 		return EINVAL;
    175 	}
    176 
    177 	val = bus_space_read_4(sc->sc_iot, sc->sc_ioh, LAN9118_ID_REV);
    178 	sc->sc_id = LAN9118_ID_REV_ID(val);
    179 	sc->sc_rev = LAN9118_ID_REV_REV(val);
    180 
    181 #define LAN9xxx_ID(id)	((id) >= 0x9000 ? (id) & 0xfff : \
    182 			    ((id) >= 0x1000 ? ((id) >> 4) + 0x100 : (id)))
    183 
    184 	aprint_normal(": SMSC LAN9%03x Rev %d\n",
    185 	    LAN9xxx_ID(sc->sc_id), sc->sc_rev);
    186 
    187 	if (sc->sc_flags & LAN9118_FLAGS_SWAP)
    188 		aprint_normal_dev(sc->sc_dev, "byte swap mode\n");
    189 
    190 	timo = 3 * 1000 * 1000;	/* XXXX 3sec */
    191 	do {
    192 		val = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
    193 		    LAN9118_MAC_CSR_CMD);
    194 		if (!(val & LAN9118_MAC_CSR_CMD_BUSY))
    195 			break;
    196 		delay(100);
    197 	} while (timo -= 100);
    198 	if (timo <= 0)
    199 		aprint_error_dev(sc->sc_dev, "%s: command busy\n", __func__);
    200 	if (!(sc->sc_flags & LAN9118_FLAGS_NO_EEPROM)) {
    201 		/* Read auto-loaded MAC address */
    202 		val = lan9118_mac_readreg(sc, LAN9118_ADDRL);
    203 		sc->sc_enaddr[3] = (val >> 24) & 0xff;
    204 		sc->sc_enaddr[2] = (val >> 16) & 0xff;
    205 		sc->sc_enaddr[1] = (val >> 8) & 0xff;
    206 		sc->sc_enaddr[0] = val & 0xff;
    207 		val = lan9118_mac_readreg(sc, LAN9118_ADDRH);
    208 		sc->sc_enaddr[5] = (val >> 8) & 0xff;
    209 		sc->sc_enaddr[4] = val & 0xff;
    210 	}
    211 	aprint_normal_dev(sc->sc_dev, "MAC address %s\n",
    212 	    ether_sprintf(sc->sc_enaddr));
    213 
    214 	KASSERT(LAN9118_TX_FIF_SZ >= 2 && LAN9118_TX_FIF_SZ < 15);
    215 	sc->sc_afc_cfg = afc_cfg[LAN9118_TX_FIF_SZ];
    216 
    217 	/* Initialize the ifnet structure. */
    218 	strlcpy(ifp->if_xname, device_xname(sc->sc_dev), IFNAMSIZ);
    219 	ifp->if_softc = sc;
    220 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    221 	ifp->if_start = lan9118_start;
    222 	ifp->if_ioctl = lan9118_ioctl;
    223 	ifp->if_init = lan9118_init;
    224 	ifp->if_stop = lan9118_stop;
    225 	ifp->if_watchdog = lan9118_watchdog;
    226 	IFQ_SET_READY(&ifp->if_snd);
    227 
    228 #if 0	/* Not support 802.1Q VLAN-sized frames yet. */
    229 	sc->sc_ec.ec_capabilities |= ETHERCAP_VLAN_MTU;
    230 #endif
    231 
    232 	ifmedia_init(&sc->sc_mii.mii_media, 0,
    233 	    lan9118_ifm_change, lan9118_ifm_status);
    234 	/*
    235 	 * Number of instance of Internal PHY is always 0.  External PHY
    236 	 * number that above.
    237 	 */
    238 	sc->sc_mii.mii_instance++;
    239 	if (sc->sc_id == LAN9118_ID_9115 || sc->sc_id == LAN9118_ID_9117) {
    240 		if (bus_space_read_4(sc->sc_iot, sc->sc_ioh, LAN9118_HW_CFG) &
    241 		    LAN9118_HW_CFG_EXT_PHY_DET) {
    242 			/*
    243 			 * We always have a internal PHY at phy1.
    244 			 * In addition, external PHY is attached.
    245 			 */
    246 			DPRINTFN(1, ("%s: detect External PHY\n", __func__));
    247 
    248 			sc->sc_mii.mii_readreg = lan9118_miibus_readreg;
    249 			sc->sc_mii.mii_writereg = lan9118_miibus_writereg;
    250 			sc->sc_mii.mii_statchg = lan9118_miibus_statchg;
    251 
    252 			/* Switch MII and SMI */
    253 			bus_space_write_4(sc->sc_iot, sc->sc_ioh,
    254 			    LAN9118_HW_CFG,
    255 			    LAN9118_HW_CFG_MBO |
    256 			    LAN9118_HW_CFG_PHY_CLK_SEL_CD);
    257 			delay(1);	/* Wait 5 cycle */
    258 			bus_space_write_4(sc->sc_iot, sc->sc_ioh,
    259 			    LAN9118_HW_CFG,
    260 			    LAN9118_HW_CFG_MBO |
    261 			    LAN9118_HW_CFG_PHY_CLK_SEL_EMII |
    262 			    LAN9118_HW_CFG_SMI_SEL |
    263 			    LAN9118_HW_CFG_EXT_PHY_EN);
    264 			delay(1);	/* Once wait more 5 cycle */
    265 
    266 			/* Call mii_attach, avoid at phy1. */
    267 			mii_attach(sc->sc_dev, &sc->sc_mii, 0xffffffff,
    268 			    0, MII_OFFSET_ANY, 0);
    269 			for (i = 2; i < MII_NPHY; i++)
    270 				mii_attach(sc->sc_dev, &sc->sc_mii, 0xffffffff,
    271 				    i, MII_OFFSET_ANY, 0);
    272 		}
    273 	}
    274 	ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER | IFM_MANUAL, 0, NULL);
    275 	ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER | IFM_10_T, 0, NULL);
    276 	ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER | IFM_10_T | IFM_FDX, 0,
    277 	    NULL);
    278 	ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER | IFM_100_TX, 0, NULL);
    279 	ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER | IFM_100_TX | IFM_FDX, 0,
    280 	    NULL);
    281 	ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER | IFM_AUTO, 0, NULL);
    282 
    283 	aprint_normal_dev(sc->sc_dev,
    284 	    "10baseT, 10baseT-FDX, 100baseTX, 100baseTX-FDX, auto\n");
    285 	ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER | IFM_AUTO);
    286 
    287 	/* Attach the interface. */
    288 	if_attach(ifp);
    289 	ether_ifattach(ifp, sc->sc_enaddr);
    290 
    291 	callout_init(&sc->sc_tick, 0);
    292 
    293 #if NRND > 0
    294 	rnd_attach_source(&sc->rnd_source, device_xname(sc->sc_dev),
    295 	    RND_TYPE_NET, 0);
    296 #endif
    297 	return 0;
    298 }
    299 
    300 int
    301 lan9118_intr(void *arg)
    302 {
    303 	struct lan9118_softc *sc = (struct lan9118_softc *)arg;
    304 	struct ifnet *ifp = &sc->sc_ec.ec_if;
    305 	uint32_t int_sts, int_en, datum = 0;
    306 	int handled = 0;
    307 
    308 	for (;;) {
    309 		int_sts =
    310 		    bus_space_read_4(sc->sc_iot, sc->sc_ioh, LAN9118_INT_STS);
    311 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, LAN9118_INT_STS,
    312 		    int_sts);
    313 		int_en =
    314 		    bus_space_read_4(sc->sc_iot, sc->sc_ioh, LAN9118_INT_EN);
    315 
    316 		DPRINTFN(3, ("%s: int_sts=0x%x, int_en=0x%x\n",
    317 		    __func__, int_sts, int_en));
    318 
    319 		if (!(int_sts & int_en))
    320 			break;
    321 		datum = int_sts;
    322 
    323 #if 0	/* not yet... */
    324 		if (int_sts & LAN9118_INT_PHY_INT) { /* PHY */
    325 			/* Shall we need? */
    326 		}
    327 		if (int_sts & LAN9118_INT_PME_INT) { /*Power Management Event*/
    328 			/* not yet... */
    329 		}
    330 #endif
    331 		if (int_sts & LAN9118_INT_RXE) {
    332 			ifp->if_ierrors++;
    333 			aprint_error_ifnet(ifp, "Receive Error\n");
    334 		}
    335 		if (int_sts & LAN9118_INT_TSFL) /* TX Status FIFO Level */
    336 			lan9118_txintr(sc);
    337 		if (int_sts & LAN9118_INT_RXDF_INT) {
    338 			ifp->if_ierrors++;
    339 			aprint_error_ifnet(ifp, "RX Dropped Frame Interrupt\n");
    340 		}
    341 		if (int_sts & LAN9118_INT_RSFF) {
    342 			ifp->if_ierrors++;
    343 			aprint_error_ifnet(ifp, "RX Status FIFO Full\n");
    344 		}
    345 		if (int_sts & LAN9118_INT_RSFL) /* RX Status FIFO Level */
    346 			 lan9118_rxintr(sc);
    347 	}
    348 
    349 	if (!IFQ_IS_EMPTY(&ifp->if_snd))
    350 		lan9118_start(ifp);
    351 
    352 #if NRND > 0
    353 	if (RND_ENABLED(&sc->rnd_source))
    354 		rnd_add_uint32(&sc->rnd_source, datum);
    355 #endif
    356 
    357 	return handled;
    358 }
    359 
    360 
    361 static void
    362 lan9118_start(struct ifnet *ifp)
    363 {
    364 	struct lan9118_softc *sc = ifp->if_softc;
    365 	struct mbuf *m0, *m;
    366 	unsigned tdfree, totlen, dso;
    367 	uint32_t txa, txb;
    368 	uint8_t *p;
    369 	int n;
    370 
    371 	DPRINTFN(3, ("%s\n", __func__));
    372 
    373 	if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
    374 		return;
    375 
    376 	totlen = 0;
    377 	for (;;) {
    378 		IFQ_POLL(&ifp->if_snd, m0);
    379 		if (m0 == NULL)
    380 			break;
    381 
    382 		tdfree = LAN9118_TX_FIFO_INF_TDFREE(bus_space_read_4(sc->sc_iot,
    383 		    sc->sc_ioh, LAN9118_TX_FIFO_INF));
    384 		if (tdfree < 2036) {
    385 			/*
    386 			 * 2036 is the possible maximum FIFO consumption
    387 			 * for the most fragmented frame.
    388 			 */
    389 			ifp->if_flags |= IFF_OACTIVE;
    390 			break;
    391 		}
    392 
    393 		IFQ_DEQUEUE(&ifp->if_snd, m0);
    394 
    395 		/*
    396 		 * WE ARE NOW COMMITTED TO TRANSMITTING THE PACKET.
    397 		 */
    398 
    399 		/*
    400 		 * Check mbuf chain -- "middle" buffers must be >= 4 bytes
    401 		 * and maximum # of buffers is 86.
    402 		 */
    403 		m = m0;
    404 		n = 0;
    405 		while (m) {
    406 			if (m->m_len < 4 || ++n > 86) {
    407 				/* Copy mbuf chain. */
    408 				MGETHDR(m, M_DONTWAIT, MT_DATA);
    409 				if (m == NULL)
    410 					goto discard;   /* discard packet */
    411 				MCLGET(m, M_DONTWAIT);
    412 				if ((m->m_flags & M_EXT) == 0) {
    413 					m_freem(m);
    414 					goto discard;	/* discard packet */
    415 				}
    416 				m_copydata(m0, 0, m0->m_pkthdr.len,
    417 				    mtod(m, void *));
    418 				m->m_pkthdr.len = m->m_len = m0->m_pkthdr.len;
    419 				m_freem(m0);
    420 				m0 = m;
    421 				break;
    422 			}
    423 			m = m->m_next;
    424 		}
    425 
    426 		m = m0;
    427 		totlen = m->m_pkthdr.len;
    428 		p = mtod(m, uint8_t *);
    429 		dso = (unsigned)p & 0x3;
    430 		txa =
    431 		    LAN9118_TXC_A_BEA_4B	|
    432 		    LAN9118_TXC_A_DSO(dso)	|
    433 		    LAN9118_TXC_A_FS		|
    434 		    LAN9118_TXC_A_BS(m->m_len);
    435 		txb = LAN9118_TXC_B_PL(totlen);
    436 		while (m->m_next != NULL) {
    437 			bus_space_write_4(sc->sc_iot, sc->sc_ioh,
    438 			    LAN9118_TXDFIFOP, txa);
    439 			bus_space_write_4(sc->sc_iot, sc->sc_ioh,
    440 			    LAN9118_TXDFIFOP, txb);
    441 			bus_space_write_multi_4(sc->sc_iot, sc->sc_ioh,
    442 			    LAN9118_TXDFIFOP, (uint32_t *)(p - dso),
    443 			    (m->m_len + dso + 3) >> 2);
    444 
    445 			m = m->m_next;
    446 			p = mtod(m, uint8_t *);
    447 			dso = (unsigned)p & 0x3;
    448 			txa =
    449 			    LAN9118_TXC_A_BEA_4B	|
    450 			    LAN9118_TXC_A_DSO(dso)	|
    451 			    LAN9118_TXC_A_BS(m->m_len);
    452 		}
    453 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, LAN9118_TXDFIFOP,
    454 		    txa | LAN9118_TXC_A_LS);
    455 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, LAN9118_TXDFIFOP,
    456 		    txb);
    457 		bus_space_write_multi_4(sc->sc_iot, sc->sc_ioh,
    458 		    LAN9118_TXDFIFOP, (uint32_t *)(p - dso),
    459 		    (m->m_len + dso + 3) >> 2);
    460 
    461 discard:
    462 #if NBPFILTER > 0
    463 		/*
    464 		 * Pass the packet to any BPF listeners.
    465 		 */
    466 		if (ifp->if_bpf)
    467 			bpf_mtap(ifp->if_bpf, m0);
    468 #endif /* NBPFILTER > 0 */
    469 
    470 		m_freem(m0);
    471 	}
    472 	if (totlen > 0)
    473 		ifp->if_timer = 5;
    474 }
    475 
    476 static int
    477 lan9118_ioctl(struct ifnet *ifp, u_long command, void *data)
    478 {
    479 	struct lan9118_softc *sc = ifp->if_softc;
    480 	struct ifreq *ifr = data;
    481 	struct mii_data *mii = &sc->sc_mii;
    482 	int s, error = 0;
    483 
    484 	s = splnet();
    485 
    486 	switch (command) {
    487 	case SIOCSIFFLAGS:
    488 		DPRINTFN(2, ("%s: IFFLAGS\n", __func__));
    489 		if ((error = ifioctl_common(ifp, command, data)) != 0)
    490 			break;
    491 		switch (ifp->if_flags & (IFF_UP|IFF_RUNNING)) {
    492 		case IFF_RUNNING:
    493 			lan9118_stop(ifp, 0);
    494 			break;
    495 		case IFF_UP:
    496 			lan9118_init(ifp);
    497 			break;
    498 		default:
    499 			break;
    500 		}
    501 		error = 0;
    502 		break;
    503 
    504 	case SIOCGIFMEDIA:
    505 	case SIOCSIFMEDIA:
    506 		DPRINTFN(2, ("%s: MEDIA\n", __func__));
    507 		error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
    508 		break;
    509 
    510 	default:
    511 		DPRINTFN(2, ("%s: ETHER\n", __func__));
    512 		error = ether_ioctl(ifp, command, data);
    513 		if (error == ENETRESET) {
    514 			if (ifp->if_flags & IFF_RUNNING) {
    515 				lan9118_set_filter(sc);
    516 				DPRINTFN(2, ("%s set_filter called\n",
    517 				    __func__));
    518 			}
    519 			error = 0;
    520 		}
    521 		break;
    522 	}
    523 
    524 	splx(s);
    525 
    526 	return error;
    527 }
    528 
    529 static int
    530 lan9118_init(struct ifnet *ifp)
    531 {
    532 	struct lan9118_softc *sc = ifp->if_softc;
    533 	struct ifmedia *ifm = &sc->sc_mii.mii_media;
    534 	uint32_t reg, hw_cfg, mac_cr;
    535 	int timo, s;
    536 
    537 	DPRINTFN(2, ("%s\n", __func__));
    538 
    539 	s = splnet();
    540 
    541 	/* wait for PMT_CTRL[READY] */
    542 	timo = mstohz(5000);	/* XXXX 5sec */
    543 	while (!(bus_space_read_4(sc->sc_iot, sc->sc_ioh, LAN9118_PMT_CTRL) &
    544 	    LAN9118_PMT_CTRL_READY)) {
    545 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, LAN9118_BYTE_TEST,
    546 		    0xbad0c0de);
    547 		tsleep(&sc, PRIBIO, "lan9118_pmt_ready", 1);
    548 		if (--timo <= 0) {
    549 			splx(s);
    550 			return EBUSY;
    551 		}
    552 	}
    553 
    554 	/* Soft Reset */
    555 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, LAN9118_HW_CFG,
    556 	    LAN9118_HW_CFG_SRST);
    557 	do {
    558 		reg = bus_space_read_4(sc->sc_iot, sc->sc_ioh, LAN9118_HW_CFG);
    559 		if (reg & LAN9118_HW_CFG_SRST_TO) {
    560 			aprint_error_dev(sc->sc_dev,
    561 			    "soft reset timeouted out\n");
    562 			splx(s);
    563 			return ETIMEDOUT;
    564 		}
    565 	} while (reg & LAN9118_HW_CFG_SRST);
    566 
    567 	/* Set MAC and PHY CSRs */
    568 
    569 	if (sc->sc_flags & LAN9118_FLAGS_SWAP)
    570 		/* need byte swap */
    571 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, LAN9118_WORD_SWAP,
    572 		    0xffffffff);
    573 
    574 	while (bus_space_read_4(sc->sc_iot, sc->sc_ioh, LAN9118_E2P_CMD) &
    575 	    LAN9118_E2P_CMD_EPCB);
    576 	if (!(bus_space_read_4(sc->sc_iot, sc->sc_ioh, LAN9118_E2P_CMD) &
    577 	    LAN9118_E2P_CMD_MACAL)) {
    578 		lan9118_mac_writereg(sc, LAN9118_ADDRL,
    579 		    sc->sc_enaddr[0] |
    580 		    sc->sc_enaddr[1] << 8 |
    581 		    sc->sc_enaddr[2] << 16 |
    582 		    sc->sc_enaddr[3] << 24);
    583 		lan9118_mac_writereg(sc, LAN9118_ADDRH,
    584 		    sc->sc_enaddr[4] | sc->sc_enaddr[5] << 8);
    585 	}
    586 
    587 	if (ifm->ifm_media & IFM_FLOW) {
    588 		lan9118_mac_writereg(sc, LAN9118_FLOW,
    589 		    LAN9118_FLOW_FCPT(1) | LAN9118_FLOW_FCEN);
    590 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, LAN9118_AFC_CFG,
    591 		    sc->sc_afc_cfg);
    592 	}
    593 
    594 	lan9118_ifm_change(ifp);
    595 	hw_cfg = bus_space_read_4(sc->sc_iot, sc->sc_ioh, LAN9118_HW_CFG);
    596 	hw_cfg &= ~LAN9118_HW_CFG_TX_FIF_MASK;
    597 	hw_cfg |= LAN9118_HW_CFG_TX_FIF_SZ(LAN9118_TX_FIF_SZ);
    598 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, LAN9118_HW_CFG, hw_cfg);
    599 
    600 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, LAN9118_GPIO_CFG,
    601 	    LAN9118_GPIO_CFG_LEDX_EN(2)  |
    602 	    LAN9118_GPIO_CFG_LEDX_EN(1)  |
    603 	    LAN9118_GPIO_CFG_LEDX_EN(0)  |
    604 	    LAN9118_GPIO_CFG_GPIOBUFN(2) |
    605 	    LAN9118_GPIO_CFG_GPIOBUFN(1) |
    606 	    LAN9118_GPIO_CFG_GPIOBUFN(0));
    607 
    608 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, LAN9118_IRQ_CFG,
    609 	    LAN9118_IRQ_CFG_IRQ_EN);
    610 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, LAN9118_INT_STS,
    611 	    bus_space_read_4(sc->sc_iot, sc->sc_ioh, LAN9118_INT_STS));
    612 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, LAN9118_FIFO_INT,
    613 	    LAN9118_FIFO_INT_TXSL(0) | LAN9118_FIFO_INT_RXSL(0));
    614 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, LAN9118_INT_EN,
    615 #if 0	/* not yet... */
    616 	    LAN9118_INT_PHY_INT | /* PHY */
    617 	    LAN9118_INT_PME_INT | /* Power Management Event */
    618 #endif
    619 	    LAN9118_INT_RXE     | /* Receive Error */
    620 	    LAN9118_INT_TSFL    | /* TX Status FIFO Level */
    621 	    LAN9118_INT_RXDF_INT| /* RX Dropped Frame Interrupt */
    622 	    LAN9118_INT_RSFF    | /* RX Status FIFO Full */
    623 	    LAN9118_INT_RSFL);	  /* RX Status FIFO Level */
    624 
    625 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, LAN9118_RX_CFG,
    626 	    LAN9118_RX_CFG_RXDOFF(2));
    627 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, LAN9118_TX_CFG,
    628 	    LAN9118_TX_CFG_TX_ON);
    629 	mac_cr = lan9118_mac_readreg(sc, LAN9118_MAC_CR);
    630 	lan9118_mac_writereg(sc, LAN9118_MAC_CR,
    631 	    mac_cr | LAN9118_MAC_CR_TXEN | LAN9118_MAC_CR_RXEN);
    632 
    633 	ifp->if_flags |= IFF_RUNNING;
    634 	ifp->if_flags &= ~IFF_OACTIVE;
    635 
    636 	callout_reset(&sc->sc_tick, hz, lan9118_tick, sc);
    637 
    638 	splx(s);
    639 
    640 	return 0;
    641 }
    642 
    643 static void
    644 lan9118_stop(struct ifnet *ifp, int disable)
    645 {
    646 	struct lan9118_softc *sc = ifp->if_softc;
    647 	uint32_t cr;
    648 
    649 	DPRINTFN(2, ("%s\n", __func__));
    650 
    651 	/* Disable IRQ */
    652 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, LAN9118_INT_EN, 0);
    653 
    654 	/* Stopping transmitter */
    655 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, LAN9118_TX_CFG,
    656 	    LAN9118_TX_CFG_STOP_TX);
    657 	while (bus_space_read_4(sc->sc_iot, sc->sc_ioh, LAN9118_TX_CFG) &
    658 	    (LAN9118_TX_CFG_TX_ON | LAN9118_TX_CFG_STOP_TX));
    659 
    660 	/* Purge TX Status/Data FIFOs */
    661 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, LAN9118_TX_CFG,
    662 	    LAN9118_TX_CFG_TXS_DUMP | LAN9118_TX_CFG_TXD_DUMP);
    663 
    664 	/* Stopping receiver, also clear TXEN */
    665 	cr = lan9118_mac_readreg(sc, LAN9118_MAC_CR);
    666 	cr &= ~(LAN9118_MAC_CR_TXEN | LAN9118_MAC_CR_RXEN);
    667 	lan9118_mac_writereg(sc, LAN9118_MAC_CR, cr);
    668 	while (!(bus_space_read_4(sc->sc_iot, sc->sc_ioh, LAN9118_INT_STS) &
    669 	    LAN9118_INT_RXSTOP_INT));
    670 
    671 	/* Clear RX Status/Data FIFOs */
    672 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, LAN9118_RX_CFG,
    673 	    LAN9118_RX_CFG_RX_DUMP);
    674 
    675 	callout_stop(&sc->sc_tick);
    676 }
    677 
    678 static void
    679 lan9118_watchdog(struct ifnet *ifp)
    680 {
    681 	struct lan9118_softc *sc = ifp->if_softc;
    682 
    683 	/*
    684 	 * Reclaim first as there is a possibility of losing Tx completion
    685 	 * interrupts.
    686 	 */
    687 	lan9118_txintr(sc);
    688 
    689 	aprint_error_ifnet(ifp, "watchdog timeout\n");
    690 	ifp->if_oerrors++;
    691 
    692 	lan9118_init(ifp);
    693 }
    694 
    695 
    696 static int
    697 lan9118_ifm_change(struct ifnet *ifp)
    698 {
    699 	struct lan9118_softc *sc = ifp->if_softc;
    700 	struct mii_data *mii = &sc->sc_mii;
    701 	struct ifmedia *ifm = &mii->mii_media;
    702 	struct ifmedia_entry *ife = ifm->ifm_cur;
    703 	uint32_t pmt_ctrl, bmc, bms, ana, anlpa;
    704 	int i;
    705 
    706 	DPRINTFN(3, ("%s: ifm inst %d\n", __func__, IFM_INST(ife->ifm_media)));
    707 
    708 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, LAN9118_HW_CFG,
    709 	    LAN9118_HW_CFG_MBO | LAN9118_HW_CFG_PHY_CLK_SEL_CD);
    710 	delay(1);	/* Wait 5 cycle */
    711 
    712 	if (IFM_INST(ife->ifm_media) != 0) {
    713 		/* Use External PHY */
    714 
    715 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, LAN9118_HW_CFG,
    716 		    LAN9118_HW_CFG_MBO			|
    717 		    LAN9118_HW_CFG_PHY_CLK_SEL_EMII	|
    718 		    LAN9118_HW_CFG_SMI_SEL		|
    719 		    LAN9118_HW_CFG_EXT_PHY_EN);
    720 		delay(1);
    721 		return mii_mediachg(&sc->sc_mii);
    722 	}
    723 
    724 	/* Setup Internal PHY */
    725 
    726 	mii->mii_media_status = IFM_AVALID;
    727 	mii->mii_media_active = IFM_ETHER;
    728 
    729 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, LAN9118_HW_CFG,
    730 	    LAN9118_HW_CFG_MBO |
    731 	    LAN9118_HW_CFG_PHY_CLK_SEL_IPHY);
    732 	delay(1);
    733 
    734 	/* Reset PHY */
    735 	pmt_ctrl = bus_space_read_4(sc->sc_iot, sc->sc_ioh, LAN9118_PMT_CTRL);
    736 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, LAN9118_PMT_CTRL,
    737 	    pmt_ctrl | LAN9118_PMT_CTRL_PHY_RST);
    738 	while (bus_space_read_4(sc->sc_iot, sc->sc_ioh, LAN9118_PMT_CTRL) &
    739 	    LAN9118_PMT_CTRL_PHY_RST);
    740 
    741 	if (IFM_SUBTYPE(ifm->ifm_media) == IFM_AUTO) {
    742 		bmc = BMCR_AUTOEN | BMCR_STARTNEG;
    743 		bms = lan9118_mii_readreg(sc, LAN9118_IPHY_ADDR, MII_BMSR);
    744 		ana = ANAR_FC | BMSR_MEDIA_TO_ANAR(bms) | ANAR_CSMA;
    745 
    746 		lan9118_mii_writereg(sc, LAN9118_IPHY_ADDR, MII_ANAR, ana);
    747 		lan9118_mii_writereg(sc, LAN9118_IPHY_ADDR, MII_BMCR, bmc);
    748 
    749 		/* Wait 5sec for it to complete. */
    750 		for (i = 0; i < 5000; i++) {
    751 			if (lan9118_mii_readreg(sc, LAN9118_IPHY_ADDR, MII_BMSR)
    752 								& BMSR_ACOMP)
    753 				break;
    754 			delay(1000);
    755 		}
    756 		if (i == 5000) {
    757 			aprint_error_ifnet(ifp, "Auto-Negotiate failed\n");
    758 			return EIO;
    759 		}
    760 	} else {
    761 		switch (IFM_SUBTYPE(ifm->ifm_media)) {
    762 		case IFM_10_T:
    763 			bmc = BMCR_S10;
    764 			ana = ANAR_CSMA | ANAR_10;
    765 			break;
    766 
    767 		case IFM_100_TX:
    768 			bmc = BMCR_S100;
    769 			if (ifm->ifm_media & IFM_FDX)
    770 				bmc |= BMCR_FDX;
    771 			ana = ANAR_CSMA | ANAR_TX;
    772 			break;
    773 
    774 		case IFM_NONE:
    775 			bmc = BMCR_PDOWN;
    776 			break;
    777 
    778 		default:
    779 			return EINVAL;
    780 		}
    781 		if (ifm->ifm_media & IFM_FDX)
    782 			bmc |= BMCR_FDX;
    783 		if (ifm->ifm_media & IFM_FLOW)
    784 			ana |= ANAR_FC;
    785 
    786 		lan9118_mii_writereg(sc, LAN9118_IPHY_ADDR, MII_ANAR, ana);
    787 		lan9118_mii_writereg(sc, LAN9118_IPHY_ADDR, MII_BMCR, bmc);
    788 	}
    789 
    790 	bms = lan9118_mii_readreg(sc, LAN9118_IPHY_ADDR, MII_BMSR);
    791 	if (bms & BMSR_LINK) {
    792 		mii->mii_media_status |= IFM_ACTIVE;
    793 
    794 		bmc = lan9118_mii_readreg(sc, LAN9118_IPHY_ADDR, MII_BMCR);
    795 		if (bmc & BMCR_AUTOEN) {
    796 			anlpa = lan9118_mii_readreg(sc, LAN9118_IPHY_ADDR,
    797 			    MII_ANLPAR);
    798 			if (anlpa & ANLPAR_TX_FD)
    799 				mii->mii_media_active |= IFM_100_TX | IFM_FDX;
    800 			else if (anlpa & ANLPAR_T4)
    801 				mii->mii_media_active |= IFM_100_T4;
    802 			else if (anlpa & ANLPAR_TX)
    803 				mii->mii_media_active |= IFM_100_TX;
    804 			else if (anlpa & ANLPAR_10_FD)
    805 				mii->mii_media_active |= IFM_10_T|IFM_FDX;
    806 			else if (anlpa & ANLPAR_10)
    807 				mii->mii_media_active |= IFM_10_T;
    808 			else
    809 				mii->mii_media_active |= IFM_NONE;
    810 		} else
    811 			mii->mii_media_active = ife->ifm_media;
    812 
    813 		lan9118_miibus_statchg(sc->sc_dev);
    814 	}
    815 	return 0;
    816 }
    817 
    818 static void
    819 lan9118_ifm_status(struct ifnet *ifp, struct ifmediareq *ifmr)
    820 {
    821 	struct lan9118_softc *sc = ifp->if_softc;
    822 	struct mii_data *mii = &sc->sc_mii;
    823 	struct ifmedia *ifm = &mii->mii_media;
    824 	struct ifmedia_entry *ife = ifm->ifm_cur;
    825 	uint32_t bms, physcs;
    826 
    827 	DPRINTFN(3, ("%s\n", __func__));
    828 
    829 	if (IFM_INST(ife->ifm_media) != 0) {
    830 		mii_pollstat(mii);
    831 		ifmr->ifm_active = mii->mii_media_active;
    832 		ifmr->ifm_status = mii->mii_media_status;
    833 		return;
    834 	}
    835 
    836 	ifmr->ifm_active = IFM_ETHER;
    837 	ifmr->ifm_status = IFM_AVALID;
    838 
    839 	bms = lan9118_mii_readreg(sc, LAN9118_IPHY_ADDR, MII_BMSR);
    840 	if (!(bms & BMSR_LINK)) {
    841 		/* link is down */
    842 		ifmr->ifm_active |= IFM_NONE;
    843 		return;
    844 	}
    845 	ifmr->ifm_status |= IFM_ACTIVE;
    846 	physcs = lan9118_mii_readreg(sc, LAN9118_IPHY_ADDR, LAN9118_PHYSCSR);
    847 	if (IFM_SUBTYPE(ifm->ifm_media) == IFM_AUTO) {
    848 		if (!(physcs & LAN9118_PHYSCSR_AUTODONE)) {
    849 			/* negotiation in progress */
    850 			ifmr->ifm_active |= IFM_NONE;
    851 			return;
    852 		}
    853 	}
    854 	if (physcs & LAN9118_PHYSCSR_SI_10)
    855 		ifmr->ifm_active |= IFM_10_T;
    856 	if (physcs & LAN9118_PHYSCSR_SI_100)
    857 		ifmr->ifm_active |= IFM_100_TX;
    858 	if (physcs & LAN9118_PHYSCSR_SI_FDX)
    859 		ifmr->ifm_active |= IFM_FDX;
    860 }
    861 
    862 
    863 static int
    864 lan9118_miibus_readreg(device_t dev, int phy, int reg)
    865 {
    866 
    867 	return lan9118_mii_readreg(device_private(dev), phy, reg);
    868 }
    869 static void
    870 lan9118_miibus_writereg(device_t dev, int phy, int reg, int val)
    871 {
    872 
    873 	lan9118_mii_writereg(device_private(dev), phy, reg, val);
    874 }
    875 
    876 static void
    877 lan9118_miibus_statchg(device_t dev)
    878 {
    879 	struct lan9118_softc *sc = device_private(dev);
    880 	u_int cr;
    881 
    882 	cr = lan9118_mac_readreg(sc, LAN9118_MAC_CR);
    883 	if (IFM_OPTIONS(sc->sc_mii.mii_media_active) & IFM_FDX) {
    884 		cr &= ~LAN9118_MAC_CR_RCVOWN;
    885 		cr |= LAN9118_MAC_CR_FDPX;
    886 	} else {
    887 		cr |= LAN9118_MAC_CR_RCVOWN;
    888 		cr &= ~LAN9118_MAC_CR_FDPX;
    889 	}
    890 	lan9118_mac_writereg(sc, LAN9118_MAC_CR, cr);
    891 }
    892 
    893 
    894 static uint16_t
    895 lan9118_mii_readreg(struct lan9118_softc *sc, int phy, int reg)
    896 {
    897 	uint32_t acc;
    898 
    899 	while (lan9118_mac_readreg(sc, LAN9118_MII_ACC) &
    900 	    LAN9118_MII_ACC_MIIBZY);
    901 	acc = LAN9118_MII_ACC_PHYA(phy) | LAN9118_MII_ACC_MIIRINDA(reg);
    902 	lan9118_mac_writereg(sc, LAN9118_MII_ACC, acc);
    903 	while (lan9118_mac_readreg(sc, LAN9118_MII_ACC) &
    904 	    LAN9118_MII_ACC_MIIBZY);
    905 	return lan9118_mac_readreg(sc, LAN9118_MII_DATA);
    906 }
    907 
    908 static void
    909 lan9118_mii_writereg(struct lan9118_softc *sc, int phy, int reg, uint16_t val)
    910 {
    911 	uint32_t acc;
    912 
    913 	while (lan9118_mac_readreg(sc, LAN9118_MII_ACC) &
    914 	    LAN9118_MII_ACC_MIIBZY);
    915 	acc = LAN9118_MII_ACC_PHYA(phy) | LAN9118_MII_ACC_MIIRINDA(reg) |
    916 	    LAN9118_MII_ACC_MIIWNR;
    917 	lan9118_mac_writereg(sc, LAN9118_MII_DATA, val);
    918 	lan9118_mac_writereg(sc, LAN9118_MII_ACC, acc);
    919 	while (lan9118_mac_readreg(sc, LAN9118_MII_ACC) &
    920 	    LAN9118_MII_ACC_MIIBZY);
    921 }
    922 
    923 static uint32_t
    924 lan9118_mac_readreg(struct lan9118_softc *sc, int reg)
    925 {
    926 	uint32_t cmd;
    927 	int timo = 3 * 1000 * 1000;	/* XXXX: 3sec */
    928 
    929 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, LAN9118_MAC_CSR_CMD,
    930 	    LAN9118_MAC_CSR_CMD_BUSY | LAN9118_MAC_CSR_CMD_R | reg);
    931 	do {
    932 		cmd = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
    933 		    LAN9118_MAC_CSR_CMD);
    934 		if (!(cmd & LAN9118_MAC_CSR_CMD_BUSY))
    935 			break;
    936 		delay(100);
    937 	} while (timo -= 100);
    938 	if (timo <= 0)
    939 		aprint_error_dev(sc->sc_dev, "%s: command busy\n", __func__);
    940 	return bus_space_read_4(sc->sc_iot, sc->sc_ioh, LAN9118_MAC_CSR_DATA);
    941 }
    942 
    943 static void
    944 lan9118_mac_writereg(struct lan9118_softc *sc, int reg, uint32_t val)
    945 {
    946 	uint32_t cmd;
    947 	int timo = 3 * 1000 * 1000;	/* XXXX: 3sec */
    948 
    949 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, LAN9118_MAC_CSR_DATA, val);
    950 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, LAN9118_MAC_CSR_CMD,
    951 	    LAN9118_MAC_CSR_CMD_BUSY | LAN9118_MAC_CSR_CMD_W | reg);
    952 	do {
    953 		cmd = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
    954 		    LAN9118_MAC_CSR_CMD);
    955 		if (!(cmd & LAN9118_MAC_CSR_CMD_BUSY))
    956 			break;
    957 		delay(100);
    958 	} while (timo -= 100);
    959 	if (timo <= 0)
    960 		aprint_error_dev(sc->sc_dev, "%s: command busy\n", __func__);
    961 }
    962 
    963 
    964 static void
    965 lan9118_set_filter(struct lan9118_softc *sc)
    966 {
    967 	struct ether_multistep step;
    968 	struct ether_multi *enm;
    969 	struct ifnet *ifp = &sc->sc_ec.ec_if;
    970 	uint32_t mac_cr, h, hashes[2] = { 0, 0 };
    971 
    972 	mac_cr = lan9118_mac_readreg(sc, LAN9118_MAC_CR);
    973 	if (ifp->if_flags & IFF_PROMISC) {
    974 		lan9118_mac_writereg(sc, LAN9118_MAC_CR,
    975 		    mac_cr | LAN9118_MAC_CR_PRMS);
    976 		return;
    977 	}
    978 
    979 	mac_cr &= ~(LAN9118_MAC_CR_PRMS | LAN9118_MAC_CR_MCPAS |
    980 	    LAN9118_MAC_CR_BCAST | LAN9118_MAC_CR_HPFILT);
    981 	if (!(ifp->if_flags & IFF_BROADCAST))
    982 		mac_cr |= LAN9118_MAC_CR_BCAST;
    983 
    984 	if (ifp->if_flags & IFF_ALLMULTI)
    985 		mac_cr |= LAN9118_MAC_CR_MCPAS;
    986 	else {
    987 		ETHER_FIRST_MULTI(step, &sc->sc_ec, enm);
    988 		while (enm != NULL) {
    989 			if (memcmp(enm->enm_addrlo, enm->enm_addrhi,
    990 			    ETHER_ADDR_LEN) != 0) {
    991 				/*
    992 				 * We must listen to a range of multicast
    993 				 * addresses.  For now, just accept all
    994 				 * multicasts, rather than trying to set
    995 				 * only those filter bits needed to match
    996 				 * the range.  (At this time, the only use
    997 				 * of address ranges is for IP multicast
    998 				 * routing, for which the range is big enough
    999 				 * to require all bits set.)
   1000 				 */
   1001 				ifp->if_flags |= IFF_ALLMULTI;
   1002 				mac_cr |= LAN9118_MAC_CR_MCPAS;
   1003 				break;
   1004 			}
   1005 			h = ether_crc32_le(enm->enm_addrlo,
   1006 			    ETHER_ADDR_LEN) >> 26;
   1007 			hashes[h >> 5] |= 1 << (h & 0x1f);
   1008 
   1009 			mac_cr |= LAN9118_MAC_CR_HPFILT;
   1010 			ETHER_NEXT_MULTI(step, enm);
   1011 		}
   1012 		if (mac_cr & LAN9118_MAC_CR_HPFILT) {
   1013 			lan9118_mac_writereg(sc, LAN9118_HASHH, hashes[1]);
   1014 			lan9118_mac_writereg(sc, LAN9118_HASHL, hashes[0]);
   1015 		}
   1016 	}
   1017 	lan9118_mac_writereg(sc, LAN9118_MAC_CR, mac_cr);
   1018 	return;
   1019 }
   1020 
   1021 static void
   1022 lan9118_rxintr(struct lan9118_softc *sc)
   1023 {
   1024 	struct ifnet *ifp = &sc->sc_ec.ec_if;
   1025 	struct mbuf *m;
   1026 	uint32_t rx_fifo_inf, rx_status;
   1027 	int pktlen;
   1028 	const int pad = ETHER_HDR_LEN % sizeof(uint32_t);
   1029 
   1030 	DPRINTFN(3, ("%s\n", __func__));
   1031 
   1032 	for (;;) {
   1033 		rx_fifo_inf = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
   1034 		    LAN9118_RX_FIFO_INF);
   1035 		if (LAN9118_RX_FIFO_INF_RXSUSED(rx_fifo_inf) == 0)
   1036 			break;
   1037 
   1038 		rx_status = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
   1039 		    LAN9118_RXSFIFOP);
   1040 		pktlen = LAN9118_RXS_PKTLEN(rx_status);
   1041 		DPRINTFN(3, ("%s: rx_status=0x%x(pktlen %d)\n",
   1042 		    __func__, rx_status, pktlen));
   1043 		if (rx_status & (LAN9118_RXS_ES | LAN9118_RXS_LENERR |
   1044 		    LAN9118_RXS_RWTO | LAN9118_RXS_MIIERR | LAN9118_RXS_DBIT)) {
   1045 			if (rx_status & LAN9118_RXS_LENERR)
   1046 				aprint_error_dev(sc->sc_dev, "Length Error\n");
   1047 			if (rx_status & LAN9118_RXS_RUNTF)
   1048 				aprint_error_dev(sc->sc_dev, "Runt Frame\n");
   1049 			if (rx_status & LAN9118_RXS_FTL)
   1050 				aprint_error_dev(sc->sc_dev,
   1051 				    "Frame Too Long\n");
   1052 			if (rx_status & LAN9118_RXS_RWTO)
   1053 				aprint_error_dev(sc->sc_dev,
   1054 				    "Receive Watchdog time-out\n");
   1055 			if (rx_status & LAN9118_RXS_MIIERR)
   1056 				aprint_error_dev(sc->sc_dev, "MII Error\n");
   1057 			if (rx_status & LAN9118_RXS_DBIT)
   1058 				aprint_error_dev(sc->sc_dev, "Drabbling Bit\n");
   1059 			if (rx_status & LAN9118_RXS_COLS)
   1060 				aprint_error_dev(sc->sc_dev,
   1061 				    "Collision Seen\n");
   1062 			if (rx_status & LAN9118_RXS_CRCERR)
   1063 				aprint_error_dev(sc->sc_dev, "CRC Error\n");
   1064 
   1065 dropit:
   1066 			ifp->if_ierrors++;
   1067 			/*
   1068 			 * Receive Data FIFO Fast Forward
   1069 			 * When performing a fast-forward, there must be at
   1070 			 * least 4 DWORDs of data in the RX data FIFO for the
   1071 			 * packet being discarded.
   1072 			 */
   1073 			if (pktlen >= 4 * sizeof(uint32_t)) {
   1074 				uint32_t rx_dp_ctl;
   1075 
   1076 				bus_space_write_4(sc->sc_iot, sc->sc_ioh,
   1077 				    LAN9118_RX_DP_CTL,
   1078 				    LAN9118_RX_DP_CTL_RX_FFWD);
   1079 				/* DP_FFWD bit is self clearing */
   1080 				do {
   1081 					rx_dp_ctl = bus_space_read_4(sc->sc_iot,
   1082 					    sc->sc_ioh, LAN9118_RX_DP_CTL);
   1083 				} while (rx_dp_ctl & LAN9118_RX_DP_CTL_RX_FFWD);
   1084 			} else {
   1085 				/* For less than 4 DWORDs do not use RX_FFWD. */
   1086 				uint32_t garbage[4];
   1087 
   1088 				bus_space_read_multi_4(sc->sc_iot, sc->sc_ioh,
   1089 				    LAN9118_RXDFIFOP, garbage,
   1090 				    roundup(pktlen, 4) >> 2);
   1091 			}
   1092 			continue;
   1093 		}
   1094 
   1095 		MGETHDR(m, M_DONTWAIT, MT_DATA);
   1096 		if (m == NULL)
   1097 			goto dropit;
   1098 		if (pktlen > (MHLEN - pad)) {
   1099 			MCLGET(m, M_DONTWAIT);
   1100 			if ((m->m_flags & M_EXT) == 0) {
   1101 				m_freem(m);
   1102 				goto dropit;
   1103 			}
   1104 		}
   1105 
   1106 		/* STRICT_ALIGNMENT */
   1107 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, LAN9118_RX_CFG,
   1108 		    LAN9118_RX_CFG_RXEA_4B | LAN9118_RX_CFG_RXDOFF(pad));
   1109 		bus_space_read_multi_4(sc->sc_iot, sc->sc_ioh, LAN9118_RXDFIFOP,
   1110 		    mtod(m, uint32_t *),
   1111 		    roundup(pad + pktlen, sizeof(uint32_t)) >> 2);
   1112 		m->m_data += pad;
   1113 
   1114 		ifp->if_ipackets++;
   1115 		m->m_pkthdr.rcvif = ifp;
   1116 		m->m_pkthdr.len = m->m_len = (pktlen - ETHER_CRC_LEN);
   1117 
   1118 #if NBPFILTER > 0
   1119 		/*
   1120 		 * Pass this up to any BPF listeners, but only
   1121 		 * pass if up the stack if it's for us.
   1122 		 */
   1123 		if (ifp->if_bpf)
   1124 			bpf_mtap(ifp->if_bpf, m);
   1125 #endif /* NBPFILTER > 0 */
   1126 
   1127 		/* Pass it on. */
   1128 		(*ifp->if_input)(ifp, m);
   1129 	}
   1130 }
   1131 
   1132 static void
   1133 lan9118_txintr(struct lan9118_softc *sc)
   1134 {
   1135 	struct ifnet *ifp = &sc->sc_ec.ec_if;
   1136 	uint32_t tx_fifo_inf, tx_status;
   1137 	int fdx = IFM_OPTIONS(sc->sc_mii.mii_media_active) & IFM_FDX;
   1138 	int tdfree;
   1139 
   1140 	DPRINTFN(3, ("%s\n", __func__));
   1141 
   1142 	for (;;) {
   1143 		tx_fifo_inf = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
   1144 		    LAN9118_TX_FIFO_INF);
   1145 		if (LAN9118_TX_FIFO_INF_TXSUSED(tx_fifo_inf) == 0)
   1146 			break;
   1147 
   1148 		tx_status = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
   1149 		    LAN9118_TXSFIFOP);
   1150 		DPRINTFN(3, ("%s: tx_status=0x%x\n", __func__, tx_status));
   1151 		if (tx_status & LAN9118_TXS_ES) {
   1152 			if (tx_status & LAN9118_TXS_LOC)
   1153 				aprint_error_dev(sc->sc_dev,
   1154 				    "Loss Of Carrier\n");
   1155 			if ((tx_status & LAN9118_TXS_NC) && !fdx)
   1156 				aprint_error_dev(sc->sc_dev, "No Carrier\n");
   1157 			if (tx_status & LAN9118_TXS_LCOL)
   1158 				aprint_error_dev(sc->sc_dev,
   1159 				    "Late Collision\n");
   1160 			if (tx_status & LAN9118_TXS_ECOL) {
   1161 				/* Rearch 16 collision */
   1162 				ifp->if_collisions += 16;
   1163 				aprint_error_dev(sc->sc_dev,
   1164 				    "Excessive Collision\n");
   1165 			}
   1166 			if (LAN9118_TXS_COLCNT(tx_status) != 0)
   1167 				aprint_error_dev(sc->sc_dev,
   1168 				    "Collision Count: %d\n",
   1169 				    LAN9118_TXS_COLCNT(tx_status));
   1170 			if (tx_status & LAN9118_TXS_ED)
   1171 				aprint_error_dev(sc->sc_dev,
   1172 				    "Excessive Deferral\n");
   1173 			if (tx_status & LAN9118_TXS_DEFERRED)
   1174 				aprint_error_dev(sc->sc_dev, "Deferred\n");
   1175 			ifp->if_oerrors++;
   1176 		} else
   1177 			ifp->if_opackets++;
   1178 	}
   1179 
   1180 	tdfree = LAN9118_TX_FIFO_INF_TDFREE(tx_fifo_inf);
   1181 	if (tdfree == LAN9118_TX_DATA_FIFO_SIZE)
   1182 		/* FIFO empty */
   1183 		ifp->if_timer = 0;
   1184 	if (tdfree >= 2036)
   1185 		/*
   1186 		 * 2036 is the possible maximum FIFO consumption
   1187 		 * for the most fragmented frame.
   1188 		 */
   1189 		ifp->if_flags &= ~IFF_OACTIVE;
   1190 }
   1191 
   1192 void
   1193 lan9118_tick(void *v)
   1194 {
   1195 	struct lan9118_softc *sc = v;
   1196 	int s;
   1197 
   1198 	s = splnet();
   1199 	mii_tick(&sc->sc_mii);
   1200 	callout_schedule(&sc->sc_tick, hz);
   1201 	splx(s);
   1202 }
   1203