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