blob: 3701b62c1d5eeb5197351125f95cf7511a072ab1 [file] [log] [blame]
Sebastian Andrzej Siewior04bea682011-01-24 09:58:55 +05301#include <linux/kernel.h>
Grant Likelya6422852011-07-23 23:52:48 -06002#include <linux/of.h>
Sebastian Andrzej Siewior04bea682011-01-24 09:58:55 +05303#include <linux/of_pci.h>
Sebastian Andrzej Siewior04bea682011-01-24 09:58:55 +05304#include <asm/prom.h>
5
Benjamin Herrenschmidt98d9f30c82011-04-11 11:37:07 +10006static inline int __of_pci_pci_compare(struct device_node *node,
7 unsigned int devfn)
Sebastian Andrzej Siewior04bea682011-01-24 09:58:55 +05308{
Benjamin Herrenschmidt98d9f30c82011-04-11 11:37:07 +10009 unsigned int size;
10 const __be32 *reg = of_get_property(node, "reg", &size);
Sebastian Andrzej Siewior04bea682011-01-24 09:58:55 +053011
Benjamin Herrenschmidt98d9f30c82011-04-11 11:37:07 +100012 if (!reg || size < 5 * sizeof(__be32))
13 return 0;
14 return ((be32_to_cpup(&reg[0]) >> 8) & 0xff) == devfn;
Sebastian Andrzej Siewior04bea682011-01-24 09:58:55 +053015}
Benjamin Herrenschmidt98d9f30c82011-04-11 11:37:07 +100016
17struct device_node *of_pci_find_child_device(struct device_node *parent,
18 unsigned int devfn)
19{
20 struct device_node *node, *node2;
21
22 for_each_child_of_node(parent, node) {
23 if (__of_pci_pci_compare(node, devfn))
24 return node;
25 /*
26 * Some OFs create a parent node "multifunc-device" as
27 * a fake root for all functions of a multi-function
28 * device we go down them as well.
29 */
30 if (!strcmp(node->name, "multifunc-device")) {
31 for_each_child_of_node(node, node2) {
32 if (__of_pci_pci_compare(node2, devfn)) {
33 of_node_put(node);
34 return node2;
35 }
36 }
37 }
38 }
39 return NULL;
40}
41EXPORT_SYMBOL_GPL(of_pci_find_child_device);