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>
73 #include "miibus_if.h"
76 #define RD4(sc, reg) bus_read_4((sc)->res[_RES_EMAC], (reg))
77 #define WR4(sc, reg, val) bus_write_4((sc)->res[_RES_EMAC], (reg), (val))
79 #define AWG_LOCK(sc) mtx_lock(&(sc)->mtx)
80 #define AWG_UNLOCK(sc) mtx_unlock(&(sc)->mtx);
81 #define AWG_ASSERT_LOCKED(sc) mtx_assert(&(sc)->mtx, MA_OWNED)
82 #define AWG_ASSERT_UNLOCKED(sc) mtx_assert(&(sc)->mtx, MA_NOTOWNED)
85 #define TX_DESC_COUNT 1024
86 #define TX_DESC_SIZE (sizeof(struct emac_desc) * TX_DESC_COUNT)
87 #define RX_DESC_COUNT 256
88 #define RX_DESC_SIZE (sizeof(struct emac_desc) * RX_DESC_COUNT)
90 #define DESC_OFF(n) ((n) * sizeof(struct emac_desc))
91 #define TX_NEXT(n) (((n) + 1) & (TX_DESC_COUNT - 1))
92 #define TX_SKIP(n, o) (((n) + (o)) & (TX_DESC_COUNT - 1))
93 #define RX_NEXT(n) (((n) + 1) & (RX_DESC_COUNT - 1))
95 #define TX_MAX_SEGS 10
97 #define SOFT_RST_RETRY 1000
98 #define MII_BUSY_RETRY 1000
99 #define MDIO_FREQ 2500000
101 #define BURST_LEN_DEFAULT 8
102 #define RX_TX_PRI_DEFAULT 0
103 #define PAUSE_TIME_DEFAULT 0x400
104 #define TX_INTERVAL_DEFAULT 64
105 #define RX_BATCH_DEFAULT 64
107 /* syscon EMAC clock register */
108 #define EMAC_CLK_EPHY_ADDR (0x1f << 20) /* H3 */
109 #define EMAC_CLK_EPHY_ADDR_SHIFT 20
110 #define EMAC_CLK_EPHY_LED_POL (1 << 17) /* H3 */
111 #define EMAC_CLK_EPHY_SHUTDOWN (1 << 16) /* H3 */
112 #define EMAC_CLK_EPHY_SELECT (1 << 15) /* H3 */
113 #define EMAC_CLK_RMII_EN (1 << 13)
114 #define EMAC_CLK_ETXDC (0x7 << 10)
115 #define EMAC_CLK_ETXDC_SHIFT 10
116 #define EMAC_CLK_ERXDC (0x1f << 5)
117 #define EMAC_CLK_ERXDC_SHIFT 5
118 #define EMAC_CLK_PIT (0x1 << 2)
119 #define EMAC_CLK_PIT_MII (0 << 2)
120 #define EMAC_CLK_PIT_RGMII (1 << 2)
121 #define EMAC_CLK_SRC (0x3 << 0)
122 #define EMAC_CLK_SRC_MII (0 << 0)
123 #define EMAC_CLK_SRC_EXT_RGMII (1 << 0)
124 #define EMAC_CLK_SRC_RGMII (2 << 0)
126 /* Burst length of RX and TX DMA transfers */
127 static int awg_burst_len = BURST_LEN_DEFAULT;
128 TUNABLE_INT("hw.awg.burst_len", &awg_burst_len);
130 /* RX / TX DMA priority. If 1, RX DMA has priority over TX DMA. */
131 static int awg_rx_tx_pri = RX_TX_PRI_DEFAULT;
132 TUNABLE_INT("hw.awg.rx_tx_pri", &awg_rx_tx_pri);
134 /* Pause time field in the transmitted control frame */
135 static int awg_pause_time = PAUSE_TIME_DEFAULT;
136 TUNABLE_INT("hw.awg.pause_time", &awg_pause_time);
138 /* Request a TX interrupt every <n> descriptors */
139 static int awg_tx_interval = TX_INTERVAL_DEFAULT;
140 TUNABLE_INT("hw.awg.tx_interval", &awg_tx_interval);
142 /* Maximum number of mbufs to send to if_input */
143 static int awg_rx_batch = RX_BATCH_DEFAULT;
144 TUNABLE_INT("hw.awg.rx_batch", &awg_rx_batch);
151 static struct ofw_compat_data compat_data[] = {
152 { "allwinner,sun8i-a83t-emac", EMAC_A83T },
153 { "allwinner,sun8i-h3-emac", EMAC_H3 },
163 bus_dma_tag_t desc_tag;
164 bus_dmamap_t desc_map;
165 struct emac_desc *desc_ring;
166 bus_addr_t desc_ring_paddr;
167 bus_dma_tag_t buf_tag;
168 struct awg_bufmap buf_map[TX_DESC_COUNT];
169 u_int cur, next, queued;
173 bus_dma_tag_t desc_tag;
174 bus_dmamap_t desc_map;
175 struct emac_desc *desc_ring;
176 bus_addr_t desc_ring_paddr;
177 bus_dma_tag_t buf_tag;
178 struct awg_bufmap buf_map[RX_DESC_COUNT];
190 struct resource *res[_RES_NITEMS];
194 struct callout stat_ch;
195 struct task link_task;
197 u_int mdc_div_ratio_m;
202 struct awg_txring tx;
203 struct awg_rxring rx;
206 static struct resource_spec awg_spec[] = {
207 { SYS_RES_MEMORY, 0, RF_ACTIVE },
208 { SYS_RES_IRQ, 0, RF_ACTIVE },
209 { SYS_RES_MEMORY, 1, RF_ACTIVE | RF_OPTIONAL },
214 awg_miibus_readreg(device_t dev, int phy, int reg)
216 struct awg_softc *sc;
219 sc = device_get_softc(dev);
222 WR4(sc, EMAC_MII_CMD,
223 (sc->mdc_div_ratio_m << MDC_DIV_RATIO_M_SHIFT) |
224 (phy << PHY_ADDR_SHIFT) |
225 (reg << PHY_REG_ADDR_SHIFT) |
227 for (retry = MII_BUSY_RETRY; retry > 0; retry--) {
228 if ((RD4(sc, EMAC_MII_CMD) & MII_BUSY) == 0) {
229 val = RD4(sc, EMAC_MII_DATA);
236 device_printf(dev, "phy read timeout, phy=%d reg=%d\n",
243 awg_miibus_writereg(device_t dev, int phy, int reg, int val)
245 struct awg_softc *sc;
248 sc = device_get_softc(dev);
250 WR4(sc, EMAC_MII_DATA, val);
251 WR4(sc, EMAC_MII_CMD,
252 (sc->mdc_div_ratio_m << MDC_DIV_RATIO_M_SHIFT) |
253 (phy << PHY_ADDR_SHIFT) |
254 (reg << PHY_REG_ADDR_SHIFT) |
256 for (retry = MII_BUSY_RETRY; retry > 0; retry--) {
257 if ((RD4(sc, EMAC_MII_CMD) & MII_BUSY) == 0)
263 device_printf(dev, "phy write timeout, phy=%d reg=%d\n",
270 awg_update_link_locked(struct awg_softc *sc)
272 struct mii_data *mii;
275 AWG_ASSERT_LOCKED(sc);
277 if ((if_getdrvflags(sc->ifp) & IFF_DRV_RUNNING) == 0)
279 mii = device_get_softc(sc->miibus);
281 if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
282 (IFM_ACTIVE | IFM_AVALID)) {
283 switch (IFM_SUBTYPE(mii->mii_media_active)) {
300 val = RD4(sc, EMAC_BASIC_CTL_0);
301 val &= ~(BASIC_CTL_SPEED | BASIC_CTL_DUPLEX);
303 if (IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_T ||
304 IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_SX)
305 val |= BASIC_CTL_SPEED_1000 << BASIC_CTL_SPEED_SHIFT;
306 else if (IFM_SUBTYPE(mii->mii_media_active) == IFM_100_TX)
307 val |= BASIC_CTL_SPEED_100 << BASIC_CTL_SPEED_SHIFT;
309 val |= BASIC_CTL_SPEED_10 << BASIC_CTL_SPEED_SHIFT;
311 if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) != 0)
312 val |= BASIC_CTL_DUPLEX;
314 WR4(sc, EMAC_BASIC_CTL_0, val);
316 val = RD4(sc, EMAC_RX_CTL_0);
317 val &= ~RX_FLOW_CTL_EN;
318 if ((IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_RXPAUSE) != 0)
319 val |= RX_FLOW_CTL_EN;
320 WR4(sc, EMAC_RX_CTL_0, val);
322 val = RD4(sc, EMAC_TX_FLOW_CTL);
323 val &= ~(PAUSE_TIME|TX_FLOW_CTL_EN);
324 if ((IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_TXPAUSE) != 0)
325 val |= TX_FLOW_CTL_EN;
326 if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) != 0)
327 val |= awg_pause_time << PAUSE_TIME_SHIFT;
328 WR4(sc, EMAC_TX_FLOW_CTL, val);
332 awg_link_task(void *arg, int pending)
334 struct awg_softc *sc;
339 awg_update_link_locked(sc);
344 awg_miibus_statchg(device_t dev)
346 struct awg_softc *sc;
348 sc = device_get_softc(dev);
350 taskqueue_enqueue(taskqueue_swi, &sc->link_task);
354 awg_media_status(if_t ifp, struct ifmediareq *ifmr)
356 struct awg_softc *sc;
357 struct mii_data *mii;
359 sc = if_getsoftc(ifp);
360 mii = device_get_softc(sc->miibus);
364 ifmr->ifm_active = mii->mii_media_active;
365 ifmr->ifm_status = mii->mii_media_status;
370 awg_media_change(if_t ifp)
372 struct awg_softc *sc;
373 struct mii_data *mii;
376 sc = if_getsoftc(ifp);
377 mii = device_get_softc(sc->miibus);
380 error = mii_mediachg(mii);
387 awg_setup_txdesc(struct awg_softc *sc, int index, int flags, bus_addr_t paddr,
390 uint32_t status, size;
392 if (paddr == 0 || len == 0) {
397 status = TX_DESC_CTL;
399 if ((index & (awg_tx_interval - 1)) == 0)
404 sc->tx.desc_ring[index].addr = htole32((uint32_t)paddr);
405 sc->tx.desc_ring[index].size = htole32(size);
406 sc->tx.desc_ring[index].status = htole32(status);
410 awg_setup_txbuf(struct awg_softc *sc, int index, struct mbuf **mp)
412 bus_dma_segment_t segs[TX_MAX_SEGS];
413 int error, nsegs, cur, i, flags;
418 error = bus_dmamap_load_mbuf_sg(sc->tx.buf_tag,
419 sc->tx.buf_map[index].map, m, segs, &nsegs, BUS_DMA_NOWAIT);
420 if (error == EFBIG) {
421 m = m_collapse(m, M_NOWAIT, TX_MAX_SEGS);
425 error = bus_dmamap_load_mbuf_sg(sc->tx.buf_tag,
426 sc->tx.buf_map[index].map, m, segs, &nsegs, BUS_DMA_NOWAIT);
431 bus_dmamap_sync(sc->tx.buf_tag, sc->tx.buf_map[index].map,
432 BUS_DMASYNC_PREWRITE);
435 if ((m->m_pkthdr.csum_flags & CSUM_IP) != 0) {
436 if ((m->m_pkthdr.csum_flags & (CSUM_TCP|CSUM_UDP)) != 0)
437 csum_flags = TX_CHECKSUM_CTL_FULL;
439 csum_flags = TX_CHECKSUM_CTL_IP;
440 flags |= (csum_flags << TX_CHECKSUM_CTL_SHIFT);
443 for (cur = index, i = 0; i < nsegs; i++) {
444 sc->tx.buf_map[cur].mbuf = (i == 0 ? m : NULL);
446 flags |= TX_LAST_DESC;
447 awg_setup_txdesc(sc, cur, flags, segs[i].ds_addr,
449 flags &= ~TX_FIR_DESC;
457 awg_setup_rxdesc(struct awg_softc *sc, int index, bus_addr_t paddr)
459 uint32_t status, size;
461 status = RX_DESC_CTL;
464 sc->rx.desc_ring[index].addr = htole32((uint32_t)paddr);
465 sc->rx.desc_ring[index].size = htole32(size);
466 sc->rx.desc_ring[index].next =
467 htole32(sc->rx.desc_ring_paddr + DESC_OFF(RX_NEXT(index)));
468 sc->rx.desc_ring[index].status = htole32(status);
472 awg_setup_rxbuf(struct awg_softc *sc, int index, struct mbuf *m)
474 bus_dma_segment_t seg;
477 m_adj(m, ETHER_ALIGN);
479 error = bus_dmamap_load_mbuf_sg(sc->rx.buf_tag,
480 sc->rx.buf_map[index].map, m, &seg, &nsegs, 0);
484 bus_dmamap_sync(sc->rx.buf_tag, sc->rx.buf_map[index].map,
485 BUS_DMASYNC_PREREAD);
487 sc->rx.buf_map[index].mbuf = m;
488 awg_setup_rxdesc(sc, index, seg.ds_addr);
494 awg_alloc_mbufcl(struct awg_softc *sc)
498 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
500 m->m_pkthdr.len = m->m_len = m->m_ext.ext_size;
506 awg_start_locked(struct awg_softc *sc)
513 AWG_ASSERT_LOCKED(sc);
520 if ((if_getdrvflags(ifp) & (IFF_DRV_RUNNING|IFF_DRV_OACTIVE)) !=
524 for (cnt = 0; ; cnt++) {
525 if (sc->tx.queued >= TX_DESC_COUNT - TX_MAX_SEGS) {
526 if_setdrvflagbits(ifp, IFF_DRV_OACTIVE, 0);
534 nsegs = awg_setup_txbuf(sc, sc->tx.cur, &m);
536 if_sendq_prepend(ifp, m);
540 sc->tx.cur = TX_SKIP(sc->tx.cur, nsegs);
544 bus_dmamap_sync(sc->tx.desc_tag, sc->tx.desc_map,
545 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
547 /* Start and run TX DMA */
548 val = RD4(sc, EMAC_TX_CTL_1);
549 WR4(sc, EMAC_TX_CTL_1, val | TX_DMA_START);
556 struct awg_softc *sc;
558 sc = if_getsoftc(ifp);
561 awg_start_locked(sc);
566 awg_tick(void *softc)
568 struct awg_softc *sc;
569 struct mii_data *mii;
575 mii = device_get_softc(sc->miibus);
577 AWG_ASSERT_LOCKED(sc);
579 if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) == 0)
584 if (sc->link && !link)
585 awg_start_locked(sc);
587 callout_reset(&sc->stat_ch, hz, awg_tick, sc);
590 /* Bit Reversal - http://aggregate.org/MAGIC/#Bit%20Reversal */
594 x = (((x & 0xaaaaaaaa) >> 1) | ((x & 0x55555555) << 1));
595 x = (((x & 0xcccccccc) >> 2) | ((x & 0x33333333) << 2));
596 x = (((x & 0xf0f0f0f0) >> 4) | ((x & 0x0f0f0f0f) << 4));
597 x = (((x & 0xff00ff00) >> 8) | ((x & 0x00ff00ff) << 8));
599 return (x >> 16) | (x << 16);
603 awg_setup_rxfilter(struct awg_softc *sc)
605 uint32_t val, crc, hashreg, hashbit, hash[2], machi, maclo;
606 int mc_count, mcnt, i;
607 uint8_t *eaddr, *mta;
610 AWG_ASSERT_LOCKED(sc);
614 hash[0] = hash[1] = 0;
616 mc_count = if_multiaddr_count(ifp, -1);
618 if (if_getflags(ifp) & IFF_PROMISC)
619 val |= DIS_ADDR_FILTER;
620 else if (if_getflags(ifp) & IFF_ALLMULTI) {
621 val |= RX_ALL_MULTICAST;
622 hash[0] = hash[1] = ~0;
623 } else if (mc_count > 0) {
624 val |= HASH_MULTICAST;
626 mta = malloc(sizeof(unsigned char) * ETHER_ADDR_LEN * mc_count,
630 "failed to allocate temporary multicast list\n");
634 if_multiaddr_array(ifp, mta, &mcnt, mc_count);
635 for (i = 0; i < mcnt; i++) {
636 crc = ether_crc32_le(mta + (i * ETHER_ADDR_LEN),
637 ETHER_ADDR_LEN) & 0x7f;
638 crc = bitrev32(~crc) >> 26;
639 hashreg = (crc >> 5);
640 hashbit = (crc & 0x1f);
641 hash[hashreg] |= (1 << hashbit);
647 /* Write our unicast address */
648 eaddr = IF_LLADDR(ifp);
649 machi = (eaddr[5] << 8) | eaddr[4];
650 maclo = (eaddr[3] << 24) | (eaddr[2] << 16) | (eaddr[1] << 8) |
652 WR4(sc, EMAC_ADDR_HIGH(0), machi);
653 WR4(sc, EMAC_ADDR_LOW(0), maclo);
655 /* Multicast hash filters */
656 WR4(sc, EMAC_RX_HASH_0, hash[1]);
657 WR4(sc, EMAC_RX_HASH_1, hash[0]);
659 /* RX frame filter config */
660 WR4(sc, EMAC_RX_FRM_FLT, val);
664 awg_enable_intr(struct awg_softc *sc)
666 /* Enable interrupts */
667 WR4(sc, EMAC_INT_EN, RX_INT_EN | TX_INT_EN | TX_BUF_UA_INT_EN);
671 awg_disable_intr(struct awg_softc *sc)
673 /* Disable interrupts */
674 WR4(sc, EMAC_INT_EN, 0);
678 awg_init_locked(struct awg_softc *sc)
680 struct mii_data *mii;
684 mii = device_get_softc(sc->miibus);
687 AWG_ASSERT_LOCKED(sc);
689 if (if_getdrvflags(ifp) & IFF_DRV_RUNNING)
692 awg_setup_rxfilter(sc);
694 /* Configure DMA burst length and priorities */
695 val = awg_burst_len << BASIC_CTL_BURST_LEN_SHIFT;
697 val |= BASIC_CTL_RX_TX_PRI;
698 WR4(sc, EMAC_BASIC_CTL_1, val);
700 /* Enable interrupts */
701 #ifdef DEVICE_POLLING
702 if ((if_getcapenable(ifp) & IFCAP_POLLING) == 0)
705 awg_disable_intr(sc);
710 /* Enable transmit DMA */
711 val = RD4(sc, EMAC_TX_CTL_1);
712 WR4(sc, EMAC_TX_CTL_1, val | TX_DMA_EN | TX_MD | TX_NEXT_FRAME);
714 /* Enable receive DMA */
715 val = RD4(sc, EMAC_RX_CTL_1);
716 WR4(sc, EMAC_RX_CTL_1, val | RX_DMA_EN | RX_MD);
718 /* Enable transmitter */
719 val = RD4(sc, EMAC_TX_CTL_0);
720 WR4(sc, EMAC_TX_CTL_0, val | TX_EN);
722 /* Enable receiver */
723 val = RD4(sc, EMAC_RX_CTL_0);
724 WR4(sc, EMAC_RX_CTL_0, val | RX_EN | CHECK_CRC);
726 if_setdrvflagbits(ifp, IFF_DRV_RUNNING, IFF_DRV_OACTIVE);
729 callout_reset(&sc->stat_ch, hz, awg_tick, sc);
733 awg_init(void *softc)
735 struct awg_softc *sc;
745 awg_stop(struct awg_softc *sc)
750 AWG_ASSERT_LOCKED(sc);
754 callout_stop(&sc->stat_ch);
756 /* Stop transmit DMA and flush data in the TX FIFO */
757 val = RD4(sc, EMAC_TX_CTL_1);
759 val |= FLUSH_TX_FIFO;
760 WR4(sc, EMAC_TX_CTL_1, val);
762 /* Disable transmitter */
763 val = RD4(sc, EMAC_TX_CTL_0);
764 WR4(sc, EMAC_TX_CTL_0, val & ~TX_EN);
766 /* Disable receiver */
767 val = RD4(sc, EMAC_RX_CTL_0);
768 WR4(sc, EMAC_RX_CTL_0, val & ~RX_EN);
770 /* Disable interrupts */
771 awg_disable_intr(sc);
773 /* Disable transmit DMA */
774 val = RD4(sc, EMAC_TX_CTL_1);
775 WR4(sc, EMAC_TX_CTL_1, val & ~TX_DMA_EN);
777 /* Disable receive DMA */
778 val = RD4(sc, EMAC_RX_CTL_1);
779 WR4(sc, EMAC_RX_CTL_1, val & ~RX_DMA_EN);
783 if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
787 awg_rxintr(struct awg_softc *sc)
790 struct mbuf *m, *m0, *mh, *mt;
791 int error, index, len, cnt, npkt;
799 bus_dmamap_sync(sc->rx.desc_tag, sc->rx.desc_map,
800 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
802 for (index = sc->rx.cur; ; index = RX_NEXT(index)) {
803 status = le32toh(sc->rx.desc_ring[index].status);
804 if ((status & RX_DESC_CTL) != 0)
807 bus_dmamap_sync(sc->rx.buf_tag, sc->rx.buf_map[index].map,
808 BUS_DMASYNC_POSTREAD);
809 bus_dmamap_unload(sc->rx.buf_tag, sc->rx.buf_map[index].map);
811 len = (status & RX_FRM_LEN) >> RX_FRM_LEN_SHIFT;
813 m = sc->rx.buf_map[index].mbuf;
814 m->m_pkthdr.rcvif = ifp;
815 m->m_pkthdr.len = len;
817 if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
819 if ((if_getcapenable(ifp) & IFCAP_RXCSUM) != 0 &&
820 (status & RX_FRM_TYPE) != 0) {
821 m->m_pkthdr.csum_flags = CSUM_IP_CHECKED;
822 if ((status & RX_HEADER_ERR) == 0)
823 m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
824 if ((status & RX_PAYLOAD_ERR) == 0) {
825 m->m_pkthdr.csum_flags |=
826 CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
827 m->m_pkthdr.csum_data = 0xffff;
840 if (cnt == awg_rx_batch) {
850 if ((m0 = awg_alloc_mbufcl(sc)) != NULL) {
851 error = awg_setup_rxbuf(sc, index, m0);
853 /* XXX hole in RX ring */
856 if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
859 if (index != sc->rx.cur) {
860 bus_dmamap_sync(sc->rx.desc_tag, sc->rx.desc_map,
861 BUS_DMASYNC_PREWRITE);
876 awg_txintr(struct awg_softc *sc)
878 struct awg_bufmap *bmap;
879 struct emac_desc *desc;
884 AWG_ASSERT_LOCKED(sc);
886 bus_dmamap_sync(sc->tx.desc_tag, sc->tx.desc_map,
887 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
890 for (i = sc->tx.next; sc->tx.queued > 0; i = TX_NEXT(i)) {
891 desc = &sc->tx.desc_ring[i];
892 status = le32toh(desc->status);
893 if ((status & TX_DESC_CTL) != 0)
895 bmap = &sc->tx.buf_map[i];
896 if (bmap->mbuf != NULL) {
897 bus_dmamap_sync(sc->tx.buf_tag, bmap->map,
898 BUS_DMASYNC_POSTWRITE);
899 bus_dmamap_unload(sc->tx.buf_tag, bmap->map);
903 awg_setup_txdesc(sc, i, 0, 0, 0);
904 if_setdrvflagbits(ifp, 0, IFF_DRV_OACTIVE);
905 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
910 bus_dmamap_sync(sc->tx.desc_tag, sc->tx.desc_map,
911 BUS_DMASYNC_PREWRITE);
917 struct awg_softc *sc;
923 val = RD4(sc, EMAC_INT_STA);
924 WR4(sc, EMAC_INT_STA, val);
929 if (val & (TX_INT|TX_BUF_UA_INT)) {
931 if (!if_sendq_empty(sc->ifp))
932 awg_start_locked(sc);
938 #ifdef DEVICE_POLLING
940 awg_poll(if_t ifp, enum poll_cmd cmd, int count)
942 struct awg_softc *sc;
946 sc = if_getsoftc(ifp);
951 if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) == 0) {
956 rx_npkts = awg_rxintr(sc);
958 if (!if_sendq_empty(ifp))
959 awg_start_locked(sc);
961 if (cmd == POLL_AND_CHECK_STATUS) {
962 val = RD4(sc, EMAC_INT_STA);
964 WR4(sc, EMAC_INT_STA, val);
974 awg_ioctl(if_t ifp, u_long cmd, caddr_t data)
976 struct awg_softc *sc;
977 struct mii_data *mii;
979 int flags, mask, error;
981 sc = if_getsoftc(ifp);
982 mii = device_get_softc(sc->miibus);
983 ifr = (struct ifreq *)data;
989 if (if_getflags(ifp) & IFF_UP) {
990 if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
991 flags = if_getflags(ifp) ^ sc->if_flags;
992 if ((flags & (IFF_PROMISC|IFF_ALLMULTI)) != 0)
993 awg_setup_rxfilter(sc);
997 if (if_getdrvflags(ifp) & IFF_DRV_RUNNING)
1000 sc->if_flags = if_getflags(ifp);
1005 if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
1007 awg_setup_rxfilter(sc);
1013 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, cmd);
1016 mask = ifr->ifr_reqcap ^ if_getcapenable(ifp);
1017 #ifdef DEVICE_POLLING
1018 if (mask & IFCAP_POLLING) {
1019 if ((ifr->ifr_reqcap & IFCAP_POLLING) != 0) {
1020 error = ether_poll_register(awg_poll, ifp);
1024 awg_disable_intr(sc);
1025 if_setcapenablebit(ifp, IFCAP_POLLING, 0);
1028 error = ether_poll_deregister(ifp);
1030 awg_enable_intr(sc);
1031 if_setcapenablebit(ifp, 0, IFCAP_POLLING);
1036 if (mask & IFCAP_VLAN_MTU)
1037 if_togglecapenable(ifp, IFCAP_VLAN_MTU);
1038 if (mask & IFCAP_RXCSUM)
1039 if_togglecapenable(ifp, IFCAP_RXCSUM);
1040 if (mask & IFCAP_TXCSUM)
1041 if_togglecapenable(ifp, IFCAP_TXCSUM);
1042 if ((if_getcapenable(ifp) & (IFCAP_RXCSUM|IFCAP_TXCSUM)) != 0)
1043 if_sethwassistbits(ifp, CSUM_IP, 0);
1045 if_sethwassistbits(ifp, 0, CSUM_IP);
1048 error = ether_ioctl(ifp, cmd, data);
1056 awg_setup_phy(device_t dev)
1058 struct awg_softc *sc;
1059 clk_t clk_tx, clk_tx_parent;
1060 const char *tx_parent_name;
1063 uint32_t reg, tx_delay, rx_delay;
1066 sc = device_get_softc(dev);
1067 node = ofw_bus_get_node(dev);
1069 if (OF_getprop_alloc(node, "phy-mode", 1, (void **)&phy_type) == 0)
1073 device_printf(dev, "PHY type: %s, conf mode: %s\n", phy_type,
1074 sc->res[_RES_SYSCON] != NULL ? "reg" : "clk");
1076 if (sc->res[_RES_SYSCON] != NULL) {
1077 reg = bus_read_4(sc->res[_RES_SYSCON], 0);
1078 reg &= ~(EMAC_CLK_PIT | EMAC_CLK_SRC | EMAC_CLK_RMII_EN);
1079 if (strcmp(phy_type, "rgmii") == 0)
1080 reg |= EMAC_CLK_PIT_RGMII | EMAC_CLK_SRC_RGMII;
1081 else if (strcmp(phy_type, "rmii") == 0)
1082 reg |= EMAC_CLK_RMII_EN;
1084 reg |= EMAC_CLK_PIT_MII | EMAC_CLK_SRC_MII;
1086 if (OF_getencprop(node, "tx-delay", &tx_delay,
1087 sizeof(tx_delay)) > 0) {
1088 reg &= ~EMAC_CLK_ETXDC;
1089 reg |= (tx_delay << EMAC_CLK_ETXDC_SHIFT);
1091 if (OF_getencprop(node, "rx-delay", &rx_delay,
1092 sizeof(rx_delay)) > 0) {
1093 reg &= ~EMAC_CLK_ERXDC;
1094 reg |= (rx_delay << EMAC_CLK_ERXDC_SHIFT);
1097 if (sc->type == EMAC_H3) {
1098 if (OF_hasprop(node, "allwinner,use-internal-phy")) {
1099 reg |= EMAC_CLK_EPHY_SELECT;
1100 reg &= ~EMAC_CLK_EPHY_SHUTDOWN;
1101 if (OF_hasprop(node,
1102 "allwinner,leds-active-low"))
1103 reg |= EMAC_CLK_EPHY_LED_POL;
1105 reg &= ~EMAC_CLK_EPHY_LED_POL;
1107 /* Set internal PHY addr to 1 */
1108 reg &= ~EMAC_CLK_EPHY_ADDR;
1109 reg |= (1 << EMAC_CLK_EPHY_ADDR_SHIFT);
1111 reg &= ~EMAC_CLK_EPHY_SELECT;
1116 device_printf(dev, "EMAC clock: 0x%08x\n", reg);
1117 bus_write_4(sc->res[_RES_SYSCON], 0, reg);
1119 if (strcmp(phy_type, "rgmii") == 0)
1120 tx_parent_name = "emac_int_tx";
1122 tx_parent_name = "mii_phy_tx";
1124 /* Get the TX clock */
1125 error = clk_get_by_ofw_name(dev, 0, "tx", &clk_tx);
1127 device_printf(dev, "cannot get tx clock\n");
1131 /* Find the desired parent clock based on phy-mode property */
1132 error = clk_get_by_name(dev, tx_parent_name, &clk_tx_parent);
1134 device_printf(dev, "cannot get clock '%s'\n",
1139 /* Set TX clock parent */
1140 error = clk_set_parent_by_clk(clk_tx, clk_tx_parent);
1142 device_printf(dev, "cannot set tx clock parent\n");
1146 /* Enable TX clock */
1147 error = clk_enable(clk_tx);
1149 device_printf(dev, "cannot enable tx clock\n");
1157 OF_prop_free(phy_type);
1162 awg_setup_extres(device_t dev)
1164 struct awg_softc *sc;
1165 hwreset_t rst_ahb, rst_ephy;
1166 clk_t clk_ahb, clk_ephy;
1172 sc = device_get_softc(dev);
1173 node = ofw_bus_get_node(dev);
1174 rst_ahb = rst_ephy = NULL;
1175 clk_ahb = clk_ephy = NULL;
1178 /* Get AHB clock and reset resources */
1179 error = hwreset_get_by_ofw_name(dev, 0, "ahb", &rst_ahb);
1181 device_printf(dev, "cannot get ahb reset\n");
1184 if (hwreset_get_by_ofw_name(dev, 0, "ephy", &rst_ephy) != 0)
1186 error = clk_get_by_ofw_name(dev, 0, "ahb", &clk_ahb);
1188 device_printf(dev, "cannot get ahb clock\n");
1191 if (clk_get_by_ofw_name(dev, 0, "ephy", &clk_ephy) != 0)
1194 /* Configure PHY for MII or RGMII mode */
1195 if (awg_setup_phy(dev) != 0)
1199 error = clk_enable(clk_ahb);
1201 device_printf(dev, "cannot enable ahb clock\n");
1204 if (clk_ephy != NULL) {
1205 error = clk_enable(clk_ephy);
1207 device_printf(dev, "cannot enable ephy clock\n");
1212 /* De-assert reset */
1213 error = hwreset_deassert(rst_ahb);
1215 device_printf(dev, "cannot de-assert ahb reset\n");
1218 if (rst_ephy != NULL) {
1219 error = hwreset_deassert(rst_ephy);
1221 device_printf(dev, "cannot de-assert ephy reset\n");
1226 /* Enable PHY regulator if applicable */
1227 if (regulator_get_by_ofw_property(dev, 0, "phy-supply", ®) == 0) {
1228 error = regulator_enable(reg);
1230 device_printf(dev, "cannot enable PHY regulator\n");
1235 /* Determine MDC clock divide ratio based on AHB clock */
1236 error = clk_get_freq(clk_ahb, &freq);
1238 device_printf(dev, "cannot get AHB clock frequency\n");
1241 div = freq / MDIO_FREQ;
1243 sc->mdc_div_ratio_m = MDC_DIV_RATIO_M_16;
1245 sc->mdc_div_ratio_m = MDC_DIV_RATIO_M_32;
1247 sc->mdc_div_ratio_m = MDC_DIV_RATIO_M_64;
1248 else if (div <= 128)
1249 sc->mdc_div_ratio_m = MDC_DIV_RATIO_M_128;
1251 device_printf(dev, "cannot determine MDC clock divide ratio\n");
1257 device_printf(dev, "AHB frequency %ju Hz, MDC div: 0x%x\n",
1258 (uintmax_t)freq, sc->mdc_div_ratio_m);
1264 regulator_release(reg);
1265 if (clk_ephy != NULL)
1266 clk_release(clk_ephy);
1267 if (clk_ahb != NULL)
1268 clk_release(clk_ahb);
1269 if (rst_ephy != NULL)
1270 hwreset_release(rst_ephy);
1271 if (rst_ahb != NULL)
1272 hwreset_release(rst_ahb);
1277 awg_get_eaddr(device_t dev, uint8_t *eaddr)
1279 struct awg_softc *sc;
1280 uint32_t maclo, machi, rnd;
1283 sc = device_get_softc(dev);
1285 machi = RD4(sc, EMAC_ADDR_HIGH(0)) & 0xffff;
1286 maclo = RD4(sc, EMAC_ADDR_LOW(0));
1288 if (maclo == 0xffffffff && machi == 0xffff) {
1289 /* MAC address in hardware is invalid, create one */
1290 if (aw_sid_get_rootkey(rootkey) == 0 &&
1291 (rootkey[3] | rootkey[12] | rootkey[13] | rootkey[14] |
1292 rootkey[15]) != 0) {
1293 /* MAC address is derived from the root key in SID */
1294 maclo = (rootkey[13] << 24) | (rootkey[12] << 16) |
1295 (rootkey[3] << 8) | 0x02;
1296 machi = (rootkey[15] << 8) | rootkey[14];
1300 maclo = 0x00f2 | (rnd & 0xffff0000);
1301 machi = rnd & 0xffff;
1305 eaddr[0] = maclo & 0xff;
1306 eaddr[1] = (maclo >> 8) & 0xff;
1307 eaddr[2] = (maclo >> 16) & 0xff;
1308 eaddr[3] = (maclo >> 24) & 0xff;
1309 eaddr[4] = machi & 0xff;
1310 eaddr[5] = (machi >> 8) & 0xff;
1315 awg_dump_regs(device_t dev)
1317 static const struct {
1321 { "BASIC_CTL_0", EMAC_BASIC_CTL_0 },
1322 { "BASIC_CTL_1", EMAC_BASIC_CTL_1 },
1323 { "INT_STA", EMAC_INT_STA },
1324 { "INT_EN", EMAC_INT_EN },
1325 { "TX_CTL_0", EMAC_TX_CTL_0 },
1326 { "TX_CTL_1", EMAC_TX_CTL_1 },
1327 { "TX_FLOW_CTL", EMAC_TX_FLOW_CTL },
1328 { "TX_DMA_LIST", EMAC_TX_DMA_LIST },
1329 { "RX_CTL_0", EMAC_RX_CTL_0 },
1330 { "RX_CTL_1", EMAC_RX_CTL_1 },
1331 { "RX_DMA_LIST", EMAC_RX_DMA_LIST },
1332 { "RX_FRM_FLT", EMAC_RX_FRM_FLT },
1333 { "RX_HASH_0", EMAC_RX_HASH_0 },
1334 { "RX_HASH_1", EMAC_RX_HASH_1 },
1335 { "MII_CMD", EMAC_MII_CMD },
1336 { "ADDR_HIGH0", EMAC_ADDR_HIGH(0) },
1337 { "ADDR_LOW0", EMAC_ADDR_LOW(0) },
1338 { "TX_DMA_STA", EMAC_TX_DMA_STA },
1339 { "TX_DMA_CUR_DESC", EMAC_TX_DMA_CUR_DESC },
1340 { "TX_DMA_CUR_BUF", EMAC_TX_DMA_CUR_BUF },
1341 { "RX_DMA_STA", EMAC_RX_DMA_STA },
1342 { "RX_DMA_CUR_DESC", EMAC_RX_DMA_CUR_DESC },
1343 { "RX_DMA_CUR_BUF", EMAC_RX_DMA_CUR_BUF },
1344 { "RGMII_STA", EMAC_RGMII_STA },
1346 struct awg_softc *sc;
1349 sc = device_get_softc(dev);
1351 for (n = 0; n < nitems(regs); n++)
1352 device_printf(dev, " %-20s %08x\n", regs[n].name,
1353 RD4(sc, regs[n].reg));
1357 #define GPIO_ACTIVE_LOW 1
1360 awg_phy_reset(device_t dev)
1362 pcell_t gpio_prop[4], delay_prop[3];
1363 phandle_t node, gpio_node;
1365 uint32_t pin, flags;
1368 node = ofw_bus_get_node(dev);
1369 if (OF_getencprop(node, "allwinner,reset-gpio", gpio_prop,
1370 sizeof(gpio_prop)) <= 0)
1373 if (OF_getencprop(node, "allwinner,reset-delays-us", delay_prop,
1374 sizeof(delay_prop)) <= 0)
1377 gpio_node = OF_node_from_xref(gpio_prop[0]);
1378 if ((gpio = OF_device_from_xref(gpio_prop[0])) == NULL)
1381 if (GPIO_MAP_GPIOS(gpio, node, gpio_node, nitems(gpio_prop) - 1,
1382 gpio_prop + 1, &pin, &flags) != 0)
1385 pin_value = GPIO_PIN_LOW;
1386 if (OF_hasprop(node, "allwinner,reset-active-low"))
1387 pin_value = GPIO_PIN_HIGH;
1389 if (flags & GPIO_ACTIVE_LOW)
1390 pin_value = !pin_value;
1392 GPIO_PIN_SETFLAGS(gpio, pin, GPIO_PIN_OUTPUT);
1393 GPIO_PIN_SET(gpio, pin, pin_value);
1394 DELAY(delay_prop[0]);
1395 GPIO_PIN_SET(gpio, pin, !pin_value);
1396 DELAY(delay_prop[1]);
1397 GPIO_PIN_SET(gpio, pin, pin_value);
1398 DELAY(delay_prop[2]);
1404 awg_reset(device_t dev)
1406 struct awg_softc *sc;
1409 sc = device_get_softc(dev);
1411 /* Reset PHY if necessary */
1412 if (awg_phy_reset(dev) != 0) {
1413 device_printf(dev, "failed to reset PHY\n");
1417 /* Soft reset all registers and logic */
1418 WR4(sc, EMAC_BASIC_CTL_1, BASIC_CTL_SOFT_RST);
1420 /* Wait for soft reset bit to self-clear */
1421 for (retry = SOFT_RST_RETRY; retry > 0; retry--) {
1422 if ((RD4(sc, EMAC_BASIC_CTL_1) & BASIC_CTL_SOFT_RST) == 0)
1427 device_printf(dev, "soft reset timed out\n");
1438 awg_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1442 *(bus_addr_t *)arg = segs[0].ds_addr;
1446 awg_setup_dma(device_t dev)
1448 struct awg_softc *sc;
1452 sc = device_get_softc(dev);
1455 error = bus_dma_tag_create(
1456 bus_get_dma_tag(dev), /* Parent tag */
1457 DESC_ALIGN, 0, /* alignment, boundary */
1458 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
1459 BUS_SPACE_MAXADDR, /* highaddr */
1460 NULL, NULL, /* filter, filterarg */
1461 TX_DESC_SIZE, 1, /* maxsize, nsegs */
1462 TX_DESC_SIZE, /* maxsegsize */
1464 NULL, NULL, /* lockfunc, lockarg */
1467 device_printf(dev, "cannot create TX descriptor ring tag\n");
1471 error = bus_dmamem_alloc(sc->tx.desc_tag, (void **)&sc->tx.desc_ring,
1472 BUS_DMA_COHERENT | BUS_DMA_WAITOK | BUS_DMA_ZERO, &sc->tx.desc_map);
1474 device_printf(dev, "cannot allocate TX descriptor ring\n");
1478 error = bus_dmamap_load(sc->tx.desc_tag, sc->tx.desc_map,
1479 sc->tx.desc_ring, TX_DESC_SIZE, awg_dmamap_cb,
1480 &sc->tx.desc_ring_paddr, 0);
1482 device_printf(dev, "cannot load TX descriptor ring\n");
1486 for (i = 0; i < TX_DESC_COUNT; i++)
1487 sc->tx.desc_ring[i].next =
1488 htole32(sc->tx.desc_ring_paddr + DESC_OFF(TX_NEXT(i)));
1490 error = bus_dma_tag_create(
1491 bus_get_dma_tag(dev), /* Parent tag */
1492 1, 0, /* alignment, boundary */
1493 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
1494 BUS_SPACE_MAXADDR, /* highaddr */
1495 NULL, NULL, /* filter, filterarg */
1496 MCLBYTES, TX_MAX_SEGS, /* maxsize, nsegs */
1497 MCLBYTES, /* maxsegsize */
1499 NULL, NULL, /* lockfunc, lockarg */
1502 device_printf(dev, "cannot create TX buffer tag\n");
1506 sc->tx.queued = TX_DESC_COUNT;
1507 for (i = 0; i < TX_DESC_COUNT; i++) {
1508 error = bus_dmamap_create(sc->tx.buf_tag, 0,
1509 &sc->tx.buf_map[i].map);
1511 device_printf(dev, "cannot create TX buffer map\n");
1514 awg_setup_txdesc(sc, i, 0, 0, 0);
1518 error = bus_dma_tag_create(
1519 bus_get_dma_tag(dev), /* Parent tag */
1520 DESC_ALIGN, 0, /* alignment, boundary */
1521 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
1522 BUS_SPACE_MAXADDR, /* highaddr */
1523 NULL, NULL, /* filter, filterarg */
1524 RX_DESC_SIZE, 1, /* maxsize, nsegs */
1525 RX_DESC_SIZE, /* maxsegsize */
1527 NULL, NULL, /* lockfunc, lockarg */
1530 device_printf(dev, "cannot create RX descriptor ring tag\n");
1534 error = bus_dmamem_alloc(sc->rx.desc_tag, (void **)&sc->rx.desc_ring,
1535 BUS_DMA_COHERENT | BUS_DMA_WAITOK | BUS_DMA_ZERO, &sc->rx.desc_map);
1537 device_printf(dev, "cannot allocate RX descriptor ring\n");
1541 error = bus_dmamap_load(sc->rx.desc_tag, sc->rx.desc_map,
1542 sc->rx.desc_ring, RX_DESC_SIZE, awg_dmamap_cb,
1543 &sc->rx.desc_ring_paddr, 0);
1545 device_printf(dev, "cannot load RX descriptor ring\n");
1549 error = bus_dma_tag_create(
1550 bus_get_dma_tag(dev), /* Parent tag */
1551 1, 0, /* alignment, boundary */
1552 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
1553 BUS_SPACE_MAXADDR, /* highaddr */
1554 NULL, NULL, /* filter, filterarg */
1555 MCLBYTES, 1, /* maxsize, nsegs */
1556 MCLBYTES, /* maxsegsize */
1558 NULL, NULL, /* lockfunc, lockarg */
1561 device_printf(dev, "cannot create RX buffer tag\n");
1565 for (i = 0; i < RX_DESC_COUNT; i++) {
1566 error = bus_dmamap_create(sc->rx.buf_tag, 0,
1567 &sc->rx.buf_map[i].map);
1569 device_printf(dev, "cannot create RX buffer map\n");
1572 if ((m = awg_alloc_mbufcl(sc)) == NULL) {
1573 device_printf(dev, "cannot allocate RX mbuf\n");
1576 error = awg_setup_rxbuf(sc, i, m);
1578 device_printf(dev, "cannot create RX buffer\n");
1582 bus_dmamap_sync(sc->rx.desc_tag, sc->rx.desc_map,
1583 BUS_DMASYNC_PREWRITE);
1585 /* Write transmit and receive descriptor base address registers */
1586 WR4(sc, EMAC_TX_DMA_LIST, sc->tx.desc_ring_paddr);
1587 WR4(sc, EMAC_RX_DMA_LIST, sc->rx.desc_ring_paddr);
1593 awg_probe(device_t dev)
1595 if (!ofw_bus_status_okay(dev))
1598 if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
1601 device_set_desc(dev, "Allwinner Gigabit Ethernet");
1602 return (BUS_PROBE_DEFAULT);
1606 awg_attach(device_t dev)
1608 uint8_t eaddr[ETHER_ADDR_LEN];
1609 struct awg_softc *sc;
1613 sc = device_get_softc(dev);
1614 sc->type = ofw_bus_search_compatible(dev, compat_data)->ocd_data;
1615 node = ofw_bus_get_node(dev);
1617 if (bus_alloc_resources(dev, awg_spec, sc->res) != 0) {
1618 device_printf(dev, "cannot allocate resources for device\n");
1622 mtx_init(&sc->mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK, MTX_DEF);
1623 callout_init_mtx(&sc->stat_ch, &sc->mtx, 0);
1624 TASK_INIT(&sc->link_task, 0, awg_link_task, sc);
1626 /* Setup clocks and regulators */
1627 error = awg_setup_extres(dev);
1631 /* Read MAC address before resetting the chip */
1632 awg_get_eaddr(dev, eaddr);
1634 /* Soft reset EMAC core */
1635 error = awg_reset(dev);
1639 /* Setup DMA descriptors */
1640 error = awg_setup_dma(dev);
1644 /* Install interrupt handler */
1645 error = bus_setup_intr(dev, sc->res[_RES_IRQ],
1646 INTR_TYPE_NET | INTR_MPSAFE, NULL, awg_intr, sc, &sc->ih);
1648 device_printf(dev, "cannot setup interrupt handler\n");
1652 /* Setup ethernet interface */
1653 sc->ifp = if_alloc(IFT_ETHER);
1654 if_setsoftc(sc->ifp, sc);
1655 if_initname(sc->ifp, device_get_name(dev), device_get_unit(dev));
1656 if_setflags(sc->ifp, IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST);
1657 if_setstartfn(sc->ifp, awg_start);
1658 if_setioctlfn(sc->ifp, awg_ioctl);
1659 if_setinitfn(sc->ifp, awg_init);
1660 if_setsendqlen(sc->ifp, TX_DESC_COUNT - 1);
1661 if_setsendqready(sc->ifp);
1662 if_sethwassist(sc->ifp, CSUM_IP | CSUM_UDP | CSUM_TCP);
1663 if_setcapabilities(sc->ifp, IFCAP_VLAN_MTU | IFCAP_HWCSUM);
1664 if_setcapenable(sc->ifp, if_getcapabilities(sc->ifp));
1665 #ifdef DEVICE_POLLING
1666 if_setcapabilitiesbit(sc->ifp, IFCAP_POLLING, 0);
1669 /* Attach MII driver */
1670 error = mii_attach(dev, &sc->miibus, sc->ifp, awg_media_change,
1671 awg_media_status, BMSR_DEFCAPMASK, MII_PHY_ANY, MII_OFFSET_ANY,
1674 device_printf(dev, "cannot attach PHY\n");
1678 /* Attach ethernet interface */
1679 ether_ifattach(sc->ifp, eaddr);
1684 static device_method_t awg_methods[] = {
1685 /* Device interface */
1686 DEVMETHOD(device_probe, awg_probe),
1687 DEVMETHOD(device_attach, awg_attach),
1690 DEVMETHOD(miibus_readreg, awg_miibus_readreg),
1691 DEVMETHOD(miibus_writereg, awg_miibus_writereg),
1692 DEVMETHOD(miibus_statchg, awg_miibus_statchg),
1697 static driver_t awg_driver = {
1700 sizeof(struct awg_softc),
1703 static devclass_t awg_devclass;
1705 DRIVER_MODULE(awg, simplebus, awg_driver, awg_devclass, 0, 0);
1706 DRIVER_MODULE(miibus, awg, miibus_driver, miibus_devclass, 0, 0);
1708 MODULE_DEPEND(awg, ether, 1, 1, 1);
1709 MODULE_DEPEND(awg, miibus, 1, 1, 1);