Commit ff61bacd authored by Rob Herring's avatar Rob Herring

of/address: Add of_property_read_reg() helper

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.

Link: https://lore.kernel.org/r/20230328-dt-address-helpers-v1-5-e2456c3e77ab@kernel.orgSigned-off-by: default avatarRob Herring <robh@kernel.org>
parent b50c788a
...@@ -760,6 +760,29 @@ const __be32 *__of_get_address(struct device_node *dev, int index, int bar_no, ...@@ -760,6 +760,29 @@ const __be32 *__of_get_address(struct device_node *dev, int index, int bar_no,
} }
EXPORT_SYMBOL(__of_get_address); 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, static int parser_init(struct of_pci_range_parser *parser,
struct device_node *node, const char *name) struct device_node *node, const char *name)
{ {
......
...@@ -1134,6 +1134,27 @@ static void __init of_unittest_bus_3cell_ranges(void) ...@@ -1134,6 +1134,27 @@ static void __init of_unittest_bus_3cell_ranges(void)
of_node_put(np); 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) static void __init of_unittest_parse_interrupts(void)
{ {
struct device_node *np; struct device_node *np;
...@@ -3772,6 +3793,7 @@ static int __init of_unittest(void) ...@@ -3772,6 +3793,7 @@ static int __init of_unittest(void)
of_unittest_pci_dma_ranges(); of_unittest_pci_dma_ranges();
of_unittest_bus_ranges(); of_unittest_bus_ranges();
of_unittest_bus_3cell_ranges(); of_unittest_bus_3cell_ranges();
of_unittest_reg();
of_unittest_match_node(); of_unittest_match_node();
of_unittest_platform_populate(); of_unittest_platform_populate();
of_unittest_overlay(); of_unittest_overlay();
......
...@@ -72,6 +72,8 @@ void __iomem *of_io_request_and_map(struct device_node *device, ...@@ -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, extern const __be32 *__of_get_address(struct device_node *dev, int index, int bar_no,
u64 *size, unsigned int *flags); 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, extern int of_pci_range_parser_init(struct of_pci_range_parser *parser,
struct device_node *node); struct device_node *node);
extern int of_pci_dma_range_parser_init(struct of_pci_range_parser *parser, 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, ...@@ -106,6 +108,11 @@ static inline const __be32 *__of_get_address(struct device_node *dev, int index,
return NULL; return NULL;
} }
static inline 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, static inline int of_pci_range_parser_init(struct of_pci_range_parser *parser,
struct device_node *node) struct device_node *node)
{ {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment