platform.c 13 KB

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