Skip to contentSkip to conversation

mtd: allow getting MTD device associated with a specific DT node

PATCH In review Rafał Miłecki 3 participants 4 messages Jun 15, 2022 – Aug 17, 2022

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH v4 1/2 mtd: allow getting MTD device associated with a specific DT node

From: Rafał Miłecki <[email protected]>

MTD subsystem API allows interacting with MTD devices (e.g. reading, writing, handling bad blocks). So far a random driver could get MTD device only by its name (get_mtd_device_nm()). This change allows getting them also by a DT node.

This API is required for drivers handling DT defined MTD partitions in a specific way (e.g. U-Boot (sub)partition with environment variables).

---
V3: First introduction of of_get_mtd_device_by_node()
V4: Use EPROBE_DEFER

Srinivas: in V3 Miquel said it's OK to push this patch through NVMEM

---
 drivers/mtd/mtdcore.c   | 28 ++++++++++++++++++++++++++++
 include/linux/mtd/mtd.h |  1 +
 2 files changed, 29 insertions(+)
drivers/mtd/mtdcore.c +28 -0
index 9eb0680db312..3613cc142f25 100644
@@ -1154,6 +1154,34 @@ int __get_mtd_device(struct mtd_info *mtd)
 }
 EXPORT_SYMBOL_GPL(__get_mtd_device);
 
+/**
+ * of_get_mtd_device_by_node - obtain an MTD device associated with a given node
+ *
+ * @np: device tree node
+ */
+struct mtd_info *of_get_mtd_device_by_node(struct device_node *np)
+{
+	struct mtd_info *mtd = NULL;
+	struct mtd_info *tmp;
+	int err;
+
+	mutex_lock(&mtd_table_mutex);
+
+	err = -EPROBE_DEFER;
+	mtd_for_each_device(tmp) {
+		if (mtd_get_of_node(tmp) == np) {
+			mtd = tmp;
+			err = __get_mtd_device(mtd);
+			break;
+		}
+	}
+
+	mutex_unlock(&mtd_table_mutex);
+
+	return err ? ERR_PTR(err) : mtd;
+}
+EXPORT_SYMBOL_GPL(of_get_mtd_device_by_node);
+
 /**
  *	get_mtd_device_nm - obtain a validated handle for an MTD device by
  *	device name
include/linux/mtd/mtd.h +1 -0
index 955aee14b0f7..6fc841ceef31 100644
@@ -677,6 +677,7 @@ extern int mtd_device_unregister(struct mtd_info *master);
 extern struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num);
 extern int __get_mtd_device(struct mtd_info *mtd);
 extern void __put_mtd_device(struct mtd_info *mtd);
+extern struct mtd_info *of_get_mtd_device_by_node(struct device_node *np);
 extern struct mtd_info *get_mtd_device_nm(const char *name);
 extern void put_mtd_device(struct mtd_info *mtd);
 
Signature · 1 line
2.34.1
#1 lore raw

PATCH v4 2/2 nvmem: add driver handling U-Boot environment variables

From: Rafał Miłecki <[email protected]>

U-Boot stores its setup as environment variables. It's a list of key-value pairs stored on flash device with a custom header.

This commit adds an NVMEM driver that:
1. Provides NVMEM access to environment vars binary data
2. Extracts variables as NVMEM cells

Current Linux's NVMEM sysfs API allows reading whole NVMEM data block. It can be used by user-space tools for reading U-Boot env vars block without the hassle of finding its location. Parsing will still need to be re-done there.

Kernel-parsed NVMEM cells can be read however by Linux drivers. This may be useful for Ethernet drivers for reading device MAC address which is often stored as U-Boot env variable.

---
V3: Use of_get_mtd_device_by_node() (thanks Ahmad) & update description
V2: Drop ARCH_BCM4908 dependency as there are plenty architectures using
U-Boot bootloader. Thanks Srinivas.
V4: Mention MTD in Kconfig help
Use correct MTD device (instead of parent) - simplifies code a lot Use flexible arrays syntax ([])
Use of_device_get_match_data() helper
Use dev_err_probe()

As noticed by Ahmad a missing NVMEM subsystem feature is user-space access to parsed NVMEM cells. That is something I started working on some time ago and I'm planning to get back to at some point, please check:
[PATCH 2/2] nvmem: expose NVMEM cells in sysfs
https://lore.kernel.org/lkml/[email protected]/

---
 MAINTAINERS                |   1 +
 drivers/nvmem/Kconfig      |  13 +++
 drivers/nvmem/Makefile     |   2 +
 drivers/nvmem/u-boot-env.c | 218 +++++++++++++++++++++++++++++++++++++
 4 files changed, 234 insertions(+)
 create mode 100644 drivers/nvmem/u-boot-env.c
MAINTAINERS +1 -0
index 475e28365385..43b427fa76b0 100644
@@ -20411,6 +20411,7 @@ U-BOOT ENVIRONMENT VARIABLES
 M:	Rafał Miłecki <[email protected]>
 S:	Maintained
 F:	Documentation/devicetree/bindings/nvmem/u-boot,env.yaml
+F:	drivers/nvmem/u-boot-env.c
 
 UACCE ACCELERATOR FRAMEWORK
 M:	Zhangfei Gao <[email protected]>
drivers/nvmem/Kconfig +13 -0
index d72d879a6d34..bab8a29c9861 100644
@@ -344,4 +344,17 @@ config NVMEM_APPLE_EFUSES
 	  This driver can also be built as a module. If so, the module will
 	  be called nvmem-apple-efuses.
 
+config NVMEM_U_BOOT_ENV
+	tristate "U-Boot environment variables support"
+	depends on OF && MTD
+	select CRC32
+	help
+	  U-Boot stores its setup as environment variables. This driver adds
+	  support for verifying & exporting such data. It also exposes variables
+	  as NVMEM cells so they can be referenced by other drivers.
+
+	  Currently this drivers works only with env variables on top of MTD.
+
+	  If compiled as module it will be called nvmem_u-boot-env.
+
 endif
drivers/nvmem/Makefile +2 -0
index c710b64f9fe4..399f9972d45b 100644
@@ -69,3 +69,5 @@ obj-$(CONFIG_NVMEM_APPLE_EFUSES)	+= nvmem-apple-efuses.o
 nvmem-apple-efuses-y 		:= apple-efuses.o
 obj-$(CONFIG_MICROCHIP_OTPC)	+= nvmem-microchip-otpc.o
 nvmem-microchip-otpc-y		:= microchip-otpc.o
+obj-$(CONFIG_NVMEM_U_BOOT_ENV)	+= nvmem_u-boot-env.o
+nvmem_u-boot-env-y		:= u-boot-env.o
drivers/nvmem/u-boot-env.c new file +218 -0

218 lines not on this page · raw message

Signature · 1 line
2.34.1
Reviewed-by on v4 1/2
#2 lore raw
In reply to Rafał Miłecki
Quoted 7 lines · Rafał Miłecki

From: Rafał Miłecki <[email protected]>

MTD subsystem API allows interacting with MTD devices (e.g. reading, writing, handling bad blocks). So far a random driver could get MTD device only by its name (get_mtd_device_nm()). This change allows getting them also by a DT node.

Quoted 5 lines

This API is required for drivers handling DT defined MTD partitions in a specific way (e.g. U-Boot (sub)partition with environment variables).

Quoted 61 lines

---
V3: First introduction of of_get_mtd_device_by_node()
V4: Use EPROBE_DEFER

Srinivas: in V3 Miquel said it's OK to push this patch through NVMEM

---
 drivers/mtd/mtdcore.c   | 28 ++++++++++++++++++++++++++++
 include/linux/mtd/mtd.h |  1 +
 2 files changed, 29 insertions(+)
drivers/mtd/mtdcore.c +28 -0
index 9eb0680db312..3613cc142f25 100644
@@ -1154,6 +1154,34 @@ int __get_mtd_device(struct mtd_info *mtd)
 }
 EXPORT_SYMBOL_GPL(__get_mtd_device);
 
+/**
+ * of_get_mtd_device_by_node - obtain an MTD device associated with a given node
+ *
+ * @np: device tree node
+ */
+struct mtd_info *of_get_mtd_device_by_node(struct device_node *np)
+{
+	struct mtd_info *mtd = NULL;
+	struct mtd_info *tmp;
+	int err;
+
+	mutex_lock(&mtd_table_mutex);
+
+	err = -EPROBE_DEFER;
+	mtd_for_each_device(tmp) {
+		if (mtd_get_of_node(tmp) == np) {
+			mtd = tmp;
+			err = __get_mtd_device(mtd);
+			break;
+		}
+	}
+
+	mutex_unlock(&mtd_table_mutex);
+
+	return err ? ERR_PTR(err) : mtd;
+}
+EXPORT_SYMBOL_GPL(of_get_mtd_device_by_node);
+
 /**
  *	get_mtd_device_nm - obtain a validated handle for an MTD device by
  *	device name
include/linux/mtd/mtd.h +1 -0
index 955aee14b0f7..6fc841ceef31 100644
@@ -677,6 +677,7 @@ extern int mtd_device_unregister(struct mtd_info *master);
 extern struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num);
 extern int __get_mtd_device(struct mtd_info *mtd);
 extern void __put_mtd_device(struct mtd_info *mtd);
+extern struct mtd_info *of_get_mtd_device_by_node(struct device_node *np);
 extern struct mtd_info *get_mtd_device_nm(const char *name);
 extern void put_mtd_device(struct mtd_info *mtd);
 
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 |
#3 lore raw
In reply to Rafał Miłecki
Quoted 7 lines · Rafał Miłecki

From: Rafał Miłecki <[email protected]>

MTD subsystem API allows interacting with MTD devices (e.g. reading, writing, handling bad blocks). So far a random driver could get MTD device only by its name (get_mtd_device_nm()). This change allows getting them also by a DT node.

Quoted 6 lines

This API is required for drivers handling DT defined MTD partitions in a specific way (e.g. U-Boot (sub)partition with environment variables).

---

Applied along with nvmem provider thanks,
--srini

Quoted 60 lines

V3: First introduction of of_get_mtd_device_by_node()
V4: Use EPROBE_DEFER

Srinivas: in V3 Miquel said it's OK to push this patch through NVMEM

---
  drivers/mtd/mtdcore.c   | 28 ++++++++++++++++++++++++++++
  include/linux/mtd/mtd.h |  1 +
  2 files changed, 29 insertions(+)
drivers/mtd/mtdcore.c +28 -0
index 9eb0680db312..3613cc142f25 100644
@@ -1154,6 +1154,34 @@ int __get_mtd_device(struct mtd_info *mtd)
  }
  EXPORT_SYMBOL_GPL(__get_mtd_device);
 
+/**
+ * of_get_mtd_device_by_node - obtain an MTD device associated with a given node
+ *
+ * @np: device tree node
+ */
+struct mtd_info *of_get_mtd_device_by_node(struct device_node *np)
+{
+	struct mtd_info *mtd = NULL;
+	struct mtd_info *tmp;
+	int err;
+
+	mutex_lock(&mtd_table_mutex);
+
+	err = -EPROBE_DEFER;
+	mtd_for_each_device(tmp) {
+		if (mtd_get_of_node(tmp) == np) {
+			mtd = tmp;
+			err = __get_mtd_device(mtd);
+			break;
+		}
+	}
+
+	mutex_unlock(&mtd_table_mutex);
+
+	return err ? ERR_PTR(err) : mtd;
+}
+EXPORT_SYMBOL_GPL(of_get_mtd_device_by_node);
+
  /**
   *	get_mtd_device_nm - obtain a validated handle for an MTD device by
   *	device name
include/linux/mtd/mtd.h +1 -0
index 955aee14b0f7..6fc841ceef31 100644
@@ -677,6 +677,7 @@ extern int mtd_device_unregister(struct mtd_info *master);
  extern struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num);
  extern int __get_mtd_device(struct mtd_info *mtd);
  extern void __put_mtd_device(struct mtd_info *mtd);
+extern struct mtd_info *of_get_mtd_device_by_node(struct device_node *np);
  extern struct mtd_info *get_mtd_device_nm(const char *name);
  extern void put_mtd_device(struct mtd_info *mtd);