blob: fe44c74ff4bd5c46104fbd4bcc34d8089bd1406a [file] [log] [blame]
Grant Likely6b884a82010-06-08 07:48:09 -06001
Arnd Bergmann5019f0b2012-04-11 17:30:11 +00002#include <linux/device.h>
Grant Likely6b884a82010-06-08 07:48:09 -06003#include <linux/io.h>
4#include <linux/ioport.h>
Grant Likelydbbdee92010-06-08 07:48:10 -06005#include <linux/module.h>
Grant Likely6b884a82010-06-08 07:48:09 -06006#include <linux/of_address.h>
Grant Likelydbbdee92010-06-08 07:48:10 -06007#include <linux/pci_regs.h>
8#include <linux/string.h>
Grant Likely6b884a82010-06-08 07:48:09 -06009
Grant Likelydbbdee92010-06-08 07:48:10 -060010/* Max address size we deal with */
11#define OF_MAX_ADDR_CELLS 4
Stephen Warren5d61b162012-07-25 17:34:37 -060012#define OF_CHECK_ADDR_COUNT(na) ((na) > 0 && (na) <= OF_MAX_ADDR_CELLS)
13#define OF_CHECK_COUNTS(na, ns) (OF_CHECK_ADDR_COUNT(na) && (ns) > 0)
Grant Likelydbbdee92010-06-08 07:48:10 -060014
15static struct of_bus *of_match_bus(struct device_node *np);
Sebastian Andrzej Siewior0131d892010-12-01 10:54:46 +010016static int __of_address_to_resource(struct device_node *dev,
17 const __be32 *addrp, u64 size, unsigned int flags,
Benoit Cousson35f3da32011-12-05 15:23:55 +010018 const char *name, struct resource *r);
Grant Likelydbbdee92010-06-08 07:48:10 -060019
20/* Debug utility */
21#ifdef DEBUG
Sebastian Andrzej Siewior0131d892010-12-01 10:54:46 +010022static void of_dump_addr(const char *s, const __be32 *addr, int na)
Grant Likelydbbdee92010-06-08 07:48:10 -060023{
24 printk(KERN_DEBUG "%s", s);
25 while (na--)
Grant Likely154063a92010-06-08 07:48:11 -060026 printk(" %08x", be32_to_cpu(*(addr++)));
Grant Likelydbbdee92010-06-08 07:48:10 -060027 printk("\n");
28}
29#else
Sebastian Andrzej Siewior0131d892010-12-01 10:54:46 +010030static void of_dump_addr(const char *s, const __be32 *addr, int na) { }
Grant Likelydbbdee92010-06-08 07:48:10 -060031#endif
32
33/* Callbacks for bus specific translators */
34struct of_bus {
35 const char *name;
36 const char *addresses;
37 int (*match)(struct device_node *parent);
38 void (*count_cells)(struct device_node *child,
39 int *addrc, int *sizec);
Kim Phillips47b1e682012-10-08 19:41:58 -050040 u64 (*map)(__be32 *addr, const __be32 *range,
Grant Likelydbbdee92010-06-08 07:48:10 -060041 int na, int ns, int pna);
Kim Phillips47b1e682012-10-08 19:41:58 -050042 int (*translate)(__be32 *addr, u64 offset, int na);
Sebastian Andrzej Siewior0131d892010-12-01 10:54:46 +010043 unsigned int (*get_flags)(const __be32 *addr);
Grant Likelydbbdee92010-06-08 07:48:10 -060044};
45
46/*
47 * Default translator (generic bus)
48 */
49
50static void of_bus_default_count_cells(struct device_node *dev,
51 int *addrc, int *sizec)
52{
53 if (addrc)
54 *addrc = of_n_addr_cells(dev);
55 if (sizec)
56 *sizec = of_n_size_cells(dev);
57}
58
Kim Phillips47b1e682012-10-08 19:41:58 -050059static u64 of_bus_default_map(__be32 *addr, const __be32 *range,
Grant Likelydbbdee92010-06-08 07:48:10 -060060 int na, int ns, int pna)
61{
62 u64 cp, s, da;
63
64 cp = of_read_number(range, na);
65 s = of_read_number(range + na + pna, ns);
66 da = of_read_number(addr, na);
67
68 pr_debug("OF: default map, cp=%llx, s=%llx, da=%llx\n",
69 (unsigned long long)cp, (unsigned long long)s,
70 (unsigned long long)da);
71
72 if (da < cp || da >= (cp + s))
73 return OF_BAD_ADDR;
74 return da - cp;
75}
76
Kim Phillips47b1e682012-10-08 19:41:58 -050077static int of_bus_default_translate(__be32 *addr, u64 offset, int na)
Grant Likelydbbdee92010-06-08 07:48:10 -060078{
79 u64 a = of_read_number(addr, na);
80 memset(addr, 0, na * 4);
81 a += offset;
82 if (na > 1)
Grant Likely154063a92010-06-08 07:48:11 -060083 addr[na - 2] = cpu_to_be32(a >> 32);
84 addr[na - 1] = cpu_to_be32(a & 0xffffffffu);
Grant Likelydbbdee92010-06-08 07:48:10 -060085
86 return 0;
87}
88
Sebastian Andrzej Siewior0131d892010-12-01 10:54:46 +010089static unsigned int of_bus_default_get_flags(const __be32 *addr)
Grant Likelydbbdee92010-06-08 07:48:10 -060090{
91 return IORESOURCE_MEM;
92}
93
Gregory CLEMENT25a31572014-02-19 23:14:55 +010094#ifdef CONFIG_OF_ADDRESS_PCI
Grant Likelydbbdee92010-06-08 07:48:10 -060095/*
96 * PCI bus specific translator
97 */
98
99static int of_bus_pci_match(struct device_node *np)
100{
Benjamin Herrenschmidt6dd18e42013-07-03 16:01:10 +1000101 /*
102 * "vci" is for the /chaos bridge on 1st-gen PCI powermacs
103 * "ht" is hypertransport
104 */
105 return !strcmp(np->type, "pci") || !strcmp(np->type, "vci") ||
106 !strcmp(np->type, "ht");
Grant Likelydbbdee92010-06-08 07:48:10 -0600107}
108
109static void of_bus_pci_count_cells(struct device_node *np,
110 int *addrc, int *sizec)
111{
112 if (addrc)
113 *addrc = 3;
114 if (sizec)
115 *sizec = 2;
116}
117
Sebastian Andrzej Siewior0131d892010-12-01 10:54:46 +0100118static unsigned int of_bus_pci_get_flags(const __be32 *addr)
Grant Likelydbbdee92010-06-08 07:48:10 -0600119{
120 unsigned int flags = 0;
Sebastian Andrzej Siewior0131d892010-12-01 10:54:46 +0100121 u32 w = be32_to_cpup(addr);
Grant Likelydbbdee92010-06-08 07:48:10 -0600122
123 switch((w >> 24) & 0x03) {
124 case 0x01:
125 flags |= IORESOURCE_IO;
126 break;
127 case 0x02: /* 32 bits */
128 case 0x03: /* 64 bits */
129 flags |= IORESOURCE_MEM;
130 break;
131 }
132 if (w & 0x40000000)
133 flags |= IORESOURCE_PREFETCH;
134 return flags;
135}
136
Kim Phillips47b1e682012-10-08 19:41:58 -0500137static u64 of_bus_pci_map(__be32 *addr, const __be32 *range, int na, int ns,
Sebastian Andrzej Siewior0131d892010-12-01 10:54:46 +0100138 int pna)
Grant Likelydbbdee92010-06-08 07:48:10 -0600139{
140 u64 cp, s, da;
141 unsigned int af, rf;
142
143 af = of_bus_pci_get_flags(addr);
144 rf = of_bus_pci_get_flags(range);
145
146 /* Check address type match */
147 if ((af ^ rf) & (IORESOURCE_MEM | IORESOURCE_IO))
148 return OF_BAD_ADDR;
149
150 /* Read address values, skipping high cell */
151 cp = of_read_number(range + 1, na - 1);
152 s = of_read_number(range + na + pna, ns);
153 da = of_read_number(addr + 1, na - 1);
154
155 pr_debug("OF: PCI map, cp=%llx, s=%llx, da=%llx\n",
156 (unsigned long long)cp, (unsigned long long)s,
157 (unsigned long long)da);
158
159 if (da < cp || da >= (cp + s))
160 return OF_BAD_ADDR;
161 return da - cp;
162}
163
Kim Phillips47b1e682012-10-08 19:41:58 -0500164static int of_bus_pci_translate(__be32 *addr, u64 offset, int na)
Grant Likelydbbdee92010-06-08 07:48:10 -0600165{
166 return of_bus_default_translate(addr + 1, offset, na - 1);
167}
Gregory CLEMENT25a31572014-02-19 23:14:55 +0100168#endif /* CONFIG_OF_ADDRESS_PCI */
Grant Likelydbbdee92010-06-08 07:48:10 -0600169
Gregory CLEMENT25a31572014-02-19 23:14:55 +0100170#ifdef CONFIG_PCI
Sebastian Andrzej Siewior0131d892010-12-01 10:54:46 +0100171const __be32 *of_get_pci_address(struct device_node *dev, int bar_no, u64 *size,
Grant Likelydbbdee92010-06-08 07:48:10 -0600172 unsigned int *flags)
173{
Jeremy Kerra9fadee2010-10-10 21:24:10 -0600174 const __be32 *prop;
Grant Likelydbbdee92010-06-08 07:48:10 -0600175 unsigned int psize;
176 struct device_node *parent;
177 struct of_bus *bus;
178 int onesize, i, na, ns;
179
180 /* Get parent & match bus type */
181 parent = of_get_parent(dev);
182 if (parent == NULL)
183 return NULL;
184 bus = of_match_bus(parent);
185 if (strcmp(bus->name, "pci")) {
186 of_node_put(parent);
187 return NULL;
188 }
189 bus->count_cells(dev, &na, &ns);
190 of_node_put(parent);
Stephen Warren5d61b162012-07-25 17:34:37 -0600191 if (!OF_CHECK_ADDR_COUNT(na))
Grant Likelydbbdee92010-06-08 07:48:10 -0600192 return NULL;
193
194 /* Get "reg" or "assigned-addresses" property */
195 prop = of_get_property(dev, bus->addresses, &psize);
196 if (prop == NULL)
197 return NULL;
198 psize /= 4;
199
200 onesize = na + ns;
Grant Likely154063a92010-06-08 07:48:11 -0600201 for (i = 0; psize >= onesize; psize -= onesize, prop += onesize, i++) {
202 u32 val = be32_to_cpu(prop[0]);
203 if ((val & 0xff) == ((bar_no * 4) + PCI_BASE_ADDRESS_0)) {
Grant Likelydbbdee92010-06-08 07:48:10 -0600204 if (size)
205 *size = of_read_number(prop + na, ns);
206 if (flags)
207 *flags = bus->get_flags(prop);
208 return prop;
209 }
Grant Likely154063a92010-06-08 07:48:11 -0600210 }
Grant Likelydbbdee92010-06-08 07:48:10 -0600211 return NULL;
212}
213EXPORT_SYMBOL(of_get_pci_address);
214
215int of_pci_address_to_resource(struct device_node *dev, int bar,
216 struct resource *r)
217{
Sebastian Andrzej Siewior0131d892010-12-01 10:54:46 +0100218 const __be32 *addrp;
Grant Likelydbbdee92010-06-08 07:48:10 -0600219 u64 size;
220 unsigned int flags;
221
222 addrp = of_get_pci_address(dev, bar, &size, &flags);
223 if (addrp == NULL)
224 return -EINVAL;
Benoit Cousson35f3da32011-12-05 15:23:55 +0100225 return __of_address_to_resource(dev, addrp, size, flags, NULL, r);
Grant Likelydbbdee92010-06-08 07:48:10 -0600226}
227EXPORT_SYMBOL_GPL(of_pci_address_to_resource);
Andrew Murray29b635c2013-05-16 17:55:17 +0200228
229int of_pci_range_parser_init(struct of_pci_range_parser *parser,
230 struct device_node *node)
231{
232 const int na = 3, ns = 2;
233 int rlen;
234
235 parser->node = node;
236 parser->pna = of_n_addr_cells(node);
237 parser->np = parser->pna + na + ns;
238
239 parser->range = of_get_property(node, "ranges", &rlen);
240 if (parser->range == NULL)
241 return -ENOENT;
242
243 parser->end = parser->range + rlen / sizeof(__be32);
244
245 return 0;
246}
247EXPORT_SYMBOL_GPL(of_pci_range_parser_init);
248
249struct of_pci_range *of_pci_range_parser_one(struct of_pci_range_parser *parser,
250 struct of_pci_range *range)
251{
252 const int na = 3, ns = 2;
253
254 if (!range)
255 return NULL;
256
257 if (!parser->range || parser->range + parser->np > parser->end)
258 return NULL;
259
260 range->pci_space = parser->range[0];
261 range->flags = of_bus_pci_get_flags(parser->range);
262 range->pci_addr = of_read_number(parser->range + 1, ns);
263 range->cpu_addr = of_translate_address(parser->node,
264 parser->range + na);
265 range->size = of_read_number(parser->range + parser->pna + na, ns);
266
267 parser->range += parser->np;
268
269 /* Now consume following elements while they are contiguous */
270 while (parser->range + parser->np <= parser->end) {
271 u32 flags, pci_space;
272 u64 pci_addr, cpu_addr, size;
273
274 pci_space = be32_to_cpup(parser->range);
275 flags = of_bus_pci_get_flags(parser->range);
276 pci_addr = of_read_number(parser->range + 1, ns);
277 cpu_addr = of_translate_address(parser->node,
278 parser->range + na);
279 size = of_read_number(parser->range + parser->pna + na, ns);
280
281 if (flags != range->flags)
282 break;
283 if (pci_addr != range->pci_addr + range->size ||
284 cpu_addr != range->cpu_addr + range->size)
285 break;
286
287 range->size += size;
288 parser->range += parser->np;
289 }
290
291 return range;
292}
293EXPORT_SYMBOL_GPL(of_pci_range_parser_one);
294
Grant Likelydbbdee92010-06-08 07:48:10 -0600295#endif /* CONFIG_PCI */
296
297/*
298 * ISA bus specific translator
299 */
300
301static int of_bus_isa_match(struct device_node *np)
302{
303 return !strcmp(np->name, "isa");
304}
305
306static void of_bus_isa_count_cells(struct device_node *child,
307 int *addrc, int *sizec)
308{
309 if (addrc)
310 *addrc = 2;
311 if (sizec)
312 *sizec = 1;
313}
314
Kim Phillips47b1e682012-10-08 19:41:58 -0500315static u64 of_bus_isa_map(__be32 *addr, const __be32 *range, int na, int ns,
Sebastian Andrzej Siewior0131d892010-12-01 10:54:46 +0100316 int pna)
Grant Likelydbbdee92010-06-08 07:48:10 -0600317{
318 u64 cp, s, da;
319
320 /* Check address type match */
Sebastian Andrzej Siewior0131d892010-12-01 10:54:46 +0100321 if ((addr[0] ^ range[0]) & cpu_to_be32(1))
Grant Likelydbbdee92010-06-08 07:48:10 -0600322 return OF_BAD_ADDR;
323
324 /* Read address values, skipping high cell */
325 cp = of_read_number(range + 1, na - 1);
326 s = of_read_number(range + na + pna, ns);
327 da = of_read_number(addr + 1, na - 1);
328
329 pr_debug("OF: ISA map, cp=%llx, s=%llx, da=%llx\n",
330 (unsigned long long)cp, (unsigned long long)s,
331 (unsigned long long)da);
332
333 if (da < cp || da >= (cp + s))
334 return OF_BAD_ADDR;
335 return da - cp;
336}
337
Kim Phillips47b1e682012-10-08 19:41:58 -0500338static int of_bus_isa_translate(__be32 *addr, u64 offset, int na)
Grant Likelydbbdee92010-06-08 07:48:10 -0600339{
340 return of_bus_default_translate(addr + 1, offset, na - 1);
341}
342
Sebastian Andrzej Siewior0131d892010-12-01 10:54:46 +0100343static unsigned int of_bus_isa_get_flags(const __be32 *addr)
Grant Likelydbbdee92010-06-08 07:48:10 -0600344{
345 unsigned int flags = 0;
Sebastian Andrzej Siewior0131d892010-12-01 10:54:46 +0100346 u32 w = be32_to_cpup(addr);
Grant Likelydbbdee92010-06-08 07:48:10 -0600347
348 if (w & 1)
349 flags |= IORESOURCE_IO;
350 else
351 flags |= IORESOURCE_MEM;
352 return flags;
353}
354
355/*
356 * Array of bus specific translators
357 */
358
359static struct of_bus of_busses[] = {
Gregory CLEMENT25a31572014-02-19 23:14:55 +0100360#ifdef CONFIG_OF_ADDRESS_PCI
Grant Likelydbbdee92010-06-08 07:48:10 -0600361 /* PCI */
362 {
363 .name = "pci",
364 .addresses = "assigned-addresses",
365 .match = of_bus_pci_match,
366 .count_cells = of_bus_pci_count_cells,
367 .map = of_bus_pci_map,
368 .translate = of_bus_pci_translate,
369 .get_flags = of_bus_pci_get_flags,
370 },
Gregory CLEMENT25a31572014-02-19 23:14:55 +0100371#endif /* CONFIG_OF_ADDRESS_PCI */
Grant Likelydbbdee92010-06-08 07:48:10 -0600372 /* ISA */
373 {
374 .name = "isa",
375 .addresses = "reg",
376 .match = of_bus_isa_match,
377 .count_cells = of_bus_isa_count_cells,
378 .map = of_bus_isa_map,
379 .translate = of_bus_isa_translate,
380 .get_flags = of_bus_isa_get_flags,
381 },
382 /* Default */
383 {
384 .name = "default",
385 .addresses = "reg",
386 .match = NULL,
387 .count_cells = of_bus_default_count_cells,
388 .map = of_bus_default_map,
389 .translate = of_bus_default_translate,
390 .get_flags = of_bus_default_get_flags,
391 },
392};
393
394static struct of_bus *of_match_bus(struct device_node *np)
395{
396 int i;
397
398 for (i = 0; i < ARRAY_SIZE(of_busses); i++)
399 if (!of_busses[i].match || of_busses[i].match(np))
400 return &of_busses[i];
401 BUG();
402 return NULL;
403}
404
405static int of_translate_one(struct device_node *parent, struct of_bus *bus,
Kim Phillips47b1e682012-10-08 19:41:58 -0500406 struct of_bus *pbus, __be32 *addr,
Grant Likelydbbdee92010-06-08 07:48:10 -0600407 int na, int ns, int pna, const char *rprop)
408{
Sebastian Andrzej Siewior0131d892010-12-01 10:54:46 +0100409 const __be32 *ranges;
Grant Likelydbbdee92010-06-08 07:48:10 -0600410 unsigned int rlen;
411 int rone;
412 u64 offset = OF_BAD_ADDR;
413
414 /* Normally, an absence of a "ranges" property means we are
415 * crossing a non-translatable boundary, and thus the addresses
416 * below the current not cannot be converted to CPU physical ones.
417 * Unfortunately, while this is very clear in the spec, it's not
418 * what Apple understood, and they do have things like /uni-n or
419 * /ht nodes with no "ranges" property and a lot of perfectly
420 * useable mapped devices below them. Thus we treat the absence of
421 * "ranges" as equivalent to an empty "ranges" property which means
422 * a 1:1 translation at that level. It's up to the caller not to try
423 * to translate addresses that aren't supposed to be translated in
424 * the first place. --BenH.
Grant Likely3930f292010-06-08 07:48:11 -0600425 *
426 * As far as we know, this damage only exists on Apple machines, so
427 * This code is only enabled on powerpc. --gcl
Grant Likelydbbdee92010-06-08 07:48:10 -0600428 */
429 ranges = of_get_property(parent, rprop, &rlen);
Grant Likely3930f292010-06-08 07:48:11 -0600430#if !defined(CONFIG_PPC)
431 if (ranges == NULL) {
432 pr_err("OF: no ranges; cannot translate\n");
433 return 1;
434 }
435#endif /* !defined(CONFIG_PPC) */
Grant Likelydbbdee92010-06-08 07:48:10 -0600436 if (ranges == NULL || rlen == 0) {
437 offset = of_read_number(addr, na);
438 memset(addr, 0, pna * 4);
Grant Likely3930f292010-06-08 07:48:11 -0600439 pr_debug("OF: empty ranges; 1:1 translation\n");
Grant Likelydbbdee92010-06-08 07:48:10 -0600440 goto finish;
441 }
442
443 pr_debug("OF: walking ranges...\n");
444
445 /* Now walk through the ranges */
446 rlen /= 4;
447 rone = na + pna + ns;
448 for (; rlen >= rone; rlen -= rone, ranges += rone) {
449 offset = bus->map(addr, ranges, na, ns, pna);
450 if (offset != OF_BAD_ADDR)
451 break;
452 }
453 if (offset == OF_BAD_ADDR) {
454 pr_debug("OF: not found !\n");
455 return 1;
456 }
457 memcpy(addr, ranges + na, 4 * pna);
458
459 finish:
460 of_dump_addr("OF: parent translation for:", addr, pna);
461 pr_debug("OF: with offset: %llx\n", (unsigned long long)offset);
462
463 /* Translate it into parent bus space */
464 return pbus->translate(addr, offset, pna);
465}
466
467/*
468 * Translate an address from the device-tree into a CPU physical address,
469 * this walks up the tree and applies the various bus mappings on the
470 * way.
471 *
472 * Note: We consider that crossing any level with #size-cells == 0 to mean
473 * that translation is impossible (that is we are not dealing with a value
474 * that can be mapped to a cpu physical address). This is not really specified
475 * that way, but this is traditionally the way IBM at least do things
476 */
Kim Phillips47b1e682012-10-08 19:41:58 -0500477static u64 __of_translate_address(struct device_node *dev,
478 const __be32 *in_addr, const char *rprop)
Grant Likelydbbdee92010-06-08 07:48:10 -0600479{
480 struct device_node *parent = NULL;
481 struct of_bus *bus, *pbus;
Kim Phillips47b1e682012-10-08 19:41:58 -0500482 __be32 addr[OF_MAX_ADDR_CELLS];
Grant Likelydbbdee92010-06-08 07:48:10 -0600483 int na, ns, pna, pns;
484 u64 result = OF_BAD_ADDR;
485
Grant Likely88048272013-09-19 11:01:52 -0500486 pr_debug("OF: ** translation for device %s **\n", of_node_full_name(dev));
Grant Likelydbbdee92010-06-08 07:48:10 -0600487
488 /* Increase refcount at current level */
489 of_node_get(dev);
490
491 /* Get parent & match bus type */
492 parent = of_get_parent(dev);
493 if (parent == NULL)
494 goto bail;
495 bus = of_match_bus(parent);
496
Andrew Murray59f5ca42012-12-13 10:11:23 +0000497 /* Count address cells & copy address locally */
Grant Likelydbbdee92010-06-08 07:48:10 -0600498 bus->count_cells(dev, &na, &ns);
499 if (!OF_CHECK_COUNTS(na, ns)) {
500 printk(KERN_ERR "prom_parse: Bad cell count for %s\n",
Grant Likely88048272013-09-19 11:01:52 -0500501 of_node_full_name(dev));
Grant Likelydbbdee92010-06-08 07:48:10 -0600502 goto bail;
503 }
504 memcpy(addr, in_addr, na * 4);
505
506 pr_debug("OF: bus is %s (na=%d, ns=%d) on %s\n",
Grant Likely88048272013-09-19 11:01:52 -0500507 bus->name, na, ns, of_node_full_name(parent));
Grant Likelydbbdee92010-06-08 07:48:10 -0600508 of_dump_addr("OF: translating address:", addr, na);
509
510 /* Translate */
511 for (;;) {
512 /* Switch to parent bus */
513 of_node_put(dev);
514 dev = parent;
515 parent = of_get_parent(dev);
516
517 /* If root, we have finished */
518 if (parent == NULL) {
519 pr_debug("OF: reached root node\n");
520 result = of_read_number(addr, na);
521 break;
522 }
523
524 /* Get new parent bus and counts */
525 pbus = of_match_bus(parent);
526 pbus->count_cells(dev, &pna, &pns);
527 if (!OF_CHECK_COUNTS(pna, pns)) {
528 printk(KERN_ERR "prom_parse: Bad cell count for %s\n",
Grant Likely0c02c802013-09-19 11:22:36 -0500529 of_node_full_name(dev));
Grant Likelydbbdee92010-06-08 07:48:10 -0600530 break;
531 }
532
533 pr_debug("OF: parent bus is %s (na=%d, ns=%d) on %s\n",
Grant Likely0c02c802013-09-19 11:22:36 -0500534 pbus->name, pna, pns, of_node_full_name(parent));
Grant Likelydbbdee92010-06-08 07:48:10 -0600535
536 /* Apply bus translation */
537 if (of_translate_one(dev, bus, pbus, addr, na, ns, pna, rprop))
538 break;
539
540 /* Complete the move up one level */
541 na = pna;
542 ns = pns;
543 bus = pbus;
544
545 of_dump_addr("OF: one level translation:", addr, na);
546 }
547 bail:
548 of_node_put(parent);
549 of_node_put(dev);
550
551 return result;
552}
553
Sebastian Andrzej Siewior0131d892010-12-01 10:54:46 +0100554u64 of_translate_address(struct device_node *dev, const __be32 *in_addr)
Grant Likelydbbdee92010-06-08 07:48:10 -0600555{
556 return __of_translate_address(dev, in_addr, "ranges");
557}
558EXPORT_SYMBOL(of_translate_address);
559
Sebastian Andrzej Siewior0131d892010-12-01 10:54:46 +0100560u64 of_translate_dma_address(struct device_node *dev, const __be32 *in_addr)
Grant Likelydbbdee92010-06-08 07:48:10 -0600561{
562 return __of_translate_address(dev, in_addr, "dma-ranges");
563}
564EXPORT_SYMBOL(of_translate_dma_address);
565
Stephen Warren5d61b162012-07-25 17:34:37 -0600566bool of_can_translate_address(struct device_node *dev)
567{
568 struct device_node *parent;
569 struct of_bus *bus;
570 int na, ns;
571
572 parent = of_get_parent(dev);
573 if (parent == NULL)
574 return false;
575
576 bus = of_match_bus(parent);
577 bus->count_cells(dev, &na, &ns);
578
579 of_node_put(parent);
580
581 return OF_CHECK_COUNTS(na, ns);
582}
583EXPORT_SYMBOL(of_can_translate_address);
584
Sebastian Andrzej Siewior0131d892010-12-01 10:54:46 +0100585const __be32 *of_get_address(struct device_node *dev, int index, u64 *size,
Grant Likelydbbdee92010-06-08 07:48:10 -0600586 unsigned int *flags)
587{
Sebastian Andrzej Siewior0131d892010-12-01 10:54:46 +0100588 const __be32 *prop;
Grant Likelydbbdee92010-06-08 07:48:10 -0600589 unsigned int psize;
590 struct device_node *parent;
591 struct of_bus *bus;
592 int onesize, i, na, ns;
593
594 /* Get parent & match bus type */
595 parent = of_get_parent(dev);
596 if (parent == NULL)
597 return NULL;
598 bus = of_match_bus(parent);
599 bus->count_cells(dev, &na, &ns);
600 of_node_put(parent);
Stephen Warren5d61b162012-07-25 17:34:37 -0600601 if (!OF_CHECK_ADDR_COUNT(na))
Grant Likelydbbdee92010-06-08 07:48:10 -0600602 return NULL;
603
604 /* Get "reg" or "assigned-addresses" property */
605 prop = of_get_property(dev, bus->addresses, &psize);
606 if (prop == NULL)
607 return NULL;
608 psize /= 4;
609
610 onesize = na + ns;
611 for (i = 0; psize >= onesize; psize -= onesize, prop += onesize, i++)
612 if (i == index) {
613 if (size)
614 *size = of_read_number(prop + na, ns);
615 if (flags)
616 *flags = bus->get_flags(prop);
617 return prop;
618 }
619 return NULL;
620}
621EXPORT_SYMBOL(of_get_address);
622
Rob Herring25ff7942013-09-07 14:07:11 -0500623unsigned long __weak pci_address_to_pio(phys_addr_t address)
624{
625 if (address > IO_SPACE_LIMIT)
626 return (unsigned long)-1;
627
628 return (unsigned long) address;
629}
630
Sebastian Andrzej Siewior0131d892010-12-01 10:54:46 +0100631static int __of_address_to_resource(struct device_node *dev,
632 const __be32 *addrp, u64 size, unsigned int flags,
Benoit Cousson35f3da32011-12-05 15:23:55 +0100633 const char *name, struct resource *r)
Grant Likely1f5bef32010-06-08 07:48:09 -0600634{
635 u64 taddr;
636
637 if ((flags & (IORESOURCE_IO | IORESOURCE_MEM)) == 0)
638 return -EINVAL;
639 taddr = of_translate_address(dev, addrp);
640 if (taddr == OF_BAD_ADDR)
641 return -EINVAL;
642 memset(r, 0, sizeof(struct resource));
643 if (flags & IORESOURCE_IO) {
644 unsigned long port;
645 port = pci_address_to_pio(taddr);
646 if (port == (unsigned long)-1)
647 return -EINVAL;
648 r->start = port;
649 r->end = port + size - 1;
650 } else {
651 r->start = taddr;
652 r->end = taddr + size - 1;
653 }
654 r->flags = flags;
Benoit Cousson35f3da32011-12-05 15:23:55 +0100655 r->name = name ? name : dev->full_name;
656
Grant Likely1f5bef32010-06-08 07:48:09 -0600657 return 0;
658}
659
660/**
661 * of_address_to_resource - Translate device tree address and return as resource
662 *
663 * Note that if your address is a PIO address, the conversion will fail if
664 * the physical address can't be internally converted to an IO token with
665 * pci_address_to_pio(), that is because it's either called to early or it
666 * can't be matched to any host bridge IO space
667 */
668int of_address_to_resource(struct device_node *dev, int index,
669 struct resource *r)
670{
Sebastian Andrzej Siewior0131d892010-12-01 10:54:46 +0100671 const __be32 *addrp;
Grant Likely1f5bef32010-06-08 07:48:09 -0600672 u64 size;
673 unsigned int flags;
Benoit Cousson35f3da32011-12-05 15:23:55 +0100674 const char *name = NULL;
Grant Likely1f5bef32010-06-08 07:48:09 -0600675
676 addrp = of_get_address(dev, index, &size, &flags);
677 if (addrp == NULL)
678 return -EINVAL;
Benoit Cousson35f3da32011-12-05 15:23:55 +0100679
680 /* Get optional "reg-names" property to add a name to a resource */
681 of_property_read_string_index(dev, "reg-names", index, &name);
682
683 return __of_address_to_resource(dev, addrp, size, flags, name, r);
Grant Likely1f5bef32010-06-08 07:48:09 -0600684}
685EXPORT_SYMBOL_GPL(of_address_to_resource);
686
Grant Likely90e33f62011-07-05 23:42:28 -0600687struct device_node *of_find_matching_node_by_address(struct device_node *from,
688 const struct of_device_id *matches,
689 u64 base_address)
690{
691 struct device_node *dn = of_find_matching_node(from, matches);
692 struct resource res;
693
694 while (dn) {
695 if (of_address_to_resource(dn, 0, &res))
696 continue;
697 if (res.start == base_address)
698 return dn;
699 dn = of_find_matching_node(dn, matches);
700 }
701
702 return NULL;
703}
704
Grant Likely1f5bef32010-06-08 07:48:09 -0600705
Grant Likely6b884a82010-06-08 07:48:09 -0600706/**
707 * of_iomap - Maps the memory mapped IO for a given device_node
708 * @device: the device whose io range will be mapped
709 * @index: index of the io range
710 *
711 * Returns a pointer to the mapped memory
712 */
713void __iomem *of_iomap(struct device_node *np, int index)
714{
715 struct resource res;
716
717 if (of_address_to_resource(np, index, &res))
718 return NULL;
719
Felipe Balbi28c1b6d2011-07-22 22:47:21 +0300720 return ioremap(res.start, resource_size(&res));
Grant Likely6b884a82010-06-08 07:48:09 -0600721}
722EXPORT_SYMBOL(of_iomap);