Add a helper, of_property_read_reg(), to read "reg" entries untranslated
address and size. This function is intended mainly for cases with an
untranslatable "reg" address (i.e. not MMIO). There's also a few
translatable cases such as address cells containing a bus chip-select
number.

Signed-off-by: Rob Herring <r...@kernel.org>
---
 drivers/of/address.c       | 23 +++++++++++++++++++++++
 drivers/of/unittest.c      | 22 ++++++++++++++++++++++
 include/linux/of_address.h |  7 +++++++
 3 files changed, 52 insertions(+)

diff --git a/drivers/of/address.c b/drivers/of/address.c
index 8cfae24148e0..fdb15c6fb3b1 100644
--- a/drivers/of/address.c
+++ b/drivers/of/address.c
@@ -760,6 +760,29 @@ const __be32 *__of_get_address(struct device_node *dev, 
int index, int bar_no,
 }
 EXPORT_SYMBOL(__of_get_address);
 
+/**
+ * of_property_read_reg - Retrieve the specified "reg" entry index without 
translating
+ * @np: device tree node for which to retrieve "reg" from
+ * @idx: "reg" entry index to read
+ * @addr: return value for the untranslated address
+ * @size: return value for the entry size
+ *
+ * Returns -EINVAL if "reg" is not found. Returns 0 on success with addr and
+ * size values filled in.
+ */
+int of_property_read_reg(struct device_node *np, int idx, u64 *addr, u64 *size)
+{
+       const __be32 *prop = of_get_address(np, idx, size, NULL);
+
+       if (!prop)
+               return -EINVAL;
+
+       *addr = of_read_number(prop, of_n_addr_cells(np));
+
+       return 0;
+}
+EXPORT_SYMBOL(of_property_read_reg);
+
 static int parser_init(struct of_pci_range_parser *parser,
                        struct device_node *node, const char *name)
 {
diff --git a/drivers/of/unittest.c b/drivers/of/unittest.c
index eaeb58065acc..e73ecbef977b 100644
--- a/drivers/of/unittest.c
+++ b/drivers/of/unittest.c
@@ -1134,6 +1134,27 @@ static void __init of_unittest_bus_3cell_ranges(void)
        of_node_put(np);
 }
 
+static void __init of_unittest_reg(void)
+{
+       struct device_node *np;
+       int ret;
+       u64 addr, size;
+
+       np = 
of_find_node_by_path("/testcase-data/address-tests/bus@80000000/device@1000");
+       if (!np) {
+               pr_err("missing testcase data\n");
+               return;
+       }
+
+       ret = of_property_read_reg(np, 0, &addr, &size);
+       unittest(!ret, "of_property_read_reg(%pOF) returned error %d\n",
+               np, ret);
+       unittest(addr == 0x1000, "of_property_read_reg(%pOF) untranslated 
address (%llx) incorrect\n",
+               np, addr);
+
+       of_node_put(np);
+}
+
 static void __init of_unittest_parse_interrupts(void)
 {
        struct device_node *np;
@@ -3772,6 +3793,7 @@ static int __init of_unittest(void)
        of_unittest_pci_dma_ranges();
        of_unittest_bus_ranges();
        of_unittest_bus_3cell_ranges();
+       of_unittest_reg();
        of_unittest_match_node();
        of_unittest_platform_populate();
        of_unittest_overlay();
diff --git a/include/linux/of_address.h b/include/linux/of_address.h
index 5292f62c1baa..95cb6c5c2d67 100644
--- a/include/linux/of_address.h
+++ b/include/linux/of_address.h
@@ -72,6 +72,8 @@ void __iomem *of_io_request_and_map(struct device_node 
*device,
 extern const __be32 *__of_get_address(struct device_node *dev, int index, int 
bar_no,
                                      u64 *size, unsigned int *flags);
 
+int of_property_read_reg(struct device_node *np, int idx, u64 *addr, u64 
*size);
+
 extern int of_pci_range_parser_init(struct of_pci_range_parser *parser,
                        struct device_node *node);
 extern int of_pci_dma_range_parser_init(struct of_pci_range_parser *parser,
@@ -106,6 +108,11 @@ static inline const __be32 *__of_get_address(struct 
device_node *dev, int index,
        return NULL;
 }
 
+static int of_property_read_reg(struct device_node *np, int idx, u64 *addr, 
u64 *size)
+{
+       return -ENOSYS;
+}
+
 static inline int of_pci_range_parser_init(struct of_pci_range_parser *parser,
                        struct device_node *node)
 {

-- 
2.39.2

Reply via email to