platform.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  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. #define pr_fmt(fmt) "OF: " fmt
  15. #include <linux/errno.h>
  16. #include <linux/module.h>
  17. #include <linux/amba/bus.h>
  18. #include <linux/device.h>
  19. #include <linux/dma-mapping.h>
  20. #include <linux/slab.h>
  21. #include <linux/of_address.h>
  22. #include <linux/of_device.h>
  23. #include <linux/of_irq.h>
  24. #include <linux/of_platform.h>
  25. #include <linux/platform_device.h>
  26. const struct of_device_id of_default_bus_match_table[] = {
  27. { .compatible = "simple-bus", },
  28. { .compatible = "simple-mfd", },
  29. #ifdef CONFIG_ARM_AMBA
  30. { .compatible = "arm,amba-bus", },
  31. #endif /* CONFIG_ARM_AMBA */
  32. {} /* Empty terminated list */
  33. };
  34. static int of_dev_node_match(struct device *dev, void *data)
  35. {
  36. return dev->of_node == data;
  37. }
  38. /**
  39. * of_find_device_by_node - Find the platform_device associated with a node
  40. * @np: Pointer to device tree node
  41. *
  42. * Returns platform_device pointer, or NULL if not found
  43. */
  44. struct platform_device *of_find_device_by_node(struct device_node *np)
  45. {
  46. struct device *dev;
  47. dev = bus_find_device(&platform_bus_type, NULL, np, of_dev_node_match);
  48. return dev ? to_platform_device(dev) : NULL;
  49. }
  50. EXPORT_SYMBOL(of_find_device_by_node);
  51. #ifdef CONFIG_OF_ADDRESS
  52. /*
  53. * The following routines scan a subtree and registers a device for
  54. * each applicable node.
  55. *
  56. * Note: sparc doesn't use these routines because it has a different
  57. * mechanism for creating devices from device tree nodes.
  58. */
  59. /**
  60. * of_device_make_bus_id - Use the device node data to assign a unique name
  61. * @dev: pointer to device structure that is linked to a device tree node
  62. *
  63. * This routine will first try using the translated bus address to
  64. * derive a unique name. If it cannot, then it will prepend names from
  65. * parent nodes until a unique name can be derived.
  66. */
  67. void of_device_make_bus_id(struct device *dev)
  68. {
  69. struct device_node *node = dev->of_node;
  70. const __be32 *reg;
  71. u64 addr;
  72. /* Construct the name, using parent nodes if necessary to ensure uniqueness */
  73. while (node->parent) {
  74. /*
  75. * If the address can be translated, then that is as much
  76. * uniqueness as we need. Make it the first component and return
  77. */
  78. reg = of_get_property(node, "reg", NULL);
  79. if (reg && (addr = of_translate_address(node, reg)) != OF_BAD_ADDR) {
  80. dev_set_name(dev, dev_name(dev) ? "%llx.%s:%s" : "%llx.%s",
  81. (unsigned long long)addr, node->name,
  82. dev_name(dev));
  83. return;
  84. }
  85. /* format arguments only used if dev_name() resolves to NULL */
  86. dev_set_name(dev, dev_name(dev) ? "%s:%s" : "%s",
  87. strrchr(node->full_name, '/') + 1, dev_name(dev));
  88. node = node->parent;
  89. }
  90. }
  91. /**
  92. * of_device_alloc - Allocate and initialize an of_device
  93. * @np: device node to assign to device
  94. * @bus_id: Name to assign to the device. May be null to use default name.
  95. * @parent: Parent device.
  96. */
  97. struct platform_device *of_device_alloc(struct device_node *np,
  98. const char *bus_id,
  99. struct device *parent)
  100. {
  101. struct platform_device *dev;
  102. int rc, i, num_reg = 0, num_irq;
  103. struct resource *res, temp_res;
  104. dev = platform_device_alloc("", -1);
  105. if (!dev)
  106. return NULL;
  107. /* count the io and irq resources */
  108. while (of_address_to_resource(np, num_reg, &temp_res) == 0)
  109. num_reg++;
  110. num_irq = of_irq_count(np);
  111. /* Populate the resource table */
  112. if (num_irq || num_reg) {
  113. res = kzalloc(sizeof(*res) * (num_irq + num_reg), GFP_KERNEL);
  114. if (!res) {
  115. platform_device_put(dev);
  116. return NULL;
  117. }
  118. dev->num_resources = num_reg + num_irq;
  119. dev->resource = res;
  120. for (i = 0; i < num_reg; i++, res++) {
  121. rc = of_address_to_resource(np, i, res);
  122. WARN_ON(rc);
  123. }
  124. if (of_irq_to_resource_table(np, res, num_irq) != num_irq)
  125. pr_debug("not all legacy IRQ resources mapped for %s\n",
  126. np->name);
  127. }
  128. dev->dev.of_node = of_node_get(np);
  129. dev->dev.fwnode = &np->fwnode;
  130. dev->dev.parent = parent ? : &platform_bus;
  131. if (bus_id)
  132. dev_set_name(&dev->dev, "%s", bus_id);
  133. else
  134. of_device_make_bus_id(&dev->dev);
  135. return dev;
  136. }
  137. EXPORT_SYMBOL(of_device_alloc);
  138. static void of_dma_deconfigure(struct device *dev)
  139. {
  140. arch_teardown_dma_ops(dev);
  141. }
  142. /**
  143. * of_platform_device_create_pdata - Alloc, initialize and register an of_device
  144. * @np: pointer to node to create device for
  145. * @bus_id: name to assign device
  146. * @platform_data: pointer to populate platform_data pointer with
  147. * @parent: Linux device model parent device.
  148. *
  149. * Returns pointer to created platform device, or NULL if a device was not
  150. * registered. Unavailable devices will not get registered.
  151. */
  152. static struct platform_device *of_platform_device_create_pdata(
  153. struct device_node *np,
  154. const char *bus_id,
  155. void *platform_data,
  156. struct device *parent)
  157. {
  158. struct platform_device *dev;
  159. if (!of_device_is_available(np) ||
  160. of_node_test_and_set_flag(np, OF_POPULATED))
  161. return NULL;
  162. dev = of_device_alloc(np, bus_id, parent);
  163. if (!dev)
  164. goto err_clear_flag;
  165. dev->dev.bus = &platform_bus_type;
  166. dev->dev.platform_data = platform_data;
  167. of_dma_configure(&dev->dev, dev->dev.of_node);
  168. of_msi_configure(&dev->dev, dev->dev.of_node);
  169. if (of_device_add(dev) != 0) {
  170. of_dma_deconfigure(&dev->dev);
  171. platform_device_put(dev);
  172. goto err_clear_flag;
  173. }
  174. return dev;
  175. err_clear_flag:
  176. of_node_clear_flag(np, OF_POPULATED);
  177. return NULL;
  178. }
  179. /**
  180. * of_platform_device_create - Alloc, initialize and register an of_device
  181. * @np: pointer to node to create device for
  182. * @bus_id: name to assign device
  183. * @parent: Linux device model parent device.
  184. *
  185. * Returns pointer to created platform device, or NULL if a device was not
  186. * registered. Unavailable devices will not get registered.
  187. */
  188. struct platform_device *of_platform_device_create(struct device_node *np,
  189. const char *bus_id,
  190. struct device *parent)
  191. {
  192. return of_platform_device_create_pdata(np, bus_id, NULL, parent);
  193. }
  194. EXPORT_SYMBOL(of_platform_device_create);
  195. #ifdef CONFIG_ARM_AMBA
  196. static struct amba_device *of_amba_device_create(struct device_node *node,
  197. const char *bus_id,
  198. void *platform_data,
  199. struct device *parent)
  200. {
  201. struct amba_device *dev;
  202. const void *prop;
  203. int i, ret;
  204. pr_debug("Creating amba device %s\n", node->full_name);
  205. if (!of_device_is_available(node) ||
  206. of_node_test_and_set_flag(node, OF_POPULATED))
  207. return NULL;
  208. dev = amba_device_alloc(NULL, 0, 0);
  209. if (!dev)
  210. goto err_clear_flag;
  211. /* setup generic device info */
  212. dev->dev.of_node = of_node_get(node);
  213. dev->dev.fwnode = &node->fwnode;
  214. dev->dev.parent = parent ? : &platform_bus;
  215. dev->dev.platform_data = platform_data;
  216. if (bus_id)
  217. dev_set_name(&dev->dev, "%s", bus_id);
  218. else
  219. of_device_make_bus_id(&dev->dev);
  220. of_dma_configure(&dev->dev, dev->dev.of_node);
  221. /* Allow the HW Peripheral ID to be overridden */
  222. prop = of_get_property(node, "arm,primecell-periphid", NULL);
  223. if (prop)
  224. dev->periphid = of_read_ulong(prop, 1);
  225. /* Decode the IRQs and address ranges */
  226. for (i = 0; i < AMBA_NR_IRQS; i++)
  227. dev->irq[i] = irq_of_parse_and_map(node, i);
  228. ret = of_address_to_resource(node, 0, &dev->res);
  229. if (ret) {
  230. pr_err("amba: of_address_to_resource() failed (%d) for %s\n",
  231. ret, node->full_name);
  232. goto err_free;
  233. }
  234. ret = amba_device_add(dev, &iomem_resource);
  235. if (ret) {
  236. pr_err("amba_device_add() failed (%d) for %s\n",
  237. ret, node->full_name);
  238. goto err_free;
  239. }
  240. return dev;
  241. err_free:
  242. amba_device_put(dev);
  243. err_clear_flag:
  244. of_node_clear_flag(node, OF_POPULATED);
  245. return NULL;
  246. }
  247. #else /* CONFIG_ARM_AMBA */
  248. static struct amba_device *of_amba_device_create(struct device_node *node,
  249. const char *bus_id,
  250. void *platform_data,
  251. struct device *parent)
  252. {
  253. return NULL;
  254. }
  255. #endif /* CONFIG_ARM_AMBA */
  256. /**
  257. * of_devname_lookup() - Given a device node, lookup the preferred Linux name
  258. */
  259. static const struct of_dev_auxdata *of_dev_lookup(const struct of_dev_auxdata *lookup,
  260. struct device_node *np)
  261. {
  262. const struct of_dev_auxdata *auxdata;
  263. struct resource res;
  264. int compatible = 0;
  265. if (!lookup)
  266. return NULL;
  267. auxdata = lookup;
  268. for (; auxdata->compatible; auxdata++) {
  269. if (!of_device_is_compatible(np, auxdata->compatible))
  270. continue;
  271. compatible++;
  272. if (!of_address_to_resource(np, 0, &res))
  273. if (res.start != auxdata->phys_addr)
  274. continue;
  275. pr_debug("%s: devname=%s\n", np->full_name, auxdata->name);
  276. return auxdata;
  277. }
  278. if (!compatible)
  279. return NULL;
  280. /* Try compatible match if no phys_addr and name are specified */
  281. auxdata = lookup;
  282. for (; auxdata->compatible; auxdata++) {
  283. if (!of_device_is_compatible(np, auxdata->compatible))
  284. continue;
  285. if (!auxdata->phys_addr && !auxdata->name) {
  286. pr_debug("%s: compatible match\n", np->full_name);
  287. return auxdata;
  288. }
  289. }
  290. return NULL;
  291. }
  292. /**
  293. * of_platform_bus_create() - Create a device for a node and its children.
  294. * @bus: device node of the bus to instantiate
  295. * @matches: match table for bus nodes
  296. * @lookup: auxdata table for matching id and platform_data with device nodes
  297. * @parent: parent for new device, or NULL for top level.
  298. * @strict: require compatible property
  299. *
  300. * Creates a platform_device for the provided device_node, and optionally
  301. * recursively create devices for all the child nodes.
  302. */
  303. static int of_platform_bus_create(struct device_node *bus,
  304. const struct of_device_id *matches,
  305. const struct of_dev_auxdata *lookup,
  306. struct device *parent, bool strict)
  307. {
  308. const struct of_dev_auxdata *auxdata;
  309. struct device_node *child;
  310. struct platform_device *dev;
  311. const char *bus_id = NULL;
  312. void *platform_data = NULL;
  313. int rc = 0;
  314. /* Make sure it has a compatible property */
  315. if (strict && (!of_get_property(bus, "compatible", NULL))) {
  316. pr_debug("%s() - skipping %s, no compatible prop\n",
  317. __func__, bus->full_name);
  318. return 0;
  319. }
  320. if (of_node_check_flag(bus, OF_POPULATED_BUS)) {
  321. pr_debug("%s() - skipping %s, already populated\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. /*
  332. * Don't return an error here to keep compatibility with older
  333. * device tree files.
  334. */
  335. of_amba_device_create(bus, bus_id, platform_data, parent);
  336. return 0;
  337. }
  338. dev = of_platform_device_create_pdata(bus, bus_id, platform_data, parent);
  339. if (!dev || !of_match_node(matches, bus))
  340. return 0;
  341. for_each_child_of_node(bus, child) {
  342. pr_debug(" create child: %s\n", child->full_name);
  343. rc = of_platform_bus_create(child, matches, lookup, &dev->dev, strict);
  344. if (rc) {
  345. of_node_put(child);
  346. break;
  347. }
  348. }
  349. of_node_set_flag(bus, OF_POPULATED_BUS);
  350. return rc;
  351. }
  352. /**
  353. * of_platform_bus_probe() - Probe the device-tree for platform buses
  354. * @root: parent of the first level to probe or NULL for the root of the tree
  355. * @matches: match table for bus nodes
  356. * @parent: parent to hook devices from, NULL for toplevel
  357. *
  358. * Note that children of the provided root are not instantiated as devices
  359. * unless the specified root itself matches the bus list and is not NULL.
  360. */
  361. int of_platform_bus_probe(struct device_node *root,
  362. const struct of_device_id *matches,
  363. struct device *parent)
  364. {
  365. struct device_node *child;
  366. int rc = 0;
  367. root = root ? of_node_get(root) : of_find_node_by_path("/");
  368. if (!root)
  369. return -EINVAL;
  370. pr_debug("%s()\n", __func__);
  371. pr_debug(" starting at: %s\n", root->full_name);
  372. /* Do a self check of bus type, if there's a match, create children */
  373. if (of_match_node(matches, root)) {
  374. rc = of_platform_bus_create(root, matches, NULL, parent, false);
  375. } else for_each_child_of_node(root, child) {
  376. if (!of_match_node(matches, child))
  377. continue;
  378. rc = of_platform_bus_create(child, matches, NULL, parent, false);
  379. if (rc) {
  380. of_node_put(child);
  381. break;
  382. }
  383. }
  384. of_node_put(root);
  385. return rc;
  386. }
  387. EXPORT_SYMBOL(of_platform_bus_probe);
  388. /**
  389. * of_platform_populate() - Populate platform_devices from device tree data
  390. * @root: parent of the first level to probe or NULL for the root of the tree
  391. * @matches: match table, NULL to use the default
  392. * @lookup: auxdata table for matching id and platform_data with device nodes
  393. * @parent: parent to hook devices from, NULL for toplevel
  394. *
  395. * Similar to of_platform_bus_probe(), this function walks the device tree
  396. * and creates devices from nodes. It differs in that it follows the modern
  397. * convention of requiring all device nodes to have a 'compatible' property,
  398. * and it is suitable for creating devices which are children of the root
  399. * node (of_platform_bus_probe will only create children of the root which
  400. * are selected by the @matches argument).
  401. *
  402. * New board support should be using this function instead of
  403. * of_platform_bus_probe().
  404. *
  405. * Returns 0 on success, < 0 on failure.
  406. */
  407. int of_platform_populate(struct device_node *root,
  408. const struct of_device_id *matches,
  409. const struct of_dev_auxdata *lookup,
  410. struct device *parent)
  411. {
  412. struct device_node *child;
  413. int rc = 0;
  414. root = root ? of_node_get(root) : of_find_node_by_path("/");
  415. if (!root)
  416. return -EINVAL;
  417. pr_debug("%s()\n", __func__);
  418. pr_debug(" starting at: %s\n", root->full_name);
  419. for_each_child_of_node(root, child) {
  420. rc = of_platform_bus_create(child, matches, lookup, parent, true);
  421. if (rc) {
  422. of_node_put(child);
  423. break;
  424. }
  425. }
  426. of_node_set_flag(root, OF_POPULATED_BUS);
  427. of_node_put(root);
  428. return rc;
  429. }
  430. EXPORT_SYMBOL_GPL(of_platform_populate);
  431. int of_platform_default_populate(struct device_node *root,
  432. const struct of_dev_auxdata *lookup,
  433. struct device *parent)
  434. {
  435. return of_platform_populate(root, of_default_bus_match_table, lookup,
  436. parent);
  437. }
  438. EXPORT_SYMBOL_GPL(of_platform_default_populate);
  439. #ifndef CONFIG_PPC
  440. static int __init of_platform_default_populate_init(void)
  441. {
  442. struct device_node *node;
  443. if (!of_have_populated_dt())
  444. return -ENODEV;
  445. /*
  446. * Handle ramoops explicitly, since it is inside /reserved-memory,
  447. * which lacks a "compatible" property.
  448. */
  449. node = of_find_node_by_path("/reserved-memory");
  450. if (node) {
  451. node = of_find_compatible_node(node, NULL, "ramoops");
  452. if (node)
  453. of_platform_device_create(node, NULL, NULL);
  454. }
  455. /* Populate everything else. */
  456. of_platform_default_populate(NULL, NULL, NULL);
  457. return 0;
  458. }
  459. arch_initcall_sync(of_platform_default_populate_init);
  460. #endif
  461. static int of_platform_device_destroy(struct device *dev, void *data)
  462. {
  463. /* Do not touch devices not populated from the device tree */
  464. if (!dev->of_node || !of_node_check_flag(dev->of_node, OF_POPULATED))
  465. return 0;
  466. /* Recurse for any nodes that were treated as busses */
  467. if (of_node_check_flag(dev->of_node, OF_POPULATED_BUS))
  468. device_for_each_child(dev, NULL, of_platform_device_destroy);
  469. if (dev->bus == &platform_bus_type)
  470. platform_device_unregister(to_platform_device(dev));
  471. #ifdef CONFIG_ARM_AMBA
  472. else if (dev->bus == &amba_bustype)
  473. amba_device_unregister(to_amba_device(dev));
  474. #endif
  475. of_dma_deconfigure(dev);
  476. of_node_clear_flag(dev->of_node, OF_POPULATED);
  477. of_node_clear_flag(dev->of_node, OF_POPULATED_BUS);
  478. return 0;
  479. }
  480. /**
  481. * of_platform_depopulate() - Remove devices populated from device tree
  482. * @parent: device which children will be removed
  483. *
  484. * Complementary to of_platform_populate(), this function removes children
  485. * of the given device (and, recurrently, their children) that have been
  486. * created from their respective device tree nodes (and only those,
  487. * leaving others - eg. manually created - unharmed).
  488. *
  489. * Returns 0 when all children devices have been removed or
  490. * -EBUSY when some children remained.
  491. */
  492. void of_platform_depopulate(struct device *parent)
  493. {
  494. if (parent->of_node && of_node_check_flag(parent->of_node, OF_POPULATED_BUS)) {
  495. device_for_each_child(parent, NULL, of_platform_device_destroy);
  496. of_node_clear_flag(parent->of_node, OF_POPULATED_BUS);
  497. }
  498. }
  499. EXPORT_SYMBOL_GPL(of_platform_depopulate);
  500. #ifdef CONFIG_OF_DYNAMIC
  501. static int of_platform_notify(struct notifier_block *nb,
  502. unsigned long action, void *arg)
  503. {
  504. struct of_reconfig_data *rd = arg;
  505. struct platform_device *pdev_parent, *pdev;
  506. bool children_left;
  507. switch (of_reconfig_get_state_change(action, rd)) {
  508. case OF_RECONFIG_CHANGE_ADD:
  509. /* verify that the parent is a bus */
  510. if (!of_node_check_flag(rd->dn->parent, OF_POPULATED_BUS))
  511. return NOTIFY_OK; /* not for us */
  512. /* already populated? (driver using of_populate manually) */
  513. if (of_node_check_flag(rd->dn, OF_POPULATED))
  514. return NOTIFY_OK;
  515. /* pdev_parent may be NULL when no bus platform device */
  516. pdev_parent = of_find_device_by_node(rd->dn->parent);
  517. pdev = of_platform_device_create(rd->dn, NULL,
  518. pdev_parent ? &pdev_parent->dev : NULL);
  519. of_dev_put(pdev_parent);
  520. if (pdev == NULL) {
  521. pr_err("%s: failed to create for '%s'\n",
  522. __func__, rd->dn->full_name);
  523. /* of_platform_device_create tosses the error code */
  524. return notifier_from_errno(-EINVAL);
  525. }
  526. break;
  527. case OF_RECONFIG_CHANGE_REMOVE:
  528. /* already depopulated? */
  529. if (!of_node_check_flag(rd->dn, OF_POPULATED))
  530. return NOTIFY_OK;
  531. /* find our device by node */
  532. pdev = of_find_device_by_node(rd->dn);
  533. if (pdev == NULL)
  534. return NOTIFY_OK; /* no? not meant for us */
  535. /* unregister takes one ref away */
  536. of_platform_device_destroy(&pdev->dev, &children_left);
  537. /* and put the reference of the find */
  538. of_dev_put(pdev);
  539. break;
  540. }
  541. return NOTIFY_OK;
  542. }
  543. static struct notifier_block platform_of_notifier = {
  544. .notifier_call = of_platform_notify,
  545. };
  546. void of_platform_register_reconfig_notifier(void)
  547. {
  548. WARN_ON(of_reconfig_notifier_register(&platform_of_notifier));
  549. }
  550. #endif /* CONFIG_OF_DYNAMIC */
  551. #endif /* CONFIG_OF_ADDRESS */