]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/arm/allwinner/if_awg.c
Upgrade Unbound to 1.6.0. More to follow.
[FreeBSD/FreeBSD.git] / sys / arm / allwinner / if_awg.c
1 /*-
2  * Copyright (c) 2016 Jared McNeill <jmcneill@invisible.ca>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28
29 /*
30  * Allwinner Gigabit Ethernet MAC (EMAC) controller
31  */
32
33 #include "opt_device_polling.h"
34
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
37
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/bus.h>
41 #include <sys/rman.h>
42 #include <sys/kernel.h>
43 #include <sys/endian.h>
44 #include <sys/mbuf.h>
45 #include <sys/socket.h>
46 #include <sys/sockio.h>
47 #include <sys/module.h>
48 #include <sys/taskqueue.h>
49 #include <sys/gpio.h>
50
51 #include <net/bpf.h>
52 #include <net/if.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>
58
59 #include <machine/bus.h>
60
61 #include <dev/ofw/ofw_bus.h>
62 #include <dev/ofw/ofw_bus_subr.h>
63
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>
68
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>
73
74 #include "syscon_if.h"
75 #include "miibus_if.h"
76 #include "gpio_if.h"
77
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))
80
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)
85
86 #define DESC_ALIGN              4
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)
91
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))
96
97 #define TX_MAX_SEGS             20
98
99 #define SOFT_RST_RETRY          1000
100 #define MII_BUSY_RETRY          1000
101 #define MDIO_FREQ               2500000
102
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
108
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)
128
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);
132
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);
136
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);
140
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);
144
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);
148
149 enum awg_type {
150         EMAC_A83T = 1,
151         EMAC_H3,
152         EMAC_A64,
153 };
154
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 },
159         { NULL,                                 0 }
160 };
161
162 struct awg_bufmap {
163         bus_dmamap_t            map;
164         struct mbuf             *mbuf;
165 };
166
167 struct awg_txring {
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;
175         u_int                   segs;
176 };
177
178 struct awg_rxring {
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;
186         u_int                   cur;
187 };
188
189 enum {
190         _RES_EMAC,
191         _RES_IRQ,
192         _RES_SYSCON,
193         _RES_NITEMS
194 };
195
196 struct awg_softc {
197         struct resource         *res[_RES_NITEMS];
198         struct mtx              mtx;
199         if_t                    ifp;
200         device_t                dev;
201         device_t                miibus;
202         struct callout          stat_ch;
203         struct task             link_task;
204         void                    *ih;
205         u_int                   mdc_div_ratio_m;
206         int                     link;
207         int                     if_flags;
208         enum awg_type           type;
209         struct syscon           *syscon;
210
211         struct awg_txring       tx;
212         struct awg_rxring       rx;
213 };
214
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 },
219         { -1, 0 }
220 };
221
222 static void awg_txeof(struct awg_softc *sc);
223
224 static int awg_parse_delay(device_t dev, uint32_t *tx_delay,
225     uint32_t *rx_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);
230
231 static int
232 awg_miibus_readreg(device_t dev, int phy, int reg)
233 {
234         struct awg_softc *sc;
235         int retry, val;
236
237         sc = device_get_softc(dev);
238         val = 0;
239
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) |
244             MII_BUSY);
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);
248                         break;
249                 }
250                 DELAY(10);
251         }
252
253         if (retry == 0)
254                 device_printf(dev, "phy read timeout, phy=%d reg=%d\n",
255                     phy, reg);
256
257         return (val);
258 }
259
260 static int
261 awg_miibus_writereg(device_t dev, int phy, int reg, int val)
262 {
263         struct awg_softc *sc;
264         int retry;
265
266         sc = device_get_softc(dev);
267
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) |
273             MII_WR | MII_BUSY);
274         for (retry = MII_BUSY_RETRY; retry > 0; retry--) {
275                 if ((RD4(sc, EMAC_MII_CMD) & MII_BUSY) == 0)
276                         break;
277                 DELAY(10);
278         }
279
280         if (retry == 0)
281                 device_printf(dev, "phy write timeout, phy=%d reg=%d\n",
282                     phy, reg);
283
284         return (0);
285 }
286
287 static void
288 awg_update_link_locked(struct awg_softc *sc)
289 {
290         struct mii_data *mii;
291         uint32_t val;
292
293         AWG_ASSERT_LOCKED(sc);
294
295         if ((if_getdrvflags(sc->ifp) & IFF_DRV_RUNNING) == 0)
296                 return;
297         mii = device_get_softc(sc->miibus);
298
299         if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
300             (IFM_ACTIVE | IFM_AVALID)) {
301                 switch (IFM_SUBTYPE(mii->mii_media_active)) {
302                 case IFM_1000_T:
303                 case IFM_1000_SX:
304                 case IFM_100_TX:
305                 case IFM_10_T:
306                         sc->link = 1;
307                         break;
308                 default:
309                         sc->link = 0;
310                         break;
311                 }
312         } else
313                 sc->link = 0;
314
315         if (sc->link == 0)
316                 return;
317
318         val = RD4(sc, EMAC_BASIC_CTL_0);
319         val &= ~(BASIC_CTL_SPEED | BASIC_CTL_DUPLEX);
320
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;
326         else
327                 val |= BASIC_CTL_SPEED_10 << BASIC_CTL_SPEED_SHIFT;
328
329         if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) != 0)
330                 val |= BASIC_CTL_DUPLEX;
331
332         WR4(sc, EMAC_BASIC_CTL_0, val);
333
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);
339
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);
347 }
348
349 static void
350 awg_link_task(void *arg, int pending)
351 {
352         struct awg_softc *sc;
353
354         sc = arg;
355
356         AWG_LOCK(sc);
357         awg_update_link_locked(sc);
358         AWG_UNLOCK(sc);
359 }
360
361 static void
362 awg_miibus_statchg(device_t dev)
363 {
364         struct awg_softc *sc;
365
366         sc = device_get_softc(dev);
367
368         taskqueue_enqueue(taskqueue_swi, &sc->link_task);
369 }
370
371 static void
372 awg_media_status(if_t ifp, struct ifmediareq *ifmr)
373 {
374         struct awg_softc *sc;
375         struct mii_data *mii;
376
377         sc = if_getsoftc(ifp);
378         mii = device_get_softc(sc->miibus);
379
380         AWG_LOCK(sc);
381         mii_pollstat(mii);
382         ifmr->ifm_active = mii->mii_media_active;
383         ifmr->ifm_status = mii->mii_media_status;
384         AWG_UNLOCK(sc);
385 }
386
387 static int
388 awg_media_change(if_t ifp)
389 {
390         struct awg_softc *sc;
391         struct mii_data *mii;
392         int error;
393
394         sc = if_getsoftc(ifp);
395         mii = device_get_softc(sc->miibus);
396
397         AWG_LOCK(sc);
398         error = mii_mediachg(mii);
399         AWG_UNLOCK(sc);
400
401         return (error);
402 }
403
404 static int
405 awg_encap(struct awg_softc *sc, struct mbuf **mp)
406 {
407         bus_dmamap_t map;
408         bus_dma_segment_t segs[TX_MAX_SEGS];
409         int error, nsegs, cur, first, last, i;
410         u_int csum_flags;
411         uint32_t flags, status;
412         struct mbuf *m;
413
414         cur = first = sc->tx.cur;
415         map = sc->tx.buf_map[first].map;
416
417         m = *mp;
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);
422                 if (m == NULL) {
423                         device_printf(sc->dev, "awg_encap: m_collapse failed\n");
424                         m_freem(*mp);
425                         *mp = NULL;
426                         return (ENOMEM);
427                 }
428                 *mp = m;
429                 error = bus_dmamap_load_mbuf_sg(sc->tx.buf_tag, map, m,
430                     segs, &nsegs, BUS_DMA_NOWAIT);
431                 if (error != 0) {
432                         m_freem(*mp);
433                         *mp = NULL;
434                 }
435         }
436         if (error != 0) {
437                 device_printf(sc->dev, "awg_encap: bus_dmamap_load_mbuf_sg failed\n");
438                 return (error);
439         }
440         if (nsegs == 0) {
441                 m_freem(*mp);
442                 *mp = NULL;
443                 return (EIO);
444         }
445
446         if (sc->tx.queued + nsegs > TX_DESC_COUNT) {
447                 bus_dmamap_unload(sc->tx.buf_tag, map);
448                 return (ENOBUFS);
449         }
450
451         bus_dmamap_sync(sc->tx.buf_tag, map, BUS_DMASYNC_PREWRITE);
452
453         flags = TX_FIR_DESC;
454         status = 0;
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;
458                 else
459                         csum_flags = TX_CHECKSUM_CTL_IP;
460                 flags |= (csum_flags << TX_CHECKSUM_CTL_SHIFT);
461         }
462
463         for (i = 0; i < nsegs; i++) {
464                 sc->tx.segs++;
465                 if (i == nsegs - 1) {
466                         flags |= TX_LAST_DESC;
467                         /*
468                          * Can only request TX completion
469                          * interrupt on last descriptor.
470                          */
471                         if (sc->tx.segs >= awg_tx_interval) {
472                                 sc->tx.segs = 0;
473                                 flags |= TX_INT_CTL;
474                         }
475                 }
476
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);
480
481                 flags &= ~TX_FIR_DESC;
482                 /*
483                  * Setting of the valid bit in the first descriptor is
484                  * deferred until the whole chain is fully set up.
485                  */
486                 status = TX_DESC_CTL;
487
488                 ++sc->tx.queued;
489                 cur = TX_NEXT(cur);
490         }
491
492         sc->tx.cur = cur;
493
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;
499
500         /*
501          * The whole mbuf chain has been DMA mapped,
502          * fix the first descriptor.
503          */
504         sc->tx.desc_ring[first].status = htole32(TX_DESC_CTL);
505
506         return (0);
507 }
508
509 static void
510 awg_clean_txbuf(struct awg_softc *sc, int index)
511 {
512         struct awg_bufmap *bmap;
513
514         --sc->tx.queued;
515
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);
521                 m_freem(bmap->mbuf);
522                 bmap->mbuf = NULL;
523         }
524 }
525
526 static void
527 awg_setup_rxdesc(struct awg_softc *sc, int index, bus_addr_t paddr)
528 {
529         uint32_t status, size;
530
531         status = RX_DESC_CTL;
532         size = MCLBYTES - 1;
533
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);
537 }
538
539 static void
540 awg_reuse_rxdesc(struct awg_softc *sc, int index)
541 {
542
543         sc->rx.desc_ring[index].status = htole32(RX_DESC_CTL);
544 }
545
546 static int
547 awg_newbuf_rx(struct awg_softc *sc, int index)
548 {
549         struct mbuf *m;
550         bus_dma_segment_t seg;
551         bus_dmamap_t map;
552         int nsegs;
553
554         m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
555         if (m == NULL)
556                 return (ENOBUFS);
557
558         m->m_pkthdr.len = m->m_len = m->m_ext.ext_size;
559         m_adj(m, ETHER_ALIGN);
560
561         if (bus_dmamap_load_mbuf_sg(sc->rx.buf_tag, sc->rx.buf_spare_map,
562             m, &seg, &nsegs, BUS_DMA_NOWAIT) != 0) {
563                 m_freem(m);
564                 return (ENOBUFS);
565         }
566
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);
571         }
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);
577
578         sc->rx.buf_map[index].mbuf = m;
579         awg_setup_rxdesc(sc, index, seg.ds_addr);
580
581         return (0);
582 }
583
584 static void
585 awg_start_locked(struct awg_softc *sc)
586 {
587         struct mbuf *m;
588         uint32_t val;
589         if_t ifp;
590         int cnt, err;
591
592         AWG_ASSERT_LOCKED(sc);
593
594         if (!sc->link)
595                 return;
596
597         ifp = sc->ifp;
598
599         if ((if_getdrvflags(ifp) & (IFF_DRV_RUNNING|IFF_DRV_OACTIVE)) !=
600             IFF_DRV_RUNNING)
601                 return;
602
603         for (cnt = 0; ; cnt++) {
604                 m = if_dequeue(ifp);
605                 if (m == NULL)
606                         break;
607
608                 err = awg_encap(sc, &m);
609                 if (err != 0) {
610                         if (err == ENOBUFS)
611                                 if_setdrvflagbits(ifp, IFF_DRV_OACTIVE, 0);
612                         if (m != NULL)
613                                 if_sendq_prepend(ifp, m);
614                         break;
615                 }
616                 if_bpfmtap(ifp, m);
617         }
618
619         if (cnt != 0) {
620                 bus_dmamap_sync(sc->tx.desc_tag, sc->tx.desc_map,
621                     BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
622
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);
626         }
627 }
628
629 static void
630 awg_start(if_t ifp)
631 {
632         struct awg_softc *sc;
633
634         sc = if_getsoftc(ifp);
635
636         AWG_LOCK(sc);
637         awg_start_locked(sc);
638         AWG_UNLOCK(sc);
639 }
640
641 static void
642 awg_tick(void *softc)
643 {
644         struct awg_softc *sc;
645         struct mii_data *mii;
646         if_t ifp;
647         int link;
648
649         sc = softc;
650         ifp = sc->ifp;
651         mii = device_get_softc(sc->miibus);
652
653         AWG_ASSERT_LOCKED(sc);
654
655         if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) == 0)
656                 return;
657
658         link = sc->link;
659         mii_tick(mii);
660         if (sc->link && !link)
661                 awg_start_locked(sc);
662
663         callout_reset(&sc->stat_ch, hz, awg_tick, sc);
664 }
665
666 /* Bit Reversal - http://aggregate.org/MAGIC/#Bit%20Reversal */
667 static uint32_t
668 bitrev32(uint32_t x)
669 {
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));
674
675         return (x >> 16) | (x << 16);
676 }
677
678 static void
679 awg_setup_rxfilter(struct awg_softc *sc)
680 {
681         uint32_t val, crc, hashreg, hashbit, hash[2], machi, maclo;
682         int mc_count, mcnt, i;
683         uint8_t *eaddr, *mta;
684         if_t ifp;
685
686         AWG_ASSERT_LOCKED(sc);
687
688         ifp = sc->ifp;
689         val = 0;
690         hash[0] = hash[1] = 0;
691
692         mc_count = if_multiaddr_count(ifp, -1);
693
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;
701
702                 mta = malloc(sizeof(unsigned char) * ETHER_ADDR_LEN * mc_count,
703                     M_DEVBUF, M_NOWAIT);
704                 if (mta == NULL) {
705                         if_printf(ifp,
706                             "failed to allocate temporary multicast list\n");
707                         return;
708                 }
709
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);
718                 }
719
720                 free(mta, M_DEVBUF);
721         }
722
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) |
727            (eaddr[0] << 0);
728         WR4(sc, EMAC_ADDR_HIGH(0), machi);
729         WR4(sc, EMAC_ADDR_LOW(0), maclo);
730
731         /* Multicast hash filters */
732         WR4(sc, EMAC_RX_HASH_0, hash[1]);
733         WR4(sc, EMAC_RX_HASH_1, hash[0]);
734
735         /* RX frame filter config */
736         WR4(sc, EMAC_RX_FRM_FLT, val);
737 }
738
739 static void
740 awg_enable_intr(struct awg_softc *sc)
741 {
742         /* Enable interrupts */
743         WR4(sc, EMAC_INT_EN, RX_INT_EN | TX_INT_EN | TX_BUF_UA_INT_EN);
744 }
745
746 static void
747 awg_disable_intr(struct awg_softc *sc)
748 {
749         /* Disable interrupts */
750         WR4(sc, EMAC_INT_EN, 0);
751 }
752
753 static void
754 awg_init_locked(struct awg_softc *sc)
755 {
756         struct mii_data *mii;
757         uint32_t val;
758         if_t ifp;
759
760         mii = device_get_softc(sc->miibus);
761         ifp = sc->ifp;
762
763         AWG_ASSERT_LOCKED(sc);
764
765         if (if_getdrvflags(ifp) & IFF_DRV_RUNNING)
766                 return;
767
768         awg_setup_rxfilter(sc);
769
770         /* Configure DMA burst length and priorities */
771         val = awg_burst_len << BASIC_CTL_BURST_LEN_SHIFT;
772         if (awg_rx_tx_pri)
773                 val |= BASIC_CTL_RX_TX_PRI;
774         WR4(sc, EMAC_BASIC_CTL_1, val);
775
776         /* Enable interrupts */
777 #ifdef DEVICE_POLLING
778         if ((if_getcapenable(ifp) & IFCAP_POLLING) == 0)
779                 awg_enable_intr(sc);
780         else
781                 awg_disable_intr(sc);
782 #else
783         awg_enable_intr(sc);
784 #endif
785
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);
789
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);
793
794         /* Enable transmitter */
795         val = RD4(sc, EMAC_TX_CTL_0);
796         WR4(sc, EMAC_TX_CTL_0, val | TX_EN);
797
798         /* Enable receiver */
799         val = RD4(sc, EMAC_RX_CTL_0);
800         WR4(sc, EMAC_RX_CTL_0, val | RX_EN | CHECK_CRC);
801
802         if_setdrvflagbits(ifp, IFF_DRV_RUNNING, IFF_DRV_OACTIVE);
803
804         mii_mediachg(mii);
805         callout_reset(&sc->stat_ch, hz, awg_tick, sc);
806 }
807
808 static void
809 awg_init(void *softc)
810 {
811         struct awg_softc *sc;
812
813         sc = softc;
814
815         AWG_LOCK(sc);
816         awg_init_locked(sc);
817         AWG_UNLOCK(sc);
818 }
819
820 static void
821 awg_stop(struct awg_softc *sc)
822 {
823         if_t ifp;
824         uint32_t val;
825         int i;
826
827         AWG_ASSERT_LOCKED(sc);
828
829         ifp = sc->ifp;
830
831         callout_stop(&sc->stat_ch);
832
833         /* Stop transmit DMA and flush data in the TX FIFO */
834         val = RD4(sc, EMAC_TX_CTL_1);
835         val &= ~TX_DMA_EN;
836         val |= FLUSH_TX_FIFO;
837         WR4(sc, EMAC_TX_CTL_1, val);
838
839         /* Disable transmitter */
840         val = RD4(sc, EMAC_TX_CTL_0);
841         WR4(sc, EMAC_TX_CTL_0, val & ~TX_EN);
842
843         /* Disable receiver */
844         val = RD4(sc, EMAC_RX_CTL_0);
845         WR4(sc, EMAC_RX_CTL_0, val & ~RX_EN);
846
847         /* Disable interrupts */
848         awg_disable_intr(sc);
849
850         /* Disable transmit DMA */
851         val = RD4(sc, EMAC_TX_CTL_1);
852         WR4(sc, EMAC_TX_CTL_1, val & ~TX_DMA_EN);
853
854         /* Disable receive DMA */
855         val = RD4(sc, EMAC_RX_CTL_1);
856         WR4(sc, EMAC_RX_CTL_1, val & ~RX_DMA_EN);
857
858         sc->link = 0;
859
860         /* Finish handling transmitted buffers */
861         awg_txeof(sc);
862
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)
867                         break;
868                 awg_clean_txbuf(sc, i);
869         }
870         sc->tx.next = 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);
874         }
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);
878
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);
882
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)
886                         break;
887                 awg_reuse_rxdesc(sc, i);
888         }
889         sc->rx.cur = i;
890         bus_dmamap_sync(sc->rx.desc_tag, sc->rx.desc_map,
891             BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
892
893         if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
894 }
895
896 static int
897 awg_rxintr(struct awg_softc *sc)
898 {
899         if_t ifp;
900         struct mbuf *m, *mh, *mt;
901         int error, index, len, cnt, npkt;
902         uint32_t status;
903
904         ifp = sc->ifp;
905         mh = mt = NULL;
906         cnt = 0;
907         npkt = 0;
908
909         bus_dmamap_sync(sc->rx.desc_tag, sc->rx.desc_map,
910             BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
911
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)
915                         break;
916
917                 len = (status & RX_FRM_LEN) >> RX_FRM_LEN_SHIFT;
918
919                 if (len == 0) {
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);
923                         continue;
924                 }
925
926                 m = sc->rx.buf_map[index].mbuf;
927
928                 error = awg_newbuf_rx(sc, index);
929                 if (error != 0) {
930                         if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
931                         awg_reuse_rxdesc(sc, index);
932                         continue;
933                 }
934
935                 m->m_pkthdr.rcvif = ifp;
936                 m->m_pkthdr.len = len;
937                 m->m_len = len;
938                 if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
939
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;
949                         }
950                 }
951
952                 m->m_nextpkt = NULL;
953                 if (mh == NULL)
954                         mh = m;
955                 else
956                         mt->m_nextpkt = m;
957                 mt = m;
958                 ++cnt;
959                 ++npkt;
960
961                 if (cnt == awg_rx_batch) {
962                         AWG_UNLOCK(sc);
963                         if_input(ifp, mh);
964                         AWG_LOCK(sc);
965                         mh = mt = NULL;
966                         cnt = 0;
967                 }
968         }
969
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);
973         }
974
975         if (mh != NULL) {
976                 AWG_UNLOCK(sc);
977                 if_input(ifp, mh);
978                 AWG_LOCK(sc);
979         }
980
981         sc->rx.cur = index;
982
983         return (npkt);
984 }
985
986 static void
987 awg_txeof(struct awg_softc *sc)
988 {
989         struct emac_desc *desc;
990         uint32_t status, size;
991         if_t ifp;
992         int i, prog;
993
994         AWG_ASSERT_LOCKED(sc);
995
996         bus_dmamap_sync(sc->tx.desc_tag, sc->tx.desc_map,
997             BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
998
999         ifp = sc->ifp;
1000
1001         prog = 0;
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)
1006                         break;
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);
1011                         else
1012                                 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
1013                 }
1014                 prog++;
1015                 awg_clean_txbuf(sc, i);
1016         }
1017
1018         if (prog > 0) {
1019                 sc->tx.next = i;
1020                 if_setdrvflagbits(ifp, 0, IFF_DRV_OACTIVE);
1021         }
1022 }
1023
1024 static void
1025 awg_intr(void *arg)
1026 {
1027         struct awg_softc *sc;
1028         uint32_t val;
1029
1030         sc = arg;
1031
1032         AWG_LOCK(sc);
1033         val = RD4(sc, EMAC_INT_STA);
1034         WR4(sc, EMAC_INT_STA, val);
1035
1036         if (val & RX_INT)
1037                 awg_rxintr(sc);
1038
1039         if (val & TX_INT)
1040                 awg_txeof(sc);
1041
1042         if (val & (TX_INT | TX_BUF_UA_INT)) {
1043                 if (!if_sendq_empty(sc->ifp))
1044                         awg_start_locked(sc);
1045         }
1046
1047         AWG_UNLOCK(sc);
1048 }
1049
1050 #ifdef DEVICE_POLLING
1051 static int
1052 awg_poll(if_t ifp, enum poll_cmd cmd, int count)
1053 {
1054         struct awg_softc *sc;
1055         uint32_t val;
1056         int rx_npkts;
1057
1058         sc = if_getsoftc(ifp);
1059         rx_npkts = 0;
1060
1061         AWG_LOCK(sc);
1062
1063         if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) == 0) {
1064                 AWG_UNLOCK(sc);
1065                 return (0);
1066         }
1067
1068         rx_npkts = awg_rxintr(sc);
1069         awg_txeof(sc);
1070         if (!if_sendq_empty(ifp))
1071                 awg_start_locked(sc);
1072
1073         if (cmd == POLL_AND_CHECK_STATUS) {
1074                 val = RD4(sc, EMAC_INT_STA);
1075                 if (val != 0)
1076                         WR4(sc, EMAC_INT_STA, val);
1077         }
1078
1079         AWG_UNLOCK(sc);
1080
1081         return (rx_npkts);
1082 }
1083 #endif
1084
1085 static int
1086 awg_ioctl(if_t ifp, u_long cmd, caddr_t data)
1087 {
1088         struct awg_softc *sc;
1089         struct mii_data *mii;
1090         struct ifreq *ifr;
1091         int flags, mask, error;
1092
1093         sc = if_getsoftc(ifp);
1094         mii = device_get_softc(sc->miibus);
1095         ifr = (struct ifreq *)data;
1096         error = 0;
1097
1098         switch (cmd) {
1099         case SIOCSIFFLAGS:
1100                 AWG_LOCK(sc);
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);
1106                         } else
1107                                 awg_init_locked(sc);
1108                 } else {
1109                         if (if_getdrvflags(ifp) & IFF_DRV_RUNNING)
1110                                 awg_stop(sc);
1111                 }
1112                 sc->if_flags = if_getflags(ifp);
1113                 AWG_UNLOCK(sc);
1114                 break;
1115         case SIOCADDMULTI:
1116         case SIOCDELMULTI:
1117                 if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
1118                         AWG_LOCK(sc);
1119                         awg_setup_rxfilter(sc);
1120                         AWG_UNLOCK(sc);
1121                 }
1122                 break;
1123         case SIOCSIFMEDIA:
1124         case SIOCGIFMEDIA:
1125                 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, cmd);
1126                 break;
1127         case SIOCSIFCAP:
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);
1133                                 if (error != 0)
1134                                         break;
1135                                 AWG_LOCK(sc);
1136                                 awg_disable_intr(sc);
1137                                 if_setcapenablebit(ifp, IFCAP_POLLING, 0);
1138                                 AWG_UNLOCK(sc);
1139                         } else {
1140                                 error = ether_poll_deregister(ifp);
1141                                 AWG_LOCK(sc);
1142                                 awg_enable_intr(sc);
1143                                 if_setcapenablebit(ifp, 0, IFCAP_POLLING);
1144                                 AWG_UNLOCK(sc);
1145                         }
1146                 }
1147 #endif
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);
1156                 else
1157                         if_sethwassistbits(ifp, 0, CSUM_IP | CSUM_UDP | CSUM_TCP);
1158                 break;
1159         default:
1160                 error = ether_ioctl(ifp, cmd, data);
1161                 break;
1162         }
1163
1164         return (error);
1165 }
1166
1167 static uint32_t
1168 syscon_read_emac_clk_reg(device_t dev)
1169 {
1170         struct awg_softc *sc;
1171
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));
1177
1178         return (0);
1179 }
1180
1181 static void
1182 syscon_write_emac_clk_reg(device_t dev, uint32_t val)
1183 {
1184         struct awg_softc *sc;
1185
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);
1191 }
1192
1193 static phandle_t
1194 awg_get_phy_node(device_t dev)
1195 {
1196         phandle_t node;
1197         pcell_t phy_handle;
1198
1199         node = ofw_bus_get_node(dev);
1200         if (OF_getencprop(node, "phy-handle", (void *)&phy_handle,
1201             sizeof(phy_handle)) <= 0)
1202                 return (0);
1203
1204         return (OF_node_from_xref(phy_handle));
1205 }
1206
1207 static bool
1208 awg_has_internal_phy(device_t dev)
1209 {
1210         phandle_t node, phy_node;
1211
1212         node = ofw_bus_get_node(dev);
1213         /* Legacy binding */
1214         if (OF_hasprop(node, "allwinner,use-internal-phy"))
1215                 return (true);
1216
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);
1220 }
1221
1222 static int
1223 awg_parse_delay(device_t dev, uint32_t *tx_delay, uint32_t *rx_delay)
1224 {
1225         phandle_t node;
1226         uint32_t delay;
1227
1228         if (tx_delay == NULL || rx_delay == NULL)
1229                 return (EINVAL);
1230         *tx_delay = *rx_delay = 0;
1231         node = ofw_bus_get_node(dev);
1232
1233         if (OF_getencprop(node, "tx-delay", &delay, sizeof(delay)) >= 0)
1234                 *tx_delay = delay;
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");
1239                         return (EDOM);
1240                 }
1241                 *tx_delay = delay / 100;
1242         }
1243         if (*tx_delay > 7) {
1244                 device_printf(dev, "tx-delay out of range\n");
1245                 return (ERANGE);
1246         }
1247
1248         if (OF_getencprop(node, "rx-delay", &delay, sizeof(delay)) >= 0)
1249                 *rx_delay = delay;
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");
1254                         return (EDOM);
1255                 }
1256                 *rx_delay = delay / 100;
1257         }
1258         if (*rx_delay > 31) {
1259                 device_printf(dev, "rx-delay out of range\n");
1260                 return (ERANGE);
1261         }
1262
1263         return (0);
1264 }
1265
1266 static int
1267 awg_setup_phy(device_t dev)
1268 {
1269         struct awg_softc *sc;
1270         clk_t clk_tx, clk_tx_parent;
1271         const char *tx_parent_name;
1272         char *phy_type;
1273         phandle_t node;
1274         uint32_t reg, tx_delay, rx_delay;
1275         int error;
1276         bool use_syscon;
1277
1278         sc = device_get_softc(dev);
1279         node = ofw_bus_get_node(dev);
1280         use_syscon = false;
1281
1282         if (OF_getprop_alloc(node, "phy-mode", (void **)&phy_type) == 0)
1283                 return (0);
1284
1285         if (sc->syscon != NULL || sc->res[_RES_SYSCON] != NULL)
1286                 use_syscon = true;
1287
1288         if (bootverbose)
1289                 device_printf(dev, "PHY type: %s, conf mode: %s\n", phy_type,
1290                     use_syscon ? "reg" : "clk");
1291
1292         if (use_syscon) {
1293                 /*
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.
1299                  */
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;
1306                 else
1307                         reg |= EMAC_CLK_PIT_MII | EMAC_CLK_SRC_MII;
1308
1309                 /*
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!
1314                  */
1315                 error = awg_parse_delay(dev, &tx_delay, &rx_delay);
1316                 if (error != 0)
1317                         goto fail;
1318
1319                 /* Default to 0 and we'll increase it if we need to. */
1320                 reg &= ~(EMAC_CLK_ETXDC | EMAC_CLK_ERXDC);
1321                 if (tx_delay > 0)
1322                         reg |= (tx_delay << EMAC_CLK_ETXDC_SHIFT);
1323                 if (rx_delay > 0)
1324                         reg |= (rx_delay << EMAC_CLK_ERXDC_SHIFT);
1325
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;
1333                                 else
1334                                         reg &= ~EMAC_CLK_EPHY_LED_POL;
1335
1336                                 /* Set internal PHY addr to 1 */
1337                                 reg &= ~EMAC_CLK_EPHY_ADDR;
1338                                 reg |= (1 << EMAC_CLK_EPHY_ADDR_SHIFT);
1339                         } else {
1340                                 reg &= ~EMAC_CLK_EPHY_SELECT;
1341                         }
1342                 }
1343
1344                 if (bootverbose)
1345                         device_printf(dev, "EMAC clock: 0x%08x\n", reg);
1346                 syscon_write_emac_clk_reg(dev, reg);
1347         } else {
1348                 if (strncmp(phy_type, "rgmii", 5) == 0)
1349                         tx_parent_name = "emac_int_tx";
1350                 else
1351                         tx_parent_name = "mii_phy_tx";
1352
1353                 /* Get the TX clock */
1354                 error = clk_get_by_ofw_name(dev, 0, "tx", &clk_tx);
1355                 if (error != 0) {
1356                         device_printf(dev, "cannot get tx clock\n");
1357                         goto fail;
1358                 }
1359
1360                 /* Find the desired parent clock based on phy-mode property */
1361                 error = clk_get_by_name(dev, tx_parent_name, &clk_tx_parent);
1362                 if (error != 0) {
1363                         device_printf(dev, "cannot get clock '%s'\n",
1364                             tx_parent_name);
1365                         goto fail;
1366                 }
1367
1368                 /* Set TX clock parent */
1369                 error = clk_set_parent_by_clk(clk_tx, clk_tx_parent);
1370                 if (error != 0) {
1371                         device_printf(dev, "cannot set tx clock parent\n");
1372                         goto fail;
1373                 }
1374
1375                 /* Enable TX clock */
1376                 error = clk_enable(clk_tx);
1377                 if (error != 0) {
1378                         device_printf(dev, "cannot enable tx clock\n");
1379                         goto fail;
1380                 }
1381         }
1382
1383         error = 0;
1384
1385 fail:
1386         OF_prop_free(phy_type);
1387         return (error);
1388 }
1389
1390 static int
1391 awg_setup_extres(device_t dev)
1392 {
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;
1397         regulator_t reg;
1398         uint64_t freq;
1399         int error, div;
1400
1401         sc = device_get_softc(dev);
1402         rst_ahb = rst_ephy = NULL;
1403         clk_ahb = clk_ephy = NULL;
1404         reg = NULL;
1405         node = ofw_bus_get_node(dev);
1406         phy_node = awg_get_phy_node(dev);
1407
1408         if (phy_node == 0 && OF_hasprop(node, "phy-handle")) {
1409                 error = ENXIO;
1410                 device_printf(dev, "cannot get phy handle\n");
1411                 goto fail;
1412         }
1413
1414         /* Get AHB clock and reset resources */
1415         error = hwreset_get_by_ofw_name(dev, 0, "stmmaceth", &rst_ahb);
1416         if (error != 0)
1417                 error = hwreset_get_by_ofw_name(dev, 0, "ahb", &rst_ahb);
1418         if (error != 0) {
1419                 device_printf(dev, "cannot get ahb reset\n");
1420                 goto fail;
1421         }
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,
1424                     &rst_ephy) != 0)
1425                         rst_ephy = NULL;
1426         error = clk_get_by_ofw_name(dev, 0, "stmmaceth", &clk_ahb);
1427         if (error != 0)
1428                 error = clk_get_by_ofw_name(dev, 0, "ahb", &clk_ahb);
1429         if (error != 0) {
1430                 device_printf(dev, "cannot get ahb clock\n");
1431                 goto fail;
1432         }
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,
1435                     &clk_ephy) != 0)
1436                         clk_ephy = NULL;
1437
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");
1441                 goto fail;
1442         }
1443
1444         /* Configure PHY for MII or RGMII mode */
1445         if (awg_setup_phy(dev) != 0)
1446                 goto fail;
1447
1448         /* Enable clocks */
1449         error = clk_enable(clk_ahb);
1450         if (error != 0) {
1451                 device_printf(dev, "cannot enable ahb clock\n");
1452                 goto fail;
1453         }
1454         if (clk_ephy != NULL) {
1455                 error = clk_enable(clk_ephy);
1456                 if (error != 0) {
1457                         device_printf(dev, "cannot enable ephy clock\n");
1458                         goto fail;
1459                 }
1460         }
1461
1462         /* De-assert reset */
1463         error = hwreset_deassert(rst_ahb);
1464         if (error != 0) {
1465                 device_printf(dev, "cannot de-assert ahb reset\n");
1466                 goto fail;
1467         }
1468         if (rst_ephy != NULL) {
1469                 error = hwreset_deassert(rst_ephy);
1470                 if (error != 0) {
1471                         device_printf(dev, "cannot de-assert ephy reset\n");
1472                         goto fail;
1473                 }
1474         }
1475
1476         /* Enable PHY regulator if applicable */
1477         if (regulator_get_by_ofw_property(dev, 0, "phy-supply", &reg) == 0) {
1478                 error = regulator_enable(reg);
1479                 if (error != 0) {
1480                         device_printf(dev, "cannot enable PHY regulator\n");
1481                         goto fail;
1482                 }
1483         }
1484
1485         /* Determine MDC clock divide ratio based on AHB clock */
1486         error = clk_get_freq(clk_ahb, &freq);
1487         if (error != 0) {
1488                 device_printf(dev, "cannot get AHB clock frequency\n");
1489                 goto fail;
1490         }
1491         div = freq / MDIO_FREQ;
1492         if (div <= 16)
1493                 sc->mdc_div_ratio_m = MDC_DIV_RATIO_M_16;
1494         else if (div <= 32)
1495                 sc->mdc_div_ratio_m = MDC_DIV_RATIO_M_32;
1496         else if (div <= 64)
1497                 sc->mdc_div_ratio_m = MDC_DIV_RATIO_M_64;
1498         else if (div <= 128)
1499                 sc->mdc_div_ratio_m = MDC_DIV_RATIO_M_128;
1500         else {
1501                 device_printf(dev, "cannot determine MDC clock divide ratio\n");
1502                 error = ENXIO;
1503                 goto fail;
1504         }
1505
1506         if (bootverbose)
1507                 device_printf(dev, "AHB frequency %ju Hz, MDC div: 0x%x\n",
1508                     (uintmax_t)freq, sc->mdc_div_ratio_m);
1509
1510         return (0);
1511
1512 fail:
1513         if (reg != NULL)
1514                 regulator_release(reg);
1515         if (clk_ephy != NULL)
1516                 clk_release(clk_ephy);
1517         if (clk_ahb != NULL)
1518                 clk_release(clk_ahb);
1519         if (rst_ephy != NULL)
1520                 hwreset_release(rst_ephy);
1521         if (rst_ahb != NULL)
1522                 hwreset_release(rst_ahb);
1523         return (error);
1524 }
1525
1526 static void 
1527 awg_get_eaddr(device_t dev, uint8_t *eaddr)
1528 {
1529         struct awg_softc *sc;
1530         uint32_t maclo, machi, rnd;
1531         u_char rootkey[16];
1532
1533         sc = device_get_softc(dev);
1534
1535         machi = RD4(sc, EMAC_ADDR_HIGH(0)) & 0xffff;
1536         maclo = RD4(sc, EMAC_ADDR_LOW(0));
1537
1538         if (maclo == 0xffffffff && machi == 0xffff) {
1539                 /* MAC address in hardware is invalid, create one */
1540                 if (aw_sid_get_rootkey(rootkey) == 0 &&
1541                     (rootkey[3] | rootkey[12] | rootkey[13] | rootkey[14] |
1542                      rootkey[15]) != 0) {
1543                         /* MAC address is derived from the root key in SID */
1544                         maclo = (rootkey[13] << 24) | (rootkey[12] << 16) |
1545                                 (rootkey[3] << 8) | 0x02;
1546                         machi = (rootkey[15] << 8) | rootkey[14];
1547                 } else {
1548                         /* Create one */
1549                         rnd = arc4random();
1550                         maclo = 0x00f2 | (rnd & 0xffff0000);
1551                         machi = rnd & 0xffff;
1552                 }
1553         }
1554
1555         eaddr[0] = maclo & 0xff;
1556         eaddr[1] = (maclo >> 8) & 0xff;
1557         eaddr[2] = (maclo >> 16) & 0xff;
1558         eaddr[3] = (maclo >> 24) & 0xff;
1559         eaddr[4] = machi & 0xff;
1560         eaddr[5] = (machi >> 8) & 0xff;
1561 }
1562
1563 #ifdef AWG_DEBUG
1564 static void
1565 awg_dump_regs(device_t dev)
1566 {
1567         static const struct {
1568                 const char *name;
1569                 u_int reg;
1570         } regs[] = {
1571                 { "BASIC_CTL_0", EMAC_BASIC_CTL_0 },
1572                 { "BASIC_CTL_1", EMAC_BASIC_CTL_1 },
1573                 { "INT_STA", EMAC_INT_STA },
1574                 { "INT_EN", EMAC_INT_EN },
1575                 { "TX_CTL_0", EMAC_TX_CTL_0 },
1576                 { "TX_CTL_1", EMAC_TX_CTL_1 },
1577                 { "TX_FLOW_CTL", EMAC_TX_FLOW_CTL },
1578                 { "TX_DMA_LIST", EMAC_TX_DMA_LIST },
1579                 { "RX_CTL_0", EMAC_RX_CTL_0 },
1580                 { "RX_CTL_1", EMAC_RX_CTL_1 },
1581                 { "RX_DMA_LIST", EMAC_RX_DMA_LIST },
1582                 { "RX_FRM_FLT", EMAC_RX_FRM_FLT },
1583                 { "RX_HASH_0", EMAC_RX_HASH_0 },
1584                 { "RX_HASH_1", EMAC_RX_HASH_1 },
1585                 { "MII_CMD", EMAC_MII_CMD },
1586                 { "ADDR_HIGH0", EMAC_ADDR_HIGH(0) },
1587                 { "ADDR_LOW0", EMAC_ADDR_LOW(0) },
1588                 { "TX_DMA_STA", EMAC_TX_DMA_STA },
1589                 { "TX_DMA_CUR_DESC", EMAC_TX_DMA_CUR_DESC },
1590                 { "TX_DMA_CUR_BUF", EMAC_TX_DMA_CUR_BUF },
1591                 { "RX_DMA_STA", EMAC_RX_DMA_STA },
1592                 { "RX_DMA_CUR_DESC", EMAC_RX_DMA_CUR_DESC },
1593                 { "RX_DMA_CUR_BUF", EMAC_RX_DMA_CUR_BUF },
1594                 { "RGMII_STA", EMAC_RGMII_STA },
1595         };
1596         struct awg_softc *sc;
1597         unsigned int n;
1598
1599         sc = device_get_softc(dev);
1600
1601         for (n = 0; n < nitems(regs); n++)
1602                 device_printf(dev, "  %-20s %08x\n", regs[n].name,
1603                     RD4(sc, regs[n].reg));
1604 }
1605 #endif
1606
1607 #define GPIO_ACTIVE_LOW         1
1608
1609 static int
1610 awg_phy_reset(device_t dev)
1611 {
1612         pcell_t gpio_prop[4], delay_prop[3];
1613         phandle_t node, gpio_node;
1614         device_t gpio;
1615         uint32_t pin, flags;
1616         uint32_t pin_value;
1617
1618         node = ofw_bus_get_node(dev);
1619         if (OF_getencprop(node, "allwinner,reset-gpio", gpio_prop,
1620             sizeof(gpio_prop)) <= 0)
1621                 return (0);
1622
1623         if (OF_getencprop(node, "allwinner,reset-delays-us", delay_prop,
1624             sizeof(delay_prop)) <= 0)
1625                 return (ENXIO);
1626
1627         gpio_node = OF_node_from_xref(gpio_prop[0]);
1628         if ((gpio = OF_device_from_xref(gpio_prop[0])) == NULL)
1629                 return (ENXIO);
1630
1631         if (GPIO_MAP_GPIOS(gpio, node, gpio_node, nitems(gpio_prop) - 1,
1632             gpio_prop + 1, &pin, &flags) != 0)
1633                 return (ENXIO);
1634
1635         pin_value = GPIO_PIN_LOW;
1636         if (OF_hasprop(node, "allwinner,reset-active-low"))
1637                 pin_value = GPIO_PIN_HIGH;
1638
1639         if (flags & GPIO_ACTIVE_LOW)
1640                 pin_value = !pin_value;
1641
1642         GPIO_PIN_SETFLAGS(gpio, pin, GPIO_PIN_OUTPUT);
1643         GPIO_PIN_SET(gpio, pin, pin_value);
1644         DELAY(delay_prop[0]);
1645         GPIO_PIN_SET(gpio, pin, !pin_value);
1646         DELAY(delay_prop[1]);
1647         GPIO_PIN_SET(gpio, pin, pin_value);
1648         DELAY(delay_prop[2]);
1649
1650         return (0);
1651 }
1652
1653 static int
1654 awg_reset(device_t dev)
1655 {
1656         struct awg_softc *sc;
1657         int retry;
1658
1659         sc = device_get_softc(dev);
1660
1661         /* Reset PHY if necessary */
1662         if (awg_phy_reset(dev) != 0) {
1663                 device_printf(dev, "failed to reset PHY\n");
1664                 return (ENXIO);
1665         }
1666
1667         /* Soft reset all registers and logic */
1668         WR4(sc, EMAC_BASIC_CTL_1, BASIC_CTL_SOFT_RST);
1669
1670         /* Wait for soft reset bit to self-clear */
1671         for (retry = SOFT_RST_RETRY; retry > 0; retry--) {
1672                 if ((RD4(sc, EMAC_BASIC_CTL_1) & BASIC_CTL_SOFT_RST) == 0)
1673                         break;
1674                 DELAY(10);
1675         }
1676         if (retry == 0) {
1677                 device_printf(dev, "soft reset timed out\n");
1678 #ifdef AWG_DEBUG
1679                 awg_dump_regs(dev);
1680 #endif
1681                 return (ETIMEDOUT);
1682         }
1683
1684         return (0);
1685 }
1686
1687 static void
1688 awg_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1689 {
1690         if (error != 0)
1691                 return;
1692         *(bus_addr_t *)arg = segs[0].ds_addr;
1693 }
1694
1695 static int
1696 awg_setup_dma(device_t dev)
1697 {
1698         struct awg_softc *sc;
1699         int error, i;
1700
1701         sc = device_get_softc(dev);
1702
1703         /* Setup TX ring */
1704         error = bus_dma_tag_create(
1705             bus_get_dma_tag(dev),       /* Parent tag */
1706             DESC_ALIGN, 0,              /* alignment, boundary */
1707             BUS_SPACE_MAXADDR_32BIT,    /* lowaddr */
1708             BUS_SPACE_MAXADDR,          /* highaddr */
1709             NULL, NULL,                 /* filter, filterarg */
1710             TX_DESC_SIZE, 1,            /* maxsize, nsegs */
1711             TX_DESC_SIZE,               /* maxsegsize */
1712             0,                          /* flags */
1713             NULL, NULL,                 /* lockfunc, lockarg */
1714             &sc->tx.desc_tag);
1715         if (error != 0) {
1716                 device_printf(dev, "cannot create TX descriptor ring tag\n");
1717                 return (error);
1718         }
1719
1720         error = bus_dmamem_alloc(sc->tx.desc_tag, (void **)&sc->tx.desc_ring,
1721             BUS_DMA_COHERENT | BUS_DMA_WAITOK | BUS_DMA_ZERO, &sc->tx.desc_map);
1722         if (error != 0) {
1723                 device_printf(dev, "cannot allocate TX descriptor ring\n");
1724                 return (error);
1725         }
1726
1727         error = bus_dmamap_load(sc->tx.desc_tag, sc->tx.desc_map,
1728             sc->tx.desc_ring, TX_DESC_SIZE, awg_dmamap_cb,
1729             &sc->tx.desc_ring_paddr, 0);
1730         if (error != 0) {
1731                 device_printf(dev, "cannot load TX descriptor ring\n");
1732                 return (error);
1733         }
1734
1735         for (i = 0; i < TX_DESC_COUNT; i++)
1736                 sc->tx.desc_ring[i].next =
1737                     htole32(sc->tx.desc_ring_paddr + DESC_OFF(TX_NEXT(i)));
1738
1739         error = bus_dma_tag_create(
1740             bus_get_dma_tag(dev),       /* Parent tag */
1741             1, 0,                       /* alignment, boundary */
1742             BUS_SPACE_MAXADDR_32BIT,    /* lowaddr */
1743             BUS_SPACE_MAXADDR,          /* highaddr */
1744             NULL, NULL,                 /* filter, filterarg */
1745             MCLBYTES, TX_MAX_SEGS,      /* maxsize, nsegs */
1746             MCLBYTES,                   /* maxsegsize */
1747             0,                          /* flags */
1748             NULL, NULL,                 /* lockfunc, lockarg */
1749             &sc->tx.buf_tag);
1750         if (error != 0) {
1751                 device_printf(dev, "cannot create TX buffer tag\n");
1752                 return (error);
1753         }
1754
1755         sc->tx.queued = 0;
1756         for (i = 0; i < TX_DESC_COUNT; i++) {
1757                 error = bus_dmamap_create(sc->tx.buf_tag, 0,
1758                     &sc->tx.buf_map[i].map);
1759                 if (error != 0) {
1760                         device_printf(dev, "cannot create TX buffer map\n");
1761                         return (error);
1762                 }
1763         }
1764
1765         /* Setup RX ring */
1766         error = bus_dma_tag_create(
1767             bus_get_dma_tag(dev),       /* Parent tag */
1768             DESC_ALIGN, 0,              /* alignment, boundary */
1769             BUS_SPACE_MAXADDR_32BIT,    /* lowaddr */
1770             BUS_SPACE_MAXADDR,          /* highaddr */
1771             NULL, NULL,                 /* filter, filterarg */
1772             RX_DESC_SIZE, 1,            /* maxsize, nsegs */
1773             RX_DESC_SIZE,               /* maxsegsize */
1774             0,                          /* flags */
1775             NULL, NULL,                 /* lockfunc, lockarg */
1776             &sc->rx.desc_tag);
1777         if (error != 0) {
1778                 device_printf(dev, "cannot create RX descriptor ring tag\n");
1779                 return (error);
1780         }
1781
1782         error = bus_dmamem_alloc(sc->rx.desc_tag, (void **)&sc->rx.desc_ring,
1783             BUS_DMA_COHERENT | BUS_DMA_WAITOK | BUS_DMA_ZERO, &sc->rx.desc_map);
1784         if (error != 0) {
1785                 device_printf(dev, "cannot allocate RX descriptor ring\n");
1786                 return (error);
1787         }
1788
1789         error = bus_dmamap_load(sc->rx.desc_tag, sc->rx.desc_map,
1790             sc->rx.desc_ring, RX_DESC_SIZE, awg_dmamap_cb,
1791             &sc->rx.desc_ring_paddr, 0);
1792         if (error != 0) {
1793                 device_printf(dev, "cannot load RX descriptor ring\n");
1794                 return (error);
1795         }
1796
1797         error = bus_dma_tag_create(
1798             bus_get_dma_tag(dev),       /* Parent tag */
1799             1, 0,                       /* alignment, boundary */
1800             BUS_SPACE_MAXADDR_32BIT,    /* lowaddr */
1801             BUS_SPACE_MAXADDR,          /* highaddr */
1802             NULL, NULL,                 /* filter, filterarg */
1803             MCLBYTES, 1,                /* maxsize, nsegs */
1804             MCLBYTES,                   /* maxsegsize */
1805             0,                          /* flags */
1806             NULL, NULL,                 /* lockfunc, lockarg */
1807             &sc->rx.buf_tag);
1808         if (error != 0) {
1809                 device_printf(dev, "cannot create RX buffer tag\n");
1810                 return (error);
1811         }
1812
1813         error = bus_dmamap_create(sc->rx.buf_tag, 0, &sc->rx.buf_spare_map);
1814         if (error != 0) {
1815                 device_printf(dev,
1816                     "cannot create RX buffer spare map\n");
1817                 return (error);
1818         }
1819
1820         for (i = 0; i < RX_DESC_COUNT; i++) {
1821                 sc->rx.desc_ring[i].next =
1822                     htole32(sc->rx.desc_ring_paddr + DESC_OFF(RX_NEXT(i)));
1823
1824                 error = bus_dmamap_create(sc->rx.buf_tag, 0,
1825                     &sc->rx.buf_map[i].map);
1826                 if (error != 0) {
1827                         device_printf(dev, "cannot create RX buffer map\n");
1828                         return (error);
1829                 }
1830                 sc->rx.buf_map[i].mbuf = NULL;
1831                 error = awg_newbuf_rx(sc, i);
1832                 if (error != 0) {
1833                         device_printf(dev, "cannot create RX buffer\n");
1834                         return (error);
1835                 }
1836         }
1837         bus_dmamap_sync(sc->rx.desc_tag, sc->rx.desc_map,
1838             BUS_DMASYNC_PREWRITE);
1839
1840         /* Write transmit and receive descriptor base address registers */
1841         WR4(sc, EMAC_TX_DMA_LIST, sc->tx.desc_ring_paddr);
1842         WR4(sc, EMAC_RX_DMA_LIST, sc->rx.desc_ring_paddr);
1843
1844         return (0);
1845 }
1846
1847 static int
1848 awg_probe(device_t dev)
1849 {
1850         if (!ofw_bus_status_okay(dev))
1851                 return (ENXIO);
1852
1853         if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
1854                 return (ENXIO);
1855
1856         device_set_desc(dev, "Allwinner Gigabit Ethernet");
1857         return (BUS_PROBE_DEFAULT);
1858 }
1859
1860 static int
1861 awg_attach(device_t dev)
1862 {
1863         uint8_t eaddr[ETHER_ADDR_LEN];
1864         struct awg_softc *sc;
1865         int error;
1866
1867         sc = device_get_softc(dev);
1868         sc->dev = dev;
1869         sc->type = ofw_bus_search_compatible(dev, compat_data)->ocd_data;
1870
1871         if (bus_alloc_resources(dev, awg_spec, sc->res) != 0) {
1872                 device_printf(dev, "cannot allocate resources for device\n");
1873                 return (ENXIO);
1874         }
1875
1876         mtx_init(&sc->mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK, MTX_DEF);
1877         callout_init_mtx(&sc->stat_ch, &sc->mtx, 0);
1878         TASK_INIT(&sc->link_task, 0, awg_link_task, sc);
1879
1880         /* Setup clocks and regulators */
1881         error = awg_setup_extres(dev);
1882         if (error != 0)
1883                 return (error);
1884
1885         /* Read MAC address before resetting the chip */
1886         awg_get_eaddr(dev, eaddr);
1887
1888         /* Soft reset EMAC core */
1889         error = awg_reset(dev);
1890         if (error != 0)
1891                 return (error);
1892
1893         /* Setup DMA descriptors */
1894         error = awg_setup_dma(dev);
1895         if (error != 0)
1896                 return (error);
1897
1898         /* Install interrupt handler */
1899         error = bus_setup_intr(dev, sc->res[_RES_IRQ],
1900             INTR_TYPE_NET | INTR_MPSAFE, NULL, awg_intr, sc, &sc->ih);
1901         if (error != 0) {
1902                 device_printf(dev, "cannot setup interrupt handler\n");
1903                 return (error);
1904         }
1905
1906         /* Setup ethernet interface */
1907         sc->ifp = if_alloc(IFT_ETHER);
1908         if_setsoftc(sc->ifp, sc);
1909         if_initname(sc->ifp, device_get_name(dev), device_get_unit(dev));
1910         if_setflags(sc->ifp, IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST);
1911         if_setstartfn(sc->ifp, awg_start);
1912         if_setioctlfn(sc->ifp, awg_ioctl);
1913         if_setinitfn(sc->ifp, awg_init);
1914         if_setsendqlen(sc->ifp, TX_DESC_COUNT - 1);
1915         if_setsendqready(sc->ifp);
1916         if_sethwassist(sc->ifp, CSUM_IP | CSUM_UDP | CSUM_TCP);
1917         if_setcapabilities(sc->ifp, IFCAP_VLAN_MTU | IFCAP_HWCSUM);
1918         if_setcapenable(sc->ifp, if_getcapabilities(sc->ifp));
1919 #ifdef DEVICE_POLLING
1920         if_setcapabilitiesbit(sc->ifp, IFCAP_POLLING, 0);
1921 #endif
1922
1923         /* Attach MII driver */
1924         error = mii_attach(dev, &sc->miibus, sc->ifp, awg_media_change,
1925             awg_media_status, BMSR_DEFCAPMASK, MII_PHY_ANY, MII_OFFSET_ANY,
1926             MIIF_DOPAUSE);
1927         if (error != 0) {
1928                 device_printf(dev, "cannot attach PHY\n");
1929                 return (error);
1930         }
1931
1932         /* Attach ethernet interface */
1933         ether_ifattach(sc->ifp, eaddr);
1934
1935         return (0);
1936 }
1937
1938 static device_method_t awg_methods[] = {
1939         /* Device interface */
1940         DEVMETHOD(device_probe,         awg_probe),
1941         DEVMETHOD(device_attach,        awg_attach),
1942
1943         /* MII interface */
1944         DEVMETHOD(miibus_readreg,       awg_miibus_readreg),
1945         DEVMETHOD(miibus_writereg,      awg_miibus_writereg),
1946         DEVMETHOD(miibus_statchg,       awg_miibus_statchg),
1947
1948         DEVMETHOD_END
1949 };
1950
1951 static driver_t awg_driver = {
1952         "awg",
1953         awg_methods,
1954         sizeof(struct awg_softc),
1955 };
1956
1957 static devclass_t awg_devclass;
1958
1959 DRIVER_MODULE(awg, simplebus, awg_driver, awg_devclass, 0, 0);
1960 DRIVER_MODULE(miibus, awg, miibus_driver, miibus_devclass, 0, 0);
1961
1962 MODULE_DEPEND(awg, ether, 1, 1, 1);
1963 MODULE_DEPEND(awg, miibus, 1, 1, 1);