Skip to contentSkip to conversation

nvmem: core: introduce NVMEM layouts

PATCH Superseded Michael Walle 4 participants 28 messages Nov 18, 2022 – Nov 23, 2022

Subsystems (7)

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v3 cover nvmem: core: introduce NVMEM layouts

This is now the third attempt to fetch the MAC addresses from the VPD for the Kontron sl28 boards. Previous discussions can be found here:
https://lore.kernel.org/lkml/[email protected]/

NVMEM cells are typically added by board code or by the devicetree. But as the cells get more complex, there is (valid) push back from the devicetree maintainers to not put that handling in the devicetree.

Therefore, introduce NVMEM layouts. They operate on the NVMEM device and can add cells during runtime. That way it is possible to add more complex cells than it is possible right now with the offset/length/bits description in the device tree. For example, you can have post processing for individual cells (think of endian swapping, or ethernet offset handling).

The imx-ocotp driver is the only user of the global post processing hook, convert it to nvmem layouts and drop the global post pocessing hook.

For now, the layouts are selected by the device tree. But the idea is that also board files or other drivers could set a layout. Although no code for that exists yet.

Thanks to Miquel, the device tree bindings are already approved and merged.

NVMEM layouts as modules?
While possible in principle, it doesn't make any sense because the NVMEM core can't be compiled as a module. The layouts needs to be available at probe time. (That is also the reason why they get registered with subsys_initcall().) So if the NVMEM core would be a module, the layouts could be modules, too.

Michael Walle (16):
net: add helper eth_addr_add()
of: base: add of_parse_phandle_with_optional_args()
nvmem: core: fix device node refcounting
nvmem: core: add an index parameter to the cell
nvmem: core: move struct nvmem_cell_info to nvmem-provider.h
nvmem: core: drop the removal of the cells in nvmem_add_cells()
nvmem: core: add nvmem_add_one_cell()
nvmem: core: use nvmem_add_one_cell() in nvmem_add_cells_from_of()
nvmem: core: introduce NVMEM layouts
nvmem: core: add per-cell post processing
nvmem: core: allow to modify a cell before adding it
nvmem: imx-ocotp: replace global post processing with layouts
nvmem: cell: drop global cell_post_process
nvmem: core: provide own priv pointer in post process callback
nvmem: layouts: add sl28vpd layout
MAINTAINERS: add myself as sl28vpd nvmem layout driver

Miquel Raynal (2):
nvmem: layouts: Add ONIE tlv layout driver
MAINTAINERS: Add myself as ONIE tlv NVMEM layout maintainer

 Documentation/driver-api/nvmem.rst |  15 ++
 MAINTAINERS                        |  12 ++
 drivers/nvmem/Kconfig              |   4 +
 drivers/nvmem/Makefile             |   1 +
 drivers/nvmem/core.c               | 281 ++++++++++++++++++++++-------
 drivers/nvmem/imx-ocotp.c          |  34 ++--
 drivers/nvmem/layouts/Kconfig      |  23 +++
 drivers/nvmem/layouts/Makefile     |   7 +
 drivers/nvmem/layouts/onie-tlv.c   | 249 +++++++++++++++++++++++++
 drivers/nvmem/layouts/sl28vpd.c    | 153 ++++++++++++++++
 include/linux/etherdevice.h        |  14 ++
 include/linux/nvmem-consumer.h     |  17 +-
 include/linux/nvmem-provider.h     |  95 +++++++++-
 include/linux/of.h                 |  25 +++
 14 files changed, 834 insertions(+), 96 deletions(-)
 create mode 100644 drivers/nvmem/layouts/Kconfig
 create mode 100644 drivers/nvmem/layouts/Makefile
 create mode 100644 drivers/nvmem/layouts/onie-tlv.c
 create mode 100644 drivers/nvmem/layouts/sl28vpd.c
Signature · 1 line
2.30.2
#1 lore raw

PATCH v3 1/18 net: add helper eth_addr_add()

Add a helper to add an offset to a ethernet address. This comes in handy if you have a base ethernet address for multiple interfaces.

---
changes since v2:
- none

changes since v1:
- none

 include/linux/etherdevice.h | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
include/linux/etherdevice.h +14 -0
index a541f0c4f146..224645f17c33 100644
@@ -507,6 +507,20 @@ static inline void eth_addr_inc(u8 *addr)
 	u64_to_ether_addr(u, addr);
 }
 
+/**
+ * eth_addr_add() - Add (or subtract) and offset to/from the given MAC address.
+ *
+ * @offset: Offset to add.
+ * @addr: Pointer to a six-byte array containing Ethernet address to increment.
+ */
+static inline void eth_addr_add(u8 *addr, long offset)
+{
+	u64 u = ether_addr_to_u64(addr);
+
+	u += offset;
+	u64_to_ether_addr(u, addr);
+}
+
 /**
  * is_etherdev_addr - Tell if given Ethernet address belongs to the device.
  * @dev: Pointer to a device structure
Signature · 1 line
2.30.2
#2 lore raw

PATCH v3 2/18 of: base: add of_parse_phandle_with_optional_args()

In reply to Michael Walle

Add a new variant of the of_parse_phandle_with_args() which treats the cells name as optional. If it's missing, it is assumed that the phandle has no arguments.

Up until now, a nvmem node didn't have any arguments, so all the device trees haven't any '#*-cells' property. But there is a need for an additional argument for the phandle, for which we need a '#*-cells' property. Therefore, we need to support nvmem nodes with and without this property.

---
changes since v2:
- none

changes since v1:
- none

 include/linux/of.h | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)
include/linux/of.h +25 -0
index 8b9f94386dc3..98c252d2d851 100644
@@ -1008,6 +1008,31 @@ static inline int of_parse_phandle_with_fixed_args(const struct device_node *np,
 					    index, out_args);
 }
 
+/**
+ * of_parse_phandle_with_optional_args() - Find a node pointed by phandle in a list
+ * @np:		pointer to a device tree node containing a list
+ * @list_name:	property name that contains a list
+ * @cells_name:	property name that specifies phandles' arguments count
+ * @index:	index of a phandle to parse out
+ * @out_args:	optional pointer to output arguments structure (will be filled)
+ *
+ * Same as of_parse_phandle_with_args() except that if the cells_name property
+ * is not found, cell_count of 0 is assumed.
+ *
+ * This is used to useful, if you have a phandle which didn't have arguments
+ * before and thus doesn't have a '#*-cells' property but is now migrated to
+ * having arguments while retaining backwards compatibility.
+ */
+static inline int of_parse_phandle_with_optional_args(const struct device_node *np,
+						      const char *list_name,
+						      const char *cells_name,
+						      int index,
+						      struct of_phandle_args *out_args)
+{
+	return __of_parse_phandle_with_args(np, list_name, cells_name,
+					    0, index, out_args);
+}
+
 /**
  * of_property_count_u8_elems - Count the number of u8 elements in a property
  *
Signature · 1 line
2.30.2
#3 lore raw

Re: [PATCH v3 02/18] of: base: add of_parse_phandle_with_optional_args()

Am 2022-11-18 19:51, schrieb Michael Walle:

Quoted 9 lines

Add a new variant of the of_parse_phandle_with_args() which treats the cells name as optional. If it's missing, it is assumed that the phandle has no arguments.

Up until now, a nvmem node didn't have any arguments, so all the device trees haven't any '#*-cells' property. But there is a need for an additional argument for the phandle, for which we need a '#*-cells' property. Therefore, we need to support nvmem nodes with and without this property.

I've just noticed that this isn't enough. We also need to fix the parsing in drivers/of/property.c, otherwise spurious device links will be created because the phandle argument is treated as a phandle itself.

-michael

#4 lore raw

PATCH 1/1 of: property: special #nvmem-cell-cells handling

Since recently, there is a new #nvmem-cell-cells. To be backwards compatible this is optional. Therefore, we need special handling and cannot use DEFINE_SIMPLE_PROP() anymore.

---
This patch will be part of the following series:
https://lore.kernel.org/linux-arm-kernel/[email protected]/

 drivers/of/property.c | 17 ++++++++++++++++-
 1 file changed, 16 insertions(+), 1 deletion(-)
drivers/of/property.c +16 -1
index 967f79b59016..93c0ea662336 100644
@@ -1305,7 +1305,6 @@ DEFINE_SIMPLE_PROP(dmas, "dmas", "#dma-cells")
 DEFINE_SIMPLE_PROP(power_domains, "power-domains", "#power-domain-cells")
 DEFINE_SIMPLE_PROP(hwlocks, "hwlocks", "#hwlock-cells")
 DEFINE_SIMPLE_PROP(extcon, "extcon", NULL)
-DEFINE_SIMPLE_PROP(nvmem_cells, "nvmem-cells", NULL)
 DEFINE_SIMPLE_PROP(phys, "phys", "#phy-cells")
 DEFINE_SIMPLE_PROP(wakeup_parent, "wakeup-parent", NULL)
 DEFINE_SIMPLE_PROP(pinctrl0, "pinctrl-0", NULL)
@@ -1381,6 +1380,22 @@ static struct device_node *parse_interrupts(struct device_node *np,
 	return of_irq_parse_one(np, index, &sup_args) ? NULL : sup_args.np;
 }
 
+static struct device_node *parse_nvmem_cells(struct device_node *np,
+					     const char *prop_name, int index)
+{
+	struct of_phandle_args sup_args;
+
+	if (strcmp(prop_name, "nvmem-cells"))
+		return NULL;
+
+	if (of_parse_phandle_with_optional_args(np, prop_name,
+						"#nvmem-cell-cells", index,
+						&sup_args))
+		return NULL;
+
+	return sup_args.np;
+}
+
 static const struct supplier_bindings of_supplier_bindings[] = {
 	{ .parse_prop = parse_clocks, },
 	{ .parse_prop = parse_interconnects, },
Signature · 1 line
2.30.2
#5 lore raw

Re: [PATCH] of: property: special #nvmem-cell-cells handling

Quoted 34 lines · Michael Walle

Since recently, there is a new #nvmem-cell-cells. To be backwards compatible this is optional. Therefore, we need special handling and cannot use DEFINE_SIMPLE_PROP() anymore.

---
This patch will be part of the following series:
https://lore.kernel.org/linux-arm-kernel/[email protected]/

 drivers/of/property.c | 17 ++++++++++++++++-
 1 file changed, 16 insertions(+), 1 deletion(-)
drivers/of/property.c +5 -1
index 967f79b59016..93c0ea662336 100644
@@ -1305,7 +1305,6 @@ DEFINE_SIMPLE_PROP(dmas, "dmas", "#dma-cells")
 DEFINE_SIMPLE_PROP(power_domains, "power-domains", "#power-domain-cells")
 DEFINE_SIMPLE_PROP(hwlocks, "hwlocks", "#hwlock-cells")
 DEFINE_SIMPLE_PROP(extcon, "extcon", NULL)
-DEFINE_SIMPLE_PROP(nvmem_cells, "nvmem-cells", NULL)
 DEFINE_SIMPLE_PROP(phys, "phys", "#phy-cells")
 DEFINE_SIMPLE_PROP(wakeup_parent, "wakeup-parent", NULL)
 DEFINE_SIMPLE_PROP(pinctrl0, "pinctrl-0", NULL)
@@ -1381,6 +1380,22 @@ static struct device_node *parse_interrupts(struct device_node *np,
        return of_irq_parse_one(np, index, &sup_args) ? NULL : sup_args.np;
 }
 
+static struct device_node *parse_nvmem_cells(struct device_node *np,
+                                            const char *prop_name, int index)
+{
+       struct of_phandle_args sup_args;
+

drivers/of/property.c static struct device_node *parse_interrupts(struct device_node *np, · line 1397

Quoted 10 lines
drivers/of/property.c +10 -0
+       if (strcmp(prop_name, "nvmem-cells"))
+               return NULL;
+
+       if (of_parse_phandle_with_optional_args(np, prop_name,
+                                               "#nvmem-cell-cells", index,
+                                               &sup_args))
+               return NULL;
+
+       return sup_args.np;
+}

There's a couple of other cases like that (MSI IIRC), so can we generalize this to work in more than 1 case?

Rob

#6 lore raw

Re: [PATCH] of: property: special #nvmem-cell-cells handling

Am 2022-11-18 22:52, schrieb Rob Herring:

Quoted 45 lines · Michael Walle
Quoted 44 lines

Since recently, there is a new #nvmem-cell-cells. To be backwards compatible this is optional. Therefore, we need special handling and cannot use DEFINE_SIMPLE_PROP() anymore.

---
This patch will be part of the following series:
https://lore.kernel.org/linux-arm-kernel/[email protected]/

 drivers/of/property.c | 17 ++++++++++++++++-
 1 file changed, 16 insertions(+), 1 deletion(-)
drivers/of/property.c +0 -1
index 967f79b59016..93c0ea662336 100644
@@ -1305,7 +1305,6 @@ DEFINE_SIMPLE_PROP(dmas, "dmas", "#dma-cells")
 DEFINE_SIMPLE_PROP(power_domains, "power-domains", 
 "#power-domain-cells")
 DEFINE_SIMPLE_PROP(hwlocks, "hwlocks", "#hwlock-cells")
 DEFINE_SIMPLE_PROP(extcon, "extcon", NULL)
-DEFINE_SIMPLE_PROP(nvmem_cells, "nvmem-cells", NULL)
 DEFINE_SIMPLE_PROP(phys, "phys", "#phy-cells")
 DEFINE_SIMPLE_PROP(wakeup_parent, "wakeup-parent", NULL)

DEFINE_SIMPLE_PROP(pinctrl0, "pinctrl-0", NULL)

drivers/of/property.c +10 -0
@@ -1381,6 +1380,22 @@ static struct device_node 
 *parse_interrupts(struct device_node *np,
        return of_irq_parse_one(np, index, &sup_args) ? NULL : 
 sup_args.np;
 }
 
+static struct device_node *parse_nvmem_cells(struct device_node *np,
+                                            const char *prop_name, 
 int index)
+{
+       struct of_phandle_args sup_args;
+
+       if (strcmp(prop_name, "nvmem-cells"))
+               return NULL;
+
+       if (of_parse_phandle_with_optional_args(np, prop_name,
+                                               "#nvmem-cell-cells", 

index,

Quoted 8 lines

drivers/of/property.c static struct device_node *parse_interrupts(struct device_node *np, · line 1397

Quoted 5 lines
drivers/of/property.c +5 -0
+                                               &sup_args))
+               return NULL;
+
+       return sup_args.np;
+}

There's a couple of other cases like that (MSI IIRC), so can we generalize this to work in more than 1 case?

You mean addding a new DEFINE_SIMPLE_PROP_OPTIONAL_ARGS()?

-michael

#7 lore raw

Re: [PATCH] of: property: special #nvmem-cell-cells handling

Quoted 56 lines · Michael Walle

Am 2022-11-18 22:52, schrieb Rob Herring:

Quoted 45 lines · Michael Walle
Quoted 44 lines

Since recently, there is a new #nvmem-cell-cells. To be backwards compatible this is optional. Therefore, we need special handling and cannot use DEFINE_SIMPLE_PROP() anymore.

---
This patch will be part of the following series:
https://lore.kernel.org/linux-arm-kernel/[email protected]/

 drivers/of/property.c | 17 ++++++++++++++++-
 1 file changed, 16 insertions(+), 1 deletion(-)
drivers/of/property.c +0 -1
index 967f79b59016..93c0ea662336 100644
@@ -1305,7 +1305,6 @@ DEFINE_SIMPLE_PROP(dmas, "dmas", "#dma-cells")
 DEFINE_SIMPLE_PROP(power_domains, "power-domains",
 "#power-domain-cells")
 DEFINE_SIMPLE_PROP(hwlocks, "hwlocks", "#hwlock-cells")
 DEFINE_SIMPLE_PROP(extcon, "extcon", NULL)
-DEFINE_SIMPLE_PROP(nvmem_cells, "nvmem-cells", NULL)
 DEFINE_SIMPLE_PROP(phys, "phys", "#phy-cells")
 DEFINE_SIMPLE_PROP(wakeup_parent, "wakeup-parent", NULL)

DEFINE_SIMPLE_PROP(pinctrl0, "pinctrl-0", NULL)

drivers/of/property.c +10 -0
@@ -1381,6 +1380,22 @@ static struct device_node
 *parse_interrupts(struct device_node *np,
        return of_irq_parse_one(np, index, &sup_args) ? NULL :
 sup_args.np;
 }
 
+static struct device_node *parse_nvmem_cells(struct device_node *np,
+                                            const char *prop_name,
 int index)
+{
+       struct of_phandle_args sup_args;
+
+       if (strcmp(prop_name, "nvmem-cells"))
+               return NULL;
+
+       if (of_parse_phandle_with_optional_args(np, prop_name,
+                                               "#nvmem-cell-cells",

index,

Quoted 8 lines

drivers/of/property.c

Quoted 5 lines
drivers/of/property.c +5 -0
+                                               &sup_args))
+               return NULL;
+
+       return sup_args.np;
+}

There's a couple of other cases like that (MSI IIRC), so can we generalize this to work in more than 1 case?

Quoted 1 line

You mean addding a new DEFINE_SIMPLE_PROP_OPTIONAL_ARGS()?

Actually, I think you can just do something like the below. I don't think we need to separately handle an optional #.*-cells and a required one. It's really just validation which we do already both with the tools and when the subsystems parse these bindings. Of course, if we need to handle cases other than 0 default cells, we'll have to restructure the define some to pass the default cells.

drivers/of/property.c +0 -0
index 967f79b59016..198f56633eb0 100644
@@ -1254,7 +1254,7 @@ static struct device_node

*parse_suffix_prop_cells(struct device_node *np,
if (strcmp_suffix(prop_name, suffix))
return NULL;

- if (of_parse_phandle_with_args(np, prop_name, cells_name, index,
+ if (__of_parse_phandle_with_args(np, prop_name, cells_name, 0, index,

                                       &sup_args))
                return NULL;
maintainer Tested-by
#8 lore raw

Re: [PATCH] of: property: special #nvmem-cell-cells handling

Hi Rob, Michael,

[email protected] wrote on Tue, 22 Nov 2022 17:44:26 -0600:

Quoted 71 lines · Michael Walle
Quoted 56 lines

Am 2022-11-18 22:52, schrieb Rob Herring:

Quoted 45 lines · Michael Walle
Quoted 44 lines

Since recently, there is a new #nvmem-cell-cells. To be backwards compatible this is optional. Therefore, we need special handling and cannot use DEFINE_SIMPLE_PROP() anymore.

---
This patch will be part of the following series:
https://lore.kernel.org/linux-arm-kernel/[email protected]/

 drivers/of/property.c | 17 ++++++++++++++++-
 1 file changed, 16 insertions(+), 1 deletion(-)
drivers/of/property.c +0 -1
index 967f79b59016..93c0ea662336 100644
@@ -1305,7 +1305,6 @@ DEFINE_SIMPLE_PROP(dmas, "dmas", "#dma-cells")
 DEFINE_SIMPLE_PROP(power_domains, "power-domains",
 "#power-domain-cells")
 DEFINE_SIMPLE_PROP(hwlocks, "hwlocks", "#hwlock-cells")
 DEFINE_SIMPLE_PROP(extcon, "extcon", NULL)
-DEFINE_SIMPLE_PROP(nvmem_cells, "nvmem-cells", NULL)
 DEFINE_SIMPLE_PROP(phys, "phys", "#phy-cells")
 DEFINE_SIMPLE_PROP(wakeup_parent, "wakeup-parent", NULL)

DEFINE_SIMPLE_PROP(pinctrl0, "pinctrl-0", NULL)

drivers/of/property.c +10 -0
@@ -1381,6 +1380,22 @@ static struct device_node
 *parse_interrupts(struct device_node *np,
        return of_irq_parse_one(np, index, &sup_args) ? NULL :
 sup_args.np;
 }
 
+static struct device_node *parse_nvmem_cells(struct device_node *np,
+                                            const char *prop_name,
 int index)
+{
+       struct of_phandle_args sup_args;
+
+       if (strcmp(prop_name, "nvmem-cells"))
+               return NULL;
+
+       if (of_parse_phandle_with_optional_args(np, prop_name,
+                                               "#nvmem-cell-cells",

index,

Quoted 8 lines

drivers/of/property.c

Quoted 5 lines
drivers/of/property.c +5 -0
+                                               &sup_args))
+               return NULL;
+
+       return sup_args.np;
+}  

There's a couple of other cases like that (MSI IIRC), so can we generalize this to work in more than 1 case?

Quoted 1 line

You mean addding a new DEFINE_SIMPLE_PROP_OPTIONAL_ARGS()?

Actually, I think you can just do something like the below. I don't think we need to separately handle an optional #.*-cells and a required one. It's really just validation which we do already both with the tools and when the subsystems parse these bindings. Of course, if we need to handle cases other than 0 default cells, we'll have to restructure the define some to pass the default cells.

drivers/of/property.c +0 -0
index 967f79b59016..198f56633eb0 100644

drivers/of/property.c static struct device_node · line 1260

Quoted 8 lines
drivers/of/property.c +1 -1
@@ -1254,7 +1254,7 @@ static struct device_node
 *parse_suffix_prop_cells(struct device_node *np,
        if (strcmp_suffix(prop_name, suffix))
                return NULL;
 
-       if (of_parse_phandle_with_args(np, prop_name, cells_name, index,
+       if (__of_parse_phandle_with_args(np, prop_name, cells_name, 0, index,
                                       &sup_args))
                return NULL;

Excellent, this small change aside with

-DEFINE_SIMPLE_PROP(nvmem_cells, "nvmem-cells", NULL) +DEFINE_SIMPLE_PROP(nvmem_cells, "nvmem-cells", "#nvmem-cell-cells")

look much simpler and work perfectly.

Thanks,
Miquèl

#9 lore raw

PATCH v3 3/18 nvmem: core: fix device node refcounting

In reply to Michael Walle

In of_nvmem_cell_get(), of_get_next_parent() is used on cell_np. This will decrement the refcount on cell_np, but cell_np is still used later in the code. Use of_get_parent() instead and of_node_put() in the appropriate places.

---
changes since v2:
- new patch

 drivers/nvmem/core.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
drivers/nvmem/core.c +8 -3
index 321d7d63e068..205a427f564d 100644
@@ -1242,16 +1242,21 @@ struct nvmem_cell *of_nvmem_cell_get(struct device_node *np, const char *id)
 	if (!cell_np)
 		return ERR_PTR(-ENOENT);
 
-	nvmem_np = of_get_next_parent(cell_np);
-	if (!nvmem_np)
+	nvmem_np = of_get_parent(cell_np);
+	if (!nvmem_np) {
+		of_node_put(cell_np);
 		return ERR_PTR(-EINVAL);
+	}
 
 	nvmem = __nvmem_device_get(nvmem_np, device_match_of_node);
 	of_node_put(nvmem_np);
-	if (IS_ERR(nvmem))
+	if (IS_ERR(nvmem)) {
+		of_node_put(cell_np);
 		return ERR_CAST(nvmem);
+	}
 
 	cell_entry = nvmem_find_cell_entry_by_node(nvmem, cell_np);
+	of_node_put(cell_np);
 	if (!cell_entry) {
 		__nvmem_device_put(nvmem);
 		return ERR_PTR(-ENOENT);
Signature · 1 line
2.30.2
#10 lore raw

PATCH v3 4/18 nvmem: core: add an index parameter to the cell

In reply to Michael Walle

Sometimes a cell can represend multiple values. For example, a base ethernet address stored in the NVMEM can be expanded into multiple discreet ones by adding an offset.

For this use case, introduce an index parameter which is then used to distiguish between values. This parameter will then be passed to the post process hook which can then use it to create different values during reading.

At the moment, there is only support for the device tree path. You can add the index to the phandle, e.g.

  &net {
          nvmem-cells = <&base_mac_address 2>;
          nvmem-cell-names = "mac-address";
  };

  &nvmem_provider {
          base_mac_address: base-mac-address@0 {
                  #nvmem-cell-cells = <1>;
                  reg = <0 6>;
          };
  };

---
changes since v2:
- none

changes since v1:
- none

 drivers/nvmem/core.c           | 37 ++++++++++++++++++++++++----------
 drivers/nvmem/imx-ocotp.c      |  4 ++--
 include/linux/nvmem-provider.h |  4 ++--
 3 files changed, 30 insertions(+), 15 deletions(-)
drivers/nvmem/core.c +26 -11
index 205a427f564d..24573e63e5a9 100644
@@ -60,6 +60,7 @@ struct nvmem_cell_entry {
 struct nvmem_cell {
 	struct nvmem_cell_entry *entry;
 	const char		*id;
+	int			index;
 };
 
 static DEFINE_MUTEX(nvmem_mutex);
@@ -1127,7 +1128,8 @@ struct nvmem_device *devm_nvmem_device_get(struct device *dev, const char *id)
 }
 EXPORT_SYMBOL_GPL(devm_nvmem_device_get);
 
-static struct nvmem_cell *nvmem_create_cell(struct nvmem_cell_entry *entry, const char *id)
+static struct nvmem_cell *nvmem_create_cell(struct nvmem_cell_entry *entry,
+					    const char *id, int index)
 {
 	struct nvmem_cell *cell;
 	const char *name = NULL;
@@ -1146,6 +1148,7 @@ static struct nvmem_cell *nvmem_create_cell(struct nvmem_cell_entry *entry, cons
 
 	cell->id = name;
 	cell->entry = entry;
+	cell->index = index;
 
 	return cell;
 }
@@ -1184,7 +1187,7 @@ nvmem_cell_get_from_lookup(struct device *dev, const char *con_id)
 				__nvmem_device_put(nvmem);
 				cell = ERR_PTR(-ENOENT);
 			} else {
-				cell = nvmem_create_cell(cell_entry, con_id);
+				cell = nvmem_create_cell(cell_entry, con_id, 0);
 				if (IS_ERR(cell))
 					__nvmem_device_put(nvmem);
 			}
@@ -1232,15 +1235,27 @@ struct nvmem_cell *of_nvmem_cell_get(struct device_node *np, const char *id)
 	struct nvmem_device *nvmem;
 	struct nvmem_cell_entry *cell_entry;
 	struct nvmem_cell *cell;
+	struct of_phandle_args cell_spec;
 	int index = 0;
+	int cell_index = 0;
+	int ret;
 
 	/* if cell name exists, find index to the name */
 	if (id)
 		index = of_property_match_string(np, "nvmem-cell-names", id);
 
-	cell_np = of_parse_phandle(np, "nvmem-cells", index);
-	if (!cell_np)
-		return ERR_PTR(-ENOENT);
+	ret = of_parse_phandle_with_optional_args(np, "nvmem-cells",
+						  "#nvmem-cell-cells",
+						  index, &cell_spec);
+	if (ret)
+		return ERR_PTR(ret);
+
+	if (cell_spec.args_count > 1)
+		return ERR_PTR(-EINVAL);
+
+	cell_np = cell_spec.np;
+	if (cell_spec.args_count)
+		cell_index = cell_spec.args[0];
 
 	nvmem_np = of_get_parent(cell_np);
 	if (!nvmem_np) {
@@ -1262,7 +1277,7 @@ struct nvmem_cell *of_nvmem_cell_get(struct device_node *np, const char *id)
 		return ERR_PTR(-ENOENT);
 	}
 
-	cell = nvmem_create_cell(cell_entry, id);
+	cell = nvmem_create_cell(cell_entry, id, cell_index);
 	if (IS_ERR(cell))
 		__nvmem_device_put(nvmem);
 
@@ -1415,8 +1430,8 @@ static void nvmem_shift_read_buffer_in_place(struct nvmem_cell_entry *cell, void
 }
 
 static int __nvmem_cell_read(struct nvmem_device *nvmem,
-		      struct nvmem_cell_entry *cell,
-		      void *buf, size_t *len, const char *id)
+			     struct nvmem_cell_entry *cell,
+			     void *buf, size_t *len, const char *id, int index)
 {
 	int rc;
 
@@ -1430,7 +1445,7 @@ static int __nvmem_cell_read(struct nvmem_device *nvmem,
 		nvmem_shift_read_buffer_in_place(cell, buf);
 
 	if (nvmem->cell_post_process) {
-		rc = nvmem->cell_post_process(nvmem->priv, id,
+		rc = nvmem->cell_post_process(nvmem->priv, id, index,
 					      cell->offset, buf, cell->bytes);
 		if (rc)
 			return rc;
@@ -1465,7 +1480,7 @@ void *nvmem_cell_read(struct nvmem_cell *cell, size_t *len)
 	if (!buf)
 		return ERR_PTR(-ENOMEM);
 
-	rc = __nvmem_cell_read(nvmem, cell->entry, buf, len, cell->id);
+	rc = __nvmem_cell_read(nvmem, cell->entry, buf, len, cell->id, cell->index);
 	if (rc) {
 		kfree(buf);
 		return ERR_PTR(rc);
@@ -1778,7 +1793,7 @@ ssize_t nvmem_device_cell_read(struct nvmem_device *nvmem,
 	if (rc)
 		return rc;
 
-	rc = __nvmem_cell_read(nvmem, &cell, buf, &len, NULL);
+	rc = __nvmem_cell_read(nvmem, &cell, buf, &len, NULL, 0);
 	if (rc)
 		return rc;
 
drivers/nvmem/imx-ocotp.c +2 -2
index 14284e866f26..e9b52ecb3f72 100644
@@ -222,8 +222,8 @@ static int imx_ocotp_read(void *context, unsigned int offset,
 	return ret;
 }
 
-static int imx_ocotp_cell_pp(void *context, const char *id, unsigned int offset,
-			     void *data, size_t bytes)
+static int imx_ocotp_cell_pp(void *context, const char *id, int index,
+			     unsigned int offset, void *data, size_t bytes)
 {
 	struct ocotp_priv *priv = context;
 
include/linux/nvmem-provider.h +2 -2
index 50caa117cb62..8f964b394292 100644
@@ -20,8 +20,8 @@ typedef int (*nvmem_reg_read_t)(void *priv, unsigned int offset,
 typedef int (*nvmem_reg_write_t)(void *priv, unsigned int offset,
 				 void *val, size_t bytes);
 /* 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);
+typedef int (*nvmem_cell_post_process_t)(void *priv, const char *id, int index,
+					 unsigned int offset, void *buf, size_t bytes);
 
 enum nvmem_type {
 	NVMEM_TYPE_UNKNOWN = 0,
Signature · 1 line
2.30.2
#11 lore raw

PATCH v3 5/18 nvmem: core: move struct nvmem_cell_info to nvmem-provider.h

In reply to Michael Walle

struct nvmem_cell_info is used to describe a cell. Thus this should really be in the nvmem-provider's header. There are two (unused) nvmem access methods which use the nvmem_cell_info to describe the cell to be accesses. One can argue, that they will create a cell before accessing, thus they are both a provider and a consumer.

struct nvmem_cell_info will get used more and more by nvmem-providers, don't force them to also include the consumer header, although they are not.

---
changes since v2:
- none

changes since v1:
- new patch

 include/linux/nvmem-consumer.h | 10 +---------
 include/linux/nvmem-provider.h | 19 ++++++++++++++++++-
 2 files changed, 19 insertions(+), 10 deletions(-)
include/linux/nvmem-consumer.h +1 -9
index 980f9c9ac0bc..1f62f7ba71ca 100644
@@ -18,15 +18,7 @@ struct device_node;
 /* consumer cookie */
 struct nvmem_cell;
 struct nvmem_device;
-
-struct nvmem_cell_info {
-	const char		*name;
-	unsigned int		offset;
-	unsigned int		bytes;
-	unsigned int		bit_offset;
-	unsigned int		nbits;
-	struct device_node	*np;
-};
+struct nvmem_cell_info;
 
 /**
  * struct nvmem_cell_lookup - cell lookup entry
include/linux/nvmem-provider.h +18 -1
index 8f964b394292..14a32a1bc249 100644
@@ -14,7 +14,6 @@
 #include <linux/gpio/consumer.h>
 
 struct nvmem_device;
-struct nvmem_cell_info;
 typedef int (*nvmem_reg_read_t)(void *priv, unsigned int offset,
 				void *val, size_t bytes);
 typedef int (*nvmem_reg_write_t)(void *priv, unsigned int offset,
@@ -47,6 +46,24 @@ struct nvmem_keepout {
 	unsigned char value;
 };
 
+/**
+ * struct nvmem_cell_info - NVMEM cell description
+ * @name:	Name.
+ * @offset:	Offset within the NVMEM device.
+ * @bytes:	Length of the cell.
+ * @bit_offset:	Bit offset if cell is smaller than a byte.
+ * @nbits:	Number of bits.
+ * @np:		Optional device_node pointer.
+ */
+struct nvmem_cell_info {
+	const char		*name;
+	unsigned int		offset;
+	unsigned int		bytes;
+	unsigned int		bit_offset;
+	unsigned int		nbits;
+	struct device_node	*np;
+};
+
 /**
  * struct nvmem_config - NVMEM device configuration
  *
Signature · 1 line
2.30.2
#12 lore raw

PATCH v3 6/18 nvmem: core: drop the removal of the cells in nvmem_add_cells()

In reply to Michael Walle

If nvmem_add_cells() fails, the whole nvmem_register() will fail and the cells will then be removed anyway. This is a prepartion to introduce a nvmem_add_one_cell() which can then be used by nvmem_add_cells().

This is then the same to what nvmem_add_cells_from_table() and nvmem_add_cells_from_of() do.

---
changes since v2:
- none

changes since v1:
- none

 drivers/nvmem/core.c | 14 ++++----------
 1 file changed, 4 insertions(+), 10 deletions(-)
drivers/nvmem/core.c +4 -10
index 24573e63e5a9..7c76e0e0072e 100644
@@ -515,7 +515,7 @@ static int nvmem_add_cells(struct nvmem_device *nvmem,
 		    int ncells)
 {
 	struct nvmem_cell_entry **cells;
-	int i, rval;
+	int i, rval = 0;
 
 	cells = kcalloc(ncells, sizeof(*cells), GFP_KERNEL);
 	if (!cells)
@@ -525,28 +525,22 @@ static int nvmem_add_cells(struct nvmem_device *nvmem,
 		cells[i] = kzalloc(sizeof(**cells), GFP_KERNEL);
 		if (!cells[i]) {
 			rval = -ENOMEM;
-			goto err;
+			goto out;
 		}
 
 		rval = nvmem_cell_info_to_nvmem_cell_entry(nvmem, &info[i], cells[i]);
 		if (rval) {
 			kfree(cells[i]);
-			goto err;
+			goto out;
 		}
 
 		nvmem_cell_entry_add(cells[i]);
 	}
 
+out:
 	/* remove tmp array */
 	kfree(cells);
 
-	return 0;
-err:
-	while (i--)
-		nvmem_cell_entry_drop(cells[i]);
-
-	kfree(cells);
-
 	return rval;
 }
 
Signature · 1 line
2.30.2
#13 lore raw

Re: [PATCH v3 06/18] nvmem: core: drop the removal of the cells in nvmem_add_cells()

Quoted 2 lines · Michael Walle

If nvmem_add_cells() fails, the whole nvmem_register() will fail and the cells will then be removed anyway. This is a prepartion

s/prepartion/preparation/

Sascha

Signature · 4 lines
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 |
#14 lore raw

PATCH v3 7/18 nvmem: core: add nvmem_add_one_cell()

In reply to Michael Walle

Add a new function to add exactly one cell. This will be used by the nvmem layout drivers to add custom cells. In contrast to the nvmem_add_cells(), this has the advantage that we don't have to assemble a list of cells on runtime.

---
changes since v2:
- add EXPORT_SYMBOL_GPL()

changes since v1:
- none

 drivers/nvmem/core.c           | 59 ++++++++++++++++++++--------------
 include/linux/nvmem-provider.h |  8 +++++
 2 files changed, 43 insertions(+), 24 deletions(-)
drivers/nvmem/core.c +35 -24
index 7c76e0e0072e..cb25bf29dea7 100644
@@ -501,6 +501,36 @@ static int nvmem_cell_info_to_nvmem_cell_entry(struct nvmem_device *nvmem,
 	return 0;
 }
 
+/**
+ * nvmem_add_one_cell() - Add one cell information to an nvmem device
+ *
+ * @nvmem: nvmem device to add cells to.
+ * @info: nvmem cell info to add to the device
+ *
+ * Return: 0 or negative error code on failure.
+ */
+int nvmem_add_one_cell(struct nvmem_device *nvmem,
+		       const struct nvmem_cell_info *info)
+{
+	struct nvmem_cell_entry *cell;
+	int rval;
+
+	cell = kzalloc(sizeof(*cell), GFP_KERNEL);
+	if (!cell)
+		return -ENOMEM;
+
+	rval = nvmem_cell_info_to_nvmem_cell_entry(nvmem, info, cell);
+	if (rval) {
+		kfree(cell);
+		return rval;
+	}
+
+	nvmem_cell_entry_add(cell);
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(nvmem_add_one_cell);
+
 /**
  * nvmem_add_cells() - Add cell information to an nvmem device
  *
@@ -514,34 +544,15 @@ static int nvmem_add_cells(struct nvmem_device *nvmem,
 		    const struct nvmem_cell_info *info,
 		    int ncells)
 {
-	struct nvmem_cell_entry **cells;
-	int i, rval = 0;
-
-	cells = kcalloc(ncells, sizeof(*cells), GFP_KERNEL);
-	if (!cells)
-		return -ENOMEM;
+	int i, rval;
 
 	for (i = 0; i < ncells; i++) {
-		cells[i] = kzalloc(sizeof(**cells), GFP_KERNEL);
-		if (!cells[i]) {
-			rval = -ENOMEM;
-			goto out;
-		}
-
-		rval = nvmem_cell_info_to_nvmem_cell_entry(nvmem, &info[i], cells[i]);
-		if (rval) {
-			kfree(cells[i]);
-			goto out;
-		}
-
-		nvmem_cell_entry_add(cells[i]);
+		rval = nvmem_add_one_cell(nvmem, &info[i]);
+		if (rval)
+			return rval;
 	}
 
-out:
-	/* remove tmp array */
-	kfree(cells);
-
-	return rval;
+	return 0;
 }
 
 /**
include/linux/nvmem-provider.h +8 -0
index 14a32a1bc249..385d29168008 100644
@@ -155,6 +155,9 @@ struct nvmem_device *devm_nvmem_register(struct device *dev,
 void nvmem_add_cell_table(struct nvmem_cell_table *table);
 void nvmem_del_cell_table(struct nvmem_cell_table *table);
 
+int nvmem_add_one_cell(struct nvmem_device *nvmem,
+		       const struct nvmem_cell_info *info);
+
 #else
 
 static inline struct nvmem_device *nvmem_register(const struct nvmem_config *c)
@@ -172,6 +175,11 @@ devm_nvmem_register(struct device *dev, const struct nvmem_config *c)
 
 static inline void nvmem_add_cell_table(struct nvmem_cell_table *table) {}
 static inline void nvmem_del_cell_table(struct nvmem_cell_table *table) {}
+static inline int nvmem_add_one_cell(struct nvmem_device *nvmem,
+				     const struct nvmem_cell_info *info)
+{
+	return -EOPNOTSUPP;
+}
 
 #endif /* CONFIG_NVMEM */
 #endif  /* ifndef _LINUX_NVMEM_PROVIDER_H */
Signature · 1 line
2.30.2
#15 lore raw

PATCH v3 8/18 nvmem: core: use nvmem_add_one_cell() in nvmem_add_cells_from_of()

In reply to Michael Walle

Convert nvmem_add_cells_from_of() to use the new nvmem_add_one_cell(). This will remove duplicate code and it will make it possible to add a hook to a nvmem layout in between, which can change fields before the cell is finally added.

---
changes since v2:
- none

changes since v1:
- new patch

 drivers/nvmem/core.c | 38 ++++++++++++++------------------------
 1 file changed, 14 insertions(+), 24 deletions(-)
drivers/nvmem/core.c +14 -24
index cb25bf29dea7..26459d582e99 100644
@@ -688,15 +688,15 @@ static int nvmem_validate_keepouts(struct nvmem_device *nvmem)
 
 static int nvmem_add_cells_from_of(struct nvmem_device *nvmem)
 {
-	struct device_node *parent, *child;
 	struct device *dev = &nvmem->dev;
 	struct nvmem_cell_entry *cell;
+	struct device_node *child;
 	const __be32 *addr;
-	int len;
+	int len, ret;
 
-	parent = dev->of_node;
+	for_each_child_of_node(dev->of_node, child) {
+		struct nvmem_cell_info info = {0};
 
-	for_each_child_of_node(parent, child) {
 		addr = of_get_property(child, "reg", &len);
 		if (!addr)
 			continue;
@@ -712,34 +712,24 @@ static int nvmem_add_cells_from_of(struct nvmem_device *nvmem)
 			return -ENOMEM;
 		}
 
-		cell->nvmem = nvmem;
-		cell->offset = be32_to_cpup(addr++);
-		cell->bytes = be32_to_cpup(addr);
-		cell->name = kasprintf(GFP_KERNEL, "%pOFn", child);
+		info.offset = be32_to_cpup(addr++);
+		info.bytes = be32_to_cpup(addr);
+		info.name = kasprintf(GFP_KERNEL, "%pOFn", child);
 
 		addr = of_get_property(child, "bits", &len);
 		if (addr && len == (2 * sizeof(u32))) {
-			cell->bit_offset = be32_to_cpup(addr++);
-			cell->nbits = be32_to_cpup(addr);
+			info.bit_offset = be32_to_cpup(addr++);
+			info.nbits = be32_to_cpup(addr);
 		}
 
-		if (cell->nbits)
-			cell->bytes = DIV_ROUND_UP(
-					cell->nbits + cell->bit_offset,
-					BITS_PER_BYTE);
+		info.np = of_node_get(child);
 
-		if (!IS_ALIGNED(cell->offset, nvmem->stride)) {
-			dev_err(dev, "cell %s unaligned to nvmem stride %d\n",
-				cell->name, nvmem->stride);
-			/* Cells already added will be freed later. */
-			kfree_const(cell->name);
-			kfree(cell);
+		ret = nvmem_add_one_cell(nvmem, &info);
+		kfree(info.name);
+		if (ret) {
 			of_node_put(child);
-			return -EINVAL;
+			return ret;
 		}
-
-		cell->np = of_node_get(child);
-		nvmem_cell_entry_add(cell);
 	}
 
 	return 0;
Signature · 1 line
2.30.2
#16 lore raw

PATCH v3 9/18 nvmem: core: introduce NVMEM layouts

In reply to Michael Walle

NVMEM layouts are used to generate NVMEM cells during runtime. Think of an EEPROM with a well-defined conent. For now, the content can be described by a device tree or a board file. But this only works if the offsets and lengths are static and don't change. One could also argue that putting the layout of the EEPROM in the device tree is the wrong place. Instead, the device tree should just have a specific compatible string.

Right now there are two use cases:
(1) The NVMEM cell needs special processing. E.g. if it only specifies a base MAC address offset and you need to add an offset, or it needs to parse a MAC from ASCII format or some proprietary format. (Post processing of cells is added in a later commit).
(2) u-boot environment parsing. The cells don't have a particular offset but it needs parsing the content to determine the offsets and length.

---
changes since v2:
- look for "nvmem-layout" node and its compatible
- add of_nvmem_layout_get_container()
- special handling in of_nvmem_cell_get()

changes since v1:
- add documentation in nvmem.rst
- add nvmem_layout_unregister() + necessary module tracking
- make it possible to supply a layout via nvmem_register()
- check add_cells, before calling

 Documentation/driver-api/nvmem.rst |  15 ++++
 drivers/nvmem/Kconfig              |   4 +
 drivers/nvmem/Makefile             |   1 +
 drivers/nvmem/core.c               | 114 +++++++++++++++++++++++++++++
 drivers/nvmem/layouts/Kconfig      |   5 ++
 drivers/nvmem/layouts/Makefile     |   4 +
 include/linux/nvmem-consumer.h     |   7 ++
 include/linux/nvmem-provider.h     |  51 +++++++++++++
 8 files changed, 201 insertions(+)
 create mode 100644 drivers/nvmem/layouts/Kconfig
 create mode 100644 drivers/nvmem/layouts/Makefile
Documentation/driver-api/nvmem.rst +15 -0
index e3366322d46c..de221e91c8e3 100644
@@ -185,3 +185,18 @@ ex::
 =====================
 
 See Documentation/devicetree/bindings/nvmem/nvmem.txt
+
+8. NVMEM layouts
+================
+
+NVMEM layouts are yet another mechanism to create cells. With the device
+tree binding it is possible to specify simple cells by using an offset
+and a length. Sometimes, the cells doesn't have a static offset, but
+the content is still well defined, e.g. tag-length-values. In this case,
+the NVMEM device content has to be first parsed and the cells need to
+be added accordingly. Layouts let you read the content of the NVMEM device
+and let you add cells dynamically.
+
+Another use case for layouts is the post processing of cells. With layouts,
+it is possible to associate a custom post processing hook to a cell. It
+even possible to add this hook to cells not created by the layout itself.
drivers/nvmem/Kconfig +4 -0
index 755f551426b5..0e10b5b094b9 100644
@@ -21,6 +21,10 @@ config NVMEM_SYSFS
 	 This interface is mostly used by userspace applications to
 	 read/write directly into nvmem.
 
+# Layouts
+
+source "drivers/nvmem/layouts/Kconfig"
+
 # Devices
 
 config NVMEM_APPLE_EFUSES
drivers/nvmem/Makefile +1 -0
index fa80fe17e567..4cf87ef6c24d 100644
@@ -5,6 +5,7 @@
 
 obj-$(CONFIG_NVMEM)		+= nvmem_core.o
 nvmem_core-y			:= core.o
+obj-y				+= layouts/
 
 # Devices
 obj-$(CONFIG_NVMEM_APPLE_EFUSES)	+= nvmem-apple-efuses.o
drivers/nvmem/core.c +114 -0

162 lines not on this page · raw message

drivers/nvmem/layouts/Kconfig new file +5 -0
new file mode 100644
index 000000000000..9ad3911d1605
@@ -0,0 +1,5 @@
+# SPDX-License-Identifier: GPL-2.0
+
+menu "Layout Types"
+
+endmenu
drivers/nvmem/layouts/Makefile new file +4 -0
new file mode 100644
index 000000000000..6fdb3c60a4fa
@@ -0,0 +1,4 @@
+# SPDX-License-Identifier: GPL-2.0
+#
+# Makefile for nvmem layouts.
+#
include/linux/nvmem-consumer.h +7 -0
index 1f62f7ba71ca..fa030d93b768 100644
@@ -239,6 +239,7 @@ struct nvmem_cell *of_nvmem_cell_get(struct device_node *np,
 				     const char *id);
 struct nvmem_device *of_nvmem_device_get(struct device_node *np,
 					 const char *name);
+struct device_node *of_nvmem_layout_get_container(struct nvmem_device *nvmem);
 #else
 static inline struct nvmem_cell *of_nvmem_cell_get(struct device_node *np,
 						   const char *id)
@@ -251,6 +252,12 @@ static inline struct nvmem_device *of_nvmem_device_get(struct device_node *np,
 {
 	return ERR_PTR(-EOPNOTSUPP);
 }
+
+static inline struct device_node *
+of_nvmem_layout_get_container(struct nvmem_device *nvmem)
+{
+	return ERR_PTR(-EOPNOTSUPP);
+}
 #endif /* CONFIG_NVMEM && CONFIG_OF */
 
 #endif  /* ifndef _LINUX_NVMEM_CONSUMER_H */
include/linux/nvmem-provider.h +51 -0

80 lines not on this page · raw message

Signature · 1 line
2.30.2
#17 lore raw

PATCH v3 10/18 nvmem: core: add per-cell post processing

In reply to Michael Walle

Instead of relying on the name the consumer is using for the cell, like it is done for the nvmem .cell_post_process configuration parameter, provide a per-cell post processing hook. This can then be populated by the NVMEM provider (or the NVMEM layout) when adding the cell.

---
changes since v2:
- none

changes since v1:
- rename hook to read_post_process

 drivers/nvmem/core.c           | 17 +++++++++++++++++
 include/linux/nvmem-provider.h |  3 +++
 2 files changed, 20 insertions(+)
drivers/nvmem/core.c +17 -0
index 4ad1a02faf30..f0c0bf3b8b25 100644
@@ -53,6 +53,7 @@ struct nvmem_cell_entry {
 	int			bytes;
 	int			bit_offset;
 	int			nbits;
+	nvmem_cell_post_process_t read_post_process;
 	struct device_node	*np;
 	struct nvmem_device	*nvmem;
 	struct list_head	node;
@@ -469,6 +470,7 @@ static int nvmem_cell_info_to_nvmem_cell_entry_nodup(struct nvmem_device *nvmem,
 	cell->offset = info->offset;
 	cell->bytes = info->bytes;
 	cell->name = info->name;
+	cell->read_post_process = info->read_post_process;
 
 	cell->bit_offset = info->bit_offset;
 	cell->nbits = info->nbits;
@@ -1553,6 +1555,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 (cell->read_post_process) {
+		rc = cell->read_post_process(nvmem->priv, id, index,
+					     cell->offset, buf, cell->bytes);
+		if (rc)
+			return rc;
+	}
+
 	if (nvmem->cell_post_process) {
 		rc = nvmem->cell_post_process(nvmem->priv, id, index,
 					      cell->offset, buf, cell->bytes);
@@ -1661,6 +1670,14 @@ static int __nvmem_cell_entry_write(struct nvmem_cell_entry *cell, void *buf, si
 	    (cell->bit_offset == 0 && len != cell->bytes))
 		return -EINVAL;
 
+	/*
+	 * Any cells which have a read_post_process hook are read-only because
+	 * we cannot reverse the operation and it might affect other cells,
+	 * too.
+	 */
+	if (cell->read_post_process)
+		return -EINVAL;
+
 	if (cell->bit_offset || cell->nbits) {
 		buf = nvmem_cell_prepare_write_buffer(cell, buf, len);
 		if (IS_ERR(buf))
include/linux/nvmem-provider.h +3 -0
index 4185767c114f..1930496d8854 100644
@@ -54,6 +54,8 @@ struct nvmem_keepout {
  * @bit_offset:	Bit offset if cell is smaller than a byte.
  * @nbits:	Number of bits.
  * @np:		Optional device_node pointer.
+ * @read_post_process:	Callback for optional post processing of cell data
+ *			on reads.
  */
 struct nvmem_cell_info {
 	const char		*name;
@@ -62,6 +64,7 @@ struct nvmem_cell_info {
 	unsigned int		bit_offset;
 	unsigned int		nbits;
 	struct device_node	*np;
+	nvmem_cell_post_process_t read_post_process;
 };
 
 /**
Signature · 1 line
2.30.2
#18 lore raw

PATCH v3 11/18 nvmem: core: allow to modify a cell before adding it

In reply to Michael Walle

Provide a way to modify a cell before it will get added. This is useful to attach a custom post processing hook via a layout.

---
changes since v2:
- none

changes since v1:
- new patch

 drivers/nvmem/core.c           | 4 ++++
 include/linux/nvmem-provider.h | 5 +++++
 2 files changed, 9 insertions(+)
drivers/nvmem/core.c +4 -0
index f0c0bf3b8b25..264a15d26c7c 100644
@@ -694,6 +694,7 @@ static int nvmem_validate_keepouts(struct nvmem_device *nvmem)
 
 static int nvmem_add_cells_from_of(struct nvmem_device *nvmem)
 {
+	struct nvmem_layout *layout = nvmem->layout;
 	struct device *dev = &nvmem->dev;
 	struct nvmem_cell_entry *cell;
 	struct device_node *child;
@@ -730,6 +731,9 @@ static int nvmem_add_cells_from_of(struct nvmem_device *nvmem)
 
 		info.np = of_node_get(child);
 
+		if (layout && layout->fixup_cell_info)
+			layout->fixup_cell_info(nvmem, layout, &info);
+
 		ret = nvmem_add_one_cell(nvmem, &info);
 		kfree(info.name);
 		if (ret) {
include/linux/nvmem-provider.h +5 -0
index 1930496d8854..bfaba5227ac9 100644
@@ -157,6 +157,8 @@ struct nvmem_cell_table {
  * @add_cells:		Will be called if a nvmem device is found which
  *			has this layout. The function will add layout
  *			specific cells with nvmem_add_one_cell().
+ * @fixup_cell_info:	Will be called before a cell is added. Can be
+ *			used to modify the nvmem_cell_info.
  * @owner:		Pointer to struct module.
  * @node:		List node.
  *
@@ -170,6 +172,9 @@ struct nvmem_layout {
 	const struct of_device_id *of_match_table;
 	int (*add_cells)(struct device *dev, struct nvmem_device *nvmem,
 			 struct nvmem_layout *layout);
+	void (*fixup_cell_info)(struct nvmem_device *nvmem,
+				struct nvmem_layout *layout,
+				struct nvmem_cell_info *cell);
 
 	/* private */
 	struct module *owner;
Signature · 1 line
2.30.2
#19 lore raw

PATCH v3 12/18 nvmem: imx-ocotp: replace global post processing with layouts

In reply to Michael Walle

In preparation of retiring the global post processing hook change this driver to use layouts. The layout will be supplied during registration and will be used to add the post processing hook to all added cells.

---
changes since v2:
- none

changes since v1:
- new patch

 drivers/nvmem/imx-ocotp.c | 30 +++++++++++++++++++-----------
 1 file changed, 19 insertions(+), 11 deletions(-)
drivers/nvmem/imx-ocotp.c +19 -11
index e9b52ecb3f72..ac0edb6398f1 100644
@@ -225,18 +225,13 @@ static int imx_ocotp_read(void *context, unsigned int offset,
 static int imx_ocotp_cell_pp(void *context, const char *id, int index,
 			     unsigned int offset, void *data, size_t bytes)
 {
-	struct ocotp_priv *priv = context;
+	u8 *buf = data;
+	int i;
 
 	/* Deal with some post processing of nvmem cell data */
-	if (id && !strcmp(id, "mac-address")) {
-		if (priv->params->reverse_mac_address) {
-			u8 *buf = data;
-			int i;
-
-			for (i = 0; i < bytes/2; i++)
-				swap(buf[i], buf[bytes - i - 1]);
-		}
-	}
+	if (id && !strcmp(id, "mac-address"))
+		for (i = 0; i < bytes / 2; i++)
+			swap(buf[i], buf[bytes - i - 1]);
 
 	return 0;
 }
@@ -488,7 +483,6 @@ static struct nvmem_config imx_ocotp_nvmem_config = {
 	.stride = 1,
 	.reg_read = imx_ocotp_read,
 	.reg_write = imx_ocotp_write,
-	.cell_post_process = imx_ocotp_cell_pp,
 };
 
 static const struct ocotp_params imx6q_params = {
@@ -595,6 +589,17 @@ static const struct of_device_id imx_ocotp_dt_ids[] = {
 };
 MODULE_DEVICE_TABLE(of, imx_ocotp_dt_ids);
 
+static void imx_ocotp_fixup_cell_info(struct nvmem_device *nvmem,
+				      struct nvmem_layout *layout,
+				      struct nvmem_cell_info *cell)
+{
+	cell->read_post_process = imx_ocotp_cell_pp;
+}
+
+struct nvmem_layout imx_ocotp_layout = {
+	.fixup_cell_info = imx_ocotp_fixup_cell_info,
+};
+
 static int imx_ocotp_probe(struct platform_device *pdev)
 {
 	struct device *dev = &pdev->dev;
@@ -619,6 +624,9 @@ static int imx_ocotp_probe(struct platform_device *pdev)
 	imx_ocotp_nvmem_config.size = 4 * priv->params->nregs;
 	imx_ocotp_nvmem_config.dev = dev;
 	imx_ocotp_nvmem_config.priv = priv;
+	if (priv->params->reverse_mac_address)
+		imx_ocotp_nvmem_config.layout = &imx_ocotp_layout;
+
 	priv->config = &imx_ocotp_nvmem_config;
 
 	clk_prepare_enable(priv->clk);
Signature · 1 line
2.30.2
#20 lore raw

PATCH v3 13/18 nvmem: cell: drop global cell_post_process

In reply to Michael Walle

There are no users anymore for the global cell_post_process callback anymore. New users should use proper nvmem layouts.

---
changes since v2:
- none

changes since v1:
- new patch

 drivers/nvmem/core.c           | 9 ---------
 include/linux/nvmem-provider.h | 2 --
 2 files changed, 11 deletions(-)
drivers/nvmem/core.c +0 -9
index 264a15d26c7c..9234c7d87a69 100644
@@ -38,7 +38,6 @@ struct nvmem_device {
 	unsigned int		nkeepout;
 	nvmem_reg_read_t	reg_read;
 	nvmem_reg_write_t	reg_write;
-	nvmem_cell_post_process_t cell_post_process;
 	struct gpio_desc	*wp_gpio;
 	struct nvmem_layout	*layout;
 	void *priv;
@@ -895,7 +894,6 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config)
 	nvmem->type = config->type;
 	nvmem->reg_read = config->reg_read;
 	nvmem->reg_write = config->reg_write;
-	nvmem->cell_post_process = config->cell_post_process;
 	nvmem->keepout = config->keepout;
 	nvmem->nkeepout = config->nkeepout;
 	if (config->of_node)
@@ -1566,13 +1564,6 @@ static int __nvmem_cell_read(struct nvmem_device *nvmem,
 			return rc;
 	}
 
-	if (nvmem->cell_post_process) {
-		rc = nvmem->cell_post_process(nvmem->priv, id, index,
-					      cell->offset, buf, cell->bytes);
-		if (rc)
-			return rc;
-	}
-
 	if (len)
 		*len = cell->bytes;
 
include/linux/nvmem-provider.h +0 -2
index bfaba5227ac9..12833fe4eb4d 100644
@@ -85,7 +85,6 @@ struct nvmem_cell_info {
  * @no_of_node:	Device should not use the parent's of_node even if it's !NULL.
  * @reg_read:	Callback to read data.
  * @reg_write:	Callback to write data.
- * @cell_post_process:	Callback for vendor specific post processing of cell data
  * @size:	Device size.
  * @word_size:	Minimum read/write access granularity.
  * @stride:	Minimum read/write access stride.
@@ -120,7 +119,6 @@ struct nvmem_config {
 	bool			no_of_node;
 	nvmem_reg_read_t	reg_read;
 	nvmem_reg_write_t	reg_write;
-	nvmem_cell_post_process_t cell_post_process;
 	int	size;
 	int	word_size;
 	int	stride;
Signature · 1 line
2.30.2
#21 lore raw

PATCH v3 14/18 nvmem: core: provide own priv pointer in post process callback

In reply to Michael Walle

It doesn't make any more sense to have a opaque pointer set up by the nvmem device. Usually, the layout isn't associated with a particular nvmem device. Instead, let the caller who set the post process callback provide the priv pointer.

---
changes since v2:
- don't drop the pointer but let the user specify an opaque pointer

changes since v1:
- new patch

 drivers/nvmem/core.c           | 4 +++-
 include/linux/nvmem-provider.h | 5 ++++-
 2 files changed, 7 insertions(+), 2 deletions(-)
drivers/nvmem/core.c +3 -1
index 9234c7d87a69..e03976f6ad1c 100644
@@ -53,6 +53,7 @@ struct nvmem_cell_entry {
 	int			bit_offset;
 	int			nbits;
 	nvmem_cell_post_process_t read_post_process;
+	void			*priv;
 	struct device_node	*np;
 	struct nvmem_device	*nvmem;
 	struct list_head	node;
@@ -470,6 +471,7 @@ static int nvmem_cell_info_to_nvmem_cell_entry_nodup(struct nvmem_device *nvmem,
 	cell->bytes = info->bytes;
 	cell->name = info->name;
 	cell->read_post_process = info->read_post_process;
+	cell->priv = info->priv;
 
 	cell->bit_offset = info->bit_offset;
 	cell->nbits = info->nbits;
@@ -1558,7 +1560,7 @@ static int __nvmem_cell_read(struct nvmem_device *nvmem,
 		nvmem_shift_read_buffer_in_place(cell, buf);
 
 	if (cell->read_post_process) {
-		rc = cell->read_post_process(nvmem->priv, id, index,
+		rc = cell->read_post_process(cell->priv, id, index,
 					     cell->offset, buf, cell->bytes);
 		if (rc)
 			return rc;
include/linux/nvmem-provider.h +4 -1
index 12833fe4eb4d..cb0814f2ddae 100644
@@ -20,7 +20,8 @@ typedef int (*nvmem_reg_write_t)(void *priv, unsigned int offset,
 				 void *val, size_t bytes);
 /* used for vendor specific post processing of cell data */
 typedef int (*nvmem_cell_post_process_t)(void *priv, const char *id, int index,
-					 unsigned int offset, void *buf, size_t bytes);
+					 unsigned int offset, void *buf,
+					 size_t bytes);
 
 enum nvmem_type {
 	NVMEM_TYPE_UNKNOWN = 0,
@@ -56,6 +57,7 @@ struct nvmem_keepout {
  * @np:		Optional device_node pointer.
  * @read_post_process:	Callback for optional post processing of cell data
  *			on reads.
+ * @priv:	Opaque data passed to the read_post_process hook.
  */
 struct nvmem_cell_info {
 	const char		*name;
@@ -65,6 +67,7 @@ struct nvmem_cell_info {
 	unsigned int		nbits;
 	struct device_node	*np;
 	nvmem_cell_post_process_t read_post_process;
+	void			*priv;
 };
 
 /**
Signature · 1 line
2.30.2
#22 lore raw

PATCH v3 15/18 nvmem: layouts: add sl28vpd layout

In reply to Michael Walle

This layout applies to the VPD of the Kontron sl28 boards. The VPD only contains a base MAC address. Therefore, we have to add an individual offset to it. This is done by taking the second argument of the nvmem phandle into account. Also this let us checking the VPD version and the checksum.

---
changes since v2:
- use of_nvmem_layout_get_container()

changes since v1:
- none

 drivers/nvmem/layouts/Kconfig   |   9 ++
 drivers/nvmem/layouts/Makefile  |   2 +
 drivers/nvmem/layouts/sl28vpd.c | 153 ++++++++++++++++++++++++++++++++
 3 files changed, 164 insertions(+)
 create mode 100644 drivers/nvmem/layouts/sl28vpd.c
drivers/nvmem/layouts/Kconfig +9 -0
index 9ad3911d1605..75082f6b471d 100644
@@ -2,4 +2,13 @@
 
 menu "Layout Types"
 
+config NVMEM_LAYOUT_SL28_VPD
+	bool "Kontron sl28 VPD layout support"
+	select CRC8
+	help
+	  Say Y here if you want to support the VPD layout of the Kontron
+	  SMARC-sAL28 boards.
+
+	  If unsure, say N.
+
 endmenu
drivers/nvmem/layouts/Makefile +2 -0
index 6fdb3c60a4fa..fc617b9e87d0 100644
@@ -2,3 +2,5 @@
 #
 # Makefile for nvmem layouts.
 #
+
+obj-$(CONFIG_NVMEM_LAYOUT_SL28_VPD) += sl28vpd.o
drivers/nvmem/layouts/sl28vpd.c new file +153 -0

153 lines not on this page · raw message

Signature · 1 line
2.30.2
#23 lore raw

PATCH v3 16/18 MAINTAINERS: add myself as sl28vpd nvmem layout driver

In reply to Michael Walle

Add myself as a maintainer for the new sl28vpd nvmem layout driver.

---
changes since v2:
- new patch

 MAINTAINERS | 6 ++++++
 1 file changed, 6 insertions(+)
MAINTAINERS +6 -0
index 1df62c469bd9..ef4b7a31f8b4 100644
@@ -19097,6 +19097,12 @@ F:	drivers/irqchip/irq-sl28cpld.c
 F:	drivers/pwm/pwm-sl28cpld.c
 F:	drivers/watchdog/sl28cpld_wdt.c
 
+SL28 VPD NVMEM LAYOUT DRIVER
+M:	Michael Walle <[email protected]>
+S:	Maintained
+F:	Documentation/devicetree/bindings/nvmem/layouts/kontron,sl28-vpd.yaml
+F:	drivers/nvmem/layouts/sl28vpd.c
+
 SLAB ALLOCATOR
 M:	Christoph Lameter <[email protected]>
 M:	Pekka Enberg <[email protected]>
Signature · 1 line
2.30.2
#24 lore raw

PATCH v3 17/18 nvmem: layouts: Add ONIE tlv layout driver

In reply to Michael Walle

From: Miquel Raynal <[email protected]>

This layout applies no top of any non volatile storage device containing an ONIE table factory flashed. This table follows the tlv (type-length-value) organization described in the link below. We cannot afford using regular parsers because the content of these tables is manufacturer specific and must be dynamically discovered.

---
changes since v2:
- new patch

 drivers/nvmem/layouts/Kconfig    |   9 ++
 drivers/nvmem/layouts/Makefile   |   1 +
 drivers/nvmem/layouts/onie-tlv.c | 249 +++++++++++++++++++++++++++++++
 3 files changed, 259 insertions(+)
 create mode 100644 drivers/nvmem/layouts/onie-tlv.c
drivers/nvmem/layouts/Kconfig +9 -0
index 75082f6b471d..9ad50474cb77 100644
@@ -11,4 +11,13 @@ config NVMEM_LAYOUT_SL28_VPD
 
 	  If unsure, say N.
 
+config NVMEM_LAYOUT_ONIE_TLV
+	bool "ONIE tlv support"
+	select CRC32
+	help
+	  Say Y here if you want to support the Open Compute Project ONIE
+	  Type-Length-Value standard table.
+
+	  If unsure, say N.
+
 endmenu
drivers/nvmem/layouts/Makefile +1 -0
index fc617b9e87d0..2974bd7d33ed 100644
@@ -4,3 +4,4 @@
 #
 
 obj-$(CONFIG_NVMEM_LAYOUT_SL28_VPD) += sl28vpd.o
+obj-$(CONFIG_NVMEM_LAYOUT_ONIE_TLV) += onie-tlv.o
drivers/nvmem/layouts/onie-tlv.c new file +249 -0

249 lines not on this page · raw message

Signature · 1 line
2.30.2
#25 lore raw

Re: [PATCH v3 17/18] nvmem: layouts: Add ONIE tlv layout driver

Quoted 3 lines · Michael Walle

From: Miquel Raynal <[email protected]>

This layout applies no top of any non volatile storage device containing

s/no/on/

Quoted 16 lines
drivers/nvmem/layouts/onie-tlv.c new file +16 -0
+	table_len = hdr_len + data_len;
+	if (table_len > ONIE_TLV_MAX_LEN) {
+		dev_err(dev, "Invalid ONIE TLV data length\n");
+		return -EINVAL;
+	}
+
+	table = devm_kmalloc(dev, table_len, GFP_KERNEL);
+	if (!table)
+		return -ENOMEM;
+
+	ret = nvmem_device_read(nvmem, 0, table_len, table);
+	if (ret != table_len)
+		goto free_data_buf;
+
+	if (!onie_tlv_crc_is_valid(dev, table_len, table)) {
+		ret = -EINVAL;

drivers/nvmem/layouts/onie-tlv.c line 229

Quoted 10 lines
drivers/nvmem/layouts/onie-tlv.c new file +10 -0
+		goto free_data_buf;
+	}
+
+	data = table + hdr_len;
+	ret = onie_tlv_add_cells(dev, nvmem, data_len, data);
+	if (ret)
+		goto free_data_buf;
+
+free_data_buf:
+	kfree(table);

This is allocated with devm_kmalloc(), you shouldn't free it here.

Sascha

Signature · 4 lines
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 |
#26 lore raw

Re: [PATCH v3 17/18] nvmem: layouts: Add ONIE tlv layout driver

Am 2022-11-21 08:51, schrieb Sascha Hauer:

Quoted 27 lines · Michael Walle
Quoted 3 lines

From: Miquel Raynal <[email protected]>

This layout applies no top of any non volatile storage device containing

s/no/on/

Quoted 20 lines
drivers/nvmem/layouts/onie-tlv.c new file +20 -0
+	table_len = hdr_len + data_len;
+	if (table_len > ONIE_TLV_MAX_LEN) {
+		dev_err(dev, "Invalid ONIE TLV data length\n");
+		return -EINVAL;
+	}
+
+	table = devm_kmalloc(dev, table_len, GFP_KERNEL);
+	if (!table)
+		return -ENOMEM;
+
+	ret = nvmem_device_read(nvmem, 0, table_len, table);
+	if (ret != table_len)
+		goto free_data_buf;
+
+	if (!onie_tlv_crc_is_valid(dev, table_len, table)) {
+		ret = -EINVAL;
+		goto free_data_buf;
+	}
+
+	data = table + hdr_len;
Quoted 8 lines

drivers/nvmem/layouts/onie-tlv.c line 229

Quoted 6 lines
drivers/nvmem/layouts/onie-tlv.c new file +6 -0
+	ret = onie_tlv_add_cells(dev, nvmem, data_len, data);
+	if (ret)
+		goto free_data_buf;
+
+free_data_buf:
+	kfree(table);

This is allocated with devm_kmalloc(), you shouldn't free it here.

Miquel, should I drop your patches or should I fix this in place and repost them?

Also, there is a mistake in nvmem_add_cells_from_layout() which doesn't handle the return code. Probably a leftover from before when I had .add_cells() return void.

-michael

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

#27 lore raw

PATCH v3 18/18 MAINTAINERS: Add myself as ONIE tlv NVMEM layout maintainer

In reply to Michael Walle

From: Miquel Raynal <[email protected]>

Following the introduction of the bindings for this NVMEM parser and the layout driver, add myself as maintainer.

---
changes since v2:
- new patch

 MAINTAINERS | 6 ++++++
 1 file changed, 6 insertions(+)
MAINTAINERS +6 -0
index ef4b7a31f8b4..101cff82ce44 100644
@@ -15499,6 +15499,12 @@ L:	[email protected]
 S:	Maintained
 F:	drivers/hwmon/oxp-sensors.c
 
+ONIE TLV NVMEM LAYOUT DRIVER
+M:	Miquel Raynal <[email protected]>
+S:	Maintained
+F:	Documentation/devicetree/bindings/nvmem/layouts/onie,tlv-layout.yaml
+F:	drivers/nvmem/layouts/onie-tlv.c
+
 ONION OMEGA2+ BOARD
 M:	Harvey Hunt <[email protected]>
 L:	[email protected]
Signature · 1 line
2.30.2