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