Skip to contentSkip to conversation

dm: Add livetree access functions

PATCH New Simon Glass 1 participant 18 messages Jan 18, 2017

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

PATCH 5/22 dm: Add livetree access functions

Add a basic assortment of functions to access the live device tree.

---

 include/dm/of_access.h | 164 +++++++++++++++++++++++++
 lib/Makefile           |   1 +
 lib/of_access.c        | 328 +++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 493 insertions(+)
 create mode 100644 include/dm/of_access.h
 create mode 100644 lib/of_access.c
include/dm/of_access.h new file +164 -0
new file mode 100644
index 00000000000..9a157d8a5b7
@@ -0,0 +1,164 @@
+/*
+ * Originally from Linux v4.9
+ * Copyright (C) 1996-2005 Paul Mackerras.
+ *
+ * Updates for PPC64 by Peter Bergner & David Engebretsen, IBM Corp.
+ * Updates for SPARC64 by David S. Miller
+ * Derived from PowerPC and Sparc prom.h files by Stephen Rothwell, IBM Corp.
+ *
+ * Copyright (c) 2017 Google, Inc
+ * Written by Simon Glass <[email protected]>
+ *
+ * Modified for U-Boot
+ * Copyright (c) 2017 Google, Inc
+ *
+ * SPDX-License-Identifier:	GPL-2.0+
+ */
+
+#ifndef _DM_OF_ACCESS_H
+#define _DM_OF_ACCESS_H
+
+#include <dm/of.h>
+
+/**
+ * of_find_all_nodes - Get next node in global list
+ * @prev:	Previous node or NULL to start iteration
+ *		of_node_put() will be called on it
+ *
+ * Returns a node pointer with refcount incremented, use
+ * of_node_put() on it when done.
+ */
+struct device_node *of_find_all_nodes(struct device_node *prev);
+
+#define for_each_of_allnodes_from(from, dn) \
+	for (dn = __of_find_all_nodes(from); dn; dn = __of_find_all_nodes(dn))
+#define for_each_of_allnodes(dn) for_each_of_allnodes_from(NULL, dn)
+
+/* Dummy functions to mirror Linux. These are not yet used in U-Boot */
+static inline struct device_node *of_node_get(struct device_node *node)
+{
+	return node;
+}
+
+static inline void of_node_put(struct device_node *node) { }
+
+/**
+ * of_find_property() - find a property in a node
+ *
+ * @np: Pointer to device node holding property
+ * @name: Name of property
+ * @lenp: If non-NULL, returns length of property
+ * @return pointer to property, or NULL if not found
+ */
+struct property *of_find_property(const struct device_node *np,
+				  const char *name, int *lenp);
+
+/*
+ * Find a property with a given name for a given node
+ * and return the value.
+ */
+const void *of_get_property(const struct device_node *np, const char *name,
+			    int *lenp);
+
+/**
+ *  of_device_is_available - check if a device is available for use
+ *
+ *  @device: Node to check for availability, with locks already held
+ *
+ *  Returns true if the status property is absent or set to "okay" or "ok",
+ *  false otherwise
+ */
+bool of_device_is_available(const struct device_node *device);
+
+/**
+ * of_find_node_by_phandle - Find a node given a phandle
+ * @handle:	phandle of the node to find
+ *
+ * Returns a node pointer with refcount incremented, use
+ * of_node_put() on it when done.
+ */
+struct device_node *of_find_node_by_phandle(phandle handle);
+
+/**
+ * of_parse_phandle - Resolve a phandle property to a device_node pointer
+ * @np: Pointer to device node holding phandle property
+ * @phandle_name: Name of property holding a phandle value
+ * @index: For properties holding a table of phandles, this is the index into
+ *         the table
+ *
+ * Returns the device_node pointer with refcount incremented.  Use
+ * of_node_put() on it when done.
+ */
+struct device_node *of_parse_phandle(const struct device_node *np,
+				     const char *phandle_name, int index);
+
+/**
+ * of_parse_phandle_with_args() - Find a node pointed by phandle in a list
+ * @np:		pointer to a device tree node containing a list
+ * @list_name:	property name that contains a list
+ * @cells_name:	property name that specifies phandles' arguments count
+ * @index:	index of a phandle to parse out
+ * @out_args:	optional pointer to output arguments structure (will be filled)
+ *
+ * This function is useful to parse lists of phandles and their arguments.
+ * Returns 0 on success and fills out_args, on error returns appropriate
+ * errno value.
+ *
+ * Caller is responsible to call of_node_put() on the returned out_args->np
+ * pointer.
+ *
+ * Example:
+ *
+ * phandle1: node1 {
+ *	#list-cells = <2>;
+ * }
+ *
+ * phandle2: node2 {
+ *	#list-cells = <1>;
+ * }
+ *
+ * node3 {
+ *	list = <&phandle1 1 2 &phandle2 3>;
+ * }
+ *
+ * To get a device_node of the `node2' node you may call this:
+ * of_parse_phandle_with_args(node3, "list", "#list-cells", 1, &args);
+ */
+int of_parse_phandle_with_args(const struct device_node *np,
+			       const char *list_name, const char *cells_name,
+			       int index, struct of_phandle_args *out_args);
+
+/**
+ * of_read_u32() - Find and read a 32-bit integer from a property
+ * @np:		device node from which the property value is to be read.
+ * @propname:	name of the property to be searched.
+ * @outp:	pointer to return value, modified only if return value is 0.
+ *
+ * Search for a property in a device node and read a 32-bit value from
+ * it. Returns 0 on success, -EINVAL if the property does not exist,
+ * -ENODATA if property does not have a value, and -EOVERFLOW if the
+ * property data isn't large enough.
+ *
+ * The out_value is modified only if a valid u64 value can be decoded.
+ */
+int of_read_u32(const struct device_node *np, const char *propname, u32 *outp);
+
+/**
+ * of_read_u32_array() - Find and read an array of 32 bit integers
+ *
+ * @np:		device node from which the property value is to be read.
+ * @propname:	name of the property to be searched.
+ * @out_values:	pointer to return value, modified only if return value is 0.
+ * @sz:		number of array elements to read
+ * @return 0 on success, -EINVAL if the property does not exist, -ENODATA
+ * if property does not have a value, and -EOVERFLOW is longer than sz.
+ *
+ * Search for a property in a device node and read 32-bit value(s) from
+ * it.
+ *
+ * The out_values is modified only if a valid u32 value can be decoded.
+ */
+int of_read_u32_array(const struct device_node *np, const char *propname,
+		      u32 *out_values, size_t sz);
+
+#endif
lib/Makefile +1 -0
index 23e9f1ef11d..d07a062483a 100644
@@ -15,6 +15,7 @@ obj-$(CONFIG_ZLIB) += zlib/
 obj-$(CONFIG_BZIP2) += bzip2/
 obj-$(CONFIG_TIZEN) += tizen/
 obj-$(CONFIG_FIT) += libfdt/
+obj-$(CONFIG_OF_LIVE) += of_access.o
 obj-$(CONFIG_CMD_DHRYSTONE) += dhry/
 
 obj-$(CONFIG_AES) += aes.o
lib/of_access.c new file +328 -0

328 lines not on this page · raw message

Signature · 1 line
2.11.0.483.g087da7b7c-goog
#1 lore raw

PATCH 6/22 dm: Add a function to create a 'live' device tree

This function converts the flat device tree into a hierarchical one with C structures and pointers. This is easier to access.

---

 include/livetree.h |  22 ++++
 lib/Makefile       |   2 +-
 lib/livetree.c     | 320 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 343 insertions(+), 1 deletion(-)
 create mode 100644 include/livetree.h
 create mode 100644 lib/livetree.c
include/livetree.h new file +22 -0
new file mode 100644
index 00000000000..aa0a8687c1b
@@ -0,0 +1,22 @@
+/*
+ * Copyright (c) 2017 Google, Inc
+ * Written by Simon Glass <[email protected]>
+ *
+ * SPDX-License-Identifier:	GPL-2.0+
+ *
+ * Support for a 'live' (as opposed to flat) device tree
+ */
+
+#ifndef _LIVETREE_H
+#define _LIVETREE_H
+
+/**
+ * livetree_build() - build a live (hierarchical) tree from a flat DT
+ *
+ * @fdt_blob: Input tree to convert
+ * @rootp: Returns live tree that was created
+ * @return 0 if OK, -ve on error
+ */
+int livetree_build(const void *fdt_blob, struct device_node **rootp);
+
+#endif
lib/Makefile +1 -1
index d07a062483a..1c76c59374d 100644
@@ -15,7 +15,7 @@ obj-$(CONFIG_ZLIB) += zlib/
 obj-$(CONFIG_BZIP2) += bzip2/
 obj-$(CONFIG_TIZEN) += tizen/
 obj-$(CONFIG_FIT) += libfdt/
-obj-$(CONFIG_OF_LIVE) += of_access.o
+obj-$(CONFIG_OF_LIVE) += livetree.o of_access.o
 obj-$(CONFIG_CMD_DHRYSTONE) += dhry/
 
 obj-$(CONFIG_AES) += aes.o
lib/livetree.c new file +320 -0

320 lines not on this page · raw message

Signature · 1 line
2.11.0.483.g087da7b7c-goog
#2 lore raw

PATCH 7/22 dm: Build a live tree after relocation

In reply to Simon Glass

If enabled, build a live device tree after relocation. This can then be used by driver model.

---

 common/board_r.c   | 12 ++++++++++++
 include/livetree.h |  2 ++
 2 files changed, 14 insertions(+)
common/board_r.c +12 -0
index a3733526c69..3c865720036 100644
@@ -33,6 +33,7 @@
 #if defined(CONFIG_CMD_KGDB)
 #include <kgdb.h>
 #endif
+#include <livetree.h>
 #include <logbuff.h>
 #include <malloc.h>
 #include <mapmem.h>
@@ -314,6 +315,14 @@ static int initr_noncached(void)
 }
 #endif
 
+#ifdef CONFIG_OF_LIVE
+static int initr_livetree(void)
+{
+	return livetree_build(gd->fdt_blob,
+			      (struct device_node **)&gd->of_root);
+}
+#endif
+
 #ifdef CONFIG_DM
 static int initr_dm(void)
 {
@@ -781,6 +790,9 @@ init_fnc_t init_sequence_r[] = {
 	initr_noncached,
 #endif
 	bootstage_relocate,
+#ifdef CONFIG_OF_LIVE
+	initr_livetree,
+#endif
 #ifdef CONFIG_DM
 	initr_dm,
 #endif
include/livetree.h +2 -0
index aa0a8687c1b..8f8fee73114 100644
@@ -10,6 +10,8 @@
 #ifndef _LIVETREE_H
 #define _LIVETREE_H
 
+struct device_node;
+
 /**
  * livetree_build() - build a live (hierarchical) tree from a flat DT
  *
Signature · 1 line
2.11.0.483.g087da7b7c-goog
#3 lore raw

PATCH 8/22 dm: Add support for device tree references

In reply to Simon Glass

Since U-Boot supports both a live tree and a flat tree, we need an easy way to access the tree without bothering about which is currently active. To support this, introduce the concept of an of_node_ref, which can refer either to a live tree node or a flat tree node.

For the live tree, the reference contains a pointer to the node (struct device_node *) or NULL if the node is invalid. For the flat tree, the reference contains the node offset or -1 if the node is invalid.

A basic set of operations is provided which use references. These are implemented by using either libfdt functions (in the case of a flat DT reference) or the live-tree of_...() functions.

Note that it is not possible to have both live and flat references active at the same time. As soon as the live tree is available, everything in U-Boot should switch to using that. This avoids confusion and allows us to assume that the type of a reference is simply based on whether we have a live tree yet, or not.

---

 drivers/core/Makefile |   1 +
 drivers/core/of_ref.c | 218 +++++++++++++++++++++++++++++++++++
 include/dm/of_ref.h   | 306 ++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 525 insertions(+)
 create mode 100644 drivers/core/of_ref.c
 create mode 100644 include/dm/of_ref.h
drivers/core/Makefile +1 -0
index 07adb61c285..0ffc38601f4 100644
@@ -11,3 +11,4 @@ obj-$(CONFIG_$(SPL_)SIMPLE_BUS)	+= simple-bus.o
 obj-$(CONFIG_DM)	+= dump.o
 obj-$(CONFIG_$(SPL_)REGMAP)	+= regmap.o
 obj-$(CONFIG_$(SPL_)SYSCON)	+= syscon-uclass.o
+obj-$(CONFIG_OF_CONTROL) += of_ref.o
drivers/core/of_ref.c new file +218 -0

218 lines not on this page · raw message

include/dm/of_ref.h new file +306 -0

306 lines not on this page · raw message

Signature · 1 line
2.11.0.483.g087da7b7c-goog
#4 lore raw

PATCH 9/22 dm: Add a place to put extra device-tree reading functions

In reply to Simon Glass

Some functions deal with structured data rather than simple data types. It makes sense to have these in their own file. For now this just has a function to read a flashmap entry.

---

 drivers/core/Makefile   |  2 +-
 drivers/core/of_extra.c | 37 +++++++++++++++++++++++++++++++++++++
 include/dm/of_extra.h   | 46 ++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 84 insertions(+), 1 deletion(-)
 create mode 100644 drivers/core/of_extra.c
 create mode 100644 include/dm/of_extra.h
drivers/core/Makefile +1 -1
index 0ffc38601f4..6d7040c2603 100644
@@ -11,4 +11,4 @@ obj-$(CONFIG_$(SPL_)SIMPLE_BUS)	+= simple-bus.o
 obj-$(CONFIG_DM)	+= dump.o
 obj-$(CONFIG_$(SPL_)REGMAP)	+= regmap.o
 obj-$(CONFIG_$(SPL_)SYSCON)	+= syscon-uclass.o
-obj-$(CONFIG_OF_CONTROL) += of_ref.o
+obj-$(CONFIG_OF_CONTROL) += of_extra.o of_ref.o
drivers/core/of_extra.c new file +37 -0
new file mode 100644
index 00000000000..76b2bd93b9f
@@ -0,0 +1,37 @@
+/*
+ * Copyright (c) 2017 Google, Inc
+ * Written by Simon Glass <[email protected]>
+ *
+ * SPDX-License-Identifier:	GPL-2.0+
+ */
+
+#include <common.h>
+#include <libfdt.h>
+#include <dm/of_access.h>
+#include <dm/of_extra.h>
+#include <dm/of_ref.h>
+
+int of_read_fmap_entry(of_node_ref node_ref, const char *name,
+		       struct fmap_entry *entry)
+{
+	const char *prop;
+	u32 reg[2];
+
+	if (of_ref_read_u32_array(node_ref, "reg", reg, 2)) {
+		debug("Node '%s' has bad/missing 'reg' property\n", name);
+		return -FDT_ERR_NOTFOUND;
+	}
+	entry->offset = reg[0];
+	entry->length = reg[1];
+	entry->used = of_ref_read_s32_default(node_ref, "used", entry->length);
+	prop = of_ref_read_string(node_ref, "compress");
+	entry->compress_algo = prop && !strcmp(prop, "lzo") ?
+		FMAP_COMPRESS_LZO : FMAP_COMPRESS_NONE;
+	prop = of_ref_read_string(node_ref, "hash");
+	if (prop)
+		entry->hash_size = strlen(prop);
+	entry->hash_algo = prop ? FMAP_HASH_SHA256 : FMAP_HASH_NONE;
+	entry->hash = (uint8_t *)prop;
+
+	return 0;
+}
include/dm/of_extra.h new file +46 -0

46 lines not on this page · raw message

Signature · 1 line
2.11.0.483.g087da7b7c-goog
#5 lore raw

PATCH 10/22 dm: core: Add device-based functions to access DT properties

In reply to Simon Glass

It is common to read a device-tree property from the node associated with a device. Add convenience functions to do this so that drivers do not need to deal with the difference between live tree and flat tree access methods.

---

 drivers/core/Makefile |  2 +-
 drivers/core/of_dev.c | 55 +++++++++++++++++++++++++++++++++++++++++
 include/dm/of_dev.h   | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 124 insertions(+), 1 deletion(-)
 create mode 100644 drivers/core/of_dev.c
 create mode 100644 include/dm/of_dev.h
drivers/core/Makefile +1 -1
index 6d7040c2603..be1e9da55c0 100644
@@ -11,4 +11,4 @@ obj-$(CONFIG_$(SPL_)SIMPLE_BUS)	+= simple-bus.o
 obj-$(CONFIG_DM)	+= dump.o
 obj-$(CONFIG_$(SPL_)REGMAP)	+= regmap.o
 obj-$(CONFIG_$(SPL_)SYSCON)	+= syscon-uclass.o
-obj-$(CONFIG_OF_CONTROL) += of_extra.o of_ref.o
+obj-$(CONFIG_OF_CONTROL) += of_dev.o of_extra.o of_ref.o
drivers/core/of_dev.c new file +55 -0

55 lines not on this page · raw message

include/dm/of_dev.h new file +68 -0

68 lines not on this page · raw message

Signature · 1 line
2.11.0.483.g087da7b7c-goog
#6 lore raw

PATCH 11/22 dm: core: Allow binding a device from a live tree

In reply to Simon Glass

When a live tree is being used we need to record the node that was used to create the device. Add a function to support this, and a new field to the udevice structure.

---

 drivers/core/device.c        | 27 +++++++++++++++++++++++----
 include/dm/device-internal.h |  6 ++++++
 include/dm/device.h          | 19 ++++++++++++++++++-
 3 files changed, 47 insertions(+), 5 deletions(-)
drivers/core/device.c +23 -4
index 70fcfc23e0c..52b8715a4ae 100644
@@ -30,6 +30,7 @@ DECLARE_GLOBAL_DATA_PTR;
 static int device_bind_common(struct udevice *parent, const struct driver *drv,
 			      const char *name, void *platdata,
 			      ulong driver_data, int of_offset,
+			      struct device_node *of_node,
 			      uint of_platdata_size, struct udevice **devp)
 {
 	struct udevice *dev;
@@ -60,7 +61,14 @@ static int device_bind_common(struct udevice *parent, const struct driver *drv,
 	dev->platdata = platdata;
 	dev->driver_data = driver_data;
 	dev->name = name;
+#ifdef CONFIG_OF_LIVE
+	if (of_use_livetree())
+		dev->node_ref = of_node_to_ref(of_node);
+	else
+		dev->node_ref = of_offset_to_ref(of_offset);
+#else
 	dev->of_offset = of_offset;
+#endif
 	dev->parent = parent;
 	dev->driver = drv;
 	dev->uclass = uc;
@@ -219,15 +227,25 @@ int device_bind_with_driver_data(struct udevice *parent,
 				 struct udevice **devp)
 {
 	return device_bind_common(parent, drv, name, NULL, driver_data,
-				  of_offset, 0, devp);
+				  of_offset, NULL, 0, devp);
+}
+
+int device_bind_node_with_driver_data(struct udevice *parent,
+				      const struct driver *drv,
+				      const char *name, ulong driver_data,
+				      struct device_node *np,
+				      struct udevice **devp)
+{
+	return device_bind_common(parent, drv, name, NULL, driver_data,
+				  -1, np, 0, devp);
 }
 
 int device_bind(struct udevice *parent, const struct driver *drv,
 		const char *name, void *platdata, int of_offset,
 		struct udevice **devp)
 {
-	return device_bind_common(parent, drv, name, platdata, 0, of_offset, 0,
-				  devp);
+	return device_bind_common(parent, drv, name, platdata, 0, of_offset,
+				  NULL, 0, devp);
 }
 
 int device_bind_by_name(struct udevice *parent, bool pre_reloc_only,
@@ -246,7 +264,8 @@ int device_bind_by_name(struct udevice *parent, bool pre_reloc_only,
 	platdata_size = info->platdata_size;
 #endif
 	return device_bind_common(parent, drv, info->name,
-			(void *)info->platdata, 0, -1, platdata_size, devp);
+				  (void *)info->platdata, 0, -1, NULL,
+				  platdata_size, devp);
 }
 
 static void *alloc_priv(int size, uint flags)
include/dm/device-internal.h +6 -0
index 0bf8707493a..5bc80e64aae 100644
@@ -11,6 +11,7 @@
 #ifndef _DM_DEVICE_INTERNAL_H
 #define _DM_DEVICE_INTERNAL_H
 
+struct device_node;
 struct udevice;
 
 /**
@@ -62,6 +63,11 @@ int device_bind_with_driver_data(struct udevice *parent,
 				 ulong driver_data, int of_offset,
 				 struct udevice **devp);
 
+int device_bind_node_with_driver_data(struct udevice *parent,
+				      const struct driver *drv,
+				      const char *name, ulong driver_data,
+				      struct device_node *np,
+				      struct udevice **devp);
 /**
  * device_bind_by_name: Create a device and bind it to a driver
  *
include/dm/device.h +18 -1
index 4e95fb7773d..94248eccee7 100644
@@ -18,6 +18,10 @@
 #include <linux/kernel.h>
 #include <linux/list.h>
 
+#ifdef CONFIG_OF_LIVE
+#include <dm/of_ref.h>
+#endif
+
 struct driver_info;
 
 /* Driver is active (probed). Cleared when it is removed */
@@ -68,7 +72,8 @@ struct driver_info;
  * @platdata: Configuration data for this device
  * @parent_platdata: The parent bus's configuration data for this device
  * @uclass_platdata: The uclass's configuration data for this device
- * @of_offset: Device tree node offset for this device (- for none)
+ * @of_noderef: Reference to device tree node for this devide)
+ * @of_offset: Device tree node offset for this device (-1 for none)
  * @driver_data: Driver data word for the entry that matched this device with
  *		its driver
  * @parent: Parent of this device, or NULL for the top level device
@@ -94,7 +99,11 @@ struct udevice {
 	void *platdata;
 	void *parent_platdata;
 	void *uclass_platdata;
+#ifdef CONFIG_OF_LIVE
+	of_node_ref node_ref;
+#else
 	int of_offset;
+#endif
 	ulong driver_data;
 	struct udevice *parent;
 	void *priv;
@@ -123,12 +132,20 @@ struct udevice {
 
 static inline int dev_of_offset(const struct udevice *dev)
 {
+#ifdef CONFIG_OF_LIVE
+	return of_ref_to_offset(dev->node_ref);
+#else
 	return dev->of_offset;
+#endif
 }
 
 static inline void dev_set_of_offset(struct udevice *dev, int of_offset)
 {
+#ifdef CONFIG_OF_LIVE
+	dev->node_ref = of_offset_to_ref(of_offset);
+#else
 	dev->of_offset = of_offset;
+#endif
 }
 
 /**
Signature · 1 line
2.11.0.483.g087da7b7c-goog
#7 lore raw

PATCH 12/22 dm: core: Add a method to find a driver for a livetree node

In reply to Simon Glass

We already have a means to locate drivers for flat device tree nodes. Add a separate method for the live tree.

TODO: Refactor this to avoid code duplication.

---

 drivers/core/lists.c | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 include/dm/lists.h   |  5 ++++
 2 files changed, 81 insertions(+)
drivers/core/lists.c +76 -0
index 72c55e205f9..b9bfbf5cc86 100644
@@ -12,6 +12,7 @@
 #include <dm/device.h>
 #include <dm/device-internal.h>
 #include <dm/lists.h>
+#include <dm/of_access.h>
 #include <dm/platdata.h>
 #include <dm/uclass.h>
 #include <dm/util.h>
@@ -199,4 +200,79 @@ int lists_bind_fdt(struct udevice *parent, const void *blob, int offset,
 
 	return result;
 }
+
+#ifdef CONFIG_OF_LIVE
+/*
+ * TODO(sjg at chromium.org): Perhaps we can use np_to_offset(np)() to merge this
+ * function with lists_bind_fdt()
+ */
+int lists_bind_node(struct udevice *parent, struct device_node *np,
+		    struct udevice **devp)
+{
+	struct driver *driver = ll_entry_start(struct driver, driver);
+	const int n_ents = ll_entry_count(struct driver, driver);
+	const struct udevice_id *id;
+	struct driver *entry;
+	struct udevice *dev;
+	bool found = false;
+	const struct property *compat_prop;
+	const char *compat;
+	int compat_length, i;
+	int result = 0;
+	int ret = 0;
+
+	if (devp)
+		*devp = NULL;
+	dm_dbg("bind node %s\n", np->name);
+
+	compat_prop = of_find_property(np, "compatible", &compat_length);
+	if (!compat_prop) {
+		dm_dbg("Device '%s' has no compatible string\n", np->name);
+		return 0;
+	}
+
+	/*
+	 * Walk through the compatible string list, attempting to match each
+	 * compatible string in order such that we match in order of priority
+	 * from the first string to the last.
+	 */
+	for (i = 0; i < compat_prop->length; i += strlen(compat) + 1) {
+		compat = compat_prop->value + i;
+		dm_dbg("   - attempt to match compatible string '%s'\n",
+		       compat);
+
+		for (entry = driver; entry != driver + n_ents; entry++) {
+			ret = driver_check_compatible(entry->of_match, &id,
+						      compat);
+			if (!ret)
+				break;
+		}
+		if (entry == driver + n_ents)
+			continue;
+
+		dm_dbg("   - found match at '%s'\n", entry->name);
+		ret = device_bind_node_with_driver_data(parent, entry, np->name,
+							id->data, np, &dev);
+		if (ret == -ENODEV) {
+			dm_dbg("Driver '%s' refuses to bind\n", entry->name);
+			continue;
+		}
+		if (ret) {
+			dm_warn("Error binding driver '%s': %d\n", entry->name,
+				ret);
+			return ret;
+		} else {
+			found = true;
+			if (devp)
+				*devp = dev;
+		}
+		break;
+	}
+
+	if (!found && !result && ret != -ENODEV)
+		dm_dbg("No match for node '%s'\n", np->name);
+
+	return result;
+}
+#endif /* CONFIG_OF_LIVE */
 #endif
include/dm/lists.h +5 -0
index 4513d6a311a..2ca7079bc86 100644
@@ -12,6 +12,8 @@
 
 #include <dm/uclass-id.h>
 
+struct device_node;
+
 /**
  * lists_driver_lookup_name() - Return u_boot_driver corresponding to name
  *
@@ -60,6 +62,9 @@ int lists_bind_drivers(struct udevice *parent, bool pre_reloc_only);
 int lists_bind_fdt(struct udevice *parent, const void *blob, int offset,
 		   struct udevice **devp);
 
+int lists_bind_node(struct udevice *parent, struct device_node *np,
+		    struct udevice **devp);
+
 /**
  * device_bind_driver() - bind a device to a driver
  *
Signature · 1 line
2.11.0.483.g087da7b7c-goog
#8 lore raw

PATCH 13/22 dm: core: Scan the live tree when setting up driver model

In reply to Simon Glass

When starting up driver model with a live tree we need to scan the tree for devices. Add code to handle this.

---

 drivers/core/root.c | 44 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 44 insertions(+)
drivers/core/root.c +44 -0
index cd09c55d9d2..b15376a9859 100644
@@ -12,9 +12,13 @@
 #include <fdtdec.h>
 #include <malloc.h>
 #include <libfdt.h>
+#include <livetree.h>
 #include <dm/device.h>
 #include <dm/device-internal.h>
 #include <dm/lists.h>
+#include <dm/of.h>
+#include <dm/of_access.h>
+#include <dm/of_ref.h>
 #include <dm/platdata.h>
 #include <dm/root.h>
 #include <dm/uclass.h>
@@ -165,7 +169,11 @@ int dm_init(void)
 	if (ret)
 		return ret;
 #if CONFIG_IS_ENABLED(OF_CONTROL)
+# if defined(CONFIG_OF_LIVE)
+	DM_ROOT_NON_CONST->node_ref = of_node_to_ref(gd->of_root);
+# else
 	DM_ROOT_NON_CONST->of_offset = 0;
+# endif
 #endif
 	ret = device_probe(DM_ROOT_NON_CONST);
 	if (ret)
@@ -195,6 +203,36 @@ int dm_scan_platdata(bool pre_reloc_only)
 	return ret;
 }
 
+#ifdef CONFIG_OF_LIVE
+static int dm_scan_fdt_live(struct udevice *parent,
+			    struct device_node *node_parent,
+			    bool pre_reloc_only)
+{
+	struct device_node *np;
+	int ret = 0, err;
+
+	for (np = node_parent->child; np; np = np->sibling) {
+		if (pre_reloc_only &&
+		    !of_find_property(np, "u-boot,dm-pre-reloc", NULL))
+			continue;
+		if (!of_device_is_available(np)) {
+			dm_dbg("   - ignoring disabled device\n");
+			continue;
+		}
+		err = lists_bind_node(parent, np, NULL);
+		if (err && !ret) {
+			ret = err;
+			debug("%s: ret=%d\n", np->name, ret);
+		}
+	}
+
+	if (ret)
+		dm_warn("Some drivers failed to bind\n");
+
+	return ret;
+}
+#endif /* CONFIG_OF_LIVE */
+
 #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
 /**
  * dm_scan_fdt_node() - Scan the device tree and bind drivers for a node
@@ -249,6 +287,12 @@ int dm_scan_fdt_dev(struct udevice *dev)
 
 int dm_scan_fdt(const void *blob, bool pre_reloc_only)
 {
+#ifdef CONFIG_OF_LIVE
+	if (of_use_livetree())
+		return dm_scan_fdt_live(gd->dm_root, gd->of_root,
+					pre_reloc_only);
+	else
+#endif
 	return dm_scan_fdt_node(gd->dm_root, blob, 0, pre_reloc_only);
 }
 #endif
Signature · 1 line
2.11.0.483.g087da7b7c-goog
#9 lore raw

PATCH 14/22 dm: core: Add a way to find a device by its live-tree node

In reply to Simon Glass

Add function which looks up a device by its device tree node in the live tree.

---

 drivers/core/uclass.c        | 42 ++++++++++++++++++++++++++++++++++++++++++
 include/dm/uclass-internal.h | 17 +++++++++++++++++
 include/dm/uclass.h          | 19 +++++++++++++++++++
 3 files changed, 78 insertions(+)
drivers/core/uclass.c +42 -0
index 7de370644d7..f7430f00789 100644
@@ -13,6 +13,7 @@
 #include <dm/device.h>
 #include <dm/device-internal.h>
 #include <dm/lists.h>
+#include <dm/of_ref.h>
 #include <dm/uclass.h>
 #include <dm/uclass-internal.h>
 #include <dm/util.h>
@@ -287,6 +288,33 @@ int uclass_find_device_by_of_offset(enum uclass_id id, int node,
 	return -ENODEV;
 }
 
+#ifdef CONFIG_OF_LIVE
+int uclass_find_device_by_of_node(enum uclass_id id,
+				  const struct device_node *np,
+				  struct udevice **devp)
+{
+	struct uclass *uc;
+	struct udevice *dev;
+	int ret;
+
+	*devp = NULL;
+	if (!np)
+		return -ENODEV;
+	ret = uclass_get(id, &uc);
+	if (ret)
+		return ret;
+
+	list_for_each_entry(dev, &uc->dev_head, uclass_node) {
+		if (of_ref_equal(dev->node_ref, of_node_to_ref(np))) {
+			*devp = dev;
+			return 0;
+		}
+	}
+
+	return -ENODEV;
+}
+#endif
+
 #if CONFIG_IS_ENABLED(OF_CONTROL)
 static int uclass_find_device_by_phandle(enum uclass_id id,
 					 struct udevice *parent,
@@ -407,6 +435,20 @@ int uclass_get_device_by_of_offset(enum uclass_id id, int node,
 	return uclass_get_device_tail(dev, ret, devp);
 }
 
+#ifdef CONFIG_OF_LIVE
+int uclass_get_device_by_of_node(enum uclass_id id,
+				 const struct device_node *np,
+				 struct udevice **devp)
+{
+	struct udevice *dev;
+	int ret;
+
+	*devp = NULL;
+	ret = uclass_find_device_by_of_node(id, np, &dev);
+	return uclass_get_device_tail(dev, ret, devp);
+}
+#endif
+
 #if CONFIG_IS_ENABLED(OF_CONTROL)
 int uclass_get_device_by_phandle(enum uclass_id id, struct udevice *parent,
 				 const char *name, struct udevice **devp)
include/dm/uclass-internal.h +17 -0
index ad284b8445a..103656fed0d 100644
@@ -115,6 +115,23 @@ int uclass_find_device_by_of_offset(enum uclass_id id, int node,
 				    struct udevice **devp);
 
 /**
+ * uclass_find_device_by_of_node() - Find a uclass device by device tree node
+ *
+ * This searches the devices in the uclass for one attached to the given
+ * device tree node.
+ *
+ * The device is NOT probed, it is merely returned.
+ *
+ * @id: ID to look up
+ * @node: Device tree offset to search for (if NULL then -ENODEV is returned)
+ * @devp: Returns pointer to device (there is only one for each node)
+ * @return 0 if OK, -ve on error
+ */
+int uclass_find_device_by_of_node(enum uclass_id id,
+				  const struct device_node *np,
+				  struct udevice **devp);
+
+/**
  * uclass_bind_device() - Associate device with a uclass
  *
  * Connect the device into uclass's list of devices.
include/dm/uclass.h +19 -0
index b583aa869b3..2d4958d37b6 100644
@@ -14,6 +14,8 @@
 #include <linker_lists.h>
 #include <linux/list.h>
 
+struct device_node;
+
 /**
  * struct uclass - a U-Boot drive class, collecting together similar drivers
  *
@@ -186,6 +188,23 @@ int uclass_get_device_by_of_offset(enum uclass_id id, int node,
 				   struct udevice **devp);
 
 /**
+ * uclass_get_device_by_of_node() - Get a uclass device by device tree node
+ *
+ * This searches the devices in the uclass for one attached to the given
+ * device tree node.
+ *
+ * The device is probed to activate it ready for use.
+ *
+ * @id: ID to look up
+ * @np: Device tree node to search for (if NULL then -ENODEV is returned)
+ * @devp: Returns pointer to device (there is only one for each node)
+ * @return 0 if OK, -ve on error
+ */
+int uclass_get_device_by_of_node(enum uclass_id id,
+				 const struct device_node *np,
+				 struct udevice **devp);
+
+/**
  * uclass_get_device_by_phandle() - Get a uclass device by phandle
  *
  * This searches the devices in the uclass for one with the given phandle.
Signature · 1 line
2.11.0.483.g087da7b7c-goog
#10 lore raw

PATCH 15/22 dm: core: Add a way to find a device by node reference

In reply to Simon Glass

Add a function which looks up a device by its node (either in live tree or flat tree).

---

 drivers/core/uclass.c        | 36 ++++++++++++++++++++++++++++++++++++
 include/dm/uclass-internal.h |  5 +++++
 include/dm/uclass.h          |  4 ++++
 3 files changed, 45 insertions(+)
drivers/core/uclass.c +36 -0
index f7430f00789..c327aaf594d 100644
@@ -13,6 +13,7 @@
 #include <dm/device.h>
 #include <dm/device-internal.h>
 #include <dm/lists.h>
+#include <dm/of_dev.h>
 #include <dm/of_ref.h>
 #include <dm/uclass.h>
 #include <dm/uclass-internal.h>
@@ -315,6 +316,30 @@ int uclass_find_device_by_of_node(enum uclass_id id,
 }
 #endif
 
+int uclass_find_device_by_of_ref(enum uclass_id id, of_node_ref node_ref,
+				 struct udevice **devp)
+{
+	struct uclass *uc;
+	struct udevice *dev;
+	int ret;
+
+	*devp = NULL;
+	if (!of_ref_valid(node_ref))
+		return -ENODEV;
+	ret = uclass_get(id, &uc);
+	if (ret)
+		return ret;
+
+	list_for_each_entry(dev, &uc->dev_head, uclass_node) {
+		if (of_ref_equal(dev_of_node_ref(dev), node_ref)) {
+			*devp = dev;
+			return 0;
+		}
+	}
+
+	return -ENODEV;
+}
+
 #if CONFIG_IS_ENABLED(OF_CONTROL)
 static int uclass_find_device_by_phandle(enum uclass_id id,
 					 struct udevice *parent,
@@ -449,6 +474,17 @@ int uclass_get_device_by_of_node(enum uclass_id id,
 }
 #endif
 
+int uclass_get_device_by_of_ref(enum uclass_id id, of_node_ref node_ref,
+				struct udevice **devp)
+{
+	struct udevice *dev;
+	int ret;
+
+	*devp = NULL;
+	ret = uclass_find_device_by_of_ref(id, node_ref, &dev);
+	return uclass_get_device_tail(dev, ret, devp);
+}
+
 #if CONFIG_IS_ENABLED(OF_CONTROL)
 int uclass_get_device_by_phandle(enum uclass_id id, struct udevice *parent,
 				 const char *name, struct udevice **devp)
include/dm/uclass-internal.h +5 -0
index 103656fed0d..bb7819b8590 100644
@@ -10,6 +10,8 @@
 #ifndef _DM_UCLASS_INTERNAL_H
 #define _DM_UCLASS_INTERNAL_H
 
+#include <dm/of_ref.h>
+
 /**
  * uclass_get_device_tail() - handle the end of a get_device call
  *
@@ -131,6 +133,9 @@ int uclass_find_device_by_of_node(enum uclass_id id,
 				  const struct device_node *np,
 				  struct udevice **devp);
 
+int uclass_find_device_by_of_ref(enum uclass_id id, of_node_ref node_ref,
+				 struct udevice **devp);
+
 /**
  * uclass_bind_device() - Associate device with a uclass
  *
include/dm/uclass.h +4 -0
index 2d4958d37b6..ed438f699c2 100644
@@ -10,6 +10,7 @@
 #ifndef _DM_UCLASS_H
 #define _DM_UCLASS_H
 
+#include <dm/of_ref.h>
 #include <dm/uclass-id.h>
 #include <linker_lists.h>
 #include <linux/list.h>
@@ -204,6 +205,9 @@ int uclass_get_device_by_of_node(enum uclass_id id,
 				 const struct device_node *np,
 				 struct udevice **devp);
 
+int uclass_get_device_by_of_ref(enum uclass_id id, of_node_ref node_ref,
+				struct udevice **devp);
+
 /**
  * uclass_get_device_by_phandle() - Get a uclass device by phandle
  *
Signature · 1 line
2.11.0.483.g087da7b7c-goog
#11 lore raw

PATCH 16/22 dm: gpio: Refactor to prepare for live tree support

In reply to Simon Glass

Move the main part of the GPIO request function into a separate function so that it can be used by the live tree function when added. Update the xlate method to use a node reference.

TODO(sjg at chromium.org): Update the other GPIO drivers

---

 drivers/gpio/gpio-uclass.c | 56 ++++++++++++++++++++++++++++------------------
 drivers/gpio/sandbox.c     |  4 +++-
 include/asm-generic/gpio.h |  6 +++--
 3 files changed, 41 insertions(+), 25 deletions(-)
drivers/gpio/gpio-uclass.c +34 -22
index 9ab9df4ce7a..721e95cd929 100644
@@ -114,9 +114,8 @@ int gpio_lookup_name(const char *name, struct udevice **devp,
 	return 0;
 }
 
-int gpio_xlate_offs_flags(struct udevice *dev,
-					 struct gpio_desc *desc,
-					 struct fdtdec_phandle_args *args)
+int gpio_xlate_offs_flags(struct udevice *dev, struct gpio_desc *desc,
+			  struct of_ref_phandle_args *args)
 {
 	if (args->args_count < 1)
 		return -EINVAL;
@@ -133,7 +132,7 @@ int gpio_xlate_offs_flags(struct udevice *dev,
 }
 
 static int gpio_find_and_xlate(struct gpio_desc *desc,
-			       struct fdtdec_phandle_args *args)
+			       struct of_ref_phandle_args *args)
 {
 	struct dm_gpio_ops *ops = gpio_get_ops(desc->dev);
 
@@ -642,37 +641,30 @@ int dm_gpio_get_values_as_int(const struct gpio_desc *desc_list, int count)
 	return vector;
 }
 
-static int _gpio_request_by_name_nodev(const void *blob, int node,
-				       const char *list_name, int index,
-				       struct gpio_desc *desc, int flags,
-				       bool add_index)
+static int gpio_request_tail(int ret, of_node_ref node_ref,
+			     struct of_ref_phandle_args *args,
+			     const char *list_name, int index,
+			     struct gpio_desc *desc, int flags, bool add_index)
 {
-	struct fdtdec_phandle_args args;
-	int ret;
-
+	if (ret)
+		goto err;
 	desc->dev = NULL;
 	desc->offset = 0;
 	desc->flags = 0;
-	ret = fdtdec_parse_phandle_with_args(blob, node, list_name,
-					     "#gpio-cells", 0, index, &args);
-	if (ret) {
-		debug("%s: fdtdec_parse_phandle_with_args failed\n", __func__);
-		goto err;
-	}
 
-	ret = uclass_get_device_by_of_offset(UCLASS_GPIO, args.node,
-					     &desc->dev);
+	ret = uclass_get_device_by_of_ref(UCLASS_GPIO, args->node_ref,
+					  &desc->dev);
 	if (ret) {
 		debug("%s: uclass_get_device_by_of_offset failed\n", __func__);
 		goto err;
 	}
-	ret = gpio_find_and_xlate(desc, &args);
+	ret = gpio_find_and_xlate(desc, args);
 	if (ret) {
 		debug("%s: gpio_find_and_xlate failed\n", __func__);
 		goto err;
 	}
 	ret = dm_gpio_requestf(desc, add_index ? "%s.%s%d" : "%s.%s",
-			       fdt_get_name(blob, node, NULL),
+			       of_ref_get_name(node_ref),
 			       list_name, index);
 	if (ret) {
 		debug("%s: dm_gpio_requestf failed\n", __func__);
@@ -687,10 +679,30 @@ static int _gpio_request_by_name_nodev(const void *blob, int node,
 	return 0;
 err:
 	debug("%s: Node '%s', property '%s', failed to request GPIO index %d: %d\n",
-	      __func__, fdt_get_name(blob, node, NULL), list_name, index, ret);
+	      __func__, of_ref_get_name(node_ref), list_name, index, ret);
 	return ret;
 }
 
+static int _gpio_request_by_name_nodev(const void *blob, int node,
+				       const char *list_name, int index,
+				       struct gpio_desc *desc, int flags,
+				       bool add_index)
+{
+	struct fdtdec_phandle_args fdtdec_args;
+	struct of_ref_phandle_args args;
+	int ret;
+
+	ret = fdtdec_parse_phandle_with_args(blob, node, list_name,
+					"#gpio-cells", 0, index, &fdtdec_args);
+	if (ret)
+		debug("%s: fdtdec_parse_phandle_with_args failed\n", __func__);
+	else
+		of_ref_from_fdtdec_phandle_args(&fdtdec_args, &args);
+
+	return gpio_request_tail(ret, of_offset_to_ref(node), &args, list_name,
+				 index, desc, flags, add_index);
+}
+
 int gpio_request_by_name_nodev(const void *blob, int node,
 			       const char *list_name, int index,
 			       struct gpio_desc *desc, int flags)
drivers/gpio/sandbox.c +3 -1
index ae6d93013f7..5bf6bb127d1 100644
@@ -8,6 +8,8 @@
 #include <fdtdec.h>
 #include <malloc.h>
 #include <asm/gpio.h>
+#include <dm/of.h>
+#include <dm/of_dev.h>
 #include <dt-bindings/gpio/gpio.h>
 
 DECLARE_GLOBAL_DATA_PTR;
@@ -165,7 +167,7 @@ static int sb_gpio_get_function(struct udevice *dev, unsigned offset)
 }
 
 static int sb_gpio_xlate(struct udevice *dev, struct gpio_desc *desc,
-			 struct fdtdec_phandle_args *args)
+			 struct of_ref_phandle_args *args)
 {
 	desc->offset = args->args[0];
 	if (args->args_count < 2)
include/asm-generic/gpio.h +4 -2
index 4aa0004fab4..f3341e51359 100644
@@ -7,6 +7,8 @@
 #ifndef _ASM_GENERIC_GPIO_H_
 #define _ASM_GENERIC_GPIO_H_
 
+struct of_ref_phandle_args;
+
 /*
  * Generic GPIO API for U-Boot
  *
@@ -214,7 +216,7 @@ struct fdtdec_phandle_args;
  *
  */
 int gpio_xlate_offs_flags(struct udevice *dev, struct gpio_desc *desc,
-			  struct fdtdec_phandle_args *args);
+			  struct of_ref_phandle_args *args);
 
 /**
  * struct struct dm_gpio_ops - Driver model GPIO operations
@@ -286,7 +288,7 @@ struct dm_gpio_ops {
 	 * @return 0 if OK, -ve on error
 	 */
 	int (*xlate)(struct udevice *dev, struct gpio_desc *desc,
-		     struct fdtdec_phandle_args *args);
+		     struct of_ref_phandle_args *args);
 };
 
 /**
Signature · 1 line
2.11.0.483.g087da7b7c-goog
#12 lore raw

PATCH 17/22 dm: gpio: Add live tree support

In reply to Simon Glass

Add support for requesting GPIOs with a live device tree.

---

 drivers/gpio/gpio-uclass.c | 44 ++++++++++++++++++++++++++++++++++++--------
 1 file changed, 36 insertions(+), 8 deletions(-)
drivers/gpio/gpio-uclass.c +36 -8
index 721e95cd929..377729e3c62 100644
@@ -11,6 +11,8 @@
 #include <fdtdec.h>
 #include <malloc.h>
 #include <asm/gpio.h>
+#include <dm/of_access.h>
+#include <dm/of_dev.h>
 #include <linux/bug.h>
 #include <linux/ctype.h>
 
@@ -711,16 +713,42 @@ int gpio_request_by_name_nodev(const void *blob, int node,
 					   flags, index > 0);
 }
 
-int gpio_request_by_name(struct udevice *dev,  const char *list_name, int index,
+static int _gpio_request_by_name(const struct device_node *np,
+				 const char *list_name, int index,
+				 struct gpio_desc *desc, int flags,
+				 bool add_index)
+{
+	struct of_phandle_args of_args;
+	struct of_ref_phandle_args args;
+	int ret;
+
+	ret = of_parse_phandle_with_args(np, list_name, "#gpio-cells", index,
+					 &of_args);
+	if (ret)
+		debug("%s: of_parse_phandle_with_args failed\n", __func__);
+	else
+		of_ref_from_of_phandle_args(&of_args, &args);
+
+	return gpio_request_tail(ret, of_node_to_ref(np), &args, list_name,
+				 index, desc, flags, add_index);
+}
+
+int gpio_request_by_name(struct udevice *dev, const char *list_name, int index,
 			 struct gpio_desc *desc, int flags)
 {
-	/*
-	 * This isn't ideal since we don't use dev->name in the debug()
-	 * calls in gpio_request_by_name(), but we can do this until
-	 * gpio_request_by_name_nodev() can be dropped.
-	 */
-	return gpio_request_by_name_nodev(gd->fdt_blob, dev_of_offset(dev),
-					  list_name, index, desc, flags);
+	if (of_use_livetree()) {
+		return _gpio_request_by_name(dev_of_node(dev), list_name,
+					     index, desc, flags, index > 0);
+	} else {
+		/*
+		 * This isn't ideal since we don't use dev->name in the debug()
+		 * calls in gpio_request_by_name(), but we can do this until
+		 * gpio_request_by_name_nodev() can be dropped.
+		 */
+		return gpio_request_by_name_nodev(gd->fdt_blob,
+						  dev_of_offset(dev), list_name,
+						  index, desc, flags);
+	}
 }
 
 int gpio_request_list_by_name_nodev(const void *blob, int node,
Signature · 1 line
2.11.0.483.g087da7b7c-goog
#13 lore raw

PATCH 18/22 dm: gpio: Drop blank line in gpio_xlate_offs_flags() comment

In reply to Simon Glass

This is not needed. Drop it.

---

 include/asm-generic/gpio.h | 1 -
 1 file changed, 1 deletion(-)
include/asm-generic/gpio.h +0 -1
index f3341e51359..efa96a5f98d 100644
@@ -213,7 +213,6 @@ struct fdtdec_phandle_args;
  *
  * This routine sets the offset field to args[0] and the flags field to
  * GPIOD_ACTIVE_LOW if the GPIO_ACTIVE_LOW flag is present in args[1].
- *
  */
 int gpio_xlate_offs_flags(struct udevice *dev, struct gpio_desc *desc,
 			  struct of_ref_phandle_args *args);
Signature · 1 line
2.11.0.483.g087da7b7c-goog
#14 lore raw

PATCH 19/22 dm: gpio: sandbox: Use dm_read...() functions to access DT

In reply to Simon Glass

Use the new dm_read...() functions to access the device tree, so that a live tree can be used.

---

 drivers/gpio/sandbox.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
drivers/gpio/sandbox.c +2 -4
index 5bf6bb127d1..ef9a05bf880 100644
@@ -199,10 +199,8 @@ static int sandbox_gpio_ofdata_to_platdata(struct udevice *dev)
 {
 	struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
 
-	uc_priv->gpio_count = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
-					     "num-gpios", 0);
-	uc_priv->bank_name = fdt_getprop(gd->fdt_blob, dev_of_offset(dev),
-					 "gpio-bank-name", NULL);
+	uc_priv->gpio_count = dev_read_u32_defaut(dev, "num-gpios", 0);
+	uc_priv->bank_name = dev_read_string(dev, "gpio-bank-name");
 
 	return 0;
 }
Signature · 1 line
2.11.0.483.g087da7b7c-goog
#15 lore raw

PATCH 20/22 cros_ec: Fix debug() statement in ec_command_inptr()

In reply to Simon Glass

This prints out the wrong pointers. Fix it.

---

 drivers/misc/cros_ec.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/misc/cros_ec.c +1 -2
index 3d449b2a552..e2027ea5d20 100644
@@ -304,8 +304,7 @@ static int ec_command_inptr(struct cros_ec_dev *dev, uint8_t cmd,
 				NULL, 0, &din, din_len);
 	}
 
-	debug("%s: len=%d, dinp=%p, *dinp=%p\n", __func__, len, dinp,
-	      dinp ? *dinp : NULL);
+	debug("%s: len=%d, din=%p\n", __func__, len, din);
 	if (dinp) {
 		/* If we have any data to return, it must be 64bit-aligned */
 		assert(len <= 0 || !((uintptr_t)din & 7));
Signature · 1 line
2.11.0.483.g087da7b7c-goog
#16 lore raw

PATCH 21/22 cros_ec: Convert to support live tree

In reply to Simon Glass

Convert this driver to support the live device tree and remove the old fdtdec support.

The keyboard is not yet converted.

---

 drivers/misc/cros_ec.c         | 33 +++++++++++++++------------------
 drivers/misc/cros_ec_sandbox.c |  2 +-
 include/cros_ec.h              |  8 +++-----
 include/fdtdec.h               | 34 ----------------------------------
 lib/fdtdec.c                   | 32 --------------------------------
 5 files changed, 19 insertions(+), 90 deletions(-)
drivers/misc/cros_ec.c +15 -18
index e2027ea5d20..624a9992b91 100644
@@ -26,6 +26,9 @@
 #include <asm/io.h>
 #include <asm-generic/gpio.h>
 #include <dm/device-internal.h>
+#include <dm/of_dev.h>
+#include <dm/of_extra.h>
+#include <dm/of_ref.h>
 #include <dm/uclass-internal.h>
 
 #ifdef DEBUG_TRACE
@@ -996,15 +999,12 @@ int cros_ec_get_ldo(struct udevice *dev, uint8_t index, uint8_t *state)
 int cros_ec_register(struct udevice *dev)
 {
 	struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
-	const void *blob = gd->fdt_blob;
-	int node = dev_of_offset(dev);
 	char id[MSG_BYTES];
 
 	cdev->dev = dev;
 	gpio_request_by_name(dev, "ec-interrupt", 0, &cdev->ec_int,
 			     GPIOD_IS_IN);
-	cdev->optimise_flash_write = fdtdec_get_bool(blob, node,
-						     "optimise-flash-write");
+	cdev->optimise_flash_write = dev_read_bool(dev, "optimise-flash-write");
 
 	if (cros_ec_check_version(cdev)) {
 		debug("%s: Could not detect CROS-EC version\n", __func__);
@@ -1023,28 +1023,26 @@ int cros_ec_register(struct udevice *dev)
 	return 0;
 }
 
-int cros_ec_decode_ec_flash(const void *blob, int node,
-			    struct fdt_cros_ec *config)
+int cros_ec_decode_ec_flash(struct udevice *dev, struct fdt_cros_ec *config)
 {
-	int flash_node;
+	of_node_ref flash_node, node;
 
-	flash_node = fdt_subnode_offset(blob, node, "flash");
-	if (flash_node < 0) {
+	flash_node = dev_find_subnode(dev, "flash");
+	if (!of_ref_valid(flash_node)) {
 		debug("Failed to find flash node\n");
 		return -1;
 	}
 
-	if (fdtdec_read_fmap_entry(blob, flash_node, "flash",
-				   &config->flash)) {
+	if (of_read_fmap_entry(flash_node, "flash", &config->flash)) {
 		debug("Failed to decode flash node in chrome-ec'\n");
 		return -1;
 	}
 
-	config->flash_erase_value = fdtdec_get_int(blob, flash_node,
-						    "erase-value", -1);
-	for (node = fdt_first_subnode(blob, flash_node); node >= 0;
-	     node = fdt_next_subnode(blob, node)) {
-		const char *name = fdt_get_name(blob, node, NULL);
+	config->flash_erase_value = of_ref_read_s32_default(flash_node,
+							    "erase-value", -1);
+	for (node = of_ref_first_subnode(flash_node); of_ref_valid(node);
+	     node = of_ref_next_subnode(node)) {
+		const char *name = of_ref_get_name(node);
 		enum ec_flash_region region;
 
 		if (0 == strcmp(name, "ro")) {
@@ -1058,8 +1056,7 @@ int cros_ec_decode_ec_flash(const void *blob, int node,
 			return -1;
 		}
 
-		if (fdtdec_read_fmap_entry(blob, node, "reg",
-					   &config->region[region])) {
+		if (of_read_fmap_entry(node, "reg", &config->region[region])) {
 			debug("Failed to decode flash region in chrome-ec'\n");
 			return -1;
 		}
drivers/misc/cros_ec_sandbox.c +1 -1
index 848c67bc230..367e42d4eb3 100644
@@ -522,7 +522,7 @@ int cros_ec_probe(struct udevice *dev)
 	int err;
 
 	memcpy(ec, &s_state, sizeof(*ec));
-	err = cros_ec_decode_ec_flash(blob, dev_of_offset(dev), &ec->ec_config);
+	err = cros_ec_decode_ec_flash(dev, &ec->ec_config);
 	if (err)
 		return err;
 
include/cros_ec.h +3 -5
index 0271f2b827c..771a176eeab 100644
@@ -11,9 +11,9 @@
 
 #include <linux/compiler.h>
 #include <ec_commands.h>
-#include <fdtdec.h>
 #include <cros_ec_message.h>
 #include <asm/gpio.h>
+#include <dm/of_extra.h>
 
 /* Our configuration information */
 struct cros_ec_dev {
@@ -377,12 +377,10 @@ int cros_ec_get_error(void);
 /**
  * Returns information from the FDT about the Chrome EC flash
  *
- * @param blob		FDT blob to use
- * @param node		Node offset to read from
+ * @param dev		Device to read from
  * @param config	Structure to use to return information
  */
-int cros_ec_decode_ec_flash(const void *blob, int node,
-			    struct fdt_cros_ec *config);
+int cros_ec_decode_ec_flash(struct udevice *dev, struct fdt_cros_ec *config);
 
 /**
  * Check the current keyboard state, in case recovery mode is requested.
include/fdtdec.h +0 -34
index d074478f141..4a0a5f37f3c 100644
@@ -807,40 +807,6 @@ const u8 *fdtdec_locate_byte_array(const void *blob, int node,
 int fdtdec_decode_region(const void *blob, int node, const char *prop_name,
 			 fdt_addr_t *basep, fdt_size_t *sizep);
 
-enum fmap_compress_t {
-	FMAP_COMPRESS_NONE,
-	FMAP_COMPRESS_LZO,
-};
-
-enum fmap_hash_t {
-	FMAP_HASH_NONE,
-	FMAP_HASH_SHA1,
-	FMAP_HASH_SHA256,
-};
-
-/* A flash map entry, containing an offset and length */
-struct fmap_entry {
-	uint32_t offset;
-	uint32_t length;
-	uint32_t used;			/* Number of bytes used in region */
-	enum fmap_compress_t compress_algo;	/* Compression type */
-	enum fmap_hash_t hash_algo;		/* Hash algorithm */
-	const uint8_t *hash;			/* Hash value */
-	int hash_size;				/* Hash size */
-};
-
-/**
- * Read a flash entry from the fdt
- *
- * @param blob		FDT blob
- * @param node		Offset of node to read
- * @param name		Name of node being read
- * @param entry		Place to put offset and size of this node
- * @return 0 if ok, -ve on error
- */
-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.
  *
lib/fdtdec.c +0 -32
index 81f47ef2c7f..30ee56571ba 100644
@@ -932,38 +932,6 @@ int fdtdec_decode_region(const void *blob, int node, const char *prop_name,
 	return 0;
 }
 
-/**
- * Read a flash entry from the fdt
- *
- * @param blob		FDT blob
- * @param node		Offset of node to read
- * @param name		Name of node being read
- * @param entry		Place to put offset and size of this node
- * @return 0 if ok, -ve on error
- */
-int fdtdec_read_fmap_entry(const void *blob, int node, const char *name,
-			   struct fmap_entry *entry)
-{
-	const char *prop;
-	u32 reg[2];
-
-	if (fdtdec_get_int_array(blob, node, "reg", reg, 2)) {
-		debug("Node '%s' has bad/missing 'reg' property\n", name);
-		return -FDT_ERR_NOTFOUND;
-	}
-	entry->offset = reg[0];
-	entry->length = reg[1];
-	entry->used = fdtdec_get_int(blob, node, "used", entry->length);
-	prop = fdt_getprop(blob, node, "compress", NULL);
-	entry->compress_algo = prop && !strcmp(prop, "lzo") ?
-		FMAP_COMPRESS_LZO : FMAP_COMPRESS_NONE;
-	prop = fdt_getprop(blob, node, "hash", &entry->hash_size);
-	entry->hash_algo = prop ? FMAP_HASH_SHA256 : FMAP_HASH_NONE;
-	entry->hash = (uint8_t *)prop;
-
-	return 0;
-}
-
 u64 fdtdec_get_number(const fdt32_t *ptr, unsigned int cells)
 {
 	u64 number = 0;
Signature · 1 line
2.11.0.483.g087da7b7c-goog
#17 lore raw

PATCH 22/22 sandbox: Move to use live tree

In reply to Simon Glass

This updates sandbox to use a live device tree. This means that after relocation (from board_init_r() onwards) it no-longer uses flat device tree.

Note: There are various drivers that still need conversion so this does not currently work correctly. For example it does not pass the test suite.

---

 configs/sandbox_defconfig | 1 +
 1 file changed, 1 insertion(+)
configs/sandbox_defconfig +1 -0
index 8ac231db994..7814574eade 100644
@@ -51,6 +51,7 @@ CONFIG_CMD_TPM=y
 CONFIG_CMD_TPM_TEST=y
 CONFIG_CMD_EXT4_WRITE=y
 CONFIG_OF_CONTROL=y
+CONFIG_OF_LIVE=y
 CONFIG_OF_HOSTFILE=y
 CONFIG_NETCONSOLE=y
 CONFIG_REGMAP=y
Signature · 1 line
2.11.0.483.g087da7b7c-goog