dm: core: Allow obtaining a node offset in the same tree
[platform/kernel/u-boot.git] / include / dm / ofnode.h
index 2c4d72d..a53cffb 100644 (file)
 
 struct resource;
 
-/**
- * typedef union ofnode_union ofnode - reference to a device tree node
- *
- * This union can hold either a straightforward pointer to a struct device_node
- * in the live device tree, or an offset within the flat device tree. In the
- * latter case, the pointer value is just the integer offset within the flat DT.
- *
- * Thus we can reference nodes in both the live tree (once available) and the
- * flat tree (until then). Functions are available to translate between an
- * ofnode and either an offset or a `struct device_node *`.
- *
- * The reference can also hold a null offset, in which case the pointer value
- * here is NULL. This corresponds to a struct device_node * value of
- * NULL, or an offset of -1.
- *
- * There is no ambiguity as to whether ofnode holds an offset or a node
- * pointer: when the live tree is active it holds a node pointer, otherwise it
- * holds an offset. The value itself does not need to be unique and in theory
- * the same value could point to a valid device node or a valid offset. We
- * could arrange for a unique value to be used (e.g. by making the pointer
- * point to an offset within the flat device tree in the case of an offset) but
- * this increases code size slightly due to the subtraction. Since it offers no
- * real benefit, the approach described here seems best.
- *
- * For now these points use constant types, since we don't allow writing
- * the DT.
- *
- * @np: Pointer to device node, used for live tree
- * @of_offset: Pointer into flat device tree, used for flat tree. Note that this
- *     is not a really a pointer to a node: it is an offset value. See above.
- */
-typedef union ofnode_union {
-       const struct device_node *np;
-       long of_offset;
-} ofnode;
+#include <dm/ofnode_decl.h>
 
 struct ofnode_phandle_args {
        ofnode node;
@@ -62,45 +28,30 @@ struct ofnode_phandle_args {
 };
 
 /**
- * struct ofprop - reference to a property of a device tree node
+ * oftree_reset() - reset the state of the oftree list
  *
- * This struct hold the reference on one property of one node,
- * using struct ofnode and an offset within the flat device tree or either
- * a pointer to a struct property in the live device tree.
- *
- * Thus we can reference arguments in both the live tree and the flat tree.
- *
- * The property reference can also hold a null reference. This corresponds to
- * a struct property NULL pointer or an offset of -1.
- *
- * @node: Pointer to device node
- * @offset: Pointer into flat device tree, used for flat tree.
- * @prop: Pointer to property, used for live treee.
+ * Reset the oftree list so it can be started again. This should be called
+ * once the control FDT is in place, but before the ofnode interface is used.
  */
-
-struct ofprop {
-       ofnode node;
-       union {
-               int offset;
-               const struct property *prop;
-       };
-};
+static inline void oftree_reset(void) {}
 
 /**
- * ofnode_to_np() - convert an ofnode to a live DT node pointer
+ * ofnode_to_fdt() - convert an ofnode to a flat DT pointer
  *
- * This cannot be called if the reference contains an offset.
+ * This cannot be called if the reference contains a node pointer.
  *
- * @node: Reference containing struct device_node * (possibly invalid)
- * Return: pointer to device node (can be NULL)
+ * @node: Reference containing offset (possibly invalid)
+ * Return: DT offset (can be NULL)
  */
-static inline const struct device_node *ofnode_to_np(ofnode node)
+static inline void *ofnode_to_fdt(ofnode node)
 {
 #ifdef OF_CHECKS
-       if (!of_live_active())
+       if (of_live_active())
                return NULL;
 #endif
-       return node.np;
+
+       /* Use the control FDT by default */
+       return (void *)gd->fdt_blob;
 }
 
 /**
@@ -121,6 +72,42 @@ static inline int ofnode_to_offset(ofnode node)
 }
 
 /**
+ * ofnode_to_np() - convert an ofnode to a live DT node pointer
+ *
+ * This cannot be called if the reference contains an offset.
+ *
+ * @node: Reference containing struct device_node * (possibly invalid)
+ * Return: pointer to device node (can be NULL)
+ */
+static inline struct device_node *ofnode_to_np(ofnode node)
+{
+#ifdef OF_CHECKS
+       if (!of_live_active())
+               return NULL;
+#endif
+       return node.np;
+}
+
+/**
+ * noffset_to_ofnode() - convert a DT offset to an ofnode
+ *
+ * @other_node: Node in the same tree to use as a reference
+ * @of_offset: DT offset (either valid, or -1)
+ * Return: reference to the associated DT offset
+ */
+static inline ofnode noffset_to_ofnode(ofnode other_node, int of_offset)
+{
+       ofnode node;
+
+       if (of_live_active())
+               node.np = NULL;
+       else
+               node.of_offset = of_offset;
+
+       return node;
+}
+
+/**
  * ofnode_valid() - check if an ofnode is valid
  *
  * @node: Reference containing offset (possibly invalid)
@@ -135,6 +122,22 @@ static inline bool ofnode_valid(ofnode node)
 }
 
 /**
+ * oftree_lookup_fdt() - obtain the FDT pointer from an oftree
+ *
+ * This can only be called when flat tree is enabled
+ *
+ * @tree: Tree to look at
+ * @return FDT pointer from the tree
+ */
+static inline void *oftree_lookup_fdt(oftree tree)
+{
+       if (of_live_active())
+               return NULL;
+       else
+               return tree.fdt;
+}
+
+/**
  * offset_to_ofnode() - convert a DT offset to an ofnode
  *
  * @of_offset: DT offset (either valid, or -1)
@@ -158,7 +161,7 @@ static inline ofnode offset_to_ofnode(int of_offset)
  * @np: Live node pointer (can be NULL)
  * Return: reference to the associated node pointer
  */
-static inline ofnode np_to_ofnode(const struct device_node *np)
+static inline ofnode np_to_ofnode(struct device_node *np)
 {
        ofnode node;
 
@@ -206,6 +209,38 @@ static inline bool ofnode_equal(ofnode ref1, ofnode ref2)
 }
 
 /**
+ * oftree_valid() - check if an oftree is valid
+ *
+ * @tree: Reference containing oftree
+ * Return: true if the reference contains a valid oftree, false if node
+ */
+static inline bool oftree_valid(oftree tree)
+{
+       if (of_live_active())
+               return tree.np;
+       else
+               return tree.fdt;
+}
+
+/**
+ * oftree_null() - Obtain a null oftree
+ *
+ * This returns an oftree which points to no tree. It works both with the flat
+ * tree and livetree.
+ */
+static inline oftree oftree_null(void)
+{
+       oftree tree;
+
+       if (of_live_active())
+               tree.np = NULL;
+       else
+               tree.fdt = NULL;
+
+       return tree;
+}
+
+/**
  * ofnode_null() - Obtain a null ofnode
  *
  * This returns an ofnode which points to no node. It works both with the flat
@@ -236,6 +271,68 @@ static inline ofnode ofnode_root(void)
 }
 
 /**
+ * ofprop_valid() - check if an ofprop is valid
+ *
+ * @prop: Pointer to ofprop to check
+ * Return: true if the reference contains a valid ofprop, false if not
+ */
+static inline bool ofprop_valid(struct ofprop *prop)
+{
+       if (of_live_active())
+               return prop->prop;
+       else
+               return prop->offset >= 0;
+}
+
+/**
+ * oftree_default() - Returns the default device tree (U-Boot's control FDT)
+ *
+ * Returns: reference to the control FDT
+ */
+static inline oftree oftree_default(void)
+{
+       oftree tree;
+
+       if (of_live_active())
+               tree.np = gd_of_root();
+       else
+               tree.fdt = (void *)gd->fdt_blob;
+
+       return tree;
+}
+
+/**
+ * oftree_from_np() - Returns an oftree from a node pointer
+ *
+ * @root: Root node of the tree
+ * Returns: reference to the given node
+ */
+static inline oftree oftree_from_np(struct device_node *root)
+{
+       oftree tree;
+
+       tree.np = root;
+
+       return tree;
+}
+
+/**
+ * oftree_from_fdt() - Returns an oftree from a flat device tree pointer
+ *
+ * @fdt: Device tree to use
+ *
+ * Returns: reference to the given node
+ */
+static inline oftree oftree_from_fdt(void *fdt)
+{
+       oftree tree;
+
+       tree.fdt = fdt;
+
+       return tree;
+}
+
+/**
  * ofnode_name_eq() - Check if the node name is equivalent to a given name
  *                    ignoring the unit address
  *
@@ -246,6 +343,46 @@ static inline ofnode ofnode_root(void)
 bool ofnode_name_eq(ofnode node, const char *name);
 
 /**
+ * ofnode_read_u8() - Read a 8-bit integer from a property
+ *
+ * @node:      valid node reference to read property from
+ * @propname:  name of the property to read from
+ * @outp:      place to put value (if found)
+ * Return: 0 if OK, -ve on error
+ */
+int ofnode_read_u8(ofnode node, const char *propname, u8 *outp);
+
+/**
+ * ofnode_read_u8_default() - Read a 8-bit integer from a property
+ *
+ * @node:      valid node reference to read property from
+ * @propname:  name of the property to read from
+ * @def:       default value to return if the property has no value
+ * Return: property value, or @def if not found
+ */
+u8 ofnode_read_u8_default(ofnode node, const char *propname, u8 def);
+
+/**
+ * ofnode_read_u16() - Read a 16-bit integer from a property
+ *
+ * @node:      valid node reference to read property from
+ * @propname:  name of the property to read from
+ * @outp:      place to put value (if found)
+ * Return: 0 if OK, -ve on error
+ */
+int ofnode_read_u16(ofnode node, const char *propname, u16 *outp);
+
+/**
+ * ofnode_read_u16_default() - Read a 16-bit integer from a property
+ *
+ * @node:      valid node reference to read property from
+ * @propname:  name of the property to read from
+ * @def:       default value to return if the property has no value
+ * Return: property value, or @def if not found
+ */
+u16 ofnode_read_u16_default(ofnode node, const char *propname, u16 def);
+
+/**
  * ofnode_read_u32() - Read a 32-bit integer from a property
  *
  * @node:      valid node reference to read property from
@@ -361,12 +498,12 @@ const char *ofnode_read_string(ofnode node, const char *propname);
  * @propname:  name of the property to read
  * @out_values:        pointer to return value, modified only if return value is 0
  * @sz:                number of array elements to read
- * Return: 0 if OK, -ve on error
+ * Return: 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
  *
  * Search for a property in a device node and read 32-bit value(s) 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.
+ * it.
  *
  * The out_values is modified only if a valid u32 value can be decoded.
  */
@@ -468,7 +605,7 @@ ofnode ofnode_get_parent(ofnode node);
  * ofnode_get_name() - get the name of a node
  *
  * @node: valid node to look up
- * Return: name of node
+ * Return: name of node (for the root node this is "")
  */
 const char *ofnode_get_name(ofnode node);
 
@@ -485,12 +622,23 @@ int ofnode_get_path(ofnode node, char *buf, int buflen);
 /**
  * ofnode_get_by_phandle() - get ofnode from phandle
  *
+ * This uses the default (control) device tree
+ *
  * @phandle:   phandle to look up
  * Return: ofnode reference to the phandle
  */
 ofnode ofnode_get_by_phandle(uint phandle);
 
 /**
+ * oftree_get_by_phandle() - get ofnode from phandle
+ *
+ * @tree:      tree to use
+ * @phandle:   phandle to look up
+ * Return: ofnode reference to the phandle
+ */
+ofnode oftree_get_by_phandle(oftree tree, uint phandle);
+
+/**
  * ofnode_read_size() - read the size of a property
  *
  * @node: node to check
@@ -699,12 +847,23 @@ int ofnode_count_phandle_with_args(ofnode node, const char *list_name,
 /**
  * ofnode_path() - find a node by full path
  *
+ * This uses the control FDT.
+ *
  * @path: Full path to node, e.g. "/bus/spi@1"
  * Return: reference to the node found. Use ofnode_valid() to check if it exists
  */
 ofnode ofnode_path(const char *path);
 
 /**
+ * ofnode_path_root() - find a node by full path from a root node
+ *
+ * @tree: Device tree to use
+ * @path: Full path to node, e.g. "/bus/spi@1"
+ * Return: reference to the node found. Use ofnode_valid() to check if it exists
+ */
+ofnode ofnode_path_root(oftree tree, const char *path);
+
+/**
  * ofnode_read_chosen_prop() - get the value of a chosen property
  *
  * This looks for a property within the /chosen node and returns its value
@@ -788,48 +947,64 @@ int ofnode_decode_display_timing(ofnode node, int index,
 const void *ofnode_get_property(ofnode node, const char *propname, int *lenp);
 
 /**
- * ofnode_get_first_property()- get the reference of the first property
+ * ofnode_first_property()- get the reference of the first property
  *
  * Get reference to the first property of the node, it is used to iterate
- * and read all the property with ofnode_get_property_by_prop().
+ * and read all the property with ofprop_get_property().
  *
  * @node: node to read
  * @prop: place to put argument reference
  * Return: 0 if OK, -ve on error. -FDT_ERR_NOTFOUND if not found
  */
-int ofnode_get_first_property(ofnode node, struct ofprop *prop);
+int ofnode_first_property(ofnode node, struct ofprop *prop);
 
 /**
- * ofnode_get_next_property() - get the reference of the next property
+ * ofnode_next_property() - get the reference of the next property
  *
  * Get reference to the next property of the node, it is used to iterate
- * and read all the property with ofnode_get_property_by_prop().
+ * and read all the property with ofprop_get_property().
  *
  * @prop: reference of current argument and place to put reference of next one
  * Return: 0 if OK, -ve on error. -FDT_ERR_NOTFOUND if not found
  */
-int ofnode_get_next_property(struct ofprop *prop);
+int ofnode_next_property(struct ofprop *prop);
 
 /**
- * ofnode_get_property_by_prop() - get a pointer to the value of a property
+ * ofnode_for_each_prop() - iterate over all properties of a node
  *
- * Get value for the property identified by the provided reference.
+ * @prop:      struct ofprop
+ * @node:      node (lvalue, ofnode)
  *
- * @prop: reference on property
- * @propname: If non-NULL, place to property name on success,
- * @lenp: If non-NULL, place to put length on success
- * Return: 0 if OK, -ve on error. -FDT_ERR_NOTFOUND if not found
+ * This is a wrapper around a for loop and is used like this::
+ *
+ *   ofnode node;
+ *   struct ofprop prop;
+ *
+ *   ofnode_for_each_prop(prop, node) {
+ *       ...use prop...
+ *   }
+ *
+ * Note that this is implemented as a macro and @prop is used as
+ * iterator in the loop. The parent variable can be a constant or even a
+ * literal.
  */
-const void *ofnode_get_property_by_prop(const struct ofprop *prop,
-                                       const char **propname, int *lenp);
+#define ofnode_for_each_prop(prop, node) \
+       for (ofnode_first_property(node, &prop); \
+            ofprop_valid(&prop); \
+            ofnode_next_property(&prop))
 
 /**
- * ofnode_is_available() - check if a node is marked available
+ * ofprop_get_property() - get a pointer to the value of a property
  *
- * @node: node to check
- * Return: true if node's 'status' property is "okay" (or is missing)
+ * Get value for the property identified by the provided reference.
+ *
+ * @prop: reference on property
+ * @propname: If non-NULL, place to property name on success,
+ * @lenp: If non-NULL, place to put length on success, or error code on failure
+ * Return: pointer to property, or NULL if not found
  */
-bool ofnode_is_available(ofnode node);
+const void *ofprop_get_property(const struct ofprop *prop,
+                               const char **propname, int *lenp);
 
 /**
  * ofnode_get_addr_size() - get address and size from a property
@@ -1019,8 +1194,9 @@ ofnode ofnode_by_compatible(ofnode from, const char *compat);
  * Find the next node after @from that has a @propname with a value
  * @propval and a length @proplen.
  *
- * @from: ofnode to start from (use ofnode_null() to start at the
- * beginning)
+ * @from: ofnode to start from. Use ofnode_null() to start at the
+ * beginning, or the return value from oftree_root() to start at the first
+ * child of the root
  * @propname: property name to check
  * @propval: property value to search for
  * @proplen: length of the value in propval
@@ -1140,17 +1316,18 @@ int ofnode_device_is_compatible(ofnode node, const char *compat);
  * ofnode_write_prop() - Set a property of a ofnode
  *
  * Note that the value passed to the function is *not* allocated by the
- * function itself, but must be allocated by the caller if necessary.
+ * function itself, but must be allocated by the caller if necessary. However
+ * it does allocate memory for the property struct and name.
  *
  * @node:      The node for whose property should be set
  * @propname:  The name of the property to set
- * @len:       The length of the new value of the property
  * @value:     The new value of the property (must be valid prior to calling
  *             the function)
+ * @len:       The length of the new value of the property
  * Return: 0 if successful, -ve on error
  */
-int ofnode_write_prop(ofnode node, const char *propname, int len,
-                     const void *value);
+int ofnode_write_prop(ofnode node, const char *propname, const void *value,
+                     int len);
 
 /**
  * ofnode_write_string() - Set a string property of a ofnode
@@ -1167,6 +1344,16 @@ int ofnode_write_prop(ofnode node, const char *propname, int len,
 int ofnode_write_string(ofnode node, const char *propname, const char *value);
 
 /**
+ * ofnode_write_u32() - Set an integer property of an ofnode
+ *
+ * @node:      The node for whose string property should be set
+ * @propname:  The name of the string property to set
+ * @value:     The new value of the 32-bit integer property
+ * Return: 0 if successful, -ve on error
+ */
+int ofnode_write_u32(ofnode node, const char *propname, u32 value);
+
+/**
  * ofnode_set_enabled() - Enable or disable a device tree node given by its
  *                       ofnode
  *
@@ -1182,6 +1369,33 @@ int ofnode_write_string(ofnode node, const char *propname, const char *value);
 int ofnode_set_enabled(ofnode node, bool value);
 
 /**
+ * ofnode_get_phy_node() - Get PHY node for a MAC (if not fixed-link)
+ *
+ * This function parses PHY handle from the Ethernet controller's ofnode
+ * (trying all possible PHY handle property names), and returns the PHY ofnode.
+ *
+ * Before this is used, ofnode_phy_is_fixed_link() should be checked first, and
+ * if the result to that is true, this function should not be called.
+ *
+ * @eth_node:  ofnode belonging to the Ethernet controller
+ * Return: ofnode of the PHY, if it exists, otherwise an invalid ofnode
+ */
+ofnode ofnode_get_phy_node(ofnode eth_node);
+
+/**
+ * ofnode_read_phy_mode() - Read PHY connection type from a MAC node
+ *
+ * This function parses the "phy-mode" / "phy-connection-type" property and
+ * returns the corresponding PHY interface type.
+ *
+ * @mac_node:  ofnode containing the property
+ * Return: one of PHY_INTERFACE_MODE_* constants, PHY_INTERFACE_MODE_NA on
+ *        error
+ */
+phy_interface_t ofnode_read_phy_mode(ofnode mac_node);
+
+#if CONFIG_IS_ENABLED(DM)
+/**
  * ofnode_conf_read_bool() - Read a boolean value from the U-Boot config
  *
  * This reads a property from the /config node of the devicetree.
@@ -1218,30 +1432,34 @@ int ofnode_conf_read_int(const char *prop_name, int default_val);
  */
 const char *ofnode_conf_read_str(const char *prop_name);
 
-/**
- * ofnode_get_phy_node() - Get PHY node for a MAC (if not fixed-link)
- *
- * This function parses PHY handle from the Ethernet controller's ofnode
- * (trying all possible PHY handle property names), and returns the PHY ofnode.
- *
- * Before this is used, ofnode_phy_is_fixed_link() should be checked first, and
- * if the result to that is true, this function should not be called.
- *
- * @eth_node:  ofnode belonging to the Ethernet controller
- * Return: ofnode of the PHY, if it exists, otherwise an invalid ofnode
- */
-ofnode ofnode_get_phy_node(ofnode eth_node);
+#else /* CONFIG_DM */
+static inline bool ofnode_conf_read_bool(const char *prop_name)
+{
+       return false;
+}
+
+static inline int ofnode_conf_read_int(const char *prop_name, int default_val)
+{
+       return default_val;
+}
+
+static inline const char *ofnode_conf_read_str(const char *prop_name)
+{
+       return NULL;
+}
+
+#endif /* CONFIG_DM */
 
 /**
- * ofnode_read_phy_mode() - Read PHY connection type from a MAC node
+ * of_add_subnode() - add a new subnode to a node
  *
- * This function parses the "phy-mode" / "phy-connection-type" property and
- * returns the corresponding PHY interface type.
- *
- * @mac_node:  ofnode containing the property
- * Return: one of PHY_INTERFACE_MODE_* constants, PHY_INTERFACE_MODE_NA on
- *        error
+ * @parent:    parent node to add to
+ * @name:      name of subnode
+ * @nodep:     returns pointer to new subnode (valid if the function returns 0
+ *     or -EEXIST)
+ * Returns 0 if OK, -EEXIST if already exists, -ENOMEM if out of memory, other
+ * -ve on other error
  */
-phy_interface_t ofnode_read_phy_mode(ofnode mac_node);
+int ofnode_add_subnode(ofnode parent, const char *name, ofnode *nodep);
 
 #endif