platform.c 16 KB

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