[PATCH 1/4] mfd: ocelot: Select the chip through match data

From: Jens Emil Schulz Østergaard

Date: Mon Nov 23 2026 - 08:26:22 EST


The SPI interface bootstrap, the chip reset and the child devices are
hardcoded for the VSC7512. Describe the serial interface configuration
target, its registers, the IF_CFGSTAT readback check and the padding
byte calculation in SPI match data, and select the soft reset register
and the child devices in the core by the chip it names. This prepares
for adding the LAN9645x switch family.

The VSC7512 soft reset defines get a VSC7512 prefix.

The probe now fails for a device bound without match data, otherwise
there is no functional change.

Signed-off-by: Jens Emil Schulz Østergaard <jensemil.schulzostergaard@xxxxxxxxxxxxx>
---
drivers/mfd/ocelot-core.c | 48 +++++++++++++++++++++++------
drivers/mfd/ocelot-spi.c | 77 +++++++++++++++++++++++++++++++++--------------
drivers/mfd/ocelot.h | 6 ++++
3 files changed, 100 insertions(+), 31 deletions(-)

diff --git a/drivers/mfd/ocelot-core.c b/drivers/mfd/ocelot-core.c
index 41aff2708854..2f53122fb964 100644
--- a/drivers/mfd/ocelot-core.c
+++ b/drivers/mfd/ocelot-core.c
@@ -28,9 +28,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,11 +84,23 @@
#define VSC7512_GCB_RST_SLEEP_US 100
#define VSC7512_GCB_RST_TIMEOUT_US 100000

+struct ocelot_soft_rst {
+ unsigned int reg;
+ u32 mask;
+};
+
+static const struct ocelot_soft_rst ocelot_soft_rst[] = {
+ [OCELOT_CHIP_VSC7512] = {
+ .reg = VSC7512_GCB_SOFT_RST,
+ .mask = VSC7512_SOFT_CHIP_RST,
+ },
+};
+
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);
+ err = regmap_read(ddata->gcb_regmap, ocelot_soft_rst[ddata->chip].reg, &val);
if (err)
return err;

@@ -98,15 +110,18 @@ static int ocelot_gcb_chip_rst_status(struct ocelot_ddata *ddata)
int ocelot_chip_reset(struct device *dev)
{
struct ocelot_ddata *ddata = dev_get_drvdata(dev);
+ const struct ocelot_soft_rst *rst;
int ret, val;

+ rst = &ocelot_soft_rst[ddata->chip];
+
/*
* Reset the entire chip here to put it into a completely known state.
* Other drivers may want to reset their own subsystems. The register
* 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, rst->reg, rst->mask);
if (ret)
return ret;

@@ -197,6 +212,18 @@ static const struct mfd_cell vsc7512_devs[] = {
},
};

+struct ocelot_cells {
+ const struct mfd_cell *cells;
+ unsigned int num_cells;
+};
+
+static const struct ocelot_cells ocelot_cells[] = {
+ [OCELOT_CHIP_VSC7512] = {
+ .cells = vsc7512_devs,
+ .num_cells = ARRAY_SIZE(vsc7512_devs),
+ },
+};
+
static void ocelot_core_try_add_regmap(struct device *dev,
const struct resource *res)
{
@@ -217,14 +244,17 @@ 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 ocelot_cells *c;
+ int i;

- ndevs = ARRAY_SIZE(vsc7512_devs);
+ c = &ocelot_cells[ddata->chip];

- for (i = 0; i < ndevs; i++)
- ocelot_core_try_add_regmaps(dev, &vsc7512_devs[i]);
+ for (i = 0; i < c->num_cells; i++)
+ ocelot_core_try_add_regmaps(dev, &c->cells[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, c->cells, c->num_cells,
+ 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..e803d0363e62 100644
--- a/drivers/mfd/ocelot-spi.c
+++ b/drivers/mfd/ocelot-spi.c
@@ -50,7 +50,42 @@ static const struct resource vsc7512_gcb_resource =
VSC7512_CHIP_REGS_RES_SIZE,
"devcpu_gcb_chip_regs");

-static int ocelot_spi_initialize(struct device *dev)
+struct ocelot_spi_data {
+ enum ocelot_chip chip;
+ const struct resource *cpuorg_res;
+ unsigned int if_ctrl;
+ unsigned int if_cfgstat;
+ u32 if_cfgstat_si;
+ const struct resource *gcb_res;
+ int (*padding_bytes)(u32 max_speed_hz);
+};
+
+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 const struct ocelot_spi_data vsc7512_spi_data = {
+ .chip = OCELOT_CHIP_VSC7512,
+ .cpuorg_res = &vsc7512_dev_cpuorg_resource,
+ .if_ctrl = REG_DEV_CPUORG_IF_CTRL,
+ .if_cfgstat = REG_DEV_CPUORG_IF_CFGSTAT,
+ .if_cfgstat_si = CFGSTAT_IF_NUM_SI,
+ .gcb_res = &vsc7512_gcb_resource,
+ .padding_bytes = vsc7512_spi_padding_bytes,
+};
+
+static int ocelot_spi_initialize(struct device *dev,
+ const struct ocelot_spi_data *data)
{
struct ocelot_ddata *ddata = dev_get_drvdata(dev);
u32 val, check;
@@ -80,7 +115,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, data->if_ctrl, val);
if (err)
return err;

@@ -90,7 +125,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, data->if_cfgstat, val);
if (err)
return err;

@@ -109,9 +144,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 | data->if_cfgstat_si;

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

@@ -196,28 +231,26 @@ EXPORT_SYMBOL_NS(ocelot_spi_init_regmap, "MFD_OCELOT_SPI");

static int ocelot_spi_probe(struct spi_device *spi)
{
+ const struct ocelot_spi_data *data;
struct device *dev = &spi->dev;
struct ocelot_ddata *ddata;
struct regmap *r;
int err;

+ data = spi_get_device_match_data(spi);
+ if (!data)
+ return -ENODEV;
+
ddata = devm_kzalloc_obj(dev, *ddata);
if (!ddata)
return -ENOMEM;

+ ddata->chip = data->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;
-
+ ddata->spi_padding_bytes = data->padding_bytes(spi->max_speed_hz);
+ 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 +262,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, data->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, data->gcb_res);
if (IS_ERR(r))
return PTR_ERR(r);

@@ -246,7 +279,7 @@ static int ocelot_spi_probe(struct spi_device *spi)
* This must be done before calling init, and after a chip reset is
* performed.
*/
- err = ocelot_spi_initialize(dev);
+ err = ocelot_spi_initialize(dev, data);
if (err)
return dev_err_probe(dev, err, "Error initializing SPI bus\n");

@@ -258,7 +291,7 @@ static int ocelot_spi_probe(struct spi_device *spi)
* A chip reset will clear the SPI configuration, so it needs to be done
* again before we can access any registers.
*/
- err = ocelot_spi_initialize(dev);
+ err = ocelot_spi_initialize(dev, data);
if (err)
return dev_err_probe(dev, err, "Error initializing SPI bus after reset\n");

@@ -270,13 +303,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 = (kernel_ulong_t)&vsc7512_spi_data },
{ }
};
MODULE_DEVICE_TABLE(spi, ocelot_spi_ids);

static const struct of_device_id ocelot_spi_of_match[] = {
- { .compatible = "mscc,vsc7512" },
+ { .compatible = "mscc,vsc7512", .data = &vsc7512_spi_data },
{ }
};
MODULE_DEVICE_TABLE(of, ocelot_spi_of_match);
diff --git a/drivers/mfd/ocelot.h b/drivers/mfd/ocelot.h
index b8bc2f1486e2..92b5b893ff9b 100644
--- a/drivers/mfd/ocelot.h
+++ b/drivers/mfd/ocelot.h
@@ -10,8 +10,13 @@ struct device;
struct regmap;
struct resource;

+enum ocelot_chip {
+ OCELOT_CHIP_VSC7512,
+};
+
/**
* 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
@@ -24,6 +29,7 @@ struct resource;
* data of a SPI read operation.
*/
struct ocelot_ddata {
+ enum ocelot_chip chip;
struct regmap *gcb_regmap;
struct regmap *cpuorg_regmap;
int spi_padding_bytes;

--
2.52.0