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/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(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. struct coresight_platform_data *of_get_coresight_platform_data(
  89. struct device *dev, struct device_node *node)
  90. {
  91. int i = 0, ret = 0, cpu;
  92. struct coresight_platform_data *pdata;
  93. struct of_endpoint endpoint, rendpoint;
  94. struct device *rdev;
  95. struct device_node *dn;
  96. struct device_node *ep = NULL;
  97. struct device_node *rparent = NULL;
  98. struct device_node *rport = NULL;
  99. pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
  100. if (!pdata)
  101. return ERR_PTR(-ENOMEM);
  102. /* Use device name as sysfs handle */
  103. pdata->name = dev_name(dev);
  104. /* Get the number of input and output port for this component */
  105. of_coresight_get_ports(node, &pdata->nr_inport, &pdata->nr_outport);
  106. if (pdata->nr_outport) {
  107. ret = of_coresight_alloc_memory(dev, pdata);
  108. if (ret)
  109. return ERR_PTR(ret);
  110. /* Iterate through each port to discover topology */
  111. do {
  112. /* Get a handle on a port */
  113. ep = of_graph_get_next_endpoint(node, ep);
  114. if (!ep)
  115. break;
  116. /*
  117. * No need to deal with input ports, processing for as
  118. * processing for output ports will deal with them.
  119. */
  120. if (of_find_property(ep, "slave-mode", NULL))
  121. continue;
  122. /* Get a handle on the local endpoint */
  123. ret = of_graph_parse_endpoint(ep, &endpoint);
  124. if (ret)
  125. continue;
  126. /* The local out port number */
  127. pdata->outports[i] = endpoint.id;
  128. /*
  129. * Get a handle on the remote port and parent
  130. * attached to it.
  131. */
  132. rparent = of_graph_get_remote_port_parent(ep);
  133. rport = of_graph_get_remote_port(ep);
  134. if (!rparent || !rport)
  135. continue;
  136. if (of_graph_parse_endpoint(rport, &rendpoint))
  137. continue;
  138. rdev = of_coresight_get_endpoint_device(rparent);
  139. if (!rdev)
  140. return ERR_PTR(-EPROBE_DEFER);
  141. pdata->child_names[i] = dev_name(rdev);
  142. pdata->child_ports[i] = rendpoint.id;
  143. i++;
  144. } while (ep);
  145. }
  146. /* Affinity defaults to CPU0 */
  147. pdata->cpu = 0;
  148. dn = of_parse_phandle(node, "cpu", 0);
  149. for (cpu = 0; dn && cpu < nr_cpu_ids; cpu++) {
  150. if (dn == of_get_cpu_node(cpu, NULL)) {
  151. pdata->cpu = cpu;
  152. break;
  153. }
  154. }
  155. of_node_put(dn);
  156. return pdata;
  157. }
  158. EXPORT_SYMBOL_GPL(of_get_coresight_platform_data);