2 * Copyright (c) 2016 Jared McNeill <jmcneill@invisible.ca>
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
18 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
20 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
21 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * Allwinner Gigabit Ethernet MAC (EMAC) controller
32 #include "opt_device_polling.h"
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 #include <sys/param.h>
38 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/endian.h>
44 #include <sys/socket.h>
45 #include <sys/sockio.h>
46 #include <sys/module.h>
47 #include <sys/taskqueue.h>
52 #include <net/ethernet.h>
53 #include <net/if_dl.h>
54 #include <net/if_media.h>
55 #include <net/if_types.h>
56 #include <net/if_var.h>
58 #include <machine/bus.h>
60 #include <dev/ofw/ofw_bus.h>
61 #include <dev/ofw/ofw_bus_subr.h>
63 #include <arm/allwinner/if_awgreg.h>
64 #include <arm/allwinner/aw_sid.h>
65 #include <dev/mii/mii.h>
66 #include <dev/mii/miivar.h>
68 #include <dev/extres/clk/clk.h>
69 #include <dev/extres/hwreset/hwreset.h>
70 #include <dev/extres/regulator/regulator.h>
71 #include <dev/extres/syscon/syscon.h>
73 #include "syscon_if.h"
74 #include "miibus_if.h"
77 #define RD4(sc, reg) bus_read_4((sc)->res[_RES_EMAC], (reg))
78 #define WR4(sc, reg, val) bus_write_4((sc)->res[_RES_EMAC], (reg), (val))
80 #define AWG_LOCK(sc) mtx_lock(&(sc)->mtx)
81 #define AWG_UNLOCK(sc) mtx_unlock(&(sc)->mtx);
82 #define AWG_ASSERT_LOCKED(sc) mtx_assert(&(sc)->mtx, MA_OWNED)
83 #define AWG_ASSERT_UNLOCKED(sc) mtx_assert(&(sc)->mtx, MA_NOTOWNED)
86 #define TX_DESC_COUNT 1024
87 #define TX_DESC_SIZE (sizeof(struct emac_desc) * TX_DESC_COUNT)
88 #define RX_DESC_COUNT 256
89 #define RX_DESC_SIZE (sizeof(struct emac_desc) * RX_DESC_COUNT)
91 #define DESC_OFF(n) ((n) * sizeof(struct emac_desc))
92 #define TX_NEXT(n) (((n) + 1) & (TX_DESC_COUNT - 1))
93 #define TX_SKIP(n, o) (((n) + (o)) & (TX_DESC_COUNT - 1))
94 #define RX_NEXT(n) (((n) + 1) & (RX_DESC_COUNT - 1))
96 #define TX_MAX_SEGS 20
98 #define SOFT_RST_RETRY 1000
99 #define MII_BUSY_RETRY 1000
100 #define MDIO_FREQ 2500000
102 #define BURST_LEN_DEFAULT 8
103 #define RX_TX_PRI_DEFAULT 0
104 #define PAUSE_TIME_DEFAULT 0x400
105 #define TX_INTERVAL_DEFAULT 64
106 #define RX_BATCH_DEFAULT 64
108 /* syscon EMAC clock register */
109 #define EMAC_CLK_REG 0x30
110 #define EMAC_CLK_EPHY_ADDR (0x1f << 20) /* H3 */
111 #define EMAC_CLK_EPHY_ADDR_SHIFT 20
112 #define EMAC_CLK_EPHY_LED_POL (1 << 17) /* H3 */
113 #define EMAC_CLK_EPHY_SHUTDOWN (1 << 16) /* H3 */
114 #define EMAC_CLK_EPHY_SELECT (1 << 15) /* H3 */
115 #define EMAC_CLK_RMII_EN (1 << 13)
116 #define EMAC_CLK_ETXDC (0x7 << 10)
117 #define EMAC_CLK_ETXDC_SHIFT 10
118 #define EMAC_CLK_ERXDC (0x1f << 5)
119 #define EMAC_CLK_ERXDC_SHIFT 5
120 #define EMAC_CLK_PIT (0x1 << 2)
121 #define EMAC_CLK_PIT_MII (0 << 2)
122 #define EMAC_CLK_PIT_RGMII (1 << 2)
123 #define EMAC_CLK_SRC (0x3 << 0)
124 #define EMAC_CLK_SRC_MII (0 << 0)
125 #define EMAC_CLK_SRC_EXT_RGMII (1 << 0)
126 #define EMAC_CLK_SRC_RGMII (2 << 0)
128 /* Burst length of RX and TX DMA transfers */
129 static int awg_burst_len = BURST_LEN_DEFAULT;
130 TUNABLE_INT("hw.awg.burst_len", &awg_burst_len);
132 /* RX / TX DMA priority. If 1, RX DMA has priority over TX DMA. */
133 static int awg_rx_tx_pri = RX_TX_PRI_DEFAULT;
134 TUNABLE_INT("hw.awg.rx_tx_pri", &awg_rx_tx_pri);
136 /* Pause time field in the transmitted control frame */
137 static int awg_pause_time = PAUSE_TIME_DEFAULT;
138 TUNABLE_INT("hw.awg.pause_time", &awg_pause_time);
140 /* Request a TX interrupt every <n> descriptors */
141 static int awg_tx_interval = TX_INTERVAL_DEFAULT;
142 TUNABLE_INT("hw.awg.tx_interval", &awg_tx_interval);
144 /* Maximum number of mbufs to send to if_input */
145 static int awg_rx_batch = RX_BATCH_DEFAULT;
146 TUNABLE_INT("hw.awg.rx_batch", &awg_rx_batch);
154 static struct ofw_compat_data compat_data[] = {
155 { "allwinner,sun8i-a83t-emac", EMAC_A83T },
156 { "allwinner,sun8i-h3-emac", EMAC_H3 },
157 { "allwinner,sun50i-a64-emac", EMAC_A64 },
167 bus_dma_tag_t desc_tag;
168 bus_dmamap_t desc_map;
169 struct emac_desc *desc_ring;
170 bus_addr_t desc_ring_paddr;
171 bus_dma_tag_t buf_tag;
172 struct awg_bufmap buf_map[TX_DESC_COUNT];
173 u_int cur, next, queued;
178 bus_dma_tag_t desc_tag;
179 bus_dmamap_t desc_map;
180 struct emac_desc *desc_ring;
181 bus_addr_t desc_ring_paddr;
182 bus_dma_tag_t buf_tag;
183 struct awg_bufmap buf_map[RX_DESC_COUNT];
184 bus_dmamap_t buf_spare_map;
196 struct resource *res[_RES_NITEMS];
201 struct callout stat_ch;
202 struct task link_task;
204 u_int mdc_div_ratio_m;
208 struct syscon *syscon;
210 struct awg_txring tx;
211 struct awg_rxring rx;
214 static struct resource_spec awg_spec[] = {
215 { SYS_RES_MEMORY, 0, RF_ACTIVE },
216 { SYS_RES_IRQ, 0, RF_ACTIVE },
217 { SYS_RES_MEMORY, 1, RF_ACTIVE | RF_OPTIONAL },
221 static void awg_txeof(struct awg_softc *sc);
223 static int awg_parse_delay(device_t dev, uint32_t *tx_delay,
225 static uint32_t syscon_read_emac_clk_reg(device_t dev);
226 static void syscon_write_emac_clk_reg(device_t dev, uint32_t val);
227 static phandle_t awg_get_phy_node(device_t dev);
228 static bool awg_has_internal_phy(device_t dev);
231 awg_miibus_readreg(device_t dev, int phy, int reg)
233 struct awg_softc *sc;
236 sc = device_get_softc(dev);
239 WR4(sc, EMAC_MII_CMD,
240 (sc->mdc_div_ratio_m << MDC_DIV_RATIO_M_SHIFT) |
241 (phy << PHY_ADDR_SHIFT) |
242 (reg << PHY_REG_ADDR_SHIFT) |
244 for (retry = MII_BUSY_RETRY; retry > 0; retry--) {
245 if ((RD4(sc, EMAC_MII_CMD) & MII_BUSY) == 0) {
246 val = RD4(sc, EMAC_MII_DATA);
253 device_printf(dev, "phy read timeout, phy=%d reg=%d\n",
260 awg_miibus_writereg(device_t dev, int phy, int reg, int val)
262 struct awg_softc *sc;
265 sc = device_get_softc(dev);
267 WR4(sc, EMAC_MII_DATA, val);
268 WR4(sc, EMAC_MII_CMD,
269 (sc->mdc_div_ratio_m << MDC_DIV_RATIO_M_SHIFT) |
270 (phy << PHY_ADDR_SHIFT) |
271 (reg << PHY_REG_ADDR_SHIFT) |
273 for (retry = MII_BUSY_RETRY; retry > 0; retry--) {
274 if ((RD4(sc, EMAC_MII_CMD) & MII_BUSY) == 0)
280 device_printf(dev, "phy write timeout, phy=%d reg=%d\n",
287 awg_update_link_locked(struct awg_softc *sc)
289 struct mii_data *mii;
292 AWG_ASSERT_LOCKED(sc);
294 if ((if_getdrvflags(sc->ifp) & IFF_DRV_RUNNING) == 0)
296 mii = device_get_softc(sc->miibus);
298 if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
299 (IFM_ACTIVE | IFM_AVALID)) {
300 switch (IFM_SUBTYPE(mii->mii_media_active)) {
317 val = RD4(sc, EMAC_BASIC_CTL_0);
318 val &= ~(BASIC_CTL_SPEED | BASIC_CTL_DUPLEX);
320 if (IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_T ||
321 IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_SX)
322 val |= BASIC_CTL_SPEED_1000 << BASIC_CTL_SPEED_SHIFT;
323 else if (IFM_SUBTYPE(mii->mii_media_active) == IFM_100_TX)
324 val |= BASIC_CTL_SPEED_100 << BASIC_CTL_SPEED_SHIFT;
326 val |= BASIC_CTL_SPEED_10 << BASIC_CTL_SPEED_SHIFT;
328 if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) != 0)
329 val |= BASIC_CTL_DUPLEX;
331 WR4(sc, EMAC_BASIC_CTL_0, val);
333 val = RD4(sc, EMAC_RX_CTL_0);
334 val &= ~RX_FLOW_CTL_EN;
335 if ((IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_RXPAUSE) != 0)
336 val |= RX_FLOW_CTL_EN;
337 WR4(sc, EMAC_RX_CTL_0, val);
339 val = RD4(sc, EMAC_TX_FLOW_CTL);
340 val &= ~(PAUSE_TIME|TX_FLOW_CTL_EN);
341 if ((IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_TXPAUSE) != 0)
342 val |= TX_FLOW_CTL_EN;
343 if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) != 0)
344 val |= awg_pause_time << PAUSE_TIME_SHIFT;
345 WR4(sc, EMAC_TX_FLOW_CTL, val);
349 awg_link_task(void *arg, int pending)
351 struct awg_softc *sc;
356 awg_update_link_locked(sc);
361 awg_miibus_statchg(device_t dev)
363 struct awg_softc *sc;
365 sc = device_get_softc(dev);
367 taskqueue_enqueue(taskqueue_swi, &sc->link_task);
371 awg_media_status(if_t ifp, struct ifmediareq *ifmr)
373 struct awg_softc *sc;
374 struct mii_data *mii;
376 sc = if_getsoftc(ifp);
377 mii = device_get_softc(sc->miibus);
381 ifmr->ifm_active = mii->mii_media_active;
382 ifmr->ifm_status = mii->mii_media_status;
387 awg_media_change(if_t ifp)
389 struct awg_softc *sc;
390 struct mii_data *mii;
393 sc = if_getsoftc(ifp);
394 mii = device_get_softc(sc->miibus);
397 error = mii_mediachg(mii);
404 awg_encap(struct awg_softc *sc, struct mbuf **mp)
407 bus_dma_segment_t segs[TX_MAX_SEGS];
408 int error, nsegs, cur, first, last, i;
410 uint32_t flags, status;
413 cur = first = sc->tx.cur;
414 map = sc->tx.buf_map[first].map;
417 error = bus_dmamap_load_mbuf_sg(sc->tx.buf_tag, map, m, segs,
418 &nsegs, BUS_DMA_NOWAIT);
419 if (error == EFBIG) {
420 m = m_collapse(m, M_NOWAIT, TX_MAX_SEGS);
422 device_printf(sc->dev, "awg_encap: m_collapse failed\n");
428 error = bus_dmamap_load_mbuf_sg(sc->tx.buf_tag, map, m,
429 segs, &nsegs, BUS_DMA_NOWAIT);
436 device_printf(sc->dev, "awg_encap: bus_dmamap_load_mbuf_sg failed\n");
445 if (sc->tx.queued + nsegs > TX_DESC_COUNT) {
446 bus_dmamap_unload(sc->tx.buf_tag, map);
450 bus_dmamap_sync(sc->tx.buf_tag, map, BUS_DMASYNC_PREWRITE);
454 if ((m->m_pkthdr.csum_flags & CSUM_IP) != 0) {
455 if ((m->m_pkthdr.csum_flags & (CSUM_TCP|CSUM_UDP)) != 0)
456 csum_flags = TX_CHECKSUM_CTL_FULL;
458 csum_flags = TX_CHECKSUM_CTL_IP;
459 flags |= (csum_flags << TX_CHECKSUM_CTL_SHIFT);
462 for (i = 0; i < nsegs; i++) {
464 if (i == nsegs - 1) {
465 flags |= TX_LAST_DESC;
467 * Can only request TX completion
468 * interrupt on last descriptor.
470 if (sc->tx.segs >= awg_tx_interval) {
476 sc->tx.desc_ring[cur].addr = htole32((uint32_t)segs[i].ds_addr);
477 sc->tx.desc_ring[cur].size = htole32(flags | segs[i].ds_len);
478 sc->tx.desc_ring[cur].status = htole32(status);
480 flags &= ~TX_FIR_DESC;
482 * Setting of the valid bit in the first descriptor is
483 * deferred until the whole chain is fully set up.
485 status = TX_DESC_CTL;
493 /* Store mapping and mbuf in the last segment */
494 last = TX_SKIP(cur, TX_DESC_COUNT - 1);
495 sc->tx.buf_map[first].map = sc->tx.buf_map[last].map;
496 sc->tx.buf_map[last].map = map;
497 sc->tx.buf_map[last].mbuf = m;
500 * The whole mbuf chain has been DMA mapped,
501 * fix the first descriptor.
503 sc->tx.desc_ring[first].status = htole32(TX_DESC_CTL);
509 awg_clean_txbuf(struct awg_softc *sc, int index)
511 struct awg_bufmap *bmap;
515 bmap = &sc->tx.buf_map[index];
516 if (bmap->mbuf != NULL) {
517 bus_dmamap_sync(sc->tx.buf_tag, bmap->map,
518 BUS_DMASYNC_POSTWRITE);
519 bus_dmamap_unload(sc->tx.buf_tag, bmap->map);
526 awg_setup_rxdesc(struct awg_softc *sc, int index, bus_addr_t paddr)
528 uint32_t status, size;
530 status = RX_DESC_CTL;
533 sc->rx.desc_ring[index].addr = htole32((uint32_t)paddr);
534 sc->rx.desc_ring[index].size = htole32(size);
535 sc->rx.desc_ring[index].status = htole32(status);
539 awg_reuse_rxdesc(struct awg_softc *sc, int index)
542 sc->rx.desc_ring[index].status = htole32(RX_DESC_CTL);
546 awg_newbuf_rx(struct awg_softc *sc, int index)
549 bus_dma_segment_t seg;
553 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
557 m->m_pkthdr.len = m->m_len = m->m_ext.ext_size;
558 m_adj(m, ETHER_ALIGN);
560 if (bus_dmamap_load_mbuf_sg(sc->rx.buf_tag, sc->rx.buf_spare_map,
561 m, &seg, &nsegs, BUS_DMA_NOWAIT) != 0) {
566 if (sc->rx.buf_map[index].mbuf != NULL) {
567 bus_dmamap_sync(sc->rx.buf_tag, sc->rx.buf_map[index].map,
568 BUS_DMASYNC_POSTREAD);
569 bus_dmamap_unload(sc->rx.buf_tag, sc->rx.buf_map[index].map);
571 map = sc->rx.buf_map[index].map;
572 sc->rx.buf_map[index].map = sc->rx.buf_spare_map;
573 sc->rx.buf_spare_map = map;
574 bus_dmamap_sync(sc->rx.buf_tag, sc->rx.buf_map[index].map,
575 BUS_DMASYNC_PREREAD);
577 sc->rx.buf_map[index].mbuf = m;
578 awg_setup_rxdesc(sc, index, seg.ds_addr);
584 awg_start_locked(struct awg_softc *sc)
591 AWG_ASSERT_LOCKED(sc);
598 if ((if_getdrvflags(ifp) & (IFF_DRV_RUNNING|IFF_DRV_OACTIVE)) !=
602 for (cnt = 0; ; cnt++) {
607 err = awg_encap(sc, &m);
610 if_setdrvflagbits(ifp, IFF_DRV_OACTIVE, 0);
612 if_sendq_prepend(ifp, m);
619 bus_dmamap_sync(sc->tx.desc_tag, sc->tx.desc_map,
620 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
622 /* Start and run TX DMA */
623 val = RD4(sc, EMAC_TX_CTL_1);
624 WR4(sc, EMAC_TX_CTL_1, val | TX_DMA_START);
631 struct awg_softc *sc;
633 sc = if_getsoftc(ifp);
636 awg_start_locked(sc);
641 awg_tick(void *softc)
643 struct awg_softc *sc;
644 struct mii_data *mii;
650 mii = device_get_softc(sc->miibus);
652 AWG_ASSERT_LOCKED(sc);
654 if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) == 0)
659 if (sc->link && !link)
660 awg_start_locked(sc);
662 callout_reset(&sc->stat_ch, hz, awg_tick, sc);
665 /* Bit Reversal - http://aggregate.org/MAGIC/#Bit%20Reversal */
669 x = (((x & 0xaaaaaaaa) >> 1) | ((x & 0x55555555) << 1));
670 x = (((x & 0xcccccccc) >> 2) | ((x & 0x33333333) << 2));
671 x = (((x & 0xf0f0f0f0) >> 4) | ((x & 0x0f0f0f0f) << 4));
672 x = (((x & 0xff00ff00) >> 8) | ((x & 0x00ff00ff) << 8));
674 return (x >> 16) | (x << 16);
678 awg_hash_maddr(void *arg, struct sockaddr_dl *sdl, u_int cnt)
680 uint32_t crc, hashreg, hashbit, *hash = arg;
682 crc = ether_crc32_le(LLADDR(sdl), ETHER_ADDR_LEN) & 0x7f;
683 crc = bitrev32(~crc) >> 26;
684 hashreg = (crc >> 5);
685 hashbit = (crc & 0x1f);
686 hash[hashreg] |= (1 << hashbit);
692 awg_setup_rxfilter(struct awg_softc *sc)
694 uint32_t val, hash[2], machi, maclo;
698 AWG_ASSERT_LOCKED(sc);
702 hash[0] = hash[1] = 0;
704 if (if_getflags(ifp) & IFF_PROMISC)
705 val |= DIS_ADDR_FILTER;
706 else if (if_getflags(ifp) & IFF_ALLMULTI) {
707 val |= RX_ALL_MULTICAST;
708 hash[0] = hash[1] = ~0;
709 } else if (if_foreach_llmaddr(ifp, awg_hash_maddr, hash) > 0)
710 val |= HASH_MULTICAST;
712 /* Write our unicast address */
713 eaddr = IF_LLADDR(ifp);
714 machi = (eaddr[5] << 8) | eaddr[4];
715 maclo = (eaddr[3] << 24) | (eaddr[2] << 16) | (eaddr[1] << 8) |
717 WR4(sc, EMAC_ADDR_HIGH(0), machi);
718 WR4(sc, EMAC_ADDR_LOW(0), maclo);
720 /* Multicast hash filters */
721 WR4(sc, EMAC_RX_HASH_0, hash[1]);
722 WR4(sc, EMAC_RX_HASH_1, hash[0]);
724 /* RX frame filter config */
725 WR4(sc, EMAC_RX_FRM_FLT, val);
729 awg_enable_intr(struct awg_softc *sc)
731 /* Enable interrupts */
732 WR4(sc, EMAC_INT_EN, RX_INT_EN | TX_INT_EN | TX_BUF_UA_INT_EN);
736 awg_disable_intr(struct awg_softc *sc)
738 /* Disable interrupts */
739 WR4(sc, EMAC_INT_EN, 0);
743 awg_init_locked(struct awg_softc *sc)
745 struct mii_data *mii;
749 mii = device_get_softc(sc->miibus);
752 AWG_ASSERT_LOCKED(sc);
754 if (if_getdrvflags(ifp) & IFF_DRV_RUNNING)
757 awg_setup_rxfilter(sc);
759 /* Configure DMA burst length and priorities */
760 val = awg_burst_len << BASIC_CTL_BURST_LEN_SHIFT;
762 val |= BASIC_CTL_RX_TX_PRI;
763 WR4(sc, EMAC_BASIC_CTL_1, val);
765 /* Enable interrupts */
766 #ifdef DEVICE_POLLING
767 if ((if_getcapenable(ifp) & IFCAP_POLLING) == 0)
770 awg_disable_intr(sc);
775 /* Enable transmit DMA */
776 val = RD4(sc, EMAC_TX_CTL_1);
777 WR4(sc, EMAC_TX_CTL_1, val | TX_DMA_EN | TX_MD | TX_NEXT_FRAME);
779 /* Enable receive DMA */
780 val = RD4(sc, EMAC_RX_CTL_1);
781 WR4(sc, EMAC_RX_CTL_1, val | RX_DMA_EN | RX_MD);
783 /* Enable transmitter */
784 val = RD4(sc, EMAC_TX_CTL_0);
785 WR4(sc, EMAC_TX_CTL_0, val | TX_EN);
787 /* Enable receiver */
788 val = RD4(sc, EMAC_RX_CTL_0);
789 WR4(sc, EMAC_RX_CTL_0, val | RX_EN | CHECK_CRC);
791 if_setdrvflagbits(ifp, IFF_DRV_RUNNING, IFF_DRV_OACTIVE);
794 callout_reset(&sc->stat_ch, hz, awg_tick, sc);
798 awg_init(void *softc)
800 struct awg_softc *sc;
810 awg_stop(struct awg_softc *sc)
816 AWG_ASSERT_LOCKED(sc);
820 callout_stop(&sc->stat_ch);
822 /* Stop transmit DMA and flush data in the TX FIFO */
823 val = RD4(sc, EMAC_TX_CTL_1);
825 val |= FLUSH_TX_FIFO;
826 WR4(sc, EMAC_TX_CTL_1, val);
828 /* Disable transmitter */
829 val = RD4(sc, EMAC_TX_CTL_0);
830 WR4(sc, EMAC_TX_CTL_0, val & ~TX_EN);
832 /* Disable receiver */
833 val = RD4(sc, EMAC_RX_CTL_0);
834 WR4(sc, EMAC_RX_CTL_0, val & ~RX_EN);
836 /* Disable interrupts */
837 awg_disable_intr(sc);
839 /* Disable transmit DMA */
840 val = RD4(sc, EMAC_TX_CTL_1);
841 WR4(sc, EMAC_TX_CTL_1, val & ~TX_DMA_EN);
843 /* Disable receive DMA */
844 val = RD4(sc, EMAC_RX_CTL_1);
845 WR4(sc, EMAC_RX_CTL_1, val & ~RX_DMA_EN);
849 /* Finish handling transmitted buffers */
852 /* Release any untransmitted buffers. */
853 for (i = sc->tx.next; sc->tx.queued > 0; i = TX_NEXT(i)) {
854 val = le32toh(sc->tx.desc_ring[i].status);
855 if ((val & TX_DESC_CTL) != 0)
857 awg_clean_txbuf(sc, i);
860 for (; sc->tx.queued > 0; i = TX_NEXT(i)) {
861 sc->tx.desc_ring[i].status = 0;
862 awg_clean_txbuf(sc, i);
864 sc->tx.cur = sc->tx.next;
865 bus_dmamap_sync(sc->tx.desc_tag, sc->tx.desc_map,
866 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
868 /* Setup RX buffers for reuse */
869 bus_dmamap_sync(sc->rx.desc_tag, sc->rx.desc_map,
870 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
872 for (i = sc->rx.cur; ; i = RX_NEXT(i)) {
873 val = le32toh(sc->rx.desc_ring[i].status);
874 if ((val & RX_DESC_CTL) != 0)
876 awg_reuse_rxdesc(sc, i);
879 bus_dmamap_sync(sc->rx.desc_tag, sc->rx.desc_map,
880 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
882 if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
886 awg_rxintr(struct awg_softc *sc)
889 struct mbuf *m, *mh, *mt;
890 int error, index, len, cnt, npkt;
898 bus_dmamap_sync(sc->rx.desc_tag, sc->rx.desc_map,
899 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
901 for (index = sc->rx.cur; ; index = RX_NEXT(index)) {
902 status = le32toh(sc->rx.desc_ring[index].status);
903 if ((status & RX_DESC_CTL) != 0)
906 len = (status & RX_FRM_LEN) >> RX_FRM_LEN_SHIFT;
909 if ((status & (RX_NO_ENOUGH_BUF_ERR | RX_OVERFLOW_ERR)) != 0)
910 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
911 awg_reuse_rxdesc(sc, index);
915 m = sc->rx.buf_map[index].mbuf;
917 error = awg_newbuf_rx(sc, index);
919 if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
920 awg_reuse_rxdesc(sc, index);
924 m->m_pkthdr.rcvif = ifp;
925 m->m_pkthdr.len = len;
927 if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
929 if ((if_getcapenable(ifp) & IFCAP_RXCSUM) != 0 &&
930 (status & RX_FRM_TYPE) != 0) {
931 m->m_pkthdr.csum_flags = CSUM_IP_CHECKED;
932 if ((status & RX_HEADER_ERR) == 0)
933 m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
934 if ((status & RX_PAYLOAD_ERR) == 0) {
935 m->m_pkthdr.csum_flags |=
936 CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
937 m->m_pkthdr.csum_data = 0xffff;
950 if (cnt == awg_rx_batch) {
959 if (index != sc->rx.cur) {
960 bus_dmamap_sync(sc->rx.desc_tag, sc->rx.desc_map,
961 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
976 awg_txeof(struct awg_softc *sc)
978 struct emac_desc *desc;
979 uint32_t status, size;
983 AWG_ASSERT_LOCKED(sc);
985 bus_dmamap_sync(sc->tx.desc_tag, sc->tx.desc_map,
986 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
991 for (i = sc->tx.next; sc->tx.queued > 0; i = TX_NEXT(i)) {
992 desc = &sc->tx.desc_ring[i];
993 status = le32toh(desc->status);
994 if ((status & TX_DESC_CTL) != 0)
996 size = le32toh(desc->size);
997 if (size & TX_LAST_DESC) {
998 if ((status & (TX_HEADER_ERR | TX_PAYLOAD_ERR)) != 0)
999 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1001 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
1004 awg_clean_txbuf(sc, i);
1009 if_setdrvflagbits(ifp, 0, IFF_DRV_OACTIVE);
1016 struct awg_softc *sc;
1022 val = RD4(sc, EMAC_INT_STA);
1023 WR4(sc, EMAC_INT_STA, val);
1031 if (val & (TX_INT | TX_BUF_UA_INT)) {
1032 if (!if_sendq_empty(sc->ifp))
1033 awg_start_locked(sc);
1039 #ifdef DEVICE_POLLING
1041 awg_poll(if_t ifp, enum poll_cmd cmd, int count)
1043 struct awg_softc *sc;
1047 sc = if_getsoftc(ifp);
1052 if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) == 0) {
1057 rx_npkts = awg_rxintr(sc);
1059 if (!if_sendq_empty(ifp))
1060 awg_start_locked(sc);
1062 if (cmd == POLL_AND_CHECK_STATUS) {
1063 val = RD4(sc, EMAC_INT_STA);
1065 WR4(sc, EMAC_INT_STA, val);
1075 awg_ioctl(if_t ifp, u_long cmd, caddr_t data)
1077 struct awg_softc *sc;
1078 struct mii_data *mii;
1080 int flags, mask, error;
1082 sc = if_getsoftc(ifp);
1083 mii = device_get_softc(sc->miibus);
1084 ifr = (struct ifreq *)data;
1090 if (if_getflags(ifp) & IFF_UP) {
1091 if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
1092 flags = if_getflags(ifp) ^ sc->if_flags;
1093 if ((flags & (IFF_PROMISC|IFF_ALLMULTI)) != 0)
1094 awg_setup_rxfilter(sc);
1096 awg_init_locked(sc);
1098 if (if_getdrvflags(ifp) & IFF_DRV_RUNNING)
1101 sc->if_flags = if_getflags(ifp);
1106 if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
1108 awg_setup_rxfilter(sc);
1114 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, cmd);
1117 mask = ifr->ifr_reqcap ^ if_getcapenable(ifp);
1118 #ifdef DEVICE_POLLING
1119 if (mask & IFCAP_POLLING) {
1120 if ((ifr->ifr_reqcap & IFCAP_POLLING) != 0) {
1121 error = ether_poll_register(awg_poll, ifp);
1125 awg_disable_intr(sc);
1126 if_setcapenablebit(ifp, IFCAP_POLLING, 0);
1129 error = ether_poll_deregister(ifp);
1131 awg_enable_intr(sc);
1132 if_setcapenablebit(ifp, 0, IFCAP_POLLING);
1137 if (mask & IFCAP_VLAN_MTU)
1138 if_togglecapenable(ifp, IFCAP_VLAN_MTU);
1139 if (mask & IFCAP_RXCSUM)
1140 if_togglecapenable(ifp, IFCAP_RXCSUM);
1141 if (mask & IFCAP_TXCSUM)
1142 if_togglecapenable(ifp, IFCAP_TXCSUM);
1143 if ((if_getcapenable(ifp) & IFCAP_TXCSUM) != 0)
1144 if_sethwassistbits(ifp, CSUM_IP | CSUM_UDP | CSUM_TCP, 0);
1146 if_sethwassistbits(ifp, 0, CSUM_IP | CSUM_UDP | CSUM_TCP);
1149 error = ether_ioctl(ifp, cmd, data);
1157 syscon_read_emac_clk_reg(device_t dev)
1159 struct awg_softc *sc;
1161 sc = device_get_softc(dev);
1162 if (sc->syscon != NULL)
1163 return (SYSCON_READ_4(sc->syscon, EMAC_CLK_REG));
1164 else if (sc->res[_RES_SYSCON] != NULL)
1165 return (bus_read_4(sc->res[_RES_SYSCON], 0));
1171 syscon_write_emac_clk_reg(device_t dev, uint32_t val)
1173 struct awg_softc *sc;
1175 sc = device_get_softc(dev);
1176 if (sc->syscon != NULL)
1177 SYSCON_WRITE_4(sc->syscon, EMAC_CLK_REG, val);
1178 else if (sc->res[_RES_SYSCON] != NULL)
1179 bus_write_4(sc->res[_RES_SYSCON], 0, val);
1183 awg_get_phy_node(device_t dev)
1188 node = ofw_bus_get_node(dev);
1189 if (OF_getencprop(node, "phy-handle", (void *)&phy_handle,
1190 sizeof(phy_handle)) <= 0)
1193 return (OF_node_from_xref(phy_handle));
1197 awg_has_internal_phy(device_t dev)
1199 phandle_t node, phy_node;
1201 node = ofw_bus_get_node(dev);
1202 /* Legacy binding */
1203 if (OF_hasprop(node, "allwinner,use-internal-phy"))
1206 phy_node = awg_get_phy_node(dev);
1207 return (phy_node != 0 && ofw_bus_node_is_compatible(OF_parent(phy_node),
1208 "allwinner,sun8i-h3-mdio-internal") != 0);
1212 awg_parse_delay(device_t dev, uint32_t *tx_delay, uint32_t *rx_delay)
1217 if (tx_delay == NULL || rx_delay == NULL)
1219 *tx_delay = *rx_delay = 0;
1220 node = ofw_bus_get_node(dev);
1222 if (OF_getencprop(node, "tx-delay", &delay, sizeof(delay)) >= 0)
1224 else if (OF_getencprop(node, "allwinner,tx-delay-ps", &delay,
1225 sizeof(delay)) >= 0) {
1226 if ((delay % 100) != 0) {
1227 device_printf(dev, "tx-delay-ps is not a multiple of 100\n");
1230 *tx_delay = delay / 100;
1232 if (*tx_delay > 7) {
1233 device_printf(dev, "tx-delay out of range\n");
1237 if (OF_getencprop(node, "rx-delay", &delay, sizeof(delay)) >= 0)
1239 else if (OF_getencprop(node, "allwinner,rx-delay-ps", &delay,
1240 sizeof(delay)) >= 0) {
1241 if ((delay % 100) != 0) {
1242 device_printf(dev, "rx-delay-ps is not within documented domain\n");
1245 *rx_delay = delay / 100;
1247 if (*rx_delay > 31) {
1248 device_printf(dev, "rx-delay out of range\n");
1256 awg_setup_phy(device_t dev)
1258 struct awg_softc *sc;
1259 clk_t clk_tx, clk_tx_parent;
1260 const char *tx_parent_name;
1263 uint32_t reg, tx_delay, rx_delay;
1267 sc = device_get_softc(dev);
1268 node = ofw_bus_get_node(dev);
1271 if (OF_getprop_alloc(node, "phy-mode", (void **)&phy_type) == 0)
1274 if (sc->syscon != NULL || sc->res[_RES_SYSCON] != NULL)
1278 device_printf(dev, "PHY type: %s, conf mode: %s\n", phy_type,
1279 use_syscon ? "reg" : "clk");
1283 * Abstract away writing to syscon for devices like the pine64.
1284 * For the pine64, we get dtb from U-Boot and it still uses the
1285 * legacy setup of specifying syscon register in emac node
1286 * rather than as its own node and using an xref in emac.
1287 * These abstractions can go away once U-Boot dts is up-to-date.
1289 reg = syscon_read_emac_clk_reg(dev);
1290 reg &= ~(EMAC_CLK_PIT | EMAC_CLK_SRC | EMAC_CLK_RMII_EN);
1291 if (strncmp(phy_type, "rgmii", 5) == 0)
1292 reg |= EMAC_CLK_PIT_RGMII | EMAC_CLK_SRC_RGMII;
1293 else if (strcmp(phy_type, "rmii") == 0)
1294 reg |= EMAC_CLK_RMII_EN;
1296 reg |= EMAC_CLK_PIT_MII | EMAC_CLK_SRC_MII;
1299 * Fail attach if we fail to parse either of the delay
1300 * parameters. If we don't have the proper delay to write to
1301 * syscon, then awg likely won't function properly anyways.
1302 * Lack of delay is not an error!
1304 error = awg_parse_delay(dev, &tx_delay, &rx_delay);
1308 /* Default to 0 and we'll increase it if we need to. */
1309 reg &= ~(EMAC_CLK_ETXDC | EMAC_CLK_ERXDC);
1311 reg |= (tx_delay << EMAC_CLK_ETXDC_SHIFT);
1313 reg |= (rx_delay << EMAC_CLK_ERXDC_SHIFT);
1315 if (sc->type == EMAC_H3) {
1316 if (awg_has_internal_phy(dev)) {
1317 reg |= EMAC_CLK_EPHY_SELECT;
1318 reg &= ~EMAC_CLK_EPHY_SHUTDOWN;
1319 if (OF_hasprop(node,
1320 "allwinner,leds-active-low"))
1321 reg |= EMAC_CLK_EPHY_LED_POL;
1323 reg &= ~EMAC_CLK_EPHY_LED_POL;
1325 /* Set internal PHY addr to 1 */
1326 reg &= ~EMAC_CLK_EPHY_ADDR;
1327 reg |= (1 << EMAC_CLK_EPHY_ADDR_SHIFT);
1329 reg &= ~EMAC_CLK_EPHY_SELECT;
1334 device_printf(dev, "EMAC clock: 0x%08x\n", reg);
1335 syscon_write_emac_clk_reg(dev, reg);
1337 if (strncmp(phy_type, "rgmii", 5) == 0)
1338 tx_parent_name = "emac_int_tx";
1340 tx_parent_name = "mii_phy_tx";
1342 /* Get the TX clock */
1343 error = clk_get_by_ofw_name(dev, 0, "tx", &clk_tx);
1345 device_printf(dev, "cannot get tx clock\n");
1349 /* Find the desired parent clock based on phy-mode property */
1350 error = clk_get_by_name(dev, tx_parent_name, &clk_tx_parent);
1352 device_printf(dev, "cannot get clock '%s'\n",
1357 /* Set TX clock parent */
1358 error = clk_set_parent_by_clk(clk_tx, clk_tx_parent);
1360 device_printf(dev, "cannot set tx clock parent\n");
1364 /* Enable TX clock */
1365 error = clk_enable(clk_tx);
1367 device_printf(dev, "cannot enable tx clock\n");
1375 OF_prop_free(phy_type);
1380 awg_setup_extres(device_t dev)
1382 struct awg_softc *sc;
1383 phandle_t node, phy_node;
1384 hwreset_t rst_ahb, rst_ephy;
1385 clk_t clk_ahb, clk_ephy;
1390 sc = device_get_softc(dev);
1391 rst_ahb = rst_ephy = NULL;
1392 clk_ahb = clk_ephy = NULL;
1394 node = ofw_bus_get_node(dev);
1395 phy_node = awg_get_phy_node(dev);
1397 if (phy_node == 0 && OF_hasprop(node, "phy-handle")) {
1399 device_printf(dev, "cannot get phy handle\n");
1403 /* Get AHB clock and reset resources */
1404 error = hwreset_get_by_ofw_name(dev, 0, "stmmaceth", &rst_ahb);
1406 error = hwreset_get_by_ofw_name(dev, 0, "ahb", &rst_ahb);
1408 device_printf(dev, "cannot get ahb reset\n");
1411 if (hwreset_get_by_ofw_name(dev, 0, "ephy", &rst_ephy) != 0)
1412 if (phy_node == 0 || hwreset_get_by_ofw_idx(dev, phy_node, 0,
1415 error = clk_get_by_ofw_name(dev, 0, "stmmaceth", &clk_ahb);
1417 error = clk_get_by_ofw_name(dev, 0, "ahb", &clk_ahb);
1419 device_printf(dev, "cannot get ahb clock\n");
1422 if (clk_get_by_ofw_name(dev, 0, "ephy", &clk_ephy) != 0)
1423 if (phy_node == 0 || clk_get_by_ofw_index(dev, phy_node, 0,
1427 if (OF_hasprop(node, "syscon") && syscon_get_by_ofw_property(dev, node,
1428 "syscon", &sc->syscon) != 0) {
1429 device_printf(dev, "cannot get syscon driver handle\n");
1433 /* Configure PHY for MII or RGMII mode */
1434 if (awg_setup_phy(dev) != 0)
1438 error = clk_enable(clk_ahb);
1440 device_printf(dev, "cannot enable ahb clock\n");
1443 if (clk_ephy != NULL) {
1444 error = clk_enable(clk_ephy);
1446 device_printf(dev, "cannot enable ephy clock\n");
1451 /* De-assert reset */
1452 error = hwreset_deassert(rst_ahb);
1454 device_printf(dev, "cannot de-assert ahb reset\n");
1457 if (rst_ephy != NULL) {
1459 * The ephy reset is left de-asserted by U-Boot. Assert it
1460 * here to make sure that we're in a known good state going
1461 * into the PHY reset.
1463 hwreset_assert(rst_ephy);
1464 error = hwreset_deassert(rst_ephy);
1466 device_printf(dev, "cannot de-assert ephy reset\n");
1471 /* Enable PHY regulator if applicable */
1472 if (regulator_get_by_ofw_property(dev, 0, "phy-supply", ®) == 0) {
1473 error = regulator_enable(reg);
1475 device_printf(dev, "cannot enable PHY regulator\n");
1480 /* Determine MDC clock divide ratio based on AHB clock */
1481 error = clk_get_freq(clk_ahb, &freq);
1483 device_printf(dev, "cannot get AHB clock frequency\n");
1486 div = freq / MDIO_FREQ;
1488 sc->mdc_div_ratio_m = MDC_DIV_RATIO_M_16;
1490 sc->mdc_div_ratio_m = MDC_DIV_RATIO_M_32;
1492 sc->mdc_div_ratio_m = MDC_DIV_RATIO_M_64;
1493 else if (div <= 128)
1494 sc->mdc_div_ratio_m = MDC_DIV_RATIO_M_128;
1496 device_printf(dev, "cannot determine MDC clock divide ratio\n");
1502 device_printf(dev, "AHB frequency %ju Hz, MDC div: 0x%x\n",
1503 (uintmax_t)freq, sc->mdc_div_ratio_m);
1509 regulator_release(reg);
1510 if (clk_ephy != NULL)
1511 clk_release(clk_ephy);
1512 if (clk_ahb != NULL)
1513 clk_release(clk_ahb);
1514 if (rst_ephy != NULL)
1515 hwreset_release(rst_ephy);
1516 if (rst_ahb != NULL)
1517 hwreset_release(rst_ahb);
1522 awg_get_eaddr(device_t dev, uint8_t *eaddr)
1524 struct awg_softc *sc;
1525 uint32_t maclo, machi, rnd;
1527 uint32_t rootkey_size;
1529 sc = device_get_softc(dev);
1531 machi = RD4(sc, EMAC_ADDR_HIGH(0)) & 0xffff;
1532 maclo = RD4(sc, EMAC_ADDR_LOW(0));
1534 rootkey_size = sizeof(rootkey);
1535 if (maclo == 0xffffffff && machi == 0xffff) {
1536 /* MAC address in hardware is invalid, create one */
1537 if (aw_sid_get_fuse(AW_SID_FUSE_ROOTKEY, rootkey,
1538 &rootkey_size) == 0 &&
1539 (rootkey[3] | rootkey[12] | rootkey[13] | rootkey[14] |
1540 rootkey[15]) != 0) {
1541 /* MAC address is derived from the root key in SID */
1542 maclo = (rootkey[13] << 24) | (rootkey[12] << 16) |
1543 (rootkey[3] << 8) | 0x02;
1544 machi = (rootkey[15] << 8) | rootkey[14];
1548 maclo = 0x00f2 | (rnd & 0xffff0000);
1549 machi = rnd & 0xffff;
1553 eaddr[0] = maclo & 0xff;
1554 eaddr[1] = (maclo >> 8) & 0xff;
1555 eaddr[2] = (maclo >> 16) & 0xff;
1556 eaddr[3] = (maclo >> 24) & 0xff;
1557 eaddr[4] = machi & 0xff;
1558 eaddr[5] = (machi >> 8) & 0xff;
1563 awg_dump_regs(device_t dev)
1565 static const struct {
1569 { "BASIC_CTL_0", EMAC_BASIC_CTL_0 },
1570 { "BASIC_CTL_1", EMAC_BASIC_CTL_1 },
1571 { "INT_STA", EMAC_INT_STA },
1572 { "INT_EN", EMAC_INT_EN },
1573 { "TX_CTL_0", EMAC_TX_CTL_0 },
1574 { "TX_CTL_1", EMAC_TX_CTL_1 },
1575 { "TX_FLOW_CTL", EMAC_TX_FLOW_CTL },
1576 { "TX_DMA_LIST", EMAC_TX_DMA_LIST },
1577 { "RX_CTL_0", EMAC_RX_CTL_0 },
1578 { "RX_CTL_1", EMAC_RX_CTL_1 },
1579 { "RX_DMA_LIST", EMAC_RX_DMA_LIST },
1580 { "RX_FRM_FLT", EMAC_RX_FRM_FLT },
1581 { "RX_HASH_0", EMAC_RX_HASH_0 },
1582 { "RX_HASH_1", EMAC_RX_HASH_1 },
1583 { "MII_CMD", EMAC_MII_CMD },
1584 { "ADDR_HIGH0", EMAC_ADDR_HIGH(0) },
1585 { "ADDR_LOW0", EMAC_ADDR_LOW(0) },
1586 { "TX_DMA_STA", EMAC_TX_DMA_STA },
1587 { "TX_DMA_CUR_DESC", EMAC_TX_DMA_CUR_DESC },
1588 { "TX_DMA_CUR_BUF", EMAC_TX_DMA_CUR_BUF },
1589 { "RX_DMA_STA", EMAC_RX_DMA_STA },
1590 { "RX_DMA_CUR_DESC", EMAC_RX_DMA_CUR_DESC },
1591 { "RX_DMA_CUR_BUF", EMAC_RX_DMA_CUR_BUF },
1592 { "RGMII_STA", EMAC_RGMII_STA },
1594 struct awg_softc *sc;
1597 sc = device_get_softc(dev);
1599 for (n = 0; n < nitems(regs); n++)
1600 device_printf(dev, " %-20s %08x\n", regs[n].name,
1601 RD4(sc, regs[n].reg));
1605 #define GPIO_ACTIVE_LOW 1
1608 awg_phy_reset(device_t dev)
1610 pcell_t gpio_prop[4], delay_prop[3];
1611 phandle_t node, gpio_node;
1613 uint32_t pin, flags;
1616 node = ofw_bus_get_node(dev);
1617 if (OF_getencprop(node, "allwinner,reset-gpio", gpio_prop,
1618 sizeof(gpio_prop)) <= 0)
1621 if (OF_getencprop(node, "allwinner,reset-delays-us", delay_prop,
1622 sizeof(delay_prop)) <= 0)
1625 gpio_node = OF_node_from_xref(gpio_prop[0]);
1626 if ((gpio = OF_device_from_xref(gpio_prop[0])) == NULL)
1629 if (GPIO_MAP_GPIOS(gpio, node, gpio_node, nitems(gpio_prop) - 1,
1630 gpio_prop + 1, &pin, &flags) != 0)
1633 pin_value = GPIO_PIN_LOW;
1634 if (OF_hasprop(node, "allwinner,reset-active-low"))
1635 pin_value = GPIO_PIN_HIGH;
1637 if (flags & GPIO_ACTIVE_LOW)
1638 pin_value = !pin_value;
1640 GPIO_PIN_SETFLAGS(gpio, pin, GPIO_PIN_OUTPUT);
1641 GPIO_PIN_SET(gpio, pin, pin_value);
1642 DELAY(delay_prop[0]);
1643 GPIO_PIN_SET(gpio, pin, !pin_value);
1644 DELAY(delay_prop[1]);
1645 GPIO_PIN_SET(gpio, pin, pin_value);
1646 DELAY(delay_prop[2]);
1652 awg_reset(device_t dev)
1654 struct awg_softc *sc;
1657 sc = device_get_softc(dev);
1659 /* Reset PHY if necessary */
1660 if (awg_phy_reset(dev) != 0) {
1661 device_printf(dev, "failed to reset PHY\n");
1665 /* Soft reset all registers and logic */
1666 WR4(sc, EMAC_BASIC_CTL_1, BASIC_CTL_SOFT_RST);
1668 /* Wait for soft reset bit to self-clear */
1669 for (retry = SOFT_RST_RETRY; retry > 0; retry--) {
1670 if ((RD4(sc, EMAC_BASIC_CTL_1) & BASIC_CTL_SOFT_RST) == 0)
1675 device_printf(dev, "soft reset timed out\n");
1686 awg_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1690 *(bus_addr_t *)arg = segs[0].ds_addr;
1694 awg_setup_dma(device_t dev)
1696 struct awg_softc *sc;
1699 sc = device_get_softc(dev);
1702 error = bus_dma_tag_create(
1703 bus_get_dma_tag(dev), /* Parent tag */
1704 DESC_ALIGN, 0, /* alignment, boundary */
1705 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
1706 BUS_SPACE_MAXADDR, /* highaddr */
1707 NULL, NULL, /* filter, filterarg */
1708 TX_DESC_SIZE, 1, /* maxsize, nsegs */
1709 TX_DESC_SIZE, /* maxsegsize */
1711 NULL, NULL, /* lockfunc, lockarg */
1714 device_printf(dev, "cannot create TX descriptor ring tag\n");
1718 error = bus_dmamem_alloc(sc->tx.desc_tag, (void **)&sc->tx.desc_ring,
1719 BUS_DMA_COHERENT | BUS_DMA_WAITOK | BUS_DMA_ZERO, &sc->tx.desc_map);
1721 device_printf(dev, "cannot allocate TX descriptor ring\n");
1725 error = bus_dmamap_load(sc->tx.desc_tag, sc->tx.desc_map,
1726 sc->tx.desc_ring, TX_DESC_SIZE, awg_dmamap_cb,
1727 &sc->tx.desc_ring_paddr, 0);
1729 device_printf(dev, "cannot load TX descriptor ring\n");
1733 for (i = 0; i < TX_DESC_COUNT; i++)
1734 sc->tx.desc_ring[i].next =
1735 htole32(sc->tx.desc_ring_paddr + DESC_OFF(TX_NEXT(i)));
1737 error = bus_dma_tag_create(
1738 bus_get_dma_tag(dev), /* Parent tag */
1739 1, 0, /* alignment, boundary */
1740 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
1741 BUS_SPACE_MAXADDR, /* highaddr */
1742 NULL, NULL, /* filter, filterarg */
1743 MCLBYTES, TX_MAX_SEGS, /* maxsize, nsegs */
1744 MCLBYTES, /* maxsegsize */
1746 NULL, NULL, /* lockfunc, lockarg */
1749 device_printf(dev, "cannot create TX buffer tag\n");
1754 for (i = 0; i < TX_DESC_COUNT; i++) {
1755 error = bus_dmamap_create(sc->tx.buf_tag, 0,
1756 &sc->tx.buf_map[i].map);
1758 device_printf(dev, "cannot create TX buffer map\n");
1764 error = bus_dma_tag_create(
1765 bus_get_dma_tag(dev), /* Parent tag */
1766 DESC_ALIGN, 0, /* alignment, boundary */
1767 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
1768 BUS_SPACE_MAXADDR, /* highaddr */
1769 NULL, NULL, /* filter, filterarg */
1770 RX_DESC_SIZE, 1, /* maxsize, nsegs */
1771 RX_DESC_SIZE, /* maxsegsize */
1773 NULL, NULL, /* lockfunc, lockarg */
1776 device_printf(dev, "cannot create RX descriptor ring tag\n");
1780 error = bus_dmamem_alloc(sc->rx.desc_tag, (void **)&sc->rx.desc_ring,
1781 BUS_DMA_COHERENT | BUS_DMA_WAITOK | BUS_DMA_ZERO, &sc->rx.desc_map);
1783 device_printf(dev, "cannot allocate RX descriptor ring\n");
1787 error = bus_dmamap_load(sc->rx.desc_tag, sc->rx.desc_map,
1788 sc->rx.desc_ring, RX_DESC_SIZE, awg_dmamap_cb,
1789 &sc->rx.desc_ring_paddr, 0);
1791 device_printf(dev, "cannot load RX descriptor ring\n");
1795 error = bus_dma_tag_create(
1796 bus_get_dma_tag(dev), /* Parent tag */
1797 1, 0, /* alignment, boundary */
1798 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
1799 BUS_SPACE_MAXADDR, /* highaddr */
1800 NULL, NULL, /* filter, filterarg */
1801 MCLBYTES, 1, /* maxsize, nsegs */
1802 MCLBYTES, /* maxsegsize */
1804 NULL, NULL, /* lockfunc, lockarg */
1807 device_printf(dev, "cannot create RX buffer tag\n");
1811 error = bus_dmamap_create(sc->rx.buf_tag, 0, &sc->rx.buf_spare_map);
1814 "cannot create RX buffer spare map\n");
1818 for (i = 0; i < RX_DESC_COUNT; i++) {
1819 sc->rx.desc_ring[i].next =
1820 htole32(sc->rx.desc_ring_paddr + DESC_OFF(RX_NEXT(i)));
1822 error = bus_dmamap_create(sc->rx.buf_tag, 0,
1823 &sc->rx.buf_map[i].map);
1825 device_printf(dev, "cannot create RX buffer map\n");
1828 sc->rx.buf_map[i].mbuf = NULL;
1829 error = awg_newbuf_rx(sc, i);
1831 device_printf(dev, "cannot create RX buffer\n");
1835 bus_dmamap_sync(sc->rx.desc_tag, sc->rx.desc_map,
1836 BUS_DMASYNC_PREWRITE);
1838 /* Write transmit and receive descriptor base address registers */
1839 WR4(sc, EMAC_TX_DMA_LIST, sc->tx.desc_ring_paddr);
1840 WR4(sc, EMAC_RX_DMA_LIST, sc->rx.desc_ring_paddr);
1846 awg_probe(device_t dev)
1848 if (!ofw_bus_status_okay(dev))
1851 if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
1854 device_set_desc(dev, "Allwinner Gigabit Ethernet");
1855 return (BUS_PROBE_DEFAULT);
1859 awg_attach(device_t dev)
1861 uint8_t eaddr[ETHER_ADDR_LEN];
1862 struct awg_softc *sc;
1865 sc = device_get_softc(dev);
1867 sc->type = ofw_bus_search_compatible(dev, compat_data)->ocd_data;
1869 if (bus_alloc_resources(dev, awg_spec, sc->res) != 0) {
1870 device_printf(dev, "cannot allocate resources for device\n");
1874 mtx_init(&sc->mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK, MTX_DEF);
1875 callout_init_mtx(&sc->stat_ch, &sc->mtx, 0);
1876 TASK_INIT(&sc->link_task, 0, awg_link_task, sc);
1878 /* Setup clocks and regulators */
1879 error = awg_setup_extres(dev);
1883 /* Read MAC address before resetting the chip */
1884 awg_get_eaddr(dev, eaddr);
1886 /* Soft reset EMAC core */
1887 error = awg_reset(dev);
1891 /* Setup DMA descriptors */
1892 error = awg_setup_dma(dev);
1896 /* Install interrupt handler */
1897 error = bus_setup_intr(dev, sc->res[_RES_IRQ],
1898 INTR_TYPE_NET | INTR_MPSAFE, NULL, awg_intr, sc, &sc->ih);
1900 device_printf(dev, "cannot setup interrupt handler\n");
1904 /* Setup ethernet interface */
1905 sc->ifp = if_alloc(IFT_ETHER);
1906 if_setsoftc(sc->ifp, sc);
1907 if_initname(sc->ifp, device_get_name(dev), device_get_unit(dev));
1908 if_setflags(sc->ifp, IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST);
1909 if_setstartfn(sc->ifp, awg_start);
1910 if_setioctlfn(sc->ifp, awg_ioctl);
1911 if_setinitfn(sc->ifp, awg_init);
1912 if_setsendqlen(sc->ifp, TX_DESC_COUNT - 1);
1913 if_setsendqready(sc->ifp);
1914 if_sethwassist(sc->ifp, CSUM_IP | CSUM_UDP | CSUM_TCP);
1915 if_setcapabilities(sc->ifp, IFCAP_VLAN_MTU | IFCAP_HWCSUM);
1916 if_setcapenable(sc->ifp, if_getcapabilities(sc->ifp));
1917 #ifdef DEVICE_POLLING
1918 if_setcapabilitiesbit(sc->ifp, IFCAP_POLLING, 0);
1921 /* Attach MII driver */
1922 error = mii_attach(dev, &sc->miibus, sc->ifp, awg_media_change,
1923 awg_media_status, BMSR_DEFCAPMASK, MII_PHY_ANY, MII_OFFSET_ANY,
1926 device_printf(dev, "cannot attach PHY\n");
1930 /* Attach ethernet interface */
1931 ether_ifattach(sc->ifp, eaddr);
1936 static device_method_t awg_methods[] = {
1937 /* Device interface */
1938 DEVMETHOD(device_probe, awg_probe),
1939 DEVMETHOD(device_attach, awg_attach),
1942 DEVMETHOD(miibus_readreg, awg_miibus_readreg),
1943 DEVMETHOD(miibus_writereg, awg_miibus_writereg),
1944 DEVMETHOD(miibus_statchg, awg_miibus_statchg),
1949 static driver_t awg_driver = {
1952 sizeof(struct awg_softc),
1955 static devclass_t awg_devclass;
1957 DRIVER_MODULE(awg, simplebus, awg_driver, awg_devclass, 0, 0);
1958 DRIVER_MODULE(miibus, awg, miibus_driver, miibus_devclass, 0, 0);
1959 MODULE_DEPEND(awg, ether, 1, 1, 1);
1960 MODULE_DEPEND(awg, miibus, 1, 1, 1);
1961 MODULE_DEPEND(awg, aw_sid, 1, 1, 1);
1962 SIMPLEBUS_PNP_INFO(compat_data);