[POWERPC] Remove ioremap64 and fixup_bigphys_addr
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / arch / powerpc / kernel / prom_parse.c
1 #undef DEBUG
2
3 #include <linux/kernel.h>
4 #include <linux/string.h>
5 #include <linux/pci_regs.h>
6 #include <linux/module.h>
7 #include <linux/ioport.h>
8 #include <asm/prom.h>
9 #include <asm/pci-bridge.h>
10
11 #ifdef DEBUG
12 #define DBG(fmt...) do { printk(fmt); } while(0)
13 #else
14 #define DBG(fmt...) do { } while(0)
15 #endif
16
17 #ifdef CONFIG_PPC64
18 #define PRu64 "%lx"
19 #else
20 #define PRu64 "%llx"
21 #endif
22
23 /* Max address size we deal with */
24 #define OF_MAX_ADDR_CELLS 4
25 #define OF_CHECK_COUNTS(na, ns) ((na) > 0 && (na) <= OF_MAX_ADDR_CELLS && \
26 (ns) > 0)
27
28 static struct of_bus *of_match_bus(struct device_node *np);
29 static int __of_address_to_resource(struct device_node *dev,
30 const u32 *addrp, u64 size, unsigned int flags,
31 struct resource *r);
32
33
34 /* Debug utility */
35 #ifdef DEBUG
36 static void of_dump_addr(const char *s, const u32 *addr, int na)
37 {
38 printk("%s", s);
39 while(na--)
40 printk(" %08x", *(addr++));
41 printk("\n");
42 }
43 #else
44 static void of_dump_addr(const char *s, const u32 *addr, int na) { }
45 #endif
46
47
48 /* Callbacks for bus specific translators */
49 struct of_bus {
50 const char *name;
51 const char *addresses;
52 int (*match)(struct device_node *parent);
53 void (*count_cells)(struct device_node *child,
54 int *addrc, int *sizec);
55 u64 (*map)(u32 *addr, const u32 *range,
56 int na, int ns, int pna);
57 int (*translate)(u32 *addr, u64 offset, int na);
58 unsigned int (*get_flags)(const u32 *addr);
59 };
60
61
62 /*
63 * Default translator (generic bus)
64 */
65
66 static void of_bus_default_count_cells(struct device_node *dev,
67 int *addrc, int *sizec)
68 {
69 if (addrc)
70 *addrc = prom_n_addr_cells(dev);
71 if (sizec)
72 *sizec = prom_n_size_cells(dev);
73 }
74
75 static u64 of_bus_default_map(u32 *addr, const u32 *range,
76 int na, int ns, int pna)
77 {
78 u64 cp, s, da;
79
80 cp = of_read_number(range, na);
81 s = of_read_number(range + na + pna, ns);
82 da = of_read_number(addr, na);
83
84 DBG("OF: default map, cp="PRu64", s="PRu64", da="PRu64"\n",
85 cp, s, da);
86
87 if (da < cp || da >= (cp + s))
88 return OF_BAD_ADDR;
89 return da - cp;
90 }
91
92 static int of_bus_default_translate(u32 *addr, u64 offset, int na)
93 {
94 u64 a = of_read_number(addr, na);
95 memset(addr, 0, na * 4);
96 a += offset;
97 if (na > 1)
98 addr[na - 2] = a >> 32;
99 addr[na - 1] = a & 0xffffffffu;
100
101 return 0;
102 }
103
104 static unsigned int of_bus_default_get_flags(const u32 *addr)
105 {
106 return IORESOURCE_MEM;
107 }
108
109
110 #ifdef CONFIG_PCI
111 /*
112 * PCI bus specific translator
113 */
114
115 static int of_bus_pci_match(struct device_node *np)
116 {
117 /* "vci" is for the /chaos bridge on 1st-gen PCI powermacs */
118 return !strcmp(np->type, "pci") || !strcmp(np->type, "vci");
119 }
120
121 static void of_bus_pci_count_cells(struct device_node *np,
122 int *addrc, int *sizec)
123 {
124 if (addrc)
125 *addrc = 3;
126 if (sizec)
127 *sizec = 2;
128 }
129
130 static u64 of_bus_pci_map(u32 *addr, const u32 *range, int na, int ns, int pna)
131 {
132 u64 cp, s, da;
133
134 /* Check address type match */
135 if ((addr[0] ^ range[0]) & 0x03000000)
136 return OF_BAD_ADDR;
137
138 /* Read address values, skipping high cell */
139 cp = of_read_number(range + 1, na - 1);
140 s = of_read_number(range + na + pna, ns);
141 da = of_read_number(addr + 1, na - 1);
142
143 DBG("OF: PCI map, cp="PRu64", s="PRu64", da="PRu64"\n", cp, s, da);
144
145 if (da < cp || da >= (cp + s))
146 return OF_BAD_ADDR;
147 return da - cp;
148 }
149
150 static int of_bus_pci_translate(u32 *addr, u64 offset, int na)
151 {
152 return of_bus_default_translate(addr + 1, offset, na - 1);
153 }
154
155 static unsigned int of_bus_pci_get_flags(const u32 *addr)
156 {
157 unsigned int flags = 0;
158 u32 w = addr[0];
159
160 switch((w >> 24) & 0x03) {
161 case 0x01:
162 flags |= IORESOURCE_IO;
163 case 0x02: /* 32 bits */
164 case 0x03: /* 64 bits */
165 flags |= IORESOURCE_MEM;
166 }
167 if (w & 0x40000000)
168 flags |= IORESOURCE_PREFETCH;
169 return flags;
170 }
171
172 const u32 *of_get_pci_address(struct device_node *dev, int bar_no, u64 *size,
173 unsigned int *flags)
174 {
175 const u32 *prop;
176 unsigned int psize;
177 struct device_node *parent;
178 struct of_bus *bus;
179 int onesize, i, na, ns;
180
181 /* Get parent & match bus type */
182 parent = of_get_parent(dev);
183 if (parent == NULL)
184 return NULL;
185 bus = of_match_bus(parent);
186 if (strcmp(bus->name, "pci")) {
187 of_node_put(parent);
188 return NULL;
189 }
190 bus->count_cells(dev, &na, &ns);
191 of_node_put(parent);
192 if (!OF_CHECK_COUNTS(na, ns))
193 return NULL;
194
195 /* Get "reg" or "assigned-addresses" property */
196 prop = get_property(dev, bus->addresses, &psize);
197 if (prop == NULL)
198 return NULL;
199 psize /= 4;
200
201 onesize = na + ns;
202 for (i = 0; psize >= onesize; psize -= onesize, prop += onesize, i++)
203 if ((prop[0] & 0xff) == ((bar_no * 4) + PCI_BASE_ADDRESS_0)) {
204 if (size)
205 *size = of_read_number(prop + na, ns);
206 if (flags)
207 *flags = bus->get_flags(prop);
208 return prop;
209 }
210 return NULL;
211 }
212 EXPORT_SYMBOL(of_get_pci_address);
213
214 int of_pci_address_to_resource(struct device_node *dev, int bar,
215 struct resource *r)
216 {
217 const u32 *addrp;
218 u64 size;
219 unsigned int flags;
220
221 addrp = of_get_pci_address(dev, bar, &size, &flags);
222 if (addrp == NULL)
223 return -EINVAL;
224 return __of_address_to_resource(dev, addrp, size, flags, r);
225 }
226 EXPORT_SYMBOL_GPL(of_pci_address_to_resource);
227
228 static u8 of_irq_pci_swizzle(u8 slot, u8 pin)
229 {
230 return (((pin - 1) + slot) % 4) + 1;
231 }
232
233 int of_irq_map_pci(struct pci_dev *pdev, struct of_irq *out_irq)
234 {
235 struct device_node *dn, *ppnode;
236 struct pci_dev *ppdev;
237 u32 lspec;
238 u32 laddr[3];
239 u8 pin;
240 int rc;
241
242 /* Check if we have a device node, if yes, fallback to standard OF
243 * parsing
244 */
245 dn = pci_device_to_OF_node(pdev);
246 if (dn)
247 return of_irq_map_one(dn, 0, out_irq);
248
249 /* Ok, we don't, time to have fun. Let's start by building up an
250 * interrupt spec. we assume #interrupt-cells is 1, which is standard
251 * for PCI. If you do different, then don't use that routine.
252 */
253 rc = pci_read_config_byte(pdev, PCI_INTERRUPT_PIN, &pin);
254 if (rc != 0)
255 return rc;
256 /* No pin, exit */
257 if (pin == 0)
258 return -ENODEV;
259
260 /* Now we walk up the PCI tree */
261 lspec = pin;
262 for (;;) {
263 /* Get the pci_dev of our parent */
264 ppdev = pdev->bus->self;
265
266 /* Ouch, it's a host bridge... */
267 if (ppdev == NULL) {
268 #ifdef CONFIG_PPC64
269 ppnode = pci_bus_to_OF_node(pdev->bus);
270 #else
271 struct pci_controller *host;
272 host = pci_bus_to_host(pdev->bus);
273 ppnode = host ? host->arch_data : NULL;
274 #endif
275 /* No node for host bridge ? give up */
276 if (ppnode == NULL)
277 return -EINVAL;
278 } else
279 /* We found a P2P bridge, check if it has a node */
280 ppnode = pci_device_to_OF_node(ppdev);
281
282 /* Ok, we have found a parent with a device-node, hand over to
283 * the OF parsing code.
284 * We build a unit address from the linux device to be used for
285 * resolution. Note that we use the linux bus number which may
286 * not match your firmware bus numbering.
287 * Fortunately, in most cases, interrupt-map-mask doesn't include
288 * the bus number as part of the matching.
289 * You should still be careful about that though if you intend
290 * to rely on this function (you ship a firmware that doesn't
291 * create device nodes for all PCI devices).
292 */
293 if (ppnode)
294 break;
295
296 /* We can only get here if we hit a P2P bridge with no node,
297 * let's do standard swizzling and try again
298 */
299 lspec = of_irq_pci_swizzle(PCI_SLOT(pdev->devfn), lspec);
300 pdev = ppdev;
301 }
302
303 laddr[0] = (pdev->bus->number << 16)
304 | (pdev->devfn << 8);
305 laddr[1] = laddr[2] = 0;
306 return of_irq_map_raw(ppnode, &lspec, 1, laddr, out_irq);
307 }
308 EXPORT_SYMBOL_GPL(of_irq_map_pci);
309 #endif /* CONFIG_PCI */
310
311 /*
312 * ISA bus specific translator
313 */
314
315 static int of_bus_isa_match(struct device_node *np)
316 {
317 return !strcmp(np->name, "isa");
318 }
319
320 static void of_bus_isa_count_cells(struct device_node *child,
321 int *addrc, int *sizec)
322 {
323 if (addrc)
324 *addrc = 2;
325 if (sizec)
326 *sizec = 1;
327 }
328
329 static u64 of_bus_isa_map(u32 *addr, const u32 *range, int na, int ns, int pna)
330 {
331 u64 cp, s, da;
332
333 /* Check address type match */
334 if ((addr[0] ^ range[0]) & 0x00000001)
335 return OF_BAD_ADDR;
336
337 /* Read address values, skipping high cell */
338 cp = of_read_number(range + 1, na - 1);
339 s = of_read_number(range + na + pna, ns);
340 da = of_read_number(addr + 1, na - 1);
341
342 DBG("OF: ISA map, cp="PRu64", s="PRu64", da="PRu64"\n", cp, s, da);
343
344 if (da < cp || da >= (cp + s))
345 return OF_BAD_ADDR;
346 return da - cp;
347 }
348
349 static int of_bus_isa_translate(u32 *addr, u64 offset, int na)
350 {
351 return of_bus_default_translate(addr + 1, offset, na - 1);
352 }
353
354 static unsigned int of_bus_isa_get_flags(const u32 *addr)
355 {
356 unsigned int flags = 0;
357 u32 w = addr[0];
358
359 if (w & 1)
360 flags |= IORESOURCE_IO;
361 else
362 flags |= IORESOURCE_MEM;
363 return flags;
364 }
365
366
367 /*
368 * Array of bus specific translators
369 */
370
371 static struct of_bus of_busses[] = {
372 #ifdef CONFIG_PCI
373 /* PCI */
374 {
375 .name = "pci",
376 .addresses = "assigned-addresses",
377 .match = of_bus_pci_match,
378 .count_cells = of_bus_pci_count_cells,
379 .map = of_bus_pci_map,
380 .translate = of_bus_pci_translate,
381 .get_flags = of_bus_pci_get_flags,
382 },
383 #endif /* CONFIG_PCI */
384 /* ISA */
385 {
386 .name = "isa",
387 .addresses = "reg",
388 .match = of_bus_isa_match,
389 .count_cells = of_bus_isa_count_cells,
390 .map = of_bus_isa_map,
391 .translate = of_bus_isa_translate,
392 .get_flags = of_bus_isa_get_flags,
393 },
394 /* Default */
395 {
396 .name = "default",
397 .addresses = "reg",
398 .match = NULL,
399 .count_cells = of_bus_default_count_cells,
400 .map = of_bus_default_map,
401 .translate = of_bus_default_translate,
402 .get_flags = of_bus_default_get_flags,
403 },
404 };
405
406 static struct of_bus *of_match_bus(struct device_node *np)
407 {
408 int i;
409
410 for (i = 0; i < ARRAY_SIZE(of_busses); i ++)
411 if (!of_busses[i].match || of_busses[i].match(np))
412 return &of_busses[i];
413 BUG();
414 return NULL;
415 }
416
417 static int of_translate_one(struct device_node *parent, struct of_bus *bus,
418 struct of_bus *pbus, u32 *addr,
419 int na, int ns, int pna)
420 {
421 const u32 *ranges;
422 unsigned int rlen;
423 int rone;
424 u64 offset = OF_BAD_ADDR;
425
426 /* Normally, an absence of a "ranges" property means we are
427 * crossing a non-translatable boundary, and thus the addresses
428 * below the current not cannot be converted to CPU physical ones.
429 * Unfortunately, while this is very clear in the spec, it's not
430 * what Apple understood, and they do have things like /uni-n or
431 * /ht nodes with no "ranges" property and a lot of perfectly
432 * useable mapped devices below them. Thus we treat the absence of
433 * "ranges" as equivalent to an empty "ranges" property which means
434 * a 1:1 translation at that level. It's up to the caller not to try
435 * to translate addresses that aren't supposed to be translated in
436 * the first place. --BenH.
437 */
438 ranges = get_property(parent, "ranges", &rlen);
439 if (ranges == NULL || rlen == 0) {
440 offset = of_read_number(addr, na);
441 memset(addr, 0, pna * 4);
442 DBG("OF: no ranges, 1:1 translation\n");
443 goto finish;
444 }
445
446 DBG("OF: walking ranges...\n");
447
448 /* Now walk through the ranges */
449 rlen /= 4;
450 rone = na + pna + ns;
451 for (; rlen >= rone; rlen -= rone, ranges += rone) {
452 offset = bus->map(addr, ranges, na, ns, pna);
453 if (offset != OF_BAD_ADDR)
454 break;
455 }
456 if (offset == OF_BAD_ADDR) {
457 DBG("OF: not found !\n");
458 return 1;
459 }
460 memcpy(addr, ranges + na, 4 * pna);
461
462 finish:
463 of_dump_addr("OF: parent translation for:", addr, pna);
464 DBG("OF: with offset: "PRu64"\n", offset);
465
466 /* Translate it into parent bus space */
467 return pbus->translate(addr, offset, pna);
468 }
469
470
471 /*
472 * Translate an address from the device-tree into a CPU physical address,
473 * this walks up the tree and applies the various bus mappings on the
474 * way.
475 *
476 * Note: We consider that crossing any level with #size-cells == 0 to mean
477 * that translation is impossible (that is we are not dealing with a value
478 * that can be mapped to a cpu physical address). This is not really specified
479 * that way, but this is traditionally the way IBM at least do things
480 */
481 u64 of_translate_address(struct device_node *dev, const u32 *in_addr)
482 {
483 struct device_node *parent = NULL;
484 struct of_bus *bus, *pbus;
485 u32 addr[OF_MAX_ADDR_CELLS];
486 int na, ns, pna, pns;
487 u64 result = OF_BAD_ADDR;
488
489 DBG("OF: ** translation for device %s **\n", dev->full_name);
490
491 /* Increase refcount at current level */
492 of_node_get(dev);
493
494 /* Get parent & match bus type */
495 parent = of_get_parent(dev);
496 if (parent == NULL)
497 goto bail;
498 bus = of_match_bus(parent);
499
500 /* Cound address cells & copy address locally */
501 bus->count_cells(dev, &na, &ns);
502 if (!OF_CHECK_COUNTS(na, ns)) {
503 printk(KERN_ERR "prom_parse: Bad cell count for %s\n",
504 dev->full_name);
505 goto bail;
506 }
507 memcpy(addr, in_addr, na * 4);
508
509 DBG("OF: bus is %s (na=%d, ns=%d) on %s\n",
510 bus->name, na, ns, parent->full_name);
511 of_dump_addr("OF: translating address:", addr, na);
512
513 /* Translate */
514 for (;;) {
515 /* Switch to parent bus */
516 of_node_put(dev);
517 dev = parent;
518 parent = of_get_parent(dev);
519
520 /* If root, we have finished */
521 if (parent == NULL) {
522 DBG("OF: reached root node\n");
523 result = of_read_number(addr, na);
524 break;
525 }
526
527 /* Get new parent bus and counts */
528 pbus = of_match_bus(parent);
529 pbus->count_cells(dev, &pna, &pns);
530 if (!OF_CHECK_COUNTS(pna, pns)) {
531 printk(KERN_ERR "prom_parse: Bad cell count for %s\n",
532 dev->full_name);
533 break;
534 }
535
536 DBG("OF: parent bus is %s (na=%d, ns=%d) on %s\n",
537 pbus->name, pna, pns, parent->full_name);
538
539 /* Apply bus translation */
540 if (of_translate_one(dev, bus, pbus, addr, na, ns, pna))
541 break;
542
543 /* Complete the move up one level */
544 na = pna;
545 ns = pns;
546 bus = pbus;
547
548 of_dump_addr("OF: one level translation:", addr, na);
549 }
550 bail:
551 of_node_put(parent);
552 of_node_put(dev);
553
554 return result;
555 }
556 EXPORT_SYMBOL(of_translate_address);
557
558 const u32 *of_get_address(struct device_node *dev, int index, u64 *size,
559 unsigned int *flags)
560 {
561 const u32 *prop;
562 unsigned int psize;
563 struct device_node *parent;
564 struct of_bus *bus;
565 int onesize, i, na, ns;
566
567 /* Get parent & match bus type */
568 parent = of_get_parent(dev);
569 if (parent == NULL)
570 return NULL;
571 bus = of_match_bus(parent);
572 bus->count_cells(dev, &na, &ns);
573 of_node_put(parent);
574 if (!OF_CHECK_COUNTS(na, ns))
575 return NULL;
576
577 /* Get "reg" or "assigned-addresses" property */
578 prop = get_property(dev, bus->addresses, &psize);
579 if (prop == NULL)
580 return NULL;
581 psize /= 4;
582
583 onesize = na + ns;
584 for (i = 0; psize >= onesize; psize -= onesize, prop += onesize, i++)
585 if (i == index) {
586 if (size)
587 *size = of_read_number(prop + na, ns);
588 if (flags)
589 *flags = bus->get_flags(prop);
590 return prop;
591 }
592 return NULL;
593 }
594 EXPORT_SYMBOL(of_get_address);
595
596 static int __of_address_to_resource(struct device_node *dev, const u32 *addrp,
597 u64 size, unsigned int flags,
598 struct resource *r)
599 {
600 u64 taddr;
601
602 if ((flags & (IORESOURCE_IO | IORESOURCE_MEM)) == 0)
603 return -EINVAL;
604 taddr = of_translate_address(dev, addrp);
605 if (taddr == OF_BAD_ADDR)
606 return -EINVAL;
607 memset(r, 0, sizeof(struct resource));
608 if (flags & IORESOURCE_IO) {
609 unsigned long port;
610 port = pci_address_to_pio(taddr);
611 if (port == (unsigned long)-1)
612 return -EINVAL;
613 r->start = port;
614 r->end = port + size - 1;
615 } else {
616 r->start = taddr;
617 r->end = taddr + size - 1;
618 }
619 r->flags = flags;
620 r->name = dev->name;
621 return 0;
622 }
623
624 int of_address_to_resource(struct device_node *dev, int index,
625 struct resource *r)
626 {
627 const u32 *addrp;
628 u64 size;
629 unsigned int flags;
630
631 addrp = of_get_address(dev, index, &size, &flags);
632 if (addrp == NULL)
633 return -EINVAL;
634 return __of_address_to_resource(dev, addrp, size, flags, r);
635 }
636 EXPORT_SYMBOL_GPL(of_address_to_resource);
637
638 void of_parse_dma_window(struct device_node *dn, const void *dma_window_prop,
639 unsigned long *busno, unsigned long *phys, unsigned long *size)
640 {
641 const u32 *dma_window;
642 u32 cells;
643 const unsigned char *prop;
644
645 dma_window = dma_window_prop;
646
647 /* busno is always one cell */
648 *busno = *(dma_window++);
649
650 prop = get_property(dn, "ibm,#dma-address-cells", NULL);
651 if (!prop)
652 prop = get_property(dn, "#address-cells", NULL);
653
654 cells = prop ? *(u32 *)prop : prom_n_addr_cells(dn);
655 *phys = of_read_number(dma_window, cells);
656
657 dma_window += cells;
658
659 prop = get_property(dn, "ibm,#dma-size-cells", NULL);
660 cells = prop ? *(u32 *)prop : prom_n_size_cells(dn);
661 *size = of_read_number(dma_window, cells);
662 }
663
664 /*
665 * Interrupt remapper
666 */
667
668 static unsigned int of_irq_workarounds;
669 static struct device_node *of_irq_dflt_pic;
670
671 static struct device_node *of_irq_find_parent(struct device_node *child)
672 {
673 struct device_node *p;
674 const phandle *parp;
675
676 if (!of_node_get(child))
677 return NULL;
678
679 do {
680 parp = get_property(child, "interrupt-parent", NULL);
681 if (parp == NULL)
682 p = of_get_parent(child);
683 else {
684 if (of_irq_workarounds & OF_IMAP_NO_PHANDLE)
685 p = of_node_get(of_irq_dflt_pic);
686 else
687 p = of_find_node_by_phandle(*parp);
688 }
689 of_node_put(child);
690 child = p;
691 } while (p && get_property(p, "#interrupt-cells", NULL) == NULL);
692
693 return p;
694 }
695
696 /* This doesn't need to be called if you don't have any special workaround
697 * flags to pass
698 */
699 void of_irq_map_init(unsigned int flags)
700 {
701 of_irq_workarounds = flags;
702
703 /* OldWorld, don't bother looking at other things */
704 if (flags & OF_IMAP_OLDWORLD_MAC)
705 return;
706
707 /* If we don't have phandles, let's try to locate a default interrupt
708 * controller (happens when booting with BootX). We do a first match
709 * here, hopefully, that only ever happens on machines with one
710 * controller.
711 */
712 if (flags & OF_IMAP_NO_PHANDLE) {
713 struct device_node *np;
714
715 for(np = NULL; (np = of_find_all_nodes(np)) != NULL;) {
716 if (get_property(np, "interrupt-controller", NULL)
717 == NULL)
718 continue;
719 /* Skip /chosen/interrupt-controller */
720 if (strcmp(np->name, "chosen") == 0)
721 continue;
722 /* It seems like at least one person on this planet wants
723 * to use BootX on a machine with an AppleKiwi controller
724 * which happens to pretend to be an interrupt
725 * controller too.
726 */
727 if (strcmp(np->name, "AppleKiwi") == 0)
728 continue;
729 /* I think we found one ! */
730 of_irq_dflt_pic = np;
731 break;
732 }
733 }
734
735 }
736
737 int of_irq_map_raw(struct device_node *parent, const u32 *intspec, u32 ointsize,
738 const u32 *addr, struct of_irq *out_irq)
739 {
740 struct device_node *ipar, *tnode, *old = NULL, *newpar = NULL;
741 const u32 *tmp, *imap, *imask;
742 u32 intsize = 1, addrsize, newintsize = 0, newaddrsize = 0;
743 int imaplen, match, i;
744
745 DBG("of_irq_map_raw: par=%s,intspec=[0x%08x 0x%08x...],ointsize=%d\n",
746 parent->full_name, intspec[0], intspec[1], ointsize);
747
748 ipar = of_node_get(parent);
749
750 /* First get the #interrupt-cells property of the current cursor
751 * that tells us how to interpret the passed-in intspec. If there
752 * is none, we are nice and just walk up the tree
753 */
754 do {
755 tmp = get_property(ipar, "#interrupt-cells", NULL);
756 if (tmp != NULL) {
757 intsize = *tmp;
758 break;
759 }
760 tnode = ipar;
761 ipar = of_irq_find_parent(ipar);
762 of_node_put(tnode);
763 } while (ipar);
764 if (ipar == NULL) {
765 DBG(" -> no parent found !\n");
766 goto fail;
767 }
768
769 DBG("of_irq_map_raw: ipar=%s, size=%d\n", ipar->full_name, intsize);
770
771 if (ointsize != intsize)
772 return -EINVAL;
773
774 /* Look for this #address-cells. We have to implement the old linux
775 * trick of looking for the parent here as some device-trees rely on it
776 */
777 old = of_node_get(ipar);
778 do {
779 tmp = get_property(old, "#address-cells", NULL);
780 tnode = of_get_parent(old);
781 of_node_put(old);
782 old = tnode;
783 } while(old && tmp == NULL);
784 of_node_put(old);
785 old = NULL;
786 addrsize = (tmp == NULL) ? 2 : *tmp;
787
788 DBG(" -> addrsize=%d\n", addrsize);
789
790 /* Now start the actual "proper" walk of the interrupt tree */
791 while (ipar != NULL) {
792 /* Now check if cursor is an interrupt-controller and if it is
793 * then we are done
794 */
795 if (get_property(ipar, "interrupt-controller", NULL) != NULL) {
796 DBG(" -> got it !\n");
797 memcpy(out_irq->specifier, intspec,
798 intsize * sizeof(u32));
799 out_irq->size = intsize;
800 out_irq->controller = ipar;
801 of_node_put(old);
802 return 0;
803 }
804
805 /* Now look for an interrupt-map */
806 imap = get_property(ipar, "interrupt-map", &imaplen);
807 /* No interrupt map, check for an interrupt parent */
808 if (imap == NULL) {
809 DBG(" -> no map, getting parent\n");
810 newpar = of_irq_find_parent(ipar);
811 goto skiplevel;
812 }
813 imaplen /= sizeof(u32);
814
815 /* Look for a mask */
816 imask = get_property(ipar, "interrupt-map-mask", NULL);
817
818 /* If we were passed no "reg" property and we attempt to parse
819 * an interrupt-map, then #address-cells must be 0.
820 * Fail if it's not.
821 */
822 if (addr == NULL && addrsize != 0) {
823 DBG(" -> no reg passed in when needed !\n");
824 goto fail;
825 }
826
827 /* Parse interrupt-map */
828 match = 0;
829 while (imaplen > (addrsize + intsize + 1) && !match) {
830 /* Compare specifiers */
831 match = 1;
832 for (i = 0; i < addrsize && match; ++i) {
833 u32 mask = imask ? imask[i] : 0xffffffffu;
834 match = ((addr[i] ^ imap[i]) & mask) == 0;
835 }
836 for (; i < (addrsize + intsize) && match; ++i) {
837 u32 mask = imask ? imask[i] : 0xffffffffu;
838 match =
839 ((intspec[i-addrsize] ^ imap[i]) & mask) == 0;
840 }
841 imap += addrsize + intsize;
842 imaplen -= addrsize + intsize;
843
844 DBG(" -> match=%d (imaplen=%d)\n", match, imaplen);
845
846 /* Get the interrupt parent */
847 if (of_irq_workarounds & OF_IMAP_NO_PHANDLE)
848 newpar = of_node_get(of_irq_dflt_pic);
849 else
850 newpar = of_find_node_by_phandle((phandle)*imap);
851 imap++;
852 --imaplen;
853
854 /* Check if not found */
855 if (newpar == NULL) {
856 DBG(" -> imap parent not found !\n");
857 goto fail;
858 }
859
860 /* Get #interrupt-cells and #address-cells of new
861 * parent
862 */
863 tmp = get_property(newpar, "#interrupt-cells",
864 NULL);
865 if (tmp == NULL) {
866 DBG(" -> parent lacks #interrupt-cells !\n");
867 goto fail;
868 }
869 newintsize = *tmp;
870 tmp = get_property(newpar, "#address-cells",
871 NULL);
872 newaddrsize = (tmp == NULL) ? 0 : *tmp;
873
874 DBG(" -> newintsize=%d, newaddrsize=%d\n",
875 newintsize, newaddrsize);
876
877 /* Check for malformed properties */
878 if (imaplen < (newaddrsize + newintsize))
879 goto fail;
880
881 imap += newaddrsize + newintsize;
882 imaplen -= newaddrsize + newintsize;
883
884 DBG(" -> imaplen=%d\n", imaplen);
885 }
886 if (!match)
887 goto fail;
888
889 of_node_put(old);
890 old = of_node_get(newpar);
891 addrsize = newaddrsize;
892 intsize = newintsize;
893 intspec = imap - intsize;
894 addr = intspec - addrsize;
895
896 skiplevel:
897 /* Iterate again with new parent */
898 DBG(" -> new parent: %s\n", newpar ? newpar->full_name : "<>");
899 of_node_put(ipar);
900 ipar = newpar;
901 newpar = NULL;
902 }
903 fail:
904 of_node_put(ipar);
905 of_node_put(old);
906 of_node_put(newpar);
907
908 return -EINVAL;
909 }
910 EXPORT_SYMBOL_GPL(of_irq_map_raw);
911
912 #if defined(CONFIG_PPC_PMAC) && defined(CONFIG_PPC32)
913 static int of_irq_map_oldworld(struct device_node *device, int index,
914 struct of_irq *out_irq)
915 {
916 const u32 *ints;
917 int intlen;
918
919 /*
920 * Old machines just have a list of interrupt numbers
921 * and no interrupt-controller nodes.
922 */
923 ints = get_property(device, "AAPL,interrupts", &intlen);
924 if (ints == NULL)
925 return -EINVAL;
926 intlen /= sizeof(u32);
927
928 if (index >= intlen)
929 return -EINVAL;
930
931 out_irq->controller = NULL;
932 out_irq->specifier[0] = ints[index];
933 out_irq->size = 1;
934
935 return 0;
936 }
937 #else /* defined(CONFIG_PPC_PMAC) && defined(CONFIG_PPC32) */
938 static int of_irq_map_oldworld(struct device_node *device, int index,
939 struct of_irq *out_irq)
940 {
941 return -EINVAL;
942 }
943 #endif /* !(defined(CONFIG_PPC_PMAC) && defined(CONFIG_PPC32)) */
944
945 int of_irq_map_one(struct device_node *device, int index, struct of_irq *out_irq)
946 {
947 struct device_node *p;
948 const u32 *intspec, *tmp, *addr;
949 u32 intsize, intlen;
950 int res;
951
952 DBG("of_irq_map_one: dev=%s, index=%d\n", device->full_name, index);
953
954 /* OldWorld mac stuff is "special", handle out of line */
955 if (of_irq_workarounds & OF_IMAP_OLDWORLD_MAC)
956 return of_irq_map_oldworld(device, index, out_irq);
957
958 /* Get the interrupts property */
959 intspec = get_property(device, "interrupts", &intlen);
960 if (intspec == NULL)
961 return -EINVAL;
962 intlen /= sizeof(u32);
963
964 /* Get the reg property (if any) */
965 addr = get_property(device, "reg", NULL);
966
967 /* Look for the interrupt parent. */
968 p = of_irq_find_parent(device);
969 if (p == NULL)
970 return -EINVAL;
971
972 /* Get size of interrupt specifier */
973 tmp = get_property(p, "#interrupt-cells", NULL);
974 if (tmp == NULL) {
975 of_node_put(p);
976 return -EINVAL;
977 }
978 intsize = *tmp;
979
980 DBG(" intsize=%d intlen=%d\n", intsize, intlen);
981
982 /* Check index */
983 if ((index + 1) * intsize > intlen)
984 return -EINVAL;
985
986 /* Get new specifier and map it */
987 res = of_irq_map_raw(p, intspec + index * intsize, intsize,
988 addr, out_irq);
989 of_node_put(p);
990 return res;
991 }
992 EXPORT_SYMBOL_GPL(of_irq_map_one);