platform.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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 a device for a node and its children.
  189. * @bus: device node of the bus to instantiate
  190. * @matches: match table for bus nodes
  191. * disallow recursive creation of child buses
  192. * @parent: parent for new device, or NULL for top level.
  193. *
  194. * Creates a platform_device for the provided device_node, and optionally
  195. * recursively create devices for all the child nodes.
  196. */
  197. static int of_platform_bus_create(struct device_node *bus,
  198. const struct of_device_id *matches,
  199. struct device *parent)
  200. {
  201. struct device_node *child;
  202. struct platform_device *dev;
  203. int rc = 0;
  204. dev = of_platform_device_create(bus, NULL, parent);
  205. if (!dev || !of_match_node(matches, bus))
  206. return 0;
  207. for_each_child_of_node(bus, child) {
  208. pr_debug(" create child: %s\n", child->full_name);
  209. rc = of_platform_bus_create(child, matches, &dev->dev);
  210. if (rc) {
  211. of_node_put(child);
  212. break;
  213. }
  214. }
  215. return rc;
  216. }
  217. /**
  218. * of_platform_bus_probe() - Probe the device-tree for platform buses
  219. * @root: parent of the first level to probe or NULL for the root of the tree
  220. * @matches: match table for bus nodes
  221. * @parent: parent to hook devices from, NULL for toplevel
  222. *
  223. * Note that children of the provided root are not instantiated as devices
  224. * unless the specified root itself matches the bus list and is not NULL.
  225. */
  226. int of_platform_bus_probe(struct device_node *root,
  227. const struct of_device_id *matches,
  228. struct device *parent)
  229. {
  230. struct device_node *child;
  231. int rc = 0;
  232. root = root ? of_node_get(root) : of_find_node_by_path("/");
  233. if (!root)
  234. return -EINVAL;
  235. pr_debug("of_platform_bus_probe()\n");
  236. pr_debug(" starting at: %s\n", root->full_name);
  237. /* Do a self check of bus type, if there's a match, create children */
  238. if (of_match_node(matches, root)) {
  239. rc = of_platform_bus_create(root, matches, parent);
  240. } else for_each_child_of_node(root, child) {
  241. if (!of_match_node(matches, child))
  242. continue;
  243. rc = of_platform_bus_create(child, matches, parent);
  244. if (rc)
  245. break;
  246. }
  247. of_node_put(root);
  248. return rc;
  249. }
  250. EXPORT_SYMBOL(of_platform_bus_probe);
  251. #endif /* !CONFIG_SPARC */