blob: 13e37e2d8ec16ba8b4179198cfbf759015628da9 [file] [log] [blame]
Sebastian Andrzej Siewior04bea682011-01-24 09:58:55 +05301#include <linux/kernel.h>
Paul Gortmaker97759132011-05-27 17:06:52 -04002#include <linux/export.h>
Grant Likelya6422852011-07-23 23:52:48 -06003#include <linux/of.h>
Sebastian Andrzej Siewior04bea682011-01-24 09:58:55 +05304#include <linux/of_pci.h>
Sebastian Andrzej Siewior04bea682011-01-24 09:58:55 +05305#include <asm/prom.h>
6
Benjamin Herrenschmidt98d9f302011-04-11 11:37:07 +10007static inline int __of_pci_pci_compare(struct device_node *node,
8 unsigned int devfn)
Sebastian Andrzej Siewior04bea682011-01-24 09:58:55 +05309{
Benjamin Herrenschmidt98d9f302011-04-11 11:37:07 +100010 unsigned int size;
11 const __be32 *reg = of_get_property(node, "reg", &size);
Sebastian Andrzej Siewior04bea682011-01-24 09:58:55 +053012
Benjamin Herrenschmidt98d9f302011-04-11 11:37:07 +100013 if (!reg || size < 5 * sizeof(__be32))
14 return 0;
15 return ((be32_to_cpup(&reg[0]) >> 8) & 0xff) == devfn;
Sebastian Andrzej Siewior04bea682011-01-24 09:58:55 +053016}
Benjamin Herrenschmidt98d9f302011-04-11 11:37:07 +100017
18struct device_node *of_pci_find_child_device(struct device_node *parent,
19 unsigned int devfn)
20{
21 struct device_node *node, *node2;
22
23 for_each_child_of_node(parent, node) {
24 if (__of_pci_pci_compare(node, devfn))
25 return node;
26 /*
27 * Some OFs create a parent node "multifunc-device" as
28 * a fake root for all functions of a multi-function
29 * device we go down them as well.
30 */
31 if (!strcmp(node->name, "multifunc-device")) {
32 for_each_child_of_node(node, node2) {
33 if (__of_pci_pci_compare(node2, devfn)) {
34 of_node_put(node);
35 return node2;
36 }
37 }
38 }
39 }
40 return NULL;
41}
42EXPORT_SYMBOL_GPL(of_pci_find_child_device);