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>
46 #include <sys/kernel.h>
48 #include <sys/malloc.h>
50 #include <sys/module.h>
51 #include <sys/mutex.h>
53 #include <sys/socket.h>
54 #include <sys/sockio.h>
58 #include <net/ethernet.h>
59 #include <net/if_dl.h>
60 #include <net/if_media.h>
61 #include <net/if_types.h>
62 #include <net/if_var.h>
64 #include <machine/bus.h>
66 #include <dev/dwc/if_dwc.h>
67 #include <dev/dwc/if_dwcvar.h>
68 #include <dev/mii/mii.h>
69 #include <dev/mii/miivar.h>
70 #include <dev/ofw/ofw_bus.h>
71 #include <dev/ofw/ofw_bus_subr.h>
72 #include <dev/mii/mii_fdt.h>
74 #include <dev/extres/clk/clk.h>
75 #include <dev/extres/hwreset/hwreset.h>
77 #include "if_dwc_if.h"
79 #include "miibus_if.h"
81 #define READ4(_sc, _reg) \
82 bus_read_4((_sc)->res[0], _reg)
83 #define WRITE4(_sc, _reg, _val) \
84 bus_write_4((_sc)->res[0], _reg, _val)
86 #define MAC_RESET_TIMEOUT 100
87 #define WATCHDOG_TIMEOUT_SECS 5
88 #define STATS_HARVEST_INTERVAL 2
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 /* TX descriptors - TDESC0 is almost unified */
96 #define TDESC0_OWN (1U << 31)
97 #define TDESC0_IHE (1U << 16) /* IP Header Error */
98 #define TDESC0_ES (1U << 15) /* Error Summary */
99 #define TDESC0_JT (1U << 14) /* Jabber Timeout */
100 #define TDESC0_FF (1U << 13) /* Frame Flushed */
101 #define TDESC0_PCE (1U << 12) /* Payload Checksum Error */
102 #define TDESC0_LOC (1U << 11) /* Loss of Carrier */
103 #define TDESC0_NC (1U << 10) /* No Carrier */
104 #define TDESC0_LC (1U << 9) /* Late Collision */
105 #define TDESC0_EC (1U << 8) /* Excessive Collision */
106 #define TDESC0_VF (1U << 7) /* VLAN Frame */
107 #define TDESC0_CC_MASK 0xf
108 #define TDESC0_CC_SHIFT 3 /* Collision Count */
109 #define TDESC0_ED (1U << 2) /* Excessive Deferral */
110 #define TDESC0_UF (1U << 1) /* Underflow Error */
111 #define TDESC0_DB (1U << 0) /* Deferred Bit */
112 /* TX descriptors - TDESC0 extended format only */
113 #define ETDESC0_IC (1U << 30) /* Interrupt on Completion */
114 #define ETDESC0_LS (1U << 29) /* Last Segment */
115 #define ETDESC0_FS (1U << 28) /* First Segment */
116 #define ETDESC0_DC (1U << 27) /* Disable CRC */
117 #define ETDESC0_DP (1U << 26) /* Disable Padding */
118 #define ETDESC0_CIC_NONE (0U << 22) /* Checksum Insertion Control */
119 #define ETDESC0_CIC_HDR (1U << 22)
120 #define ETDESC0_CIC_SEG (2U << 22)
121 #define ETDESC0_CIC_FULL (3U << 22)
122 #define ETDESC0_TER (1U << 21) /* Transmit End of Ring */
123 #define ETDESC0_TCH (1U << 20) /* Second Address Chained */
125 /* TX descriptors - TDESC1 normal format */
126 #define NTDESC1_IC (1U << 31) /* Interrupt on Completion */
127 #define NTDESC1_LS (1U << 30) /* Last Segment */
128 #define NTDESC1_FS (1U << 29) /* First Segment */
129 #define NTDESC1_CIC_NONE (0U << 27) /* Checksum Insertion Control */
130 #define NTDESC1_CIC_HDR (1U << 27)
131 #define NTDESC1_CIC_SEG (2U << 27)
132 #define NTDESC1_CIC_FULL (3U << 27)
133 #define NTDESC1_DC (1U << 26) /* Disable CRC */
134 #define NTDESC1_TER (1U << 25) /* Transmit End of Ring */
135 #define NTDESC1_TCH (1U << 24) /* Second Address Chained */
136 /* TX descriptors - TDESC1 extended format */
137 #define ETDESC1_DP (1U << 23) /* Disable Padding */
138 #define ETDESC1_TBS2_MASK 0x7ff
139 #define ETDESC1_TBS2_SHIFT 11 /* Receive Buffer 2 Size */
140 #define ETDESC1_TBS1_MASK 0x7ff
141 #define ETDESC1_TBS1_SHIFT 0 /* Receive Buffer 1 Size */
143 /* RX descriptor - RDESC0 is unified */
144 #define RDESC0_OWN (1U << 31)
145 #define RDESC0_AFM (1U << 30) /* Dest. Address Filter Fail */
146 #define RDESC0_FL_MASK 0x3fff
147 #define RDESC0_FL_SHIFT 16 /* Frame Length */
148 #define RDESC0_ES (1U << 15) /* Error Summary */
149 #define RDESC0_DE (1U << 14) /* Descriptor Error */
150 #define RDESC0_SAF (1U << 13) /* Source Address Filter Fail */
151 #define RDESC0_LE (1U << 12) /* Length Error */
152 #define RDESC0_OE (1U << 11) /* Overflow Error */
153 #define RDESC0_VLAN (1U << 10) /* VLAN Tag */
154 #define RDESC0_FS (1U << 9) /* First Descriptor */
155 #define RDESC0_LS (1U << 8) /* Last Descriptor */
156 #define RDESC0_ICE (1U << 7) /* IPC Checksum Error */
157 #define RDESC0_LC (1U << 6) /* Late Collision */
158 #define RDESC0_FT (1U << 5) /* Frame Type */
159 #define RDESC0_RWT (1U << 4) /* Receive Watchdog Timeout */
160 #define RDESC0_RE (1U << 3) /* Receive Error */
161 #define RDESC0_DBE (1U << 2) /* Dribble Bit Error */
162 #define RDESC0_CE (1U << 1) /* CRC Error */
163 #define RDESC0_PCE (1U << 0) /* Payload Checksum Error */
164 #define RDESC0_RXMA (1U << 0) /* Rx MAC Address */
166 /* RX descriptors - RDESC1 normal format */
167 #define NRDESC1_DIC (1U << 31) /* Disable Intr on Completion */
168 #define NRDESC1_RER (1U << 25) /* Receive End of Ring */
169 #define NRDESC1_RCH (1U << 24) /* Second Address Chained */
170 #define NRDESC1_RBS2_MASK 0x7ff
171 #define NRDESC1_RBS2_SHIFT 11 /* Receive Buffer 2 Size */
172 #define NRDESC1_RBS1_MASK 0x7ff
173 #define NRDESC1_RBS1_SHIFT 0 /* Receive Buffer 1 Size */
175 /* RX descriptors - RDESC1 enhanced format */
176 #define ERDESC1_DIC (1U << 31) /* Disable Intr on Completion */
177 #define ERDESC1_RBS2_MASK 0x7ffff
178 #define ERDESC1_RBS2_SHIFT 16 /* Receive Buffer 2 Size */
179 #define ERDESC1_RER (1U << 15) /* Receive End of Ring */
180 #define ERDESC1_RCH (1U << 14) /* Second Address Chained */
181 #define ERDESC1_RBS1_MASK 0x7ffff
182 #define ERDESC1_RBS1_SHIFT 0 /* Receive Buffer 1 Size */
185 * A hardware buffer descriptor. Rx and Tx buffers have the same descriptor
186 * layout, but the bits in the fields have different meanings.
192 uint32_t addr1; /* ptr to first buffer data */
193 uint32_t addr2; /* ptr to next descriptor / second buffer data*/
197 struct dwc_hash_maddr_ctx {
198 struct dwc_softc *sc;
203 * The hardware imposes alignment restrictions on various objects involved in
204 * DMA transfers. These values are expressed in bytes (not bits).
206 #define DWC_DESC_RING_ALIGN 2048
208 static struct resource_spec dwc_spec[] = {
209 { SYS_RES_MEMORY, 0, RF_ACTIVE },
210 { SYS_RES_IRQ, 0, RF_ACTIVE },
214 static void dwc_txfinish_locked(struct dwc_softc *sc);
215 static void dwc_rxfinish_locked(struct dwc_softc *sc);
216 static void dwc_stop_locked(struct dwc_softc *sc);
217 static void dwc_setup_rxfilter(struct dwc_softc *sc);
218 static void dwc_setup_core(struct dwc_softc *sc);
219 static void dwc_enable_mac(struct dwc_softc *sc, bool enable);
220 static void dwc_init_dma(struct dwc_softc *sc);
221 static void dwc_stop_dma(struct dwc_softc *sc);
223 static void dwc_tick(void *arg);
225 /* Pause time field in the transmitted control frame */
226 static int dwc_pause_time = 0xffff;
227 TUNABLE_INT("hw.dwc.pause_time", &dwc_pause_time);
234 dwc_miibus_read_reg(device_t dev, int phy, int reg)
236 struct dwc_softc *sc;
241 sc = device_get_softc(dev);
243 mii = ((phy & GMII_ADDRESS_PA_MASK) << GMII_ADDRESS_PA_SHIFT)
244 | ((reg & GMII_ADDRESS_GR_MASK) << GMII_ADDRESS_GR_SHIFT)
245 | (sc->mii_clk << GMII_ADDRESS_CR_SHIFT)
246 | GMII_ADDRESS_GB; /* Busy flag */
248 WRITE4(sc, GMII_ADDRESS, mii);
250 for (cnt = 0; cnt < 1000; cnt++) {
251 if (!(READ4(sc, GMII_ADDRESS) & GMII_ADDRESS_GB)) {
252 rv = READ4(sc, GMII_DATA);
262 dwc_miibus_write_reg(device_t dev, int phy, int reg, int val)
264 struct dwc_softc *sc;
268 sc = device_get_softc(dev);
270 mii = ((phy & GMII_ADDRESS_PA_MASK) << GMII_ADDRESS_PA_SHIFT)
271 | ((reg & GMII_ADDRESS_GR_MASK) << GMII_ADDRESS_GR_SHIFT)
272 | (sc->mii_clk << GMII_ADDRESS_CR_SHIFT)
273 | GMII_ADDRESS_GB | GMII_ADDRESS_GW;
275 WRITE4(sc, GMII_DATA, val);
276 WRITE4(sc, GMII_ADDRESS, mii);
278 for (cnt = 0; cnt < 1000; cnt++) {
279 if (!(READ4(sc, GMII_ADDRESS) & GMII_ADDRESS_GB)) {
289 dwc_miibus_statchg(device_t dev)
291 struct dwc_softc *sc;
292 struct mii_data *mii;
296 * Called by the MII bus driver when the PHY establishes
297 * link to set the MAC interface registers.
300 sc = device_get_softc(dev);
302 DWC_ASSERT_LOCKED(sc);
306 if (mii->mii_media_status & IFM_ACTIVE)
307 sc->link_is_up = true;
309 sc->link_is_up = false;
311 reg = READ4(sc, MAC_CONFIGURATION);
312 switch (IFM_SUBTYPE(mii->mii_media_active)) {
315 reg &= ~(CONF_FES | CONF_PS);
318 reg |= (CONF_FES | CONF_PS);
325 sc->link_is_up = false;
328 sc->link_is_up = false;
329 device_printf(dev, "Unsupported media %u\n",
330 IFM_SUBTYPE(mii->mii_media_active));
333 if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) != 0)
337 WRITE4(sc, MAC_CONFIGURATION, reg);
339 reg = FLOW_CONTROL_UP;
340 if ((IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_TXPAUSE) != 0)
341 reg |= FLOW_CONTROL_TX;
342 if ((IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_RXPAUSE) != 0)
343 reg |= FLOW_CONTROL_RX;
344 if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) != 0)
345 reg |= dwc_pause_time << FLOW_CONTROL_PT_SHIFT;
346 WRITE4(sc, FLOW_CONTROL, reg);
348 IF_DWC_SET_SPEED(dev, IFM_SUBTYPE(mii->mii_media_active));
357 dwc_media_status(if_t ifp, struct ifmediareq *ifmr)
359 struct dwc_softc *sc;
360 struct mii_data *mii;
362 sc = if_getsoftc(ifp);
366 ifmr->ifm_active = mii->mii_media_active;
367 ifmr->ifm_status = mii->mii_media_status;
372 dwc_media_change_locked(struct dwc_softc *sc)
375 return (mii_mediachg(sc->mii_softc));
379 dwc_media_change(if_t ifp)
381 struct dwc_softc *sc;
384 sc = if_getsoftc(ifp);
387 error = dwc_media_change_locked(sc);
396 static const uint8_t nibbletab[] = {
397 /* 0x0 0000 -> 0000 */ 0x0,
398 /* 0x1 0001 -> 1000 */ 0x8,
399 /* 0x2 0010 -> 0100 */ 0x4,
400 /* 0x3 0011 -> 1100 */ 0xc,
401 /* 0x4 0100 -> 0010 */ 0x2,
402 /* 0x5 0101 -> 1010 */ 0xa,
403 /* 0x6 0110 -> 0110 */ 0x6,
404 /* 0x7 0111 -> 1110 */ 0xe,
405 /* 0x8 1000 -> 0001 */ 0x1,
406 /* 0x9 1001 -> 1001 */ 0x9,
407 /* 0xa 1010 -> 0101 */ 0x5,
408 /* 0xb 1011 -> 1101 */ 0xd,
409 /* 0xc 1100 -> 0011 */ 0x3,
410 /* 0xd 1101 -> 1011 */ 0xb,
411 /* 0xe 1110 -> 0111 */ 0x7,
412 /* 0xf 1111 -> 1111 */ 0xf, };
415 bitreverse(uint8_t x)
418 return (nibbletab[x & 0xf] << 4) | nibbletab[x >> 4];
422 dwc_hash_maddr(void *arg, struct sockaddr_dl *sdl, u_int cnt)
424 struct dwc_hash_maddr_ctx *ctx = arg;
425 uint32_t crc, hashbit, hashreg;
428 crc = ether_crc32_le(LLADDR(sdl), ETHER_ADDR_LEN);
429 /* Take lower 8 bits and reverse it */
430 val = bitreverse(~crc & 0xff);
431 if (ctx->sc->mactype != DWC_GMAC_EXT_DESC)
432 val >>= 2; /* Only need lower 6 bits */
433 hashreg = (val >> 5);
434 hashbit = (val & 31);
435 ctx->hash[hashreg] |= (1 << hashbit);
441 dwc_setup_rxfilter(struct dwc_softc *sc)
443 struct dwc_hash_maddr_ctx ctx;
446 uint32_t ffval, hi, lo;
449 DWC_ASSERT_LOCKED(sc);
452 nhash = sc->mactype != DWC_GMAC_EXT_DESC ? 2 : 8;
455 * Set the multicast (group) filter hash.
457 if ((if_getflags(ifp) & IFF_ALLMULTI) != 0) {
458 ffval = (FRAME_FILTER_PM);
459 for (i = 0; i < nhash; i++)
462 ffval = (FRAME_FILTER_HMC);
463 for (i = 0; i < nhash; i++)
466 if_foreach_llmaddr(ifp, dwc_hash_maddr, &ctx);
470 * Set the individual address filter hash.
472 if ((if_getflags(ifp) & IFF_PROMISC) != 0)
473 ffval |= (FRAME_FILTER_PR);
476 * Set the primary address.
478 eaddr = IF_LLADDR(ifp);
479 lo = eaddr[0] | (eaddr[1] << 8) | (eaddr[2] << 16) |
481 hi = eaddr[4] | (eaddr[5] << 8);
482 WRITE4(sc, MAC_ADDRESS_LOW(0), lo);
483 WRITE4(sc, MAC_ADDRESS_HIGH(0), hi);
484 WRITE4(sc, MAC_FRAME_FILTER, ffval);
485 if (sc->mactype != DWC_GMAC_EXT_DESC) {
486 WRITE4(sc, GMAC_MAC_HTLOW, ctx.hash[0]);
487 WRITE4(sc, GMAC_MAC_HTHIGH, ctx.hash[1]);
489 for (i = 0; i < nhash; i++)
490 WRITE4(sc, HASH_TABLE_REG(i), ctx.hash[i]);
495 dwc_setup_core(struct dwc_softc *sc)
499 DWC_ASSERT_LOCKED(sc);
502 reg = READ4(sc, MAC_CONFIGURATION);
503 reg |= (CONF_JD | CONF_ACS | CONF_BE);
504 WRITE4(sc, MAC_CONFIGURATION, reg);
508 dwc_enable_mac(struct dwc_softc *sc, bool enable)
512 DWC_ASSERT_LOCKED(sc);
513 reg = READ4(sc, MAC_CONFIGURATION);
515 reg |= CONF_TE | CONF_RE;
517 reg &= ~(CONF_TE | CONF_RE);
518 WRITE4(sc, MAC_CONFIGURATION, reg);
522 dwc_enable_csum_offload(struct dwc_softc *sc)
526 DWC_ASSERT_LOCKED(sc);
527 reg = READ4(sc, MAC_CONFIGURATION);
528 if ((if_getcapenable(sc->ifp) & IFCAP_RXCSUM) != 0)
532 WRITE4(sc, MAC_CONFIGURATION, reg);
536 dwc_get_hwaddr(struct dwc_softc *sc, uint8_t *hwaddr)
538 uint32_t hi, lo, rnd;
541 * Try to recover a MAC address from the running hardware. If there's
542 * something non-zero there, assume the bootloader did the right thing
545 * Otherwise, set the address to a convenient locally assigned address,
546 * 'bsd' + random 24 low-order bits. 'b' is 0x62, which has the locally
547 * assigned bit set, and the broadcast/multicast bit clear.
549 lo = READ4(sc, MAC_ADDRESS_LOW(0));
550 hi = READ4(sc, MAC_ADDRESS_HIGH(0)) & 0xffff;
551 if ((lo != 0xffffffff) || (hi != 0xffff)) {
552 hwaddr[0] = (lo >> 0) & 0xff;
553 hwaddr[1] = (lo >> 8) & 0xff;
554 hwaddr[2] = (lo >> 16) & 0xff;
555 hwaddr[3] = (lo >> 24) & 0xff;
556 hwaddr[4] = (hi >> 0) & 0xff;
557 hwaddr[5] = (hi >> 8) & 0xff;
559 rnd = arc4random() & 0x00ffffff;
563 hwaddr[3] = rnd >> 16;
564 hwaddr[4] = rnd >> 8;
565 hwaddr[5] = rnd >> 0;
574 dwc_init_dma(struct dwc_softc *sc)
578 DWC_ASSERT_LOCKED(sc);
580 /* Initializa DMA and enable transmitters */
581 reg = READ4(sc, OPERATION_MODE);
582 reg |= (MODE_TSF | MODE_OSF | MODE_FUF);
584 reg |= (MODE_RTC_LEV32 << MODE_RTC_SHIFT);
585 WRITE4(sc, OPERATION_MODE, reg);
587 WRITE4(sc, INTERRUPT_ENABLE, INT_EN_DEFAULT);
590 reg = READ4(sc, OPERATION_MODE);
591 reg |= (MODE_ST | MODE_SR);
592 WRITE4(sc, OPERATION_MODE, reg);
596 dwc_stop_dma(struct dwc_softc *sc)
600 DWC_ASSERT_LOCKED(sc);
603 reg = READ4(sc, OPERATION_MODE);
605 WRITE4(sc, OPERATION_MODE, reg);
608 reg = READ4(sc, OPERATION_MODE);
610 WRITE4(sc, OPERATION_MODE, reg);
613 reg = READ4(sc, OPERATION_MODE);
615 WRITE4(sc, OPERATION_MODE, reg);
618 static inline uint32_t
619 next_rxidx(struct dwc_softc *sc, uint32_t curidx)
622 return ((curidx + 1) % RX_DESC_COUNT);
625 static inline uint32_t
626 next_txidx(struct dwc_softc *sc, uint32_t curidx)
629 return ((curidx + 1) % TX_DESC_COUNT);
633 dwc_get1paddr(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
638 *(bus_addr_t *)arg = segs[0].ds_addr;
642 dwc_setup_txdesc(struct dwc_softc *sc, int idx, bus_addr_t paddr,
643 uint32_t len, uint32_t flags, bool first, bool last)
645 uint32_t desc0, desc1;
647 /* Addr/len 0 means we're clearing the descriptor after xmit done. */
648 if (paddr == 0 || len == 0) {
653 if (sc->mactype != DWC_GMAC_EXT_DESC) {
655 desc1 = NTDESC1_TCH | len | flags;
659 desc1 |= NTDESC1_LS | NTDESC1_IC;
661 desc0 = ETDESC0_TCH | flags;
665 desc0 |= ETDESC0_LS | ETDESC0_IC;
671 sc->txdesc_ring[idx].addr1 = (uint32_t)(paddr);
672 sc->txdesc_ring[idx].desc0 = desc0;
673 sc->txdesc_ring[idx].desc1 = desc1;
677 dwc_set_owner(struct dwc_softc *sc, int idx)
680 sc->txdesc_ring[idx].desc0 |= TDESC0_OWN;
685 dwc_setup_txbuf(struct dwc_softc *sc, int idx, struct mbuf **mp)
687 struct bus_dma_segment segs[TX_MAP_MAX_SEGS];
694 error = bus_dmamap_load_mbuf_sg(sc->txbuf_tag, sc->txbuf_map[idx].map,
695 *mp, segs, &nsegs, 0);
696 if (error == EFBIG) {
698 * The map may be partially mapped from the first call.
699 * Make sure to reset it.
701 bus_dmamap_unload(sc->txbuf_tag, sc->txbuf_map[idx].map);
702 if ((m = m_defrag(*mp, M_NOWAIT)) == NULL)
705 error = bus_dmamap_load_mbuf_sg(sc->txbuf_tag, sc->txbuf_map[idx].map,
706 *mp, segs, &nsegs, 0);
711 if (sc->tx_desccount + nsegs > TX_DESC_COUNT) {
712 bus_dmamap_unload(sc->txbuf_tag, sc->txbuf_map[idx].map);
718 if ((m->m_pkthdr.csum_flags & CSUM_IP) != 0) {
719 if ((m->m_pkthdr.csum_flags & (CSUM_TCP|CSUM_UDP)) != 0) {
720 if (sc->mactype != DWC_GMAC_EXT_DESC)
721 flags = NTDESC1_CIC_FULL;
723 flags = ETDESC0_CIC_FULL;
725 if (sc->mactype != DWC_GMAC_EXT_DESC)
726 flags = NTDESC1_CIC_HDR;
728 flags = ETDESC0_CIC_HDR;
732 bus_dmamap_sync(sc->txbuf_tag, sc->txbuf_map[idx].map,
733 BUS_DMASYNC_PREWRITE);
735 sc->txbuf_map[idx].mbuf = m;
737 first = sc->tx_desc_head;
738 for (i = 0; i < nsegs; i++) {
739 dwc_setup_txdesc(sc, sc->tx_desc_head,
740 segs[i].ds_addr, segs[i].ds_len,
741 (i == 0) ? flags : 0, /* only first desc needs flags */
745 dwc_set_owner(sc, sc->tx_desc_head);
746 last = sc->tx_desc_head;
747 sc->tx_desc_head = next_txidx(sc, sc->tx_desc_head);
750 sc->txbuf_map[idx].last_desc_idx = last;
752 dwc_set_owner(sc, first);
757 inline static uint32_t
758 dwc_setup_rxdesc(struct dwc_softc *sc, int idx, bus_addr_t paddr)
762 sc->rxdesc_ring[idx].addr1 = (uint32_t)paddr;
763 nidx = next_rxidx(sc, idx);
764 sc->rxdesc_ring[idx].addr2 = sc->rxdesc_ring_paddr +
765 (nidx * sizeof(struct dwc_hwdesc));
766 if (sc->mactype != DWC_GMAC_EXT_DESC)
767 sc->rxdesc_ring[idx].desc1 = NRDESC1_RCH |
768 MIN(MCLBYTES, NRDESC1_RBS1_MASK);
770 sc->rxdesc_ring[idx].desc1 = ERDESC1_RCH |
771 MIN(MCLBYTES, ERDESC1_RBS1_MASK);
774 sc->rxdesc_ring[idx].desc0 = RDESC0_OWN;
780 dwc_setup_rxbuf(struct dwc_softc *sc, int idx, struct mbuf *m)
782 struct bus_dma_segment seg;
785 m_adj(m, ETHER_ALIGN);
787 error = bus_dmamap_load_mbuf_sg(sc->rxbuf_tag, sc->rxbuf_map[idx].map,
792 KASSERT(nsegs == 1, ("%s: %d segments returned!", __func__, nsegs));
794 bus_dmamap_sync(sc->rxbuf_tag, sc->rxbuf_map[idx].map,
795 BUS_DMASYNC_PREREAD);
797 sc->rxbuf_map[idx].mbuf = m;
798 dwc_setup_rxdesc(sc, idx, seg.ds_addr);
804 dwc_alloc_mbufcl(struct dwc_softc *sc)
808 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
810 m->m_pkthdr.len = m->m_len = m->m_ext.ext_size;
816 dwc_rxfinish_one(struct dwc_softc *sc, struct dwc_hwdesc *desc,
817 struct dwc_bufmap *map)
826 rdesc0 = desc ->desc0;
828 if ((rdesc0 & (RDESC0_FS | RDESC0_LS)) !=
829 (RDESC0_FS | RDESC0_LS)) {
831 * Something very wrong happens. The whole packet should be
832 * recevied in one descriptr. Report problem.
834 device_printf(sc->dev,
835 "%s: RX descriptor without FIRST and LAST bit set: 0x%08X",
840 len = (rdesc0 >> RDESC0_FL_SHIFT) & RDESC0_FL_MASK;
843 * Lenght is invalid, recycle old mbuf
844 * Probably impossible case
849 /* Allocate new buffer */
850 m0 = dwc_alloc_mbufcl(sc);
852 /* no new mbuf available, recycle old */
853 if_inc_counter(sc->ifp, IFCOUNTER_IQDROPS, 1);
856 /* Do dmasync for newly received packet */
857 bus_dmamap_sync(sc->rxbuf_tag, map->map, BUS_DMASYNC_POSTREAD);
858 bus_dmamap_unload(sc->rxbuf_tag, map->map);
860 /* Received packet is valid, process it */
861 m->m_pkthdr.rcvif = ifp;
862 m->m_pkthdr.len = len;
864 if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
866 if ((if_getcapenable(ifp) & IFCAP_RXCSUM) != 0 &&
867 (rdesc0 & RDESC0_FT) != 0) {
868 m->m_pkthdr.csum_flags = CSUM_IP_CHECKED;
869 if ((rdesc0 & RDESC0_ICE) == 0)
870 m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
871 if ((rdesc0 & RDESC0_PCE) == 0) {
872 m->m_pkthdr.csum_flags |=
873 CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
874 m->m_pkthdr.csum_data = 0xffff;
878 /* Remove trailing FCS */
879 m_adj(m, -ETHER_CRC_LEN);
888 setup_dma(struct dwc_softc *sc)
896 * Set up TX descriptor ring, descriptors, and dma maps.
898 error = bus_dma_tag_create(
899 bus_get_dma_tag(sc->dev), /* Parent tag. */
900 DWC_DESC_RING_ALIGN, 0, /* alignment, boundary */
901 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
902 BUS_SPACE_MAXADDR, /* highaddr */
903 NULL, NULL, /* filter, filterarg */
904 TX_DESC_SIZE, 1, /* maxsize, nsegments */
905 TX_DESC_SIZE, /* maxsegsize */
907 NULL, NULL, /* lockfunc, lockarg */
910 device_printf(sc->dev,
911 "could not create TX ring DMA tag.\n");
915 error = bus_dmamem_alloc(sc->txdesc_tag, (void**)&sc->txdesc_ring,
916 BUS_DMA_COHERENT | BUS_DMA_WAITOK | BUS_DMA_ZERO,
919 device_printf(sc->dev,
920 "could not allocate TX descriptor ring.\n");
924 error = bus_dmamap_load(sc->txdesc_tag, sc->txdesc_map,
925 sc->txdesc_ring, TX_DESC_SIZE, dwc_get1paddr,
926 &sc->txdesc_ring_paddr, 0);
928 device_printf(sc->dev,
929 "could not load TX descriptor ring map.\n");
933 for (idx = 0; idx < TX_DESC_COUNT; idx++) {
934 nidx = next_txidx(sc, idx);
935 sc->txdesc_ring[idx].addr2 = sc->txdesc_ring_paddr +
936 (nidx * sizeof(struct dwc_hwdesc));
939 error = bus_dma_tag_create(
940 bus_get_dma_tag(sc->dev), /* Parent tag. */
941 1, 0, /* alignment, boundary */
942 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
943 BUS_SPACE_MAXADDR, /* highaddr */
944 NULL, NULL, /* filter, filterarg */
945 MCLBYTES*TX_MAP_MAX_SEGS, /* maxsize */
946 TX_MAP_MAX_SEGS, /* nsegments */
947 MCLBYTES, /* maxsegsize */
949 NULL, NULL, /* lockfunc, lockarg */
952 device_printf(sc->dev,
953 "could not create TX ring DMA tag.\n");
957 for (idx = 0; idx < TX_MAP_COUNT; idx++) {
958 error = bus_dmamap_create(sc->txbuf_tag, BUS_DMA_COHERENT,
959 &sc->txbuf_map[idx].map);
961 device_printf(sc->dev,
962 "could not create TX buffer DMA map.\n");
967 for (idx = 0; idx < TX_DESC_COUNT; idx++)
968 dwc_setup_txdesc(sc, idx, 0, 0, 0, false, false);
971 * Set up RX descriptor ring, descriptors, dma maps, and mbufs.
973 error = bus_dma_tag_create(
974 bus_get_dma_tag(sc->dev), /* Parent tag. */
975 DWC_DESC_RING_ALIGN, 0, /* alignment, boundary */
976 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
977 BUS_SPACE_MAXADDR, /* highaddr */
978 NULL, NULL, /* filter, filterarg */
979 RX_DESC_SIZE, 1, /* maxsize, nsegments */
980 RX_DESC_SIZE, /* maxsegsize */
982 NULL, NULL, /* lockfunc, lockarg */
985 device_printf(sc->dev,
986 "could not create RX ring DMA tag.\n");
990 error = bus_dmamem_alloc(sc->rxdesc_tag, (void **)&sc->rxdesc_ring,
991 BUS_DMA_COHERENT | BUS_DMA_WAITOK | BUS_DMA_ZERO,
994 device_printf(sc->dev,
995 "could not allocate RX descriptor ring.\n");
999 error = bus_dmamap_load(sc->rxdesc_tag, sc->rxdesc_map,
1000 sc->rxdesc_ring, RX_DESC_SIZE, dwc_get1paddr,
1001 &sc->rxdesc_ring_paddr, 0);
1003 device_printf(sc->dev,
1004 "could not load RX descriptor ring map.\n");
1008 error = bus_dma_tag_create(
1009 bus_get_dma_tag(sc->dev), /* Parent tag. */
1010 1, 0, /* alignment, boundary */
1011 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
1012 BUS_SPACE_MAXADDR, /* highaddr */
1013 NULL, NULL, /* filter, filterarg */
1014 MCLBYTES, 1, /* maxsize, nsegments */
1015 MCLBYTES, /* maxsegsize */
1017 NULL, NULL, /* lockfunc, lockarg */
1020 device_printf(sc->dev,
1021 "could not create RX buf DMA tag.\n");
1025 for (idx = 0; idx < RX_DESC_COUNT; idx++) {
1026 error = bus_dmamap_create(sc->rxbuf_tag, BUS_DMA_COHERENT,
1027 &sc->rxbuf_map[idx].map);
1029 device_printf(sc->dev,
1030 "could not create RX buffer DMA map.\n");
1033 if ((m = dwc_alloc_mbufcl(sc)) == NULL) {
1034 device_printf(sc->dev, "Could not alloc mbuf\n");
1038 if ((error = dwc_setup_rxbuf(sc, idx, m)) != 0) {
1039 device_printf(sc->dev,
1040 "could not create new RX buffer.\n");
1053 free_dma(struct dwc_softc *sc)
1058 /* Clean up RX DMA resources and free mbufs. */
1059 for (idx = 0; idx < RX_DESC_COUNT; ++idx) {
1060 if ((map = sc->rxbuf_map[idx].map) != NULL) {
1061 bus_dmamap_unload(sc->rxbuf_tag, map);
1062 bus_dmamap_destroy(sc->rxbuf_tag, map);
1063 m_freem(sc->rxbuf_map[idx].mbuf);
1066 if (sc->rxbuf_tag != NULL)
1067 bus_dma_tag_destroy(sc->rxbuf_tag);
1068 if (sc->rxdesc_map != NULL) {
1069 bus_dmamap_unload(sc->rxdesc_tag, sc->rxdesc_map);
1070 bus_dmamem_free(sc->rxdesc_tag, sc->rxdesc_ring,
1073 if (sc->rxdesc_tag != NULL)
1074 bus_dma_tag_destroy(sc->rxdesc_tag);
1076 /* Clean up TX DMA resources. */
1077 for (idx = 0; idx < TX_DESC_COUNT; ++idx) {
1078 if ((map = sc->txbuf_map[idx].map) != NULL) {
1079 /* TX maps are already unloaded. */
1080 bus_dmamap_destroy(sc->txbuf_tag, map);
1083 if (sc->txbuf_tag != NULL)
1084 bus_dma_tag_destroy(sc->txbuf_tag);
1085 if (sc->txdesc_map != NULL) {
1086 bus_dmamap_unload(sc->txdesc_tag, sc->txdesc_map);
1087 bus_dmamem_free(sc->txdesc_tag, sc->txdesc_ring,
1090 if (sc->txdesc_tag != NULL)
1091 bus_dma_tag_destroy(sc->txdesc_tag);
1099 dwc_txstart_locked(struct dwc_softc *sc)
1105 DWC_ASSERT_LOCKED(sc);
1107 if (!sc->link_is_up)
1112 if ((if_getdrvflags(ifp) & (IFF_DRV_RUNNING|IFF_DRV_OACTIVE)) !=
1119 if (sc->tx_desccount > (TX_DESC_COUNT - TX_MAP_MAX_SEGS + 1)) {
1120 if_setdrvflagbits(ifp, IFF_DRV_OACTIVE, 0);
1124 if (sc->tx_mapcount == (TX_MAP_COUNT - 1)) {
1125 if_setdrvflagbits(ifp, IFF_DRV_OACTIVE, 0);
1129 m = if_dequeue(ifp);
1132 if (dwc_setup_txbuf(sc, sc->tx_map_head, &m) != 0) {
1133 if_sendq_prepend(ifp, m);
1134 if_setdrvflagbits(ifp, IFF_DRV_OACTIVE, 0);
1138 sc->tx_map_head = next_txidx(sc, sc->tx_map_head);
1143 if (enqueued != 0) {
1144 WRITE4(sc, TRANSMIT_POLL_DEMAND, 0x1);
1145 sc->tx_watchdog_count = WATCHDOG_TIMEOUT_SECS;
1150 dwc_txstart(if_t ifp)
1152 struct dwc_softc *sc = if_getsoftc(ifp);
1155 dwc_txstart_locked(sc);
1160 dwc_init_locked(struct dwc_softc *sc)
1164 DWC_ASSERT_LOCKED(sc);
1166 if (if_getdrvflags(ifp) & IFF_DRV_RUNNING)
1169 dwc_setup_rxfilter(sc);
1171 dwc_enable_mac(sc, true);
1172 dwc_enable_csum_offload(sc);
1175 if_setdrvflagbits(ifp, IFF_DRV_RUNNING, IFF_DRV_OACTIVE);
1178 * Call mii_mediachg() which will call back into dwc_miibus_statchg()
1179 * to set up the remaining config registers based on current media.
1181 mii_mediachg(sc->mii_softc);
1182 callout_reset(&sc->dwc_callout, hz, dwc_tick, sc);
1186 dwc_init(void *if_softc)
1188 struct dwc_softc *sc = if_softc;
1191 dwc_init_locked(sc);
1196 dwc_stop_locked(struct dwc_softc *sc)
1200 DWC_ASSERT_LOCKED(sc);
1203 if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
1204 sc->tx_watchdog_count = 0;
1205 sc->stats_harvest_count = 0;
1207 callout_stop(&sc->dwc_callout);
1210 dwc_enable_mac(sc, false);
1214 dwc_ioctl(if_t ifp, u_long cmd, caddr_t data)
1216 struct dwc_softc *sc;
1217 struct mii_data *mii;
1219 int flags, mask, error;
1221 sc = if_getsoftc(ifp);
1222 ifr = (struct ifreq *)data;
1228 if (if_getflags(ifp) & IFF_UP) {
1229 if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
1230 flags = if_getflags(ifp) ^ sc->if_flags;
1231 if ((flags & (IFF_PROMISC|IFF_ALLMULTI)) != 0)
1232 dwc_setup_rxfilter(sc);
1234 if (!sc->is_detaching)
1235 dwc_init_locked(sc);
1238 if (if_getdrvflags(ifp) & IFF_DRV_RUNNING)
1239 dwc_stop_locked(sc);
1241 sc->if_flags = if_getflags(ifp);
1246 if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
1248 dwc_setup_rxfilter(sc);
1254 mii = sc->mii_softc;
1255 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, cmd);
1258 mask = ifr->ifr_reqcap ^ if_getcapenable(ifp);
1259 if (mask & IFCAP_VLAN_MTU) {
1260 /* No work to do except acknowledge the change took */
1261 if_togglecapenable(ifp, IFCAP_VLAN_MTU);
1263 if (mask & IFCAP_RXCSUM)
1264 if_togglecapenable(ifp, IFCAP_RXCSUM);
1265 if (mask & IFCAP_TXCSUM)
1266 if_togglecapenable(ifp, IFCAP_TXCSUM);
1267 if ((if_getcapenable(ifp) & IFCAP_TXCSUM) != 0)
1268 if_sethwassistbits(ifp, CSUM_IP | CSUM_UDP | CSUM_TCP, 0);
1270 if_sethwassistbits(ifp, 0, CSUM_IP | CSUM_UDP | CSUM_TCP);
1272 if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
1274 dwc_enable_csum_offload(sc);
1280 error = ether_ioctl(ifp, cmd, data);
1288 * Interrupts functions
1292 dwc_txfinish_locked(struct dwc_softc *sc)
1294 struct dwc_bufmap *bmap;
1295 struct dwc_hwdesc *desc;
1300 DWC_ASSERT_LOCKED(sc);
1303 /* check if all descriptors of the map are done */
1304 while (sc->tx_map_tail != sc->tx_map_head) {
1305 map_finished = true;
1306 bmap = &sc->txbuf_map[sc->tx_map_tail];
1307 idx = sc->tx_desc_tail;
1308 last_idx = next_txidx(sc, bmap->last_desc_idx);
1309 while (idx != last_idx) {
1310 desc = &sc->txdesc_ring[idx];
1311 if ((desc->desc0 & TDESC0_OWN) != 0) {
1312 map_finished = false;
1315 idx = next_txidx(sc, idx);
1320 bus_dmamap_sync(sc->txbuf_tag, bmap->map,
1321 BUS_DMASYNC_POSTWRITE);
1322 bus_dmamap_unload(sc->txbuf_tag, bmap->map);
1323 m_freem(bmap->mbuf);
1326 while (sc->tx_desc_tail != last_idx) {
1327 dwc_setup_txdesc(sc, sc->tx_desc_tail, 0, 0, 0, false, false);
1328 sc->tx_desc_tail = next_txidx(sc, sc->tx_desc_tail);
1330 sc->tx_map_tail = next_txidx(sc, sc->tx_map_tail);
1331 if_setdrvflagbits(ifp, 0, IFF_DRV_OACTIVE);
1332 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
1335 /* If there are no buffers outstanding, muzzle the watchdog. */
1336 if (sc->tx_desc_tail == sc->tx_desc_head) {
1337 sc->tx_watchdog_count = 0;
1342 dwc_rxfinish_locked(struct dwc_softc *sc)
1346 struct dwc_hwdesc *desc;
1348 DWC_ASSERT_LOCKED(sc);
1351 desc = sc->rxdesc_ring + idx;
1352 if ((desc->desc0 & RDESC0_OWN) != 0)
1355 m = dwc_rxfinish_one(sc, desc, sc->rxbuf_map + idx);
1358 desc->desc0 = RDESC0_OWN;
1361 /* We cannot create hole in RX ring */
1362 error = dwc_setup_rxbuf(sc, idx, m);
1364 panic("dwc_setup_rxbuf failed: error %d\n",
1368 sc->rx_idx = next_rxidx(sc, sc->rx_idx);
1375 struct dwc_softc *sc;
1382 reg = READ4(sc, INTERRUPT_STATUS);
1384 READ4(sc, SGMII_RGMII_SMII_CTRL_STATUS);
1386 reg = READ4(sc, DMA_STATUS);
1387 if (reg & DMA_STATUS_NIS) {
1388 if (reg & DMA_STATUS_RI)
1389 dwc_rxfinish_locked(sc);
1391 if (reg & DMA_STATUS_TI) {
1392 dwc_txfinish_locked(sc);
1393 dwc_txstart_locked(sc);
1397 if (reg & DMA_STATUS_AIS) {
1398 if (reg & DMA_STATUS_FBI) {
1399 /* Fatal bus error */
1400 device_printf(sc->dev,
1401 "Ethernet DMA error, restarting controller.\n");
1402 dwc_stop_locked(sc);
1403 dwc_init_locked(sc);
1407 WRITE4(sc, DMA_STATUS, reg & DMA_STATUS_INTR_MASK);
1415 static void dwc_clear_stats(struct dwc_softc *sc)
1419 reg = READ4(sc, MMC_CONTROL);
1420 reg |= (MMC_CONTROL_CNTRST);
1421 WRITE4(sc, MMC_CONTROL, reg);
1425 dwc_harvest_stats(struct dwc_softc *sc)
1429 /* We don't need to harvest too often. */
1430 if (++sc->stats_harvest_count < STATS_HARVEST_INTERVAL)
1433 sc->stats_harvest_count = 0;
1436 if_inc_counter(ifp, IFCOUNTER_IERRORS,
1437 READ4(sc, RXOVERSIZE_G) + READ4(sc, RXUNDERSIZE_G) +
1438 READ4(sc, RXCRCERROR) + READ4(sc, RXALIGNMENTERROR) +
1439 READ4(sc, RXRUNTERROR) + READ4(sc, RXJABBERERROR) +
1440 READ4(sc, RXLENGTHERROR));
1442 if_inc_counter(ifp, IFCOUNTER_OERRORS,
1443 READ4(sc, TXOVERSIZE_G) + READ4(sc, TXEXCESSDEF) +
1444 READ4(sc, TXCARRIERERR) + READ4(sc, TXUNDERFLOWERROR));
1446 if_inc_counter(ifp, IFCOUNTER_COLLISIONS,
1447 READ4(sc, TXEXESSCOL) + READ4(sc, TXLATECOL));
1449 dwc_clear_stats(sc);
1455 struct dwc_softc *sc;
1461 DWC_ASSERT_LOCKED(sc);
1465 if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) == 0)
1469 * Typical tx watchdog. If this fires it indicates that we enqueued
1470 * packets for output and never got a txdone interrupt for them. Maybe
1471 * it's a missed interrupt somehow, just pretend we got one.
1473 if (sc->tx_watchdog_count > 0) {
1474 if (--sc->tx_watchdog_count == 0) {
1475 dwc_txfinish_locked(sc);
1479 /* Gather stats from hardware counters. */
1480 dwc_harvest_stats(sc);
1482 /* Check the media status. */
1483 link_was_up = sc->link_is_up;
1484 mii_tick(sc->mii_softc);
1485 if (sc->link_is_up && !link_was_up)
1486 dwc_txstart_locked(sc);
1488 /* Schedule another check one second from now. */
1489 callout_reset(&sc->dwc_callout, hz, dwc_tick, sc);
1493 * Probe/Attach functions
1496 #define GPIO_ACTIVE_LOW 1
1499 dwc_reset(device_t dev)
1501 pcell_t gpio_prop[4];
1502 pcell_t delay_prop[3];
1503 phandle_t node, gpio_node;
1505 uint32_t pin, flags;
1508 node = ofw_bus_get_node(dev);
1509 if (OF_getencprop(node, "snps,reset-gpio",
1510 gpio_prop, sizeof(gpio_prop)) <= 0)
1513 if (OF_getencprop(node, "snps,reset-delays-us",
1514 delay_prop, sizeof(delay_prop)) <= 0) {
1516 "Wrong property for snps,reset-delays-us");
1520 gpio_node = OF_node_from_xref(gpio_prop[0]);
1521 if ((gpio = OF_device_from_xref(gpio_prop[0])) == NULL) {
1523 "Can't find gpio controller for phy reset\n");
1527 if (GPIO_MAP_GPIOS(gpio, node, gpio_node,
1528 nitems(gpio_prop) - 1,
1529 gpio_prop + 1, &pin, &flags) != 0) {
1530 device_printf(dev, "Can't map gpio for phy reset\n");
1534 pin_value = GPIO_PIN_LOW;
1535 if (OF_hasprop(node, "snps,reset-active-low"))
1536 pin_value = GPIO_PIN_HIGH;
1538 GPIO_PIN_SETFLAGS(gpio, pin, GPIO_PIN_OUTPUT);
1539 GPIO_PIN_SET(gpio, pin, pin_value);
1540 DELAY(delay_prop[0] * 5);
1541 GPIO_PIN_SET(gpio, pin, !pin_value);
1542 DELAY(delay_prop[1] * 5);
1543 GPIO_PIN_SET(gpio, pin, pin_value);
1544 DELAY(delay_prop[2] * 5);
1550 dwc_clock_init(device_t dev)
1558 if (clk_get_by_ofw_name(dev, 0, "stmmaceth", &clk) == 0) {
1559 error = clk_enable(clk);
1561 device_printf(dev, "could not enable main clock\n");
1565 clk_get_freq(clk, &freq);
1566 device_printf(dev, "MAC clock(%s) freq: %jd\n",
1567 clk_get_name(clk), (intmax_t)freq);
1571 device_printf(dev, "could not find clock stmmaceth\n");
1574 /* De-assert reset */
1575 if (hwreset_get_by_ofw_name(dev, 0, "stmmaceth", &rst) == 0) {
1576 error = hwreset_deassert(rst);
1578 device_printf(dev, "could not de-assert reset\n");
1587 dwc_probe(device_t dev)
1590 if (!ofw_bus_status_okay(dev))
1593 if (!ofw_bus_is_compatible(dev, "snps,dwmac"))
1596 device_set_desc(dev, "Gigabit Ethernet Controller");
1597 return (BUS_PROBE_DEFAULT);
1601 dwc_attach(device_t dev)
1603 uint8_t macaddr[ETHER_ADDR_LEN];
1604 struct dwc_softc *sc;
1609 uint32_t txpbl, rxpbl, pbl;
1610 bool nopblx8 = false;
1611 bool fixed_burst = false;
1613 sc = device_get_softc(dev);
1616 sc->tx_desccount = TX_DESC_COUNT;
1617 sc->tx_mapcount = 0;
1618 sc->mii_clk = IF_DWC_MII_CLK(dev);
1619 sc->mactype = IF_DWC_MAC_TYPE(dev);
1621 node = ofw_bus_get_node(dev);
1622 switch (mii_fdt_get_contype(node)) {
1623 case MII_CONTYPE_RGMII:
1624 case MII_CONTYPE_RGMII_ID:
1625 case MII_CONTYPE_RGMII_RXID:
1626 case MII_CONTYPE_RGMII_TXID:
1627 sc->phy_mode = PHY_MODE_RGMII;
1629 case MII_CONTYPE_RMII:
1630 sc->phy_mode = PHY_MODE_RMII;
1632 case MII_CONTYPE_MII:
1633 sc->phy_mode = PHY_MODE_MII;
1636 device_printf(dev, "Unsupported MII type\n");
1640 if (OF_getencprop(node, "snps,pbl", &pbl, sizeof(uint32_t)) <= 0)
1641 pbl = BUS_MODE_DEFAULT_PBL;
1642 if (OF_getencprop(node, "snps,txpbl", &txpbl, sizeof(uint32_t)) <= 0)
1644 if (OF_getencprop(node, "snps,rxpbl", &rxpbl, sizeof(uint32_t)) <= 0)
1646 if (OF_hasprop(node, "snps,no-pbl-x8") == 1)
1648 if (OF_hasprop(node, "snps,fixed-burst") == 1)
1651 if (IF_DWC_INIT(dev) != 0)
1654 if (dwc_clock_init(dev) != 0)
1657 if (bus_alloc_resources(dev, dwc_spec, sc->res)) {
1658 device_printf(dev, "could not allocate resources\n");
1662 /* Read MAC before reset */
1663 dwc_get_hwaddr(sc, macaddr);
1665 /* Reset the PHY if needed */
1666 if (dwc_reset(dev) != 0) {
1667 device_printf(dev, "Can't reset the PHY\n");
1668 bus_release_resources(dev, dwc_spec, sc->res);
1673 reg = READ4(sc, BUS_MODE);
1674 reg |= (BUS_MODE_SWR);
1675 WRITE4(sc, BUS_MODE, reg);
1677 for (i = 0; i < MAC_RESET_TIMEOUT; i++) {
1678 if ((READ4(sc, BUS_MODE) & BUS_MODE_SWR) == 0)
1682 if (i >= MAC_RESET_TIMEOUT) {
1683 device_printf(sc->dev, "Can't reset DWC.\n");
1684 bus_release_resources(dev, dwc_spec, sc->res);
1690 reg |= BUS_MODE_EIGHTXPBL;
1691 reg |= (txpbl << BUS_MODE_PBL_SHIFT);
1692 reg |= (rxpbl << BUS_MODE_RPBL_SHIFT);
1694 reg |= BUS_MODE_FIXEDBURST;
1696 WRITE4(sc, BUS_MODE, reg);
1699 * DMA must be stop while changing descriptor list addresses.
1701 reg = READ4(sc, OPERATION_MODE);
1702 reg &= ~(MODE_ST | MODE_SR);
1703 WRITE4(sc, OPERATION_MODE, reg);
1705 if (setup_dma(sc)) {
1706 bus_release_resources(dev, dwc_spec, sc->res);
1710 /* Setup addresses */
1711 WRITE4(sc, RX_DESCR_LIST_ADDR, sc->rxdesc_ring_paddr);
1712 WRITE4(sc, TX_DESCR_LIST_ADDR, sc->txdesc_ring_paddr);
1714 mtx_init(&sc->mtx, device_get_nameunit(sc->dev),
1715 MTX_NETWORK_LOCK, MTX_DEF);
1717 callout_init_mtx(&sc->dwc_callout, &sc->mtx, 0);
1719 /* Setup interrupt handler. */
1720 error = bus_setup_intr(dev, sc->res[1], INTR_TYPE_NET | INTR_MPSAFE,
1721 NULL, dwc_intr, sc, &sc->intr_cookie);
1723 device_printf(dev, "could not setup interrupt handler.\n");
1724 bus_release_resources(dev, dwc_spec, sc->res);
1728 /* Set up the ethernet interface. */
1729 sc->ifp = ifp = if_alloc(IFT_ETHER);
1731 if_setsoftc(ifp, sc);
1732 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
1733 if_setflags(sc->ifp, IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST);
1734 if_setstartfn(ifp, dwc_txstart);
1735 if_setioctlfn(ifp, dwc_ioctl);
1736 if_setinitfn(ifp, dwc_init);
1737 if_setsendqlen(ifp, TX_MAP_COUNT - 1);
1738 if_setsendqready(sc->ifp);
1739 if_sethwassist(sc->ifp, CSUM_IP | CSUM_UDP | CSUM_TCP);
1740 if_setcapabilities(sc->ifp, IFCAP_VLAN_MTU | IFCAP_HWCSUM);
1741 if_setcapenable(sc->ifp, if_getcapabilities(sc->ifp));
1743 /* Attach the mii driver. */
1744 error = mii_attach(dev, &sc->miibus, ifp, dwc_media_change,
1745 dwc_media_status, BMSR_DEFCAPMASK, MII_PHY_ANY,
1749 device_printf(dev, "PHY attach failed\n");
1750 bus_teardown_intr(dev, sc->res[1], sc->intr_cookie);
1751 bus_release_resources(dev, dwc_spec, sc->res);
1754 sc->mii_softc = device_get_softc(sc->miibus);
1756 /* All ready to run, attach the ethernet interface. */
1757 ether_ifattach(ifp, macaddr);
1758 sc->is_attached = true;
1764 dwc_detach(device_t dev)
1766 struct dwc_softc *sc;
1768 sc = device_get_softc(dev);
1771 * Disable and tear down interrupts before anything else, so we don't
1772 * race with the handler.
1774 WRITE4(sc, INTERRUPT_ENABLE, 0);
1775 if (sc->intr_cookie != NULL) {
1776 bus_teardown_intr(dev, sc->res[1], sc->intr_cookie);
1779 if (sc->is_attached) {
1781 sc->is_detaching = true;
1782 dwc_stop_locked(sc);
1784 callout_drain(&sc->dwc_callout);
1785 ether_ifdetach(sc->ifp);
1788 if (sc->miibus != NULL) {
1789 device_delete_child(dev, sc->miibus);
1792 bus_generic_detach(dev);
1794 /* Free DMA descriptors */
1797 if (sc->ifp != NULL) {
1802 bus_release_resources(dev, dwc_spec, sc->res);
1804 mtx_destroy(&sc->mtx);
1808 static device_method_t dwc_methods[] = {
1809 DEVMETHOD(device_probe, dwc_probe),
1810 DEVMETHOD(device_attach, dwc_attach),
1811 DEVMETHOD(device_detach, dwc_detach),
1814 DEVMETHOD(miibus_readreg, dwc_miibus_read_reg),
1815 DEVMETHOD(miibus_writereg, dwc_miibus_write_reg),
1816 DEVMETHOD(miibus_statchg, dwc_miibus_statchg),
1821 driver_t dwc_driver = {
1824 sizeof(struct dwc_softc),
1827 DRIVER_MODULE(dwc, simplebus, dwc_driver, 0, 0);
1828 DRIVER_MODULE(miibus, dwc, miibus_driver, 0, 0);
1830 MODULE_DEPEND(dwc, ether, 1, 1, 1);
1831 MODULE_DEPEND(dwc, miibus, 1, 1, 1);