of_coresight.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /* Copyright (c) 2012, The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. */
  12. #include <linux/types.h>
  13. #include <linux/err.h>
  14. #include <linux/slab.h>
  15. #include <linux/clk.h>
  16. #include <linux/of.h>
  17. #include <linux/of_address.h>
  18. #include <linux/of_graph.h>
  19. #include <linux/of_platform.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/amba/bus.h>
  22. #include <linux/coresight.h>
  23. #include <linux/cpumask.h>
  24. #include <asm/smp_plat.h>
  25. static int of_dev_node_match(struct device *dev, void *data)
  26. {
  27. return dev->of_node == data;
  28. }
  29. static struct device *
  30. of_coresight_get_endpoint_device(struct device_node *endpoint)
  31. {
  32. struct device *dev = NULL;
  33. /*
  34. * If we have a non-configurable replicator, it will be found on the
  35. * platform bus.
  36. */
  37. dev = bus_find_device(&platform_bus_type, NULL,
  38. endpoint, of_dev_node_match);
  39. if (dev)
  40. return dev;
  41. /*
  42. * We have a configurable component - circle through the AMBA bus
  43. * looking for the device that matches the endpoint node.
  44. */
  45. return bus_find_device(&amba_bustype, NULL,
  46. endpoint, of_dev_node_match);
  47. }
  48. static void of_coresight_get_ports(const struct device_node *node,
  49. int *nr_inport, int *nr_outport)
  50. {
  51. struct device_node *ep = NULL;
  52. int in = 0, out = 0;
  53. do {
  54. ep = of_graph_get_next_endpoint(node, ep);
  55. if (!ep)
  56. break;
  57. if (of_property_read_bool(ep, "slave-mode"))
  58. in++;
  59. else
  60. out++;
  61. } while (ep);
  62. *nr_inport = in;
  63. *nr_outport = out;
  64. }
  65. static int of_coresight_alloc_memory(struct device *dev,
  66. struct coresight_platform_data *pdata)
  67. {
  68. /* List of output port on this component */
  69. pdata->outports = devm_kzalloc(dev, pdata->nr_outport *
  70. sizeof(*pdata->outports),
  71. GFP_KERNEL);
  72. if (!pdata->outports)
  73. return -ENOMEM;
  74. /* Children connected to this component via @outports */
  75. pdata->child_names = devm_kzalloc(dev, pdata->nr_outport *
  76. sizeof(*pdata->child_names),
  77. GFP_KERNEL);
  78. if (!pdata->child_names)
  79. return -ENOMEM;
  80. /* Port number on the child this component is connected to */
  81. pdata->child_ports = devm_kzalloc(dev, pdata->nr_outport *
  82. sizeof(*pdata->child_ports),
  83. GFP_KERNEL);
  84. if (!pdata->child_ports)
  85. return -ENOMEM;
  86. return 0;
  87. }
  88. int of_coresight_get_cpu(const struct device_node *node)
  89. {
  90. int cpu;
  91. bool found;
  92. struct device_node *dn, *np;
  93. dn = of_parse_phandle(node, "cpu", 0);
  94. /* Affinity defaults to CPU0 */
  95. if (!dn)
  96. return 0;
  97. for_each_possible_cpu(cpu) {
  98. np = of_cpu_device_node_get(cpu);
  99. found = (dn == np);
  100. of_node_put(np);
  101. if (found)
  102. break;
  103. }
  104. of_node_put(dn);
  105. /* Affinity to CPU0 if no cpu nodes are found */
  106. return found ? cpu : 0;
  107. }
  108. EXPORT_SYMBOL_GPL(of_coresight_get_cpu);
  109. struct coresight_platform_data *
  110. of_get_coresight_platform_data(struct device *dev,
  111. const struct device_node *node)
  112. {
  113. int i = 0, ret = 0;
  114. struct coresight_platform_data *pdata;
  115. struct of_endpoint endpoint, rendpoint;
  116. struct device *rdev;
  117. struct device_node *ep = NULL;
  118. struct device_node *rparent = NULL;
  119. struct device_node *rport = NULL;
  120. pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
  121. if (!pdata)
  122. return ERR_PTR(-ENOMEM);
  123. /* Use device name as sysfs handle */
  124. pdata->name = dev_name(dev);
  125. /* Get the number of input and output port for this component */
  126. of_coresight_get_ports(node, &pdata->nr_inport, &pdata->nr_outport);
  127. if (pdata->nr_outport) {
  128. ret = of_coresight_alloc_memory(dev, pdata);
  129. if (ret)
  130. return ERR_PTR(ret);
  131. /* Iterate through each port to discover topology */
  132. do {
  133. /* Get a handle on a port */
  134. ep = of_graph_get_next_endpoint(node, ep);
  135. if (!ep)
  136. break;
  137. /*
  138. * No need to deal with input ports, processing for as
  139. * processing for output ports will deal with them.
  140. */
  141. if (of_find_property(ep, "slave-mode", NULL))
  142. continue;
  143. /* Get a handle on the local endpoint */
  144. ret = of_graph_parse_endpoint(ep, &endpoint);
  145. if (ret)
  146. continue;
  147. /* The local out port number */
  148. pdata->outports[i] = endpoint.port;
  149. /*
  150. * Get a handle on the remote port and parent
  151. * attached to it.
  152. */
  153. rparent = of_graph_get_remote_port_parent(ep);
  154. rport = of_graph_get_remote_port(ep);
  155. if (!rparent || !rport)
  156. continue;
  157. if (of_graph_parse_endpoint(rport, &rendpoint))
  158. continue;
  159. rdev = of_coresight_get_endpoint_device(rparent);
  160. if (!rdev)
  161. return ERR_PTR(-EPROBE_DEFER);
  162. pdata->child_names[i] = dev_name(rdev);
  163. pdata->child_ports[i] = rendpoint.id;
  164. i++;
  165. } while (ep);
  166. }
  167. pdata->cpu = of_coresight_get_cpu(node);
  168. return pdata;
  169. }
  170. EXPORT_SYMBOL_GPL(of_get_coresight_platform_data);