Home | History | Annotate | Line # | Download | only in sunxi
sun4i_emac.c revision 1.9
      1  1.9   msaitoh /* $NetBSD: sun4i_emac.c,v 1.9 2019/05/23 10:57:27 msaitoh Exp $ */
      2  1.1  jmcneill 
      3  1.1  jmcneill /*-
      4  1.1  jmcneill  * Copyright (c) 2013-2017 The NetBSD Foundation, Inc.
      5  1.1  jmcneill  * All rights reserved.
      6  1.1  jmcneill  *
      7  1.1  jmcneill  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1  jmcneill  * by Matt Thomas of 3am Software Foundry and Jared McNeill.
      9  1.1  jmcneill  *
     10  1.1  jmcneill  * Redistribution and use in source and binary forms, with or without
     11  1.1  jmcneill  * modification, are permitted provided that the following conditions
     12  1.1  jmcneill  * are met:
     13  1.1  jmcneill  * 1. Redistributions of source code must retain the above copyright
     14  1.1  jmcneill  *    notice, this list of conditions and the following disclaimer.
     15  1.1  jmcneill  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  jmcneill  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  jmcneill  *    documentation and/or other materials provided with the distribution.
     18  1.1  jmcneill  *
     19  1.1  jmcneill  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.1  jmcneill  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.1  jmcneill  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.1  jmcneill  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.1  jmcneill  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.1  jmcneill  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.1  jmcneill  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.1  jmcneill  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.1  jmcneill  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.1  jmcneill  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.1  jmcneill  * POSSIBILITY OF SUCH DAMAGE.
     30  1.1  jmcneill  */
     31  1.1  jmcneill 
     32  1.1  jmcneill #include <sys/cdefs.h>
     33  1.1  jmcneill 
     34  1.9   msaitoh __KERNEL_RCSID(1, "$NetBSD: sun4i_emac.c,v 1.9 2019/05/23 10:57:27 msaitoh Exp $");
     35  1.1  jmcneill 
     36  1.1  jmcneill #include <sys/param.h>
     37  1.1  jmcneill #include <sys/bus.h>
     38  1.1  jmcneill #include <sys/device.h>
     39  1.1  jmcneill #include <sys/intr.h>
     40  1.1  jmcneill #include <sys/ioctl.h>
     41  1.1  jmcneill #include <sys/mutex.h>
     42  1.1  jmcneill #include <sys/rndsource.h>
     43  1.1  jmcneill #include <sys/kernel.h>
     44  1.1  jmcneill #include <sys/systm.h>
     45  1.1  jmcneill 
     46  1.1  jmcneill #include <net/bpf.h>
     47  1.1  jmcneill #include <net/if.h>
     48  1.1  jmcneill #include <net/if_dl.h>
     49  1.1  jmcneill #include <net/if_ether.h>
     50  1.1  jmcneill #include <net/if_media.h>
     51  1.1  jmcneill 
     52  1.1  jmcneill #include <dev/mii/miivar.h>
     53  1.1  jmcneill 
     54  1.1  jmcneill #include <dev/fdt/fdtvar.h>
     55  1.1  jmcneill 
     56  1.1  jmcneill #include <arm/sunxi/sunxi_sramc.h>
     57  1.1  jmcneill 
     58  1.1  jmcneill #define	EMAC_IFNAME	"emac%d"
     59  1.1  jmcneill 
     60  1.1  jmcneill #define	EMAC_CTL_REG		0x00
     61  1.1  jmcneill #define	 EMAC_CTL_RX_EN			__BIT(2)
     62  1.1  jmcneill #define	 EMAC_CTL_TX_EN			__BIT(1)
     63  1.1  jmcneill #define	 EMAC_CTL_RST			__BIT(0)
     64  1.1  jmcneill #define	EMAC_TX_MODE_REG	0x04
     65  1.1  jmcneill #define	 EMAC_TX_MODE_DMA		__BIT(1)
     66  1.1  jmcneill #define	 EMAC_TX_MODE_ABF_ENA		__BIT(0)
     67  1.1  jmcneill #define	EMAC_TX_FLOW_REG	0x08
     68  1.1  jmcneill #define	EMAC_TX_CTL0_REG	0x0c
     69  1.1  jmcneill #define	EMAC_TX_CTL1_REG	0x10
     70  1.1  jmcneill #define	EMAC_TX_CTL_REG(n)	(EMAC_TX_CTL0_REG+4*(n))
     71  1.1  jmcneill #define	 EMAC_TX_CTL_START		__BIT(0)
     72  1.1  jmcneill #define	EMAC_TX_INS_REG		0x14
     73  1.1  jmcneill #define	EMAC_TX_PL0_REG		0x18
     74  1.1  jmcneill #define	EMAC_TX_PL1_REG		0x1c
     75  1.1  jmcneill #define	EMAC_TX_PL_REG(n)	(EMAC_TX_PL0_REG+4*(n))
     76  1.1  jmcneill #define	EMAC_TX_STA_REG		0x20
     77  1.1  jmcneill #define	EMAC_TX_IO_DATA0_REG	0x24
     78  1.1  jmcneill #define	EMAC_TX_IO_DATA1_REG	0x28
     79  1.1  jmcneill #define	EMAC_TX_IO_DATA_REG(n)	(EMAC_TX_IO_DATA0_REG+4*(n))
     80  1.1  jmcneill #define	EMAC_TX_TSVL0_REG	0x2c
     81  1.1  jmcneill #define	EMAC_TX_TSVH0_REG	0x30
     82  1.1  jmcneill #define	EMAC_TX_TSVL1_REG	0x34
     83  1.1  jmcneill #define	EMAC_TX_TSVH1_REG	0x38
     84  1.1  jmcneill #define	EMAC_RX_CTL_REG		0x3c
     85  1.1  jmcneill #define	 EMAC_RX_CTL_SA_IF		__BIT(25)
     86  1.1  jmcneill #define	 EMAC_RX_CTL_SA			__BIT(24)
     87  1.1  jmcneill #define	 EMAC_RX_CTL_BC0		__BIT(22)
     88  1.1  jmcneill #define	 EMAC_RX_CTL_MHF		__BIT(21)
     89  1.1  jmcneill #define	 EMAC_RX_CTL_MC0		__BIT(20)
     90  1.1  jmcneill #define	 EMAC_RX_CTL_DAF		__BIT(17)
     91  1.1  jmcneill #define	 EMAC_RX_CTL_UCAD		__BIT(16)
     92  1.1  jmcneill #define	 EMAC_RX_CTL_POR		__BIT(8)
     93  1.1  jmcneill #define	 EMAC_RX_CTL_PLE		__BIT(7)
     94  1.1  jmcneill #define	 EMAC_RX_CTL_PCRCE		__BIT(6)
     95  1.1  jmcneill #define	 EMAC_RX_CTL_PCF		__BIT(5)
     96  1.1  jmcneill #define	 EMAC_RX_CTL_PROMISC		__BIT(4)
     97  1.1  jmcneill #define	 EMAC_RX_CTL_FIFO_RESET		__BIT(3)
     98  1.1  jmcneill #define	 EMAC_RX_CTL_DMA		__BIT(2)
     99  1.1  jmcneill #define	 EMAC_RX_CTL_DRQ_MODE		__BIT(1)
    100  1.1  jmcneill #define	 EMAC_RX_CTL_START		__BIT(0)
    101  1.1  jmcneill #define	EMAC_RX_HASH0_REG	0x40
    102  1.1  jmcneill #define	EMAC_RX_HASH1_REG	0x44
    103  1.1  jmcneill #define	EMAC_RX_STA_REG		0x48
    104  1.1  jmcneill #define	 EMAC_RX_STA_PKTOK		__BIT(7)
    105  1.1  jmcneill #define	 EMAC_RX_STA_ALNERR		__BIT(6)
    106  1.1  jmcneill #define	 EMAC_RX_STA_LENERR		__BIT(5)
    107  1.1  jmcneill #define	 EMAC_RX_STA_CRCERR		__BIT(4)
    108  1.1  jmcneill #define	EMAC_RX_IO_DATA_REG	0x4c
    109  1.1  jmcneill #define	EMAC_RX_FBC_REG		0x50
    110  1.1  jmcneill #define	EMAC_INT_CTL_REG	0x54
    111  1.1  jmcneill #define	EMAC_INT_STA_REG	0x58
    112  1.1  jmcneill #define	 EMAC_INT_RX			__BIT(8)
    113  1.1  jmcneill #define	 EMAC_INT_TX1			__BIT(1)
    114  1.1  jmcneill #define	 EMAC_INT_TX0			__BIT(0)
    115  1.1  jmcneill #define	 EMAC_INT_ENABLE		\
    116  1.9   msaitoh 		(EMAC_INT_RX | EMAC_INT_TX1 | EMAC_INT_TX0)
    117  1.1  jmcneill #define	EMAC_MAC_CTL0_REG	0x5c
    118  1.1  jmcneill #define	 EMAC_MAC_CTL0_SOFT_RESET	__BIT(15)
    119  1.1  jmcneill #define	 EMAC_MAC_CTL0_TFC		__BIT(3)
    120  1.1  jmcneill #define	 EMAC_MAC_CTL0_RFC		__BIT(2)
    121  1.1  jmcneill #define	EMAC_MAC_CTL1_REG	0x60
    122  1.1  jmcneill #define	 EMAC_MAC_CTL1_ED		__BIT(15)
    123  1.1  jmcneill #define	 EMAC_MAC_CTL1_NB		__BIT(13)
    124  1.1  jmcneill #define	 EMAC_MAC_CTL1_BNB		__BIT(12)
    125  1.1  jmcneill #define	 EMAC_MAC_CTL1_LPE		__BIT(9)
    126  1.1  jmcneill #define	 EMAC_MAC_CTL1_PRE		__BIT(8)
    127  1.1  jmcneill #define	 EMAC_MAC_CTL1_ADP		__BIT(7)
    128  1.1  jmcneill #define	 EMAC_MAC_CTL1_VC		__BIT(6)
    129  1.1  jmcneill #define	 EMAC_MAC_CTL1_PC		__BIT(5)
    130  1.1  jmcneill #define	 EMAC_MAC_CTL1_CRC		__BIT(4)
    131  1.1  jmcneill #define	 EMAC_MAC_CTL1_DCRC		__BIT(3)
    132  1.1  jmcneill #define	 EMAC_MAC_CTL1_HF		__BIT(2)
    133  1.1  jmcneill #define	 EMAC_MAC_CTL1_FLC		__BIT(1)
    134  1.1  jmcneill #define	 EMAC_MAC_CTL1_FD		__BIT(0)
    135  1.1  jmcneill #define	EMAC_MAC_IPGT_REG	0x64
    136  1.1  jmcneill #define	 EMAC_MAC_IPGT_FD		0x15
    137  1.1  jmcneill #define	EMAC_MAC_IPGR_REG	0x68
    138  1.1  jmcneill #define	 EMAC_MAC_IPGR_IPG1		__BITS(15,8)
    139  1.1  jmcneill #define	 EMAC_MAC_IPGR_IPG2		__BITS(7,0)
    140  1.1  jmcneill #define	EMAC_MAC_CLRT_REG	0x6c
    141  1.1  jmcneill #define	 EMAC_MAC_CLRT_CW		__BITS(15,8)
    142  1.1  jmcneill #define	 EMAC_MAC_CLRT_RM		__BITS(7,0)
    143  1.1  jmcneill #define	EMAC_MAC_MAXF_REG	0x70
    144  1.1  jmcneill #define	EMAC_MAC_SUPP_REG	0x74
    145  1.1  jmcneill #define	 EMAC_MAC_SUPP_100M		__BIT(8)
    146  1.1  jmcneill #define	EMAC_MAC_TEST_REG	0x78
    147  1.1  jmcneill #define	EMAC_MAC_MCFG_REG	0x7c
    148  1.1  jmcneill #define	 EMAC_MAC_MCFG_CLK		__BITS(5,2)
    149  1.1  jmcneill #define	EMAC_MAC_MCMD_REG	0x80
    150  1.1  jmcneill #define	EMAC_MAC_MADR_REG	0x84
    151  1.1  jmcneill #define	EMAC_MAC_MWTD_REG	0x88
    152  1.1  jmcneill #define	EMAC_MAC_MRDD_REG	0x8c
    153  1.1  jmcneill #define	EMAC_MAC_MIND_REG	0x90
    154  1.1  jmcneill #define	EMAC_MAC_SSRR_REG	0x94
    155  1.1  jmcneill #define	EMAC_MAC_A0_REG		0x98
    156  1.1  jmcneill #define	EMAC_MAC_A1_REG		0x9c
    157  1.1  jmcneill #define	EMAC_MAC_A2_REG		0xa0
    158  1.1  jmcneill 
    159  1.1  jmcneill #define	EMAC_RXHDR_STS			__BITS(31,16)
    160  1.1  jmcneill #define	EMAC_RXHDR_LEN			__BITS(15,0)
    161  1.1  jmcneill 
    162  1.1  jmcneill #define	EMAC_RX_MAGIC		0x0143414d	/* M A C \001 */
    163  1.1  jmcneill 
    164  1.1  jmcneill #define	EMAC_TXBUF_SIZE		4096
    165  1.1  jmcneill 
    166  1.1  jmcneill static int sun4i_emac_match(device_t, cfdata_t, void *);
    167  1.1  jmcneill static void sun4i_emac_attach(device_t, device_t, void *);
    168  1.1  jmcneill 
    169  1.1  jmcneill static int sun4i_emac_intr(void *);
    170  1.1  jmcneill static void sun4i_emac_tick(void *);
    171  1.1  jmcneill 
    172  1.6   msaitoh static int sun4i_emac_miibus_read_reg(device_t, int, int, uint16_t *);
    173  1.6   msaitoh static int sun4i_emac_miibus_write_reg(device_t, int, int, uint16_t);
    174  1.1  jmcneill static void sun4i_emac_miibus_statchg(struct ifnet *);
    175  1.1  jmcneill 
    176  1.1  jmcneill static void sun4i_emac_ifstart(struct ifnet *);
    177  1.1  jmcneill static int sun4i_emac_ifioctl(struct ifnet *, u_long, void *);
    178  1.1  jmcneill static int sun4i_emac_ifinit(struct ifnet *);
    179  1.1  jmcneill static void sun4i_emac_ifstop(struct ifnet *, int);
    180  1.1  jmcneill static void sun4i_emac_ifwatchdog(struct ifnet *);
    181  1.1  jmcneill 
    182  1.1  jmcneill struct sun4i_emac_softc;
    183  1.1  jmcneill static void sun4i_emac_rx_hash(struct sun4i_emac_softc *);
    184  1.1  jmcneill 
    185  1.1  jmcneill struct sun4i_emac_softc {
    186  1.1  jmcneill 	device_t sc_dev;
    187  1.1  jmcneill 	int sc_phandle;
    188  1.1  jmcneill 	bus_space_tag_t sc_bst;
    189  1.1  jmcneill 	bus_space_handle_t sc_bsh;
    190  1.1  jmcneill 	bus_dma_tag_t sc_dmat;
    191  1.1  jmcneill 	struct ethercom sc_ec;
    192  1.1  jmcneill 	struct mii_data sc_mii;
    193  1.1  jmcneill 	krndsource_t sc_rnd_source;	/* random source */
    194  1.1  jmcneill 	kmutex_t sc_intr_lock;
    195  1.1  jmcneill 	uint8_t sc_tx_active;
    196  1.1  jmcneill 	callout_t sc_stat_ch;
    197  1.1  jmcneill 	void *sc_ih;
    198  1.1  jmcneill 	uint32_t sc_txbuf[EMAC_TXBUF_SIZE/4];
    199  1.1  jmcneill };
    200  1.1  jmcneill 
    201  1.1  jmcneill static const char * compatible[] = {
    202  1.1  jmcneill 	"allwinner,sun4i-a10-emac",
    203  1.1  jmcneill 	NULL
    204  1.1  jmcneill };
    205  1.1  jmcneill 
    206  1.1  jmcneill CFATTACH_DECL_NEW(sun4i_emac, sizeof(struct sun4i_emac_softc),
    207  1.1  jmcneill 	sun4i_emac_match, sun4i_emac_attach, NULL, NULL);
    208  1.1  jmcneill 
    209  1.1  jmcneill static inline uint32_t
    210  1.1  jmcneill sun4i_emac_read(struct sun4i_emac_softc *sc, bus_size_t o)
    211  1.1  jmcneill {
    212  1.1  jmcneill 	return bus_space_read_4(sc->sc_bst, sc->sc_bsh, o);
    213  1.1  jmcneill }
    214  1.1  jmcneill 
    215  1.1  jmcneill static inline void
    216  1.1  jmcneill sun4i_emac_write(struct sun4i_emac_softc *sc, bus_size_t o, uint32_t v)
    217  1.1  jmcneill {
    218  1.1  jmcneill 	return bus_space_write_4(sc->sc_bst, sc->sc_bsh, o, v);
    219  1.1  jmcneill }
    220  1.1  jmcneill 
    221  1.1  jmcneill static inline void
    222  1.1  jmcneill sun4i_emac_clear_set(struct sun4i_emac_softc *sc, bus_size_t o, uint32_t c,
    223  1.1  jmcneill     uint32_t s)
    224  1.1  jmcneill {
    225  1.1  jmcneill 	uint32_t v = bus_space_read_4(sc->sc_bst, sc->sc_bsh, o);
    226  1.1  jmcneill 	return bus_space_write_4(sc->sc_bst, sc->sc_bsh, o, (v & ~c) | s);
    227  1.1  jmcneill }
    228  1.1  jmcneill 
    229  1.1  jmcneill static int
    230  1.1  jmcneill sun4i_emac_match(device_t parent, cfdata_t cf, void *aux)
    231  1.1  jmcneill {
    232  1.1  jmcneill 	struct fdt_attach_args * const faa = aux;
    233  1.1  jmcneill 
    234  1.1  jmcneill 	return of_match_compatible(faa->faa_phandle, compatible);
    235  1.1  jmcneill }
    236  1.1  jmcneill 
    237  1.1  jmcneill static void
    238  1.1  jmcneill sun4i_emac_attach(device_t parent, device_t self, void *aux)
    239  1.1  jmcneill {
    240  1.1  jmcneill 	struct sun4i_emac_softc * const sc = device_private(self);
    241  1.1  jmcneill 	struct fdt_attach_args * const faa = aux;
    242  1.1  jmcneill 	struct ifnet * const ifp = &sc->sc_ec.ec_if;
    243  1.1  jmcneill 	struct mii_data * const mii = &sc->sc_mii;
    244  1.1  jmcneill 	const int phandle = faa->faa_phandle;
    245  1.1  jmcneill 	char enaddr[ETHER_ADDR_LEN];
    246  1.1  jmcneill 	const uint8_t *local_addr;
    247  1.1  jmcneill 	char intrstr[128];
    248  1.1  jmcneill 	struct clk *clk;
    249  1.1  jmcneill 	bus_addr_t addr;
    250  1.1  jmcneill 	bus_size_t size;
    251  1.1  jmcneill 	int len;
    252  1.1  jmcneill 
    253  1.1  jmcneill 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
    254  1.1  jmcneill 		aprint_error(": cannot get registers\n");
    255  1.1  jmcneill 		return;
    256  1.1  jmcneill 	}
    257  1.1  jmcneill 
    258  1.1  jmcneill 	if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
    259  1.1  jmcneill 		aprint_error(": cannot decode interrupt\n");
    260  1.1  jmcneill 		return;
    261  1.1  jmcneill 	}
    262  1.1  jmcneill 
    263  1.1  jmcneill 	clk = fdtbus_clock_get_index(phandle, 0);
    264  1.1  jmcneill 	if (clk == NULL) {
    265  1.1  jmcneill 		aprint_error(": cannot acquire clock\n");
    266  1.1  jmcneill 		return;
    267  1.1  jmcneill 	}
    268  1.1  jmcneill 	if (clk_enable(clk) != 0) {
    269  1.1  jmcneill 		aprint_error(": cannot enable clock\n");
    270  1.1  jmcneill 		return;
    271  1.1  jmcneill 	}
    272  1.1  jmcneill 
    273  1.1  jmcneill 	if (sunxi_sramc_claim(phandle) != 0) {
    274  1.1  jmcneill 		aprint_error(": cannot map SRAM to EMAC\n");
    275  1.1  jmcneill 		return;
    276  1.1  jmcneill 	}
    277  1.1  jmcneill 
    278  1.1  jmcneill 	sc->sc_dev = self;
    279  1.1  jmcneill 	sc->sc_phandle = phandle;
    280  1.1  jmcneill 	sc->sc_ec.ec_mii = mii;
    281  1.1  jmcneill 	sc->sc_bst = faa->faa_bst;
    282  1.1  jmcneill 	if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
    283  1.1  jmcneill 		aprint_error(": cannot map registers\n");
    284  1.1  jmcneill 		return;
    285  1.1  jmcneill 	}
    286  1.1  jmcneill 	sc->sc_dmat = faa->faa_dmat;
    287  1.1  jmcneill 
    288  1.1  jmcneill 	mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_NET);
    289  1.1  jmcneill 	callout_init(&sc->sc_stat_ch, 0);
    290  1.1  jmcneill 	callout_setfunc(&sc->sc_stat_ch, sun4i_emac_tick, sc);
    291  1.1  jmcneill 
    292  1.1  jmcneill 	aprint_naive("\n");
    293  1.1  jmcneill 	aprint_normal(": 10/100 Ethernet Controller\n");
    294  1.1  jmcneill 
    295  1.1  jmcneill 	/*
    296  1.1  jmcneill 	 * Disable and then clear all interrupts
    297  1.1  jmcneill 	 */
    298  1.1  jmcneill 	sun4i_emac_write(sc, EMAC_INT_CTL_REG, 0);
    299  1.1  jmcneill 	sun4i_emac_write(sc, EMAC_INT_STA_REG,
    300  1.1  jmcneill 	    sun4i_emac_read(sc, EMAC_INT_STA_REG));
    301  1.1  jmcneill 
    302  1.1  jmcneill 	sc->sc_ih = fdtbus_intr_establish(phandle, 0, IPL_NET, 0,
    303  1.1  jmcneill 	    sun4i_emac_intr, sc);
    304  1.1  jmcneill 	if (sc->sc_ih == NULL) {
    305  1.1  jmcneill 		aprint_error_dev(self, "failed to establish interrupt on %s\n",
    306  1.1  jmcneill 		    intrstr);
    307  1.1  jmcneill 		return;
    308  1.1  jmcneill 	}
    309  1.1  jmcneill 	aprint_normal_dev(self, "interrupting on %s\n", intrstr);
    310  1.1  jmcneill 
    311  1.1  jmcneill 	local_addr = fdtbus_get_prop(phandle, "local-mac-address", &len);
    312  1.1  jmcneill 	if (local_addr && len == ETHER_ADDR_LEN) {
    313  1.1  jmcneill 		memcpy(enaddr, local_addr, ETHER_ADDR_LEN);
    314  1.1  jmcneill 
    315  1.1  jmcneill 		uint32_t a1 = ((uint32_t)enaddr[0] << 16) |
    316  1.1  jmcneill 			      ((uint32_t)enaddr[1] << 8) |
    317  1.1  jmcneill 			       (uint32_t)enaddr[2];
    318  1.1  jmcneill 		uint32_t a0 = ((uint32_t)enaddr[3] << 16) |
    319  1.1  jmcneill 			      ((uint32_t)enaddr[4] << 8) |
    320  1.1  jmcneill 			       (uint32_t)enaddr[5];
    321  1.1  jmcneill 
    322  1.1  jmcneill 		sun4i_emac_write(sc, EMAC_MAC_A1_REG, a1);
    323  1.1  jmcneill 		sun4i_emac_write(sc, EMAC_MAC_A0_REG, a0);
    324  1.1  jmcneill 	}
    325  1.1  jmcneill 
    326  1.1  jmcneill 	uint32_t a1 = sun4i_emac_read(sc, EMAC_MAC_A1_REG);
    327  1.1  jmcneill 	uint32_t a0 = sun4i_emac_read(sc, EMAC_MAC_A0_REG);
    328  1.1  jmcneill 	if (a0 != 0 || a1 != 0) {
    329  1.1  jmcneill 		enaddr[0] = a1 >> 16;
    330  1.1  jmcneill 		enaddr[1] = a1 >>  8;
    331  1.1  jmcneill 		enaddr[2] = a1 >>  0;
    332  1.1  jmcneill 		enaddr[3] = a0 >> 16;
    333  1.1  jmcneill 		enaddr[4] = a0 >>  8;
    334  1.1  jmcneill 		enaddr[5] = a0 >>  0;
    335  1.1  jmcneill 	}
    336  1.5     sevan 	aprint_normal_dev(self, "Ethernet address %s\n", ether_sprintf(enaddr));
    337  1.1  jmcneill 
    338  1.1  jmcneill 	snprintf(ifp->if_xname, IFNAMSIZ, EMAC_IFNAME, device_unit(self));
    339  1.1  jmcneill 	ifp->if_softc = sc;
    340  1.1  jmcneill 	ifp->if_capabilities = 0;
    341  1.1  jmcneill 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    342  1.1  jmcneill 	ifp->if_start = sun4i_emac_ifstart;
    343  1.1  jmcneill 	ifp->if_ioctl = sun4i_emac_ifioctl;
    344  1.1  jmcneill 	ifp->if_init = sun4i_emac_ifinit;
    345  1.1  jmcneill 	ifp->if_stop = sun4i_emac_ifstop;
    346  1.1  jmcneill 	ifp->if_watchdog = sun4i_emac_ifwatchdog;
    347  1.1  jmcneill 	IFQ_SET_READY(&ifp->if_snd);
    348  1.1  jmcneill 
    349  1.2  jmcneill 	/* 802.1Q VLAN-sized frames are supported */
    350  1.2  jmcneill 	sc->sc_ec.ec_capabilities |= ETHERCAP_VLAN_MTU;
    351  1.2  jmcneill 
    352  1.1  jmcneill 	ifmedia_init(&mii->mii_media, 0, ether_mediachange, ether_mediastatus);
    353  1.1  jmcneill 
    354  1.1  jmcneill         mii->mii_ifp = ifp;
    355  1.1  jmcneill         mii->mii_readreg = sun4i_emac_miibus_read_reg;
    356  1.1  jmcneill         mii->mii_writereg = sun4i_emac_miibus_write_reg;
    357  1.1  jmcneill         mii->mii_statchg = sun4i_emac_miibus_statchg;
    358  1.1  jmcneill 
    359  1.1  jmcneill         mii_attach(self, mii, 0xffffffff, MII_PHY_ANY, MII_OFFSET_ANY, 0);
    360  1.1  jmcneill 
    361  1.9   msaitoh         if (LIST_EMPTY(&mii->mii_phys)) {
    362  1.1  jmcneill                 aprint_error_dev(self, "no PHY found!\n");
    363  1.9   msaitoh                 ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_MANUAL, 0, NULL);
    364  1.9   msaitoh                 ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_MANUAL);
    365  1.1  jmcneill         } else {
    366  1.9   msaitoh                 ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
    367  1.1  jmcneill         }
    368  1.1  jmcneill 
    369  1.9   msaitoh 	/*
    370  1.1  jmcneill 	 * Attach the interface.
    371  1.1  jmcneill 	 */
    372  1.1  jmcneill 	if_attach(ifp);
    373  1.1  jmcneill 	if_deferred_start_init(ifp, NULL);
    374  1.9   msaitoh 	ether_ifattach(ifp, enaddr);
    375  1.1  jmcneill 	rnd_attach_source(&sc->sc_rnd_source, device_xname(self),
    376  1.1  jmcneill 	    RND_TYPE_NET, RND_FLAG_DEFAULT);
    377  1.1  jmcneill }
    378  1.1  jmcneill 
    379  1.1  jmcneill static inline void
    380  1.1  jmcneill sun4i_emac_int_enable(struct sun4i_emac_softc *sc)
    381  1.1  jmcneill {
    382  1.1  jmcneill 	sun4i_emac_clear_set(sc, EMAC_INT_CTL_REG, 0,
    383  1.1  jmcneill 	    EMAC_INT_ENABLE);
    384  1.1  jmcneill 	sun4i_emac_write(sc, EMAC_INT_STA_REG,
    385  1.1  jmcneill 	    sun4i_emac_read(sc, EMAC_INT_STA_REG));
    386  1.1  jmcneill }
    387  1.1  jmcneill 
    388  1.1  jmcneill int
    389  1.6   msaitoh sun4i_emac_miibus_read_reg(device_t self, int phy, int reg, uint16_t *val)
    390  1.1  jmcneill {
    391  1.1  jmcneill 	struct sun4i_emac_softc * const sc = device_private(self);
    392  1.1  jmcneill 	int retry = 100;
    393  1.6   msaitoh 	int rv = 0;
    394  1.1  jmcneill 
    395  1.1  jmcneill 	sun4i_emac_write(sc, EMAC_MAC_MADR_REG, (phy << 8) | reg);
    396  1.1  jmcneill 	sun4i_emac_write(sc, EMAC_MAC_MCMD_REG, 1);
    397  1.1  jmcneill 
    398  1.1  jmcneill 	while (--retry > 0 && (sun4i_emac_read(sc, EMAC_MAC_MIND_REG) & 1) != 0)
    399  1.1  jmcneill 		delay(1000);
    400  1.6   msaitoh 	if (retry == 0) {
    401  1.1  jmcneill 		device_printf(self, "PHY read timeout\n");
    402  1.6   msaitoh 		rv = ETIMEDOUT;
    403  1.6   msaitoh 	}
    404  1.1  jmcneill 
    405  1.1  jmcneill 	sun4i_emac_write(sc, EMAC_MAC_MCMD_REG, 0);
    406  1.6   msaitoh 	*val = sun4i_emac_read(sc, EMAC_MAC_MRDD_REG) & 0xffff;
    407  1.1  jmcneill 
    408  1.1  jmcneill 	return rv;
    409  1.1  jmcneill }
    410  1.1  jmcneill 
    411  1.6   msaitoh int
    412  1.6   msaitoh sun4i_emac_miibus_write_reg(device_t self, int phy, int reg, uint16_t val)
    413  1.1  jmcneill {
    414  1.1  jmcneill 	struct sun4i_emac_softc * const sc = device_private(self);
    415  1.1  jmcneill 	int retry = 100;
    416  1.6   msaitoh 	int rv = 0;
    417  1.1  jmcneill 
    418  1.1  jmcneill 	sun4i_emac_write(sc, EMAC_MAC_MADR_REG, (phy << 8) | reg);
    419  1.1  jmcneill 	sun4i_emac_write(sc, EMAC_MAC_MCMD_REG, 1);
    420  1.1  jmcneill 
    421  1.1  jmcneill 	while (--retry > 0 && (sun4i_emac_read(sc, EMAC_MAC_MIND_REG) & 1) != 0)
    422  1.1  jmcneill 		delay(1000);
    423  1.6   msaitoh 	if (retry == 0) {
    424  1.1  jmcneill 		device_printf(self, "PHY write timeout\n");
    425  1.6   msaitoh 		rv = ETIMEDOUT;
    426  1.6   msaitoh 	}
    427  1.1  jmcneill 
    428  1.1  jmcneill 	sun4i_emac_write(sc, EMAC_MAC_MCMD_REG, 0);
    429  1.1  jmcneill 	sun4i_emac_write(sc, EMAC_MAC_MWTD_REG, val);
    430  1.6   msaitoh 
    431  1.6   msaitoh 	return rv;
    432  1.1  jmcneill }
    433  1.1  jmcneill 
    434  1.1  jmcneill void
    435  1.1  jmcneill sun4i_emac_miibus_statchg(struct ifnet *ifp)
    436  1.1  jmcneill {
    437  1.1  jmcneill 	struct sun4i_emac_softc * const sc = ifp->if_softc;
    438  1.1  jmcneill 	struct mii_data * const mii = &sc->sc_mii;
    439  1.1  jmcneill 	const u_int media = mii->mii_media_active;
    440  1.1  jmcneill 
    441  1.1  jmcneill 	/*
    442  1.1  jmcneill 	 * Set MII interface based on the speed
    443  1.9   msaitoh 	 * negotiated by the PHY.
    444  1.9   msaitoh 	 */
    445  1.1  jmcneill 	switch (IFM_SUBTYPE(media)) {
    446  1.1  jmcneill 	case IFM_10_T:
    447  1.1  jmcneill 		sun4i_emac_clear_set(sc, EMAC_MAC_SUPP_REG,
    448  1.1  jmcneill 		    EMAC_MAC_SUPP_100M, 0);
    449  1.1  jmcneill 		break;
    450  1.1  jmcneill 	case IFM_100_TX:
    451  1.1  jmcneill 		sun4i_emac_clear_set(sc, EMAC_MAC_SUPP_REG,
    452  1.1  jmcneill 		    0, EMAC_MAC_SUPP_100M);
    453  1.1  jmcneill 		break;
    454  1.1  jmcneill 	}
    455  1.1  jmcneill 
    456  1.9   msaitoh 	const bool link = (IFM_SUBTYPE(media) & (IFM_10_T | IFM_100_TX)) != 0;
    457  1.1  jmcneill 	if (link) {
    458  1.1  jmcneill 		if (media & IFM_FDX) {
    459  1.1  jmcneill 			sun4i_emac_clear_set(sc, EMAC_MAC_CTL1_REG,
    460  1.1  jmcneill 			    0, EMAC_MAC_CTL1_FD);
    461  1.1  jmcneill 		} else {
    462  1.1  jmcneill 			sun4i_emac_clear_set(sc, EMAC_MAC_CTL1_REG,
    463  1.1  jmcneill 			    EMAC_MAC_CTL1_FD, 0);
    464  1.1  jmcneill 		}
    465  1.1  jmcneill 	}
    466  1.1  jmcneill }
    467  1.1  jmcneill 
    468  1.1  jmcneill static void
    469  1.1  jmcneill sun4i_emac_tick(void *softc)
    470  1.1  jmcneill {
    471  1.1  jmcneill 	struct sun4i_emac_softc * const sc = softc;
    472  1.1  jmcneill 	struct mii_data * const mii = &sc->sc_mii;
    473  1.1  jmcneill 	int s;
    474  1.1  jmcneill 
    475  1.1  jmcneill 	s = splnet();
    476  1.1  jmcneill 	mii_tick(mii);
    477  1.1  jmcneill 	callout_schedule(&sc->sc_stat_ch, hz);
    478  1.1  jmcneill 	splx(s);
    479  1.1  jmcneill }
    480  1.1  jmcneill 
    481  1.1  jmcneill static inline void
    482  1.1  jmcneill sun4i_emac_rxfifo_flush(struct sun4i_emac_softc *sc)
    483  1.1  jmcneill {
    484  1.1  jmcneill 	sun4i_emac_clear_set(sc, EMAC_CTL_REG, EMAC_CTL_RX_EN, 0);
    485  1.1  jmcneill 
    486  1.1  jmcneill 	sun4i_emac_clear_set(sc, EMAC_RX_CTL_REG, 0, EMAC_RX_CTL_FIFO_RESET);
    487  1.1  jmcneill 
    488  1.1  jmcneill 	for (;;) {
    489  1.1  jmcneill 		uint32_t v0 = sun4i_emac_read(sc, EMAC_RX_CTL_REG);
    490  1.1  jmcneill 		if ((v0 & EMAC_RX_CTL_FIFO_RESET) == 0)
    491  1.1  jmcneill 			break;
    492  1.1  jmcneill 	}
    493  1.1  jmcneill 
    494  1.1  jmcneill 	sun4i_emac_clear_set(sc, EMAC_CTL_REG, 0, EMAC_CTL_RX_EN);
    495  1.1  jmcneill }
    496  1.1  jmcneill 
    497  1.1  jmcneill static void
    498  1.1  jmcneill sun4i_emac_rxfifo_consume(struct sun4i_emac_softc *sc, size_t len)
    499  1.1  jmcneill {
    500  1.1  jmcneill 	for (len = (len + 3) >> 2; len > 0; len--) {
    501  1.1  jmcneill 		(void) sun4i_emac_read(sc, EMAC_RX_IO_DATA_REG);
    502  1.1  jmcneill 	}
    503  1.1  jmcneill }
    504  1.1  jmcneill 
    505  1.1  jmcneill static void
    506  1.1  jmcneill sun4i_emac_rxfifo_transfer(struct sun4i_emac_softc *sc, struct mbuf *m)
    507  1.1  jmcneill {
    508  1.1  jmcneill 	uint32_t *dp32 = mtod(m, uint32_t *);
    509  1.1  jmcneill 	const int len = roundup2(m->m_len, 4);
    510  1.1  jmcneill 
    511  1.9   msaitoh 	bus_space_read_multi_4(sc->sc_bst, sc->sc_bsh,
    512  1.1  jmcneill 	    EMAC_RX_IO_DATA_REG, dp32, len / 4);
    513  1.1  jmcneill }
    514  1.1  jmcneill 
    515  1.1  jmcneill static struct mbuf *
    516  1.1  jmcneill sun4i_emac_mgethdr(struct sun4i_emac_softc *sc, size_t rxlen)
    517  1.1  jmcneill {
    518  1.1  jmcneill 	struct mbuf *m = m_gethdr(M_DONTWAIT, MT_DATA);
    519  1.1  jmcneill 
    520  1.3      maxv 	if (m == NULL) {
    521  1.3      maxv 		return NULL;
    522  1.3      maxv 	}
    523  1.1  jmcneill 	if (rxlen + 2 > MHLEN) {
    524  1.1  jmcneill 		MCLGET(m, M_DONTWAIT);
    525  1.1  jmcneill 		if ((m->m_flags & M_EXT) == 0) {
    526  1.1  jmcneill 			m_free(m);
    527  1.1  jmcneill 			return NULL;
    528  1.1  jmcneill 		}
    529  1.1  jmcneill 	}
    530  1.1  jmcneill 
    531  1.1  jmcneill 	m_adj(m, 2);
    532  1.1  jmcneill 	m->m_len = rxlen;
    533  1.1  jmcneill 	m->m_pkthdr.len = rxlen;
    534  1.1  jmcneill 	m_set_rcvif(m, &sc->sc_ec.ec_if);
    535  1.1  jmcneill 	m->m_flags |= M_HASFCS;
    536  1.1  jmcneill 
    537  1.1  jmcneill 	return m;
    538  1.1  jmcneill }
    539  1.1  jmcneill 
    540  1.1  jmcneill static void
    541  1.1  jmcneill sun4i_emac_if_input(struct sun4i_emac_softc *sc, struct mbuf *m)
    542  1.1  jmcneill {
    543  1.1  jmcneill 	struct ifnet * const ifp = &sc->sc_ec.ec_if;
    544  1.1  jmcneill 
    545  1.1  jmcneill 	if_percpuq_enqueue(ifp->if_percpuq, m);
    546  1.1  jmcneill }
    547  1.1  jmcneill 
    548  1.1  jmcneill static void
    549  1.1  jmcneill sun4i_emac_rx_intr(struct sun4i_emac_softc *sc)
    550  1.1  jmcneill {
    551  1.1  jmcneill 	for (;;) {
    552  1.1  jmcneill 		uint32_t rx_count = sun4i_emac_read(sc, EMAC_RX_FBC_REG);
    553  1.1  jmcneill 		struct mbuf *m;
    554  1.1  jmcneill 
    555  1.1  jmcneill 		if (rx_count == 0) {
    556  1.1  jmcneill 			rx_count = sun4i_emac_read(sc, EMAC_RX_FBC_REG);
    557  1.1  jmcneill 			if (rx_count == 0)
    558  1.1  jmcneill 				return;
    559  1.1  jmcneill 		}
    560  1.1  jmcneill 
    561  1.1  jmcneill 		uint32_t v = sun4i_emac_read(sc, EMAC_RX_IO_DATA_REG);
    562  1.1  jmcneill 		if (v != EMAC_RX_MAGIC) {
    563  1.1  jmcneill 			sun4i_emac_rxfifo_flush(sc);
    564  1.1  jmcneill 			return;
    565  1.1  jmcneill 		}
    566  1.1  jmcneill 
    567  1.1  jmcneill 		uint32_t rxhdr = sun4i_emac_read(sc, EMAC_RX_IO_DATA_REG);
    568  1.1  jmcneill 		uint32_t rxlen = __SHIFTOUT(rxhdr, EMAC_RXHDR_LEN);
    569  1.1  jmcneill 		uint32_t rxsts = __SHIFTOUT(rxhdr, EMAC_RXHDR_STS);
    570  1.1  jmcneill 
    571  1.1  jmcneill 		if (rxlen < ETHER_MIN_LEN || (rxsts & EMAC_RX_STA_PKTOK) == 0) {
    572  1.1  jmcneill 			sc->sc_ec.ec_if.if_ierrors++;
    573  1.1  jmcneill 			continue;
    574  1.1  jmcneill 		}
    575  1.1  jmcneill 
    576  1.1  jmcneill 		m = sun4i_emac_mgethdr(sc, rxlen);
    577  1.1  jmcneill 		if (m == NULL) {
    578  1.1  jmcneill 			sc->sc_ec.ec_if.if_ierrors++;
    579  1.1  jmcneill 			sun4i_emac_rxfifo_consume(sc, rxlen);
    580  1.1  jmcneill 			return;
    581  1.1  jmcneill 		}
    582  1.1  jmcneill 
    583  1.1  jmcneill 		sun4i_emac_rxfifo_transfer(sc, m);
    584  1.1  jmcneill 		sun4i_emac_if_input(sc, m);
    585  1.1  jmcneill 	}
    586  1.1  jmcneill }
    587  1.1  jmcneill 
    588  1.1  jmcneill static int
    589  1.1  jmcneill sun4i_emac_txfifo_transfer(struct sun4i_emac_softc *sc, struct mbuf *m, u_int slot)
    590  1.1  jmcneill {
    591  1.1  jmcneill 	bus_size_t const io_data_reg = EMAC_TX_IO_DATA_REG(0);
    592  1.1  jmcneill 	const int len = m->m_pkthdr.len;
    593  1.1  jmcneill 	uint32_t *pktdata;
    594  1.1  jmcneill 
    595  1.1  jmcneill 	KASSERT(len > 0 && len <= sizeof(sc->sc_txbuf));
    596  1.1  jmcneill 
    597  1.1  jmcneill 	if (m->m_next != NULL) {
    598  1.1  jmcneill 		m_copydata(m, 0, len, sc->sc_txbuf);
    599  1.1  jmcneill 		pktdata = sc->sc_txbuf;
    600  1.1  jmcneill 	} else {
    601  1.1  jmcneill 		pktdata = mtod(m, uint32_t *);
    602  1.1  jmcneill 	}
    603  1.1  jmcneill 
    604  1.1  jmcneill 	bus_space_write_multi_4(sc->sc_bst, sc->sc_bsh, io_data_reg,
    605  1.1  jmcneill 	    pktdata, roundup2(len, 4) / 4);
    606  1.1  jmcneill 
    607  1.1  jmcneill 	return len;
    608  1.1  jmcneill }
    609  1.1  jmcneill 
    610  1.1  jmcneill static void
    611  1.1  jmcneill sun4i_emac_tx_enqueue(struct sun4i_emac_softc *sc, struct mbuf *m, u_int slot)
    612  1.1  jmcneill {
    613  1.1  jmcneill 	struct ifnet * const ifp = &sc->sc_ec.ec_if;
    614  1.1  jmcneill 
    615  1.1  jmcneill 	sun4i_emac_write(sc, EMAC_TX_INS_REG, slot);
    616  1.1  jmcneill 
    617  1.1  jmcneill 	const int len = sun4i_emac_txfifo_transfer(sc, m, slot);
    618  1.1  jmcneill 
    619  1.1  jmcneill 	bus_size_t const pl_reg = EMAC_TX_PL_REG(slot);
    620  1.1  jmcneill 	bus_size_t const ctl_reg = EMAC_TX_CTL_REG(slot);
    621  1.1  jmcneill 
    622  1.1  jmcneill 	sun4i_emac_write(sc, pl_reg, len);
    623  1.1  jmcneill 	sun4i_emac_clear_set(sc, ctl_reg, 0, EMAC_TX_CTL_START);
    624  1.1  jmcneill 
    625  1.4   msaitoh 	bpf_mtap(ifp, m, BPF_D_OUT);
    626  1.1  jmcneill 
    627  1.1  jmcneill 	m_freem(m);
    628  1.1  jmcneill }
    629  1.1  jmcneill 
    630  1.1  jmcneill static void
    631  1.1  jmcneill sun4i_emac_tx_intr(struct sun4i_emac_softc *sc, u_int slot)
    632  1.1  jmcneill {
    633  1.1  jmcneill 	struct ifnet * const ifp = &sc->sc_ec.ec_if;
    634  1.1  jmcneill 
    635  1.1  jmcneill 	sc->sc_tx_active &= ~__BIT(slot);
    636  1.1  jmcneill 	ifp->if_flags &= ~IFF_OACTIVE;
    637  1.1  jmcneill }
    638  1.1  jmcneill 
    639  1.1  jmcneill int
    640  1.1  jmcneill sun4i_emac_intr(void *arg)
    641  1.1  jmcneill {
    642  1.1  jmcneill 	struct sun4i_emac_softc * const sc = arg;
    643  1.1  jmcneill 	struct ifnet * const ifp = &sc->sc_ec.ec_if;
    644  1.1  jmcneill 
    645  1.1  jmcneill 	mutex_enter(&sc->sc_intr_lock);
    646  1.1  jmcneill 
    647  1.1  jmcneill 	uint32_t sts = sun4i_emac_read(sc, EMAC_INT_STA_REG);
    648  1.1  jmcneill 	sun4i_emac_write(sc, EMAC_INT_STA_REG, sts);
    649  1.1  jmcneill 	rnd_add_uint32(&sc->sc_rnd_source, sts);
    650  1.1  jmcneill 
    651  1.1  jmcneill 	if (sts & EMAC_INT_RX) {
    652  1.1  jmcneill 		sun4i_emac_rx_intr(sc);
    653  1.1  jmcneill 	}
    654  1.1  jmcneill 	if (sts & EMAC_INT_TX0) {
    655  1.1  jmcneill 		sun4i_emac_tx_intr(sc, 0);
    656  1.1  jmcneill 	}
    657  1.1  jmcneill 	if (sts & EMAC_INT_TX1) {
    658  1.1  jmcneill 		sun4i_emac_tx_intr(sc, 1);
    659  1.1  jmcneill 	}
    660  1.9   msaitoh 	if (sts & (EMAC_INT_TX0 | EMAC_INT_TX1)) {
    661  1.1  jmcneill 		if (sc->sc_tx_active == 0)
    662  1.1  jmcneill 			ifp->if_timer = 0;
    663  1.1  jmcneill 		if_schedule_deferred_start(ifp);
    664  1.1  jmcneill 	}
    665  1.1  jmcneill 
    666  1.1  jmcneill 	mutex_exit(&sc->sc_intr_lock);
    667  1.1  jmcneill 
    668  1.1  jmcneill 	return 1;
    669  1.1  jmcneill }
    670  1.1  jmcneill 
    671  1.1  jmcneill void
    672  1.1  jmcneill sun4i_emac_ifstart(struct ifnet *ifp)
    673  1.1  jmcneill {
    674  1.1  jmcneill 	struct sun4i_emac_softc * const sc = ifp->if_softc;
    675  1.1  jmcneill 
    676  1.1  jmcneill 	mutex_enter(&sc->sc_intr_lock);
    677  1.1  jmcneill 
    678  1.1  jmcneill 	if ((sc->sc_tx_active & 1) == 0) {
    679  1.1  jmcneill 		struct mbuf *m;
    680  1.1  jmcneill 		IFQ_DEQUEUE(&ifp->if_snd, m);
    681  1.1  jmcneill 		if (m == NULL) {
    682  1.1  jmcneill 			mutex_exit(&sc->sc_intr_lock);
    683  1.1  jmcneill 			return;
    684  1.1  jmcneill 		}
    685  1.1  jmcneill 		sun4i_emac_tx_enqueue(sc, m, 0);
    686  1.1  jmcneill 		sc->sc_tx_active |= 1;
    687  1.1  jmcneill 	}
    688  1.1  jmcneill 
    689  1.1  jmcneill 	if ((sc->sc_tx_active & 2) == 0) {
    690  1.1  jmcneill 		struct mbuf *m;
    691  1.1  jmcneill 		IFQ_DEQUEUE(&ifp->if_snd, m);
    692  1.1  jmcneill 		if (m == NULL) {
    693  1.1  jmcneill 			mutex_exit(&sc->sc_intr_lock);
    694  1.1  jmcneill 			return;
    695  1.1  jmcneill 		}
    696  1.1  jmcneill 		sun4i_emac_tx_enqueue(sc, m, 1);
    697  1.1  jmcneill 		sc->sc_tx_active |= 2;
    698  1.1  jmcneill 	}
    699  1.1  jmcneill 
    700  1.1  jmcneill 	if (sc->sc_tx_active == 3)
    701  1.1  jmcneill 		ifp->if_flags |= IFF_OACTIVE;
    702  1.1  jmcneill 
    703  1.1  jmcneill 	ifp->if_timer = 5;
    704  1.1  jmcneill 
    705  1.1  jmcneill 	mutex_exit(&sc->sc_intr_lock);
    706  1.1  jmcneill }
    707  1.1  jmcneill 
    708  1.1  jmcneill 
    709  1.1  jmcneill static int
    710  1.1  jmcneill sun4i_emac_ifioctl(struct ifnet *ifp, u_long cmd, void *data)
    711  1.1  jmcneill {
    712  1.1  jmcneill 	struct sun4i_emac_softc * const sc = ifp->if_softc;
    713  1.1  jmcneill 	int error;
    714  1.1  jmcneill 
    715  1.1  jmcneill 	switch (cmd) {
    716  1.1  jmcneill 	default:
    717  1.1  jmcneill 		if ((error = ether_ioctl(ifp, cmd, data)) != ENETRESET)
    718  1.1  jmcneill 			break;
    719  1.1  jmcneill 		error = 0;
    720  1.1  jmcneill 		if (cmd != SIOCADDMULTI && cmd != SIOCDELMULTI)
    721  1.1  jmcneill 			break;
    722  1.1  jmcneill 		if (ifp->if_flags & IFF_RUNNING) {
    723  1.1  jmcneill 			/*
    724  1.1  jmcneill 			 * Multicast list has changed; set the hardware filter
    725  1.1  jmcneill 			 * accordingly.
    726  1.1  jmcneill 			 */
    727  1.1  jmcneill 			mutex_enter(&sc->sc_intr_lock);
    728  1.1  jmcneill 			sun4i_emac_ifstop(ifp, 0);
    729  1.1  jmcneill 			error = sun4i_emac_ifinit(ifp);
    730  1.1  jmcneill 			mutex_exit(&sc->sc_intr_lock);
    731  1.1  jmcneill 		}
    732  1.1  jmcneill 		break;
    733  1.1  jmcneill 	}
    734  1.1  jmcneill 
    735  1.1  jmcneill 	return error;
    736  1.1  jmcneill }
    737  1.1  jmcneill 
    738  1.1  jmcneill static void
    739  1.1  jmcneill sun4i_emac_ifstop(struct ifnet *ifp, int discard)
    740  1.1  jmcneill {
    741  1.1  jmcneill 	struct sun4i_emac_softc * const sc = ifp->if_softc;
    742  1.1  jmcneill 	struct mii_data * const mii = &sc->sc_mii;
    743  1.1  jmcneill 
    744  1.1  jmcneill 	KASSERT(mutex_owned(&sc->sc_intr_lock));
    745  1.1  jmcneill 
    746  1.1  jmcneill 	callout_stop(&sc->sc_stat_ch);
    747  1.1  jmcneill 	mii_down(mii);
    748  1.1  jmcneill 
    749  1.1  jmcneill 	sun4i_emac_write(sc, EMAC_INT_CTL_REG, 0);
    750  1.1  jmcneill 	sun4i_emac_write(sc, EMAC_INT_STA_REG,
    751  1.1  jmcneill 	    sun4i_emac_read(sc, EMAC_INT_STA_REG));
    752  1.1  jmcneill 
    753  1.1  jmcneill 	sun4i_emac_clear_set(sc, EMAC_CTL_REG,
    754  1.1  jmcneill 	    EMAC_CTL_RST | EMAC_CTL_TX_EN | EMAC_CTL_RX_EN, 0);
    755  1.1  jmcneill 
    756  1.1  jmcneill 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
    757  1.1  jmcneill 	ifp->if_timer = 0;
    758  1.1  jmcneill }
    759  1.1  jmcneill 
    760  1.1  jmcneill int
    761  1.1  jmcneill sun4i_emac_ifinit(struct ifnet *ifp)
    762  1.1  jmcneill {
    763  1.1  jmcneill 	struct sun4i_emac_softc * const sc = ifp->if_softc;
    764  1.1  jmcneill 	struct mii_data * const mii = &sc->sc_mii;
    765  1.1  jmcneill 
    766  1.1  jmcneill 	sun4i_emac_clear_set(sc, EMAC_RX_CTL_REG,
    767  1.1  jmcneill 	    0, EMAC_RX_CTL_FIFO_RESET);
    768  1.1  jmcneill 
    769  1.1  jmcneill 	delay(1);
    770  1.1  jmcneill 
    771  1.1  jmcneill 	sun4i_emac_clear_set(sc, EMAC_MAC_CTL0_REG,
    772  1.1  jmcneill 	    EMAC_MAC_CTL0_SOFT_RESET, 0);
    773  1.1  jmcneill 
    774  1.1  jmcneill 	sun4i_emac_clear_set(sc, EMAC_MAC_MCFG_REG,
    775  1.1  jmcneill 	    EMAC_MAC_MCFG_CLK, __SHIFTIN(0xd, EMAC_MAC_MCFG_CLK));
    776  1.1  jmcneill 
    777  1.1  jmcneill 	sun4i_emac_write(sc, EMAC_RX_FBC_REG, 0);
    778  1.1  jmcneill 
    779  1.1  jmcneill 	sun4i_emac_write(sc, EMAC_INT_CTL_REG, 0);
    780  1.1  jmcneill 	sun4i_emac_write(sc, EMAC_INT_STA_REG,
    781  1.1  jmcneill 	    sun4i_emac_read(sc, EMAC_INT_STA_REG));
    782  1.1  jmcneill 
    783  1.1  jmcneill 	delay(1);
    784  1.1  jmcneill 
    785  1.1  jmcneill 	sun4i_emac_clear_set(sc, EMAC_TX_MODE_REG,
    786  1.1  jmcneill 	    EMAC_TX_MODE_DMA, EMAC_TX_MODE_ABF_ENA);
    787  1.1  jmcneill 
    788  1.1  jmcneill 	sun4i_emac_clear_set(sc, EMAC_MAC_CTL0_REG,
    789  1.1  jmcneill 	    0, EMAC_MAC_CTL0_TFC | EMAC_MAC_CTL0_RFC);
    790  1.1  jmcneill 
    791  1.1  jmcneill 	sun4i_emac_clear_set(sc, EMAC_RX_CTL_REG,
    792  1.1  jmcneill 	    EMAC_RX_CTL_DMA, 0);
    793  1.1  jmcneill 
    794  1.1  jmcneill 	sun4i_emac_clear_set(sc, EMAC_MAC_CTL1_REG,
    795  1.1  jmcneill 	    0,
    796  1.1  jmcneill 	    EMAC_MAC_CTL1_FLC | EMAC_MAC_CTL1_CRC |
    797  1.1  jmcneill 	    EMAC_MAC_CTL1_PC);
    798  1.1  jmcneill 
    799  1.1  jmcneill 	sun4i_emac_write(sc, EMAC_MAC_IPGT_REG, EMAC_MAC_IPGT_FD);
    800  1.1  jmcneill 	sun4i_emac_write(sc, EMAC_MAC_IPGR_REG,
    801  1.1  jmcneill 	    __SHIFTIN(0x0c, EMAC_MAC_IPGR_IPG1) |
    802  1.1  jmcneill 	    __SHIFTIN(0x12, EMAC_MAC_IPGR_IPG2));
    803  1.1  jmcneill 
    804  1.1  jmcneill 	sun4i_emac_write(sc, EMAC_MAC_CLRT_REG,
    805  1.1  jmcneill 	    __SHIFTIN(0x0f, EMAC_MAC_CLRT_RM) |
    806  1.1  jmcneill 	    __SHIFTIN(0x37, EMAC_MAC_CLRT_CW));
    807  1.1  jmcneill 
    808  1.1  jmcneill 	sun4i_emac_write(sc, EMAC_MAC_MAXF_REG, 0x600);
    809  1.1  jmcneill 
    810  1.1  jmcneill 	sun4i_emac_rx_hash(sc);
    811  1.1  jmcneill 
    812  1.1  jmcneill 	sun4i_emac_int_enable(sc);
    813  1.1  jmcneill 
    814  1.1  jmcneill 	ifp->if_flags |= IFF_RUNNING;
    815  1.1  jmcneill 	ifp->if_flags &= ~IFF_OACTIVE;
    816  1.1  jmcneill 
    817  1.1  jmcneill 	/* Enable RX/TX */
    818  1.1  jmcneill 	sun4i_emac_clear_set(sc, EMAC_CTL_REG,
    819  1.1  jmcneill 	    0, EMAC_CTL_RST | EMAC_CTL_TX_EN | EMAC_CTL_RX_EN);
    820  1.1  jmcneill 
    821  1.1  jmcneill 	mii_mediachg(mii);
    822  1.1  jmcneill 	callout_schedule(&sc->sc_stat_ch, hz);
    823  1.1  jmcneill 
    824  1.1  jmcneill 	return 0;
    825  1.1  jmcneill }
    826  1.1  jmcneill 
    827  1.1  jmcneill static void
    828  1.1  jmcneill sun4i_emac_ifwatchdog(struct ifnet *ifp)
    829  1.1  jmcneill {
    830  1.1  jmcneill 	struct sun4i_emac_softc * const sc = ifp->if_softc;
    831  1.1  jmcneill 
    832  1.1  jmcneill 	device_printf(sc->sc_dev, "device timeout\n");
    833  1.1  jmcneill 
    834  1.1  jmcneill 	ifp->if_oerrors++;
    835  1.1  jmcneill 	sun4i_emac_ifinit(ifp);
    836  1.1  jmcneill 	sun4i_emac_ifstart(ifp);
    837  1.1  jmcneill }
    838  1.1  jmcneill 
    839  1.1  jmcneill static void
    840  1.1  jmcneill sun4i_emac_rx_hash(struct sun4i_emac_softc *sc)
    841  1.1  jmcneill {
    842  1.1  jmcneill 	struct ifnet * const ifp = &sc->sc_ec.ec_if;
    843  1.1  jmcneill 	struct ether_multistep step;
    844  1.1  jmcneill 	struct ether_multi *enm;
    845  1.1  jmcneill 	uint32_t hash[2];
    846  1.1  jmcneill 	uint32_t rxctl;
    847  1.1  jmcneill 
    848  1.1  jmcneill 	rxctl = sun4i_emac_read(sc, EMAC_RX_CTL_REG);
    849  1.1  jmcneill 	rxctl &= ~EMAC_RX_CTL_MHF;
    850  1.1  jmcneill 	rxctl |= EMAC_RX_CTL_UCAD;
    851  1.1  jmcneill 	rxctl |= EMAC_RX_CTL_DAF;
    852  1.1  jmcneill 	rxctl |= EMAC_RX_CTL_MC0;
    853  1.1  jmcneill 	rxctl |= EMAC_RX_CTL_BC0;
    854  1.1  jmcneill 	rxctl |= EMAC_RX_CTL_POR;
    855  1.1  jmcneill 
    856  1.1  jmcneill 	hash[0] = hash[1] = ~0;
    857  1.1  jmcneill 	if (ifp->if_flags & IFF_PROMISC) {
    858  1.1  jmcneill 		ifp->if_flags |= IFF_ALLMULTI;
    859  1.1  jmcneill 		rxctl |= EMAC_RX_CTL_PROMISC;
    860  1.1  jmcneill 	} else {
    861  1.1  jmcneill 		rxctl &= ~EMAC_RX_CTL_PROMISC;
    862  1.1  jmcneill 	}
    863  1.1  jmcneill 
    864  1.1  jmcneill 	if ((ifp->if_flags & IFF_PROMISC) == 0) {
    865  1.1  jmcneill 		hash[0] = hash[1] = 0;
    866  1.1  jmcneill 
    867  1.8     ozaki 		ETHER_LOCK(&sc->sc_ec);
    868  1.1  jmcneill 		ETHER_FIRST_MULTI(step, &sc->sc_ec, enm);
    869  1.1  jmcneill 		while (enm != NULL) {
    870  1.9   msaitoh 			if (memcmp(enm->enm_addrlo, enm->enm_addrhi,
    871  1.9   msaitoh 			    ETHER_ADDR_LEN)) {
    872  1.8     ozaki 				ETHER_UNLOCK(&sc->sc_ec);
    873  1.1  jmcneill 				/*
    874  1.9   msaitoh 				 * We must listen to a range of multicast
    875  1.9   msaitoh 				 * addresses. For now, just accept all
    876  1.9   msaitoh 				 * multicasts, rather than trying to set only
    877  1.9   msaitoh 				 * those filter bits needed to match the range.
    878  1.9   msaitoh 				 * (At this time, the only use of address
    879  1.9   msaitoh 				 * ranges is for IP multicast routing, for
    880  1.9   msaitoh 				 * which the range is big enough to require all
    881  1.9   msaitoh 				 * bits set.)
    882  1.9   msaitoh 				 */
    883  1.1  jmcneill 				hash[0] = hash[1] = ~0;
    884  1.1  jmcneill 				ifp->if_flags |= IFF_ALLMULTI;
    885  1.1  jmcneill 				goto done;
    886  1.1  jmcneill                 	}
    887  1.1  jmcneill 
    888  1.9   msaitoh 			u_int crc = ether_crc32_be(enm->enm_addrlo,
    889  1.9   msaitoh 			    ETHER_ADDR_LEN);
    890  1.1  jmcneill 
    891  1.1  jmcneill 			/* Just want the 6 most significant bits. */
    892  1.9   msaitoh 			crc >>= 26;
    893  1.1  jmcneill 
    894  1.1  jmcneill 			/* Set the corresponding bit in the filter. */
    895  1.1  jmcneill 			hash[crc >> 5] |= __BIT(crc & 31);
    896  1.1  jmcneill                 	ETHER_NEXT_MULTI(step, enm);
    897  1.1  jmcneill 		}
    898  1.8     ozaki 		ETHER_UNLOCK(&sc->sc_ec);
    899  1.1  jmcneill 		ifp->if_flags &= ~IFF_ALLMULTI;
    900  1.1  jmcneill 		rxctl |= EMAC_RX_CTL_MHF;
    901  1.1  jmcneill 	}
    902  1.1  jmcneill 
    903  1.1  jmcneill done:
    904  1.1  jmcneill 
    905  1.1  jmcneill 	sun4i_emac_write(sc, EMAC_RX_HASH0_REG, hash[0]);
    906  1.1  jmcneill 	sun4i_emac_write(sc, EMAC_RX_HASH1_REG, hash[1]);
    907  1.1  jmcneill 
    908  1.1  jmcneill 	sun4i_emac_write(sc, EMAC_RX_CTL_REG, rxctl);
    909  1.1  jmcneill }
    910