ec7b060ae952f0ac6cfd2bcaa134155ddc278ced
[linux-3.10.git] / drivers / of / of_pci.c
1 #include <linux/kernel.h>
2 #include <linux/of_pci.h>
3 #include <asm/prom.h>
4
5 static inline int __of_pci_pci_compare(struct device_node *node,
6                                        unsigned int devfn)
7 {
8         unsigned int size;
9         const __be32 *reg = of_get_property(node, "reg", &size);
10
11         if (!reg || size < 5 * sizeof(__be32))
12                 return 0;
13         return ((be32_to_cpup(&reg[0]) >> 8) & 0xff) == devfn;
14 }
15
16 struct device_node *of_pci_find_child_device(struct device_node *parent,
17                                              unsigned int devfn)
18 {
19         struct device_node *node, *node2;
20
21         for_each_child_of_node(parent, node) {
22                 if (__of_pci_pci_compare(node, devfn))
23                         return node;
24                 /*
25                  * Some OFs create a parent node "multifunc-device" as
26                  * a fake root for all functions of a multi-function
27                  * device we go down them as well.
28                  */
29                 if (!strcmp(node->name, "multifunc-device")) {
30                         for_each_child_of_node(node, node2) {
31                                 if (__of_pci_pci_compare(node2, devfn)) {
32                                         of_node_put(node);
33                                         return node2;
34                                 }
35                         }
36                 }
37         }
38         return NULL;
39 }
40 EXPORT_SYMBOL_GPL(of_pci_find_child_device);