xref: /freebsd/sys/dev/dwc/if_dwc.c (revision 801fb66a7e34c340f23d82f2b375feee4bd87df4)
15d43fd68SRuslan Bukin /*-
25d43fd68SRuslan Bukin  * Copyright (c) 2014 Ruslan Bukin <br@bsdpad.com>
35d43fd68SRuslan Bukin  *
45d43fd68SRuslan Bukin  * This software was developed by SRI International and the University of
55d43fd68SRuslan Bukin  * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237)
65d43fd68SRuslan Bukin  * ("CTSRD"), as part of the DARPA CRASH research programme.
75d43fd68SRuslan Bukin  *
85d43fd68SRuslan Bukin  * Redistribution and use in source and binary forms, with or without
95d43fd68SRuslan Bukin  * modification, are permitted provided that the following conditions
105d43fd68SRuslan Bukin  * are met:
115d43fd68SRuslan Bukin  * 1. Redistributions of source code must retain the above copyright
125d43fd68SRuslan Bukin  *    notice, this list of conditions and the following disclaimer.
135d43fd68SRuslan Bukin  * 2. Redistributions in binary form must reproduce the above copyright
145d43fd68SRuslan Bukin  *    notice, this list of conditions and the following disclaimer in the
155d43fd68SRuslan Bukin  *    documentation and/or other materials provided with the distribution.
165d43fd68SRuslan Bukin  *
175d43fd68SRuslan Bukin  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
185d43fd68SRuslan Bukin  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
195d43fd68SRuslan Bukin  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
205d43fd68SRuslan Bukin  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
215d43fd68SRuslan Bukin  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
225d43fd68SRuslan Bukin  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
235d43fd68SRuslan Bukin  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
245d43fd68SRuslan Bukin  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
255d43fd68SRuslan Bukin  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
265d43fd68SRuslan Bukin  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
275d43fd68SRuslan Bukin  * SUCH DAMAGE.
285d43fd68SRuslan Bukin  */
295d43fd68SRuslan Bukin 
305d43fd68SRuslan Bukin /*
315d43fd68SRuslan Bukin  * Ethernet media access controller (EMAC)
325d43fd68SRuslan Bukin  * Chapter 17, Altera Cyclone V Device Handbook (CV-5V2 2014.07.22)
335d43fd68SRuslan Bukin  *
345d43fd68SRuslan Bukin  * EMAC is an instance of the Synopsys DesignWare 3504-0
355d43fd68SRuslan Bukin  * Universal 10/100/1000 Ethernet MAC (DWC_gmac).
365d43fd68SRuslan Bukin  */
375d43fd68SRuslan Bukin 
385d43fd68SRuslan Bukin #include <sys/cdefs.h>
395d43fd68SRuslan Bukin #include <sys/param.h>
405d43fd68SRuslan Bukin #include <sys/systm.h>
415d43fd68SRuslan Bukin #include <sys/bus.h>
42d7acb49aSJared McNeill #include <sys/gpio.h>
435d43fd68SRuslan Bukin #include <sys/kernel.h>
445d43fd68SRuslan Bukin #include <sys/lock.h>
45da9a326bSLuiz Otavio O Souza #include <sys/malloc.h>
465d43fd68SRuslan Bukin #include <sys/mbuf.h>
47da9a326bSLuiz Otavio O Souza #include <sys/module.h>
485d43fd68SRuslan Bukin #include <sys/mutex.h>
49da9a326bSLuiz Otavio O Souza #include <sys/rman.h>
505d43fd68SRuslan Bukin #include <sys/socket.h>
515d43fd68SRuslan Bukin #include <sys/sockio.h>
525d43fd68SRuslan Bukin 
535d43fd68SRuslan Bukin #include <net/bpf.h>
545d43fd68SRuslan Bukin #include <net/if.h>
555d43fd68SRuslan Bukin #include <net/ethernet.h>
565d43fd68SRuslan Bukin #include <net/if_dl.h>
575d43fd68SRuslan Bukin #include <net/if_media.h>
585d43fd68SRuslan Bukin #include <net/if_types.h>
595d43fd68SRuslan Bukin #include <net/if_var.h>
605d43fd68SRuslan Bukin 
615d43fd68SRuslan Bukin #include <machine/bus.h>
625d43fd68SRuslan Bukin 
6350059a60SEmmanuel Vadot #include <dev/extres/clk/clk.h>
6450059a60SEmmanuel Vadot #include <dev/extres/hwreset/hwreset.h>
6550059a60SEmmanuel Vadot 
665d43fd68SRuslan Bukin #include <dev/mii/mii.h>
675d43fd68SRuslan Bukin #include <dev/mii/miivar.h>
68da9a326bSLuiz Otavio O Souza #include <dev/ofw/ofw_bus.h>
69da9a326bSLuiz Otavio O Souza #include <dev/ofw/ofw_bus_subr.h>
70f77d8d10SEmmanuel Vadot #include <dev/mii/mii_fdt.h>
71da9a326bSLuiz Otavio O Souza 
72c36125f6SEmmanuel Vadot #include <dev/dwc/if_dwcvar.h>
73972adf0fSEmmanuel Vadot #include <dev/dwc/dwc1000_reg.h>
7462519d5aSEmmanuel Vadot #include <dev/dwc/dwc1000_core.h>
75972adf0fSEmmanuel Vadot #include <dev/dwc/dwc1000_dma.h>
76c36125f6SEmmanuel Vadot 
775df53927SLuiz Otavio O Souza #include "if_dwc_if.h"
78d7acb49aSJared McNeill #include "gpio_if.h"
795d43fd68SRuslan Bukin #include "miibus_if.h"
805d43fd68SRuslan Bukin 
815d43fd68SRuslan Bukin static struct resource_spec dwc_spec[] = {
825d43fd68SRuslan Bukin 	{ SYS_RES_MEMORY,	0,	RF_ACTIVE },
835d43fd68SRuslan Bukin 	{ SYS_RES_IRQ,		0,	RF_ACTIVE },
845d43fd68SRuslan Bukin 	{ -1, 0 }
855d43fd68SRuslan Bukin };
865d43fd68SRuslan Bukin 
875d43fd68SRuslan Bukin static void dwc_stop_locked(struct dwc_softc *sc);
885d43fd68SRuslan Bukin 
892a35d391SEmmanuel Vadot static void dwc_tick(void *arg);
902a35d391SEmmanuel Vadot 
912a35d391SEmmanuel Vadot /*
922a35d391SEmmanuel Vadot  * Media functions
932a35d391SEmmanuel Vadot  */
942a35d391SEmmanuel Vadot 
952a35d391SEmmanuel Vadot static void
96ca018790SMitchell Horne dwc_media_status(if_t ifp, struct ifmediareq *ifmr)
972a35d391SEmmanuel Vadot {
982a35d391SEmmanuel Vadot 	struct dwc_softc *sc;
992a35d391SEmmanuel Vadot 	struct mii_data *mii;
1002a35d391SEmmanuel Vadot 
101ca018790SMitchell Horne 	sc = if_getsoftc(ifp);
1022a35d391SEmmanuel Vadot 	mii = sc->mii_softc;
1032a35d391SEmmanuel Vadot 	DWC_LOCK(sc);
1042a35d391SEmmanuel Vadot 	mii_pollstat(mii);
1052a35d391SEmmanuel Vadot 	ifmr->ifm_active = mii->mii_media_active;
1062a35d391SEmmanuel Vadot 	ifmr->ifm_status = mii->mii_media_status;
1072a35d391SEmmanuel Vadot 	DWC_UNLOCK(sc);
1082a35d391SEmmanuel Vadot }
1092a35d391SEmmanuel Vadot 
1102a35d391SEmmanuel Vadot static int
1112a35d391SEmmanuel Vadot dwc_media_change_locked(struct dwc_softc *sc)
1122a35d391SEmmanuel Vadot {
1132a35d391SEmmanuel Vadot 
1142a35d391SEmmanuel Vadot 	return (mii_mediachg(sc->mii_softc));
1152a35d391SEmmanuel Vadot }
1162a35d391SEmmanuel Vadot 
1172a35d391SEmmanuel Vadot static int
118ca018790SMitchell Horne dwc_media_change(if_t ifp)
1192a35d391SEmmanuel Vadot {
1202a35d391SEmmanuel Vadot 	struct dwc_softc *sc;
1212a35d391SEmmanuel Vadot 	int error;
1222a35d391SEmmanuel Vadot 
123ca018790SMitchell Horne 	sc = if_getsoftc(ifp);
1242a35d391SEmmanuel Vadot 
1252a35d391SEmmanuel Vadot 	DWC_LOCK(sc);
1262a35d391SEmmanuel Vadot 	error = dwc_media_change_locked(sc);
1272a35d391SEmmanuel Vadot 	DWC_UNLOCK(sc);
1282a35d391SEmmanuel Vadot 	return (error);
1292a35d391SEmmanuel Vadot }
1302a35d391SEmmanuel Vadot 
1312a35d391SEmmanuel Vadot /*
1322a35d391SEmmanuel Vadot  * if_ functions
1332a35d391SEmmanuel Vadot  */
1342a35d391SEmmanuel Vadot 
135f88e0af6SEmmanuel Vadot static void
1362a35d391SEmmanuel Vadot dwc_txstart_locked(struct dwc_softc *sc)
1375d43fd68SRuslan Bukin {
138ca018790SMitchell Horne 	if_t ifp;
1392a35d391SEmmanuel Vadot 
1402a35d391SEmmanuel Vadot 	DWC_ASSERT_LOCKED(sc);
1412a35d391SEmmanuel Vadot 
1422a35d391SEmmanuel Vadot 	if (!sc->link_is_up)
1432a35d391SEmmanuel Vadot 		return;
1442a35d391SEmmanuel Vadot 
1452a35d391SEmmanuel Vadot 	ifp = sc->ifp;
1462a35d391SEmmanuel Vadot 
1473bbd11eeSEmmanuel Vadot 	if ((if_getdrvflags(ifp) & (IFF_DRV_RUNNING|IFF_DRV_OACTIVE)) !=
1483bbd11eeSEmmanuel Vadot 	    IFF_DRV_RUNNING)
1492a35d391SEmmanuel Vadot 		return;
150afa0f66eSEmmanuel Vadot 	dma1000_txstart(sc);
1512a35d391SEmmanuel Vadot }
1522a35d391SEmmanuel Vadot 
1532a35d391SEmmanuel Vadot static void
154ca018790SMitchell Horne dwc_txstart(if_t ifp)
1552a35d391SEmmanuel Vadot {
156ca018790SMitchell Horne 	struct dwc_softc *sc = if_getsoftc(ifp);
1572a35d391SEmmanuel Vadot 
1582a35d391SEmmanuel Vadot 	DWC_LOCK(sc);
1592a35d391SEmmanuel Vadot 	dwc_txstart_locked(sc);
1602a35d391SEmmanuel Vadot 	DWC_UNLOCK(sc);
1612a35d391SEmmanuel Vadot }
1622a35d391SEmmanuel Vadot 
1632a35d391SEmmanuel Vadot static void
1642a35d391SEmmanuel Vadot dwc_init_locked(struct dwc_softc *sc)
1652a35d391SEmmanuel Vadot {
166ca018790SMitchell Horne 	if_t ifp = sc->ifp;
1672a35d391SEmmanuel Vadot 
1682a35d391SEmmanuel Vadot 	DWC_ASSERT_LOCKED(sc);
1692a35d391SEmmanuel Vadot 
1703bbd11eeSEmmanuel Vadot 	if (if_getdrvflags(ifp) & IFF_DRV_RUNNING)
1712a35d391SEmmanuel Vadot 		return;
1722a35d391SEmmanuel Vadot 
1736501fcdcSJiahao LI 	/*
17462519d5aSEmmanuel Vadot 	 * Call mii_mediachg() which will call back into dwc1000_miibus_statchg()
1756501fcdcSJiahao LI 	 * to set up the remaining config registers based on current media.
1766501fcdcSJiahao LI 	 */
1776501fcdcSJiahao LI 	mii_mediachg(sc->mii_softc);
1786501fcdcSJiahao LI 
17962519d5aSEmmanuel Vadot 	dwc1000_setup_rxfilter(sc);
18062519d5aSEmmanuel Vadot 	dwc1000_core_setup(sc);
18162519d5aSEmmanuel Vadot 	dwc1000_enable_mac(sc, true);
18262519d5aSEmmanuel Vadot 	dwc1000_enable_csum_offload(sc);
183972adf0fSEmmanuel Vadot 	dma1000_start(sc);
1842a35d391SEmmanuel Vadot 
1852a35d391SEmmanuel Vadot 	if_setdrvflagbits(ifp, IFF_DRV_RUNNING, IFF_DRV_OACTIVE);
1865d43fd68SRuslan Bukin 
1872a35d391SEmmanuel Vadot 	callout_reset(&sc->dwc_callout, hz, dwc_tick, sc);
1882a35d391SEmmanuel Vadot }
1892a35d391SEmmanuel Vadot 
1902a35d391SEmmanuel Vadot static void
1912a35d391SEmmanuel Vadot dwc_init(void *if_softc)
1922a35d391SEmmanuel Vadot {
1932a35d391SEmmanuel Vadot 	struct dwc_softc *sc = if_softc;
1942a35d391SEmmanuel Vadot 
1952a35d391SEmmanuel Vadot 	DWC_LOCK(sc);
1962a35d391SEmmanuel Vadot 	dwc_init_locked(sc);
1972a35d391SEmmanuel Vadot 	DWC_UNLOCK(sc);
1982a35d391SEmmanuel Vadot }
1992a35d391SEmmanuel Vadot 
2002a35d391SEmmanuel Vadot static void
2012a35d391SEmmanuel Vadot dwc_stop_locked(struct dwc_softc *sc)
2022a35d391SEmmanuel Vadot {
203ca018790SMitchell Horne 	if_t ifp;
2042a35d391SEmmanuel Vadot 
2052a35d391SEmmanuel Vadot 	DWC_ASSERT_LOCKED(sc);
2062a35d391SEmmanuel Vadot 
2072a35d391SEmmanuel Vadot 	ifp = sc->ifp;
2083bbd11eeSEmmanuel Vadot 	if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
2092a35d391SEmmanuel Vadot 	sc->tx_watchdog_count = 0;
2102a35d391SEmmanuel Vadot 	sc->stats_harvest_count = 0;
2112a35d391SEmmanuel Vadot 
2122a35d391SEmmanuel Vadot 	callout_stop(&sc->dwc_callout);
2132a35d391SEmmanuel Vadot 
214972adf0fSEmmanuel Vadot 	dma1000_stop(sc);
21562519d5aSEmmanuel Vadot 	dwc1000_enable_mac(sc, false);
2162a35d391SEmmanuel Vadot }
2172a35d391SEmmanuel Vadot 
2182a35d391SEmmanuel Vadot static int
219ca018790SMitchell Horne dwc_ioctl(if_t ifp, u_long cmd, caddr_t data)
2202a35d391SEmmanuel Vadot {
2212a35d391SEmmanuel Vadot 	struct dwc_softc *sc;
2222a35d391SEmmanuel Vadot 	struct mii_data *mii;
2232a35d391SEmmanuel Vadot 	struct ifreq *ifr;
2243bbd11eeSEmmanuel Vadot 	int flags, mask, error;
2252a35d391SEmmanuel Vadot 
226ca018790SMitchell Horne 	sc = if_getsoftc(ifp);
2272a35d391SEmmanuel Vadot 	ifr = (struct ifreq *)data;
2282a35d391SEmmanuel Vadot 
2292a35d391SEmmanuel Vadot 	error = 0;
2302a35d391SEmmanuel Vadot 	switch (cmd) {
2312a35d391SEmmanuel Vadot 	case SIOCSIFFLAGS:
2322a35d391SEmmanuel Vadot 		DWC_LOCK(sc);
2333bbd11eeSEmmanuel Vadot 		if (if_getflags(ifp) & IFF_UP) {
2343bbd11eeSEmmanuel Vadot 			if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
2353bbd11eeSEmmanuel Vadot 				flags = if_getflags(ifp) ^ sc->if_flags;
2363bbd11eeSEmmanuel Vadot 				if ((flags & (IFF_PROMISC|IFF_ALLMULTI)) != 0)
23762519d5aSEmmanuel Vadot 					dwc1000_setup_rxfilter(sc);
2385d43fd68SRuslan Bukin 			} else {
2392a35d391SEmmanuel Vadot 				if (!sc->is_detaching)
2402a35d391SEmmanuel Vadot 					dwc_init_locked(sc);
2412a35d391SEmmanuel Vadot 			}
2422a35d391SEmmanuel Vadot 		} else {
2433bbd11eeSEmmanuel Vadot 			if (if_getdrvflags(ifp) & IFF_DRV_RUNNING)
2442a35d391SEmmanuel Vadot 				dwc_stop_locked(sc);
2452a35d391SEmmanuel Vadot 		}
2463bbd11eeSEmmanuel Vadot 		sc->if_flags = if_getflags(ifp);
2472a35d391SEmmanuel Vadot 		DWC_UNLOCK(sc);
2482a35d391SEmmanuel Vadot 		break;
2492a35d391SEmmanuel Vadot 	case SIOCADDMULTI:
2502a35d391SEmmanuel Vadot 	case SIOCDELMULTI:
2513bbd11eeSEmmanuel Vadot 		if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
2522a35d391SEmmanuel Vadot 			DWC_LOCK(sc);
25362519d5aSEmmanuel Vadot 			dwc1000_setup_rxfilter(sc);
2542a35d391SEmmanuel Vadot 			DWC_UNLOCK(sc);
2552a35d391SEmmanuel Vadot 		}
2562a35d391SEmmanuel Vadot 		break;
2572a35d391SEmmanuel Vadot 	case SIOCSIFMEDIA:
2582a35d391SEmmanuel Vadot 	case SIOCGIFMEDIA:
2592a35d391SEmmanuel Vadot 		mii = sc->mii_softc;
2602a35d391SEmmanuel Vadot 		error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, cmd);
2612a35d391SEmmanuel Vadot 		break;
2622a35d391SEmmanuel Vadot 	case SIOCSIFCAP:
2633bbd11eeSEmmanuel Vadot 		mask = ifr->ifr_reqcap ^ if_getcapenable(ifp);
2642a35d391SEmmanuel Vadot 		if (mask & IFCAP_VLAN_MTU) {
2652a35d391SEmmanuel Vadot 			/* No work to do except acknowledge the change took */
2663bbd11eeSEmmanuel Vadot 			if_togglecapenable(ifp, IFCAP_VLAN_MTU);
2672a35d391SEmmanuel Vadot 		}
26898ea5a7bSEmmanuel Vadot 		if (mask & IFCAP_RXCSUM)
26998ea5a7bSEmmanuel Vadot 			if_togglecapenable(ifp, IFCAP_RXCSUM);
27098ea5a7bSEmmanuel Vadot 		if (mask & IFCAP_TXCSUM)
27198ea5a7bSEmmanuel Vadot 			if_togglecapenable(ifp, IFCAP_TXCSUM);
27298ea5a7bSEmmanuel Vadot 		if ((if_getcapenable(ifp) & IFCAP_TXCSUM) != 0)
27398ea5a7bSEmmanuel Vadot 			if_sethwassistbits(ifp, CSUM_IP | CSUM_UDP | CSUM_TCP, 0);
27498ea5a7bSEmmanuel Vadot 		else
27598ea5a7bSEmmanuel Vadot 			if_sethwassistbits(ifp, 0, CSUM_IP | CSUM_UDP | CSUM_TCP);
27635c9edabSJiahao Li 
27735c9edabSJiahao Li 		if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
27835c9edabSJiahao Li 			DWC_LOCK(sc);
27962519d5aSEmmanuel Vadot 			dwc1000_enable_csum_offload(sc);
28035c9edabSJiahao Li 			DWC_UNLOCK(sc);
28135c9edabSJiahao Li 		}
2822a35d391SEmmanuel Vadot 		break;
2832a35d391SEmmanuel Vadot 
2842a35d391SEmmanuel Vadot 	default:
2852a35d391SEmmanuel Vadot 		error = ether_ioctl(ifp, cmd, data);
2862a35d391SEmmanuel Vadot 		break;
2872a35d391SEmmanuel Vadot 	}
2882a35d391SEmmanuel Vadot 
2892a35d391SEmmanuel Vadot 	return (error);
2902a35d391SEmmanuel Vadot }
2912a35d391SEmmanuel Vadot 
2922a35d391SEmmanuel Vadot /*
2932a35d391SEmmanuel Vadot  * Interrupts functions
2942a35d391SEmmanuel Vadot  */
2952a35d391SEmmanuel Vadot 
2962a35d391SEmmanuel Vadot 
2972a35d391SEmmanuel Vadot static void
2982a35d391SEmmanuel Vadot dwc_intr(void *arg)
2992a35d391SEmmanuel Vadot {
3002a35d391SEmmanuel Vadot 	struct dwc_softc *sc;
30129776aa4SEmmanuel Vadot 	int rv;
3022a35d391SEmmanuel Vadot 
3032a35d391SEmmanuel Vadot 	sc = arg;
3042a35d391SEmmanuel Vadot 	DWC_LOCK(sc);
30529776aa4SEmmanuel Vadot 	dwc1000_intr(sc);
30629776aa4SEmmanuel Vadot 	rv = dma1000_intr(sc);
30729776aa4SEmmanuel Vadot 	if (rv == EIO) {
3082a35d391SEmmanuel Vadot 		device_printf(sc->dev,
3092a35d391SEmmanuel Vadot 		  "Ethernet DMA error, restarting controller.\n");
3102a35d391SEmmanuel Vadot 		dwc_stop_locked(sc);
3112a35d391SEmmanuel Vadot 		dwc_init_locked(sc);
3122a35d391SEmmanuel Vadot 	}
3132a35d391SEmmanuel Vadot 	DWC_UNLOCK(sc);
3142a35d391SEmmanuel Vadot }
3152a35d391SEmmanuel Vadot 
3162a35d391SEmmanuel Vadot static void
3172a35d391SEmmanuel Vadot dwc_tick(void *arg)
3182a35d391SEmmanuel Vadot {
3192a35d391SEmmanuel Vadot 	struct dwc_softc *sc;
320ca018790SMitchell Horne 	if_t ifp;
3212a35d391SEmmanuel Vadot 	int link_was_up;
3222a35d391SEmmanuel Vadot 
3232a35d391SEmmanuel Vadot 	sc = arg;
3242a35d391SEmmanuel Vadot 
3252a35d391SEmmanuel Vadot 	DWC_ASSERT_LOCKED(sc);
3262a35d391SEmmanuel Vadot 
3272a35d391SEmmanuel Vadot 	ifp = sc->ifp;
3282a35d391SEmmanuel Vadot 
3293bbd11eeSEmmanuel Vadot 	if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) == 0)
3302a35d391SEmmanuel Vadot 	    return;
3312a35d391SEmmanuel Vadot 
3322a35d391SEmmanuel Vadot 	/*
3332a35d391SEmmanuel Vadot 	 * Typical tx watchdog.  If this fires it indicates that we enqueued
3342a35d391SEmmanuel Vadot 	 * packets for output and never got a txdone interrupt for them.  Maybe
3352a35d391SEmmanuel Vadot 	 * it's a missed interrupt somehow, just pretend we got one.
3362a35d391SEmmanuel Vadot 	 */
3372a35d391SEmmanuel Vadot 	if (sc->tx_watchdog_count > 0) {
3382a35d391SEmmanuel Vadot 		if (--sc->tx_watchdog_count == 0) {
339972adf0fSEmmanuel Vadot 			dma1000_txfinish_locked(sc);
3402a35d391SEmmanuel Vadot 		}
3412a35d391SEmmanuel Vadot 	}
3422a35d391SEmmanuel Vadot 
3432a35d391SEmmanuel Vadot 	/* Gather stats from hardware counters. */
34462519d5aSEmmanuel Vadot 	dwc1000_harvest_stats(sc);
3452a35d391SEmmanuel Vadot 
3462a35d391SEmmanuel Vadot 	/* Check the media status. */
3472a35d391SEmmanuel Vadot 	link_was_up = sc->link_is_up;
3482a35d391SEmmanuel Vadot 	mii_tick(sc->mii_softc);
3492a35d391SEmmanuel Vadot 	if (sc->link_is_up && !link_was_up)
3502a35d391SEmmanuel Vadot 		dwc_txstart_locked(sc);
3512a35d391SEmmanuel Vadot 
3522a35d391SEmmanuel Vadot 	/* Schedule another check one second from now. */
3532a35d391SEmmanuel Vadot 	callout_reset(&sc->dwc_callout, hz, dwc_tick, sc);
3542a35d391SEmmanuel Vadot }
3552a35d391SEmmanuel Vadot 
356d7acb49aSJared McNeill static int
357f114aaddSEmmanuel Vadot dwc_reset_phy(struct dwc_softc *sc)
358d7acb49aSJared McNeill {
359d7acb49aSJared McNeill 	pcell_t gpio_prop[4];
360d7acb49aSJared McNeill 	pcell_t delay_prop[3];
361f114aaddSEmmanuel Vadot 	phandle_t gpio_node;
362d7acb49aSJared McNeill 	device_t gpio;
363d7acb49aSJared McNeill 	uint32_t pin, flags;
364d7acb49aSJared McNeill 	uint32_t pin_value;
365d7acb49aSJared McNeill 
366f114aaddSEmmanuel Vadot 	/*
367f114aaddSEmmanuel Vadot 	 * All those properties are deprecated but still used in some DTS.
368f114aaddSEmmanuel Vadot 	 * The new way to deal with this is to use the generic bindings
369f114aaddSEmmanuel Vadot 	 * present in the ethernet-phy node.
370f114aaddSEmmanuel Vadot 	 */
371f114aaddSEmmanuel Vadot 	if (OF_getencprop(sc->node, "snps,reset-gpio",
372d7acb49aSJared McNeill 	    gpio_prop, sizeof(gpio_prop)) <= 0)
373d7acb49aSJared McNeill 		return (0);
374d7acb49aSJared McNeill 
375f114aaddSEmmanuel Vadot 	if (OF_getencprop(sc->node, "snps,reset-delays-us",
376d7acb49aSJared McNeill 	    delay_prop, sizeof(delay_prop)) <= 0) {
377f114aaddSEmmanuel Vadot 		device_printf(sc->dev,
378d7acb49aSJared McNeill 		    "Wrong property for snps,reset-delays-us");
379d7acb49aSJared McNeill 		return (ENXIO);
380d7acb49aSJared McNeill 	}
381d7acb49aSJared McNeill 
382d7acb49aSJared McNeill 	gpio_node = OF_node_from_xref(gpio_prop[0]);
383d7acb49aSJared McNeill 	if ((gpio = OF_device_from_xref(gpio_prop[0])) == NULL) {
384f114aaddSEmmanuel Vadot 		device_printf(sc->dev,
385d7acb49aSJared McNeill 		    "Can't find gpio controller for phy reset\n");
386d7acb49aSJared McNeill 		return (ENXIO);
387d7acb49aSJared McNeill 	}
388d7acb49aSJared McNeill 
389f114aaddSEmmanuel Vadot 	if (GPIO_MAP_GPIOS(gpio, sc->node, gpio_node,
39073a1170aSPedro F. Giffuni 	    nitems(gpio_prop) - 1,
391d7acb49aSJared McNeill 	    gpio_prop + 1, &pin, &flags) != 0) {
392f114aaddSEmmanuel Vadot 		device_printf(sc->dev, "Can't map gpio for phy reset\n");
393d7acb49aSJared McNeill 		return (ENXIO);
394d7acb49aSJared McNeill 	}
395d7acb49aSJared McNeill 
396d7acb49aSJared McNeill 	pin_value = GPIO_PIN_LOW;
397f114aaddSEmmanuel Vadot 	if (OF_hasprop(sc->node, "snps,reset-active-low"))
398d7acb49aSJared McNeill 		pin_value = GPIO_PIN_HIGH;
399d7acb49aSJared McNeill 
400d7acb49aSJared McNeill 	GPIO_PIN_SETFLAGS(gpio, pin, GPIO_PIN_OUTPUT);
401d7acb49aSJared McNeill 	GPIO_PIN_SET(gpio, pin, pin_value);
402c069412eSEmmanuel Vadot 	DELAY(delay_prop[0] * 5);
403d7acb49aSJared McNeill 	GPIO_PIN_SET(gpio, pin, !pin_value);
404c069412eSEmmanuel Vadot 	DELAY(delay_prop[1] * 5);
405d7acb49aSJared McNeill 	GPIO_PIN_SET(gpio, pin, pin_value);
406c069412eSEmmanuel Vadot 	DELAY(delay_prop[2] * 5);
407d7acb49aSJared McNeill 
408d7acb49aSJared McNeill 	return (0);
409d7acb49aSJared McNeill }
410d7acb49aSJared McNeill 
4116a05f063SJared McNeill static int
41250059a60SEmmanuel Vadot dwc_clock_init(struct dwc_softc *sc)
4136a05f063SJared McNeill {
41450059a60SEmmanuel Vadot 	int rv;
415824cfb47SOleksandr Tymoshenko 	int64_t freq;
4166a05f063SJared McNeill 
41750059a60SEmmanuel Vadot 	/* Required clock */
41850059a60SEmmanuel Vadot 	rv = clk_get_by_ofw_name(sc->dev, 0, "stmmaceth", &sc->clk_stmmaceth);
41950059a60SEmmanuel Vadot 	if (rv != 0) {
42050059a60SEmmanuel Vadot 		device_printf(sc->dev, "Cannot get GMAC main clock\n");
42150059a60SEmmanuel Vadot 		return (ENXIO);
4226a05f063SJared McNeill 	}
42350059a60SEmmanuel Vadot 	if ((rv = clk_enable(sc->clk_stmmaceth)) != 0) {
42450059a60SEmmanuel Vadot 		device_printf(sc->dev, "could not enable main clock\n");
42550059a60SEmmanuel Vadot 		return (rv);
4266a05f063SJared McNeill 	}
4276a05f063SJared McNeill 
42850059a60SEmmanuel Vadot 	/* Optional clock */
42950059a60SEmmanuel Vadot 	rv = clk_get_by_ofw_name(sc->dev, 0, "pclk", &sc->clk_pclk);
43050059a60SEmmanuel Vadot 	if (rv != 0)
43150059a60SEmmanuel Vadot 		return (0);
43250059a60SEmmanuel Vadot 	if ((rv = clk_enable(sc->clk_pclk)) != 0) {
43350059a60SEmmanuel Vadot 		device_printf(sc->dev, "could not enable peripheral clock\n");
43450059a60SEmmanuel Vadot 		return (rv);
4356a05f063SJared McNeill 	}
43650059a60SEmmanuel Vadot 
43750059a60SEmmanuel Vadot 	if (bootverbose) {
43850059a60SEmmanuel Vadot 		clk_get_freq(sc->clk_stmmaceth, &freq);
43950059a60SEmmanuel Vadot 		device_printf(sc->dev, "MAC clock(%s) freq: %jd\n",
44050059a60SEmmanuel Vadot 		    clk_get_name(sc->clk_stmmaceth), (intmax_t)freq);
44150059a60SEmmanuel Vadot 	}
44250059a60SEmmanuel Vadot 
44350059a60SEmmanuel Vadot 	return (0);
44450059a60SEmmanuel Vadot }
44550059a60SEmmanuel Vadot 
44650059a60SEmmanuel Vadot static int
44750059a60SEmmanuel Vadot dwc_reset_deassert(struct dwc_softc *sc)
44850059a60SEmmanuel Vadot {
44950059a60SEmmanuel Vadot 	int rv;
45050059a60SEmmanuel Vadot 
45150059a60SEmmanuel Vadot 	/* Required reset */
45250059a60SEmmanuel Vadot 	rv = hwreset_get_by_ofw_name(sc->dev, 0, "stmmaceth", &sc->rst_stmmaceth);
45350059a60SEmmanuel Vadot 	if (rv != 0) {
45450059a60SEmmanuel Vadot 		device_printf(sc->dev, "Cannot get GMAC reset\n");
45550059a60SEmmanuel Vadot 		return (ENXIO);
45650059a60SEmmanuel Vadot 	}
45750059a60SEmmanuel Vadot 	rv = hwreset_deassert(sc->rst_stmmaceth);
45850059a60SEmmanuel Vadot 	if (rv != 0) {
45950059a60SEmmanuel Vadot 		device_printf(sc->dev, "could not de-assert GMAC reset\n");
46050059a60SEmmanuel Vadot 		return (rv);
46150059a60SEmmanuel Vadot 	}
46250059a60SEmmanuel Vadot 
46350059a60SEmmanuel Vadot 	/* Optional reset */
46450059a60SEmmanuel Vadot 	rv = hwreset_get_by_ofw_name(sc->dev, 0, "ahb", &sc->rst_ahb);
46550059a60SEmmanuel Vadot 	if (rv != 0)
46650059a60SEmmanuel Vadot 		return (0);
46750059a60SEmmanuel Vadot 	rv = hwreset_deassert(sc->rst_ahb);
46850059a60SEmmanuel Vadot 	if (rv != 0) {
46950059a60SEmmanuel Vadot 		device_printf(sc->dev, "could not de-assert AHB reset\n");
47050059a60SEmmanuel Vadot 		return (rv);
4716a05f063SJared McNeill 	}
4726a05f063SJared McNeill 
4736a05f063SJared McNeill 	return (0);
4746a05f063SJared McNeill }
4756a05f063SJared McNeill 
47627f03f16SEmmanuel Vadot /*
47727f03f16SEmmanuel Vadot  * Probe/Attach functions
47827f03f16SEmmanuel Vadot  */
47927f03f16SEmmanuel Vadot 
4805d43fd68SRuslan Bukin static int
4815d43fd68SRuslan Bukin dwc_probe(device_t dev)
4825d43fd68SRuslan Bukin {
4835d43fd68SRuslan Bukin 
4845d43fd68SRuslan Bukin 	if (!ofw_bus_status_okay(dev))
4855d43fd68SRuslan Bukin 		return (ENXIO);
4865d43fd68SRuslan Bukin 
4875d43fd68SRuslan Bukin 	if (!ofw_bus_is_compatible(dev, "snps,dwmac"))
4885d43fd68SRuslan Bukin 		return (ENXIO);
4895d43fd68SRuslan Bukin 
4905d43fd68SRuslan Bukin 	device_set_desc(dev, "Gigabit Ethernet Controller");
4915d43fd68SRuslan Bukin 	return (BUS_PROBE_DEFAULT);
4925d43fd68SRuslan Bukin }
4935d43fd68SRuslan Bukin 
4945d43fd68SRuslan Bukin static int
4955d43fd68SRuslan Bukin dwc_attach(device_t dev)
4965d43fd68SRuslan Bukin {
4975d43fd68SRuslan Bukin 	uint8_t macaddr[ETHER_ADDR_LEN];
4985d43fd68SRuslan Bukin 	struct dwc_softc *sc;
499ca018790SMitchell Horne 	if_t ifp;
500363b7c39SEmmanuel Vadot 	int error;
5015d88a52bSEmmanuel Vadot 	uint32_t pbl;
5025d43fd68SRuslan Bukin 
5035d43fd68SRuslan Bukin 	sc = device_get_softc(dev);
5045d43fd68SRuslan Bukin 	sc->dev = dev;
5055d43fd68SRuslan Bukin 	sc->rx_idx = 0;
506e5232621SOleksandr Tymoshenko 	sc->tx_desccount = TX_DESC_COUNT;
507e5232621SOleksandr Tymoshenko 	sc->tx_mapcount = 0;
5085df53927SLuiz Otavio O Souza 	sc->mii_clk = IF_DWC_MII_CLK(dev);
5095df53927SLuiz Otavio O Souza 	sc->mactype = IF_DWC_MAC_TYPE(dev);
5105df53927SLuiz Otavio O Souza 
511f114aaddSEmmanuel Vadot 	sc->node = ofw_bus_get_node(dev);
512*801fb66aSEmmanuel Vadot 	sc->phy_mode = mii_fdt_get_contype(sc->node);
513*801fb66aSEmmanuel Vadot 	switch (sc->phy_mode) {
514f77d8d10SEmmanuel Vadot 	case MII_CONTYPE_RGMII:
515f77d8d10SEmmanuel Vadot 	case MII_CONTYPE_RGMII_ID:
516f77d8d10SEmmanuel Vadot 	case MII_CONTYPE_RGMII_RXID:
517f77d8d10SEmmanuel Vadot 	case MII_CONTYPE_RGMII_TXID:
518f77d8d10SEmmanuel Vadot 	case MII_CONTYPE_RMII:
519da6252a6SEmmanuel Vadot 	case MII_CONTYPE_MII:
520da6252a6SEmmanuel Vadot 		break;
521f77d8d10SEmmanuel Vadot 	default:
522f77d8d10SEmmanuel Vadot 		device_printf(dev, "Unsupported MII type\n");
523f77d8d10SEmmanuel Vadot 		return (ENXIO);
524824cfb47SOleksandr Tymoshenko 	}
525824cfb47SOleksandr Tymoshenko 
526f114aaddSEmmanuel Vadot 	if (OF_getencprop(sc->node, "snps,pbl", &pbl, sizeof(uint32_t)) <= 0)
5273e5cd548SEmmanuel Vadot 		pbl = BUS_MODE_DEFAULT_PBL;
5285d88a52bSEmmanuel Vadot 	if (OF_getencprop(sc->node, "snps,txpbl", &sc->txpbl, sizeof(uint32_t)) <= 0)
5295d88a52bSEmmanuel Vadot 		sc->txpbl = pbl;
5305d88a52bSEmmanuel Vadot 	if (OF_getencprop(sc->node, "snps,rxpbl", &sc->rxpbl, sizeof(uint32_t)) <= 0)
5315d88a52bSEmmanuel Vadot 		sc->rxpbl = pbl;
532f114aaddSEmmanuel Vadot 	if (OF_hasprop(sc->node, "snps,no-pbl-x8") == 1)
5335d88a52bSEmmanuel Vadot 		sc->nopblx8 = true;
534f114aaddSEmmanuel Vadot 	if (OF_hasprop(sc->node, "snps,fixed-burst") == 1)
5355d88a52bSEmmanuel Vadot 		sc->fixed_burst = true;
536f4bb6ea5SEmmanuel Vadot 	if (OF_hasprop(sc->node, "snps,mixed-burst") == 1)
5375d88a52bSEmmanuel Vadot 		sc->mixed_burst = true;
538b69c49d1SEmmanuel Vadot 	if (OF_hasprop(sc->node, "snps,aal") == 1)
5395d88a52bSEmmanuel Vadot 		sc->aal = true;
5405e38d9e4SEmmanuel Vadot 
54150059a60SEmmanuel Vadot 	error = clk_set_assigned(dev, ofw_bus_get_node(dev));
54250059a60SEmmanuel Vadot 	if (error != 0) {
54350059a60SEmmanuel Vadot 		device_printf(dev, "clk_set_assigned failed\n");
54450059a60SEmmanuel Vadot 		return (error);
54550059a60SEmmanuel Vadot 	}
5465d43fd68SRuslan Bukin 
54750059a60SEmmanuel Vadot 	/* Enable main clock */
54850059a60SEmmanuel Vadot 	if ((error = dwc_clock_init(sc)) != 0)
54950059a60SEmmanuel Vadot 		return (error);
55050059a60SEmmanuel Vadot 	/* De-assert main reset */
55150059a60SEmmanuel Vadot 	if ((error = dwc_reset_deassert(sc)) != 0)
55250059a60SEmmanuel Vadot 		return (error);
55350059a60SEmmanuel Vadot 
55450059a60SEmmanuel Vadot 	if (IF_DWC_INIT(dev) != 0)
5556a05f063SJared McNeill 		return (ENXIO);
5566a05f063SJared McNeill 
5575d43fd68SRuslan Bukin 	if (bus_alloc_resources(dev, dwc_spec, sc->res)) {
5585d43fd68SRuslan Bukin 		device_printf(dev, "could not allocate resources\n");
5595d43fd68SRuslan Bukin 		return (ENXIO);
5605d43fd68SRuslan Bukin 	}
5615d43fd68SRuslan Bukin 
5625d43fd68SRuslan Bukin 	/* Read MAC before reset */
56362519d5aSEmmanuel Vadot 	dwc1000_get_hwaddr(sc, macaddr);
5645d43fd68SRuslan Bukin 
565d7acb49aSJared McNeill 	/* Reset the PHY if needed */
566f114aaddSEmmanuel Vadot 	if (dwc_reset_phy(sc) != 0) {
567d7acb49aSJared McNeill 		device_printf(dev, "Can't reset the PHY\n");
56830f16ad4SEmmanuel Vadot 		bus_release_resources(dev, dwc_spec, sc->res);
569d7acb49aSJared McNeill 		return (ENXIO);
570d7acb49aSJared McNeill 	}
571d7acb49aSJared McNeill 
5725d43fd68SRuslan Bukin 	/* Reset */
573363b7c39SEmmanuel Vadot 	if ((error = dma1000_reset(sc)) != 0) {
574363b7c39SEmmanuel Vadot 		device_printf(sc->dev, "Can't reset DMA controller.\n");
575363b7c39SEmmanuel Vadot 		bus_release_resources(sc->dev, dwc_spec, sc->res);
576363b7c39SEmmanuel Vadot 		return (error);
5775d43fd68SRuslan Bukin 	}
5785d43fd68SRuslan Bukin 
579972adf0fSEmmanuel Vadot 	if (dma1000_init(sc)) {
58030f16ad4SEmmanuel Vadot 		bus_release_resources(dev, dwc_spec, sc->res);
5815d43fd68SRuslan Bukin 		return (ENXIO);
58230f16ad4SEmmanuel Vadot 	}
5835d43fd68SRuslan Bukin 
584d8e5258dSRuslan Bukin 	mtx_init(&sc->mtx, device_get_nameunit(sc->dev),
585d8e5258dSRuslan Bukin 	    MTX_NETWORK_LOCK, MTX_DEF);
586d8e5258dSRuslan Bukin 
587d8e5258dSRuslan Bukin 	callout_init_mtx(&sc->dwc_callout, &sc->mtx, 0);
588d8e5258dSRuslan Bukin 
589d8e5258dSRuslan Bukin 	/* Setup interrupt handler. */
590d8e5258dSRuslan Bukin 	error = bus_setup_intr(dev, sc->res[1], INTR_TYPE_NET | INTR_MPSAFE,
591d8e5258dSRuslan Bukin 	    NULL, dwc_intr, sc, &sc->intr_cookie);
592d8e5258dSRuslan Bukin 	if (error != 0) {
593d8e5258dSRuslan Bukin 		device_printf(dev, "could not setup interrupt handler.\n");
59430f16ad4SEmmanuel Vadot 		bus_release_resources(dev, dwc_spec, sc->res);
595d8e5258dSRuslan Bukin 		return (ENXIO);
596d8e5258dSRuslan Bukin 	}
597d8e5258dSRuslan Bukin 
5985d43fd68SRuslan Bukin 	/* Set up the ethernet interface. */
5995d43fd68SRuslan Bukin 	sc->ifp = ifp = if_alloc(IFT_ETHER);
6005d43fd68SRuslan Bukin 
601ca018790SMitchell Horne 	if_setsoftc(ifp, sc);
6025d43fd68SRuslan Bukin 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
6033bbd11eeSEmmanuel Vadot 	if_setflags(sc->ifp, IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST);
6043bbd11eeSEmmanuel Vadot 	if_setstartfn(ifp, dwc_txstart);
6053bbd11eeSEmmanuel Vadot 	if_setioctlfn(ifp, dwc_ioctl);
6063bbd11eeSEmmanuel Vadot 	if_setinitfn(ifp, dwc_init);
607e5232621SOleksandr Tymoshenko 	if_setsendqlen(ifp, TX_MAP_COUNT - 1);
6083bbd11eeSEmmanuel Vadot 	if_setsendqready(sc->ifp);
60998ea5a7bSEmmanuel Vadot 	if_sethwassist(sc->ifp, CSUM_IP | CSUM_UDP | CSUM_TCP);
61098ea5a7bSEmmanuel Vadot 	if_setcapabilities(sc->ifp, IFCAP_VLAN_MTU | IFCAP_HWCSUM);
6113bbd11eeSEmmanuel Vadot 	if_setcapenable(sc->ifp, if_getcapabilities(sc->ifp));
6125d43fd68SRuslan Bukin 
6135d43fd68SRuslan Bukin 	/* Attach the mii driver. */
6145d43fd68SRuslan Bukin 	error = mii_attach(dev, &sc->miibus, ifp, dwc_media_change,
6155d43fd68SRuslan Bukin 	    dwc_media_status, BMSR_DEFCAPMASK, MII_PHY_ANY,
6165d43fd68SRuslan Bukin 	    MII_OFFSET_ANY, 0);
6175d43fd68SRuslan Bukin 
6185d43fd68SRuslan Bukin 	if (error != 0) {
6195d43fd68SRuslan Bukin 		device_printf(dev, "PHY attach failed\n");
62030f16ad4SEmmanuel Vadot 		bus_teardown_intr(dev, sc->res[1], sc->intr_cookie);
62130f16ad4SEmmanuel Vadot 		bus_release_resources(dev, dwc_spec, sc->res);
6225d43fd68SRuslan Bukin 		return (ENXIO);
6235d43fd68SRuslan Bukin 	}
6245d43fd68SRuslan Bukin 	sc->mii_softc = device_get_softc(sc->miibus);
6255d43fd68SRuslan Bukin 
6265d43fd68SRuslan Bukin 	/* All ready to run, attach the ethernet interface. */
6275d43fd68SRuslan Bukin 	ether_ifattach(ifp, macaddr);
6285d43fd68SRuslan Bukin 	sc->is_attached = true;
6295d43fd68SRuslan Bukin 
6305d43fd68SRuslan Bukin 	return (0);
6315d43fd68SRuslan Bukin }
6325d43fd68SRuslan Bukin 
63327b39e58SMitchell Horne static int
63427b39e58SMitchell Horne dwc_detach(device_t dev)
63527b39e58SMitchell Horne {
63627b39e58SMitchell Horne 	struct dwc_softc *sc;
63727b39e58SMitchell Horne 
63827b39e58SMitchell Horne 	sc = device_get_softc(dev);
63927b39e58SMitchell Horne 
64027b39e58SMitchell Horne 	/*
64127b39e58SMitchell Horne 	 * Disable and tear down interrupts before anything else, so we don't
64227b39e58SMitchell Horne 	 * race with the handler.
64327b39e58SMitchell Horne 	 */
64429776aa4SEmmanuel Vadot 	dwc1000_intr_disable(sc);
64527b39e58SMitchell Horne 	if (sc->intr_cookie != NULL) {
64627b39e58SMitchell Horne 		bus_teardown_intr(dev, sc->res[1], sc->intr_cookie);
64727b39e58SMitchell Horne 	}
64827b39e58SMitchell Horne 
64927b39e58SMitchell Horne 	if (sc->is_attached) {
65027b39e58SMitchell Horne 		DWC_LOCK(sc);
65127b39e58SMitchell Horne 		sc->is_detaching = true;
65227b39e58SMitchell Horne 		dwc_stop_locked(sc);
65327b39e58SMitchell Horne 		DWC_UNLOCK(sc);
65427b39e58SMitchell Horne 		callout_drain(&sc->dwc_callout);
65527b39e58SMitchell Horne 		ether_ifdetach(sc->ifp);
65627b39e58SMitchell Horne 	}
65727b39e58SMitchell Horne 
65827b39e58SMitchell Horne 	if (sc->miibus != NULL) {
65927b39e58SMitchell Horne 		device_delete_child(dev, sc->miibus);
66027b39e58SMitchell Horne 		sc->miibus = NULL;
66127b39e58SMitchell Horne 	}
66227b39e58SMitchell Horne 	bus_generic_detach(dev);
66327b39e58SMitchell Horne 
66427b39e58SMitchell Horne 	/* Free DMA descriptors */
665972adf0fSEmmanuel Vadot 	dma1000_free(sc);
66627b39e58SMitchell Horne 
66727b39e58SMitchell Horne 	if (sc->ifp != NULL) {
66827b39e58SMitchell Horne 		if_free(sc->ifp);
66927b39e58SMitchell Horne 		sc->ifp = NULL;
67027b39e58SMitchell Horne 	}
67127b39e58SMitchell Horne 
67227b39e58SMitchell Horne 	bus_release_resources(dev, dwc_spec, sc->res);
67327b39e58SMitchell Horne 
67427b39e58SMitchell Horne 	mtx_destroy(&sc->mtx);
67527b39e58SMitchell Horne 	return (0);
67627b39e58SMitchell Horne }
67727b39e58SMitchell Horne 
6785d43fd68SRuslan Bukin static device_method_t dwc_methods[] = {
6795d43fd68SRuslan Bukin 	DEVMETHOD(device_probe,		dwc_probe),
6805d43fd68SRuslan Bukin 	DEVMETHOD(device_attach,	dwc_attach),
68127b39e58SMitchell Horne 	DEVMETHOD(device_detach,	dwc_detach),
6825d43fd68SRuslan Bukin 
6835d43fd68SRuslan Bukin 	/* MII Interface */
68462519d5aSEmmanuel Vadot 	DEVMETHOD(miibus_readreg,	dwc1000_miibus_read_reg),
68562519d5aSEmmanuel Vadot 	DEVMETHOD(miibus_writereg,	dwc1000_miibus_write_reg),
68662519d5aSEmmanuel Vadot 	DEVMETHOD(miibus_statchg,	dwc1000_miibus_statchg),
6875d43fd68SRuslan Bukin 
6885d43fd68SRuslan Bukin 	{ 0, 0 }
6895d43fd68SRuslan Bukin };
6905d43fd68SRuslan Bukin 
6915df53927SLuiz Otavio O Souza driver_t dwc_driver = {
6925d43fd68SRuslan Bukin 	"dwc",
6935d43fd68SRuslan Bukin 	dwc_methods,
6945d43fd68SRuslan Bukin 	sizeof(struct dwc_softc),
6955d43fd68SRuslan Bukin };
6965d43fd68SRuslan Bukin 
697e55d0536SJohn Baldwin DRIVER_MODULE(dwc, simplebus, dwc_driver, 0, 0);
6983e38757dSJohn Baldwin DRIVER_MODULE(miibus, dwc, miibus_driver, 0, 0);
6995d43fd68SRuslan Bukin 
7005d43fd68SRuslan Bukin MODULE_DEPEND(dwc, ether, 1, 1, 1);
7015d43fd68SRuslan Bukin MODULE_DEPEND(dwc, miibus, 1, 1, 1);
702