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