aboutsummaryrefslogtreecommitdiffstats
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
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>
-rw-r--r--arch/microblaze/include/asm/prom.h4
-rw-r--r--arch/microblaze/kernel/prom_parse.c489
-rw-r--r--arch/powerpc/include/asm/prom.h4
-rw-r--r--arch/powerpc/kernel/prom_parse.c515
-rw-r--r--drivers/of/address.c517
-rw-r--r--include/linux/of_address.h4
6 files changed, 515 insertions, 1018 deletions
diff --git a/arch/microblaze/include/asm/prom.h b/arch/microblaze/include/asm/prom.h
index 4e94c0706c50..cb9c3dd9a23b 100644
--- a/arch/microblaze/include/asm/prom.h
+++ b/arch/microblaze/include/asm/prom.h
@@ -52,10 +52,6 @@ extern void pci_create_OF_bus_map(void);
52 * OF address retreival & translation 52 * OF address retreival & translation
53 */ 53 */
54 54
55/* Translate an OF address block into a CPU physical address
56 */
57extern u64 of_translate_address(struct device_node *np, const u32 *addr);
58
59/* Extract an address from a device, returns the region size and 55/* Extract an address from a device, returns the region size and
60 * the address space flags too. The PCI version uses a BAR number 56 * the address space flags too. The PCI version uses a BAR number
61 * instead of an absolute index 57 * instead of an absolute index
diff --git a/arch/microblaze/kernel/prom_parse.c b/arch/microblaze/kernel/prom_parse.c
index 2f9cdd26ca12..d33ba17601fa 100644
--- a/arch/microblaze/kernel/prom_parse.c
+++ b/arch/microblaze/kernel/prom_parse.c
@@ -10,213 +10,7 @@
10#include <asm/prom.h> 10#include <asm/prom.h>
11#include <asm/pci-bridge.h> 11#include <asm/pci-bridge.h>
12 12
13#define PRu64 "%llx"
14
15/* Max address size we deal with */
16#define OF_MAX_ADDR_CELLS 4
17#define OF_CHECK_COUNTS(na, ns) ((na) > 0 && (na) <= OF_MAX_ADDR_CELLS && \
18 (ns) > 0)
19
20static struct of_bus *of_match_bus(struct device_node *np);
21
22/* Debug utility */
23#ifdef DEBUG
24static void of_dump_addr(const char *s, const u32 *addr, int na)
25{
26 printk(KERN_INFO "%s", s);
27 while (na--)
28 printk(KERN_INFO " %08x", *(addr++));
29 printk(KERN_INFO "\n");
30}
31#else
32static void of_dump_addr(const char *s, const u32 *addr, int na) { }
33#endif
34
35/* Callbacks for bus specific translators */
36struct of_bus {
37 const char *name;
38 const char *addresses;
39 int (*match)(struct device_node *parent);
40 void (*count_cells)(struct device_node *child,
41 int *addrc, int *sizec);
42 u64 (*map)(u32 *addr, const u32 *range,
43 int na, int ns, int pna);
44 int (*translate)(u32 *addr, u64 offset, int na);
45 unsigned int (*get_flags)(const u32 *addr);
46};
47
48/*
49 * Default translator (generic bus)
50 */
51
52static void of_bus_default_count_cells(struct device_node *dev,
53 int *addrc, int *sizec)
54{
55 if (addrc)
56 *addrc = of_n_addr_cells(dev);
57 if (sizec)
58 *sizec = of_n_size_cells(dev);
59}
60
61static u64 of_bus_default_map(u32 *addr, const u32 *range,
62 int na, int ns, int pna)
63{
64 u64 cp, s, da;
65
66 cp = of_read_number(range, na);
67 s = of_read_number(range + na + pna, ns);
68 da = of_read_number(addr, na);
69
70 pr_debug("OF: default map, cp="PRu64", s="PRu64", da="PRu64"\n",
71 cp, s, da);
72
73 if (da < cp || da >= (cp + s))
74 return OF_BAD_ADDR;
75 return da - cp;
76}
77
78static int of_bus_default_translate(u32 *addr, u64 offset, int na)
79{
80 u64 a = of_read_number(addr, na);
81 memset(addr, 0, na * 4);
82 a += offset;
83 if (na > 1)
84 addr[na - 2] = a >> 32;
85 addr[na - 1] = a & 0xffffffffu;
86
87 return 0;
88}
89
90static unsigned int of_bus_default_get_flags(const u32 *addr)
91{
92 return IORESOURCE_MEM;
93}
94
95#ifdef CONFIG_PCI 13#ifdef CONFIG_PCI
96/*
97 * PCI bus specific translator
98 */
99
100static int of_bus_pci_match(struct device_node *np)
101{
102 /* "vci" is for the /chaos bridge on 1st-gen PCI powermacs */
103 return !strcmp(np->type, "pci") || !strcmp(np->type, "vci");
104}
105
106static void of_bus_pci_count_cells(struct device_node *np,
107 int *addrc, int *sizec)
108{
109 if (addrc)
110 *addrc = 3;
111 if (sizec)
112 *sizec = 2;
113}
114
115static u64 of_bus_pci_map(u32 *addr, const u32 *range, int na, int ns, int pna)
116{
117 u64 cp, s, da;
118
119 /* Check address type match */
120 if ((addr[0] ^ range[0]) & 0x03000000)
121 return OF_BAD_ADDR;
122
123 /* Read address values, skipping high cell */
124 cp = of_read_number(range + 1, na - 1);
125 s = of_read_number(range + na + pna, ns);
126 da = of_read_number(addr + 1, na - 1);
127
128 pr_debug("OF: PCI map, cp="PRu64", s="PRu64", da="PRu64"\n", cp, s, da);
129
130 if (da < cp || da >= (cp + s))
131 return OF_BAD_ADDR;
132 return da - cp;
133}
134
135static int of_bus_pci_translate(u32 *addr, u64 offset, int na)
136{
137 return of_bus_default_translate(addr + 1, offset, na - 1);
138}
139
140static unsigned int of_bus_pci_get_flags(const u32 *addr)
141{
142 unsigned int flags = 0;
143 u32 w = addr[0];
144
145 switch ((w >> 24) & 0x03) {
146 case 0x01:
147 flags |= IORESOURCE_IO;
148 break;
149 case 0x02: /* 32 bits */
150 case 0x03: /* 64 bits */
151 flags |= IORESOURCE_MEM;
152 break;
153 }
154 if (w & 0x40000000)
155 flags |= IORESOURCE_PREFETCH;
156 return flags;
157}
158
159const u32 *of_get_pci_address(struct device_node *dev, int bar_no, u64 *size,
160 unsigned int *flags)
161{
162 const u32 *prop;
163 unsigned int psize;
164 struct device_node *parent;
165 struct of_bus *bus;
166 int onesize, i, na, ns;
167
168 /* Get parent & match bus type */
169 parent = of_get_parent(dev);
170 if (parent == NULL)
171 return NULL;
172 bus = of_match_bus(parent);
173 if (strcmp(bus->name, "pci")) {
174 of_node_put(parent);
175 return NULL;
176 }
177 bus->count_cells(dev, &na, &ns);
178 of_node_put(parent);
179 if (!OF_CHECK_COUNTS(na, ns))
180 return NULL;
181
182 /* Get "reg" or "assigned-addresses" property */
183 prop = of_get_property(dev, bus->addresses, &psize);
184 if (prop == NULL)
185 return NULL;
186 psize /= 4;
187
188 onesize = na + ns;
189 for (i = 0; psize >= onesize; psize -= onesize, prop += onesize, i++)
190 if ((prop[0] & 0xff) == ((bar_no * 4) + PCI_BASE_ADDRESS_0)) {
191 if (size)
192 *size = of_read_number(prop + na, ns);
193 if (flags)
194 *flags = bus->get_flags(prop);
195 return prop;
196 }
197 return NULL;
198}
199EXPORT_SYMBOL(of_get_pci_address);
200
201int of_pci_address_to_resource(struct device_node *dev, int bar,
202 struct resource *r)
203{
204 const u32 *addrp;
205 u64 size;
206 unsigned int flags;
207
208 addrp = of_get_pci_address(dev, bar, &size, &flags);
209 if (addrp == NULL)
210 return -EINVAL;
211 return __of_address_to_resource(dev, addrp, size, flags, r);
212}
213EXPORT_SYMBOL_GPL(of_pci_address_to_resource);
214
215static u8 of_irq_pci_swizzle(u8 slot, u8 pin)
216{
217 return (((pin - 1) + slot) % 4) + 1;
218}
219
220int of_irq_map_pci(struct pci_dev *pdev, struct of_irq *out_irq) 14int of_irq_map_pci(struct pci_dev *pdev, struct of_irq *out_irq)
221{ 15{
222 struct device_node *dn, *ppnode; 16 struct device_node *dn, *ppnode;
@@ -291,289 +85,6 @@ int of_irq_map_pci(struct pci_dev *pdev, struct of_irq *out_irq)
291EXPORT_SYMBOL_GPL(of_irq_map_pci); 85EXPORT_SYMBOL_GPL(of_irq_map_pci);
292#endif /* CONFIG_PCI */ 86#endif /* CONFIG_PCI */
293 87
294/*
295 * ISA bus specific translator
296 */
297
298static int of_bus_isa_match(struct device_node *np)
299{
300 return !strcmp(np->name, "isa");
301}
302
303static void of_bus_isa_count_cells(struct device_node *child,
304 int *addrc, int *sizec)
305{
306 if (addrc)
307 *addrc = 2;
308 if (sizec)
309 *sizec = 1;
310}
311
312static u64 of_bus_isa_map(u32 *addr, const u32 *range, int na, int ns, int pna)
313{
314 u64 cp, s, da;
315
316 /* Check address type match */
317 if ((addr[0] ^ range[0]) & 0x00000001)
318 return OF_BAD_ADDR;
319
320 /* Read address values, skipping high cell */
321 cp = of_read_number(range + 1, na - 1);
322 s = of_read_number(range + na + pna, ns);
323 da = of_read_number(addr + 1, na - 1);
324
325 pr_debug("OF: ISA map, cp="PRu64", s="PRu64", da="PRu64"\n", cp, s, da);
326
327 if (da < cp || da >= (cp + s))
328 return OF_BAD_ADDR;
329 return da - cp;
330}
331
332static int of_bus_isa_translate(u32 *addr, u64 offset, int na)
333{
334 return of_bus_default_translate(addr + 1, offset, na - 1);
335}
336
337static unsigned int of_bus_isa_get_flags(const u32 *addr)
338{
339 unsigned int flags = 0;
340 u32 w = addr[0];
341
342 if (w & 1)
343 flags |= IORESOURCE_IO;
344 else
345 flags |= IORESOURCE_MEM;
346 return flags;
347}
348
349/*
350 * Array of bus specific translators
351 */
352
353static struct of_bus of_busses[] = {
354#ifdef CONFIG_PCI
355 /* PCI */
356 {
357 .name = "pci",
358 .addresses = "assigned-addresses",
359 .match = of_bus_pci_match,
360 .count_cells = of_bus_pci_count_cells,
361 .map = of_bus_pci_map,
362 .translate = of_bus_pci_translate,
363 .get_flags = of_bus_pci_get_flags,
364 },
365#endif /* CONFIG_PCI */
366 /* ISA */
367 {
368 .name = "isa",
369 .addresses = "reg",
370 .match = of_bus_isa_match,
371 .count_cells = of_bus_isa_count_cells,
372 .map = of_bus_isa_map,
373 .translate = of_bus_isa_translate,
374 .get_flags = of_bus_isa_get_flags,
375 },
376 /* Default */
377 {
378 .name = "default",
379 .addresses = "reg",
380 .match = NULL,
381 .count_cells = of_bus_default_count_cells,
382 .map = of_bus_default_map,
383 .translate = of_bus_default_translate,
384 .get_flags = of_bus_default_get_flags,
385 },
386};
387
388static struct of_bus *of_match_bus(struct device_node *np)
389{
390 int i;
391
392 for (i = 0; i < ARRAY_SIZE(of_busses); i++)
393 if (!of_busses[i].match || of_busses[i].match(np))
394 return &of_busses[i];
395 BUG();
396 return NULL;
397}
398
399static int of_translate_one(struct device_node *parent, struct of_bus *bus,
400 struct of_bus *pbus, u32 *addr,
401 int na, int ns, int pna)
402{
403 const u32 *ranges;
404 unsigned int rlen;
405 int rone;
406 u64 offset = OF_BAD_ADDR;
407
408 /* Normally, an absence of a "ranges" property means we are
409 * crossing a non-translatable boundary, and thus the addresses
410 * below the current not cannot be converted to CPU physical ones.
411 * Unfortunately, while this is very clear in the spec, it's not
412 * what Apple understood, and they do have things like /uni-n or
413 * /ht nodes with no "ranges" property and a lot of perfectly
414 * useable mapped devices below them. Thus we treat the absence of
415 * "ranges" as equivalent to an empty "ranges" property which means
416 * a 1:1 translation at that level. It's up to the caller not to try
417 * to translate addresses that aren't supposed to be translated in
418 * the first place. --BenH.
419 */
420 ranges = of_get_property(parent, "ranges", (int *) &rlen);
421 if (ranges == NULL || rlen == 0) {
422 offset = of_read_number(addr, na);
423 memset(addr, 0, pna * 4);
424 pr_debug("OF: no ranges, 1:1 translation\n");
425 goto finish;
426 }
427
428 pr_debug("OF: walking ranges...\n");
429
430 /* Now walk through the ranges */
431 rlen /= 4;
432 rone = na + pna + ns;
433 for (; rlen >= rone; rlen -= rone, ranges += rone) {
434 offset = bus->map(addr, ranges, na, ns, pna);
435 if (offset != OF_BAD_ADDR)
436 break;
437 }
438 if (offset == OF_BAD_ADDR) {
439 pr_debug("OF: not found !\n");
440 return 1;
441 }
442 memcpy(addr, ranges + na, 4 * pna);
443
444 finish:
445 of_dump_addr("OF: parent translation for:", addr, pna);
446 pr_debug("OF: with offset: "PRu64"\n", offset);
447
448 /* Translate it into parent bus space */
449 return pbus->translate(addr, offset, pna);
450}
451
452/*
453 * Translate an address from the device-tree into a CPU physical address,
454 * this walks up the tree and applies the various bus mappings on the
455 * way.
456 *
457 * Note: We consider that crossing any level with #size-cells == 0 to mean
458 * that translation is impossible (that is we are not dealing with a value
459 * that can be mapped to a cpu physical address). This is not really specified
460 * that way, but this is traditionally the way IBM at least do things
461 */
462u64 of_translate_address(struct device_node *dev, const u32 *in_addr)
463{
464 struct device_node *parent = NULL;
465 struct of_bus *bus, *pbus;
466 u32 addr[OF_MAX_ADDR_CELLS];
467 int na, ns, pna, pns;
468 u64 result = OF_BAD_ADDR;
469
470 pr_debug("OF: ** translation for device %s **\n", dev->full_name);
471
472 /* Increase refcount at current level */
473 of_node_get(dev);
474
475 /* Get parent & match bus type */
476 parent = of_get_parent(dev);
477 if (parent == NULL)
478 goto bail;
479 bus = of_match_bus(parent);
480
481 /* Cound address cells & copy address locally */
482 bus->count_cells(dev, &na, &ns);
483 if (!OF_CHECK_COUNTS(na, ns)) {
484 printk(KERN_ERR "prom_parse: Bad cell count for %s\n",
485 dev->full_name);
486 goto bail;
487 }
488 memcpy(addr, in_addr, na * 4);
489
490 pr_debug("OF: bus is %s (na=%d, ns=%d) on %s\n",
491 bus->name, na, ns, parent->full_name);
492 of_dump_addr("OF: translating address:", addr, na);
493
494 /* Translate */
495 for (;;) {
496 /* Switch to parent bus */
497 of_node_put(dev);
498 dev = parent;
499 parent = of_get_parent(dev);
500
501 /* If root, we have finished */
502 if (parent == NULL) {
503 pr_debug("OF: reached root node\n");
504 result = of_read_number(addr, na);
505 break;
506 }
507
508 /* Get new parent bus and counts */
509 pbus = of_match_bus(parent);
510 pbus->count_cells(dev, &pna, &pns);
511 if (!OF_CHECK_COUNTS(pna, pns)) {
512 printk(KERN_ERR "prom_parse: Bad cell count for %s\n",
513 dev->full_name);
514 break;
515 }
516
517 pr_debug("OF: parent bus is %s (na=%d, ns=%d) on %s\n",
518 pbus->name, pna, pns, parent->full_name);
519
520 /* Apply bus translation */
521 if (of_translate_one(dev, bus, pbus, addr, na, ns, pna))
522 break;
523
524 /* Complete the move up one level */
525 na = pna;
526 ns = pns;
527 bus = pbus;
528
529 of_dump_addr("OF: one level translation:", addr, na);
530 }
531 bail:
532 of_node_put(parent);
533 of_node_put(dev);
534
535 return result;
536}
537EXPORT_SYMBOL(of_translate_address);
538
539const u32 *of_get_address(struct device_node *dev, int index, u64 *size,
540 unsigned int *flags)
541{
542 const u32 *prop;
543 unsigned int psize;
544 struct device_node *parent;
545 struct of_bus *bus;
546 int onesize, i, na, ns;
547
548 /* Get parent & match bus type */
549 parent = of_get_parent(dev);
550 if (parent == NULL)
551 return NULL;
552 bus = of_match_bus(parent);
553 bus->count_cells(dev, &na, &ns);
554 of_node_put(parent);
555 if (!OF_CHECK_COUNTS(na, ns))
556 return NULL;
557
558 /* Get "reg" or "assigned-addresses" property */
559 prop = of_get_property(dev, bus->addresses, (int *) &psize);
560 if (prop == NULL)
561 return NULL;
562 psize /= 4;
563
564 onesize = na + ns;
565 for (i = 0; psize >= onesize; psize -= onesize, prop += onesize, i++)
566 if (i == index) {
567 if (size)
568 *size = of_read_number(prop + na, ns);
569 if (flags)
570 *flags = bus->get_flags(prop);
571 return prop;
572 }
573 return NULL;
574}
575EXPORT_SYMBOL(of_get_address);
576
577void of_parse_dma_window(struct device_node *dn, const void *dma_window_prop, 88void of_parse_dma_window(struct device_node *dn, const void *dma_window_prop,
578 unsigned long *busno, unsigned long *phys, unsigned long *size) 89 unsigned long *busno, unsigned long *phys, unsigned long *size)
579{ 90{
diff --git a/arch/powerpc/include/asm/prom.h b/arch/powerpc/include/asm/prom.h
index ceace966c51c..f864722679e8 100644
--- a/arch/powerpc/include/asm/prom.h
+++ b/arch/powerpc/include/asm/prom.h
@@ -45,10 +45,6 @@ extern void pci_create_OF_bus_map(void);
45 * OF address retreival & translation 45 * OF address retreival & translation
46 */ 46 */
47 47
48/* Translate an OF address block into a CPU physical address
49 */
50extern u64 of_translate_address(struct device_node *np, const u32 *addr);
51
52/* Translate a DMA address from device space to CPU space */ 48/* Translate a DMA address from device space to CPU space */
53extern u64 of_translate_dma_address(struct device_node *dev, 49extern u64 of_translate_dma_address(struct device_node *dev,
54 const u32 *in_addr); 50 const u32 *in_addr);
diff --git a/arch/powerpc/kernel/prom_parse.c b/arch/powerpc/kernel/prom_parse.c
index 1dac535de78f..88334af038e5 100644
--- a/arch/powerpc/kernel/prom_parse.c
+++ b/arch/powerpc/kernel/prom_parse.c
@@ -10,225 +10,7 @@
10#include <asm/prom.h> 10#include <asm/prom.h>
11#include <asm/pci-bridge.h> 11#include <asm/pci-bridge.h>
12 12
13#ifdef DEBUG
14#define DBG(fmt...) do { printk(fmt); } while(0)
15#else
16#define DBG(fmt...) do { } while(0)
17#endif
18
19#ifdef CONFIG_PPC64
20#define PRu64 "%lx"
21#else
22#define PRu64 "%llx"
23#endif
24
25/* Max address size we deal with */
26#define OF_MAX_ADDR_CELLS 4
27#define OF_CHECK_COUNTS(na, ns) ((na) > 0 && (na) <= OF_MAX_ADDR_CELLS && \
28 (ns) > 0)
29
30static struct of_bus *of_match_bus(struct device_node *np);
31
32/* Debug utility */
33#ifdef DEBUG
34static void of_dump_addr(const char *s, const u32 *addr, int na)
35{
36 printk("%s", s);
37 while(na--)
38 printk(" %08x", *(addr++));
39 printk("\n");
40}
41#else
42static void of_dump_addr(const char *s, const u32 *addr, int na) { }
43#endif
44
45
46/* Callbacks for bus specific translators */
47struct of_bus {
48 const char *name;
49 const char *addresses;
50 int (*match)(struct device_node *parent);
51 void (*count_cells)(struct device_node *child,
52 int *addrc, int *sizec);
53 u64 (*map)(u32 *addr, const u32 *range,
54 int na, int ns, int pna);
55 int (*translate)(u32 *addr, u64 offset, int na);
56 unsigned int (*get_flags)(const u32 *addr);
57};
58
59
60/*
61 * Default translator (generic bus)
62 */
63
64static void of_bus_default_count_cells(struct device_node *dev,
65 int *addrc, int *sizec)
66{
67 if (addrc)
68 *addrc = of_n_addr_cells(dev);
69 if (sizec)
70 *sizec = of_n_size_cells(dev);
71}
72
73static u64 of_bus_default_map(u32 *addr, const u32 *range,
74 int na, int ns, int pna)
75{
76 u64 cp, s, da;
77
78 cp = of_read_number(range, na);
79 s = of_read_number(range + na + pna, ns);
80 da = of_read_number(addr, na);
81
82 DBG("OF: default map, cp="PRu64", s="PRu64", da="PRu64"\n",
83 cp, s, da);
84
85 if (da < cp || da >= (cp + s))
86 return OF_BAD_ADDR;
87 return da - cp;
88}
89
90static int of_bus_default_translate(u32 *addr, u64 offset, int na)
91{
92 u64 a = of_read_number(addr, na);
93 memset(addr, 0, na * 4);
94 a += offset;
95 if (na > 1)
96 addr[na - 2] = a >> 32;
97 addr[na - 1] = a & 0xffffffffu;
98
99 return 0;
100}
101
102static unsigned int of_bus_default_get_flags(const u32 *addr)
103{
104 return IORESOURCE_MEM;
105}
106
107
108#ifdef CONFIG_PCI 13#ifdef CONFIG_PCI
109/*
110 * PCI bus specific translator
111 */
112
113static int of_bus_pci_match(struct device_node *np)
114{
115 /* "vci" is for the /chaos bridge on 1st-gen PCI powermacs */
116 return !strcmp(np->type, "pci") || !strcmp(np->type, "vci");
117}
118
119static void of_bus_pci_count_cells(struct device_node *np,
120 int *addrc, int *sizec)
121{
122 if (addrc)
123 *addrc = 3;
124 if (sizec)
125 *sizec = 2;
126}
127
128static unsigned int of_bus_pci_get_flags(const u32 *addr)
129{
130 unsigned int flags = 0;
131 u32 w = addr[0];
132
133 switch((w >> 24) & 0x03) {
134 case 0x01:
135 flags |= IORESOURCE_IO;
136 break;
137 case 0x02: /* 32 bits */
138 case 0x03: /* 64 bits */
139 flags |= IORESOURCE_MEM;
140 break;
141 }
142 if (w & 0x40000000)
143 flags |= IORESOURCE_PREFETCH;
144 return flags;
145}
146
147static u64 of_bus_pci_map(u32 *addr, const u32 *range, int na, int ns, int pna)
148{
149 u64 cp, s, da;
150 unsigned int af, rf;
151
152 af = of_bus_pci_get_flags(addr);
153 rf = of_bus_pci_get_flags(range);
154
155 /* Check address type match */
156 if ((af ^ rf) & (IORESOURCE_MEM | IORESOURCE_IO))
157 return OF_BAD_ADDR;
158
159 /* Read address values, skipping high cell */
160 cp = of_read_number(range + 1, na - 1);
161 s = of_read_number(range + na + pna, ns);
162 da = of_read_number(addr + 1, na - 1);
163
164 DBG("OF: PCI map, cp="PRu64", s="PRu64", da="PRu64"\n", cp, s, da);
165
166 if (da < cp || da >= (cp + s))
167 return OF_BAD_ADDR;
168 return da - cp;
169}
170
171static int of_bus_pci_translate(u32 *addr, u64 offset, int na)
172{
173 return of_bus_default_translate(addr + 1, offset, na - 1);
174}
175
176const u32 *of_get_pci_address(struct device_node *dev, int bar_no, u64 *size,
177 unsigned int *flags)
178{
179 const u32 *prop;
180 unsigned int psize;
181 struct device_node *parent;
182 struct of_bus *bus;
183 int onesize, i, na, ns;
184
185 /* Get parent & match bus type */
186 parent = of_get_parent(dev);
187 if (parent == NULL)
188 return NULL;
189 bus = of_match_bus(parent);
190 if (strcmp(bus->name, "pci")) {
191 of_node_put(parent);
192 return NULL;
193 }
194 bus->count_cells(dev, &na, &ns);
195 of_node_put(parent);
196 if (!OF_CHECK_COUNTS(na, ns))
197 return NULL;
198
199 /* Get "reg" or "assigned-addresses" property */
200 prop = of_get_property(dev, bus->addresses, &psize);
201 if (prop == NULL)
202 return NULL;
203 psize /= 4;
204
205 onesize = na + ns;
206 for (i = 0; psize >= onesize; psize -= onesize, prop += onesize, i++)
207 if ((prop[0] & 0xff) == ((bar_no * 4) + PCI_BASE_ADDRESS_0)) {
208 if (size)
209 *size = of_read_number(prop + na, ns);
210 if (flags)
211 *flags = bus->get_flags(prop);
212 return prop;
213 }
214 return NULL;
215}
216EXPORT_SYMBOL(of_get_pci_address);
217
218int of_pci_address_to_resource(struct device_node *dev, int bar,
219 struct resource *r)
220{
221 const u32 *addrp;
222 u64 size;
223 unsigned int flags;
224
225 addrp = of_get_pci_address(dev, bar, &size, &flags);
226 if (addrp == NULL)
227 return -EINVAL;
228 return __of_address_to_resource(dev, addrp, size, flags, r);
229}
230EXPORT_SYMBOL_GPL(of_pci_address_to_resource);
231
232int of_irq_map_pci(struct pci_dev *pdev, struct of_irq *out_irq) 14int of_irq_map_pci(struct pci_dev *pdev, struct of_irq *out_irq)
233{ 15{
234 struct device_node *dn, *ppnode; 16 struct device_node *dn, *ppnode;
@@ -310,303 +92,6 @@ int of_irq_map_pci(struct pci_dev *pdev, struct of_irq *out_irq)
310EXPORT_SYMBOL_GPL(of_irq_map_pci); 92EXPORT_SYMBOL_GPL(of_irq_map_pci);
311#endif /* CONFIG_PCI */ 93#endif /* CONFIG_PCI */
312 94
313/*
314 * ISA bus specific translator
315 */
316
317static int of_bus_isa_match(struct device_node *np)
318{
319 return !strcmp(np->name, "isa");
320}
321
322static void of_bus_isa_count_cells(struct device_node *child,
323 int *addrc, int *sizec)
324{
325 if (addrc)
326 *addrc = 2;
327 if (sizec)
328 *sizec = 1;
329}
330
331static u64 of_bus_isa_map(u32 *addr, const u32 *range, int na, int ns, int pna)
332{
333 u64 cp, s, da;
334
335 /* Check address type match */
336 if ((addr[0] ^ range[0]) & 0x00000001)
337 return OF_BAD_ADDR;
338
339 /* Read address values, skipping high cell */
340 cp = of_read_number(range + 1, na - 1);
341 s = of_read_number(range + na + pna, ns);
342 da = of_read_number(addr + 1, na - 1);
343
344 DBG("OF: ISA map, cp="PRu64", s="PRu64", da="PRu64"\n", cp, s, da);
345
346 if (da < cp || da >= (cp + s))
347 return OF_BAD_ADDR;
348 return da - cp;
349}
350
351static int of_bus_isa_translate(u32 *addr, u64 offset, int na)
352{
353 return of_bus_default_translate(addr + 1, offset, na - 1);
354}
355
356static unsigned int of_bus_isa_get_flags(const u32 *addr)
357{
358 unsigned int flags = 0;
359 u32 w = addr[0];
360
361 if (w & 1)
362 flags |= IORESOURCE_IO;
363 else
364 flags |= IORESOURCE_MEM;
365 return flags;
366}
367
368
369/*
370 * Array of bus specific translators
371 */
372
373static struct of_bus of_busses[] = {
374#ifdef CONFIG_PCI
375 /* PCI */
376 {
377 .name = "pci",
378 .addresses = "assigned-addresses",
379 .match = of_bus_pci_match,
380 .count_cells = of_bus_pci_count_cells,
381 .map = of_bus_pci_map,
382 .translate = of_bus_pci_translate,
383 .get_flags = of_bus_pci_get_flags,
384 },
385#endif /* CONFIG_PCI */
386 /* ISA */
387 {
388 .name = "isa",
389 .addresses = "reg",
390 .match = of_bus_isa_match,
391 .count_cells = of_bus_isa_count_cells,
392 .map = of_bus_isa_map,
393 .translate = of_bus_isa_translate,
394 .get_flags = of_bus_isa_get_flags,
395 },
396 /* Default */
397 {
398 .name = "default",
399 .addresses = "reg",
400 .match = NULL,
401 .count_cells = of_bus_default_count_cells,
402 .map = of_bus_default_map,
403 .translate = of_bus_default_translate,
404 .get_flags = of_bus_default_get_flags,
405 },
406};
407
408static struct of_bus *of_match_bus(struct device_node *np)
409{
410 int i;
411
412 for (i = 0; i < ARRAY_SIZE(of_busses); i ++)
413 if (!of_busses[i].match || of_busses[i].match(np))
414 return &of_busses[i];
415 BUG();
416 return NULL;
417}
418
419static int of_translate_one(struct device_node *parent, struct of_bus *bus,
420 struct of_bus *pbus, u32 *addr,
421 int na, int ns, int pna, const char *rprop)
422{
423 const u32 *ranges;
424 unsigned int rlen;
425 int rone;
426 u64 offset = OF_BAD_ADDR;
427
428 /* Normally, an absence of a "ranges" property means we are
429 * crossing a non-translatable boundary, and thus the addresses
430 * below the current not cannot be converted to CPU physical ones.
431 * Unfortunately, while this is very clear in the spec, it's not
432 * what Apple understood, and they do have things like /uni-n or
433 * /ht nodes with no "ranges" property and a lot of perfectly
434 * useable mapped devices below them. Thus we treat the absence of
435 * "ranges" as equivalent to an empty "ranges" property which means
436 * a 1:1 translation at that level. It's up to the caller not to try
437 * to translate addresses that aren't supposed to be translated in
438 * the first place. --BenH.
439 */
440 ranges = of_get_property(parent, rprop, &rlen);
441 if (ranges == NULL || rlen == 0) {
442 offset = of_read_number(addr, na);
443 memset(addr, 0, pna * 4);
444 DBG("OF: no ranges, 1:1 translation\n");
445 goto finish;
446 }
447
448 DBG("OF: walking ranges...\n");
449
450 /* Now walk through the ranges */
451 rlen /= 4;
452 rone = na + pna + ns;
453 for (; rlen >= rone; rlen -= rone, ranges += rone) {
454 offset = bus->map(addr, ranges, na, ns, pna);
455 if (offset != OF_BAD_ADDR)
456 break;
457 }
458 if (offset == OF_BAD_ADDR) {
459 DBG("OF: not found !\n");
460 return 1;
461 }
462 memcpy(addr, ranges + na, 4 * pna);
463
464 finish:
465 of_dump_addr("OF: parent translation for:", addr, pna);
466 DBG("OF: with offset: "PRu64"\n", offset);
467
468 /* Translate it into parent bus space */
469 return pbus->translate(addr, offset, pna);
470}
471
472
473/*
474 * Translate an address from the device-tree into a CPU physical address,
475 * this walks up the tree and applies the various bus mappings on the
476 * way.
477 *
478 * Note: We consider that crossing any level with #size-cells == 0 to mean
479 * that translation is impossible (that is we are not dealing with a value
480 * that can be mapped to a cpu physical address). This is not really specified
481 * that way, but this is traditionally the way IBM at least do things
482 */
483u64 __of_translate_address(struct device_node *dev, const u32 *in_addr,
484 const char *rprop)
485{
486 struct device_node *parent = NULL;
487 struct of_bus *bus, *pbus;
488 u32 addr[OF_MAX_ADDR_CELLS];
489 int na, ns, pna, pns;
490 u64 result = OF_BAD_ADDR;
491
492 DBG("OF: ** translation for device %s **\n", dev->full_name);
493
494 /* Increase refcount at current level */
495 of_node_get(dev);
496
497 /* Get parent & match bus type */
498 parent = of_get_parent(dev);
499 if (parent == NULL)
500 goto bail;
501 bus = of_match_bus(parent);
502
503 /* Cound address cells & copy address locally */
504 bus->count_cells(dev, &na, &ns);
505 if (!OF_CHECK_COUNTS(na, ns)) {
506 printk(KERN_ERR "prom_parse: Bad cell count for %s\n",
507 dev->full_name);
508 goto bail;
509 }
510 memcpy(addr, in_addr, na * 4);
511
512 DBG("OF: bus is %s (na=%d, ns=%d) on %s\n",
513 bus->name, na, ns, parent->full_name);
514 of_dump_addr("OF: translating address:", addr, na);
515
516 /* Translate */
517 for (;;) {
518 /* Switch to parent bus */
519 of_node_put(dev);
520 dev = parent;
521 parent = of_get_parent(dev);
522
523 /* If root, we have finished */
524 if (parent == NULL) {
525 DBG("OF: reached root node\n");
526 result = of_read_number(addr, na);
527 break;
528 }
529
530 /* Get new parent bus and counts */
531 pbus = of_match_bus(parent);
532 pbus->count_cells(dev, &pna, &pns);
533 if (!OF_CHECK_COUNTS(pna, pns)) {
534 printk(KERN_ERR "prom_parse: Bad cell count for %s\n",
535 dev->full_name);
536 break;
537 }
538
539 DBG("OF: parent bus is %s (na=%d, ns=%d) on %s\n",
540 pbus->name, pna, pns, parent->full_name);
541
542 /* Apply bus translation */
543 if (of_translate_one(dev, bus, pbus, addr, na, ns, pna, rprop))
544 break;
545
546 /* Complete the move up one level */
547 na = pna;
548 ns = pns;
549 bus = pbus;
550
551 of_dump_addr("OF: one level translation:", addr, na);
552 }
553 bail:
554 of_node_put(parent);
555 of_node_put(dev);
556
557 return result;
558}
559
560u64 of_translate_address(struct device_node *dev, const u32 *in_addr)
561{
562 return __of_translate_address(dev, in_addr, "ranges");
563}
564EXPORT_SYMBOL(of_translate_address);
565
566u64 of_translate_dma_address(struct device_node *dev, const u32 *in_addr)
567{
568 return __of_translate_address(dev, in_addr, "dma-ranges");
569}
570EXPORT_SYMBOL(of_translate_dma_address);
571
572const u32 *of_get_address(struct device_node *dev, int index, u64 *size,
573 unsigned int *flags)
574{
575 const u32 *prop;
576 unsigned int psize;
577 struct device_node *parent;
578 struct of_bus *bus;
579 int onesize, i, na, ns;
580
581 /* Get parent & match bus type */
582 parent = of_get_parent(dev);
583 if (parent == NULL)
584 return NULL;
585 bus = of_match_bus(parent);
586 bus->count_cells(dev, &na, &ns);
587 of_node_put(parent);
588 if (!OF_CHECK_COUNTS(na, ns))
589 return NULL;
590
591 /* Get "reg" or "assigned-addresses" property */
592 prop = of_get_property(dev, bus->addresses, &psize);
593 if (prop == NULL)
594 return NULL;
595 psize /= 4;
596
597 onesize = na + ns;
598 for (i = 0; psize >= onesize; psize -= onesize, prop += onesize, i++)
599 if (i == index) {
600 if (size)
601 *size = of_read_number(prop + na, ns);
602 if (flags)
603 *flags = bus->get_flags(prop);
604 return prop;
605 }
606 return NULL;
607}
608EXPORT_SYMBOL(of_get_address);
609
610void of_parse_dma_window(struct device_node *dn, const void *dma_window_prop, 95void of_parse_dma_window(struct device_node *dn, const void *dma_window_prop,
611 unsigned long *busno, unsigned long *phys, unsigned long *size) 96 unsigned long *busno, unsigned long *phys, unsigned long *size)
612{ 97{
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
diff --git a/include/linux/of_address.h b/include/linux/of_address.h
index 474b794ed9d2..cc567df9a00d 100644
--- a/include/linux/of_address.h
+++ b/include/linux/of_address.h
@@ -3,9 +3,7 @@
3#include <linux/ioport.h> 3#include <linux/ioport.h>
4#include <linux/of.h> 4#include <linux/of.h>
5 5
6extern int __of_address_to_resource(struct device_node *dev, const u32 *addrp, 6extern u64 of_translate_address(struct device_node *np, const u32 *addr);
7 u64 size, unsigned int flags,
8 struct resource *r);
9extern int of_address_to_resource(struct device_node *dev, int index, 7extern int of_address_to_resource(struct device_node *dev, int index,
10 struct resource *r); 8 struct resource *r);
11extern void __iomem *of_iomap(struct device_node *device, int index); 9extern void __iomem *of_iomap(struct device_node *device, int index);