2 * Copyright (c) 2006 M. Warner Losh. All rights reserved.
3 * Copyright (c) 2009 Greg Ansley. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * 1) Turn on the clock in pmc? Turn off?
30 * 2) GPIO initializtion in board setup code.
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include <sys/param.h>
37 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/malloc.h>
42 #include <sys/module.h>
44 #include <sys/socket.h>
45 #include <sys/sockio.h>
46 #include <sys/sysctl.h>
48 #include <machine/bus.h>
50 #include <net/ethernet.h>
52 #include <net/if_arp.h>
53 #include <net/if_dl.h>
54 #include <net/if_media.h>
55 #include <net/if_mib.h>
56 #include <net/if_types.h>
59 #include <netinet/in.h>
60 #include <netinet/in_systm.h>
61 #include <netinet/in_var.h>
62 #include <netinet/ip.h>
66 #include <net/bpfdesc.h>
68 #include <dev/mii/mii.h>
69 #include <dev/mii/miivar.h>
72 #include <arm/at91/at91reg.h>
73 #include <arm/at91/at91var.h>
74 #include <arm/at91/if_atereg.h>
76 #include "miibus_if.h"
79 * Driver-specific flags.
81 #define ATE_FLAG_DETACHING 0x01
82 #define ATE_FLAG_MULTICAST 0x02
85 * Old EMAC assumes whole packet fits in one buffer;
86 * new EBACB assumes all receive buffers are 128 bytes
88 #define RX_BUF_SIZE(sc) (sc->is_emacb ? 128 : MCLBYTES)
91 * EMACB has an 11 bit counter for Rx/Tx Descriptors
92 * for max total of 1024 decriptors each.
94 #define ATE_MAX_RX_DESCR 1024
95 #define ATE_MAX_TX_DESCR 1024
97 /* How many buffers to allocate */
98 #define ATE_MAX_TX_BUFFERS 4 /* We have ping-pong tx buffers */
100 /* How much memory to use for rx buffers */
101 #define ATE_RX_MEMORY (ATE_MAX_RX_DESCR * 128)
103 /* Actual number of descriptors we allocate */
104 #define ATE_NUM_RX_DESCR ATE_MAX_RX_DESCR
105 #define ATE_NUM_TX_DESCR ATE_MAX_TX_BUFFERS
107 #if ATE_NUM_TX_DESCR > ATE_MAX_TX_DESCR
108 #error "Can't have more TX buffers that descriptors"
110 #if ATE_NUM_RX_DESCR > ATE_MAX_RX_DESCR
111 #error "Can't have more RX buffers that descriptors"
114 /* Wrap indexes the same way the hardware does */
115 #define NEXT_RX_IDX(sc, cur) \
116 ((sc->rx_descs[cur].addr & ETH_WRAP_BIT) ? 0 : (cur + 1))
118 #define NEXT_TX_IDX(sc, cur) \
119 ((sc->tx_descs[cur].status & ETHB_TX_WRAP) ? 0 : (cur + 1))
123 struct ifnet *ifp; /* ifnet pointer */
124 struct mtx sc_mtx; /* Basically a perimeter lock */
125 device_t dev; /* Myself */
126 device_t miibus; /* My child miibus */
127 struct resource *irq_res; /* IRQ resource */
128 struct resource *mem_res; /* Memory resource */
129 struct callout tick_ch; /* Tick callout */
130 struct ifmib_iso_8802_3 mibdata; /* Stuff for network mgmt */
131 bus_dma_tag_t mtag; /* bus dma tag for mbufs */
132 bus_dma_tag_t rx_tag;
133 bus_dma_tag_t rx_desc_tag;
134 bus_dmamap_t rx_desc_map;
135 bus_dmamap_t rx_map[ATE_MAX_RX_DESCR];
136 bus_addr_t rx_desc_phys; /* PA of rx descriptors */
137 eth_rx_desc_t *rx_descs; /* VA of rx descriptors */
138 void *rx_buf[ATE_NUM_RX_DESCR]; /* RX buffer space */
139 int rxhead; /* Current RX map/desc index */
140 uint32_t rx_buf_size; /* Size of Rx buffers */
142 bus_dma_tag_t tx_desc_tag;
143 bus_dmamap_t tx_desc_map;
144 bus_dmamap_t tx_map[ATE_MAX_TX_BUFFERS];
145 bus_addr_t tx_desc_phys; /* PA of tx descriptors */
146 eth_tx_desc_t *tx_descs; /* VA of tx descriptors */
147 int txhead; /* Current TX map/desc index */
148 int txtail; /* Current TX map/desc index */
149 struct mbuf *sent_mbuf[ATE_MAX_TX_BUFFERS]; /* Sent mbufs */
150 void *intrhand; /* Interrupt handle */
154 int is_emacb; /* SAM9x hardware version */
157 static inline uint32_t
158 RD4(struct ate_softc *sc, bus_size_t off)
161 return (bus_read_4(sc->mem_res, off));
165 WR4(struct ate_softc *sc, bus_size_t off, uint32_t val)
168 bus_write_4(sc->mem_res, off, val);
172 BARRIER(struct ate_softc *sc, bus_size_t off, bus_size_t len, int flags)
175 bus_barrier(sc->mem_res, off, len, flags);
178 #define ATE_LOCK(_sc) mtx_lock(&(_sc)->sc_mtx)
179 #define ATE_UNLOCK(_sc) mtx_unlock(&(_sc)->sc_mtx)
180 #define ATE_LOCK_INIT(_sc) \
181 mtx_init(&_sc->sc_mtx, device_get_nameunit(_sc->dev), \
182 MTX_NETWORK_LOCK, MTX_DEF)
183 #define ATE_LOCK_DESTROY(_sc) mtx_destroy(&_sc->sc_mtx);
184 #define ATE_ASSERT_LOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_OWNED);
185 #define ATE_ASSERT_UNLOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_NOTOWNED);
187 static devclass_t ate_devclass;
190 * ifnet entry points.
192 static void ateinit_locked(void *);
193 static void atestart_locked(struct ifnet *);
195 static void ateinit(void *);
196 static void atestart(struct ifnet *);
197 static void atestop(struct ate_softc *);
198 static int ateioctl(struct ifnet * ifp, u_long, caddr_t);
203 static int ate_probe(device_t dev);
204 static int ate_attach(device_t dev);
205 static int ate_detach(device_t dev);
206 static void ate_intr(void *);
211 static int ate_activate(device_t dev);
212 static void ate_deactivate(struct ate_softc *sc);
213 static int ate_ifmedia_upd(struct ifnet *ifp);
214 static void ate_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr);
215 static int ate_get_mac(struct ate_softc *sc, u_char *eaddr);
216 static void ate_set_mac(struct ate_softc *sc, u_char *eaddr);
217 static void ate_rxfilter(struct ate_softc *sc);
219 static int ate_miibus_readreg(device_t dev, int phy, int reg);
221 static int ate_miibus_writereg(device_t dev, int phy, int reg, int data);
223 * The AT91 family of products has the ethernet interface called EMAC.
224 * However, it isn't self identifying. It is anticipated that the parent bus
225 * code will take care to only add ate devices where they really are. As
226 * such, we do nothing here to identify the device and just set its name.
229 ate_probe(device_t dev)
232 device_set_desc(dev, "EMAC");
237 ate_attach(device_t dev)
239 struct ate_softc *sc;
240 struct ifnet *ifp = NULL;
241 struct sysctl_ctx_list *sctx;
242 struct sysctl_oid *soid;
243 u_char eaddr[ETHER_ADDR_LEN];
247 sc = device_get_softc(dev);
252 sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
254 if (sc->mem_res == NULL) {
255 device_printf(dev, "could not allocate memory resources.\n");
260 sc->irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
262 if (sc->irq_res == NULL) {
263 device_printf(dev, "could not allocate interrupt resources.\n");
268 /* New or old version, chooses buffer size. */
269 sc->is_emacb = at91_is_sam9() || at91_is_sam9xe();
270 sc->rx_buf_size = RX_BUF_SIZE(sc);
272 err = ate_activate(dev);
276 /* Default to what boot rom did */
279 (RD4(sc, ETH_CFG) & ETH_CFG_RMII) == ETH_CFG_RMII;
282 (RD4(sc, ETHB_UIO) & ETHB_UIO_RMII) == ETHB_UIO_RMII;
284 #ifdef AT91_ATE_USE_RMII
285 /* Compile time override */
289 sctx = device_get_sysctl_ctx(dev);
290 soid = device_get_sysctl_tree(dev);
291 SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "rmii",
292 CTLFLAG_RW, &sc->use_rmii, 0, "rmii in use");
294 /* Calling atestop before ifp is set is OK. */
298 callout_init_mtx(&sc->tick_ch, &sc->sc_mtx, 0);
300 if ((err = ate_get_mac(sc, eaddr)) != 0) {
301 /* No MAC address configured. Generate the random one. */
304 "Generating random ethernet address.\n");
308 * Set OUI to convenient locally assigned address. 'b'
309 * is 0x62, which has the locally assigned bit set, and
310 * the broadcast/multicast bit clear.
315 eaddr[3] = (rnd >> 16) & 0xff;
316 eaddr[4] = (rnd >> 8) & 0xff;
317 eaddr[5] = (rnd >> 0) & 0xff;
320 sc->ifp = ifp = if_alloc(IFT_ETHER);
321 err = mii_attach(dev, &sc->miibus, ifp, ate_ifmedia_upd,
322 ate_ifmedia_sts, BMSR_DEFCAPMASK, MII_PHY_ANY, MII_OFFSET_ANY, 0);
324 device_printf(dev, "attaching PHYs failed\n");
328 * XXX: Clear the isolate bit, or we won't get up,
329 * at least on the HL201
331 ate_miibus_writereg(dev, 0, 0, 0x3000);
334 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
335 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
336 ifp->if_capabilities |= IFCAP_VLAN_MTU;
337 ifp->if_capenable |= IFCAP_VLAN_MTU; /* The hw bits already set. */
338 ifp->if_start = atestart;
339 ifp->if_ioctl = ateioctl;
340 ifp->if_init = ateinit;
341 ifp->if_baudrate = 10000000;
342 IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
343 ifp->if_snd.ifq_drv_maxlen = IFQ_MAXLEN;
344 IFQ_SET_READY(&ifp->if_snd);
345 ifp->if_linkmib = &sc->mibdata;
346 ifp->if_linkmiblen = sizeof(sc->mibdata);
347 sc->mibdata.dot3Compliance = DOT3COMPLIANCE_COLLS;
348 sc->if_flags = ifp->if_flags;
350 ether_ifattach(ifp, eaddr);
352 /* Activate the interrupt. */
353 err = bus_setup_intr(dev, sc->irq_res, INTR_TYPE_NET | INTR_MPSAFE,
354 NULL, ate_intr, sc, &sc->intrhand);
356 device_printf(dev, "could not establish interrupt handler.\n");
368 ate_detach(device_t dev)
370 struct ate_softc *sc;
373 sc = device_get_softc(dev);
374 KASSERT(sc != NULL, ("[ate: %d]: sc is NULL", __LINE__));
376 if (device_is_attached(dev)) {
378 sc->flags |= ATE_FLAG_DETACHING;
381 callout_drain(&sc->tick_ch);
384 if (sc->miibus != NULL) {
385 device_delete_child(dev, sc->miibus);
388 bus_generic_detach(sc->dev);
390 if (sc->intrhand != NULL) {
391 bus_teardown_intr(dev, sc->irq_res, sc->intrhand);
398 if (sc->mem_res != NULL) {
399 bus_release_resource(dev, SYS_RES_IOPORT,
400 rman_get_rid(sc->mem_res), sc->mem_res);
403 if (sc->irq_res != NULL) {
404 bus_release_resource(dev, SYS_RES_IRQ,
405 rman_get_rid(sc->irq_res), sc->irq_res);
408 ATE_LOCK_DESTROY(sc);
413 ate_getaddr(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
418 *(bus_addr_t *)arg = segs[0].ds_addr;
422 ate_load_rx_buf(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
424 struct ate_softc *sc;
428 sc = (struct ate_softc *)arg;
430 bus_dmamap_sync(sc->rx_desc_tag, sc->rx_desc_map, BUS_DMASYNC_PREWRITE);
431 sc->rx_descs[sc->rxhead].addr = segs[0].ds_addr;
432 sc->rx_descs[sc->rxhead].status = 0;
433 bus_dmamap_sync(sc->rx_desc_tag, sc->rx_desc_map, BUS_DMASYNC_POSTWRITE);
437 ate_mac_hash(const uint8_t *buf)
440 for (int i = 0; i < 48; i++) {
441 index ^= ((buf[i >> 3] >> (i & 7)) & 1) << (i % 6);
447 * Compute the multicast filter for this device.
450 ate_setmcast(struct ate_softc *sc)
454 u_char *af = (u_char *) mcaf;
455 struct ifmultiaddr *ifma;
460 if ((ifp->if_flags & IFF_PROMISC) != 0)
462 if ((ifp->if_flags & IFF_ALLMULTI) != 0) {
463 WR4(sc, ETH_HSL, 0xffffffff);
464 WR4(sc, ETH_HSH, 0xffffffff);
468 /* Compute the multicast hash. */
472 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
473 if (ifma->ifma_addr->sa_family != AF_LINK)
475 index = ate_mac_hash(LLADDR((struct sockaddr_dl *)
477 af[index >> 3] |= 1 << (index & 7);
479 if_maddr_runlock(ifp);
482 * Write the hash to the hash register. This card can also
483 * accept unicast packets as well as multicast packets using this
484 * register for easier bridging operations, but we don't take
485 * advantage of that. Locks here are to avoid LOR with the
486 * if_maddr_rlock, but might not be strictly necessary.
488 WR4(sc, ETH_HSL, mcaf[0]);
489 WR4(sc, ETH_HSH, mcaf[1]);
490 return (mcaf[0] || mcaf[1]);
494 ate_activate(device_t dev)
496 struct ate_softc *sc;
499 sc = device_get_softc(dev);
501 /* Allocate DMA tags and maps for TX mbufs */
502 if (bus_dma_tag_create(bus_get_dma_tag(dev), 1, 0,
503 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES,
504 1, MCLBYTES, 0, busdma_lock_mutex, &sc->sc_mtx, &sc->mtag))
506 for (i = 0; i < ATE_MAX_TX_BUFFERS; i++) {
507 if ( bus_dmamap_create(sc->mtag, 0, &sc->tx_map[i]))
512 /* DMA tag and map for the RX descriptors. */
513 if (bus_dma_tag_create(bus_get_dma_tag(dev), sizeof(eth_rx_desc_t),
514 0, BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
515 ATE_NUM_RX_DESCR * sizeof(eth_rx_desc_t), 1,
516 ATE_NUM_RX_DESCR * sizeof(eth_rx_desc_t), 0, busdma_lock_mutex,
517 &sc->sc_mtx, &sc->rx_desc_tag))
519 if (bus_dmamem_alloc(sc->rx_desc_tag, (void **)&sc->rx_descs,
520 BUS_DMA_NOWAIT | BUS_DMA_COHERENT, &sc->rx_desc_map) != 0)
522 if (bus_dmamap_load(sc->rx_desc_tag, sc->rx_desc_map,
523 sc->rx_descs, ATE_NUM_RX_DESCR * sizeof(eth_rx_desc_t),
524 ate_getaddr, &sc->rx_desc_phys, 0) != 0)
527 /* Allocate DMA tags and maps for RX. buffers */
528 if (bus_dma_tag_create(bus_get_dma_tag(dev), 1, 0,
529 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
530 sc->rx_buf_size, 1, sc->rx_buf_size, 0,
531 busdma_lock_mutex, &sc->sc_mtx, &sc->rx_tag))
535 * Allocate our RX buffers.
536 * This chip has a RX structure that's filled in.
537 * XXX On MACB (SAM9 part) we should receive directly into mbuf
538 * to avoid the copy. XXX
541 for (sc->rxhead = 0; sc->rxhead < ATE_RX_MEMORY/sc->rx_buf_size;
543 if (bus_dmamem_alloc(sc->rx_tag,
544 (void **)&sc->rx_buf[sc->rxhead], BUS_DMA_NOWAIT,
545 &sc->rx_map[sc->rxhead]) != 0)
548 if (bus_dmamap_load(sc->rx_tag, sc->rx_map[sc->rxhead],
549 sc->rx_buf[sc->rxhead], sc->rx_buf_size,
550 ate_load_rx_buf, sc, 0) != 0) {
551 printf("bus_dmamem_load\n");
554 bus_dmamap_sync(sc->rx_tag, sc->rx_map[sc->rxhead], BUS_DMASYNC_PREREAD);
558 * For the last buffer, set the wrap bit so the controller
559 * restarts from the first descriptor.
561 sc->rx_descs[--sc->rxhead].addr |= ETH_WRAP_BIT;
564 /* Flush the memory for the EMAC rx descriptor. */
565 bus_dmamap_sync(sc->rx_desc_tag, sc->rx_desc_map, BUS_DMASYNC_PREWRITE);
567 /* Write the descriptor queue address. */
568 WR4(sc, ETH_RBQP, sc->rx_desc_phys);
571 * DMA tag and map for the TX descriptors.
572 * XXX Old EMAC (not EMACB) doesn't really need DMA'able
573 * memory. We could just malloc it. gja XXX
575 if (bus_dma_tag_create(bus_get_dma_tag(dev), sizeof(eth_tx_desc_t),
576 0, BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
577 ATE_MAX_TX_BUFFERS * sizeof(eth_tx_desc_t), 1,
578 ATE_MAX_TX_BUFFERS * sizeof(eth_tx_desc_t), 0, busdma_lock_mutex,
579 &sc->sc_mtx, &sc->tx_desc_tag) != 0)
582 if (bus_dmamem_alloc(sc->tx_desc_tag, (void **)&sc->tx_descs,
583 BUS_DMA_NOWAIT | BUS_DMA_COHERENT, &sc->tx_desc_map) != 0)
586 if (bus_dmamap_load(sc->tx_desc_tag, sc->tx_desc_map,
587 sc->tx_descs, ATE_MAX_TX_BUFFERS * sizeof(eth_tx_desc_t),
588 ate_getaddr, &sc->tx_desc_phys, 0) != 0)
591 /* Initilize descriptors; mark all empty */
592 for (i = 0; i < ATE_MAX_TX_BUFFERS; i++) {
593 sc->tx_descs[i].addr =0;
594 sc->tx_descs[i].status = ETHB_TX_USED;
595 sc->sent_mbuf[i] = NULL;
598 /* Mark last entry to cause wrap when indexing through */
599 sc->tx_descs[ATE_MAX_TX_BUFFERS - 1].status =
600 ETHB_TX_WRAP | ETHB_TX_USED;
602 /* Flush the memory for the EMAC tx descriptor. */
603 bus_dmamap_sync(sc->tx_desc_tag, sc->tx_desc_map, BUS_DMASYNC_PREWRITE);
605 sc->txhead = sc->txtail = 0;
607 /* Write the descriptor queue address. */
608 WR4(sc, ETHB_TBQP, sc->tx_desc_phys);
611 /* EMACB: Enable transceiver input clock */
613 WR4(sc, ETHB_UIO, RD4(sc, ETHB_UIO) | ETHB_UIO_CLKE);
622 ate_deactivate(struct ate_softc *sc)
626 KASSERT(sc != NULL, ("[ate, %d]: sc is NULL!", __LINE__));
627 if (sc->mtag != NULL) {
628 for (i = 0; i < ATE_MAX_TX_BUFFERS; i++) {
629 if (sc->sent_mbuf[i] != NULL) {
630 bus_dmamap_sync(sc->mtag, sc->tx_map[i],
631 BUS_DMASYNC_POSTWRITE);
632 bus_dmamap_unload(sc->mtag, sc->tx_map[i]);
633 m_freem(sc->sent_mbuf[i]);
635 bus_dmamap_destroy(sc->mtag, sc->tx_map[i]);
636 sc->sent_mbuf[i] = NULL;
637 sc->tx_map[i] = NULL;
639 bus_dma_tag_destroy(sc->mtag);
641 if (sc->rx_desc_tag != NULL) {
642 if (sc->rx_descs != NULL) {
643 if (sc->rx_desc_phys != 0) {
644 bus_dmamap_sync(sc->rx_desc_tag,
645 sc->rx_desc_map, BUS_DMASYNC_POSTREAD);
646 bus_dmamap_unload(sc->rx_desc_tag,
648 sc->rx_desc_phys = 0;
652 if (sc->rx_tag != NULL) {
653 for (i = 0; sc->rx_buf[i] != NULL; i++) {
654 if (sc->rx_descs[i].addr != 0) {
655 bus_dmamap_sync(sc->rx_tag,
657 BUS_DMASYNC_POSTREAD);
658 bus_dmamap_unload(sc->rx_tag,
660 sc->rx_descs[i].addr = 0;
662 bus_dmamem_free(sc->rx_tag, sc->rx_buf[i],
664 sc->rx_buf[i] = NULL;
665 sc->rx_map[i] = NULL;
667 bus_dma_tag_destroy(sc->rx_tag);
669 if (sc->rx_desc_tag != NULL) {
670 if (sc->rx_descs != NULL)
671 bus_dmamem_free(sc->rx_desc_tag, sc->rx_descs,
673 bus_dma_tag_destroy(sc->rx_desc_tag);
675 sc->rx_desc_tag = NULL;
679 WR4(sc, ETHB_UIO, RD4(sc, ETHB_UIO) & ~ETHB_UIO_CLKE);
683 * Change media according to request.
686 ate_ifmedia_upd(struct ifnet *ifp)
688 struct ate_softc *sc = ifp->if_softc;
689 struct mii_data *mii;
691 mii = device_get_softc(sc->miibus);
699 * Notify the world which media we're using.
702 ate_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
704 struct ate_softc *sc = ifp->if_softc;
705 struct mii_data *mii;
707 mii = device_get_softc(sc->miibus);
710 ifmr->ifm_active = mii->mii_media_active;
711 ifmr->ifm_status = mii->mii_media_status;
716 ate_stat_update(struct ate_softc *sc, int active)
721 * The speed and full/half-duplex state needs to be reflected
722 * in the ETH_CFG register.
724 reg = RD4(sc, ETH_CFG);
725 reg &= ~(ETH_CFG_SPD | ETH_CFG_FD);
726 if (IFM_SUBTYPE(active) != IFM_10_T)
728 if (active & IFM_FDX)
730 WR4(sc, ETH_CFG, reg);
736 struct ate_softc *sc = xsc;
737 struct ifnet *ifp = sc->ifp;
738 struct mii_data *mii;
743 * The KB920x boot loader tests ETH_SR & ETH_SR_LINK and will ask
744 * the MII if there's a link if this bit is clear. Not sure if we
745 * should do the same thing here or not.
747 ATE_ASSERT_LOCKED(sc);
748 if (sc->miibus != NULL) {
749 mii = device_get_softc(sc->miibus);
750 active = mii->mii_media_active;
752 if (mii->mii_media_status & IFM_ACTIVE &&
753 active != mii->mii_media_active)
754 ate_stat_update(sc, mii->mii_media_active);
758 * Update the stats as best we can. When we're done, clear
759 * the status counters and start over. We're supposed to read these
760 * registers often enough that they won't overflow. Hopefully
761 * once a second is often enough. Some don't map well to
762 * the dot3Stats mib, so for those we just count them as general
763 * errors. Stats for iframes, ibutes, oframes and obytes are
764 * collected elsewhere. These registers zero on a read to prevent
765 * races. For all the collision stats, also update the collision
766 * stats for the interface.
768 sc->mibdata.dot3StatsAlignmentErrors += RD4(sc, ETH_ALE);
769 sc->mibdata.dot3StatsFCSErrors += RD4(sc, ETH_SEQE);
770 c = RD4(sc, ETH_SCOL);
771 ifp->if_collisions += c;
772 sc->mibdata.dot3StatsSingleCollisionFrames += c;
773 c = RD4(sc, ETH_MCOL);
774 sc->mibdata.dot3StatsMultipleCollisionFrames += c;
775 ifp->if_collisions += c;
776 sc->mibdata.dot3StatsSQETestErrors += RD4(sc, ETH_SQEE);
777 sc->mibdata.dot3StatsDeferredTransmissions += RD4(sc, ETH_DTE);
778 c = RD4(sc, ETH_LCOL);
779 sc->mibdata.dot3StatsLateCollisions += c;
780 ifp->if_collisions += c;
781 c = RD4(sc, ETH_ECOL);
782 sc->mibdata.dot3StatsExcessiveCollisions += c;
783 ifp->if_collisions += c;
784 sc->mibdata.dot3StatsCarrierSenseErrors += RD4(sc, ETH_CSE);
785 sc->mibdata.dot3StatsFrameTooLongs += RD4(sc, ETH_ELR);
786 sc->mibdata.dot3StatsInternalMacReceiveErrors += RD4(sc, ETH_DRFC);
789 * Not sure where to lump these, so count them against the errors
792 sc->ifp->if_oerrors += RD4(sc, ETH_TUE);
793 sc->ifp->if_ierrors += RD4(sc, ETH_CDE) + RD4(sc, ETH_RJB) +
796 /* Schedule another timeout one second from now. */
797 callout_reset(&sc->tick_ch, hz, ate_tick, sc);
801 ate_set_mac(struct ate_softc *sc, u_char *eaddr)
804 WR4(sc, ETH_SA1L, (eaddr[3] << 24) | (eaddr[2] << 16) |
805 (eaddr[1] << 8) | eaddr[0]);
806 WR4(sc, ETH_SA1H, (eaddr[5] << 8) | (eaddr[4]));
810 ate_get_mac(struct ate_softc *sc, u_char *eaddr)
812 bus_size_t sa_low_reg[] = { ETH_SA1L, ETH_SA2L, ETH_SA3L, ETH_SA4L };
813 bus_size_t sa_high_reg[] = { ETH_SA1H, ETH_SA2H, ETH_SA3H, ETH_SA4H };
818 * The boot loader may setup the MAC with an address(es), grab the
819 * first MAC address from the SA[1-4][HL] registers.
821 for (i = 0; i < 4; i++) {
822 low = RD4(sc, sa_low_reg[i]);
823 high = RD4(sc, sa_high_reg[i]);
824 if ((low | (high & 0xffff)) != 0) {
825 eaddr[0] = low & 0xff;
826 eaddr[1] = (low >> 8) & 0xff;
827 eaddr[2] = (low >> 16) & 0xff;
828 eaddr[3] = (low >> 24) & 0xff;
829 eaddr[4] = high & 0xff;
830 eaddr[5] = (high >> 8) & 0xff;
840 struct ate_softc *sc = xsc;
841 struct ifnet *ifp = sc->ifp;
843 eth_rx_desc_t *rxdhead;
844 uint32_t status, reg, idx;
845 int remain, count, done;
847 status = RD4(sc, ETH_ISR);
851 if (status & ETH_ISR_RCOM) {
853 bus_dmamap_sync(sc->rx_desc_tag, sc->rx_desc_map,
854 BUS_DMASYNC_POSTREAD);
856 rxdhead = &sc->rx_descs[sc->rxhead];
857 while (rxdhead->addr & ETH_CPU_OWNER) {
860 * Simulate SAM9 FIRST/LAST bits for RM9200.
861 * RM9200 EMAC has only on Rx buffer per packet.
862 * But sometime we are handed a zero lenght packet.
864 if ((rxdhead->status & ETH_LEN_MASK) == 0)
865 rxdhead->status = 0; /* Mark error */
867 rxdhead->status |= ETH_BUF_FIRST | ETH_BUF_LAST;
870 if ((rxdhead->status & ETH_BUF_FIRST) == 0) {
871 /* Something went wrong during RX so
872 release back to EMAC all buffers of invalid packets.
875 rxdhead->addr &= ~ETH_CPU_OWNER;
876 sc->rxhead = NEXT_RX_IDX(sc, sc->rxhead);
877 rxdhead = &sc->rx_descs[sc->rxhead];
881 /* Find end of packet or start of next */
883 if ((sc->rx_descs[idx].status & ETH_BUF_LAST) == 0) {
884 idx = NEXT_RX_IDX(sc, idx);
886 while ((sc->rx_descs[idx].addr & ETH_CPU_OWNER) &&
887 ((sc->rx_descs[idx].status &
888 (ETH_BUF_FIRST|ETH_BUF_LAST))== 0))
889 idx = NEXT_RX_IDX(sc, idx);
892 /* Packet NOT yet completely in memory; we are done */
893 if ((sc->rx_descs[idx].addr & ETH_CPU_OWNER) == 0 ||
894 ((sc->rx_descs[idx].status & (ETH_BUF_FIRST|ETH_BUF_LAST))== 0))
897 /* Packets with no end descriptor are invalid. */
898 if ((sc->rx_descs[idx].status & ETH_BUF_LAST) == 0) {
899 rxdhead->status &= ~ETH_BUF_FIRST;
903 /* FCS is not coppied into mbuf. */
904 remain = (sc->rx_descs[idx].status & ETH_LEN_MASK) - 4;
906 /* Get an appropriately sized mbuf */
907 if (remain + ETHER_ALIGN >= MINCLSIZE)
908 mb = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
910 MGETHDR(mb, M_DONTWAIT, MT_DATA);
913 sc->ifp->if_iqdrops++;
917 mb->m_data += ETHER_ALIGN;
918 mb->m_pkthdr.rcvif = ifp;
920 WR4(sc, ETH_RSR, RD4(sc, ETH_RSR)); /* Reset status */
922 /* Now we process the buffers that make up the packet */
925 /* Last buffer may just be 1-4 bytes of FCS so remain
926 * may be zero for last decriptor. */
928 /* Make sure we get the current bytes */
929 bus_dmamap_sync(sc->rx_tag, sc->rx_map[sc->rxhead],
930 BUS_DMASYNC_POSTREAD);
932 count = MIN(remain, sc->rx_buf_size);
934 /* XXX Performance robbing copy. Could
935 * recieve directly to mbufs if not an
937 m_append(mb, count, sc->rx_buf[sc->rxhead]);
941 done = (rxdhead->status & ETH_BUF_LAST) != 0;
943 /* Return the descriptor to the EMAC */
945 rxdhead->addr &= ~ETH_CPU_OWNER;
946 bus_dmamap_sync(sc->rx_desc_tag, sc->rx_desc_map,
947 BUS_DMASYNC_PREWRITE);
949 /* Move on to next descriptor with wrap */
950 sc->rxhead = NEXT_RX_IDX(sc, sc->rxhead);
951 rxdhead = &sc->rx_descs[sc->rxhead];
957 (*ifp->if_input)(ifp, mb);
963 if (status & ETH_ISR_TCOM) {
964 bus_dmamap_sync(sc->tx_desc_tag, sc->tx_desc_map,
965 BUS_DMASYNC_POSTREAD);
968 /* XXX TSR register should be cleared */
970 /* Simulate Transmit descriptor table */
972 /* First packet done */
973 if (sc->txtail < sc->txhead)
974 sc->tx_descs[sc->txtail].status |= ETHB_TX_USED;
976 /* Second Packet done */
977 if (sc->txtail + 1 < sc->txhead &&
978 RD4(sc, ETH_TSR) & ETH_TSR_IDLE)
979 sc->tx_descs[sc->txtail + 1].status |= ETHB_TX_USED;
982 while (sc->txtail != sc->txhead &&
983 sc->tx_descs[sc->txtail].status & ETHB_TX_USED ) {
985 bus_dmamap_sync(sc->mtag, sc->tx_map[sc->txtail],
986 BUS_DMASYNC_POSTWRITE);
987 bus_dmamap_unload(sc->mtag, sc->tx_map[sc->txtail]);
988 m_freem(sc->sent_mbuf[sc->txtail]);
989 sc->tx_descs[sc->txtail].addr = 0;
990 sc->sent_mbuf[sc->txtail] = NULL;
993 sc->txtail = NEXT_TX_IDX(sc, sc->txtail);
996 /* Flush descriptors to EMAC */
997 bus_dmamap_sync(sc->tx_desc_tag, sc->tx_desc_map, BUS_DMASYNC_PREWRITE);
1000 * We're no longer busy, so clear the busy flag and call the
1001 * start routine to xmit more packets.
1003 sc->ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1004 atestart_locked(sc->ifp);
1008 if (status & ETH_ISR_RBNA) {
1009 /* Workaround RM9200 Errata #11 */
1011 device_printf(sc->dev, "RBNA workaround\n");
1012 reg = RD4(sc, ETH_CTL);
1013 WR4(sc, ETH_CTL, reg & ~ETH_CTL_RE);
1014 BARRIER(sc, ETH_CTL, 4, BUS_SPACE_BARRIER_WRITE);
1015 WR4(sc, ETH_CTL, reg | ETH_CTL_RE);
1020 * Reset and initialize the chip.
1023 ateinit_locked(void *xsc)
1025 struct ate_softc *sc = xsc;
1026 struct ifnet *ifp = sc->ifp;
1027 struct mii_data *mii;
1028 uint8_t eaddr[ETHER_ADDR_LEN];
1031 ATE_ASSERT_LOCKED(sc);
1035 * we need to turn on the EMAC clock in the pmc. With the
1036 * default boot loader, this is already turned on. However, we
1037 * need to think about how best to turn it on/off as the interface
1038 * is brought up/down, as well as dealing with the mii bus...
1040 * We also need to multiplex the pins correctly (in board_xxx.c).
1044 * There are two different ways that the mii bus is connected
1045 * to this chip mii or rmii.
1047 if (!sc->is_emacb) {
1049 reg = RD4(sc, ETH_CFG);
1051 reg |= ETH_CFG_RMII;
1053 reg &= ~ETH_CFG_RMII;
1054 WR4(sc, ETH_CFG, reg);
1057 reg = ETHB_UIO_CLKE;
1058 reg |= (sc->use_rmii) ? ETHB_UIO_RMII : 0;
1059 WR4(sc, ETHB_UIO, reg);
1065 * Set the chip MAC address.
1067 bcopy(IF_LLADDR(ifp), eaddr, ETHER_ADDR_LEN);
1068 ate_set_mac(sc, eaddr);
1070 /* Make sure we know state of TX queue */
1071 sc->txhead = sc->txtail = 0;
1073 /* Write the descriptor queue address. */
1074 WR4(sc, ETHB_TBQP, sc->tx_desc_phys);
1078 * Turn on MACs and interrupt processing.
1080 WR4(sc, ETH_CTL, RD4(sc, ETH_CTL) | ETH_CTL_TE | ETH_CTL_RE);
1081 WR4(sc, ETH_IER, ETH_ISR_RCOM | ETH_ISR_TCOM | ETH_ISR_RBNA);
1083 /* Enable big packets. */
1084 WR4(sc, ETH_CFG, RD4(sc, ETH_CFG) | ETH_CFG_BIG);
1087 * Set 'running' flag, and clear output active flag
1088 * and attempt to start the output.
1090 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1091 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1093 mii = device_get_softc(sc->miibus);
1095 ate_stat_update(sc, mii->mii_media_active);
1096 atestart_locked(ifp);
1098 callout_reset(&sc->tick_ch, hz, ate_tick, sc);
1102 * Dequeue packets and transmit.
1105 atestart_locked(struct ifnet *ifp)
1107 struct ate_softc *sc = ifp->if_softc;
1108 struct mbuf *m, *mdefrag;
1109 bus_dma_segment_t segs[1];
1112 ATE_ASSERT_LOCKED(sc);
1113 if (ifp->if_drv_flags & IFF_DRV_OACTIVE)
1116 while (sc->tx_descs[sc->txhead].status & ETHB_TX_USED) {
1118 * Check to see if there's room to put another packet into the
1119 * xmit queue. The old EMAC version has a ping-pong buffer for
1120 * xmit packets. We use OACTIVE to indicate "we can stuff more
1121 * into our buffers (clear) or not (set)."
1123 if (!sc->is_emacb) {
1124 /* RM9200 has only two hardware entries */
1125 if (!sc->is_emacb && (RD4(sc, ETH_TSR) & ETH_TSR_BNQ) == 0) {
1126 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1131 IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
1135 e = bus_dmamap_load_mbuf_sg(sc->mtag, sc->tx_map[sc->txhead], m,
1138 mdefrag = m_defrag(m, M_DONTWAIT);
1139 if (mdefrag == NULL) {
1140 IFQ_DRV_PREPEND(&ifp->if_snd, m);
1144 e = bus_dmamap_load_mbuf_sg(sc->mtag,
1145 sc->tx_map[sc->txhead], m, segs, &nseg, 0);
1151 sc->sent_mbuf[sc->txhead] = m;
1153 bus_dmamap_sync(sc->mtag, sc->tx_map[sc->txhead],
1154 BUS_DMASYNC_PREWRITE);
1156 /* Tell the hardware to xmit the packet. */
1157 if (!sc->is_emacb) {
1158 WR4(sc, ETH_TAR, segs[0].ds_addr);
1159 BARRIER(sc, ETH_TAR, 4, BUS_SPACE_BARRIER_WRITE);
1160 WR4(sc, ETH_TCR, segs[0].ds_len);
1162 bus_dmamap_sync(sc->tx_desc_tag, sc->tx_desc_map,
1163 BUS_DMASYNC_POSTWRITE);
1164 sc->tx_descs[sc->txhead].addr = segs[0].ds_addr;
1165 sc->tx_descs[sc->txhead].status = segs[0].ds_len |
1166 (sc->tx_descs[sc->txhead].status & ETHB_TX_WRAP) |
1168 bus_dmamap_sync(sc->tx_desc_tag, sc->tx_desc_map,
1169 BUS_DMASYNC_PREWRITE);
1170 WR4(sc, ETH_CTL, RD4(sc, ETH_CTL) | ETHB_CTL_TGO);
1172 sc->txhead = NEXT_TX_IDX(sc, sc->txhead);
1174 /* Tap off here if there is a bpf listener. */
1178 if ((sc->tx_descs[sc->txhead].status & ETHB_TX_USED) == 0)
1179 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1185 struct ate_softc *sc = xsc;
1193 atestart(struct ifnet *ifp)
1195 struct ate_softc *sc = ifp->if_softc;
1198 atestart_locked(ifp);
1203 * Turn off interrupts, and stop the NIC. Can be called with sc->ifp NULL,
1207 atestop(struct ate_softc *sc)
1212 ATE_ASSERT_LOCKED(sc);
1215 //ifp->if_timer = 0;
1216 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
1219 callout_stop(&sc->tick_ch);
1222 * Enable some parts of the MAC that are needed always (like the
1223 * MII bus. This turns off the RE and TE bits, which will remain
1224 * off until ateinit() is called to turn them on. With RE and TE
1225 * turned off, there's no DMA to worry about after this write.
1227 WR4(sc, ETH_CTL, ETH_CTL_MPE);
1230 * Turn off all the configured options and revert to defaults.
1233 /* Make sure thate the MDIO clk is less than
1234 * 2.5 Mhz. Can no longer default to /32 since
1235 * SAM9 family may have MCK > 80 Mhz */
1236 if (at91_master_clock <= 2000000)
1237 WR4(sc, ETH_CFG, ETH_CFG_CLK_8);
1238 else if (at91_master_clock <= 4000000)
1239 WR4(sc, ETH_CFG, ETH_CFG_CLK_16);
1240 else if (at91_master_clock <= 800000)
1241 WR4(sc, ETH_CFG, ETH_CFG_CLK_32);
1243 WR4(sc, ETH_CFG, ETH_CFG_CLK_64);
1246 * Turn off all the interrupts, and ack any pending ones by reading
1249 WR4(sc, ETH_IDR, 0xffffffff);
1253 * Clear out the Transmit and Receiver Status registers of any
1254 * errors they may be reporting
1256 WR4(sc, ETH_TSR, 0xffffffff);
1257 WR4(sc, ETH_RSR, 0xffffffff);
1259 /* Release TX resources. */
1260 for (i = 0; i < ATE_MAX_TX_BUFFERS; i++) {
1261 if (sc->sent_mbuf[i] != NULL) {
1262 bus_dmamap_sync(sc->mtag, sc->tx_map[i],
1263 BUS_DMASYNC_POSTWRITE);
1264 bus_dmamap_unload(sc->mtag, sc->tx_map[i]);
1265 m_freem(sc->sent_mbuf[i]);
1266 sc->sent_mbuf[i] = NULL;
1270 /* Turn off transeiver input clock */
1272 WR4(sc, ETHB_UIO, RD4(sc, ETHB_UIO) & ~ETHB_UIO_CLKE);
1275 * XXX we should power down the EMAC if it isn't in use, after
1276 * putting it into loopback mode. This saves about 400uA according
1282 ate_rxfilter(struct ate_softc *sc)
1288 KASSERT(sc != NULL, ("[ate, %d]: sc is NULL!", __LINE__));
1289 ATE_ASSERT_LOCKED(sc);
1292 /* Wipe out old filter settings. */
1293 reg = RD4(sc, ETH_CFG);
1294 reg &= ~(ETH_CFG_CAF | ETH_CFG_MTI | ETH_CFG_UNI);
1296 sc->flags &= ~ATE_FLAG_MULTICAST;
1298 /* Set new parameters. */
1299 if ((ifp->if_flags & IFF_BROADCAST) != 0)
1300 reg &= ~ETH_CFG_NBC;
1301 if ((ifp->if_flags & IFF_PROMISC) != 0) {
1304 enabled = ate_setmcast(sc);
1307 sc->flags |= ATE_FLAG_MULTICAST;
1310 WR4(sc, ETH_CFG, reg);
1314 ateioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1316 struct ate_softc *sc = ifp->if_softc;
1317 struct mii_data *mii;
1318 struct ifreq *ifr = (struct ifreq *)data;
1319 int drv_flags, flags;
1320 int mask, error, enabled;
1323 flags = ifp->if_flags;
1324 drv_flags = ifp->if_drv_flags;
1328 if ((flags & IFF_UP) != 0) {
1329 if ((drv_flags & IFF_DRV_RUNNING) != 0) {
1330 if (((flags ^ sc->if_flags)
1331 & (IFF_PROMISC | IFF_ALLMULTI)) != 0)
1334 if ((sc->flags & ATE_FLAG_DETACHING) == 0)
1337 } else if ((drv_flags & IFF_DRV_RUNNING) != 0) {
1338 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1341 sc->if_flags = flags;
1347 if ((drv_flags & IFF_DRV_RUNNING) != 0) {
1349 enabled = ate_setmcast(sc);
1350 if (enabled != (sc->flags & ATE_FLAG_MULTICAST))
1358 mii = device_get_softc(sc->miibus);
1359 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, cmd);
1362 mask = ifp->if_capenable ^ ifr->ifr_reqcap;
1363 if (mask & IFCAP_VLAN_MTU) {
1365 if (ifr->ifr_reqcap & IFCAP_VLAN_MTU) {
1366 WR4(sc, ETH_CFG, RD4(sc, ETH_CFG) | ETH_CFG_BIG);
1367 ifp->if_capenable |= IFCAP_VLAN_MTU;
1369 WR4(sc, ETH_CFG, RD4(sc, ETH_CFG) & ~ETH_CFG_BIG);
1370 ifp->if_capenable &= ~IFCAP_VLAN_MTU;
1375 error = ether_ioctl(ifp, cmd, data);
1382 ate_child_detached(device_t dev, device_t child)
1384 struct ate_softc *sc;
1386 sc = device_get_softc(dev);
1387 if (child == sc->miibus)
1392 * MII bus support routines.
1395 ate_miibus_readreg(device_t dev, int phy, int reg)
1397 struct ate_softc *sc;
1401 * XXX if we implement agressive power savings, then we need
1402 * XXX to make sure that the clock to the emac is on here
1405 sc = device_get_softc(dev);
1406 DELAY(1); /* Hangs w/o this delay really 30.5us atm */
1407 WR4(sc, ETH_MAN, ETH_MAN_REG_RD(phy, reg));
1408 while ((RD4(sc, ETH_SR) & ETH_SR_IDLE) == 0)
1410 val = RD4(sc, ETH_MAN) & ETH_MAN_VALUE_MASK;
1416 ate_miibus_writereg(device_t dev, int phy, int reg, int data)
1418 struct ate_softc *sc;
1421 * XXX if we implement agressive power savings, then we need
1422 * XXX to make sure that the clock to the emac is on here
1425 sc = device_get_softc(dev);
1426 WR4(sc, ETH_MAN, ETH_MAN_REG_WR(phy, reg, data));
1427 while ((RD4(sc, ETH_SR) & ETH_SR_IDLE) == 0)
1432 static device_method_t ate_methods[] = {
1433 /* Device interface */
1434 DEVMETHOD(device_probe, ate_probe),
1435 DEVMETHOD(device_attach, ate_attach),
1436 DEVMETHOD(device_detach, ate_detach),
1439 DEVMETHOD(bus_child_detached, ate_child_detached),
1442 DEVMETHOD(miibus_readreg, ate_miibus_readreg),
1443 DEVMETHOD(miibus_writereg, ate_miibus_writereg),
1448 static driver_t ate_driver = {
1451 sizeof(struct ate_softc),
1454 DRIVER_MODULE(ate, atmelarm, ate_driver, ate_devclass, NULL, NULL);
1455 DRIVER_MODULE(miibus, ate, miibus_driver, miibus_devclass, NULL, NULL);
1456 MODULE_DEPEND(ate, miibus, 1, 1, 1);
1457 MODULE_DEPEND(ate, ether, 1, 1, 1);