git: 4885d6f3ec5b - main - if_mvneta: Use clock frequency

From: Wojciech Macek <wma_at_FreeBSD.org>
Date: Tue, 04 Jan 2022 12:29:33 UTC
The branch main has been updated by wma:

URL: https://cgit.FreeBSD.org/src/commit/?id=4885d6f3ec5bdb9c7bf57f7a07575089572ef3ab

commit 4885d6f3ec5bdb9c7bf57f7a07575089572ef3ab
Author:     Hubert Mazur <hum@semihalf.com>
AuthorDate: 2021-08-24 10:04:30 +0000
Commit:     Wojciech Macek <wma@FreeBSD.org>
CommitDate: 2022-01-04 12:26:35 +0000

    if_mvneta: Use clock frequency
    
    Make neta use frequency obtained from clock, instead of
    hardcoded one. Use default frequency in case of clock
    device failure. Remove unnecessary function calls
    to obtain frequency and use cached one instead.
    
    Reviewed by:            manu
    Obtained from:          Semihalf
    Differential revision:  https://reviews.freebsd.org/D32336
---
 sys/dev/neta/if_mvneta.c    | 47 ++++++++++++++++++++++++++++-----------------
 sys/dev/neta/if_mvnetavar.h |  2 ++
 2 files changed, 31 insertions(+), 18 deletions(-)

diff --git a/sys/dev/neta/if_mvneta.c b/sys/dev/neta/if_mvneta.c
index c94e91b16013..4d54e95e807d 100644
--- a/sys/dev/neta/if_mvneta.c
+++ b/sys/dev/neta/if_mvneta.c
@@ -65,6 +65,8 @@ __FBSDID("$FreeBSD$");
 #include <sys/rman.h>
 #include <machine/resource.h>
 
+#include <dev/extres/clk/clk.h>
+
 #include <dev/mii/mii.h>
 #include <dev/mii/miivar.h>
 
@@ -200,9 +202,6 @@ STATIC int mvneta_detach(device_t);
 STATIC int mvneta_miibus_readreg(device_t, int, int);
 STATIC int mvneta_miibus_writereg(device_t, int, int, int);
 
-/* Clock */
-STATIC uint32_t mvneta_get_clk(void);
-
 static device_method_t mvneta_methods[] = {
 	/* Device interface */
 	DEVMETHOD(device_detach,	mvneta_detach),
@@ -344,16 +343,6 @@ static struct {
 	{ mvneta_rxtxth_intr, "MVNETA aggregated interrupt" },
 };
 
-STATIC uint32_t
-mvneta_get_clk()
-{
-#if defined(__aarch64__)
-	return (A3700_TCLK_250MHZ);
-#else
-	return (get_tclk());
-#endif
-}
-
 static int
 mvneta_set_mac_address(struct mvneta_softc *sc, uint8_t *addr)
 {
@@ -541,7 +530,9 @@ mvneta_attach(device_t self)
 #if !defined(__aarch64__)
 	uint32_t reg;
 #endif
-
+#if defined(__aarch64__)
+	clk_t clk;
+#endif
 	sc = device_get_softc(self);
 	sc->dev = self;
 
@@ -563,6 +554,27 @@ mvneta_attach(device_t self)
 	MVNETA_WRITE(sc, MVNETA_PRXINIT, 0x00000001);
 	MVNETA_WRITE(sc, MVNETA_PTXINIT, 0x00000001);
 
+#if defined(__aarch64__)
+	error = clk_get_by_ofw_index(sc->dev, ofw_bus_get_node(sc->dev), 0,
+	    &clk);
+	if (error != 0) {
+		device_printf(sc->dev,
+			"Cannot get clock, using default frequency: %d\n",
+			A3700_TCLK_250MHZ);
+		sc->clk_freq = A3700_TCLK_250MHZ;
+	} else {
+		error = clk_get_freq(clk, &sc->clk_freq);
+		if (error != 0) {
+			device_printf(sc->dev,
+				"Cannot obtain frequency from parent clock\n");
+			bus_release_resources(sc->dev, res_spec, sc->res);
+			return (error);
+		}
+	}
+#else
+	sc->clk_freq = get_tclk();
+#endif
+
 #if !defined(__aarch64__)
 	/*
 	 * Disable port snoop for buffers and descriptors
@@ -1380,7 +1392,7 @@ mvneta_ring_init_rx_queue(struct mvneta_softc *sc, int q)
 	rx = MVNETA_RX_RING(sc, q);
 	rx->dma = rx->cpu = 0;
 	rx->queue_th_received = MVNETA_RXTH_COUNT;
-	rx->queue_th_time = (mvneta_get_clk() / 1000) / 10; /* 0.1 [ms] */
+	rx->queue_th_time = (sc->clk_freq / 1000) / 10; /* 0.1 [ms] */
 
 	/* Initialize LRO */
 	rx->lro_enabled = FALSE;
@@ -3389,7 +3401,7 @@ sysctl_set_queue_rxthtime(SYSCTL_HANDLER_ARGS)
 	mvneta_rx_lockq(sc, arg->queue);
 	rx = MVNETA_RX_RING(sc, arg->queue);
 	time_mvtclk = rx->queue_th_time;
-	time_us = ((uint64_t)time_mvtclk * 1000ULL * 1000ULL) / mvneta_get_clk();
+	time_us = ((uint64_t)time_mvtclk * 1000ULL * 1000ULL) / sc->clk_freq;
 	mvneta_rx_unlockq(sc, arg->queue);
 	mvneta_sc_unlock(sc);
 
@@ -3406,8 +3418,7 @@ sysctl_set_queue_rxthtime(SYSCTL_HANDLER_ARGS)
 		mvneta_sc_unlock(sc);
 		return (EINVAL);
 	}
-	time_mvtclk =
-	    (uint64_t)mvneta_get_clk() * (uint64_t)time_us / (1000ULL * 1000ULL);
+	time_mvtclk = sc->clk_freq * (uint64_t)time_us / (1000ULL * 1000ULL);
 	rx->queue_th_time = time_mvtclk;
 	reg = MVNETA_PRXITTH_RITT(rx->queue_th_time);
 	MVNETA_WRITE(sc, MVNETA_PRXITTH(arg->queue), reg);
diff --git a/sys/dev/neta/if_mvnetavar.h b/sys/dev/neta/if_mvnetavar.h
index 0e6cf7829197..b73ced5d2fe1 100644
--- a/sys/dev/neta/if_mvnetavar.h
+++ b/sys/dev/neta/if_mvnetavar.h
@@ -258,6 +258,8 @@ struct mvneta_softc {
 	struct mtx	mtx;
 	struct resource *res[2];
 	void            *ih_cookie[1];
+	
+	uint64_t	clk_freq;
 
 	struct ifnet	*ifp;
 	uint32_t        mvneta_if_flags;