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