platform.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  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;
  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. /**
  136. * of_dma_configure - Setup DMA configuration
  137. * @dev: Device to apply DMA configuration
  138. *
  139. * Try to get devices's DMA configuration from DT and update it
  140. * accordingly.
  141. *
  142. * In case if platform code need to use own special DMA configuration,it
  143. * can use Platform bus notifier and handle BUS_NOTIFY_ADD_DEVICE event
  144. * to fix up DMA configuration.
  145. */
  146. static void of_dma_configure(struct platform_device *pdev)
  147. {
  148. u64 dma_addr, paddr, size;
  149. int ret;
  150. struct device *dev = &pdev->dev;
  151. /*
  152. * Set default dma-mask to 32 bit. Drivers are expected to setup
  153. * the correct supported dma_mask.
  154. */
  155. dev->coherent_dma_mask = DMA_BIT_MASK(32);
  156. /*
  157. * Set it to coherent_dma_mask by default if the architecture
  158. * code has not set it.
  159. */
  160. if (!dev->dma_mask)
  161. dev->dma_mask = &dev->coherent_dma_mask;
  162. /*
  163. * if dma-coherent property exist, call arch hook to setup
  164. * dma coherent operations.
  165. */
  166. if (of_dma_is_coherent(dev->of_node)) {
  167. set_arch_dma_coherent_ops(dev);
  168. dev_dbg(dev, "device is dma coherent\n");
  169. }
  170. /*
  171. * if dma-ranges property doesn't exist - just return else
  172. * setup the dma offset
  173. */
  174. ret = of_dma_get_range(dev->of_node, &dma_addr, &paddr, &size);
  175. if (ret < 0) {
  176. dev_dbg(dev, "no dma range information to setup\n");
  177. return;
  178. }
  179. /* DMA ranges found. Calculate and set dma_pfn_offset */
  180. dev->dma_pfn_offset = PFN_DOWN(paddr - dma_addr);
  181. dev_dbg(dev, "dma_pfn_offset(%#08lx)\n", dev->dma_pfn_offset);
  182. }
  183. /**
  184. * of_platform_device_create_pdata - Alloc, initialize and register an of_device
  185. * @np: pointer to node to create device for
  186. * @bus_id: name to assign device
  187. * @platform_data: pointer to populate platform_data pointer with
  188. * @parent: Linux device model parent device.
  189. *
  190. * Returns pointer to created platform device, or NULL if a device was not
  191. * registered. Unavailable devices will not get registered.
  192. */
  193. static struct platform_device *of_platform_device_create_pdata(
  194. struct device_node *np,
  195. const char *bus_id,
  196. void *platform_data,
  197. struct device *parent)
  198. {
  199. struct platform_device *dev;
  200. if (!of_device_is_available(np) ||
  201. of_node_test_and_set_flag(np, OF_POPULATED))
  202. return NULL;
  203. dev = of_device_alloc(np, bus_id, parent);
  204. if (!dev)
  205. goto err_clear_flag;
  206. of_dma_configure(dev);
  207. dev->dev.bus = &platform_bus_type;
  208. dev->dev.platform_data = platform_data;
  209. /* We do not fill the DMA ops for platform devices by default.
  210. * This is currently the responsibility of the platform code
  211. * to do such, possibly using a device notifier
  212. */
  213. if (of_device_add(dev) != 0) {
  214. platform_device_put(dev);
  215. goto err_clear_flag;
  216. }
  217. return dev;
  218. err_clear_flag:
  219. of_node_clear_flag(np, OF_POPULATED);
  220. return NULL;
  221. }
  222. /**
  223. * of_platform_device_create - Alloc, initialize and register an of_device
  224. * @np: pointer to node to create device for
  225. * @bus_id: name to assign device
  226. * @parent: Linux device model parent device.
  227. *
  228. * Returns pointer to created platform device, or NULL if a device was not
  229. * registered. Unavailable devices will not get registered.
  230. */
  231. struct platform_device *of_platform_device_create(struct device_node *np,
  232. const char *bus_id,
  233. struct device *parent)
  234. {
  235. return of_platform_device_create_pdata(np, bus_id, NULL, parent);
  236. }
  237. EXPORT_SYMBOL(of_platform_device_create);
  238. #ifdef CONFIG_ARM_AMBA
  239. static struct amba_device *of_amba_device_create(struct device_node *node,
  240. const char *bus_id,
  241. void *platform_data,
  242. struct device *parent)
  243. {
  244. struct amba_device *dev;
  245. const void *prop;
  246. int i, ret;
  247. pr_debug("Creating amba device %s\n", node->full_name);
  248. if (!of_device_is_available(node) ||
  249. of_node_test_and_set_flag(node, OF_POPULATED))
  250. return NULL;
  251. dev = amba_device_alloc(NULL, 0, 0);
  252. if (!dev) {
  253. pr_err("%s(): amba_device_alloc() failed for %s\n",
  254. __func__, node->full_name);
  255. goto err_clear_flag;
  256. }
  257. /* setup generic device info */
  258. dev->dev.coherent_dma_mask = ~0;
  259. dev->dev.of_node = of_node_get(node);
  260. dev->dev.parent = parent;
  261. dev->dev.platform_data = platform_data;
  262. if (bus_id)
  263. dev_set_name(&dev->dev, "%s", bus_id);
  264. else
  265. of_device_make_bus_id(&dev->dev);
  266. /* Allow the HW Peripheral ID to be overridden */
  267. prop = of_get_property(node, "arm,primecell-periphid", NULL);
  268. if (prop)
  269. dev->periphid = of_read_ulong(prop, 1);
  270. /* Decode the IRQs and address ranges */
  271. for (i = 0; i < AMBA_NR_IRQS; i++)
  272. dev->irq[i] = irq_of_parse_and_map(node, i);
  273. ret = of_address_to_resource(node, 0, &dev->res);
  274. if (ret) {
  275. pr_err("%s(): of_address_to_resource() failed (%d) for %s\n",
  276. __func__, ret, node->full_name);
  277. goto err_free;
  278. }
  279. ret = amba_device_add(dev, &iomem_resource);
  280. if (ret) {
  281. pr_err("%s(): amba_device_add() failed (%d) for %s\n",
  282. __func__, ret, node->full_name);
  283. goto err_free;
  284. }
  285. return dev;
  286. err_free:
  287. amba_device_put(dev);
  288. err_clear_flag:
  289. of_node_clear_flag(node, OF_POPULATED);
  290. return NULL;
  291. }
  292. #else /* CONFIG_ARM_AMBA */
  293. static struct amba_device *of_amba_device_create(struct device_node *node,
  294. const char *bus_id,
  295. void *platform_data,
  296. struct device *parent)
  297. {
  298. return NULL;
  299. }
  300. #endif /* CONFIG_ARM_AMBA */
  301. /**
  302. * of_devname_lookup() - Given a device node, lookup the preferred Linux name
  303. */
  304. static const struct of_dev_auxdata *of_dev_lookup(const struct of_dev_auxdata *lookup,
  305. struct device_node *np)
  306. {
  307. struct resource res;
  308. if (!lookup)
  309. return NULL;
  310. for(; lookup->compatible != NULL; lookup++) {
  311. if (!of_device_is_compatible(np, lookup->compatible))
  312. continue;
  313. if (!of_address_to_resource(np, 0, &res))
  314. if (res.start != lookup->phys_addr)
  315. continue;
  316. pr_debug("%s: devname=%s\n", np->full_name, lookup->name);
  317. return lookup;
  318. }
  319. return NULL;
  320. }
  321. /**
  322. * of_platform_bus_create() - Create a device for a node and its children.
  323. * @bus: device node of the bus to instantiate
  324. * @matches: match table for bus nodes
  325. * @lookup: auxdata table for matching id and platform_data with device nodes
  326. * @parent: parent for new device, or NULL for top level.
  327. * @strict: require compatible property
  328. *
  329. * Creates a platform_device for the provided device_node, and optionally
  330. * recursively create devices for all the child nodes.
  331. */
  332. static int of_platform_bus_create(struct device_node *bus,
  333. const struct of_device_id *matches,
  334. const struct of_dev_auxdata *lookup,
  335. struct device *parent, bool strict)
  336. {
  337. const struct of_dev_auxdata *auxdata;
  338. struct device_node *child;
  339. struct platform_device *dev;
  340. const char *bus_id = NULL;
  341. void *platform_data = NULL;
  342. int rc = 0;
  343. /* Make sure it has a compatible property */
  344. if (strict && (!of_get_property(bus, "compatible", NULL))) {
  345. pr_debug("%s() - skipping %s, no compatible prop\n",
  346. __func__, bus->full_name);
  347. return 0;
  348. }
  349. auxdata = of_dev_lookup(lookup, bus);
  350. if (auxdata) {
  351. bus_id = auxdata->name;
  352. platform_data = auxdata->platform_data;
  353. }
  354. if (of_device_is_compatible(bus, "arm,primecell")) {
  355. /*
  356. * Don't return an error here to keep compatibility with older
  357. * device tree files.
  358. */
  359. of_amba_device_create(bus, bus_id, platform_data, parent);
  360. return 0;
  361. }
  362. dev = of_platform_device_create_pdata(bus, bus_id, platform_data, parent);
  363. if (!dev || !of_match_node(matches, bus))
  364. return 0;
  365. for_each_child_of_node(bus, child) {
  366. pr_debug(" create child: %s\n", child->full_name);
  367. rc = of_platform_bus_create(child, matches, lookup, &dev->dev, strict);
  368. if (rc) {
  369. of_node_put(child);
  370. break;
  371. }
  372. }
  373. return rc;
  374. }
  375. /**
  376. * of_platform_bus_probe() - Probe the device-tree for platform buses
  377. * @root: parent of the first level to probe or NULL for the root of the tree
  378. * @matches: match table for bus nodes
  379. * @parent: parent to hook devices from, NULL for toplevel
  380. *
  381. * Note that children of the provided root are not instantiated as devices
  382. * unless the specified root itself matches the bus list and is not NULL.
  383. */
  384. int of_platform_bus_probe(struct device_node *root,
  385. const struct of_device_id *matches,
  386. struct device *parent)
  387. {
  388. struct device_node *child;
  389. int rc = 0;
  390. root = root ? of_node_get(root) : of_find_node_by_path("/");
  391. if (!root)
  392. return -EINVAL;
  393. pr_debug("of_platform_bus_probe()\n");
  394. pr_debug(" starting at: %s\n", root->full_name);
  395. /* Do a self check of bus type, if there's a match, create children */
  396. if (of_match_node(matches, root)) {
  397. rc = of_platform_bus_create(root, matches, NULL, parent, false);
  398. } else for_each_child_of_node(root, child) {
  399. if (!of_match_node(matches, child))
  400. continue;
  401. rc = of_platform_bus_create(child, matches, NULL, parent, false);
  402. if (rc)
  403. break;
  404. }
  405. of_node_put(root);
  406. return rc;
  407. }
  408. EXPORT_SYMBOL(of_platform_bus_probe);
  409. /**
  410. * of_platform_populate() - Populate platform_devices from device tree data
  411. * @root: parent of the first level to probe or NULL for the root of the tree
  412. * @matches: match table, NULL to use the default
  413. * @lookup: auxdata table for matching id and platform_data with device nodes
  414. * @parent: parent to hook devices from, NULL for toplevel
  415. *
  416. * Similar to of_platform_bus_probe(), this function walks the device tree
  417. * and creates devices from nodes. It differs in that it follows the modern
  418. * convention of requiring all device nodes to have a 'compatible' property,
  419. * and it is suitable for creating devices which are children of the root
  420. * node (of_platform_bus_probe will only create children of the root which
  421. * are selected by the @matches argument).
  422. *
  423. * New board support should be using this function instead of
  424. * of_platform_bus_probe().
  425. *
  426. * Returns 0 on success, < 0 on failure.
  427. */
  428. int of_platform_populate(struct device_node *root,
  429. const struct of_device_id *matches,
  430. const struct of_dev_auxdata *lookup,
  431. struct device *parent)
  432. {
  433. struct device_node *child;
  434. int rc = 0;
  435. root = root ? of_node_get(root) : of_find_node_by_path("/");
  436. if (!root)
  437. return -EINVAL;
  438. for_each_child_of_node(root, child) {
  439. rc = of_platform_bus_create(child, matches, lookup, parent, true);
  440. if (rc)
  441. break;
  442. }
  443. of_node_put(root);
  444. return rc;
  445. }
  446. EXPORT_SYMBOL_GPL(of_platform_populate);
  447. static int of_platform_device_destroy(struct device *dev, void *data)
  448. {
  449. bool *children_left = data;
  450. /* Do not touch devices not populated from the device tree */
  451. if (!dev->of_node || !of_node_check_flag(dev->of_node, OF_POPULATED)) {
  452. *children_left = true;
  453. return 0;
  454. }
  455. /* Recurse, but don't touch this device if it has any children left */
  456. if (of_platform_depopulate(dev) != 0) {
  457. *children_left = true;
  458. return 0;
  459. }
  460. if (dev->bus == &platform_bus_type)
  461. platform_device_unregister(to_platform_device(dev));
  462. #ifdef CONFIG_ARM_AMBA
  463. else if (dev->bus == &amba_bustype)
  464. amba_device_unregister(to_amba_device(dev));
  465. #endif
  466. else {
  467. *children_left = true;
  468. return 0;
  469. }
  470. of_node_clear_flag(dev->of_node, OF_POPULATED);
  471. return 0;
  472. }
  473. /**
  474. * of_platform_depopulate() - Remove devices populated from device tree
  475. * @parent: device which childred will be removed
  476. *
  477. * Complementary to of_platform_populate(), this function removes children
  478. * of the given device (and, recurrently, their children) that have been
  479. * created from their respective device tree nodes (and only those,
  480. * leaving others - eg. manually created - unharmed).
  481. *
  482. * Returns 0 when all children devices have been removed or
  483. * -EBUSY when some children remained.
  484. */
  485. int of_platform_depopulate(struct device *parent)
  486. {
  487. bool children_left = false;
  488. device_for_each_child(parent, &children_left,
  489. of_platform_device_destroy);
  490. return children_left ? -EBUSY : 0;
  491. }
  492. EXPORT_SYMBOL_GPL(of_platform_depopulate);
  493. #endif /* CONFIG_OF_ADDRESS */