of_platform.c 7.7 KB

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