platform.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /*
  2. * Copyright (C) 2006 Benjamin Herrenschmidt, IBM Corp.
  3. * <benh@kernel.crashing.org>
  4. * and Arnd Bergmann, IBM Corp.
  5. * Merged from powerpc/kernel/of_platform.c and
  6. * sparc{,64}/kernel/of_device.c by Stephen Rothwell
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version
  11. * 2 of the License, or (at your option) any later version.
  12. *
  13. */
  14. #include <linux/errno.h>
  15. #include <linux/module.h>
  16. #include <linux/device.h>
  17. #include <linux/dma-mapping.h>
  18. #include <linux/slab.h>
  19. #include <linux/of_address.h>
  20. #include <linux/of_device.h>
  21. #include <linux/of_irq.h>
  22. #include <linux/of_platform.h>
  23. #include <linux/platform_device.h>
  24. static int of_dev_node_match(struct device *dev, void *data)
  25. {
  26. return dev->of_node == data;
  27. }
  28. /**
  29. * of_find_device_by_node - Find the platform_device associated with a node
  30. * @np: Pointer to device tree node
  31. *
  32. * Returns platform_device pointer, or NULL if not found
  33. */
  34. struct platform_device *of_find_device_by_node(struct device_node *np)
  35. {
  36. struct device *dev;
  37. dev = bus_find_device(&platform_bus_type, NULL, np, of_dev_node_match);
  38. return dev ? to_platform_device(dev) : NULL;
  39. }
  40. EXPORT_SYMBOL(of_find_device_by_node);
  41. #if defined(CONFIG_PPC_DCR)
  42. #include <asm/dcr.h>
  43. #endif
  44. #if !defined(CONFIG_SPARC)
  45. /*
  46. * The following routines scan a subtree and registers a device for
  47. * each applicable node.
  48. *
  49. * Note: sparc doesn't use these routines because it has a different
  50. * mechanism for creating devices from device tree nodes.
  51. */
  52. /**
  53. * of_device_make_bus_id - Use the device node data to assign a unique name
  54. * @dev: pointer to device structure that is linked to a device tree node
  55. *
  56. * This routine will first try using either the dcr-reg or the reg property
  57. * value to derive a unique name. As a last resort it will use the node
  58. * name followed by a unique number.
  59. */
  60. void of_device_make_bus_id(struct device *dev)
  61. {
  62. static atomic_t bus_no_reg_magic;
  63. struct device_node *node = dev->of_node;
  64. const u32 *reg;
  65. u64 addr;
  66. int magic;
  67. #ifdef CONFIG_PPC_DCR
  68. /*
  69. * If it's a DCR based device, use 'd' for native DCRs
  70. * and 'D' for MMIO DCRs.
  71. */
  72. reg = of_get_property(node, "dcr-reg", NULL);
  73. if (reg) {
  74. #ifdef CONFIG_PPC_DCR_NATIVE
  75. dev_set_name(dev, "d%x.%s", *reg, node->name);
  76. #else /* CONFIG_PPC_DCR_NATIVE */
  77. u64 addr = of_translate_dcr_address(node, *reg, NULL);
  78. if (addr != OF_BAD_ADDR) {
  79. dev_set_name(dev, "D%llx.%s",
  80. (unsigned long long)addr, node->name);
  81. return;
  82. }
  83. #endif /* !CONFIG_PPC_DCR_NATIVE */
  84. }
  85. #endif /* CONFIG_PPC_DCR */
  86. /*
  87. * For MMIO, get the physical address
  88. */
  89. reg = of_get_property(node, "reg", NULL);
  90. if (reg) {
  91. addr = of_translate_address(node, reg);
  92. if (addr != OF_BAD_ADDR) {
  93. dev_set_name(dev, "%llx.%s",
  94. (unsigned long long)addr, node->name);
  95. return;
  96. }
  97. }
  98. /*
  99. * No BusID, use the node name and add a globally incremented
  100. * counter (and pray...)
  101. */
  102. magic = atomic_add_return(1, &bus_no_reg_magic);
  103. dev_set_name(dev, "%s.%d", node->name, magic - 1);
  104. }
  105. /**
  106. * of_device_alloc - Allocate and initialize an of_device
  107. * @np: device node to assign to device
  108. * @bus_id: Name to assign to the device. May be null to use default name.
  109. * @parent: Parent device.
  110. */
  111. struct platform_device *of_device_alloc(struct device_node *np,
  112. const char *bus_id,
  113. struct device *parent)
  114. {
  115. struct platform_device *dev;
  116. int rc, i, num_reg = 0, num_irq;
  117. struct resource *res, temp_res;
  118. dev = platform_device_alloc("", -1);
  119. if (!dev)
  120. return NULL;
  121. /* count the io and irq resources */
  122. while (of_address_to_resource(np, num_reg, &temp_res) == 0)
  123. num_reg++;
  124. num_irq = of_irq_count(np);
  125. /* Populate the resource table */
  126. if (num_irq || num_reg) {
  127. res = kzalloc(sizeof(*res) * (num_irq + num_reg), GFP_KERNEL);
  128. if (!res) {
  129. platform_device_put(dev);
  130. return NULL;
  131. }
  132. dev->num_resources = num_reg + num_irq;
  133. dev->resource = res;
  134. for (i = 0; i < num_reg; i++, res++) {
  135. rc = of_address_to_resource(np, i, res);
  136. WARN_ON(rc);
  137. }
  138. WARN_ON(of_irq_to_resource_table(np, res, num_irq) != num_irq);
  139. }
  140. dev->dev.of_node = of_node_get(np);
  141. #if defined(CONFIG_PPC) || defined(CONFIG_MICROBLAZE)
  142. dev->dev.dma_mask = &dev->archdata.dma_mask;
  143. #endif
  144. dev->dev.parent = parent;
  145. if (bus_id)
  146. dev_set_name(&dev->dev, "%s", bus_id);
  147. else
  148. of_device_make_bus_id(&dev->dev);
  149. return dev;
  150. }
  151. EXPORT_SYMBOL(of_device_alloc);
  152. /**
  153. * of_platform_device_create - Alloc, initialize and register an of_device
  154. * @np: pointer to node to create device for
  155. * @bus_id: name to assign device
  156. * @parent: Linux device model parent device.
  157. *
  158. * Returns pointer to created platform device, or NULL if a device was not
  159. * registered. Unavailable devices will not get registered.
  160. */
  161. struct platform_device *of_platform_device_create(struct device_node *np,
  162. const char *bus_id,
  163. struct device *parent)
  164. {
  165. struct platform_device *dev;
  166. if (!of_device_is_available(np))
  167. return NULL;
  168. dev = of_device_alloc(np, bus_id, parent);
  169. if (!dev)
  170. return NULL;
  171. #if defined(CONFIG_PPC) || defined(CONFIG_MICROBLAZE)
  172. dev->archdata.dma_mask = 0xffffffffUL;
  173. #endif
  174. dev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
  175. dev->dev.bus = &platform_bus_type;
  176. /* We do not fill the DMA ops for platform devices by default.
  177. * This is currently the responsibility of the platform code
  178. * to do such, possibly using a device notifier
  179. */
  180. if (of_device_add(dev) != 0) {
  181. platform_device_put(dev);
  182. return NULL;
  183. }
  184. return dev;
  185. }
  186. EXPORT_SYMBOL(of_platform_device_create);
  187. /**
  188. * of_platform_bus_create - Create an OF device for a bus node and all its
  189. * children. Optionally recursively instantiate matching busses.
  190. * @bus: device node of the bus to instantiate
  191. * @matches: match table, NULL to use the default, OF_NO_DEEP_PROBE to
  192. * disallow recursive creation of child busses
  193. */
  194. static int of_platform_bus_create(const struct device_node *bus,
  195. const struct of_device_id *matches,
  196. struct device *parent)
  197. {
  198. struct device_node *child;
  199. struct platform_device *dev;
  200. int rc = 0;
  201. for_each_child_of_node(bus, child) {
  202. pr_debug(" create child: %s\n", child->full_name);
  203. dev = of_platform_device_create(child, NULL, parent);
  204. if (dev == NULL)
  205. continue;
  206. if (!of_match_node(matches, child))
  207. continue;
  208. if (rc == 0) {
  209. pr_debug(" and sub busses\n");
  210. rc = of_platform_bus_create(child, matches, &dev->dev);
  211. }
  212. if (rc) {
  213. of_node_put(child);
  214. break;
  215. }
  216. }
  217. return rc;
  218. }
  219. /**
  220. * of_platform_bus_probe - Probe the device-tree for platform busses
  221. * @root: parent of the first level to probe or NULL for the root of the tree
  222. * @matches: match table, NULL to use the default
  223. * @parent: parent to hook devices from, NULL for toplevel
  224. *
  225. * Note that children of the provided root are not instantiated as devices
  226. * unless the specified root itself matches the bus list and is not NULL.
  227. */
  228. int of_platform_bus_probe(struct device_node *root,
  229. const struct of_device_id *matches,
  230. struct device *parent)
  231. {
  232. struct device_node *child;
  233. struct platform_device *dev;
  234. int rc = 0;
  235. if (WARN_ON(!matches || matches == OF_NO_DEEP_PROBE))
  236. return -EINVAL;
  237. if (root == NULL)
  238. root = of_find_node_by_path("/");
  239. else
  240. of_node_get(root);
  241. if (root == NULL)
  242. return -EINVAL;
  243. pr_debug("of_platform_bus_probe()\n");
  244. pr_debug(" starting at: %s\n", root->full_name);
  245. /* Do a self check of bus type, if there's a match, create
  246. * children
  247. */
  248. if (of_match_node(matches, root)) {
  249. pr_debug(" root match, create all sub devices\n");
  250. dev = of_platform_device_create(root, NULL, parent);
  251. if (dev == NULL)
  252. goto bail;
  253. pr_debug(" create all sub busses\n");
  254. rc = of_platform_bus_create(root, matches, &dev->dev);
  255. goto bail;
  256. }
  257. for_each_child_of_node(root, child) {
  258. if (!of_match_node(matches, child))
  259. continue;
  260. pr_debug(" match: %s\n", child->full_name);
  261. dev = of_platform_device_create(child, NULL, parent);
  262. if (dev == NULL)
  263. continue;
  264. rc = of_platform_bus_create(child, matches, &dev->dev);
  265. if (rc) {
  266. of_node_put(child);
  267. break;
  268. }
  269. }
  270. bail:
  271. of_node_put(root);
  272. return rc;
  273. }
  274. EXPORT_SYMBOL(of_platform_bus_probe);
  275. #endif /* !CONFIG_SPARC */