kirkwood: add linux 3.10 support
[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 6b5917890ada1dc078ee64af2500cd6289fcf9bc Mon Sep 17 00:00:00 2001
2 From: Andrew Murray <Andrew.Murray@arm.com>
3 Date: Tue, 7 May 2013 16:31:12 +0100
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 ---
46 drivers/of/address.c | 67 ++++++++++++++++++++++++++++++++++++++++++++++
47 include/linux/of_address.h | 48 +++++++++++++++++++++++++++++++++
48 2 files changed, 115 insertions(+)
49
50 diff --git a/drivers/of/address.c b/drivers/of/address.c
51 index 7c8221d..b55c218 100644
52 --- a/drivers/of/address.c
53 +++ b/drivers/of/address.c
54 @@ -231,6 +231,73 @@ int of_pci_address_to_resource(struct device_node *dev, int bar,
55 return __of_address_to_resource(dev, addrp, size, flags, NULL, r);
56 }
57 EXPORT_SYMBOL_GPL(of_pci_address_to_resource);
58 +
59 +int of_pci_range_parser_init(struct of_pci_range_parser *parser,
60 + struct device_node *node)
61 +{
62 + const int na = 3, ns = 2;
63 + int rlen;
64 +
65 + parser->node = node;
66 + parser->pna = of_n_addr_cells(node);
67 + parser->np = parser->pna + na + ns;
68 +
69 + parser->range = of_get_property(node, "ranges", &rlen);
70 + if (parser->range == NULL)
71 + return -ENOENT;
72 +
73 + parser->end = parser->range + rlen / sizeof(__be32);
74 +
75 + return 0;
76 +}
77 +EXPORT_SYMBOL_GPL(of_pci_range_parser_init);
78 +
79 +struct of_pci_range *of_pci_range_parser_one(struct of_pci_range_parser *parser,
80 + struct of_pci_range *range)
81 +{
82 + const int na = 3, ns = 2;
83 +
84 + if (!range)
85 + return NULL;
86 +
87 + if (!parser->range || parser->range + parser->np > parser->end)
88 + return NULL;
89 +
90 + range->pci_space = parser->range[0];
91 + range->flags = of_bus_pci_get_flags(parser->range);
92 + range->pci_addr = of_read_number(parser->range + 1, ns);
93 + range->cpu_addr = of_translate_address(parser->node,
94 + parser->range + na);
95 + range->size = of_read_number(parser->range + parser->pna + na, ns);
96 +
97 + parser->range += parser->np;
98 +
99 + /* Now consume following elements while they are contiguous */
100 + while (parser->range + parser->np <= parser->end) {
101 + u32 flags, pci_space;
102 + u64 pci_addr, cpu_addr, size;
103 +
104 + pci_space = be32_to_cpup(parser->range);
105 + flags = of_bus_pci_get_flags(parser->range);
106 + pci_addr = of_read_number(parser->range + 1, ns);
107 + cpu_addr = of_translate_address(parser->node,
108 + parser->range + na);
109 + size = of_read_number(parser->range + parser->pna + na, ns);
110 +
111 + if (flags != range->flags)
112 + break;
113 + if (pci_addr != range->pci_addr + range->size ||
114 + cpu_addr != range->cpu_addr + range->size)
115 + break;
116 +
117 + range->size += size;
118 + parser->range += parser->np;
119 + }
120 +
121 + return range;
122 +}
123 +EXPORT_SYMBOL_GPL(of_pci_range_parser_one);
124 +
125 #endif /* CONFIG_PCI */
126
127 /*
128 diff --git a/include/linux/of_address.h b/include/linux/of_address.h
129 index 0506eb5..4c2e6f2 100644
130 --- a/include/linux/of_address.h
131 +++ b/include/linux/of_address.h
132 @@ -4,6 +4,36 @@
133 #include <linux/errno.h>
134 #include <linux/of.h>
135
136 +struct of_pci_range_parser {
137 + struct device_node *node;
138 + const __be32 *range;
139 + const __be32 *end;
140 + int np;
141 + int pna;
142 +};
143 +
144 +struct of_pci_range {
145 + u32 pci_space;
146 + u64 pci_addr;
147 + u64 cpu_addr;
148 + u64 size;
149 + u32 flags;
150 +};
151 +
152 +#define for_each_of_pci_range(parser, range) \
153 + for (; of_pci_range_parser_one(parser, range);)
154 +
155 +static inline void of_pci_range_to_resource(struct of_pci_range *range,
156 + struct device_node *np,
157 + struct resource *res)
158 +{
159 + res->flags = range->flags;
160 + res->start = range->cpu_addr;
161 + res->end = range->cpu_addr + range->size - 1;
162 + res->parent = res->child = res->sibling = NULL;
163 + res->name = np->full_name;
164 +}
165 +
166 #ifdef CONFIG_OF_ADDRESS
167 extern u64 of_translate_address(struct device_node *np, const __be32 *addr);
168 extern bool of_can_translate_address(struct device_node *dev);
169 @@ -27,6 +57,11 @@ static inline unsigned long pci_address_to_pio(phys_addr_t addr) { return -1; }
170 #define pci_address_to_pio pci_address_to_pio
171 #endif
172
173 +extern int of_pci_range_parser_init(struct of_pci_range_parser *parser,
174 + struct device_node *node);
175 +extern struct of_pci_range *of_pci_range_parser_one(
176 + struct of_pci_range_parser *parser,
177 + struct of_pci_range *range);
178 #else /* CONFIG_OF_ADDRESS */
179 #ifndef of_address_to_resource
180 static inline int of_address_to_resource(struct device_node *dev, int index,
181 @@ -53,6 +88,19 @@ static inline const __be32 *of_get_address(struct device_node *dev, int index,
182 {
183 return NULL;
184 }
185 +
186 +static inline int of_pci_range_parser_init(struct of_pci_range_parser *parser,
187 + struct device_node *node)
188 +{
189 + return -1;
190 +}
191 +
192 +static inline struct of_pci_range *of_pci_range_parser_one(
193 + struct of_pci_range_parser *parser,
194 + struct of_pci_range *range)
195 +{
196 + return NULL;
197 +}
198 #endif /* CONFIG_OF_ADDRESS */
199
200
201 --
202 1.8.4.rc1
203