Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 1 Apr 2024 23:47:33 GMT
From:      "Bjoern A. Zeeb" <bz@FreeBSD.org>
To:        src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org
Subject:   git: d1aff3556656 - stable/14 - vf_i2c: split up and add ACPI attachments in addition to FDT
Message-ID:  <202404012347.431NlX9x084172@gitrepo.freebsd.org>

next in thread | raw e-mail | index | archive | help
The branch stable/14 has been updated by bz:

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

commit d1aff3556656bf85a2a0f07963264ad5d2ddc704
Author:     Pierre-Luc Drouin <pldrouin@pldrouin.net>
AuthorDate: 2024-03-22 22:12:07 +0000
Commit:     Bjoern A. Zeeb <bz@FreeBSD.org>
CommitDate: 2024-04-01 23:46:10 +0000

    vf_i2c: split up and add ACPI attachments in addition to FDT
    
    Move the code from the arm specific to the iicbus controller directory.
    Split up between general logic and bus attachment code.
    Add support for ACPI attachment in addition to FDT.
    
    Tested by:      bz (LS1088a FDT), Pierre-Luc Drouin (Honeycomb, ACPI)
    Based on:       D24917 by Val Packett (initial early version)
    
    (cherry picked from commit 5ca8e32633c4ffbbcd6762e5888b6a4ba0708c6c)
---
 sys/arm/freescale/vybrid/files.vybrid              |   3 +-
 sys/conf/files.arm64                               |   4 +-
 .../iicbus/controller}/vybrid/vf_i2c.c             | 240 +++++++++++----------
 sys/dev/iicbus/controller/vybrid/vf_i2c.h          |  75 +++++++
 sys/dev/iicbus/controller/vybrid/vf_i2c_acpi.c     | 101 +++++++++
 sys/dev/iicbus/controller/vybrid/vf_i2c_fdt.c      | 122 +++++++++++
 sys/modules/Makefile                               |   2 +
 sys/modules/vf_i2c/Makefile                        |  14 ++
 8 files changed, 440 insertions(+), 121 deletions(-)

diff --git a/sys/arm/freescale/vybrid/files.vybrid b/sys/arm/freescale/vybrid/files.vybrid
index b39cbbfd83cf..dff5892e9931 100644
--- a/sys/arm/freescale/vybrid/files.vybrid
+++ b/sys/arm/freescale/vybrid/files.vybrid
@@ -9,7 +9,8 @@ arm/freescale/vybrid/vf_edma.c			standard
 arm/freescale/vybrid/vf_dmamux.c		standard
 arm/freescale/vybrid/vf_port.c			standard
 arm/freescale/vybrid/vf_adc.c			standard
-arm/freescale/vybrid/vf_i2c.c			optional	iicbus
+dev/iicbus/controller/vybrid/vf_i2c.c		optional	iicbus
+dev/iicbus/controller/vybrid/vf_i2c_fdt.c	optional	iicbus fdt
 arm/freescale/vybrid/vf_tcon.c			optional	vt
 arm/freescale/vybrid/vf_dcu4.c			optional	vt
 arm/freescale/vybrid/vf_nfc.c			optional	nand
diff --git a/sys/conf/files.arm64 b/sys/conf/files.arm64
index 15ae804bfb0f..218571d0e23a 100644
--- a/sys/conf/files.arm64
+++ b/sys/conf/files.arm64
@@ -628,7 +628,9 @@ tegra210_xusb.fw				optional tegra210_xusb_fw	\
 	clean		"tegra210_xusb.fw"
 
 # NXP
-arm/freescale/vybrid/vf_i2c.c			optional vf_i2c iicbus soc_nxp_ls
+dev/iicbus/controller/vybrid/vf_i2c.c		optional vf_i2c iicbus soc_nxp_ls
+dev/iicbus/controller/vybrid/vf_i2c_acpi.c	optional vf_i2c iicbus acpi soc_nxp_ls
+dev/iicbus/controller/vybrid/vf_i2c_fdt.c	optional vf_i2c iicbus fdt soc_nxp_ls
 arm64/qoriq/qoriq_dw_pci.c			optional pci fdt soc_nxp_ls
 arm64/qoriq/qoriq_gpio_pic.c			optional gpio fdt soc_nxp_ls
 arm64/qoriq/qoriq_therm.c			optional pci fdt soc_nxp_ls
diff --git a/sys/arm/freescale/vybrid/vf_i2c.c b/sys/dev/iicbus/controller/vybrid/vf_i2c.c
similarity index 75%
rename from sys/arm/freescale/vybrid/vf_i2c.c
rename to sys/dev/iicbus/controller/vybrid/vf_i2c.c
index 0bda13ef31d2..7a6ec3948e13 100644
--- a/sys/arm/freescale/vybrid/vf_i2c.c
+++ b/sys/dev/iicbus/controller/vybrid/vf_i2c.c
@@ -2,7 +2,7 @@
  * SPDX-License-Identifier: BSD-2-Clause
  *
  * Copyright (c) 2014 Ruslan Bukin <br@bsdpad.com>
- * All rights reserved.
+ * Copyright (c) 2024 Pierre-Luc Drouin <pldrouin@pldrouin.net>
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -29,13 +29,14 @@
 /*
  * Vybrid Family Inter-Integrated Circuit (I2C)
  * Chapter 48, Vybrid Reference Manual, Rev. 5, 07/2013
+ *
+ * The current implementation is based on the original driver by Ruslan Bukin,
+ * later modified by Dawid Górecki, and split into FDT and ACPI drivers by Val
+ * Packett.
  */
 
-/*
- * This driver is based on the I2C driver for i.MX
- */
-
-#include <sys/cdefs.h>
+#include <sys/types.h>
+#include <sys/mutex.h>
 #include <sys/param.h>
 #include <sys/systm.h>
 #include <sys/bus.h>
@@ -51,17 +52,11 @@
 
 #include "iicbus_if.h"
 
-#include <dev/ofw/openfirm.h>
-#include <dev/ofw/ofw_bus.h>
-#include <dev/ofw/ofw_bus_subr.h>
-
-#include <dev/extres/clk/clk.h>
-
 #include <machine/bus.h>
 #include <machine/cpu.h>
 #include <machine/intr.h>
 
-#include <arm/freescale/vybrid/vf_common.h>
+#include <dev/iicbus/controller/vybrid/vf_i2c.h>
 
 #define	I2C_IBAD	0x0	/* I2C Bus Address Register */
 #define	I2C_IBFD	0x1	/* I2C Bus Frequency Divider Register */
@@ -86,42 +81,44 @@
 #define	 IBIC_BIIE		(1 << 7) /* Bus Idle Interrupt Enable bit. */
 #define	I2C_IBDBG	0x6	/* I2C Bus Debug Register */
 
+#define DIV_REG_UNSET	0xFF
+
+#define	READ1(_sc, _reg) bus_space_read_1(_sc->bst, _sc->bsh, _reg)
+#define	WRITE1(_sc, _reg, _val)	bus_space_write_1(_sc->bst,\
+		_sc->bsh, _reg, _val)
+
 #ifdef DEBUG
 #define vf_i2c_dbg(_sc, fmt, args...) \
 	device_printf((_sc)->dev, fmt, ##args)
+#ifdef DEBUG2
+#undef WRITE1
+#define WRITE1(_sc, _reg, _val) ({\
+		vf_i2c_dbg(_sc, "WRITE1 REG 0x%02X VAL 0x%02X\n",_reg,_val);\
+		bus_space_write_1(_sc->bst, _sc->bsh, _reg, _val);\
+		})
+#undef READ1
+#define READ1(_sc, _reg) ({\
+		uint32_t ret=bus_space_read_1(_sc->bst, _sc->bsh, _reg);\
+		vf_i2c_dbg(_sc, "READ1 REG 0x%02X RETURNS 0x%02X\n",_reg,ret);\
+		ret;\
+		})
+#endif
 #else
 #define vf_i2c_dbg(_sc, fmt, args...)
 #endif
 
-#define	HW_UNKNOWN	0x00
-#define	HW_MVF600	0x01
-#define	HW_VF610	0x02
-
 static int i2c_repeated_start(device_t, u_char, int);
 static int i2c_start(device_t, u_char, int);
 static int i2c_stop(device_t);
 static int i2c_reset(device_t, u_char, u_char, u_char *);
 static int i2c_read(device_t, char *, int, int *, int, int);
 static int i2c_write(device_t, const char *, int, int *, int);
-static phandle_t i2c_get_node(device_t, device_t);
 
 struct i2c_div_type {
 	uint32_t reg_val;
 	uint32_t div;
 };
 
-struct i2c_softc {
-	struct resource		*res[2];
-	bus_space_tag_t		bst;
-	bus_space_handle_t	bsh;
-	clk_t			clock;
-	uint32_t		freq;
-	device_t		dev;
-	device_t		iicbus;
-	struct mtx		mutex;
-	uintptr_t		hwtype;
-};
-
 static struct resource_spec i2c_spec[] = {
 	{ SYS_RES_MEMORY,	0,	RF_ACTIVE },
 	{ SYS_RES_IRQ,		0,	RF_ACTIVE },
@@ -144,49 +141,15 @@ static struct i2c_div_type vf610_div_table[] = {
 	{ 0x3F, 3840 }, { 0x7B, 4096 }, { 0x7D, 5120 }, { 0x7E, 6144 },
 };
 
-static const struct ofw_compat_data i2c_compat_data[] = {
-	{"fsl,mvf600-i2c",	HW_MVF600},
-	{"fsl,vf610-i2c",	HW_VF610},
-	{NULL,			HW_UNKNOWN}
-};
-
-static int
-i2c_probe(device_t dev)
-{
-
-	if (!ofw_bus_status_okay(dev))
-		return (ENXIO);
-
-	if (!ofw_bus_search_compatible(dev, i2c_compat_data)->ocd_data)
-		return (ENXIO);
-
-	device_set_desc(dev, "Vybrid Family Inter-Integrated Circuit (I2C)");
-	return (BUS_PROBE_DEFAULT);
-}
-
-static int
-i2c_attach(device_t dev)
+int
+vf_i2c_attach_common(device_t dev)
 {
-	struct i2c_softc *sc;
-	phandle_t node;
+	struct vf_i2c_softc *sc;
 	int error;
 
 	sc = device_get_softc(dev);
-	sc->dev = dev;
-	sc->hwtype = ofw_bus_search_compatible(dev, i2c_compat_data)->ocd_data;
-	node = ofw_bus_get_node(dev);
 
-	error = clk_get_by_ofw_index(dev, node, 0, &sc->clock);
-	if (error != 0) {
-		sc->freq = 0;
-		device_printf(dev, "Parent clock not found.\n");
-	} else {
-		if (OF_hasprop(node, "clock-frequency"))
-			OF_getencprop(node, "clock-frequency", &sc->freq,
-			    sizeof(sc->freq));
-		else
-			sc->freq = 100000;
-	}
+	vf_i2c_dbg(sc, "i2c attach common\n");
 
 	mtx_init(&sc->mutex, device_get_nameunit(dev), "I2C", MTX_DEF);
 
@@ -201,8 +164,25 @@ i2c_attach(device_t dev)
 	sc->bst = rman_get_bustag(sc->res[0]);
 	sc->bsh = rman_get_bushandle(sc->res[0]);
 
+	mtx_lock(&sc->mutex);
+
 	WRITE1(sc, I2C_IBIC, IBIC_BIIE);
 
+	if (sc->freq == 0) {
+		uint8_t div_reg;
+
+		div_reg = READ1(sc, I2C_IBFD);
+
+		if (div_reg != 0x00) {
+			sc->freq = UINT32_MAX;
+			device_printf(dev, "Using existing bus frequency divider register value (0x%02X).\n", div_reg);
+		} else {
+			device_printf(dev, "Bus frequency divider value appears unset, defaulting to low I2C bus speed.\n");
+		}
+	}
+
+	mtx_unlock(&sc->mutex);
+
 	sc->iicbus = device_add_child(dev, "iicbus", -1);
 	if (sc->iicbus == NULL) {
 		device_printf(dev, "could not add iicbus child");
@@ -219,10 +199,18 @@ i2c_attach(device_t dev)
 static int
 i2c_detach(device_t dev)
 {
-	struct i2c_softc *sc;
+	struct vf_i2c_softc *sc;
 	int error = 0;
 
 	sc = device_get_softc(dev);
+	vf_i2c_dbg(sc, "i2c detach\n");
+
+	mtx_lock(&sc->mutex);
+
+	if (sc->freq == 0) {
+		vf_i2c_dbg(sc, "Writing 0x00 to clock divider register\n");
+		WRITE1(sc, I2C_IBFD, 0x00);
+	}
 
 	error = bus_generic_detach(dev);
 	if (error != 0) {
@@ -238,6 +226,8 @@ i2c_detach(device_t dev)
 
 	bus_release_resources(dev, i2c_spec, sc->res);
 
+	mtx_unlock(&sc->mutex);
+
 	mtx_destroy(&sc->mutex);
 
 	return (0);
@@ -245,7 +235,7 @@ i2c_detach(device_t dev)
 
 /* Wait for transfer interrupt flag */
 static int
-wait_for_iif(struct i2c_softc *sc)
+wait_for_iif(struct vf_i2c_softc *sc)
 {
 	int retry;
 
@@ -263,7 +253,7 @@ wait_for_iif(struct i2c_softc *sc)
 
 /* Wait for free bus */
 static int
-wait_for_nibb(struct i2c_softc *sc)
+wait_for_nibb(struct vf_i2c_softc *sc)
 {
 	int retry;
 
@@ -279,7 +269,7 @@ wait_for_nibb(struct i2c_softc *sc)
 
 /* Wait for transfer complete+interrupt flag */
 static int
-wait_for_icf(struct i2c_softc *sc)
+wait_for_icf(struct vf_i2c_softc *sc)
 {
 	int retry;
 
@@ -298,8 +288,9 @@ wait_for_icf(struct i2c_softc *sc)
 }
 /* Get ACK bit from last write */
 static bool
-tx_acked(struct i2c_softc *sc)
+tx_acked(struct vf_i2c_softc *sc)
 {
+	vf_i2c_dbg(sc, "i2c get ACK bit from last write\n");
 
 	return (READ1(sc, I2C_IBSR) & IBSR_RXAK) ? false : true;
 
@@ -308,7 +299,7 @@ tx_acked(struct i2c_softc *sc)
 static int
 i2c_repeated_start(device_t dev, u_char slave, int timeout)
 {
-	struct i2c_softc *sc;
+	struct vf_i2c_softc *sc;
 	int error;
 	int reg;
 
@@ -356,7 +347,7 @@ i2c_repeated_start(device_t dev, u_char slave, int timeout)
 static int
 i2c_start(device_t dev, u_char slave, int timeout)
 {
-	struct i2c_softc *sc;
+	struct vf_i2c_softc *sc;
 	int error;
 	int reg;
 
@@ -406,7 +397,7 @@ i2c_start(device_t dev, u_char slave, int timeout)
 static int
 i2c_stop(device_t dev)
 {
-	struct i2c_softc *sc;
+	struct vf_i2c_softc *sc;
 
 	sc = device_get_softc(dev);
 
@@ -429,45 +420,53 @@ i2c_stop(device_t dev)
 	return (IIC_NOERR);
 }
 
-static uint32_t
+static uint8_t
 i2c_get_div_val(device_t dev)
 {
-	struct i2c_softc *sc;
-	uint64_t clk_freq;
-	int error, i;
+	struct vf_i2c_softc *sc;
+	uint8_t div_reg = DIV_REG_UNSET;
 
 	sc = device_get_softc(dev);
 
+	if (sc->freq == UINT32_MAX)
+		return div_reg;
+#ifndef FDT
+	div_reg = vf610_div_table[nitems(vf610_div_table) - 1].reg_val;
+#else
 	if (sc->hwtype == HW_MVF600)
-		return 20;
-
-	if (sc->freq == 0)
-		return vf610_div_table[nitems(vf610_div_table) - 1].reg_val;
-
-	error = clk_get_freq(sc->clock, &clk_freq);
-	if (error != 0) {
-		device_printf(dev, "Could not get parent clock frequency. "
-		    "Using default divider.\n");
-		return vf610_div_table[nitems(vf610_div_table) - 1].reg_val;
+		div_reg = MVF600_DIV_REG;
+	else if (sc->freq == 0)
+		div_reg = vf610_div_table[nitems(vf610_div_table) - 1].reg_val;
+	else {
+		uint64_t clk_freq;
+		int error, i;
+
+		error = clk_get_freq(sc->clock, &clk_freq);
+		if (error != 0) {
+			device_printf(dev, "Could not get parent clock frequency. "
+					"Using default divider.\n");
+			div_reg = vf610_div_table[nitems(vf610_div_table) - 1].reg_val;
+		} else {
+
+			for (i = 0; i < nitems(vf610_div_table) - 1; i++)
+				if ((clk_freq / vf610_div_table[i].div) <= sc->freq)
+					break;
+			div_reg = vf610_div_table[i].reg_val;
+		}
 	}
-
-	for (i = 0; i < nitems(vf610_div_table) - 1; i++)
-		if ((clk_freq / vf610_div_table[i].div) <= sc->freq)
-			break;
-
-	return vf610_div_table[i].reg_val;
+#endif
+	vf_i2c_dbg(sc, "Writing 0x%02X to clock divider register\n", div_reg);
+	return div_reg;
 }
 
 static int
 i2c_reset(device_t dev, u_char speed, u_char addr, u_char *oldadr)
 {
-	struct i2c_softc *sc;
-	uint32_t div;
+	struct vf_i2c_softc *sc;
+	uint8_t div_reg;
 
 	sc = device_get_softc(dev);
-	div = i2c_get_div_val(dev);
-	vf_i2c_dbg(sc, "Div val: %02x\n", div);
-
+	div_reg = i2c_get_div_val(dev);
 	vf_i2c_dbg(sc, "i2c reset\n");
 
 	switch (speed) {
@@ -484,7 +483,9 @@ i2c_reset(device_t dev, u_char speed, u_char addr, u_char *oldadr)
 
 	DELAY(1000);
 
-	WRITE1(sc, I2C_IBFD, div);
+	if(div_reg != DIV_REG_UNSET)
+		WRITE1(sc, I2C_IBFD, div_reg);
+
 	WRITE1(sc, I2C_IBCR, 0x0); /* Enable i2c */
 
 	DELAY(1000);
@@ -497,7 +498,7 @@ i2c_reset(device_t dev, u_char speed, u_char addr, u_char *oldadr)
 static int
 i2c_read(device_t dev, char *buf, int len, int *read, int last, int delay)
 {
-	struct i2c_softc *sc;
+	struct vf_i2c_softc *sc;
 	int error;
 
 	sc = device_get_softc(dev);
@@ -549,7 +550,7 @@ i2c_read(device_t dev, char *buf, int len, int *read, int last, int delay)
 static int
 i2c_write(device_t dev, const char *buf, int len, int *sent, int timeout)
 {
-	struct i2c_softc *sc;
+	struct vf_i2c_softc *sc;
 	int error;
 
 	sc = device_get_softc(dev);
@@ -580,20 +581,20 @@ i2c_write(device_t dev, const char *buf, int len, int *sent, int timeout)
 	return (IIC_NOERR);
 }
 
-static phandle_t
-i2c_get_node(device_t bus, device_t dev)
-{
-
-	return ofw_bus_get_node(bus);
-}
-
 static device_method_t i2c_methods[] = {
-	DEVMETHOD(device_probe,			i2c_probe),
-	DEVMETHOD(device_attach,		i2c_attach),
+	/* Device interface */
 	DEVMETHOD(device_detach,		i2c_detach),
 
-	DEVMETHOD(ofw_bus_get_node,		i2c_get_node),
+	/* Device interface */
+	DEVMETHOD(bus_setup_intr,		bus_generic_setup_intr),
+	DEVMETHOD(bus_teardown_intr,		bus_generic_teardown_intr),
+	DEVMETHOD(bus_alloc_resource,		bus_generic_alloc_resource),
+	DEVMETHOD(bus_release_resource,		bus_generic_release_resource),
+	DEVMETHOD(bus_activate_resource,	bus_generic_activate_resource),
+	DEVMETHOD(bus_deactivate_resource,	bus_generic_deactivate_resource),
+	DEVMETHOD(bus_adjust_resource,		bus_generic_adjust_resource),
 
+	/* iicbus interface */
 	DEVMETHOD(iicbus_callback,		iicbus_null_callback),
 	DEVMETHOD(iicbus_repeated_start,	i2c_repeated_start),
 	DEVMETHOD(iicbus_start,			i2c_start),
@@ -602,10 +603,11 @@ static device_method_t i2c_methods[] = {
 	DEVMETHOD(iicbus_read,			i2c_read),
 	DEVMETHOD(iicbus_write,			i2c_write),
 	DEVMETHOD(iicbus_transfer,		iicbus_transfer_gen),
-	{ 0, 0 }
+	DEVMETHOD_END
 };
 
-static DEFINE_CLASS_0(i2c, i2c_driver, i2c_methods, sizeof(struct i2c_softc));
-DRIVER_MODULE(vybrid_i2c, simplebus, i2c_driver, 0, 0);
-DRIVER_MODULE(iicbus, i2c, iicbus_driver, 0, 0);
-DRIVER_MODULE(ofw_iicbus, i2c, ofw_iicbus_driver, 0, 0);
+driver_t vf_i2c_driver = {
+	"i2c",
+	i2c_methods,
+	sizeof(struct vf_i2c_softc),
+};
diff --git a/sys/dev/iicbus/controller/vybrid/vf_i2c.h b/sys/dev/iicbus/controller/vybrid/vf_i2c.h
new file mode 100644
index 000000000000..8be6e2d86324
--- /dev/null
+++ b/sys/dev/iicbus/controller/vybrid/vf_i2c.h
@@ -0,0 +1,75 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2014 Ruslan Bukin <br@bsdpad.com>
+ * Copyright (c) 2024 Pierre-Luc Drouin <pldrouin@pldrouin.net>
+ *
+ * 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.
+ */
+
+/*
+ * Vybrid Family Inter-Integrated Circuit (I2C)
+ * Chapter 48, Vybrid Reference Manual, Rev. 5, 07/2013
+ *
+ * The current implementation is based on the original driver by Ruslan Bukin,
+ * later modified by Dawid Górecki, and split into FDT and ACPI drivers by Val
+ * Packett.
+ */
+
+#ifndef __VF_I2C_H__
+#define __VF_I2C_H__
+
+#include "opt_acpi.h"
+#include "opt_platform.h"
+
+#ifdef FDT
+#include <dev/extres/clk/clk.h>
+#endif
+
+#define VF_I2C_DEVSTR "Vybrid Family Inter-Integrated Circuit (I2C)"
+
+#define HW_MVF600       0x01
+#define HW_VF610        0x02
+
+#define MVF600_DIV_REG	0x14
+
+#define VF_I2C_DEFAULT_BUS_SPEED	100000
+
+struct vf_i2c_softc {
+	struct resource         *res[2];
+	bus_space_tag_t         bst;
+	bus_space_handle_t      bsh;
+	uint32_t                freq;
+	device_t                dev;
+	device_t                iicbus;
+	struct mtx              mutex;
+	uintptr_t               hwtype;
+#ifdef FDT
+	clk_t                   clock;
+#endif
+};
+
+extern driver_t vf_i2c_driver;
+
+device_attach_t vf_i2c_attach_common;
+
+#endif /* !__VF_I2C_H__ */
diff --git a/sys/dev/iicbus/controller/vybrid/vf_i2c_acpi.c b/sys/dev/iicbus/controller/vybrid/vf_i2c_acpi.c
new file mode 100644
index 000000000000..03f5c99a45c6
--- /dev/null
+++ b/sys/dev/iicbus/controller/vybrid/vf_i2c_acpi.c
@@ -0,0 +1,101 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2024 Pierre-Luc Drouin <pldrouin@pldrouin.net>
+ *
+ * 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.
+ */
+
+/*
+ * Vybrid Family Inter-Integrated Circuit (I2C)
+ * Chapter 48, Vybrid Reference Manual, Rev. 5, 07/2013
+ *
+ * The current implementation is based on the original driver by Ruslan Bukin,
+ * later modified by Dawid Górecki, and split into FDT and ACPI drivers by Val
+ * Packett.
+ */
+
+#include "opt_acpi.h"
+
+#include <sys/param.h>
+#include <sys/systm.h>
+#include <sys/bus.h>
+#include <sys/kernel.h>
+#include <sys/module.h>
+#include <sys/rman.h>
+
+#include <contrib/dev/acpica/include/acpi.h>
+#include <contrib/dev/acpica/include/accommon.h>
+
+#include <dev/acpica/acpivar.h>
+#include <dev/iicbus/iicbus.h>
+#include <dev/iicbus/iiconf.h>
+
+#include <dev/iicbus/controller/vybrid/vf_i2c.h>
+
+static char *vf_i2c_ids[] = {
+	"NXP0001",
+	NULL
+};
+
+static int
+vf_i2c_acpi_probe(device_t dev)
+{
+	int rv;
+
+	if (acpi_disabled("vf_i2c"))
+		return (ENXIO);
+
+	rv = ACPI_ID_PROBE(device_get_parent(dev), dev, vf_i2c_ids, NULL);
+	if (rv > 0)
+		return (rv);
+
+	device_set_desc(dev, VF_I2C_DEVSTR);
+	return (BUS_PROBE_DEFAULT);
+}
+
+static int
+vf_i2c_acpi_attach(device_t dev)
+{
+	struct vf_i2c_softc *sc;
+
+	sc = device_get_softc(dev);
+	sc->dev = dev;
+	sc->hwtype = HW_VF610;
+	sc->freq = 0;
+
+	return (vf_i2c_attach_common(dev));
+}
+
+static device_method_t vf_i2c_acpi_methods[] = {
+	/* Device interface */
+	DEVMETHOD(device_probe,                 vf_i2c_acpi_probe),
+	DEVMETHOD(device_attach,                vf_i2c_acpi_attach),
+	DEVMETHOD_END
+};
+
+DEFINE_CLASS_1(vf_i2c_acpi, vf_i2c_acpi_driver, vf_i2c_acpi_methods,
+		sizeof(struct vf_i2c_softc), vf_i2c_driver);
+
+DRIVER_MODULE(vf_i2c_acpi, acpi, vf_i2c_acpi_driver, 0, 0);
+DRIVER_MODULE(iicbus, vf_i2c_acpi, iicbus_driver, 0, 0);
+DRIVER_MODULE(acpi_iicbus, vf_i2c_acpi, acpi_iicbus_driver, 0, 0);
diff --git a/sys/dev/iicbus/controller/vybrid/vf_i2c_fdt.c b/sys/dev/iicbus/controller/vybrid/vf_i2c_fdt.c
new file mode 100644
index 000000000000..935f389ab516
--- /dev/null
+++ b/sys/dev/iicbus/controller/vybrid/vf_i2c_fdt.c
@@ -0,0 +1,122 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2014 Ruslan Bukin <br@bsdpad.com>
+ * Copyright (c) 2024 Pierre-Luc Drouin <pldrouin@pldrouin.net>
+ *
+ * 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.
+ */
+
+/*
+ * Vybrid Family Inter-Integrated Circuit (I2C)
+ * Chapter 48, Vybrid Reference Manual, Rev. 5, 07/2013
+ *
+ * The current implementation is based on the original driver by Ruslan Bukin,
+ * later modified by Dawid Górecki, and split into FDT and ACPI drivers by Val
+ * Packett.
+ */
+
+#include <sys/param.h>
+#include <sys/systm.h>
+#include <sys/bus.h>
+#include <sys/kernel.h>
+#include <sys/module.h>
+#include <sys/malloc.h>
+#include <sys/rman.h>
+
+#include <dev/ofw/openfirm.h>
+#include <dev/ofw/ofw_bus.h>
+#include <dev/ofw/ofw_bus_subr.h>
+
+#include <dev/iicbus/iicbus.h>
+
+#include <dev/iicbus/controller/vybrid/vf_i2c.h>
+
+static const struct ofw_compat_data vf_i2c_compat_data[] = {
+	{"fsl,mvf600-i2c",    HW_MVF600},
+	{"fsl,vf610-i2c",     HW_VF610},
+	{NULL,                0}
+};
+
+static int
+vf_i2c_fdt_probe(device_t dev)
+{
+
+	if (!ofw_bus_status_okay(dev))
+		return (ENXIO);
+
+	if (ofw_bus_search_compatible(dev, vf_i2c_compat_data)->ocd_data == 0)
+		return (ENXIO);
+
+	device_set_desc(dev, VF_I2C_DEVSTR);
+	return (BUS_PROBE_DEFAULT);
+}
+
+static int
+vf_i2c_fdt_attach(device_t dev)
+{
+	struct vf_i2c_softc *sc;
+	phandle_t node;
+	int error;
+
+	sc = device_get_softc(dev);
+	sc->dev = dev;
+	sc->hwtype = ofw_bus_search_compatible(dev, vf_i2c_compat_data)->ocd_data;
+	node = ofw_bus_get_node(dev);
+
+	error = clk_get_by_ofw_index(dev, node, 0, &sc->clock);
+	if (error != 0) {
+		sc->freq = 0;
+		device_printf(dev, "Parent clock not found.\n");
+	} else {
+		if (OF_hasprop(node, "clock-frequency"))
+			OF_getencprop(node, "clock-frequency", &sc->freq,
+					sizeof(sc->freq));
+		else
+			sc->freq = VF_I2C_DEFAULT_BUS_SPEED;
+	}
+	return (vf_i2c_attach_common(dev));
+}
+
+static phandle_t
+vf_i2c_get_node(device_t bus, device_t dev)
+{
+	return (ofw_bus_get_node(bus));
+}
+
+static device_method_t vf_i2c_fdt_methods[] = {
+	/* Device interface */
+	DEVMETHOD(device_probe,                 vf_i2c_fdt_probe),
+	DEVMETHOD(device_attach,                vf_i2c_fdt_attach),
+
+	/* ofw_bus interface */
+	DEVMETHOD(ofw_bus_get_node,             vf_i2c_get_node),
+	DEVMETHOD_END
+};
+
+DEFINE_CLASS_1(vf_i2c_fdt, vf_i2c_fdt_driver, vf_i2c_fdt_methods,
+		sizeof(struct vf_i2c_softc), vf_i2c_driver);
+
+DRIVER_MODULE(vf_i2c_fdt, simplebus, vf_i2c_fdt_driver, 0, 0);
+DRIVER_MODULE(iicbus, vf_i2c_fdt, iicbus_driver, 0, 0);
+DRIVER_MODULE(ofw_iicbus, vf_i2c_fdt, ofw_iicbus_driver, 0, 0);
+
diff --git a/sys/modules/Makefile b/sys/modules/Makefile
index 4dedd440017f..b6327ec224af 100644
--- a/sys/modules/Makefile
+++ b/sys/modules/Makefile
@@ -396,6 +396,7 @@ SUBDIR=	\
 	unionfs \
 	usb \
 	${_vesa} \
+	${_vf_i2c} \
 	virtio \
 	vge \
 	${_viawd} \
@@ -686,6 +687,7 @@ _dpaa2=		dpaa2
 _sff=		sff
 _em=		em
 _hyperv=  hyperv
+_vf_i2c=	vf_i2c
 
 .if !empty(OPT_FDT)
 _allwinner=	allwinner
diff --git a/sys/modules/vf_i2c/Makefile b/sys/modules/vf_i2c/Makefile
new file mode 100644
index 000000000000..53c9f9948ac8
--- /dev/null
+++ b/sys/modules/vf_i2c/Makefile
@@ -0,0 +1,14 @@
+.PATH: ${SRCTOP}/sys/dev/iicbus/controller/vybrid
+
+KMOD=	vf_i2c
+SRCS=	vf_i2c.c vf_i2c.h
+SRCS+=	device_if.h bus_if.h iicbus_if.h
+SRCS+=	opt_acpi.h opt_platform.h
+
+SRCS.DEV_ACPI=	vf_i2c_acpi.c acpi_if.h
+
+.if !empty(OPT_FDT)
+SRCS+=	vf_i2c_fdt.c ofw_bus_if.h clknode_if.h
+.endif
+
+.include <bsd.kmod.mk>



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?202404012347.431NlX9x084172>