git: b8b7c0dda9a0 - main - qoriq_gpio: Add support for driver inheritance

From: Wojciech Macek <wma_at_FreeBSD.org>
Date: Sat, 06 Nov 2021 08:11:30 UTC
The branch main has been updated by wma:

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

commit b8b7c0dda9a085902f702f8c89271b852aa898a4
Author:     Kornel Duleba <mindal@semihalf.com>
AuthorDate: 2021-10-29 14:06:35 +0000
Commit:     Wojciech Macek <wma@FreeBSD.org>
CommitDate: 2021-11-06 08:08:45 +0000

    qoriq_gpio: Add support for driver inheritance
    
    Expose softc and other necessary things in a separate header.
    This is needed for an armv8 specific driver, that will inherit from this
    one. Driver mutex was converted to a spin lock, so that it can be later
    used in interrupt filter context.
    
    Obtained from: Semihalf
    Sponsored by: Alstom Group
    Differential revision: https://reviews.freebsd.org/D32587
---
 sys/dev/gpio/qoriq_gpio.c | 44 +++++---------------------------
 sys/dev/gpio/qoriq_gpio.h | 65 +++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 71 insertions(+), 38 deletions(-)

diff --git a/sys/dev/gpio/qoriq_gpio.c b/sys/dev/gpio/qoriq_gpio.c
index dc4813e07b8e..eb790eb50d29 100644
--- a/sys/dev/gpio/qoriq_gpio.c
+++ b/sys/dev/gpio/qoriq_gpio.c
@@ -46,40 +46,12 @@ __FBSDID("$FreeBSD$");
 #include <machine/stdarg.h>
 
 #include <dev/gpio/gpiobusvar.h>
+#include <dev/gpio/qoriq_gpio.h>
 #include <dev/ofw/ofw_bus.h>
 #include <dev/ofw/ofw_bus_subr.h>
 
 #include "gpio_if.h"
 
-#define MAXPIN		(31)
-
-#define VALID_PIN(u)	((u) >= 0 && (u) <= MAXPIN)
-#define DEFAULT_CAPS	(GPIO_PIN_INPUT | GPIO_PIN_OUTPUT | \
-			 GPIO_PIN_OPENDRAIN | GPIO_PIN_PUSHPULL)
-
-#define GPIO_LOCK(sc)			mtx_lock(&(sc)->sc_mtx)
-#define	GPIO_UNLOCK(sc)		mtx_unlock(&(sc)->sc_mtx)
-#define GPIO_LOCK_INIT(sc) \
-	mtx_init(&(sc)->sc_mtx, device_get_nameunit((sc)->dev),	\
-	    "gpio", MTX_DEF)
-#define GPIO_LOCK_DESTROY(_sc)	mtx_destroy(&_sc->sc_mtx);
-
-#define	GPIO_GPDIR	0x0
-#define	GPIO_GPODR	0x4
-#define	GPIO_GPDAT	0x8
-#define	GPIO_GPIER	0xc
-#define	GPIO_GPIMR	0x10
-#define	GPIO_GPICR	0x14
-#define	GPIO_GPIBE	0x18
-
-struct qoriq_gpio_softc {
-	device_t	dev;
-	device_t	busdev;
-	struct mtx	sc_mtx;
-	struct resource *sc_mem;	/* Memory resource */
-	struct gpio_pin	 sc_pins[MAXPIN + 1];
-};
-
 static device_t
 qoriq_gpio_get_bus(device_t dev)
 {
@@ -379,9 +351,7 @@ qoriq_gpio_map_gpios(device_t bus, phandle_t dev, phandle_t gparent, int gcells,
 	return (err);
 }
 
-static int qoriq_gpio_detach(device_t dev);
-
-static int
+int
 qoriq_gpio_attach(device_t dev)
 {
 	struct qoriq_gpio_softc *sc = device_get_softc(dev);
@@ -422,7 +392,7 @@ qoriq_gpio_attach(device_t dev)
 	return (0);
 }
 
-static int
+int
 qoriq_gpio_detach(device_t dev)
 {
 	struct qoriq_gpio_softc *sc = device_get_softc(dev);
@@ -464,13 +434,11 @@ static device_method_t qoriq_gpio_methods[] = {
 	DEVMETHOD_END
 };
 
-static driver_t qoriq_gpio_driver = {
-	"gpio",
-	qoriq_gpio_methods,
-	sizeof(struct qoriq_gpio_softc),
-};
 static devclass_t qoriq_gpio_devclass;
 
+DEFINE_CLASS_0(gpio, qoriq_gpio_driver, qoriq_gpio_methods,
+    sizeof(struct qoriq_gpio_softc));
+
 EARLY_DRIVER_MODULE(qoriq_gpio, simplebus, qoriq_gpio_driver,
     qoriq_gpio_devclass, NULL, NULL,
     BUS_PASS_RESOURCE + BUS_PASS_ORDER_MIDDLE);
diff --git a/sys/dev/gpio/qoriq_gpio.h b/sys/dev/gpio/qoriq_gpio.h
new file mode 100644
index 000000000000..6b401e1d3f44
--- /dev/null
+++ b/sys/dev/gpio/qoriq_gpio.h
@@ -0,0 +1,65 @@
+/*-
+ * Copyright (c) 2020 Alstom Group.
+ * Copyright (c) 2020 Semihalf.
+ * Copyright (c) 2015 Justin Hibbits
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $FreeBSD$
+ */
+
+#define MAXPIN	(31)
+
+#define BIT(x)	(1 << (x))
+
+#define VALID_PIN(u)	((u) >= 0 && (u) <= MAXPIN)
+#define DEFAULT_CAPS	(GPIO_PIN_INPUT | GPIO_PIN_OUTPUT | \
+			 GPIO_PIN_OPENDRAIN | GPIO_PIN_PUSHPULL)
+
+#define GPIO_LOCK(sc)		mtx_lock_spin(&(sc)->sc_mtx)
+#define	GPIO_UNLOCK(sc)		mtx_unlock_spin(&(sc)->sc_mtx)
+#define GPIO_LOCK_INIT(sc) \
+	mtx_init(&(sc)->sc_mtx, device_get_nameunit((sc)->dev),	\
+	    "gpio", MTX_SPIN)
+#define GPIO_LOCK_DESTROY(_sc)	mtx_destroy(&_sc->sc_mtx);
+
+#define	GPIO_GPDIR	0x0
+#define	GPIO_GPODR	0x4
+#define	GPIO_GPDAT	0x8
+#define	GPIO_GPIER	0xc
+#define	GPIO_GPIMR	0x10
+#define	GPIO_GPICR	0x14
+#define	GPIO_GPIBE	0x18
+
+struct qoriq_gpio_softc {
+	device_t	dev;
+	device_t	busdev;
+	struct mtx	sc_mtx;
+	struct resource *sc_mem;
+	struct gpio_pin	 sc_pins[MAXPIN + 1];
+};
+
+device_attach_t qoriq_gpio_attach;
+device_detach_t qoriq_gpio_detach;
+
+DECLARE_CLASS(qoriq_gpio_driver);