1 /* Copyright (c) 2012, The Linux Foundation. All rights reserved.
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.
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.
13 #include <linux/module.h>
14 #include <linux/types.h>
15 #include <linux/err.h>
16 #include <linux/slab.h>
17 #include <linux/clk.h>
19 #include <linux/of_address.h>
20 #include <linux/of_graph.h>
21 #include <linux/of_platform.h>
22 #include <linux/platform_device.h>
23 #include <linux/amba/bus.h>
24 #include <linux/coresight.h>
25 #include <linux/cpumask.h>
26 #include <asm/smp_plat.h>
29 static int of_dev_node_match(struct device
*dev
, void *data
)
31 return dev
->of_node
== data
;
34 static struct device
*
35 of_coresight_get_endpoint_device(struct device_node
*endpoint
)
37 struct device
*dev
= NULL
;
40 * If we have a non-configuable replicator, it will be found on the
43 dev
= bus_find_device(&platform_bus_type
, NULL
,
44 endpoint
, of_dev_node_match
);
49 * We have a configurable component - circle through the AMBA bus
50 * looking for the device that matches the endpoint node.
52 return bus_find_device(&amba_bustype
, NULL
,
53 endpoint
, of_dev_node_match
);
56 static void of_coresight_get_ports(struct device_node
*node
,
57 int *nr_inport
, int *nr_outport
)
59 struct device_node
*ep
= NULL
;
63 ep
= of_graph_get_next_endpoint(node
, ep
);
67 if (of_property_read_bool(ep
, "slave-mode"))
78 static int of_coresight_alloc_memory(struct device
*dev
,
79 struct coresight_platform_data
*pdata
)
81 /* List of output port on this component */
82 pdata
->outports
= devm_kzalloc(dev
, pdata
->nr_outport
*
83 sizeof(*pdata
->outports
),
88 /* Children connected to this component via @outports */
89 pdata
->child_names
= devm_kzalloc(dev
, pdata
->nr_outport
*
90 sizeof(*pdata
->child_names
),
92 if (!pdata
->child_names
)
95 /* Port number on the child this component is connected to */
96 pdata
->child_ports
= devm_kzalloc(dev
, pdata
->nr_outport
*
97 sizeof(*pdata
->child_ports
),
99 if (!pdata
->child_ports
)
105 struct coresight_platform_data
*of_get_coresight_platform_data(
106 struct device
*dev
, struct device_node
*node
)
108 int i
= 0, ret
= 0, cpu
;
109 struct coresight_platform_data
*pdata
;
110 struct of_endpoint endpoint
, rendpoint
;
112 struct device_node
*dn
;
113 struct device_node
*ep
= NULL
;
114 struct device_node
*rparent
= NULL
;
115 struct device_node
*rport
= NULL
;
117 pdata
= devm_kzalloc(dev
, sizeof(*pdata
), GFP_KERNEL
);
119 return ERR_PTR(-ENOMEM
);
121 /* Use device name as sysfs handle */
122 pdata
->name
= dev_name(dev
);
124 /* Get the number of input and output port for this component */
125 of_coresight_get_ports(node
, &pdata
->nr_inport
, &pdata
->nr_outport
);
127 if (pdata
->nr_outport
) {
128 ret
= of_coresight_alloc_memory(dev
, pdata
);
132 /* Iterate through each port to discover topology */
134 /* Get a handle on a port */
135 ep
= of_graph_get_next_endpoint(node
, ep
);
140 * No need to deal with input ports, processing for as
141 * processing for output ports will deal with them.
143 if (of_find_property(ep
, "slave-mode", NULL
))
146 /* Get a handle on the local endpoint */
147 ret
= of_graph_parse_endpoint(ep
, &endpoint
);
152 /* The local out port number */
153 pdata
->outports
[i
] = endpoint
.id
;
156 * Get a handle on the remote port and parent
159 rparent
= of_graph_get_remote_port_parent(ep
);
160 rport
= of_graph_get_remote_port(ep
);
162 if (!rparent
|| !rport
)
165 if (of_graph_parse_endpoint(rport
, &rendpoint
))
168 rdev
= of_coresight_get_endpoint_device(rparent
);
172 pdata
->child_names
[i
] = dev_name(rdev
);
173 pdata
->child_ports
[i
] = rendpoint
.id
;
179 /* Affinity defaults to CPU0 */
181 dn
= of_parse_phandle(node
, "cpu", 0);
182 for (cpu
= 0; dn
&& cpu
< nr_cpu_ids
; cpu
++) {
183 if (dn
== of_get_cpu_node(cpu
, NULL
)) {
191 EXPORT_SYMBOL_GPL(of_get_coresight_platform_data
);