platform.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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/amba/bus.h>
  17. #include <linux/device.h>
  18. #include <linux/dma-mapping.h>
  19. #include <linux/slab.h>
  20. #include <linux/of_address.h>
  21. #include <linux/of_device.h>
  22. #include <linux/of_irq.h>
  23. #include <linux/of_platform.h>
  24. #include <linux/platform_device.h>
  25. const struct of_device_id of_default_bus_match_table[] = {
  26. { .compatible = "simple-bus", },
  27. #ifdef CONFIG_ARM_AMBA
  28. { .compatible = "arm,amba-bus", },
  29. #endif /* CONFIG_ARM_AMBA */
  30. {} /* Empty terminated list */
  31. };
  32. static int of_dev_node_match(struct device *dev, void *data)
  33. {
  34. return dev->of_node == data;
  35. }
  36. /**
  37. * of_find_device_by_node - Find the platform_device associated with a node
  38. * @np: Pointer to device tree node
  39. *
  40. * Returns platform_device pointer, or NULL if not found
  41. */
  42. struct platform_device *of_find_device_by_node(struct device_node *np)
  43. {
  44. struct device *dev;
  45. dev = bus_find_device(&platform_bus_type, NULL, np, of_dev_node_match);
  46. return dev ? to_platform_device(dev) : NULL;
  47. }
  48. EXPORT_SYMBOL(of_find_device_by_node);
  49. #if defined(CONFIG_PPC_DCR)
  50. #include <asm/dcr.h>
  51. #endif
  52. #if !defined(CONFIG_SPARC)
  53. /*
  54. * The following routines scan a subtree and registers a device for
  55. * each applicable node.
  56. *
  57. * Note: sparc doesn't use these routines because it has a different
  58. * mechanism for creating devices from device tree nodes.
  59. */
  60. /**
  61. * of_device_make_bus_id - Use the device node data to assign a unique name
  62. * @dev: pointer to device structure that is linked to a device tree node
  63. *
  64. * This routine will first try using either the dcr-reg or the reg property
  65. * value to derive a unique name. As a last resort it will use the node
  66. * name followed by a unique number.
  67. */
  68. void of_device_make_bus_id(struct device *dev)
  69. {
  70. static atomic_t bus_no_reg_magic;
  71. struct device_node *node = dev->of_node;
  72. const u32 *reg;
  73. u64 addr;
  74. int magic;
  75. #ifdef CONFIG_PPC_DCR
  76. /*
  77. * If it's a DCR based device, use 'd' for native DCRs
  78. * and 'D' for MMIO DCRs.
  79. */
  80. reg = of_get_property(node, "dcr-reg", NULL);
  81. if (reg) {
  82. #ifdef CONFIG_PPC_DCR_NATIVE
  83. dev_set_name(dev, "d%x.%s", *reg, node->name);
  84. #else /* CONFIG_PPC_DCR_NATIVE */
  85. u64 addr = of_translate_dcr_address(node, *reg, NULL);
  86. if (addr != OF_BAD_ADDR) {
  87. dev_set_name(dev, "D%llx.%s",
  88. (unsigned long long)addr, node->name);
  89. return;
  90. }
  91. #endif /* !CONFIG_PPC_DCR_NATIVE */
  92. }
  93. #endif /* CONFIG_PPC_DCR */
  94. /*
  95. * For MMIO, get the physical address
  96. */
  97. reg = of_get_property(node, "reg", NULL);
  98. if (reg) {
  99. addr = of_translate_address(node, reg);
  100. if (addr != OF_BAD_ADDR) {
  101. dev_set_name(dev, "%llx.%s",
  102. (unsigned long long)addr, node->name);
  103. return;
  104. }
  105. }
  106. /*
  107. * No BusID, use the node name and add a globally incremented
  108. * counter (and pray...)
  109. */
  110. magic = atomic_add_return(1, &bus_no_reg_magic);
  111. dev_set_name(dev, "%s.%d", node->name, magic - 1);
  112. }
  113. /**
  114. * of_device_alloc - Allocate and initialize an of_device
  115. * @np: device node to assign to device
  116. * @bus_id: Name to assign to the device. May be null to use default name.
  117. * @parent: Parent device.
  118. */
  119. struct platform_device *of_device_alloc(struct device_node *np,
  120. const char *bus_id,
  121. struct device *parent)
  122. {
  123. struct platform_device *dev;
  124. int rc, i, num_reg = 0, num_irq;
  125. struct resource *res, temp_res;
  126. dev = platform_device_alloc("", -1);
  127. if (!dev)
  128. return NULL;
  129. /* count the io and irq resources */
  130. while (of_address_to_resource(np, num_reg, &temp_res) == 0)
  131. num_reg++;
  132. num_irq = of_irq_count(np);
  133. /* Populate the resource table */
  134. if (num_irq || num_reg) {
  135. res = kzalloc(sizeof(*res) * (num_irq + num_reg), GFP_KERNEL);
  136. if (!res) {
  137. platform_device_put(dev);
  138. return NULL;
  139. }
  140. dev->num_resources = num_reg + num_irq;
  141. dev->resource = res;
  142. for (i = 0; i < num_reg; i++, res++) {
  143. rc = of_address_to_resource(np, i, res);
  144. WARN_ON(rc);
  145. }
  146. WARN_ON(of_irq_to_resource_table(np, res, num_irq) != num_irq);
  147. }
  148. dev->dev.of_node = of_node_get(np);
  149. #if defined(CONFIG_MICROBLAZE)
  150. dev->dev.dma_mask = &dev->archdata.dma_mask;
  151. #endif
  152. dev->dev.parent = parent;
  153. if (bus_id)
  154. dev_set_name(&dev->dev, "%s", bus_id);
  155. else
  156. of_device_make_bus_id(&dev->dev);
  157. return dev;
  158. }
  159. EXPORT_SYMBOL(of_device_alloc);
  160. /**
  161. * of_platform_device_create_pdata - Alloc, initialize and register an of_device
  162. * @np: pointer to node to create device for
  163. * @bus_id: name to assign device
  164. * @platform_data: pointer to populate platform_data pointer with
  165. * @parent: Linux device model parent device.
  166. *
  167. * Returns pointer to created platform device, or NULL if a device was not
  168. * registered. Unavailable devices will not get registered.
  169. */
  170. struct platform_device *of_platform_device_create_pdata(
  171. struct device_node *np,
  172. const char *bus_id,
  173. void *platform_data,
  174. struct device *parent)
  175. {
  176. struct platform_device *dev;
  177. if (!of_device_is_available(np))
  178. return NULL;
  179. dev = of_device_alloc(np, bus_id, parent);
  180. if (!dev)
  181. return NULL;
  182. #if defined(CONFIG_MICROBLAZE)
  183. dev->archdata.dma_mask = 0xffffffffUL;
  184. #endif
  185. dev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
  186. dev->dev.bus = &platform_bus_type;
  187. dev->dev.platform_data = platform_data;
  188. /* We do not fill the DMA ops for platform devices by default.
  189. * This is currently the responsibility of the platform code
  190. * to do such, possibly using a device notifier
  191. */
  192. if (of_device_add(dev) != 0) {
  193. platform_device_put(dev);
  194. return NULL;
  195. }
  196. return dev;
  197. }
  198. /**
  199. * of_platform_device_create - Alloc, initialize and register an of_device
  200. * @np: pointer to node to create device for
  201. * @bus_id: name to assign device
  202. * @parent: Linux device model parent device.
  203. *
  204. * Returns pointer to created platform device, or NULL if a device was not
  205. * registered. Unavailable devices will not get registered.
  206. */
  207. struct platform_device *of_platform_device_create(struct device_node *np,
  208. const char *bus_id,
  209. struct device *parent)
  210. {
  211. return of_platform_device_create_pdata(np, bus_id, NULL, parent);
  212. }
  213. EXPORT_SYMBOL(of_platform_device_create);
  214. #ifdef CONFIG_ARM_AMBA
  215. static struct amba_device *of_amba_device_create(struct device_node *node,
  216. const char *bus_id,
  217. void *platform_data,
  218. struct device *parent)
  219. {
  220. struct amba_device *dev;
  221. const void *prop;
  222. int i, ret;
  223. pr_debug("Creating amba device %s\n", node->full_name);
  224. if (!of_device_is_available(node))
  225. return NULL;
  226. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  227. if (!dev)
  228. return NULL;
  229. /* setup generic device info */
  230. dev->dev.coherent_dma_mask = ~0;
  231. dev->dev.of_node = of_node_get(node);
  232. dev->dev.parent = parent;
  233. dev->dev.platform_data = platform_data;
  234. if (bus_id)
  235. dev_set_name(&dev->dev, "%s", bus_id);
  236. else
  237. of_device_make_bus_id(&dev->dev);
  238. /* setup amba-specific device info */
  239. dev->dma_mask = ~0;
  240. /* Allow the HW Peripheral ID to be overridden */
  241. prop = of_get_property(node, "arm,primecell-periphid", NULL);
  242. if (prop)
  243. dev->periphid = of_read_ulong(prop, 1);
  244. /* Decode the IRQs and address ranges */
  245. for (i = 0; i < AMBA_NR_IRQS; i++)
  246. dev->irq[i] = irq_of_parse_and_map(node, i);
  247. ret = of_address_to_resource(node, 0, &dev->res);
  248. if (ret)
  249. goto err_free;
  250. ret = amba_device_register(dev, &iomem_resource);
  251. if (ret)
  252. goto err_free;
  253. return dev;
  254. err_free:
  255. kfree(dev);
  256. return NULL;
  257. }
  258. #else /* CONFIG_ARM_AMBA */
  259. static struct amba_device *of_amba_device_create(struct device_node *node,
  260. const char *bus_id,
  261. void *platform_data,
  262. struct device *parent)
  263. {
  264. return NULL;
  265. }
  266. #endif /* CONFIG_ARM_AMBA */
  267. /**
  268. * of_devname_lookup() - Given a device node, lookup the preferred Linux name
  269. */
  270. static const struct of_dev_auxdata *of_dev_lookup(const struct of_dev_auxdata *lookup,
  271. struct device_node *np)
  272. {
  273. struct resource res;
  274. if (!lookup)
  275. return NULL;
  276. for(; lookup->name != NULL; lookup++) {
  277. if (!of_device_is_compatible(np, lookup->compatible))
  278. continue;
  279. if (of_address_to_resource(np, 0, &res))
  280. continue;
  281. if (res.start != lookup->phys_addr)
  282. continue;
  283. pr_debug("%s: devname=%s\n", np->full_name, lookup->name);
  284. return lookup;
  285. }
  286. return NULL;
  287. }
  288. /**
  289. * of_platform_bus_create() - Create a device for a node and its children.
  290. * @bus: device node of the bus to instantiate
  291. * @matches: match table for bus nodes
  292. * @lookup: auxdata table for matching id and platform_data with device nodes
  293. * @parent: parent for new device, or NULL for top level.
  294. * @strict: require compatible property
  295. *
  296. * Creates a platform_device for the provided device_node, and optionally
  297. * recursively create devices for all the child nodes.
  298. */
  299. static int of_platform_bus_create(struct device_node *bus,
  300. const struct of_device_id *matches,
  301. const struct of_dev_auxdata *lookup,
  302. struct device *parent, bool strict)
  303. {
  304. const struct of_dev_auxdata *auxdata;
  305. struct device_node *child;
  306. struct platform_device *dev;
  307. const char *bus_id = NULL;
  308. void *platform_data = NULL;
  309. int rc = 0;
  310. /* Make sure it has a compatible property */
  311. if (strict && (!of_get_property(bus, "compatible", NULL))) {
  312. pr_debug("%s() - skipping %s, no compatible prop\n",
  313. __func__, bus->full_name);
  314. return 0;
  315. }
  316. auxdata = of_dev_lookup(lookup, bus);
  317. if (auxdata) {
  318. bus_id = auxdata->name;
  319. platform_data = auxdata->platform_data;
  320. }
  321. if (of_device_is_compatible(bus, "arm,primecell")) {
  322. of_amba_device_create(bus, bus_id, platform_data, parent);
  323. return 0;
  324. }
  325. dev = of_platform_device_create_pdata(bus, bus_id, platform_data, parent);
  326. if (!dev || !of_match_node(matches, bus))
  327. return 0;
  328. for_each_child_of_node(bus, child) {
  329. pr_debug(" create child: %s\n", child->full_name);
  330. rc = of_platform_bus_create(child, matches, lookup, &dev->dev, strict);
  331. if (rc) {
  332. of_node_put(child);
  333. break;
  334. }
  335. }
  336. return rc;
  337. }
  338. /**
  339. * of_platform_bus_probe() - Probe the device-tree for platform buses
  340. * @root: parent of the first level to probe or NULL for the root of the tree
  341. * @matches: match table for bus nodes
  342. * @parent: parent to hook devices from, NULL for toplevel
  343. *
  344. * Note that children of the provided root are not instantiated as devices
  345. * unless the specified root itself matches the bus list and is not NULL.
  346. */
  347. int of_platform_bus_probe(struct device_node *root,
  348. const struct of_device_id *matches,
  349. struct device *parent)
  350. {
  351. struct device_node *child;
  352. int rc = 0;
  353. root = root ? of_node_get(root) : of_find_node_by_path("/");
  354. if (!root)
  355. return -EINVAL;
  356. pr_debug("of_platform_bus_probe()\n");
  357. pr_debug(" starting at: %s\n", root->full_name);
  358. /* Do a self check of bus type, if there's a match, create children */
  359. if (of_match_node(matches, root)) {
  360. rc = of_platform_bus_create(root, matches, NULL, parent, false);
  361. } else for_each_child_of_node(root, child) {
  362. if (!of_match_node(matches, child))
  363. continue;
  364. rc = of_platform_bus_create(child, matches, NULL, parent, false);
  365. if (rc)
  366. break;
  367. }
  368. of_node_put(root);
  369. return rc;
  370. }
  371. EXPORT_SYMBOL(of_platform_bus_probe);
  372. /**
  373. * of_platform_populate() - Populate platform_devices from device tree data
  374. * @root: parent of the first level to probe or NULL for the root of the tree
  375. * @matches: match table, NULL to use the default
  376. * @parent: parent to hook devices from, NULL for toplevel
  377. *
  378. * Similar to of_platform_bus_probe(), this function walks the device tree
  379. * and creates devices from nodes. It differs in that it follows the modern
  380. * convention of requiring all device nodes to have a 'compatible' property,
  381. * and it is suitable for creating devices which are children of the root
  382. * node (of_platform_bus_probe will only create children of the root which
  383. * are selected by the @matches argument).
  384. *
  385. * New board support should be using this function instead of
  386. * of_platform_bus_probe().
  387. *
  388. * Returns 0 on success, < 0 on failure.
  389. */
  390. int of_platform_populate(struct device_node *root,
  391. const struct of_device_id *matches,
  392. const struct of_dev_auxdata *lookup,
  393. struct device *parent)
  394. {
  395. struct device_node *child;
  396. int rc = 0;
  397. root = root ? of_node_get(root) : of_find_node_by_path("/");
  398. if (!root)
  399. return -EINVAL;
  400. for_each_child_of_node(root, child) {
  401. rc = of_platform_bus_create(child, matches, lookup, parent, true);
  402. if (rc)
  403. break;
  404. }
  405. of_node_put(root);
  406. return rc;
  407. }
  408. #endif /* !CONFIG_SPARC */