platform.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  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. static 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(32);
  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. pr_err("%s(): amba_device_alloc() failed for %s\n",
  239. __func__, node->full_name);
  240. return NULL;
  241. }
  242. /* setup generic device info */
  243. dev->dev.coherent_dma_mask = ~0;
  244. dev->dev.of_node = of_node_get(node);
  245. dev->dev.parent = parent;
  246. dev->dev.platform_data = platform_data;
  247. if (bus_id)
  248. dev_set_name(&dev->dev, "%s", bus_id);
  249. else
  250. of_device_make_bus_id(&dev->dev);
  251. /* setup amba-specific device info */
  252. dev->dma_mask = ~0;
  253. /* Allow the HW Peripheral ID to be overridden */
  254. prop = of_get_property(node, "arm,primecell-periphid", NULL);
  255. if (prop)
  256. dev->periphid = of_read_ulong(prop, 1);
  257. /* Decode the IRQs and address ranges */
  258. for (i = 0; i < AMBA_NR_IRQS; i++)
  259. dev->irq[i] = irq_of_parse_and_map(node, i);
  260. ret = of_address_to_resource(node, 0, &dev->res);
  261. if (ret) {
  262. pr_err("%s(): of_address_to_resource() failed (%d) for %s\n",
  263. __func__, ret, node->full_name);
  264. goto err_free;
  265. }
  266. ret = amba_device_add(dev, &iomem_resource);
  267. if (ret) {
  268. pr_err("%s(): amba_device_add() failed (%d) for %s\n",
  269. __func__, ret, node->full_name);
  270. goto err_free;
  271. }
  272. return dev;
  273. err_free:
  274. amba_device_put(dev);
  275. return NULL;
  276. }
  277. #else /* CONFIG_ARM_AMBA */
  278. static struct amba_device *of_amba_device_create(struct device_node *node,
  279. const char *bus_id,
  280. void *platform_data,
  281. struct device *parent)
  282. {
  283. return NULL;
  284. }
  285. #endif /* CONFIG_ARM_AMBA */
  286. /**
  287. * of_devname_lookup() - Given a device node, lookup the preferred Linux name
  288. */
  289. static const struct of_dev_auxdata *of_dev_lookup(const struct of_dev_auxdata *lookup,
  290. struct device_node *np)
  291. {
  292. struct resource res;
  293. if (!lookup)
  294. return NULL;
  295. for(; lookup->compatible != NULL; lookup++) {
  296. if (!of_device_is_compatible(np, lookup->compatible))
  297. continue;
  298. if (!of_address_to_resource(np, 0, &res))
  299. if (res.start != lookup->phys_addr)
  300. continue;
  301. pr_debug("%s: devname=%s\n", np->full_name, lookup->name);
  302. return lookup;
  303. }
  304. return NULL;
  305. }
  306. /**
  307. * of_platform_bus_create() - Create a device for a node and its children.
  308. * @bus: device node of the bus to instantiate
  309. * @matches: match table for bus nodes
  310. * @lookup: auxdata table for matching id and platform_data with device nodes
  311. * @parent: parent for new device, or NULL for top level.
  312. * @strict: require compatible property
  313. *
  314. * Creates a platform_device for the provided device_node, and optionally
  315. * recursively create devices for all the child nodes.
  316. */
  317. static int of_platform_bus_create(struct device_node *bus,
  318. const struct of_device_id *matches,
  319. const struct of_dev_auxdata *lookup,
  320. struct device *parent, bool strict)
  321. {
  322. const struct of_dev_auxdata *auxdata;
  323. struct device_node *child;
  324. struct platform_device *dev;
  325. const char *bus_id = NULL;
  326. void *platform_data = NULL;
  327. int rc = 0;
  328. /* Make sure it has a compatible property */
  329. if (strict && (!of_get_property(bus, "compatible", NULL))) {
  330. pr_debug("%s() - skipping %s, no compatible prop\n",
  331. __func__, bus->full_name);
  332. return 0;
  333. }
  334. auxdata = of_dev_lookup(lookup, bus);
  335. if (auxdata) {
  336. bus_id = auxdata->name;
  337. platform_data = auxdata->platform_data;
  338. }
  339. if (of_device_is_compatible(bus, "arm,primecell")) {
  340. /*
  341. * Don't return an error here to keep compatibility with older
  342. * device tree files.
  343. */
  344. of_amba_device_create(bus, bus_id, platform_data, parent);
  345. return 0;
  346. }
  347. dev = of_platform_device_create_pdata(bus, bus_id, platform_data, parent);
  348. if (!dev || !of_match_node(matches, bus))
  349. return 0;
  350. for_each_child_of_node(bus, child) {
  351. pr_debug(" create child: %s\n", child->full_name);
  352. rc = of_platform_bus_create(child, matches, lookup, &dev->dev, strict);
  353. if (rc) {
  354. of_node_put(child);
  355. break;
  356. }
  357. }
  358. return rc;
  359. }
  360. /**
  361. * of_platform_bus_probe() - Probe the device-tree for platform buses
  362. * @root: parent of the first level to probe or NULL for the root of the tree
  363. * @matches: match table for bus nodes
  364. * @parent: parent to hook devices from, NULL for toplevel
  365. *
  366. * Note that children of the provided root are not instantiated as devices
  367. * unless the specified root itself matches the bus list and is not NULL.
  368. */
  369. int of_platform_bus_probe(struct device_node *root,
  370. const struct of_device_id *matches,
  371. struct device *parent)
  372. {
  373. struct device_node *child;
  374. int rc = 0;
  375. root = root ? of_node_get(root) : of_find_node_by_path("/");
  376. if (!root)
  377. return -EINVAL;
  378. pr_debug("of_platform_bus_probe()\n");
  379. pr_debug(" starting at: %s\n", root->full_name);
  380. /* Do a self check of bus type, if there's a match, create children */
  381. if (of_match_node(matches, root)) {
  382. rc = of_platform_bus_create(root, matches, NULL, parent, false);
  383. } else for_each_child_of_node(root, child) {
  384. if (!of_match_node(matches, child))
  385. continue;
  386. rc = of_platform_bus_create(child, matches, NULL, parent, false);
  387. if (rc)
  388. break;
  389. }
  390. of_node_put(root);
  391. return rc;
  392. }
  393. EXPORT_SYMBOL(of_platform_bus_probe);
  394. /**
  395. * of_platform_populate() - Populate platform_devices from device tree data
  396. * @root: parent of the first level to probe or NULL for the root of the tree
  397. * @matches: match table, NULL to use the default
  398. * @lookup: auxdata table for matching id and platform_data with device nodes
  399. * @parent: parent to hook devices from, NULL for toplevel
  400. *
  401. * Similar to of_platform_bus_probe(), this function walks the device tree
  402. * and creates devices from nodes. It differs in that it follows the modern
  403. * convention of requiring all device nodes to have a 'compatible' property,
  404. * and it is suitable for creating devices which are children of the root
  405. * node (of_platform_bus_probe will only create children of the root which
  406. * are selected by the @matches argument).
  407. *
  408. * New board support should be using this function instead of
  409. * of_platform_bus_probe().
  410. *
  411. * Returns 0 on success, < 0 on failure.
  412. */
  413. int of_platform_populate(struct device_node *root,
  414. const struct of_device_id *matches,
  415. const struct of_dev_auxdata *lookup,
  416. struct device *parent)
  417. {
  418. struct device_node *child;
  419. int rc = 0;
  420. root = root ? of_node_get(root) : of_find_node_by_path("/");
  421. if (!root)
  422. return -EINVAL;
  423. for_each_child_of_node(root, child) {
  424. rc = of_platform_bus_create(child, matches, lookup, parent, true);
  425. if (rc)
  426. break;
  427. }
  428. of_node_put(root);
  429. return rc;
  430. }
  431. EXPORT_SYMBOL_GPL(of_platform_populate);
  432. #endif /* CONFIG_OF_ADDRESS */