0b04137f04f7bbaea3acd4b587197c4ddf9e000f
[deliverable/linux.git] / drivers / of / address.c
1
2 #include <linux/io.h>
3 #include <linux/ioport.h>
4 #include <linux/module.h>
5 #include <linux/of_address.h>
6 #include <linux/pci_regs.h>
7 #include <linux/string.h>
8
9 /* Max address size we deal with */
10 #define OF_MAX_ADDR_CELLS 4
11 #define OF_CHECK_COUNTS(na, ns) ((na) > 0 && (na) <= OF_MAX_ADDR_CELLS && \
12 (ns) > 0)
13
14 static struct of_bus *of_match_bus(struct device_node *np);
15 static 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
21 static 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", be32_to_cpu(*(addr++)));
26 printk("\n");
27 }
28 #else
29 static void of_dump_addr(const char *s, const u32 *addr, int na) { }
30 #endif
31
32 /* Callbacks for bus specific translators */
33 struct 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
49 static 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
58 static 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
76 static 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] = cpu_to_be32(a >> 32);
83 addr[na - 1] = cpu_to_be32(a & 0xffffffffu);
84
85 return 0;
86 }
87
88 static 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
98 static 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
104 static 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
113 static 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
132 static 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
158 static 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
163 const 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 u32 val = be32_to_cpu(prop[0]);
195 if ((val & 0xff) == ((bar_no * 4) + PCI_BASE_ADDRESS_0)) {
196 if (size)
197 *size = of_read_number(prop + na, ns);
198 if (flags)
199 *flags = bus->get_flags(prop);
200 return prop;
201 }
202 }
203 return NULL;
204 }
205 EXPORT_SYMBOL(of_get_pci_address);
206
207 int of_pci_address_to_resource(struct device_node *dev, int bar,
208 struct resource *r)
209 {
210 const u32 *addrp;
211 u64 size;
212 unsigned int flags;
213
214 addrp = of_get_pci_address(dev, bar, &size, &flags);
215 if (addrp == NULL)
216 return -EINVAL;
217 return __of_address_to_resource(dev, addrp, size, flags, r);
218 }
219 EXPORT_SYMBOL_GPL(of_pci_address_to_resource);
220 #endif /* CONFIG_PCI */
221
222 /*
223 * ISA bus specific translator
224 */
225
226 static int of_bus_isa_match(struct device_node *np)
227 {
228 return !strcmp(np->name, "isa");
229 }
230
231 static void of_bus_isa_count_cells(struct device_node *child,
232 int *addrc, int *sizec)
233 {
234 if (addrc)
235 *addrc = 2;
236 if (sizec)
237 *sizec = 1;
238 }
239
240 static u64 of_bus_isa_map(u32 *addr, const u32 *range, int na, int ns, int pna)
241 {
242 u64 cp, s, da;
243
244 /* Check address type match */
245 if ((addr[0] ^ range[0]) & 0x00000001)
246 return OF_BAD_ADDR;
247
248 /* Read address values, skipping high cell */
249 cp = of_read_number(range + 1, na - 1);
250 s = of_read_number(range + na + pna, ns);
251 da = of_read_number(addr + 1, na - 1);
252
253 pr_debug("OF: ISA map, cp=%llx, s=%llx, da=%llx\n",
254 (unsigned long long)cp, (unsigned long long)s,
255 (unsigned long long)da);
256
257 if (da < cp || da >= (cp + s))
258 return OF_BAD_ADDR;
259 return da - cp;
260 }
261
262 static int of_bus_isa_translate(u32 *addr, u64 offset, int na)
263 {
264 return of_bus_default_translate(addr + 1, offset, na - 1);
265 }
266
267 static unsigned int of_bus_isa_get_flags(const u32 *addr)
268 {
269 unsigned int flags = 0;
270 u32 w = addr[0];
271
272 if (w & 1)
273 flags |= IORESOURCE_IO;
274 else
275 flags |= IORESOURCE_MEM;
276 return flags;
277 }
278
279 /*
280 * Array of bus specific translators
281 */
282
283 static struct of_bus of_busses[] = {
284 #ifdef CONFIG_PCI
285 /* PCI */
286 {
287 .name = "pci",
288 .addresses = "assigned-addresses",
289 .match = of_bus_pci_match,
290 .count_cells = of_bus_pci_count_cells,
291 .map = of_bus_pci_map,
292 .translate = of_bus_pci_translate,
293 .get_flags = of_bus_pci_get_flags,
294 },
295 #endif /* CONFIG_PCI */
296 /* ISA */
297 {
298 .name = "isa",
299 .addresses = "reg",
300 .match = of_bus_isa_match,
301 .count_cells = of_bus_isa_count_cells,
302 .map = of_bus_isa_map,
303 .translate = of_bus_isa_translate,
304 .get_flags = of_bus_isa_get_flags,
305 },
306 /* Default */
307 {
308 .name = "default",
309 .addresses = "reg",
310 .match = NULL,
311 .count_cells = of_bus_default_count_cells,
312 .map = of_bus_default_map,
313 .translate = of_bus_default_translate,
314 .get_flags = of_bus_default_get_flags,
315 },
316 };
317
318 static struct of_bus *of_match_bus(struct device_node *np)
319 {
320 int i;
321
322 for (i = 0; i < ARRAY_SIZE(of_busses); i++)
323 if (!of_busses[i].match || of_busses[i].match(np))
324 return &of_busses[i];
325 BUG();
326 return NULL;
327 }
328
329 static int of_translate_one(struct device_node *parent, struct of_bus *bus,
330 struct of_bus *pbus, u32 *addr,
331 int na, int ns, int pna, const char *rprop)
332 {
333 const u32 *ranges;
334 unsigned int rlen;
335 int rone;
336 u64 offset = OF_BAD_ADDR;
337
338 /* Normally, an absence of a "ranges" property means we are
339 * crossing a non-translatable boundary, and thus the addresses
340 * below the current not cannot be converted to CPU physical ones.
341 * Unfortunately, while this is very clear in the spec, it's not
342 * what Apple understood, and they do have things like /uni-n or
343 * /ht nodes with no "ranges" property and a lot of perfectly
344 * useable mapped devices below them. Thus we treat the absence of
345 * "ranges" as equivalent to an empty "ranges" property which means
346 * a 1:1 translation at that level. It's up to the caller not to try
347 * to translate addresses that aren't supposed to be translated in
348 * the first place. --BenH.
349 */
350 ranges = of_get_property(parent, rprop, &rlen);
351 if (ranges == NULL || rlen == 0) {
352 offset = of_read_number(addr, na);
353 memset(addr, 0, pna * 4);
354 pr_debug("OF: no ranges, 1:1 translation\n");
355 goto finish;
356 }
357
358 pr_debug("OF: walking ranges...\n");
359
360 /* Now walk through the ranges */
361 rlen /= 4;
362 rone = na + pna + ns;
363 for (; rlen >= rone; rlen -= rone, ranges += rone) {
364 offset = bus->map(addr, ranges, na, ns, pna);
365 if (offset != OF_BAD_ADDR)
366 break;
367 }
368 if (offset == OF_BAD_ADDR) {
369 pr_debug("OF: not found !\n");
370 return 1;
371 }
372 memcpy(addr, ranges + na, 4 * pna);
373
374 finish:
375 of_dump_addr("OF: parent translation for:", addr, pna);
376 pr_debug("OF: with offset: %llx\n", (unsigned long long)offset);
377
378 /* Translate it into parent bus space */
379 return pbus->translate(addr, offset, pna);
380 }
381
382 /*
383 * Translate an address from the device-tree into a CPU physical address,
384 * this walks up the tree and applies the various bus mappings on the
385 * way.
386 *
387 * Note: We consider that crossing any level with #size-cells == 0 to mean
388 * that translation is impossible (that is we are not dealing with a value
389 * that can be mapped to a cpu physical address). This is not really specified
390 * that way, but this is traditionally the way IBM at least do things
391 */
392 u64 __of_translate_address(struct device_node *dev, const u32 *in_addr,
393 const char *rprop)
394 {
395 struct device_node *parent = NULL;
396 struct of_bus *bus, *pbus;
397 u32 addr[OF_MAX_ADDR_CELLS];
398 int na, ns, pna, pns;
399 u64 result = OF_BAD_ADDR;
400
401 pr_debug("OF: ** translation for device %s **\n", dev->full_name);
402
403 /* Increase refcount at current level */
404 of_node_get(dev);
405
406 /* Get parent & match bus type */
407 parent = of_get_parent(dev);
408 if (parent == NULL)
409 goto bail;
410 bus = of_match_bus(parent);
411
412 /* Cound address cells & copy address locally */
413 bus->count_cells(dev, &na, &ns);
414 if (!OF_CHECK_COUNTS(na, ns)) {
415 printk(KERN_ERR "prom_parse: Bad cell count for %s\n",
416 dev->full_name);
417 goto bail;
418 }
419 memcpy(addr, in_addr, na * 4);
420
421 pr_debug("OF: bus is %s (na=%d, ns=%d) on %s\n",
422 bus->name, na, ns, parent->full_name);
423 of_dump_addr("OF: translating address:", addr, na);
424
425 /* Translate */
426 for (;;) {
427 /* Switch to parent bus */
428 of_node_put(dev);
429 dev = parent;
430 parent = of_get_parent(dev);
431
432 /* If root, we have finished */
433 if (parent == NULL) {
434 pr_debug("OF: reached root node\n");
435 result = of_read_number(addr, na);
436 break;
437 }
438
439 /* Get new parent bus and counts */
440 pbus = of_match_bus(parent);
441 pbus->count_cells(dev, &pna, &pns);
442 if (!OF_CHECK_COUNTS(pna, pns)) {
443 printk(KERN_ERR "prom_parse: Bad cell count for %s\n",
444 dev->full_name);
445 break;
446 }
447
448 pr_debug("OF: parent bus is %s (na=%d, ns=%d) on %s\n",
449 pbus->name, pna, pns, parent->full_name);
450
451 /* Apply bus translation */
452 if (of_translate_one(dev, bus, pbus, addr, na, ns, pna, rprop))
453 break;
454
455 /* Complete the move up one level */
456 na = pna;
457 ns = pns;
458 bus = pbus;
459
460 of_dump_addr("OF: one level translation:", addr, na);
461 }
462 bail:
463 of_node_put(parent);
464 of_node_put(dev);
465
466 return result;
467 }
468
469 u64 of_translate_address(struct device_node *dev, const u32 *in_addr)
470 {
471 return __of_translate_address(dev, in_addr, "ranges");
472 }
473 EXPORT_SYMBOL(of_translate_address);
474
475 u64 of_translate_dma_address(struct device_node *dev, const u32 *in_addr)
476 {
477 return __of_translate_address(dev, in_addr, "dma-ranges");
478 }
479 EXPORT_SYMBOL(of_translate_dma_address);
480
481 const u32 *of_get_address(struct device_node *dev, int index, u64 *size,
482 unsigned int *flags)
483 {
484 const u32 *prop;
485 unsigned int psize;
486 struct device_node *parent;
487 struct of_bus *bus;
488 int onesize, i, na, ns;
489
490 /* Get parent & match bus type */
491 parent = of_get_parent(dev);
492 if (parent == NULL)
493 return NULL;
494 bus = of_match_bus(parent);
495 bus->count_cells(dev, &na, &ns);
496 of_node_put(parent);
497 if (!OF_CHECK_COUNTS(na, ns))
498 return NULL;
499
500 /* Get "reg" or "assigned-addresses" property */
501 prop = of_get_property(dev, bus->addresses, &psize);
502 if (prop == NULL)
503 return NULL;
504 psize /= 4;
505
506 onesize = na + ns;
507 for (i = 0; psize >= onesize; psize -= onesize, prop += onesize, i++)
508 if (i == index) {
509 if (size)
510 *size = of_read_number(prop + na, ns);
511 if (flags)
512 *flags = bus->get_flags(prop);
513 return prop;
514 }
515 return NULL;
516 }
517 EXPORT_SYMBOL(of_get_address);
518
519 static int __of_address_to_resource(struct device_node *dev, const u32 *addrp,
520 u64 size, unsigned int flags,
521 struct resource *r)
522 {
523 u64 taddr;
524
525 if ((flags & (IORESOURCE_IO | IORESOURCE_MEM)) == 0)
526 return -EINVAL;
527 taddr = of_translate_address(dev, addrp);
528 if (taddr == OF_BAD_ADDR)
529 return -EINVAL;
530 memset(r, 0, sizeof(struct resource));
531 if (flags & IORESOURCE_IO) {
532 unsigned long port;
533 port = pci_address_to_pio(taddr);
534 if (port == (unsigned long)-1)
535 return -EINVAL;
536 r->start = port;
537 r->end = port + size - 1;
538 } else {
539 r->start = taddr;
540 r->end = taddr + size - 1;
541 }
542 r->flags = flags;
543 r->name = dev->name;
544 return 0;
545 }
546
547 /**
548 * of_address_to_resource - Translate device tree address and return as resource
549 *
550 * Note that if your address is a PIO address, the conversion will fail if
551 * the physical address can't be internally converted to an IO token with
552 * pci_address_to_pio(), that is because it's either called to early or it
553 * can't be matched to any host bridge IO space
554 */
555 int of_address_to_resource(struct device_node *dev, int index,
556 struct resource *r)
557 {
558 const u32 *addrp;
559 u64 size;
560 unsigned int flags;
561
562 addrp = of_get_address(dev, index, &size, &flags);
563 if (addrp == NULL)
564 return -EINVAL;
565 return __of_address_to_resource(dev, addrp, size, flags, r);
566 }
567 EXPORT_SYMBOL_GPL(of_address_to_resource);
568
569
570 /**
571 * of_iomap - Maps the memory mapped IO for a given device_node
572 * @device: the device whose io range will be mapped
573 * @index: index of the io range
574 *
575 * Returns a pointer to the mapped memory
576 */
577 void __iomem *of_iomap(struct device_node *np, int index)
578 {
579 struct resource res;
580
581 if (of_address_to_resource(np, index, &res))
582 return NULL;
583
584 return ioremap(res.start, 1 + res.end - res.start);
585 }
586 EXPORT_SYMBOL(of_iomap);
This page took 0.050629 seconds and 4 git commands to generate.