platform.c 16 KB

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