mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH 1/5] nvmem: add support for post processing
@ 2023-02-02 17:33 Lucas Stach
  2023-02-02 17:33 ` [PATCH 2/5] nvmem: ocotp: switch to nvmem_regmap_register Lucas Stach
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: Lucas Stach @ 2023-02-02 17:33 UTC (permalink / raw)
  To: barebox

This is a port of the Linux commit 5008062f1c3f ("nvmem: core: add nvmem
cell post processing callback"). It looks a little different, as Linux
switched to create nvmem cells at registration time, effectively
deduplicating the cells, but then needed to introduce nvmem_cells_entry
to be able to store the lookup name, which is used by the post-processing.

As Barebox simply created a nvmem cell per lookup, as Linux did before
e888d445ac33 ("nvmem: resolve cells from DT at registration time"), we
can simply store the lookup name in the cell.

Signed-off-by: Lucas Stach <l.stach@pengutronix.de>
---
 drivers/nvmem/core.c           | 12 ++++++++++++
 include/linux/nvmem-provider.h |  6 ++++++
 2 files changed, 18 insertions(+)

diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c
index cd3328a650d6..e0110296f87b 100644
--- a/drivers/nvmem/core.c
+++ b/drivers/nvmem/core.c
@@ -26,10 +26,12 @@ struct nvmem_device {
 	bool			read_only;
 	struct cdev		cdev;
 	void			*priv;
+	nvmem_cell_post_process_t cell_post_process;
 };
 
 struct nvmem_cell {
 	const char		*name;
+	const char		*id;
 	int			offset;
 	int			bytes;
 	int			bit_offset;
@@ -145,6 +147,7 @@ static struct nvmem_cell *nvmem_find_cell(const char *cell_id)
 static void nvmem_cell_drop(struct nvmem_cell *cell)
 {
 	list_del(&cell->node);
+	kfree(cell->id);
 	kfree(cell);
 }
 
@@ -209,6 +212,7 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config)
 	np = config->cdev ? config->cdev->device_node : config->dev->of_node;
 	nvmem->dev.of_node = np;
 	nvmem->priv = config->priv;
+	nvmem->cell_post_process = config->cell_post_process;
 
 	if (config->read_only || !config->bus->write || of_property_read_bool(np, "read-only"))
 		nvmem->read_only = true;
@@ -417,6 +421,7 @@ struct nvmem_cell *of_nvmem_cell_get(struct device_node *np,
 	cell->offset = be32_to_cpup(addr++);
 	cell->bytes = be32_to_cpup(addr);
 	cell->name = cell_np->name;
+	cell->id = kstrdup_const(name, GFP_KERNEL);
 
 	addr = of_get_property(cell_np, "bits", &len);
 	if (addr && len == (2 * sizeof(u32))) {
@@ -534,6 +539,13 @@ static int __nvmem_cell_read(struct nvmem_device *nvmem,
 	if (cell->bit_offset || cell->nbits)
 		nvmem_shift_read_buffer_in_place(cell, buf);
 
+	if (nvmem->cell_post_process) {
+		rc = nvmem->cell_post_process(nvmem->priv, cell->id,
+					      cell->offset, buf, cell->bytes);
+		if (rc)
+			return rc;
+	}
+
 	*len = cell->bytes;
 
 	return 0;
diff --git a/include/linux/nvmem-provider.h b/include/linux/nvmem-provider.h
index 1d4e1b75b204..2f130e51791c 100644
--- a/include/linux/nvmem-provider.h
+++ b/include/linux/nvmem-provider.h
@@ -22,6 +22,11 @@ struct nvmem_bus {
 	int (*read)(void *ctx, unsigned int reg, void *val, size_t val_size);
 };
 
+/* used for vendor specific post processing of cell data */
+typedef int (*nvmem_cell_post_process_t)(void *priv, const char *id,
+					 unsigned int offset, void *buf,
+					 size_t bytes);
+
 struct nvmem_config {
 	struct device		*dev;
 	const char		*name;
@@ -32,6 +37,7 @@ struct nvmem_config {
 	int			size;
 	const struct nvmem_bus	*bus;
 	void			*priv;
+	nvmem_cell_post_process_t cell_post_process;
 };
 
 struct regmap;
-- 
2.39.1




^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH 2/5] nvmem: ocotp: switch to nvmem_regmap_register
  2023-02-02 17:33 [PATCH 1/5] nvmem: add support for post processing Lucas Stach
@ 2023-02-02 17:33 ` Lucas Stach
  2023-02-02 17:33 ` [PATCH 3/5] nvmem: regmap: allow to register with post processing Lucas Stach
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Lucas Stach @ 2023-02-02 17:33 UTC (permalink / raw)
  To: barebox

Signed-off-by: Lucas Stach <l.stach@pengutronix.de>
---
 drivers/nvmem/ocotp.c | 30 +-----------------------------
 1 file changed, 1 insertion(+), 29 deletions(-)

diff --git a/drivers/nvmem/ocotp.c b/drivers/nvmem/ocotp.c
index e75fac75eab8..78c9f9726db0 100644
--- a/drivers/nvmem/ocotp.c
+++ b/drivers/nvmem/ocotp.c
@@ -131,7 +131,6 @@ struct ocotp_priv {
 	struct regmap_config map_config;
 	const struct imx_ocotp_data *data;
 	int  mac_offset_idx;
-	struct nvmem_config config;
 };
 
 static struct ocotp_priv *imx_ocotp;
@@ -682,20 +681,6 @@ static int imx_ocotp_init_dt(struct ocotp_priv *priv)
 	return imx8m_feat_ctrl_init(priv->dev.parent, tester4, priv->data->feat);
 }
 
-static int imx_ocotp_write(void *ctx, unsigned offset, const void *val, size_t bytes)
-{
-	struct ocotp_priv *priv = ctx;
-
-	return regmap_bulk_write(priv->map, offset, val, bytes);
-}
-
-static int imx_ocotp_read(void *ctx, unsigned offset, void *val, size_t bytes)
-{
-	struct ocotp_priv *priv = ctx;
-
-	return regmap_bulk_read(priv->map, offset, val, bytes);
-}
-
 static void imx_ocotp_set_unique_machine_id(void)
 {
 	uint32_t unique_id_parts[UNIQUE_ID_NUM];
@@ -709,11 +694,6 @@ static void imx_ocotp_set_unique_machine_id(void)
 	machine_id_set_hashable(unique_id_parts, sizeof(unique_id_parts));
 }
 
-static const struct nvmem_bus imx_ocotp_nvmem_bus = {
-	.write = imx_ocotp_write,
-	.read  = imx_ocotp_read,
-};
-
 static int imx_ocotp_probe(struct device *dev)
 {
 	struct resource *iores;
@@ -751,15 +731,7 @@ static int imx_ocotp_probe(struct device *dev)
 	if (IS_ERR(priv->map))
 		return PTR_ERR(priv->map);
 
-	priv->config.name = "imx-ocotp";
-	priv->config.dev = dev;
-	priv->config.priv = priv;
-	priv->config.stride = 4;
-	priv->config.word_size = 4;
-	priv->config.size = data->num_regs;
-	priv->config.bus = &imx_ocotp_nvmem_bus;
-
-	nvmem = nvmem_register(&priv->config);
+	nvmem = nvmem_regmap_register(priv->map, "imx-ocotp");
 	if (IS_ERR(nvmem))
 		return PTR_ERR(nvmem);
 
-- 
2.39.1




^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH 3/5] nvmem: regmap: allow to register with post processing
  2023-02-02 17:33 [PATCH 1/5] nvmem: add support for post processing Lucas Stach
  2023-02-02 17:33 ` [PATCH 2/5] nvmem: ocotp: switch to nvmem_regmap_register Lucas Stach
@ 2023-02-02 17:33 ` Lucas Stach
  2023-02-02 17:33 ` [PATCH 4/5] nvmem: ocotp: add post processing for MAC cells Lucas Stach
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Lucas Stach @ 2023-02-02 17:33 UTC (permalink / raw)
  To: barebox

Add a new registration function that allows to fill the cell_post_process
function pointer.

Signed-off-by: Lucas Stach <l.stach@pengutronix.de>
---
 drivers/nvmem/regmap.c         | 10 +++++++++-
 include/linux/nvmem-provider.h |  9 +++++++++
 2 files changed, 18 insertions(+), 1 deletion(-)

diff --git a/drivers/nvmem/regmap.c b/drivers/nvmem/regmap.c
index 56611819a2b4..58b3fe647b28 100644
--- a/drivers/nvmem/regmap.c
+++ b/drivers/nvmem/regmap.c
@@ -58,7 +58,9 @@ static struct nvmem_bus nvmem_regmap_bus = {
 	.write = nvmem_regmap_write,
 };
 
-struct nvmem_device *nvmem_regmap_register(struct regmap *map, const char *name)
+struct nvmem_device *
+nvmem_regmap_register_with_pp(struct regmap *map, const char *name,
+			      nvmem_cell_post_process_t cell_post_process)
 {
 	struct nvmem_config config = {};
 
@@ -73,6 +75,12 @@ struct nvmem_device *nvmem_regmap_register(struct regmap *map, const char *name)
 	config.word_size = 1;
 	config.size = regmap_get_max_register(map) * regmap_get_reg_stride(map);
 	config.bus = &nvmem_regmap_bus;
+	config.cell_post_process = cell_post_process;
 
 	return nvmem_register(&config);
 }
+
+struct nvmem_device *nvmem_regmap_register(struct regmap *map, const char *name)
+{
+	return nvmem_regmap_register_with_pp(map, name, NULL);
+}
diff --git a/include/linux/nvmem-provider.h b/include/linux/nvmem-provider.h
index 2f130e51791c..43fe49648e66 100644
--- a/include/linux/nvmem-provider.h
+++ b/include/linux/nvmem-provider.h
@@ -47,6 +47,8 @@ struct cdev;
 
 struct nvmem_device *nvmem_register(const struct nvmem_config *cfg);
 struct nvmem_device *nvmem_regmap_register(struct regmap *regmap, const char *name);
+struct nvmem_device *nvmem_regmap_register_with_pp(struct regmap *regmap,
+		const char *name, nvmem_cell_post_process_t cell_post_process);
 struct nvmem_device *nvmem_partition_register(struct cdev *cdev);
 
 #else
@@ -61,6 +63,13 @@ static inline struct nvmem_device *nvmem_regmap_register(struct regmap *regmap,
 	return ERR_PTR(-ENOSYS);
 }
 
+static inline struct nvmem_device *
+nvmem_regmap_register_with_pp(struct regmap *regmap, const char *name,
+			      nvmem_cell_post_process_t cell_post_process)
+{
+	return ERR_PTR(-ENOSYS);
+}
+
 static inline struct nvmem_device *nvmem_partition_register(struct cdev *cdev)
 {
 	return ERR_PTR(-ENOSYS);
-- 
2.39.1




^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH 4/5] nvmem: ocotp: add post processing for MAC cells
  2023-02-02 17:33 [PATCH 1/5] nvmem: add support for post processing Lucas Stach
  2023-02-02 17:33 ` [PATCH 2/5] nvmem: ocotp: switch to nvmem_regmap_register Lucas Stach
  2023-02-02 17:33 ` [PATCH 3/5] nvmem: regmap: allow to register with post processing Lucas Stach
@ 2023-02-02 17:33 ` Lucas Stach
  2023-02-02 17:33 ` [PATCH 5/5] ARM: dts: i.MX8MP: drop OCOTP MAC address provider Lucas Stach
  2023-02-06  7:43 ` [PATCH 1/5] nvmem: add support for post processing Sascha Hauer
  4 siblings, 0 replies; 6+ messages in thread
From: Lucas Stach @ 2023-02-02 17:33 UTC (permalink / raw)
  To: barebox

Add a nvmem post process callback to swap the MAC address as required
when read via nvmem.

Signed-off-by: Lucas Stach <l.stach@pengutronix.de>
---
 drivers/nvmem/ocotp.c | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)

diff --git a/drivers/nvmem/ocotp.c b/drivers/nvmem/ocotp.c
index 78c9f9726db0..b478ece30680 100644
--- a/drivers/nvmem/ocotp.c
+++ b/drivers/nvmem/ocotp.c
@@ -641,6 +641,21 @@ static struct regmap_bus imx_ocotp_regmap_bus = {
 	.reg_read = imx_ocotp_reg_read,
 };
 
+static int imx_ocotp_cell_pp(void *context, const char *id, unsigned int offset,
+			     void *data, size_t bytes)
+{
+	/* Deal with some post processing of nvmem cell data */
+	if (id && !strcmp(id, "mac-address")) {
+		u8 *buf = data;
+		int i;
+
+		for (i = 0; i < bytes/2; i++)
+			swap(buf[i], buf[bytes - i - 1]);
+	}
+
+	return 0;
+}
+
 static int imx_ocotp_init_dt(struct ocotp_priv *priv)
 {
 	char mac[MAC_BYTES];
@@ -731,7 +746,8 @@ static int imx_ocotp_probe(struct device *dev)
 	if (IS_ERR(priv->map))
 		return PTR_ERR(priv->map);
 
-	nvmem = nvmem_regmap_register(priv->map, "imx-ocotp");
+	nvmem = nvmem_regmap_register_with_pp(priv->map, "imx-ocotp",
+					      imx_ocotp_cell_pp);
 	if (IS_ERR(nvmem))
 		return PTR_ERR(nvmem);
 
-- 
2.39.1




^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH 5/5] ARM: dts: i.MX8MP: drop OCOTP MAC address provider
  2023-02-02 17:33 [PATCH 1/5] nvmem: add support for post processing Lucas Stach
                   ` (2 preceding siblings ...)
  2023-02-02 17:33 ` [PATCH 4/5] nvmem: ocotp: add post processing for MAC cells Lucas Stach
@ 2023-02-02 17:33 ` Lucas Stach
  2023-02-06  7:43 ` [PATCH 1/5] nvmem: add support for post processing Sascha Hauer
  4 siblings, 0 replies; 6+ messages in thread
From: Lucas Stach @ 2023-02-02 17:33 UTC (permalink / raw)
  To: barebox

Now that the MAC address can be read properly through the
NVMEM framework, there is no need for the custom Barebox
MAC address provider anymore. Both FEC and EQOS network
interfaces already reference the proper nvmem cells to fetch
their MAC address from OCOTP.

Signed-off-by: Lucas Stach <l.stach@pengutronix.de>
---
 arch/arm/dts/imx8mp-evk.dts                | 4 ----
 arch/arm/dts/imx8mp-tqma8mpql-mba8mpxl.dts | 4 ----
 2 files changed, 8 deletions(-)

diff --git a/arch/arm/dts/imx8mp-evk.dts b/arch/arm/dts/imx8mp-evk.dts
index d992b14882a3..28d8c5f9292e 100644
--- a/arch/arm/dts/imx8mp-evk.dts
+++ b/arch/arm/dts/imx8mp-evk.dts
@@ -68,7 +68,3 @@
 		reg = <0xe0000 0x20000>;
 	};
 };
-
-&ocotp {
-	barebox,provide-mac-address = <&fec 0x640>;
-};
diff --git a/arch/arm/dts/imx8mp-tqma8mpql-mba8mpxl.dts b/arch/arm/dts/imx8mp-tqma8mpql-mba8mpxl.dts
index c47e7285a703..6e81f58e2786 100644
--- a/arch/arm/dts/imx8mp-tqma8mpql-mba8mpxl.dts
+++ b/arch/arm/dts/imx8mp-tqma8mpql-mba8mpxl.dts
@@ -53,7 +53,3 @@
 		reg = <0xe0000 0x20000>;
 	};
 };
-
-&ocotp {
-	barebox,provide-mac-address = <&fec 0x640>;
-};
-- 
2.39.1




^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH 1/5] nvmem: add support for post processing
  2023-02-02 17:33 [PATCH 1/5] nvmem: add support for post processing Lucas Stach
                   ` (3 preceding siblings ...)
  2023-02-02 17:33 ` [PATCH 5/5] ARM: dts: i.MX8MP: drop OCOTP MAC address provider Lucas Stach
@ 2023-02-06  7:43 ` Sascha Hauer
  4 siblings, 0 replies; 6+ messages in thread
From: Sascha Hauer @ 2023-02-06  7:43 UTC (permalink / raw)
  To: Lucas Stach; +Cc: barebox

On Thu, Feb 02, 2023 at 06:33:08PM +0100, Lucas Stach wrote:
> This is a port of the Linux commit 5008062f1c3f ("nvmem: core: add nvmem
> cell post processing callback"). It looks a little different, as Linux
> switched to create nvmem cells at registration time, effectively
> deduplicating the cells, but then needed to introduce nvmem_cells_entry
> to be able to store the lookup name, which is used by the post-processing.
> 
> As Barebox simply created a nvmem cell per lookup, as Linux did before
> e888d445ac33 ("nvmem: resolve cells from DT at registration time"), we
> can simply store the lookup name in the cell.
> 
> Signed-off-by: Lucas Stach <l.stach@pengutronix.de>
> ---
>  drivers/nvmem/core.c           | 12 ++++++++++++
>  include/linux/nvmem-provider.h |  6 ++++++
>  2 files changed, 18 insertions(+)

Applied, thanks

Sascha

> 
> diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c
> index cd3328a650d6..e0110296f87b 100644
> --- a/drivers/nvmem/core.c
> +++ b/drivers/nvmem/core.c
> @@ -26,10 +26,12 @@ struct nvmem_device {
>  	bool			read_only;
>  	struct cdev		cdev;
>  	void			*priv;
> +	nvmem_cell_post_process_t cell_post_process;
>  };
>  
>  struct nvmem_cell {
>  	const char		*name;
> +	const char		*id;
>  	int			offset;
>  	int			bytes;
>  	int			bit_offset;
> @@ -145,6 +147,7 @@ static struct nvmem_cell *nvmem_find_cell(const char *cell_id)
>  static void nvmem_cell_drop(struct nvmem_cell *cell)
>  {
>  	list_del(&cell->node);
> +	kfree(cell->id);
>  	kfree(cell);
>  }
>  
> @@ -209,6 +212,7 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config)
>  	np = config->cdev ? config->cdev->device_node : config->dev->of_node;
>  	nvmem->dev.of_node = np;
>  	nvmem->priv = config->priv;
> +	nvmem->cell_post_process = config->cell_post_process;
>  
>  	if (config->read_only || !config->bus->write || of_property_read_bool(np, "read-only"))
>  		nvmem->read_only = true;
> @@ -417,6 +421,7 @@ struct nvmem_cell *of_nvmem_cell_get(struct device_node *np,
>  	cell->offset = be32_to_cpup(addr++);
>  	cell->bytes = be32_to_cpup(addr);
>  	cell->name = cell_np->name;
> +	cell->id = kstrdup_const(name, GFP_KERNEL);
>  
>  	addr = of_get_property(cell_np, "bits", &len);
>  	if (addr && len == (2 * sizeof(u32))) {
> @@ -534,6 +539,13 @@ static int __nvmem_cell_read(struct nvmem_device *nvmem,
>  	if (cell->bit_offset || cell->nbits)
>  		nvmem_shift_read_buffer_in_place(cell, buf);
>  
> +	if (nvmem->cell_post_process) {
> +		rc = nvmem->cell_post_process(nvmem->priv, cell->id,
> +					      cell->offset, buf, cell->bytes);
> +		if (rc)
> +			return rc;
> +	}
> +
>  	*len = cell->bytes;
>  
>  	return 0;
> diff --git a/include/linux/nvmem-provider.h b/include/linux/nvmem-provider.h
> index 1d4e1b75b204..2f130e51791c 100644
> --- a/include/linux/nvmem-provider.h
> +++ b/include/linux/nvmem-provider.h
> @@ -22,6 +22,11 @@ struct nvmem_bus {
>  	int (*read)(void *ctx, unsigned int reg, void *val, size_t val_size);
>  };
>  
> +/* used for vendor specific post processing of cell data */
> +typedef int (*nvmem_cell_post_process_t)(void *priv, const char *id,
> +					 unsigned int offset, void *buf,
> +					 size_t bytes);
> +
>  struct nvmem_config {
>  	struct device		*dev;
>  	const char		*name;
> @@ -32,6 +37,7 @@ struct nvmem_config {
>  	int			size;
>  	const struct nvmem_bus	*bus;
>  	void			*priv;
> +	nvmem_cell_post_process_t cell_post_process;
>  };
>  
>  struct regmap;
> -- 
> 2.39.1
> 
> 
> 

-- 
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |



^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2023-02-06  7:44 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-02-02 17:33 [PATCH 1/5] nvmem: add support for post processing Lucas Stach
2023-02-02 17:33 ` [PATCH 2/5] nvmem: ocotp: switch to nvmem_regmap_register Lucas Stach
2023-02-02 17:33 ` [PATCH 3/5] nvmem: regmap: allow to register with post processing Lucas Stach
2023-02-02 17:33 ` [PATCH 4/5] nvmem: ocotp: add post processing for MAC cells Lucas Stach
2023-02-02 17:33 ` [PATCH 5/5] ARM: dts: i.MX8MP: drop OCOTP MAC address provider Lucas Stach
2023-02-06  7:43 ` [PATCH 1/5] nvmem: add support for post processing Sascha Hauer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox