Commit 050a2c62 authored by Rob Herring's avatar Rob Herring

of: Merge of_get_address() and of_get_pci_address() implementations

of_get_address() and of_get_pci_address() are the same implementation
except of_get_pci_address() takes the PCI BAR number rather than an
index. Modify the of_get_address() implementation to work on either
index or BAR and provide wrapper functions for the existing functions.

Cc: Frank Rowand <frowand.list@gmail.com>
Signed-off-by: default avatarRob Herring <robh@kernel.org>
Link: https://lore.kernel.org/r/20210527194547.1287934-3-robh@kernel.org
parent 00dcc7cf
...@@ -199,50 +199,6 @@ static int of_bus_pci_translate(__be32 *addr, u64 offset, int na) ...@@ -199,50 +199,6 @@ static int of_bus_pci_translate(__be32 *addr, u64 offset, int na)
return of_bus_default_translate(addr + 1, offset, na - 1); return of_bus_default_translate(addr + 1, offset, na - 1);
} }
const __be32 *of_get_pci_address(struct device_node *dev, int bar_no, u64 *size,
unsigned int *flags)
{
const __be32 *prop;
unsigned int psize;
struct device_node *parent;
struct of_bus *bus;
int onesize, i, na, ns;
/* Get parent & match bus type */
parent = of_get_parent(dev);
if (parent == NULL)
return NULL;
bus = of_match_bus(parent);
if (strcmp(bus->name, "pci")) {
of_node_put(parent);
return NULL;
}
bus->count_cells(dev, &na, &ns);
of_node_put(parent);
if (!OF_CHECK_ADDR_COUNT(na))
return NULL;
/* Get "reg" or "assigned-addresses" property */
prop = of_get_property(dev, bus->addresses, &psize);
if (prop == NULL)
return NULL;
psize /= 4;
onesize = na + ns;
for (i = 0; psize >= onesize; psize -= onesize, prop += onesize, i++) {
u32 val = be32_to_cpu(prop[0]);
if ((val & 0xff) == ((bar_no * 4) + PCI_BASE_ADDRESS_0)) {
if (size)
*size = of_read_number(prop + na, ns);
if (flags)
*flags = bus->get_flags(prop);
return prop;
}
}
return NULL;
}
EXPORT_SYMBOL(of_get_pci_address);
int of_pci_address_to_resource(struct device_node *dev, int bar, int of_pci_address_to_resource(struct device_node *dev, int bar,
struct resource *r) struct resource *r)
{ {
...@@ -675,8 +631,8 @@ u64 of_translate_dma_address(struct device_node *dev, const __be32 *in_addr) ...@@ -675,8 +631,8 @@ u64 of_translate_dma_address(struct device_node *dev, const __be32 *in_addr)
} }
EXPORT_SYMBOL(of_translate_dma_address); EXPORT_SYMBOL(of_translate_dma_address);
const __be32 *of_get_address(struct device_node *dev, int index, u64 *size, const __be32 *__of_get_address(struct device_node *dev, int index, int bar_no,
unsigned int *flags) u64 *size, unsigned int *flags)
{ {
const __be32 *prop; const __be32 *prop;
unsigned int psize; unsigned int psize;
...@@ -689,6 +645,10 @@ const __be32 *of_get_address(struct device_node *dev, int index, u64 *size, ...@@ -689,6 +645,10 @@ const __be32 *of_get_address(struct device_node *dev, int index, u64 *size,
if (parent == NULL) if (parent == NULL)
return NULL; return NULL;
bus = of_match_bus(parent); bus = of_match_bus(parent);
if (strcmp(bus->name, "pci") && (bar_no >= 0)) {
of_node_put(parent);
return NULL;
}
bus->count_cells(dev, &na, &ns); bus->count_cells(dev, &na, &ns);
of_node_put(parent); of_node_put(parent);
if (!OF_CHECK_ADDR_COUNT(na)) if (!OF_CHECK_ADDR_COUNT(na))
...@@ -701,17 +661,21 @@ const __be32 *of_get_address(struct device_node *dev, int index, u64 *size, ...@@ -701,17 +661,21 @@ const __be32 *of_get_address(struct device_node *dev, int index, u64 *size,
psize /= 4; psize /= 4;
onesize = na + ns; onesize = na + ns;
for (i = 0; psize >= onesize; psize -= onesize, prop += onesize, i++) for (i = 0; psize >= onesize; psize -= onesize, prop += onesize, i++) {
if (i == index) { u32 val = be32_to_cpu(prop[0]);
/* PCI bus matches on BAR number instead of index */
if (((bar_no >= 0) && ((val & 0xff) == ((bar_no * 4) + PCI_BASE_ADDRESS_0))) ||
((index >= 0) && (i == index))) {
if (size) if (size)
*size = of_read_number(prop + na, ns); *size = of_read_number(prop + na, ns);
if (flags) if (flags)
*flags = bus->get_flags(prop); *flags = bus->get_flags(prop);
return prop; return prop;
} }
}
return NULL; return NULL;
} }
EXPORT_SYMBOL(of_get_address); EXPORT_SYMBOL(__of_get_address);
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)
......
...@@ -51,8 +51,8 @@ void __iomem *of_io_request_and_map(struct device_node *device, ...@@ -51,8 +51,8 @@ void __iomem *of_io_request_and_map(struct device_node *device,
* the address space flags too. The PCI version uses a BAR number * the address space flags too. The PCI version uses a BAR number
* instead of an absolute index * instead of an absolute index
*/ */
extern const __be32 *of_get_address(struct device_node *dev, int index, 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);
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);
...@@ -75,8 +75,8 @@ static inline u64 of_translate_address(struct device_node *np, ...@@ -75,8 +75,8 @@ static inline u64 of_translate_address(struct device_node *np,
return OF_BAD_ADDR; return OF_BAD_ADDR;
} }
static inline const __be32 *of_get_address(struct device_node *dev, int index, static inline const __be32 *__of_get_address(struct device_node *dev, int index, int bar_no,
u64 *size, unsigned int *flags) u64 *size, unsigned int *flags)
{ {
return NULL; return NULL;
} }
...@@ -125,8 +125,6 @@ static inline void __iomem *of_iomap(struct device_node *device, int index) ...@@ -125,8 +125,6 @@ static inline void __iomem *of_iomap(struct device_node *device, int index)
#define of_range_parser_init of_pci_range_parser_init #define of_range_parser_init of_pci_range_parser_init
#if defined(CONFIG_OF_ADDRESS) && defined(CONFIG_PCI) #if defined(CONFIG_OF_ADDRESS) && defined(CONFIG_PCI)
extern const __be32 *of_get_pci_address(struct device_node *dev, int bar_no,
u64 *size, unsigned int *flags);
extern int of_pci_address_to_resource(struct device_node *dev, int bar, extern int of_pci_address_to_resource(struct device_node *dev, int bar,
struct resource *r); struct resource *r);
extern int of_pci_range_to_resource(struct of_pci_range *range, extern int of_pci_range_to_resource(struct of_pci_range *range,
...@@ -139,11 +137,6 @@ static inline int of_pci_address_to_resource(struct device_node *dev, int bar, ...@@ -139,11 +137,6 @@ static inline int of_pci_address_to_resource(struct device_node *dev, int bar,
return -ENOSYS; return -ENOSYS;
} }
static inline const __be32 *of_get_pci_address(struct device_node *dev,
int bar_no, u64 *size, unsigned int *flags)
{
return NULL;
}
static inline int of_pci_range_to_resource(struct of_pci_range *range, static inline int of_pci_range_to_resource(struct of_pci_range *range,
struct device_node *np, struct device_node *np,
struct resource *res) struct resource *res)
...@@ -152,4 +145,16 @@ static inline int of_pci_range_to_resource(struct of_pci_range *range, ...@@ -152,4 +145,16 @@ static inline int of_pci_range_to_resource(struct of_pci_range *range,
} }
#endif /* CONFIG_OF_ADDRESS && CONFIG_PCI */ #endif /* CONFIG_OF_ADDRESS && CONFIG_PCI */
static inline const __be32 *of_get_address(struct device_node *dev, int index,
u64 *size, unsigned int *flags)
{
return __of_get_address(dev, index, -1, size, flags);
}
static inline const __be32 *of_get_pci_address(struct device_node *dev, int bar_no,
u64 *size, unsigned int *flags)
{
return __of_get_address(dev, -1, bar_no, size, flags);
}
#endif /* __OF_ADDRESS_H */ #endif /* __OF_ADDRESS_H */
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