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