of_platform.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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 <asm/errno.h>
  21. #include <asm/dcr.h>
  22. #include <asm/of_device.h>
  23. #include <asm/of_platform.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 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. static atomic_t bus_no_reg_magic;
  51. struct bus_type of_platform_bus_type = {
  52. .uevent = of_device_uevent,
  53. };
  54. EXPORT_SYMBOL(of_platform_bus_type);
  55. static int __init of_bus_driver_init(void)
  56. {
  57. return of_bus_type_init(&of_platform_bus_type, "of_platform");
  58. }
  59. postcore_initcall(of_bus_driver_init);
  60. int of_register_platform_driver(struct of_platform_driver *drv)
  61. {
  62. /* initialize common driver fields */
  63. drv->driver.name = drv->name;
  64. drv->driver.bus = &of_platform_bus_type;
  65. /* register with core */
  66. return driver_register(&drv->driver);
  67. }
  68. EXPORT_SYMBOL(of_register_platform_driver);
  69. void of_unregister_platform_driver(struct of_platform_driver *drv)
  70. {
  71. driver_unregister(&drv->driver);
  72. }
  73. EXPORT_SYMBOL(of_unregister_platform_driver);
  74. static void of_platform_make_bus_id(struct of_device *dev)
  75. {
  76. struct device_node *node = dev->node;
  77. char *name = dev->dev.bus_id;
  78. const u32 *reg;
  79. u64 addr;
  80. int magic;
  81. /*
  82. * If it's a DCR based device, use 'd' for native DCRs
  83. * and 'D' for MMIO DCRs.
  84. */
  85. #ifdef CONFIG_PPC_DCR
  86. reg = of_get_property(node, "dcr-reg", NULL);
  87. if (reg) {
  88. #ifdef CONFIG_PPC_DCR_NATIVE
  89. snprintf(name, BUS_ID_SIZE, "d%x.%s",
  90. *reg, node->name);
  91. #else /* CONFIG_PPC_DCR_NATIVE */
  92. addr = of_translate_dcr_address(node, *reg, NULL);
  93. if (addr != OF_BAD_ADDR) {
  94. snprintf(name, BUS_ID_SIZE,
  95. "D%llx.%s", (unsigned long long)addr,
  96. node->name);
  97. return;
  98. }
  99. #endif /* !CONFIG_PPC_DCR_NATIVE */
  100. }
  101. #endif /* CONFIG_PPC_DCR */
  102. /*
  103. * For MMIO, get the physical address
  104. */
  105. reg = of_get_property(node, "reg", NULL);
  106. if (reg) {
  107. addr = of_translate_address(node, reg);
  108. if (addr != OF_BAD_ADDR) {
  109. snprintf(name, BUS_ID_SIZE,
  110. "%llx.%s", (unsigned long long)addr,
  111. node->name);
  112. return;
  113. }
  114. }
  115. /*
  116. * No BusID, use the node name and add a globally incremented
  117. * counter (and pray...)
  118. */
  119. magic = atomic_add_return(1, &bus_no_reg_magic);
  120. snprintf(name, BUS_ID_SIZE, "%s.%d", node->name, magic - 1);
  121. }
  122. struct of_device* of_platform_device_create(struct device_node *np,
  123. const char *bus_id,
  124. struct device *parent)
  125. {
  126. struct of_device *dev;
  127. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  128. if (!dev)
  129. return NULL;
  130. dev->node = of_node_get(np);
  131. dev->dma_mask = 0xffffffffUL;
  132. dev->dev.dma_mask = &dev->dma_mask;
  133. dev->dev.parent = parent;
  134. dev->dev.bus = &of_platform_bus_type;
  135. dev->dev.release = of_release_dev;
  136. dev->dev.archdata.of_node = np;
  137. dev->dev.archdata.numa_node = of_node_to_nid(np);
  138. /* We do not fill the DMA ops for platform devices by default.
  139. * This is currently the responsibility of the platform code
  140. * to do such, possibly using a device notifier
  141. */
  142. if (bus_id)
  143. strlcpy(dev->dev.bus_id, bus_id, BUS_ID_SIZE);
  144. else
  145. of_platform_make_bus_id(dev);
  146. if (of_device_register(dev) != 0) {
  147. kfree(dev);
  148. return NULL;
  149. }
  150. return dev;
  151. }
  152. EXPORT_SYMBOL(of_platform_device_create);
  153. /**
  154. * of_platform_bus_create - Create an OF device for a bus node and all its
  155. * children. Optionally recursively instanciate matching busses.
  156. * @bus: device node of the bus to instanciate
  157. * @matches: match table, NULL to use the default, OF_NO_DEEP_PROBE to
  158. * disallow recursive creation of child busses
  159. */
  160. static int of_platform_bus_create(struct device_node *bus,
  161. struct of_device_id *matches,
  162. struct device *parent)
  163. {
  164. struct device_node *child;
  165. struct of_device *dev;
  166. int rc = 0;
  167. for (child = NULL; (child = of_get_next_child(bus, child)); ) {
  168. pr_debug(" create child: %s\n", child->full_name);
  169. dev = of_platform_device_create(child, NULL, parent);
  170. if (dev == NULL)
  171. rc = -ENOMEM;
  172. else if (!of_match_node(matches, child))
  173. continue;
  174. if (rc == 0) {
  175. pr_debug(" and sub busses\n");
  176. rc = of_platform_bus_create(child, matches, &dev->dev);
  177. } if (rc) {
  178. of_node_put(child);
  179. break;
  180. }
  181. }
  182. return rc;
  183. }
  184. /**
  185. * of_platform_bus_probe - Probe the device-tree for platform busses
  186. * @root: parent of the first level to probe or NULL for the root of the tree
  187. * @matches: match table, NULL to use the default
  188. * @parent: parent to hook devices from, NULL for toplevel
  189. *
  190. * Note that children of the provided root are not instanciated as devices
  191. * unless the specified root itself matches the bus list and is not NULL.
  192. */
  193. int of_platform_bus_probe(struct device_node *root,
  194. struct of_device_id *matches,
  195. struct device *parent)
  196. {
  197. struct device_node *child;
  198. struct of_device *dev;
  199. int rc = 0;
  200. if (matches == NULL)
  201. matches = of_default_bus_ids;
  202. if (matches == OF_NO_DEEP_PROBE)
  203. return -EINVAL;
  204. if (root == NULL)
  205. root = of_find_node_by_path("/");
  206. else
  207. of_node_get(root);
  208. pr_debug("of_platform_bus_probe()\n");
  209. pr_debug(" starting at: %s\n", root->full_name);
  210. /* Do a self check of bus type, if there's a match, create
  211. * children
  212. */
  213. if (of_match_node(matches, root)) {
  214. pr_debug(" root match, create all sub devices\n");
  215. dev = of_platform_device_create(root, NULL, parent);
  216. if (dev == NULL) {
  217. rc = -ENOMEM;
  218. goto bail;
  219. }
  220. pr_debug(" create all sub busses\n");
  221. rc = of_platform_bus_create(root, matches, &dev->dev);
  222. goto bail;
  223. }
  224. for (child = NULL; (child = of_get_next_child(root, child)); ) {
  225. if (!of_match_node(matches, child))
  226. continue;
  227. pr_debug(" match: %s\n", child->full_name);
  228. dev = of_platform_device_create(child, NULL, parent);
  229. if (dev == NULL)
  230. rc = -ENOMEM;
  231. else
  232. rc = of_platform_bus_create(child, matches, &dev->dev);
  233. if (rc) {
  234. of_node_put(child);
  235. break;
  236. }
  237. }
  238. bail:
  239. of_node_put(root);
  240. return rc;
  241. }
  242. EXPORT_SYMBOL(of_platform_bus_probe);
  243. static int of_dev_node_match(struct device *dev, void *data)
  244. {
  245. return to_of_device(dev)->node == data;
  246. }
  247. struct of_device *of_find_device_by_node(struct device_node *np)
  248. {
  249. struct device *dev;
  250. dev = bus_find_device(&of_platform_bus_type,
  251. NULL, np, of_dev_node_match);
  252. if (dev)
  253. return to_of_device(dev);
  254. return NULL;
  255. }
  256. EXPORT_SYMBOL(of_find_device_by_node);
  257. static int of_dev_phandle_match(struct device *dev, void *data)
  258. {
  259. phandle *ph = data;
  260. return to_of_device(dev)->node->linux_phandle == *ph;
  261. }
  262. struct of_device *of_find_device_by_phandle(phandle ph)
  263. {
  264. struct device *dev;
  265. dev = bus_find_device(&of_platform_bus_type,
  266. NULL, &ph, of_dev_phandle_match);
  267. if (dev)
  268. return to_of_device(dev);
  269. return NULL;
  270. }
  271. EXPORT_SYMBOL(of_find_device_by_phandle);
  272. #ifdef CONFIG_PPC_OF_PLATFORM_PCI
  273. /* The probing of PCI controllers from of_platform is currently
  274. * 64 bits only, mostly due to gratuitous differences between
  275. * the 32 and 64 bits PCI code on PowerPC and the 32 bits one
  276. * lacking some bits needed here.
  277. */
  278. static int __devinit of_pci_phb_probe(struct of_device *dev,
  279. const struct of_device_id *match)
  280. {
  281. struct pci_controller *phb;
  282. /* Check if we can do that ... */
  283. if (ppc_md.pci_setup_phb == NULL)
  284. return -ENODEV;
  285. printk(KERN_INFO "Setting up PCI bus %s\n", dev->node->full_name);
  286. /* Alloc and setup PHB data structure */
  287. phb = pcibios_alloc_controller(dev->node);
  288. if (!phb)
  289. return -ENODEV;
  290. /* Setup parent in sysfs */
  291. phb->parent = &dev->dev;
  292. /* Setup the PHB using arch provided callback */
  293. if (ppc_md.pci_setup_phb(phb)) {
  294. pcibios_free_controller(phb);
  295. return -ENODEV;
  296. }
  297. /* Process "ranges" property */
  298. pci_process_bridge_OF_ranges(phb, dev->node, 0);
  299. /* Init pci_dn data structures */
  300. pci_devs_phb_init_dynamic(phb);
  301. /* Register devices with EEH */
  302. #ifdef CONFIG_EEH
  303. if (dev->node->child)
  304. eeh_add_device_tree_early(dev->node);
  305. #endif /* CONFIG_EEH */
  306. /* Scan the bus */
  307. scan_phb(phb);
  308. /* Claim resources. This might need some rework as well depending
  309. * wether we are doing probe-only or not, like assigning unassigned
  310. * resources etc...
  311. */
  312. pcibios_claim_one_bus(phb->bus);
  313. /* Finish EEH setup */
  314. #ifdef CONFIG_EEH
  315. eeh_add_device_tree_late(phb->bus);
  316. #endif
  317. /* Add probed PCI devices to the device model */
  318. pci_bus_add_devices(phb->bus);
  319. return 0;
  320. }
  321. static struct of_device_id of_pci_phb_ids[] = {
  322. { .type = "pci", },
  323. { .type = "pcix", },
  324. { .type = "pcie", },
  325. { .type = "pciex", },
  326. { .type = "ht", },
  327. {}
  328. };
  329. static struct of_platform_driver of_pci_phb_driver = {
  330. .name = "of-pci",
  331. .match_table = of_pci_phb_ids,
  332. .probe = of_pci_phb_probe,
  333. };
  334. static __init int of_pci_phb_init(void)
  335. {
  336. return of_register_platform_driver(&of_pci_phb_driver);
  337. }
  338. device_initcall(of_pci_phb_init);
  339. #endif /* CONFIG_PPC_OF_PLATFORM_PCI */