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