[U-Boot] [PATCH v3 04/35] dm: core: Add functions to obtain node's address/size cells

Simon Glass sjg at chromium.org
Mon Jun 12 12:21:31 UTC 2017


The of_n_addr_cells() and of_n_size_cells() functions are useful for
getting the size of addresses in a node, but in a few places U-Boot needs
to obtain the actual property value for a node without walking up the
stack. Add functions for this and just the existing code to use it.

Add a comment the existing ofnode functions which do not do the right
thing with a flat tree.

This fixes a problem reading PCI addresses.

Signed-off-by: Simon Glass <sjg at chromium.org>
---

Changes in v3:
- Add new patch with functions to obtain node's address/size cells

Changes in v2: None

 drivers/core/of_access.c | 24 +++++++++++++++++++++
 drivers/core/ofnode.c    | 18 +++++++++++++++-
 drivers/core/read.c      | 10 +++++++++
 drivers/core/regmap.c    |  4 ++--
 drivers/pci/pci-uclass.c |  6 +++---
 include/dm/of_access.h   | 20 +++++++++++++++++
 include/dm/ofnode.h      | 20 +++++++++++++++++
 include/dm/read.h        | 56 +++++++++++++++++++++++++++++++++++++++++++++++-
 8 files changed, 151 insertions(+), 7 deletions(-)

diff --git a/drivers/core/of_access.c b/drivers/core/of_access.c
index 94ef3cc251..b1eaea924a 100644
--- a/drivers/core/of_access.c
+++ b/drivers/core/of_access.c
@@ -96,6 +96,30 @@ int of_n_size_cells(const struct device_node *np)
 	return OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
 }
 
+int of_simple_addr_cells(const struct device_node *np)
+{
+	const __be32 *ip;
+
+	ip = of_get_property(np, "#address-cells", NULL);
+	if (ip)
+		return be32_to_cpup(ip);
+
+	/* Return a default of 2 to match fdt_address_cells()*/
+	return 2;
+}
+
+int of_simple_size_cells(const struct device_node *np)
+{
+	const __be32 *ip;
+
+	ip = of_get_property(np, "#size-cells", NULL);
+	if (ip)
+		return be32_to_cpup(ip);
+
+	/* Return a default of 2 to match fdt_size_cells()*/
+	return 2;
+}
+
 struct property *of_find_property(const struct device_node *np,
 				  const char *name, int *lenp)
 {
diff --git a/drivers/core/ofnode.c b/drivers/core/ofnode.c
index 79c80df7f4..da7c477c81 100644
--- a/drivers/core/ofnode.c
+++ b/drivers/core/ofnode.c
@@ -552,7 +552,7 @@ int ofnode_read_addr_cells(ofnode node)
 {
 	if (ofnode_is_np(node))
 		return of_n_addr_cells(ofnode_to_np(node));
-	else
+	else  /* NOTE: this call should walk up the parent stack */
 		return fdt_address_cells(gd->fdt_blob, ofnode_to_offset(node));
 }
 
@@ -560,6 +560,22 @@ int ofnode_read_size_cells(ofnode node)
 {
 	if (ofnode_is_np(node))
 		return of_n_size_cells(ofnode_to_np(node));
+	else  /* NOTE: this call should walk up the parent stack */
+		return fdt_size_cells(gd->fdt_blob, ofnode_to_offset(node));
+}
+
+int ofnode_read_simple_addr_cells(ofnode node)
+{
+	if (ofnode_is_np(node))
+		return of_simple_addr_cells(ofnode_to_np(node));
+	else
+		return fdt_address_cells(gd->fdt_blob, ofnode_to_offset(node));
+}
+
+int ofnode_read_simple_size_cells(ofnode node)
+{
+	if (ofnode_is_np(node))
+		return of_simple_size_cells(ofnode_to_np(node));
 	else
 		return fdt_size_cells(gd->fdt_blob, ofnode_to_offset(node));
 }
diff --git a/drivers/core/read.c b/drivers/core/read.c
index 1080767313..36293ba326 100644
--- a/drivers/core/read.c
+++ b/drivers/core/read.c
@@ -94,6 +94,16 @@ int dev_read_size_cells(struct udevice *dev)
 	return ofnode_read_size_cells(dev_ofnode(dev));
 }
 
+int dev_read_simple_addr_cells(struct udevice *dev)
+{
+	return ofnode_read_simple_addr_cells(dev_ofnode(dev));
+}
+
+int dev_read_simple_size_cells(struct udevice *dev)
+{
+	return ofnode_read_simple_size_cells(dev_ofnode(dev));
+}
+
 int dev_read_phandle(struct udevice *dev)
 {
 	ofnode node = dev_ofnode(dev);
diff --git a/drivers/core/regmap.c b/drivers/core/regmap.c
index 749d913372..d4e16a27ef 100644
--- a/drivers/core/regmap.c
+++ b/drivers/core/regmap.c
@@ -72,8 +72,8 @@ int regmap_init_mem(struct udevice *dev, struct regmap **mapp)
 	ofnode node = dev_ofnode(dev);
 	struct resource r;
 
-	addr_len = dev_read_addr_cells(dev->parent);
-	size_len = dev_read_size_cells(dev->parent);
+	addr_len = dev_read_simple_addr_cells(dev->parent);
+	size_len = dev_read_simple_size_cells(dev->parent);
 	both_len = addr_len + size_len;
 
 	len = dev_read_size(dev, "reg");
diff --git a/drivers/pci/pci-uclass.c b/drivers/pci/pci-uclass.c
index b36ef3338c..4223040541 100644
--- a/drivers/pci/pci-uclass.c
+++ b/drivers/pci/pci-uclass.c
@@ -766,9 +766,9 @@ static int decode_regions(struct pci_controller *hose, ofnode parent_node,
 	prop = ofnode_read_prop(node, "ranges", &len);
 	if (!prop)
 		return -EINVAL;
-	pci_addr_cells = ofnode_read_addr_cells(node);
-	addr_cells = ofnode_read_addr_cells(parent_node);
-	size_cells = ofnode_read_size_cells(node);
+	pci_addr_cells = ofnode_read_simple_addr_cells(node);
+	addr_cells = ofnode_read_simple_addr_cells(parent_node);
+	size_cells = ofnode_read_simple_size_cells(node);
 
 	/* PCI addresses are always 3-cells */
 	len /= sizeof(u32);
diff --git a/include/dm/of_access.h b/include/dm/of_access.h
index d2827001e2..c5ea391aec 100644
--- a/include/dm/of_access.h
+++ b/include/dm/of_access.h
@@ -60,6 +60,26 @@ int of_n_addr_cells(const struct device_node *np);
  */
 int of_n_size_cells(const struct device_node *np);
 
+/**
+ * of_simple_addr_cells() - Get the address cells property in a node
+ *
+ * This function matches fdt_address_cells().
+ *
+ * @np: Node pointer to check
+ * @return value of #address-cells property in this node, or 2 if none
+ */
+int of_simple_addr_cells(const struct device_node *np);
+
+/**
+ * of_simple_size_cells() - Get the size cells property in a node
+ *
+ * This function matches fdt_size_cells().
+ *
+ * @np: Node pointer to check
+ * @return value of #size-cells property in this node, or 2 if none
+ */
+int of_simple_size_cells(const struct device_node *np);
+
 /**
  * of_find_property() - find a property in a node
  *
diff --git a/include/dm/ofnode.h b/include/dm/ofnode.h
index d261a61e91..c3d8db5b16 100644
--- a/include/dm/ofnode.h
+++ b/include/dm/ofnode.h
@@ -561,6 +561,26 @@ int ofnode_read_addr_cells(ofnode node);
  */
 int ofnode_read_size_cells(ofnode node);
 
+/**
+ * ofnode_read_simple_addr_cells() - Get the address cells property in a node
+ *
+ * This function matches fdt_address_cells().
+ *
+ * @np: Node pointer to check
+ * @return value of #address-cells property in this node, or 2 if none
+ */
+int ofnode_read_simple_addr_cells(ofnode node);
+
+/**
+ * ofnode_read_simple_size_cells() - Get the size cells property in a node
+ *
+ * This function matches fdt_size_cells().
+ *
+ * @np: Node pointer to check
+ * @return value of #size-cells property in this node, or 2 if none
+ */
+int ofnode_read_simple_size_cells(ofnode node);
+
 /**
  * ofnode_pre_reloc() - check if a node should be bound before relocation
  *
diff --git a/include/dm/read.h b/include/dm/read.h
index cea1c16a00..e519c97327 100644
--- a/include/dm/read.h
+++ b/include/dm/read.h
@@ -230,6 +230,48 @@ int dev_read_addr_cells(struct udevice *dev);
  */
 int dev_read_size_cells(struct udevice *dev);
 
+/**
+ * dev_read_addr_cells() - Get the number of address cells for a device's node
+ *
+ * This walks back up the tree to find the closest #address-cells property
+ * which controls the given node.
+ *
+ * @dev: devioe to check
+ * @return number of address cells this node uses
+ */
+int dev_read_addr_cells(struct udevice *dev);
+
+/**
+ * dev_read_size_cells() - Get the number of size cells for a device's node
+ *
+ * This walks back up the tree to find the closest #size-cells property
+ * which controls the given node.
+ *
+ * @dev: devioe to check
+ * @return number of size cells this node uses
+ */
+int dev_read_size_cells(struct udevice *dev);
+
+/**
+ * dev_read_addr_cells() - Get the address cells property in a node
+ *
+ * This function matches fdt_address_cells().
+ *
+ * @dev: devioe to check
+ * @return number of address cells this node uses
+ */
+int dev_read_simple_addr_cells(struct udevice *dev);
+
+/**
+ * dev_read_size_cells() - Get the size cells property in a node
+ *
+ * This function matches fdt_size_cells().
+ *
+ * @dev: devioe to check
+ * @return number of size cells this node uses
+ */
+int dev_read_simple_size_cells(struct udevice *dev);
+
 /**
  * dev_read_phandle() - Get the phandle from a device
  *
@@ -398,17 +440,29 @@ static inline int dev_read_phandle_with_args(struct udevice *dev,
 
 static inline int dev_read_addr_cells(struct udevice *dev)
 {
+	/* NOTE: this call should walk up the parent stack */
 	return fdt_address_cells(gd->fdt_blob, dev_of_offset(dev));
 }
 
 static inline int dev_read_size_cells(struct udevice *dev)
+{
+	/* NOTE: this call should walk up the parent stack */
+	return fdt_size_cells(gd->fdt_blob, dev_of_offset(dev));
+}
+
+static inline int dev_read_simple_addr_cells(struct udevice *dev)
+{
+	return fdt_address_cells(gd->fdt_blob, dev_of_offset(dev));
+}
+
+static inline int dev_read_simple_size_cells(struct udevice *dev)
 {
 	return fdt_size_cells(gd->fdt_blob, dev_of_offset(dev));
 }
 
 static inline int dev_read_phandle(struct udevice *dev)
 {
-	return fdt_get_phandle(gd->fdt_blob, dev_of_offset(dev));
+	return fdt_size_cells(gd->fdt_blob, dev_of_offset(dev));
 }
 
 static inline const u32 *dev_read_prop(struct udevice *dev,
-- 
2.13.1.508.gb3defc5cc-goog



More information about the U-Boot mailing list