blob: 314535fa32c1960ede8f0217f36e2ba6088e8e9d [file] [log] [blame]
Sebastian Andrzej Siewior04bea682011-01-24 09:58:55 +05301#include <linux/kernel.h>
2#include <linux/of_pci.h>
3#include <asm/prom.h>
4
5/**
6 * of_irq_map_pci - Resolve the interrupt for a PCI device
7 * @pdev: the device whose interrupt is to be resolved
8 * @out_irq: structure of_irq filled by this function
9 *
10 * This function resolves the PCI interrupt for a given PCI device. If a
11 * device-node exists for a given pci_dev, it will use normal OF tree
12 * walking. If not, it will implement standard swizzling and walk up the
13 * PCI tree until an device-node is found, at which point it will finish
14 * resolving using the OF tree walking.
15 */
16int of_irq_map_pci(struct pci_dev *pdev, struct of_irq *out_irq)
17{
18 struct device_node *dn, *ppnode;
19 struct pci_dev *ppdev;
20 u32 lspec;
21 __be32 lspec_be;
22 __be32 laddr[3];
23 u8 pin;
24 int rc;
25
26 /* Check if we have a device node, if yes, fallback to standard
27 * device tree parsing
28 */
29 dn = pci_device_to_OF_node(pdev);
30 if (dn) {
31 rc = of_irq_map_one(dn, 0, out_irq);
32 if (!rc)
33 return rc;
34 }
35
36 /* Ok, we don't, time to have fun. Let's start by building up an
37 * interrupt spec. we assume #interrupt-cells is 1, which is standard
38 * for PCI. If you do different, then don't use that routine.
39 */
40 rc = pci_read_config_byte(pdev, PCI_INTERRUPT_PIN, &pin);
41 if (rc != 0)
42 return rc;
43 /* No pin, exit */
44 if (pin == 0)
45 return -ENODEV;
46
47 /* Now we walk up the PCI tree */
48 lspec = pin;
49 for (;;) {
50 /* Get the pci_dev of our parent */
51 ppdev = pdev->bus->self;
52
53 /* Ouch, it's a host bridge... */
54 if (ppdev == NULL) {
55 ppnode = pci_bus_to_OF_node(pdev->bus);
56
57 /* No node for host bridge ? give up */
58 if (ppnode == NULL)
59 return -EINVAL;
60 } else {
61 /* We found a P2P bridge, check if it has a node */
62 ppnode = pci_device_to_OF_node(ppdev);
63 }
64
65 /* Ok, we have found a parent with a device-node, hand over to
66 * the OF parsing code.
67 * We build a unit address from the linux device to be used for
68 * resolution. Note that we use the linux bus number which may
69 * not match your firmware bus numbering.
70 * Fortunately, in most cases, interrupt-map-mask doesn't
71 * include the bus number as part of the matching.
72 * You should still be careful about that though if you intend
73 * to rely on this function (you ship a firmware that doesn't
74 * create device nodes for all PCI devices).
75 */
76 if (ppnode)
77 break;
78
79 /* We can only get here if we hit a P2P bridge with no node,
80 * let's do standard swizzling and try again
81 */
82 lspec = pci_swizzle_interrupt_pin(pdev, lspec);
83 pdev = ppdev;
84 }
85
86 lspec_be = cpu_to_be32(lspec);
87 laddr[0] = cpu_to_be32((pdev->bus->number << 16) | (pdev->devfn << 8));
88 laddr[1] = laddr[2] = cpu_to_be32(0);
89 return of_irq_map_raw(ppnode, &lspec_be, 1, laddr, out_irq);
90}
91EXPORT_SYMBOL_GPL(of_irq_map_pci);