1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2012, The Linux Foundation. All rights reserved.
6 #include <linux/types.h>
8 #include <linux/slab.h>
11 #include <linux/of_address.h>
12 #include <linux/of_graph.h>
13 #include <linux/of_platform.h>
14 #include <linux/platform_device.h>
15 #include <linux/amba/bus.h>
16 #include <linux/coresight.h>
17 #include <linux/cpumask.h>
18 #include <asm/smp_plat.h>
21 static int of_dev_node_match(struct device *dev, void *data)
23 return dev->of_node == data;
26 static struct device *
27 of_coresight_get_endpoint_device(struct device_node *endpoint)
29 struct device *dev = NULL;
32 * If we have a non-configurable replicator, it will be found on the
35 dev = bus_find_device(&platform_bus_type, NULL,
36 endpoint, of_dev_node_match);
41 * We have a configurable component - circle through the AMBA bus
42 * looking for the device that matches the endpoint node.
44 return bus_find_device(&amba_bustype, NULL,
45 endpoint, of_dev_node_match);
48 static void of_coresight_get_ports(const struct device_node *node,
49 int *nr_inport, int *nr_outport)
51 struct device_node *ep = NULL;
55 ep = of_graph_get_next_endpoint(node, ep);
59 if (of_property_read_bool(ep, "slave-mode"))
70 static int of_coresight_alloc_memory(struct device *dev,
71 struct coresight_platform_data *pdata)
73 /* List of output port on this component */
74 pdata->outports = devm_kcalloc(dev,
76 sizeof(*pdata->outports),
81 /* Children connected to this component via @outports */
82 pdata->child_names = devm_kcalloc(dev,
84 sizeof(*pdata->child_names),
86 if (!pdata->child_names)
89 /* Port number on the child this component is connected to */
90 pdata->child_ports = devm_kcalloc(dev,
92 sizeof(*pdata->child_ports),
94 if (!pdata->child_ports)
100 int of_coresight_get_cpu(const struct device_node *node)
103 struct device_node *dn;
105 dn = of_parse_phandle(node, "cpu", 0);
106 /* Affinity defaults to CPU0 */
109 cpu = of_cpu_node_to_id(dn);
112 /* Affinity to CPU0 if no cpu nodes are found */
113 return (cpu < 0) ? 0 : cpu;
115 EXPORT_SYMBOL_GPL(of_coresight_get_cpu);
117 struct coresight_platform_data *
118 of_get_coresight_platform_data(struct device *dev,
119 const struct device_node *node)
122 struct coresight_platform_data *pdata;
123 struct of_endpoint endpoint, rendpoint;
125 struct device_node *ep = NULL;
126 struct device_node *rparent = NULL;
127 struct device_node *rport = NULL;
129 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
131 return ERR_PTR(-ENOMEM);
133 /* Use device name as sysfs handle */
134 pdata->name = dev_name(dev);
136 /* Get the number of input and output port for this component */
137 of_coresight_get_ports(node, &pdata->nr_inport, &pdata->nr_outport);
139 if (pdata->nr_outport) {
140 ret = of_coresight_alloc_memory(dev, pdata);
144 /* Iterate through each port to discover topology */
146 /* Get a handle on a port */
147 ep = of_graph_get_next_endpoint(node, ep);
152 * No need to deal with input ports, processing for as
153 * processing for output ports will deal with them.
155 if (of_find_property(ep, "slave-mode", NULL))
158 /* Get a handle on the local endpoint */
159 ret = of_graph_parse_endpoint(ep, &endpoint);
164 /* The local out port number */
165 pdata->outports[i] = endpoint.port;
168 * Get a handle on the remote port and parent
171 rparent = of_graph_get_remote_port_parent(ep);
172 rport = of_graph_get_remote_port(ep);
174 if (!rparent || !rport)
177 if (of_graph_parse_endpoint(rport, &rendpoint))
180 rdev = of_coresight_get_endpoint_device(rparent);
182 return ERR_PTR(-EPROBE_DEFER);
184 pdata->child_names[i] = dev_name(rdev);
185 pdata->child_ports[i] = rendpoint.id;
191 pdata->cpu = of_coresight_get_cpu(node);
195 EXPORT_SYMBOL_GPL(of_get_coresight_platform_data);