2 * Copyright (c) 2016 Jared McNeill <jmcneill@invisible.ca>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
19 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
21 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
22 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * Allwinner Gigabit Ethernet MAC (EMAC) controller
33 #include "opt_device_polling.h"
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
38 #include <sys/param.h>
39 #include <sys/systm.h>
42 #include <sys/kernel.h>
43 #include <sys/endian.h>
45 #include <sys/socket.h>
46 #include <sys/sockio.h>
47 #include <sys/module.h>
48 #include <sys/taskqueue.h>
53 #include <net/ethernet.h>
54 #include <net/if_dl.h>
55 #include <net/if_media.h>
56 #include <net/if_types.h>
57 #include <net/if_var.h>
59 #include <machine/bus.h>
61 #include <dev/ofw/ofw_bus.h>
62 #include <dev/ofw/ofw_bus_subr.h>
64 #include <arm/allwinner/if_awgreg.h>
65 #include <arm/allwinner/aw_sid.h>
66 #include <dev/mii/mii.h>
67 #include <dev/mii/miivar.h>
69 #include <dev/extres/clk/clk.h>
70 #include <dev/extres/hwreset/hwreset.h>
71 #include <dev/extres/regulator/regulator.h>
72 #include <dev/extres/syscon/syscon.h>
74 #include "syscon_if.h"
75 #include "miibus_if.h"
78 #define RD4(sc, reg) bus_read_4((sc)->res[_RES_EMAC], (reg))
79 #define WR4(sc, reg, val) bus_write_4((sc)->res[_RES_EMAC], (reg), (val))
81 #define AWG_LOCK(sc) mtx_lock(&(sc)->mtx)
82 #define AWG_UNLOCK(sc) mtx_unlock(&(sc)->mtx);
83 #define AWG_ASSERT_LOCKED(sc) mtx_assert(&(sc)->mtx, MA_OWNED)
84 #define AWG_ASSERT_UNLOCKED(sc) mtx_assert(&(sc)->mtx, MA_NOTOWNED)
87 #define TX_DESC_COUNT 1024
88 #define TX_DESC_SIZE (sizeof(struct emac_desc) * TX_DESC_COUNT)
89 #define RX_DESC_COUNT 256
90 #define RX_DESC_SIZE (sizeof(struct emac_desc) * RX_DESC_COUNT)
92 #define DESC_OFF(n) ((n) * sizeof(struct emac_desc))
93 #define TX_NEXT(n) (((n) + 1) & (TX_DESC_COUNT - 1))
94 #define TX_SKIP(n, o) (((n) + (o)) & (TX_DESC_COUNT - 1))
95 #define RX_NEXT(n) (((n) + 1) & (RX_DESC_COUNT - 1))
97 #define TX_MAX_SEGS 20
99 #define SOFT_RST_RETRY 1000
100 #define MII_BUSY_RETRY 1000
101 #define MDIO_FREQ 2500000
103 #define BURST_LEN_DEFAULT 8
104 #define RX_TX_PRI_DEFAULT 0
105 #define PAUSE_TIME_DEFAULT 0x400
106 #define TX_INTERVAL_DEFAULT 64
107 #define RX_BATCH_DEFAULT 64
109 /* syscon EMAC clock register */
110 #define EMAC_CLK_REG 0x30
111 #define EMAC_CLK_EPHY_ADDR (0x1f << 20) /* H3 */
112 #define EMAC_CLK_EPHY_ADDR_SHIFT 20
113 #define EMAC_CLK_EPHY_LED_POL (1 << 17) /* H3 */
114 #define EMAC_CLK_EPHY_SHUTDOWN (1 << 16) /* H3 */
115 #define EMAC_CLK_EPHY_SELECT (1 << 15) /* H3 */
116 #define EMAC_CLK_RMII_EN (1 << 13)
117 #define EMAC_CLK_ETXDC (0x7 << 10)
118 #define EMAC_CLK_ETXDC_SHIFT 10
119 #define EMAC_CLK_ERXDC (0x1f << 5)
120 #define EMAC_CLK_ERXDC_SHIFT 5
121 #define EMAC_CLK_PIT (0x1 << 2)
122 #define EMAC_CLK_PIT_MII (0 << 2)
123 #define EMAC_CLK_PIT_RGMII (1 << 2)
124 #define EMAC_CLK_SRC (0x3 << 0)
125 #define EMAC_CLK_SRC_MII (0 << 0)
126 #define EMAC_CLK_SRC_EXT_RGMII (1 << 0)
127 #define EMAC_CLK_SRC_RGMII (2 << 0)
129 /* Burst length of RX and TX DMA transfers */
130 static int awg_burst_len = BURST_LEN_DEFAULT;
131 TUNABLE_INT("hw.awg.burst_len", &awg_burst_len);
133 /* RX / TX DMA priority. If 1, RX DMA has priority over TX DMA. */
134 static int awg_rx_tx_pri = RX_TX_PRI_DEFAULT;
135 TUNABLE_INT("hw.awg.rx_tx_pri", &awg_rx_tx_pri);
137 /* Pause time field in the transmitted control frame */
138 static int awg_pause_time = PAUSE_TIME_DEFAULT;
139 TUNABLE_INT("hw.awg.pause_time", &awg_pause_time);
141 /* Request a TX interrupt every <n> descriptors */
142 static int awg_tx_interval = TX_INTERVAL_DEFAULT;
143 TUNABLE_INT("hw.awg.tx_interval", &awg_tx_interval);
145 /* Maximum number of mbufs to send to if_input */
146 static int awg_rx_batch = RX_BATCH_DEFAULT;
147 TUNABLE_INT("hw.awg.rx_batch", &awg_rx_batch);
155 static struct ofw_compat_data compat_data[] = {
156 { "allwinner,sun8i-a83t-emac", EMAC_A83T },
157 { "allwinner,sun8i-h3-emac", EMAC_H3 },
158 { "allwinner,sun50i-a64-emac", EMAC_A64 },
168 bus_dma_tag_t desc_tag;
169 bus_dmamap_t desc_map;
170 struct emac_desc *desc_ring;
171 bus_addr_t desc_ring_paddr;
172 bus_dma_tag_t buf_tag;
173 struct awg_bufmap buf_map[TX_DESC_COUNT];
174 u_int cur, next, queued;
179 bus_dma_tag_t desc_tag;
180 bus_dmamap_t desc_map;
181 struct emac_desc *desc_ring;
182 bus_addr_t desc_ring_paddr;
183 bus_dma_tag_t buf_tag;
184 struct awg_bufmap buf_map[RX_DESC_COUNT];
185 bus_dmamap_t buf_spare_map;
197 struct resource *res[_RES_NITEMS];
202 struct callout stat_ch;
203 struct task link_task;
205 u_int mdc_div_ratio_m;
209 struct syscon *syscon;
211 struct awg_txring tx;
212 struct awg_rxring rx;
215 static struct resource_spec awg_spec[] = {
216 { SYS_RES_MEMORY, 0, RF_ACTIVE },
217 { SYS_RES_IRQ, 0, RF_ACTIVE },
218 { SYS_RES_MEMORY, 1, RF_ACTIVE | RF_OPTIONAL },
222 static void awg_txeof(struct awg_softc *sc);
224 static int awg_parse_delay(device_t dev, uint32_t *tx_delay,
226 static uint32_t syscon_read_emac_clk_reg(device_t dev);
227 static void syscon_write_emac_clk_reg(device_t dev, uint32_t val);
228 static phandle_t awg_get_phy_node(device_t dev);
229 static bool awg_has_internal_phy(device_t dev);
232 awg_miibus_readreg(device_t dev, int phy, int reg)
234 struct awg_softc *sc;
237 sc = device_get_softc(dev);
240 WR4(sc, EMAC_MII_CMD,
241 (sc->mdc_div_ratio_m << MDC_DIV_RATIO_M_SHIFT) |
242 (phy << PHY_ADDR_SHIFT) |
243 (reg << PHY_REG_ADDR_SHIFT) |
245 for (retry = MII_BUSY_RETRY; retry > 0; retry--) {
246 if ((RD4(sc, EMAC_MII_CMD) & MII_BUSY) == 0) {
247 val = RD4(sc, EMAC_MII_DATA);
254 device_printf(dev, "phy read timeout, phy=%d reg=%d\n",
261 awg_miibus_writereg(device_t dev, int phy, int reg, int val)
263 struct awg_softc *sc;
266 sc = device_get_softc(dev);
268 WR4(sc, EMAC_MII_DATA, val);
269 WR4(sc, EMAC_MII_CMD,
270 (sc->mdc_div_ratio_m << MDC_DIV_RATIO_M_SHIFT) |
271 (phy << PHY_ADDR_SHIFT) |
272 (reg << PHY_REG_ADDR_SHIFT) |
274 for (retry = MII_BUSY_RETRY; retry > 0; retry--) {
275 if ((RD4(sc, EMAC_MII_CMD) & MII_BUSY) == 0)
281 device_printf(dev, "phy write timeout, phy=%d reg=%d\n",
288 awg_update_link_locked(struct awg_softc *sc)
290 struct mii_data *mii;
293 AWG_ASSERT_LOCKED(sc);
295 if ((if_getdrvflags(sc->ifp) & IFF_DRV_RUNNING) == 0)
297 mii = device_get_softc(sc->miibus);
299 if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
300 (IFM_ACTIVE | IFM_AVALID)) {
301 switch (IFM_SUBTYPE(mii->mii_media_active)) {
318 val = RD4(sc, EMAC_BASIC_CTL_0);
319 val &= ~(BASIC_CTL_SPEED | BASIC_CTL_DUPLEX);
321 if (IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_T ||
322 IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_SX)
323 val |= BASIC_CTL_SPEED_1000 << BASIC_CTL_SPEED_SHIFT;
324 else if (IFM_SUBTYPE(mii->mii_media_active) == IFM_100_TX)
325 val |= BASIC_CTL_SPEED_100 << BASIC_CTL_SPEED_SHIFT;
327 val |= BASIC_CTL_SPEED_10 << BASIC_CTL_SPEED_SHIFT;
329 if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) != 0)
330 val |= BASIC_CTL_DUPLEX;
332 WR4(sc, EMAC_BASIC_CTL_0, val);
334 val = RD4(sc, EMAC_RX_CTL_0);
335 val &= ~RX_FLOW_CTL_EN;
336 if ((IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_RXPAUSE) != 0)
337 val |= RX_FLOW_CTL_EN;
338 WR4(sc, EMAC_RX_CTL_0, val);
340 val = RD4(sc, EMAC_TX_FLOW_CTL);
341 val &= ~(PAUSE_TIME|TX_FLOW_CTL_EN);
342 if ((IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_TXPAUSE) != 0)
343 val |= TX_FLOW_CTL_EN;
344 if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) != 0)
345 val |= awg_pause_time << PAUSE_TIME_SHIFT;
346 WR4(sc, EMAC_TX_FLOW_CTL, val);
350 awg_link_task(void *arg, int pending)
352 struct awg_softc *sc;
357 awg_update_link_locked(sc);
362 awg_miibus_statchg(device_t dev)
364 struct awg_softc *sc;
366 sc = device_get_softc(dev);
368 taskqueue_enqueue(taskqueue_swi, &sc->link_task);
372 awg_media_status(if_t ifp, struct ifmediareq *ifmr)
374 struct awg_softc *sc;
375 struct mii_data *mii;
377 sc = if_getsoftc(ifp);
378 mii = device_get_softc(sc->miibus);
382 ifmr->ifm_active = mii->mii_media_active;
383 ifmr->ifm_status = mii->mii_media_status;
388 awg_media_change(if_t ifp)
390 struct awg_softc *sc;
391 struct mii_data *mii;
394 sc = if_getsoftc(ifp);
395 mii = device_get_softc(sc->miibus);
398 error = mii_mediachg(mii);
405 awg_encap(struct awg_softc *sc, struct mbuf **mp)
408 bus_dma_segment_t segs[TX_MAX_SEGS];
409 int error, nsegs, cur, first, last, i;
411 uint32_t flags, status;
414 cur = first = sc->tx.cur;
415 map = sc->tx.buf_map[first].map;
418 error = bus_dmamap_load_mbuf_sg(sc->tx.buf_tag, map, m, segs,
419 &nsegs, BUS_DMA_NOWAIT);
420 if (error == EFBIG) {
421 m = m_collapse(m, M_NOWAIT, TX_MAX_SEGS);
423 device_printf(sc->dev, "awg_encap: m_collapse failed\n");
429 error = bus_dmamap_load_mbuf_sg(sc->tx.buf_tag, map, m,
430 segs, &nsegs, BUS_DMA_NOWAIT);
437 device_printf(sc->dev, "awg_encap: bus_dmamap_load_mbuf_sg failed\n");
446 if (sc->tx.queued + nsegs > TX_DESC_COUNT) {
447 bus_dmamap_unload(sc->tx.buf_tag, map);
451 bus_dmamap_sync(sc->tx.buf_tag, map, BUS_DMASYNC_PREWRITE);
455 if ((m->m_pkthdr.csum_flags & CSUM_IP) != 0) {
456 if ((m->m_pkthdr.csum_flags & (CSUM_TCP|CSUM_UDP)) != 0)
457 csum_flags = TX_CHECKSUM_CTL_FULL;
459 csum_flags = TX_CHECKSUM_CTL_IP;
460 flags |= (csum_flags << TX_CHECKSUM_CTL_SHIFT);
463 for (i = 0; i < nsegs; i++) {
465 if (i == nsegs - 1) {
466 flags |= TX_LAST_DESC;
468 * Can only request TX completion
469 * interrupt on last descriptor.
471 if (sc->tx.segs >= awg_tx_interval) {
477 sc->tx.desc_ring[cur].addr = htole32((uint32_t)segs[i].ds_addr);
478 sc->tx.desc_ring[cur].size = htole32(flags | segs[i].ds_len);
479 sc->tx.desc_ring[cur].status = htole32(status);
481 flags &= ~TX_FIR_DESC;
483 * Setting of the valid bit in the first descriptor is
484 * deferred until the whole chain is fully set up.
486 status = TX_DESC_CTL;
494 /* Store mapping and mbuf in the last segment */
495 last = TX_SKIP(cur, TX_DESC_COUNT - 1);
496 sc->tx.buf_map[first].map = sc->tx.buf_map[last].map;
497 sc->tx.buf_map[last].map = map;
498 sc->tx.buf_map[last].mbuf = m;
501 * The whole mbuf chain has been DMA mapped,
502 * fix the first descriptor.
504 sc->tx.desc_ring[first].status = htole32(TX_DESC_CTL);
510 awg_clean_txbuf(struct awg_softc *sc, int index)
512 struct awg_bufmap *bmap;
516 bmap = &sc->tx.buf_map[index];
517 if (bmap->mbuf != NULL) {
518 bus_dmamap_sync(sc->tx.buf_tag, bmap->map,
519 BUS_DMASYNC_POSTWRITE);
520 bus_dmamap_unload(sc->tx.buf_tag, bmap->map);
527 awg_setup_rxdesc(struct awg_softc *sc, int index, bus_addr_t paddr)
529 uint32_t status, size;
531 status = RX_DESC_CTL;
534 sc->rx.desc_ring[index].addr = htole32((uint32_t)paddr);
535 sc->rx.desc_ring[index].size = htole32(size);
536 sc->rx.desc_ring[index].status = htole32(status);
540 awg_reuse_rxdesc(struct awg_softc *sc, int index)
543 sc->rx.desc_ring[index].status = htole32(RX_DESC_CTL);
547 awg_newbuf_rx(struct awg_softc *sc, int index)
550 bus_dma_segment_t seg;
554 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
558 m->m_pkthdr.len = m->m_len = m->m_ext.ext_size;
559 m_adj(m, ETHER_ALIGN);
561 if (bus_dmamap_load_mbuf_sg(sc->rx.buf_tag, sc->rx.buf_spare_map,
562 m, &seg, &nsegs, BUS_DMA_NOWAIT) != 0) {
567 if (sc->rx.buf_map[index].mbuf != NULL) {
568 bus_dmamap_sync(sc->rx.buf_tag, sc->rx.buf_map[index].map,
569 BUS_DMASYNC_POSTREAD);
570 bus_dmamap_unload(sc->rx.buf_tag, sc->rx.buf_map[index].map);
572 map = sc->rx.buf_map[index].map;
573 sc->rx.buf_map[index].map = sc->rx.buf_spare_map;
574 sc->rx.buf_spare_map = map;
575 bus_dmamap_sync(sc->rx.buf_tag, sc->rx.buf_map[index].map,
576 BUS_DMASYNC_PREREAD);
578 sc->rx.buf_map[index].mbuf = m;
579 awg_setup_rxdesc(sc, index, seg.ds_addr);
585 awg_start_locked(struct awg_softc *sc)
592 AWG_ASSERT_LOCKED(sc);
599 if ((if_getdrvflags(ifp) & (IFF_DRV_RUNNING|IFF_DRV_OACTIVE)) !=
603 for (cnt = 0; ; cnt++) {
608 err = awg_encap(sc, &m);
611 if_setdrvflagbits(ifp, IFF_DRV_OACTIVE, 0);
613 if_sendq_prepend(ifp, m);
620 bus_dmamap_sync(sc->tx.desc_tag, sc->tx.desc_map,
621 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
623 /* Start and run TX DMA */
624 val = RD4(sc, EMAC_TX_CTL_1);
625 WR4(sc, EMAC_TX_CTL_1, val | TX_DMA_START);
632 struct awg_softc *sc;
634 sc = if_getsoftc(ifp);
637 awg_start_locked(sc);
642 awg_tick(void *softc)
644 struct awg_softc *sc;
645 struct mii_data *mii;
651 mii = device_get_softc(sc->miibus);
653 AWG_ASSERT_LOCKED(sc);
655 if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) == 0)
660 if (sc->link && !link)
661 awg_start_locked(sc);
663 callout_reset(&sc->stat_ch, hz, awg_tick, sc);
666 /* Bit Reversal - http://aggregate.org/MAGIC/#Bit%20Reversal */
670 x = (((x & 0xaaaaaaaa) >> 1) | ((x & 0x55555555) << 1));
671 x = (((x & 0xcccccccc) >> 2) | ((x & 0x33333333) << 2));
672 x = (((x & 0xf0f0f0f0) >> 4) | ((x & 0x0f0f0f0f) << 4));
673 x = (((x & 0xff00ff00) >> 8) | ((x & 0x00ff00ff) << 8));
675 return (x >> 16) | (x << 16);
679 awg_setup_rxfilter(struct awg_softc *sc)
681 uint32_t val, crc, hashreg, hashbit, hash[2], machi, maclo;
682 int mc_count, mcnt, i;
683 uint8_t *eaddr, *mta;
686 AWG_ASSERT_LOCKED(sc);
690 hash[0] = hash[1] = 0;
692 mc_count = if_multiaddr_count(ifp, -1);
694 if (if_getflags(ifp) & IFF_PROMISC)
695 val |= DIS_ADDR_FILTER;
696 else if (if_getflags(ifp) & IFF_ALLMULTI) {
697 val |= RX_ALL_MULTICAST;
698 hash[0] = hash[1] = ~0;
699 } else if (mc_count > 0) {
700 val |= HASH_MULTICAST;
702 mta = malloc(sizeof(unsigned char) * ETHER_ADDR_LEN * mc_count,
706 "failed to allocate temporary multicast list\n");
710 if_multiaddr_array(ifp, mta, &mcnt, mc_count);
711 for (i = 0; i < mcnt; i++) {
712 crc = ether_crc32_le(mta + (i * ETHER_ADDR_LEN),
713 ETHER_ADDR_LEN) & 0x7f;
714 crc = bitrev32(~crc) >> 26;
715 hashreg = (crc >> 5);
716 hashbit = (crc & 0x1f);
717 hash[hashreg] |= (1 << hashbit);
723 /* Write our unicast address */
724 eaddr = IF_LLADDR(ifp);
725 machi = (eaddr[5] << 8) | eaddr[4];
726 maclo = (eaddr[3] << 24) | (eaddr[2] << 16) | (eaddr[1] << 8) |
728 WR4(sc, EMAC_ADDR_HIGH(0), machi);
729 WR4(sc, EMAC_ADDR_LOW(0), maclo);
731 /* Multicast hash filters */
732 WR4(sc, EMAC_RX_HASH_0, hash[1]);
733 WR4(sc, EMAC_RX_HASH_1, hash[0]);
735 /* RX frame filter config */
736 WR4(sc, EMAC_RX_FRM_FLT, val);
740 awg_enable_intr(struct awg_softc *sc)
742 /* Enable interrupts */
743 WR4(sc, EMAC_INT_EN, RX_INT_EN | TX_INT_EN | TX_BUF_UA_INT_EN);
747 awg_disable_intr(struct awg_softc *sc)
749 /* Disable interrupts */
750 WR4(sc, EMAC_INT_EN, 0);
754 awg_init_locked(struct awg_softc *sc)
756 struct mii_data *mii;
760 mii = device_get_softc(sc->miibus);
763 AWG_ASSERT_LOCKED(sc);
765 if (if_getdrvflags(ifp) & IFF_DRV_RUNNING)
768 awg_setup_rxfilter(sc);
770 /* Configure DMA burst length and priorities */
771 val = awg_burst_len << BASIC_CTL_BURST_LEN_SHIFT;
773 val |= BASIC_CTL_RX_TX_PRI;
774 WR4(sc, EMAC_BASIC_CTL_1, val);
776 /* Enable interrupts */
777 #ifdef DEVICE_POLLING
778 if ((if_getcapenable(ifp) & IFCAP_POLLING) == 0)
781 awg_disable_intr(sc);
786 /* Enable transmit DMA */
787 val = RD4(sc, EMAC_TX_CTL_1);
788 WR4(sc, EMAC_TX_CTL_1, val | TX_DMA_EN | TX_MD | TX_NEXT_FRAME);
790 /* Enable receive DMA */
791 val = RD4(sc, EMAC_RX_CTL_1);
792 WR4(sc, EMAC_RX_CTL_1, val | RX_DMA_EN | RX_MD);
794 /* Enable transmitter */
795 val = RD4(sc, EMAC_TX_CTL_0);
796 WR4(sc, EMAC_TX_CTL_0, val | TX_EN);
798 /* Enable receiver */
799 val = RD4(sc, EMAC_RX_CTL_0);
800 WR4(sc, EMAC_RX_CTL_0, val | RX_EN | CHECK_CRC);
802 if_setdrvflagbits(ifp, IFF_DRV_RUNNING, IFF_DRV_OACTIVE);
805 callout_reset(&sc->stat_ch, hz, awg_tick, sc);
809 awg_init(void *softc)
811 struct awg_softc *sc;
821 awg_stop(struct awg_softc *sc)
827 AWG_ASSERT_LOCKED(sc);
831 callout_stop(&sc->stat_ch);
833 /* Stop transmit DMA and flush data in the TX FIFO */
834 val = RD4(sc, EMAC_TX_CTL_1);
836 val |= FLUSH_TX_FIFO;
837 WR4(sc, EMAC_TX_CTL_1, val);
839 /* Disable transmitter */
840 val = RD4(sc, EMAC_TX_CTL_0);
841 WR4(sc, EMAC_TX_CTL_0, val & ~TX_EN);
843 /* Disable receiver */
844 val = RD4(sc, EMAC_RX_CTL_0);
845 WR4(sc, EMAC_RX_CTL_0, val & ~RX_EN);
847 /* Disable interrupts */
848 awg_disable_intr(sc);
850 /* Disable transmit DMA */
851 val = RD4(sc, EMAC_TX_CTL_1);
852 WR4(sc, EMAC_TX_CTL_1, val & ~TX_DMA_EN);
854 /* Disable receive DMA */
855 val = RD4(sc, EMAC_RX_CTL_1);
856 WR4(sc, EMAC_RX_CTL_1, val & ~RX_DMA_EN);
860 /* Finish handling transmitted buffers */
863 /* Release any untransmitted buffers. */
864 for (i = sc->tx.next; sc->tx.queued > 0; i = TX_NEXT(i)) {
865 val = le32toh(sc->tx.desc_ring[i].status);
866 if ((val & TX_DESC_CTL) != 0)
868 awg_clean_txbuf(sc, i);
871 for (; sc->tx.queued > 0; i = TX_NEXT(i)) {
872 sc->tx.desc_ring[i].status = 0;
873 awg_clean_txbuf(sc, i);
875 sc->tx.cur = sc->tx.next;
876 bus_dmamap_sync(sc->tx.desc_tag, sc->tx.desc_map,
877 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
879 /* Setup RX buffers for reuse */
880 bus_dmamap_sync(sc->rx.desc_tag, sc->rx.desc_map,
881 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
883 for (i = sc->rx.cur; ; i = RX_NEXT(i)) {
884 val = le32toh(sc->rx.desc_ring[i].status);
885 if ((val & RX_DESC_CTL) != 0)
887 awg_reuse_rxdesc(sc, i);
890 bus_dmamap_sync(sc->rx.desc_tag, sc->rx.desc_map,
891 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
893 if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
897 awg_rxintr(struct awg_softc *sc)
900 struct mbuf *m, *mh, *mt;
901 int error, index, len, cnt, npkt;
909 bus_dmamap_sync(sc->rx.desc_tag, sc->rx.desc_map,
910 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
912 for (index = sc->rx.cur; ; index = RX_NEXT(index)) {
913 status = le32toh(sc->rx.desc_ring[index].status);
914 if ((status & RX_DESC_CTL) != 0)
917 len = (status & RX_FRM_LEN) >> RX_FRM_LEN_SHIFT;
920 if ((status & (RX_NO_ENOUGH_BUF_ERR | RX_OVERFLOW_ERR)) != 0)
921 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
922 awg_reuse_rxdesc(sc, index);
926 m = sc->rx.buf_map[index].mbuf;
928 error = awg_newbuf_rx(sc, index);
930 if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
931 awg_reuse_rxdesc(sc, index);
935 m->m_pkthdr.rcvif = ifp;
936 m->m_pkthdr.len = len;
938 if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
940 if ((if_getcapenable(ifp) & IFCAP_RXCSUM) != 0 &&
941 (status & RX_FRM_TYPE) != 0) {
942 m->m_pkthdr.csum_flags = CSUM_IP_CHECKED;
943 if ((status & RX_HEADER_ERR) == 0)
944 m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
945 if ((status & RX_PAYLOAD_ERR) == 0) {
946 m->m_pkthdr.csum_flags |=
947 CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
948 m->m_pkthdr.csum_data = 0xffff;
961 if (cnt == awg_rx_batch) {
970 if (index != sc->rx.cur) {
971 bus_dmamap_sync(sc->rx.desc_tag, sc->rx.desc_map,
972 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
987 awg_txeof(struct awg_softc *sc)
989 struct emac_desc *desc;
990 uint32_t status, size;
994 AWG_ASSERT_LOCKED(sc);
996 bus_dmamap_sync(sc->tx.desc_tag, sc->tx.desc_map,
997 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
1002 for (i = sc->tx.next; sc->tx.queued > 0; i = TX_NEXT(i)) {
1003 desc = &sc->tx.desc_ring[i];
1004 status = le32toh(desc->status);
1005 if ((status & TX_DESC_CTL) != 0)
1007 size = le32toh(desc->size);
1008 if (size & TX_LAST_DESC) {
1009 if ((status & (TX_HEADER_ERR | TX_PAYLOAD_ERR)) != 0)
1010 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1012 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
1015 awg_clean_txbuf(sc, i);
1020 if_setdrvflagbits(ifp, 0, IFF_DRV_OACTIVE);
1027 struct awg_softc *sc;
1033 val = RD4(sc, EMAC_INT_STA);
1034 WR4(sc, EMAC_INT_STA, val);
1042 if (val & (TX_INT | TX_BUF_UA_INT)) {
1043 if (!if_sendq_empty(sc->ifp))
1044 awg_start_locked(sc);
1050 #ifdef DEVICE_POLLING
1052 awg_poll(if_t ifp, enum poll_cmd cmd, int count)
1054 struct awg_softc *sc;
1058 sc = if_getsoftc(ifp);
1063 if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) == 0) {
1068 rx_npkts = awg_rxintr(sc);
1070 if (!if_sendq_empty(ifp))
1071 awg_start_locked(sc);
1073 if (cmd == POLL_AND_CHECK_STATUS) {
1074 val = RD4(sc, EMAC_INT_STA);
1076 WR4(sc, EMAC_INT_STA, val);
1086 awg_ioctl(if_t ifp, u_long cmd, caddr_t data)
1088 struct awg_softc *sc;
1089 struct mii_data *mii;
1091 int flags, mask, error;
1093 sc = if_getsoftc(ifp);
1094 mii = device_get_softc(sc->miibus);
1095 ifr = (struct ifreq *)data;
1101 if (if_getflags(ifp) & IFF_UP) {
1102 if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
1103 flags = if_getflags(ifp) ^ sc->if_flags;
1104 if ((flags & (IFF_PROMISC|IFF_ALLMULTI)) != 0)
1105 awg_setup_rxfilter(sc);
1107 awg_init_locked(sc);
1109 if (if_getdrvflags(ifp) & IFF_DRV_RUNNING)
1112 sc->if_flags = if_getflags(ifp);
1117 if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
1119 awg_setup_rxfilter(sc);
1125 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, cmd);
1128 mask = ifr->ifr_reqcap ^ if_getcapenable(ifp);
1129 #ifdef DEVICE_POLLING
1130 if (mask & IFCAP_POLLING) {
1131 if ((ifr->ifr_reqcap & IFCAP_POLLING) != 0) {
1132 error = ether_poll_register(awg_poll, ifp);
1136 awg_disable_intr(sc);
1137 if_setcapenablebit(ifp, IFCAP_POLLING, 0);
1140 error = ether_poll_deregister(ifp);
1142 awg_enable_intr(sc);
1143 if_setcapenablebit(ifp, 0, IFCAP_POLLING);
1148 if (mask & IFCAP_VLAN_MTU)
1149 if_togglecapenable(ifp, IFCAP_VLAN_MTU);
1150 if (mask & IFCAP_RXCSUM)
1151 if_togglecapenable(ifp, IFCAP_RXCSUM);
1152 if (mask & IFCAP_TXCSUM)
1153 if_togglecapenable(ifp, IFCAP_TXCSUM);
1154 if ((if_getcapenable(ifp) & IFCAP_TXCSUM) != 0)
1155 if_sethwassistbits(ifp, CSUM_IP | CSUM_UDP | CSUM_TCP, 0);
1157 if_sethwassistbits(ifp, 0, CSUM_IP | CSUM_UDP | CSUM_TCP);
1160 error = ether_ioctl(ifp, cmd, data);
1168 syscon_read_emac_clk_reg(device_t dev)
1170 struct awg_softc *sc;
1172 sc = device_get_softc(dev);
1173 if (sc->syscon != NULL)
1174 return (SYSCON_READ_4(sc->syscon, EMAC_CLK_REG));
1175 else if (sc->res[_RES_SYSCON] != NULL)
1176 return (bus_read_4(sc->res[_RES_SYSCON], 0));
1182 syscon_write_emac_clk_reg(device_t dev, uint32_t val)
1184 struct awg_softc *sc;
1186 sc = device_get_softc(dev);
1187 if (sc->syscon != NULL)
1188 SYSCON_WRITE_4(sc->syscon, EMAC_CLK_REG, val);
1189 else if (sc->res[_RES_SYSCON] != NULL)
1190 bus_write_4(sc->res[_RES_SYSCON], 0, val);
1194 awg_get_phy_node(device_t dev)
1199 node = ofw_bus_get_node(dev);
1200 if (OF_getencprop(node, "phy-handle", (void *)&phy_handle,
1201 sizeof(phy_handle)) <= 0)
1204 return (OF_node_from_xref(phy_handle));
1208 awg_has_internal_phy(device_t dev)
1210 phandle_t node, phy_node;
1212 node = ofw_bus_get_node(dev);
1213 /* Legacy binding */
1214 if (OF_hasprop(node, "allwinner,use-internal-phy"))
1217 phy_node = awg_get_phy_node(dev);
1218 return (phy_node != 0 && ofw_bus_node_is_compatible(OF_parent(phy_node),
1219 "allwinner,sun8i-h3-mdio-internal") != 0);
1223 awg_parse_delay(device_t dev, uint32_t *tx_delay, uint32_t *rx_delay)
1228 if (tx_delay == NULL || rx_delay == NULL)
1230 *tx_delay = *rx_delay = 0;
1231 node = ofw_bus_get_node(dev);
1233 if (OF_getencprop(node, "tx-delay", &delay, sizeof(delay)) >= 0)
1235 else if (OF_getencprop(node, "allwinner,tx-delay-ps", &delay,
1236 sizeof(delay)) >= 0) {
1237 if ((delay % 100) != 0) {
1238 device_printf(dev, "tx-delay-ps is not a multiple of 100\n");
1241 *tx_delay = delay / 100;
1243 if (*tx_delay > 7) {
1244 device_printf(dev, "tx-delay out of range\n");
1248 if (OF_getencprop(node, "rx-delay", &delay, sizeof(delay)) >= 0)
1250 else if (OF_getencprop(node, "allwinner,rx-delay-ps", &delay,
1251 sizeof(delay)) >= 0) {
1252 if ((delay % 100) != 0) {
1253 device_printf(dev, "rx-delay-ps is not within documented domain\n");
1256 *rx_delay = delay / 100;
1258 if (*rx_delay > 31) {
1259 device_printf(dev, "rx-delay out of range\n");
1267 awg_setup_phy(device_t dev)
1269 struct awg_softc *sc;
1270 clk_t clk_tx, clk_tx_parent;
1271 const char *tx_parent_name;
1274 uint32_t reg, tx_delay, rx_delay;
1278 sc = device_get_softc(dev);
1279 node = ofw_bus_get_node(dev);
1282 if (OF_getprop_alloc(node, "phy-mode", (void **)&phy_type) == 0)
1285 if (sc->syscon != NULL || sc->res[_RES_SYSCON] != NULL)
1289 device_printf(dev, "PHY type: %s, conf mode: %s\n", phy_type,
1290 use_syscon ? "reg" : "clk");
1294 * Abstract away writing to syscon for devices like the pine64.
1295 * For the pine64, we get dtb from U-Boot and it still uses the
1296 * legacy setup of specifying syscon register in emac node
1297 * rather than as its own node and using an xref in emac.
1298 * These abstractions can go away once U-Boot dts is up-to-date.
1300 reg = syscon_read_emac_clk_reg(dev);
1301 reg &= ~(EMAC_CLK_PIT | EMAC_CLK_SRC | EMAC_CLK_RMII_EN);
1302 if (strncmp(phy_type, "rgmii", 5) == 0)
1303 reg |= EMAC_CLK_PIT_RGMII | EMAC_CLK_SRC_RGMII;
1304 else if (strcmp(phy_type, "rmii") == 0)
1305 reg |= EMAC_CLK_RMII_EN;
1307 reg |= EMAC_CLK_PIT_MII | EMAC_CLK_SRC_MII;
1310 * Fail attach if we fail to parse either of the delay
1311 * parameters. If we don't have the proper delay to write to
1312 * syscon, then awg likely won't function properly anyways.
1313 * Lack of delay is not an error!
1315 error = awg_parse_delay(dev, &tx_delay, &rx_delay);
1319 /* Default to 0 and we'll increase it if we need to. */
1320 reg &= ~(EMAC_CLK_ETXDC | EMAC_CLK_ERXDC);
1322 reg |= (tx_delay << EMAC_CLK_ETXDC_SHIFT);
1324 reg |= (rx_delay << EMAC_CLK_ERXDC_SHIFT);
1326 if (sc->type == EMAC_H3) {
1327 if (awg_has_internal_phy(dev)) {
1328 reg |= EMAC_CLK_EPHY_SELECT;
1329 reg &= ~EMAC_CLK_EPHY_SHUTDOWN;
1330 if (OF_hasprop(node,
1331 "allwinner,leds-active-low"))
1332 reg |= EMAC_CLK_EPHY_LED_POL;
1334 reg &= ~EMAC_CLK_EPHY_LED_POL;
1336 /* Set internal PHY addr to 1 */
1337 reg &= ~EMAC_CLK_EPHY_ADDR;
1338 reg |= (1 << EMAC_CLK_EPHY_ADDR_SHIFT);
1340 reg &= ~EMAC_CLK_EPHY_SELECT;
1345 device_printf(dev, "EMAC clock: 0x%08x\n", reg);
1346 syscon_write_emac_clk_reg(dev, reg);
1348 if (strncmp(phy_type, "rgmii", 5) == 0)
1349 tx_parent_name = "emac_int_tx";
1351 tx_parent_name = "mii_phy_tx";
1353 /* Get the TX clock */
1354 error = clk_get_by_ofw_name(dev, 0, "tx", &clk_tx);
1356 device_printf(dev, "cannot get tx clock\n");
1360 /* Find the desired parent clock based on phy-mode property */
1361 error = clk_get_by_name(dev, tx_parent_name, &clk_tx_parent);
1363 device_printf(dev, "cannot get clock '%s'\n",
1368 /* Set TX clock parent */
1369 error = clk_set_parent_by_clk(clk_tx, clk_tx_parent);
1371 device_printf(dev, "cannot set tx clock parent\n");
1375 /* Enable TX clock */
1376 error = clk_enable(clk_tx);
1378 device_printf(dev, "cannot enable tx clock\n");
1386 OF_prop_free(phy_type);
1391 awg_setup_extres(device_t dev)
1393 struct awg_softc *sc;
1394 phandle_t node, phy_node;
1395 hwreset_t rst_ahb, rst_ephy;
1396 clk_t clk_ahb, clk_ephy;
1401 sc = device_get_softc(dev);
1402 rst_ahb = rst_ephy = NULL;
1403 clk_ahb = clk_ephy = NULL;
1405 node = ofw_bus_get_node(dev);
1406 phy_node = awg_get_phy_node(dev);
1408 if (phy_node == 0 && OF_hasprop(node, "phy-handle")) {
1410 device_printf(dev, "cannot get phy handle\n");
1414 /* Get AHB clock and reset resources */
1415 error = hwreset_get_by_ofw_name(dev, 0, "stmmaceth", &rst_ahb);
1417 error = hwreset_get_by_ofw_name(dev, 0, "ahb", &rst_ahb);
1419 device_printf(dev, "cannot get ahb reset\n");
1422 if (hwreset_get_by_ofw_name(dev, 0, "ephy", &rst_ephy) != 0)
1423 if (phy_node == 0 || hwreset_get_by_ofw_idx(dev, phy_node, 0,
1426 error = clk_get_by_ofw_name(dev, 0, "stmmaceth", &clk_ahb);
1428 error = clk_get_by_ofw_name(dev, 0, "ahb", &clk_ahb);
1430 device_printf(dev, "cannot get ahb clock\n");
1433 if (clk_get_by_ofw_name(dev, 0, "ephy", &clk_ephy) != 0)
1434 if (phy_node == 0 || clk_get_by_ofw_index(dev, phy_node, 0,
1438 if (OF_hasprop(node, "syscon") && syscon_get_by_ofw_property(dev, node,
1439 "syscon", &sc->syscon) != 0) {
1440 device_printf(dev, "cannot get syscon driver handle\n");
1444 /* Configure PHY for MII or RGMII mode */
1445 if (awg_setup_phy(dev) != 0)
1449 error = clk_enable(clk_ahb);
1451 device_printf(dev, "cannot enable ahb clock\n");
1454 if (clk_ephy != NULL) {
1455 error = clk_enable(clk_ephy);
1457 device_printf(dev, "cannot enable ephy clock\n");
1462 /* De-assert reset */
1463 error = hwreset_deassert(rst_ahb);
1465 device_printf(dev, "cannot de-assert ahb reset\n");
1468 if (rst_ephy != NULL) {
1470 * The ephy reset is left de-asserted by U-Boot. Assert it
1471 * here to make sure that we're in a known good state going
1472 * into the PHY reset.
1474 hwreset_assert(rst_ephy);
1475 error = hwreset_deassert(rst_ephy);
1477 device_printf(dev, "cannot de-assert ephy reset\n");
1482 /* Enable PHY regulator if applicable */
1483 if (regulator_get_by_ofw_property(dev, 0, "phy-supply", ®) == 0) {
1484 error = regulator_enable(reg);
1486 device_printf(dev, "cannot enable PHY regulator\n");
1491 /* Determine MDC clock divide ratio based on AHB clock */
1492 error = clk_get_freq(clk_ahb, &freq);
1494 device_printf(dev, "cannot get AHB clock frequency\n");
1497 div = freq / MDIO_FREQ;
1499 sc->mdc_div_ratio_m = MDC_DIV_RATIO_M_16;
1501 sc->mdc_div_ratio_m = MDC_DIV_RATIO_M_32;
1503 sc->mdc_div_ratio_m = MDC_DIV_RATIO_M_64;
1504 else if (div <= 128)
1505 sc->mdc_div_ratio_m = MDC_DIV_RATIO_M_128;
1507 device_printf(dev, "cannot determine MDC clock divide ratio\n");
1513 device_printf(dev, "AHB frequency %ju Hz, MDC div: 0x%x\n",
1514 (uintmax_t)freq, sc->mdc_div_ratio_m);
1520 regulator_release(reg);
1521 if (clk_ephy != NULL)
1522 clk_release(clk_ephy);
1523 if (clk_ahb != NULL)
1524 clk_release(clk_ahb);
1525 if (rst_ephy != NULL)
1526 hwreset_release(rst_ephy);
1527 if (rst_ahb != NULL)
1528 hwreset_release(rst_ahb);
1533 awg_get_eaddr(device_t dev, uint8_t *eaddr)
1535 struct awg_softc *sc;
1536 uint32_t maclo, machi, rnd;
1538 uint32_t rootkey_size;
1540 sc = device_get_softc(dev);
1542 machi = RD4(sc, EMAC_ADDR_HIGH(0)) & 0xffff;
1543 maclo = RD4(sc, EMAC_ADDR_LOW(0));
1545 rootkey_size = sizeof(rootkey);
1546 if (maclo == 0xffffffff && machi == 0xffff) {
1547 /* MAC address in hardware is invalid, create one */
1548 if (aw_sid_get_fuse(AW_SID_FUSE_ROOTKEY, rootkey,
1549 &rootkey_size) == 0 &&
1550 (rootkey[3] | rootkey[12] | rootkey[13] | rootkey[14] |
1551 rootkey[15]) != 0) {
1552 /* MAC address is derived from the root key in SID */
1553 maclo = (rootkey[13] << 24) | (rootkey[12] << 16) |
1554 (rootkey[3] << 8) | 0x02;
1555 machi = (rootkey[15] << 8) | rootkey[14];
1559 maclo = 0x00f2 | (rnd & 0xffff0000);
1560 machi = rnd & 0xffff;
1564 eaddr[0] = maclo & 0xff;
1565 eaddr[1] = (maclo >> 8) & 0xff;
1566 eaddr[2] = (maclo >> 16) & 0xff;
1567 eaddr[3] = (maclo >> 24) & 0xff;
1568 eaddr[4] = machi & 0xff;
1569 eaddr[5] = (machi >> 8) & 0xff;
1574 awg_dump_regs(device_t dev)
1576 static const struct {
1580 { "BASIC_CTL_0", EMAC_BASIC_CTL_0 },
1581 { "BASIC_CTL_1", EMAC_BASIC_CTL_1 },
1582 { "INT_STA", EMAC_INT_STA },
1583 { "INT_EN", EMAC_INT_EN },
1584 { "TX_CTL_0", EMAC_TX_CTL_0 },
1585 { "TX_CTL_1", EMAC_TX_CTL_1 },
1586 { "TX_FLOW_CTL", EMAC_TX_FLOW_CTL },
1587 { "TX_DMA_LIST", EMAC_TX_DMA_LIST },
1588 { "RX_CTL_0", EMAC_RX_CTL_0 },
1589 { "RX_CTL_1", EMAC_RX_CTL_1 },
1590 { "RX_DMA_LIST", EMAC_RX_DMA_LIST },
1591 { "RX_FRM_FLT", EMAC_RX_FRM_FLT },
1592 { "RX_HASH_0", EMAC_RX_HASH_0 },
1593 { "RX_HASH_1", EMAC_RX_HASH_1 },
1594 { "MII_CMD", EMAC_MII_CMD },
1595 { "ADDR_HIGH0", EMAC_ADDR_HIGH(0) },
1596 { "ADDR_LOW0", EMAC_ADDR_LOW(0) },
1597 { "TX_DMA_STA", EMAC_TX_DMA_STA },
1598 { "TX_DMA_CUR_DESC", EMAC_TX_DMA_CUR_DESC },
1599 { "TX_DMA_CUR_BUF", EMAC_TX_DMA_CUR_BUF },
1600 { "RX_DMA_STA", EMAC_RX_DMA_STA },
1601 { "RX_DMA_CUR_DESC", EMAC_RX_DMA_CUR_DESC },
1602 { "RX_DMA_CUR_BUF", EMAC_RX_DMA_CUR_BUF },
1603 { "RGMII_STA", EMAC_RGMII_STA },
1605 struct awg_softc *sc;
1608 sc = device_get_softc(dev);
1610 for (n = 0; n < nitems(regs); n++)
1611 device_printf(dev, " %-20s %08x\n", regs[n].name,
1612 RD4(sc, regs[n].reg));
1616 #define GPIO_ACTIVE_LOW 1
1619 awg_phy_reset(device_t dev)
1621 pcell_t gpio_prop[4], delay_prop[3];
1622 phandle_t node, gpio_node;
1624 uint32_t pin, flags;
1627 node = ofw_bus_get_node(dev);
1628 if (OF_getencprop(node, "allwinner,reset-gpio", gpio_prop,
1629 sizeof(gpio_prop)) <= 0)
1632 if (OF_getencprop(node, "allwinner,reset-delays-us", delay_prop,
1633 sizeof(delay_prop)) <= 0)
1636 gpio_node = OF_node_from_xref(gpio_prop[0]);
1637 if ((gpio = OF_device_from_xref(gpio_prop[0])) == NULL)
1640 if (GPIO_MAP_GPIOS(gpio, node, gpio_node, nitems(gpio_prop) - 1,
1641 gpio_prop + 1, &pin, &flags) != 0)
1644 pin_value = GPIO_PIN_LOW;
1645 if (OF_hasprop(node, "allwinner,reset-active-low"))
1646 pin_value = GPIO_PIN_HIGH;
1648 if (flags & GPIO_ACTIVE_LOW)
1649 pin_value = !pin_value;
1651 GPIO_PIN_SETFLAGS(gpio, pin, GPIO_PIN_OUTPUT);
1652 GPIO_PIN_SET(gpio, pin, pin_value);
1653 DELAY(delay_prop[0]);
1654 GPIO_PIN_SET(gpio, pin, !pin_value);
1655 DELAY(delay_prop[1]);
1656 GPIO_PIN_SET(gpio, pin, pin_value);
1657 DELAY(delay_prop[2]);
1663 awg_reset(device_t dev)
1665 struct awg_softc *sc;
1668 sc = device_get_softc(dev);
1670 /* Reset PHY if necessary */
1671 if (awg_phy_reset(dev) != 0) {
1672 device_printf(dev, "failed to reset PHY\n");
1676 /* Soft reset all registers and logic */
1677 WR4(sc, EMAC_BASIC_CTL_1, BASIC_CTL_SOFT_RST);
1679 /* Wait for soft reset bit to self-clear */
1680 for (retry = SOFT_RST_RETRY; retry > 0; retry--) {
1681 if ((RD4(sc, EMAC_BASIC_CTL_1) & BASIC_CTL_SOFT_RST) == 0)
1686 device_printf(dev, "soft reset timed out\n");
1697 awg_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1701 *(bus_addr_t *)arg = segs[0].ds_addr;
1705 awg_setup_dma(device_t dev)
1707 struct awg_softc *sc;
1710 sc = device_get_softc(dev);
1713 error = bus_dma_tag_create(
1714 bus_get_dma_tag(dev), /* Parent tag */
1715 DESC_ALIGN, 0, /* alignment, boundary */
1716 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
1717 BUS_SPACE_MAXADDR, /* highaddr */
1718 NULL, NULL, /* filter, filterarg */
1719 TX_DESC_SIZE, 1, /* maxsize, nsegs */
1720 TX_DESC_SIZE, /* maxsegsize */
1722 NULL, NULL, /* lockfunc, lockarg */
1725 device_printf(dev, "cannot create TX descriptor ring tag\n");
1729 error = bus_dmamem_alloc(sc->tx.desc_tag, (void **)&sc->tx.desc_ring,
1730 BUS_DMA_COHERENT | BUS_DMA_WAITOK | BUS_DMA_ZERO, &sc->tx.desc_map);
1732 device_printf(dev, "cannot allocate TX descriptor ring\n");
1736 error = bus_dmamap_load(sc->tx.desc_tag, sc->tx.desc_map,
1737 sc->tx.desc_ring, TX_DESC_SIZE, awg_dmamap_cb,
1738 &sc->tx.desc_ring_paddr, 0);
1740 device_printf(dev, "cannot load TX descriptor ring\n");
1744 for (i = 0; i < TX_DESC_COUNT; i++)
1745 sc->tx.desc_ring[i].next =
1746 htole32(sc->tx.desc_ring_paddr + DESC_OFF(TX_NEXT(i)));
1748 error = bus_dma_tag_create(
1749 bus_get_dma_tag(dev), /* Parent tag */
1750 1, 0, /* alignment, boundary */
1751 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
1752 BUS_SPACE_MAXADDR, /* highaddr */
1753 NULL, NULL, /* filter, filterarg */
1754 MCLBYTES, TX_MAX_SEGS, /* maxsize, nsegs */
1755 MCLBYTES, /* maxsegsize */
1757 NULL, NULL, /* lockfunc, lockarg */
1760 device_printf(dev, "cannot create TX buffer tag\n");
1765 for (i = 0; i < TX_DESC_COUNT; i++) {
1766 error = bus_dmamap_create(sc->tx.buf_tag, 0,
1767 &sc->tx.buf_map[i].map);
1769 device_printf(dev, "cannot create TX buffer map\n");
1775 error = bus_dma_tag_create(
1776 bus_get_dma_tag(dev), /* Parent tag */
1777 DESC_ALIGN, 0, /* alignment, boundary */
1778 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
1779 BUS_SPACE_MAXADDR, /* highaddr */
1780 NULL, NULL, /* filter, filterarg */
1781 RX_DESC_SIZE, 1, /* maxsize, nsegs */
1782 RX_DESC_SIZE, /* maxsegsize */
1784 NULL, NULL, /* lockfunc, lockarg */
1787 device_printf(dev, "cannot create RX descriptor ring tag\n");
1791 error = bus_dmamem_alloc(sc->rx.desc_tag, (void **)&sc->rx.desc_ring,
1792 BUS_DMA_COHERENT | BUS_DMA_WAITOK | BUS_DMA_ZERO, &sc->rx.desc_map);
1794 device_printf(dev, "cannot allocate RX descriptor ring\n");
1798 error = bus_dmamap_load(sc->rx.desc_tag, sc->rx.desc_map,
1799 sc->rx.desc_ring, RX_DESC_SIZE, awg_dmamap_cb,
1800 &sc->rx.desc_ring_paddr, 0);
1802 device_printf(dev, "cannot load RX descriptor ring\n");
1806 error = bus_dma_tag_create(
1807 bus_get_dma_tag(dev), /* Parent tag */
1808 1, 0, /* alignment, boundary */
1809 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
1810 BUS_SPACE_MAXADDR, /* highaddr */
1811 NULL, NULL, /* filter, filterarg */
1812 MCLBYTES, 1, /* maxsize, nsegs */
1813 MCLBYTES, /* maxsegsize */
1815 NULL, NULL, /* lockfunc, lockarg */
1818 device_printf(dev, "cannot create RX buffer tag\n");
1822 error = bus_dmamap_create(sc->rx.buf_tag, 0, &sc->rx.buf_spare_map);
1825 "cannot create RX buffer spare map\n");
1829 for (i = 0; i < RX_DESC_COUNT; i++) {
1830 sc->rx.desc_ring[i].next =
1831 htole32(sc->rx.desc_ring_paddr + DESC_OFF(RX_NEXT(i)));
1833 error = bus_dmamap_create(sc->rx.buf_tag, 0,
1834 &sc->rx.buf_map[i].map);
1836 device_printf(dev, "cannot create RX buffer map\n");
1839 sc->rx.buf_map[i].mbuf = NULL;
1840 error = awg_newbuf_rx(sc, i);
1842 device_printf(dev, "cannot create RX buffer\n");
1846 bus_dmamap_sync(sc->rx.desc_tag, sc->rx.desc_map,
1847 BUS_DMASYNC_PREWRITE);
1849 /* Write transmit and receive descriptor base address registers */
1850 WR4(sc, EMAC_TX_DMA_LIST, sc->tx.desc_ring_paddr);
1851 WR4(sc, EMAC_RX_DMA_LIST, sc->rx.desc_ring_paddr);
1857 awg_probe(device_t dev)
1859 if (!ofw_bus_status_okay(dev))
1862 if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
1865 device_set_desc(dev, "Allwinner Gigabit Ethernet");
1866 return (BUS_PROBE_DEFAULT);
1870 awg_attach(device_t dev)
1872 uint8_t eaddr[ETHER_ADDR_LEN];
1873 struct awg_softc *sc;
1876 sc = device_get_softc(dev);
1878 sc->type = ofw_bus_search_compatible(dev, compat_data)->ocd_data;
1880 if (bus_alloc_resources(dev, awg_spec, sc->res) != 0) {
1881 device_printf(dev, "cannot allocate resources for device\n");
1885 mtx_init(&sc->mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK, MTX_DEF);
1886 callout_init_mtx(&sc->stat_ch, &sc->mtx, 0);
1887 TASK_INIT(&sc->link_task, 0, awg_link_task, sc);
1889 /* Setup clocks and regulators */
1890 error = awg_setup_extres(dev);
1894 /* Read MAC address before resetting the chip */
1895 awg_get_eaddr(dev, eaddr);
1897 /* Soft reset EMAC core */
1898 error = awg_reset(dev);
1902 /* Setup DMA descriptors */
1903 error = awg_setup_dma(dev);
1907 /* Install interrupt handler */
1908 error = bus_setup_intr(dev, sc->res[_RES_IRQ],
1909 INTR_TYPE_NET | INTR_MPSAFE, NULL, awg_intr, sc, &sc->ih);
1911 device_printf(dev, "cannot setup interrupt handler\n");
1915 /* Setup ethernet interface */
1916 sc->ifp = if_alloc(IFT_ETHER);
1917 if_setsoftc(sc->ifp, sc);
1918 if_initname(sc->ifp, device_get_name(dev), device_get_unit(dev));
1919 if_setflags(sc->ifp, IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST);
1920 if_setstartfn(sc->ifp, awg_start);
1921 if_setioctlfn(sc->ifp, awg_ioctl);
1922 if_setinitfn(sc->ifp, awg_init);
1923 if_setsendqlen(sc->ifp, TX_DESC_COUNT - 1);
1924 if_setsendqready(sc->ifp);
1925 if_sethwassist(sc->ifp, CSUM_IP | CSUM_UDP | CSUM_TCP);
1926 if_setcapabilities(sc->ifp, IFCAP_VLAN_MTU | IFCAP_HWCSUM);
1927 if_setcapenable(sc->ifp, if_getcapabilities(sc->ifp));
1928 #ifdef DEVICE_POLLING
1929 if_setcapabilitiesbit(sc->ifp, IFCAP_POLLING, 0);
1932 /* Attach MII driver */
1933 error = mii_attach(dev, &sc->miibus, sc->ifp, awg_media_change,
1934 awg_media_status, BMSR_DEFCAPMASK, MII_PHY_ANY, MII_OFFSET_ANY,
1937 device_printf(dev, "cannot attach PHY\n");
1941 /* Attach ethernet interface */
1942 ether_ifattach(sc->ifp, eaddr);
1947 static device_method_t awg_methods[] = {
1948 /* Device interface */
1949 DEVMETHOD(device_probe, awg_probe),
1950 DEVMETHOD(device_attach, awg_attach),
1953 DEVMETHOD(miibus_readreg, awg_miibus_readreg),
1954 DEVMETHOD(miibus_writereg, awg_miibus_writereg),
1955 DEVMETHOD(miibus_statchg, awg_miibus_statchg),
1960 static driver_t awg_driver = {
1963 sizeof(struct awg_softc),
1966 static devclass_t awg_devclass;
1968 DRIVER_MODULE(awg, simplebus, awg_driver, awg_devclass, 0, 0);
1969 DRIVER_MODULE(miibus, awg, miibus_driver, miibus_devclass, 0, 0);
1970 MODULE_DEPEND(awg, ether, 1, 1, 1);
1971 MODULE_DEPEND(awg, miibus, 1, 1, 1);
1972 MODULE_DEPEND(awg, aw_sid, 1, 1, 1);
1973 SIMPLEBUS_PNP_INFO(compat_data);