aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/of/address.c
diff options
context:
space:
mode:
authorGrant Likely <grant.likely@secretlab.ca>2010-06-08 09:48:10 -0400
committerGrant Likely <grant.likely@secretlab.ca>2010-07-05 18:14:26 -0400
commitdbbdee94734bf6f1db7af42008a53655e77cab8f (patch)
treec0f571b0ab57a6483bc07e21e3b888e253d699ea /drivers/of/address.c
parent1f5bef30cf6c66f097ea5dfc580a41924df888d1 (diff)
of/address: Merge all of the bus translation code
Microblaze and PowerPC share a large chunk of code for translating OF device tree data into usable addresses. Differences between the two consist of cosmetic differences, and the addition of dma-ranges support code to powerpc but not microblaze. This patch moves the powerpc version into common code and applies many of the cosmetic (non-functional) changes from the microblaze version. Signed-off-by: Grant Likely <grant.likely@secretlab.ca> Acked-by: Benjamin Herrenschmidt <benh@kernel.crashing.org> CC: Michal Simek <monstr@monstr.eu> CC: Wolfram Sang <w.sang@pengutronix.de> CC: Stephen Rothwell <sfr@canb.auug.org.au>
Diffstat (limited to 'drivers/of/address.c')
-rw-r--r--drivers/of/address.c517
1 files changed, 514 insertions, 3 deletions
diff --git a/drivers/of/address.c b/drivers/of/address.c
index c3819550f907..2a905d560c1c 100644
--- a/drivers/of/address.c
+++ b/drivers/of/address.c
@@ -1,11 +1,522 @@
1 1
2#include <linux/io.h> 2#include <linux/io.h>
3#include <linux/ioport.h> 3#include <linux/ioport.h>
4#include <linux/module.h>
4#include <linux/of_address.h> 5#include <linux/of_address.h>
6#include <linux/pci_regs.h>
7#include <linux/string.h>
5 8
6int __of_address_to_resource(struct device_node *dev, const u32 *addrp, 9/* Max address size we deal with */
7 u64 size, unsigned int flags, 10#define OF_MAX_ADDR_CELLS 4
8 struct resource *r) 11#define OF_CHECK_COUNTS(na, ns) ((na) > 0 && (na) <= OF_MAX_ADDR_CELLS && \
12 (ns) > 0)
13
14static struct of_bus *of_match_bus(struct device_node *np);
15static int __of_address_to_resource(struct device_node *dev, const u32 *addrp,
16 u64 size, unsigned int flags,
17 struct resource *r);
18
19/* Debug utility */
20#ifdef DEBUG
21static void of_dump_addr(const char *s, const u32 *addr, int na)
22{
23 printk(KERN_DEBUG "%s", s);
24 while (na--)
25 printk(" %08x", *(addr++));
26 printk("\n");
27}
28#else
29static void of_dump_addr(const char *s, const u32 *addr, int na) { }
30#endif
31
32/* Callbacks for bus specific translators */
33struct of_bus {
34 const char *name;
35 const char *addresses;
36 int (*match)(struct device_node *parent);
37 void (*count_cells)(struct device_node *child,
38 int *addrc, int *sizec);
39 u64 (*map)(u32 *addr, const u32 *range,
40 int na, int ns, int pna);
41 int (*translate)(u32 *addr, u64 offset, int na);
42 unsigned int (*get_flags)(const u32 *addr);
43};
44
45/*
46 * Default translator (generic bus)
47 */
48
49static void of_bus_default_count_cells(struct device_node *dev,
50 int *addrc, int *sizec)
51{
52 if (addrc)
53 *addrc = of_n_addr_cells(dev);
54 if (sizec)
55 *sizec = of_n_size_cells(dev);
56}
57
58static u64 of_bus_default_map(u32 *addr, const u32 *range,
59 int na, int ns, int pna)
60{
61 u64 cp, s, da;
62
63 cp = of_read_number(range, na);
64 s = of_read_number(range + na + pna, ns);
65 da = of_read_number(addr, na);
66
67 pr_debug("OF: default map, cp=%llx, s=%llx, da=%llx\n",
68 (unsigned long long)cp, (unsigned long long)s,
69 (unsigned long long)da);
70
71 if (da < cp || da >= (cp + s))
72 return OF_BAD_ADDR;
73 return da - cp;
74}
75
76static int of_bus_default_translate(u32 *addr, u64 offset, int na)
77{
78 u64 a = of_read_number(addr, na);
79 memset(addr, 0, na * 4);
80 a += offset;
81 if (na > 1)
82 addr[na - 2] = a >> 32;
83 addr[na - 1] = a & 0xffffffffu;
84
85 return 0;
86}
87
88static unsigned int of_bus_default_get_flags(const u32 *addr)
89{
90 return IORESOURCE_MEM;
91}
92
93#ifdef CONFIG_PCI
94/*
95 * PCI bus specific translator
96 */
97
98static int of_bus_pci_match(struct device_node *np)
99{
100 /* "vci" is for the /chaos bridge on 1st-gen PCI powermacs */
101 return !strcmp(np->type, "pci") || !strcmp(np->type, "vci");
102}
103
104static void of_bus_pci_count_cells(struct device_node *np,
105 int *addrc, int *sizec)
106{
107 if (addrc)
108 *addrc = 3;
109 if (sizec)
110 *sizec = 2;
111}
112
113static unsigned int of_bus_pci_get_flags(const u32 *addr)
114{
115 unsigned int flags = 0;
116 u32 w = addr[0];
117
118 switch((w >> 24) & 0x03) {
119 case 0x01:
120 flags |= IORESOURCE_IO;
121 break;
122 case 0x02: /* 32 bits */
123 case 0x03: /* 64 bits */
124 flags |= IORESOURCE_MEM;
125 break;
126 }
127 if (w & 0x40000000)
128 flags |= IORESOURCE_PREFETCH;
129 return flags;
130}
131
132static u64 of_bus_pci_map(u32 *addr, const u32 *range, int na, int ns, int pna)
133{
134 u64 cp, s, da;
135 unsigned int af, rf;
136
137 af = of_bus_pci_get_flags(addr);
138 rf = of_bus_pci_get_flags(range);
139
140 /* Check address type match */
141 if ((af ^ rf) & (IORESOURCE_MEM | IORESOURCE_IO))
142 return OF_BAD_ADDR;
143
144 /* Read address values, skipping high cell */
145 cp = of_read_number(range + 1, na - 1);
146 s = of_read_number(range + na + pna, ns);
147 da = of_read_number(addr + 1, na - 1);
148
149 pr_debug("OF: PCI map, cp=%llx, s=%llx, da=%llx\n",
150 (unsigned long long)cp, (unsigned long long)s,
151 (unsigned long long)da);
152
153 if (da < cp || da >= (cp + s))
154 return OF_BAD_ADDR;
155 return da - cp;
156}
157
158static int of_bus_pci_translate(u32 *addr, u64 offset, int na)
159{
160 return of_bus_default_translate(addr + 1, offset, na - 1);
161}
162
163const u32 *of_get_pci_address(struct device_node *dev, int bar_no, u64 *size,
164 unsigned int *flags)
165{
166 const u32 *prop;
167 unsigned int psize;
168 struct device_node *parent;
169 struct of_bus *bus;
170 int onesize, i, na, ns;
171
172 /* Get parent & match bus type */
173 parent = of_get_parent(dev);
174 if (parent == NULL)
175 return NULL;
176 bus = of_match_bus(parent);
177 if (strcmp(bus->name, "pci")) {
178 of_node_put(parent);
179 return NULL;
180 }
181 bus->count_cells(dev, &na, &ns);
182 of_node_put(parent);
183 if (!OF_CHECK_COUNTS(na, ns))
184 return NULL;
185
186 /* Get "reg" or "assigned-addresses" property */
187 prop = of_get_property(dev, bus->addresses, &psize);
188 if (prop == NULL)
189 return NULL;
190 psize /= 4;
191
192 onesize = na + ns;
193 for (i = 0; psize >= onesize; psize -= onesize, prop += onesize, i++)
194 if ((prop[0] & 0xff) == ((bar_no * 4) + PCI_BASE_ADDRESS_0)) {
195 if (size)
196 *size = of_read_number(prop + na, ns);
197 if (flags)
198 *flags = bus->get_flags(prop);
199 return prop;
200 }
201 return NULL;
202}
203EXPORT_SYMBOL(of_get_pci_address);
204
205int of_pci_address_to_resource(struct device_node *dev, int bar,
206 struct resource *r)
207{
208 const u32 *addrp;
209 u64 size;
210 unsigned int flags;
211
212 addrp = of_get_pci_address(dev, bar, &size, &flags);
213 if (addrp == NULL)
214 return -EINVAL;
215 return __of_address_to_resource(dev, addrp, size, flags, r);
216}
217EXPORT_SYMBOL_GPL(of_pci_address_to_resource);
218#endif /* CONFIG_PCI */
219
220/*
221 * ISA bus specific translator
222 */
223
224static int of_bus_isa_match(struct device_node *np)
225{
226 return !strcmp(np->name, "isa");
227}
228
229static void of_bus_isa_count_cells(struct device_node *child,
230 int *addrc, int *sizec)
231{
232 if (addrc)
233 *addrc = 2;
234 if (sizec)
235 *sizec = 1;
236}
237
238static u64 of_bus_isa_map(u32 *addr, const u32 *range, int na, int ns, int pna)
239{
240 u64 cp, s, da;
241
242 /* Check address type match */
243 if ((addr[0] ^ range[0]) & 0x00000001)
244 return OF_BAD_ADDR;
245
246 /* Read address values, skipping high cell */
247 cp = of_read_number(range + 1, na - 1);
248 s = of_read_number(range + na + pna, ns);
249 da = of_read_number(addr + 1, na - 1);
250
251 pr_debug("OF: ISA map, cp=%llx, s=%llx, da=%llx\n",
252 (unsigned long long)cp, (unsigned long long)s,
253 (unsigned long long)da);
254
255 if (da < cp || da >= (cp + s))
256 return OF_BAD_ADDR;
257 return da - cp;
258}
259
260static int of_bus_isa_translate(u32 *addr, u64 offset, int na)
261{
262 return of_bus_default_translate(addr + 1, offset, na - 1);
263}
264
265static unsigned int of_bus_isa_get_flags(const u32 *addr)
266{
267 unsigned int flags = 0;
268 u32 w = addr[0];
269
270 if (w & 1)
271 flags |= IORESOURCE_IO;
272 else
273 flags |= IORESOURCE_MEM;
274 return flags;
275}
276
277/*
278 * Array of bus specific translators
279 */
280
281static struct of_bus of_busses[] = {
282#ifdef CONFIG_PCI
283 /* PCI */
284 {
285 .name = "pci",
286 .addresses = "assigned-addresses",
287 .match = of_bus_pci_match,
288 .count_cells = of_bus_pci_count_cells,
289 .map = of_bus_pci_map,
290 .translate = of_bus_pci_translate,
291 .get_flags = of_bus_pci_get_flags,
292 },
293#endif /* CONFIG_PCI */
294 /* ISA */
295 {
296 .name = "isa",
297 .addresses = "reg",
298 .match = of_bus_isa_match,
299 .count_cells = of_bus_isa_count_cells,
300 .map = of_bus_isa_map,
301 .translate = of_bus_isa_translate,
302 .get_flags = of_bus_isa_get_flags,
303 },
304 /* Default */
305 {
306 .name = "default",
307 .addresses = "reg",
308 .match = NULL,
309 .count_cells = of_bus_default_count_cells,
310 .map = of_bus_default_map,
311 .translate = of_bus_default_translate,
312 .get_flags = of_bus_default_get_flags,
313 },
314};
315
316static struct of_bus *of_match_bus(struct device_node *np)
317{
318 int i;
319
320 for (i = 0; i < ARRAY_SIZE(of_busses); i++)
321 if (!of_busses[i].match || of_busses[i].match(np))
322 return &of_busses[i];
323 BUG();
324 return NULL;
325}
326
327static int of_translate_one(struct device_node *parent, struct of_bus *bus,
328 struct of_bus *pbus, u32 *addr,
329 int na, int ns, int pna, const char *rprop)
330{
331 const u32 *ranges;
332 unsigned int rlen;
333 int rone;
334 u64 offset = OF_BAD_ADDR;
335
336 /* Normally, an absence of a "ranges" property means we are
337 * crossing a non-translatable boundary, and thus the addresses
338 * below the current not cannot be converted to CPU physical ones.
339 * Unfortunately, while this is very clear in the spec, it's not
340 * what Apple understood, and they do have things like /uni-n or
341 * /ht nodes with no "ranges" property and a lot of perfectly
342 * useable mapped devices below them. Thus we treat the absence of
343 * "ranges" as equivalent to an empty "ranges" property which means
344 * a 1:1 translation at that level. It's up to the caller not to try
345 * to translate addresses that aren't supposed to be translated in
346 * the first place. --BenH.
347 */
348 ranges = of_get_property(parent, rprop, &rlen);
349 if (ranges == NULL || rlen == 0) {
350 offset = of_read_number(addr, na);
351 memset(addr, 0, pna * 4);
352 pr_debug("OF: no ranges, 1:1 translation\n");
353 goto finish;
354 }
355
356 pr_debug("OF: walking ranges...\n");
357
358 /* Now walk through the ranges */
359 rlen /= 4;
360 rone = na + pna + ns;
361 for (; rlen >= rone; rlen -= rone, ranges += rone) {
362 offset = bus->map(addr, ranges, na, ns, pna);
363 if (offset != OF_BAD_ADDR)
364 break;
365 }
366 if (offset == OF_BAD_ADDR) {
367 pr_debug("OF: not found !\n");
368 return 1;
369 }
370 memcpy(addr, ranges + na, 4 * pna);
371
372 finish:
373 of_dump_addr("OF: parent translation for:", addr, pna);
374 pr_debug("OF: with offset: %llx\n", (unsigned long long)offset);
375
376 /* Translate it into parent bus space */
377 return pbus->translate(addr, offset, pna);
378}
379
380/*
381 * Translate an address from the device-tree into a CPU physical address,
382 * this walks up the tree and applies the various bus mappings on the
383 * way.
384 *
385 * Note: We consider that crossing any level with #size-cells == 0 to mean
386 * that translation is impossible (that is we are not dealing with a value
387 * that can be mapped to a cpu physical address). This is not really specified
388 * that way, but this is traditionally the way IBM at least do things
389 */
390u64 __of_translate_address(struct device_node *dev, const u32 *in_addr,
391 const char *rprop)
392{
393 struct device_node *parent = NULL;
394 struct of_bus *bus, *pbus;
395 u32 addr[OF_MAX_ADDR_CELLS];
396 int na, ns, pna, pns;
397 u64 result = OF_BAD_ADDR;
398
399 pr_debug("OF: ** translation for device %s **\n", dev->full_name);
400
401 /* Increase refcount at current level */
402 of_node_get(dev);
403
404 /* Get parent & match bus type */
405 parent = of_get_parent(dev);
406 if (parent == NULL)
407 goto bail;
408 bus = of_match_bus(parent);
409
410 /* Cound address cells & copy address locally */
411 bus->count_cells(dev, &na, &ns);
412 if (!OF_CHECK_COUNTS(na, ns)) {
413 printk(KERN_ERR "prom_parse: Bad cell count for %s\n",
414 dev->full_name);
415 goto bail;
416 }
417 memcpy(addr, in_addr, na * 4);
418
419 pr_debug("OF: bus is %s (na=%d, ns=%d) on %s\n",
420 bus->name, na, ns, parent->full_name);
421 of_dump_addr("OF: translating address:", addr, na);
422
423 /* Translate */
424 for (;;) {
425 /* Switch to parent bus */
426 of_node_put(dev);
427 dev = parent;
428 parent = of_get_parent(dev);
429
430 /* If root, we have finished */
431 if (parent == NULL) {
432 pr_debug("OF: reached root node\n");
433 result = of_read_number(addr, na);
434 break;
435 }
436
437 /* Get new parent bus and counts */
438 pbus = of_match_bus(parent);
439 pbus->count_cells(dev, &pna, &pns);
440 if (!OF_CHECK_COUNTS(pna, pns)) {
441 printk(KERN_ERR "prom_parse: Bad cell count for %s\n",
442 dev->full_name);
443 break;
444 }
445
446 pr_debug("OF: parent bus is %s (na=%d, ns=%d) on %s\n",
447 pbus->name, pna, pns, parent->full_name);
448
449 /* Apply bus translation */
450 if (of_translate_one(dev, bus, pbus, addr, na, ns, pna, rprop))
451 break;
452
453 /* Complete the move up one level */
454 na = pna;
455 ns = pns;
456 bus = pbus;
457
458 of_dump_addr("OF: one level translation:", addr, na);
459 }
460 bail:
461 of_node_put(parent);
462 of_node_put(dev);
463
464 return result;
465}
466
467u64 of_translate_address(struct device_node *dev, const u32 *in_addr)
468{
469 return __of_translate_address(dev, in_addr, "ranges");
470}
471EXPORT_SYMBOL(of_translate_address);
472
473u64 of_translate_dma_address(struct device_node *dev, const u32 *in_addr)
474{
475 return __of_translate_address(dev, in_addr, "dma-ranges");
476}
477EXPORT_SYMBOL(of_translate_dma_address);
478
479const u32 *of_get_address(struct device_node *dev, int index, u64 *size,
480 unsigned int *flags)
481{
482 const u32 *prop;
483 unsigned int psize;
484 struct device_node *parent;
485 struct of_bus *bus;
486 int onesize, i, na, ns;
487
488 /* Get parent & match bus type */
489 parent = of_get_parent(dev);
490 if (parent == NULL)
491 return NULL;
492 bus = of_match_bus(parent);
493 bus->count_cells(dev, &na, &ns);
494 of_node_put(parent);
495 if (!OF_CHECK_COUNTS(na, ns))
496 return NULL;
497
498 /* Get "reg" or "assigned-addresses" property */
499 prop = of_get_property(dev, bus->addresses, &psize);
500 if (prop == NULL)
501 return NULL;
502 psize /= 4;
503
504 onesize = na + ns;
505 for (i = 0; psize >= onesize; psize -= onesize, prop += onesize, i++)
506 if (i == index) {
507 if (size)
508 *size = of_read_number(prop + na, ns);
509 if (flags)
510 *flags = bus->get_flags(prop);
511 return prop;
512 }
513 return NULL;
514}
515EXPORT_SYMBOL(of_get_address);
516
517static int __of_address_to_resource(struct device_node *dev, const u32 *addrp,
518 u64 size, unsigned int flags,
519 struct resource *r)
9{ 520{
10 u64 taddr; 521 u64 taddr;
11 522