[U-Boot] [PATCH 1/6] drivers: of: add support for get device address based on index

Mugunthan V N mugunthanvnm at ti.com
Wed Sep 2 13:15:37 CEST 2015


When a device has multiple memory regions, api "dev_get_addr"
doesn't return the address and returns error. So implementing a
new api to get device address based on index.

Signed-off-by: Mugunthan V N <mugunthanvnm at ti.com>
---
 drivers/core/device.c | 18 ++++++++++++++++++
 include/dm/device.h   | 10 ++++++++++
 include/fdtdec.h      | 14 ++++++++++++++
 lib/fdtdec.c          | 35 +++++++++++++++++++++++++++++++++++
 4 files changed, 77 insertions(+)

diff --git a/drivers/core/device.c b/drivers/core/device.c
index a6cd936..398aad0 100644
--- a/drivers/core/device.c
+++ b/drivers/core/device.c
@@ -591,6 +591,24 @@ fdt_addr_t dev_get_addr(struct udevice *dev)
 #endif
 }
 
+fdt_addr_t dev_get_addr_index(struct udevice *dev, int index)
+{
+#if CONFIG_IS_ENABLED(OF_CONTROL)
+	fdt_addr_t addr;
+
+	addr = fdtdec_get_addr_size_index(gd->fdt_blob, dev->of_offset,
+					  "reg", NULL, index);
+	if (addr != FDT_ADDR_T_NONE) {
+		if (device_get_uclass_id(dev->parent) == UCLASS_SIMPLE_BUS)
+			addr = simple_bus_translate(dev->parent, addr);
+	}
+
+	return addr;
+#else
+	return FDT_ADDR_T_NONE;
+#endif
+}
+
 bool device_has_children(struct udevice *dev)
 {
 	return !list_empty(&dev->child_head);
diff --git a/include/dm/device.h b/include/dm/device.h
index a239be6..dea04f8 100644
--- a/include/dm/device.h
+++ b/include/dm/device.h
@@ -441,6 +441,16 @@ int device_find_next_child(struct udevice **devp);
 fdt_addr_t dev_get_addr(struct udevice *dev);
 
 /**
+ * dev_get_addr_index() - Get the index reg property of a device
+ *
+ * @dev: Pointer to a device
+ * @index: reg address index
+ *
+ * @return addr
+ */
+fdt_addr_t dev_get_addr_index(struct udevice *dev, int index);
+
+/**
  * device_has_children() - check if a device has any children
  *
  * @dev:	Device to check
diff --git a/include/fdtdec.h b/include/fdtdec.h
index 3e23731..b9e4ac6 100644
--- a/include/fdtdec.h
+++ b/include/fdtdec.h
@@ -321,6 +321,20 @@ fdt_addr_t fdtdec_get_addr_size(const void *blob, int node,
 		const char *prop_name, fdt_size_t *sizep);
 
 /**
+ * Look up an address property in a node and return it as an address.
+ * The property must hold one address with a length. This is only tested
+ * on 32-bit machines.
+ *
+ * @param blob	FDT blob
+ * @param node	node to examine
+ * @param prop_name	name of property to find
+ * @param index		index of the address
+ * @return address, if found, or FDT_ADDR_T_NONE if not
+ */
+fdt_addr_t fdtdec_get_addr_size_index(const void *blob, int node,
+		const char *prop_name, fdt_size_t *sizep, int index);
+
+/**
  * Look at an address property in a node and return the pci address which
  * corresponds to the given type in the form of fdt_pci_addr.
  * The property must hold one fdt_pci_addr with a lengh.
diff --git a/lib/fdtdec.c b/lib/fdtdec.c
index 29c5ccb..3afcf07 100644
--- a/lib/fdtdec.c
+++ b/lib/fdtdec.c
@@ -119,6 +119,41 @@ fdt_addr_t fdtdec_get_addr(const void *blob, int node,
 	return fdtdec_get_addr_size(blob, node, prop_name, NULL);
 }
 
+fdt_addr_t fdtdec_get_addr_size_index(const void *blob, int node,
+		const char *prop_name, fdt_size_t *sizep, int index)
+{
+	const fdt_addr_t *cell;
+	int len;
+
+	debug("%s: %s: ", __func__, prop_name);
+	cell = fdt_getprop(blob, node, prop_name, &len);
+	if (len < sizeof(fdt_addr_t) * 2 * (index + 1)) {
+		debug("(not found)\n");
+		return FDT_ADDR_T_NONE;
+	}
+
+	len -= sizeof(fdt_addr_t) * 2 * index;
+	cell += index * 2;
+	if (cell && ((!sizep && len >= sizeof(fdt_addr_t)) ||
+		     len >= sizeof(fdt_addr_t) * 2)) {
+		fdt_addr_t addr = fdt_addr_to_cpu(*cell);
+		if (sizep) {
+			const fdt_size_t *size;
+
+			size = (fdt_size_t *)((char *)cell +
+					sizeof(fdt_addr_t));
+			*sizep = fdt_size_to_cpu(*size);
+			debug("addr=%08lx, size=%llx\n",
+			      (ulong)addr, (u64)*sizep);
+		} else {
+			debug("%08lx\n", (ulong)addr);
+		}
+		return addr;
+	}
+	debug("(not found)\n");
+	return FDT_ADDR_T_NONE;
+}
+
 #ifdef CONFIG_PCI
 int fdtdec_get_pci_addr(const void *blob, int node, enum fdt_pci_space type,
 		const char *prop_name, struct fdt_pci_addr *addr)
-- 
2.5.1.522.g7aa67f6



More information about the U-Boot mailing list