of_platform.c 7.8 KB

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