git: 4ea0c3f04f42 - main - ofw_cpu: collapse some #ifdef code

From: Mitchell Horne <mhorne_at_FreeBSD.org>
Date: Fri, 10 Jan 2025 19:18:30 UTC
The branch main has been updated by mhorne:

URL: https://cgit.FreeBSD.org/src/commit/?id=4ea0c3f04f42119dff92317c0e4cef52350ed9db

commit 4ea0c3f04f42119dff92317c0e4cef52350ed9db
Author:     Mitchell Horne <mhorne@FreeBSD.org>
AuthorDate: 2025-01-10 18:46:43 +0000
Commit:     Mitchell Horne <mhorne@FreeBSD.org>
CommitDate: 2025-01-10 18:55:03 +0000

    ofw_cpu: collapse some #ifdef code
    
    Mainly, to avoid repeating the list of architectures, #define HAS_CLK.
    
    Further, split the clk code into a helper function, which is a stub in
    the !HAS_CLK case. This aids in overall legibility.
    
    While here, add one separating whitespace, again for legibility.
    
    Reviewed by:    jhb
    Sponsored by:   The FreeBSD Foundation
    Differential Revision:  https://reviews.freebsd.org/D48149
---
 sys/dev/ofw/ofw_cpu.c | 43 +++++++++++++++++++++++++++----------------
 1 file changed, 27 insertions(+), 16 deletions(-)

diff --git a/sys/dev/ofw/ofw_cpu.c b/sys/dev/ofw/ofw_cpu.c
index cbca8caee186..339716a946ff 100644
--- a/sys/dev/ofw/ofw_cpu.c
+++ b/sys/dev/ofw/ofw_cpu.c
@@ -44,6 +44,7 @@
 
 #if defined(__arm__) || defined(__arm64__) || defined(__riscv)
 #include <dev/clk/clk.h>
+#define	HAS_CLK
 #endif
 
 static int	ofw_cpulist_probe(device_t);
@@ -198,6 +199,30 @@ ofw_cpu_probe(device_t dev)
 	return (0);
 }
 
+static int
+get_freq_from_clk(device_t dev, struct ofw_cpu_softc *sc)
+{
+#ifdef HAS_CLK
+	clk_t cpuclk;
+	uint64_t freq;
+	int rv;
+
+	rv = clk_get_by_ofw_index(dev, 0, 0, &cpuclk);
+	if (rv == 0) {
+		rv = clk_get_freq(cpuclk, &freq);
+		if (rv != 0 && bootverbose)
+			device_printf(dev,
+			    "Cannot get freq of property clocks\n");
+		else
+			sc->sc_nominal_mhz = freq / 1000000;
+	}
+
+	return (rv);
+#else
+	return (ENODEV);
+#endif
+}
+
 static int
 ofw_cpu_attach(device_t dev)
 {
@@ -206,10 +231,6 @@ ofw_cpu_attach(device_t dev)
 	phandle_t node;
 	pcell_t cell;
 	int rv;
-#if defined(__arm__) || defined(__arm64__) || defined(__riscv)
-	clk_t cpuclk;
-	uint64_t freq;
-#endif
 
 	sc = device_get_softc(dev);
 	psc = device_get_softc(device_get_parent(dev));
@@ -276,18 +297,7 @@ ofw_cpu_attach(device_t dev)
 	sc->sc_cpu_pcpu = pcpu_find(device_get_unit(dev));
 
 	if (OF_getencprop(node, "clock-frequency", &cell, sizeof(cell)) < 0) {
-#if defined(__arm__) || defined(__arm64__) || defined(__riscv)
-		rv = clk_get_by_ofw_index(dev, 0, 0, &cpuclk);
-		if (rv == 0) {
-			rv = clk_get_freq(cpuclk, &freq);
-			if (rv != 0 && bootverbose)
-				device_printf(dev,
-				    "Cannot get freq of property clocks\n");
-			else
-				sc->sc_nominal_mhz = freq / 1000000;
-		} else
-#endif
-		{
+		if (get_freq_from_clk(dev, sc) != 0) {
 			if (bootverbose)
 				device_printf(dev,
 				    "missing 'clock-frequency' property\n");
@@ -298,6 +308,7 @@ ofw_cpu_attach(device_t dev)
 	if (sc->sc_nominal_mhz != 0 && bootverbose)
 		device_printf(dev, "Nominal frequency %dMhz\n",
 		    sc->sc_nominal_mhz);
+
 	bus_identify_children(dev);
 	bus_attach_children(dev);
 	return (0);