From patchwork Mon Feb 27 20:52:35 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 143273 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from theia.denx.de (theia.denx.de [85.214.87.163]) by ozlabs.org (Postfix) with ESMTP id 11DB4B6FD5 for ; Tue, 28 Feb 2012 07:54:16 +1100 (EST) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 7677B2832D; Mon, 27 Feb 2012 21:54:07 +0100 (CET) X-Virus-Scanned: Debian amavisd-new at theia.denx.de Received: from theia.denx.de ([127.0.0.1]) by localhost (theia.denx.de [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id hRbvFtZUz+P5; Mon, 27 Feb 2012 21:54:07 +0100 (CET) Received: from theia.denx.de (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 5FE962832F; Mon, 27 Feb 2012 21:53:46 +0100 (CET) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id D76D028337 for ; Mon, 27 Feb 2012 21:53:25 +0100 (CET) X-Virus-Scanned: Debian amavisd-new at theia.denx.de Received: from theia.denx.de ([127.0.0.1]) by localhost (theia.denx.de [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id bx6j3USFumIj for ; Mon, 27 Feb 2012 21:53:25 +0100 (CET) X-policyd-weight: NOT_IN_SBL_XBL_SPAMHAUS=-1.5 NOT_IN_SPAMCOP=-1.5 NOT_IN_BL_NJABL=-1.5 (only DNSBL check requested) Received: from mail-ee0-f74.google.com (mail-ee0-f74.google.com [74.125.83.74]) by theia.denx.de (Postfix) with ESMTPS id 3D80228320 for ; Mon, 27 Feb 2012 21:53:23 +0100 (CET) Received: by eekc41 with SMTP id c41so64524eek.3 for ; Mon, 27 Feb 2012 12:53:21 -0800 (PST) Received-SPF: pass (google.com: domain of sjg@google.com designates 10.14.127.10 as permitted sender) client-ip=10.14.127.10; Authentication-Results: mr.google.com; spf=pass (google.com: domain of sjg@google.com designates 10.14.127.10 as permitted sender) smtp.mail=sjg@google.com Received: from mr.google.com ([10.14.127.10]) by 10.14.127.10 with SMTP id c10mr1761109eei.2.1330376001570 (num_hops = 1); Mon, 27 Feb 2012 12:53:21 -0800 (PST) Received: by 10.14.127.10 with SMTP id c10mr1533046eei.2.1330376001554; Mon, 27 Feb 2012 12:53:21 -0800 (PST) MIME-Version: 1.0 Received: by 10.14.127.10 with SMTP id c10mr1533022eei.2.1330376001423; Mon, 27 Feb 2012 12:53:21 -0800 (PST) Received: from hpza10.eem.corp.google.com ([74.125.121.33]) by gmr-mx.google.com with ESMTPS id s3si11827925eem.3.2012.02.27.12.53.21 (version=TLSv1/SSLv3 cipher=AES128-SHA); Mon, 27 Feb 2012 12:53:21 -0800 (PST) Received: from sglass.mtv.corp.google.com (sglass.mtv.corp.google.com [172.22.72.144]) by hpza10.eem.corp.google.com (Postfix) with ESMTP id 03154200057; Mon, 27 Feb 2012 12:53:21 -0800 (PST) Received: by sglass.mtv.corp.google.com (Postfix, from userid 121222) id 5F1841412D6; Mon, 27 Feb 2012 12:53:20 -0800 (PST) From: Simon Glass To: U-Boot Mailing List Date: Mon, 27 Feb 2012 12:52:35 -0800 Message-Id: <1330375973-10681-3-git-send-email-sjg@chromium.org> X-Mailer: git-send-email 1.7.7.3 In-Reply-To: <1330375973-10681-1-git-send-email-sjg@chromium.org> References: <1330375973-10681-1-git-send-email-sjg@chromium.org> X-Gm-Message-State: ALoCoQmmf+MRPPoAK5Y/q7qRonmUoWzDShoWayHhSTC9hNxwDRd+RlRFUCZ3Bj9VteK3FEhtOtYQfgrAL2qWRLPdAUFGbR1u1qIeoPEHcCAGXvAHZg0cjE00wwbBoPR6j6iEZTMlDpM/Wz2Au1epI648ueJaie1H9lDgBzklVJn4DkwhK5czNyo= Cc: Devicetree Discuss , Tom Warren , Jerry Van Baren Subject: [U-Boot] [PATCH v6 02/20] fdt: Add functions to access phandles, arrays and bools X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.11 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: u-boot-bounces@lists.denx.de Errors-To: u-boot-bounces@lists.denx.de Add a function to look up 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, although there is no actual boolean type in U-Boot. Signed-off-by: Simon Glass Acked-by: Gerald Van Baren Acked-by: Stephen Warren --- include/fdtdec.h | 39 +++++++++++++++++++++++++++++++ lib/fdtdec.c | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 106 insertions(+), 0 deletions(-) diff --git a/include/fdtdec.h b/include/fdtdec.h index 01badd4..047f603 100644 --- a/include/fdtdec.h +++ b/include/fdtdec.h @@ -188,3 +188,42 @@ int fdtdec_find_aliases_for_id(const void *blob, const char *name, * @return compatible string for that id */ const char *fdtdec_get_compatible(enum fdt_compat_id id); + +/* 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, + u32 *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, regardless of its 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 + */ +int fdtdec_get_bool(const void *blob, int node, const char *prop_name); diff --git a/lib/fdtdec.c b/lib/fdtdec.c index 846ec3f..977528b 100644 --- a/lib/fdtdec.c +++ b/lib/fdtdec.c @@ -269,3 +269,70 @@ 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_check_min_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, + u32 *array, int count) +{ + const u32 *cell; + int i, err = 0; + + debug("%s: %s\n", __func__, prop_name); + cell = get_prop_check_min_len(blob, node, prop_name, + sizeof(u32) * count, &err); + if (!err) { + for (i = 0; i < count; i++) + array[i] = fdt32_to_cpu(cell[i]); + } + return err; +} + +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); + return cell != NULL; +}