1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2013 Ganbold Tsagaankhuu <ganbold@freebsd.org>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 /* A10/A20 EMAC driver */
30
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/kernel.h>
34 #include <sys/module.h>
35 #include <sys/bus.h>
36 #include <sys/lock.h>
37 #include <sys/mbuf.h>
38 #include <sys/mutex.h>
39 #include <sys/rman.h>
40 #include <sys/socket.h>
41 #include <sys/sockio.h>
42 #include <sys/sysctl.h>
43 #include <sys/gpio.h>
44
45 #include <machine/bus.h>
46 #include <machine/resource.h>
47 #include <machine/intr.h>
48
49 #include <net/if.h>
50 #include <net/if_var.h>
51 #include <net/if_arp.h>
52 #include <net/if_dl.h>
53 #include <net/if_media.h>
54 #include <net/if_types.h>
55 #include <net/if_mib.h>
56 #include <net/ethernet.h>
57 #include <net/if_vlan_var.h>
58
59 #ifdef INET
60 #include <netinet/in.h>
61 #include <netinet/in_systm.h>
62 #include <netinet/in_var.h>
63 #include <netinet/ip.h>
64 #endif
65
66 #include <net/bpf.h>
67 #include <net/bpfdesc.h>
68
69 #include <dev/ofw/ofw_bus.h>
70 #include <dev/ofw/ofw_bus_subr.h>
71
72 #include <dev/mii/mii.h>
73 #include <dev/mii/miivar.h>
74
75 #include <arm/allwinner/if_emacreg.h>
76 #include <arm/allwinner/aw_sid.h>
77
78 #include <dev/clk/clk.h>
79
80 #include "miibus_if.h"
81
82 #include "gpio_if.h"
83
84 #include "a10_sramc.h"
85
86 struct emac_softc {
87 if_t emac_ifp;
88 device_t emac_dev;
89 device_t emac_miibus;
90 bus_space_handle_t emac_handle;
91 bus_space_tag_t emac_tag;
92 struct resource *emac_res;
93 struct resource *emac_irq;
94 void *emac_intrhand;
95 clk_t emac_clk;
96 int emac_if_flags;
97 struct mtx emac_mtx;
98 struct callout emac_tick_ch;
99 int emac_watchdog_timer;
100 int emac_rx_process_limit;
101 int emac_link;
102 uint32_t emac_fifo_mask;
103 };
104
105 static int emac_probe(device_t);
106 static int emac_attach(device_t);
107 static int emac_detach(device_t);
108 static int emac_shutdown(device_t);
109 static int emac_suspend(device_t);
110 static int emac_resume(device_t);
111
112 static int emac_sys_setup(struct emac_softc *);
113 static void emac_reset(struct emac_softc *);
114
115 static void emac_init_locked(struct emac_softc *);
116 static void emac_start_locked(if_t);
117 static void emac_init(void *);
118 static void emac_stop_locked(struct emac_softc *);
119 static void emac_intr(void *);
120 static int emac_ioctl(if_t, u_long, caddr_t);
121
122 static void emac_rxeof(struct emac_softc *, int);
123 static void emac_txeof(struct emac_softc *, uint32_t);
124
125 static int emac_miibus_readreg(device_t, int, int);
126 static int emac_miibus_writereg(device_t, int, int, int);
127 static void emac_miibus_statchg(device_t);
128
129 static int emac_ifmedia_upd(if_t);
130 static void emac_ifmedia_sts(if_t, struct ifmediareq *);
131
132 static int sysctl_int_range(SYSCTL_HANDLER_ARGS, int, int);
133 static int sysctl_hw_emac_proc_limit(SYSCTL_HANDLER_ARGS);
134
135 #define EMAC_READ_REG(sc, reg) \
136 bus_space_read_4(sc->emac_tag, sc->emac_handle, reg)
137 #define EMAC_WRITE_REG(sc, reg, val) \
138 bus_space_write_4(sc->emac_tag, sc->emac_handle, reg, val)
139
140 static int
emac_sys_setup(struct emac_softc * sc)141 emac_sys_setup(struct emac_softc *sc)
142 {
143 int error;
144
145 /* Activate EMAC clock. */
146 error = clk_get_by_ofw_index(sc->emac_dev, 0, 0, &sc->emac_clk);
147 if (error != 0) {
148 device_printf(sc->emac_dev, "cannot get clock\n");
149 return (error);
150 }
151 error = clk_enable(sc->emac_clk);
152 if (error != 0) {
153 device_printf(sc->emac_dev, "cannot enable clock\n");
154 return (error);
155 }
156
157 /* Map sram. */
158 a10_map_to_emac();
159
160 return (0);
161 }
162
163 static void
emac_get_hwaddr(struct emac_softc * sc,uint8_t * hwaddr)164 emac_get_hwaddr(struct emac_softc *sc, uint8_t *hwaddr)
165 {
166 uint32_t val0, val1, rnd;
167 u_char rootkey[16];
168 size_t rootkey_size;
169
170 /*
171 * Try to get MAC address from running hardware.
172 * If there is something non-zero there just use it.
173 *
174 * Otherwise set the address to a convenient locally assigned address,
175 * using the SID rootkey.
176 * This is was uboot does so we end up with the same mac as if uboot
177 * did set it.
178 * If we can't get the root key, generate a random one,
179 * 'bsd' + random 24 low-order bits. 'b' is 0x62, which has the locally
180 * assigned bit set, and the broadcast/multicast bit clear.
181 */
182 val0 = EMAC_READ_REG(sc, EMAC_MAC_A0);
183 val1 = EMAC_READ_REG(sc, EMAC_MAC_A1);
184 if ((val0 | val1) != 0 && (val0 | val1) != 0xffffff) {
185 hwaddr[0] = (val1 >> 16) & 0xff;
186 hwaddr[1] = (val1 >> 8) & 0xff;
187 hwaddr[2] = (val1 >> 0) & 0xff;
188 hwaddr[3] = (val0 >> 16) & 0xff;
189 hwaddr[4] = (val0 >> 8) & 0xff;
190 hwaddr[5] = (val0 >> 0) & 0xff;
191 } else {
192 rootkey_size = sizeof(rootkey);
193 if (aw_sid_get_fuse(AW_SID_FUSE_ROOTKEY, rootkey,
194 &rootkey_size) == 0) {
195 hwaddr[0] = 0x2;
196 hwaddr[1] = rootkey[3];
197 hwaddr[2] = rootkey[12];
198 hwaddr[3] = rootkey[13];
199 hwaddr[4] = rootkey[14];
200 hwaddr[5] = rootkey[15];
201 }
202 else {
203 rnd = arc4random() & 0x00ffffff;
204 hwaddr[0] = 'b';
205 hwaddr[1] = 's';
206 hwaddr[2] = 'd';
207 hwaddr[3] = (rnd >> 16) & 0xff;
208 hwaddr[4] = (rnd >> 8) & 0xff;
209 hwaddr[5] = (rnd >> 0) & 0xff;
210 }
211 }
212 if (bootverbose)
213 printf("MAC address: %s\n", ether_sprintf(hwaddr));
214 }
215
216 static u_int
emac_hash_maddr(void * arg,struct sockaddr_dl * sdl,u_int cnt)217 emac_hash_maddr(void *arg, struct sockaddr_dl *sdl, u_int cnt)
218 {
219 uint32_t h, *hashes = arg;
220
221 h = ether_crc32_be(LLADDR(sdl), ETHER_ADDR_LEN) >> 26;
222 hashes[h >> 5] |= 1 << (h & 0x1f);
223
224 return (1);
225 }
226
227 static void
emac_set_rx_mode(struct emac_softc * sc)228 emac_set_rx_mode(struct emac_softc *sc)
229 {
230 if_t ifp;
231 uint32_t hashes[2];
232 uint32_t rcr = 0;
233
234 EMAC_ASSERT_LOCKED(sc);
235
236 ifp = sc->emac_ifp;
237
238 rcr = EMAC_READ_REG(sc, EMAC_RX_CTL);
239
240 /* Unicast packet and DA filtering */
241 rcr |= EMAC_RX_UCAD;
242 rcr |= EMAC_RX_DAF;
243
244 hashes[0] = 0;
245 hashes[1] = 0;
246 if (if_getflags(ifp) & IFF_ALLMULTI) {
247 hashes[0] = 0xffffffff;
248 hashes[1] = 0xffffffff;
249 } else
250 if_foreach_llmaddr(ifp, emac_hash_maddr, hashes);
251 rcr |= EMAC_RX_MCO;
252 rcr |= EMAC_RX_MHF;
253 EMAC_WRITE_REG(sc, EMAC_RX_HASH0, hashes[0]);
254 EMAC_WRITE_REG(sc, EMAC_RX_HASH1, hashes[1]);
255
256 if (if_getflags(ifp) & IFF_BROADCAST) {
257 rcr |= EMAC_RX_BCO;
258 rcr |= EMAC_RX_MCO;
259 }
260
261 if (if_getflags(ifp) & IFF_PROMISC)
262 rcr |= EMAC_RX_PA;
263 else
264 rcr |= EMAC_RX_UCAD;
265
266 EMAC_WRITE_REG(sc, EMAC_RX_CTL, rcr);
267 }
268
269 static void
emac_reset(struct emac_softc * sc)270 emac_reset(struct emac_softc *sc)
271 {
272
273 EMAC_WRITE_REG(sc, EMAC_CTL, 0);
274 DELAY(200);
275 EMAC_WRITE_REG(sc, EMAC_CTL, 1);
276 DELAY(200);
277 }
278
279 static void
emac_drain_rxfifo(struct emac_softc * sc)280 emac_drain_rxfifo(struct emac_softc *sc)
281 {
282
283 while (EMAC_READ_REG(sc, EMAC_RX_FBC) > 0)
284 (void)EMAC_READ_REG(sc, EMAC_RX_IO_DATA);
285 }
286
287 static void
emac_txeof(struct emac_softc * sc,uint32_t status)288 emac_txeof(struct emac_softc *sc, uint32_t status)
289 {
290 if_t ifp;
291
292 EMAC_ASSERT_LOCKED(sc);
293
294 ifp = sc->emac_ifp;
295 status &= (EMAC_TX_FIFO0 | EMAC_TX_FIFO1);
296 sc->emac_fifo_mask &= ~status;
297 if (status == (EMAC_TX_FIFO0 | EMAC_TX_FIFO1))
298 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 2);
299 else
300 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
301 if_setdrvflagbits(ifp, 0, IFF_DRV_OACTIVE);
302
303 /* Unarm watchdog timer if no TX */
304 sc->emac_watchdog_timer = 0;
305 }
306
307 static void
emac_rxeof(struct emac_softc * sc,int count)308 emac_rxeof(struct emac_softc *sc, int count)
309 {
310 if_t ifp;
311 struct mbuf *m, *m0;
312 uint32_t reg_val, rxcount;
313 int16_t len;
314 uint16_t status;
315 int i;
316
317 ifp = sc->emac_ifp;
318 for (; count > 0 &&
319 (if_getdrvflags(ifp) & IFF_DRV_RUNNING) != 0; count--) {
320 /*
321 * Race warning: The first packet might arrive with
322 * the interrupts disabled, but the second will fix
323 */
324 rxcount = EMAC_READ_REG(sc, EMAC_RX_FBC);
325 if (!rxcount) {
326 /* Had one stuck? */
327 rxcount = EMAC_READ_REG(sc, EMAC_RX_FBC);
328 if (!rxcount)
329 return;
330 }
331 /* Check packet header */
332 reg_val = EMAC_READ_REG(sc, EMAC_RX_IO_DATA);
333 if (reg_val != EMAC_PACKET_HEADER) {
334 /* Packet header is wrong */
335 if (bootverbose)
336 if_printf(ifp, "wrong packet header\n");
337 /* Disable RX */
338 reg_val = EMAC_READ_REG(sc, EMAC_CTL);
339 reg_val &= ~EMAC_CTL_RX_EN;
340 EMAC_WRITE_REG(sc, EMAC_CTL, reg_val);
341
342 /* Flush RX FIFO */
343 reg_val = EMAC_READ_REG(sc, EMAC_RX_CTL);
344 reg_val |= EMAC_RX_FLUSH_FIFO;
345 EMAC_WRITE_REG(sc, EMAC_RX_CTL, reg_val);
346 for (i = 100; i > 0; i--) {
347 DELAY(100);
348 if ((EMAC_READ_REG(sc, EMAC_RX_CTL) &
349 EMAC_RX_FLUSH_FIFO) == 0)
350 break;
351 }
352 if (i == 0) {
353 device_printf(sc->emac_dev,
354 "flush FIFO timeout\n");
355 /* Reinitialize controller */
356 emac_init_locked(sc);
357 return;
358 }
359 /* Enable RX */
360 reg_val = EMAC_READ_REG(sc, EMAC_CTL);
361 reg_val |= EMAC_CTL_RX_EN;
362 EMAC_WRITE_REG(sc, EMAC_CTL, reg_val);
363
364 return;
365 }
366
367 /* Get packet size and status */
368 reg_val = EMAC_READ_REG(sc, EMAC_RX_IO_DATA);
369 len = reg_val & 0xffff;
370 status = (reg_val >> 16) & 0xffff;
371
372 if (len < 64 || (status & EMAC_PKT_OK) == 0) {
373 if (bootverbose)
374 if_printf(ifp,
375 "bad packet: len = %i status = %i\n",
376 len, status);
377 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
378 emac_drain_rxfifo(sc);
379 continue;
380 }
381 #if 0
382 if (status & (EMAC_CRCERR | EMAC_LENERR)) {
383 good_packet = 0;
384 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
385 if (status & EMAC_CRCERR)
386 if_printf(ifp, "crc error\n");
387 if (status & EMAC_LENERR)
388 if_printf(ifp, "length error\n");
389 }
390 #endif
391 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
392 if (m == NULL) {
393 emac_drain_rxfifo(sc);
394 return;
395 }
396 m->m_len = m->m_pkthdr.len = MCLBYTES;
397
398 /* Copy entire frame to mbuf first. */
399 bus_space_read_multi_4(sc->emac_tag, sc->emac_handle,
400 EMAC_RX_IO_DATA, mtod(m, uint32_t *), roundup2(len, 4) / 4);
401
402 m->m_pkthdr.rcvif = ifp;
403 m->m_len = m->m_pkthdr.len = len - ETHER_CRC_LEN;
404
405 /*
406 * Emac controller needs strict alignment, so to avoid
407 * copying over an entire frame to align, we allocate
408 * a new mbuf and copy ethernet header + IP header to
409 * the new mbuf. The new mbuf is prepended into the
410 * existing mbuf chain.
411 */
412 if (m->m_len <= (MHLEN - ETHER_HDR_LEN)) {
413 bcopy(m->m_data, m->m_data + ETHER_HDR_LEN, m->m_len);
414 m->m_data += ETHER_HDR_LEN;
415 } else if (m->m_len <= (MCLBYTES - ETHER_HDR_LEN) &&
416 m->m_len > (MHLEN - ETHER_HDR_LEN)) {
417 MGETHDR(m0, M_NOWAIT, MT_DATA);
418 if (m0 != NULL) {
419 len = ETHER_HDR_LEN + m->m_pkthdr.l2hlen;
420 bcopy(m->m_data, m0->m_data, len);
421 m->m_data += len;
422 m->m_len -= len;
423 m0->m_len = len;
424 M_MOVE_PKTHDR(m0, m);
425 m0->m_next = m;
426 m = m0;
427 } else {
428 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
429 m_freem(m);
430 m = NULL;
431 continue;
432 }
433 } else if (m->m_len > EMAC_MAC_MAXF) {
434 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
435 m_freem(m);
436 m = NULL;
437 continue;
438 }
439 if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
440 EMAC_UNLOCK(sc);
441 if_input(ifp, m);
442 EMAC_LOCK(sc);
443 }
444 }
445
446 static void
emac_watchdog(struct emac_softc * sc)447 emac_watchdog(struct emac_softc *sc)
448 {
449 if_t ifp;
450
451 EMAC_ASSERT_LOCKED(sc);
452
453 if (sc->emac_watchdog_timer == 0 || --sc->emac_watchdog_timer)
454 return;
455
456 ifp = sc->emac_ifp;
457
458 if (sc->emac_link == 0) {
459 if (bootverbose)
460 if_printf(sc->emac_ifp, "watchdog timeout "
461 "(missed link)\n");
462 } else
463 if_printf(sc->emac_ifp, "watchdog timeout -- resetting\n");
464
465 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
466 if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING);
467 emac_init_locked(sc);
468 if (!if_sendq_empty(ifp))
469 emac_start_locked(ifp);
470 }
471
472 static void
emac_tick(void * arg)473 emac_tick(void *arg)
474 {
475 struct emac_softc *sc;
476 struct mii_data *mii;
477
478 sc = (struct emac_softc *)arg;
479 mii = device_get_softc(sc->emac_miibus);
480 mii_tick(mii);
481
482 emac_watchdog(sc);
483 callout_reset(&sc->emac_tick_ch, hz, emac_tick, sc);
484 }
485
486 static void
emac_init(void * xcs)487 emac_init(void *xcs)
488 {
489 struct emac_softc *sc;
490
491 sc = (struct emac_softc *)xcs;
492 EMAC_LOCK(sc);
493 emac_init_locked(sc);
494 EMAC_UNLOCK(sc);
495 }
496
497 static void
emac_init_locked(struct emac_softc * sc)498 emac_init_locked(struct emac_softc *sc)
499 {
500 if_t ifp;
501 struct mii_data *mii;
502 uint32_t reg_val;
503 uint8_t *eaddr;
504
505 EMAC_ASSERT_LOCKED(sc);
506
507 ifp = sc->emac_ifp;
508 if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) != 0)
509 return;
510
511 /* Flush RX FIFO */
512 reg_val = EMAC_READ_REG(sc, EMAC_RX_CTL);
513 reg_val |= EMAC_RX_FLUSH_FIFO;
514 EMAC_WRITE_REG(sc, EMAC_RX_CTL, reg_val);
515 DELAY(1);
516
517 /* Soft reset MAC */
518 reg_val = EMAC_READ_REG(sc, EMAC_MAC_CTL0);
519 reg_val &= (~EMAC_MAC_CTL0_SOFT_RST);
520 EMAC_WRITE_REG(sc, EMAC_MAC_CTL0, reg_val);
521
522 /* Set MII clock */
523 reg_val = EMAC_READ_REG(sc, EMAC_MAC_MCFG);
524 reg_val &= (~(0xf << 2));
525 reg_val |= (0xd << 2);
526 EMAC_WRITE_REG(sc, EMAC_MAC_MCFG, reg_val);
527
528 /* Clear RX counter */
529 EMAC_WRITE_REG(sc, EMAC_RX_FBC, 0);
530
531 /* Disable all interrupt and clear interrupt status */
532 EMAC_WRITE_REG(sc, EMAC_INT_CTL, 0);
533 reg_val = EMAC_READ_REG(sc, EMAC_INT_STA);
534 EMAC_WRITE_REG(sc, EMAC_INT_STA, reg_val);
535 DELAY(1);
536
537 /* Set up TX */
538 reg_val = EMAC_READ_REG(sc, EMAC_TX_MODE);
539 reg_val |= EMAC_TX_AB_M;
540 reg_val &= EMAC_TX_TM;
541 EMAC_WRITE_REG(sc, EMAC_TX_MODE, reg_val);
542
543 /* Set up RX */
544 reg_val = EMAC_READ_REG(sc, EMAC_RX_CTL);
545 reg_val |= EMAC_RX_SETUP;
546 reg_val &= EMAC_RX_TM;
547 EMAC_WRITE_REG(sc, EMAC_RX_CTL, reg_val);
548
549 /* Set up MAC CTL0. */
550 reg_val = EMAC_READ_REG(sc, EMAC_MAC_CTL0);
551 reg_val |= EMAC_MAC_CTL0_SETUP;
552 EMAC_WRITE_REG(sc, EMAC_MAC_CTL0, reg_val);
553
554 /* Set up MAC CTL1. */
555 reg_val = EMAC_READ_REG(sc, EMAC_MAC_CTL1);
556 reg_val |= EMAC_MAC_CTL1_SETUP;
557 EMAC_WRITE_REG(sc, EMAC_MAC_CTL1, reg_val);
558
559 /* Set up IPGT */
560 EMAC_WRITE_REG(sc, EMAC_MAC_IPGT, EMAC_MAC_IPGT_FD);
561
562 /* Set up IPGR */
563 EMAC_WRITE_REG(sc, EMAC_MAC_IPGR, EMAC_MAC_NBTB_IPG2 |
564 (EMAC_MAC_NBTB_IPG1 << 8));
565
566 /* Set up Collison window */
567 EMAC_WRITE_REG(sc, EMAC_MAC_CLRT, EMAC_MAC_RM | (EMAC_MAC_CW << 8));
568
569 /* Set up Max Frame Length */
570 EMAC_WRITE_REG(sc, EMAC_MAC_MAXF, EMAC_MAC_MFL);
571
572 /* Setup ethernet address */
573 eaddr = if_getlladdr(ifp);
574 EMAC_WRITE_REG(sc, EMAC_MAC_A1, eaddr[0] << 16 |
575 eaddr[1] << 8 | eaddr[2]);
576 EMAC_WRITE_REG(sc, EMAC_MAC_A0, eaddr[3] << 16 |
577 eaddr[4] << 8 | eaddr[5]);
578
579 /* Setup rx filter */
580 emac_set_rx_mode(sc);
581
582 /* Enable RX/TX0/RX Hlevel interrupt */
583 reg_val = EMAC_READ_REG(sc, EMAC_INT_CTL);
584 reg_val |= EMAC_INT_EN;
585 EMAC_WRITE_REG(sc, EMAC_INT_CTL, reg_val);
586
587 if_setdrvflagbits(ifp, IFF_DRV_RUNNING, 0);
588 if_setdrvflagbits(ifp, 0, IFF_DRV_OACTIVE);
589
590 sc->emac_link = 0;
591
592 /* Switch to the current media. */
593 mii = device_get_softc(sc->emac_miibus);
594 mii_mediachg(mii);
595
596 callout_reset(&sc->emac_tick_ch, hz, emac_tick, sc);
597 }
598
599 static void
emac_start(if_t ifp)600 emac_start(if_t ifp)
601 {
602 struct emac_softc *sc;
603
604 sc = if_getsoftc(ifp);
605 EMAC_LOCK(sc);
606 emac_start_locked(ifp);
607 EMAC_UNLOCK(sc);
608 }
609
610 static void
emac_start_locked(if_t ifp)611 emac_start_locked(if_t ifp)
612 {
613 struct emac_softc *sc;
614 struct mbuf *m, *m0;
615 uint32_t fifo, reg;
616
617 sc = if_getsoftc(ifp);
618 if (if_getdrvflags(ifp) & IFF_DRV_OACTIVE)
619 return;
620 if (sc->emac_fifo_mask == (EMAC_TX_FIFO0 | EMAC_TX_FIFO1))
621 return;
622 if (sc->emac_link == 0)
623 return;
624 m = if_dequeue(ifp);
625 if (m == NULL)
626 return;
627
628 /* Select channel */
629 if (sc->emac_fifo_mask & EMAC_TX_FIFO0)
630 fifo = 1;
631 else
632 fifo = 0;
633 sc->emac_fifo_mask |= (1 << fifo);
634 if (sc->emac_fifo_mask == (EMAC_TX_FIFO0 | EMAC_TX_FIFO1))
635 if_setdrvflagbits(ifp, IFF_DRV_OACTIVE, 0);
636 EMAC_WRITE_REG(sc, EMAC_TX_INS, fifo);
637
638 /*
639 * Emac controller wants 4 byte aligned TX buffers.
640 * We have to copy pretty much all the time.
641 */
642 if (m->m_next != NULL || (mtod(m, uintptr_t) & 3) != 0) {
643 m0 = m_defrag(m, M_NOWAIT);
644 if (m0 == NULL) {
645 m_freem(m);
646 m = NULL;
647 return;
648 }
649 m = m0;
650 }
651 /* Write data */
652 bus_space_write_multi_4(sc->emac_tag, sc->emac_handle,
653 EMAC_TX_IO_DATA, mtod(m, uint32_t *),
654 roundup2(m->m_len, 4) / 4);
655
656 /* Send the data lengh. */
657 reg = (fifo == 0) ? EMAC_TX_PL0 : EMAC_TX_PL1;
658 EMAC_WRITE_REG(sc, reg, m->m_len);
659
660 /* Start translate from fifo to phy. */
661 reg = (fifo == 0) ? EMAC_TX_CTL0 : EMAC_TX_CTL1;
662 EMAC_WRITE_REG(sc, reg, EMAC_READ_REG(sc, reg) | 1);
663
664 /* Set timeout */
665 sc->emac_watchdog_timer = 5;
666
667 /* Data have been sent to hardware, it is okay to free the mbuf now. */
668 BPF_MTAP(ifp, m);
669 m_freem(m);
670 }
671
672 static void
emac_stop_locked(struct emac_softc * sc)673 emac_stop_locked(struct emac_softc *sc)
674 {
675 if_t ifp;
676 uint32_t reg_val;
677
678 EMAC_ASSERT_LOCKED(sc);
679
680 ifp = sc->emac_ifp;
681 if_setdrvflagbits(ifp, 0, (IFF_DRV_RUNNING | IFF_DRV_OACTIVE));
682 sc->emac_link = 0;
683
684 /* Disable all interrupt and clear interrupt status */
685 EMAC_WRITE_REG(sc, EMAC_INT_CTL, 0);
686 reg_val = EMAC_READ_REG(sc, EMAC_INT_STA);
687 EMAC_WRITE_REG(sc, EMAC_INT_STA, reg_val);
688
689 /* Disable RX/TX */
690 reg_val = EMAC_READ_REG(sc, EMAC_CTL);
691 reg_val &= ~(EMAC_CTL_RST | EMAC_CTL_TX_EN | EMAC_CTL_RX_EN);
692 EMAC_WRITE_REG(sc, EMAC_CTL, reg_val);
693
694 callout_stop(&sc->emac_tick_ch);
695 }
696
697 static void
emac_intr(void * arg)698 emac_intr(void *arg)
699 {
700 struct emac_softc *sc;
701 if_t ifp;
702 uint32_t reg_val;
703
704 sc = (struct emac_softc *)arg;
705 EMAC_LOCK(sc);
706
707 /* Disable all interrupts */
708 EMAC_WRITE_REG(sc, EMAC_INT_CTL, 0);
709 /* Get EMAC interrupt status */
710 reg_val = EMAC_READ_REG(sc, EMAC_INT_STA);
711 /* Clear ISR status */
712 EMAC_WRITE_REG(sc, EMAC_INT_STA, reg_val);
713
714 /* Received incoming packet */
715 if (reg_val & EMAC_INT_STA_RX)
716 emac_rxeof(sc, sc->emac_rx_process_limit);
717
718 /* Transmit Interrupt check */
719 if (reg_val & EMAC_INT_STA_TX) {
720 emac_txeof(sc, reg_val);
721 ifp = sc->emac_ifp;
722 if (!if_sendq_empty(ifp))
723 emac_start_locked(ifp);
724 }
725
726 /* Re-enable interrupt mask */
727 reg_val = EMAC_READ_REG(sc, EMAC_INT_CTL);
728 reg_val |= EMAC_INT_EN;
729 EMAC_WRITE_REG(sc, EMAC_INT_CTL, reg_val);
730 EMAC_UNLOCK(sc);
731 }
732
733 static int
emac_ioctl(if_t ifp,u_long command,caddr_t data)734 emac_ioctl(if_t ifp, u_long command, caddr_t data)
735 {
736 struct emac_softc *sc;
737 struct mii_data *mii;
738 struct ifreq *ifr;
739 int error = 0;
740
741 sc = if_getsoftc(ifp);
742 ifr = (struct ifreq *)data;
743
744 switch (command) {
745 case SIOCSIFFLAGS:
746 EMAC_LOCK(sc);
747 if (if_getflags(ifp) & IFF_UP) {
748 if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) != 0) {
749 if ((if_getflags(ifp) ^ sc->emac_if_flags) &
750 (IFF_PROMISC | IFF_ALLMULTI))
751 emac_set_rx_mode(sc);
752 } else
753 emac_init_locked(sc);
754 } else {
755 if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) != 0)
756 emac_stop_locked(sc);
757 }
758 sc->emac_if_flags = if_getflags(ifp);
759 EMAC_UNLOCK(sc);
760 break;
761 case SIOCADDMULTI:
762 case SIOCDELMULTI:
763 EMAC_LOCK(sc);
764 if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
765 emac_set_rx_mode(sc);
766 }
767 EMAC_UNLOCK(sc);
768 break;
769 case SIOCGIFMEDIA:
770 case SIOCSIFMEDIA:
771 mii = device_get_softc(sc->emac_miibus);
772 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
773 break;
774 default:
775 error = ether_ioctl(ifp, command, data);
776 break;
777 }
778 return (error);
779 }
780
781 static int
emac_probe(device_t dev)782 emac_probe(device_t dev)
783 {
784
785 if (!ofw_bus_status_okay(dev))
786 return (ENXIO);
787
788 if (!ofw_bus_is_compatible(dev, "allwinner,sun4i-a10-emac"))
789 return (ENXIO);
790
791 device_set_desc(dev, "A10/A20 EMAC ethernet controller");
792 return (BUS_PROBE_DEFAULT);
793 }
794
795 static int
emac_detach(device_t dev)796 emac_detach(device_t dev)
797 {
798 struct emac_softc *sc;
799
800 sc = device_get_softc(dev);
801 if_setdrvflagbits(sc->emac_ifp, 0, IFF_DRV_RUNNING);
802 if (device_is_attached(dev)) {
803 ether_ifdetach(sc->emac_ifp);
804 EMAC_LOCK(sc);
805 emac_stop_locked(sc);
806 EMAC_UNLOCK(sc);
807 callout_drain(&sc->emac_tick_ch);
808 }
809
810 if (sc->emac_intrhand != NULL)
811 bus_teardown_intr(sc->emac_dev, sc->emac_irq,
812 sc->emac_intrhand);
813
814 bus_generic_detach(sc->emac_dev);
815
816 if (sc->emac_clk != NULL)
817 clk_disable(sc->emac_clk);
818
819 if (sc->emac_res != NULL)
820 bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->emac_res);
821
822 if (sc->emac_irq != NULL)
823 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->emac_irq);
824
825 if (sc->emac_ifp != NULL)
826 if_free(sc->emac_ifp);
827
828 if (mtx_initialized(&sc->emac_mtx))
829 mtx_destroy(&sc->emac_mtx);
830
831 return (0);
832 }
833
834 static int
emac_shutdown(device_t dev)835 emac_shutdown(device_t dev)
836 {
837
838 return (emac_suspend(dev));
839 }
840
841 static int
emac_suspend(device_t dev)842 emac_suspend(device_t dev)
843 {
844 struct emac_softc *sc;
845 if_t ifp;
846
847 sc = device_get_softc(dev);
848
849 EMAC_LOCK(sc);
850 ifp = sc->emac_ifp;
851 if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) != 0)
852 emac_stop_locked(sc);
853 EMAC_UNLOCK(sc);
854
855 return (0);
856 }
857
858 static int
emac_resume(device_t dev)859 emac_resume(device_t dev)
860 {
861 struct emac_softc *sc;
862 if_t ifp;
863
864 sc = device_get_softc(dev);
865
866 EMAC_LOCK(sc);
867 ifp = sc->emac_ifp;
868 if ((if_getflags(ifp) & IFF_UP) != 0) {
869 if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING);
870 emac_init_locked(sc);
871 }
872 EMAC_UNLOCK(sc);
873
874 return (0);
875 }
876
877 static int
emac_attach(device_t dev)878 emac_attach(device_t dev)
879 {
880 struct emac_softc *sc;
881 if_t ifp;
882 int error, rid;
883 uint8_t eaddr[ETHER_ADDR_LEN];
884
885 sc = device_get_softc(dev);
886 sc->emac_dev = dev;
887
888 error = 0;
889 mtx_init(&sc->emac_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
890 MTX_DEF);
891 callout_init_mtx(&sc->emac_tick_ch, &sc->emac_mtx, 0);
892
893 rid = 0;
894 sc->emac_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
895 RF_ACTIVE);
896 if (sc->emac_res == NULL) {
897 device_printf(dev, "unable to map memory\n");
898 error = ENXIO;
899 goto fail;
900 }
901
902 sc->emac_tag = rman_get_bustag(sc->emac_res);
903 sc->emac_handle = rman_get_bushandle(sc->emac_res);
904
905 rid = 0;
906 sc->emac_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
907 RF_SHAREABLE | RF_ACTIVE);
908 if (sc->emac_irq == NULL) {
909 device_printf(dev, "cannot allocate IRQ resources.\n");
910 error = ENXIO;
911 goto fail;
912 }
913 /* Create device sysctl node. */
914 SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
915 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
916 OID_AUTO, "process_limit",
917 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
918 &sc->emac_rx_process_limit, 0, sysctl_hw_emac_proc_limit, "I",
919 "max number of Rx events to process");
920
921 sc->emac_rx_process_limit = EMAC_PROC_DEFAULT;
922 error = resource_int_value(device_get_name(dev), device_get_unit(dev),
923 "process_limit", &sc->emac_rx_process_limit);
924 if (error == 0) {
925 if (sc->emac_rx_process_limit < EMAC_PROC_MIN ||
926 sc->emac_rx_process_limit > EMAC_PROC_MAX) {
927 device_printf(dev, "process_limit value out of range; "
928 "using default: %d\n", EMAC_PROC_DEFAULT);
929 sc->emac_rx_process_limit = EMAC_PROC_DEFAULT;
930 }
931 }
932 /* Setup EMAC */
933 error = emac_sys_setup(sc);
934 if (error != 0)
935 goto fail;
936
937 emac_reset(sc);
938
939 ifp = sc->emac_ifp = if_alloc(IFT_ETHER);
940 if_setsoftc(ifp, sc);
941
942 /* Setup MII */
943 error = mii_attach(dev, &sc->emac_miibus, ifp, emac_ifmedia_upd,
944 emac_ifmedia_sts, BMSR_DEFCAPMASK, MII_PHY_ANY, MII_OFFSET_ANY, 0);
945 if (error != 0) {
946 device_printf(dev, "PHY probe failed\n");
947 goto fail;
948 }
949
950 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
951 if_setflags(ifp, IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST);
952 if_setstartfn(ifp, emac_start);
953 if_setioctlfn(ifp, emac_ioctl);
954 if_setinitfn(ifp, emac_init);
955 if_setsendqlen(ifp, IFQ_MAXLEN);
956
957 /* Get MAC address */
958 emac_get_hwaddr(sc, eaddr);
959 ether_ifattach(ifp, eaddr);
960
961 /* VLAN capability setup. */
962 if_setcapabilitiesbit(ifp, IFCAP_VLAN_MTU, 0);
963 if_setcapenable(ifp, if_getcapabilities(ifp));
964 /* Tell the upper layer we support VLAN over-sized frames. */
965 if_setifheaderlen(ifp, sizeof(struct ether_vlan_header));
966
967 error = bus_setup_intr(dev, sc->emac_irq, INTR_TYPE_NET | INTR_MPSAFE,
968 NULL, emac_intr, sc, &sc->emac_intrhand);
969 if (error != 0) {
970 device_printf(dev, "could not set up interrupt handler.\n");
971 ether_ifdetach(ifp);
972 goto fail;
973 }
974
975 fail:
976 if (error != 0)
977 emac_detach(dev);
978 return (error);
979 }
980
981 static bool
emac_miibus_iowait(struct emac_softc * sc)982 emac_miibus_iowait(struct emac_softc *sc)
983 {
984 uint32_t timeout;
985
986 for (timeout = 100; timeout != 0; --timeout) {
987 DELAY(100);
988 if ((EMAC_READ_REG(sc, EMAC_MAC_MIND) & 0x1) == 0)
989 return (true);
990 }
991
992 return (false);
993 }
994
995 /*
996 * The MII bus interface
997 */
998 static int
emac_miibus_readreg(device_t dev,int phy,int reg)999 emac_miibus_readreg(device_t dev, int phy, int reg)
1000 {
1001 struct emac_softc *sc;
1002 int rval;
1003
1004 sc = device_get_softc(dev);
1005
1006 /* Issue phy address and reg */
1007 EMAC_WRITE_REG(sc, EMAC_MAC_MADR, (phy << 8) | reg);
1008 /* Pull up the phy io line */
1009 EMAC_WRITE_REG(sc, EMAC_MAC_MCMD, 0x1);
1010 if (!emac_miibus_iowait(sc)) {
1011 device_printf(dev, "timeout waiting for mii read\n");
1012 return (0);
1013 }
1014 /* Push down the phy io line */
1015 EMAC_WRITE_REG(sc, EMAC_MAC_MCMD, 0x0);
1016 /* Read data */
1017 rval = EMAC_READ_REG(sc, EMAC_MAC_MRDD);
1018
1019 return (rval);
1020 }
1021
1022 static int
emac_miibus_writereg(device_t dev,int phy,int reg,int data)1023 emac_miibus_writereg(device_t dev, int phy, int reg, int data)
1024 {
1025 struct emac_softc *sc;
1026
1027 sc = device_get_softc(dev);
1028
1029 /* Issue phy address and reg */
1030 EMAC_WRITE_REG(sc, EMAC_MAC_MADR, (phy << 8) | reg);
1031 /* Write data */
1032 EMAC_WRITE_REG(sc, EMAC_MAC_MWTD, data);
1033 /* Pull up the phy io line */
1034 EMAC_WRITE_REG(sc, EMAC_MAC_MCMD, 0x1);
1035 if (!emac_miibus_iowait(sc)) {
1036 device_printf(dev, "timeout waiting for mii write\n");
1037 return (0);
1038 }
1039 /* Push down the phy io line */
1040 EMAC_WRITE_REG(sc, EMAC_MAC_MCMD, 0x0);
1041
1042 return (0);
1043 }
1044
1045 static void
emac_miibus_statchg(device_t dev)1046 emac_miibus_statchg(device_t dev)
1047 {
1048 struct emac_softc *sc;
1049 struct mii_data *mii;
1050 if_t ifp;
1051 uint32_t reg_val;
1052
1053 sc = device_get_softc(dev);
1054
1055 mii = device_get_softc(sc->emac_miibus);
1056 ifp = sc->emac_ifp;
1057 if (mii == NULL || ifp == NULL ||
1058 (if_getdrvflags(ifp) & IFF_DRV_RUNNING) == 0)
1059 return;
1060
1061 sc->emac_link = 0;
1062 if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
1063 (IFM_ACTIVE | IFM_AVALID)) {
1064 switch (IFM_SUBTYPE(mii->mii_media_active)) {
1065 case IFM_10_T:
1066 case IFM_100_TX:
1067 sc->emac_link = 1;
1068 break;
1069 default:
1070 break;
1071 }
1072 }
1073 /* Program MACs with resolved speed/duplex. */
1074 if (sc->emac_link != 0) {
1075 reg_val = EMAC_READ_REG(sc, EMAC_MAC_IPGT);
1076 if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) != 0) {
1077 reg_val &= ~EMAC_MAC_IPGT_HD;
1078 reg_val |= EMAC_MAC_IPGT_FD;
1079 } else {
1080 reg_val &= ~EMAC_MAC_IPGT_FD;
1081 reg_val |= EMAC_MAC_IPGT_HD;
1082 }
1083 EMAC_WRITE_REG(sc, EMAC_MAC_IPGT, reg_val);
1084 /* Enable RX/TX */
1085 reg_val = EMAC_READ_REG(sc, EMAC_CTL);
1086 reg_val |= EMAC_CTL_RST | EMAC_CTL_TX_EN | EMAC_CTL_RX_EN;
1087 EMAC_WRITE_REG(sc, EMAC_CTL, reg_val);
1088 } else {
1089 /* Disable RX/TX */
1090 reg_val = EMAC_READ_REG(sc, EMAC_CTL);
1091 reg_val &= ~(EMAC_CTL_RST | EMAC_CTL_TX_EN | EMAC_CTL_RX_EN);
1092 EMAC_WRITE_REG(sc, EMAC_CTL, reg_val);
1093 }
1094 }
1095
1096 static int
emac_ifmedia_upd(if_t ifp)1097 emac_ifmedia_upd(if_t ifp)
1098 {
1099 struct emac_softc *sc;
1100 struct mii_data *mii;
1101 struct mii_softc *miisc;
1102 int error;
1103
1104 sc = if_getsoftc(ifp);
1105 mii = device_get_softc(sc->emac_miibus);
1106 EMAC_LOCK(sc);
1107 LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
1108 PHY_RESET(miisc);
1109 error = mii_mediachg(mii);
1110 EMAC_UNLOCK(sc);
1111
1112 return (error);
1113 }
1114
1115 static void
emac_ifmedia_sts(if_t ifp,struct ifmediareq * ifmr)1116 emac_ifmedia_sts(if_t ifp, struct ifmediareq *ifmr)
1117 {
1118 struct emac_softc *sc;
1119 struct mii_data *mii;
1120
1121 sc = if_getsoftc(ifp);
1122 mii = device_get_softc(sc->emac_miibus);
1123
1124 EMAC_LOCK(sc);
1125 mii_pollstat(mii);
1126 ifmr->ifm_active = mii->mii_media_active;
1127 ifmr->ifm_status = mii->mii_media_status;
1128 EMAC_UNLOCK(sc);
1129 }
1130
1131 static device_method_t emac_methods[] = {
1132 /* Device interface */
1133 DEVMETHOD(device_probe, emac_probe),
1134 DEVMETHOD(device_attach, emac_attach),
1135 DEVMETHOD(device_detach, emac_detach),
1136 DEVMETHOD(device_shutdown, emac_shutdown),
1137 DEVMETHOD(device_suspend, emac_suspend),
1138 DEVMETHOD(device_resume, emac_resume),
1139
1140 /* bus interface, for miibus */
1141 DEVMETHOD(bus_print_child, bus_generic_print_child),
1142 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
1143
1144 /* MII interface */
1145 DEVMETHOD(miibus_readreg, emac_miibus_readreg),
1146 DEVMETHOD(miibus_writereg, emac_miibus_writereg),
1147 DEVMETHOD(miibus_statchg, emac_miibus_statchg),
1148
1149 DEVMETHOD_END
1150 };
1151
1152 static driver_t emac_driver = {
1153 "emac",
1154 emac_methods,
1155 sizeof(struct emac_softc)
1156 };
1157
1158 DRIVER_MODULE(emac, simplebus, emac_driver, 0, 0);
1159 DRIVER_MODULE(miibus, emac, miibus_driver, 0, 0);
1160 MODULE_DEPEND(emac, miibus, 1, 1, 1);
1161 MODULE_DEPEND(emac, ether, 1, 1, 1);
1162
1163 static int
sysctl_int_range(SYSCTL_HANDLER_ARGS,int low,int high)1164 sysctl_int_range(SYSCTL_HANDLER_ARGS, int low, int high)
1165 {
1166 int error, value;
1167
1168 if (arg1 == NULL)
1169 return (EINVAL);
1170 value = *(int *)arg1;
1171 error = sysctl_handle_int(oidp, &value, 0, req);
1172 if (error || req->newptr == NULL)
1173 return (error);
1174 if (value < low || value > high)
1175 return (EINVAL);
1176 *(int *)arg1 = value;
1177
1178 return (0);
1179 }
1180
1181 static int
sysctl_hw_emac_proc_limit(SYSCTL_HANDLER_ARGS)1182 sysctl_hw_emac_proc_limit(SYSCTL_HANDLER_ARGS)
1183 {
1184
1185 return (sysctl_int_range(oidp, arg1, arg2, req,
1186 EMAC_PROC_MIN, EMAC_PROC_MAX));
1187 }
1188