of_coresight.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 <linux/cpumask.h>
  25. #include <asm/smp_plat.h>
  26. static int of_dev_node_match(struct device *dev, void *data)
  27. {
  28. return dev->of_node == data;
  29. }
  30. static struct device *
  31. of_coresight_get_endpoint_device(struct device_node *endpoint)
  32. {
  33. struct device *dev = NULL;
  34. /*
  35. * If we have a non-configurable replicator, it will be found on the
  36. * platform bus.
  37. */
  38. dev = bus_find_device(&platform_bus_type, NULL,
  39. endpoint, of_dev_node_match);
  40. if (dev)
  41. return dev;
  42. /*
  43. * We have a configurable component - circle through the AMBA bus
  44. * looking for the device that matches the endpoint node.
  45. */
  46. return bus_find_device(&amba_bustype, NULL,
  47. endpoint, of_dev_node_match);
  48. }
  49. static void of_coresight_get_ports(struct device_node *node,
  50. int *nr_inport, int *nr_outport)
  51. {
  52. struct device_node *ep = NULL;
  53. int in = 0, out = 0;
  54. do {
  55. ep = of_graph_get_next_endpoint(node, ep);
  56. if (!ep)
  57. break;
  58. if (of_property_read_bool(ep, "slave-mode"))
  59. in++;
  60. else
  61. out++;
  62. } while (ep);
  63. *nr_inport = in;
  64. *nr_outport = out;
  65. }
  66. static int of_coresight_alloc_memory(struct device *dev,
  67. struct coresight_platform_data *pdata)
  68. {
  69. /* List of output port on this component */
  70. pdata->outports = devm_kzalloc(dev, pdata->nr_outport *
  71. sizeof(*pdata->outports),
  72. GFP_KERNEL);
  73. if (!pdata->outports)
  74. return -ENOMEM;
  75. /* Children connected to this component via @outports */
  76. pdata->child_names = devm_kzalloc(dev, pdata->nr_outport *
  77. sizeof(*pdata->child_names),
  78. GFP_KERNEL);
  79. if (!pdata->child_names)
  80. return -ENOMEM;
  81. /* Port number on the child this component is connected to */
  82. pdata->child_ports = devm_kzalloc(dev, pdata->nr_outport *
  83. sizeof(*pdata->child_ports),
  84. GFP_KERNEL);
  85. if (!pdata->child_ports)
  86. return -ENOMEM;
  87. return 0;
  88. }
  89. struct coresight_platform_data *of_get_coresight_platform_data(
  90. struct device *dev, struct device_node *node)
  91. {
  92. int i = 0, ret = 0, cpu;
  93. struct coresight_platform_data *pdata;
  94. struct of_endpoint endpoint, rendpoint;
  95. struct device *rdev;
  96. struct device_node *dn;
  97. struct device_node *ep = NULL;
  98. struct device_node *rparent = NULL;
  99. struct device_node *rport = NULL;
  100. pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
  101. if (!pdata)
  102. return ERR_PTR(-ENOMEM);
  103. /* Use device name as sysfs handle */
  104. pdata->name = dev_name(dev);
  105. /* Get the number of input and output port for this component */
  106. of_coresight_get_ports(node, &pdata->nr_inport, &pdata->nr_outport);
  107. if (pdata->nr_outport) {
  108. ret = of_coresight_alloc_memory(dev, pdata);
  109. if (ret)
  110. return ERR_PTR(ret);
  111. /* Iterate through each port to discover topology */
  112. do {
  113. /* Get a handle on a port */
  114. ep = of_graph_get_next_endpoint(node, ep);
  115. if (!ep)
  116. break;
  117. /*
  118. * No need to deal with input ports, processing for as
  119. * processing for output ports will deal with them.
  120. */
  121. if (of_find_property(ep, "slave-mode", NULL))
  122. continue;
  123. /* Get a handle on the local endpoint */
  124. ret = of_graph_parse_endpoint(ep, &endpoint);
  125. if (ret)
  126. continue;
  127. /* The local out port number */
  128. pdata->outports[i] = endpoint.id;
  129. /*
  130. * Get a handle on the remote port and parent
  131. * attached to it.
  132. */
  133. rparent = of_graph_get_remote_port_parent(ep);
  134. rport = of_graph_get_remote_port(ep);
  135. if (!rparent || !rport)
  136. continue;
  137. if (of_graph_parse_endpoint(rport, &rendpoint))
  138. continue;
  139. rdev = of_coresight_get_endpoint_device(rparent);
  140. if (!rdev)
  141. continue;
  142. pdata->child_names[i] = dev_name(rdev);
  143. pdata->child_ports[i] = rendpoint.id;
  144. i++;
  145. } while (ep);
  146. }
  147. /* Affinity defaults to CPU0 */
  148. pdata->cpu = 0;
  149. dn = of_parse_phandle(node, "cpu", 0);
  150. for (cpu = 0; dn && cpu < nr_cpu_ids; cpu++) {
  151. if (dn == of_get_cpu_node(cpu, NULL)) {
  152. pdata->cpu = cpu;
  153. break;
  154. }
  155. }
  156. return pdata;
  157. }
  158. EXPORT_SYMBOL_GPL(of_get_coresight_platform_data);