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