blob: ec7b060ae952f0ac6cfd2bcaa134155ddc278ced [file] [log] [blame]
Sebastian Andrzej Siewior04bea682011-01-24 09:58:55 +05301#include <linux/kernel.h>
2#include <linux/of_pci.h>
Sebastian Andrzej Siewior04bea682011-01-24 09:58:55 +05303#include <asm/prom.h>
4
Benjamin Herrenschmidt98d9f30c82011-04-11 11:37:07 +10005static inline int __of_pci_pci_compare(struct device_node *node,
6 unsigned int devfn)
Sebastian Andrzej Siewior04bea682011-01-24 09:58:55 +05307{
Benjamin Herrenschmidt98d9f30c82011-04-11 11:37:07 +10008 unsigned int size;
9 const __be32 *reg = of_get_property(node, "reg", &size);
Sebastian Andrzej Siewior04bea682011-01-24 09:58:55 +053010
Benjamin Herrenschmidt98d9f30c82011-04-11 11:37:07 +100011 if (!reg || size < 5 * sizeof(__be32))
12 return 0;
13 return ((be32_to_cpup(&reg[0]) >> 8) & 0xff) == devfn;
Sebastian Andrzej Siewior04bea682011-01-24 09:58:55 +053014}
Benjamin Herrenschmidt98d9f30c82011-04-11 11:37:07 +100015
16struct 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}
40EXPORT_SYMBOL_GPL(of_pci_find_child_device);