From bc4147ab2d698bf7375a10af73ce34d76129edff Mon Sep 17 00:00:00 2001 From: Thierry Reding Date: Tue, 26 Aug 2014 17:33:50 +0200 Subject: fdt: Add a function to count strings Given a device tree node and a property name, the fdt_count_strings() function counts the number of strings found in the property value. Signed-off-by: Thierry Reding Acked-by: Simon Glass --- include/libfdt.h | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'include') diff --git a/include/libfdt.h b/include/libfdt.h index a1ef1e1..cf97bf0 100644 --- a/include/libfdt.h +++ b/include/libfdt.h @@ -857,6 +857,15 @@ int fdt_node_offset_by_compatible(const void *fdt, int startoffset, */ int fdt_stringlist_contains(const char *strlist, int listlen, const char *str); +/** + * fdt_count_strings - count the number of strings in a string list + * @fdt: pointer to the device tree blob + * @node: offset of the node + * @property: name of the property containing the string list + * @return: the number of strings in the given property + */ +int fdt_count_strings(const void *fdt, int node, const char *property); + /**********************************************************************/ /* Read-only functions (addressing related) */ /**********************************************************************/ -- cgit v1.1 From fc503c1791f35294d04495cd8ba3794bb6f15055 Mon Sep 17 00:00:00 2001 From: Thierry Reding Date: Tue, 26 Aug 2014 17:33:51 +0200 Subject: fdt: Add a function to get the index of a string Given a device tree node and a property name, the new fdt_find_string() function will look up a given string in the string list contained in the property's value and return its index. Signed-off-by: Thierry Reding Acked-by: Simon Glass --- include/libfdt.h | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'include') diff --git a/include/libfdt.h b/include/libfdt.h index cf97bf0..d0dea66 100644 --- a/include/libfdt.h +++ b/include/libfdt.h @@ -866,6 +866,17 @@ int fdt_stringlist_contains(const char *strlist, int listlen, const char *str); */ int fdt_count_strings(const void *fdt, int node, const char *property); +/** + * fdt_find_string - find a string in a string list and return its index + * @fdt: pointer to the device tree blob + * @node: offset of the node + * @property: name of the property containing the string list + * @string: string to look up in the string list + * @return: the index of the string or negative on error + */ +int fdt_find_string(const void *fdt, int node, const char *property, + const char *string); + /**********************************************************************/ /* Read-only functions (addressing related) */ /**********************************************************************/ -- cgit v1.1 From 5094eb408a5de69cce8e6bc5564fda10eb79eba0 Mon Sep 17 00:00:00 2001 From: Thierry Reding Date: Tue, 26 Aug 2014 17:33:52 +0200 Subject: fdt: Add functions to retrieve strings Given a device tree node, a property name and an index, the new function fdt_get_string_index() will return in an output argument a pointer to the index'th string in the property's value. The fdt_get_string() is a shortcut for the above with the index being 0. Signed-off-by: Thierry Reding Acked-by: Simon Glass --- include/libfdt.h | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'include') diff --git a/include/libfdt.h b/include/libfdt.h index d0dea66..2dfc6d9 100644 --- a/include/libfdt.h +++ b/include/libfdt.h @@ -877,6 +877,33 @@ int fdt_count_strings(const void *fdt, int node, const char *property); int fdt_find_string(const void *fdt, int node, const char *property, const char *string); +/** + * fdt_get_string_index() - obtain the string at a given index in a string list + * @fdt: pointer to the device tree blob + * @node: offset of the node + * @property: name of the property containing the string list + * @index: index of the string to return + * @output: return location for the string + * @return: 0 if the string was found or a negative error code otherwise + */ +int fdt_get_string_index(const void *fdt, int node, const char *property, + int index, const char **output); + +/** + * fdt_get_string() - obtain the string at a given index in a string list + * @fdt: pointer to the device tree blob + * @node: offset of the node + * @property: name of the property containing the string list + * @output: return location for the string + * @return: 0 if the string was found or a negative error code otherwise + * + * This is a shortcut for: + * + * fdt_get_string_index(fdt, node, property, 0, output). + */ +int fdt_get_string(const void *fdt, int node, const char *property, + const char **output); + /**********************************************************************/ /* Read-only functions (addressing related) */ /**********************************************************************/ -- cgit v1.1 From 56f42242f020254ea1c383694edb072e3a5ca6d6 Mon Sep 17 00:00:00 2001 From: Thierry Reding Date: Tue, 26 Aug 2014 17:33:53 +0200 Subject: fdt: Add resource parsing functions Add the fdt_get_resource() and fdt_get_named_resource() functions which can be used to parse resources (memory regions) from an FDT. A helper to compute the size of a region is also provided. Signed-off-by: Thierry Reding Acked-by: Simon Glass --- include/fdtdec.h | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) (limited to 'include') diff --git a/include/fdtdec.h b/include/fdtdec.h index 2590d30..8b24deb 100644 --- a/include/fdtdec.h +++ b/include/fdtdec.h @@ -40,6 +40,27 @@ struct fdt_memory { fdt_addr_t end; }; +/* + * Information about a resource. start is the first address of the resource + * and end is the last address (inclusive). The length of the resource will + * be equal to: end - start + 1. + */ +struct fdt_resource { + fdt_addr_t start; + fdt_addr_t end; +}; + +/** + * Compute the size of a resource. + * + * @param res the resource to operate on + * @return the size of the resource + */ +static inline fdt_size_t fdt_resource_size(const struct fdt_resource *res) +{ + return res->end - res->start + 1; +} + /** * Compat types that we know about and for which we might have drivers. * Each is named COMPAT__ where is the directory @@ -597,4 +618,35 @@ struct fmap_entry { */ int fdtdec_read_fmap_entry(const void *blob, int node, const char *name, struct fmap_entry *entry); + +/** + * Obtain an indexed resource from a device property. + * + * @param fdt FDT blob + * @param node node to examine + * @param property name of the property to parse + * @param index index of the resource to retrieve + * @param res returns the resource + * @return 0 if ok, negative on error + */ +int fdt_get_resource(const void *fdt, int node, const char *property, + unsigned int index, struct fdt_resource *res); + +/** + * Obtain a named resource from a device property. + * + * Look up the index of the name in a list of strings and return the resource + * at that index. + * + * @param fdt FDT blob + * @param node node to examine + * @param property name of the property to parse + * @param prop_names name of the property containing the list of names + * @param name the name of the entry to look up + * @param res returns the resource + */ +int fdt_get_named_resource(const void *fdt, int node, const char *property, + const char *prop_names, const char *name, + struct fdt_resource *res); + #endif -- cgit v1.1 From 9f85eee72a2a3918b2d5cd7b1bf88da69232dede Mon Sep 17 00:00:00 2001 From: Thierry Reding Date: Tue, 26 Aug 2014 17:33:54 +0200 Subject: fdt: Add a function to return PCI BDF triplet The fdtdec_pci_get_bdf() function returns the bus, device, function triplet of a PCI device by parsing the "reg" property according to the PCI device tree binding. Acked-by: Simon Glass Signed-off-by: Thierry Reding --- include/fdtdec.h | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'include') diff --git a/include/fdtdec.h b/include/fdtdec.h index 8b24deb..99cb353 100644 --- a/include/fdtdec.h +++ b/include/fdtdec.h @@ -649,4 +649,15 @@ int fdt_get_named_resource(const void *fdt, int node, const char *property, const char *prop_names, const char *name, struct fdt_resource *res); +/** + * Look at the reg property of a device node that represents a PCI device + * and parse the bus, device and function number from it. + * + * @param fdt FDT blob + * @param node node to examine + * @param bdf returns bus, device, function triplet + * @return 0 if ok, negative on error + */ +int fdtdec_pci_get_bdf(const void *fdt, int node, int *bdf); + #endif -- cgit v1.1 From 2f3760428ff3c4d5d1a087d016da5943921f0980 Mon Sep 17 00:00:00 2001 From: Thierry Reding Date: Tue, 26 Aug 2014 17:33:55 +0200 Subject: fdt: Add a subnodes iterator macro The fdt_for_each_subnode() iterator macro provided by this patch can be used to iterate over a device tree node's subnodes. At each iteration a loop variable will be set to the next subnode. Acked-by: Simon Glass Signed-off-by: Thierry Reding --- include/libfdt.h | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'include') diff --git a/include/libfdt.h b/include/libfdt.h index 2dfc6d9..f3cbb63 100644 --- a/include/libfdt.h +++ b/include/libfdt.h @@ -163,6 +163,31 @@ int fdt_first_subnode(const void *fdt, int offset); */ int fdt_next_subnode(const void *fdt, int offset); +/** + * fdt_for_each_subnode - iterate over all subnodes of a parent + * + * This is actually a wrapper around a for loop and would be used like so: + * + * fdt_for_each_subnode(fdt, node, parent) { + * ... + * use node + * ... + * } + * + * Note that this is implemented as a macro and node is used as iterator in + * the loop. It should therefore be a locally allocated variable. The parent + * variable on the other hand is never modified, so it can be constant or + * even a literal. + * + * @fdt: FDT blob (const void *) + * @node: child node (int) + * @parent: parent node (int) + */ +#define fdt_for_each_subnode(fdt, node, parent) \ + for (node = fdt_first_subnode(fdt, parent); \ + node >= 0; \ + node = fdt_next_subnode(fdt, node)) + /**********************************************************************/ /* General functions */ /**********************************************************************/ -- cgit v1.1