All of lore.kernel.org
 help / color / mirror / Atom feed
From: Thierry Reding <thierry.reding@gmail.com>
To: u-boot@lists.denx.de
Subject: [U-Boot] [PATCH 1/3] dm: core: Add dev_read_bytes()
Date: Mon, 15 Apr 2019 11:10:45 +0200	[thread overview]
Message-ID: <20190415091047.26218-1-thierry.reding@gmail.com> (raw)

From: Thierry Reding <treding@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@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

             reply	other threads:[~2019-04-15  9:10 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-04-15  9:10 Thierry Reding [this message]
2019-04-15  9:10 ` [U-Boot] [PATCH 2/3] net: eth-uclass: Write MAC address to hardware after probe Thierry Reding
2019-04-15 21:24   ` Joe Hershberger
2019-04-16  8:21     ` Thierry Reding
2019-04-15  9:10 ` [U-Boot] [PATCH 3/3] net: eth-uclass: Support device tree MAC addresses Thierry Reding
2019-04-15 21:26   ` Joe Hershberger
2019-04-15 21:21 ` [U-Boot] [PATCH 1/3] dm: core: Add dev_read_bytes() Joe Hershberger
2019-04-16  8:06   ` Thierry Reding

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190415091047.26218-1-thierry.reding@gmail.com \
    --to=thierry.reding@gmail.com \
    --cc=u-boot@lists.denx.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.