of_platform.c 8.2 KB

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