xref: /netbsd-src/sys/arch/arm/ep93xx/epe.c (revision bdc22b2e01993381dcefeff2bc9b56ca75a4235c)
1 /*	$NetBSD: epe.c,v 1.38 2018/06/26 06:47:57 msaitoh Exp $	*/
2 
3 /*
4  * Copyright (c) 2004 Jesse Off
5  * 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  *
16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: epe.c,v 1.38 2018/06/26 06:47:57 msaitoh Exp $");
31 
32 #include <sys/types.h>
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/ioctl.h>
36 #include <sys/kernel.h>
37 #include <sys/proc.h>
38 #include <sys/malloc.h>
39 #include <sys/time.h>
40 #include <sys/device.h>
41 #include <uvm/uvm_extern.h>
42 
43 #include <sys/bus.h>
44 #include <machine/intr.h>
45 
46 #include <arm/cpufunc.h>
47 
48 #include <arm/ep93xx/epsocvar.h>
49 #include <arm/ep93xx/ep93xxvar.h>
50 
51 #include <net/if.h>
52 #include <net/if_dl.h>
53 #include <net/if_types.h>
54 #include <net/if_media.h>
55 #include <net/if_ether.h>
56 #include <net/bpf.h>
57 
58 #include <dev/mii/mii.h>
59 #include <dev/mii/miivar.h>
60 
61 #ifdef INET
62 #include <netinet/in.h>
63 #include <netinet/in_systm.h>
64 #include <netinet/in_var.h>
65 #include <netinet/ip.h>
66 #include <netinet/if_inarp.h>
67 #endif
68 
69 #include <arm/ep93xx/ep93xxreg.h>
70 #include <arm/ep93xx/epereg.h>
71 #include <arm/ep93xx/epevar.h>
72 
73 #define DEFAULT_MDCDIV	32
74 
75 #ifndef EPE_FAST
76 #define EPE_FAST
77 #endif
78 
79 #ifndef EPE_FAST
80 #define EPE_READ(x) \
81 	bus_space_read_4(sc->sc_iot, sc->sc_ioh, (EPE_ ## x))
82 #define EPE_WRITE(x, y) \
83 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, (EPE_ ## x), (y))
84 #define CTRLPAGE_DMASYNC(x, y, z) \
85 	bus_dmamap_sync(sc->sc_dmat, sc->ctrlpage_dmamap, (x), (y), (z))
86 #else
87 #define EPE_READ(x) *(volatile uint32_t *) \
88 	(EP93XX_AHB_VBASE + EP93XX_AHB_EPE + (EPE_ ## x))
89 #define EPE_WRITE(x, y) *(volatile uint32_t *) \
90 	(EP93XX_AHB_VBASE + EP93XX_AHB_EPE + (EPE_ ## x)) = y
91 #define CTRLPAGE_DMASYNC(x, y, z)
92 #endif /* ! EPE_FAST */
93 
94 static int	epe_match(device_t , cfdata_t, void *);
95 static void	epe_attach(device_t, device_t, void *);
96 static void	epe_init(struct epe_softc *);
97 static int      epe_intr(void* arg);
98 static int	epe_gctx(struct epe_softc *);
99 static int	epe_mediachange(struct ifnet *);
100 int		epe_mii_readreg (device_t, int, int);
101 void		epe_mii_writereg (device_t, int, int, int);
102 void		epe_statchg (struct ifnet *);
103 void		epe_tick (void *);
104 static int	epe_ifioctl (struct ifnet *, u_long, void *);
105 static void	epe_ifstart (struct ifnet *);
106 static void	epe_ifwatchdog (struct ifnet *);
107 static int	epe_ifinit (struct ifnet *);
108 static void	epe_ifstop (struct ifnet *, int);
109 static void	epe_setaddr (struct ifnet *);
110 
111 CFATTACH_DECL_NEW(epe, sizeof(struct epe_softc),
112     epe_match, epe_attach, NULL, NULL);
113 
114 static int
115 epe_match(device_t parent, cfdata_t match, void *aux)
116 {
117 	return 2;
118 }
119 
120 static void
121 epe_attach(device_t parent, device_t self, void *aux)
122 {
123 	struct epe_softc		*sc = device_private(self);
124 	struct epsoc_attach_args	*sa;
125 	prop_data_t			 enaddr;
126 
127 	aprint_normal("\n");
128 	sa = aux;
129 	sc->sc_dev = self;
130 	sc->sc_iot = sa->sa_iot;
131 	sc->sc_intr = sa->sa_intr;
132 	sc->sc_dmat = sa->sa_dmat;
133 
134 	if (bus_space_map(sa->sa_iot, sa->sa_addr, sa->sa_size,
135 		0, &sc->sc_ioh))
136 		panic("%s: Cannot map registers", device_xname(self));
137 
138 	/* Fetch the Ethernet address from property if set. */
139 	enaddr = prop_dictionary_get(device_properties(self), "mac-address");
140 	if (enaddr != NULL) {
141 		KASSERT(prop_object_type(enaddr) == PROP_TYPE_DATA);
142 		KASSERT(prop_data_size(enaddr) == ETHER_ADDR_LEN);
143 		memcpy(sc->sc_enaddr, prop_data_data_nocopy(enaddr),
144 		       ETHER_ADDR_LEN);
145 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, EPE_AFP, 0);
146 		bus_space_write_region_1(sc->sc_iot, sc->sc_ioh, EPE_IndAd,
147 					 sc->sc_enaddr, ETHER_ADDR_LEN);
148 	}
149 
150         ep93xx_intr_establish(sc->sc_intr, IPL_NET, epe_intr, sc);
151 	epe_init(sc);
152 }
153 
154 static int
155 epe_gctx(struct epe_softc *sc)
156 {
157 	struct ifnet * ifp = &sc->sc_ec.ec_if;
158 	uint32_t *cur, ndq = 0;
159 
160 	/* Handle transmit completions */
161 	cur = (uint32_t *)(EPE_READ(TXStsQCurAdd) -
162 		sc->ctrlpage_dsaddr + (char*)sc->ctrlpage);
163 
164 	if (sc->TXStsQ_cur != cur) {
165 		CTRLPAGE_DMASYNC(TX_QLEN * 2 * sizeof(uint32_t),
166 			TX_QLEN * sizeof(uint32_t), BUS_DMASYNC_PREREAD);
167 	} else {
168 		return 0;
169 	}
170 
171 	do {
172 		uint32_t tbi = *sc->TXStsQ_cur & 0x7fff;
173 		struct mbuf *m = sc->txq[tbi].m;
174 
175 		if ((*sc->TXStsQ_cur & TXStsQ_TxWE) == 0) {
176 			ifp->if_oerrors++;
177 		}
178 		bus_dmamap_unload(sc->sc_dmat, sc->txq[tbi].m_dmamap);
179 		m_freem(m);
180 		do {
181 			sc->txq[tbi].m = NULL;
182 			ndq++;
183 			tbi = (tbi + 1) % TX_QLEN;
184 		} while (sc->txq[tbi].m == m);
185 
186 		ifp->if_opackets++;
187 		sc->TXStsQ_cur++;
188 		if (sc->TXStsQ_cur >= sc->TXStsQ + TX_QLEN) {
189 			sc->TXStsQ_cur = sc->TXStsQ;
190 		}
191 	} while (sc->TXStsQ_cur != cur);
192 
193 	sc->TXDQ_avail += ndq;
194 	if (ifp->if_flags & IFF_OACTIVE) {
195 		ifp->if_flags &= ~IFF_OACTIVE;
196 		/* Disable end-of-tx-chain interrupt */
197 		EPE_WRITE(IntEn, IntEn_REOFIE);
198 	}
199 	return ndq;
200 }
201 
202 static int
203 epe_intr(void *arg)
204 {
205 	struct epe_softc *sc = (struct epe_softc *)arg;
206 	struct ifnet * ifp = &sc->sc_ec.ec_if;
207 	uint32_t ndq = 0, irq, *cur;
208 
209 	irq = EPE_READ(IntStsC);
210 begin:
211 	cur = (uint32_t *)(EPE_READ(RXStsQCurAdd) -
212 		sc->ctrlpage_dsaddr + (char*)sc->ctrlpage);
213 	CTRLPAGE_DMASYNC(TX_QLEN * 3 * sizeof(uint32_t),
214 		RX_QLEN * 4 * sizeof(uint32_t),
215 		BUS_DMASYNC_PREREAD);
216 	while (sc->RXStsQ_cur != cur) {
217 		if ((sc->RXStsQ_cur[0] & (RXStsQ_RWE|RXStsQ_RFP|RXStsQ_EOB)) ==
218 			(RXStsQ_RWE|RXStsQ_RFP|RXStsQ_EOB)) {
219 			uint32_t bi = (sc->RXStsQ_cur[1] >> 16) & 0x7fff;
220 			uint32_t fl = sc->RXStsQ_cur[1] & 0xffff;
221 			struct mbuf *m;
222 
223 			MGETHDR(m, M_DONTWAIT, MT_DATA);
224 			if (m != NULL) MCLGET(m, M_DONTWAIT);
225 			if (m != NULL && (m->m_flags & M_EXT)) {
226 				bus_dmamap_unload(sc->sc_dmat,
227 					sc->rxq[bi].m_dmamap);
228 				m_set_rcvif(sc->rxq[bi].m, ifp);
229 				sc->rxq[bi].m->m_pkthdr.len =
230 					sc->rxq[bi].m->m_len = fl;
231 				if_percpuq_enqueue(ifp->if_percpuq,
232 				    sc->rxq[bi].m);
233 				sc->rxq[bi].m = m;
234 				bus_dmamap_load(sc->sc_dmat,
235 					sc->rxq[bi].m_dmamap,
236 					m->m_ext.ext_buf, MCLBYTES,
237 					NULL, BUS_DMA_NOWAIT);
238 				sc->RXDQ[bi * 2] =
239 					sc->rxq[bi].m_dmamap->dm_segs[0].ds_addr;
240 			} else {
241 				/* Drop packets until we can get replacement
242 				 * empty mbufs for the RXDQ.
243 				 */
244 				if (m != NULL) {
245 					m_freem(m);
246 				}
247 				ifp->if_ierrors++;
248 			}
249 		} else {
250 			ifp->if_ierrors++;
251 		}
252 
253 		ndq++;
254 
255 		sc->RXStsQ_cur += 2;
256 		if (sc->RXStsQ_cur >= sc->RXStsQ + (RX_QLEN * 2)) {
257 			sc->RXStsQ_cur = sc->RXStsQ;
258 		}
259 	}
260 
261 	if (ndq > 0) {
262 		ifp->if_ipackets += ndq;
263 		CTRLPAGE_DMASYNC(TX_QLEN * 3 * sizeof(uint32_t),
264  			RX_QLEN * 4 * sizeof(uint32_t),
265 			BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
266 		EPE_WRITE(RXStsEnq, ndq);
267 		EPE_WRITE(RXDEnq, ndq);
268 		ndq = 0;
269 	}
270 
271 	if (epe_gctx(sc) > 0 && IFQ_IS_EMPTY(&ifp->if_snd) == 0) {
272 		if_schedule_deferred_start(ifp);
273 	}
274 
275 	irq = EPE_READ(IntStsC);
276 	if ((irq & (IntSts_RxSQ|IntSts_ECI)) != 0)
277 		goto begin;
278 
279 	return (1);
280 }
281 
282 
283 static void
284 epe_init(struct epe_softc *sc)
285 {
286 	bus_dma_segment_t segs;
287 	char *addr;
288 	int rsegs, err, i;
289 	struct ifnet * ifp = &sc->sc_ec.ec_if;
290 	int mdcdiv = DEFAULT_MDCDIV;
291 
292 	callout_init(&sc->epe_tick_ch, 0);
293 
294 	/* Select primary Individual Address in Address Filter Pointer */
295 	EPE_WRITE(AFP, 0);
296 	/* Read ethernet MAC, should already be set by bootrom */
297 	bus_space_read_region_1(sc->sc_iot, sc->sc_ioh, EPE_IndAd,
298 		sc->sc_enaddr, ETHER_ADDR_LEN);
299 	aprint_normal_dev(sc->sc_dev, "MAC address %s\n",
300 		ether_sprintf(sc->sc_enaddr));
301 
302 	/* Soft Reset the MAC */
303 	EPE_WRITE(SelfCtl, SelfCtl_RESET);
304 	while(EPE_READ(SelfCtl) & SelfCtl_RESET);
305 
306 	/* suggested magic initialization values from datasheet */
307 	EPE_WRITE(RXBufThrshld, 0x800040);
308 	EPE_WRITE(TXBufThrshld, 0x200010);
309 	EPE_WRITE(RXStsThrshld, 0x40002);
310 	EPE_WRITE(TXStsThrshld, 0x40002);
311 	EPE_WRITE(RXDThrshld, 0x40002);
312 	EPE_WRITE(TXDThrshld, 0x40002);
313 
314 	/* Allocate a page of memory for descriptor and status queues */
315 	err = bus_dmamem_alloc(sc->sc_dmat, PAGE_SIZE, 0, PAGE_SIZE,
316 		&segs, 1, &rsegs, BUS_DMA_WAITOK);
317 	if (err == 0) {
318 		err = bus_dmamem_map(sc->sc_dmat, &segs, 1, PAGE_SIZE,
319 			&sc->ctrlpage, (BUS_DMA_WAITOK|BUS_DMA_COHERENT));
320 	}
321 	if (err == 0) {
322 		err = bus_dmamap_create(sc->sc_dmat, PAGE_SIZE, 1, PAGE_SIZE,
323 			0, BUS_DMA_WAITOK, &sc->ctrlpage_dmamap);
324 	}
325 	if (err == 0) {
326 		err = bus_dmamap_load(sc->sc_dmat, sc->ctrlpage_dmamap,
327 			sc->ctrlpage, PAGE_SIZE, NULL, BUS_DMA_WAITOK);
328 	}
329 	if (err != 0) {
330 		panic("%s: Cannot get DMA memory", device_xname(sc->sc_dev));
331 	}
332 	sc->ctrlpage_dsaddr = sc->ctrlpage_dmamap->dm_segs[0].ds_addr;
333 	memset(sc->ctrlpage, 0, PAGE_SIZE);
334 
335 	/* Set up pointers to start of each queue in kernel addr space.
336 	 * Each descriptor queue or status queue entry uses 2 words
337 	 */
338 	sc->TXDQ = (uint32_t *)sc->ctrlpage;
339 	sc->TXDQ_cur = sc->TXDQ;
340 	sc->TXDQ_avail = TX_QLEN - 1;
341 	sc->TXStsQ = &sc->TXDQ[TX_QLEN * 2];
342 	sc->TXStsQ_cur = sc->TXStsQ;
343 	sc->RXDQ = &sc->TXStsQ[TX_QLEN];
344 	sc->RXStsQ = &sc->RXDQ[RX_QLEN * 2];
345 	sc->RXStsQ_cur = sc->RXStsQ;
346 
347 	/* Program each queue's start addr, cur addr, and len registers
348 	 * with the physical addresses.
349 	 */
350 	addr = (char *)sc->ctrlpage_dmamap->dm_segs[0].ds_addr;
351 	EPE_WRITE(TXDQBAdd, (uint32_t)addr);
352 	EPE_WRITE(TXDQCurAdd, (uint32_t)addr);
353 	EPE_WRITE(TXDQBLen, TX_QLEN * 2 * sizeof(uint32_t));
354 
355 	addr += (sc->TXStsQ - sc->TXDQ) * sizeof(uint32_t);
356 	EPE_WRITE(TXStsQBAdd, (uint32_t)addr);
357 	EPE_WRITE(TXStsQCurAdd, (uint32_t)addr);
358 	EPE_WRITE(TXStsQBLen, TX_QLEN * sizeof(uint32_t));
359 
360 	addr += (sc->RXDQ - sc->TXStsQ) * sizeof(uint32_t);
361 	EPE_WRITE(RXDQBAdd, (uint32_t)addr);
362 	EPE_WRITE(RXDCurAdd, (uint32_t)addr);
363 	EPE_WRITE(RXDQBLen, RX_QLEN * 2 * sizeof(uint32_t));
364 
365 	addr += (sc->RXStsQ - sc->RXDQ) * sizeof(uint32_t);
366 	EPE_WRITE(RXStsQBAdd, (uint32_t)addr);
367 	EPE_WRITE(RXStsQCurAdd, (uint32_t)addr);
368 	EPE_WRITE(RXStsQBLen, RX_QLEN * 2 * sizeof(uint32_t));
369 
370 	/* Populate the RXDQ with mbufs */
371 	for(i = 0; i < RX_QLEN; i++) {
372 		struct mbuf *m;
373 
374 		bus_dmamap_create(sc->sc_dmat, MCLBYTES, TX_QLEN/4, MCLBYTES, 0,
375 			BUS_DMA_WAITOK, &sc->rxq[i].m_dmamap);
376 		MGETHDR(m, M_WAIT, MT_DATA);
377 		MCLGET(m, M_WAIT);
378 		sc->rxq[i].m = m;
379 		bus_dmamap_load(sc->sc_dmat, sc->rxq[i].m_dmamap,
380 			m->m_ext.ext_buf, MCLBYTES, NULL,
381 			BUS_DMA_WAITOK);
382 
383 		sc->RXDQ[i * 2] = sc->rxq[i].m_dmamap->dm_segs[0].ds_addr;
384 		sc->RXDQ[i * 2 + 1] = (i << 16) | MCLBYTES;
385 		bus_dmamap_sync(sc->sc_dmat, sc->rxq[i].m_dmamap, 0,
386 			MCLBYTES, BUS_DMASYNC_PREREAD);
387 	}
388 
389 	for(i = 0; i < TX_QLEN; i++) {
390 		bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1, MCLBYTES, 0,
391 			(BUS_DMA_WAITOK|BUS_DMA_ALLOCNOW),
392 			&sc->txq[i].m_dmamap);
393 		sc->txq[i].m = NULL;
394 		sc->TXDQ[i * 2 + 1] = (i << 16);
395 	}
396 
397 	/* Divide HCLK by 32 for MDC clock */
398 	if (device_cfdata(sc->sc_dev)->cf_flags)
399 		mdcdiv = device_cfdata(sc->sc_dev)->cf_flags;
400 	EPE_WRITE(SelfCtl, (SelfCtl_MDCDIV(mdcdiv)|SelfCtl_PSPRS));
401 
402 	sc->sc_mii.mii_ifp = ifp;
403 	sc->sc_mii.mii_readreg = epe_mii_readreg;
404 	sc->sc_mii.mii_writereg = epe_mii_writereg;
405 	sc->sc_mii.mii_statchg = epe_statchg;
406 	sc->sc_ec.ec_mii = &sc->sc_mii;
407 	ifmedia_init(&sc->sc_mii.mii_media, IFM_IMASK, epe_mediachange,
408 		ether_mediastatus);
409 	mii_attach(sc->sc_dev, &sc->sc_mii, 0xffffffff, MII_PHY_ANY,
410 		MII_OFFSET_ANY, 0);
411 	ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
412 
413 	EPE_WRITE(BMCtl, BMCtl_RxEn|BMCtl_TxEn);
414 	EPE_WRITE(IntEn, IntEn_REOFIE);
415 	/* maximum valid max frame length */
416 	EPE_WRITE(MaxFrmLen, (0x7ff << 16)|MHLEN);
417 	/* wait for receiver ready */
418 	while((EPE_READ(BMSts) & BMSts_RxAct) == 0)
419 		continue;
420 	/* enqueue the entries in RXStsQ and RXDQ */
421 	CTRLPAGE_DMASYNC(0, sc->ctrlpage_dmamap->dm_mapsize,
422 		BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
423 	EPE_WRITE(RXDEnq, RX_QLEN - 1);
424 	EPE_WRITE(RXStsEnq, RX_QLEN - 1);
425 
426 	/*
427 	 * We can support 802.1Q VLAN-sized frames.
428 	 */
429 	sc->sc_ec.ec_capabilities |= ETHERCAP_VLAN_MTU;
430 
431         strcpy(ifp->if_xname, device_xname(sc->sc_dev));
432         ifp->if_flags = IFF_BROADCAST|IFF_SIMPLEX|IFF_NOTRAILERS|IFF_MULTICAST;
433         ifp->if_ioctl = epe_ifioctl;
434         ifp->if_start = epe_ifstart;
435         ifp->if_watchdog = epe_ifwatchdog;
436         ifp->if_init = epe_ifinit;
437         ifp->if_stop = epe_ifstop;
438         ifp->if_timer = 0;
439 	ifp->if_softc = sc;
440         IFQ_SET_READY(&ifp->if_snd);
441         if_attach(ifp);
442 	if_deferred_start_init(ifp, NULL);
443         ether_ifattach(ifp, (sc)->sc_enaddr);
444 }
445 
446 static int
447 epe_mediachange(struct ifnet *ifp)
448 {
449 	if (ifp->if_flags & IFF_UP)
450 		epe_ifinit(ifp);
451 	return (0);
452 }
453 
454 int
455 epe_mii_readreg(device_t self, int phy, int reg)
456 {
457 	uint32_t d, v;
458 
459 	d = EPE_READ(SelfCtl);
460 	EPE_WRITE(SelfCtl, d & ~SelfCtl_PSPRS); /* no preamble suppress */
461 	EPE_WRITE(MIICmd, (MIICmd_READ | (phy << 5) | reg));
462 	while(EPE_READ(MIISts) & MIISts_BUSY);
463 	v = EPE_READ(MIIData);
464 	EPE_WRITE(SelfCtl, d); /* restore old value */
465 	return v;
466 }
467 
468 void
469 epe_mii_writereg(device_t self, int phy, int reg, int val)
470 {
471 	uint32_t d;
472 
473 	d = EPE_READ(SelfCtl);
474 	EPE_WRITE(SelfCtl, d & ~SelfCtl_PSPRS); /* no preamble suppress */
475 	EPE_WRITE(MIIData, val);
476 	EPE_WRITE(MIICmd, (MIICmd_WRITE | (phy << 5) | reg));
477 	while(EPE_READ(MIISts) & MIISts_BUSY);
478 	EPE_WRITE(SelfCtl, d); /* restore old value */
479 }
480 
481 
482 void
483 epe_statchg(struct ifnet *ifp)
484 {
485         struct epe_softc *sc = ifp->if_softc;
486         uint32_t reg;
487 
488         /*
489          * We must keep the MAC and the PHY in sync as
490          * to the status of full-duplex!
491          */
492         reg = EPE_READ(TestCtl);
493         if (sc->sc_mii.mii_media_active & IFM_FDX)
494                 reg |= TestCtl_MFDX;
495         else
496                 reg &= ~TestCtl_MFDX;
497 	EPE_WRITE(TestCtl, reg);
498 }
499 
500 void
501 epe_tick(void *arg)
502 {
503 	struct epe_softc* sc = (struct epe_softc *)arg;
504 	struct ifnet * ifp = &sc->sc_ec.ec_if;
505 	int s;
506 	uint32_t misses;
507 
508 	ifp->if_collisions += EPE_READ(TXCollCnt);
509 	/* These misses are ok, they will happen if the RAM/CPU can't keep up */
510 	misses = EPE_READ(RXMissCnt);
511 	if (misses > 0)
512 		printf("%s: %d rx misses\n", device_xname(sc->sc_dev), misses);
513 
514 	s = splnet();
515 	if (epe_gctx(sc) > 0 && IFQ_IS_EMPTY(&ifp->if_snd) == 0) {
516 		epe_ifstart(ifp);
517 	}
518 	splx(s);
519 
520 	mii_tick(&sc->sc_mii);
521 	callout_reset(&sc->epe_tick_ch, hz, epe_tick, sc);
522 }
523 
524 
525 static int
526 epe_ifioctl(struct ifnet *ifp, u_long cmd, void *data)
527 {
528 	int s, error;
529 
530 	s = splnet();
531 	error = ether_ioctl(ifp, cmd, data);
532 	if (error == ENETRESET) {
533 		if (ifp->if_flags & IFF_RUNNING)
534 			epe_setaddr(ifp);
535 		error = 0;
536 	}
537 	splx(s);
538 	return error;
539 }
540 
541 static void
542 epe_ifstart(struct ifnet *ifp)
543 {
544 	struct epe_softc *sc = (struct epe_softc *)ifp->if_softc;
545 	struct mbuf *m;
546 	bus_dma_segment_t *segs;
547 	int s, bi, err, nsegs, ndq;
548 
549 	s = splnet();
550 start:
551 	ndq = 0;
552 	if (sc->TXDQ_avail == 0) {
553 		if (epe_gctx(sc) == 0) {
554 			/* Enable End-Of-TX-Chain interrupt */
555 			EPE_WRITE(IntEn, IntEn_REOFIE|IntEn_ECIE);
556 			ifp->if_flags |= IFF_OACTIVE;
557 			ifp->if_timer = 10;
558 			splx(s);
559 			return;
560 		}
561 	}
562 
563 	bi = sc->TXDQ_cur - sc->TXDQ;
564 
565 	IFQ_POLL(&ifp->if_snd, m);
566 	if (m == NULL) {
567 		splx(s);
568 		return;
569 	}
570 more:
571 	if ((err = bus_dmamap_load_mbuf(sc->sc_dmat, sc->txq[bi].m_dmamap, m,
572 		BUS_DMA_NOWAIT)) ||
573 		sc->txq[bi].m_dmamap->dm_segs[0].ds_addr & 0x3 ||
574 		sc->txq[bi].m_dmamap->dm_nsegs > (sc->TXDQ_avail - ndq)) {
575 		/* Copy entire mbuf chain to new and 32-bit aligned storage */
576 		struct mbuf *mn;
577 
578 		if (err == 0)
579 			bus_dmamap_unload(sc->sc_dmat, sc->txq[bi].m_dmamap);
580 
581 		MGETHDR(mn, M_DONTWAIT, MT_DATA);
582 		if (mn == NULL) goto stop;
583 		if (m->m_pkthdr.len > (MHLEN & (~0x3))) {
584 			MCLGET(mn, M_DONTWAIT);
585 			if ((mn->m_flags & M_EXT) == 0) {
586 				m_freem(mn);
587 				goto stop;
588 			}
589 		}
590 		mn->m_data = (void *)(((uint32_t)mn->m_data + 0x3) & (~0x3));
591 		m_copydata(m, 0, m->m_pkthdr.len, mtod(mn, void *));
592 		mn->m_pkthdr.len = mn->m_len = m->m_pkthdr.len;
593 		IFQ_DEQUEUE(&ifp->if_snd, m);
594 		m_freem(m);
595 		m = mn;
596 		bus_dmamap_load_mbuf(sc->sc_dmat, sc->txq[bi].m_dmamap, m,
597 			BUS_DMA_NOWAIT);
598 	} else {
599 		IFQ_DEQUEUE(&ifp->if_snd, m);
600 	}
601 
602 	bpf_mtap(ifp, m, BPF_D_OUT);
603 
604 	nsegs = sc->txq[bi].m_dmamap->dm_nsegs;
605 	segs = sc->txq[bi].m_dmamap->dm_segs;
606 	bus_dmamap_sync(sc->sc_dmat, sc->txq[bi].m_dmamap, 0,
607 		sc->txq[bi].m_dmamap->dm_mapsize,
608 		BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
609 
610 	/* XXX: This driver hasn't been tested w/nsegs > 1 */
611 	while (nsegs > 0) {
612 		nsegs--;
613 		sc->txq[bi].m = m;
614 		sc->TXDQ[bi * 2] = segs->ds_addr;
615 		if (nsegs == 0)
616 			sc->TXDQ[bi * 2 + 1] = segs->ds_len | (bi << 16) |
617 				(1 << 31);
618 		else
619 			sc->TXDQ[bi * 2 + 1] = segs->ds_len | (bi << 16);
620 		segs++;
621 		bi = (bi + 1) % TX_QLEN;
622 		ndq++;
623 	}
624 
625 
626 	/*
627 	 * Enqueue another.  Don't do more than half the available
628 	 * descriptors before telling the MAC about them
629 	 */
630 	if ((sc->TXDQ_avail - ndq) > 0 && ndq < TX_QLEN / 2) {
631 		IFQ_POLL(&ifp->if_snd, m);
632 		if (m != NULL) {
633 			goto more;
634 		}
635 	}
636 stop:
637 	if (ndq > 0) {
638 		sc->TXDQ_avail -= ndq;
639 		sc->TXDQ_cur = &sc->TXDQ[bi];
640 		CTRLPAGE_DMASYNC(0, TX_QLEN * 2 * sizeof(uint32_t),
641 			BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
642 		EPE_WRITE(TXDEnq, ndq);
643 	}
644 
645 	if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
646 		goto start;
647 
648 	splx(s);
649 	return;
650 }
651 
652 static void
653 epe_ifwatchdog(struct ifnet *ifp)
654 {
655 	struct epe_softc *sc = (struct epe_softc *)ifp->if_softc;
656 
657 	if ((ifp->if_flags & IFF_RUNNING) == 0)
658 		return;
659        	printf("%s: device timeout, BMCtl = 0x%08x, BMSts = 0x%08x\n",
660 		device_xname(sc->sc_dev), EPE_READ(BMCtl), EPE_READ(BMSts));
661 }
662 
663 static int
664 epe_ifinit(struct ifnet *ifp)
665 {
666 	struct epe_softc *sc = ifp->if_softc;
667 	int rc, s = splnet();
668 
669 	callout_stop(&sc->epe_tick_ch);
670 	EPE_WRITE(RXCtl, RXCtl_IA0|RXCtl_BA|RXCtl_RCRCA|RXCtl_SRxON);
671 	EPE_WRITE(TXCtl, TXCtl_STxON);
672 	EPE_WRITE(GIIntMsk, GIIntMsk_INT); /* start interrupting */
673 
674 	if ((rc = mii_mediachg(&sc->sc_mii)) == ENXIO)
675 		rc = 0;
676 	else if (rc != 0)
677 		goto out;
678 
679 	callout_reset(&sc->epe_tick_ch, hz, epe_tick, sc);
680         ifp->if_flags |= IFF_RUNNING;
681 out:
682 	splx(s);
683 	return 0;
684 }
685 
686 static void
687 epe_ifstop(struct ifnet *ifp, int disable)
688 {
689 	struct epe_softc *sc = ifp->if_softc;
690 
691 
692 	EPE_WRITE(RXCtl, 0);
693 	EPE_WRITE(TXCtl, 0);
694 	EPE_WRITE(GIIntMsk, 0);
695 	callout_stop(&sc->epe_tick_ch);
696 
697 	/* Down the MII. */
698 	mii_down(&sc->sc_mii);
699 
700 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
701 	ifp->if_timer = 0;
702 	sc->sc_mii.mii_media_status &= ~IFM_ACTIVE;
703 }
704 
705 static void
706 epe_setaddr(struct ifnet *ifp)
707 {
708 	struct epe_softc *sc = ifp->if_softc;
709 	struct ethercom *ac = &sc->sc_ec;
710 	struct ether_multi *enm;
711 	struct ether_multistep step;
712 	uint8_t ias[2][ETHER_ADDR_LEN];
713 	uint32_t h, nma = 0, hashes[2] = { 0, 0 };
714 	uint32_t rxctl = EPE_READ(RXCtl);
715 
716 	/* disable receiver temporarily */
717 	EPE_WRITE(RXCtl, rxctl & ~RXCtl_SRxON);
718 
719 	rxctl &= ~(RXCtl_MA|RXCtl_PA|RXCtl_IA2|RXCtl_IA3);
720 
721 	if (ifp->if_flags & IFF_PROMISC) {
722 		rxctl |= RXCtl_PA;
723 	}
724 
725 	ifp->if_flags &= ~IFF_ALLMULTI;
726 
727 	ETHER_FIRST_MULTI(step, ac, enm);
728 	while (enm != NULL) {
729 		if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
730 			/*
731 			 * We must listen to a range of multicast addresses.
732 			 * For now, just accept all multicasts, rather than
733 			 * trying to set only those filter bits needed to match
734 			 * the range.  (At this time, the only use of address
735 			 * ranges is for IP multicast routing, for which the
736 			 * range is big enough to require all bits set.)
737 			 */
738 			rxctl &= ~(RXCtl_IA2|RXCtl_IA3);
739 			rxctl |= RXCtl_MA;
740 			hashes[0] = 0xffffffffUL;
741 			hashes[1] = 0xffffffffUL;
742 			ifp->if_flags |= IFF_ALLMULTI;
743 			break;
744 		}
745 
746 		if (nma < 2) {
747 			/* We can program 2 perfect address filters for mcast */
748 			memcpy(ias[nma], enm->enm_addrlo, ETHER_ADDR_LEN);
749 			rxctl |= (1 << (nma + 2));
750 		} else {
751 			/*
752 			 * XXX: Datasheet is not very clear here, I'm not sure
753 			 * if I'm doing this right.  --joff
754 			 */
755 			h = ether_crc32_le(enm->enm_addrlo, ETHER_ADDR_LEN);
756 
757 			/* Just want the 6 most-significant bits. */
758 			h = h >> 26;
759 
760 			hashes[ h / 32 ] |=  (1 << (h % 32));
761 			rxctl |= RXCtl_MA;
762 		}
763 		ETHER_NEXT_MULTI(step, enm);
764 		nma++;
765 	}
766 
767 	EPE_WRITE(AFP, 0);
768 	bus_space_write_region_1(sc->sc_iot, sc->sc_ioh, EPE_IndAd,
769 		sc->sc_enaddr, ETHER_ADDR_LEN);
770 	if (rxctl & RXCtl_IA2) {
771 		EPE_WRITE(AFP, 2);
772 		bus_space_write_region_1(sc->sc_iot, sc->sc_ioh, EPE_IndAd,
773 			ias[0], ETHER_ADDR_LEN);
774 	}
775 	if (rxctl & RXCtl_IA3) {
776 		EPE_WRITE(AFP, 3);
777 		bus_space_write_region_1(sc->sc_iot, sc->sc_ioh, EPE_IndAd,
778 			ias[1], ETHER_ADDR_LEN);
779 	}
780 	if (hashes[0] != 0 && hashes[1] != 0) {
781 		EPE_WRITE(AFP, 7);
782 		EPE_WRITE(HashTbl, hashes[0]);
783 		EPE_WRITE(HashTbl + 4, hashes[1]);
784 	}
785 	EPE_WRITE(RXCtl, rxctl);
786 }
787