if_ate.c revision 155443
1/*-
2 * Copyright (c) 2006 M. Warner Losh.  All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 */
24
25/* TODO: (in no order)
26 *
27 * 5) Setup RX buffers in ateinit_locked
28 * 8) Need to sync busdma goo in atestop
29 * 9) atestop should maybe free the mbufs?
30 * 10) On Rx, how do we get a new mbuf?
31 *
32 * 1) detach
33 * 2) Free dma setup
34 * 3) Turn on the clock in pmc and turn on pins?  Turn off?
35 */
36
37#include <sys/cdefs.h>
38__FBSDID("$FreeBSD: head/sys/arm/at91/if_ate.c 155443 2006-02-07 20:48:52Z cognet $");
39
40#include <sys/param.h>
41#include <sys/systm.h>
42#include <sys/bus.h>
43#include <sys/kernel.h>
44#include <sys/mbuf.h>
45#include <sys/malloc.h>
46#include <sys/module.h>
47#include <sys/rman.h>
48#include <sys/socket.h>
49#include <sys/sockio.h>
50#include <machine/bus.h>
51
52#include <net/ethernet.h>
53#include <net/if.h>
54#include <net/if_arp.h>
55#include <net/if_dl.h>
56#include <net/if_media.h>
57#include <net/if_mib.h>
58#include <net/if_types.h>
59
60#ifdef INET
61#include <netinet/in.h>
62#include <netinet/in_systm.h>
63#include <netinet/in_var.h>
64#include <netinet/ip.h>
65#endif
66
67#include <net/bpf.h>
68#include <net/bpfdesc.h>
69
70#include <dev/mii/mii.h>
71#include <dev/mii/miivar.h>
72#include <arm/at91/if_atereg.h>
73
74#include "miibus_if.h"
75
76#define ATE_MAX_TX_BUFFERS 2		/* We have ping-pong tx buffers */
77#define ATE_MAX_RX_BUFFERS 8
78
79struct ate_softc
80{
81	struct ifnet *ifp;		/* ifnet pointer */
82	struct mtx sc_mtx;		/* basically a perimeter lock */
83	device_t dev;			/* Myself */
84	device_t miibus;		/* My child miibus */
85	void *intrhand;			/* Interrupt handle */
86	struct resource *irq_res;	/* IRQ resource */
87	struct resource	*mem_res;	/* Memory resource */
88	struct callout tick_ch;		/* Tick callout */
89	bus_dma_tag_t mtag;		/* bus dma tag for mbufs */
90	bus_dmamap_t tx_map[ATE_MAX_TX_BUFFERS];
91	bus_dma_tag_t rxtag;
92	bus_dmamap_t rx_map[ATE_MAX_RX_BUFFERS];
93	bus_dma_tag_t rx_desc_tag;
94	bus_dmamap_t rx_desc_map;
95	int txcur;			/* current tx map pointer */
96	struct mbuf *sent_mbuf[ATE_MAX_TX_BUFFERS]; /* Sent mbufs */
97	struct mbuf *rx_mbuf[ATE_MAX_RX_BUFFERS]; /* RX mbufs */
98	bus_addr_t rx_desc_phys;
99	eth_rx_desc_t *rx_descs;
100	struct	ifmib_iso_8802_3 mibdata; /* stuff for network mgmt */
101};
102
103static inline uint32_t
104RD4(struct ate_softc *sc, bus_size_t off)
105{
106	return bus_read_4(sc->mem_res, off);
107}
108
109static inline void
110WR4(struct ate_softc *sc, bus_size_t off, uint32_t val)
111{
112	bus_write_4(sc->mem_res, off, val);
113}
114
115#define ATE_LOCK(_sc)		mtx_lock(&(_sc)->sc_mtx)
116#define	ATE_UNLOCK(_sc)		mtx_unlock(&(_sc)->sc_mtx)
117#define ATE_LOCK_INIT(_sc) \
118	mtx_init(&_sc->sc_mtx, device_get_nameunit(_sc->dev), \
119	    MTX_NETWORK_LOCK, MTX_DEF)
120#define ATE_LOCK_DESTROY(_sc)	mtx_destroy(&_sc->sc_mtx);
121#define ATE_ASSERT_LOCKED(_sc)	mtx_assert(&_sc->sc_mtx, MA_OWNED);
122#define ATE_ASSERT_UNLOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_NOTOWNED);
123
124static devclass_t ate_devclass;
125
126/* ifnet entry points */
127
128static void ateinit_locked(void *);
129static void atestart_locked(struct ifnet *);
130
131static void ateinit(void *);
132static void atestart(struct ifnet *);
133static void atestop(struct ate_softc *);
134static void atewatchdog(struct ifnet *);
135static int ateioctl(struct ifnet * ifp, u_long, caddr_t);
136
137/* bus entry points */
138
139static int ate_probe(device_t dev);
140static int ate_attach(device_t dev);
141static int ate_detach(device_t dev);
142static void ate_intr(void *);
143
144/* helper routines */
145static int ate_activate(device_t dev);
146static void ate_deactivate(device_t dev);
147static int ate_ifmedia_upd(struct ifnet *ifp);
148static void ate_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr);
149static void ate_get_mac(struct ate_softc *sc, u_char *eaddr);
150
151/*
152 * The AT91 family of products has the ethernet called EMAC.  However,
153 * it isn't self identifying.  It is anticipated that the parent bus
154 * code will take care to only add ate devices where they really are.  As
155 * such, we do nothing here to identify the device and just set its name.
156 */
157static int
158ate_probe(device_t dev)
159{
160	device_set_desc(dev, "EMAC");
161	return (0);
162}
163
164static int
165ate_attach(device_t dev)
166{
167	struct ate_softc *sc = device_get_softc(dev);
168	struct ifnet *ifp = NULL;
169	int err;
170	u_char eaddr[6];
171
172	sc->dev = dev;
173	err = ate_activate(dev);
174	if (err)
175		goto out;
176
177	/* calling atestop before ifp is set is OK */
178	atestop(sc);
179	ATE_LOCK_INIT(sc);
180	callout_init_mtx(&sc->tick_ch, &sc->sc_mtx, 0);
181
182	ate_get_mac(sc, eaddr);
183
184	sc->ifp = ifp = if_alloc(IFT_ETHER);
185	if (mii_phy_probe(dev, &sc->miibus, ate_ifmedia_upd, ate_ifmedia_sts)) {
186		device_printf(dev, "Cannot find my PHY.\n");
187		err = ENXIO;
188		goto out;
189	}
190
191	ifp->if_softc = sc;
192	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
193	ifp->if_mtu = ETHERMTU;
194	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
195	ifp->if_start = atestart;
196	ifp->if_ioctl = ateioctl;
197	ifp->if_watchdog = atewatchdog;
198	ifp->if_init = ateinit;
199	ifp->if_baudrate = 10000000;
200	IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
201	ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
202	IFQ_SET_READY(&ifp->if_snd);
203	ifp->if_timer = 0;
204	ifp->if_linkmib = &sc->mibdata;
205	ifp->if_linkmiblen = sizeof(sc->mibdata);
206	sc->mibdata.dot3Compliance = DOT3COMPLIANCE_COLLS;
207
208	ether_ifattach(ifp, eaddr);
209
210	/*
211	 * Activate the interrupt
212	 */
213	err = bus_setup_intr(dev, sc->irq_res, INTR_TYPE_NET | INTR_MPSAFE,
214	    ate_intr, sc, &sc->intrhand);
215	if (err) {
216		ether_ifdetach(ifp);
217		ATE_LOCK_DESTROY(sc);
218	}
219out:;
220	if (err)
221		ate_deactivate(dev);
222	if (err && ifp)
223		if_free(ifp);
224	return (err);
225}
226
227static int
228ate_detach(device_t dev)
229{
230	return EBUSY;	/* XXX TODO(1) */
231}
232
233static void
234ate_getaddr(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
235{
236	struct ate_softc *sc;
237
238	if (error != 0)
239		return;
240	sc = (struct ate_softc *)arg;
241	sc->rx_desc_phys = segs[0].ds_addr;
242}
243
244/*
245 * Compute the multicast filter for this device using the standard
246 * algorithm.  I wonder why this isn't in ether somewhere as a lot
247 * of different MAC chips use this method (or the reverse the bits)
248 * method.
249 */
250static void
251ate_setmcast(struct ate_softc *sc)
252{
253	uint32_t index;
254	uint32_t mcaf[2];
255	u_char *af = (u_char *) mcaf;
256	struct ifmultiaddr *ifma;
257
258	mcaf[0] = 0;
259	mcaf[1] = 0;
260
261	IF_ADDR_LOCK(sc->ifp);
262	TAILQ_FOREACH(ifma, &sc->ifp->if_multiaddrs, ifma_link) {
263		if (ifma->ifma_addr->sa_family != AF_LINK)
264			continue;
265		index = ether_crc32_be(LLADDR((struct sockaddr_dl *)
266		    ifma->ifma_addr), ETHER_ADDR_LEN) >> 26;
267		af[index >> 3] |= 1 << (index & 7);
268	}
269	IF_ADDR_UNLOCK(sc->ifp);
270
271	/*
272	 * Write the hash to the hash register.  This card can also
273	 * accept unicast packets as well as multicast packets using this
274	 * register for easier bridging operations, but we don't take
275	 * advantage of that.  Locks here are to avoid LOR with the
276	 * IF_ADDR_LOCK, but might not be strictly necessary.
277	 */
278	ATE_LOCK(sc);
279	WR4(sc, ETH_HSL, mcaf[0]);
280	WR4(sc, ETH_HSH, mcaf[1]);
281	ATE_UNLOCK(sc);
282}
283
284static int
285ate_activate(device_t dev)
286{
287	struct ate_softc *sc;
288	int rid, err, i;
289
290	sc = device_get_softc(dev);
291	rid = 0;
292	sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
293	    RF_ACTIVE);
294	if (sc->mem_res == NULL)
295		goto errout;
296	rid = 0;
297	sc->irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
298	    RF_ACTIVE);
299	if (sc->mem_res == NULL)
300		goto errout;
301
302	/*
303	 * Allocate DMA tags and maps
304	 */
305	err = bus_dma_tag_create(NULL, 1, 0, BUS_SPACE_MAXADDR_32BIT,
306	    BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES, 1, MCLBYTES, 0,
307	    busdma_lock_mutex, &sc->sc_mtx, &sc->mtag);
308	if (err != 0)
309		goto errout;
310	for (i = 0; i < ATE_MAX_TX_BUFFERS; i++) {
311		err = bus_dmamap_create(sc->mtag, 0, &sc->tx_map[i]);
312		if (err != 0)
313			goto errout;
314	}
315	 /*
316	  * Allocate our Rx buffers.  This chip has a rx structure that's filled
317	  * in
318	  */
319
320	/*
321	 * Allocate DMA tags and maps for RX.
322	 */
323	err = bus_dma_tag_create(NULL, 1, 0, BUS_SPACE_MAXADDR_32BIT,
324	    BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES, 1, MCLBYTES, 0,
325	    busdma_lock_mutex, &sc->sc_mtx, &sc->rxtag);
326	if (err != 0)
327		goto errout;
328	for (i = 0; i < ATE_MAX_RX_BUFFERS; i++) {
329		err = bus_dmamap_create(sc->rxtag, 0, &sc->rx_map[i]);
330		if (err != 0)
331			goto errout;
332	}
333
334	/* Dma TAG and MAP for the rx descriptors. */
335	err = bus_dma_tag_create(NULL, sizeof(eth_rx_desc_t), 0,
336	    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
337	    ATE_MAX_RX_BUFFERS * sizeof(eth_rx_desc_t), 1,
338	    ATE_MAX_RX_BUFFERS * sizeof(eth_rx_desc_t), 0, busdma_lock_mutex,
339	    &sc->sc_mtx, &sc->rx_desc_tag);
340	if (err != 0)
341		goto errout;
342	if (bus_dmamem_alloc(sc->rx_desc_tag, (void **)&sc->rx_descs, M_WAITOK,
343	    &sc->rx_desc_map) != 0)
344		goto errout;
345	if (bus_dmamap_load(sc->rx_desc_tag, sc->rx_desc_map,
346	    sc->rx_descs, ATE_MAX_RX_BUFFERS * sizeof(eth_rx_desc_t),
347	    ate_getaddr, sc, 0) != 0)
348		goto errout;
349	/* XXX TODO(5) Put this in ateinit_locked? */
350	for (i = 0; i < ATE_MAX_RX_BUFFERS; i++) {
351		bus_dma_segment_t seg;
352		int nsegs;
353
354		sc->rx_mbuf[i] = m_getcl(M_WAITOK, MT_DATA, M_PKTHDR);
355		sc->rx_mbuf[i]->m_len = sc->rx_mbuf[i]->m_pkthdr.len =
356		    MCLBYTES;
357		if (bus_dmamap_load_mbuf_sg(sc->rxtag, sc->rx_map[i],
358		    sc->rx_mbuf[i], &seg, &nsegs, 0) != 0)
359			goto errout;
360		/*
361		 * For the last buffer, set the wrap bit so the controller
362		 * restarts from the first descriptor.
363		 */
364		if (i == ATE_MAX_RX_BUFFERS - 1)
365			seg.ds_addr |= 1 << 1;
366		sc->rx_descs[i].addr = seg.ds_addr;
367		sc->rx_descs[i].status = 0;
368		bus_dmamap_sync(sc->rxtag, sc->rx_map[i], BUS_DMASYNC_PREWRITE);
369	}
370	bus_dmamap_sync(sc->rx_desc_tag, sc->rx_desc_map, BUS_DMASYNC_PREWRITE);
371	/* Write the descriptor queue address. */
372	WR4(sc, ETH_RBQP, sc->rx_desc_phys);
373	return (0);
374errout:
375	ate_deactivate(dev);
376	return (ENOMEM);
377}
378
379static void
380ate_deactivate(device_t dev)
381{
382	struct ate_softc *sc;
383
384	sc = device_get_softc(dev);
385	/* XXX TODO(2) teardown busdma junk, below from fxp -- customize */
386#if 0
387	if (sc->fxp_mtag) {
388		for (i = 0; i < FXP_NRFABUFS; i++) {
389			rxp = &sc->fxp_desc.rx_list[i];
390			if (rxp->rx_mbuf != NULL) {
391				bus_dmamap_sync(sc->fxp_mtag, rxp->rx_map,
392				    BUS_DMASYNC_POSTREAD);
393				bus_dmamap_unload(sc->fxp_mtag, rxp->rx_map);
394				m_freem(rxp->rx_mbuf);
395			}
396			bus_dmamap_destroy(sc->fxp_mtag, rxp->rx_map);
397		}
398		bus_dmamap_destroy(sc->fxp_mtag, sc->spare_map);
399		for (i = 0; i < FXP_NTXCB; i++) {
400			txp = &sc->fxp_desc.tx_list[i];
401			if (txp->tx_mbuf != NULL) {
402				bus_dmamap_sync(sc->fxp_mtag, txp->tx_map,
403				    BUS_DMASYNC_POSTWRITE);
404				bus_dmamap_unload(sc->fxp_mtag, txp->tx_map);
405				m_freem(txp->tx_mbuf);
406			}
407			bus_dmamap_destroy(sc->fxp_mtag, txp->tx_map);
408		}
409		bus_dma_tag_destroy(sc->fxp_mtag);
410	}
411	if (sc->fxp_stag)
412		bus_dma_tag_destroy(sc->fxp_stag);
413	if (sc->cbl_tag)
414		bus_dma_tag_destroy(sc->cbl_tag);
415	if (sc->mcs_tag)
416		bus_dma_tag_destroy(sc->mcs_tag);
417#endif
418	if (sc->intrhand)
419		bus_teardown_intr(dev, sc->irq_res, sc->intrhand);
420	sc->intrhand = 0;
421	bus_generic_detach(sc->dev);
422	if (sc->miibus)
423		device_delete_child(sc->dev, sc->miibus);
424	if (sc->mem_res)
425		bus_release_resource(dev, SYS_RES_IOPORT,
426		    rman_get_rid(sc->mem_res), sc->mem_res);
427	sc->mem_res = 0;
428	if (sc->irq_res)
429		bus_release_resource(dev, SYS_RES_IRQ,
430		    rman_get_rid(sc->irq_res), sc->irq_res);
431	sc->irq_res = 0;
432	return;
433}
434
435/*
436 * Change media according to request.
437 */
438static int
439ate_ifmedia_upd(struct ifnet *ifp)
440{
441	struct ate_softc *sc = ifp->if_softc;
442	struct mii_data *mii;
443
444	mii = device_get_softc(sc->miibus);
445	ATE_LOCK(sc);
446	mii_mediachg(mii);
447	ATE_UNLOCK(sc);
448	return (0);
449}
450
451/*
452 * Notify the world which media we're using.
453 */
454static void
455ate_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
456{
457	struct ate_softc *sc = ifp->if_softc;
458	struct mii_data *mii;
459
460	mii = device_get_softc(sc->miibus);
461	ATE_LOCK(sc);
462	mii_pollstat(mii);
463	ifmr->ifm_active = mii->mii_media_active;
464	ifmr->ifm_status = mii->mii_media_status;
465	ATE_UNLOCK(sc);
466}
467
468static void
469ate_tick(void *xsc)
470{
471	struct ate_softc *sc = xsc;
472	struct mii_data *mii;
473	int active;
474
475	/*
476	 * The KB920x boot loader tests ETH_SR & ETH_SR_LINK and will ask
477	 * the MII if there's a link if this bit is clear.  Not sure if we
478	 * should do the same thing here or not.
479	 */
480	ATE_ASSERT_LOCKED(sc);
481	if (sc->miibus != NULL) {
482		mii = device_get_softc(sc->miibus);
483		active = mii->mii_media_active;
484		mii_tick(mii);
485		if (mii->mii_media_status & IFM_ACTIVE &&
486		     active != mii->mii_media_active) {
487			/*
488			 * The speed and full/half-duplex state needs
489			 * to be reflected in the ETH_CFG register, it
490			 * seems.
491			 */
492			if (IFM_SUBTYPE(mii->mii_media_active) == IFM_10_T)
493				WR4(sc, ETH_CFG, RD4(sc, ETH_CFG) &
494				    ~ETH_CFG_SPD);
495			else
496				WR4(sc, ETH_CFG, RD4(sc, ETH_CFG) |
497				    ETH_CFG_SPD);
498			if (mii->mii_media_active & IFM_FDX)
499				WR4(sc, ETH_CFG, RD4(sc, ETH_CFG) |
500				    ETH_CFG_FD);
501			else
502				WR4(sc, ETH_CFG, RD4(sc, ETH_CFG) &
503				    ~ETH_CFG_FD);
504		}
505	}
506
507	/*
508	 * Update the stats as best we can.  When we're done, clear
509	 * the status counters and start over.  We're supposed to read these
510	 * registers often enough that they won't overflow.  Hopefully
511	 * once a second is often enough.  Some don't map well to
512	 * the dot3Stats mib, so for those we just count them as general
513	 * errors.  Stats for iframes, ibutes, oframes and obytes are
514	 * collected elsewhere.  These registers zero on a read to prevent
515	 * races.
516	 */
517	sc->mibdata.dot3StatsAlignmentErrors += RD4(sc, ETH_ALE);
518	sc->mibdata.dot3StatsFCSErrors += RD4(sc, ETH_SEQE);
519	sc->mibdata.dot3StatsSingleCollisionFrames += RD4(sc, ETH_SCOL);
520	sc->mibdata.dot3StatsMultipleCollisionFrames += RD4(sc, ETH_MCOL);
521	sc->mibdata.dot3StatsSQETestErrors += RD4(sc, ETH_SQEE);
522	sc->mibdata.dot3StatsDeferredTransmissions += RD4(sc, ETH_DTE);
523	sc->mibdata.dot3StatsLateCollisions += RD4(sc, ETH_LCOL);
524	sc->mibdata.dot3StatsExcessiveCollisions += RD4(sc, ETH_ECOL);
525	sc->mibdata.dot3StatsCarrierSenseErrors += RD4(sc, ETH_CSE);
526	sc->mibdata.dot3StatsFrameTooLongs += RD4(sc, ETH_ELR);
527	sc->mibdata.dot3StatsInternalMacReceiveErrors += RD4(sc, ETH_DRFC);
528	/*
529	 * not sure where to lump these, so count them against the errors
530	 * for the interface.
531	 */
532	sc->ifp->if_oerrors += RD4(sc, ETH_CSE) + RD4(sc, ETH_TUE);
533	sc->ifp->if_ierrors += RD4(sc, ETH_CDE) + RD4(sc, ETH_RJB) +
534	    RD4(sc, ETH_USF);
535
536	/*
537	 * Schedule another timeout one second from now.
538	 */
539	callout_reset(&sc->tick_ch, hz, ate_tick, sc);
540}
541
542static void
543ate_get_mac(struct ate_softc *sc, u_char *eaddr)
544{
545    uint32_t low, high;
546
547    /*
548     * The KB920x loaders will setup the MAC with an address, if one
549     * is set in the loader.  The TSC loader will also set the MAC address
550     * in a similar way.  Grab the MAC address from the SA1[HL] registers.
551     */
552    low = RD4(sc, ETH_SA1L);
553    high =  RD4(sc, ETH_SA1H);
554    eaddr[0] = (high >> 8) & 0xff;
555    eaddr[1] = high & 0xff;
556    eaddr[2] = (low >> 24) & 0xff;
557    eaddr[3] = (low >> 16) & 0xff;
558    eaddr[4] = (low >> 8) & 0xff;
559    eaddr[5] = low & 0xff;
560}
561
562static void
563ate_intr(void *xsc)
564{
565	struct ate_softc *sc = xsc;
566	int status;
567	int i;
568
569	status = RD4(sc, ETH_ISR);
570	if (status == 0)
571		return;
572	printf("IT IS %x %x\n", RD4(sc, ETH_RSR), RD4(sc, ETH_CTL));
573
574	if (status & ETH_ISR_RCOM) {
575		bus_dmamap_sync(sc->rx_desc_tag, sc->rx_desc_map,
576		    BUS_DMASYNC_POSTREAD);
577		for (i = 0; i < ATE_MAX_RX_BUFFERS; i++) {
578			if (sc->rx_descs[i].addr & ETH_CPU_OWNER) {
579				struct mbuf *mb = sc->rx_mbuf[i];
580				bus_dma_segment_t seg;
581				int rx_stat = sc->rx_descs[i].status;
582				int nsegs;
583
584				printf("GOT ONE\n");
585				bus_dmamap_sync(sc->rxtag,
586				    sc->rx_map[i], BUS_DMASYNC_POSTREAD);
587				bus_dmamap_unload(sc->rxtag,
588				    sc->rx_map[i]);
589				WR4(sc, ETH_RSR, RD4(sc, ETH_RSR));
590				/*
591				 * Allocate a new buffer to replace this one.
592				 * if we cannot, then we drop this packet
593				 * and keep the old buffer we had.
594				 */
595				sc->rx_mbuf[i] = m_getcl(M_DONTWAIT, MT_DATA,
596				    M_PKTHDR);
597				if (!sc->rx_mbuf[i]) {
598					sc->rx_mbuf[i] = mb;
599					sc->rx_descs[i].addr &= ~ETH_CPU_OWNER;
600					bus_dmamap_sync(sc->rx_desc_tag,
601					    sc->rx_desc_map,
602					    BUS_DMASYNC_PREWRITE);
603					continue;
604				}
605				if (bus_dmamap_load_mbuf_sg(sc->rxtag,
606				    sc->rx_map[i],
607				    sc->rx_mbuf[i], &seg, &nsegs, 0) != 0) {
608					sc->rx_mbuf[i] = mb;
609					sc->rx_descs[i].addr &= ~ETH_CPU_OWNER;
610					bus_dmamap_sync(sc->rx_desc_tag,
611					    sc->rx_desc_map,
612					    BUS_DMASYNC_PREWRITE);
613					continue;
614				}
615				mb->m_len = sc->rx_descs[i].status &
616				    ETH_LEN_MASK;
617				mb->m_pkthdr.len = mb->m_len;
618				mb->m_pkthdr.rcvif = sc->ifp;
619				/*
620				 * For the last buffer, set the wrap bit so
621				 * the controller restarts from the first
622				 * descriptor.
623				 */
624				if (i == ATE_MAX_RX_BUFFERS - 1)
625					seg.ds_addr |= 1 << 1;
626				sc->rx_descs[i].addr = seg.ds_addr;
627				sc->rx_descs[i].status = 0;
628				mb->m_len = rx_stat & ETH_LEN_MASK;
629				(*sc->ifp->if_input)(sc->ifp, mb);
630				break;
631			}
632		}
633	}
634	if (status & ETH_ISR_TCOM) {
635		if (sc->sent_mbuf[0])
636			m_freem(sc->sent_mbuf[0]);
637		if (sc->sent_mbuf[1]) {
638			if (RD4(sc, ETH_TSR) & ETH_TSR_IDLE) {
639				m_freem(sc->sent_mbuf[1]);
640				sc->txcur = 0;
641				sc->sent_mbuf[0] = sc->sent_mbuf[1] = NULL;
642			} else {
643				sc->sent_mbuf[0] = sc->sent_mbuf[1];
644				sc->sent_mbuf[1] = NULL;
645				sc->txcur = 1;
646			}
647		} else {
648			sc->sent_mbuf[0] = NULL;
649			sc->txcur = 0;
650		}
651	}
652	if (status & ETH_ISR_RBNA) {
653		/* Workaround Errata #11 */
654		WR4(sc, ETH_CTL, RD4(sc, ETH_CTL) &~ ETH_CTL_RE);
655		WR4(sc, ETH_CTL, RD4(sc, ETH_CTL) | ETH_CTL_RE);
656	}
657}
658
659/*
660 * Reset and initialize the chip
661 */
662static void
663ateinit_locked(void *xsc)
664{
665	struct ate_softc *sc = xsc;
666	struct ifnet *ifp = sc->ifp;
667
668	ATE_ASSERT_LOCKED(sc);
669
670	/*
671	 * XXX TODO(3)
672	 * we need to turn on the EMAC clock in the pmc.  With the
673	 * default boot loader, this is already turned on.  However, we
674	 * need to think about how best to turn it on/off as the interface
675	 * is brought up/down, as well as dealing with the mii bus...
676	 *
677	 * We also need to multiplex the pins correctly.
678	 */
679
680	/*
681	 * There are two different ways that the mii bus is connected
682	 * to this chip.  Select the right one based on a compile-time
683	 * option.
684	 */
685#ifdef ATE_USE_RMII
686	WR4(sc, ETH_CFG, RD4(sc, ETH_CFG) | ETH_CFG_RMII);
687#else
688	WR4(sc, ETH_CFG, RD4(sc, ETH_CFG) & ~ETH_CFG_RMII);
689#endif
690	/*
691	 * Turn on the multicast hash, and write 0's to it.
692	 */
693	WR4(sc, ETH_CFG, RD4(sc, ETH_CFG) | ETH_CFG_MTI);
694	WR4(sc, ETH_HSH, 0);
695	WR4(sc, ETH_HSL, 0);
696
697	WR4(sc, ETH_CTL, RD4(sc, ETH_CTL) | ETH_CTL_TE | ETH_CTL_RE);
698	WR4(sc, ETH_IER, /*ETH_ISR_RCOM | ETH_ISR_TCOM | ETH_ISR_RBNA*/
699	    0xffffffff);
700
701	/*
702	 * Boot loader fills in MAC address.  If that's not the case, then
703	 * we should set SA1L and SA1H here to the appropriate value.  Note:
704	 * the byte order is big endian, not little endian, so we have some
705	 * swapping to do.  Again, if we need it (which I don't think we do).
706	 */
707
708	ate_setmcast(sc);
709
710	/*
711	 * Set 'running' flag, and clear output active flag
712	 * and attempt to start the output
713	 */
714	ifp->if_drv_flags |= IFF_DRV_RUNNING;
715	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
716	atestart_locked(ifp);
717
718	callout_reset(&sc->tick_ch, hz, ate_tick, sc);
719}
720
721/*
722 * dequeu packets and transmit
723 */
724static void
725atestart_locked(struct ifnet *ifp)
726{
727	struct ate_softc *sc = ifp->if_softc;
728	struct mbuf *m, *mdefrag;
729	bus_dma_segment_t segs[1];
730	int nseg;
731
732	ATE_ASSERT_LOCKED(sc);
733	if (ifp->if_drv_flags & IFF_DRV_OACTIVE)
734		return;
735
736outloop:
737	/*
738	 * check to see if there's room to put another packet into the
739	 * xmit queue.  The EMAC chip has a ping-pong buffer for xmit
740	 * packets.  We use OACTIVE to indicate "we can stuff more into
741	 * our buffers (clear) or not (set)."
742	 */
743	if (!(RD4(sc, ETH_TSR) & ETH_TSR_BNQ)) {
744		ifp->if_drv_flags |= IFF_DRV_OACTIVE;
745		return;
746	}
747	IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
748	if (m == 0) {
749		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
750		return;
751	}
752	mdefrag = m_defrag(m, M_DONTWAIT);
753	if (mdefrag == NULL) {
754		m_freem(m);
755		return;
756	}
757	m = mdefrag;
758
759	if (bus_dmamap_load_mbuf_sg(sc->mtag, sc->tx_map[sc->txcur], m, segs,
760	    &nseg, 0) != 0) {
761		m_free(m);
762		goto outloop;
763	}
764	bus_dmamap_sync(sc->mtag, sc->tx_map[sc->txcur], BUS_DMASYNC_PREWRITE);
765	sc->sent_mbuf[sc->txcur] = m;
766	sc->txcur++;
767	if (sc->txcur >= ATE_MAX_TX_BUFFERS)
768		sc->txcur = 0;
769
770	/*
771	 * tell the hardware to xmit the packet.
772	 */
773	WR4(sc, ETH_TAR, segs[0].ds_addr);
774	WR4(sc, ETH_TCR, segs[0].ds_len);
775
776	/*
777	 * Tap off here if there is a bpf listener.
778	 */
779	BPF_MTAP(ifp, m);
780
781	/*
782	 * Once we've queued one packet, we'll do the rest via the ISR,
783	 * save off a pointer.
784	 */
785	sc->sent_mbuf[1] = m;
786}
787
788static void
789ateinit(void *xsc)
790{
791	struct ate_softc *sc = xsc;
792	ATE_LOCK(sc);
793	ateinit_locked(sc);
794	ATE_UNLOCK(sc);
795}
796
797static void
798atestart(struct ifnet *ifp)
799{
800	struct ate_softc *sc = ifp->if_softc;
801	ATE_LOCK(sc);
802	atestart_locked(ifp);
803	ATE_UNLOCK(sc);
804}
805
806/*
807 * Turn off interrupts, and stop the nic.  Can be called with sc->ifp NULL
808 * so be careful.
809 */
810static void
811atestop(struct ate_softc *sc)
812{
813	struct ifnet *ifp = sc->ifp;
814
815	if (ifp) {
816		ifp->if_timer = 0;
817		ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
818	}
819
820	callout_stop(&sc->tick_ch);
821
822	/*
823	 * Enable some parts of the MAC that are needed always (like the
824	 * MII bus.  This turns off the RE and TE bits, which will remain
825	 * off until ateinit() is called to turn them on.  With RE and TE
826	 * turned off, there's no DMA to worry about after this write.
827	 */
828	WR4(sc, ETH_CTL, ETH_CTL_MPE);
829
830	/*
831	 * Turn off all the configured options and revert to defaults.
832	 */
833	WR4(sc, ETH_CFG, ETH_CFG_CLK_32);
834
835	/*
836	 * Turn off all the interrupts, and ack any pending ones by reading
837	 * the ISR.
838	 */
839	WR4(sc, ETH_IDR, 0xffffffff);
840	RD4(sc, ETH_ISR);
841
842	/*
843	 * Clear out the Transmit and Receiver Status registers of any
844	 * errors they may be reporting
845	 */
846	WR4(sc, ETH_TSR, 0xffffffff);
847	WR4(sc, ETH_RSR, 0xffffffff);
848
849	/*
850	 * XXX TODO(8)
851	 * need to worry about the busdma resources?  Yes, I think we need
852	 * to sync and unload them.  We may also need to release the mbufs
853	 * that are assocaited with RX and TX operations.
854	 */
855
856	/*
857	 * XXX we should power down the EMAC if it isn't in use, after
858	 * putting it into loopback mode.  This saves about 400uA according
859	 * to the datasheet.
860	 */
861}
862
863static void
864atewatchdog(struct ifnet *ifp)
865{
866	struct ate_softc *sc = ifp->if_softc;
867
868	ATE_LOCK(sc);
869	device_printf(sc->dev, "Device timeout\n");
870	ifp->if_oerrors++;
871	ateinit_locked(sc);
872	ATE_UNLOCK(sc);
873}
874
875static int
876ateioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
877{
878	struct ate_softc *sc = ifp->if_softc;
879	int             error = 0;
880
881	switch (cmd) {
882	case SIOCSIFFLAGS:
883		ATE_LOCK(sc);
884		if ((ifp->if_flags & IFF_UP) == 0 &&
885		    ifp->if_drv_flags & IFF_DRV_RUNNING) {
886			ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
887			atestop(sc);
888		} else {
889			/* reinitialize card on any parameter change */
890			ateinit_locked(sc);
891		}
892		ATE_UNLOCK(sc);
893		break;
894
895	case SIOCADDMULTI:
896	case SIOCDELMULTI:
897		/* update multicast filter list. */
898		ate_setmcast(sc);
899		error = 0;
900		break;
901
902	default:
903		error = ether_ioctl(ifp, cmd, data);
904		break;
905	}
906	return (error);
907}
908
909static void
910ate_child_detached(device_t dev, device_t child)
911{
912	struct ate_softc *sc;
913
914	sc = device_get_softc(dev);
915	if (child == sc->miibus)
916		sc->miibus = NULL;
917}
918
919/*
920 * MII bus support routines.
921 */
922static int
923ate_miibus_readreg(device_t dev, int phy, int reg)
924{
925	struct ate_softc *sc;
926	int val;
927
928	/*
929	 * XXX if we implement agressive power savings, then we need
930	 * XXX to make sure that the clock to the emac is on here
931	 */
932
933	if (phy != 0)
934		return (0xffff);
935	sc = device_get_softc(dev);
936	DELAY(1);	/* Hangs w/o this delay really 30.5us atm */
937	WR4(sc, ETH_MAN, ETH_MAN_REG_RD(phy, reg));
938	while ((RD4(sc, ETH_SR) & ETH_SR_IDLE) == 0)
939		continue;
940	val = RD4(sc, ETH_MAN) & ETH_MAN_VALUE_MASK;
941
942	return (val);
943}
944
945static void
946ate_miibus_writereg(device_t dev, int phy, int reg, int data)
947{
948	struct ate_softc *sc;
949
950	/*
951	 * XXX if we implement agressive power savings, then we need
952	 * XXX to make sure that the clock to the emac is on here
953	 */
954
955	sc = device_get_softc(dev);
956	WR4(sc, ETH_MAN, ETH_MAN_REG_WR(phy, reg, data));
957	while ((RD4(sc, ETH_SR) & ETH_SR_IDLE) == 0)
958		continue;
959	return;
960}
961
962static device_method_t ate_methods[] = {
963	/* Device interface */
964	DEVMETHOD(device_probe,		ate_probe),
965	DEVMETHOD(device_attach,	ate_attach),
966	DEVMETHOD(device_detach,	ate_detach),
967
968	/* Bus interface */
969	DEVMETHOD(bus_child_detached,	ate_child_detached),
970
971	/* MII interface */
972	DEVMETHOD(miibus_readreg,	ate_miibus_readreg),
973	DEVMETHOD(miibus_writereg,	ate_miibus_writereg),
974
975	{ 0, 0 }
976};
977
978static driver_t ate_driver = {
979	"ate",
980	ate_methods,
981	sizeof(struct ate_softc),
982};
983
984DRIVER_MODULE(ate, atmelarm, ate_driver, ate_devclass, 0, 0);
985DRIVER_MODULE(miibus, ate, miibus_driver, miibus_devclass, 0, 0);
986MODULE_DEPEND(ate, miibus, 1, 1, 1);
987MODULE_DEPEND(ate, ether, 1, 1, 1);
988