platform.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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. int rc;
  188. if (!of_device_is_available(np))
  189. return NULL;
  190. dev = of_device_alloc(np, bus_id, parent);
  191. if (!dev)
  192. return NULL;
  193. #if defined(CONFIG_MICROBLAZE)
  194. dev->archdata.dma_mask = 0xffffffffUL;
  195. #endif
  196. dev->name = dev_name(&dev->dev);
  197. dev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
  198. dev->dev.platform_data = platform_data;
  199. dev->dev.id = PLATFORM_DEVID_NONE;
  200. /* device_add will assume that this device is on the same node as
  201. * the parent. If there is no parent defined, set the node
  202. * explicitly */
  203. if (!parent)
  204. set_dev_node(&dev->dev, of_node_to_nid(np));
  205. /* We do not fill the DMA ops for platform devices by default.
  206. * This is currently the responsibility of the platform code
  207. * to do such, possibly using a device notifier
  208. */
  209. rc = platform_device_add(dev);
  210. if (rc) {
  211. dev_err(&dev->dev, "device registration failed\n");
  212. platform_device_put(dev);
  213. return NULL;
  214. }
  215. return dev;
  216. }
  217. /**
  218. * of_platform_device_create - Alloc, initialize and register an of_device
  219. * @np: pointer to node to create device for
  220. * @bus_id: name to assign device
  221. * @parent: Linux device model parent device.
  222. *
  223. * Returns pointer to created platform device, or NULL if a device was not
  224. * registered. Unavailable devices will not get registered.
  225. */
  226. struct platform_device *of_platform_device_create(struct device_node *np,
  227. const char *bus_id,
  228. struct device *parent)
  229. {
  230. return of_platform_device_create_pdata(np, bus_id, NULL, parent);
  231. }
  232. EXPORT_SYMBOL(of_platform_device_create);
  233. #ifdef CONFIG_ARM_AMBA
  234. static struct amba_device *of_amba_device_create(struct device_node *node,
  235. const char *bus_id,
  236. void *platform_data,
  237. struct device *parent)
  238. {
  239. struct amba_device *dev;
  240. const void *prop;
  241. int i, ret;
  242. pr_debug("Creating amba device %s\n", node->full_name);
  243. if (!of_device_is_available(node))
  244. return NULL;
  245. dev = amba_device_alloc(NULL, 0, 0);
  246. if (!dev)
  247. return NULL;
  248. /* setup generic device info */
  249. dev->dev.coherent_dma_mask = ~0;
  250. dev->dev.of_node = of_node_get(node);
  251. dev->dev.parent = parent;
  252. dev->dev.platform_data = platform_data;
  253. if (bus_id)
  254. dev_set_name(&dev->dev, "%s", bus_id);
  255. else
  256. of_device_make_bus_id(&dev->dev);
  257. /* setup amba-specific device info */
  258. dev->dma_mask = ~0;
  259. /* Allow the HW Peripheral ID to be overridden */
  260. prop = of_get_property(node, "arm,primecell-periphid", NULL);
  261. if (prop)
  262. dev->periphid = of_read_ulong(prop, 1);
  263. /* Decode the IRQs and address ranges */
  264. for (i = 0; i < AMBA_NR_IRQS; i++)
  265. dev->irq[i] = irq_of_parse_and_map(node, i);
  266. ret = of_address_to_resource(node, 0, &dev->res);
  267. if (ret)
  268. goto err_free;
  269. ret = amba_device_add(dev, &iomem_resource);
  270. if (ret)
  271. goto err_free;
  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. of_amba_device_create(bus, bus_id, platform_data, parent);
  341. return 0;
  342. }
  343. dev = of_platform_device_create_pdata(bus, bus_id, platform_data, parent);
  344. if (!dev || !of_match_node(matches, bus))
  345. return 0;
  346. for_each_child_of_node(bus, child) {
  347. pr_debug(" create child: %s\n", child->full_name);
  348. rc = of_platform_bus_create(child, matches, lookup, &dev->dev, strict);
  349. if (rc) {
  350. of_node_put(child);
  351. break;
  352. }
  353. }
  354. return rc;
  355. }
  356. /**
  357. * of_platform_bus_probe() - Probe the device-tree for platform buses
  358. * @root: parent of the first level to probe or NULL for the root of the tree
  359. * @matches: match table for bus nodes
  360. * @parent: parent to hook devices from, NULL for toplevel
  361. *
  362. * Note that children of the provided root are not instantiated as devices
  363. * unless the specified root itself matches the bus list and is not NULL.
  364. */
  365. int of_platform_bus_probe(struct device_node *root,
  366. const struct of_device_id *matches,
  367. struct device *parent)
  368. {
  369. struct device_node *child;
  370. int rc = 0;
  371. root = root ? of_node_get(root) : of_find_node_by_path("/");
  372. if (!root)
  373. return -EINVAL;
  374. pr_debug("of_platform_bus_probe()\n");
  375. pr_debug(" starting at: %s\n", root->full_name);
  376. /* Do a self check of bus type, if there's a match, create children */
  377. if (of_match_node(matches, root)) {
  378. rc = of_platform_bus_create(root, matches, NULL, parent, false);
  379. } else for_each_child_of_node(root, child) {
  380. if (!of_match_node(matches, child))
  381. continue;
  382. rc = of_platform_bus_create(child, matches, NULL, parent, false);
  383. if (rc)
  384. break;
  385. }
  386. of_node_put(root);
  387. return rc;
  388. }
  389. EXPORT_SYMBOL(of_platform_bus_probe);
  390. /**
  391. * of_platform_populate() - Populate platform_devices from device tree data
  392. * @root: parent of the first level to probe or NULL for the root of the tree
  393. * @matches: match table, NULL to use the default
  394. * @parent: parent to hook devices from, NULL for toplevel
  395. *
  396. * Similar to of_platform_bus_probe(), this function walks the device tree
  397. * and creates devices from nodes. It differs in that it follows the modern
  398. * convention of requiring all device nodes to have a 'compatible' property,
  399. * and it is suitable for creating devices which are children of the root
  400. * node (of_platform_bus_probe will only create children of the root which
  401. * are selected by the @matches argument).
  402. *
  403. * New board support should be using this function instead of
  404. * of_platform_bus_probe().
  405. *
  406. * Returns 0 on success, < 0 on failure.
  407. */
  408. int of_platform_populate(struct device_node *root,
  409. const struct of_device_id *matches,
  410. const struct of_dev_auxdata *lookup,
  411. struct device *parent)
  412. {
  413. struct device_node *child;
  414. int rc = 0;
  415. root = root ? of_node_get(root) : of_find_node_by_path("/");
  416. if (!root)
  417. return -EINVAL;
  418. for_each_child_of_node(root, child) {
  419. rc = of_platform_bus_create(child, matches, lookup, parent, true);
  420. if (rc)
  421. break;
  422. }
  423. of_node_put(root);
  424. return rc;
  425. }
  426. EXPORT_SYMBOL_GPL(of_platform_populate);
  427. #endif /* CONFIG_OF_ADDRESS */