PageRenderTime 29ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/arch/powerpc/kernel/of_platform.c

https://bitbucket.org/cresqo/cm7-p500-kernel
C | 324 lines | 207 code | 53 blank | 64 comment | 34 complexity | 2e1362058508e5cdc9a06b07cff3cfd9 MD5 | raw file
Possible License(s): LGPL-2.0, AGPL-1.0, GPL-2.0
  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. static 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. 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->archdata.dma_mask = 0xffffffffUL;
  67. dev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
  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)->dev.of_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)->dev.of_node->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. pr_info("Setting up PCI bus %s\n", dev->dev.of_node->full_name);
  213. /* Alloc and setup PHB data structure */
  214. phb = pcibios_alloc_controller(dev->dev.of_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->dev.of_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->dev.of_node->child)
  231. eeh_add_device_tree_early(dev->dev.of_node);
  232. #endif /* CONFIG_EEH */
  233. /* Scan the bus */
  234. pcibios_scan_phb(phb, dev->dev.of_node);
  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. .probe = of_pci_phb_probe,
  260. .driver = {
  261. .name = "of-pci",
  262. .owner = THIS_MODULE,
  263. .of_match_table = of_pci_phb_ids,
  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 */