of_platform.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. * Copyright (C) 2006 Benjamin Herrenschmidt, IBM Corp.
  3. * <benh@kernel.crashing.org>
  4. * and Arnd Bergmann, 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. */
  12. #undef DEBUG
  13. #include <linux/string.h>
  14. #include <linux/kernel.h>
  15. #include <linux/init.h>
  16. #include <linux/module.h>
  17. #include <linux/mod_devicetable.h>
  18. #include <linux/pci.h>
  19. #include <linux/of.h>
  20. #include <linux/of_device.h>
  21. #include <linux/of_platform.h>
  22. #include <asm/errno.h>
  23. #include <asm/topology.h>
  24. #include <asm/pci-bridge.h>
  25. #include <asm/ppc-pci.h>
  26. #include <asm/atomic.h>
  27. /*
  28. * The list of OF IDs below is used for matching bus types in the
  29. * system whose devices are to be exposed as of_platform_devices.
  30. *
  31. * This is the default list valid for most platforms. This file provides
  32. * functions who can take an explicit list if necessary though
  33. *
  34. * The search is always performed recursively looking for children of
  35. * the provided device_node and recursively if such a children matches
  36. * a bus type in the list
  37. */
  38. const struct of_device_id of_default_bus_ids[] = {
  39. { .type = "soc", },
  40. { .compatible = "soc", },
  41. { .type = "spider", },
  42. { .type = "axon", },
  43. { .type = "plb5", },
  44. { .type = "plb4", },
  45. { .type = "opb", },
  46. { .type = "ebc", },
  47. {},
  48. };
  49. struct bus_type of_platform_bus_type = {
  50. .uevent = of_device_uevent,
  51. };
  52. EXPORT_SYMBOL(of_platform_bus_type);
  53. static int __init of_bus_driver_init(void)
  54. {
  55. return of_bus_type_init(&of_platform_bus_type, "of_platform");
  56. }
  57. postcore_initcall(of_bus_driver_init);
  58. static int of_dev_node_match(struct device *dev, void *data)
  59. {
  60. return to_of_device(dev)->dev.of_node == data;
  61. }
  62. struct of_device *of_find_device_by_node(struct device_node *np)
  63. {
  64. struct device *dev;
  65. dev = bus_find_device(&of_platform_bus_type,
  66. NULL, np, of_dev_node_match);
  67. if (dev)
  68. return to_of_device(dev);
  69. return NULL;
  70. }
  71. EXPORT_SYMBOL(of_find_device_by_node);
  72. static int of_dev_phandle_match(struct device *dev, void *data)
  73. {
  74. phandle *ph = data;
  75. return to_of_device(dev)->dev.of_node->phandle == *ph;
  76. }
  77. struct of_device *of_find_device_by_phandle(phandle ph)
  78. {
  79. struct device *dev;
  80. dev = bus_find_device(&of_platform_bus_type,
  81. NULL, &ph, of_dev_phandle_match);
  82. if (dev)
  83. return to_of_device(dev);
  84. return NULL;
  85. }
  86. EXPORT_SYMBOL(of_find_device_by_phandle);
  87. #ifdef CONFIG_PPC_OF_PLATFORM_PCI
  88. /* The probing of PCI controllers from of_platform is currently
  89. * 64 bits only, mostly due to gratuitous differences between
  90. * the 32 and 64 bits PCI code on PowerPC and the 32 bits one
  91. * lacking some bits needed here.
  92. */
  93. static int __devinit of_pci_phb_probe(struct of_device *dev,
  94. const struct of_device_id *match)
  95. {
  96. struct pci_controller *phb;
  97. /* Check if we can do that ... */
  98. if (ppc_md.pci_setup_phb == NULL)
  99. return -ENODEV;
  100. pr_info("Setting up PCI bus %s\n", dev->dev.of_node->full_name);
  101. /* Alloc and setup PHB data structure */
  102. phb = pcibios_alloc_controller(dev->dev.of_node);
  103. if (!phb)
  104. return -ENODEV;
  105. /* Setup parent in sysfs */
  106. phb->parent = &dev->dev;
  107. /* Setup the PHB using arch provided callback */
  108. if (ppc_md.pci_setup_phb(phb)) {
  109. pcibios_free_controller(phb);
  110. return -ENODEV;
  111. }
  112. /* Process "ranges" property */
  113. pci_process_bridge_OF_ranges(phb, dev->dev.of_node, 0);
  114. /* Init pci_dn data structures */
  115. pci_devs_phb_init_dynamic(phb);
  116. /* Register devices with EEH */
  117. #ifdef CONFIG_EEH
  118. if (dev->dev.of_node->child)
  119. eeh_add_device_tree_early(dev->dev.of_node);
  120. #endif /* CONFIG_EEH */
  121. /* Scan the bus */
  122. pcibios_scan_phb(phb, dev->dev.of_node);
  123. if (phb->bus == NULL)
  124. return -ENXIO;
  125. /* Claim resources. This might need some rework as well depending
  126. * wether we are doing probe-only or not, like assigning unassigned
  127. * resources etc...
  128. */
  129. pcibios_claim_one_bus(phb->bus);
  130. /* Finish EEH setup */
  131. #ifdef CONFIG_EEH
  132. eeh_add_device_tree_late(phb->bus);
  133. #endif
  134. /* Add probed PCI devices to the device model */
  135. pci_bus_add_devices(phb->bus);
  136. return 0;
  137. }
  138. static struct of_device_id of_pci_phb_ids[] = {
  139. { .type = "pci", },
  140. { .type = "pcix", },
  141. { .type = "pcie", },
  142. { .type = "pciex", },
  143. { .type = "ht", },
  144. {}
  145. };
  146. static struct of_platform_driver of_pci_phb_driver = {
  147. .probe = of_pci_phb_probe,
  148. .driver = {
  149. .name = "of-pci",
  150. .owner = THIS_MODULE,
  151. .of_match_table = of_pci_phb_ids,
  152. },
  153. };
  154. static __init int of_pci_phb_init(void)
  155. {
  156. return of_register_platform_driver(&of_pci_phb_driver);
  157. }
  158. device_initcall(of_pci_phb_init);
  159. #endif /* CONFIG_PPC_OF_PLATFORM_PCI */