of_platform.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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/slab.h>
  19. #include <linux/pci.h>
  20. #include <linux/of.h>
  21. #include <linux/of_device.h>
  22. #include <linux/of_platform.h>
  23. #include <asm/errno.h>
  24. #include <asm/topology.h>
  25. #include <asm/pci-bridge.h>
  26. #include <asm/ppc-pci.h>
  27. #include <asm/atomic.h>
  28. /*
  29. * The list of OF IDs below is used for matching bus types in the
  30. * system whose devices are to be exposed as of_platform_devices.
  31. *
  32. * This is the default list valid for most platforms. This file provides
  33. * functions who can take an explicit list if necessary though
  34. *
  35. * The search is always performed recursively looking for children of
  36. * the provided device_node and recursively if such a children matches
  37. * a bus type in the list
  38. */
  39. static const struct of_device_id of_default_bus_ids[] = {
  40. { .type = "soc", },
  41. { .compatible = "soc", },
  42. { .type = "spider", },
  43. { .type = "axon", },
  44. { .type = "plb5", },
  45. { .type = "plb4", },
  46. { .type = "opb", },
  47. { .type = "ebc", },
  48. {},
  49. };
  50. struct bus_type of_platform_bus_type = {
  51. .uevent = of_device_uevent,
  52. };
  53. EXPORT_SYMBOL(of_platform_bus_type);
  54. static int __init of_bus_driver_init(void)
  55. {
  56. return of_bus_type_init(&of_platform_bus_type, "of_platform");
  57. }
  58. postcore_initcall(of_bus_driver_init);
  59. struct of_device* of_platform_device_create(struct device_node *np,
  60. const char *bus_id,
  61. struct device *parent)
  62. {
  63. struct of_device *dev;
  64. dev = of_device_alloc(np, bus_id, parent);
  65. if (!dev)
  66. return NULL;
  67. dev->dma_mask = 0xffffffffUL;
  68. dev->dev.bus = &of_platform_bus_type;
  69. /* We do not fill the DMA ops for platform devices by default.
  70. * This is currently the responsibility of the platform code
  71. * to do such, possibly using a device notifier
  72. */
  73. if (of_device_register(dev) != 0) {
  74. of_device_free(dev);
  75. return NULL;
  76. }
  77. return dev;
  78. }
  79. EXPORT_SYMBOL(of_platform_device_create);
  80. /**
  81. * of_platform_bus_create - Create an OF device for a bus node and all its
  82. * children. Optionally recursively instanciate matching busses.
  83. * @bus: device node of the bus to instanciate
  84. * @matches: match table, NULL to use the default, OF_NO_DEEP_PROBE to
  85. * disallow recursive creation of child busses
  86. */
  87. static int of_platform_bus_create(const struct device_node *bus,
  88. const struct of_device_id *matches,
  89. struct device *parent)
  90. {
  91. struct device_node *child;
  92. struct of_device *dev;
  93. int rc = 0;
  94. for_each_child_of_node(bus, child) {
  95. pr_debug(" create child: %s\n", child->full_name);
  96. dev = of_platform_device_create(child, NULL, parent);
  97. if (dev == NULL)
  98. rc = -ENOMEM;
  99. else if (!of_match_node(matches, child))
  100. continue;
  101. if (rc == 0) {
  102. pr_debug(" and sub busses\n");
  103. rc = of_platform_bus_create(child, matches, &dev->dev);
  104. } if (rc) {
  105. of_node_put(child);
  106. break;
  107. }
  108. }
  109. return rc;
  110. }
  111. /**
  112. * of_platform_bus_probe - Probe the device-tree for platform busses
  113. * @root: parent of the first level to probe or NULL for the root of the tree
  114. * @matches: match table, NULL to use the default
  115. * @parent: parent to hook devices from, NULL for toplevel
  116. *
  117. * Note that children of the provided root are not instanciated as devices
  118. * unless the specified root itself matches the bus list and is not NULL.
  119. */
  120. int of_platform_bus_probe(struct device_node *root,
  121. const struct of_device_id *matches,
  122. struct device *parent)
  123. {
  124. struct device_node *child;
  125. struct of_device *dev;
  126. int rc = 0;
  127. if (matches == NULL)
  128. matches = of_default_bus_ids;
  129. if (matches == OF_NO_DEEP_PROBE)
  130. return -EINVAL;
  131. if (root == NULL)
  132. root = of_find_node_by_path("/");
  133. else
  134. of_node_get(root);
  135. pr_debug("of_platform_bus_probe()\n");
  136. pr_debug(" starting at: %s\n", root->full_name);
  137. /* Do a self check of bus type, if there's a match, create
  138. * children
  139. */
  140. if (of_match_node(matches, root)) {
  141. pr_debug(" root match, create all sub devices\n");
  142. dev = of_platform_device_create(root, NULL, parent);
  143. if (dev == NULL) {
  144. rc = -ENOMEM;
  145. goto bail;
  146. }
  147. pr_debug(" create all sub busses\n");
  148. rc = of_platform_bus_create(root, matches, &dev->dev);
  149. goto bail;
  150. }
  151. for_each_child_of_node(root, child) {
  152. if (!of_match_node(matches, child))
  153. continue;
  154. pr_debug(" match: %s\n", child->full_name);
  155. dev = of_platform_device_create(child, NULL, parent);
  156. if (dev == NULL)
  157. rc = -ENOMEM;
  158. else
  159. rc = of_platform_bus_create(child, matches, &dev->dev);
  160. if (rc) {
  161. of_node_put(child);
  162. break;
  163. }
  164. }
  165. bail:
  166. of_node_put(root);
  167. return rc;
  168. }
  169. EXPORT_SYMBOL(of_platform_bus_probe);
  170. static int of_dev_node_match(struct device *dev, void *data)
  171. {
  172. return to_of_device(dev)->node == data;
  173. }
  174. struct of_device *of_find_device_by_node(struct device_node *np)
  175. {
  176. struct device *dev;
  177. dev = bus_find_device(&of_platform_bus_type,
  178. NULL, np, of_dev_node_match);
  179. if (dev)
  180. return to_of_device(dev);
  181. return NULL;
  182. }
  183. EXPORT_SYMBOL(of_find_device_by_node);
  184. static int of_dev_phandle_match(struct device *dev, void *data)
  185. {
  186. phandle *ph = data;
  187. return to_of_device(dev)->node->linux_phandle == *ph;
  188. }
  189. struct of_device *of_find_device_by_phandle(phandle ph)
  190. {
  191. struct device *dev;
  192. dev = bus_find_device(&of_platform_bus_type,
  193. NULL, &ph, of_dev_phandle_match);
  194. if (dev)
  195. return to_of_device(dev);
  196. return NULL;
  197. }
  198. EXPORT_SYMBOL(of_find_device_by_phandle);
  199. #ifdef CONFIG_PPC_OF_PLATFORM_PCI
  200. /* The probing of PCI controllers from of_platform is currently
  201. * 64 bits only, mostly due to gratuitous differences between
  202. * the 32 and 64 bits PCI code on PowerPC and the 32 bits one
  203. * lacking some bits needed here.
  204. */
  205. static int __devinit of_pci_phb_probe(struct of_device *dev,
  206. const struct of_device_id *match)
  207. {
  208. struct pci_controller *phb;
  209. /* Check if we can do that ... */
  210. if (ppc_md.pci_setup_phb == NULL)
  211. return -ENODEV;
  212. printk(KERN_INFO "Setting up PCI bus %s\n", dev->node->full_name);
  213. /* Alloc and setup PHB data structure */
  214. phb = pcibios_alloc_controller(dev->node);
  215. if (!phb)
  216. return -ENODEV;
  217. /* Setup parent in sysfs */
  218. phb->parent = &dev->dev;
  219. /* Setup the PHB using arch provided callback */
  220. if (ppc_md.pci_setup_phb(phb)) {
  221. pcibios_free_controller(phb);
  222. return -ENODEV;
  223. }
  224. /* Process "ranges" property */
  225. pci_process_bridge_OF_ranges(phb, dev->node, 0);
  226. /* Init pci_dn data structures */
  227. pci_devs_phb_init_dynamic(phb);
  228. /* Register devices with EEH */
  229. #ifdef CONFIG_EEH
  230. if (dev->node->child)
  231. eeh_add_device_tree_early(dev->node);
  232. #endif /* CONFIG_EEH */
  233. /* Scan the bus */
  234. scan_phb(phb);
  235. if (phb->bus == NULL)
  236. return -ENXIO;
  237. /* Claim resources. This might need some rework as well depending
  238. * wether we are doing probe-only or not, like assigning unassigned
  239. * resources etc...
  240. */
  241. pcibios_claim_one_bus(phb->bus);
  242. /* Finish EEH setup */
  243. #ifdef CONFIG_EEH
  244. eeh_add_device_tree_late(phb->bus);
  245. #endif
  246. /* Add probed PCI devices to the device model */
  247. pci_bus_add_devices(phb->bus);
  248. return 0;
  249. }
  250. static struct of_device_id of_pci_phb_ids[] = {
  251. { .type = "pci", },
  252. { .type = "pcix", },
  253. { .type = "pcie", },
  254. { .type = "pciex", },
  255. { .type = "ht", },
  256. {}
  257. };
  258. static struct of_platform_driver of_pci_phb_driver = {
  259. .match_table = of_pci_phb_ids,
  260. .probe = of_pci_phb_probe,
  261. .driver = {
  262. .name = "of-pci",
  263. },
  264. };
  265. static __init int of_pci_phb_init(void)
  266. {
  267. return of_register_platform_driver(&of_pci_phb_driver);
  268. }
  269. device_initcall(of_pci_phb_init);
  270. #endif /* CONFIG_PPC_OF_PLATFORM_PCI */