From: Simon Glass <sjg-F7+t8E8rja9g9hUCZPvPmw@public.gmane.org>
To: U-Boot Mailing List <u-boot-0aAXYlwwYIKGBzrmiIFOJg@public.gmane.org>
Cc: Devicetree Discuss
<devicetree-discuss-uLR06cmDAlY/bJ5BZ2RsiQ@public.gmane.org>,
Albert ARIBAUD
<albert.u.boot-LhW3hqR2+23R7s880joybQ@public.gmane.org>,
Tom Warren <twarren-DDmLM1+adcrQT0dZR+AlfA@public.gmane.org>
Subject: [PATCH 02/14] fdt: Add functions to access phandles, arrays and bools
Date: Wed, 23 Nov 2011 19:54:44 -0800 [thread overview]
Message-ID: <1322106896-23054-3-git-send-email-sjg@chromium.org> (raw)
In-Reply-To: <1322106896-23054-2-git-send-email-sjg-F7+t8E8rja9g9hUCZPvPmw@public.gmane.org>
Add a function to lookup a property which is a phandle in a node, and
another to read a fixed-length integer array from an fdt property.
Also add a function to read boolean properties.
Signed-off-by: Simon Glass <sjg-F7+t8E8rja9g9hUCZPvPmw@public.gmane.org>
---
include/fdtdec.h | 40 ++++++++++++++++++++++++++
lib/fdtdec.c | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 122 insertions(+), 0 deletions(-)
diff --git a/include/fdtdec.h b/include/fdtdec.h
index 9018181..9ecb6ab 100644
--- a/include/fdtdec.h
+++ b/include/fdtdec.h
@@ -126,3 +126,43 @@ int fdtdec_get_is_enabled(const void *blob, int node);
* if not.
*/
int fdtdec_check_fdt(void);
+
+/**
+ * Look up a phandle and follow it to its node. Then return the offset
+ * of that node.
+ *
+ * @param blob FDT blob
+ * @param node node to examine
+ * @param prop_name name of property to find
+ * @return node offset if found, -ve error code on error
+ */
+int fdtdec_lookup_phandle(const void *blob, int node, const char *prop_name);
+
+/**
+ * Look up a property in a node and return its contents in an integer
+ * array of given length. The property must have at least enough data for
+ * the array (4*count bytes). It may have more, but this will be ignored.
+ *
+ * @param blob FDT blob
+ * @param node node to examine
+ * @param prop_name name of property to find
+ * @param array array to fill with data
+ * @param count number of array elements
+ * @return 0 if ok, or -FDT_ERR_NOTFOUND if the property is not found,
+ * or -FDT_ERR_BADLAYOUT if not enough data
+ */
+int fdtdec_get_int_array(const void *blob, int node, const char *prop_name,
+ int *array, int count);
+
+/**
+ * Look up a boolean property in a node and return it.
+ *
+ * A boolean properly is true if present in the device tree and false if not
+ * present, or present with a 0 value.
+ *
+ * @param blob FDT blob
+ * @param node node to examine
+ * @param prop_name name of property to find
+ * @return 1 if the properly is present; 0 if it isn't present or is 0
+ */
+int fdtdec_get_bool(const void *blob, int node, const char *prop_name);
diff --git a/lib/fdtdec.c b/lib/fdtdec.c
index d1321a8..613547a 100644
--- a/lib/fdtdec.c
+++ b/lib/fdtdec.c
@@ -146,3 +146,85 @@ int fdtdec_check_fdt(void)
"binary or define CONFIG_OF_EMBED\n");
return 0;
}
+
+int fdtdec_lookup_phandle(const void *blob, int node, const char *prop_name)
+{
+ const u32 *phandle;
+ int lookup;
+
+ phandle = fdt_getprop(blob, node, prop_name, NULL);
+ if (!phandle)
+ return -FDT_ERR_NOTFOUND;
+
+ lookup = fdt_node_offset_by_phandle(blob, fdt32_to_cpu(*phandle));
+ return lookup;
+}
+
+/**
+ * Look up a property in a node and check that it has a minimum length.
+ *
+ * @param blob FDT blob
+ * @param node node to examine
+ * @param prop_name name of property to find
+ * @param min_len minimum property length in bytes
+ * @param err 0 if ok, or -FDT_ERR_NOTFOUND if the property is not
+ found, or -FDT_ERR_BADLAYOUT if not enough data
+ * @return pointer to cell, which is only valid if err == 0
+ */
+static const void *get_prop_len(const void *blob, int node,
+ const char *prop_name, int min_len, int *err)
+{
+ const void *cell;
+ int len;
+
+ debug("%s: %s\n", __func__, prop_name);
+ cell = fdt_getprop(blob, node, prop_name, &len);
+ if (!cell)
+ *err = -FDT_ERR_NOTFOUND;
+ else if (len < min_len)
+ *err = -FDT_ERR_BADLAYOUT;
+ else
+ *err = 0;
+ return cell;
+}
+
+int fdtdec_get_int_array(const void *blob, int node, const char *prop_name,
+ int *array, int count)
+{
+ const s32 *cell;
+ int i, err = 0;
+
+ debug("%s: %s\n", __func__, prop_name);
+ cell = get_prop_len(blob, node, prop_name, sizeof(s32) * count, &err);
+ if (!err) {
+ for (i = 0; i < count; i++)
+ array[i] = fdt32_to_cpu(cell[i]);
+ }
+ return err;
+}
+
+/**
+ * Look up a boolean property in a node and return it.
+ *
+ * A boolean properly is true if present in the device tree and false if not
+ * present, or present with a 0 value.
+ *
+ * @param blob FDT blob
+ * @param node node to examine
+ * @param prop_name name of property to find
+ * @return 1 if the properly is present; 0 if it isn't present or is 0
+ */
+int fdtdec_get_bool(const void *blob, int node, const char *prop_name)
+{
+ const s32 *cell;
+ int len;
+
+ debug("%s: %s\n", __func__, prop_name);
+ cell = fdt_getprop(blob, node, prop_name, &len);
+ if (!cell)
+ return 0;
+ if (len >= sizeof(u32) && *cell == 0)
+ return 0;
+
+ return 1;
+}
--
1.7.3.1
next prev parent reply other threads:[~2011-11-24 3:54 UTC|newest]
Thread overview: 28+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-11-24 3:54 [PATCH 01/14] fdt: Tidy up a few fdtdec problems Simon Glass
2011-11-24 3:54 ` [PATCH 03/14] arm: fdt: Ensure that an embedded fdt is word-aligned Simon Glass
[not found] ` <1322106896-23054-2-git-send-email-sjg-F7+t8E8rja9g9hUCZPvPmw@public.gmane.org>
2011-11-24 3:54 ` Simon Glass [this message]
[not found] ` <1322106896-23054-3-git-send-email-sjg-F7+t8E8rja9g9hUCZPvPmw@public.gmane.org>
2011-11-28 18:41 ` [PATCH 02/14] fdt: Add functions to access phandles, arrays and bools Stephen Warren
[not found] ` <4ED3D5DC.10502-DDmLM1+adcrQT0dZR+AlfA@public.gmane.org>
2011-11-29 5:12 ` David Gibson
2011-12-02 1:01 ` Simon Glass
[not found] ` <CAPnjgZ29tsNXd1+1eXdTHRjgh_MQJrXoc23_oqO9UPJ73mu7ZA-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2011-12-02 15:55 ` Stephen Warren
2011-12-02 16:38 ` Simon Glass
2011-12-02 3:33 ` Jerry Van Baren
2011-12-02 4:58 ` Simon Glass
2011-12-02 17:22 ` Jerry Van Baren
2011-12-02 18:12 ` Simon Glass
2011-11-24 3:54 ` [PATCH 04/14] arm: fdt: Add skeleton device tree file Simon Glass
2011-11-24 3:54 ` [PATCH 05/14] tegra: fdt: Add Tegra2x " Simon Glass
[not found] ` <1322106896-23054-6-git-send-email-sjg-F7+t8E8rja9g9hUCZPvPmw@public.gmane.org>
2011-11-28 18:56 ` Stephen Warren
2011-12-02 1:24 ` Simon Glass
[not found] ` <CAPnjgZ11cFm15E9MHXno_YGp0NxdOHhGBavYbQBP5Nu_TOtx7A-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2011-12-02 15:58 ` Stephen Warren
2011-12-02 16:47 ` Simon Glass
2011-11-24 3:54 ` [PATCH 07/14] tegra: fdt: Add initial device tree definitions for USB ports Simon Glass
2011-11-24 3:54 ` [PATCH 14/14] tegra: fdt: Enable FDT support for Seaboard Simon Glass
2011-11-28 18:33 ` [PATCH 01/14] fdt: Tidy up a few fdtdec problems Stephen Warren
[not found] ` <4ED3D3F2.8070302-DDmLM1+adcrQT0dZR+AlfA@public.gmane.org>
2011-11-29 1:10 ` David Gibson
2011-12-01 20:59 ` Simon Glass
2011-11-24 3:54 ` [PATCH 06/14] tegra: fdt: Add device tree file for Tegra2 Seaboard Simon Glass
[not found] ` <1322106896-23054-11-git-send-email-sjg@chromium.org>
[not found] ` <4ED3DF1D.6080009@nvidia.com>
[not found] ` <CAPnjgZ1-yxq-O-vsy1gNJ8AOzh7Sz9j5PG9Tx1sKpnAxhApJAQ@mail.gmail.com>
[not found] ` <4EDD38AC.30908@nvidia.com>
[not found] ` <CAPnjgZ0MTaPa4pq1VXk_XbmuDyWTfy-H33W4deNVn2ALUrcW_A@mail.gmail.com>
[not found] ` <4EDD429E.8010507@nvidia.com>
[not found] ` <CAPnjgZ2G220G-i3PPtS4XJYY0ornWFOoK3=b93jG-6GE_g2O5w@mail.gmail.com>
[not found] ` <4EDD5F1B.5060205@nvidia.com>
[not found] ` <CAPnjgZ2h7F228q=pXuf7M9vTASurY3R5an_QV-mrETLf8nzA1A@mail.gmail.com>
[not found] ` <4EDE7E1C.2090203@nvidia.com>
[not found] ` <CAPnjgZ1G+mv6uv8SrdMm7DoqFjeeyVHYv6nbQxn9qixfbQMGvA@mail.gmail.com>
[not found] ` <CAPnjgZ1G+mv6uv8SrdMm7DoqFjeeyVHYv6nbQxn9qixfbQMGvA-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2011-12-07 23:46 ` [PATCH 10/14] tegra: usb: Add support for USB peripheral Stephen Warren
2011-12-08 21:24 ` Simon Glass
[not found] ` <CAPnjgZ0AuBNkYKN0JHQyc7DdzwUSZivR+Dv2BkiFsKQBNMeP8A-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2011-12-12 18:18 ` Stephen Warren
2011-12-12 18:42 ` Simon Glass
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=1322106896-23054-3-git-send-email-sjg@chromium.org \
--to=sjg-f7+t8e8rja9g9huczpvpmw@public.gmane.org \
--cc=albert.u.boot-LhW3hqR2+23R7s880joybQ@public.gmane.org \
--cc=devicetree-discuss-uLR06cmDAlY/bJ5BZ2RsiQ@public.gmane.org \
--cc=twarren-DDmLM1+adcrQT0dZR+AlfA@public.gmane.org \
--cc=u-boot-0aAXYlwwYIKGBzrmiIFOJg@public.gmane.org \
/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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).