of_pci.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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);