sun4i_emac.c revision 1.4 1 1.4 msaitoh /* $NetBSD: sun4i_emac.c,v 1.4 2018/06/26 06:47:58 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.4 msaitoh __KERNEL_RCSID(1, "$NetBSD: sun4i_emac.c,v 1.4 2018/06/26 06:47:58 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.1 jmcneill (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.1 jmcneill static int sun4i_emac_miibus_read_reg(device_t, int, int);
173 1.1 jmcneill static void sun4i_emac_miibus_write_reg(device_t, int, int, int);
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.1 jmcneill 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.1 jmcneill if (LIST_EMPTY(&mii->mii_phys)) {
362 1.1 jmcneill aprint_error_dev(self, "no PHY found!\n");
363 1.1 jmcneill ifmedia_add(&mii->mii_media, IFM_ETHER|IFM_MANUAL, 0, NULL);
364 1.1 jmcneill ifmedia_set(&mii->mii_media, IFM_ETHER|IFM_MANUAL);
365 1.1 jmcneill } else {
366 1.1 jmcneill ifmedia_set(&mii->mii_media, IFM_ETHER|IFM_AUTO);
367 1.1 jmcneill }
368 1.1 jmcneill
369 1.1 jmcneill /*
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.1 jmcneill 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.1 jmcneill sun4i_emac_miibus_read_reg(device_t self, int phy, int reg)
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.1 jmcneill
394 1.1 jmcneill sun4i_emac_write(sc, EMAC_MAC_MADR_REG, (phy << 8) | reg);
395 1.1 jmcneill sun4i_emac_write(sc, EMAC_MAC_MCMD_REG, 1);
396 1.1 jmcneill
397 1.1 jmcneill while (--retry > 0 && (sun4i_emac_read(sc, EMAC_MAC_MIND_REG) & 1) != 0)
398 1.1 jmcneill delay(1000);
399 1.1 jmcneill if (retry == 0)
400 1.1 jmcneill device_printf(self, "PHY read timeout\n");
401 1.1 jmcneill
402 1.1 jmcneill sun4i_emac_write(sc, EMAC_MAC_MCMD_REG, 0);
403 1.1 jmcneill const uint32_t rv = sun4i_emac_read(sc, EMAC_MAC_MRDD_REG);
404 1.1 jmcneill
405 1.1 jmcneill return rv;
406 1.1 jmcneill }
407 1.1 jmcneill
408 1.1 jmcneill void
409 1.1 jmcneill sun4i_emac_miibus_write_reg(device_t self, int phy, int reg, int val)
410 1.1 jmcneill {
411 1.1 jmcneill struct sun4i_emac_softc * const sc = device_private(self);
412 1.1 jmcneill int retry = 100;
413 1.1 jmcneill
414 1.1 jmcneill sun4i_emac_write(sc, EMAC_MAC_MADR_REG, (phy << 8) | reg);
415 1.1 jmcneill sun4i_emac_write(sc, EMAC_MAC_MCMD_REG, 1);
416 1.1 jmcneill
417 1.1 jmcneill while (--retry > 0 && (sun4i_emac_read(sc, EMAC_MAC_MIND_REG) & 1) != 0)
418 1.1 jmcneill delay(1000);
419 1.1 jmcneill if (retry == 0)
420 1.1 jmcneill device_printf(self, "PHY write timeout\n");
421 1.1 jmcneill
422 1.1 jmcneill sun4i_emac_write(sc, EMAC_MAC_MCMD_REG, 0);
423 1.1 jmcneill sun4i_emac_write(sc, EMAC_MAC_MWTD_REG, val);
424 1.1 jmcneill }
425 1.1 jmcneill
426 1.1 jmcneill void
427 1.1 jmcneill sun4i_emac_miibus_statchg(struct ifnet *ifp)
428 1.1 jmcneill {
429 1.1 jmcneill struct sun4i_emac_softc * const sc = ifp->if_softc;
430 1.1 jmcneill struct mii_data * const mii = &sc->sc_mii;
431 1.1 jmcneill const u_int media = mii->mii_media_active;
432 1.1 jmcneill
433 1.1 jmcneill /*
434 1.1 jmcneill * Set MII interface based on the speed
435 1.1 jmcneill * negotiated by the PHY.
436 1.1 jmcneill */
437 1.1 jmcneill switch (IFM_SUBTYPE(media)) {
438 1.1 jmcneill case IFM_10_T:
439 1.1 jmcneill sun4i_emac_clear_set(sc, EMAC_MAC_SUPP_REG,
440 1.1 jmcneill EMAC_MAC_SUPP_100M, 0);
441 1.1 jmcneill break;
442 1.1 jmcneill case IFM_100_TX:
443 1.1 jmcneill sun4i_emac_clear_set(sc, EMAC_MAC_SUPP_REG,
444 1.1 jmcneill 0, EMAC_MAC_SUPP_100M);
445 1.1 jmcneill break;
446 1.1 jmcneill }
447 1.1 jmcneill
448 1.1 jmcneill const bool link = (IFM_SUBTYPE(media) & (IFM_10_T|IFM_100_TX)) != 0;
449 1.1 jmcneill if (link) {
450 1.1 jmcneill if (media & IFM_FDX) {
451 1.1 jmcneill sun4i_emac_clear_set(sc, EMAC_MAC_CTL1_REG,
452 1.1 jmcneill 0, EMAC_MAC_CTL1_FD);
453 1.1 jmcneill } else {
454 1.1 jmcneill sun4i_emac_clear_set(sc, EMAC_MAC_CTL1_REG,
455 1.1 jmcneill EMAC_MAC_CTL1_FD, 0);
456 1.1 jmcneill }
457 1.1 jmcneill }
458 1.1 jmcneill }
459 1.1 jmcneill
460 1.1 jmcneill static void
461 1.1 jmcneill sun4i_emac_tick(void *softc)
462 1.1 jmcneill {
463 1.1 jmcneill struct sun4i_emac_softc * const sc = softc;
464 1.1 jmcneill struct mii_data * const mii = &sc->sc_mii;
465 1.1 jmcneill int s;
466 1.1 jmcneill
467 1.1 jmcneill s = splnet();
468 1.1 jmcneill mii_tick(mii);
469 1.1 jmcneill callout_schedule(&sc->sc_stat_ch, hz);
470 1.1 jmcneill splx(s);
471 1.1 jmcneill }
472 1.1 jmcneill
473 1.1 jmcneill static inline void
474 1.1 jmcneill sun4i_emac_rxfifo_flush(struct sun4i_emac_softc *sc)
475 1.1 jmcneill {
476 1.1 jmcneill sun4i_emac_clear_set(sc, EMAC_CTL_REG, EMAC_CTL_RX_EN, 0);
477 1.1 jmcneill
478 1.1 jmcneill sun4i_emac_clear_set(sc, EMAC_RX_CTL_REG, 0, EMAC_RX_CTL_FIFO_RESET);
479 1.1 jmcneill
480 1.1 jmcneill for (;;) {
481 1.1 jmcneill uint32_t v0 = sun4i_emac_read(sc, EMAC_RX_CTL_REG);
482 1.1 jmcneill if ((v0 & EMAC_RX_CTL_FIFO_RESET) == 0)
483 1.1 jmcneill break;
484 1.1 jmcneill }
485 1.1 jmcneill
486 1.1 jmcneill sun4i_emac_clear_set(sc, EMAC_CTL_REG, 0, EMAC_CTL_RX_EN);
487 1.1 jmcneill }
488 1.1 jmcneill
489 1.1 jmcneill static void
490 1.1 jmcneill sun4i_emac_rxfifo_consume(struct sun4i_emac_softc *sc, size_t len)
491 1.1 jmcneill {
492 1.1 jmcneill for (len = (len + 3) >> 2; len > 0; len--) {
493 1.1 jmcneill (void) sun4i_emac_read(sc, EMAC_RX_IO_DATA_REG);
494 1.1 jmcneill }
495 1.1 jmcneill }
496 1.1 jmcneill
497 1.1 jmcneill static void
498 1.1 jmcneill sun4i_emac_rxfifo_transfer(struct sun4i_emac_softc *sc, struct mbuf *m)
499 1.1 jmcneill {
500 1.1 jmcneill uint32_t *dp32 = mtod(m, uint32_t *);
501 1.1 jmcneill const int len = roundup2(m->m_len, 4);
502 1.1 jmcneill
503 1.1 jmcneill bus_space_read_multi_4(sc->sc_bst, sc->sc_bsh,
504 1.1 jmcneill EMAC_RX_IO_DATA_REG, dp32, len / 4);
505 1.1 jmcneill }
506 1.1 jmcneill
507 1.1 jmcneill static struct mbuf *
508 1.1 jmcneill sun4i_emac_mgethdr(struct sun4i_emac_softc *sc, size_t rxlen)
509 1.1 jmcneill {
510 1.1 jmcneill struct mbuf *m = m_gethdr(M_DONTWAIT, MT_DATA);
511 1.1 jmcneill
512 1.3 maxv if (m == NULL) {
513 1.3 maxv return NULL;
514 1.3 maxv }
515 1.1 jmcneill if (rxlen + 2 > MHLEN) {
516 1.1 jmcneill MCLGET(m, M_DONTWAIT);
517 1.1 jmcneill if ((m->m_flags & M_EXT) == 0) {
518 1.1 jmcneill m_free(m);
519 1.1 jmcneill return NULL;
520 1.1 jmcneill }
521 1.1 jmcneill }
522 1.1 jmcneill
523 1.1 jmcneill m_adj(m, 2);
524 1.1 jmcneill m->m_len = rxlen;
525 1.1 jmcneill m->m_pkthdr.len = rxlen;
526 1.1 jmcneill m_set_rcvif(m, &sc->sc_ec.ec_if);
527 1.1 jmcneill m->m_flags |= M_HASFCS;
528 1.1 jmcneill
529 1.1 jmcneill return m;
530 1.1 jmcneill }
531 1.1 jmcneill
532 1.1 jmcneill static void
533 1.1 jmcneill sun4i_emac_if_input(struct sun4i_emac_softc *sc, struct mbuf *m)
534 1.1 jmcneill {
535 1.1 jmcneill struct ifnet * const ifp = &sc->sc_ec.ec_if;
536 1.1 jmcneill
537 1.1 jmcneill if_percpuq_enqueue(ifp->if_percpuq, m);
538 1.1 jmcneill }
539 1.1 jmcneill
540 1.1 jmcneill static void
541 1.1 jmcneill sun4i_emac_rx_intr(struct sun4i_emac_softc *sc)
542 1.1 jmcneill {
543 1.1 jmcneill for (;;) {
544 1.1 jmcneill uint32_t rx_count = sun4i_emac_read(sc, EMAC_RX_FBC_REG);
545 1.1 jmcneill struct mbuf *m;
546 1.1 jmcneill
547 1.1 jmcneill if (rx_count == 0) {
548 1.1 jmcneill rx_count = sun4i_emac_read(sc, EMAC_RX_FBC_REG);
549 1.1 jmcneill if (rx_count == 0)
550 1.1 jmcneill return;
551 1.1 jmcneill }
552 1.1 jmcneill
553 1.1 jmcneill uint32_t v = sun4i_emac_read(sc, EMAC_RX_IO_DATA_REG);
554 1.1 jmcneill if (v != EMAC_RX_MAGIC) {
555 1.1 jmcneill sun4i_emac_rxfifo_flush(sc);
556 1.1 jmcneill return;
557 1.1 jmcneill }
558 1.1 jmcneill
559 1.1 jmcneill uint32_t rxhdr = sun4i_emac_read(sc, EMAC_RX_IO_DATA_REG);
560 1.1 jmcneill uint32_t rxlen = __SHIFTOUT(rxhdr, EMAC_RXHDR_LEN);
561 1.1 jmcneill uint32_t rxsts = __SHIFTOUT(rxhdr, EMAC_RXHDR_STS);
562 1.1 jmcneill
563 1.1 jmcneill if (rxlen < ETHER_MIN_LEN || (rxsts & EMAC_RX_STA_PKTOK) == 0) {
564 1.1 jmcneill sc->sc_ec.ec_if.if_ierrors++;
565 1.1 jmcneill continue;
566 1.1 jmcneill }
567 1.1 jmcneill
568 1.1 jmcneill m = sun4i_emac_mgethdr(sc, rxlen);
569 1.1 jmcneill if (m == NULL) {
570 1.1 jmcneill sc->sc_ec.ec_if.if_ierrors++;
571 1.1 jmcneill sun4i_emac_rxfifo_consume(sc, rxlen);
572 1.1 jmcneill return;
573 1.1 jmcneill }
574 1.1 jmcneill
575 1.1 jmcneill sun4i_emac_rxfifo_transfer(sc, m);
576 1.1 jmcneill sun4i_emac_if_input(sc, m);
577 1.1 jmcneill }
578 1.1 jmcneill }
579 1.1 jmcneill
580 1.1 jmcneill static int
581 1.1 jmcneill sun4i_emac_txfifo_transfer(struct sun4i_emac_softc *sc, struct mbuf *m, u_int slot)
582 1.1 jmcneill {
583 1.1 jmcneill bus_size_t const io_data_reg = EMAC_TX_IO_DATA_REG(0);
584 1.1 jmcneill const int len = m->m_pkthdr.len;
585 1.1 jmcneill uint32_t *pktdata;
586 1.1 jmcneill
587 1.1 jmcneill KASSERT(len > 0 && len <= sizeof(sc->sc_txbuf));
588 1.1 jmcneill
589 1.1 jmcneill if (m->m_next != NULL) {
590 1.1 jmcneill m_copydata(m, 0, len, sc->sc_txbuf);
591 1.1 jmcneill pktdata = sc->sc_txbuf;
592 1.1 jmcneill } else {
593 1.1 jmcneill pktdata = mtod(m, uint32_t *);
594 1.1 jmcneill }
595 1.1 jmcneill
596 1.1 jmcneill bus_space_write_multi_4(sc->sc_bst, sc->sc_bsh, io_data_reg,
597 1.1 jmcneill pktdata, roundup2(len, 4) / 4);
598 1.1 jmcneill
599 1.1 jmcneill return len;
600 1.1 jmcneill }
601 1.1 jmcneill
602 1.1 jmcneill static void
603 1.1 jmcneill sun4i_emac_tx_enqueue(struct sun4i_emac_softc *sc, struct mbuf *m, u_int slot)
604 1.1 jmcneill {
605 1.1 jmcneill struct ifnet * const ifp = &sc->sc_ec.ec_if;
606 1.1 jmcneill
607 1.1 jmcneill sun4i_emac_write(sc, EMAC_TX_INS_REG, slot);
608 1.1 jmcneill
609 1.1 jmcneill const int len = sun4i_emac_txfifo_transfer(sc, m, slot);
610 1.1 jmcneill
611 1.1 jmcneill bus_size_t const pl_reg = EMAC_TX_PL_REG(slot);
612 1.1 jmcneill bus_size_t const ctl_reg = EMAC_TX_CTL_REG(slot);
613 1.1 jmcneill
614 1.1 jmcneill sun4i_emac_write(sc, pl_reg, len);
615 1.1 jmcneill sun4i_emac_clear_set(sc, ctl_reg, 0, EMAC_TX_CTL_START);
616 1.1 jmcneill
617 1.4 msaitoh bpf_mtap(ifp, m, BPF_D_OUT);
618 1.1 jmcneill
619 1.1 jmcneill m_freem(m);
620 1.1 jmcneill }
621 1.1 jmcneill
622 1.1 jmcneill static void
623 1.1 jmcneill sun4i_emac_tx_intr(struct sun4i_emac_softc *sc, u_int slot)
624 1.1 jmcneill {
625 1.1 jmcneill struct ifnet * const ifp = &sc->sc_ec.ec_if;
626 1.1 jmcneill
627 1.1 jmcneill sc->sc_tx_active &= ~__BIT(slot);
628 1.1 jmcneill ifp->if_flags &= ~IFF_OACTIVE;
629 1.1 jmcneill }
630 1.1 jmcneill
631 1.1 jmcneill int
632 1.1 jmcneill sun4i_emac_intr(void *arg)
633 1.1 jmcneill {
634 1.1 jmcneill struct sun4i_emac_softc * const sc = arg;
635 1.1 jmcneill struct ifnet * const ifp = &sc->sc_ec.ec_if;
636 1.1 jmcneill
637 1.1 jmcneill mutex_enter(&sc->sc_intr_lock);
638 1.1 jmcneill
639 1.1 jmcneill uint32_t sts = sun4i_emac_read(sc, EMAC_INT_STA_REG);
640 1.1 jmcneill sun4i_emac_write(sc, EMAC_INT_STA_REG, sts);
641 1.1 jmcneill rnd_add_uint32(&sc->sc_rnd_source, sts);
642 1.1 jmcneill
643 1.1 jmcneill if (sts & EMAC_INT_RX) {
644 1.1 jmcneill sun4i_emac_rx_intr(sc);
645 1.1 jmcneill }
646 1.1 jmcneill if (sts & EMAC_INT_TX0) {
647 1.1 jmcneill sun4i_emac_tx_intr(sc, 0);
648 1.1 jmcneill }
649 1.1 jmcneill if (sts & EMAC_INT_TX1) {
650 1.1 jmcneill sun4i_emac_tx_intr(sc, 1);
651 1.1 jmcneill }
652 1.1 jmcneill if (sts & (EMAC_INT_TX0|EMAC_INT_TX1)) {
653 1.1 jmcneill if (sc->sc_tx_active == 0)
654 1.1 jmcneill ifp->if_timer = 0;
655 1.1 jmcneill if_schedule_deferred_start(ifp);
656 1.1 jmcneill }
657 1.1 jmcneill
658 1.1 jmcneill mutex_exit(&sc->sc_intr_lock);
659 1.1 jmcneill
660 1.1 jmcneill return 1;
661 1.1 jmcneill }
662 1.1 jmcneill
663 1.1 jmcneill void
664 1.1 jmcneill sun4i_emac_ifstart(struct ifnet *ifp)
665 1.1 jmcneill {
666 1.1 jmcneill struct sun4i_emac_softc * const sc = ifp->if_softc;
667 1.1 jmcneill
668 1.1 jmcneill mutex_enter(&sc->sc_intr_lock);
669 1.1 jmcneill
670 1.1 jmcneill if ((sc->sc_tx_active & 1) == 0) {
671 1.1 jmcneill struct mbuf *m;
672 1.1 jmcneill IFQ_DEQUEUE(&ifp->if_snd, m);
673 1.1 jmcneill if (m == NULL) {
674 1.1 jmcneill mutex_exit(&sc->sc_intr_lock);
675 1.1 jmcneill return;
676 1.1 jmcneill }
677 1.1 jmcneill sun4i_emac_tx_enqueue(sc, m, 0);
678 1.1 jmcneill sc->sc_tx_active |= 1;
679 1.1 jmcneill }
680 1.1 jmcneill
681 1.1 jmcneill if ((sc->sc_tx_active & 2) == 0) {
682 1.1 jmcneill struct mbuf *m;
683 1.1 jmcneill IFQ_DEQUEUE(&ifp->if_snd, m);
684 1.1 jmcneill if (m == NULL) {
685 1.1 jmcneill mutex_exit(&sc->sc_intr_lock);
686 1.1 jmcneill return;
687 1.1 jmcneill }
688 1.1 jmcneill sun4i_emac_tx_enqueue(sc, m, 1);
689 1.1 jmcneill sc->sc_tx_active |= 2;
690 1.1 jmcneill }
691 1.1 jmcneill
692 1.1 jmcneill if (sc->sc_tx_active == 3)
693 1.1 jmcneill ifp->if_flags |= IFF_OACTIVE;
694 1.1 jmcneill
695 1.1 jmcneill ifp->if_timer = 5;
696 1.1 jmcneill
697 1.1 jmcneill mutex_exit(&sc->sc_intr_lock);
698 1.1 jmcneill }
699 1.1 jmcneill
700 1.1 jmcneill
701 1.1 jmcneill static int
702 1.1 jmcneill sun4i_emac_ifioctl(struct ifnet *ifp, u_long cmd, void *data)
703 1.1 jmcneill {
704 1.1 jmcneill struct sun4i_emac_softc * const sc = ifp->if_softc;
705 1.1 jmcneill struct ifreq *ifr = (struct ifreq *)data;
706 1.1 jmcneill int error;
707 1.1 jmcneill
708 1.1 jmcneill switch (cmd) {
709 1.1 jmcneill case SIOCGIFMEDIA:
710 1.1 jmcneill case SIOCSIFMEDIA:
711 1.1 jmcneill error = ifmedia_ioctl(ifp, ifr, &sc->sc_mii.mii_media, cmd);
712 1.1 jmcneill break;
713 1.1 jmcneill default:
714 1.1 jmcneill if ((error = ether_ioctl(ifp, cmd, data)) != ENETRESET)
715 1.1 jmcneill break;
716 1.1 jmcneill error = 0;
717 1.1 jmcneill if (cmd != SIOCADDMULTI && cmd != SIOCDELMULTI)
718 1.1 jmcneill break;
719 1.1 jmcneill if (ifp->if_flags & IFF_RUNNING) {
720 1.1 jmcneill /*
721 1.1 jmcneill * Multicast list has changed; set the hardware filter
722 1.1 jmcneill * accordingly.
723 1.1 jmcneill */
724 1.1 jmcneill mutex_enter(&sc->sc_intr_lock);
725 1.1 jmcneill sun4i_emac_ifstop(ifp, 0);
726 1.1 jmcneill error = sun4i_emac_ifinit(ifp);
727 1.1 jmcneill mutex_exit(&sc->sc_intr_lock);
728 1.1 jmcneill }
729 1.1 jmcneill break;
730 1.1 jmcneill }
731 1.1 jmcneill
732 1.1 jmcneill return error;
733 1.1 jmcneill }
734 1.1 jmcneill
735 1.1 jmcneill static void
736 1.1 jmcneill sun4i_emac_ifstop(struct ifnet *ifp, int discard)
737 1.1 jmcneill {
738 1.1 jmcneill struct sun4i_emac_softc * const sc = ifp->if_softc;
739 1.1 jmcneill struct mii_data * const mii = &sc->sc_mii;
740 1.1 jmcneill
741 1.1 jmcneill KASSERT(mutex_owned(&sc->sc_intr_lock));
742 1.1 jmcneill
743 1.1 jmcneill callout_stop(&sc->sc_stat_ch);
744 1.1 jmcneill mii_down(mii);
745 1.1 jmcneill
746 1.1 jmcneill sun4i_emac_write(sc, EMAC_INT_CTL_REG, 0);
747 1.1 jmcneill sun4i_emac_write(sc, EMAC_INT_STA_REG,
748 1.1 jmcneill sun4i_emac_read(sc, EMAC_INT_STA_REG));
749 1.1 jmcneill
750 1.1 jmcneill sun4i_emac_clear_set(sc, EMAC_CTL_REG,
751 1.1 jmcneill EMAC_CTL_RST | EMAC_CTL_TX_EN | EMAC_CTL_RX_EN, 0);
752 1.1 jmcneill
753 1.1 jmcneill ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
754 1.1 jmcneill ifp->if_timer = 0;
755 1.1 jmcneill }
756 1.1 jmcneill
757 1.1 jmcneill int
758 1.1 jmcneill sun4i_emac_ifinit(struct ifnet *ifp)
759 1.1 jmcneill {
760 1.1 jmcneill struct sun4i_emac_softc * const sc = ifp->if_softc;
761 1.1 jmcneill struct mii_data * const mii = &sc->sc_mii;
762 1.1 jmcneill
763 1.1 jmcneill sun4i_emac_clear_set(sc, EMAC_RX_CTL_REG,
764 1.1 jmcneill 0, EMAC_RX_CTL_FIFO_RESET);
765 1.1 jmcneill
766 1.1 jmcneill delay(1);
767 1.1 jmcneill
768 1.1 jmcneill sun4i_emac_clear_set(sc, EMAC_MAC_CTL0_REG,
769 1.1 jmcneill EMAC_MAC_CTL0_SOFT_RESET, 0);
770 1.1 jmcneill
771 1.1 jmcneill sun4i_emac_clear_set(sc, EMAC_MAC_MCFG_REG,
772 1.1 jmcneill EMAC_MAC_MCFG_CLK, __SHIFTIN(0xd, EMAC_MAC_MCFG_CLK));
773 1.1 jmcneill
774 1.1 jmcneill sun4i_emac_write(sc, EMAC_RX_FBC_REG, 0);
775 1.1 jmcneill
776 1.1 jmcneill sun4i_emac_write(sc, EMAC_INT_CTL_REG, 0);
777 1.1 jmcneill sun4i_emac_write(sc, EMAC_INT_STA_REG,
778 1.1 jmcneill sun4i_emac_read(sc, EMAC_INT_STA_REG));
779 1.1 jmcneill
780 1.1 jmcneill delay(1);
781 1.1 jmcneill
782 1.1 jmcneill sun4i_emac_clear_set(sc, EMAC_TX_MODE_REG,
783 1.1 jmcneill EMAC_TX_MODE_DMA, EMAC_TX_MODE_ABF_ENA);
784 1.1 jmcneill
785 1.1 jmcneill sun4i_emac_clear_set(sc, EMAC_MAC_CTL0_REG,
786 1.1 jmcneill 0, EMAC_MAC_CTL0_TFC | EMAC_MAC_CTL0_RFC);
787 1.1 jmcneill
788 1.1 jmcneill sun4i_emac_clear_set(sc, EMAC_RX_CTL_REG,
789 1.1 jmcneill EMAC_RX_CTL_DMA, 0);
790 1.1 jmcneill
791 1.1 jmcneill sun4i_emac_clear_set(sc, EMAC_MAC_CTL1_REG,
792 1.1 jmcneill 0,
793 1.1 jmcneill EMAC_MAC_CTL1_FLC | EMAC_MAC_CTL1_CRC |
794 1.1 jmcneill EMAC_MAC_CTL1_PC);
795 1.1 jmcneill
796 1.1 jmcneill sun4i_emac_write(sc, EMAC_MAC_IPGT_REG, EMAC_MAC_IPGT_FD);
797 1.1 jmcneill sun4i_emac_write(sc, EMAC_MAC_IPGR_REG,
798 1.1 jmcneill __SHIFTIN(0x0c, EMAC_MAC_IPGR_IPG1) |
799 1.1 jmcneill __SHIFTIN(0x12, EMAC_MAC_IPGR_IPG2));
800 1.1 jmcneill
801 1.1 jmcneill sun4i_emac_write(sc, EMAC_MAC_CLRT_REG,
802 1.1 jmcneill __SHIFTIN(0x0f, EMAC_MAC_CLRT_RM) |
803 1.1 jmcneill __SHIFTIN(0x37, EMAC_MAC_CLRT_CW));
804 1.1 jmcneill
805 1.1 jmcneill sun4i_emac_write(sc, EMAC_MAC_MAXF_REG, 0x600);
806 1.1 jmcneill
807 1.1 jmcneill sun4i_emac_rx_hash(sc);
808 1.1 jmcneill
809 1.1 jmcneill sun4i_emac_int_enable(sc);
810 1.1 jmcneill
811 1.1 jmcneill ifp->if_flags |= IFF_RUNNING;
812 1.1 jmcneill ifp->if_flags &= ~IFF_OACTIVE;
813 1.1 jmcneill
814 1.1 jmcneill /* Enable RX/TX */
815 1.1 jmcneill sun4i_emac_clear_set(sc, EMAC_CTL_REG,
816 1.1 jmcneill 0, EMAC_CTL_RST | EMAC_CTL_TX_EN | EMAC_CTL_RX_EN);
817 1.1 jmcneill
818 1.1 jmcneill mii_mediachg(mii);
819 1.1 jmcneill callout_schedule(&sc->sc_stat_ch, hz);
820 1.1 jmcneill
821 1.1 jmcneill return 0;
822 1.1 jmcneill }
823 1.1 jmcneill
824 1.1 jmcneill static void
825 1.1 jmcneill sun4i_emac_ifwatchdog(struct ifnet *ifp)
826 1.1 jmcneill {
827 1.1 jmcneill struct sun4i_emac_softc * const sc = ifp->if_softc;
828 1.1 jmcneill
829 1.1 jmcneill device_printf(sc->sc_dev, "device timeout\n");
830 1.1 jmcneill
831 1.1 jmcneill ifp->if_oerrors++;
832 1.1 jmcneill sun4i_emac_ifinit(ifp);
833 1.1 jmcneill sun4i_emac_ifstart(ifp);
834 1.1 jmcneill }
835 1.1 jmcneill
836 1.1 jmcneill static void
837 1.1 jmcneill sun4i_emac_rx_hash(struct sun4i_emac_softc *sc)
838 1.1 jmcneill {
839 1.1 jmcneill struct ifnet * const ifp = &sc->sc_ec.ec_if;
840 1.1 jmcneill struct ether_multistep step;
841 1.1 jmcneill struct ether_multi *enm;
842 1.1 jmcneill uint32_t hash[2];
843 1.1 jmcneill uint32_t rxctl;
844 1.1 jmcneill
845 1.1 jmcneill rxctl = sun4i_emac_read(sc, EMAC_RX_CTL_REG);
846 1.1 jmcneill rxctl &= ~EMAC_RX_CTL_MHF;
847 1.1 jmcneill rxctl |= EMAC_RX_CTL_UCAD;
848 1.1 jmcneill rxctl |= EMAC_RX_CTL_DAF;
849 1.1 jmcneill rxctl |= EMAC_RX_CTL_MC0;
850 1.1 jmcneill rxctl |= EMAC_RX_CTL_BC0;
851 1.1 jmcneill rxctl |= EMAC_RX_CTL_POR;
852 1.1 jmcneill
853 1.1 jmcneill hash[0] = hash[1] = ~0;
854 1.1 jmcneill if (ifp->if_flags & IFF_PROMISC) {
855 1.1 jmcneill ifp->if_flags |= IFF_ALLMULTI;
856 1.1 jmcneill rxctl |= EMAC_RX_CTL_PROMISC;
857 1.1 jmcneill } else {
858 1.1 jmcneill rxctl &= ~EMAC_RX_CTL_PROMISC;
859 1.1 jmcneill }
860 1.1 jmcneill
861 1.1 jmcneill if ((ifp->if_flags & IFF_PROMISC) == 0) {
862 1.1 jmcneill hash[0] = hash[1] = 0;
863 1.1 jmcneill
864 1.1 jmcneill ETHER_FIRST_MULTI(step, &sc->sc_ec, enm);
865 1.1 jmcneill while (enm != NULL) {
866 1.1 jmcneill if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
867 1.1 jmcneill /*
868 1.1 jmcneill * We must listen to a range of multicast addresses.
869 1.1 jmcneill * For now, just accept all multicasts, rather than
870 1.1 jmcneill * trying to set only those filter bits needed to match
871 1.1 jmcneill * the range. (At this time, the only use of address
872 1.1 jmcneill * ranges is for IP multicast routing, for which the
873 1.1 jmcneill * range is big enough to require all bits set.)
874 1.1 jmcneill */
875 1.1 jmcneill hash[0] = hash[1] = ~0;
876 1.1 jmcneill ifp->if_flags |= IFF_ALLMULTI;
877 1.1 jmcneill goto done;
878 1.1 jmcneill }
879 1.1 jmcneill
880 1.1 jmcneill u_int crc = ether_crc32_be(enm->enm_addrlo, ETHER_ADDR_LEN);
881 1.1 jmcneill
882 1.1 jmcneill /* Just want the 6 most significant bits. */
883 1.1 jmcneill crc >>= 26;
884 1.1 jmcneill
885 1.1 jmcneill /* Set the corresponding bit in the filter. */
886 1.1 jmcneill hash[crc >> 5] |= __BIT(crc & 31);
887 1.1 jmcneill ETHER_NEXT_MULTI(step, enm);
888 1.1 jmcneill }
889 1.1 jmcneill ifp->if_flags &= ~IFF_ALLMULTI;
890 1.1 jmcneill rxctl |= EMAC_RX_CTL_MHF;
891 1.1 jmcneill }
892 1.1 jmcneill
893 1.1 jmcneill done:
894 1.1 jmcneill
895 1.1 jmcneill sun4i_emac_write(sc, EMAC_RX_HASH0_REG, hash[0]);
896 1.1 jmcneill sun4i_emac_write(sc, EMAC_RX_HASH1_REG, hash[1]);
897 1.1 jmcneill
898 1.1 jmcneill sun4i_emac_write(sc, EMAC_RX_CTL_REG, rxctl);
899 1.1 jmcneill }
900