of_platform.c 12 KB

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