Merge tag 'xilinx-for-v2021.01-v2' of https://gitlab.denx.de/u-boot/custodians/u...
[pandora-u-boot.git] / include / dm / ofnode.h
index c06d778..ced7f6f 100644 (file)
@@ -10,6 +10,7 @@
 /* TODO(sjg@chromium.org): Drop fdtdec.h include */
 #include <fdtdec.h>
 #include <dm/of.h>
+#include <log.h>
 
 /* Enable checks to protect against invalid calls */
 #undef OF_CHECKS
@@ -48,7 +49,7 @@ struct resource;
  *     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;   /* will be used for future live tree */
+       const struct device_node *np;
        long of_offset;
 } ofnode;
 
@@ -59,7 +60,32 @@ struct ofnode_phandle_args {
 };
 
 /**
- * _ofnode_to_np() - convert an ofnode to a live DT node pointer
+ * ofprop - reference to a property of a device tree node
+ *
+ * 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.
+ */
+
+struct ofprop {
+       ofnode node;
+       union {
+               int offset;
+               const struct property *prop;
+       };
+};
+
+/**
+ * ofnode_to_np() - convert an ofnode to a live DT node pointer
  *
  * This cannot be called if the reference contains an offset.
  *
@@ -102,7 +128,7 @@ static inline bool ofnode_valid(ofnode node)
        if (of_live_active())
                return node.np != NULL;
        else
-               return node.of_offset != -1;
+               return node.of_offset >= 0;
 }
 
 /**
@@ -118,7 +144,7 @@ static inline ofnode offset_to_ofnode(int of_offset)
        if (of_live_active())
                node.np = NULL;
        else
-               node.of_offset = of_offset;
+               node.of_offset = of_offset >= 0 ? of_offset : -1;
 
        return node;
 }
@@ -157,8 +183,8 @@ static inline bool ofnode_is_np(ofnode node)
         * live tree is in use.
         */
        assert(!ofnode_valid(node) ||
-              (of_live_active() ? _ofnode_to_np(node)
-                                 : _ofnode_to_np(node)));
+              (of_live_active() ? ofnode_to_np(node)
+                                 : ofnode_to_np(node)));
 #endif
        return of_live_active() && ofnode_valid(node);
 }
@@ -202,6 +228,18 @@ static inline ofnode ofnode_null(void)
  */
 int ofnode_read_u32(ofnode node, const char *propname, u32 *outp);
 
+/**
+ * ofnode_read_u32_index() - Read a 32-bit integer from a multi-value property
+ *
+ * @ref:       valid node reference to read property from
+ * @propname:  name of the property to read from
+ * @index:     index of the integer to return
+ * @outp:      place to put value (if found)
+ * @return 0 if OK, -ve on error
+ */
+int ofnode_read_u32_index(ofnode node, const char *propname, int index,
+                         u32 *outp);
+
 /**
  * ofnode_read_s32() - Read a 32-bit integer from a property
  *
@@ -224,7 +262,20 @@ static inline int ofnode_read_s32(ofnode node, const char *propname,
  * @def:       default value to return if the property has no value
  * @return property value, or @def if not found
  */
-int ofnode_read_u32_default(ofnode ref, const char *propname, u32 def);
+u32 ofnode_read_u32_default(ofnode ref, const char *propname, u32 def);
+
+/**
+ * ofnode_read_u32_index_default() - Read a 32-bit integer from a multi-value
+ *                                   property
+ *
+ * @ref:       valid node reference to read property from
+ * @propname:  name of the property to read from
+ * @index:     index of the integer to return
+ * @def:       default value to return if the property has no value
+ * @return property value, or @def if not found
+ */
+u32 ofnode_read_u32_index_default(ofnode ref, const char *propname, int index,
+                                 u32 def);
 
 /**
  * ofnode_read_s32_default() - Read a 32-bit integer from a property
@@ -236,6 +287,16 @@ int ofnode_read_u32_default(ofnode ref, const char *propname, u32 def);
  */
 int ofnode_read_s32_default(ofnode node, const char *propname, s32 def);
 
+/**
+ * ofnode_read_u64() - Read a 64-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_u64(ofnode node, const char *propname, u64 *outp);
+
 /**
  * ofnode_read_u64_default() - Read a 64-bit integer from a property
  *
@@ -244,12 +305,23 @@ int ofnode_read_s32_default(ofnode node, const char *propname, s32 def);
  * @def:       default value to return if the property has no value
  * @return property value, or @def if not found
  */
-int ofnode_read_u64_default(ofnode node, const char *propname, u64 def);
+u64 ofnode_read_u64_default(ofnode node, const char *propname, u64 def);
+
+/**
+ * ofnode_read_prop() - Read a property from a node
+ *
+ * @node:      valid node reference to read property from
+ * @propname:  name of the property to read
+ * @sizep:     if non-NULL, returns the size of the property, or an error code
+               if not found
+ * @return property value, or NULL if there is no such property
+ */
+const void *ofnode_read_prop(ofnode node, const char *propname, int *sizep);
 
 /**
  * ofnode_read_string() - Read a string from a property
  *
- * @ref:       valid node reference to read property from
+ * @node:      valid node reference to read property from
  * @propname:  name of the property to read
  * @return string from property value, or NULL if there is no such property
  */
@@ -323,7 +395,7 @@ ofnode ofnode_get_parent(ofnode node);
  * ofnode_get_name() - get the name of a node
  *
  * @node: valid node to look up
- * @return name or node
+ * @return name of node
  */
 const char *ofnode_get_name(ofnode node);
 
@@ -344,6 +416,20 @@ ofnode ofnode_get_by_phandle(uint phandle);
  */
 int ofnode_read_size(ofnode node, const char *propname);
 
+/**
+ * ofnode_get_addr_size_index() - get an address/size from a node
+ *                               based on index
+ *
+ * This reads the register address/size from a node based on index
+ *
+ * @node: node to read from
+ * @index: Index of address to read (0 for first)
+ * @size: Pointer to size of the address
+ * @return address, or FDT_ADDR_T_NONE if not present or invalid
+ */
+phys_addr_t ofnode_get_addr_size_index(ofnode node, int index,
+                                      fdt_size_t *size);
+
 /**
  * ofnode_get_addr_index() - get an address from a node
  *
@@ -470,12 +556,13 @@ int ofnode_parse_phandle_with_args(ofnode node, const char *list_name,
  * @node:      device tree node containing a list
  * @list_name: property name that contains a list
  * @cells_name:        property name that specifies phandles' arguments count
+ * @cells_count: Cell count to use if @cells_name is NULL
  * @return number of phandle on success, -ENOENT if @list_name does not
  *      exist, -EINVAL if a phandle was not found, @cells_name could not
  *      be found.
  */
 int ofnode_count_phandle_with_args(ofnode node, const char *list_name,
-                                  const char *cells_name);
+                                  const char *cells_name, int cell_count);
 
 /**
  * ofnode_path() - find a node by full path
@@ -486,21 +573,59 @@ int ofnode_count_phandle_with_args(ofnode node, const char *list_name,
 ofnode ofnode_path(const char *path);
 
 /**
- * ofnode_get_chosen_prop() - get the value of a chosen property
+ * ofnode_read_chosen_prop() - get the value of a chosen property
  *
  * This looks for a property within the /chosen node and returns its value
  *
  * @propname: Property name to look for
+ * @sizep: Returns size of property, or FDT_ERR_... error code if function
+ *     returns NULL
+ * @return property value if found, else NULL
+ */
+const void *ofnode_read_chosen_prop(const char *propname, int *sizep);
+
+/**
+ * ofnode_read_chosen_string() - get the string value of a chosen property
+ *
+ * This looks for a property within the /chosen node and returns its value,
+ * checking that it is a valid nul-terminated string
+ *
+ * @propname: Property name to look for
+ * @return string value if found, else NULL
+ */
+const char *ofnode_read_chosen_string(const char *propname);
+
+/**
+ * ofnode_get_chosen_node() - get a referenced node from the chosen node
+ *
+ * This looks up a named property in the chosen node and uses that as a path to
+ * look up a code.
+ *
+ * @return the referenced node if present, else ofnode_null()
+ */
+ofnode ofnode_get_chosen_node(const char *propname);
+
+/**
+ * ofnode_read_aliases_prop() - get the value of a aliases property
+ *
+ * This looks for a property within the /aliases node and returns its value
+ *
+ * @propname: Property name to look for
+ * @sizep: Returns size of property, or FDT_ERR_... error code if function
+ *     returns NULL
  * @return property value if found, else NULL
  */
-const char *ofnode_get_chosen_prop(const char *propname);
+const void *ofnode_read_aliases_prop(const char *propname, int *sizep);
 
 /**
- * ofnode_get_chosen_node() - get the chosen node
+ * ofnode_get_aliases_node() - get a referenced node from the aliases node
+ *
+ * This looks up a named property in the aliases node and uses that as a path to
+ * look up a code.
  *
- * @return the chosen node if present, else ofnode_null()
+ * @return the referenced node if present, else ofnode_null()
  */
-ofnode ofnode_get_chosen_node(const char *name);
+ofnode ofnode_get_aliases_node(const char *propname);
 
 struct display_timing;
 /**
@@ -519,7 +644,7 @@ int ofnode_decode_display_timing(ofnode node, int index,
                                 struct display_timing *config);
 
 /**
- * ofnode_get_property()- - get a pointer to the value of a node property
+ * ofnode_get_property() - get a pointer to the value of a node property
  *
  * @node: node to read
  * @propname: property to read
@@ -528,6 +653,42 @@ 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
+ *
+ * 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().
+ *
+ * @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);
+
+/**
+ * ofnode_get_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().
+ *
+ * @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);
+
+/**
+ * ofnode_get_property_by_prop() - get a pointer to the value of a property
+ *
+ * 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
+ * @return 0 if OK, -ve on error. -FDT_ERR_NOTFOUND if not found
+ */
+const void *ofnode_get_property_by_prop(const struct ofprop *prop,
+                                       const char **propname, int *lenp);
+
 /**
  * ofnode_is_available() - check if a node is marked available
  *
@@ -652,12 +813,14 @@ int ofnode_read_simple_size_cells(ofnode node);
  * After relocation and jumping into the real U-Boot binary it is possible to
  * determine if a node was bound in one of SPL/TPL stages.
  *
- * There are 3 settings currently in use
- * -
+ * There are 4 settings currently in use
+ * - u-boot,dm-pre-proper: U-Boot proper pre-relocation only
  * - u-boot,dm-pre-reloc: legacy and indicates any of TPL or SPL
  *   Existing platforms only use it to indicate nodes needed in
  *   SPL. Should probably be replaced by u-boot,dm-spl for
  *   new platforms.
+ * - u-boot,dm-spl: SPL and U-Boot pre-relocation
+ * - u-boot,dm-tpl: TPL and U-Boot pre-relocation
  *
  * @node: node to check
  * @return true if node is needed in SPL/TL, false otherwise
@@ -741,7 +904,15 @@ ofnode ofnode_by_prop_value(ofnode from, const char *propname,
             node = ofnode_next_subnode(node))
 
 /**
- * ofnode_translate_address() - Tranlate a device-tree address
+ * ofnode_get_child_count() - get the child count of a ofnode
+ *
+ * @node: valid node to get its child count
+ * @return the number of subnodes
+ */
+int ofnode_get_child_count(ofnode parent);
+
+/**
+ * ofnode_translate_address() - Translate a device-tree address
  *
  * Translate an address from the device-tree into a CPU physical address. This
  * function walks up the tree and applies the various bus mappings along the
@@ -754,6 +925,20 @@ ofnode ofnode_by_prop_value(ofnode from, const char *propname,
  */
 u64 ofnode_translate_address(ofnode node, const fdt32_t *in_addr);
 
+/**
+ * ofnode_translate_dma_address() - Translate a device-tree DMA address
+ *
+ * Translate a DMA address from the device-tree into a CPU physical address.
+ * This function walks up the tree and applies the various bus mappings along
+ * the way.
+ *
+ * @ofnode: Device tree node giving the context in which to translate the
+ *          DMA address
+ * @in_addr: pointer to the DMA address to translate
+ * @return the translated DMA address; OF_BAD_ADDR on error
+ */
+u64 ofnode_translate_dma_address(ofnode node, const fdt32_t *in_addr);
+
 /**
  * ofnode_device_is_compatible() - check if the node is compatible with compat
  *
@@ -764,4 +949,50 @@ u64 ofnode_translate_address(ofnode node, const fdt32_t *in_addr);
  * @return true if OK, false if the compatible is not found
  */
 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.
+ *
+ * @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)
+ * @return 0 if successful, -ve on error
+ */
+int ofnode_write_prop(ofnode node, const char *propname, int len,
+                     const void *value);
+
+/**
+ * ofnode_write_string() - Set a string 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.
+ *
+ * @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 string property (must be valid prior to
+ *             calling the function)
+ * @return 0 if successful, -ve on error
+ */
+int ofnode_write_string(ofnode node, const char *propname, const char *value);
+
+/**
+ * ofnode_set_enabled() - Enable or disable a device tree node given by its
+ *                       ofnode
+ *
+ * This function effectively sets the node's "status" property to either "okay"
+ * or "disable", hence making it available for driver model initialization or
+ * not.
+ *
+ * @node:      The node to enable
+ * @value:     Flag that tells the function to either disable or enable the
+ *             node
+ * @return 0 if successful, -ve on error
+ */
+int ofnode_set_enabled(ofnode node, bool value);
+
 #endif