platform.c 18 KB

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