Skip to content

Commit c75a794

Browse files
committed
of/address: Add of_range_to_resource() helper
A few users need to convert a specific "ranges" entry into a struct resource. Add a helper to similar to of_address_to_resource(). The existing of_pci_range_to_resource() helper isn't really PCI specific, so it can be used with the CONFIG_PCI check dropped. Link: https://lore.kernel.org/r/20230328-dt-address-helpers-v1-2-e2456c3e77ab@kernel.org Signed-off-by: Rob Herring <robh@kernel.org>
1 parent 6d32dad commit c75a794

3 files changed

Lines changed: 51 additions & 4 deletions

File tree

drivers/of/address.c

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -229,9 +229,6 @@ int of_pci_range_to_resource(struct of_pci_range *range,
229229
res->parent = res->child = res->sibling = NULL;
230230
res->name = np->full_name;
231231

232-
if (!IS_ENABLED(CONFIG_PCI))
233-
return -ENOSYS;
234-
235232
if (res->flags & IORESOURCE_IO) {
236233
unsigned long port;
237234
err = pci_register_io_range(&np->fwnode, range->cpu_addr,
@@ -263,6 +260,34 @@ int of_pci_range_to_resource(struct of_pci_range *range,
263260
}
264261
EXPORT_SYMBOL(of_pci_range_to_resource);
265262

263+
/*
264+
* of_range_to_resource - Create a resource from a ranges entry
265+
* @np: device node where the range belongs to
266+
* @index: the 'ranges' index to convert to a resource
267+
* @res: pointer to a valid resource that will be updated to
268+
* reflect the values contained in the range.
269+
*
270+
* Returns ENOENT if the entry is not found or EINVAL if the range cannot be
271+
* converted to resource.
272+
*/
273+
int of_range_to_resource(struct device_node *np, int index, struct resource *res)
274+
{
275+
int ret, i = 0;
276+
struct of_range_parser parser;
277+
struct of_range range;
278+
279+
ret = of_range_parser_init(&parser, np);
280+
if (ret)
281+
return ret;
282+
283+
for_each_of_range(&parser, &range)
284+
if (i++ == index)
285+
return of_pci_range_to_resource(&range, np, res);
286+
287+
return -ENOENT;
288+
}
289+
EXPORT_SYMBOL(of_range_to_resource);
290+
266291
/*
267292
* ISA bus specific translator
268293
*/

drivers/of/unittest.c

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1013,7 +1013,8 @@ static void __init of_unittest_bus_ranges(void)
10131013
struct device_node *np;
10141014
struct of_range range;
10151015
struct of_range_parser parser;
1016-
int i = 0;
1016+
struct resource res;
1017+
int ret, i = 0;
10171018

10181019
np = of_find_node_by_path("/testcase-data/address-tests");
10191020
if (!np) {
@@ -1026,6 +1027,19 @@ static void __init of_unittest_bus_ranges(void)
10261027
return;
10271028
}
10281029

1030+
ret = of_range_to_resource(np, 1, &res);
1031+
unittest(!ret, "of_range_to_resource returned error (%d) node %pOF\n",
1032+
ret, np);
1033+
unittest(resource_type(&res) == IORESOURCE_MEM,
1034+
"of_range_to_resource wrong resource type on node %pOF res=%pR\n",
1035+
np, &res);
1036+
unittest(res.start == 0xd0000000,
1037+
"of_range_to_resource wrong resource start address on node %pOF res=%pR\n",
1038+
np, &res);
1039+
unittest(resource_size(&res) == 0x20000000,
1040+
"of_range_to_resource wrong resource start address on node %pOF res=%pR\n",
1041+
np, &res);
1042+
10291043
/*
10301044
* Get the "ranges" from the device tree
10311045
*/

include/linux/of_address.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ extern int of_pci_address_to_resource(struct device_node *dev, int bar,
6868
extern int of_pci_range_to_resource(struct of_pci_range *range,
6969
struct device_node *np,
7070
struct resource *res);
71+
extern int of_range_to_resource(struct device_node *np, int index,
72+
struct resource *res);
7173
extern bool of_dma_is_coherent(struct device_node *np);
7274
#else /* CONFIG_OF_ADDRESS */
7375
static inline void __iomem *of_io_request_and_map(struct device_node *device,
@@ -120,6 +122,12 @@ static inline int of_pci_range_to_resource(struct of_pci_range *range,
120122
return -ENOSYS;
121123
}
122124

125+
static inline int of_range_to_resource(struct device_node *np, int index,
126+
struct resource *res)
127+
{
128+
return -ENOSYS;
129+
}
130+
123131
static inline bool of_dma_is_coherent(struct device_node *np)
124132
{
125133
return false;

0 commit comments

Comments
 (0)