[net-next PATCH v14 07/16] net: mdio: regmap: add support for C45 read/write

Christian Marangi
2025-04-08T09:52:23Z

Add support for C45 read/write for mdio regmap. This can be done
by enabling the support_encoded_addr bool in mdio regmap config and by
using the new API devm_mdio_regmap_init to init a regmap.

To support C45, additional info needs to be appended to the regmap
address passed to regmap OPs.

The logic applied to the regmap address value:
- First the regnum value (20, 16)
- Second the devnum value (25, 21)
- A bit to signal if it's C45 (26)

devm_mdio_regmap_init MUST be used to register a regmap for this to
correctly handle internally the encode/decode of the address.

Drivers needs to define a mdio_regmap_init_config where an optional regmap
name can be defined and MUST define C22 OPs (mdio_read/write).
To support C45 operation also C45 OPs (mdio_read/write_c45).

The regmap from devm_mdio_regmap_init will internally decode the encoded
regmap address and extract the various info (addr, devnum if C45 and
regnum). It will then call the related OP and pass the extracted values to
the function.

Example for a C45 read operation:
- With an encoded address with C45 bit enabled, it will call the
.mdio_read_c45 and addr, devnum and regnum will be passed.
.mdio_read_c45 will then return the val and val will be stored in the
regmap_read pointer and will return 0. If .mdio_read_c45 returns
any error, then the regmap_read will return such error.

With support_encoded_addr enabled, also C22 will encode the address in
the regmap address and .mdio_read/write will called accordingly similar
to C45 operation.

Signed-off-by: Christian Marangi <ansuelsmth@gmail.com>
---
drivers/net/mdio/mdio-regmap.c | 170 +++++++++++++++++++++++++++++--
include/linux/mdio/mdio-regmap.h | 14 +++
2 files changed, 176 insertions(+), 8 deletions(-)

diff --git a/drivers/net/mdio/mdio-regmap.c b/drivers/net/mdio/mdio-regmap.c
index 810ba0a736f0..f263e4ae2477 100644
--- a/drivers/net/mdio/mdio-regmap.c
+++ b/drivers/net/mdio/mdio-regmap.c
@@ -15,22 +15,72 @@
#include <linux/regmap.h>
#include <linux/mdio/mdio-regmap.h>

+#define MDIO_REGMAP_C45 BIT(26)
+#define MDIO_REGMAP_ADDR GENMASK(25, 21)
+#define MDIO_REGMAP_DEVNUM GENMASK(20, 16)
+#define MDIO_REGMAP_REGNUM GENMASK(15, 0)
+
#define DRV_NAME "mdio-regmap"

struct mdio_regmap_priv {
+ void *ctx;
+
+ const struct mdio_regmap_init_config *config;
+};
+
+struct mdio_regmap_mii_priv {
struct regmap *regmap;
u32 valid_addr_mask;
+ bool encode_addr;
};

-static int mdio_regmap_read_c22(struct mii_bus *bus, int addr, int regnum)
+static int mdio_regmap_mii_read_c22(struct mii_bus *bus, int addr, int regnum)
+{
+ struct mdio_regmap_mii_priv *ctx = bus->priv;
+ unsigned int val;
+ int ret;
+
+ if (!(ctx->valid_addr_mask & BIT(addr)))
+ return -ENODEV;
+
+ if (ctx->encode_addr)
+ regnum |= FIELD_PREP(MDIO_REGMAP_ADDR, addr);
+
+ ret = regmap_read(ctx->regmap, regnum, &val);
+ if (ret < 0)
+ return ret;
+
+ return val;
+}
+
+static int mdio_regmap_mii_write_c22(struct mii_bus *bus, int addr, int regnum,
+ u16 val)
{
- struct mdio_regmap_priv *ctx = bus->priv;
+ struct mdio_regmap_mii_priv *ctx = bus->priv;
+
+ if (!(ctx->valid_addr_mask & BIT(addr)))
+ return -ENODEV;
+
+ if (ctx->encode_addr)
+ regnum |= FIELD_PREP(MDIO_REGMAP_ADDR, addr);
+
+ return regmap_write(ctx->regmap, regnum, val);
+}
+
+static int mdio_regmap_mii_read_c45(struct mii_bus *bus, int addr, int devnum,
+ int regnum)
+{
+ struct mdio_regmap_mii_priv *ctx = bus->priv;
unsigned int val;
int ret;

if (!(ctx->valid_addr_mask & BIT(addr)))
return -ENODEV;

+ regnum |= MDIO_REGMAP_C45;
+ regnum |= FIELD_PREP(MDIO_REGMAP_ADDR, addr);
+ regnum |= FIELD_PREP(MDIO_REGMAP_DEVNUM, devnum);
+
ret = regmap_read(ctx->regmap, regnum, &val);
if (ret < 0)
return ret;
@@ -38,21 +88,25 @@ static int mdio_regmap_read_c22(struct mii_bus *bus, int addr, int regnum)
return val;
}

-static int mdio_regmap_write_c22(struct mii_bus *bus, int addr, int regnum,
- u16 val)
+static int mdio_regmap_mii_write_c45(struct mii_bus *bus, int addr, int devnum,
+ int regnum, u16 val)
{
- struct mdio_regmap_priv *ctx = bus->priv;
+ struct mdio_regmap_mii_priv *ctx = bus->priv;

if (!(ctx->valid_addr_mask & BIT(addr)))
return -ENODEV;

+ regnum |= MDIO_REGMAP_C45;
+ regnum |= FIELD_PREP(MDIO_REGMAP_ADDR, addr);
+ regnum |= FIELD_PREP(MDIO_REGMAP_DEVNUM, devnum);
+
return regmap_write(ctx->regmap, regnum, val);
}

struct mii_bus *devm_mdio_regmap_register(struct device *dev,
const struct mdio_regmap_config *config)
{
- struct mdio_regmap_priv *mr;
+ struct mdio_regmap_mii_priv *mr;
struct mii_bus *mii;
int rc;

@@ -66,12 +120,17 @@ struct mii_bus *devm_mdio_regmap_register(struct device *dev,
mr = mii->priv;
mr->regmap = config->regmap;
mr->valid_addr_mask = BIT(config->valid_addr);
+ mr->encode_addr = config->support_encoded_addr;

mii->name = DRV_NAME;
strscpy(mii->id, config->name, MII_BUS_ID_SIZE);
mii->parent = config->parent;
- mii->read = mdio_regmap_read_c22;
- mii->write = mdio_regmap_write_c22;
+ mii->read = mdio_regmap_mii_read_c22;
+ mii->write = mdio_regmap_mii_write_c22;
+ if (config->support_encoded_addr) {
+ mii->read_c45 = mdio_regmap_mii_read_c45;
+ mii->write_c45 = mdio_regmap_mii_write_c45;
+ }

if (config->autoscan)
mii->phy_mask = ~mr->valid_addr_mask;
@@ -88,6 +147,101 @@ struct mii_bus *devm_mdio_regmap_register(struct device *dev,
}
EXPORT_SYMBOL_GPL(devm_mdio_regmap_register);

+static int mdio_regmap_reg_read(void *context, unsigned int reg, unsigned int *val)
+{
+ const struct mdio_regmap_init_config *config;
+ struct mdio_regmap_priv *priv = context;
+ int addr, regnum;
+ int ret;
+
+ config = priv->config;
+
+ addr = FIELD_GET(MDIO_REGMAP_ADDR, reg);
+ regnum = FIELD_GET(MDIO_REGMAP_REGNUM, reg);
+
+ if (reg & MDIO_REGMAP_C45) {
+ int devnum;
+
+ if (!config->mdio_write_c45)
+ return -EOPNOTSUPP;
+
+ devnum = FIELD_GET(MDIO_REGMAP_DEVNUM, reg);
+ ret = config->mdio_read_c45(priv->ctx, addr, devnum, regnum);
+ } else {
+ ret = config->mdio_read(priv->ctx, addr, regnum);
+ }
+
+ if (ret < 0)
+ return ret;
+
+ *val = ret;
+ return 0;
+}
+
+static int mdio_regmap_reg_write(void *context, unsigned int reg, unsigned int val)
+{
+ const struct mdio_regmap_init_config *config;
+ struct mdio_regmap_priv *priv = context;
+ int addr, regnum;
+
+ config = priv->config;
+
+ addr = FIELD_GET(MDIO_REGMAP_ADDR, reg);
+ regnum = FIELD_GET(MDIO_REGMAP_REGNUM, reg);
+
+ if (reg & MDIO_REGMAP_C45) {
+ int devnum;
+
+ if (!config->mdio_write_c45)
+ return -EOPNOTSUPP;
+
+ devnum = FIELD_GET(MDIO_REGMAP_DEVNUM, reg);
+ return config->mdio_write_c45(priv->ctx, addr, devnum, regnum, val);
+ }
+
+ return config->mdio_write(priv->ctx, addr, regnum, val);
+}
+
+static const struct regmap_config mdio_regmap_default_config = {
+ .reg_bits = 26,
+ .val_bits = 16,
+ .reg_stride = 1,
+ .max_register = MDIO_REGMAP_C45 | MDIO_REGMAP_ADDR |
+ MDIO_REGMAP_DEVNUM | MDIO_REGMAP_REGNUM,
+ .reg_read = mdio_regmap_reg_read,
+ .reg_write = mdio_regmap_reg_write,
+ /* Locking MUST be handled in mdio_write/read(_c45) */
+ .disable_locking = true,
+};
+
+struct regmap *devm_mdio_regmap_init(struct device *dev, void *priv,
+ const struct mdio_regmap_init_config *config)
+{
+ struct mdio_regmap_priv *mdio_regmap_priv;
+ struct regmap_config regmap_config;
+
+ /* Validate config */
+ if (!config->mdio_read || !config->mdio_write) {
+ dev_err(dev, ".mdio_read and .mdio_write MUST be defined in config\n");
+ return ERR_PTR(-EINVAL);
+ }
+
+ mdio_regmap_priv = devm_kzalloc(dev, sizeof(*mdio_regmap_priv),
+ GFP_KERNEL);
+ if (!mdio_regmap_priv)
+ return ERR_PTR(-ENOMEM);
+
+ memcpy(&regmap_config, &mdio_regmap_default_config, sizeof(regmap_config));
+ regmap_config.name = config->name;
+
+ mdio_regmap_priv->ctx = priv;
+ mdio_regmap_priv->config = config;
+
+ return devm_regmap_init(dev, NULL, mdio_regmap_priv,
+ &regmap_config);
+}
+EXPORT_SYMBOL_GPL(devm_mdio_regmap_init);
+
MODULE_DESCRIPTION("MDIO API over regmap");
MODULE_AUTHOR("Maxime Chevallier <maxime.chevallier@bootlin.com>");
MODULE_LICENSE("GPL");
diff --git a/include/linux/mdio/mdio-regmap.h b/include/linux/mdio/mdio-regmap.h
index 679d9069846b..504fa2046043 100644
--- a/include/linux/mdio/mdio-regmap.h
+++ b/include/linux/mdio/mdio-regmap.h
@@ -17,10 +17,24 @@ struct mdio_regmap_config {
struct regmap *regmap;
char name[MII_BUS_ID_SIZE];
u8 valid_addr;
+ /* devm_mdio_regmap_init is required with this enabled */
+ bool support_encoded_addr;
bool autoscan;
};

struct mii_bus *devm_mdio_regmap_register(struct device *dev,
const struct mdio_regmap_config *config);

+struct mdio_regmap_init_config {
+ const char *name;
+
+ int (*mdio_read)(void *ctx, int addr, int regnum);
+ int (*mdio_write)(void *ctx, int addr, int regnum, u16 val);
+ int (*mdio_read_c45)(void *ctx, int addr, int devnum, int regnum);
+ int (*mdio_write_c45)(void *ctx, int addr, int devnum, int regnum, u16 val);
+};
+
+struct regmap *devm_mdio_regmap_init(struct device *dev, void *priv,
+ const struct mdio_regmap_init_config *config);
+
#endif
--
2.48.1