platform.c 13 KB

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