xref: /netbsd-src/sys/arch/arm/at91/at91emac.c (revision 274254cdae52594c1aa480a736aef78313d15c9c)
1 /*	$Id: at91emac.c,v 1.5 2009/03/18 16:00:09 cegger Exp $	*/
2 /*	$NetBSD: at91emac.c,v 1.5 2009/03/18 16:00:09 cegger Exp $	*/
3 
4 /*
5  * Copyright (c) 2007 Embedtronics Oy
6  * All rights reserved.
7  *
8  * Based on arch/arm/ep93xx/epe.c
9  *
10  * Copyright (c) 2004 Jesse Off
11  * All rights reserved.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  * 3. All advertising materials mentioning features or use of this software
22  *    must display the following acknowledgement:
23  *	This product includes software developed by the NetBSD
24  *	Foundation, Inc. and its contributors.
25  * 4. Neither the name of The NetBSD Foundation nor the names of its
26  *    contributors may be used to endorse or promote products derived
27  *    from this software without specific prior written permission.
28  *
29  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
30  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
31  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
32  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
33  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
34  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
35  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
36  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
37  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
39  * POSSIBILITY OF SUCH DAMAGE.
40  */
41 
42 #include <sys/cdefs.h>
43 __KERNEL_RCSID(0, "$NetBSD: at91emac.c,v 1.5 2009/03/18 16:00:09 cegger Exp $");
44 
45 #include <sys/types.h>
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/ioctl.h>
49 #include <sys/kernel.h>
50 #include <sys/proc.h>
51 #include <sys/malloc.h>
52 #include <sys/time.h>
53 #include <sys/device.h>
54 #include <uvm/uvm_extern.h>
55 
56 #include <machine/bus.h>
57 #include <machine/intr.h>
58 
59 #include <arm/cpufunc.h>
60 
61 #include <net/if.h>
62 #include <net/if_dl.h>
63 #include <net/if_types.h>
64 #include <net/if_media.h>
65 #include <net/if_ether.h>
66 
67 #include <dev/mii/mii.h>
68 #include <dev/mii/miivar.h>
69 
70 #ifdef INET
71 #include <netinet/in.h>
72 #include <netinet/in_systm.h>
73 #include <netinet/in_var.h>
74 #include <netinet/ip.h>
75 #include <netinet/if_inarp.h>
76 #endif
77 
78 #ifdef NS
79 #include <netns/ns.h>
80 #include <netns/ns_if.h>
81 #endif
82 
83 #include "bpfilter.h"
84 #if NBPFILTER > 0
85 #include <net/bpf.h>
86 #include <net/bpfdesc.h>
87 #endif
88 
89 #ifdef IPKDB_AT91	// @@@
90 #include <ipkdb/ipkdb.h>
91 #endif
92 
93 #include <arm/at91/at91var.h>
94 #include <arm/at91/at91emacreg.h>
95 #include <arm/at91/at91emacvar.h>
96 
97 #define DEFAULT_MDCDIV	32
98 
99 #ifndef EMAC_FAST
100 #define EMAC_FAST
101 #endif
102 
103 #ifndef EMAC_FAST
104 #define EMAC_READ(x) \
105 	bus_space_read_4(sc->sc_iot, sc->sc_ioh, (EPE_ ## x))
106 #define EMAC_WRITE(x, y) \
107 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, (EPE_ ## x), (y))
108 #else
109 #define EMAC_READ(x) ETHREG(x)
110 #define EMAC_WRITE(x, y) ETHREG(x) = (y)
111 #endif /* ! EMAC_FAST */
112 
113 static int	emac_match(device_t, cfdata_t, void *);
114 static void	emac_attach(device_t, device_t, void *);
115 static void	emac_init(struct emac_softc *);
116 static int      emac_intr(void* arg);
117 static int	emac_gctx(struct emac_softc *);
118 static int	emac_mediachange(struct ifnet *);
119 static void	emac_mediastatus(struct ifnet *, struct ifmediareq *);
120 int		emac_mii_readreg (device_t, int, int);
121 void		emac_mii_writereg (device_t, int, int, int);
122 void		emac_statchg (device_t );
123 void		emac_tick (void *);
124 static int	emac_ifioctl (struct ifnet *, u_long, void *);
125 static void	emac_ifstart (struct ifnet *);
126 static void	emac_ifwatchdog (struct ifnet *);
127 static int	emac_ifinit (struct ifnet *);
128 static void	emac_ifstop (struct ifnet *, int);
129 static void	emac_setaddr (struct ifnet *);
130 
131 CFATTACH_DECL(at91emac, sizeof(struct emac_softc),
132     emac_match, emac_attach, NULL, NULL);
133 
134 #ifdef	EMAC_DEBUG
135 int emac_debug = EMAC_DEBUG;
136 #define	DPRINTFN(n,fmt)	if (emac_debug >= (n)) printf fmt
137 #else
138 #define	DPRINTFN(n,fmt)
139 #endif
140 
141 static int
142 emac_match(device_t parent, cfdata_t match, void *aux)
143 {
144 	if (strcmp(match->cf_name, "at91emac") == 0)
145 		return 2;
146 	return 0;
147 }
148 
149 static void
150 emac_attach(device_t parent, device_t self, void *aux)
151 {
152 	struct emac_softc		*sc = device_private(self);
153 	struct at91bus_attach_args	*sa = aux;
154 	prop_data_t			enaddr;
155 	uint32_t			u;
156 
157 	printf("\n");
158 	sc->sc_dev = self;
159 	sc->sc_iot = sa->sa_iot;
160 	sc->sc_pid = sa->sa_pid;
161 	sc->sc_dmat = sa->sa_dmat;
162 
163 	if (bus_space_map(sa->sa_iot, sa->sa_addr, sa->sa_size, 0, &sc->sc_ioh))
164 		panic("%s: Cannot map registers", device_xname(self));
165 
166 	/* enable peripheral clock */
167 	at91_peripheral_clock(sc->sc_pid, 1);
168 
169 	/* configure emac: */
170 	EMAC_WRITE(ETH_CTL, 0);			// disable everything
171 	EMAC_WRITE(ETH_IDR, -1);		// disable interrupts
172 	EMAC_WRITE(ETH_RBQP, 0);		// clear receive
173 	EMAC_WRITE(ETH_CFG, ETH_CFG_CLK_32 | ETH_CFG_SPD | ETH_CFG_FD | ETH_CFG_BIG);
174 	EMAC_WRITE(ETH_TCR, 0);			// send nothing
175 	//(void)EMAC_READ(ETH_ISR);
176 	u = EMAC_READ(ETH_TSR);
177 	EMAC_WRITE(ETH_TSR, (u & (ETH_TSR_UND | ETH_TSR_COMP | ETH_TSR_BNQ
178 				  | ETH_TSR_IDLE | ETH_TSR_RLE
179 				  | ETH_TSR_COL|ETH_TSR_OVR)));
180 	u = EMAC_READ(ETH_RSR);
181 	EMAC_WRITE(ETH_RSR, (u & (ETH_RSR_OVR|ETH_RSR_REC|ETH_RSR_BNA)));
182 
183 	/* Fetch the Ethernet address from property if set. */
184 	enaddr = prop_dictionary_get(device_properties(self), "mac-addr");
185 
186 	if (enaddr != NULL) {
187 		KASSERT(prop_object_type(enaddr) == PROP_TYPE_DATA);
188 		KASSERT(prop_data_size(enaddr) == ETHER_ADDR_LEN);
189 		memcpy(sc->sc_enaddr, prop_data_data_nocopy(enaddr),
190 		       ETHER_ADDR_LEN);
191 	} else {
192 		static const uint8_t hardcoded[ETHER_ADDR_LEN] = {
193 		  0x00, 0x0d, 0x10, 0x81, 0x0c, 0x94
194 		};
195 		memcpy(sc->sc_enaddr, hardcoded, ETHER_ADDR_LEN);
196 	}
197 
198         at91_intr_establish(sc->sc_pid, IPL_NET, INTR_HIGH_LEVEL, emac_intr, sc);
199 	emac_init(sc);
200 }
201 
202 static int
203 emac_gctx(struct emac_softc *sc)
204 {
205 	struct ifnet * ifp = &sc->sc_ec.ec_if;
206 	u_int32_t tsr;
207 
208 	tsr = EMAC_READ(ETH_TSR);
209 	if (!(tsr & ETH_TSR_BNQ)) {
210 		// no space left
211 		return 0;
212 	}
213 
214 	// free sent frames
215 	while (sc->txqc > (tsr & ETH_TSR_IDLE ? 0 : 1)) {
216 		int i = sc->txqi % TX_QLEN;
217 		bus_dmamap_sync(sc->sc_dmat, sc->txq[i].m_dmamap, 0,
218 				sc->txq[i].m->m_pkthdr.len, BUS_DMASYNC_POSTWRITE);
219 		bus_dmamap_unload(sc->sc_dmat, sc->txq[i].m_dmamap);
220 		m_freem(sc->txq[i].m);
221 		DPRINTFN(2,("%s: freed idx #%i mbuf %p (txqc=%i)\n", __FUNCTION__, i, sc->txq[i].m, sc->txqc));
222 		sc->txq[i].m = NULL;
223 		sc->txqi = (i + 1) % TX_QLEN;
224 		sc->txqc--;
225 	}
226 
227 	// mark we're free
228 	if (ifp->if_flags & IFF_OACTIVE) {
229 		ifp->if_flags &= ~IFF_OACTIVE;
230 		/* Disable transmit-buffer-free interrupt */
231 		/*EMAC_WRITE(ETH_IDR, ETH_ISR_TBRE);*/
232 	}
233 
234 	return 1;
235 }
236 
237 static int
238 emac_intr(void *arg)
239 {
240 	struct emac_softc *sc = (struct emac_softc *)arg;
241 	struct ifnet * ifp = &sc->sc_ec.ec_if;
242 	u_int32_t imr, isr, rsr, ctl;
243 	int bi;
244 
245 	imr = ~EMAC_READ(ETH_IMR);
246 	if (!(imr & (ETH_ISR_RCOM|ETH_ISR_TBRE|ETH_ISR_TIDLE|ETH_ISR_RBNA|ETH_ISR_ROVR))) {
247 		// interrupt not enabled, can't be us
248 		return 0;
249 	}
250 
251 	isr = EMAC_READ(ETH_ISR) & imr;
252 	rsr = EMAC_READ(ETH_RSR);		// get receive status register
253 
254 	DPRINTFN(2, ("%s: isr=0x%08X rsr=0x%08X imr=0x%08X\n", __FUNCTION__, isr, rsr, imr));
255 
256 	if (isr & ETH_ISR_RBNA) {		// out of receive buffers
257 		EMAC_WRITE(ETH_RSR, ETH_RSR_BNA);	// clear interrupt
258 		ctl = EMAC_READ(ETH_CTL);		// get current control register value
259 		EMAC_WRITE(ETH_CTL, ctl & ~ETH_CTL_RE);	// disable receiver
260 		EMAC_WRITE(ETH_RSR, ETH_RSR_BNA);	// clear BNA bit
261 		EMAC_WRITE(ETH_CTL, ctl |  ETH_CTL_RE);	// re-enable receiver
262 		ifp->if_ierrors++;
263 		ifp->if_ipackets++;
264 		DPRINTFN(1,("%s: out of receive buffers\n", __FUNCTION__));
265 	}
266 	if (isr & ETH_ISR_ROVR) {
267 		EMAC_WRITE(ETH_RSR, ETH_RSR_OVR);	// clear interrupt
268 		ifp->if_ierrors++;
269 		ifp->if_ipackets++;
270 		DPRINTFN(1,("%s: receive overrun\n", __FUNCTION__));
271 	}
272 
273 	if (isr & ETH_ISR_RCOM) {			// packet has been received!
274 		uint32_t nfo;
275 		// @@@ if memory is NOT coherent, then we're in trouble @@@@
276 //		bus_dmamap_sync(sc->sc_dmat, sc->rbqpage_dmamap, 0, sc->rbqlen, BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
277 //		printf("## RDSC[%i].ADDR=0x%08X\n", sc->rxqi % RX_QLEN, sc->RDSC[sc->rxqi % RX_QLEN].Addr);
278 		DPRINTFN(2,("#2 RDSC[%i].INFO=0x%08X\n", sc->rxqi % RX_QLEN, sc->RDSC[sc->rxqi % RX_QLEN].Info));
279 		while (sc->RDSC[(bi = sc->rxqi % RX_QLEN)].Addr & ETH_RDSC_F_USED) {
280 			int fl;
281 			struct mbuf *m;
282 
283 			nfo = sc->RDSC[bi].Info;
284 		  	fl = (nfo & ETH_RDSC_I_LEN) - 4;
285 			DPRINTFN(2,("## nfo=0x%08X\n", nfo));
286 
287 			MGETHDR(m, M_DONTWAIT, MT_DATA);
288 			if (m != NULL) MCLGET(m, M_DONTWAIT);
289 			if (m != NULL && (m->m_flags & M_EXT)) {
290 				bus_dmamap_sync(sc->sc_dmat, sc->rxq[bi].m_dmamap, 0,
291 						MCLBYTES, BUS_DMASYNC_POSTREAD);
292 				bus_dmamap_unload(sc->sc_dmat,
293 					sc->rxq[bi].m_dmamap);
294 				sc->rxq[bi].m->m_pkthdr.rcvif = ifp;
295 				sc->rxq[bi].m->m_pkthdr.len =
296 					sc->rxq[bi].m->m_len = fl;
297 #if NBPFILTER > 0
298 				if (ifp->if_bpf)
299 					bpf_mtap(ifp->if_bpf, sc->rxq[bi].m);
300 #endif /* NBPFILTER > 0 */
301 				DPRINTFN(2,("received %u bytes packet\n", fl));
302                                 (*ifp->if_input)(ifp, sc->rxq[bi].m);
303 				if (mtod(m, intptr_t) & 3) {
304 					m_adj(m, mtod(m, intptr_t) & 3);
305 				}
306 				sc->rxq[bi].m = m;
307 				bus_dmamap_load(sc->sc_dmat,
308 					sc->rxq[bi].m_dmamap,
309 					m->m_ext.ext_buf, MCLBYTES,
310 					NULL, BUS_DMA_NOWAIT);
311 				bus_dmamap_sync(sc->sc_dmat, sc->rxq[bi].m_dmamap, 0,
312 						MCLBYTES, BUS_DMASYNC_PREREAD);
313 				sc->RDSC[bi].Info = 0;
314 				sc->RDSC[bi].Addr =
315 					sc->rxq[bi].m_dmamap->dm_segs[0].ds_addr
316 					| (bi == (RX_QLEN-1) ? ETH_RDSC_F_WRAP : 0);
317 			} else {
318 				/* Drop packets until we can get replacement
319 				 * empty mbufs for the RXDQ.
320 				 */
321 				if (m != NULL) {
322 					m_freem(m);
323 				}
324 				ifp->if_ierrors++;
325 			}
326 			sc->rxqi++;
327 		}
328 //		bus_dmamap_sync(sc->sc_dmat, sc->rbqpage_dmamap, 0, sc->rbqlen, BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
329 	}
330 
331 	if (emac_gctx(sc) > 0 && IFQ_IS_EMPTY(&ifp->if_snd) == 0) {
332 		emac_ifstart(ifp);
333 	}
334 #if 0 // reloop
335 	irq = EMAC_READ(IntStsC);
336 	if ((irq & (IntSts_RxSQ|IntSts_ECI)) != 0)
337 		goto begin;
338 #endif
339 
340 	return (1);
341 }
342 
343 
344 static void
345 emac_init(struct emac_softc *sc)
346 {
347 	bus_dma_segment_t segs;
348 	void *addr;
349 	int rsegs, err, i;
350 	struct ifnet * ifp = &sc->sc_ec.ec_if;
351 	uint32_t u;
352 #if 0
353 	int mdcdiv = DEFAULT_MDCDIV;
354 #endif
355 
356 	callout_init(&sc->emac_tick_ch, 0);
357 
358 	// ok...
359 	EMAC_WRITE(ETH_CTL, ETH_CTL_MPE);	// disable everything
360 	EMAC_WRITE(ETH_IDR, -1);		// disable interrupts
361 	EMAC_WRITE(ETH_RBQP, 0);		// clear receive
362 	EMAC_WRITE(ETH_CFG, ETH_CFG_CLK_32 | ETH_CFG_SPD | ETH_CFG_FD | ETH_CFG_BIG);
363 	EMAC_WRITE(ETH_TCR, 0);			// send nothing
364 //	(void)EMAC_READ(ETH_ISR);
365 	u = EMAC_READ(ETH_TSR);
366 	EMAC_WRITE(ETH_TSR, (u & (ETH_TSR_UND | ETH_TSR_COMP | ETH_TSR_BNQ
367 				  | ETH_TSR_IDLE | ETH_TSR_RLE
368 				  | ETH_TSR_COL|ETH_TSR_OVR)));
369 	u = EMAC_READ(ETH_RSR);
370 	EMAC_WRITE(ETH_RSR, (u & (ETH_RSR_OVR|ETH_RSR_REC|ETH_RSR_BNA)));
371 
372 	/* configure EMAC */
373 	EMAC_WRITE(ETH_CFG, ETH_CFG_CLK_32 | ETH_CFG_SPD | ETH_CFG_FD | ETH_CFG_BIG);
374 	EMAC_WRITE(ETH_CTL, ETH_CTL_MPE);
375 #if 0
376 	if (device_cfdata(&sc->sc_dev)->cf_flags)
377 		mdcdiv = device_cfdata(&sc->sc_dev)->cf_flags;
378 #endif
379 	/* set ethernet address */
380 	EMAC_WRITE(ETH_SA1L, (sc->sc_enaddr[3] << 24)
381 		   | (sc->sc_enaddr[2] << 16) | (sc->sc_enaddr[1] << 8)
382 		   | (sc->sc_enaddr[0]));
383 	EMAC_WRITE(ETH_SA1H, (sc->sc_enaddr[5] << 8)
384 		   | (sc->sc_enaddr[4]));
385 	EMAC_WRITE(ETH_SA2L, 0);
386 	EMAC_WRITE(ETH_SA2H, 0);
387 	EMAC_WRITE(ETH_SA3L, 0);
388 	EMAC_WRITE(ETH_SA3H, 0);
389 	EMAC_WRITE(ETH_SA4L, 0);
390 	EMAC_WRITE(ETH_SA4H, 0);
391 
392 	/* Allocate a page of memory for receive queue descriptors */
393 	sc->rbqlen = (ETH_RDSC_SIZE * (RX_QLEN + 1) * 2 + PAGE_SIZE - 1) / PAGE_SIZE;
394 	sc->rbqlen *= PAGE_SIZE;
395 	DPRINTFN(1,("%s: rbqlen=%i\n", __FUNCTION__, sc->rbqlen));
396 
397 	err = bus_dmamem_alloc(sc->sc_dmat, sc->rbqlen, 0,
398 		MAX(16384, PAGE_SIZE),	// see EMAC errata why forced to 16384 byte boundary
399 		&segs, 1, &rsegs, BUS_DMA_WAITOK);
400 	if (err == 0) {
401 		DPRINTFN(1,("%s: -> bus_dmamem_map\n", __FUNCTION__));
402 		err = bus_dmamem_map(sc->sc_dmat, &segs, 1, sc->rbqlen,
403 			&sc->rbqpage, (BUS_DMA_WAITOK|BUS_DMA_COHERENT));
404 	}
405 	if (err == 0) {
406 		DPRINTFN(1,("%s: -> bus_dmamap_create\n", __FUNCTION__));
407 		err = bus_dmamap_create(sc->sc_dmat, sc->rbqlen, 1,
408 			sc->rbqlen, MAX(16384, PAGE_SIZE), BUS_DMA_WAITOK,
409 			&sc->rbqpage_dmamap);
410 	}
411 	if (err == 0) {
412 		DPRINTFN(1,("%s: -> bus_dmamap_load\n", __FUNCTION__));
413 		err = bus_dmamap_load(sc->sc_dmat, sc->rbqpage_dmamap,
414 			sc->rbqpage, sc->rbqlen, NULL, BUS_DMA_WAITOK);
415 	}
416 	if (err != 0) {
417 		panic("%s: Cannot get DMA memory", device_xname(sc->sc_dev));
418 	}
419 	sc->rbqpage_dsaddr = sc->rbqpage_dmamap->dm_segs[0].ds_addr;
420 
421 	memset(sc->rbqpage, 0, sc->rbqlen);
422 
423 	/* Set up pointers to start of each queue in kernel addr space.
424 	 * Each descriptor queue or status queue entry uses 2 words
425 	 */
426 	sc->RDSC = (void*)sc->rbqpage;
427 
428 	/* Populate the RXQ with mbufs */
429 	sc->rxqi = 0;
430 	for(i = 0; i < RX_QLEN; i++) {
431 		struct mbuf *m;
432 
433 		err = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1, MCLBYTES, PAGE_SIZE,
434 			BUS_DMA_WAITOK, &sc->rxq[i].m_dmamap);
435 		if (err) {
436 			panic("%s: dmamap_create failed: %i\n", __FUNCTION__, err);
437 		}
438 		MGETHDR(m, M_WAIT, MT_DATA);
439 		MCLGET(m, M_WAIT);
440 		sc->rxq[i].m = m;
441 		if (mtod(m, intptr_t) & 3) {
442 			m_adj(m, mtod(m, intptr_t) & 3);
443 		}
444 		err = bus_dmamap_load(sc->sc_dmat, sc->rxq[i].m_dmamap,
445 			m->m_ext.ext_buf, MCLBYTES, NULL,
446 			BUS_DMA_WAITOK);
447 		if (err) {
448 			panic("%s: dmamap_load failed: %i\n", __FUNCTION__, err);
449 		}
450 		sc->RDSC[i].Addr = sc->rxq[i].m_dmamap->dm_segs[0].ds_addr
451 			| (i == (RX_QLEN-1) ? ETH_RDSC_F_WRAP : 0);
452 		sc->RDSC[i].Info = 0;
453 		bus_dmamap_sync(sc->sc_dmat, sc->rxq[i].m_dmamap, 0,
454 			MCLBYTES, BUS_DMASYNC_PREREAD);
455 	}
456 
457 	/* prepare transmit queue */
458 	for (i = 0; i < TX_QLEN; i++) {
459 		err = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1, MCLBYTES, 0,
460 					(BUS_DMA_WAITOK | BUS_DMA_ALLOCNOW),
461 					&sc->txq[i].m_dmamap);
462 		if (err)
463 			panic("ARGH #1");
464 		sc->txq[i].m = NULL;
465 	}
466 
467 	/* Program each queue's start addr, cur addr, and len registers
468 	 * with the physical addresses.
469 	 */
470 	bus_dmamap_sync(sc->sc_dmat, sc->rbqpage_dmamap, 0, sc->rbqlen,
471 			 BUS_DMASYNC_PREREAD);
472 	addr = (void *)sc->rbqpage_dmamap->dm_segs[0].ds_addr;
473 	EMAC_WRITE(ETH_RBQP, (u_int32_t)addr);
474 
475 	/* Divide HCLK by 32 for MDC clock */
476 	sc->sc_mii.mii_ifp = ifp;
477 	sc->sc_mii.mii_readreg = emac_mii_readreg;
478 	sc->sc_mii.mii_writereg = emac_mii_writereg;
479 	sc->sc_mii.mii_statchg = emac_statchg;
480 	ifmedia_init(&sc->sc_mii.mii_media, IFM_IMASK, emac_mediachange,
481 		emac_mediastatus);
482 	mii_attach((device_t )sc, &sc->sc_mii, 0xffffffff, MII_PHY_ANY,
483 		MII_OFFSET_ANY, 0);
484 	ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
485 
486 	// enable / disable interrupts
487 
488 #if 0
489 	// enable / disable interrupts
490 	EMAC_WRITE(ETH_IDR, -1);
491 	EMAC_WRITE(ETH_IER, ETH_ISR_RCOM | ETH_ISR_TBRE | ETH_ISR_TIDLE
492 		   | ETH_ISR_RBNA | ETH_ISR_ROVR);
493 //	(void)EMAC_READ(ETH_ISR); // why
494 
495 	// enable transmitter / receiver
496 	EMAC_WRITE(ETH_CTL, ETH_CTL_TE | ETH_CTL_RE | ETH_CTL_ISR
497 		   | ETH_CTL_CSR | ETH_CTL_MPE);
498 #endif
499 	/*
500 	 * We can support 802.1Q VLAN-sized frames.
501 	 */
502 	sc->sc_ec.ec_capabilities |= ETHERCAP_VLAN_MTU;
503 
504         strcpy(ifp->if_xname, device_xname(sc->sc_dev));
505         ifp->if_flags = IFF_BROADCAST|IFF_SIMPLEX|IFF_NOTRAILERS|IFF_MULTICAST;
506         ifp->if_ioctl = emac_ifioctl;
507         ifp->if_start = emac_ifstart;
508         ifp->if_watchdog = emac_ifwatchdog;
509         ifp->if_init = emac_ifinit;
510         ifp->if_stop = emac_ifstop;
511         ifp->if_timer = 0;
512 	ifp->if_softc = sc;
513         IFQ_SET_READY(&ifp->if_snd);
514         if_attach(ifp);
515         ether_ifattach(ifp, (sc)->sc_enaddr);
516 }
517 
518 static int
519 emac_mediachange(struct ifnet *ifp)
520 {
521 	if (ifp->if_flags & IFF_UP)
522 		emac_ifinit(ifp);
523 	return (0);
524 }
525 
526 static void
527 emac_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
528 {
529 	struct emac_softc *sc = ifp->if_softc;
530 
531 	mii_pollstat(&sc->sc_mii);
532 	ifmr->ifm_active = sc->sc_mii.mii_media_active;
533 	ifmr->ifm_status = sc->sc_mii.mii_media_status;
534 }
535 
536 
537 int
538 emac_mii_readreg(device_t self, int phy, int reg)
539 {
540 	struct emac_softc *sc;
541 
542 	sc = (struct emac_softc *)self;
543 	EMAC_WRITE(ETH_MAN, (ETH_MAN_HIGH | ETH_MAN_RW_RD
544 			     | ((phy << ETH_MAN_PHYA_SHIFT) & ETH_MAN_PHYA)
545 			     | ((reg << ETH_MAN_REGA_SHIFT) & ETH_MAN_REGA)
546 			     | ETH_MAN_CODE_IEEE802_3));
547 	while (!(EMAC_READ(ETH_SR) & ETH_SR_IDLE)) ;
548 	return (EMAC_READ(ETH_MAN) & ETH_MAN_DATA);
549 }
550 
551 void
552 emac_mii_writereg(device_t self, int phy, int reg, int val)
553 {
554 	struct emac_softc *sc;
555 	sc = (struct emac_softc *)self;
556 	EMAC_WRITE(ETH_MAN, (ETH_MAN_HIGH | ETH_MAN_RW_WR
557 			     | ((phy << ETH_MAN_PHYA_SHIFT) & ETH_MAN_PHYA)
558 			     | ((reg << ETH_MAN_REGA_SHIFT) & ETH_MAN_REGA)
559 			     | ETH_MAN_CODE_IEEE802_3
560 			     | (val & ETH_MAN_DATA)));
561 	while (!(EMAC_READ(ETH_SR) & ETH_SR_IDLE)) ;
562 }
563 
564 
565 void
566 emac_statchg(device_t self)
567 {
568         struct emac_softc *sc = (struct emac_softc *)self;
569         u_int32_t reg;
570 
571         /*
572          * We must keep the MAC and the PHY in sync as
573          * to the status of full-duplex!
574          */
575 	reg = EMAC_READ(ETH_CFG);
576         if (sc->sc_mii.mii_media_active & IFM_FDX)
577                 reg |= ETH_CFG_FD;
578         else
579                 reg &= ~ETH_CFG_FD;
580 	EMAC_WRITE(ETH_CFG, reg);
581 }
582 
583 void
584 emac_tick(void *arg)
585 {
586 	struct emac_softc* sc = (struct emac_softc *)arg;
587 	struct ifnet * ifp = &sc->sc_ec.ec_if;
588 	int s;
589 	u_int32_t misses;
590 
591 	ifp->if_collisions += EMAC_READ(ETH_SCOL) + EMAC_READ(ETH_MCOL);
592 	/* These misses are ok, they will happen if the RAM/CPU can't keep up */
593 	misses = EMAC_READ(ETH_DRFC);
594 	if (misses > 0)
595 		printf("%s: %d rx misses\n", device_xname(sc->sc_dev), misses);
596 
597 	s = splnet();
598 	if (emac_gctx(sc) > 0 && IFQ_IS_EMPTY(&ifp->if_snd) == 0) {
599 		emac_ifstart(ifp);
600 	}
601 	splx(s);
602 
603 	mii_tick(&sc->sc_mii);
604 	callout_reset(&sc->emac_tick_ch, hz, emac_tick, sc);
605 }
606 
607 
608 static int
609 emac_ifioctl(struct ifnet *ifp, u_long cmd, void *data)
610 {
611 	struct emac_softc *sc = ifp->if_softc;
612 	struct ifreq *ifr = (struct ifreq *)data;
613 	int s, error;
614 
615 	s = splnet();
616 	switch(cmd) {
617 	case SIOCSIFMEDIA:
618 	case SIOCGIFMEDIA:
619 		error = ifmedia_ioctl(ifp, ifr, &sc->sc_mii.mii_media, cmd);
620 		break;
621 	default:
622 		error = ether_ioctl(ifp, cmd, data);
623 		if (error == ENETRESET) {
624 			if (ifp->if_flags & IFF_RUNNING)
625 				emac_setaddr(ifp);
626 			error = 0;
627 		}
628 	}
629 	splx(s);
630 	return error;
631 }
632 
633 static void
634 emac_ifstart(struct ifnet *ifp)
635 {
636 	struct emac_softc *sc = (struct emac_softc *)ifp->if_softc;
637 	struct mbuf *m;
638 	bus_dma_segment_t *segs;
639 	int s, bi, err, nsegs;
640 
641 	s = splnet();
642 start:
643 	if (emac_gctx(sc) == 0) {
644 		/* Enable transmit-buffer-free interrupt */
645 		EMAC_WRITE(ETH_IER, ETH_ISR_TBRE);
646 		ifp->if_flags |= IFF_OACTIVE;
647 		ifp->if_timer = 10;
648 		splx(s);
649 		return;
650 	}
651 
652 	ifp->if_timer = 0;
653 
654 	IFQ_POLL(&ifp->if_snd, m);
655 	if (m == NULL) {
656 		splx(s);
657 		return;
658 	}
659 //more:
660 	bi = (sc->txqi + sc->txqc) % TX_QLEN;
661 	if ((err = bus_dmamap_load_mbuf(sc->sc_dmat, sc->txq[bi].m_dmamap, m,
662 		BUS_DMA_NOWAIT)) ||
663 		sc->txq[bi].m_dmamap->dm_segs[0].ds_addr & 0x3 ||
664 		sc->txq[bi].m_dmamap->dm_nsegs > 1) {
665 		/* Copy entire mbuf chain to new single */
666 		struct mbuf *mn;
667 
668 		if (err == 0)
669 			bus_dmamap_unload(sc->sc_dmat, sc->txq[bi].m_dmamap);
670 
671 		MGETHDR(mn, M_DONTWAIT, MT_DATA);
672 		if (mn == NULL) goto stop;
673 		if (m->m_pkthdr.len > MHLEN) {
674 			MCLGET(mn, M_DONTWAIT);
675 			if ((mn->m_flags & M_EXT) == 0) {
676 				m_freem(mn);
677 				goto stop;
678 			}
679 		}
680 		m_copydata(m, 0, m->m_pkthdr.len, mtod(mn, void *));
681 		mn->m_pkthdr.len = mn->m_len = m->m_pkthdr.len;
682 		IFQ_DEQUEUE(&ifp->if_snd, m);
683 		m_freem(m);
684 		m = mn;
685 		bus_dmamap_load_mbuf(sc->sc_dmat, sc->txq[bi].m_dmamap, m,
686 			BUS_DMA_NOWAIT);
687 	} else {
688 		IFQ_DEQUEUE(&ifp->if_snd, m);
689 	}
690 
691 #if NBPFILTER > 0
692 	if (ifp->if_bpf)
693 		bpf_mtap(ifp->if_bpf, m);
694 #endif /* NBPFILTER > 0 */
695 
696 	nsegs = sc->txq[bi].m_dmamap->dm_nsegs;
697 	segs = sc->txq[bi].m_dmamap->dm_segs;
698 	if (nsegs > 1) {
699 		panic("#### ARGH #2");
700 	}
701 
702 	sc->txq[bi].m = m;
703 	sc->txqc++;
704 
705 	DPRINTFN(2,("%s: start sending idx #%i mbuf %p (txqc=%i, phys %p), len=%u\n", __FUNCTION__, bi, sc->txq[bi].m, sc->txqc, (void*)segs->ds_addr,
706 		       (unsigned)m->m_pkthdr.len));
707 #ifdef	DIAGNOSTIC
708 	if (sc->txqc > TX_QLEN) {
709 		panic("%s: txqc %i > %i", __FUNCTION__, sc->txqc, TX_QLEN);
710 	}
711 #endif
712 
713 	bus_dmamap_sync(sc->sc_dmat, sc->txq[bi].m_dmamap, 0,
714 		sc->txq[bi].m_dmamap->dm_mapsize,
715 		BUS_DMASYNC_PREWRITE);
716 
717 	EMAC_WRITE(ETH_TAR, segs->ds_addr);
718 	EMAC_WRITE(ETH_TCR, m->m_pkthdr.len);
719 	if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
720 		goto start;
721 stop:
722 
723 	splx(s);
724 	return;
725 }
726 
727 static void
728 emac_ifwatchdog(struct ifnet *ifp)
729 {
730 	struct emac_softc *sc = (struct emac_softc *)ifp->if_softc;
731 
732 	if ((ifp->if_flags & IFF_RUNNING) == 0)
733 		return;
734        	printf("%s: device timeout, CTL = 0x%08x, CFG = 0x%08x\n",
735 		device_xname(sc->sc_dev), EMAC_READ(ETH_CTL), EMAC_READ(ETH_CFG));
736 }
737 
738 static int
739 emac_ifinit(struct ifnet *ifp)
740 {
741 	struct emac_softc *sc = ifp->if_softc;
742 	int s = splnet();
743 
744 	callout_stop(&sc->emac_tick_ch);
745 
746 	// enable interrupts
747 	EMAC_WRITE(ETH_IDR, -1);
748 	EMAC_WRITE(ETH_IER, ETH_ISR_RCOM | ETH_ISR_TBRE | ETH_ISR_TIDLE
749 		   | ETH_ISR_RBNA | ETH_ISR_ROVR);
750 
751 	// enable transmitter / receiver
752 	EMAC_WRITE(ETH_CTL, ETH_CTL_TE | ETH_CTL_RE | ETH_CTL_ISR
753 		   | ETH_CTL_CSR | ETH_CTL_MPE);
754 
755 	mii_mediachg(&sc->sc_mii);
756 	callout_reset(&sc->emac_tick_ch, hz, emac_tick, sc);
757         ifp->if_flags |= IFF_RUNNING;
758 	splx(s);
759 	return 0;
760 }
761 
762 static void
763 emac_ifstop(struct ifnet *ifp, int disable)
764 {
765 //	u_int32_t u;
766 	struct emac_softc *sc = ifp->if_softc;
767 
768 #if 0
769 	EMAC_WRITE(ETH_CTL, ETH_CTL_MPE);	// disable everything
770 	EMAC_WRITE(ETH_IDR, -1);		// disable interrupts
771 //	EMAC_WRITE(ETH_RBQP, 0);		// clear receive
772 	EMAC_WRITE(ETH_CFG, ETH_CFG_CLK_32 | ETH_CFG_SPD | ETH_CFG_FD | ETH_CFG_BIG);
773 	EMAC_WRITE(ETH_TCR, 0);			// send nothing
774 //	(void)EMAC_READ(ETH_ISR);
775 	u = EMAC_READ(ETH_TSR);
776 	EMAC_WRITE(ETH_TSR, (u & (ETH_TSR_UND | ETH_TSR_COMP | ETH_TSR_BNQ
777 				  | ETH_TSR_IDLE | ETH_TSR_RLE
778 				  | ETH_TSR_COL|ETH_TSR_OVR)));
779 	u = EMAC_READ(ETH_RSR);
780 	EMAC_WRITE(ETH_RSR, (u & (ETH_RSR_OVR|ETH_RSR_REC|ETH_RSR_BNA)));
781 #endif
782 	callout_stop(&sc->emac_tick_ch);
783 
784 	/* Down the MII. */
785 	mii_down(&sc->sc_mii);
786 
787 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
788 	ifp->if_timer = 0;
789 	sc->sc_mii.mii_media_status &= ~IFM_ACTIVE;
790 }
791 
792 static void
793 emac_setaddr(struct ifnet *ifp)
794 {
795 	struct emac_softc *sc = ifp->if_softc;
796 	struct ethercom *ac = &sc->sc_ec;
797 	struct ether_multi *enm;
798 	struct ether_multistep step;
799 	u_int8_t ias[3][ETHER_ADDR_LEN];
800 	u_int32_t h, nma = 0, hashes[2] = { 0, 0 };
801 	u_int32_t ctl = EMAC_READ(ETH_CTL);
802 	u_int32_t cfg = EMAC_READ(ETH_CFG);
803 
804 	/* disable receiver temporarily */
805 	EMAC_WRITE(ETH_CTL, ctl & ~ETH_CTL_RE);
806 
807 	cfg &= ~(ETH_CFG_MTI | ETH_CFG_UNI | ETH_CFG_CAF | ETH_CFG_UNI);
808 
809 	if (ifp->if_flags & IFF_PROMISC) {
810 		cfg |=  ETH_CFG_CAF;
811 	} else {
812 		cfg &= ~ETH_CFG_CAF;
813 	}
814 
815 	// ETH_CFG_BIG?
816 
817 	ifp->if_flags &= ~IFF_ALLMULTI;
818 
819 	ETHER_FIRST_MULTI(step, ac, enm);
820 	while (enm != NULL) {
821 		if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
822 			/*
823 			 * We must listen to a range of multicast addresses.
824 			 * For now, just accept all multicasts, rather than
825 			 * trying to set only those filter bits needed to match
826 			 * the range.  (At this time, the only use of address
827 			 * ranges is for IP multicast routing, for which the
828 			 * range is big enough to require all bits set.)
829 			 */
830 			cfg |= ETH_CFG_CAF;
831 			hashes[0] = 0xffffffffUL;
832 			hashes[1] = 0xffffffffUL;
833 			ifp->if_flags |= IFF_ALLMULTI;
834 			nma = 0;
835 			break;
836 		}
837 
838 		if (nma < 3) {
839 			/* We can program 3 perfect address filters for mcast */
840 			memcpy(ias[nma], enm->enm_addrlo, ETHER_ADDR_LEN);
841 		} else {
842 			/*
843 			 * XXX: Datasheet is not very clear here, I'm not sure
844 			 * if I'm doing this right.  --joff
845 			 */
846 			h = ether_crc32_le(enm->enm_addrlo, ETHER_ADDR_LEN);
847 
848 			/* Just want the 6 most-significant bits. */
849 			h = h >> 26;
850 
851 			hashes[ h / 32 ] |=  (1 << (h % 32));
852 			cfg |= ETH_CFG_MTI;
853 		}
854 		ETHER_NEXT_MULTI(step, enm);
855 		nma++;
856 	}
857 
858 	// program...
859 	DPRINTFN(1,("%s: en0 %02x:%02x:%02x:%02x:%02x:%02x\n", __FUNCTION__,
860 		    sc->sc_enaddr[0], sc->sc_enaddr[1], sc->sc_enaddr[2],
861 		    sc->sc_enaddr[3], sc->sc_enaddr[4], sc->sc_enaddr[5]));
862 	EMAC_WRITE(ETH_SA1L, (sc->sc_enaddr[3] << 24)
863 		   | (sc->sc_enaddr[2] << 16) | (sc->sc_enaddr[1] << 8)
864 		   | (sc->sc_enaddr[0]));
865 	EMAC_WRITE(ETH_SA1H, (sc->sc_enaddr[5] << 8)
866 		   | (sc->sc_enaddr[4]));
867 	if (nma > 1) {
868 		DPRINTFN(1,("%s: en1 %02x:%02x:%02x:%02x:%02x:%02x\n", __FUNCTION__,
869 		       ias[0][0], ias[0][1], ias[0][2],
870 		       ias[0][3], ias[0][4], ias[0][5]));
871 		EMAC_WRITE(ETH_SA2L, (ias[0][3] << 24)
872 			   | (ias[0][2] << 16) | (ias[0][1] << 8)
873 			   | (ias[0][0]));
874 		EMAC_WRITE(ETH_SA2H, (ias[0][4] << 8)
875 			   | (ias[0][5]));
876 	}
877 	if (nma > 2) {
878 		DPRINTFN(1,("%s: en2 %02x:%02x:%02x:%02x:%02x:%02x\n", __FUNCTION__,
879 		       ias[1][0], ias[1][1], ias[1][2],
880 		       ias[1][3], ias[1][4], ias[1][5]));
881 		EMAC_WRITE(ETH_SA3L, (ias[1][3] << 24)
882 			   | (ias[1][2] << 16) | (ias[1][1] << 8)
883 			   | (ias[1][0]));
884 		EMAC_WRITE(ETH_SA3H, (ias[1][4] << 8)
885 			   | (ias[1][5]));
886 	}
887 	if (nma > 3) {
888 		DPRINTFN(1,("%s: en3 %02x:%02x:%02x:%02x:%02x:%02x\n", __FUNCTION__,
889 		       ias[2][0], ias[2][1], ias[2][2],
890 		       ias[2][3], ias[2][4], ias[2][5]));
891 		EMAC_WRITE(ETH_SA3L, (ias[2][3] << 24)
892 			   | (ias[2][2] << 16) | (ias[2][1] << 8)
893 			   | (ias[2][0]));
894 		EMAC_WRITE(ETH_SA3H, (ias[2][4] << 8)
895 			   | (ias[2][5]));
896 	}
897 	EMAC_WRITE(ETH_HSH, hashes[0]);
898 	EMAC_WRITE(ETH_HSL, hashes[1]);
899 	EMAC_WRITE(ETH_CFG, cfg);
900 	EMAC_WRITE(ETH_CTL, ctl | ETH_CTL_RE);
901 }
902