[PATCH v2 2/5] mfd: ocelot: Select the chip through match data

From: Jens Emil Schulz Østergaard

Date: Fri Dec 04 2026 - 07:03:03 EST


The SPI interface bootstrap, the chip reset and the child devices are
hardcoded for the VSC7512. Pass the chip through the SPI and OF match
data, and select the serial interface configuration target, its
registers, the IF_CFGSTAT readback value, the padding byte calculation,
the soft reset register and the child devices with a switch on the
chip. This prepares for adding the LAN9645x switch family.

The VSC7512 soft reset defines get a VSC7512 prefix. The reset is
polled with regmap_read_poll_timeout(), which returns at the first
failed read instead of retrying until the timeout.

The chip numbers start at 1, so the probe fails for a device bound
without match data. Otherwise there is no functional change.

Signed-off-by: Jens Emil Schulz Østergaard <jensemil.schulzostergaard@xxxxxxxxxxxxx>
---
Changes in v2:
- Pass the chip as an integer through the match data instead of
struct ocelot_spi_data (Lee)
- Select the per chip SPI setup with a switch in the probe, and call the
padding bytes calculation from it instead of through a function
pointer (Lee)
- Select the soft reset register and the cells with switch statements
instead of indexed tables (Lee)
- Poll the soft reset with regmap_read_poll_timeout()
- Start the chip numbers at 1, so a device without match data is
rejected
---
drivers/mfd/ocelot-core.c | 55 +++++++++++++++++++++++++-------------------
drivers/mfd/ocelot-spi.c | 58 +++++++++++++++++++++++++++++++----------------
drivers/mfd/ocelot.h | 15 ++++++++++++
3 files changed, 86 insertions(+), 42 deletions(-)

diff --git a/drivers/mfd/ocelot-core.c b/drivers/mfd/ocelot-core.c
index 7adf357e8bbd..348f613d7883 100644
--- a/drivers/mfd/ocelot-core.c
+++ b/drivers/mfd/ocelot-core.c
@@ -15,7 +15,6 @@
#include <linux/bits.h>
#include <linux/device.h>
#include <linux/export.h>
-#include <linux/iopoll.h>
#include <linux/ioport.h>
#include <linux/kernel.h>
#include <linux/mfd/core.h>
@@ -28,9 +27,9 @@

#include "ocelot.h"

-#define REG_GCB_SOFT_RST 0x0008
+#define VSC7512_GCB_SOFT_RST 0x0008

-#define BIT_SOFT_CHIP_RST BIT(0)
+#define VSC7512_SOFT_CHIP_RST BIT(0)

#define VSC7512_MIIM0_RES_START 0x7107009c
#define VSC7512_MIIM1_RES_START 0x710700c0
@@ -84,21 +83,21 @@
#define VSC7512_GCB_RST_SLEEP_US 100
#define VSC7512_GCB_RST_TIMEOUT_US 100000

-static int ocelot_gcb_chip_rst_status(struct ocelot_ddata *ddata)
-{
- int val, err;
-
- err = regmap_read(ddata->gcb_regmap, REG_GCB_SOFT_RST, &val);
- if (err)
- return err;
-
- return val;
-}
-
int ocelot_chip_reset(struct device *dev)
{
struct ocelot_ddata *ddata = dev_get_drvdata(dev);
- int ret, val;
+ unsigned int reg, val;
+ u32 mask;
+ int ret;
+
+ switch (ddata->chip) {
+ case OCELOT_CHIP_VSC7512:
+ reg = VSC7512_GCB_SOFT_RST;
+ mask = VSC7512_SOFT_CHIP_RST;
+ break;
+ default:
+ return -EINVAL;
+ }

/*
* Reset the entire chip here to put it into a completely known state.
@@ -106,12 +105,13 @@ int ocelot_chip_reset(struct device *dev)
* self-clears, so one write is all that is needed and wait for it to
* clear.
*/
- ret = regmap_write(ddata->gcb_regmap, REG_GCB_SOFT_RST, BIT_SOFT_CHIP_RST);
+ ret = regmap_write(ddata->gcb_regmap, reg, mask);
if (ret)
return ret;

- return readx_poll_timeout(ocelot_gcb_chip_rst_status, ddata, val, !val,
- VSC7512_GCB_RST_SLEEP_US, VSC7512_GCB_RST_TIMEOUT_US);
+ return regmap_read_poll_timeout(ddata->gcb_regmap, reg, val, !val,
+ VSC7512_GCB_RST_SLEEP_US,
+ VSC7512_GCB_RST_TIMEOUT_US);
}
EXPORT_SYMBOL_NS(ocelot_chip_reset, "MFD_OCELOT");

@@ -194,14 +194,23 @@ static void ocelot_core_try_add_regmaps(struct device *dev,

int ocelot_core_init(struct device *dev)
{
- int i, ndevs;
+ struct ocelot_ddata *ddata = dev_get_drvdata(dev);
+ const struct mfd_cell *devs;
+ int ndevs;

- ndevs = ARRAY_SIZE(vsc7512_devs);
+ switch (ddata->chip) {
+ case OCELOT_CHIP_VSC7512:
+ devs = vsc7512_devs;
+ ndevs = ARRAY_SIZE(vsc7512_devs);
+ break;
+ default:
+ return -EINVAL;
+ }

- for (i = 0; i < ndevs; i++)
- ocelot_core_try_add_regmaps(dev, &vsc7512_devs[i]);
+ for (int i = 0; i < ndevs; i++)
+ ocelot_core_try_add_regmaps(dev, &devs[i]);

- return devm_mfd_add_devices(dev, PLATFORM_DEVID_AUTO, vsc7512_devs, ndevs, NULL, 0, NULL);
+ return devm_mfd_add_devices(dev, PLATFORM_DEVID_AUTO, devs, ndevs, NULL, 0, NULL);
}
EXPORT_SYMBOL_NS(ocelot_core_init, "MFD_OCELOT");

diff --git a/drivers/mfd/ocelot-spi.c b/drivers/mfd/ocelot-spi.c
index 52bde8d4daf3..b02f8a5eaab9 100644
--- a/drivers/mfd/ocelot-spi.c
+++ b/drivers/mfd/ocelot-spi.c
@@ -50,6 +50,20 @@ static const struct resource vsc7512_gcb_resource =
VSC7512_CHIP_REGS_RES_SIZE,
"devcpu_gcb_chip_regs");

+static int vsc7512_spi_padding_bytes(u32 max_speed_hz)
+{
+ if (max_speed_hz <= 500000)
+ return 0;
+
+ /*
+ * Calculation taken from the manual for IF_CFGSTAT:IF_CFG. Register
+ * access time is 1us, so we need to configure and send out enough
+ * padding bytes between the read request and data transmission that
+ * lasts at least 1 microsecond.
+ */
+ return 1 + (max_speed_hz / HZ_PER_MHZ + 2) / 8;
+}
+
static int ocelot_spi_initialize(struct device *dev)
{
struct ocelot_ddata *ddata = dev_get_drvdata(dev);
@@ -80,7 +94,7 @@ static int ocelot_spi_initialize(struct device *dev)
* | 22222233 | 11112222 | 111111 | |
* | 45678901 | 67890123 | 89012345 | 01234567 |
*/
- err = regmap_write(ddata->cpuorg_regmap, REG_DEV_CPUORG_IF_CTRL, val);
+ err = regmap_write(ddata->cpuorg_regmap, ddata->if_ctrl, val);
if (err)
return err;

@@ -90,7 +104,7 @@ static int ocelot_spi_initialize(struct device *dev)
* first payload bit is shifted out too quickly, the read will fail.
*/
val = ddata->spi_padding_bytes;
- err = regmap_write(ddata->cpuorg_regmap, REG_DEV_CPUORG_IF_CFGSTAT, val);
+ err = regmap_write(ddata->cpuorg_regmap, ddata->if_cfgstat, val);
if (err)
return err;

@@ -109,9 +123,9 @@ static int ocelot_spi_initialize(struct device *dev)
* the registers are being accessed. Since we're accessing them via the
* serial interface, it must return IF_NUM_SI.
*/
- check = val | CFGSTAT_IF_NUM_SI;
+ check = val | ddata->if_cfgstat_si;

- err = regmap_read(ddata->cpuorg_regmap, REG_DEV_CPUORG_IF_CFGSTAT, &val);
+ err = regmap_read(ddata->cpuorg_regmap, ddata->if_cfgstat, &val);
if (err)
return err;

@@ -196,6 +210,7 @@ EXPORT_SYMBOL_NS(ocelot_spi_init_regmap, "MFD_OCELOT_SPI");

static int ocelot_spi_probe(struct spi_device *spi)
{
+ const struct resource *cpuorg_res, *gcb_res;
struct device *dev = &spi->dev;
struct ocelot_ddata *ddata;
struct regmap *r;
@@ -205,19 +220,24 @@ static int ocelot_spi_probe(struct spi_device *spi)
if (!ddata)
return -ENOMEM;

+ ddata->chip = (uintptr_t)spi_get_device_match_data(spi);
+
+ switch (ddata->chip) {
+ case OCELOT_CHIP_VSC7512:
+ cpuorg_res = &vsc7512_dev_cpuorg_resource;
+ gcb_res = &vsc7512_gcb_resource;
+ ddata->if_ctrl = REG_DEV_CPUORG_IF_CTRL;
+ ddata->if_cfgstat = REG_DEV_CPUORG_IF_CFGSTAT;
+ ddata->if_cfgstat_si = CFGSTAT_IF_NUM_SI;
+ ddata->spi_padding_bytes = vsc7512_spi_padding_bytes(spi->max_speed_hz);
+ break;
+ default:
+ return dev_err_probe(dev, -ENODEV, "Unknown chip %d\n", ddata->chip);
+ }
+
spi_set_drvdata(spi, ddata);

- if (spi->max_speed_hz <= 500000) {
- ddata->spi_padding_bytes = 0;
- } else {
- /*
- * Calculation taken from the manual for IF_CFGSTAT:IF_CFG.
- * Register access time is 1us, so we need to configure and send
- * out enough padding bytes between the read request and data
- * transmission that lasts at least 1 microsecond.
- */
- ddata->spi_padding_bytes = 1 + (spi->max_speed_hz / HZ_PER_MHZ + 2) / 8;
-
+ if (ddata->spi_padding_bytes) {
ddata->dummy_buf = devm_kzalloc(dev, ddata->spi_padding_bytes, GFP_KERNEL);
if (!ddata->dummy_buf)
return -ENOMEM;
@@ -229,13 +249,13 @@ static int ocelot_spi_probe(struct spi_device *spi)
if (err)
return dev_err_probe(&spi->dev, err, "Error performing SPI setup\n");

- r = ocelot_spi_init_regmap(dev, &vsc7512_dev_cpuorg_resource);
+ r = ocelot_spi_init_regmap(dev, cpuorg_res);
if (IS_ERR(r))
return PTR_ERR(r);

ddata->cpuorg_regmap = r;

- r = ocelot_spi_init_regmap(dev, &vsc7512_gcb_resource);
+ r = ocelot_spi_init_regmap(dev, gcb_res);
if (IS_ERR(r))
return PTR_ERR(r);

@@ -270,13 +290,13 @@ static int ocelot_spi_probe(struct spi_device *spi)
}

static const struct spi_device_id ocelot_spi_ids[] = {
- { .name = "vsc7512" },
+ { .name = "vsc7512", .driver_data = OCELOT_CHIP_VSC7512 },
{ }
};
MODULE_DEVICE_TABLE(spi, ocelot_spi_ids);

static const struct of_device_id ocelot_spi_of_match[] = {
- { .compatible = "mscc,vsc7512" },
+ { .compatible = "mscc,vsc7512", .data = (void *)OCELOT_CHIP_VSC7512 },
{ }
};
MODULE_DEVICE_TABLE(of, ocelot_spi_of_match);
diff --git a/drivers/mfd/ocelot.h b/drivers/mfd/ocelot.h
index b8bc2f1486e2..2cfb21aa10cb 100644
--- a/drivers/mfd/ocelot.h
+++ b/drivers/mfd/ocelot.h
@@ -5,17 +5,28 @@
#define _MFD_OCELOT_H

#include <linux/kconfig.h>
+#include <linux/types.h>

struct device;
struct regmap;
struct resource;

+enum ocelot_chip {
+ OCELOT_CHIP_VSC7512 = 1,
+};
+
/**
* struct ocelot_ddata - Private data for an external Ocelot chip
+ * @chip: Chip family member.
* @gcb_regmap: General Configuration Block regmap. Used for
* operations like chip reset.
* @cpuorg_regmap: CPU Device Origin Block regmap. Used for operations
* like SPI bus configuration.
+ * @if_ctrl: Offset of IF_CTRL in the cpuorg_regmap.
+ * @if_cfgstat: Offset of IF_CFGSTAT in the cpuorg_regmap.
+ * @if_cfgstat_si: IF_CFGSTAT interface number that is expected to read
+ * back when the registers are accessed through the serial
+ * interface.
* @spi_padding_bytes: Number of padding bytes that must be thrown out before
* read data gets returned. This is calculated during
* initialization based on bus speed.
@@ -24,8 +35,12 @@ struct resource;
* data of a SPI read operation.
*/
struct ocelot_ddata {
+ enum ocelot_chip chip;
struct regmap *gcb_regmap;
struct regmap *cpuorg_regmap;
+ unsigned int if_ctrl;
+ unsigned int if_cfgstat;
+ u32 if_cfgstat_si;
int spi_padding_bytes;
void *dummy_buf;
};

--
2.52.0