platform.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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. const struct of_device_id of_default_bus_match_table[] = {
  25. { .compatible = "simple-bus", },
  26. #ifdef CONFIG_ARM_AMBA
  27. { .compatible = "arm,amba-bus", },
  28. #endif /* CONFIG_ARM_AMBA */
  29. {} /* Empty terminated list */
  30. };
  31. static int of_dev_node_match(struct device *dev, void *data)
  32. {
  33. return dev->of_node == data;
  34. }
  35. /**
  36. * of_find_device_by_node - Find the platform_device associated with a node
  37. * @np: Pointer to device tree node
  38. *
  39. * Returns platform_device pointer, or NULL if not found
  40. */
  41. struct platform_device *of_find_device_by_node(struct device_node *np)
  42. {
  43. struct device *dev;
  44. dev = bus_find_device(&platform_bus_type, NULL, np, of_dev_node_match);
  45. return dev ? to_platform_device(dev) : NULL;
  46. }
  47. EXPORT_SYMBOL(of_find_device_by_node);
  48. #if defined(CONFIG_PPC_DCR)
  49. #include <asm/dcr.h>
  50. #endif
  51. #if !defined(CONFIG_SPARC)
  52. /*
  53. * The following routines scan a subtree and registers a device for
  54. * each applicable node.
  55. *
  56. * Note: sparc doesn't use these routines because it has a different
  57. * mechanism for creating devices from device tree nodes.
  58. */
  59. /**
  60. * of_device_make_bus_id - Use the device node data to assign a unique name
  61. * @dev: pointer to device structure that is linked to a device tree node
  62. *
  63. * This routine will first try using either the dcr-reg or the reg property
  64. * value to derive a unique name. As a last resort it will use the node
  65. * name followed by a unique number.
  66. */
  67. void of_device_make_bus_id(struct device *dev)
  68. {
  69. static atomic_t bus_no_reg_magic;
  70. struct device_node *node = dev->of_node;
  71. const u32 *reg;
  72. u64 addr;
  73. int magic;
  74. #ifdef CONFIG_PPC_DCR
  75. /*
  76. * If it's a DCR based device, use 'd' for native DCRs
  77. * and 'D' for MMIO DCRs.
  78. */
  79. reg = of_get_property(node, "dcr-reg", NULL);
  80. if (reg) {
  81. #ifdef CONFIG_PPC_DCR_NATIVE
  82. dev_set_name(dev, "d%x.%s", *reg, node->name);
  83. #else /* CONFIG_PPC_DCR_NATIVE */
  84. u64 addr = of_translate_dcr_address(node, *reg, NULL);
  85. if (addr != OF_BAD_ADDR) {
  86. dev_set_name(dev, "D%llx.%s",
  87. (unsigned long long)addr, node->name);
  88. return;
  89. }
  90. #endif /* !CONFIG_PPC_DCR_NATIVE */
  91. }
  92. #endif /* CONFIG_PPC_DCR */
  93. /*
  94. * For MMIO, get the physical address
  95. */
  96. reg = of_get_property(node, "reg", NULL);
  97. if (reg) {
  98. addr = of_translate_address(node, reg);
  99. if (addr != OF_BAD_ADDR) {
  100. dev_set_name(dev, "%llx.%s",
  101. (unsigned long long)addr, node->name);
  102. return;
  103. }
  104. }
  105. /*
  106. * No BusID, use the node name and add a globally incremented
  107. * counter (and pray...)
  108. */
  109. magic = atomic_add_return(1, &bus_no_reg_magic);
  110. dev_set_name(dev, "%s.%d", node->name, magic - 1);
  111. }
  112. /**
  113. * of_device_alloc - Allocate and initialize an of_device
  114. * @np: device node to assign to device
  115. * @bus_id: Name to assign to the device. May be null to use default name.
  116. * @parent: Parent device.
  117. */
  118. struct platform_device *of_device_alloc(struct device_node *np,
  119. const char *bus_id,
  120. struct device *parent)
  121. {
  122. struct platform_device *dev;
  123. int rc, i, num_reg = 0, num_irq;
  124. struct resource *res, temp_res;
  125. dev = platform_device_alloc("", -1);
  126. if (!dev)
  127. return NULL;
  128. /* count the io and irq resources */
  129. while (of_address_to_resource(np, num_reg, &temp_res) == 0)
  130. num_reg++;
  131. num_irq = of_irq_count(np);
  132. /* Populate the resource table */
  133. if (num_irq || num_reg) {
  134. res = kzalloc(sizeof(*res) * (num_irq + num_reg), GFP_KERNEL);
  135. if (!res) {
  136. platform_device_put(dev);
  137. return NULL;
  138. }
  139. dev->num_resources = num_reg + num_irq;
  140. dev->resource = res;
  141. for (i = 0; i < num_reg; i++, res++) {
  142. rc = of_address_to_resource(np, i, res);
  143. WARN_ON(rc);
  144. }
  145. WARN_ON(of_irq_to_resource_table(np, res, num_irq) != num_irq);
  146. }
  147. dev->dev.of_node = of_node_get(np);
  148. #if defined(CONFIG_PPC) || defined(CONFIG_MICROBLAZE)
  149. dev->dev.dma_mask = &dev->archdata.dma_mask;
  150. #endif
  151. dev->dev.parent = parent;
  152. if (bus_id)
  153. dev_set_name(&dev->dev, "%s", bus_id);
  154. else
  155. of_device_make_bus_id(&dev->dev);
  156. return dev;
  157. }
  158. EXPORT_SYMBOL(of_device_alloc);
  159. /**
  160. * of_platform_device_create - Alloc, initialize and register an of_device
  161. * @np: pointer to node to create device for
  162. * @bus_id: name to assign device
  163. * @parent: Linux device model parent device.
  164. *
  165. * Returns pointer to created platform device, or NULL if a device was not
  166. * registered. Unavailable devices will not get registered.
  167. */
  168. struct platform_device *of_platform_device_create(struct device_node *np,
  169. const char *bus_id,
  170. struct device *parent)
  171. {
  172. struct platform_device *dev;
  173. if (!of_device_is_available(np))
  174. return NULL;
  175. dev = of_device_alloc(np, bus_id, parent);
  176. if (!dev)
  177. return NULL;
  178. #if defined(CONFIG_PPC) || defined(CONFIG_MICROBLAZE)
  179. dev->archdata.dma_mask = 0xffffffffUL;
  180. #endif
  181. dev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
  182. dev->dev.bus = &platform_bus_type;
  183. /* We do not fill the DMA ops for platform devices by default.
  184. * This is currently the responsibility of the platform code
  185. * to do such, possibly using a device notifier
  186. */
  187. if (of_device_add(dev) != 0) {
  188. platform_device_put(dev);
  189. return NULL;
  190. }
  191. return dev;
  192. }
  193. EXPORT_SYMBOL(of_platform_device_create);
  194. /**
  195. * of_platform_bus_create() - Create a device for a node and its children.
  196. * @bus: device node of the bus to instantiate
  197. * @matches: match table for bus nodes
  198. * disallow recursive creation of child buses
  199. * @parent: parent for new device, or NULL for top level.
  200. *
  201. * Creates a platform_device for the provided device_node, and optionally
  202. * recursively create devices for all the child nodes.
  203. */
  204. static int of_platform_bus_create(struct device_node *bus,
  205. const struct of_device_id *matches,
  206. struct device *parent, bool strict)
  207. {
  208. struct device_node *child;
  209. struct platform_device *dev;
  210. int rc = 0;
  211. /* Make sure it has a compatible property */
  212. if (strict && (!of_get_property(bus, "compatible", NULL))) {
  213. pr_debug("%s() - skipping %s, no compatible prop\n",
  214. __func__, bus->full_name);
  215. return 0;
  216. }
  217. dev = of_platform_device_create(bus, NULL, parent);
  218. if (!dev || !of_match_node(matches, bus))
  219. return 0;
  220. for_each_child_of_node(bus, child) {
  221. pr_debug(" create child: %s\n", child->full_name);
  222. rc = of_platform_bus_create(child, matches, &dev->dev, strict);
  223. if (rc) {
  224. of_node_put(child);
  225. break;
  226. }
  227. }
  228. return rc;
  229. }
  230. /**
  231. * of_platform_bus_probe() - Probe the device-tree for platform buses
  232. * @root: parent of the first level to probe or NULL for the root of the tree
  233. * @matches: match table for bus nodes
  234. * @parent: parent to hook devices from, NULL for toplevel
  235. *
  236. * Note that children of the provided root are not instantiated as devices
  237. * unless the specified root itself matches the bus list and is not NULL.
  238. */
  239. int of_platform_bus_probe(struct device_node *root,
  240. const struct of_device_id *matches,
  241. struct device *parent)
  242. {
  243. struct device_node *child;
  244. int rc = 0;
  245. root = root ? of_node_get(root) : of_find_node_by_path("/");
  246. if (!root)
  247. return -EINVAL;
  248. pr_debug("of_platform_bus_probe()\n");
  249. pr_debug(" starting at: %s\n", root->full_name);
  250. /* Do a self check of bus type, if there's a match, create children */
  251. if (of_match_node(matches, root)) {
  252. rc = of_platform_bus_create(root, matches, parent, false);
  253. } else for_each_child_of_node(root, child) {
  254. if (!of_match_node(matches, child))
  255. continue;
  256. rc = of_platform_bus_create(child, matches, parent, false);
  257. if (rc)
  258. break;
  259. }
  260. of_node_put(root);
  261. return rc;
  262. }
  263. EXPORT_SYMBOL(of_platform_bus_probe);
  264. /**
  265. * of_platform_populate() - Populate platform_devices from device tree data
  266. * @root: parent of the first level to probe or NULL for the root of the tree
  267. * @matches: match table, NULL to use the default
  268. * @parent: parent to hook devices from, NULL for toplevel
  269. *
  270. * Similar to of_platform_bus_probe(), this function walks the device tree
  271. * and creates devices from nodes. It differs in that it follows the modern
  272. * convention of requiring all device nodes to have a 'compatible' property,
  273. * and it is suitable for creating devices which are children of the root
  274. * node (of_platform_bus_probe will only create children of the root which
  275. * are selected by the @matches argument).
  276. *
  277. * New board support should be using this function instead of
  278. * of_platform_bus_probe().
  279. *
  280. * Returns 0 on success, < 0 on failure.
  281. */
  282. int of_platform_populate(struct device_node *root,
  283. const struct of_device_id *matches,
  284. struct device *parent)
  285. {
  286. struct device_node *child;
  287. int rc = 0;
  288. root = root ? of_node_get(root) : of_find_node_by_path("/");
  289. if (!root)
  290. return -EINVAL;
  291. for_each_child_of_node(root, child) {
  292. rc = of_platform_bus_create(child, matches, parent, true);
  293. if (rc)
  294. break;
  295. }
  296. of_node_put(root);
  297. return rc;
  298. }
  299. #endif /* !CONFIG_SPARC */