of_platform.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. /*
  2. * Copyright (C) 2006 Benjamin Herrenschmidt, IBM Corp.
  3. * <benh@kernel.crashing.org>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation; either version
  8. * 2 of the License, or (at your option) any later version.
  9. *
  10. */
  11. #undef DEBUG
  12. #include <linux/string.h>
  13. #include <linux/kernel.h>
  14. #include <linux/init.h>
  15. #include <linux/module.h>
  16. #include <linux/mod_devicetable.h>
  17. #include <linux/slab.h>
  18. #include <asm/errno.h>
  19. #include <asm/dcr.h>
  20. #include <asm/of_device.h>
  21. #include <asm/of_platform.h>
  22. #include <asm/topology.h>
  23. /*
  24. * The list of OF IDs below is used for matching bus types in the
  25. * system whose devices are to be exposed as of_platform_devices.
  26. *
  27. * This is the default list valid for most platforms. This file provides
  28. * functions who can take an explicit list if necessary though
  29. *
  30. * The search is always performed recursively looking for children of
  31. * the provided device_node and recursively if such a children matches
  32. * a bus type in the list
  33. */
  34. static struct of_device_id of_default_bus_ids[] = {
  35. { .type = "soc", },
  36. { .compatible = "soc", },
  37. { .type = "spider", },
  38. { .type = "axon", },
  39. { .type = "plb5", },
  40. { .type = "plb4", },
  41. { .type = "opb", },
  42. {},
  43. };
  44. /*
  45. *
  46. * OF platform device type definition & base infrastructure
  47. *
  48. */
  49. static int of_platform_bus_match(struct device *dev, struct device_driver *drv)
  50. {
  51. struct of_device * of_dev = to_of_device(dev);
  52. struct of_platform_driver * of_drv = to_of_platform_driver(drv);
  53. const struct of_device_id * matches = of_drv->match_table;
  54. if (!matches)
  55. return 0;
  56. return of_match_device(matches, of_dev) != NULL;
  57. }
  58. static int of_platform_device_probe(struct device *dev)
  59. {
  60. int error = -ENODEV;
  61. struct of_platform_driver *drv;
  62. struct of_device *of_dev;
  63. const struct of_device_id *match;
  64. drv = to_of_platform_driver(dev->driver);
  65. of_dev = to_of_device(dev);
  66. if (!drv->probe)
  67. return error;
  68. of_dev_get(of_dev);
  69. match = of_match_device(drv->match_table, of_dev);
  70. if (match)
  71. error = drv->probe(of_dev, match);
  72. if (error)
  73. of_dev_put(of_dev);
  74. return error;
  75. }
  76. static int of_platform_device_remove(struct device *dev)
  77. {
  78. struct of_device * of_dev = to_of_device(dev);
  79. struct of_platform_driver * drv = to_of_platform_driver(dev->driver);
  80. if (dev->driver && drv->remove)
  81. drv->remove(of_dev);
  82. return 0;
  83. }
  84. static int of_platform_device_suspend(struct device *dev, pm_message_t state)
  85. {
  86. struct of_device * of_dev = to_of_device(dev);
  87. struct of_platform_driver * drv = to_of_platform_driver(dev->driver);
  88. int error = 0;
  89. if (dev->driver && drv->suspend)
  90. error = drv->suspend(of_dev, state);
  91. return error;
  92. }
  93. static int of_platform_device_resume(struct device * dev)
  94. {
  95. struct of_device * of_dev = to_of_device(dev);
  96. struct of_platform_driver * drv = to_of_platform_driver(dev->driver);
  97. int error = 0;
  98. if (dev->driver && drv->resume)
  99. error = drv->resume(of_dev);
  100. return error;
  101. }
  102. struct bus_type of_platform_bus_type = {
  103. .name = "of_platform",
  104. .match = of_platform_bus_match,
  105. .probe = of_platform_device_probe,
  106. .remove = of_platform_device_remove,
  107. .suspend = of_platform_device_suspend,
  108. .resume = of_platform_device_resume,
  109. };
  110. EXPORT_SYMBOL(of_platform_bus_type);
  111. static int __init of_bus_driver_init(void)
  112. {
  113. return bus_register(&of_platform_bus_type);
  114. }
  115. postcore_initcall(of_bus_driver_init);
  116. int of_register_platform_driver(struct of_platform_driver *drv)
  117. {
  118. /* initialize common driver fields */
  119. drv->driver.name = drv->name;
  120. drv->driver.bus = &of_platform_bus_type;
  121. /* register with core */
  122. return driver_register(&drv->driver);
  123. }
  124. EXPORT_SYMBOL(of_register_platform_driver);
  125. void of_unregister_platform_driver(struct of_platform_driver *drv)
  126. {
  127. driver_unregister(&drv->driver);
  128. }
  129. EXPORT_SYMBOL(of_unregister_platform_driver);
  130. static void of_platform_make_bus_id(struct of_device *dev)
  131. {
  132. struct device_node *node = dev->node;
  133. char *name = dev->dev.bus_id;
  134. const u32 *reg;
  135. u64 addr;
  136. /*
  137. * If it's a DCR based device, use 'd' for native DCRs
  138. * and 'D' for MMIO DCRs.
  139. */
  140. #ifdef CONFIG_PPC_DCR
  141. reg = get_property(node, "dcr-reg", NULL);
  142. if (reg) {
  143. #ifdef CONFIG_PPC_DCR_NATIVE
  144. snprintf(name, BUS_ID_SIZE, "d%x.%s",
  145. *reg, node->name);
  146. #else /* CONFIG_PPC_DCR_NATIVE */
  147. addr = of_translate_dcr_address(node, *reg, NULL);
  148. if (addr != OF_BAD_ADDR) {
  149. snprintf(name, BUS_ID_SIZE,
  150. "D%llx.%s", (unsigned long long)addr,
  151. node->name);
  152. return;
  153. }
  154. #endif /* !CONFIG_PPC_DCR_NATIVE */
  155. }
  156. #endif /* CONFIG_PPC_DCR */
  157. /*
  158. * For MMIO, get the physical address
  159. */
  160. reg = get_property(node, "reg", NULL);
  161. if (reg) {
  162. addr = of_translate_address(node, reg);
  163. if (addr != OF_BAD_ADDR) {
  164. snprintf(name, BUS_ID_SIZE,
  165. "%llx.%s", (unsigned long long)addr,
  166. node->name);
  167. return;
  168. }
  169. }
  170. /*
  171. * No BusID, use the node name and pray
  172. */
  173. snprintf(name, BUS_ID_SIZE, "%s", node->name);
  174. }
  175. struct of_device* of_platform_device_create(struct device_node *np,
  176. const char *bus_id,
  177. struct device *parent)
  178. {
  179. struct of_device *dev;
  180. dev = kmalloc(sizeof(*dev), GFP_KERNEL);
  181. if (!dev)
  182. return NULL;
  183. memset(dev, 0, sizeof(*dev));
  184. dev->node = of_node_get(np);
  185. dev->dma_mask = 0xffffffffUL;
  186. dev->dev.dma_mask = &dev->dma_mask;
  187. dev->dev.parent = parent;
  188. dev->dev.bus = &of_platform_bus_type;
  189. dev->dev.release = of_release_dev;
  190. dev->dev.archdata.of_node = np;
  191. dev->dev.archdata.numa_node = of_node_to_nid(np);
  192. /* We do not fill the DMA ops for platform devices by default.
  193. * This is currently the responsibility of the platform code
  194. * to do such, possibly using a device notifier
  195. */
  196. if (bus_id)
  197. strlcpy(dev->dev.bus_id, bus_id, BUS_ID_SIZE);
  198. else
  199. of_platform_make_bus_id(dev);
  200. if (of_device_register(dev) != 0) {
  201. kfree(dev);
  202. return NULL;
  203. }
  204. return dev;
  205. }
  206. EXPORT_SYMBOL(of_platform_device_create);
  207. /**
  208. * of_platform_bus_create - Create an OF device for a bus node and all its
  209. * children. Optionally recursively instanciate matching busses.
  210. * @bus: device node of the bus to instanciate
  211. * @matches: match table, NULL to use the default, OF_NO_DEEP_PROBE to
  212. * disallow recursive creation of child busses
  213. */
  214. static int of_platform_bus_create(struct device_node *bus,
  215. struct of_device_id *matches,
  216. struct device *parent)
  217. {
  218. struct device_node *child;
  219. struct of_device *dev;
  220. int rc = 0;
  221. for (child = NULL; (child = of_get_next_child(bus, child)); ) {
  222. pr_debug(" create child: %s\n", child->full_name);
  223. dev = of_platform_device_create(child, NULL, parent);
  224. if (dev == NULL)
  225. rc = -ENOMEM;
  226. else if (!of_match_node(matches, child))
  227. continue;
  228. if (rc == 0) {
  229. pr_debug(" and sub busses\n");
  230. rc = of_platform_bus_create(child, matches, &dev->dev);
  231. } if (rc) {
  232. of_node_put(child);
  233. break;
  234. }
  235. }
  236. return rc;
  237. }
  238. /**
  239. * of_platform_bus_probe - Probe the device-tree for platform busses
  240. * @root: parent of the first level to probe or NULL for the root of the tree
  241. * @matches: match table, NULL to use the default
  242. * @parent: parent to hook devices from, NULL for toplevel
  243. *
  244. * Note that children of the provided root are not instanciated as devices
  245. * unless the specified root itself matches the bus list and is not NULL.
  246. */
  247. int of_platform_bus_probe(struct device_node *root,
  248. struct of_device_id *matches,
  249. struct device *parent)
  250. {
  251. struct device_node *child;
  252. struct of_device *dev;
  253. int rc = 0;
  254. if (matches == NULL)
  255. matches = of_default_bus_ids;
  256. if (matches == OF_NO_DEEP_PROBE)
  257. return -EINVAL;
  258. if (root == NULL)
  259. root = of_find_node_by_path("/");
  260. else
  261. of_node_get(root);
  262. pr_debug("of_platform_bus_probe()\n");
  263. pr_debug(" starting at: %s\n", root->full_name);
  264. /* Do a self check of bus type, if there's a match, create
  265. * children
  266. */
  267. if (of_match_node(matches, root)) {
  268. pr_debug(" root match, create all sub devices\n");
  269. dev = of_platform_device_create(root, NULL, parent);
  270. if (dev == NULL) {
  271. rc = -ENOMEM;
  272. goto bail;
  273. }
  274. pr_debug(" create all sub busses\n");
  275. rc = of_platform_bus_create(root, matches, &dev->dev);
  276. goto bail;
  277. }
  278. for (child = NULL; (child = of_get_next_child(root, child)); ) {
  279. if (!of_match_node(matches, child))
  280. continue;
  281. pr_debug(" match: %s\n", child->full_name);
  282. dev = of_platform_device_create(child, NULL, parent);
  283. if (dev == NULL)
  284. rc = -ENOMEM;
  285. else
  286. rc = of_platform_bus_create(child, matches, &dev->dev);
  287. if (rc) {
  288. of_node_put(child);
  289. break;
  290. }
  291. }
  292. bail:
  293. of_node_put(root);
  294. return rc;
  295. }
  296. EXPORT_SYMBOL(of_platform_bus_probe);
  297. static int of_dev_node_match(struct device *dev, void *data)
  298. {
  299. return to_of_device(dev)->node == data;
  300. }
  301. struct of_device *of_find_device_by_node(struct device_node *np)
  302. {
  303. struct device *dev;
  304. dev = bus_find_device(&of_platform_bus_type,
  305. NULL, np, of_dev_node_match);
  306. if (dev)
  307. return to_of_device(dev);
  308. return NULL;
  309. }
  310. EXPORT_SYMBOL(of_find_device_by_node);
  311. static int of_dev_phandle_match(struct device *dev, void *data)
  312. {
  313. phandle *ph = data;
  314. return to_of_device(dev)->node->linux_phandle == *ph;
  315. }
  316. struct of_device *of_find_device_by_phandle(phandle ph)
  317. {
  318. struct device *dev;
  319. dev = bus_find_device(&of_platform_bus_type,
  320. NULL, &ph, of_dev_phandle_match);
  321. if (dev)
  322. return to_of_device(dev);
  323. return NULL;
  324. }
  325. EXPORT_SYMBOL(of_find_device_by_phandle);