[U-Boot] [PATCH 1/3] dm: core: Add dev_read_bytes()

Thierry Reding thierry.reding at gmail.com
Mon Apr 15 09:10:45 UTC 2019


From: Thierry Reding <treding at nvidia.com>

This function can be used to read a binary property into a buffer. One
example where this is needed is to read a MAC address from device tree.

Signed-off-by: Thierry Reding <treding at nvidia.com>
---
 drivers/core/of_access.c | 21 +++++++++++++++++++++
 drivers/core/ofnode.c    | 13 +++++++++++++
 drivers/core/read.c      |  6 ++++++
 include/dm/of_access.h   | 15 +++++++++++++++
 include/dm/ofnode.h      | 11 +++++++++++
 include/dm/read.h        | 17 +++++++++++++++++
 6 files changed, 83 insertions(+)

diff --git a/drivers/core/of_access.c b/drivers/core/of_access.c
index 945b81448cce..d110d171a3ea 100644
--- a/drivers/core/of_access.c
+++ b/drivers/core/of_access.c
@@ -446,6 +446,27 @@ static void *of_find_property_value_of_size(const struct device_node *np,
 	return prop->value;
 }
 
+int of_read_bytes(const struct device_node *np, const char *propname,
+		  u8 *buffer, int size)
+{
+	const fdt32_t *value;
+
+	debug("%s: %s: ", __func__, propname);
+
+	if (!np)
+		return -EINVAL;
+
+	value = of_find_property_value_of_size(np, propname, size);
+	if (IS_ERR(value)) {
+		debug("(not found)\n");
+		return PTR_ERR(value);
+	}
+
+	memcpy(buffer, value, size);
+
+	return 0;
+}
+
 int of_read_u32(const struct device_node *np, const char *propname, u32 *outp)
 {
 	const __be32 *val;
diff --git a/drivers/core/ofnode.c b/drivers/core/ofnode.c
index 785f5c3acf7a..21b24e5aa00e 100644
--- a/drivers/core/ofnode.c
+++ b/drivers/core/ofnode.c
@@ -15,6 +15,19 @@
 #include <linux/err.h>
 #include <linux/ioport.h>
 
+int ofnode_read_bytes(ofnode node, const char *propname, u8 *buffer, int size)
+{
+	assert(ofnode_valid(node));
+	debug("%s: %s: ", __func__, propname);
+
+	if (ofnode_is_np(node))
+		return of_read_bytes(ofnode_to_np(node), propname, buffer,
+				     size);
+
+	return fdtdec_get_byte_array(gd->fdt_blob, ofnode_to_offset(node),
+				     propname, buffer, size);
+}
+
 int ofnode_read_u32(ofnode node, const char *propname, u32 *outp)
 {
 	assert(ofnode_valid(node));
diff --git a/drivers/core/read.c b/drivers/core/read.c
index 6bda077a34b9..9919ec19d4d8 100644
--- a/drivers/core/read.c
+++ b/drivers/core/read.c
@@ -11,6 +11,12 @@
 #include <mapmem.h>
 #include <dm/of_access.h>
 
+int dev_read_bytes(struct udevice *dev, const char *propname, u8 *buffer,
+		   int size)
+{
+	return ofnode_read_bytes(dev_ofnode(dev), propname, buffer, size);
+}
+
 int dev_read_u32(struct udevice *dev, const char *propname, u32 *outp)
 {
 	return ofnode_read_u32(dev_ofnode(dev), propname, outp);
diff --git a/include/dm/of_access.h b/include/dm/of_access.h
index 13fedb7cf5e6..fc6a86959b23 100644
--- a/include/dm/of_access.h
+++ b/include/dm/of_access.h
@@ -218,6 +218,21 @@ struct device_node *of_find_node_by_prop_value(struct device_node *from,
  */
 struct device_node *of_find_node_by_phandle(phandle handle);
 
+/**
+ * of_read_bytes() - Find and read an array of bytes from a property
+ *
+ * Search for a property in a device node and read an array of bytes from it.
+ *
+ * @np: device node from which the property is to be read
+ * @propname: name of the property to be read
+ * @buffer: buffer to read the property value into
+ * @size: number of bytes to read
+ *
+ * @return 0 on success, or a negative error-code on failure.
+ */
+int of_read_bytes(const struct device_node *np, const char *propname,
+		  u8 *buffer, int size);
+
 /**
  * of_read_u32() - Find and read a 32-bit integer from a property
  *
diff --git a/include/dm/ofnode.h b/include/dm/ofnode.h
index d206ee2caab7..5158296dfd96 100644
--- a/include/dm/ofnode.h
+++ b/include/dm/ofnode.h
@@ -192,6 +192,17 @@ static inline ofnode ofnode_null(void)
 	return node;
 }
 
+/**
+ * ofnode_read_bytes() - Read an array of bytes from a property
+ *
+ * @node: valid node reference to read property from
+ * @propname: name of the property to read from
+ * @buffer: buffer to read the value into
+ * @size: size of @buffer
+ * @return 0 on success, or a negative error code on failure
+ */
+int ofnode_read_bytes(ofnode node, const char *propname, u8 *buffer, int size);
+
 /**
  * ofnode_read_u32() - Read a 32-bit integer from a property
  *
diff --git a/include/dm/read.h b/include/dm/read.h
index 60b727cbd821..cb9776b39721 100644
--- a/include/dm/read.h
+++ b/include/dm/read.h
@@ -44,6 +44,18 @@ static inline bool dev_of_valid(struct udevice *dev)
 }
 
 #ifndef CONFIG_DM_DEV_READ_INLINE
+/**
+ * dev_read_bytes() - read an array of bytes from a device's DT property
+ *
+ * @dev: device to read DT property from
+ * @propname: name of the property to read from
+ * @buffer: buffer to read the value into
+ * @size: size of @buffer
+ * @return 0 on success, or a negative error code on failure
+ */
+int dev_read_bytes(struct udevice *dev, const char *propname, u8 *buffer,
+		   int size);
+
 /**
  * dev_read_u32() - read a 32-bit integer from a device's DT property
  *
@@ -522,6 +534,11 @@ u64 dev_translate_address(struct udevice *dev, const fdt32_t *in_addr);
 int dev_read_alias_highest_id(const char *stem);
 
 #else /* CONFIG_DM_DEV_READ_INLINE is enabled */
+static inline int dev_read_bytes(struct udevice *dev, const char *propname,
+				 u8 *buffer, int size)
+{
+	return ofnode_read_bytes(dev_ofnode(dev), propname, buffer, size);
+}
 
 static inline int dev_read_u32(struct udevice *dev,
 			       const char *propname, u32 *outp)
-- 
2.21.0



More information about the U-Boot mailing list