1 From: Andrew Murray <Andrew.Murray@arm.com>
2 Subject: [PATCH] of/pci: Provide support for parsing PCI DT ranges property
4 This patch factors out common implementation patterns to reduce overall kernel
5 code and provide a means for host bridge drivers to directly obtain struct
6 resources from the DT's ranges property without relying on architecture specific
7 DT handling. This will make it easier to write archiecture independent host bridge
8 drivers and mitigate against further duplication of DT parsing code.
10 This patch can be used in the following way:
12 struct of_pci_range_parser parser;
13 struct of_pci_range range;
15 if (of_pci_range_parser_init(&parser, np))
16 ; //no ranges property
18 for_each_of_pci_range(&parser, &range) {
21 directly access properties of the address range, e.g.:
22 range.pci_space, range.pci_addr, range.cpu_addr,
23 range.size, range.flags
25 alternatively obtain a struct resource, e.g.:
27 of_pci_range_to_resource(&range, np, &res);
31 Additionally the implementation takes care of adjacent ranges and merges them
32 into a single range (as was the case with powerpc and microblaze).
34 Signed-off-by: Andrew Murray <Andrew.Murray@arm.com>
35 Signed-off-by: Liviu Dudau <Liviu.Dudau@arm.com>
36 Signed-off-by: Thomas Petazzoni <thomas.petazzoni@free-electrons.com>
37 Reviewed-by: Rob Herring <rob.herring@calxeda.com>
38 Tested-by: Thomas Petazzoni <thomas.petazzoni@free-electrons.com>
39 Tested-by: Linus Walleij <linus.walleij@linaro.org>
40 Tested-by: Jingoo Han <jg1.han@samsung.com>
41 Acked-by: Grant Likely <grant.likely@secretlab.ca>
42 Signed-off-by: Jason Cooper <jason@lakedaemon.net>
44 drivers/of/address.c | 67 ++++++++++++++++++++++++++++++++++++++++++++++
45 include/linux/of_address.h | 48 +++++++++++++++++++++++++++++++++
46 2 files changed, 115 insertions(+)
48 diff --git a/drivers/of/address.c b/drivers/of/address.c
49 index 04da786..fdd0636 100644
50 --- a/drivers/of/address.c
51 +++ b/drivers/of/address.c
52 @@ -227,6 +227,73 @@ int of_pci_address_to_resource(struct device_node *dev, int bar,
53 return __of_address_to_resource(dev, addrp, size, flags, NULL, r);
55 EXPORT_SYMBOL_GPL(of_pci_address_to_resource);
57 +int of_pci_range_parser_init(struct of_pci_range_parser *parser,
58 + struct device_node *node)
60 + const int na = 3, ns = 2;
63 + parser->node = node;
64 + parser->pna = of_n_addr_cells(node);
65 + parser->np = parser->pna + na + ns;
67 + parser->range = of_get_property(node, "ranges", &rlen);
68 + if (parser->range == NULL)
71 + parser->end = parser->range + rlen / sizeof(__be32);
75 +EXPORT_SYMBOL_GPL(of_pci_range_parser_init);
77 +struct of_pci_range *of_pci_range_parser_one(struct of_pci_range_parser *parser,
78 + struct of_pci_range *range)
80 + const int na = 3, ns = 2;
85 + if (!parser->range || parser->range + parser->np > parser->end)
88 + range->pci_space = parser->range[0];
89 + range->flags = of_bus_pci_get_flags(parser->range);
90 + range->pci_addr = of_read_number(parser->range + 1, ns);
91 + range->cpu_addr = of_translate_address(parser->node,
92 + parser->range + na);
93 + range->size = of_read_number(parser->range + parser->pna + na, ns);
95 + parser->range += parser->np;
97 + /* Now consume following elements while they are contiguous */
98 + while (parser->range + parser->np <= parser->end) {
99 + u32 flags, pci_space;
100 + u64 pci_addr, cpu_addr, size;
102 + pci_space = be32_to_cpup(parser->range);
103 + flags = of_bus_pci_get_flags(parser->range);
104 + pci_addr = of_read_number(parser->range + 1, ns);
105 + cpu_addr = of_translate_address(parser->node,
106 + parser->range + na);
107 + size = of_read_number(parser->range + parser->pna + na, ns);
109 + if (flags != range->flags)
111 + if (pci_addr != range->pci_addr + range->size ||
112 + cpu_addr != range->cpu_addr + range->size)
115 + range->size += size;
116 + parser->range += parser->np;
121 +EXPORT_SYMBOL_GPL(of_pci_range_parser_one);
123 #endif /* CONFIG_PCI */
126 diff --git a/include/linux/of_address.h b/include/linux/of_address.h
127 index 0506eb5..4c2e6f2 100644
128 --- a/include/linux/of_address.h
129 +++ b/include/linux/of_address.h
131 #include <linux/errno.h>
132 #include <linux/of.h>
134 +struct of_pci_range_parser {
135 + struct device_node *node;
136 + const __be32 *range;
142 +struct of_pci_range {
150 +#define for_each_of_pci_range(parser, range) \
151 + for (; of_pci_range_parser_one(parser, range);)
153 +static inline void of_pci_range_to_resource(struct of_pci_range *range,
154 + struct device_node *np,
155 + struct resource *res)
157 + res->flags = range->flags;
158 + res->start = range->cpu_addr;
159 + res->end = range->cpu_addr + range->size - 1;
160 + res->parent = res->child = res->sibling = NULL;
161 + res->name = np->full_name;
164 #ifdef CONFIG_OF_ADDRESS
165 extern u64 of_translate_address(struct device_node *np, const __be32 *addr);
166 extern bool of_can_translate_address(struct device_node *dev);
167 @@ -27,6 +57,11 @@ static inline unsigned long pci_address_to_pio(phys_addr_t addr) { return -1; }
168 #define pci_address_to_pio pci_address_to_pio
171 +extern int of_pci_range_parser_init(struct of_pci_range_parser *parser,
172 + struct device_node *node);
173 +extern struct of_pci_range *of_pci_range_parser_one(
174 + struct of_pci_range_parser *parser,
175 + struct of_pci_range *range);
176 #else /* CONFIG_OF_ADDRESS */
177 #ifndef of_address_to_resource
178 static inline int of_address_to_resource(struct device_node *dev, int index,
179 @@ -53,6 +88,19 @@ static inline const __be32 *of_get_address(struct device_node *dev, int index,
184 +static inline int of_pci_range_parser_init(struct of_pci_range_parser *parser,
185 + struct device_node *node)
190 +static inline struct of_pci_range *of_pci_range_parser_one(
191 + struct of_pci_range_parser *parser,
192 + struct of_pci_range *range)
196 #endif /* CONFIG_OF_ADDRESS */