2 * Copyright (c) 2014 Ruslan Bukin <br@bsdpad.com>
5 * This software was developed by SRI International and the University of
6 * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237)
7 * ("CTSRD"), as part of the DARPA CRASH research programme.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * Ethernet media access controller (EMAC)
33 * Chapter 17, Altera Cyclone V Device Handbook (CV-5V2 2014.07.22)
35 * EMAC is an instance of the Synopsys DesignWare 3504-0
36 * Universal 10/100/1000 Ethernet MAC (DWC_gmac).
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
42 #include <sys/param.h>
43 #include <sys/systm.h>
45 #include <sys/kernel.h>
46 #include <sys/module.h>
47 #include <sys/malloc.h>
49 #include <sys/endian.h>
52 #include <sys/mutex.h>
53 #include <sys/socket.h>
54 #include <sys/sockio.h>
55 #include <sys/sysctl.h>
57 #include <dev/fdt/fdt_common.h>
58 #include <dev/ofw/openfirm.h>
59 #include <dev/ofw/ofw_bus.h>
60 #include <dev/ofw/ofw_bus_subr.h>
64 #include <net/ethernet.h>
65 #include <net/if_dl.h>
66 #include <net/if_media.h>
67 #include <net/if_types.h>
68 #include <net/if_var.h>
69 #include <net/if_vlan_var.h>
71 #include <machine/bus.h>
72 #include <machine/fdt.h>
74 #include <dev/mii/mii.h>
75 #include <dev/mii/miivar.h>
76 #include "miibus_if.h"
78 #define READ4(_sc, _reg) \
79 bus_read_4((_sc)->res[0], _reg)
80 #define WRITE4(_sc, _reg, _val) \
81 bus_write_4((_sc)->res[0], _reg, _val)
83 #define MAC_RESET_TIMEOUT 100
84 #define WATCHDOG_TIMEOUT_SECS 5
85 #define STATS_HARVEST_INTERVAL 2
88 #include <dev/dwc/if_dwc.h>
90 #define DWC_LOCK(sc) mtx_lock(&(sc)->mtx)
91 #define DWC_UNLOCK(sc) mtx_unlock(&(sc)->mtx)
92 #define DWC_ASSERT_LOCKED(sc) mtx_assert(&(sc)->mtx, MA_OWNED);
93 #define DWC_ASSERT_UNLOCKED(sc) mtx_assert(&(sc)->mtx, MA_NOTOWNED);
95 #define DDESC_TDES0_OWN (1 << 31)
96 #define DDESC_TDES0_TXINT (1 << 30)
97 #define DDESC_TDES0_TXLAST (1 << 29)
98 #define DDESC_TDES0_TXFIRST (1 << 28)
99 #define DDESC_TDES0_TXCRCDIS (1 << 27)
100 #define DDESC_TDES0_TXRINGEND (1 << 21)
101 #define DDESC_TDES0_TXCHAIN (1 << 20)
103 #define DDESC_RDES0_OWN (1 << 31)
104 #define DDESC_RDES0_FL_MASK 0x3fff
105 #define DDESC_RDES0_FL_SHIFT 16 /* Frame Length */
106 #define DDESC_RDES1_CHAINED (1 << 14)
114 * A hardware buffer descriptor. Rx and Tx buffers have the same descriptor
115 * layout, but the bits in the flags field have different meanings.
121 uint32_t addr; /* pointer to buffer data */
122 uint32_t addr_next; /* link to next descriptor */
126 * Driver data and defines.
128 #define RX_DESC_COUNT 1024
129 #define RX_DESC_SIZE (sizeof(struct dwc_hwdesc) * RX_DESC_COUNT)
130 #define TX_DESC_COUNT 1024
131 #define TX_DESC_SIZE (sizeof(struct dwc_hwdesc) * TX_DESC_COUNT)
134 * The hardware imposes alignment restrictions on various objects involved in
135 * DMA transfers. These values are expressed in bytes (not bits).
137 #define DWC_DESC_RING_ALIGN 2048
140 struct resource *res[2];
142 bus_space_handle_t bsh;
146 struct mii_data * mii_softc;
151 struct callout dwc_callout;
152 uint8_t phy_conn_type;
154 boolean_t link_is_up;
155 boolean_t is_attached;
156 boolean_t is_detaching;
157 int tx_watchdog_count;
158 int stats_harvest_count;
161 bus_dma_tag_t rxdesc_tag;
162 bus_dmamap_t rxdesc_map;
163 struct dwc_hwdesc *rxdesc_ring;
164 bus_addr_t rxdesc_ring_paddr;
165 bus_dma_tag_t rxbuf_tag;
166 struct dwc_bufmap rxbuf_map[RX_DESC_COUNT];
170 bus_dma_tag_t txdesc_tag;
171 bus_dmamap_t txdesc_map;
172 struct dwc_hwdesc *txdesc_ring;
173 bus_addr_t txdesc_ring_paddr;
174 bus_dma_tag_t txbuf_tag;
175 struct dwc_bufmap txbuf_map[RX_DESC_COUNT];
176 uint32_t tx_idx_head;
177 uint32_t tx_idx_tail;
181 static struct resource_spec dwc_spec[] = {
182 { SYS_RES_MEMORY, 0, RF_ACTIVE },
183 { SYS_RES_IRQ, 0, RF_ACTIVE },
187 static void dwc_txfinish_locked(struct dwc_softc *sc);
188 static void dwc_rxfinish_locked(struct dwc_softc *sc);
189 static void dwc_stop_locked(struct dwc_softc *sc);
190 static void dwc_setup_rxfilter(struct dwc_softc *sc);
192 static inline uint32_t
193 next_rxidx(struct dwc_softc *sc, uint32_t curidx)
196 return ((curidx + 1) % RX_DESC_COUNT);
199 static inline uint32_t
200 next_txidx(struct dwc_softc *sc, uint32_t curidx)
203 return ((curidx + 1) % TX_DESC_COUNT);
207 dwc_get1paddr(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
212 *(bus_addr_t *)arg = segs[0].ds_addr;
215 inline static uint32_t
216 dwc_setup_txdesc(struct dwc_softc *sc, int idx, bus_addr_t paddr,
222 nidx = next_txidx(sc, idx);
224 /* Addr/len 0 means we're clearing the descriptor after xmit done. */
225 if (paddr == 0 || len == 0) {
229 flags = DDESC_TDES0_TXCHAIN | DDESC_TDES0_TXFIRST
230 | DDESC_TDES0_TXLAST | DDESC_TDES0_TXINT;
234 sc->txdesc_ring[idx].addr = (uint32_t)(paddr);
235 sc->txdesc_ring[idx].tdes0 = flags;
236 sc->txdesc_ring[idx].tdes1 = len;
240 sc->txdesc_ring[idx].tdes0 |= DDESC_TDES0_OWN;
248 dwc_setup_txbuf(struct dwc_softc *sc, int idx, struct mbuf **mp)
250 struct bus_dma_segment seg;
254 if ((m = m_defrag(*mp, M_NOWAIT)) == NULL)
258 error = bus_dmamap_load_mbuf_sg(sc->txbuf_tag, sc->txbuf_map[idx].map,
264 KASSERT(nsegs == 1, ("%s: %d segments returned!", __func__, nsegs));
266 bus_dmamap_sync(sc->txbuf_tag, sc->txbuf_map[idx].map,
267 BUS_DMASYNC_PREWRITE);
269 sc->txbuf_map[idx].mbuf = m;
271 dwc_setup_txdesc(sc, idx, seg.ds_addr, seg.ds_len);
277 dwc_txstart_locked(struct dwc_softc *sc)
283 DWC_ASSERT_LOCKED(sc);
290 if (ifp->if_drv_flags & IFF_DRV_OACTIVE) {
297 if (sc->txcount == (TX_DESC_COUNT-1)) {
298 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
302 IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
305 if (dwc_setup_txbuf(sc, sc->tx_idx_head, &m) != 0) {
306 IFQ_DRV_PREPEND(&ifp->if_snd, m);
310 sc->tx_idx_head = next_txidx(sc, sc->tx_idx_head);
315 WRITE4(sc, TRANSMIT_POLL_DEMAND, 0x1);
316 sc->tx_watchdog_count = WATCHDOG_TIMEOUT_SECS;
321 dwc_txstart(struct ifnet *ifp)
323 struct dwc_softc *sc = ifp->if_softc;
326 dwc_txstart_locked(sc);
331 dwc_stop_locked(struct dwc_softc *sc)
336 DWC_ASSERT_LOCKED(sc);
339 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
340 sc->tx_watchdog_count = 0;
341 sc->stats_harvest_count = 0;
343 callout_stop(&sc->dwc_callout);
346 reg = READ4(sc, OPERATION_MODE);
348 WRITE4(sc, OPERATION_MODE, reg);
351 reg = READ4(sc, OPERATION_MODE);
353 WRITE4(sc, OPERATION_MODE, reg);
355 /* Stop transmitters */
356 reg = READ4(sc, MAC_CONFIGURATION);
357 reg &= ~(CONF_TE | CONF_RE);
358 WRITE4(sc, MAC_CONFIGURATION, reg);
361 reg = READ4(sc, OPERATION_MODE);
363 WRITE4(sc, OPERATION_MODE, reg);
366 static void dwc_clear_stats(struct dwc_softc *sc)
370 reg = READ4(sc, MMC_CONTROL);
371 reg |= (MMC_CONTROL_CNTRST);
372 WRITE4(sc, MMC_CONTROL, reg);
376 dwc_harvest_stats(struct dwc_softc *sc)
380 /* We don't need to harvest too often. */
381 if (++sc->stats_harvest_count < STATS_HARVEST_INTERVAL)
384 sc->stats_harvest_count = 0;
387 if_inc_counter(ifp, IFCOUNTER_IPACKETS, READ4(sc, RXFRAMECOUNT_GB));
388 if_inc_counter(ifp, IFCOUNTER_IMCASTS, READ4(sc, RXMULTICASTFRAMES_G));
389 if_inc_counter(ifp, IFCOUNTER_IERRORS,
390 READ4(sc, RXOVERSIZE_G) + READ4(sc, RXUNDERSIZE_G) +
391 READ4(sc, RXCRCERROR) + READ4(sc, RXALIGNMENTERROR) +
392 READ4(sc, RXRUNTERROR) + READ4(sc, RXJABBERERROR) +
393 READ4(sc, RXLENGTHERROR));
395 if_inc_counter(ifp, IFCOUNTER_OPACKETS, READ4(sc, TXFRAMECOUNT_G));
396 if_inc_counter(ifp, IFCOUNTER_OMCASTS, READ4(sc, TXMULTICASTFRAMES_G));
397 if_inc_counter(ifp, IFCOUNTER_OERRORS,
398 READ4(sc, TXOVERSIZE_G) + READ4(sc, TXEXCESSDEF) +
399 READ4(sc, TXCARRIERERR) + READ4(sc, TXUNDERFLOWERROR));
401 if_inc_counter(ifp, IFCOUNTER_COLLISIONS,
402 READ4(sc, TXEXESSCOL) + READ4(sc, TXLATECOL));
410 struct dwc_softc *sc;
416 DWC_ASSERT_LOCKED(sc);
420 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
424 * Typical tx watchdog. If this fires it indicates that we enqueued
425 * packets for output and never got a txdone interrupt for them. Maybe
426 * it's a missed interrupt somehow, just pretend we got one.
428 if (sc->tx_watchdog_count > 0) {
429 if (--sc->tx_watchdog_count == 0) {
430 dwc_txfinish_locked(sc);
434 /* Gather stats from hardware counters. */
435 dwc_harvest_stats(sc);
437 /* Check the media status. */
438 link_was_up = sc->link_is_up;
439 mii_tick(sc->mii_softc);
440 if (sc->link_is_up && !link_was_up)
441 dwc_txstart_locked(sc);
443 /* Schedule another check one second from now. */
444 callout_reset(&sc->dwc_callout, hz, dwc_tick, sc);
448 dwc_init_locked(struct dwc_softc *sc)
450 struct ifnet *ifp = sc->ifp;
453 DWC_ASSERT_LOCKED(sc);
455 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
458 ifp->if_drv_flags |= IFF_DRV_RUNNING;
460 dwc_setup_rxfilter(sc);
462 /* Initializa DMA and enable transmitters */
463 reg = READ4(sc, OPERATION_MODE);
464 reg |= (MODE_TSF | MODE_OSF | MODE_FUF);
466 reg |= (MODE_RTC_LEV32 << MODE_RTC_SHIFT);
467 WRITE4(sc, OPERATION_MODE, reg);
469 WRITE4(sc, INTERRUPT_ENABLE, INT_EN_DEFAULT);
472 reg = READ4(sc, OPERATION_MODE);
473 reg |= (MODE_ST | MODE_SR);
474 WRITE4(sc, OPERATION_MODE, reg);
476 /* Enable transmitters */
477 reg = READ4(sc, MAC_CONFIGURATION);
478 reg |= (CONF_JD | CONF_ACS | CONF_BE);
479 reg |= (CONF_TE | CONF_RE);
480 WRITE4(sc, MAC_CONFIGURATION, reg);
483 * Call mii_mediachg() which will call back into dwc_miibus_statchg()
484 * to set up the remaining config registers based on current media.
486 mii_mediachg(sc->mii_softc);
487 callout_reset(&sc->dwc_callout, hz, dwc_tick, sc);
491 dwc_init(void *if_softc)
493 struct dwc_softc *sc = if_softc;
500 inline static uint32_t
501 dwc_setup_rxdesc(struct dwc_softc *sc, int idx, bus_addr_t paddr)
505 sc->rxdesc_ring[idx].addr = (uint32_t)paddr;
506 nidx = next_rxidx(sc, idx);
507 sc->rxdesc_ring[idx].addr_next = sc->rxdesc_ring_paddr + \
508 (nidx * sizeof(struct dwc_hwdesc));
509 sc->rxdesc_ring[idx].tdes1 = DDESC_RDES1_CHAINED | MCLBYTES;
512 sc->rxdesc_ring[idx].tdes0 = DDESC_RDES0_OWN;
519 dwc_setup_rxbuf(struct dwc_softc *sc, int idx, struct mbuf *m)
521 struct bus_dma_segment seg;
524 m_adj(m, ETHER_ALIGN);
526 error = bus_dmamap_load_mbuf_sg(sc->rxbuf_tag, sc->rxbuf_map[idx].map,
532 KASSERT(nsegs == 1, ("%s: %d segments returned!", __func__, nsegs));
534 bus_dmamap_sync(sc->rxbuf_tag, sc->rxbuf_map[idx].map,
535 BUS_DMASYNC_PREREAD);
537 sc->rxbuf_map[idx].mbuf = m;
538 dwc_setup_rxdesc(sc, idx, seg.ds_addr);
544 dwc_alloc_mbufcl(struct dwc_softc *sc)
548 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
550 m->m_pkthdr.len = m->m_len = m->m_ext.ext_size;
556 dwc_media_status(struct ifnet * ifp, struct ifmediareq *ifmr)
558 struct dwc_softc *sc;
559 struct mii_data *mii;
565 ifmr->ifm_active = mii->mii_media_active;
566 ifmr->ifm_status = mii->mii_media_status;
571 dwc_media_change_locked(struct dwc_softc *sc)
574 return (mii_mediachg(sc->mii_softc));
578 dwc_media_change(struct ifnet * ifp)
580 struct dwc_softc *sc;
586 error = dwc_media_change_locked(sc);
591 static const uint8_t nibbletab[] = {
592 /* 0x0 0000 -> 0000 */ 0x0,
593 /* 0x1 0001 -> 1000 */ 0x8,
594 /* 0x2 0010 -> 0100 */ 0x4,
595 /* 0x3 0011 -> 1100 */ 0xc,
596 /* 0x4 0100 -> 0010 */ 0x2,
597 /* 0x5 0101 -> 1010 */ 0xa,
598 /* 0x6 0110 -> 0110 */ 0x6,
599 /* 0x7 0111 -> 1110 */ 0xe,
600 /* 0x8 1000 -> 0001 */ 0x1,
601 /* 0x9 1001 -> 1001 */ 0x9,
602 /* 0xa 1010 -> 0101 */ 0x5,
603 /* 0xb 1011 -> 1101 */ 0xd,
604 /* 0xc 1100 -> 0011 */ 0x3,
605 /* 0xd 1101 -> 1011 */ 0xb,
606 /* 0xe 1110 -> 0111 */ 0x7,
607 /* 0xf 1111 -> 1111 */ 0xf, };
610 bitreverse(uint8_t x)
613 return (nibbletab[x & 0xf] << 4) | nibbletab[x >> 4];
617 dwc_setup_rxfilter(struct dwc_softc *sc)
619 struct ifmultiaddr *ifma;
631 DWC_ASSERT_LOCKED(sc);
636 * Set the multicast (group) filter hash.
638 if ((ifp->if_flags & IFF_ALLMULTI))
639 ffval = (FRAME_FILTER_PM);
641 ffval = (FRAME_FILTER_HMC);
643 TAILQ_FOREACH(ifma, &sc->ifp->if_multiaddrs, ifma_link) {
644 if (ifma->ifma_addr->sa_family != AF_LINK)
646 crc = ether_crc32_le(LLADDR((struct sockaddr_dl *)
647 ifma->ifma_addr), ETHER_ADDR_LEN);
649 /* Take lower 8 bits and reverse it */
650 val = bitreverse(~crc & 0xff);
651 hashreg = (val >> 5);
652 hashbit = (val & 31);
654 reg = READ4(sc, HASH_TABLE_REG(hashreg));
655 reg |= (1 << hashbit);
656 WRITE4(sc, HASH_TABLE_REG(hashreg), reg);
658 if_maddr_runlock(ifp);
662 * Set the individual address filter hash.
664 if (ifp->if_flags & IFF_PROMISC)
665 ffval |= (FRAME_FILTER_PR);
668 * Set the primary address.
670 eaddr = IF_LLADDR(ifp);
671 lo = eaddr[0] | (eaddr[1] << 8) | (eaddr[2] << 16) |
673 hi = eaddr[4] | (eaddr[5] << 8);
674 WRITE4(sc, MAC_ADDRESS_LOW(0), lo);
675 WRITE4(sc, MAC_ADDRESS_HIGH(0), hi);
676 WRITE4(sc, MAC_FRAME_FILTER, ffval);
680 dwc_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
682 struct dwc_softc *sc;
683 struct mii_data *mii;
688 ifr = (struct ifreq *)data;
694 if (ifp->if_flags & IFF_UP) {
695 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
696 if ((ifp->if_flags ^ sc->if_flags) &
697 (IFF_PROMISC | IFF_ALLMULTI))
698 dwc_setup_rxfilter(sc);
700 if (!sc->is_detaching)
704 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
707 sc->if_flags = ifp->if_flags;
712 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
714 dwc_setup_rxfilter(sc);
721 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, cmd);
724 mask = ifp->if_capenable ^ ifr->ifr_reqcap;
725 if (mask & IFCAP_VLAN_MTU) {
726 /* No work to do except acknowledge the change took */
727 ifp->if_capenable ^= IFCAP_VLAN_MTU;
732 error = ether_ioctl(ifp, cmd, data);
740 dwc_txfinish_locked(struct dwc_softc *sc)
742 struct dwc_bufmap *bmap;
743 struct dwc_hwdesc *desc;
746 DWC_ASSERT_LOCKED(sc);
749 while (sc->tx_idx_tail != sc->tx_idx_head) {
750 desc = &sc->txdesc_ring[sc->tx_idx_tail];
751 if ((desc->tdes0 & DDESC_TDES0_OWN) != 0)
753 bmap = &sc->txbuf_map[sc->tx_idx_tail];
754 bus_dmamap_sync(sc->txbuf_tag, bmap->map,
755 BUS_DMASYNC_POSTWRITE);
756 bus_dmamap_unload(sc->txbuf_tag, bmap->map);
759 dwc_setup_txdesc(sc, sc->tx_idx_tail, 0, 0);
760 sc->tx_idx_tail = next_txidx(sc, sc->tx_idx_tail);
761 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
764 /* If there are no buffers outstanding, muzzle the watchdog. */
765 if (sc->tx_idx_tail == sc->tx_idx_head) {
766 sc->tx_watchdog_count = 0;
771 dwc_rxfinish_locked(struct dwc_softc *sc)
786 rdes0 = sc->rxdesc_ring[idx].tdes0;
787 if ((rdes0 & DDESC_RDES0_OWN) != 0)
790 bus_dmamap_sync(sc->rxbuf_tag, sc->rxbuf_map[idx].map,
791 BUS_DMASYNC_POSTREAD);
792 bus_dmamap_unload(sc->rxbuf_tag, sc->rxbuf_map[idx].map);
794 len = (rdes0 >> DDESC_RDES0_FL_SHIFT) & DDESC_RDES0_FL_MASK;
796 m = sc->rxbuf_map[idx].mbuf;
797 m->m_pkthdr.rcvif = ifp;
798 m->m_pkthdr.len = len;
800 if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
803 (*ifp->if_input)(ifp, m);
806 /* XXX Zero-length packet ? */
809 if ((m0 = dwc_alloc_mbufcl(sc)) != NULL) {
810 if ((error = dwc_setup_rxbuf(sc, idx, m0)) != 0) {
813 * We've got a hole in the rx ring.
817 if_inc_counter(sc->ifp, IFCOUNTER_IQDROPS, 1);
819 sc->rx_idx = next_rxidx(sc, sc->rx_idx);
826 struct dwc_softc *sc;
833 reg = READ4(sc, INTERRUPT_STATUS);
835 mii_mediachg(sc->mii_softc);
836 READ4(sc, SGMII_RGMII_SMII_CTRL_STATUS);
839 reg = READ4(sc, DMA_STATUS);
840 if (reg & DMA_STATUS_NIS) {
841 if (reg & DMA_STATUS_RI)
842 dwc_rxfinish_locked(sc);
844 if (reg & DMA_STATUS_TI) {
845 dwc_txfinish_locked(sc);
846 dwc_txstart_locked(sc);
850 if (reg & DMA_STATUS_AIS) {
851 if (reg & DMA_STATUS_FBI) {
852 /* Fatal bus error */
853 device_printf(sc->dev,
854 "Ethernet DMA error, restarting controller.\n");
860 WRITE4(sc, DMA_STATUS, reg & DMA_STATUS_INTR_MASK);
865 setup_dma(struct dwc_softc *sc)
873 * Set up TX descriptor ring, descriptors, and dma maps.
875 error = bus_dma_tag_create(
876 bus_get_dma_tag(sc->dev), /* Parent tag. */
877 DWC_DESC_RING_ALIGN, 0, /* alignment, boundary */
878 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
879 BUS_SPACE_MAXADDR, /* highaddr */
880 NULL, NULL, /* filter, filterarg */
881 TX_DESC_SIZE, 1, /* maxsize, nsegments */
882 TX_DESC_SIZE, /* maxsegsize */
884 NULL, NULL, /* lockfunc, lockarg */
887 device_printf(sc->dev,
888 "could not create TX ring DMA tag.\n");
892 error = bus_dmamem_alloc(sc->txdesc_tag, (void**)&sc->txdesc_ring,
893 BUS_DMA_COHERENT | BUS_DMA_WAITOK | BUS_DMA_ZERO,
896 device_printf(sc->dev,
897 "could not allocate TX descriptor ring.\n");
901 error = bus_dmamap_load(sc->txdesc_tag, sc->txdesc_map,
902 sc->txdesc_ring, TX_DESC_SIZE, dwc_get1paddr,
903 &sc->txdesc_ring_paddr, 0);
905 device_printf(sc->dev,
906 "could not load TX descriptor ring map.\n");
910 for (idx = 0; idx < TX_DESC_COUNT; idx++) {
911 sc->txdesc_ring[idx].tdes0 = DDESC_TDES0_TXCHAIN;
912 sc->txdesc_ring[idx].tdes1 = 0;
913 nidx = next_txidx(sc, idx);
914 sc->txdesc_ring[idx].addr_next = sc->txdesc_ring_paddr + \
915 (nidx * sizeof(struct dwc_hwdesc));
918 error = bus_dma_tag_create(
919 bus_get_dma_tag(sc->dev), /* Parent tag. */
920 1, 0, /* alignment, boundary */
921 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
922 BUS_SPACE_MAXADDR, /* highaddr */
923 NULL, NULL, /* filter, filterarg */
924 MCLBYTES, 1, /* maxsize, nsegments */
925 MCLBYTES, /* maxsegsize */
927 NULL, NULL, /* lockfunc, lockarg */
930 device_printf(sc->dev,
931 "could not create TX ring DMA tag.\n");
935 for (idx = 0; idx < TX_DESC_COUNT; idx++) {
936 error = bus_dmamap_create(sc->txbuf_tag, BUS_DMA_COHERENT,
937 &sc->txbuf_map[idx].map);
939 device_printf(sc->dev,
940 "could not create TX buffer DMA map.\n");
943 dwc_setup_txdesc(sc, idx, 0, 0);
947 * Set up RX descriptor ring, descriptors, dma maps, and mbufs.
949 error = bus_dma_tag_create(
950 bus_get_dma_tag(sc->dev), /* Parent tag. */
951 DWC_DESC_RING_ALIGN, 0, /* alignment, boundary */
952 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
953 BUS_SPACE_MAXADDR, /* highaddr */
954 NULL, NULL, /* filter, filterarg */
955 RX_DESC_SIZE, 1, /* maxsize, nsegments */
956 RX_DESC_SIZE, /* maxsegsize */
958 NULL, NULL, /* lockfunc, lockarg */
961 device_printf(sc->dev,
962 "could not create RX ring DMA tag.\n");
966 error = bus_dmamem_alloc(sc->rxdesc_tag, (void **)&sc->rxdesc_ring,
967 BUS_DMA_COHERENT | BUS_DMA_WAITOK | BUS_DMA_ZERO,
970 device_printf(sc->dev,
971 "could not allocate RX descriptor ring.\n");
975 error = bus_dmamap_load(sc->rxdesc_tag, sc->rxdesc_map,
976 sc->rxdesc_ring, RX_DESC_SIZE, dwc_get1paddr,
977 &sc->rxdesc_ring_paddr, 0);
979 device_printf(sc->dev,
980 "could not load RX descriptor ring map.\n");
984 error = bus_dma_tag_create(
985 bus_get_dma_tag(sc->dev), /* Parent tag. */
986 1, 0, /* alignment, boundary */
987 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
988 BUS_SPACE_MAXADDR, /* highaddr */
989 NULL, NULL, /* filter, filterarg */
990 MCLBYTES, 1, /* maxsize, nsegments */
991 MCLBYTES, /* maxsegsize */
993 NULL, NULL, /* lockfunc, lockarg */
996 device_printf(sc->dev,
997 "could not create RX buf DMA tag.\n");
1001 for (idx = 0; idx < RX_DESC_COUNT; idx++) {
1002 error = bus_dmamap_create(sc->rxbuf_tag, BUS_DMA_COHERENT,
1003 &sc->rxbuf_map[idx].map);
1005 device_printf(sc->dev,
1006 "could not create RX buffer DMA map.\n");
1009 if ((m = dwc_alloc_mbufcl(sc)) == NULL) {
1010 device_printf(sc->dev, "Could not alloc mbuf\n");
1014 if ((error = dwc_setup_rxbuf(sc, idx, m)) != 0) {
1015 device_printf(sc->dev,
1016 "could not create new RX buffer.\n");
1029 dwc_get_hwaddr(struct dwc_softc *sc, uint8_t *hwaddr)
1036 * Try to recover a MAC address from the running hardware. If there's
1037 * something non-zero there, assume the bootloader did the right thing
1040 * Otherwise, set the address to a convenient locally assigned address,
1041 * 'bsd' + random 24 low-order bits. 'b' is 0x62, which has the locally
1042 * assigned bit set, and the broadcast/multicast bit clear.
1044 lo = READ4(sc, MAC_ADDRESS_LOW(0));
1045 hi = READ4(sc, MAC_ADDRESS_HIGH(0)) & 0xffff;
1046 if ((lo != 0xffffffff) || (hi != 0xffff)) {
1047 hwaddr[0] = (lo >> 0) & 0xff;
1048 hwaddr[1] = (lo >> 8) & 0xff;
1049 hwaddr[2] = (lo >> 16) & 0xff;
1050 hwaddr[3] = (lo >> 24) & 0xff;
1051 hwaddr[4] = (hi >> 0) & 0xff;
1052 hwaddr[5] = (hi >> 8) & 0xff;
1054 rnd = arc4random() & 0x00ffffff;
1058 hwaddr[3] = rnd >> 16;
1059 hwaddr[4] = rnd >> 8;
1060 hwaddr[5] = rnd >> 0;
1067 dwc_probe(device_t dev)
1070 if (!ofw_bus_status_okay(dev))
1073 if (!ofw_bus_is_compatible(dev, "snps,dwmac"))
1076 device_set_desc(dev, "Gigabit Ethernet Controller");
1077 return (BUS_PROBE_DEFAULT);
1081 dwc_attach(device_t dev)
1083 uint8_t macaddr[ETHER_ADDR_LEN];
1084 struct dwc_softc *sc;
1090 sc = device_get_softc(dev);
1092 sc->mii_clk = MII_CLK_VAL;
1095 sc->txcount = TX_DESC_COUNT;
1097 if (bus_alloc_resources(dev, dwc_spec, sc->res)) {
1098 device_printf(dev, "could not allocate resources\n");
1102 /* Memory interface */
1103 sc->bst = rman_get_bustag(sc->res[0]);
1104 sc->bsh = rman_get_bushandle(sc->res[0]);
1106 /* Read MAC before reset */
1107 if (dwc_get_hwaddr(sc, macaddr)) {
1108 device_printf(sc->dev, "can't get mac\n");
1113 reg = READ4(sc, BUS_MODE);
1114 reg |= (BUS_MODE_SWR);
1115 WRITE4(sc, BUS_MODE, reg);
1117 for (i = 0; i < MAC_RESET_TIMEOUT; i++) {
1118 if ((READ4(sc, BUS_MODE) & BUS_MODE_SWR) == 0)
1122 if (i >= MAC_RESET_TIMEOUT) {
1123 device_printf(sc->dev, "Can't reset DWC.\n");
1127 reg = READ4(sc, BUS_MODE);
1128 reg |= (BUS_MODE_EIGHTXPBL);
1129 reg |= (BUS_MODE_PBL_BEATS_8 << BUS_MODE_PBL_SHIFT);
1130 WRITE4(sc, BUS_MODE, reg);
1133 * DMA must be stop while changing descriptor list addresses.
1135 reg = READ4(sc, OPERATION_MODE);
1136 reg &= ~(MODE_ST | MODE_SR);
1137 WRITE4(sc, OPERATION_MODE, reg);
1142 /* Setup addresses */
1143 WRITE4(sc, RX_DESCR_LIST_ADDR, sc->rxdesc_ring_paddr);
1144 WRITE4(sc, TX_DESCR_LIST_ADDR, sc->txdesc_ring_paddr);
1146 mtx_init(&sc->mtx, device_get_nameunit(sc->dev),
1147 MTX_NETWORK_LOCK, MTX_DEF);
1149 callout_init_mtx(&sc->dwc_callout, &sc->mtx, 0);
1151 /* Setup interrupt handler. */
1152 error = bus_setup_intr(dev, sc->res[1], INTR_TYPE_NET | INTR_MPSAFE,
1153 NULL, dwc_intr, sc, &sc->intr_cookie);
1155 device_printf(dev, "could not setup interrupt handler.\n");
1159 /* Set up the ethernet interface. */
1160 sc->ifp = ifp = if_alloc(IFT_ETHER);
1163 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
1164 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
1165 ifp->if_capabilities = IFCAP_VLAN_MTU;
1166 ifp->if_capenable = ifp->if_capabilities;
1167 ifp->if_start = dwc_txstart;
1168 ifp->if_ioctl = dwc_ioctl;
1169 ifp->if_init = dwc_init;
1170 IFQ_SET_MAXLEN(&ifp->if_snd, TX_DESC_COUNT - 1);
1171 ifp->if_snd.ifq_drv_maxlen = TX_DESC_COUNT - 1;
1172 IFQ_SET_READY(&ifp->if_snd);
1173 ifp->if_hdrlen = sizeof(struct ether_vlan_header);
1175 /* Attach the mii driver. */
1176 error = mii_attach(dev, &sc->miibus, ifp, dwc_media_change,
1177 dwc_media_status, BMSR_DEFCAPMASK, MII_PHY_ANY,
1181 device_printf(dev, "PHY attach failed\n");
1184 sc->mii_softc = device_get_softc(sc->miibus);
1186 /* All ready to run, attach the ethernet interface. */
1187 ether_ifattach(ifp, macaddr);
1188 sc->is_attached = true;
1194 dwc_miibus_read_reg(device_t dev, int phy, int reg)
1196 struct dwc_softc *sc;
1201 sc = device_get_softc(dev);
1203 mii = ((phy & GMII_ADDRESS_PA_MASK) << GMII_ADDRESS_PA_SHIFT)
1204 | ((reg & GMII_ADDRESS_GR_MASK) << GMII_ADDRESS_GR_SHIFT)
1205 | (sc->mii_clk << GMII_ADDRESS_CR_SHIFT)
1206 | GMII_ADDRESS_GB; /* Busy flag */
1208 WRITE4(sc, GMII_ADDRESS, mii);
1210 for (cnt = 0; cnt < 1000; cnt++) {
1211 if (!(READ4(sc, GMII_ADDRESS) & GMII_ADDRESS_GB)) {
1212 rv = READ4(sc, GMII_DATA);
1222 dwc_miibus_write_reg(device_t dev, int phy, int reg, int val)
1224 struct dwc_softc *sc;
1228 sc = device_get_softc(dev);
1230 mii = ((phy & GMII_ADDRESS_PA_MASK) << GMII_ADDRESS_PA_SHIFT)
1231 | ((reg & GMII_ADDRESS_GR_MASK) << GMII_ADDRESS_GR_SHIFT)
1232 | (sc->mii_clk << GMII_ADDRESS_CR_SHIFT)
1233 | GMII_ADDRESS_GB | GMII_ADDRESS_GW;
1235 WRITE4(sc, GMII_DATA, val);
1236 WRITE4(sc, GMII_ADDRESS, mii);
1238 for (cnt = 0; cnt < 1000; cnt++) {
1239 if (!(READ4(sc, GMII_ADDRESS) & GMII_ADDRESS_GB)) {
1249 dwc_miibus_statchg(device_t dev)
1251 struct dwc_softc *sc;
1252 struct mii_data *mii;
1256 * Called by the MII bus driver when the PHY establishes
1257 * link to set the MAC interface registers.
1260 sc = device_get_softc(dev);
1262 DWC_ASSERT_LOCKED(sc);
1264 mii = sc->mii_softc;
1266 if (mii->mii_media_status & IFM_ACTIVE)
1267 sc->link_is_up = true;
1269 sc->link_is_up = false;
1271 reg = READ4(sc, MAC_CONFIGURATION);
1272 switch (IFM_SUBTYPE(mii->mii_media_active)) {
1275 reg &= ~(CONF_FES | CONF_PS);
1278 reg |= (CONF_FES | CONF_PS);
1285 sc->link_is_up = false;
1288 sc->link_is_up = false;
1289 device_printf(dev, "Unsupported media %u\n",
1290 IFM_SUBTYPE(mii->mii_media_active));
1293 if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) != 0)
1297 WRITE4(sc, MAC_CONFIGURATION, reg);
1300 static device_method_t dwc_methods[] = {
1301 DEVMETHOD(device_probe, dwc_probe),
1302 DEVMETHOD(device_attach, dwc_attach),
1305 DEVMETHOD(miibus_readreg, dwc_miibus_read_reg),
1306 DEVMETHOD(miibus_writereg, dwc_miibus_write_reg),
1307 DEVMETHOD(miibus_statchg, dwc_miibus_statchg),
1312 static driver_t dwc_driver = {
1315 sizeof(struct dwc_softc),
1318 static devclass_t dwc_devclass;
1320 DRIVER_MODULE(dwc, simplebus, dwc_driver, dwc_devclass, 0, 0);
1321 DRIVER_MODULE(miibus, dwc, miibus_driver, miibus_devclass, 0, 0);
1323 MODULE_DEPEND(dwc, ether, 1, 1, 1);
1324 MODULE_DEPEND(dwc, miibus, 1, 1, 1);