Home | History | Annotate | Line # | Download | only in sunxi
sunxi_emac.c revision 1.20
      1 /* $NetBSD: sunxi_emac.c,v 1.20 2018/10/18 13:33:10 martin Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2016-2017 Jared McNeill <jmcneill (at) invisible.ca>
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  * SUCH DAMAGE.
     27  */
     28 
     29 /*
     30  * Allwinner Gigabit Ethernet MAC (EMAC) controller
     31  */
     32 
     33 #include "opt_net_mpsafe.h"
     34 
     35 #include <sys/cdefs.h>
     36 __KERNEL_RCSID(0, "$NetBSD: sunxi_emac.c,v 1.20 2018/10/18 13:33:10 martin Exp $");
     37 
     38 #include <sys/param.h>
     39 #include <sys/bus.h>
     40 #include <sys/device.h>
     41 #include <sys/intr.h>
     42 #include <sys/systm.h>
     43 #include <sys/kernel.h>
     44 #include <sys/mutex.h>
     45 #include <sys/callout.h>
     46 #include <sys/gpio.h>
     47 #include <sys/cprng.h>
     48 
     49 #include <net/if.h>
     50 #include <net/if_dl.h>
     51 #include <net/if_ether.h>
     52 #include <net/if_media.h>
     53 #include <net/bpf.h>
     54 
     55 #include <dev/mii/miivar.h>
     56 
     57 #include <dev/fdt/fdtvar.h>
     58 #include <dev/fdt/syscon.h>
     59 
     60 #include <arm/sunxi/sunxi_emac.h>
     61 
     62 #ifdef NET_MPSAFE
     63 #define	EMAC_MPSAFE		1
     64 #define	CALLOUT_FLAGS		CALLOUT_MPSAFE
     65 #define	FDT_INTR_FLAGS		FDT_INTR_MPSAFE
     66 #else
     67 #define	CALLOUT_FLAGS		0
     68 #define	FDT_INTR_FLAGS		0
     69 #endif
     70 
     71 #define	EMAC_IFNAME		"emac%d"
     72 
     73 #define	ETHER_ALIGN		2
     74 
     75 #define	EMAC_LOCK(sc)		mutex_enter(&(sc)->mtx)
     76 #define	EMAC_UNLOCK(sc)		mutex_exit(&(sc)->mtx)
     77 #define	EMAC_ASSERT_LOCKED(sc)	KASSERT(mutex_owned(&(sc)->mtx))
     78 
     79 #define	DESC_ALIGN		sizeof(struct sunxi_emac_desc)
     80 #define	TX_DESC_COUNT		1024
     81 #define	TX_DESC_SIZE		(sizeof(struct sunxi_emac_desc) * TX_DESC_COUNT)
     82 #define	RX_DESC_COUNT		256
     83 #define	RX_DESC_SIZE		(sizeof(struct sunxi_emac_desc) * RX_DESC_COUNT)
     84 
     85 #define	DESC_OFF(n)		((n) * sizeof(struct sunxi_emac_desc))
     86 #define	TX_NEXT(n)		(((n) + 1) & (TX_DESC_COUNT - 1))
     87 #define	TX_SKIP(n, o)		(((n) + (o)) & (TX_DESC_COUNT - 1))
     88 #define	RX_NEXT(n)		(((n) + 1) & (RX_DESC_COUNT - 1))
     89 
     90 #define	TX_MAX_SEGS		128
     91 
     92 #define	SOFT_RST_RETRY		1000
     93 #define	MII_BUSY_RETRY		1000
     94 #define	MDIO_FREQ		2500000
     95 
     96 #define	BURST_LEN_DEFAULT	8
     97 #define	RX_TX_PRI_DEFAULT	0
     98 #define	PAUSE_TIME_DEFAULT	0x400
     99 
    100 /* syscon EMAC clock register */
    101 #define	EMAC_CLK_REG		0x30
    102 #define	 EMAC_CLK_EPHY_ADDR		(0x1f << 20)	/* H3 */
    103 #define	 EMAC_CLK_EPHY_ADDR_SHIFT	20
    104 #define	 EMAC_CLK_EPHY_LED_POL		(1 << 17)	/* H3 */
    105 #define	 EMAC_CLK_EPHY_SHUTDOWN		(1 << 16)	/* H3 */
    106 #define	 EMAC_CLK_EPHY_SELECT		(1 << 15)	/* H3 */
    107 #define	 EMAC_CLK_RMII_EN		(1 << 13)
    108 #define	 EMAC_CLK_ETXDC			(0x7 << 10)
    109 #define	 EMAC_CLK_ETXDC_SHIFT		10
    110 #define	 EMAC_CLK_ERXDC			(0x1f << 5)
    111 #define	 EMAC_CLK_ERXDC_SHIFT		5
    112 #define	 EMAC_CLK_PIT			(0x1 << 2)
    113 #define	  EMAC_CLK_PIT_MII		(0 << 2)
    114 #define	  EMAC_CLK_PIT_RGMII		(1 << 2)
    115 #define	 EMAC_CLK_SRC			(0x3 << 0)
    116 #define	  EMAC_CLK_SRC_MII		(0 << 0)
    117 #define	  EMAC_CLK_SRC_EXT_RGMII	(1 << 0)
    118 #define	  EMAC_CLK_SRC_RGMII		(2 << 0)
    119 
    120 /* Burst length of RX and TX DMA transfers */
    121 static int sunxi_emac_burst_len = BURST_LEN_DEFAULT;
    122 
    123 /* RX / TX DMA priority. If 1, RX DMA has priority over TX DMA. */
    124 static int sunxi_emac_rx_tx_pri = RX_TX_PRI_DEFAULT;
    125 
    126 /* Pause time field in the transmitted control frame */
    127 static int sunxi_emac_pause_time = PAUSE_TIME_DEFAULT;
    128 
    129 enum sunxi_emac_type {
    130 	EMAC_A64 = 1,
    131 	EMAC_A83T,
    132 	EMAC_H3,
    133 	EMAC_H6,
    134 };
    135 
    136 static const struct of_compat_data compat_data[] = {
    137 	{ "allwinner,sun8i-a83t-emac",	EMAC_A83T },
    138 	{ "allwinner,sun8i-h3-emac",	EMAC_H3 },
    139 	{ "allwinner,sun50i-a64-emac",	EMAC_A64 },
    140 	{ "allwinner,sun50i-h6-emac",	EMAC_H6 },
    141 	{ NULL }
    142 };
    143 
    144 struct sunxi_emac_bufmap {
    145 	bus_dmamap_t		map;
    146 	struct mbuf		*mbuf;
    147 };
    148 
    149 struct sunxi_emac_txring {
    150 	bus_dma_tag_t		desc_tag;
    151 	bus_dmamap_t		desc_map;
    152 	bus_dma_segment_t	desc_dmaseg;
    153 	struct sunxi_emac_desc	*desc_ring;
    154 	bus_addr_t		desc_ring_paddr;
    155 	bus_dma_tag_t		buf_tag;
    156 	struct sunxi_emac_bufmap buf_map[TX_DESC_COUNT];
    157 	u_int			cur, next, queued;
    158 };
    159 
    160 struct sunxi_emac_rxring {
    161 	bus_dma_tag_t		desc_tag;
    162 	bus_dmamap_t		desc_map;
    163 	bus_dma_segment_t	desc_dmaseg;
    164 	struct sunxi_emac_desc	*desc_ring;
    165 	bus_addr_t		desc_ring_paddr;
    166 	bus_dma_tag_t		buf_tag;
    167 	struct sunxi_emac_bufmap buf_map[RX_DESC_COUNT];
    168 	u_int			cur;
    169 };
    170 
    171 struct sunxi_emac_softc {
    172 	device_t		dev;
    173 	int			phandle;
    174 	enum sunxi_emac_type	type;
    175 	bus_space_tag_t		bst;
    176 	bus_dma_tag_t		dmat;
    177 
    178 	bus_space_handle_t	bsh;
    179 	struct clk		*clk_ahb;
    180 	struct clk		*clk_ephy;
    181 	struct fdtbus_reset	*rst_ahb;
    182 	struct fdtbus_reset	*rst_ephy;
    183 	struct fdtbus_regulator	*reg_phy;
    184 	struct fdtbus_gpio_pin	*pin_reset;
    185 
    186 	struct syscon		*syscon;
    187 
    188 	int			phy_id;
    189 
    190 	kmutex_t		mtx;
    191 	struct ethercom		ec;
    192 	struct mii_data		mii;
    193 	callout_t		stat_ch;
    194 	void			*ih;
    195 	u_int			mdc_div_ratio_m;
    196 
    197 	struct sunxi_emac_txring	tx;
    198 	struct sunxi_emac_rxring	rx;
    199 };
    200 
    201 #define	RD4(sc, reg)			\
    202 	bus_space_read_4((sc)->bst, (sc)->bsh, (reg))
    203 #define	WR4(sc, reg, val)		\
    204 	bus_space_write_4((sc)->bst, (sc)->bsh, (reg), (val))
    205 
    206 static int
    207 sunxi_emac_mii_readreg(device_t dev, int phy, int reg)
    208 {
    209 	struct sunxi_emac_softc *sc = device_private(dev);
    210 	int retry, val;
    211 
    212 	val = 0;
    213 
    214 	WR4(sc, EMAC_MII_CMD,
    215 	    (sc->mdc_div_ratio_m << MDC_DIV_RATIO_M_SHIFT) |
    216 	    (phy << PHY_ADDR_SHIFT) |
    217 	    (reg << PHY_REG_ADDR_SHIFT) |
    218 	    MII_BUSY);
    219 	for (retry = MII_BUSY_RETRY; retry > 0; retry--) {
    220 		if ((RD4(sc, EMAC_MII_CMD) & MII_BUSY) == 0) {
    221 			val = RD4(sc, EMAC_MII_DATA);
    222 			break;
    223 		}
    224 		delay(10);
    225 	}
    226 
    227 	if (retry == 0)
    228 		device_printf(dev, "phy read timeout, phy=%d reg=%d\n",
    229 		    phy, reg);
    230 
    231 	return val;
    232 }
    233 
    234 static void
    235 sunxi_emac_mii_writereg(device_t dev, int phy, int reg, int val)
    236 {
    237 	struct sunxi_emac_softc *sc = device_private(dev);
    238 	int retry;
    239 
    240 	WR4(sc, EMAC_MII_DATA, val);
    241 	WR4(sc, EMAC_MII_CMD,
    242 	    (sc->mdc_div_ratio_m << MDC_DIV_RATIO_M_SHIFT) |
    243 	    (phy << PHY_ADDR_SHIFT) |
    244 	    (reg << PHY_REG_ADDR_SHIFT) |
    245 	    MII_WR | MII_BUSY);
    246 	for (retry = MII_BUSY_RETRY; retry > 0; retry--) {
    247 		if ((RD4(sc, EMAC_MII_CMD) & MII_BUSY) == 0)
    248 			break;
    249 		delay(10);
    250 	}
    251 
    252 	if (retry == 0)
    253 		device_printf(dev, "phy write timeout, phy=%d reg=%d\n",
    254 		    phy, reg);
    255 }
    256 
    257 static void
    258 sunxi_emac_update_link(struct sunxi_emac_softc *sc)
    259 {
    260 	struct mii_data *mii = &sc->mii;
    261 	uint32_t val;
    262 
    263 	val = RD4(sc, EMAC_BASIC_CTL_0);
    264 	val &= ~(BASIC_CTL_SPEED | BASIC_CTL_DUPLEX);
    265 
    266 	if (IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_T ||
    267 	    IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_SX)
    268 		val |= BASIC_CTL_SPEED_1000 << BASIC_CTL_SPEED_SHIFT;
    269 	else if (IFM_SUBTYPE(mii->mii_media_active) == IFM_100_TX)
    270 		val |= BASIC_CTL_SPEED_100 << BASIC_CTL_SPEED_SHIFT;
    271 	else
    272 		val |= BASIC_CTL_SPEED_10 << BASIC_CTL_SPEED_SHIFT;
    273 
    274 	if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) != 0)
    275 		val |= BASIC_CTL_DUPLEX;
    276 
    277 	WR4(sc, EMAC_BASIC_CTL_0, val);
    278 
    279 	val = RD4(sc, EMAC_RX_CTL_0);
    280 	val &= ~RX_FLOW_CTL_EN;
    281 	if ((IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_RXPAUSE) != 0)
    282 		val |= RX_FLOW_CTL_EN;
    283 	WR4(sc, EMAC_RX_CTL_0, val);
    284 
    285 	val = RD4(sc, EMAC_TX_FLOW_CTL);
    286 	val &= ~(PAUSE_TIME|TX_FLOW_CTL_EN);
    287 	if ((IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_TXPAUSE) != 0)
    288 		val |= TX_FLOW_CTL_EN;
    289 	if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) != 0)
    290 		val |= sunxi_emac_pause_time << PAUSE_TIME_SHIFT;
    291 	WR4(sc, EMAC_TX_FLOW_CTL, val);
    292 }
    293 
    294 static void
    295 sunxi_emac_mii_statchg(struct ifnet *ifp)
    296 {
    297 	struct sunxi_emac_softc * const sc = ifp->if_softc;
    298 
    299 	sunxi_emac_update_link(sc);
    300 }
    301 
    302 static void
    303 sunxi_emac_dma_sync(struct sunxi_emac_softc *sc, bus_dma_tag_t dmat,
    304     bus_dmamap_t map, int start, int end, int total, int flags)
    305 {
    306 	if (end > start) {
    307 		bus_dmamap_sync(dmat, map, DESC_OFF(start),
    308 		    DESC_OFF(end) - DESC_OFF(start), flags);
    309 	} else {
    310 		bus_dmamap_sync(dmat, map, DESC_OFF(start),
    311 		    DESC_OFF(total) - DESC_OFF(start), flags);
    312 		if (DESC_OFF(end) - DESC_OFF(0) > 0)
    313 			bus_dmamap_sync(dmat, map, DESC_OFF(0),
    314 			    DESC_OFF(end) - DESC_OFF(0), flags);
    315 	}
    316 }
    317 
    318 static void
    319 sunxi_emac_setup_txdesc(struct sunxi_emac_softc *sc, int index, int flags,
    320     bus_addr_t paddr, u_int len)
    321 {
    322 	uint32_t status, size;
    323 
    324 	if (paddr == 0 || len == 0) {
    325 		status = 0;
    326 		size = 0;
    327 		--sc->tx.queued;
    328 	} else {
    329 		status = TX_DESC_CTL;
    330 		size = flags | len;
    331 		++sc->tx.queued;
    332 	}
    333 
    334 	sc->tx.desc_ring[index].addr = htole32((uint32_t)paddr);
    335 	sc->tx.desc_ring[index].size = htole32(size);
    336 	sc->tx.desc_ring[index].status = htole32(status);
    337 }
    338 
    339 static int
    340 sunxi_emac_setup_txbuf(struct sunxi_emac_softc *sc, int index, struct mbuf *m)
    341 {
    342 	bus_dma_segment_t *segs;
    343 	int error, nsegs, cur, i, flags;
    344 	u_int csum_flags;
    345 
    346 	error = bus_dmamap_load_mbuf(sc->tx.buf_tag,
    347 	    sc->tx.buf_map[index].map, m, BUS_DMA_WRITE|BUS_DMA_NOWAIT);
    348 	if (error == EFBIG) {
    349 		device_printf(sc->dev,
    350 		    "TX packet needs too many DMA segments, dropping...\n");
    351 		m_freem(m);
    352 		return 0;
    353 	}
    354 	if (error != 0)
    355 		return 0;
    356 
    357 	segs = sc->tx.buf_map[index].map->dm_segs;
    358 	nsegs = sc->tx.buf_map[index].map->dm_nsegs;
    359 
    360 	flags = TX_FIR_DESC;
    361 	if ((m->m_pkthdr.csum_flags & M_CSUM_IPv4) != 0) {
    362 		if ((m->m_pkthdr.csum_flags & (M_CSUM_TCPv4|M_CSUM_UDPv4)) != 0)
    363 			csum_flags = TX_CHECKSUM_CTL_FULL;
    364 		else
    365 			csum_flags = TX_CHECKSUM_CTL_IP;
    366 		flags |= (csum_flags << TX_CHECKSUM_CTL_SHIFT);
    367 	}
    368 
    369 	for (cur = index, i = 0; i < nsegs; i++) {
    370 		sc->tx.buf_map[cur].mbuf = (i == 0 ? m : NULL);
    371 		if (i == nsegs - 1)
    372 			flags |= TX_LAST_DESC | TX_INT_CTL;
    373 
    374 		sunxi_emac_setup_txdesc(sc, cur, flags, segs[i].ds_addr,
    375 		    segs[i].ds_len);
    376 		flags &= ~TX_FIR_DESC;
    377 		cur = TX_NEXT(cur);
    378 	}
    379 
    380 	bus_dmamap_sync(sc->tx.buf_tag, sc->tx.buf_map[index].map,
    381 	    0, sc->tx.buf_map[index].map->dm_mapsize, BUS_DMASYNC_PREWRITE);
    382 
    383 	return nsegs;
    384 }
    385 
    386 static void
    387 sunxi_emac_setup_rxdesc(struct sunxi_emac_softc *sc, int index,
    388     bus_addr_t paddr)
    389 {
    390 	uint32_t status, size;
    391 
    392 	status = RX_DESC_CTL;
    393 	size = MCLBYTES - 1;
    394 
    395 	sc->rx.desc_ring[index].addr = htole32((uint32_t)paddr);
    396 	sc->rx.desc_ring[index].size = htole32(size);
    397 	sc->rx.desc_ring[index].next =
    398 	    htole32(sc->rx.desc_ring_paddr + DESC_OFF(RX_NEXT(index)));
    399 	sc->rx.desc_ring[index].status = htole32(status);
    400 }
    401 
    402 static int
    403 sunxi_emac_setup_rxbuf(struct sunxi_emac_softc *sc, int index, struct mbuf *m)
    404 {
    405 	int error;
    406 
    407 	m_adj(m, ETHER_ALIGN);
    408 
    409 	error = bus_dmamap_load_mbuf(sc->rx.buf_tag,
    410 	    sc->rx.buf_map[index].map, m, BUS_DMA_READ|BUS_DMA_NOWAIT);
    411 	if (error != 0)
    412 		return error;
    413 
    414 	bus_dmamap_sync(sc->rx.buf_tag, sc->rx.buf_map[index].map,
    415 	    0, sc->rx.buf_map[index].map->dm_mapsize,
    416 	    BUS_DMASYNC_PREREAD);
    417 
    418 	sc->rx.buf_map[index].mbuf = m;
    419 	sunxi_emac_setup_rxdesc(sc, index,
    420 	    sc->rx.buf_map[index].map->dm_segs[0].ds_addr);
    421 
    422 	return 0;
    423 }
    424 
    425 static struct mbuf *
    426 sunxi_emac_alloc_mbufcl(struct sunxi_emac_softc *sc)
    427 {
    428 	struct mbuf *m;
    429 
    430 	m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
    431 	if (m != NULL)
    432 		m->m_pkthdr.len = m->m_len = m->m_ext.ext_size;
    433 
    434 	return m;
    435 }
    436 
    437 static void
    438 sunxi_emac_start_locked(struct sunxi_emac_softc *sc)
    439 {
    440 	struct ifnet *ifp = &sc->ec.ec_if;
    441 	struct mbuf *m;
    442 	uint32_t val;
    443 	int cnt, nsegs, start;
    444 
    445 	EMAC_ASSERT_LOCKED(sc);
    446 
    447 	if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
    448 		return;
    449 
    450 	for (cnt = 0, start = sc->tx.cur; ; cnt++) {
    451 		if (sc->tx.queued >= TX_DESC_COUNT - TX_MAX_SEGS) {
    452 			ifp->if_flags |= IFF_OACTIVE;
    453 			break;
    454 		}
    455 
    456 		IFQ_POLL(&ifp->if_snd, m);
    457 		if (m == NULL)
    458 			break;
    459 
    460 		nsegs = sunxi_emac_setup_txbuf(sc, sc->tx.cur, m);
    461 		if (nsegs == 0) {
    462 			ifp->if_flags |= IFF_OACTIVE;
    463 			break;
    464 		}
    465 		IFQ_DEQUEUE(&ifp->if_snd, m);
    466 		bpf_mtap(ifp, m, BPF_D_OUT);
    467 
    468 		sc->tx.cur = TX_SKIP(sc->tx.cur, nsegs);
    469 	}
    470 
    471 	if (cnt != 0) {
    472 		sunxi_emac_dma_sync(sc, sc->tx.desc_tag, sc->tx.desc_map,
    473 		    start, sc->tx.cur, TX_DESC_COUNT,
    474 		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
    475 
    476 		/* Start and run TX DMA */
    477 		val = RD4(sc, EMAC_TX_CTL_1);
    478 		WR4(sc, EMAC_TX_CTL_1, val | TX_DMA_START);
    479 	}
    480 }
    481 
    482 static void
    483 sunxi_emac_start(struct ifnet *ifp)
    484 {
    485 	struct sunxi_emac_softc *sc = ifp->if_softc;
    486 
    487 	EMAC_LOCK(sc);
    488 	sunxi_emac_start_locked(sc);
    489 	EMAC_UNLOCK(sc);
    490 }
    491 
    492 static void
    493 sunxi_emac_tick(void *softc)
    494 {
    495 	struct sunxi_emac_softc *sc = softc;
    496 	struct mii_data *mii = &sc->mii;
    497 #ifndef EMAC_MPSAFE
    498 	int s = splnet();
    499 #endif
    500 
    501 	EMAC_LOCK(sc);
    502 	mii_tick(mii);
    503 	callout_schedule(&sc->stat_ch, hz);
    504 	EMAC_UNLOCK(sc);
    505 
    506 #ifndef EMAC_MPSAFE
    507 	splx(s);
    508 #endif
    509 }
    510 
    511 /* Bit Reversal - http://aggregate.org/MAGIC/#Bit%20Reversal */
    512 static uint32_t
    513 bitrev32(uint32_t x)
    514 {
    515 	x = (((x & 0xaaaaaaaa) >> 1) | ((x & 0x55555555) << 1));
    516 	x = (((x & 0xcccccccc) >> 2) | ((x & 0x33333333) << 2));
    517 	x = (((x & 0xf0f0f0f0) >> 4) | ((x & 0x0f0f0f0f) << 4));
    518 	x = (((x & 0xff00ff00) >> 8) | ((x & 0x00ff00ff) << 8));
    519 
    520 	return (x >> 16) | (x << 16);
    521 }
    522 
    523 static void
    524 sunxi_emac_setup_rxfilter(struct sunxi_emac_softc *sc)
    525 {
    526 	struct ifnet *ifp = &sc->ec.ec_if;
    527 	uint32_t val, crc, hashreg, hashbit, hash[2], machi, maclo;
    528 	struct ether_multi *enm;
    529 	struct ether_multistep step;
    530 	const uint8_t *eaddr;
    531 
    532 	EMAC_ASSERT_LOCKED(sc);
    533 
    534 	val = 0;
    535 	hash[0] = hash[1] = 0;
    536 
    537 	if ((ifp->if_flags & IFF_PROMISC) != 0)
    538 		val |= DIS_ADDR_FILTER;
    539 	else if ((ifp->if_flags & IFF_ALLMULTI) != 0) {
    540 		val |= RX_ALL_MULTICAST;
    541 		hash[0] = hash[1] = ~0;
    542 	} else {
    543 		val |= HASH_MULTICAST;
    544 		ETHER_FIRST_MULTI(step, &sc->ec, enm);
    545 		while (enm != NULL) {
    546 			crc = ether_crc32_le(enm->enm_addrlo, ETHER_ADDR_LEN);
    547 			crc &= 0x7f;
    548 			crc = bitrev32(~crc) >> 26;
    549 			hashreg = (crc >> 5);
    550 			hashbit = (crc & 0x1f);
    551 			hash[hashreg] |= (1 << hashbit);
    552 			ETHER_NEXT_MULTI(step, enm);
    553 		}
    554 	}
    555 
    556 	/* Write our unicast address */
    557 	eaddr = CLLADDR(ifp->if_sadl);
    558 	machi = (eaddr[5] << 8) | eaddr[4];
    559 	maclo = (eaddr[3] << 24) | (eaddr[2] << 16) | (eaddr[1] << 8) |
    560 	   (eaddr[0] << 0);
    561 	WR4(sc, EMAC_ADDR_HIGH(0), machi);
    562 	WR4(sc, EMAC_ADDR_LOW(0), maclo);
    563 
    564 	/* Multicast hash filters */
    565 	WR4(sc, EMAC_RX_HASH_0, hash[1]);
    566 	WR4(sc, EMAC_RX_HASH_1, hash[0]);
    567 
    568 	/* RX frame filter config */
    569 	WR4(sc, EMAC_RX_FRM_FLT, val);
    570 }
    571 
    572 static void
    573 sunxi_emac_enable_intr(struct sunxi_emac_softc *sc)
    574 {
    575 	/* Enable interrupts */
    576 	WR4(sc, EMAC_INT_EN, RX_INT_EN | TX_INT_EN | TX_BUF_UA_INT_EN);
    577 }
    578 
    579 static void
    580 sunxi_emac_disable_intr(struct sunxi_emac_softc *sc)
    581 {
    582 	/* Disable interrupts */
    583 	WR4(sc, EMAC_INT_EN, 0);
    584 }
    585 
    586 #ifdef SUNXI_EMAC_DEBUG
    587 static void
    588 sunxi_emac_dump_regs(struct sunxi_emac_softc *sc)
    589 {
    590 	static const struct {
    591 		const char *name;
    592 		u_int reg;
    593 	} regs[] = {
    594 		{ "BASIC_CTL_0", EMAC_BASIC_CTL_0 },
    595 		{ "BASIC_CTL_1", EMAC_BASIC_CTL_1 },
    596 		{ "INT_STA", EMAC_INT_STA },
    597 		{ "INT_EN", EMAC_INT_EN },
    598 		{ "TX_CTL_0", EMAC_TX_CTL_0 },
    599 		{ "TX_CTL_1", EMAC_TX_CTL_1 },
    600 		{ "TX_FLOW_CTL", EMAC_TX_FLOW_CTL },
    601 		{ "TX_DMA_LIST", EMAC_TX_DMA_LIST },
    602 		{ "RX_CTL_0", EMAC_RX_CTL_0 },
    603 		{ "RX_CTL_1", EMAC_RX_CTL_1 },
    604 		{ "RX_DMA_LIST", EMAC_RX_DMA_LIST },
    605 		{ "RX_FRM_FLT", EMAC_RX_FRM_FLT },
    606 		{ "RX_HASH_0", EMAC_RX_HASH_0 },
    607 		{ "RX_HASH_1", EMAC_RX_HASH_1 },
    608 		{ "MII_CMD", EMAC_MII_CMD },
    609 		{ "ADDR_HIGH0", EMAC_ADDR_HIGH(0) },
    610 		{ "ADDR_LOW0", EMAC_ADDR_LOW(0) },
    611 		{ "TX_DMA_STA", EMAC_TX_DMA_STA },
    612 		{ "TX_DMA_CUR_DESC", EMAC_TX_DMA_CUR_DESC },
    613 		{ "TX_DMA_CUR_BUF", EMAC_TX_DMA_CUR_BUF },
    614 		{ "RX_DMA_STA", EMAC_RX_DMA_STA },
    615 		{ "RX_DMA_CUR_DESC", EMAC_RX_DMA_CUR_DESC },
    616 		{ "RX_DMA_CUR_BUF", EMAC_RX_DMA_CUR_BUF },
    617 		{ "RGMII_STA", EMAC_RGMII_STA },
    618 	};
    619 	u_int n;
    620 
    621 	for (n = 0; n < __arraycount(regs); n++)
    622 		device_printf(sc->dev, "  %-20s %08x\n", regs[n].name,
    623 		    RD4(sc, regs[n].reg));
    624 }
    625 #endif
    626 
    627 static int
    628 sunxi_emac_reset(struct sunxi_emac_softc *sc)
    629 {
    630 	int retry;
    631 
    632 	/* Soft reset all registers and logic */
    633 	WR4(sc, EMAC_BASIC_CTL_1, BASIC_CTL_SOFT_RST);
    634 
    635 	/* Wait for soft reset bit to self-clear */
    636 	for (retry = SOFT_RST_RETRY; retry > 0; retry--) {
    637 		if ((RD4(sc, EMAC_BASIC_CTL_1) & BASIC_CTL_SOFT_RST) == 0)
    638 			break;
    639 		delay(10);
    640 	}
    641 	if (retry == 0) {
    642 		aprint_debug_dev(sc->dev, "soft reset timed out\n");
    643 #ifdef SUNXI_EMAC_DEBUG
    644 		sunxi_emac_dump_regs(sc);
    645 #endif
    646 		return ETIMEDOUT;
    647 	}
    648 
    649 	return 0;
    650 }
    651 
    652 static int
    653 sunxi_emac_init_locked(struct sunxi_emac_softc *sc)
    654 {
    655 	struct ifnet *ifp = &sc->ec.ec_if;
    656 	struct mii_data *mii = &sc->mii;
    657 	uint32_t val;
    658 
    659 	EMAC_ASSERT_LOCKED(sc);
    660 
    661 	if ((ifp->if_flags & IFF_RUNNING) != 0)
    662 		return 0;
    663 
    664 	/* Soft reset EMAC core */
    665 	sunxi_emac_reset(sc);
    666 
    667 	/* Write transmit and receive descriptor base address registers */
    668 	WR4(sc, EMAC_TX_DMA_LIST, sc->tx.desc_ring_paddr);
    669 	WR4(sc, EMAC_RX_DMA_LIST, sc->rx.desc_ring_paddr);
    670 
    671 	sunxi_emac_setup_rxfilter(sc);
    672 
    673 	/* Configure DMA burst length and priorities */
    674 	val = sunxi_emac_burst_len << BASIC_CTL_BURST_LEN_SHIFT;
    675 	if (sunxi_emac_rx_tx_pri)
    676 		val |= BASIC_CTL_RX_TX_PRI;
    677 	WR4(sc, EMAC_BASIC_CTL_1, val);
    678 
    679 	/* Enable interrupts */
    680 	sunxi_emac_enable_intr(sc);
    681 
    682 	/* Enable transmit DMA */
    683 	val = RD4(sc, EMAC_TX_CTL_1);
    684 	WR4(sc, EMAC_TX_CTL_1, val | TX_DMA_EN | TX_MD | TX_NEXT_FRAME);
    685 
    686 	/* Enable receive DMA */
    687 	val = RD4(sc, EMAC_RX_CTL_1);
    688 	WR4(sc, EMAC_RX_CTL_1, val | RX_DMA_EN | RX_MD);
    689 
    690 	/* Enable transmitter */
    691 	val = RD4(sc, EMAC_TX_CTL_0);
    692 	WR4(sc, EMAC_TX_CTL_0, val | TX_EN);
    693 
    694 	/* Enable receiver */
    695 	val = RD4(sc, EMAC_RX_CTL_0);
    696 	WR4(sc, EMAC_RX_CTL_0, val | RX_EN | CHECK_CRC);
    697 
    698 	ifp->if_flags |= IFF_RUNNING;
    699 	ifp->if_flags &= ~IFF_OACTIVE;
    700 
    701 	mii_mediachg(mii);
    702 	callout_schedule(&sc->stat_ch, hz);
    703 
    704 	return 0;
    705 }
    706 
    707 static int
    708 sunxi_emac_init(struct ifnet *ifp)
    709 {
    710 	struct sunxi_emac_softc *sc = ifp->if_softc;
    711 	int error;
    712 
    713 	EMAC_LOCK(sc);
    714 	error = sunxi_emac_init_locked(sc);
    715 	EMAC_UNLOCK(sc);
    716 
    717 	return error;
    718 }
    719 
    720 static void
    721 sunxi_emac_stop_locked(struct sunxi_emac_softc *sc, int disable)
    722 {
    723 	struct ifnet *ifp = &sc->ec.ec_if;
    724 	uint32_t val;
    725 
    726 	EMAC_ASSERT_LOCKED(sc);
    727 
    728 	callout_stop(&sc->stat_ch);
    729 
    730 	mii_down(&sc->mii);
    731 
    732 	/* Stop transmit DMA and flush data in the TX FIFO */
    733 	val = RD4(sc, EMAC_TX_CTL_1);
    734 	val &= ~TX_DMA_EN;
    735 	val |= FLUSH_TX_FIFO;
    736 	WR4(sc, EMAC_TX_CTL_1, val);
    737 
    738 	/* Disable transmitter */
    739 	val = RD4(sc, EMAC_TX_CTL_0);
    740 	WR4(sc, EMAC_TX_CTL_0, val & ~TX_EN);
    741 
    742 	/* Disable receiver */
    743 	val = RD4(sc, EMAC_RX_CTL_0);
    744 	WR4(sc, EMAC_RX_CTL_0, val & ~RX_EN);
    745 
    746 	/* Disable interrupts */
    747 	sunxi_emac_disable_intr(sc);
    748 
    749 	/* Disable transmit DMA */
    750 	val = RD4(sc, EMAC_TX_CTL_1);
    751 	WR4(sc, EMAC_TX_CTL_1, val & ~TX_DMA_EN);
    752 
    753 	/* Disable receive DMA */
    754 	val = RD4(sc, EMAC_RX_CTL_1);
    755 	WR4(sc, EMAC_RX_CTL_1, val & ~RX_DMA_EN);
    756 
    757 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
    758 }
    759 
    760 static void
    761 sunxi_emac_stop(struct ifnet *ifp, int disable)
    762 {
    763 	struct sunxi_emac_softc * const sc = ifp->if_softc;
    764 
    765 	EMAC_LOCK(sc);
    766 	sunxi_emac_stop_locked(sc, disable);
    767 	EMAC_UNLOCK(sc);
    768 }
    769 
    770 static int
    771 sunxi_emac_rxintr(struct sunxi_emac_softc *sc)
    772 {
    773 	struct ifnet *ifp = &sc->ec.ec_if;
    774 	int error, index, len, npkt;
    775 	struct mbuf *m, *m0;
    776 	uint32_t status;
    777 
    778 	npkt = 0;
    779 
    780 	for (index = sc->rx.cur; ; index = RX_NEXT(index)) {
    781 		sunxi_emac_dma_sync(sc, sc->rx.desc_tag, sc->rx.desc_map,
    782 		    index, index + 1,
    783 		    RX_DESC_COUNT, BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
    784 
    785 		status = le32toh(sc->rx.desc_ring[index].status);
    786 		if ((status & RX_DESC_CTL) != 0)
    787 			break;
    788 
    789 		bus_dmamap_sync(sc->rx.buf_tag, sc->rx.buf_map[index].map,
    790 		    0, sc->rx.buf_map[index].map->dm_mapsize,
    791 		    BUS_DMASYNC_POSTREAD);
    792 		bus_dmamap_unload(sc->rx.buf_tag, sc->rx.buf_map[index].map);
    793 
    794 		len = (status & RX_FRM_LEN) >> RX_FRM_LEN_SHIFT;
    795 		if (len != 0) {
    796 			m = sc->rx.buf_map[index].mbuf;
    797 			m_set_rcvif(m, ifp);
    798 			m->m_flags |= M_HASFCS;
    799 			m->m_pkthdr.len = len;
    800 			m->m_len = len;
    801 			m->m_nextpkt = NULL;
    802 
    803 			if ((ifp->if_capenable & IFCAP_CSUM_IPv4_Rx) != 0 &&
    804 			    (status & RX_FRM_TYPE) != 0) {
    805 				m->m_pkthdr.csum_flags = M_CSUM_IPv4 |
    806 				    M_CSUM_TCPv4 | M_CSUM_UDPv4;
    807 				if ((status & RX_HEADER_ERR) != 0)
    808 					m->m_pkthdr.csum_flags |=
    809 					    M_CSUM_IPv4_BAD;
    810 				if ((status & RX_PAYLOAD_ERR) != 0)
    811 					m->m_pkthdr.csum_flags |=
    812 					    M_CSUM_TCP_UDP_BAD;
    813 			}
    814 
    815 			++npkt;
    816 
    817 			if_percpuq_enqueue(ifp->if_percpuq, m);
    818 		}
    819 
    820 		if ((m0 = sunxi_emac_alloc_mbufcl(sc)) != NULL) {
    821 			error = sunxi_emac_setup_rxbuf(sc, index, m0);
    822 			if (error != 0) {
    823 				/* XXX hole in RX ring */
    824 			}
    825 		} else
    826 			ifp->if_ierrors++;
    827 
    828 		sunxi_emac_dma_sync(sc, sc->rx.desc_tag, sc->rx.desc_map,
    829 		    index, index + 1,
    830 		    RX_DESC_COUNT, BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
    831 	}
    832 
    833 	sc->rx.cur = index;
    834 
    835 	return npkt;
    836 }
    837 
    838 static void
    839 sunxi_emac_txintr(struct sunxi_emac_softc *sc)
    840 {
    841 	struct ifnet *ifp = &sc->ec.ec_if;
    842 	struct sunxi_emac_bufmap *bmap;
    843 	struct sunxi_emac_desc *desc;
    844 	uint32_t status;
    845 	int i;
    846 
    847 	EMAC_ASSERT_LOCKED(sc);
    848 
    849 	for (i = sc->tx.next; sc->tx.queued > 0; i = TX_NEXT(i)) {
    850 		KASSERT(sc->tx.queued > 0 && sc->tx.queued <= TX_DESC_COUNT);
    851 		sunxi_emac_dma_sync(sc, sc->tx.desc_tag, sc->tx.desc_map,
    852 		    i, i + 1, TX_DESC_COUNT,
    853 		    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
    854 		desc = &sc->tx.desc_ring[i];
    855 		status = le32toh(desc->status);
    856 		if ((status & TX_DESC_CTL) != 0)
    857 			break;
    858 		bmap = &sc->tx.buf_map[i];
    859 		if (bmap->mbuf != NULL) {
    860 			bus_dmamap_sync(sc->tx.buf_tag, bmap->map,
    861 			    0, bmap->map->dm_mapsize,
    862 			    BUS_DMASYNC_POSTWRITE);
    863 			bus_dmamap_unload(sc->tx.buf_tag, bmap->map);
    864 			m_freem(bmap->mbuf);
    865 			bmap->mbuf = NULL;
    866 		}
    867 
    868 		sunxi_emac_setup_txdesc(sc, i, 0, 0, 0);
    869 		sunxi_emac_dma_sync(sc, sc->tx.desc_tag, sc->tx.desc_map,
    870 		    i, i + 1, TX_DESC_COUNT,
    871 		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
    872 
    873 		ifp->if_flags &= ~IFF_OACTIVE;
    874 		ifp->if_opackets++;
    875 	}
    876 
    877 	sc->tx.next = i;
    878 }
    879 
    880 static int
    881 sunxi_emac_intr(void *arg)
    882 {
    883 	struct sunxi_emac_softc *sc = arg;
    884 	struct ifnet *ifp = &sc->ec.ec_if;
    885 	uint32_t val;
    886 
    887 	EMAC_LOCK(sc);
    888 
    889 	val = RD4(sc, EMAC_INT_STA);
    890 	WR4(sc, EMAC_INT_STA, val);
    891 
    892 	if (val & RX_INT)
    893 		sunxi_emac_rxintr(sc);
    894 
    895 	if (val & (TX_INT|TX_BUF_UA_INT)) {
    896 		sunxi_emac_txintr(sc);
    897 		if_schedule_deferred_start(ifp);
    898 	}
    899 
    900 	EMAC_UNLOCK(sc);
    901 
    902 	return 1;
    903 }
    904 
    905 static int
    906 sunxi_emac_ioctl(struct ifnet *ifp, u_long cmd, void *data)
    907 {
    908 	struct sunxi_emac_softc *sc = ifp->if_softc;
    909 	struct mii_data *mii = &sc->mii;
    910 	struct ifreq *ifr = data;
    911 	int error, s;
    912 
    913 #ifndef EMAC_MPSAFE
    914 	s = splnet();
    915 #endif
    916 
    917 	switch (cmd) {
    918 	case SIOCSIFMEDIA:
    919 	case SIOCGIFMEDIA:
    920 #ifdef EMAC_MPSAFE
    921 		s = splnet();
    922 #endif
    923 		error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, cmd);
    924 #ifdef EMAC_MPSAFE
    925 		splx(s);
    926 #endif
    927 		break;
    928 	default:
    929 #ifdef EMAC_MPSAFE
    930 		s = splnet();
    931 #endif
    932 		error = ether_ioctl(ifp, cmd, data);
    933 #ifdef EMAC_MPSAFE
    934 		splx(s);
    935 #endif
    936 		if (error != ENETRESET)
    937 			break;
    938 
    939 		error = 0;
    940 
    941 		if (cmd == SIOCSIFCAP)
    942 			error = (*ifp->if_init)(ifp);
    943 		else if (cmd != SIOCADDMULTI && cmd != SIOCDELMULTI)
    944 			;
    945 		else if ((ifp->if_flags & IFF_RUNNING) != 0) {
    946 			EMAC_LOCK(sc);
    947 			sunxi_emac_setup_rxfilter(sc);
    948 			EMAC_UNLOCK(sc);
    949 		}
    950 		break;
    951 	}
    952 
    953 #ifndef EMAC_MPSAFE
    954 	splx(s);
    955 #endif
    956 
    957 	return error;
    958 }
    959 
    960 static bool
    961 sunxi_emac_has_internal_phy(struct sunxi_emac_softc *sc)
    962 {
    963 	const char * mdio_internal_compat[] = {
    964 		"allwinner,sun8i-h3-mdio-internal",
    965 		NULL
    966 	};
    967 	int phy;
    968 
    969 	/* Non-standard property, for compatible with old dts files */
    970 	if (of_hasprop(sc->phandle, "allwinner,use-internal-phy"))
    971 		return true;
    972 
    973 	phy = fdtbus_get_phandle(sc->phandle, "phy-handle");
    974 	if (phy == -1)
    975 		return false;
    976 
    977 	/* For internal PHY, check compatible string of parent node */
    978 	return of_compatible(OF_parent(phy), mdio_internal_compat) >= 0;
    979 }
    980 
    981 static int
    982 sunxi_emac_setup_phy(struct sunxi_emac_softc *sc)
    983 {
    984 	uint32_t reg, tx_delay, rx_delay;
    985 	const char *phy_type;
    986 
    987 	phy_type = fdtbus_get_string(sc->phandle, "phy-mode");
    988 	if (phy_type == NULL)
    989 		return 0;
    990 
    991 	aprint_debug_dev(sc->dev, "PHY type: %s\n", phy_type);
    992 
    993 	syscon_lock(sc->syscon);
    994 	reg = syscon_read_4(sc->syscon, EMAC_CLK_REG);
    995 
    996 	reg &= ~(EMAC_CLK_PIT | EMAC_CLK_SRC | EMAC_CLK_RMII_EN);
    997 	if (strcmp(phy_type, "rgmii") == 0)
    998 		reg |= EMAC_CLK_PIT_RGMII | EMAC_CLK_SRC_RGMII;
    999 	else if (strcmp(phy_type, "rmii") == 0)
   1000 		reg |= EMAC_CLK_RMII_EN;
   1001 	else
   1002 		reg |= EMAC_CLK_PIT_MII | EMAC_CLK_SRC_MII;
   1003 
   1004 	if (of_getprop_uint32(sc->phandle, "allwinner,tx-delay-ps",
   1005 	    &tx_delay) == 0) {
   1006 		reg &= ~EMAC_CLK_ETXDC;
   1007 		reg |= ((tx_delay / 100) << EMAC_CLK_ETXDC_SHIFT);
   1008 	} else if (of_getprop_uint32(sc->phandle, "tx-delay", &tx_delay) == 0) {
   1009 		reg &= ~EMAC_CLK_ETXDC;
   1010 		reg |= (tx_delay << EMAC_CLK_ETXDC_SHIFT);
   1011 	}
   1012 	if (of_getprop_uint32(sc->phandle, "allwinner,rx-delay-ps",
   1013 	    &rx_delay) == 0) {
   1014 		reg &= ~EMAC_CLK_ERXDC;
   1015 		reg |= ((rx_delay / 100) << EMAC_CLK_ERXDC_SHIFT);
   1016 	} else if (of_getprop_uint32(sc->phandle, "rx-delay", &rx_delay) == 0) {
   1017 		reg &= ~EMAC_CLK_ERXDC;
   1018 		reg |= (rx_delay << EMAC_CLK_ERXDC_SHIFT);
   1019 	}
   1020 
   1021 	if (sc->type == EMAC_H3 || sc->type == EMAC_H6) {
   1022 		if (sunxi_emac_has_internal_phy(sc)) {
   1023 			reg |= EMAC_CLK_EPHY_SELECT;
   1024 			reg &= ~EMAC_CLK_EPHY_SHUTDOWN;
   1025 			if (of_hasprop(sc->phandle,
   1026 			    "allwinner,leds-active-low"))
   1027 				reg |= EMAC_CLK_EPHY_LED_POL;
   1028 			else
   1029 				reg &= ~EMAC_CLK_EPHY_LED_POL;
   1030 
   1031 			/* Set internal PHY addr to 1 */
   1032 			reg &= ~EMAC_CLK_EPHY_ADDR;
   1033 			reg |= (1 << EMAC_CLK_EPHY_ADDR_SHIFT);
   1034 		} else {
   1035 			reg &= ~EMAC_CLK_EPHY_SELECT;
   1036 		}
   1037 	}
   1038 
   1039 	aprint_debug_dev(sc->dev, "EMAC clock: 0x%08x\n", reg);
   1040 
   1041 	syscon_write_4(sc->syscon, EMAC_CLK_REG, reg);
   1042 	syscon_unlock(sc->syscon);
   1043 
   1044 	return 0;
   1045 }
   1046 
   1047 static int
   1048 sunxi_emac_setup_resources(struct sunxi_emac_softc *sc)
   1049 {
   1050 	u_int freq;
   1051 	int error, div;
   1052 
   1053 	/* Configure PHY for MII or RGMII mode */
   1054 	if (sunxi_emac_setup_phy(sc) != 0)
   1055 		return ENXIO;
   1056 
   1057 	/* Enable clocks */
   1058 	error = clk_enable(sc->clk_ahb);
   1059 	if (error != 0) {
   1060 		aprint_error_dev(sc->dev, "cannot enable ahb clock\n");
   1061 		return error;
   1062 	}
   1063 
   1064 	if (sc->clk_ephy != NULL) {
   1065 		error = clk_enable(sc->clk_ephy);
   1066 		if (error != 0) {
   1067 			aprint_error_dev(sc->dev, "cannot enable ephy clock\n");
   1068 			return error;
   1069 		}
   1070 	}
   1071 
   1072 	/* De-assert reset */
   1073 	error = fdtbus_reset_deassert(sc->rst_ahb);
   1074 	if (error != 0) {
   1075 		aprint_error_dev(sc->dev, "cannot de-assert ahb reset\n");
   1076 		return error;
   1077 	}
   1078 	if (sc->rst_ephy != NULL) {
   1079 		error = fdtbus_reset_deassert(sc->rst_ephy);
   1080 		if (error != 0) {
   1081 			aprint_error_dev(sc->dev,
   1082 			    "cannot de-assert ephy reset\n");
   1083 			return error;
   1084 		}
   1085 	}
   1086 
   1087 	/* Enable PHY regulator if applicable */
   1088 	if (sc->reg_phy != NULL) {
   1089 		error = fdtbus_regulator_enable(sc->reg_phy);
   1090 		if (error != 0) {
   1091 			aprint_error_dev(sc->dev,
   1092 			    "cannot enable PHY regulator\n");
   1093 			return error;
   1094 		}
   1095 	}
   1096 
   1097 	/* Determine MDC clock divide ratio based on AHB clock */
   1098 	freq = clk_get_rate(sc->clk_ahb);
   1099 	if (freq == 0) {
   1100 		aprint_error_dev(sc->dev, "cannot get AHB clock frequency\n");
   1101 		return ENXIO;
   1102 	}
   1103 	div = freq / MDIO_FREQ;
   1104 	if (div <= 16)
   1105 		sc->mdc_div_ratio_m = MDC_DIV_RATIO_M_16;
   1106 	else if (div <= 32)
   1107 		sc->mdc_div_ratio_m = MDC_DIV_RATIO_M_32;
   1108 	else if (div <= 64)
   1109 		sc->mdc_div_ratio_m = MDC_DIV_RATIO_M_64;
   1110 	else if (div <= 128)
   1111 		sc->mdc_div_ratio_m = MDC_DIV_RATIO_M_128;
   1112 	else {
   1113 		aprint_error_dev(sc->dev,
   1114 		    "cannot determine MDC clock divide ratio\n");
   1115 		return ENXIO;
   1116 	}
   1117 
   1118 	aprint_debug_dev(sc->dev, "AHB frequency %u Hz, MDC div: 0x%x\n",
   1119 	    freq, sc->mdc_div_ratio_m);
   1120 
   1121 	return 0;
   1122 }
   1123 
   1124 static void
   1125 sunxi_emac_get_eaddr(struct sunxi_emac_softc *sc, uint8_t *eaddr)
   1126 {
   1127 	uint32_t maclo, machi;
   1128 #if notyet
   1129 	u_char rootkey[16];
   1130 #endif
   1131 
   1132 	machi = RD4(sc, EMAC_ADDR_HIGH(0)) & 0xffff;
   1133 	maclo = RD4(sc, EMAC_ADDR_LOW(0));
   1134 
   1135 	if (maclo == 0xffffffff && machi == 0xffff) {
   1136 #if notyet
   1137 		/* MAC address in hardware is invalid, create one */
   1138 		if (aw_sid_get_rootkey(rootkey) == 0 &&
   1139 		    (rootkey[3] | rootkey[12] | rootkey[13] | rootkey[14] |
   1140 		     rootkey[15]) != 0) {
   1141 			/* MAC address is derived from the root key in SID */
   1142 			maclo = (rootkey[13] << 24) | (rootkey[12] << 16) |
   1143 				(rootkey[3] << 8) | 0x02;
   1144 			machi = (rootkey[15] << 8) | rootkey[14];
   1145 		} else {
   1146 #endif
   1147 			/* Create one */
   1148 			maclo = 0x00f2 | (cprng_strong32() & 0xffff0000);
   1149 			machi = cprng_strong32() & 0xffff;
   1150 #if notyet
   1151 		}
   1152 #endif
   1153 	}
   1154 
   1155 	eaddr[0] = maclo & 0xff;
   1156 	eaddr[1] = (maclo >> 8) & 0xff;
   1157 	eaddr[2] = (maclo >> 16) & 0xff;
   1158 	eaddr[3] = (maclo >> 24) & 0xff;
   1159 	eaddr[4] = machi & 0xff;
   1160 	eaddr[5] = (machi >> 8) & 0xff;
   1161 }
   1162 
   1163 static int
   1164 sunxi_emac_phy_reset(struct sunxi_emac_softc *sc)
   1165 {
   1166 	uint32_t delay_prop[3];
   1167 	int pin_value;
   1168 
   1169 	if (sc->pin_reset == NULL)
   1170 		return 0;
   1171 
   1172 	if (OF_getprop(sc->phandle, "allwinner,reset-delays-us", delay_prop,
   1173 	    sizeof(delay_prop)) <= 0)
   1174 		return ENXIO;
   1175 
   1176 	pin_value = of_hasprop(sc->phandle, "allwinner,reset-active-low");
   1177 
   1178 	fdtbus_gpio_write(sc->pin_reset, pin_value);
   1179 	delay(htole32(delay_prop[0]));
   1180 	fdtbus_gpio_write(sc->pin_reset, !pin_value);
   1181 	delay(htole32(delay_prop[1]));
   1182 	fdtbus_gpio_write(sc->pin_reset, pin_value);
   1183 	delay(htole32(delay_prop[2]));
   1184 
   1185 	return 0;
   1186 }
   1187 
   1188 static int
   1189 sunxi_emac_setup_dma(struct sunxi_emac_softc *sc)
   1190 {
   1191 	struct mbuf *m;
   1192 	int error, nsegs, i;
   1193 
   1194 	/* Setup TX ring */
   1195 	sc->tx.buf_tag = sc->tx.desc_tag = sc->dmat;
   1196 	error = bus_dmamap_create(sc->dmat, TX_DESC_SIZE, 1, TX_DESC_SIZE, 0,
   1197 	    BUS_DMA_WAITOK, &sc->tx.desc_map);
   1198 	if (error)
   1199 		return error;
   1200 	error = bus_dmamem_alloc(sc->dmat, TX_DESC_SIZE, DESC_ALIGN, 0,
   1201 	    &sc->tx.desc_dmaseg, 1, &nsegs, BUS_DMA_WAITOK);
   1202 	if (error)
   1203 		return error;
   1204 	error = bus_dmamem_map(sc->dmat, &sc->tx.desc_dmaseg, nsegs,
   1205 	    TX_DESC_SIZE, (void *)&sc->tx.desc_ring,
   1206 	    BUS_DMA_WAITOK);
   1207 	if (error)
   1208 		return error;
   1209 	error = bus_dmamap_load(sc->dmat, sc->tx.desc_map, sc->tx.desc_ring,
   1210 	    TX_DESC_SIZE, NULL, BUS_DMA_WAITOK);
   1211 	if (error)
   1212 		return error;
   1213 	sc->tx.desc_ring_paddr = sc->tx.desc_map->dm_segs[0].ds_addr;
   1214 
   1215 	memset(sc->tx.desc_ring, 0, TX_DESC_SIZE);
   1216 	bus_dmamap_sync(sc->dmat, sc->tx.desc_map, 0, TX_DESC_SIZE,
   1217 	    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   1218 
   1219 	for (i = 0; i < TX_DESC_COUNT; i++)
   1220 		sc->tx.desc_ring[i].next =
   1221 		    htole32(sc->tx.desc_ring_paddr + DESC_OFF(TX_NEXT(i)));
   1222 
   1223 	sc->tx.queued = TX_DESC_COUNT;
   1224 	for (i = 0; i < TX_DESC_COUNT; i++) {
   1225 		error = bus_dmamap_create(sc->tx.buf_tag, MCLBYTES,
   1226 		    TX_MAX_SEGS, MCLBYTES, 0, BUS_DMA_WAITOK,
   1227 		    &sc->tx.buf_map[i].map);
   1228 		if (error != 0) {
   1229 			device_printf(sc->dev, "cannot create TX buffer map\n");
   1230 			return error;
   1231 		}
   1232 		sunxi_emac_setup_txdesc(sc, i, 0, 0, 0);
   1233 	}
   1234 
   1235 	/* Setup RX ring */
   1236 	sc->rx.buf_tag = sc->rx.desc_tag = sc->dmat;
   1237 	error = bus_dmamap_create(sc->dmat, RX_DESC_SIZE, 1, RX_DESC_SIZE, 0,
   1238 	    BUS_DMA_WAITOK, &sc->rx.desc_map);
   1239 	if (error)
   1240 		return error;
   1241 	error = bus_dmamem_alloc(sc->dmat, RX_DESC_SIZE, DESC_ALIGN, 0,
   1242 	    &sc->rx.desc_dmaseg, 1, &nsegs, BUS_DMA_WAITOK);
   1243 	if (error)
   1244 		return error;
   1245 	error = bus_dmamem_map(sc->dmat, &sc->rx.desc_dmaseg, nsegs,
   1246 	    RX_DESC_SIZE, (void *)&sc->rx.desc_ring,
   1247 	    BUS_DMA_WAITOK);
   1248 	if (error)
   1249 		return error;
   1250 	error = bus_dmamap_load(sc->dmat, sc->rx.desc_map, sc->rx.desc_ring,
   1251 	    RX_DESC_SIZE, NULL, BUS_DMA_WAITOK);
   1252 	if (error)
   1253 		return error;
   1254 	sc->rx.desc_ring_paddr = sc->rx.desc_map->dm_segs[0].ds_addr;
   1255 
   1256 	memset(sc->rx.desc_ring, 0, RX_DESC_SIZE);
   1257 
   1258 	for (i = 0; i < RX_DESC_COUNT; i++) {
   1259 		error = bus_dmamap_create(sc->rx.buf_tag, MCLBYTES,
   1260 		    RX_DESC_COUNT, MCLBYTES, 0, BUS_DMA_WAITOK,
   1261 		    &sc->rx.buf_map[i].map);
   1262 		if (error != 0) {
   1263 			device_printf(sc->dev, "cannot create RX buffer map\n");
   1264 			return error;
   1265 		}
   1266 		if ((m = sunxi_emac_alloc_mbufcl(sc)) == NULL) {
   1267 			device_printf(sc->dev, "cannot allocate RX mbuf\n");
   1268 			return ENOMEM;
   1269 		}
   1270 		error = sunxi_emac_setup_rxbuf(sc, i, m);
   1271 		if (error != 0) {
   1272 			device_printf(sc->dev, "cannot create RX buffer\n");
   1273 			return error;
   1274 		}
   1275 	}
   1276 	bus_dmamap_sync(sc->rx.desc_tag, sc->rx.desc_map,
   1277 	    0, sc->rx.desc_map->dm_mapsize,
   1278 	    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   1279 
   1280 	return 0;
   1281 }
   1282 
   1283 static int
   1284 sunxi_emac_get_resources(struct sunxi_emac_softc *sc)
   1285 {
   1286 	const int phandle = sc->phandle;
   1287 	bus_addr_t addr, size;
   1288 
   1289 	/* Map EMAC registers */
   1290 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0)
   1291 		return ENXIO;
   1292 	if (bus_space_map(sc->bst, addr, size, 0, &sc->bsh) != 0)
   1293 		return ENXIO;
   1294 
   1295 	/* Get SYSCON registers */
   1296 	sc->syscon = fdtbus_syscon_acquire(phandle, "syscon");
   1297 	if (sc->syscon == NULL)
   1298 		return ENXIO;
   1299 
   1300 	/* The "ahb"/"stmmaceth" clock and reset is required */
   1301 	if ((sc->clk_ahb = fdtbus_clock_get(phandle, "ahb")) == NULL &&
   1302 	    (sc->clk_ahb = fdtbus_clock_get(phandle, "stmmaceth")) == NULL)
   1303 		return ENXIO;
   1304 	if ((sc->rst_ahb = fdtbus_reset_get(phandle, "ahb")) == NULL &&
   1305 	    (sc->rst_ahb = fdtbus_reset_get(phandle, "stmmaceth")) == NULL)
   1306 		return ENXIO;
   1307 
   1308 	/* Internal PHY clock and reset are optional properties. */
   1309 	sc->clk_ephy = fdtbus_clock_get(phandle, "ephy");
   1310 	if (sc->clk_ephy == NULL) {
   1311 		int phy_phandle = fdtbus_get_phandle(phandle, "phy-handle");
   1312 		if (phy_phandle != -1)
   1313 			sc->clk_ephy = fdtbus_clock_get_index(phy_phandle, 0);
   1314 	}
   1315 	sc->rst_ephy = fdtbus_reset_get(phandle, "ephy");
   1316 	if (sc->rst_ephy == NULL) {
   1317 		int phy_phandle = fdtbus_get_phandle(phandle, "phy-handle");
   1318 		if (phy_phandle != -1)
   1319 			sc->rst_ephy = fdtbus_reset_get_index(phy_phandle, 0);
   1320 	}
   1321 
   1322 	/* Regulator is optional */
   1323 	sc->reg_phy = fdtbus_regulator_acquire(phandle, "phy-supply");
   1324 
   1325 	/* Reset GPIO is optional */
   1326 	sc->pin_reset = fdtbus_gpio_acquire(sc->phandle,
   1327 	    "allwinner,reset-gpio", GPIO_PIN_OUTPUT);
   1328 
   1329 	return 0;
   1330 }
   1331 
   1332 static int
   1333 sunxi_emac_get_phyid(struct sunxi_emac_softc *sc)
   1334 {
   1335 	bus_addr_t addr;
   1336 	int phy_phandle;
   1337 
   1338 	phy_phandle = fdtbus_get_phandle(sc->phandle, "phy");
   1339 	if (phy_phandle == -1)
   1340 		phy_phandle = fdtbus_get_phandle(sc->phandle, "phy-handle");
   1341 	if (phy_phandle == -1)
   1342 		return MII_PHY_ANY;
   1343 
   1344 	if (fdtbus_get_reg(phy_phandle, 0, &addr, NULL) != 0)
   1345 		return MII_PHY_ANY;
   1346 
   1347 	return (int)addr;
   1348 }
   1349 
   1350 static int
   1351 sunxi_emac_match(device_t parent, cfdata_t cf, void *aux)
   1352 {
   1353 	struct fdt_attach_args * const faa = aux;
   1354 
   1355 	return of_match_compat_data(faa->faa_phandle, compat_data);
   1356 }
   1357 
   1358 static void
   1359 sunxi_emac_attach(device_t parent, device_t self, void *aux)
   1360 {
   1361 	struct fdt_attach_args * const faa = aux;
   1362 	struct sunxi_emac_softc * const sc = device_private(self);
   1363 	const int phandle = faa->faa_phandle;
   1364 	struct mii_data *mii = &sc->mii;
   1365 	struct ifnet *ifp = &sc->ec.ec_if;
   1366 	uint8_t eaddr[ETHER_ADDR_LEN];
   1367 	char intrstr[128];
   1368 
   1369 	sc->dev = self;
   1370 	sc->phandle = phandle;
   1371 	sc->bst = faa->faa_bst;
   1372 	sc->dmat = faa->faa_dmat;
   1373 	sc->type = of_search_compatible(phandle, compat_data)->data;
   1374 	sc->phy_id = sunxi_emac_get_phyid(sc);
   1375 
   1376 	if (sunxi_emac_get_resources(sc) != 0) {
   1377 		aprint_error(": cannot allocate resources for device\n");
   1378 		return;
   1379 	}
   1380 	if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
   1381 		aprint_error(": cannot decode interrupt\n");
   1382 		return;
   1383 	}
   1384 
   1385 	mutex_init(&sc->mtx, MUTEX_DEFAULT, IPL_NET);
   1386 	callout_init(&sc->stat_ch, CALLOUT_FLAGS);
   1387 	callout_setfunc(&sc->stat_ch, sunxi_emac_tick, sc);
   1388 
   1389 	aprint_naive("\n");
   1390 	aprint_normal(": EMAC\n");
   1391 
   1392 	/* Setup clocks and regulators */
   1393 	if (sunxi_emac_setup_resources(sc) != 0)
   1394 		return;
   1395 
   1396 	/* Read MAC address before resetting the chip */
   1397 	sunxi_emac_get_eaddr(sc, eaddr);
   1398 	aprint_normal_dev(self, "Ethernet address %s\n", ether_sprintf(eaddr));
   1399 
   1400 	/* Reset PHY if necessary */
   1401 	if (sunxi_emac_phy_reset(sc) != 0) {
   1402 		aprint_error_dev(self, "failed to reset PHY\n");
   1403 		return;
   1404 	}
   1405 
   1406 	/* Setup DMA descriptors */
   1407 	if (sunxi_emac_setup_dma(sc) != 0) {
   1408 		aprint_error_dev(self, "failed to setup DMA descriptors\n");
   1409 		return;
   1410 	}
   1411 
   1412 	/* Install interrupt handler */
   1413 	sc->ih = fdtbus_intr_establish(phandle, 0, IPL_NET,
   1414 	    FDT_INTR_FLAGS, sunxi_emac_intr, sc);
   1415 	if (sc->ih == NULL) {
   1416 		aprint_error_dev(self, "failed to establish interrupt on %s\n",
   1417 		    intrstr);
   1418 		return;
   1419 	}
   1420 	aprint_normal_dev(self, "interrupting on %s\n", intrstr);
   1421 
   1422 	/* Setup ethernet interface */
   1423 	ifp->if_softc = sc;
   1424 	snprintf(ifp->if_xname, IFNAMSIZ, EMAC_IFNAME, device_unit(self));
   1425 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
   1426 #ifdef EMAC_MPSAFE
   1427 	ifp->if_extflags = IFEF_MPSAFE;
   1428 #endif
   1429 	ifp->if_start = sunxi_emac_start;
   1430 	ifp->if_ioctl = sunxi_emac_ioctl;
   1431 	ifp->if_init = sunxi_emac_init;
   1432 	ifp->if_stop = sunxi_emac_stop;
   1433 	ifp->if_capabilities = IFCAP_CSUM_IPv4_Rx |
   1434 			       IFCAP_CSUM_IPv4_Tx |
   1435 			       IFCAP_CSUM_TCPv4_Rx |
   1436 			       IFCAP_CSUM_TCPv4_Tx |
   1437 			       IFCAP_CSUM_UDPv4_Rx |
   1438 			       IFCAP_CSUM_UDPv4_Tx;
   1439 	IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
   1440 	IFQ_SET_READY(&ifp->if_snd);
   1441 
   1442 	/* 802.1Q VLAN-sized frames are supported */
   1443 	sc->ec.ec_capabilities |= ETHERCAP_VLAN_MTU;
   1444 
   1445 	/* Attach MII driver */
   1446 	sc->ec.ec_mii = mii;
   1447 	ifmedia_init(&mii->mii_media, 0, ether_mediachange, ether_mediastatus);
   1448 	mii->mii_ifp = ifp;
   1449 	mii->mii_readreg = sunxi_emac_mii_readreg;
   1450 	mii->mii_writereg = sunxi_emac_mii_writereg;
   1451 	mii->mii_statchg = sunxi_emac_mii_statchg;
   1452 	mii_attach(self, mii, 0xffffffff, sc->phy_id, MII_OFFSET_ANY,
   1453 	    MIIF_DOPAUSE);
   1454 
   1455 	if (LIST_EMPTY(&mii->mii_phys)) {
   1456 		aprint_error_dev(self, "no PHY found!\n");
   1457 		return;
   1458 	}
   1459 	ifmedia_set(&mii->mii_media, IFM_ETHER|IFM_AUTO);
   1460 
   1461 	/* Attach interface */
   1462 	if_attach(ifp);
   1463 	if_deferred_start_init(ifp, NULL);
   1464 
   1465 	/* Attach ethernet interface */
   1466 	ether_ifattach(ifp, eaddr);
   1467 }
   1468 
   1469 CFATTACH_DECL_NEW(sunxi_emac, sizeof(struct sunxi_emac_softc),
   1470     sunxi_emac_match, sunxi_emac_attach, NULL, NULL);
   1471