of_platform.c 9.0 KB

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