2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2013 Ganbold Tsagaankhuu <ganbold@freebsd.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
31 /* A10/A20 EMAC driver */
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
39 #include <sys/module.h>
43 #include <sys/mutex.h>
45 #include <sys/socket.h>
46 #include <sys/sockio.h>
47 #include <sys/sysctl.h>
50 #include <machine/bus.h>
51 #include <machine/resource.h>
52 #include <machine/intr.h>
55 #include <net/if_var.h>
56 #include <net/if_arp.h>
57 #include <net/if_dl.h>
58 #include <net/if_media.h>
59 #include <net/if_types.h>
60 #include <net/if_mib.h>
61 #include <net/ethernet.h>
62 #include <net/if_vlan_var.h>
65 #include <netinet/in.h>
66 #include <netinet/in_systm.h>
67 #include <netinet/in_var.h>
68 #include <netinet/ip.h>
72 #include <net/bpfdesc.h>
74 #include <dev/ofw/ofw_bus.h>
75 #include <dev/ofw/ofw_bus_subr.h>
77 #include <dev/mii/mii.h>
78 #include <dev/mii/miivar.h>
80 #include <arm/allwinner/if_emacreg.h>
81 #include <arm/allwinner/aw_sid.h>
83 #include <dev/extres/clk/clk.h>
85 #include "miibus_if.h"
89 #include "a10_sramc.h"
92 struct ifnet *emac_ifp;
95 bus_space_handle_t emac_handle;
96 bus_space_tag_t emac_tag;
97 struct resource *emac_res;
98 struct resource *emac_irq;
103 struct callout emac_tick_ch;
104 int emac_watchdog_timer;
105 int emac_rx_process_limit;
107 uint32_t emac_fifo_mask;
110 static int emac_probe(device_t);
111 static int emac_attach(device_t);
112 static int emac_detach(device_t);
113 static int emac_shutdown(device_t);
114 static int emac_suspend(device_t);
115 static int emac_resume(device_t);
117 static int emac_sys_setup(struct emac_softc *);
118 static void emac_reset(struct emac_softc *);
120 static void emac_init_locked(struct emac_softc *);
121 static void emac_start_locked(struct ifnet *);
122 static void emac_init(void *);
123 static void emac_stop_locked(struct emac_softc *);
124 static void emac_intr(void *);
125 static int emac_ioctl(struct ifnet *, u_long, caddr_t);
127 static void emac_rxeof(struct emac_softc *, int);
128 static void emac_txeof(struct emac_softc *, uint32_t);
130 static int emac_miibus_readreg(device_t, int, int);
131 static int emac_miibus_writereg(device_t, int, int, int);
132 static void emac_miibus_statchg(device_t);
134 static int emac_ifmedia_upd(struct ifnet *);
135 static void emac_ifmedia_sts(struct ifnet *, struct ifmediareq *);
137 static int sysctl_int_range(SYSCTL_HANDLER_ARGS, int, int);
138 static int sysctl_hw_emac_proc_limit(SYSCTL_HANDLER_ARGS);
140 #define EMAC_READ_REG(sc, reg) \
141 bus_space_read_4(sc->emac_tag, sc->emac_handle, reg)
142 #define EMAC_WRITE_REG(sc, reg, val) \
143 bus_space_write_4(sc->emac_tag, sc->emac_handle, reg, val)
146 emac_sys_setup(struct emac_softc *sc)
150 /* Activate EMAC clock. */
151 error = clk_get_by_ofw_index(sc->emac_dev, 0, 0, &sc->emac_clk);
153 device_printf(sc->emac_dev, "cannot get clock\n");
156 error = clk_enable(sc->emac_clk);
158 device_printf(sc->emac_dev, "cannot enable clock\n");
169 emac_get_hwaddr(struct emac_softc *sc, uint8_t *hwaddr)
171 uint32_t val0, val1, rnd;
176 * Try to get MAC address from running hardware.
177 * If there is something non-zero there just use it.
179 * Otherwise set the address to a convenient locally assigned address,
180 * using the SID rootkey.
181 * This is was uboot does so we end up with the same mac as if uboot
183 * If we can't get the root key, generate a random one,
184 * 'bsd' + random 24 low-order bits. 'b' is 0x62, which has the locally
185 * assigned bit set, and the broadcast/multicast bit clear.
187 val0 = EMAC_READ_REG(sc, EMAC_MAC_A0);
188 val1 = EMAC_READ_REG(sc, EMAC_MAC_A1);
189 if ((val0 | val1) != 0 && (val0 | val1) != 0xffffff) {
190 hwaddr[0] = (val1 >> 16) & 0xff;
191 hwaddr[1] = (val1 >> 8) & 0xff;
192 hwaddr[2] = (val1 >> 0) & 0xff;
193 hwaddr[3] = (val0 >> 16) & 0xff;
194 hwaddr[4] = (val0 >> 8) & 0xff;
195 hwaddr[5] = (val0 >> 0) & 0xff;
197 rootkey_size = sizeof(rootkey);
198 if (aw_sid_get_fuse(AW_SID_FUSE_ROOTKEY, rootkey,
199 &rootkey_size) == 0) {
201 hwaddr[1] = rootkey[3];
202 hwaddr[2] = rootkey[12];
203 hwaddr[3] = rootkey[13];
204 hwaddr[4] = rootkey[14];
205 hwaddr[5] = rootkey[15];
208 rnd = arc4random() & 0x00ffffff;
212 hwaddr[3] = (rnd >> 16) & 0xff;
213 hwaddr[4] = (rnd >> 8) & 0xff;
214 hwaddr[5] = (rnd >> 0) & 0xff;
218 printf("MAC address: %s\n", ether_sprintf(hwaddr));
222 emac_set_rx_mode(struct emac_softc *sc)
225 struct ifmultiaddr *ifma;
226 uint32_t h, hashes[2];
229 EMAC_ASSERT_LOCKED(sc);
233 rcr = EMAC_READ_REG(sc, EMAC_RX_CTL);
235 /* Unicast packet and DA filtering */
241 if (ifp->if_flags & IFF_ALLMULTI) {
242 hashes[0] = 0xffffffff;
243 hashes[1] = 0xffffffff;
246 CK_STAILQ_FOREACH(ifma, &sc->emac_ifp->if_multiaddrs, ifma_link) {
247 if (ifma->ifma_addr->sa_family != AF_LINK)
249 h = ether_crc32_be(LLADDR((struct sockaddr_dl *)
250 ifma->ifma_addr), ETHER_ADDR_LEN) >> 26;
251 hashes[h >> 5] |= 1 << (h & 0x1f);
253 if_maddr_runlock(ifp);
257 EMAC_WRITE_REG(sc, EMAC_RX_HASH0, hashes[0]);
258 EMAC_WRITE_REG(sc, EMAC_RX_HASH1, hashes[1]);
260 if (ifp->if_flags & IFF_BROADCAST) {
265 if (ifp->if_flags & IFF_PROMISC)
270 EMAC_WRITE_REG(sc, EMAC_RX_CTL, rcr);
274 emac_reset(struct emac_softc *sc)
277 EMAC_WRITE_REG(sc, EMAC_CTL, 0);
279 EMAC_WRITE_REG(sc, EMAC_CTL, 1);
284 emac_drain_rxfifo(struct emac_softc *sc)
288 while (EMAC_READ_REG(sc, EMAC_RX_FBC) > 0)
289 data = EMAC_READ_REG(sc, EMAC_RX_IO_DATA);
293 emac_txeof(struct emac_softc *sc, uint32_t status)
297 EMAC_ASSERT_LOCKED(sc);
300 status &= (EMAC_TX_FIFO0 | EMAC_TX_FIFO1);
301 sc->emac_fifo_mask &= ~status;
302 if (status == (EMAC_TX_FIFO0 | EMAC_TX_FIFO1))
303 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 2);
305 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
306 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
308 /* Unarm watchdog timer if no TX */
309 sc->emac_watchdog_timer = 0;
313 emac_rxeof(struct emac_softc *sc, int count)
317 uint32_t reg_val, rxcount;
324 (ifp->if_drv_flags & IFF_DRV_RUNNING) != 0; count--) {
326 * Race warning: The first packet might arrive with
327 * the interrupts disabled, but the second will fix
329 rxcount = EMAC_READ_REG(sc, EMAC_RX_FBC);
332 rxcount = EMAC_READ_REG(sc, EMAC_RX_FBC);
336 /* Check packet header */
337 reg_val = EMAC_READ_REG(sc, EMAC_RX_IO_DATA);
338 if (reg_val != EMAC_PACKET_HEADER) {
339 /* Packet header is wrong */
341 if_printf(ifp, "wrong packet header\n");
343 reg_val = EMAC_READ_REG(sc, EMAC_CTL);
344 reg_val &= ~EMAC_CTL_RX_EN;
345 EMAC_WRITE_REG(sc, EMAC_CTL, reg_val);
348 reg_val = EMAC_READ_REG(sc, EMAC_RX_CTL);
349 reg_val |= EMAC_RX_FLUSH_FIFO;
350 EMAC_WRITE_REG(sc, EMAC_RX_CTL, reg_val);
351 for (i = 100; i > 0; i--) {
353 if ((EMAC_READ_REG(sc, EMAC_RX_CTL) &
354 EMAC_RX_FLUSH_FIFO) == 0)
358 device_printf(sc->emac_dev,
359 "flush FIFO timeout\n");
360 /* Reinitialize controller */
361 emac_init_locked(sc);
365 reg_val = EMAC_READ_REG(sc, EMAC_CTL);
366 reg_val |= EMAC_CTL_RX_EN;
367 EMAC_WRITE_REG(sc, EMAC_CTL, reg_val);
372 /* Get packet size and status */
373 reg_val = EMAC_READ_REG(sc, EMAC_RX_IO_DATA);
374 len = reg_val & 0xffff;
375 status = (reg_val >> 16) & 0xffff;
377 if (len < 64 || (status & EMAC_PKT_OK) == 0) {
380 "bad packet: len = %i status = %i\n",
382 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
383 emac_drain_rxfifo(sc);
387 if (status & (EMAC_CRCERR | EMAC_LENERR)) {
389 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
390 if (status & EMAC_CRCERR)
391 if_printf(ifp, "crc error\n");
392 if (status & EMAC_LENERR)
393 if_printf(ifp, "length error\n");
396 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
398 emac_drain_rxfifo(sc);
401 m->m_len = m->m_pkthdr.len = MCLBYTES;
403 /* Copy entire frame to mbuf first. */
404 bus_space_read_multi_4(sc->emac_tag, sc->emac_handle,
405 EMAC_RX_IO_DATA, mtod(m, uint32_t *), roundup2(len, 4) / 4);
407 m->m_pkthdr.rcvif = ifp;
408 m->m_len = m->m_pkthdr.len = len - ETHER_CRC_LEN;
411 * Emac controller needs strict aligment, so to avoid
412 * copying over an entire frame to align, we allocate
413 * a new mbuf and copy ethernet header + IP header to
414 * the new mbuf. The new mbuf is prepended into the
415 * existing mbuf chain.
417 if (m->m_len <= (MHLEN - ETHER_HDR_LEN)) {
418 bcopy(m->m_data, m->m_data + ETHER_HDR_LEN, m->m_len);
419 m->m_data += ETHER_HDR_LEN;
420 } else if (m->m_len <= (MCLBYTES - ETHER_HDR_LEN) &&
421 m->m_len > (MHLEN - ETHER_HDR_LEN)) {
422 MGETHDR(m0, M_NOWAIT, MT_DATA);
424 len = ETHER_HDR_LEN + m->m_pkthdr.l2hlen;
425 bcopy(m->m_data, m0->m_data, len);
429 M_MOVE_PKTHDR(m0, m);
433 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
438 } else if (m->m_len > EMAC_MAC_MAXF) {
439 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
444 if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
446 (*ifp->if_input)(ifp, m);
452 emac_watchdog(struct emac_softc *sc)
456 EMAC_ASSERT_LOCKED(sc);
458 if (sc->emac_watchdog_timer == 0 || --sc->emac_watchdog_timer)
463 if (sc->emac_link == 0) {
465 if_printf(sc->emac_ifp, "watchdog timeout "
468 if_printf(sc->emac_ifp, "watchdog timeout -- resetting\n");
470 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
471 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
472 emac_init_locked(sc);
473 if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
474 emac_start_locked(ifp);
480 struct emac_softc *sc;
481 struct mii_data *mii;
483 sc = (struct emac_softc *)arg;
484 mii = device_get_softc(sc->emac_miibus);
488 callout_reset(&sc->emac_tick_ch, hz, emac_tick, sc);
494 struct emac_softc *sc;
496 sc = (struct emac_softc *)xcs;
498 emac_init_locked(sc);
503 emac_init_locked(struct emac_softc *sc)
506 struct mii_data *mii;
510 EMAC_ASSERT_LOCKED(sc);
513 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
517 reg_val = EMAC_READ_REG(sc, EMAC_RX_CTL);
518 reg_val |= EMAC_RX_FLUSH_FIFO;
519 EMAC_WRITE_REG(sc, EMAC_RX_CTL, reg_val);
523 reg_val = EMAC_READ_REG(sc, EMAC_MAC_CTL0);
524 reg_val &= (~EMAC_MAC_CTL0_SOFT_RST);
525 EMAC_WRITE_REG(sc, EMAC_MAC_CTL0, reg_val);
528 reg_val = EMAC_READ_REG(sc, EMAC_MAC_MCFG);
529 reg_val &= (~(0xf << 2));
530 reg_val |= (0xd << 2);
531 EMAC_WRITE_REG(sc, EMAC_MAC_MCFG, reg_val);
533 /* Clear RX counter */
534 EMAC_WRITE_REG(sc, EMAC_RX_FBC, 0);
536 /* Disable all interrupt and clear interrupt status */
537 EMAC_WRITE_REG(sc, EMAC_INT_CTL, 0);
538 reg_val = EMAC_READ_REG(sc, EMAC_INT_STA);
539 EMAC_WRITE_REG(sc, EMAC_INT_STA, reg_val);
543 reg_val = EMAC_READ_REG(sc, EMAC_TX_MODE);
544 reg_val |= EMAC_TX_AB_M;
545 reg_val &= EMAC_TX_TM;
546 EMAC_WRITE_REG(sc, EMAC_TX_MODE, reg_val);
549 reg_val = EMAC_READ_REG(sc, EMAC_RX_CTL);
550 reg_val |= EMAC_RX_SETUP;
551 reg_val &= EMAC_RX_TM;
552 EMAC_WRITE_REG(sc, EMAC_RX_CTL, reg_val);
554 /* Set up MAC CTL0. */
555 reg_val = EMAC_READ_REG(sc, EMAC_MAC_CTL0);
556 reg_val |= EMAC_MAC_CTL0_SETUP;
557 EMAC_WRITE_REG(sc, EMAC_MAC_CTL0, reg_val);
559 /* Set up MAC CTL1. */
560 reg_val = EMAC_READ_REG(sc, EMAC_MAC_CTL1);
561 reg_val |= EMAC_MAC_CTL1_SETUP;
562 EMAC_WRITE_REG(sc, EMAC_MAC_CTL1, reg_val);
565 EMAC_WRITE_REG(sc, EMAC_MAC_IPGT, EMAC_MAC_IPGT_FD);
568 EMAC_WRITE_REG(sc, EMAC_MAC_IPGR, EMAC_MAC_NBTB_IPG2 |
569 (EMAC_MAC_NBTB_IPG1 << 8));
571 /* Set up Collison window */
572 EMAC_WRITE_REG(sc, EMAC_MAC_CLRT, EMAC_MAC_RM | (EMAC_MAC_CW << 8));
574 /* Set up Max Frame Length */
575 EMAC_WRITE_REG(sc, EMAC_MAC_MAXF, EMAC_MAC_MFL);
577 /* Setup ethernet address */
578 eaddr = IF_LLADDR(ifp);
579 EMAC_WRITE_REG(sc, EMAC_MAC_A1, eaddr[0] << 16 |
580 eaddr[1] << 8 | eaddr[2]);
581 EMAC_WRITE_REG(sc, EMAC_MAC_A0, eaddr[3] << 16 |
582 eaddr[4] << 8 | eaddr[5]);
584 /* Setup rx filter */
585 emac_set_rx_mode(sc);
587 /* Enable RX/TX0/RX Hlevel interrupt */
588 reg_val = EMAC_READ_REG(sc, EMAC_INT_CTL);
589 reg_val |= EMAC_INT_EN;
590 EMAC_WRITE_REG(sc, EMAC_INT_CTL, reg_val);
592 ifp->if_drv_flags |= IFF_DRV_RUNNING;
593 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
597 /* Switch to the current media. */
598 mii = device_get_softc(sc->emac_miibus);
601 callout_reset(&sc->emac_tick_ch, hz, emac_tick, sc);
606 emac_start(struct ifnet *ifp)
608 struct emac_softc *sc;
612 emac_start_locked(ifp);
617 emac_start_locked(struct ifnet *ifp)
619 struct emac_softc *sc;
624 if (ifp->if_drv_flags & IFF_DRV_OACTIVE)
626 if (sc->emac_fifo_mask == (EMAC_TX_FIFO0 | EMAC_TX_FIFO1))
628 if (sc->emac_link == 0)
630 IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
635 if (sc->emac_fifo_mask & EMAC_TX_FIFO0)
639 sc->emac_fifo_mask |= (1 << fifo);
640 if (sc->emac_fifo_mask == (EMAC_TX_FIFO0 | EMAC_TX_FIFO1))
641 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
642 EMAC_WRITE_REG(sc, EMAC_TX_INS, fifo);
645 * Emac controller wants 4 byte aligned TX buffers.
646 * We have to copy pretty much all the time.
648 if (m->m_next != NULL || (mtod(m, uintptr_t) & 3) != 0) {
649 m0 = m_defrag(m, M_NOWAIT);
658 bus_space_write_multi_4(sc->emac_tag, sc->emac_handle,
659 EMAC_TX_IO_DATA, mtod(m, uint32_t *),
660 roundup2(m->m_len, 4) / 4);
662 /* Send the data lengh. */
663 reg = (fifo == 0) ? EMAC_TX_PL0 : EMAC_TX_PL1;
664 EMAC_WRITE_REG(sc, reg, m->m_len);
666 /* Start translate from fifo to phy. */
667 reg = (fifo == 0) ? EMAC_TX_CTL0 : EMAC_TX_CTL1;
668 EMAC_WRITE_REG(sc, reg, EMAC_READ_REG(sc, reg) | 1);
671 sc->emac_watchdog_timer = 5;
673 /* Data have been sent to hardware, it is okay to free the mbuf now. */
679 emac_stop_locked(struct emac_softc *sc)
684 EMAC_ASSERT_LOCKED(sc);
687 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
690 /* Disable all interrupt and clear interrupt status */
691 EMAC_WRITE_REG(sc, EMAC_INT_CTL, 0);
692 reg_val = EMAC_READ_REG(sc, EMAC_INT_STA);
693 EMAC_WRITE_REG(sc, EMAC_INT_STA, reg_val);
696 reg_val = EMAC_READ_REG(sc, EMAC_CTL);
697 reg_val &= ~(EMAC_CTL_RST | EMAC_CTL_TX_EN | EMAC_CTL_RX_EN);
698 EMAC_WRITE_REG(sc, EMAC_CTL, reg_val);
700 callout_stop(&sc->emac_tick_ch);
706 struct emac_softc *sc;
710 sc = (struct emac_softc *)arg;
713 /* Disable all interrupts */
714 EMAC_WRITE_REG(sc, EMAC_INT_CTL, 0);
715 /* Get EMAC interrupt status */
716 reg_val = EMAC_READ_REG(sc, EMAC_INT_STA);
717 /* Clear ISR status */
718 EMAC_WRITE_REG(sc, EMAC_INT_STA, reg_val);
720 /* Received incoming packet */
721 if (reg_val & EMAC_INT_STA_RX)
722 emac_rxeof(sc, sc->emac_rx_process_limit);
724 /* Transmit Interrupt check */
725 if (reg_val & EMAC_INT_STA_TX) {
726 emac_txeof(sc, reg_val);
728 if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
729 emac_start_locked(ifp);
732 /* Re-enable interrupt mask */
733 reg_val = EMAC_READ_REG(sc, EMAC_INT_CTL);
734 reg_val |= EMAC_INT_EN;
735 EMAC_WRITE_REG(sc, EMAC_INT_CTL, reg_val);
740 emac_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
742 struct emac_softc *sc;
743 struct mii_data *mii;
748 ifr = (struct ifreq *)data;
753 if (ifp->if_flags & IFF_UP) {
754 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) {
755 if ((ifp->if_flags ^ sc->emac_if_flags) &
756 (IFF_PROMISC | IFF_ALLMULTI))
757 emac_set_rx_mode(sc);
759 emac_init_locked(sc);
761 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
762 emac_stop_locked(sc);
764 sc->emac_if_flags = ifp->if_flags;
770 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
771 emac_set_rx_mode(sc);
777 mii = device_get_softc(sc->emac_miibus);
778 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
781 error = ether_ioctl(ifp, command, data);
788 emac_probe(device_t dev)
791 if (!ofw_bus_status_okay(dev))
794 if (!ofw_bus_is_compatible(dev, "allwinner,sun4i-a10-emac"))
797 device_set_desc(dev, "A10/A20 EMAC ethernet controller");
798 return (BUS_PROBE_DEFAULT);
802 emac_detach(device_t dev)
804 struct emac_softc *sc;
806 sc = device_get_softc(dev);
807 sc->emac_ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
808 if (device_is_attached(dev)) {
809 ether_ifdetach(sc->emac_ifp);
811 emac_stop_locked(sc);
813 callout_drain(&sc->emac_tick_ch);
816 if (sc->emac_intrhand != NULL)
817 bus_teardown_intr(sc->emac_dev, sc->emac_irq,
820 if (sc->emac_miibus != NULL) {
821 device_delete_child(sc->emac_dev, sc->emac_miibus);
822 bus_generic_detach(sc->emac_dev);
825 if (sc->emac_clk != NULL)
826 clk_disable(sc->emac_clk);
828 if (sc->emac_res != NULL)
829 bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->emac_res);
831 if (sc->emac_irq != NULL)
832 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->emac_irq);
834 if (sc->emac_ifp != NULL)
835 if_free(sc->emac_ifp);
837 if (mtx_initialized(&sc->emac_mtx))
838 mtx_destroy(&sc->emac_mtx);
844 emac_shutdown(device_t dev)
847 return (emac_suspend(dev));
851 emac_suspend(device_t dev)
853 struct emac_softc *sc;
856 sc = device_get_softc(dev);
860 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
861 emac_stop_locked(sc);
868 emac_resume(device_t dev)
870 struct emac_softc *sc;
873 sc = device_get_softc(dev);
877 if ((ifp->if_flags & IFF_UP) != 0) {
878 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
879 emac_init_locked(sc);
887 emac_attach(device_t dev)
889 struct emac_softc *sc;
892 uint8_t eaddr[ETHER_ADDR_LEN];
894 sc = device_get_softc(dev);
898 mtx_init(&sc->emac_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
900 callout_init_mtx(&sc->emac_tick_ch, &sc->emac_mtx, 0);
903 sc->emac_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
905 if (sc->emac_res == NULL) {
906 device_printf(dev, "unable to map memory\n");
911 sc->emac_tag = rman_get_bustag(sc->emac_res);
912 sc->emac_handle = rman_get_bushandle(sc->emac_res);
915 sc->emac_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
916 RF_SHAREABLE | RF_ACTIVE);
917 if (sc->emac_irq == NULL) {
918 device_printf(dev, "cannot allocate IRQ resources.\n");
922 /* Create device sysctl node. */
923 SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
924 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
925 OID_AUTO, "process_limit", CTLTYPE_INT | CTLFLAG_RW,
926 &sc->emac_rx_process_limit, 0, sysctl_hw_emac_proc_limit, "I",
927 "max number of Rx events to process");
929 sc->emac_rx_process_limit = EMAC_PROC_DEFAULT;
930 error = resource_int_value(device_get_name(dev), device_get_unit(dev),
931 "process_limit", &sc->emac_rx_process_limit);
933 if (sc->emac_rx_process_limit < EMAC_PROC_MIN ||
934 sc->emac_rx_process_limit > EMAC_PROC_MAX) {
935 device_printf(dev, "process_limit value out of range; "
936 "using default: %d\n", EMAC_PROC_DEFAULT);
937 sc->emac_rx_process_limit = EMAC_PROC_DEFAULT;
941 error = emac_sys_setup(sc);
947 ifp = sc->emac_ifp = if_alloc(IFT_ETHER);
949 device_printf(dev, "unable to allocate ifp\n");
956 error = mii_attach(dev, &sc->emac_miibus, ifp, emac_ifmedia_upd,
957 emac_ifmedia_sts, BMSR_DEFCAPMASK, MII_PHY_ANY, MII_OFFSET_ANY, 0);
959 device_printf(dev, "PHY probe failed\n");
963 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
964 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
965 ifp->if_start = emac_start;
966 ifp->if_ioctl = emac_ioctl;
967 ifp->if_init = emac_init;
968 IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
970 /* Get MAC address */
971 emac_get_hwaddr(sc, eaddr);
972 ether_ifattach(ifp, eaddr);
974 /* VLAN capability setup. */
975 ifp->if_capabilities |= IFCAP_VLAN_MTU;
976 ifp->if_capenable = ifp->if_capabilities;
977 /* Tell the upper layer we support VLAN over-sized frames. */
978 ifp->if_hdrlen = sizeof(struct ether_vlan_header);
980 error = bus_setup_intr(dev, sc->emac_irq, INTR_TYPE_NET | INTR_MPSAFE,
981 NULL, emac_intr, sc, &sc->emac_intrhand);
983 device_printf(dev, "could not set up interrupt handler.\n");
995 emac_miibus_iowait(struct emac_softc *sc)
999 for (timeout = 100; timeout != 0; --timeout) {
1001 if ((EMAC_READ_REG(sc, EMAC_MAC_MIND) & 0x1) == 0)
1009 * The MII bus interface
1012 emac_miibus_readreg(device_t dev, int phy, int reg)
1014 struct emac_softc *sc;
1017 sc = device_get_softc(dev);
1019 /* Issue phy address and reg */
1020 EMAC_WRITE_REG(sc, EMAC_MAC_MADR, (phy << 8) | reg);
1021 /* Pull up the phy io line */
1022 EMAC_WRITE_REG(sc, EMAC_MAC_MCMD, 0x1);
1023 if (!emac_miibus_iowait(sc)) {
1024 device_printf(dev, "timeout waiting for mii read\n");
1027 /* Push down the phy io line */
1028 EMAC_WRITE_REG(sc, EMAC_MAC_MCMD, 0x0);
1030 rval = EMAC_READ_REG(sc, EMAC_MAC_MRDD);
1036 emac_miibus_writereg(device_t dev, int phy, int reg, int data)
1038 struct emac_softc *sc;
1040 sc = device_get_softc(dev);
1042 /* Issue phy address and reg */
1043 EMAC_WRITE_REG(sc, EMAC_MAC_MADR, (phy << 8) | reg);
1045 EMAC_WRITE_REG(sc, EMAC_MAC_MWTD, data);
1046 /* Pull up the phy io line */
1047 EMAC_WRITE_REG(sc, EMAC_MAC_MCMD, 0x1);
1048 if (!emac_miibus_iowait(sc)) {
1049 device_printf(dev, "timeout waiting for mii write\n");
1052 /* Push down the phy io line */
1053 EMAC_WRITE_REG(sc, EMAC_MAC_MCMD, 0x0);
1059 emac_miibus_statchg(device_t dev)
1061 struct emac_softc *sc;
1062 struct mii_data *mii;
1066 sc = device_get_softc(dev);
1068 mii = device_get_softc(sc->emac_miibus);
1070 if (mii == NULL || ifp == NULL ||
1071 (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
1075 if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
1076 (IFM_ACTIVE | IFM_AVALID)) {
1077 switch (IFM_SUBTYPE(mii->mii_media_active)) {
1086 /* Program MACs with resolved speed/duplex. */
1087 if (sc->emac_link != 0) {
1088 reg_val = EMAC_READ_REG(sc, EMAC_MAC_IPGT);
1089 if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) != 0) {
1090 reg_val &= ~EMAC_MAC_IPGT_HD;
1091 reg_val |= EMAC_MAC_IPGT_FD;
1093 reg_val &= ~EMAC_MAC_IPGT_FD;
1094 reg_val |= EMAC_MAC_IPGT_HD;
1096 EMAC_WRITE_REG(sc, EMAC_MAC_IPGT, reg_val);
1098 reg_val = EMAC_READ_REG(sc, EMAC_CTL);
1099 reg_val |= EMAC_CTL_RST | EMAC_CTL_TX_EN | EMAC_CTL_RX_EN;
1100 EMAC_WRITE_REG(sc, EMAC_CTL, reg_val);
1103 reg_val = EMAC_READ_REG(sc, EMAC_CTL);
1104 reg_val &= ~(EMAC_CTL_RST | EMAC_CTL_TX_EN | EMAC_CTL_RX_EN);
1105 EMAC_WRITE_REG(sc, EMAC_CTL, reg_val);
1110 emac_ifmedia_upd(struct ifnet *ifp)
1112 struct emac_softc *sc;
1113 struct mii_data *mii;
1114 struct mii_softc *miisc;
1118 mii = device_get_softc(sc->emac_miibus);
1120 LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
1122 error = mii_mediachg(mii);
1129 emac_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
1131 struct emac_softc *sc;
1132 struct mii_data *mii;
1135 mii = device_get_softc(sc->emac_miibus);
1139 ifmr->ifm_active = mii->mii_media_active;
1140 ifmr->ifm_status = mii->mii_media_status;
1144 static device_method_t emac_methods[] = {
1145 /* Device interface */
1146 DEVMETHOD(device_probe, emac_probe),
1147 DEVMETHOD(device_attach, emac_attach),
1148 DEVMETHOD(device_detach, emac_detach),
1149 DEVMETHOD(device_shutdown, emac_shutdown),
1150 DEVMETHOD(device_suspend, emac_suspend),
1151 DEVMETHOD(device_resume, emac_resume),
1153 /* bus interface, for miibus */
1154 DEVMETHOD(bus_print_child, bus_generic_print_child),
1155 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
1158 DEVMETHOD(miibus_readreg, emac_miibus_readreg),
1159 DEVMETHOD(miibus_writereg, emac_miibus_writereg),
1160 DEVMETHOD(miibus_statchg, emac_miibus_statchg),
1165 static driver_t emac_driver = {
1168 sizeof(struct emac_softc)
1171 static devclass_t emac_devclass;
1173 DRIVER_MODULE(emac, simplebus, emac_driver, emac_devclass, 0, 0);
1174 DRIVER_MODULE(miibus, emac, miibus_driver, miibus_devclass, 0, 0);
1175 MODULE_DEPEND(emac, miibus, 1, 1, 1);
1176 MODULE_DEPEND(emac, ether, 1, 1, 1);
1179 sysctl_int_range(SYSCTL_HANDLER_ARGS, int low, int high)
1185 value = *(int *)arg1;
1186 error = sysctl_handle_int(oidp, &value, 0, req);
1187 if (error || req->newptr == NULL)
1189 if (value < low || value > high)
1191 *(int *)arg1 = value;
1197 sysctl_hw_emac_proc_limit(SYSCTL_HANDLER_ARGS)
1200 return (sysctl_int_range(oidp, arg1, arg2, req,
1201 EMAC_PROC_MIN, EMAC_PROC_MAX));