of_pci.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #include <linux/kernel.h>
  2. #include <linux/export.h>
  3. #include <linux/of.h>
  4. #include <linux/of_pci.h>
  5. #include <asm/prom.h>
  6. static inline int __of_pci_pci_compare(struct device_node *node,
  7. unsigned int data)
  8. {
  9. int devfn;
  10. devfn = of_pci_get_devfn(node);
  11. if (devfn < 0)
  12. return 0;
  13. return devfn == data;
  14. }
  15. struct device_node *of_pci_find_child_device(struct device_node *parent,
  16. unsigned int devfn)
  17. {
  18. struct device_node *node, *node2;
  19. for_each_child_of_node(parent, node) {
  20. if (__of_pci_pci_compare(node, devfn))
  21. return node;
  22. /*
  23. * Some OFs create a parent node "multifunc-device" as
  24. * a fake root for all functions of a multi-function
  25. * device we go down them as well.
  26. */
  27. if (!strcmp(node->name, "multifunc-device")) {
  28. for_each_child_of_node(node, node2) {
  29. if (__of_pci_pci_compare(node2, devfn)) {
  30. of_node_put(node);
  31. return node2;
  32. }
  33. }
  34. }
  35. }
  36. return NULL;
  37. }
  38. EXPORT_SYMBOL_GPL(of_pci_find_child_device);
  39. /**
  40. * of_pci_get_devfn() - Get device and function numbers for a device node
  41. * @np: device node
  42. *
  43. * Parses a standard 5-cell PCI resource and returns an 8-bit value that can
  44. * be passed to the PCI_SLOT() and PCI_FUNC() macros to extract the device
  45. * and function numbers respectively. On error a negative error code is
  46. * returned.
  47. */
  48. int of_pci_get_devfn(struct device_node *np)
  49. {
  50. unsigned int size;
  51. const __be32 *reg;
  52. reg = of_get_property(np, "reg", &size);
  53. if (!reg || size < 5 * sizeof(__be32))
  54. return -EINVAL;
  55. return (be32_to_cpup(reg) >> 8) & 0xff;
  56. }
  57. EXPORT_SYMBOL_GPL(of_pci_get_devfn);
  58. /**
  59. * of_pci_parse_bus_range() - parse the bus-range property of a PCI device
  60. * @node: device node
  61. * @res: address to a struct resource to return the bus-range
  62. *
  63. * Returns 0 on success or a negative error-code on failure.
  64. */
  65. int of_pci_parse_bus_range(struct device_node *node, struct resource *res)
  66. {
  67. const __be32 *values;
  68. int len;
  69. values = of_get_property(node, "bus-range", &len);
  70. if (!values || len < sizeof(*values) * 2)
  71. return -EINVAL;
  72. res->name = node->name;
  73. res->start = be32_to_cpup(values++);
  74. res->end = be32_to_cpup(values);
  75. res->flags = IORESOURCE_BUS;
  76. return 0;
  77. }
  78. EXPORT_SYMBOL_GPL(of_pci_parse_bus_range);
  79. #ifdef CONFIG_PCI_MSI
  80. static LIST_HEAD(of_pci_msi_chip_list);
  81. static DEFINE_MUTEX(of_pci_msi_chip_mutex);
  82. int of_pci_msi_chip_add(struct msi_chip *chip)
  83. {
  84. if (!of_property_read_bool(chip->of_node, "msi-controller"))
  85. return -EINVAL;
  86. mutex_lock(&of_pci_msi_chip_mutex);
  87. list_add(&chip->list, &of_pci_msi_chip_list);
  88. mutex_unlock(&of_pci_msi_chip_mutex);
  89. return 0;
  90. }
  91. EXPORT_SYMBOL_GPL(of_pci_msi_chip_add);
  92. void of_pci_msi_chip_remove(struct msi_chip *chip)
  93. {
  94. mutex_lock(&of_pci_msi_chip_mutex);
  95. list_del(&chip->list);
  96. mutex_unlock(&of_pci_msi_chip_mutex);
  97. }
  98. EXPORT_SYMBOL_GPL(of_pci_msi_chip_remove);
  99. struct msi_chip *of_pci_find_msi_chip_by_node(struct device_node *of_node)
  100. {
  101. struct msi_chip *c;
  102. mutex_lock(&of_pci_msi_chip_mutex);
  103. list_for_each_entry(c, &of_pci_msi_chip_list, list) {
  104. if (c->of_node == of_node) {
  105. mutex_unlock(&of_pci_msi_chip_mutex);
  106. return c;
  107. }
  108. }
  109. mutex_unlock(&of_pci_msi_chip_mutex);
  110. return NULL;
  111. }
  112. EXPORT_SYMBOL_GPL(of_pci_find_msi_chip_by_node);
  113. #endif /* CONFIG_PCI_MSI */