Shashank Mittal | b237257 | 2016-01-25 23:17:44 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2012, 2016 The Linux Foundation. All rights reserved. |
Pratik Patel | a06ae86 | 2014-11-03 11:07:35 -0700 | [diff] [blame] | 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 | |
Pratik Patel | a06ae86 | 2014-11-03 11:07:35 -0700 | [diff] [blame] | 13 | #include <linux/types.h> |
| 14 | #include <linux/err.h> |
| 15 | #include <linux/slab.h> |
| 16 | #include <linux/clk.h> |
| 17 | #include <linux/of.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> |
Mathieu Poirier | 8c02a5b | 2015-03-30 14:13:34 -0600 | [diff] [blame] | 24 | #include <linux/cpumask.h> |
Pratik Patel | a06ae86 | 2014-11-03 11:07:35 -0700 | [diff] [blame] | 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 | /* |
Pankaj Dubey | 4370123 | 2015-05-19 10:55:20 -0600 | [diff] [blame] | 39 | * If we have a non-configurable replicator, it will be found on the |
Pratik Patel | a06ae86 | 2014-11-03 11:07:35 -0700 | [diff] [blame] | 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 | |
Pratik Patel | a06ae86 | 2014-11-03 11:07:35 -0700 | [diff] [blame] | 55 | static void of_coresight_get_ports(struct device_node *node, |
| 56 | int *nr_inport, int *nr_outport) |
| 57 | { |
| 58 | struct device_node *ep = NULL; |
| 59 | int in = 0, out = 0; |
| 60 | |
| 61 | do { |
Philipp Zabel | f033c0b | 2014-12-01 13:32:32 +0100 | [diff] [blame] | 62 | ep = of_graph_get_next_endpoint(node, ep); |
Pratik Patel | a06ae86 | 2014-11-03 11:07:35 -0700 | [diff] [blame] | 63 | if (!ep) |
| 64 | break; |
| 65 | |
| 66 | if (of_property_read_bool(ep, "slave-mode")) |
| 67 | in++; |
| 68 | else |
| 69 | out++; |
| 70 | |
| 71 | } while (ep); |
| 72 | |
| 73 | *nr_inport = in; |
| 74 | *nr_outport = out; |
| 75 | } |
| 76 | |
| 77 | static int of_coresight_alloc_memory(struct device *dev, |
| 78 | struct coresight_platform_data *pdata) |
| 79 | { |
| 80 | /* List of output port on this component */ |
| 81 | pdata->outports = devm_kzalloc(dev, pdata->nr_outport * |
| 82 | sizeof(*pdata->outports), |
| 83 | GFP_KERNEL); |
| 84 | if (!pdata->outports) |
| 85 | return -ENOMEM; |
| 86 | |
Kaixu Xia | f7c5529 | 2015-01-09 16:57:21 -0700 | [diff] [blame] | 87 | /* Children connected to this component via @outports */ |
Eric Long | bf16e5b | 2016-02-17 17:51:43 -0700 | [diff] [blame] | 88 | pdata->child_names = devm_kzalloc(dev, pdata->nr_outport * |
Pratik Patel | a06ae86 | 2014-11-03 11:07:35 -0700 | [diff] [blame] | 89 | sizeof(*pdata->child_names), |
| 90 | GFP_KERNEL); |
| 91 | if (!pdata->child_names) |
| 92 | return -ENOMEM; |
| 93 | |
| 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), |
| 97 | GFP_KERNEL); |
| 98 | if (!pdata->child_ports) |
| 99 | return -ENOMEM; |
| 100 | |
| 101 | return 0; |
| 102 | } |
| 103 | |
| 104 | struct coresight_platform_data *of_get_coresight_platform_data( |
| 105 | struct device *dev, struct device_node *node) |
| 106 | { |
Mathieu Poirier | 8c02a5b | 2015-03-30 14:13:34 -0600 | [diff] [blame] | 107 | int i = 0, ret = 0, cpu; |
Pratik Patel | a06ae86 | 2014-11-03 11:07:35 -0700 | [diff] [blame] | 108 | struct coresight_platform_data *pdata; |
| 109 | struct of_endpoint endpoint, rendpoint; |
| 110 | struct device *rdev; |
Mathieu Poirier | 34a03c1 | 2015-01-26 09:22:23 -0700 | [diff] [blame] | 111 | struct device_node *dn; |
Pratik Patel | a06ae86 | 2014-11-03 11:07:35 -0700 | [diff] [blame] | 112 | struct device_node *ep = NULL; |
| 113 | struct device_node *rparent = NULL; |
| 114 | struct device_node *rport = NULL; |
| 115 | |
| 116 | pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL); |
| 117 | if (!pdata) |
| 118 | return ERR_PTR(-ENOMEM); |
| 119 | |
Shashank Mittal | b237257 | 2016-01-25 23:17:44 -0800 | [diff] [blame] | 120 | ret = of_property_read_string(node, "coresight-name", &pdata->name); |
| 121 | if (ret) { |
| 122 | /* Use device name as sysfs handle */ |
| 123 | pdata->name = dev_name(dev); |
| 124 | } |
Pratik Patel | a06ae86 | 2014-11-03 11:07:35 -0700 | [diff] [blame] | 125 | |
| 126 | /* Get the number of input and output port for this component */ |
| 127 | of_coresight_get_ports(node, &pdata->nr_inport, &pdata->nr_outport); |
| 128 | |
| 129 | if (pdata->nr_outport) { |
| 130 | ret = of_coresight_alloc_memory(dev, pdata); |
| 131 | if (ret) |
| 132 | return ERR_PTR(ret); |
| 133 | |
| 134 | /* Iterate through each port to discover topology */ |
| 135 | do { |
| 136 | /* Get a handle on a port */ |
Philipp Zabel | f033c0b | 2014-12-01 13:32:32 +0100 | [diff] [blame] | 137 | ep = of_graph_get_next_endpoint(node, ep); |
Pratik Patel | a06ae86 | 2014-11-03 11:07:35 -0700 | [diff] [blame] | 138 | if (!ep) |
| 139 | break; |
| 140 | |
| 141 | /* |
| 142 | * No need to deal with input ports, processing for as |
| 143 | * processing for output ports will deal with them. |
| 144 | */ |
| 145 | if (of_find_property(ep, "slave-mode", NULL)) |
| 146 | continue; |
| 147 | |
| 148 | /* Get a handle on the local endpoint */ |
| 149 | ret = of_graph_parse_endpoint(ep, &endpoint); |
| 150 | |
| 151 | if (ret) |
| 152 | continue; |
| 153 | |
| 154 | /* The local out port number */ |
| 155 | pdata->outports[i] = endpoint.id; |
| 156 | |
| 157 | /* |
| 158 | * Get a handle on the remote port and parent |
| 159 | * attached to it. |
| 160 | */ |
| 161 | rparent = of_graph_get_remote_port_parent(ep); |
| 162 | rport = of_graph_get_remote_port(ep); |
| 163 | |
| 164 | if (!rparent || !rport) |
| 165 | continue; |
| 166 | |
| 167 | if (of_graph_parse_endpoint(rport, &rendpoint)) |
| 168 | continue; |
| 169 | |
| 170 | rdev = of_coresight_get_endpoint_device(rparent); |
Kaixu Xia | 2ccffaf | 2015-01-09 16:57:22 -0700 | [diff] [blame] | 171 | if (!rdev) |
Mathieu Poirier | 62d39ac | 2016-08-25 15:19:01 -0600 | [diff] [blame] | 172 | return ERR_PTR(-EPROBE_DEFER); |
Pratik Patel | a06ae86 | 2014-11-03 11:07:35 -0700 | [diff] [blame] | 173 | |
Shashank Mittal | b237257 | 2016-01-25 23:17:44 -0800 | [diff] [blame] | 174 | ret = of_property_read_string(rparent, "coresight-name", |
| 175 | &pdata->child_names[i]); |
| 176 | if (ret) |
| 177 | pdata->child_names[i] = dev_name(rdev); |
| 178 | |
Pratik Patel | a06ae86 | 2014-11-03 11:07:35 -0700 | [diff] [blame] | 179 | pdata->child_ports[i] = rendpoint.id; |
| 180 | |
| 181 | i++; |
| 182 | } while (ep); |
| 183 | } |
| 184 | |
| 185 | /* Affinity defaults to CPU0 */ |
| 186 | pdata->cpu = 0; |
Mathieu Poirier | 34a03c1 | 2015-01-26 09:22:23 -0700 | [diff] [blame] | 187 | dn = of_parse_phandle(node, "cpu", 0); |
Mathieu Poirier | 8c02a5b | 2015-03-30 14:13:34 -0600 | [diff] [blame] | 188 | for (cpu = 0; dn && cpu < nr_cpu_ids; cpu++) { |
| 189 | if (dn == of_get_cpu_node(cpu, NULL)) { |
| 190 | pdata->cpu = cpu; |
| 191 | break; |
Pratik Patel | a06ae86 | 2014-11-03 11:07:35 -0700 | [diff] [blame] | 192 | } |
| 193 | } |
Peter Chen | 7f73b0b | 2016-08-25 15:19:04 -0600 | [diff] [blame] | 194 | of_node_put(dn); |
Pratik Patel | a06ae86 | 2014-11-03 11:07:35 -0700 | [diff] [blame] | 195 | |
| 196 | return pdata; |
| 197 | } |
| 198 | EXPORT_SYMBOL_GPL(of_get_coresight_platform_data); |