of.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /*
  2. * PCI <-> OF mapping helpers
  3. *
  4. * Copyright 2011 IBM Corp.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/pci.h>
  13. #include <linux/of.h>
  14. #include <linux/of_pci.h>
  15. #include "pci.h"
  16. void pci_set_of_node(struct pci_dev *dev)
  17. {
  18. if (!dev->bus->dev.of_node)
  19. return;
  20. dev->dev.of_node = of_pci_find_child_device(dev->bus->dev.of_node,
  21. dev->devfn);
  22. }
  23. void pci_release_of_node(struct pci_dev *dev)
  24. {
  25. of_node_put(dev->dev.of_node);
  26. dev->dev.of_node = NULL;
  27. }
  28. void pci_set_bus_of_node(struct pci_bus *bus)
  29. {
  30. if (bus->self == NULL)
  31. bus->dev.of_node = pcibios_get_phb_of_node(bus);
  32. else
  33. bus->dev.of_node = of_node_get(bus->self->dev.of_node);
  34. }
  35. void pci_release_bus_of_node(struct pci_bus *bus)
  36. {
  37. of_node_put(bus->dev.of_node);
  38. bus->dev.of_node = NULL;
  39. }
  40. struct device_node * __weak pcibios_get_phb_of_node(struct pci_bus *bus)
  41. {
  42. /* This should only be called for PHBs */
  43. if (WARN_ON(bus->self || bus->parent))
  44. return NULL;
  45. /* Look for a node pointer in either the intermediary device we
  46. * create above the root bus or it's own parent. Normally only
  47. * the later is populated.
  48. */
  49. if (bus->bridge->of_node)
  50. return of_node_get(bus->bridge->of_node);
  51. if (bus->bridge->parent && bus->bridge->parent->of_node)
  52. return of_node_get(bus->bridge->parent->of_node);
  53. return NULL;
  54. }