of_platform.c 9.8 KB

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