of_coresight.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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/module.h>
  13. #include <linux/types.h>
  14. #include <linux/err.h>
  15. #include <linux/slab.h>
  16. #include <linux/clk.h>
  17. #include <linux/of.h>
  18. #include <linux/of_address.h>
  19. #include <linux/of_graph.h>
  20. #include <linux/of_platform.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/amba/bus.h>
  23. #include <linux/coresight.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-configuable 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 struct device_node *of_get_coresight_endpoint(
  49. const struct device_node *parent, struct device_node *prev)
  50. {
  51. struct device_node *node = of_graph_get_next_endpoint(parent, prev);
  52. of_node_put(prev);
  53. return node;
  54. }
  55. static void of_coresight_get_ports(struct device_node *node,
  56. int *nr_inport, int *nr_outport)
  57. {
  58. struct device_node *ep = NULL;
  59. int in = 0, out = 0;
  60. do {
  61. ep = of_get_coresight_endpoint(node, ep);
  62. if (!ep)
  63. break;
  64. if (of_property_read_bool(ep, "slave-mode"))
  65. in++;
  66. else
  67. out++;
  68. } while (ep);
  69. *nr_inport = in;
  70. *nr_outport = out;
  71. }
  72. static int of_coresight_alloc_memory(struct device *dev,
  73. struct coresight_platform_data *pdata)
  74. {
  75. /* List of output port on this component */
  76. pdata->outports = devm_kzalloc(dev, pdata->nr_outport *
  77. sizeof(*pdata->outports),
  78. GFP_KERNEL);
  79. if (!pdata->outports)
  80. return -ENOMEM;
  81. /* Children connected to this component via @outport */
  82. pdata->child_names = devm_kzalloc(dev, pdata->nr_outport *
  83. sizeof(*pdata->child_names),
  84. GFP_KERNEL);
  85. if (!pdata->child_names)
  86. return -ENOMEM;
  87. /* Port number on the child this component is connected to */
  88. pdata->child_ports = devm_kzalloc(dev, pdata->nr_outport *
  89. sizeof(*pdata->child_ports),
  90. GFP_KERNEL);
  91. if (!pdata->child_ports)
  92. return -ENOMEM;
  93. return 0;
  94. }
  95. struct coresight_platform_data *of_get_coresight_platform_data(
  96. struct device *dev, struct device_node *node)
  97. {
  98. int i = 0, ret = 0;
  99. struct coresight_platform_data *pdata;
  100. struct of_endpoint endpoint, rendpoint;
  101. struct device *rdev;
  102. struct device_node *cpu;
  103. struct device_node *ep = NULL;
  104. struct device_node *rparent = NULL;
  105. struct device_node *rport = NULL;
  106. pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
  107. if (!pdata)
  108. return ERR_PTR(-ENOMEM);
  109. /* Use device name as debugfs handle */
  110. pdata->name = dev_name(dev);
  111. /* Get the number of input and output port for this component */
  112. of_coresight_get_ports(node, &pdata->nr_inport, &pdata->nr_outport);
  113. if (pdata->nr_outport) {
  114. ret = of_coresight_alloc_memory(dev, pdata);
  115. if (ret)
  116. return ERR_PTR(ret);
  117. /* Iterate through each port to discover topology */
  118. do {
  119. /* Get a handle on a port */
  120. ep = of_get_coresight_endpoint(node, ep);
  121. if (!ep)
  122. break;
  123. /*
  124. * No need to deal with input ports, processing for as
  125. * processing for output ports will deal with them.
  126. */
  127. if (of_find_property(ep, "slave-mode", NULL))
  128. continue;
  129. /* Get a handle on the local endpoint */
  130. ret = of_graph_parse_endpoint(ep, &endpoint);
  131. if (ret)
  132. continue;
  133. /* The local out port number */
  134. pdata->outports[i] = endpoint.id;
  135. /*
  136. * Get a handle on the remote port and parent
  137. * attached to it.
  138. */
  139. rparent = of_graph_get_remote_port_parent(ep);
  140. rport = of_graph_get_remote_port(ep);
  141. if (!rparent || !rport)
  142. continue;
  143. if (of_graph_parse_endpoint(rport, &rendpoint))
  144. continue;
  145. rdev = of_coresight_get_endpoint_device(rparent);
  146. if (!dev)
  147. continue;
  148. pdata->child_names[i] = dev_name(rdev);
  149. pdata->child_ports[i] = rendpoint.id;
  150. i++;
  151. } while (ep);
  152. }
  153. /* Affinity defaults to CPU0 */
  154. pdata->cpu = 0;
  155. cpu = of_parse_phandle(node, "cpu", 0);
  156. if (cpu) {
  157. const u32 *mpidr;
  158. int len, index;
  159. mpidr = of_get_property(cpu, "reg", &len);
  160. if (mpidr && len == 4) {
  161. index = get_logical_index(be32_to_cpup(mpidr));
  162. if (index != -EINVAL)
  163. pdata->cpu = index;
  164. }
  165. }
  166. return pdata;
  167. }
  168. EXPORT_SYMBOL_GPL(of_get_coresight_platform_data);