88100d61962d7cf720ebef1a2b4ec20f8bfaf1c3
[openwrt/svn-archive/archive.git] / target / linux / kirkwood / patches-3.10 / 0001-of-pci-Provide-support-for-parsing-PCI-DT-ranges-pro.patch
1 From 3c72277cf2c10e3b7065541b3588f21138efeba2 Mon Sep 17 00:00:00 2001
2 From: Andrew Murray <Andrew.Murray@arm.com>
3 Date: Thu, 16 May 2013 17:55:17 +0200
4 Subject: [PATCH 01/29] of/pci: Provide support for parsing PCI DT ranges
5 property
6
7 This patch factors out common implementation patterns to reduce overall kernel
8 code and provide a means for host bridge drivers to directly obtain struct
9 resources from the DT's ranges property without relying on architecture specific
10 DT handling. This will make it easier to write archiecture independent host bridge
11 drivers and mitigate against further duplication of DT parsing code.
12
13 This patch can be used in the following way:
14
15 struct of_pci_range_parser parser;
16 struct of_pci_range range;
17
18 if (of_pci_range_parser_init(&parser, np))
19 ; //no ranges property
20
21 for_each_of_pci_range(&parser, &range) {
22
23 /*
24 directly access properties of the address range, e.g.:
25 range.pci_space, range.pci_addr, range.cpu_addr,
26 range.size, range.flags
27
28 alternatively obtain a struct resource, e.g.:
29 struct resource res;
30 of_pci_range_to_resource(&range, np, &res);
31 */
32 }
33
34 Additionally the implementation takes care of adjacent ranges and merges them
35 into a single range (as was the case with powerpc and microblaze).
36
37 Signed-off-by: Andrew Murray <Andrew.Murray@arm.com>
38 Signed-off-by: Liviu Dudau <Liviu.Dudau@arm.com>
39 Signed-off-by: Thomas Petazzoni <thomas.petazzoni@free-electrons.com>
40 Reviewed-by: Rob Herring <rob.herring@calxeda.com>
41 Tested-by: Thomas Petazzoni <thomas.petazzoni@free-electrons.com>
42 Tested-by: Linus Walleij <linus.walleij@linaro.org>
43 Tested-by: Jingoo Han <jg1.han@samsung.com>
44 Acked-by: Grant Likely <grant.likely@secretlab.ca>
45 Signed-off-by: Jason Cooper <jason@lakedaemon.net>
46 ---
47 drivers/of/address.c | 67 ++++++++++++++++++++++++++++++++++++++++++++++
48 include/linux/of_address.h | 48 +++++++++++++++++++++++++++++++++
49 2 files changed, 115 insertions(+)
50
51 --- a/drivers/of/address.c
52 +++ b/drivers/of/address.c
53 @@ -223,6 +223,73 @@ int of_pci_address_to_resource(struct de
54 return __of_address_to_resource(dev, addrp, size, flags, NULL, r);
55 }
56 EXPORT_SYMBOL_GPL(of_pci_address_to_resource);
57 +
58 +int of_pci_range_parser_init(struct of_pci_range_parser *parser,
59 + struct device_node *node)
60 +{
61 + const int na = 3, ns = 2;
62 + int rlen;
63 +
64 + parser->node = node;
65 + parser->pna = of_n_addr_cells(node);
66 + parser->np = parser->pna + na + ns;
67 +
68 + parser->range = of_get_property(node, "ranges", &rlen);
69 + if (parser->range == NULL)
70 + return -ENOENT;
71 +
72 + parser->end = parser->range + rlen / sizeof(__be32);
73 +
74 + return 0;
75 +}
76 +EXPORT_SYMBOL_GPL(of_pci_range_parser_init);
77 +
78 +struct of_pci_range *of_pci_range_parser_one(struct of_pci_range_parser *parser,
79 + struct of_pci_range *range)
80 +{
81 + const int na = 3, ns = 2;
82 +
83 + if (!range)
84 + return NULL;
85 +
86 + if (!parser->range || parser->range + parser->np > parser->end)
87 + return NULL;
88 +
89 + range->pci_space = parser->range[0];
90 + range->flags = of_bus_pci_get_flags(parser->range);
91 + range->pci_addr = of_read_number(parser->range + 1, ns);
92 + range->cpu_addr = of_translate_address(parser->node,
93 + parser->range + na);
94 + range->size = of_read_number(parser->range + parser->pna + na, ns);
95 +
96 + parser->range += parser->np;
97 +
98 + /* Now consume following elements while they are contiguous */
99 + while (parser->range + parser->np <= parser->end) {
100 + u32 flags, pci_space;
101 + u64 pci_addr, cpu_addr, size;
102 +
103 + pci_space = be32_to_cpup(parser->range);
104 + flags = of_bus_pci_get_flags(parser->range);
105 + pci_addr = of_read_number(parser->range + 1, ns);
106 + cpu_addr = of_translate_address(parser->node,
107 + parser->range + na);
108 + size = of_read_number(parser->range + parser->pna + na, ns);
109 +
110 + if (flags != range->flags)
111 + break;
112 + if (pci_addr != range->pci_addr + range->size ||
113 + cpu_addr != range->cpu_addr + range->size)
114 + break;
115 +
116 + range->size += size;
117 + parser->range += parser->np;
118 + }
119 +
120 + return range;
121 +}
122 +EXPORT_SYMBOL_GPL(of_pci_range_parser_one);
123 +
124 #endif /* CONFIG_PCI */
125
126 /*
127 --- a/include/linux/of_address.h
128 +++ b/include/linux/of_address.h
129 @@ -4,6 +4,36 @@
130 #include <linux/errno.h>
131 #include <linux/of.h>
132
133 +struct of_pci_range_parser {
134 + struct device_node *node;
135 + const __be32 *range;
136 + const __be32 *end;
137 + int np;
138 + int pna;
139 +};
140 +
141 +struct of_pci_range {
142 + u32 pci_space;
143 + u64 pci_addr;
144 + u64 cpu_addr;
145 + u64 size;
146 + u32 flags;
147 +};
148 +
149 +#define for_each_of_pci_range(parser, range) \
150 + for (; of_pci_range_parser_one(parser, range);)
151 +
152 +static inline void of_pci_range_to_resource(struct of_pci_range *range,
153 + struct device_node *np,
154 + struct resource *res)
155 +{
156 + res->flags = range->flags;
157 + res->start = range->cpu_addr;
158 + res->end = range->cpu_addr + range->size - 1;
159 + res->parent = res->child = res->sibling = NULL;
160 + res->name = np->full_name;
161 +}
162 +
163 #ifdef CONFIG_OF_ADDRESS
164 extern u64 of_translate_address(struct device_node *np, const __be32 *addr);
165 extern bool of_can_translate_address(struct device_node *dev);
166 @@ -27,6 +57,11 @@ static inline unsigned long pci_address_
167 #define pci_address_to_pio pci_address_to_pio
168 #endif
169
170 +extern int of_pci_range_parser_init(struct of_pci_range_parser *parser,
171 + struct device_node *node);
172 +extern struct of_pci_range *of_pci_range_parser_one(
173 + struct of_pci_range_parser *parser,
174 + struct of_pci_range *range);
175 #else /* CONFIG_OF_ADDRESS */
176 #ifndef of_address_to_resource
177 static inline int of_address_to_resource(struct device_node *dev, int index,
178 @@ -53,6 +88,19 @@ static inline const __be32 *of_get_addre
179 {
180 return NULL;
181 }
182 +
183 +static inline int of_pci_range_parser_init(struct of_pci_range_parser *parser,
184 + struct device_node *node)
185 +{
186 + return -1;
187 +}
188 +
189 +static inline struct of_pci_range *of_pci_range_parser_one(
190 + struct of_pci_range_parser *parser,
191 + struct of_pci_range *range)
192 +{
193 + return NULL;
194 +}
195 #endif /* CONFIG_OF_ADDRESS */
196
197