Home | History | Annotate | Line # | Download | only in sunxi
sunxi_emac.c revision 1.25
      1 /* $NetBSD: sunxi_emac.c,v 1.25 2019/04/22 14:53:51 maya 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.25 2019/04/22 14:53:51 maya 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_FIRST_MULTI(step, &sc->ec, enm);
    547 		while (enm != NULL) {
    548 			crc = ether_crc32_le(enm->enm_addrlo, ETHER_ADDR_LEN);
    549 			crc &= 0x7f;
    550 			crc = bitrev32(~crc) >> 26;
    551 			hashreg = (crc >> 5);
    552 			hashbit = (crc & 0x1f);
    553 			hash[hashreg] |= (1 << hashbit);
    554 			ETHER_NEXT_MULTI(step, enm);
    555 		}
    556 	}
    557 
    558 	/* Write our unicast address */
    559 	eaddr = CLLADDR(ifp->if_sadl);
    560 	machi = (eaddr[5] << 8) | eaddr[4];
    561 	maclo = (eaddr[3] << 24) | (eaddr[2] << 16) | (eaddr[1] << 8) |
    562 	   (eaddr[0] << 0);
    563 	WR4(sc, EMAC_ADDR_HIGH(0), machi);
    564 	WR4(sc, EMAC_ADDR_LOW(0), maclo);
    565 
    566 	/* Multicast hash filters */
    567 	WR4(sc, EMAC_RX_HASH_0, hash[1]);
    568 	WR4(sc, EMAC_RX_HASH_1, hash[0]);
    569 
    570 	/* RX frame filter config */
    571 	WR4(sc, EMAC_RX_FRM_FLT, val);
    572 }
    573 
    574 static void
    575 sunxi_emac_enable_intr(struct sunxi_emac_softc *sc)
    576 {
    577 	/* Enable interrupts */
    578 	WR4(sc, EMAC_INT_EN, RX_INT_EN | TX_INT_EN | TX_BUF_UA_INT_EN);
    579 }
    580 
    581 static void
    582 sunxi_emac_disable_intr(struct sunxi_emac_softc *sc)
    583 {
    584 	/* Disable interrupts */
    585 	WR4(sc, EMAC_INT_EN, 0);
    586 }
    587 
    588 #ifdef SUNXI_EMAC_DEBUG
    589 static void
    590 sunxi_emac_dump_regs(struct sunxi_emac_softc *sc)
    591 {
    592 	static const struct {
    593 		const char *name;
    594 		u_int reg;
    595 	} regs[] = {
    596 		{ "BASIC_CTL_0", EMAC_BASIC_CTL_0 },
    597 		{ "BASIC_CTL_1", EMAC_BASIC_CTL_1 },
    598 		{ "INT_STA", EMAC_INT_STA },
    599 		{ "INT_EN", EMAC_INT_EN },
    600 		{ "TX_CTL_0", EMAC_TX_CTL_0 },
    601 		{ "TX_CTL_1", EMAC_TX_CTL_1 },
    602 		{ "TX_FLOW_CTL", EMAC_TX_FLOW_CTL },
    603 		{ "TX_DMA_LIST", EMAC_TX_DMA_LIST },
    604 		{ "RX_CTL_0", EMAC_RX_CTL_0 },
    605 		{ "RX_CTL_1", EMAC_RX_CTL_1 },
    606 		{ "RX_DMA_LIST", EMAC_RX_DMA_LIST },
    607 		{ "RX_FRM_FLT", EMAC_RX_FRM_FLT },
    608 		{ "RX_HASH_0", EMAC_RX_HASH_0 },
    609 		{ "RX_HASH_1", EMAC_RX_HASH_1 },
    610 		{ "MII_CMD", EMAC_MII_CMD },
    611 		{ "ADDR_HIGH0", EMAC_ADDR_HIGH(0) },
    612 		{ "ADDR_LOW0", EMAC_ADDR_LOW(0) },
    613 		{ "TX_DMA_STA", EMAC_TX_DMA_STA },
    614 		{ "TX_DMA_CUR_DESC", EMAC_TX_DMA_CUR_DESC },
    615 		{ "TX_DMA_CUR_BUF", EMAC_TX_DMA_CUR_BUF },
    616 		{ "RX_DMA_STA", EMAC_RX_DMA_STA },
    617 		{ "RX_DMA_CUR_DESC", EMAC_RX_DMA_CUR_DESC },
    618 		{ "RX_DMA_CUR_BUF", EMAC_RX_DMA_CUR_BUF },
    619 		{ "RGMII_STA", EMAC_RGMII_STA },
    620 	};
    621 	u_int n;
    622 
    623 	for (n = 0; n < __arraycount(regs); n++)
    624 		device_printf(sc->dev, "  %-20s %08x\n", regs[n].name,
    625 		    RD4(sc, regs[n].reg));
    626 }
    627 #endif
    628 
    629 static int
    630 sunxi_emac_reset(struct sunxi_emac_softc *sc)
    631 {
    632 	int retry;
    633 
    634 	/* Soft reset all registers and logic */
    635 	WR4(sc, EMAC_BASIC_CTL_1, BASIC_CTL_SOFT_RST);
    636 
    637 	/* Wait for soft reset bit to self-clear */
    638 	for (retry = SOFT_RST_RETRY; retry > 0; retry--) {
    639 		if ((RD4(sc, EMAC_BASIC_CTL_1) & BASIC_CTL_SOFT_RST) == 0)
    640 			break;
    641 		delay(10);
    642 	}
    643 	if (retry == 0) {
    644 		aprint_debug_dev(sc->dev, "soft reset timed out\n");
    645 #ifdef SUNXI_EMAC_DEBUG
    646 		sunxi_emac_dump_regs(sc);
    647 #endif
    648 		return ETIMEDOUT;
    649 	}
    650 
    651 	return 0;
    652 }
    653 
    654 static int
    655 sunxi_emac_init_locked(struct sunxi_emac_softc *sc)
    656 {
    657 	struct ifnet *ifp = &sc->ec.ec_if;
    658 	struct mii_data *mii = &sc->mii;
    659 	uint32_t val;
    660 
    661 	EMAC_ASSERT_LOCKED(sc);
    662 
    663 	if ((ifp->if_flags & IFF_RUNNING) != 0)
    664 		return 0;
    665 
    666 	/* Soft reset EMAC core */
    667 	sunxi_emac_reset(sc);
    668 
    669 	/* Write transmit and receive descriptor base address registers */
    670 	WR4(sc, EMAC_TX_DMA_LIST, sc->tx.desc_ring_paddr);
    671 	WR4(sc, EMAC_RX_DMA_LIST, sc->rx.desc_ring_paddr);
    672 
    673 	sunxi_emac_setup_rxfilter(sc);
    674 
    675 	/* Configure DMA burst length and priorities */
    676 	val = sunxi_emac_burst_len << BASIC_CTL_BURST_LEN_SHIFT;
    677 	if (sunxi_emac_rx_tx_pri)
    678 		val |= BASIC_CTL_RX_TX_PRI;
    679 	WR4(sc, EMAC_BASIC_CTL_1, val);
    680 
    681 	/* Enable interrupts */
    682 	sunxi_emac_enable_intr(sc);
    683 
    684 	/* Enable transmit DMA */
    685 	val = RD4(sc, EMAC_TX_CTL_1);
    686 	WR4(sc, EMAC_TX_CTL_1, val | TX_DMA_EN | TX_MD | TX_NEXT_FRAME);
    687 
    688 	/* Enable receive DMA */
    689 	val = RD4(sc, EMAC_RX_CTL_1);
    690 	WR4(sc, EMAC_RX_CTL_1, val | RX_DMA_EN | RX_MD);
    691 
    692 	/* Enable transmitter */
    693 	val = RD4(sc, EMAC_TX_CTL_0);
    694 	WR4(sc, EMAC_TX_CTL_0, val | TX_EN);
    695 
    696 	/* Enable receiver */
    697 	val = RD4(sc, EMAC_RX_CTL_0);
    698 	WR4(sc, EMAC_RX_CTL_0, val | RX_EN | CHECK_CRC);
    699 
    700 	ifp->if_flags |= IFF_RUNNING;
    701 	ifp->if_flags &= ~IFF_OACTIVE;
    702 
    703 	mii_mediachg(mii);
    704 	callout_schedule(&sc->stat_ch, hz);
    705 
    706 	return 0;
    707 }
    708 
    709 static int
    710 sunxi_emac_init(struct ifnet *ifp)
    711 {
    712 	struct sunxi_emac_softc *sc = ifp->if_softc;
    713 	int error;
    714 
    715 	EMAC_LOCK(sc);
    716 	error = sunxi_emac_init_locked(sc);
    717 	EMAC_UNLOCK(sc);
    718 
    719 	return error;
    720 }
    721 
    722 static void
    723 sunxi_emac_stop_locked(struct sunxi_emac_softc *sc, int disable)
    724 {
    725 	struct ifnet *ifp = &sc->ec.ec_if;
    726 	uint32_t val;
    727 
    728 	EMAC_ASSERT_LOCKED(sc);
    729 
    730 	callout_stop(&sc->stat_ch);
    731 
    732 	mii_down(&sc->mii);
    733 
    734 	/* Stop transmit DMA and flush data in the TX FIFO */
    735 	val = RD4(sc, EMAC_TX_CTL_1);
    736 	val &= ~TX_DMA_EN;
    737 	val |= FLUSH_TX_FIFO;
    738 	WR4(sc, EMAC_TX_CTL_1, val);
    739 
    740 	/* Disable transmitter */
    741 	val = RD4(sc, EMAC_TX_CTL_0);
    742 	WR4(sc, EMAC_TX_CTL_0, val & ~TX_EN);
    743 
    744 	/* Disable receiver */
    745 	val = RD4(sc, EMAC_RX_CTL_0);
    746 	WR4(sc, EMAC_RX_CTL_0, val & ~RX_EN);
    747 
    748 	/* Disable interrupts */
    749 	sunxi_emac_disable_intr(sc);
    750 
    751 	/* Disable transmit DMA */
    752 	val = RD4(sc, EMAC_TX_CTL_1);
    753 	WR4(sc, EMAC_TX_CTL_1, val & ~TX_DMA_EN);
    754 
    755 	/* Disable receive DMA */
    756 	val = RD4(sc, EMAC_RX_CTL_1);
    757 	WR4(sc, EMAC_RX_CTL_1, val & ~RX_DMA_EN);
    758 
    759 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
    760 }
    761 
    762 static void
    763 sunxi_emac_stop(struct ifnet *ifp, int disable)
    764 {
    765 	struct sunxi_emac_softc * const sc = ifp->if_softc;
    766 
    767 	EMAC_LOCK(sc);
    768 	sunxi_emac_stop_locked(sc, disable);
    769 	EMAC_UNLOCK(sc);
    770 }
    771 
    772 static int
    773 sunxi_emac_rxintr(struct sunxi_emac_softc *sc)
    774 {
    775 	struct ifnet *ifp = &sc->ec.ec_if;
    776 	int error, index, len, npkt;
    777 	struct mbuf *m, *m0;
    778 	uint32_t status;
    779 
    780 	npkt = 0;
    781 
    782 	for (index = sc->rx.cur; ; index = RX_NEXT(index)) {
    783 		sunxi_emac_dma_sync(sc, sc->rx.desc_tag, sc->rx.desc_map,
    784 		    index, index + 1,
    785 		    RX_DESC_COUNT, BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
    786 
    787 		status = le32toh(sc->rx.desc_ring[index].status);
    788 		if ((status & RX_DESC_CTL) != 0)
    789 			break;
    790 
    791 		bus_dmamap_sync(sc->rx.buf_tag, sc->rx.buf_map[index].map,
    792 		    0, sc->rx.buf_map[index].map->dm_mapsize,
    793 		    BUS_DMASYNC_POSTREAD);
    794 		bus_dmamap_unload(sc->rx.buf_tag, sc->rx.buf_map[index].map);
    795 
    796 		len = (status & RX_FRM_LEN) >> RX_FRM_LEN_SHIFT;
    797 		if (len != 0) {
    798 			m = sc->rx.buf_map[index].mbuf;
    799 			m_set_rcvif(m, ifp);
    800 			m->m_flags |= M_HASFCS;
    801 			m->m_pkthdr.len = len;
    802 			m->m_len = len;
    803 			m->m_nextpkt = NULL;
    804 
    805 			if ((ifp->if_capenable & IFCAP_CSUM_IPv4_Rx) != 0 &&
    806 			    (status & RX_FRM_TYPE) != 0) {
    807 				m->m_pkthdr.csum_flags = M_CSUM_IPv4 |
    808 				    M_CSUM_TCPv4 | M_CSUM_UDPv4;
    809 				if ((status & RX_HEADER_ERR) != 0)
    810 					m->m_pkthdr.csum_flags |=
    811 					    M_CSUM_IPv4_BAD;
    812 				if ((status & RX_PAYLOAD_ERR) != 0)
    813 					m->m_pkthdr.csum_flags |=
    814 					    M_CSUM_TCP_UDP_BAD;
    815 			}
    816 
    817 			++npkt;
    818 
    819 			if_percpuq_enqueue(ifp->if_percpuq, m);
    820 		}
    821 
    822 		if ((m0 = sunxi_emac_alloc_mbufcl(sc)) != NULL) {
    823 			error = sunxi_emac_setup_rxbuf(sc, index, m0);
    824 			if (error != 0) {
    825 				/* XXX hole in RX ring */
    826 			}
    827 		} else
    828 			ifp->if_ierrors++;
    829 
    830 		sunxi_emac_dma_sync(sc, sc->rx.desc_tag, sc->rx.desc_map,
    831 		    index, index + 1,
    832 		    RX_DESC_COUNT, BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
    833 	}
    834 
    835 	sc->rx.cur = index;
    836 
    837 	return npkt;
    838 }
    839 
    840 static void
    841 sunxi_emac_txintr(struct sunxi_emac_softc *sc)
    842 {
    843 	struct ifnet *ifp = &sc->ec.ec_if;
    844 	struct sunxi_emac_bufmap *bmap;
    845 	struct sunxi_emac_desc *desc;
    846 	uint32_t status;
    847 	int i;
    848 
    849 	EMAC_ASSERT_LOCKED(sc);
    850 
    851 	for (i = sc->tx.next; sc->tx.queued > 0; i = TX_NEXT(i)) {
    852 		KASSERT(sc->tx.queued > 0 && sc->tx.queued <= TX_DESC_COUNT);
    853 		sunxi_emac_dma_sync(sc, sc->tx.desc_tag, sc->tx.desc_map,
    854 		    i, i + 1, TX_DESC_COUNT,
    855 		    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
    856 		desc = &sc->tx.desc_ring[i];
    857 		status = le32toh(desc->status);
    858 		if ((status & TX_DESC_CTL) != 0)
    859 			break;
    860 		bmap = &sc->tx.buf_map[i];
    861 		if (bmap->mbuf != NULL) {
    862 			bus_dmamap_sync(sc->tx.buf_tag, bmap->map,
    863 			    0, bmap->map->dm_mapsize,
    864 			    BUS_DMASYNC_POSTWRITE);
    865 			bus_dmamap_unload(sc->tx.buf_tag, bmap->map);
    866 			m_freem(bmap->mbuf);
    867 			bmap->mbuf = NULL;
    868 		}
    869 
    870 		sunxi_emac_setup_txdesc(sc, i, 0, 0, 0);
    871 		sunxi_emac_dma_sync(sc, sc->tx.desc_tag, sc->tx.desc_map,
    872 		    i, i + 1, TX_DESC_COUNT,
    873 		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
    874 
    875 		ifp->if_flags &= ~IFF_OACTIVE;
    876 		ifp->if_opackets++;
    877 	}
    878 
    879 	sc->tx.next = i;
    880 }
    881 
    882 static int
    883 sunxi_emac_intr(void *arg)
    884 {
    885 	struct sunxi_emac_softc *sc = arg;
    886 	struct ifnet *ifp = &sc->ec.ec_if;
    887 	uint32_t val;
    888 
    889 	EMAC_LOCK(sc);
    890 
    891 	val = RD4(sc, EMAC_INT_STA);
    892 	WR4(sc, EMAC_INT_STA, val);
    893 
    894 	if (val & RX_INT)
    895 		sunxi_emac_rxintr(sc);
    896 
    897 	if (val & (TX_INT|TX_BUF_UA_INT)) {
    898 		sunxi_emac_txintr(sc);
    899 		if_schedule_deferred_start(ifp);
    900 	}
    901 
    902 	EMAC_UNLOCK(sc);
    903 
    904 	return 1;
    905 }
    906 
    907 static int
    908 sunxi_emac_ioctl(struct ifnet *ifp, u_long cmd, void *data)
    909 {
    910 	struct sunxi_emac_softc *sc = ifp->if_softc;
    911 	int error, s;
    912 
    913 #ifndef EMAC_MPSAFE
    914 	s = splnet();
    915 #endif
    916 
    917 	switch (cmd) {
    918 	default:
    919 #ifdef EMAC_MPSAFE
    920 		s = splnet();
    921 #endif
    922 		error = ether_ioctl(ifp, cmd, data);
    923 #ifdef EMAC_MPSAFE
    924 		splx(s);
    925 #endif
    926 		if (error != ENETRESET)
    927 			break;
    928 
    929 		error = 0;
    930 
    931 		if (cmd == SIOCSIFCAP)
    932 			error = (*ifp->if_init)(ifp);
    933 		else if (cmd != SIOCADDMULTI && cmd != SIOCDELMULTI)
    934 			;
    935 		else if ((ifp->if_flags & IFF_RUNNING) != 0) {
    936 			EMAC_LOCK(sc);
    937 			sunxi_emac_setup_rxfilter(sc);
    938 			EMAC_UNLOCK(sc);
    939 		}
    940 		break;
    941 	}
    942 
    943 #ifndef EMAC_MPSAFE
    944 	splx(s);
    945 #endif
    946 
    947 	return error;
    948 }
    949 
    950 static bool
    951 sunxi_emac_has_internal_phy(struct sunxi_emac_softc *sc)
    952 {
    953 	const char * mdio_internal_compat[] = {
    954 		"allwinner,sun8i-h3-mdio-internal",
    955 		NULL
    956 	};
    957 	int phy;
    958 
    959 	/* Non-standard property, for compatible with old dts files */
    960 	if (of_hasprop(sc->phandle, "allwinner,use-internal-phy"))
    961 		return true;
    962 
    963 	phy = fdtbus_get_phandle(sc->phandle, "phy-handle");
    964 	if (phy == -1)
    965 		return false;
    966 
    967 	/* For internal PHY, check compatible string of parent node */
    968 	return of_compatible(OF_parent(phy), mdio_internal_compat) >= 0;
    969 }
    970 
    971 static int
    972 sunxi_emac_setup_phy(struct sunxi_emac_softc *sc)
    973 {
    974 	uint32_t reg, tx_delay, rx_delay;
    975 	const char *phy_type;
    976 
    977 	phy_type = fdtbus_get_string(sc->phandle, "phy-mode");
    978 	if (phy_type == NULL)
    979 		return 0;
    980 
    981 	aprint_debug_dev(sc->dev, "PHY type: %s\n", phy_type);
    982 
    983 	syscon_lock(sc->syscon);
    984 	reg = syscon_read_4(sc->syscon, EMAC_CLK_REG);
    985 
    986 	reg &= ~(EMAC_CLK_PIT | EMAC_CLK_SRC | EMAC_CLK_RMII_EN);
    987 	if (strcmp(phy_type, "rgmii") == 0)
    988 		reg |= EMAC_CLK_PIT_RGMII | EMAC_CLK_SRC_RGMII;
    989 	else if (strcmp(phy_type, "rmii") == 0)
    990 		reg |= EMAC_CLK_RMII_EN;
    991 	else
    992 		reg |= EMAC_CLK_PIT_MII | EMAC_CLK_SRC_MII;
    993 
    994 	if (of_getprop_uint32(sc->phandle, "allwinner,tx-delay-ps",
    995 	    &tx_delay) == 0) {
    996 		reg &= ~EMAC_CLK_ETXDC;
    997 		reg |= ((tx_delay / 100) << EMAC_CLK_ETXDC_SHIFT);
    998 	} else if (of_getprop_uint32(sc->phandle, "tx-delay", &tx_delay) == 0) {
    999 		reg &= ~EMAC_CLK_ETXDC;
   1000 		reg |= (tx_delay << EMAC_CLK_ETXDC_SHIFT);
   1001 	}
   1002 	if (of_getprop_uint32(sc->phandle, "allwinner,rx-delay-ps",
   1003 	    &rx_delay) == 0) {
   1004 		reg &= ~EMAC_CLK_ERXDC;
   1005 		reg |= ((rx_delay / 100) << EMAC_CLK_ERXDC_SHIFT);
   1006 	} else if (of_getprop_uint32(sc->phandle, "rx-delay", &rx_delay) == 0) {
   1007 		reg &= ~EMAC_CLK_ERXDC;
   1008 		reg |= (rx_delay << EMAC_CLK_ERXDC_SHIFT);
   1009 	}
   1010 
   1011 	if (sc->type == EMAC_H3 || sc->type == EMAC_H6) {
   1012 		if (sunxi_emac_has_internal_phy(sc)) {
   1013 			reg |= EMAC_CLK_EPHY_SELECT;
   1014 			reg &= ~EMAC_CLK_EPHY_SHUTDOWN;
   1015 			if (of_hasprop(sc->phandle,
   1016 			    "allwinner,leds-active-low"))
   1017 				reg |= EMAC_CLK_EPHY_LED_POL;
   1018 			else
   1019 				reg &= ~EMAC_CLK_EPHY_LED_POL;
   1020 
   1021 			/* Set internal PHY addr to 1 */
   1022 			reg &= ~EMAC_CLK_EPHY_ADDR;
   1023 			reg |= (1 << EMAC_CLK_EPHY_ADDR_SHIFT);
   1024 		} else {
   1025 			reg &= ~EMAC_CLK_EPHY_SELECT;
   1026 		}
   1027 	}
   1028 
   1029 	aprint_debug_dev(sc->dev, "EMAC clock: 0x%08x\n", reg);
   1030 
   1031 	syscon_write_4(sc->syscon, EMAC_CLK_REG, reg);
   1032 	syscon_unlock(sc->syscon);
   1033 
   1034 	return 0;
   1035 }
   1036 
   1037 static int
   1038 sunxi_emac_setup_resources(struct sunxi_emac_softc *sc)
   1039 {
   1040 	u_int freq;
   1041 	int error, div;
   1042 
   1043 	/* Configure PHY for MII or RGMII mode */
   1044 	if (sunxi_emac_setup_phy(sc) != 0)
   1045 		return ENXIO;
   1046 
   1047 	/* Enable clocks */
   1048 	error = clk_enable(sc->clk_ahb);
   1049 	if (error != 0) {
   1050 		aprint_error_dev(sc->dev, "cannot enable ahb clock\n");
   1051 		return error;
   1052 	}
   1053 
   1054 	if (sc->clk_ephy != NULL) {
   1055 		error = clk_enable(sc->clk_ephy);
   1056 		if (error != 0) {
   1057 			aprint_error_dev(sc->dev, "cannot enable ephy clock\n");
   1058 			return error;
   1059 		}
   1060 	}
   1061 
   1062 	/* De-assert reset */
   1063 	error = fdtbus_reset_deassert(sc->rst_ahb);
   1064 	if (error != 0) {
   1065 		aprint_error_dev(sc->dev, "cannot de-assert ahb reset\n");
   1066 		return error;
   1067 	}
   1068 	if (sc->rst_ephy != NULL) {
   1069 		error = fdtbus_reset_deassert(sc->rst_ephy);
   1070 		if (error != 0) {
   1071 			aprint_error_dev(sc->dev,
   1072 			    "cannot de-assert ephy reset\n");
   1073 			return error;
   1074 		}
   1075 	}
   1076 
   1077 	/* Enable PHY regulator if applicable */
   1078 	if (sc->reg_phy != NULL) {
   1079 		error = fdtbus_regulator_enable(sc->reg_phy);
   1080 		if (error != 0) {
   1081 			aprint_error_dev(sc->dev,
   1082 			    "cannot enable PHY regulator\n");
   1083 			return error;
   1084 		}
   1085 	}
   1086 
   1087 	/* Determine MDC clock divide ratio based on AHB clock */
   1088 	freq = clk_get_rate(sc->clk_ahb);
   1089 	if (freq == 0) {
   1090 		aprint_error_dev(sc->dev, "cannot get AHB clock frequency\n");
   1091 		return ENXIO;
   1092 	}
   1093 	div = freq / MDIO_FREQ;
   1094 	if (div <= 16)
   1095 		sc->mdc_div_ratio_m = MDC_DIV_RATIO_M_16;
   1096 	else if (div <= 32)
   1097 		sc->mdc_div_ratio_m = MDC_DIV_RATIO_M_32;
   1098 	else if (div <= 64)
   1099 		sc->mdc_div_ratio_m = MDC_DIV_RATIO_M_64;
   1100 	else if (div <= 128)
   1101 		sc->mdc_div_ratio_m = MDC_DIV_RATIO_M_128;
   1102 	else {
   1103 		aprint_error_dev(sc->dev,
   1104 		    "cannot determine MDC clock divide ratio\n");
   1105 		return ENXIO;
   1106 	}
   1107 
   1108 	aprint_debug_dev(sc->dev, "AHB frequency %u Hz, MDC div: 0x%x\n",
   1109 	    freq, sc->mdc_div_ratio_m);
   1110 
   1111 	return 0;
   1112 }
   1113 
   1114 static void
   1115 sunxi_emac_get_eaddr(struct sunxi_emac_softc *sc, uint8_t *eaddr)
   1116 {
   1117 	uint32_t maclo, machi;
   1118 #if notyet
   1119 	u_char rootkey[16];
   1120 #endif
   1121 
   1122 	machi = RD4(sc, EMAC_ADDR_HIGH(0)) & 0xffff;
   1123 	maclo = RD4(sc, EMAC_ADDR_LOW(0));
   1124 
   1125 	if (maclo == 0xffffffff && machi == 0xffff) {
   1126 #if notyet
   1127 		/* MAC address in hardware is invalid, create one */
   1128 		if (aw_sid_get_rootkey(rootkey) == 0 &&
   1129 		    (rootkey[3] | rootkey[12] | rootkey[13] | rootkey[14] |
   1130 		     rootkey[15]) != 0) {
   1131 			/* MAC address is derived from the root key in SID */
   1132 			maclo = (rootkey[13] << 24) | (rootkey[12] << 16) |
   1133 				(rootkey[3] << 8) | 0x02;
   1134 			machi = (rootkey[15] << 8) | rootkey[14];
   1135 		} else {
   1136 #endif
   1137 			/* Create one */
   1138 			maclo = 0x00f2 | (cprng_strong32() & 0xffff0000);
   1139 			machi = cprng_strong32() & 0xffff;
   1140 #if notyet
   1141 		}
   1142 #endif
   1143 	}
   1144 
   1145 	eaddr[0] = maclo & 0xff;
   1146 	eaddr[1] = (maclo >> 8) & 0xff;
   1147 	eaddr[2] = (maclo >> 16) & 0xff;
   1148 	eaddr[3] = (maclo >> 24) & 0xff;
   1149 	eaddr[4] = machi & 0xff;
   1150 	eaddr[5] = (machi >> 8) & 0xff;
   1151 }
   1152 
   1153 static int
   1154 sunxi_emac_phy_reset(struct sunxi_emac_softc *sc)
   1155 {
   1156 	uint32_t delay_prop[3];
   1157 	int pin_value;
   1158 
   1159 	if (sc->pin_reset == NULL)
   1160 		return 0;
   1161 
   1162 	if (OF_getprop(sc->phandle, "allwinner,reset-delays-us", delay_prop,
   1163 	    sizeof(delay_prop)) <= 0)
   1164 		return ENXIO;
   1165 
   1166 	pin_value = of_hasprop(sc->phandle, "allwinner,reset-active-low");
   1167 
   1168 	fdtbus_gpio_write(sc->pin_reset, pin_value);
   1169 	delay(htole32(delay_prop[0]));
   1170 	fdtbus_gpio_write(sc->pin_reset, !pin_value);
   1171 	delay(htole32(delay_prop[1]));
   1172 	fdtbus_gpio_write(sc->pin_reset, pin_value);
   1173 	delay(htole32(delay_prop[2]));
   1174 
   1175 	return 0;
   1176 }
   1177 
   1178 static int
   1179 sunxi_emac_setup_dma(struct sunxi_emac_softc *sc)
   1180 {
   1181 	struct mbuf *m;
   1182 	int error, nsegs, i;
   1183 
   1184 	/* Setup TX ring */
   1185 	sc->tx.buf_tag = sc->tx.desc_tag = sc->dmat;
   1186 	error = bus_dmamap_create(sc->dmat, TX_DESC_SIZE, 1, TX_DESC_SIZE, 0,
   1187 	    BUS_DMA_WAITOK, &sc->tx.desc_map);
   1188 	if (error)
   1189 		return error;
   1190 	error = bus_dmamem_alloc(sc->dmat, TX_DESC_SIZE, DESC_ALIGN, 0,
   1191 	    &sc->tx.desc_dmaseg, 1, &nsegs, BUS_DMA_WAITOK);
   1192 	if (error)
   1193 		return error;
   1194 	error = bus_dmamem_map(sc->dmat, &sc->tx.desc_dmaseg, nsegs,
   1195 	    TX_DESC_SIZE, (void *)&sc->tx.desc_ring,
   1196 	    BUS_DMA_WAITOK);
   1197 	if (error)
   1198 		return error;
   1199 	error = bus_dmamap_load(sc->dmat, sc->tx.desc_map, sc->tx.desc_ring,
   1200 	    TX_DESC_SIZE, NULL, BUS_DMA_WAITOK);
   1201 	if (error)
   1202 		return error;
   1203 	sc->tx.desc_ring_paddr = sc->tx.desc_map->dm_segs[0].ds_addr;
   1204 
   1205 	memset(sc->tx.desc_ring, 0, TX_DESC_SIZE);
   1206 	bus_dmamap_sync(sc->dmat, sc->tx.desc_map, 0, TX_DESC_SIZE,
   1207 	    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   1208 
   1209 	for (i = 0; i < TX_DESC_COUNT; i++)
   1210 		sc->tx.desc_ring[i].next =
   1211 		    htole32(sc->tx.desc_ring_paddr + DESC_OFF(TX_NEXT(i)));
   1212 
   1213 	sc->tx.queued = TX_DESC_COUNT;
   1214 	for (i = 0; i < TX_DESC_COUNT; i++) {
   1215 		error = bus_dmamap_create(sc->tx.buf_tag, MCLBYTES,
   1216 		    TX_MAX_SEGS, MCLBYTES, 0, BUS_DMA_WAITOK,
   1217 		    &sc->tx.buf_map[i].map);
   1218 		if (error != 0) {
   1219 			device_printf(sc->dev, "cannot create TX buffer map\n");
   1220 			return error;
   1221 		}
   1222 		sunxi_emac_setup_txdesc(sc, i, 0, 0, 0);
   1223 	}
   1224 
   1225 	/* Setup RX ring */
   1226 	sc->rx.buf_tag = sc->rx.desc_tag = sc->dmat;
   1227 	error = bus_dmamap_create(sc->dmat, RX_DESC_SIZE, 1, RX_DESC_SIZE, 0,
   1228 	    BUS_DMA_WAITOK, &sc->rx.desc_map);
   1229 	if (error)
   1230 		return error;
   1231 	error = bus_dmamem_alloc(sc->dmat, RX_DESC_SIZE, DESC_ALIGN, 0,
   1232 	    &sc->rx.desc_dmaseg, 1, &nsegs, BUS_DMA_WAITOK);
   1233 	if (error)
   1234 		return error;
   1235 	error = bus_dmamem_map(sc->dmat, &sc->rx.desc_dmaseg, nsegs,
   1236 	    RX_DESC_SIZE, (void *)&sc->rx.desc_ring,
   1237 	    BUS_DMA_WAITOK);
   1238 	if (error)
   1239 		return error;
   1240 	error = bus_dmamap_load(sc->dmat, sc->rx.desc_map, sc->rx.desc_ring,
   1241 	    RX_DESC_SIZE, NULL, BUS_DMA_WAITOK);
   1242 	if (error)
   1243 		return error;
   1244 	sc->rx.desc_ring_paddr = sc->rx.desc_map->dm_segs[0].ds_addr;
   1245 
   1246 	memset(sc->rx.desc_ring, 0, RX_DESC_SIZE);
   1247 
   1248 	for (i = 0; i < RX_DESC_COUNT; i++) {
   1249 		error = bus_dmamap_create(sc->rx.buf_tag, MCLBYTES,
   1250 		    RX_DESC_COUNT, MCLBYTES, 0, BUS_DMA_WAITOK,
   1251 		    &sc->rx.buf_map[i].map);
   1252 		if (error != 0) {
   1253 			device_printf(sc->dev, "cannot create RX buffer map\n");
   1254 			return error;
   1255 		}
   1256 		if ((m = sunxi_emac_alloc_mbufcl(sc)) == NULL) {
   1257 			device_printf(sc->dev, "cannot allocate RX mbuf\n");
   1258 			return ENOMEM;
   1259 		}
   1260 		error = sunxi_emac_setup_rxbuf(sc, i, m);
   1261 		if (error != 0) {
   1262 			device_printf(sc->dev, "cannot create RX buffer\n");
   1263 			return error;
   1264 		}
   1265 	}
   1266 	bus_dmamap_sync(sc->rx.desc_tag, sc->rx.desc_map,
   1267 	    0, sc->rx.desc_map->dm_mapsize,
   1268 	    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   1269 
   1270 	return 0;
   1271 }
   1272 
   1273 static int
   1274 sunxi_emac_get_resources(struct sunxi_emac_softc *sc)
   1275 {
   1276 	const int phandle = sc->phandle;
   1277 	bus_addr_t addr, size;
   1278 
   1279 	/* Map EMAC registers */
   1280 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0)
   1281 		return ENXIO;
   1282 	if (bus_space_map(sc->bst, addr, size, 0, &sc->bsh) != 0)
   1283 		return ENXIO;
   1284 
   1285 	/* Get SYSCON registers */
   1286 	sc->syscon = fdtbus_syscon_acquire(phandle, "syscon");
   1287 	if (sc->syscon == NULL)
   1288 		return ENXIO;
   1289 
   1290 	/* The "ahb"/"stmmaceth" clock and reset is required */
   1291 	if ((sc->clk_ahb = fdtbus_clock_get(phandle, "ahb")) == NULL &&
   1292 	    (sc->clk_ahb = fdtbus_clock_get(phandle, "stmmaceth")) == NULL)
   1293 		return ENXIO;
   1294 	if ((sc->rst_ahb = fdtbus_reset_get(phandle, "ahb")) == NULL &&
   1295 	    (sc->rst_ahb = fdtbus_reset_get(phandle, "stmmaceth")) == NULL)
   1296 		return ENXIO;
   1297 
   1298 	/* Internal PHY clock and reset are optional properties. */
   1299 	sc->clk_ephy = fdtbus_clock_get(phandle, "ephy");
   1300 	if (sc->clk_ephy == NULL) {
   1301 		int phy_phandle = fdtbus_get_phandle(phandle, "phy-handle");
   1302 		if (phy_phandle != -1)
   1303 			sc->clk_ephy = fdtbus_clock_get_index(phy_phandle, 0);
   1304 	}
   1305 	sc->rst_ephy = fdtbus_reset_get(phandle, "ephy");
   1306 	if (sc->rst_ephy == NULL) {
   1307 		int phy_phandle = fdtbus_get_phandle(phandle, "phy-handle");
   1308 		if (phy_phandle != -1)
   1309 			sc->rst_ephy = fdtbus_reset_get_index(phy_phandle, 0);
   1310 	}
   1311 
   1312 	/* Regulator is optional */
   1313 	sc->reg_phy = fdtbus_regulator_acquire(phandle, "phy-supply");
   1314 
   1315 	/* Reset GPIO is optional */
   1316 	sc->pin_reset = fdtbus_gpio_acquire(sc->phandle,
   1317 	    "allwinner,reset-gpio", GPIO_PIN_OUTPUT);
   1318 
   1319 	return 0;
   1320 }
   1321 
   1322 static int
   1323 sunxi_emac_get_phyid(struct sunxi_emac_softc *sc)
   1324 {
   1325 	bus_addr_t addr;
   1326 	int phy_phandle;
   1327 
   1328 	phy_phandle = fdtbus_get_phandle(sc->phandle, "phy");
   1329 	if (phy_phandle == -1)
   1330 		phy_phandle = fdtbus_get_phandle(sc->phandle, "phy-handle");
   1331 	if (phy_phandle == -1)
   1332 		return MII_PHY_ANY;
   1333 
   1334 	if (fdtbus_get_reg(phy_phandle, 0, &addr, NULL) != 0)
   1335 		return MII_PHY_ANY;
   1336 
   1337 	return (int)addr;
   1338 }
   1339 
   1340 static int
   1341 sunxi_emac_match(device_t parent, cfdata_t cf, void *aux)
   1342 {
   1343 	struct fdt_attach_args * const faa = aux;
   1344 
   1345 	return of_match_compat_data(faa->faa_phandle, compat_data);
   1346 }
   1347 
   1348 static void
   1349 sunxi_emac_attach(device_t parent, device_t self, void *aux)
   1350 {
   1351 	struct fdt_attach_args * const faa = aux;
   1352 	struct sunxi_emac_softc * const sc = device_private(self);
   1353 	const int phandle = faa->faa_phandle;
   1354 	struct mii_data *mii = &sc->mii;
   1355 	struct ifnet *ifp = &sc->ec.ec_if;
   1356 	uint8_t eaddr[ETHER_ADDR_LEN];
   1357 	char intrstr[128];
   1358 
   1359 	sc->dev = self;
   1360 	sc->phandle = phandle;
   1361 	sc->bst = faa->faa_bst;
   1362 	sc->dmat = faa->faa_dmat;
   1363 	sc->type = of_search_compatible(phandle, compat_data)->data;
   1364 	sc->phy_id = sunxi_emac_get_phyid(sc);
   1365 
   1366 	if (sunxi_emac_get_resources(sc) != 0) {
   1367 		aprint_error(": cannot allocate resources for device\n");
   1368 		return;
   1369 	}
   1370 	if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
   1371 		aprint_error(": cannot decode interrupt\n");
   1372 		return;
   1373 	}
   1374 
   1375 	mutex_init(&sc->mtx, MUTEX_DEFAULT, IPL_NET);
   1376 	callout_init(&sc->stat_ch, CALLOUT_FLAGS);
   1377 	callout_setfunc(&sc->stat_ch, sunxi_emac_tick, sc);
   1378 
   1379 	aprint_naive("\n");
   1380 	aprint_normal(": EMAC\n");
   1381 
   1382 	/* Setup clocks and regulators */
   1383 	if (sunxi_emac_setup_resources(sc) != 0)
   1384 		return;
   1385 
   1386 	/* Read MAC address before resetting the chip */
   1387 	sunxi_emac_get_eaddr(sc, eaddr);
   1388 	aprint_normal_dev(self, "Ethernet address %s\n", ether_sprintf(eaddr));
   1389 
   1390 	/* Reset PHY if necessary */
   1391 	if (sunxi_emac_phy_reset(sc) != 0) {
   1392 		aprint_error_dev(self, "failed to reset PHY\n");
   1393 		return;
   1394 	}
   1395 
   1396 	/* Setup DMA descriptors */
   1397 	if (sunxi_emac_setup_dma(sc) != 0) {
   1398 		aprint_error_dev(self, "failed to setup DMA descriptors\n");
   1399 		return;
   1400 	}
   1401 
   1402 	/* Install interrupt handler */
   1403 	sc->ih = fdtbus_intr_establish(phandle, 0, IPL_NET,
   1404 	    FDT_INTR_FLAGS, sunxi_emac_intr, sc);
   1405 	if (sc->ih == NULL) {
   1406 		aprint_error_dev(self, "failed to establish interrupt on %s\n",
   1407 		    intrstr);
   1408 		return;
   1409 	}
   1410 	aprint_normal_dev(self, "interrupting on %s\n", intrstr);
   1411 
   1412 	/* Setup ethernet interface */
   1413 	ifp->if_softc = sc;
   1414 	snprintf(ifp->if_xname, IFNAMSIZ, EMAC_IFNAME, device_unit(self));
   1415 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
   1416 #ifdef EMAC_MPSAFE
   1417 	ifp->if_extflags = IFEF_MPSAFE;
   1418 #endif
   1419 	ifp->if_start = sunxi_emac_start;
   1420 	ifp->if_ioctl = sunxi_emac_ioctl;
   1421 	ifp->if_init = sunxi_emac_init;
   1422 	ifp->if_stop = sunxi_emac_stop;
   1423 	ifp->if_capabilities = IFCAP_CSUM_IPv4_Rx |
   1424 			       IFCAP_CSUM_IPv4_Tx |
   1425 			       IFCAP_CSUM_TCPv4_Rx |
   1426 			       IFCAP_CSUM_TCPv4_Tx |
   1427 			       IFCAP_CSUM_UDPv4_Rx |
   1428 			       IFCAP_CSUM_UDPv4_Tx;
   1429 	ifp->if_capenable = ifp->if_capabilities;
   1430 	IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
   1431 	IFQ_SET_READY(&ifp->if_snd);
   1432 
   1433 	/* 802.1Q VLAN-sized frames are supported */
   1434 	sc->ec.ec_capabilities |= ETHERCAP_VLAN_MTU;
   1435 
   1436 	/* Attach MII driver */
   1437 	sc->ec.ec_mii = mii;
   1438 	ifmedia_init(&mii->mii_media, 0, ether_mediachange, ether_mediastatus);
   1439 	mii->mii_ifp = ifp;
   1440 	mii->mii_readreg = sunxi_emac_mii_readreg;
   1441 	mii->mii_writereg = sunxi_emac_mii_writereg;
   1442 	mii->mii_statchg = sunxi_emac_mii_statchg;
   1443 	mii_attach(self, mii, 0xffffffff, sc->phy_id, MII_OFFSET_ANY,
   1444 	    MIIF_DOPAUSE);
   1445 
   1446 	if (LIST_EMPTY(&mii->mii_phys)) {
   1447 		aprint_error_dev(self, "no PHY found!\n");
   1448 		return;
   1449 	}
   1450 	ifmedia_set(&mii->mii_media, IFM_ETHER|IFM_AUTO);
   1451 
   1452 	/* Attach interface */
   1453 	if_attach(ifp);
   1454 	if_deferred_start_init(ifp, NULL);
   1455 
   1456 	/* Attach ethernet interface */
   1457 	ether_ifattach(ifp, eaddr);
   1458 }
   1459 
   1460 CFATTACH_DECL_NEW(sunxi_emac, sizeof(struct sunxi_emac_softc),
   1461     sunxi_emac_match, sunxi_emac_attach, NULL, NULL);
   1462