of_platform.c 12 KB

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