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/types.h>
14 #include <linux/err.h>
15 #include <linux/slab.h>
16 #include <linux/clk.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>
28 static int of_dev_node_match(struct device
*dev
, void *data
)
30 return dev
->of_node
== data
;
33 static struct device
*
34 of_coresight_get_endpoint_device(struct device_node
*endpoint
)
36 struct device
*dev
= NULL
;
39 * If we have a non-configurable replicator, it will be found on the
42 dev
= bus_find_device(&platform_bus_type
, NULL
,
43 endpoint
, of_dev_node_match
);
48 * We have a configurable component - circle through the AMBA bus
49 * looking for the device that matches the endpoint node.
51 return bus_find_device(&amba_bustype
, NULL
,
52 endpoint
, of_dev_node_match
);
55 static void of_coresight_get_ports(const struct device_node
*node
,
56 int *nr_inport
, int *nr_outport
)
58 struct device_node
*ep
= NULL
;
62 ep
= of_graph_get_next_endpoint(node
, ep
);
66 if (of_property_read_bool(ep
, "slave-mode"))
77 static int of_coresight_alloc_memory(struct device
*dev
,
78 struct coresight_platform_data
*pdata
)
80 /* List of output port on this component */
81 pdata
->outports
= devm_kzalloc(dev
, pdata
->nr_outport
*
82 sizeof(*pdata
->outports
),
87 /* Children connected to this component via @outports */
88 pdata
->child_names
= devm_kzalloc(dev
, pdata
->nr_outport
*
89 sizeof(*pdata
->child_names
),
91 if (!pdata
->child_names
)
94 /* Port number on the child this component is connected to */
95 pdata
->child_ports
= devm_kzalloc(dev
, pdata
->nr_outport
*
96 sizeof(*pdata
->child_ports
),
98 if (!pdata
->child_ports
)
104 int of_coresight_get_cpu(const struct device_node
*node
)
108 struct device_node
*dn
, *np
;
110 dn
= of_parse_phandle(node
, "cpu", 0);
112 /* Affinity defaults to CPU0 */
116 for_each_possible_cpu(cpu
) {
117 np
= of_cpu_device_node_get(cpu
);
125 /* Affinity to CPU0 if no cpu nodes are found */
126 return found
? cpu
: 0;
128 EXPORT_SYMBOL_GPL(of_coresight_get_cpu
);
130 struct coresight_platform_data
*
131 of_get_coresight_platform_data(struct device
*dev
,
132 const struct device_node
*node
)
135 struct coresight_platform_data
*pdata
;
136 struct of_endpoint endpoint
, rendpoint
;
138 struct device_node
*ep
= NULL
;
139 struct device_node
*rparent
= NULL
;
140 struct device_node
*rport
= NULL
;
142 pdata
= devm_kzalloc(dev
, sizeof(*pdata
), GFP_KERNEL
);
144 return ERR_PTR(-ENOMEM
);
146 /* Use device name as sysfs handle */
147 pdata
->name
= dev_name(dev
);
149 /* Get the number of input and output port for this component */
150 of_coresight_get_ports(node
, &pdata
->nr_inport
, &pdata
->nr_outport
);
152 if (pdata
->nr_outport
) {
153 ret
= of_coresight_alloc_memory(dev
, pdata
);
157 /* Iterate through each port to discover topology */
159 /* Get a handle on a port */
160 ep
= of_graph_get_next_endpoint(node
, ep
);
165 * No need to deal with input ports, processing for as
166 * processing for output ports will deal with them.
168 if (of_find_property(ep
, "slave-mode", NULL
))
171 /* Get a handle on the local endpoint */
172 ret
= of_graph_parse_endpoint(ep
, &endpoint
);
177 /* The local out port number */
178 pdata
->outports
[i
] = endpoint
.port
;
181 * Get a handle on the remote port and parent
184 rparent
= of_graph_get_remote_port_parent(ep
);
185 rport
= of_graph_get_remote_port(ep
);
187 if (!rparent
|| !rport
)
190 if (of_graph_parse_endpoint(rport
, &rendpoint
))
193 rdev
= of_coresight_get_endpoint_device(rparent
);
195 return ERR_PTR(-EPROBE_DEFER
);
197 pdata
->child_names
[i
] = dev_name(rdev
);
198 pdata
->child_ports
[i
] = rendpoint
.id
;
204 pdata
->cpu
= of_coresight_get_cpu(node
);
208 EXPORT_SYMBOL_GPL(of_get_coresight_platform_data
);